blob: e84ec63a39746135f2eca33317c9c4a06fb83ad6 [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
Peter Zijlstra029632f2011-10-25 10:00:11 +020034#include "sched.h"
Syed Rameez Mustafadddcab72016-09-07 16:18:27 -070035#include <trace/events/sched.h>
Arjan van de Ven97455122008-01-25 21:08:34 +010036
Ingo Molnarbf0f6f22007-07-09 18:51:58 +020037/*
Peter Zijlstra21805082007-08-25 18:41:53 +020038 * Targeted preemption latency for CPU-bound tasks:
Takuya Yoshikawa864616e2010-10-14 16:09:13 +090039 * (default: 6ms * (1 + ilog(ncpus)), units: nanoseconds)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +020040 *
Peter Zijlstra21805082007-08-25 18:41:53 +020041 * NOTE: this latency value is not the same as the concept of
Ingo Molnard274a4c2007-10-15 17:00:14 +020042 * 'timeslice length' - timeslices in CFS are of variable length
43 * and have no persistent notion like in traditional, time-slice
44 * based scheduling concepts.
Ingo Molnarbf0f6f22007-07-09 18:51:58 +020045 *
Ingo Molnard274a4c2007-10-15 17:00:14 +020046 * (to see the precise effective timeslice length of your workload,
47 * run vmstat and monitor the context-switches (cs) field)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +020048 */
Mike Galbraith21406922010-03-11 17:17:15 +010049unsigned int sysctl_sched_latency = 6000000ULL;
50unsigned int normalized_sysctl_sched_latency = 6000000ULL;
Ingo Molnar2bd8e6d2007-10-15 17:00:02 +020051
52/*
Christian Ehrhardt1983a922009-11-30 12:16:47 +010053 * The initial- and re-scaling of tunables is configurable
54 * (default SCHED_TUNABLESCALING_LOG = *(1+ilog(ncpus))
55 *
56 * Options are:
57 * SCHED_TUNABLESCALING_NONE - unscaled, always *1
58 * SCHED_TUNABLESCALING_LOG - scaled logarithmical, *1+ilog(ncpus)
59 * SCHED_TUNABLESCALING_LINEAR - scaled linear, *ncpus
60 */
61enum sched_tunable_scaling sysctl_sched_tunable_scaling
62 = SCHED_TUNABLESCALING_LOG;
63
64/*
Peter Zijlstrab2be5e92007-11-09 22:39:37 +010065 * Minimal preemption granularity for CPU-bound tasks:
Takuya Yoshikawa864616e2010-10-14 16:09:13 +090066 * (default: 0.75 msec * (1 + ilog(ncpus)), units: nanoseconds)
Peter Zijlstrab2be5e92007-11-09 22:39:37 +010067 */
Ingo Molnar0bf377b2010-09-12 08:14:52 +020068unsigned int sysctl_sched_min_granularity = 750000ULL;
69unsigned int normalized_sysctl_sched_min_granularity = 750000ULL;
Peter Zijlstrab2be5e92007-11-09 22:39:37 +010070
71/*
72 * is kept at sysctl_sched_latency / sysctl_sched_min_granularity
73 */
Ingo Molnar0bf377b2010-09-12 08:14:52 +020074static unsigned int sched_nr_latency = 8;
Peter Zijlstrab2be5e92007-11-09 22:39:37 +010075
76/*
Mike Galbraith2bba22c2009-09-09 15:41:37 +020077 * After fork, child runs first. If set to 0 (default) then
Ingo Molnar2bd8e6d2007-10-15 17:00:02 +020078 * parent will (try to) run first.
79 */
Mike Galbraith2bba22c2009-09-09 15:41:37 +020080unsigned int sysctl_sched_child_runs_first __read_mostly;
Peter Zijlstra21805082007-08-25 18:41:53 +020081
82/*
Syed Rameez Mustafadddcab72016-09-07 16:18:27 -070083 * Controls whether, when SD_SHARE_PKG_RESOURCES is on, if all
84 * tasks go to idle CPUs when woken. If this is off, note that the
85 * per-task flag PF_WAKE_UP_IDLE can still cause a task to go to an
86 * idle CPU upon being woken.
87 */
88unsigned int __read_mostly sysctl_sched_wake_to_idle;
89
90/*
Ingo Molnarbf0f6f22007-07-09 18:51:58 +020091 * SCHED_OTHER wake-up granularity.
Mike Galbraith172e0822009-09-09 15:41:37 +020092 * (default: 1 msec * (1 + ilog(ncpus)), units: nanoseconds)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +020093 *
94 * This option delays the preemption effects of decoupled workloads
95 * and reduces their over-scheduling. Synchronous workloads will still
96 * have immediate wakeup/sleep latencies.
97 */
Mike Galbraith172e0822009-09-09 15:41:37 +020098unsigned int sysctl_sched_wakeup_granularity = 1000000UL;
Christian Ehrhardt0bcdcf22009-11-30 12:16:46 +010099unsigned int normalized_sysctl_sched_wakeup_granularity = 1000000UL;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200100
Ingo Molnarda84d962007-10-15 17:00:18 +0200101const_debug unsigned int sysctl_sched_migration_cost = 500000UL;
102
Paul Turnera7a4f8a2010-11-15 15:47:06 -0800103/*
104 * The exponential sliding window over which load is averaged for shares
105 * distribution.
106 * (default: 10msec)
107 */
108unsigned int __read_mostly sysctl_sched_shares_window = 10000000UL;
109
Paul Turnerec12cb72011-07-21 09:43:30 -0700110#ifdef CONFIG_CFS_BANDWIDTH
111/*
112 * Amount of runtime to allocate from global (tg) to local (per-cfs_rq) pool
113 * each time a cfs_rq requests quota.
114 *
115 * Note: in the case that the slice exceeds the runtime remaining (either due
116 * to consumption or the quota being specified to be smaller than the slice)
117 * we will always only issue the remaining available time.
118 *
119 * default: 5 msec, units: microseconds
120 */
121unsigned int sysctl_sched_cfs_bandwidth_slice = 5000UL;
122#endif
123
Morten Rasmussen32731632016-07-25 14:34:26 +0100124/*
125 * The margin used when comparing utilization with CPU capacity:
126 * util * 1024 < capacity * margin
127 */
128unsigned int capacity_margin = 1280; /* ~20% */
129
Paul Gortmaker85276322013-04-19 15:10:50 -0400130static inline void update_load_add(struct load_weight *lw, unsigned long inc)
131{
132 lw->weight += inc;
133 lw->inv_weight = 0;
134}
135
136static inline void update_load_sub(struct load_weight *lw, unsigned long dec)
137{
138 lw->weight -= dec;
139 lw->inv_weight = 0;
140}
141
142static inline void update_load_set(struct load_weight *lw, unsigned long w)
143{
144 lw->weight = w;
145 lw->inv_weight = 0;
146}
147
Peter Zijlstra029632f2011-10-25 10:00:11 +0200148/*
149 * Increase the granularity value when there are more CPUs,
150 * because with more CPUs the 'effective latency' as visible
151 * to users decreases. But the relationship is not linear,
152 * so pick a second-best guess by going with the log2 of the
153 * number of CPUs.
154 *
155 * This idea comes from the SD scheduler of Con Kolivas:
156 */
Nicholas Mc Guire58ac93e2015-05-15 21:05:42 +0200157static unsigned int get_update_sysctl_factor(void)
Peter Zijlstra029632f2011-10-25 10:00:11 +0200158{
Nicholas Mc Guire58ac93e2015-05-15 21:05:42 +0200159 unsigned int cpus = min_t(unsigned int, num_online_cpus(), 8);
Peter Zijlstra029632f2011-10-25 10:00:11 +0200160 unsigned int factor;
161
162 switch (sysctl_sched_tunable_scaling) {
163 case SCHED_TUNABLESCALING_NONE:
164 factor = 1;
165 break;
166 case SCHED_TUNABLESCALING_LINEAR:
167 factor = cpus;
168 break;
169 case SCHED_TUNABLESCALING_LOG:
170 default:
171 factor = 1 + ilog2(cpus);
172 break;
173 }
174
175 return factor;
176}
177
178static void update_sysctl(void)
179{
180 unsigned int factor = get_update_sysctl_factor();
181
182#define SET_SYSCTL(name) \
183 (sysctl_##name = (factor) * normalized_sysctl_##name)
184 SET_SYSCTL(sched_min_granularity);
185 SET_SYSCTL(sched_latency);
186 SET_SYSCTL(sched_wakeup_granularity);
187#undef SET_SYSCTL
188}
189
190void sched_init_granularity(void)
191{
192 update_sysctl();
193}
194
Peter Zijlstra9dbdb152013-11-18 18:27:06 +0100195#define WMULT_CONST (~0U)
Peter Zijlstra029632f2011-10-25 10:00:11 +0200196#define WMULT_SHIFT 32
197
Peter Zijlstra9dbdb152013-11-18 18:27:06 +0100198static void __update_inv_weight(struct load_weight *lw)
Peter Zijlstra029632f2011-10-25 10:00:11 +0200199{
Peter Zijlstra9dbdb152013-11-18 18:27:06 +0100200 unsigned long w;
Peter Zijlstra029632f2011-10-25 10:00:11 +0200201
Peter Zijlstra9dbdb152013-11-18 18:27:06 +0100202 if (likely(lw->inv_weight))
203 return;
204
205 w = scale_load_down(lw->weight);
206
207 if (BITS_PER_LONG > 32 && unlikely(w >= WMULT_CONST))
208 lw->inv_weight = 1;
209 else if (unlikely(!w))
210 lw->inv_weight = WMULT_CONST;
Peter Zijlstra029632f2011-10-25 10:00:11 +0200211 else
Peter Zijlstra9dbdb152013-11-18 18:27:06 +0100212 lw->inv_weight = WMULT_CONST / w;
213}
Peter Zijlstra029632f2011-10-25 10:00:11 +0200214
Peter Zijlstra9dbdb152013-11-18 18:27:06 +0100215/*
216 * delta_exec * weight / lw.weight
217 * OR
218 * (delta_exec * (weight * lw->inv_weight)) >> WMULT_SHIFT
219 *
Yuyang Du1c3de5e2016-03-30 07:07:51 +0800220 * Either weight := NICE_0_LOAD and lw \e sched_prio_to_wmult[], in which case
Peter Zijlstra9dbdb152013-11-18 18:27:06 +0100221 * we're guaranteed shift stays positive because inv_weight is guaranteed to
222 * fit 32 bits, and NICE_0_LOAD gives another 10 bits; therefore shift >= 22.
223 *
224 * Or, weight =< lw.weight (because lw.weight is the runqueue weight), thus
225 * weight/lw.weight <= 1, and therefore our shift will also be positive.
226 */
227static u64 __calc_delta(u64 delta_exec, unsigned long weight, struct load_weight *lw)
228{
229 u64 fact = scale_load_down(weight);
230 int shift = WMULT_SHIFT;
Peter Zijlstra029632f2011-10-25 10:00:11 +0200231
Peter Zijlstra9dbdb152013-11-18 18:27:06 +0100232 __update_inv_weight(lw);
233
234 if (unlikely(fact >> 32)) {
235 while (fact >> 32) {
236 fact >>= 1;
237 shift--;
238 }
Peter Zijlstra029632f2011-10-25 10:00:11 +0200239 }
240
Peter Zijlstra9dbdb152013-11-18 18:27:06 +0100241 /* hint to use a 32x32->64 mul */
242 fact = (u64)(u32)fact * lw->inv_weight;
Peter Zijlstra029632f2011-10-25 10:00:11 +0200243
Peter Zijlstra9dbdb152013-11-18 18:27:06 +0100244 while (fact >> 32) {
245 fact >>= 1;
246 shift--;
247 }
248
249 return mul_u64_u32_shr(delta_exec, fact, shift);
Peter Zijlstra029632f2011-10-25 10:00:11 +0200250}
251
Syed Rameez Mustafadddcab72016-09-07 16:18:27 -0700252#ifdef CONFIG_SMP
253static int active_load_balance_cpu_stop(void *data);
254#endif
Peter Zijlstra029632f2011-10-25 10:00:11 +0200255
256const struct sched_class fair_sched_class;
Peter Zijlstraa4c2f002008-10-17 19:27:03 +0200257
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200258/**************************************************************
259 * CFS operations on generic schedulable entities:
260 */
261
262#ifdef CONFIG_FAIR_GROUP_SCHED
263
264/* cpu runqueue to which this cfs_rq is attached */
265static inline struct rq *rq_of(struct cfs_rq *cfs_rq)
266{
267 return cfs_rq->rq;
268}
269
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200270/* An entity is a task if it doesn't "own" a runqueue */
271#define entity_is_task(se) (!se->my_q)
272
Peter Zijlstra8f488942009-07-24 12:25:30 +0200273static inline struct task_struct *task_of(struct sched_entity *se)
274{
Peter Zijlstra9148a3a2016-09-20 22:34:51 +0200275 SCHED_WARN_ON(!entity_is_task(se));
Peter Zijlstra8f488942009-07-24 12:25:30 +0200276 return container_of(se, struct task_struct, se);
277}
278
Peter Zijlstrab7581492008-04-19 19:45:00 +0200279/* Walk up scheduling entities hierarchy */
280#define for_each_sched_entity(se) \
281 for (; se; se = se->parent)
282
283static inline struct cfs_rq *task_cfs_rq(struct task_struct *p)
284{
285 return p->se.cfs_rq;
286}
287
288/* runqueue on which this entity is (to be) queued */
289static inline struct cfs_rq *cfs_rq_of(struct sched_entity *se)
290{
291 return se->cfs_rq;
292}
293
294/* runqueue "owned" by this group */
295static inline struct cfs_rq *group_cfs_rq(struct sched_entity *grp)
296{
297 return grp->my_q;
298}
299
Peter Zijlstra3d4b47b2010-11-15 15:47:01 -0800300static inline void list_add_leaf_cfs_rq(struct cfs_rq *cfs_rq)
301{
302 if (!cfs_rq->on_list) {
Paul Turner67e86252010-11-15 15:47:05 -0800303 /*
304 * Ensure we either appear before our parent (if already
305 * enqueued) or force our parent to appear after us when it is
306 * enqueued. The fact that we always enqueue bottom-up
307 * reduces this to two cases.
308 */
309 if (cfs_rq->tg->parent &&
310 cfs_rq->tg->parent->cfs_rq[cpu_of(rq_of(cfs_rq))]->on_list) {
311 list_add_rcu(&cfs_rq->leaf_cfs_rq_list,
Peter Zijlstra3d4b47b2010-11-15 15:47:01 -0800312 &rq_of(cfs_rq)->leaf_cfs_rq_list);
Paul Turner67e86252010-11-15 15:47:05 -0800313 } else {
314 list_add_tail_rcu(&cfs_rq->leaf_cfs_rq_list,
315 &rq_of(cfs_rq)->leaf_cfs_rq_list);
316 }
Peter Zijlstra3d4b47b2010-11-15 15:47:01 -0800317
318 cfs_rq->on_list = 1;
319 }
320}
321
322static inline void list_del_leaf_cfs_rq(struct cfs_rq *cfs_rq)
323{
324 if (cfs_rq->on_list) {
325 list_del_rcu(&cfs_rq->leaf_cfs_rq_list);
326 cfs_rq->on_list = 0;
327 }
328}
329
Peter Zijlstrab7581492008-04-19 19:45:00 +0200330/* Iterate thr' all leaf cfs_rq's on a runqueue */
331#define for_each_leaf_cfs_rq(rq, cfs_rq) \
332 list_for_each_entry_rcu(cfs_rq, &rq->leaf_cfs_rq_list, leaf_cfs_rq_list)
333
334/* Do the two (enqueued) entities belong to the same group ? */
Peter Zijlstrafed14d42012-02-11 06:05:00 +0100335static inline struct cfs_rq *
Peter Zijlstrab7581492008-04-19 19:45:00 +0200336is_same_group(struct sched_entity *se, struct sched_entity *pse)
337{
338 if (se->cfs_rq == pse->cfs_rq)
Peter Zijlstrafed14d42012-02-11 06:05:00 +0100339 return se->cfs_rq;
Peter Zijlstrab7581492008-04-19 19:45:00 +0200340
Peter Zijlstrafed14d42012-02-11 06:05:00 +0100341 return NULL;
Peter Zijlstrab7581492008-04-19 19:45:00 +0200342}
343
344static inline struct sched_entity *parent_entity(struct sched_entity *se)
345{
346 return se->parent;
347}
348
Peter Zijlstra464b7522008-10-24 11:06:15 +0200349static void
350find_matching_se(struct sched_entity **se, struct sched_entity **pse)
351{
352 int se_depth, pse_depth;
353
354 /*
355 * preemption test can be made between sibling entities who are in the
356 * same cfs_rq i.e who have a common parent. Walk up the hierarchy of
357 * both tasks until we find their ancestors who are siblings of common
358 * parent.
359 */
360
361 /* First walk up until both entities are at same depth */
Peter Zijlstrafed14d42012-02-11 06:05:00 +0100362 se_depth = (*se)->depth;
363 pse_depth = (*pse)->depth;
Peter Zijlstra464b7522008-10-24 11:06:15 +0200364
365 while (se_depth > pse_depth) {
366 se_depth--;
367 *se = parent_entity(*se);
368 }
369
370 while (pse_depth > se_depth) {
371 pse_depth--;
372 *pse = parent_entity(*pse);
373 }
374
375 while (!is_same_group(*se, *pse)) {
376 *se = parent_entity(*se);
377 *pse = parent_entity(*pse);
378 }
379}
380
Peter Zijlstra8f488942009-07-24 12:25:30 +0200381#else /* !CONFIG_FAIR_GROUP_SCHED */
382
383static inline struct task_struct *task_of(struct sched_entity *se)
384{
385 return container_of(se, struct task_struct, se);
386}
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200387
388static inline struct rq *rq_of(struct cfs_rq *cfs_rq)
389{
390 return container_of(cfs_rq, struct rq, cfs);
391}
392
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200393#define entity_is_task(se) 1
394
Peter Zijlstrab7581492008-04-19 19:45:00 +0200395#define for_each_sched_entity(se) \
396 for (; se; se = NULL)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200397
Peter Zijlstrab7581492008-04-19 19:45:00 +0200398static inline struct cfs_rq *task_cfs_rq(struct task_struct *p)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200399{
Peter Zijlstrab7581492008-04-19 19:45:00 +0200400 return &task_rq(p)->cfs;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200401}
402
Peter Zijlstrab7581492008-04-19 19:45:00 +0200403static inline struct cfs_rq *cfs_rq_of(struct sched_entity *se)
404{
405 struct task_struct *p = task_of(se);
406 struct rq *rq = task_rq(p);
407
408 return &rq->cfs;
409}
410
411/* runqueue "owned" by this group */
412static inline struct cfs_rq *group_cfs_rq(struct sched_entity *grp)
413{
414 return NULL;
415}
416
Peter Zijlstra3d4b47b2010-11-15 15:47:01 -0800417static inline void list_add_leaf_cfs_rq(struct cfs_rq *cfs_rq)
418{
419}
420
421static inline void list_del_leaf_cfs_rq(struct cfs_rq *cfs_rq)
422{
423}
424
Peter Zijlstrab7581492008-04-19 19:45:00 +0200425#define for_each_leaf_cfs_rq(rq, cfs_rq) \
426 for (cfs_rq = &rq->cfs; cfs_rq; cfs_rq = NULL)
427
Peter Zijlstrab7581492008-04-19 19:45:00 +0200428static inline struct sched_entity *parent_entity(struct sched_entity *se)
429{
430 return NULL;
431}
432
Peter Zijlstra464b7522008-10-24 11:06:15 +0200433static inline void
434find_matching_se(struct sched_entity **se, struct sched_entity **pse)
435{
436}
437
Peter Zijlstrab7581492008-04-19 19:45:00 +0200438#endif /* CONFIG_FAIR_GROUP_SCHED */
439
Peter Zijlstra6c16a6d2012-03-21 13:07:16 -0700440static __always_inline
Peter Zijlstra9dbdb152013-11-18 18:27:06 +0100441void account_cfs_rq_runtime(struct cfs_rq *cfs_rq, u64 delta_exec);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200442
443/**************************************************************
444 * Scheduling class tree data structure manipulation methods:
445 */
446
Andrei Epure1bf08232013-03-12 21:12:24 +0200447static inline u64 max_vruntime(u64 max_vruntime, u64 vruntime)
Peter Zijlstra02e04312007-10-15 17:00:07 +0200448{
Andrei Epure1bf08232013-03-12 21:12:24 +0200449 s64 delta = (s64)(vruntime - max_vruntime);
Peter Zijlstra368059a2007-10-15 17:00:11 +0200450 if (delta > 0)
Andrei Epure1bf08232013-03-12 21:12:24 +0200451 max_vruntime = vruntime;
Peter Zijlstra02e04312007-10-15 17:00:07 +0200452
Andrei Epure1bf08232013-03-12 21:12:24 +0200453 return max_vruntime;
Peter Zijlstra02e04312007-10-15 17:00:07 +0200454}
455
Ingo Molnar0702e3e2007-10-15 17:00:14 +0200456static inline u64 min_vruntime(u64 min_vruntime, u64 vruntime)
Peter Zijlstrab0ffd242007-10-15 17:00:12 +0200457{
458 s64 delta = (s64)(vruntime - min_vruntime);
459 if (delta < 0)
460 min_vruntime = vruntime;
461
462 return min_vruntime;
463}
464
Fabio Checconi54fdc582009-07-16 12:32:27 +0200465static inline int entity_before(struct sched_entity *a,
466 struct sched_entity *b)
467{
468 return (s64)(a->vruntime - b->vruntime) < 0;
469}
470
Peter Zijlstra1af5f732008-10-24 11:06:13 +0200471static void update_min_vruntime(struct cfs_rq *cfs_rq)
472{
Peter Zijlstrab60205c2016-09-20 21:58:12 +0200473 struct sched_entity *curr = cfs_rq->curr;
474
Peter Zijlstra1af5f732008-10-24 11:06:13 +0200475 u64 vruntime = cfs_rq->min_vruntime;
476
Peter Zijlstrab60205c2016-09-20 21:58:12 +0200477 if (curr) {
478 if (curr->on_rq)
479 vruntime = curr->vruntime;
480 else
481 curr = NULL;
482 }
Peter Zijlstra1af5f732008-10-24 11:06:13 +0200483
484 if (cfs_rq->rb_leftmost) {
485 struct sched_entity *se = rb_entry(cfs_rq->rb_leftmost,
486 struct sched_entity,
487 run_node);
488
Peter Zijlstrab60205c2016-09-20 21:58:12 +0200489 if (!curr)
Peter Zijlstra1af5f732008-10-24 11:06:13 +0200490 vruntime = se->vruntime;
491 else
492 vruntime = min_vruntime(vruntime, se->vruntime);
493 }
494
Andrei Epure1bf08232013-03-12 21:12:24 +0200495 /* ensure we never gain time by being placed backwards. */
Peter Zijlstra1af5f732008-10-24 11:06:13 +0200496 cfs_rq->min_vruntime = max_vruntime(cfs_rq->min_vruntime, vruntime);
Peter Zijlstra3fe16982011-04-05 17:23:48 +0200497#ifndef CONFIG_64BIT
498 smp_wmb();
499 cfs_rq->min_vruntime_copy = cfs_rq->min_vruntime;
500#endif
Peter Zijlstra1af5f732008-10-24 11:06:13 +0200501}
502
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200503/*
504 * Enqueue an entity into the rb-tree:
505 */
Ingo Molnar0702e3e2007-10-15 17:00:14 +0200506static void __enqueue_entity(struct cfs_rq *cfs_rq, struct sched_entity *se)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200507{
508 struct rb_node **link = &cfs_rq->tasks_timeline.rb_node;
509 struct rb_node *parent = NULL;
510 struct sched_entity *entry;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200511 int leftmost = 1;
512
513 /*
514 * Find the right place in the rbtree:
515 */
516 while (*link) {
517 parent = *link;
518 entry = rb_entry(parent, struct sched_entity, run_node);
519 /*
520 * We dont care about collisions. Nodes with
521 * the same key stay together.
522 */
Stephan Baerwolf2bd2d6f2011-07-20 14:46:59 +0200523 if (entity_before(se, entry)) {
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200524 link = &parent->rb_left;
525 } else {
526 link = &parent->rb_right;
527 leftmost = 0;
528 }
529 }
530
531 /*
532 * Maintain a cache of leftmost tree entries (it is frequently
533 * used):
534 */
Peter Zijlstra1af5f732008-10-24 11:06:13 +0200535 if (leftmost)
Ingo Molnar57cb4992007-10-15 17:00:11 +0200536 cfs_rq->rb_leftmost = &se->run_node;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200537
538 rb_link_node(&se->run_node, parent, link);
539 rb_insert_color(&se->run_node, &cfs_rq->tasks_timeline);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200540}
541
Ingo Molnar0702e3e2007-10-15 17:00:14 +0200542static void __dequeue_entity(struct cfs_rq *cfs_rq, struct sched_entity *se)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200543{
Peter Zijlstra3fe69742008-03-14 20:55:51 +0100544 if (cfs_rq->rb_leftmost == &se->run_node) {
545 struct rb_node *next_node;
Peter Zijlstra3fe69742008-03-14 20:55:51 +0100546
547 next_node = rb_next(&se->run_node);
548 cfs_rq->rb_leftmost = next_node;
Peter Zijlstra3fe69742008-03-14 20:55:51 +0100549 }
Ingo Molnare9acbff2007-10-15 17:00:04 +0200550
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200551 rb_erase(&se->run_node, &cfs_rq->tasks_timeline);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200552}
553
Peter Zijlstra029632f2011-10-25 10:00:11 +0200554struct sched_entity *__pick_first_entity(struct cfs_rq *cfs_rq)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200555{
Peter Zijlstraf4b67552008-11-04 21:25:07 +0100556 struct rb_node *left = cfs_rq->rb_leftmost;
557
558 if (!left)
559 return NULL;
560
561 return rb_entry(left, struct sched_entity, run_node);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200562}
563
Rik van Rielac53db52011-02-01 09:51:03 -0500564static struct sched_entity *__pick_next_entity(struct sched_entity *se)
565{
566 struct rb_node *next = rb_next(&se->run_node);
567
568 if (!next)
569 return NULL;
570
571 return rb_entry(next, struct sched_entity, run_node);
572}
573
574#ifdef CONFIG_SCHED_DEBUG
Peter Zijlstra029632f2011-10-25 10:00:11 +0200575struct sched_entity *__pick_last_entity(struct cfs_rq *cfs_rq)
Peter Zijlstraaeb73b02007-10-15 17:00:05 +0200576{
Ingo Molnar7eee3e62008-02-22 10:32:21 +0100577 struct rb_node *last = rb_last(&cfs_rq->tasks_timeline);
Peter Zijlstraaeb73b02007-10-15 17:00:05 +0200578
Balbir Singh70eee742008-02-22 13:25:53 +0530579 if (!last)
580 return NULL;
Ingo Molnar7eee3e62008-02-22 10:32:21 +0100581
582 return rb_entry(last, struct sched_entity, run_node);
Peter Zijlstraaeb73b02007-10-15 17:00:05 +0200583}
584
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200585/**************************************************************
586 * Scheduling class statistics methods:
587 */
588
Christian Ehrhardtacb4a842009-11-30 12:16:48 +0100589int sched_proc_update_handler(struct ctl_table *table, int write,
Alexey Dobriyan8d65af72009-09-23 15:57:19 -0700590 void __user *buffer, size_t *lenp,
Peter Zijlstrab2be5e92007-11-09 22:39:37 +0100591 loff_t *ppos)
592{
Alexey Dobriyan8d65af72009-09-23 15:57:19 -0700593 int ret = proc_dointvec_minmax(table, write, buffer, lenp, ppos);
Nicholas Mc Guire58ac93e2015-05-15 21:05:42 +0200594 unsigned int factor = get_update_sysctl_factor();
Peter Zijlstrab2be5e92007-11-09 22:39:37 +0100595
596 if (ret || !write)
597 return ret;
598
599 sched_nr_latency = DIV_ROUND_UP(sysctl_sched_latency,
600 sysctl_sched_min_granularity);
601
Christian Ehrhardtacb4a842009-11-30 12:16:48 +0100602#define WRT_SYSCTL(name) \
603 (normalized_sysctl_##name = sysctl_##name / (factor))
604 WRT_SYSCTL(sched_min_granularity);
605 WRT_SYSCTL(sched_latency);
606 WRT_SYSCTL(sched_wakeup_granularity);
Christian Ehrhardtacb4a842009-11-30 12:16:48 +0100607#undef WRT_SYSCTL
608
Peter Zijlstrab2be5e92007-11-09 22:39:37 +0100609 return 0;
610}
611#endif
Ingo Molnar647e7ca2007-10-15 17:00:13 +0200612
613/*
Peter Zijlstraf9c0b092008-10-17 19:27:04 +0200614 * delta /= w
Peter Zijlstraa7be37a2008-06-27 13:41:11 +0200615 */
Peter Zijlstra9dbdb152013-11-18 18:27:06 +0100616static inline u64 calc_delta_fair(u64 delta, struct sched_entity *se)
Peter Zijlstraa7be37a2008-06-27 13:41:11 +0200617{
Peter Zijlstraf9c0b092008-10-17 19:27:04 +0200618 if (unlikely(se->load.weight != NICE_0_LOAD))
Peter Zijlstra9dbdb152013-11-18 18:27:06 +0100619 delta = __calc_delta(delta, NICE_0_LOAD, &se->load);
Peter Zijlstraa7be37a2008-06-27 13:41:11 +0200620
621 return delta;
622}
623
624/*
Ingo Molnar647e7ca2007-10-15 17:00:13 +0200625 * The idea is to set a period in which each task runs once.
626 *
Borislav Petkov532b1852012-08-08 16:16:04 +0200627 * When there are too many tasks (sched_nr_latency) we have to stretch
Ingo Molnar647e7ca2007-10-15 17:00:13 +0200628 * this period because otherwise the slices get too small.
629 *
630 * p = (nr <= nl) ? l : l*nr/nl
631 */
Peter Zijlstra4d78e7b2007-10-15 17:00:04 +0200632static u64 __sched_period(unsigned long nr_running)
633{
Boqun Feng8e2b0bf2015-07-02 22:25:52 +0800634 if (unlikely(nr_running > sched_nr_latency))
635 return nr_running * sysctl_sched_min_granularity;
636 else
637 return sysctl_sched_latency;
Peter Zijlstra4d78e7b2007-10-15 17:00:04 +0200638}
639
Ingo Molnar647e7ca2007-10-15 17:00:13 +0200640/*
641 * We calculate the wall-time slice from the period by taking a part
642 * proportional to the weight.
643 *
Peter Zijlstraf9c0b092008-10-17 19:27:04 +0200644 * s = p*P[w/rw]
Ingo Molnar647e7ca2007-10-15 17:00:13 +0200645 */
Peter Zijlstra6d0f0ebd2007-10-15 17:00:05 +0200646static u64 sched_slice(struct cfs_rq *cfs_rq, struct sched_entity *se)
Peter Zijlstra21805082007-08-25 18:41:53 +0200647{
Mike Galbraith0a582442009-01-02 12:16:42 +0100648 u64 slice = __sched_period(cfs_rq->nr_running + !se->on_rq);
Peter Zijlstraf9c0b092008-10-17 19:27:04 +0200649
Mike Galbraith0a582442009-01-02 12:16:42 +0100650 for_each_sched_entity(se) {
Lin Ming6272d682009-01-15 17:17:15 +0100651 struct load_weight *load;
Christian Engelmayer3104bf02009-06-16 10:35:12 +0200652 struct load_weight lw;
Lin Ming6272d682009-01-15 17:17:15 +0100653
654 cfs_rq = cfs_rq_of(se);
655 load = &cfs_rq->load;
Peter Zijlstraf9c0b092008-10-17 19:27:04 +0200656
Mike Galbraith0a582442009-01-02 12:16:42 +0100657 if (unlikely(!se->on_rq)) {
Christian Engelmayer3104bf02009-06-16 10:35:12 +0200658 lw = cfs_rq->load;
Mike Galbraith0a582442009-01-02 12:16:42 +0100659
660 update_load_add(&lw, se->load.weight);
661 load = &lw;
662 }
Peter Zijlstra9dbdb152013-11-18 18:27:06 +0100663 slice = __calc_delta(slice, se->load.weight, load);
Mike Galbraith0a582442009-01-02 12:16:42 +0100664 }
665 return slice;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200666}
667
Ingo Molnar647e7ca2007-10-15 17:00:13 +0200668/*
Andrei Epure660cc002013-03-11 12:03:20 +0200669 * We calculate the vruntime slice of a to-be-inserted task.
Ingo Molnar647e7ca2007-10-15 17:00:13 +0200670 *
Peter Zijlstraf9c0b092008-10-17 19:27:04 +0200671 * vs = s/w
Ingo Molnar647e7ca2007-10-15 17:00:13 +0200672 */
Peter Zijlstraf9c0b092008-10-17 19:27:04 +0200673static u64 sched_vslice(struct cfs_rq *cfs_rq, struct sched_entity *se)
Ingo Molnar647e7ca2007-10-15 17:00:13 +0200674{
Peter Zijlstraf9c0b092008-10-17 19:27:04 +0200675 return calc_delta_fair(sched_slice(cfs_rq, se), se);
Peter Zijlstraa7be37a2008-06-27 13:41:11 +0200676}
677
Alex Shia75cdaa2013-06-20 10:18:47 +0800678#ifdef CONFIG_SMP
Morten Rasmussen772bd008c2016-06-22 18:03:13 +0100679static int select_idle_sibling(struct task_struct *p, int prev_cpu, int cpu);
Mel Gormanfb13c7e2013-10-07 11:29:17 +0100680static unsigned long task_h_load(struct task_struct *p);
681
Yuyang Du9d89c252015-07-15 08:04:37 +0800682/*
683 * We choose a half-life close to 1 scheduling period.
Leo Yan84fb5a12015-09-15 18:57:37 +0800684 * Note: The tables runnable_avg_yN_inv and runnable_avg_yN_sum are
685 * dependent on this value.
Yuyang Du9d89c252015-07-15 08:04:37 +0800686 */
687#define LOAD_AVG_PERIOD 32
688#define LOAD_AVG_MAX 47742 /* maximum possible load avg */
Leo Yan84fb5a12015-09-15 18:57:37 +0800689#define LOAD_AVG_MAX_N 345 /* number of full periods to produce LOAD_AVG_MAX */
Alex Shia75cdaa2013-06-20 10:18:47 +0800690
Yuyang Du540247f2015-07-15 08:04:39 +0800691/* Give new sched_entity start runnable values to heavy its load in infant time */
692void init_entity_runnable_average(struct sched_entity *se)
Alex Shia75cdaa2013-06-20 10:18:47 +0800693{
Yuyang Du540247f2015-07-15 08:04:39 +0800694 struct sched_avg *sa = &se->avg;
Alex Shia75cdaa2013-06-20 10:18:47 +0800695
Yuyang Du9d89c252015-07-15 08:04:37 +0800696 sa->last_update_time = 0;
697 /*
698 * sched_avg's period_contrib should be strictly less then 1024, so
699 * we give it 1023 to make sure it is almost a period (1024us), and
700 * will definitely be update (after enqueue).
701 */
702 sa->period_contrib = 1023;
Vincent Guittotb5a9b342016-10-19 14:45:23 +0200703 /*
704 * Tasks are intialized with full load to be seen as heavy tasks until
705 * they get a chance to stabilize to their real load level.
706 * Group entities are intialized with zero load to reflect the fact that
707 * nothing has been attached to the task group yet.
708 */
709 if (entity_is_task(se))
710 sa->load_avg = scale_load_down(se->load.weight);
Yuyang Du9d89c252015-07-15 08:04:37 +0800711 sa->load_sum = sa->load_avg * LOAD_AVG_MAX;
Yuyang Du2b8c41d2016-03-30 04:30:56 +0800712 /*
713 * At this point, util_avg won't be used in select_task_rq_fair anyway
714 */
715 sa->util_avg = 0;
716 sa->util_sum = 0;
Yuyang Du9d89c252015-07-15 08:04:37 +0800717 /* when this task enqueue'ed, it will contribute to its cfs_rq's load_avg */
Alex Shia75cdaa2013-06-20 10:18:47 +0800718}
Yuyang Du7ea241a2015-07-15 08:04:42 +0800719
Peter Zijlstra7dc603c2016-06-16 13:29:28 +0200720static inline u64 cfs_rq_clock_task(struct cfs_rq *cfs_rq);
721static int update_cfs_rq_load_avg(u64 now, struct cfs_rq *cfs_rq, bool update_freq);
Peter Zijlstra3d30544f2016-06-21 14:27:50 +0200722static void update_tg_load_avg(struct cfs_rq *cfs_rq, int force);
Peter Zijlstra7dc603c2016-06-16 13:29:28 +0200723static void attach_entity_load_avg(struct cfs_rq *cfs_rq, struct sched_entity *se);
724
Yuyang Du2b8c41d2016-03-30 04:30:56 +0800725/*
726 * With new tasks being created, their initial util_avgs are extrapolated
727 * based on the cfs_rq's current util_avg:
728 *
729 * util_avg = cfs_rq->util_avg / (cfs_rq->load_avg + 1) * se.load.weight
730 *
731 * However, in many cases, the above util_avg does not give a desired
732 * value. Moreover, the sum of the util_avgs may be divergent, such
733 * as when the series is a harmonic series.
734 *
735 * To solve this problem, we also cap the util_avg of successive tasks to
736 * only 1/2 of the left utilization budget:
737 *
738 * util_avg_cap = (1024 - cfs_rq->avg.util_avg) / 2^n
739 *
740 * where n denotes the nth task.
741 *
742 * For example, a simplest series from the beginning would be like:
743 *
744 * task util_avg: 512, 256, 128, 64, 32, 16, 8, ...
745 * cfs_rq util_avg: 512, 768, 896, 960, 992, 1008, 1016, ...
746 *
747 * Finally, that extrapolated util_avg is clamped to the cap (util_avg_cap)
748 * if util_avg > util_avg_cap.
749 */
750void post_init_entity_util_avg(struct sched_entity *se)
751{
752 struct cfs_rq *cfs_rq = cfs_rq_of(se);
753 struct sched_avg *sa = &se->avg;
Yuyang Du172895e2016-04-05 12:12:27 +0800754 long cap = (long)(SCHED_CAPACITY_SCALE - cfs_rq->avg.util_avg) / 2;
Peter Zijlstra7dc603c2016-06-16 13:29:28 +0200755 u64 now = cfs_rq_clock_task(cfs_rq);
Yuyang Du2b8c41d2016-03-30 04:30:56 +0800756
757 if (cap > 0) {
758 if (cfs_rq->avg.util_avg != 0) {
759 sa->util_avg = cfs_rq->avg.util_avg * se->load.weight;
760 sa->util_avg /= (cfs_rq->avg.load_avg + 1);
761
762 if (sa->util_avg > cap)
763 sa->util_avg = cap;
764 } else {
765 sa->util_avg = cap;
766 }
767 sa->util_sum = sa->util_avg * LOAD_AVG_MAX;
768 }
Peter Zijlstra7dc603c2016-06-16 13:29:28 +0200769
770 if (entity_is_task(se)) {
771 struct task_struct *p = task_of(se);
772 if (p->sched_class != &fair_sched_class) {
773 /*
774 * For !fair tasks do:
775 *
776 update_cfs_rq_load_avg(now, cfs_rq, false);
777 attach_entity_load_avg(cfs_rq, se);
778 switched_from_fair(rq, p);
779 *
780 * such that the next switched_to_fair() has the
781 * expected state.
782 */
783 se->avg.last_update_time = now;
784 return;
785 }
786 }
787
Peter Zijlstra7c3edd22016-07-13 10:56:25 +0200788 update_cfs_rq_load_avg(now, cfs_rq, false);
Peter Zijlstra7dc603c2016-06-16 13:29:28 +0200789 attach_entity_load_avg(cfs_rq, se);
Peter Zijlstra7c3edd22016-07-13 10:56:25 +0200790 update_tg_load_avg(cfs_rq, false);
Yuyang Du2b8c41d2016-03-30 04:30:56 +0800791}
792
Peter Zijlstra7dc603c2016-06-16 13:29:28 +0200793#else /* !CONFIG_SMP */
Yuyang Du540247f2015-07-15 08:04:39 +0800794void init_entity_runnable_average(struct sched_entity *se)
Alex Shia75cdaa2013-06-20 10:18:47 +0800795{
796}
Yuyang Du2b8c41d2016-03-30 04:30:56 +0800797void post_init_entity_util_avg(struct sched_entity *se)
798{
799}
Peter Zijlstra3d30544f2016-06-21 14:27:50 +0200800static void update_tg_load_avg(struct cfs_rq *cfs_rq, int force)
801{
802}
Peter Zijlstra7dc603c2016-06-16 13:29:28 +0200803#endif /* CONFIG_SMP */
Alex Shia75cdaa2013-06-20 10:18:47 +0800804
Peter Zijlstraa7be37a2008-06-27 13:41:11 +0200805/*
Peter Zijlstra9dbdb152013-11-18 18:27:06 +0100806 * Update the current task's runtime statistics.
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200807 */
Ingo Molnarb7cc0892007-08-09 11:16:47 +0200808static void update_curr(struct cfs_rq *cfs_rq)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200809{
Ingo Molnar429d43b2007-10-15 17:00:03 +0200810 struct sched_entity *curr = cfs_rq->curr;
Frederic Weisbecker78becc22013-04-12 01:51:02 +0200811 u64 now = rq_clock_task(rq_of(cfs_rq));
Peter Zijlstra9dbdb152013-11-18 18:27:06 +0100812 u64 delta_exec;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200813
814 if (unlikely(!curr))
815 return;
816
Peter Zijlstra9dbdb152013-11-18 18:27:06 +0100817 delta_exec = now - curr->exec_start;
818 if (unlikely((s64)delta_exec <= 0))
Peter Zijlstra34f28ec2008-12-16 08:45:31 +0100819 return;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200820
Ingo Molnar8ebc91d2007-10-15 17:00:03 +0200821 curr->exec_start = now;
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +0100822
Peter Zijlstra9dbdb152013-11-18 18:27:06 +0100823 schedstat_set(curr->statistics.exec_max,
824 max(delta_exec, curr->statistics.exec_max));
825
826 curr->sum_exec_runtime += delta_exec;
Josh Poimboeufae928822016-06-17 12:43:24 -0500827 schedstat_add(cfs_rq->exec_clock, delta_exec);
Peter Zijlstra9dbdb152013-11-18 18:27:06 +0100828
829 curr->vruntime += calc_delta_fair(delta_exec, curr);
830 update_min_vruntime(cfs_rq);
831
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +0100832 if (entity_is_task(curr)) {
833 struct task_struct *curtask = task_of(curr);
834
Ingo Molnarf977bb42009-09-13 18:15:54 +0200835 trace_sched_stat_runtime(curtask, delta_exec, curr->vruntime);
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +0100836 cpuacct_charge(curtask, delta_exec);
Frank Mayharf06febc2008-09-12 09:54:39 -0700837 account_group_exec_runtime(curtask, delta_exec);
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +0100838 }
Paul Turnerec12cb72011-07-21 09:43:30 -0700839
840 account_cfs_rq_runtime(cfs_rq, delta_exec);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200841}
842
Stanislaw Gruszka6e998912014-11-12 16:58:44 +0100843static void update_curr_fair(struct rq *rq)
844{
845 update_curr(cfs_rq_of(&rq->curr->se));
846}
847
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200848static inline void
Ingo Molnar5870db52007-08-09 11:16:47 +0200849update_stats_wait_start(struct cfs_rq *cfs_rq, struct sched_entity *se)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200850{
Josh Poimboeuf4fa8d2992016-06-17 12:43:26 -0500851 u64 wait_start, prev_wait_start;
852
853 if (!schedstat_enabled())
854 return;
855
856 wait_start = rq_clock(rq_of(cfs_rq));
857 prev_wait_start = schedstat_val(se->statistics.wait_start);
Joonwoo Park3ea94de2015-11-12 19:38:54 -0800858
859 if (entity_is_task(se) && task_on_rq_migrating(task_of(se)) &&
Josh Poimboeuf4fa8d2992016-06-17 12:43:26 -0500860 likely(wait_start > prev_wait_start))
861 wait_start -= prev_wait_start;
Joonwoo Park3ea94de2015-11-12 19:38:54 -0800862
Josh Poimboeuf4fa8d2992016-06-17 12:43:26 -0500863 schedstat_set(se->statistics.wait_start, wait_start);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200864}
865
Josh Poimboeuf4fa8d2992016-06-17 12:43:26 -0500866static inline void
Joonwoo Park3ea94de2015-11-12 19:38:54 -0800867update_stats_wait_end(struct cfs_rq *cfs_rq, struct sched_entity *se)
868{
869 struct task_struct *p;
Mel Gormancb251762016-02-05 09:08:36 +0000870 u64 delta;
871
Josh Poimboeuf4fa8d2992016-06-17 12:43:26 -0500872 if (!schedstat_enabled())
873 return;
874
875 delta = rq_clock(rq_of(cfs_rq)) - schedstat_val(se->statistics.wait_start);
Joonwoo Park3ea94de2015-11-12 19:38:54 -0800876
877 if (entity_is_task(se)) {
878 p = task_of(se);
879 if (task_on_rq_migrating(p)) {
880 /*
881 * Preserve migrating task's wait time so wait_start
882 * time stamp can be adjusted to accumulate wait time
883 * prior to migration.
884 */
Josh Poimboeuf4fa8d2992016-06-17 12:43:26 -0500885 schedstat_set(se->statistics.wait_start, delta);
Joonwoo Park3ea94de2015-11-12 19:38:54 -0800886 return;
887 }
888 trace_sched_stat_wait(p, delta);
889 }
890
Josh Poimboeuf4fa8d2992016-06-17 12:43:26 -0500891 schedstat_set(se->statistics.wait_max,
892 max(schedstat_val(se->statistics.wait_max), delta));
893 schedstat_inc(se->statistics.wait_count);
894 schedstat_add(se->statistics.wait_sum, delta);
895 schedstat_set(se->statistics.wait_start, 0);
Joonwoo Park3ea94de2015-11-12 19:38:54 -0800896}
Joonwoo Park3ea94de2015-11-12 19:38:54 -0800897
Josh Poimboeuf4fa8d2992016-06-17 12:43:26 -0500898static inline void
Josh Poimboeuf1a3d0272016-06-17 12:43:23 -0500899update_stats_enqueue_sleeper(struct cfs_rq *cfs_rq, struct sched_entity *se)
900{
901 struct task_struct *tsk = NULL;
Josh Poimboeuf4fa8d2992016-06-17 12:43:26 -0500902 u64 sleep_start, block_start;
903
904 if (!schedstat_enabled())
905 return;
906
907 sleep_start = schedstat_val(se->statistics.sleep_start);
908 block_start = schedstat_val(se->statistics.block_start);
Josh Poimboeuf1a3d0272016-06-17 12:43:23 -0500909
910 if (entity_is_task(se))
911 tsk = task_of(se);
912
Josh Poimboeuf4fa8d2992016-06-17 12:43:26 -0500913 if (sleep_start) {
914 u64 delta = rq_clock(rq_of(cfs_rq)) - sleep_start;
Josh Poimboeuf1a3d0272016-06-17 12:43:23 -0500915
916 if ((s64)delta < 0)
917 delta = 0;
918
Josh Poimboeuf4fa8d2992016-06-17 12:43:26 -0500919 if (unlikely(delta > schedstat_val(se->statistics.sleep_max)))
920 schedstat_set(se->statistics.sleep_max, delta);
Josh Poimboeuf1a3d0272016-06-17 12:43:23 -0500921
Josh Poimboeuf4fa8d2992016-06-17 12:43:26 -0500922 schedstat_set(se->statistics.sleep_start, 0);
923 schedstat_add(se->statistics.sum_sleep_runtime, delta);
Josh Poimboeuf1a3d0272016-06-17 12:43:23 -0500924
925 if (tsk) {
926 account_scheduler_latency(tsk, delta >> 10, 1);
927 trace_sched_stat_sleep(tsk, delta);
928 }
929 }
Josh Poimboeuf4fa8d2992016-06-17 12:43:26 -0500930 if (block_start) {
931 u64 delta = rq_clock(rq_of(cfs_rq)) - block_start;
Josh Poimboeuf1a3d0272016-06-17 12:43:23 -0500932
933 if ((s64)delta < 0)
934 delta = 0;
935
Josh Poimboeuf4fa8d2992016-06-17 12:43:26 -0500936 if (unlikely(delta > schedstat_val(se->statistics.block_max)))
937 schedstat_set(se->statistics.block_max, delta);
Josh Poimboeuf1a3d0272016-06-17 12:43:23 -0500938
Josh Poimboeuf4fa8d2992016-06-17 12:43:26 -0500939 schedstat_set(se->statistics.block_start, 0);
940 schedstat_add(se->statistics.sum_sleep_runtime, delta);
Josh Poimboeuf1a3d0272016-06-17 12:43:23 -0500941
942 if (tsk) {
943 if (tsk->in_iowait) {
Josh Poimboeuf4fa8d2992016-06-17 12:43:26 -0500944 schedstat_add(se->statistics.iowait_sum, delta);
945 schedstat_inc(se->statistics.iowait_count);
Josh Poimboeuf1a3d0272016-06-17 12:43:23 -0500946 trace_sched_stat_iowait(tsk, delta);
947 }
948
949 trace_sched_stat_blocked(tsk, delta);
Riley Andrews4c873b42015-10-02 00:39:53 -0700950 trace_sched_blocked_reason(tsk);
Josh Poimboeuf1a3d0272016-06-17 12:43:23 -0500951
952 /*
953 * Blocking time is in units of nanosecs, so shift by
954 * 20 to get a milliseconds-range estimation of the
955 * amount of time that the task spent sleeping:
956 */
957 if (unlikely(prof_on == SLEEP_PROFILING)) {
958 profile_hits(SLEEP_PROFILING,
959 (void *)get_wchan(tsk),
960 delta >> 20);
961 }
962 account_scheduler_latency(tsk, delta >> 10, 0);
963 }
964 }
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200965}
966
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200967/*
968 * Task is being enqueued - update stats:
969 */
Mel Gormancb251762016-02-05 09:08:36 +0000970static inline void
Josh Poimboeuf1a3d0272016-06-17 12:43:23 -0500971update_stats_enqueue(struct cfs_rq *cfs_rq, struct sched_entity *se, int flags)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200972{
Josh Poimboeuf4fa8d2992016-06-17 12:43:26 -0500973 if (!schedstat_enabled())
974 return;
975
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200976 /*
977 * Are we enqueueing a waiting task? (for current tasks
978 * a dequeue/enqueue event is a NOP)
979 */
Ingo Molnar429d43b2007-10-15 17:00:03 +0200980 if (se != cfs_rq->curr)
Ingo Molnar5870db52007-08-09 11:16:47 +0200981 update_stats_wait_start(cfs_rq, se);
Josh Poimboeuf1a3d0272016-06-17 12:43:23 -0500982
983 if (flags & ENQUEUE_WAKEUP)
984 update_stats_enqueue_sleeper(cfs_rq, se);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200985}
986
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200987static inline void
Mel Gormancb251762016-02-05 09:08:36 +0000988update_stats_dequeue(struct cfs_rq *cfs_rq, struct sched_entity *se, int flags)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200989{
Josh Poimboeuf4fa8d2992016-06-17 12:43:26 -0500990
991 if (!schedstat_enabled())
992 return;
993
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200994 /*
995 * Mark the end of the wait period if dequeueing a
996 * waiting task:
997 */
Ingo Molnar429d43b2007-10-15 17:00:03 +0200998 if (se != cfs_rq->curr)
Ingo Molnar9ef0a962007-08-09 11:16:47 +0200999 update_stats_wait_end(cfs_rq, se);
Mel Gormancb251762016-02-05 09:08:36 +00001000
Josh Poimboeuf4fa8d2992016-06-17 12:43:26 -05001001 if ((flags & DEQUEUE_SLEEP) && entity_is_task(se)) {
1002 struct task_struct *tsk = task_of(se);
Mel Gormancb251762016-02-05 09:08:36 +00001003
Josh Poimboeuf4fa8d2992016-06-17 12:43:26 -05001004 if (tsk->state & TASK_INTERRUPTIBLE)
1005 schedstat_set(se->statistics.sleep_start,
1006 rq_clock(rq_of(cfs_rq)));
1007 if (tsk->state & TASK_UNINTERRUPTIBLE)
1008 schedstat_set(se->statistics.block_start,
1009 rq_clock(rq_of(cfs_rq)));
Mel Gormancb251762016-02-05 09:08:36 +00001010 }
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001011}
1012
1013/*
1014 * We are picking a new current task - update its stats:
1015 */
1016static inline void
Ingo Molnar79303e92007-08-09 11:16:47 +02001017update_stats_curr_start(struct cfs_rq *cfs_rq, struct sched_entity *se)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001018{
1019 /*
1020 * We are starting a new run period:
1021 */
Frederic Weisbecker78becc22013-04-12 01:51:02 +02001022 se->exec_start = rq_clock_task(rq_of(cfs_rq));
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001023}
1024
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001025/**************************************************
1026 * Scheduling class queueing methods:
1027 */
1028
Peter Zijlstracbee9f82012-10-25 14:16:43 +02001029#ifdef CONFIG_NUMA_BALANCING
1030/*
Mel Gorman598f0ec2013-10-07 11:28:55 +01001031 * Approximate time to scan a full NUMA task in ms. The task scan period is
1032 * calculated based on the tasks virtual memory size and
1033 * numa_balancing_scan_size.
Peter Zijlstracbee9f82012-10-25 14:16:43 +02001034 */
Mel Gorman598f0ec2013-10-07 11:28:55 +01001035unsigned int sysctl_numa_balancing_scan_period_min = 1000;
1036unsigned int sysctl_numa_balancing_scan_period_max = 60000;
Peter Zijlstra6e5fb222012-10-25 14:16:45 +02001037
1038/* Portion of address space to scan in MB */
1039unsigned int sysctl_numa_balancing_scan_size = 256;
Peter Zijlstracbee9f82012-10-25 14:16:43 +02001040
Peter Zijlstra4b96a29b2012-10-25 14:16:47 +02001041/* Scan @scan_size MB every @scan_period after an initial @scan_delay in ms */
1042unsigned int sysctl_numa_balancing_scan_delay = 1000;
1043
Mel Gorman598f0ec2013-10-07 11:28:55 +01001044static unsigned int task_nr_scan_windows(struct task_struct *p)
1045{
1046 unsigned long rss = 0;
1047 unsigned long nr_scan_pages;
1048
1049 /*
1050 * Calculations based on RSS as non-present and empty pages are skipped
1051 * by the PTE scanner and NUMA hinting faults should be trapped based
1052 * on resident pages
1053 */
1054 nr_scan_pages = sysctl_numa_balancing_scan_size << (20 - PAGE_SHIFT);
1055 rss = get_mm_rss(p->mm);
1056 if (!rss)
1057 rss = nr_scan_pages;
1058
1059 rss = round_up(rss, nr_scan_pages);
1060 return rss / nr_scan_pages;
1061}
1062
1063/* For sanitys sake, never scan more PTEs than MAX_SCAN_WINDOW MB/sec. */
1064#define MAX_SCAN_WINDOW 2560
1065
1066static unsigned int task_scan_min(struct task_struct *p)
1067{
Jason Low316c1608d2015-04-28 13:00:20 -07001068 unsigned int scan_size = READ_ONCE(sysctl_numa_balancing_scan_size);
Mel Gorman598f0ec2013-10-07 11:28:55 +01001069 unsigned int scan, floor;
1070 unsigned int windows = 1;
1071
Kirill Tkhai64192652014-10-16 14:39:37 +04001072 if (scan_size < MAX_SCAN_WINDOW)
1073 windows = MAX_SCAN_WINDOW / scan_size;
Mel Gorman598f0ec2013-10-07 11:28:55 +01001074 floor = 1000 / windows;
1075
1076 scan = sysctl_numa_balancing_scan_period_min / task_nr_scan_windows(p);
1077 return max_t(unsigned int, floor, scan);
1078}
1079
1080static unsigned int task_scan_max(struct task_struct *p)
1081{
1082 unsigned int smin = task_scan_min(p);
1083 unsigned int smax;
1084
1085 /* Watch for min being lower than max due to floor calculations */
1086 smax = sysctl_numa_balancing_scan_period_max / task_nr_scan_windows(p);
1087 return max(smin, smax);
1088}
1089
Peter Zijlstra0ec8aa02013-10-07 11:29:33 +01001090static void account_numa_enqueue(struct rq *rq, struct task_struct *p)
1091{
1092 rq->nr_numa_running += (p->numa_preferred_nid != -1);
1093 rq->nr_preferred_running += (p->numa_preferred_nid == task_node(p));
1094}
1095
1096static void account_numa_dequeue(struct rq *rq, struct task_struct *p)
1097{
1098 rq->nr_numa_running -= (p->numa_preferred_nid != -1);
1099 rq->nr_preferred_running -= (p->numa_preferred_nid == task_node(p));
1100}
1101
Peter Zijlstra8c8a7432013-10-07 11:29:21 +01001102struct numa_group {
1103 atomic_t refcount;
1104
1105 spinlock_t lock; /* nr_tasks, tasks */
1106 int nr_tasks;
Mel Gormane29cf082013-10-07 11:29:22 +01001107 pid_t gid;
Rik van Riel4142c3e2016-01-25 17:07:39 -05001108 int active_nodes;
Peter Zijlstra8c8a7432013-10-07 11:29:21 +01001109
1110 struct rcu_head rcu;
Mel Gorman989348b2013-10-07 11:29:40 +01001111 unsigned long total_faults;
Rik van Riel4142c3e2016-01-25 17:07:39 -05001112 unsigned long max_faults_cpu;
Rik van Riel7e2703e2014-01-27 17:03:45 -05001113 /*
1114 * Faults_cpu is used to decide whether memory should move
1115 * towards the CPU. As a consequence, these stats are weighted
1116 * more by CPU use than by memory faults.
1117 */
Rik van Riel50ec8a42014-01-27 17:03:42 -05001118 unsigned long *faults_cpu;
Mel Gorman989348b2013-10-07 11:29:40 +01001119 unsigned long faults[0];
Peter Zijlstra8c8a7432013-10-07 11:29:21 +01001120};
1121
Rik van Rielbe1e4e72014-01-27 17:03:48 -05001122/* Shared or private faults. */
1123#define NR_NUMA_HINT_FAULT_TYPES 2
1124
1125/* Memory and CPU locality */
1126#define NR_NUMA_HINT_FAULT_STATS (NR_NUMA_HINT_FAULT_TYPES * 2)
1127
1128/* Averaged statistics, and temporary buffers. */
1129#define NR_NUMA_HINT_FAULT_BUCKETS (NR_NUMA_HINT_FAULT_STATS * 2)
1130
Mel Gormane29cf082013-10-07 11:29:22 +01001131pid_t task_numa_group_id(struct task_struct *p)
1132{
1133 return p->numa_group ? p->numa_group->gid : 0;
1134}
1135
Iulia Manda44dba3d2014-10-31 02:13:31 +02001136/*
1137 * The averaged statistics, shared & private, memory & cpu,
1138 * occupy the first half of the array. The second half of the
1139 * array is for current counters, which are averaged into the
1140 * first set by task_numa_placement.
1141 */
1142static inline int task_faults_idx(enum numa_faults_stats s, int nid, int priv)
Mel Gormanac8e8952013-10-07 11:29:03 +01001143{
Iulia Manda44dba3d2014-10-31 02:13:31 +02001144 return NR_NUMA_HINT_FAULT_TYPES * (s * nr_node_ids + nid) + priv;
Mel Gormanac8e8952013-10-07 11:29:03 +01001145}
1146
1147static inline unsigned long task_faults(struct task_struct *p, int nid)
1148{
Iulia Manda44dba3d2014-10-31 02:13:31 +02001149 if (!p->numa_faults)
Mel Gormanac8e8952013-10-07 11:29:03 +01001150 return 0;
1151
Iulia Manda44dba3d2014-10-31 02:13:31 +02001152 return p->numa_faults[task_faults_idx(NUMA_MEM, nid, 0)] +
1153 p->numa_faults[task_faults_idx(NUMA_MEM, nid, 1)];
Mel Gormanac8e8952013-10-07 11:29:03 +01001154}
1155
Mel Gorman83e1d2c2013-10-07 11:29:27 +01001156static inline unsigned long group_faults(struct task_struct *p, int nid)
1157{
1158 if (!p->numa_group)
1159 return 0;
1160
Iulia Manda44dba3d2014-10-31 02:13:31 +02001161 return p->numa_group->faults[task_faults_idx(NUMA_MEM, nid, 0)] +
1162 p->numa_group->faults[task_faults_idx(NUMA_MEM, nid, 1)];
Mel Gorman83e1d2c2013-10-07 11:29:27 +01001163}
1164
Rik van Riel20e07de2014-01-27 17:03:43 -05001165static inline unsigned long group_faults_cpu(struct numa_group *group, int nid)
1166{
Iulia Manda44dba3d2014-10-31 02:13:31 +02001167 return group->faults_cpu[task_faults_idx(NUMA_MEM, nid, 0)] +
1168 group->faults_cpu[task_faults_idx(NUMA_MEM, nid, 1)];
Rik van Riel20e07de2014-01-27 17:03:43 -05001169}
1170
Rik van Riel4142c3e2016-01-25 17:07:39 -05001171/*
1172 * A node triggering more than 1/3 as many NUMA faults as the maximum is
1173 * considered part of a numa group's pseudo-interleaving set. Migrations
1174 * between these nodes are slowed down, to allow things to settle down.
1175 */
1176#define ACTIVE_NODE_FRACTION 3
1177
1178static bool numa_is_active_node(int nid, struct numa_group *ng)
1179{
1180 return group_faults_cpu(ng, nid) * ACTIVE_NODE_FRACTION > ng->max_faults_cpu;
1181}
1182
Rik van Riel6c6b1192014-10-17 03:29:52 -04001183/* Handle placement on systems where not all nodes are directly connected. */
1184static unsigned long score_nearby_nodes(struct task_struct *p, int nid,
1185 int maxdist, bool task)
1186{
1187 unsigned long score = 0;
1188 int node;
1189
1190 /*
1191 * All nodes are directly connected, and the same distance
1192 * from each other. No need for fancy placement algorithms.
1193 */
1194 if (sched_numa_topology_type == NUMA_DIRECT)
1195 return 0;
1196
1197 /*
1198 * This code is called for each node, introducing N^2 complexity,
1199 * which should be ok given the number of nodes rarely exceeds 8.
1200 */
1201 for_each_online_node(node) {
1202 unsigned long faults;
1203 int dist = node_distance(nid, node);
1204
1205 /*
1206 * The furthest away nodes in the system are not interesting
1207 * for placement; nid was already counted.
1208 */
1209 if (dist == sched_max_numa_distance || node == nid)
1210 continue;
1211
1212 /*
1213 * On systems with a backplane NUMA topology, compare groups
1214 * of nodes, and move tasks towards the group with the most
1215 * memory accesses. When comparing two nodes at distance
1216 * "hoplimit", only nodes closer by than "hoplimit" are part
1217 * of each group. Skip other nodes.
1218 */
1219 if (sched_numa_topology_type == NUMA_BACKPLANE &&
1220 dist > maxdist)
1221 continue;
1222
1223 /* Add up the faults from nearby nodes. */
1224 if (task)
1225 faults = task_faults(p, node);
1226 else
1227 faults = group_faults(p, node);
1228
1229 /*
1230 * On systems with a glueless mesh NUMA topology, there are
1231 * no fixed "groups of nodes". Instead, nodes that are not
1232 * directly connected bounce traffic through intermediate
1233 * nodes; a numa_group can occupy any set of nodes.
1234 * The further away a node is, the less the faults count.
1235 * This seems to result in good task placement.
1236 */
1237 if (sched_numa_topology_type == NUMA_GLUELESS_MESH) {
1238 faults *= (sched_max_numa_distance - dist);
1239 faults /= (sched_max_numa_distance - LOCAL_DISTANCE);
1240 }
1241
1242 score += faults;
1243 }
1244
1245 return score;
1246}
1247
Mel Gorman83e1d2c2013-10-07 11:29:27 +01001248/*
1249 * These return the fraction of accesses done by a particular task, or
1250 * task group, on a particular numa node. The group weight is given a
1251 * larger multiplier, in order to group tasks together that are almost
1252 * evenly spread out between numa nodes.
1253 */
Rik van Riel7bd95322014-10-17 03:29:51 -04001254static inline unsigned long task_weight(struct task_struct *p, int nid,
1255 int dist)
Mel Gorman83e1d2c2013-10-07 11:29:27 +01001256{
Rik van Riel7bd95322014-10-17 03:29:51 -04001257 unsigned long faults, total_faults;
Mel Gorman83e1d2c2013-10-07 11:29:27 +01001258
Iulia Manda44dba3d2014-10-31 02:13:31 +02001259 if (!p->numa_faults)
Mel Gorman83e1d2c2013-10-07 11:29:27 +01001260 return 0;
1261
1262 total_faults = p->total_numa_faults;
1263
1264 if (!total_faults)
1265 return 0;
1266
Rik van Riel7bd95322014-10-17 03:29:51 -04001267 faults = task_faults(p, nid);
Rik van Riel6c6b1192014-10-17 03:29:52 -04001268 faults += score_nearby_nodes(p, nid, dist, true);
1269
Rik van Riel7bd95322014-10-17 03:29:51 -04001270 return 1000 * faults / total_faults;
Mel Gorman83e1d2c2013-10-07 11:29:27 +01001271}
1272
Rik van Riel7bd95322014-10-17 03:29:51 -04001273static inline unsigned long group_weight(struct task_struct *p, int nid,
1274 int dist)
Mel Gorman83e1d2c2013-10-07 11:29:27 +01001275{
Rik van Riel7bd95322014-10-17 03:29:51 -04001276 unsigned long faults, total_faults;
1277
1278 if (!p->numa_group)
Mel Gorman83e1d2c2013-10-07 11:29:27 +01001279 return 0;
1280
Rik van Riel7bd95322014-10-17 03:29:51 -04001281 total_faults = p->numa_group->total_faults;
1282
1283 if (!total_faults)
1284 return 0;
1285
1286 faults = group_faults(p, nid);
Rik van Riel6c6b1192014-10-17 03:29:52 -04001287 faults += score_nearby_nodes(p, nid, dist, false);
1288
Rik van Riel7bd95322014-10-17 03:29:51 -04001289 return 1000 * faults / total_faults;
Mel Gorman83e1d2c2013-10-07 11:29:27 +01001290}
1291
Rik van Riel10f39042014-01-27 17:03:44 -05001292bool should_numa_migrate_memory(struct task_struct *p, struct page * page,
1293 int src_nid, int dst_cpu)
1294{
1295 struct numa_group *ng = p->numa_group;
1296 int dst_nid = cpu_to_node(dst_cpu);
1297 int last_cpupid, this_cpupid;
1298
1299 this_cpupid = cpu_pid_to_cpupid(dst_cpu, current->pid);
1300
1301 /*
1302 * Multi-stage node selection is used in conjunction with a periodic
1303 * migration fault to build a temporal task<->page relation. By using
1304 * a two-stage filter we remove short/unlikely relations.
1305 *
1306 * Using P(p) ~ n_p / n_t as per frequentist probability, we can equate
1307 * a task's usage of a particular page (n_p) per total usage of this
1308 * page (n_t) (in a given time-span) to a probability.
1309 *
1310 * Our periodic faults will sample this probability and getting the
1311 * same result twice in a row, given these samples are fully
1312 * independent, is then given by P(n)^2, provided our sample period
1313 * is sufficiently short compared to the usage pattern.
1314 *
1315 * This quadric squishes small probabilities, making it less likely we
1316 * act on an unlikely task<->page relation.
1317 */
1318 last_cpupid = page_cpupid_xchg_last(page, this_cpupid);
1319 if (!cpupid_pid_unset(last_cpupid) &&
1320 cpupid_to_nid(last_cpupid) != dst_nid)
1321 return false;
1322
1323 /* Always allow migrate on private faults */
1324 if (cpupid_match_pid(p, last_cpupid))
1325 return true;
1326
1327 /* A shared fault, but p->numa_group has not been set up yet. */
1328 if (!ng)
1329 return true;
1330
1331 /*
Rik van Riel4142c3e2016-01-25 17:07:39 -05001332 * Destination node is much more heavily used than the source
1333 * node? Allow migration.
Rik van Riel10f39042014-01-27 17:03:44 -05001334 */
Rik van Riel4142c3e2016-01-25 17:07:39 -05001335 if (group_faults_cpu(ng, dst_nid) > group_faults_cpu(ng, src_nid) *
1336 ACTIVE_NODE_FRACTION)
Rik van Riel10f39042014-01-27 17:03:44 -05001337 return true;
1338
1339 /*
Rik van Riel4142c3e2016-01-25 17:07:39 -05001340 * Distribute memory according to CPU & memory use on each node,
1341 * with 3/4 hysteresis to avoid unnecessary memory migrations:
1342 *
1343 * faults_cpu(dst) 3 faults_cpu(src)
1344 * --------------- * - > ---------------
1345 * faults_mem(dst) 4 faults_mem(src)
Rik van Riel10f39042014-01-27 17:03:44 -05001346 */
Rik van Riel4142c3e2016-01-25 17:07:39 -05001347 return group_faults_cpu(ng, dst_nid) * group_faults(p, src_nid) * 3 >
1348 group_faults_cpu(ng, src_nid) * group_faults(p, dst_nid) * 4;
Rik van Riel10f39042014-01-27 17:03:44 -05001349}
1350
Mel Gormane6628d52013-10-07 11:29:02 +01001351static unsigned long weighted_cpuload(const int cpu);
Mel Gorman58d081b2013-10-07 11:29:10 +01001352static unsigned long source_load(int cpu, int type);
1353static unsigned long target_load(int cpu, int type);
Nicolas Pitreced549f2014-05-26 18:19:38 -04001354static unsigned long capacity_of(int cpu);
Mel Gorman58d081b2013-10-07 11:29:10 +01001355static long effective_load(struct task_group *tg, int cpu, long wl, long wg);
Mel Gormane6628d52013-10-07 11:29:02 +01001356
Mel Gormanfb13c7e2013-10-07 11:29:17 +01001357/* Cached statistics for all CPUs within a node */
Mel Gorman58d081b2013-10-07 11:29:10 +01001358struct numa_stats {
Mel Gormanfb13c7e2013-10-07 11:29:17 +01001359 unsigned long nr_running;
Mel Gorman58d081b2013-10-07 11:29:10 +01001360 unsigned long load;
Mel Gormanfb13c7e2013-10-07 11:29:17 +01001361
1362 /* Total compute capacity of CPUs on a node */
Nicolas Pitre5ef20ca2014-05-26 18:19:34 -04001363 unsigned long compute_capacity;
Mel Gormanfb13c7e2013-10-07 11:29:17 +01001364
1365 /* Approximate capacity in terms of runnable tasks on a node */
Nicolas Pitre5ef20ca2014-05-26 18:19:34 -04001366 unsigned long task_capacity;
Nicolas Pitre1b6a7492014-05-26 18:19:35 -04001367 int has_free_capacity;
Mel Gorman58d081b2013-10-07 11:29:10 +01001368};
Mel Gormane6628d52013-10-07 11:29:02 +01001369
Mel Gormanfb13c7e2013-10-07 11:29:17 +01001370/*
1371 * XXX borrowed from update_sg_lb_stats
1372 */
1373static void update_numa_stats(struct numa_stats *ns, int nid)
1374{
Rik van Riel83d7f242014-08-04 13:23:28 -04001375 int smt, cpu, cpus = 0;
1376 unsigned long capacity;
Mel Gormanfb13c7e2013-10-07 11:29:17 +01001377
1378 memset(ns, 0, sizeof(*ns));
1379 for_each_cpu(cpu, cpumask_of_node(nid)) {
1380 struct rq *rq = cpu_rq(cpu);
1381
1382 ns->nr_running += rq->nr_running;
1383 ns->load += weighted_cpuload(cpu);
Nicolas Pitreced549f2014-05-26 18:19:38 -04001384 ns->compute_capacity += capacity_of(cpu);
Peter Zijlstra5eca82a2013-11-06 18:47:57 +01001385
1386 cpus++;
Mel Gormanfb13c7e2013-10-07 11:29:17 +01001387 }
1388
Peter Zijlstra5eca82a2013-11-06 18:47:57 +01001389 /*
1390 * If we raced with hotplug and there are no CPUs left in our mask
1391 * the @ns structure is NULL'ed and task_numa_compare() will
1392 * not find this node attractive.
1393 *
Nicolas Pitre1b6a7492014-05-26 18:19:35 -04001394 * We'll either bail at !has_free_capacity, or we'll detect a huge
1395 * imbalance and bail there.
Peter Zijlstra5eca82a2013-11-06 18:47:57 +01001396 */
1397 if (!cpus)
1398 return;
1399
Rik van Riel83d7f242014-08-04 13:23:28 -04001400 /* smt := ceil(cpus / capacity), assumes: 1 < smt_power < 2 */
1401 smt = DIV_ROUND_UP(SCHED_CAPACITY_SCALE * cpus, ns->compute_capacity);
1402 capacity = cpus / smt; /* cores */
1403
1404 ns->task_capacity = min_t(unsigned, capacity,
1405 DIV_ROUND_CLOSEST(ns->compute_capacity, SCHED_CAPACITY_SCALE));
Nicolas Pitre1b6a7492014-05-26 18:19:35 -04001406 ns->has_free_capacity = (ns->nr_running < ns->task_capacity);
Mel Gormanfb13c7e2013-10-07 11:29:17 +01001407}
1408
Mel Gorman58d081b2013-10-07 11:29:10 +01001409struct task_numa_env {
1410 struct task_struct *p;
1411
1412 int src_cpu, src_nid;
1413 int dst_cpu, dst_nid;
1414
1415 struct numa_stats src_stats, dst_stats;
1416
Wanpeng Li40ea2b42013-12-05 19:10:17 +08001417 int imbalance_pct;
Rik van Riel7bd95322014-10-17 03:29:51 -04001418 int dist;
Mel Gormanfb13c7e2013-10-07 11:29:17 +01001419
1420 struct task_struct *best_task;
1421 long best_imp;
Mel Gorman58d081b2013-10-07 11:29:10 +01001422 int best_cpu;
1423};
1424
Mel Gormanfb13c7e2013-10-07 11:29:17 +01001425static void task_numa_assign(struct task_numa_env *env,
1426 struct task_struct *p, long imp)
1427{
1428 if (env->best_task)
1429 put_task_struct(env->best_task);
Oleg Nesterovbac78572016-05-18 21:57:33 +02001430 if (p)
1431 get_task_struct(p);
Mel Gormanfb13c7e2013-10-07 11:29:17 +01001432
1433 env->best_task = p;
1434 env->best_imp = imp;
1435 env->best_cpu = env->dst_cpu;
1436}
1437
Rik van Riel28a21742014-06-23 11:46:13 -04001438static bool load_too_imbalanced(long src_load, long dst_load,
Rik van Riele63da032014-05-14 13:22:21 -04001439 struct task_numa_env *env)
1440{
Rik van Riele4991b22015-05-27 15:04:27 -04001441 long imb, old_imb;
1442 long orig_src_load, orig_dst_load;
Rik van Riel28a21742014-06-23 11:46:13 -04001443 long src_capacity, dst_capacity;
1444
1445 /*
1446 * The load is corrected for the CPU capacity available on each node.
1447 *
1448 * src_load dst_load
1449 * ------------ vs ---------
1450 * src_capacity dst_capacity
1451 */
1452 src_capacity = env->src_stats.compute_capacity;
1453 dst_capacity = env->dst_stats.compute_capacity;
Rik van Riele63da032014-05-14 13:22:21 -04001454
1455 /* We care about the slope of the imbalance, not the direction. */
Rik van Riele4991b22015-05-27 15:04:27 -04001456 if (dst_load < src_load)
1457 swap(dst_load, src_load);
Rik van Riele63da032014-05-14 13:22:21 -04001458
1459 /* Is the difference below the threshold? */
Rik van Riele4991b22015-05-27 15:04:27 -04001460 imb = dst_load * src_capacity * 100 -
1461 src_load * dst_capacity * env->imbalance_pct;
Rik van Riele63da032014-05-14 13:22:21 -04001462 if (imb <= 0)
1463 return false;
1464
1465 /*
1466 * The imbalance is above the allowed threshold.
Rik van Riele4991b22015-05-27 15:04:27 -04001467 * Compare it with the old imbalance.
Rik van Riele63da032014-05-14 13:22:21 -04001468 */
Rik van Riel28a21742014-06-23 11:46:13 -04001469 orig_src_load = env->src_stats.load;
Rik van Riele4991b22015-05-27 15:04:27 -04001470 orig_dst_load = env->dst_stats.load;
Rik van Riel28a21742014-06-23 11:46:13 -04001471
Rik van Riele4991b22015-05-27 15:04:27 -04001472 if (orig_dst_load < orig_src_load)
1473 swap(orig_dst_load, orig_src_load);
Rik van Riele63da032014-05-14 13:22:21 -04001474
Rik van Riele4991b22015-05-27 15:04:27 -04001475 old_imb = orig_dst_load * src_capacity * 100 -
1476 orig_src_load * dst_capacity * env->imbalance_pct;
1477
1478 /* Would this change make things worse? */
1479 return (imb > old_imb);
Rik van Riele63da032014-05-14 13:22:21 -04001480}
1481
Mel Gormanfb13c7e2013-10-07 11:29:17 +01001482/*
1483 * This checks if the overall compute and NUMA accesses of the system would
1484 * be improved if the source tasks was migrated to the target dst_cpu taking
1485 * into account that it might be best if task running on the dst_cpu should
1486 * be exchanged with the source task
1487 */
Rik van Riel887c2902013-10-07 11:29:31 +01001488static void task_numa_compare(struct task_numa_env *env,
1489 long taskimp, long groupimp)
Mel Gormanfb13c7e2013-10-07 11:29:17 +01001490{
1491 struct rq *src_rq = cpu_rq(env->src_cpu);
1492 struct rq *dst_rq = cpu_rq(env->dst_cpu);
1493 struct task_struct *cur;
Rik van Riel28a21742014-06-23 11:46:13 -04001494 long src_load, dst_load;
Mel Gormanfb13c7e2013-10-07 11:29:17 +01001495 long load;
Rik van Riel1c5d3eb2014-06-23 11:46:15 -04001496 long imp = env->p->numa_group ? groupimp : taskimp;
Rik van Riel0132c3e2014-06-23 11:46:16 -04001497 long moveimp = imp;
Rik van Riel7bd95322014-10-17 03:29:51 -04001498 int dist = env->dist;
Mel Gormanfb13c7e2013-10-07 11:29:17 +01001499
1500 rcu_read_lock();
Oleg Nesterovbac78572016-05-18 21:57:33 +02001501 cur = task_rcu_dereference(&dst_rq->curr);
1502 if (cur && ((cur->flags & PF_EXITING) || is_idle_task(cur)))
Mel Gormanfb13c7e2013-10-07 11:29:17 +01001503 cur = NULL;
1504
1505 /*
Peter Zijlstra7af68332014-11-10 10:54:35 +01001506 * Because we have preemption enabled we can get migrated around and
1507 * end try selecting ourselves (current == env->p) as a swap candidate.
1508 */
1509 if (cur == env->p)
1510 goto unlock;
1511
1512 /*
Mel Gormanfb13c7e2013-10-07 11:29:17 +01001513 * "imp" is the fault differential for the source task between the
1514 * source and destination node. Calculate the total differential for
1515 * the source task and potential destination task. The more negative
1516 * the value is, the more rmeote accesses that would be expected to
1517 * be incurred if the tasks were swapped.
1518 */
1519 if (cur) {
1520 /* Skip this swap candidate if cannot move to the source cpu */
1521 if (!cpumask_test_cpu(env->src_cpu, tsk_cpus_allowed(cur)))
1522 goto unlock;
1523
Rik van Riel887c2902013-10-07 11:29:31 +01001524 /*
1525 * If dst and source tasks are in the same NUMA group, or not
Rik van Rielca28aa532013-10-07 11:29:32 +01001526 * in any group then look only at task weights.
Rik van Riel887c2902013-10-07 11:29:31 +01001527 */
Rik van Rielca28aa532013-10-07 11:29:32 +01001528 if (cur->numa_group == env->p->numa_group) {
Rik van Riel7bd95322014-10-17 03:29:51 -04001529 imp = taskimp + task_weight(cur, env->src_nid, dist) -
1530 task_weight(cur, env->dst_nid, dist);
Rik van Rielca28aa532013-10-07 11:29:32 +01001531 /*
1532 * Add some hysteresis to prevent swapping the
1533 * tasks within a group over tiny differences.
1534 */
1535 if (cur->numa_group)
1536 imp -= imp/16;
Rik van Riel887c2902013-10-07 11:29:31 +01001537 } else {
Rik van Rielca28aa532013-10-07 11:29:32 +01001538 /*
1539 * Compare the group weights. If a task is all by
1540 * itself (not part of a group), use the task weight
1541 * instead.
1542 */
Rik van Rielca28aa532013-10-07 11:29:32 +01001543 if (cur->numa_group)
Rik van Riel7bd95322014-10-17 03:29:51 -04001544 imp += group_weight(cur, env->src_nid, dist) -
1545 group_weight(cur, env->dst_nid, dist);
Rik van Rielca28aa532013-10-07 11:29:32 +01001546 else
Rik van Riel7bd95322014-10-17 03:29:51 -04001547 imp += task_weight(cur, env->src_nid, dist) -
1548 task_weight(cur, env->dst_nid, dist);
Rik van Riel887c2902013-10-07 11:29:31 +01001549 }
Mel Gormanfb13c7e2013-10-07 11:29:17 +01001550 }
1551
Rik van Riel0132c3e2014-06-23 11:46:16 -04001552 if (imp <= env->best_imp && moveimp <= env->best_imp)
Mel Gormanfb13c7e2013-10-07 11:29:17 +01001553 goto unlock;
1554
1555 if (!cur) {
1556 /* Is there capacity at our destination? */
Rik van Rielb932c032014-08-04 13:23:27 -04001557 if (env->src_stats.nr_running <= env->src_stats.task_capacity &&
Nicolas Pitre1b6a7492014-05-26 18:19:35 -04001558 !env->dst_stats.has_free_capacity)
Mel Gormanfb13c7e2013-10-07 11:29:17 +01001559 goto unlock;
1560
1561 goto balance;
1562 }
1563
1564 /* Balance doesn't matter much if we're running a task per cpu */
Rik van Riel0132c3e2014-06-23 11:46:16 -04001565 if (imp > env->best_imp && src_rq->nr_running == 1 &&
1566 dst_rq->nr_running == 1)
Mel Gormanfb13c7e2013-10-07 11:29:17 +01001567 goto assign;
1568
1569 /*
1570 * In the overloaded case, try and keep the load balanced.
1571 */
1572balance:
Peter Zijlstrae720fff2014-07-11 16:01:53 +02001573 load = task_h_load(env->p);
1574 dst_load = env->dst_stats.load + load;
1575 src_load = env->src_stats.load - load;
Mel Gormanfb13c7e2013-10-07 11:29:17 +01001576
Rik van Riel0132c3e2014-06-23 11:46:16 -04001577 if (moveimp > imp && moveimp > env->best_imp) {
1578 /*
1579 * If the improvement from just moving env->p direction is
1580 * better than swapping tasks around, check if a move is
1581 * possible. Store a slightly smaller score than moveimp,
1582 * so an actually idle CPU will win.
1583 */
1584 if (!load_too_imbalanced(src_load, dst_load, env)) {
1585 imp = moveimp - 1;
1586 cur = NULL;
1587 goto assign;
1588 }
1589 }
1590
1591 if (imp <= env->best_imp)
1592 goto unlock;
1593
Mel Gormanfb13c7e2013-10-07 11:29:17 +01001594 if (cur) {
Peter Zijlstrae720fff2014-07-11 16:01:53 +02001595 load = task_h_load(cur);
1596 dst_load -= load;
1597 src_load += load;
Mel Gormanfb13c7e2013-10-07 11:29:17 +01001598 }
1599
Rik van Riel28a21742014-06-23 11:46:13 -04001600 if (load_too_imbalanced(src_load, dst_load, env))
Mel Gormanfb13c7e2013-10-07 11:29:17 +01001601 goto unlock;
1602
Rik van Rielba7e5a22014-09-04 16:35:30 -04001603 /*
1604 * One idle CPU per node is evaluated for a task numa move.
1605 * Call select_idle_sibling to maybe find a better one.
1606 */
Peter Zijlstra10e2f1a2016-05-09 10:38:05 +02001607 if (!cur) {
1608 /*
1609 * select_idle_siblings() uses an per-cpu cpumask that
1610 * can be used from IRQ context.
1611 */
1612 local_irq_disable();
Morten Rasmussen772bd008c2016-06-22 18:03:13 +01001613 env->dst_cpu = select_idle_sibling(env->p, env->src_cpu,
1614 env->dst_cpu);
Peter Zijlstra10e2f1a2016-05-09 10:38:05 +02001615 local_irq_enable();
1616 }
Rik van Rielba7e5a22014-09-04 16:35:30 -04001617
Mel Gormanfb13c7e2013-10-07 11:29:17 +01001618assign:
1619 task_numa_assign(env, cur, imp);
1620unlock:
1621 rcu_read_unlock();
1622}
1623
Rik van Riel887c2902013-10-07 11:29:31 +01001624static void task_numa_find_cpu(struct task_numa_env *env,
1625 long taskimp, long groupimp)
Mel Gorman2c8a50a2013-10-07 11:29:18 +01001626{
1627 int cpu;
1628
1629 for_each_cpu(cpu, cpumask_of_node(env->dst_nid)) {
1630 /* Skip this CPU if the source task cannot migrate */
1631 if (!cpumask_test_cpu(cpu, tsk_cpus_allowed(env->p)))
1632 continue;
1633
1634 env->dst_cpu = cpu;
Rik van Riel887c2902013-10-07 11:29:31 +01001635 task_numa_compare(env, taskimp, groupimp);
Mel Gorman2c8a50a2013-10-07 11:29:18 +01001636 }
1637}
1638
Rik van Riel6f9aad02015-05-28 09:52:49 -04001639/* Only move tasks to a NUMA node less busy than the current node. */
1640static bool numa_has_capacity(struct task_numa_env *env)
1641{
1642 struct numa_stats *src = &env->src_stats;
1643 struct numa_stats *dst = &env->dst_stats;
1644
1645 if (src->has_free_capacity && !dst->has_free_capacity)
1646 return false;
1647
1648 /*
1649 * Only consider a task move if the source has a higher load
1650 * than the destination, corrected for CPU capacity on each node.
1651 *
1652 * src->load dst->load
1653 * --------------------- vs ---------------------
1654 * src->compute_capacity dst->compute_capacity
1655 */
Srikar Dronamraju44dcb042015-06-16 17:26:00 +05301656 if (src->load * dst->compute_capacity * env->imbalance_pct >
1657
1658 dst->load * src->compute_capacity * 100)
Rik van Riel6f9aad02015-05-28 09:52:49 -04001659 return true;
1660
1661 return false;
1662}
1663
Mel Gorman58d081b2013-10-07 11:29:10 +01001664static int task_numa_migrate(struct task_struct *p)
Mel Gormane6628d52013-10-07 11:29:02 +01001665{
Mel Gorman58d081b2013-10-07 11:29:10 +01001666 struct task_numa_env env = {
1667 .p = p,
Mel Gormanfb13c7e2013-10-07 11:29:17 +01001668
Mel Gorman58d081b2013-10-07 11:29:10 +01001669 .src_cpu = task_cpu(p),
Ingo Molnarb32e86b2013-10-07 11:29:30 +01001670 .src_nid = task_node(p),
Mel Gormanfb13c7e2013-10-07 11:29:17 +01001671
1672 .imbalance_pct = 112,
1673
1674 .best_task = NULL,
1675 .best_imp = 0,
Rik van Riel4142c3e2016-01-25 17:07:39 -05001676 .best_cpu = -1,
Mel Gorman58d081b2013-10-07 11:29:10 +01001677 };
1678 struct sched_domain *sd;
Rik van Riel887c2902013-10-07 11:29:31 +01001679 unsigned long taskweight, groupweight;
Rik van Riel7bd95322014-10-17 03:29:51 -04001680 int nid, ret, dist;
Rik van Riel887c2902013-10-07 11:29:31 +01001681 long taskimp, groupimp;
Mel Gormane6628d52013-10-07 11:29:02 +01001682
Mel Gorman58d081b2013-10-07 11:29:10 +01001683 /*
Mel Gormanfb13c7e2013-10-07 11:29:17 +01001684 * Pick the lowest SD_NUMA domain, as that would have the smallest
1685 * imbalance and would be the first to start moving tasks about.
1686 *
1687 * And we want to avoid any moving of tasks about, as that would create
1688 * random movement of tasks -- counter the numa conditions we're trying
1689 * to satisfy here.
Mel Gorman58d081b2013-10-07 11:29:10 +01001690 */
Mel Gormane6628d52013-10-07 11:29:02 +01001691 rcu_read_lock();
Mel Gormanfb13c7e2013-10-07 11:29:17 +01001692 sd = rcu_dereference(per_cpu(sd_numa, env.src_cpu));
Rik van Riel46a73e82013-11-11 19:29:25 -05001693 if (sd)
1694 env.imbalance_pct = 100 + (sd->imbalance_pct - 100) / 2;
Mel Gormane6628d52013-10-07 11:29:02 +01001695 rcu_read_unlock();
1696
Rik van Riel46a73e82013-11-11 19:29:25 -05001697 /*
1698 * Cpusets can break the scheduler domain tree into smaller
1699 * balance domains, some of which do not cross NUMA boundaries.
1700 * Tasks that are "trapped" in such domains cannot be migrated
1701 * elsewhere, so there is no point in (re)trying.
1702 */
1703 if (unlikely(!sd)) {
Wanpeng Lide1b3012013-12-12 15:23:24 +08001704 p->numa_preferred_nid = task_node(p);
Rik van Riel46a73e82013-11-11 19:29:25 -05001705 return -EINVAL;
1706 }
1707
Mel Gorman2c8a50a2013-10-07 11:29:18 +01001708 env.dst_nid = p->numa_preferred_nid;
Rik van Riel7bd95322014-10-17 03:29:51 -04001709 dist = env.dist = node_distance(env.src_nid, env.dst_nid);
1710 taskweight = task_weight(p, env.src_nid, dist);
1711 groupweight = group_weight(p, env.src_nid, dist);
1712 update_numa_stats(&env.src_stats, env.src_nid);
1713 taskimp = task_weight(p, env.dst_nid, dist) - taskweight;
1714 groupimp = group_weight(p, env.dst_nid, dist) - groupweight;
Mel Gorman2c8a50a2013-10-07 11:29:18 +01001715 update_numa_stats(&env.dst_stats, env.dst_nid);
Mel Gorman58d081b2013-10-07 11:29:10 +01001716
Rik van Riela43455a2014-06-04 16:09:42 -04001717 /* Try to find a spot on the preferred nid. */
Rik van Riel6f9aad02015-05-28 09:52:49 -04001718 if (numa_has_capacity(&env))
1719 task_numa_find_cpu(&env, taskimp, groupimp);
Rik van Riele1dda8a2013-10-07 11:29:19 +01001720
Rik van Riel9de05d42014-10-09 17:27:47 -04001721 /*
1722 * Look at other nodes in these cases:
1723 * - there is no space available on the preferred_nid
1724 * - the task is part of a numa_group that is interleaved across
1725 * multiple NUMA nodes; in order to better consolidate the group,
1726 * we need to check other locations.
1727 */
Rik van Riel4142c3e2016-01-25 17:07:39 -05001728 if (env.best_cpu == -1 || (p->numa_group && p->numa_group->active_nodes > 1)) {
Mel Gorman2c8a50a2013-10-07 11:29:18 +01001729 for_each_online_node(nid) {
1730 if (nid == env.src_nid || nid == p->numa_preferred_nid)
Mel Gormanfb13c7e2013-10-07 11:29:17 +01001731 continue;
1732
Rik van Riel7bd95322014-10-17 03:29:51 -04001733 dist = node_distance(env.src_nid, env.dst_nid);
Rik van Riel6c6b1192014-10-17 03:29:52 -04001734 if (sched_numa_topology_type == NUMA_BACKPLANE &&
1735 dist != env.dist) {
1736 taskweight = task_weight(p, env.src_nid, dist);
1737 groupweight = group_weight(p, env.src_nid, dist);
1738 }
Rik van Riel7bd95322014-10-17 03:29:51 -04001739
Mel Gorman83e1d2c2013-10-07 11:29:27 +01001740 /* Only consider nodes where both task and groups benefit */
Rik van Riel7bd95322014-10-17 03:29:51 -04001741 taskimp = task_weight(p, nid, dist) - taskweight;
1742 groupimp = group_weight(p, nid, dist) - groupweight;
Rik van Riel887c2902013-10-07 11:29:31 +01001743 if (taskimp < 0 && groupimp < 0)
Mel Gorman2c8a50a2013-10-07 11:29:18 +01001744 continue;
1745
Rik van Riel7bd95322014-10-17 03:29:51 -04001746 env.dist = dist;
Mel Gorman2c8a50a2013-10-07 11:29:18 +01001747 env.dst_nid = nid;
1748 update_numa_stats(&env.dst_stats, env.dst_nid);
Rik van Riel6f9aad02015-05-28 09:52:49 -04001749 if (numa_has_capacity(&env))
1750 task_numa_find_cpu(&env, taskimp, groupimp);
Mel Gorman58d081b2013-10-07 11:29:10 +01001751 }
1752 }
1753
Rik van Riel68d1b022014-04-11 13:00:29 -04001754 /*
1755 * If the task is part of a workload that spans multiple NUMA nodes,
1756 * and is migrating into one of the workload's active nodes, remember
1757 * this node as the task's preferred numa node, so the workload can
1758 * settle down.
1759 * A task that migrated to a second choice node will be better off
1760 * trying for a better one later. Do not set the preferred node here.
1761 */
Rik van Rieldb015da2014-06-23 11:41:34 -04001762 if (p->numa_group) {
Rik van Riel4142c3e2016-01-25 17:07:39 -05001763 struct numa_group *ng = p->numa_group;
1764
Rik van Rieldb015da2014-06-23 11:41:34 -04001765 if (env.best_cpu == -1)
1766 nid = env.src_nid;
1767 else
1768 nid = env.dst_nid;
1769
Rik van Riel4142c3e2016-01-25 17:07:39 -05001770 if (ng->active_nodes > 1 && numa_is_active_node(env.dst_nid, ng))
Rik van Rieldb015da2014-06-23 11:41:34 -04001771 sched_setnuma(p, env.dst_nid);
1772 }
1773
1774 /* No better CPU than the current one was found. */
1775 if (env.best_cpu == -1)
1776 return -EAGAIN;
Peter Zijlstra0ec8aa02013-10-07 11:29:33 +01001777
Rik van Riel04bb2f92013-10-07 11:29:36 +01001778 /*
1779 * Reset the scan period if the task is being rescheduled on an
1780 * alternative node to recheck if the tasks is now properly placed.
1781 */
1782 p->numa_scan_period = task_scan_min(p);
1783
Mel Gormanfb13c7e2013-10-07 11:29:17 +01001784 if (env.best_task == NULL) {
Mel Gorman286549d2014-01-21 15:51:03 -08001785 ret = migrate_task_to(p, env.best_cpu);
1786 if (ret != 0)
1787 trace_sched_stick_numa(p, env.src_cpu, env.best_cpu);
Mel Gormanfb13c7e2013-10-07 11:29:17 +01001788 return ret;
1789 }
1790
1791 ret = migrate_swap(p, env.best_task);
Mel Gorman286549d2014-01-21 15:51:03 -08001792 if (ret != 0)
1793 trace_sched_stick_numa(p, env.src_cpu, task_cpu(env.best_task));
Mel Gormanfb13c7e2013-10-07 11:29:17 +01001794 put_task_struct(env.best_task);
1795 return ret;
Mel Gormane6628d52013-10-07 11:29:02 +01001796}
1797
Mel Gorman6b9a7462013-10-07 11:29:11 +01001798/* Attempt to migrate a task to a CPU on the preferred node. */
1799static void numa_migrate_preferred(struct task_struct *p)
1800{
Rik van Riel5085e2a2014-04-11 13:00:28 -04001801 unsigned long interval = HZ;
1802
Rik van Riel2739d3e2013-10-07 11:29:41 +01001803 /* This task has no NUMA fault statistics yet */
Iulia Manda44dba3d2014-10-31 02:13:31 +02001804 if (unlikely(p->numa_preferred_nid == -1 || !p->numa_faults))
Rik van Riel2739d3e2013-10-07 11:29:41 +01001805 return;
1806
1807 /* Periodically retry migrating the task to the preferred node */
Rik van Riel5085e2a2014-04-11 13:00:28 -04001808 interval = min(interval, msecs_to_jiffies(p->numa_scan_period) / 16);
1809 p->numa_migrate_retry = jiffies + interval;
Rik van Riel2739d3e2013-10-07 11:29:41 +01001810
Mel Gorman6b9a7462013-10-07 11:29:11 +01001811 /* Success if task is already running on preferred CPU */
Wanpeng Lide1b3012013-12-12 15:23:24 +08001812 if (task_node(p) == p->numa_preferred_nid)
Mel Gorman6b9a7462013-10-07 11:29:11 +01001813 return;
1814
Mel Gorman6b9a7462013-10-07 11:29:11 +01001815 /* Otherwise, try migrate to a CPU on the preferred node */
Rik van Riel2739d3e2013-10-07 11:29:41 +01001816 task_numa_migrate(p);
Mel Gorman6b9a7462013-10-07 11:29:11 +01001817}
1818
Rik van Riel04bb2f92013-10-07 11:29:36 +01001819/*
Rik van Riel4142c3e2016-01-25 17:07:39 -05001820 * Find out how many nodes on the workload is actively running on. Do this by
Rik van Riel20e07de2014-01-27 17:03:43 -05001821 * tracking the nodes from which NUMA hinting faults are triggered. This can
1822 * be different from the set of nodes where the workload's memory is currently
1823 * located.
Rik van Riel20e07de2014-01-27 17:03:43 -05001824 */
Rik van Riel4142c3e2016-01-25 17:07:39 -05001825static void numa_group_count_active_nodes(struct numa_group *numa_group)
Rik van Riel20e07de2014-01-27 17:03:43 -05001826{
1827 unsigned long faults, max_faults = 0;
Rik van Riel4142c3e2016-01-25 17:07:39 -05001828 int nid, active_nodes = 0;
Rik van Riel20e07de2014-01-27 17:03:43 -05001829
1830 for_each_online_node(nid) {
1831 faults = group_faults_cpu(numa_group, nid);
1832 if (faults > max_faults)
1833 max_faults = faults;
1834 }
1835
1836 for_each_online_node(nid) {
1837 faults = group_faults_cpu(numa_group, nid);
Rik van Riel4142c3e2016-01-25 17:07:39 -05001838 if (faults * ACTIVE_NODE_FRACTION > max_faults)
1839 active_nodes++;
Rik van Riel20e07de2014-01-27 17:03:43 -05001840 }
Rik van Riel4142c3e2016-01-25 17:07:39 -05001841
1842 numa_group->max_faults_cpu = max_faults;
1843 numa_group->active_nodes = active_nodes;
Rik van Riel20e07de2014-01-27 17:03:43 -05001844}
1845
1846/*
Rik van Riel04bb2f92013-10-07 11:29:36 +01001847 * When adapting the scan rate, the period is divided into NUMA_PERIOD_SLOTS
1848 * increments. The more local the fault statistics are, the higher the scan
Rik van Riela22b4b02014-06-23 11:41:35 -04001849 * period will be for the next scan window. If local/(local+remote) ratio is
1850 * below NUMA_PERIOD_THRESHOLD (where range of ratio is 1..NUMA_PERIOD_SLOTS)
1851 * the scan period will decrease. Aim for 70% local accesses.
Rik van Riel04bb2f92013-10-07 11:29:36 +01001852 */
1853#define NUMA_PERIOD_SLOTS 10
Rik van Riela22b4b02014-06-23 11:41:35 -04001854#define NUMA_PERIOD_THRESHOLD 7
Rik van Riel04bb2f92013-10-07 11:29:36 +01001855
1856/*
1857 * Increase the scan period (slow down scanning) if the majority of
1858 * our memory is already on our local node, or if the majority of
1859 * the page accesses are shared with other processes.
1860 * Otherwise, decrease the scan period.
1861 */
1862static void update_task_scan_period(struct task_struct *p,
1863 unsigned long shared, unsigned long private)
1864{
1865 unsigned int period_slot;
1866 int ratio;
1867 int diff;
1868
1869 unsigned long remote = p->numa_faults_locality[0];
1870 unsigned long local = p->numa_faults_locality[1];
1871
1872 /*
1873 * If there were no record hinting faults then either the task is
1874 * completely idle or all activity is areas that are not of interest
Mel Gorman074c2382015-03-25 15:55:42 -07001875 * to automatic numa balancing. Related to that, if there were failed
1876 * migration then it implies we are migrating too quickly or the local
1877 * node is overloaded. In either case, scan slower
Rik van Riel04bb2f92013-10-07 11:29:36 +01001878 */
Mel Gorman074c2382015-03-25 15:55:42 -07001879 if (local + shared == 0 || p->numa_faults_locality[2]) {
Rik van Riel04bb2f92013-10-07 11:29:36 +01001880 p->numa_scan_period = min(p->numa_scan_period_max,
1881 p->numa_scan_period << 1);
1882
1883 p->mm->numa_next_scan = jiffies +
1884 msecs_to_jiffies(p->numa_scan_period);
1885
1886 return;
1887 }
1888
1889 /*
1890 * Prepare to scale scan period relative to the current period.
1891 * == NUMA_PERIOD_THRESHOLD scan period stays the same
1892 * < NUMA_PERIOD_THRESHOLD scan period decreases (scan faster)
1893 * >= NUMA_PERIOD_THRESHOLD scan period increases (scan slower)
1894 */
1895 period_slot = DIV_ROUND_UP(p->numa_scan_period, NUMA_PERIOD_SLOTS);
1896 ratio = (local * NUMA_PERIOD_SLOTS) / (local + remote);
1897 if (ratio >= NUMA_PERIOD_THRESHOLD) {
1898 int slot = ratio - NUMA_PERIOD_THRESHOLD;
1899 if (!slot)
1900 slot = 1;
1901 diff = slot * period_slot;
1902 } else {
1903 diff = -(NUMA_PERIOD_THRESHOLD - ratio) * period_slot;
1904
1905 /*
1906 * Scale scan rate increases based on sharing. There is an
1907 * inverse relationship between the degree of sharing and
1908 * the adjustment made to the scanning period. Broadly
1909 * speaking the intent is that there is little point
1910 * scanning faster if shared accesses dominate as it may
1911 * simply bounce migrations uselessly
1912 */
Yasuaki Ishimatsu2847c902014-10-22 16:04:35 +09001913 ratio = DIV_ROUND_UP(private * NUMA_PERIOD_SLOTS, (private + shared + 1));
Rik van Riel04bb2f92013-10-07 11:29:36 +01001914 diff = (diff * ratio) / NUMA_PERIOD_SLOTS;
1915 }
1916
1917 p->numa_scan_period = clamp(p->numa_scan_period + diff,
1918 task_scan_min(p), task_scan_max(p));
1919 memset(p->numa_faults_locality, 0, sizeof(p->numa_faults_locality));
1920}
1921
Rik van Riel7e2703e2014-01-27 17:03:45 -05001922/*
1923 * Get the fraction of time the task has been running since the last
1924 * NUMA placement cycle. The scheduler keeps similar statistics, but
1925 * decays those on a 32ms period, which is orders of magnitude off
1926 * from the dozens-of-seconds NUMA balancing period. Use the scheduler
1927 * stats only if the task is so new there are no NUMA statistics yet.
1928 */
1929static u64 numa_get_avg_runtime(struct task_struct *p, u64 *period)
1930{
1931 u64 runtime, delta, now;
1932 /* Use the start of this time slice to avoid calculations. */
1933 now = p->se.exec_start;
1934 runtime = p->se.sum_exec_runtime;
1935
1936 if (p->last_task_numa_placement) {
1937 delta = runtime - p->last_sum_exec_runtime;
1938 *period = now - p->last_task_numa_placement;
1939 } else {
Yuyang Du9d89c252015-07-15 08:04:37 +08001940 delta = p->se.avg.load_sum / p->se.load.weight;
1941 *period = LOAD_AVG_MAX;
Rik van Riel7e2703e2014-01-27 17:03:45 -05001942 }
1943
1944 p->last_sum_exec_runtime = runtime;
1945 p->last_task_numa_placement = now;
1946
1947 return delta;
1948}
1949
Rik van Riel54009412014-10-17 03:29:53 -04001950/*
1951 * Determine the preferred nid for a task in a numa_group. This needs to
1952 * be done in a way that produces consistent results with group_weight,
1953 * otherwise workloads might not converge.
1954 */
1955static int preferred_group_nid(struct task_struct *p, int nid)
1956{
1957 nodemask_t nodes;
1958 int dist;
1959
1960 /* Direct connections between all NUMA nodes. */
1961 if (sched_numa_topology_type == NUMA_DIRECT)
1962 return nid;
1963
1964 /*
1965 * On a system with glueless mesh NUMA topology, group_weight
1966 * scores nodes according to the number of NUMA hinting faults on
1967 * both the node itself, and on nearby nodes.
1968 */
1969 if (sched_numa_topology_type == NUMA_GLUELESS_MESH) {
1970 unsigned long score, max_score = 0;
1971 int node, max_node = nid;
1972
1973 dist = sched_max_numa_distance;
1974
1975 for_each_online_node(node) {
1976 score = group_weight(p, node, dist);
1977 if (score > max_score) {
1978 max_score = score;
1979 max_node = node;
1980 }
1981 }
1982 return max_node;
1983 }
1984
1985 /*
1986 * Finding the preferred nid in a system with NUMA backplane
1987 * interconnect topology is more involved. The goal is to locate
1988 * tasks from numa_groups near each other in the system, and
1989 * untangle workloads from different sides of the system. This requires
1990 * searching down the hierarchy of node groups, recursively searching
1991 * inside the highest scoring group of nodes. The nodemask tricks
1992 * keep the complexity of the search down.
1993 */
1994 nodes = node_online_map;
1995 for (dist = sched_max_numa_distance; dist > LOCAL_DISTANCE; dist--) {
1996 unsigned long max_faults = 0;
Jan Beulich81907472015-01-23 08:25:38 +00001997 nodemask_t max_group = NODE_MASK_NONE;
Rik van Riel54009412014-10-17 03:29:53 -04001998 int a, b;
1999
2000 /* Are there nodes at this distance from each other? */
2001 if (!find_numa_distance(dist))
2002 continue;
2003
2004 for_each_node_mask(a, nodes) {
2005 unsigned long faults = 0;
2006 nodemask_t this_group;
2007 nodes_clear(this_group);
2008
2009 /* Sum group's NUMA faults; includes a==b case. */
2010 for_each_node_mask(b, nodes) {
2011 if (node_distance(a, b) < dist) {
2012 faults += group_faults(p, b);
2013 node_set(b, this_group);
2014 node_clear(b, nodes);
2015 }
2016 }
2017
2018 /* Remember the top group. */
2019 if (faults > max_faults) {
2020 max_faults = faults;
2021 max_group = this_group;
2022 /*
2023 * subtle: at the smallest distance there is
2024 * just one node left in each "group", the
2025 * winner is the preferred nid.
2026 */
2027 nid = a;
2028 }
2029 }
2030 /* Next round, evaluate the nodes within max_group. */
Jan Beulich890a5402015-02-09 12:30:00 +01002031 if (!max_faults)
2032 break;
Rik van Riel54009412014-10-17 03:29:53 -04002033 nodes = max_group;
2034 }
2035 return nid;
2036}
2037
Peter Zijlstracbee9f82012-10-25 14:16:43 +02002038static void task_numa_placement(struct task_struct *p)
2039{
Mel Gorman83e1d2c2013-10-07 11:29:27 +01002040 int seq, nid, max_nid = -1, max_group_nid = -1;
2041 unsigned long max_faults = 0, max_group_faults = 0;
Rik van Riel04bb2f92013-10-07 11:29:36 +01002042 unsigned long fault_types[2] = { 0, 0 };
Rik van Riel7e2703e2014-01-27 17:03:45 -05002043 unsigned long total_faults;
2044 u64 runtime, period;
Mel Gorman7dbd13e2013-10-07 11:29:29 +01002045 spinlock_t *group_lock = NULL;
Peter Zijlstracbee9f82012-10-25 14:16:43 +02002046
Jason Low7e5a2c12015-04-30 17:28:14 -07002047 /*
2048 * The p->mm->numa_scan_seq field gets updated without
2049 * exclusive access. Use READ_ONCE() here to ensure
2050 * that the field is read in a single access:
2051 */
Jason Low316c1608d2015-04-28 13:00:20 -07002052 seq = READ_ONCE(p->mm->numa_scan_seq);
Peter Zijlstracbee9f82012-10-25 14:16:43 +02002053 if (p->numa_scan_seq == seq)
2054 return;
2055 p->numa_scan_seq = seq;
Mel Gorman598f0ec2013-10-07 11:28:55 +01002056 p->numa_scan_period_max = task_scan_max(p);
Peter Zijlstracbee9f82012-10-25 14:16:43 +02002057
Rik van Riel7e2703e2014-01-27 17:03:45 -05002058 total_faults = p->numa_faults_locality[0] +
2059 p->numa_faults_locality[1];
2060 runtime = numa_get_avg_runtime(p, &period);
2061
Mel Gorman7dbd13e2013-10-07 11:29:29 +01002062 /* If the task is part of a group prevent parallel updates to group stats */
2063 if (p->numa_group) {
2064 group_lock = &p->numa_group->lock;
Mike Galbraith60e69ee2014-04-07 10:55:15 +02002065 spin_lock_irq(group_lock);
Mel Gorman7dbd13e2013-10-07 11:29:29 +01002066 }
2067
Mel Gorman688b7582013-10-07 11:28:58 +01002068 /* Find the node with the highest number of faults */
2069 for_each_online_node(nid) {
Iulia Manda44dba3d2014-10-31 02:13:31 +02002070 /* Keep track of the offsets in numa_faults array */
2071 int mem_idx, membuf_idx, cpu_idx, cpubuf_idx;
Mel Gorman83e1d2c2013-10-07 11:29:27 +01002072 unsigned long faults = 0, group_faults = 0;
Iulia Manda44dba3d2014-10-31 02:13:31 +02002073 int priv;
Mel Gorman745d6142013-10-07 11:28:59 +01002074
Rik van Rielbe1e4e72014-01-27 17:03:48 -05002075 for (priv = 0; priv < NR_NUMA_HINT_FAULT_TYPES; priv++) {
Rik van Riel7e2703e2014-01-27 17:03:45 -05002076 long diff, f_diff, f_weight;
Peter Zijlstra8c8a7432013-10-07 11:29:21 +01002077
Iulia Manda44dba3d2014-10-31 02:13:31 +02002078 mem_idx = task_faults_idx(NUMA_MEM, nid, priv);
2079 membuf_idx = task_faults_idx(NUMA_MEMBUF, nid, priv);
2080 cpu_idx = task_faults_idx(NUMA_CPU, nid, priv);
2081 cpubuf_idx = task_faults_idx(NUMA_CPUBUF, nid, priv);
Mel Gorman745d6142013-10-07 11:28:59 +01002082
Mel Gormanac8e8952013-10-07 11:29:03 +01002083 /* Decay existing window, copy faults since last scan */
Iulia Manda44dba3d2014-10-31 02:13:31 +02002084 diff = p->numa_faults[membuf_idx] - p->numa_faults[mem_idx] / 2;
2085 fault_types[priv] += p->numa_faults[membuf_idx];
2086 p->numa_faults[membuf_idx] = 0;
Mel Gormanfb13c7e2013-10-07 11:29:17 +01002087
Rik van Riel7e2703e2014-01-27 17:03:45 -05002088 /*
2089 * Normalize the faults_from, so all tasks in a group
2090 * count according to CPU use, instead of by the raw
2091 * number of faults. Tasks with little runtime have
2092 * little over-all impact on throughput, and thus their
2093 * faults are less important.
2094 */
2095 f_weight = div64_u64(runtime << 16, period + 1);
Iulia Manda44dba3d2014-10-31 02:13:31 +02002096 f_weight = (f_weight * p->numa_faults[cpubuf_idx]) /
Rik van Riel7e2703e2014-01-27 17:03:45 -05002097 (total_faults + 1);
Iulia Manda44dba3d2014-10-31 02:13:31 +02002098 f_diff = f_weight - p->numa_faults[cpu_idx] / 2;
2099 p->numa_faults[cpubuf_idx] = 0;
Rik van Riel50ec8a42014-01-27 17:03:42 -05002100
Iulia Manda44dba3d2014-10-31 02:13:31 +02002101 p->numa_faults[mem_idx] += diff;
2102 p->numa_faults[cpu_idx] += f_diff;
2103 faults += p->numa_faults[mem_idx];
Mel Gorman83e1d2c2013-10-07 11:29:27 +01002104 p->total_numa_faults += diff;
Peter Zijlstra8c8a7432013-10-07 11:29:21 +01002105 if (p->numa_group) {
Iulia Manda44dba3d2014-10-31 02:13:31 +02002106 /*
2107 * safe because we can only change our own group
2108 *
2109 * mem_idx represents the offset for a given
2110 * nid and priv in a specific region because it
2111 * is at the beginning of the numa_faults array.
2112 */
2113 p->numa_group->faults[mem_idx] += diff;
2114 p->numa_group->faults_cpu[mem_idx] += f_diff;
Mel Gorman989348b2013-10-07 11:29:40 +01002115 p->numa_group->total_faults += diff;
Iulia Manda44dba3d2014-10-31 02:13:31 +02002116 group_faults += p->numa_group->faults[mem_idx];
Peter Zijlstra8c8a7432013-10-07 11:29:21 +01002117 }
Mel Gormanac8e8952013-10-07 11:29:03 +01002118 }
2119
Mel Gorman688b7582013-10-07 11:28:58 +01002120 if (faults > max_faults) {
2121 max_faults = faults;
2122 max_nid = nid;
2123 }
Mel Gorman83e1d2c2013-10-07 11:29:27 +01002124
2125 if (group_faults > max_group_faults) {
2126 max_group_faults = group_faults;
2127 max_group_nid = nid;
2128 }
2129 }
2130
Rik van Riel04bb2f92013-10-07 11:29:36 +01002131 update_task_scan_period(p, fault_types[0], fault_types[1]);
2132
Mel Gorman7dbd13e2013-10-07 11:29:29 +01002133 if (p->numa_group) {
Rik van Riel4142c3e2016-01-25 17:07:39 -05002134 numa_group_count_active_nodes(p->numa_group);
Mike Galbraith60e69ee2014-04-07 10:55:15 +02002135 spin_unlock_irq(group_lock);
Rik van Riel54009412014-10-17 03:29:53 -04002136 max_nid = preferred_group_nid(p, max_group_nid);
Mel Gorman688b7582013-10-07 11:28:58 +01002137 }
2138
Rik van Rielbb97fc32014-06-04 16:33:15 -04002139 if (max_faults) {
2140 /* Set the new preferred node */
2141 if (max_nid != p->numa_preferred_nid)
2142 sched_setnuma(p, max_nid);
2143
2144 if (task_node(p) != p->numa_preferred_nid)
2145 numa_migrate_preferred(p);
Mel Gorman3a7053b2013-10-07 11:29:00 +01002146 }
Peter Zijlstracbee9f82012-10-25 14:16:43 +02002147}
2148
Peter Zijlstra8c8a7432013-10-07 11:29:21 +01002149static inline int get_numa_group(struct numa_group *grp)
2150{
2151 return atomic_inc_not_zero(&grp->refcount);
2152}
2153
2154static inline void put_numa_group(struct numa_group *grp)
2155{
2156 if (atomic_dec_and_test(&grp->refcount))
2157 kfree_rcu(grp, rcu);
2158}
2159
Mel Gorman3e6a9412013-10-07 11:29:35 +01002160static void task_numa_group(struct task_struct *p, int cpupid, int flags,
2161 int *priv)
Peter Zijlstra8c8a7432013-10-07 11:29:21 +01002162{
2163 struct numa_group *grp, *my_grp;
2164 struct task_struct *tsk;
2165 bool join = false;
2166 int cpu = cpupid_to_cpu(cpupid);
2167 int i;
2168
2169 if (unlikely(!p->numa_group)) {
2170 unsigned int size = sizeof(struct numa_group) +
Rik van Riel50ec8a42014-01-27 17:03:42 -05002171 4*nr_node_ids*sizeof(unsigned long);
Peter Zijlstra8c8a7432013-10-07 11:29:21 +01002172
2173 grp = kzalloc(size, GFP_KERNEL | __GFP_NOWARN);
2174 if (!grp)
2175 return;
2176
2177 atomic_set(&grp->refcount, 1);
Rik van Riel4142c3e2016-01-25 17:07:39 -05002178 grp->active_nodes = 1;
2179 grp->max_faults_cpu = 0;
Peter Zijlstra8c8a7432013-10-07 11:29:21 +01002180 spin_lock_init(&grp->lock);
Mel Gormane29cf082013-10-07 11:29:22 +01002181 grp->gid = p->pid;
Rik van Riel50ec8a42014-01-27 17:03:42 -05002182 /* Second half of the array tracks nids where faults happen */
Rik van Rielbe1e4e72014-01-27 17:03:48 -05002183 grp->faults_cpu = grp->faults + NR_NUMA_HINT_FAULT_TYPES *
2184 nr_node_ids;
Peter Zijlstra8c8a7432013-10-07 11:29:21 +01002185
Rik van Rielbe1e4e72014-01-27 17:03:48 -05002186 for (i = 0; i < NR_NUMA_HINT_FAULT_STATS * nr_node_ids; i++)
Iulia Manda44dba3d2014-10-31 02:13:31 +02002187 grp->faults[i] = p->numa_faults[i];
Peter Zijlstra8c8a7432013-10-07 11:29:21 +01002188
Mel Gorman989348b2013-10-07 11:29:40 +01002189 grp->total_faults = p->total_numa_faults;
Mel Gorman83e1d2c2013-10-07 11:29:27 +01002190
Peter Zijlstra8c8a7432013-10-07 11:29:21 +01002191 grp->nr_tasks++;
2192 rcu_assign_pointer(p->numa_group, grp);
2193 }
2194
2195 rcu_read_lock();
Jason Low316c1608d2015-04-28 13:00:20 -07002196 tsk = READ_ONCE(cpu_rq(cpu)->curr);
Peter Zijlstra8c8a7432013-10-07 11:29:21 +01002197
2198 if (!cpupid_match_pid(tsk, cpupid))
Peter Zijlstra33547812013-10-09 10:24:48 +02002199 goto no_join;
Peter Zijlstra8c8a7432013-10-07 11:29:21 +01002200
2201 grp = rcu_dereference(tsk->numa_group);
2202 if (!grp)
Peter Zijlstra33547812013-10-09 10:24:48 +02002203 goto no_join;
Peter Zijlstra8c8a7432013-10-07 11:29:21 +01002204
2205 my_grp = p->numa_group;
2206 if (grp == my_grp)
Peter Zijlstra33547812013-10-09 10:24:48 +02002207 goto no_join;
Peter Zijlstra8c8a7432013-10-07 11:29:21 +01002208
2209 /*
2210 * Only join the other group if its bigger; if we're the bigger group,
2211 * the other task will join us.
2212 */
2213 if (my_grp->nr_tasks > grp->nr_tasks)
Peter Zijlstra33547812013-10-09 10:24:48 +02002214 goto no_join;
Peter Zijlstra8c8a7432013-10-07 11:29:21 +01002215
2216 /*
2217 * Tie-break on the grp address.
2218 */
2219 if (my_grp->nr_tasks == grp->nr_tasks && my_grp > grp)
Peter Zijlstra33547812013-10-09 10:24:48 +02002220 goto no_join;
Peter Zijlstra8c8a7432013-10-07 11:29:21 +01002221
Rik van Rieldabe1d92013-10-07 11:29:34 +01002222 /* Always join threads in the same process. */
2223 if (tsk->mm == current->mm)
2224 join = true;
Peter Zijlstra8c8a7432013-10-07 11:29:21 +01002225
Rik van Rieldabe1d92013-10-07 11:29:34 +01002226 /* Simple filter to avoid false positives due to PID collisions */
2227 if (flags & TNF_SHARED)
2228 join = true;
2229
Mel Gorman3e6a9412013-10-07 11:29:35 +01002230 /* Update priv based on whether false sharing was detected */
2231 *priv = !join;
2232
Rik van Rieldabe1d92013-10-07 11:29:34 +01002233 if (join && !get_numa_group(grp))
Peter Zijlstra33547812013-10-09 10:24:48 +02002234 goto no_join;
Peter Zijlstra8c8a7432013-10-07 11:29:21 +01002235
Peter Zijlstra8c8a7432013-10-07 11:29:21 +01002236 rcu_read_unlock();
2237
2238 if (!join)
2239 return;
2240
Mike Galbraith60e69ee2014-04-07 10:55:15 +02002241 BUG_ON(irqs_disabled());
2242 double_lock_irq(&my_grp->lock, &grp->lock);
Peter Zijlstra8c8a7432013-10-07 11:29:21 +01002243
Rik van Rielbe1e4e72014-01-27 17:03:48 -05002244 for (i = 0; i < NR_NUMA_HINT_FAULT_STATS * nr_node_ids; i++) {
Iulia Manda44dba3d2014-10-31 02:13:31 +02002245 my_grp->faults[i] -= p->numa_faults[i];
2246 grp->faults[i] += p->numa_faults[i];
Mel Gorman989348b2013-10-07 11:29:40 +01002247 }
2248 my_grp->total_faults -= p->total_numa_faults;
2249 grp->total_faults += p->total_numa_faults;
2250
Peter Zijlstra8c8a7432013-10-07 11:29:21 +01002251 my_grp->nr_tasks--;
2252 grp->nr_tasks++;
2253
2254 spin_unlock(&my_grp->lock);
Mike Galbraith60e69ee2014-04-07 10:55:15 +02002255 spin_unlock_irq(&grp->lock);
Peter Zijlstra8c8a7432013-10-07 11:29:21 +01002256
2257 rcu_assign_pointer(p->numa_group, grp);
2258
2259 put_numa_group(my_grp);
Peter Zijlstra33547812013-10-09 10:24:48 +02002260 return;
2261
2262no_join:
2263 rcu_read_unlock();
2264 return;
Peter Zijlstra8c8a7432013-10-07 11:29:21 +01002265}
2266
2267void task_numa_free(struct task_struct *p)
2268{
2269 struct numa_group *grp = p->numa_group;
Iulia Manda44dba3d2014-10-31 02:13:31 +02002270 void *numa_faults = p->numa_faults;
Steven Rostedte9dd6852014-05-27 17:02:04 -04002271 unsigned long flags;
2272 int i;
Peter Zijlstra8c8a7432013-10-07 11:29:21 +01002273
2274 if (grp) {
Steven Rostedte9dd6852014-05-27 17:02:04 -04002275 spin_lock_irqsave(&grp->lock, flags);
Rik van Rielbe1e4e72014-01-27 17:03:48 -05002276 for (i = 0; i < NR_NUMA_HINT_FAULT_STATS * nr_node_ids; i++)
Iulia Manda44dba3d2014-10-31 02:13:31 +02002277 grp->faults[i] -= p->numa_faults[i];
Mel Gorman989348b2013-10-07 11:29:40 +01002278 grp->total_faults -= p->total_numa_faults;
2279
Peter Zijlstra8c8a7432013-10-07 11:29:21 +01002280 grp->nr_tasks--;
Steven Rostedte9dd6852014-05-27 17:02:04 -04002281 spin_unlock_irqrestore(&grp->lock, flags);
Andreea-Cristina Bernat35b123e2014-08-22 17:50:43 +03002282 RCU_INIT_POINTER(p->numa_group, NULL);
Peter Zijlstra8c8a7432013-10-07 11:29:21 +01002283 put_numa_group(grp);
2284 }
2285
Iulia Manda44dba3d2014-10-31 02:13:31 +02002286 p->numa_faults = NULL;
Rik van Riel82727012013-10-07 11:29:28 +01002287 kfree(numa_faults);
Peter Zijlstra8c8a7432013-10-07 11:29:21 +01002288}
2289
Peter Zijlstracbee9f82012-10-25 14:16:43 +02002290/*
2291 * Got a PROT_NONE fault for a page on @node.
2292 */
Rik van Riel58b46da2014-01-27 17:03:47 -05002293void task_numa_fault(int last_cpupid, int mem_node, int pages, int flags)
Peter Zijlstracbee9f82012-10-25 14:16:43 +02002294{
2295 struct task_struct *p = current;
Peter Zijlstra6688cc02013-10-07 11:29:24 +01002296 bool migrated = flags & TNF_MIGRATED;
Rik van Riel58b46da2014-01-27 17:03:47 -05002297 int cpu_node = task_node(current);
Rik van Riel792568e2014-04-11 13:00:27 -04002298 int local = !!(flags & TNF_FAULT_LOCAL);
Rik van Riel4142c3e2016-01-25 17:07:39 -05002299 struct numa_group *ng;
Mel Gormanac8e8952013-10-07 11:29:03 +01002300 int priv;
Peter Zijlstracbee9f82012-10-25 14:16:43 +02002301
Srikar Dronamraju2a595722015-08-11 21:54:21 +05302302 if (!static_branch_likely(&sched_numa_balancing))
Mel Gorman1a687c22012-11-22 11:16:36 +00002303 return;
2304
Mel Gorman9ff1d9f2013-10-07 11:29:04 +01002305 /* for example, ksmd faulting in a user's mm */
2306 if (!p->mm)
2307 return;
2308
Mel Gormanf809ca92013-10-07 11:28:57 +01002309 /* Allocate buffer to track faults on a per-node basis */
Iulia Manda44dba3d2014-10-31 02:13:31 +02002310 if (unlikely(!p->numa_faults)) {
2311 int size = sizeof(*p->numa_faults) *
Rik van Rielbe1e4e72014-01-27 17:03:48 -05002312 NR_NUMA_HINT_FAULT_BUCKETS * nr_node_ids;
Mel Gormanf809ca92013-10-07 11:28:57 +01002313
Iulia Manda44dba3d2014-10-31 02:13:31 +02002314 p->numa_faults = kzalloc(size, GFP_KERNEL|__GFP_NOWARN);
2315 if (!p->numa_faults)
Mel Gormanf809ca92013-10-07 11:28:57 +01002316 return;
Mel Gorman745d6142013-10-07 11:28:59 +01002317
Mel Gorman83e1d2c2013-10-07 11:29:27 +01002318 p->total_numa_faults = 0;
Rik van Riel04bb2f92013-10-07 11:29:36 +01002319 memset(p->numa_faults_locality, 0, sizeof(p->numa_faults_locality));
Mel Gormanf809ca92013-10-07 11:28:57 +01002320 }
Peter Zijlstracbee9f82012-10-25 14:16:43 +02002321
Mel Gormanfb003b82012-11-15 09:01:14 +00002322 /*
Peter Zijlstra8c8a7432013-10-07 11:29:21 +01002323 * First accesses are treated as private, otherwise consider accesses
2324 * to be private if the accessing pid has not changed
2325 */
2326 if (unlikely(last_cpupid == (-1 & LAST_CPUPID_MASK))) {
2327 priv = 1;
2328 } else {
2329 priv = cpupid_match_pid(p, last_cpupid);
Peter Zijlstra6688cc02013-10-07 11:29:24 +01002330 if (!priv && !(flags & TNF_NO_GROUP))
Mel Gorman3e6a9412013-10-07 11:29:35 +01002331 task_numa_group(p, last_cpupid, flags, &priv);
Peter Zijlstra8c8a7432013-10-07 11:29:21 +01002332 }
2333
Rik van Riel792568e2014-04-11 13:00:27 -04002334 /*
2335 * If a workload spans multiple NUMA nodes, a shared fault that
2336 * occurs wholly within the set of nodes that the workload is
2337 * actively using should be counted as local. This allows the
2338 * scan rate to slow down when a workload has settled down.
2339 */
Rik van Riel4142c3e2016-01-25 17:07:39 -05002340 ng = p->numa_group;
2341 if (!priv && !local && ng && ng->active_nodes > 1 &&
2342 numa_is_active_node(cpu_node, ng) &&
2343 numa_is_active_node(mem_node, ng))
Rik van Riel792568e2014-04-11 13:00:27 -04002344 local = 1;
2345
Peter Zijlstracbee9f82012-10-25 14:16:43 +02002346 task_numa_placement(p);
Mel Gormanf809ca92013-10-07 11:28:57 +01002347
Rik van Riel2739d3e2013-10-07 11:29:41 +01002348 /*
2349 * Retry task to preferred node migration periodically, in case it
2350 * case it previously failed, or the scheduler moved us.
2351 */
2352 if (time_after(jiffies, p->numa_migrate_retry))
Mel Gorman6b9a7462013-10-07 11:29:11 +01002353 numa_migrate_preferred(p);
2354
Ingo Molnarb32e86b2013-10-07 11:29:30 +01002355 if (migrated)
2356 p->numa_pages_migrated += pages;
Mel Gorman074c2382015-03-25 15:55:42 -07002357 if (flags & TNF_MIGRATE_FAIL)
2358 p->numa_faults_locality[2] += pages;
Ingo Molnarb32e86b2013-10-07 11:29:30 +01002359
Iulia Manda44dba3d2014-10-31 02:13:31 +02002360 p->numa_faults[task_faults_idx(NUMA_MEMBUF, mem_node, priv)] += pages;
2361 p->numa_faults[task_faults_idx(NUMA_CPUBUF, cpu_node, priv)] += pages;
Rik van Riel792568e2014-04-11 13:00:27 -04002362 p->numa_faults_locality[local] += pages;
Peter Zijlstracbee9f82012-10-25 14:16:43 +02002363}
2364
Peter Zijlstra6e5fb222012-10-25 14:16:45 +02002365static void reset_ptenuma_scan(struct task_struct *p)
2366{
Jason Low7e5a2c12015-04-30 17:28:14 -07002367 /*
2368 * We only did a read acquisition of the mmap sem, so
2369 * p->mm->numa_scan_seq is written to without exclusive access
2370 * and the update is not guaranteed to be atomic. That's not
2371 * much of an issue though, since this is just used for
2372 * statistical sampling. Use READ_ONCE/WRITE_ONCE, which are not
2373 * expensive, to avoid any form of compiler optimizations:
2374 */
Jason Low316c1608d2015-04-28 13:00:20 -07002375 WRITE_ONCE(p->mm->numa_scan_seq, READ_ONCE(p->mm->numa_scan_seq) + 1);
Peter Zijlstra6e5fb222012-10-25 14:16:45 +02002376 p->mm->numa_scan_offset = 0;
2377}
2378
Peter Zijlstracbee9f82012-10-25 14:16:43 +02002379/*
2380 * The expensive part of numa migration is done from task_work context.
2381 * Triggered from task_tick_numa().
2382 */
2383void task_numa_work(struct callback_head *work)
2384{
2385 unsigned long migrate, next_scan, now = jiffies;
2386 struct task_struct *p = current;
2387 struct mm_struct *mm = p->mm;
Rik van Riel51170842015-11-05 15:56:23 -05002388 u64 runtime = p->se.sum_exec_runtime;
Peter Zijlstra6e5fb222012-10-25 14:16:45 +02002389 struct vm_area_struct *vma;
Mel Gorman9f406042012-11-14 18:34:32 +00002390 unsigned long start, end;
Mel Gorman598f0ec2013-10-07 11:28:55 +01002391 unsigned long nr_pte_updates = 0;
Rik van Riel4620f8c2015-09-11 09:00:27 -04002392 long pages, virtpages;
Peter Zijlstracbee9f82012-10-25 14:16:43 +02002393
Peter Zijlstra9148a3a2016-09-20 22:34:51 +02002394 SCHED_WARN_ON(p != container_of(work, struct task_struct, numa_work));
Peter Zijlstracbee9f82012-10-25 14:16:43 +02002395
2396 work->next = work; /* protect against double add */
2397 /*
2398 * Who cares about NUMA placement when they're dying.
2399 *
2400 * NOTE: make sure not to dereference p->mm before this check,
2401 * exit_task_work() happens _after_ exit_mm() so we could be called
2402 * without p->mm even though we still had it when we enqueued this
2403 * work.
2404 */
2405 if (p->flags & PF_EXITING)
2406 return;
2407
Mel Gorman930aa172013-10-07 11:29:37 +01002408 if (!mm->numa_next_scan) {
Mel Gorman7e8d16b2013-10-07 11:28:54 +01002409 mm->numa_next_scan = now +
2410 msecs_to_jiffies(sysctl_numa_balancing_scan_delay);
Mel Gormanb8593bf2012-11-21 01:18:23 +00002411 }
2412
2413 /*
Peter Zijlstracbee9f82012-10-25 14:16:43 +02002414 * Enforce maximal scan/migration frequency..
2415 */
2416 migrate = mm->numa_next_scan;
2417 if (time_before(now, migrate))
2418 return;
2419
Mel Gorman598f0ec2013-10-07 11:28:55 +01002420 if (p->numa_scan_period == 0) {
2421 p->numa_scan_period_max = task_scan_max(p);
2422 p->numa_scan_period = task_scan_min(p);
2423 }
Peter Zijlstracbee9f82012-10-25 14:16:43 +02002424
Mel Gormanfb003b82012-11-15 09:01:14 +00002425 next_scan = now + msecs_to_jiffies(p->numa_scan_period);
Peter Zijlstracbee9f82012-10-25 14:16:43 +02002426 if (cmpxchg(&mm->numa_next_scan, migrate, next_scan) != migrate)
2427 return;
2428
Mel Gormane14808b2012-11-19 10:59:15 +00002429 /*
Peter Zijlstra19a78d12013-10-07 11:28:51 +01002430 * Delay this task enough that another task of this mm will likely win
2431 * the next time around.
2432 */
2433 p->node_stamp += 2 * TICK_NSEC;
2434
Mel Gorman9f406042012-11-14 18:34:32 +00002435 start = mm->numa_scan_offset;
2436 pages = sysctl_numa_balancing_scan_size;
2437 pages <<= 20 - PAGE_SHIFT; /* MB in pages */
Rik van Riel4620f8c2015-09-11 09:00:27 -04002438 virtpages = pages * 8; /* Scan up to this much virtual space */
Mel Gorman9f406042012-11-14 18:34:32 +00002439 if (!pages)
2440 return;
Peter Zijlstracbee9f82012-10-25 14:16:43 +02002441
Rik van Riel4620f8c2015-09-11 09:00:27 -04002442
Peter Zijlstra6e5fb222012-10-25 14:16:45 +02002443 down_read(&mm->mmap_sem);
Mel Gorman9f406042012-11-14 18:34:32 +00002444 vma = find_vma(mm, start);
Peter Zijlstra6e5fb222012-10-25 14:16:45 +02002445 if (!vma) {
2446 reset_ptenuma_scan(p);
Mel Gorman9f406042012-11-14 18:34:32 +00002447 start = 0;
Peter Zijlstra6e5fb222012-10-25 14:16:45 +02002448 vma = mm->mmap;
Peter Zijlstracbee9f82012-10-25 14:16:43 +02002449 }
Mel Gorman9f406042012-11-14 18:34:32 +00002450 for (; vma; vma = vma->vm_next) {
Naoya Horiguchi6b79c572015-04-07 14:26:47 -07002451 if (!vma_migratable(vma) || !vma_policy_mof(vma) ||
Mel Gorman8e76d4e2015-06-10 11:15:00 -07002452 is_vm_hugetlb_page(vma) || (vma->vm_flags & VM_MIXEDMAP)) {
Peter Zijlstra6e5fb222012-10-25 14:16:45 +02002453 continue;
Naoya Horiguchi6b79c572015-04-07 14:26:47 -07002454 }
Peter Zijlstra6e5fb222012-10-25 14:16:45 +02002455
Mel Gorman4591ce4f2013-10-07 11:29:13 +01002456 /*
2457 * Shared library pages mapped by multiple processes are not
2458 * migrated as it is expected they are cache replicated. Avoid
2459 * hinting faults in read-only file-backed mappings or the vdso
2460 * as migrating the pages will be of marginal benefit.
2461 */
2462 if (!vma->vm_mm ||
2463 (vma->vm_file && (vma->vm_flags & (VM_READ|VM_WRITE)) == (VM_READ)))
2464 continue;
2465
Mel Gorman3c67f472013-12-18 17:08:40 -08002466 /*
2467 * Skip inaccessible VMAs to avoid any confusion between
2468 * PROT_NONE and NUMA hinting ptes
2469 */
2470 if (!(vma->vm_flags & (VM_READ | VM_EXEC | VM_WRITE)))
2471 continue;
2472
Mel Gorman9f406042012-11-14 18:34:32 +00002473 do {
2474 start = max(start, vma->vm_start);
2475 end = ALIGN(start + (pages << PAGE_SHIFT), HPAGE_SIZE);
2476 end = min(end, vma->vm_end);
Rik van Riel4620f8c2015-09-11 09:00:27 -04002477 nr_pte_updates = change_prot_numa(vma, start, end);
Mel Gorman598f0ec2013-10-07 11:28:55 +01002478
2479 /*
Rik van Riel4620f8c2015-09-11 09:00:27 -04002480 * Try to scan sysctl_numa_balancing_size worth of
2481 * hpages that have at least one present PTE that
2482 * is not already pte-numa. If the VMA contains
2483 * areas that are unused or already full of prot_numa
2484 * PTEs, scan up to virtpages, to skip through those
2485 * areas faster.
Mel Gorman598f0ec2013-10-07 11:28:55 +01002486 */
2487 if (nr_pte_updates)
2488 pages -= (end - start) >> PAGE_SHIFT;
Rik van Riel4620f8c2015-09-11 09:00:27 -04002489 virtpages -= (end - start) >> PAGE_SHIFT;
Peter Zijlstra6e5fb222012-10-25 14:16:45 +02002490
Mel Gorman9f406042012-11-14 18:34:32 +00002491 start = end;
Rik van Riel4620f8c2015-09-11 09:00:27 -04002492 if (pages <= 0 || virtpages <= 0)
Mel Gorman9f406042012-11-14 18:34:32 +00002493 goto out;
Rik van Riel3cf19622014-02-18 17:12:44 -05002494
2495 cond_resched();
Mel Gorman9f406042012-11-14 18:34:32 +00002496 } while (end != vma->vm_end);
Peter Zijlstra6e5fb222012-10-25 14:16:45 +02002497 }
2498
Mel Gorman9f406042012-11-14 18:34:32 +00002499out:
Peter Zijlstra6e5fb222012-10-25 14:16:45 +02002500 /*
Peter Zijlstrac69307d2013-10-07 11:28:41 +01002501 * It is possible to reach the end of the VMA list but the last few
2502 * VMAs are not guaranteed to the vma_migratable. If they are not, we
2503 * would find the !migratable VMA on the next scan but not reset the
2504 * scanner to the start so check it now.
Peter Zijlstra6e5fb222012-10-25 14:16:45 +02002505 */
2506 if (vma)
Mel Gorman9f406042012-11-14 18:34:32 +00002507 mm->numa_scan_offset = start;
Peter Zijlstra6e5fb222012-10-25 14:16:45 +02002508 else
2509 reset_ptenuma_scan(p);
2510 up_read(&mm->mmap_sem);
Rik van Riel51170842015-11-05 15:56:23 -05002511
2512 /*
2513 * Make sure tasks use at least 32x as much time to run other code
2514 * than they used here, to limit NUMA PTE scanning overhead to 3% max.
2515 * Usually update_task_scan_period slows down scanning enough; on an
2516 * overloaded system we need to limit overhead on a per task basis.
2517 */
2518 if (unlikely(p->se.sum_exec_runtime != runtime)) {
2519 u64 diff = p->se.sum_exec_runtime - runtime;
2520 p->node_stamp += 32 * diff;
2521 }
Peter Zijlstracbee9f82012-10-25 14:16:43 +02002522}
2523
2524/*
2525 * Drive the periodic memory faults..
2526 */
2527void task_tick_numa(struct rq *rq, struct task_struct *curr)
2528{
2529 struct callback_head *work = &curr->numa_work;
2530 u64 period, now;
2531
2532 /*
2533 * We don't care about NUMA placement if we don't have memory.
2534 */
2535 if (!curr->mm || (curr->flags & PF_EXITING) || work->next != work)
2536 return;
2537
2538 /*
2539 * Using runtime rather than walltime has the dual advantage that
2540 * we (mostly) drive the selection from busy threads and that the
2541 * task needs to have done some actual work before we bother with
2542 * NUMA placement.
2543 */
2544 now = curr->se.sum_exec_runtime;
2545 period = (u64)curr->numa_scan_period * NSEC_PER_MSEC;
2546
Rik van Riel25b3e5a2015-11-05 15:56:22 -05002547 if (now > curr->node_stamp + period) {
Peter Zijlstra4b96a29b2012-10-25 14:16:47 +02002548 if (!curr->node_stamp)
Mel Gorman598f0ec2013-10-07 11:28:55 +01002549 curr->numa_scan_period = task_scan_min(curr);
Peter Zijlstra19a78d12013-10-07 11:28:51 +01002550 curr->node_stamp += period;
Peter Zijlstracbee9f82012-10-25 14:16:43 +02002551
2552 if (!time_before(jiffies, curr->mm->numa_next_scan)) {
2553 init_task_work(work, task_numa_work); /* TODO: move this into sched_fork() */
2554 task_work_add(curr, work, true);
2555 }
2556 }
2557}
2558#else
2559static void task_tick_numa(struct rq *rq, struct task_struct *curr)
2560{
2561}
Peter Zijlstra0ec8aa02013-10-07 11:29:33 +01002562
2563static inline void account_numa_enqueue(struct rq *rq, struct task_struct *p)
2564{
2565}
2566
2567static inline void account_numa_dequeue(struct rq *rq, struct task_struct *p)
2568{
2569}
Peter Zijlstracbee9f82012-10-25 14:16:43 +02002570#endif /* CONFIG_NUMA_BALANCING */
2571
Dmitry Adamushko30cfdcf2007-10-15 17:00:07 +02002572static void
2573account_entity_enqueue(struct cfs_rq *cfs_rq, struct sched_entity *se)
2574{
2575 update_load_add(&cfs_rq->load, se->load.weight);
Peter Zijlstrac09595f2008-06-27 13:41:14 +02002576 if (!parent_entity(se))
Peter Zijlstra029632f2011-10-25 10:00:11 +02002577 update_load_add(&rq_of(cfs_rq)->load, se->load.weight);
Peter Zijlstra367456c2012-02-20 21:49:09 +01002578#ifdef CONFIG_SMP
Peter Zijlstra0ec8aa02013-10-07 11:29:33 +01002579 if (entity_is_task(se)) {
2580 struct rq *rq = rq_of(cfs_rq);
2581
2582 account_numa_enqueue(rq, task_of(se));
2583 list_add(&se->group_node, &rq->cfs_tasks);
2584 }
Peter Zijlstra367456c2012-02-20 21:49:09 +01002585#endif
Dmitry Adamushko30cfdcf2007-10-15 17:00:07 +02002586 cfs_rq->nr_running++;
Dmitry Adamushko30cfdcf2007-10-15 17:00:07 +02002587}
2588
2589static void
2590account_entity_dequeue(struct cfs_rq *cfs_rq, struct sched_entity *se)
2591{
2592 update_load_sub(&cfs_rq->load, se->load.weight);
Peter Zijlstrac09595f2008-06-27 13:41:14 +02002593 if (!parent_entity(se))
Peter Zijlstra029632f2011-10-25 10:00:11 +02002594 update_load_sub(&rq_of(cfs_rq)->load, se->load.weight);
Tim Chenbfdb1982016-02-01 14:47:59 -08002595#ifdef CONFIG_SMP
Peter Zijlstra0ec8aa02013-10-07 11:29:33 +01002596 if (entity_is_task(se)) {
2597 account_numa_dequeue(rq_of(cfs_rq), task_of(se));
Bharata B Raob87f1722008-09-25 09:53:54 +05302598 list_del_init(&se->group_node);
Peter Zijlstra0ec8aa02013-10-07 11:29:33 +01002599 }
Tim Chenbfdb1982016-02-01 14:47:59 -08002600#endif
Dmitry Adamushko30cfdcf2007-10-15 17:00:07 +02002601 cfs_rq->nr_running--;
Dmitry Adamushko30cfdcf2007-10-15 17:00:07 +02002602}
2603
Yong Zhang3ff6dca2011-01-24 15:33:52 +08002604#ifdef CONFIG_FAIR_GROUP_SCHED
2605# ifdef CONFIG_SMP
Paul Turner6d5ab292011-01-21 20:45:01 -08002606static long calc_cfs_shares(struct cfs_rq *cfs_rq, struct task_group *tg)
Yong Zhang3ff6dca2011-01-24 15:33:52 +08002607{
Peter Zijlstracf5f0ac2011-10-13 16:52:28 +02002608 long tg_weight, load, shares;
Yong Zhang3ff6dca2011-01-24 15:33:52 +08002609
Peter Zijlstraea1dc6f2016-06-24 16:11:02 +02002610 /*
2611 * This really should be: cfs_rq->avg.load_avg, but instead we use
2612 * cfs_rq->load.weight, which is its upper bound. This helps ramp up
2613 * the shares for small weight interactive tasks.
2614 */
2615 load = scale_load_down(cfs_rq->load.weight);
2616
2617 tg_weight = atomic_long_read(&tg->load_avg);
2618
2619 /* Ensure tg_weight >= load */
2620 tg_weight -= cfs_rq->tg_load_avg_contrib;
2621 tg_weight += load;
Yong Zhang3ff6dca2011-01-24 15:33:52 +08002622
Yong Zhang3ff6dca2011-01-24 15:33:52 +08002623 shares = (tg->shares * load);
Peter Zijlstracf5f0ac2011-10-13 16:52:28 +02002624 if (tg_weight)
2625 shares /= tg_weight;
Yong Zhang3ff6dca2011-01-24 15:33:52 +08002626
2627 if (shares < MIN_SHARES)
2628 shares = MIN_SHARES;
2629 if (shares > tg->shares)
2630 shares = tg->shares;
2631
2632 return shares;
2633}
Yong Zhang3ff6dca2011-01-24 15:33:52 +08002634# else /* CONFIG_SMP */
Paul Turner6d5ab292011-01-21 20:45:01 -08002635static inline long calc_cfs_shares(struct cfs_rq *cfs_rq, struct task_group *tg)
Yong Zhang3ff6dca2011-01-24 15:33:52 +08002636{
2637 return tg->shares;
2638}
Yong Zhang3ff6dca2011-01-24 15:33:52 +08002639# endif /* CONFIG_SMP */
Peter Zijlstraea1dc6f2016-06-24 16:11:02 +02002640
Peter Zijlstra2069dd72010-11-15 15:47:00 -08002641static void reweight_entity(struct cfs_rq *cfs_rq, struct sched_entity *se,
2642 unsigned long weight)
2643{
Paul Turner19e5eeb2010-12-15 19:10:18 -08002644 if (se->on_rq) {
2645 /* commit outstanding execution time */
2646 if (cfs_rq->curr == se)
2647 update_curr(cfs_rq);
Peter Zijlstra2069dd72010-11-15 15:47:00 -08002648 account_entity_dequeue(cfs_rq, se);
Paul Turner19e5eeb2010-12-15 19:10:18 -08002649 }
Peter Zijlstra2069dd72010-11-15 15:47:00 -08002650
2651 update_load_set(&se->load, weight);
2652
2653 if (se->on_rq)
2654 account_entity_enqueue(cfs_rq, se);
2655}
2656
Paul Turner82958362012-10-04 13:18:31 +02002657static inline int throttled_hierarchy(struct cfs_rq *cfs_rq);
2658
Paul Turner6d5ab292011-01-21 20:45:01 -08002659static void update_cfs_shares(struct cfs_rq *cfs_rq)
Peter Zijlstra2069dd72010-11-15 15:47:00 -08002660{
2661 struct task_group *tg;
2662 struct sched_entity *se;
Yong Zhang3ff6dca2011-01-24 15:33:52 +08002663 long shares;
Peter Zijlstra2069dd72010-11-15 15:47:00 -08002664
Peter Zijlstra2069dd72010-11-15 15:47:00 -08002665 tg = cfs_rq->tg;
2666 se = tg->se[cpu_of(rq_of(cfs_rq))];
Paul Turner64660c82011-07-21 09:43:36 -07002667 if (!se || throttled_hierarchy(cfs_rq))
Peter Zijlstra2069dd72010-11-15 15:47:00 -08002668 return;
Yong Zhang3ff6dca2011-01-24 15:33:52 +08002669#ifndef CONFIG_SMP
2670 if (likely(se->load.weight == tg->shares))
2671 return;
2672#endif
Paul Turner6d5ab292011-01-21 20:45:01 -08002673 shares = calc_cfs_shares(cfs_rq, tg);
Peter Zijlstra2069dd72010-11-15 15:47:00 -08002674
2675 reweight_entity(cfs_rq_of(se), se, shares);
2676}
2677#else /* CONFIG_FAIR_GROUP_SCHED */
Paul Turner6d5ab292011-01-21 20:45:01 -08002678static inline void update_cfs_shares(struct cfs_rq *cfs_rq)
Peter Zijlstra2069dd72010-11-15 15:47:00 -08002679{
2680}
2681#endif /* CONFIG_FAIR_GROUP_SCHED */
2682
Alex Shi141965c2013-06-26 13:05:39 +08002683#ifdef CONFIG_SMP
Syed Rameez Mustafadddcab72016-09-07 16:18:27 -07002684u32 sched_get_wake_up_idle(struct task_struct *p)
2685{
2686 u32 enabled = p->flags & PF_WAKE_UP_IDLE;
2687
2688 return !!enabled;
2689}
2690
2691int sched_set_wake_up_idle(struct task_struct *p, int wake_up_idle)
2692{
2693 int enable = !!wake_up_idle;
2694
2695 if (enable)
2696 p->flags |= PF_WAKE_UP_IDLE;
2697 else
2698 p->flags &= ~PF_WAKE_UP_IDLE;
2699
2700 return 0;
2701}
2702
Paul Turner5b51f2f2012-10-04 13:18:32 +02002703/* Precomputed fixed inverse multiplies for multiplication by y^n */
2704static const u32 runnable_avg_yN_inv[] = {
2705 0xffffffff, 0xfa83b2da, 0xf5257d14, 0xefe4b99a, 0xeac0c6e6, 0xe5b906e6,
2706 0xe0ccdeeb, 0xdbfbb796, 0xd744fcc9, 0xd2a81d91, 0xce248c14, 0xc9b9bd85,
2707 0xc5672a10, 0xc12c4cc9, 0xbd08a39e, 0xb8fbaf46, 0xb504f333, 0xb123f581,
2708 0xad583ee9, 0xa9a15ab4, 0xa5fed6a9, 0xa2704302, 0x9ef5325f, 0x9b8d39b9,
2709 0x9837f050, 0x94f4efa8, 0x91c3d373, 0x8ea4398a, 0x8b95c1e3, 0x88980e80,
2710 0x85aac367, 0x82cd8698,
2711};
2712
2713/*
2714 * Precomputed \Sum y^k { 1<=k<=n }. These are floor(true_value) to prevent
2715 * over-estimates when re-combining.
2716 */
2717static const u32 runnable_avg_yN_sum[] = {
2718 0, 1002, 1982, 2941, 3880, 4798, 5697, 6576, 7437, 8279, 9103,
2719 9909,10698,11470,12226,12966,13690,14398,15091,15769,16433,17082,
2720 17718,18340,18949,19545,20128,20698,21256,21802,22336,22859,23371,
2721};
2722
2723/*
Yuyang Du7b20b912016-05-03 05:54:27 +08002724 * Precomputed \Sum y^k { 1<=k<=n, where n%32=0). Values are rolled down to
2725 * lower integers. See Documentation/scheduler/sched-avg.txt how these
2726 * were generated:
2727 */
2728static const u32 __accumulated_sum_N32[] = {
2729 0, 23371, 35056, 40899, 43820, 45281,
2730 46011, 46376, 46559, 46650, 46696, 46719,
2731};
2732
2733/*
Paul Turner9d85f212012-10-04 13:18:29 +02002734 * Approximate:
2735 * val * y^n, where y^32 ~= 0.5 (~1 scheduling period)
2736 */
2737static __always_inline u64 decay_load(u64 val, u64 n)
2738{
Paul Turner5b51f2f2012-10-04 13:18:32 +02002739 unsigned int local_n;
2740
2741 if (!n)
2742 return val;
2743 else if (unlikely(n > LOAD_AVG_PERIOD * 63))
2744 return 0;
2745
2746 /* after bounds checking we can collapse to 32-bit */
2747 local_n = n;
2748
2749 /*
2750 * As y^PERIOD = 1/2, we can combine
Zhihui Zhang9c58c792014-09-20 21:24:36 -04002751 * y^n = 1/2^(n/PERIOD) * y^(n%PERIOD)
2752 * With a look-up table which covers y^n (n<PERIOD)
Paul Turner5b51f2f2012-10-04 13:18:32 +02002753 *
2754 * To achieve constant time decay_load.
2755 */
2756 if (unlikely(local_n >= LOAD_AVG_PERIOD)) {
2757 val >>= local_n / LOAD_AVG_PERIOD;
2758 local_n %= LOAD_AVG_PERIOD;
Paul Turner9d85f212012-10-04 13:18:29 +02002759 }
2760
Yuyang Du9d89c252015-07-15 08:04:37 +08002761 val = mul_u64_u32_shr(val, runnable_avg_yN_inv[local_n], 32);
2762 return val;
Paul Turner5b51f2f2012-10-04 13:18:32 +02002763}
2764
2765/*
2766 * For updates fully spanning n periods, the contribution to runnable
2767 * average will be: \Sum 1024*y^n
2768 *
2769 * We can compute this reasonably efficiently by combining:
2770 * y^PERIOD = 1/2 with precomputed \Sum 1024*y^n {for n <PERIOD}
2771 */
2772static u32 __compute_runnable_contrib(u64 n)
2773{
2774 u32 contrib = 0;
2775
2776 if (likely(n <= LOAD_AVG_PERIOD))
2777 return runnable_avg_yN_sum[n];
2778 else if (unlikely(n >= LOAD_AVG_MAX_N))
2779 return LOAD_AVG_MAX;
2780
Yuyang Du7b20b912016-05-03 05:54:27 +08002781 /* Since n < LOAD_AVG_MAX_N, n/LOAD_AVG_PERIOD < 11 */
2782 contrib = __accumulated_sum_N32[n/LOAD_AVG_PERIOD];
2783 n %= LOAD_AVG_PERIOD;
Paul Turner5b51f2f2012-10-04 13:18:32 +02002784 contrib = decay_load(contrib, n);
2785 return contrib + runnable_avg_yN_sum[n];
Paul Turner9d85f212012-10-04 13:18:29 +02002786}
2787
Syed Rameez Mustafadddcab72016-09-07 16:18:27 -07002788#ifdef CONFIG_SCHED_HMP
2789
2790/* CPU selection flag */
2791#define SBC_FLAG_PREV_CPU 0x1
2792#define SBC_FLAG_BEST_CAP_CPU 0x2
2793#define SBC_FLAG_CPU_COST 0x4
2794#define SBC_FLAG_MIN_COST 0x8
2795#define SBC_FLAG_IDLE_LEAST_LOADED 0x10
2796#define SBC_FLAG_IDLE_CSTATE 0x20
2797#define SBC_FLAG_COST_CSTATE_TIE_BREAKER 0x40
2798#define SBC_FLAG_COST_CSTATE_PREV_CPU_TIE_BREAKER 0x80
2799#define SBC_FLAG_CSTATE_LOAD 0x100
2800#define SBC_FLAG_BEST_SIBLING 0x200
2801
2802/* Cluster selection flag */
2803#define SBC_FLAG_COLOC_CLUSTER 0x10000
2804#define SBC_FLAG_WAKER_CLUSTER 0x20000
2805#define SBC_FLAG_BACKUP_CLUSTER 0x40000
2806
2807struct cpu_select_env {
2808 struct task_struct *p;
2809 struct related_thread_group *rtg;
2810 u8 reason;
2811 u8 need_idle:1;
2812 u8 need_waker_cluster:1;
2813 u8 sync:1;
2814 u8 ignore_prev_cpu:1;
2815 enum sched_boost_type boost_type;
2816 int prev_cpu;
2817 DECLARE_BITMAP(candidate_list, NR_CPUS);
2818 DECLARE_BITMAP(backup_list, NR_CPUS);
2819 u64 task_load;
2820 u64 cpu_load;
2821 u32 sbc_best_flag;
2822 u32 sbc_best_cluster_flag;
2823};
2824
2825struct cluster_cpu_stats {
2826 int best_idle_cpu, least_loaded_cpu;
2827 int best_capacity_cpu, best_cpu, best_sibling_cpu;
2828 int min_cost, best_sibling_cpu_cost;
2829 int best_cpu_cstate;
2830 u64 min_load, best_load, best_sibling_cpu_load;
2831 s64 highest_spare_capacity;
2832};
2833
2834static int spill_threshold_crossed(struct cpu_select_env *env, struct rq *rq)
2835{
2836 u64 total_load;
2837
2838 total_load = env->task_load + env->cpu_load;
2839
2840 if (total_load > sched_spill_load ||
2841 (rq->nr_running + 1) > sysctl_sched_spill_nr_run)
2842 return 1;
2843
2844 return 0;
2845}
2846
2847static int skip_cpu(int cpu, struct cpu_select_env *env)
2848{
2849 int tcpu = task_cpu(env->p);
2850 int skip = 0;
2851
2852 if (!env->reason)
2853 return 0;
2854
2855 if (is_reserved(cpu))
2856 return 1;
2857
2858 switch (env->reason) {
2859 case UP_MIGRATION:
2860 skip = !idle_cpu(cpu);
2861 break;
2862 case IRQLOAD_MIGRATION:
2863 /* Purposely fall through */
2864 default:
2865 skip = (cpu == tcpu);
2866 break;
2867 }
2868
2869 return skip;
2870}
2871
2872static inline int
2873acceptable_capacity(struct sched_cluster *cluster, struct cpu_select_env *env)
2874{
2875 int tcpu;
2876
2877 if (!env->reason)
2878 return 1;
2879
2880 tcpu = task_cpu(env->p);
2881 switch (env->reason) {
2882 case UP_MIGRATION:
2883 return cluster->capacity > cpu_capacity(tcpu);
2884
2885 case DOWN_MIGRATION:
2886 return cluster->capacity < cpu_capacity(tcpu);
2887
2888 default:
2889 break;
2890 }
2891
2892 return 1;
2893}
2894
2895static int
2896skip_cluster(struct sched_cluster *cluster, struct cpu_select_env *env)
2897{
2898 if (!test_bit(cluster->id, env->candidate_list))
2899 return 1;
2900
2901 if (!acceptable_capacity(cluster, env)) {
2902 __clear_bit(cluster->id, env->candidate_list);
2903 return 1;
2904 }
2905
2906 return 0;
2907}
2908
2909static struct sched_cluster *
2910select_least_power_cluster(struct cpu_select_env *env)
2911{
2912 struct sched_cluster *cluster;
2913
2914 if (env->rtg) {
2915 env->task_load = scale_load_to_cpu(task_load(env->p),
2916 cluster_first_cpu(env->rtg->preferred_cluster));
2917 env->sbc_best_cluster_flag |= SBC_FLAG_COLOC_CLUSTER;
2918 return env->rtg->preferred_cluster;
2919 }
2920
2921 for_each_sched_cluster(cluster) {
2922 if (!skip_cluster(cluster, env)) {
2923 int cpu = cluster_first_cpu(cluster);
2924
2925 env->task_load = scale_load_to_cpu(task_load(env->p),
2926 cpu);
2927 if (task_load_will_fit(env->p, env->task_load, cpu,
2928 env->boost_type))
2929 return cluster;
2930
2931 __set_bit(cluster->id, env->backup_list);
2932 __clear_bit(cluster->id, env->candidate_list);
2933 }
2934 }
2935
2936 return NULL;
2937}
2938
2939static struct sched_cluster *
2940next_candidate(const unsigned long *list, int start, int end)
2941{
2942 int cluster_id;
2943
2944 cluster_id = find_next_bit(list, end, start - 1 + 1);
2945 if (cluster_id >= end)
2946 return NULL;
2947
2948 return sched_cluster[cluster_id];
2949}
2950
2951static void
2952update_spare_capacity(struct cluster_cpu_stats *stats,
2953 struct cpu_select_env *env, int cpu, int capacity,
2954 u64 cpu_load)
2955{
2956 s64 spare_capacity = sched_ravg_window - cpu_load;
2957
2958 if (spare_capacity > 0 &&
2959 (spare_capacity > stats->highest_spare_capacity ||
2960 (spare_capacity == stats->highest_spare_capacity &&
2961 ((!env->need_waker_cluster &&
2962 capacity > cpu_capacity(stats->best_capacity_cpu)) ||
2963 (env->need_waker_cluster &&
2964 cpu_rq(cpu)->nr_running <
2965 cpu_rq(stats->best_capacity_cpu)->nr_running))))) {
2966 /*
2967 * If sync waker is the only runnable of CPU, cr_avg of the
2968 * CPU is 0 so we have high chance to place the wakee on the
2969 * waker's CPU which likely causes preemtion of the waker.
2970 * This can lead migration of preempted waker. Place the
2971 * wakee on the real idle CPU when it's possible by checking
2972 * nr_running to avoid such preemption.
2973 */
2974 stats->highest_spare_capacity = spare_capacity;
2975 stats->best_capacity_cpu = cpu;
2976 }
2977}
2978
2979static inline void find_backup_cluster(
2980struct cpu_select_env *env, struct cluster_cpu_stats *stats)
2981{
2982 struct sched_cluster *next = NULL;
2983 int i;
2984
2985 while (!bitmap_empty(env->backup_list, num_clusters)) {
2986 next = next_candidate(env->backup_list, 0, num_clusters);
2987 __clear_bit(next->id, env->backup_list);
2988 for_each_cpu_and(i, &env->p->cpus_allowed, &next->cpus) {
2989 trace_sched_cpu_load_wakeup(cpu_rq(i), idle_cpu(i),
2990 sched_irqload(i), power_cost(i, task_load(env->p) +
2991 cpu_cravg_sync(i, env->sync)), 0);
2992
2993 update_spare_capacity(stats, env, i, next->capacity,
2994 cpu_load_sync(i, env->sync));
2995 }
2996 env->sbc_best_cluster_flag = SBC_FLAG_BACKUP_CLUSTER;
2997 }
2998}
2999
3000struct sched_cluster *
3001next_best_cluster(struct sched_cluster *cluster, struct cpu_select_env *env,
3002 struct cluster_cpu_stats *stats)
3003{
3004 struct sched_cluster *next = NULL;
3005
3006 __clear_bit(cluster->id, env->candidate_list);
3007
3008 if (env->rtg && preferred_cluster(cluster, env->p))
3009 return NULL;
3010
3011 do {
3012 if (bitmap_empty(env->candidate_list, num_clusters))
3013 return NULL;
3014
3015 next = next_candidate(env->candidate_list, 0, num_clusters);
3016 if (next) {
3017 if (next->min_power_cost > stats->min_cost) {
3018 clear_bit(next->id, env->candidate_list);
3019 next = NULL;
3020 continue;
3021 }
3022
3023 if (skip_cluster(next, env))
3024 next = NULL;
3025 }
3026 } while (!next);
3027
3028 env->task_load = scale_load_to_cpu(task_load(env->p),
3029 cluster_first_cpu(next));
3030 return next;
3031}
3032
3033#ifdef CONFIG_SCHED_HMP_CSTATE_AWARE
3034static void __update_cluster_stats(int cpu, struct cluster_cpu_stats *stats,
3035 struct cpu_select_env *env, int cpu_cost)
3036{
3037 int cpu_cstate;
3038 int prev_cpu = env->prev_cpu;
3039
3040 cpu_cstate = cpu_rq(cpu)->cstate;
3041
3042 if (env->need_idle) {
3043 stats->min_cost = cpu_cost;
3044 if (idle_cpu(cpu)) {
3045 if (cpu_cstate < stats->best_cpu_cstate ||
3046 (cpu_cstate == stats->best_cpu_cstate &&
3047 cpu == prev_cpu)) {
3048 stats->best_idle_cpu = cpu;
3049 stats->best_cpu_cstate = cpu_cstate;
3050 }
3051 } else {
3052 if (env->cpu_load < stats->min_load ||
3053 (env->cpu_load == stats->min_load &&
3054 cpu == prev_cpu)) {
3055 stats->least_loaded_cpu = cpu;
3056 stats->min_load = env->cpu_load;
3057 }
3058 }
3059
3060 return;
3061 }
3062
3063 if (cpu_cost < stats->min_cost) {
3064 stats->min_cost = cpu_cost;
3065 stats->best_cpu_cstate = cpu_cstate;
3066 stats->best_load = env->cpu_load;
3067 stats->best_cpu = cpu;
3068 env->sbc_best_flag = SBC_FLAG_CPU_COST;
3069 return;
3070 }
3071
3072 /* CPU cost is the same. Start breaking the tie by C-state */
3073
3074 if (cpu_cstate > stats->best_cpu_cstate)
3075 return;
3076
3077 if (cpu_cstate < stats->best_cpu_cstate) {
3078 stats->best_cpu_cstate = cpu_cstate;
3079 stats->best_load = env->cpu_load;
3080 stats->best_cpu = cpu;
3081 env->sbc_best_flag = SBC_FLAG_COST_CSTATE_TIE_BREAKER;
3082 return;
3083 }
3084
3085 /* C-state is the same. Use prev CPU to break the tie */
3086 if (cpu == prev_cpu) {
3087 stats->best_cpu = cpu;
3088 env->sbc_best_flag = SBC_FLAG_COST_CSTATE_PREV_CPU_TIE_BREAKER;
3089 return;
3090 }
3091
3092 if (stats->best_cpu != prev_cpu &&
3093 ((cpu_cstate == 0 && env->cpu_load < stats->best_load) ||
3094 (cpu_cstate > 0 && env->cpu_load > stats->best_load))) {
3095 stats->best_load = env->cpu_load;
3096 stats->best_cpu = cpu;
3097 env->sbc_best_flag = SBC_FLAG_CSTATE_LOAD;
3098 }
3099}
3100#else /* CONFIG_SCHED_HMP_CSTATE_AWARE */
3101static void __update_cluster_stats(int cpu, struct cluster_cpu_stats *stats,
3102 struct cpu_select_env *env, int cpu_cost)
3103{
3104 int prev_cpu = env->prev_cpu;
3105
3106 if (cpu != prev_cpu && cpus_share_cache(prev_cpu, cpu)) {
3107 if (stats->best_sibling_cpu_cost > cpu_cost ||
3108 (stats->best_sibling_cpu_cost == cpu_cost &&
3109 stats->best_sibling_cpu_load > env->cpu_load)) {
3110 stats->best_sibling_cpu_cost = cpu_cost;
3111 stats->best_sibling_cpu_load = env->cpu_load;
3112 stats->best_sibling_cpu = cpu;
3113 }
3114 }
3115
3116 if ((cpu_cost < stats->min_cost) ||
3117 ((stats->best_cpu != prev_cpu &&
3118 stats->min_load > env->cpu_load) || cpu == prev_cpu)) {
3119 if (env->need_idle) {
3120 if (idle_cpu(cpu)) {
3121 stats->min_cost = cpu_cost;
3122 stats->best_idle_cpu = cpu;
3123 }
3124 } else {
3125 stats->min_cost = cpu_cost;
3126 stats->min_load = env->cpu_load;
3127 stats->best_cpu = cpu;
3128 env->sbc_best_flag = SBC_FLAG_MIN_COST;
3129 }
3130 }
3131}
3132#endif /* CONFIG_SCHED_HMP_CSTATE_AWARE */
3133
3134static void update_cluster_stats(int cpu, struct cluster_cpu_stats *stats,
3135 struct cpu_select_env *env)
3136{
3137 int cpu_cost;
3138
3139 cpu_cost = power_cost(cpu, task_load(env->p) +
3140 cpu_cravg_sync(cpu, env->sync));
3141 if (cpu_cost <= stats->min_cost)
3142 __update_cluster_stats(cpu, stats, env, cpu_cost);
3143}
3144
3145static void find_best_cpu_in_cluster(struct sched_cluster *c,
3146 struct cpu_select_env *env, struct cluster_cpu_stats *stats)
3147{
3148 int i;
3149 struct cpumask search_cpus;
3150
3151 cpumask_and(&search_cpus, tsk_cpus_allowed(env->p), &c->cpus);
3152 if (env->ignore_prev_cpu)
3153 cpumask_clear_cpu(env->prev_cpu, &search_cpus);
3154
3155 for_each_cpu(i, &search_cpus) {
3156 env->cpu_load = cpu_load_sync(i, env->sync);
3157
3158 trace_sched_cpu_load_wakeup(cpu_rq(i), idle_cpu(i),
3159 sched_irqload(i),
3160 power_cost(i, task_load(env->p) +
3161 cpu_cravg_sync(i, env->sync)), 0);
3162
3163 if (unlikely(!cpu_active(i)) || skip_cpu(i, env))
3164 continue;
3165
3166 update_spare_capacity(stats, env, i, c->capacity,
3167 env->cpu_load);
3168
3169 if (env->boost_type == SCHED_BOOST_ON_ALL ||
3170 env->need_waker_cluster ||
3171 sched_cpu_high_irqload(i) ||
3172 spill_threshold_crossed(env, cpu_rq(i)))
3173 continue;
3174
3175 update_cluster_stats(i, stats, env);
3176 }
3177}
3178
3179static inline void init_cluster_cpu_stats(struct cluster_cpu_stats *stats)
3180{
3181 stats->best_cpu = stats->best_idle_cpu = -1;
3182 stats->best_capacity_cpu = stats->best_sibling_cpu = -1;
3183 stats->min_cost = stats->best_sibling_cpu_cost = INT_MAX;
3184 stats->min_load = stats->best_sibling_cpu_load = ULLONG_MAX;
3185 stats->highest_spare_capacity = 0;
3186 stats->least_loaded_cpu = -1;
3187 stats->best_cpu_cstate = INT_MAX;
3188 /* No need to initialize stats->best_load */
3189}
3190
3191/*
3192 * Should task be woken to any available idle cpu?
3193 *
3194 * Waking tasks to idle cpu has mixed implications on both performance and
3195 * power. In many cases, scheduler can't estimate correctly impact of using idle
3196 * cpus on either performance or power. PF_WAKE_UP_IDLE allows external kernel
3197 * module to pass a strong hint to scheduler that the task in question should be
3198 * woken to idle cpu, generally to improve performance.
3199 */
3200static inline int wake_to_idle(struct task_struct *p)
3201{
3202 return (current->flags & PF_WAKE_UP_IDLE) ||
3203 (p->flags & PF_WAKE_UP_IDLE) || sysctl_sched_wake_to_idle;
3204}
3205
3206static inline bool
3207bias_to_prev_cpu(struct cpu_select_env *env, struct cluster_cpu_stats *stats)
3208{
3209 int prev_cpu;
3210 struct task_struct *task = env->p;
3211 struct sched_cluster *cluster;
3212
3213 if (env->boost_type != SCHED_BOOST_NONE || env->reason ||
3214 !task->ravg.mark_start ||
3215 env->need_idle || !sched_short_sleep_task_threshold)
3216 return false;
3217
3218 prev_cpu = env->prev_cpu;
3219 if (!cpumask_test_cpu(prev_cpu, tsk_cpus_allowed(task)) ||
3220 unlikely(!cpu_active(prev_cpu)))
3221 return false;
3222
3223 if (task->ravg.mark_start - task->last_cpu_selected_ts >=
3224 sched_long_cpu_selection_threshold)
3225 return false;
3226
3227 /*
3228 * This function should be used by task wake up path only as it's
3229 * assuming p->last_switch_out_ts as last sleep time.
3230 * p->last_switch_out_ts can denote last preemption time as well as
3231 * last sleep time.
3232 */
3233 if (task->ravg.mark_start - task->last_switch_out_ts >=
3234 sched_short_sleep_task_threshold)
3235 return false;
3236
3237 env->task_load = scale_load_to_cpu(task_load(task), prev_cpu);
3238 cluster = cpu_rq(prev_cpu)->cluster;
3239
3240 if (!task_load_will_fit(task, env->task_load, prev_cpu,
3241 sched_boost_type())) {
3242
3243 __set_bit(cluster->id, env->backup_list);
3244 __clear_bit(cluster->id, env->candidate_list);
3245 return false;
3246 }
3247
3248 env->cpu_load = cpu_load_sync(prev_cpu, env->sync);
3249 if (sched_cpu_high_irqload(prev_cpu) ||
3250 spill_threshold_crossed(env, cpu_rq(prev_cpu))) {
3251 update_spare_capacity(stats, env, prev_cpu,
3252 cluster->capacity, env->cpu_load);
3253 env->ignore_prev_cpu = 1;
3254 return false;
3255 }
3256
3257 return true;
3258}
3259
3260static inline bool
3261wake_to_waker_cluster(struct cpu_select_env *env)
3262{
3263 return !env->need_idle && !env->reason && env->sync &&
3264 task_load(current) > sched_big_waker_task_load &&
3265 task_load(env->p) < sched_small_wakee_task_load;
3266}
3267
3268static inline int
3269cluster_allowed(struct task_struct *p, struct sched_cluster *cluster)
3270{
3271 cpumask_t tmp_mask;
3272
3273 cpumask_and(&tmp_mask, &cluster->cpus, cpu_active_mask);
3274 cpumask_and(&tmp_mask, &tmp_mask, &p->cpus_allowed);
3275
3276 return !cpumask_empty(&tmp_mask);
3277}
3278
3279
3280/* return cheapest cpu that can fit this task */
3281static int select_best_cpu(struct task_struct *p, int target, int reason,
3282 int sync)
3283{
3284 struct sched_cluster *cluster, *pref_cluster = NULL;
3285 struct cluster_cpu_stats stats;
3286 struct related_thread_group *grp;
3287 unsigned int sbc_flag = 0;
3288
3289 struct cpu_select_env env = {
3290 .p = p,
3291 .reason = reason,
3292 .need_idle = wake_to_idle(p),
3293 .need_waker_cluster = 0,
3294 .boost_type = sched_boost_type(),
3295 .sync = sync,
3296 .prev_cpu = target,
3297 .ignore_prev_cpu = 0,
3298 .rtg = NULL,
3299 .sbc_best_flag = 0,
3300 .sbc_best_cluster_flag = 0,
3301 };
3302
3303 bitmap_copy(env.candidate_list, all_cluster_ids, NR_CPUS);
3304 bitmap_zero(env.backup_list, NR_CPUS);
3305
3306 init_cluster_cpu_stats(&stats);
3307
3308 rcu_read_lock();
3309
3310 grp = task_related_thread_group(p);
3311
3312 if (grp && grp->preferred_cluster) {
3313 pref_cluster = grp->preferred_cluster;
3314 if (!cluster_allowed(p, pref_cluster))
3315 clear_bit(pref_cluster->id, env.candidate_list);
3316 else
3317 env.rtg = grp;
3318 } else {
3319 cluster = cpu_rq(smp_processor_id())->cluster;
3320 if (wake_to_waker_cluster(&env) &&
3321 cluster_allowed(p, cluster)) {
3322 env.need_waker_cluster = 1;
3323 bitmap_zero(env.candidate_list, NR_CPUS);
3324 __set_bit(cluster->id, env.candidate_list);
3325 env.sbc_best_cluster_flag = SBC_FLAG_WAKER_CLUSTER;
3326
3327 } else if (bias_to_prev_cpu(&env, &stats)) {
3328 sbc_flag = SBC_FLAG_PREV_CPU;
3329 goto out;
3330 }
3331 }
3332
3333retry:
3334 cluster = select_least_power_cluster(&env);
3335
3336 if (!cluster)
3337 goto out;
3338
3339 /*
3340 * 'cluster' now points to the minimum power cluster which can satisfy
3341 * task's perf goals. Walk down the cluster list starting with that
3342 * cluster. For non-small tasks, skip clusters that don't have
3343 * mostly_idle/idle cpus
3344 */
3345
3346 do {
3347 find_best_cpu_in_cluster(cluster, &env, &stats);
3348
3349 } while ((cluster = next_best_cluster(cluster, &env, &stats)));
3350
3351 if (env.need_idle) {
3352 if (stats.best_idle_cpu >= 0) {
3353 target = stats.best_idle_cpu;
3354 sbc_flag |= SBC_FLAG_IDLE_CSTATE;
3355 } else if (stats.least_loaded_cpu >= 0) {
3356 target = stats.least_loaded_cpu;
3357 sbc_flag |= SBC_FLAG_IDLE_LEAST_LOADED;
3358 }
3359 } else if (stats.best_cpu >= 0) {
3360 if (stats.best_cpu != task_cpu(p) &&
3361 stats.min_cost == stats.best_sibling_cpu_cost) {
3362 stats.best_cpu = stats.best_sibling_cpu;
3363 sbc_flag |= SBC_FLAG_BEST_SIBLING;
3364 }
3365 sbc_flag |= env.sbc_best_flag;
3366 target = stats.best_cpu;
3367 } else {
3368 if (env.rtg) {
3369 env.rtg = NULL;
3370 goto retry;
3371 }
3372
3373 find_backup_cluster(&env, &stats);
3374 if (stats.best_capacity_cpu >= 0) {
3375 target = stats.best_capacity_cpu;
3376 sbc_flag |= SBC_FLAG_BEST_CAP_CPU;
3377 }
3378 }
3379 p->last_cpu_selected_ts = sched_ktime_clock();
3380 sbc_flag |= env.sbc_best_cluster_flag;
3381out:
3382 rcu_read_unlock();
3383 trace_sched_task_load(p, sched_boost(), env.reason, env.sync,
3384 env.need_idle, sbc_flag, target);
3385 return target;
3386}
3387
3388#ifdef CONFIG_CFS_BANDWIDTH
3389
3390static inline struct task_group *next_task_group(struct task_group *tg)
3391{
3392 tg = list_entry_rcu(tg->list.next, typeof(struct task_group), list);
3393
3394 return (&tg->list == &task_groups) ? NULL : tg;
3395}
3396
3397/* Iterate over all cfs_rq in a cpu */
3398#define for_each_cfs_rq(cfs_rq, tg, cpu) \
3399 for (tg = container_of(&task_groups, struct task_group, list); \
3400 ((tg = next_task_group(tg)) && (cfs_rq = tg->cfs_rq[cpu]));)
3401
3402void reset_cfs_rq_hmp_stats(int cpu, int reset_cra)
3403{
3404 struct task_group *tg;
3405 struct cfs_rq *cfs_rq;
3406
3407 rcu_read_lock();
3408
3409 for_each_cfs_rq(cfs_rq, tg, cpu)
3410 reset_hmp_stats(&cfs_rq->hmp_stats, reset_cra);
3411
3412 rcu_read_unlock();
3413}
3414
3415static inline int cfs_rq_throttled(struct cfs_rq *cfs_rq);
3416
3417static void inc_cfs_rq_hmp_stats(struct cfs_rq *cfs_rq,
3418 struct task_struct *p, int change_cra);
3419static void dec_cfs_rq_hmp_stats(struct cfs_rq *cfs_rq,
3420 struct task_struct *p, int change_cra);
3421
3422/* Add task's contribution to a cpu' HMP statistics */
3423void inc_hmp_sched_stats_fair(struct rq *rq,
3424 struct task_struct *p, int change_cra)
3425{
3426 struct cfs_rq *cfs_rq;
3427 struct sched_entity *se = &p->se;
3428
3429 /*
3430 * Although below check is not strictly required (as
3431 * inc/dec_nr_big_task and inc/dec_cumulative_runnable_avg called
3432 * from inc_cfs_rq_hmp_stats() have similar checks), we gain a bit on
3433 * efficiency by short-circuiting for_each_sched_entity() loop when
3434 * sched_disable_window_stats
3435 */
3436 if (sched_disable_window_stats)
3437 return;
3438
3439 for_each_sched_entity(se) {
3440 cfs_rq = cfs_rq_of(se);
3441 inc_cfs_rq_hmp_stats(cfs_rq, p, change_cra);
3442 if (cfs_rq_throttled(cfs_rq))
3443 break;
3444 }
3445
3446 /* Update rq->hmp_stats only if we didn't find any throttled cfs_rq */
3447 if (!se)
3448 inc_rq_hmp_stats(rq, p, change_cra);
3449}
3450
3451static void fixup_hmp_sched_stats_fair(struct rq *rq, struct task_struct *p,
3452 u32 new_task_load, u32 new_pred_demand)
3453{
3454 struct cfs_rq *cfs_rq;
3455 struct sched_entity *se = &p->se;
3456 s64 task_load_delta = (s64)new_task_load - task_load(p);
3457 s64 pred_demand_delta = PRED_DEMAND_DELTA;
3458
3459 for_each_sched_entity(se) {
3460 cfs_rq = cfs_rq_of(se);
3461
3462 fixup_cumulative_runnable_avg(&cfs_rq->hmp_stats, p,
3463 task_load_delta,
3464 pred_demand_delta);
3465 fixup_nr_big_tasks(&cfs_rq->hmp_stats, p, task_load_delta);
3466 if (cfs_rq_throttled(cfs_rq))
3467 break;
3468 }
3469
3470 /* Fix up rq->hmp_stats only if we didn't find any throttled cfs_rq */
3471 if (!se) {
3472 fixup_cumulative_runnable_avg(&rq->hmp_stats, p,
3473 task_load_delta,
3474 pred_demand_delta);
3475 fixup_nr_big_tasks(&rq->hmp_stats, p, task_load_delta);
3476 }
3477}
3478
3479static int task_will_be_throttled(struct task_struct *p);
3480
3481#else /* CONFIG_CFS_BANDWIDTH */
3482
3483inline void reset_cfs_rq_hmp_stats(int cpu, int reset_cra) { }
3484
3485static void
3486fixup_hmp_sched_stats_fair(struct rq *rq, struct task_struct *p,
3487 u32 new_task_load, u32 new_pred_demand)
3488{
3489 s64 task_load_delta = (s64)new_task_load - task_load(p);
3490 s64 pred_demand_delta = PRED_DEMAND_DELTA;
3491
3492 fixup_cumulative_runnable_avg(&rq->hmp_stats, p, task_load_delta,
3493 pred_demand_delta);
3494 fixup_nr_big_tasks(&rq->hmp_stats, p, task_load_delta);
3495}
3496
3497static inline int task_will_be_throttled(struct task_struct *p)
3498{
3499 return 0;
3500}
3501
3502void inc_hmp_sched_stats_fair(struct rq *rq,
3503 struct task_struct *p, int change_cra)
3504{
3505 inc_nr_big_task(&rq->hmp_stats, p);
3506}
3507
3508#endif /* CONFIG_CFS_BANDWIDTH */
3509
3510/*
3511 * Reset balance_interval at all sched_domain levels of given cpu, so that it
3512 * honors kick.
3513 */
3514static inline void reset_balance_interval(int cpu)
3515{
3516 struct sched_domain *sd;
3517
3518 if (cpu >= nr_cpu_ids)
3519 return;
3520
3521 rcu_read_lock();
3522 for_each_domain(cpu, sd)
3523 sd->balance_interval = 0;
3524 rcu_read_unlock();
3525}
3526
3527/*
3528 * Check if a task is on the "wrong" cpu (i.e its current cpu is not the ideal
3529 * cpu as per its demand or priority)
3530 *
3531 * Returns reason why task needs to be migrated
3532 */
3533static inline int migration_needed(struct task_struct *p, int cpu)
3534{
3535 int nice;
3536 struct related_thread_group *grp;
3537
3538 if (p->state != TASK_RUNNING || p->nr_cpus_allowed == 1)
3539 return 0;
3540
3541 /* No need to migrate task that is about to be throttled */
3542 if (task_will_be_throttled(p))
3543 return 0;
3544
3545 if (sched_boost_type() == SCHED_BOOST_ON_BIG) {
3546 if (cpu_capacity(cpu) != max_capacity)
3547 return UP_MIGRATION;
3548 return 0;
3549 }
3550
3551 if (sched_cpu_high_irqload(cpu))
3552 return IRQLOAD_MIGRATION;
3553
3554 nice = task_nice(p);
3555 rcu_read_lock();
3556 grp = task_related_thread_group(p);
3557 if (!grp && (nice > SCHED_UPMIGRATE_MIN_NICE ||
3558 upmigrate_discouraged(p)) && cpu_capacity(cpu) > min_capacity) {
3559 rcu_read_unlock();
3560 return DOWN_MIGRATION;
3561 }
3562
3563 if (!grp && !task_will_fit(p, cpu)) {
3564 rcu_read_unlock();
3565 return UP_MIGRATION;
3566 }
3567 rcu_read_unlock();
3568
3569 return 0;
3570}
3571
3572static inline int
3573kick_active_balance(struct rq *rq, struct task_struct *p, int new_cpu)
3574{
3575 unsigned long flags;
3576 int rc = 0;
3577
3578 /* Invoke active balance to force migrate currently running task */
3579 raw_spin_lock_irqsave(&rq->lock, flags);
3580 if (!rq->active_balance) {
3581 rq->active_balance = 1;
3582 rq->push_cpu = new_cpu;
3583 get_task_struct(p);
3584 rq->push_task = p;
3585 rc = 1;
3586 }
3587 raw_spin_unlock_irqrestore(&rq->lock, flags);
3588
3589 return rc;
3590}
3591
3592static DEFINE_RAW_SPINLOCK(migration_lock);
3593
3594/*
3595 * Check if currently running task should be migrated to a better cpu.
3596 *
3597 * Todo: Effect this via changes to nohz_balancer_kick() and load balance?
3598 */
3599void check_for_migration(struct rq *rq, struct task_struct *p)
3600{
3601 int cpu = cpu_of(rq), new_cpu;
3602 int active_balance = 0, reason;
3603
3604 reason = migration_needed(p, cpu);
3605 if (!reason)
3606 return;
3607
3608 raw_spin_lock(&migration_lock);
3609 new_cpu = select_best_cpu(p, cpu, reason, 0);
3610
3611 if (new_cpu != cpu) {
3612 active_balance = kick_active_balance(rq, p, new_cpu);
3613 if (active_balance)
3614 mark_reserved(new_cpu);
3615 }
3616
3617 raw_spin_unlock(&migration_lock);
3618
3619 if (active_balance)
3620 stop_one_cpu_nowait(cpu, active_load_balance_cpu_stop, rq,
3621 &rq->active_balance_work);
3622}
3623
3624#ifdef CONFIG_CFS_BANDWIDTH
3625
3626static void init_cfs_rq_hmp_stats(struct cfs_rq *cfs_rq)
3627{
3628 cfs_rq->hmp_stats.nr_big_tasks = 0;
3629 cfs_rq->hmp_stats.cumulative_runnable_avg = 0;
3630 cfs_rq->hmp_stats.pred_demands_sum = 0;
3631}
3632
3633static void inc_cfs_rq_hmp_stats(struct cfs_rq *cfs_rq,
3634 struct task_struct *p, int change_cra)
3635{
3636 inc_nr_big_task(&cfs_rq->hmp_stats, p);
3637 if (change_cra)
3638 inc_cumulative_runnable_avg(&cfs_rq->hmp_stats, p);
3639}
3640
3641static void dec_cfs_rq_hmp_stats(struct cfs_rq *cfs_rq,
3642 struct task_struct *p, int change_cra)
3643{
3644 dec_nr_big_task(&cfs_rq->hmp_stats, p);
3645 if (change_cra)
3646 dec_cumulative_runnable_avg(&cfs_rq->hmp_stats, p);
3647}
3648
3649static void inc_throttled_cfs_rq_hmp_stats(struct hmp_sched_stats *stats,
3650 struct cfs_rq *cfs_rq)
3651{
3652 stats->nr_big_tasks += cfs_rq->hmp_stats.nr_big_tasks;
3653 stats->cumulative_runnable_avg +=
3654 cfs_rq->hmp_stats.cumulative_runnable_avg;
3655 stats->pred_demands_sum += cfs_rq->hmp_stats.pred_demands_sum;
3656}
3657
3658static void dec_throttled_cfs_rq_hmp_stats(struct hmp_sched_stats *stats,
3659 struct cfs_rq *cfs_rq)
3660{
3661 stats->nr_big_tasks -= cfs_rq->hmp_stats.nr_big_tasks;
3662 stats->cumulative_runnable_avg -=
3663 cfs_rq->hmp_stats.cumulative_runnable_avg;
3664 stats->pred_demands_sum -= cfs_rq->hmp_stats.pred_demands_sum;
3665
3666 BUG_ON(stats->nr_big_tasks < 0 ||
3667 (s64)stats->cumulative_runnable_avg < 0);
3668 verify_pred_demands_sum(stats);
3669}
3670
3671#else /* CONFIG_CFS_BANDWIDTH */
3672
3673static inline void inc_cfs_rq_hmp_stats(struct cfs_rq *cfs_rq,
3674 struct task_struct *p, int change_cra) { }
3675
3676static inline void dec_cfs_rq_hmp_stats(struct cfs_rq *cfs_rq,
3677 struct task_struct *p, int change_cra) { }
3678
3679#endif /* CONFIG_CFS_BANDWIDTH */
3680
3681#else /* CONFIG_SCHED_HMP */
3682
3683static inline void init_cfs_rq_hmp_stats(struct cfs_rq *cfs_rq) { }
3684
3685static inline void inc_cfs_rq_hmp_stats(struct cfs_rq *cfs_rq,
3686 struct task_struct *p, int change_cra) { }
3687
3688static inline void dec_cfs_rq_hmp_stats(struct cfs_rq *cfs_rq,
3689 struct task_struct *p, int change_cra) { }
3690
3691static inline void inc_throttled_cfs_rq_hmp_stats(struct hmp_sched_stats *stats,
3692 struct cfs_rq *cfs_rq)
3693{
3694}
3695
3696static inline void dec_throttled_cfs_rq_hmp_stats(struct hmp_sched_stats *stats,
3697 struct cfs_rq *cfs_rq)
3698{
3699}
3700#endif /* CONFIG_SCHED_HMP */
3701
Peter Zijlstra54a21382015-09-07 15:05:42 +02003702#define cap_scale(v, s) ((v)*(s) >> SCHED_CAPACITY_SHIFT)
Dietmar Eggemanne0f5f3a2015-08-14 17:23:09 +01003703
Paul Turner9d85f212012-10-04 13:18:29 +02003704/*
3705 * We can represent the historical contribution to runnable average as the
3706 * coefficients of a geometric series. To do this we sub-divide our runnable
3707 * history into segments of approximately 1ms (1024us); label the segment that
3708 * occurred N-ms ago p_N, with p_0 corresponding to the current period, e.g.
3709 *
3710 * [<- 1024us ->|<- 1024us ->|<- 1024us ->| ...
3711 * p0 p1 p2
3712 * (now) (~1ms ago) (~2ms ago)
3713 *
3714 * Let u_i denote the fraction of p_i that the entity was runnable.
3715 *
3716 * We then designate the fractions u_i as our co-efficients, yielding the
3717 * following representation of historical load:
3718 * u_0 + u_1*y + u_2*y^2 + u_3*y^3 + ...
3719 *
3720 * We choose y based on the with of a reasonably scheduling period, fixing:
3721 * y^32 = 0.5
3722 *
3723 * This means that the contribution to load ~32ms ago (u_32) will be weighted
3724 * approximately half as much as the contribution to load within the last ms
3725 * (u_0).
3726 *
3727 * When a period "rolls over" and we have new u_0`, multiplying the previous
3728 * sum again by y is sufficient to update:
3729 * load_avg = u_0` + y*(u_0 + u_1*y + u_2*y^2 + ... )
3730 * = u_0 + u_1*y + u_2*y^2 + ... [re-labeling u_i --> u_{i+1}]
3731 */
Yuyang Du9d89c252015-07-15 08:04:37 +08003732static __always_inline int
3733__update_load_avg(u64 now, int cpu, struct sched_avg *sa,
Yuyang Du13962232015-07-15 08:04:41 +08003734 unsigned long weight, int running, struct cfs_rq *cfs_rq)
Paul Turner9d85f212012-10-04 13:18:29 +02003735{
Dietmar Eggemanne0f5f3a2015-08-14 17:23:09 +01003736 u64 delta, scaled_delta, periods;
Yuyang Du9d89c252015-07-15 08:04:37 +08003737 u32 contrib;
Peter Zijlstra6115c792015-09-07 15:09:15 +02003738 unsigned int delta_w, scaled_delta_w, decayed = 0;
Dietmar Eggemann6f2b0452015-09-07 14:57:22 +01003739 unsigned long scale_freq, scale_cpu;
Paul Turner9d85f212012-10-04 13:18:29 +02003740
Yuyang Du9d89c252015-07-15 08:04:37 +08003741 delta = now - sa->last_update_time;
Paul Turner9d85f212012-10-04 13:18:29 +02003742 /*
3743 * This should only happen when time goes backwards, which it
3744 * unfortunately does during sched clock init when we swap over to TSC.
3745 */
3746 if ((s64)delta < 0) {
Yuyang Du9d89c252015-07-15 08:04:37 +08003747 sa->last_update_time = now;
Paul Turner9d85f212012-10-04 13:18:29 +02003748 return 0;
3749 }
3750
3751 /*
3752 * Use 1024ns as the unit of measurement since it's a reasonable
3753 * approximation of 1us and fast to compute.
3754 */
3755 delta >>= 10;
3756 if (!delta)
3757 return 0;
Yuyang Du9d89c252015-07-15 08:04:37 +08003758 sa->last_update_time = now;
Paul Turner9d85f212012-10-04 13:18:29 +02003759
Dietmar Eggemann6f2b0452015-09-07 14:57:22 +01003760 scale_freq = arch_scale_freq_capacity(NULL, cpu);
3761 scale_cpu = arch_scale_cpu_capacity(NULL, cpu);
3762
Paul Turner9d85f212012-10-04 13:18:29 +02003763 /* delta_w is the amount already accumulated against our next period */
Yuyang Du9d89c252015-07-15 08:04:37 +08003764 delta_w = sa->period_contrib;
Paul Turner9d85f212012-10-04 13:18:29 +02003765 if (delta + delta_w >= 1024) {
Paul Turner9d85f212012-10-04 13:18:29 +02003766 decayed = 1;
3767
Yuyang Du9d89c252015-07-15 08:04:37 +08003768 /* how much left for next period will start over, we don't know yet */
3769 sa->period_contrib = 0;
3770
Paul Turner9d85f212012-10-04 13:18:29 +02003771 /*
3772 * Now that we know we're crossing a period boundary, figure
3773 * out how much from delta we need to complete the current
3774 * period and accrue it.
3775 */
3776 delta_w = 1024 - delta_w;
Peter Zijlstra54a21382015-09-07 15:05:42 +02003777 scaled_delta_w = cap_scale(delta_w, scale_freq);
Yuyang Du13962232015-07-15 08:04:41 +08003778 if (weight) {
Dietmar Eggemanne0f5f3a2015-08-14 17:23:09 +01003779 sa->load_sum += weight * scaled_delta_w;
3780 if (cfs_rq) {
3781 cfs_rq->runnable_load_sum +=
3782 weight * scaled_delta_w;
3783 }
Yuyang Du13962232015-07-15 08:04:41 +08003784 }
Vincent Guittot36ee28e2015-02-27 16:54:04 +01003785 if (running)
Peter Zijlstra006cdf02015-09-09 09:06:17 +02003786 sa->util_sum += scaled_delta_w * scale_cpu;
Paul Turner9d85f212012-10-04 13:18:29 +02003787
Paul Turner5b51f2f2012-10-04 13:18:32 +02003788 delta -= delta_w;
Paul Turner9d85f212012-10-04 13:18:29 +02003789
Paul Turner5b51f2f2012-10-04 13:18:32 +02003790 /* Figure out how many additional periods this update spans */
3791 periods = delta / 1024;
3792 delta %= 1024;
3793
Yuyang Du9d89c252015-07-15 08:04:37 +08003794 sa->load_sum = decay_load(sa->load_sum, periods + 1);
Yuyang Du13962232015-07-15 08:04:41 +08003795 if (cfs_rq) {
3796 cfs_rq->runnable_load_sum =
3797 decay_load(cfs_rq->runnable_load_sum, periods + 1);
3798 }
Yuyang Du9d89c252015-07-15 08:04:37 +08003799 sa->util_sum = decay_load((u64)(sa->util_sum), periods + 1);
Paul Turner5b51f2f2012-10-04 13:18:32 +02003800
3801 /* Efficiently calculate \sum (1..n_period) 1024*y^i */
Yuyang Du9d89c252015-07-15 08:04:37 +08003802 contrib = __compute_runnable_contrib(periods);
Peter Zijlstra54a21382015-09-07 15:05:42 +02003803 contrib = cap_scale(contrib, scale_freq);
Yuyang Du13962232015-07-15 08:04:41 +08003804 if (weight) {
Yuyang Du9d89c252015-07-15 08:04:37 +08003805 sa->load_sum += weight * contrib;
Yuyang Du13962232015-07-15 08:04:41 +08003806 if (cfs_rq)
3807 cfs_rq->runnable_load_sum += weight * contrib;
3808 }
Vincent Guittot36ee28e2015-02-27 16:54:04 +01003809 if (running)
Peter Zijlstra006cdf02015-09-09 09:06:17 +02003810 sa->util_sum += contrib * scale_cpu;
Paul Turner9d85f212012-10-04 13:18:29 +02003811 }
3812
3813 /* Remainder of delta accrued against u_0` */
Peter Zijlstra54a21382015-09-07 15:05:42 +02003814 scaled_delta = cap_scale(delta, scale_freq);
Yuyang Du13962232015-07-15 08:04:41 +08003815 if (weight) {
Dietmar Eggemanne0f5f3a2015-08-14 17:23:09 +01003816 sa->load_sum += weight * scaled_delta;
Yuyang Du13962232015-07-15 08:04:41 +08003817 if (cfs_rq)
Dietmar Eggemanne0f5f3a2015-08-14 17:23:09 +01003818 cfs_rq->runnable_load_sum += weight * scaled_delta;
Yuyang Du13962232015-07-15 08:04:41 +08003819 }
Vincent Guittot36ee28e2015-02-27 16:54:04 +01003820 if (running)
Peter Zijlstra006cdf02015-09-09 09:06:17 +02003821 sa->util_sum += scaled_delta * scale_cpu;
Yuyang Du9d89c252015-07-15 08:04:37 +08003822
3823 sa->period_contrib += delta;
3824
3825 if (decayed) {
3826 sa->load_avg = div_u64(sa->load_sum, LOAD_AVG_MAX);
Yuyang Du13962232015-07-15 08:04:41 +08003827 if (cfs_rq) {
3828 cfs_rq->runnable_load_avg =
3829 div_u64(cfs_rq->runnable_load_sum, LOAD_AVG_MAX);
3830 }
Peter Zijlstra006cdf02015-09-09 09:06:17 +02003831 sa->util_avg = sa->util_sum / LOAD_AVG_MAX;
Yuyang Du9d89c252015-07-15 08:04:37 +08003832 }
Paul Turner9d85f212012-10-04 13:18:29 +02003833
3834 return decayed;
3835}
3836
Paul Turnerc566e8e2012-10-04 13:18:30 +02003837#ifdef CONFIG_FAIR_GROUP_SCHED
Peter Zijlstra7c3edd22016-07-13 10:56:25 +02003838/**
3839 * update_tg_load_avg - update the tg's load avg
3840 * @cfs_rq: the cfs_rq whose avg changed
3841 * @force: update regardless of how small the difference
3842 *
3843 * This function 'ensures': tg->load_avg := \Sum tg->cfs_rq[]->avg.load.
3844 * However, because tg->load_avg is a global value there are performance
3845 * considerations.
3846 *
3847 * In order to avoid having to look at the other cfs_rq's, we use a
3848 * differential update where we store the last value we propagated. This in
3849 * turn allows skipping updates if the differential is 'small'.
3850 *
3851 * Updating tg's load_avg is necessary before update_cfs_share() (which is
3852 * done) and effective_load() (which is not done because it is too costly).
Paul Turnerbb17f652012-10-04 13:18:31 +02003853 */
Yuyang Du9d89c252015-07-15 08:04:37 +08003854static inline void update_tg_load_avg(struct cfs_rq *cfs_rq, int force)
Paul Turnerbb17f652012-10-04 13:18:31 +02003855{
Yuyang Du9d89c252015-07-15 08:04:37 +08003856 long delta = cfs_rq->avg.load_avg - cfs_rq->tg_load_avg_contrib;
Paul Turnerbb17f652012-10-04 13:18:31 +02003857
Waiman Longaa0b7ae2015-12-02 13:41:50 -05003858 /*
3859 * No need to update load_avg for root_task_group as it is not used.
3860 */
3861 if (cfs_rq->tg == &root_task_group)
3862 return;
3863
Yuyang Du9d89c252015-07-15 08:04:37 +08003864 if (force || abs(delta) > cfs_rq->tg_load_avg_contrib / 64) {
3865 atomic_long_add(delta, &cfs_rq->tg->load_avg);
3866 cfs_rq->tg_load_avg_contrib = cfs_rq->avg.load_avg;
Paul Turnerbb17f652012-10-04 13:18:31 +02003867 }
Paul Turner8165e142012-10-04 13:18:31 +02003868}
Dietmar Eggemannf5f97392014-02-26 11:19:33 +00003869
Byungchul Parkad936d82015-10-24 01:16:19 +09003870/*
3871 * Called within set_task_rq() right before setting a task's cpu. The
3872 * caller only guarantees p->pi_lock is held; no other assumptions,
3873 * including the state of rq->lock, should be made.
3874 */
3875void set_task_rq_fair(struct sched_entity *se,
3876 struct cfs_rq *prev, struct cfs_rq *next)
3877{
3878 if (!sched_feat(ATTACH_AGE_LOAD))
3879 return;
3880
3881 /*
3882 * We are supposed to update the task to "current" time, then its up to
3883 * date and ready to go to new CPU/cfs_rq. But we have difficulty in
3884 * getting what current time is, so simply throw away the out-of-date
3885 * time. This will result in the wakee task is less decayed, but giving
3886 * the wakee more load sounds not bad.
3887 */
3888 if (se->avg.last_update_time && prev) {
3889 u64 p_last_update_time;
3890 u64 n_last_update_time;
3891
3892#ifndef CONFIG_64BIT
3893 u64 p_last_update_time_copy;
3894 u64 n_last_update_time_copy;
3895
3896 do {
3897 p_last_update_time_copy = prev->load_last_update_time_copy;
3898 n_last_update_time_copy = next->load_last_update_time_copy;
3899
3900 smp_rmb();
3901
3902 p_last_update_time = prev->avg.last_update_time;
3903 n_last_update_time = next->avg.last_update_time;
3904
3905 } while (p_last_update_time != p_last_update_time_copy ||
3906 n_last_update_time != n_last_update_time_copy);
3907#else
3908 p_last_update_time = prev->avg.last_update_time;
3909 n_last_update_time = next->avg.last_update_time;
3910#endif
3911 __update_load_avg(p_last_update_time, cpu_of(rq_of(prev)),
3912 &se->avg, 0, 0, NULL);
3913 se->avg.last_update_time = n_last_update_time;
3914 }
3915}
Peter Zijlstra6e831252014-02-11 16:11:48 +01003916#else /* CONFIG_FAIR_GROUP_SCHED */
Yuyang Du9d89c252015-07-15 08:04:37 +08003917static inline void update_tg_load_avg(struct cfs_rq *cfs_rq, int force) {}
Peter Zijlstra6e831252014-02-11 16:11:48 +01003918#endif /* CONFIG_FAIR_GROUP_SCHED */
Paul Turnerc566e8e2012-10-04 13:18:30 +02003919
Steve Mucklea2c6c912016-03-24 15:26:07 -07003920static inline void cfs_rq_util_change(struct cfs_rq *cfs_rq)
Yuyang Du9d89c252015-07-15 08:04:37 +08003921{
Rafael J. Wysocki58919e82016-08-16 22:14:55 +02003922 if (&this_rq()->cfs == cfs_rq) {
Steve Muckle21e96f82016-03-21 17:21:07 -07003923 /*
3924 * There are a few boundary cases this might miss but it should
3925 * get called often enough that that should (hopefully) not be
3926 * a real problem -- added to that it only calls on the local
3927 * CPU, so if we enqueue remotely we'll miss an update, but
3928 * the next tick/schedule should update.
3929 *
3930 * It will not get called when we go idle, because the idle
3931 * thread is a different class (!fair), nor will the utilization
3932 * number include things like RT tasks.
3933 *
3934 * As is, the util number is not freq-invariant (we'd have to
3935 * implement arch_scale_freq_capacity() for that).
3936 *
3937 * See cpu_util().
3938 */
Rafael J. Wysocki12bde332016-08-10 03:11:17 +02003939 cpufreq_update_util(rq_of(cfs_rq), 0);
Steve Muckle21e96f82016-03-21 17:21:07 -07003940 }
Steve Mucklea2c6c912016-03-24 15:26:07 -07003941}
3942
Peter Zijlstra89741892016-06-16 10:50:40 +02003943/*
3944 * Unsigned subtract and clamp on underflow.
3945 *
3946 * Explicitly do a load-store to ensure the intermediate value never hits
3947 * memory. This allows lockless observations without ever seeing the negative
3948 * values.
3949 */
3950#define sub_positive(_ptr, _val) do { \
3951 typeof(_ptr) ptr = (_ptr); \
3952 typeof(*ptr) val = (_val); \
3953 typeof(*ptr) res, var = READ_ONCE(*ptr); \
3954 res = var - val; \
3955 if (res > var) \
3956 res = 0; \
3957 WRITE_ONCE(*ptr, res); \
3958} while (0)
3959
Peter Zijlstra3d30544f2016-06-21 14:27:50 +02003960/**
3961 * update_cfs_rq_load_avg - update the cfs_rq's load/util averages
3962 * @now: current time, as per cfs_rq_clock_task()
3963 * @cfs_rq: cfs_rq to update
3964 * @update_freq: should we call cfs_rq_util_change() or will the call do so
3965 *
3966 * The cfs_rq avg is the direct sum of all its entities (blocked and runnable)
3967 * avg. The immediate corollary is that all (fair) tasks must be attached, see
3968 * post_init_entity_util_avg().
3969 *
3970 * cfs_rq->avg is used for task_h_load() and update_cfs_share() for example.
3971 *
Peter Zijlstra7c3edd22016-07-13 10:56:25 +02003972 * Returns true if the load decayed or we removed load.
3973 *
3974 * Since both these conditions indicate a changed cfs_rq->avg.load we should
3975 * call update_tg_load_avg() when this function returns true.
Peter Zijlstra3d30544f2016-06-21 14:27:50 +02003976 */
Steve Mucklea2c6c912016-03-24 15:26:07 -07003977static inline int
3978update_cfs_rq_load_avg(u64 now, struct cfs_rq *cfs_rq, bool update_freq)
3979{
3980 struct sched_avg *sa = &cfs_rq->avg;
3981 int decayed, removed_load = 0, removed_util = 0;
3982
3983 if (atomic_long_read(&cfs_rq->removed_load_avg)) {
3984 s64 r = atomic_long_xchg(&cfs_rq->removed_load_avg, 0);
Peter Zijlstra89741892016-06-16 10:50:40 +02003985 sub_positive(&sa->load_avg, r);
3986 sub_positive(&sa->load_sum, r * LOAD_AVG_MAX);
Steve Mucklea2c6c912016-03-24 15:26:07 -07003987 removed_load = 1;
3988 }
3989
3990 if (atomic_long_read(&cfs_rq->removed_util_avg)) {
3991 long r = atomic_long_xchg(&cfs_rq->removed_util_avg, 0);
Peter Zijlstra89741892016-06-16 10:50:40 +02003992 sub_positive(&sa->util_avg, r);
3993 sub_positive(&sa->util_sum, r * LOAD_AVG_MAX);
Steve Mucklea2c6c912016-03-24 15:26:07 -07003994 removed_util = 1;
3995 }
3996
3997 decayed = __update_load_avg(now, cpu_of(rq_of(cfs_rq)), sa,
3998 scale_load_down(cfs_rq->load.weight), cfs_rq->curr != NULL, cfs_rq);
3999
4000#ifndef CONFIG_64BIT
4001 smp_wmb();
4002 cfs_rq->load_last_update_time_copy = sa->last_update_time;
4003#endif
4004
4005 if (update_freq && (decayed || removed_util))
4006 cfs_rq_util_change(cfs_rq);
Steve Muckle21e96f82016-03-21 17:21:07 -07004007
Steve Muckle41e0d372016-03-21 17:21:08 -07004008 return decayed || removed_load;
Yuyang Du9d89c252015-07-15 08:04:37 +08004009}
4010
4011/* Update task and its cfs_rq load average */
4012static inline void update_load_avg(struct sched_entity *se, int update_tg)
Paul Turner9d85f212012-10-04 13:18:29 +02004013{
Paul Turner2dac7542012-10-04 13:18:30 +02004014 struct cfs_rq *cfs_rq = cfs_rq_of(se);
Yuyang Du9d89c252015-07-15 08:04:37 +08004015 u64 now = cfs_rq_clock_task(cfs_rq);
Rafael J. Wysocki34e2c552016-02-15 20:20:42 +01004016 struct rq *rq = rq_of(cfs_rq);
4017 int cpu = cpu_of(rq);
Paul Turner2dac7542012-10-04 13:18:30 +02004018
Paul Turnerf1b17282012-10-04 13:18:31 +02004019 /*
Yuyang Du9d89c252015-07-15 08:04:37 +08004020 * Track task load average for carrying it to new CPU after migrated, and
4021 * track group sched_entity load average for task_h_load calc in migration
Paul Turnerf1b17282012-10-04 13:18:31 +02004022 */
Yuyang Du9d89c252015-07-15 08:04:37 +08004023 __update_load_avg(now, cpu, &se->avg,
Byungchul Parka05e8c52015-08-20 20:21:56 +09004024 se->on_rq * scale_load_down(se->load.weight),
4025 cfs_rq->curr == se, NULL);
Paul Turnerf1b17282012-10-04 13:18:31 +02004026
Steve Mucklea2c6c912016-03-24 15:26:07 -07004027 if (update_cfs_rq_load_avg(now, cfs_rq, true) && update_tg)
Yuyang Du9d89c252015-07-15 08:04:37 +08004028 update_tg_load_avg(cfs_rq, 0);
4029}
Paul Turner2dac7542012-10-04 13:18:30 +02004030
Peter Zijlstra3d30544f2016-06-21 14:27:50 +02004031/**
4032 * attach_entity_load_avg - attach this entity to its cfs_rq load avg
4033 * @cfs_rq: cfs_rq to attach to
4034 * @se: sched_entity to attach
4035 *
4036 * Must call update_cfs_rq_load_avg() before this, since we rely on
4037 * cfs_rq->avg.last_update_time being current.
4038 */
Byungchul Parka05e8c52015-08-20 20:21:56 +09004039static void attach_entity_load_avg(struct cfs_rq *cfs_rq, struct sched_entity *se)
4040{
Peter Zijlstraa9280512015-09-11 16:10:59 +02004041 if (!sched_feat(ATTACH_AGE_LOAD))
4042 goto skip_aging;
4043
Byungchul Park6efdb102015-08-20 20:21:59 +09004044 /*
4045 * If we got migrated (either between CPUs or between cgroups) we'll
4046 * have aged the average right before clearing @last_update_time.
Peter Zijlstra7dc603c2016-06-16 13:29:28 +02004047 *
4048 * Or we're fresh through post_init_entity_util_avg().
Byungchul Park6efdb102015-08-20 20:21:59 +09004049 */
4050 if (se->avg.last_update_time) {
4051 __update_load_avg(cfs_rq->avg.last_update_time, cpu_of(rq_of(cfs_rq)),
4052 &se->avg, 0, 0, NULL);
4053
4054 /*
4055 * XXX: we could have just aged the entire load away if we've been
4056 * absent from the fair class for too long.
4057 */
4058 }
4059
Peter Zijlstraa9280512015-09-11 16:10:59 +02004060skip_aging:
Byungchul Parka05e8c52015-08-20 20:21:56 +09004061 se->avg.last_update_time = cfs_rq->avg.last_update_time;
4062 cfs_rq->avg.load_avg += se->avg.load_avg;
4063 cfs_rq->avg.load_sum += se->avg.load_sum;
4064 cfs_rq->avg.util_avg += se->avg.util_avg;
4065 cfs_rq->avg.util_sum += se->avg.util_sum;
Steve Mucklea2c6c912016-03-24 15:26:07 -07004066
4067 cfs_rq_util_change(cfs_rq);
Byungchul Parka05e8c52015-08-20 20:21:56 +09004068}
4069
Peter Zijlstra3d30544f2016-06-21 14:27:50 +02004070/**
4071 * detach_entity_load_avg - detach this entity from its cfs_rq load avg
4072 * @cfs_rq: cfs_rq to detach from
4073 * @se: sched_entity to detach
4074 *
4075 * Must call update_cfs_rq_load_avg() before this, since we rely on
4076 * cfs_rq->avg.last_update_time being current.
4077 */
Byungchul Parka05e8c52015-08-20 20:21:56 +09004078static void detach_entity_load_avg(struct cfs_rq *cfs_rq, struct sched_entity *se)
4079{
4080 __update_load_avg(cfs_rq->avg.last_update_time, cpu_of(rq_of(cfs_rq)),
4081 &se->avg, se->on_rq * scale_load_down(se->load.weight),
4082 cfs_rq->curr == se, NULL);
4083
Peter Zijlstra89741892016-06-16 10:50:40 +02004084 sub_positive(&cfs_rq->avg.load_avg, se->avg.load_avg);
4085 sub_positive(&cfs_rq->avg.load_sum, se->avg.load_sum);
4086 sub_positive(&cfs_rq->avg.util_avg, se->avg.util_avg);
4087 sub_positive(&cfs_rq->avg.util_sum, se->avg.util_sum);
Steve Mucklea2c6c912016-03-24 15:26:07 -07004088
4089 cfs_rq_util_change(cfs_rq);
Byungchul Parka05e8c52015-08-20 20:21:56 +09004090}
4091
Yuyang Du9d89c252015-07-15 08:04:37 +08004092/* Add the load generated by se into cfs_rq's load average */
4093static inline void
4094enqueue_entity_load_avg(struct cfs_rq *cfs_rq, struct sched_entity *se)
4095{
4096 struct sched_avg *sa = &se->avg;
4097 u64 now = cfs_rq_clock_task(cfs_rq);
Byungchul Parka05e8c52015-08-20 20:21:56 +09004098 int migrated, decayed;
Paul Turner9ee474f2012-10-04 13:18:30 +02004099
Byungchul Parka05e8c52015-08-20 20:21:56 +09004100 migrated = !sa->last_update_time;
4101 if (!migrated) {
Yuyang Du9d89c252015-07-15 08:04:37 +08004102 __update_load_avg(now, cpu_of(rq_of(cfs_rq)), sa,
Yuyang Du13962232015-07-15 08:04:41 +08004103 se->on_rq * scale_load_down(se->load.weight),
4104 cfs_rq->curr == se, NULL);
Yuyang Du9d89c252015-07-15 08:04:37 +08004105 }
4106
Steve Mucklea2c6c912016-03-24 15:26:07 -07004107 decayed = update_cfs_rq_load_avg(now, cfs_rq, !migrated);
Yuyang Du9d89c252015-07-15 08:04:37 +08004108
Yuyang Du13962232015-07-15 08:04:41 +08004109 cfs_rq->runnable_load_avg += sa->load_avg;
4110 cfs_rq->runnable_load_sum += sa->load_sum;
4111
Byungchul Parka05e8c52015-08-20 20:21:56 +09004112 if (migrated)
4113 attach_entity_load_avg(cfs_rq, se);
Yuyang Du9d89c252015-07-15 08:04:37 +08004114
4115 if (decayed || migrated)
4116 update_tg_load_avg(cfs_rq, 0);
Paul Turner9ee474f2012-10-04 13:18:30 +02004117}
4118
Yuyang Du13962232015-07-15 08:04:41 +08004119/* Remove the runnable load generated by se from cfs_rq's runnable load average */
4120static inline void
4121dequeue_entity_load_avg(struct cfs_rq *cfs_rq, struct sched_entity *se)
4122{
4123 update_load_avg(se, 1);
4124
4125 cfs_rq->runnable_load_avg =
4126 max_t(long, cfs_rq->runnable_load_avg - se->avg.load_avg, 0);
4127 cfs_rq->runnable_load_sum =
Byungchul Parka05e8c52015-08-20 20:21:56 +09004128 max_t(s64, cfs_rq->runnable_load_sum - se->avg.load_sum, 0);
Yuyang Du13962232015-07-15 08:04:41 +08004129}
4130
Yuyang Du0905f042015-12-17 07:34:27 +08004131#ifndef CONFIG_64BIT
4132static inline u64 cfs_rq_last_update_time(struct cfs_rq *cfs_rq)
4133{
4134 u64 last_update_time_copy;
4135 u64 last_update_time;
4136
4137 do {
4138 last_update_time_copy = cfs_rq->load_last_update_time_copy;
4139 smp_rmb();
4140 last_update_time = cfs_rq->avg.last_update_time;
4141 } while (last_update_time != last_update_time_copy);
4142
4143 return last_update_time;
4144}
4145#else
4146static inline u64 cfs_rq_last_update_time(struct cfs_rq *cfs_rq)
4147{
4148 return cfs_rq->avg.last_update_time;
4149}
4150#endif
4151
Paul Turner9ee474f2012-10-04 13:18:30 +02004152/*
Yuyang Du9d89c252015-07-15 08:04:37 +08004153 * Task first catches up with cfs_rq, and then subtract
4154 * itself from the cfs_rq (task must be off the queue now).
Paul Turner9ee474f2012-10-04 13:18:30 +02004155 */
Yuyang Du9d89c252015-07-15 08:04:37 +08004156void remove_entity_load_avg(struct sched_entity *se)
Paul Turner9ee474f2012-10-04 13:18:30 +02004157{
Yuyang Du9d89c252015-07-15 08:04:37 +08004158 struct cfs_rq *cfs_rq = cfs_rq_of(se);
4159 u64 last_update_time;
Paul Turner9ee474f2012-10-04 13:18:30 +02004160
Yuyang Du0905f042015-12-17 07:34:27 +08004161 /*
Peter Zijlstra7dc603c2016-06-16 13:29:28 +02004162 * tasks cannot exit without having gone through wake_up_new_task() ->
4163 * post_init_entity_util_avg() which will have added things to the
4164 * cfs_rq, so we can remove unconditionally.
4165 *
4166 * Similarly for groups, they will have passed through
4167 * post_init_entity_util_avg() before unregister_sched_fair_group()
4168 * calls this.
Yuyang Du0905f042015-12-17 07:34:27 +08004169 */
Paul Turner9ee474f2012-10-04 13:18:30 +02004170
Yuyang Du0905f042015-12-17 07:34:27 +08004171 last_update_time = cfs_rq_last_update_time(cfs_rq);
Paul Turner9ee474f2012-10-04 13:18:30 +02004172
Yuyang Du13962232015-07-15 08:04:41 +08004173 __update_load_avg(last_update_time, cpu_of(rq_of(cfs_rq)), &se->avg, 0, 0, NULL);
Yuyang Du9d89c252015-07-15 08:04:37 +08004174 atomic_long_add(se->avg.load_avg, &cfs_rq->removed_load_avg);
4175 atomic_long_add(se->avg.util_avg, &cfs_rq->removed_util_avg);
Paul Turner2dac7542012-10-04 13:18:30 +02004176}
Vincent Guittot642dbc32013-04-18 18:34:26 +02004177
Yuyang Du7ea241a2015-07-15 08:04:42 +08004178static inline unsigned long cfs_rq_runnable_load_avg(struct cfs_rq *cfs_rq)
4179{
4180 return cfs_rq->runnable_load_avg;
4181}
4182
4183static inline unsigned long cfs_rq_load_avg(struct cfs_rq *cfs_rq)
4184{
4185 return cfs_rq->avg.load_avg;
4186}
4187
Peter Zijlstra6e831252014-02-11 16:11:48 +01004188static int idle_balance(struct rq *this_rq);
4189
Peter Zijlstra38033c32014-01-23 20:32:21 +01004190#else /* CONFIG_SMP */
4191
Peter Zijlstra01011472016-06-17 11:20:46 +02004192static inline int
4193update_cfs_rq_load_avg(u64 now, struct cfs_rq *cfs_rq, bool update_freq)
4194{
4195 return 0;
4196}
4197
Rafael J. Wysocki536bd002016-05-06 14:58:43 +02004198static inline void update_load_avg(struct sched_entity *se, int not_used)
4199{
Rafael J. Wysocki12bde332016-08-10 03:11:17 +02004200 cpufreq_update_util(rq_of(cfs_rq_of(se)), 0);
Rafael J. Wysocki536bd002016-05-06 14:58:43 +02004201}
4202
Yuyang Du9d89c252015-07-15 08:04:37 +08004203static inline void
4204enqueue_entity_load_avg(struct cfs_rq *cfs_rq, struct sched_entity *se) {}
Yuyang Du13962232015-07-15 08:04:41 +08004205static inline void
4206dequeue_entity_load_avg(struct cfs_rq *cfs_rq, struct sched_entity *se) {}
Yuyang Du9d89c252015-07-15 08:04:37 +08004207static inline void remove_entity_load_avg(struct sched_entity *se) {}
Peter Zijlstra6e831252014-02-11 16:11:48 +01004208
Byungchul Parka05e8c52015-08-20 20:21:56 +09004209static inline void
4210attach_entity_load_avg(struct cfs_rq *cfs_rq, struct sched_entity *se) {}
4211static inline void
4212detach_entity_load_avg(struct cfs_rq *cfs_rq, struct sched_entity *se) {}
4213
Peter Zijlstra6e831252014-02-11 16:11:48 +01004214static inline int idle_balance(struct rq *rq)
4215{
4216 return 0;
4217}
4218
Syed Rameez Mustafadddcab72016-09-07 16:18:27 -07004219static inline void inc_cfs_rq_hmp_stats(struct cfs_rq *cfs_rq,
4220 struct task_struct *p, int change_cra) { }
4221
4222static inline void dec_cfs_rq_hmp_stats(struct cfs_rq *cfs_rq,
4223 struct task_struct *p, int change_cra) { }
4224
Peter Zijlstra38033c32014-01-23 20:32:21 +01004225#endif /* CONFIG_SMP */
Paul Turner9d85f212012-10-04 13:18:29 +02004226
Peter Zijlstraddc97292007-10-15 17:00:10 +02004227static void check_spread(struct cfs_rq *cfs_rq, struct sched_entity *se)
4228{
4229#ifdef CONFIG_SCHED_DEBUG
4230 s64 d = se->vruntime - cfs_rq->min_vruntime;
4231
4232 if (d < 0)
4233 d = -d;
4234
4235 if (d > 3*sysctl_sched_latency)
Josh Poimboeufae928822016-06-17 12:43:24 -05004236 schedstat_inc(cfs_rq->nr_spread_over);
Peter Zijlstraddc97292007-10-15 17:00:10 +02004237#endif
4238}
4239
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02004240static void
Peter Zijlstraaeb73b02007-10-15 17:00:05 +02004241place_entity(struct cfs_rq *cfs_rq, struct sched_entity *se, int initial)
4242{
Peter Zijlstra1af5f732008-10-24 11:06:13 +02004243 u64 vruntime = cfs_rq->min_vruntime;
Peter Zijlstra94dfb5e2007-10-15 17:00:05 +02004244
Peter Zijlstra2cb86002007-11-09 22:39:37 +01004245 /*
4246 * The 'current' period is already promised to the current tasks,
4247 * however the extra weight of the new task will slow them down a
4248 * little, place the new task so that it fits in the slot that
4249 * stays open at the end.
4250 */
Peter Zijlstra94dfb5e2007-10-15 17:00:05 +02004251 if (initial && sched_feat(START_DEBIT))
Peter Zijlstraf9c0b092008-10-17 19:27:04 +02004252 vruntime += sched_vslice(cfs_rq, se);
Peter Zijlstraaeb73b02007-10-15 17:00:05 +02004253
Mike Galbraitha2e7a7e2009-09-18 09:19:25 +02004254 /* sleeps up to a single latency don't count. */
Mike Galbraith5ca98802010-03-11 17:17:17 +01004255 if (!initial) {
Mike Galbraitha2e7a7e2009-09-18 09:19:25 +02004256 unsigned long thresh = sysctl_sched_latency;
Peter Zijlstraa7be37a2008-06-27 13:41:11 +02004257
Mike Galbraitha2e7a7e2009-09-18 09:19:25 +02004258 /*
Mike Galbraitha2e7a7e2009-09-18 09:19:25 +02004259 * Halve their sleep time's effect, to allow
4260 * for a gentler effect of sleepers:
4261 */
4262 if (sched_feat(GENTLE_FAIR_SLEEPERS))
4263 thresh >>= 1;
Ingo Molnar51e03042009-09-16 08:54:45 +02004264
Mike Galbraitha2e7a7e2009-09-18 09:19:25 +02004265 vruntime -= thresh;
Peter Zijlstraaeb73b02007-10-15 17:00:05 +02004266 }
4267
Mike Galbraithb5d9d732009-09-08 11:12:28 +02004268 /* ensure we never gain time by being placed backwards. */
Viresh Kumar16c8f1c2012-11-08 13:33:46 +05304269 se->vruntime = max_vruntime(se->vruntime, vruntime);
Peter Zijlstraaeb73b02007-10-15 17:00:05 +02004270}
4271
Paul Turnerd3d9dc32011-07-21 09:43:39 -07004272static void check_enqueue_throttle(struct cfs_rq *cfs_rq);
4273
Mel Gormancb251762016-02-05 09:08:36 +00004274static inline void check_schedstat_required(void)
4275{
4276#ifdef CONFIG_SCHEDSTATS
4277 if (schedstat_enabled())
4278 return;
4279
4280 /* Force schedstat enabled if a dependent tracepoint is active */
4281 if (trace_sched_stat_wait_enabled() ||
4282 trace_sched_stat_sleep_enabled() ||
4283 trace_sched_stat_iowait_enabled() ||
4284 trace_sched_stat_blocked_enabled() ||
4285 trace_sched_stat_runtime_enabled()) {
Josh Poimboeufeda8dca2016-06-13 02:32:09 -05004286 printk_deferred_once("Scheduler tracepoints stat_sleep, stat_iowait, "
Mel Gormancb251762016-02-05 09:08:36 +00004287 "stat_blocked and stat_runtime require the "
4288 "kernel parameter schedstats=enabled or "
4289 "kernel.sched_schedstats=1\n");
4290 }
4291#endif
4292}
4293
Peter Zijlstrab5179ac2016-05-11 16:10:34 +02004294
4295/*
4296 * MIGRATION
4297 *
4298 * dequeue
4299 * update_curr()
4300 * update_min_vruntime()
4301 * vruntime -= min_vruntime
4302 *
4303 * enqueue
4304 * update_curr()
4305 * update_min_vruntime()
4306 * vruntime += min_vruntime
4307 *
4308 * this way the vruntime transition between RQs is done when both
4309 * min_vruntime are up-to-date.
4310 *
4311 * WAKEUP (remote)
4312 *
Peter Zijlstra59efa0b2016-05-10 18:24:37 +02004313 * ->migrate_task_rq_fair() (p->state == TASK_WAKING)
Peter Zijlstrab5179ac2016-05-11 16:10:34 +02004314 * vruntime -= min_vruntime
4315 *
4316 * enqueue
4317 * update_curr()
4318 * update_min_vruntime()
4319 * vruntime += min_vruntime
4320 *
4321 * this way we don't have the most up-to-date min_vruntime on the originating
4322 * CPU and an up-to-date min_vruntime on the destination CPU.
4323 */
4324
Peter Zijlstraaeb73b02007-10-15 17:00:05 +02004325static void
Peter Zijlstra88ec22d2009-12-16 18:04:41 +01004326enqueue_entity(struct cfs_rq *cfs_rq, struct sched_entity *se, int flags)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02004327{
Peter Zijlstra2f950352016-05-11 19:27:56 +02004328 bool renorm = !(flags & ENQUEUE_WAKEUP) || (flags & ENQUEUE_MIGRATED);
4329 bool curr = cfs_rq->curr == se;
Peter Zijlstra3a47d512016-03-09 13:04:03 +01004330
Ingo Molnar53d3bc72016-05-11 08:25:53 +02004331 /*
Peter Zijlstra2f950352016-05-11 19:27:56 +02004332 * If we're the current task, we must renormalise before calling
4333 * update_curr().
Ingo Molnar53d3bc72016-05-11 08:25:53 +02004334 */
Peter Zijlstra2f950352016-05-11 19:27:56 +02004335 if (renorm && curr)
4336 se->vruntime += cfs_rq->min_vruntime;
4337
Ingo Molnarb7cc0892007-08-09 11:16:47 +02004338 update_curr(cfs_rq);
Peter Zijlstra2f950352016-05-11 19:27:56 +02004339
4340 /*
4341 * Otherwise, renormalise after, such that we're placed at the current
4342 * moment in time, instead of some random moment in the past. Being
4343 * placed in the past could significantly boost this task to the
4344 * fairness detriment of existing tasks.
4345 */
4346 if (renorm && !curr)
4347 se->vruntime += cfs_rq->min_vruntime;
4348
Yuyang Du9d89c252015-07-15 08:04:37 +08004349 enqueue_entity_load_avg(cfs_rq, se);
Linus Torvalds17bc14b2012-12-14 07:20:43 -08004350 account_entity_enqueue(cfs_rq, se);
4351 update_cfs_shares(cfs_rq);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02004352
Josh Poimboeuf1a3d0272016-06-17 12:43:23 -05004353 if (flags & ENQUEUE_WAKEUP)
Peter Zijlstraaeb73b02007-10-15 17:00:05 +02004354 place_entity(cfs_rq, se, 0);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02004355
Mel Gormancb251762016-02-05 09:08:36 +00004356 check_schedstat_required();
Josh Poimboeuf4fa8d2992016-06-17 12:43:26 -05004357 update_stats_enqueue(cfs_rq, se, flags);
4358 check_spread(cfs_rq, se);
Peter Zijlstra2f950352016-05-11 19:27:56 +02004359 if (!curr)
Srivatsa Vaddagiri83b699e2007-10-15 17:00:08 +02004360 __enqueue_entity(cfs_rq, se);
Peter Zijlstra2069dd72010-11-15 15:47:00 -08004361 se->on_rq = 1;
Peter Zijlstra3d4b47b2010-11-15 15:47:01 -08004362
Paul Turnerd3d9dc32011-07-21 09:43:39 -07004363 if (cfs_rq->nr_running == 1) {
Peter Zijlstra3d4b47b2010-11-15 15:47:01 -08004364 list_add_leaf_cfs_rq(cfs_rq);
Paul Turnerd3d9dc32011-07-21 09:43:39 -07004365 check_enqueue_throttle(cfs_rq);
4366 }
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02004367}
4368
Rik van Riel2c13c9192011-02-01 09:48:37 -05004369static void __clear_buddies_last(struct sched_entity *se)
Peter Zijlstra2002c692008-11-11 11:52:33 +01004370{
Rik van Riel2c13c9192011-02-01 09:48:37 -05004371 for_each_sched_entity(se) {
4372 struct cfs_rq *cfs_rq = cfs_rq_of(se);
Peter Zijlstraf1044792012-02-11 06:05:00 +01004373 if (cfs_rq->last != se)
Rik van Riel2c13c9192011-02-01 09:48:37 -05004374 break;
Peter Zijlstraf1044792012-02-11 06:05:00 +01004375
4376 cfs_rq->last = NULL;
Rik van Riel2c13c9192011-02-01 09:48:37 -05004377 }
4378}
Peter Zijlstra2002c692008-11-11 11:52:33 +01004379
Rik van Riel2c13c9192011-02-01 09:48:37 -05004380static void __clear_buddies_next(struct sched_entity *se)
4381{
4382 for_each_sched_entity(se) {
4383 struct cfs_rq *cfs_rq = cfs_rq_of(se);
Peter Zijlstraf1044792012-02-11 06:05:00 +01004384 if (cfs_rq->next != se)
Rik van Riel2c13c9192011-02-01 09:48:37 -05004385 break;
Peter Zijlstraf1044792012-02-11 06:05:00 +01004386
4387 cfs_rq->next = NULL;
Rik van Riel2c13c9192011-02-01 09:48:37 -05004388 }
Peter Zijlstra2002c692008-11-11 11:52:33 +01004389}
4390
Rik van Rielac53db52011-02-01 09:51:03 -05004391static void __clear_buddies_skip(struct sched_entity *se)
4392{
4393 for_each_sched_entity(se) {
4394 struct cfs_rq *cfs_rq = cfs_rq_of(se);
Peter Zijlstraf1044792012-02-11 06:05:00 +01004395 if (cfs_rq->skip != se)
Rik van Rielac53db52011-02-01 09:51:03 -05004396 break;
Peter Zijlstraf1044792012-02-11 06:05:00 +01004397
4398 cfs_rq->skip = NULL;
Rik van Rielac53db52011-02-01 09:51:03 -05004399 }
4400}
4401
Peter Zijlstraa571bbe2009-01-28 14:51:40 +01004402static void clear_buddies(struct cfs_rq *cfs_rq, struct sched_entity *se)
4403{
Rik van Riel2c13c9192011-02-01 09:48:37 -05004404 if (cfs_rq->last == se)
4405 __clear_buddies_last(se);
4406
4407 if (cfs_rq->next == se)
4408 __clear_buddies_next(se);
Rik van Rielac53db52011-02-01 09:51:03 -05004409
4410 if (cfs_rq->skip == se)
4411 __clear_buddies_skip(se);
Peter Zijlstraa571bbe2009-01-28 14:51:40 +01004412}
4413
Peter Zijlstra6c16a6d2012-03-21 13:07:16 -07004414static __always_inline void return_cfs_rq_runtime(struct cfs_rq *cfs_rq);
Paul Turnerd8b49862011-07-21 09:43:41 -07004415
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02004416static void
Peter Zijlstra371fd7e2010-03-24 16:38:48 +01004417dequeue_entity(struct cfs_rq *cfs_rq, struct sched_entity *se, int flags)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02004418{
Dmitry Adamushkoa2a2d682007-10-15 17:00:13 +02004419 /*
4420 * Update run-time statistics of the 'current'.
4421 */
4422 update_curr(cfs_rq);
Yuyang Du13962232015-07-15 08:04:41 +08004423 dequeue_entity_load_avg(cfs_rq, se);
Dmitry Adamushkoa2a2d682007-10-15 17:00:13 +02004424
Josh Poimboeuf4fa8d2992016-06-17 12:43:26 -05004425 update_stats_dequeue(cfs_rq, se, flags);
Peter Zijlstra67e9fb22007-10-15 17:00:10 +02004426
Peter Zijlstra2002c692008-11-11 11:52:33 +01004427 clear_buddies(cfs_rq, se);
Peter Zijlstra47932412008-11-04 21:25:09 +01004428
Srivatsa Vaddagiri83b699e2007-10-15 17:00:08 +02004429 if (se != cfs_rq->curr)
Dmitry Adamushko30cfdcf2007-10-15 17:00:07 +02004430 __dequeue_entity(cfs_rq, se);
Linus Torvalds17bc14b2012-12-14 07:20:43 -08004431 se->on_rq = 0;
Dmitry Adamushko30cfdcf2007-10-15 17:00:07 +02004432 account_entity_dequeue(cfs_rq, se);
Peter Zijlstra88ec22d2009-12-16 18:04:41 +01004433
4434 /*
Peter Zijlstrab60205c2016-09-20 21:58:12 +02004435 * Normalize after update_curr(); which will also have moved
4436 * min_vruntime if @se is the one holding it back. But before doing
4437 * update_min_vruntime() again, which will discount @se's position and
4438 * can move min_vruntime forward still more.
Peter Zijlstra88ec22d2009-12-16 18:04:41 +01004439 */
Peter Zijlstra371fd7e2010-03-24 16:38:48 +01004440 if (!(flags & DEQUEUE_SLEEP))
Peter Zijlstra88ec22d2009-12-16 18:04:41 +01004441 se->vruntime -= cfs_rq->min_vruntime;
Peter Zijlstra1e876232011-05-17 16:21:10 -07004442
Paul Turnerd8b49862011-07-21 09:43:41 -07004443 /* return excess runtime on last dequeue */
4444 return_cfs_rq_runtime(cfs_rq);
4445
Linus Torvalds17bc14b2012-12-14 07:20:43 -08004446 update_cfs_shares(cfs_rq);
Peter Zijlstrab60205c2016-09-20 21:58:12 +02004447
4448 /*
4449 * Now advance min_vruntime if @se was the entity holding it back,
4450 * except when: DEQUEUE_SAVE && !DEQUEUE_MOVE, in this case we'll be
4451 * put back on, and if we advance min_vruntime, we'll be placed back
4452 * further than we started -- ie. we'll be penalized.
4453 */
4454 if ((flags & (DEQUEUE_SAVE | DEQUEUE_MOVE)) == DEQUEUE_SAVE)
4455 update_min_vruntime(cfs_rq);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02004456}
4457
4458/*
4459 * Preempt the current task with a newly woken task if needed:
4460 */
Peter Zijlstra7c92e542007-09-05 14:32:49 +02004461static void
Ingo Molnar2e09bf52007-10-15 17:00:05 +02004462check_preempt_tick(struct cfs_rq *cfs_rq, struct sched_entity *curr)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02004463{
Peter Zijlstra11697832007-09-05 14:32:49 +02004464 unsigned long ideal_runtime, delta_exec;
Wang Xingchaof4cfb332011-09-16 13:35:52 -04004465 struct sched_entity *se;
4466 s64 delta;
Peter Zijlstra11697832007-09-05 14:32:49 +02004467
Peter Zijlstra6d0f0ebd2007-10-15 17:00:05 +02004468 ideal_runtime = sched_slice(cfs_rq, curr);
Peter Zijlstra11697832007-09-05 14:32:49 +02004469 delta_exec = curr->sum_exec_runtime - curr->prev_sum_exec_runtime;
Mike Galbraitha9f3e2b2009-01-28 14:51:39 +01004470 if (delta_exec > ideal_runtime) {
Kirill Tkhai88751252014-06-29 00:03:57 +04004471 resched_curr(rq_of(cfs_rq));
Mike Galbraitha9f3e2b2009-01-28 14:51:39 +01004472 /*
4473 * The current task ran long enough, ensure it doesn't get
4474 * re-elected due to buddy favours.
4475 */
4476 clear_buddies(cfs_rq, curr);
Mike Galbraithf685cea2009-10-23 23:09:22 +02004477 return;
4478 }
4479
4480 /*
4481 * Ensure that a task that missed wakeup preemption by a
4482 * narrow margin doesn't have to wait for a full slice.
4483 * This also mitigates buddy induced latencies under load.
4484 */
Mike Galbraithf685cea2009-10-23 23:09:22 +02004485 if (delta_exec < sysctl_sched_min_granularity)
4486 return;
4487
Wang Xingchaof4cfb332011-09-16 13:35:52 -04004488 se = __pick_first_entity(cfs_rq);
4489 delta = curr->vruntime - se->vruntime;
Mike Galbraithf685cea2009-10-23 23:09:22 +02004490
Wang Xingchaof4cfb332011-09-16 13:35:52 -04004491 if (delta < 0)
4492 return;
Mike Galbraithd7d82942011-01-05 05:41:17 +01004493
Wang Xingchaof4cfb332011-09-16 13:35:52 -04004494 if (delta > ideal_runtime)
Kirill Tkhai88751252014-06-29 00:03:57 +04004495 resched_curr(rq_of(cfs_rq));
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02004496}
4497
Srivatsa Vaddagiri83b699e2007-10-15 17:00:08 +02004498static void
Ingo Molnar8494f412007-08-09 11:16:48 +02004499set_next_entity(struct cfs_rq *cfs_rq, struct sched_entity *se)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02004500{
Srivatsa Vaddagiri83b699e2007-10-15 17:00:08 +02004501 /* 'current' is not kept within the tree. */
4502 if (se->on_rq) {
4503 /*
4504 * Any task has to be enqueued before it get to execute on
4505 * a CPU. So account for the time it spent waiting on the
4506 * runqueue.
4507 */
Josh Poimboeuf4fa8d2992016-06-17 12:43:26 -05004508 update_stats_wait_end(cfs_rq, se);
Srivatsa Vaddagiri83b699e2007-10-15 17:00:08 +02004509 __dequeue_entity(cfs_rq, se);
Yuyang Du9d89c252015-07-15 08:04:37 +08004510 update_load_avg(se, 1);
Srivatsa Vaddagiri83b699e2007-10-15 17:00:08 +02004511 }
4512
Ingo Molnar79303e92007-08-09 11:16:47 +02004513 update_stats_curr_start(cfs_rq, se);
Ingo Molnar429d43b2007-10-15 17:00:03 +02004514 cfs_rq->curr = se;
Josh Poimboeuf4fa8d2992016-06-17 12:43:26 -05004515
Ingo Molnareba1ed42007-10-15 17:00:02 +02004516 /*
4517 * Track our maximum slice length, if the CPU's load is at
4518 * least twice that of our own weight (i.e. dont track it
4519 * when there are only lesser-weight tasks around):
4520 */
Mel Gormancb251762016-02-05 09:08:36 +00004521 if (schedstat_enabled() && rq_of(cfs_rq)->load.weight >= 2*se->load.weight) {
Josh Poimboeuf4fa8d2992016-06-17 12:43:26 -05004522 schedstat_set(se->statistics.slice_max,
4523 max((u64)schedstat_val(se->statistics.slice_max),
4524 se->sum_exec_runtime - se->prev_sum_exec_runtime));
Ingo Molnareba1ed42007-10-15 17:00:02 +02004525 }
Josh Poimboeuf4fa8d2992016-06-17 12:43:26 -05004526
Peter Zijlstra4a55b452007-09-05 14:32:49 +02004527 se->prev_sum_exec_runtime = se->sum_exec_runtime;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02004528}
4529
Peter Zijlstra3f3a4902008-10-24 11:06:16 +02004530static int
4531wakeup_preempt_entity(struct sched_entity *curr, struct sched_entity *se);
4532
Rik van Rielac53db52011-02-01 09:51:03 -05004533/*
4534 * Pick the next process, keeping these things in mind, in this order:
4535 * 1) keep things fair between processes/task groups
4536 * 2) pick the "next" process, since someone really wants that to run
4537 * 3) pick the "last" process, for cache locality
4538 * 4) do not run the "skip" process, if something else is available
4539 */
Peter Zijlstra678d5712012-02-11 06:05:00 +01004540static struct sched_entity *
4541pick_next_entity(struct cfs_rq *cfs_rq, struct sched_entity *curr)
Peter Zijlstraaa2ac252008-03-14 21:12:12 +01004542{
Peter Zijlstra678d5712012-02-11 06:05:00 +01004543 struct sched_entity *left = __pick_first_entity(cfs_rq);
4544 struct sched_entity *se;
4545
4546 /*
4547 * If curr is set we have to see if its left of the leftmost entity
4548 * still in the tree, provided there was anything in the tree at all.
4549 */
4550 if (!left || (curr && entity_before(curr, left)))
4551 left = curr;
4552
4553 se = left; /* ideally we run the leftmost entity */
Peter Zijlstraf4b67552008-11-04 21:25:07 +01004554
Rik van Rielac53db52011-02-01 09:51:03 -05004555 /*
4556 * Avoid running the skip buddy, if running something else can
4557 * be done without getting too unfair.
4558 */
4559 if (cfs_rq->skip == se) {
Peter Zijlstra678d5712012-02-11 06:05:00 +01004560 struct sched_entity *second;
4561
4562 if (se == curr) {
4563 second = __pick_first_entity(cfs_rq);
4564 } else {
4565 second = __pick_next_entity(se);
4566 if (!second || (curr && entity_before(curr, second)))
4567 second = curr;
4568 }
4569
Rik van Rielac53db52011-02-01 09:51:03 -05004570 if (second && wakeup_preempt_entity(second, left) < 1)
4571 se = second;
4572 }
Peter Zijlstraaa2ac252008-03-14 21:12:12 +01004573
Mike Galbraithf685cea2009-10-23 23:09:22 +02004574 /*
4575 * Prefer last buddy, try to return the CPU to a preempted task.
4576 */
4577 if (cfs_rq->last && wakeup_preempt_entity(cfs_rq->last, left) < 1)
4578 se = cfs_rq->last;
4579
Rik van Rielac53db52011-02-01 09:51:03 -05004580 /*
4581 * Someone really wants this to run. If it's not unfair, run it.
4582 */
4583 if (cfs_rq->next && wakeup_preempt_entity(cfs_rq->next, left) < 1)
4584 se = cfs_rq->next;
4585
Mike Galbraithf685cea2009-10-23 23:09:22 +02004586 clear_buddies(cfs_rq, se);
Peter Zijlstra47932412008-11-04 21:25:09 +01004587
4588 return se;
Peter Zijlstraaa2ac252008-03-14 21:12:12 +01004589}
4590
Peter Zijlstra678d5712012-02-11 06:05:00 +01004591static bool check_cfs_rq_runtime(struct cfs_rq *cfs_rq);
Paul Turnerd3d9dc32011-07-21 09:43:39 -07004592
Ingo Molnarab6cde22007-08-09 11:16:48 +02004593static void put_prev_entity(struct cfs_rq *cfs_rq, struct sched_entity *prev)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02004594{
4595 /*
4596 * If still on the runqueue then deactivate_task()
4597 * was not called and update_curr() has to be done:
4598 */
4599 if (prev->on_rq)
Ingo Molnarb7cc0892007-08-09 11:16:47 +02004600 update_curr(cfs_rq);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02004601
Paul Turnerd3d9dc32011-07-21 09:43:39 -07004602 /* throttle cfs_rqs exceeding runtime */
4603 check_cfs_rq_runtime(cfs_rq);
4604
Josh Poimboeuf4fa8d2992016-06-17 12:43:26 -05004605 check_spread(cfs_rq, prev);
Mel Gormancb251762016-02-05 09:08:36 +00004606
Dmitry Adamushko30cfdcf2007-10-15 17:00:07 +02004607 if (prev->on_rq) {
Josh Poimboeuf4fa8d2992016-06-17 12:43:26 -05004608 update_stats_wait_start(cfs_rq, prev);
Dmitry Adamushko30cfdcf2007-10-15 17:00:07 +02004609 /* Put 'current' back into the tree. */
4610 __enqueue_entity(cfs_rq, prev);
Paul Turner9d85f212012-10-04 13:18:29 +02004611 /* in !on_rq case, update occurred at dequeue */
Yuyang Du9d89c252015-07-15 08:04:37 +08004612 update_load_avg(prev, 0);
Dmitry Adamushko30cfdcf2007-10-15 17:00:07 +02004613 }
Ingo Molnar429d43b2007-10-15 17:00:03 +02004614 cfs_rq->curr = NULL;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02004615}
4616
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01004617static void
4618entity_tick(struct cfs_rq *cfs_rq, struct sched_entity *curr, int queued)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02004619{
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02004620 /*
Dmitry Adamushko30cfdcf2007-10-15 17:00:07 +02004621 * Update run-time statistics of the 'current'.
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02004622 */
Dmitry Adamushko30cfdcf2007-10-15 17:00:07 +02004623 update_curr(cfs_rq);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02004624
Paul Turner43365bd2010-12-15 19:10:17 -08004625 /*
Paul Turner9d85f212012-10-04 13:18:29 +02004626 * Ensure that runnable average is periodically updated.
4627 */
Yuyang Du9d89c252015-07-15 08:04:37 +08004628 update_load_avg(curr, 1);
Peter Zijlstrabf0bd942013-07-26 23:48:42 +02004629 update_cfs_shares(cfs_rq);
Paul Turner9d85f212012-10-04 13:18:29 +02004630
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01004631#ifdef CONFIG_SCHED_HRTICK
4632 /*
4633 * queued ticks are scheduled to match the slice, so don't bother
4634 * validating it and just reschedule.
4635 */
Harvey Harrison983ed7a2008-04-24 18:17:55 -07004636 if (queued) {
Kirill Tkhai88751252014-06-29 00:03:57 +04004637 resched_curr(rq_of(cfs_rq));
Harvey Harrison983ed7a2008-04-24 18:17:55 -07004638 return;
4639 }
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01004640 /*
4641 * don't let the period tick interfere with the hrtick preemption
4642 */
4643 if (!sched_feat(DOUBLE_TICK) &&
4644 hrtimer_active(&rq_of(cfs_rq)->hrtick_timer))
4645 return;
4646#endif
4647
Yong Zhang2c2efae2011-07-29 16:20:33 +08004648 if (cfs_rq->nr_running > 1)
Ingo Molnar2e09bf52007-10-15 17:00:05 +02004649 check_preempt_tick(cfs_rq, curr);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02004650}
4651
Paul Turnerab84d312011-07-21 09:43:28 -07004652
4653/**************************************************
4654 * CFS bandwidth control machinery
4655 */
4656
4657#ifdef CONFIG_CFS_BANDWIDTH
Peter Zijlstra029632f2011-10-25 10:00:11 +02004658
4659#ifdef HAVE_JUMP_LABEL
Ingo Molnarc5905af2012-02-24 08:31:31 +01004660static struct static_key __cfs_bandwidth_used;
Peter Zijlstra029632f2011-10-25 10:00:11 +02004661
4662static inline bool cfs_bandwidth_used(void)
4663{
Ingo Molnarc5905af2012-02-24 08:31:31 +01004664 return static_key_false(&__cfs_bandwidth_used);
Peter Zijlstra029632f2011-10-25 10:00:11 +02004665}
4666
Ben Segall1ee14e62013-10-16 11:16:12 -07004667void cfs_bandwidth_usage_inc(void)
Peter Zijlstra029632f2011-10-25 10:00:11 +02004668{
Ben Segall1ee14e62013-10-16 11:16:12 -07004669 static_key_slow_inc(&__cfs_bandwidth_used);
4670}
4671
4672void cfs_bandwidth_usage_dec(void)
4673{
4674 static_key_slow_dec(&__cfs_bandwidth_used);
Peter Zijlstra029632f2011-10-25 10:00:11 +02004675}
4676#else /* HAVE_JUMP_LABEL */
4677static bool cfs_bandwidth_used(void)
4678{
4679 return true;
4680}
4681
Ben Segall1ee14e62013-10-16 11:16:12 -07004682void cfs_bandwidth_usage_inc(void) {}
4683void cfs_bandwidth_usage_dec(void) {}
Peter Zijlstra029632f2011-10-25 10:00:11 +02004684#endif /* HAVE_JUMP_LABEL */
4685
Paul Turnerab84d312011-07-21 09:43:28 -07004686/*
4687 * default period for cfs group bandwidth.
4688 * default: 0.1s, units: nanoseconds
4689 */
4690static inline u64 default_cfs_period(void)
4691{
4692 return 100000000ULL;
4693}
Paul Turnerec12cb72011-07-21 09:43:30 -07004694
4695static inline u64 sched_cfs_bandwidth_slice(void)
4696{
4697 return (u64)sysctl_sched_cfs_bandwidth_slice * NSEC_PER_USEC;
4698}
4699
Paul Turnera9cf55b2011-07-21 09:43:32 -07004700/*
4701 * Replenish runtime according to assigned quota and update expiration time.
4702 * We use sched_clock_cpu directly instead of rq->clock to avoid adding
4703 * additional synchronization around rq->lock.
4704 *
4705 * requires cfs_b->lock
4706 */
Peter Zijlstra029632f2011-10-25 10:00:11 +02004707void __refill_cfs_bandwidth_runtime(struct cfs_bandwidth *cfs_b)
Paul Turnera9cf55b2011-07-21 09:43:32 -07004708{
4709 u64 now;
4710
4711 if (cfs_b->quota == RUNTIME_INF)
4712 return;
4713
4714 now = sched_clock_cpu(smp_processor_id());
4715 cfs_b->runtime = cfs_b->quota;
4716 cfs_b->runtime_expires = now + ktime_to_ns(cfs_b->period);
4717}
4718
Peter Zijlstra029632f2011-10-25 10:00:11 +02004719static inline struct cfs_bandwidth *tg_cfs_bandwidth(struct task_group *tg)
4720{
4721 return &tg->cfs_bandwidth;
4722}
4723
Paul Turnerf1b17282012-10-04 13:18:31 +02004724/* rq->task_clock normalized against any time this cfs_rq has spent throttled */
4725static inline u64 cfs_rq_clock_task(struct cfs_rq *cfs_rq)
4726{
4727 if (unlikely(cfs_rq->throttle_count))
Xunlei Pang1a99ae32016-05-10 21:03:18 +08004728 return cfs_rq->throttled_clock_task - cfs_rq->throttled_clock_task_time;
Paul Turnerf1b17282012-10-04 13:18:31 +02004729
Frederic Weisbecker78becc22013-04-12 01:51:02 +02004730 return rq_clock_task(rq_of(cfs_rq)) - cfs_rq->throttled_clock_task_time;
Paul Turnerf1b17282012-10-04 13:18:31 +02004731}
4732
Paul Turner85dac902011-07-21 09:43:33 -07004733/* returns 0 on failure to allocate runtime */
4734static int assign_cfs_rq_runtime(struct cfs_rq *cfs_rq)
Paul Turnerec12cb72011-07-21 09:43:30 -07004735{
4736 struct task_group *tg = cfs_rq->tg;
4737 struct cfs_bandwidth *cfs_b = tg_cfs_bandwidth(tg);
Paul Turnera9cf55b2011-07-21 09:43:32 -07004738 u64 amount = 0, min_amount, expires;
Paul Turnerec12cb72011-07-21 09:43:30 -07004739
4740 /* note: this is a positive sum as runtime_remaining <= 0 */
4741 min_amount = sched_cfs_bandwidth_slice() - cfs_rq->runtime_remaining;
4742
4743 raw_spin_lock(&cfs_b->lock);
4744 if (cfs_b->quota == RUNTIME_INF)
4745 amount = min_amount;
Paul Turner58088ad2011-07-21 09:43:31 -07004746 else {
Peter Zijlstra77a4d1a2015-04-15 11:41:57 +02004747 start_cfs_bandwidth(cfs_b);
Paul Turner58088ad2011-07-21 09:43:31 -07004748
4749 if (cfs_b->runtime > 0) {
4750 amount = min(cfs_b->runtime, min_amount);
4751 cfs_b->runtime -= amount;
4752 cfs_b->idle = 0;
4753 }
Paul Turnerec12cb72011-07-21 09:43:30 -07004754 }
Paul Turnera9cf55b2011-07-21 09:43:32 -07004755 expires = cfs_b->runtime_expires;
Paul Turnerec12cb72011-07-21 09:43:30 -07004756 raw_spin_unlock(&cfs_b->lock);
4757
4758 cfs_rq->runtime_remaining += amount;
Paul Turnera9cf55b2011-07-21 09:43:32 -07004759 /*
4760 * we may have advanced our local expiration to account for allowed
4761 * spread between our sched_clock and the one on which runtime was
4762 * issued.
4763 */
4764 if ((s64)(expires - cfs_rq->runtime_expires) > 0)
4765 cfs_rq->runtime_expires = expires;
Paul Turner85dac902011-07-21 09:43:33 -07004766
4767 return cfs_rq->runtime_remaining > 0;
Paul Turnera9cf55b2011-07-21 09:43:32 -07004768}
4769
4770/*
4771 * Note: This depends on the synchronization provided by sched_clock and the
4772 * fact that rq->clock snapshots this value.
4773 */
4774static void expire_cfs_rq_runtime(struct cfs_rq *cfs_rq)
4775{
4776 struct cfs_bandwidth *cfs_b = tg_cfs_bandwidth(cfs_rq->tg);
Paul Turnera9cf55b2011-07-21 09:43:32 -07004777
4778 /* if the deadline is ahead of our clock, nothing to do */
Frederic Weisbecker78becc22013-04-12 01:51:02 +02004779 if (likely((s64)(rq_clock(rq_of(cfs_rq)) - cfs_rq->runtime_expires) < 0))
Paul Turnera9cf55b2011-07-21 09:43:32 -07004780 return;
4781
4782 if (cfs_rq->runtime_remaining < 0)
4783 return;
4784
4785 /*
4786 * If the local deadline has passed we have to consider the
4787 * possibility that our sched_clock is 'fast' and the global deadline
4788 * has not truly expired.
4789 *
4790 * Fortunately we can check determine whether this the case by checking
Ben Segall51f21762014-05-19 15:49:45 -07004791 * whether the global deadline has advanced. It is valid to compare
4792 * cfs_b->runtime_expires without any locks since we only care about
4793 * exact equality, so a partial write will still work.
Paul Turnera9cf55b2011-07-21 09:43:32 -07004794 */
4795
Ben Segall51f21762014-05-19 15:49:45 -07004796 if (cfs_rq->runtime_expires != cfs_b->runtime_expires) {
Paul Turnera9cf55b2011-07-21 09:43:32 -07004797 /* extend local deadline, drift is bounded above by 2 ticks */
4798 cfs_rq->runtime_expires += TICK_NSEC;
4799 } else {
4800 /* global deadline is ahead, expiration has passed */
4801 cfs_rq->runtime_remaining = 0;
4802 }
Paul Turnerec12cb72011-07-21 09:43:30 -07004803}
4804
Peter Zijlstra9dbdb152013-11-18 18:27:06 +01004805static void __account_cfs_rq_runtime(struct cfs_rq *cfs_rq, u64 delta_exec)
Paul Turnerec12cb72011-07-21 09:43:30 -07004806{
Paul Turnera9cf55b2011-07-21 09:43:32 -07004807 /* dock delta_exec before expiring quota (as it could span periods) */
Paul Turnerec12cb72011-07-21 09:43:30 -07004808 cfs_rq->runtime_remaining -= delta_exec;
Paul Turnera9cf55b2011-07-21 09:43:32 -07004809 expire_cfs_rq_runtime(cfs_rq);
4810
4811 if (likely(cfs_rq->runtime_remaining > 0))
Paul Turnerec12cb72011-07-21 09:43:30 -07004812 return;
4813
Paul Turner85dac902011-07-21 09:43:33 -07004814 /*
4815 * if we're unable to extend our runtime we resched so that the active
4816 * hierarchy can be throttled
4817 */
4818 if (!assign_cfs_rq_runtime(cfs_rq) && likely(cfs_rq->curr))
Kirill Tkhai88751252014-06-29 00:03:57 +04004819 resched_curr(rq_of(cfs_rq));
Paul Turnerec12cb72011-07-21 09:43:30 -07004820}
4821
Peter Zijlstra6c16a6d2012-03-21 13:07:16 -07004822static __always_inline
Peter Zijlstra9dbdb152013-11-18 18:27:06 +01004823void account_cfs_rq_runtime(struct cfs_rq *cfs_rq, u64 delta_exec)
Paul Turnerec12cb72011-07-21 09:43:30 -07004824{
Paul Turner56f570e2011-11-07 20:26:33 -08004825 if (!cfs_bandwidth_used() || !cfs_rq->runtime_enabled)
Paul Turnerec12cb72011-07-21 09:43:30 -07004826 return;
4827
4828 __account_cfs_rq_runtime(cfs_rq, delta_exec);
4829}
4830
Paul Turner85dac902011-07-21 09:43:33 -07004831static inline int cfs_rq_throttled(struct cfs_rq *cfs_rq)
4832{
Paul Turner56f570e2011-11-07 20:26:33 -08004833 return cfs_bandwidth_used() && cfs_rq->throttled;
Paul Turner85dac902011-07-21 09:43:33 -07004834}
4835
Syed Rameez Mustafadddcab72016-09-07 16:18:27 -07004836/*
4837 * Check if task is part of a hierarchy where some cfs_rq does not have any
4838 * runtime left.
4839 *
4840 * We can't rely on throttled_hierarchy() to do this test, as
4841 * cfs_rq->throttle_count will not be updated yet when this function is called
4842 * from scheduler_tick()
4843 */
4844static int task_will_be_throttled(struct task_struct *p)
4845{
4846 struct sched_entity *se = &p->se;
4847 struct cfs_rq *cfs_rq;
4848
4849 if (!cfs_bandwidth_used())
4850 return 0;
4851
4852 for_each_sched_entity(se) {
4853 cfs_rq = cfs_rq_of(se);
4854 if (!cfs_rq->runtime_enabled)
4855 continue;
4856 if (cfs_rq->runtime_remaining <= 0)
4857 return 1;
4858 }
4859
4860 return 0;
4861}
4862
Paul Turner64660c82011-07-21 09:43:36 -07004863/* check whether cfs_rq, or any parent, is throttled */
4864static inline int throttled_hierarchy(struct cfs_rq *cfs_rq)
4865{
Paul Turner56f570e2011-11-07 20:26:33 -08004866 return cfs_bandwidth_used() && cfs_rq->throttle_count;
Paul Turner64660c82011-07-21 09:43:36 -07004867}
4868
4869/*
4870 * Ensure that neither of the group entities corresponding to src_cpu or
4871 * dest_cpu are members of a throttled hierarchy when performing group
4872 * load-balance operations.
4873 */
4874static inline int throttled_lb_pair(struct task_group *tg,
4875 int src_cpu, int dest_cpu)
4876{
4877 struct cfs_rq *src_cfs_rq, *dest_cfs_rq;
4878
4879 src_cfs_rq = tg->cfs_rq[src_cpu];
4880 dest_cfs_rq = tg->cfs_rq[dest_cpu];
4881
4882 return throttled_hierarchy(src_cfs_rq) ||
4883 throttled_hierarchy(dest_cfs_rq);
4884}
4885
4886/* updated child weight may affect parent so we have to do this bottom up */
4887static int tg_unthrottle_up(struct task_group *tg, void *data)
4888{
4889 struct rq *rq = data;
4890 struct cfs_rq *cfs_rq = tg->cfs_rq[cpu_of(rq)];
4891
4892 cfs_rq->throttle_count--;
Paul Turner64660c82011-07-21 09:43:36 -07004893 if (!cfs_rq->throttle_count) {
Paul Turnerf1b17282012-10-04 13:18:31 +02004894 /* adjust cfs_rq_clock_task() */
Frederic Weisbecker78becc22013-04-12 01:51:02 +02004895 cfs_rq->throttled_clock_task_time += rq_clock_task(rq) -
Paul Turnerf1b17282012-10-04 13:18:31 +02004896 cfs_rq->throttled_clock_task;
Paul Turner64660c82011-07-21 09:43:36 -07004897 }
Paul Turner64660c82011-07-21 09:43:36 -07004898
4899 return 0;
4900}
4901
4902static int tg_throttle_down(struct task_group *tg, void *data)
4903{
4904 struct rq *rq = data;
4905 struct cfs_rq *cfs_rq = tg->cfs_rq[cpu_of(rq)];
4906
Paul Turner82958362012-10-04 13:18:31 +02004907 /* group is entering throttled state, stop time */
4908 if (!cfs_rq->throttle_count)
Frederic Weisbecker78becc22013-04-12 01:51:02 +02004909 cfs_rq->throttled_clock_task = rq_clock_task(rq);
Paul Turner64660c82011-07-21 09:43:36 -07004910 cfs_rq->throttle_count++;
4911
4912 return 0;
4913}
4914
Paul Turnerd3d9dc32011-07-21 09:43:39 -07004915static void throttle_cfs_rq(struct cfs_rq *cfs_rq)
Paul Turner85dac902011-07-21 09:43:33 -07004916{
4917 struct rq *rq = rq_of(cfs_rq);
4918 struct cfs_bandwidth *cfs_b = tg_cfs_bandwidth(cfs_rq->tg);
4919 struct sched_entity *se;
4920 long task_delta, dequeue = 1;
Peter Zijlstra77a4d1a2015-04-15 11:41:57 +02004921 bool empty;
Paul Turner85dac902011-07-21 09:43:33 -07004922
4923 se = cfs_rq->tg->se[cpu_of(rq_of(cfs_rq))];
4924
Paul Turnerf1b17282012-10-04 13:18:31 +02004925 /* freeze hierarchy runnable averages while throttled */
Paul Turner64660c82011-07-21 09:43:36 -07004926 rcu_read_lock();
4927 walk_tg_tree_from(cfs_rq->tg, tg_throttle_down, tg_nop, (void *)rq);
4928 rcu_read_unlock();
Paul Turner85dac902011-07-21 09:43:33 -07004929
4930 task_delta = cfs_rq->h_nr_running;
4931 for_each_sched_entity(se) {
4932 struct cfs_rq *qcfs_rq = cfs_rq_of(se);
4933 /* throttled entity or throttle-on-deactivate */
4934 if (!se->on_rq)
4935 break;
4936
4937 if (dequeue)
4938 dequeue_entity(qcfs_rq, se, DEQUEUE_SLEEP);
4939 qcfs_rq->h_nr_running -= task_delta;
Syed Rameez Mustafadddcab72016-09-07 16:18:27 -07004940#ifdef CONFIG_SCHED_HMP
4941 dec_throttled_cfs_rq_hmp_stats(&qcfs_rq->hmp_stats, cfs_rq);
4942#endif
Paul Turner85dac902011-07-21 09:43:33 -07004943
4944 if (qcfs_rq->load.weight)
4945 dequeue = 0;
4946 }
4947
Syed Rameez Mustafadddcab72016-09-07 16:18:27 -07004948 if (!se) {
Kirill Tkhai72465442014-05-09 03:00:14 +04004949 sub_nr_running(rq, task_delta);
Syed Rameez Mustafadddcab72016-09-07 16:18:27 -07004950#ifdef CONFIG_SCHED_HMP
4951 dec_throttled_cfs_rq_hmp_stats(&rq->hmp_stats, cfs_rq);
4952#endif
4953 }
Paul Turner85dac902011-07-21 09:43:33 -07004954
4955 cfs_rq->throttled = 1;
Frederic Weisbecker78becc22013-04-12 01:51:02 +02004956 cfs_rq->throttled_clock = rq_clock(rq);
Paul Turner85dac902011-07-21 09:43:33 -07004957 raw_spin_lock(&cfs_b->lock);
Cong Wangd49db342015-06-24 12:41:47 -07004958 empty = list_empty(&cfs_b->throttled_cfs_rq);
Peter Zijlstra77a4d1a2015-04-15 11:41:57 +02004959
Ben Segallc06f04c2014-06-20 15:21:20 -07004960 /*
4961 * Add to the _head_ of the list, so that an already-started
4962 * distribute_cfs_runtime will not see us
4963 */
4964 list_add_rcu(&cfs_rq->throttled_list, &cfs_b->throttled_cfs_rq);
Peter Zijlstra77a4d1a2015-04-15 11:41:57 +02004965
4966 /*
4967 * If we're the first throttled task, make sure the bandwidth
4968 * timer is running.
4969 */
4970 if (empty)
4971 start_cfs_bandwidth(cfs_b);
4972
Paul Turner85dac902011-07-21 09:43:33 -07004973 raw_spin_unlock(&cfs_b->lock);
Syed Rameez Mustafadddcab72016-09-07 16:18:27 -07004974
4975 /* Log effect on hmp stats after throttling */
4976 trace_sched_cpu_load_cgroup(rq, idle_cpu(cpu_of(rq)),
4977 sched_irqload(cpu_of(rq)),
4978 power_cost(cpu_of(rq), 0),
4979 cpu_temp(cpu_of(rq)));
Paul Turner85dac902011-07-21 09:43:33 -07004980}
4981
Peter Zijlstra029632f2011-10-25 10:00:11 +02004982void unthrottle_cfs_rq(struct cfs_rq *cfs_rq)
Paul Turner671fd9d2011-07-21 09:43:34 -07004983{
4984 struct rq *rq = rq_of(cfs_rq);
4985 struct cfs_bandwidth *cfs_b = tg_cfs_bandwidth(cfs_rq->tg);
4986 struct sched_entity *se;
4987 int enqueue = 1;
4988 long task_delta;
Syed Rameez Mustafadddcab72016-09-07 16:18:27 -07004989 struct cfs_rq *tcfs_rq = cfs_rq;
Paul Turner671fd9d2011-07-21 09:43:34 -07004990
Michael Wang22b958d2013-06-04 14:23:39 +08004991 se = cfs_rq->tg->se[cpu_of(rq)];
Paul Turner671fd9d2011-07-21 09:43:34 -07004992
4993 cfs_rq->throttled = 0;
Frederic Weisbecker1a55af22013-04-12 01:51:01 +02004994
4995 update_rq_clock(rq);
4996
Paul Turner671fd9d2011-07-21 09:43:34 -07004997 raw_spin_lock(&cfs_b->lock);
Frederic Weisbecker78becc22013-04-12 01:51:02 +02004998 cfs_b->throttled_time += rq_clock(rq) - cfs_rq->throttled_clock;
Paul Turner671fd9d2011-07-21 09:43:34 -07004999 list_del_rcu(&cfs_rq->throttled_list);
5000 raw_spin_unlock(&cfs_b->lock);
5001
Paul Turner64660c82011-07-21 09:43:36 -07005002 /* update hierarchical throttle state */
5003 walk_tg_tree_from(cfs_rq->tg, tg_nop, tg_unthrottle_up, (void *)rq);
5004
Paul Turner671fd9d2011-07-21 09:43:34 -07005005 if (!cfs_rq->load.weight)
5006 return;
5007
5008 task_delta = cfs_rq->h_nr_running;
5009 for_each_sched_entity(se) {
5010 if (se->on_rq)
5011 enqueue = 0;
5012
5013 cfs_rq = cfs_rq_of(se);
5014 if (enqueue)
5015 enqueue_entity(cfs_rq, se, ENQUEUE_WAKEUP);
5016 cfs_rq->h_nr_running += task_delta;
Syed Rameez Mustafadddcab72016-09-07 16:18:27 -07005017#ifdef CONFIG_SCHED_HMP
5018 inc_throttled_cfs_rq_hmp_stats(&cfs_rq->hmp_stats, tcfs_rq);
5019#endif
Paul Turner671fd9d2011-07-21 09:43:34 -07005020
5021 if (cfs_rq_throttled(cfs_rq))
5022 break;
5023 }
5024
Syed Rameez Mustafadddcab72016-09-07 16:18:27 -07005025 if (!se) {
Kirill Tkhai72465442014-05-09 03:00:14 +04005026 add_nr_running(rq, task_delta);
Syed Rameez Mustafadddcab72016-09-07 16:18:27 -07005027#ifdef CONFIG_SCHED_HMP
5028 inc_throttled_cfs_rq_hmp_stats(&rq->hmp_stats, tcfs_rq);
5029#endif
5030 }
Paul Turner671fd9d2011-07-21 09:43:34 -07005031
5032 /* determine whether we need to wake up potentially idle cpu */
5033 if (rq->curr == rq->idle && rq->cfs.nr_running)
Kirill Tkhai88751252014-06-29 00:03:57 +04005034 resched_curr(rq);
Syed Rameez Mustafadddcab72016-09-07 16:18:27 -07005035
5036 /* Log effect on hmp stats after un-throttling */
5037 trace_sched_cpu_load_cgroup(rq, idle_cpu(cpu_of(rq)),
5038 sched_irqload(cpu_of(rq)),
5039 power_cost(cpu_of(rq), 0),
5040 cpu_temp(cpu_of(rq)));
Paul Turner671fd9d2011-07-21 09:43:34 -07005041}
5042
5043static u64 distribute_cfs_runtime(struct cfs_bandwidth *cfs_b,
5044 u64 remaining, u64 expires)
5045{
5046 struct cfs_rq *cfs_rq;
Ben Segallc06f04c2014-06-20 15:21:20 -07005047 u64 runtime;
5048 u64 starting_runtime = remaining;
Paul Turner671fd9d2011-07-21 09:43:34 -07005049
5050 rcu_read_lock();
5051 list_for_each_entry_rcu(cfs_rq, &cfs_b->throttled_cfs_rq,
5052 throttled_list) {
5053 struct rq *rq = rq_of(cfs_rq);
5054
5055 raw_spin_lock(&rq->lock);
5056 if (!cfs_rq_throttled(cfs_rq))
5057 goto next;
5058
5059 runtime = -cfs_rq->runtime_remaining + 1;
5060 if (runtime > remaining)
5061 runtime = remaining;
5062 remaining -= runtime;
5063
5064 cfs_rq->runtime_remaining += runtime;
5065 cfs_rq->runtime_expires = expires;
5066
5067 /* we check whether we're throttled above */
5068 if (cfs_rq->runtime_remaining > 0)
5069 unthrottle_cfs_rq(cfs_rq);
5070
5071next:
5072 raw_spin_unlock(&rq->lock);
5073
5074 if (!remaining)
5075 break;
5076 }
5077 rcu_read_unlock();
5078
Ben Segallc06f04c2014-06-20 15:21:20 -07005079 return starting_runtime - remaining;
Paul Turner671fd9d2011-07-21 09:43:34 -07005080}
5081
Paul Turner58088ad2011-07-21 09:43:31 -07005082/*
5083 * Responsible for refilling a task_group's bandwidth and unthrottling its
5084 * cfs_rqs as appropriate. If there has been no activity within the last
5085 * period the timer is deactivated until scheduling resumes; cfs_b->idle is
5086 * used to track this state.
5087 */
5088static int do_sched_cfs_period_timer(struct cfs_bandwidth *cfs_b, int overrun)
5089{
Paul Turner671fd9d2011-07-21 09:43:34 -07005090 u64 runtime, runtime_expires;
Ben Segall51f21762014-05-19 15:49:45 -07005091 int throttled;
Paul Turner58088ad2011-07-21 09:43:31 -07005092
Paul Turner58088ad2011-07-21 09:43:31 -07005093 /* no need to continue the timer with no bandwidth constraint */
5094 if (cfs_b->quota == RUNTIME_INF)
Ben Segall51f21762014-05-19 15:49:45 -07005095 goto out_deactivate;
Paul Turner58088ad2011-07-21 09:43:31 -07005096
Paul Turner671fd9d2011-07-21 09:43:34 -07005097 throttled = !list_empty(&cfs_b->throttled_cfs_rq);
Nikhil Raoe8da1b12011-07-21 09:43:40 -07005098 cfs_b->nr_periods += overrun;
Paul Turner671fd9d2011-07-21 09:43:34 -07005099
Ben Segall51f21762014-05-19 15:49:45 -07005100 /*
5101 * idle depends on !throttled (for the case of a large deficit), and if
5102 * we're going inactive then everything else can be deferred
5103 */
5104 if (cfs_b->idle && !throttled)
5105 goto out_deactivate;
Paul Turnera9cf55b2011-07-21 09:43:32 -07005106
5107 __refill_cfs_bandwidth_runtime(cfs_b);
5108
Paul Turner671fd9d2011-07-21 09:43:34 -07005109 if (!throttled) {
5110 /* mark as potentially idle for the upcoming period */
5111 cfs_b->idle = 1;
Ben Segall51f21762014-05-19 15:49:45 -07005112 return 0;
Paul Turner671fd9d2011-07-21 09:43:34 -07005113 }
Paul Turner58088ad2011-07-21 09:43:31 -07005114
Nikhil Raoe8da1b12011-07-21 09:43:40 -07005115 /* account preceding periods in which throttling occurred */
5116 cfs_b->nr_throttled += overrun;
5117
Paul Turner671fd9d2011-07-21 09:43:34 -07005118 runtime_expires = cfs_b->runtime_expires;
Paul Turner671fd9d2011-07-21 09:43:34 -07005119
5120 /*
Ben Segallc06f04c2014-06-20 15:21:20 -07005121 * This check is repeated as we are holding onto the new bandwidth while
5122 * we unthrottle. This can potentially race with an unthrottled group
5123 * trying to acquire new bandwidth from the global pool. This can result
5124 * in us over-using our runtime if it is all used during this loop, but
5125 * only by limited amounts in that extreme case.
Paul Turner671fd9d2011-07-21 09:43:34 -07005126 */
Ben Segallc06f04c2014-06-20 15:21:20 -07005127 while (throttled && cfs_b->runtime > 0) {
5128 runtime = cfs_b->runtime;
Paul Turner671fd9d2011-07-21 09:43:34 -07005129 raw_spin_unlock(&cfs_b->lock);
5130 /* we can't nest cfs_b->lock while distributing bandwidth */
5131 runtime = distribute_cfs_runtime(cfs_b, runtime,
5132 runtime_expires);
5133 raw_spin_lock(&cfs_b->lock);
5134
5135 throttled = !list_empty(&cfs_b->throttled_cfs_rq);
Ben Segallc06f04c2014-06-20 15:21:20 -07005136
5137 cfs_b->runtime -= min(runtime, cfs_b->runtime);
Paul Turner671fd9d2011-07-21 09:43:34 -07005138 }
5139
Paul Turner671fd9d2011-07-21 09:43:34 -07005140 /*
5141 * While we are ensured activity in the period following an
5142 * unthrottle, this also covers the case in which the new bandwidth is
5143 * insufficient to cover the existing bandwidth deficit. (Forcing the
5144 * timer to remain active while there are any throttled entities.)
5145 */
5146 cfs_b->idle = 0;
Paul Turner58088ad2011-07-21 09:43:31 -07005147
Ben Segall51f21762014-05-19 15:49:45 -07005148 return 0;
5149
5150out_deactivate:
Ben Segall51f21762014-05-19 15:49:45 -07005151 return 1;
Paul Turner58088ad2011-07-21 09:43:31 -07005152}
Paul Turnerd3d9dc32011-07-21 09:43:39 -07005153
Paul Turnerd8b49862011-07-21 09:43:41 -07005154/* a cfs_rq won't donate quota below this amount */
5155static const u64 min_cfs_rq_runtime = 1 * NSEC_PER_MSEC;
5156/* minimum remaining period time to redistribute slack quota */
5157static const u64 min_bandwidth_expiration = 2 * NSEC_PER_MSEC;
5158/* how long we wait to gather additional slack before distributing */
5159static const u64 cfs_bandwidth_slack_period = 5 * NSEC_PER_MSEC;
5160
Ben Segalldb06e782013-10-16 11:16:17 -07005161/*
5162 * Are we near the end of the current quota period?
5163 *
5164 * Requires cfs_b->lock for hrtimer_expires_remaining to be safe against the
Thomas Gleixner4961b6e2015-04-14 21:09:05 +00005165 * hrtimer base being cleared by hrtimer_start. In the case of
Ben Segalldb06e782013-10-16 11:16:17 -07005166 * migrate_hrtimers, base is never cleared, so we are fine.
5167 */
Paul Turnerd8b49862011-07-21 09:43:41 -07005168static int runtime_refresh_within(struct cfs_bandwidth *cfs_b, u64 min_expire)
5169{
5170 struct hrtimer *refresh_timer = &cfs_b->period_timer;
5171 u64 remaining;
5172
5173 /* if the call-back is running a quota refresh is already occurring */
5174 if (hrtimer_callback_running(refresh_timer))
5175 return 1;
5176
5177 /* is a quota refresh about to occur? */
5178 remaining = ktime_to_ns(hrtimer_expires_remaining(refresh_timer));
5179 if (remaining < min_expire)
5180 return 1;
5181
5182 return 0;
5183}
5184
5185static void start_cfs_slack_bandwidth(struct cfs_bandwidth *cfs_b)
5186{
5187 u64 min_left = cfs_bandwidth_slack_period + min_bandwidth_expiration;
5188
5189 /* if there's a quota refresh soon don't bother with slack */
5190 if (runtime_refresh_within(cfs_b, min_left))
5191 return;
5192
Peter Zijlstra4cfafd32015-05-14 12:23:11 +02005193 hrtimer_start(&cfs_b->slack_timer,
5194 ns_to_ktime(cfs_bandwidth_slack_period),
5195 HRTIMER_MODE_REL);
Paul Turnerd8b49862011-07-21 09:43:41 -07005196}
5197
5198/* we know any runtime found here is valid as update_curr() precedes return */
5199static void __return_cfs_rq_runtime(struct cfs_rq *cfs_rq)
5200{
5201 struct cfs_bandwidth *cfs_b = tg_cfs_bandwidth(cfs_rq->tg);
5202 s64 slack_runtime = cfs_rq->runtime_remaining - min_cfs_rq_runtime;
5203
5204 if (slack_runtime <= 0)
5205 return;
5206
5207 raw_spin_lock(&cfs_b->lock);
5208 if (cfs_b->quota != RUNTIME_INF &&
5209 cfs_rq->runtime_expires == cfs_b->runtime_expires) {
5210 cfs_b->runtime += slack_runtime;
5211
5212 /* we are under rq->lock, defer unthrottling using a timer */
5213 if (cfs_b->runtime > sched_cfs_bandwidth_slice() &&
5214 !list_empty(&cfs_b->throttled_cfs_rq))
5215 start_cfs_slack_bandwidth(cfs_b);
5216 }
5217 raw_spin_unlock(&cfs_b->lock);
5218
5219 /* even if it's not valid for return we don't want to try again */
5220 cfs_rq->runtime_remaining -= slack_runtime;
5221}
5222
5223static __always_inline void return_cfs_rq_runtime(struct cfs_rq *cfs_rq)
5224{
Paul Turner56f570e2011-11-07 20:26:33 -08005225 if (!cfs_bandwidth_used())
5226 return;
5227
Paul Turnerfccfdc62011-11-07 20:26:34 -08005228 if (!cfs_rq->runtime_enabled || cfs_rq->nr_running)
Paul Turnerd8b49862011-07-21 09:43:41 -07005229 return;
5230
5231 __return_cfs_rq_runtime(cfs_rq);
5232}
5233
5234/*
5235 * This is done with a timer (instead of inline with bandwidth return) since
5236 * it's necessary to juggle rq->locks to unthrottle their respective cfs_rqs.
5237 */
5238static void do_sched_cfs_slack_timer(struct cfs_bandwidth *cfs_b)
5239{
5240 u64 runtime = 0, slice = sched_cfs_bandwidth_slice();
5241 u64 expires;
5242
5243 /* confirm we're still not at a refresh boundary */
Paul Turnerd8b49862011-07-21 09:43:41 -07005244 raw_spin_lock(&cfs_b->lock);
Ben Segalldb06e782013-10-16 11:16:17 -07005245 if (runtime_refresh_within(cfs_b, min_bandwidth_expiration)) {
5246 raw_spin_unlock(&cfs_b->lock);
5247 return;
5248 }
5249
Ben Segallc06f04c2014-06-20 15:21:20 -07005250 if (cfs_b->quota != RUNTIME_INF && cfs_b->runtime > slice)
Paul Turnerd8b49862011-07-21 09:43:41 -07005251 runtime = cfs_b->runtime;
Ben Segallc06f04c2014-06-20 15:21:20 -07005252
Paul Turnerd8b49862011-07-21 09:43:41 -07005253 expires = cfs_b->runtime_expires;
5254 raw_spin_unlock(&cfs_b->lock);
5255
5256 if (!runtime)
5257 return;
5258
5259 runtime = distribute_cfs_runtime(cfs_b, runtime, expires);
5260
5261 raw_spin_lock(&cfs_b->lock);
5262 if (expires == cfs_b->runtime_expires)
Ben Segallc06f04c2014-06-20 15:21:20 -07005263 cfs_b->runtime -= min(runtime, cfs_b->runtime);
Paul Turnerd8b49862011-07-21 09:43:41 -07005264 raw_spin_unlock(&cfs_b->lock);
5265}
5266
Paul Turnerd3d9dc32011-07-21 09:43:39 -07005267/*
5268 * When a group wakes up we want to make sure that its quota is not already
5269 * expired/exceeded, otherwise it may be allowed to steal additional ticks of
5270 * runtime as update_curr() throttling can not not trigger until it's on-rq.
5271 */
5272static void check_enqueue_throttle(struct cfs_rq *cfs_rq)
5273{
Paul Turner56f570e2011-11-07 20:26:33 -08005274 if (!cfs_bandwidth_used())
5275 return;
5276
Paul Turnerd3d9dc32011-07-21 09:43:39 -07005277 /* an active group must be handled by the update_curr()->put() path */
5278 if (!cfs_rq->runtime_enabled || cfs_rq->curr)
5279 return;
5280
5281 /* ensure the group is not already throttled */
5282 if (cfs_rq_throttled(cfs_rq))
5283 return;
5284
5285 /* update runtime allocation */
5286 account_cfs_rq_runtime(cfs_rq, 0);
5287 if (cfs_rq->runtime_remaining <= 0)
5288 throttle_cfs_rq(cfs_rq);
5289}
5290
Peter Zijlstra55e16d32016-06-22 15:14:26 +02005291static void sync_throttle(struct task_group *tg, int cpu)
5292{
5293 struct cfs_rq *pcfs_rq, *cfs_rq;
5294
5295 if (!cfs_bandwidth_used())
5296 return;
5297
5298 if (!tg->parent)
5299 return;
5300
5301 cfs_rq = tg->cfs_rq[cpu];
5302 pcfs_rq = tg->parent->cfs_rq[cpu];
5303
5304 cfs_rq->throttle_count = pcfs_rq->throttle_count;
Xunlei Pangb8922122016-07-09 15:54:22 +08005305 cfs_rq->throttled_clock_task = rq_clock_task(cpu_rq(cpu));
Peter Zijlstra55e16d32016-06-22 15:14:26 +02005306}
5307
Paul Turnerd3d9dc32011-07-21 09:43:39 -07005308/* conditionally throttle active cfs_rq's from put_prev_entity() */
Peter Zijlstra678d5712012-02-11 06:05:00 +01005309static bool check_cfs_rq_runtime(struct cfs_rq *cfs_rq)
Paul Turnerd3d9dc32011-07-21 09:43:39 -07005310{
Paul Turner56f570e2011-11-07 20:26:33 -08005311 if (!cfs_bandwidth_used())
Peter Zijlstra678d5712012-02-11 06:05:00 +01005312 return false;
Paul Turner56f570e2011-11-07 20:26:33 -08005313
Paul Turnerd3d9dc32011-07-21 09:43:39 -07005314 if (likely(!cfs_rq->runtime_enabled || cfs_rq->runtime_remaining > 0))
Peter Zijlstra678d5712012-02-11 06:05:00 +01005315 return false;
Paul Turnerd3d9dc32011-07-21 09:43:39 -07005316
5317 /*
5318 * it's possible for a throttled entity to be forced into a running
5319 * state (e.g. set_curr_task), in this case we're finished.
5320 */
5321 if (cfs_rq_throttled(cfs_rq))
Peter Zijlstra678d5712012-02-11 06:05:00 +01005322 return true;
Paul Turnerd3d9dc32011-07-21 09:43:39 -07005323
5324 throttle_cfs_rq(cfs_rq);
Peter Zijlstra678d5712012-02-11 06:05:00 +01005325 return true;
Paul Turnerd3d9dc32011-07-21 09:43:39 -07005326}
Peter Zijlstra029632f2011-10-25 10:00:11 +02005327
Peter Zijlstra029632f2011-10-25 10:00:11 +02005328static enum hrtimer_restart sched_cfs_slack_timer(struct hrtimer *timer)
5329{
5330 struct cfs_bandwidth *cfs_b =
5331 container_of(timer, struct cfs_bandwidth, slack_timer);
Peter Zijlstra77a4d1a2015-04-15 11:41:57 +02005332
Peter Zijlstra029632f2011-10-25 10:00:11 +02005333 do_sched_cfs_slack_timer(cfs_b);
5334
5335 return HRTIMER_NORESTART;
5336}
5337
5338static enum hrtimer_restart sched_cfs_period_timer(struct hrtimer *timer)
5339{
5340 struct cfs_bandwidth *cfs_b =
5341 container_of(timer, struct cfs_bandwidth, period_timer);
Peter Zijlstra029632f2011-10-25 10:00:11 +02005342 int overrun;
5343 int idle = 0;
5344
Ben Segall51f21762014-05-19 15:49:45 -07005345 raw_spin_lock(&cfs_b->lock);
Peter Zijlstra029632f2011-10-25 10:00:11 +02005346 for (;;) {
Peter Zijlstra77a4d1a2015-04-15 11:41:57 +02005347 overrun = hrtimer_forward_now(timer, cfs_b->period);
Peter Zijlstra029632f2011-10-25 10:00:11 +02005348 if (!overrun)
5349 break;
5350
5351 idle = do_sched_cfs_period_timer(cfs_b, overrun);
5352 }
Peter Zijlstra4cfafd32015-05-14 12:23:11 +02005353 if (idle)
5354 cfs_b->period_active = 0;
Ben Segall51f21762014-05-19 15:49:45 -07005355 raw_spin_unlock(&cfs_b->lock);
Peter Zijlstra029632f2011-10-25 10:00:11 +02005356
5357 return idle ? HRTIMER_NORESTART : HRTIMER_RESTART;
5358}
5359
5360void init_cfs_bandwidth(struct cfs_bandwidth *cfs_b)
5361{
5362 raw_spin_lock_init(&cfs_b->lock);
5363 cfs_b->runtime = 0;
5364 cfs_b->quota = RUNTIME_INF;
5365 cfs_b->period = ns_to_ktime(default_cfs_period());
5366
5367 INIT_LIST_HEAD(&cfs_b->throttled_cfs_rq);
Peter Zijlstra4cfafd32015-05-14 12:23:11 +02005368 hrtimer_init(&cfs_b->period_timer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS_PINNED);
Peter Zijlstra029632f2011-10-25 10:00:11 +02005369 cfs_b->period_timer.function = sched_cfs_period_timer;
5370 hrtimer_init(&cfs_b->slack_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
5371 cfs_b->slack_timer.function = sched_cfs_slack_timer;
5372}
5373
5374static void init_cfs_rq_runtime(struct cfs_rq *cfs_rq)
5375{
5376 cfs_rq->runtime_enabled = 0;
5377 INIT_LIST_HEAD(&cfs_rq->throttled_list);
Syed Rameez Mustafadddcab72016-09-07 16:18:27 -07005378 init_cfs_rq_hmp_stats(cfs_rq);
Peter Zijlstra029632f2011-10-25 10:00:11 +02005379}
5380
Peter Zijlstra77a4d1a2015-04-15 11:41:57 +02005381void start_cfs_bandwidth(struct cfs_bandwidth *cfs_b)
Peter Zijlstra029632f2011-10-25 10:00:11 +02005382{
Peter Zijlstra4cfafd32015-05-14 12:23:11 +02005383 lockdep_assert_held(&cfs_b->lock);
Peter Zijlstra029632f2011-10-25 10:00:11 +02005384
Peter Zijlstra4cfafd32015-05-14 12:23:11 +02005385 if (!cfs_b->period_active) {
5386 cfs_b->period_active = 1;
5387 hrtimer_forward_now(&cfs_b->period_timer, cfs_b->period);
5388 hrtimer_start_expires(&cfs_b->period_timer, HRTIMER_MODE_ABS_PINNED);
5389 }
Peter Zijlstra029632f2011-10-25 10:00:11 +02005390}
5391
5392static void destroy_cfs_bandwidth(struct cfs_bandwidth *cfs_b)
5393{
Tetsuo Handa7f1a1692014-12-25 15:51:21 +09005394 /* init_cfs_bandwidth() was not called */
5395 if (!cfs_b->throttled_cfs_rq.next)
5396 return;
5397
Peter Zijlstra029632f2011-10-25 10:00:11 +02005398 hrtimer_cancel(&cfs_b->period_timer);
5399 hrtimer_cancel(&cfs_b->slack_timer);
5400}
5401
Kirill Tkhai0e59bda2014-06-25 12:19:42 +04005402static void __maybe_unused update_runtime_enabled(struct rq *rq)
5403{
5404 struct cfs_rq *cfs_rq;
5405
5406 for_each_leaf_cfs_rq(rq, cfs_rq) {
5407 struct cfs_bandwidth *cfs_b = &cfs_rq->tg->cfs_bandwidth;
5408
5409 raw_spin_lock(&cfs_b->lock);
5410 cfs_rq->runtime_enabled = cfs_b->quota != RUNTIME_INF;
5411 raw_spin_unlock(&cfs_b->lock);
5412 }
5413}
5414
Arnd Bergmann38dc3342013-01-25 14:14:22 +00005415static void __maybe_unused unthrottle_offline_cfs_rqs(struct rq *rq)
Peter Zijlstra029632f2011-10-25 10:00:11 +02005416{
5417 struct cfs_rq *cfs_rq;
5418
5419 for_each_leaf_cfs_rq(rq, cfs_rq) {
Peter Zijlstra029632f2011-10-25 10:00:11 +02005420 if (!cfs_rq->runtime_enabled)
5421 continue;
5422
5423 /*
5424 * clock_task is not advancing so we just need to make sure
5425 * there's some valid quota amount
5426 */
Ben Segall51f21762014-05-19 15:49:45 -07005427 cfs_rq->runtime_remaining = 1;
Kirill Tkhai0e59bda2014-06-25 12:19:42 +04005428 /*
5429 * Offline rq is schedulable till cpu is completely disabled
5430 * in take_cpu_down(), so we prevent new cfs throttling here.
5431 */
5432 cfs_rq->runtime_enabled = 0;
5433
Peter Zijlstra029632f2011-10-25 10:00:11 +02005434 if (cfs_rq_throttled(cfs_rq))
5435 unthrottle_cfs_rq(cfs_rq);
5436 }
5437}
5438
5439#else /* CONFIG_CFS_BANDWIDTH */
Paul Turnerf1b17282012-10-04 13:18:31 +02005440static inline u64 cfs_rq_clock_task(struct cfs_rq *cfs_rq)
5441{
Frederic Weisbecker78becc22013-04-12 01:51:02 +02005442 return rq_clock_task(rq_of(cfs_rq));
Paul Turnerf1b17282012-10-04 13:18:31 +02005443}
5444
Peter Zijlstra9dbdb152013-11-18 18:27:06 +01005445static void account_cfs_rq_runtime(struct cfs_rq *cfs_rq, u64 delta_exec) {}
Peter Zijlstra678d5712012-02-11 06:05:00 +01005446static bool check_cfs_rq_runtime(struct cfs_rq *cfs_rq) { return false; }
Paul Turnerd3d9dc32011-07-21 09:43:39 -07005447static void check_enqueue_throttle(struct cfs_rq *cfs_rq) {}
Peter Zijlstra55e16d32016-06-22 15:14:26 +02005448static inline void sync_throttle(struct task_group *tg, int cpu) {}
Peter Zijlstra6c16a6d2012-03-21 13:07:16 -07005449static __always_inline void return_cfs_rq_runtime(struct cfs_rq *cfs_rq) {}
Paul Turner85dac902011-07-21 09:43:33 -07005450
5451static inline int cfs_rq_throttled(struct cfs_rq *cfs_rq)
5452{
5453 return 0;
5454}
Paul Turner64660c82011-07-21 09:43:36 -07005455
5456static inline int throttled_hierarchy(struct cfs_rq *cfs_rq)
5457{
5458 return 0;
5459}
5460
5461static inline int throttled_lb_pair(struct task_group *tg,
5462 int src_cpu, int dest_cpu)
5463{
5464 return 0;
5465}
Peter Zijlstra029632f2011-10-25 10:00:11 +02005466
5467void init_cfs_bandwidth(struct cfs_bandwidth *cfs_b) {}
5468
5469#ifdef CONFIG_FAIR_GROUP_SCHED
5470static void init_cfs_rq_runtime(struct cfs_rq *cfs_rq) {}
Paul Turnerab84d312011-07-21 09:43:28 -07005471#endif
5472
Peter Zijlstra029632f2011-10-25 10:00:11 +02005473static inline struct cfs_bandwidth *tg_cfs_bandwidth(struct task_group *tg)
5474{
5475 return NULL;
5476}
5477static inline void destroy_cfs_bandwidth(struct cfs_bandwidth *cfs_b) {}
Kirill Tkhai0e59bda2014-06-25 12:19:42 +04005478static inline void update_runtime_enabled(struct rq *rq) {}
Peter Boonstoppela4c96ae2012-08-09 15:34:47 -07005479static inline void unthrottle_offline_cfs_rqs(struct rq *rq) {}
Peter Zijlstra029632f2011-10-25 10:00:11 +02005480
5481#endif /* CONFIG_CFS_BANDWIDTH */
5482
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02005483/**************************************************
5484 * CFS operations on tasks:
5485 */
5486
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01005487#ifdef CONFIG_SCHED_HRTICK
5488static void hrtick_start_fair(struct rq *rq, struct task_struct *p)
5489{
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01005490 struct sched_entity *se = &p->se;
5491 struct cfs_rq *cfs_rq = cfs_rq_of(se);
5492
Peter Zijlstra9148a3a2016-09-20 22:34:51 +02005493 SCHED_WARN_ON(task_rq(p) != rq);
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01005494
Srivatsa Vaddagiri8bf46a32016-09-16 18:28:51 -07005495 if (rq->cfs.h_nr_running > 1) {
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01005496 u64 slice = sched_slice(cfs_rq, se);
5497 u64 ran = se->sum_exec_runtime - se->prev_sum_exec_runtime;
5498 s64 delta = slice - ran;
5499
5500 if (delta < 0) {
5501 if (rq->curr == p)
Kirill Tkhai88751252014-06-29 00:03:57 +04005502 resched_curr(rq);
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01005503 return;
5504 }
Peter Zijlstra31656512008-07-18 18:01:23 +02005505 hrtick_start(rq, delta);
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01005506 }
5507}
Peter Zijlstraa4c2f002008-10-17 19:27:03 +02005508
5509/*
5510 * called from enqueue/dequeue and updates the hrtick when the
5511 * current task is from our class and nr_running is low enough
5512 * to matter.
5513 */
5514static void hrtick_update(struct rq *rq)
5515{
5516 struct task_struct *curr = rq->curr;
5517
Mike Galbraithb39e66e2011-11-22 15:20:07 +01005518 if (!hrtick_enabled(rq) || curr->sched_class != &fair_sched_class)
Peter Zijlstraa4c2f002008-10-17 19:27:03 +02005519 return;
5520
5521 if (cfs_rq_of(&curr->se)->nr_running < sched_nr_latency)
5522 hrtick_start_fair(rq, curr);
5523}
Dhaval Giani55e12e52008-06-24 23:39:43 +05305524#else /* !CONFIG_SCHED_HRTICK */
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01005525static inline void
5526hrtick_start_fair(struct rq *rq, struct task_struct *p)
5527{
5528}
Peter Zijlstraa4c2f002008-10-17 19:27:03 +02005529
5530static inline void hrtick_update(struct rq *rq)
5531{
5532}
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01005533#endif
5534
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02005535/*
5536 * The enqueue_task method is called before nr_running is
5537 * increased. Here we update the fair scheduling stats and
5538 * then put the task into the rbtree:
5539 */
Thomas Gleixnerea87bb72010-01-20 20:58:57 +00005540static void
Peter Zijlstra371fd7e2010-03-24 16:38:48 +01005541enqueue_task_fair(struct rq *rq, struct task_struct *p, int flags)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02005542{
5543 struct cfs_rq *cfs_rq;
Peter Zijlstra62fb1852008-02-25 17:34:02 +01005544 struct sched_entity *se = &p->se;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02005545
Rafael J. Wysocki8c34ab12016-09-09 23:59:33 +02005546 /*
5547 * If in_iowait is set, the code below may not trigger any cpufreq
5548 * utilization updates, so do it here explicitly with the IOWAIT flag
5549 * passed.
5550 */
5551 if (p->in_iowait)
5552 cpufreq_update_this_cpu(rq, SCHED_CPUFREQ_IOWAIT);
5553
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02005554 for_each_sched_entity(se) {
Peter Zijlstra62fb1852008-02-25 17:34:02 +01005555 if (se->on_rq)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02005556 break;
5557 cfs_rq = cfs_rq_of(se);
Peter Zijlstra88ec22d2009-12-16 18:04:41 +01005558 enqueue_entity(cfs_rq, se, flags);
Paul Turner85dac902011-07-21 09:43:33 -07005559
5560 /*
5561 * end evaluation on encountering a throttled cfs_rq
5562 *
5563 * note: in the case of encountering a throttled cfs_rq we will
5564 * post the final h_nr_running increment below.
Peter Zijlstrae210bff2016-06-16 18:51:48 +02005565 */
Paul Turner85dac902011-07-21 09:43:33 -07005566 if (cfs_rq_throttled(cfs_rq))
5567 break;
Paul Turner953bfcd2011-07-21 09:43:27 -07005568 cfs_rq->h_nr_running++;
Syed Rameez Mustafadddcab72016-09-07 16:18:27 -07005569 inc_cfs_rq_hmp_stats(cfs_rq, p, 1);
Paul Turner85dac902011-07-21 09:43:33 -07005570
Peter Zijlstra88ec22d2009-12-16 18:04:41 +01005571 flags = ENQUEUE_WAKEUP;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02005572 }
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01005573
Peter Zijlstra2069dd72010-11-15 15:47:00 -08005574 for_each_sched_entity(se) {
Lin Ming0f317142011-07-22 09:14:31 +08005575 cfs_rq = cfs_rq_of(se);
Paul Turner953bfcd2011-07-21 09:43:27 -07005576 cfs_rq->h_nr_running++;
Syed Rameez Mustafadddcab72016-09-07 16:18:27 -07005577 inc_cfs_rq_hmp_stats(cfs_rq, p, 1);
Peter Zijlstra2069dd72010-11-15 15:47:00 -08005578
Paul Turner85dac902011-07-21 09:43:33 -07005579 if (cfs_rq_throttled(cfs_rq))
5580 break;
5581
Yuyang Du9d89c252015-07-15 08:04:37 +08005582 update_load_avg(se, 1);
Linus Torvalds17bc14b2012-12-14 07:20:43 -08005583 update_cfs_shares(cfs_rq);
Peter Zijlstra2069dd72010-11-15 15:47:00 -08005584 }
5585
Syed Rameez Mustafadddcab72016-09-07 16:18:27 -07005586 if (!se) {
Kirill Tkhai72465442014-05-09 03:00:14 +04005587 add_nr_running(rq, 1);
Syed Rameez Mustafadddcab72016-09-07 16:18:27 -07005588 inc_rq_hmp_stats(rq, p, 1);
5589 }
Yuyang Ducd126af2015-07-15 08:04:36 +08005590
Peter Zijlstraa4c2f002008-10-17 19:27:03 +02005591 hrtick_update(rq);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02005592}
5593
Venkatesh Pallipadi2f368252011-04-14 10:30:53 -07005594static void set_next_buddy(struct sched_entity *se);
5595
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02005596/*
5597 * The dequeue_task method is called before nr_running is
5598 * decreased. We remove the task from the rbtree and
5599 * update the fair scheduling stats:
5600 */
Peter Zijlstra371fd7e2010-03-24 16:38:48 +01005601static void dequeue_task_fair(struct rq *rq, struct task_struct *p, int flags)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02005602{
5603 struct cfs_rq *cfs_rq;
Peter Zijlstra62fb1852008-02-25 17:34:02 +01005604 struct sched_entity *se = &p->se;
Venkatesh Pallipadi2f368252011-04-14 10:30:53 -07005605 int task_sleep = flags & DEQUEUE_SLEEP;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02005606
5607 for_each_sched_entity(se) {
5608 cfs_rq = cfs_rq_of(se);
Peter Zijlstra371fd7e2010-03-24 16:38:48 +01005609 dequeue_entity(cfs_rq, se, flags);
Paul Turner85dac902011-07-21 09:43:33 -07005610
5611 /*
5612 * end evaluation on encountering a throttled cfs_rq
5613 *
5614 * note: in the case of encountering a throttled cfs_rq we will
5615 * post the final h_nr_running decrement below.
5616 */
5617 if (cfs_rq_throttled(cfs_rq))
5618 break;
Paul Turner953bfcd2011-07-21 09:43:27 -07005619 cfs_rq->h_nr_running--;
Syed Rameez Mustafadddcab72016-09-07 16:18:27 -07005620 dec_cfs_rq_hmp_stats(cfs_rq, p, 1);
Peter Zijlstra2069dd72010-11-15 15:47:00 -08005621
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02005622 /* Don't dequeue parent if it has other entities besides us */
Venkatesh Pallipadi2f368252011-04-14 10:30:53 -07005623 if (cfs_rq->load.weight) {
Konstantin Khlebnikov754bd592016-06-16 15:57:15 +03005624 /* Avoid re-evaluating load for this entity: */
5625 se = parent_entity(se);
Venkatesh Pallipadi2f368252011-04-14 10:30:53 -07005626 /*
5627 * Bias pick_next to pick a task from this cfs_rq, as
5628 * p is sleeping when it is within its sched_slice.
5629 */
Konstantin Khlebnikov754bd592016-06-16 15:57:15 +03005630 if (task_sleep && se && !throttled_hierarchy(cfs_rq))
5631 set_next_buddy(se);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02005632 break;
Venkatesh Pallipadi2f368252011-04-14 10:30:53 -07005633 }
Peter Zijlstra371fd7e2010-03-24 16:38:48 +01005634 flags |= DEQUEUE_SLEEP;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02005635 }
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01005636
Peter Zijlstra2069dd72010-11-15 15:47:00 -08005637 for_each_sched_entity(se) {
Lin Ming0f317142011-07-22 09:14:31 +08005638 cfs_rq = cfs_rq_of(se);
Paul Turner953bfcd2011-07-21 09:43:27 -07005639 cfs_rq->h_nr_running--;
Syed Rameez Mustafadddcab72016-09-07 16:18:27 -07005640 dec_cfs_rq_hmp_stats(cfs_rq, p, 1);
Peter Zijlstra2069dd72010-11-15 15:47:00 -08005641
Paul Turner85dac902011-07-21 09:43:33 -07005642 if (cfs_rq_throttled(cfs_rq))
5643 break;
5644
Yuyang Du9d89c252015-07-15 08:04:37 +08005645 update_load_avg(se, 1);
Linus Torvalds17bc14b2012-12-14 07:20:43 -08005646 update_cfs_shares(cfs_rq);
Peter Zijlstra2069dd72010-11-15 15:47:00 -08005647 }
5648
Syed Rameez Mustafadddcab72016-09-07 16:18:27 -07005649 if (!se) {
Kirill Tkhai72465442014-05-09 03:00:14 +04005650 sub_nr_running(rq, 1);
Syed Rameez Mustafadddcab72016-09-07 16:18:27 -07005651 dec_rq_hmp_stats(rq, p, 1);
5652 }
Yuyang Ducd126af2015-07-15 08:04:36 +08005653
Peter Zijlstraa4c2f002008-10-17 19:27:03 +02005654 hrtick_update(rq);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02005655}
5656
Gregory Haskinse7693a32008-01-25 21:08:09 +01005657#ifdef CONFIG_SMP
Peter Zijlstra10e2f1a2016-05-09 10:38:05 +02005658
5659/* Working cpumask for: load_balance, load_balance_newidle. */
5660DEFINE_PER_CPU(cpumask_var_t, load_balance_mask);
5661DEFINE_PER_CPU(cpumask_var_t, select_idle_mask);
5662
Frederic Weisbecker9fd81dd2016-04-19 17:36:51 +02005663#ifdef CONFIG_NO_HZ_COMMON
Peter Zijlstra3289bdb2015-04-14 13:19:42 +02005664/*
5665 * per rq 'load' arrray crap; XXX kill this.
5666 */
5667
5668/*
Peter Zijlstrad937cdc2015-10-19 13:49:30 +02005669 * The exact cpuload calculated at every tick would be:
Peter Zijlstra3289bdb2015-04-14 13:19:42 +02005670 *
Peter Zijlstrad937cdc2015-10-19 13:49:30 +02005671 * load' = (1 - 1/2^i) * load + (1/2^i) * cur_load
5672 *
5673 * If a cpu misses updates for n ticks (as it was idle) and update gets
5674 * called on the n+1-th tick when cpu may be busy, then we have:
5675 *
5676 * load_n = (1 - 1/2^i)^n * load_0
5677 * load_n+1 = (1 - 1/2^i) * load_n + (1/2^i) * cur_load
Peter Zijlstra3289bdb2015-04-14 13:19:42 +02005678 *
5679 * decay_load_missed() below does efficient calculation of
Peter Zijlstrad937cdc2015-10-19 13:49:30 +02005680 *
5681 * load' = (1 - 1/2^i)^n * load
5682 *
5683 * Because x^(n+m) := x^n * x^m we can decompose any x^n in power-of-2 factors.
5684 * This allows us to precompute the above in said factors, thereby allowing the
5685 * reduction of an arbitrary n in O(log_2 n) steps. (See also
5686 * fixed_power_int())
Peter Zijlstra3289bdb2015-04-14 13:19:42 +02005687 *
5688 * The calculation is approximated on a 128 point scale.
Peter Zijlstra3289bdb2015-04-14 13:19:42 +02005689 */
5690#define DEGRADE_SHIFT 7
Peter Zijlstrad937cdc2015-10-19 13:49:30 +02005691
5692static const u8 degrade_zero_ticks[CPU_LOAD_IDX_MAX] = {0, 8, 32, 64, 128};
5693static const u8 degrade_factor[CPU_LOAD_IDX_MAX][DEGRADE_SHIFT + 1] = {
5694 { 0, 0, 0, 0, 0, 0, 0, 0 },
5695 { 64, 32, 8, 0, 0, 0, 0, 0 },
5696 { 96, 72, 40, 12, 1, 0, 0, 0 },
5697 { 112, 98, 75, 43, 15, 1, 0, 0 },
5698 { 120, 112, 98, 76, 45, 16, 2, 0 }
5699};
Peter Zijlstra3289bdb2015-04-14 13:19:42 +02005700
5701/*
5702 * Update cpu_load for any missed ticks, due to tickless idle. The backlog
5703 * would be when CPU is idle and so we just decay the old load without
5704 * adding any new load.
5705 */
5706static unsigned long
5707decay_load_missed(unsigned long load, unsigned long missed_updates, int idx)
5708{
5709 int j = 0;
5710
5711 if (!missed_updates)
5712 return load;
5713
5714 if (missed_updates >= degrade_zero_ticks[idx])
5715 return 0;
5716
5717 if (idx == 1)
5718 return load >> missed_updates;
5719
5720 while (missed_updates) {
5721 if (missed_updates % 2)
5722 load = (load * degrade_factor[idx][j]) >> DEGRADE_SHIFT;
5723
5724 missed_updates >>= 1;
5725 j++;
5726 }
5727 return load;
5728}
Frederic Weisbecker9fd81dd2016-04-19 17:36:51 +02005729#endif /* CONFIG_NO_HZ_COMMON */
Peter Zijlstra3289bdb2015-04-14 13:19:42 +02005730
Byungchul Park59543272015-10-14 18:47:35 +09005731/**
Frederic Weisbeckercee1afc2016-04-13 15:56:50 +02005732 * __cpu_load_update - update the rq->cpu_load[] statistics
Byungchul Park59543272015-10-14 18:47:35 +09005733 * @this_rq: The rq to update statistics for
5734 * @this_load: The current load
5735 * @pending_updates: The number of missed updates
Byungchul Park59543272015-10-14 18:47:35 +09005736 *
Peter Zijlstra3289bdb2015-04-14 13:19:42 +02005737 * Update rq->cpu_load[] statistics. This function is usually called every
Byungchul Park59543272015-10-14 18:47:35 +09005738 * scheduler tick (TICK_NSEC).
5739 *
5740 * This function computes a decaying average:
5741 *
5742 * load[i]' = (1 - 1/2^i) * load[i] + (1/2^i) * load
5743 *
5744 * Because of NOHZ it might not get called on every tick which gives need for
5745 * the @pending_updates argument.
5746 *
5747 * load[i]_n = (1 - 1/2^i) * load[i]_n-1 + (1/2^i) * load_n-1
5748 * = A * load[i]_n-1 + B ; A := (1 - 1/2^i), B := (1/2^i) * load
5749 * = A * (A * load[i]_n-2 + B) + B
5750 * = A * (A * (A * load[i]_n-3 + B) + B) + B
5751 * = A^3 * load[i]_n-3 + (A^2 + A + 1) * B
5752 * = A^n * load[i]_0 + (A^(n-1) + A^(n-2) + ... + 1) * B
5753 * = A^n * load[i]_0 + ((1 - A^n) / (1 - A)) * B
5754 * = (1 - 1/2^i)^n * (load[i]_0 - load) + load
5755 *
5756 * In the above we've assumed load_n := load, which is true for NOHZ_FULL as
5757 * any change in load would have resulted in the tick being turned back on.
5758 *
5759 * For regular NOHZ, this reduces to:
5760 *
5761 * load[i]_n = (1 - 1/2^i)^n * load[i]_0
5762 *
5763 * see decay_load_misses(). For NOHZ_FULL we get to subtract and add the extra
Frederic Weisbecker1f419062016-04-13 15:56:51 +02005764 * term.
Peter Zijlstra3289bdb2015-04-14 13:19:42 +02005765 */
Frederic Weisbecker1f419062016-04-13 15:56:51 +02005766static void cpu_load_update(struct rq *this_rq, unsigned long this_load,
5767 unsigned long pending_updates)
Peter Zijlstra3289bdb2015-04-14 13:19:42 +02005768{
Frederic Weisbecker9fd81dd2016-04-19 17:36:51 +02005769 unsigned long __maybe_unused tickless_load = this_rq->cpu_load[0];
Peter Zijlstra3289bdb2015-04-14 13:19:42 +02005770 int i, scale;
5771
5772 this_rq->nr_load_updates++;
5773
5774 /* Update our load: */
5775 this_rq->cpu_load[0] = this_load; /* Fasttrack for idx 0 */
5776 for (i = 1, scale = 2; i < CPU_LOAD_IDX_MAX; i++, scale += scale) {
5777 unsigned long old_load, new_load;
5778
5779 /* scale is effectively 1 << i now, and >> i divides by scale */
5780
Byungchul Park7400d3b2016-01-15 16:07:49 +09005781 old_load = this_rq->cpu_load[i];
Frederic Weisbecker9fd81dd2016-04-19 17:36:51 +02005782#ifdef CONFIG_NO_HZ_COMMON
Peter Zijlstra3289bdb2015-04-14 13:19:42 +02005783 old_load = decay_load_missed(old_load, pending_updates - 1, i);
Byungchul Park7400d3b2016-01-15 16:07:49 +09005784 if (tickless_load) {
5785 old_load -= decay_load_missed(tickless_load, pending_updates - 1, i);
5786 /*
5787 * old_load can never be a negative value because a
5788 * decayed tickless_load cannot be greater than the
5789 * original tickless_load.
5790 */
5791 old_load += tickless_load;
5792 }
Frederic Weisbecker9fd81dd2016-04-19 17:36:51 +02005793#endif
Peter Zijlstra3289bdb2015-04-14 13:19:42 +02005794 new_load = this_load;
5795 /*
5796 * Round up the averaging division if load is increasing. This
5797 * prevents us from getting stuck on 9 if the load is 10, for
5798 * example.
5799 */
5800 if (new_load > old_load)
5801 new_load += scale - 1;
5802
5803 this_rq->cpu_load[i] = (old_load * (scale - 1) + new_load) >> i;
5804 }
5805
5806 sched_avg_update(this_rq);
5807}
5808
Yuyang Du7ea241a2015-07-15 08:04:42 +08005809/* Used instead of source_load when we know the type == 0 */
5810static unsigned long weighted_cpuload(const int cpu)
5811{
5812 return cfs_rq_runnable_load_avg(&cpu_rq(cpu)->cfs);
5813}
5814
Peter Zijlstra3289bdb2015-04-14 13:19:42 +02005815#ifdef CONFIG_NO_HZ_COMMON
Frederic Weisbecker1f419062016-04-13 15:56:51 +02005816/*
5817 * There is no sane way to deal with nohz on smp when using jiffies because the
5818 * cpu doing the jiffies update might drift wrt the cpu doing the jiffy reading
5819 * causing off-by-one errors in observed deltas; {0,2} instead of {1,1}.
5820 *
5821 * Therefore we need to avoid the delta approach from the regular tick when
5822 * possible since that would seriously skew the load calculation. This is why we
5823 * use cpu_load_update_periodic() for CPUs out of nohz. However we'll rely on
5824 * jiffies deltas for updates happening while in nohz mode (idle ticks, idle
5825 * loop exit, nohz_idle_balance, nohz full exit...)
5826 *
5827 * This means we might still be one tick off for nohz periods.
5828 */
5829
5830static void cpu_load_update_nohz(struct rq *this_rq,
5831 unsigned long curr_jiffies,
5832 unsigned long load)
Frederic Weisbeckerbe68a682016-01-13 17:01:29 +01005833{
5834 unsigned long pending_updates;
5835
5836 pending_updates = curr_jiffies - this_rq->last_load_update_tick;
5837 if (pending_updates) {
5838 this_rq->last_load_update_tick = curr_jiffies;
5839 /*
5840 * In the regular NOHZ case, we were idle, this means load 0.
5841 * In the NOHZ_FULL case, we were non-idle, we should consider
5842 * its weighted load.
5843 */
Frederic Weisbecker1f419062016-04-13 15:56:51 +02005844 cpu_load_update(this_rq, load, pending_updates);
Frederic Weisbeckerbe68a682016-01-13 17:01:29 +01005845 }
5846}
5847
Peter Zijlstra3289bdb2015-04-14 13:19:42 +02005848/*
Peter Zijlstra3289bdb2015-04-14 13:19:42 +02005849 * Called from nohz_idle_balance() to update the load ratings before doing the
5850 * idle balance.
5851 */
Frederic Weisbeckercee1afc2016-04-13 15:56:50 +02005852static void cpu_load_update_idle(struct rq *this_rq)
Peter Zijlstra3289bdb2015-04-14 13:19:42 +02005853{
Peter Zijlstra3289bdb2015-04-14 13:19:42 +02005854 /*
5855 * bail if there's load or we're actually up-to-date.
5856 */
Frederic Weisbeckerbe68a682016-01-13 17:01:29 +01005857 if (weighted_cpuload(cpu_of(this_rq)))
Peter Zijlstra3289bdb2015-04-14 13:19:42 +02005858 return;
5859
Frederic Weisbecker1f419062016-04-13 15:56:51 +02005860 cpu_load_update_nohz(this_rq, READ_ONCE(jiffies), 0);
Peter Zijlstra3289bdb2015-04-14 13:19:42 +02005861}
5862
5863/*
Frederic Weisbecker1f419062016-04-13 15:56:51 +02005864 * Record CPU load on nohz entry so we know the tickless load to account
5865 * on nohz exit. cpu_load[0] happens then to be updated more frequently
5866 * than other cpu_load[idx] but it should be fine as cpu_load readers
5867 * shouldn't rely into synchronized cpu_load[*] updates.
Peter Zijlstra3289bdb2015-04-14 13:19:42 +02005868 */
Frederic Weisbecker1f419062016-04-13 15:56:51 +02005869void cpu_load_update_nohz_start(void)
Peter Zijlstra3289bdb2015-04-14 13:19:42 +02005870{
5871 struct rq *this_rq = this_rq();
Frederic Weisbecker1f419062016-04-13 15:56:51 +02005872
5873 /*
5874 * This is all lockless but should be fine. If weighted_cpuload changes
5875 * concurrently we'll exit nohz. And cpu_load write can race with
5876 * cpu_load_update_idle() but both updater would be writing the same.
5877 */
5878 this_rq->cpu_load[0] = weighted_cpuload(cpu_of(this_rq));
5879}
5880
5881/*
5882 * Account the tickless load in the end of a nohz frame.
5883 */
5884void cpu_load_update_nohz_stop(void)
5885{
Jason Low316c1608d2015-04-28 13:00:20 -07005886 unsigned long curr_jiffies = READ_ONCE(jiffies);
Frederic Weisbecker1f419062016-04-13 15:56:51 +02005887 struct rq *this_rq = this_rq();
5888 unsigned long load;
Peter Zijlstra3289bdb2015-04-14 13:19:42 +02005889
5890 if (curr_jiffies == this_rq->last_load_update_tick)
5891 return;
5892
Frederic Weisbecker1f419062016-04-13 15:56:51 +02005893 load = weighted_cpuload(cpu_of(this_rq));
Peter Zijlstra3289bdb2015-04-14 13:19:42 +02005894 raw_spin_lock(&this_rq->lock);
Matt Flemingb52fad22016-05-03 20:46:54 +01005895 update_rq_clock(this_rq);
Frederic Weisbecker1f419062016-04-13 15:56:51 +02005896 cpu_load_update_nohz(this_rq, curr_jiffies, load);
Peter Zijlstra3289bdb2015-04-14 13:19:42 +02005897 raw_spin_unlock(&this_rq->lock);
5898}
Frederic Weisbecker1f419062016-04-13 15:56:51 +02005899#else /* !CONFIG_NO_HZ_COMMON */
5900static inline void cpu_load_update_nohz(struct rq *this_rq,
5901 unsigned long curr_jiffies,
5902 unsigned long load) { }
5903#endif /* CONFIG_NO_HZ_COMMON */
5904
5905static void cpu_load_update_periodic(struct rq *this_rq, unsigned long load)
5906{
Frederic Weisbecker9fd81dd2016-04-19 17:36:51 +02005907#ifdef CONFIG_NO_HZ_COMMON
Frederic Weisbecker1f419062016-04-13 15:56:51 +02005908 /* See the mess around cpu_load_update_nohz(). */
5909 this_rq->last_load_update_tick = READ_ONCE(jiffies);
Frederic Weisbecker9fd81dd2016-04-19 17:36:51 +02005910#endif
Frederic Weisbecker1f419062016-04-13 15:56:51 +02005911 cpu_load_update(this_rq, load, 1);
5912}
Peter Zijlstra3289bdb2015-04-14 13:19:42 +02005913
5914/*
5915 * Called from scheduler_tick()
5916 */
Frederic Weisbeckercee1afc2016-04-13 15:56:50 +02005917void cpu_load_update_active(struct rq *this_rq)
Peter Zijlstra3289bdb2015-04-14 13:19:42 +02005918{
Yuyang Du7ea241a2015-07-15 08:04:42 +08005919 unsigned long load = weighted_cpuload(cpu_of(this_rq));
Frederic Weisbecker1f419062016-04-13 15:56:51 +02005920
5921 if (tick_nohz_tick_stopped())
5922 cpu_load_update_nohz(this_rq, READ_ONCE(jiffies), load);
5923 else
5924 cpu_load_update_periodic(this_rq, load);
Peter Zijlstra3289bdb2015-04-14 13:19:42 +02005925}
5926
Peter Zijlstra029632f2011-10-25 10:00:11 +02005927/*
5928 * Return a low guess at the load of a migration-source cpu weighted
5929 * according to the scheduling class and "nice" value.
5930 *
5931 * We want to under-estimate the load of migration sources, to
5932 * balance conservatively.
5933 */
5934static unsigned long source_load(int cpu, int type)
5935{
5936 struct rq *rq = cpu_rq(cpu);
5937 unsigned long total = weighted_cpuload(cpu);
5938
5939 if (type == 0 || !sched_feat(LB_BIAS))
5940 return total;
5941
5942 return min(rq->cpu_load[type-1], total);
5943}
5944
5945/*
5946 * Return a high guess at the load of a migration-target cpu weighted
5947 * according to the scheduling class and "nice" value.
5948 */
5949static unsigned long target_load(int cpu, int type)
5950{
5951 struct rq *rq = cpu_rq(cpu);
5952 unsigned long total = weighted_cpuload(cpu);
5953
5954 if (type == 0 || !sched_feat(LB_BIAS))
5955 return total;
5956
5957 return max(rq->cpu_load[type-1], total);
5958}
5959
Nicolas Pitreced549f2014-05-26 18:19:38 -04005960static unsigned long capacity_of(int cpu)
Peter Zijlstra029632f2011-10-25 10:00:11 +02005961{
Nicolas Pitreced549f2014-05-26 18:19:38 -04005962 return cpu_rq(cpu)->cpu_capacity;
Peter Zijlstra029632f2011-10-25 10:00:11 +02005963}
5964
Vincent Guittotca6d75e2015-02-27 16:54:09 +01005965static unsigned long capacity_orig_of(int cpu)
5966{
5967 return cpu_rq(cpu)->cpu_capacity_orig;
5968}
5969
Peter Zijlstra029632f2011-10-25 10:00:11 +02005970static unsigned long cpu_avg_load_per_task(int cpu)
5971{
5972 struct rq *rq = cpu_rq(cpu);
Jason Low316c1608d2015-04-28 13:00:20 -07005973 unsigned long nr_running = READ_ONCE(rq->cfs.h_nr_running);
Yuyang Du7ea241a2015-07-15 08:04:42 +08005974 unsigned long load_avg = weighted_cpuload(cpu);
Peter Zijlstra029632f2011-10-25 10:00:11 +02005975
5976 if (nr_running)
Alex Shib92486c2013-06-20 10:18:50 +08005977 return load_avg / nr_running;
Peter Zijlstra029632f2011-10-25 10:00:11 +02005978
5979 return 0;
5980}
5981
Peter Zijlstrabb3469a2008-06-27 13:41:27 +02005982#ifdef CONFIG_FAIR_GROUP_SCHED
Peter Zijlstraf5bfb7d2008-06-27 13:41:39 +02005983/*
5984 * effective_load() calculates the load change as seen from the root_task_group
5985 *
5986 * Adding load to a group doesn't make a group heavier, but can cause movement
5987 * of group shares between cpus. Assuming the shares were perfectly aligned one
5988 * can calculate the shift in shares.
Peter Zijlstracf5f0ac2011-10-13 16:52:28 +02005989 *
5990 * Calculate the effective load difference if @wl is added (subtracted) to @tg
5991 * on this @cpu and results in a total addition (subtraction) of @wg to the
5992 * total group weight.
5993 *
5994 * Given a runqueue weight distribution (rw_i) we can compute a shares
5995 * distribution (s_i) using:
5996 *
5997 * s_i = rw_i / \Sum rw_j (1)
5998 *
5999 * Suppose we have 4 CPUs and our @tg is a direct child of the root group and
6000 * has 7 equal weight tasks, distributed as below (rw_i), with the resulting
6001 * shares distribution (s_i):
6002 *
6003 * rw_i = { 2, 4, 1, 0 }
6004 * s_i = { 2/7, 4/7, 1/7, 0 }
6005 *
6006 * As per wake_affine() we're interested in the load of two CPUs (the CPU the
6007 * task used to run on and the CPU the waker is running on), we need to
6008 * compute the effect of waking a task on either CPU and, in case of a sync
6009 * wakeup, compute the effect of the current task going to sleep.
6010 *
6011 * So for a change of @wl to the local @cpu with an overall group weight change
6012 * of @wl we can compute the new shares distribution (s'_i) using:
6013 *
6014 * s'_i = (rw_i + @wl) / (@wg + \Sum rw_j) (2)
6015 *
6016 * Suppose we're interested in CPUs 0 and 1, and want to compute the load
6017 * differences in waking a task to CPU 0. The additional task changes the
6018 * weight and shares distributions like:
6019 *
6020 * rw'_i = { 3, 4, 1, 0 }
6021 * s'_i = { 3/8, 4/8, 1/8, 0 }
6022 *
6023 * We can then compute the difference in effective weight by using:
6024 *
6025 * dw_i = S * (s'_i - s_i) (3)
6026 *
6027 * Where 'S' is the group weight as seen by its parent.
6028 *
6029 * Therefore the effective change in loads on CPU 0 would be 5/56 (3/8 - 2/7)
6030 * times the weight of the group. The effect on CPU 1 would be -4/56 (4/8 -
6031 * 4/7) times the weight of the group.
Peter Zijlstraf5bfb7d2008-06-27 13:41:39 +02006032 */
Peter Zijlstra2069dd72010-11-15 15:47:00 -08006033static long effective_load(struct task_group *tg, int cpu, long wl, long wg)
Peter Zijlstrabb3469a2008-06-27 13:41:27 +02006034{
Peter Zijlstra4be9daa2008-06-27 13:41:30 +02006035 struct sched_entity *se = tg->se[cpu];
Peter Zijlstraf1d239f2008-06-27 13:41:38 +02006036
Rik van Riel9722c2d2014-01-06 11:39:12 +00006037 if (!tg->parent) /* the trivial, non-cgroup case */
Peter Zijlstraf1d239f2008-06-27 13:41:38 +02006038 return wl;
6039
Peter Zijlstra4be9daa2008-06-27 13:41:30 +02006040 for_each_sched_entity(se) {
Peter Zijlstra7dd49122016-06-24 15:53:54 +02006041 struct cfs_rq *cfs_rq = se->my_q;
6042 long W, w = cfs_rq_load_avg(cfs_rq);
Peter Zijlstrabb3469a2008-06-27 13:41:27 +02006043
Peter Zijlstra7dd49122016-06-24 15:53:54 +02006044 tg = cfs_rq->tg;
Peter Zijlstra4be9daa2008-06-27 13:41:30 +02006045
Peter Zijlstracf5f0ac2011-10-13 16:52:28 +02006046 /*
6047 * W = @wg + \Sum rw_j
6048 */
Peter Zijlstra7dd49122016-06-24 15:53:54 +02006049 W = wg + atomic_long_read(&tg->load_avg);
6050
6051 /* Ensure \Sum rw_j >= rw_i */
6052 W -= cfs_rq->tg_load_avg_contrib;
6053 W += w;
Peter Zijlstra4be9daa2008-06-27 13:41:30 +02006054
Peter Zijlstracf5f0ac2011-10-13 16:52:28 +02006055 /*
6056 * w = rw_i + @wl
6057 */
Peter Zijlstra7dd49122016-06-24 15:53:54 +02006058 w += wl;
Peter Zijlstra940959e2008-09-23 15:33:42 +02006059
Peter Zijlstracf5f0ac2011-10-13 16:52:28 +02006060 /*
6061 * wl = S * s'_i; see (2)
6062 */
6063 if (W > 0 && w < W)
Dietmar Eggemannab522e32016-08-22 15:00:41 +01006064 wl = (w * (long)scale_load_down(tg->shares)) / W;
Paul Turner977dda72011-01-14 17:57:50 -08006065 else
Dietmar Eggemannab522e32016-08-22 15:00:41 +01006066 wl = scale_load_down(tg->shares);
Peter Zijlstra940959e2008-09-23 15:33:42 +02006067
Peter Zijlstracf5f0ac2011-10-13 16:52:28 +02006068 /*
6069 * Per the above, wl is the new se->load.weight value; since
6070 * those are clipped to [MIN_SHARES, ...) do so now. See
6071 * calc_cfs_shares().
6072 */
Paul Turner977dda72011-01-14 17:57:50 -08006073 if (wl < MIN_SHARES)
6074 wl = MIN_SHARES;
Peter Zijlstracf5f0ac2011-10-13 16:52:28 +02006075
6076 /*
6077 * wl = dw_i = S * (s'_i - s_i); see (3)
6078 */
Yuyang Du9d89c252015-07-15 08:04:37 +08006079 wl -= se->avg.load_avg;
Peter Zijlstracf5f0ac2011-10-13 16:52:28 +02006080
6081 /*
6082 * Recursively apply this logic to all parent groups to compute
6083 * the final effective load change on the root group. Since
6084 * only the @tg group gets extra weight, all parent groups can
6085 * only redistribute existing shares. @wl is the shift in shares
6086 * resulting from this level per the above.
6087 */
Peter Zijlstra4be9daa2008-06-27 13:41:30 +02006088 wg = 0;
Peter Zijlstra4be9daa2008-06-27 13:41:30 +02006089 }
6090
6091 return wl;
Peter Zijlstrabb3469a2008-06-27 13:41:27 +02006092}
6093#else
Peter Zijlstra4be9daa2008-06-27 13:41:30 +02006094
Mel Gorman58d081b2013-10-07 11:29:10 +01006095static long effective_load(struct task_group *tg, int cpu, long wl, long wg)
Peter Zijlstra4be9daa2008-06-27 13:41:30 +02006096{
Peter Zijlstra83378262008-06-27 13:41:37 +02006097 return wl;
Peter Zijlstrabb3469a2008-06-27 13:41:27 +02006098}
Peter Zijlstra4be9daa2008-06-27 13:41:30 +02006099
Peter Zijlstrabb3469a2008-06-27 13:41:27 +02006100#endif
6101
Peter Zijlstrac58d25f2016-05-12 09:19:59 +02006102static void record_wakee(struct task_struct *p)
6103{
6104 /*
6105 * Only decay a single time; tasks that have less then 1 wakeup per
6106 * jiffy will not have built up many flips.
6107 */
6108 if (time_after(jiffies, current->wakee_flip_decay_ts + HZ)) {
6109 current->wakee_flips >>= 1;
6110 current->wakee_flip_decay_ts = jiffies;
6111 }
6112
6113 if (current->last_wakee != p) {
6114 current->last_wakee = p;
6115 current->wakee_flips++;
6116 }
6117}
6118
Mike Galbraith63b0e9e2015-07-14 17:39:50 +02006119/*
6120 * Detect M:N waker/wakee relationships via a switching-frequency heuristic.
Peter Zijlstrac58d25f2016-05-12 09:19:59 +02006121 *
Mike Galbraith63b0e9e2015-07-14 17:39:50 +02006122 * A waker of many should wake a different task than the one last awakened
Peter Zijlstrac58d25f2016-05-12 09:19:59 +02006123 * at a frequency roughly N times higher than one of its wakees.
6124 *
6125 * In order to determine whether we should let the load spread vs consolidating
6126 * to shared cache, we look for a minimum 'flip' frequency of llc_size in one
6127 * partner, and a factor of lls_size higher frequency in the other.
6128 *
6129 * With both conditions met, we can be relatively sure that the relationship is
6130 * non-monogamous, with partner count exceeding socket size.
6131 *
6132 * Waker/wakee being client/server, worker/dispatcher, interrupt source or
6133 * whatever is irrelevant, spread criteria is apparent partner count exceeds
6134 * socket size.
Mike Galbraith63b0e9e2015-07-14 17:39:50 +02006135 */
Michael Wang62470412013-07-04 12:55:51 +08006136static int wake_wide(struct task_struct *p)
6137{
Mike Galbraith63b0e9e2015-07-14 17:39:50 +02006138 unsigned int master = current->wakee_flips;
6139 unsigned int slave = p->wakee_flips;
Peter Zijlstra7d9ffa82013-07-04 12:56:46 +08006140 int factor = this_cpu_read(sd_llc_size);
Michael Wang62470412013-07-04 12:55:51 +08006141
Mike Galbraith63b0e9e2015-07-14 17:39:50 +02006142 if (master < slave)
6143 swap(master, slave);
6144 if (slave < factor || master < slave * factor)
6145 return 0;
6146 return 1;
Michael Wang62470412013-07-04 12:55:51 +08006147}
6148
Morten Rasmussen772bd008c2016-06-22 18:03:13 +01006149static int wake_affine(struct sched_domain *sd, struct task_struct *p,
6150 int prev_cpu, int sync)
Ingo Molnar098fb9d2008-03-16 20:36:10 +01006151{
Paul Turnere37b6a72011-01-21 20:44:59 -08006152 s64 this_load, load;
Vincent Guittotbd61c982014-08-26 13:06:50 +02006153 s64 this_eff_load, prev_eff_load;
Morten Rasmussen772bd008c2016-06-22 18:03:13 +01006154 int idx, this_cpu;
Peter Zijlstrac88d5912009-09-10 13:50:02 +02006155 struct task_group *tg;
Peter Zijlstra83378262008-06-27 13:41:37 +02006156 unsigned long weight;
Mike Galbraithb3137bc2008-05-29 11:11:41 +02006157 int balanced;
Ingo Molnar098fb9d2008-03-16 20:36:10 +01006158
Peter Zijlstrac88d5912009-09-10 13:50:02 +02006159 idx = sd->wake_idx;
6160 this_cpu = smp_processor_id();
Peter Zijlstrac88d5912009-09-10 13:50:02 +02006161 load = source_load(prev_cpu, idx);
6162 this_load = target_load(this_cpu, idx);
Ingo Molnar098fb9d2008-03-16 20:36:10 +01006163
6164 /*
Ingo Molnar098fb9d2008-03-16 20:36:10 +01006165 * If sync wakeup then subtract the (maximum possible)
6166 * effect of the currently running task from the load
6167 * of the current CPU:
6168 */
Peter Zijlstra83378262008-06-27 13:41:37 +02006169 if (sync) {
6170 tg = task_group(current);
Yuyang Du9d89c252015-07-15 08:04:37 +08006171 weight = current->se.avg.load_avg;
Ingo Molnar098fb9d2008-03-16 20:36:10 +01006172
Peter Zijlstrac88d5912009-09-10 13:50:02 +02006173 this_load += effective_load(tg, this_cpu, -weight, -weight);
Peter Zijlstra83378262008-06-27 13:41:37 +02006174 load += effective_load(tg, prev_cpu, 0, -weight);
6175 }
6176
6177 tg = task_group(p);
Yuyang Du9d89c252015-07-15 08:04:37 +08006178 weight = p->se.avg.load_avg;
Peter Zijlstra83378262008-06-27 13:41:37 +02006179
Peter Zijlstra71a29aa2009-09-07 18:28:05 +02006180 /*
6181 * In low-load situations, where prev_cpu is idle and this_cpu is idle
Peter Zijlstrac88d5912009-09-10 13:50:02 +02006182 * due to the sync cause above having dropped this_load to 0, we'll
6183 * always have an imbalance, but there's really nothing you can do
6184 * about that, so that's good too.
Peter Zijlstra71a29aa2009-09-07 18:28:05 +02006185 *
6186 * Otherwise check if either cpus are near enough in load to allow this
6187 * task to be woken on this_cpu.
6188 */
Vincent Guittotbd61c982014-08-26 13:06:50 +02006189 this_eff_load = 100;
6190 this_eff_load *= capacity_of(prev_cpu);
Peter Zijlstrae51fd5e2010-05-31 12:37:30 +02006191
Vincent Guittotbd61c982014-08-26 13:06:50 +02006192 prev_eff_load = 100 + (sd->imbalance_pct - 100) / 2;
6193 prev_eff_load *= capacity_of(this_cpu);
6194
6195 if (this_load > 0) {
Peter Zijlstrae51fd5e2010-05-31 12:37:30 +02006196 this_eff_load *= this_load +
6197 effective_load(tg, this_cpu, weight, weight);
6198
Peter Zijlstrae51fd5e2010-05-31 12:37:30 +02006199 prev_eff_load *= load + effective_load(tg, prev_cpu, 0, weight);
Vincent Guittotbd61c982014-08-26 13:06:50 +02006200 }
Peter Zijlstrae51fd5e2010-05-31 12:37:30 +02006201
Vincent Guittotbd61c982014-08-26 13:06:50 +02006202 balanced = this_eff_load <= prev_eff_load;
Mike Galbraithb3137bc2008-05-29 11:11:41 +02006203
Josh Poimboeufae928822016-06-17 12:43:24 -05006204 schedstat_inc(p->se.statistics.nr_wakeups_affine_attempts);
Mike Galbraithb3137bc2008-05-29 11:11:41 +02006205
Vincent Guittot05bfb652014-08-26 13:06:45 +02006206 if (!balanced)
6207 return 0;
Ingo Molnar098fb9d2008-03-16 20:36:10 +01006208
Josh Poimboeufae928822016-06-17 12:43:24 -05006209 schedstat_inc(sd->ttwu_move_affine);
6210 schedstat_inc(p->se.statistics.nr_wakeups_affine);
Vincent Guittot05bfb652014-08-26 13:06:45 +02006211
6212 return 1;
Ingo Molnar098fb9d2008-03-16 20:36:10 +01006213}
6214
Peter Zijlstraaaee1202009-09-10 13:36:25 +02006215/*
6216 * find_idlest_group finds and returns the least busy CPU group within the
6217 * domain.
6218 */
6219static struct sched_group *
Peter Zijlstra78e7ed52009-09-03 13:16:51 +02006220find_idlest_group(struct sched_domain *sd, struct task_struct *p,
Vincent Guittotc44f2a02013-10-18 13:52:21 +02006221 int this_cpu, int sd_flag)
Gregory Haskinse7693a32008-01-25 21:08:09 +01006222{
Andi Kleenb3bd3de2010-08-10 14:17:51 -07006223 struct sched_group *idlest = NULL, *group = sd->groups;
Peter Zijlstraaaee1202009-09-10 13:36:25 +02006224 unsigned long min_load = ULONG_MAX, this_load = 0;
Vincent Guittotc44f2a02013-10-18 13:52:21 +02006225 int load_idx = sd->forkexec_idx;
Peter Zijlstraaaee1202009-09-10 13:36:25 +02006226 int imbalance = 100 + (sd->imbalance_pct-100)/2;
Gregory Haskinse7693a32008-01-25 21:08:09 +01006227
Vincent Guittotc44f2a02013-10-18 13:52:21 +02006228 if (sd_flag & SD_BALANCE_WAKE)
6229 load_idx = sd->wake_idx;
6230
Peter Zijlstraaaee1202009-09-10 13:36:25 +02006231 do {
6232 unsigned long load, avg_load;
6233 int local_group;
6234 int i;
Gregory Haskinse7693a32008-01-25 21:08:09 +01006235
Peter Zijlstraaaee1202009-09-10 13:36:25 +02006236 /* Skip over this group if it has no CPUs allowed */
6237 if (!cpumask_intersects(sched_group_cpus(group),
Peter Zijlstrafa17b502011-06-16 12:23:22 +02006238 tsk_cpus_allowed(p)))
Peter Zijlstraaaee1202009-09-10 13:36:25 +02006239 continue;
6240
6241 local_group = cpumask_test_cpu(this_cpu,
6242 sched_group_cpus(group));
6243
6244 /* Tally up the load of all CPUs in the group */
6245 avg_load = 0;
6246
6247 for_each_cpu(i, sched_group_cpus(group)) {
6248 /* Bias balancing toward cpus of our domain */
6249 if (local_group)
6250 load = source_load(i, load_idx);
6251 else
6252 load = target_load(i, load_idx);
6253
6254 avg_load += load;
6255 }
6256
Nicolas Pitre63b2ca32014-05-26 18:19:37 -04006257 /* Adjust by relative CPU capacity of the group */
Nicolas Pitreca8ce3d2014-05-26 18:19:39 -04006258 avg_load = (avg_load * SCHED_CAPACITY_SCALE) / group->sgc->capacity;
Peter Zijlstraaaee1202009-09-10 13:36:25 +02006259
6260 if (local_group) {
6261 this_load = avg_load;
Peter Zijlstraaaee1202009-09-10 13:36:25 +02006262 } else if (avg_load < min_load) {
6263 min_load = avg_load;
6264 idlest = group;
6265 }
6266 } while (group = group->next, group != sd->groups);
6267
6268 if (!idlest || 100*this_load < imbalance*min_load)
6269 return NULL;
6270 return idlest;
6271}
6272
6273/*
6274 * find_idlest_cpu - find the idlest cpu among the cpus in group.
6275 */
6276static int
6277find_idlest_cpu(struct sched_group *group, struct task_struct *p, int this_cpu)
6278{
6279 unsigned long load, min_load = ULONG_MAX;
Nicolas Pitre83a0a962014-09-04 11:32:10 -04006280 unsigned int min_exit_latency = UINT_MAX;
6281 u64 latest_idle_timestamp = 0;
6282 int least_loaded_cpu = this_cpu;
6283 int shallowest_idle_cpu = -1;
Peter Zijlstraaaee1202009-09-10 13:36:25 +02006284 int i;
6285
Morten Rasmusseneaecf412016-06-22 18:03:14 +01006286 /* Check if we have any choice: */
6287 if (group->group_weight == 1)
6288 return cpumask_first(sched_group_cpus(group));
6289
Peter Zijlstraaaee1202009-09-10 13:36:25 +02006290 /* Traverse only the allowed CPUs */
Peter Zijlstrafa17b502011-06-16 12:23:22 +02006291 for_each_cpu_and(i, sched_group_cpus(group), tsk_cpus_allowed(p)) {
Nicolas Pitre83a0a962014-09-04 11:32:10 -04006292 if (idle_cpu(i)) {
6293 struct rq *rq = cpu_rq(i);
6294 struct cpuidle_state *idle = idle_get_state(rq);
6295 if (idle && idle->exit_latency < min_exit_latency) {
6296 /*
6297 * We give priority to a CPU whose idle state
6298 * has the smallest exit latency irrespective
6299 * of any idle timestamp.
6300 */
6301 min_exit_latency = idle->exit_latency;
6302 latest_idle_timestamp = rq->idle_stamp;
6303 shallowest_idle_cpu = i;
6304 } else if ((!idle || idle->exit_latency == min_exit_latency) &&
6305 rq->idle_stamp > latest_idle_timestamp) {
6306 /*
6307 * If equal or no active idle state, then
6308 * the most recently idled CPU might have
6309 * a warmer cache.
6310 */
6311 latest_idle_timestamp = rq->idle_stamp;
6312 shallowest_idle_cpu = i;
6313 }
Yao Dongdong9f967422014-10-28 04:08:06 +00006314 } else if (shallowest_idle_cpu == -1) {
Nicolas Pitre83a0a962014-09-04 11:32:10 -04006315 load = weighted_cpuload(i);
6316 if (load < min_load || (load == min_load && i == this_cpu)) {
6317 min_load = load;
6318 least_loaded_cpu = i;
6319 }
Gregory Haskinse7693a32008-01-25 21:08:09 +01006320 }
6321 }
6322
Nicolas Pitre83a0a962014-09-04 11:32:10 -04006323 return shallowest_idle_cpu != -1 ? shallowest_idle_cpu : least_loaded_cpu;
Peter Zijlstraaaee1202009-09-10 13:36:25 +02006324}
Gregory Haskinse7693a32008-01-25 21:08:09 +01006325
Peter Zijlstraaaee1202009-09-10 13:36:25 +02006326/*
Peter Zijlstra10e2f1a2016-05-09 10:38:05 +02006327 * Implement a for_each_cpu() variant that starts the scan at a given cpu
6328 * (@start), and wraps around.
6329 *
6330 * This is used to scan for idle CPUs; such that not all CPUs looking for an
6331 * idle CPU find the same CPU. The down-side is that tasks tend to cycle
6332 * through the LLC domain.
6333 *
6334 * Especially tbench is found sensitive to this.
Peter Zijlstraa50bde52009-11-12 15:55:28 +01006335 */
Peter Zijlstra10e2f1a2016-05-09 10:38:05 +02006336
6337static int cpumask_next_wrap(int n, const struct cpumask *mask, int start, int *wrapped)
6338{
6339 int next;
6340
6341again:
6342 next = find_next_bit(cpumask_bits(mask), nr_cpumask_bits, n+1);
6343
6344 if (*wrapped) {
6345 if (next >= start)
6346 return nr_cpumask_bits;
6347 } else {
6348 if (next >= nr_cpumask_bits) {
6349 *wrapped = 1;
6350 n = -1;
6351 goto again;
6352 }
6353 }
6354
6355 return next;
6356}
6357
6358#define for_each_cpu_wrap(cpu, mask, start, wrap) \
6359 for ((wrap) = 0, (cpu) = (start)-1; \
6360 (cpu) = cpumask_next_wrap((cpu), (mask), (start), &(wrap)), \
6361 (cpu) < nr_cpumask_bits; )
6362
6363#ifdef CONFIG_SCHED_SMT
6364
6365static inline void set_idle_cores(int cpu, int val)
6366{
6367 struct sched_domain_shared *sds;
6368
6369 sds = rcu_dereference(per_cpu(sd_llc_shared, cpu));
6370 if (sds)
6371 WRITE_ONCE(sds->has_idle_cores, val);
6372}
6373
6374static inline bool test_idle_cores(int cpu, bool def)
6375{
6376 struct sched_domain_shared *sds;
6377
6378 sds = rcu_dereference(per_cpu(sd_llc_shared, cpu));
6379 if (sds)
6380 return READ_ONCE(sds->has_idle_cores);
6381
6382 return def;
6383}
6384
6385/*
6386 * Scans the local SMT mask to see if the entire core is idle, and records this
6387 * information in sd_llc_shared->has_idle_cores.
6388 *
6389 * Since SMT siblings share all cache levels, inspecting this limited remote
6390 * state should be fairly cheap.
6391 */
Peter Zijlstra1b568f02016-05-09 10:38:41 +02006392void __update_idle_core(struct rq *rq)
Peter Zijlstra10e2f1a2016-05-09 10:38:05 +02006393{
6394 int core = cpu_of(rq);
6395 int cpu;
6396
6397 rcu_read_lock();
6398 if (test_idle_cores(core, true))
6399 goto unlock;
6400
6401 for_each_cpu(cpu, cpu_smt_mask(core)) {
6402 if (cpu == core)
6403 continue;
6404
6405 if (!idle_cpu(cpu))
6406 goto unlock;
6407 }
6408
6409 set_idle_cores(core, 1);
6410unlock:
6411 rcu_read_unlock();
6412}
6413
6414/*
6415 * Scan the entire LLC domain for idle cores; this dynamically switches off if
6416 * there are no idle cores left in the system; tracked through
6417 * sd_llc->shared->has_idle_cores and enabled through update_idle_core() above.
6418 */
6419static int select_idle_core(struct task_struct *p, struct sched_domain *sd, int target)
6420{
6421 struct cpumask *cpus = this_cpu_cpumask_var_ptr(select_idle_mask);
6422 int core, cpu, wrap;
6423
Peter Zijlstra1b568f02016-05-09 10:38:41 +02006424 if (!static_branch_likely(&sched_smt_present))
6425 return -1;
6426
Peter Zijlstra10e2f1a2016-05-09 10:38:05 +02006427 if (!test_idle_cores(target, false))
6428 return -1;
6429
6430 cpumask_and(cpus, sched_domain_span(sd), tsk_cpus_allowed(p));
6431
6432 for_each_cpu_wrap(core, cpus, target, wrap) {
6433 bool idle = true;
6434
6435 for_each_cpu(cpu, cpu_smt_mask(core)) {
6436 cpumask_clear_cpu(cpu, cpus);
6437 if (!idle_cpu(cpu))
6438 idle = false;
6439 }
6440
6441 if (idle)
6442 return core;
6443 }
6444
6445 /*
6446 * Failed to find an idle core; stop looking for one.
6447 */
6448 set_idle_cores(target, 0);
6449
6450 return -1;
6451}
6452
6453/*
6454 * Scan the local SMT mask for idle CPUs.
6455 */
6456static int select_idle_smt(struct task_struct *p, struct sched_domain *sd, int target)
6457{
6458 int cpu;
6459
Peter Zijlstra1b568f02016-05-09 10:38:41 +02006460 if (!static_branch_likely(&sched_smt_present))
6461 return -1;
6462
Peter Zijlstra10e2f1a2016-05-09 10:38:05 +02006463 for_each_cpu(cpu, cpu_smt_mask(target)) {
6464 if (!cpumask_test_cpu(cpu, tsk_cpus_allowed(p)))
6465 continue;
6466 if (idle_cpu(cpu))
6467 return cpu;
6468 }
6469
6470 return -1;
6471}
6472
6473#else /* CONFIG_SCHED_SMT */
6474
6475static inline int select_idle_core(struct task_struct *p, struct sched_domain *sd, int target)
6476{
6477 return -1;
6478}
6479
6480static inline int select_idle_smt(struct task_struct *p, struct sched_domain *sd, int target)
6481{
6482 return -1;
6483}
6484
6485#endif /* CONFIG_SCHED_SMT */
6486
6487/*
6488 * Scan the LLC domain for idle CPUs; this is dynamically regulated by
6489 * comparing the average scan cost (tracked in sd->avg_scan_cost) against the
6490 * average idle time for this rq (as found in rq->avg_idle).
6491 */
6492static int select_idle_cpu(struct task_struct *p, struct sched_domain *sd, int target)
6493{
Wanpeng Li9cfb38a2016-10-09 08:04:03 +08006494 struct sched_domain *this_sd;
6495 u64 avg_cost, avg_idle = this_rq()->avg_idle;
Peter Zijlstra10e2f1a2016-05-09 10:38:05 +02006496 u64 time, cost;
6497 s64 delta;
6498 int cpu, wrap;
6499
Wanpeng Li9cfb38a2016-10-09 08:04:03 +08006500 this_sd = rcu_dereference(*this_cpu_ptr(&sd_llc));
6501 if (!this_sd)
6502 return -1;
6503
6504 avg_cost = this_sd->avg_scan_cost;
6505
Peter Zijlstra10e2f1a2016-05-09 10:38:05 +02006506 /*
6507 * Due to large variance we need a large fuzz factor; hackbench in
6508 * particularly is sensitive here.
6509 */
6510 if ((avg_idle / 512) < avg_cost)
6511 return -1;
6512
6513 time = local_clock();
6514
6515 for_each_cpu_wrap(cpu, sched_domain_span(sd), target, wrap) {
6516 if (!cpumask_test_cpu(cpu, tsk_cpus_allowed(p)))
6517 continue;
6518 if (idle_cpu(cpu))
6519 break;
6520 }
6521
6522 time = local_clock() - time;
6523 cost = this_sd->avg_scan_cost;
6524 delta = (s64)(time - cost) / 8;
6525 this_sd->avg_scan_cost += delta;
6526
6527 return cpu;
6528}
6529
6530/*
6531 * Try and locate an idle core/thread in the LLC cache domain.
Peter Zijlstraa50bde52009-11-12 15:55:28 +01006532 */
Morten Rasmussen772bd008c2016-06-22 18:03:13 +01006533static int select_idle_sibling(struct task_struct *p, int prev, int target)
Peter Zijlstraa50bde52009-11-12 15:55:28 +01006534{
Suresh Siddha99bd5e22010-03-31 16:47:45 -07006535 struct sched_domain *sd;
Peter Zijlstra10e2f1a2016-05-09 10:38:05 +02006536 int i;
Mike Galbraithe0a79f52013-01-28 12:19:25 +01006537
6538 if (idle_cpu(target))
6539 return target;
Peter Zijlstraa50bde52009-11-12 15:55:28 +01006540
6541 /*
Peter Zijlstra10e2f1a2016-05-09 10:38:05 +02006542 * If the previous cpu is cache affine and idle, don't be stupid.
Peter Zijlstraa50bde52009-11-12 15:55:28 +01006543 */
Morten Rasmussen772bd008c2016-06-22 18:03:13 +01006544 if (prev != target && cpus_share_cache(prev, target) && idle_cpu(prev))
6545 return prev;
Peter Zijlstraa50bde52009-11-12 15:55:28 +01006546
Peter Zijlstra518cd622011-12-07 15:07:31 +01006547 sd = rcu_dereference(per_cpu(sd_llc, target));
Peter Zijlstra10e2f1a2016-05-09 10:38:05 +02006548 if (!sd)
6549 return target;
Morten Rasmussen772bd008c2016-06-22 18:03:13 +01006550
Peter Zijlstra10e2f1a2016-05-09 10:38:05 +02006551 i = select_idle_core(p, sd, target);
6552 if ((unsigned)i < nr_cpumask_bits)
Gregory Haskinse7693a32008-01-25 21:08:09 +01006553 return i;
Ingo Molnar098fb9d2008-03-16 20:36:10 +01006554
Peter Zijlstra10e2f1a2016-05-09 10:38:05 +02006555 i = select_idle_cpu(p, sd, target);
6556 if ((unsigned)i < nr_cpumask_bits)
6557 return i;
Mike Galbraith970e1782012-06-12 05:18:32 +02006558
Peter Zijlstra10e2f1a2016-05-09 10:38:05 +02006559 i = select_idle_smt(p, sd, target);
6560 if ((unsigned)i < nr_cpumask_bits)
6561 return i;
Linus Torvalds37407ea2012-09-16 12:29:43 -07006562
Peter Zijlstraa50bde52009-11-12 15:55:28 +01006563 return target;
6564}
Dietmar Eggemann231678b2015-08-14 17:23:13 +01006565
Vincent Guittot8bb5b002015-03-04 08:48:47 +01006566/*
Dietmar Eggemann9e91d612015-08-14 17:23:12 +01006567 * cpu_util returns the amount of capacity of a CPU that is used by CFS
Vincent Guittot8bb5b002015-03-04 08:48:47 +01006568 * tasks. The unit of the return value must be the one of capacity so we can
Dietmar Eggemann9e91d612015-08-14 17:23:12 +01006569 * compare the utilization with the capacity of the CPU that is available for
6570 * CFS task (ie cpu_capacity).
Dietmar Eggemann231678b2015-08-14 17:23:13 +01006571 *
6572 * cfs_rq.avg.util_avg is the sum of running time of runnable tasks plus the
6573 * recent utilization of currently non-runnable tasks on a CPU. It represents
6574 * the amount of utilization of a CPU in the range [0..capacity_orig] where
6575 * capacity_orig is the cpu_capacity available at the highest frequency
6576 * (arch_scale_freq_capacity()).
6577 * The utilization of a CPU converges towards a sum equal to or less than the
6578 * current capacity (capacity_curr <= capacity_orig) of the CPU because it is
6579 * the running time on this CPU scaled by capacity_curr.
6580 *
6581 * Nevertheless, cfs_rq.avg.util_avg can be higher than capacity_curr or even
6582 * higher than capacity_orig because of unfortunate rounding in
6583 * cfs.avg.util_avg or just after migrating tasks and new task wakeups until
6584 * the average stabilizes with the new running time. We need to check that the
6585 * utilization stays within the range of [0..capacity_orig] and cap it if
6586 * necessary. Without utilization capping, a group could be seen as overloaded
6587 * (CPU0 utilization at 121% + CPU1 utilization at 80%) whereas CPU1 has 20% of
6588 * available capacity. We allow utilization to overshoot capacity_curr (but not
6589 * capacity_orig) as it useful for predicting the capacity required after task
6590 * migrations (scheduler-driven DVFS).
Vincent Guittot8bb5b002015-03-04 08:48:47 +01006591 */
Dietmar Eggemann9e91d612015-08-14 17:23:12 +01006592static int cpu_util(int cpu)
Vincent Guittot8bb5b002015-03-04 08:48:47 +01006593{
Dietmar Eggemann9e91d612015-08-14 17:23:12 +01006594 unsigned long util = cpu_rq(cpu)->cfs.avg.util_avg;
Vincent Guittot8bb5b002015-03-04 08:48:47 +01006595 unsigned long capacity = capacity_orig_of(cpu);
6596
Dietmar Eggemann231678b2015-08-14 17:23:13 +01006597 return (util >= capacity) ? capacity : util;
Vincent Guittot8bb5b002015-03-04 08:48:47 +01006598}
Peter Zijlstraa50bde52009-11-12 15:55:28 +01006599
Morten Rasmussen32731632016-07-25 14:34:26 +01006600static inline int task_util(struct task_struct *p)
6601{
6602 return p->se.avg.util_avg;
6603}
6604
6605/*
6606 * Disable WAKE_AFFINE in the case where task @p doesn't fit in the
6607 * capacity of either the waking CPU @cpu or the previous CPU @prev_cpu.
6608 *
6609 * In that case WAKE_AFFINE doesn't make sense and we'll let
6610 * BALANCE_WAKE sort things out.
6611 */
6612static int wake_cap(struct task_struct *p, int cpu, int prev_cpu)
6613{
6614 long min_cap, max_cap;
6615
6616 min_cap = min(capacity_orig_of(prev_cpu), capacity_orig_of(cpu));
6617 max_cap = cpu_rq(cpu)->rd->max_cpu_capacity;
6618
6619 /* Minimum capacity is close to max, no need to abort wake_affine */
6620 if (max_cap - min_cap < max_cap >> 3)
6621 return 0;
6622
6623 return min_cap * 1024 < task_util(p) * capacity_margin;
6624}
6625
Peter Zijlstraa50bde52009-11-12 15:55:28 +01006626/*
Morten Rasmussende91b9c2014-02-18 14:14:24 +00006627 * select_task_rq_fair: Select target runqueue for the waking task in domains
6628 * that have the 'sd_flag' flag set. In practice, this is SD_BALANCE_WAKE,
6629 * SD_BALANCE_FORK, or SD_BALANCE_EXEC.
Peter Zijlstraaaee1202009-09-10 13:36:25 +02006630 *
Morten Rasmussende91b9c2014-02-18 14:14:24 +00006631 * Balances load by selecting the idlest cpu in the idlest group, or under
6632 * certain conditions an idle sibling cpu if the domain has SD_WAKE_AFFINE set.
Peter Zijlstraaaee1202009-09-10 13:36:25 +02006633 *
Morten Rasmussende91b9c2014-02-18 14:14:24 +00006634 * Returns the target cpu number.
Peter Zijlstraaaee1202009-09-10 13:36:25 +02006635 *
6636 * preempt must be disabled.
6637 */
Peter Zijlstra0017d732010-03-24 18:34:10 +01006638static int
Peter Zijlstraac66f542013-10-07 11:29:16 +01006639select_task_rq_fair(struct task_struct *p, int prev_cpu, int sd_flag, int wake_flags)
Peter Zijlstraaaee1202009-09-10 13:36:25 +02006640{
Peter Zijlstra29cd8ba2009-09-17 09:01:14 +02006641 struct sched_domain *tmp, *affine_sd = NULL, *sd = NULL;
Peter Zijlstrac88d5912009-09-10 13:50:02 +02006642 int cpu = smp_processor_id();
Mike Galbraith63b0e9e2015-07-14 17:39:50 +02006643 int new_cpu = prev_cpu;
Suresh Siddha99bd5e22010-03-31 16:47:45 -07006644 int want_affine = 0;
Peter Zijlstra5158f4e2009-09-16 13:46:59 +02006645 int sync = wake_flags & WF_SYNC;
Gregory Haskinse7693a32008-01-25 21:08:09 +01006646
Syed Rameez Mustafadddcab72016-09-07 16:18:27 -07006647#ifdef CONFIG_SCHED_HMP
6648 return select_best_cpu(p, prev_cpu, 0, sync);
6649#endif
6650
Peter Zijlstrac58d25f2016-05-12 09:19:59 +02006651 if (sd_flag & SD_BALANCE_WAKE) {
6652 record_wakee(p);
Morten Rasmussen32731632016-07-25 14:34:26 +01006653 want_affine = !wake_wide(p) && !wake_cap(p, cpu, prev_cpu)
6654 && cpumask_test_cpu(cpu, tsk_cpus_allowed(p));
Peter Zijlstrac58d25f2016-05-12 09:19:59 +02006655 }
Gregory Haskinse7693a32008-01-25 21:08:09 +01006656
Peter Zijlstradce840a2011-04-07 14:09:50 +02006657 rcu_read_lock();
Peter Zijlstraaaee1202009-09-10 13:36:25 +02006658 for_each_domain(cpu, tmp) {
Peter Zijlstrae4f428882009-12-16 18:04:34 +01006659 if (!(tmp->flags & SD_LOAD_BALANCE))
Mike Galbraith63b0e9e2015-07-14 17:39:50 +02006660 break;
Peter Zijlstrae4f428882009-12-16 18:04:34 +01006661
Peter Zijlstraaaee1202009-09-10 13:36:25 +02006662 /*
Suresh Siddha99bd5e22010-03-31 16:47:45 -07006663 * If both cpu and prev_cpu are part of this domain,
6664 * cpu is a valid SD_WAKE_AFFINE target.
Peter Zijlstrafe3bcfe2009-11-12 15:55:29 +01006665 */
Suresh Siddha99bd5e22010-03-31 16:47:45 -07006666 if (want_affine && (tmp->flags & SD_WAKE_AFFINE) &&
6667 cpumask_test_cpu(prev_cpu, sched_domain_span(tmp))) {
6668 affine_sd = tmp;
Alex Shif03542a2012-07-26 08:55:34 +08006669 break;
Peter Zijlstrac88d5912009-09-10 13:50:02 +02006670 }
6671
Alex Shif03542a2012-07-26 08:55:34 +08006672 if (tmp->flags & sd_flag)
Peter Zijlstra29cd8ba2009-09-17 09:01:14 +02006673 sd = tmp;
Mike Galbraith63b0e9e2015-07-14 17:39:50 +02006674 else if (!want_affine)
6675 break;
Peter Zijlstrac88d5912009-09-10 13:50:02 +02006676 }
Peter Zijlstraaaee1202009-09-10 13:36:25 +02006677
Mike Galbraith63b0e9e2015-07-14 17:39:50 +02006678 if (affine_sd) {
6679 sd = NULL; /* Prefer wake_affine over balance flags */
Morten Rasmussen772bd008c2016-06-22 18:03:13 +01006680 if (cpu != prev_cpu && wake_affine(affine_sd, p, prev_cpu, sync))
Mike Galbraith63b0e9e2015-07-14 17:39:50 +02006681 new_cpu = cpu;
Mike Galbraith8b911ac2010-03-11 17:17:16 +01006682 }
Peter Zijlstra3b640892009-09-16 13:44:33 +02006683
Mike Galbraith63b0e9e2015-07-14 17:39:50 +02006684 if (!sd) {
6685 if (sd_flag & SD_BALANCE_WAKE) /* XXX always ? */
Morten Rasmussen772bd008c2016-06-22 18:03:13 +01006686 new_cpu = select_idle_sibling(p, prev_cpu, new_cpu);
Mike Galbraith63b0e9e2015-07-14 17:39:50 +02006687
6688 } else while (sd) {
Peter Zijlstraaaee1202009-09-10 13:36:25 +02006689 struct sched_group *group;
Peter Zijlstrac88d5912009-09-10 13:50:02 +02006690 int weight;
Peter Zijlstraaaee1202009-09-10 13:36:25 +02006691
Peter Zijlstra0763a662009-09-14 19:37:39 +02006692 if (!(sd->flags & sd_flag)) {
Peter Zijlstraaaee1202009-09-10 13:36:25 +02006693 sd = sd->child;
6694 continue;
6695 }
6696
Vincent Guittotc44f2a02013-10-18 13:52:21 +02006697 group = find_idlest_group(sd, p, cpu, sd_flag);
Peter Zijlstraaaee1202009-09-10 13:36:25 +02006698 if (!group) {
6699 sd = sd->child;
6700 continue;
6701 }
6702
Peter Zijlstrad7c33c42009-09-11 12:45:38 +02006703 new_cpu = find_idlest_cpu(group, p, cpu);
Peter Zijlstraaaee1202009-09-10 13:36:25 +02006704 if (new_cpu == -1 || new_cpu == cpu) {
6705 /* Now try balancing at a lower domain level of cpu */
6706 sd = sd->child;
6707 continue;
6708 }
6709
6710 /* Now try balancing at a lower domain level of new_cpu */
6711 cpu = new_cpu;
Peter Zijlstra669c55e2010-04-16 14:59:29 +02006712 weight = sd->span_weight;
Peter Zijlstraaaee1202009-09-10 13:36:25 +02006713 sd = NULL;
6714 for_each_domain(cpu, tmp) {
Peter Zijlstra669c55e2010-04-16 14:59:29 +02006715 if (weight <= tmp->span_weight)
Peter Zijlstraaaee1202009-09-10 13:36:25 +02006716 break;
Peter Zijlstra0763a662009-09-14 19:37:39 +02006717 if (tmp->flags & sd_flag)
Peter Zijlstraaaee1202009-09-10 13:36:25 +02006718 sd = tmp;
6719 }
6720 /* while loop will break here if sd == NULL */
Gregory Haskinse7693a32008-01-25 21:08:09 +01006721 }
Peter Zijlstradce840a2011-04-07 14:09:50 +02006722 rcu_read_unlock();
Gregory Haskinse7693a32008-01-25 21:08:09 +01006723
Peter Zijlstrac88d5912009-09-10 13:50:02 +02006724 return new_cpu;
Gregory Haskinse7693a32008-01-25 21:08:09 +01006725}
Paul Turner0a74bef2012-10-04 13:18:30 +02006726
6727/*
6728 * Called immediately before a task is migrated to a new cpu; task_cpu(p) and
6729 * cfs_rq_of(p) references at time of call are still valid and identify the
Byungchul Park525628c2015-11-18 09:34:59 +09006730 * previous cpu. The caller guarantees p->pi_lock or task_rq(p)->lock is held.
Paul Turner0a74bef2012-10-04 13:18:30 +02006731 */
xiaofeng.yan5a4fd032015-09-23 14:55:59 +08006732static void migrate_task_rq_fair(struct task_struct *p)
Paul Turner0a74bef2012-10-04 13:18:30 +02006733{
Paul Turneraff3e492012-10-04 13:18:30 +02006734 /*
Peter Zijlstra59efa0b2016-05-10 18:24:37 +02006735 * As blocked tasks retain absolute vruntime the migration needs to
6736 * deal with this by subtracting the old and adding the new
6737 * min_vruntime -- the latter is done by enqueue_entity() when placing
6738 * the task on the new runqueue.
6739 */
6740 if (p->state == TASK_WAKING) {
6741 struct sched_entity *se = &p->se;
6742 struct cfs_rq *cfs_rq = cfs_rq_of(se);
6743 u64 min_vruntime;
6744
6745#ifndef CONFIG_64BIT
6746 u64 min_vruntime_copy;
6747
6748 do {
6749 min_vruntime_copy = cfs_rq->min_vruntime_copy;
6750 smp_rmb();
6751 min_vruntime = cfs_rq->min_vruntime;
6752 } while (min_vruntime != min_vruntime_copy);
6753#else
6754 min_vruntime = cfs_rq->min_vruntime;
6755#endif
6756
6757 se->vruntime -= min_vruntime;
6758 }
6759
6760 /*
Yuyang Du9d89c252015-07-15 08:04:37 +08006761 * We are supposed to update the task to "current" time, then its up to date
6762 * and ready to go to new CPU/cfs_rq. But we have difficulty in getting
6763 * what current time is, so simply throw away the out-of-date time. This
6764 * will result in the wakee task is less decayed, but giving the wakee more
6765 * load sounds not bad.
Paul Turneraff3e492012-10-04 13:18:30 +02006766 */
Yuyang Du9d89c252015-07-15 08:04:37 +08006767 remove_entity_load_avg(&p->se);
6768
6769 /* Tell new CPU we are migrated */
6770 p->se.avg.last_update_time = 0;
Ben Segall3944a922014-05-15 15:59:20 -07006771
6772 /* We have migrated, no longer consider this task hot */
Yuyang Du9d89c252015-07-15 08:04:37 +08006773 p->se.exec_start = 0;
Paul Turner0a74bef2012-10-04 13:18:30 +02006774}
Yuyang Du12695572015-07-15 08:04:40 +08006775
6776static void task_dead_fair(struct task_struct *p)
6777{
6778 remove_entity_load_avg(&p->se);
6779}
Gregory Haskinse7693a32008-01-25 21:08:09 +01006780#endif /* CONFIG_SMP */
6781
Peter Zijlstrae52fb7c2009-01-14 12:39:19 +01006782static unsigned long
6783wakeup_gran(struct sched_entity *curr, struct sched_entity *se)
Peter Zijlstra0bbd3332008-04-19 19:44:57 +02006784{
6785 unsigned long gran = sysctl_sched_wakeup_granularity;
6786
6787 /*
Peter Zijlstrae52fb7c2009-01-14 12:39:19 +01006788 * Since its curr running now, convert the gran from real-time
6789 * to virtual-time in his units.
Mike Galbraith13814d42010-03-11 17:17:04 +01006790 *
6791 * By using 'se' instead of 'curr' we penalize light tasks, so
6792 * they get preempted easier. That is, if 'se' < 'curr' then
6793 * the resulting gran will be larger, therefore penalizing the
6794 * lighter, if otoh 'se' > 'curr' then the resulting gran will
6795 * be smaller, again penalizing the lighter task.
6796 *
6797 * This is especially important for buddies when the leftmost
6798 * task is higher priority than the buddy.
Peter Zijlstra0bbd3332008-04-19 19:44:57 +02006799 */
Shaohua Lif4ad9bd2011-04-08 12:53:09 +08006800 return calc_delta_fair(gran, se);
Peter Zijlstra0bbd3332008-04-19 19:44:57 +02006801}
6802
6803/*
Peter Zijlstra464b7522008-10-24 11:06:15 +02006804 * Should 'se' preempt 'curr'.
6805 *
6806 * |s1
6807 * |s2
6808 * |s3
6809 * g
6810 * |<--->|c
6811 *
6812 * w(c, s1) = -1
6813 * w(c, s2) = 0
6814 * w(c, s3) = 1
6815 *
6816 */
6817static int
6818wakeup_preempt_entity(struct sched_entity *curr, struct sched_entity *se)
6819{
6820 s64 gran, vdiff = curr->vruntime - se->vruntime;
6821
6822 if (vdiff <= 0)
6823 return -1;
6824
Peter Zijlstrae52fb7c2009-01-14 12:39:19 +01006825 gran = wakeup_gran(curr, se);
Peter Zijlstra464b7522008-10-24 11:06:15 +02006826 if (vdiff > gran)
6827 return 1;
6828
6829 return 0;
6830}
6831
Peter Zijlstra02479092008-11-04 21:25:10 +01006832static void set_last_buddy(struct sched_entity *se)
6833{
Venkatesh Pallipadi69c80f32011-04-13 18:21:09 -07006834 if (entity_is_task(se) && unlikely(task_of(se)->policy == SCHED_IDLE))
6835 return;
6836
6837 for_each_sched_entity(se)
6838 cfs_rq_of(se)->last = se;
Peter Zijlstra02479092008-11-04 21:25:10 +01006839}
6840
6841static void set_next_buddy(struct sched_entity *se)
6842{
Venkatesh Pallipadi69c80f32011-04-13 18:21:09 -07006843 if (entity_is_task(se) && unlikely(task_of(se)->policy == SCHED_IDLE))
6844 return;
6845
6846 for_each_sched_entity(se)
6847 cfs_rq_of(se)->next = se;
Peter Zijlstra02479092008-11-04 21:25:10 +01006848}
6849
Rik van Rielac53db52011-02-01 09:51:03 -05006850static void set_skip_buddy(struct sched_entity *se)
6851{
Venkatesh Pallipadi69c80f32011-04-13 18:21:09 -07006852 for_each_sched_entity(se)
6853 cfs_rq_of(se)->skip = se;
Rik van Rielac53db52011-02-01 09:51:03 -05006854}
6855
Peter Zijlstra464b7522008-10-24 11:06:15 +02006856/*
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02006857 * Preempt the current task with a newly woken task if needed:
6858 */
Peter Zijlstra5a9b86f2009-09-16 13:47:58 +02006859static void check_preempt_wakeup(struct rq *rq, struct task_struct *p, int wake_flags)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02006860{
6861 struct task_struct *curr = rq->curr;
Srivatsa Vaddagiri8651a862007-10-15 17:00:12 +02006862 struct sched_entity *se = &curr->se, *pse = &p->se;
Mike Galbraith03e89e42008-12-16 08:45:30 +01006863 struct cfs_rq *cfs_rq = task_cfs_rq(curr);
Mike Galbraithf685cea2009-10-23 23:09:22 +02006864 int scale = cfs_rq->nr_running >= sched_nr_latency;
Venkatesh Pallipadi2f368252011-04-14 10:30:53 -07006865 int next_buddy_marked = 0;
Mike Galbraith03e89e42008-12-16 08:45:30 +01006866
Ingo Molnar4ae7d5c2008-03-19 01:42:00 +01006867 if (unlikely(se == pse))
6868 return;
6869
Paul Turner5238cdd2011-07-21 09:43:37 -07006870 /*
Kirill Tkhai163122b2014-08-20 13:48:29 +04006871 * This is possible from callers such as attach_tasks(), in which we
Paul Turner5238cdd2011-07-21 09:43:37 -07006872 * unconditionally check_prempt_curr() after an enqueue (which may have
6873 * lead to a throttle). This both saves work and prevents false
6874 * next-buddy nomination below.
6875 */
6876 if (unlikely(throttled_hierarchy(cfs_rq_of(pse))))
6877 return;
6878
Venkatesh Pallipadi2f368252011-04-14 10:30:53 -07006879 if (sched_feat(NEXT_BUDDY) && scale && !(wake_flags & WF_FORK)) {
Mike Galbraith3cb63d52009-09-11 12:01:17 +02006880 set_next_buddy(pse);
Venkatesh Pallipadi2f368252011-04-14 10:30:53 -07006881 next_buddy_marked = 1;
6882 }
Peter Zijlstra57fdc262008-09-23 15:33:45 +02006883
Bharata B Raoaec0a512008-08-28 14:42:49 +05306884 /*
6885 * We can come here with TIF_NEED_RESCHED already set from new task
6886 * wake up path.
Paul Turner5238cdd2011-07-21 09:43:37 -07006887 *
6888 * Note: this also catches the edge-case of curr being in a throttled
6889 * group (e.g. via set_curr_task), since update_curr() (in the
6890 * enqueue of curr) will have resulted in resched being set. This
6891 * prevents us from potentially nominating it as a false LAST_BUDDY
6892 * below.
Bharata B Raoaec0a512008-08-28 14:42:49 +05306893 */
6894 if (test_tsk_need_resched(curr))
6895 return;
6896
Darren Harta2f5c9a2011-02-22 13:04:33 -08006897 /* Idle tasks are by definition preempted by non-idle tasks. */
6898 if (unlikely(curr->policy == SCHED_IDLE) &&
6899 likely(p->policy != SCHED_IDLE))
6900 goto preempt;
6901
Ingo Molnar91c234b2007-10-15 17:00:18 +02006902 /*
Darren Harta2f5c9a2011-02-22 13:04:33 -08006903 * Batch and idle tasks do not preempt non-idle tasks (their preemption
6904 * is driven by the tick):
Ingo Molnar91c234b2007-10-15 17:00:18 +02006905 */
Ingo Molnar8ed92e52012-10-14 14:28:50 +02006906 if (unlikely(p->policy != SCHED_NORMAL) || !sched_feat(WAKEUP_PREEMPTION))
Ingo Molnar91c234b2007-10-15 17:00:18 +02006907 return;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02006908
Peter Zijlstra3a7e73a2009-11-28 18:51:02 +01006909 find_matching_se(&se, &pse);
Paul Turner9bbd7372011-07-05 19:07:21 -07006910 update_curr(cfs_rq_of(se));
Peter Zijlstra3a7e73a2009-11-28 18:51:02 +01006911 BUG_ON(!pse);
Venkatesh Pallipadi2f368252011-04-14 10:30:53 -07006912 if (wakeup_preempt_entity(se, pse) == 1) {
6913 /*
6914 * Bias pick_next to pick the sched entity that is
6915 * triggering this preemption.
6916 */
6917 if (!next_buddy_marked)
6918 set_next_buddy(pse);
Peter Zijlstra3a7e73a2009-11-28 18:51:02 +01006919 goto preempt;
Venkatesh Pallipadi2f368252011-04-14 10:30:53 -07006920 }
Jupyung Leea65ac742009-11-17 18:51:40 +09006921
Peter Zijlstra3a7e73a2009-11-28 18:51:02 +01006922 return;
6923
6924preempt:
Kirill Tkhai88751252014-06-29 00:03:57 +04006925 resched_curr(rq);
Peter Zijlstra3a7e73a2009-11-28 18:51:02 +01006926 /*
6927 * Only set the backward buddy when the current task is still
6928 * on the rq. This can happen when a wakeup gets interleaved
6929 * with schedule on the ->pre_schedule() or idle_balance()
6930 * point, either of which can * drop the rq lock.
6931 *
6932 * Also, during early boot the idle thread is in the fair class,
6933 * for obvious reasons its a bad idea to schedule back to it.
6934 */
6935 if (unlikely(!se->on_rq || curr == rq->idle))
6936 return;
6937
6938 if (sched_feat(LAST_BUDDY) && scale && entity_is_task(se))
6939 set_last_buddy(se);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02006940}
6941
Peter Zijlstra606dba22012-02-11 06:05:00 +01006942static struct task_struct *
Peter Zijlstrae7904a22015-08-01 19:25:08 +02006943pick_next_task_fair(struct rq *rq, struct task_struct *prev, struct pin_cookie cookie)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02006944{
6945 struct cfs_rq *cfs_rq = &rq->cfs;
6946 struct sched_entity *se;
Peter Zijlstra678d5712012-02-11 06:05:00 +01006947 struct task_struct *p;
Peter Zijlstra37e117c2014-02-14 12:25:08 +01006948 int new_tasks;
Peter Zijlstra678d5712012-02-11 06:05:00 +01006949
Peter Zijlstra6e831252014-02-11 16:11:48 +01006950again:
Peter Zijlstra678d5712012-02-11 06:05:00 +01006951#ifdef CONFIG_FAIR_GROUP_SCHED
6952 if (!cfs_rq->nr_running)
Peter Zijlstra38033c32014-01-23 20:32:21 +01006953 goto idle;
Peter Zijlstra678d5712012-02-11 06:05:00 +01006954
Peter Zijlstra3f1d2a32014-02-12 10:49:30 +01006955 if (prev->sched_class != &fair_sched_class)
Peter Zijlstra678d5712012-02-11 06:05:00 +01006956 goto simple;
6957
6958 /*
6959 * Because of the set_next_buddy() in dequeue_task_fair() it is rather
6960 * likely that a next task is from the same cgroup as the current.
6961 *
6962 * Therefore attempt to avoid putting and setting the entire cgroup
6963 * hierarchy, only change the part that actually changes.
6964 */
6965
6966 do {
6967 struct sched_entity *curr = cfs_rq->curr;
6968
6969 /*
6970 * Since we got here without doing put_prev_entity() we also
6971 * have to consider cfs_rq->curr. If it is still a runnable
6972 * entity, update_curr() will update its vruntime, otherwise
6973 * forget we've ever seen it.
6974 */
Ben Segall54d27362015-04-06 15:28:10 -07006975 if (curr) {
6976 if (curr->on_rq)
6977 update_curr(cfs_rq);
6978 else
6979 curr = NULL;
Peter Zijlstra678d5712012-02-11 06:05:00 +01006980
Ben Segall54d27362015-04-06 15:28:10 -07006981 /*
6982 * This call to check_cfs_rq_runtime() will do the
6983 * throttle and dequeue its entity in the parent(s).
6984 * Therefore the 'simple' nr_running test will indeed
6985 * be correct.
6986 */
6987 if (unlikely(check_cfs_rq_runtime(cfs_rq)))
6988 goto simple;
6989 }
Peter Zijlstra678d5712012-02-11 06:05:00 +01006990
6991 se = pick_next_entity(cfs_rq, curr);
6992 cfs_rq = group_cfs_rq(se);
6993 } while (cfs_rq);
6994
6995 p = task_of(se);
6996
6997 /*
6998 * Since we haven't yet done put_prev_entity and if the selected task
6999 * is a different task than we started out with, try and touch the
7000 * least amount of cfs_rqs.
7001 */
7002 if (prev != p) {
7003 struct sched_entity *pse = &prev->se;
7004
7005 while (!(cfs_rq = is_same_group(se, pse))) {
7006 int se_depth = se->depth;
7007 int pse_depth = pse->depth;
7008
7009 if (se_depth <= pse_depth) {
7010 put_prev_entity(cfs_rq_of(pse), pse);
7011 pse = parent_entity(pse);
7012 }
7013 if (se_depth >= pse_depth) {
7014 set_next_entity(cfs_rq_of(se), se);
7015 se = parent_entity(se);
7016 }
7017 }
7018
7019 put_prev_entity(cfs_rq, pse);
7020 set_next_entity(cfs_rq, se);
7021 }
7022
7023 if (hrtick_enabled(rq))
7024 hrtick_start_fair(rq, p);
7025
7026 return p;
7027simple:
7028 cfs_rq = &rq->cfs;
7029#endif
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02007030
Tim Blechmann36ace272009-11-24 11:55:45 +01007031 if (!cfs_rq->nr_running)
Peter Zijlstra38033c32014-01-23 20:32:21 +01007032 goto idle;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02007033
Peter Zijlstra3f1d2a32014-02-12 10:49:30 +01007034 put_prev_task(rq, prev);
Peter Zijlstra606dba22012-02-11 06:05:00 +01007035
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02007036 do {
Peter Zijlstra678d5712012-02-11 06:05:00 +01007037 se = pick_next_entity(cfs_rq, NULL);
Peter Zijlstraf4b67552008-11-04 21:25:07 +01007038 set_next_entity(cfs_rq, se);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02007039 cfs_rq = group_cfs_rq(se);
7040 } while (cfs_rq);
7041
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01007042 p = task_of(se);
Peter Zijlstra678d5712012-02-11 06:05:00 +01007043
Mike Galbraithb39e66e2011-11-22 15:20:07 +01007044 if (hrtick_enabled(rq))
7045 hrtick_start_fair(rq, p);
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01007046
7047 return p;
Peter Zijlstra38033c32014-01-23 20:32:21 +01007048
7049idle:
Peter Zijlstracbce1a62015-06-11 14:46:54 +02007050 /*
7051 * This is OK, because current is on_cpu, which avoids it being picked
7052 * for load-balance and preemption/IRQs are still disabled avoiding
7053 * further scheduler activity on it and we're being very careful to
7054 * re-start the picking loop.
7055 */
Peter Zijlstrae7904a22015-08-01 19:25:08 +02007056 lockdep_unpin_lock(&rq->lock, cookie);
Kirill Tkhaie4aa3582014-03-06 13:31:55 +04007057 new_tasks = idle_balance(rq);
Peter Zijlstrae7904a22015-08-01 19:25:08 +02007058 lockdep_repin_lock(&rq->lock, cookie);
Peter Zijlstra37e117c2014-02-14 12:25:08 +01007059 /*
7060 * Because idle_balance() releases (and re-acquires) rq->lock, it is
7061 * possible for any higher priority task to appear. In that case we
7062 * must re-start the pick_next_entity() loop.
7063 */
Kirill Tkhaie4aa3582014-03-06 13:31:55 +04007064 if (new_tasks < 0)
Peter Zijlstra37e117c2014-02-14 12:25:08 +01007065 return RETRY_TASK;
7066
Kirill Tkhaie4aa3582014-03-06 13:31:55 +04007067 if (new_tasks > 0)
Peter Zijlstra38033c32014-01-23 20:32:21 +01007068 goto again;
Peter Zijlstra38033c32014-01-23 20:32:21 +01007069
7070 return NULL;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02007071}
7072
7073/*
7074 * Account for a descheduled task:
7075 */
Ingo Molnar31ee5292007-08-09 11:16:49 +02007076static void put_prev_task_fair(struct rq *rq, struct task_struct *prev)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02007077{
7078 struct sched_entity *se = &prev->se;
7079 struct cfs_rq *cfs_rq;
7080
7081 for_each_sched_entity(se) {
7082 cfs_rq = cfs_rq_of(se);
Ingo Molnarab6cde22007-08-09 11:16:48 +02007083 put_prev_entity(cfs_rq, se);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02007084 }
7085}
7086
Rik van Rielac53db52011-02-01 09:51:03 -05007087/*
7088 * sched_yield() is very simple
7089 *
7090 * The magic of dealing with the ->skip buddy is in pick_next_entity.
7091 */
7092static void yield_task_fair(struct rq *rq)
7093{
7094 struct task_struct *curr = rq->curr;
7095 struct cfs_rq *cfs_rq = task_cfs_rq(curr);
7096 struct sched_entity *se = &curr->se;
7097
7098 /*
7099 * Are we the only task in the tree?
7100 */
7101 if (unlikely(rq->nr_running == 1))
7102 return;
7103
7104 clear_buddies(cfs_rq, se);
7105
7106 if (curr->policy != SCHED_BATCH) {
7107 update_rq_clock(rq);
7108 /*
7109 * Update run-time statistics of the 'current'.
7110 */
7111 update_curr(cfs_rq);
Mike Galbraith916671c2011-11-22 15:21:26 +01007112 /*
7113 * Tell update_rq_clock() that we've just updated,
7114 * so we don't do microscopic update in schedule()
7115 * and double the fastpath cost.
7116 */
Peter Zijlstra9edfbfe2015-01-05 11:18:11 +01007117 rq_clock_skip_update(rq, true);
Rik van Rielac53db52011-02-01 09:51:03 -05007118 }
7119
7120 set_skip_buddy(se);
7121}
7122
Mike Galbraithd95f4122011-02-01 09:50:51 -05007123static bool yield_to_task_fair(struct rq *rq, struct task_struct *p, bool preempt)
7124{
7125 struct sched_entity *se = &p->se;
7126
Paul Turner5238cdd2011-07-21 09:43:37 -07007127 /* throttled hierarchies are not runnable */
7128 if (!se->on_rq || throttled_hierarchy(cfs_rq_of(se)))
Mike Galbraithd95f4122011-02-01 09:50:51 -05007129 return false;
7130
7131 /* Tell the scheduler that we'd really like pse to run next. */
7132 set_next_buddy(se);
7133
Mike Galbraithd95f4122011-02-01 09:50:51 -05007134 yield_task_fair(rq);
7135
7136 return true;
7137}
7138
Peter Williams681f3e62007-10-24 18:23:51 +02007139#ifdef CONFIG_SMP
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02007140/**************************************************
Peter Zijlstrae9c84cb2012-07-03 13:53:26 +02007141 * Fair scheduling class load-balancing methods.
7142 *
7143 * BASICS
7144 *
7145 * The purpose of load-balancing is to achieve the same basic fairness the
7146 * per-cpu scheduler provides, namely provide a proportional amount of compute
7147 * time to each task. This is expressed in the following equation:
7148 *
7149 * W_i,n/P_i == W_j,n/P_j for all i,j (1)
7150 *
7151 * Where W_i,n is the n-th weight average for cpu i. The instantaneous weight
7152 * W_i,0 is defined as:
7153 *
7154 * W_i,0 = \Sum_j w_i,j (2)
7155 *
7156 * 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 +08007157 * is derived from the nice value as per sched_prio_to_weight[].
Peter Zijlstrae9c84cb2012-07-03 13:53:26 +02007158 *
7159 * The weight average is an exponential decay average of the instantaneous
7160 * weight:
7161 *
7162 * W'_i,n = (2^n - 1) / 2^n * W_i,n + 1 / 2^n * W_i,0 (3)
7163 *
Nicolas Pitreced549f2014-05-26 18:19:38 -04007164 * C_i is the compute capacity of cpu i, typically it is the
Peter Zijlstrae9c84cb2012-07-03 13:53:26 +02007165 * fraction of 'recent' time available for SCHED_OTHER task execution. But it
7166 * can also include other factors [XXX].
7167 *
7168 * To achieve this balance we define a measure of imbalance which follows
7169 * directly from (1):
7170 *
Nicolas Pitreced549f2014-05-26 18:19:38 -04007171 * 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 +02007172 *
7173 * We them move tasks around to minimize the imbalance. In the continuous
7174 * function space it is obvious this converges, in the discrete case we get
7175 * a few fun cases generally called infeasible weight scenarios.
7176 *
7177 * [XXX expand on:
7178 * - infeasible weights;
7179 * - local vs global optima in the discrete case. ]
7180 *
7181 *
7182 * SCHED DOMAINS
7183 *
7184 * In order to solve the imbalance equation (4), and avoid the obvious O(n^2)
7185 * for all i,j solution, we create a tree of cpus that follows the hardware
7186 * topology where each level pairs two lower groups (or better). This results
7187 * in O(log n) layers. Furthermore we reduce the number of cpus going up the
7188 * tree to only the first of the previous level and we decrease the frequency
7189 * of load-balance at each level inv. proportional to the number of cpus in
7190 * the groups.
7191 *
7192 * This yields:
7193 *
7194 * log_2 n 1 n
7195 * \Sum { --- * --- * 2^i } = O(n) (5)
7196 * i = 0 2^i 2^i
7197 * `- size of each group
7198 * | | `- number of cpus doing load-balance
7199 * | `- freq
7200 * `- sum over all levels
7201 *
7202 * Coupled with a limit on how many tasks we can migrate every balance pass,
7203 * this makes (5) the runtime complexity of the balancer.
7204 *
7205 * An important property here is that each CPU is still (indirectly) connected
7206 * to every other cpu in at most O(log n) steps:
7207 *
7208 * The adjacency matrix of the resulting graph is given by:
7209 *
Byungchul Park97a71422015-07-05 18:33:48 +09007210 * log_2 n
Peter Zijlstrae9c84cb2012-07-03 13:53:26 +02007211 * A_i,j = \Union (i % 2^k == 0) && i / 2^(k+1) == j / 2^(k+1) (6)
7212 * k = 0
7213 *
7214 * And you'll find that:
7215 *
7216 * A^(log_2 n)_i,j != 0 for all i,j (7)
7217 *
7218 * Showing there's indeed a path between every cpu in at most O(log n) steps.
7219 * The task movement gives a factor of O(m), giving a convergence complexity
7220 * of:
7221 *
7222 * O(nm log n), n := nr_cpus, m := nr_tasks (8)
7223 *
7224 *
7225 * WORK CONSERVING
7226 *
7227 * In order to avoid CPUs going idle while there's still work to do, new idle
7228 * balancing is more aggressive and has the newly idle cpu iterate up the domain
7229 * tree itself instead of relying on other CPUs to bring it work.
7230 *
7231 * This adds some complexity to both (5) and (8) but it reduces the total idle
7232 * time.
7233 *
7234 * [XXX more?]
7235 *
7236 *
7237 * CGROUPS
7238 *
7239 * Cgroups make a horror show out of (2), instead of a simple sum we get:
7240 *
7241 * s_k,i
7242 * W_i,0 = \Sum_j \Prod_k w_k * ----- (9)
7243 * S_k
7244 *
7245 * Where
7246 *
7247 * s_k,i = \Sum_j w_i,j,k and S_k = \Sum_i s_k,i (10)
7248 *
7249 * w_i,j,k is the weight of the j-th runnable task in the k-th cgroup on cpu i.
7250 *
7251 * The big problem is S_k, its a global sum needed to compute a local (W_i)
7252 * property.
7253 *
7254 * [XXX write more on how we solve this.. _after_ merging pjt's patches that
7255 * rewrite all of this once again.]
Byungchul Park97a71422015-07-05 18:33:48 +09007256 */
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02007257
Hiroshi Shimamotoed387b72012-01-31 11:40:32 +09007258static unsigned long __read_mostly max_load_balance_interval = HZ/10;
7259
Peter Zijlstra0ec8aa02013-10-07 11:29:33 +01007260enum fbq_type { regular, remote, all };
7261
Peter Zijlstraddcdf6e2012-02-22 19:27:40 +01007262#define LBF_ALL_PINNED 0x01
Peter Zijlstra367456c2012-02-20 21:49:09 +01007263#define LBF_NEED_BREAK 0x02
Peter Zijlstra62633222013-08-19 12:41:09 +02007264#define LBF_DST_PINNED 0x04
7265#define LBF_SOME_PINNED 0x08
Syed Rameez Mustafadddcab72016-09-07 16:18:27 -07007266#define LBF_SCHED_BOOST_ACTIVE_BALANCE 0x40
7267#define LBF_BIG_TASK_ACTIVE_BALANCE 0x80
7268#define LBF_HMP_ACTIVE_BALANCE (LBF_SCHED_BOOST_ACTIVE_BALANCE | \
7269 LBF_BIG_TASK_ACTIVE_BALANCE)
7270#define LBF_IGNORE_BIG_TASKS 0x100
7271#define LBF_IGNORE_PREFERRED_CLUSTER_TASKS 0x200
7272#define LBF_MOVED_RELATED_THREAD_GROUP_TASK 0x400
Peter Zijlstraddcdf6e2012-02-22 19:27:40 +01007273
7274struct lb_env {
7275 struct sched_domain *sd;
7276
Peter Zijlstraddcdf6e2012-02-22 19:27:40 +01007277 struct rq *src_rq;
Prashanth Nageshappa85c1e7d2012-06-19 17:47:34 +05307278 int src_cpu;
Peter Zijlstraddcdf6e2012-02-22 19:27:40 +01007279
7280 int dst_cpu;
7281 struct rq *dst_rq;
7282
Srivatsa Vaddagiri88b8dac2012-06-19 17:43:15 +05307283 struct cpumask *dst_grpmask;
7284 int new_dst_cpu;
Peter Zijlstraddcdf6e2012-02-22 19:27:40 +01007285 enum cpu_idle_type idle;
Peter Zijlstrabd939f42012-05-02 14:20:37 +02007286 long imbalance;
Michael Wangb94031302012-07-12 16:10:13 +08007287 /* The set of CPUs under consideration for load-balancing */
7288 struct cpumask *cpus;
Syed Rameez Mustafadddcab72016-09-07 16:18:27 -07007289 unsigned int busiest_grp_capacity;
7290 unsigned int busiest_nr_running;
Michael Wangb94031302012-07-12 16:10:13 +08007291
Peter Zijlstraddcdf6e2012-02-22 19:27:40 +01007292 unsigned int flags;
Peter Zijlstra367456c2012-02-20 21:49:09 +01007293
7294 unsigned int loop;
7295 unsigned int loop_break;
7296 unsigned int loop_max;
Peter Zijlstra0ec8aa02013-10-07 11:29:33 +01007297
7298 enum fbq_type fbq_type;
Kirill Tkhai163122b2014-08-20 13:48:29 +04007299 struct list_head tasks;
Peter Zijlstraddcdf6e2012-02-22 19:27:40 +01007300};
7301
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007302/*
Peter Zijlstra029632f2011-10-25 10:00:11 +02007303 * Is this task likely cache-hot:
7304 */
Hillf Danton5d5e2b12014-06-10 10:58:43 +02007305static int task_hot(struct task_struct *p, struct lb_env *env)
Peter Zijlstra029632f2011-10-25 10:00:11 +02007306{
7307 s64 delta;
7308
Kirill Tkhaie5673f22014-08-20 13:48:01 +04007309 lockdep_assert_held(&env->src_rq->lock);
7310
Peter Zijlstra029632f2011-10-25 10:00:11 +02007311 if (p->sched_class != &fair_sched_class)
7312 return 0;
7313
7314 if (unlikely(p->policy == SCHED_IDLE))
7315 return 0;
7316
7317 /*
7318 * Buddy candidates are cache hot:
7319 */
Hillf Danton5d5e2b12014-06-10 10:58:43 +02007320 if (sched_feat(CACHE_HOT_BUDDY) && env->dst_rq->nr_running &&
Peter Zijlstra029632f2011-10-25 10:00:11 +02007321 (&p->se == cfs_rq_of(&p->se)->next ||
7322 &p->se == cfs_rq_of(&p->se)->last))
7323 return 1;
7324
7325 if (sysctl_sched_migration_cost == -1)
7326 return 1;
7327 if (sysctl_sched_migration_cost == 0)
7328 return 0;
7329
Hillf Danton5d5e2b12014-06-10 10:58:43 +02007330 delta = rq_clock_task(env->src_rq) - p->se.exec_start;
Peter Zijlstra029632f2011-10-25 10:00:11 +02007331
7332 return delta < (s64)sysctl_sched_migration_cost;
7333}
7334
Mel Gorman3a7053b2013-10-07 11:29:00 +01007335#ifdef CONFIG_NUMA_BALANCING
Rik van Rielc1ceac62015-05-14 22:59:36 -04007336/*
Srikar Dronamraju2a1ed242015-06-16 17:25:59 +05307337 * Returns 1, if task migration degrades locality
7338 * Returns 0, if task migration improves locality i.e migration preferred.
7339 * Returns -1, if task migration is not affected by locality.
Rik van Rielc1ceac62015-05-14 22:59:36 -04007340 */
Srikar Dronamraju2a1ed242015-06-16 17:25:59 +05307341static int migrate_degrades_locality(struct task_struct *p, struct lb_env *env)
Mel Gorman3a7053b2013-10-07 11:29:00 +01007342{
Rik van Rielb1ad0652014-05-15 13:03:06 -04007343 struct numa_group *numa_group = rcu_dereference(p->numa_group);
Rik van Rielc1ceac62015-05-14 22:59:36 -04007344 unsigned long src_faults, dst_faults;
Mel Gorman3a7053b2013-10-07 11:29:00 +01007345 int src_nid, dst_nid;
7346
Srikar Dronamraju2a595722015-08-11 21:54:21 +05307347 if (!static_branch_likely(&sched_numa_balancing))
Srikar Dronamraju2a1ed242015-06-16 17:25:59 +05307348 return -1;
7349
Srikar Dronamrajuc3b9bc52015-08-11 16:30:12 +05307350 if (!p->numa_faults || !(env->sd->flags & SD_NUMA))
Srikar Dronamraju2a1ed242015-06-16 17:25:59 +05307351 return -1;
Mel Gorman7a0f3082013-10-07 11:29:01 +01007352
7353 src_nid = cpu_to_node(env->src_cpu);
7354 dst_nid = cpu_to_node(env->dst_cpu);
7355
Mel Gorman83e1d2c2013-10-07 11:29:27 +01007356 if (src_nid == dst_nid)
Srikar Dronamraju2a1ed242015-06-16 17:25:59 +05307357 return -1;
Mel Gorman7a0f3082013-10-07 11:29:01 +01007358
Srikar Dronamraju2a1ed242015-06-16 17:25:59 +05307359 /* Migrating away from the preferred node is always bad. */
7360 if (src_nid == p->numa_preferred_nid) {
7361 if (env->src_rq->nr_running > env->src_rq->nr_preferred_running)
7362 return 1;
7363 else
7364 return -1;
7365 }
Mel Gorman83e1d2c2013-10-07 11:29:27 +01007366
Rik van Rielc1ceac62015-05-14 22:59:36 -04007367 /* Encourage migration to the preferred node. */
7368 if (dst_nid == p->numa_preferred_nid)
Srikar Dronamraju2a1ed242015-06-16 17:25:59 +05307369 return 0;
Rik van Rielc1ceac62015-05-14 22:59:36 -04007370
7371 if (numa_group) {
7372 src_faults = group_faults(p, src_nid);
7373 dst_faults = group_faults(p, dst_nid);
7374 } else {
7375 src_faults = task_faults(p, src_nid);
7376 dst_faults = task_faults(p, dst_nid);
7377 }
7378
7379 return dst_faults < src_faults;
Mel Gorman7a0f3082013-10-07 11:29:01 +01007380}
7381
Mel Gorman3a7053b2013-10-07 11:29:00 +01007382#else
Srikar Dronamraju2a1ed242015-06-16 17:25:59 +05307383static inline int migrate_degrades_locality(struct task_struct *p,
Mel Gorman3a7053b2013-10-07 11:29:00 +01007384 struct lb_env *env)
7385{
Srikar Dronamraju2a1ed242015-06-16 17:25:59 +05307386 return -1;
Mel Gorman7a0f3082013-10-07 11:29:01 +01007387}
Mel Gorman3a7053b2013-10-07 11:29:00 +01007388#endif
7389
Peter Zijlstra029632f2011-10-25 10:00:11 +02007390/*
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007391 * can_migrate_task - may task p from runqueue rq be migrated to this_cpu?
7392 */
7393static
Peter Zijlstra8e45cb52012-02-22 12:47:19 +01007394int can_migrate_task(struct task_struct *p, struct lb_env *env)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007395{
Srikar Dronamraju2a1ed242015-06-16 17:25:59 +05307396 int tsk_cache_hot;
Syed Rameez Mustafadddcab72016-09-07 16:18:27 -07007397 int twf, group_cpus;
Kirill Tkhaie5673f22014-08-20 13:48:01 +04007398
7399 lockdep_assert_held(&env->src_rq->lock);
7400
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007401 /*
7402 * We do not migrate tasks that are:
Joonsoo Kimd3198082013-04-23 17:27:40 +09007403 * 1) throttled_lb_pair, or
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007404 * 2) cannot be migrated to this CPU due to cpus_allowed, or
Joonsoo Kimd3198082013-04-23 17:27:40 +09007405 * 3) running (obviously), or
7406 * 4) are cache-hot on their current CPU.
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007407 */
Joonsoo Kimd3198082013-04-23 17:27:40 +09007408 if (throttled_lb_pair(task_group(p), env->src_cpu, env->dst_cpu))
7409 return 0;
7410
Peter Zijlstraddcdf6e2012-02-22 19:27:40 +01007411 if (!cpumask_test_cpu(env->dst_cpu, tsk_cpus_allowed(p))) {
Joonsoo Kime02e60c2013-04-23 17:27:42 +09007412 int cpu;
Srivatsa Vaddagiri88b8dac2012-06-19 17:43:15 +05307413
Josh Poimboeufae928822016-06-17 12:43:24 -05007414 schedstat_inc(p->se.statistics.nr_failed_migrations_affine);
Srivatsa Vaddagiri88b8dac2012-06-19 17:43:15 +05307415
Peter Zijlstra62633222013-08-19 12:41:09 +02007416 env->flags |= LBF_SOME_PINNED;
7417
Srivatsa Vaddagiri88b8dac2012-06-19 17:43:15 +05307418 /*
7419 * Remember if this task can be migrated to any other cpu in
7420 * our sched_group. We may want to revisit it if we couldn't
7421 * meet load balance goals by pulling other tasks on src_cpu.
7422 *
7423 * Also avoid computing new_dst_cpu if we have already computed
7424 * one in current iteration.
7425 */
Peter Zijlstra62633222013-08-19 12:41:09 +02007426 if (!env->dst_grpmask || (env->flags & LBF_DST_PINNED))
Srivatsa Vaddagiri88b8dac2012-06-19 17:43:15 +05307427 return 0;
7428
Joonsoo Kime02e60c2013-04-23 17:27:42 +09007429 /* Prevent to re-select dst_cpu via env's cpus */
7430 for_each_cpu_and(cpu, env->dst_grpmask, env->cpus) {
7431 if (cpumask_test_cpu(cpu, tsk_cpus_allowed(p))) {
Peter Zijlstra62633222013-08-19 12:41:09 +02007432 env->flags |= LBF_DST_PINNED;
Joonsoo Kime02e60c2013-04-23 17:27:42 +09007433 env->new_dst_cpu = cpu;
7434 break;
7435 }
Srivatsa Vaddagiri88b8dac2012-06-19 17:43:15 +05307436 }
Joonsoo Kime02e60c2013-04-23 17:27:42 +09007437
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007438 return 0;
7439 }
Srivatsa Vaddagiri88b8dac2012-06-19 17:43:15 +05307440
7441 /* Record that we found atleast one task that could run on dst_cpu */
Peter Zijlstra8e45cb52012-02-22 12:47:19 +01007442 env->flags &= ~LBF_ALL_PINNED;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007443
Syed Rameez Mustafadddcab72016-09-07 16:18:27 -07007444 if (cpu_capacity(env->dst_cpu) > cpu_capacity(env->src_cpu) &&
7445 nr_big_tasks(env->src_rq) && !is_big_task(p))
7446 return 0;
7447
7448 twf = task_will_fit(p, env->dst_cpu);
7449
7450 /*
7451 * Attempt to not pull tasks that don't fit. We may get lucky and find
7452 * one that actually fits.
7453 */
7454 if (env->flags & LBF_IGNORE_BIG_TASKS && !twf)
7455 return 0;
7456
7457 if (env->flags & LBF_IGNORE_PREFERRED_CLUSTER_TASKS &&
7458 !preferred_cluster(rq_cluster(cpu_rq(env->dst_cpu)), p))
7459 return 0;
7460
7461 /*
7462 * Group imbalance can sometimes cause work to be pulled across groups
7463 * even though the group could have managed the imbalance on its own.
7464 * Prevent inter-cluster migrations for big tasks when the number of
7465 * tasks is lower than the capacity of the group.
7466 */
7467 group_cpus = DIV_ROUND_UP(env->busiest_grp_capacity,
7468 SCHED_CAPACITY_SCALE);
7469 if (!twf && env->busiest_nr_running <= group_cpus)
7470 return 0;
7471
Peter Zijlstraddcdf6e2012-02-22 19:27:40 +01007472 if (task_running(env->src_rq, p)) {
Josh Poimboeufae928822016-06-17 12:43:24 -05007473 schedstat_inc(p->se.statistics.nr_failed_migrations_running);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007474 return 0;
7475 }
7476
7477 /*
7478 * Aggressive migration if:
Syed Rameez Mustafadddcab72016-09-07 16:18:27 -07007479 * 1) IDLE or NEWLY_IDLE balance.
7480 * 2) destination numa is preferred
7481 * 3) task is cache cold, or
7482 * 4) too many balance attempts have failed.
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007483 */
Srikar Dronamraju2a1ed242015-06-16 17:25:59 +05307484 tsk_cache_hot = migrate_degrades_locality(p, env);
7485 if (tsk_cache_hot == -1)
7486 tsk_cache_hot = task_hot(p, env);
Mel Gorman3a7053b2013-10-07 11:29:00 +01007487
Syed Rameez Mustafadddcab72016-09-07 16:18:27 -07007488 if (env->idle != CPU_NOT_IDLE || tsk_cache_hot <= 0 ||
Kirill Tkhai7a96c232014-09-22 22:36:12 +04007489 env->sd->nr_balance_failed > env->sd->cache_nice_tries) {
Srikar Dronamraju2a1ed242015-06-16 17:25:59 +05307490 if (tsk_cache_hot == 1) {
Josh Poimboeufae928822016-06-17 12:43:24 -05007491 schedstat_inc(env->sd->lb_hot_gained[env->idle]);
7492 schedstat_inc(p->se.statistics.nr_forced_migrations);
Mel Gorman3a7053b2013-10-07 11:29:00 +01007493 }
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007494 return 1;
7495 }
7496
Josh Poimboeufae928822016-06-17 12:43:24 -05007497 schedstat_inc(p->se.statistics.nr_failed_migrations_hot);
Zhang Hang4e2dcb72013-04-10 14:04:55 +08007498 return 0;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007499}
7500
Peter Zijlstra897c3952009-12-17 17:45:42 +01007501/*
Kirill Tkhai163122b2014-08-20 13:48:29 +04007502 * detach_task() -- detach the task for the migration specified in env
Peter Zijlstra897c3952009-12-17 17:45:42 +01007503 */
Kirill Tkhai163122b2014-08-20 13:48:29 +04007504static void detach_task(struct task_struct *p, struct lb_env *env)
7505{
7506 lockdep_assert_held(&env->src_rq->lock);
7507
Kirill Tkhai163122b2014-08-20 13:48:29 +04007508 p->on_rq = TASK_ON_RQ_MIGRATING;
Joonwoo Park3ea94de2015-11-12 19:38:54 -08007509 deactivate_task(env->src_rq, p, 0);
Syed Rameez Mustafadddcab72016-09-07 16:18:27 -07007510 double_lock_balance(env->src_rq, env->dst_rq);
Kirill Tkhai163122b2014-08-20 13:48:29 +04007511 set_task_cpu(p, env->dst_cpu);
Syed Rameez Mustafadddcab72016-09-07 16:18:27 -07007512 if (task_in_related_thread_group(p))
7513 env->flags |= LBF_MOVED_RELATED_THREAD_GROUP_TASK;
7514 double_unlock_balance(env->src_rq, env->dst_rq);
Kirill Tkhai163122b2014-08-20 13:48:29 +04007515}
7516
7517/*
Kirill Tkhaie5673f22014-08-20 13:48:01 +04007518 * detach_one_task() -- tries to dequeue exactly one task from env->src_rq, as
Peter Zijlstra897c3952009-12-17 17:45:42 +01007519 * part of active balancing operations within "domain".
Peter Zijlstra897c3952009-12-17 17:45:42 +01007520 *
Kirill Tkhaie5673f22014-08-20 13:48:01 +04007521 * Returns a task if successful and NULL otherwise.
Peter Zijlstra897c3952009-12-17 17:45:42 +01007522 */
Kirill Tkhaie5673f22014-08-20 13:48:01 +04007523static struct task_struct *detach_one_task(struct lb_env *env)
Peter Zijlstra897c3952009-12-17 17:45:42 +01007524{
7525 struct task_struct *p, *n;
Peter Zijlstra897c3952009-12-17 17:45:42 +01007526
Kirill Tkhaie5673f22014-08-20 13:48:01 +04007527 lockdep_assert_held(&env->src_rq->lock);
7528
Peter Zijlstra367456c2012-02-20 21:49:09 +01007529 list_for_each_entry_safe(p, n, &env->src_rq->cfs_tasks, se.group_node) {
Peter Zijlstra367456c2012-02-20 21:49:09 +01007530 if (!can_migrate_task(p, env))
7531 continue;
Peter Zijlstra897c3952009-12-17 17:45:42 +01007532
Kirill Tkhai163122b2014-08-20 13:48:29 +04007533 detach_task(p, env);
Kirill Tkhaie5673f22014-08-20 13:48:01 +04007534
Peter Zijlstra367456c2012-02-20 21:49:09 +01007535 /*
Kirill Tkhaie5673f22014-08-20 13:48:01 +04007536 * Right now, this is only the second place where
Kirill Tkhai163122b2014-08-20 13:48:29 +04007537 * lb_gained[env->idle] is updated (other is detach_tasks)
Kirill Tkhaie5673f22014-08-20 13:48:01 +04007538 * so we can safely collect stats here rather than
Kirill Tkhai163122b2014-08-20 13:48:29 +04007539 * inside detach_tasks().
Peter Zijlstra367456c2012-02-20 21:49:09 +01007540 */
Josh Poimboeufae928822016-06-17 12:43:24 -05007541 schedstat_inc(env->sd->lb_gained[env->idle]);
Kirill Tkhaie5673f22014-08-20 13:48:01 +04007542 return p;
Peter Zijlstra897c3952009-12-17 17:45:42 +01007543 }
Kirill Tkhaie5673f22014-08-20 13:48:01 +04007544 return NULL;
Peter Zijlstra897c3952009-12-17 17:45:42 +01007545}
7546
Peter Zijlstraeb953082012-04-17 13:38:40 +02007547static const unsigned int sched_nr_migrate_break = 32;
7548
Peter Zijlstra5d6523e2012-03-10 00:07:36 +01007549/*
Kirill Tkhai163122b2014-08-20 13:48:29 +04007550 * detach_tasks() -- tries to detach up to imbalance weighted load from
7551 * busiest_rq, as part of a balancing operation within domain "sd".
Peter Zijlstra5d6523e2012-03-10 00:07:36 +01007552 *
Kirill Tkhai163122b2014-08-20 13:48:29 +04007553 * Returns number of detached tasks if successful and 0 otherwise.
Peter Zijlstra5d6523e2012-03-10 00:07:36 +01007554 */
Kirill Tkhai163122b2014-08-20 13:48:29 +04007555static int detach_tasks(struct lb_env *env)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007556{
Peter Zijlstra5d6523e2012-03-10 00:07:36 +01007557 struct list_head *tasks = &env->src_rq->cfs_tasks;
7558 struct task_struct *p;
Peter Zijlstra367456c2012-02-20 21:49:09 +01007559 unsigned long load;
Kirill Tkhai163122b2014-08-20 13:48:29 +04007560 int detached = 0;
Syed Rameez Mustafadddcab72016-09-07 16:18:27 -07007561 int orig_loop = env->loop;
Kirill Tkhai163122b2014-08-20 13:48:29 +04007562
7563 lockdep_assert_held(&env->src_rq->lock);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007564
Peter Zijlstrabd939f42012-05-02 14:20:37 +02007565 if (env->imbalance <= 0)
Peter Zijlstra5d6523e2012-03-10 00:07:36 +01007566 return 0;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007567
Syed Rameez Mustafadddcab72016-09-07 16:18:27 -07007568 if (cpu_capacity(env->dst_cpu) < cpu_capacity(env->src_cpu) &&
7569 !sched_boost())
7570 env->flags |= LBF_IGNORE_BIG_TASKS;
7571 else if (!same_cluster(env->dst_cpu, env->src_cpu))
7572 env->flags |= LBF_IGNORE_PREFERRED_CLUSTER_TASKS;
7573
7574redo:
Peter Zijlstra5d6523e2012-03-10 00:07:36 +01007575 while (!list_empty(tasks)) {
Yuyang Du985d3a42015-07-06 06:11:51 +08007576 /*
7577 * We don't want to steal all, otherwise we may be treated likewise,
7578 * which could at worst lead to a livelock crash.
7579 */
7580 if (env->idle != CPU_NOT_IDLE && env->src_rq->nr_running <= 1)
7581 break;
7582
Peter Zijlstra5d6523e2012-03-10 00:07:36 +01007583 p = list_first_entry(tasks, struct task_struct, se.group_node);
7584
Peter Zijlstra367456c2012-02-20 21:49:09 +01007585 env->loop++;
7586 /* We've more or less seen every task there is, call it quits */
Peter Zijlstra5d6523e2012-03-10 00:07:36 +01007587 if (env->loop > env->loop_max)
Peter Zijlstra367456c2012-02-20 21:49:09 +01007588 break;
Peter Zijlstra5d6523e2012-03-10 00:07:36 +01007589
7590 /* take a breather every nr_migrate tasks */
Peter Zijlstra367456c2012-02-20 21:49:09 +01007591 if (env->loop > env->loop_break) {
Peter Zijlstraeb953082012-04-17 13:38:40 +02007592 env->loop_break += sched_nr_migrate_break;
Peter Zijlstra8e45cb52012-02-22 12:47:19 +01007593 env->flags |= LBF_NEED_BREAK;
Peter Zijlstraee00e662009-12-17 17:25:20 +01007594 break;
Peter Zijlstraa195f002011-09-22 15:30:18 +02007595 }
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007596
Joonsoo Kimd3198082013-04-23 17:27:40 +09007597 if (!can_migrate_task(p, env))
Peter Zijlstra367456c2012-02-20 21:49:09 +01007598 goto next;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007599
Peter Zijlstra367456c2012-02-20 21:49:09 +01007600 load = task_h_load(p);
Peter Zijlstra5d6523e2012-03-10 00:07:36 +01007601
Peter Zijlstraeb953082012-04-17 13:38:40 +02007602 if (sched_feat(LB_MIN) && load < 16 && !env->sd->nr_balance_failed)
Peter Zijlstra367456c2012-02-20 21:49:09 +01007603 goto next;
7604
Peter Zijlstrabd939f42012-05-02 14:20:37 +02007605 if ((load / 2) > env->imbalance)
Peter Zijlstra367456c2012-02-20 21:49:09 +01007606 goto next;
7607
Kirill Tkhai163122b2014-08-20 13:48:29 +04007608 detach_task(p, env);
7609 list_add(&p->se.group_node, &env->tasks);
7610
7611 detached++;
Peter Zijlstrabd939f42012-05-02 14:20:37 +02007612 env->imbalance -= load;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007613
7614#ifdef CONFIG_PREEMPT
Peter Zijlstraee00e662009-12-17 17:25:20 +01007615 /*
7616 * NEWIDLE balancing is a source of latency, so preemptible
Kirill Tkhai163122b2014-08-20 13:48:29 +04007617 * kernels will stop after the first task is detached to minimize
Peter Zijlstraee00e662009-12-17 17:25:20 +01007618 * the critical section.
7619 */
Peter Zijlstra5d6523e2012-03-10 00:07:36 +01007620 if (env->idle == CPU_NEWLY_IDLE)
Peter Zijlstraee00e662009-12-17 17:25:20 +01007621 break;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007622#endif
7623
Peter Zijlstraee00e662009-12-17 17:25:20 +01007624 /*
7625 * We only want to steal up to the prescribed amount of
7626 * weighted load.
7627 */
Peter Zijlstrabd939f42012-05-02 14:20:37 +02007628 if (env->imbalance <= 0)
Peter Zijlstraee00e662009-12-17 17:25:20 +01007629 break;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007630
Peter Zijlstra367456c2012-02-20 21:49:09 +01007631 continue;
7632next:
Peter Zijlstra5d6523e2012-03-10 00:07:36 +01007633 list_move_tail(&p->se.group_node, tasks);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007634 }
Peter Zijlstra5d6523e2012-03-10 00:07:36 +01007635
Syed Rameez Mustafadddcab72016-09-07 16:18:27 -07007636 if (env->flags & (LBF_IGNORE_BIG_TASKS |
7637 LBF_IGNORE_PREFERRED_CLUSTER_TASKS) && !detached) {
7638 tasks = &env->src_rq->cfs_tasks;
7639 env->flags &= ~(LBF_IGNORE_BIG_TASKS |
7640 LBF_IGNORE_PREFERRED_CLUSTER_TASKS);
7641 env->loop = orig_loop;
7642 goto redo;
7643 }
7644
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007645 /*
Kirill Tkhai163122b2014-08-20 13:48:29 +04007646 * Right now, this is one of only two places we collect this stat
7647 * so we can safely collect detach_one_task() stats here rather
7648 * than inside detach_one_task().
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007649 */
Josh Poimboeufae928822016-06-17 12:43:24 -05007650 schedstat_add(env->sd->lb_gained[env->idle], detached);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007651
Kirill Tkhai163122b2014-08-20 13:48:29 +04007652 return detached;
7653}
7654
7655/*
7656 * attach_task() -- attach the task detached by detach_task() to its new rq.
7657 */
7658static void attach_task(struct rq *rq, struct task_struct *p)
7659{
7660 lockdep_assert_held(&rq->lock);
7661
7662 BUG_ON(task_rq(p) != rq);
Kirill Tkhai163122b2014-08-20 13:48:29 +04007663 activate_task(rq, p, 0);
Joonwoo Park3ea94de2015-11-12 19:38:54 -08007664 p->on_rq = TASK_ON_RQ_QUEUED;
Kirill Tkhai163122b2014-08-20 13:48:29 +04007665 check_preempt_curr(rq, p, 0);
7666}
7667
7668/*
7669 * attach_one_task() -- attaches the task returned from detach_one_task() to
7670 * its new rq.
7671 */
7672static void attach_one_task(struct rq *rq, struct task_struct *p)
7673{
7674 raw_spin_lock(&rq->lock);
7675 attach_task(rq, p);
7676 raw_spin_unlock(&rq->lock);
7677}
7678
7679/*
7680 * attach_tasks() -- attaches all tasks detached by detach_tasks() to their
7681 * new rq.
7682 */
7683static void attach_tasks(struct lb_env *env)
7684{
7685 struct list_head *tasks = &env->tasks;
7686 struct task_struct *p;
7687
7688 raw_spin_lock(&env->dst_rq->lock);
7689
7690 while (!list_empty(tasks)) {
7691 p = list_first_entry(tasks, struct task_struct, se.group_node);
7692 list_del_init(&p->se.group_node);
7693
7694 attach_task(env->dst_rq, p);
7695 }
7696
7697 raw_spin_unlock(&env->dst_rq->lock);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007698}
7699
Peter Zijlstra230059de2009-12-17 17:47:12 +01007700#ifdef CONFIG_FAIR_GROUP_SCHED
Paul Turner48a16752012-10-04 13:18:31 +02007701static void update_blocked_averages(int cpu)
Peter Zijlstra9e3081c2010-11-15 15:47:02 -08007702{
Peter Zijlstra9e3081c2010-11-15 15:47:02 -08007703 struct rq *rq = cpu_rq(cpu);
Paul Turner48a16752012-10-04 13:18:31 +02007704 struct cfs_rq *cfs_rq;
7705 unsigned long flags;
Peter Zijlstra9e3081c2010-11-15 15:47:02 -08007706
Paul Turner48a16752012-10-04 13:18:31 +02007707 raw_spin_lock_irqsave(&rq->lock, flags);
7708 update_rq_clock(rq);
Yuyang Du9d89c252015-07-15 08:04:37 +08007709
Peter Zijlstra9763b672011-07-13 13:09:25 +02007710 /*
7711 * Iterates the task_group tree in a bottom up fashion, see
7712 * list_add_leaf_cfs_rq() for details.
7713 */
Paul Turner64660c82011-07-21 09:43:36 -07007714 for_each_leaf_cfs_rq(rq, cfs_rq) {
Yuyang Du9d89c252015-07-15 08:04:37 +08007715 /* throttled entities do not contribute to load */
7716 if (throttled_hierarchy(cfs_rq))
7717 continue;
Paul Turner48a16752012-10-04 13:18:31 +02007718
Steve Mucklea2c6c912016-03-24 15:26:07 -07007719 if (update_cfs_rq_load_avg(cfs_rq_clock_task(cfs_rq), cfs_rq, true))
Yuyang Du9d89c252015-07-15 08:04:37 +08007720 update_tg_load_avg(cfs_rq, 0);
7721 }
Paul Turner48a16752012-10-04 13:18:31 +02007722 raw_spin_unlock_irqrestore(&rq->lock, flags);
Peter Zijlstra9e3081c2010-11-15 15:47:02 -08007723}
7724
Peter Zijlstra9763b672011-07-13 13:09:25 +02007725/*
Vladimir Davydov68520792013-07-15 17:49:19 +04007726 * Compute the hierarchical load factor for cfs_rq and all its ascendants.
Peter Zijlstra9763b672011-07-13 13:09:25 +02007727 * This needs to be done in a top-down fashion because the load of a child
7728 * group is a fraction of its parents load.
7729 */
Vladimir Davydov68520792013-07-15 17:49:19 +04007730static void update_cfs_rq_h_load(struct cfs_rq *cfs_rq)
Peter Zijlstra9763b672011-07-13 13:09:25 +02007731{
Vladimir Davydov68520792013-07-15 17:49:19 +04007732 struct rq *rq = rq_of(cfs_rq);
7733 struct sched_entity *se = cfs_rq->tg->se[cpu_of(rq)];
Peter Zijlstraa35b6462012-08-08 21:46:40 +02007734 unsigned long now = jiffies;
Vladimir Davydov68520792013-07-15 17:49:19 +04007735 unsigned long load;
Peter Zijlstraa35b6462012-08-08 21:46:40 +02007736
Vladimir Davydov68520792013-07-15 17:49:19 +04007737 if (cfs_rq->last_h_load_update == now)
Peter Zijlstraa35b6462012-08-08 21:46:40 +02007738 return;
7739
Vladimir Davydov68520792013-07-15 17:49:19 +04007740 cfs_rq->h_load_next = NULL;
7741 for_each_sched_entity(se) {
7742 cfs_rq = cfs_rq_of(se);
7743 cfs_rq->h_load_next = se;
7744 if (cfs_rq->last_h_load_update == now)
7745 break;
7746 }
Peter Zijlstraa35b6462012-08-08 21:46:40 +02007747
Vladimir Davydov68520792013-07-15 17:49:19 +04007748 if (!se) {
Yuyang Du7ea241a2015-07-15 08:04:42 +08007749 cfs_rq->h_load = cfs_rq_load_avg(cfs_rq);
Vladimir Davydov68520792013-07-15 17:49:19 +04007750 cfs_rq->last_h_load_update = now;
7751 }
7752
7753 while ((se = cfs_rq->h_load_next) != NULL) {
7754 load = cfs_rq->h_load;
Yuyang Du7ea241a2015-07-15 08:04:42 +08007755 load = div64_ul(load * se->avg.load_avg,
7756 cfs_rq_load_avg(cfs_rq) + 1);
Vladimir Davydov68520792013-07-15 17:49:19 +04007757 cfs_rq = group_cfs_rq(se);
7758 cfs_rq->h_load = load;
7759 cfs_rq->last_h_load_update = now;
7760 }
Peter Zijlstra9763b672011-07-13 13:09:25 +02007761}
7762
Peter Zijlstra367456c2012-02-20 21:49:09 +01007763static unsigned long task_h_load(struct task_struct *p)
Peter Zijlstra230059de2009-12-17 17:47:12 +01007764{
Peter Zijlstra367456c2012-02-20 21:49:09 +01007765 struct cfs_rq *cfs_rq = task_cfs_rq(p);
Peter Zijlstra230059de2009-12-17 17:47:12 +01007766
Vladimir Davydov68520792013-07-15 17:49:19 +04007767 update_cfs_rq_h_load(cfs_rq);
Yuyang Du9d89c252015-07-15 08:04:37 +08007768 return div64_ul(p->se.avg.load_avg * cfs_rq->h_load,
Yuyang Du7ea241a2015-07-15 08:04:42 +08007769 cfs_rq_load_avg(cfs_rq) + 1);
Peter Zijlstra230059de2009-12-17 17:47:12 +01007770}
7771#else
Paul Turner48a16752012-10-04 13:18:31 +02007772static inline void update_blocked_averages(int cpu)
Peter Zijlstra9e3081c2010-11-15 15:47:02 -08007773{
Vincent Guittot6c1d47c2015-07-15 08:04:38 +08007774 struct rq *rq = cpu_rq(cpu);
7775 struct cfs_rq *cfs_rq = &rq->cfs;
7776 unsigned long flags;
7777
7778 raw_spin_lock_irqsave(&rq->lock, flags);
7779 update_rq_clock(rq);
Steve Mucklea2c6c912016-03-24 15:26:07 -07007780 update_cfs_rq_load_avg(cfs_rq_clock_task(cfs_rq), cfs_rq, true);
Vincent Guittot6c1d47c2015-07-15 08:04:38 +08007781 raw_spin_unlock_irqrestore(&rq->lock, flags);
Peter Zijlstra9e3081c2010-11-15 15:47:02 -08007782}
7783
Peter Zijlstra367456c2012-02-20 21:49:09 +01007784static unsigned long task_h_load(struct task_struct *p)
7785{
Yuyang Du9d89c252015-07-15 08:04:37 +08007786 return p->se.avg.load_avg;
Peter Zijlstra230059de2009-12-17 17:47:12 +01007787}
7788#endif
7789
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007790/********** Helpers for find_busiest_group ************************/
Rik van Rielcaeb1782014-07-28 14:16:28 -04007791
7792enum group_type {
7793 group_other = 0,
7794 group_imbalanced,
7795 group_overloaded,
7796};
7797
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007798/*
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007799 * sg_lb_stats - stats of a sched_group required for load_balancing
7800 */
7801struct sg_lb_stats {
7802 unsigned long avg_load; /*Avg load across the CPUs of the group */
7803 unsigned long group_load; /* Total load over the CPUs of the group */
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007804 unsigned long sum_weighted_load; /* Weighted load of group's tasks */
Joonsoo Kim56cf5152013-08-06 17:36:43 +09007805 unsigned long load_per_task;
Nicolas Pitre63b2ca32014-05-26 18:19:37 -04007806 unsigned long group_capacity;
Dietmar Eggemann9e91d612015-08-14 17:23:12 +01007807 unsigned long group_util; /* Total utilization of the group */
Peter Zijlstra147c5fc2013-08-19 15:22:57 +02007808 unsigned int sum_nr_running; /* Nr tasks running in the group */
Syed Rameez Mustafadddcab72016-09-07 16:18:27 -07007809#ifdef CONFIG_SCHED_HMP
7810 unsigned long sum_nr_big_tasks;
7811 u64 group_cpu_load; /* Scaled load of all CPUs of the group */
7812#endif
Peter Zijlstra147c5fc2013-08-19 15:22:57 +02007813 unsigned int idle_cpus;
7814 unsigned int group_weight;
Rik van Rielcaeb1782014-07-28 14:16:28 -04007815 enum group_type group_type;
Vincent Guittotea678212015-02-27 16:54:11 +01007816 int group_no_capacity;
Peter Zijlstra0ec8aa02013-10-07 11:29:33 +01007817#ifdef CONFIG_NUMA_BALANCING
7818 unsigned int nr_numa_running;
7819 unsigned int nr_preferred_running;
7820#endif
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007821};
7822
Joonsoo Kim56cf5152013-08-06 17:36:43 +09007823/*
7824 * sd_lb_stats - Structure to store the statistics of a sched_domain
7825 * during load balancing.
7826 */
7827struct sd_lb_stats {
7828 struct sched_group *busiest; /* Busiest group in this sd */
7829 struct sched_group *local; /* Local group in this sd */
7830 unsigned long total_load; /* Total load of all groups in sd */
Nicolas Pitre63b2ca32014-05-26 18:19:37 -04007831 unsigned long total_capacity; /* Total capacity of all groups in sd */
Joonsoo Kim56cf5152013-08-06 17:36:43 +09007832 unsigned long avg_load; /* Average load across all groups in sd */
7833
Joonsoo Kim56cf5152013-08-06 17:36:43 +09007834 struct sg_lb_stats busiest_stat;/* Statistics of the busiest group */
Peter Zijlstra147c5fc2013-08-19 15:22:57 +02007835 struct sg_lb_stats local_stat; /* Statistics of the local group */
Joonsoo Kim56cf5152013-08-06 17:36:43 +09007836};
7837
Peter Zijlstra147c5fc2013-08-19 15:22:57 +02007838static inline void init_sd_lb_stats(struct sd_lb_stats *sds)
7839{
7840 /*
7841 * Skimp on the clearing to avoid duplicate work. We can avoid clearing
7842 * local_stat because update_sg_lb_stats() does a full clear/assignment.
7843 * We must however clear busiest_stat::avg_load because
7844 * update_sd_pick_busiest() reads this before assignment.
7845 */
7846 *sds = (struct sd_lb_stats){
7847 .busiest = NULL,
7848 .local = NULL,
7849 .total_load = 0UL,
Nicolas Pitre63b2ca32014-05-26 18:19:37 -04007850 .total_capacity = 0UL,
Peter Zijlstra147c5fc2013-08-19 15:22:57 +02007851 .busiest_stat = {
7852 .avg_load = 0UL,
Rik van Rielcaeb1782014-07-28 14:16:28 -04007853 .sum_nr_running = 0,
7854 .group_type = group_other,
Syed Rameez Mustafadddcab72016-09-07 16:18:27 -07007855#ifdef CONFIG_SCHED_HMP
7856 .sum_nr_big_tasks = 0UL,
7857 .group_cpu_load = 0ULL,
7858#endif
Peter Zijlstra147c5fc2013-08-19 15:22:57 +02007859 },
7860 };
7861}
7862
Syed Rameez Mustafadddcab72016-09-07 16:18:27 -07007863#ifdef CONFIG_SCHED_HMP
7864
7865static int
7866bail_inter_cluster_balance(struct lb_env *env, struct sd_lb_stats *sds)
7867{
7868 int local_cpu, busiest_cpu;
7869 int local_capacity, busiest_capacity;
7870 int local_pwr_cost, busiest_pwr_cost;
7871 int nr_cpus;
7872
7873 if (!sysctl_sched_restrict_cluster_spill || sched_boost())
7874 return 0;
7875
7876 local_cpu = group_first_cpu(sds->local);
7877 busiest_cpu = group_first_cpu(sds->busiest);
7878
7879 local_capacity = cpu_max_possible_capacity(local_cpu);
7880 busiest_capacity = cpu_max_possible_capacity(busiest_cpu);
7881
7882 local_pwr_cost = cpu_max_power_cost(local_cpu);
7883 busiest_pwr_cost = cpu_max_power_cost(busiest_cpu);
7884
7885 if (local_capacity < busiest_capacity ||
7886 (local_capacity == busiest_capacity &&
7887 local_pwr_cost <= busiest_pwr_cost))
7888 return 0;
7889
7890 if (local_capacity > busiest_capacity &&
7891 sds->busiest_stat.sum_nr_big_tasks)
7892 return 0;
7893
7894 nr_cpus = cpumask_weight(sched_group_cpus(sds->busiest));
7895 if ((sds->busiest_stat.group_cpu_load < nr_cpus * sched_spill_load) &&
7896 (sds->busiest_stat.sum_nr_running <
7897 nr_cpus * sysctl_sched_spill_nr_run))
7898 return 1;
7899
7900 return 0;
7901}
7902
7903#else /* CONFIG_SCHED_HMP */
7904
7905static inline int
7906bail_inter_cluster_balance(struct lb_env *env, struct sd_lb_stats *sds)
7907{
7908 return 0;
7909}
7910
7911#endif /* CONFIG_SCHED_HMP */
7912
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007913/**
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007914 * get_sd_load_idx - Obtain the load index for a given sched domain.
7915 * @sd: The sched_domain whose load_idx is to be obtained.
Kamalesh Babulaled1b7732013-10-13 23:06:15 +05307916 * @idle: The idle status of the CPU for whose sd load_idx is obtained.
Yacine Belkadie69f6182013-07-12 20:45:47 +02007917 *
7918 * Return: The load index.
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007919 */
7920static inline int get_sd_load_idx(struct sched_domain *sd,
7921 enum cpu_idle_type idle)
7922{
7923 int load_idx;
7924
7925 switch (idle) {
7926 case CPU_NOT_IDLE:
7927 load_idx = sd->busy_idx;
7928 break;
7929
7930 case CPU_NEWLY_IDLE:
7931 load_idx = sd->newidle_idx;
7932 break;
7933 default:
7934 load_idx = sd->idle_idx;
7935 break;
7936 }
7937
7938 return load_idx;
7939}
7940
Nicolas Pitreced549f2014-05-26 18:19:38 -04007941static unsigned long scale_rt_capacity(int cpu)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007942{
7943 struct rq *rq = cpu_rq(cpu);
Vincent Guittotb5b48602015-02-27 16:54:08 +01007944 u64 total, used, age_stamp, avg;
Peter Zijlstracadefd32014-02-27 10:40:35 +01007945 s64 delta;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007946
Peter Zijlstrab654f7d2012-05-22 14:04:28 +02007947 /*
7948 * Since we're reading these variables without serialization make sure
7949 * we read them once before doing sanity checks on them.
7950 */
Jason Low316c1608d2015-04-28 13:00:20 -07007951 age_stamp = READ_ONCE(rq->age_stamp);
7952 avg = READ_ONCE(rq->rt_avg);
Peter Zijlstracebde6d2015-01-05 11:18:10 +01007953 delta = __rq_clock_broken(rq) - age_stamp;
Venkatesh Pallipadiaa483802010-10-04 17:03:22 -07007954
Peter Zijlstracadefd32014-02-27 10:40:35 +01007955 if (unlikely(delta < 0))
7956 delta = 0;
7957
7958 total = sched_avg_period() + delta;
Peter Zijlstrab654f7d2012-05-22 14:04:28 +02007959
Vincent Guittotb5b48602015-02-27 16:54:08 +01007960 used = div_u64(avg, total);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007961
Vincent Guittotb5b48602015-02-27 16:54:08 +01007962 if (likely(used < SCHED_CAPACITY_SCALE))
7963 return SCHED_CAPACITY_SCALE - used;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007964
Vincent Guittotb5b48602015-02-27 16:54:08 +01007965 return 1;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007966}
7967
Nicolas Pitreced549f2014-05-26 18:19:38 -04007968static void update_cpu_capacity(struct sched_domain *sd, int cpu)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007969{
Morten Rasmussen8cd56012015-08-14 17:23:10 +01007970 unsigned long capacity = arch_scale_cpu_capacity(sd, cpu);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007971 struct sched_group *sdg = sd->groups;
7972
Vincent Guittotca6d75e2015-02-27 16:54:09 +01007973 cpu_rq(cpu)->cpu_capacity_orig = capacity;
Srivatsa Vaddagiri9d5efe02010-06-08 14:57:02 +10007974
Nicolas Pitreced549f2014-05-26 18:19:38 -04007975 capacity *= scale_rt_capacity(cpu);
Nicolas Pitreca8ce3d2014-05-26 18:19:39 -04007976 capacity >>= SCHED_CAPACITY_SHIFT;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007977
Nicolas Pitreced549f2014-05-26 18:19:38 -04007978 if (!capacity)
7979 capacity = 1;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007980
Nicolas Pitreced549f2014-05-26 18:19:38 -04007981 cpu_rq(cpu)->cpu_capacity = capacity;
7982 sdg->sgc->capacity = capacity;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007983}
7984
Nicolas Pitre63b2ca32014-05-26 18:19:37 -04007985void update_group_capacity(struct sched_domain *sd, int cpu)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007986{
7987 struct sched_domain *child = sd->child;
7988 struct sched_group *group, *sdg = sd->groups;
Vincent Guittotdc7ff762015-03-03 11:35:03 +01007989 unsigned long capacity;
Vincent Guittot4ec44122011-12-12 20:21:08 +01007990 unsigned long interval;
7991
7992 interval = msecs_to_jiffies(sd->balance_interval);
7993 interval = clamp(interval, 1UL, max_load_balance_interval);
Nicolas Pitre63b2ca32014-05-26 18:19:37 -04007994 sdg->sgc->next_update = jiffies + interval;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007995
7996 if (!child) {
Nicolas Pitreced549f2014-05-26 18:19:38 -04007997 update_cpu_capacity(sd, cpu);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007998 return;
7999 }
8000
Vincent Guittotdc7ff762015-03-03 11:35:03 +01008001 capacity = 0;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008002
Peter Zijlstra74a5ce22012-05-23 18:00:43 +02008003 if (child->flags & SD_OVERLAP) {
8004 /*
8005 * SD_OVERLAP domains cannot assume that child groups
8006 * span the current group.
8007 */
8008
Peter Zijlstra863bffc2013-08-28 11:44:39 +02008009 for_each_cpu(cpu, sched_group_cpus(sdg)) {
Nicolas Pitre63b2ca32014-05-26 18:19:37 -04008010 struct sched_group_capacity *sgc;
Srikar Dronamraju9abf24d2013-11-12 22:11:26 +05308011 struct rq *rq = cpu_rq(cpu);
Peter Zijlstra863bffc2013-08-28 11:44:39 +02008012
Srikar Dronamraju9abf24d2013-11-12 22:11:26 +05308013 /*
Nicolas Pitre63b2ca32014-05-26 18:19:37 -04008014 * build_sched_domains() -> init_sched_groups_capacity()
Srikar Dronamraju9abf24d2013-11-12 22:11:26 +05308015 * gets here before we've attached the domains to the
8016 * runqueues.
8017 *
Nicolas Pitreced549f2014-05-26 18:19:38 -04008018 * Use capacity_of(), which is set irrespective of domains
8019 * in update_cpu_capacity().
Srikar Dronamraju9abf24d2013-11-12 22:11:26 +05308020 *
Vincent Guittotdc7ff762015-03-03 11:35:03 +01008021 * This avoids capacity from being 0 and
Srikar Dronamraju9abf24d2013-11-12 22:11:26 +05308022 * causing divide-by-zero issues on boot.
Srikar Dronamraju9abf24d2013-11-12 22:11:26 +05308023 */
8024 if (unlikely(!rq->sd)) {
Nicolas Pitreced549f2014-05-26 18:19:38 -04008025 capacity += capacity_of(cpu);
Srikar Dronamraju9abf24d2013-11-12 22:11:26 +05308026 continue;
8027 }
8028
Nicolas Pitre63b2ca32014-05-26 18:19:37 -04008029 sgc = rq->sd->groups->sgc;
Nicolas Pitre63b2ca32014-05-26 18:19:37 -04008030 capacity += sgc->capacity;
Peter Zijlstra863bffc2013-08-28 11:44:39 +02008031 }
Peter Zijlstra74a5ce22012-05-23 18:00:43 +02008032 } else {
8033 /*
8034 * !SD_OVERLAP domains can assume that child groups
8035 * span the current group.
Byungchul Park97a71422015-07-05 18:33:48 +09008036 */
Peter Zijlstra74a5ce22012-05-23 18:00:43 +02008037
8038 group = child->groups;
8039 do {
Nicolas Pitre63b2ca32014-05-26 18:19:37 -04008040 capacity += group->sgc->capacity;
Peter Zijlstra74a5ce22012-05-23 18:00:43 +02008041 group = group->next;
8042 } while (group != child->groups);
8043 }
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008044
Nicolas Pitre63b2ca32014-05-26 18:19:37 -04008045 sdg->sgc->capacity = capacity;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008046}
8047
Srivatsa Vaddagiri9d5efe02010-06-08 14:57:02 +10008048/*
Vincent Guittotea678212015-02-27 16:54:11 +01008049 * Check whether the capacity of the rq has been noticeably reduced by side
8050 * activity. The imbalance_pct is used for the threshold.
8051 * Return true is the capacity is reduced
Srivatsa Vaddagiri9d5efe02010-06-08 14:57:02 +10008052 */
8053static inline int
Vincent Guittotea678212015-02-27 16:54:11 +01008054check_cpu_capacity(struct rq *rq, struct sched_domain *sd)
Srivatsa Vaddagiri9d5efe02010-06-08 14:57:02 +10008055{
Vincent Guittotea678212015-02-27 16:54:11 +01008056 return ((rq->cpu_capacity * sd->imbalance_pct) <
8057 (rq->cpu_capacity_orig * 100));
Srivatsa Vaddagiri9d5efe02010-06-08 14:57:02 +10008058}
8059
Peter Zijlstra30ce5da2013-08-15 20:29:29 +02008060/*
8061 * Group imbalance indicates (and tries to solve) the problem where balancing
8062 * groups is inadequate due to tsk_cpus_allowed() constraints.
8063 *
8064 * Imagine a situation of two groups of 4 cpus each and 4 tasks each with a
8065 * cpumask covering 1 cpu of the first group and 3 cpus of the second group.
8066 * Something like:
8067 *
8068 * { 0 1 2 3 } { 4 5 6 7 }
8069 * * * * *
8070 *
8071 * If we were to balance group-wise we'd place two tasks in the first group and
8072 * two tasks in the second group. Clearly this is undesired as it will overload
8073 * cpu 3 and leave one of the cpus in the second group unused.
8074 *
8075 * The current solution to this issue is detecting the skew in the first group
Peter Zijlstra62633222013-08-19 12:41:09 +02008076 * by noticing the lower domain failed to reach balance and had difficulty
8077 * moving tasks due to affinity constraints.
Peter Zijlstra30ce5da2013-08-15 20:29:29 +02008078 *
8079 * When this is so detected; this group becomes a candidate for busiest; see
Kamalesh Babulaled1b7732013-10-13 23:06:15 +05308080 * update_sd_pick_busiest(). And calculate_imbalance() and
Peter Zijlstra62633222013-08-19 12:41:09 +02008081 * find_busiest_group() avoid some of the usual balance conditions to allow it
Peter Zijlstra30ce5da2013-08-15 20:29:29 +02008082 * to create an effective group imbalance.
8083 *
8084 * This is a somewhat tricky proposition since the next run might not find the
8085 * group imbalance and decide the groups need to be balanced again. A most
8086 * subtle and fragile situation.
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008087 */
Peter Zijlstra30ce5da2013-08-15 20:29:29 +02008088
Peter Zijlstra62633222013-08-19 12:41:09 +02008089static inline int sg_imbalanced(struct sched_group *group)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008090{
Nicolas Pitre63b2ca32014-05-26 18:19:37 -04008091 return group->sgc->imbalance;
Peter Zijlstra30ce5da2013-08-15 20:29:29 +02008092}
8093
Peter Zijlstrab37d9312013-08-28 11:50:34 +02008094/*
Vincent Guittotea678212015-02-27 16:54:11 +01008095 * group_has_capacity returns true if the group has spare capacity that could
8096 * be used by some tasks.
8097 * We consider that a group has spare capacity if the * number of task is
Dietmar Eggemann9e91d612015-08-14 17:23:12 +01008098 * smaller than the number of CPUs or if the utilization is lower than the
8099 * available capacity for CFS tasks.
Vincent Guittotea678212015-02-27 16:54:11 +01008100 * For the latter, we use a threshold to stabilize the state, to take into
8101 * account the variance of the tasks' load and to return true if the available
8102 * capacity in meaningful for the load balancer.
8103 * As an example, an available capacity of 1% can appear but it doesn't make
8104 * any benefit for the load balance.
Peter Zijlstrab37d9312013-08-28 11:50:34 +02008105 */
Vincent Guittotea678212015-02-27 16:54:11 +01008106static inline bool
8107group_has_capacity(struct lb_env *env, struct sg_lb_stats *sgs)
Peter Zijlstrab37d9312013-08-28 11:50:34 +02008108{
Vincent Guittotea678212015-02-27 16:54:11 +01008109 if (sgs->sum_nr_running < sgs->group_weight)
8110 return true;
Peter Zijlstrab37d9312013-08-28 11:50:34 +02008111
Vincent Guittotea678212015-02-27 16:54:11 +01008112 if ((sgs->group_capacity * 100) >
Dietmar Eggemann9e91d612015-08-14 17:23:12 +01008113 (sgs->group_util * env->sd->imbalance_pct))
Vincent Guittotea678212015-02-27 16:54:11 +01008114 return true;
Peter Zijlstrab37d9312013-08-28 11:50:34 +02008115
Vincent Guittotea678212015-02-27 16:54:11 +01008116 return false;
Peter Zijlstrab37d9312013-08-28 11:50:34 +02008117}
8118
Vincent Guittotea678212015-02-27 16:54:11 +01008119/*
8120 * group_is_overloaded returns true if the group has more tasks than it can
8121 * handle.
8122 * group_is_overloaded is not equals to !group_has_capacity because a group
8123 * with the exact right number of tasks, has no more spare capacity but is not
8124 * overloaded so both group_has_capacity and group_is_overloaded return
8125 * false.
8126 */
8127static inline bool
8128group_is_overloaded(struct lb_env *env, struct sg_lb_stats *sgs)
Rik van Rielcaeb1782014-07-28 14:16:28 -04008129{
Vincent Guittotea678212015-02-27 16:54:11 +01008130 if (sgs->sum_nr_running <= sgs->group_weight)
8131 return false;
8132
8133 if ((sgs->group_capacity * 100) <
Dietmar Eggemann9e91d612015-08-14 17:23:12 +01008134 (sgs->group_util * env->sd->imbalance_pct))
Vincent Guittotea678212015-02-27 16:54:11 +01008135 return true;
8136
8137 return false;
8138}
8139
Leo Yan79a89f92015-09-15 18:56:45 +08008140static inline enum
8141group_type group_classify(struct sched_group *group,
8142 struct sg_lb_stats *sgs)
Vincent Guittotea678212015-02-27 16:54:11 +01008143{
8144 if (sgs->group_no_capacity)
Rik van Rielcaeb1782014-07-28 14:16:28 -04008145 return group_overloaded;
8146
8147 if (sg_imbalanced(group))
8148 return group_imbalanced;
8149
8150 return group_other;
8151}
8152
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008153/**
8154 * update_sg_lb_stats - Update sched_group's statistics for load balancing.
8155 * @env: The load balancing environment.
8156 * @group: sched_group whose statistics are to be updated.
8157 * @load_idx: Load index of sched_domain of this_cpu for load calc.
8158 * @local_group: Does group contain this_cpu.
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008159 * @sgs: variable to hold the statistics for this group.
Masanari Iidacd3bd4e2014-07-28 12:38:06 +09008160 * @overload: Indicate more than one runnable task for any CPU.
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008161 */
8162static inline void update_sg_lb_stats(struct lb_env *env,
8163 struct sched_group *group, int load_idx,
Tim Chen4486edd2014-06-23 12:16:49 -07008164 int local_group, struct sg_lb_stats *sgs,
8165 bool *overload)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008166{
Peter Zijlstra30ce5da2013-08-15 20:29:29 +02008167 unsigned long load;
Waiman Longa426f992015-11-25 14:09:38 -05008168 int i, nr_running;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008169
Peter Zijlstrab72ff132013-08-28 10:32:32 +02008170 memset(sgs, 0, sizeof(*sgs));
8171
Michael Wangb94031302012-07-12 16:10:13 +08008172 for_each_cpu_and(i, sched_group_cpus(group), env->cpus) {
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008173 struct rq *rq = cpu_rq(i);
8174
Syed Rameez Mustafadddcab72016-09-07 16:18:27 -07008175 trace_sched_cpu_load_lb(cpu_rq(i), idle_cpu(i),
8176 sched_irqload(i),
8177 power_cost(i, 0),
8178 cpu_temp(i));
8179
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008180 /* Bias balancing toward cpus of our domain */
Peter Zijlstra62633222013-08-19 12:41:09 +02008181 if (local_group)
Peter Zijlstra04f733b2012-05-11 00:12:02 +02008182 load = target_load(i, load_idx);
Peter Zijlstra62633222013-08-19 12:41:09 +02008183 else
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008184 load = source_load(i, load_idx);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008185
8186 sgs->group_load += load;
Dietmar Eggemann9e91d612015-08-14 17:23:12 +01008187 sgs->group_util += cpu_util(i);
Vincent Guittot65fdac02014-08-26 13:06:46 +02008188 sgs->sum_nr_running += rq->cfs.h_nr_running;
Tim Chen4486edd2014-06-23 12:16:49 -07008189
Waiman Longa426f992015-11-25 14:09:38 -05008190 nr_running = rq->nr_running;
8191 if (nr_running > 1)
Tim Chen4486edd2014-06-23 12:16:49 -07008192 *overload = true;
8193
Syed Rameez Mustafadddcab72016-09-07 16:18:27 -07008194#ifdef CONFIG_SCHED_HMP
8195 sgs->sum_nr_big_tasks += rq->hmp_stats.nr_big_tasks;
8196 sgs->group_cpu_load += cpu_load(i);
8197#endif
8198
Peter Zijlstra0ec8aa02013-10-07 11:29:33 +01008199#ifdef CONFIG_NUMA_BALANCING
8200 sgs->nr_numa_running += rq->nr_numa_running;
8201 sgs->nr_preferred_running += rq->nr_preferred_running;
8202#endif
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008203 sgs->sum_weighted_load += weighted_cpuload(i);
Waiman Longa426f992015-11-25 14:09:38 -05008204 /*
8205 * No need to call idle_cpu() if nr_running is not 0
8206 */
8207 if (!nr_running && idle_cpu(i))
Suresh Siddhaaae6d3d2010-09-17 15:02:32 -07008208 sgs->idle_cpus++;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008209 }
8210
Nicolas Pitre63b2ca32014-05-26 18:19:37 -04008211 /* Adjust by relative CPU capacity of the group */
8212 sgs->group_capacity = group->sgc->capacity;
Nicolas Pitreca8ce3d2014-05-26 18:19:39 -04008213 sgs->avg_load = (sgs->group_load*SCHED_CAPACITY_SCALE) / sgs->group_capacity;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008214
Suresh Siddhadd5feea2010-02-23 16:13:52 -08008215 if (sgs->sum_nr_running)
Peter Zijlstra38d0f772013-08-15 19:47:56 +02008216 sgs->load_per_task = sgs->sum_weighted_load / sgs->sum_nr_running;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008217
Suresh Siddhaaae6d3d2010-09-17 15:02:32 -07008218 sgs->group_weight = group->group_weight;
Peter Zijlstrab37d9312013-08-28 11:50:34 +02008219
Vincent Guittotea678212015-02-27 16:54:11 +01008220 sgs->group_no_capacity = group_is_overloaded(env, sgs);
Leo Yan79a89f92015-09-15 18:56:45 +08008221 sgs->group_type = group_classify(group, sgs);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008222}
8223
Syed Rameez Mustafadddcab72016-09-07 16:18:27 -07008224#ifdef CONFIG_SCHED_HMP
8225static bool update_sd_pick_busiest_active_balance(struct lb_env *env,
8226 struct sd_lb_stats *sds,
8227 struct sched_group *sg,
8228 struct sg_lb_stats *sgs)
8229{
8230 if (env->idle != CPU_NOT_IDLE &&
8231 cpu_capacity(env->dst_cpu) > group_rq_capacity(sg)) {
8232 if (sched_boost() && !sds->busiest && sgs->sum_nr_running) {
8233 env->flags |= LBF_SCHED_BOOST_ACTIVE_BALANCE;
8234 return true;
8235 }
8236
8237 if (sgs->sum_nr_big_tasks >
8238 sds->busiest_stat.sum_nr_big_tasks) {
8239 env->flags |= LBF_BIG_TASK_ACTIVE_BALANCE;
8240 return true;
8241 }
8242 }
8243
8244 return false;
8245}
8246#else
8247static bool update_sd_pick_busiest_active_balance(struct lb_env *env,
8248 struct sd_lb_stats *sds,
8249 struct sched_group *sg,
8250 struct sg_lb_stats *sgs)
8251{
8252 return false;
8253}
8254#endif
8255
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008256/**
Michael Neuling532cb4c2010-06-08 14:57:02 +10008257 * update_sd_pick_busiest - return 1 on busiest group
Randy Dunlapcd968912012-06-08 13:18:33 -07008258 * @env: The load balancing environment.
Michael Neuling532cb4c2010-06-08 14:57:02 +10008259 * @sds: sched_domain statistics
8260 * @sg: sched_group candidate to be checked for being the busiest
Michael Neulingb6b12292010-06-10 12:06:21 +10008261 * @sgs: sched_group statistics
Michael Neuling532cb4c2010-06-08 14:57:02 +10008262 *
8263 * Determine if @sg is a busier group than the previously selected
8264 * busiest group.
Yacine Belkadie69f6182013-07-12 20:45:47 +02008265 *
8266 * Return: %true if @sg is a busier group than the previously selected
8267 * busiest group. %false otherwise.
Michael Neuling532cb4c2010-06-08 14:57:02 +10008268 */
Peter Zijlstrabd939f42012-05-02 14:20:37 +02008269static bool update_sd_pick_busiest(struct lb_env *env,
Michael Neuling532cb4c2010-06-08 14:57:02 +10008270 struct sd_lb_stats *sds,
8271 struct sched_group *sg,
Peter Zijlstrabd939f42012-05-02 14:20:37 +02008272 struct sg_lb_stats *sgs)
Michael Neuling532cb4c2010-06-08 14:57:02 +10008273{
Rik van Rielcaeb1782014-07-28 14:16:28 -04008274 struct sg_lb_stats *busiest = &sds->busiest_stat;
Michael Neuling532cb4c2010-06-08 14:57:02 +10008275
Syed Rameez Mustafadddcab72016-09-07 16:18:27 -07008276 if (update_sd_pick_busiest_active_balance(env, sds, sg, sgs))
8277 return true;
8278
Rik van Rielcaeb1782014-07-28 14:16:28 -04008279 if (sgs->group_type > busiest->group_type)
Michael Neuling532cb4c2010-06-08 14:57:02 +10008280 return true;
8281
Rik van Rielcaeb1782014-07-28 14:16:28 -04008282 if (sgs->group_type < busiest->group_type)
8283 return false;
8284
8285 if (sgs->avg_load <= busiest->avg_load)
8286 return false;
8287
8288 /* This is the busiest node in its class. */
8289 if (!(env->sd->flags & SD_ASYM_PACKING))
Michael Neuling532cb4c2010-06-08 14:57:02 +10008290 return true;
8291
Srikar Dronamraju1f621e02016-04-06 18:47:40 +05308292 /* No ASYM_PACKING if target cpu is already busy */
8293 if (env->idle == CPU_NOT_IDLE)
8294 return true;
Michael Neuling532cb4c2010-06-08 14:57:02 +10008295 /*
8296 * ASYM_PACKING needs to move all the work to the lowest
8297 * numbered CPUs in the group, therefore mark all groups
8298 * higher than ourself as busy.
8299 */
Rik van Rielcaeb1782014-07-28 14:16:28 -04008300 if (sgs->sum_nr_running && env->dst_cpu < group_first_cpu(sg)) {
Michael Neuling532cb4c2010-06-08 14:57:02 +10008301 if (!sds->busiest)
8302 return true;
8303
Srikar Dronamraju1f621e02016-04-06 18:47:40 +05308304 /* Prefer to move from highest possible cpu's work */
8305 if (group_first_cpu(sds->busiest) < group_first_cpu(sg))
Michael Neuling532cb4c2010-06-08 14:57:02 +10008306 return true;
8307 }
8308
8309 return false;
8310}
8311
Peter Zijlstra0ec8aa02013-10-07 11:29:33 +01008312#ifdef CONFIG_NUMA_BALANCING
8313static inline enum fbq_type fbq_classify_group(struct sg_lb_stats *sgs)
8314{
8315 if (sgs->sum_nr_running > sgs->nr_numa_running)
8316 return regular;
8317 if (sgs->sum_nr_running > sgs->nr_preferred_running)
8318 return remote;
8319 return all;
8320}
8321
8322static inline enum fbq_type fbq_classify_rq(struct rq *rq)
8323{
8324 if (rq->nr_running > rq->nr_numa_running)
8325 return regular;
8326 if (rq->nr_running > rq->nr_preferred_running)
8327 return remote;
8328 return all;
8329}
8330#else
8331static inline enum fbq_type fbq_classify_group(struct sg_lb_stats *sgs)
8332{
8333 return all;
8334}
8335
8336static inline enum fbq_type fbq_classify_rq(struct rq *rq)
8337{
8338 return regular;
8339}
8340#endif /* CONFIG_NUMA_BALANCING */
8341
Michael Neuling532cb4c2010-06-08 14:57:02 +10008342/**
Hui Kang461819a2011-10-11 23:00:59 -04008343 * update_sd_lb_stats - Update sched_domain's statistics for load balancing.
Randy Dunlapcd968912012-06-08 13:18:33 -07008344 * @env: The load balancing environment.
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008345 * @sds: variable to hold the statistics for this sched_domain.
8346 */
Peter Zijlstra0ec8aa02013-10-07 11:29:33 +01008347static inline void update_sd_lb_stats(struct lb_env *env, struct sd_lb_stats *sds)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008348{
Peter Zijlstrabd939f42012-05-02 14:20:37 +02008349 struct sched_domain *child = env->sd->child;
8350 struct sched_group *sg = env->sd->groups;
Joonsoo Kim56cf5152013-08-06 17:36:43 +09008351 struct sg_lb_stats tmp_sgs;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008352 int load_idx, prefer_sibling = 0;
Tim Chen4486edd2014-06-23 12:16:49 -07008353 bool overload = false;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008354
8355 if (child && child->flags & SD_PREFER_SIBLING)
8356 prefer_sibling = 1;
8357
Peter Zijlstrabd939f42012-05-02 14:20:37 +02008358 load_idx = get_sd_load_idx(env->sd, env->idle);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008359
8360 do {
Joonsoo Kim56cf5152013-08-06 17:36:43 +09008361 struct sg_lb_stats *sgs = &tmp_sgs;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008362 int local_group;
8363
Peter Zijlstrabd939f42012-05-02 14:20:37 +02008364 local_group = cpumask_test_cpu(env->dst_cpu, sched_group_cpus(sg));
Joonsoo Kim56cf5152013-08-06 17:36:43 +09008365 if (local_group) {
8366 sds->local = sg;
8367 sgs = &sds->local_stat;
Peter Zijlstrab72ff132013-08-28 10:32:32 +02008368
8369 if (env->idle != CPU_NEWLY_IDLE ||
Nicolas Pitre63b2ca32014-05-26 18:19:37 -04008370 time_after_eq(jiffies, sg->sgc->next_update))
8371 update_group_capacity(env->sd, env->dst_cpu);
Joonsoo Kim56cf5152013-08-06 17:36:43 +09008372 }
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008373
Tim Chen4486edd2014-06-23 12:16:49 -07008374 update_sg_lb_stats(env, sg, load_idx, local_group, sgs,
8375 &overload);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008376
Peter Zijlstrab72ff132013-08-28 10:32:32 +02008377 if (local_group)
8378 goto next_group;
8379
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008380 /*
8381 * In case the child domain prefers tasks go to siblings
Vincent Guittotea678212015-02-27 16:54:11 +01008382 * first, lower the sg capacity so that we'll try
Nikhil Rao75dd3212010-10-15 13:12:30 -07008383 * and move all the excess tasks away. We lower the capacity
8384 * of a group only if the local group has the capacity to fit
Vincent Guittotea678212015-02-27 16:54:11 +01008385 * these excess tasks. The extra check prevents the case where
8386 * you always pull from the heaviest group when it is already
8387 * under-utilized (possible with a large weight task outweighs
8388 * the tasks on the system).
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008389 */
Peter Zijlstrab72ff132013-08-28 10:32:32 +02008390 if (prefer_sibling && sds->local &&
Vincent Guittotea678212015-02-27 16:54:11 +01008391 group_has_capacity(env, &sds->local_stat) &&
8392 (sgs->sum_nr_running > 1)) {
8393 sgs->group_no_capacity = 1;
Leo Yan79a89f92015-09-15 18:56:45 +08008394 sgs->group_type = group_classify(sg, sgs);
Wanpeng Licb0b9f22014-11-05 07:44:50 +08008395 }
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008396
Peter Zijlstrab72ff132013-08-28 10:32:32 +02008397 if (update_sd_pick_busiest(env, sds, sg, sgs)) {
Michael Neuling532cb4c2010-06-08 14:57:02 +10008398 sds->busiest = sg;
Joonsoo Kim56cf5152013-08-06 17:36:43 +09008399 sds->busiest_stat = *sgs;
Syed Rameez Mustafadddcab72016-09-07 16:18:27 -07008400 env->busiest_nr_running = sgs->sum_nr_running;
8401 env->busiest_grp_capacity = sgs->group_capacity;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008402 }
8403
Peter Zijlstrab72ff132013-08-28 10:32:32 +02008404next_group:
8405 /* Now, start updating sd_lb_stats */
8406 sds->total_load += sgs->group_load;
Nicolas Pitre63b2ca32014-05-26 18:19:37 -04008407 sds->total_capacity += sgs->group_capacity;
Peter Zijlstrab72ff132013-08-28 10:32:32 +02008408
Michael Neuling532cb4c2010-06-08 14:57:02 +10008409 sg = sg->next;
Peter Zijlstrabd939f42012-05-02 14:20:37 +02008410 } while (sg != env->sd->groups);
Peter Zijlstra0ec8aa02013-10-07 11:29:33 +01008411
8412 if (env->sd->flags & SD_NUMA)
8413 env->fbq_type = fbq_classify_group(&sds->busiest_stat);
Tim Chen4486edd2014-06-23 12:16:49 -07008414
8415 if (!env->sd->parent) {
8416 /* update overload indicator if we are at root domain */
8417 if (env->dst_rq->rd->overload != overload)
8418 env->dst_rq->rd->overload = overload;
8419 }
8420
Michael Neuling532cb4c2010-06-08 14:57:02 +10008421}
8422
Michael Neuling532cb4c2010-06-08 14:57:02 +10008423/**
8424 * check_asym_packing - Check to see if the group is packed into the
8425 * sched doman.
8426 *
8427 * This is primarily intended to used at the sibling level. Some
8428 * cores like POWER7 prefer to use lower numbered SMT threads. In the
8429 * case of POWER7, it can move to lower SMT modes only when higher
8430 * threads are idle. When in lower SMT modes, the threads will
8431 * perform better since they share less core resources. Hence when we
8432 * have idle threads, we want them to be the higher ones.
8433 *
8434 * This packing function is run on idle threads. It checks to see if
8435 * the busiest CPU in this domain (core in the P7 case) has a higher
8436 * CPU number than the packing function is being run on. Here we are
8437 * assuming lower CPU number will be equivalent to lower a SMT thread
8438 * number.
8439 *
Yacine Belkadie69f6182013-07-12 20:45:47 +02008440 * Return: 1 when packing is required and a task should be moved to
Michael Neulingb6b12292010-06-10 12:06:21 +10008441 * this CPU. The amount of the imbalance is returned in *imbalance.
8442 *
Randy Dunlapcd968912012-06-08 13:18:33 -07008443 * @env: The load balancing environment.
Michael Neuling532cb4c2010-06-08 14:57:02 +10008444 * @sds: Statistics of the sched_domain which is to be packed
Michael Neuling532cb4c2010-06-08 14:57:02 +10008445 */
Peter Zijlstrabd939f42012-05-02 14:20:37 +02008446static int check_asym_packing(struct lb_env *env, struct sd_lb_stats *sds)
Michael Neuling532cb4c2010-06-08 14:57:02 +10008447{
8448 int busiest_cpu;
8449
Peter Zijlstrabd939f42012-05-02 14:20:37 +02008450 if (!(env->sd->flags & SD_ASYM_PACKING))
Michael Neuling532cb4c2010-06-08 14:57:02 +10008451 return 0;
8452
Srikar Dronamraju1f621e02016-04-06 18:47:40 +05308453 if (env->idle == CPU_NOT_IDLE)
8454 return 0;
8455
Michael Neuling532cb4c2010-06-08 14:57:02 +10008456 if (!sds->busiest)
8457 return 0;
8458
8459 busiest_cpu = group_first_cpu(sds->busiest);
Peter Zijlstrabd939f42012-05-02 14:20:37 +02008460 if (env->dst_cpu > busiest_cpu)
Michael Neuling532cb4c2010-06-08 14:57:02 +10008461 return 0;
8462
Peter Zijlstrabd939f42012-05-02 14:20:37 +02008463 env->imbalance = DIV_ROUND_CLOSEST(
Nicolas Pitre63b2ca32014-05-26 18:19:37 -04008464 sds->busiest_stat.avg_load * sds->busiest_stat.group_capacity,
Nicolas Pitreca8ce3d2014-05-26 18:19:39 -04008465 SCHED_CAPACITY_SCALE);
Peter Zijlstrabd939f42012-05-02 14:20:37 +02008466
Michael Neuling532cb4c2010-06-08 14:57:02 +10008467 return 1;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008468}
8469
8470/**
8471 * fix_small_imbalance - Calculate the minor imbalance that exists
8472 * amongst the groups of a sched_domain, during
8473 * load balancing.
Randy Dunlapcd968912012-06-08 13:18:33 -07008474 * @env: The load balancing environment.
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008475 * @sds: Statistics of the sched_domain whose imbalance is to be calculated.
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008476 */
Peter Zijlstrabd939f42012-05-02 14:20:37 +02008477static inline
8478void fix_small_imbalance(struct lb_env *env, struct sd_lb_stats *sds)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008479{
Nicolas Pitre63b2ca32014-05-26 18:19:37 -04008480 unsigned long tmp, capa_now = 0, capa_move = 0;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008481 unsigned int imbn = 2;
Suresh Siddhadd5feea2010-02-23 16:13:52 -08008482 unsigned long scaled_busy_load_per_task;
Joonsoo Kim56cf5152013-08-06 17:36:43 +09008483 struct sg_lb_stats *local, *busiest;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008484
Joonsoo Kim56cf5152013-08-06 17:36:43 +09008485 local = &sds->local_stat;
8486 busiest = &sds->busiest_stat;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008487
Joonsoo Kim56cf5152013-08-06 17:36:43 +09008488 if (!local->sum_nr_running)
8489 local->load_per_task = cpu_avg_load_per_task(env->dst_cpu);
8490 else if (busiest->load_per_task > local->load_per_task)
8491 imbn = 1;
Suresh Siddhadd5feea2010-02-23 16:13:52 -08008492
Joonsoo Kim56cf5152013-08-06 17:36:43 +09008493 scaled_busy_load_per_task =
Nicolas Pitreca8ce3d2014-05-26 18:19:39 -04008494 (busiest->load_per_task * SCHED_CAPACITY_SCALE) /
Nicolas Pitre63b2ca32014-05-26 18:19:37 -04008495 busiest->group_capacity;
Joonsoo Kim56cf5152013-08-06 17:36:43 +09008496
Vladimir Davydov3029ede2013-09-15 17:49:14 +04008497 if (busiest->avg_load + scaled_busy_load_per_task >=
8498 local->avg_load + (scaled_busy_load_per_task * imbn)) {
Joonsoo Kim56cf5152013-08-06 17:36:43 +09008499 env->imbalance = busiest->load_per_task;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008500 return;
8501 }
8502
8503 /*
8504 * OK, we don't have enough imbalance to justify moving tasks,
Nicolas Pitreced549f2014-05-26 18:19:38 -04008505 * however we may be able to increase total CPU capacity used by
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008506 * moving them.
8507 */
8508
Nicolas Pitre63b2ca32014-05-26 18:19:37 -04008509 capa_now += busiest->group_capacity *
Joonsoo Kim56cf5152013-08-06 17:36:43 +09008510 min(busiest->load_per_task, busiest->avg_load);
Nicolas Pitre63b2ca32014-05-26 18:19:37 -04008511 capa_now += local->group_capacity *
Joonsoo Kim56cf5152013-08-06 17:36:43 +09008512 min(local->load_per_task, local->avg_load);
Nicolas Pitreca8ce3d2014-05-26 18:19:39 -04008513 capa_now /= SCHED_CAPACITY_SCALE;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008514
8515 /* Amount of load we'd subtract */
Vincent Guittota2cd4262014-03-11 17:26:06 +01008516 if (busiest->avg_load > scaled_busy_load_per_task) {
Nicolas Pitre63b2ca32014-05-26 18:19:37 -04008517 capa_move += busiest->group_capacity *
Joonsoo Kim56cf5152013-08-06 17:36:43 +09008518 min(busiest->load_per_task,
Vincent Guittota2cd4262014-03-11 17:26:06 +01008519 busiest->avg_load - scaled_busy_load_per_task);
Joonsoo Kim56cf5152013-08-06 17:36:43 +09008520 }
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008521
8522 /* Amount of load we'd add */
Nicolas Pitre63b2ca32014-05-26 18:19:37 -04008523 if (busiest->avg_load * busiest->group_capacity <
Nicolas Pitreca8ce3d2014-05-26 18:19:39 -04008524 busiest->load_per_task * SCHED_CAPACITY_SCALE) {
Nicolas Pitre63b2ca32014-05-26 18:19:37 -04008525 tmp = (busiest->avg_load * busiest->group_capacity) /
8526 local->group_capacity;
Joonsoo Kim56cf5152013-08-06 17:36:43 +09008527 } else {
Nicolas Pitreca8ce3d2014-05-26 18:19:39 -04008528 tmp = (busiest->load_per_task * SCHED_CAPACITY_SCALE) /
Nicolas Pitre63b2ca32014-05-26 18:19:37 -04008529 local->group_capacity;
Joonsoo Kim56cf5152013-08-06 17:36:43 +09008530 }
Nicolas Pitre63b2ca32014-05-26 18:19:37 -04008531 capa_move += local->group_capacity *
Peter Zijlstra3ae11c92013-08-15 20:37:48 +02008532 min(local->load_per_task, local->avg_load + tmp);
Nicolas Pitreca8ce3d2014-05-26 18:19:39 -04008533 capa_move /= SCHED_CAPACITY_SCALE;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008534
8535 /* Move if we gain throughput */
Nicolas Pitre63b2ca32014-05-26 18:19:37 -04008536 if (capa_move > capa_now)
Joonsoo Kim56cf5152013-08-06 17:36:43 +09008537 env->imbalance = busiest->load_per_task;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008538}
8539
8540/**
8541 * calculate_imbalance - Calculate the amount of imbalance present within the
8542 * groups of a given sched_domain during load balance.
Peter Zijlstrabd939f42012-05-02 14:20:37 +02008543 * @env: load balance environment
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008544 * @sds: statistics of the sched_domain whose imbalance is to be calculated.
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008545 */
Peter Zijlstrabd939f42012-05-02 14:20:37 +02008546static inline void calculate_imbalance(struct lb_env *env, struct sd_lb_stats *sds)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008547{
Suresh Siddhadd5feea2010-02-23 16:13:52 -08008548 unsigned long max_pull, load_above_capacity = ~0UL;
Joonsoo Kim56cf5152013-08-06 17:36:43 +09008549 struct sg_lb_stats *local, *busiest;
Suresh Siddhadd5feea2010-02-23 16:13:52 -08008550
Joonsoo Kim56cf5152013-08-06 17:36:43 +09008551 local = &sds->local_stat;
Joonsoo Kim56cf5152013-08-06 17:36:43 +09008552 busiest = &sds->busiest_stat;
Joonsoo Kim56cf5152013-08-06 17:36:43 +09008553
Rik van Rielcaeb1782014-07-28 14:16:28 -04008554 if (busiest->group_type == group_imbalanced) {
Peter Zijlstra30ce5da2013-08-15 20:29:29 +02008555 /*
8556 * In the group_imb case we cannot rely on group-wide averages
8557 * to ensure cpu-load equilibrium, look at wider averages. XXX
8558 */
Joonsoo Kim56cf5152013-08-06 17:36:43 +09008559 busiest->load_per_task =
8560 min(busiest->load_per_task, sds->avg_load);
Suresh Siddhadd5feea2010-02-23 16:13:52 -08008561 }
8562
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008563 /*
Dietmar Eggemann885e5422016-04-29 20:32:39 +01008564 * Avg load of busiest sg can be less and avg load of local sg can
8565 * be greater than avg load across all sgs of sd because avg load
8566 * factors in sg capacity and sgs with smaller group_type are
8567 * skipped when updating the busiest sg:
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008568 */
Vladimir Davydovb1885552013-09-15 17:49:13 +04008569 if (busiest->avg_load <= sds->avg_load ||
8570 local->avg_load >= sds->avg_load) {
Peter Zijlstrabd939f42012-05-02 14:20:37 +02008571 env->imbalance = 0;
8572 return fix_small_imbalance(env, sds);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008573 }
8574
Peter Zijlstra9a5d9ba2014-07-29 17:15:11 +02008575 /*
8576 * If there aren't any idle cpus, avoid creating some.
8577 */
8578 if (busiest->group_type == group_overloaded &&
8579 local->group_type == group_overloaded) {
Peter Zijlstra1be0eb22016-05-06 12:21:23 +02008580 load_above_capacity = busiest->sum_nr_running * SCHED_CAPACITY_SCALE;
Morten Rasmussencfa10332016-04-29 20:32:40 +01008581 if (load_above_capacity > busiest->group_capacity) {
Vincent Guittotea678212015-02-27 16:54:11 +01008582 load_above_capacity -= busiest->group_capacity;
Dietmar Eggemann26656212016-08-10 11:27:27 +01008583 load_above_capacity *= scale_load_down(NICE_0_LOAD);
Morten Rasmussencfa10332016-04-29 20:32:40 +01008584 load_above_capacity /= busiest->group_capacity;
8585 } else
Vincent Guittotea678212015-02-27 16:54:11 +01008586 load_above_capacity = ~0UL;
Suresh Siddhadd5feea2010-02-23 16:13:52 -08008587 }
8588
8589 /*
8590 * We're trying to get all the cpus to the average_load, so we don't
8591 * want to push ourselves above the average load, nor do we wish to
8592 * reduce the max loaded cpu below the average load. At the same time,
Dietmar Eggemann0a9b23c2016-04-29 20:32:38 +01008593 * we also don't want to reduce the group load below the group
8594 * capacity. Thus we look for the minimum possible imbalance.
Suresh Siddhadd5feea2010-02-23 16:13:52 -08008595 */
Peter Zijlstra30ce5da2013-08-15 20:29:29 +02008596 max_pull = min(busiest->avg_load - sds->avg_load, load_above_capacity);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008597
8598 /* How much load to actually move to equalise the imbalance */
Joonsoo Kim56cf5152013-08-06 17:36:43 +09008599 env->imbalance = min(
Nicolas Pitre63b2ca32014-05-26 18:19:37 -04008600 max_pull * busiest->group_capacity,
8601 (sds->avg_load - local->avg_load) * local->group_capacity
Nicolas Pitreca8ce3d2014-05-26 18:19:39 -04008602 ) / SCHED_CAPACITY_SCALE;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008603
8604 /*
8605 * if *imbalance is less than the average load per runnable task
Lucas De Marchi25985ed2011-03-30 22:57:33 -03008606 * there is no guarantee that any tasks will be moved so we'll have
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008607 * a think about bumping its value to force at least one task to be
8608 * moved
8609 */
Joonsoo Kim56cf5152013-08-06 17:36:43 +09008610 if (env->imbalance < busiest->load_per_task)
Peter Zijlstrabd939f42012-05-02 14:20:37 +02008611 return fix_small_imbalance(env, sds);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008612}
Nikhil Raofab47622010-10-15 13:12:29 -07008613
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008614/******* find_busiest_group() helpers end here *********************/
8615
8616/**
8617 * find_busiest_group - Returns the busiest group within the sched_domain
Dietmar Eggemann0a9b23c2016-04-29 20:32:38 +01008618 * if there is an imbalance.
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008619 *
8620 * Also calculates the amount of weighted load which should be moved
8621 * to restore balance.
8622 *
Randy Dunlapcd968912012-06-08 13:18:33 -07008623 * @env: The load balancing environment.
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008624 *
Yacine Belkadie69f6182013-07-12 20:45:47 +02008625 * Return: - The busiest group if imbalance exists.
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008626 */
Joonsoo Kim56cf5152013-08-06 17:36:43 +09008627static struct sched_group *find_busiest_group(struct lb_env *env)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008628{
Joonsoo Kim56cf5152013-08-06 17:36:43 +09008629 struct sg_lb_stats *local, *busiest;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008630 struct sd_lb_stats sds;
8631
Peter Zijlstra147c5fc2013-08-19 15:22:57 +02008632 init_sd_lb_stats(&sds);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008633
8634 /*
8635 * Compute the various statistics relavent for load balancing at
8636 * this level.
8637 */
Joonsoo Kim23f0d202013-08-06 17:36:42 +09008638 update_sd_lb_stats(env, &sds);
Joonsoo Kim56cf5152013-08-06 17:36:43 +09008639 local = &sds.local_stat;
8640 busiest = &sds.busiest_stat;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008641
Vincent Guittotea678212015-02-27 16:54:11 +01008642 /* ASYM feature bypasses nice load balance check */
Srikar Dronamraju1f621e02016-04-06 18:47:40 +05308643 if (check_asym_packing(env, &sds))
Michael Neuling532cb4c2010-06-08 14:57:02 +10008644 return sds.busiest;
8645
Peter Zijlstracc57aa82011-02-21 18:55:32 +01008646 /* There is no busy sibling group to pull tasks from */
Joonsoo Kim56cf5152013-08-06 17:36:43 +09008647 if (!sds.busiest || busiest->sum_nr_running == 0)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008648 goto out_balanced;
8649
Syed Rameez Mustafadddcab72016-09-07 16:18:27 -07008650 if (env->flags & LBF_HMP_ACTIVE_BALANCE)
8651 goto force_balance;
8652
8653 if (bail_inter_cluster_balance(env, &sds))
8654 goto out_balanced;
8655
Nicolas Pitreca8ce3d2014-05-26 18:19:39 -04008656 sds.avg_load = (SCHED_CAPACITY_SCALE * sds.total_load)
8657 / sds.total_capacity;
Ken Chenb0432d82011-04-07 17:23:22 -07008658
Peter Zijlstra866ab432011-02-21 18:56:47 +01008659 /*
8660 * If the busiest group is imbalanced the below checks don't
Peter Zijlstra30ce5da2013-08-15 20:29:29 +02008661 * work because they assume all things are equal, which typically
Peter Zijlstra866ab432011-02-21 18:56:47 +01008662 * isn't true due to cpus_allowed constraints and the like.
8663 */
Rik van Rielcaeb1782014-07-28 14:16:28 -04008664 if (busiest->group_type == group_imbalanced)
Peter Zijlstra866ab432011-02-21 18:56:47 +01008665 goto force_balance;
8666
Peter Zijlstracc57aa82011-02-21 18:55:32 +01008667 /* SD_BALANCE_NEWIDLE trumps SMP nice when underutilized */
Vincent Guittotea678212015-02-27 16:54:11 +01008668 if (env->idle == CPU_NEWLY_IDLE && group_has_capacity(env, local) &&
8669 busiest->group_no_capacity)
Nikhil Raofab47622010-10-15 13:12:29 -07008670 goto force_balance;
8671
Peter Zijlstracc57aa82011-02-21 18:55:32 +01008672 /*
Zhihui Zhang9c58c792014-09-20 21:24:36 -04008673 * If the local group is busier than the selected busiest group
Peter Zijlstracc57aa82011-02-21 18:55:32 +01008674 * don't try and pull any tasks.
8675 */
Joonsoo Kim56cf5152013-08-06 17:36:43 +09008676 if (local->avg_load >= busiest->avg_load)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008677 goto out_balanced;
8678
Peter Zijlstracc57aa82011-02-21 18:55:32 +01008679 /*
8680 * Don't pull any tasks if this group is already above the domain
8681 * average load.
8682 */
Joonsoo Kim56cf5152013-08-06 17:36:43 +09008683 if (local->avg_load >= sds.avg_load)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008684 goto out_balanced;
8685
Peter Zijlstrabd939f42012-05-02 14:20:37 +02008686 if (env->idle == CPU_IDLE) {
Suresh Siddhaaae6d3d2010-09-17 15:02:32 -07008687 /*
Vincent Guittot43f4d662014-10-01 15:38:55 +02008688 * This cpu is idle. If the busiest group is not overloaded
8689 * and there is no imbalance between this and busiest group
8690 * wrt idle cpus, it is balanced. The imbalance becomes
8691 * significant if the diff is greater than 1 otherwise we
8692 * might end up to just move the imbalance on another group
Suresh Siddhaaae6d3d2010-09-17 15:02:32 -07008693 */
Vincent Guittot43f4d662014-10-01 15:38:55 +02008694 if ((busiest->group_type != group_overloaded) &&
8695 (local->idle_cpus <= (busiest->idle_cpus + 1)))
Suresh Siddhaaae6d3d2010-09-17 15:02:32 -07008696 goto out_balanced;
Peter Zijlstrac186faf2011-02-21 18:52:53 +01008697 } else {
8698 /*
8699 * In the CPU_NEWLY_IDLE, CPU_NOT_IDLE cases, use
8700 * imbalance_pct to be conservative.
8701 */
Joonsoo Kim56cf5152013-08-06 17:36:43 +09008702 if (100 * busiest->avg_load <=
8703 env->sd->imbalance_pct * local->avg_load)
Peter Zijlstrac186faf2011-02-21 18:52:53 +01008704 goto out_balanced;
Suresh Siddhaaae6d3d2010-09-17 15:02:32 -07008705 }
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008706
Nikhil Raofab47622010-10-15 13:12:29 -07008707force_balance:
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008708 /* Looks like there is an imbalance. Compute it */
Peter Zijlstrabd939f42012-05-02 14:20:37 +02008709 calculate_imbalance(env, &sds);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008710 return sds.busiest;
8711
8712out_balanced:
Peter Zijlstrabd939f42012-05-02 14:20:37 +02008713 env->imbalance = 0;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008714 return NULL;
8715}
8716
Syed Rameez Mustafadddcab72016-09-07 16:18:27 -07008717#ifdef CONFIG_SCHED_HMP
8718static struct rq *find_busiest_queue_hmp(struct lb_env *env,
8719 struct sched_group *group)
8720{
8721 struct rq *busiest = NULL, *busiest_big = NULL;
8722 u64 max_runnable_avg = 0, max_runnable_avg_big = 0;
8723 int max_nr_big = 0, nr_big;
8724 bool find_big = !!(env->flags & LBF_BIG_TASK_ACTIVE_BALANCE);
8725 int i;
8726
8727 for_each_cpu(i, sched_group_cpus(group)) {
8728 struct rq *rq = cpu_rq(i);
8729 u64 cumulative_runnable_avg =
8730 rq->hmp_stats.cumulative_runnable_avg;
8731
8732 if (!cpumask_test_cpu(i, env->cpus))
8733 continue;
8734
8735
8736 if (find_big) {
8737 nr_big = nr_big_tasks(rq);
8738 if (nr_big > max_nr_big ||
8739 (nr_big > 0 && nr_big == max_nr_big &&
8740 cumulative_runnable_avg > max_runnable_avg_big)) {
8741 max_runnable_avg_big = cumulative_runnable_avg;
8742 busiest_big = rq;
8743 max_nr_big = nr_big;
8744 continue;
8745 }
8746 }
8747
8748 if (cumulative_runnable_avg > max_runnable_avg) {
8749 max_runnable_avg = cumulative_runnable_avg;
8750 busiest = rq;
8751 }
8752 }
8753
8754 if (busiest_big)
8755 return busiest_big;
8756
8757 env->flags &= ~LBF_BIG_TASK_ACTIVE_BALANCE;
8758 return busiest;
8759}
8760#else
8761static inline struct rq *find_busiest_queue_hmp(struct lb_env *env,
8762 struct sched_group *group)
8763{
8764 return NULL;
8765}
8766#endif
8767
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008768/*
8769 * find_busiest_queue - find the busiest runqueue among the cpus in group.
8770 */
Peter Zijlstrabd939f42012-05-02 14:20:37 +02008771static struct rq *find_busiest_queue(struct lb_env *env,
Michael Wangb94031302012-07-12 16:10:13 +08008772 struct sched_group *group)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008773{
8774 struct rq *busiest = NULL, *rq;
Nicolas Pitreced549f2014-05-26 18:19:38 -04008775 unsigned long busiest_load = 0, busiest_capacity = 1;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008776 int i;
8777
Syed Rameez Mustafadddcab72016-09-07 16:18:27 -07008778#ifdef CONFIG_SCHED_HMP
8779 return find_busiest_queue_hmp(env, group);
8780#endif
8781
Peter Zijlstra6906a402013-08-19 15:20:21 +02008782 for_each_cpu_and(i, sched_group_cpus(group), env->cpus) {
Vincent Guittotea678212015-02-27 16:54:11 +01008783 unsigned long capacity, wl;
Peter Zijlstra0ec8aa02013-10-07 11:29:33 +01008784 enum fbq_type rt;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008785
Peter Zijlstra0ec8aa02013-10-07 11:29:33 +01008786 rq = cpu_rq(i);
8787 rt = fbq_classify_rq(rq);
8788
8789 /*
8790 * We classify groups/runqueues into three groups:
8791 * - regular: there are !numa tasks
8792 * - remote: there are numa tasks that run on the 'wrong' node
8793 * - all: there is no distinction
8794 *
8795 * In order to avoid migrating ideally placed numa tasks,
8796 * ignore those when there's better options.
8797 *
8798 * If we ignore the actual busiest queue to migrate another
8799 * task, the next balance pass can still reduce the busiest
8800 * queue by moving tasks around inside the node.
8801 *
8802 * If we cannot move enough load due to this classification
8803 * the next pass will adjust the group classification and
8804 * allow migration of more tasks.
8805 *
8806 * Both cases only affect the total convergence complexity.
8807 */
8808 if (rt > env->fbq_type)
8809 continue;
8810
Nicolas Pitreced549f2014-05-26 18:19:38 -04008811 capacity = capacity_of(i);
Srivatsa Vaddagiri9d5efe02010-06-08 14:57:02 +10008812
Thomas Gleixner6e40f5b2010-02-16 16:48:56 +01008813 wl = weighted_cpuload(i);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008814
Thomas Gleixner6e40f5b2010-02-16 16:48:56 +01008815 /*
8816 * When comparing with imbalance, use weighted_cpuload()
Nicolas Pitreced549f2014-05-26 18:19:38 -04008817 * which is not scaled with the cpu capacity.
Thomas Gleixner6e40f5b2010-02-16 16:48:56 +01008818 */
Vincent Guittotea678212015-02-27 16:54:11 +01008819
8820 if (rq->nr_running == 1 && wl > env->imbalance &&
8821 !check_cpu_capacity(rq, env->sd))
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008822 continue;
8823
Thomas Gleixner6e40f5b2010-02-16 16:48:56 +01008824 /*
8825 * For the load comparisons with the other cpu's, consider
Nicolas Pitreced549f2014-05-26 18:19:38 -04008826 * the weighted_cpuload() scaled with the cpu capacity, so
8827 * that the load can be moved away from the cpu that is
8828 * potentially running at a lower capacity.
Joonsoo Kim95a79b82013-08-06 17:36:41 +09008829 *
Nicolas Pitreced549f2014-05-26 18:19:38 -04008830 * Thus we're looking for max(wl_i / capacity_i), crosswise
Joonsoo Kim95a79b82013-08-06 17:36:41 +09008831 * multiplication to rid ourselves of the division works out
Nicolas Pitreced549f2014-05-26 18:19:38 -04008832 * to: wl_i * capacity_j > wl_j * capacity_i; where j is
8833 * our previous maximum.
Thomas Gleixner6e40f5b2010-02-16 16:48:56 +01008834 */
Nicolas Pitreced549f2014-05-26 18:19:38 -04008835 if (wl * busiest_capacity > busiest_load * capacity) {
Joonsoo Kim95a79b82013-08-06 17:36:41 +09008836 busiest_load = wl;
Nicolas Pitreced549f2014-05-26 18:19:38 -04008837 busiest_capacity = capacity;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008838 busiest = rq;
8839 }
8840 }
8841
8842 return busiest;
8843}
8844
8845/*
8846 * Max backoff if we encounter pinned tasks. Pretty arbitrary value, but
8847 * so long as it is large enough.
8848 */
Syed Rameez Mustafadddcab72016-09-07 16:18:27 -07008849#define MAX_PINNED_INTERVAL 16
8850#define NEED_ACTIVE_BALANCE_THRESHOLD 10
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008851
Peter Zijlstrabd939f42012-05-02 14:20:37 +02008852static int need_active_balance(struct lb_env *env)
Peter Zijlstra1af3ed32009-12-23 15:10:31 +01008853{
Peter Zijlstrabd939f42012-05-02 14:20:37 +02008854 struct sched_domain *sd = env->sd;
8855
Syed Rameez Mustafadddcab72016-09-07 16:18:27 -07008856 if (env->flags & LBF_HMP_ACTIVE_BALANCE)
8857 return 1;
8858
Peter Zijlstrabd939f42012-05-02 14:20:37 +02008859 if (env->idle == CPU_NEWLY_IDLE) {
Michael Neuling532cb4c2010-06-08 14:57:02 +10008860
8861 /*
8862 * ASYM_PACKING needs to force migrate tasks from busy but
8863 * higher numbered CPUs in order to pack all tasks in the
8864 * lowest numbered CPUs.
8865 */
Peter Zijlstrabd939f42012-05-02 14:20:37 +02008866 if ((sd->flags & SD_ASYM_PACKING) && env->src_cpu > env->dst_cpu)
Michael Neuling532cb4c2010-06-08 14:57:02 +10008867 return 1;
Peter Zijlstra1af3ed32009-12-23 15:10:31 +01008868 }
8869
Vincent Guittot1aaf90a2015-02-27 16:54:14 +01008870 /*
8871 * The dst_cpu is idle and the src_cpu CPU has only 1 CFS task.
8872 * It's worth migrating the task if the src_cpu's capacity is reduced
8873 * because of other sched_class or IRQs if more capacity stays
8874 * available on dst_cpu.
8875 */
8876 if ((env->idle != CPU_NOT_IDLE) &&
8877 (env->src_rq->cfs.h_nr_running == 1)) {
8878 if ((check_cpu_capacity(env->src_rq, sd)) &&
8879 (capacity_of(env->src_cpu)*sd->imbalance_pct < capacity_of(env->dst_cpu)*100))
8880 return 1;
8881 }
8882
Syed Rameez Mustafadddcab72016-09-07 16:18:27 -07008883 return unlikely(sd->nr_balance_failed >
8884 sd->cache_nice_tries + NEED_ACTIVE_BALANCE_THRESHOLD);
Peter Zijlstra1af3ed32009-12-23 15:10:31 +01008885}
8886
Tejun Heo969c7922010-05-06 18:49:21 +02008887static int active_load_balance_cpu_stop(void *data);
8888
Joonsoo Kim23f0d202013-08-06 17:36:42 +09008889static int should_we_balance(struct lb_env *env)
8890{
8891 struct sched_group *sg = env->sd->groups;
8892 struct cpumask *sg_cpus, *sg_mask;
8893 int cpu, balance_cpu = -1;
8894
8895 /*
8896 * In the newly idle case, we will allow all the cpu's
8897 * to do the newly idle load balance.
8898 */
8899 if (env->idle == CPU_NEWLY_IDLE)
8900 return 1;
8901
8902 sg_cpus = sched_group_cpus(sg);
8903 sg_mask = sched_group_mask(sg);
8904 /* Try to find first idle cpu */
8905 for_each_cpu_and(cpu, sg_cpus, env->cpus) {
8906 if (!cpumask_test_cpu(cpu, sg_mask) || !idle_cpu(cpu))
8907 continue;
8908
8909 balance_cpu = cpu;
8910 break;
8911 }
8912
8913 if (balance_cpu == -1)
8914 balance_cpu = group_balance_cpu(sg);
8915
8916 /*
8917 * First idle cpu or the first cpu(busiest) in this sched group
8918 * is eligible for doing load balancing at this and above domains.
8919 */
Joonsoo Kimb0cff9d2013-09-10 15:54:49 +09008920 return balance_cpu == env->dst_cpu;
Joonsoo Kim23f0d202013-08-06 17:36:42 +09008921}
8922
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008923/*
8924 * Check this_cpu to ensure it is balanced within domain. Attempt to move
8925 * tasks if there is an imbalance.
8926 */
8927static int load_balance(int this_cpu, struct rq *this_rq,
8928 struct sched_domain *sd, enum cpu_idle_type idle,
Joonsoo Kim23f0d202013-08-06 17:36:42 +09008929 int *continue_balancing)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008930{
Syed Rameez Mustafadddcab72016-09-07 16:18:27 -07008931 int ld_moved = 0, cur_ld_moved, active_balance = 0;
Peter Zijlstra62633222013-08-19 12:41:09 +02008932 struct sched_domain *sd_parent = sd->parent;
Syed Rameez Mustafadddcab72016-09-07 16:18:27 -07008933 struct sched_group *group = NULL;
8934 struct rq *busiest = NULL;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008935 unsigned long flags;
Christoph Lameter4ba29682014-08-26 19:12:21 -05008936 struct cpumask *cpus = this_cpu_cpumask_var_ptr(load_balance_mask);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008937
Peter Zijlstra8e45cb52012-02-22 12:47:19 +01008938 struct lb_env env = {
8939 .sd = sd,
Peter Zijlstraddcdf6e2012-02-22 19:27:40 +01008940 .dst_cpu = this_cpu,
8941 .dst_rq = this_rq,
Srivatsa Vaddagiri88b8dac2012-06-19 17:43:15 +05308942 .dst_grpmask = sched_group_cpus(sd->groups),
Peter Zijlstra8e45cb52012-02-22 12:47:19 +01008943 .idle = idle,
Peter Zijlstraeb953082012-04-17 13:38:40 +02008944 .loop_break = sched_nr_migrate_break,
Michael Wangb94031302012-07-12 16:10:13 +08008945 .cpus = cpus,
Peter Zijlstra0ec8aa02013-10-07 11:29:33 +01008946 .fbq_type = all,
Kirill Tkhai163122b2014-08-20 13:48:29 +04008947 .tasks = LIST_HEAD_INIT(env.tasks),
Syed Rameez Mustafadddcab72016-09-07 16:18:27 -07008948 .imbalance = 0,
8949 .flags = 0,
8950 .loop = 0,
8951 .busiest_nr_running = 0,
8952 .busiest_grp_capacity = 0,
Peter Zijlstra8e45cb52012-02-22 12:47:19 +01008953 };
8954
Joonsoo Kimcfc03112013-04-23 17:27:39 +09008955 /*
8956 * For NEWLY_IDLE load_balancing, we don't need to consider
8957 * other cpus in our group
8958 */
Joonsoo Kime02e60c2013-04-23 17:27:42 +09008959 if (idle == CPU_NEWLY_IDLE)
Joonsoo Kimcfc03112013-04-23 17:27:39 +09008960 env.dst_grpmask = NULL;
Joonsoo Kimcfc03112013-04-23 17:27:39 +09008961
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008962 cpumask_copy(cpus, cpu_active_mask);
8963
Josh Poimboeufae928822016-06-17 12:43:24 -05008964 schedstat_inc(sd->lb_count[idle]);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008965
8966redo:
Joonsoo Kim23f0d202013-08-06 17:36:42 +09008967 if (!should_we_balance(&env)) {
8968 *continue_balancing = 0;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008969 goto out_balanced;
Joonsoo Kim23f0d202013-08-06 17:36:42 +09008970 }
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008971
Joonsoo Kim23f0d202013-08-06 17:36:42 +09008972 group = find_busiest_group(&env);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008973 if (!group) {
Josh Poimboeufae928822016-06-17 12:43:24 -05008974 schedstat_inc(sd->lb_nobusyg[idle]);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008975 goto out_balanced;
8976 }
8977
Michael Wangb94031302012-07-12 16:10:13 +08008978 busiest = find_busiest_queue(&env, group);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008979 if (!busiest) {
Josh Poimboeufae928822016-06-17 12:43:24 -05008980 schedstat_inc(sd->lb_nobusyq[idle]);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008981 goto out_balanced;
8982 }
8983
Michael Wang78feefc2012-08-06 16:41:59 +08008984 BUG_ON(busiest == env.dst_rq);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008985
Josh Poimboeufae928822016-06-17 12:43:24 -05008986 schedstat_add(sd->lb_imbalance[idle], env.imbalance);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008987
Vincent Guittot1aaf90a2015-02-27 16:54:14 +01008988 env.src_cpu = busiest->cpu;
8989 env.src_rq = busiest;
8990
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008991 ld_moved = 0;
8992 if (busiest->nr_running > 1) {
8993 /*
8994 * Attempt to move tasks. If find_busiest_group has found
8995 * an imbalance but busiest->nr_running <= 1, the group is
8996 * still unbalanced. ld_moved simply stays zero, so it is
8997 * correctly treated as an imbalance.
8998 */
Peter Zijlstra8e45cb52012-02-22 12:47:19 +01008999 env.flags |= LBF_ALL_PINNED;
Peter Zijlstrac82513e2012-04-26 13:12:27 +02009000 env.loop_max = min(sysctl_sched_nr_migrate, busiest->nr_running);
Peter Zijlstra8e45cb52012-02-22 12:47:19 +01009001
Peter Zijlstra5d6523e2012-03-10 00:07:36 +01009002more_balance:
Kirill Tkhai163122b2014-08-20 13:48:29 +04009003 raw_spin_lock_irqsave(&busiest->lock, flags);
Srivatsa Vaddagiri88b8dac2012-06-19 17:43:15 +05309004
Syed Rameez Mustafadddcab72016-09-07 16:18:27 -07009005 /* The world might have changed. Validate assumptions */
9006 if (busiest->nr_running <= 1) {
9007 raw_spin_unlock_irqrestore(&busiest->lock, flags);
9008 env.flags &= ~LBF_ALL_PINNED;
9009 goto no_move;
9010 }
9011
Srivatsa Vaddagiri88b8dac2012-06-19 17:43:15 +05309012 /*
9013 * cur_ld_moved - load moved in current iteration
9014 * ld_moved - cumulative load moved across iterations
9015 */
Kirill Tkhai163122b2014-08-20 13:48:29 +04009016 cur_ld_moved = detach_tasks(&env);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01009017
9018 /*
Kirill Tkhai163122b2014-08-20 13:48:29 +04009019 * We've detached some tasks from busiest_rq. Every
9020 * task is masked "TASK_ON_RQ_MIGRATING", so we can safely
9021 * unlock busiest->lock, and we are able to be sure
9022 * that nobody can manipulate the tasks in parallel.
9023 * See task_rq_lock() family for the details.
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01009024 */
Kirill Tkhai163122b2014-08-20 13:48:29 +04009025
9026 raw_spin_unlock(&busiest->lock);
9027
9028 if (cur_ld_moved) {
9029 attach_tasks(&env);
9030 ld_moved += cur_ld_moved;
9031 }
9032
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01009033 local_irq_restore(flags);
Srivatsa Vaddagiri88b8dac2012-06-19 17:43:15 +05309034
Joonsoo Kimf1cd0852013-04-23 17:27:37 +09009035 if (env.flags & LBF_NEED_BREAK) {
9036 env.flags &= ~LBF_NEED_BREAK;
9037 goto more_balance;
9038 }
9039
Srivatsa Vaddagiri88b8dac2012-06-19 17:43:15 +05309040 /*
9041 * Revisit (affine) tasks on src_cpu that couldn't be moved to
9042 * us and move them to an alternate dst_cpu in our sched_group
9043 * where they can run. The upper limit on how many times we
9044 * iterate on same src_cpu is dependent on number of cpus in our
9045 * sched_group.
9046 *
9047 * This changes load balance semantics a bit on who can move
9048 * load to a given_cpu. In addition to the given_cpu itself
9049 * (or a ilb_cpu acting on its behalf where given_cpu is
9050 * nohz-idle), we now have balance_cpu in a position to move
9051 * load to given_cpu. In rare situations, this may cause
9052 * conflicts (balance_cpu and given_cpu/ilb_cpu deciding
9053 * _independently_ and at _same_ time to move some load to
9054 * given_cpu) causing exceess load to be moved to given_cpu.
9055 * This however should not happen so much in practice and
9056 * moreover subsequent load balance cycles should correct the
9057 * excess load moved.
9058 */
Peter Zijlstra62633222013-08-19 12:41:09 +02009059 if ((env.flags & LBF_DST_PINNED) && env.imbalance > 0) {
Srivatsa Vaddagiri88b8dac2012-06-19 17:43:15 +05309060
Vladimir Davydov7aff2e32013-09-15 21:30:13 +04009061 /* Prevent to re-select dst_cpu via env's cpus */
9062 cpumask_clear_cpu(env.dst_cpu, env.cpus);
9063
Michael Wang78feefc2012-08-06 16:41:59 +08009064 env.dst_rq = cpu_rq(env.new_dst_cpu);
Srivatsa Vaddagiri88b8dac2012-06-19 17:43:15 +05309065 env.dst_cpu = env.new_dst_cpu;
Peter Zijlstra62633222013-08-19 12:41:09 +02009066 env.flags &= ~LBF_DST_PINNED;
Srivatsa Vaddagiri88b8dac2012-06-19 17:43:15 +05309067 env.loop = 0;
9068 env.loop_break = sched_nr_migrate_break;
Joonsoo Kime02e60c2013-04-23 17:27:42 +09009069
Srivatsa Vaddagiri88b8dac2012-06-19 17:43:15 +05309070 /*
9071 * Go back to "more_balance" rather than "redo" since we
9072 * need to continue with same src_cpu.
9073 */
9074 goto more_balance;
9075 }
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01009076
Peter Zijlstra62633222013-08-19 12:41:09 +02009077 /*
9078 * We failed to reach balance because of affinity.
9079 */
9080 if (sd_parent) {
Nicolas Pitre63b2ca32014-05-26 18:19:37 -04009081 int *group_imbalance = &sd_parent->groups->sgc->imbalance;
Peter Zijlstra62633222013-08-19 12:41:09 +02009082
Vincent Guittotafdeee02014-08-26 13:06:44 +02009083 if ((env.flags & LBF_SOME_PINNED) && env.imbalance > 0)
Peter Zijlstra62633222013-08-19 12:41:09 +02009084 *group_imbalance = 1;
Peter Zijlstra62633222013-08-19 12:41:09 +02009085 }
9086
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01009087 /* All tasks on this runqueue were pinned by CPU affinity */
Peter Zijlstra8e45cb52012-02-22 12:47:19 +01009088 if (unlikely(env.flags & LBF_ALL_PINNED)) {
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01009089 cpumask_clear_cpu(cpu_of(busiest), cpus);
Prashanth Nageshappabbf18b12012-06-19 17:52:07 +05309090 if (!cpumask_empty(cpus)) {
9091 env.loop = 0;
9092 env.loop_break = sched_nr_migrate_break;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01009093 goto redo;
Prashanth Nageshappabbf18b12012-06-19 17:52:07 +05309094 }
Vincent Guittotafdeee02014-08-26 13:06:44 +02009095 goto out_all_pinned;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01009096 }
9097 }
9098
Syed Rameez Mustafadddcab72016-09-07 16:18:27 -07009099no_move:
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01009100 if (!ld_moved) {
Syed Rameez Mustafadddcab72016-09-07 16:18:27 -07009101 if (!(env.flags & LBF_HMP_ACTIVE_BALANCE))
9102 schedstat_inc(sd->lb_failed[idle]);
Venkatesh Pallipadi58b26c42010-09-10 18:19:17 -07009103 /*
9104 * Increment the failure counter only on periodic balance.
9105 * We do not want newidle balance, which can be very
9106 * frequent, pollute the failure counter causing
9107 * excessive cache_hot migrations and active balances.
9108 */
Syed Rameez Mustafadddcab72016-09-07 16:18:27 -07009109 if (idle != CPU_NEWLY_IDLE &&
9110 !(env.flags & LBF_HMP_ACTIVE_BALANCE))
Venkatesh Pallipadi58b26c42010-09-10 18:19:17 -07009111 sd->nr_balance_failed++;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01009112
Peter Zijlstrabd939f42012-05-02 14:20:37 +02009113 if (need_active_balance(&env)) {
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01009114 raw_spin_lock_irqsave(&busiest->lock, flags);
9115
Tejun Heo969c7922010-05-06 18:49:21 +02009116 /* don't kick the active_load_balance_cpu_stop,
9117 * if the curr task on busiest cpu can't be
9118 * moved to this_cpu
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01009119 */
9120 if (!cpumask_test_cpu(this_cpu,
Peter Zijlstrafa17b502011-06-16 12:23:22 +02009121 tsk_cpus_allowed(busiest->curr))) {
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01009122 raw_spin_unlock_irqrestore(&busiest->lock,
9123 flags);
Peter Zijlstra8e45cb52012-02-22 12:47:19 +01009124 env.flags |= LBF_ALL_PINNED;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01009125 goto out_one_pinned;
9126 }
9127
Tejun Heo969c7922010-05-06 18:49:21 +02009128 /*
9129 * ->active_balance synchronizes accesses to
9130 * ->active_balance_work. Once set, it's cleared
9131 * only after active load balance is finished.
9132 */
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01009133 if (!busiest->active_balance) {
9134 busiest->active_balance = 1;
9135 busiest->push_cpu = this_cpu;
9136 active_balance = 1;
9137 }
9138 raw_spin_unlock_irqrestore(&busiest->lock, flags);
Tejun Heo969c7922010-05-06 18:49:21 +02009139
Peter Zijlstrabd939f42012-05-02 14:20:37 +02009140 if (active_balance) {
Tejun Heo969c7922010-05-06 18:49:21 +02009141 stop_one_cpu_nowait(cpu_of(busiest),
9142 active_load_balance_cpu_stop, busiest,
9143 &busiest->active_balance_work);
Syed Rameez Mustafadddcab72016-09-07 16:18:27 -07009144 *continue_balancing = 0;
Peter Zijlstrabd939f42012-05-02 14:20:37 +02009145 }
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01009146
Srikar Dronamrajud02c0712016-03-23 17:54:44 +05309147 /* We've kicked active balancing, force task migration. */
Syed Rameez Mustafadddcab72016-09-07 16:18:27 -07009148 sd->nr_balance_failed = sd->cache_nice_tries +
9149 NEED_ACTIVE_BALANCE_THRESHOLD - 1;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01009150 }
Syed Rameez Mustafadddcab72016-09-07 16:18:27 -07009151 } else {
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01009152 sd->nr_balance_failed = 0;
9153
Syed Rameez Mustafadddcab72016-09-07 16:18:27 -07009154 /* Assumes one 'busiest' cpu that we pulled tasks from */
9155 if (!same_freq_domain(this_cpu, cpu_of(busiest))) {
9156 int check_groups = !!(env.flags &
9157 LBF_MOVED_RELATED_THREAD_GROUP_TASK);
9158
9159 check_for_freq_change(this_rq, false, check_groups);
9160 check_for_freq_change(busiest, false, check_groups);
9161 } else {
9162 check_for_freq_change(this_rq, true, false);
9163 }
9164 }
9165
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01009166 if (likely(!active_balance)) {
9167 /* We were unbalanced, so reset the balancing interval */
9168 sd->balance_interval = sd->min_interval;
9169 } else {
9170 /*
9171 * If we've begun active balancing, start to back off. This
9172 * case may not be covered by the all_pinned logic if there
9173 * is only 1 task on the busy runqueue (because we don't call
Kirill Tkhai163122b2014-08-20 13:48:29 +04009174 * detach_tasks).
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01009175 */
9176 if (sd->balance_interval < sd->max_interval)
9177 sd->balance_interval *= 2;
9178 }
9179
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01009180 goto out;
9181
9182out_balanced:
Vincent Guittotafdeee02014-08-26 13:06:44 +02009183 /*
9184 * We reach balance although we may have faced some affinity
9185 * constraints. Clear the imbalance flag if it was set.
9186 */
9187 if (sd_parent) {
9188 int *group_imbalance = &sd_parent->groups->sgc->imbalance;
9189
9190 if (*group_imbalance)
9191 *group_imbalance = 0;
9192 }
9193
9194out_all_pinned:
9195 /*
9196 * We reach balance because all tasks are pinned at this level so
9197 * we can't migrate them. Let the imbalance flag set so parent level
9198 * can try to migrate them.
9199 */
Josh Poimboeufae928822016-06-17 12:43:24 -05009200 schedstat_inc(sd->lb_balanced[idle]);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01009201
9202 sd->nr_balance_failed = 0;
9203
9204out_one_pinned:
9205 /* tune up the balancing interval */
Peter Zijlstra8e45cb52012-02-22 12:47:19 +01009206 if (((env.flags & LBF_ALL_PINNED) &&
Peter Zijlstra5b54b562011-09-22 15:23:13 +02009207 sd->balance_interval < MAX_PINNED_INTERVAL) ||
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01009208 (sd->balance_interval < sd->max_interval))
9209 sd->balance_interval *= 2;
9210
Venkatesh Pallipadi46e49b32011-02-14 14:38:50 -08009211 ld_moved = 0;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01009212out:
Syed Rameez Mustafadddcab72016-09-07 16:18:27 -07009213 trace_sched_load_balance(this_cpu, idle, *continue_balancing,
9214 group ? group->cpumask[0] : 0,
9215 busiest ? busiest->nr_running : 0,
9216 env.imbalance, env.flags, ld_moved,
9217 sd->balance_interval);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01009218 return ld_moved;
9219}
9220
Jason Low52a08ef2014-05-08 17:49:22 -07009221static inline unsigned long
9222get_sd_balance_interval(struct sched_domain *sd, int cpu_busy)
9223{
9224 unsigned long interval = sd->balance_interval;
9225
9226 if (cpu_busy)
9227 interval *= sd->busy_factor;
9228
9229 /* scale ms to jiffies */
9230 interval = msecs_to_jiffies(interval);
9231 interval = clamp(interval, 1UL, max_load_balance_interval);
9232
9233 return interval;
9234}
9235
9236static inline void
Leo Yan31851a92016-08-05 14:31:29 +08009237update_next_balance(struct sched_domain *sd, unsigned long *next_balance)
Jason Low52a08ef2014-05-08 17:49:22 -07009238{
9239 unsigned long interval, next;
9240
Leo Yan31851a92016-08-05 14:31:29 +08009241 /* used by idle balance, so cpu_busy = 0 */
9242 interval = get_sd_balance_interval(sd, 0);
Jason Low52a08ef2014-05-08 17:49:22 -07009243 next = sd->last_balance + interval;
9244
9245 if (time_after(*next_balance, next))
9246 *next_balance = next;
9247}
9248
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01009249/*
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01009250 * idle_balance is called by schedule() if this_cpu is about to become
9251 * idle. Attempts to pull tasks from other CPUs.
9252 */
Peter Zijlstra6e831252014-02-11 16:11:48 +01009253static int idle_balance(struct rq *this_rq)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01009254{
Jason Low52a08ef2014-05-08 17:49:22 -07009255 unsigned long next_balance = jiffies + HZ;
9256 int this_cpu = this_rq->cpu;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01009257 struct sched_domain *sd;
9258 int pulled_task = 0;
Jason Low9bd721c2013-09-13 11:26:52 -07009259 u64 curr_cost = 0;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01009260
Peter Zijlstra6e831252014-02-11 16:11:48 +01009261 /*
9262 * We must set idle_stamp _before_ calling idle_balance(), such that we
9263 * measure the duration of idle_balance() as idle time.
9264 */
9265 this_rq->idle_stamp = rq_clock(this_rq);
9266
Tim Chen4486edd2014-06-23 12:16:49 -07009267 if (this_rq->avg_idle < sysctl_sched_migration_cost ||
9268 !this_rq->rd->overload) {
Jason Low52a08ef2014-05-08 17:49:22 -07009269 rcu_read_lock();
9270 sd = rcu_dereference_check_sched_domain(this_rq->sd);
9271 if (sd)
Leo Yan31851a92016-08-05 14:31:29 +08009272 update_next_balance(sd, &next_balance);
Jason Low52a08ef2014-05-08 17:49:22 -07009273 rcu_read_unlock();
9274
Peter Zijlstra6e831252014-02-11 16:11:48 +01009275 goto out;
Jason Low52a08ef2014-05-08 17:49:22 -07009276 }
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01009277
Peter Zijlstraf492e122009-12-23 15:29:42 +01009278 raw_spin_unlock(&this_rq->lock);
9279
Paul Turner48a16752012-10-04 13:18:31 +02009280 update_blocked_averages(this_cpu);
Peter Zijlstradce840a2011-04-07 14:09:50 +02009281 rcu_read_lock();
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01009282 for_each_domain(this_cpu, sd) {
Joonsoo Kim23f0d202013-08-06 17:36:42 +09009283 int continue_balancing = 1;
Jason Low9bd721c2013-09-13 11:26:52 -07009284 u64 t0, domain_cost;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01009285
9286 if (!(sd->flags & SD_LOAD_BALANCE))
9287 continue;
9288
Jason Low52a08ef2014-05-08 17:49:22 -07009289 if (this_rq->avg_idle < curr_cost + sd->max_newidle_lb_cost) {
Leo Yan31851a92016-08-05 14:31:29 +08009290 update_next_balance(sd, &next_balance);
Jason Low9bd721c2013-09-13 11:26:52 -07009291 break;
Jason Low52a08ef2014-05-08 17:49:22 -07009292 }
Jason Low9bd721c2013-09-13 11:26:52 -07009293
Peter Zijlstraf492e122009-12-23 15:29:42 +01009294 if (sd->flags & SD_BALANCE_NEWIDLE) {
Jason Low9bd721c2013-09-13 11:26:52 -07009295 t0 = sched_clock_cpu(this_cpu);
9296
Peter Zijlstraf492e122009-12-23 15:29:42 +01009297 pulled_task = load_balance(this_cpu, this_rq,
Joonsoo Kim23f0d202013-08-06 17:36:42 +09009298 sd, CPU_NEWLY_IDLE,
9299 &continue_balancing);
Jason Low9bd721c2013-09-13 11:26:52 -07009300
9301 domain_cost = sched_clock_cpu(this_cpu) - t0;
9302 if (domain_cost > sd->max_newidle_lb_cost)
9303 sd->max_newidle_lb_cost = domain_cost;
9304
9305 curr_cost += domain_cost;
Peter Zijlstraf492e122009-12-23 15:29:42 +01009306 }
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01009307
Leo Yan31851a92016-08-05 14:31:29 +08009308 update_next_balance(sd, &next_balance);
Jason Low39a4d9c2014-04-23 18:30:35 -07009309
9310 /*
9311 * Stop searching for tasks to pull if there are
Syed Rameez Mustafadddcab72016-09-07 16:18:27 -07009312 * now runnable tasks on the balance rq or if
9313 * continue_balancing has been unset (only possible
9314 * due to active migration).
Jason Low39a4d9c2014-04-23 18:30:35 -07009315 */
Syed Rameez Mustafadddcab72016-09-07 16:18:27 -07009316 if (pulled_task || this_rq->nr_running > 0 ||
9317 !continue_balancing)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01009318 break;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01009319 }
Peter Zijlstradce840a2011-04-07 14:09:50 +02009320 rcu_read_unlock();
Peter Zijlstraf492e122009-12-23 15:29:42 +01009321
9322 raw_spin_lock(&this_rq->lock);
9323
Jason Low0e5b5332014-04-28 15:45:54 -07009324 if (curr_cost > this_rq->max_idle_balance_cost)
9325 this_rq->max_idle_balance_cost = curr_cost;
9326
Daniel Lezcanoe5fc6612014-01-17 10:04:02 +01009327 /*
Jason Low0e5b5332014-04-28 15:45:54 -07009328 * While browsing the domains, we released the rq lock, a task could
9329 * have been enqueued in the meantime. Since we're not going idle,
9330 * pretend we pulled a task.
Daniel Lezcanoe5fc6612014-01-17 10:04:02 +01009331 */
Jason Low0e5b5332014-04-28 15:45:54 -07009332 if (this_rq->cfs.h_nr_running && !pulled_task)
Peter Zijlstra6e831252014-02-11 16:11:48 +01009333 pulled_task = 1;
Daniel Lezcanoe5fc6612014-01-17 10:04:02 +01009334
Peter Zijlstra6e831252014-02-11 16:11:48 +01009335out:
Jason Low52a08ef2014-05-08 17:49:22 -07009336 /* Move the next balance forward */
9337 if (time_after(this_rq->next_balance, next_balance))
9338 this_rq->next_balance = next_balance;
9339
Kirill Tkhaie4aa3582014-03-06 13:31:55 +04009340 /* Is there a task of a high priority class? */
Kirill Tkhai46383642014-03-15 02:15:07 +04009341 if (this_rq->nr_running != this_rq->cfs.h_nr_running)
Kirill Tkhaie4aa3582014-03-06 13:31:55 +04009342 pulled_task = -1;
9343
Dietmar Eggemann38c6ade2015-10-20 13:04:41 +01009344 if (pulled_task)
Peter Zijlstra6e831252014-02-11 16:11:48 +01009345 this_rq->idle_stamp = 0;
9346
Daniel Lezcano3c4017c2014-01-17 10:04:03 +01009347 return pulled_task;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01009348}
9349
9350/*
Tejun Heo969c7922010-05-06 18:49:21 +02009351 * active_load_balance_cpu_stop is run by cpu stopper. It pushes
9352 * running tasks off the busiest CPU onto idle CPUs. It requires at
9353 * least 1 task to be running on each physical CPU where possible, and
9354 * avoids physical / logical imbalances.
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01009355 */
Tejun Heo969c7922010-05-06 18:49:21 +02009356static int active_load_balance_cpu_stop(void *data)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01009357{
Tejun Heo969c7922010-05-06 18:49:21 +02009358 struct rq *busiest_rq = data;
9359 int busiest_cpu = cpu_of(busiest_rq);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01009360 int target_cpu = busiest_rq->push_cpu;
Tejun Heo969c7922010-05-06 18:49:21 +02009361 struct rq *target_rq = cpu_rq(target_cpu);
Syed Rameez Mustafadddcab72016-09-07 16:18:27 -07009362 struct sched_domain *sd = NULL;
Kirill Tkhaie5673f22014-08-20 13:48:01 +04009363 struct task_struct *p = NULL;
Syed Rameez Mustafadddcab72016-09-07 16:18:27 -07009364 struct task_struct *push_task;
9365 int push_task_detached = 0;
9366 struct lb_env env = {
9367 .sd = sd,
9368 .dst_cpu = target_cpu,
9369 .dst_rq = target_rq,
9370 .src_cpu = busiest_rq->cpu,
9371 .src_rq = busiest_rq,
9372 .idle = CPU_IDLE,
9373 .busiest_nr_running = 0,
9374 .busiest_grp_capacity = 0,
9375 .flags = 0,
9376 .loop = 0,
9377 };
9378 bool moved = false;
Tejun Heo969c7922010-05-06 18:49:21 +02009379
9380 raw_spin_lock_irq(&busiest_rq->lock);
9381
9382 /* make sure the requested cpu hasn't gone down in the meantime */
9383 if (unlikely(busiest_cpu != smp_processor_id() ||
9384 !busiest_rq->active_balance))
9385 goto out_unlock;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01009386
9387 /* Is there any task to move? */
9388 if (busiest_rq->nr_running <= 1)
Tejun Heo969c7922010-05-06 18:49:21 +02009389 goto out_unlock;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01009390
9391 /*
9392 * This condition is "impossible", if it occurs
9393 * we need to fix it. Originally reported by
9394 * Bjorn Helgaas on a 128-cpu setup.
9395 */
9396 BUG_ON(busiest_rq == target_rq);
9397
Syed Rameez Mustafadddcab72016-09-07 16:18:27 -07009398 push_task = busiest_rq->push_task;
9399 target_cpu = busiest_rq->push_cpu;
9400 if (push_task) {
9401 if (task_on_rq_queued(push_task) &&
9402 push_task->state == TASK_RUNNING &&
9403 task_cpu(push_task) == busiest_cpu &&
9404 cpu_online(target_cpu)) {
9405 detach_task(push_task, &env);
9406 push_task_detached = 1;
9407 moved = true;
9408 }
9409 goto out_unlock;
9410 }
9411
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01009412 /* Search for an sd spanning us and the target CPU. */
Peter Zijlstradce840a2011-04-07 14:09:50 +02009413 rcu_read_lock();
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01009414 for_each_domain(target_cpu, sd) {
9415 if ((sd->flags & SD_LOAD_BALANCE) &&
9416 cpumask_test_cpu(busiest_cpu, sched_domain_span(sd)))
9417 break;
9418 }
9419
9420 if (likely(sd)) {
Syed Rameez Mustafadddcab72016-09-07 16:18:27 -07009421 env.sd = sd;
Josh Poimboeufae928822016-06-17 12:43:24 -05009422 schedstat_inc(sd->alb_count);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01009423
Kirill Tkhaie5673f22014-08-20 13:48:01 +04009424 p = detach_one_task(&env);
Srikar Dronamrajud02c0712016-03-23 17:54:44 +05309425 if (p) {
Josh Poimboeufae928822016-06-17 12:43:24 -05009426 schedstat_inc(sd->alb_pushed);
Srikar Dronamrajud02c0712016-03-23 17:54:44 +05309427 /* Active balancing done, reset the failure counter. */
9428 sd->nr_balance_failed = 0;
Syed Rameez Mustafadddcab72016-09-07 16:18:27 -07009429 moved = true;
Srikar Dronamrajud02c0712016-03-23 17:54:44 +05309430 } else {
Josh Poimboeufae928822016-06-17 12:43:24 -05009431 schedstat_inc(sd->alb_failed);
Srikar Dronamrajud02c0712016-03-23 17:54:44 +05309432 }
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01009433 }
Peter Zijlstradce840a2011-04-07 14:09:50 +02009434 rcu_read_unlock();
Tejun Heo969c7922010-05-06 18:49:21 +02009435out_unlock:
9436 busiest_rq->active_balance = 0;
Syed Rameez Mustafadddcab72016-09-07 16:18:27 -07009437 push_task = busiest_rq->push_task;
9438 target_cpu = busiest_rq->push_cpu;
9439
9440 if (push_task)
9441 busiest_rq->push_task = NULL;
9442
Kirill Tkhaie5673f22014-08-20 13:48:01 +04009443 raw_spin_unlock(&busiest_rq->lock);
9444
9445 if (p)
9446 attach_one_task(target_rq, p);
9447
9448 local_irq_enable();
9449
Syed Rameez Mustafadddcab72016-09-07 16:18:27 -07009450 if (moved && !same_freq_domain(busiest_cpu, target_cpu)) {
9451 int check_groups = !!(env.flags &
9452 LBF_MOVED_RELATED_THREAD_GROUP_TASK);
9453 check_for_freq_change(busiest_rq, false, check_groups);
9454 check_for_freq_change(target_rq, false, check_groups);
9455 } else if (moved) {
9456 check_for_freq_change(target_rq, true, false);
9457 }
9458
Tejun Heo969c7922010-05-06 18:49:21 +02009459 return 0;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01009460}
9461
Mike Galbraithd987fc72011-12-05 10:01:47 +01009462static inline int on_null_domain(struct rq *rq)
9463{
9464 return unlikely(!rcu_dereference_sched(rq->sd));
9465}
9466
Frederic Weisbecker3451d022011-08-10 23:21:01 +02009467#ifdef CONFIG_NO_HZ_COMMON
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07009468/*
9469 * idle load balancing details
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07009470 * - When one of the busy CPUs notice that there may be an idle rebalancing
9471 * needed, they will kick the idle load balancer, which then does idle
9472 * load balancing for all the idle CPUs.
9473 */
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01009474static struct {
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07009475 cpumask_var_t idle_cpus_mask;
Suresh Siddha0b005cf2011-12-01 17:07:34 -08009476 atomic_t nr_cpus;
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07009477 unsigned long next_balance; /* in jiffy units */
9478} nohz ____cacheline_aligned;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01009479
Syed Rameez Mustafadddcab72016-09-07 16:18:27 -07009480#ifdef CONFIG_SCHED_HMP
9481static inline int find_new_hmp_ilb(int type)
9482{
9483 int call_cpu = raw_smp_processor_id();
9484 struct sched_domain *sd;
9485 int ilb;
9486
9487 rcu_read_lock();
9488
9489 /* Pick an idle cpu "closest" to call_cpu */
9490 for_each_domain(call_cpu, sd) {
9491 for_each_cpu_and(ilb, nohz.idle_cpus_mask,
9492 sched_domain_span(sd)) {
9493 if (idle_cpu(ilb) && (type != NOHZ_KICK_RESTRICT ||
9494 (hmp_capable() &&
9495 cpu_max_possible_capacity(ilb) <=
9496 cpu_max_possible_capacity(call_cpu)) ||
9497 cpu_max_power_cost(ilb) <=
9498 cpu_max_power_cost(call_cpu))) {
9499 rcu_read_unlock();
9500 reset_balance_interval(ilb);
9501 return ilb;
9502 }
9503 }
9504 }
9505
9506 rcu_read_unlock();
9507 return nr_cpu_ids;
9508}
9509#endif /* CONFIG_SCHED_HMP */
9510
9511static inline int find_new_ilb(int type)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01009512{
Suresh Siddha0b005cf2011-12-01 17:07:34 -08009513 int ilb = cpumask_first(nohz.idle_cpus_mask);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01009514
Syed Rameez Mustafadddcab72016-09-07 16:18:27 -07009515#ifdef CONFIG_SCHED_HMP
9516 return find_new_hmp_ilb(type);
9517#endif
9518
Suresh Siddha786d6dc72011-12-01 17:07:35 -08009519 if (ilb < nr_cpu_ids && idle_cpu(ilb))
9520 return ilb;
9521
9522 return nr_cpu_ids;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01009523}
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01009524
9525/*
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07009526 * Kick a CPU to do the nohz balancing, if it is time for it. We pick the
9527 * nohz_load_balancer CPU (if there is one) otherwise fallback to any idle
9528 * CPU (if there is one).
9529 */
Syed Rameez Mustafadddcab72016-09-07 16:18:27 -07009530static void nohz_balancer_kick(int type)
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07009531{
9532 int ilb_cpu;
9533
9534 nohz.next_balance++;
9535
Syed Rameez Mustafadddcab72016-09-07 16:18:27 -07009536 ilb_cpu = find_new_ilb(type);
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07009537
Suresh Siddha0b005cf2011-12-01 17:07:34 -08009538 if (ilb_cpu >= nr_cpu_ids)
9539 return;
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07009540
Suresh Siddhacd490c52011-12-06 11:26:34 -08009541 if (test_and_set_bit(NOHZ_BALANCE_KICK, nohz_flags(ilb_cpu)))
Suresh Siddha1c792db2011-12-01 17:07:32 -08009542 return;
9543 /*
9544 * Use smp_send_reschedule() instead of resched_cpu().
9545 * This way we generate a sched IPI on the target cpu which
9546 * is idle. And the softirq performing nohz idle load balance
9547 * will be run before returning from the IPI.
9548 */
9549 smp_send_reschedule(ilb_cpu);
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07009550 return;
9551}
9552
Thomas Gleixner20a5c8c2016-03-10 12:54:20 +01009553void nohz_balance_exit_idle(unsigned int cpu)
Suresh Siddha71325962012-01-19 18:28:57 -08009554{
9555 if (unlikely(test_bit(NOHZ_TICK_STOPPED, nohz_flags(cpu)))) {
Mike Galbraithd987fc72011-12-05 10:01:47 +01009556 /*
9557 * Completely isolated CPUs don't ever set, so we must test.
9558 */
9559 if (likely(cpumask_test_cpu(cpu, nohz.idle_cpus_mask))) {
9560 cpumask_clear_cpu(cpu, nohz.idle_cpus_mask);
9561 atomic_dec(&nohz.nr_cpus);
9562 }
Suresh Siddha71325962012-01-19 18:28:57 -08009563 clear_bit(NOHZ_TICK_STOPPED, nohz_flags(cpu));
9564 }
9565}
9566
Suresh Siddha69e1e812011-12-01 17:07:33 -08009567static inline void set_cpu_sd_state_busy(void)
9568{
9569 struct sched_domain *sd;
Preeti U Murthy37dc6b52013-10-30 08:42:52 +05309570 int cpu = smp_processor_id();
Suresh Siddha69e1e812011-12-01 17:07:33 -08009571
Suresh Siddha69e1e812011-12-01 17:07:33 -08009572 rcu_read_lock();
Peter Zijlstra0e369d72016-05-09 10:38:01 +02009573 sd = rcu_dereference(per_cpu(sd_llc, cpu));
Vincent Guittot25f55d92013-04-23 16:59:02 +02009574
9575 if (!sd || !sd->nohz_idle)
9576 goto unlock;
9577 sd->nohz_idle = 0;
9578
Peter Zijlstra0e369d72016-05-09 10:38:01 +02009579 atomic_inc(&sd->shared->nr_busy_cpus);
Vincent Guittot25f55d92013-04-23 16:59:02 +02009580unlock:
Suresh Siddha69e1e812011-12-01 17:07:33 -08009581 rcu_read_unlock();
9582}
9583
9584void set_cpu_sd_state_idle(void)
9585{
9586 struct sched_domain *sd;
Preeti U Murthy37dc6b52013-10-30 08:42:52 +05309587 int cpu = smp_processor_id();
Suresh Siddha69e1e812011-12-01 17:07:33 -08009588
Suresh Siddha69e1e812011-12-01 17:07:33 -08009589 rcu_read_lock();
Peter Zijlstra0e369d72016-05-09 10:38:01 +02009590 sd = rcu_dereference(per_cpu(sd_llc, cpu));
Vincent Guittot25f55d92013-04-23 16:59:02 +02009591
9592 if (!sd || sd->nohz_idle)
9593 goto unlock;
9594 sd->nohz_idle = 1;
9595
Peter Zijlstra0e369d72016-05-09 10:38:01 +02009596 atomic_dec(&sd->shared->nr_busy_cpus);
Vincent Guittot25f55d92013-04-23 16:59:02 +02009597unlock:
Suresh Siddha69e1e812011-12-01 17:07:33 -08009598 rcu_read_unlock();
9599}
9600
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07009601/*
Alex Shic1cc0172012-09-10 15:10:58 +08009602 * This routine will record that the cpu is going idle with tick stopped.
Suresh Siddha0b005cf2011-12-01 17:07:34 -08009603 * This info will be used in performing idle load balancing in the future.
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01009604 */
Alex Shic1cc0172012-09-10 15:10:58 +08009605void nohz_balance_enter_idle(int cpu)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01009606{
Suresh Siddha71325962012-01-19 18:28:57 -08009607 /*
9608 * If this cpu is going down, then nothing needs to be done.
9609 */
9610 if (!cpu_active(cpu))
9611 return;
9612
Alex Shic1cc0172012-09-10 15:10:58 +08009613 if (test_bit(NOHZ_TICK_STOPPED, nohz_flags(cpu)))
9614 return;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01009615
Mike Galbraithd987fc72011-12-05 10:01:47 +01009616 /*
9617 * If we're a completely isolated CPU, we don't play.
9618 */
9619 if (on_null_domain(cpu_rq(cpu)))
9620 return;
9621
Alex Shic1cc0172012-09-10 15:10:58 +08009622 cpumask_set_cpu(cpu, nohz.idle_cpus_mask);
9623 atomic_inc(&nohz.nr_cpus);
9624 set_bit(NOHZ_TICK_STOPPED, nohz_flags(cpu));
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01009625}
9626#endif
9627
9628static DEFINE_SPINLOCK(balancing);
9629
Peter Zijlstra49c022e2011-04-05 10:14:25 +02009630/*
9631 * Scale the max load_balance interval with the number of CPUs in the system.
9632 * This trades load-balance latency on larger machines for less cross talk.
9633 */
Peter Zijlstra029632f2011-10-25 10:00:11 +02009634void update_max_interval(void)
Peter Zijlstra49c022e2011-04-05 10:14:25 +02009635{
9636 max_load_balance_interval = HZ*num_online_cpus()/10;
9637}
9638
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01009639/*
9640 * It checks each scheduling domain to see if it is due to be balanced,
9641 * and initiates a balancing operation if so.
9642 *
Libinb9b08532013-04-01 19:14:01 +08009643 * Balancing parameters are set up in init_sched_domains.
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01009644 */
Daniel Lezcanof7ed0a82014-01-06 12:34:43 +01009645static void rebalance_domains(struct rq *rq, enum cpu_idle_type idle)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01009646{
Joonsoo Kim23f0d202013-08-06 17:36:42 +09009647 int continue_balancing = 1;
Daniel Lezcanof7ed0a82014-01-06 12:34:43 +01009648 int cpu = rq->cpu;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01009649 unsigned long interval;
Peter Zijlstra04f733b2012-05-11 00:12:02 +02009650 struct sched_domain *sd;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01009651 /* Earliest time when we have to do rebalance again */
9652 unsigned long next_balance = jiffies + 60*HZ;
9653 int update_next_balance = 0;
Jason Lowf48627e2013-09-13 11:26:53 -07009654 int need_serialize, need_decay = 0;
9655 u64 max_cost = 0;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01009656
Paul Turner48a16752012-10-04 13:18:31 +02009657 update_blocked_averages(cpu);
Peter Zijlstra2069dd72010-11-15 15:47:00 -08009658
Peter Zijlstradce840a2011-04-07 14:09:50 +02009659 rcu_read_lock();
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01009660 for_each_domain(cpu, sd) {
Jason Lowf48627e2013-09-13 11:26:53 -07009661 /*
9662 * Decay the newidle max times here because this is a regular
9663 * visit to all the domains. Decay ~1% per second.
9664 */
9665 if (time_after(jiffies, sd->next_decay_max_lb_cost)) {
9666 sd->max_newidle_lb_cost =
9667 (sd->max_newidle_lb_cost * 253) / 256;
9668 sd->next_decay_max_lb_cost = jiffies + HZ;
9669 need_decay = 1;
9670 }
9671 max_cost += sd->max_newidle_lb_cost;
9672
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01009673 if (!(sd->flags & SD_LOAD_BALANCE))
9674 continue;
9675
Jason Lowf48627e2013-09-13 11:26:53 -07009676 /*
9677 * Stop the load balance at this level. There is another
9678 * CPU in our sched group which is doing load balancing more
9679 * actively.
9680 */
9681 if (!continue_balancing) {
9682 if (need_decay)
9683 continue;
9684 break;
9685 }
9686
Jason Low52a08ef2014-05-08 17:49:22 -07009687 interval = get_sd_balance_interval(sd, idle != CPU_IDLE);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01009688
9689 need_serialize = sd->flags & SD_SERIALIZE;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01009690 if (need_serialize) {
9691 if (!spin_trylock(&balancing))
9692 goto out;
9693 }
9694
9695 if (time_after_eq(jiffies, sd->last_balance + interval)) {
Joonsoo Kim23f0d202013-08-06 17:36:42 +09009696 if (load_balance(cpu, rq, sd, idle, &continue_balancing)) {
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01009697 /*
Peter Zijlstra62633222013-08-19 12:41:09 +02009698 * The LBF_DST_PINNED logic could have changed
Joonsoo Kimde5eb2d2013-04-23 17:27:38 +09009699 * env->dst_cpu, so we can't know our idle
9700 * state even if we migrated tasks. Update it.
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01009701 */
Joonsoo Kimde5eb2d2013-04-23 17:27:38 +09009702 idle = idle_cpu(cpu) ? CPU_IDLE : CPU_NOT_IDLE;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01009703 }
9704 sd->last_balance = jiffies;
Jason Low52a08ef2014-05-08 17:49:22 -07009705 interval = get_sd_balance_interval(sd, idle != CPU_IDLE);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01009706 }
9707 if (need_serialize)
9708 spin_unlock(&balancing);
9709out:
9710 if (time_after(next_balance, sd->last_balance + interval)) {
9711 next_balance = sd->last_balance + interval;
9712 update_next_balance = 1;
9713 }
Jason Lowf48627e2013-09-13 11:26:53 -07009714 }
9715 if (need_decay) {
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01009716 /*
Jason Lowf48627e2013-09-13 11:26:53 -07009717 * Ensure the rq-wide value also decays but keep it at a
9718 * reasonable floor to avoid funnies with rq->avg_idle.
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01009719 */
Jason Lowf48627e2013-09-13 11:26:53 -07009720 rq->max_idle_balance_cost =
9721 max((u64)sysctl_sched_migration_cost, max_cost);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01009722 }
Peter Zijlstradce840a2011-04-07 14:09:50 +02009723 rcu_read_unlock();
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01009724
9725 /*
9726 * next_balance will be updated only when there is a need.
9727 * When the cpu is attached to null domain for ex, it will not be
9728 * updated.
9729 */
Vincent Guittotc5afb6a2015-08-03 11:55:50 +02009730 if (likely(update_next_balance)) {
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01009731 rq->next_balance = next_balance;
Vincent Guittotc5afb6a2015-08-03 11:55:50 +02009732
9733#ifdef CONFIG_NO_HZ_COMMON
9734 /*
9735 * If this CPU has been elected to perform the nohz idle
9736 * balance. Other idle CPUs have already rebalanced with
9737 * nohz_idle_balance() and nohz.next_balance has been
9738 * updated accordingly. This CPU is now running the idle load
9739 * balance for itself and we need to update the
9740 * nohz.next_balance accordingly.
9741 */
9742 if ((idle == CPU_IDLE) && time_after(nohz.next_balance, rq->next_balance))
9743 nohz.next_balance = rq->next_balance;
9744#endif
9745 }
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01009746}
9747
Frederic Weisbecker3451d022011-08-10 23:21:01 +02009748#ifdef CONFIG_NO_HZ_COMMON
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07009749/*
Frederic Weisbecker3451d022011-08-10 23:21:01 +02009750 * In CONFIG_NO_HZ_COMMON case, the idle balance kickee will do the
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07009751 * rebalancing for all the cpus for whom scheduler ticks are stopped.
9752 */
Daniel Lezcano208cb162014-01-06 12:34:44 +01009753static void nohz_idle_balance(struct rq *this_rq, enum cpu_idle_type idle)
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07009754{
Daniel Lezcano208cb162014-01-06 12:34:44 +01009755 int this_cpu = this_rq->cpu;
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07009756 struct rq *rq;
9757 int balance_cpu;
Vincent Guittotc5afb6a2015-08-03 11:55:50 +02009758 /* Earliest time when we have to do rebalance again */
9759 unsigned long next_balance = jiffies + 60*HZ;
9760 int update_next_balance = 0;
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07009761
Suresh Siddha1c792db2011-12-01 17:07:32 -08009762 if (idle != CPU_IDLE ||
9763 !test_bit(NOHZ_BALANCE_KICK, nohz_flags(this_cpu)))
9764 goto end;
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07009765
9766 for_each_cpu(balance_cpu, nohz.idle_cpus_mask) {
Suresh Siddha8a6d42d2011-12-06 11:19:37 -08009767 if (balance_cpu == this_cpu || !idle_cpu(balance_cpu))
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07009768 continue;
9769
9770 /*
9771 * If this cpu gets work to do, stop the load balancing
9772 * work being done for other cpus. Next load
9773 * balancing owner will pick it up.
9774 */
Suresh Siddha1c792db2011-12-01 17:07:32 -08009775 if (need_resched())
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07009776 break;
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07009777
Vincent Guittot5ed4f1d2012-09-13 06:11:26 +02009778 rq = cpu_rq(balance_cpu);
9779
Tim Chened61bbc2014-05-20 14:39:27 -07009780 /*
9781 * If time for next balance is due,
9782 * do the balance.
9783 */
9784 if (time_after_eq(jiffies, rq->next_balance)) {
9785 raw_spin_lock_irq(&rq->lock);
9786 update_rq_clock(rq);
Frederic Weisbeckercee1afc2016-04-13 15:56:50 +02009787 cpu_load_update_idle(rq);
Tim Chened61bbc2014-05-20 14:39:27 -07009788 raw_spin_unlock_irq(&rq->lock);
9789 rebalance_domains(rq, CPU_IDLE);
9790 }
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07009791
Vincent Guittotc5afb6a2015-08-03 11:55:50 +02009792 if (time_after(next_balance, rq->next_balance)) {
9793 next_balance = rq->next_balance;
9794 update_next_balance = 1;
9795 }
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07009796 }
Vincent Guittotc5afb6a2015-08-03 11:55:50 +02009797
9798 /*
9799 * next_balance will be updated only when there is a need.
9800 * When the CPU is attached to null domain for ex, it will not be
9801 * updated.
9802 */
9803 if (likely(update_next_balance))
9804 nohz.next_balance = next_balance;
Suresh Siddha1c792db2011-12-01 17:07:32 -08009805end:
9806 clear_bit(NOHZ_BALANCE_KICK, nohz_flags(this_cpu));
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07009807}
9808
Syed Rameez Mustafadddcab72016-09-07 16:18:27 -07009809#ifdef CONFIG_SCHED_HMP
9810static inline int _nohz_kick_needed_hmp(struct rq *rq, int cpu, int *type)
9811{
9812 struct sched_domain *sd;
9813 int i;
9814
9815 if (rq->nr_running < 2)
9816 return 0;
9817
9818 if (!sysctl_sched_restrict_cluster_spill || sched_boost())
9819 return 1;
9820
9821 if (hmp_capable() && cpu_max_possible_capacity(cpu) ==
9822 max_possible_capacity)
9823 return 1;
9824
9825 rcu_read_lock();
9826 sd = rcu_dereference_check_sched_domain(rq->sd);
9827 if (!sd) {
9828 rcu_read_unlock();
9829 return 0;
9830 }
9831
9832 for_each_cpu(i, sched_domain_span(sd)) {
9833 if (cpu_load(i) < sched_spill_load &&
9834 cpu_rq(i)->nr_running <
9835 sysctl_sched_spill_nr_run) {
9836 /* Change the kick type to limit to CPUs that
9837 * are of equal or lower capacity.
9838 */
9839 *type = NOHZ_KICK_RESTRICT;
9840 break;
9841 }
9842 }
9843 rcu_read_unlock();
9844 return 1;
9845}
9846#endif /* CONFIG_SCHED_HMP */
9847
9848static inline int _nohz_kick_needed(struct rq *rq, int cpu, int *type)
9849{
9850 unsigned long now = jiffies;
9851
9852 /*
9853 * None are in tickless mode and hence no need for NOHZ idle load
9854 * balancing.
9855 */
9856 if (likely(!atomic_read(&nohz.nr_cpus)))
9857 return 0;
9858
9859#ifdef CONFIG_SCHED_HMP
9860 return _nohz_kick_needed_hmp(rq, cpu, type);
9861#endif
9862
9863 if (time_before(now, nohz.next_balance))
9864 return 0;
9865
9866 return (rq->nr_running >= 2);
9867}
9868
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07009869/*
Suresh Siddha0b005cf2011-12-01 17:07:34 -08009870 * Current heuristic for kicking the idle load balancer in the presence
Vincent Guittot1aaf90a2015-02-27 16:54:14 +01009871 * of an idle cpu in the system.
Suresh Siddha0b005cf2011-12-01 17:07:34 -08009872 * - This rq has more than one task.
Vincent Guittot1aaf90a2015-02-27 16:54:14 +01009873 * - This rq has at least one CFS task and the capacity of the CPU is
9874 * significantly reduced because of RT tasks or IRQs.
9875 * - At parent of LLC scheduler domain level, this cpu's scheduler group has
9876 * multiple busy cpu.
Suresh Siddha0b005cf2011-12-01 17:07:34 -08009877 * - For SD_ASYM_PACKING, if the lower numbered cpu's in the scheduler
9878 * domain span are idle.
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07009879 */
Syed Rameez Mustafadddcab72016-09-07 16:18:27 -07009880static inline bool nohz_kick_needed(struct rq *rq, int *type)
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07009881{
Syed Rameez Mustafadddcab72016-09-07 16:18:27 -07009882#ifndef CONFIG_SCHED_HMP
Peter Zijlstra0e369d72016-05-09 10:38:01 +02009883 struct sched_domain_shared *sds;
Suresh Siddha0b005cf2011-12-01 17:07:34 -08009884 struct sched_domain *sd;
Syed Rameez Mustafadddcab72016-09-07 16:18:27 -07009885 int nr_busy;
9886#endif
9887 int cpu = rq->cpu;
Vincent Guittot1aaf90a2015-02-27 16:54:14 +01009888 bool kick = false;
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07009889
Daniel Lezcano4a725622014-01-06 12:34:39 +01009890 if (unlikely(rq->idle_balance))
Vincent Guittot1aaf90a2015-02-27 16:54:14 +01009891 return false;
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07009892
Suresh Siddha1c792db2011-12-01 17:07:32 -08009893 /*
9894 * We may be recently in ticked or tickless idle mode. At the first
9895 * busy tick after returning from idle, we will update the busy stats.
9896 */
Suresh Siddha69e1e812011-12-01 17:07:33 -08009897 set_cpu_sd_state_busy();
Alex Shic1cc0172012-09-10 15:10:58 +08009898 nohz_balance_exit_idle(cpu);
Suresh Siddha0b005cf2011-12-01 17:07:34 -08009899
Syed Rameez Mustafadddcab72016-09-07 16:18:27 -07009900 if (_nohz_kick_needed(rq, cpu, type))
Vincent Guittot1aaf90a2015-02-27 16:54:14 +01009901 return true;
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07009902
Syed Rameez Mustafadddcab72016-09-07 16:18:27 -07009903#ifndef CONFIG_SCHED_HMP
Peter Zijlstra067491b2011-12-07 14:32:08 +01009904 rcu_read_lock();
Peter Zijlstra0e369d72016-05-09 10:38:01 +02009905 sds = rcu_dereference(per_cpu(sd_llc_shared, cpu));
9906 if (sds) {
9907 /*
9908 * XXX: write a coherent comment on why we do this.
9909 * See also: http://lkml.kernel.org/r/20111202010832.602203411@sbsiddha-desk.sc.intel.com
9910 */
9911 nr_busy = atomic_read(&sds->nr_busy_cpus);
Vincent Guittot1aaf90a2015-02-27 16:54:14 +01009912 if (nr_busy > 1) {
9913 kick = true;
9914 goto unlock;
9915 }
9916
9917 }
9918
9919 sd = rcu_dereference(rq->sd);
9920 if (sd) {
9921 if ((rq->cfs.h_nr_running >= 1) &&
9922 check_cpu_capacity(rq, sd)) {
9923 kick = true;
9924 goto unlock;
9925 }
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07009926 }
Preeti U Murthy37dc6b52013-10-30 08:42:52 +05309927
9928 sd = rcu_dereference(per_cpu(sd_asym, cpu));
Preeti U Murthy37dc6b52013-10-30 08:42:52 +05309929 if (sd && (cpumask_first_and(nohz.idle_cpus_mask,
Vincent Guittot1aaf90a2015-02-27 16:54:14 +01009930 sched_domain_span(sd)) < cpu)) {
9931 kick = true;
9932 goto unlock;
9933 }
Preeti U Murthy37dc6b52013-10-30 08:42:52 +05309934
Vincent Guittot1aaf90a2015-02-27 16:54:14 +01009935unlock:
Peter Zijlstra067491b2011-12-07 14:32:08 +01009936 rcu_read_unlock();
Syed Rameez Mustafadddcab72016-09-07 16:18:27 -07009937#endif
Vincent Guittot1aaf90a2015-02-27 16:54:14 +01009938 return kick;
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07009939}
9940#else
Daniel Lezcano208cb162014-01-06 12:34:44 +01009941static void nohz_idle_balance(struct rq *this_rq, enum cpu_idle_type idle) { }
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07009942#endif
9943
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01009944/*
9945 * run_rebalance_domains is triggered when needed from the scheduler tick.
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07009946 * Also triggered for nohz idle balancing (with nohz_balancing_kick set).
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01009947 */
Emese Revfy0766f782016-06-20 20:42:34 +02009948static __latent_entropy void run_rebalance_domains(struct softirq_action *h)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01009949{
Daniel Lezcano208cb162014-01-06 12:34:44 +01009950 struct rq *this_rq = this_rq();
Suresh Siddha6eb57e02011-10-03 15:09:01 -07009951 enum cpu_idle_type idle = this_rq->idle_balance ?
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01009952 CPU_IDLE : CPU_NOT_IDLE;
9953
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01009954 /*
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07009955 * If this cpu has a pending nohz_balance_kick, then do the
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01009956 * balancing on behalf of the other idle cpus whose ticks are
Preeti U Murthyd4573c32015-03-26 18:32:44 +05309957 * stopped. Do nohz_idle_balance *before* rebalance_domains to
9958 * give the idle cpus a chance to load balance. Else we may
9959 * load balance only within the local sched_domain hierarchy
9960 * and abort nohz_idle_balance altogether if we pull some load.
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01009961 */
Daniel Lezcano208cb162014-01-06 12:34:44 +01009962 nohz_idle_balance(this_rq, idle);
Preeti U Murthyd4573c32015-03-26 18:32:44 +05309963 rebalance_domains(this_rq, idle);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01009964}
9965
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01009966/*
9967 * Trigger the SCHED_SOFTIRQ if it is time to do periodic load balancing.
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01009968 */
Daniel Lezcano7caff662014-01-06 12:34:38 +01009969void trigger_load_balance(struct rq *rq)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01009970{
Syed Rameez Mustafadddcab72016-09-07 16:18:27 -07009971 int type = NOHZ_KICK_ANY;
9972
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01009973 /* Don't need to rebalance while attached to NULL domain */
Daniel Lezcanoc7260992014-01-06 12:34:45 +01009974 if (unlikely(on_null_domain(rq)))
9975 return;
9976
9977 if (time_after_eq(jiffies, rq->next_balance))
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01009978 raise_softirq(SCHED_SOFTIRQ);
Frederic Weisbecker3451d022011-08-10 23:21:01 +02009979#ifdef CONFIG_NO_HZ_COMMON
Syed Rameez Mustafadddcab72016-09-07 16:18:27 -07009980 if (nohz_kick_needed(rq, &type))
9981 nohz_balancer_kick(type);
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07009982#endif
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01009983}
9984
Christian Ehrhardt0bcdcf22009-11-30 12:16:46 +01009985static void rq_online_fair(struct rq *rq)
9986{
9987 update_sysctl();
Kirill Tkhai0e59bda2014-06-25 12:19:42 +04009988
9989 update_runtime_enabled(rq);
Christian Ehrhardt0bcdcf22009-11-30 12:16:46 +01009990}
9991
9992static void rq_offline_fair(struct rq *rq)
9993{
9994 update_sysctl();
Peter Boonstoppela4c96ae2012-08-09 15:34:47 -07009995
9996 /* Ensure any throttled groups are reachable by pick_next_task */
9997 unthrottle_offline_cfs_rqs(rq);
Christian Ehrhardt0bcdcf22009-11-30 12:16:46 +01009998}
9999
Dhaval Giani55e12e52008-06-24 23:39:43 +053010000#endif /* CONFIG_SMP */
Peter Williamse1d14842007-10-24 18:23:51 +020010001
Ingo Molnarbf0f6f22007-07-09 18:51:58 +020010002/*
10003 * scheduler tick hitting a task of our scheduling class:
10004 */
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +010010005static void task_tick_fair(struct rq *rq, struct task_struct *curr, int queued)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +020010006{
10007 struct cfs_rq *cfs_rq;
10008 struct sched_entity *se = &curr->se;
10009
10010 for_each_sched_entity(se) {
10011 cfs_rq = cfs_rq_of(se);
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +010010012 entity_tick(cfs_rq, se, queued);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +020010013 }
Ben Segall18bf2802012-10-04 12:51:20 +020010014
Srikar Dronamrajub52da862015-10-02 07:48:25 +053010015 if (static_branch_unlikely(&sched_numa_balancing))
Peter Zijlstracbee9f82012-10-25 14:16:43 +020010016 task_tick_numa(rq, curr);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +020010017}
10018
10019/*
Peter Zijlstracd29fe62009-11-27 17:32:46 +010010020 * called on fork with the child task as argument from the parent's context
10021 * - child not yet on the tasklist
10022 * - preemption disabled
Ingo Molnarbf0f6f22007-07-09 18:51:58 +020010023 */
Peter Zijlstracd29fe62009-11-27 17:32:46 +010010024static void task_fork_fair(struct task_struct *p)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +020010025{
Daisuke Nishimura4fc420c2011-12-15 14:36:55 +090010026 struct cfs_rq *cfs_rq;
10027 struct sched_entity *se = &p->se, *curr;
Peter Zijlstracd29fe62009-11-27 17:32:46 +010010028 struct rq *rq = this_rq();
Ingo Molnarbf0f6f22007-07-09 18:51:58 +020010029
Peter Zijlstrae210bff2016-06-16 18:51:48 +020010030 raw_spin_lock(&rq->lock);
Peter Zijlstra861d0342010-08-19 13:31:43 +020010031 update_rq_clock(rq);
10032
Daisuke Nishimura4fc420c2011-12-15 14:36:55 +090010033 cfs_rq = task_cfs_rq(current);
10034 curr = cfs_rq->curr;
Peter Zijlstrae210bff2016-06-16 18:51:48 +020010035 if (curr) {
10036 update_curr(cfs_rq);
Mike Galbraithb5d9d732009-09-08 11:12:28 +020010037 se->vruntime = curr->vruntime;
Peter Zijlstrae210bff2016-06-16 18:51:48 +020010038 }
Peter Zijlstraaeb73b02007-10-15 17:00:05 +020010039 place_entity(cfs_rq, se, 1);
Peter Zijlstra4d78e7b2007-10-15 17:00:04 +020010040
Peter Zijlstracd29fe62009-11-27 17:32:46 +010010041 if (sysctl_sched_child_runs_first && curr && entity_before(curr, se)) {
Dmitry Adamushko87fefa32007-10-15 17:00:08 +020010042 /*
Ingo Molnaredcb60a2007-10-15 17:00:08 +020010043 * Upon rescheduling, sched_class::put_prev_task() will place
10044 * 'current' within the tree based on its new key value.
10045 */
Peter Zijlstra4d78e7b2007-10-15 17:00:04 +020010046 swap(curr->vruntime, se->vruntime);
Kirill Tkhai88751252014-06-29 00:03:57 +040010047 resched_curr(rq);
Peter Zijlstra4d78e7b2007-10-15 17:00:04 +020010048 }
10049
Peter Zijlstra88ec22d2009-12-16 18:04:41 +010010050 se->vruntime -= cfs_rq->min_vruntime;
Peter Zijlstrae210bff2016-06-16 18:51:48 +020010051 raw_spin_unlock(&rq->lock);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +020010052}
10053
Steven Rostedtcb469842008-01-25 21:08:22 +010010054/*
10055 * Priority of the task has changed. Check to see if we preempt
10056 * the current task.
10057 */
Peter Zijlstrada7a7352011-01-17 17:03:27 +010010058static void
10059prio_changed_fair(struct rq *rq, struct task_struct *p, int oldprio)
Steven Rostedtcb469842008-01-25 21:08:22 +010010060{
Kirill Tkhaida0c1e62014-08-20 13:47:32 +040010061 if (!task_on_rq_queued(p))
Peter Zijlstrada7a7352011-01-17 17:03:27 +010010062 return;
10063
Steven Rostedtcb469842008-01-25 21:08:22 +010010064 /*
10065 * Reschedule if we are currently running on this runqueue and
10066 * our priority decreased, or if we are not currently running on
10067 * this runqueue and our priority is higher than the current's
10068 */
Peter Zijlstrada7a7352011-01-17 17:03:27 +010010069 if (rq->curr == p) {
Steven Rostedtcb469842008-01-25 21:08:22 +010010070 if (p->prio > oldprio)
Kirill Tkhai88751252014-06-29 00:03:57 +040010071 resched_curr(rq);
Steven Rostedtcb469842008-01-25 21:08:22 +010010072 } else
Peter Zijlstra15afe092008-09-20 23:38:02 +020010073 check_preempt_curr(rq, p, 0);
Steven Rostedtcb469842008-01-25 21:08:22 +010010074}
10075
Byungchul Parkdaa59402015-08-20 20:22:00 +090010076static inline bool vruntime_normalized(struct task_struct *p)
10077{
10078 struct sched_entity *se = &p->se;
10079
10080 /*
10081 * In both the TASK_ON_RQ_QUEUED and TASK_ON_RQ_MIGRATING cases,
10082 * the dequeue_entity(.flags=0) will already have normalized the
10083 * vruntime.
10084 */
10085 if (p->on_rq)
10086 return true;
10087
10088 /*
10089 * When !on_rq, vruntime of the task has usually NOT been normalized.
10090 * But there are some cases where it has already been normalized:
10091 *
10092 * - A forked child which is waiting for being woken up by
10093 * wake_up_new_task().
10094 * - A task which has been woken up by try_to_wake_up() and
10095 * waiting for actually being woken up by sched_ttwu_pending().
10096 */
10097 if (!se->sum_exec_runtime || p->state == TASK_WAKING)
10098 return true;
10099
10100 return false;
10101}
10102
10103static void detach_task_cfs_rq(struct task_struct *p)
Peter Zijlstrada7a7352011-01-17 17:03:27 +010010104{
10105 struct sched_entity *se = &p->se;
10106 struct cfs_rq *cfs_rq = cfs_rq_of(se);
Peter Zijlstra01011472016-06-17 11:20:46 +020010107 u64 now = cfs_rq_clock_task(cfs_rq);
Peter Zijlstrada7a7352011-01-17 17:03:27 +010010108
Byungchul Parkdaa59402015-08-20 20:22:00 +090010109 if (!vruntime_normalized(p)) {
Peter Zijlstrada7a7352011-01-17 17:03:27 +010010110 /*
10111 * Fix up our vruntime so that the current sleep doesn't
10112 * cause 'unlimited' sleep bonus.
10113 */
10114 place_entity(cfs_rq, se, 0);
10115 se->vruntime -= cfs_rq->min_vruntime;
10116 }
Paul Turner9ee474f2012-10-04 13:18:30 +020010117
Yuyang Du9d89c252015-07-15 08:04:37 +080010118 /* Catch up with the cfs_rq and remove our load when we leave */
Peter Zijlstra7c3edd22016-07-13 10:56:25 +020010119 update_cfs_rq_load_avg(now, cfs_rq, false);
Byungchul Parka05e8c52015-08-20 20:21:56 +090010120 detach_entity_load_avg(cfs_rq, se);
Peter Zijlstra7c3edd22016-07-13 10:56:25 +020010121 update_tg_load_avg(cfs_rq, false);
Peter Zijlstrada7a7352011-01-17 17:03:27 +010010122}
10123
Byungchul Parkdaa59402015-08-20 20:22:00 +090010124static void attach_task_cfs_rq(struct task_struct *p)
Steven Rostedtcb469842008-01-25 21:08:22 +010010125{
Kirill Tkhaif36c0192014-08-06 12:06:01 +040010126 struct sched_entity *se = &p->se;
Byungchul Parkdaa59402015-08-20 20:22:00 +090010127 struct cfs_rq *cfs_rq = cfs_rq_of(se);
Peter Zijlstra01011472016-06-17 11:20:46 +020010128 u64 now = cfs_rq_clock_task(cfs_rq);
Byungchul Park7855a352015-08-10 18:02:55 +090010129
10130#ifdef CONFIG_FAIR_GROUP_SCHED
Michael wangeb7a59b2014-02-20 11:14:53 +080010131 /*
10132 * Since the real-depth could have been changed (only FAIR
10133 * class maintain depth value), reset depth properly.
10134 */
10135 se->depth = se->parent ? se->parent->depth + 1 : 0;
10136#endif
Byungchul Park7855a352015-08-10 18:02:55 +090010137
Byungchul Park6efdb102015-08-20 20:21:59 +090010138 /* Synchronize task with its cfs_rq */
Peter Zijlstra7c3edd22016-07-13 10:56:25 +020010139 update_cfs_rq_load_avg(now, cfs_rq, false);
Byungchul Parkdaa59402015-08-20 20:22:00 +090010140 attach_entity_load_avg(cfs_rq, se);
Peter Zijlstra7c3edd22016-07-13 10:56:25 +020010141 update_tg_load_avg(cfs_rq, false);
Byungchul Park6efdb102015-08-20 20:21:59 +090010142
Byungchul Parkdaa59402015-08-20 20:22:00 +090010143 if (!vruntime_normalized(p))
10144 se->vruntime += cfs_rq->min_vruntime;
10145}
Byungchul Park7855a352015-08-10 18:02:55 +090010146
Byungchul Parkdaa59402015-08-20 20:22:00 +090010147static void switched_from_fair(struct rq *rq, struct task_struct *p)
10148{
10149 detach_task_cfs_rq(p);
10150}
10151
10152static void switched_to_fair(struct rq *rq, struct task_struct *p)
10153{
10154 attach_task_cfs_rq(p);
10155
10156 if (task_on_rq_queued(p)) {
Byungchul Park7855a352015-08-10 18:02:55 +090010157 /*
Byungchul Parkdaa59402015-08-20 20:22:00 +090010158 * We were most likely switched from sched_rt, so
10159 * kick off the schedule if running, otherwise just see
10160 * if we can still preempt the current task.
Byungchul Park7855a352015-08-10 18:02:55 +090010161 */
Byungchul Parkdaa59402015-08-20 20:22:00 +090010162 if (rq->curr == p)
10163 resched_curr(rq);
10164 else
10165 check_preempt_curr(rq, p, 0);
Byungchul Park7855a352015-08-10 18:02:55 +090010166 }
Steven Rostedtcb469842008-01-25 21:08:22 +010010167}
10168
Srivatsa Vaddagiri83b699e2007-10-15 17:00:08 +020010169/* Account for a task changing its policy or group.
10170 *
10171 * This routine is mostly called to set cfs_rq->curr field when a task
10172 * migrates between groups/classes.
10173 */
10174static void set_curr_task_fair(struct rq *rq)
10175{
10176 struct sched_entity *se = &rq->curr->se;
10177
Paul Turnerec12cb72011-07-21 09:43:30 -070010178 for_each_sched_entity(se) {
10179 struct cfs_rq *cfs_rq = cfs_rq_of(se);
10180
10181 set_next_entity(cfs_rq, se);
10182 /* ensure bandwidth has been allocated on our new cfs_rq */
10183 account_cfs_rq_runtime(cfs_rq, 0);
10184 }
Srivatsa Vaddagiri83b699e2007-10-15 17:00:08 +020010185}
10186
Peter Zijlstra029632f2011-10-25 10:00:11 +020010187void init_cfs_rq(struct cfs_rq *cfs_rq)
10188{
10189 cfs_rq->tasks_timeline = RB_ROOT;
Peter Zijlstra029632f2011-10-25 10:00:11 +020010190 cfs_rq->min_vruntime = (u64)(-(1LL << 20));
10191#ifndef CONFIG_64BIT
10192 cfs_rq->min_vruntime_copy = cfs_rq->min_vruntime;
10193#endif
Alex Shi141965c2013-06-26 13:05:39 +080010194#ifdef CONFIG_SMP
Yuyang Du9d89c252015-07-15 08:04:37 +080010195 atomic_long_set(&cfs_rq->removed_load_avg, 0);
10196 atomic_long_set(&cfs_rq->removed_util_avg, 0);
Paul Turner9ee474f2012-10-04 13:18:30 +020010197#endif
Peter Zijlstra029632f2011-10-25 10:00:11 +020010198}
10199
Peter Zijlstra810b3812008-02-29 15:21:01 -050010200#ifdef CONFIG_FAIR_GROUP_SCHED
Vincent Guittotea86cb42016-06-17 13:38:55 +020010201static void task_set_group_fair(struct task_struct *p)
10202{
10203 struct sched_entity *se = &p->se;
10204
10205 set_task_rq(p, task_cpu(p));
10206 se->depth = se->parent ? se->parent->depth + 1 : 0;
10207}
10208
Peter Zijlstrabc54da22015-08-31 17:13:55 +020010209static void task_move_group_fair(struct task_struct *p)
Peter Zijlstra810b3812008-02-29 15:21:01 -050010210{
Byungchul Parkdaa59402015-08-20 20:22:00 +090010211 detach_task_cfs_rq(p);
Peter Zijlstrab2b5ce02010-10-15 15:24:15 +020010212 set_task_rq(p, task_cpu(p));
Byungchul Park6efdb102015-08-20 20:21:59 +090010213
10214#ifdef CONFIG_SMP
10215 /* Tell se's cfs_rq has been changed -- migrated */
10216 p->se.avg.last_update_time = 0;
10217#endif
Byungchul Parkdaa59402015-08-20 20:22:00 +090010218 attach_task_cfs_rq(p);
Peter Zijlstra810b3812008-02-29 15:21:01 -050010219}
Peter Zijlstra029632f2011-10-25 10:00:11 +020010220
Vincent Guittotea86cb42016-06-17 13:38:55 +020010221static void task_change_group_fair(struct task_struct *p, int type)
10222{
10223 switch (type) {
10224 case TASK_SET_GROUP:
10225 task_set_group_fair(p);
10226 break;
10227
10228 case TASK_MOVE_GROUP:
10229 task_move_group_fair(p);
10230 break;
10231 }
10232}
10233
Peter Zijlstra029632f2011-10-25 10:00:11 +020010234void free_fair_sched_group(struct task_group *tg)
10235{
10236 int i;
10237
10238 destroy_cfs_bandwidth(tg_cfs_bandwidth(tg));
10239
10240 for_each_possible_cpu(i) {
10241 if (tg->cfs_rq)
10242 kfree(tg->cfs_rq[i]);
Peter Zijlstra6fe1f342016-01-21 22:24:16 +010010243 if (tg->se)
Peter Zijlstra029632f2011-10-25 10:00:11 +020010244 kfree(tg->se[i]);
10245 }
10246
10247 kfree(tg->cfs_rq);
10248 kfree(tg->se);
10249}
10250
10251int alloc_fair_sched_group(struct task_group *tg, struct task_group *parent)
10252{
Peter Zijlstra029632f2011-10-25 10:00:11 +020010253 struct sched_entity *se;
Peter Zijlstrab7fa30c2016-06-09 15:07:50 +020010254 struct cfs_rq *cfs_rq;
Peter Zijlstra029632f2011-10-25 10:00:11 +020010255 int i;
10256
10257 tg->cfs_rq = kzalloc(sizeof(cfs_rq) * nr_cpu_ids, GFP_KERNEL);
10258 if (!tg->cfs_rq)
10259 goto err;
10260 tg->se = kzalloc(sizeof(se) * nr_cpu_ids, GFP_KERNEL);
10261 if (!tg->se)
10262 goto err;
10263
10264 tg->shares = NICE_0_LOAD;
10265
10266 init_cfs_bandwidth(tg_cfs_bandwidth(tg));
10267
10268 for_each_possible_cpu(i) {
10269 cfs_rq = kzalloc_node(sizeof(struct cfs_rq),
10270 GFP_KERNEL, cpu_to_node(i));
10271 if (!cfs_rq)
10272 goto err;
10273
10274 se = kzalloc_node(sizeof(struct sched_entity),
10275 GFP_KERNEL, cpu_to_node(i));
10276 if (!se)
10277 goto err_free_rq;
10278
10279 init_cfs_rq(cfs_rq);
10280 init_tg_cfs_entry(tg, cfs_rq, se, i, parent->se[i]);
Yuyang Du540247f2015-07-15 08:04:39 +080010281 init_entity_runnable_average(se);
Peter Zijlstra029632f2011-10-25 10:00:11 +020010282 }
10283
10284 return 1;
10285
10286err_free_rq:
10287 kfree(cfs_rq);
10288err:
10289 return 0;
10290}
10291
Peter Zijlstra8663e242016-06-22 14:58:02 +020010292void online_fair_sched_group(struct task_group *tg)
10293{
10294 struct sched_entity *se;
10295 struct rq *rq;
10296 int i;
10297
10298 for_each_possible_cpu(i) {
10299 rq = cpu_rq(i);
10300 se = tg->se[i];
10301
10302 raw_spin_lock_irq(&rq->lock);
10303 post_init_entity_util_avg(se);
Peter Zijlstra55e16d32016-06-22 15:14:26 +020010304 sync_throttle(tg, i);
Peter Zijlstra8663e242016-06-22 14:58:02 +020010305 raw_spin_unlock_irq(&rq->lock);
10306 }
10307}
10308
Peter Zijlstra6fe1f342016-01-21 22:24:16 +010010309void unregister_fair_sched_group(struct task_group *tg)
Peter Zijlstra029632f2011-10-25 10:00:11 +020010310{
Peter Zijlstra029632f2011-10-25 10:00:11 +020010311 unsigned long flags;
Peter Zijlstra6fe1f342016-01-21 22:24:16 +010010312 struct rq *rq;
10313 int cpu;
Peter Zijlstra029632f2011-10-25 10:00:11 +020010314
Peter Zijlstra6fe1f342016-01-21 22:24:16 +010010315 for_each_possible_cpu(cpu) {
10316 if (tg->se[cpu])
10317 remove_entity_load_avg(tg->se[cpu]);
Peter Zijlstra029632f2011-10-25 10:00:11 +020010318
Peter Zijlstra6fe1f342016-01-21 22:24:16 +010010319 /*
10320 * Only empty task groups can be destroyed; so we can speculatively
10321 * check on_list without danger of it being re-added.
10322 */
10323 if (!tg->cfs_rq[cpu]->on_list)
10324 continue;
10325
10326 rq = cpu_rq(cpu);
10327
10328 raw_spin_lock_irqsave(&rq->lock, flags);
10329 list_del_leaf_cfs_rq(tg->cfs_rq[cpu]);
10330 raw_spin_unlock_irqrestore(&rq->lock, flags);
10331 }
Peter Zijlstra029632f2011-10-25 10:00:11 +020010332}
10333
10334void init_tg_cfs_entry(struct task_group *tg, struct cfs_rq *cfs_rq,
10335 struct sched_entity *se, int cpu,
10336 struct sched_entity *parent)
10337{
10338 struct rq *rq = cpu_rq(cpu);
10339
10340 cfs_rq->tg = tg;
10341 cfs_rq->rq = rq;
Peter Zijlstra029632f2011-10-25 10:00:11 +020010342 init_cfs_rq_runtime(cfs_rq);
10343
10344 tg->cfs_rq[cpu] = cfs_rq;
10345 tg->se[cpu] = se;
10346
10347 /* se could be NULL for root_task_group */
10348 if (!se)
10349 return;
10350
Peter Zijlstrafed14d42012-02-11 06:05:00 +010010351 if (!parent) {
Peter Zijlstra029632f2011-10-25 10:00:11 +020010352 se->cfs_rq = &rq->cfs;
Peter Zijlstrafed14d42012-02-11 06:05:00 +010010353 se->depth = 0;
10354 } else {
Peter Zijlstra029632f2011-10-25 10:00:11 +020010355 se->cfs_rq = parent->my_q;
Peter Zijlstrafed14d42012-02-11 06:05:00 +010010356 se->depth = parent->depth + 1;
10357 }
Peter Zijlstra029632f2011-10-25 10:00:11 +020010358
10359 se->my_q = cfs_rq;
Paul Turner0ac9b1c2013-10-16 11:16:27 -070010360 /* guarantee group entities always have weight */
10361 update_load_set(&se->load, NICE_0_LOAD);
Peter Zijlstra029632f2011-10-25 10:00:11 +020010362 se->parent = parent;
10363}
10364
10365static DEFINE_MUTEX(shares_mutex);
10366
10367int sched_group_set_shares(struct task_group *tg, unsigned long shares)
10368{
10369 int i;
10370 unsigned long flags;
10371
10372 /*
10373 * We can't change the weight of the root cgroup.
10374 */
10375 if (!tg->se[0])
10376 return -EINVAL;
10377
10378 shares = clamp(shares, scale_load(MIN_SHARES), scale_load(MAX_SHARES));
10379
10380 mutex_lock(&shares_mutex);
10381 if (tg->shares == shares)
10382 goto done;
10383
10384 tg->shares = shares;
10385 for_each_possible_cpu(i) {
10386 struct rq *rq = cpu_rq(i);
10387 struct sched_entity *se;
10388
10389 se = tg->se[i];
10390 /* Propagate contribution to hierarchy */
10391 raw_spin_lock_irqsave(&rq->lock, flags);
Frederic Weisbecker71b1da42013-04-12 01:50:59 +020010392
10393 /* Possible calls to update_curr() need rq clock */
10394 update_rq_clock(rq);
Linus Torvalds17bc14b2012-12-14 07:20:43 -080010395 for_each_sched_entity(se)
Peter Zijlstra029632f2011-10-25 10:00:11 +020010396 update_cfs_shares(group_cfs_rq(se));
10397 raw_spin_unlock_irqrestore(&rq->lock, flags);
10398 }
10399
10400done:
10401 mutex_unlock(&shares_mutex);
10402 return 0;
10403}
10404#else /* CONFIG_FAIR_GROUP_SCHED */
10405
10406void free_fair_sched_group(struct task_group *tg) { }
10407
10408int alloc_fair_sched_group(struct task_group *tg, struct task_group *parent)
10409{
10410 return 1;
10411}
10412
Peter Zijlstra8663e242016-06-22 14:58:02 +020010413void online_fair_sched_group(struct task_group *tg) { }
10414
Peter Zijlstra6fe1f342016-01-21 22:24:16 +010010415void unregister_fair_sched_group(struct task_group *tg) { }
Peter Zijlstra029632f2011-10-25 10:00:11 +020010416
10417#endif /* CONFIG_FAIR_GROUP_SCHED */
10418
Peter Zijlstra810b3812008-02-29 15:21:01 -050010419
H Hartley Sweeten6d686f42010-01-13 20:21:52 -070010420static unsigned int get_rr_interval_fair(struct rq *rq, struct task_struct *task)
Peter Williams0d721ce2009-09-21 01:31:53 +000010421{
10422 struct sched_entity *se = &task->se;
Peter Williams0d721ce2009-09-21 01:31:53 +000010423 unsigned int rr_interval = 0;
10424
10425 /*
10426 * Time slice is 0 for SCHED_OTHER tasks that are on an otherwise
10427 * idle runqueue:
10428 */
Peter Williams0d721ce2009-09-21 01:31:53 +000010429 if (rq->cfs.load.weight)
Zhu Yanhaia59f4e02013-01-08 12:56:52 +080010430 rr_interval = NS_TO_JIFFIES(sched_slice(cfs_rq_of(se), se));
Peter Williams0d721ce2009-09-21 01:31:53 +000010431
10432 return rr_interval;
10433}
10434
Ingo Molnarbf0f6f22007-07-09 18:51:58 +020010435/*
10436 * All the scheduling class methods:
10437 */
Peter Zijlstra029632f2011-10-25 10:00:11 +020010438const struct sched_class fair_sched_class = {
Ingo Molnar5522d5d2007-10-15 17:00:12 +020010439 .next = &idle_sched_class,
Ingo Molnarbf0f6f22007-07-09 18:51:58 +020010440 .enqueue_task = enqueue_task_fair,
10441 .dequeue_task = dequeue_task_fair,
10442 .yield_task = yield_task_fair,
Mike Galbraithd95f4122011-02-01 09:50:51 -050010443 .yield_to_task = yield_to_task_fair,
Ingo Molnarbf0f6f22007-07-09 18:51:58 +020010444
Ingo Molnar2e09bf52007-10-15 17:00:05 +020010445 .check_preempt_curr = check_preempt_wakeup,
Ingo Molnarbf0f6f22007-07-09 18:51:58 +020010446
10447 .pick_next_task = pick_next_task_fair,
10448 .put_prev_task = put_prev_task_fair,
10449
Peter Williams681f3e62007-10-24 18:23:51 +020010450#ifdef CONFIG_SMP
Li Zefan4ce72a22008-10-22 15:25:26 +080010451 .select_task_rq = select_task_rq_fair,
Paul Turner0a74bef2012-10-04 13:18:30 +020010452 .migrate_task_rq = migrate_task_rq_fair,
Alex Shi141965c2013-06-26 13:05:39 +080010453
Christian Ehrhardt0bcdcf22009-11-30 12:16:46 +010010454 .rq_online = rq_online_fair,
10455 .rq_offline = rq_offline_fair,
Peter Zijlstra88ec22d2009-12-16 18:04:41 +010010456
Yuyang Du12695572015-07-15 08:04:40 +080010457 .task_dead = task_dead_fair,
Peter Zijlstrac5b28032015-05-15 17:43:35 +020010458 .set_cpus_allowed = set_cpus_allowed_common,
Peter Williams681f3e62007-10-24 18:23:51 +020010459#endif
Ingo Molnarbf0f6f22007-07-09 18:51:58 +020010460
Srivatsa Vaddagiri83b699e2007-10-15 17:00:08 +020010461 .set_curr_task = set_curr_task_fair,
Ingo Molnarbf0f6f22007-07-09 18:51:58 +020010462 .task_tick = task_tick_fair,
Peter Zijlstracd29fe62009-11-27 17:32:46 +010010463 .task_fork = task_fork_fair,
Steven Rostedtcb469842008-01-25 21:08:22 +010010464
10465 .prio_changed = prio_changed_fair,
Peter Zijlstrada7a7352011-01-17 17:03:27 +010010466 .switched_from = switched_from_fair,
Steven Rostedtcb469842008-01-25 21:08:22 +010010467 .switched_to = switched_to_fair,
Peter Zijlstra810b3812008-02-29 15:21:01 -050010468
Peter Williams0d721ce2009-09-21 01:31:53 +000010469 .get_rr_interval = get_rr_interval_fair,
10470
Stanislaw Gruszka6e998912014-11-12 16:58:44 +010010471 .update_curr = update_curr_fair,
10472
Peter Zijlstra810b3812008-02-29 15:21:01 -050010473#ifdef CONFIG_FAIR_GROUP_SCHED
Vincent Guittotea86cb42016-06-17 13:38:55 +020010474 .task_change_group = task_change_group_fair,
Peter Zijlstra810b3812008-02-29 15:21:01 -050010475#endif
Syed Rameez Mustafadddcab72016-09-07 16:18:27 -070010476#ifdef CONFIG_SCHED_HMP
10477 .fixup_hmp_sched_stats = fixup_hmp_sched_stats_fair,
10478#endif
Ingo Molnarbf0f6f22007-07-09 18:51:58 +020010479};
10480
10481#ifdef CONFIG_SCHED_DEBUG
Peter Zijlstra029632f2011-10-25 10:00:11 +020010482void print_cfs_stats(struct seq_file *m, int cpu)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +020010483{
Ingo Molnarbf0f6f22007-07-09 18:51:58 +020010484 struct cfs_rq *cfs_rq;
10485
Peter Zijlstra5973e5b2008-01-25 21:08:34 +010010486 rcu_read_lock();
Ingo Molnarc3b64f12007-08-09 11:16:51 +020010487 for_each_leaf_cfs_rq(cpu_rq(cpu), cfs_rq)
Ingo Molnar5cef9ec2007-08-09 11:16:47 +020010488 print_cfs_rq(m, cpu, cfs_rq);
Peter Zijlstra5973e5b2008-01-25 21:08:34 +010010489 rcu_read_unlock();
Ingo Molnarbf0f6f22007-07-09 18:51:58 +020010490}
Srikar Dronamraju397f2372015-06-25 22:51:43 +053010491
10492#ifdef CONFIG_NUMA_BALANCING
10493void show_numa_stats(struct task_struct *p, struct seq_file *m)
10494{
10495 int node;
10496 unsigned long tsf = 0, tpf = 0, gsf = 0, gpf = 0;
10497
10498 for_each_online_node(node) {
10499 if (p->numa_faults) {
10500 tsf = p->numa_faults[task_faults_idx(NUMA_MEM, node, 0)];
10501 tpf = p->numa_faults[task_faults_idx(NUMA_MEM, node, 1)];
10502 }
10503 if (p->numa_group) {
10504 gsf = p->numa_group->faults[task_faults_idx(NUMA_MEM, node, 0)],
10505 gpf = p->numa_group->faults[task_faults_idx(NUMA_MEM, node, 1)];
10506 }
10507 print_numa_stats(m, node, tsf, tpf, gsf, gpf);
10508 }
10509}
10510#endif /* CONFIG_NUMA_BALANCING */
10511#endif /* CONFIG_SCHED_DEBUG */
Peter Zijlstra029632f2011-10-25 10:00:11 +020010512
10513__init void init_sched_fair_class(void)
10514{
10515#ifdef CONFIG_SMP
10516 open_softirq(SCHED_SOFTIRQ, run_rebalance_domains);
10517
Frederic Weisbecker3451d022011-08-10 23:21:01 +020010518#ifdef CONFIG_NO_HZ_COMMON
Diwakar Tundlam554ceca2012-03-07 14:44:26 -080010519 nohz.next_balance = jiffies;
Peter Zijlstra029632f2011-10-25 10:00:11 +020010520 zalloc_cpumask_var(&nohz.idle_cpus_mask, GFP_NOWAIT);
Peter Zijlstra029632f2011-10-25 10:00:11 +020010521#endif
10522#endif /* SMP */
10523
10524}