blob: 34b6bf939335b291f6b93e266e8c0c0e889b537b [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>
Peter Zijlstra029632f2011-10-25 10:00:11 +020033
34#include <trace/events/sched.h>
35
36#include "sched.h"
Patrick Bellasiedd28d32015-07-07 15:33:20 +010037#include "tune.h"
Arjan van de Ven97455122008-01-25 21:08:34 +010038
Ingo Molnarbf0f6f22007-07-09 18:51:58 +020039/*
Peter Zijlstra21805082007-08-25 18:41:53 +020040 * Targeted preemption latency for CPU-bound tasks:
Takuya Yoshikawa864616e2010-10-14 16:09:13 +090041 * (default: 6ms * (1 + ilog(ncpus)), units: nanoseconds)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +020042 *
Peter Zijlstra21805082007-08-25 18:41:53 +020043 * NOTE: this latency value is not the same as the concept of
Ingo Molnard274a4c2007-10-15 17:00:14 +020044 * 'timeslice length' - timeslices in CFS are of variable length
45 * and have no persistent notion like in traditional, time-slice
46 * based scheduling concepts.
Ingo Molnarbf0f6f22007-07-09 18:51:58 +020047 *
Ingo Molnard274a4c2007-10-15 17:00:14 +020048 * (to see the precise effective timeslice length of your workload,
49 * run vmstat and monitor the context-switches (cs) field)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +020050 */
Mike Galbraith21406922010-03-11 17:17:15 +010051unsigned int sysctl_sched_latency = 6000000ULL;
52unsigned int normalized_sysctl_sched_latency = 6000000ULL;
Ingo Molnar2bd8e6d2007-10-15 17:00:02 +020053
54/*
Christian Ehrhardt1983a922009-11-30 12:16:47 +010055 * The initial- and re-scaling of tunables is configurable
56 * (default SCHED_TUNABLESCALING_LOG = *(1+ilog(ncpus))
57 *
58 * Options are:
59 * SCHED_TUNABLESCALING_NONE - unscaled, always *1
60 * SCHED_TUNABLESCALING_LOG - scaled logarithmical, *1+ilog(ncpus)
61 * SCHED_TUNABLESCALING_LINEAR - scaled linear, *ncpus
62 */
63enum sched_tunable_scaling sysctl_sched_tunable_scaling
64 = SCHED_TUNABLESCALING_LOG;
65
66/*
Peter Zijlstrab2be5e92007-11-09 22:39:37 +010067 * Minimal preemption granularity for CPU-bound tasks:
Takuya Yoshikawa864616e2010-10-14 16:09:13 +090068 * (default: 0.75 msec * (1 + ilog(ncpus)), units: nanoseconds)
Peter Zijlstrab2be5e92007-11-09 22:39:37 +010069 */
Ingo Molnar0bf377bb2010-09-12 08:14:52 +020070unsigned int sysctl_sched_min_granularity = 750000ULL;
71unsigned int normalized_sysctl_sched_min_granularity = 750000ULL;
Peter Zijlstrab2be5e92007-11-09 22:39:37 +010072
73/*
74 * is kept at sysctl_sched_latency / sysctl_sched_min_granularity
75 */
Ingo Molnar0bf377bb2010-09-12 08:14:52 +020076static unsigned int sched_nr_latency = 8;
Peter Zijlstrab2be5e92007-11-09 22:39:37 +010077
78/*
Mike Galbraith2bba22c2009-09-09 15:41:37 +020079 * After fork, child runs first. If set to 0 (default) then
Ingo Molnar2bd8e6d2007-10-15 17:00:02 +020080 * parent will (try to) run first.
81 */
Mike Galbraith2bba22c2009-09-09 15:41:37 +020082unsigned int sysctl_sched_child_runs_first __read_mostly;
Peter Zijlstra21805082007-08-25 18:41:53 +020083
84/*
Ingo Molnarbf0f6f22007-07-09 18:51:58 +020085 * SCHED_OTHER wake-up granularity.
Mike Galbraith172e0822009-09-09 15:41:37 +020086 * (default: 1 msec * (1 + ilog(ncpus)), units: nanoseconds)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +020087 *
88 * This option delays the preemption effects of decoupled workloads
89 * and reduces their over-scheduling. Synchronous workloads will still
90 * have immediate wakeup/sleep latencies.
91 */
Mike Galbraith172e0822009-09-09 15:41:37 +020092unsigned int sysctl_sched_wakeup_granularity = 1000000UL;
Christian Ehrhardt0bcdcf22009-11-30 12:16:46 +010093unsigned int normalized_sysctl_sched_wakeup_granularity = 1000000UL;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +020094
Ingo Molnarda84d962007-10-15 17:00:18 +020095const_debug unsigned int sysctl_sched_migration_cost = 500000UL;
96
Paul Turnera7a4f8a2010-11-15 15:47:06 -080097/*
98 * The exponential sliding window over which load is averaged for shares
99 * distribution.
100 * (default: 10msec)
101 */
102unsigned int __read_mostly sysctl_sched_shares_window = 10000000UL;
103
Paul Turnerec12cb72011-07-21 09:43:30 -0700104#ifdef CONFIG_CFS_BANDWIDTH
105/*
106 * Amount of runtime to allocate from global (tg) to local (per-cfs_rq) pool
107 * each time a cfs_rq requests quota.
108 *
109 * Note: in the case that the slice exceeds the runtime remaining (either due
110 * to consumption or the quota being specified to be smaller than the slice)
111 * we will always only issue the remaining available time.
112 *
113 * default: 5 msec, units: microseconds
114 */
115unsigned int sysctl_sched_cfs_bandwidth_slice = 5000UL;
116#endif
117
Morten Rasmussen32731632016-07-25 14:34:26 +0100118/*
119 * The margin used when comparing utilization with CPU capacity:
120 * util * 1024 < capacity * margin
121 */
122unsigned int capacity_margin = 1280; /* ~20% */
123
Paul Gortmaker85276322013-04-19 15:10:50 -0400124static inline void update_load_add(struct load_weight *lw, unsigned long inc)
125{
126 lw->weight += inc;
127 lw->inv_weight = 0;
128}
129
130static inline void update_load_sub(struct load_weight *lw, unsigned long dec)
131{
132 lw->weight -= dec;
133 lw->inv_weight = 0;
134}
135
136static inline void update_load_set(struct load_weight *lw, unsigned long w)
137{
138 lw->weight = w;
139 lw->inv_weight = 0;
140}
141
Peter Zijlstra029632f2011-10-25 10:00:11 +0200142/*
143 * Increase the granularity value when there are more CPUs,
144 * because with more CPUs the 'effective latency' as visible
145 * to users decreases. But the relationship is not linear,
146 * so pick a second-best guess by going with the log2 of the
147 * number of CPUs.
148 *
149 * This idea comes from the SD scheduler of Con Kolivas:
150 */
Nicholas Mc Guire58ac93e2015-05-15 21:05:42 +0200151static unsigned int get_update_sysctl_factor(void)
Peter Zijlstra029632f2011-10-25 10:00:11 +0200152{
Nicholas Mc Guire58ac93e2015-05-15 21:05:42 +0200153 unsigned int cpus = min_t(unsigned int, num_online_cpus(), 8);
Peter Zijlstra029632f2011-10-25 10:00:11 +0200154 unsigned int factor;
155
156 switch (sysctl_sched_tunable_scaling) {
157 case SCHED_TUNABLESCALING_NONE:
158 factor = 1;
159 break;
160 case SCHED_TUNABLESCALING_LINEAR:
161 factor = cpus;
162 break;
163 case SCHED_TUNABLESCALING_LOG:
164 default:
165 factor = 1 + ilog2(cpus);
166 break;
167 }
168
169 return factor;
170}
171
172static void update_sysctl(void)
173{
174 unsigned int factor = get_update_sysctl_factor();
175
176#define SET_SYSCTL(name) \
177 (sysctl_##name = (factor) * normalized_sysctl_##name)
178 SET_SYSCTL(sched_min_granularity);
179 SET_SYSCTL(sched_latency);
180 SET_SYSCTL(sched_wakeup_granularity);
181#undef SET_SYSCTL
182}
183
184void sched_init_granularity(void)
185{
186 update_sysctl();
187}
188
Peter Zijlstra9dbdb152013-11-18 18:27:06 +0100189#define WMULT_CONST (~0U)
Peter Zijlstra029632f2011-10-25 10:00:11 +0200190#define WMULT_SHIFT 32
191
Peter Zijlstra9dbdb152013-11-18 18:27:06 +0100192static void __update_inv_weight(struct load_weight *lw)
Peter Zijlstra029632f2011-10-25 10:00:11 +0200193{
Peter Zijlstra9dbdb152013-11-18 18:27:06 +0100194 unsigned long w;
Peter Zijlstra029632f2011-10-25 10:00:11 +0200195
Peter Zijlstra9dbdb152013-11-18 18:27:06 +0100196 if (likely(lw->inv_weight))
197 return;
198
199 w = scale_load_down(lw->weight);
200
201 if (BITS_PER_LONG > 32 && unlikely(w >= WMULT_CONST))
202 lw->inv_weight = 1;
203 else if (unlikely(!w))
204 lw->inv_weight = WMULT_CONST;
Peter Zijlstra029632f2011-10-25 10:00:11 +0200205 else
Peter Zijlstra9dbdb152013-11-18 18:27:06 +0100206 lw->inv_weight = WMULT_CONST / w;
207}
Peter Zijlstra029632f2011-10-25 10:00:11 +0200208
Peter Zijlstra9dbdb152013-11-18 18:27:06 +0100209/*
210 * delta_exec * weight / lw.weight
211 * OR
212 * (delta_exec * (weight * lw->inv_weight)) >> WMULT_SHIFT
213 *
Yuyang Du1c3de5e2016-03-30 07:07:51 +0800214 * Either weight := NICE_0_LOAD and lw \e sched_prio_to_wmult[], in which case
Peter Zijlstra9dbdb152013-11-18 18:27:06 +0100215 * we're guaranteed shift stays positive because inv_weight is guaranteed to
216 * fit 32 bits, and NICE_0_LOAD gives another 10 bits; therefore shift >= 22.
217 *
218 * Or, weight =< lw.weight (because lw.weight is the runqueue weight), thus
219 * weight/lw.weight <= 1, and therefore our shift will also be positive.
220 */
221static u64 __calc_delta(u64 delta_exec, unsigned long weight, struct load_weight *lw)
222{
223 u64 fact = scale_load_down(weight);
224 int shift = WMULT_SHIFT;
Peter Zijlstra029632f2011-10-25 10:00:11 +0200225
Peter Zijlstra9dbdb152013-11-18 18:27:06 +0100226 __update_inv_weight(lw);
227
228 if (unlikely(fact >> 32)) {
229 while (fact >> 32) {
230 fact >>= 1;
231 shift--;
232 }
Peter Zijlstra029632f2011-10-25 10:00:11 +0200233 }
234
Peter Zijlstra9dbdb152013-11-18 18:27:06 +0100235 /* hint to use a 32x32->64 mul */
236 fact = (u64)(u32)fact * lw->inv_weight;
Peter Zijlstra029632f2011-10-25 10:00:11 +0200237
Peter Zijlstra9dbdb152013-11-18 18:27:06 +0100238 while (fact >> 32) {
239 fact >>= 1;
240 shift--;
241 }
242
243 return mul_u64_u32_shr(delta_exec, fact, shift);
Peter Zijlstra029632f2011-10-25 10:00:11 +0200244}
245
246
247const struct sched_class fair_sched_class;
Peter Zijlstraa4c2f002008-10-17 19:27:03 +0200248
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200249/**************************************************************
250 * CFS operations on generic schedulable entities:
251 */
252
253#ifdef CONFIG_FAIR_GROUP_SCHED
254
255/* cpu runqueue to which this cfs_rq is attached */
256static inline struct rq *rq_of(struct cfs_rq *cfs_rq)
257{
258 return cfs_rq->rq;
259}
260
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200261/* An entity is a task if it doesn't "own" a runqueue */
262#define entity_is_task(se) (!se->my_q)
263
Peter Zijlstra8f488942009-07-24 12:25:30 +0200264static inline struct task_struct *task_of(struct sched_entity *se)
265{
Peter Zijlstra9148a3a2016-09-20 22:34:51 +0200266 SCHED_WARN_ON(!entity_is_task(se));
Peter Zijlstra8f488942009-07-24 12:25:30 +0200267 return container_of(se, struct task_struct, se);
268}
269
Peter Zijlstrab7581492008-04-19 19:45:00 +0200270/* Walk up scheduling entities hierarchy */
271#define for_each_sched_entity(se) \
272 for (; se; se = se->parent)
273
274static inline struct cfs_rq *task_cfs_rq(struct task_struct *p)
275{
276 return p->se.cfs_rq;
277}
278
279/* runqueue on which this entity is (to be) queued */
280static inline struct cfs_rq *cfs_rq_of(struct sched_entity *se)
281{
282 return se->cfs_rq;
283}
284
285/* runqueue "owned" by this group */
286static inline struct cfs_rq *group_cfs_rq(struct sched_entity *grp)
287{
288 return grp->my_q;
289}
290
Peter Zijlstra3d4b47b2010-11-15 15:47:01 -0800291static inline void list_add_leaf_cfs_rq(struct cfs_rq *cfs_rq)
292{
293 if (!cfs_rq->on_list) {
Paul Turner67e86252010-11-15 15:47:05 -0800294 /*
295 * Ensure we either appear before our parent (if already
296 * enqueued) or force our parent to appear after us when it is
297 * enqueued. The fact that we always enqueue bottom-up
298 * reduces this to two cases.
299 */
300 if (cfs_rq->tg->parent &&
301 cfs_rq->tg->parent->cfs_rq[cpu_of(rq_of(cfs_rq))]->on_list) {
302 list_add_rcu(&cfs_rq->leaf_cfs_rq_list,
Peter Zijlstra3d4b47b2010-11-15 15:47:01 -0800303 &rq_of(cfs_rq)->leaf_cfs_rq_list);
Paul Turner67e86252010-11-15 15:47:05 -0800304 } else {
305 list_add_tail_rcu(&cfs_rq->leaf_cfs_rq_list,
306 &rq_of(cfs_rq)->leaf_cfs_rq_list);
307 }
Peter Zijlstra3d4b47b2010-11-15 15:47:01 -0800308
309 cfs_rq->on_list = 1;
310 }
311}
312
313static inline void list_del_leaf_cfs_rq(struct cfs_rq *cfs_rq)
314{
315 if (cfs_rq->on_list) {
316 list_del_rcu(&cfs_rq->leaf_cfs_rq_list);
317 cfs_rq->on_list = 0;
318 }
319}
320
Peter Zijlstrab7581492008-04-19 19:45:00 +0200321/* Iterate thr' all leaf cfs_rq's on a runqueue */
322#define for_each_leaf_cfs_rq(rq, cfs_rq) \
323 list_for_each_entry_rcu(cfs_rq, &rq->leaf_cfs_rq_list, leaf_cfs_rq_list)
324
325/* Do the two (enqueued) entities belong to the same group ? */
Peter Zijlstrafed14d42012-02-11 06:05:00 +0100326static inline struct cfs_rq *
Peter Zijlstrab7581492008-04-19 19:45:00 +0200327is_same_group(struct sched_entity *se, struct sched_entity *pse)
328{
329 if (se->cfs_rq == pse->cfs_rq)
Peter Zijlstrafed14d42012-02-11 06:05:00 +0100330 return se->cfs_rq;
Peter Zijlstrab7581492008-04-19 19:45:00 +0200331
Peter Zijlstrafed14d42012-02-11 06:05:00 +0100332 return NULL;
Peter Zijlstrab7581492008-04-19 19:45:00 +0200333}
334
335static inline struct sched_entity *parent_entity(struct sched_entity *se)
336{
337 return se->parent;
338}
339
Peter Zijlstra464b7522008-10-24 11:06:15 +0200340static void
341find_matching_se(struct sched_entity **se, struct sched_entity **pse)
342{
343 int se_depth, pse_depth;
344
345 /*
346 * preemption test can be made between sibling entities who are in the
347 * same cfs_rq i.e who have a common parent. Walk up the hierarchy of
348 * both tasks until we find their ancestors who are siblings of common
349 * parent.
350 */
351
352 /* First walk up until both entities are at same depth */
Peter Zijlstrafed14d42012-02-11 06:05:00 +0100353 se_depth = (*se)->depth;
354 pse_depth = (*pse)->depth;
Peter Zijlstra464b7522008-10-24 11:06:15 +0200355
356 while (se_depth > pse_depth) {
357 se_depth--;
358 *se = parent_entity(*se);
359 }
360
361 while (pse_depth > se_depth) {
362 pse_depth--;
363 *pse = parent_entity(*pse);
364 }
365
366 while (!is_same_group(*se, *pse)) {
367 *se = parent_entity(*se);
368 *pse = parent_entity(*pse);
369 }
370}
371
Peter Zijlstra8f488942009-07-24 12:25:30 +0200372#else /* !CONFIG_FAIR_GROUP_SCHED */
373
374static inline struct task_struct *task_of(struct sched_entity *se)
375{
376 return container_of(se, struct task_struct, se);
377}
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200378
379static inline struct rq *rq_of(struct cfs_rq *cfs_rq)
380{
381 return container_of(cfs_rq, struct rq, cfs);
382}
383
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200384#define entity_is_task(se) 1
385
Peter Zijlstrab7581492008-04-19 19:45:00 +0200386#define for_each_sched_entity(se) \
387 for (; se; se = NULL)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200388
Peter Zijlstrab7581492008-04-19 19:45:00 +0200389static inline struct cfs_rq *task_cfs_rq(struct task_struct *p)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200390{
Peter Zijlstrab7581492008-04-19 19:45:00 +0200391 return &task_rq(p)->cfs;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200392}
393
Peter Zijlstrab7581492008-04-19 19:45:00 +0200394static inline struct cfs_rq *cfs_rq_of(struct sched_entity *se)
395{
396 struct task_struct *p = task_of(se);
397 struct rq *rq = task_rq(p);
398
399 return &rq->cfs;
400}
401
402/* runqueue "owned" by this group */
403static inline struct cfs_rq *group_cfs_rq(struct sched_entity *grp)
404{
405 return NULL;
406}
407
Peter Zijlstra3d4b47b2010-11-15 15:47:01 -0800408static inline void list_add_leaf_cfs_rq(struct cfs_rq *cfs_rq)
409{
410}
411
412static inline void list_del_leaf_cfs_rq(struct cfs_rq *cfs_rq)
413{
414}
415
Peter Zijlstrab7581492008-04-19 19:45:00 +0200416#define for_each_leaf_cfs_rq(rq, cfs_rq) \
417 for (cfs_rq = &rq->cfs; cfs_rq; cfs_rq = NULL)
418
Peter Zijlstrab7581492008-04-19 19:45:00 +0200419static inline struct sched_entity *parent_entity(struct sched_entity *se)
420{
421 return NULL;
422}
423
Peter Zijlstra464b7522008-10-24 11:06:15 +0200424static inline void
425find_matching_se(struct sched_entity **se, struct sched_entity **pse)
426{
427}
428
Peter Zijlstrab7581492008-04-19 19:45:00 +0200429#endif /* CONFIG_FAIR_GROUP_SCHED */
430
Peter Zijlstra6c16a6d2012-03-21 13:07:16 -0700431static __always_inline
Peter Zijlstra9dbdb152013-11-18 18:27:06 +0100432void account_cfs_rq_runtime(struct cfs_rq *cfs_rq, u64 delta_exec);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200433
434/**************************************************************
435 * Scheduling class tree data structure manipulation methods:
436 */
437
Andrei Epure1bf08232013-03-12 21:12:24 +0200438static inline u64 max_vruntime(u64 max_vruntime, u64 vruntime)
Peter Zijlstra02e04312007-10-15 17:00:07 +0200439{
Andrei Epure1bf08232013-03-12 21:12:24 +0200440 s64 delta = (s64)(vruntime - max_vruntime);
Peter Zijlstra368059a2007-10-15 17:00:11 +0200441 if (delta > 0)
Andrei Epure1bf08232013-03-12 21:12:24 +0200442 max_vruntime = vruntime;
Peter Zijlstra02e04312007-10-15 17:00:07 +0200443
Andrei Epure1bf08232013-03-12 21:12:24 +0200444 return max_vruntime;
Peter Zijlstra02e04312007-10-15 17:00:07 +0200445}
446
Ingo Molnar0702e3e2007-10-15 17:00:14 +0200447static inline u64 min_vruntime(u64 min_vruntime, u64 vruntime)
Peter Zijlstrab0ffd242007-10-15 17:00:12 +0200448{
449 s64 delta = (s64)(vruntime - min_vruntime);
450 if (delta < 0)
451 min_vruntime = vruntime;
452
453 return min_vruntime;
454}
455
Fabio Checconi54fdc582009-07-16 12:32:27 +0200456static inline int entity_before(struct sched_entity *a,
457 struct sched_entity *b)
458{
459 return (s64)(a->vruntime - b->vruntime) < 0;
460}
461
Peter Zijlstra1af5f732008-10-24 11:06:13 +0200462static void update_min_vruntime(struct cfs_rq *cfs_rq)
463{
Peter Zijlstrab60205c2016-09-20 21:58:12 +0200464 struct sched_entity *curr = cfs_rq->curr;
465
Peter Zijlstra1af5f732008-10-24 11:06:13 +0200466 u64 vruntime = cfs_rq->min_vruntime;
467
Peter Zijlstrab60205c2016-09-20 21:58:12 +0200468 if (curr) {
469 if (curr->on_rq)
470 vruntime = curr->vruntime;
471 else
472 curr = NULL;
473 }
Peter Zijlstra1af5f732008-10-24 11:06:13 +0200474
475 if (cfs_rq->rb_leftmost) {
476 struct sched_entity *se = rb_entry(cfs_rq->rb_leftmost,
477 struct sched_entity,
478 run_node);
479
Peter Zijlstrab60205c2016-09-20 21:58:12 +0200480 if (!curr)
Peter Zijlstra1af5f732008-10-24 11:06:13 +0200481 vruntime = se->vruntime;
482 else
483 vruntime = min_vruntime(vruntime, se->vruntime);
484 }
485
Andrei Epure1bf08232013-03-12 21:12:24 +0200486 /* ensure we never gain time by being placed backwards. */
Peter Zijlstra1af5f732008-10-24 11:06:13 +0200487 cfs_rq->min_vruntime = max_vruntime(cfs_rq->min_vruntime, vruntime);
Peter Zijlstra3fe16982011-04-05 17:23:48 +0200488#ifndef CONFIG_64BIT
489 smp_wmb();
490 cfs_rq->min_vruntime_copy = cfs_rq->min_vruntime;
491#endif
Peter Zijlstra1af5f732008-10-24 11:06:13 +0200492}
493
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200494/*
495 * Enqueue an entity into the rb-tree:
496 */
Ingo Molnar0702e3e2007-10-15 17:00:14 +0200497static void __enqueue_entity(struct cfs_rq *cfs_rq, struct sched_entity *se)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200498{
499 struct rb_node **link = &cfs_rq->tasks_timeline.rb_node;
500 struct rb_node *parent = NULL;
501 struct sched_entity *entry;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200502 int leftmost = 1;
503
504 /*
505 * Find the right place in the rbtree:
506 */
507 while (*link) {
508 parent = *link;
509 entry = rb_entry(parent, struct sched_entity, run_node);
510 /*
511 * We dont care about collisions. Nodes with
512 * the same key stay together.
513 */
Stephan Baerwolf2bd2d6f2011-07-20 14:46:59 +0200514 if (entity_before(se, entry)) {
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200515 link = &parent->rb_left;
516 } else {
517 link = &parent->rb_right;
518 leftmost = 0;
519 }
520 }
521
522 /*
523 * Maintain a cache of leftmost tree entries (it is frequently
524 * used):
525 */
Peter Zijlstra1af5f732008-10-24 11:06:13 +0200526 if (leftmost)
Ingo Molnar57cb4992007-10-15 17:00:11 +0200527 cfs_rq->rb_leftmost = &se->run_node;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200528
529 rb_link_node(&se->run_node, parent, link);
530 rb_insert_color(&se->run_node, &cfs_rq->tasks_timeline);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200531}
532
Ingo Molnar0702e3e2007-10-15 17:00:14 +0200533static void __dequeue_entity(struct cfs_rq *cfs_rq, struct sched_entity *se)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200534{
Peter Zijlstra3fe69742008-03-14 20:55:51 +0100535 if (cfs_rq->rb_leftmost == &se->run_node) {
536 struct rb_node *next_node;
Peter Zijlstra3fe69742008-03-14 20:55:51 +0100537
538 next_node = rb_next(&se->run_node);
539 cfs_rq->rb_leftmost = next_node;
Peter Zijlstra3fe69742008-03-14 20:55:51 +0100540 }
Ingo Molnare9acbff2007-10-15 17:00:04 +0200541
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200542 rb_erase(&se->run_node, &cfs_rq->tasks_timeline);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200543}
544
Peter Zijlstra029632f2011-10-25 10:00:11 +0200545struct sched_entity *__pick_first_entity(struct cfs_rq *cfs_rq)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200546{
Peter Zijlstraf4b67552008-11-04 21:25:07 +0100547 struct rb_node *left = cfs_rq->rb_leftmost;
548
549 if (!left)
550 return NULL;
551
552 return rb_entry(left, struct sched_entity, run_node);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200553}
554
Rik van Rielac53db52011-02-01 09:51:03 -0500555static struct sched_entity *__pick_next_entity(struct sched_entity *se)
556{
557 struct rb_node *next = rb_next(&se->run_node);
558
559 if (!next)
560 return NULL;
561
562 return rb_entry(next, struct sched_entity, run_node);
563}
564
565#ifdef CONFIG_SCHED_DEBUG
Peter Zijlstra029632f2011-10-25 10:00:11 +0200566struct sched_entity *__pick_last_entity(struct cfs_rq *cfs_rq)
Peter Zijlstraaeb73b02007-10-15 17:00:05 +0200567{
Ingo Molnar7eee3e62008-02-22 10:32:21 +0100568 struct rb_node *last = rb_last(&cfs_rq->tasks_timeline);
Peter Zijlstraaeb73b02007-10-15 17:00:05 +0200569
Balbir Singh70eee742008-02-22 13:25:53 +0530570 if (!last)
571 return NULL;
Ingo Molnar7eee3e62008-02-22 10:32:21 +0100572
573 return rb_entry(last, struct sched_entity, run_node);
Peter Zijlstraaeb73b02007-10-15 17:00:05 +0200574}
575
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200576/**************************************************************
577 * Scheduling class statistics methods:
578 */
579
Christian Ehrhardtacb4a842009-11-30 12:16:48 +0100580int sched_proc_update_handler(struct ctl_table *table, int write,
Alexey Dobriyan8d65af72009-09-23 15:57:19 -0700581 void __user *buffer, size_t *lenp,
Peter Zijlstrab2be5e92007-11-09 22:39:37 +0100582 loff_t *ppos)
583{
Alexey Dobriyan8d65af72009-09-23 15:57:19 -0700584 int ret = proc_dointvec_minmax(table, write, buffer, lenp, ppos);
Nicholas Mc Guire58ac93e2015-05-15 21:05:42 +0200585 unsigned int factor = get_update_sysctl_factor();
Peter Zijlstrab2be5e92007-11-09 22:39:37 +0100586
587 if (ret || !write)
588 return ret;
589
590 sched_nr_latency = DIV_ROUND_UP(sysctl_sched_latency,
591 sysctl_sched_min_granularity);
592
Christian Ehrhardtacb4a842009-11-30 12:16:48 +0100593#define WRT_SYSCTL(name) \
594 (normalized_sysctl_##name = sysctl_##name / (factor))
595 WRT_SYSCTL(sched_min_granularity);
596 WRT_SYSCTL(sched_latency);
597 WRT_SYSCTL(sched_wakeup_granularity);
Christian Ehrhardtacb4a842009-11-30 12:16:48 +0100598#undef WRT_SYSCTL
599
Peter Zijlstrab2be5e92007-11-09 22:39:37 +0100600 return 0;
601}
602#endif
Ingo Molnar647e7ca2007-10-15 17:00:13 +0200603
604/*
Peter Zijlstraf9c0b092008-10-17 19:27:04 +0200605 * delta /= w
Peter Zijlstraa7be37a2008-06-27 13:41:11 +0200606 */
Peter Zijlstra9dbdb152013-11-18 18:27:06 +0100607static inline u64 calc_delta_fair(u64 delta, struct sched_entity *se)
Peter Zijlstraa7be37a2008-06-27 13:41:11 +0200608{
Peter Zijlstraf9c0b092008-10-17 19:27:04 +0200609 if (unlikely(se->load.weight != NICE_0_LOAD))
Peter Zijlstra9dbdb152013-11-18 18:27:06 +0100610 delta = __calc_delta(delta, NICE_0_LOAD, &se->load);
Peter Zijlstraa7be37a2008-06-27 13:41:11 +0200611
612 return delta;
613}
614
615/*
Ingo Molnar647e7ca2007-10-15 17:00:13 +0200616 * The idea is to set a period in which each task runs once.
617 *
Borislav Petkov532b1852012-08-08 16:16:04 +0200618 * When there are too many tasks (sched_nr_latency) we have to stretch
Ingo Molnar647e7ca2007-10-15 17:00:13 +0200619 * this period because otherwise the slices get too small.
620 *
621 * p = (nr <= nl) ? l : l*nr/nl
622 */
Peter Zijlstra4d78e7b2007-10-15 17:00:04 +0200623static u64 __sched_period(unsigned long nr_running)
624{
Boqun Feng8e2b0bf2015-07-02 22:25:52 +0800625 if (unlikely(nr_running > sched_nr_latency))
626 return nr_running * sysctl_sched_min_granularity;
627 else
628 return sysctl_sched_latency;
Peter Zijlstra4d78e7b2007-10-15 17:00:04 +0200629}
630
Ingo Molnar647e7ca2007-10-15 17:00:13 +0200631/*
632 * We calculate the wall-time slice from the period by taking a part
633 * proportional to the weight.
634 *
Peter Zijlstraf9c0b092008-10-17 19:27:04 +0200635 * s = p*P[w/rw]
Ingo Molnar647e7ca2007-10-15 17:00:13 +0200636 */
Peter Zijlstra6d0f0ebd2007-10-15 17:00:05 +0200637static u64 sched_slice(struct cfs_rq *cfs_rq, struct sched_entity *se)
Peter Zijlstra21805082007-08-25 18:41:53 +0200638{
Mike Galbraith0a582442009-01-02 12:16:42 +0100639 u64 slice = __sched_period(cfs_rq->nr_running + !se->on_rq);
Peter Zijlstraf9c0b092008-10-17 19:27:04 +0200640
Mike Galbraith0a582442009-01-02 12:16:42 +0100641 for_each_sched_entity(se) {
Lin Ming6272d682009-01-15 17:17:15 +0100642 struct load_weight *load;
Christian Engelmayer3104bf02009-06-16 10:35:12 +0200643 struct load_weight lw;
Lin Ming6272d682009-01-15 17:17:15 +0100644
645 cfs_rq = cfs_rq_of(se);
646 load = &cfs_rq->load;
Peter Zijlstraf9c0b092008-10-17 19:27:04 +0200647
Mike Galbraith0a582442009-01-02 12:16:42 +0100648 if (unlikely(!se->on_rq)) {
Christian Engelmayer3104bf02009-06-16 10:35:12 +0200649 lw = cfs_rq->load;
Mike Galbraith0a582442009-01-02 12:16:42 +0100650
651 update_load_add(&lw, se->load.weight);
652 load = &lw;
653 }
Peter Zijlstra9dbdb152013-11-18 18:27:06 +0100654 slice = __calc_delta(slice, se->load.weight, load);
Mike Galbraith0a582442009-01-02 12:16:42 +0100655 }
656 return slice;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200657}
658
Ingo Molnar647e7ca2007-10-15 17:00:13 +0200659/*
Andrei Epure660cc002013-03-11 12:03:20 +0200660 * We calculate the vruntime slice of a to-be-inserted task.
Ingo Molnar647e7ca2007-10-15 17:00:13 +0200661 *
Peter Zijlstraf9c0b092008-10-17 19:27:04 +0200662 * vs = s/w
Ingo Molnar647e7ca2007-10-15 17:00:13 +0200663 */
Peter Zijlstraf9c0b092008-10-17 19:27:04 +0200664static u64 sched_vslice(struct cfs_rq *cfs_rq, struct sched_entity *se)
Ingo Molnar647e7ca2007-10-15 17:00:13 +0200665{
Peter Zijlstraf9c0b092008-10-17 19:27:04 +0200666 return calc_delta_fair(sched_slice(cfs_rq, se), se);
Peter Zijlstraa7be37a2008-06-27 13:41:11 +0200667}
668
Alex Shia75cdaa2013-06-20 10:18:47 +0800669#ifdef CONFIG_SMP
Morten Rasmussen772bd008c2016-06-22 18:03:13 +0100670static int select_idle_sibling(struct task_struct *p, int prev_cpu, int cpu);
Mel Gormanfb13c7e2013-10-07 11:29:17 +0100671static unsigned long task_h_load(struct task_struct *p);
672
Yuyang Du9d89c252015-07-15 08:04:37 +0800673/*
674 * We choose a half-life close to 1 scheduling period.
Leo Yan84fb5a12015-09-15 18:57:37 +0800675 * Note: The tables runnable_avg_yN_inv and runnable_avg_yN_sum are
676 * dependent on this value.
Yuyang Du9d89c252015-07-15 08:04:37 +0800677 */
678#define LOAD_AVG_PERIOD 32
679#define LOAD_AVG_MAX 47742 /* maximum possible load avg */
Leo Yan84fb5a12015-09-15 18:57:37 +0800680#define LOAD_AVG_MAX_N 345 /* number of full periods to produce LOAD_AVG_MAX */
Alex Shia75cdaa2013-06-20 10:18:47 +0800681
Yuyang Du540247f2015-07-15 08:04:39 +0800682/* Give new sched_entity start runnable values to heavy its load in infant time */
683void init_entity_runnable_average(struct sched_entity *se)
Alex Shia75cdaa2013-06-20 10:18:47 +0800684{
Yuyang Du540247f2015-07-15 08:04:39 +0800685 struct sched_avg *sa = &se->avg;
Alex Shia75cdaa2013-06-20 10:18:47 +0800686
Yuyang Du9d89c252015-07-15 08:04:37 +0800687 sa->last_update_time = 0;
688 /*
689 * sched_avg's period_contrib should be strictly less then 1024, so
690 * we give it 1023 to make sure it is almost a period (1024us), and
691 * will definitely be update (after enqueue).
692 */
693 sa->period_contrib = 1023;
Vincent Guittotb5a9b342016-10-19 14:45:23 +0200694 /*
695 * Tasks are intialized with full load to be seen as heavy tasks until
696 * they get a chance to stabilize to their real load level.
697 * Group entities are intialized with zero load to reflect the fact that
698 * nothing has been attached to the task group yet.
699 */
700 if (entity_is_task(se))
701 sa->load_avg = scale_load_down(se->load.weight);
Yuyang Du9d89c252015-07-15 08:04:37 +0800702 sa->load_sum = sa->load_avg * LOAD_AVG_MAX;
Yuyang Du2b8c41d2016-03-30 04:30:56 +0800703 /*
704 * At this point, util_avg won't be used in select_task_rq_fair anyway
705 */
706 sa->util_avg = 0;
707 sa->util_sum = 0;
Yuyang Du9d89c252015-07-15 08:04:37 +0800708 /* when this task enqueue'ed, it will contribute to its cfs_rq's load_avg */
Alex Shia75cdaa2013-06-20 10:18:47 +0800709}
Yuyang Du7ea241a2015-07-15 08:04:42 +0800710
Peter Zijlstra7dc603c2016-06-16 13:29:28 +0200711static inline u64 cfs_rq_clock_task(struct cfs_rq *cfs_rq);
712static int update_cfs_rq_load_avg(u64 now, struct cfs_rq *cfs_rq, bool update_freq);
Peter Zijlstra3d30544f2016-06-21 14:27:50 +0200713static void update_tg_load_avg(struct cfs_rq *cfs_rq, int force);
Peter Zijlstra7dc603c2016-06-16 13:29:28 +0200714static void attach_entity_load_avg(struct cfs_rq *cfs_rq, struct sched_entity *se);
715
Yuyang Du2b8c41d2016-03-30 04:30:56 +0800716/*
717 * With new tasks being created, their initial util_avgs are extrapolated
718 * based on the cfs_rq's current util_avg:
719 *
720 * util_avg = cfs_rq->util_avg / (cfs_rq->load_avg + 1) * se.load.weight
721 *
722 * However, in many cases, the above util_avg does not give a desired
723 * value. Moreover, the sum of the util_avgs may be divergent, such
724 * as when the series is a harmonic series.
725 *
726 * To solve this problem, we also cap the util_avg of successive tasks to
727 * only 1/2 of the left utilization budget:
728 *
729 * util_avg_cap = (1024 - cfs_rq->avg.util_avg) / 2^n
730 *
731 * where n denotes the nth task.
732 *
733 * For example, a simplest series from the beginning would be like:
734 *
735 * task util_avg: 512, 256, 128, 64, 32, 16, 8, ...
736 * cfs_rq util_avg: 512, 768, 896, 960, 992, 1008, 1016, ...
737 *
738 * Finally, that extrapolated util_avg is clamped to the cap (util_avg_cap)
739 * if util_avg > util_avg_cap.
740 */
741void post_init_entity_util_avg(struct sched_entity *se)
742{
743 struct cfs_rq *cfs_rq = cfs_rq_of(se);
744 struct sched_avg *sa = &se->avg;
Yuyang Du172895e2016-04-05 12:12:27 +0800745 long cap = (long)(SCHED_CAPACITY_SCALE - cfs_rq->avg.util_avg) / 2;
Peter Zijlstra7dc603c2016-06-16 13:29:28 +0200746 u64 now = cfs_rq_clock_task(cfs_rq);
Yuyang Du2b8c41d2016-03-30 04:30:56 +0800747
748 if (cap > 0) {
749 if (cfs_rq->avg.util_avg != 0) {
750 sa->util_avg = cfs_rq->avg.util_avg * se->load.weight;
751 sa->util_avg /= (cfs_rq->avg.load_avg + 1);
752
753 if (sa->util_avg > cap)
754 sa->util_avg = cap;
755 } else {
756 sa->util_avg = cap;
757 }
758 sa->util_sum = sa->util_avg * LOAD_AVG_MAX;
759 }
Peter Zijlstra7dc603c2016-06-16 13:29:28 +0200760
761 if (entity_is_task(se)) {
762 struct task_struct *p = task_of(se);
763 if (p->sched_class != &fair_sched_class) {
764 /*
765 * For !fair tasks do:
766 *
767 update_cfs_rq_load_avg(now, cfs_rq, false);
768 attach_entity_load_avg(cfs_rq, se);
769 switched_from_fair(rq, p);
770 *
771 * such that the next switched_to_fair() has the
772 * expected state.
773 */
774 se->avg.last_update_time = now;
775 return;
776 }
777 }
778
Peter Zijlstra7c3edd22016-07-13 10:56:25 +0200779 update_cfs_rq_load_avg(now, cfs_rq, false);
Peter Zijlstra7dc603c2016-06-16 13:29:28 +0200780 attach_entity_load_avg(cfs_rq, se);
Peter Zijlstra7c3edd22016-07-13 10:56:25 +0200781 update_tg_load_avg(cfs_rq, false);
Yuyang Du2b8c41d2016-03-30 04:30:56 +0800782}
783
Peter Zijlstra7dc603c2016-06-16 13:29:28 +0200784#else /* !CONFIG_SMP */
Yuyang Du540247f2015-07-15 08:04:39 +0800785void init_entity_runnable_average(struct sched_entity *se)
Alex Shia75cdaa2013-06-20 10:18:47 +0800786{
787}
Yuyang Du2b8c41d2016-03-30 04:30:56 +0800788void post_init_entity_util_avg(struct sched_entity *se)
789{
790}
Peter Zijlstra3d30544f2016-06-21 14:27:50 +0200791static void update_tg_load_avg(struct cfs_rq *cfs_rq, int force)
792{
793}
Peter Zijlstra7dc603c2016-06-16 13:29:28 +0200794#endif /* CONFIG_SMP */
Alex Shia75cdaa2013-06-20 10:18:47 +0800795
Peter Zijlstraa7be37a2008-06-27 13:41:11 +0200796/*
Peter Zijlstra9dbdb152013-11-18 18:27:06 +0100797 * Update the current task's runtime statistics.
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200798 */
Ingo Molnarb7cc0892007-08-09 11:16:47 +0200799static void update_curr(struct cfs_rq *cfs_rq)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200800{
Ingo Molnar429d43b2007-10-15 17:00:03 +0200801 struct sched_entity *curr = cfs_rq->curr;
Frederic Weisbecker78becc22013-04-12 01:51:02 +0200802 u64 now = rq_clock_task(rq_of(cfs_rq));
Peter Zijlstra9dbdb152013-11-18 18:27:06 +0100803 u64 delta_exec;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200804
805 if (unlikely(!curr))
806 return;
807
Peter Zijlstra9dbdb152013-11-18 18:27:06 +0100808 delta_exec = now - curr->exec_start;
809 if (unlikely((s64)delta_exec <= 0))
Peter Zijlstra34f28ec2008-12-16 08:45:31 +0100810 return;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200811
Ingo Molnar8ebc91d2007-10-15 17:00:03 +0200812 curr->exec_start = now;
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +0100813
Peter Zijlstra9dbdb152013-11-18 18:27:06 +0100814 schedstat_set(curr->statistics.exec_max,
815 max(delta_exec, curr->statistics.exec_max));
816
817 curr->sum_exec_runtime += delta_exec;
Josh Poimboeufae928822016-06-17 12:43:24 -0500818 schedstat_add(cfs_rq->exec_clock, delta_exec);
Peter Zijlstra9dbdb152013-11-18 18:27:06 +0100819
820 curr->vruntime += calc_delta_fair(delta_exec, curr);
821 update_min_vruntime(cfs_rq);
822
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +0100823 if (entity_is_task(curr)) {
824 struct task_struct *curtask = task_of(curr);
825
Ingo Molnarf977bb42009-09-13 18:15:54 +0200826 trace_sched_stat_runtime(curtask, delta_exec, curr->vruntime);
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +0100827 cpuacct_charge(curtask, delta_exec);
Frank Mayharf06febc2008-09-12 09:54:39 -0700828 account_group_exec_runtime(curtask, delta_exec);
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +0100829 }
Paul Turnerec12cb72011-07-21 09:43:30 -0700830
831 account_cfs_rq_runtime(cfs_rq, delta_exec);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200832}
833
Stanislaw Gruszka6e998912014-11-12 16:58:44 +0100834static void update_curr_fair(struct rq *rq)
835{
836 update_curr(cfs_rq_of(&rq->curr->se));
837}
838
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200839static inline void
Ingo Molnar5870db52007-08-09 11:16:47 +0200840update_stats_wait_start(struct cfs_rq *cfs_rq, struct sched_entity *se)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200841{
Josh Poimboeuf4fa8d292016-06-17 12:43:26 -0500842 u64 wait_start, prev_wait_start;
843
844 if (!schedstat_enabled())
845 return;
846
847 wait_start = rq_clock(rq_of(cfs_rq));
848 prev_wait_start = schedstat_val(se->statistics.wait_start);
Joonwoo Park3ea94de2015-11-12 19:38:54 -0800849
850 if (entity_is_task(se) && task_on_rq_migrating(task_of(se)) &&
Josh Poimboeuf4fa8d292016-06-17 12:43:26 -0500851 likely(wait_start > prev_wait_start))
852 wait_start -= prev_wait_start;
Joonwoo Park3ea94de2015-11-12 19:38:54 -0800853
Josh Poimboeuf4fa8d292016-06-17 12:43:26 -0500854 schedstat_set(se->statistics.wait_start, wait_start);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200855}
856
Josh Poimboeuf4fa8d292016-06-17 12:43:26 -0500857static inline void
Joonwoo Park3ea94de2015-11-12 19:38:54 -0800858update_stats_wait_end(struct cfs_rq *cfs_rq, struct sched_entity *se)
859{
860 struct task_struct *p;
Mel Gormancb251762016-02-05 09:08:36 +0000861 u64 delta;
862
Josh Poimboeuf4fa8d292016-06-17 12:43:26 -0500863 if (!schedstat_enabled())
864 return;
865
866 delta = rq_clock(rq_of(cfs_rq)) - schedstat_val(se->statistics.wait_start);
Joonwoo Park3ea94de2015-11-12 19:38:54 -0800867
868 if (entity_is_task(se)) {
869 p = task_of(se);
870 if (task_on_rq_migrating(p)) {
871 /*
872 * Preserve migrating task's wait time so wait_start
873 * time stamp can be adjusted to accumulate wait time
874 * prior to migration.
875 */
Josh Poimboeuf4fa8d292016-06-17 12:43:26 -0500876 schedstat_set(se->statistics.wait_start, delta);
Joonwoo Park3ea94de2015-11-12 19:38:54 -0800877 return;
878 }
879 trace_sched_stat_wait(p, delta);
880 }
881
Josh Poimboeuf4fa8d292016-06-17 12:43:26 -0500882 schedstat_set(se->statistics.wait_max,
883 max(schedstat_val(se->statistics.wait_max), delta));
884 schedstat_inc(se->statistics.wait_count);
885 schedstat_add(se->statistics.wait_sum, delta);
886 schedstat_set(se->statistics.wait_start, 0);
Joonwoo Park3ea94de2015-11-12 19:38:54 -0800887}
Joonwoo Park3ea94de2015-11-12 19:38:54 -0800888
Josh Poimboeuf4fa8d292016-06-17 12:43:26 -0500889static inline void
Josh Poimboeuf1a3d0272016-06-17 12:43:23 -0500890update_stats_enqueue_sleeper(struct cfs_rq *cfs_rq, struct sched_entity *se)
891{
892 struct task_struct *tsk = NULL;
Josh Poimboeuf4fa8d292016-06-17 12:43:26 -0500893 u64 sleep_start, block_start;
894
895 if (!schedstat_enabled())
896 return;
897
898 sleep_start = schedstat_val(se->statistics.sleep_start);
899 block_start = schedstat_val(se->statistics.block_start);
Josh Poimboeuf1a3d0272016-06-17 12:43:23 -0500900
901 if (entity_is_task(se))
902 tsk = task_of(se);
903
Josh Poimboeuf4fa8d292016-06-17 12:43:26 -0500904 if (sleep_start) {
905 u64 delta = rq_clock(rq_of(cfs_rq)) - sleep_start;
Josh Poimboeuf1a3d0272016-06-17 12:43:23 -0500906
907 if ((s64)delta < 0)
908 delta = 0;
909
Josh Poimboeuf4fa8d292016-06-17 12:43:26 -0500910 if (unlikely(delta > schedstat_val(se->statistics.sleep_max)))
911 schedstat_set(se->statistics.sleep_max, delta);
Josh Poimboeuf1a3d0272016-06-17 12:43:23 -0500912
Josh Poimboeuf4fa8d292016-06-17 12:43:26 -0500913 schedstat_set(se->statistics.sleep_start, 0);
914 schedstat_add(se->statistics.sum_sleep_runtime, delta);
Josh Poimboeuf1a3d0272016-06-17 12:43:23 -0500915
916 if (tsk) {
917 account_scheduler_latency(tsk, delta >> 10, 1);
918 trace_sched_stat_sleep(tsk, delta);
919 }
920 }
Josh Poimboeuf4fa8d292016-06-17 12:43:26 -0500921 if (block_start) {
922 u64 delta = rq_clock(rq_of(cfs_rq)) - block_start;
Josh Poimboeuf1a3d0272016-06-17 12:43:23 -0500923
924 if ((s64)delta < 0)
925 delta = 0;
926
Josh Poimboeuf4fa8d292016-06-17 12:43:26 -0500927 if (unlikely(delta > schedstat_val(se->statistics.block_max)))
928 schedstat_set(se->statistics.block_max, delta);
Josh Poimboeuf1a3d0272016-06-17 12:43:23 -0500929
Josh Poimboeuf4fa8d292016-06-17 12:43:26 -0500930 schedstat_set(se->statistics.block_start, 0);
931 schedstat_add(se->statistics.sum_sleep_runtime, delta);
Josh Poimboeuf1a3d0272016-06-17 12:43:23 -0500932
933 if (tsk) {
934 if (tsk->in_iowait) {
Josh Poimboeuf4fa8d292016-06-17 12:43:26 -0500935 schedstat_add(se->statistics.iowait_sum, delta);
936 schedstat_inc(se->statistics.iowait_count);
Josh Poimboeuf1a3d0272016-06-17 12:43:23 -0500937 trace_sched_stat_iowait(tsk, delta);
938 }
939
940 trace_sched_stat_blocked(tsk, delta);
Riley Andrewsa5a44472015-10-02 00:39:53 -0700941 trace_sched_blocked_reason(tsk);
Josh Poimboeuf1a3d0272016-06-17 12:43:23 -0500942
943 /*
944 * Blocking time is in units of nanosecs, so shift by
945 * 20 to get a milliseconds-range estimation of the
946 * amount of time that the task spent sleeping:
947 */
948 if (unlikely(prof_on == SLEEP_PROFILING)) {
949 profile_hits(SLEEP_PROFILING,
950 (void *)get_wchan(tsk),
951 delta >> 20);
952 }
953 account_scheduler_latency(tsk, delta >> 10, 0);
954 }
955 }
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200956}
957
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200958/*
959 * Task is being enqueued - update stats:
960 */
Mel Gormancb251762016-02-05 09:08:36 +0000961static inline void
Josh Poimboeuf1a3d0272016-06-17 12:43:23 -0500962update_stats_enqueue(struct cfs_rq *cfs_rq, struct sched_entity *se, int flags)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200963{
Josh Poimboeuf4fa8d292016-06-17 12:43:26 -0500964 if (!schedstat_enabled())
965 return;
966
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200967 /*
968 * Are we enqueueing a waiting task? (for current tasks
969 * a dequeue/enqueue event is a NOP)
970 */
Ingo Molnar429d43b2007-10-15 17:00:03 +0200971 if (se != cfs_rq->curr)
Ingo Molnar5870db52007-08-09 11:16:47 +0200972 update_stats_wait_start(cfs_rq, se);
Josh Poimboeuf1a3d0272016-06-17 12:43:23 -0500973
974 if (flags & ENQUEUE_WAKEUP)
975 update_stats_enqueue_sleeper(cfs_rq, se);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200976}
977
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200978static inline void
Mel Gormancb251762016-02-05 09:08:36 +0000979update_stats_dequeue(struct cfs_rq *cfs_rq, struct sched_entity *se, int flags)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200980{
Josh Poimboeuf4fa8d292016-06-17 12:43:26 -0500981
982 if (!schedstat_enabled())
983 return;
984
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200985 /*
986 * Mark the end of the wait period if dequeueing a
987 * waiting task:
988 */
Ingo Molnar429d43b2007-10-15 17:00:03 +0200989 if (se != cfs_rq->curr)
Ingo Molnar9ef0a962007-08-09 11:16:47 +0200990 update_stats_wait_end(cfs_rq, se);
Mel Gormancb251762016-02-05 09:08:36 +0000991
Josh Poimboeuf4fa8d292016-06-17 12:43:26 -0500992 if ((flags & DEQUEUE_SLEEP) && entity_is_task(se)) {
993 struct task_struct *tsk = task_of(se);
Mel Gormancb251762016-02-05 09:08:36 +0000994
Josh Poimboeuf4fa8d292016-06-17 12:43:26 -0500995 if (tsk->state & TASK_INTERRUPTIBLE)
996 schedstat_set(se->statistics.sleep_start,
997 rq_clock(rq_of(cfs_rq)));
998 if (tsk->state & TASK_UNINTERRUPTIBLE)
999 schedstat_set(se->statistics.block_start,
1000 rq_clock(rq_of(cfs_rq)));
Mel Gormancb251762016-02-05 09:08:36 +00001001 }
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001002}
1003
1004/*
1005 * We are picking a new current task - update its stats:
1006 */
1007static inline void
Ingo Molnar79303e92007-08-09 11:16:47 +02001008update_stats_curr_start(struct cfs_rq *cfs_rq, struct sched_entity *se)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001009{
1010 /*
1011 * We are starting a new run period:
1012 */
Frederic Weisbecker78becc22013-04-12 01:51:02 +02001013 se->exec_start = rq_clock_task(rq_of(cfs_rq));
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001014}
1015
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001016/**************************************************
1017 * Scheduling class queueing methods:
1018 */
1019
Peter Zijlstracbee9f82012-10-25 14:16:43 +02001020#ifdef CONFIG_NUMA_BALANCING
1021/*
Mel Gorman598f0ec2013-10-07 11:28:55 +01001022 * Approximate time to scan a full NUMA task in ms. The task scan period is
1023 * calculated based on the tasks virtual memory size and
1024 * numa_balancing_scan_size.
Peter Zijlstracbee9f82012-10-25 14:16:43 +02001025 */
Mel Gorman598f0ec2013-10-07 11:28:55 +01001026unsigned int sysctl_numa_balancing_scan_period_min = 1000;
1027unsigned int sysctl_numa_balancing_scan_period_max = 60000;
Peter Zijlstra6e5fb222012-10-25 14:16:45 +02001028
1029/* Portion of address space to scan in MB */
1030unsigned int sysctl_numa_balancing_scan_size = 256;
Peter Zijlstracbee9f82012-10-25 14:16:43 +02001031
Peter Zijlstra4b96a29b2012-10-25 14:16:47 +02001032/* Scan @scan_size MB every @scan_period after an initial @scan_delay in ms */
1033unsigned int sysctl_numa_balancing_scan_delay = 1000;
1034
Mel Gorman598f0ec2013-10-07 11:28:55 +01001035static unsigned int task_nr_scan_windows(struct task_struct *p)
1036{
1037 unsigned long rss = 0;
1038 unsigned long nr_scan_pages;
1039
1040 /*
1041 * Calculations based on RSS as non-present and empty pages are skipped
1042 * by the PTE scanner and NUMA hinting faults should be trapped based
1043 * on resident pages
1044 */
1045 nr_scan_pages = sysctl_numa_balancing_scan_size << (20 - PAGE_SHIFT);
1046 rss = get_mm_rss(p->mm);
1047 if (!rss)
1048 rss = nr_scan_pages;
1049
1050 rss = round_up(rss, nr_scan_pages);
1051 return rss / nr_scan_pages;
1052}
1053
1054/* For sanitys sake, never scan more PTEs than MAX_SCAN_WINDOW MB/sec. */
1055#define MAX_SCAN_WINDOW 2560
1056
1057static unsigned int task_scan_min(struct task_struct *p)
1058{
Jason Low316c1608d2015-04-28 13:00:20 -07001059 unsigned int scan_size = READ_ONCE(sysctl_numa_balancing_scan_size);
Mel Gorman598f0ec2013-10-07 11:28:55 +01001060 unsigned int scan, floor;
1061 unsigned int windows = 1;
1062
Kirill Tkhai64192652014-10-16 14:39:37 +04001063 if (scan_size < MAX_SCAN_WINDOW)
1064 windows = MAX_SCAN_WINDOW / scan_size;
Mel Gorman598f0ec2013-10-07 11:28:55 +01001065 floor = 1000 / windows;
1066
1067 scan = sysctl_numa_balancing_scan_period_min / task_nr_scan_windows(p);
1068 return max_t(unsigned int, floor, scan);
1069}
1070
1071static unsigned int task_scan_max(struct task_struct *p)
1072{
1073 unsigned int smin = task_scan_min(p);
1074 unsigned int smax;
1075
1076 /* Watch for min being lower than max due to floor calculations */
1077 smax = sysctl_numa_balancing_scan_period_max / task_nr_scan_windows(p);
1078 return max(smin, smax);
1079}
1080
Peter Zijlstra0ec8aa02013-10-07 11:29:33 +01001081static void account_numa_enqueue(struct rq *rq, struct task_struct *p)
1082{
1083 rq->nr_numa_running += (p->numa_preferred_nid != -1);
1084 rq->nr_preferred_running += (p->numa_preferred_nid == task_node(p));
1085}
1086
1087static void account_numa_dequeue(struct rq *rq, struct task_struct *p)
1088{
1089 rq->nr_numa_running -= (p->numa_preferred_nid != -1);
1090 rq->nr_preferred_running -= (p->numa_preferred_nid == task_node(p));
1091}
1092
Peter Zijlstra8c8a7432013-10-07 11:29:21 +01001093struct numa_group {
1094 atomic_t refcount;
1095
1096 spinlock_t lock; /* nr_tasks, tasks */
1097 int nr_tasks;
Mel Gormane29cf082013-10-07 11:29:22 +01001098 pid_t gid;
Rik van Riel4142c3e2016-01-25 17:07:39 -05001099 int active_nodes;
Peter Zijlstra8c8a7432013-10-07 11:29:21 +01001100
1101 struct rcu_head rcu;
Mel Gorman989348b2013-10-07 11:29:40 +01001102 unsigned long total_faults;
Rik van Riel4142c3e2016-01-25 17:07:39 -05001103 unsigned long max_faults_cpu;
Rik van Riel7e2703e2014-01-27 17:03:45 -05001104 /*
1105 * Faults_cpu is used to decide whether memory should move
1106 * towards the CPU. As a consequence, these stats are weighted
1107 * more by CPU use than by memory faults.
1108 */
Rik van Riel50ec8a42014-01-27 17:03:42 -05001109 unsigned long *faults_cpu;
Mel Gorman989348b2013-10-07 11:29:40 +01001110 unsigned long faults[0];
Peter Zijlstra8c8a7432013-10-07 11:29:21 +01001111};
1112
Rik van Rielbe1e4e72014-01-27 17:03:48 -05001113/* Shared or private faults. */
1114#define NR_NUMA_HINT_FAULT_TYPES 2
1115
1116/* Memory and CPU locality */
1117#define NR_NUMA_HINT_FAULT_STATS (NR_NUMA_HINT_FAULT_TYPES * 2)
1118
1119/* Averaged statistics, and temporary buffers. */
1120#define NR_NUMA_HINT_FAULT_BUCKETS (NR_NUMA_HINT_FAULT_STATS * 2)
1121
Mel Gormane29cf082013-10-07 11:29:22 +01001122pid_t task_numa_group_id(struct task_struct *p)
1123{
1124 return p->numa_group ? p->numa_group->gid : 0;
1125}
1126
Iulia Manda44dba3d2014-10-31 02:13:31 +02001127/*
1128 * The averaged statistics, shared & private, memory & cpu,
1129 * occupy the first half of the array. The second half of the
1130 * array is for current counters, which are averaged into the
1131 * first set by task_numa_placement.
1132 */
1133static inline int task_faults_idx(enum numa_faults_stats s, int nid, int priv)
Mel Gormanac8e8952013-10-07 11:29:03 +01001134{
Iulia Manda44dba3d2014-10-31 02:13:31 +02001135 return NR_NUMA_HINT_FAULT_TYPES * (s * nr_node_ids + nid) + priv;
Mel Gormanac8e8952013-10-07 11:29:03 +01001136}
1137
1138static inline unsigned long task_faults(struct task_struct *p, int nid)
1139{
Iulia Manda44dba3d2014-10-31 02:13:31 +02001140 if (!p->numa_faults)
Mel Gormanac8e8952013-10-07 11:29:03 +01001141 return 0;
1142
Iulia Manda44dba3d2014-10-31 02:13:31 +02001143 return p->numa_faults[task_faults_idx(NUMA_MEM, nid, 0)] +
1144 p->numa_faults[task_faults_idx(NUMA_MEM, nid, 1)];
Mel Gormanac8e8952013-10-07 11:29:03 +01001145}
1146
Mel Gorman83e1d2c2013-10-07 11:29:27 +01001147static inline unsigned long group_faults(struct task_struct *p, int nid)
1148{
1149 if (!p->numa_group)
1150 return 0;
1151
Iulia Manda44dba3d2014-10-31 02:13:31 +02001152 return p->numa_group->faults[task_faults_idx(NUMA_MEM, nid, 0)] +
1153 p->numa_group->faults[task_faults_idx(NUMA_MEM, nid, 1)];
Mel Gorman83e1d2c2013-10-07 11:29:27 +01001154}
1155
Rik van Riel20e07de2014-01-27 17:03:43 -05001156static inline unsigned long group_faults_cpu(struct numa_group *group, int nid)
1157{
Iulia Manda44dba3d2014-10-31 02:13:31 +02001158 return group->faults_cpu[task_faults_idx(NUMA_MEM, nid, 0)] +
1159 group->faults_cpu[task_faults_idx(NUMA_MEM, nid, 1)];
Rik van Riel20e07de2014-01-27 17:03:43 -05001160}
1161
Rik van Riel4142c3e2016-01-25 17:07:39 -05001162/*
1163 * A node triggering more than 1/3 as many NUMA faults as the maximum is
1164 * considered part of a numa group's pseudo-interleaving set. Migrations
1165 * between these nodes are slowed down, to allow things to settle down.
1166 */
1167#define ACTIVE_NODE_FRACTION 3
1168
1169static bool numa_is_active_node(int nid, struct numa_group *ng)
1170{
1171 return group_faults_cpu(ng, nid) * ACTIVE_NODE_FRACTION > ng->max_faults_cpu;
1172}
1173
Rik van Riel6c6b1192014-10-17 03:29:52 -04001174/* Handle placement on systems where not all nodes are directly connected. */
1175static unsigned long score_nearby_nodes(struct task_struct *p, int nid,
1176 int maxdist, bool task)
1177{
1178 unsigned long score = 0;
1179 int node;
1180
1181 /*
1182 * All nodes are directly connected, and the same distance
1183 * from each other. No need for fancy placement algorithms.
1184 */
1185 if (sched_numa_topology_type == NUMA_DIRECT)
1186 return 0;
1187
1188 /*
1189 * This code is called for each node, introducing N^2 complexity,
1190 * which should be ok given the number of nodes rarely exceeds 8.
1191 */
1192 for_each_online_node(node) {
1193 unsigned long faults;
1194 int dist = node_distance(nid, node);
1195
1196 /*
1197 * The furthest away nodes in the system are not interesting
1198 * for placement; nid was already counted.
1199 */
1200 if (dist == sched_max_numa_distance || node == nid)
1201 continue;
1202
1203 /*
1204 * On systems with a backplane NUMA topology, compare groups
1205 * of nodes, and move tasks towards the group with the most
1206 * memory accesses. When comparing two nodes at distance
1207 * "hoplimit", only nodes closer by than "hoplimit" are part
1208 * of each group. Skip other nodes.
1209 */
1210 if (sched_numa_topology_type == NUMA_BACKPLANE &&
1211 dist > maxdist)
1212 continue;
1213
1214 /* Add up the faults from nearby nodes. */
1215 if (task)
1216 faults = task_faults(p, node);
1217 else
1218 faults = group_faults(p, node);
1219
1220 /*
1221 * On systems with a glueless mesh NUMA topology, there are
1222 * no fixed "groups of nodes". Instead, nodes that are not
1223 * directly connected bounce traffic through intermediate
1224 * nodes; a numa_group can occupy any set of nodes.
1225 * The further away a node is, the less the faults count.
1226 * This seems to result in good task placement.
1227 */
1228 if (sched_numa_topology_type == NUMA_GLUELESS_MESH) {
1229 faults *= (sched_max_numa_distance - dist);
1230 faults /= (sched_max_numa_distance - LOCAL_DISTANCE);
1231 }
1232
1233 score += faults;
1234 }
1235
1236 return score;
1237}
1238
Mel Gorman83e1d2c2013-10-07 11:29:27 +01001239/*
1240 * These return the fraction of accesses done by a particular task, or
1241 * task group, on a particular numa node. The group weight is given a
1242 * larger multiplier, in order to group tasks together that are almost
1243 * evenly spread out between numa nodes.
1244 */
Rik van Riel7bd95322014-10-17 03:29:51 -04001245static inline unsigned long task_weight(struct task_struct *p, int nid,
1246 int dist)
Mel Gorman83e1d2c2013-10-07 11:29:27 +01001247{
Rik van Riel7bd95322014-10-17 03:29:51 -04001248 unsigned long faults, total_faults;
Mel Gorman83e1d2c2013-10-07 11:29:27 +01001249
Iulia Manda44dba3d2014-10-31 02:13:31 +02001250 if (!p->numa_faults)
Mel Gorman83e1d2c2013-10-07 11:29:27 +01001251 return 0;
1252
1253 total_faults = p->total_numa_faults;
1254
1255 if (!total_faults)
1256 return 0;
1257
Rik van Riel7bd95322014-10-17 03:29:51 -04001258 faults = task_faults(p, nid);
Rik van Riel6c6b1192014-10-17 03:29:52 -04001259 faults += score_nearby_nodes(p, nid, dist, true);
1260
Rik van Riel7bd95322014-10-17 03:29:51 -04001261 return 1000 * faults / total_faults;
Mel Gorman83e1d2c2013-10-07 11:29:27 +01001262}
1263
Rik van Riel7bd95322014-10-17 03:29:51 -04001264static inline unsigned long group_weight(struct task_struct *p, int nid,
1265 int dist)
Mel Gorman83e1d2c2013-10-07 11:29:27 +01001266{
Rik van Riel7bd95322014-10-17 03:29:51 -04001267 unsigned long faults, total_faults;
1268
1269 if (!p->numa_group)
Mel Gorman83e1d2c2013-10-07 11:29:27 +01001270 return 0;
1271
Rik van Riel7bd95322014-10-17 03:29:51 -04001272 total_faults = p->numa_group->total_faults;
1273
1274 if (!total_faults)
1275 return 0;
1276
1277 faults = group_faults(p, nid);
Rik van Riel6c6b1192014-10-17 03:29:52 -04001278 faults += score_nearby_nodes(p, nid, dist, false);
1279
Rik van Riel7bd95322014-10-17 03:29:51 -04001280 return 1000 * faults / total_faults;
Mel Gorman83e1d2c2013-10-07 11:29:27 +01001281}
1282
Rik van Riel10f39042014-01-27 17:03:44 -05001283bool should_numa_migrate_memory(struct task_struct *p, struct page * page,
1284 int src_nid, int dst_cpu)
1285{
1286 struct numa_group *ng = p->numa_group;
1287 int dst_nid = cpu_to_node(dst_cpu);
1288 int last_cpupid, this_cpupid;
1289
1290 this_cpupid = cpu_pid_to_cpupid(dst_cpu, current->pid);
1291
1292 /*
1293 * Multi-stage node selection is used in conjunction with a periodic
1294 * migration fault to build a temporal task<->page relation. By using
1295 * a two-stage filter we remove short/unlikely relations.
1296 *
1297 * Using P(p) ~ n_p / n_t as per frequentist probability, we can equate
1298 * a task's usage of a particular page (n_p) per total usage of this
1299 * page (n_t) (in a given time-span) to a probability.
1300 *
1301 * Our periodic faults will sample this probability and getting the
1302 * same result twice in a row, given these samples are fully
1303 * independent, is then given by P(n)^2, provided our sample period
1304 * is sufficiently short compared to the usage pattern.
1305 *
1306 * This quadric squishes small probabilities, making it less likely we
1307 * act on an unlikely task<->page relation.
1308 */
1309 last_cpupid = page_cpupid_xchg_last(page, this_cpupid);
1310 if (!cpupid_pid_unset(last_cpupid) &&
1311 cpupid_to_nid(last_cpupid) != dst_nid)
1312 return false;
1313
1314 /* Always allow migrate on private faults */
1315 if (cpupid_match_pid(p, last_cpupid))
1316 return true;
1317
1318 /* A shared fault, but p->numa_group has not been set up yet. */
1319 if (!ng)
1320 return true;
1321
1322 /*
Rik van Riel4142c3e2016-01-25 17:07:39 -05001323 * Destination node is much more heavily used than the source
1324 * node? Allow migration.
Rik van Riel10f39042014-01-27 17:03:44 -05001325 */
Rik van Riel4142c3e2016-01-25 17:07:39 -05001326 if (group_faults_cpu(ng, dst_nid) > group_faults_cpu(ng, src_nid) *
1327 ACTIVE_NODE_FRACTION)
Rik van Riel10f39042014-01-27 17:03:44 -05001328 return true;
1329
1330 /*
Rik van Riel4142c3e2016-01-25 17:07:39 -05001331 * Distribute memory according to CPU & memory use on each node,
1332 * with 3/4 hysteresis to avoid unnecessary memory migrations:
1333 *
1334 * faults_cpu(dst) 3 faults_cpu(src)
1335 * --------------- * - > ---------------
1336 * faults_mem(dst) 4 faults_mem(src)
Rik van Riel10f39042014-01-27 17:03:44 -05001337 */
Rik van Riel4142c3e2016-01-25 17:07:39 -05001338 return group_faults_cpu(ng, dst_nid) * group_faults(p, src_nid) * 3 >
1339 group_faults_cpu(ng, src_nid) * group_faults(p, dst_nid) * 4;
Rik van Riel10f39042014-01-27 17:03:44 -05001340}
1341
Mel Gormane6628d52013-10-07 11:29:02 +01001342static unsigned long weighted_cpuload(const int cpu);
Mel Gorman58d081b2013-10-07 11:29:10 +01001343static unsigned long source_load(int cpu, int type);
1344static unsigned long target_load(int cpu, int type);
Nicolas Pitreced549f2014-05-26 18:19:38 -04001345static unsigned long capacity_of(int cpu);
Mel Gorman58d081b2013-10-07 11:29:10 +01001346static long effective_load(struct task_group *tg, int cpu, long wl, long wg);
Mel Gormane6628d52013-10-07 11:29:02 +01001347
Mel Gormanfb13c7e2013-10-07 11:29:17 +01001348/* Cached statistics for all CPUs within a node */
Mel Gorman58d081b2013-10-07 11:29:10 +01001349struct numa_stats {
Mel Gormanfb13c7e2013-10-07 11:29:17 +01001350 unsigned long nr_running;
Mel Gorman58d081b2013-10-07 11:29:10 +01001351 unsigned long load;
Mel Gormanfb13c7e2013-10-07 11:29:17 +01001352
1353 /* Total compute capacity of CPUs on a node */
Nicolas Pitre5ef20ca2014-05-26 18:19:34 -04001354 unsigned long compute_capacity;
Mel Gormanfb13c7e2013-10-07 11:29:17 +01001355
1356 /* Approximate capacity in terms of runnable tasks on a node */
Nicolas Pitre5ef20ca2014-05-26 18:19:34 -04001357 unsigned long task_capacity;
Nicolas Pitre1b6a7492014-05-26 18:19:35 -04001358 int has_free_capacity;
Mel Gorman58d081b2013-10-07 11:29:10 +01001359};
Mel Gormane6628d52013-10-07 11:29:02 +01001360
Mel Gormanfb13c7e2013-10-07 11:29:17 +01001361/*
1362 * XXX borrowed from update_sg_lb_stats
1363 */
1364static void update_numa_stats(struct numa_stats *ns, int nid)
1365{
Rik van Riel83d7f242014-08-04 13:23:28 -04001366 int smt, cpu, cpus = 0;
1367 unsigned long capacity;
Mel Gormanfb13c7e2013-10-07 11:29:17 +01001368
1369 memset(ns, 0, sizeof(*ns));
1370 for_each_cpu(cpu, cpumask_of_node(nid)) {
1371 struct rq *rq = cpu_rq(cpu);
1372
1373 ns->nr_running += rq->nr_running;
1374 ns->load += weighted_cpuload(cpu);
Nicolas Pitreced549f2014-05-26 18:19:38 -04001375 ns->compute_capacity += capacity_of(cpu);
Peter Zijlstra5eca82a2013-11-06 18:47:57 +01001376
1377 cpus++;
Mel Gormanfb13c7e2013-10-07 11:29:17 +01001378 }
1379
Peter Zijlstra5eca82a2013-11-06 18:47:57 +01001380 /*
1381 * If we raced with hotplug and there are no CPUs left in our mask
1382 * the @ns structure is NULL'ed and task_numa_compare() will
1383 * not find this node attractive.
1384 *
Nicolas Pitre1b6a7492014-05-26 18:19:35 -04001385 * We'll either bail at !has_free_capacity, or we'll detect a huge
1386 * imbalance and bail there.
Peter Zijlstra5eca82a2013-11-06 18:47:57 +01001387 */
1388 if (!cpus)
1389 return;
1390
Rik van Riel83d7f242014-08-04 13:23:28 -04001391 /* smt := ceil(cpus / capacity), assumes: 1 < smt_power < 2 */
1392 smt = DIV_ROUND_UP(SCHED_CAPACITY_SCALE * cpus, ns->compute_capacity);
1393 capacity = cpus / smt; /* cores */
1394
1395 ns->task_capacity = min_t(unsigned, capacity,
1396 DIV_ROUND_CLOSEST(ns->compute_capacity, SCHED_CAPACITY_SCALE));
Nicolas Pitre1b6a7492014-05-26 18:19:35 -04001397 ns->has_free_capacity = (ns->nr_running < ns->task_capacity);
Mel Gormanfb13c7e2013-10-07 11:29:17 +01001398}
1399
Mel Gorman58d081b2013-10-07 11:29:10 +01001400struct task_numa_env {
1401 struct task_struct *p;
1402
1403 int src_cpu, src_nid;
1404 int dst_cpu, dst_nid;
1405
1406 struct numa_stats src_stats, dst_stats;
1407
Wanpeng Li40ea2b42013-12-05 19:10:17 +08001408 int imbalance_pct;
Rik van Riel7bd95322014-10-17 03:29:51 -04001409 int dist;
Mel Gormanfb13c7e2013-10-07 11:29:17 +01001410
1411 struct task_struct *best_task;
1412 long best_imp;
Mel Gorman58d081b2013-10-07 11:29:10 +01001413 int best_cpu;
1414};
1415
Mel Gormanfb13c7e2013-10-07 11:29:17 +01001416static void task_numa_assign(struct task_numa_env *env,
1417 struct task_struct *p, long imp)
1418{
1419 if (env->best_task)
1420 put_task_struct(env->best_task);
Oleg Nesterovbac78572016-05-18 21:57:33 +02001421 if (p)
1422 get_task_struct(p);
Mel Gormanfb13c7e2013-10-07 11:29:17 +01001423
1424 env->best_task = p;
1425 env->best_imp = imp;
1426 env->best_cpu = env->dst_cpu;
1427}
1428
Rik van Riel28a21742014-06-23 11:46:13 -04001429static bool load_too_imbalanced(long src_load, long dst_load,
Rik van Riele63da032014-05-14 13:22:21 -04001430 struct task_numa_env *env)
1431{
Rik van Riele4991b22015-05-27 15:04:27 -04001432 long imb, old_imb;
1433 long orig_src_load, orig_dst_load;
Rik van Riel28a21742014-06-23 11:46:13 -04001434 long src_capacity, dst_capacity;
1435
1436 /*
1437 * The load is corrected for the CPU capacity available on each node.
1438 *
1439 * src_load dst_load
1440 * ------------ vs ---------
1441 * src_capacity dst_capacity
1442 */
1443 src_capacity = env->src_stats.compute_capacity;
1444 dst_capacity = env->dst_stats.compute_capacity;
Rik van Riele63da032014-05-14 13:22:21 -04001445
1446 /* We care about the slope of the imbalance, not the direction. */
Rik van Riele4991b22015-05-27 15:04:27 -04001447 if (dst_load < src_load)
1448 swap(dst_load, src_load);
Rik van Riele63da032014-05-14 13:22:21 -04001449
1450 /* Is the difference below the threshold? */
Rik van Riele4991b22015-05-27 15:04:27 -04001451 imb = dst_load * src_capacity * 100 -
1452 src_load * dst_capacity * env->imbalance_pct;
Rik van Riele63da032014-05-14 13:22:21 -04001453 if (imb <= 0)
1454 return false;
1455
1456 /*
1457 * The imbalance is above the allowed threshold.
Rik van Riele4991b22015-05-27 15:04:27 -04001458 * Compare it with the old imbalance.
Rik van Riele63da032014-05-14 13:22:21 -04001459 */
Rik van Riel28a21742014-06-23 11:46:13 -04001460 orig_src_load = env->src_stats.load;
Rik van Riele4991b22015-05-27 15:04:27 -04001461 orig_dst_load = env->dst_stats.load;
Rik van Riel28a21742014-06-23 11:46:13 -04001462
Rik van Riele4991b22015-05-27 15:04:27 -04001463 if (orig_dst_load < orig_src_load)
1464 swap(orig_dst_load, orig_src_load);
Rik van Riele63da032014-05-14 13:22:21 -04001465
Rik van Riele4991b22015-05-27 15:04:27 -04001466 old_imb = orig_dst_load * src_capacity * 100 -
1467 orig_src_load * dst_capacity * env->imbalance_pct;
1468
1469 /* Would this change make things worse? */
1470 return (imb > old_imb);
Rik van Riele63da032014-05-14 13:22:21 -04001471}
1472
Mel Gormanfb13c7e2013-10-07 11:29:17 +01001473/*
1474 * This checks if the overall compute and NUMA accesses of the system would
1475 * be improved if the source tasks was migrated to the target dst_cpu taking
1476 * into account that it might be best if task running on the dst_cpu should
1477 * be exchanged with the source task
1478 */
Rik van Riel887c2902013-10-07 11:29:31 +01001479static void task_numa_compare(struct task_numa_env *env,
1480 long taskimp, long groupimp)
Mel Gormanfb13c7e2013-10-07 11:29:17 +01001481{
1482 struct rq *src_rq = cpu_rq(env->src_cpu);
1483 struct rq *dst_rq = cpu_rq(env->dst_cpu);
1484 struct task_struct *cur;
Rik van Riel28a21742014-06-23 11:46:13 -04001485 long src_load, dst_load;
Mel Gormanfb13c7e2013-10-07 11:29:17 +01001486 long load;
Rik van Riel1c5d3eb2014-06-23 11:46:15 -04001487 long imp = env->p->numa_group ? groupimp : taskimp;
Rik van Riel0132c3e2014-06-23 11:46:16 -04001488 long moveimp = imp;
Rik van Riel7bd95322014-10-17 03:29:51 -04001489 int dist = env->dist;
Mel Gormanfb13c7e2013-10-07 11:29:17 +01001490
1491 rcu_read_lock();
Oleg Nesterovbac78572016-05-18 21:57:33 +02001492 cur = task_rcu_dereference(&dst_rq->curr);
1493 if (cur && ((cur->flags & PF_EXITING) || is_idle_task(cur)))
Mel Gormanfb13c7e2013-10-07 11:29:17 +01001494 cur = NULL;
1495
1496 /*
Peter Zijlstra7af68332014-11-10 10:54:35 +01001497 * Because we have preemption enabled we can get migrated around and
1498 * end try selecting ourselves (current == env->p) as a swap candidate.
1499 */
1500 if (cur == env->p)
1501 goto unlock;
1502
1503 /*
Mel Gormanfb13c7e2013-10-07 11:29:17 +01001504 * "imp" is the fault differential for the source task between the
1505 * source and destination node. Calculate the total differential for
1506 * the source task and potential destination task. The more negative
1507 * the value is, the more rmeote accesses that would be expected to
1508 * be incurred if the tasks were swapped.
1509 */
1510 if (cur) {
1511 /* Skip this swap candidate if cannot move to the source cpu */
1512 if (!cpumask_test_cpu(env->src_cpu, tsk_cpus_allowed(cur)))
1513 goto unlock;
1514
Rik van Riel887c2902013-10-07 11:29:31 +01001515 /*
1516 * If dst and source tasks are in the same NUMA group, or not
Rik van Rielca28aa532013-10-07 11:29:32 +01001517 * in any group then look only at task weights.
Rik van Riel887c2902013-10-07 11:29:31 +01001518 */
Rik van Rielca28aa532013-10-07 11:29:32 +01001519 if (cur->numa_group == env->p->numa_group) {
Rik van Riel7bd95322014-10-17 03:29:51 -04001520 imp = taskimp + task_weight(cur, env->src_nid, dist) -
1521 task_weight(cur, env->dst_nid, dist);
Rik van Rielca28aa532013-10-07 11:29:32 +01001522 /*
1523 * Add some hysteresis to prevent swapping the
1524 * tasks within a group over tiny differences.
1525 */
1526 if (cur->numa_group)
1527 imp -= imp/16;
Rik van Riel887c2902013-10-07 11:29:31 +01001528 } else {
Rik van Rielca28aa532013-10-07 11:29:32 +01001529 /*
1530 * Compare the group weights. If a task is all by
1531 * itself (not part of a group), use the task weight
1532 * instead.
1533 */
Rik van Rielca28aa532013-10-07 11:29:32 +01001534 if (cur->numa_group)
Rik van Riel7bd95322014-10-17 03:29:51 -04001535 imp += group_weight(cur, env->src_nid, dist) -
1536 group_weight(cur, env->dst_nid, dist);
Rik van Rielca28aa532013-10-07 11:29:32 +01001537 else
Rik van Riel7bd95322014-10-17 03:29:51 -04001538 imp += task_weight(cur, env->src_nid, dist) -
1539 task_weight(cur, env->dst_nid, dist);
Rik van Riel887c2902013-10-07 11:29:31 +01001540 }
Mel Gormanfb13c7e2013-10-07 11:29:17 +01001541 }
1542
Rik van Riel0132c3e2014-06-23 11:46:16 -04001543 if (imp <= env->best_imp && moveimp <= env->best_imp)
Mel Gormanfb13c7e2013-10-07 11:29:17 +01001544 goto unlock;
1545
1546 if (!cur) {
1547 /* Is there capacity at our destination? */
Rik van Rielb932c032014-08-04 13:23:27 -04001548 if (env->src_stats.nr_running <= env->src_stats.task_capacity &&
Nicolas Pitre1b6a7492014-05-26 18:19:35 -04001549 !env->dst_stats.has_free_capacity)
Mel Gormanfb13c7e2013-10-07 11:29:17 +01001550 goto unlock;
1551
1552 goto balance;
1553 }
1554
1555 /* Balance doesn't matter much if we're running a task per cpu */
Rik van Riel0132c3e2014-06-23 11:46:16 -04001556 if (imp > env->best_imp && src_rq->nr_running == 1 &&
1557 dst_rq->nr_running == 1)
Mel Gormanfb13c7e2013-10-07 11:29:17 +01001558 goto assign;
1559
1560 /*
1561 * In the overloaded case, try and keep the load balanced.
1562 */
1563balance:
Peter Zijlstrae720fff2014-07-11 16:01:53 +02001564 load = task_h_load(env->p);
1565 dst_load = env->dst_stats.load + load;
1566 src_load = env->src_stats.load - load;
Mel Gormanfb13c7e2013-10-07 11:29:17 +01001567
Rik van Riel0132c3e2014-06-23 11:46:16 -04001568 if (moveimp > imp && moveimp > env->best_imp) {
1569 /*
1570 * If the improvement from just moving env->p direction is
1571 * better than swapping tasks around, check if a move is
1572 * possible. Store a slightly smaller score than moveimp,
1573 * so an actually idle CPU will win.
1574 */
1575 if (!load_too_imbalanced(src_load, dst_load, env)) {
1576 imp = moveimp - 1;
1577 cur = NULL;
1578 goto assign;
1579 }
1580 }
1581
1582 if (imp <= env->best_imp)
1583 goto unlock;
1584
Mel Gormanfb13c7e2013-10-07 11:29:17 +01001585 if (cur) {
Peter Zijlstrae720fff2014-07-11 16:01:53 +02001586 load = task_h_load(cur);
1587 dst_load -= load;
1588 src_load += load;
Mel Gormanfb13c7e2013-10-07 11:29:17 +01001589 }
1590
Rik van Riel28a21742014-06-23 11:46:13 -04001591 if (load_too_imbalanced(src_load, dst_load, env))
Mel Gormanfb13c7e2013-10-07 11:29:17 +01001592 goto unlock;
1593
Rik van Rielba7e5a22014-09-04 16:35:30 -04001594 /*
1595 * One idle CPU per node is evaluated for a task numa move.
1596 * Call select_idle_sibling to maybe find a better one.
1597 */
Peter Zijlstra10e2f1a2016-05-09 10:38:05 +02001598 if (!cur) {
1599 /*
1600 * select_idle_siblings() uses an per-cpu cpumask that
1601 * can be used from IRQ context.
1602 */
1603 local_irq_disable();
Morten Rasmussen772bd008c2016-06-22 18:03:13 +01001604 env->dst_cpu = select_idle_sibling(env->p, env->src_cpu,
1605 env->dst_cpu);
Peter Zijlstra10e2f1a2016-05-09 10:38:05 +02001606 local_irq_enable();
1607 }
Rik van Rielba7e5a22014-09-04 16:35:30 -04001608
Mel Gormanfb13c7e2013-10-07 11:29:17 +01001609assign:
1610 task_numa_assign(env, cur, imp);
1611unlock:
1612 rcu_read_unlock();
1613}
1614
Rik van Riel887c2902013-10-07 11:29:31 +01001615static void task_numa_find_cpu(struct task_numa_env *env,
1616 long taskimp, long groupimp)
Mel Gorman2c8a50a2013-10-07 11:29:18 +01001617{
1618 int cpu;
1619
1620 for_each_cpu(cpu, cpumask_of_node(env->dst_nid)) {
1621 /* Skip this CPU if the source task cannot migrate */
1622 if (!cpumask_test_cpu(cpu, tsk_cpus_allowed(env->p)))
1623 continue;
1624
1625 env->dst_cpu = cpu;
Rik van Riel887c2902013-10-07 11:29:31 +01001626 task_numa_compare(env, taskimp, groupimp);
Mel Gorman2c8a50a2013-10-07 11:29:18 +01001627 }
1628}
1629
Rik van Riel6f9aad02015-05-28 09:52:49 -04001630/* Only move tasks to a NUMA node less busy than the current node. */
1631static bool numa_has_capacity(struct task_numa_env *env)
1632{
1633 struct numa_stats *src = &env->src_stats;
1634 struct numa_stats *dst = &env->dst_stats;
1635
1636 if (src->has_free_capacity && !dst->has_free_capacity)
1637 return false;
1638
1639 /*
1640 * Only consider a task move if the source has a higher load
1641 * than the destination, corrected for CPU capacity on each node.
1642 *
1643 * src->load dst->load
1644 * --------------------- vs ---------------------
1645 * src->compute_capacity dst->compute_capacity
1646 */
Srikar Dronamraju44dcb042015-06-16 17:26:00 +05301647 if (src->load * dst->compute_capacity * env->imbalance_pct >
1648
1649 dst->load * src->compute_capacity * 100)
Rik van Riel6f9aad02015-05-28 09:52:49 -04001650 return true;
1651
1652 return false;
1653}
1654
Mel Gorman58d081b2013-10-07 11:29:10 +01001655static int task_numa_migrate(struct task_struct *p)
Mel Gormane6628d52013-10-07 11:29:02 +01001656{
Mel Gorman58d081b2013-10-07 11:29:10 +01001657 struct task_numa_env env = {
1658 .p = p,
Mel Gormanfb13c7e2013-10-07 11:29:17 +01001659
Mel Gorman58d081b2013-10-07 11:29:10 +01001660 .src_cpu = task_cpu(p),
Ingo Molnarb32e86b2013-10-07 11:29:30 +01001661 .src_nid = task_node(p),
Mel Gormanfb13c7e2013-10-07 11:29:17 +01001662
1663 .imbalance_pct = 112,
1664
1665 .best_task = NULL,
1666 .best_imp = 0,
Rik van Riel4142c3e2016-01-25 17:07:39 -05001667 .best_cpu = -1,
Mel Gorman58d081b2013-10-07 11:29:10 +01001668 };
1669 struct sched_domain *sd;
Rik van Riel887c2902013-10-07 11:29:31 +01001670 unsigned long taskweight, groupweight;
Rik van Riel7bd95322014-10-17 03:29:51 -04001671 int nid, ret, dist;
Rik van Riel887c2902013-10-07 11:29:31 +01001672 long taskimp, groupimp;
Mel Gormane6628d52013-10-07 11:29:02 +01001673
Mel Gorman58d081b2013-10-07 11:29:10 +01001674 /*
Mel Gormanfb13c7e2013-10-07 11:29:17 +01001675 * Pick the lowest SD_NUMA domain, as that would have the smallest
1676 * imbalance and would be the first to start moving tasks about.
1677 *
1678 * And we want to avoid any moving of tasks about, as that would create
1679 * random movement of tasks -- counter the numa conditions we're trying
1680 * to satisfy here.
Mel Gorman58d081b2013-10-07 11:29:10 +01001681 */
Mel Gormane6628d52013-10-07 11:29:02 +01001682 rcu_read_lock();
Mel Gormanfb13c7e2013-10-07 11:29:17 +01001683 sd = rcu_dereference(per_cpu(sd_numa, env.src_cpu));
Rik van Riel46a73e82013-11-11 19:29:25 -05001684 if (sd)
1685 env.imbalance_pct = 100 + (sd->imbalance_pct - 100) / 2;
Mel Gormane6628d52013-10-07 11:29:02 +01001686 rcu_read_unlock();
1687
Rik van Riel46a73e82013-11-11 19:29:25 -05001688 /*
1689 * Cpusets can break the scheduler domain tree into smaller
1690 * balance domains, some of which do not cross NUMA boundaries.
1691 * Tasks that are "trapped" in such domains cannot be migrated
1692 * elsewhere, so there is no point in (re)trying.
1693 */
1694 if (unlikely(!sd)) {
Wanpeng Lide1b3012013-12-12 15:23:24 +08001695 p->numa_preferred_nid = task_node(p);
Rik van Riel46a73e82013-11-11 19:29:25 -05001696 return -EINVAL;
1697 }
1698
Mel Gorman2c8a50a2013-10-07 11:29:18 +01001699 env.dst_nid = p->numa_preferred_nid;
Rik van Riel7bd95322014-10-17 03:29:51 -04001700 dist = env.dist = node_distance(env.src_nid, env.dst_nid);
1701 taskweight = task_weight(p, env.src_nid, dist);
1702 groupweight = group_weight(p, env.src_nid, dist);
1703 update_numa_stats(&env.src_stats, env.src_nid);
1704 taskimp = task_weight(p, env.dst_nid, dist) - taskweight;
1705 groupimp = group_weight(p, env.dst_nid, dist) - groupweight;
Mel Gorman2c8a50a2013-10-07 11:29:18 +01001706 update_numa_stats(&env.dst_stats, env.dst_nid);
Mel Gorman58d081b2013-10-07 11:29:10 +01001707
Rik van Riela43455a2014-06-04 16:09:42 -04001708 /* Try to find a spot on the preferred nid. */
Rik van Riel6f9aad02015-05-28 09:52:49 -04001709 if (numa_has_capacity(&env))
1710 task_numa_find_cpu(&env, taskimp, groupimp);
Rik van Riele1dda8a2013-10-07 11:29:19 +01001711
Rik van Riel9de05d42014-10-09 17:27:47 -04001712 /*
1713 * Look at other nodes in these cases:
1714 * - there is no space available on the preferred_nid
1715 * - the task is part of a numa_group that is interleaved across
1716 * multiple NUMA nodes; in order to better consolidate the group,
1717 * we need to check other locations.
1718 */
Rik van Riel4142c3e2016-01-25 17:07:39 -05001719 if (env.best_cpu == -1 || (p->numa_group && p->numa_group->active_nodes > 1)) {
Mel Gorman2c8a50a2013-10-07 11:29:18 +01001720 for_each_online_node(nid) {
1721 if (nid == env.src_nid || nid == p->numa_preferred_nid)
Mel Gormanfb13c7e2013-10-07 11:29:17 +01001722 continue;
1723
Rik van Riel7bd95322014-10-17 03:29:51 -04001724 dist = node_distance(env.src_nid, env.dst_nid);
Rik van Riel6c6b1192014-10-17 03:29:52 -04001725 if (sched_numa_topology_type == NUMA_BACKPLANE &&
1726 dist != env.dist) {
1727 taskweight = task_weight(p, env.src_nid, dist);
1728 groupweight = group_weight(p, env.src_nid, dist);
1729 }
Rik van Riel7bd95322014-10-17 03:29:51 -04001730
Mel Gorman83e1d2c2013-10-07 11:29:27 +01001731 /* Only consider nodes where both task and groups benefit */
Rik van Riel7bd95322014-10-17 03:29:51 -04001732 taskimp = task_weight(p, nid, dist) - taskweight;
1733 groupimp = group_weight(p, nid, dist) - groupweight;
Rik van Riel887c2902013-10-07 11:29:31 +01001734 if (taskimp < 0 && groupimp < 0)
Mel Gorman2c8a50a2013-10-07 11:29:18 +01001735 continue;
1736
Rik van Riel7bd95322014-10-17 03:29:51 -04001737 env.dist = dist;
Mel Gorman2c8a50a2013-10-07 11:29:18 +01001738 env.dst_nid = nid;
1739 update_numa_stats(&env.dst_stats, env.dst_nid);
Rik van Riel6f9aad02015-05-28 09:52:49 -04001740 if (numa_has_capacity(&env))
1741 task_numa_find_cpu(&env, taskimp, groupimp);
Mel Gorman58d081b2013-10-07 11:29:10 +01001742 }
1743 }
1744
Rik van Riel68d1b022014-04-11 13:00:29 -04001745 /*
1746 * If the task is part of a workload that spans multiple NUMA nodes,
1747 * and is migrating into one of the workload's active nodes, remember
1748 * this node as the task's preferred numa node, so the workload can
1749 * settle down.
1750 * A task that migrated to a second choice node will be better off
1751 * trying for a better one later. Do not set the preferred node here.
1752 */
Rik van Rieldb015da2014-06-23 11:41:34 -04001753 if (p->numa_group) {
Rik van Riel4142c3e2016-01-25 17:07:39 -05001754 struct numa_group *ng = p->numa_group;
1755
Rik van Rieldb015da2014-06-23 11:41:34 -04001756 if (env.best_cpu == -1)
1757 nid = env.src_nid;
1758 else
1759 nid = env.dst_nid;
1760
Rik van Riel4142c3e2016-01-25 17:07:39 -05001761 if (ng->active_nodes > 1 && numa_is_active_node(env.dst_nid, ng))
Rik van Rieldb015da2014-06-23 11:41:34 -04001762 sched_setnuma(p, env.dst_nid);
1763 }
1764
1765 /* No better CPU than the current one was found. */
1766 if (env.best_cpu == -1)
1767 return -EAGAIN;
Peter Zijlstra0ec8aa02013-10-07 11:29:33 +01001768
Rik van Riel04bb2f92013-10-07 11:29:36 +01001769 /*
1770 * Reset the scan period if the task is being rescheduled on an
1771 * alternative node to recheck if the tasks is now properly placed.
1772 */
1773 p->numa_scan_period = task_scan_min(p);
1774
Mel Gormanfb13c7e2013-10-07 11:29:17 +01001775 if (env.best_task == NULL) {
Mel Gorman286549d2014-01-21 15:51:03 -08001776 ret = migrate_task_to(p, env.best_cpu);
1777 if (ret != 0)
1778 trace_sched_stick_numa(p, env.src_cpu, env.best_cpu);
Mel Gormanfb13c7e2013-10-07 11:29:17 +01001779 return ret;
1780 }
1781
1782 ret = migrate_swap(p, env.best_task);
Mel Gorman286549d2014-01-21 15:51:03 -08001783 if (ret != 0)
1784 trace_sched_stick_numa(p, env.src_cpu, task_cpu(env.best_task));
Mel Gormanfb13c7e2013-10-07 11:29:17 +01001785 put_task_struct(env.best_task);
1786 return ret;
Mel Gormane6628d52013-10-07 11:29:02 +01001787}
1788
Mel Gorman6b9a7462013-10-07 11:29:11 +01001789/* Attempt to migrate a task to a CPU on the preferred node. */
1790static void numa_migrate_preferred(struct task_struct *p)
1791{
Rik van Riel5085e2a2014-04-11 13:00:28 -04001792 unsigned long interval = HZ;
1793
Rik van Riel2739d3e2013-10-07 11:29:41 +01001794 /* This task has no NUMA fault statistics yet */
Iulia Manda44dba3d2014-10-31 02:13:31 +02001795 if (unlikely(p->numa_preferred_nid == -1 || !p->numa_faults))
Rik van Riel2739d3e2013-10-07 11:29:41 +01001796 return;
1797
1798 /* Periodically retry migrating the task to the preferred node */
Rik van Riel5085e2a2014-04-11 13:00:28 -04001799 interval = min(interval, msecs_to_jiffies(p->numa_scan_period) / 16);
1800 p->numa_migrate_retry = jiffies + interval;
Rik van Riel2739d3e2013-10-07 11:29:41 +01001801
Mel Gorman6b9a7462013-10-07 11:29:11 +01001802 /* Success if task is already running on preferred CPU */
Wanpeng Lide1b3012013-12-12 15:23:24 +08001803 if (task_node(p) == p->numa_preferred_nid)
Mel Gorman6b9a7462013-10-07 11:29:11 +01001804 return;
1805
Mel Gorman6b9a7462013-10-07 11:29:11 +01001806 /* Otherwise, try migrate to a CPU on the preferred node */
Rik van Riel2739d3e2013-10-07 11:29:41 +01001807 task_numa_migrate(p);
Mel Gorman6b9a7462013-10-07 11:29:11 +01001808}
1809
Rik van Riel04bb2f92013-10-07 11:29:36 +01001810/*
Rik van Riel4142c3e2016-01-25 17:07:39 -05001811 * Find out how many nodes on the workload is actively running on. Do this by
Rik van Riel20e07de2014-01-27 17:03:43 -05001812 * tracking the nodes from which NUMA hinting faults are triggered. This can
1813 * be different from the set of nodes where the workload's memory is currently
1814 * located.
Rik van Riel20e07de2014-01-27 17:03:43 -05001815 */
Rik van Riel4142c3e2016-01-25 17:07:39 -05001816static void numa_group_count_active_nodes(struct numa_group *numa_group)
Rik van Riel20e07de2014-01-27 17:03:43 -05001817{
1818 unsigned long faults, max_faults = 0;
Rik van Riel4142c3e2016-01-25 17:07:39 -05001819 int nid, active_nodes = 0;
Rik van Riel20e07de2014-01-27 17:03:43 -05001820
1821 for_each_online_node(nid) {
1822 faults = group_faults_cpu(numa_group, nid);
1823 if (faults > max_faults)
1824 max_faults = faults;
1825 }
1826
1827 for_each_online_node(nid) {
1828 faults = group_faults_cpu(numa_group, nid);
Rik van Riel4142c3e2016-01-25 17:07:39 -05001829 if (faults * ACTIVE_NODE_FRACTION > max_faults)
1830 active_nodes++;
Rik van Riel20e07de2014-01-27 17:03:43 -05001831 }
Rik van Riel4142c3e2016-01-25 17:07:39 -05001832
1833 numa_group->max_faults_cpu = max_faults;
1834 numa_group->active_nodes = active_nodes;
Rik van Riel20e07de2014-01-27 17:03:43 -05001835}
1836
1837/*
Rik van Riel04bb2f92013-10-07 11:29:36 +01001838 * When adapting the scan rate, the period is divided into NUMA_PERIOD_SLOTS
1839 * increments. The more local the fault statistics are, the higher the scan
Rik van Riela22b4b02014-06-23 11:41:35 -04001840 * period will be for the next scan window. If local/(local+remote) ratio is
1841 * below NUMA_PERIOD_THRESHOLD (where range of ratio is 1..NUMA_PERIOD_SLOTS)
1842 * the scan period will decrease. Aim for 70% local accesses.
Rik van Riel04bb2f92013-10-07 11:29:36 +01001843 */
1844#define NUMA_PERIOD_SLOTS 10
Rik van Riela22b4b02014-06-23 11:41:35 -04001845#define NUMA_PERIOD_THRESHOLD 7
Rik van Riel04bb2f92013-10-07 11:29:36 +01001846
1847/*
1848 * Increase the scan period (slow down scanning) if the majority of
1849 * our memory is already on our local node, or if the majority of
1850 * the page accesses are shared with other processes.
1851 * Otherwise, decrease the scan period.
1852 */
1853static void update_task_scan_period(struct task_struct *p,
1854 unsigned long shared, unsigned long private)
1855{
1856 unsigned int period_slot;
1857 int ratio;
1858 int diff;
1859
1860 unsigned long remote = p->numa_faults_locality[0];
1861 unsigned long local = p->numa_faults_locality[1];
1862
1863 /*
1864 * If there were no record hinting faults then either the task is
1865 * completely idle or all activity is areas that are not of interest
Mel Gorman074c2382015-03-25 15:55:42 -07001866 * to automatic numa balancing. Related to that, if there were failed
1867 * migration then it implies we are migrating too quickly or the local
1868 * node is overloaded. In either case, scan slower
Rik van Riel04bb2f92013-10-07 11:29:36 +01001869 */
Mel Gorman074c2382015-03-25 15:55:42 -07001870 if (local + shared == 0 || p->numa_faults_locality[2]) {
Rik van Riel04bb2f92013-10-07 11:29:36 +01001871 p->numa_scan_period = min(p->numa_scan_period_max,
1872 p->numa_scan_period << 1);
1873
1874 p->mm->numa_next_scan = jiffies +
1875 msecs_to_jiffies(p->numa_scan_period);
1876
1877 return;
1878 }
1879
1880 /*
1881 * Prepare to scale scan period relative to the current period.
1882 * == NUMA_PERIOD_THRESHOLD scan period stays the same
1883 * < NUMA_PERIOD_THRESHOLD scan period decreases (scan faster)
1884 * >= NUMA_PERIOD_THRESHOLD scan period increases (scan slower)
1885 */
1886 period_slot = DIV_ROUND_UP(p->numa_scan_period, NUMA_PERIOD_SLOTS);
1887 ratio = (local * NUMA_PERIOD_SLOTS) / (local + remote);
1888 if (ratio >= NUMA_PERIOD_THRESHOLD) {
1889 int slot = ratio - NUMA_PERIOD_THRESHOLD;
1890 if (!slot)
1891 slot = 1;
1892 diff = slot * period_slot;
1893 } else {
1894 diff = -(NUMA_PERIOD_THRESHOLD - ratio) * period_slot;
1895
1896 /*
1897 * Scale scan rate increases based on sharing. There is an
1898 * inverse relationship between the degree of sharing and
1899 * the adjustment made to the scanning period. Broadly
1900 * speaking the intent is that there is little point
1901 * scanning faster if shared accesses dominate as it may
1902 * simply bounce migrations uselessly
1903 */
Yasuaki Ishimatsu2847c902014-10-22 16:04:35 +09001904 ratio = DIV_ROUND_UP(private * NUMA_PERIOD_SLOTS, (private + shared + 1));
Rik van Riel04bb2f92013-10-07 11:29:36 +01001905 diff = (diff * ratio) / NUMA_PERIOD_SLOTS;
1906 }
1907
1908 p->numa_scan_period = clamp(p->numa_scan_period + diff,
1909 task_scan_min(p), task_scan_max(p));
1910 memset(p->numa_faults_locality, 0, sizeof(p->numa_faults_locality));
1911}
1912
Rik van Riel7e2703e2014-01-27 17:03:45 -05001913/*
1914 * Get the fraction of time the task has been running since the last
1915 * NUMA placement cycle. The scheduler keeps similar statistics, but
1916 * decays those on a 32ms period, which is orders of magnitude off
1917 * from the dozens-of-seconds NUMA balancing period. Use the scheduler
1918 * stats only if the task is so new there are no NUMA statistics yet.
1919 */
1920static u64 numa_get_avg_runtime(struct task_struct *p, u64 *period)
1921{
1922 u64 runtime, delta, now;
1923 /* Use the start of this time slice to avoid calculations. */
1924 now = p->se.exec_start;
1925 runtime = p->se.sum_exec_runtime;
1926
1927 if (p->last_task_numa_placement) {
1928 delta = runtime - p->last_sum_exec_runtime;
1929 *period = now - p->last_task_numa_placement;
1930 } else {
Yuyang Du9d89c252015-07-15 08:04:37 +08001931 delta = p->se.avg.load_sum / p->se.load.weight;
1932 *period = LOAD_AVG_MAX;
Rik van Riel7e2703e2014-01-27 17:03:45 -05001933 }
1934
1935 p->last_sum_exec_runtime = runtime;
1936 p->last_task_numa_placement = now;
1937
1938 return delta;
1939}
1940
Rik van Riel54009412014-10-17 03:29:53 -04001941/*
1942 * Determine the preferred nid for a task in a numa_group. This needs to
1943 * be done in a way that produces consistent results with group_weight,
1944 * otherwise workloads might not converge.
1945 */
1946static int preferred_group_nid(struct task_struct *p, int nid)
1947{
1948 nodemask_t nodes;
1949 int dist;
1950
1951 /* Direct connections between all NUMA nodes. */
1952 if (sched_numa_topology_type == NUMA_DIRECT)
1953 return nid;
1954
1955 /*
1956 * On a system with glueless mesh NUMA topology, group_weight
1957 * scores nodes according to the number of NUMA hinting faults on
1958 * both the node itself, and on nearby nodes.
1959 */
1960 if (sched_numa_topology_type == NUMA_GLUELESS_MESH) {
1961 unsigned long score, max_score = 0;
1962 int node, max_node = nid;
1963
1964 dist = sched_max_numa_distance;
1965
1966 for_each_online_node(node) {
1967 score = group_weight(p, node, dist);
1968 if (score > max_score) {
1969 max_score = score;
1970 max_node = node;
1971 }
1972 }
1973 return max_node;
1974 }
1975
1976 /*
1977 * Finding the preferred nid in a system with NUMA backplane
1978 * interconnect topology is more involved. The goal is to locate
1979 * tasks from numa_groups near each other in the system, and
1980 * untangle workloads from different sides of the system. This requires
1981 * searching down the hierarchy of node groups, recursively searching
1982 * inside the highest scoring group of nodes. The nodemask tricks
1983 * keep the complexity of the search down.
1984 */
1985 nodes = node_online_map;
1986 for (dist = sched_max_numa_distance; dist > LOCAL_DISTANCE; dist--) {
1987 unsigned long max_faults = 0;
Jan Beulich81907472015-01-23 08:25:38 +00001988 nodemask_t max_group = NODE_MASK_NONE;
Rik van Riel54009412014-10-17 03:29:53 -04001989 int a, b;
1990
1991 /* Are there nodes at this distance from each other? */
1992 if (!find_numa_distance(dist))
1993 continue;
1994
1995 for_each_node_mask(a, nodes) {
1996 unsigned long faults = 0;
1997 nodemask_t this_group;
1998 nodes_clear(this_group);
1999
2000 /* Sum group's NUMA faults; includes a==b case. */
2001 for_each_node_mask(b, nodes) {
2002 if (node_distance(a, b) < dist) {
2003 faults += group_faults(p, b);
2004 node_set(b, this_group);
2005 node_clear(b, nodes);
2006 }
2007 }
2008
2009 /* Remember the top group. */
2010 if (faults > max_faults) {
2011 max_faults = faults;
2012 max_group = this_group;
2013 /*
2014 * subtle: at the smallest distance there is
2015 * just one node left in each "group", the
2016 * winner is the preferred nid.
2017 */
2018 nid = a;
2019 }
2020 }
2021 /* Next round, evaluate the nodes within max_group. */
Jan Beulich890a5402015-02-09 12:30:00 +01002022 if (!max_faults)
2023 break;
Rik van Riel54009412014-10-17 03:29:53 -04002024 nodes = max_group;
2025 }
2026 return nid;
2027}
2028
Peter Zijlstracbee9f82012-10-25 14:16:43 +02002029static void task_numa_placement(struct task_struct *p)
2030{
Mel Gorman83e1d2c2013-10-07 11:29:27 +01002031 int seq, nid, max_nid = -1, max_group_nid = -1;
2032 unsigned long max_faults = 0, max_group_faults = 0;
Rik van Riel04bb2f92013-10-07 11:29:36 +01002033 unsigned long fault_types[2] = { 0, 0 };
Rik van Riel7e2703e2014-01-27 17:03:45 -05002034 unsigned long total_faults;
2035 u64 runtime, period;
Mel Gorman7dbd13e2013-10-07 11:29:29 +01002036 spinlock_t *group_lock = NULL;
Peter Zijlstracbee9f82012-10-25 14:16:43 +02002037
Jason Low7e5a2c12015-04-30 17:28:14 -07002038 /*
2039 * The p->mm->numa_scan_seq field gets updated without
2040 * exclusive access. Use READ_ONCE() here to ensure
2041 * that the field is read in a single access:
2042 */
Jason Low316c1608d2015-04-28 13:00:20 -07002043 seq = READ_ONCE(p->mm->numa_scan_seq);
Peter Zijlstracbee9f82012-10-25 14:16:43 +02002044 if (p->numa_scan_seq == seq)
2045 return;
2046 p->numa_scan_seq = seq;
Mel Gorman598f0ec2013-10-07 11:28:55 +01002047 p->numa_scan_period_max = task_scan_max(p);
Peter Zijlstracbee9f82012-10-25 14:16:43 +02002048
Rik van Riel7e2703e2014-01-27 17:03:45 -05002049 total_faults = p->numa_faults_locality[0] +
2050 p->numa_faults_locality[1];
2051 runtime = numa_get_avg_runtime(p, &period);
2052
Mel Gorman7dbd13e2013-10-07 11:29:29 +01002053 /* If the task is part of a group prevent parallel updates to group stats */
2054 if (p->numa_group) {
2055 group_lock = &p->numa_group->lock;
Mike Galbraith60e69ee2014-04-07 10:55:15 +02002056 spin_lock_irq(group_lock);
Mel Gorman7dbd13e2013-10-07 11:29:29 +01002057 }
2058
Mel Gorman688b7582013-10-07 11:28:58 +01002059 /* Find the node with the highest number of faults */
2060 for_each_online_node(nid) {
Iulia Manda44dba3d2014-10-31 02:13:31 +02002061 /* Keep track of the offsets in numa_faults array */
2062 int mem_idx, membuf_idx, cpu_idx, cpubuf_idx;
Mel Gorman83e1d2c2013-10-07 11:29:27 +01002063 unsigned long faults = 0, group_faults = 0;
Iulia Manda44dba3d2014-10-31 02:13:31 +02002064 int priv;
Mel Gorman745d6142013-10-07 11:28:59 +01002065
Rik van Rielbe1e4e72014-01-27 17:03:48 -05002066 for (priv = 0; priv < NR_NUMA_HINT_FAULT_TYPES; priv++) {
Rik van Riel7e2703e2014-01-27 17:03:45 -05002067 long diff, f_diff, f_weight;
Peter Zijlstra8c8a7432013-10-07 11:29:21 +01002068
Iulia Manda44dba3d2014-10-31 02:13:31 +02002069 mem_idx = task_faults_idx(NUMA_MEM, nid, priv);
2070 membuf_idx = task_faults_idx(NUMA_MEMBUF, nid, priv);
2071 cpu_idx = task_faults_idx(NUMA_CPU, nid, priv);
2072 cpubuf_idx = task_faults_idx(NUMA_CPUBUF, nid, priv);
Mel Gorman745d6142013-10-07 11:28:59 +01002073
Mel Gormanac8e8952013-10-07 11:29:03 +01002074 /* Decay existing window, copy faults since last scan */
Iulia Manda44dba3d2014-10-31 02:13:31 +02002075 diff = p->numa_faults[membuf_idx] - p->numa_faults[mem_idx] / 2;
2076 fault_types[priv] += p->numa_faults[membuf_idx];
2077 p->numa_faults[membuf_idx] = 0;
Mel Gormanfb13c7e2013-10-07 11:29:17 +01002078
Rik van Riel7e2703e2014-01-27 17:03:45 -05002079 /*
2080 * Normalize the faults_from, so all tasks in a group
2081 * count according to CPU use, instead of by the raw
2082 * number of faults. Tasks with little runtime have
2083 * little over-all impact on throughput, and thus their
2084 * faults are less important.
2085 */
2086 f_weight = div64_u64(runtime << 16, period + 1);
Iulia Manda44dba3d2014-10-31 02:13:31 +02002087 f_weight = (f_weight * p->numa_faults[cpubuf_idx]) /
Rik van Riel7e2703e2014-01-27 17:03:45 -05002088 (total_faults + 1);
Iulia Manda44dba3d2014-10-31 02:13:31 +02002089 f_diff = f_weight - p->numa_faults[cpu_idx] / 2;
2090 p->numa_faults[cpubuf_idx] = 0;
Rik van Riel50ec8a42014-01-27 17:03:42 -05002091
Iulia Manda44dba3d2014-10-31 02:13:31 +02002092 p->numa_faults[mem_idx] += diff;
2093 p->numa_faults[cpu_idx] += f_diff;
2094 faults += p->numa_faults[mem_idx];
Mel Gorman83e1d2c2013-10-07 11:29:27 +01002095 p->total_numa_faults += diff;
Peter Zijlstra8c8a7432013-10-07 11:29:21 +01002096 if (p->numa_group) {
Iulia Manda44dba3d2014-10-31 02:13:31 +02002097 /*
2098 * safe because we can only change our own group
2099 *
2100 * mem_idx represents the offset for a given
2101 * nid and priv in a specific region because it
2102 * is at the beginning of the numa_faults array.
2103 */
2104 p->numa_group->faults[mem_idx] += diff;
2105 p->numa_group->faults_cpu[mem_idx] += f_diff;
Mel Gorman989348b2013-10-07 11:29:40 +01002106 p->numa_group->total_faults += diff;
Iulia Manda44dba3d2014-10-31 02:13:31 +02002107 group_faults += p->numa_group->faults[mem_idx];
Peter Zijlstra8c8a7432013-10-07 11:29:21 +01002108 }
Mel Gormanac8e8952013-10-07 11:29:03 +01002109 }
2110
Mel Gorman688b7582013-10-07 11:28:58 +01002111 if (faults > max_faults) {
2112 max_faults = faults;
2113 max_nid = nid;
2114 }
Mel Gorman83e1d2c2013-10-07 11:29:27 +01002115
2116 if (group_faults > max_group_faults) {
2117 max_group_faults = group_faults;
2118 max_group_nid = nid;
2119 }
2120 }
2121
Rik van Riel04bb2f92013-10-07 11:29:36 +01002122 update_task_scan_period(p, fault_types[0], fault_types[1]);
2123
Mel Gorman7dbd13e2013-10-07 11:29:29 +01002124 if (p->numa_group) {
Rik van Riel4142c3e2016-01-25 17:07:39 -05002125 numa_group_count_active_nodes(p->numa_group);
Mike Galbraith60e69ee2014-04-07 10:55:15 +02002126 spin_unlock_irq(group_lock);
Rik van Riel54009412014-10-17 03:29:53 -04002127 max_nid = preferred_group_nid(p, max_group_nid);
Mel Gorman688b7582013-10-07 11:28:58 +01002128 }
2129
Rik van Rielbb97fc32014-06-04 16:33:15 -04002130 if (max_faults) {
2131 /* Set the new preferred node */
2132 if (max_nid != p->numa_preferred_nid)
2133 sched_setnuma(p, max_nid);
2134
2135 if (task_node(p) != p->numa_preferred_nid)
2136 numa_migrate_preferred(p);
Mel Gorman3a7053b2013-10-07 11:29:00 +01002137 }
Peter Zijlstracbee9f82012-10-25 14:16:43 +02002138}
2139
Peter Zijlstra8c8a7432013-10-07 11:29:21 +01002140static inline int get_numa_group(struct numa_group *grp)
2141{
2142 return atomic_inc_not_zero(&grp->refcount);
2143}
2144
2145static inline void put_numa_group(struct numa_group *grp)
2146{
2147 if (atomic_dec_and_test(&grp->refcount))
2148 kfree_rcu(grp, rcu);
2149}
2150
Mel Gorman3e6a9412013-10-07 11:29:35 +01002151static void task_numa_group(struct task_struct *p, int cpupid, int flags,
2152 int *priv)
Peter Zijlstra8c8a7432013-10-07 11:29:21 +01002153{
2154 struct numa_group *grp, *my_grp;
2155 struct task_struct *tsk;
2156 bool join = false;
2157 int cpu = cpupid_to_cpu(cpupid);
2158 int i;
2159
2160 if (unlikely(!p->numa_group)) {
2161 unsigned int size = sizeof(struct numa_group) +
Rik van Riel50ec8a42014-01-27 17:03:42 -05002162 4*nr_node_ids*sizeof(unsigned long);
Peter Zijlstra8c8a7432013-10-07 11:29:21 +01002163
2164 grp = kzalloc(size, GFP_KERNEL | __GFP_NOWARN);
2165 if (!grp)
2166 return;
2167
2168 atomic_set(&grp->refcount, 1);
Rik van Riel4142c3e2016-01-25 17:07:39 -05002169 grp->active_nodes = 1;
2170 grp->max_faults_cpu = 0;
Peter Zijlstra8c8a7432013-10-07 11:29:21 +01002171 spin_lock_init(&grp->lock);
Mel Gormane29cf082013-10-07 11:29:22 +01002172 grp->gid = p->pid;
Rik van Riel50ec8a42014-01-27 17:03:42 -05002173 /* Second half of the array tracks nids where faults happen */
Rik van Rielbe1e4e72014-01-27 17:03:48 -05002174 grp->faults_cpu = grp->faults + NR_NUMA_HINT_FAULT_TYPES *
2175 nr_node_ids;
Peter Zijlstra8c8a7432013-10-07 11:29:21 +01002176
Rik van Rielbe1e4e72014-01-27 17:03:48 -05002177 for (i = 0; i < NR_NUMA_HINT_FAULT_STATS * nr_node_ids; i++)
Iulia Manda44dba3d2014-10-31 02:13:31 +02002178 grp->faults[i] = p->numa_faults[i];
Peter Zijlstra8c8a7432013-10-07 11:29:21 +01002179
Mel Gorman989348b2013-10-07 11:29:40 +01002180 grp->total_faults = p->total_numa_faults;
Mel Gorman83e1d2c2013-10-07 11:29:27 +01002181
Peter Zijlstra8c8a7432013-10-07 11:29:21 +01002182 grp->nr_tasks++;
2183 rcu_assign_pointer(p->numa_group, grp);
2184 }
2185
2186 rcu_read_lock();
Jason Low316c1608d2015-04-28 13:00:20 -07002187 tsk = READ_ONCE(cpu_rq(cpu)->curr);
Peter Zijlstra8c8a7432013-10-07 11:29:21 +01002188
2189 if (!cpupid_match_pid(tsk, cpupid))
Peter Zijlstra33547812013-10-09 10:24:48 +02002190 goto no_join;
Peter Zijlstra8c8a7432013-10-07 11:29:21 +01002191
2192 grp = rcu_dereference(tsk->numa_group);
2193 if (!grp)
Peter Zijlstra33547812013-10-09 10:24:48 +02002194 goto no_join;
Peter Zijlstra8c8a7432013-10-07 11:29:21 +01002195
2196 my_grp = p->numa_group;
2197 if (grp == my_grp)
Peter Zijlstra33547812013-10-09 10:24:48 +02002198 goto no_join;
Peter Zijlstra8c8a7432013-10-07 11:29:21 +01002199
2200 /*
2201 * Only join the other group if its bigger; if we're the bigger group,
2202 * the other task will join us.
2203 */
2204 if (my_grp->nr_tasks > grp->nr_tasks)
Peter Zijlstra33547812013-10-09 10:24:48 +02002205 goto no_join;
Peter Zijlstra8c8a7432013-10-07 11:29:21 +01002206
2207 /*
2208 * Tie-break on the grp address.
2209 */
2210 if (my_grp->nr_tasks == grp->nr_tasks && my_grp > grp)
Peter Zijlstra33547812013-10-09 10:24:48 +02002211 goto no_join;
Peter Zijlstra8c8a7432013-10-07 11:29:21 +01002212
Rik van Rieldabe1d92013-10-07 11:29:34 +01002213 /* Always join threads in the same process. */
2214 if (tsk->mm == current->mm)
2215 join = true;
Peter Zijlstra8c8a7432013-10-07 11:29:21 +01002216
Rik van Rieldabe1d92013-10-07 11:29:34 +01002217 /* Simple filter to avoid false positives due to PID collisions */
2218 if (flags & TNF_SHARED)
2219 join = true;
2220
Mel Gorman3e6a9412013-10-07 11:29:35 +01002221 /* Update priv based on whether false sharing was detected */
2222 *priv = !join;
2223
Rik van Rieldabe1d92013-10-07 11:29:34 +01002224 if (join && !get_numa_group(grp))
Peter Zijlstra33547812013-10-09 10:24:48 +02002225 goto no_join;
Peter Zijlstra8c8a7432013-10-07 11:29:21 +01002226
Peter Zijlstra8c8a7432013-10-07 11:29:21 +01002227 rcu_read_unlock();
2228
2229 if (!join)
2230 return;
2231
Mike Galbraith60e69ee2014-04-07 10:55:15 +02002232 BUG_ON(irqs_disabled());
2233 double_lock_irq(&my_grp->lock, &grp->lock);
Peter Zijlstra8c8a7432013-10-07 11:29:21 +01002234
Rik van Rielbe1e4e72014-01-27 17:03:48 -05002235 for (i = 0; i < NR_NUMA_HINT_FAULT_STATS * nr_node_ids; i++) {
Iulia Manda44dba3d2014-10-31 02:13:31 +02002236 my_grp->faults[i] -= p->numa_faults[i];
2237 grp->faults[i] += p->numa_faults[i];
Mel Gorman989348b2013-10-07 11:29:40 +01002238 }
2239 my_grp->total_faults -= p->total_numa_faults;
2240 grp->total_faults += p->total_numa_faults;
2241
Peter Zijlstra8c8a7432013-10-07 11:29:21 +01002242 my_grp->nr_tasks--;
2243 grp->nr_tasks++;
2244
2245 spin_unlock(&my_grp->lock);
Mike Galbraith60e69ee2014-04-07 10:55:15 +02002246 spin_unlock_irq(&grp->lock);
Peter Zijlstra8c8a7432013-10-07 11:29:21 +01002247
2248 rcu_assign_pointer(p->numa_group, grp);
2249
2250 put_numa_group(my_grp);
Peter Zijlstra33547812013-10-09 10:24:48 +02002251 return;
2252
2253no_join:
2254 rcu_read_unlock();
2255 return;
Peter Zijlstra8c8a7432013-10-07 11:29:21 +01002256}
2257
2258void task_numa_free(struct task_struct *p)
2259{
2260 struct numa_group *grp = p->numa_group;
Iulia Manda44dba3d2014-10-31 02:13:31 +02002261 void *numa_faults = p->numa_faults;
Steven Rostedte9dd6852014-05-27 17:02:04 -04002262 unsigned long flags;
2263 int i;
Peter Zijlstra8c8a7432013-10-07 11:29:21 +01002264
2265 if (grp) {
Steven Rostedte9dd6852014-05-27 17:02:04 -04002266 spin_lock_irqsave(&grp->lock, flags);
Rik van Rielbe1e4e72014-01-27 17:03:48 -05002267 for (i = 0; i < NR_NUMA_HINT_FAULT_STATS * nr_node_ids; i++)
Iulia Manda44dba3d2014-10-31 02:13:31 +02002268 grp->faults[i] -= p->numa_faults[i];
Mel Gorman989348b2013-10-07 11:29:40 +01002269 grp->total_faults -= p->total_numa_faults;
2270
Peter Zijlstra8c8a7432013-10-07 11:29:21 +01002271 grp->nr_tasks--;
Steven Rostedte9dd6852014-05-27 17:02:04 -04002272 spin_unlock_irqrestore(&grp->lock, flags);
Andreea-Cristina Bernat35b123e2014-08-22 17:50:43 +03002273 RCU_INIT_POINTER(p->numa_group, NULL);
Peter Zijlstra8c8a7432013-10-07 11:29:21 +01002274 put_numa_group(grp);
2275 }
2276
Iulia Manda44dba3d2014-10-31 02:13:31 +02002277 p->numa_faults = NULL;
Rik van Riel82727012013-10-07 11:29:28 +01002278 kfree(numa_faults);
Peter Zijlstra8c8a7432013-10-07 11:29:21 +01002279}
2280
Peter Zijlstracbee9f82012-10-25 14:16:43 +02002281/*
2282 * Got a PROT_NONE fault for a page on @node.
2283 */
Rik van Riel58b46da2014-01-27 17:03:47 -05002284void task_numa_fault(int last_cpupid, int mem_node, int pages, int flags)
Peter Zijlstracbee9f82012-10-25 14:16:43 +02002285{
2286 struct task_struct *p = current;
Peter Zijlstra6688cc02013-10-07 11:29:24 +01002287 bool migrated = flags & TNF_MIGRATED;
Rik van Riel58b46da2014-01-27 17:03:47 -05002288 int cpu_node = task_node(current);
Rik van Riel792568e2014-04-11 13:00:27 -04002289 int local = !!(flags & TNF_FAULT_LOCAL);
Rik van Riel4142c3e2016-01-25 17:07:39 -05002290 struct numa_group *ng;
Mel Gormanac8e8952013-10-07 11:29:03 +01002291 int priv;
Peter Zijlstracbee9f82012-10-25 14:16:43 +02002292
Srikar Dronamraju2a595722015-08-11 21:54:21 +05302293 if (!static_branch_likely(&sched_numa_balancing))
Mel Gorman1a687c22012-11-22 11:16:36 +00002294 return;
2295
Mel Gorman9ff1d9f2013-10-07 11:29:04 +01002296 /* for example, ksmd faulting in a user's mm */
2297 if (!p->mm)
2298 return;
2299
Mel Gormanf809ca92013-10-07 11:28:57 +01002300 /* Allocate buffer to track faults on a per-node basis */
Iulia Manda44dba3d2014-10-31 02:13:31 +02002301 if (unlikely(!p->numa_faults)) {
2302 int size = sizeof(*p->numa_faults) *
Rik van Rielbe1e4e72014-01-27 17:03:48 -05002303 NR_NUMA_HINT_FAULT_BUCKETS * nr_node_ids;
Mel Gormanf809ca92013-10-07 11:28:57 +01002304
Iulia Manda44dba3d2014-10-31 02:13:31 +02002305 p->numa_faults = kzalloc(size, GFP_KERNEL|__GFP_NOWARN);
2306 if (!p->numa_faults)
Mel Gormanf809ca92013-10-07 11:28:57 +01002307 return;
Mel Gorman745d6142013-10-07 11:28:59 +01002308
Mel Gorman83e1d2c2013-10-07 11:29:27 +01002309 p->total_numa_faults = 0;
Rik van Riel04bb2f92013-10-07 11:29:36 +01002310 memset(p->numa_faults_locality, 0, sizeof(p->numa_faults_locality));
Mel Gormanf809ca92013-10-07 11:28:57 +01002311 }
Peter Zijlstracbee9f82012-10-25 14:16:43 +02002312
Mel Gormanfb003b82012-11-15 09:01:14 +00002313 /*
Peter Zijlstra8c8a7432013-10-07 11:29:21 +01002314 * First accesses are treated as private, otherwise consider accesses
2315 * to be private if the accessing pid has not changed
2316 */
2317 if (unlikely(last_cpupid == (-1 & LAST_CPUPID_MASK))) {
2318 priv = 1;
2319 } else {
2320 priv = cpupid_match_pid(p, last_cpupid);
Peter Zijlstra6688cc02013-10-07 11:29:24 +01002321 if (!priv && !(flags & TNF_NO_GROUP))
Mel Gorman3e6a9412013-10-07 11:29:35 +01002322 task_numa_group(p, last_cpupid, flags, &priv);
Peter Zijlstra8c8a7432013-10-07 11:29:21 +01002323 }
2324
Rik van Riel792568e2014-04-11 13:00:27 -04002325 /*
2326 * If a workload spans multiple NUMA nodes, a shared fault that
2327 * occurs wholly within the set of nodes that the workload is
2328 * actively using should be counted as local. This allows the
2329 * scan rate to slow down when a workload has settled down.
2330 */
Rik van Riel4142c3e2016-01-25 17:07:39 -05002331 ng = p->numa_group;
2332 if (!priv && !local && ng && ng->active_nodes > 1 &&
2333 numa_is_active_node(cpu_node, ng) &&
2334 numa_is_active_node(mem_node, ng))
Rik van Riel792568e2014-04-11 13:00:27 -04002335 local = 1;
2336
Peter Zijlstracbee9f82012-10-25 14:16:43 +02002337 task_numa_placement(p);
Mel Gormanf809ca92013-10-07 11:28:57 +01002338
Rik van Riel2739d3e2013-10-07 11:29:41 +01002339 /*
2340 * Retry task to preferred node migration periodically, in case it
2341 * case it previously failed, or the scheduler moved us.
2342 */
2343 if (time_after(jiffies, p->numa_migrate_retry))
Mel Gorman6b9a7462013-10-07 11:29:11 +01002344 numa_migrate_preferred(p);
2345
Ingo Molnarb32e86b2013-10-07 11:29:30 +01002346 if (migrated)
2347 p->numa_pages_migrated += pages;
Mel Gorman074c2382015-03-25 15:55:42 -07002348 if (flags & TNF_MIGRATE_FAIL)
2349 p->numa_faults_locality[2] += pages;
Ingo Molnarb32e86b2013-10-07 11:29:30 +01002350
Iulia Manda44dba3d2014-10-31 02:13:31 +02002351 p->numa_faults[task_faults_idx(NUMA_MEMBUF, mem_node, priv)] += pages;
2352 p->numa_faults[task_faults_idx(NUMA_CPUBUF, cpu_node, priv)] += pages;
Rik van Riel792568e2014-04-11 13:00:27 -04002353 p->numa_faults_locality[local] += pages;
Peter Zijlstracbee9f82012-10-25 14:16:43 +02002354}
2355
Peter Zijlstra6e5fb222012-10-25 14:16:45 +02002356static void reset_ptenuma_scan(struct task_struct *p)
2357{
Jason Low7e5a2c12015-04-30 17:28:14 -07002358 /*
2359 * We only did a read acquisition of the mmap sem, so
2360 * p->mm->numa_scan_seq is written to without exclusive access
2361 * and the update is not guaranteed to be atomic. That's not
2362 * much of an issue though, since this is just used for
2363 * statistical sampling. Use READ_ONCE/WRITE_ONCE, which are not
2364 * expensive, to avoid any form of compiler optimizations:
2365 */
Jason Low316c1608d2015-04-28 13:00:20 -07002366 WRITE_ONCE(p->mm->numa_scan_seq, READ_ONCE(p->mm->numa_scan_seq) + 1);
Peter Zijlstra6e5fb222012-10-25 14:16:45 +02002367 p->mm->numa_scan_offset = 0;
2368}
2369
Peter Zijlstracbee9f82012-10-25 14:16:43 +02002370/*
2371 * The expensive part of numa migration is done from task_work context.
2372 * Triggered from task_tick_numa().
2373 */
2374void task_numa_work(struct callback_head *work)
2375{
2376 unsigned long migrate, next_scan, now = jiffies;
2377 struct task_struct *p = current;
2378 struct mm_struct *mm = p->mm;
Rik van Riel51170842015-11-05 15:56:23 -05002379 u64 runtime = p->se.sum_exec_runtime;
Peter Zijlstra6e5fb222012-10-25 14:16:45 +02002380 struct vm_area_struct *vma;
Mel Gorman9f406042012-11-14 18:34:32 +00002381 unsigned long start, end;
Mel Gorman598f0ec2013-10-07 11:28:55 +01002382 unsigned long nr_pte_updates = 0;
Rik van Riel4620f8c2015-09-11 09:00:27 -04002383 long pages, virtpages;
Peter Zijlstracbee9f82012-10-25 14:16:43 +02002384
Peter Zijlstra9148a3a2016-09-20 22:34:51 +02002385 SCHED_WARN_ON(p != container_of(work, struct task_struct, numa_work));
Peter Zijlstracbee9f82012-10-25 14:16:43 +02002386
2387 work->next = work; /* protect against double add */
2388 /*
2389 * Who cares about NUMA placement when they're dying.
2390 *
2391 * NOTE: make sure not to dereference p->mm before this check,
2392 * exit_task_work() happens _after_ exit_mm() so we could be called
2393 * without p->mm even though we still had it when we enqueued this
2394 * work.
2395 */
2396 if (p->flags & PF_EXITING)
2397 return;
2398
Mel Gorman930aa172013-10-07 11:29:37 +01002399 if (!mm->numa_next_scan) {
Mel Gorman7e8d16b2013-10-07 11:28:54 +01002400 mm->numa_next_scan = now +
2401 msecs_to_jiffies(sysctl_numa_balancing_scan_delay);
Mel Gormanb8593bf2012-11-21 01:18:23 +00002402 }
2403
2404 /*
Peter Zijlstracbee9f82012-10-25 14:16:43 +02002405 * Enforce maximal scan/migration frequency..
2406 */
2407 migrate = mm->numa_next_scan;
2408 if (time_before(now, migrate))
2409 return;
2410
Mel Gorman598f0ec2013-10-07 11:28:55 +01002411 if (p->numa_scan_period == 0) {
2412 p->numa_scan_period_max = task_scan_max(p);
2413 p->numa_scan_period = task_scan_min(p);
2414 }
Peter Zijlstracbee9f82012-10-25 14:16:43 +02002415
Mel Gormanfb003b82012-11-15 09:01:14 +00002416 next_scan = now + msecs_to_jiffies(p->numa_scan_period);
Peter Zijlstracbee9f82012-10-25 14:16:43 +02002417 if (cmpxchg(&mm->numa_next_scan, migrate, next_scan) != migrate)
2418 return;
2419
Mel Gormane14808b2012-11-19 10:59:15 +00002420 /*
Peter Zijlstra19a78d12013-10-07 11:28:51 +01002421 * Delay this task enough that another task of this mm will likely win
2422 * the next time around.
2423 */
2424 p->node_stamp += 2 * TICK_NSEC;
2425
Mel Gorman9f406042012-11-14 18:34:32 +00002426 start = mm->numa_scan_offset;
2427 pages = sysctl_numa_balancing_scan_size;
2428 pages <<= 20 - PAGE_SHIFT; /* MB in pages */
Rik van Riel4620f8c2015-09-11 09:00:27 -04002429 virtpages = pages * 8; /* Scan up to this much virtual space */
Mel Gorman9f406042012-11-14 18:34:32 +00002430 if (!pages)
2431 return;
Peter Zijlstracbee9f82012-10-25 14:16:43 +02002432
Rik van Riel4620f8c2015-09-11 09:00:27 -04002433
Peter Zijlstra6e5fb222012-10-25 14:16:45 +02002434 down_read(&mm->mmap_sem);
Mel Gorman9f406042012-11-14 18:34:32 +00002435 vma = find_vma(mm, start);
Peter Zijlstra6e5fb222012-10-25 14:16:45 +02002436 if (!vma) {
2437 reset_ptenuma_scan(p);
Mel Gorman9f406042012-11-14 18:34:32 +00002438 start = 0;
Peter Zijlstra6e5fb222012-10-25 14:16:45 +02002439 vma = mm->mmap;
Peter Zijlstracbee9f82012-10-25 14:16:43 +02002440 }
Mel Gorman9f406042012-11-14 18:34:32 +00002441 for (; vma; vma = vma->vm_next) {
Naoya Horiguchi6b79c572015-04-07 14:26:47 -07002442 if (!vma_migratable(vma) || !vma_policy_mof(vma) ||
Mel Gorman8e76d4e2015-06-10 11:15:00 -07002443 is_vm_hugetlb_page(vma) || (vma->vm_flags & VM_MIXEDMAP)) {
Peter Zijlstra6e5fb222012-10-25 14:16:45 +02002444 continue;
Naoya Horiguchi6b79c572015-04-07 14:26:47 -07002445 }
Peter Zijlstra6e5fb222012-10-25 14:16:45 +02002446
Mel Gorman4591ce4f2013-10-07 11:29:13 +01002447 /*
2448 * Shared library pages mapped by multiple processes are not
2449 * migrated as it is expected they are cache replicated. Avoid
2450 * hinting faults in read-only file-backed mappings or the vdso
2451 * as migrating the pages will be of marginal benefit.
2452 */
2453 if (!vma->vm_mm ||
2454 (vma->vm_file && (vma->vm_flags & (VM_READ|VM_WRITE)) == (VM_READ)))
2455 continue;
2456
Mel Gorman3c67f472013-12-18 17:08:40 -08002457 /*
2458 * Skip inaccessible VMAs to avoid any confusion between
2459 * PROT_NONE and NUMA hinting ptes
2460 */
2461 if (!(vma->vm_flags & (VM_READ | VM_EXEC | VM_WRITE)))
2462 continue;
2463
Mel Gorman9f406042012-11-14 18:34:32 +00002464 do {
2465 start = max(start, vma->vm_start);
2466 end = ALIGN(start + (pages << PAGE_SHIFT), HPAGE_SIZE);
2467 end = min(end, vma->vm_end);
Rik van Riel4620f8c2015-09-11 09:00:27 -04002468 nr_pte_updates = change_prot_numa(vma, start, end);
Mel Gorman598f0ec2013-10-07 11:28:55 +01002469
2470 /*
Rik van Riel4620f8c2015-09-11 09:00:27 -04002471 * Try to scan sysctl_numa_balancing_size worth of
2472 * hpages that have at least one present PTE that
2473 * is not already pte-numa. If the VMA contains
2474 * areas that are unused or already full of prot_numa
2475 * PTEs, scan up to virtpages, to skip through those
2476 * areas faster.
Mel Gorman598f0ec2013-10-07 11:28:55 +01002477 */
2478 if (nr_pte_updates)
2479 pages -= (end - start) >> PAGE_SHIFT;
Rik van Riel4620f8c2015-09-11 09:00:27 -04002480 virtpages -= (end - start) >> PAGE_SHIFT;
Peter Zijlstra6e5fb222012-10-25 14:16:45 +02002481
Mel Gorman9f406042012-11-14 18:34:32 +00002482 start = end;
Rik van Riel4620f8c2015-09-11 09:00:27 -04002483 if (pages <= 0 || virtpages <= 0)
Mel Gorman9f406042012-11-14 18:34:32 +00002484 goto out;
Rik van Riel3cf19622014-02-18 17:12:44 -05002485
2486 cond_resched();
Mel Gorman9f406042012-11-14 18:34:32 +00002487 } while (end != vma->vm_end);
Peter Zijlstra6e5fb222012-10-25 14:16:45 +02002488 }
2489
Mel Gorman9f406042012-11-14 18:34:32 +00002490out:
Peter Zijlstra6e5fb222012-10-25 14:16:45 +02002491 /*
Peter Zijlstrac69307d2013-10-07 11:28:41 +01002492 * It is possible to reach the end of the VMA list but the last few
2493 * VMAs are not guaranteed to the vma_migratable. If they are not, we
2494 * would find the !migratable VMA on the next scan but not reset the
2495 * scanner to the start so check it now.
Peter Zijlstra6e5fb222012-10-25 14:16:45 +02002496 */
2497 if (vma)
Mel Gorman9f406042012-11-14 18:34:32 +00002498 mm->numa_scan_offset = start;
Peter Zijlstra6e5fb222012-10-25 14:16:45 +02002499 else
2500 reset_ptenuma_scan(p);
2501 up_read(&mm->mmap_sem);
Rik van Riel51170842015-11-05 15:56:23 -05002502
2503 /*
2504 * Make sure tasks use at least 32x as much time to run other code
2505 * than they used here, to limit NUMA PTE scanning overhead to 3% max.
2506 * Usually update_task_scan_period slows down scanning enough; on an
2507 * overloaded system we need to limit overhead on a per task basis.
2508 */
2509 if (unlikely(p->se.sum_exec_runtime != runtime)) {
2510 u64 diff = p->se.sum_exec_runtime - runtime;
2511 p->node_stamp += 32 * diff;
2512 }
Peter Zijlstracbee9f82012-10-25 14:16:43 +02002513}
2514
2515/*
2516 * Drive the periodic memory faults..
2517 */
2518void task_tick_numa(struct rq *rq, struct task_struct *curr)
2519{
2520 struct callback_head *work = &curr->numa_work;
2521 u64 period, now;
2522
2523 /*
2524 * We don't care about NUMA placement if we don't have memory.
2525 */
2526 if (!curr->mm || (curr->flags & PF_EXITING) || work->next != work)
2527 return;
2528
2529 /*
2530 * Using runtime rather than walltime has the dual advantage that
2531 * we (mostly) drive the selection from busy threads and that the
2532 * task needs to have done some actual work before we bother with
2533 * NUMA placement.
2534 */
2535 now = curr->se.sum_exec_runtime;
2536 period = (u64)curr->numa_scan_period * NSEC_PER_MSEC;
2537
Rik van Riel25b3e5a2015-11-05 15:56:22 -05002538 if (now > curr->node_stamp + period) {
Peter Zijlstra4b96a29b2012-10-25 14:16:47 +02002539 if (!curr->node_stamp)
Mel Gorman598f0ec2013-10-07 11:28:55 +01002540 curr->numa_scan_period = task_scan_min(curr);
Peter Zijlstra19a78d12013-10-07 11:28:51 +01002541 curr->node_stamp += period;
Peter Zijlstracbee9f82012-10-25 14:16:43 +02002542
2543 if (!time_before(jiffies, curr->mm->numa_next_scan)) {
2544 init_task_work(work, task_numa_work); /* TODO: move this into sched_fork() */
2545 task_work_add(curr, work, true);
2546 }
2547 }
2548}
2549#else
2550static void task_tick_numa(struct rq *rq, struct task_struct *curr)
2551{
2552}
Peter Zijlstra0ec8aa02013-10-07 11:29:33 +01002553
2554static inline void account_numa_enqueue(struct rq *rq, struct task_struct *p)
2555{
2556}
2557
2558static inline void account_numa_dequeue(struct rq *rq, struct task_struct *p)
2559{
2560}
Peter Zijlstracbee9f82012-10-25 14:16:43 +02002561#endif /* CONFIG_NUMA_BALANCING */
2562
Dmitry Adamushko30cfdcf2007-10-15 17:00:07 +02002563static void
2564account_entity_enqueue(struct cfs_rq *cfs_rq, struct sched_entity *se)
2565{
2566 update_load_add(&cfs_rq->load, se->load.weight);
Peter Zijlstrac09595f2008-06-27 13:41:14 +02002567 if (!parent_entity(se))
Peter Zijlstra029632f2011-10-25 10:00:11 +02002568 update_load_add(&rq_of(cfs_rq)->load, se->load.weight);
Peter Zijlstra367456c2012-02-20 21:49:09 +01002569#ifdef CONFIG_SMP
Peter Zijlstra0ec8aa02013-10-07 11:29:33 +01002570 if (entity_is_task(se)) {
2571 struct rq *rq = rq_of(cfs_rq);
2572
2573 account_numa_enqueue(rq, task_of(se));
2574 list_add(&se->group_node, &rq->cfs_tasks);
2575 }
Peter Zijlstra367456c2012-02-20 21:49:09 +01002576#endif
Dmitry Adamushko30cfdcf2007-10-15 17:00:07 +02002577 cfs_rq->nr_running++;
Dmitry Adamushko30cfdcf2007-10-15 17:00:07 +02002578}
2579
2580static void
2581account_entity_dequeue(struct cfs_rq *cfs_rq, struct sched_entity *se)
2582{
2583 update_load_sub(&cfs_rq->load, se->load.weight);
Peter Zijlstrac09595f2008-06-27 13:41:14 +02002584 if (!parent_entity(se))
Peter Zijlstra029632f2011-10-25 10:00:11 +02002585 update_load_sub(&rq_of(cfs_rq)->load, se->load.weight);
Tim Chenbfdb1982016-02-01 14:47:59 -08002586#ifdef CONFIG_SMP
Peter Zijlstra0ec8aa02013-10-07 11:29:33 +01002587 if (entity_is_task(se)) {
2588 account_numa_dequeue(rq_of(cfs_rq), task_of(se));
Bharata B Raob87f1722008-09-25 09:53:54 +05302589 list_del_init(&se->group_node);
Peter Zijlstra0ec8aa02013-10-07 11:29:33 +01002590 }
Tim Chenbfdb1982016-02-01 14:47:59 -08002591#endif
Dmitry Adamushko30cfdcf2007-10-15 17:00:07 +02002592 cfs_rq->nr_running--;
Dmitry Adamushko30cfdcf2007-10-15 17:00:07 +02002593}
2594
Yong Zhang3ff6dca2011-01-24 15:33:52 +08002595#ifdef CONFIG_FAIR_GROUP_SCHED
2596# ifdef CONFIG_SMP
Paul Turner6d5ab292011-01-21 20:45:01 -08002597static long calc_cfs_shares(struct cfs_rq *cfs_rq, struct task_group *tg)
Yong Zhang3ff6dca2011-01-24 15:33:52 +08002598{
Peter Zijlstracf5f0ac2011-10-13 16:52:28 +02002599 long tg_weight, load, shares;
Yong Zhang3ff6dca2011-01-24 15:33:52 +08002600
Peter Zijlstraea1dc6f2016-06-24 16:11:02 +02002601 /*
2602 * This really should be: cfs_rq->avg.load_avg, but instead we use
2603 * cfs_rq->load.weight, which is its upper bound. This helps ramp up
2604 * the shares for small weight interactive tasks.
2605 */
2606 load = scale_load_down(cfs_rq->load.weight);
2607
2608 tg_weight = atomic_long_read(&tg->load_avg);
2609
2610 /* Ensure tg_weight >= load */
2611 tg_weight -= cfs_rq->tg_load_avg_contrib;
2612 tg_weight += load;
Yong Zhang3ff6dca2011-01-24 15:33:52 +08002613
Yong Zhang3ff6dca2011-01-24 15:33:52 +08002614 shares = (tg->shares * load);
Peter Zijlstracf5f0ac2011-10-13 16:52:28 +02002615 if (tg_weight)
2616 shares /= tg_weight;
Yong Zhang3ff6dca2011-01-24 15:33:52 +08002617
2618 if (shares < MIN_SHARES)
2619 shares = MIN_SHARES;
2620 if (shares > tg->shares)
2621 shares = tg->shares;
2622
2623 return shares;
2624}
Yong Zhang3ff6dca2011-01-24 15:33:52 +08002625# else /* CONFIG_SMP */
Paul Turner6d5ab292011-01-21 20:45:01 -08002626static inline long calc_cfs_shares(struct cfs_rq *cfs_rq, struct task_group *tg)
Yong Zhang3ff6dca2011-01-24 15:33:52 +08002627{
2628 return tg->shares;
2629}
Yong Zhang3ff6dca2011-01-24 15:33:52 +08002630# endif /* CONFIG_SMP */
Peter Zijlstraea1dc6f2016-06-24 16:11:02 +02002631
Peter Zijlstra2069dd72010-11-15 15:47:00 -08002632static void reweight_entity(struct cfs_rq *cfs_rq, struct sched_entity *se,
2633 unsigned long weight)
2634{
Paul Turner19e5eeb2010-12-15 19:10:18 -08002635 if (se->on_rq) {
2636 /* commit outstanding execution time */
2637 if (cfs_rq->curr == se)
2638 update_curr(cfs_rq);
Peter Zijlstra2069dd72010-11-15 15:47:00 -08002639 account_entity_dequeue(cfs_rq, se);
Paul Turner19e5eeb2010-12-15 19:10:18 -08002640 }
Peter Zijlstra2069dd72010-11-15 15:47:00 -08002641
2642 update_load_set(&se->load, weight);
2643
2644 if (se->on_rq)
2645 account_entity_enqueue(cfs_rq, se);
2646}
2647
Paul Turner82958362012-10-04 13:18:31 +02002648static inline int throttled_hierarchy(struct cfs_rq *cfs_rq);
2649
Paul Turner6d5ab292011-01-21 20:45:01 -08002650static void update_cfs_shares(struct cfs_rq *cfs_rq)
Peter Zijlstra2069dd72010-11-15 15:47:00 -08002651{
2652 struct task_group *tg;
2653 struct sched_entity *se;
Yong Zhang3ff6dca2011-01-24 15:33:52 +08002654 long shares;
Peter Zijlstra2069dd72010-11-15 15:47:00 -08002655
Peter Zijlstra2069dd72010-11-15 15:47:00 -08002656 tg = cfs_rq->tg;
2657 se = tg->se[cpu_of(rq_of(cfs_rq))];
Paul Turner64660c82011-07-21 09:43:36 -07002658 if (!se || throttled_hierarchy(cfs_rq))
Peter Zijlstra2069dd72010-11-15 15:47:00 -08002659 return;
Yong Zhang3ff6dca2011-01-24 15:33:52 +08002660#ifndef CONFIG_SMP
2661 if (likely(se->load.weight == tg->shares))
2662 return;
2663#endif
Paul Turner6d5ab292011-01-21 20:45:01 -08002664 shares = calc_cfs_shares(cfs_rq, tg);
Peter Zijlstra2069dd72010-11-15 15:47:00 -08002665
2666 reweight_entity(cfs_rq_of(se), se, shares);
2667}
2668#else /* CONFIG_FAIR_GROUP_SCHED */
Paul Turner6d5ab292011-01-21 20:45:01 -08002669static inline void update_cfs_shares(struct cfs_rq *cfs_rq)
Peter Zijlstra2069dd72010-11-15 15:47:00 -08002670{
2671}
2672#endif /* CONFIG_FAIR_GROUP_SCHED */
2673
Alex Shi141965c2013-06-26 13:05:39 +08002674#ifdef CONFIG_SMP
Paul Turner5b51f2f2012-10-04 13:18:32 +02002675/* Precomputed fixed inverse multiplies for multiplication by y^n */
2676static const u32 runnable_avg_yN_inv[] = {
2677 0xffffffff, 0xfa83b2da, 0xf5257d14, 0xefe4b99a, 0xeac0c6e6, 0xe5b906e6,
2678 0xe0ccdeeb, 0xdbfbb796, 0xd744fcc9, 0xd2a81d91, 0xce248c14, 0xc9b9bd85,
2679 0xc5672a10, 0xc12c4cc9, 0xbd08a39e, 0xb8fbaf46, 0xb504f333, 0xb123f581,
2680 0xad583ee9, 0xa9a15ab4, 0xa5fed6a9, 0xa2704302, 0x9ef5325f, 0x9b8d39b9,
2681 0x9837f050, 0x94f4efa8, 0x91c3d373, 0x8ea4398a, 0x8b95c1e3, 0x88980e80,
2682 0x85aac367, 0x82cd8698,
2683};
2684
2685/*
2686 * Precomputed \Sum y^k { 1<=k<=n }. These are floor(true_value) to prevent
2687 * over-estimates when re-combining.
2688 */
2689static const u32 runnable_avg_yN_sum[] = {
2690 0, 1002, 1982, 2941, 3880, 4798, 5697, 6576, 7437, 8279, 9103,
2691 9909,10698,11470,12226,12966,13690,14398,15091,15769,16433,17082,
2692 17718,18340,18949,19545,20128,20698,21256,21802,22336,22859,23371,
2693};
2694
2695/*
Yuyang Du7b20b912016-05-03 05:54:27 +08002696 * Precomputed \Sum y^k { 1<=k<=n, where n%32=0). Values are rolled down to
2697 * lower integers. See Documentation/scheduler/sched-avg.txt how these
2698 * were generated:
2699 */
2700static const u32 __accumulated_sum_N32[] = {
2701 0, 23371, 35056, 40899, 43820, 45281,
2702 46011, 46376, 46559, 46650, 46696, 46719,
2703};
2704
2705/*
Paul Turner9d85f212012-10-04 13:18:29 +02002706 * Approximate:
2707 * val * y^n, where y^32 ~= 0.5 (~1 scheduling period)
2708 */
2709static __always_inline u64 decay_load(u64 val, u64 n)
2710{
Paul Turner5b51f2f2012-10-04 13:18:32 +02002711 unsigned int local_n;
2712
2713 if (!n)
2714 return val;
2715 else if (unlikely(n > LOAD_AVG_PERIOD * 63))
2716 return 0;
2717
2718 /* after bounds checking we can collapse to 32-bit */
2719 local_n = n;
2720
2721 /*
2722 * As y^PERIOD = 1/2, we can combine
Zhihui Zhang9c58c792014-09-20 21:24:36 -04002723 * y^n = 1/2^(n/PERIOD) * y^(n%PERIOD)
2724 * With a look-up table which covers y^n (n<PERIOD)
Paul Turner5b51f2f2012-10-04 13:18:32 +02002725 *
2726 * To achieve constant time decay_load.
2727 */
2728 if (unlikely(local_n >= LOAD_AVG_PERIOD)) {
2729 val >>= local_n / LOAD_AVG_PERIOD;
2730 local_n %= LOAD_AVG_PERIOD;
Paul Turner9d85f212012-10-04 13:18:29 +02002731 }
2732
Yuyang Du9d89c252015-07-15 08:04:37 +08002733 val = mul_u64_u32_shr(val, runnable_avg_yN_inv[local_n], 32);
2734 return val;
Paul Turner5b51f2f2012-10-04 13:18:32 +02002735}
2736
2737/*
2738 * For updates fully spanning n periods, the contribution to runnable
2739 * average will be: \Sum 1024*y^n
2740 *
2741 * We can compute this reasonably efficiently by combining:
2742 * y^PERIOD = 1/2 with precomputed \Sum 1024*y^n {for n <PERIOD}
2743 */
2744static u32 __compute_runnable_contrib(u64 n)
2745{
2746 u32 contrib = 0;
2747
2748 if (likely(n <= LOAD_AVG_PERIOD))
2749 return runnable_avg_yN_sum[n];
2750 else if (unlikely(n >= LOAD_AVG_MAX_N))
2751 return LOAD_AVG_MAX;
2752
Yuyang Du7b20b912016-05-03 05:54:27 +08002753 /* Since n < LOAD_AVG_MAX_N, n/LOAD_AVG_PERIOD < 11 */
2754 contrib = __accumulated_sum_N32[n/LOAD_AVG_PERIOD];
2755 n %= LOAD_AVG_PERIOD;
Paul Turner5b51f2f2012-10-04 13:18:32 +02002756 contrib = decay_load(contrib, n);
2757 return contrib + runnable_avg_yN_sum[n];
Paul Turner9d85f212012-10-04 13:18:29 +02002758}
2759
Peter Zijlstra54a21382015-09-07 15:05:42 +02002760#define cap_scale(v, s) ((v)*(s) >> SCHED_CAPACITY_SHIFT)
Dietmar Eggemanne0f5f3a2015-08-14 17:23:09 +01002761
Paul Turner9d85f212012-10-04 13:18:29 +02002762/*
2763 * We can represent the historical contribution to runnable average as the
2764 * coefficients of a geometric series. To do this we sub-divide our runnable
2765 * history into segments of approximately 1ms (1024us); label the segment that
2766 * occurred N-ms ago p_N, with p_0 corresponding to the current period, e.g.
2767 *
2768 * [<- 1024us ->|<- 1024us ->|<- 1024us ->| ...
2769 * p0 p1 p2
2770 * (now) (~1ms ago) (~2ms ago)
2771 *
2772 * Let u_i denote the fraction of p_i that the entity was runnable.
2773 *
2774 * We then designate the fractions u_i as our co-efficients, yielding the
2775 * following representation of historical load:
2776 * u_0 + u_1*y + u_2*y^2 + u_3*y^3 + ...
2777 *
2778 * We choose y based on the with of a reasonably scheduling period, fixing:
2779 * y^32 = 0.5
2780 *
2781 * This means that the contribution to load ~32ms ago (u_32) will be weighted
2782 * approximately half as much as the contribution to load within the last ms
2783 * (u_0).
2784 *
2785 * When a period "rolls over" and we have new u_0`, multiplying the previous
2786 * sum again by y is sufficient to update:
2787 * load_avg = u_0` + y*(u_0 + u_1*y + u_2*y^2 + ... )
2788 * = u_0 + u_1*y + u_2*y^2 + ... [re-labeling u_i --> u_{i+1}]
2789 */
Yuyang Du9d89c252015-07-15 08:04:37 +08002790static __always_inline int
2791__update_load_avg(u64 now, int cpu, struct sched_avg *sa,
Yuyang Du13962232015-07-15 08:04:41 +08002792 unsigned long weight, int running, struct cfs_rq *cfs_rq)
Paul Turner9d85f212012-10-04 13:18:29 +02002793{
Dietmar Eggemanne0f5f3a2015-08-14 17:23:09 +01002794 u64 delta, scaled_delta, periods;
Yuyang Du9d89c252015-07-15 08:04:37 +08002795 u32 contrib;
Peter Zijlstra6115c792015-09-07 15:09:15 +02002796 unsigned int delta_w, scaled_delta_w, decayed = 0;
Dietmar Eggemann6f2b0452015-09-07 14:57:22 +01002797 unsigned long scale_freq, scale_cpu;
Paul Turner9d85f212012-10-04 13:18:29 +02002798
Yuyang Du9d89c252015-07-15 08:04:37 +08002799 delta = now - sa->last_update_time;
Paul Turner9d85f212012-10-04 13:18:29 +02002800 /*
2801 * This should only happen when time goes backwards, which it
2802 * unfortunately does during sched clock init when we swap over to TSC.
2803 */
2804 if ((s64)delta < 0) {
Yuyang Du9d89c252015-07-15 08:04:37 +08002805 sa->last_update_time = now;
Paul Turner9d85f212012-10-04 13:18:29 +02002806 return 0;
2807 }
2808
2809 /*
2810 * Use 1024ns as the unit of measurement since it's a reasonable
2811 * approximation of 1us and fast to compute.
2812 */
2813 delta >>= 10;
2814 if (!delta)
2815 return 0;
Yuyang Du9d89c252015-07-15 08:04:37 +08002816 sa->last_update_time = now;
Paul Turner9d85f212012-10-04 13:18:29 +02002817
Dietmar Eggemann6f2b0452015-09-07 14:57:22 +01002818 scale_freq = arch_scale_freq_capacity(NULL, cpu);
2819 scale_cpu = arch_scale_cpu_capacity(NULL, cpu);
Juri Lelli0a942002015-11-09 12:06:24 +00002820 trace_sched_contrib_scale_f(cpu, scale_freq, scale_cpu);
Dietmar Eggemann6f2b0452015-09-07 14:57:22 +01002821
Paul Turner9d85f212012-10-04 13:18:29 +02002822 /* delta_w is the amount already accumulated against our next period */
Yuyang Du9d89c252015-07-15 08:04:37 +08002823 delta_w = sa->period_contrib;
Paul Turner9d85f212012-10-04 13:18:29 +02002824 if (delta + delta_w >= 1024) {
Paul Turner9d85f212012-10-04 13:18:29 +02002825 decayed = 1;
2826
Yuyang Du9d89c252015-07-15 08:04:37 +08002827 /* how much left for next period will start over, we don't know yet */
2828 sa->period_contrib = 0;
2829
Paul Turner9d85f212012-10-04 13:18:29 +02002830 /*
2831 * Now that we know we're crossing a period boundary, figure
2832 * out how much from delta we need to complete the current
2833 * period and accrue it.
2834 */
2835 delta_w = 1024 - delta_w;
Peter Zijlstra54a21382015-09-07 15:05:42 +02002836 scaled_delta_w = cap_scale(delta_w, scale_freq);
Yuyang Du13962232015-07-15 08:04:41 +08002837 if (weight) {
Dietmar Eggemanne0f5f3a2015-08-14 17:23:09 +01002838 sa->load_sum += weight * scaled_delta_w;
2839 if (cfs_rq) {
2840 cfs_rq->runnable_load_sum +=
2841 weight * scaled_delta_w;
2842 }
Yuyang Du13962232015-07-15 08:04:41 +08002843 }
Vincent Guittot36ee28e2015-02-27 16:54:04 +01002844 if (running)
Peter Zijlstra006cdf02015-09-09 09:06:17 +02002845 sa->util_sum += scaled_delta_w * scale_cpu;
Paul Turner9d85f212012-10-04 13:18:29 +02002846
Paul Turner5b51f2f2012-10-04 13:18:32 +02002847 delta -= delta_w;
Paul Turner9d85f212012-10-04 13:18:29 +02002848
Paul Turner5b51f2f2012-10-04 13:18:32 +02002849 /* Figure out how many additional periods this update spans */
2850 periods = delta / 1024;
2851 delta %= 1024;
2852
Yuyang Du9d89c252015-07-15 08:04:37 +08002853 sa->load_sum = decay_load(sa->load_sum, periods + 1);
Yuyang Du13962232015-07-15 08:04:41 +08002854 if (cfs_rq) {
2855 cfs_rq->runnable_load_sum =
2856 decay_load(cfs_rq->runnable_load_sum, periods + 1);
2857 }
Yuyang Du9d89c252015-07-15 08:04:37 +08002858 sa->util_sum = decay_load((u64)(sa->util_sum), periods + 1);
Paul Turner5b51f2f2012-10-04 13:18:32 +02002859
2860 /* Efficiently calculate \sum (1..n_period) 1024*y^i */
Yuyang Du9d89c252015-07-15 08:04:37 +08002861 contrib = __compute_runnable_contrib(periods);
Peter Zijlstra54a21382015-09-07 15:05:42 +02002862 contrib = cap_scale(contrib, scale_freq);
Yuyang Du13962232015-07-15 08:04:41 +08002863 if (weight) {
Yuyang Du9d89c252015-07-15 08:04:37 +08002864 sa->load_sum += weight * contrib;
Yuyang Du13962232015-07-15 08:04:41 +08002865 if (cfs_rq)
2866 cfs_rq->runnable_load_sum += weight * contrib;
2867 }
Vincent Guittot36ee28e2015-02-27 16:54:04 +01002868 if (running)
Peter Zijlstra006cdf02015-09-09 09:06:17 +02002869 sa->util_sum += contrib * scale_cpu;
Paul Turner9d85f212012-10-04 13:18:29 +02002870 }
2871
2872 /* Remainder of delta accrued against u_0` */
Peter Zijlstra54a21382015-09-07 15:05:42 +02002873 scaled_delta = cap_scale(delta, scale_freq);
Yuyang Du13962232015-07-15 08:04:41 +08002874 if (weight) {
Dietmar Eggemanne0f5f3a2015-08-14 17:23:09 +01002875 sa->load_sum += weight * scaled_delta;
Yuyang Du13962232015-07-15 08:04:41 +08002876 if (cfs_rq)
Dietmar Eggemanne0f5f3a2015-08-14 17:23:09 +01002877 cfs_rq->runnable_load_sum += weight * scaled_delta;
Yuyang Du13962232015-07-15 08:04:41 +08002878 }
Vincent Guittot36ee28e2015-02-27 16:54:04 +01002879 if (running)
Peter Zijlstra006cdf02015-09-09 09:06:17 +02002880 sa->util_sum += scaled_delta * scale_cpu;
Yuyang Du9d89c252015-07-15 08:04:37 +08002881
2882 sa->period_contrib += delta;
2883
2884 if (decayed) {
2885 sa->load_avg = div_u64(sa->load_sum, LOAD_AVG_MAX);
Yuyang Du13962232015-07-15 08:04:41 +08002886 if (cfs_rq) {
2887 cfs_rq->runnable_load_avg =
2888 div_u64(cfs_rq->runnable_load_sum, LOAD_AVG_MAX);
2889 }
Peter Zijlstra006cdf02015-09-09 09:06:17 +02002890 sa->util_avg = sa->util_sum / LOAD_AVG_MAX;
Yuyang Du9d89c252015-07-15 08:04:37 +08002891 }
Paul Turner9d85f212012-10-04 13:18:29 +02002892
2893 return decayed;
2894}
2895
Paul Turnerc566e8e2012-10-04 13:18:30 +02002896#ifdef CONFIG_FAIR_GROUP_SCHED
Peter Zijlstra7c3edd22016-07-13 10:56:25 +02002897/**
2898 * update_tg_load_avg - update the tg's load avg
2899 * @cfs_rq: the cfs_rq whose avg changed
2900 * @force: update regardless of how small the difference
2901 *
2902 * This function 'ensures': tg->load_avg := \Sum tg->cfs_rq[]->avg.load.
2903 * However, because tg->load_avg is a global value there are performance
2904 * considerations.
2905 *
2906 * In order to avoid having to look at the other cfs_rq's, we use a
2907 * differential update where we store the last value we propagated. This in
2908 * turn allows skipping updates if the differential is 'small'.
2909 *
2910 * Updating tg's load_avg is necessary before update_cfs_share() (which is
2911 * done) and effective_load() (which is not done because it is too costly).
Paul Turnerbb17f652012-10-04 13:18:31 +02002912 */
Yuyang Du9d89c252015-07-15 08:04:37 +08002913static inline void update_tg_load_avg(struct cfs_rq *cfs_rq, int force)
Paul Turnerbb17f652012-10-04 13:18:31 +02002914{
Yuyang Du9d89c252015-07-15 08:04:37 +08002915 long delta = cfs_rq->avg.load_avg - cfs_rq->tg_load_avg_contrib;
Paul Turnerbb17f652012-10-04 13:18:31 +02002916
Waiman Longaa0b7ae2015-12-02 13:41:50 -05002917 /*
2918 * No need to update load_avg for root_task_group as it is not used.
2919 */
2920 if (cfs_rq->tg == &root_task_group)
2921 return;
2922
Yuyang Du9d89c252015-07-15 08:04:37 +08002923 if (force || abs(delta) > cfs_rq->tg_load_avg_contrib / 64) {
2924 atomic_long_add(delta, &cfs_rq->tg->load_avg);
2925 cfs_rq->tg_load_avg_contrib = cfs_rq->avg.load_avg;
Paul Turnerbb17f652012-10-04 13:18:31 +02002926 }
Paul Turner8165e142012-10-04 13:18:31 +02002927}
Dietmar Eggemannf5f97392014-02-26 11:19:33 +00002928
Byungchul Parkad936d82015-10-24 01:16:19 +09002929/*
2930 * Called within set_task_rq() right before setting a task's cpu. The
2931 * caller only guarantees p->pi_lock is held; no other assumptions,
2932 * including the state of rq->lock, should be made.
2933 */
2934void set_task_rq_fair(struct sched_entity *se,
2935 struct cfs_rq *prev, struct cfs_rq *next)
2936{
2937 if (!sched_feat(ATTACH_AGE_LOAD))
2938 return;
2939
2940 /*
2941 * We are supposed to update the task to "current" time, then its up to
2942 * date and ready to go to new CPU/cfs_rq. But we have difficulty in
2943 * getting what current time is, so simply throw away the out-of-date
2944 * time. This will result in the wakee task is less decayed, but giving
2945 * the wakee more load sounds not bad.
2946 */
2947 if (se->avg.last_update_time && prev) {
2948 u64 p_last_update_time;
2949 u64 n_last_update_time;
2950
2951#ifndef CONFIG_64BIT
2952 u64 p_last_update_time_copy;
2953 u64 n_last_update_time_copy;
2954
2955 do {
2956 p_last_update_time_copy = prev->load_last_update_time_copy;
2957 n_last_update_time_copy = next->load_last_update_time_copy;
2958
2959 smp_rmb();
2960
2961 p_last_update_time = prev->avg.last_update_time;
2962 n_last_update_time = next->avg.last_update_time;
2963
2964 } while (p_last_update_time != p_last_update_time_copy ||
2965 n_last_update_time != n_last_update_time_copy);
2966#else
2967 p_last_update_time = prev->avg.last_update_time;
2968 n_last_update_time = next->avg.last_update_time;
2969#endif
2970 __update_load_avg(p_last_update_time, cpu_of(rq_of(prev)),
2971 &se->avg, 0, 0, NULL);
2972 se->avg.last_update_time = n_last_update_time;
2973 }
2974}
Peter Zijlstra6e831252014-02-11 16:11:48 +01002975#else /* CONFIG_FAIR_GROUP_SCHED */
Yuyang Du9d89c252015-07-15 08:04:37 +08002976static inline void update_tg_load_avg(struct cfs_rq *cfs_rq, int force) {}
Peter Zijlstra6e831252014-02-11 16:11:48 +01002977#endif /* CONFIG_FAIR_GROUP_SCHED */
Paul Turnerc566e8e2012-10-04 13:18:30 +02002978
Steve Mucklea2c6c912016-03-24 15:26:07 -07002979static inline void cfs_rq_util_change(struct cfs_rq *cfs_rq)
Yuyang Du9d89c252015-07-15 08:04:37 +08002980{
Rafael J. Wysocki58919e82016-08-16 22:14:55 +02002981 if (&this_rq()->cfs == cfs_rq) {
Steve Muckle21e96f82016-03-21 17:21:07 -07002982 /*
2983 * There are a few boundary cases this might miss but it should
2984 * get called often enough that that should (hopefully) not be
2985 * a real problem -- added to that it only calls on the local
2986 * CPU, so if we enqueue remotely we'll miss an update, but
2987 * the next tick/schedule should update.
2988 *
2989 * It will not get called when we go idle, because the idle
2990 * thread is a different class (!fair), nor will the utilization
2991 * number include things like RT tasks.
2992 *
2993 * As is, the util number is not freq-invariant (we'd have to
2994 * implement arch_scale_freq_capacity() for that).
2995 *
2996 * See cpu_util().
2997 */
Rafael J. Wysocki12bde332016-08-10 03:11:17 +02002998 cpufreq_update_util(rq_of(cfs_rq), 0);
Steve Muckle21e96f82016-03-21 17:21:07 -07002999 }
Steve Mucklea2c6c912016-03-24 15:26:07 -07003000}
3001
Peter Zijlstra89741892016-06-16 10:50:40 +02003002/*
3003 * Unsigned subtract and clamp on underflow.
3004 *
3005 * Explicitly do a load-store to ensure the intermediate value never hits
3006 * memory. This allows lockless observations without ever seeing the negative
3007 * values.
3008 */
3009#define sub_positive(_ptr, _val) do { \
3010 typeof(_ptr) ptr = (_ptr); \
3011 typeof(*ptr) val = (_val); \
3012 typeof(*ptr) res, var = READ_ONCE(*ptr); \
3013 res = var - val; \
3014 if (res > var) \
3015 res = 0; \
3016 WRITE_ONCE(*ptr, res); \
3017} while (0)
3018
Peter Zijlstra3d30544f2016-06-21 14:27:50 +02003019/**
3020 * update_cfs_rq_load_avg - update the cfs_rq's load/util averages
3021 * @now: current time, as per cfs_rq_clock_task()
3022 * @cfs_rq: cfs_rq to update
3023 * @update_freq: should we call cfs_rq_util_change() or will the call do so
3024 *
3025 * The cfs_rq avg is the direct sum of all its entities (blocked and runnable)
3026 * avg. The immediate corollary is that all (fair) tasks must be attached, see
3027 * post_init_entity_util_avg().
3028 *
3029 * cfs_rq->avg is used for task_h_load() and update_cfs_share() for example.
3030 *
Peter Zijlstra7c3edd22016-07-13 10:56:25 +02003031 * Returns true if the load decayed or we removed load.
3032 *
3033 * Since both these conditions indicate a changed cfs_rq->avg.load we should
3034 * call update_tg_load_avg() when this function returns true.
Peter Zijlstra3d30544f2016-06-21 14:27:50 +02003035 */
Steve Mucklea2c6c912016-03-24 15:26:07 -07003036static inline int
3037update_cfs_rq_load_avg(u64 now, struct cfs_rq *cfs_rq, bool update_freq)
3038{
3039 struct sched_avg *sa = &cfs_rq->avg;
3040 int decayed, removed_load = 0, removed_util = 0;
3041
3042 if (atomic_long_read(&cfs_rq->removed_load_avg)) {
3043 s64 r = atomic_long_xchg(&cfs_rq->removed_load_avg, 0);
Peter Zijlstra89741892016-06-16 10:50:40 +02003044 sub_positive(&sa->load_avg, r);
3045 sub_positive(&sa->load_sum, r * LOAD_AVG_MAX);
Steve Mucklea2c6c912016-03-24 15:26:07 -07003046 removed_load = 1;
3047 }
3048
3049 if (atomic_long_read(&cfs_rq->removed_util_avg)) {
3050 long r = atomic_long_xchg(&cfs_rq->removed_util_avg, 0);
Peter Zijlstra89741892016-06-16 10:50:40 +02003051 sub_positive(&sa->util_avg, r);
3052 sub_positive(&sa->util_sum, r * LOAD_AVG_MAX);
Steve Mucklea2c6c912016-03-24 15:26:07 -07003053 removed_util = 1;
3054 }
3055
3056 decayed = __update_load_avg(now, cpu_of(rq_of(cfs_rq)), sa,
3057 scale_load_down(cfs_rq->load.weight), cfs_rq->curr != NULL, cfs_rq);
3058
3059#ifndef CONFIG_64BIT
3060 smp_wmb();
3061 cfs_rq->load_last_update_time_copy = sa->last_update_time;
3062#endif
3063
3064 if (update_freq && (decayed || removed_util))
3065 cfs_rq_util_change(cfs_rq);
Steve Muckle21e96f82016-03-21 17:21:07 -07003066
Steve Muckle41e0d372016-03-21 17:21:08 -07003067 return decayed || removed_load;
Yuyang Du9d89c252015-07-15 08:04:37 +08003068}
3069
3070/* Update task and its cfs_rq load average */
3071static inline void update_load_avg(struct sched_entity *se, int update_tg)
Paul Turner9d85f212012-10-04 13:18:29 +02003072{
Paul Turner2dac7542012-10-04 13:18:30 +02003073 struct cfs_rq *cfs_rq = cfs_rq_of(se);
Yuyang Du9d89c252015-07-15 08:04:37 +08003074 u64 now = cfs_rq_clock_task(cfs_rq);
Rafael J. Wysocki34e2c552016-02-15 20:20:42 +01003075 struct rq *rq = rq_of(cfs_rq);
3076 int cpu = cpu_of(rq);
Paul Turner2dac7542012-10-04 13:18:30 +02003077
Paul Turnerf1b17282012-10-04 13:18:31 +02003078 /*
Yuyang Du9d89c252015-07-15 08:04:37 +08003079 * Track task load average for carrying it to new CPU after migrated, and
3080 * track group sched_entity load average for task_h_load calc in migration
Paul Turnerf1b17282012-10-04 13:18:31 +02003081 */
Yuyang Du9d89c252015-07-15 08:04:37 +08003082 __update_load_avg(now, cpu, &se->avg,
Byungchul Parka05e8c52015-08-20 20:21:56 +09003083 se->on_rq * scale_load_down(se->load.weight),
3084 cfs_rq->curr == se, NULL);
Paul Turnerf1b17282012-10-04 13:18:31 +02003085
Steve Mucklea2c6c912016-03-24 15:26:07 -07003086 if (update_cfs_rq_load_avg(now, cfs_rq, true) && update_tg)
Yuyang Du9d89c252015-07-15 08:04:37 +08003087 update_tg_load_avg(cfs_rq, 0);
Juri Lellia4b0c3a2015-11-09 12:07:27 +00003088
3089 if (entity_is_task(se))
3090 trace_sched_load_avg_task(task_of(se), &se->avg);
Juri Lelli79478802015-11-09 12:07:48 +00003091 trace_sched_load_avg_cpu(cpu, cfs_rq);
Yuyang Du9d89c252015-07-15 08:04:37 +08003092}
Paul Turner2dac7542012-10-04 13:18:30 +02003093
Peter Zijlstra3d30544f2016-06-21 14:27:50 +02003094/**
3095 * attach_entity_load_avg - attach this entity to its cfs_rq load avg
3096 * @cfs_rq: cfs_rq to attach to
3097 * @se: sched_entity to attach
3098 *
3099 * Must call update_cfs_rq_load_avg() before this, since we rely on
3100 * cfs_rq->avg.last_update_time being current.
3101 */
Byungchul Parka05e8c52015-08-20 20:21:56 +09003102static void attach_entity_load_avg(struct cfs_rq *cfs_rq, struct sched_entity *se)
3103{
Peter Zijlstraa9280512015-09-11 16:10:59 +02003104 if (!sched_feat(ATTACH_AGE_LOAD))
3105 goto skip_aging;
3106
Byungchul Park6efdb102015-08-20 20:21:59 +09003107 /*
3108 * If we got migrated (either between CPUs or between cgroups) we'll
3109 * have aged the average right before clearing @last_update_time.
Peter Zijlstra7dc603c2016-06-16 13:29:28 +02003110 *
3111 * Or we're fresh through post_init_entity_util_avg().
Byungchul Park6efdb102015-08-20 20:21:59 +09003112 */
3113 if (se->avg.last_update_time) {
3114 __update_load_avg(cfs_rq->avg.last_update_time, cpu_of(rq_of(cfs_rq)),
3115 &se->avg, 0, 0, NULL);
3116
3117 /*
3118 * XXX: we could have just aged the entire load away if we've been
3119 * absent from the fair class for too long.
3120 */
3121 }
3122
Peter Zijlstraa9280512015-09-11 16:10:59 +02003123skip_aging:
Byungchul Parka05e8c52015-08-20 20:21:56 +09003124 se->avg.last_update_time = cfs_rq->avg.last_update_time;
3125 cfs_rq->avg.load_avg += se->avg.load_avg;
3126 cfs_rq->avg.load_sum += se->avg.load_sum;
3127 cfs_rq->avg.util_avg += se->avg.util_avg;
3128 cfs_rq->avg.util_sum += se->avg.util_sum;
Steve Mucklea2c6c912016-03-24 15:26:07 -07003129
3130 cfs_rq_util_change(cfs_rq);
Byungchul Parka05e8c52015-08-20 20:21:56 +09003131}
3132
Peter Zijlstra3d30544f2016-06-21 14:27:50 +02003133/**
3134 * detach_entity_load_avg - detach this entity from its cfs_rq load avg
3135 * @cfs_rq: cfs_rq to detach from
3136 * @se: sched_entity to detach
3137 *
3138 * Must call update_cfs_rq_load_avg() before this, since we rely on
3139 * cfs_rq->avg.last_update_time being current.
3140 */
Byungchul Parka05e8c52015-08-20 20:21:56 +09003141static void detach_entity_load_avg(struct cfs_rq *cfs_rq, struct sched_entity *se)
3142{
3143 __update_load_avg(cfs_rq->avg.last_update_time, cpu_of(rq_of(cfs_rq)),
3144 &se->avg, se->on_rq * scale_load_down(se->load.weight),
3145 cfs_rq->curr == se, NULL);
3146
Peter Zijlstra89741892016-06-16 10:50:40 +02003147 sub_positive(&cfs_rq->avg.load_avg, se->avg.load_avg);
3148 sub_positive(&cfs_rq->avg.load_sum, se->avg.load_sum);
3149 sub_positive(&cfs_rq->avg.util_avg, se->avg.util_avg);
3150 sub_positive(&cfs_rq->avg.util_sum, se->avg.util_sum);
Steve Mucklea2c6c912016-03-24 15:26:07 -07003151
3152 cfs_rq_util_change(cfs_rq);
Byungchul Parka05e8c52015-08-20 20:21:56 +09003153}
3154
Yuyang Du9d89c252015-07-15 08:04:37 +08003155/* Add the load generated by se into cfs_rq's load average */
3156static inline void
3157enqueue_entity_load_avg(struct cfs_rq *cfs_rq, struct sched_entity *se)
3158{
3159 struct sched_avg *sa = &se->avg;
3160 u64 now = cfs_rq_clock_task(cfs_rq);
Byungchul Parka05e8c52015-08-20 20:21:56 +09003161 int migrated, decayed;
Paul Turner9ee474f2012-10-04 13:18:30 +02003162
Byungchul Parka05e8c52015-08-20 20:21:56 +09003163 migrated = !sa->last_update_time;
3164 if (!migrated) {
Yuyang Du9d89c252015-07-15 08:04:37 +08003165 __update_load_avg(now, cpu_of(rq_of(cfs_rq)), sa,
Yuyang Du13962232015-07-15 08:04:41 +08003166 se->on_rq * scale_load_down(se->load.weight),
3167 cfs_rq->curr == se, NULL);
Yuyang Du9d89c252015-07-15 08:04:37 +08003168 }
3169
Steve Mucklea2c6c912016-03-24 15:26:07 -07003170 decayed = update_cfs_rq_load_avg(now, cfs_rq, !migrated);
Yuyang Du9d89c252015-07-15 08:04:37 +08003171
Yuyang Du13962232015-07-15 08:04:41 +08003172 cfs_rq->runnable_load_avg += sa->load_avg;
3173 cfs_rq->runnable_load_sum += sa->load_sum;
3174
Byungchul Parka05e8c52015-08-20 20:21:56 +09003175 if (migrated)
3176 attach_entity_load_avg(cfs_rq, se);
Yuyang Du9d89c252015-07-15 08:04:37 +08003177
3178 if (decayed || migrated)
3179 update_tg_load_avg(cfs_rq, 0);
Paul Turner9ee474f2012-10-04 13:18:30 +02003180}
3181
Yuyang Du13962232015-07-15 08:04:41 +08003182/* Remove the runnable load generated by se from cfs_rq's runnable load average */
3183static inline void
3184dequeue_entity_load_avg(struct cfs_rq *cfs_rq, struct sched_entity *se)
3185{
3186 update_load_avg(se, 1);
3187
3188 cfs_rq->runnable_load_avg =
3189 max_t(long, cfs_rq->runnable_load_avg - se->avg.load_avg, 0);
3190 cfs_rq->runnable_load_sum =
Byungchul Parka05e8c52015-08-20 20:21:56 +09003191 max_t(s64, cfs_rq->runnable_load_sum - se->avg.load_sum, 0);
Yuyang Du13962232015-07-15 08:04:41 +08003192}
3193
Yuyang Du0905f042015-12-17 07:34:27 +08003194#ifndef CONFIG_64BIT
3195static inline u64 cfs_rq_last_update_time(struct cfs_rq *cfs_rq)
3196{
3197 u64 last_update_time_copy;
3198 u64 last_update_time;
3199
3200 do {
3201 last_update_time_copy = cfs_rq->load_last_update_time_copy;
3202 smp_rmb();
3203 last_update_time = cfs_rq->avg.last_update_time;
3204 } while (last_update_time != last_update_time_copy);
3205
3206 return last_update_time;
3207}
3208#else
3209static inline u64 cfs_rq_last_update_time(struct cfs_rq *cfs_rq)
3210{
3211 return cfs_rq->avg.last_update_time;
3212}
3213#endif
3214
Paul Turner9ee474f2012-10-04 13:18:30 +02003215/*
Yuyang Du9d89c252015-07-15 08:04:37 +08003216 * Task first catches up with cfs_rq, and then subtract
3217 * itself from the cfs_rq (task must be off the queue now).
Paul Turner9ee474f2012-10-04 13:18:30 +02003218 */
Yuyang Du9d89c252015-07-15 08:04:37 +08003219void remove_entity_load_avg(struct sched_entity *se)
Paul Turner9ee474f2012-10-04 13:18:30 +02003220{
Yuyang Du9d89c252015-07-15 08:04:37 +08003221 struct cfs_rq *cfs_rq = cfs_rq_of(se);
3222 u64 last_update_time;
Paul Turner9ee474f2012-10-04 13:18:30 +02003223
Yuyang Du0905f042015-12-17 07:34:27 +08003224 /*
Peter Zijlstra7dc603c2016-06-16 13:29:28 +02003225 * tasks cannot exit without having gone through wake_up_new_task() ->
3226 * post_init_entity_util_avg() which will have added things to the
3227 * cfs_rq, so we can remove unconditionally.
3228 *
3229 * Similarly for groups, they will have passed through
3230 * post_init_entity_util_avg() before unregister_sched_fair_group()
3231 * calls this.
Yuyang Du0905f042015-12-17 07:34:27 +08003232 */
Paul Turner9ee474f2012-10-04 13:18:30 +02003233
Yuyang Du0905f042015-12-17 07:34:27 +08003234 last_update_time = cfs_rq_last_update_time(cfs_rq);
Paul Turner9ee474f2012-10-04 13:18:30 +02003235
Yuyang Du13962232015-07-15 08:04:41 +08003236 __update_load_avg(last_update_time, cpu_of(rq_of(cfs_rq)), &se->avg, 0, 0, NULL);
Yuyang Du9d89c252015-07-15 08:04:37 +08003237 atomic_long_add(se->avg.load_avg, &cfs_rq->removed_load_avg);
3238 atomic_long_add(se->avg.util_avg, &cfs_rq->removed_util_avg);
Paul Turner2dac7542012-10-04 13:18:30 +02003239}
Vincent Guittot642dbc32013-04-18 18:34:26 +02003240
Yuyang Du7ea241a2015-07-15 08:04:42 +08003241static inline unsigned long cfs_rq_runnable_load_avg(struct cfs_rq *cfs_rq)
3242{
3243 return cfs_rq->runnable_load_avg;
3244}
3245
3246static inline unsigned long cfs_rq_load_avg(struct cfs_rq *cfs_rq)
3247{
3248 return cfs_rq->avg.load_avg;
3249}
3250
Peter Zijlstra6e831252014-02-11 16:11:48 +01003251static int idle_balance(struct rq *this_rq);
3252
Peter Zijlstra38033c32014-01-23 20:32:21 +01003253#else /* CONFIG_SMP */
3254
Peter Zijlstra01011472016-06-17 11:20:46 +02003255static inline int
3256update_cfs_rq_load_avg(u64 now, struct cfs_rq *cfs_rq, bool update_freq)
3257{
3258 return 0;
3259}
3260
Rafael J. Wysocki536bd002016-05-06 14:58:43 +02003261static inline void update_load_avg(struct sched_entity *se, int not_used)
3262{
Rafael J. Wysocki12bde332016-08-10 03:11:17 +02003263 cpufreq_update_util(rq_of(cfs_rq_of(se)), 0);
Rafael J. Wysocki536bd002016-05-06 14:58:43 +02003264}
3265
Yuyang Du9d89c252015-07-15 08:04:37 +08003266static inline void
3267enqueue_entity_load_avg(struct cfs_rq *cfs_rq, struct sched_entity *se) {}
Yuyang Du13962232015-07-15 08:04:41 +08003268static inline void
3269dequeue_entity_load_avg(struct cfs_rq *cfs_rq, struct sched_entity *se) {}
Yuyang Du9d89c252015-07-15 08:04:37 +08003270static inline void remove_entity_load_avg(struct sched_entity *se) {}
Peter Zijlstra6e831252014-02-11 16:11:48 +01003271
Byungchul Parka05e8c52015-08-20 20:21:56 +09003272static inline void
3273attach_entity_load_avg(struct cfs_rq *cfs_rq, struct sched_entity *se) {}
3274static inline void
3275detach_entity_load_avg(struct cfs_rq *cfs_rq, struct sched_entity *se) {}
3276
Peter Zijlstra6e831252014-02-11 16:11:48 +01003277static inline int idle_balance(struct rq *rq)
3278{
3279 return 0;
3280}
3281
Peter Zijlstra38033c32014-01-23 20:32:21 +01003282#endif /* CONFIG_SMP */
Paul Turner9d85f212012-10-04 13:18:29 +02003283
Peter Zijlstraddc97292007-10-15 17:00:10 +02003284static void check_spread(struct cfs_rq *cfs_rq, struct sched_entity *se)
3285{
3286#ifdef CONFIG_SCHED_DEBUG
3287 s64 d = se->vruntime - cfs_rq->min_vruntime;
3288
3289 if (d < 0)
3290 d = -d;
3291
3292 if (d > 3*sysctl_sched_latency)
Josh Poimboeufae928822016-06-17 12:43:24 -05003293 schedstat_inc(cfs_rq->nr_spread_over);
Peter Zijlstraddc97292007-10-15 17:00:10 +02003294#endif
3295}
3296
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02003297static void
Peter Zijlstraaeb73b02007-10-15 17:00:05 +02003298place_entity(struct cfs_rq *cfs_rq, struct sched_entity *se, int initial)
3299{
Peter Zijlstra1af5f732008-10-24 11:06:13 +02003300 u64 vruntime = cfs_rq->min_vruntime;
Peter Zijlstra94dfb5e2007-10-15 17:00:05 +02003301
Peter Zijlstra2cb86002007-11-09 22:39:37 +01003302 /*
3303 * The 'current' period is already promised to the current tasks,
3304 * however the extra weight of the new task will slow them down a
3305 * little, place the new task so that it fits in the slot that
3306 * stays open at the end.
3307 */
Peter Zijlstra94dfb5e2007-10-15 17:00:05 +02003308 if (initial && sched_feat(START_DEBIT))
Peter Zijlstraf9c0b092008-10-17 19:27:04 +02003309 vruntime += sched_vslice(cfs_rq, se);
Peter Zijlstraaeb73b02007-10-15 17:00:05 +02003310
Mike Galbraitha2e7a7e2009-09-18 09:19:25 +02003311 /* sleeps up to a single latency don't count. */
Mike Galbraith5ca98802010-03-11 17:17:17 +01003312 if (!initial) {
Mike Galbraitha2e7a7e2009-09-18 09:19:25 +02003313 unsigned long thresh = sysctl_sched_latency;
Peter Zijlstraa7be37a2008-06-27 13:41:11 +02003314
Mike Galbraitha2e7a7e2009-09-18 09:19:25 +02003315 /*
Mike Galbraitha2e7a7e2009-09-18 09:19:25 +02003316 * Halve their sleep time's effect, to allow
3317 * for a gentler effect of sleepers:
3318 */
3319 if (sched_feat(GENTLE_FAIR_SLEEPERS))
3320 thresh >>= 1;
Ingo Molnar51e03042009-09-16 08:54:45 +02003321
Mike Galbraitha2e7a7e2009-09-18 09:19:25 +02003322 vruntime -= thresh;
Peter Zijlstraaeb73b02007-10-15 17:00:05 +02003323 }
3324
Mike Galbraithb5d9d732009-09-08 11:12:28 +02003325 /* ensure we never gain time by being placed backwards. */
Viresh Kumar16c8f1c2012-11-08 13:33:46 +05303326 se->vruntime = max_vruntime(se->vruntime, vruntime);
Peter Zijlstraaeb73b02007-10-15 17:00:05 +02003327}
3328
Paul Turnerd3d9dc32011-07-21 09:43:39 -07003329static void check_enqueue_throttle(struct cfs_rq *cfs_rq);
3330
Mel Gormancb251762016-02-05 09:08:36 +00003331static inline void check_schedstat_required(void)
3332{
3333#ifdef CONFIG_SCHEDSTATS
3334 if (schedstat_enabled())
3335 return;
3336
3337 /* Force schedstat enabled if a dependent tracepoint is active */
3338 if (trace_sched_stat_wait_enabled() ||
3339 trace_sched_stat_sleep_enabled() ||
3340 trace_sched_stat_iowait_enabled() ||
3341 trace_sched_stat_blocked_enabled() ||
3342 trace_sched_stat_runtime_enabled()) {
Josh Poimboeufeda8dca2016-06-13 02:32:09 -05003343 printk_deferred_once("Scheduler tracepoints stat_sleep, stat_iowait, "
Mel Gormancb251762016-02-05 09:08:36 +00003344 "stat_blocked and stat_runtime require the "
3345 "kernel parameter schedstats=enabled or "
3346 "kernel.sched_schedstats=1\n");
3347 }
3348#endif
3349}
3350
Peter Zijlstrab5179ac2016-05-11 16:10:34 +02003351
3352/*
3353 * MIGRATION
3354 *
3355 * dequeue
3356 * update_curr()
3357 * update_min_vruntime()
3358 * vruntime -= min_vruntime
3359 *
3360 * enqueue
3361 * update_curr()
3362 * update_min_vruntime()
3363 * vruntime += min_vruntime
3364 *
3365 * this way the vruntime transition between RQs is done when both
3366 * min_vruntime are up-to-date.
3367 *
3368 * WAKEUP (remote)
3369 *
Peter Zijlstra59efa0b2016-05-10 18:24:37 +02003370 * ->migrate_task_rq_fair() (p->state == TASK_WAKING)
Peter Zijlstrab5179ac2016-05-11 16:10:34 +02003371 * vruntime -= min_vruntime
3372 *
3373 * enqueue
3374 * update_curr()
3375 * update_min_vruntime()
3376 * vruntime += min_vruntime
3377 *
3378 * this way we don't have the most up-to-date min_vruntime on the originating
3379 * CPU and an up-to-date min_vruntime on the destination CPU.
3380 */
3381
Peter Zijlstraaeb73b02007-10-15 17:00:05 +02003382static void
Peter Zijlstra88ec22d2009-12-16 18:04:41 +01003383enqueue_entity(struct cfs_rq *cfs_rq, struct sched_entity *se, int flags)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02003384{
Peter Zijlstra2f950352016-05-11 19:27:56 +02003385 bool renorm = !(flags & ENQUEUE_WAKEUP) || (flags & ENQUEUE_MIGRATED);
3386 bool curr = cfs_rq->curr == se;
Peter Zijlstra3a47d512016-03-09 13:04:03 +01003387
Ingo Molnar53d3bc72016-05-11 08:25:53 +02003388 /*
Peter Zijlstra2f950352016-05-11 19:27:56 +02003389 * If we're the current task, we must renormalise before calling
3390 * update_curr().
Ingo Molnar53d3bc72016-05-11 08:25:53 +02003391 */
Peter Zijlstra2f950352016-05-11 19:27:56 +02003392 if (renorm && curr)
3393 se->vruntime += cfs_rq->min_vruntime;
3394
Ingo Molnarb7cc0892007-08-09 11:16:47 +02003395 update_curr(cfs_rq);
Peter Zijlstra2f950352016-05-11 19:27:56 +02003396
3397 /*
3398 * Otherwise, renormalise after, such that we're placed at the current
3399 * moment in time, instead of some random moment in the past. Being
3400 * placed in the past could significantly boost this task to the
3401 * fairness detriment of existing tasks.
3402 */
3403 if (renorm && !curr)
3404 se->vruntime += cfs_rq->min_vruntime;
3405
Yuyang Du9d89c252015-07-15 08:04:37 +08003406 enqueue_entity_load_avg(cfs_rq, se);
Linus Torvalds17bc14b2012-12-14 07:20:43 -08003407 account_entity_enqueue(cfs_rq, se);
3408 update_cfs_shares(cfs_rq);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02003409
Josh Poimboeuf1a3d0272016-06-17 12:43:23 -05003410 if (flags & ENQUEUE_WAKEUP)
Peter Zijlstraaeb73b02007-10-15 17:00:05 +02003411 place_entity(cfs_rq, se, 0);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02003412
Mel Gormancb251762016-02-05 09:08:36 +00003413 check_schedstat_required();
Josh Poimboeuf4fa8d292016-06-17 12:43:26 -05003414 update_stats_enqueue(cfs_rq, se, flags);
3415 check_spread(cfs_rq, se);
Peter Zijlstra2f950352016-05-11 19:27:56 +02003416 if (!curr)
Srivatsa Vaddagiri83b699e2007-10-15 17:00:08 +02003417 __enqueue_entity(cfs_rq, se);
Peter Zijlstra2069dd72010-11-15 15:47:00 -08003418 se->on_rq = 1;
Peter Zijlstra3d4b47b2010-11-15 15:47:01 -08003419
Paul Turnerd3d9dc32011-07-21 09:43:39 -07003420 if (cfs_rq->nr_running == 1) {
Peter Zijlstra3d4b47b2010-11-15 15:47:01 -08003421 list_add_leaf_cfs_rq(cfs_rq);
Paul Turnerd3d9dc32011-07-21 09:43:39 -07003422 check_enqueue_throttle(cfs_rq);
3423 }
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02003424}
3425
Rik van Riel2c13c9192011-02-01 09:48:37 -05003426static void __clear_buddies_last(struct sched_entity *se)
Peter Zijlstra2002c692008-11-11 11:52:33 +01003427{
Rik van Riel2c13c9192011-02-01 09:48:37 -05003428 for_each_sched_entity(se) {
3429 struct cfs_rq *cfs_rq = cfs_rq_of(se);
Peter Zijlstraf1044792012-02-11 06:05:00 +01003430 if (cfs_rq->last != se)
Rik van Riel2c13c9192011-02-01 09:48:37 -05003431 break;
Peter Zijlstraf1044792012-02-11 06:05:00 +01003432
3433 cfs_rq->last = NULL;
Rik van Riel2c13c9192011-02-01 09:48:37 -05003434 }
3435}
Peter Zijlstra2002c692008-11-11 11:52:33 +01003436
Rik van Riel2c13c9192011-02-01 09:48:37 -05003437static void __clear_buddies_next(struct sched_entity *se)
3438{
3439 for_each_sched_entity(se) {
3440 struct cfs_rq *cfs_rq = cfs_rq_of(se);
Peter Zijlstraf1044792012-02-11 06:05:00 +01003441 if (cfs_rq->next != se)
Rik van Riel2c13c9192011-02-01 09:48:37 -05003442 break;
Peter Zijlstraf1044792012-02-11 06:05:00 +01003443
3444 cfs_rq->next = NULL;
Rik van Riel2c13c9192011-02-01 09:48:37 -05003445 }
Peter Zijlstra2002c692008-11-11 11:52:33 +01003446}
3447
Rik van Rielac53db52011-02-01 09:51:03 -05003448static void __clear_buddies_skip(struct sched_entity *se)
3449{
3450 for_each_sched_entity(se) {
3451 struct cfs_rq *cfs_rq = cfs_rq_of(se);
Peter Zijlstraf1044792012-02-11 06:05:00 +01003452 if (cfs_rq->skip != se)
Rik van Rielac53db52011-02-01 09:51:03 -05003453 break;
Peter Zijlstraf1044792012-02-11 06:05:00 +01003454
3455 cfs_rq->skip = NULL;
Rik van Rielac53db52011-02-01 09:51:03 -05003456 }
3457}
3458
Peter Zijlstraa571bbe2009-01-28 14:51:40 +01003459static void clear_buddies(struct cfs_rq *cfs_rq, struct sched_entity *se)
3460{
Rik van Riel2c13c9192011-02-01 09:48:37 -05003461 if (cfs_rq->last == se)
3462 __clear_buddies_last(se);
3463
3464 if (cfs_rq->next == se)
3465 __clear_buddies_next(se);
Rik van Rielac53db52011-02-01 09:51:03 -05003466
3467 if (cfs_rq->skip == se)
3468 __clear_buddies_skip(se);
Peter Zijlstraa571bbe2009-01-28 14:51:40 +01003469}
3470
Peter Zijlstra6c16a6d2012-03-21 13:07:16 -07003471static __always_inline void return_cfs_rq_runtime(struct cfs_rq *cfs_rq);
Paul Turnerd8b49862011-07-21 09:43:41 -07003472
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02003473static void
Peter Zijlstra371fd7e2010-03-24 16:38:48 +01003474dequeue_entity(struct cfs_rq *cfs_rq, struct sched_entity *se, int flags)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02003475{
Dmitry Adamushkoa2a2d682007-10-15 17:00:13 +02003476 /*
3477 * Update run-time statistics of the 'current'.
3478 */
3479 update_curr(cfs_rq);
Yuyang Du13962232015-07-15 08:04:41 +08003480 dequeue_entity_load_avg(cfs_rq, se);
Dmitry Adamushkoa2a2d682007-10-15 17:00:13 +02003481
Josh Poimboeuf4fa8d292016-06-17 12:43:26 -05003482 update_stats_dequeue(cfs_rq, se, flags);
Peter Zijlstra67e9fb22007-10-15 17:00:10 +02003483
Peter Zijlstra2002c692008-11-11 11:52:33 +01003484 clear_buddies(cfs_rq, se);
Peter Zijlstra47932412008-11-04 21:25:09 +01003485
Srivatsa Vaddagiri83b699e2007-10-15 17:00:08 +02003486 if (se != cfs_rq->curr)
Dmitry Adamushko30cfdcf2007-10-15 17:00:07 +02003487 __dequeue_entity(cfs_rq, se);
Linus Torvalds17bc14b2012-12-14 07:20:43 -08003488 se->on_rq = 0;
Dmitry Adamushko30cfdcf2007-10-15 17:00:07 +02003489 account_entity_dequeue(cfs_rq, se);
Peter Zijlstra88ec22d2009-12-16 18:04:41 +01003490
3491 /*
Peter Zijlstrab60205c2016-09-20 21:58:12 +02003492 * Normalize after update_curr(); which will also have moved
3493 * min_vruntime if @se is the one holding it back. But before doing
3494 * update_min_vruntime() again, which will discount @se's position and
3495 * can move min_vruntime forward still more.
Peter Zijlstra88ec22d2009-12-16 18:04:41 +01003496 */
Peter Zijlstra371fd7e2010-03-24 16:38:48 +01003497 if (!(flags & DEQUEUE_SLEEP))
Peter Zijlstra88ec22d2009-12-16 18:04:41 +01003498 se->vruntime -= cfs_rq->min_vruntime;
Peter Zijlstra1e876232011-05-17 16:21:10 -07003499
Paul Turnerd8b49862011-07-21 09:43:41 -07003500 /* return excess runtime on last dequeue */
3501 return_cfs_rq_runtime(cfs_rq);
3502
Linus Torvalds17bc14b2012-12-14 07:20:43 -08003503 update_cfs_shares(cfs_rq);
Peter Zijlstrab60205c2016-09-20 21:58:12 +02003504
3505 /*
3506 * Now advance min_vruntime if @se was the entity holding it back,
3507 * except when: DEQUEUE_SAVE && !DEQUEUE_MOVE, in this case we'll be
3508 * put back on, and if we advance min_vruntime, we'll be placed back
3509 * further than we started -- ie. we'll be penalized.
3510 */
3511 if ((flags & (DEQUEUE_SAVE | DEQUEUE_MOVE)) == DEQUEUE_SAVE)
3512 update_min_vruntime(cfs_rq);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02003513}
3514
3515/*
3516 * Preempt the current task with a newly woken task if needed:
3517 */
Peter Zijlstra7c92e542007-09-05 14:32:49 +02003518static void
Ingo Molnar2e09bf52007-10-15 17:00:05 +02003519check_preempt_tick(struct cfs_rq *cfs_rq, struct sched_entity *curr)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02003520{
Peter Zijlstra11697832007-09-05 14:32:49 +02003521 unsigned long ideal_runtime, delta_exec;
Wang Xingchaof4cfb332011-09-16 13:35:52 -04003522 struct sched_entity *se;
3523 s64 delta;
Peter Zijlstra11697832007-09-05 14:32:49 +02003524
Peter Zijlstra6d0f0ebd2007-10-15 17:00:05 +02003525 ideal_runtime = sched_slice(cfs_rq, curr);
Peter Zijlstra11697832007-09-05 14:32:49 +02003526 delta_exec = curr->sum_exec_runtime - curr->prev_sum_exec_runtime;
Mike Galbraitha9f3e2b2009-01-28 14:51:39 +01003527 if (delta_exec > ideal_runtime) {
Kirill Tkhai88751252014-06-29 00:03:57 +04003528 resched_curr(rq_of(cfs_rq));
Mike Galbraitha9f3e2b2009-01-28 14:51:39 +01003529 /*
3530 * The current task ran long enough, ensure it doesn't get
3531 * re-elected due to buddy favours.
3532 */
3533 clear_buddies(cfs_rq, curr);
Mike Galbraithf685cea2009-10-23 23:09:22 +02003534 return;
3535 }
3536
3537 /*
3538 * Ensure that a task that missed wakeup preemption by a
3539 * narrow margin doesn't have to wait for a full slice.
3540 * This also mitigates buddy induced latencies under load.
3541 */
Mike Galbraithf685cea2009-10-23 23:09:22 +02003542 if (delta_exec < sysctl_sched_min_granularity)
3543 return;
3544
Wang Xingchaof4cfb332011-09-16 13:35:52 -04003545 se = __pick_first_entity(cfs_rq);
3546 delta = curr->vruntime - se->vruntime;
Mike Galbraithf685cea2009-10-23 23:09:22 +02003547
Wang Xingchaof4cfb332011-09-16 13:35:52 -04003548 if (delta < 0)
3549 return;
Mike Galbraithd7d82942011-01-05 05:41:17 +01003550
Wang Xingchaof4cfb332011-09-16 13:35:52 -04003551 if (delta > ideal_runtime)
Kirill Tkhai88751252014-06-29 00:03:57 +04003552 resched_curr(rq_of(cfs_rq));
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02003553}
3554
Srivatsa Vaddagiri83b699e2007-10-15 17:00:08 +02003555static void
Ingo Molnar8494f412007-08-09 11:16:48 +02003556set_next_entity(struct cfs_rq *cfs_rq, struct sched_entity *se)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02003557{
Srivatsa Vaddagiri83b699e2007-10-15 17:00:08 +02003558 /* 'current' is not kept within the tree. */
3559 if (se->on_rq) {
3560 /*
3561 * Any task has to be enqueued before it get to execute on
3562 * a CPU. So account for the time it spent waiting on the
3563 * runqueue.
3564 */
Josh Poimboeuf4fa8d292016-06-17 12:43:26 -05003565 update_stats_wait_end(cfs_rq, se);
Srivatsa Vaddagiri83b699e2007-10-15 17:00:08 +02003566 __dequeue_entity(cfs_rq, se);
Yuyang Du9d89c252015-07-15 08:04:37 +08003567 update_load_avg(se, 1);
Srivatsa Vaddagiri83b699e2007-10-15 17:00:08 +02003568 }
3569
Ingo Molnar79303e92007-08-09 11:16:47 +02003570 update_stats_curr_start(cfs_rq, se);
Ingo Molnar429d43b2007-10-15 17:00:03 +02003571 cfs_rq->curr = se;
Josh Poimboeuf4fa8d292016-06-17 12:43:26 -05003572
Ingo Molnareba1ed42007-10-15 17:00:02 +02003573 /*
3574 * Track our maximum slice length, if the CPU's load is at
3575 * least twice that of our own weight (i.e. dont track it
3576 * when there are only lesser-weight tasks around):
3577 */
Mel Gormancb251762016-02-05 09:08:36 +00003578 if (schedstat_enabled() && rq_of(cfs_rq)->load.weight >= 2*se->load.weight) {
Josh Poimboeuf4fa8d292016-06-17 12:43:26 -05003579 schedstat_set(se->statistics.slice_max,
3580 max((u64)schedstat_val(se->statistics.slice_max),
3581 se->sum_exec_runtime - se->prev_sum_exec_runtime));
Ingo Molnareba1ed42007-10-15 17:00:02 +02003582 }
Josh Poimboeuf4fa8d292016-06-17 12:43:26 -05003583
Peter Zijlstra4a55b452007-09-05 14:32:49 +02003584 se->prev_sum_exec_runtime = se->sum_exec_runtime;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02003585}
3586
Peter Zijlstra3f3a4902008-10-24 11:06:16 +02003587static int
3588wakeup_preempt_entity(struct sched_entity *curr, struct sched_entity *se);
3589
Rik van Rielac53db52011-02-01 09:51:03 -05003590/*
3591 * Pick the next process, keeping these things in mind, in this order:
3592 * 1) keep things fair between processes/task groups
3593 * 2) pick the "next" process, since someone really wants that to run
3594 * 3) pick the "last" process, for cache locality
3595 * 4) do not run the "skip" process, if something else is available
3596 */
Peter Zijlstra678d5712012-02-11 06:05:00 +01003597static struct sched_entity *
3598pick_next_entity(struct cfs_rq *cfs_rq, struct sched_entity *curr)
Peter Zijlstraaa2ac252008-03-14 21:12:12 +01003599{
Peter Zijlstra678d5712012-02-11 06:05:00 +01003600 struct sched_entity *left = __pick_first_entity(cfs_rq);
3601 struct sched_entity *se;
3602
3603 /*
3604 * If curr is set we have to see if its left of the leftmost entity
3605 * still in the tree, provided there was anything in the tree at all.
3606 */
3607 if (!left || (curr && entity_before(curr, left)))
3608 left = curr;
3609
3610 se = left; /* ideally we run the leftmost entity */
Peter Zijlstraf4b67552008-11-04 21:25:07 +01003611
Rik van Rielac53db52011-02-01 09:51:03 -05003612 /*
3613 * Avoid running the skip buddy, if running something else can
3614 * be done without getting too unfair.
3615 */
3616 if (cfs_rq->skip == se) {
Peter Zijlstra678d5712012-02-11 06:05:00 +01003617 struct sched_entity *second;
3618
3619 if (se == curr) {
3620 second = __pick_first_entity(cfs_rq);
3621 } else {
3622 second = __pick_next_entity(se);
3623 if (!second || (curr && entity_before(curr, second)))
3624 second = curr;
3625 }
3626
Rik van Rielac53db52011-02-01 09:51:03 -05003627 if (second && wakeup_preempt_entity(second, left) < 1)
3628 se = second;
3629 }
Peter Zijlstraaa2ac252008-03-14 21:12:12 +01003630
Mike Galbraithf685cea2009-10-23 23:09:22 +02003631 /*
3632 * Prefer last buddy, try to return the CPU to a preempted task.
3633 */
3634 if (cfs_rq->last && wakeup_preempt_entity(cfs_rq->last, left) < 1)
3635 se = cfs_rq->last;
3636
Rik van Rielac53db52011-02-01 09:51:03 -05003637 /*
3638 * Someone really wants this to run. If it's not unfair, run it.
3639 */
3640 if (cfs_rq->next && wakeup_preempt_entity(cfs_rq->next, left) < 1)
3641 se = cfs_rq->next;
3642
Mike Galbraithf685cea2009-10-23 23:09:22 +02003643 clear_buddies(cfs_rq, se);
Peter Zijlstra47932412008-11-04 21:25:09 +01003644
3645 return se;
Peter Zijlstraaa2ac252008-03-14 21:12:12 +01003646}
3647
Peter Zijlstra678d5712012-02-11 06:05:00 +01003648static bool check_cfs_rq_runtime(struct cfs_rq *cfs_rq);
Paul Turnerd3d9dc32011-07-21 09:43:39 -07003649
Ingo Molnarab6cde22007-08-09 11:16:48 +02003650static void put_prev_entity(struct cfs_rq *cfs_rq, struct sched_entity *prev)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02003651{
3652 /*
3653 * If still on the runqueue then deactivate_task()
3654 * was not called and update_curr() has to be done:
3655 */
3656 if (prev->on_rq)
Ingo Molnarb7cc0892007-08-09 11:16:47 +02003657 update_curr(cfs_rq);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02003658
Paul Turnerd3d9dc32011-07-21 09:43:39 -07003659 /* throttle cfs_rqs exceeding runtime */
3660 check_cfs_rq_runtime(cfs_rq);
3661
Josh Poimboeuf4fa8d292016-06-17 12:43:26 -05003662 check_spread(cfs_rq, prev);
Mel Gormancb251762016-02-05 09:08:36 +00003663
Dmitry Adamushko30cfdcf2007-10-15 17:00:07 +02003664 if (prev->on_rq) {
Josh Poimboeuf4fa8d292016-06-17 12:43:26 -05003665 update_stats_wait_start(cfs_rq, prev);
Dmitry Adamushko30cfdcf2007-10-15 17:00:07 +02003666 /* Put 'current' back into the tree. */
3667 __enqueue_entity(cfs_rq, prev);
Paul Turner9d85f212012-10-04 13:18:29 +02003668 /* in !on_rq case, update occurred at dequeue */
Yuyang Du9d89c252015-07-15 08:04:37 +08003669 update_load_avg(prev, 0);
Dmitry Adamushko30cfdcf2007-10-15 17:00:07 +02003670 }
Ingo Molnar429d43b2007-10-15 17:00:03 +02003671 cfs_rq->curr = NULL;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02003672}
3673
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01003674static void
3675entity_tick(struct cfs_rq *cfs_rq, struct sched_entity *curr, int queued)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02003676{
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02003677 /*
Dmitry Adamushko30cfdcf2007-10-15 17:00:07 +02003678 * Update run-time statistics of the 'current'.
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02003679 */
Dmitry Adamushko30cfdcf2007-10-15 17:00:07 +02003680 update_curr(cfs_rq);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02003681
Paul Turner43365bd2010-12-15 19:10:17 -08003682 /*
Paul Turner9d85f212012-10-04 13:18:29 +02003683 * Ensure that runnable average is periodically updated.
3684 */
Yuyang Du9d89c252015-07-15 08:04:37 +08003685 update_load_avg(curr, 1);
Peter Zijlstrabf0bd942013-07-26 23:48:42 +02003686 update_cfs_shares(cfs_rq);
Paul Turner9d85f212012-10-04 13:18:29 +02003687
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01003688#ifdef CONFIG_SCHED_HRTICK
3689 /*
3690 * queued ticks are scheduled to match the slice, so don't bother
3691 * validating it and just reschedule.
3692 */
Harvey Harrison983ed7a2008-04-24 18:17:55 -07003693 if (queued) {
Kirill Tkhai88751252014-06-29 00:03:57 +04003694 resched_curr(rq_of(cfs_rq));
Harvey Harrison983ed7a2008-04-24 18:17:55 -07003695 return;
3696 }
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01003697 /*
3698 * don't let the period tick interfere with the hrtick preemption
3699 */
3700 if (!sched_feat(DOUBLE_TICK) &&
3701 hrtimer_active(&rq_of(cfs_rq)->hrtick_timer))
3702 return;
3703#endif
3704
Yong Zhang2c2efae2011-07-29 16:20:33 +08003705 if (cfs_rq->nr_running > 1)
Ingo Molnar2e09bf52007-10-15 17:00:05 +02003706 check_preempt_tick(cfs_rq, curr);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02003707}
3708
Paul Turnerab84d312011-07-21 09:43:28 -07003709
3710/**************************************************
3711 * CFS bandwidth control machinery
3712 */
3713
3714#ifdef CONFIG_CFS_BANDWIDTH
Peter Zijlstra029632f2011-10-25 10:00:11 +02003715
3716#ifdef HAVE_JUMP_LABEL
Ingo Molnarc5905af2012-02-24 08:31:31 +01003717static struct static_key __cfs_bandwidth_used;
Peter Zijlstra029632f2011-10-25 10:00:11 +02003718
3719static inline bool cfs_bandwidth_used(void)
3720{
Ingo Molnarc5905af2012-02-24 08:31:31 +01003721 return static_key_false(&__cfs_bandwidth_used);
Peter Zijlstra029632f2011-10-25 10:00:11 +02003722}
3723
Ben Segall1ee14e62013-10-16 11:16:12 -07003724void cfs_bandwidth_usage_inc(void)
Peter Zijlstra029632f2011-10-25 10:00:11 +02003725{
Ben Segall1ee14e62013-10-16 11:16:12 -07003726 static_key_slow_inc(&__cfs_bandwidth_used);
3727}
3728
3729void cfs_bandwidth_usage_dec(void)
3730{
3731 static_key_slow_dec(&__cfs_bandwidth_used);
Peter Zijlstra029632f2011-10-25 10:00:11 +02003732}
3733#else /* HAVE_JUMP_LABEL */
3734static bool cfs_bandwidth_used(void)
3735{
3736 return true;
3737}
3738
Ben Segall1ee14e62013-10-16 11:16:12 -07003739void cfs_bandwidth_usage_inc(void) {}
3740void cfs_bandwidth_usage_dec(void) {}
Peter Zijlstra029632f2011-10-25 10:00:11 +02003741#endif /* HAVE_JUMP_LABEL */
3742
Paul Turnerab84d312011-07-21 09:43:28 -07003743/*
3744 * default period for cfs group bandwidth.
3745 * default: 0.1s, units: nanoseconds
3746 */
3747static inline u64 default_cfs_period(void)
3748{
3749 return 100000000ULL;
3750}
Paul Turnerec12cb72011-07-21 09:43:30 -07003751
3752static inline u64 sched_cfs_bandwidth_slice(void)
3753{
3754 return (u64)sysctl_sched_cfs_bandwidth_slice * NSEC_PER_USEC;
3755}
3756
Paul Turnera9cf55b2011-07-21 09:43:32 -07003757/*
3758 * Replenish runtime according to assigned quota and update expiration time.
3759 * We use sched_clock_cpu directly instead of rq->clock to avoid adding
3760 * additional synchronization around rq->lock.
3761 *
3762 * requires cfs_b->lock
3763 */
Peter Zijlstra029632f2011-10-25 10:00:11 +02003764void __refill_cfs_bandwidth_runtime(struct cfs_bandwidth *cfs_b)
Paul Turnera9cf55b2011-07-21 09:43:32 -07003765{
3766 u64 now;
3767
3768 if (cfs_b->quota == RUNTIME_INF)
3769 return;
3770
3771 now = sched_clock_cpu(smp_processor_id());
3772 cfs_b->runtime = cfs_b->quota;
3773 cfs_b->runtime_expires = now + ktime_to_ns(cfs_b->period);
3774}
3775
Peter Zijlstra029632f2011-10-25 10:00:11 +02003776static inline struct cfs_bandwidth *tg_cfs_bandwidth(struct task_group *tg)
3777{
3778 return &tg->cfs_bandwidth;
3779}
3780
Paul Turnerf1b17282012-10-04 13:18:31 +02003781/* rq->task_clock normalized against any time this cfs_rq has spent throttled */
3782static inline u64 cfs_rq_clock_task(struct cfs_rq *cfs_rq)
3783{
3784 if (unlikely(cfs_rq->throttle_count))
Xunlei Pang1a99ae32016-05-10 21:03:18 +08003785 return cfs_rq->throttled_clock_task - cfs_rq->throttled_clock_task_time;
Paul Turnerf1b17282012-10-04 13:18:31 +02003786
Frederic Weisbecker78becc22013-04-12 01:51:02 +02003787 return rq_clock_task(rq_of(cfs_rq)) - cfs_rq->throttled_clock_task_time;
Paul Turnerf1b17282012-10-04 13:18:31 +02003788}
3789
Paul Turner85dac902011-07-21 09:43:33 -07003790/* returns 0 on failure to allocate runtime */
3791static int assign_cfs_rq_runtime(struct cfs_rq *cfs_rq)
Paul Turnerec12cb72011-07-21 09:43:30 -07003792{
3793 struct task_group *tg = cfs_rq->tg;
3794 struct cfs_bandwidth *cfs_b = tg_cfs_bandwidth(tg);
Paul Turnera9cf55b2011-07-21 09:43:32 -07003795 u64 amount = 0, min_amount, expires;
Paul Turnerec12cb72011-07-21 09:43:30 -07003796
3797 /* note: this is a positive sum as runtime_remaining <= 0 */
3798 min_amount = sched_cfs_bandwidth_slice() - cfs_rq->runtime_remaining;
3799
3800 raw_spin_lock(&cfs_b->lock);
3801 if (cfs_b->quota == RUNTIME_INF)
3802 amount = min_amount;
Paul Turner58088ad2011-07-21 09:43:31 -07003803 else {
Peter Zijlstra77a4d1a2015-04-15 11:41:57 +02003804 start_cfs_bandwidth(cfs_b);
Paul Turner58088ad2011-07-21 09:43:31 -07003805
3806 if (cfs_b->runtime > 0) {
3807 amount = min(cfs_b->runtime, min_amount);
3808 cfs_b->runtime -= amount;
3809 cfs_b->idle = 0;
3810 }
Paul Turnerec12cb72011-07-21 09:43:30 -07003811 }
Paul Turnera9cf55b2011-07-21 09:43:32 -07003812 expires = cfs_b->runtime_expires;
Paul Turnerec12cb72011-07-21 09:43:30 -07003813 raw_spin_unlock(&cfs_b->lock);
3814
3815 cfs_rq->runtime_remaining += amount;
Paul Turnera9cf55b2011-07-21 09:43:32 -07003816 /*
3817 * we may have advanced our local expiration to account for allowed
3818 * spread between our sched_clock and the one on which runtime was
3819 * issued.
3820 */
3821 if ((s64)(expires - cfs_rq->runtime_expires) > 0)
3822 cfs_rq->runtime_expires = expires;
Paul Turner85dac902011-07-21 09:43:33 -07003823
3824 return cfs_rq->runtime_remaining > 0;
Paul Turnera9cf55b2011-07-21 09:43:32 -07003825}
3826
3827/*
3828 * Note: This depends on the synchronization provided by sched_clock and the
3829 * fact that rq->clock snapshots this value.
3830 */
3831static void expire_cfs_rq_runtime(struct cfs_rq *cfs_rq)
3832{
3833 struct cfs_bandwidth *cfs_b = tg_cfs_bandwidth(cfs_rq->tg);
Paul Turnera9cf55b2011-07-21 09:43:32 -07003834
3835 /* if the deadline is ahead of our clock, nothing to do */
Frederic Weisbecker78becc22013-04-12 01:51:02 +02003836 if (likely((s64)(rq_clock(rq_of(cfs_rq)) - cfs_rq->runtime_expires) < 0))
Paul Turnera9cf55b2011-07-21 09:43:32 -07003837 return;
3838
3839 if (cfs_rq->runtime_remaining < 0)
3840 return;
3841
3842 /*
3843 * If the local deadline has passed we have to consider the
3844 * possibility that our sched_clock is 'fast' and the global deadline
3845 * has not truly expired.
3846 *
3847 * Fortunately we can check determine whether this the case by checking
Ben Segall51f21762014-05-19 15:49:45 -07003848 * whether the global deadline has advanced. It is valid to compare
3849 * cfs_b->runtime_expires without any locks since we only care about
3850 * exact equality, so a partial write will still work.
Paul Turnera9cf55b2011-07-21 09:43:32 -07003851 */
3852
Ben Segall51f21762014-05-19 15:49:45 -07003853 if (cfs_rq->runtime_expires != cfs_b->runtime_expires) {
Paul Turnera9cf55b2011-07-21 09:43:32 -07003854 /* extend local deadline, drift is bounded above by 2 ticks */
3855 cfs_rq->runtime_expires += TICK_NSEC;
3856 } else {
3857 /* global deadline is ahead, expiration has passed */
3858 cfs_rq->runtime_remaining = 0;
3859 }
Paul Turnerec12cb72011-07-21 09:43:30 -07003860}
3861
Peter Zijlstra9dbdb152013-11-18 18:27:06 +01003862static void __account_cfs_rq_runtime(struct cfs_rq *cfs_rq, u64 delta_exec)
Paul Turnerec12cb72011-07-21 09:43:30 -07003863{
Paul Turnera9cf55b2011-07-21 09:43:32 -07003864 /* dock delta_exec before expiring quota (as it could span periods) */
Paul Turnerec12cb72011-07-21 09:43:30 -07003865 cfs_rq->runtime_remaining -= delta_exec;
Paul Turnera9cf55b2011-07-21 09:43:32 -07003866 expire_cfs_rq_runtime(cfs_rq);
3867
3868 if (likely(cfs_rq->runtime_remaining > 0))
Paul Turnerec12cb72011-07-21 09:43:30 -07003869 return;
3870
Paul Turner85dac902011-07-21 09:43:33 -07003871 /*
3872 * if we're unable to extend our runtime we resched so that the active
3873 * hierarchy can be throttled
3874 */
3875 if (!assign_cfs_rq_runtime(cfs_rq) && likely(cfs_rq->curr))
Kirill Tkhai88751252014-06-29 00:03:57 +04003876 resched_curr(rq_of(cfs_rq));
Paul Turnerec12cb72011-07-21 09:43:30 -07003877}
3878
Peter Zijlstra6c16a6d2012-03-21 13:07:16 -07003879static __always_inline
Peter Zijlstra9dbdb152013-11-18 18:27:06 +01003880void account_cfs_rq_runtime(struct cfs_rq *cfs_rq, u64 delta_exec)
Paul Turnerec12cb72011-07-21 09:43:30 -07003881{
Paul Turner56f570e2011-11-07 20:26:33 -08003882 if (!cfs_bandwidth_used() || !cfs_rq->runtime_enabled)
Paul Turnerec12cb72011-07-21 09:43:30 -07003883 return;
3884
3885 __account_cfs_rq_runtime(cfs_rq, delta_exec);
3886}
3887
Paul Turner85dac902011-07-21 09:43:33 -07003888static inline int cfs_rq_throttled(struct cfs_rq *cfs_rq)
3889{
Paul Turner56f570e2011-11-07 20:26:33 -08003890 return cfs_bandwidth_used() && cfs_rq->throttled;
Paul Turner85dac902011-07-21 09:43:33 -07003891}
3892
Paul Turner64660c82011-07-21 09:43:36 -07003893/* check whether cfs_rq, or any parent, is throttled */
3894static inline int throttled_hierarchy(struct cfs_rq *cfs_rq)
3895{
Paul Turner56f570e2011-11-07 20:26:33 -08003896 return cfs_bandwidth_used() && cfs_rq->throttle_count;
Paul Turner64660c82011-07-21 09:43:36 -07003897}
3898
3899/*
3900 * Ensure that neither of the group entities corresponding to src_cpu or
3901 * dest_cpu are members of a throttled hierarchy when performing group
3902 * load-balance operations.
3903 */
3904static inline int throttled_lb_pair(struct task_group *tg,
3905 int src_cpu, int dest_cpu)
3906{
3907 struct cfs_rq *src_cfs_rq, *dest_cfs_rq;
3908
3909 src_cfs_rq = tg->cfs_rq[src_cpu];
3910 dest_cfs_rq = tg->cfs_rq[dest_cpu];
3911
3912 return throttled_hierarchy(src_cfs_rq) ||
3913 throttled_hierarchy(dest_cfs_rq);
3914}
3915
3916/* updated child weight may affect parent so we have to do this bottom up */
3917static int tg_unthrottle_up(struct task_group *tg, void *data)
3918{
3919 struct rq *rq = data;
3920 struct cfs_rq *cfs_rq = tg->cfs_rq[cpu_of(rq)];
3921
3922 cfs_rq->throttle_count--;
Paul Turner64660c82011-07-21 09:43:36 -07003923 if (!cfs_rq->throttle_count) {
Paul Turnerf1b17282012-10-04 13:18:31 +02003924 /* adjust cfs_rq_clock_task() */
Frederic Weisbecker78becc22013-04-12 01:51:02 +02003925 cfs_rq->throttled_clock_task_time += rq_clock_task(rq) -
Paul Turnerf1b17282012-10-04 13:18:31 +02003926 cfs_rq->throttled_clock_task;
Paul Turner64660c82011-07-21 09:43:36 -07003927 }
Paul Turner64660c82011-07-21 09:43:36 -07003928
3929 return 0;
3930}
3931
3932static int tg_throttle_down(struct task_group *tg, void *data)
3933{
3934 struct rq *rq = data;
3935 struct cfs_rq *cfs_rq = tg->cfs_rq[cpu_of(rq)];
3936
Paul Turner82958362012-10-04 13:18:31 +02003937 /* group is entering throttled state, stop time */
3938 if (!cfs_rq->throttle_count)
Frederic Weisbecker78becc22013-04-12 01:51:02 +02003939 cfs_rq->throttled_clock_task = rq_clock_task(rq);
Paul Turner64660c82011-07-21 09:43:36 -07003940 cfs_rq->throttle_count++;
3941
3942 return 0;
3943}
3944
Paul Turnerd3d9dc32011-07-21 09:43:39 -07003945static void throttle_cfs_rq(struct cfs_rq *cfs_rq)
Paul Turner85dac902011-07-21 09:43:33 -07003946{
3947 struct rq *rq = rq_of(cfs_rq);
3948 struct cfs_bandwidth *cfs_b = tg_cfs_bandwidth(cfs_rq->tg);
3949 struct sched_entity *se;
3950 long task_delta, dequeue = 1;
Peter Zijlstra77a4d1a2015-04-15 11:41:57 +02003951 bool empty;
Paul Turner85dac902011-07-21 09:43:33 -07003952
3953 se = cfs_rq->tg->se[cpu_of(rq_of(cfs_rq))];
3954
Paul Turnerf1b17282012-10-04 13:18:31 +02003955 /* freeze hierarchy runnable averages while throttled */
Paul Turner64660c82011-07-21 09:43:36 -07003956 rcu_read_lock();
3957 walk_tg_tree_from(cfs_rq->tg, tg_throttle_down, tg_nop, (void *)rq);
3958 rcu_read_unlock();
Paul Turner85dac902011-07-21 09:43:33 -07003959
3960 task_delta = cfs_rq->h_nr_running;
3961 for_each_sched_entity(se) {
3962 struct cfs_rq *qcfs_rq = cfs_rq_of(se);
3963 /* throttled entity or throttle-on-deactivate */
3964 if (!se->on_rq)
3965 break;
3966
3967 if (dequeue)
3968 dequeue_entity(qcfs_rq, se, DEQUEUE_SLEEP);
3969 qcfs_rq->h_nr_running -= task_delta;
3970
3971 if (qcfs_rq->load.weight)
3972 dequeue = 0;
3973 }
3974
3975 if (!se)
Kirill Tkhai72465442014-05-09 03:00:14 +04003976 sub_nr_running(rq, task_delta);
Paul Turner85dac902011-07-21 09:43:33 -07003977
3978 cfs_rq->throttled = 1;
Frederic Weisbecker78becc22013-04-12 01:51:02 +02003979 cfs_rq->throttled_clock = rq_clock(rq);
Paul Turner85dac902011-07-21 09:43:33 -07003980 raw_spin_lock(&cfs_b->lock);
Cong Wangd49db342015-06-24 12:41:47 -07003981 empty = list_empty(&cfs_b->throttled_cfs_rq);
Peter Zijlstra77a4d1a2015-04-15 11:41:57 +02003982
Ben Segallc06f04c2014-06-20 15:21:20 -07003983 /*
3984 * Add to the _head_ of the list, so that an already-started
3985 * distribute_cfs_runtime will not see us
3986 */
3987 list_add_rcu(&cfs_rq->throttled_list, &cfs_b->throttled_cfs_rq);
Peter Zijlstra77a4d1a2015-04-15 11:41:57 +02003988
3989 /*
3990 * If we're the first throttled task, make sure the bandwidth
3991 * timer is running.
3992 */
3993 if (empty)
3994 start_cfs_bandwidth(cfs_b);
3995
Paul Turner85dac902011-07-21 09:43:33 -07003996 raw_spin_unlock(&cfs_b->lock);
3997}
3998
Peter Zijlstra029632f2011-10-25 10:00:11 +02003999void unthrottle_cfs_rq(struct cfs_rq *cfs_rq)
Paul Turner671fd9d2011-07-21 09:43:34 -07004000{
4001 struct rq *rq = rq_of(cfs_rq);
4002 struct cfs_bandwidth *cfs_b = tg_cfs_bandwidth(cfs_rq->tg);
4003 struct sched_entity *se;
4004 int enqueue = 1;
4005 long task_delta;
4006
Michael Wang22b958d2013-06-04 14:23:39 +08004007 se = cfs_rq->tg->se[cpu_of(rq)];
Paul Turner671fd9d2011-07-21 09:43:34 -07004008
4009 cfs_rq->throttled = 0;
Frederic Weisbecker1a55af22013-04-12 01:51:01 +02004010
4011 update_rq_clock(rq);
4012
Paul Turner671fd9d2011-07-21 09:43:34 -07004013 raw_spin_lock(&cfs_b->lock);
Frederic Weisbecker78becc22013-04-12 01:51:02 +02004014 cfs_b->throttled_time += rq_clock(rq) - cfs_rq->throttled_clock;
Paul Turner671fd9d2011-07-21 09:43:34 -07004015 list_del_rcu(&cfs_rq->throttled_list);
4016 raw_spin_unlock(&cfs_b->lock);
4017
Paul Turner64660c82011-07-21 09:43:36 -07004018 /* update hierarchical throttle state */
4019 walk_tg_tree_from(cfs_rq->tg, tg_nop, tg_unthrottle_up, (void *)rq);
4020
Paul Turner671fd9d2011-07-21 09:43:34 -07004021 if (!cfs_rq->load.weight)
4022 return;
4023
4024 task_delta = cfs_rq->h_nr_running;
4025 for_each_sched_entity(se) {
4026 if (se->on_rq)
4027 enqueue = 0;
4028
4029 cfs_rq = cfs_rq_of(se);
4030 if (enqueue)
4031 enqueue_entity(cfs_rq, se, ENQUEUE_WAKEUP);
4032 cfs_rq->h_nr_running += task_delta;
4033
4034 if (cfs_rq_throttled(cfs_rq))
4035 break;
4036 }
4037
4038 if (!se)
Kirill Tkhai72465442014-05-09 03:00:14 +04004039 add_nr_running(rq, task_delta);
Paul Turner671fd9d2011-07-21 09:43:34 -07004040
4041 /* determine whether we need to wake up potentially idle cpu */
4042 if (rq->curr == rq->idle && rq->cfs.nr_running)
Kirill Tkhai88751252014-06-29 00:03:57 +04004043 resched_curr(rq);
Paul Turner671fd9d2011-07-21 09:43:34 -07004044}
4045
4046static u64 distribute_cfs_runtime(struct cfs_bandwidth *cfs_b,
4047 u64 remaining, u64 expires)
4048{
4049 struct cfs_rq *cfs_rq;
Ben Segallc06f04c2014-06-20 15:21:20 -07004050 u64 runtime;
4051 u64 starting_runtime = remaining;
Paul Turner671fd9d2011-07-21 09:43:34 -07004052
4053 rcu_read_lock();
4054 list_for_each_entry_rcu(cfs_rq, &cfs_b->throttled_cfs_rq,
4055 throttled_list) {
4056 struct rq *rq = rq_of(cfs_rq);
4057
4058 raw_spin_lock(&rq->lock);
4059 if (!cfs_rq_throttled(cfs_rq))
4060 goto next;
4061
4062 runtime = -cfs_rq->runtime_remaining + 1;
4063 if (runtime > remaining)
4064 runtime = remaining;
4065 remaining -= runtime;
4066
4067 cfs_rq->runtime_remaining += runtime;
4068 cfs_rq->runtime_expires = expires;
4069
4070 /* we check whether we're throttled above */
4071 if (cfs_rq->runtime_remaining > 0)
4072 unthrottle_cfs_rq(cfs_rq);
4073
4074next:
4075 raw_spin_unlock(&rq->lock);
4076
4077 if (!remaining)
4078 break;
4079 }
4080 rcu_read_unlock();
4081
Ben Segallc06f04c2014-06-20 15:21:20 -07004082 return starting_runtime - remaining;
Paul Turner671fd9d2011-07-21 09:43:34 -07004083}
4084
Paul Turner58088ad2011-07-21 09:43:31 -07004085/*
4086 * Responsible for refilling a task_group's bandwidth and unthrottling its
4087 * cfs_rqs as appropriate. If there has been no activity within the last
4088 * period the timer is deactivated until scheduling resumes; cfs_b->idle is
4089 * used to track this state.
4090 */
4091static int do_sched_cfs_period_timer(struct cfs_bandwidth *cfs_b, int overrun)
4092{
Paul Turner671fd9d2011-07-21 09:43:34 -07004093 u64 runtime, runtime_expires;
Ben Segall51f21762014-05-19 15:49:45 -07004094 int throttled;
Paul Turner58088ad2011-07-21 09:43:31 -07004095
Paul Turner58088ad2011-07-21 09:43:31 -07004096 /* no need to continue the timer with no bandwidth constraint */
4097 if (cfs_b->quota == RUNTIME_INF)
Ben Segall51f21762014-05-19 15:49:45 -07004098 goto out_deactivate;
Paul Turner58088ad2011-07-21 09:43:31 -07004099
Paul Turner671fd9d2011-07-21 09:43:34 -07004100 throttled = !list_empty(&cfs_b->throttled_cfs_rq);
Nikhil Raoe8da1b12011-07-21 09:43:40 -07004101 cfs_b->nr_periods += overrun;
Paul Turner671fd9d2011-07-21 09:43:34 -07004102
Ben Segall51f21762014-05-19 15:49:45 -07004103 /*
4104 * idle depends on !throttled (for the case of a large deficit), and if
4105 * we're going inactive then everything else can be deferred
4106 */
4107 if (cfs_b->idle && !throttled)
4108 goto out_deactivate;
Paul Turnera9cf55b2011-07-21 09:43:32 -07004109
4110 __refill_cfs_bandwidth_runtime(cfs_b);
4111
Paul Turner671fd9d2011-07-21 09:43:34 -07004112 if (!throttled) {
4113 /* mark as potentially idle for the upcoming period */
4114 cfs_b->idle = 1;
Ben Segall51f21762014-05-19 15:49:45 -07004115 return 0;
Paul Turner671fd9d2011-07-21 09:43:34 -07004116 }
Paul Turner58088ad2011-07-21 09:43:31 -07004117
Nikhil Raoe8da1b12011-07-21 09:43:40 -07004118 /* account preceding periods in which throttling occurred */
4119 cfs_b->nr_throttled += overrun;
4120
Paul Turner671fd9d2011-07-21 09:43:34 -07004121 runtime_expires = cfs_b->runtime_expires;
Paul Turner671fd9d2011-07-21 09:43:34 -07004122
4123 /*
Ben Segallc06f04c2014-06-20 15:21:20 -07004124 * This check is repeated as we are holding onto the new bandwidth while
4125 * we unthrottle. This can potentially race with an unthrottled group
4126 * trying to acquire new bandwidth from the global pool. This can result
4127 * in us over-using our runtime if it is all used during this loop, but
4128 * only by limited amounts in that extreme case.
Paul Turner671fd9d2011-07-21 09:43:34 -07004129 */
Ben Segallc06f04c2014-06-20 15:21:20 -07004130 while (throttled && cfs_b->runtime > 0) {
4131 runtime = cfs_b->runtime;
Paul Turner671fd9d2011-07-21 09:43:34 -07004132 raw_spin_unlock(&cfs_b->lock);
4133 /* we can't nest cfs_b->lock while distributing bandwidth */
4134 runtime = distribute_cfs_runtime(cfs_b, runtime,
4135 runtime_expires);
4136 raw_spin_lock(&cfs_b->lock);
4137
4138 throttled = !list_empty(&cfs_b->throttled_cfs_rq);
Ben Segallc06f04c2014-06-20 15:21:20 -07004139
4140 cfs_b->runtime -= min(runtime, cfs_b->runtime);
Paul Turner671fd9d2011-07-21 09:43:34 -07004141 }
4142
Paul Turner671fd9d2011-07-21 09:43:34 -07004143 /*
4144 * While we are ensured activity in the period following an
4145 * unthrottle, this also covers the case in which the new bandwidth is
4146 * insufficient to cover the existing bandwidth deficit. (Forcing the
4147 * timer to remain active while there are any throttled entities.)
4148 */
4149 cfs_b->idle = 0;
Paul Turner58088ad2011-07-21 09:43:31 -07004150
Ben Segall51f21762014-05-19 15:49:45 -07004151 return 0;
4152
4153out_deactivate:
Ben Segall51f21762014-05-19 15:49:45 -07004154 return 1;
Paul Turner58088ad2011-07-21 09:43:31 -07004155}
Paul Turnerd3d9dc32011-07-21 09:43:39 -07004156
Paul Turnerd8b49862011-07-21 09:43:41 -07004157/* a cfs_rq won't donate quota below this amount */
4158static const u64 min_cfs_rq_runtime = 1 * NSEC_PER_MSEC;
4159/* minimum remaining period time to redistribute slack quota */
4160static const u64 min_bandwidth_expiration = 2 * NSEC_PER_MSEC;
4161/* how long we wait to gather additional slack before distributing */
4162static const u64 cfs_bandwidth_slack_period = 5 * NSEC_PER_MSEC;
4163
Ben Segalldb06e782013-10-16 11:16:17 -07004164/*
4165 * Are we near the end of the current quota period?
4166 *
4167 * Requires cfs_b->lock for hrtimer_expires_remaining to be safe against the
Thomas Gleixner4961b6e2015-04-14 21:09:05 +00004168 * hrtimer base being cleared by hrtimer_start. In the case of
Ben Segalldb06e782013-10-16 11:16:17 -07004169 * migrate_hrtimers, base is never cleared, so we are fine.
4170 */
Paul Turnerd8b49862011-07-21 09:43:41 -07004171static int runtime_refresh_within(struct cfs_bandwidth *cfs_b, u64 min_expire)
4172{
4173 struct hrtimer *refresh_timer = &cfs_b->period_timer;
4174 u64 remaining;
4175
4176 /* if the call-back is running a quota refresh is already occurring */
4177 if (hrtimer_callback_running(refresh_timer))
4178 return 1;
4179
4180 /* is a quota refresh about to occur? */
4181 remaining = ktime_to_ns(hrtimer_expires_remaining(refresh_timer));
4182 if (remaining < min_expire)
4183 return 1;
4184
4185 return 0;
4186}
4187
4188static void start_cfs_slack_bandwidth(struct cfs_bandwidth *cfs_b)
4189{
4190 u64 min_left = cfs_bandwidth_slack_period + min_bandwidth_expiration;
4191
4192 /* if there's a quota refresh soon don't bother with slack */
4193 if (runtime_refresh_within(cfs_b, min_left))
4194 return;
4195
Peter Zijlstra4cfafd32015-05-14 12:23:11 +02004196 hrtimer_start(&cfs_b->slack_timer,
4197 ns_to_ktime(cfs_bandwidth_slack_period),
4198 HRTIMER_MODE_REL);
Paul Turnerd8b49862011-07-21 09:43:41 -07004199}
4200
4201/* we know any runtime found here is valid as update_curr() precedes return */
4202static void __return_cfs_rq_runtime(struct cfs_rq *cfs_rq)
4203{
4204 struct cfs_bandwidth *cfs_b = tg_cfs_bandwidth(cfs_rq->tg);
4205 s64 slack_runtime = cfs_rq->runtime_remaining - min_cfs_rq_runtime;
4206
4207 if (slack_runtime <= 0)
4208 return;
4209
4210 raw_spin_lock(&cfs_b->lock);
4211 if (cfs_b->quota != RUNTIME_INF &&
4212 cfs_rq->runtime_expires == cfs_b->runtime_expires) {
4213 cfs_b->runtime += slack_runtime;
4214
4215 /* we are under rq->lock, defer unthrottling using a timer */
4216 if (cfs_b->runtime > sched_cfs_bandwidth_slice() &&
4217 !list_empty(&cfs_b->throttled_cfs_rq))
4218 start_cfs_slack_bandwidth(cfs_b);
4219 }
4220 raw_spin_unlock(&cfs_b->lock);
4221
4222 /* even if it's not valid for return we don't want to try again */
4223 cfs_rq->runtime_remaining -= slack_runtime;
4224}
4225
4226static __always_inline void return_cfs_rq_runtime(struct cfs_rq *cfs_rq)
4227{
Paul Turner56f570e2011-11-07 20:26:33 -08004228 if (!cfs_bandwidth_used())
4229 return;
4230
Paul Turnerfccfdc62011-11-07 20:26:34 -08004231 if (!cfs_rq->runtime_enabled || cfs_rq->nr_running)
Paul Turnerd8b49862011-07-21 09:43:41 -07004232 return;
4233
4234 __return_cfs_rq_runtime(cfs_rq);
4235}
4236
4237/*
4238 * This is done with a timer (instead of inline with bandwidth return) since
4239 * it's necessary to juggle rq->locks to unthrottle their respective cfs_rqs.
4240 */
4241static void do_sched_cfs_slack_timer(struct cfs_bandwidth *cfs_b)
4242{
4243 u64 runtime = 0, slice = sched_cfs_bandwidth_slice();
4244 u64 expires;
4245
4246 /* confirm we're still not at a refresh boundary */
Paul Turnerd8b49862011-07-21 09:43:41 -07004247 raw_spin_lock(&cfs_b->lock);
Ben Segalldb06e782013-10-16 11:16:17 -07004248 if (runtime_refresh_within(cfs_b, min_bandwidth_expiration)) {
4249 raw_spin_unlock(&cfs_b->lock);
4250 return;
4251 }
4252
Ben Segallc06f04c2014-06-20 15:21:20 -07004253 if (cfs_b->quota != RUNTIME_INF && cfs_b->runtime > slice)
Paul Turnerd8b49862011-07-21 09:43:41 -07004254 runtime = cfs_b->runtime;
Ben Segallc06f04c2014-06-20 15:21:20 -07004255
Paul Turnerd8b49862011-07-21 09:43:41 -07004256 expires = cfs_b->runtime_expires;
4257 raw_spin_unlock(&cfs_b->lock);
4258
4259 if (!runtime)
4260 return;
4261
4262 runtime = distribute_cfs_runtime(cfs_b, runtime, expires);
4263
4264 raw_spin_lock(&cfs_b->lock);
4265 if (expires == cfs_b->runtime_expires)
Ben Segallc06f04c2014-06-20 15:21:20 -07004266 cfs_b->runtime -= min(runtime, cfs_b->runtime);
Paul Turnerd8b49862011-07-21 09:43:41 -07004267 raw_spin_unlock(&cfs_b->lock);
4268}
4269
Paul Turnerd3d9dc32011-07-21 09:43:39 -07004270/*
4271 * When a group wakes up we want to make sure that its quota is not already
4272 * expired/exceeded, otherwise it may be allowed to steal additional ticks of
4273 * runtime as update_curr() throttling can not not trigger until it's on-rq.
4274 */
4275static void check_enqueue_throttle(struct cfs_rq *cfs_rq)
4276{
Paul Turner56f570e2011-11-07 20:26:33 -08004277 if (!cfs_bandwidth_used())
4278 return;
4279
Paul Turnerd3d9dc32011-07-21 09:43:39 -07004280 /* an active group must be handled by the update_curr()->put() path */
4281 if (!cfs_rq->runtime_enabled || cfs_rq->curr)
4282 return;
4283
4284 /* ensure the group is not already throttled */
4285 if (cfs_rq_throttled(cfs_rq))
4286 return;
4287
4288 /* update runtime allocation */
4289 account_cfs_rq_runtime(cfs_rq, 0);
4290 if (cfs_rq->runtime_remaining <= 0)
4291 throttle_cfs_rq(cfs_rq);
4292}
4293
Peter Zijlstra55e16d32016-06-22 15:14:26 +02004294static void sync_throttle(struct task_group *tg, int cpu)
4295{
4296 struct cfs_rq *pcfs_rq, *cfs_rq;
4297
4298 if (!cfs_bandwidth_used())
4299 return;
4300
4301 if (!tg->parent)
4302 return;
4303
4304 cfs_rq = tg->cfs_rq[cpu];
4305 pcfs_rq = tg->parent->cfs_rq[cpu];
4306
4307 cfs_rq->throttle_count = pcfs_rq->throttle_count;
Xunlei Pangb8922122016-07-09 15:54:22 +08004308 cfs_rq->throttled_clock_task = rq_clock_task(cpu_rq(cpu));
Peter Zijlstra55e16d32016-06-22 15:14:26 +02004309}
4310
Paul Turnerd3d9dc32011-07-21 09:43:39 -07004311/* conditionally throttle active cfs_rq's from put_prev_entity() */
Peter Zijlstra678d5712012-02-11 06:05:00 +01004312static bool check_cfs_rq_runtime(struct cfs_rq *cfs_rq)
Paul Turnerd3d9dc32011-07-21 09:43:39 -07004313{
Paul Turner56f570e2011-11-07 20:26:33 -08004314 if (!cfs_bandwidth_used())
Peter Zijlstra678d5712012-02-11 06:05:00 +01004315 return false;
Paul Turner56f570e2011-11-07 20:26:33 -08004316
Paul Turnerd3d9dc32011-07-21 09:43:39 -07004317 if (likely(!cfs_rq->runtime_enabled || cfs_rq->runtime_remaining > 0))
Peter Zijlstra678d5712012-02-11 06:05:00 +01004318 return false;
Paul Turnerd3d9dc32011-07-21 09:43:39 -07004319
4320 /*
4321 * it's possible for a throttled entity to be forced into a running
4322 * state (e.g. set_curr_task), in this case we're finished.
4323 */
4324 if (cfs_rq_throttled(cfs_rq))
Peter Zijlstra678d5712012-02-11 06:05:00 +01004325 return true;
Paul Turnerd3d9dc32011-07-21 09:43:39 -07004326
4327 throttle_cfs_rq(cfs_rq);
Peter Zijlstra678d5712012-02-11 06:05:00 +01004328 return true;
Paul Turnerd3d9dc32011-07-21 09:43:39 -07004329}
Peter Zijlstra029632f2011-10-25 10:00:11 +02004330
Peter Zijlstra029632f2011-10-25 10:00:11 +02004331static enum hrtimer_restart sched_cfs_slack_timer(struct hrtimer *timer)
4332{
4333 struct cfs_bandwidth *cfs_b =
4334 container_of(timer, struct cfs_bandwidth, slack_timer);
Peter Zijlstra77a4d1a2015-04-15 11:41:57 +02004335
Peter Zijlstra029632f2011-10-25 10:00:11 +02004336 do_sched_cfs_slack_timer(cfs_b);
4337
4338 return HRTIMER_NORESTART;
4339}
4340
4341static enum hrtimer_restart sched_cfs_period_timer(struct hrtimer *timer)
4342{
4343 struct cfs_bandwidth *cfs_b =
4344 container_of(timer, struct cfs_bandwidth, period_timer);
Peter Zijlstra029632f2011-10-25 10:00:11 +02004345 int overrun;
4346 int idle = 0;
4347
Ben Segall51f21762014-05-19 15:49:45 -07004348 raw_spin_lock(&cfs_b->lock);
Peter Zijlstra029632f2011-10-25 10:00:11 +02004349 for (;;) {
Peter Zijlstra77a4d1a2015-04-15 11:41:57 +02004350 overrun = hrtimer_forward_now(timer, cfs_b->period);
Peter Zijlstra029632f2011-10-25 10:00:11 +02004351 if (!overrun)
4352 break;
4353
4354 idle = do_sched_cfs_period_timer(cfs_b, overrun);
4355 }
Peter Zijlstra4cfafd32015-05-14 12:23:11 +02004356 if (idle)
4357 cfs_b->period_active = 0;
Ben Segall51f21762014-05-19 15:49:45 -07004358 raw_spin_unlock(&cfs_b->lock);
Peter Zijlstra029632f2011-10-25 10:00:11 +02004359
4360 return idle ? HRTIMER_NORESTART : HRTIMER_RESTART;
4361}
4362
4363void init_cfs_bandwidth(struct cfs_bandwidth *cfs_b)
4364{
4365 raw_spin_lock_init(&cfs_b->lock);
4366 cfs_b->runtime = 0;
4367 cfs_b->quota = RUNTIME_INF;
4368 cfs_b->period = ns_to_ktime(default_cfs_period());
4369
4370 INIT_LIST_HEAD(&cfs_b->throttled_cfs_rq);
Peter Zijlstra4cfafd32015-05-14 12:23:11 +02004371 hrtimer_init(&cfs_b->period_timer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS_PINNED);
Peter Zijlstra029632f2011-10-25 10:00:11 +02004372 cfs_b->period_timer.function = sched_cfs_period_timer;
4373 hrtimer_init(&cfs_b->slack_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
4374 cfs_b->slack_timer.function = sched_cfs_slack_timer;
4375}
4376
4377static void init_cfs_rq_runtime(struct cfs_rq *cfs_rq)
4378{
4379 cfs_rq->runtime_enabled = 0;
4380 INIT_LIST_HEAD(&cfs_rq->throttled_list);
4381}
4382
Peter Zijlstra77a4d1a2015-04-15 11:41:57 +02004383void start_cfs_bandwidth(struct cfs_bandwidth *cfs_b)
Peter Zijlstra029632f2011-10-25 10:00:11 +02004384{
Peter Zijlstra4cfafd32015-05-14 12:23:11 +02004385 lockdep_assert_held(&cfs_b->lock);
Peter Zijlstra029632f2011-10-25 10:00:11 +02004386
Peter Zijlstra4cfafd32015-05-14 12:23:11 +02004387 if (!cfs_b->period_active) {
4388 cfs_b->period_active = 1;
4389 hrtimer_forward_now(&cfs_b->period_timer, cfs_b->period);
4390 hrtimer_start_expires(&cfs_b->period_timer, HRTIMER_MODE_ABS_PINNED);
4391 }
Peter Zijlstra029632f2011-10-25 10:00:11 +02004392}
4393
4394static void destroy_cfs_bandwidth(struct cfs_bandwidth *cfs_b)
4395{
Tetsuo Handa7f1a1692014-12-25 15:51:21 +09004396 /* init_cfs_bandwidth() was not called */
4397 if (!cfs_b->throttled_cfs_rq.next)
4398 return;
4399
Peter Zijlstra029632f2011-10-25 10:00:11 +02004400 hrtimer_cancel(&cfs_b->period_timer);
4401 hrtimer_cancel(&cfs_b->slack_timer);
4402}
4403
Kirill Tkhai0e59bda2014-06-25 12:19:42 +04004404static void __maybe_unused update_runtime_enabled(struct rq *rq)
4405{
4406 struct cfs_rq *cfs_rq;
4407
4408 for_each_leaf_cfs_rq(rq, cfs_rq) {
4409 struct cfs_bandwidth *cfs_b = &cfs_rq->tg->cfs_bandwidth;
4410
4411 raw_spin_lock(&cfs_b->lock);
4412 cfs_rq->runtime_enabled = cfs_b->quota != RUNTIME_INF;
4413 raw_spin_unlock(&cfs_b->lock);
4414 }
4415}
4416
Arnd Bergmann38dc3342013-01-25 14:14:22 +00004417static void __maybe_unused unthrottle_offline_cfs_rqs(struct rq *rq)
Peter Zijlstra029632f2011-10-25 10:00:11 +02004418{
4419 struct cfs_rq *cfs_rq;
4420
4421 for_each_leaf_cfs_rq(rq, cfs_rq) {
Peter Zijlstra029632f2011-10-25 10:00:11 +02004422 if (!cfs_rq->runtime_enabled)
4423 continue;
4424
4425 /*
4426 * clock_task is not advancing so we just need to make sure
4427 * there's some valid quota amount
4428 */
Ben Segall51f21762014-05-19 15:49:45 -07004429 cfs_rq->runtime_remaining = 1;
Kirill Tkhai0e59bda2014-06-25 12:19:42 +04004430 /*
4431 * Offline rq is schedulable till cpu is completely disabled
4432 * in take_cpu_down(), so we prevent new cfs throttling here.
4433 */
4434 cfs_rq->runtime_enabled = 0;
4435
Peter Zijlstra029632f2011-10-25 10:00:11 +02004436 if (cfs_rq_throttled(cfs_rq))
4437 unthrottle_cfs_rq(cfs_rq);
4438 }
4439}
4440
4441#else /* CONFIG_CFS_BANDWIDTH */
Paul Turnerf1b17282012-10-04 13:18:31 +02004442static inline u64 cfs_rq_clock_task(struct cfs_rq *cfs_rq)
4443{
Frederic Weisbecker78becc22013-04-12 01:51:02 +02004444 return rq_clock_task(rq_of(cfs_rq));
Paul Turnerf1b17282012-10-04 13:18:31 +02004445}
4446
Peter Zijlstra9dbdb152013-11-18 18:27:06 +01004447static void account_cfs_rq_runtime(struct cfs_rq *cfs_rq, u64 delta_exec) {}
Peter Zijlstra678d5712012-02-11 06:05:00 +01004448static bool check_cfs_rq_runtime(struct cfs_rq *cfs_rq) { return false; }
Paul Turnerd3d9dc32011-07-21 09:43:39 -07004449static void check_enqueue_throttle(struct cfs_rq *cfs_rq) {}
Peter Zijlstra55e16d32016-06-22 15:14:26 +02004450static inline void sync_throttle(struct task_group *tg, int cpu) {}
Peter Zijlstra6c16a6d2012-03-21 13:07:16 -07004451static __always_inline void return_cfs_rq_runtime(struct cfs_rq *cfs_rq) {}
Paul Turner85dac902011-07-21 09:43:33 -07004452
4453static inline int cfs_rq_throttled(struct cfs_rq *cfs_rq)
4454{
4455 return 0;
4456}
Paul Turner64660c82011-07-21 09:43:36 -07004457
4458static inline int throttled_hierarchy(struct cfs_rq *cfs_rq)
4459{
4460 return 0;
4461}
4462
4463static inline int throttled_lb_pair(struct task_group *tg,
4464 int src_cpu, int dest_cpu)
4465{
4466 return 0;
4467}
Peter Zijlstra029632f2011-10-25 10:00:11 +02004468
4469void init_cfs_bandwidth(struct cfs_bandwidth *cfs_b) {}
4470
4471#ifdef CONFIG_FAIR_GROUP_SCHED
4472static void init_cfs_rq_runtime(struct cfs_rq *cfs_rq) {}
Paul Turnerab84d312011-07-21 09:43:28 -07004473#endif
4474
Peter Zijlstra029632f2011-10-25 10:00:11 +02004475static inline struct cfs_bandwidth *tg_cfs_bandwidth(struct task_group *tg)
4476{
4477 return NULL;
4478}
4479static inline void destroy_cfs_bandwidth(struct cfs_bandwidth *cfs_b) {}
Kirill Tkhai0e59bda2014-06-25 12:19:42 +04004480static inline void update_runtime_enabled(struct rq *rq) {}
Peter Boonstoppela4c96ae2012-08-09 15:34:47 -07004481static inline void unthrottle_offline_cfs_rqs(struct rq *rq) {}
Peter Zijlstra029632f2011-10-25 10:00:11 +02004482
4483#endif /* CONFIG_CFS_BANDWIDTH */
4484
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02004485/**************************************************
4486 * CFS operations on tasks:
4487 */
4488
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01004489#ifdef CONFIG_SCHED_HRTICK
4490static void hrtick_start_fair(struct rq *rq, struct task_struct *p)
4491{
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01004492 struct sched_entity *se = &p->se;
4493 struct cfs_rq *cfs_rq = cfs_rq_of(se);
4494
Peter Zijlstra9148a3a2016-09-20 22:34:51 +02004495 SCHED_WARN_ON(task_rq(p) != rq);
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01004496
Srivatsa Vaddagiri8bf46a32016-09-16 18:28:51 -07004497 if (rq->cfs.h_nr_running > 1) {
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01004498 u64 slice = sched_slice(cfs_rq, se);
4499 u64 ran = se->sum_exec_runtime - se->prev_sum_exec_runtime;
4500 s64 delta = slice - ran;
4501
4502 if (delta < 0) {
4503 if (rq->curr == p)
Kirill Tkhai88751252014-06-29 00:03:57 +04004504 resched_curr(rq);
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01004505 return;
4506 }
Peter Zijlstra31656512008-07-18 18:01:23 +02004507 hrtick_start(rq, delta);
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01004508 }
4509}
Peter Zijlstraa4c2f002008-10-17 19:27:03 +02004510
4511/*
4512 * called from enqueue/dequeue and updates the hrtick when the
4513 * current task is from our class and nr_running is low enough
4514 * to matter.
4515 */
4516static void hrtick_update(struct rq *rq)
4517{
4518 struct task_struct *curr = rq->curr;
4519
Mike Galbraithb39e66e2011-11-22 15:20:07 +01004520 if (!hrtick_enabled(rq) || curr->sched_class != &fair_sched_class)
Peter Zijlstraa4c2f002008-10-17 19:27:03 +02004521 return;
4522
4523 if (cfs_rq_of(&curr->se)->nr_running < sched_nr_latency)
4524 hrtick_start_fair(rq, curr);
4525}
Dhaval Giani55e12e52008-06-24 23:39:43 +05304526#else /* !CONFIG_SCHED_HRTICK */
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01004527static inline void
4528hrtick_start_fair(struct rq *rq, struct task_struct *p)
4529{
4530}
Peter Zijlstraa4c2f002008-10-17 19:27:03 +02004531
4532static inline void hrtick_update(struct rq *rq)
4533{
4534}
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01004535#endif
4536
Patrick Bellasi2178e842016-07-22 11:35:59 +01004537#ifdef CONFIG_SMP
4538static bool cpu_overutilized(int cpu);
Juri Lelli4585a262015-08-19 19:47:12 +01004539static unsigned long capacity_orig_of(int cpu);
Juri Lelli43aac892015-06-26 12:14:23 +01004540static unsigned long cpu_util(int cpu);
Patrick Bellasicaa24e42015-06-26 09:55:06 +01004541static inline unsigned long boosted_cpu_util(int cpu);
Patrick Bellasi2178e842016-07-22 11:35:59 +01004542#else
4543#define boosted_cpu_util(cpu) cpu_util(cpu)
4544#endif
Juri Lelli4585a262015-08-19 19:47:12 +01004545
Patrick Bellasi2178e842016-07-22 11:35:59 +01004546#ifdef CONFIG_SMP
Juri Lelli4585a262015-08-19 19:47:12 +01004547static void update_capacity_of(int cpu)
4548{
4549 unsigned long req_cap;
4550
4551 if (!sched_freq())
4552 return;
4553
4554 /* Convert scale-invariant capacity to cpu. */
Patrick Bellasicaa24e42015-06-26 09:55:06 +01004555 req_cap = boosted_cpu_util(cpu);
4556 req_cap = req_cap * SCHED_CAPACITY_SCALE / capacity_orig_of(cpu);
Juri Lelli4585a262015-08-19 19:47:12 +01004557 set_cfs_cpu_capacity(cpu, true, req_cap);
4558}
Patrick Bellasi2178e842016-07-22 11:35:59 +01004559#endif
Morten Rasmussena562dfc2015-05-09 16:49:57 +01004560
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02004561/*
4562 * The enqueue_task method is called before nr_running is
4563 * increased. Here we update the fair scheduling stats and
4564 * then put the task into the rbtree:
4565 */
Thomas Gleixnerea87bb72010-01-20 20:58:57 +00004566static void
Peter Zijlstra371fd7e2010-03-24 16:38:48 +01004567enqueue_task_fair(struct rq *rq, struct task_struct *p, int flags)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02004568{
4569 struct cfs_rq *cfs_rq;
Peter Zijlstra62fb1852008-02-25 17:34:02 +01004570 struct sched_entity *se = &p->se;
Patrick Bellasi2178e842016-07-22 11:35:59 +01004571#ifdef CONFIG_SMP
Juri Lelli43aac892015-06-26 12:14:23 +01004572 int task_new = flags & ENQUEUE_WAKEUP_NEW;
4573 int task_wakeup = flags & ENQUEUE_WAKEUP;
Patrick Bellasi2178e842016-07-22 11:35:59 +01004574#endif
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02004575
Rafael J. Wysocki8c34ab12016-09-09 23:59:33 +02004576 /*
4577 * If in_iowait is set, the code below may not trigger any cpufreq
4578 * utilization updates, so do it here explicitly with the IOWAIT flag
4579 * passed.
4580 */
4581 if (p->in_iowait)
4582 cpufreq_update_this_cpu(rq, SCHED_CPUFREQ_IOWAIT);
4583
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02004584 for_each_sched_entity(se) {
Peter Zijlstra62fb1852008-02-25 17:34:02 +01004585 if (se->on_rq)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02004586 break;
4587 cfs_rq = cfs_rq_of(se);
Peter Zijlstra88ec22d2009-12-16 18:04:41 +01004588 enqueue_entity(cfs_rq, se, flags);
Paul Turner85dac902011-07-21 09:43:33 -07004589
4590 /*
4591 * end evaluation on encountering a throttled cfs_rq
4592 *
4593 * note: in the case of encountering a throttled cfs_rq we will
4594 * post the final h_nr_running increment below.
Peter Zijlstrae210bff2016-06-16 18:51:48 +02004595 */
Paul Turner85dac902011-07-21 09:43:33 -07004596 if (cfs_rq_throttled(cfs_rq))
4597 break;
Paul Turner953bfcd2011-07-21 09:43:27 -07004598 cfs_rq->h_nr_running++;
Paul Turner85dac902011-07-21 09:43:33 -07004599
Peter Zijlstra88ec22d2009-12-16 18:04:41 +01004600 flags = ENQUEUE_WAKEUP;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02004601 }
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01004602
Peter Zijlstra2069dd72010-11-15 15:47:00 -08004603 for_each_sched_entity(se) {
Lin Ming0f317142011-07-22 09:14:31 +08004604 cfs_rq = cfs_rq_of(se);
Paul Turner953bfcd2011-07-21 09:43:27 -07004605 cfs_rq->h_nr_running++;
Peter Zijlstra2069dd72010-11-15 15:47:00 -08004606
Paul Turner85dac902011-07-21 09:43:33 -07004607 if (cfs_rq_throttled(cfs_rq))
4608 break;
4609
Yuyang Du9d89c252015-07-15 08:04:37 +08004610 update_load_avg(se, 1);
Linus Torvalds17bc14b2012-12-14 07:20:43 -08004611 update_cfs_shares(cfs_rq);
Peter Zijlstra2069dd72010-11-15 15:47:00 -08004612 }
4613
Patrick Bellasi2178e842016-07-22 11:35:59 +01004614 if (!se)
Kirill Tkhai72465442014-05-09 03:00:14 +04004615 add_nr_running(rq, 1);
Patrick Bellasi2178e842016-07-22 11:35:59 +01004616
4617#ifdef CONFIG_SMP
4618
4619 if (!se) {
Morten Rasmussena562dfc2015-05-09 16:49:57 +01004620 if (!task_new && !rq->rd->overutilized &&
4621 cpu_overutilized(rq->cpu))
4622 rq->rd->overutilized = true;
Juri Lelli4585a262015-08-19 19:47:12 +01004623
Patrick Bellasiedd28d32015-07-07 15:33:20 +01004624 schedtune_enqueue_task(p, cpu_of(rq));
4625
Juri Lelli4585a262015-08-19 19:47:12 +01004626 /*
4627 * We want to potentially trigger a freq switch
4628 * request only for tasks that are waking up; this is
4629 * because we get here also during load balancing, but
4630 * in these cases it seems wise to trigger as single
4631 * request after load balancing is done.
Juri Lelli4585a262015-08-19 19:47:12 +01004632 */
Juri Lelli43aac892015-06-26 12:14:23 +01004633 if (task_new || task_wakeup)
Juri Lelli4585a262015-08-19 19:47:12 +01004634 update_capacity_of(cpu_of(rq));
Morten Rasmussena562dfc2015-05-09 16:49:57 +01004635 }
Patrick Bellasi2178e842016-07-22 11:35:59 +01004636#endif /* CONFIG_SMP */
4637
Peter Zijlstraa4c2f002008-10-17 19:27:03 +02004638 hrtick_update(rq);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02004639}
4640
Venkatesh Pallipadi2f368252011-04-14 10:30:53 -07004641static void set_next_buddy(struct sched_entity *se);
4642
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02004643/*
4644 * The dequeue_task method is called before nr_running is
4645 * decreased. We remove the task from the rbtree and
4646 * update the fair scheduling stats:
4647 */
Peter Zijlstra371fd7e2010-03-24 16:38:48 +01004648static void dequeue_task_fair(struct rq *rq, struct task_struct *p, int flags)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02004649{
4650 struct cfs_rq *cfs_rq;
Peter Zijlstra62fb1852008-02-25 17:34:02 +01004651 struct sched_entity *se = &p->se;
Venkatesh Pallipadi2f368252011-04-14 10:30:53 -07004652 int task_sleep = flags & DEQUEUE_SLEEP;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02004653
4654 for_each_sched_entity(se) {
4655 cfs_rq = cfs_rq_of(se);
Peter Zijlstra371fd7e2010-03-24 16:38:48 +01004656 dequeue_entity(cfs_rq, se, flags);
Paul Turner85dac902011-07-21 09:43:33 -07004657
4658 /*
4659 * end evaluation on encountering a throttled cfs_rq
4660 *
4661 * note: in the case of encountering a throttled cfs_rq we will
4662 * post the final h_nr_running decrement below.
4663 */
4664 if (cfs_rq_throttled(cfs_rq))
4665 break;
Paul Turner953bfcd2011-07-21 09:43:27 -07004666 cfs_rq->h_nr_running--;
Peter Zijlstra2069dd72010-11-15 15:47:00 -08004667
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02004668 /* Don't dequeue parent if it has other entities besides us */
Venkatesh Pallipadi2f368252011-04-14 10:30:53 -07004669 if (cfs_rq->load.weight) {
Konstantin Khlebnikov754bd592016-06-16 15:57:15 +03004670 /* Avoid re-evaluating load for this entity: */
4671 se = parent_entity(se);
Venkatesh Pallipadi2f368252011-04-14 10:30:53 -07004672 /*
4673 * Bias pick_next to pick a task from this cfs_rq, as
4674 * p is sleeping when it is within its sched_slice.
4675 */
Konstantin Khlebnikov754bd592016-06-16 15:57:15 +03004676 if (task_sleep && se && !throttled_hierarchy(cfs_rq))
4677 set_next_buddy(se);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02004678 break;
Venkatesh Pallipadi2f368252011-04-14 10:30:53 -07004679 }
Peter Zijlstra371fd7e2010-03-24 16:38:48 +01004680 flags |= DEQUEUE_SLEEP;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02004681 }
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01004682
Peter Zijlstra2069dd72010-11-15 15:47:00 -08004683 for_each_sched_entity(se) {
Lin Ming0f317142011-07-22 09:14:31 +08004684 cfs_rq = cfs_rq_of(se);
Paul Turner953bfcd2011-07-21 09:43:27 -07004685 cfs_rq->h_nr_running--;
Peter Zijlstra2069dd72010-11-15 15:47:00 -08004686
Paul Turner85dac902011-07-21 09:43:33 -07004687 if (cfs_rq_throttled(cfs_rq))
4688 break;
4689
Yuyang Du9d89c252015-07-15 08:04:37 +08004690 update_load_avg(se, 1);
Linus Torvalds17bc14b2012-12-14 07:20:43 -08004691 update_cfs_shares(cfs_rq);
Peter Zijlstra2069dd72010-11-15 15:47:00 -08004692 }
4693
Patrick Bellasi2178e842016-07-22 11:35:59 +01004694 if (!se)
Kirill Tkhai72465442014-05-09 03:00:14 +04004695 sub_nr_running(rq, 1);
Patrick Bellasi2178e842016-07-22 11:35:59 +01004696
4697#ifdef CONFIG_SMP
4698
4699 if (!se) {
Patrick Bellasiedd28d32015-07-07 15:33:20 +01004700 schedtune_dequeue_task(p, cpu_of(rq));
Yuyang Ducd126af2015-07-15 08:04:36 +08004701
Juri Lelli4585a262015-08-19 19:47:12 +01004702 /*
4703 * We want to potentially trigger a freq switch
4704 * request only for tasks that are going to sleep;
4705 * this is because we get here also during load
4706 * balancing, but in these cases it seems wise to
4707 * trigger as single request after load balancing is
4708 * done.
4709 */
4710 if (task_sleep) {
4711 if (rq->cfs.nr_running)
4712 update_capacity_of(cpu_of(rq));
4713 else if (sched_freq())
4714 set_cfs_cpu_capacity(cpu_of(rq), false, 0);
4715 }
4716 }
Patrick Bellasi2178e842016-07-22 11:35:59 +01004717
4718#endif /* CONFIG_SMP */
4719
Peter Zijlstraa4c2f002008-10-17 19:27:03 +02004720 hrtick_update(rq);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02004721}
4722
Gregory Haskinse7693a32008-01-25 21:08:09 +01004723#ifdef CONFIG_SMP
Peter Zijlstra10e2f1a2016-05-09 10:38:05 +02004724
4725/* Working cpumask for: load_balance, load_balance_newidle. */
4726DEFINE_PER_CPU(cpumask_var_t, load_balance_mask);
4727DEFINE_PER_CPU(cpumask_var_t, select_idle_mask);
4728
Frederic Weisbecker9fd81dd2016-04-19 17:36:51 +02004729#ifdef CONFIG_NO_HZ_COMMON
Peter Zijlstra3289bdb2015-04-14 13:19:42 +02004730/*
4731 * per rq 'load' arrray crap; XXX kill this.
4732 */
4733
4734/*
Peter Zijlstrad937cdc2015-10-19 13:49:30 +02004735 * The exact cpuload calculated at every tick would be:
Peter Zijlstra3289bdb2015-04-14 13:19:42 +02004736 *
Peter Zijlstrad937cdc2015-10-19 13:49:30 +02004737 * load' = (1 - 1/2^i) * load + (1/2^i) * cur_load
4738 *
4739 * If a cpu misses updates for n ticks (as it was idle) and update gets
4740 * called on the n+1-th tick when cpu may be busy, then we have:
4741 *
4742 * load_n = (1 - 1/2^i)^n * load_0
4743 * load_n+1 = (1 - 1/2^i) * load_n + (1/2^i) * cur_load
Peter Zijlstra3289bdb2015-04-14 13:19:42 +02004744 *
4745 * decay_load_missed() below does efficient calculation of
Peter Zijlstrad937cdc2015-10-19 13:49:30 +02004746 *
4747 * load' = (1 - 1/2^i)^n * load
4748 *
4749 * Because x^(n+m) := x^n * x^m we can decompose any x^n in power-of-2 factors.
4750 * This allows us to precompute the above in said factors, thereby allowing the
4751 * reduction of an arbitrary n in O(log_2 n) steps. (See also
4752 * fixed_power_int())
Peter Zijlstra3289bdb2015-04-14 13:19:42 +02004753 *
4754 * The calculation is approximated on a 128 point scale.
Peter Zijlstra3289bdb2015-04-14 13:19:42 +02004755 */
4756#define DEGRADE_SHIFT 7
Peter Zijlstrad937cdc2015-10-19 13:49:30 +02004757
4758static const u8 degrade_zero_ticks[CPU_LOAD_IDX_MAX] = {0, 8, 32, 64, 128};
4759static const u8 degrade_factor[CPU_LOAD_IDX_MAX][DEGRADE_SHIFT + 1] = {
4760 { 0, 0, 0, 0, 0, 0, 0, 0 },
4761 { 64, 32, 8, 0, 0, 0, 0, 0 },
4762 { 96, 72, 40, 12, 1, 0, 0, 0 },
4763 { 112, 98, 75, 43, 15, 1, 0, 0 },
4764 { 120, 112, 98, 76, 45, 16, 2, 0 }
4765};
Peter Zijlstra3289bdb2015-04-14 13:19:42 +02004766
4767/*
4768 * Update cpu_load for any missed ticks, due to tickless idle. The backlog
4769 * would be when CPU is idle and so we just decay the old load without
4770 * adding any new load.
4771 */
4772static unsigned long
4773decay_load_missed(unsigned long load, unsigned long missed_updates, int idx)
4774{
4775 int j = 0;
4776
4777 if (!missed_updates)
4778 return load;
4779
4780 if (missed_updates >= degrade_zero_ticks[idx])
4781 return 0;
4782
4783 if (idx == 1)
4784 return load >> missed_updates;
4785
4786 while (missed_updates) {
4787 if (missed_updates % 2)
4788 load = (load * degrade_factor[idx][j]) >> DEGRADE_SHIFT;
4789
4790 missed_updates >>= 1;
4791 j++;
4792 }
4793 return load;
4794}
Frederic Weisbecker9fd81dd2016-04-19 17:36:51 +02004795#endif /* CONFIG_NO_HZ_COMMON */
Peter Zijlstra3289bdb2015-04-14 13:19:42 +02004796
Byungchul Park59543272015-10-14 18:47:35 +09004797/**
Frederic Weisbeckercee1afc2016-04-13 15:56:50 +02004798 * __cpu_load_update - update the rq->cpu_load[] statistics
Byungchul Park59543272015-10-14 18:47:35 +09004799 * @this_rq: The rq to update statistics for
4800 * @this_load: The current load
4801 * @pending_updates: The number of missed updates
Byungchul Park59543272015-10-14 18:47:35 +09004802 *
Peter Zijlstra3289bdb2015-04-14 13:19:42 +02004803 * Update rq->cpu_load[] statistics. This function is usually called every
Byungchul Park59543272015-10-14 18:47:35 +09004804 * scheduler tick (TICK_NSEC).
4805 *
4806 * This function computes a decaying average:
4807 *
4808 * load[i]' = (1 - 1/2^i) * load[i] + (1/2^i) * load
4809 *
4810 * Because of NOHZ it might not get called on every tick which gives need for
4811 * the @pending_updates argument.
4812 *
4813 * load[i]_n = (1 - 1/2^i) * load[i]_n-1 + (1/2^i) * load_n-1
4814 * = A * load[i]_n-1 + B ; A := (1 - 1/2^i), B := (1/2^i) * load
4815 * = A * (A * load[i]_n-2 + B) + B
4816 * = A * (A * (A * load[i]_n-3 + B) + B) + B
4817 * = A^3 * load[i]_n-3 + (A^2 + A + 1) * B
4818 * = A^n * load[i]_0 + (A^(n-1) + A^(n-2) + ... + 1) * B
4819 * = A^n * load[i]_0 + ((1 - A^n) / (1 - A)) * B
4820 * = (1 - 1/2^i)^n * (load[i]_0 - load) + load
4821 *
4822 * In the above we've assumed load_n := load, which is true for NOHZ_FULL as
4823 * any change in load would have resulted in the tick being turned back on.
4824 *
4825 * For regular NOHZ, this reduces to:
4826 *
4827 * load[i]_n = (1 - 1/2^i)^n * load[i]_0
4828 *
4829 * see decay_load_misses(). For NOHZ_FULL we get to subtract and add the extra
Frederic Weisbecker1f419062016-04-13 15:56:51 +02004830 * term.
Peter Zijlstra3289bdb2015-04-14 13:19:42 +02004831 */
Frederic Weisbecker1f419062016-04-13 15:56:51 +02004832static void cpu_load_update(struct rq *this_rq, unsigned long this_load,
4833 unsigned long pending_updates)
Peter Zijlstra3289bdb2015-04-14 13:19:42 +02004834{
Frederic Weisbecker9fd81dd2016-04-19 17:36:51 +02004835 unsigned long __maybe_unused tickless_load = this_rq->cpu_load[0];
Peter Zijlstra3289bdb2015-04-14 13:19:42 +02004836 int i, scale;
4837
4838 this_rq->nr_load_updates++;
4839
4840 /* Update our load: */
4841 this_rq->cpu_load[0] = this_load; /* Fasttrack for idx 0 */
4842 for (i = 1, scale = 2; i < CPU_LOAD_IDX_MAX; i++, scale += scale) {
4843 unsigned long old_load, new_load;
4844
4845 /* scale is effectively 1 << i now, and >> i divides by scale */
4846
Byungchul Park7400d3b2016-01-15 16:07:49 +09004847 old_load = this_rq->cpu_load[i];
Frederic Weisbecker9fd81dd2016-04-19 17:36:51 +02004848#ifdef CONFIG_NO_HZ_COMMON
Peter Zijlstra3289bdb2015-04-14 13:19:42 +02004849 old_load = decay_load_missed(old_load, pending_updates - 1, i);
Byungchul Park7400d3b2016-01-15 16:07:49 +09004850 if (tickless_load) {
4851 old_load -= decay_load_missed(tickless_load, pending_updates - 1, i);
4852 /*
4853 * old_load can never be a negative value because a
4854 * decayed tickless_load cannot be greater than the
4855 * original tickless_load.
4856 */
4857 old_load += tickless_load;
4858 }
Frederic Weisbecker9fd81dd2016-04-19 17:36:51 +02004859#endif
Peter Zijlstra3289bdb2015-04-14 13:19:42 +02004860 new_load = this_load;
4861 /*
4862 * Round up the averaging division if load is increasing. This
4863 * prevents us from getting stuck on 9 if the load is 10, for
4864 * example.
4865 */
4866 if (new_load > old_load)
4867 new_load += scale - 1;
4868
4869 this_rq->cpu_load[i] = (old_load * (scale - 1) + new_load) >> i;
4870 }
4871
4872 sched_avg_update(this_rq);
4873}
4874
Yuyang Du7ea241a2015-07-15 08:04:42 +08004875/* Used instead of source_load when we know the type == 0 */
4876static unsigned long weighted_cpuload(const int cpu)
4877{
4878 return cfs_rq_runnable_load_avg(&cpu_rq(cpu)->cfs);
4879}
4880
Peter Zijlstra3289bdb2015-04-14 13:19:42 +02004881#ifdef CONFIG_NO_HZ_COMMON
Frederic Weisbecker1f419062016-04-13 15:56:51 +02004882/*
4883 * There is no sane way to deal with nohz on smp when using jiffies because the
4884 * cpu doing the jiffies update might drift wrt the cpu doing the jiffy reading
4885 * causing off-by-one errors in observed deltas; {0,2} instead of {1,1}.
4886 *
4887 * Therefore we need to avoid the delta approach from the regular tick when
4888 * possible since that would seriously skew the load calculation. This is why we
4889 * use cpu_load_update_periodic() for CPUs out of nohz. However we'll rely on
4890 * jiffies deltas for updates happening while in nohz mode (idle ticks, idle
4891 * loop exit, nohz_idle_balance, nohz full exit...)
4892 *
4893 * This means we might still be one tick off for nohz periods.
4894 */
4895
4896static void cpu_load_update_nohz(struct rq *this_rq,
4897 unsigned long curr_jiffies,
4898 unsigned long load)
Frederic Weisbeckerbe68a682016-01-13 17:01:29 +01004899{
4900 unsigned long pending_updates;
4901
4902 pending_updates = curr_jiffies - this_rq->last_load_update_tick;
4903 if (pending_updates) {
4904 this_rq->last_load_update_tick = curr_jiffies;
4905 /*
4906 * In the regular NOHZ case, we were idle, this means load 0.
4907 * In the NOHZ_FULL case, we were non-idle, we should consider
4908 * its weighted load.
4909 */
Frederic Weisbecker1f419062016-04-13 15:56:51 +02004910 cpu_load_update(this_rq, load, pending_updates);
Frederic Weisbeckerbe68a682016-01-13 17:01:29 +01004911 }
4912}
4913
Peter Zijlstra3289bdb2015-04-14 13:19:42 +02004914/*
Peter Zijlstra3289bdb2015-04-14 13:19:42 +02004915 * Called from nohz_idle_balance() to update the load ratings before doing the
4916 * idle balance.
4917 */
Frederic Weisbeckercee1afc2016-04-13 15:56:50 +02004918static void cpu_load_update_idle(struct rq *this_rq)
Peter Zijlstra3289bdb2015-04-14 13:19:42 +02004919{
Peter Zijlstra3289bdb2015-04-14 13:19:42 +02004920 /*
4921 * bail if there's load or we're actually up-to-date.
4922 */
Frederic Weisbeckerbe68a682016-01-13 17:01:29 +01004923 if (weighted_cpuload(cpu_of(this_rq)))
Peter Zijlstra3289bdb2015-04-14 13:19:42 +02004924 return;
4925
Frederic Weisbecker1f419062016-04-13 15:56:51 +02004926 cpu_load_update_nohz(this_rq, READ_ONCE(jiffies), 0);
Peter Zijlstra3289bdb2015-04-14 13:19:42 +02004927}
4928
4929/*
Frederic Weisbecker1f419062016-04-13 15:56:51 +02004930 * Record CPU load on nohz entry so we know the tickless load to account
4931 * on nohz exit. cpu_load[0] happens then to be updated more frequently
4932 * than other cpu_load[idx] but it should be fine as cpu_load readers
4933 * shouldn't rely into synchronized cpu_load[*] updates.
Peter Zijlstra3289bdb2015-04-14 13:19:42 +02004934 */
Frederic Weisbecker1f419062016-04-13 15:56:51 +02004935void cpu_load_update_nohz_start(void)
Peter Zijlstra3289bdb2015-04-14 13:19:42 +02004936{
4937 struct rq *this_rq = this_rq();
Frederic Weisbecker1f419062016-04-13 15:56:51 +02004938
4939 /*
4940 * This is all lockless but should be fine. If weighted_cpuload changes
4941 * concurrently we'll exit nohz. And cpu_load write can race with
4942 * cpu_load_update_idle() but both updater would be writing the same.
4943 */
4944 this_rq->cpu_load[0] = weighted_cpuload(cpu_of(this_rq));
4945}
4946
4947/*
4948 * Account the tickless load in the end of a nohz frame.
4949 */
4950void cpu_load_update_nohz_stop(void)
4951{
Jason Low316c1608d2015-04-28 13:00:20 -07004952 unsigned long curr_jiffies = READ_ONCE(jiffies);
Frederic Weisbecker1f419062016-04-13 15:56:51 +02004953 struct rq *this_rq = this_rq();
4954 unsigned long load;
Peter Zijlstra3289bdb2015-04-14 13:19:42 +02004955
4956 if (curr_jiffies == this_rq->last_load_update_tick)
4957 return;
4958
Frederic Weisbecker1f419062016-04-13 15:56:51 +02004959 load = weighted_cpuload(cpu_of(this_rq));
Peter Zijlstra3289bdb2015-04-14 13:19:42 +02004960 raw_spin_lock(&this_rq->lock);
Matt Flemingb52fad22016-05-03 20:46:54 +01004961 update_rq_clock(this_rq);
Frederic Weisbecker1f419062016-04-13 15:56:51 +02004962 cpu_load_update_nohz(this_rq, curr_jiffies, load);
Peter Zijlstra3289bdb2015-04-14 13:19:42 +02004963 raw_spin_unlock(&this_rq->lock);
4964}
Frederic Weisbecker1f419062016-04-13 15:56:51 +02004965#else /* !CONFIG_NO_HZ_COMMON */
4966static inline void cpu_load_update_nohz(struct rq *this_rq,
4967 unsigned long curr_jiffies,
4968 unsigned long load) { }
4969#endif /* CONFIG_NO_HZ_COMMON */
4970
4971static void cpu_load_update_periodic(struct rq *this_rq, unsigned long load)
4972{
Frederic Weisbecker9fd81dd2016-04-19 17:36:51 +02004973#ifdef CONFIG_NO_HZ_COMMON
Frederic Weisbecker1f419062016-04-13 15:56:51 +02004974 /* See the mess around cpu_load_update_nohz(). */
4975 this_rq->last_load_update_tick = READ_ONCE(jiffies);
Frederic Weisbecker9fd81dd2016-04-19 17:36:51 +02004976#endif
Frederic Weisbecker1f419062016-04-13 15:56:51 +02004977 cpu_load_update(this_rq, load, 1);
4978}
Peter Zijlstra3289bdb2015-04-14 13:19:42 +02004979
4980/*
4981 * Called from scheduler_tick()
4982 */
Frederic Weisbeckercee1afc2016-04-13 15:56:50 +02004983void cpu_load_update_active(struct rq *this_rq)
Peter Zijlstra3289bdb2015-04-14 13:19:42 +02004984{
Yuyang Du7ea241a2015-07-15 08:04:42 +08004985 unsigned long load = weighted_cpuload(cpu_of(this_rq));
Frederic Weisbecker1f419062016-04-13 15:56:51 +02004986
4987 if (tick_nohz_tick_stopped())
4988 cpu_load_update_nohz(this_rq, READ_ONCE(jiffies), load);
4989 else
4990 cpu_load_update_periodic(this_rq, load);
Peter Zijlstra3289bdb2015-04-14 13:19:42 +02004991}
4992
Peter Zijlstra029632f2011-10-25 10:00:11 +02004993/*
4994 * Return a low guess at the load of a migration-source cpu weighted
4995 * according to the scheduling class and "nice" value.
4996 *
4997 * We want to under-estimate the load of migration sources, to
4998 * balance conservatively.
4999 */
5000static unsigned long source_load(int cpu, int type)
5001{
5002 struct rq *rq = cpu_rq(cpu);
5003 unsigned long total = weighted_cpuload(cpu);
5004
5005 if (type == 0 || !sched_feat(LB_BIAS))
5006 return total;
5007
5008 return min(rq->cpu_load[type-1], total);
5009}
5010
5011/*
5012 * Return a high guess at the load of a migration-target cpu weighted
5013 * according to the scheduling class and "nice" value.
5014 */
5015static unsigned long target_load(int cpu, int type)
5016{
5017 struct rq *rq = cpu_rq(cpu);
5018 unsigned long total = weighted_cpuload(cpu);
5019
5020 if (type == 0 || !sched_feat(LB_BIAS))
5021 return total;
5022
5023 return max(rq->cpu_load[type-1], total);
5024}
5025
Vincent Guittotca6d75e2015-02-27 16:54:09 +01005026
Peter Zijlstra029632f2011-10-25 10:00:11 +02005027static unsigned long cpu_avg_load_per_task(int cpu)
5028{
5029 struct rq *rq = cpu_rq(cpu);
Jason Low316c1608d2015-04-28 13:00:20 -07005030 unsigned long nr_running = READ_ONCE(rq->cfs.h_nr_running);
Yuyang Du7ea241a2015-07-15 08:04:42 +08005031 unsigned long load_avg = weighted_cpuload(cpu);
Peter Zijlstra029632f2011-10-25 10:00:11 +02005032
5033 if (nr_running)
Alex Shib92486c2013-06-20 10:18:50 +08005034 return load_avg / nr_running;
Peter Zijlstra029632f2011-10-25 10:00:11 +02005035
5036 return 0;
5037}
5038
Peter Zijlstrabb3469a2008-06-27 13:41:27 +02005039#ifdef CONFIG_FAIR_GROUP_SCHED
Peter Zijlstraf5bfb7d2008-06-27 13:41:39 +02005040/*
5041 * effective_load() calculates the load change as seen from the root_task_group
5042 *
5043 * Adding load to a group doesn't make a group heavier, but can cause movement
5044 * of group shares between cpus. Assuming the shares were perfectly aligned one
5045 * can calculate the shift in shares.
Peter Zijlstracf5f0ac2011-10-13 16:52:28 +02005046 *
5047 * Calculate the effective load difference if @wl is added (subtracted) to @tg
5048 * on this @cpu and results in a total addition (subtraction) of @wg to the
5049 * total group weight.
5050 *
5051 * Given a runqueue weight distribution (rw_i) we can compute a shares
5052 * distribution (s_i) using:
5053 *
5054 * s_i = rw_i / \Sum rw_j (1)
5055 *
5056 * Suppose we have 4 CPUs and our @tg is a direct child of the root group and
5057 * has 7 equal weight tasks, distributed as below (rw_i), with the resulting
5058 * shares distribution (s_i):
5059 *
5060 * rw_i = { 2, 4, 1, 0 }
5061 * s_i = { 2/7, 4/7, 1/7, 0 }
5062 *
5063 * As per wake_affine() we're interested in the load of two CPUs (the CPU the
5064 * task used to run on and the CPU the waker is running on), we need to
5065 * compute the effect of waking a task on either CPU and, in case of a sync
5066 * wakeup, compute the effect of the current task going to sleep.
5067 *
5068 * So for a change of @wl to the local @cpu with an overall group weight change
5069 * of @wl we can compute the new shares distribution (s'_i) using:
5070 *
5071 * s'_i = (rw_i + @wl) / (@wg + \Sum rw_j) (2)
5072 *
5073 * Suppose we're interested in CPUs 0 and 1, and want to compute the load
5074 * differences in waking a task to CPU 0. The additional task changes the
5075 * weight and shares distributions like:
5076 *
5077 * rw'_i = { 3, 4, 1, 0 }
5078 * s'_i = { 3/8, 4/8, 1/8, 0 }
5079 *
5080 * We can then compute the difference in effective weight by using:
5081 *
5082 * dw_i = S * (s'_i - s_i) (3)
5083 *
5084 * Where 'S' is the group weight as seen by its parent.
5085 *
5086 * Therefore the effective change in loads on CPU 0 would be 5/56 (3/8 - 2/7)
5087 * times the weight of the group. The effect on CPU 1 would be -4/56 (4/8 -
5088 * 4/7) times the weight of the group.
Peter Zijlstraf5bfb7d2008-06-27 13:41:39 +02005089 */
Peter Zijlstra2069dd72010-11-15 15:47:00 -08005090static long effective_load(struct task_group *tg, int cpu, long wl, long wg)
Peter Zijlstrabb3469a2008-06-27 13:41:27 +02005091{
Peter Zijlstra4be9daa2008-06-27 13:41:30 +02005092 struct sched_entity *se = tg->se[cpu];
Peter Zijlstraf1d239f2008-06-27 13:41:38 +02005093
Rik van Riel9722c2d2014-01-06 11:39:12 +00005094 if (!tg->parent) /* the trivial, non-cgroup case */
Peter Zijlstraf1d239f2008-06-27 13:41:38 +02005095 return wl;
5096
Peter Zijlstra4be9daa2008-06-27 13:41:30 +02005097 for_each_sched_entity(se) {
Peter Zijlstra7dd49122016-06-24 15:53:54 +02005098 struct cfs_rq *cfs_rq = se->my_q;
5099 long W, w = cfs_rq_load_avg(cfs_rq);
Peter Zijlstrabb3469a2008-06-27 13:41:27 +02005100
Peter Zijlstra7dd49122016-06-24 15:53:54 +02005101 tg = cfs_rq->tg;
Peter Zijlstra4be9daa2008-06-27 13:41:30 +02005102
Peter Zijlstracf5f0ac2011-10-13 16:52:28 +02005103 /*
5104 * W = @wg + \Sum rw_j
5105 */
Peter Zijlstra7dd49122016-06-24 15:53:54 +02005106 W = wg + atomic_long_read(&tg->load_avg);
5107
5108 /* Ensure \Sum rw_j >= rw_i */
5109 W -= cfs_rq->tg_load_avg_contrib;
5110 W += w;
Peter Zijlstra4be9daa2008-06-27 13:41:30 +02005111
Peter Zijlstracf5f0ac2011-10-13 16:52:28 +02005112 /*
5113 * w = rw_i + @wl
5114 */
Peter Zijlstra7dd49122016-06-24 15:53:54 +02005115 w += wl;
Peter Zijlstra940959e2008-09-23 15:33:42 +02005116
Peter Zijlstracf5f0ac2011-10-13 16:52:28 +02005117 /*
5118 * wl = S * s'_i; see (2)
5119 */
5120 if (W > 0 && w < W)
Dietmar Eggemannab522e32016-08-22 15:00:41 +01005121 wl = (w * (long)scale_load_down(tg->shares)) / W;
Paul Turner977dda72011-01-14 17:57:50 -08005122 else
Dietmar Eggemannab522e32016-08-22 15:00:41 +01005123 wl = scale_load_down(tg->shares);
Peter Zijlstra940959e2008-09-23 15:33:42 +02005124
Peter Zijlstracf5f0ac2011-10-13 16:52:28 +02005125 /*
5126 * Per the above, wl is the new se->load.weight value; since
5127 * those are clipped to [MIN_SHARES, ...) do so now. See
5128 * calc_cfs_shares().
5129 */
Paul Turner977dda72011-01-14 17:57:50 -08005130 if (wl < MIN_SHARES)
5131 wl = MIN_SHARES;
Peter Zijlstracf5f0ac2011-10-13 16:52:28 +02005132
5133 /*
5134 * wl = dw_i = S * (s'_i - s_i); see (3)
5135 */
Yuyang Du9d89c252015-07-15 08:04:37 +08005136 wl -= se->avg.load_avg;
Peter Zijlstracf5f0ac2011-10-13 16:52:28 +02005137
5138 /*
5139 * Recursively apply this logic to all parent groups to compute
5140 * the final effective load change on the root group. Since
5141 * only the @tg group gets extra weight, all parent groups can
5142 * only redistribute existing shares. @wl is the shift in shares
5143 * resulting from this level per the above.
5144 */
Peter Zijlstra4be9daa2008-06-27 13:41:30 +02005145 wg = 0;
Peter Zijlstra4be9daa2008-06-27 13:41:30 +02005146 }
5147
5148 return wl;
Peter Zijlstrabb3469a2008-06-27 13:41:27 +02005149}
5150#else
Peter Zijlstra4be9daa2008-06-27 13:41:30 +02005151
Mel Gorman58d081b2013-10-07 11:29:10 +01005152static long effective_load(struct task_group *tg, int cpu, long wl, long wg)
Peter Zijlstra4be9daa2008-06-27 13:41:30 +02005153{
Peter Zijlstra83378262008-06-27 13:41:37 +02005154 return wl;
Peter Zijlstrabb3469a2008-06-27 13:41:27 +02005155}
Peter Zijlstra4be9daa2008-06-27 13:41:30 +02005156
Peter Zijlstrabb3469a2008-06-27 13:41:27 +02005157#endif
5158
Peter Zijlstrac58d25f2016-05-12 09:19:59 +02005159static void record_wakee(struct task_struct *p)
5160{
5161 /*
5162 * Only decay a single time; tasks that have less then 1 wakeup per
5163 * jiffy will not have built up many flips.
5164 */
5165 if (time_after(jiffies, current->wakee_flip_decay_ts + HZ)) {
5166 current->wakee_flips >>= 1;
5167 current->wakee_flip_decay_ts = jiffies;
5168 }
5169
5170 if (current->last_wakee != p) {
5171 current->last_wakee = p;
5172 current->wakee_flips++;
5173 }
5174}
5175
Juri Lelli2f8ed122015-04-30 17:35:23 +01005176/*
5177 * Returns the current capacity of cpu after applying both
5178 * cpu and freq scaling.
5179 */
5180unsigned long capacity_curr_of(int cpu)
5181{
5182 return cpu_rq(cpu)->cpu_capacity_orig *
5183 arch_scale_freq_capacity(NULL, cpu)
5184 >> SCHED_CAPACITY_SHIFT;
5185}
5186
Morten Rasmussen94c4cea2015-01-13 13:45:51 +00005187static inline bool energy_aware(void)
5188{
5189 return sched_feat(ENERGY_AWARE);
5190}
5191
Morten Rasmussena455fa72015-01-02 14:21:56 +00005192struct energy_env {
5193 struct sched_group *sg_top;
5194 struct sched_group *sg_cap;
5195 int cap_idx;
5196 int util_delta;
5197 int src_cpu;
5198 int dst_cpu;
5199 int energy;
Patrick Bellasi88d06052016-01-15 15:48:03 +00005200 int payoff;
5201 struct task_struct *task;
Patrick Bellasi632905f2016-01-14 18:35:13 +00005202 struct {
5203 int before;
5204 int after;
Patrick Bellasi88d06052016-01-15 15:48:03 +00005205 int delta;
Patrick Bellasi632905f2016-01-14 18:35:13 +00005206 int diff;
5207 } nrg;
5208 struct {
5209 int before;
5210 int after;
5211 int delta;
5212 } cap;
Morten Rasmussena455fa72015-01-02 14:21:56 +00005213};
5214
Mike Galbraith63b0e9e2015-07-14 17:39:50 +02005215/*
Morten Rasmussena455fa72015-01-02 14:21:56 +00005216 * __cpu_norm_util() returns the cpu util relative to a specific capacity,
Morten Rasmussen61bf6252014-12-18 14:47:18 +00005217 * i.e. it's busy ratio, in the range [0..SCHED_LOAD_SCALE] which is useful for
5218 * energy calculations. Using the scale-invariant util returned by
5219 * cpu_util() and approximating scale-invariant util by:
5220 *
5221 * util ~ (curr_freq/max_freq)*1024 * capacity_orig/1024 * running_time/time
5222 *
5223 * the normalized util can be found using the specific capacity.
5224 *
5225 * capacity = capacity_orig * curr_freq/max_freq
5226 *
5227 * norm_util = running_time/time ~ util/capacity
5228 */
Morten Rasmussena455fa72015-01-02 14:21:56 +00005229static unsigned long __cpu_norm_util(int cpu, unsigned long capacity, int delta)
Morten Rasmussen61bf6252014-12-18 14:47:18 +00005230{
Morten Rasmussena455fa72015-01-02 14:21:56 +00005231 int util = __cpu_util(cpu, delta);
Morten Rasmussen61bf6252014-12-18 14:47:18 +00005232
5233 if (util >= capacity)
5234 return SCHED_CAPACITY_SCALE;
5235
5236 return (util << SCHED_CAPACITY_SHIFT)/capacity;
5237}
5238
Morten Rasmussena455fa72015-01-02 14:21:56 +00005239static int calc_util_delta(struct energy_env *eenv, int cpu)
Morten Rasmussen61bf6252014-12-18 14:47:18 +00005240{
Morten Rasmussena455fa72015-01-02 14:21:56 +00005241 if (cpu == eenv->src_cpu)
5242 return -eenv->util_delta;
5243 if (cpu == eenv->dst_cpu)
5244 return eenv->util_delta;
5245 return 0;
5246}
5247
5248static
5249unsigned long group_max_util(struct energy_env *eenv)
5250{
5251 int i, delta;
Morten Rasmussen61bf6252014-12-18 14:47:18 +00005252 unsigned long max_util = 0;
5253
Morten Rasmussena455fa72015-01-02 14:21:56 +00005254 for_each_cpu(i, sched_group_cpus(eenv->sg_cap)) {
5255 delta = calc_util_delta(eenv, i);
5256 max_util = max(max_util, __cpu_util(i, delta));
5257 }
Morten Rasmussen61bf6252014-12-18 14:47:18 +00005258
5259 return max_util;
5260}
5261
5262/*
5263 * group_norm_util() returns the approximated group util relative to it's
5264 * current capacity (busy ratio) in the range [0..SCHED_LOAD_SCALE] for use in
5265 * energy calculations. Since task executions may or may not overlap in time in
5266 * the group the true normalized util is between max(cpu_norm_util(i)) and
5267 * sum(cpu_norm_util(i)) when iterating over all cpus in the group, i. The
5268 * latter is used as the estimate as it leads to a more pessimistic energy
5269 * estimate (more busy).
5270 */
Morten Rasmussena455fa72015-01-02 14:21:56 +00005271static unsigned
5272long group_norm_util(struct energy_env *eenv, struct sched_group *sg)
Morten Rasmussen61bf6252014-12-18 14:47:18 +00005273{
Morten Rasmussena455fa72015-01-02 14:21:56 +00005274 int i, delta;
Morten Rasmussen61bf6252014-12-18 14:47:18 +00005275 unsigned long util_sum = 0;
Morten Rasmussena455fa72015-01-02 14:21:56 +00005276 unsigned long capacity = sg->sge->cap_states[eenv->cap_idx].cap;
Morten Rasmussen61bf6252014-12-18 14:47:18 +00005277
Morten Rasmussena455fa72015-01-02 14:21:56 +00005278 for_each_cpu(i, sched_group_cpus(sg)) {
5279 delta = calc_util_delta(eenv, i);
5280 util_sum += __cpu_norm_util(i, capacity, delta);
5281 }
Morten Rasmussen61bf6252014-12-18 14:47:18 +00005282
5283 if (util_sum > SCHED_CAPACITY_SCALE)
5284 return SCHED_CAPACITY_SCALE;
5285 return util_sum;
5286}
5287
Morten Rasmussena455fa72015-01-02 14:21:56 +00005288static int find_new_capacity(struct energy_env *eenv,
Morten Rasmussen61bf6252014-12-18 14:47:18 +00005289 const struct sched_group_energy const *sge)
5290{
5291 int idx;
Morten Rasmussena455fa72015-01-02 14:21:56 +00005292 unsigned long util = group_max_util(eenv);
Morten Rasmussen61bf6252014-12-18 14:47:18 +00005293
5294 for (idx = 0; idx < sge->nr_cap_states; idx++) {
5295 if (sge->cap_states[idx].cap >= util)
Morten Rasmussena455fa72015-01-02 14:21:56 +00005296 break;
Morten Rasmussen61bf6252014-12-18 14:47:18 +00005297 }
5298
Morten Rasmussena455fa72015-01-02 14:21:56 +00005299 eenv->cap_idx = idx;
5300
Morten Rasmussen61bf6252014-12-18 14:47:18 +00005301 return idx;
5302}
5303
Dietmar Eggemann1f884f42015-01-27 14:04:17 +00005304static int group_idle_state(struct sched_group *sg)
5305{
5306 int i, state = INT_MAX;
5307
5308 /* Find the shallowest idle state in the sched group. */
5309 for_each_cpu(i, sched_group_cpus(sg))
5310 state = min(state, idle_get_state_idx(cpu_rq(i)));
5311
5312 /* Take non-cpuidle idling into account (active idle/arch_cpu_idle()) */
5313 state++;
5314
5315 return state;
5316}
5317
Morten Rasmussen61bf6252014-12-18 14:47:18 +00005318/*
5319 * sched_group_energy(): Computes the absolute energy consumption of cpus
5320 * belonging to the sched_group including shared resources shared only by
5321 * members of the group. Iterates over all cpus in the hierarchy below the
5322 * sched_group starting from the bottom working it's way up before going to
5323 * the next cpu until all cpus are covered at all levels. The current
5324 * implementation is likely to gather the same util statistics multiple times.
5325 * This can probably be done in a faster but more complex way.
5326 * Note: sched_group_energy() may fail when racing with sched_domain updates.
5327 */
Morten Rasmussena455fa72015-01-02 14:21:56 +00005328static int sched_group_energy(struct energy_env *eenv)
Morten Rasmussen61bf6252014-12-18 14:47:18 +00005329{
5330 struct sched_domain *sd;
5331 int cpu, total_energy = 0;
5332 struct cpumask visit_cpus;
5333 struct sched_group *sg;
5334
Morten Rasmussena455fa72015-01-02 14:21:56 +00005335 WARN_ON(!eenv->sg_top->sge);
Morten Rasmussen61bf6252014-12-18 14:47:18 +00005336
Morten Rasmussena455fa72015-01-02 14:21:56 +00005337 cpumask_copy(&visit_cpus, sched_group_cpus(eenv->sg_top));
Morten Rasmussen61bf6252014-12-18 14:47:18 +00005338
5339 while (!cpumask_empty(&visit_cpus)) {
5340 struct sched_group *sg_shared_cap = NULL;
5341
5342 cpu = cpumask_first(&visit_cpus);
5343
5344 /*
5345 * Is the group utilization affected by cpus outside this
5346 * sched_group?
5347 */
5348 sd = rcu_dereference(per_cpu(sd_scs, cpu));
5349
5350 if (!sd)
5351 /*
5352 * We most probably raced with hotplug; returning a
5353 * wrong energy estimation is better than entering an
5354 * infinite loop.
5355 */
5356 return -EINVAL;
5357
5358 if (sd->parent)
5359 sg_shared_cap = sd->parent->groups;
5360
5361 for_each_domain(cpu, sd) {
5362 sg = sd->groups;
5363
5364 /* Has this sched_domain already been visited? */
5365 if (sd->child && group_first_cpu(sg) != cpu)
5366 break;
5367
5368 do {
Morten Rasmussen61bf6252014-12-18 14:47:18 +00005369 unsigned long group_util;
Dietmar Eggemann1f884f42015-01-27 14:04:17 +00005370 int sg_busy_energy, sg_idle_energy;
5371 int cap_idx, idle_idx;
Morten Rasmussen61bf6252014-12-18 14:47:18 +00005372
5373 if (sg_shared_cap && sg_shared_cap->group_weight >= sg->group_weight)
Morten Rasmussena455fa72015-01-02 14:21:56 +00005374 eenv->sg_cap = sg_shared_cap;
Morten Rasmussen61bf6252014-12-18 14:47:18 +00005375 else
Morten Rasmussena455fa72015-01-02 14:21:56 +00005376 eenv->sg_cap = sg;
Morten Rasmussen61bf6252014-12-18 14:47:18 +00005377
Morten Rasmussena455fa72015-01-02 14:21:56 +00005378 cap_idx = find_new_capacity(eenv, sg->sge);
Patrick Bellasi632905f2016-01-14 18:35:13 +00005379
5380 if (sg->group_weight == 1) {
5381 /* Remove capacity of src CPU (before task move) */
5382 if (eenv->util_delta == 0 &&
5383 cpumask_test_cpu(eenv->src_cpu, sched_group_cpus(sg))) {
5384 eenv->cap.before = sg->sge->cap_states[cap_idx].cap;
5385 eenv->cap.delta -= eenv->cap.before;
5386 }
5387 /* Add capacity of dst CPU (after task move) */
5388 if (eenv->util_delta != 0 &&
5389 cpumask_test_cpu(eenv->dst_cpu, sched_group_cpus(sg))) {
5390 eenv->cap.after = sg->sge->cap_states[cap_idx].cap;
5391 eenv->cap.delta += eenv->cap.after;
5392 }
5393 }
5394
Dietmar Eggemann1f884f42015-01-27 14:04:17 +00005395 idle_idx = group_idle_state(sg);
Morten Rasmussena455fa72015-01-02 14:21:56 +00005396 group_util = group_norm_util(eenv, sg);
Morten Rasmussen61bf6252014-12-18 14:47:18 +00005397 sg_busy_energy = (group_util * sg->sge->cap_states[cap_idx].power)
Dietmar Eggemann1f884f42015-01-27 14:04:17 +00005398 >> SCHED_CAPACITY_SHIFT;
5399 sg_idle_energy = ((SCHED_CAPACITY_SCALE-group_util)
5400 * sg->sge->idle_states[idle_idx].power)
5401 >> SCHED_CAPACITY_SHIFT;
Morten Rasmussen61bf6252014-12-18 14:47:18 +00005402
5403 total_energy += sg_busy_energy + sg_idle_energy;
5404
5405 if (!sd->child)
5406 cpumask_xor(&visit_cpus, &visit_cpus, sched_group_cpus(sg));
5407
Morten Rasmussena455fa72015-01-02 14:21:56 +00005408 if (cpumask_equal(sched_group_cpus(sg), sched_group_cpus(eenv->sg_top)))
Morten Rasmussen61bf6252014-12-18 14:47:18 +00005409 goto next_cpu;
5410
5411 } while (sg = sg->next, sg != sd->groups);
5412 }
5413next_cpu:
5414 continue;
5415 }
5416
Morten Rasmussena455fa72015-01-02 14:21:56 +00005417 eenv->energy = total_energy;
5418 return 0;
Morten Rasmussen61bf6252014-12-18 14:47:18 +00005419}
5420
Morten Rasmussen931bd822015-01-06 17:34:05 +00005421static inline bool cpu_in_sg(struct sched_group *sg, int cpu)
5422{
5423 return cpu != -1 && cpumask_test_cpu(cpu, sched_group_cpus(sg));
5424}
5425
Patrick Bellasi88d06052016-01-15 15:48:03 +00005426#ifdef CONFIG_SCHED_TUNE
5427static int energy_diff_evaluate(struct energy_env *eenv)
5428{
5429 unsigned int boost;
5430 int nrg_delta;
5431
5432 /* Return energy diff when boost margin is 0 */
5433#ifdef CONFIG_CGROUP_SCHEDTUNE
5434 boost = schedtune_task_boost(eenv->task);
5435#else
5436 boost = get_sysctl_sched_cfs_boost();
5437#endif
5438 if (boost == 0)
5439 return eenv->nrg.diff;
5440
5441 /* Compute normalized energy diff */
5442 nrg_delta = schedtune_normalize_energy(eenv->nrg.diff);
5443 eenv->nrg.delta = nrg_delta;
5444
5445 eenv->payoff = schedtune_accept_deltas(
5446 eenv->nrg.delta,
5447 eenv->cap.delta,
5448 eenv->task);
5449
5450 /*
5451 * When SchedTune is enabled, the energy_diff() function will return
5452 * the computed energy payoff value. Since the energy_diff() return
5453 * value is expected to be negative by its callers, this evaluation
5454 * function return a negative value each time the evaluation return a
5455 * positive payoff, which is the condition for the acceptance of
5456 * a scheduling decision
5457 */
5458 return -eenv->payoff;
5459}
5460#else /* CONFIG_SCHED_TUNE */
5461#define energy_diff_evaluate(eenv) eenv->nrg.diff
5462#endif
5463
Morten Rasmussen931bd822015-01-06 17:34:05 +00005464/*
5465 * energy_diff(): Estimate the energy impact of changing the utilization
5466 * distribution. eenv specifies the change: utilisation amount, source, and
5467 * destination cpu. Source or destination cpu may be -1 in which case the
5468 * utilization is removed from or added to the system (e.g. task wake-up). If
5469 * both are specified, the utilization is migrated.
5470 */
5471static int energy_diff(struct energy_env *eenv)
5472{
5473 struct sched_domain *sd;
5474 struct sched_group *sg;
5475 int sd_cpu = -1, energy_before = 0, energy_after = 0;
Patrick Bellasi110441b2016-01-14 18:47:21 +00005476 int result;
Morten Rasmussen931bd822015-01-06 17:34:05 +00005477
5478 struct energy_env eenv_before = {
5479 .util_delta = 0,
5480 .src_cpu = eenv->src_cpu,
5481 .dst_cpu = eenv->dst_cpu,
Patrick Bellasi88d06052016-01-15 15:48:03 +00005482 .nrg = { 0, 0, 0, 0},
Patrick Bellasi632905f2016-01-14 18:35:13 +00005483 .cap = { 0, 0, 0 },
Morten Rasmussen931bd822015-01-06 17:34:05 +00005484 };
5485
5486 if (eenv->src_cpu == eenv->dst_cpu)
5487 return 0;
5488
5489 sd_cpu = (eenv->src_cpu != -1) ? eenv->src_cpu : eenv->dst_cpu;
5490 sd = rcu_dereference(per_cpu(sd_ea, sd_cpu));
5491
5492 if (!sd)
5493 return 0; /* Error */
5494
5495 sg = sd->groups;
5496
5497 do {
5498 if (cpu_in_sg(sg, eenv->src_cpu) || cpu_in_sg(sg, eenv->dst_cpu)) {
5499 eenv_before.sg_top = eenv->sg_top = sg;
5500
5501 if (sched_group_energy(&eenv_before))
5502 return 0; /* Invalid result abort */
5503 energy_before += eenv_before.energy;
5504
Patrick Bellasi632905f2016-01-14 18:35:13 +00005505 /* Keep track of SRC cpu (before) capacity */
5506 eenv->cap.before = eenv_before.cap.before;
5507 eenv->cap.delta = eenv_before.cap.delta;
5508
Morten Rasmussen931bd822015-01-06 17:34:05 +00005509 if (sched_group_energy(eenv))
5510 return 0; /* Invalid result abort */
5511 energy_after += eenv->energy;
5512 }
5513 } while (sg = sg->next, sg != sd->groups);
5514
Patrick Bellasi632905f2016-01-14 18:35:13 +00005515 eenv->nrg.before = energy_before;
5516 eenv->nrg.after = energy_after;
5517 eenv->nrg.diff = eenv->nrg.after - eenv->nrg.before;
Patrick Bellasi88d06052016-01-15 15:48:03 +00005518 eenv->payoff = 0;
Patrick Bellasi632905f2016-01-14 18:35:13 +00005519
Patrick Bellasi110441b2016-01-14 18:47:21 +00005520 result = energy_diff_evaluate(eenv);
5521
5522 trace_sched_energy_diff(eenv->task,
5523 eenv->src_cpu, eenv->dst_cpu, eenv->util_delta,
5524 eenv->nrg.before, eenv->nrg.after, eenv->nrg.diff,
5525 eenv->cap.before, eenv->cap.after, eenv->cap.delta,
5526 eenv->nrg.delta, eenv->payoff);
5527
5528 return result;
Morten Rasmussen931bd822015-01-06 17:34:05 +00005529}
5530
Morten Rasmussen61bf6252014-12-18 14:47:18 +00005531/*
Mike Galbraith63b0e9e2015-07-14 17:39:50 +02005532 * Detect M:N waker/wakee relationships via a switching-frequency heuristic.
Peter Zijlstrac58d25f2016-05-12 09:19:59 +02005533 *
Mike Galbraith63b0e9e2015-07-14 17:39:50 +02005534 * A waker of many should wake a different task than the one last awakened
Peter Zijlstrac58d25f2016-05-12 09:19:59 +02005535 * at a frequency roughly N times higher than one of its wakees.
5536 *
5537 * In order to determine whether we should let the load spread vs consolidating
5538 * to shared cache, we look for a minimum 'flip' frequency of llc_size in one
5539 * partner, and a factor of lls_size higher frequency in the other.
5540 *
5541 * With both conditions met, we can be relatively sure that the relationship is
5542 * non-monogamous, with partner count exceeding socket size.
5543 *
5544 * Waker/wakee being client/server, worker/dispatcher, interrupt source or
5545 * whatever is irrelevant, spread criteria is apparent partner count exceeds
5546 * socket size.
Mike Galbraith63b0e9e2015-07-14 17:39:50 +02005547 */
Michael Wang62470412013-07-04 12:55:51 +08005548static int wake_wide(struct task_struct *p)
5549{
Mike Galbraith63b0e9e2015-07-14 17:39:50 +02005550 unsigned int master = current->wakee_flips;
5551 unsigned int slave = p->wakee_flips;
Peter Zijlstra7d9ffa82013-07-04 12:56:46 +08005552 int factor = this_cpu_read(sd_llc_size);
Michael Wang62470412013-07-04 12:55:51 +08005553
Mike Galbraith63b0e9e2015-07-14 17:39:50 +02005554 if (master < slave)
5555 swap(master, slave);
5556 if (slave < factor || master < slave * factor)
5557 return 0;
5558 return 1;
Michael Wang62470412013-07-04 12:55:51 +08005559}
5560
Morten Rasmussen772bd008c2016-06-22 18:03:13 +01005561static int wake_affine(struct sched_domain *sd, struct task_struct *p,
5562 int prev_cpu, int sync)
Ingo Molnar098fb9d2008-03-16 20:36:10 +01005563{
Paul Turnere37b6a72011-01-21 20:44:59 -08005564 s64 this_load, load;
Vincent Guittotbd61c982014-08-26 13:06:50 +02005565 s64 this_eff_load, prev_eff_load;
Morten Rasmussen772bd008c2016-06-22 18:03:13 +01005566 int idx, this_cpu;
Peter Zijlstrac88d5912009-09-10 13:50:02 +02005567 struct task_group *tg;
Peter Zijlstra83378262008-06-27 13:41:37 +02005568 unsigned long weight;
Mike Galbraithb3137bc2008-05-29 11:11:41 +02005569 int balanced;
Ingo Molnar098fb9d2008-03-16 20:36:10 +01005570
Peter Zijlstrac88d5912009-09-10 13:50:02 +02005571 idx = sd->wake_idx;
5572 this_cpu = smp_processor_id();
Peter Zijlstrac88d5912009-09-10 13:50:02 +02005573 load = source_load(prev_cpu, idx);
5574 this_load = target_load(this_cpu, idx);
Ingo Molnar098fb9d2008-03-16 20:36:10 +01005575
5576 /*
Ingo Molnar098fb9d2008-03-16 20:36:10 +01005577 * If sync wakeup then subtract the (maximum possible)
5578 * effect of the currently running task from the load
5579 * of the current CPU:
5580 */
Peter Zijlstra83378262008-06-27 13:41:37 +02005581 if (sync) {
5582 tg = task_group(current);
Yuyang Du9d89c252015-07-15 08:04:37 +08005583 weight = current->se.avg.load_avg;
Ingo Molnar098fb9d2008-03-16 20:36:10 +01005584
Peter Zijlstrac88d5912009-09-10 13:50:02 +02005585 this_load += effective_load(tg, this_cpu, -weight, -weight);
Peter Zijlstra83378262008-06-27 13:41:37 +02005586 load += effective_load(tg, prev_cpu, 0, -weight);
5587 }
5588
5589 tg = task_group(p);
Yuyang Du9d89c252015-07-15 08:04:37 +08005590 weight = p->se.avg.load_avg;
Peter Zijlstra83378262008-06-27 13:41:37 +02005591
Peter Zijlstra71a29aa2009-09-07 18:28:05 +02005592 /*
5593 * In low-load situations, where prev_cpu is idle and this_cpu is idle
Peter Zijlstrac88d5912009-09-10 13:50:02 +02005594 * due to the sync cause above having dropped this_load to 0, we'll
5595 * always have an imbalance, but there's really nothing you can do
5596 * about that, so that's good too.
Peter Zijlstra71a29aa2009-09-07 18:28:05 +02005597 *
5598 * Otherwise check if either cpus are near enough in load to allow this
5599 * task to be woken on this_cpu.
5600 */
Vincent Guittotbd61c982014-08-26 13:06:50 +02005601 this_eff_load = 100;
5602 this_eff_load *= capacity_of(prev_cpu);
Peter Zijlstrae51fd5e2010-05-31 12:37:30 +02005603
Vincent Guittotbd61c982014-08-26 13:06:50 +02005604 prev_eff_load = 100 + (sd->imbalance_pct - 100) / 2;
5605 prev_eff_load *= capacity_of(this_cpu);
5606
5607 if (this_load > 0) {
Peter Zijlstrae51fd5e2010-05-31 12:37:30 +02005608 this_eff_load *= this_load +
5609 effective_load(tg, this_cpu, weight, weight);
5610
Peter Zijlstrae51fd5e2010-05-31 12:37:30 +02005611 prev_eff_load *= load + effective_load(tg, prev_cpu, 0, weight);
Vincent Guittotbd61c982014-08-26 13:06:50 +02005612 }
Peter Zijlstrae51fd5e2010-05-31 12:37:30 +02005613
Vincent Guittotbd61c982014-08-26 13:06:50 +02005614 balanced = this_eff_load <= prev_eff_load;
Mike Galbraithb3137bc2008-05-29 11:11:41 +02005615
Josh Poimboeufae928822016-06-17 12:43:24 -05005616 schedstat_inc(p->se.statistics.nr_wakeups_affine_attempts);
Mike Galbraithb3137bc2008-05-29 11:11:41 +02005617
Vincent Guittot05bfb652014-08-26 13:06:45 +02005618 if (!balanced)
5619 return 0;
Ingo Molnar098fb9d2008-03-16 20:36:10 +01005620
Josh Poimboeufae928822016-06-17 12:43:24 -05005621 schedstat_inc(sd->ttwu_move_affine);
5622 schedstat_inc(p->se.statistics.nr_wakeups_affine);
Vincent Guittot05bfb652014-08-26 13:06:45 +02005623
5624 return 1;
Ingo Molnar098fb9d2008-03-16 20:36:10 +01005625}
5626
Morten Rasmussenb9ac0092015-05-09 19:53:49 +01005627static inline int task_util(struct task_struct *p)
5628{
5629 return p->se.avg.util_avg;
5630}
5631
Patrick Bellasi9b2b8da2016-01-14 18:31:53 +00005632static inline unsigned long boosted_task_util(struct task_struct *task);
5633
Morten Rasmussenb9ac0092015-05-09 19:53:49 +01005634static inline bool __task_fits(struct task_struct *p, int cpu, int util)
5635{
5636 unsigned long capacity = capacity_of(cpu);
5637
Patrick Bellasi9b2b8da2016-01-14 18:31:53 +00005638 util += boosted_task_util(p);
Morten Rasmussenb9ac0092015-05-09 19:53:49 +01005639
5640 return (capacity * 1024) > (util * capacity_margin);
5641}
5642
5643static inline bool task_fits_max(struct task_struct *p, int cpu)
5644{
5645 unsigned long capacity = capacity_of(cpu);
Dietmar Eggemannbbb138b2015-09-26 18:19:54 +01005646 unsigned long max_capacity = cpu_rq(cpu)->rd->max_cpu_capacity.val;
Morten Rasmussenb9ac0092015-05-09 19:53:49 +01005647
5648 if (capacity == max_capacity)
5649 return true;
5650
5651 if (capacity * capacity_margin > max_capacity * 1024)
5652 return true;
5653
5654 return __task_fits(p, cpu, 0);
5655}
5656
Morten Rasmussenb9ac0092015-05-09 19:53:49 +01005657static inline bool task_fits_spare(struct task_struct *p, int cpu)
5658{
5659 return __task_fits(p, cpu, cpu_util(cpu));
5660}
5661
Dietmar Eggemann90f309f2015-01-26 19:47:28 +00005662static bool cpu_overutilized(int cpu)
5663{
5664 return (capacity_of(cpu) * 1024) < (cpu_util(cpu) * capacity_margin);
5665}
5666
Patrick Bellasib08685b2015-06-22 18:32:36 +01005667#ifdef CONFIG_SCHED_TUNE
5668
5669static unsigned long
5670schedtune_margin(unsigned long signal, unsigned long boost)
5671{
5672 unsigned long long margin = 0;
5673
5674 /*
5675 * Signal proportional compensation (SPC)
5676 *
5677 * The Boost (B) value is used to compute a Margin (M) which is
5678 * proportional to the complement of the original Signal (S):
5679 * M = B * (SCHED_LOAD_SCALE - S)
5680 * The obtained M could be used by the caller to "boost" S.
5681 */
5682 margin = SCHED_CAPACITY_SCALE - signal;
5683 margin *= boost;
5684
5685 /*
5686 * Fast integer division by constant:
5687 * Constant : (C) = 100
5688 * Precision : 0.1% (P) = 0.1
5689 * Reference : C * 100 / P (R) = 100000
5690 *
5691 * Thus:
5692 * Shift bits : ceil(log(R,2)) (S) = 17
5693 * Mult const : round(2^S/C) (M) = 1311
5694 *
5695 *
5696 */
5697 margin *= 1311;
5698 margin >>= 17;
5699
5700 return margin;
5701}
5702
Patrick Bellasicaa24e42015-06-26 09:55:06 +01005703static inline unsigned int
Patrick Bellasiedd28d32015-07-07 15:33:20 +01005704schedtune_cpu_margin(unsigned long util, int cpu)
Patrick Bellasicaa24e42015-06-26 09:55:06 +01005705{
Patrick Bellasiedd28d32015-07-07 15:33:20 +01005706 unsigned int boost;
Patrick Bellasicaa24e42015-06-26 09:55:06 +01005707
Patrick Bellasiedd28d32015-07-07 15:33:20 +01005708#ifdef CONFIG_CGROUP_SCHEDTUNE
5709 boost = schedtune_cpu_boost(cpu);
5710#else
5711 boost = get_sysctl_sched_cfs_boost();
5712#endif
Patrick Bellasicaa24e42015-06-26 09:55:06 +01005713 if (boost == 0)
5714 return 0;
5715
5716 return schedtune_margin(util, boost);
5717}
5718
Patrick Bellasi9b2b8da2016-01-14 18:31:53 +00005719static inline unsigned long
5720schedtune_task_margin(struct task_struct *task)
5721{
5722 unsigned int boost;
5723 unsigned long util;
5724 unsigned long margin;
5725
5726#ifdef CONFIG_CGROUP_SCHEDTUNE
5727 boost = schedtune_task_boost(task);
5728#else
5729 boost = get_sysctl_sched_cfs_boost();
5730#endif
5731 if (boost == 0)
5732 return 0;
5733
5734 util = task_util(task);
5735 margin = schedtune_margin(util, boost);
5736
5737 return margin;
5738}
5739
Patrick Bellasicaa24e42015-06-26 09:55:06 +01005740#else /* CONFIG_SCHED_TUNE */
5741
5742static inline unsigned int
Patrick Bellasiedd28d32015-07-07 15:33:20 +01005743schedtune_cpu_margin(unsigned long util, int cpu)
Patrick Bellasicaa24e42015-06-26 09:55:06 +01005744{
5745 return 0;
5746}
5747
Patrick Bellasi9b2b8da2016-01-14 18:31:53 +00005748static inline unsigned int
5749schedtune_task_margin(struct task_struct *task)
5750{
5751 return 0;
5752}
5753
Patrick Bellasib08685b2015-06-22 18:32:36 +01005754#endif /* CONFIG_SCHED_TUNE */
5755
Patrick Bellasicaa24e42015-06-26 09:55:06 +01005756static inline unsigned long
5757boosted_cpu_util(int cpu)
5758{
5759 unsigned long util = cpu_util(cpu);
Patrick Bellasiedd28d32015-07-07 15:33:20 +01005760 unsigned long margin = schedtune_cpu_margin(util, cpu);
Patrick Bellasicaa24e42015-06-26 09:55:06 +01005761
Patrick Bellasicccead12015-06-22 13:51:07 +01005762 trace_sched_boost_cpu(cpu, util, margin);
5763
Patrick Bellasicaa24e42015-06-26 09:55:06 +01005764 return util + margin;
5765}
5766
Patrick Bellasi9b2b8da2016-01-14 18:31:53 +00005767static inline unsigned long
5768boosted_task_util(struct task_struct *task)
5769{
5770 unsigned long util = task_util(task);
5771 unsigned long margin = schedtune_task_margin(task);
5772
Patrick Bellasiecccdb72016-01-14 18:43:37 +00005773 trace_sched_boost_task(task, util, margin);
5774
Patrick Bellasi9b2b8da2016-01-14 18:31:53 +00005775 return util + margin;
5776}
5777
Peter Zijlstraaaee1202009-09-10 13:36:25 +02005778/*
5779 * find_idlest_group finds and returns the least busy CPU group within the
5780 * domain.
5781 */
5782static struct sched_group *
Peter Zijlstra78e7ed52009-09-03 13:16:51 +02005783find_idlest_group(struct sched_domain *sd, struct task_struct *p,
Vincent Guittotc44f2a02013-10-18 13:52:21 +02005784 int this_cpu, int sd_flag)
Gregory Haskinse7693a32008-01-25 21:08:09 +01005785{
Andi Kleenb3bd3de2010-08-10 14:17:51 -07005786 struct sched_group *idlest = NULL, *group = sd->groups;
Morten Rasmussende9b6362015-07-06 15:01:10 +01005787 struct sched_group *fit_group = NULL, *spare_group = NULL;
Peter Zijlstraaaee1202009-09-10 13:36:25 +02005788 unsigned long min_load = ULONG_MAX, this_load = 0;
Morten Rasmussenb9ac0092015-05-09 19:53:49 +01005789 unsigned long fit_capacity = ULONG_MAX;
Morten Rasmussende9b6362015-07-06 15:01:10 +01005790 unsigned long max_spare_capacity = capacity_margin - SCHED_CAPACITY_SCALE;
Vincent Guittotc44f2a02013-10-18 13:52:21 +02005791 int load_idx = sd->forkexec_idx;
Peter Zijlstraaaee1202009-09-10 13:36:25 +02005792 int imbalance = 100 + (sd->imbalance_pct-100)/2;
Gregory Haskinse7693a32008-01-25 21:08:09 +01005793
Vincent Guittotc44f2a02013-10-18 13:52:21 +02005794 if (sd_flag & SD_BALANCE_WAKE)
5795 load_idx = sd->wake_idx;
5796
Peter Zijlstraaaee1202009-09-10 13:36:25 +02005797 do {
Morten Rasmussende9b6362015-07-06 15:01:10 +01005798 unsigned long load, avg_load, spare_capacity;
Peter Zijlstraaaee1202009-09-10 13:36:25 +02005799 int local_group;
5800 int i;
Gregory Haskinse7693a32008-01-25 21:08:09 +01005801
Peter Zijlstraaaee1202009-09-10 13:36:25 +02005802 /* Skip over this group if it has no CPUs allowed */
5803 if (!cpumask_intersects(sched_group_cpus(group),
Peter Zijlstrafa17b502011-06-16 12:23:22 +02005804 tsk_cpus_allowed(p)))
Peter Zijlstraaaee1202009-09-10 13:36:25 +02005805 continue;
5806
5807 local_group = cpumask_test_cpu(this_cpu,
5808 sched_group_cpus(group));
5809
5810 /* Tally up the load of all CPUs in the group */
5811 avg_load = 0;
5812
5813 for_each_cpu(i, sched_group_cpus(group)) {
5814 /* Bias balancing toward cpus of our domain */
5815 if (local_group)
5816 load = source_load(i, load_idx);
5817 else
5818 load = target_load(i, load_idx);
5819
5820 avg_load += load;
Morten Rasmussenb9ac0092015-05-09 19:53:49 +01005821
5822 /*
5823 * Look for most energy-efficient group that can fit
5824 * that can fit the task.
5825 */
5826 if (capacity_of(i) < fit_capacity && task_fits_spare(p, i)) {
5827 fit_capacity = capacity_of(i);
5828 fit_group = group;
5829 }
Morten Rasmussende9b6362015-07-06 15:01:10 +01005830
5831 /*
5832 * Look for group which has most spare capacity on a
5833 * single cpu.
5834 */
5835 spare_capacity = capacity_of(i) - cpu_util(i);
5836 if (spare_capacity > max_spare_capacity) {
5837 max_spare_capacity = spare_capacity;
5838 spare_group = group;
5839 }
Peter Zijlstraaaee1202009-09-10 13:36:25 +02005840 }
5841
Nicolas Pitre63b2ca32014-05-26 18:19:37 -04005842 /* Adjust by relative CPU capacity of the group */
Nicolas Pitreca8ce3d2014-05-26 18:19:39 -04005843 avg_load = (avg_load * SCHED_CAPACITY_SCALE) / group->sgc->capacity;
Peter Zijlstraaaee1202009-09-10 13:36:25 +02005844
5845 if (local_group) {
5846 this_load = avg_load;
Peter Zijlstraaaee1202009-09-10 13:36:25 +02005847 } else if (avg_load < min_load) {
5848 min_load = avg_load;
5849 idlest = group;
5850 }
5851 } while (group = group->next, group != sd->groups);
5852
Morten Rasmussenb9ac0092015-05-09 19:53:49 +01005853 if (fit_group)
5854 return fit_group;
5855
Morten Rasmussende9b6362015-07-06 15:01:10 +01005856 if (spare_group)
5857 return spare_group;
5858
Peter Zijlstraaaee1202009-09-10 13:36:25 +02005859 if (!idlest || 100*this_load < imbalance*min_load)
5860 return NULL;
5861 return idlest;
5862}
5863
5864/*
5865 * find_idlest_cpu - find the idlest cpu among the cpus in group.
5866 */
5867static int
5868find_idlest_cpu(struct sched_group *group, struct task_struct *p, int this_cpu)
5869{
5870 unsigned long load, min_load = ULONG_MAX;
Nicolas Pitre83a0a962014-09-04 11:32:10 -04005871 unsigned int min_exit_latency = UINT_MAX;
5872 u64 latest_idle_timestamp = 0;
5873 int least_loaded_cpu = this_cpu;
5874 int shallowest_idle_cpu = -1;
Peter Zijlstraaaee1202009-09-10 13:36:25 +02005875 int i;
5876
Morten Rasmusseneaecf412016-06-22 18:03:14 +01005877 /* Check if we have any choice: */
5878 if (group->group_weight == 1)
5879 return cpumask_first(sched_group_cpus(group));
5880
Peter Zijlstraaaee1202009-09-10 13:36:25 +02005881 /* Traverse only the allowed CPUs */
Peter Zijlstrafa17b502011-06-16 12:23:22 +02005882 for_each_cpu_and(i, sched_group_cpus(group), tsk_cpus_allowed(p)) {
Morten Rasmussenb9ac0092015-05-09 19:53:49 +01005883 if (task_fits_spare(p, i)) {
Nicolas Pitre83a0a962014-09-04 11:32:10 -04005884 struct rq *rq = cpu_rq(i);
5885 struct cpuidle_state *idle = idle_get_state(rq);
5886 if (idle && idle->exit_latency < min_exit_latency) {
5887 /*
5888 * We give priority to a CPU whose idle state
5889 * has the smallest exit latency irrespective
5890 * of any idle timestamp.
5891 */
5892 min_exit_latency = idle->exit_latency;
5893 latest_idle_timestamp = rq->idle_stamp;
5894 shallowest_idle_cpu = i;
Morten Rasmussenb9ac0092015-05-09 19:53:49 +01005895 } else if (idle_cpu(i) &&
5896 (!idle || idle->exit_latency == min_exit_latency) &&
Nicolas Pitre83a0a962014-09-04 11:32:10 -04005897 rq->idle_stamp > latest_idle_timestamp) {
5898 /*
5899 * If equal or no active idle state, then
5900 * the most recently idled CPU might have
5901 * a warmer cache.
5902 */
5903 latest_idle_timestamp = rq->idle_stamp;
5904 shallowest_idle_cpu = i;
Morten Rasmussenb9ac0092015-05-09 19:53:49 +01005905 } else if (shallowest_idle_cpu == -1) {
5906 /*
5907 * If we haven't found an idle CPU yet
5908 * pick a non-idle one that can fit the task as
5909 * fallback.
5910 */
5911 shallowest_idle_cpu = i;
Nicolas Pitre83a0a962014-09-04 11:32:10 -04005912 }
Yao Dongdong9f967422014-10-28 04:08:06 +00005913 } else if (shallowest_idle_cpu == -1) {
Nicolas Pitre83a0a962014-09-04 11:32:10 -04005914 load = weighted_cpuload(i);
5915 if (load < min_load || (load == min_load && i == this_cpu)) {
5916 min_load = load;
5917 least_loaded_cpu = i;
5918 }
Gregory Haskinse7693a32008-01-25 21:08:09 +01005919 }
5920 }
5921
Nicolas Pitre83a0a962014-09-04 11:32:10 -04005922 return shallowest_idle_cpu != -1 ? shallowest_idle_cpu : least_loaded_cpu;
Peter Zijlstraaaee1202009-09-10 13:36:25 +02005923}
Gregory Haskinse7693a32008-01-25 21:08:09 +01005924
Peter Zijlstraaaee1202009-09-10 13:36:25 +02005925/*
Peter Zijlstra10e2f1a2016-05-09 10:38:05 +02005926 * Implement a for_each_cpu() variant that starts the scan at a given cpu
5927 * (@start), and wraps around.
5928 *
5929 * This is used to scan for idle CPUs; such that not all CPUs looking for an
5930 * idle CPU find the same CPU. The down-side is that tasks tend to cycle
5931 * through the LLC domain.
5932 *
5933 * Especially tbench is found sensitive to this.
Peter Zijlstraa50bde52009-11-12 15:55:28 +01005934 */
Peter Zijlstra10e2f1a2016-05-09 10:38:05 +02005935
5936static int cpumask_next_wrap(int n, const struct cpumask *mask, int start, int *wrapped)
5937{
5938 int next;
5939
5940again:
5941 next = find_next_bit(cpumask_bits(mask), nr_cpumask_bits, n+1);
5942
5943 if (*wrapped) {
5944 if (next >= start)
5945 return nr_cpumask_bits;
5946 } else {
5947 if (next >= nr_cpumask_bits) {
5948 *wrapped = 1;
5949 n = -1;
5950 goto again;
5951 }
5952 }
5953
5954 return next;
5955}
5956
5957#define for_each_cpu_wrap(cpu, mask, start, wrap) \
5958 for ((wrap) = 0, (cpu) = (start)-1; \
5959 (cpu) = cpumask_next_wrap((cpu), (mask), (start), &(wrap)), \
5960 (cpu) < nr_cpumask_bits; )
5961
5962#ifdef CONFIG_SCHED_SMT
5963
5964static inline void set_idle_cores(int cpu, int val)
5965{
5966 struct sched_domain_shared *sds;
5967
5968 sds = rcu_dereference(per_cpu(sd_llc_shared, cpu));
5969 if (sds)
5970 WRITE_ONCE(sds->has_idle_cores, val);
5971}
5972
5973static inline bool test_idle_cores(int cpu, bool def)
5974{
5975 struct sched_domain_shared *sds;
5976
5977 sds = rcu_dereference(per_cpu(sd_llc_shared, cpu));
5978 if (sds)
5979 return READ_ONCE(sds->has_idle_cores);
5980
5981 return def;
5982}
5983
5984/*
5985 * Scans the local SMT mask to see if the entire core is idle, and records this
5986 * information in sd_llc_shared->has_idle_cores.
5987 *
5988 * Since SMT siblings share all cache levels, inspecting this limited remote
5989 * state should be fairly cheap.
5990 */
Peter Zijlstra1b568f02016-05-09 10:38:41 +02005991void __update_idle_core(struct rq *rq)
Peter Zijlstra10e2f1a2016-05-09 10:38:05 +02005992{
5993 int core = cpu_of(rq);
5994 int cpu;
5995
5996 rcu_read_lock();
5997 if (test_idle_cores(core, true))
5998 goto unlock;
5999
6000 for_each_cpu(cpu, cpu_smt_mask(core)) {
6001 if (cpu == core)
6002 continue;
6003
6004 if (!idle_cpu(cpu))
6005 goto unlock;
6006 }
6007
6008 set_idle_cores(core, 1);
6009unlock:
6010 rcu_read_unlock();
6011}
6012
6013/*
6014 * Scan the entire LLC domain for idle cores; this dynamically switches off if
6015 * there are no idle cores left in the system; tracked through
6016 * sd_llc->shared->has_idle_cores and enabled through update_idle_core() above.
6017 */
6018static int select_idle_core(struct task_struct *p, struct sched_domain *sd, int target)
6019{
6020 struct cpumask *cpus = this_cpu_cpumask_var_ptr(select_idle_mask);
6021 int core, cpu, wrap;
6022
Peter Zijlstra1b568f02016-05-09 10:38:41 +02006023 if (!static_branch_likely(&sched_smt_present))
6024 return -1;
6025
Peter Zijlstra10e2f1a2016-05-09 10:38:05 +02006026 if (!test_idle_cores(target, false))
6027 return -1;
6028
6029 cpumask_and(cpus, sched_domain_span(sd), tsk_cpus_allowed(p));
6030
6031 for_each_cpu_wrap(core, cpus, target, wrap) {
6032 bool idle = true;
6033
6034 for_each_cpu(cpu, cpu_smt_mask(core)) {
6035 cpumask_clear_cpu(cpu, cpus);
6036 if (!idle_cpu(cpu))
6037 idle = false;
6038 }
6039
6040 if (idle)
6041 return core;
6042 }
6043
6044 /*
6045 * Failed to find an idle core; stop looking for one.
6046 */
6047 set_idle_cores(target, 0);
6048
6049 return -1;
6050}
6051
6052/*
6053 * Scan the local SMT mask for idle CPUs.
6054 */
6055static int select_idle_smt(struct task_struct *p, struct sched_domain *sd, int target)
6056{
6057 int cpu;
6058
Peter Zijlstra1b568f02016-05-09 10:38:41 +02006059 if (!static_branch_likely(&sched_smt_present))
6060 return -1;
6061
Peter Zijlstra10e2f1a2016-05-09 10:38:05 +02006062 for_each_cpu(cpu, cpu_smt_mask(target)) {
6063 if (!cpumask_test_cpu(cpu, tsk_cpus_allowed(p)))
6064 continue;
6065 if (idle_cpu(cpu))
6066 return cpu;
6067 }
6068
6069 return -1;
6070}
6071
6072#else /* CONFIG_SCHED_SMT */
6073
6074static inline int select_idle_core(struct task_struct *p, struct sched_domain *sd, int target)
6075{
6076 return -1;
6077}
6078
6079static inline int select_idle_smt(struct task_struct *p, struct sched_domain *sd, int target)
6080{
6081 return -1;
6082}
6083
6084#endif /* CONFIG_SCHED_SMT */
6085
6086/*
6087 * Scan the LLC domain for idle CPUs; this is dynamically regulated by
6088 * comparing the average scan cost (tracked in sd->avg_scan_cost) against the
6089 * average idle time for this rq (as found in rq->avg_idle).
6090 */
6091static int select_idle_cpu(struct task_struct *p, struct sched_domain *sd, int target)
6092{
Wanpeng Li9cfb38a2016-10-09 08:04:03 +08006093 struct sched_domain *this_sd;
6094 u64 avg_cost, avg_idle = this_rq()->avg_idle;
Peter Zijlstra10e2f1a2016-05-09 10:38:05 +02006095 u64 time, cost;
6096 s64 delta;
6097 int cpu, wrap;
6098
Wanpeng Li9cfb38a2016-10-09 08:04:03 +08006099 this_sd = rcu_dereference(*this_cpu_ptr(&sd_llc));
6100 if (!this_sd)
6101 return -1;
6102
6103 avg_cost = this_sd->avg_scan_cost;
6104
Peter Zijlstra10e2f1a2016-05-09 10:38:05 +02006105 /*
6106 * Due to large variance we need a large fuzz factor; hackbench in
6107 * particularly is sensitive here.
6108 */
6109 if ((avg_idle / 512) < avg_cost)
6110 return -1;
6111
6112 time = local_clock();
6113
6114 for_each_cpu_wrap(cpu, sched_domain_span(sd), target, wrap) {
6115 if (!cpumask_test_cpu(cpu, tsk_cpus_allowed(p)))
6116 continue;
6117 if (idle_cpu(cpu))
6118 break;
6119 }
6120
6121 time = local_clock() - time;
6122 cost = this_sd->avg_scan_cost;
6123 delta = (s64)(time - cost) / 8;
6124 this_sd->avg_scan_cost += delta;
6125
6126 return cpu;
6127}
6128
6129/*
6130 * Try and locate an idle core/thread in the LLC cache domain.
Peter Zijlstraa50bde52009-11-12 15:55:28 +01006131 */
Morten Rasmussen772bd008c2016-06-22 18:03:13 +01006132static int select_idle_sibling(struct task_struct *p, int prev, int target)
Peter Zijlstraa50bde52009-11-12 15:55:28 +01006133{
Suresh Siddha99bd5e22010-03-31 16:47:45 -07006134 struct sched_domain *sd;
Peter Zijlstra10e2f1a2016-05-09 10:38:05 +02006135 int i;
Mike Galbraithe0a79f52013-01-28 12:19:25 +01006136
6137 if (idle_cpu(target))
6138 return target;
Peter Zijlstraa50bde52009-11-12 15:55:28 +01006139
6140 /*
Peter Zijlstra10e2f1a2016-05-09 10:38:05 +02006141 * If the previous cpu is cache affine and idle, don't be stupid.
Peter Zijlstraa50bde52009-11-12 15:55:28 +01006142 */
Morten Rasmussen772bd008c2016-06-22 18:03:13 +01006143 if (prev != target && cpus_share_cache(prev, target) && idle_cpu(prev))
6144 return prev;
Peter Zijlstraa50bde52009-11-12 15:55:28 +01006145
Peter Zijlstra518cd622011-12-07 15:07:31 +01006146 sd = rcu_dereference(per_cpu(sd_llc, target));
Peter Zijlstra10e2f1a2016-05-09 10:38:05 +02006147 if (!sd)
6148 return target;
Morten Rasmussen772bd008c2016-06-22 18:03:13 +01006149
Peter Zijlstra10e2f1a2016-05-09 10:38:05 +02006150 i = select_idle_core(p, sd, target);
6151 if ((unsigned)i < nr_cpumask_bits)
Gregory Haskinse7693a32008-01-25 21:08:09 +01006152 return i;
Ingo Molnar098fb9d2008-03-16 20:36:10 +01006153
Peter Zijlstra10e2f1a2016-05-09 10:38:05 +02006154 i = select_idle_cpu(p, sd, target);
6155 if ((unsigned)i < nr_cpumask_bits)
6156 return i;
Mike Galbraith970e1782012-06-12 05:18:32 +02006157
Peter Zijlstra10e2f1a2016-05-09 10:38:05 +02006158 i = select_idle_smt(p, sd, target);
6159 if ((unsigned)i < nr_cpumask_bits)
6160 return i;
Linus Torvalds37407ea2012-09-16 12:29:43 -07006161
Peter Zijlstraa50bde52009-11-12 15:55:28 +01006162 return target;
6163}
Dietmar Eggemann231678b2015-08-14 17:23:13 +01006164
Morten Rasmussen4017a8e2015-05-09 20:03:19 +01006165static int energy_aware_wake_cpu(struct task_struct *p, int target)
6166{
6167 struct sched_domain *sd;
6168 struct sched_group *sg, *sg_target;
6169 int target_max_cap = INT_MAX;
6170 int target_cpu = task_cpu(p);
6171 int i;
6172
6173 sd = rcu_dereference(per_cpu(sd_ea, task_cpu(p)));
6174
6175 if (!sd)
6176 return target;
6177
6178 sg = sd->groups;
6179 sg_target = sg;
6180
6181 /*
6182 * Find group with sufficient capacity. We only get here if no cpu is
6183 * overutilized. We may end up overutilizing a cpu by adding the task,
6184 * but that should not be any worse than select_idle_sibling().
6185 * load_balance() should sort it out later as we get above the tipping
6186 * point.
6187 */
6188 do {
6189 /* Assuming all cpus are the same in group */
6190 int max_cap_cpu = group_first_cpu(sg);
6191
6192 /*
6193 * Assume smaller max capacity means more energy-efficient.
6194 * Ideally we should query the energy model for the right
6195 * answer but it easily ends up in an exhaustive search.
6196 */
6197 if (capacity_of(max_cap_cpu) < target_max_cap &&
6198 task_fits_max(p, max_cap_cpu)) {
6199 sg_target = sg;
6200 target_max_cap = capacity_of(max_cap_cpu);
6201 }
6202 } while (sg = sg->next, sg != sd->groups);
6203
6204 /* Find cpu with sufficient capacity */
6205 for_each_cpu_and(i, tsk_cpus_allowed(p), sched_group_cpus(sg_target)) {
6206 /*
6207 * p's blocked utilization is still accounted for on prev_cpu
6208 * so prev_cpu will receive a negative bias due to the double
6209 * accounting. However, the blocked utilization may be zero.
6210 */
Patrick Bellasi9b2b8da2016-01-14 18:31:53 +00006211 int new_util = cpu_util(i) + boosted_task_util(p);
Morten Rasmussen4017a8e2015-05-09 20:03:19 +01006212
6213 if (new_util > capacity_orig_of(i))
6214 continue;
6215
6216 if (new_util < capacity_curr_of(i)) {
6217 target_cpu = i;
6218 if (cpu_rq(i)->nr_running)
6219 break;
6220 }
6221
6222 /* cpu has capacity at higher OPP, keep it as fallback */
6223 if (target_cpu == task_cpu(p))
6224 target_cpu = i;
6225 }
6226
6227 if (target_cpu != task_cpu(p)) {
6228 struct energy_env eenv = {
6229 .util_delta = task_util(p),
6230 .src_cpu = task_cpu(p),
6231 .dst_cpu = target_cpu,
Patrick Bellasi88d06052016-01-15 15:48:03 +00006232 .task = p,
Morten Rasmussen4017a8e2015-05-09 20:03:19 +01006233 };
6234
6235 /* Not enough spare capacity on previous cpu */
6236 if (cpu_overutilized(task_cpu(p)))
6237 return target_cpu;
6238
6239 if (energy_diff(&eenv) >= 0)
6240 return task_cpu(p);
6241 }
6242
6243 return target_cpu;
6244}
6245
Vincent Guittot8bb5b002015-03-04 08:48:47 +01006246/*
Morten Rasmussende91b9c2014-02-18 14:14:24 +00006247 * select_task_rq_fair: Select target runqueue for the waking task in domains
6248 * that have the 'sd_flag' flag set. In practice, this is SD_BALANCE_WAKE,
6249 * SD_BALANCE_FORK, or SD_BALANCE_EXEC.
Peter Zijlstraaaee1202009-09-10 13:36:25 +02006250 *
Morten Rasmussende91b9c2014-02-18 14:14:24 +00006251 * Balances load by selecting the idlest cpu in the idlest group, or under
6252 * certain conditions an idle sibling cpu if the domain has SD_WAKE_AFFINE set.
Peter Zijlstraaaee1202009-09-10 13:36:25 +02006253 *
Morten Rasmussende91b9c2014-02-18 14:14:24 +00006254 * Returns the target cpu number.
Peter Zijlstraaaee1202009-09-10 13:36:25 +02006255 *
6256 * preempt must be disabled.
6257 */
Peter Zijlstra0017d732010-03-24 18:34:10 +01006258static int
Peter Zijlstraac66f542013-10-07 11:29:16 +01006259select_task_rq_fair(struct task_struct *p, int prev_cpu, int sd_flag, int wake_flags)
Peter Zijlstraaaee1202009-09-10 13:36:25 +02006260{
Peter Zijlstra29cd8ba2009-09-17 09:01:14 +02006261 struct sched_domain *tmp, *affine_sd = NULL, *sd = NULL;
Peter Zijlstrac88d5912009-09-10 13:50:02 +02006262 int cpu = smp_processor_id();
Mike Galbraith63b0e9e2015-07-14 17:39:50 +02006263 int new_cpu = prev_cpu;
Suresh Siddha99bd5e22010-03-31 16:47:45 -07006264 int want_affine = 0;
Peter Zijlstra5158f4e2009-09-16 13:46:59 +02006265 int sync = wake_flags & WF_SYNC;
Gregory Haskinse7693a32008-01-25 21:08:09 +01006266
Peter Zijlstrac58d25f2016-05-12 09:19:59 +02006267 if (sd_flag & SD_BALANCE_WAKE) {
6268 record_wakee(p);
Morten Rasmussen4017a8e2015-05-09 20:03:19 +01006269 want_affine = (!wake_wide(p) && task_fits_max(p, cpu) &&
6270 cpumask_test_cpu(cpu, tsk_cpus_allowed(p))) ||
6271 energy_aware();
Peter Zijlstrac58d25f2016-05-12 09:19:59 +02006272 }
Gregory Haskinse7693a32008-01-25 21:08:09 +01006273
Peter Zijlstradce840a2011-04-07 14:09:50 +02006274 rcu_read_lock();
Peter Zijlstraaaee1202009-09-10 13:36:25 +02006275 for_each_domain(cpu, tmp) {
Peter Zijlstrae4f428882009-12-16 18:04:34 +01006276 if (!(tmp->flags & SD_LOAD_BALANCE))
Mike Galbraith63b0e9e2015-07-14 17:39:50 +02006277 break;
Peter Zijlstrae4f428882009-12-16 18:04:34 +01006278
Peter Zijlstraaaee1202009-09-10 13:36:25 +02006279 /*
Suresh Siddha99bd5e22010-03-31 16:47:45 -07006280 * If both cpu and prev_cpu are part of this domain,
6281 * cpu is a valid SD_WAKE_AFFINE target.
Peter Zijlstrafe3bcfe2009-11-12 15:55:29 +01006282 */
Suresh Siddha99bd5e22010-03-31 16:47:45 -07006283 if (want_affine && (tmp->flags & SD_WAKE_AFFINE) &&
6284 cpumask_test_cpu(prev_cpu, sched_domain_span(tmp))) {
6285 affine_sd = tmp;
Alex Shif03542a2012-07-26 08:55:34 +08006286 break;
Peter Zijlstrac88d5912009-09-10 13:50:02 +02006287 }
6288
Alex Shif03542a2012-07-26 08:55:34 +08006289 if (tmp->flags & sd_flag)
Peter Zijlstra29cd8ba2009-09-17 09:01:14 +02006290 sd = tmp;
Mike Galbraith63b0e9e2015-07-14 17:39:50 +02006291 else if (!want_affine)
6292 break;
Peter Zijlstrac88d5912009-09-10 13:50:02 +02006293 }
Peter Zijlstraaaee1202009-09-10 13:36:25 +02006294
Mike Galbraith63b0e9e2015-07-14 17:39:50 +02006295 if (affine_sd) {
6296 sd = NULL; /* Prefer wake_affine over balance flags */
Morten Rasmussen772bd008c2016-06-22 18:03:13 +01006297 if (cpu != prev_cpu && wake_affine(affine_sd, p, prev_cpu, sync))
Mike Galbraith63b0e9e2015-07-14 17:39:50 +02006298 new_cpu = cpu;
Mike Galbraith8b911ac2010-03-11 17:17:16 +01006299 }
Peter Zijlstra3b640892009-09-16 13:44:33 +02006300
Mike Galbraith63b0e9e2015-07-14 17:39:50 +02006301 if (!sd) {
Morten Rasmussen4017a8e2015-05-09 20:03:19 +01006302 if (energy_aware() && !cpu_rq(cpu)->rd->overutilized)
6303 new_cpu = energy_aware_wake_cpu(p, prev_cpu);
6304 else if (sd_flag & SD_BALANCE_WAKE) /* XXX always ? */
Morten Rasmussen772bd008c2016-06-22 18:03:13 +01006305 new_cpu = select_idle_sibling(p, prev_cpu, new_cpu);
Mike Galbraith63b0e9e2015-07-14 17:39:50 +02006306
6307 } else while (sd) {
Peter Zijlstraaaee1202009-09-10 13:36:25 +02006308 struct sched_group *group;
Peter Zijlstrac88d5912009-09-10 13:50:02 +02006309 int weight;
Peter Zijlstraaaee1202009-09-10 13:36:25 +02006310
Peter Zijlstra0763a662009-09-14 19:37:39 +02006311 if (!(sd->flags & sd_flag)) {
Peter Zijlstraaaee1202009-09-10 13:36:25 +02006312 sd = sd->child;
6313 continue;
6314 }
6315
Vincent Guittotc44f2a02013-10-18 13:52:21 +02006316 group = find_idlest_group(sd, p, cpu, sd_flag);
Peter Zijlstraaaee1202009-09-10 13:36:25 +02006317 if (!group) {
6318 sd = sd->child;
6319 continue;
6320 }
6321
Peter Zijlstrad7c33c42009-09-11 12:45:38 +02006322 new_cpu = find_idlest_cpu(group, p, cpu);
Peter Zijlstraaaee1202009-09-10 13:36:25 +02006323 if (new_cpu == -1 || new_cpu == cpu) {
6324 /* Now try balancing at a lower domain level of cpu */
6325 sd = sd->child;
6326 continue;
6327 }
6328
6329 /* Now try balancing at a lower domain level of new_cpu */
6330 cpu = new_cpu;
Peter Zijlstra669c55e2010-04-16 14:59:29 +02006331 weight = sd->span_weight;
Peter Zijlstraaaee1202009-09-10 13:36:25 +02006332 sd = NULL;
6333 for_each_domain(cpu, tmp) {
Peter Zijlstra669c55e2010-04-16 14:59:29 +02006334 if (weight <= tmp->span_weight)
Peter Zijlstraaaee1202009-09-10 13:36:25 +02006335 break;
Peter Zijlstra0763a662009-09-14 19:37:39 +02006336 if (tmp->flags & sd_flag)
Peter Zijlstraaaee1202009-09-10 13:36:25 +02006337 sd = tmp;
6338 }
6339 /* while loop will break here if sd == NULL */
Gregory Haskinse7693a32008-01-25 21:08:09 +01006340 }
Peter Zijlstradce840a2011-04-07 14:09:50 +02006341 rcu_read_unlock();
Gregory Haskinse7693a32008-01-25 21:08:09 +01006342
Peter Zijlstrac88d5912009-09-10 13:50:02 +02006343 return new_cpu;
Gregory Haskinse7693a32008-01-25 21:08:09 +01006344}
Paul Turner0a74bef2012-10-04 13:18:30 +02006345
6346/*
6347 * Called immediately before a task is migrated to a new cpu; task_cpu(p) and
6348 * cfs_rq_of(p) references at time of call are still valid and identify the
Byungchul Park525628c2015-11-18 09:34:59 +09006349 * previous cpu. The caller guarantees p->pi_lock or task_rq(p)->lock is held.
Paul Turner0a74bef2012-10-04 13:18:30 +02006350 */
xiaofeng.yan5a4fd032015-09-23 14:55:59 +08006351static void migrate_task_rq_fair(struct task_struct *p)
Paul Turner0a74bef2012-10-04 13:18:30 +02006352{
Paul Turneraff3e492012-10-04 13:18:30 +02006353 /*
Peter Zijlstra59efa0b2016-05-10 18:24:37 +02006354 * As blocked tasks retain absolute vruntime the migration needs to
6355 * deal with this by subtracting the old and adding the new
6356 * min_vruntime -- the latter is done by enqueue_entity() when placing
6357 * the task on the new runqueue.
6358 */
6359 if (p->state == TASK_WAKING) {
6360 struct sched_entity *se = &p->se;
6361 struct cfs_rq *cfs_rq = cfs_rq_of(se);
6362 u64 min_vruntime;
6363
6364#ifndef CONFIG_64BIT
6365 u64 min_vruntime_copy;
6366
6367 do {
6368 min_vruntime_copy = cfs_rq->min_vruntime_copy;
6369 smp_rmb();
6370 min_vruntime = cfs_rq->min_vruntime;
6371 } while (min_vruntime != min_vruntime_copy);
6372#else
6373 min_vruntime = cfs_rq->min_vruntime;
6374#endif
6375
6376 se->vruntime -= min_vruntime;
6377 }
6378
6379 /*
Yuyang Du9d89c252015-07-15 08:04:37 +08006380 * We are supposed to update the task to "current" time, then its up to date
6381 * and ready to go to new CPU/cfs_rq. But we have difficulty in getting
6382 * what current time is, so simply throw away the out-of-date time. This
6383 * will result in the wakee task is less decayed, but giving the wakee more
6384 * load sounds not bad.
Paul Turneraff3e492012-10-04 13:18:30 +02006385 */
Yuyang Du9d89c252015-07-15 08:04:37 +08006386 remove_entity_load_avg(&p->se);
6387
6388 /* Tell new CPU we are migrated */
6389 p->se.avg.last_update_time = 0;
Ben Segall3944a922014-05-15 15:59:20 -07006390
6391 /* We have migrated, no longer consider this task hot */
Yuyang Du9d89c252015-07-15 08:04:37 +08006392 p->se.exec_start = 0;
Paul Turner0a74bef2012-10-04 13:18:30 +02006393}
Yuyang Du12695572015-07-15 08:04:40 +08006394
6395static void task_dead_fair(struct task_struct *p)
6396{
6397 remove_entity_load_avg(&p->se);
6398}
Patrick Bellasi2178e842016-07-22 11:35:59 +01006399#else
6400#define task_fits_max(p, cpu) true
Gregory Haskinse7693a32008-01-25 21:08:09 +01006401#endif /* CONFIG_SMP */
6402
Peter Zijlstrae52fb7c2009-01-14 12:39:19 +01006403static unsigned long
6404wakeup_gran(struct sched_entity *curr, struct sched_entity *se)
Peter Zijlstra0bbd3332008-04-19 19:44:57 +02006405{
6406 unsigned long gran = sysctl_sched_wakeup_granularity;
6407
6408 /*
Peter Zijlstrae52fb7c2009-01-14 12:39:19 +01006409 * Since its curr running now, convert the gran from real-time
6410 * to virtual-time in his units.
Mike Galbraith13814d42010-03-11 17:17:04 +01006411 *
6412 * By using 'se' instead of 'curr' we penalize light tasks, so
6413 * they get preempted easier. That is, if 'se' < 'curr' then
6414 * the resulting gran will be larger, therefore penalizing the
6415 * lighter, if otoh 'se' > 'curr' then the resulting gran will
6416 * be smaller, again penalizing the lighter task.
6417 *
6418 * This is especially important for buddies when the leftmost
6419 * task is higher priority than the buddy.
Peter Zijlstra0bbd3332008-04-19 19:44:57 +02006420 */
Shaohua Lif4ad9bd2011-04-08 12:53:09 +08006421 return calc_delta_fair(gran, se);
Peter Zijlstra0bbd3332008-04-19 19:44:57 +02006422}
6423
6424/*
Peter Zijlstra464b7522008-10-24 11:06:15 +02006425 * Should 'se' preempt 'curr'.
6426 *
6427 * |s1
6428 * |s2
6429 * |s3
6430 * g
6431 * |<--->|c
6432 *
6433 * w(c, s1) = -1
6434 * w(c, s2) = 0
6435 * w(c, s3) = 1
6436 *
6437 */
6438static int
6439wakeup_preempt_entity(struct sched_entity *curr, struct sched_entity *se)
6440{
6441 s64 gran, vdiff = curr->vruntime - se->vruntime;
6442
6443 if (vdiff <= 0)
6444 return -1;
6445
Peter Zijlstrae52fb7c2009-01-14 12:39:19 +01006446 gran = wakeup_gran(curr, se);
Peter Zijlstra464b7522008-10-24 11:06:15 +02006447 if (vdiff > gran)
6448 return 1;
6449
6450 return 0;
6451}
6452
Peter Zijlstra02479092008-11-04 21:25:10 +01006453static void set_last_buddy(struct sched_entity *se)
6454{
Venkatesh Pallipadi69c80f32011-04-13 18:21:09 -07006455 if (entity_is_task(se) && unlikely(task_of(se)->policy == SCHED_IDLE))
6456 return;
6457
6458 for_each_sched_entity(se)
6459 cfs_rq_of(se)->last = se;
Peter Zijlstra02479092008-11-04 21:25:10 +01006460}
6461
6462static void set_next_buddy(struct sched_entity *se)
6463{
Venkatesh Pallipadi69c80f32011-04-13 18:21:09 -07006464 if (entity_is_task(se) && unlikely(task_of(se)->policy == SCHED_IDLE))
6465 return;
6466
6467 for_each_sched_entity(se)
6468 cfs_rq_of(se)->next = se;
Peter Zijlstra02479092008-11-04 21:25:10 +01006469}
6470
Rik van Rielac53db52011-02-01 09:51:03 -05006471static void set_skip_buddy(struct sched_entity *se)
6472{
Venkatesh Pallipadi69c80f32011-04-13 18:21:09 -07006473 for_each_sched_entity(se)
6474 cfs_rq_of(se)->skip = se;
Rik van Rielac53db52011-02-01 09:51:03 -05006475}
6476
Peter Zijlstra464b7522008-10-24 11:06:15 +02006477/*
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02006478 * Preempt the current task with a newly woken task if needed:
6479 */
Peter Zijlstra5a9b86f2009-09-16 13:47:58 +02006480static void check_preempt_wakeup(struct rq *rq, struct task_struct *p, int wake_flags)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02006481{
6482 struct task_struct *curr = rq->curr;
Srivatsa Vaddagiri8651a862007-10-15 17:00:12 +02006483 struct sched_entity *se = &curr->se, *pse = &p->se;
Mike Galbraith03e89e42008-12-16 08:45:30 +01006484 struct cfs_rq *cfs_rq = task_cfs_rq(curr);
Mike Galbraithf685cea2009-10-23 23:09:22 +02006485 int scale = cfs_rq->nr_running >= sched_nr_latency;
Venkatesh Pallipadi2f368252011-04-14 10:30:53 -07006486 int next_buddy_marked = 0;
Mike Galbraith03e89e42008-12-16 08:45:30 +01006487
Ingo Molnar4ae7d5c2008-03-19 01:42:00 +01006488 if (unlikely(se == pse))
6489 return;
6490
Paul Turner5238cdd2011-07-21 09:43:37 -07006491 /*
Kirill Tkhai163122b2014-08-20 13:48:29 +04006492 * This is possible from callers such as attach_tasks(), in which we
Paul Turner5238cdd2011-07-21 09:43:37 -07006493 * unconditionally check_prempt_curr() after an enqueue (which may have
6494 * lead to a throttle). This both saves work and prevents false
6495 * next-buddy nomination below.
6496 */
6497 if (unlikely(throttled_hierarchy(cfs_rq_of(pse))))
6498 return;
6499
Venkatesh Pallipadi2f368252011-04-14 10:30:53 -07006500 if (sched_feat(NEXT_BUDDY) && scale && !(wake_flags & WF_FORK)) {
Mike Galbraith3cb63d52009-09-11 12:01:17 +02006501 set_next_buddy(pse);
Venkatesh Pallipadi2f368252011-04-14 10:30:53 -07006502 next_buddy_marked = 1;
6503 }
Peter Zijlstra57fdc262008-09-23 15:33:45 +02006504
Bharata B Raoaec0a5142008-08-28 14:42:49 +05306505 /*
6506 * We can come here with TIF_NEED_RESCHED already set from new task
6507 * wake up path.
Paul Turner5238cdd2011-07-21 09:43:37 -07006508 *
6509 * Note: this also catches the edge-case of curr being in a throttled
6510 * group (e.g. via set_curr_task), since update_curr() (in the
6511 * enqueue of curr) will have resulted in resched being set. This
6512 * prevents us from potentially nominating it as a false LAST_BUDDY
6513 * below.
Bharata B Raoaec0a5142008-08-28 14:42:49 +05306514 */
6515 if (test_tsk_need_resched(curr))
6516 return;
6517
Darren Harta2f5c9a2011-02-22 13:04:33 -08006518 /* Idle tasks are by definition preempted by non-idle tasks. */
6519 if (unlikely(curr->policy == SCHED_IDLE) &&
6520 likely(p->policy != SCHED_IDLE))
6521 goto preempt;
6522
Ingo Molnar91c234b2007-10-15 17:00:18 +02006523 /*
Darren Harta2f5c9a2011-02-22 13:04:33 -08006524 * Batch and idle tasks do not preempt non-idle tasks (their preemption
6525 * is driven by the tick):
Ingo Molnar91c234b2007-10-15 17:00:18 +02006526 */
Ingo Molnar8ed92e52012-10-14 14:28:50 +02006527 if (unlikely(p->policy != SCHED_NORMAL) || !sched_feat(WAKEUP_PREEMPTION))
Ingo Molnar91c234b2007-10-15 17:00:18 +02006528 return;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02006529
Peter Zijlstra3a7e73a2009-11-28 18:51:02 +01006530 find_matching_se(&se, &pse);
Paul Turner9bbd7372011-07-05 19:07:21 -07006531 update_curr(cfs_rq_of(se));
Peter Zijlstra3a7e73a2009-11-28 18:51:02 +01006532 BUG_ON(!pse);
Venkatesh Pallipadi2f368252011-04-14 10:30:53 -07006533 if (wakeup_preempt_entity(se, pse) == 1) {
6534 /*
6535 * Bias pick_next to pick the sched entity that is
6536 * triggering this preemption.
6537 */
6538 if (!next_buddy_marked)
6539 set_next_buddy(pse);
Peter Zijlstra3a7e73a2009-11-28 18:51:02 +01006540 goto preempt;
Venkatesh Pallipadi2f368252011-04-14 10:30:53 -07006541 }
Jupyung Leea65ac742009-11-17 18:51:40 +09006542
Peter Zijlstra3a7e73a2009-11-28 18:51:02 +01006543 return;
6544
6545preempt:
Kirill Tkhai88751252014-06-29 00:03:57 +04006546 resched_curr(rq);
Peter Zijlstra3a7e73a2009-11-28 18:51:02 +01006547 /*
6548 * Only set the backward buddy when the current task is still
6549 * on the rq. This can happen when a wakeup gets interleaved
6550 * with schedule on the ->pre_schedule() or idle_balance()
6551 * point, either of which can * drop the rq lock.
6552 *
6553 * Also, during early boot the idle thread is in the fair class,
6554 * for obvious reasons its a bad idea to schedule back to it.
6555 */
6556 if (unlikely(!se->on_rq || curr == rq->idle))
6557 return;
6558
6559 if (sched_feat(LAST_BUDDY) && scale && entity_is_task(se))
6560 set_last_buddy(se);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02006561}
6562
Peter Zijlstra606dba22012-02-11 06:05:00 +01006563static struct task_struct *
Peter Zijlstrae7904a22015-08-01 19:25:08 +02006564pick_next_task_fair(struct rq *rq, struct task_struct *prev, struct pin_cookie cookie)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02006565{
6566 struct cfs_rq *cfs_rq = &rq->cfs;
6567 struct sched_entity *se;
Peter Zijlstra678d5712012-02-11 06:05:00 +01006568 struct task_struct *p;
Peter Zijlstra37e117c2014-02-14 12:25:08 +01006569 int new_tasks;
Peter Zijlstra678d5712012-02-11 06:05:00 +01006570
Peter Zijlstra6e831252014-02-11 16:11:48 +01006571again:
Peter Zijlstra678d5712012-02-11 06:05:00 +01006572#ifdef CONFIG_FAIR_GROUP_SCHED
6573 if (!cfs_rq->nr_running)
Peter Zijlstra38033c32014-01-23 20:32:21 +01006574 goto idle;
Peter Zijlstra678d5712012-02-11 06:05:00 +01006575
Peter Zijlstra3f1d2a32014-02-12 10:49:30 +01006576 if (prev->sched_class != &fair_sched_class)
Peter Zijlstra678d5712012-02-11 06:05:00 +01006577 goto simple;
6578
6579 /*
6580 * Because of the set_next_buddy() in dequeue_task_fair() it is rather
6581 * likely that a next task is from the same cgroup as the current.
6582 *
6583 * Therefore attempt to avoid putting and setting the entire cgroup
6584 * hierarchy, only change the part that actually changes.
6585 */
6586
6587 do {
6588 struct sched_entity *curr = cfs_rq->curr;
6589
6590 /*
6591 * Since we got here without doing put_prev_entity() we also
6592 * have to consider cfs_rq->curr. If it is still a runnable
6593 * entity, update_curr() will update its vruntime, otherwise
6594 * forget we've ever seen it.
6595 */
Ben Segall54d27362015-04-06 15:28:10 -07006596 if (curr) {
6597 if (curr->on_rq)
6598 update_curr(cfs_rq);
6599 else
6600 curr = NULL;
Peter Zijlstra678d5712012-02-11 06:05:00 +01006601
Ben Segall54d27362015-04-06 15:28:10 -07006602 /*
6603 * This call to check_cfs_rq_runtime() will do the
6604 * throttle and dequeue its entity in the parent(s).
6605 * Therefore the 'simple' nr_running test will indeed
6606 * be correct.
6607 */
6608 if (unlikely(check_cfs_rq_runtime(cfs_rq)))
6609 goto simple;
6610 }
Peter Zijlstra678d5712012-02-11 06:05:00 +01006611
6612 se = pick_next_entity(cfs_rq, curr);
6613 cfs_rq = group_cfs_rq(se);
6614 } while (cfs_rq);
6615
6616 p = task_of(se);
6617
6618 /*
6619 * Since we haven't yet done put_prev_entity and if the selected task
6620 * is a different task than we started out with, try and touch the
6621 * least amount of cfs_rqs.
6622 */
6623 if (prev != p) {
6624 struct sched_entity *pse = &prev->se;
6625
6626 while (!(cfs_rq = is_same_group(se, pse))) {
6627 int se_depth = se->depth;
6628 int pse_depth = pse->depth;
6629
6630 if (se_depth <= pse_depth) {
6631 put_prev_entity(cfs_rq_of(pse), pse);
6632 pse = parent_entity(pse);
6633 }
6634 if (se_depth >= pse_depth) {
6635 set_next_entity(cfs_rq_of(se), se);
6636 se = parent_entity(se);
6637 }
6638 }
6639
6640 put_prev_entity(cfs_rq, pse);
6641 set_next_entity(cfs_rq, se);
6642 }
6643
6644 if (hrtick_enabled(rq))
6645 hrtick_start_fair(rq, p);
6646
Morten Rasmussen4c6a8242016-02-25 12:47:54 +00006647 rq->misfit_task = !task_fits_max(p, rq->cpu);
6648
Peter Zijlstra678d5712012-02-11 06:05:00 +01006649 return p;
6650simple:
6651 cfs_rq = &rq->cfs;
6652#endif
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02006653
Tim Blechmann36ace272009-11-24 11:55:45 +01006654 if (!cfs_rq->nr_running)
Peter Zijlstra38033c32014-01-23 20:32:21 +01006655 goto idle;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02006656
Peter Zijlstra3f1d2a32014-02-12 10:49:30 +01006657 put_prev_task(rq, prev);
Peter Zijlstra606dba22012-02-11 06:05:00 +01006658
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02006659 do {
Peter Zijlstra678d5712012-02-11 06:05:00 +01006660 se = pick_next_entity(cfs_rq, NULL);
Peter Zijlstraf4b67552008-11-04 21:25:07 +01006661 set_next_entity(cfs_rq, se);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02006662 cfs_rq = group_cfs_rq(se);
6663 } while (cfs_rq);
6664
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01006665 p = task_of(se);
Peter Zijlstra678d5712012-02-11 06:05:00 +01006666
Mike Galbraithb39e66e2011-11-22 15:20:07 +01006667 if (hrtick_enabled(rq))
6668 hrtick_start_fair(rq, p);
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01006669
Morten Rasmussen4c6a8242016-02-25 12:47:54 +00006670 rq->misfit_task = !task_fits_max(p, rq->cpu);
6671
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01006672 return p;
Peter Zijlstra38033c32014-01-23 20:32:21 +01006673
6674idle:
Morten Rasmussen4c6a8242016-02-25 12:47:54 +00006675 rq->misfit_task = 0;
Peter Zijlstracbce1a62015-06-11 14:46:54 +02006676 /*
6677 * This is OK, because current is on_cpu, which avoids it being picked
6678 * for load-balance and preemption/IRQs are still disabled avoiding
6679 * further scheduler activity on it and we're being very careful to
6680 * re-start the picking loop.
6681 */
Peter Zijlstrae7904a22015-08-01 19:25:08 +02006682 lockdep_unpin_lock(&rq->lock, cookie);
Kirill Tkhaie4aa3582014-03-06 13:31:55 +04006683 new_tasks = idle_balance(rq);
Peter Zijlstrae7904a22015-08-01 19:25:08 +02006684 lockdep_repin_lock(&rq->lock, cookie);
Peter Zijlstra37e117c2014-02-14 12:25:08 +01006685 /*
6686 * Because idle_balance() releases (and re-acquires) rq->lock, it is
6687 * possible for any higher priority task to appear. In that case we
6688 * must re-start the pick_next_entity() loop.
6689 */
Kirill Tkhaie4aa3582014-03-06 13:31:55 +04006690 if (new_tasks < 0)
Peter Zijlstra37e117c2014-02-14 12:25:08 +01006691 return RETRY_TASK;
6692
Kirill Tkhaie4aa3582014-03-06 13:31:55 +04006693 if (new_tasks > 0)
Peter Zijlstra38033c32014-01-23 20:32:21 +01006694 goto again;
Peter Zijlstra38033c32014-01-23 20:32:21 +01006695
6696 return NULL;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02006697}
6698
6699/*
6700 * Account for a descheduled task:
6701 */
Ingo Molnar31ee5292007-08-09 11:16:49 +02006702static void put_prev_task_fair(struct rq *rq, struct task_struct *prev)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02006703{
6704 struct sched_entity *se = &prev->se;
6705 struct cfs_rq *cfs_rq;
6706
6707 for_each_sched_entity(se) {
6708 cfs_rq = cfs_rq_of(se);
Ingo Molnarab6cde22007-08-09 11:16:48 +02006709 put_prev_entity(cfs_rq, se);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02006710 }
6711}
6712
Rik van Rielac53db52011-02-01 09:51:03 -05006713/*
6714 * sched_yield() is very simple
6715 *
6716 * The magic of dealing with the ->skip buddy is in pick_next_entity.
6717 */
6718static void yield_task_fair(struct rq *rq)
6719{
6720 struct task_struct *curr = rq->curr;
6721 struct cfs_rq *cfs_rq = task_cfs_rq(curr);
6722 struct sched_entity *se = &curr->se;
6723
6724 /*
6725 * Are we the only task in the tree?
6726 */
6727 if (unlikely(rq->nr_running == 1))
6728 return;
6729
6730 clear_buddies(cfs_rq, se);
6731
6732 if (curr->policy != SCHED_BATCH) {
6733 update_rq_clock(rq);
6734 /*
6735 * Update run-time statistics of the 'current'.
6736 */
6737 update_curr(cfs_rq);
Mike Galbraith916671c2011-11-22 15:21:26 +01006738 /*
6739 * Tell update_rq_clock() that we've just updated,
6740 * so we don't do microscopic update in schedule()
6741 * and double the fastpath cost.
6742 */
Peter Zijlstra9edfbfe2015-01-05 11:18:11 +01006743 rq_clock_skip_update(rq, true);
Rik van Rielac53db52011-02-01 09:51:03 -05006744 }
6745
6746 set_skip_buddy(se);
6747}
6748
Mike Galbraithd95f4122011-02-01 09:50:51 -05006749static bool yield_to_task_fair(struct rq *rq, struct task_struct *p, bool preempt)
6750{
6751 struct sched_entity *se = &p->se;
6752
Paul Turner5238cdd2011-07-21 09:43:37 -07006753 /* throttled hierarchies are not runnable */
6754 if (!se->on_rq || throttled_hierarchy(cfs_rq_of(se)))
Mike Galbraithd95f4122011-02-01 09:50:51 -05006755 return false;
6756
6757 /* Tell the scheduler that we'd really like pse to run next. */
6758 set_next_buddy(se);
6759
Mike Galbraithd95f4122011-02-01 09:50:51 -05006760 yield_task_fair(rq);
6761
6762 return true;
6763}
6764
Peter Williams681f3e62007-10-24 18:23:51 +02006765#ifdef CONFIG_SMP
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02006766/**************************************************
Peter Zijlstrae9c84cb2012-07-03 13:53:26 +02006767 * Fair scheduling class load-balancing methods.
6768 *
6769 * BASICS
6770 *
6771 * The purpose of load-balancing is to achieve the same basic fairness the
6772 * per-cpu scheduler provides, namely provide a proportional amount of compute
6773 * time to each task. This is expressed in the following equation:
6774 *
6775 * W_i,n/P_i == W_j,n/P_j for all i,j (1)
6776 *
6777 * Where W_i,n is the n-th weight average for cpu i. The instantaneous weight
6778 * W_i,0 is defined as:
6779 *
6780 * W_i,0 = \Sum_j w_i,j (2)
6781 *
6782 * 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 +08006783 * is derived from the nice value as per sched_prio_to_weight[].
Peter Zijlstrae9c84cb2012-07-03 13:53:26 +02006784 *
6785 * The weight average is an exponential decay average of the instantaneous
6786 * weight:
6787 *
6788 * W'_i,n = (2^n - 1) / 2^n * W_i,n + 1 / 2^n * W_i,0 (3)
6789 *
Nicolas Pitreced549f2014-05-26 18:19:38 -04006790 * C_i is the compute capacity of cpu i, typically it is the
Peter Zijlstrae9c84cb2012-07-03 13:53:26 +02006791 * fraction of 'recent' time available for SCHED_OTHER task execution. But it
6792 * can also include other factors [XXX].
6793 *
6794 * To achieve this balance we define a measure of imbalance which follows
6795 * directly from (1):
6796 *
Nicolas Pitreced549f2014-05-26 18:19:38 -04006797 * 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 +02006798 *
6799 * We them move tasks around to minimize the imbalance. In the continuous
6800 * function space it is obvious this converges, in the discrete case we get
6801 * a few fun cases generally called infeasible weight scenarios.
6802 *
6803 * [XXX expand on:
6804 * - infeasible weights;
6805 * - local vs global optima in the discrete case. ]
6806 *
6807 *
6808 * SCHED DOMAINS
6809 *
6810 * In order to solve the imbalance equation (4), and avoid the obvious O(n^2)
6811 * for all i,j solution, we create a tree of cpus that follows the hardware
6812 * topology where each level pairs two lower groups (or better). This results
6813 * in O(log n) layers. Furthermore we reduce the number of cpus going up the
6814 * tree to only the first of the previous level and we decrease the frequency
6815 * of load-balance at each level inv. proportional to the number of cpus in
6816 * the groups.
6817 *
6818 * This yields:
6819 *
6820 * log_2 n 1 n
6821 * \Sum { --- * --- * 2^i } = O(n) (5)
6822 * i = 0 2^i 2^i
6823 * `- size of each group
6824 * | | `- number of cpus doing load-balance
6825 * | `- freq
6826 * `- sum over all levels
6827 *
6828 * Coupled with a limit on how many tasks we can migrate every balance pass,
6829 * this makes (5) the runtime complexity of the balancer.
6830 *
6831 * An important property here is that each CPU is still (indirectly) connected
6832 * to every other cpu in at most O(log n) steps:
6833 *
6834 * The adjacency matrix of the resulting graph is given by:
6835 *
Byungchul Park97a71422015-07-05 18:33:48 +09006836 * log_2 n
Peter Zijlstrae9c84cb2012-07-03 13:53:26 +02006837 * A_i,j = \Union (i % 2^k == 0) && i / 2^(k+1) == j / 2^(k+1) (6)
6838 * k = 0
6839 *
6840 * And you'll find that:
6841 *
6842 * A^(log_2 n)_i,j != 0 for all i,j (7)
6843 *
6844 * Showing there's indeed a path between every cpu in at most O(log n) steps.
6845 * The task movement gives a factor of O(m), giving a convergence complexity
6846 * of:
6847 *
6848 * O(nm log n), n := nr_cpus, m := nr_tasks (8)
6849 *
6850 *
6851 * WORK CONSERVING
6852 *
6853 * In order to avoid CPUs going idle while there's still work to do, new idle
6854 * balancing is more aggressive and has the newly idle cpu iterate up the domain
6855 * tree itself instead of relying on other CPUs to bring it work.
6856 *
6857 * This adds some complexity to both (5) and (8) but it reduces the total idle
6858 * time.
6859 *
6860 * [XXX more?]
6861 *
6862 *
6863 * CGROUPS
6864 *
6865 * Cgroups make a horror show out of (2), instead of a simple sum we get:
6866 *
6867 * s_k,i
6868 * W_i,0 = \Sum_j \Prod_k w_k * ----- (9)
6869 * S_k
6870 *
6871 * Where
6872 *
6873 * s_k,i = \Sum_j w_i,j,k and S_k = \Sum_i s_k,i (10)
6874 *
6875 * w_i,j,k is the weight of the j-th runnable task in the k-th cgroup on cpu i.
6876 *
6877 * The big problem is S_k, its a global sum needed to compute a local (W_i)
6878 * property.
6879 *
6880 * [XXX write more on how we solve this.. _after_ merging pjt's patches that
6881 * rewrite all of this once again.]
Byungchul Park97a71422015-07-05 18:33:48 +09006882 */
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02006883
Hiroshi Shimamotoed387b72012-01-31 11:40:32 +09006884static unsigned long __read_mostly max_load_balance_interval = HZ/10;
6885
Peter Zijlstra0ec8aa02013-10-07 11:29:33 +01006886enum fbq_type { regular, remote, all };
6887
Morten Rasmussen4c6a8242016-02-25 12:47:54 +00006888enum group_type {
6889 group_other = 0,
6890 group_misfit_task,
6891 group_imbalanced,
6892 group_overloaded,
6893};
6894
Peter Zijlstraddcdf6e2012-02-22 19:27:40 +01006895#define LBF_ALL_PINNED 0x01
Peter Zijlstra367456c2012-02-20 21:49:09 +01006896#define LBF_NEED_BREAK 0x02
Peter Zijlstra62633222013-08-19 12:41:09 +02006897#define LBF_DST_PINNED 0x04
6898#define LBF_SOME_PINNED 0x08
Peter Zijlstraddcdf6e2012-02-22 19:27:40 +01006899
6900struct lb_env {
6901 struct sched_domain *sd;
6902
Peter Zijlstraddcdf6e2012-02-22 19:27:40 +01006903 struct rq *src_rq;
Prashanth Nageshappa85c1e7d2012-06-19 17:47:34 +05306904 int src_cpu;
Peter Zijlstraddcdf6e2012-02-22 19:27:40 +01006905
6906 int dst_cpu;
6907 struct rq *dst_rq;
6908
Srivatsa Vaddagiri88b8dac2012-06-19 17:43:15 +05306909 struct cpumask *dst_grpmask;
6910 int new_dst_cpu;
Peter Zijlstraddcdf6e2012-02-22 19:27:40 +01006911 enum cpu_idle_type idle;
Peter Zijlstrabd939f42012-05-02 14:20:37 +02006912 long imbalance;
Morten Rasmussen94beeae2015-07-02 17:16:34 +01006913 unsigned int src_grp_nr_running;
Michael Wangb94031302012-07-12 16:10:13 +08006914 /* The set of CPUs under consideration for load-balancing */
6915 struct cpumask *cpus;
6916
Peter Zijlstraddcdf6e2012-02-22 19:27:40 +01006917 unsigned int flags;
Peter Zijlstra367456c2012-02-20 21:49:09 +01006918
6919 unsigned int loop;
6920 unsigned int loop_break;
6921 unsigned int loop_max;
Peter Zijlstra0ec8aa02013-10-07 11:29:33 +01006922
6923 enum fbq_type fbq_type;
Kirill Tkhai163122b2014-08-20 13:48:29 +04006924 struct list_head tasks;
Peter Zijlstraddcdf6e2012-02-22 19:27:40 +01006925};
6926
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01006927/*
Peter Zijlstra029632f2011-10-25 10:00:11 +02006928 * Is this task likely cache-hot:
6929 */
Hillf Danton5d5e2b12014-06-10 10:58:43 +02006930static int task_hot(struct task_struct *p, struct lb_env *env)
Peter Zijlstra029632f2011-10-25 10:00:11 +02006931{
6932 s64 delta;
6933
Kirill Tkhaie5673f22014-08-20 13:48:01 +04006934 lockdep_assert_held(&env->src_rq->lock);
6935
Peter Zijlstra029632f2011-10-25 10:00:11 +02006936 if (p->sched_class != &fair_sched_class)
6937 return 0;
6938
6939 if (unlikely(p->policy == SCHED_IDLE))
6940 return 0;
6941
6942 /*
6943 * Buddy candidates are cache hot:
6944 */
Hillf Danton5d5e2b12014-06-10 10:58:43 +02006945 if (sched_feat(CACHE_HOT_BUDDY) && env->dst_rq->nr_running &&
Peter Zijlstra029632f2011-10-25 10:00:11 +02006946 (&p->se == cfs_rq_of(&p->se)->next ||
6947 &p->se == cfs_rq_of(&p->se)->last))
6948 return 1;
6949
6950 if (sysctl_sched_migration_cost == -1)
6951 return 1;
6952 if (sysctl_sched_migration_cost == 0)
6953 return 0;
6954
Hillf Danton5d5e2b12014-06-10 10:58:43 +02006955 delta = rq_clock_task(env->src_rq) - p->se.exec_start;
Peter Zijlstra029632f2011-10-25 10:00:11 +02006956
6957 return delta < (s64)sysctl_sched_migration_cost;
6958}
6959
Mel Gorman3a7053b2013-10-07 11:29:00 +01006960#ifdef CONFIG_NUMA_BALANCING
Rik van Rielc1ceac62015-05-14 22:59:36 -04006961/*
Srikar Dronamraju2a1ed242015-06-16 17:25:59 +05306962 * Returns 1, if task migration degrades locality
6963 * Returns 0, if task migration improves locality i.e migration preferred.
6964 * Returns -1, if task migration is not affected by locality.
Rik van Rielc1ceac62015-05-14 22:59:36 -04006965 */
Srikar Dronamraju2a1ed242015-06-16 17:25:59 +05306966static int migrate_degrades_locality(struct task_struct *p, struct lb_env *env)
Mel Gorman3a7053b2013-10-07 11:29:00 +01006967{
Rik van Rielb1ad0652014-05-15 13:03:06 -04006968 struct numa_group *numa_group = rcu_dereference(p->numa_group);
Rik van Rielc1ceac62015-05-14 22:59:36 -04006969 unsigned long src_faults, dst_faults;
Mel Gorman3a7053b2013-10-07 11:29:00 +01006970 int src_nid, dst_nid;
6971
Srikar Dronamraju2a595722015-08-11 21:54:21 +05306972 if (!static_branch_likely(&sched_numa_balancing))
Srikar Dronamraju2a1ed242015-06-16 17:25:59 +05306973 return -1;
6974
Srikar Dronamrajuc3b9bc52015-08-11 16:30:12 +05306975 if (!p->numa_faults || !(env->sd->flags & SD_NUMA))
Srikar Dronamraju2a1ed242015-06-16 17:25:59 +05306976 return -1;
Mel Gorman7a0f3082013-10-07 11:29:01 +01006977
6978 src_nid = cpu_to_node(env->src_cpu);
6979 dst_nid = cpu_to_node(env->dst_cpu);
6980
Mel Gorman83e1d2c2013-10-07 11:29:27 +01006981 if (src_nid == dst_nid)
Srikar Dronamraju2a1ed242015-06-16 17:25:59 +05306982 return -1;
Mel Gorman7a0f3082013-10-07 11:29:01 +01006983
Srikar Dronamraju2a1ed242015-06-16 17:25:59 +05306984 /* Migrating away from the preferred node is always bad. */
6985 if (src_nid == p->numa_preferred_nid) {
6986 if (env->src_rq->nr_running > env->src_rq->nr_preferred_running)
6987 return 1;
6988 else
6989 return -1;
6990 }
Mel Gorman83e1d2c2013-10-07 11:29:27 +01006991
Rik van Rielc1ceac62015-05-14 22:59:36 -04006992 /* Encourage migration to the preferred node. */
6993 if (dst_nid == p->numa_preferred_nid)
Srikar Dronamraju2a1ed242015-06-16 17:25:59 +05306994 return 0;
Rik van Rielc1ceac62015-05-14 22:59:36 -04006995
6996 if (numa_group) {
6997 src_faults = group_faults(p, src_nid);
6998 dst_faults = group_faults(p, dst_nid);
6999 } else {
7000 src_faults = task_faults(p, src_nid);
7001 dst_faults = task_faults(p, dst_nid);
7002 }
7003
7004 return dst_faults < src_faults;
Mel Gorman7a0f3082013-10-07 11:29:01 +01007005}
7006
Mel Gorman3a7053b2013-10-07 11:29:00 +01007007#else
Srikar Dronamraju2a1ed242015-06-16 17:25:59 +05307008static inline int migrate_degrades_locality(struct task_struct *p,
Mel Gorman3a7053b2013-10-07 11:29:00 +01007009 struct lb_env *env)
7010{
Srikar Dronamraju2a1ed242015-06-16 17:25:59 +05307011 return -1;
Mel Gorman7a0f3082013-10-07 11:29:01 +01007012}
Mel Gorman3a7053b2013-10-07 11:29:00 +01007013#endif
7014
Peter Zijlstra029632f2011-10-25 10:00:11 +02007015/*
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007016 * can_migrate_task - may task p from runqueue rq be migrated to this_cpu?
7017 */
7018static
Peter Zijlstra8e45cb52012-02-22 12:47:19 +01007019int can_migrate_task(struct task_struct *p, struct lb_env *env)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007020{
Srikar Dronamraju2a1ed242015-06-16 17:25:59 +05307021 int tsk_cache_hot;
Kirill Tkhaie5673f22014-08-20 13:48:01 +04007022
7023 lockdep_assert_held(&env->src_rq->lock);
7024
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007025 /*
7026 * We do not migrate tasks that are:
Joonsoo Kimd3198082013-04-23 17:27:40 +09007027 * 1) throttled_lb_pair, or
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007028 * 2) cannot be migrated to this CPU due to cpus_allowed, or
Joonsoo Kimd3198082013-04-23 17:27:40 +09007029 * 3) running (obviously), or
7030 * 4) are cache-hot on their current CPU.
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007031 */
Joonsoo Kimd3198082013-04-23 17:27:40 +09007032 if (throttled_lb_pair(task_group(p), env->src_cpu, env->dst_cpu))
7033 return 0;
7034
Peter Zijlstraddcdf6e2012-02-22 19:27:40 +01007035 if (!cpumask_test_cpu(env->dst_cpu, tsk_cpus_allowed(p))) {
Joonsoo Kime02e60c2013-04-23 17:27:42 +09007036 int cpu;
Srivatsa Vaddagiri88b8dac2012-06-19 17:43:15 +05307037
Josh Poimboeufae928822016-06-17 12:43:24 -05007038 schedstat_inc(p->se.statistics.nr_failed_migrations_affine);
Srivatsa Vaddagiri88b8dac2012-06-19 17:43:15 +05307039
Peter Zijlstra62633222013-08-19 12:41:09 +02007040 env->flags |= LBF_SOME_PINNED;
7041
Srivatsa Vaddagiri88b8dac2012-06-19 17:43:15 +05307042 /*
7043 * Remember if this task can be migrated to any other cpu in
7044 * our sched_group. We may want to revisit it if we couldn't
7045 * meet load balance goals by pulling other tasks on src_cpu.
7046 *
7047 * Also avoid computing new_dst_cpu if we have already computed
7048 * one in current iteration.
7049 */
Peter Zijlstra62633222013-08-19 12:41:09 +02007050 if (!env->dst_grpmask || (env->flags & LBF_DST_PINNED))
Srivatsa Vaddagiri88b8dac2012-06-19 17:43:15 +05307051 return 0;
7052
Joonsoo Kime02e60c2013-04-23 17:27:42 +09007053 /* Prevent to re-select dst_cpu via env's cpus */
7054 for_each_cpu_and(cpu, env->dst_grpmask, env->cpus) {
7055 if (cpumask_test_cpu(cpu, tsk_cpus_allowed(p))) {
Peter Zijlstra62633222013-08-19 12:41:09 +02007056 env->flags |= LBF_DST_PINNED;
Joonsoo Kime02e60c2013-04-23 17:27:42 +09007057 env->new_dst_cpu = cpu;
7058 break;
7059 }
Srivatsa Vaddagiri88b8dac2012-06-19 17:43:15 +05307060 }
Joonsoo Kime02e60c2013-04-23 17:27:42 +09007061
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007062 return 0;
7063 }
Srivatsa Vaddagiri88b8dac2012-06-19 17:43:15 +05307064
7065 /* Record that we found atleast one task that could run on dst_cpu */
Peter Zijlstra8e45cb52012-02-22 12:47:19 +01007066 env->flags &= ~LBF_ALL_PINNED;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007067
Peter Zijlstraddcdf6e2012-02-22 19:27:40 +01007068 if (task_running(env->src_rq, p)) {
Josh Poimboeufae928822016-06-17 12:43:24 -05007069 schedstat_inc(p->se.statistics.nr_failed_migrations_running);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007070 return 0;
7071 }
7072
7073 /*
7074 * Aggressive migration if:
Mel Gorman3a7053b2013-10-07 11:29:00 +01007075 * 1) destination numa is preferred
7076 * 2) task is cache cold, or
7077 * 3) too many balance attempts have failed.
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007078 */
Srikar Dronamraju2a1ed242015-06-16 17:25:59 +05307079 tsk_cache_hot = migrate_degrades_locality(p, env);
7080 if (tsk_cache_hot == -1)
7081 tsk_cache_hot = task_hot(p, env);
Mel Gorman3a7053b2013-10-07 11:29:00 +01007082
Srikar Dronamraju2a1ed242015-06-16 17:25:59 +05307083 if (tsk_cache_hot <= 0 ||
Kirill Tkhai7a96c232014-09-22 22:36:12 +04007084 env->sd->nr_balance_failed > env->sd->cache_nice_tries) {
Srikar Dronamraju2a1ed242015-06-16 17:25:59 +05307085 if (tsk_cache_hot == 1) {
Josh Poimboeufae928822016-06-17 12:43:24 -05007086 schedstat_inc(env->sd->lb_hot_gained[env->idle]);
7087 schedstat_inc(p->se.statistics.nr_forced_migrations);
Mel Gorman3a7053b2013-10-07 11:29:00 +01007088 }
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007089 return 1;
7090 }
7091
Josh Poimboeufae928822016-06-17 12:43:24 -05007092 schedstat_inc(p->se.statistics.nr_failed_migrations_hot);
Zhang Hang4e2dcb72013-04-10 14:04:55 +08007093 return 0;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007094}
7095
Peter Zijlstra897c3952009-12-17 17:45:42 +01007096/*
Kirill Tkhai163122b2014-08-20 13:48:29 +04007097 * detach_task() -- detach the task for the migration specified in env
Peter Zijlstra897c3952009-12-17 17:45:42 +01007098 */
Kirill Tkhai163122b2014-08-20 13:48:29 +04007099static void detach_task(struct task_struct *p, struct lb_env *env)
7100{
7101 lockdep_assert_held(&env->src_rq->lock);
7102
Kirill Tkhai163122b2014-08-20 13:48:29 +04007103 p->on_rq = TASK_ON_RQ_MIGRATING;
Joonwoo Park3ea94de2015-11-12 19:38:54 -08007104 deactivate_task(env->src_rq, p, 0);
Kirill Tkhai163122b2014-08-20 13:48:29 +04007105 set_task_cpu(p, env->dst_cpu);
7106}
7107
7108/*
Kirill Tkhaie5673f22014-08-20 13:48:01 +04007109 * detach_one_task() -- tries to dequeue exactly one task from env->src_rq, as
Peter Zijlstra897c3952009-12-17 17:45:42 +01007110 * part of active balancing operations within "domain".
Peter Zijlstra897c3952009-12-17 17:45:42 +01007111 *
Kirill Tkhaie5673f22014-08-20 13:48:01 +04007112 * Returns a task if successful and NULL otherwise.
Peter Zijlstra897c3952009-12-17 17:45:42 +01007113 */
Kirill Tkhaie5673f22014-08-20 13:48:01 +04007114static struct task_struct *detach_one_task(struct lb_env *env)
Peter Zijlstra897c3952009-12-17 17:45:42 +01007115{
7116 struct task_struct *p, *n;
Peter Zijlstra897c3952009-12-17 17:45:42 +01007117
Kirill Tkhaie5673f22014-08-20 13:48:01 +04007118 lockdep_assert_held(&env->src_rq->lock);
7119
Peter Zijlstra367456c2012-02-20 21:49:09 +01007120 list_for_each_entry_safe(p, n, &env->src_rq->cfs_tasks, se.group_node) {
Peter Zijlstra367456c2012-02-20 21:49:09 +01007121 if (!can_migrate_task(p, env))
7122 continue;
Peter Zijlstra897c3952009-12-17 17:45:42 +01007123
Kirill Tkhai163122b2014-08-20 13:48:29 +04007124 detach_task(p, env);
Kirill Tkhaie5673f22014-08-20 13:48:01 +04007125
Peter Zijlstra367456c2012-02-20 21:49:09 +01007126 /*
Kirill Tkhaie5673f22014-08-20 13:48:01 +04007127 * Right now, this is only the second place where
Kirill Tkhai163122b2014-08-20 13:48:29 +04007128 * lb_gained[env->idle] is updated (other is detach_tasks)
Kirill Tkhaie5673f22014-08-20 13:48:01 +04007129 * so we can safely collect stats here rather than
Kirill Tkhai163122b2014-08-20 13:48:29 +04007130 * inside detach_tasks().
Peter Zijlstra367456c2012-02-20 21:49:09 +01007131 */
Josh Poimboeufae928822016-06-17 12:43:24 -05007132 schedstat_inc(env->sd->lb_gained[env->idle]);
Kirill Tkhaie5673f22014-08-20 13:48:01 +04007133 return p;
Peter Zijlstra897c3952009-12-17 17:45:42 +01007134 }
Kirill Tkhaie5673f22014-08-20 13:48:01 +04007135 return NULL;
Peter Zijlstra897c3952009-12-17 17:45:42 +01007136}
7137
Peter Zijlstraeb953082012-04-17 13:38:40 +02007138static const unsigned int sched_nr_migrate_break = 32;
7139
Peter Zijlstra5d6523e2012-03-10 00:07:36 +01007140/*
Kirill Tkhai163122b2014-08-20 13:48:29 +04007141 * detach_tasks() -- tries to detach up to imbalance weighted load from
7142 * busiest_rq, as part of a balancing operation within domain "sd".
Peter Zijlstra5d6523e2012-03-10 00:07:36 +01007143 *
Kirill Tkhai163122b2014-08-20 13:48:29 +04007144 * Returns number of detached tasks if successful and 0 otherwise.
Peter Zijlstra5d6523e2012-03-10 00:07:36 +01007145 */
Kirill Tkhai163122b2014-08-20 13:48:29 +04007146static int detach_tasks(struct lb_env *env)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007147{
Peter Zijlstra5d6523e2012-03-10 00:07:36 +01007148 struct list_head *tasks = &env->src_rq->cfs_tasks;
7149 struct task_struct *p;
Peter Zijlstra367456c2012-02-20 21:49:09 +01007150 unsigned long load;
Kirill Tkhai163122b2014-08-20 13:48:29 +04007151 int detached = 0;
7152
7153 lockdep_assert_held(&env->src_rq->lock);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007154
Peter Zijlstrabd939f42012-05-02 14:20:37 +02007155 if (env->imbalance <= 0)
Peter Zijlstra5d6523e2012-03-10 00:07:36 +01007156 return 0;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007157
Peter Zijlstra5d6523e2012-03-10 00:07:36 +01007158 while (!list_empty(tasks)) {
Yuyang Du985d3a42015-07-06 06:11:51 +08007159 /*
7160 * We don't want to steal all, otherwise we may be treated likewise,
7161 * which could at worst lead to a livelock crash.
7162 */
7163 if (env->idle != CPU_NOT_IDLE && env->src_rq->nr_running <= 1)
7164 break;
7165
Peter Zijlstra5d6523e2012-03-10 00:07:36 +01007166 p = list_first_entry(tasks, struct task_struct, se.group_node);
7167
Peter Zijlstra367456c2012-02-20 21:49:09 +01007168 env->loop++;
7169 /* We've more or less seen every task there is, call it quits */
Peter Zijlstra5d6523e2012-03-10 00:07:36 +01007170 if (env->loop > env->loop_max)
Peter Zijlstra367456c2012-02-20 21:49:09 +01007171 break;
Peter Zijlstra5d6523e2012-03-10 00:07:36 +01007172
7173 /* take a breather every nr_migrate tasks */
Peter Zijlstra367456c2012-02-20 21:49:09 +01007174 if (env->loop > env->loop_break) {
Peter Zijlstraeb953082012-04-17 13:38:40 +02007175 env->loop_break += sched_nr_migrate_break;
Peter Zijlstra8e45cb52012-02-22 12:47:19 +01007176 env->flags |= LBF_NEED_BREAK;
Peter Zijlstraee00e662009-12-17 17:25:20 +01007177 break;
Peter Zijlstraa195f002011-09-22 15:30:18 +02007178 }
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007179
Joonsoo Kimd3198082013-04-23 17:27:40 +09007180 if (!can_migrate_task(p, env))
Peter Zijlstra367456c2012-02-20 21:49:09 +01007181 goto next;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007182
Peter Zijlstra367456c2012-02-20 21:49:09 +01007183 load = task_h_load(p);
Peter Zijlstra5d6523e2012-03-10 00:07:36 +01007184
Peter Zijlstraeb953082012-04-17 13:38:40 +02007185 if (sched_feat(LB_MIN) && load < 16 && !env->sd->nr_balance_failed)
Peter Zijlstra367456c2012-02-20 21:49:09 +01007186 goto next;
7187
Peter Zijlstrabd939f42012-05-02 14:20:37 +02007188 if ((load / 2) > env->imbalance)
Peter Zijlstra367456c2012-02-20 21:49:09 +01007189 goto next;
7190
Kirill Tkhai163122b2014-08-20 13:48:29 +04007191 detach_task(p, env);
7192 list_add(&p->se.group_node, &env->tasks);
7193
7194 detached++;
Peter Zijlstrabd939f42012-05-02 14:20:37 +02007195 env->imbalance -= load;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007196
7197#ifdef CONFIG_PREEMPT
Peter Zijlstraee00e662009-12-17 17:25:20 +01007198 /*
7199 * NEWIDLE balancing is a source of latency, so preemptible
Kirill Tkhai163122b2014-08-20 13:48:29 +04007200 * kernels will stop after the first task is detached to minimize
Peter Zijlstraee00e662009-12-17 17:25:20 +01007201 * the critical section.
7202 */
Peter Zijlstra5d6523e2012-03-10 00:07:36 +01007203 if (env->idle == CPU_NEWLY_IDLE)
Peter Zijlstraee00e662009-12-17 17:25:20 +01007204 break;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007205#endif
7206
Peter Zijlstraee00e662009-12-17 17:25:20 +01007207 /*
7208 * We only want to steal up to the prescribed amount of
7209 * weighted load.
7210 */
Peter Zijlstrabd939f42012-05-02 14:20:37 +02007211 if (env->imbalance <= 0)
Peter Zijlstraee00e662009-12-17 17:25:20 +01007212 break;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007213
Peter Zijlstra367456c2012-02-20 21:49:09 +01007214 continue;
7215next:
Peter Zijlstra5d6523e2012-03-10 00:07:36 +01007216 list_move_tail(&p->se.group_node, tasks);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007217 }
Peter Zijlstra5d6523e2012-03-10 00:07:36 +01007218
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007219 /*
Kirill Tkhai163122b2014-08-20 13:48:29 +04007220 * Right now, this is one of only two places we collect this stat
7221 * so we can safely collect detach_one_task() stats here rather
7222 * than inside detach_one_task().
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007223 */
Josh Poimboeufae928822016-06-17 12:43:24 -05007224 schedstat_add(env->sd->lb_gained[env->idle], detached);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007225
Kirill Tkhai163122b2014-08-20 13:48:29 +04007226 return detached;
7227}
7228
7229/*
7230 * attach_task() -- attach the task detached by detach_task() to its new rq.
7231 */
7232static void attach_task(struct rq *rq, struct task_struct *p)
7233{
7234 lockdep_assert_held(&rq->lock);
7235
7236 BUG_ON(task_rq(p) != rq);
Kirill Tkhai163122b2014-08-20 13:48:29 +04007237 activate_task(rq, p, 0);
Joonwoo Park3ea94de2015-11-12 19:38:54 -08007238 p->on_rq = TASK_ON_RQ_QUEUED;
Kirill Tkhai163122b2014-08-20 13:48:29 +04007239 check_preempt_curr(rq, p, 0);
7240}
7241
7242/*
7243 * attach_one_task() -- attaches the task returned from detach_one_task() to
7244 * its new rq.
7245 */
7246static void attach_one_task(struct rq *rq, struct task_struct *p)
7247{
7248 raw_spin_lock(&rq->lock);
7249 attach_task(rq, p);
Juri Lellic9bf15a2015-06-25 14:37:27 +01007250 /*
7251 * We want to potentially raise target_cpu's OPP.
7252 */
7253 update_capacity_of(cpu_of(rq));
Kirill Tkhai163122b2014-08-20 13:48:29 +04007254 raw_spin_unlock(&rq->lock);
7255}
7256
7257/*
7258 * attach_tasks() -- attaches all tasks detached by detach_tasks() to their
7259 * new rq.
7260 */
7261static void attach_tasks(struct lb_env *env)
7262{
7263 struct list_head *tasks = &env->tasks;
7264 struct task_struct *p;
7265
7266 raw_spin_lock(&env->dst_rq->lock);
7267
7268 while (!list_empty(tasks)) {
7269 p = list_first_entry(tasks, struct task_struct, se.group_node);
7270 list_del_init(&p->se.group_node);
7271
7272 attach_task(env->dst_rq, p);
7273 }
7274
Juri Lellic9bf15a2015-06-25 14:37:27 +01007275 /*
7276 * We want to potentially raise env.dst_cpu's OPP.
7277 */
7278 update_capacity_of(env->dst_cpu);
7279
Kirill Tkhai163122b2014-08-20 13:48:29 +04007280 raw_spin_unlock(&env->dst_rq->lock);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007281}
7282
Peter Zijlstra230059de2009-12-17 17:47:12 +01007283#ifdef CONFIG_FAIR_GROUP_SCHED
Paul Turner48a16752012-10-04 13:18:31 +02007284static void update_blocked_averages(int cpu)
Peter Zijlstra9e3081c2010-11-15 15:47:02 -08007285{
Peter Zijlstra9e3081c2010-11-15 15:47:02 -08007286 struct rq *rq = cpu_rq(cpu);
Paul Turner48a16752012-10-04 13:18:31 +02007287 struct cfs_rq *cfs_rq;
7288 unsigned long flags;
Peter Zijlstra9e3081c2010-11-15 15:47:02 -08007289
Paul Turner48a16752012-10-04 13:18:31 +02007290 raw_spin_lock_irqsave(&rq->lock, flags);
7291 update_rq_clock(rq);
Yuyang Du9d89c252015-07-15 08:04:37 +08007292
Peter Zijlstra9763b672011-07-13 13:09:25 +02007293 /*
7294 * Iterates the task_group tree in a bottom up fashion, see
7295 * list_add_leaf_cfs_rq() for details.
7296 */
Paul Turner64660c82011-07-21 09:43:36 -07007297 for_each_leaf_cfs_rq(rq, cfs_rq) {
Yuyang Du9d89c252015-07-15 08:04:37 +08007298 /* throttled entities do not contribute to load */
7299 if (throttled_hierarchy(cfs_rq))
7300 continue;
Paul Turner48a16752012-10-04 13:18:31 +02007301
Steve Mucklea2c6c912016-03-24 15:26:07 -07007302 if (update_cfs_rq_load_avg(cfs_rq_clock_task(cfs_rq), cfs_rq, true))
Yuyang Du9d89c252015-07-15 08:04:37 +08007303 update_tg_load_avg(cfs_rq, 0);
7304 }
Paul Turner48a16752012-10-04 13:18:31 +02007305 raw_spin_unlock_irqrestore(&rq->lock, flags);
Peter Zijlstra9e3081c2010-11-15 15:47:02 -08007306}
7307
Peter Zijlstra9763b672011-07-13 13:09:25 +02007308/*
Vladimir Davydov68520792013-07-15 17:49:19 +04007309 * Compute the hierarchical load factor for cfs_rq and all its ascendants.
Peter Zijlstra9763b672011-07-13 13:09:25 +02007310 * This needs to be done in a top-down fashion because the load of a child
7311 * group is a fraction of its parents load.
7312 */
Vladimir Davydov68520792013-07-15 17:49:19 +04007313static void update_cfs_rq_h_load(struct cfs_rq *cfs_rq)
Peter Zijlstra9763b672011-07-13 13:09:25 +02007314{
Vladimir Davydov68520792013-07-15 17:49:19 +04007315 struct rq *rq = rq_of(cfs_rq);
7316 struct sched_entity *se = cfs_rq->tg->se[cpu_of(rq)];
Peter Zijlstraa35b6462012-08-08 21:46:40 +02007317 unsigned long now = jiffies;
Vladimir Davydov68520792013-07-15 17:49:19 +04007318 unsigned long load;
Peter Zijlstraa35b6462012-08-08 21:46:40 +02007319
Vladimir Davydov68520792013-07-15 17:49:19 +04007320 if (cfs_rq->last_h_load_update == now)
Peter Zijlstraa35b6462012-08-08 21:46:40 +02007321 return;
7322
Vladimir Davydov68520792013-07-15 17:49:19 +04007323 cfs_rq->h_load_next = NULL;
7324 for_each_sched_entity(se) {
7325 cfs_rq = cfs_rq_of(se);
7326 cfs_rq->h_load_next = se;
7327 if (cfs_rq->last_h_load_update == now)
7328 break;
7329 }
Peter Zijlstraa35b6462012-08-08 21:46:40 +02007330
Vladimir Davydov68520792013-07-15 17:49:19 +04007331 if (!se) {
Yuyang Du7ea241a2015-07-15 08:04:42 +08007332 cfs_rq->h_load = cfs_rq_load_avg(cfs_rq);
Vladimir Davydov68520792013-07-15 17:49:19 +04007333 cfs_rq->last_h_load_update = now;
7334 }
7335
7336 while ((se = cfs_rq->h_load_next) != NULL) {
7337 load = cfs_rq->h_load;
Yuyang Du7ea241a2015-07-15 08:04:42 +08007338 load = div64_ul(load * se->avg.load_avg,
7339 cfs_rq_load_avg(cfs_rq) + 1);
Vladimir Davydov68520792013-07-15 17:49:19 +04007340 cfs_rq = group_cfs_rq(se);
7341 cfs_rq->h_load = load;
7342 cfs_rq->last_h_load_update = now;
7343 }
Peter Zijlstra9763b672011-07-13 13:09:25 +02007344}
7345
Peter Zijlstra367456c2012-02-20 21:49:09 +01007346static unsigned long task_h_load(struct task_struct *p)
Peter Zijlstra230059de2009-12-17 17:47:12 +01007347{
Peter Zijlstra367456c2012-02-20 21:49:09 +01007348 struct cfs_rq *cfs_rq = task_cfs_rq(p);
Peter Zijlstra230059de2009-12-17 17:47:12 +01007349
Vladimir Davydov68520792013-07-15 17:49:19 +04007350 update_cfs_rq_h_load(cfs_rq);
Yuyang Du9d89c252015-07-15 08:04:37 +08007351 return div64_ul(p->se.avg.load_avg * cfs_rq->h_load,
Yuyang Du7ea241a2015-07-15 08:04:42 +08007352 cfs_rq_load_avg(cfs_rq) + 1);
Peter Zijlstra230059de2009-12-17 17:47:12 +01007353}
7354#else
Paul Turner48a16752012-10-04 13:18:31 +02007355static inline void update_blocked_averages(int cpu)
Peter Zijlstra9e3081c2010-11-15 15:47:02 -08007356{
Vincent Guittot6c1d47c2015-07-15 08:04:38 +08007357 struct rq *rq = cpu_rq(cpu);
7358 struct cfs_rq *cfs_rq = &rq->cfs;
7359 unsigned long flags;
7360
7361 raw_spin_lock_irqsave(&rq->lock, flags);
7362 update_rq_clock(rq);
Steve Mucklea2c6c912016-03-24 15:26:07 -07007363 update_cfs_rq_load_avg(cfs_rq_clock_task(cfs_rq), cfs_rq, true);
Vincent Guittot6c1d47c2015-07-15 08:04:38 +08007364 raw_spin_unlock_irqrestore(&rq->lock, flags);
Peter Zijlstra9e3081c2010-11-15 15:47:02 -08007365}
7366
Peter Zijlstra367456c2012-02-20 21:49:09 +01007367static unsigned long task_h_load(struct task_struct *p)
7368{
Yuyang Du9d89c252015-07-15 08:04:37 +08007369 return p->se.avg.load_avg;
Peter Zijlstra230059de2009-12-17 17:47:12 +01007370}
7371#endif
7372
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007373/********** Helpers for find_busiest_group ************************/
Rik van Rielcaeb1782014-07-28 14:16:28 -04007374
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007375/*
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007376 * sg_lb_stats - stats of a sched_group required for load_balancing
7377 */
7378struct sg_lb_stats {
7379 unsigned long avg_load; /*Avg load across the CPUs of the group */
7380 unsigned long group_load; /* Total load over the CPUs of the group */
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007381 unsigned long sum_weighted_load; /* Weighted load of group's tasks */
Joonsoo Kim56cf5152013-08-06 17:36:43 +09007382 unsigned long load_per_task;
Nicolas Pitre63b2ca32014-05-26 18:19:37 -04007383 unsigned long group_capacity;
Dietmar Eggemann9e91d612015-08-14 17:23:12 +01007384 unsigned long group_util; /* Total utilization of the group */
Peter Zijlstra147c5fc22013-08-19 15:22:57 +02007385 unsigned int sum_nr_running; /* Nr tasks running in the group */
Peter Zijlstra147c5fc22013-08-19 15:22:57 +02007386 unsigned int idle_cpus;
7387 unsigned int group_weight;
Rik van Rielcaeb1782014-07-28 14:16:28 -04007388 enum group_type group_type;
Vincent Guittotea678212015-02-27 16:54:11 +01007389 int group_no_capacity;
Morten Rasmussen4c6a8242016-02-25 12:47:54 +00007390 int group_misfit_task; /* A cpu has a task too big for its capacity */
Peter Zijlstra0ec8aa02013-10-07 11:29:33 +01007391#ifdef CONFIG_NUMA_BALANCING
7392 unsigned int nr_numa_running;
7393 unsigned int nr_preferred_running;
7394#endif
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007395};
7396
Joonsoo Kim56cf5152013-08-06 17:36:43 +09007397/*
7398 * sd_lb_stats - Structure to store the statistics of a sched_domain
7399 * during load balancing.
7400 */
7401struct sd_lb_stats {
7402 struct sched_group *busiest; /* Busiest group in this sd */
7403 struct sched_group *local; /* Local group in this sd */
7404 unsigned long total_load; /* Total load of all groups in sd */
Nicolas Pitre63b2ca32014-05-26 18:19:37 -04007405 unsigned long total_capacity; /* Total capacity of all groups in sd */
Joonsoo Kim56cf5152013-08-06 17:36:43 +09007406 unsigned long avg_load; /* Average load across all groups in sd */
7407
Joonsoo Kim56cf5152013-08-06 17:36:43 +09007408 struct sg_lb_stats busiest_stat;/* Statistics of the busiest group */
Peter Zijlstra147c5fc22013-08-19 15:22:57 +02007409 struct sg_lb_stats local_stat; /* Statistics of the local group */
Joonsoo Kim56cf5152013-08-06 17:36:43 +09007410};
7411
Peter Zijlstra147c5fc22013-08-19 15:22:57 +02007412static inline void init_sd_lb_stats(struct sd_lb_stats *sds)
7413{
7414 /*
7415 * Skimp on the clearing to avoid duplicate work. We can avoid clearing
7416 * local_stat because update_sg_lb_stats() does a full clear/assignment.
7417 * We must however clear busiest_stat::avg_load because
7418 * update_sd_pick_busiest() reads this before assignment.
7419 */
7420 *sds = (struct sd_lb_stats){
7421 .busiest = NULL,
7422 .local = NULL,
7423 .total_load = 0UL,
Nicolas Pitre63b2ca32014-05-26 18:19:37 -04007424 .total_capacity = 0UL,
Peter Zijlstra147c5fc22013-08-19 15:22:57 +02007425 .busiest_stat = {
7426 .avg_load = 0UL,
Rik van Rielcaeb1782014-07-28 14:16:28 -04007427 .sum_nr_running = 0,
7428 .group_type = group_other,
Peter Zijlstra147c5fc22013-08-19 15:22:57 +02007429 },
7430 };
7431}
7432
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007433/**
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007434 * get_sd_load_idx - Obtain the load index for a given sched domain.
7435 * @sd: The sched_domain whose load_idx is to be obtained.
Kamalesh Babulaled1b7732013-10-13 23:06:15 +05307436 * @idle: The idle status of the CPU for whose sd load_idx is obtained.
Yacine Belkadie69f6182013-07-12 20:45:47 +02007437 *
7438 * Return: The load index.
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007439 */
7440static inline int get_sd_load_idx(struct sched_domain *sd,
7441 enum cpu_idle_type idle)
7442{
7443 int load_idx;
7444
7445 switch (idle) {
7446 case CPU_NOT_IDLE:
7447 load_idx = sd->busy_idx;
7448 break;
7449
7450 case CPU_NEWLY_IDLE:
7451 load_idx = sd->newidle_idx;
7452 break;
7453 default:
7454 load_idx = sd->idle_idx;
7455 break;
7456 }
7457
7458 return load_idx;
7459}
7460
Nicolas Pitreced549f2014-05-26 18:19:38 -04007461static unsigned long scale_rt_capacity(int cpu)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007462{
7463 struct rq *rq = cpu_rq(cpu);
Vincent Guittotb5b48602015-02-27 16:54:08 +01007464 u64 total, used, age_stamp, avg;
Peter Zijlstracadefd32014-02-27 10:40:35 +01007465 s64 delta;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007466
Peter Zijlstrab654f7d2012-05-22 14:04:28 +02007467 /*
7468 * Since we're reading these variables without serialization make sure
7469 * we read them once before doing sanity checks on them.
7470 */
Jason Low316c1608d2015-04-28 13:00:20 -07007471 age_stamp = READ_ONCE(rq->age_stamp);
7472 avg = READ_ONCE(rq->rt_avg);
Peter Zijlstracebde6d2015-01-05 11:18:10 +01007473 delta = __rq_clock_broken(rq) - age_stamp;
Venkatesh Pallipadiaa483802010-10-04 17:03:22 -07007474
Peter Zijlstracadefd32014-02-27 10:40:35 +01007475 if (unlikely(delta < 0))
7476 delta = 0;
7477
7478 total = sched_avg_period() + delta;
Peter Zijlstrab654f7d2012-05-22 14:04:28 +02007479
Vincent Guittotb5b48602015-02-27 16:54:08 +01007480 used = div_u64(avg, total);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007481
Vincent Guittotb5b48602015-02-27 16:54:08 +01007482 if (likely(used < SCHED_CAPACITY_SCALE))
7483 return SCHED_CAPACITY_SCALE - used;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007484
Vincent Guittotb5b48602015-02-27 16:54:08 +01007485 return 1;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007486}
7487
Dietmar Eggemannbbb138b2015-09-26 18:19:54 +01007488void init_max_cpu_capacity(struct max_cpu_capacity *mcc)
7489{
7490 raw_spin_lock_init(&mcc->lock);
7491 mcc->val = 0;
7492 mcc->cpu = -1;
7493}
7494
Nicolas Pitreced549f2014-05-26 18:19:38 -04007495static void update_cpu_capacity(struct sched_domain *sd, int cpu)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007496{
Morten Rasmussen8cd56012015-08-14 17:23:10 +01007497 unsigned long capacity = arch_scale_cpu_capacity(sd, cpu);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007498 struct sched_group *sdg = sd->groups;
Dietmar Eggemannbbb138b2015-09-26 18:19:54 +01007499 struct max_cpu_capacity *mcc;
7500 unsigned long max_capacity;
7501 int max_cap_cpu;
7502 unsigned long flags;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007503
Vincent Guittotca6d75e2015-02-27 16:54:09 +01007504 cpu_rq(cpu)->cpu_capacity_orig = capacity;
Srivatsa Vaddagiri9d5efe02010-06-08 14:57:02 +10007505
Dietmar Eggemannbbb138b2015-09-26 18:19:54 +01007506 mcc = &cpu_rq(cpu)->rd->max_cpu_capacity;
7507
7508 raw_spin_lock_irqsave(&mcc->lock, flags);
7509 max_capacity = mcc->val;
7510 max_cap_cpu = mcc->cpu;
7511
7512 if ((max_capacity > capacity && max_cap_cpu == cpu) ||
7513 (max_capacity < capacity)) {
7514 mcc->val = capacity;
7515 mcc->cpu = cpu;
7516#ifdef CONFIG_SCHED_DEBUG
7517 raw_spin_unlock_irqrestore(&mcc->lock, flags);
7518 pr_info("CPU%d: update max cpu_capacity %lu\n", cpu, capacity);
7519 goto skip_unlock;
7520#endif
7521 }
7522 raw_spin_unlock_irqrestore(&mcc->lock, flags);
7523
7524skip_unlock: __attribute__ ((unused));
Nicolas Pitreced549f2014-05-26 18:19:38 -04007525 capacity *= scale_rt_capacity(cpu);
Nicolas Pitreca8ce3d2014-05-26 18:19:39 -04007526 capacity >>= SCHED_CAPACITY_SHIFT;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007527
Nicolas Pitreced549f2014-05-26 18:19:38 -04007528 if (!capacity)
7529 capacity = 1;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007530
Nicolas Pitreced549f2014-05-26 18:19:38 -04007531 cpu_rq(cpu)->cpu_capacity = capacity;
7532 sdg->sgc->capacity = capacity;
Morten Rasmussen5cdeb5f2016-02-25 12:43:49 +00007533 sdg->sgc->max_capacity = capacity;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007534}
7535
Nicolas Pitre63b2ca32014-05-26 18:19:37 -04007536void update_group_capacity(struct sched_domain *sd, int cpu)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007537{
7538 struct sched_domain *child = sd->child;
7539 struct sched_group *group, *sdg = sd->groups;
Morten Rasmussen5cdeb5f2016-02-25 12:43:49 +00007540 unsigned long capacity, max_capacity;
Vincent Guittot4ec44122011-12-12 20:21:08 +01007541 unsigned long interval;
7542
7543 interval = msecs_to_jiffies(sd->balance_interval);
7544 interval = clamp(interval, 1UL, max_load_balance_interval);
Nicolas Pitre63b2ca32014-05-26 18:19:37 -04007545 sdg->sgc->next_update = jiffies + interval;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007546
7547 if (!child) {
Nicolas Pitreced549f2014-05-26 18:19:38 -04007548 update_cpu_capacity(sd, cpu);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007549 return;
7550 }
7551
Vincent Guittotdc7ff762015-03-03 11:35:03 +01007552 capacity = 0;
Morten Rasmussen5cdeb5f2016-02-25 12:43:49 +00007553 max_capacity = 0;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007554
Peter Zijlstra74a5ce22012-05-23 18:00:43 +02007555 if (child->flags & SD_OVERLAP) {
7556 /*
7557 * SD_OVERLAP domains cannot assume that child groups
7558 * span the current group.
7559 */
7560
Peter Zijlstra863bffc2013-08-28 11:44:39 +02007561 for_each_cpu(cpu, sched_group_cpus(sdg)) {
Nicolas Pitre63b2ca32014-05-26 18:19:37 -04007562 struct sched_group_capacity *sgc;
Srikar Dronamraju9abf24d2013-11-12 22:11:26 +05307563 struct rq *rq = cpu_rq(cpu);
Peter Zijlstra863bffc2013-08-28 11:44:39 +02007564
Srikar Dronamraju9abf24d2013-11-12 22:11:26 +05307565 /*
Nicolas Pitre63b2ca32014-05-26 18:19:37 -04007566 * build_sched_domains() -> init_sched_groups_capacity()
Srikar Dronamraju9abf24d2013-11-12 22:11:26 +05307567 * gets here before we've attached the domains to the
7568 * runqueues.
7569 *
Nicolas Pitreced549f2014-05-26 18:19:38 -04007570 * Use capacity_of(), which is set irrespective of domains
7571 * in update_cpu_capacity().
Srikar Dronamraju9abf24d2013-11-12 22:11:26 +05307572 *
Vincent Guittotdc7ff762015-03-03 11:35:03 +01007573 * This avoids capacity from being 0 and
Srikar Dronamraju9abf24d2013-11-12 22:11:26 +05307574 * causing divide-by-zero issues on boot.
Srikar Dronamraju9abf24d2013-11-12 22:11:26 +05307575 */
7576 if (unlikely(!rq->sd)) {
Nicolas Pitreced549f2014-05-26 18:19:38 -04007577 capacity += capacity_of(cpu);
Morten Rasmussen5cdeb5f2016-02-25 12:43:49 +00007578 } else {
7579 sgc = rq->sd->groups->sgc;
7580 capacity += sgc->capacity;
Srikar Dronamraju9abf24d2013-11-12 22:11:26 +05307581 }
7582
Morten Rasmussen5cdeb5f2016-02-25 12:43:49 +00007583 max_capacity = max(capacity, max_capacity);
Peter Zijlstra863bffc2013-08-28 11:44:39 +02007584 }
Peter Zijlstra74a5ce22012-05-23 18:00:43 +02007585 } else {
7586 /*
7587 * !SD_OVERLAP domains can assume that child groups
7588 * span the current group.
Byungchul Park97a71422015-07-05 18:33:48 +09007589 */
Peter Zijlstra74a5ce22012-05-23 18:00:43 +02007590
7591 group = child->groups;
7592 do {
Morten Rasmussen5cdeb5f2016-02-25 12:43:49 +00007593 struct sched_group_capacity *sgc = group->sgc;
7594
7595 capacity += sgc->capacity;
7596 max_capacity = max(sgc->max_capacity, max_capacity);
Peter Zijlstra74a5ce22012-05-23 18:00:43 +02007597 group = group->next;
7598 } while (group != child->groups);
7599 }
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007600
Nicolas Pitre63b2ca32014-05-26 18:19:37 -04007601 sdg->sgc->capacity = capacity;
Morten Rasmussen5cdeb5f2016-02-25 12:43:49 +00007602 sdg->sgc->max_capacity = max_capacity;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007603}
7604
Srivatsa Vaddagiri9d5efe02010-06-08 14:57:02 +10007605/*
Vincent Guittotea678212015-02-27 16:54:11 +01007606 * Check whether the capacity of the rq has been noticeably reduced by side
7607 * activity. The imbalance_pct is used for the threshold.
7608 * Return true is the capacity is reduced
Srivatsa Vaddagiri9d5efe02010-06-08 14:57:02 +10007609 */
7610static inline int
Vincent Guittotea678212015-02-27 16:54:11 +01007611check_cpu_capacity(struct rq *rq, struct sched_domain *sd)
Srivatsa Vaddagiri9d5efe02010-06-08 14:57:02 +10007612{
Vincent Guittotea678212015-02-27 16:54:11 +01007613 return ((rq->cpu_capacity * sd->imbalance_pct) <
7614 (rq->cpu_capacity_orig * 100));
Srivatsa Vaddagiri9d5efe02010-06-08 14:57:02 +10007615}
7616
Peter Zijlstra30ce5da2013-08-15 20:29:29 +02007617/*
7618 * Group imbalance indicates (and tries to solve) the problem where balancing
7619 * groups is inadequate due to tsk_cpus_allowed() constraints.
7620 *
7621 * Imagine a situation of two groups of 4 cpus each and 4 tasks each with a
7622 * cpumask covering 1 cpu of the first group and 3 cpus of the second group.
7623 * Something like:
7624 *
7625 * { 0 1 2 3 } { 4 5 6 7 }
7626 * * * * *
7627 *
7628 * If we were to balance group-wise we'd place two tasks in the first group and
7629 * two tasks in the second group. Clearly this is undesired as it will overload
7630 * cpu 3 and leave one of the cpus in the second group unused.
7631 *
7632 * The current solution to this issue is detecting the skew in the first group
Peter Zijlstra62633222013-08-19 12:41:09 +02007633 * by noticing the lower domain failed to reach balance and had difficulty
7634 * moving tasks due to affinity constraints.
Peter Zijlstra30ce5da2013-08-15 20:29:29 +02007635 *
7636 * When this is so detected; this group becomes a candidate for busiest; see
Kamalesh Babulaled1b7732013-10-13 23:06:15 +05307637 * update_sd_pick_busiest(). And calculate_imbalance() and
Peter Zijlstra62633222013-08-19 12:41:09 +02007638 * find_busiest_group() avoid some of the usual balance conditions to allow it
Peter Zijlstra30ce5da2013-08-15 20:29:29 +02007639 * to create an effective group imbalance.
7640 *
7641 * This is a somewhat tricky proposition since the next run might not find the
7642 * group imbalance and decide the groups need to be balanced again. A most
7643 * subtle and fragile situation.
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007644 */
Peter Zijlstra30ce5da2013-08-15 20:29:29 +02007645
Peter Zijlstra62633222013-08-19 12:41:09 +02007646static inline int sg_imbalanced(struct sched_group *group)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007647{
Nicolas Pitre63b2ca32014-05-26 18:19:37 -04007648 return group->sgc->imbalance;
Peter Zijlstra30ce5da2013-08-15 20:29:29 +02007649}
7650
Peter Zijlstrab37d9312013-08-28 11:50:34 +02007651/*
Vincent Guittotea678212015-02-27 16:54:11 +01007652 * group_has_capacity returns true if the group has spare capacity that could
7653 * be used by some tasks.
7654 * We consider that a group has spare capacity if the * number of task is
Dietmar Eggemann9e91d612015-08-14 17:23:12 +01007655 * smaller than the number of CPUs or if the utilization is lower than the
7656 * available capacity for CFS tasks.
Vincent Guittotea678212015-02-27 16:54:11 +01007657 * For the latter, we use a threshold to stabilize the state, to take into
7658 * account the variance of the tasks' load and to return true if the available
7659 * capacity in meaningful for the load balancer.
7660 * As an example, an available capacity of 1% can appear but it doesn't make
7661 * any benefit for the load balance.
Peter Zijlstrab37d9312013-08-28 11:50:34 +02007662 */
Vincent Guittotea678212015-02-27 16:54:11 +01007663static inline bool
7664group_has_capacity(struct lb_env *env, struct sg_lb_stats *sgs)
Peter Zijlstrab37d9312013-08-28 11:50:34 +02007665{
Vincent Guittotea678212015-02-27 16:54:11 +01007666 if (sgs->sum_nr_running < sgs->group_weight)
7667 return true;
Peter Zijlstrab37d9312013-08-28 11:50:34 +02007668
Vincent Guittotea678212015-02-27 16:54:11 +01007669 if ((sgs->group_capacity * 100) >
Dietmar Eggemann9e91d612015-08-14 17:23:12 +01007670 (sgs->group_util * env->sd->imbalance_pct))
Vincent Guittotea678212015-02-27 16:54:11 +01007671 return true;
Peter Zijlstrab37d9312013-08-28 11:50:34 +02007672
Vincent Guittotea678212015-02-27 16:54:11 +01007673 return false;
Peter Zijlstrab37d9312013-08-28 11:50:34 +02007674}
7675
Vincent Guittotea678212015-02-27 16:54:11 +01007676/*
7677 * group_is_overloaded returns true if the group has more tasks than it can
7678 * handle.
7679 * group_is_overloaded is not equals to !group_has_capacity because a group
7680 * with the exact right number of tasks, has no more spare capacity but is not
7681 * overloaded so both group_has_capacity and group_is_overloaded return
7682 * false.
7683 */
7684static inline bool
7685group_is_overloaded(struct lb_env *env, struct sg_lb_stats *sgs)
Rik van Rielcaeb1782014-07-28 14:16:28 -04007686{
Vincent Guittotea678212015-02-27 16:54:11 +01007687 if (sgs->sum_nr_running <= sgs->group_weight)
7688 return false;
7689
7690 if ((sgs->group_capacity * 100) <
Dietmar Eggemann9e91d612015-08-14 17:23:12 +01007691 (sgs->group_util * env->sd->imbalance_pct))
Vincent Guittotea678212015-02-27 16:54:11 +01007692 return true;
7693
7694 return false;
7695}
7696
Leo Yan79a89f92015-09-15 18:56:45 +08007697static inline enum
7698group_type group_classify(struct sched_group *group,
7699 struct sg_lb_stats *sgs)
Vincent Guittotea678212015-02-27 16:54:11 +01007700{
7701 if (sgs->group_no_capacity)
Rik van Rielcaeb1782014-07-28 14:16:28 -04007702 return group_overloaded;
7703
7704 if (sg_imbalanced(group))
7705 return group_imbalanced;
7706
Morten Rasmussen4c6a8242016-02-25 12:47:54 +00007707 if (sgs->group_misfit_task)
7708 return group_misfit_task;
7709
Rik van Rielcaeb1782014-07-28 14:16:28 -04007710 return group_other;
7711}
7712
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007713/**
7714 * update_sg_lb_stats - Update sched_group's statistics for load balancing.
7715 * @env: The load balancing environment.
7716 * @group: sched_group whose statistics are to be updated.
7717 * @load_idx: Load index of sched_domain of this_cpu for load calc.
7718 * @local_group: Does group contain this_cpu.
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007719 * @sgs: variable to hold the statistics for this group.
Masanari Iidacd3bd4e2014-07-28 12:38:06 +09007720 * @overload: Indicate more than one runnable task for any CPU.
Morten Rasmussena562dfc2015-05-09 16:49:57 +01007721 * @overutilized: Indicate overutilization for any CPU.
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007722 */
7723static inline void update_sg_lb_stats(struct lb_env *env,
7724 struct sched_group *group, int load_idx,
Tim Chen4486edd2014-06-23 12:16:49 -07007725 int local_group, struct sg_lb_stats *sgs,
Morten Rasmussena562dfc2015-05-09 16:49:57 +01007726 bool *overload, bool *overutilized)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007727{
Peter Zijlstra30ce5da2013-08-15 20:29:29 +02007728 unsigned long load;
Waiman Longa426f992015-11-25 14:09:38 -05007729 int i, nr_running;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007730
Peter Zijlstrab72ff132013-08-28 10:32:32 +02007731 memset(sgs, 0, sizeof(*sgs));
7732
Michael Wangb94031302012-07-12 16:10:13 +08007733 for_each_cpu_and(i, sched_group_cpus(group), env->cpus) {
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007734 struct rq *rq = cpu_rq(i);
7735
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007736 /* Bias balancing toward cpus of our domain */
Peter Zijlstra62633222013-08-19 12:41:09 +02007737 if (local_group)
Peter Zijlstra04f733b2012-05-11 00:12:02 +02007738 load = target_load(i, load_idx);
Peter Zijlstra62633222013-08-19 12:41:09 +02007739 else
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007740 load = source_load(i, load_idx);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007741
7742 sgs->group_load += load;
Dietmar Eggemann9e91d612015-08-14 17:23:12 +01007743 sgs->group_util += cpu_util(i);
Vincent Guittot65fdac02014-08-26 13:06:46 +02007744 sgs->sum_nr_running += rq->cfs.h_nr_running;
Tim Chen4486edd2014-06-23 12:16:49 -07007745
Waiman Longa426f992015-11-25 14:09:38 -05007746 nr_running = rq->nr_running;
7747 if (nr_running > 1)
Tim Chen4486edd2014-06-23 12:16:49 -07007748 *overload = true;
7749
Peter Zijlstra0ec8aa02013-10-07 11:29:33 +01007750#ifdef CONFIG_NUMA_BALANCING
7751 sgs->nr_numa_running += rq->nr_numa_running;
7752 sgs->nr_preferred_running += rq->nr_preferred_running;
7753#endif
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007754 sgs->sum_weighted_load += weighted_cpuload(i);
Waiman Longa426f992015-11-25 14:09:38 -05007755 /*
7756 * No need to call idle_cpu() if nr_running is not 0
7757 */
7758 if (!nr_running && idle_cpu(i))
Suresh Siddhaaae6d3d2010-09-17 15:02:32 -07007759 sgs->idle_cpus++;
Morten Rasmussena562dfc2015-05-09 16:49:57 +01007760
Morten Rasmussen4c6a8242016-02-25 12:47:54 +00007761 if (cpu_overutilized(i)) {
Morten Rasmussena562dfc2015-05-09 16:49:57 +01007762 *overutilized = true;
Morten Rasmussen4c6a8242016-02-25 12:47:54 +00007763 if (!sgs->group_misfit_task && rq->misfit_task)
7764 sgs->group_misfit_task = capacity_of(i);
7765 }
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007766 }
7767
Nicolas Pitre63b2ca32014-05-26 18:19:37 -04007768 /* Adjust by relative CPU capacity of the group */
7769 sgs->group_capacity = group->sgc->capacity;
Nicolas Pitreca8ce3d2014-05-26 18:19:39 -04007770 sgs->avg_load = (sgs->group_load*SCHED_CAPACITY_SCALE) / sgs->group_capacity;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007771
Suresh Siddhadd5feea2010-02-23 16:13:52 -08007772 if (sgs->sum_nr_running)
Peter Zijlstra38d0f772013-08-15 19:47:56 +02007773 sgs->load_per_task = sgs->sum_weighted_load / sgs->sum_nr_running;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007774
Suresh Siddhaaae6d3d2010-09-17 15:02:32 -07007775 sgs->group_weight = group->group_weight;
Peter Zijlstrab37d9312013-08-28 11:50:34 +02007776
Vincent Guittotea678212015-02-27 16:54:11 +01007777 sgs->group_no_capacity = group_is_overloaded(env, sgs);
Leo Yan79a89f92015-09-15 18:56:45 +08007778 sgs->group_type = group_classify(group, sgs);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007779}
7780
7781/**
Michael Neuling532cb4c2010-06-08 14:57:02 +10007782 * update_sd_pick_busiest - return 1 on busiest group
Randy Dunlapcd968912012-06-08 13:18:33 -07007783 * @env: The load balancing environment.
Michael Neuling532cb4c2010-06-08 14:57:02 +10007784 * @sds: sched_domain statistics
7785 * @sg: sched_group candidate to be checked for being the busiest
Michael Neulingb6b12292010-06-10 12:06:21 +10007786 * @sgs: sched_group statistics
Michael Neuling532cb4c2010-06-08 14:57:02 +10007787 *
7788 * Determine if @sg is a busier group than the previously selected
7789 * busiest group.
Yacine Belkadie69f6182013-07-12 20:45:47 +02007790 *
7791 * Return: %true if @sg is a busier group than the previously selected
7792 * busiest group. %false otherwise.
Michael Neuling532cb4c2010-06-08 14:57:02 +10007793 */
Peter Zijlstrabd939f42012-05-02 14:20:37 +02007794static bool update_sd_pick_busiest(struct lb_env *env,
Michael Neuling532cb4c2010-06-08 14:57:02 +10007795 struct sd_lb_stats *sds,
7796 struct sched_group *sg,
Peter Zijlstrabd939f42012-05-02 14:20:37 +02007797 struct sg_lb_stats *sgs)
Michael Neuling532cb4c2010-06-08 14:57:02 +10007798{
Rik van Rielcaeb1782014-07-28 14:16:28 -04007799 struct sg_lb_stats *busiest = &sds->busiest_stat;
Michael Neuling532cb4c2010-06-08 14:57:02 +10007800
Rik van Rielcaeb1782014-07-28 14:16:28 -04007801 if (sgs->group_type > busiest->group_type)
Michael Neuling532cb4c2010-06-08 14:57:02 +10007802 return true;
7803
Rik van Rielcaeb1782014-07-28 14:16:28 -04007804 if (sgs->group_type < busiest->group_type)
7805 return false;
7806
7807 if (sgs->avg_load <= busiest->avg_load)
7808 return false;
7809
7810 /* This is the busiest node in its class. */
7811 if (!(env->sd->flags & SD_ASYM_PACKING))
Michael Neuling532cb4c2010-06-08 14:57:02 +10007812 return true;
7813
Srikar Dronamraju1f621e02016-04-06 18:47:40 +05307814 /* No ASYM_PACKING if target cpu is already busy */
7815 if (env->idle == CPU_NOT_IDLE)
7816 return true;
Michael Neuling532cb4c2010-06-08 14:57:02 +10007817 /*
7818 * ASYM_PACKING needs to move all the work to the lowest
7819 * numbered CPUs in the group, therefore mark all groups
7820 * higher than ourself as busy.
7821 */
Rik van Rielcaeb1782014-07-28 14:16:28 -04007822 if (sgs->sum_nr_running && env->dst_cpu < group_first_cpu(sg)) {
Michael Neuling532cb4c2010-06-08 14:57:02 +10007823 if (!sds->busiest)
7824 return true;
7825
Srikar Dronamraju1f621e02016-04-06 18:47:40 +05307826 /* Prefer to move from highest possible cpu's work */
7827 if (group_first_cpu(sds->busiest) < group_first_cpu(sg))
Michael Neuling532cb4c2010-06-08 14:57:02 +10007828 return true;
7829 }
7830
7831 return false;
7832}
7833
Peter Zijlstra0ec8aa02013-10-07 11:29:33 +01007834#ifdef CONFIG_NUMA_BALANCING
7835static inline enum fbq_type fbq_classify_group(struct sg_lb_stats *sgs)
7836{
7837 if (sgs->sum_nr_running > sgs->nr_numa_running)
7838 return regular;
7839 if (sgs->sum_nr_running > sgs->nr_preferred_running)
7840 return remote;
7841 return all;
7842}
7843
7844static inline enum fbq_type fbq_classify_rq(struct rq *rq)
7845{
7846 if (rq->nr_running > rq->nr_numa_running)
7847 return regular;
7848 if (rq->nr_running > rq->nr_preferred_running)
7849 return remote;
7850 return all;
7851}
7852#else
7853static inline enum fbq_type fbq_classify_group(struct sg_lb_stats *sgs)
7854{
7855 return all;
7856}
7857
7858static inline enum fbq_type fbq_classify_rq(struct rq *rq)
7859{
7860 return regular;
7861}
7862#endif /* CONFIG_NUMA_BALANCING */
7863
Michael Neuling532cb4c2010-06-08 14:57:02 +10007864/**
Hui Kang461819a2011-10-11 23:00:59 -04007865 * update_sd_lb_stats - Update sched_domain's statistics for load balancing.
Randy Dunlapcd968912012-06-08 13:18:33 -07007866 * @env: The load balancing environment.
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007867 * @sds: variable to hold the statistics for this sched_domain.
7868 */
Peter Zijlstra0ec8aa02013-10-07 11:29:33 +01007869static inline void update_sd_lb_stats(struct lb_env *env, struct sd_lb_stats *sds)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007870{
Peter Zijlstrabd939f42012-05-02 14:20:37 +02007871 struct sched_domain *child = env->sd->child;
7872 struct sched_group *sg = env->sd->groups;
Joonsoo Kim56cf5152013-08-06 17:36:43 +09007873 struct sg_lb_stats tmp_sgs;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007874 int load_idx, prefer_sibling = 0;
Morten Rasmussena562dfc2015-05-09 16:49:57 +01007875 bool overload = false, overutilized = false;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007876
7877 if (child && child->flags & SD_PREFER_SIBLING)
7878 prefer_sibling = 1;
7879
Peter Zijlstrabd939f42012-05-02 14:20:37 +02007880 load_idx = get_sd_load_idx(env->sd, env->idle);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007881
7882 do {
Joonsoo Kim56cf5152013-08-06 17:36:43 +09007883 struct sg_lb_stats *sgs = &tmp_sgs;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007884 int local_group;
7885
Peter Zijlstrabd939f42012-05-02 14:20:37 +02007886 local_group = cpumask_test_cpu(env->dst_cpu, sched_group_cpus(sg));
Joonsoo Kim56cf5152013-08-06 17:36:43 +09007887 if (local_group) {
7888 sds->local = sg;
7889 sgs = &sds->local_stat;
Peter Zijlstrab72ff132013-08-28 10:32:32 +02007890
7891 if (env->idle != CPU_NEWLY_IDLE ||
Nicolas Pitre63b2ca32014-05-26 18:19:37 -04007892 time_after_eq(jiffies, sg->sgc->next_update))
7893 update_group_capacity(env->sd, env->dst_cpu);
Joonsoo Kim56cf5152013-08-06 17:36:43 +09007894 }
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007895
Tim Chen4486edd2014-06-23 12:16:49 -07007896 update_sg_lb_stats(env, sg, load_idx, local_group, sgs,
Morten Rasmussena562dfc2015-05-09 16:49:57 +01007897 &overload, &overutilized);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007898
Peter Zijlstrab72ff132013-08-28 10:32:32 +02007899 if (local_group)
7900 goto next_group;
7901
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007902 /*
7903 * In case the child domain prefers tasks go to siblings
Vincent Guittotea678212015-02-27 16:54:11 +01007904 * first, lower the sg capacity so that we'll try
Nikhil Rao75dd3212010-10-15 13:12:30 -07007905 * and move all the excess tasks away. We lower the capacity
7906 * of a group only if the local group has the capacity to fit
Vincent Guittotea678212015-02-27 16:54:11 +01007907 * these excess tasks. The extra check prevents the case where
7908 * you always pull from the heaviest group when it is already
7909 * under-utilized (possible with a large weight task outweighs
7910 * the tasks on the system).
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007911 */
Peter Zijlstrab72ff132013-08-28 10:32:32 +02007912 if (prefer_sibling && sds->local &&
Vincent Guittotea678212015-02-27 16:54:11 +01007913 group_has_capacity(env, &sds->local_stat) &&
7914 (sgs->sum_nr_running > 1)) {
7915 sgs->group_no_capacity = 1;
Leo Yan79a89f92015-09-15 18:56:45 +08007916 sgs->group_type = group_classify(sg, sgs);
Wanpeng Licb0b9f22014-11-05 07:44:50 +08007917 }
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007918
Peter Zijlstrab72ff132013-08-28 10:32:32 +02007919 if (update_sd_pick_busiest(env, sds, sg, sgs)) {
Michael Neuling532cb4c2010-06-08 14:57:02 +10007920 sds->busiest = sg;
Joonsoo Kim56cf5152013-08-06 17:36:43 +09007921 sds->busiest_stat = *sgs;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007922 }
7923
Peter Zijlstrab72ff132013-08-28 10:32:32 +02007924next_group:
7925 /* Now, start updating sd_lb_stats */
7926 sds->total_load += sgs->group_load;
Nicolas Pitre63b2ca32014-05-26 18:19:37 -04007927 sds->total_capacity += sgs->group_capacity;
Peter Zijlstrab72ff132013-08-28 10:32:32 +02007928
Michael Neuling532cb4c2010-06-08 14:57:02 +10007929 sg = sg->next;
Peter Zijlstrabd939f42012-05-02 14:20:37 +02007930 } while (sg != env->sd->groups);
Peter Zijlstra0ec8aa02013-10-07 11:29:33 +01007931
7932 if (env->sd->flags & SD_NUMA)
7933 env->fbq_type = fbq_classify_group(&sds->busiest_stat);
Tim Chen4486edd2014-06-23 12:16:49 -07007934
Morten Rasmussen94beeae2015-07-02 17:16:34 +01007935 env->src_grp_nr_running = sds->busiest_stat.sum_nr_running;
7936
Tim Chen4486edd2014-06-23 12:16:49 -07007937 if (!env->sd->parent) {
7938 /* update overload indicator if we are at root domain */
7939 if (env->dst_rq->rd->overload != overload)
7940 env->dst_rq->rd->overload = overload;
Tim Chen4486edd2014-06-23 12:16:49 -07007941
Morten Rasmussena562dfc2015-05-09 16:49:57 +01007942 /* Update over-utilization (tipping point, U >= 0) indicator */
7943 if (env->dst_rq->rd->overutilized != overutilized)
7944 env->dst_rq->rd->overutilized = overutilized;
7945 } else {
7946 if (!env->dst_rq->rd->overutilized && overutilized)
7947 env->dst_rq->rd->overutilized = true;
7948 }
Michael Neuling532cb4c2010-06-08 14:57:02 +10007949}
7950
Michael Neuling532cb4c2010-06-08 14:57:02 +10007951/**
7952 * check_asym_packing - Check to see if the group is packed into the
7953 * sched doman.
7954 *
7955 * This is primarily intended to used at the sibling level. Some
7956 * cores like POWER7 prefer to use lower numbered SMT threads. In the
7957 * case of POWER7, it can move to lower SMT modes only when higher
7958 * threads are idle. When in lower SMT modes, the threads will
7959 * perform better since they share less core resources. Hence when we
7960 * have idle threads, we want them to be the higher ones.
7961 *
7962 * This packing function is run on idle threads. It checks to see if
7963 * the busiest CPU in this domain (core in the P7 case) has a higher
7964 * CPU number than the packing function is being run on. Here we are
7965 * assuming lower CPU number will be equivalent to lower a SMT thread
7966 * number.
7967 *
Yacine Belkadie69f6182013-07-12 20:45:47 +02007968 * Return: 1 when packing is required and a task should be moved to
Michael Neulingb6b12292010-06-10 12:06:21 +10007969 * this CPU. The amount of the imbalance is returned in *imbalance.
7970 *
Randy Dunlapcd968912012-06-08 13:18:33 -07007971 * @env: The load balancing environment.
Michael Neuling532cb4c2010-06-08 14:57:02 +10007972 * @sds: Statistics of the sched_domain which is to be packed
Michael Neuling532cb4c2010-06-08 14:57:02 +10007973 */
Peter Zijlstrabd939f42012-05-02 14:20:37 +02007974static int check_asym_packing(struct lb_env *env, struct sd_lb_stats *sds)
Michael Neuling532cb4c2010-06-08 14:57:02 +10007975{
7976 int busiest_cpu;
7977
Peter Zijlstrabd939f42012-05-02 14:20:37 +02007978 if (!(env->sd->flags & SD_ASYM_PACKING))
Michael Neuling532cb4c2010-06-08 14:57:02 +10007979 return 0;
7980
Srikar Dronamraju1f621e02016-04-06 18:47:40 +05307981 if (env->idle == CPU_NOT_IDLE)
7982 return 0;
7983
Michael Neuling532cb4c2010-06-08 14:57:02 +10007984 if (!sds->busiest)
7985 return 0;
7986
7987 busiest_cpu = group_first_cpu(sds->busiest);
Peter Zijlstrabd939f42012-05-02 14:20:37 +02007988 if (env->dst_cpu > busiest_cpu)
Michael Neuling532cb4c2010-06-08 14:57:02 +10007989 return 0;
7990
Peter Zijlstrabd939f42012-05-02 14:20:37 +02007991 env->imbalance = DIV_ROUND_CLOSEST(
Nicolas Pitre63b2ca32014-05-26 18:19:37 -04007992 sds->busiest_stat.avg_load * sds->busiest_stat.group_capacity,
Nicolas Pitreca8ce3d2014-05-26 18:19:39 -04007993 SCHED_CAPACITY_SCALE);
Peter Zijlstrabd939f42012-05-02 14:20:37 +02007994
Michael Neuling532cb4c2010-06-08 14:57:02 +10007995 return 1;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007996}
7997
7998/**
7999 * fix_small_imbalance - Calculate the minor imbalance that exists
8000 * amongst the groups of a sched_domain, during
8001 * load balancing.
Randy Dunlapcd968912012-06-08 13:18:33 -07008002 * @env: The load balancing environment.
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008003 * @sds: Statistics of the sched_domain whose imbalance is to be calculated.
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008004 */
Peter Zijlstrabd939f42012-05-02 14:20:37 +02008005static inline
8006void fix_small_imbalance(struct lb_env *env, struct sd_lb_stats *sds)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008007{
Nicolas Pitre63b2ca32014-05-26 18:19:37 -04008008 unsigned long tmp, capa_now = 0, capa_move = 0;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008009 unsigned int imbn = 2;
Suresh Siddhadd5feea2010-02-23 16:13:52 -08008010 unsigned long scaled_busy_load_per_task;
Joonsoo Kim56cf5152013-08-06 17:36:43 +09008011 struct sg_lb_stats *local, *busiest;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008012
Joonsoo Kim56cf5152013-08-06 17:36:43 +09008013 local = &sds->local_stat;
8014 busiest = &sds->busiest_stat;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008015
Joonsoo Kim56cf5152013-08-06 17:36:43 +09008016 if (!local->sum_nr_running)
8017 local->load_per_task = cpu_avg_load_per_task(env->dst_cpu);
8018 else if (busiest->load_per_task > local->load_per_task)
8019 imbn = 1;
Suresh Siddhadd5feea2010-02-23 16:13:52 -08008020
Joonsoo Kim56cf5152013-08-06 17:36:43 +09008021 scaled_busy_load_per_task =
Nicolas Pitreca8ce3d2014-05-26 18:19:39 -04008022 (busiest->load_per_task * SCHED_CAPACITY_SCALE) /
Nicolas Pitre63b2ca32014-05-26 18:19:37 -04008023 busiest->group_capacity;
Joonsoo Kim56cf5152013-08-06 17:36:43 +09008024
Vladimir Davydov3029ede2013-09-15 17:49:14 +04008025 if (busiest->avg_load + scaled_busy_load_per_task >=
8026 local->avg_load + (scaled_busy_load_per_task * imbn)) {
Joonsoo Kim56cf5152013-08-06 17:36:43 +09008027 env->imbalance = busiest->load_per_task;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008028 return;
8029 }
8030
8031 /*
8032 * OK, we don't have enough imbalance to justify moving tasks,
Nicolas Pitreced549f2014-05-26 18:19:38 -04008033 * however we may be able to increase total CPU capacity used by
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008034 * moving them.
8035 */
8036
Nicolas Pitre63b2ca32014-05-26 18:19:37 -04008037 capa_now += busiest->group_capacity *
Joonsoo Kim56cf5152013-08-06 17:36:43 +09008038 min(busiest->load_per_task, busiest->avg_load);
Nicolas Pitre63b2ca32014-05-26 18:19:37 -04008039 capa_now += local->group_capacity *
Joonsoo Kim56cf5152013-08-06 17:36:43 +09008040 min(local->load_per_task, local->avg_load);
Nicolas Pitreca8ce3d2014-05-26 18:19:39 -04008041 capa_now /= SCHED_CAPACITY_SCALE;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008042
8043 /* Amount of load we'd subtract */
Vincent Guittota2cd4262014-03-11 17:26:06 +01008044 if (busiest->avg_load > scaled_busy_load_per_task) {
Nicolas Pitre63b2ca32014-05-26 18:19:37 -04008045 capa_move += busiest->group_capacity *
Joonsoo Kim56cf5152013-08-06 17:36:43 +09008046 min(busiest->load_per_task,
Vincent Guittota2cd4262014-03-11 17:26:06 +01008047 busiest->avg_load - scaled_busy_load_per_task);
Joonsoo Kim56cf5152013-08-06 17:36:43 +09008048 }
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008049
8050 /* Amount of load we'd add */
Nicolas Pitre63b2ca32014-05-26 18:19:37 -04008051 if (busiest->avg_load * busiest->group_capacity <
Nicolas Pitreca8ce3d2014-05-26 18:19:39 -04008052 busiest->load_per_task * SCHED_CAPACITY_SCALE) {
Nicolas Pitre63b2ca32014-05-26 18:19:37 -04008053 tmp = (busiest->avg_load * busiest->group_capacity) /
8054 local->group_capacity;
Joonsoo Kim56cf5152013-08-06 17:36:43 +09008055 } else {
Nicolas Pitreca8ce3d2014-05-26 18:19:39 -04008056 tmp = (busiest->load_per_task * SCHED_CAPACITY_SCALE) /
Nicolas Pitre63b2ca32014-05-26 18:19:37 -04008057 local->group_capacity;
Joonsoo Kim56cf5152013-08-06 17:36:43 +09008058 }
Nicolas Pitre63b2ca32014-05-26 18:19:37 -04008059 capa_move += local->group_capacity *
Peter Zijlstra3ae11c92013-08-15 20:37:48 +02008060 min(local->load_per_task, local->avg_load + tmp);
Nicolas Pitreca8ce3d2014-05-26 18:19:39 -04008061 capa_move /= SCHED_CAPACITY_SCALE;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008062
8063 /* Move if we gain throughput */
Nicolas Pitre63b2ca32014-05-26 18:19:37 -04008064 if (capa_move > capa_now)
Joonsoo Kim56cf5152013-08-06 17:36:43 +09008065 env->imbalance = busiest->load_per_task;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008066}
8067
8068/**
8069 * calculate_imbalance - Calculate the amount of imbalance present within the
8070 * groups of a given sched_domain during load balance.
Peter Zijlstrabd939f42012-05-02 14:20:37 +02008071 * @env: load balance environment
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008072 * @sds: statistics of the sched_domain whose imbalance is to be calculated.
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008073 */
Peter Zijlstrabd939f42012-05-02 14:20:37 +02008074static inline void calculate_imbalance(struct lb_env *env, struct sd_lb_stats *sds)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008075{
Suresh Siddhadd5feea2010-02-23 16:13:52 -08008076 unsigned long max_pull, load_above_capacity = ~0UL;
Joonsoo Kim56cf5152013-08-06 17:36:43 +09008077 struct sg_lb_stats *local, *busiest;
Suresh Siddhadd5feea2010-02-23 16:13:52 -08008078
Joonsoo Kim56cf5152013-08-06 17:36:43 +09008079 local = &sds->local_stat;
Joonsoo Kim56cf5152013-08-06 17:36:43 +09008080 busiest = &sds->busiest_stat;
Joonsoo Kim56cf5152013-08-06 17:36:43 +09008081
Rik van Rielcaeb1782014-07-28 14:16:28 -04008082 if (busiest->group_type == group_imbalanced) {
Peter Zijlstra30ce5da2013-08-15 20:29:29 +02008083 /*
8084 * In the group_imb case we cannot rely on group-wide averages
8085 * to ensure cpu-load equilibrium, look at wider averages. XXX
8086 */
Joonsoo Kim56cf5152013-08-06 17:36:43 +09008087 busiest->load_per_task =
8088 min(busiest->load_per_task, sds->avg_load);
Suresh Siddhadd5feea2010-02-23 16:13:52 -08008089 }
8090
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008091 /*
Dietmar Eggemann885e5422016-04-29 20:32:39 +01008092 * Avg load of busiest sg can be less and avg load of local sg can
8093 * be greater than avg load across all sgs of sd because avg load
8094 * factors in sg capacity and sgs with smaller group_type are
8095 * skipped when updating the busiest sg:
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008096 */
Vladimir Davydovb1885552013-09-15 17:49:13 +04008097 if (busiest->avg_load <= sds->avg_load ||
8098 local->avg_load >= sds->avg_load) {
Peter Zijlstrabd939f42012-05-02 14:20:37 +02008099 env->imbalance = 0;
8100 return fix_small_imbalance(env, sds);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008101 }
8102
Peter Zijlstra9a5d9ba2014-07-29 17:15:11 +02008103 /*
8104 * If there aren't any idle cpus, avoid creating some.
8105 */
8106 if (busiest->group_type == group_overloaded &&
8107 local->group_type == group_overloaded) {
Peter Zijlstra1be0eb22016-05-06 12:21:23 +02008108 load_above_capacity = busiest->sum_nr_running * SCHED_CAPACITY_SCALE;
Morten Rasmussencfa10332016-04-29 20:32:40 +01008109 if (load_above_capacity > busiest->group_capacity) {
Vincent Guittotea678212015-02-27 16:54:11 +01008110 load_above_capacity -= busiest->group_capacity;
Dietmar Eggemann26656212016-08-10 11:27:27 +01008111 load_above_capacity *= scale_load_down(NICE_0_LOAD);
Morten Rasmussencfa10332016-04-29 20:32:40 +01008112 load_above_capacity /= busiest->group_capacity;
8113 } else
Vincent Guittotea678212015-02-27 16:54:11 +01008114 load_above_capacity = ~0UL;
Suresh Siddhadd5feea2010-02-23 16:13:52 -08008115 }
8116
8117 /*
8118 * We're trying to get all the cpus to the average_load, so we don't
8119 * want to push ourselves above the average load, nor do we wish to
8120 * reduce the max loaded cpu below the average load. At the same time,
Dietmar Eggemann0a9b23c2016-04-29 20:32:38 +01008121 * we also don't want to reduce the group load below the group
8122 * capacity. Thus we look for the minimum possible imbalance.
Suresh Siddhadd5feea2010-02-23 16:13:52 -08008123 */
Peter Zijlstra30ce5da2013-08-15 20:29:29 +02008124 max_pull = min(busiest->avg_load - sds->avg_load, load_above_capacity);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008125
8126 /* How much load to actually move to equalise the imbalance */
Joonsoo Kim56cf5152013-08-06 17:36:43 +09008127 env->imbalance = min(
Nicolas Pitre63b2ca32014-05-26 18:19:37 -04008128 max_pull * busiest->group_capacity,
8129 (sds->avg_load - local->avg_load) * local->group_capacity
Nicolas Pitreca8ce3d2014-05-26 18:19:39 -04008130 ) / SCHED_CAPACITY_SCALE;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008131
8132 /*
8133 * if *imbalance is less than the average load per runnable task
Lucas De Marchi25985ed2011-03-30 22:57:33 -03008134 * there is no guarantee that any tasks will be moved so we'll have
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008135 * a think about bumping its value to force at least one task to be
8136 * moved
8137 */
Joonsoo Kim56cf5152013-08-06 17:36:43 +09008138 if (env->imbalance < busiest->load_per_task)
Peter Zijlstrabd939f42012-05-02 14:20:37 +02008139 return fix_small_imbalance(env, sds);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008140}
Nikhil Raofab47622010-10-15 13:12:29 -07008141
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008142/******* find_busiest_group() helpers end here *********************/
8143
8144/**
8145 * find_busiest_group - Returns the busiest group within the sched_domain
Dietmar Eggemann0a9b23c2016-04-29 20:32:38 +01008146 * if there is an imbalance.
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008147 *
8148 * Also calculates the amount of weighted load which should be moved
8149 * to restore balance.
8150 *
Randy Dunlapcd968912012-06-08 13:18:33 -07008151 * @env: The load balancing environment.
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008152 *
Yacine Belkadie69f6182013-07-12 20:45:47 +02008153 * Return: - The busiest group if imbalance exists.
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008154 */
Joonsoo Kim56cf5152013-08-06 17:36:43 +09008155static struct sched_group *find_busiest_group(struct lb_env *env)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008156{
Joonsoo Kim56cf5152013-08-06 17:36:43 +09008157 struct sg_lb_stats *local, *busiest;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008158 struct sd_lb_stats sds;
8159
Peter Zijlstra147c5fc22013-08-19 15:22:57 +02008160 init_sd_lb_stats(&sds);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008161
8162 /*
8163 * Compute the various statistics relavent for load balancing at
8164 * this level.
8165 */
Joonsoo Kim23f0d202013-08-06 17:36:42 +09008166 update_sd_lb_stats(env, &sds);
Dietmar Eggemann53065e82015-05-10 15:17:32 +01008167
8168 if (energy_aware() && !env->dst_rq->rd->overutilized)
8169 goto out_balanced;
8170
Joonsoo Kim56cf5152013-08-06 17:36:43 +09008171 local = &sds.local_stat;
8172 busiest = &sds.busiest_stat;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008173
Vincent Guittotea678212015-02-27 16:54:11 +01008174 /* ASYM feature bypasses nice load balance check */
Srikar Dronamraju1f621e02016-04-06 18:47:40 +05308175 if (check_asym_packing(env, &sds))
Michael Neuling532cb4c2010-06-08 14:57:02 +10008176 return sds.busiest;
8177
Peter Zijlstracc57aa82011-02-21 18:55:32 +01008178 /* There is no busy sibling group to pull tasks from */
Joonsoo Kim56cf5152013-08-06 17:36:43 +09008179 if (!sds.busiest || busiest->sum_nr_running == 0)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008180 goto out_balanced;
8181
Nicolas Pitreca8ce3d2014-05-26 18:19:39 -04008182 sds.avg_load = (SCHED_CAPACITY_SCALE * sds.total_load)
8183 / sds.total_capacity;
Ken Chenb0432d82011-04-07 17:23:22 -07008184
Peter Zijlstra866ab432011-02-21 18:56:47 +01008185 /*
8186 * If the busiest group is imbalanced the below checks don't
Peter Zijlstra30ce5da2013-08-15 20:29:29 +02008187 * work because they assume all things are equal, which typically
Peter Zijlstra866ab432011-02-21 18:56:47 +01008188 * isn't true due to cpus_allowed constraints and the like.
8189 */
Rik van Rielcaeb1782014-07-28 14:16:28 -04008190 if (busiest->group_type == group_imbalanced)
Peter Zijlstra866ab432011-02-21 18:56:47 +01008191 goto force_balance;
8192
Peter Zijlstracc57aa82011-02-21 18:55:32 +01008193 /* SD_BALANCE_NEWIDLE trumps SMP nice when underutilized */
Vincent Guittotea678212015-02-27 16:54:11 +01008194 if (env->idle == CPU_NEWLY_IDLE && group_has_capacity(env, local) &&
8195 busiest->group_no_capacity)
Nikhil Raofab47622010-10-15 13:12:29 -07008196 goto force_balance;
8197
Peter Zijlstracc57aa82011-02-21 18:55:32 +01008198 /*
Zhihui Zhang9c58c792014-09-20 21:24:36 -04008199 * If the local group is busier than the selected busiest group
Peter Zijlstracc57aa82011-02-21 18:55:32 +01008200 * don't try and pull any tasks.
8201 */
Joonsoo Kim56cf5152013-08-06 17:36:43 +09008202 if (local->avg_load >= busiest->avg_load)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008203 goto out_balanced;
8204
Peter Zijlstracc57aa82011-02-21 18:55:32 +01008205 /*
8206 * Don't pull any tasks if this group is already above the domain
8207 * average load.
8208 */
Joonsoo Kim56cf5152013-08-06 17:36:43 +09008209 if (local->avg_load >= sds.avg_load)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008210 goto out_balanced;
8211
Peter Zijlstrabd939f42012-05-02 14:20:37 +02008212 if (env->idle == CPU_IDLE) {
Suresh Siddhaaae6d3d2010-09-17 15:02:32 -07008213 /*
Vincent Guittot43f4d662014-10-01 15:38:55 +02008214 * This cpu is idle. If the busiest group is not overloaded
8215 * and there is no imbalance between this and busiest group
8216 * wrt idle cpus, it is balanced. The imbalance becomes
8217 * significant if the diff is greater than 1 otherwise we
8218 * might end up to just move the imbalance on another group
Suresh Siddhaaae6d3d2010-09-17 15:02:32 -07008219 */
Vincent Guittot43f4d662014-10-01 15:38:55 +02008220 if ((busiest->group_type != group_overloaded) &&
8221 (local->idle_cpus <= (busiest->idle_cpus + 1)))
Suresh Siddhaaae6d3d2010-09-17 15:02:32 -07008222 goto out_balanced;
Peter Zijlstrac186faf2011-02-21 18:52:53 +01008223 } else {
8224 /*
8225 * In the CPU_NEWLY_IDLE, CPU_NOT_IDLE cases, use
8226 * imbalance_pct to be conservative.
8227 */
Joonsoo Kim56cf5152013-08-06 17:36:43 +09008228 if (100 * busiest->avg_load <=
8229 env->sd->imbalance_pct * local->avg_load)
Peter Zijlstrac186faf2011-02-21 18:52:53 +01008230 goto out_balanced;
Suresh Siddhaaae6d3d2010-09-17 15:02:32 -07008231 }
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008232
Nikhil Raofab47622010-10-15 13:12:29 -07008233force_balance:
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008234 /* Looks like there is an imbalance. Compute it */
Peter Zijlstrabd939f42012-05-02 14:20:37 +02008235 calculate_imbalance(env, &sds);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008236 return sds.busiest;
8237
8238out_balanced:
Peter Zijlstrabd939f42012-05-02 14:20:37 +02008239 env->imbalance = 0;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008240 return NULL;
8241}
8242
8243/*
8244 * find_busiest_queue - find the busiest runqueue among the cpus in group.
8245 */
Peter Zijlstrabd939f42012-05-02 14:20:37 +02008246static struct rq *find_busiest_queue(struct lb_env *env,
Michael Wangb94031302012-07-12 16:10:13 +08008247 struct sched_group *group)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008248{
8249 struct rq *busiest = NULL, *rq;
Nicolas Pitreced549f2014-05-26 18:19:38 -04008250 unsigned long busiest_load = 0, busiest_capacity = 1;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008251 int i;
8252
Peter Zijlstra6906a402013-08-19 15:20:21 +02008253 for_each_cpu_and(i, sched_group_cpus(group), env->cpus) {
Vincent Guittotea678212015-02-27 16:54:11 +01008254 unsigned long capacity, wl;
Peter Zijlstra0ec8aa02013-10-07 11:29:33 +01008255 enum fbq_type rt;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008256
Peter Zijlstra0ec8aa02013-10-07 11:29:33 +01008257 rq = cpu_rq(i);
8258 rt = fbq_classify_rq(rq);
8259
8260 /*
8261 * We classify groups/runqueues into three groups:
8262 * - regular: there are !numa tasks
8263 * - remote: there are numa tasks that run on the 'wrong' node
8264 * - all: there is no distinction
8265 *
8266 * In order to avoid migrating ideally placed numa tasks,
8267 * ignore those when there's better options.
8268 *
8269 * If we ignore the actual busiest queue to migrate another
8270 * task, the next balance pass can still reduce the busiest
8271 * queue by moving tasks around inside the node.
8272 *
8273 * If we cannot move enough load due to this classification
8274 * the next pass will adjust the group classification and
8275 * allow migration of more tasks.
8276 *
8277 * Both cases only affect the total convergence complexity.
8278 */
8279 if (rt > env->fbq_type)
8280 continue;
8281
Nicolas Pitreced549f2014-05-26 18:19:38 -04008282 capacity = capacity_of(i);
Srivatsa Vaddagiri9d5efe02010-06-08 14:57:02 +10008283
Thomas Gleixner6e40f5b2010-02-16 16:48:56 +01008284 wl = weighted_cpuload(i);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008285
Thomas Gleixner6e40f5b2010-02-16 16:48:56 +01008286 /*
8287 * When comparing with imbalance, use weighted_cpuload()
Nicolas Pitreced549f2014-05-26 18:19:38 -04008288 * which is not scaled with the cpu capacity.
Thomas Gleixner6e40f5b2010-02-16 16:48:56 +01008289 */
Vincent Guittotea678212015-02-27 16:54:11 +01008290
8291 if (rq->nr_running == 1 && wl > env->imbalance &&
8292 !check_cpu_capacity(rq, env->sd))
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008293 continue;
8294
Thomas Gleixner6e40f5b2010-02-16 16:48:56 +01008295 /*
8296 * For the load comparisons with the other cpu's, consider
Nicolas Pitreced549f2014-05-26 18:19:38 -04008297 * the weighted_cpuload() scaled with the cpu capacity, so
8298 * that the load can be moved away from the cpu that is
8299 * potentially running at a lower capacity.
Joonsoo Kim95a79b82013-08-06 17:36:41 +09008300 *
Nicolas Pitreced549f2014-05-26 18:19:38 -04008301 * Thus we're looking for max(wl_i / capacity_i), crosswise
Joonsoo Kim95a79b82013-08-06 17:36:41 +09008302 * multiplication to rid ourselves of the division works out
Nicolas Pitreced549f2014-05-26 18:19:38 -04008303 * to: wl_i * capacity_j > wl_j * capacity_i; where j is
8304 * our previous maximum.
Thomas Gleixner6e40f5b2010-02-16 16:48:56 +01008305 */
Nicolas Pitreced549f2014-05-26 18:19:38 -04008306 if (wl * busiest_capacity > busiest_load * capacity) {
Joonsoo Kim95a79b82013-08-06 17:36:41 +09008307 busiest_load = wl;
Nicolas Pitreced549f2014-05-26 18:19:38 -04008308 busiest_capacity = capacity;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008309 busiest = rq;
8310 }
8311 }
8312
8313 return busiest;
8314}
8315
8316/*
8317 * Max backoff if we encounter pinned tasks. Pretty arbitrary value, but
8318 * so long as it is large enough.
8319 */
8320#define MAX_PINNED_INTERVAL 512
8321
Peter Zijlstrabd939f42012-05-02 14:20:37 +02008322static int need_active_balance(struct lb_env *env)
Peter Zijlstra1af3ed32009-12-23 15:10:31 +01008323{
Peter Zijlstrabd939f42012-05-02 14:20:37 +02008324 struct sched_domain *sd = env->sd;
8325
8326 if (env->idle == CPU_NEWLY_IDLE) {
Michael Neuling532cb4c2010-06-08 14:57:02 +10008327
8328 /*
8329 * ASYM_PACKING needs to force migrate tasks from busy but
8330 * higher numbered CPUs in order to pack all tasks in the
8331 * lowest numbered CPUs.
8332 */
Peter Zijlstrabd939f42012-05-02 14:20:37 +02008333 if ((sd->flags & SD_ASYM_PACKING) && env->src_cpu > env->dst_cpu)
Michael Neuling532cb4c2010-06-08 14:57:02 +10008334 return 1;
Peter Zijlstra1af3ed32009-12-23 15:10:31 +01008335 }
8336
Vincent Guittot1aaf90a2015-02-27 16:54:14 +01008337 /*
8338 * The dst_cpu is idle and the src_cpu CPU has only 1 CFS task.
8339 * It's worth migrating the task if the src_cpu's capacity is reduced
8340 * because of other sched_class or IRQs if more capacity stays
8341 * available on dst_cpu.
8342 */
8343 if ((env->idle != CPU_NOT_IDLE) &&
8344 (env->src_rq->cfs.h_nr_running == 1)) {
8345 if ((check_cpu_capacity(env->src_rq, sd)) &&
8346 (capacity_of(env->src_cpu)*sd->imbalance_pct < capacity_of(env->dst_cpu)*100))
8347 return 1;
8348 }
8349
Dietmar Eggemann90f309f2015-01-26 19:47:28 +00008350 if ((capacity_of(env->src_cpu) < capacity_of(env->dst_cpu)) &&
8351 env->src_rq->cfs.h_nr_running == 1 &&
8352 cpu_overutilized(env->src_cpu) &&
8353 !cpu_overutilized(env->dst_cpu)) {
8354 return 1;
8355 }
8356
Peter Zijlstra1af3ed32009-12-23 15:10:31 +01008357 return unlikely(sd->nr_balance_failed > sd->cache_nice_tries+2);
8358}
8359
Tejun Heo969c7922010-05-06 18:49:21 +02008360static int active_load_balance_cpu_stop(void *data);
8361
Joonsoo Kim23f0d202013-08-06 17:36:42 +09008362static int should_we_balance(struct lb_env *env)
8363{
8364 struct sched_group *sg = env->sd->groups;
8365 struct cpumask *sg_cpus, *sg_mask;
8366 int cpu, balance_cpu = -1;
8367
8368 /*
8369 * In the newly idle case, we will allow all the cpu's
8370 * to do the newly idle load balance.
8371 */
8372 if (env->idle == CPU_NEWLY_IDLE)
8373 return 1;
8374
8375 sg_cpus = sched_group_cpus(sg);
8376 sg_mask = sched_group_mask(sg);
8377 /* Try to find first idle cpu */
8378 for_each_cpu_and(cpu, sg_cpus, env->cpus) {
8379 if (!cpumask_test_cpu(cpu, sg_mask) || !idle_cpu(cpu))
8380 continue;
8381
8382 balance_cpu = cpu;
8383 break;
8384 }
8385
8386 if (balance_cpu == -1)
8387 balance_cpu = group_balance_cpu(sg);
8388
8389 /*
8390 * First idle cpu or the first cpu(busiest) in this sched group
8391 * is eligible for doing load balancing at this and above domains.
8392 */
Joonsoo Kimb0cff9d2013-09-10 15:54:49 +09008393 return balance_cpu == env->dst_cpu;
Joonsoo Kim23f0d202013-08-06 17:36:42 +09008394}
8395
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008396/*
8397 * Check this_cpu to ensure it is balanced within domain. Attempt to move
8398 * tasks if there is an imbalance.
8399 */
8400static int load_balance(int this_cpu, struct rq *this_rq,
8401 struct sched_domain *sd, enum cpu_idle_type idle,
Joonsoo Kim23f0d202013-08-06 17:36:42 +09008402 int *continue_balancing)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008403{
Srivatsa Vaddagiri88b8dac2012-06-19 17:43:15 +05308404 int ld_moved, cur_ld_moved, active_balance = 0;
Peter Zijlstra62633222013-08-19 12:41:09 +02008405 struct sched_domain *sd_parent = sd->parent;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008406 struct sched_group *group;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008407 struct rq *busiest;
8408 unsigned long flags;
Christoph Lameter4ba29682014-08-26 19:12:21 -05008409 struct cpumask *cpus = this_cpu_cpumask_var_ptr(load_balance_mask);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008410
Peter Zijlstra8e45cb52012-02-22 12:47:19 +01008411 struct lb_env env = {
8412 .sd = sd,
Peter Zijlstraddcdf6e2012-02-22 19:27:40 +01008413 .dst_cpu = this_cpu,
8414 .dst_rq = this_rq,
Srivatsa Vaddagiri88b8dac2012-06-19 17:43:15 +05308415 .dst_grpmask = sched_group_cpus(sd->groups),
Peter Zijlstra8e45cb52012-02-22 12:47:19 +01008416 .idle = idle,
Peter Zijlstraeb953082012-04-17 13:38:40 +02008417 .loop_break = sched_nr_migrate_break,
Michael Wangb94031302012-07-12 16:10:13 +08008418 .cpus = cpus,
Peter Zijlstra0ec8aa02013-10-07 11:29:33 +01008419 .fbq_type = all,
Kirill Tkhai163122b2014-08-20 13:48:29 +04008420 .tasks = LIST_HEAD_INIT(env.tasks),
Peter Zijlstra8e45cb52012-02-22 12:47:19 +01008421 };
8422
Joonsoo Kimcfc03112013-04-23 17:27:39 +09008423 /*
8424 * For NEWLY_IDLE load_balancing, we don't need to consider
8425 * other cpus in our group
8426 */
Joonsoo Kime02e60c2013-04-23 17:27:42 +09008427 if (idle == CPU_NEWLY_IDLE)
Joonsoo Kimcfc03112013-04-23 17:27:39 +09008428 env.dst_grpmask = NULL;
Joonsoo Kimcfc03112013-04-23 17:27:39 +09008429
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008430 cpumask_copy(cpus, cpu_active_mask);
8431
Josh Poimboeufae928822016-06-17 12:43:24 -05008432 schedstat_inc(sd->lb_count[idle]);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008433
8434redo:
Joonsoo Kim23f0d202013-08-06 17:36:42 +09008435 if (!should_we_balance(&env)) {
8436 *continue_balancing = 0;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008437 goto out_balanced;
Joonsoo Kim23f0d202013-08-06 17:36:42 +09008438 }
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008439
Joonsoo Kim23f0d202013-08-06 17:36:42 +09008440 group = find_busiest_group(&env);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008441 if (!group) {
Josh Poimboeufae928822016-06-17 12:43:24 -05008442 schedstat_inc(sd->lb_nobusyg[idle]);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008443 goto out_balanced;
8444 }
8445
Michael Wangb94031302012-07-12 16:10:13 +08008446 busiest = find_busiest_queue(&env, group);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008447 if (!busiest) {
Josh Poimboeufae928822016-06-17 12:43:24 -05008448 schedstat_inc(sd->lb_nobusyq[idle]);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008449 goto out_balanced;
8450 }
8451
Michael Wang78feefc2012-08-06 16:41:59 +08008452 BUG_ON(busiest == env.dst_rq);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008453
Josh Poimboeufae928822016-06-17 12:43:24 -05008454 schedstat_add(sd->lb_imbalance[idle], env.imbalance);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008455
Vincent Guittot1aaf90a2015-02-27 16:54:14 +01008456 env.src_cpu = busiest->cpu;
8457 env.src_rq = busiest;
8458
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008459 ld_moved = 0;
8460 if (busiest->nr_running > 1) {
8461 /*
8462 * Attempt to move tasks. If find_busiest_group has found
8463 * an imbalance but busiest->nr_running <= 1, the group is
8464 * still unbalanced. ld_moved simply stays zero, so it is
8465 * correctly treated as an imbalance.
8466 */
Peter Zijlstra8e45cb52012-02-22 12:47:19 +01008467 env.flags |= LBF_ALL_PINNED;
Peter Zijlstrac82513e2012-04-26 13:12:27 +02008468 env.loop_max = min(sysctl_sched_nr_migrate, busiest->nr_running);
Peter Zijlstra8e45cb52012-02-22 12:47:19 +01008469
Peter Zijlstra5d6523e2012-03-10 00:07:36 +01008470more_balance:
Kirill Tkhai163122b2014-08-20 13:48:29 +04008471 raw_spin_lock_irqsave(&busiest->lock, flags);
Srivatsa Vaddagiri88b8dac2012-06-19 17:43:15 +05308472
8473 /*
8474 * cur_ld_moved - load moved in current iteration
8475 * ld_moved - cumulative load moved across iterations
8476 */
Kirill Tkhai163122b2014-08-20 13:48:29 +04008477 cur_ld_moved = detach_tasks(&env);
Juri Lellic9bf15a2015-06-25 14:37:27 +01008478 /*
8479 * We want to potentially lower env.src_cpu's OPP.
8480 */
8481 if (cur_ld_moved)
8482 update_capacity_of(env.src_cpu);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008483
8484 /*
Kirill Tkhai163122b2014-08-20 13:48:29 +04008485 * We've detached some tasks from busiest_rq. Every
8486 * task is masked "TASK_ON_RQ_MIGRATING", so we can safely
8487 * unlock busiest->lock, and we are able to be sure
8488 * that nobody can manipulate the tasks in parallel.
8489 * See task_rq_lock() family for the details.
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008490 */
Kirill Tkhai163122b2014-08-20 13:48:29 +04008491
8492 raw_spin_unlock(&busiest->lock);
8493
8494 if (cur_ld_moved) {
8495 attach_tasks(&env);
8496 ld_moved += cur_ld_moved;
8497 }
8498
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008499 local_irq_restore(flags);
Srivatsa Vaddagiri88b8dac2012-06-19 17:43:15 +05308500
Joonsoo Kimf1cd0852013-04-23 17:27:37 +09008501 if (env.flags & LBF_NEED_BREAK) {
8502 env.flags &= ~LBF_NEED_BREAK;
8503 goto more_balance;
8504 }
8505
Srivatsa Vaddagiri88b8dac2012-06-19 17:43:15 +05308506 /*
8507 * Revisit (affine) tasks on src_cpu that couldn't be moved to
8508 * us and move them to an alternate dst_cpu in our sched_group
8509 * where they can run. The upper limit on how many times we
8510 * iterate on same src_cpu is dependent on number of cpus in our
8511 * sched_group.
8512 *
8513 * This changes load balance semantics a bit on who can move
8514 * load to a given_cpu. In addition to the given_cpu itself
8515 * (or a ilb_cpu acting on its behalf where given_cpu is
8516 * nohz-idle), we now have balance_cpu in a position to move
8517 * load to given_cpu. In rare situations, this may cause
8518 * conflicts (balance_cpu and given_cpu/ilb_cpu deciding
8519 * _independently_ and at _same_ time to move some load to
8520 * given_cpu) causing exceess load to be moved to given_cpu.
8521 * This however should not happen so much in practice and
8522 * moreover subsequent load balance cycles should correct the
8523 * excess load moved.
8524 */
Peter Zijlstra62633222013-08-19 12:41:09 +02008525 if ((env.flags & LBF_DST_PINNED) && env.imbalance > 0) {
Srivatsa Vaddagiri88b8dac2012-06-19 17:43:15 +05308526
Vladimir Davydov7aff2e32013-09-15 21:30:13 +04008527 /* Prevent to re-select dst_cpu via env's cpus */
8528 cpumask_clear_cpu(env.dst_cpu, env.cpus);
8529
Michael Wang78feefc2012-08-06 16:41:59 +08008530 env.dst_rq = cpu_rq(env.new_dst_cpu);
Srivatsa Vaddagiri88b8dac2012-06-19 17:43:15 +05308531 env.dst_cpu = env.new_dst_cpu;
Peter Zijlstra62633222013-08-19 12:41:09 +02008532 env.flags &= ~LBF_DST_PINNED;
Srivatsa Vaddagiri88b8dac2012-06-19 17:43:15 +05308533 env.loop = 0;
8534 env.loop_break = sched_nr_migrate_break;
Joonsoo Kime02e60c2013-04-23 17:27:42 +09008535
Srivatsa Vaddagiri88b8dac2012-06-19 17:43:15 +05308536 /*
8537 * Go back to "more_balance" rather than "redo" since we
8538 * need to continue with same src_cpu.
8539 */
8540 goto more_balance;
8541 }
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008542
Peter Zijlstra62633222013-08-19 12:41:09 +02008543 /*
8544 * We failed to reach balance because of affinity.
8545 */
8546 if (sd_parent) {
Nicolas Pitre63b2ca32014-05-26 18:19:37 -04008547 int *group_imbalance = &sd_parent->groups->sgc->imbalance;
Peter Zijlstra62633222013-08-19 12:41:09 +02008548
Vincent Guittotafdeee02014-08-26 13:06:44 +02008549 if ((env.flags & LBF_SOME_PINNED) && env.imbalance > 0)
Peter Zijlstra62633222013-08-19 12:41:09 +02008550 *group_imbalance = 1;
Peter Zijlstra62633222013-08-19 12:41:09 +02008551 }
8552
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008553 /* All tasks on this runqueue were pinned by CPU affinity */
Peter Zijlstra8e45cb52012-02-22 12:47:19 +01008554 if (unlikely(env.flags & LBF_ALL_PINNED)) {
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008555 cpumask_clear_cpu(cpu_of(busiest), cpus);
Prashanth Nageshappabbf18b12012-06-19 17:52:07 +05308556 if (!cpumask_empty(cpus)) {
8557 env.loop = 0;
8558 env.loop_break = sched_nr_migrate_break;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008559 goto redo;
Prashanth Nageshappabbf18b12012-06-19 17:52:07 +05308560 }
Vincent Guittotafdeee02014-08-26 13:06:44 +02008561 goto out_all_pinned;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008562 }
8563 }
8564
8565 if (!ld_moved) {
Josh Poimboeufae928822016-06-17 12:43:24 -05008566 schedstat_inc(sd->lb_failed[idle]);
Venkatesh Pallipadi58b26c42010-09-10 18:19:17 -07008567 /*
8568 * Increment the failure counter only on periodic balance.
8569 * We do not want newidle balance, which can be very
8570 * frequent, pollute the failure counter causing
8571 * excessive cache_hot migrations and active balances.
8572 */
8573 if (idle != CPU_NEWLY_IDLE)
Morten Rasmussen94beeae2015-07-02 17:16:34 +01008574 if (env.src_grp_nr_running > 1)
8575 sd->nr_balance_failed++;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008576
Peter Zijlstrabd939f42012-05-02 14:20:37 +02008577 if (need_active_balance(&env)) {
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008578 raw_spin_lock_irqsave(&busiest->lock, flags);
8579
Tejun Heo969c7922010-05-06 18:49:21 +02008580 /* don't kick the active_load_balance_cpu_stop,
8581 * if the curr task on busiest cpu can't be
8582 * moved to this_cpu
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008583 */
8584 if (!cpumask_test_cpu(this_cpu,
Peter Zijlstrafa17b502011-06-16 12:23:22 +02008585 tsk_cpus_allowed(busiest->curr))) {
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008586 raw_spin_unlock_irqrestore(&busiest->lock,
8587 flags);
Peter Zijlstra8e45cb52012-02-22 12:47:19 +01008588 env.flags |= LBF_ALL_PINNED;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008589 goto out_one_pinned;
8590 }
8591
Tejun Heo969c7922010-05-06 18:49:21 +02008592 /*
8593 * ->active_balance synchronizes accesses to
8594 * ->active_balance_work. Once set, it's cleared
8595 * only after active load balance is finished.
8596 */
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008597 if (!busiest->active_balance) {
8598 busiest->active_balance = 1;
8599 busiest->push_cpu = this_cpu;
8600 active_balance = 1;
8601 }
8602 raw_spin_unlock_irqrestore(&busiest->lock, flags);
Tejun Heo969c7922010-05-06 18:49:21 +02008603
Peter Zijlstrabd939f42012-05-02 14:20:37 +02008604 if (active_balance) {
Tejun Heo969c7922010-05-06 18:49:21 +02008605 stop_one_cpu_nowait(cpu_of(busiest),
8606 active_load_balance_cpu_stop, busiest,
8607 &busiest->active_balance_work);
Peter Zijlstrabd939f42012-05-02 14:20:37 +02008608 }
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008609
Srikar Dronamrajud02c071182016-03-23 17:54:44 +05308610 /* We've kicked active balancing, force task migration. */
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008611 sd->nr_balance_failed = sd->cache_nice_tries+1;
8612 }
8613 } else
8614 sd->nr_balance_failed = 0;
8615
8616 if (likely(!active_balance)) {
8617 /* We were unbalanced, so reset the balancing interval */
8618 sd->balance_interval = sd->min_interval;
8619 } else {
8620 /*
8621 * If we've begun active balancing, start to back off. This
8622 * case may not be covered by the all_pinned logic if there
8623 * is only 1 task on the busy runqueue (because we don't call
Kirill Tkhai163122b2014-08-20 13:48:29 +04008624 * detach_tasks).
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008625 */
8626 if (sd->balance_interval < sd->max_interval)
8627 sd->balance_interval *= 2;
8628 }
8629
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008630 goto out;
8631
8632out_balanced:
Vincent Guittotafdeee02014-08-26 13:06:44 +02008633 /*
8634 * We reach balance although we may have faced some affinity
8635 * constraints. Clear the imbalance flag if it was set.
8636 */
8637 if (sd_parent) {
8638 int *group_imbalance = &sd_parent->groups->sgc->imbalance;
8639
8640 if (*group_imbalance)
8641 *group_imbalance = 0;
8642 }
8643
8644out_all_pinned:
8645 /*
8646 * We reach balance because all tasks are pinned at this level so
8647 * we can't migrate them. Let the imbalance flag set so parent level
8648 * can try to migrate them.
8649 */
Josh Poimboeufae928822016-06-17 12:43:24 -05008650 schedstat_inc(sd->lb_balanced[idle]);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008651
8652 sd->nr_balance_failed = 0;
8653
8654out_one_pinned:
8655 /* tune up the balancing interval */
Peter Zijlstra8e45cb52012-02-22 12:47:19 +01008656 if (((env.flags & LBF_ALL_PINNED) &&
Peter Zijlstra5b54b562011-09-22 15:23:13 +02008657 sd->balance_interval < MAX_PINNED_INTERVAL) ||
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008658 (sd->balance_interval < sd->max_interval))
8659 sd->balance_interval *= 2;
8660
Venkatesh Pallipadi46e49b32011-02-14 14:38:50 -08008661 ld_moved = 0;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008662out:
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008663 return ld_moved;
8664}
8665
Jason Low52a08ef2014-05-08 17:49:22 -07008666static inline unsigned long
8667get_sd_balance_interval(struct sched_domain *sd, int cpu_busy)
8668{
8669 unsigned long interval = sd->balance_interval;
8670
8671 if (cpu_busy)
8672 interval *= sd->busy_factor;
8673
8674 /* scale ms to jiffies */
8675 interval = msecs_to_jiffies(interval);
8676 interval = clamp(interval, 1UL, max_load_balance_interval);
8677
8678 return interval;
8679}
8680
8681static inline void
Leo Yan31851a92016-08-05 14:31:29 +08008682update_next_balance(struct sched_domain *sd, unsigned long *next_balance)
Jason Low52a08ef2014-05-08 17:49:22 -07008683{
8684 unsigned long interval, next;
8685
Leo Yan31851a92016-08-05 14:31:29 +08008686 /* used by idle balance, so cpu_busy = 0 */
8687 interval = get_sd_balance_interval(sd, 0);
Jason Low52a08ef2014-05-08 17:49:22 -07008688 next = sd->last_balance + interval;
8689
8690 if (time_after(*next_balance, next))
8691 *next_balance = next;
8692}
8693
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008694/*
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008695 * idle_balance is called by schedule() if this_cpu is about to become
8696 * idle. Attempts to pull tasks from other CPUs.
8697 */
Peter Zijlstra6e831252014-02-11 16:11:48 +01008698static int idle_balance(struct rq *this_rq)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008699{
Jason Low52a08ef2014-05-08 17:49:22 -07008700 unsigned long next_balance = jiffies + HZ;
8701 int this_cpu = this_rq->cpu;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008702 struct sched_domain *sd;
8703 int pulled_task = 0;
Jason Low9bd721c2013-09-13 11:26:52 -07008704 u64 curr_cost = 0;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008705
Peter Zijlstra6e831252014-02-11 16:11:48 +01008706 /*
8707 * We must set idle_stamp _before_ calling idle_balance(), such that we
8708 * measure the duration of idle_balance() as idle time.
8709 */
8710 this_rq->idle_stamp = rq_clock(this_rq);
8711
Dietmar Eggemann785367f2016-01-13 15:49:44 +00008712 if (!energy_aware() &&
8713 (this_rq->avg_idle < sysctl_sched_migration_cost ||
8714 !this_rq->rd->overload)) {
Jason Low52a08ef2014-05-08 17:49:22 -07008715 rcu_read_lock();
8716 sd = rcu_dereference_check_sched_domain(this_rq->sd);
8717 if (sd)
Leo Yan31851a92016-08-05 14:31:29 +08008718 update_next_balance(sd, &next_balance);
Jason Low52a08ef2014-05-08 17:49:22 -07008719 rcu_read_unlock();
8720
Peter Zijlstra6e831252014-02-11 16:11:48 +01008721 goto out;
Jason Low52a08ef2014-05-08 17:49:22 -07008722 }
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008723
Peter Zijlstraf492e122009-12-23 15:29:42 +01008724 raw_spin_unlock(&this_rq->lock);
8725
Paul Turner48a16752012-10-04 13:18:31 +02008726 update_blocked_averages(this_cpu);
Peter Zijlstradce840a2011-04-07 14:09:50 +02008727 rcu_read_lock();
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008728 for_each_domain(this_cpu, sd) {
Joonsoo Kim23f0d202013-08-06 17:36:42 +09008729 int continue_balancing = 1;
Jason Low9bd721c2013-09-13 11:26:52 -07008730 u64 t0, domain_cost;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008731
8732 if (!(sd->flags & SD_LOAD_BALANCE))
8733 continue;
8734
Jason Low52a08ef2014-05-08 17:49:22 -07008735 if (this_rq->avg_idle < curr_cost + sd->max_newidle_lb_cost) {
Leo Yan31851a92016-08-05 14:31:29 +08008736 update_next_balance(sd, &next_balance);
Jason Low9bd721c2013-09-13 11:26:52 -07008737 break;
Jason Low52a08ef2014-05-08 17:49:22 -07008738 }
Jason Low9bd721c2013-09-13 11:26:52 -07008739
Peter Zijlstraf492e122009-12-23 15:29:42 +01008740 if (sd->flags & SD_BALANCE_NEWIDLE) {
Jason Low9bd721c2013-09-13 11:26:52 -07008741 t0 = sched_clock_cpu(this_cpu);
8742
Peter Zijlstraf492e122009-12-23 15:29:42 +01008743 pulled_task = load_balance(this_cpu, this_rq,
Joonsoo Kim23f0d202013-08-06 17:36:42 +09008744 sd, CPU_NEWLY_IDLE,
8745 &continue_balancing);
Jason Low9bd721c2013-09-13 11:26:52 -07008746
8747 domain_cost = sched_clock_cpu(this_cpu) - t0;
8748 if (domain_cost > sd->max_newidle_lb_cost)
8749 sd->max_newidle_lb_cost = domain_cost;
8750
8751 curr_cost += domain_cost;
Peter Zijlstraf492e122009-12-23 15:29:42 +01008752 }
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008753
Leo Yan31851a92016-08-05 14:31:29 +08008754 update_next_balance(sd, &next_balance);
Jason Low39a4d9c2014-04-23 18:30:35 -07008755
8756 /*
8757 * Stop searching for tasks to pull if there are
8758 * now runnable tasks on this rq.
8759 */
8760 if (pulled_task || this_rq->nr_running > 0)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008761 break;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008762 }
Peter Zijlstradce840a2011-04-07 14:09:50 +02008763 rcu_read_unlock();
Peter Zijlstraf492e122009-12-23 15:29:42 +01008764
8765 raw_spin_lock(&this_rq->lock);
8766
Jason Low0e5b5332014-04-28 15:45:54 -07008767 if (curr_cost > this_rq->max_idle_balance_cost)
8768 this_rq->max_idle_balance_cost = curr_cost;
8769
Daniel Lezcanoe5fc6612014-01-17 10:04:02 +01008770 /*
Jason Low0e5b5332014-04-28 15:45:54 -07008771 * While browsing the domains, we released the rq lock, a task could
8772 * have been enqueued in the meantime. Since we're not going idle,
8773 * pretend we pulled a task.
Daniel Lezcanoe5fc6612014-01-17 10:04:02 +01008774 */
Jason Low0e5b5332014-04-28 15:45:54 -07008775 if (this_rq->cfs.h_nr_running && !pulled_task)
Peter Zijlstra6e831252014-02-11 16:11:48 +01008776 pulled_task = 1;
Daniel Lezcanoe5fc6612014-01-17 10:04:02 +01008777
Peter Zijlstra6e831252014-02-11 16:11:48 +01008778out:
Jason Low52a08ef2014-05-08 17:49:22 -07008779 /* Move the next balance forward */
8780 if (time_after(this_rq->next_balance, next_balance))
8781 this_rq->next_balance = next_balance;
8782
Kirill Tkhaie4aa3582014-03-06 13:31:55 +04008783 /* Is there a task of a high priority class? */
Kirill Tkhai46383642014-03-15 02:15:07 +04008784 if (this_rq->nr_running != this_rq->cfs.h_nr_running)
Kirill Tkhaie4aa3582014-03-06 13:31:55 +04008785 pulled_task = -1;
8786
Dietmar Eggemann38c6ade2015-10-20 13:04:41 +01008787 if (pulled_task)
Peter Zijlstra6e831252014-02-11 16:11:48 +01008788 this_rq->idle_stamp = 0;
8789
Daniel Lezcano3c4017c2014-01-17 10:04:03 +01008790 return pulled_task;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008791}
8792
8793/*
Tejun Heo969c7922010-05-06 18:49:21 +02008794 * active_load_balance_cpu_stop is run by cpu stopper. It pushes
8795 * running tasks off the busiest CPU onto idle CPUs. It requires at
8796 * least 1 task to be running on each physical CPU where possible, and
8797 * avoids physical / logical imbalances.
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008798 */
Tejun Heo969c7922010-05-06 18:49:21 +02008799static int active_load_balance_cpu_stop(void *data)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008800{
Tejun Heo969c7922010-05-06 18:49:21 +02008801 struct rq *busiest_rq = data;
8802 int busiest_cpu = cpu_of(busiest_rq);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008803 int target_cpu = busiest_rq->push_cpu;
Tejun Heo969c7922010-05-06 18:49:21 +02008804 struct rq *target_rq = cpu_rq(target_cpu);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008805 struct sched_domain *sd;
Kirill Tkhaie5673f22014-08-20 13:48:01 +04008806 struct task_struct *p = NULL;
Tejun Heo969c7922010-05-06 18:49:21 +02008807
8808 raw_spin_lock_irq(&busiest_rq->lock);
8809
8810 /* make sure the requested cpu hasn't gone down in the meantime */
8811 if (unlikely(busiest_cpu != smp_processor_id() ||
8812 !busiest_rq->active_balance))
8813 goto out_unlock;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008814
8815 /* Is there any task to move? */
8816 if (busiest_rq->nr_running <= 1)
Tejun Heo969c7922010-05-06 18:49:21 +02008817 goto out_unlock;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008818
8819 /*
8820 * This condition is "impossible", if it occurs
8821 * we need to fix it. Originally reported by
8822 * Bjorn Helgaas on a 128-cpu setup.
8823 */
8824 BUG_ON(busiest_rq == target_rq);
8825
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008826 /* Search for an sd spanning us and the target CPU. */
Peter Zijlstradce840a2011-04-07 14:09:50 +02008827 rcu_read_lock();
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008828 for_each_domain(target_cpu, sd) {
8829 if ((sd->flags & SD_LOAD_BALANCE) &&
8830 cpumask_test_cpu(busiest_cpu, sched_domain_span(sd)))
8831 break;
8832 }
8833
8834 if (likely(sd)) {
Peter Zijlstra8e45cb52012-02-22 12:47:19 +01008835 struct lb_env env = {
8836 .sd = sd,
Peter Zijlstraddcdf6e2012-02-22 19:27:40 +01008837 .dst_cpu = target_cpu,
8838 .dst_rq = target_rq,
8839 .src_cpu = busiest_rq->cpu,
8840 .src_rq = busiest_rq,
Peter Zijlstra8e45cb52012-02-22 12:47:19 +01008841 .idle = CPU_IDLE,
8842 };
8843
Josh Poimboeufae928822016-06-17 12:43:24 -05008844 schedstat_inc(sd->alb_count);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008845
Kirill Tkhaie5673f22014-08-20 13:48:01 +04008846 p = detach_one_task(&env);
Srikar Dronamrajud02c071182016-03-23 17:54:44 +05308847 if (p) {
Josh Poimboeufae928822016-06-17 12:43:24 -05008848 schedstat_inc(sd->alb_pushed);
Juri Lellic9bf15a2015-06-25 14:37:27 +01008849 /*
8850 * We want to potentially lower env.src_cpu's OPP.
8851 */
8852 update_capacity_of(env.src_cpu);
Srikar Dronamrajud02c071182016-03-23 17:54:44 +05308853 /* Active balancing done, reset the failure counter. */
8854 sd->nr_balance_failed = 0;
8855 } else {
Josh Poimboeufae928822016-06-17 12:43:24 -05008856 schedstat_inc(sd->alb_failed);
Srikar Dronamrajud02c071182016-03-23 17:54:44 +05308857 }
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008858 }
Peter Zijlstradce840a2011-04-07 14:09:50 +02008859 rcu_read_unlock();
Tejun Heo969c7922010-05-06 18:49:21 +02008860out_unlock:
8861 busiest_rq->active_balance = 0;
Kirill Tkhaie5673f22014-08-20 13:48:01 +04008862 raw_spin_unlock(&busiest_rq->lock);
8863
8864 if (p)
8865 attach_one_task(target_rq, p);
8866
8867 local_irq_enable();
8868
Tejun Heo969c7922010-05-06 18:49:21 +02008869 return 0;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008870}
8871
Mike Galbraithd987fc72011-12-05 10:01:47 +01008872static inline int on_null_domain(struct rq *rq)
8873{
8874 return unlikely(!rcu_dereference_sched(rq->sd));
8875}
8876
Frederic Weisbecker3451d022011-08-10 23:21:01 +02008877#ifdef CONFIG_NO_HZ_COMMON
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07008878/*
8879 * idle load balancing details
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07008880 * - When one of the busy CPUs notice that there may be an idle rebalancing
8881 * needed, they will kick the idle load balancer, which then does idle
8882 * load balancing for all the idle CPUs.
8883 */
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008884static struct {
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07008885 cpumask_var_t idle_cpus_mask;
Suresh Siddha0b005cf2011-12-01 17:07:34 -08008886 atomic_t nr_cpus;
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07008887 unsigned long next_balance; /* in jiffy units */
8888} nohz ____cacheline_aligned;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008889
Daniel Lezcano3dd03372014-01-06 12:34:41 +01008890static inline int find_new_ilb(void)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008891{
Suresh Siddha0b005cf2011-12-01 17:07:34 -08008892 int ilb = cpumask_first(nohz.idle_cpus_mask);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008893
Suresh Siddha786d6dc72011-12-01 17:07:35 -08008894 if (ilb < nr_cpu_ids && idle_cpu(ilb))
8895 return ilb;
8896
8897 return nr_cpu_ids;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008898}
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008899
8900/*
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07008901 * Kick a CPU to do the nohz balancing, if it is time for it. We pick the
8902 * nohz_load_balancer CPU (if there is one) otherwise fallback to any idle
8903 * CPU (if there is one).
8904 */
Daniel Lezcano0aeeeeb2014-01-06 12:34:42 +01008905static void nohz_balancer_kick(void)
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07008906{
8907 int ilb_cpu;
8908
8909 nohz.next_balance++;
8910
Daniel Lezcano3dd03372014-01-06 12:34:41 +01008911 ilb_cpu = find_new_ilb();
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07008912
Suresh Siddha0b005cf2011-12-01 17:07:34 -08008913 if (ilb_cpu >= nr_cpu_ids)
8914 return;
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07008915
Suresh Siddhacd490c52011-12-06 11:26:34 -08008916 if (test_and_set_bit(NOHZ_BALANCE_KICK, nohz_flags(ilb_cpu)))
Suresh Siddha1c792db2011-12-01 17:07:32 -08008917 return;
8918 /*
8919 * Use smp_send_reschedule() instead of resched_cpu().
8920 * This way we generate a sched IPI on the target cpu which
8921 * is idle. And the softirq performing nohz idle load balance
8922 * will be run before returning from the IPI.
8923 */
8924 smp_send_reschedule(ilb_cpu);
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07008925 return;
8926}
8927
Thomas Gleixner20a5c8c2016-03-10 12:54:20 +01008928void nohz_balance_exit_idle(unsigned int cpu)
Suresh Siddha71325962012-01-19 18:28:57 -08008929{
8930 if (unlikely(test_bit(NOHZ_TICK_STOPPED, nohz_flags(cpu)))) {
Mike Galbraithd987fc72011-12-05 10:01:47 +01008931 /*
8932 * Completely isolated CPUs don't ever set, so we must test.
8933 */
8934 if (likely(cpumask_test_cpu(cpu, nohz.idle_cpus_mask))) {
8935 cpumask_clear_cpu(cpu, nohz.idle_cpus_mask);
8936 atomic_dec(&nohz.nr_cpus);
8937 }
Suresh Siddha71325962012-01-19 18:28:57 -08008938 clear_bit(NOHZ_TICK_STOPPED, nohz_flags(cpu));
8939 }
8940}
8941
Suresh Siddha69e1e812011-12-01 17:07:33 -08008942static inline void set_cpu_sd_state_busy(void)
8943{
8944 struct sched_domain *sd;
Preeti U Murthy37dc6b502013-10-30 08:42:52 +05308945 int cpu = smp_processor_id();
Suresh Siddha69e1e812011-12-01 17:07:33 -08008946
Suresh Siddha69e1e812011-12-01 17:07:33 -08008947 rcu_read_lock();
Peter Zijlstra0e369d72016-05-09 10:38:01 +02008948 sd = rcu_dereference(per_cpu(sd_llc, cpu));
Vincent Guittot25f55d92013-04-23 16:59:02 +02008949
8950 if (!sd || !sd->nohz_idle)
8951 goto unlock;
8952 sd->nohz_idle = 0;
8953
Peter Zijlstra0e369d72016-05-09 10:38:01 +02008954 atomic_inc(&sd->shared->nr_busy_cpus);
Vincent Guittot25f55d92013-04-23 16:59:02 +02008955unlock:
Suresh Siddha69e1e812011-12-01 17:07:33 -08008956 rcu_read_unlock();
8957}
8958
8959void set_cpu_sd_state_idle(void)
8960{
8961 struct sched_domain *sd;
Preeti U Murthy37dc6b502013-10-30 08:42:52 +05308962 int cpu = smp_processor_id();
Suresh Siddha69e1e812011-12-01 17:07:33 -08008963
Suresh Siddha69e1e812011-12-01 17:07:33 -08008964 rcu_read_lock();
Peter Zijlstra0e369d72016-05-09 10:38:01 +02008965 sd = rcu_dereference(per_cpu(sd_llc, cpu));
Vincent Guittot25f55d92013-04-23 16:59:02 +02008966
8967 if (!sd || sd->nohz_idle)
8968 goto unlock;
8969 sd->nohz_idle = 1;
8970
Peter Zijlstra0e369d72016-05-09 10:38:01 +02008971 atomic_dec(&sd->shared->nr_busy_cpus);
Vincent Guittot25f55d92013-04-23 16:59:02 +02008972unlock:
Suresh Siddha69e1e812011-12-01 17:07:33 -08008973 rcu_read_unlock();
8974}
8975
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07008976/*
Alex Shic1cc0172012-09-10 15:10:58 +08008977 * This routine will record that the cpu is going idle with tick stopped.
Suresh Siddha0b005cf2011-12-01 17:07:34 -08008978 * This info will be used in performing idle load balancing in the future.
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008979 */
Alex Shic1cc0172012-09-10 15:10:58 +08008980void nohz_balance_enter_idle(int cpu)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008981{
Suresh Siddha71325962012-01-19 18:28:57 -08008982 /*
8983 * If this cpu is going down, then nothing needs to be done.
8984 */
8985 if (!cpu_active(cpu))
8986 return;
8987
Alex Shic1cc0172012-09-10 15:10:58 +08008988 if (test_bit(NOHZ_TICK_STOPPED, nohz_flags(cpu)))
8989 return;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008990
Mike Galbraithd987fc72011-12-05 10:01:47 +01008991 /*
8992 * If we're a completely isolated CPU, we don't play.
8993 */
8994 if (on_null_domain(cpu_rq(cpu)))
8995 return;
8996
Alex Shic1cc0172012-09-10 15:10:58 +08008997 cpumask_set_cpu(cpu, nohz.idle_cpus_mask);
8998 atomic_inc(&nohz.nr_cpus);
8999 set_bit(NOHZ_TICK_STOPPED, nohz_flags(cpu));
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01009000}
9001#endif
9002
9003static DEFINE_SPINLOCK(balancing);
9004
Peter Zijlstra49c022e2011-04-05 10:14:25 +02009005/*
9006 * Scale the max load_balance interval with the number of CPUs in the system.
9007 * This trades load-balance latency on larger machines for less cross talk.
9008 */
Peter Zijlstra029632f2011-10-25 10:00:11 +02009009void update_max_interval(void)
Peter Zijlstra49c022e2011-04-05 10:14:25 +02009010{
9011 max_load_balance_interval = HZ*num_online_cpus()/10;
9012}
9013
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01009014/*
9015 * It checks each scheduling domain to see if it is due to be balanced,
9016 * and initiates a balancing operation if so.
9017 *
Libinb9b08532013-04-01 19:14:01 +08009018 * Balancing parameters are set up in init_sched_domains.
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01009019 */
Daniel Lezcanof7ed0a82014-01-06 12:34:43 +01009020static void rebalance_domains(struct rq *rq, enum cpu_idle_type idle)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01009021{
Joonsoo Kim23f0d202013-08-06 17:36:42 +09009022 int continue_balancing = 1;
Daniel Lezcanof7ed0a82014-01-06 12:34:43 +01009023 int cpu = rq->cpu;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01009024 unsigned long interval;
Peter Zijlstra04f733b2012-05-11 00:12:02 +02009025 struct sched_domain *sd;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01009026 /* Earliest time when we have to do rebalance again */
9027 unsigned long next_balance = jiffies + 60*HZ;
9028 int update_next_balance = 0;
Jason Lowf48627e2013-09-13 11:26:53 -07009029 int need_serialize, need_decay = 0;
9030 u64 max_cost = 0;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01009031
Paul Turner48a16752012-10-04 13:18:31 +02009032 update_blocked_averages(cpu);
Peter Zijlstra2069dd72010-11-15 15:47:00 -08009033
Peter Zijlstradce840a2011-04-07 14:09:50 +02009034 rcu_read_lock();
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01009035 for_each_domain(cpu, sd) {
Jason Lowf48627e2013-09-13 11:26:53 -07009036 /*
9037 * Decay the newidle max times here because this is a regular
9038 * visit to all the domains. Decay ~1% per second.
9039 */
9040 if (time_after(jiffies, sd->next_decay_max_lb_cost)) {
9041 sd->max_newidle_lb_cost =
9042 (sd->max_newidle_lb_cost * 253) / 256;
9043 sd->next_decay_max_lb_cost = jiffies + HZ;
9044 need_decay = 1;
9045 }
9046 max_cost += sd->max_newidle_lb_cost;
9047
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01009048 if (!(sd->flags & SD_LOAD_BALANCE))
9049 continue;
9050
Jason Lowf48627e2013-09-13 11:26:53 -07009051 /*
9052 * Stop the load balance at this level. There is another
9053 * CPU in our sched group which is doing load balancing more
9054 * actively.
9055 */
9056 if (!continue_balancing) {
9057 if (need_decay)
9058 continue;
9059 break;
9060 }
9061
Jason Low52a08ef2014-05-08 17:49:22 -07009062 interval = get_sd_balance_interval(sd, idle != CPU_IDLE);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01009063
9064 need_serialize = sd->flags & SD_SERIALIZE;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01009065 if (need_serialize) {
9066 if (!spin_trylock(&balancing))
9067 goto out;
9068 }
9069
9070 if (time_after_eq(jiffies, sd->last_balance + interval)) {
Joonsoo Kim23f0d202013-08-06 17:36:42 +09009071 if (load_balance(cpu, rq, sd, idle, &continue_balancing)) {
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01009072 /*
Peter Zijlstra62633222013-08-19 12:41:09 +02009073 * The LBF_DST_PINNED logic could have changed
Joonsoo Kimde5eb2d2013-04-23 17:27:38 +09009074 * env->dst_cpu, so we can't know our idle
9075 * state even if we migrated tasks. Update it.
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01009076 */
Joonsoo Kimde5eb2d2013-04-23 17:27:38 +09009077 idle = idle_cpu(cpu) ? CPU_IDLE : CPU_NOT_IDLE;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01009078 }
9079 sd->last_balance = jiffies;
Jason Low52a08ef2014-05-08 17:49:22 -07009080 interval = get_sd_balance_interval(sd, idle != CPU_IDLE);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01009081 }
9082 if (need_serialize)
9083 spin_unlock(&balancing);
9084out:
9085 if (time_after(next_balance, sd->last_balance + interval)) {
9086 next_balance = sd->last_balance + interval;
9087 update_next_balance = 1;
9088 }
Jason Lowf48627e2013-09-13 11:26:53 -07009089 }
9090 if (need_decay) {
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01009091 /*
Jason Lowf48627e2013-09-13 11:26:53 -07009092 * Ensure the rq-wide value also decays but keep it at a
9093 * reasonable floor to avoid funnies with rq->avg_idle.
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01009094 */
Jason Lowf48627e2013-09-13 11:26:53 -07009095 rq->max_idle_balance_cost =
9096 max((u64)sysctl_sched_migration_cost, max_cost);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01009097 }
Peter Zijlstradce840a2011-04-07 14:09:50 +02009098 rcu_read_unlock();
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01009099
9100 /*
9101 * next_balance will be updated only when there is a need.
9102 * When the cpu is attached to null domain for ex, it will not be
9103 * updated.
9104 */
Vincent Guittotc5afb6a2015-08-03 11:55:50 +02009105 if (likely(update_next_balance)) {
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01009106 rq->next_balance = next_balance;
Vincent Guittotc5afb6a2015-08-03 11:55:50 +02009107
9108#ifdef CONFIG_NO_HZ_COMMON
9109 /*
9110 * If this CPU has been elected to perform the nohz idle
9111 * balance. Other idle CPUs have already rebalanced with
9112 * nohz_idle_balance() and nohz.next_balance has been
9113 * updated accordingly. This CPU is now running the idle load
9114 * balance for itself and we need to update the
9115 * nohz.next_balance accordingly.
9116 */
9117 if ((idle == CPU_IDLE) && time_after(nohz.next_balance, rq->next_balance))
9118 nohz.next_balance = rq->next_balance;
9119#endif
9120 }
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01009121}
9122
Frederic Weisbecker3451d022011-08-10 23:21:01 +02009123#ifdef CONFIG_NO_HZ_COMMON
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07009124/*
Frederic Weisbecker3451d022011-08-10 23:21:01 +02009125 * In CONFIG_NO_HZ_COMMON case, the idle balance kickee will do the
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07009126 * rebalancing for all the cpus for whom scheduler ticks are stopped.
9127 */
Daniel Lezcano208cb162014-01-06 12:34:44 +01009128static void nohz_idle_balance(struct rq *this_rq, enum cpu_idle_type idle)
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07009129{
Daniel Lezcano208cb162014-01-06 12:34:44 +01009130 int this_cpu = this_rq->cpu;
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07009131 struct rq *rq;
9132 int balance_cpu;
Vincent Guittotc5afb6a2015-08-03 11:55:50 +02009133 /* Earliest time when we have to do rebalance again */
9134 unsigned long next_balance = jiffies + 60*HZ;
9135 int update_next_balance = 0;
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07009136
Suresh Siddha1c792db2011-12-01 17:07:32 -08009137 if (idle != CPU_IDLE ||
9138 !test_bit(NOHZ_BALANCE_KICK, nohz_flags(this_cpu)))
9139 goto end;
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07009140
9141 for_each_cpu(balance_cpu, nohz.idle_cpus_mask) {
Suresh Siddha8a6d42d2011-12-06 11:19:37 -08009142 if (balance_cpu == this_cpu || !idle_cpu(balance_cpu))
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07009143 continue;
9144
9145 /*
9146 * If this cpu gets work to do, stop the load balancing
9147 * work being done for other cpus. Next load
9148 * balancing owner will pick it up.
9149 */
Suresh Siddha1c792db2011-12-01 17:07:32 -08009150 if (need_resched())
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07009151 break;
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07009152
Vincent Guittot5ed4f1d2012-09-13 06:11:26 +02009153 rq = cpu_rq(balance_cpu);
9154
Tim Chened61bbc2014-05-20 14:39:27 -07009155 /*
9156 * If time for next balance is due,
9157 * do the balance.
9158 */
9159 if (time_after_eq(jiffies, rq->next_balance)) {
9160 raw_spin_lock_irq(&rq->lock);
9161 update_rq_clock(rq);
Frederic Weisbeckercee1afc2016-04-13 15:56:50 +02009162 cpu_load_update_idle(rq);
Tim Chened61bbc2014-05-20 14:39:27 -07009163 raw_spin_unlock_irq(&rq->lock);
9164 rebalance_domains(rq, CPU_IDLE);
9165 }
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07009166
Vincent Guittotc5afb6a2015-08-03 11:55:50 +02009167 if (time_after(next_balance, rq->next_balance)) {
9168 next_balance = rq->next_balance;
9169 update_next_balance = 1;
9170 }
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07009171 }
Vincent Guittotc5afb6a2015-08-03 11:55:50 +02009172
9173 /*
9174 * next_balance will be updated only when there is a need.
9175 * When the CPU is attached to null domain for ex, it will not be
9176 * updated.
9177 */
9178 if (likely(update_next_balance))
9179 nohz.next_balance = next_balance;
Suresh Siddha1c792db2011-12-01 17:07:32 -08009180end:
9181 clear_bit(NOHZ_BALANCE_KICK, nohz_flags(this_cpu));
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07009182}
9183
9184/*
Suresh Siddha0b005cf2011-12-01 17:07:34 -08009185 * Current heuristic for kicking the idle load balancer in the presence
Vincent Guittot1aaf90a2015-02-27 16:54:14 +01009186 * of an idle cpu in the system.
Suresh Siddha0b005cf2011-12-01 17:07:34 -08009187 * - This rq has more than one task.
Vincent Guittot1aaf90a2015-02-27 16:54:14 +01009188 * - This rq has at least one CFS task and the capacity of the CPU is
9189 * significantly reduced because of RT tasks or IRQs.
9190 * - At parent of LLC scheduler domain level, this cpu's scheduler group has
9191 * multiple busy cpu.
Suresh Siddha0b005cf2011-12-01 17:07:34 -08009192 * - For SD_ASYM_PACKING, if the lower numbered cpu's in the scheduler
9193 * domain span are idle.
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07009194 */
Vincent Guittot1aaf90a2015-02-27 16:54:14 +01009195static inline bool nohz_kick_needed(struct rq *rq)
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07009196{
9197 unsigned long now = jiffies;
Peter Zijlstra0e369d72016-05-09 10:38:01 +02009198 struct sched_domain_shared *sds;
Suresh Siddha0b005cf2011-12-01 17:07:34 -08009199 struct sched_domain *sd;
Daniel Lezcano4a725622014-01-06 12:34:39 +01009200 int nr_busy, cpu = rq->cpu;
Vincent Guittot1aaf90a2015-02-27 16:54:14 +01009201 bool kick = false;
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07009202
Daniel Lezcano4a725622014-01-06 12:34:39 +01009203 if (unlikely(rq->idle_balance))
Vincent Guittot1aaf90a2015-02-27 16:54:14 +01009204 return false;
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07009205
Suresh Siddha1c792db2011-12-01 17:07:32 -08009206 /*
9207 * We may be recently in ticked or tickless idle mode. At the first
9208 * busy tick after returning from idle, we will update the busy stats.
9209 */
Suresh Siddha69e1e812011-12-01 17:07:33 -08009210 set_cpu_sd_state_busy();
Alex Shic1cc0172012-09-10 15:10:58 +08009211 nohz_balance_exit_idle(cpu);
Suresh Siddha0b005cf2011-12-01 17:07:34 -08009212
9213 /*
9214 * None are in tickless mode and hence no need for NOHZ idle load
9215 * balancing.
9216 */
9217 if (likely(!atomic_read(&nohz.nr_cpus)))
Vincent Guittot1aaf90a2015-02-27 16:54:14 +01009218 return false;
Suresh Siddha1c792db2011-12-01 17:07:32 -08009219
9220 if (time_before(now, nohz.next_balance))
Vincent Guittot1aaf90a2015-02-27 16:54:14 +01009221 return false;
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07009222
Morten Rasmussenf69e2dc2015-02-03 13:54:11 +00009223 if (rq->nr_running >= 2 &&
9224 (!energy_aware() || cpu_overutilized(cpu)))
Vincent Guittot1aaf90a2015-02-27 16:54:14 +01009225 return true;
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07009226
Peter Zijlstra067491b2011-12-07 14:32:08 +01009227 rcu_read_lock();
Peter Zijlstra0e369d72016-05-09 10:38:01 +02009228 sds = rcu_dereference(per_cpu(sd_llc_shared, cpu));
Morten Rasmussenf69e2dc2015-02-03 13:54:11 +00009229 if (sds && !energy_aware()) {
Peter Zijlstra0e369d72016-05-09 10:38:01 +02009230 /*
9231 * XXX: write a coherent comment on why we do this.
9232 * See also: http://lkml.kernel.org/r/20111202010832.602203411@sbsiddha-desk.sc.intel.com
9233 */
9234 nr_busy = atomic_read(&sds->nr_busy_cpus);
Vincent Guittot1aaf90a2015-02-27 16:54:14 +01009235 if (nr_busy > 1) {
9236 kick = true;
9237 goto unlock;
9238 }
9239
9240 }
9241
9242 sd = rcu_dereference(rq->sd);
9243 if (sd) {
9244 if ((rq->cfs.h_nr_running >= 1) &&
9245 check_cpu_capacity(rq, sd)) {
9246 kick = true;
9247 goto unlock;
9248 }
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07009249 }
Preeti U Murthy37dc6b502013-10-30 08:42:52 +05309250
9251 sd = rcu_dereference(per_cpu(sd_asym, cpu));
Preeti U Murthy37dc6b502013-10-30 08:42:52 +05309252 if (sd && (cpumask_first_and(nohz.idle_cpus_mask,
Vincent Guittot1aaf90a2015-02-27 16:54:14 +01009253 sched_domain_span(sd)) < cpu)) {
9254 kick = true;
9255 goto unlock;
9256 }
Preeti U Murthy37dc6b502013-10-30 08:42:52 +05309257
Vincent Guittot1aaf90a2015-02-27 16:54:14 +01009258unlock:
Peter Zijlstra067491b2011-12-07 14:32:08 +01009259 rcu_read_unlock();
Vincent Guittot1aaf90a2015-02-27 16:54:14 +01009260 return kick;
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07009261}
9262#else
Daniel Lezcano208cb162014-01-06 12:34:44 +01009263static void nohz_idle_balance(struct rq *this_rq, enum cpu_idle_type idle) { }
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07009264#endif
9265
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01009266/*
9267 * run_rebalance_domains is triggered when needed from the scheduler tick.
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07009268 * Also triggered for nohz idle balancing (with nohz_balancing_kick set).
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01009269 */
Emese Revfy0766f782016-06-20 20:42:34 +02009270static __latent_entropy void run_rebalance_domains(struct softirq_action *h)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01009271{
Daniel Lezcano208cb162014-01-06 12:34:44 +01009272 struct rq *this_rq = this_rq();
Suresh Siddha6eb57e02011-10-03 15:09:01 -07009273 enum cpu_idle_type idle = this_rq->idle_balance ?
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01009274 CPU_IDLE : CPU_NOT_IDLE;
9275
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01009276 /*
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07009277 * If this cpu has a pending nohz_balance_kick, then do the
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01009278 * balancing on behalf of the other idle cpus whose ticks are
Preeti U Murthyd4573c32015-03-26 18:32:44 +05309279 * stopped. Do nohz_idle_balance *before* rebalance_domains to
9280 * give the idle cpus a chance to load balance. Else we may
9281 * load balance only within the local sched_domain hierarchy
9282 * and abort nohz_idle_balance altogether if we pull some load.
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01009283 */
Daniel Lezcano208cb162014-01-06 12:34:44 +01009284 nohz_idle_balance(this_rq, idle);
Preeti U Murthyd4573c32015-03-26 18:32:44 +05309285 rebalance_domains(this_rq, idle);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01009286}
9287
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01009288/*
9289 * Trigger the SCHED_SOFTIRQ if it is time to do periodic load balancing.
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01009290 */
Daniel Lezcano7caff662014-01-06 12:34:38 +01009291void trigger_load_balance(struct rq *rq)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01009292{
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01009293 /* Don't need to rebalance while attached to NULL domain */
Daniel Lezcanoc7260992014-01-06 12:34:45 +01009294 if (unlikely(on_null_domain(rq)))
9295 return;
9296
9297 if (time_after_eq(jiffies, rq->next_balance))
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01009298 raise_softirq(SCHED_SOFTIRQ);
Frederic Weisbecker3451d022011-08-10 23:21:01 +02009299#ifdef CONFIG_NO_HZ_COMMON
Daniel Lezcanoc7260992014-01-06 12:34:45 +01009300 if (nohz_kick_needed(rq))
Daniel Lezcano0aeeeeb2014-01-06 12:34:42 +01009301 nohz_balancer_kick();
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07009302#endif
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01009303}
9304
Christian Ehrhardt0bcdcf22009-11-30 12:16:46 +01009305static void rq_online_fair(struct rq *rq)
9306{
9307 update_sysctl();
Kirill Tkhai0e59bda2014-06-25 12:19:42 +04009308
9309 update_runtime_enabled(rq);
Christian Ehrhardt0bcdcf22009-11-30 12:16:46 +01009310}
9311
9312static void rq_offline_fair(struct rq *rq)
9313{
9314 update_sysctl();
Peter Boonstoppela4c96ae2012-08-09 15:34:47 -07009315
9316 /* Ensure any throttled groups are reachable by pick_next_task */
9317 unthrottle_offline_cfs_rqs(rq);
Christian Ehrhardt0bcdcf22009-11-30 12:16:46 +01009318}
9319
Dhaval Giani55e12e52008-06-24 23:39:43 +05309320#endif /* CONFIG_SMP */
Peter Williamse1d14842007-10-24 18:23:51 +02009321
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02009322/*
9323 * scheduler tick hitting a task of our scheduling class:
9324 */
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01009325static void task_tick_fair(struct rq *rq, struct task_struct *curr, int queued)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02009326{
9327 struct cfs_rq *cfs_rq;
9328 struct sched_entity *se = &curr->se;
9329
9330 for_each_sched_entity(se) {
9331 cfs_rq = cfs_rq_of(se);
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01009332 entity_tick(cfs_rq, se, queued);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02009333 }
Ben Segall18bf2802012-10-04 12:51:20 +02009334
Srikar Dronamrajub52da862015-10-02 07:48:25 +05309335 if (static_branch_unlikely(&sched_numa_balancing))
Peter Zijlstracbee9f82012-10-25 14:16:43 +02009336 task_tick_numa(rq, curr);
Morten Rasmussena562dfc2015-05-09 16:49:57 +01009337
Patrick Bellasi2178e842016-07-22 11:35:59 +01009338#ifdef CONFIG_SMP
Morten Rasmussena562dfc2015-05-09 16:49:57 +01009339 if (!rq->rd->overutilized && cpu_overutilized(task_cpu(curr)))
9340 rq->rd->overutilized = true;
Morten Rasmussen4c6a8242016-02-25 12:47:54 +00009341
9342 rq->misfit_task = !task_fits_max(curr, rq->cpu);
Patrick Bellasi2178e842016-07-22 11:35:59 +01009343#endif
9344
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02009345}
9346
9347/*
Peter Zijlstracd29fe62009-11-27 17:32:46 +01009348 * called on fork with the child task as argument from the parent's context
9349 * - child not yet on the tasklist
9350 * - preemption disabled
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02009351 */
Peter Zijlstracd29fe62009-11-27 17:32:46 +01009352static void task_fork_fair(struct task_struct *p)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02009353{
Daisuke Nishimura4fc420c2011-12-15 14:36:55 +09009354 struct cfs_rq *cfs_rq;
9355 struct sched_entity *se = &p->se, *curr;
Peter Zijlstracd29fe62009-11-27 17:32:46 +01009356 struct rq *rq = this_rq();
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02009357
Peter Zijlstrae210bff2016-06-16 18:51:48 +02009358 raw_spin_lock(&rq->lock);
Peter Zijlstra861d0342010-08-19 13:31:43 +02009359 update_rq_clock(rq);
9360
Daisuke Nishimura4fc420c2011-12-15 14:36:55 +09009361 cfs_rq = task_cfs_rq(current);
9362 curr = cfs_rq->curr;
Peter Zijlstrae210bff2016-06-16 18:51:48 +02009363 if (curr) {
9364 update_curr(cfs_rq);
Mike Galbraithb5d9d732009-09-08 11:12:28 +02009365 se->vruntime = curr->vruntime;
Peter Zijlstrae210bff2016-06-16 18:51:48 +02009366 }
Peter Zijlstraaeb73b02007-10-15 17:00:05 +02009367 place_entity(cfs_rq, se, 1);
Peter Zijlstra4d78e7b2007-10-15 17:00:04 +02009368
Peter Zijlstracd29fe62009-11-27 17:32:46 +01009369 if (sysctl_sched_child_runs_first && curr && entity_before(curr, se)) {
Dmitry Adamushko87fefa32007-10-15 17:00:08 +02009370 /*
Ingo Molnaredcb60a2007-10-15 17:00:08 +02009371 * Upon rescheduling, sched_class::put_prev_task() will place
9372 * 'current' within the tree based on its new key value.
9373 */
Peter Zijlstra4d78e7b2007-10-15 17:00:04 +02009374 swap(curr->vruntime, se->vruntime);
Kirill Tkhai88751252014-06-29 00:03:57 +04009375 resched_curr(rq);
Peter Zijlstra4d78e7b2007-10-15 17:00:04 +02009376 }
9377
Peter Zijlstra88ec22d2009-12-16 18:04:41 +01009378 se->vruntime -= cfs_rq->min_vruntime;
Peter Zijlstrae210bff2016-06-16 18:51:48 +02009379 raw_spin_unlock(&rq->lock);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02009380}
9381
Steven Rostedtcb469842008-01-25 21:08:22 +01009382/*
9383 * Priority of the task has changed. Check to see if we preempt
9384 * the current task.
9385 */
Peter Zijlstrada7a7352011-01-17 17:03:27 +01009386static void
9387prio_changed_fair(struct rq *rq, struct task_struct *p, int oldprio)
Steven Rostedtcb469842008-01-25 21:08:22 +01009388{
Kirill Tkhaida0c1e62014-08-20 13:47:32 +04009389 if (!task_on_rq_queued(p))
Peter Zijlstrada7a7352011-01-17 17:03:27 +01009390 return;
9391
Steven Rostedtcb469842008-01-25 21:08:22 +01009392 /*
9393 * Reschedule if we are currently running on this runqueue and
9394 * our priority decreased, or if we are not currently running on
9395 * this runqueue and our priority is higher than the current's
9396 */
Peter Zijlstrada7a7352011-01-17 17:03:27 +01009397 if (rq->curr == p) {
Steven Rostedtcb469842008-01-25 21:08:22 +01009398 if (p->prio > oldprio)
Kirill Tkhai88751252014-06-29 00:03:57 +04009399 resched_curr(rq);
Steven Rostedtcb469842008-01-25 21:08:22 +01009400 } else
Peter Zijlstra15afe092008-09-20 23:38:02 +02009401 check_preempt_curr(rq, p, 0);
Steven Rostedtcb469842008-01-25 21:08:22 +01009402}
9403
Byungchul Parkdaa59402015-08-20 20:22:00 +09009404static inline bool vruntime_normalized(struct task_struct *p)
9405{
9406 struct sched_entity *se = &p->se;
9407
9408 /*
9409 * In both the TASK_ON_RQ_QUEUED and TASK_ON_RQ_MIGRATING cases,
9410 * the dequeue_entity(.flags=0) will already have normalized the
9411 * vruntime.
9412 */
9413 if (p->on_rq)
9414 return true;
9415
9416 /*
9417 * When !on_rq, vruntime of the task has usually NOT been normalized.
9418 * But there are some cases where it has already been normalized:
9419 *
9420 * - A forked child which is waiting for being woken up by
9421 * wake_up_new_task().
9422 * - A task which has been woken up by try_to_wake_up() and
9423 * waiting for actually being woken up by sched_ttwu_pending().
9424 */
9425 if (!se->sum_exec_runtime || p->state == TASK_WAKING)
9426 return true;
9427
9428 return false;
9429}
9430
9431static void detach_task_cfs_rq(struct task_struct *p)
Peter Zijlstrada7a7352011-01-17 17:03:27 +01009432{
9433 struct sched_entity *se = &p->se;
9434 struct cfs_rq *cfs_rq = cfs_rq_of(se);
Peter Zijlstra01011472016-06-17 11:20:46 +02009435 u64 now = cfs_rq_clock_task(cfs_rq);
Peter Zijlstrada7a7352011-01-17 17:03:27 +01009436
Byungchul Parkdaa59402015-08-20 20:22:00 +09009437 if (!vruntime_normalized(p)) {
Peter Zijlstrada7a7352011-01-17 17:03:27 +01009438 /*
9439 * Fix up our vruntime so that the current sleep doesn't
9440 * cause 'unlimited' sleep bonus.
9441 */
9442 place_entity(cfs_rq, se, 0);
9443 se->vruntime -= cfs_rq->min_vruntime;
9444 }
Paul Turner9ee474f2012-10-04 13:18:30 +02009445
Yuyang Du9d89c252015-07-15 08:04:37 +08009446 /* Catch up with the cfs_rq and remove our load when we leave */
Peter Zijlstra7c3edd22016-07-13 10:56:25 +02009447 update_cfs_rq_load_avg(now, cfs_rq, false);
Byungchul Parka05e8c52015-08-20 20:21:56 +09009448 detach_entity_load_avg(cfs_rq, se);
Peter Zijlstra7c3edd22016-07-13 10:56:25 +02009449 update_tg_load_avg(cfs_rq, false);
Peter Zijlstrada7a7352011-01-17 17:03:27 +01009450}
9451
Byungchul Parkdaa59402015-08-20 20:22:00 +09009452static void attach_task_cfs_rq(struct task_struct *p)
Steven Rostedtcb469842008-01-25 21:08:22 +01009453{
Kirill Tkhaif36c0192014-08-06 12:06:01 +04009454 struct sched_entity *se = &p->se;
Byungchul Parkdaa59402015-08-20 20:22:00 +09009455 struct cfs_rq *cfs_rq = cfs_rq_of(se);
Peter Zijlstra01011472016-06-17 11:20:46 +02009456 u64 now = cfs_rq_clock_task(cfs_rq);
Byungchul Park7855a352015-08-10 18:02:55 +09009457
9458#ifdef CONFIG_FAIR_GROUP_SCHED
Michael wangeb7a59b2014-02-20 11:14:53 +08009459 /*
9460 * Since the real-depth could have been changed (only FAIR
9461 * class maintain depth value), reset depth properly.
9462 */
9463 se->depth = se->parent ? se->parent->depth + 1 : 0;
9464#endif
Byungchul Park7855a352015-08-10 18:02:55 +09009465
Byungchul Park6efdb102015-08-20 20:21:59 +09009466 /* Synchronize task with its cfs_rq */
Peter Zijlstra7c3edd22016-07-13 10:56:25 +02009467 update_cfs_rq_load_avg(now, cfs_rq, false);
Byungchul Parkdaa59402015-08-20 20:22:00 +09009468 attach_entity_load_avg(cfs_rq, se);
Peter Zijlstra7c3edd22016-07-13 10:56:25 +02009469 update_tg_load_avg(cfs_rq, false);
Byungchul Park6efdb102015-08-20 20:21:59 +09009470
Byungchul Parkdaa59402015-08-20 20:22:00 +09009471 if (!vruntime_normalized(p))
9472 se->vruntime += cfs_rq->min_vruntime;
9473}
Byungchul Park7855a352015-08-10 18:02:55 +09009474
Byungchul Parkdaa59402015-08-20 20:22:00 +09009475static void switched_from_fair(struct rq *rq, struct task_struct *p)
9476{
9477 detach_task_cfs_rq(p);
9478}
9479
9480static void switched_to_fair(struct rq *rq, struct task_struct *p)
9481{
9482 attach_task_cfs_rq(p);
9483
9484 if (task_on_rq_queued(p)) {
Byungchul Park7855a352015-08-10 18:02:55 +09009485 /*
Byungchul Parkdaa59402015-08-20 20:22:00 +09009486 * We were most likely switched from sched_rt, so
9487 * kick off the schedule if running, otherwise just see
9488 * if we can still preempt the current task.
Byungchul Park7855a352015-08-10 18:02:55 +09009489 */
Byungchul Parkdaa59402015-08-20 20:22:00 +09009490 if (rq->curr == p)
9491 resched_curr(rq);
9492 else
9493 check_preempt_curr(rq, p, 0);
Byungchul Park7855a352015-08-10 18:02:55 +09009494 }
Steven Rostedtcb469842008-01-25 21:08:22 +01009495}
9496
Srivatsa Vaddagiri83b699e2007-10-15 17:00:08 +02009497/* Account for a task changing its policy or group.
9498 *
9499 * This routine is mostly called to set cfs_rq->curr field when a task
9500 * migrates between groups/classes.
9501 */
9502static void set_curr_task_fair(struct rq *rq)
9503{
9504 struct sched_entity *se = &rq->curr->se;
9505
Paul Turnerec12cb72011-07-21 09:43:30 -07009506 for_each_sched_entity(se) {
9507 struct cfs_rq *cfs_rq = cfs_rq_of(se);
9508
9509 set_next_entity(cfs_rq, se);
9510 /* ensure bandwidth has been allocated on our new cfs_rq */
9511 account_cfs_rq_runtime(cfs_rq, 0);
9512 }
Srivatsa Vaddagiri83b699e2007-10-15 17:00:08 +02009513}
9514
Peter Zijlstra029632f2011-10-25 10:00:11 +02009515void init_cfs_rq(struct cfs_rq *cfs_rq)
9516{
9517 cfs_rq->tasks_timeline = RB_ROOT;
Peter Zijlstra029632f2011-10-25 10:00:11 +02009518 cfs_rq->min_vruntime = (u64)(-(1LL << 20));
9519#ifndef CONFIG_64BIT
9520 cfs_rq->min_vruntime_copy = cfs_rq->min_vruntime;
9521#endif
Alex Shi141965c2013-06-26 13:05:39 +08009522#ifdef CONFIG_SMP
Yuyang Du9d89c252015-07-15 08:04:37 +08009523 atomic_long_set(&cfs_rq->removed_load_avg, 0);
9524 atomic_long_set(&cfs_rq->removed_util_avg, 0);
Paul Turner9ee474f2012-10-04 13:18:30 +02009525#endif
Peter Zijlstra029632f2011-10-25 10:00:11 +02009526}
9527
Peter Zijlstra810b3812008-02-29 15:21:01 -05009528#ifdef CONFIG_FAIR_GROUP_SCHED
Vincent Guittotea86cb42016-06-17 13:38:55 +02009529static void task_set_group_fair(struct task_struct *p)
9530{
9531 struct sched_entity *se = &p->se;
9532
9533 set_task_rq(p, task_cpu(p));
9534 se->depth = se->parent ? se->parent->depth + 1 : 0;
9535}
9536
Peter Zijlstrabc54da22015-08-31 17:13:55 +02009537static void task_move_group_fair(struct task_struct *p)
Peter Zijlstra810b3812008-02-29 15:21:01 -05009538{
Byungchul Parkdaa59402015-08-20 20:22:00 +09009539 detach_task_cfs_rq(p);
Peter Zijlstrab2b5ce02010-10-15 15:24:15 +02009540 set_task_rq(p, task_cpu(p));
Byungchul Park6efdb102015-08-20 20:21:59 +09009541
9542#ifdef CONFIG_SMP
9543 /* Tell se's cfs_rq has been changed -- migrated */
9544 p->se.avg.last_update_time = 0;
9545#endif
Byungchul Parkdaa59402015-08-20 20:22:00 +09009546 attach_task_cfs_rq(p);
Peter Zijlstra810b3812008-02-29 15:21:01 -05009547}
Peter Zijlstra029632f2011-10-25 10:00:11 +02009548
Vincent Guittotea86cb42016-06-17 13:38:55 +02009549static void task_change_group_fair(struct task_struct *p, int type)
9550{
9551 switch (type) {
9552 case TASK_SET_GROUP:
9553 task_set_group_fair(p);
9554 break;
9555
9556 case TASK_MOVE_GROUP:
9557 task_move_group_fair(p);
9558 break;
9559 }
9560}
9561
Peter Zijlstra029632f2011-10-25 10:00:11 +02009562void free_fair_sched_group(struct task_group *tg)
9563{
9564 int i;
9565
9566 destroy_cfs_bandwidth(tg_cfs_bandwidth(tg));
9567
9568 for_each_possible_cpu(i) {
9569 if (tg->cfs_rq)
9570 kfree(tg->cfs_rq[i]);
Peter Zijlstra6fe1f342016-01-21 22:24:16 +01009571 if (tg->se)
Peter Zijlstra029632f2011-10-25 10:00:11 +02009572 kfree(tg->se[i]);
9573 }
9574
9575 kfree(tg->cfs_rq);
9576 kfree(tg->se);
9577}
9578
9579int alloc_fair_sched_group(struct task_group *tg, struct task_group *parent)
9580{
Peter Zijlstra029632f2011-10-25 10:00:11 +02009581 struct sched_entity *se;
Peter Zijlstrab7fa30c2016-06-09 15:07:50 +02009582 struct cfs_rq *cfs_rq;
Peter Zijlstra029632f2011-10-25 10:00:11 +02009583 int i;
9584
9585 tg->cfs_rq = kzalloc(sizeof(cfs_rq) * nr_cpu_ids, GFP_KERNEL);
9586 if (!tg->cfs_rq)
9587 goto err;
9588 tg->se = kzalloc(sizeof(se) * nr_cpu_ids, GFP_KERNEL);
9589 if (!tg->se)
9590 goto err;
9591
9592 tg->shares = NICE_0_LOAD;
9593
9594 init_cfs_bandwidth(tg_cfs_bandwidth(tg));
9595
9596 for_each_possible_cpu(i) {
9597 cfs_rq = kzalloc_node(sizeof(struct cfs_rq),
9598 GFP_KERNEL, cpu_to_node(i));
9599 if (!cfs_rq)
9600 goto err;
9601
9602 se = kzalloc_node(sizeof(struct sched_entity),
9603 GFP_KERNEL, cpu_to_node(i));
9604 if (!se)
9605 goto err_free_rq;
9606
9607 init_cfs_rq(cfs_rq);
9608 init_tg_cfs_entry(tg, cfs_rq, se, i, parent->se[i]);
Yuyang Du540247f2015-07-15 08:04:39 +08009609 init_entity_runnable_average(se);
Peter Zijlstra029632f2011-10-25 10:00:11 +02009610 }
9611
9612 return 1;
9613
9614err_free_rq:
9615 kfree(cfs_rq);
9616err:
9617 return 0;
9618}
9619
Peter Zijlstra8663e242016-06-22 14:58:02 +02009620void online_fair_sched_group(struct task_group *tg)
9621{
9622 struct sched_entity *se;
9623 struct rq *rq;
9624 int i;
9625
9626 for_each_possible_cpu(i) {
9627 rq = cpu_rq(i);
9628 se = tg->se[i];
9629
9630 raw_spin_lock_irq(&rq->lock);
9631 post_init_entity_util_avg(se);
Peter Zijlstra55e16d32016-06-22 15:14:26 +02009632 sync_throttle(tg, i);
Peter Zijlstra8663e242016-06-22 14:58:02 +02009633 raw_spin_unlock_irq(&rq->lock);
9634 }
9635}
9636
Peter Zijlstra6fe1f342016-01-21 22:24:16 +01009637void unregister_fair_sched_group(struct task_group *tg)
Peter Zijlstra029632f2011-10-25 10:00:11 +02009638{
Peter Zijlstra029632f2011-10-25 10:00:11 +02009639 unsigned long flags;
Peter Zijlstra6fe1f342016-01-21 22:24:16 +01009640 struct rq *rq;
9641 int cpu;
Peter Zijlstra029632f2011-10-25 10:00:11 +02009642
Peter Zijlstra6fe1f342016-01-21 22:24:16 +01009643 for_each_possible_cpu(cpu) {
9644 if (tg->se[cpu])
9645 remove_entity_load_avg(tg->se[cpu]);
Peter Zijlstra029632f2011-10-25 10:00:11 +02009646
Peter Zijlstra6fe1f342016-01-21 22:24:16 +01009647 /*
9648 * Only empty task groups can be destroyed; so we can speculatively
9649 * check on_list without danger of it being re-added.
9650 */
9651 if (!tg->cfs_rq[cpu]->on_list)
9652 continue;
9653
9654 rq = cpu_rq(cpu);
9655
9656 raw_spin_lock_irqsave(&rq->lock, flags);
9657 list_del_leaf_cfs_rq(tg->cfs_rq[cpu]);
9658 raw_spin_unlock_irqrestore(&rq->lock, flags);
9659 }
Peter Zijlstra029632f2011-10-25 10:00:11 +02009660}
9661
9662void init_tg_cfs_entry(struct task_group *tg, struct cfs_rq *cfs_rq,
9663 struct sched_entity *se, int cpu,
9664 struct sched_entity *parent)
9665{
9666 struct rq *rq = cpu_rq(cpu);
9667
9668 cfs_rq->tg = tg;
9669 cfs_rq->rq = rq;
Peter Zijlstra029632f2011-10-25 10:00:11 +02009670 init_cfs_rq_runtime(cfs_rq);
9671
9672 tg->cfs_rq[cpu] = cfs_rq;
9673 tg->se[cpu] = se;
9674
9675 /* se could be NULL for root_task_group */
9676 if (!se)
9677 return;
9678
Peter Zijlstrafed14d42012-02-11 06:05:00 +01009679 if (!parent) {
Peter Zijlstra029632f2011-10-25 10:00:11 +02009680 se->cfs_rq = &rq->cfs;
Peter Zijlstrafed14d42012-02-11 06:05:00 +01009681 se->depth = 0;
9682 } else {
Peter Zijlstra029632f2011-10-25 10:00:11 +02009683 se->cfs_rq = parent->my_q;
Peter Zijlstrafed14d42012-02-11 06:05:00 +01009684 se->depth = parent->depth + 1;
9685 }
Peter Zijlstra029632f2011-10-25 10:00:11 +02009686
9687 se->my_q = cfs_rq;
Paul Turner0ac9b1c2013-10-16 11:16:27 -07009688 /* guarantee group entities always have weight */
9689 update_load_set(&se->load, NICE_0_LOAD);
Peter Zijlstra029632f2011-10-25 10:00:11 +02009690 se->parent = parent;
9691}
9692
9693static DEFINE_MUTEX(shares_mutex);
9694
9695int sched_group_set_shares(struct task_group *tg, unsigned long shares)
9696{
9697 int i;
9698 unsigned long flags;
9699
9700 /*
9701 * We can't change the weight of the root cgroup.
9702 */
9703 if (!tg->se[0])
9704 return -EINVAL;
9705
9706 shares = clamp(shares, scale_load(MIN_SHARES), scale_load(MAX_SHARES));
9707
9708 mutex_lock(&shares_mutex);
9709 if (tg->shares == shares)
9710 goto done;
9711
9712 tg->shares = shares;
9713 for_each_possible_cpu(i) {
9714 struct rq *rq = cpu_rq(i);
9715 struct sched_entity *se;
9716
9717 se = tg->se[i];
9718 /* Propagate contribution to hierarchy */
9719 raw_spin_lock_irqsave(&rq->lock, flags);
Frederic Weisbecker71b1da42013-04-12 01:50:59 +02009720
9721 /* Possible calls to update_curr() need rq clock */
9722 update_rq_clock(rq);
Linus Torvalds17bc14b2012-12-14 07:20:43 -08009723 for_each_sched_entity(se)
Peter Zijlstra029632f2011-10-25 10:00:11 +02009724 update_cfs_shares(group_cfs_rq(se));
9725 raw_spin_unlock_irqrestore(&rq->lock, flags);
9726 }
9727
9728done:
9729 mutex_unlock(&shares_mutex);
9730 return 0;
9731}
9732#else /* CONFIG_FAIR_GROUP_SCHED */
9733
9734void free_fair_sched_group(struct task_group *tg) { }
9735
9736int alloc_fair_sched_group(struct task_group *tg, struct task_group *parent)
9737{
9738 return 1;
9739}
9740
Peter Zijlstra8663e242016-06-22 14:58:02 +02009741void online_fair_sched_group(struct task_group *tg) { }
9742
Peter Zijlstra6fe1f342016-01-21 22:24:16 +01009743void unregister_fair_sched_group(struct task_group *tg) { }
Peter Zijlstra029632f2011-10-25 10:00:11 +02009744
9745#endif /* CONFIG_FAIR_GROUP_SCHED */
9746
Peter Zijlstra810b3812008-02-29 15:21:01 -05009747
H Hartley Sweeten6d686f42010-01-13 20:21:52 -07009748static unsigned int get_rr_interval_fair(struct rq *rq, struct task_struct *task)
Peter Williams0d721ce2009-09-21 01:31:53 +00009749{
9750 struct sched_entity *se = &task->se;
Peter Williams0d721ce2009-09-21 01:31:53 +00009751 unsigned int rr_interval = 0;
9752
9753 /*
9754 * Time slice is 0 for SCHED_OTHER tasks that are on an otherwise
9755 * idle runqueue:
9756 */
Peter Williams0d721ce2009-09-21 01:31:53 +00009757 if (rq->cfs.load.weight)
Zhu Yanhaia59f4e02013-01-08 12:56:52 +08009758 rr_interval = NS_TO_JIFFIES(sched_slice(cfs_rq_of(se), se));
Peter Williams0d721ce2009-09-21 01:31:53 +00009759
9760 return rr_interval;
9761}
9762
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02009763/*
9764 * All the scheduling class methods:
9765 */
Peter Zijlstra029632f2011-10-25 10:00:11 +02009766const struct sched_class fair_sched_class = {
Ingo Molnar5522d5d2007-10-15 17:00:12 +02009767 .next = &idle_sched_class,
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02009768 .enqueue_task = enqueue_task_fair,
9769 .dequeue_task = dequeue_task_fair,
9770 .yield_task = yield_task_fair,
Mike Galbraithd95f4122011-02-01 09:50:51 -05009771 .yield_to_task = yield_to_task_fair,
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02009772
Ingo Molnar2e09bf52007-10-15 17:00:05 +02009773 .check_preempt_curr = check_preempt_wakeup,
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02009774
9775 .pick_next_task = pick_next_task_fair,
9776 .put_prev_task = put_prev_task_fair,
9777
Peter Williams681f3e62007-10-24 18:23:51 +02009778#ifdef CONFIG_SMP
Li Zefan4ce72a22008-10-22 15:25:26 +08009779 .select_task_rq = select_task_rq_fair,
Paul Turner0a74bef2012-10-04 13:18:30 +02009780 .migrate_task_rq = migrate_task_rq_fair,
Alex Shi141965c2013-06-26 13:05:39 +08009781
Christian Ehrhardt0bcdcf22009-11-30 12:16:46 +01009782 .rq_online = rq_online_fair,
9783 .rq_offline = rq_offline_fair,
Peter Zijlstra88ec22d2009-12-16 18:04:41 +01009784
Yuyang Du12695572015-07-15 08:04:40 +08009785 .task_dead = task_dead_fair,
Peter Zijlstrac5b28032015-05-15 17:43:35 +02009786 .set_cpus_allowed = set_cpus_allowed_common,
Peter Williams681f3e62007-10-24 18:23:51 +02009787#endif
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02009788
Srivatsa Vaddagiri83b699e2007-10-15 17:00:08 +02009789 .set_curr_task = set_curr_task_fair,
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02009790 .task_tick = task_tick_fair,
Peter Zijlstracd29fe62009-11-27 17:32:46 +01009791 .task_fork = task_fork_fair,
Steven Rostedtcb469842008-01-25 21:08:22 +01009792
9793 .prio_changed = prio_changed_fair,
Peter Zijlstrada7a7352011-01-17 17:03:27 +01009794 .switched_from = switched_from_fair,
Steven Rostedtcb469842008-01-25 21:08:22 +01009795 .switched_to = switched_to_fair,
Peter Zijlstra810b3812008-02-29 15:21:01 -05009796
Peter Williams0d721ce2009-09-21 01:31:53 +00009797 .get_rr_interval = get_rr_interval_fair,
9798
Stanislaw Gruszka6e998912014-11-12 16:58:44 +01009799 .update_curr = update_curr_fair,
9800
Peter Zijlstra810b3812008-02-29 15:21:01 -05009801#ifdef CONFIG_FAIR_GROUP_SCHED
Vincent Guittotea86cb42016-06-17 13:38:55 +02009802 .task_change_group = task_change_group_fair,
Peter Zijlstra810b3812008-02-29 15:21:01 -05009803#endif
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02009804};
9805
9806#ifdef CONFIG_SCHED_DEBUG
Peter Zijlstra029632f2011-10-25 10:00:11 +02009807void print_cfs_stats(struct seq_file *m, int cpu)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02009808{
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02009809 struct cfs_rq *cfs_rq;
9810
Peter Zijlstra5973e5b2008-01-25 21:08:34 +01009811 rcu_read_lock();
Ingo Molnarc3b64f12007-08-09 11:16:51 +02009812 for_each_leaf_cfs_rq(cpu_rq(cpu), cfs_rq)
Ingo Molnar5cef9ec2007-08-09 11:16:47 +02009813 print_cfs_rq(m, cpu, cfs_rq);
Peter Zijlstra5973e5b2008-01-25 21:08:34 +01009814 rcu_read_unlock();
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02009815}
Srikar Dronamraju397f2372015-06-25 22:51:43 +05309816
9817#ifdef CONFIG_NUMA_BALANCING
9818void show_numa_stats(struct task_struct *p, struct seq_file *m)
9819{
9820 int node;
9821 unsigned long tsf = 0, tpf = 0, gsf = 0, gpf = 0;
9822
9823 for_each_online_node(node) {
9824 if (p->numa_faults) {
9825 tsf = p->numa_faults[task_faults_idx(NUMA_MEM, node, 0)];
9826 tpf = p->numa_faults[task_faults_idx(NUMA_MEM, node, 1)];
9827 }
9828 if (p->numa_group) {
9829 gsf = p->numa_group->faults[task_faults_idx(NUMA_MEM, node, 0)],
9830 gpf = p->numa_group->faults[task_faults_idx(NUMA_MEM, node, 1)];
9831 }
9832 print_numa_stats(m, node, tsf, tpf, gsf, gpf);
9833 }
9834}
9835#endif /* CONFIG_NUMA_BALANCING */
9836#endif /* CONFIG_SCHED_DEBUG */
Peter Zijlstra029632f2011-10-25 10:00:11 +02009837
9838__init void init_sched_fair_class(void)
9839{
9840#ifdef CONFIG_SMP
9841 open_softirq(SCHED_SOFTIRQ, run_rebalance_domains);
9842
Frederic Weisbecker3451d022011-08-10 23:21:01 +02009843#ifdef CONFIG_NO_HZ_COMMON
Diwakar Tundlam554ceca2012-03-07 14:44:26 -08009844 nohz.next_balance = jiffies;
Peter Zijlstra029632f2011-10-25 10:00:11 +02009845 zalloc_cpumask_var(&nohz.idle_cpus_mask, GFP_NOWAIT);
Peter Zijlstra029632f2011-10-25 10:00:11 +02009846#endif
9847#endif /* SMP */
9848
9849}