blob: 5c09ddf8c8321ca1aa18a6ff7f78d3d2937ec92e [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02002/*
3 * Completely Fair Scheduling (CFS) Class (SCHED_NORMAL/SCHED_BATCH)
4 *
5 * Copyright (C) 2007 Red Hat, Inc., Ingo Molnar <mingo@redhat.com>
6 *
7 * Interactivity improvements by Mike Galbraith
8 * (C) 2007 Mike Galbraith <efault@gmx.de>
9 *
10 * Various enhancements by Dmitry Adamushko.
11 * (C) 2007 Dmitry Adamushko <dmitry.adamushko@gmail.com>
12 *
13 * Group scheduling enhancements by Srivatsa Vaddagiri
14 * Copyright IBM Corporation, 2007
15 * Author: Srivatsa Vaddagiri <vatsa@linux.vnet.ibm.com>
16 *
17 * Scaled math optimizations by Thomas Gleixner
18 * Copyright (C) 2007, Thomas Gleixner <tglx@linutronix.de>
Peter Zijlstra21805082007-08-25 18:41:53 +020019 *
20 * Adaptive scheduling granularity, math enhancements by Peter Zijlstra
Peter Zijlstra90eec102015-11-16 11:08:45 +010021 * Copyright (C) 2007 Red Hat, Inc., Peter Zijlstra
Ingo Molnarbf0f6f22007-07-09 18:51:58 +020022 */
23
Ingo Molnar589ee622017-02-04 00:16:44 +010024#include <linux/sched/mm.h>
Ingo Molnar105ab3d2017-02-01 16:36:40 +010025#include <linux/sched/topology.h>
26
Mel Gormancb251762016-02-05 09:08:36 +000027#include <linux/latencytop.h>
Sisir Koppaka3436ae12011-03-26 18:22:55 +053028#include <linux/cpumask.h>
Nicolas Pitre83a0a962014-09-04 11:32:10 -040029#include <linux/cpuidle.h>
Peter Zijlstra029632f2011-10-25 10:00:11 +020030#include <linux/slab.h>
31#include <linux/profile.h>
32#include <linux/interrupt.h>
Peter Zijlstracbee9f82012-10-25 14:16:43 +020033#include <linux/mempolicy.h>
Mel Gormane14808b2012-11-19 10:59:15 +000034#include <linux/migrate.h>
Peter Zijlstracbee9f82012-10-25 14:16:43 +020035#include <linux/task_work.h>
Peter Zijlstra029632f2011-10-25 10:00:11 +020036
37#include <trace/events/sched.h>
38
39#include "sched.h"
Arjan van de Ven97455122008-01-25 21:08:34 +010040
Ingo Molnarbf0f6f22007-07-09 18:51:58 +020041/*
Peter Zijlstra21805082007-08-25 18:41:53 +020042 * Targeted preemption latency for CPU-bound tasks:
Ingo Molnarbf0f6f22007-07-09 18:51:58 +020043 *
Peter Zijlstra21805082007-08-25 18:41:53 +020044 * NOTE: this latency value is not the same as the concept of
Ingo Molnard274a4c2007-10-15 17:00:14 +020045 * 'timeslice length' - timeslices in CFS are of variable length
46 * and have no persistent notion like in traditional, time-slice
47 * based scheduling concepts.
Ingo Molnarbf0f6f22007-07-09 18:51:58 +020048 *
Ingo Molnard274a4c2007-10-15 17:00:14 +020049 * (to see the precise effective timeslice length of your workload,
50 * run vmstat and monitor the context-switches (cs) field)
Ingo Molnar2b4d5b22016-11-23 07:37:00 +010051 *
52 * (default: 6ms * (1 + ilog(ncpus)), units: nanoseconds)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +020053 */
Ingo Molnar2b4d5b22016-11-23 07:37:00 +010054unsigned int sysctl_sched_latency = 6000000ULL;
55unsigned int normalized_sysctl_sched_latency = 6000000ULL;
Ingo Molnar2bd8e6d2007-10-15 17:00:02 +020056
57/*
Christian Ehrhardt1983a922009-11-30 12:16:47 +010058 * The initial- and re-scaling of tunables is configurable
Christian Ehrhardt1983a922009-11-30 12:16:47 +010059 *
60 * Options are:
Ingo Molnar2b4d5b22016-11-23 07:37:00 +010061 *
62 * SCHED_TUNABLESCALING_NONE - unscaled, always *1
63 * SCHED_TUNABLESCALING_LOG - scaled logarithmical, *1+ilog(ncpus)
64 * SCHED_TUNABLESCALING_LINEAR - scaled linear, *ncpus
65 *
66 * (default SCHED_TUNABLESCALING_LOG = *(1+ilog(ncpus))
Christian Ehrhardt1983a922009-11-30 12:16:47 +010067 */
Ingo Molnar2b4d5b22016-11-23 07:37:00 +010068enum sched_tunable_scaling sysctl_sched_tunable_scaling = SCHED_TUNABLESCALING_LOG;
Christian Ehrhardt1983a922009-11-30 12:16:47 +010069
70/*
Peter Zijlstrab2be5e92007-11-09 22:39:37 +010071 * Minimal preemption granularity for CPU-bound tasks:
Ingo Molnar2b4d5b22016-11-23 07:37:00 +010072 *
Takuya Yoshikawa864616e2010-10-14 16:09:13 +090073 * (default: 0.75 msec * (1 + ilog(ncpus)), units: nanoseconds)
Peter Zijlstrab2be5e92007-11-09 22:39:37 +010074 */
Ingo Molnar2b4d5b22016-11-23 07:37:00 +010075unsigned int sysctl_sched_min_granularity = 750000ULL;
76unsigned int normalized_sysctl_sched_min_granularity = 750000ULL;
Peter Zijlstrab2be5e92007-11-09 22:39:37 +010077
78/*
Ingo Molnar2b4d5b22016-11-23 07:37:00 +010079 * This value is kept at sysctl_sched_latency/sysctl_sched_min_granularity
Peter Zijlstrab2be5e92007-11-09 22:39:37 +010080 */
Ingo Molnar0bf377b2010-09-12 08:14:52 +020081static unsigned int sched_nr_latency = 8;
Peter Zijlstrab2be5e92007-11-09 22:39:37 +010082
83/*
Mike Galbraith2bba22c2009-09-09 15:41:37 +020084 * After fork, child runs first. If set to 0 (default) then
Ingo Molnar2bd8e6d2007-10-15 17:00:02 +020085 * parent will (try to) run first.
86 */
Mike Galbraith2bba22c2009-09-09 15:41:37 +020087unsigned int sysctl_sched_child_runs_first __read_mostly;
Peter Zijlstra21805082007-08-25 18:41:53 +020088
89/*
Ingo Molnarbf0f6f22007-07-09 18:51:58 +020090 * SCHED_OTHER wake-up granularity.
Ingo Molnarbf0f6f22007-07-09 18:51:58 +020091 *
92 * This option delays the preemption effects of decoupled workloads
93 * and reduces their over-scheduling. Synchronous workloads will still
94 * have immediate wakeup/sleep latencies.
Ingo Molnar2b4d5b22016-11-23 07:37:00 +010095 *
96 * (default: 1 msec * (1 + ilog(ncpus)), units: nanoseconds)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +020097 */
Ingo Molnar2b4d5b22016-11-23 07:37:00 +010098unsigned int sysctl_sched_wakeup_granularity = 1000000UL;
99unsigned int normalized_sysctl_sched_wakeup_granularity = 1000000UL;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200100
Ingo Molnar2b4d5b22016-11-23 07:37:00 +0100101const_debug unsigned int sysctl_sched_migration_cost = 500000UL;
Ingo Molnarda84d962007-10-15 17:00:18 +0200102
Tim Chenafe06ef2016-11-22 12:23:53 -0800103#ifdef CONFIG_SMP
104/*
105 * For asym packing, by default the lower numbered cpu has higher priority.
106 */
107int __weak arch_asym_cpu_priority(int cpu)
108{
109 return -cpu;
110}
111#endif
112
Paul Turnerec12cb72011-07-21 09:43:30 -0700113#ifdef CONFIG_CFS_BANDWIDTH
114/*
115 * Amount of runtime to allocate from global (tg) to local (per-cfs_rq) pool
116 * each time a cfs_rq requests quota.
117 *
118 * Note: in the case that the slice exceeds the runtime remaining (either due
119 * to consumption or the quota being specified to be smaller than the slice)
120 * we will always only issue the remaining available time.
121 *
Ingo Molnar2b4d5b22016-11-23 07:37:00 +0100122 * (default: 5 msec, units: microseconds)
123 */
124unsigned int sysctl_sched_cfs_bandwidth_slice = 5000UL;
Paul Turnerec12cb72011-07-21 09:43:30 -0700125#endif
126
Morten Rasmussen32731632016-07-25 14:34:26 +0100127/*
128 * The margin used when comparing utilization with CPU capacity:
Morten Rasmussen893c5d22016-10-14 14:41:12 +0100129 * util * margin < capacity * 1024
Ingo Molnar2b4d5b22016-11-23 07:37:00 +0100130 *
131 * (default: ~20%)
Morten Rasmussen32731632016-07-25 14:34:26 +0100132 */
Ingo Molnar2b4d5b22016-11-23 07:37:00 +0100133unsigned int capacity_margin = 1280;
Morten Rasmussen32731632016-07-25 14:34:26 +0100134
Paul Gortmaker85276322013-04-19 15:10:50 -0400135static inline void update_load_add(struct load_weight *lw, unsigned long inc)
136{
137 lw->weight += inc;
138 lw->inv_weight = 0;
139}
140
141static inline void update_load_sub(struct load_weight *lw, unsigned long dec)
142{
143 lw->weight -= dec;
144 lw->inv_weight = 0;
145}
146
147static inline void update_load_set(struct load_weight *lw, unsigned long w)
148{
149 lw->weight = w;
150 lw->inv_weight = 0;
151}
152
Peter Zijlstra029632f2011-10-25 10:00:11 +0200153/*
154 * Increase the granularity value when there are more CPUs,
155 * because with more CPUs the 'effective latency' as visible
156 * to users decreases. But the relationship is not linear,
157 * so pick a second-best guess by going with the log2 of the
158 * number of CPUs.
159 *
160 * This idea comes from the SD scheduler of Con Kolivas:
161 */
Nicholas Mc Guire58ac93e2015-05-15 21:05:42 +0200162static unsigned int get_update_sysctl_factor(void)
Peter Zijlstra029632f2011-10-25 10:00:11 +0200163{
Nicholas Mc Guire58ac93e2015-05-15 21:05:42 +0200164 unsigned int cpus = min_t(unsigned int, num_online_cpus(), 8);
Peter Zijlstra029632f2011-10-25 10:00:11 +0200165 unsigned int factor;
166
167 switch (sysctl_sched_tunable_scaling) {
168 case SCHED_TUNABLESCALING_NONE:
169 factor = 1;
170 break;
171 case SCHED_TUNABLESCALING_LINEAR:
172 factor = cpus;
173 break;
174 case SCHED_TUNABLESCALING_LOG:
175 default:
176 factor = 1 + ilog2(cpus);
177 break;
178 }
179
180 return factor;
181}
182
183static void update_sysctl(void)
184{
185 unsigned int factor = get_update_sysctl_factor();
186
187#define SET_SYSCTL(name) \
188 (sysctl_##name = (factor) * normalized_sysctl_##name)
189 SET_SYSCTL(sched_min_granularity);
190 SET_SYSCTL(sched_latency);
191 SET_SYSCTL(sched_wakeup_granularity);
192#undef SET_SYSCTL
193}
194
195void sched_init_granularity(void)
196{
197 update_sysctl();
198}
199
Peter Zijlstra9dbdb152013-11-18 18:27:06 +0100200#define WMULT_CONST (~0U)
Peter Zijlstra029632f2011-10-25 10:00:11 +0200201#define WMULT_SHIFT 32
202
Peter Zijlstra9dbdb152013-11-18 18:27:06 +0100203static void __update_inv_weight(struct load_weight *lw)
Peter Zijlstra029632f2011-10-25 10:00:11 +0200204{
Peter Zijlstra9dbdb152013-11-18 18:27:06 +0100205 unsigned long w;
Peter Zijlstra029632f2011-10-25 10:00:11 +0200206
Peter Zijlstra9dbdb152013-11-18 18:27:06 +0100207 if (likely(lw->inv_weight))
208 return;
209
210 w = scale_load_down(lw->weight);
211
212 if (BITS_PER_LONG > 32 && unlikely(w >= WMULT_CONST))
213 lw->inv_weight = 1;
214 else if (unlikely(!w))
215 lw->inv_weight = WMULT_CONST;
Peter Zijlstra029632f2011-10-25 10:00:11 +0200216 else
Peter Zijlstra9dbdb152013-11-18 18:27:06 +0100217 lw->inv_weight = WMULT_CONST / w;
218}
Peter Zijlstra029632f2011-10-25 10:00:11 +0200219
Peter Zijlstra9dbdb152013-11-18 18:27:06 +0100220/*
221 * delta_exec * weight / lw.weight
222 * OR
223 * (delta_exec * (weight * lw->inv_weight)) >> WMULT_SHIFT
224 *
Yuyang Du1c3de5e2016-03-30 07:07:51 +0800225 * Either weight := NICE_0_LOAD and lw \e sched_prio_to_wmult[], in which case
Peter Zijlstra9dbdb152013-11-18 18:27:06 +0100226 * we're guaranteed shift stays positive because inv_weight is guaranteed to
227 * fit 32 bits, and NICE_0_LOAD gives another 10 bits; therefore shift >= 22.
228 *
229 * Or, weight =< lw.weight (because lw.weight is the runqueue weight), thus
230 * weight/lw.weight <= 1, and therefore our shift will also be positive.
231 */
232static u64 __calc_delta(u64 delta_exec, unsigned long weight, struct load_weight *lw)
233{
234 u64 fact = scale_load_down(weight);
235 int shift = WMULT_SHIFT;
Peter Zijlstra029632f2011-10-25 10:00:11 +0200236
Peter Zijlstra9dbdb152013-11-18 18:27:06 +0100237 __update_inv_weight(lw);
238
239 if (unlikely(fact >> 32)) {
240 while (fact >> 32) {
241 fact >>= 1;
242 shift--;
243 }
Peter Zijlstra029632f2011-10-25 10:00:11 +0200244 }
245
Peter Zijlstra9dbdb152013-11-18 18:27:06 +0100246 /* hint to use a 32x32->64 mul */
247 fact = (u64)(u32)fact * lw->inv_weight;
Peter Zijlstra029632f2011-10-25 10:00:11 +0200248
Peter Zijlstra9dbdb152013-11-18 18:27:06 +0100249 while (fact >> 32) {
250 fact >>= 1;
251 shift--;
252 }
253
254 return mul_u64_u32_shr(delta_exec, fact, shift);
Peter Zijlstra029632f2011-10-25 10:00:11 +0200255}
256
257
258const struct sched_class fair_sched_class;
Peter Zijlstraa4c2f002008-10-17 19:27:03 +0200259
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200260/**************************************************************
261 * CFS operations on generic schedulable entities:
262 */
263
264#ifdef CONFIG_FAIR_GROUP_SCHED
265
266/* cpu runqueue to which this cfs_rq is attached */
267static inline struct rq *rq_of(struct cfs_rq *cfs_rq)
268{
269 return cfs_rq->rq;
270}
271
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200272/* An entity is a task if it doesn't "own" a runqueue */
273#define entity_is_task(se) (!se->my_q)
274
Peter Zijlstra8f488942009-07-24 12:25:30 +0200275static inline struct task_struct *task_of(struct sched_entity *se)
276{
Peter Zijlstra9148a3a2016-09-20 22:34:51 +0200277 SCHED_WARN_ON(!entity_is_task(se));
Peter Zijlstra8f488942009-07-24 12:25:30 +0200278 return container_of(se, struct task_struct, se);
279}
280
Peter Zijlstrab7581492008-04-19 19:45:00 +0200281/* Walk up scheduling entities hierarchy */
282#define for_each_sched_entity(se) \
283 for (; se; se = se->parent)
284
285static inline struct cfs_rq *task_cfs_rq(struct task_struct *p)
286{
287 return p->se.cfs_rq;
288}
289
290/* runqueue on which this entity is (to be) queued */
291static inline struct cfs_rq *cfs_rq_of(struct sched_entity *se)
292{
293 return se->cfs_rq;
294}
295
296/* runqueue "owned" by this group */
297static inline struct cfs_rq *group_cfs_rq(struct sched_entity *grp)
298{
299 return grp->my_q;
300}
301
Peter Zijlstra3d4b47b2010-11-15 15:47:01 -0800302static inline void list_add_leaf_cfs_rq(struct cfs_rq *cfs_rq)
303{
304 if (!cfs_rq->on_list) {
Vincent Guittot9c2791f2016-11-08 10:53:43 +0100305 struct rq *rq = rq_of(cfs_rq);
306 int cpu = cpu_of(rq);
Paul Turner67e86252010-11-15 15:47:05 -0800307 /*
308 * Ensure we either appear before our parent (if already
309 * enqueued) or force our parent to appear after us when it is
Vincent Guittot9c2791f2016-11-08 10:53:43 +0100310 * enqueued. The fact that we always enqueue bottom-up
311 * reduces this to two cases and a special case for the root
312 * cfs_rq. Furthermore, it also means that we will always reset
313 * tmp_alone_branch either when the branch is connected
314 * to a tree or when we reach the beg of the tree
Paul Turner67e86252010-11-15 15:47:05 -0800315 */
316 if (cfs_rq->tg->parent &&
Vincent Guittot9c2791f2016-11-08 10:53:43 +0100317 cfs_rq->tg->parent->cfs_rq[cpu]->on_list) {
318 /*
319 * If parent is already on the list, we add the child
320 * just before. Thanks to circular linked property of
321 * the list, this means to put the child at the tail
322 * of the list that starts by parent.
323 */
Paul Turner67e86252010-11-15 15:47:05 -0800324 list_add_tail_rcu(&cfs_rq->leaf_cfs_rq_list,
Vincent Guittot9c2791f2016-11-08 10:53:43 +0100325 &(cfs_rq->tg->parent->cfs_rq[cpu]->leaf_cfs_rq_list));
326 /*
327 * The branch is now connected to its tree so we can
328 * reset tmp_alone_branch to the beginning of the
329 * list.
330 */
331 rq->tmp_alone_branch = &rq->leaf_cfs_rq_list;
332 } else if (!cfs_rq->tg->parent) {
333 /*
334 * cfs rq without parent should be put
335 * at the tail of the list.
336 */
337 list_add_tail_rcu(&cfs_rq->leaf_cfs_rq_list,
338 &rq->leaf_cfs_rq_list);
339 /*
340 * We have reach the beg of a tree so we can reset
341 * tmp_alone_branch to the beginning of the list.
342 */
343 rq->tmp_alone_branch = &rq->leaf_cfs_rq_list;
344 } else {
345 /*
346 * The parent has not already been added so we want to
347 * make sure that it will be put after us.
348 * tmp_alone_branch points to the beg of the branch
349 * where we will add parent.
350 */
351 list_add_rcu(&cfs_rq->leaf_cfs_rq_list,
352 rq->tmp_alone_branch);
353 /*
354 * update tmp_alone_branch to points to the new beg
355 * of the branch
356 */
357 rq->tmp_alone_branch = &cfs_rq->leaf_cfs_rq_list;
Paul Turner67e86252010-11-15 15:47:05 -0800358 }
Peter Zijlstra3d4b47b2010-11-15 15:47:01 -0800359
360 cfs_rq->on_list = 1;
361 }
362}
363
364static inline void list_del_leaf_cfs_rq(struct cfs_rq *cfs_rq)
365{
366 if (cfs_rq->on_list) {
367 list_del_rcu(&cfs_rq->leaf_cfs_rq_list);
368 cfs_rq->on_list = 0;
369 }
370}
371
Peter Zijlstrab7581492008-04-19 19:45:00 +0200372/* Iterate thr' all leaf cfs_rq's on a runqueue */
Tejun Heoa9e7f652017-04-25 17:43:50 -0700373#define for_each_leaf_cfs_rq_safe(rq, cfs_rq, pos) \
374 list_for_each_entry_safe(cfs_rq, pos, &rq->leaf_cfs_rq_list, \
375 leaf_cfs_rq_list)
Peter Zijlstrab7581492008-04-19 19:45:00 +0200376
377/* Do the two (enqueued) entities belong to the same group ? */
Peter Zijlstrafed14d42012-02-11 06:05:00 +0100378static inline struct cfs_rq *
Peter Zijlstrab7581492008-04-19 19:45:00 +0200379is_same_group(struct sched_entity *se, struct sched_entity *pse)
380{
381 if (se->cfs_rq == pse->cfs_rq)
Peter Zijlstrafed14d42012-02-11 06:05:00 +0100382 return se->cfs_rq;
Peter Zijlstrab7581492008-04-19 19:45:00 +0200383
Peter Zijlstrafed14d42012-02-11 06:05:00 +0100384 return NULL;
Peter Zijlstrab7581492008-04-19 19:45:00 +0200385}
386
387static inline struct sched_entity *parent_entity(struct sched_entity *se)
388{
389 return se->parent;
390}
391
Peter Zijlstra464b7522008-10-24 11:06:15 +0200392static void
393find_matching_se(struct sched_entity **se, struct sched_entity **pse)
394{
395 int se_depth, pse_depth;
396
397 /*
398 * preemption test can be made between sibling entities who are in the
399 * same cfs_rq i.e who have a common parent. Walk up the hierarchy of
400 * both tasks until we find their ancestors who are siblings of common
401 * parent.
402 */
403
404 /* First walk up until both entities are at same depth */
Peter Zijlstrafed14d42012-02-11 06:05:00 +0100405 se_depth = (*se)->depth;
406 pse_depth = (*pse)->depth;
Peter Zijlstra464b7522008-10-24 11:06:15 +0200407
408 while (se_depth > pse_depth) {
409 se_depth--;
410 *se = parent_entity(*se);
411 }
412
413 while (pse_depth > se_depth) {
414 pse_depth--;
415 *pse = parent_entity(*pse);
416 }
417
418 while (!is_same_group(*se, *pse)) {
419 *se = parent_entity(*se);
420 *pse = parent_entity(*pse);
421 }
422}
423
Peter Zijlstra8f488942009-07-24 12:25:30 +0200424#else /* !CONFIG_FAIR_GROUP_SCHED */
425
426static inline struct task_struct *task_of(struct sched_entity *se)
427{
428 return container_of(se, struct task_struct, se);
429}
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200430
431static inline struct rq *rq_of(struct cfs_rq *cfs_rq)
432{
433 return container_of(cfs_rq, struct rq, cfs);
434}
435
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200436#define entity_is_task(se) 1
437
Peter Zijlstrab7581492008-04-19 19:45:00 +0200438#define for_each_sched_entity(se) \
439 for (; se; se = NULL)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200440
Peter Zijlstrab7581492008-04-19 19:45:00 +0200441static inline struct cfs_rq *task_cfs_rq(struct task_struct *p)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200442{
Peter Zijlstrab7581492008-04-19 19:45:00 +0200443 return &task_rq(p)->cfs;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200444}
445
Peter Zijlstrab7581492008-04-19 19:45:00 +0200446static inline struct cfs_rq *cfs_rq_of(struct sched_entity *se)
447{
448 struct task_struct *p = task_of(se);
449 struct rq *rq = task_rq(p);
450
451 return &rq->cfs;
452}
453
454/* runqueue "owned" by this group */
455static inline struct cfs_rq *group_cfs_rq(struct sched_entity *grp)
456{
457 return NULL;
458}
459
Peter Zijlstra3d4b47b2010-11-15 15:47:01 -0800460static inline void list_add_leaf_cfs_rq(struct cfs_rq *cfs_rq)
461{
462}
463
464static inline void list_del_leaf_cfs_rq(struct cfs_rq *cfs_rq)
465{
466}
467
Tejun Heoa9e7f652017-04-25 17:43:50 -0700468#define for_each_leaf_cfs_rq_safe(rq, cfs_rq, pos) \
469 for (cfs_rq = &rq->cfs, pos = NULL; cfs_rq; cfs_rq = pos)
Peter Zijlstrab7581492008-04-19 19:45:00 +0200470
Peter Zijlstrab7581492008-04-19 19:45:00 +0200471static inline struct sched_entity *parent_entity(struct sched_entity *se)
472{
473 return NULL;
474}
475
Peter Zijlstra464b7522008-10-24 11:06:15 +0200476static inline void
477find_matching_se(struct sched_entity **se, struct sched_entity **pse)
478{
479}
480
Peter Zijlstrab7581492008-04-19 19:45:00 +0200481#endif /* CONFIG_FAIR_GROUP_SCHED */
482
Peter Zijlstra6c16a6d2012-03-21 13:07:16 -0700483static __always_inline
Peter Zijlstra9dbdb152013-11-18 18:27:06 +0100484void account_cfs_rq_runtime(struct cfs_rq *cfs_rq, u64 delta_exec);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200485
486/**************************************************************
487 * Scheduling class tree data structure manipulation methods:
488 */
489
Andrei Epure1bf08232013-03-12 21:12:24 +0200490static inline u64 max_vruntime(u64 max_vruntime, u64 vruntime)
Peter Zijlstra02e04312007-10-15 17:00:07 +0200491{
Andrei Epure1bf08232013-03-12 21:12:24 +0200492 s64 delta = (s64)(vruntime - max_vruntime);
Peter Zijlstra368059a2007-10-15 17:00:11 +0200493 if (delta > 0)
Andrei Epure1bf08232013-03-12 21:12:24 +0200494 max_vruntime = vruntime;
Peter Zijlstra02e04312007-10-15 17:00:07 +0200495
Andrei Epure1bf08232013-03-12 21:12:24 +0200496 return max_vruntime;
Peter Zijlstra02e04312007-10-15 17:00:07 +0200497}
498
Ingo Molnar0702e3e2007-10-15 17:00:14 +0200499static inline u64 min_vruntime(u64 min_vruntime, u64 vruntime)
Peter Zijlstrab0ffd242007-10-15 17:00:12 +0200500{
501 s64 delta = (s64)(vruntime - min_vruntime);
502 if (delta < 0)
503 min_vruntime = vruntime;
504
505 return min_vruntime;
506}
507
Fabio Checconi54fdc582009-07-16 12:32:27 +0200508static inline int entity_before(struct sched_entity *a,
509 struct sched_entity *b)
510{
511 return (s64)(a->vruntime - b->vruntime) < 0;
512}
513
Peter Zijlstra1af5f732008-10-24 11:06:13 +0200514static void update_min_vruntime(struct cfs_rq *cfs_rq)
515{
Peter Zijlstrab60205c2016-09-20 21:58:12 +0200516 struct sched_entity *curr = cfs_rq->curr;
Davidlohr Buesobfb06882017-09-08 16:14:55 -0700517 struct rb_node *leftmost = rb_first_cached(&cfs_rq->tasks_timeline);
Peter Zijlstrab60205c2016-09-20 21:58:12 +0200518
Peter Zijlstra1af5f732008-10-24 11:06:13 +0200519 u64 vruntime = cfs_rq->min_vruntime;
520
Peter Zijlstrab60205c2016-09-20 21:58:12 +0200521 if (curr) {
522 if (curr->on_rq)
523 vruntime = curr->vruntime;
524 else
525 curr = NULL;
526 }
Peter Zijlstra1af5f732008-10-24 11:06:13 +0200527
Davidlohr Buesobfb06882017-09-08 16:14:55 -0700528 if (leftmost) { /* non-empty tree */
529 struct sched_entity *se;
530 se = rb_entry(leftmost, struct sched_entity, run_node);
Peter Zijlstra1af5f732008-10-24 11:06:13 +0200531
Peter Zijlstrab60205c2016-09-20 21:58:12 +0200532 if (!curr)
Peter Zijlstra1af5f732008-10-24 11:06:13 +0200533 vruntime = se->vruntime;
534 else
535 vruntime = min_vruntime(vruntime, se->vruntime);
536 }
537
Andrei Epure1bf08232013-03-12 21:12:24 +0200538 /* ensure we never gain time by being placed backwards. */
Peter Zijlstra1af5f732008-10-24 11:06:13 +0200539 cfs_rq->min_vruntime = max_vruntime(cfs_rq->min_vruntime, vruntime);
Peter Zijlstra3fe16982011-04-05 17:23:48 +0200540#ifndef CONFIG_64BIT
541 smp_wmb();
542 cfs_rq->min_vruntime_copy = cfs_rq->min_vruntime;
543#endif
Peter Zijlstra1af5f732008-10-24 11:06:13 +0200544}
545
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200546/*
547 * Enqueue an entity into the rb-tree:
548 */
Ingo Molnar0702e3e2007-10-15 17:00:14 +0200549static void __enqueue_entity(struct cfs_rq *cfs_rq, struct sched_entity *se)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200550{
Davidlohr Buesobfb06882017-09-08 16:14:55 -0700551 struct rb_node **link = &cfs_rq->tasks_timeline.rb_root.rb_node;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200552 struct rb_node *parent = NULL;
553 struct sched_entity *entry;
Davidlohr Buesobfb06882017-09-08 16:14:55 -0700554 bool leftmost = true;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200555
556 /*
557 * Find the right place in the rbtree:
558 */
559 while (*link) {
560 parent = *link;
561 entry = rb_entry(parent, struct sched_entity, run_node);
562 /*
563 * We dont care about collisions. Nodes with
564 * the same key stay together.
565 */
Stephan Baerwolf2bd2d6f2011-07-20 14:46:59 +0200566 if (entity_before(se, entry)) {
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200567 link = &parent->rb_left;
568 } else {
569 link = &parent->rb_right;
Davidlohr Buesobfb06882017-09-08 16:14:55 -0700570 leftmost = false;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200571 }
572 }
573
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200574 rb_link_node(&se->run_node, parent, link);
Davidlohr Buesobfb06882017-09-08 16:14:55 -0700575 rb_insert_color_cached(&se->run_node,
576 &cfs_rq->tasks_timeline, leftmost);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200577}
578
Ingo Molnar0702e3e2007-10-15 17:00:14 +0200579static void __dequeue_entity(struct cfs_rq *cfs_rq, struct sched_entity *se)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200580{
Davidlohr Buesobfb06882017-09-08 16:14:55 -0700581 rb_erase_cached(&se->run_node, &cfs_rq->tasks_timeline);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200582}
583
Peter Zijlstra029632f2011-10-25 10:00:11 +0200584struct sched_entity *__pick_first_entity(struct cfs_rq *cfs_rq)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200585{
Davidlohr Buesobfb06882017-09-08 16:14:55 -0700586 struct rb_node *left = rb_first_cached(&cfs_rq->tasks_timeline);
Peter Zijlstraf4b67552008-11-04 21:25:07 +0100587
588 if (!left)
589 return NULL;
590
591 return rb_entry(left, struct sched_entity, run_node);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200592}
593
Rik van Rielac53db52011-02-01 09:51:03 -0500594static struct sched_entity *__pick_next_entity(struct sched_entity *se)
595{
596 struct rb_node *next = rb_next(&se->run_node);
597
598 if (!next)
599 return NULL;
600
601 return rb_entry(next, struct sched_entity, run_node);
602}
603
604#ifdef CONFIG_SCHED_DEBUG
Peter Zijlstra029632f2011-10-25 10:00:11 +0200605struct sched_entity *__pick_last_entity(struct cfs_rq *cfs_rq)
Peter Zijlstraaeb73b02007-10-15 17:00:05 +0200606{
Davidlohr Buesobfb06882017-09-08 16:14:55 -0700607 struct rb_node *last = rb_last(&cfs_rq->tasks_timeline.rb_root);
Peter Zijlstraaeb73b02007-10-15 17:00:05 +0200608
Balbir Singh70eee742008-02-22 13:25:53 +0530609 if (!last)
610 return NULL;
Ingo Molnar7eee3e62008-02-22 10:32:21 +0100611
612 return rb_entry(last, struct sched_entity, run_node);
Peter Zijlstraaeb73b02007-10-15 17:00:05 +0200613}
614
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200615/**************************************************************
616 * Scheduling class statistics methods:
617 */
618
Christian Ehrhardtacb4a842009-11-30 12:16:48 +0100619int sched_proc_update_handler(struct ctl_table *table, int write,
Alexey Dobriyan8d65af72009-09-23 15:57:19 -0700620 void __user *buffer, size_t *lenp,
Peter Zijlstrab2be5e92007-11-09 22:39:37 +0100621 loff_t *ppos)
622{
Alexey Dobriyan8d65af72009-09-23 15:57:19 -0700623 int ret = proc_dointvec_minmax(table, write, buffer, lenp, ppos);
Nicholas Mc Guire58ac93e2015-05-15 21:05:42 +0200624 unsigned int factor = get_update_sysctl_factor();
Peter Zijlstrab2be5e92007-11-09 22:39:37 +0100625
626 if (ret || !write)
627 return ret;
628
629 sched_nr_latency = DIV_ROUND_UP(sysctl_sched_latency,
630 sysctl_sched_min_granularity);
631
Christian Ehrhardtacb4a842009-11-30 12:16:48 +0100632#define WRT_SYSCTL(name) \
633 (normalized_sysctl_##name = sysctl_##name / (factor))
634 WRT_SYSCTL(sched_min_granularity);
635 WRT_SYSCTL(sched_latency);
636 WRT_SYSCTL(sched_wakeup_granularity);
Christian Ehrhardtacb4a842009-11-30 12:16:48 +0100637#undef WRT_SYSCTL
638
Peter Zijlstrab2be5e92007-11-09 22:39:37 +0100639 return 0;
640}
641#endif
Ingo Molnar647e7ca2007-10-15 17:00:13 +0200642
643/*
Peter Zijlstraf9c0b092008-10-17 19:27:04 +0200644 * delta /= w
Peter Zijlstraa7be37a2008-06-27 13:41:11 +0200645 */
Peter Zijlstra9dbdb152013-11-18 18:27:06 +0100646static inline u64 calc_delta_fair(u64 delta, struct sched_entity *se)
Peter Zijlstraa7be37a2008-06-27 13:41:11 +0200647{
Peter Zijlstraf9c0b092008-10-17 19:27:04 +0200648 if (unlikely(se->load.weight != NICE_0_LOAD))
Peter Zijlstra9dbdb152013-11-18 18:27:06 +0100649 delta = __calc_delta(delta, NICE_0_LOAD, &se->load);
Peter Zijlstraa7be37a2008-06-27 13:41:11 +0200650
651 return delta;
652}
653
654/*
Ingo Molnar647e7ca2007-10-15 17:00:13 +0200655 * The idea is to set a period in which each task runs once.
656 *
Borislav Petkov532b1852012-08-08 16:16:04 +0200657 * When there are too many tasks (sched_nr_latency) we have to stretch
Ingo Molnar647e7ca2007-10-15 17:00:13 +0200658 * this period because otherwise the slices get too small.
659 *
660 * p = (nr <= nl) ? l : l*nr/nl
661 */
Peter Zijlstra4d78e7b2007-10-15 17:00:04 +0200662static u64 __sched_period(unsigned long nr_running)
663{
Boqun Feng8e2b0bf2015-07-02 22:25:52 +0800664 if (unlikely(nr_running > sched_nr_latency))
665 return nr_running * sysctl_sched_min_granularity;
666 else
667 return sysctl_sched_latency;
Peter Zijlstra4d78e7b2007-10-15 17:00:04 +0200668}
669
Ingo Molnar647e7ca2007-10-15 17:00:13 +0200670/*
671 * We calculate the wall-time slice from the period by taking a part
672 * proportional to the weight.
673 *
Peter Zijlstraf9c0b092008-10-17 19:27:04 +0200674 * s = p*P[w/rw]
Ingo Molnar647e7ca2007-10-15 17:00:13 +0200675 */
Peter Zijlstra6d0f0eb2007-10-15 17:00:05 +0200676static u64 sched_slice(struct cfs_rq *cfs_rq, struct sched_entity *se)
Peter Zijlstra21805082007-08-25 18:41:53 +0200677{
Mike Galbraith0a582442009-01-02 12:16:42 +0100678 u64 slice = __sched_period(cfs_rq->nr_running + !se->on_rq);
Peter Zijlstraf9c0b092008-10-17 19:27:04 +0200679
Mike Galbraith0a582442009-01-02 12:16:42 +0100680 for_each_sched_entity(se) {
Lin Ming6272d682009-01-15 17:17:15 +0100681 struct load_weight *load;
Christian Engelmayer3104bf02009-06-16 10:35:12 +0200682 struct load_weight lw;
Lin Ming6272d682009-01-15 17:17:15 +0100683
684 cfs_rq = cfs_rq_of(se);
685 load = &cfs_rq->load;
Peter Zijlstraf9c0b092008-10-17 19:27:04 +0200686
Mike Galbraith0a582442009-01-02 12:16:42 +0100687 if (unlikely(!se->on_rq)) {
Christian Engelmayer3104bf02009-06-16 10:35:12 +0200688 lw = cfs_rq->load;
Mike Galbraith0a582442009-01-02 12:16:42 +0100689
690 update_load_add(&lw, se->load.weight);
691 load = &lw;
692 }
Peter Zijlstra9dbdb152013-11-18 18:27:06 +0100693 slice = __calc_delta(slice, se->load.weight, load);
Mike Galbraith0a582442009-01-02 12:16:42 +0100694 }
695 return slice;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200696}
697
Ingo Molnar647e7ca2007-10-15 17:00:13 +0200698/*
Andrei Epure660cc002013-03-11 12:03:20 +0200699 * We calculate the vruntime slice of a to-be-inserted task.
Ingo Molnar647e7ca2007-10-15 17:00:13 +0200700 *
Peter Zijlstraf9c0b092008-10-17 19:27:04 +0200701 * vs = s/w
Ingo Molnar647e7ca2007-10-15 17:00:13 +0200702 */
Peter Zijlstraf9c0b092008-10-17 19:27:04 +0200703static u64 sched_vslice(struct cfs_rq *cfs_rq, struct sched_entity *se)
Ingo Molnar647e7ca2007-10-15 17:00:13 +0200704{
Peter Zijlstraf9c0b092008-10-17 19:27:04 +0200705 return calc_delta_fair(sched_slice(cfs_rq, se), se);
Peter Zijlstraa7be37a2008-06-27 13:41:11 +0200706}
707
Alex Shia75cdaa2013-06-20 10:18:47 +0800708#ifdef CONFIG_SMP
Peter Zijlstra283e2ed2017-04-11 11:08:42 +0200709
710#include "sched-pelt.h"
711
Morten Rasmussen772bd008c2016-06-22 18:03:13 +0100712static int select_idle_sibling(struct task_struct *p, int prev_cpu, int cpu);
Mel Gormanfb13c7e2013-10-07 11:29:17 +0100713static unsigned long task_h_load(struct task_struct *p);
714
Yuyang Du540247f2015-07-15 08:04:39 +0800715/* Give new sched_entity start runnable values to heavy its load in infant time */
716void init_entity_runnable_average(struct sched_entity *se)
Alex Shia75cdaa2013-06-20 10:18:47 +0800717{
Yuyang Du540247f2015-07-15 08:04:39 +0800718 struct sched_avg *sa = &se->avg;
Alex Shia75cdaa2013-06-20 10:18:47 +0800719
Yuyang Du9d89c252015-07-15 08:04:37 +0800720 sa->last_update_time = 0;
721 /*
722 * sched_avg's period_contrib should be strictly less then 1024, so
723 * we give it 1023 to make sure it is almost a period (1024us), and
724 * will definitely be update (after enqueue).
725 */
726 sa->period_contrib = 1023;
Vincent Guittotb5a9b342016-10-19 14:45:23 +0200727 /*
728 * Tasks are intialized with full load to be seen as heavy tasks until
729 * they get a chance to stabilize to their real load level.
730 * Group entities are intialized with zero load to reflect the fact that
731 * nothing has been attached to the task group yet.
732 */
733 if (entity_is_task(se))
734 sa->load_avg = scale_load_down(se->load.weight);
Yuyang Du9d89c252015-07-15 08:04:37 +0800735 sa->load_sum = sa->load_avg * LOAD_AVG_MAX;
Yuyang Du2b8c41d2016-03-30 04:30:56 +0800736 /*
737 * At this point, util_avg won't be used in select_task_rq_fair anyway
738 */
739 sa->util_avg = 0;
740 sa->util_sum = 0;
Yuyang Du9d89c252015-07-15 08:04:37 +0800741 /* when this task enqueue'ed, it will contribute to its cfs_rq's load_avg */
Alex Shia75cdaa2013-06-20 10:18:47 +0800742}
Yuyang Du7ea241a2015-07-15 08:04:42 +0800743
Peter Zijlstra7dc603c2016-06-16 13:29:28 +0200744static inline u64 cfs_rq_clock_task(struct cfs_rq *cfs_rq);
Vincent Guittotdf217912016-11-08 10:53:42 +0100745static void attach_entity_cfs_rq(struct sched_entity *se);
Peter Zijlstra7dc603c2016-06-16 13:29:28 +0200746
Yuyang Du2b8c41d2016-03-30 04:30:56 +0800747/*
748 * With new tasks being created, their initial util_avgs are extrapolated
749 * based on the cfs_rq's current util_avg:
750 *
751 * util_avg = cfs_rq->util_avg / (cfs_rq->load_avg + 1) * se.load.weight
752 *
753 * However, in many cases, the above util_avg does not give a desired
754 * value. Moreover, the sum of the util_avgs may be divergent, such
755 * as when the series is a harmonic series.
756 *
757 * To solve this problem, we also cap the util_avg of successive tasks to
758 * only 1/2 of the left utilization budget:
759 *
760 * util_avg_cap = (1024 - cfs_rq->avg.util_avg) / 2^n
761 *
762 * where n denotes the nth task.
763 *
764 * For example, a simplest series from the beginning would be like:
765 *
766 * task util_avg: 512, 256, 128, 64, 32, 16, 8, ...
767 * cfs_rq util_avg: 512, 768, 896, 960, 992, 1008, 1016, ...
768 *
769 * Finally, that extrapolated util_avg is clamped to the cap (util_avg_cap)
770 * if util_avg > util_avg_cap.
771 */
772void post_init_entity_util_avg(struct sched_entity *se)
773{
774 struct cfs_rq *cfs_rq = cfs_rq_of(se);
775 struct sched_avg *sa = &se->avg;
Yuyang Du172895e2016-04-05 12:12:27 +0800776 long cap = (long)(SCHED_CAPACITY_SCALE - cfs_rq->avg.util_avg) / 2;
Yuyang Du2b8c41d2016-03-30 04:30:56 +0800777
778 if (cap > 0) {
779 if (cfs_rq->avg.util_avg != 0) {
780 sa->util_avg = cfs_rq->avg.util_avg * se->load.weight;
781 sa->util_avg /= (cfs_rq->avg.load_avg + 1);
782
783 if (sa->util_avg > cap)
784 sa->util_avg = cap;
785 } else {
786 sa->util_avg = cap;
787 }
788 sa->util_sum = sa->util_avg * LOAD_AVG_MAX;
789 }
Peter Zijlstra7dc603c2016-06-16 13:29:28 +0200790
791 if (entity_is_task(se)) {
792 struct task_struct *p = task_of(se);
793 if (p->sched_class != &fair_sched_class) {
794 /*
795 * For !fair tasks do:
796 *
Viresh Kumar3a123bb2017-05-24 10:59:56 +0530797 update_cfs_rq_load_avg(now, cfs_rq);
Peter Zijlstra7dc603c2016-06-16 13:29:28 +0200798 attach_entity_load_avg(cfs_rq, se);
799 switched_from_fair(rq, p);
800 *
801 * such that the next switched_to_fair() has the
802 * expected state.
803 */
Vincent Guittotdf217912016-11-08 10:53:42 +0100804 se->avg.last_update_time = cfs_rq_clock_task(cfs_rq);
Peter Zijlstra7dc603c2016-06-16 13:29:28 +0200805 return;
806 }
807 }
808
Vincent Guittotdf217912016-11-08 10:53:42 +0100809 attach_entity_cfs_rq(se);
Yuyang Du2b8c41d2016-03-30 04:30:56 +0800810}
811
Peter Zijlstra7dc603c2016-06-16 13:29:28 +0200812#else /* !CONFIG_SMP */
Yuyang Du540247f2015-07-15 08:04:39 +0800813void init_entity_runnable_average(struct sched_entity *se)
Alex Shia75cdaa2013-06-20 10:18:47 +0800814{
815}
Yuyang Du2b8c41d2016-03-30 04:30:56 +0800816void post_init_entity_util_avg(struct sched_entity *se)
817{
818}
Peter Zijlstra3d30544f2016-06-21 14:27:50 +0200819static void update_tg_load_avg(struct cfs_rq *cfs_rq, int force)
820{
821}
Peter Zijlstra7dc603c2016-06-16 13:29:28 +0200822#endif /* CONFIG_SMP */
Alex Shia75cdaa2013-06-20 10:18:47 +0800823
Peter Zijlstraa7be37a2008-06-27 13:41:11 +0200824/*
Peter Zijlstra9dbdb152013-11-18 18:27:06 +0100825 * Update the current task's runtime statistics.
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200826 */
Ingo Molnarb7cc0892007-08-09 11:16:47 +0200827static void update_curr(struct cfs_rq *cfs_rq)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200828{
Ingo Molnar429d43b2007-10-15 17:00:03 +0200829 struct sched_entity *curr = cfs_rq->curr;
Frederic Weisbecker78becc22013-04-12 01:51:02 +0200830 u64 now = rq_clock_task(rq_of(cfs_rq));
Peter Zijlstra9dbdb152013-11-18 18:27:06 +0100831 u64 delta_exec;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200832
833 if (unlikely(!curr))
834 return;
835
Peter Zijlstra9dbdb152013-11-18 18:27:06 +0100836 delta_exec = now - curr->exec_start;
837 if (unlikely((s64)delta_exec <= 0))
Peter Zijlstra34f28ec2008-12-16 08:45:31 +0100838 return;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200839
Ingo Molnar8ebc91d2007-10-15 17:00:03 +0200840 curr->exec_start = now;
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +0100841
Peter Zijlstra9dbdb152013-11-18 18:27:06 +0100842 schedstat_set(curr->statistics.exec_max,
843 max(delta_exec, curr->statistics.exec_max));
844
845 curr->sum_exec_runtime += delta_exec;
Josh Poimboeufae928822016-06-17 12:43:24 -0500846 schedstat_add(cfs_rq->exec_clock, delta_exec);
Peter Zijlstra9dbdb152013-11-18 18:27:06 +0100847
848 curr->vruntime += calc_delta_fair(delta_exec, curr);
849 update_min_vruntime(cfs_rq);
850
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +0100851 if (entity_is_task(curr)) {
852 struct task_struct *curtask = task_of(curr);
853
Ingo Molnarf977bb42009-09-13 18:15:54 +0200854 trace_sched_stat_runtime(curtask, delta_exec, curr->vruntime);
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +0100855 cpuacct_charge(curtask, delta_exec);
Frank Mayharf06febc2008-09-12 09:54:39 -0700856 account_group_exec_runtime(curtask, delta_exec);
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +0100857 }
Paul Turnerec12cb72011-07-21 09:43:30 -0700858
859 account_cfs_rq_runtime(cfs_rq, delta_exec);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200860}
861
Stanislaw Gruszka6e998912014-11-12 16:58:44 +0100862static void update_curr_fair(struct rq *rq)
863{
864 update_curr(cfs_rq_of(&rq->curr->se));
865}
866
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200867static inline void
Ingo Molnar5870db52007-08-09 11:16:47 +0200868update_stats_wait_start(struct cfs_rq *cfs_rq, struct sched_entity *se)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200869{
Josh Poimboeuf4fa8d292016-06-17 12:43:26 -0500870 u64 wait_start, prev_wait_start;
871
872 if (!schedstat_enabled())
873 return;
874
875 wait_start = rq_clock(rq_of(cfs_rq));
876 prev_wait_start = schedstat_val(se->statistics.wait_start);
Joonwoo Park3ea94de2015-11-12 19:38:54 -0800877
878 if (entity_is_task(se) && task_on_rq_migrating(task_of(se)) &&
Josh Poimboeuf4fa8d292016-06-17 12:43:26 -0500879 likely(wait_start > prev_wait_start))
880 wait_start -= prev_wait_start;
Joonwoo Park3ea94de2015-11-12 19:38:54 -0800881
Josh Poimboeuf4fa8d292016-06-17 12:43:26 -0500882 schedstat_set(se->statistics.wait_start, wait_start);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200883}
884
Josh Poimboeuf4fa8d292016-06-17 12:43:26 -0500885static inline void
Joonwoo Park3ea94de2015-11-12 19:38:54 -0800886update_stats_wait_end(struct cfs_rq *cfs_rq, struct sched_entity *se)
887{
888 struct task_struct *p;
Mel Gormancb251762016-02-05 09:08:36 +0000889 u64 delta;
890
Josh Poimboeuf4fa8d292016-06-17 12:43:26 -0500891 if (!schedstat_enabled())
892 return;
893
894 delta = rq_clock(rq_of(cfs_rq)) - schedstat_val(se->statistics.wait_start);
Joonwoo Park3ea94de2015-11-12 19:38:54 -0800895
896 if (entity_is_task(se)) {
897 p = task_of(se);
898 if (task_on_rq_migrating(p)) {
899 /*
900 * Preserve migrating task's wait time so wait_start
901 * time stamp can be adjusted to accumulate wait time
902 * prior to migration.
903 */
Josh Poimboeuf4fa8d292016-06-17 12:43:26 -0500904 schedstat_set(se->statistics.wait_start, delta);
Joonwoo Park3ea94de2015-11-12 19:38:54 -0800905 return;
906 }
907 trace_sched_stat_wait(p, delta);
908 }
909
Josh Poimboeuf4fa8d292016-06-17 12:43:26 -0500910 schedstat_set(se->statistics.wait_max,
911 max(schedstat_val(se->statistics.wait_max), delta));
912 schedstat_inc(se->statistics.wait_count);
913 schedstat_add(se->statistics.wait_sum, delta);
914 schedstat_set(se->statistics.wait_start, 0);
Joonwoo Park3ea94de2015-11-12 19:38:54 -0800915}
Joonwoo Park3ea94de2015-11-12 19:38:54 -0800916
Josh Poimboeuf4fa8d292016-06-17 12:43:26 -0500917static inline void
Josh Poimboeuf1a3d0272016-06-17 12:43:23 -0500918update_stats_enqueue_sleeper(struct cfs_rq *cfs_rq, struct sched_entity *se)
919{
920 struct task_struct *tsk = NULL;
Josh Poimboeuf4fa8d292016-06-17 12:43:26 -0500921 u64 sleep_start, block_start;
922
923 if (!schedstat_enabled())
924 return;
925
926 sleep_start = schedstat_val(se->statistics.sleep_start);
927 block_start = schedstat_val(se->statistics.block_start);
Josh Poimboeuf1a3d0272016-06-17 12:43:23 -0500928
929 if (entity_is_task(se))
930 tsk = task_of(se);
931
Josh Poimboeuf4fa8d292016-06-17 12:43:26 -0500932 if (sleep_start) {
933 u64 delta = rq_clock(rq_of(cfs_rq)) - sleep_start;
Josh Poimboeuf1a3d0272016-06-17 12:43:23 -0500934
935 if ((s64)delta < 0)
936 delta = 0;
937
Josh Poimboeuf4fa8d292016-06-17 12:43:26 -0500938 if (unlikely(delta > schedstat_val(se->statistics.sleep_max)))
939 schedstat_set(se->statistics.sleep_max, delta);
Josh Poimboeuf1a3d0272016-06-17 12:43:23 -0500940
Josh Poimboeuf4fa8d292016-06-17 12:43:26 -0500941 schedstat_set(se->statistics.sleep_start, 0);
942 schedstat_add(se->statistics.sum_sleep_runtime, delta);
Josh Poimboeuf1a3d0272016-06-17 12:43:23 -0500943
944 if (tsk) {
945 account_scheduler_latency(tsk, delta >> 10, 1);
946 trace_sched_stat_sleep(tsk, delta);
947 }
948 }
Josh Poimboeuf4fa8d292016-06-17 12:43:26 -0500949 if (block_start) {
950 u64 delta = rq_clock(rq_of(cfs_rq)) - block_start;
Josh Poimboeuf1a3d0272016-06-17 12:43:23 -0500951
952 if ((s64)delta < 0)
953 delta = 0;
954
Josh Poimboeuf4fa8d292016-06-17 12:43:26 -0500955 if (unlikely(delta > schedstat_val(se->statistics.block_max)))
956 schedstat_set(se->statistics.block_max, delta);
Josh Poimboeuf1a3d0272016-06-17 12:43:23 -0500957
Josh Poimboeuf4fa8d292016-06-17 12:43:26 -0500958 schedstat_set(se->statistics.block_start, 0);
959 schedstat_add(se->statistics.sum_sleep_runtime, delta);
Josh Poimboeuf1a3d0272016-06-17 12:43:23 -0500960
961 if (tsk) {
962 if (tsk->in_iowait) {
Josh Poimboeuf4fa8d292016-06-17 12:43:26 -0500963 schedstat_add(se->statistics.iowait_sum, delta);
964 schedstat_inc(se->statistics.iowait_count);
Josh Poimboeuf1a3d0272016-06-17 12:43:23 -0500965 trace_sched_stat_iowait(tsk, delta);
966 }
967
968 trace_sched_stat_blocked(tsk, delta);
969
970 /*
971 * Blocking time is in units of nanosecs, so shift by
972 * 20 to get a milliseconds-range estimation of the
973 * amount of time that the task spent sleeping:
974 */
975 if (unlikely(prof_on == SLEEP_PROFILING)) {
976 profile_hits(SLEEP_PROFILING,
977 (void *)get_wchan(tsk),
978 delta >> 20);
979 }
980 account_scheduler_latency(tsk, delta >> 10, 0);
981 }
982 }
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200983}
984
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200985/*
986 * Task is being enqueued - update stats:
987 */
Mel Gormancb251762016-02-05 09:08:36 +0000988static inline void
Josh Poimboeuf1a3d0272016-06-17 12:43:23 -0500989update_stats_enqueue(struct cfs_rq *cfs_rq, struct sched_entity *se, int flags)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200990{
Josh Poimboeuf4fa8d292016-06-17 12:43:26 -0500991 if (!schedstat_enabled())
992 return;
993
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200994 /*
995 * Are we enqueueing a waiting task? (for current tasks
996 * a dequeue/enqueue event is a NOP)
997 */
Ingo Molnar429d43b2007-10-15 17:00:03 +0200998 if (se != cfs_rq->curr)
Ingo Molnar5870db52007-08-09 11:16:47 +0200999 update_stats_wait_start(cfs_rq, se);
Josh Poimboeuf1a3d0272016-06-17 12:43:23 -05001000
1001 if (flags & ENQUEUE_WAKEUP)
1002 update_stats_enqueue_sleeper(cfs_rq, se);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001003}
1004
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001005static inline void
Mel Gormancb251762016-02-05 09:08:36 +00001006update_stats_dequeue(struct cfs_rq *cfs_rq, struct sched_entity *se, int flags)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001007{
Josh Poimboeuf4fa8d292016-06-17 12:43:26 -05001008
1009 if (!schedstat_enabled())
1010 return;
1011
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001012 /*
1013 * Mark the end of the wait period if dequeueing a
1014 * waiting task:
1015 */
Ingo Molnar429d43b2007-10-15 17:00:03 +02001016 if (se != cfs_rq->curr)
Ingo Molnar9ef0a962007-08-09 11:16:47 +02001017 update_stats_wait_end(cfs_rq, se);
Mel Gormancb251762016-02-05 09:08:36 +00001018
Josh Poimboeuf4fa8d292016-06-17 12:43:26 -05001019 if ((flags & DEQUEUE_SLEEP) && entity_is_task(se)) {
1020 struct task_struct *tsk = task_of(se);
Mel Gormancb251762016-02-05 09:08:36 +00001021
Josh Poimboeuf4fa8d292016-06-17 12:43:26 -05001022 if (tsk->state & TASK_INTERRUPTIBLE)
1023 schedstat_set(se->statistics.sleep_start,
1024 rq_clock(rq_of(cfs_rq)));
1025 if (tsk->state & TASK_UNINTERRUPTIBLE)
1026 schedstat_set(se->statistics.block_start,
1027 rq_clock(rq_of(cfs_rq)));
Mel Gormancb251762016-02-05 09:08:36 +00001028 }
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001029}
1030
1031/*
1032 * We are picking a new current task - update its stats:
1033 */
1034static inline void
Ingo Molnar79303e92007-08-09 11:16:47 +02001035update_stats_curr_start(struct cfs_rq *cfs_rq, struct sched_entity *se)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001036{
1037 /*
1038 * We are starting a new run period:
1039 */
Frederic Weisbecker78becc22013-04-12 01:51:02 +02001040 se->exec_start = rq_clock_task(rq_of(cfs_rq));
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001041}
1042
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001043/**************************************************
1044 * Scheduling class queueing methods:
1045 */
1046
Peter Zijlstracbee9f82012-10-25 14:16:43 +02001047#ifdef CONFIG_NUMA_BALANCING
1048/*
Mel Gorman598f0ec2013-10-07 11:28:55 +01001049 * Approximate time to scan a full NUMA task in ms. The task scan period is
1050 * calculated based on the tasks virtual memory size and
1051 * numa_balancing_scan_size.
Peter Zijlstracbee9f82012-10-25 14:16:43 +02001052 */
Mel Gorman598f0ec2013-10-07 11:28:55 +01001053unsigned int sysctl_numa_balancing_scan_period_min = 1000;
1054unsigned int sysctl_numa_balancing_scan_period_max = 60000;
Peter Zijlstra6e5fb222012-10-25 14:16:45 +02001055
1056/* Portion of address space to scan in MB */
1057unsigned int sysctl_numa_balancing_scan_size = 256;
Peter Zijlstracbee9f82012-10-25 14:16:43 +02001058
Peter Zijlstra4b96a292012-10-25 14:16:47 +02001059/* Scan @scan_size MB every @scan_period after an initial @scan_delay in ms */
1060unsigned int sysctl_numa_balancing_scan_delay = 1000;
1061
Rik van Rielb5dd77c2017-07-31 15:28:47 -04001062struct numa_group {
1063 atomic_t refcount;
1064
1065 spinlock_t lock; /* nr_tasks, tasks */
1066 int nr_tasks;
1067 pid_t gid;
1068 int active_nodes;
1069
1070 struct rcu_head rcu;
1071 unsigned long total_faults;
1072 unsigned long max_faults_cpu;
1073 /*
1074 * Faults_cpu is used to decide whether memory should move
1075 * towards the CPU. As a consequence, these stats are weighted
1076 * more by CPU use than by memory faults.
1077 */
1078 unsigned long *faults_cpu;
1079 unsigned long faults[0];
1080};
1081
1082static inline unsigned long group_faults_priv(struct numa_group *ng);
1083static inline unsigned long group_faults_shared(struct numa_group *ng);
1084
Mel Gorman598f0ec2013-10-07 11:28:55 +01001085static unsigned int task_nr_scan_windows(struct task_struct *p)
1086{
1087 unsigned long rss = 0;
1088 unsigned long nr_scan_pages;
1089
1090 /*
1091 * Calculations based on RSS as non-present and empty pages are skipped
1092 * by the PTE scanner and NUMA hinting faults should be trapped based
1093 * on resident pages
1094 */
1095 nr_scan_pages = sysctl_numa_balancing_scan_size << (20 - PAGE_SHIFT);
1096 rss = get_mm_rss(p->mm);
1097 if (!rss)
1098 rss = nr_scan_pages;
1099
1100 rss = round_up(rss, nr_scan_pages);
1101 return rss / nr_scan_pages;
1102}
1103
1104/* For sanitys sake, never scan more PTEs than MAX_SCAN_WINDOW MB/sec. */
1105#define MAX_SCAN_WINDOW 2560
1106
1107static unsigned int task_scan_min(struct task_struct *p)
1108{
Jason Low316c1608d2015-04-28 13:00:20 -07001109 unsigned int scan_size = READ_ONCE(sysctl_numa_balancing_scan_size);
Mel Gorman598f0ec2013-10-07 11:28:55 +01001110 unsigned int scan, floor;
1111 unsigned int windows = 1;
1112
Kirill Tkhai64192652014-10-16 14:39:37 +04001113 if (scan_size < MAX_SCAN_WINDOW)
1114 windows = MAX_SCAN_WINDOW / scan_size;
Mel Gorman598f0ec2013-10-07 11:28:55 +01001115 floor = 1000 / windows;
1116
1117 scan = sysctl_numa_balancing_scan_period_min / task_nr_scan_windows(p);
1118 return max_t(unsigned int, floor, scan);
1119}
1120
Rik van Rielb5dd77c2017-07-31 15:28:47 -04001121static unsigned int task_scan_start(struct task_struct *p)
1122{
1123 unsigned long smin = task_scan_min(p);
1124 unsigned long period = smin;
1125
1126 /* Scale the maximum scan period with the amount of shared memory. */
1127 if (p->numa_group) {
1128 struct numa_group *ng = p->numa_group;
1129 unsigned long shared = group_faults_shared(ng);
1130 unsigned long private = group_faults_priv(ng);
1131
1132 period *= atomic_read(&ng->refcount);
1133 period *= shared + 1;
1134 period /= private + shared + 1;
1135 }
1136
1137 return max(smin, period);
1138}
1139
Mel Gorman598f0ec2013-10-07 11:28:55 +01001140static unsigned int task_scan_max(struct task_struct *p)
1141{
Rik van Rielb5dd77c2017-07-31 15:28:47 -04001142 unsigned long smin = task_scan_min(p);
1143 unsigned long smax;
Mel Gorman598f0ec2013-10-07 11:28:55 +01001144
1145 /* Watch for min being lower than max due to floor calculations */
1146 smax = sysctl_numa_balancing_scan_period_max / task_nr_scan_windows(p);
Rik van Rielb5dd77c2017-07-31 15:28:47 -04001147
1148 /* Scale the maximum scan period with the amount of shared memory. */
1149 if (p->numa_group) {
1150 struct numa_group *ng = p->numa_group;
1151 unsigned long shared = group_faults_shared(ng);
1152 unsigned long private = group_faults_priv(ng);
1153 unsigned long period = smax;
1154
1155 period *= atomic_read(&ng->refcount);
1156 period *= shared + 1;
1157 period /= private + shared + 1;
1158
1159 smax = max(smax, period);
1160 }
1161
Mel Gorman598f0ec2013-10-07 11:28:55 +01001162 return max(smin, smax);
1163}
1164
Peter Zijlstra0ec8aa02013-10-07 11:29:33 +01001165static void account_numa_enqueue(struct rq *rq, struct task_struct *p)
1166{
1167 rq->nr_numa_running += (p->numa_preferred_nid != -1);
1168 rq->nr_preferred_running += (p->numa_preferred_nid == task_node(p));
1169}
1170
1171static void account_numa_dequeue(struct rq *rq, struct task_struct *p)
1172{
1173 rq->nr_numa_running -= (p->numa_preferred_nid != -1);
1174 rq->nr_preferred_running -= (p->numa_preferred_nid == task_node(p));
1175}
1176
Rik van Rielbe1e4e72014-01-27 17:03:48 -05001177/* Shared or private faults. */
1178#define NR_NUMA_HINT_FAULT_TYPES 2
1179
1180/* Memory and CPU locality */
1181#define NR_NUMA_HINT_FAULT_STATS (NR_NUMA_HINT_FAULT_TYPES * 2)
1182
1183/* Averaged statistics, and temporary buffers. */
1184#define NR_NUMA_HINT_FAULT_BUCKETS (NR_NUMA_HINT_FAULT_STATS * 2)
1185
Mel Gormane29cf082013-10-07 11:29:22 +01001186pid_t task_numa_group_id(struct task_struct *p)
1187{
1188 return p->numa_group ? p->numa_group->gid : 0;
1189}
1190
Iulia Manda44dba3d2014-10-31 02:13:31 +02001191/*
1192 * The averaged statistics, shared & private, memory & cpu,
1193 * occupy the first half of the array. The second half of the
1194 * array is for current counters, which are averaged into the
1195 * first set by task_numa_placement.
1196 */
1197static inline int task_faults_idx(enum numa_faults_stats s, int nid, int priv)
Mel Gormanac8e8952013-10-07 11:29:03 +01001198{
Iulia Manda44dba3d2014-10-31 02:13:31 +02001199 return NR_NUMA_HINT_FAULT_TYPES * (s * nr_node_ids + nid) + priv;
Mel Gormanac8e8952013-10-07 11:29:03 +01001200}
1201
1202static inline unsigned long task_faults(struct task_struct *p, int nid)
1203{
Iulia Manda44dba3d2014-10-31 02:13:31 +02001204 if (!p->numa_faults)
Mel Gormanac8e8952013-10-07 11:29:03 +01001205 return 0;
1206
Iulia Manda44dba3d2014-10-31 02:13:31 +02001207 return p->numa_faults[task_faults_idx(NUMA_MEM, nid, 0)] +
1208 p->numa_faults[task_faults_idx(NUMA_MEM, nid, 1)];
Mel Gormanac8e8952013-10-07 11:29:03 +01001209}
1210
Mel Gorman83e1d2c2013-10-07 11:29:27 +01001211static inline unsigned long group_faults(struct task_struct *p, int nid)
1212{
1213 if (!p->numa_group)
1214 return 0;
1215
Iulia Manda44dba3d2014-10-31 02:13:31 +02001216 return p->numa_group->faults[task_faults_idx(NUMA_MEM, nid, 0)] +
1217 p->numa_group->faults[task_faults_idx(NUMA_MEM, nid, 1)];
Mel Gorman83e1d2c2013-10-07 11:29:27 +01001218}
1219
Rik van Riel20e07de2014-01-27 17:03:43 -05001220static inline unsigned long group_faults_cpu(struct numa_group *group, int nid)
1221{
Iulia Manda44dba3d2014-10-31 02:13:31 +02001222 return group->faults_cpu[task_faults_idx(NUMA_MEM, nid, 0)] +
1223 group->faults_cpu[task_faults_idx(NUMA_MEM, nid, 1)];
Rik van Riel20e07de2014-01-27 17:03:43 -05001224}
1225
Rik van Rielb5dd77c2017-07-31 15:28:47 -04001226static inline unsigned long group_faults_priv(struct numa_group *ng)
1227{
1228 unsigned long faults = 0;
1229 int node;
1230
1231 for_each_online_node(node) {
1232 faults += ng->faults[task_faults_idx(NUMA_MEM, node, 1)];
1233 }
1234
1235 return faults;
1236}
1237
1238static inline unsigned long group_faults_shared(struct numa_group *ng)
1239{
1240 unsigned long faults = 0;
1241 int node;
1242
1243 for_each_online_node(node) {
1244 faults += ng->faults[task_faults_idx(NUMA_MEM, node, 0)];
1245 }
1246
1247 return faults;
1248}
1249
Rik van Riel4142c3e2016-01-25 17:07:39 -05001250/*
1251 * A node triggering more than 1/3 as many NUMA faults as the maximum is
1252 * considered part of a numa group's pseudo-interleaving set. Migrations
1253 * between these nodes are slowed down, to allow things to settle down.
1254 */
1255#define ACTIVE_NODE_FRACTION 3
1256
1257static bool numa_is_active_node(int nid, struct numa_group *ng)
1258{
1259 return group_faults_cpu(ng, nid) * ACTIVE_NODE_FRACTION > ng->max_faults_cpu;
1260}
1261
Rik van Riel6c6b1192014-10-17 03:29:52 -04001262/* Handle placement on systems where not all nodes are directly connected. */
1263static unsigned long score_nearby_nodes(struct task_struct *p, int nid,
1264 int maxdist, bool task)
1265{
1266 unsigned long score = 0;
1267 int node;
1268
1269 /*
1270 * All nodes are directly connected, and the same distance
1271 * from each other. No need for fancy placement algorithms.
1272 */
1273 if (sched_numa_topology_type == NUMA_DIRECT)
1274 return 0;
1275
1276 /*
1277 * This code is called for each node, introducing N^2 complexity,
1278 * which should be ok given the number of nodes rarely exceeds 8.
1279 */
1280 for_each_online_node(node) {
1281 unsigned long faults;
1282 int dist = node_distance(nid, node);
1283
1284 /*
1285 * The furthest away nodes in the system are not interesting
1286 * for placement; nid was already counted.
1287 */
1288 if (dist == sched_max_numa_distance || node == nid)
1289 continue;
1290
1291 /*
1292 * On systems with a backplane NUMA topology, compare groups
1293 * of nodes, and move tasks towards the group with the most
1294 * memory accesses. When comparing two nodes at distance
1295 * "hoplimit", only nodes closer by than "hoplimit" are part
1296 * of each group. Skip other nodes.
1297 */
1298 if (sched_numa_topology_type == NUMA_BACKPLANE &&
1299 dist > maxdist)
1300 continue;
1301
1302 /* Add up the faults from nearby nodes. */
1303 if (task)
1304 faults = task_faults(p, node);
1305 else
1306 faults = group_faults(p, node);
1307
1308 /*
1309 * On systems with a glueless mesh NUMA topology, there are
1310 * no fixed "groups of nodes". Instead, nodes that are not
1311 * directly connected bounce traffic through intermediate
1312 * nodes; a numa_group can occupy any set of nodes.
1313 * The further away a node is, the less the faults count.
1314 * This seems to result in good task placement.
1315 */
1316 if (sched_numa_topology_type == NUMA_GLUELESS_MESH) {
1317 faults *= (sched_max_numa_distance - dist);
1318 faults /= (sched_max_numa_distance - LOCAL_DISTANCE);
1319 }
1320
1321 score += faults;
1322 }
1323
1324 return score;
1325}
1326
Mel Gorman83e1d2c2013-10-07 11:29:27 +01001327/*
1328 * These return the fraction of accesses done by a particular task, or
1329 * task group, on a particular numa node. The group weight is given a
1330 * larger multiplier, in order to group tasks together that are almost
1331 * evenly spread out between numa nodes.
1332 */
Rik van Riel7bd95322014-10-17 03:29:51 -04001333static inline unsigned long task_weight(struct task_struct *p, int nid,
1334 int dist)
Mel Gorman83e1d2c2013-10-07 11:29:27 +01001335{
Rik van Riel7bd95322014-10-17 03:29:51 -04001336 unsigned long faults, total_faults;
Mel Gorman83e1d2c2013-10-07 11:29:27 +01001337
Iulia Manda44dba3d2014-10-31 02:13:31 +02001338 if (!p->numa_faults)
Mel Gorman83e1d2c2013-10-07 11:29:27 +01001339 return 0;
1340
1341 total_faults = p->total_numa_faults;
1342
1343 if (!total_faults)
1344 return 0;
1345
Rik van Riel7bd95322014-10-17 03:29:51 -04001346 faults = task_faults(p, nid);
Rik van Riel6c6b1192014-10-17 03:29:52 -04001347 faults += score_nearby_nodes(p, nid, dist, true);
1348
Rik van Riel7bd95322014-10-17 03:29:51 -04001349 return 1000 * faults / total_faults;
Mel Gorman83e1d2c2013-10-07 11:29:27 +01001350}
1351
Rik van Riel7bd95322014-10-17 03:29:51 -04001352static inline unsigned long group_weight(struct task_struct *p, int nid,
1353 int dist)
Mel Gorman83e1d2c2013-10-07 11:29:27 +01001354{
Rik van Riel7bd95322014-10-17 03:29:51 -04001355 unsigned long faults, total_faults;
1356
1357 if (!p->numa_group)
Mel Gorman83e1d2c2013-10-07 11:29:27 +01001358 return 0;
1359
Rik van Riel7bd95322014-10-17 03:29:51 -04001360 total_faults = p->numa_group->total_faults;
1361
1362 if (!total_faults)
1363 return 0;
1364
1365 faults = group_faults(p, nid);
Rik van Riel6c6b1192014-10-17 03:29:52 -04001366 faults += score_nearby_nodes(p, nid, dist, false);
1367
Rik van Riel7bd95322014-10-17 03:29:51 -04001368 return 1000 * faults / total_faults;
Mel Gorman83e1d2c2013-10-07 11:29:27 +01001369}
1370
Rik van Riel10f39042014-01-27 17:03:44 -05001371bool should_numa_migrate_memory(struct task_struct *p, struct page * page,
1372 int src_nid, int dst_cpu)
1373{
1374 struct numa_group *ng = p->numa_group;
1375 int dst_nid = cpu_to_node(dst_cpu);
1376 int last_cpupid, this_cpupid;
1377
1378 this_cpupid = cpu_pid_to_cpupid(dst_cpu, current->pid);
1379
1380 /*
1381 * Multi-stage node selection is used in conjunction with a periodic
1382 * migration fault to build a temporal task<->page relation. By using
1383 * a two-stage filter we remove short/unlikely relations.
1384 *
1385 * Using P(p) ~ n_p / n_t as per frequentist probability, we can equate
1386 * a task's usage of a particular page (n_p) per total usage of this
1387 * page (n_t) (in a given time-span) to a probability.
1388 *
1389 * Our periodic faults will sample this probability and getting the
1390 * same result twice in a row, given these samples are fully
1391 * independent, is then given by P(n)^2, provided our sample period
1392 * is sufficiently short compared to the usage pattern.
1393 *
1394 * This quadric squishes small probabilities, making it less likely we
1395 * act on an unlikely task<->page relation.
1396 */
1397 last_cpupid = page_cpupid_xchg_last(page, this_cpupid);
1398 if (!cpupid_pid_unset(last_cpupid) &&
1399 cpupid_to_nid(last_cpupid) != dst_nid)
1400 return false;
1401
1402 /* Always allow migrate on private faults */
1403 if (cpupid_match_pid(p, last_cpupid))
1404 return true;
1405
1406 /* A shared fault, but p->numa_group has not been set up yet. */
1407 if (!ng)
1408 return true;
1409
1410 /*
Rik van Riel4142c3e2016-01-25 17:07:39 -05001411 * Destination node is much more heavily used than the source
1412 * node? Allow migration.
Rik van Riel10f39042014-01-27 17:03:44 -05001413 */
Rik van Riel4142c3e2016-01-25 17:07:39 -05001414 if (group_faults_cpu(ng, dst_nid) > group_faults_cpu(ng, src_nid) *
1415 ACTIVE_NODE_FRACTION)
Rik van Riel10f39042014-01-27 17:03:44 -05001416 return true;
1417
1418 /*
Rik van Riel4142c3e2016-01-25 17:07:39 -05001419 * Distribute memory according to CPU & memory use on each node,
1420 * with 3/4 hysteresis to avoid unnecessary memory migrations:
1421 *
1422 * faults_cpu(dst) 3 faults_cpu(src)
1423 * --------------- * - > ---------------
1424 * faults_mem(dst) 4 faults_mem(src)
Rik van Riel10f39042014-01-27 17:03:44 -05001425 */
Rik van Riel4142c3e2016-01-25 17:07:39 -05001426 return group_faults_cpu(ng, dst_nid) * group_faults(p, src_nid) * 3 >
1427 group_faults_cpu(ng, src_nid) * group_faults(p, dst_nid) * 4;
Rik van Riel10f39042014-01-27 17:03:44 -05001428}
1429
Viresh Kumarc7132dd2017-05-24 10:59:54 +05301430static unsigned long weighted_cpuload(struct rq *rq);
Mel Gorman58d081b2013-10-07 11:29:10 +01001431static unsigned long source_load(int cpu, int type);
1432static unsigned long target_load(int cpu, int type);
Nicolas Pitreced549f2014-05-26 18:19:38 -04001433static unsigned long capacity_of(int cpu);
Mel Gormane6628d52013-10-07 11:29:02 +01001434
Mel Gormanfb13c7e2013-10-07 11:29:17 +01001435/* Cached statistics for all CPUs within a node */
Mel Gorman58d081b2013-10-07 11:29:10 +01001436struct numa_stats {
Mel Gormanfb13c7e2013-10-07 11:29:17 +01001437 unsigned long nr_running;
Mel Gorman58d081b2013-10-07 11:29:10 +01001438 unsigned long load;
Mel Gormanfb13c7e2013-10-07 11:29:17 +01001439
1440 /* Total compute capacity of CPUs on a node */
Nicolas Pitre5ef20ca2014-05-26 18:19:34 -04001441 unsigned long compute_capacity;
Mel Gormanfb13c7e2013-10-07 11:29:17 +01001442
1443 /* Approximate capacity in terms of runnable tasks on a node */
Nicolas Pitre5ef20ca2014-05-26 18:19:34 -04001444 unsigned long task_capacity;
Nicolas Pitre1b6a7492014-05-26 18:19:35 -04001445 int has_free_capacity;
Mel Gorman58d081b2013-10-07 11:29:10 +01001446};
Mel Gormane6628d52013-10-07 11:29:02 +01001447
Mel Gormanfb13c7e2013-10-07 11:29:17 +01001448/*
1449 * XXX borrowed from update_sg_lb_stats
1450 */
1451static void update_numa_stats(struct numa_stats *ns, int nid)
1452{
Rik van Riel83d7f242014-08-04 13:23:28 -04001453 int smt, cpu, cpus = 0;
1454 unsigned long capacity;
Mel Gormanfb13c7e2013-10-07 11:29:17 +01001455
1456 memset(ns, 0, sizeof(*ns));
1457 for_each_cpu(cpu, cpumask_of_node(nid)) {
1458 struct rq *rq = cpu_rq(cpu);
1459
1460 ns->nr_running += rq->nr_running;
Viresh Kumarc7132dd2017-05-24 10:59:54 +05301461 ns->load += weighted_cpuload(rq);
Nicolas Pitreced549f2014-05-26 18:19:38 -04001462 ns->compute_capacity += capacity_of(cpu);
Peter Zijlstra5eca82a2013-11-06 18:47:57 +01001463
1464 cpus++;
Mel Gormanfb13c7e2013-10-07 11:29:17 +01001465 }
1466
Peter Zijlstra5eca82a2013-11-06 18:47:57 +01001467 /*
1468 * If we raced with hotplug and there are no CPUs left in our mask
1469 * the @ns structure is NULL'ed and task_numa_compare() will
1470 * not find this node attractive.
1471 *
Nicolas Pitre1b6a7492014-05-26 18:19:35 -04001472 * We'll either bail at !has_free_capacity, or we'll detect a huge
1473 * imbalance and bail there.
Peter Zijlstra5eca82a2013-11-06 18:47:57 +01001474 */
1475 if (!cpus)
1476 return;
1477
Rik van Riel83d7f242014-08-04 13:23:28 -04001478 /* smt := ceil(cpus / capacity), assumes: 1 < smt_power < 2 */
1479 smt = DIV_ROUND_UP(SCHED_CAPACITY_SCALE * cpus, ns->compute_capacity);
1480 capacity = cpus / smt; /* cores */
1481
1482 ns->task_capacity = min_t(unsigned, capacity,
1483 DIV_ROUND_CLOSEST(ns->compute_capacity, SCHED_CAPACITY_SCALE));
Nicolas Pitre1b6a7492014-05-26 18:19:35 -04001484 ns->has_free_capacity = (ns->nr_running < ns->task_capacity);
Mel Gormanfb13c7e2013-10-07 11:29:17 +01001485}
1486
Mel Gorman58d081b2013-10-07 11:29:10 +01001487struct task_numa_env {
1488 struct task_struct *p;
1489
1490 int src_cpu, src_nid;
1491 int dst_cpu, dst_nid;
1492
1493 struct numa_stats src_stats, dst_stats;
1494
Wanpeng Li40ea2b42013-12-05 19:10:17 +08001495 int imbalance_pct;
Rik van Riel7bd95322014-10-17 03:29:51 -04001496 int dist;
Mel Gormanfb13c7e2013-10-07 11:29:17 +01001497
1498 struct task_struct *best_task;
1499 long best_imp;
Mel Gorman58d081b2013-10-07 11:29:10 +01001500 int best_cpu;
1501};
1502
Mel Gormanfb13c7e2013-10-07 11:29:17 +01001503static void task_numa_assign(struct task_numa_env *env,
1504 struct task_struct *p, long imp)
1505{
1506 if (env->best_task)
1507 put_task_struct(env->best_task);
Oleg Nesterovbac78572016-05-18 21:57:33 +02001508 if (p)
1509 get_task_struct(p);
Mel Gormanfb13c7e2013-10-07 11:29:17 +01001510
1511 env->best_task = p;
1512 env->best_imp = imp;
1513 env->best_cpu = env->dst_cpu;
1514}
1515
Rik van Riel28a21742014-06-23 11:46:13 -04001516static bool load_too_imbalanced(long src_load, long dst_load,
Rik van Riele63da032014-05-14 13:22:21 -04001517 struct task_numa_env *env)
1518{
Rik van Riele4991b22015-05-27 15:04:27 -04001519 long imb, old_imb;
1520 long orig_src_load, orig_dst_load;
Rik van Riel28a21742014-06-23 11:46:13 -04001521 long src_capacity, dst_capacity;
1522
1523 /*
1524 * The load is corrected for the CPU capacity available on each node.
1525 *
1526 * src_load dst_load
1527 * ------------ vs ---------
1528 * src_capacity dst_capacity
1529 */
1530 src_capacity = env->src_stats.compute_capacity;
1531 dst_capacity = env->dst_stats.compute_capacity;
Rik van Riele63da032014-05-14 13:22:21 -04001532
1533 /* We care about the slope of the imbalance, not the direction. */
Rik van Riele4991b22015-05-27 15:04:27 -04001534 if (dst_load < src_load)
1535 swap(dst_load, src_load);
Rik van Riele63da032014-05-14 13:22:21 -04001536
1537 /* Is the difference below the threshold? */
Rik van Riele4991b22015-05-27 15:04:27 -04001538 imb = dst_load * src_capacity * 100 -
1539 src_load * dst_capacity * env->imbalance_pct;
Rik van Riele63da032014-05-14 13:22:21 -04001540 if (imb <= 0)
1541 return false;
1542
1543 /*
1544 * The imbalance is above the allowed threshold.
Rik van Riele4991b22015-05-27 15:04:27 -04001545 * Compare it with the old imbalance.
Rik van Riele63da032014-05-14 13:22:21 -04001546 */
Rik van Riel28a21742014-06-23 11:46:13 -04001547 orig_src_load = env->src_stats.load;
Rik van Riele4991b22015-05-27 15:04:27 -04001548 orig_dst_load = env->dst_stats.load;
Rik van Riel28a21742014-06-23 11:46:13 -04001549
Rik van Riele4991b22015-05-27 15:04:27 -04001550 if (orig_dst_load < orig_src_load)
1551 swap(orig_dst_load, orig_src_load);
Rik van Riele63da032014-05-14 13:22:21 -04001552
Rik van Riele4991b22015-05-27 15:04:27 -04001553 old_imb = orig_dst_load * src_capacity * 100 -
1554 orig_src_load * dst_capacity * env->imbalance_pct;
1555
1556 /* Would this change make things worse? */
1557 return (imb > old_imb);
Rik van Riele63da032014-05-14 13:22:21 -04001558}
1559
Mel Gormanfb13c7e2013-10-07 11:29:17 +01001560/*
1561 * This checks if the overall compute and NUMA accesses of the system would
1562 * be improved if the source tasks was migrated to the target dst_cpu taking
1563 * into account that it might be best if task running on the dst_cpu should
1564 * be exchanged with the source task
1565 */
Rik van Riel887c2902013-10-07 11:29:31 +01001566static void task_numa_compare(struct task_numa_env *env,
1567 long taskimp, long groupimp)
Mel Gormanfb13c7e2013-10-07 11:29:17 +01001568{
1569 struct rq *src_rq = cpu_rq(env->src_cpu);
1570 struct rq *dst_rq = cpu_rq(env->dst_cpu);
1571 struct task_struct *cur;
Rik van Riel28a21742014-06-23 11:46:13 -04001572 long src_load, dst_load;
Mel Gormanfb13c7e2013-10-07 11:29:17 +01001573 long load;
Rik van Riel1c5d3eb2014-06-23 11:46:15 -04001574 long imp = env->p->numa_group ? groupimp : taskimp;
Rik van Riel0132c3e2014-06-23 11:46:16 -04001575 long moveimp = imp;
Rik van Riel7bd95322014-10-17 03:29:51 -04001576 int dist = env->dist;
Mel Gormanfb13c7e2013-10-07 11:29:17 +01001577
1578 rcu_read_lock();
Oleg Nesterovbac78572016-05-18 21:57:33 +02001579 cur = task_rcu_dereference(&dst_rq->curr);
1580 if (cur && ((cur->flags & PF_EXITING) || is_idle_task(cur)))
Mel Gormanfb13c7e2013-10-07 11:29:17 +01001581 cur = NULL;
1582
1583 /*
Peter Zijlstra7af68332014-11-10 10:54:35 +01001584 * Because we have preemption enabled we can get migrated around and
1585 * end try selecting ourselves (current == env->p) as a swap candidate.
1586 */
1587 if (cur == env->p)
1588 goto unlock;
1589
1590 /*
Mel Gormanfb13c7e2013-10-07 11:29:17 +01001591 * "imp" is the fault differential for the source task between the
1592 * source and destination node. Calculate the total differential for
1593 * the source task and potential destination task. The more negative
1594 * the value is, the more rmeote accesses that would be expected to
1595 * be incurred if the tasks were swapped.
1596 */
1597 if (cur) {
1598 /* Skip this swap candidate if cannot move to the source cpu */
Ingo Molnar0c98d342017-02-05 15:38:10 +01001599 if (!cpumask_test_cpu(env->src_cpu, &cur->cpus_allowed))
Mel Gormanfb13c7e2013-10-07 11:29:17 +01001600 goto unlock;
1601
Rik van Riel887c2902013-10-07 11:29:31 +01001602 /*
1603 * If dst and source tasks are in the same NUMA group, or not
Rik van Rielca28aa532013-10-07 11:29:32 +01001604 * in any group then look only at task weights.
Rik van Riel887c2902013-10-07 11:29:31 +01001605 */
Rik van Rielca28aa532013-10-07 11:29:32 +01001606 if (cur->numa_group == env->p->numa_group) {
Rik van Riel7bd95322014-10-17 03:29:51 -04001607 imp = taskimp + task_weight(cur, env->src_nid, dist) -
1608 task_weight(cur, env->dst_nid, dist);
Rik van Rielca28aa532013-10-07 11:29:32 +01001609 /*
1610 * Add some hysteresis to prevent swapping the
1611 * tasks within a group over tiny differences.
1612 */
1613 if (cur->numa_group)
1614 imp -= imp/16;
Rik van Riel887c2902013-10-07 11:29:31 +01001615 } else {
Rik van Rielca28aa532013-10-07 11:29:32 +01001616 /*
1617 * Compare the group weights. If a task is all by
1618 * itself (not part of a group), use the task weight
1619 * instead.
1620 */
Rik van Rielca28aa532013-10-07 11:29:32 +01001621 if (cur->numa_group)
Rik van Riel7bd95322014-10-17 03:29:51 -04001622 imp += group_weight(cur, env->src_nid, dist) -
1623 group_weight(cur, env->dst_nid, dist);
Rik van Rielca28aa532013-10-07 11:29:32 +01001624 else
Rik van Riel7bd95322014-10-17 03:29:51 -04001625 imp += task_weight(cur, env->src_nid, dist) -
1626 task_weight(cur, env->dst_nid, dist);
Rik van Riel887c2902013-10-07 11:29:31 +01001627 }
Mel Gormanfb13c7e2013-10-07 11:29:17 +01001628 }
1629
Rik van Riel0132c3e2014-06-23 11:46:16 -04001630 if (imp <= env->best_imp && moveimp <= env->best_imp)
Mel Gormanfb13c7e2013-10-07 11:29:17 +01001631 goto unlock;
1632
1633 if (!cur) {
1634 /* Is there capacity at our destination? */
Rik van Rielb932c032014-08-04 13:23:27 -04001635 if (env->src_stats.nr_running <= env->src_stats.task_capacity &&
Nicolas Pitre1b6a7492014-05-26 18:19:35 -04001636 !env->dst_stats.has_free_capacity)
Mel Gormanfb13c7e2013-10-07 11:29:17 +01001637 goto unlock;
1638
1639 goto balance;
1640 }
1641
1642 /* Balance doesn't matter much if we're running a task per cpu */
Rik van Riel0132c3e2014-06-23 11:46:16 -04001643 if (imp > env->best_imp && src_rq->nr_running == 1 &&
1644 dst_rq->nr_running == 1)
Mel Gormanfb13c7e2013-10-07 11:29:17 +01001645 goto assign;
1646
1647 /*
1648 * In the overloaded case, try and keep the load balanced.
1649 */
1650balance:
Peter Zijlstrae720fff2014-07-11 16:01:53 +02001651 load = task_h_load(env->p);
1652 dst_load = env->dst_stats.load + load;
1653 src_load = env->src_stats.load - load;
Mel Gormanfb13c7e2013-10-07 11:29:17 +01001654
Rik van Riel0132c3e2014-06-23 11:46:16 -04001655 if (moveimp > imp && moveimp > env->best_imp) {
1656 /*
1657 * If the improvement from just moving env->p direction is
1658 * better than swapping tasks around, check if a move is
1659 * possible. Store a slightly smaller score than moveimp,
1660 * so an actually idle CPU will win.
1661 */
1662 if (!load_too_imbalanced(src_load, dst_load, env)) {
1663 imp = moveimp - 1;
1664 cur = NULL;
1665 goto assign;
1666 }
1667 }
1668
1669 if (imp <= env->best_imp)
1670 goto unlock;
1671
Mel Gormanfb13c7e2013-10-07 11:29:17 +01001672 if (cur) {
Peter Zijlstrae720fff2014-07-11 16:01:53 +02001673 load = task_h_load(cur);
1674 dst_load -= load;
1675 src_load += load;
Mel Gormanfb13c7e2013-10-07 11:29:17 +01001676 }
1677
Rik van Riel28a21742014-06-23 11:46:13 -04001678 if (load_too_imbalanced(src_load, dst_load, env))
Mel Gormanfb13c7e2013-10-07 11:29:17 +01001679 goto unlock;
1680
Rik van Rielba7e5a22014-09-04 16:35:30 -04001681 /*
1682 * One idle CPU per node is evaluated for a task numa move.
1683 * Call select_idle_sibling to maybe find a better one.
1684 */
Peter Zijlstra10e2f1a2016-05-09 10:38:05 +02001685 if (!cur) {
1686 /*
1687 * select_idle_siblings() uses an per-cpu cpumask that
1688 * can be used from IRQ context.
1689 */
1690 local_irq_disable();
Morten Rasmussen772bd008c2016-06-22 18:03:13 +01001691 env->dst_cpu = select_idle_sibling(env->p, env->src_cpu,
1692 env->dst_cpu);
Peter Zijlstra10e2f1a2016-05-09 10:38:05 +02001693 local_irq_enable();
1694 }
Rik van Rielba7e5a22014-09-04 16:35:30 -04001695
Mel Gormanfb13c7e2013-10-07 11:29:17 +01001696assign:
1697 task_numa_assign(env, cur, imp);
1698unlock:
1699 rcu_read_unlock();
1700}
1701
Rik van Riel887c2902013-10-07 11:29:31 +01001702static void task_numa_find_cpu(struct task_numa_env *env,
1703 long taskimp, long groupimp)
Mel Gorman2c8a50a2013-10-07 11:29:18 +01001704{
1705 int cpu;
1706
1707 for_each_cpu(cpu, cpumask_of_node(env->dst_nid)) {
1708 /* Skip this CPU if the source task cannot migrate */
Ingo Molnar0c98d342017-02-05 15:38:10 +01001709 if (!cpumask_test_cpu(cpu, &env->p->cpus_allowed))
Mel Gorman2c8a50a2013-10-07 11:29:18 +01001710 continue;
1711
1712 env->dst_cpu = cpu;
Rik van Riel887c2902013-10-07 11:29:31 +01001713 task_numa_compare(env, taskimp, groupimp);
Mel Gorman2c8a50a2013-10-07 11:29:18 +01001714 }
1715}
1716
Rik van Riel6f9aad02015-05-28 09:52:49 -04001717/* Only move tasks to a NUMA node less busy than the current node. */
1718static bool numa_has_capacity(struct task_numa_env *env)
1719{
1720 struct numa_stats *src = &env->src_stats;
1721 struct numa_stats *dst = &env->dst_stats;
1722
1723 if (src->has_free_capacity && !dst->has_free_capacity)
1724 return false;
1725
1726 /*
1727 * Only consider a task move if the source has a higher load
1728 * than the destination, corrected for CPU capacity on each node.
1729 *
1730 * src->load dst->load
1731 * --------------------- vs ---------------------
1732 * src->compute_capacity dst->compute_capacity
1733 */
Srikar Dronamraju44dcb042015-06-16 17:26:00 +05301734 if (src->load * dst->compute_capacity * env->imbalance_pct >
1735
1736 dst->load * src->compute_capacity * 100)
Rik van Riel6f9aad02015-05-28 09:52:49 -04001737 return true;
1738
1739 return false;
1740}
1741
Mel Gorman58d081b2013-10-07 11:29:10 +01001742static int task_numa_migrate(struct task_struct *p)
Mel Gormane6628d52013-10-07 11:29:02 +01001743{
Mel Gorman58d081b2013-10-07 11:29:10 +01001744 struct task_numa_env env = {
1745 .p = p,
Mel Gormanfb13c7e2013-10-07 11:29:17 +01001746
Mel Gorman58d081b2013-10-07 11:29:10 +01001747 .src_cpu = task_cpu(p),
Ingo Molnarb32e86b2013-10-07 11:29:30 +01001748 .src_nid = task_node(p),
Mel Gormanfb13c7e2013-10-07 11:29:17 +01001749
1750 .imbalance_pct = 112,
1751
1752 .best_task = NULL,
1753 .best_imp = 0,
Rik van Riel4142c3e2016-01-25 17:07:39 -05001754 .best_cpu = -1,
Mel Gorman58d081b2013-10-07 11:29:10 +01001755 };
1756 struct sched_domain *sd;
Rik van Riel887c2902013-10-07 11:29:31 +01001757 unsigned long taskweight, groupweight;
Rik van Riel7bd95322014-10-17 03:29:51 -04001758 int nid, ret, dist;
Rik van Riel887c2902013-10-07 11:29:31 +01001759 long taskimp, groupimp;
Mel Gormane6628d52013-10-07 11:29:02 +01001760
Mel Gorman58d081b2013-10-07 11:29:10 +01001761 /*
Mel Gormanfb13c7e2013-10-07 11:29:17 +01001762 * Pick the lowest SD_NUMA domain, as that would have the smallest
1763 * imbalance and would be the first to start moving tasks about.
1764 *
1765 * And we want to avoid any moving of tasks about, as that would create
1766 * random movement of tasks -- counter the numa conditions we're trying
1767 * to satisfy here.
Mel Gorman58d081b2013-10-07 11:29:10 +01001768 */
Mel Gormane6628d52013-10-07 11:29:02 +01001769 rcu_read_lock();
Mel Gormanfb13c7e2013-10-07 11:29:17 +01001770 sd = rcu_dereference(per_cpu(sd_numa, env.src_cpu));
Rik van Riel46a73e82013-11-11 19:29:25 -05001771 if (sd)
1772 env.imbalance_pct = 100 + (sd->imbalance_pct - 100) / 2;
Mel Gormane6628d52013-10-07 11:29:02 +01001773 rcu_read_unlock();
1774
Rik van Riel46a73e82013-11-11 19:29:25 -05001775 /*
1776 * Cpusets can break the scheduler domain tree into smaller
1777 * balance domains, some of which do not cross NUMA boundaries.
1778 * Tasks that are "trapped" in such domains cannot be migrated
1779 * elsewhere, so there is no point in (re)trying.
1780 */
1781 if (unlikely(!sd)) {
Wanpeng Lide1b3012013-12-12 15:23:24 +08001782 p->numa_preferred_nid = task_node(p);
Rik van Riel46a73e82013-11-11 19:29:25 -05001783 return -EINVAL;
1784 }
1785
Mel Gorman2c8a50a2013-10-07 11:29:18 +01001786 env.dst_nid = p->numa_preferred_nid;
Rik van Riel7bd95322014-10-17 03:29:51 -04001787 dist = env.dist = node_distance(env.src_nid, env.dst_nid);
1788 taskweight = task_weight(p, env.src_nid, dist);
1789 groupweight = group_weight(p, env.src_nid, dist);
1790 update_numa_stats(&env.src_stats, env.src_nid);
1791 taskimp = task_weight(p, env.dst_nid, dist) - taskweight;
1792 groupimp = group_weight(p, env.dst_nid, dist) - groupweight;
Mel Gorman2c8a50a2013-10-07 11:29:18 +01001793 update_numa_stats(&env.dst_stats, env.dst_nid);
Mel Gorman58d081b2013-10-07 11:29:10 +01001794
Rik van Riela43455a2014-06-04 16:09:42 -04001795 /* Try to find a spot on the preferred nid. */
Rik van Riel6f9aad02015-05-28 09:52:49 -04001796 if (numa_has_capacity(&env))
1797 task_numa_find_cpu(&env, taskimp, groupimp);
Rik van Riele1dda8a2013-10-07 11:29:19 +01001798
Rik van Riel9de05d42014-10-09 17:27:47 -04001799 /*
1800 * Look at other nodes in these cases:
1801 * - there is no space available on the preferred_nid
1802 * - the task is part of a numa_group that is interleaved across
1803 * multiple NUMA nodes; in order to better consolidate the group,
1804 * we need to check other locations.
1805 */
Rik van Riel4142c3e2016-01-25 17:07:39 -05001806 if (env.best_cpu == -1 || (p->numa_group && p->numa_group->active_nodes > 1)) {
Mel Gorman2c8a50a2013-10-07 11:29:18 +01001807 for_each_online_node(nid) {
1808 if (nid == env.src_nid || nid == p->numa_preferred_nid)
Mel Gormanfb13c7e2013-10-07 11:29:17 +01001809 continue;
1810
Rik van Riel7bd95322014-10-17 03:29:51 -04001811 dist = node_distance(env.src_nid, env.dst_nid);
Rik van Riel6c6b1192014-10-17 03:29:52 -04001812 if (sched_numa_topology_type == NUMA_BACKPLANE &&
1813 dist != env.dist) {
1814 taskweight = task_weight(p, env.src_nid, dist);
1815 groupweight = group_weight(p, env.src_nid, dist);
1816 }
Rik van Riel7bd95322014-10-17 03:29:51 -04001817
Mel Gorman83e1d2c2013-10-07 11:29:27 +01001818 /* Only consider nodes where both task and groups benefit */
Rik van Riel7bd95322014-10-17 03:29:51 -04001819 taskimp = task_weight(p, nid, dist) - taskweight;
1820 groupimp = group_weight(p, nid, dist) - groupweight;
Rik van Riel887c2902013-10-07 11:29:31 +01001821 if (taskimp < 0 && groupimp < 0)
Mel Gorman2c8a50a2013-10-07 11:29:18 +01001822 continue;
1823
Rik van Riel7bd95322014-10-17 03:29:51 -04001824 env.dist = dist;
Mel Gorman2c8a50a2013-10-07 11:29:18 +01001825 env.dst_nid = nid;
1826 update_numa_stats(&env.dst_stats, env.dst_nid);
Rik van Riel6f9aad02015-05-28 09:52:49 -04001827 if (numa_has_capacity(&env))
1828 task_numa_find_cpu(&env, taskimp, groupimp);
Mel Gorman58d081b2013-10-07 11:29:10 +01001829 }
1830 }
1831
Rik van Riel68d1b022014-04-11 13:00:29 -04001832 /*
1833 * If the task is part of a workload that spans multiple NUMA nodes,
1834 * and is migrating into one of the workload's active nodes, remember
1835 * this node as the task's preferred numa node, so the workload can
1836 * settle down.
1837 * A task that migrated to a second choice node will be better off
1838 * trying for a better one later. Do not set the preferred node here.
1839 */
Rik van Rieldb015da2014-06-23 11:41:34 -04001840 if (p->numa_group) {
Rik van Riel4142c3e2016-01-25 17:07:39 -05001841 struct numa_group *ng = p->numa_group;
1842
Rik van Rieldb015da2014-06-23 11:41:34 -04001843 if (env.best_cpu == -1)
1844 nid = env.src_nid;
1845 else
1846 nid = env.dst_nid;
1847
Rik van Riel4142c3e2016-01-25 17:07:39 -05001848 if (ng->active_nodes > 1 && numa_is_active_node(env.dst_nid, ng))
Rik van Rieldb015da2014-06-23 11:41:34 -04001849 sched_setnuma(p, env.dst_nid);
1850 }
1851
1852 /* No better CPU than the current one was found. */
1853 if (env.best_cpu == -1)
1854 return -EAGAIN;
Peter Zijlstra0ec8aa02013-10-07 11:29:33 +01001855
Rik van Riel04bb2f92013-10-07 11:29:36 +01001856 /*
1857 * Reset the scan period if the task is being rescheduled on an
1858 * alternative node to recheck if the tasks is now properly placed.
1859 */
Rik van Rielb5dd77c2017-07-31 15:28:47 -04001860 p->numa_scan_period = task_scan_start(p);
Rik van Riel04bb2f92013-10-07 11:29:36 +01001861
Mel Gormanfb13c7e2013-10-07 11:29:17 +01001862 if (env.best_task == NULL) {
Mel Gorman286549d2014-01-21 15:51:03 -08001863 ret = migrate_task_to(p, env.best_cpu);
1864 if (ret != 0)
1865 trace_sched_stick_numa(p, env.src_cpu, env.best_cpu);
Mel Gormanfb13c7e2013-10-07 11:29:17 +01001866 return ret;
1867 }
1868
1869 ret = migrate_swap(p, env.best_task);
Mel Gorman286549d2014-01-21 15:51:03 -08001870 if (ret != 0)
1871 trace_sched_stick_numa(p, env.src_cpu, task_cpu(env.best_task));
Mel Gormanfb13c7e2013-10-07 11:29:17 +01001872 put_task_struct(env.best_task);
1873 return ret;
Mel Gormane6628d52013-10-07 11:29:02 +01001874}
1875
Mel Gorman6b9a7462013-10-07 11:29:11 +01001876/* Attempt to migrate a task to a CPU on the preferred node. */
1877static void numa_migrate_preferred(struct task_struct *p)
1878{
Rik van Riel5085e2a2014-04-11 13:00:28 -04001879 unsigned long interval = HZ;
1880
Rik van Riel2739d3e2013-10-07 11:29:41 +01001881 /* This task has no NUMA fault statistics yet */
Iulia Manda44dba3d2014-10-31 02:13:31 +02001882 if (unlikely(p->numa_preferred_nid == -1 || !p->numa_faults))
Rik van Riel2739d3e2013-10-07 11:29:41 +01001883 return;
1884
1885 /* Periodically retry migrating the task to the preferred node */
Rik van Riel5085e2a2014-04-11 13:00:28 -04001886 interval = min(interval, msecs_to_jiffies(p->numa_scan_period) / 16);
1887 p->numa_migrate_retry = jiffies + interval;
Rik van Riel2739d3e2013-10-07 11:29:41 +01001888
Mel Gorman6b9a7462013-10-07 11:29:11 +01001889 /* Success if task is already running on preferred CPU */
Wanpeng Lide1b3012013-12-12 15:23:24 +08001890 if (task_node(p) == p->numa_preferred_nid)
Mel Gorman6b9a7462013-10-07 11:29:11 +01001891 return;
1892
Mel Gorman6b9a7462013-10-07 11:29:11 +01001893 /* Otherwise, try migrate to a CPU on the preferred node */
Rik van Riel2739d3e2013-10-07 11:29:41 +01001894 task_numa_migrate(p);
Mel Gorman6b9a7462013-10-07 11:29:11 +01001895}
1896
Rik van Riel04bb2f92013-10-07 11:29:36 +01001897/*
Rik van Riel4142c3e2016-01-25 17:07:39 -05001898 * Find out how many nodes on the workload is actively running on. Do this by
Rik van Riel20e07de2014-01-27 17:03:43 -05001899 * tracking the nodes from which NUMA hinting faults are triggered. This can
1900 * be different from the set of nodes where the workload's memory is currently
1901 * located.
Rik van Riel20e07de2014-01-27 17:03:43 -05001902 */
Rik van Riel4142c3e2016-01-25 17:07:39 -05001903static void numa_group_count_active_nodes(struct numa_group *numa_group)
Rik van Riel20e07de2014-01-27 17:03:43 -05001904{
1905 unsigned long faults, max_faults = 0;
Rik van Riel4142c3e2016-01-25 17:07:39 -05001906 int nid, active_nodes = 0;
Rik van Riel20e07de2014-01-27 17:03:43 -05001907
1908 for_each_online_node(nid) {
1909 faults = group_faults_cpu(numa_group, nid);
1910 if (faults > max_faults)
1911 max_faults = faults;
1912 }
1913
1914 for_each_online_node(nid) {
1915 faults = group_faults_cpu(numa_group, nid);
Rik van Riel4142c3e2016-01-25 17:07:39 -05001916 if (faults * ACTIVE_NODE_FRACTION > max_faults)
1917 active_nodes++;
Rik van Riel20e07de2014-01-27 17:03:43 -05001918 }
Rik van Riel4142c3e2016-01-25 17:07:39 -05001919
1920 numa_group->max_faults_cpu = max_faults;
1921 numa_group->active_nodes = active_nodes;
Rik van Riel20e07de2014-01-27 17:03:43 -05001922}
1923
1924/*
Rik van Riel04bb2f92013-10-07 11:29:36 +01001925 * When adapting the scan rate, the period is divided into NUMA_PERIOD_SLOTS
1926 * increments. The more local the fault statistics are, the higher the scan
Rik van Riela22b4b02014-06-23 11:41:35 -04001927 * period will be for the next scan window. If local/(local+remote) ratio is
1928 * below NUMA_PERIOD_THRESHOLD (where range of ratio is 1..NUMA_PERIOD_SLOTS)
1929 * the scan period will decrease. Aim for 70% local accesses.
Rik van Riel04bb2f92013-10-07 11:29:36 +01001930 */
1931#define NUMA_PERIOD_SLOTS 10
Rik van Riela22b4b02014-06-23 11:41:35 -04001932#define NUMA_PERIOD_THRESHOLD 7
Rik van Riel04bb2f92013-10-07 11:29:36 +01001933
1934/*
1935 * Increase the scan period (slow down scanning) if the majority of
1936 * our memory is already on our local node, or if the majority of
1937 * the page accesses are shared with other processes.
1938 * Otherwise, decrease the scan period.
1939 */
1940static void update_task_scan_period(struct task_struct *p,
1941 unsigned long shared, unsigned long private)
1942{
1943 unsigned int period_slot;
Rik van Riel37ec97de2017-07-31 15:28:46 -04001944 int lr_ratio, ps_ratio;
Rik van Riel04bb2f92013-10-07 11:29:36 +01001945 int diff;
1946
1947 unsigned long remote = p->numa_faults_locality[0];
1948 unsigned long local = p->numa_faults_locality[1];
1949
1950 /*
1951 * If there were no record hinting faults then either the task is
1952 * completely idle or all activity is areas that are not of interest
Mel Gorman074c2382015-03-25 15:55:42 -07001953 * to automatic numa balancing. Related to that, if there were failed
1954 * migration then it implies we are migrating too quickly or the local
1955 * node is overloaded. In either case, scan slower
Rik van Riel04bb2f92013-10-07 11:29:36 +01001956 */
Mel Gorman074c2382015-03-25 15:55:42 -07001957 if (local + shared == 0 || p->numa_faults_locality[2]) {
Rik van Riel04bb2f92013-10-07 11:29:36 +01001958 p->numa_scan_period = min(p->numa_scan_period_max,
1959 p->numa_scan_period << 1);
1960
1961 p->mm->numa_next_scan = jiffies +
1962 msecs_to_jiffies(p->numa_scan_period);
1963
1964 return;
1965 }
1966
1967 /*
1968 * Prepare to scale scan period relative to the current period.
1969 * == NUMA_PERIOD_THRESHOLD scan period stays the same
1970 * < NUMA_PERIOD_THRESHOLD scan period decreases (scan faster)
1971 * >= NUMA_PERIOD_THRESHOLD scan period increases (scan slower)
1972 */
1973 period_slot = DIV_ROUND_UP(p->numa_scan_period, NUMA_PERIOD_SLOTS);
Rik van Riel37ec97de2017-07-31 15:28:46 -04001974 lr_ratio = (local * NUMA_PERIOD_SLOTS) / (local + remote);
1975 ps_ratio = (private * NUMA_PERIOD_SLOTS) / (private + shared);
1976
1977 if (ps_ratio >= NUMA_PERIOD_THRESHOLD) {
1978 /*
1979 * Most memory accesses are local. There is no need to
1980 * do fast NUMA scanning, since memory is already local.
1981 */
1982 int slot = ps_ratio - NUMA_PERIOD_THRESHOLD;
1983 if (!slot)
1984 slot = 1;
1985 diff = slot * period_slot;
1986 } else if (lr_ratio >= NUMA_PERIOD_THRESHOLD) {
1987 /*
1988 * Most memory accesses are shared with other tasks.
1989 * There is no point in continuing fast NUMA scanning,
1990 * since other tasks may just move the memory elsewhere.
1991 */
1992 int slot = lr_ratio - NUMA_PERIOD_THRESHOLD;
Rik van Riel04bb2f92013-10-07 11:29:36 +01001993 if (!slot)
1994 slot = 1;
1995 diff = slot * period_slot;
1996 } else {
Rik van Riel04bb2f92013-10-07 11:29:36 +01001997 /*
Rik van Riel37ec97de2017-07-31 15:28:46 -04001998 * Private memory faults exceed (SLOTS-THRESHOLD)/SLOTS,
1999 * yet they are not on the local NUMA node. Speed up
2000 * NUMA scanning to get the memory moved over.
Rik van Riel04bb2f92013-10-07 11:29:36 +01002001 */
Rik van Riel37ec97de2017-07-31 15:28:46 -04002002 int ratio = max(lr_ratio, ps_ratio);
2003 diff = -(NUMA_PERIOD_THRESHOLD - ratio) * period_slot;
Rik van Riel04bb2f92013-10-07 11:29:36 +01002004 }
2005
2006 p->numa_scan_period = clamp(p->numa_scan_period + diff,
2007 task_scan_min(p), task_scan_max(p));
2008 memset(p->numa_faults_locality, 0, sizeof(p->numa_faults_locality));
2009}
2010
Rik van Riel7e2703e2014-01-27 17:03:45 -05002011/*
2012 * Get the fraction of time the task has been running since the last
2013 * NUMA placement cycle. The scheduler keeps similar statistics, but
2014 * decays those on a 32ms period, which is orders of magnitude off
2015 * from the dozens-of-seconds NUMA balancing period. Use the scheduler
2016 * stats only if the task is so new there are no NUMA statistics yet.
2017 */
2018static u64 numa_get_avg_runtime(struct task_struct *p, u64 *period)
2019{
2020 u64 runtime, delta, now;
2021 /* Use the start of this time slice to avoid calculations. */
2022 now = p->se.exec_start;
2023 runtime = p->se.sum_exec_runtime;
2024
2025 if (p->last_task_numa_placement) {
2026 delta = runtime - p->last_sum_exec_runtime;
2027 *period = now - p->last_task_numa_placement;
2028 } else {
Yuyang Du9d89c252015-07-15 08:04:37 +08002029 delta = p->se.avg.load_sum / p->se.load.weight;
2030 *period = LOAD_AVG_MAX;
Rik van Riel7e2703e2014-01-27 17:03:45 -05002031 }
2032
2033 p->last_sum_exec_runtime = runtime;
2034 p->last_task_numa_placement = now;
2035
2036 return delta;
2037}
2038
Rik van Riel54009412014-10-17 03:29:53 -04002039/*
2040 * Determine the preferred nid for a task in a numa_group. This needs to
2041 * be done in a way that produces consistent results with group_weight,
2042 * otherwise workloads might not converge.
2043 */
2044static int preferred_group_nid(struct task_struct *p, int nid)
2045{
2046 nodemask_t nodes;
2047 int dist;
2048
2049 /* Direct connections between all NUMA nodes. */
2050 if (sched_numa_topology_type == NUMA_DIRECT)
2051 return nid;
2052
2053 /*
2054 * On a system with glueless mesh NUMA topology, group_weight
2055 * scores nodes according to the number of NUMA hinting faults on
2056 * both the node itself, and on nearby nodes.
2057 */
2058 if (sched_numa_topology_type == NUMA_GLUELESS_MESH) {
2059 unsigned long score, max_score = 0;
2060 int node, max_node = nid;
2061
2062 dist = sched_max_numa_distance;
2063
2064 for_each_online_node(node) {
2065 score = group_weight(p, node, dist);
2066 if (score > max_score) {
2067 max_score = score;
2068 max_node = node;
2069 }
2070 }
2071 return max_node;
2072 }
2073
2074 /*
2075 * Finding the preferred nid in a system with NUMA backplane
2076 * interconnect topology is more involved. The goal is to locate
2077 * tasks from numa_groups near each other in the system, and
2078 * untangle workloads from different sides of the system. This requires
2079 * searching down the hierarchy of node groups, recursively searching
2080 * inside the highest scoring group of nodes. The nodemask tricks
2081 * keep the complexity of the search down.
2082 */
2083 nodes = node_online_map;
2084 for (dist = sched_max_numa_distance; dist > LOCAL_DISTANCE; dist--) {
2085 unsigned long max_faults = 0;
Jan Beulich81907472015-01-23 08:25:38 +00002086 nodemask_t max_group = NODE_MASK_NONE;
Rik van Riel54009412014-10-17 03:29:53 -04002087 int a, b;
2088
2089 /* Are there nodes at this distance from each other? */
2090 if (!find_numa_distance(dist))
2091 continue;
2092
2093 for_each_node_mask(a, nodes) {
2094 unsigned long faults = 0;
2095 nodemask_t this_group;
2096 nodes_clear(this_group);
2097
2098 /* Sum group's NUMA faults; includes a==b case. */
2099 for_each_node_mask(b, nodes) {
2100 if (node_distance(a, b) < dist) {
2101 faults += group_faults(p, b);
2102 node_set(b, this_group);
2103 node_clear(b, nodes);
2104 }
2105 }
2106
2107 /* Remember the top group. */
2108 if (faults > max_faults) {
2109 max_faults = faults;
2110 max_group = this_group;
2111 /*
2112 * subtle: at the smallest distance there is
2113 * just one node left in each "group", the
2114 * winner is the preferred nid.
2115 */
2116 nid = a;
2117 }
2118 }
2119 /* Next round, evaluate the nodes within max_group. */
Jan Beulich890a5402015-02-09 12:30:00 +01002120 if (!max_faults)
2121 break;
Rik van Riel54009412014-10-17 03:29:53 -04002122 nodes = max_group;
2123 }
2124 return nid;
2125}
2126
Peter Zijlstracbee9f82012-10-25 14:16:43 +02002127static void task_numa_placement(struct task_struct *p)
2128{
Mel Gorman83e1d2c2013-10-07 11:29:27 +01002129 int seq, nid, max_nid = -1, max_group_nid = -1;
2130 unsigned long max_faults = 0, max_group_faults = 0;
Rik van Riel04bb2f92013-10-07 11:29:36 +01002131 unsigned long fault_types[2] = { 0, 0 };
Rik van Riel7e2703e2014-01-27 17:03:45 -05002132 unsigned long total_faults;
2133 u64 runtime, period;
Mel Gorman7dbd13e2013-10-07 11:29:29 +01002134 spinlock_t *group_lock = NULL;
Peter Zijlstracbee9f82012-10-25 14:16:43 +02002135
Jason Low7e5a2c12015-04-30 17:28:14 -07002136 /*
2137 * The p->mm->numa_scan_seq field gets updated without
2138 * exclusive access. Use READ_ONCE() here to ensure
2139 * that the field is read in a single access:
2140 */
Jason Low316c1608d2015-04-28 13:00:20 -07002141 seq = READ_ONCE(p->mm->numa_scan_seq);
Peter Zijlstracbee9f82012-10-25 14:16:43 +02002142 if (p->numa_scan_seq == seq)
2143 return;
2144 p->numa_scan_seq = seq;
Mel Gorman598f0ec2013-10-07 11:28:55 +01002145 p->numa_scan_period_max = task_scan_max(p);
Peter Zijlstracbee9f82012-10-25 14:16:43 +02002146
Rik van Riel7e2703e2014-01-27 17:03:45 -05002147 total_faults = p->numa_faults_locality[0] +
2148 p->numa_faults_locality[1];
2149 runtime = numa_get_avg_runtime(p, &period);
2150
Mel Gorman7dbd13e2013-10-07 11:29:29 +01002151 /* If the task is part of a group prevent parallel updates to group stats */
2152 if (p->numa_group) {
2153 group_lock = &p->numa_group->lock;
Mike Galbraith60e69ee2014-04-07 10:55:15 +02002154 spin_lock_irq(group_lock);
Mel Gorman7dbd13e2013-10-07 11:29:29 +01002155 }
2156
Mel Gorman688b7582013-10-07 11:28:58 +01002157 /* Find the node with the highest number of faults */
2158 for_each_online_node(nid) {
Iulia Manda44dba3d2014-10-31 02:13:31 +02002159 /* Keep track of the offsets in numa_faults array */
2160 int mem_idx, membuf_idx, cpu_idx, cpubuf_idx;
Mel Gorman83e1d2c2013-10-07 11:29:27 +01002161 unsigned long faults = 0, group_faults = 0;
Iulia Manda44dba3d2014-10-31 02:13:31 +02002162 int priv;
Mel Gorman745d6142013-10-07 11:28:59 +01002163
Rik van Rielbe1e4e72014-01-27 17:03:48 -05002164 for (priv = 0; priv < NR_NUMA_HINT_FAULT_TYPES; priv++) {
Rik van Riel7e2703e2014-01-27 17:03:45 -05002165 long diff, f_diff, f_weight;
Peter Zijlstra8c8a7432013-10-07 11:29:21 +01002166
Iulia Manda44dba3d2014-10-31 02:13:31 +02002167 mem_idx = task_faults_idx(NUMA_MEM, nid, priv);
2168 membuf_idx = task_faults_idx(NUMA_MEMBUF, nid, priv);
2169 cpu_idx = task_faults_idx(NUMA_CPU, nid, priv);
2170 cpubuf_idx = task_faults_idx(NUMA_CPUBUF, nid, priv);
Mel Gorman745d6142013-10-07 11:28:59 +01002171
Mel Gormanac8e8952013-10-07 11:29:03 +01002172 /* Decay existing window, copy faults since last scan */
Iulia Manda44dba3d2014-10-31 02:13:31 +02002173 diff = p->numa_faults[membuf_idx] - p->numa_faults[mem_idx] / 2;
2174 fault_types[priv] += p->numa_faults[membuf_idx];
2175 p->numa_faults[membuf_idx] = 0;
Mel Gormanfb13c7e2013-10-07 11:29:17 +01002176
Rik van Riel7e2703e2014-01-27 17:03:45 -05002177 /*
2178 * Normalize the faults_from, so all tasks in a group
2179 * count according to CPU use, instead of by the raw
2180 * number of faults. Tasks with little runtime have
2181 * little over-all impact on throughput, and thus their
2182 * faults are less important.
2183 */
2184 f_weight = div64_u64(runtime << 16, period + 1);
Iulia Manda44dba3d2014-10-31 02:13:31 +02002185 f_weight = (f_weight * p->numa_faults[cpubuf_idx]) /
Rik van Riel7e2703e2014-01-27 17:03:45 -05002186 (total_faults + 1);
Iulia Manda44dba3d2014-10-31 02:13:31 +02002187 f_diff = f_weight - p->numa_faults[cpu_idx] / 2;
2188 p->numa_faults[cpubuf_idx] = 0;
Rik van Riel50ec8a42014-01-27 17:03:42 -05002189
Iulia Manda44dba3d2014-10-31 02:13:31 +02002190 p->numa_faults[mem_idx] += diff;
2191 p->numa_faults[cpu_idx] += f_diff;
2192 faults += p->numa_faults[mem_idx];
Mel Gorman83e1d2c2013-10-07 11:29:27 +01002193 p->total_numa_faults += diff;
Peter Zijlstra8c8a7432013-10-07 11:29:21 +01002194 if (p->numa_group) {
Iulia Manda44dba3d2014-10-31 02:13:31 +02002195 /*
2196 * safe because we can only change our own group
2197 *
2198 * mem_idx represents the offset for a given
2199 * nid and priv in a specific region because it
2200 * is at the beginning of the numa_faults array.
2201 */
2202 p->numa_group->faults[mem_idx] += diff;
2203 p->numa_group->faults_cpu[mem_idx] += f_diff;
Mel Gorman989348b2013-10-07 11:29:40 +01002204 p->numa_group->total_faults += diff;
Iulia Manda44dba3d2014-10-31 02:13:31 +02002205 group_faults += p->numa_group->faults[mem_idx];
Peter Zijlstra8c8a7432013-10-07 11:29:21 +01002206 }
Mel Gormanac8e8952013-10-07 11:29:03 +01002207 }
2208
Mel Gorman688b7582013-10-07 11:28:58 +01002209 if (faults > max_faults) {
2210 max_faults = faults;
2211 max_nid = nid;
2212 }
Mel Gorman83e1d2c2013-10-07 11:29:27 +01002213
2214 if (group_faults > max_group_faults) {
2215 max_group_faults = group_faults;
2216 max_group_nid = nid;
2217 }
2218 }
2219
Rik van Riel04bb2f92013-10-07 11:29:36 +01002220 update_task_scan_period(p, fault_types[0], fault_types[1]);
2221
Mel Gorman7dbd13e2013-10-07 11:29:29 +01002222 if (p->numa_group) {
Rik van Riel4142c3e2016-01-25 17:07:39 -05002223 numa_group_count_active_nodes(p->numa_group);
Mike Galbraith60e69ee2014-04-07 10:55:15 +02002224 spin_unlock_irq(group_lock);
Rik van Riel54009412014-10-17 03:29:53 -04002225 max_nid = preferred_group_nid(p, max_group_nid);
Mel Gorman688b7582013-10-07 11:28:58 +01002226 }
2227
Rik van Rielbb97fc32014-06-04 16:33:15 -04002228 if (max_faults) {
2229 /* Set the new preferred node */
2230 if (max_nid != p->numa_preferred_nid)
2231 sched_setnuma(p, max_nid);
2232
2233 if (task_node(p) != p->numa_preferred_nid)
2234 numa_migrate_preferred(p);
Mel Gorman3a7053b2013-10-07 11:29:00 +01002235 }
Peter Zijlstracbee9f82012-10-25 14:16:43 +02002236}
2237
Peter Zijlstra8c8a7432013-10-07 11:29:21 +01002238static inline int get_numa_group(struct numa_group *grp)
2239{
2240 return atomic_inc_not_zero(&grp->refcount);
2241}
2242
2243static inline void put_numa_group(struct numa_group *grp)
2244{
2245 if (atomic_dec_and_test(&grp->refcount))
2246 kfree_rcu(grp, rcu);
2247}
2248
Mel Gorman3e6a9412013-10-07 11:29:35 +01002249static void task_numa_group(struct task_struct *p, int cpupid, int flags,
2250 int *priv)
Peter Zijlstra8c8a7432013-10-07 11:29:21 +01002251{
2252 struct numa_group *grp, *my_grp;
2253 struct task_struct *tsk;
2254 bool join = false;
2255 int cpu = cpupid_to_cpu(cpupid);
2256 int i;
2257
2258 if (unlikely(!p->numa_group)) {
2259 unsigned int size = sizeof(struct numa_group) +
Rik van Riel50ec8a42014-01-27 17:03:42 -05002260 4*nr_node_ids*sizeof(unsigned long);
Peter Zijlstra8c8a7432013-10-07 11:29:21 +01002261
2262 grp = kzalloc(size, GFP_KERNEL | __GFP_NOWARN);
2263 if (!grp)
2264 return;
2265
2266 atomic_set(&grp->refcount, 1);
Rik van Riel4142c3e2016-01-25 17:07:39 -05002267 grp->active_nodes = 1;
2268 grp->max_faults_cpu = 0;
Peter Zijlstra8c8a7432013-10-07 11:29:21 +01002269 spin_lock_init(&grp->lock);
Mel Gormane29cf082013-10-07 11:29:22 +01002270 grp->gid = p->pid;
Rik van Riel50ec8a42014-01-27 17:03:42 -05002271 /* Second half of the array tracks nids where faults happen */
Rik van Rielbe1e4e72014-01-27 17:03:48 -05002272 grp->faults_cpu = grp->faults + NR_NUMA_HINT_FAULT_TYPES *
2273 nr_node_ids;
Peter Zijlstra8c8a7432013-10-07 11:29:21 +01002274
Rik van Rielbe1e4e72014-01-27 17:03:48 -05002275 for (i = 0; i < NR_NUMA_HINT_FAULT_STATS * nr_node_ids; i++)
Iulia Manda44dba3d2014-10-31 02:13:31 +02002276 grp->faults[i] = p->numa_faults[i];
Peter Zijlstra8c8a7432013-10-07 11:29:21 +01002277
Mel Gorman989348b2013-10-07 11:29:40 +01002278 grp->total_faults = p->total_numa_faults;
Mel Gorman83e1d2c2013-10-07 11:29:27 +01002279
Peter Zijlstra8c8a7432013-10-07 11:29:21 +01002280 grp->nr_tasks++;
2281 rcu_assign_pointer(p->numa_group, grp);
2282 }
2283
2284 rcu_read_lock();
Jason Low316c1608d2015-04-28 13:00:20 -07002285 tsk = READ_ONCE(cpu_rq(cpu)->curr);
Peter Zijlstra8c8a7432013-10-07 11:29:21 +01002286
2287 if (!cpupid_match_pid(tsk, cpupid))
Peter Zijlstra33547812013-10-09 10:24:48 +02002288 goto no_join;
Peter Zijlstra8c8a7432013-10-07 11:29:21 +01002289
2290 grp = rcu_dereference(tsk->numa_group);
2291 if (!grp)
Peter Zijlstra33547812013-10-09 10:24:48 +02002292 goto no_join;
Peter Zijlstra8c8a7432013-10-07 11:29:21 +01002293
2294 my_grp = p->numa_group;
2295 if (grp == my_grp)
Peter Zijlstra33547812013-10-09 10:24:48 +02002296 goto no_join;
Peter Zijlstra8c8a7432013-10-07 11:29:21 +01002297
2298 /*
2299 * Only join the other group if its bigger; if we're the bigger group,
2300 * the other task will join us.
2301 */
2302 if (my_grp->nr_tasks > grp->nr_tasks)
Peter Zijlstra33547812013-10-09 10:24:48 +02002303 goto no_join;
Peter Zijlstra8c8a7432013-10-07 11:29:21 +01002304
2305 /*
2306 * Tie-break on the grp address.
2307 */
2308 if (my_grp->nr_tasks == grp->nr_tasks && my_grp > grp)
Peter Zijlstra33547812013-10-09 10:24:48 +02002309 goto no_join;
Peter Zijlstra8c8a7432013-10-07 11:29:21 +01002310
Rik van Rieldabe1d92013-10-07 11:29:34 +01002311 /* Always join threads in the same process. */
2312 if (tsk->mm == current->mm)
2313 join = true;
Peter Zijlstra8c8a7432013-10-07 11:29:21 +01002314
Rik van Rieldabe1d92013-10-07 11:29:34 +01002315 /* Simple filter to avoid false positives due to PID collisions */
2316 if (flags & TNF_SHARED)
2317 join = true;
2318
Mel Gorman3e6a9412013-10-07 11:29:35 +01002319 /* Update priv based on whether false sharing was detected */
2320 *priv = !join;
2321
Rik van Rieldabe1d92013-10-07 11:29:34 +01002322 if (join && !get_numa_group(grp))
Peter Zijlstra33547812013-10-09 10:24:48 +02002323 goto no_join;
Peter Zijlstra8c8a7432013-10-07 11:29:21 +01002324
Peter Zijlstra8c8a7432013-10-07 11:29:21 +01002325 rcu_read_unlock();
2326
2327 if (!join)
2328 return;
2329
Mike Galbraith60e69ee2014-04-07 10:55:15 +02002330 BUG_ON(irqs_disabled());
2331 double_lock_irq(&my_grp->lock, &grp->lock);
Peter Zijlstra8c8a7432013-10-07 11:29:21 +01002332
Rik van Rielbe1e4e72014-01-27 17:03:48 -05002333 for (i = 0; i < NR_NUMA_HINT_FAULT_STATS * nr_node_ids; i++) {
Iulia Manda44dba3d2014-10-31 02:13:31 +02002334 my_grp->faults[i] -= p->numa_faults[i];
2335 grp->faults[i] += p->numa_faults[i];
Mel Gorman989348b2013-10-07 11:29:40 +01002336 }
2337 my_grp->total_faults -= p->total_numa_faults;
2338 grp->total_faults += p->total_numa_faults;
2339
Peter Zijlstra8c8a7432013-10-07 11:29:21 +01002340 my_grp->nr_tasks--;
2341 grp->nr_tasks++;
2342
2343 spin_unlock(&my_grp->lock);
Mike Galbraith60e69ee2014-04-07 10:55:15 +02002344 spin_unlock_irq(&grp->lock);
Peter Zijlstra8c8a7432013-10-07 11:29:21 +01002345
2346 rcu_assign_pointer(p->numa_group, grp);
2347
2348 put_numa_group(my_grp);
Peter Zijlstra33547812013-10-09 10:24:48 +02002349 return;
2350
2351no_join:
2352 rcu_read_unlock();
2353 return;
Peter Zijlstra8c8a7432013-10-07 11:29:21 +01002354}
2355
2356void task_numa_free(struct task_struct *p)
2357{
2358 struct numa_group *grp = p->numa_group;
Iulia Manda44dba3d2014-10-31 02:13:31 +02002359 void *numa_faults = p->numa_faults;
Steven Rostedte9dd6852014-05-27 17:02:04 -04002360 unsigned long flags;
2361 int i;
Peter Zijlstra8c8a7432013-10-07 11:29:21 +01002362
2363 if (grp) {
Steven Rostedte9dd6852014-05-27 17:02:04 -04002364 spin_lock_irqsave(&grp->lock, flags);
Rik van Rielbe1e4e72014-01-27 17:03:48 -05002365 for (i = 0; i < NR_NUMA_HINT_FAULT_STATS * nr_node_ids; i++)
Iulia Manda44dba3d2014-10-31 02:13:31 +02002366 grp->faults[i] -= p->numa_faults[i];
Mel Gorman989348b2013-10-07 11:29:40 +01002367 grp->total_faults -= p->total_numa_faults;
2368
Peter Zijlstra8c8a7432013-10-07 11:29:21 +01002369 grp->nr_tasks--;
Steven Rostedte9dd6852014-05-27 17:02:04 -04002370 spin_unlock_irqrestore(&grp->lock, flags);
Andreea-Cristina Bernat35b123e2014-08-22 17:50:43 +03002371 RCU_INIT_POINTER(p->numa_group, NULL);
Peter Zijlstra8c8a7432013-10-07 11:29:21 +01002372 put_numa_group(grp);
2373 }
2374
Iulia Manda44dba3d2014-10-31 02:13:31 +02002375 p->numa_faults = NULL;
Rik van Riel82727012013-10-07 11:29:28 +01002376 kfree(numa_faults);
Peter Zijlstra8c8a7432013-10-07 11:29:21 +01002377}
2378
Peter Zijlstracbee9f82012-10-25 14:16:43 +02002379/*
2380 * Got a PROT_NONE fault for a page on @node.
2381 */
Rik van Riel58b46da2014-01-27 17:03:47 -05002382void task_numa_fault(int last_cpupid, int mem_node, int pages, int flags)
Peter Zijlstracbee9f82012-10-25 14:16:43 +02002383{
2384 struct task_struct *p = current;
Peter Zijlstra6688cc02013-10-07 11:29:24 +01002385 bool migrated = flags & TNF_MIGRATED;
Rik van Riel58b46da2014-01-27 17:03:47 -05002386 int cpu_node = task_node(current);
Rik van Riel792568e2014-04-11 13:00:27 -04002387 int local = !!(flags & TNF_FAULT_LOCAL);
Rik van Riel4142c3e2016-01-25 17:07:39 -05002388 struct numa_group *ng;
Mel Gormanac8e8952013-10-07 11:29:03 +01002389 int priv;
Peter Zijlstracbee9f82012-10-25 14:16:43 +02002390
Srikar Dronamraju2a595722015-08-11 21:54:21 +05302391 if (!static_branch_likely(&sched_numa_balancing))
Mel Gorman1a687c22012-11-22 11:16:36 +00002392 return;
2393
Mel Gorman9ff1d9f2013-10-07 11:29:04 +01002394 /* for example, ksmd faulting in a user's mm */
2395 if (!p->mm)
2396 return;
2397
Mel Gormanf809ca92013-10-07 11:28:57 +01002398 /* Allocate buffer to track faults on a per-node basis */
Iulia Manda44dba3d2014-10-31 02:13:31 +02002399 if (unlikely(!p->numa_faults)) {
2400 int size = sizeof(*p->numa_faults) *
Rik van Rielbe1e4e72014-01-27 17:03:48 -05002401 NR_NUMA_HINT_FAULT_BUCKETS * nr_node_ids;
Mel Gormanf809ca92013-10-07 11:28:57 +01002402
Iulia Manda44dba3d2014-10-31 02:13:31 +02002403 p->numa_faults = kzalloc(size, GFP_KERNEL|__GFP_NOWARN);
2404 if (!p->numa_faults)
Mel Gormanf809ca92013-10-07 11:28:57 +01002405 return;
Mel Gorman745d6142013-10-07 11:28:59 +01002406
Mel Gorman83e1d2c2013-10-07 11:29:27 +01002407 p->total_numa_faults = 0;
Rik van Riel04bb2f92013-10-07 11:29:36 +01002408 memset(p->numa_faults_locality, 0, sizeof(p->numa_faults_locality));
Mel Gormanf809ca92013-10-07 11:28:57 +01002409 }
Peter Zijlstracbee9f82012-10-25 14:16:43 +02002410
Mel Gormanfb003b82012-11-15 09:01:14 +00002411 /*
Peter Zijlstra8c8a7432013-10-07 11:29:21 +01002412 * First accesses are treated as private, otherwise consider accesses
2413 * to be private if the accessing pid has not changed
2414 */
2415 if (unlikely(last_cpupid == (-1 & LAST_CPUPID_MASK))) {
2416 priv = 1;
2417 } else {
2418 priv = cpupid_match_pid(p, last_cpupid);
Peter Zijlstra6688cc02013-10-07 11:29:24 +01002419 if (!priv && !(flags & TNF_NO_GROUP))
Mel Gorman3e6a9412013-10-07 11:29:35 +01002420 task_numa_group(p, last_cpupid, flags, &priv);
Peter Zijlstra8c8a7432013-10-07 11:29:21 +01002421 }
2422
Rik van Riel792568e2014-04-11 13:00:27 -04002423 /*
2424 * If a workload spans multiple NUMA nodes, a shared fault that
2425 * occurs wholly within the set of nodes that the workload is
2426 * actively using should be counted as local. This allows the
2427 * scan rate to slow down when a workload has settled down.
2428 */
Rik van Riel4142c3e2016-01-25 17:07:39 -05002429 ng = p->numa_group;
2430 if (!priv && !local && ng && ng->active_nodes > 1 &&
2431 numa_is_active_node(cpu_node, ng) &&
2432 numa_is_active_node(mem_node, ng))
Rik van Riel792568e2014-04-11 13:00:27 -04002433 local = 1;
2434
Peter Zijlstracbee9f82012-10-25 14:16:43 +02002435 task_numa_placement(p);
Mel Gormanf809ca92013-10-07 11:28:57 +01002436
Rik van Riel2739d3e2013-10-07 11:29:41 +01002437 /*
2438 * Retry task to preferred node migration periodically, in case it
2439 * case it previously failed, or the scheduler moved us.
2440 */
2441 if (time_after(jiffies, p->numa_migrate_retry))
Mel Gorman6b9a7462013-10-07 11:29:11 +01002442 numa_migrate_preferred(p);
2443
Ingo Molnarb32e86b2013-10-07 11:29:30 +01002444 if (migrated)
2445 p->numa_pages_migrated += pages;
Mel Gorman074c2382015-03-25 15:55:42 -07002446 if (flags & TNF_MIGRATE_FAIL)
2447 p->numa_faults_locality[2] += pages;
Ingo Molnarb32e86b2013-10-07 11:29:30 +01002448
Iulia Manda44dba3d2014-10-31 02:13:31 +02002449 p->numa_faults[task_faults_idx(NUMA_MEMBUF, mem_node, priv)] += pages;
2450 p->numa_faults[task_faults_idx(NUMA_CPUBUF, cpu_node, priv)] += pages;
Rik van Riel792568e2014-04-11 13:00:27 -04002451 p->numa_faults_locality[local] += pages;
Peter Zijlstracbee9f82012-10-25 14:16:43 +02002452}
2453
Peter Zijlstra6e5fb222012-10-25 14:16:45 +02002454static void reset_ptenuma_scan(struct task_struct *p)
2455{
Jason Low7e5a2c12015-04-30 17:28:14 -07002456 /*
2457 * We only did a read acquisition of the mmap sem, so
2458 * p->mm->numa_scan_seq is written to without exclusive access
2459 * and the update is not guaranteed to be atomic. That's not
2460 * much of an issue though, since this is just used for
2461 * statistical sampling. Use READ_ONCE/WRITE_ONCE, which are not
2462 * expensive, to avoid any form of compiler optimizations:
2463 */
Jason Low316c1608d2015-04-28 13:00:20 -07002464 WRITE_ONCE(p->mm->numa_scan_seq, READ_ONCE(p->mm->numa_scan_seq) + 1);
Peter Zijlstra6e5fb222012-10-25 14:16:45 +02002465 p->mm->numa_scan_offset = 0;
2466}
2467
Peter Zijlstracbee9f82012-10-25 14:16:43 +02002468/*
2469 * The expensive part of numa migration is done from task_work context.
2470 * Triggered from task_tick_numa().
2471 */
2472void task_numa_work(struct callback_head *work)
2473{
2474 unsigned long migrate, next_scan, now = jiffies;
2475 struct task_struct *p = current;
2476 struct mm_struct *mm = p->mm;
Rik van Riel51170842015-11-05 15:56:23 -05002477 u64 runtime = p->se.sum_exec_runtime;
Peter Zijlstra6e5fb222012-10-25 14:16:45 +02002478 struct vm_area_struct *vma;
Mel Gorman9f406042012-11-14 18:34:32 +00002479 unsigned long start, end;
Mel Gorman598f0ec2013-10-07 11:28:55 +01002480 unsigned long nr_pte_updates = 0;
Rik van Riel4620f8c2015-09-11 09:00:27 -04002481 long pages, virtpages;
Peter Zijlstracbee9f82012-10-25 14:16:43 +02002482
Peter Zijlstra9148a3a2016-09-20 22:34:51 +02002483 SCHED_WARN_ON(p != container_of(work, struct task_struct, numa_work));
Peter Zijlstracbee9f82012-10-25 14:16:43 +02002484
2485 work->next = work; /* protect against double add */
2486 /*
2487 * Who cares about NUMA placement when they're dying.
2488 *
2489 * NOTE: make sure not to dereference p->mm before this check,
2490 * exit_task_work() happens _after_ exit_mm() so we could be called
2491 * without p->mm even though we still had it when we enqueued this
2492 * work.
2493 */
2494 if (p->flags & PF_EXITING)
2495 return;
2496
Mel Gorman930aa172013-10-07 11:29:37 +01002497 if (!mm->numa_next_scan) {
Mel Gorman7e8d16b2013-10-07 11:28:54 +01002498 mm->numa_next_scan = now +
2499 msecs_to_jiffies(sysctl_numa_balancing_scan_delay);
Mel Gormanb8593bf2012-11-21 01:18:23 +00002500 }
2501
2502 /*
Peter Zijlstracbee9f82012-10-25 14:16:43 +02002503 * Enforce maximal scan/migration frequency..
2504 */
2505 migrate = mm->numa_next_scan;
2506 if (time_before(now, migrate))
2507 return;
2508
Mel Gorman598f0ec2013-10-07 11:28:55 +01002509 if (p->numa_scan_period == 0) {
2510 p->numa_scan_period_max = task_scan_max(p);
Rik van Rielb5dd77c2017-07-31 15:28:47 -04002511 p->numa_scan_period = task_scan_start(p);
Mel Gorman598f0ec2013-10-07 11:28:55 +01002512 }
Peter Zijlstracbee9f82012-10-25 14:16:43 +02002513
Mel Gormanfb003b82012-11-15 09:01:14 +00002514 next_scan = now + msecs_to_jiffies(p->numa_scan_period);
Peter Zijlstracbee9f82012-10-25 14:16:43 +02002515 if (cmpxchg(&mm->numa_next_scan, migrate, next_scan) != migrate)
2516 return;
2517
Mel Gormane14808b2012-11-19 10:59:15 +00002518 /*
Peter Zijlstra19a78d12013-10-07 11:28:51 +01002519 * Delay this task enough that another task of this mm will likely win
2520 * the next time around.
2521 */
2522 p->node_stamp += 2 * TICK_NSEC;
2523
Mel Gorman9f406042012-11-14 18:34:32 +00002524 start = mm->numa_scan_offset;
2525 pages = sysctl_numa_balancing_scan_size;
2526 pages <<= 20 - PAGE_SHIFT; /* MB in pages */
Rik van Riel4620f8c2015-09-11 09:00:27 -04002527 virtpages = pages * 8; /* Scan up to this much virtual space */
Mel Gorman9f406042012-11-14 18:34:32 +00002528 if (!pages)
2529 return;
Peter Zijlstracbee9f82012-10-25 14:16:43 +02002530
Rik van Riel4620f8c2015-09-11 09:00:27 -04002531
Vlastimil Babka8655d542017-05-15 15:13:16 +02002532 if (!down_read_trylock(&mm->mmap_sem))
2533 return;
Mel Gorman9f406042012-11-14 18:34:32 +00002534 vma = find_vma(mm, start);
Peter Zijlstra6e5fb222012-10-25 14:16:45 +02002535 if (!vma) {
2536 reset_ptenuma_scan(p);
Mel Gorman9f406042012-11-14 18:34:32 +00002537 start = 0;
Peter Zijlstra6e5fb222012-10-25 14:16:45 +02002538 vma = mm->mmap;
Peter Zijlstracbee9f82012-10-25 14:16:43 +02002539 }
Mel Gorman9f406042012-11-14 18:34:32 +00002540 for (; vma; vma = vma->vm_next) {
Naoya Horiguchi6b79c572015-04-07 14:26:47 -07002541 if (!vma_migratable(vma) || !vma_policy_mof(vma) ||
Mel Gorman8e76d4e2015-06-10 11:15:00 -07002542 is_vm_hugetlb_page(vma) || (vma->vm_flags & VM_MIXEDMAP)) {
Peter Zijlstra6e5fb222012-10-25 14:16:45 +02002543 continue;
Naoya Horiguchi6b79c572015-04-07 14:26:47 -07002544 }
Peter Zijlstra6e5fb222012-10-25 14:16:45 +02002545
Mel Gorman4591ce4f2013-10-07 11:29:13 +01002546 /*
2547 * Shared library pages mapped by multiple processes are not
2548 * migrated as it is expected they are cache replicated. Avoid
2549 * hinting faults in read-only file-backed mappings or the vdso
2550 * as migrating the pages will be of marginal benefit.
2551 */
2552 if (!vma->vm_mm ||
2553 (vma->vm_file && (vma->vm_flags & (VM_READ|VM_WRITE)) == (VM_READ)))
2554 continue;
2555
Mel Gorman3c67f472013-12-18 17:08:40 -08002556 /*
2557 * Skip inaccessible VMAs to avoid any confusion between
2558 * PROT_NONE and NUMA hinting ptes
2559 */
2560 if (!(vma->vm_flags & (VM_READ | VM_EXEC | VM_WRITE)))
2561 continue;
2562
Mel Gorman9f406042012-11-14 18:34:32 +00002563 do {
2564 start = max(start, vma->vm_start);
2565 end = ALIGN(start + (pages << PAGE_SHIFT), HPAGE_SIZE);
2566 end = min(end, vma->vm_end);
Rik van Riel4620f8c2015-09-11 09:00:27 -04002567 nr_pte_updates = change_prot_numa(vma, start, end);
Mel Gorman598f0ec2013-10-07 11:28:55 +01002568
2569 /*
Rik van Riel4620f8c2015-09-11 09:00:27 -04002570 * Try to scan sysctl_numa_balancing_size worth of
2571 * hpages that have at least one present PTE that
2572 * is not already pte-numa. If the VMA contains
2573 * areas that are unused or already full of prot_numa
2574 * PTEs, scan up to virtpages, to skip through those
2575 * areas faster.
Mel Gorman598f0ec2013-10-07 11:28:55 +01002576 */
2577 if (nr_pte_updates)
2578 pages -= (end - start) >> PAGE_SHIFT;
Rik van Riel4620f8c2015-09-11 09:00:27 -04002579 virtpages -= (end - start) >> PAGE_SHIFT;
Peter Zijlstra6e5fb222012-10-25 14:16:45 +02002580
Mel Gorman9f406042012-11-14 18:34:32 +00002581 start = end;
Rik van Riel4620f8c2015-09-11 09:00:27 -04002582 if (pages <= 0 || virtpages <= 0)
Mel Gorman9f406042012-11-14 18:34:32 +00002583 goto out;
Rik van Riel3cf19622014-02-18 17:12:44 -05002584
2585 cond_resched();
Mel Gorman9f406042012-11-14 18:34:32 +00002586 } while (end != vma->vm_end);
Peter Zijlstra6e5fb222012-10-25 14:16:45 +02002587 }
2588
Mel Gorman9f406042012-11-14 18:34:32 +00002589out:
Peter Zijlstra6e5fb222012-10-25 14:16:45 +02002590 /*
Peter Zijlstrac69307d2013-10-07 11:28:41 +01002591 * It is possible to reach the end of the VMA list but the last few
2592 * VMAs are not guaranteed to the vma_migratable. If they are not, we
2593 * would find the !migratable VMA on the next scan but not reset the
2594 * scanner to the start so check it now.
Peter Zijlstra6e5fb222012-10-25 14:16:45 +02002595 */
2596 if (vma)
Mel Gorman9f406042012-11-14 18:34:32 +00002597 mm->numa_scan_offset = start;
Peter Zijlstra6e5fb222012-10-25 14:16:45 +02002598 else
2599 reset_ptenuma_scan(p);
2600 up_read(&mm->mmap_sem);
Rik van Riel51170842015-11-05 15:56:23 -05002601
2602 /*
2603 * Make sure tasks use at least 32x as much time to run other code
2604 * than they used here, to limit NUMA PTE scanning overhead to 3% max.
2605 * Usually update_task_scan_period slows down scanning enough; on an
2606 * overloaded system we need to limit overhead on a per task basis.
2607 */
2608 if (unlikely(p->se.sum_exec_runtime != runtime)) {
2609 u64 diff = p->se.sum_exec_runtime - runtime;
2610 p->node_stamp += 32 * diff;
2611 }
Peter Zijlstracbee9f82012-10-25 14:16:43 +02002612}
2613
2614/*
2615 * Drive the periodic memory faults..
2616 */
2617void task_tick_numa(struct rq *rq, struct task_struct *curr)
2618{
2619 struct callback_head *work = &curr->numa_work;
2620 u64 period, now;
2621
2622 /*
2623 * We don't care about NUMA placement if we don't have memory.
2624 */
2625 if (!curr->mm || (curr->flags & PF_EXITING) || work->next != work)
2626 return;
2627
2628 /*
2629 * Using runtime rather than walltime has the dual advantage that
2630 * we (mostly) drive the selection from busy threads and that the
2631 * task needs to have done some actual work before we bother with
2632 * NUMA placement.
2633 */
2634 now = curr->se.sum_exec_runtime;
2635 period = (u64)curr->numa_scan_period * NSEC_PER_MSEC;
2636
Rik van Riel25b3e5a2015-11-05 15:56:22 -05002637 if (now > curr->node_stamp + period) {
Peter Zijlstra4b96a292012-10-25 14:16:47 +02002638 if (!curr->node_stamp)
Rik van Rielb5dd77c2017-07-31 15:28:47 -04002639 curr->numa_scan_period = task_scan_start(curr);
Peter Zijlstra19a78d12013-10-07 11:28:51 +01002640 curr->node_stamp += period;
Peter Zijlstracbee9f82012-10-25 14:16:43 +02002641
2642 if (!time_before(jiffies, curr->mm->numa_next_scan)) {
2643 init_task_work(work, task_numa_work); /* TODO: move this into sched_fork() */
2644 task_work_add(curr, work, true);
2645 }
2646 }
2647}
Rik van Riel3fed3822017-06-23 12:55:29 -04002648
Peter Zijlstracbee9f82012-10-25 14:16:43 +02002649#else
2650static void task_tick_numa(struct rq *rq, struct task_struct *curr)
2651{
2652}
Peter Zijlstra0ec8aa02013-10-07 11:29:33 +01002653
2654static inline void account_numa_enqueue(struct rq *rq, struct task_struct *p)
2655{
2656}
2657
2658static inline void account_numa_dequeue(struct rq *rq, struct task_struct *p)
2659{
2660}
Rik van Riel3fed3822017-06-23 12:55:29 -04002661
Peter Zijlstracbee9f82012-10-25 14:16:43 +02002662#endif /* CONFIG_NUMA_BALANCING */
2663
Dmitry Adamushko30cfdcf2007-10-15 17:00:07 +02002664static void
2665account_entity_enqueue(struct cfs_rq *cfs_rq, struct sched_entity *se)
2666{
2667 update_load_add(&cfs_rq->load, se->load.weight);
Peter Zijlstrac09595f2008-06-27 13:41:14 +02002668 if (!parent_entity(se))
Peter Zijlstra029632f2011-10-25 10:00:11 +02002669 update_load_add(&rq_of(cfs_rq)->load, se->load.weight);
Peter Zijlstra367456c2012-02-20 21:49:09 +01002670#ifdef CONFIG_SMP
Peter Zijlstra0ec8aa02013-10-07 11:29:33 +01002671 if (entity_is_task(se)) {
2672 struct rq *rq = rq_of(cfs_rq);
2673
2674 account_numa_enqueue(rq, task_of(se));
2675 list_add(&se->group_node, &rq->cfs_tasks);
2676 }
Peter Zijlstra367456c2012-02-20 21:49:09 +01002677#endif
Dmitry Adamushko30cfdcf2007-10-15 17:00:07 +02002678 cfs_rq->nr_running++;
Dmitry Adamushko30cfdcf2007-10-15 17:00:07 +02002679}
2680
2681static void
2682account_entity_dequeue(struct cfs_rq *cfs_rq, struct sched_entity *se)
2683{
2684 update_load_sub(&cfs_rq->load, se->load.weight);
Peter Zijlstrac09595f2008-06-27 13:41:14 +02002685 if (!parent_entity(se))
Peter Zijlstra029632f2011-10-25 10:00:11 +02002686 update_load_sub(&rq_of(cfs_rq)->load, se->load.weight);
Tim Chenbfdb1982016-02-01 14:47:59 -08002687#ifdef CONFIG_SMP
Peter Zijlstra0ec8aa02013-10-07 11:29:33 +01002688 if (entity_is_task(se)) {
2689 account_numa_dequeue(rq_of(cfs_rq), task_of(se));
Bharata B Raob87f1722008-09-25 09:53:54 +05302690 list_del_init(&se->group_node);
Peter Zijlstra0ec8aa02013-10-07 11:29:33 +01002691 }
Tim Chenbfdb1982016-02-01 14:47:59 -08002692#endif
Dmitry Adamushko30cfdcf2007-10-15 17:00:07 +02002693 cfs_rq->nr_running--;
Dmitry Adamushko30cfdcf2007-10-15 17:00:07 +02002694}
2695
Yong Zhang3ff6dca2011-01-24 15:33:52 +08002696#ifdef CONFIG_FAIR_GROUP_SCHED
2697# ifdef CONFIG_SMP
Paul Turner6d5ab292011-01-21 20:45:01 -08002698static long calc_cfs_shares(struct cfs_rq *cfs_rq, struct task_group *tg)
Yong Zhang3ff6dca2011-01-24 15:33:52 +08002699{
Peter Zijlstracf5f0ac2011-10-13 16:52:28 +02002700 long tg_weight, load, shares;
Yong Zhang3ff6dca2011-01-24 15:33:52 +08002701
Peter Zijlstraea1dc6f2016-06-24 16:11:02 +02002702 /*
2703 * This really should be: cfs_rq->avg.load_avg, but instead we use
2704 * cfs_rq->load.weight, which is its upper bound. This helps ramp up
2705 * the shares for small weight interactive tasks.
2706 */
2707 load = scale_load_down(cfs_rq->load.weight);
2708
2709 tg_weight = atomic_long_read(&tg->load_avg);
2710
2711 /* Ensure tg_weight >= load */
2712 tg_weight -= cfs_rq->tg_load_avg_contrib;
2713 tg_weight += load;
Yong Zhang3ff6dca2011-01-24 15:33:52 +08002714
Yong Zhang3ff6dca2011-01-24 15:33:52 +08002715 shares = (tg->shares * load);
Peter Zijlstracf5f0ac2011-10-13 16:52:28 +02002716 if (tg_weight)
2717 shares /= tg_weight;
Yong Zhang3ff6dca2011-01-24 15:33:52 +08002718
Dietmar Eggemannb8fd8422017-01-11 11:29:47 +00002719 /*
2720 * MIN_SHARES has to be unscaled here to support per-CPU partitioning
2721 * of a group with small tg->shares value. It is a floor value which is
2722 * assigned as a minimum load.weight to the sched_entity representing
2723 * the group on a CPU.
2724 *
2725 * E.g. on 64-bit for a group with tg->shares of scale_load(15)=15*1024
2726 * on an 8-core system with 8 tasks each runnable on one CPU shares has
2727 * to be 15*1024*1/8=1920 instead of scale_load(MIN_SHARES)=2*1024. In
2728 * case no task is runnable on a CPU MIN_SHARES=2 should be returned
2729 * instead of 0.
2730 */
Yong Zhang3ff6dca2011-01-24 15:33:52 +08002731 if (shares < MIN_SHARES)
2732 shares = MIN_SHARES;
2733 if (shares > tg->shares)
2734 shares = tg->shares;
2735
2736 return shares;
2737}
Yong Zhang3ff6dca2011-01-24 15:33:52 +08002738# else /* CONFIG_SMP */
Paul Turner6d5ab292011-01-21 20:45:01 -08002739static inline long calc_cfs_shares(struct cfs_rq *cfs_rq, struct task_group *tg)
Yong Zhang3ff6dca2011-01-24 15:33:52 +08002740{
2741 return tg->shares;
2742}
Yong Zhang3ff6dca2011-01-24 15:33:52 +08002743# endif /* CONFIG_SMP */
Peter Zijlstraea1dc6f2016-06-24 16:11:02 +02002744
Peter Zijlstra2069dd72010-11-15 15:47:00 -08002745static void reweight_entity(struct cfs_rq *cfs_rq, struct sched_entity *se,
2746 unsigned long weight)
2747{
Paul Turner19e5eeb2010-12-15 19:10:18 -08002748 if (se->on_rq) {
2749 /* commit outstanding execution time */
2750 if (cfs_rq->curr == se)
2751 update_curr(cfs_rq);
Peter Zijlstra2069dd72010-11-15 15:47:00 -08002752 account_entity_dequeue(cfs_rq, se);
Paul Turner19e5eeb2010-12-15 19:10:18 -08002753 }
Peter Zijlstra2069dd72010-11-15 15:47:00 -08002754
2755 update_load_set(&se->load, weight);
2756
2757 if (se->on_rq)
2758 account_entity_enqueue(cfs_rq, se);
2759}
2760
Paul Turner82958362012-10-04 13:18:31 +02002761static inline int throttled_hierarchy(struct cfs_rq *cfs_rq);
2762
Vincent Guittot89ee0482016-12-21 16:50:26 +01002763static void update_cfs_shares(struct sched_entity *se)
Peter Zijlstra2069dd72010-11-15 15:47:00 -08002764{
Vincent Guittot89ee0482016-12-21 16:50:26 +01002765 struct cfs_rq *cfs_rq = group_cfs_rq(se);
Peter Zijlstra2069dd72010-11-15 15:47:00 -08002766 struct task_group *tg;
Yong Zhang3ff6dca2011-01-24 15:33:52 +08002767 long shares;
Peter Zijlstra2069dd72010-11-15 15:47:00 -08002768
Vincent Guittot89ee0482016-12-21 16:50:26 +01002769 if (!cfs_rq)
Peter Zijlstra2069dd72010-11-15 15:47:00 -08002770 return;
Vincent Guittot89ee0482016-12-21 16:50:26 +01002771
2772 if (throttled_hierarchy(cfs_rq))
2773 return;
2774
2775 tg = cfs_rq->tg;
2776
Yong Zhang3ff6dca2011-01-24 15:33:52 +08002777#ifndef CONFIG_SMP
2778 if (likely(se->load.weight == tg->shares))
2779 return;
2780#endif
Paul Turner6d5ab292011-01-21 20:45:01 -08002781 shares = calc_cfs_shares(cfs_rq, tg);
Peter Zijlstra2069dd72010-11-15 15:47:00 -08002782
2783 reweight_entity(cfs_rq_of(se), se, shares);
2784}
Vincent Guittot89ee0482016-12-21 16:50:26 +01002785
Peter Zijlstra2069dd72010-11-15 15:47:00 -08002786#else /* CONFIG_FAIR_GROUP_SCHED */
Vincent Guittot89ee0482016-12-21 16:50:26 +01002787static inline void update_cfs_shares(struct sched_entity *se)
Peter Zijlstra2069dd72010-11-15 15:47:00 -08002788{
2789}
2790#endif /* CONFIG_FAIR_GROUP_SCHED */
2791
Viresh Kumara030d732017-05-24 10:59:52 +05302792static inline void cfs_rq_util_change(struct cfs_rq *cfs_rq)
2793{
Linus Torvalds43964402017-09-05 12:19:08 -07002794 struct rq *rq = rq_of(cfs_rq);
2795
2796 if (&rq->cfs == cfs_rq) {
Viresh Kumara030d732017-05-24 10:59:52 +05302797 /*
2798 * There are a few boundary cases this might miss but it should
2799 * get called often enough that that should (hopefully) not be
2800 * a real problem -- added to that it only calls on the local
2801 * CPU, so if we enqueue remotely we'll miss an update, but
2802 * the next tick/schedule should update.
2803 *
2804 * It will not get called when we go idle, because the idle
2805 * thread is a different class (!fair), nor will the utilization
2806 * number include things like RT tasks.
2807 *
2808 * As is, the util number is not freq-invariant (we'd have to
2809 * implement arch_scale_freq_capacity() for that).
2810 *
2811 * See cpu_util().
2812 */
Linus Torvalds43964402017-09-05 12:19:08 -07002813 cpufreq_update_util(rq, 0);
Viresh Kumara030d732017-05-24 10:59:52 +05302814 }
2815}
2816
Alex Shi141965c2013-06-26 13:05:39 +08002817#ifdef CONFIG_SMP
Paul Turner5b51f2f2012-10-04 13:18:32 +02002818/*
Paul Turner9d85f212012-10-04 13:18:29 +02002819 * Approximate:
2820 * val * y^n, where y^32 ~= 0.5 (~1 scheduling period)
2821 */
Yuyang Dua481db32017-02-13 05:44:23 +08002822static u64 decay_load(u64 val, u64 n)
Paul Turner9d85f212012-10-04 13:18:29 +02002823{
Paul Turner5b51f2f2012-10-04 13:18:32 +02002824 unsigned int local_n;
2825
Peter Zijlstra05296e72017-03-31 10:51:41 +02002826 if (unlikely(n > LOAD_AVG_PERIOD * 63))
Paul Turner5b51f2f2012-10-04 13:18:32 +02002827 return 0;
2828
2829 /* after bounds checking we can collapse to 32-bit */
2830 local_n = n;
2831
2832 /*
2833 * As y^PERIOD = 1/2, we can combine
Zhihui Zhang9c58c792014-09-20 21:24:36 -04002834 * y^n = 1/2^(n/PERIOD) * y^(n%PERIOD)
2835 * With a look-up table which covers y^n (n<PERIOD)
Paul Turner5b51f2f2012-10-04 13:18:32 +02002836 *
2837 * To achieve constant time decay_load.
2838 */
2839 if (unlikely(local_n >= LOAD_AVG_PERIOD)) {
2840 val >>= local_n / LOAD_AVG_PERIOD;
2841 local_n %= LOAD_AVG_PERIOD;
Paul Turner9d85f212012-10-04 13:18:29 +02002842 }
2843
Yuyang Du9d89c252015-07-15 08:04:37 +08002844 val = mul_u64_u32_shr(val, runnable_avg_yN_inv[local_n], 32);
2845 return val;
Paul Turner5b51f2f2012-10-04 13:18:32 +02002846}
2847
Peter Zijlstra05296e72017-03-31 10:51:41 +02002848static u32 __accumulate_pelt_segments(u64 periods, u32 d1, u32 d3)
Paul Turner5b51f2f2012-10-04 13:18:32 +02002849{
Peter Zijlstra05296e72017-03-31 10:51:41 +02002850 u32 c1, c2, c3 = d3; /* y^0 == 1 */
Paul Turner5b51f2f2012-10-04 13:18:32 +02002851
Yuyang Dua481db32017-02-13 05:44:23 +08002852 /*
Peter Zijlstra3841cdc2017-04-10 12:47:33 +02002853 * c1 = d1 y^p
Yuyang Dua481db32017-02-13 05:44:23 +08002854 */
Peter Zijlstra05296e72017-03-31 10:51:41 +02002855 c1 = decay_load((u64)d1, periods);
Yuyang Dua481db32017-02-13 05:44:23 +08002856
Yuyang Dua481db32017-02-13 05:44:23 +08002857 /*
Peter Zijlstra3841cdc2017-04-10 12:47:33 +02002858 * p-1
Peter Zijlstra05296e72017-03-31 10:51:41 +02002859 * c2 = 1024 \Sum y^n
2860 * n=1
Yuyang Dua481db32017-02-13 05:44:23 +08002861 *
Peter Zijlstra05296e72017-03-31 10:51:41 +02002862 * inf inf
2863 * = 1024 ( \Sum y^n - \Sum y^n - y^0 )
Peter Zijlstra3841cdc2017-04-10 12:47:33 +02002864 * n=0 n=p
Yuyang Dua481db32017-02-13 05:44:23 +08002865 */
Peter Zijlstra05296e72017-03-31 10:51:41 +02002866 c2 = LOAD_AVG_MAX - decay_load(LOAD_AVG_MAX, periods) - 1024;
Yuyang Dua481db32017-02-13 05:44:23 +08002867
2868 return c1 + c2 + c3;
Paul Turner9d85f212012-10-04 13:18:29 +02002869}
2870
Peter Zijlstra54a21382015-09-07 15:05:42 +02002871#define cap_scale(v, s) ((v)*(s) >> SCHED_CAPACITY_SHIFT)
Dietmar Eggemanne0f5f3a2015-08-14 17:23:09 +01002872
Paul Turner9d85f212012-10-04 13:18:29 +02002873/*
Yuyang Dua481db32017-02-13 05:44:23 +08002874 * Accumulate the three separate parts of the sum; d1 the remainder
2875 * of the last (incomplete) period, d2 the span of full periods and d3
2876 * the remainder of the (incomplete) current period.
2877 *
2878 * d1 d2 d3
2879 * ^ ^ ^
2880 * | | |
2881 * |<->|<----------------->|<--->|
2882 * ... |---x---|------| ... |------|-----x (now)
2883 *
Peter Zijlstra3841cdc2017-04-10 12:47:33 +02002884 * p-1
2885 * u' = (u + d1) y^p + 1024 \Sum y^n + d3 y^0
2886 * n=1
Yuyang Dua481db32017-02-13 05:44:23 +08002887 *
Peter Zijlstra3841cdc2017-04-10 12:47:33 +02002888 * = u y^p + (Step 1)
Yuyang Dua481db32017-02-13 05:44:23 +08002889 *
Peter Zijlstra3841cdc2017-04-10 12:47:33 +02002890 * p-1
2891 * d1 y^p + 1024 \Sum y^n + d3 y^0 (Step 2)
2892 * n=1
Yuyang Dua481db32017-02-13 05:44:23 +08002893 */
2894static __always_inline u32
2895accumulate_sum(u64 delta, int cpu, struct sched_avg *sa,
2896 unsigned long weight, int running, struct cfs_rq *cfs_rq)
2897{
2898 unsigned long scale_freq, scale_cpu;
Peter Zijlstra05296e72017-03-31 10:51:41 +02002899 u32 contrib = (u32)delta; /* p == 0 -> delta < 1024 */
Yuyang Dua481db32017-02-13 05:44:23 +08002900 u64 periods;
Yuyang Dua481db32017-02-13 05:44:23 +08002901
2902 scale_freq = arch_scale_freq_capacity(NULL, cpu);
2903 scale_cpu = arch_scale_cpu_capacity(NULL, cpu);
2904
2905 delta += sa->period_contrib;
2906 periods = delta / 1024; /* A period is 1024us (~1ms) */
2907
2908 /*
2909 * Step 1: decay old *_sum if we crossed period boundaries.
2910 */
2911 if (periods) {
2912 sa->load_sum = decay_load(sa->load_sum, periods);
2913 if (cfs_rq) {
2914 cfs_rq->runnable_load_sum =
2915 decay_load(cfs_rq->runnable_load_sum, periods);
2916 }
2917 sa->util_sum = decay_load((u64)(sa->util_sum), periods);
Yuyang Dua481db32017-02-13 05:44:23 +08002918
Peter Zijlstra05296e72017-03-31 10:51:41 +02002919 /*
2920 * Step 2
2921 */
2922 delta %= 1024;
2923 contrib = __accumulate_pelt_segments(periods,
2924 1024 - sa->period_contrib, delta);
2925 }
Yuyang Dua481db32017-02-13 05:44:23 +08002926 sa->period_contrib = delta;
2927
2928 contrib = cap_scale(contrib, scale_freq);
2929 if (weight) {
2930 sa->load_sum += weight * contrib;
2931 if (cfs_rq)
2932 cfs_rq->runnable_load_sum += weight * contrib;
2933 }
2934 if (running)
2935 sa->util_sum += contrib * scale_cpu;
2936
2937 return periods;
2938}
2939
2940/*
Paul Turner9d85f212012-10-04 13:18:29 +02002941 * We can represent the historical contribution to runnable average as the
2942 * coefficients of a geometric series. To do this we sub-divide our runnable
2943 * history into segments of approximately 1ms (1024us); label the segment that
2944 * occurred N-ms ago p_N, with p_0 corresponding to the current period, e.g.
2945 *
2946 * [<- 1024us ->|<- 1024us ->|<- 1024us ->| ...
2947 * p0 p1 p2
2948 * (now) (~1ms ago) (~2ms ago)
2949 *
2950 * Let u_i denote the fraction of p_i that the entity was runnable.
2951 *
2952 * We then designate the fractions u_i as our co-efficients, yielding the
2953 * following representation of historical load:
2954 * u_0 + u_1*y + u_2*y^2 + u_3*y^3 + ...
2955 *
2956 * We choose y based on the with of a reasonably scheduling period, fixing:
2957 * y^32 = 0.5
2958 *
2959 * This means that the contribution to load ~32ms ago (u_32) will be weighted
2960 * approximately half as much as the contribution to load within the last ms
2961 * (u_0).
2962 *
2963 * When a period "rolls over" and we have new u_0`, multiplying the previous
2964 * sum again by y is sufficient to update:
2965 * load_avg = u_0` + y*(u_0 + u_1*y + u_2*y^2 + ... )
2966 * = u_0 + u_1*y + u_2*y^2 + ... [re-labeling u_i --> u_{i+1}]
2967 */
Yuyang Du9d89c252015-07-15 08:04:37 +08002968static __always_inline int
Peter Zijlstra0ccb9772017-03-28 11:08:20 +02002969___update_load_avg(u64 now, int cpu, struct sched_avg *sa,
Yuyang Du13962232015-07-15 08:04:41 +08002970 unsigned long weight, int running, struct cfs_rq *cfs_rq)
Paul Turner9d85f212012-10-04 13:18:29 +02002971{
Yuyang Dua481db32017-02-13 05:44:23 +08002972 u64 delta;
Paul Turner9d85f212012-10-04 13:18:29 +02002973
Yuyang Du9d89c252015-07-15 08:04:37 +08002974 delta = now - sa->last_update_time;
Paul Turner9d85f212012-10-04 13:18:29 +02002975 /*
2976 * This should only happen when time goes backwards, which it
2977 * unfortunately does during sched clock init when we swap over to TSC.
2978 */
2979 if ((s64)delta < 0) {
Yuyang Du9d89c252015-07-15 08:04:37 +08002980 sa->last_update_time = now;
Paul Turner9d85f212012-10-04 13:18:29 +02002981 return 0;
2982 }
2983
2984 /*
2985 * Use 1024ns as the unit of measurement since it's a reasonable
2986 * approximation of 1us and fast to compute.
2987 */
2988 delta >>= 10;
2989 if (!delta)
2990 return 0;
Peter Zijlstrabb0bd042017-04-10 13:20:45 +02002991
2992 sa->last_update_time += delta << 10;
Paul Turner9d85f212012-10-04 13:18:29 +02002993
Yuyang Dua481db32017-02-13 05:44:23 +08002994 /*
Vincent Guittotf235a542017-07-01 07:06:13 +02002995 * running is a subset of runnable (weight) so running can't be set if
2996 * runnable is clear. But there are some corner cases where the current
2997 * se has been already dequeued but cfs_rq->curr still points to it.
2998 * This means that weight will be 0 but not running for a sched_entity
2999 * but also for a cfs_rq if the latter becomes idle. As an example,
3000 * this happens during idle_balance() which calls
3001 * update_blocked_averages()
3002 */
3003 if (!weight)
3004 running = 0;
3005
3006 /*
Yuyang Dua481db32017-02-13 05:44:23 +08003007 * Now we know we crossed measurement unit boundaries. The *_avg
3008 * accrues by two steps:
3009 *
3010 * Step 1: accumulate *_sum since last_update_time. If we haven't
3011 * crossed period boundaries, finish.
3012 */
3013 if (!accumulate_sum(delta, cpu, sa, weight, running, cfs_rq))
3014 return 0;
Dietmar Eggemann6f2b0452015-09-07 14:57:22 +01003015
Yuyang Dua481db32017-02-13 05:44:23 +08003016 /*
3017 * Step 2: update *_avg.
3018 */
Vincent Guittot625ed2b2017-04-26 08:27:56 +02003019 sa->load_avg = div_u64(sa->load_sum, LOAD_AVG_MAX - 1024 + sa->period_contrib);
Yuyang Dua481db32017-02-13 05:44:23 +08003020 if (cfs_rq) {
3021 cfs_rq->runnable_load_avg =
Vincent Guittot625ed2b2017-04-26 08:27:56 +02003022 div_u64(cfs_rq->runnable_load_sum, LOAD_AVG_MAX - 1024 + sa->period_contrib);
Paul Turner9d85f212012-10-04 13:18:29 +02003023 }
Vincent Guittot625ed2b2017-04-26 08:27:56 +02003024 sa->util_avg = sa->util_sum / (LOAD_AVG_MAX - 1024 + sa->period_contrib);
Paul Turner9d85f212012-10-04 13:18:29 +02003025
Yuyang Dua481db32017-02-13 05:44:23 +08003026 return 1;
Paul Turner9d85f212012-10-04 13:18:29 +02003027}
3028
Peter Zijlstra0ccb9772017-03-28 11:08:20 +02003029static int
3030__update_load_avg_blocked_se(u64 now, int cpu, struct sched_entity *se)
3031{
3032 return ___update_load_avg(now, cpu, &se->avg, 0, 0, NULL);
3033}
3034
3035static int
3036__update_load_avg_se(u64 now, int cpu, struct cfs_rq *cfs_rq, struct sched_entity *se)
3037{
3038 return ___update_load_avg(now, cpu, &se->avg,
3039 se->on_rq * scale_load_down(se->load.weight),
3040 cfs_rq->curr == se, NULL);
3041}
3042
3043static int
3044__update_load_avg_cfs_rq(u64 now, int cpu, struct cfs_rq *cfs_rq)
3045{
3046 return ___update_load_avg(now, cpu, &cfs_rq->avg,
3047 scale_load_down(cfs_rq->load.weight),
3048 cfs_rq->curr != NULL, cfs_rq);
3049}
3050
Vincent Guittot09a43ac2016-11-08 10:53:45 +01003051/*
3052 * Signed add and clamp on underflow.
3053 *
3054 * Explicitly do a load-store to ensure the intermediate value never hits
3055 * memory. This allows lockless observations without ever seeing the negative
3056 * values.
3057 */
3058#define add_positive(_ptr, _val) do { \
3059 typeof(_ptr) ptr = (_ptr); \
3060 typeof(_val) val = (_val); \
3061 typeof(*ptr) res, var = READ_ONCE(*ptr); \
3062 \
3063 res = var + val; \
3064 \
3065 if (val < 0 && res > var) \
3066 res = 0; \
3067 \
3068 WRITE_ONCE(*ptr, res); \
3069} while (0)
3070
Paul Turnerc566e8e2012-10-04 13:18:30 +02003071#ifdef CONFIG_FAIR_GROUP_SCHED
Peter Zijlstra7c3edd22016-07-13 10:56:25 +02003072/**
3073 * update_tg_load_avg - update the tg's load avg
3074 * @cfs_rq: the cfs_rq whose avg changed
3075 * @force: update regardless of how small the difference
3076 *
3077 * This function 'ensures': tg->load_avg := \Sum tg->cfs_rq[]->avg.load.
3078 * However, because tg->load_avg is a global value there are performance
3079 * considerations.
3080 *
3081 * In order to avoid having to look at the other cfs_rq's, we use a
3082 * differential update where we store the last value we propagated. This in
3083 * turn allows skipping updates if the differential is 'small'.
3084 *
Rik van Riel815abf52017-06-23 12:55:30 -04003085 * Updating tg's load_avg is necessary before update_cfs_share().
Paul Turnerbb17f652012-10-04 13:18:31 +02003086 */
Yuyang Du9d89c252015-07-15 08:04:37 +08003087static inline void update_tg_load_avg(struct cfs_rq *cfs_rq, int force)
Paul Turnerbb17f652012-10-04 13:18:31 +02003088{
Yuyang Du9d89c252015-07-15 08:04:37 +08003089 long delta = cfs_rq->avg.load_avg - cfs_rq->tg_load_avg_contrib;
Paul Turnerbb17f652012-10-04 13:18:31 +02003090
Waiman Longaa0b7ae2015-12-02 13:41:50 -05003091 /*
3092 * No need to update load_avg for root_task_group as it is not used.
3093 */
3094 if (cfs_rq->tg == &root_task_group)
3095 return;
3096
Yuyang Du9d89c252015-07-15 08:04:37 +08003097 if (force || abs(delta) > cfs_rq->tg_load_avg_contrib / 64) {
3098 atomic_long_add(delta, &cfs_rq->tg->load_avg);
3099 cfs_rq->tg_load_avg_contrib = cfs_rq->avg.load_avg;
Paul Turnerbb17f652012-10-04 13:18:31 +02003100 }
Paul Turner8165e142012-10-04 13:18:31 +02003101}
Dietmar Eggemannf5f97392014-02-26 11:19:33 +00003102
Byungchul Parkad936d82015-10-24 01:16:19 +09003103/*
3104 * Called within set_task_rq() right before setting a task's cpu. The
3105 * caller only guarantees p->pi_lock is held; no other assumptions,
3106 * including the state of rq->lock, should be made.
3107 */
3108void set_task_rq_fair(struct sched_entity *se,
3109 struct cfs_rq *prev, struct cfs_rq *next)
3110{
Peter Zijlstra0ccb9772017-03-28 11:08:20 +02003111 u64 p_last_update_time;
3112 u64 n_last_update_time;
3113
Byungchul Parkad936d82015-10-24 01:16:19 +09003114 if (!sched_feat(ATTACH_AGE_LOAD))
3115 return;
3116
3117 /*
3118 * We are supposed to update the task to "current" time, then its up to
3119 * date and ready to go to new CPU/cfs_rq. But we have difficulty in
3120 * getting what current time is, so simply throw away the out-of-date
3121 * time. This will result in the wakee task is less decayed, but giving
3122 * the wakee more load sounds not bad.
3123 */
Peter Zijlstra0ccb9772017-03-28 11:08:20 +02003124 if (!(se->avg.last_update_time && prev))
3125 return;
Byungchul Parkad936d82015-10-24 01:16:19 +09003126
3127#ifndef CONFIG_64BIT
Peter Zijlstra0ccb9772017-03-28 11:08:20 +02003128 {
Byungchul Parkad936d82015-10-24 01:16:19 +09003129 u64 p_last_update_time_copy;
3130 u64 n_last_update_time_copy;
3131
3132 do {
3133 p_last_update_time_copy = prev->load_last_update_time_copy;
3134 n_last_update_time_copy = next->load_last_update_time_copy;
3135
3136 smp_rmb();
3137
3138 p_last_update_time = prev->avg.last_update_time;
3139 n_last_update_time = next->avg.last_update_time;
3140
3141 } while (p_last_update_time != p_last_update_time_copy ||
3142 n_last_update_time != n_last_update_time_copy);
Byungchul Parkad936d82015-10-24 01:16:19 +09003143 }
Peter Zijlstra0ccb9772017-03-28 11:08:20 +02003144#else
3145 p_last_update_time = prev->avg.last_update_time;
3146 n_last_update_time = next->avg.last_update_time;
3147#endif
3148 __update_load_avg_blocked_se(p_last_update_time, cpu_of(rq_of(prev)), se);
3149 se->avg.last_update_time = n_last_update_time;
Byungchul Parkad936d82015-10-24 01:16:19 +09003150}
Vincent Guittot09a43ac2016-11-08 10:53:45 +01003151
3152/* Take into account change of utilization of a child task group */
3153static inline void
3154update_tg_cfs_util(struct cfs_rq *cfs_rq, struct sched_entity *se)
3155{
3156 struct cfs_rq *gcfs_rq = group_cfs_rq(se);
3157 long delta = gcfs_rq->avg.util_avg - se->avg.util_avg;
3158
3159 /* Nothing to update */
3160 if (!delta)
3161 return;
3162
3163 /* Set new sched_entity's utilization */
3164 se->avg.util_avg = gcfs_rq->avg.util_avg;
3165 se->avg.util_sum = se->avg.util_avg * LOAD_AVG_MAX;
3166
3167 /* Update parent cfs_rq utilization */
3168 add_positive(&cfs_rq->avg.util_avg, delta);
3169 cfs_rq->avg.util_sum = cfs_rq->avg.util_avg * LOAD_AVG_MAX;
3170}
3171
3172/* Take into account change of load of a child task group */
3173static inline void
3174update_tg_cfs_load(struct cfs_rq *cfs_rq, struct sched_entity *se)
3175{
3176 struct cfs_rq *gcfs_rq = group_cfs_rq(se);
3177 long delta, load = gcfs_rq->avg.load_avg;
3178
3179 /*
3180 * If the load of group cfs_rq is null, the load of the
3181 * sched_entity will also be null so we can skip the formula
3182 */
3183 if (load) {
3184 long tg_load;
3185
3186 /* Get tg's load and ensure tg_load > 0 */
3187 tg_load = atomic_long_read(&gcfs_rq->tg->load_avg) + 1;
3188
3189 /* Ensure tg_load >= load and updated with current load*/
3190 tg_load -= gcfs_rq->tg_load_avg_contrib;
3191 tg_load += load;
3192
3193 /*
3194 * We need to compute a correction term in the case that the
3195 * task group is consuming more CPU than a task of equal
3196 * weight. A task with a weight equals to tg->shares will have
3197 * a load less or equal to scale_load_down(tg->shares).
3198 * Similarly, the sched_entities that represent the task group
3199 * at parent level, can't have a load higher than
3200 * scale_load_down(tg->shares). And the Sum of sched_entities'
3201 * load must be <= scale_load_down(tg->shares).
3202 */
3203 if (tg_load > scale_load_down(gcfs_rq->tg->shares)) {
3204 /* scale gcfs_rq's load into tg's shares*/
3205 load *= scale_load_down(gcfs_rq->tg->shares);
3206 load /= tg_load;
3207 }
3208 }
3209
3210 delta = load - se->avg.load_avg;
3211
3212 /* Nothing to update */
3213 if (!delta)
3214 return;
3215
3216 /* Set new sched_entity's load */
3217 se->avg.load_avg = load;
3218 se->avg.load_sum = se->avg.load_avg * LOAD_AVG_MAX;
3219
3220 /* Update parent cfs_rq load */
3221 add_positive(&cfs_rq->avg.load_avg, delta);
3222 cfs_rq->avg.load_sum = cfs_rq->avg.load_avg * LOAD_AVG_MAX;
3223
3224 /*
3225 * If the sched_entity is already enqueued, we also have to update the
3226 * runnable load avg.
3227 */
3228 if (se->on_rq) {
3229 /* Update parent cfs_rq runnable_load_avg */
3230 add_positive(&cfs_rq->runnable_load_avg, delta);
3231 cfs_rq->runnable_load_sum = cfs_rq->runnable_load_avg * LOAD_AVG_MAX;
3232 }
3233}
3234
3235static inline void set_tg_cfs_propagate(struct cfs_rq *cfs_rq)
3236{
3237 cfs_rq->propagate_avg = 1;
3238}
3239
3240static inline int test_and_clear_tg_cfs_propagate(struct sched_entity *se)
3241{
3242 struct cfs_rq *cfs_rq = group_cfs_rq(se);
3243
3244 if (!cfs_rq->propagate_avg)
3245 return 0;
3246
3247 cfs_rq->propagate_avg = 0;
3248 return 1;
3249}
3250
3251/* Update task and its cfs_rq load average */
3252static inline int propagate_entity_load_avg(struct sched_entity *se)
3253{
3254 struct cfs_rq *cfs_rq;
3255
3256 if (entity_is_task(se))
3257 return 0;
3258
3259 if (!test_and_clear_tg_cfs_propagate(se))
3260 return 0;
3261
3262 cfs_rq = cfs_rq_of(se);
3263
3264 set_tg_cfs_propagate(cfs_rq);
3265
3266 update_tg_cfs_util(cfs_rq, se);
3267 update_tg_cfs_load(cfs_rq, se);
3268
3269 return 1;
3270}
3271
Vincent Guittotbc427892017-03-17 14:47:22 +01003272/*
3273 * Check if we need to update the load and the utilization of a blocked
3274 * group_entity:
3275 */
3276static inline bool skip_blocked_update(struct sched_entity *se)
3277{
3278 struct cfs_rq *gcfs_rq = group_cfs_rq(se);
3279
3280 /*
3281 * If sched_entity still have not zero load or utilization, we have to
3282 * decay it:
3283 */
3284 if (se->avg.load_avg || se->avg.util_avg)
3285 return false;
3286
3287 /*
3288 * If there is a pending propagation, we have to update the load and
3289 * the utilization of the sched_entity:
3290 */
3291 if (gcfs_rq->propagate_avg)
3292 return false;
3293
3294 /*
3295 * Otherwise, the load and the utilization of the sched_entity is
3296 * already zero and there is no pending propagation, so it will be a
3297 * waste of time to try to decay it:
3298 */
3299 return true;
3300}
3301
Peter Zijlstra6e831252014-02-11 16:11:48 +01003302#else /* CONFIG_FAIR_GROUP_SCHED */
Vincent Guittot09a43ac2016-11-08 10:53:45 +01003303
Yuyang Du9d89c252015-07-15 08:04:37 +08003304static inline void update_tg_load_avg(struct cfs_rq *cfs_rq, int force) {}
Vincent Guittot09a43ac2016-11-08 10:53:45 +01003305
3306static inline int propagate_entity_load_avg(struct sched_entity *se)
3307{
3308 return 0;
3309}
3310
3311static inline void set_tg_cfs_propagate(struct cfs_rq *cfs_rq) {}
3312
Peter Zijlstra6e831252014-02-11 16:11:48 +01003313#endif /* CONFIG_FAIR_GROUP_SCHED */
Paul Turnerc566e8e2012-10-04 13:18:30 +02003314
Peter Zijlstra89741892016-06-16 10:50:40 +02003315/*
3316 * Unsigned subtract and clamp on underflow.
3317 *
3318 * Explicitly do a load-store to ensure the intermediate value never hits
3319 * memory. This allows lockless observations without ever seeing the negative
3320 * values.
3321 */
3322#define sub_positive(_ptr, _val) do { \
3323 typeof(_ptr) ptr = (_ptr); \
3324 typeof(*ptr) val = (_val); \
3325 typeof(*ptr) res, var = READ_ONCE(*ptr); \
3326 res = var - val; \
3327 if (res > var) \
3328 res = 0; \
3329 WRITE_ONCE(*ptr, res); \
3330} while (0)
3331
Peter Zijlstra3d30544f2016-06-21 14:27:50 +02003332/**
3333 * update_cfs_rq_load_avg - update the cfs_rq's load/util averages
3334 * @now: current time, as per cfs_rq_clock_task()
3335 * @cfs_rq: cfs_rq to update
Peter Zijlstra3d30544f2016-06-21 14:27:50 +02003336 *
3337 * The cfs_rq avg is the direct sum of all its entities (blocked and runnable)
3338 * avg. The immediate corollary is that all (fair) tasks must be attached, see
3339 * post_init_entity_util_avg().
3340 *
3341 * cfs_rq->avg is used for task_h_load() and update_cfs_share() for example.
3342 *
Peter Zijlstra7c3edd22016-07-13 10:56:25 +02003343 * Returns true if the load decayed or we removed load.
3344 *
3345 * Since both these conditions indicate a changed cfs_rq->avg.load we should
3346 * call update_tg_load_avg() when this function returns true.
Peter Zijlstra3d30544f2016-06-21 14:27:50 +02003347 */
Steve Mucklea2c6c912016-03-24 15:26:07 -07003348static inline int
Viresh Kumar3a123bb2017-05-24 10:59:56 +05303349update_cfs_rq_load_avg(u64 now, struct cfs_rq *cfs_rq)
Steve Mucklea2c6c912016-03-24 15:26:07 -07003350{
3351 struct sched_avg *sa = &cfs_rq->avg;
3352 int decayed, removed_load = 0, removed_util = 0;
3353
3354 if (atomic_long_read(&cfs_rq->removed_load_avg)) {
3355 s64 r = atomic_long_xchg(&cfs_rq->removed_load_avg, 0);
Peter Zijlstra89741892016-06-16 10:50:40 +02003356 sub_positive(&sa->load_avg, r);
3357 sub_positive(&sa->load_sum, r * LOAD_AVG_MAX);
Steve Mucklea2c6c912016-03-24 15:26:07 -07003358 removed_load = 1;
Vincent Guittot4e516072016-11-08 10:53:46 +01003359 set_tg_cfs_propagate(cfs_rq);
Steve Mucklea2c6c912016-03-24 15:26:07 -07003360 }
3361
3362 if (atomic_long_read(&cfs_rq->removed_util_avg)) {
3363 long r = atomic_long_xchg(&cfs_rq->removed_util_avg, 0);
Peter Zijlstra89741892016-06-16 10:50:40 +02003364 sub_positive(&sa->util_avg, r);
3365 sub_positive(&sa->util_sum, r * LOAD_AVG_MAX);
Steve Mucklea2c6c912016-03-24 15:26:07 -07003366 removed_util = 1;
Vincent Guittot4e516072016-11-08 10:53:46 +01003367 set_tg_cfs_propagate(cfs_rq);
Steve Mucklea2c6c912016-03-24 15:26:07 -07003368 }
3369
Peter Zijlstra0ccb9772017-03-28 11:08:20 +02003370 decayed = __update_load_avg_cfs_rq(now, cpu_of(rq_of(cfs_rq)), cfs_rq);
Steve Mucklea2c6c912016-03-24 15:26:07 -07003371
3372#ifndef CONFIG_64BIT
3373 smp_wmb();
3374 cfs_rq->load_last_update_time_copy = sa->last_update_time;
3375#endif
3376
Viresh Kumar3a123bb2017-05-24 10:59:56 +05303377 if (decayed || removed_util)
Steve Mucklea2c6c912016-03-24 15:26:07 -07003378 cfs_rq_util_change(cfs_rq);
Steve Muckle21e96f82016-03-21 17:21:07 -07003379
Steve Muckle41e0d372016-03-21 17:21:08 -07003380 return decayed || removed_load;
Yuyang Du9d89c252015-07-15 08:04:37 +08003381}
3382
Vincent Guittotd31b1a62016-11-08 10:53:44 +01003383/*
3384 * Optional action to be done while updating the load average
3385 */
3386#define UPDATE_TG 0x1
3387#define SKIP_AGE_LOAD 0x2
3388
Yuyang Du9d89c252015-07-15 08:04:37 +08003389/* Update task and its cfs_rq load average */
Vincent Guittotd31b1a62016-11-08 10:53:44 +01003390static inline void update_load_avg(struct sched_entity *se, int flags)
Paul Turner9d85f212012-10-04 13:18:29 +02003391{
Paul Turner2dac7542012-10-04 13:18:30 +02003392 struct cfs_rq *cfs_rq = cfs_rq_of(se);
Yuyang Du9d89c252015-07-15 08:04:37 +08003393 u64 now = cfs_rq_clock_task(cfs_rq);
Rafael J. Wysocki34e2c552016-02-15 20:20:42 +01003394 struct rq *rq = rq_of(cfs_rq);
3395 int cpu = cpu_of(rq);
Vincent Guittot09a43ac2016-11-08 10:53:45 +01003396 int decayed;
Paul Turner2dac7542012-10-04 13:18:30 +02003397
Paul Turnerf1b17282012-10-04 13:18:31 +02003398 /*
Yuyang Du9d89c252015-07-15 08:04:37 +08003399 * Track task load average for carrying it to new CPU after migrated, and
3400 * track group sched_entity load average for task_h_load calc in migration
Paul Turnerf1b17282012-10-04 13:18:31 +02003401 */
Peter Zijlstra0ccb9772017-03-28 11:08:20 +02003402 if (se->avg.last_update_time && !(flags & SKIP_AGE_LOAD))
3403 __update_load_avg_se(now, cpu, cfs_rq, se);
Paul Turnerf1b17282012-10-04 13:18:31 +02003404
Viresh Kumar3a123bb2017-05-24 10:59:56 +05303405 decayed = update_cfs_rq_load_avg(now, cfs_rq);
Vincent Guittot09a43ac2016-11-08 10:53:45 +01003406 decayed |= propagate_entity_load_avg(se);
3407
3408 if (decayed && (flags & UPDATE_TG))
Yuyang Du9d89c252015-07-15 08:04:37 +08003409 update_tg_load_avg(cfs_rq, 0);
3410}
Paul Turner2dac7542012-10-04 13:18:30 +02003411
Peter Zijlstra3d30544f2016-06-21 14:27:50 +02003412/**
3413 * attach_entity_load_avg - attach this entity to its cfs_rq load avg
3414 * @cfs_rq: cfs_rq to attach to
3415 * @se: sched_entity to attach
3416 *
3417 * Must call update_cfs_rq_load_avg() before this, since we rely on
3418 * cfs_rq->avg.last_update_time being current.
3419 */
Byungchul Parka05e8c52015-08-20 20:21:56 +09003420static void attach_entity_load_avg(struct cfs_rq *cfs_rq, struct sched_entity *se)
3421{
3422 se->avg.last_update_time = cfs_rq->avg.last_update_time;
3423 cfs_rq->avg.load_avg += se->avg.load_avg;
3424 cfs_rq->avg.load_sum += se->avg.load_sum;
3425 cfs_rq->avg.util_avg += se->avg.util_avg;
3426 cfs_rq->avg.util_sum += se->avg.util_sum;
Vincent Guittot09a43ac2016-11-08 10:53:45 +01003427 set_tg_cfs_propagate(cfs_rq);
Steve Mucklea2c6c912016-03-24 15:26:07 -07003428
3429 cfs_rq_util_change(cfs_rq);
Byungchul Parka05e8c52015-08-20 20:21:56 +09003430}
3431
Peter Zijlstra3d30544f2016-06-21 14:27:50 +02003432/**
3433 * detach_entity_load_avg - detach this entity from its cfs_rq load avg
3434 * @cfs_rq: cfs_rq to detach from
3435 * @se: sched_entity to detach
3436 *
3437 * Must call update_cfs_rq_load_avg() before this, since we rely on
3438 * cfs_rq->avg.last_update_time being current.
3439 */
Byungchul Parka05e8c52015-08-20 20:21:56 +09003440static void detach_entity_load_avg(struct cfs_rq *cfs_rq, struct sched_entity *se)
3441{
Byungchul Parka05e8c52015-08-20 20:21:56 +09003442
Peter Zijlstra89741892016-06-16 10:50:40 +02003443 sub_positive(&cfs_rq->avg.load_avg, se->avg.load_avg);
3444 sub_positive(&cfs_rq->avg.load_sum, se->avg.load_sum);
3445 sub_positive(&cfs_rq->avg.util_avg, se->avg.util_avg);
3446 sub_positive(&cfs_rq->avg.util_sum, se->avg.util_sum);
Vincent Guittot09a43ac2016-11-08 10:53:45 +01003447 set_tg_cfs_propagate(cfs_rq);
Steve Mucklea2c6c912016-03-24 15:26:07 -07003448
3449 cfs_rq_util_change(cfs_rq);
Byungchul Parka05e8c52015-08-20 20:21:56 +09003450}
3451
Yuyang Du9d89c252015-07-15 08:04:37 +08003452/* Add the load generated by se into cfs_rq's load average */
3453static inline void
3454enqueue_entity_load_avg(struct cfs_rq *cfs_rq, struct sched_entity *se)
3455{
3456 struct sched_avg *sa = &se->avg;
Yuyang Du9d89c252015-07-15 08:04:37 +08003457
Yuyang Du13962232015-07-15 08:04:41 +08003458 cfs_rq->runnable_load_avg += sa->load_avg;
3459 cfs_rq->runnable_load_sum += sa->load_sum;
3460
Vincent Guittotd31b1a62016-11-08 10:53:44 +01003461 if (!sa->last_update_time) {
Byungchul Parka05e8c52015-08-20 20:21:56 +09003462 attach_entity_load_avg(cfs_rq, se);
Yuyang Du9d89c252015-07-15 08:04:37 +08003463 update_tg_load_avg(cfs_rq, 0);
Vincent Guittotd31b1a62016-11-08 10:53:44 +01003464 }
Paul Turner9ee474f2012-10-04 13:18:30 +02003465}
3466
Yuyang Du13962232015-07-15 08:04:41 +08003467/* Remove the runnable load generated by se from cfs_rq's runnable load average */
3468static inline void
3469dequeue_entity_load_avg(struct cfs_rq *cfs_rq, struct sched_entity *se)
3470{
Yuyang Du13962232015-07-15 08:04:41 +08003471 cfs_rq->runnable_load_avg =
3472 max_t(long, cfs_rq->runnable_load_avg - se->avg.load_avg, 0);
3473 cfs_rq->runnable_load_sum =
Byungchul Parka05e8c52015-08-20 20:21:56 +09003474 max_t(s64, cfs_rq->runnable_load_sum - se->avg.load_sum, 0);
Yuyang Du13962232015-07-15 08:04:41 +08003475}
3476
Yuyang Du0905f042015-12-17 07:34:27 +08003477#ifndef CONFIG_64BIT
3478static inline u64 cfs_rq_last_update_time(struct cfs_rq *cfs_rq)
3479{
3480 u64 last_update_time_copy;
3481 u64 last_update_time;
3482
3483 do {
3484 last_update_time_copy = cfs_rq->load_last_update_time_copy;
3485 smp_rmb();
3486 last_update_time = cfs_rq->avg.last_update_time;
3487 } while (last_update_time != last_update_time_copy);
3488
3489 return last_update_time;
3490}
3491#else
3492static inline u64 cfs_rq_last_update_time(struct cfs_rq *cfs_rq)
3493{
3494 return cfs_rq->avg.last_update_time;
3495}
3496#endif
3497
Paul Turner9ee474f2012-10-04 13:18:30 +02003498/*
Morten Rasmussen104cb162016-10-14 14:41:07 +01003499 * Synchronize entity load avg of dequeued entity without locking
3500 * the previous rq.
3501 */
3502void sync_entity_load_avg(struct sched_entity *se)
3503{
3504 struct cfs_rq *cfs_rq = cfs_rq_of(se);
3505 u64 last_update_time;
3506
3507 last_update_time = cfs_rq_last_update_time(cfs_rq);
Peter Zijlstra0ccb9772017-03-28 11:08:20 +02003508 __update_load_avg_blocked_se(last_update_time, cpu_of(rq_of(cfs_rq)), se);
Morten Rasmussen104cb162016-10-14 14:41:07 +01003509}
3510
3511/*
Yuyang Du9d89c252015-07-15 08:04:37 +08003512 * Task first catches up with cfs_rq, and then subtract
3513 * itself from the cfs_rq (task must be off the queue now).
Paul Turner9ee474f2012-10-04 13:18:30 +02003514 */
Yuyang Du9d89c252015-07-15 08:04:37 +08003515void remove_entity_load_avg(struct sched_entity *se)
Paul Turner9ee474f2012-10-04 13:18:30 +02003516{
Yuyang Du9d89c252015-07-15 08:04:37 +08003517 struct cfs_rq *cfs_rq = cfs_rq_of(se);
Paul Turner9ee474f2012-10-04 13:18:30 +02003518
Yuyang Du0905f042015-12-17 07:34:27 +08003519 /*
Peter Zijlstra7dc603c2016-06-16 13:29:28 +02003520 * tasks cannot exit without having gone through wake_up_new_task() ->
3521 * post_init_entity_util_avg() which will have added things to the
3522 * cfs_rq, so we can remove unconditionally.
3523 *
3524 * Similarly for groups, they will have passed through
3525 * post_init_entity_util_avg() before unregister_sched_fair_group()
3526 * calls this.
Yuyang Du0905f042015-12-17 07:34:27 +08003527 */
Paul Turner9ee474f2012-10-04 13:18:30 +02003528
Morten Rasmussen104cb162016-10-14 14:41:07 +01003529 sync_entity_load_avg(se);
Yuyang Du9d89c252015-07-15 08:04:37 +08003530 atomic_long_add(se->avg.load_avg, &cfs_rq->removed_load_avg);
3531 atomic_long_add(se->avg.util_avg, &cfs_rq->removed_util_avg);
Paul Turner2dac7542012-10-04 13:18:30 +02003532}
Vincent Guittot642dbc32013-04-18 18:34:26 +02003533
Yuyang Du7ea241a2015-07-15 08:04:42 +08003534static inline unsigned long cfs_rq_runnable_load_avg(struct cfs_rq *cfs_rq)
3535{
3536 return cfs_rq->runnable_load_avg;
3537}
3538
3539static inline unsigned long cfs_rq_load_avg(struct cfs_rq *cfs_rq)
3540{
3541 return cfs_rq->avg.load_avg;
3542}
3543
Matt Fleming46f69fa2016-09-21 14:38:12 +01003544static int idle_balance(struct rq *this_rq, struct rq_flags *rf);
Peter Zijlstra6e831252014-02-11 16:11:48 +01003545
Peter Zijlstra38033c32014-01-23 20:32:21 +01003546#else /* CONFIG_SMP */
3547
Peter Zijlstra01011472016-06-17 11:20:46 +02003548static inline int
Viresh Kumar3a123bb2017-05-24 10:59:56 +05303549update_cfs_rq_load_avg(u64 now, struct cfs_rq *cfs_rq)
Peter Zijlstra01011472016-06-17 11:20:46 +02003550{
3551 return 0;
3552}
3553
Vincent Guittotd31b1a62016-11-08 10:53:44 +01003554#define UPDATE_TG 0x0
3555#define SKIP_AGE_LOAD 0x0
3556
3557static inline void update_load_avg(struct sched_entity *se, int not_used1)
Rafael J. Wysocki536bd002016-05-06 14:58:43 +02003558{
Viresh Kumara030d732017-05-24 10:59:52 +05303559 cfs_rq_util_change(cfs_rq_of(se));
Rafael J. Wysocki536bd002016-05-06 14:58:43 +02003560}
3561
Yuyang Du9d89c252015-07-15 08:04:37 +08003562static inline void
3563enqueue_entity_load_avg(struct cfs_rq *cfs_rq, struct sched_entity *se) {}
Yuyang Du13962232015-07-15 08:04:41 +08003564static inline void
3565dequeue_entity_load_avg(struct cfs_rq *cfs_rq, struct sched_entity *se) {}
Yuyang Du9d89c252015-07-15 08:04:37 +08003566static inline void remove_entity_load_avg(struct sched_entity *se) {}
Peter Zijlstra6e831252014-02-11 16:11:48 +01003567
Byungchul Parka05e8c52015-08-20 20:21:56 +09003568static inline void
3569attach_entity_load_avg(struct cfs_rq *cfs_rq, struct sched_entity *se) {}
3570static inline void
3571detach_entity_load_avg(struct cfs_rq *cfs_rq, struct sched_entity *se) {}
3572
Matt Fleming46f69fa2016-09-21 14:38:12 +01003573static inline int idle_balance(struct rq *rq, struct rq_flags *rf)
Peter Zijlstra6e831252014-02-11 16:11:48 +01003574{
3575 return 0;
3576}
3577
Peter Zijlstra38033c32014-01-23 20:32:21 +01003578#endif /* CONFIG_SMP */
Paul Turner9d85f212012-10-04 13:18:29 +02003579
Peter Zijlstraddc97292007-10-15 17:00:10 +02003580static void check_spread(struct cfs_rq *cfs_rq, struct sched_entity *se)
3581{
3582#ifdef CONFIG_SCHED_DEBUG
3583 s64 d = se->vruntime - cfs_rq->min_vruntime;
3584
3585 if (d < 0)
3586 d = -d;
3587
3588 if (d > 3*sysctl_sched_latency)
Josh Poimboeufae928822016-06-17 12:43:24 -05003589 schedstat_inc(cfs_rq->nr_spread_over);
Peter Zijlstraddc97292007-10-15 17:00:10 +02003590#endif
3591}
3592
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02003593static void
Peter Zijlstraaeb73b02007-10-15 17:00:05 +02003594place_entity(struct cfs_rq *cfs_rq, struct sched_entity *se, int initial)
3595{
Peter Zijlstra1af5f732008-10-24 11:06:13 +02003596 u64 vruntime = cfs_rq->min_vruntime;
Peter Zijlstra94dfb5e2007-10-15 17:00:05 +02003597
Peter Zijlstra2cb86002007-11-09 22:39:37 +01003598 /*
3599 * The 'current' period is already promised to the current tasks,
3600 * however the extra weight of the new task will slow them down a
3601 * little, place the new task so that it fits in the slot that
3602 * stays open at the end.
3603 */
Peter Zijlstra94dfb5e2007-10-15 17:00:05 +02003604 if (initial && sched_feat(START_DEBIT))
Peter Zijlstraf9c0b092008-10-17 19:27:04 +02003605 vruntime += sched_vslice(cfs_rq, se);
Peter Zijlstraaeb73b02007-10-15 17:00:05 +02003606
Mike Galbraitha2e7a7e2009-09-18 09:19:25 +02003607 /* sleeps up to a single latency don't count. */
Mike Galbraith5ca98802010-03-11 17:17:17 +01003608 if (!initial) {
Mike Galbraitha2e7a7e2009-09-18 09:19:25 +02003609 unsigned long thresh = sysctl_sched_latency;
Peter Zijlstraa7be37a2008-06-27 13:41:11 +02003610
Mike Galbraitha2e7a7e2009-09-18 09:19:25 +02003611 /*
Mike Galbraitha2e7a7e2009-09-18 09:19:25 +02003612 * Halve their sleep time's effect, to allow
3613 * for a gentler effect of sleepers:
3614 */
3615 if (sched_feat(GENTLE_FAIR_SLEEPERS))
3616 thresh >>= 1;
Ingo Molnar51e03042009-09-16 08:54:45 +02003617
Mike Galbraitha2e7a7e2009-09-18 09:19:25 +02003618 vruntime -= thresh;
Peter Zijlstraaeb73b02007-10-15 17:00:05 +02003619 }
3620
Mike Galbraithb5d9d732009-09-08 11:12:28 +02003621 /* ensure we never gain time by being placed backwards. */
Viresh Kumar16c8f1c2012-11-08 13:33:46 +05303622 se->vruntime = max_vruntime(se->vruntime, vruntime);
Peter Zijlstraaeb73b02007-10-15 17:00:05 +02003623}
3624
Paul Turnerd3d9dc32011-07-21 09:43:39 -07003625static void check_enqueue_throttle(struct cfs_rq *cfs_rq);
3626
Mel Gormancb251762016-02-05 09:08:36 +00003627static inline void check_schedstat_required(void)
3628{
3629#ifdef CONFIG_SCHEDSTATS
3630 if (schedstat_enabled())
3631 return;
3632
3633 /* Force schedstat enabled if a dependent tracepoint is active */
3634 if (trace_sched_stat_wait_enabled() ||
3635 trace_sched_stat_sleep_enabled() ||
3636 trace_sched_stat_iowait_enabled() ||
3637 trace_sched_stat_blocked_enabled() ||
3638 trace_sched_stat_runtime_enabled()) {
Josh Poimboeufeda8dca2016-06-13 02:32:09 -05003639 printk_deferred_once("Scheduler tracepoints stat_sleep, stat_iowait, "
Mel Gormancb251762016-02-05 09:08:36 +00003640 "stat_blocked and stat_runtime require the "
Marcin Nowakowskif67abed2017-06-09 10:00:29 +02003641 "kernel parameter schedstats=enable or "
Mel Gormancb251762016-02-05 09:08:36 +00003642 "kernel.sched_schedstats=1\n");
3643 }
3644#endif
3645}
3646
Peter Zijlstrab5179ac2016-05-11 16:10:34 +02003647
3648/*
3649 * MIGRATION
3650 *
3651 * dequeue
3652 * update_curr()
3653 * update_min_vruntime()
3654 * vruntime -= min_vruntime
3655 *
3656 * enqueue
3657 * update_curr()
3658 * update_min_vruntime()
3659 * vruntime += min_vruntime
3660 *
3661 * this way the vruntime transition between RQs is done when both
3662 * min_vruntime are up-to-date.
3663 *
3664 * WAKEUP (remote)
3665 *
Peter Zijlstra59efa0b2016-05-10 18:24:37 +02003666 * ->migrate_task_rq_fair() (p->state == TASK_WAKING)
Peter Zijlstrab5179ac2016-05-11 16:10:34 +02003667 * vruntime -= min_vruntime
3668 *
3669 * enqueue
3670 * update_curr()
3671 * update_min_vruntime()
3672 * vruntime += min_vruntime
3673 *
3674 * this way we don't have the most up-to-date min_vruntime on the originating
3675 * CPU and an up-to-date min_vruntime on the destination CPU.
3676 */
3677
Peter Zijlstraaeb73b02007-10-15 17:00:05 +02003678static void
Peter Zijlstra88ec22d2009-12-16 18:04:41 +01003679enqueue_entity(struct cfs_rq *cfs_rq, struct sched_entity *se, int flags)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02003680{
Peter Zijlstra2f950352016-05-11 19:27:56 +02003681 bool renorm = !(flags & ENQUEUE_WAKEUP) || (flags & ENQUEUE_MIGRATED);
3682 bool curr = cfs_rq->curr == se;
Peter Zijlstra3a47d512016-03-09 13:04:03 +01003683
Ingo Molnar53d3bc72016-05-11 08:25:53 +02003684 /*
Peter Zijlstra2f950352016-05-11 19:27:56 +02003685 * If we're the current task, we must renormalise before calling
3686 * update_curr().
Ingo Molnar53d3bc72016-05-11 08:25:53 +02003687 */
Peter Zijlstra2f950352016-05-11 19:27:56 +02003688 if (renorm && curr)
3689 se->vruntime += cfs_rq->min_vruntime;
3690
Ingo Molnarb7cc0892007-08-09 11:16:47 +02003691 update_curr(cfs_rq);
Peter Zijlstra2f950352016-05-11 19:27:56 +02003692
3693 /*
3694 * Otherwise, renormalise after, such that we're placed at the current
3695 * moment in time, instead of some random moment in the past. Being
3696 * placed in the past could significantly boost this task to the
3697 * fairness detriment of existing tasks.
3698 */
3699 if (renorm && !curr)
3700 se->vruntime += cfs_rq->min_vruntime;
3701
Vincent Guittot89ee0482016-12-21 16:50:26 +01003702 /*
3703 * When enqueuing a sched_entity, we must:
3704 * - Update loads to have both entity and cfs_rq synced with now.
3705 * - Add its load to cfs_rq->runnable_avg
3706 * - For group_entity, update its weight to reflect the new share of
3707 * its group cfs_rq
3708 * - Add its new weight to cfs_rq->load.weight
3709 */
Vincent Guittotd31b1a62016-11-08 10:53:44 +01003710 update_load_avg(se, UPDATE_TG);
Yuyang Du9d89c252015-07-15 08:04:37 +08003711 enqueue_entity_load_avg(cfs_rq, se);
Vincent Guittot89ee0482016-12-21 16:50:26 +01003712 update_cfs_shares(se);
Linus Torvalds17bc14b2012-12-14 07:20:43 -08003713 account_entity_enqueue(cfs_rq, se);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02003714
Josh Poimboeuf1a3d0272016-06-17 12:43:23 -05003715 if (flags & ENQUEUE_WAKEUP)
Peter Zijlstraaeb73b02007-10-15 17:00:05 +02003716 place_entity(cfs_rq, se, 0);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02003717
Mel Gormancb251762016-02-05 09:08:36 +00003718 check_schedstat_required();
Josh Poimboeuf4fa8d292016-06-17 12:43:26 -05003719 update_stats_enqueue(cfs_rq, se, flags);
3720 check_spread(cfs_rq, se);
Peter Zijlstra2f950352016-05-11 19:27:56 +02003721 if (!curr)
Srivatsa Vaddagiri83b699e2007-10-15 17:00:08 +02003722 __enqueue_entity(cfs_rq, se);
Peter Zijlstra2069dd72010-11-15 15:47:00 -08003723 se->on_rq = 1;
Peter Zijlstra3d4b47b2010-11-15 15:47:01 -08003724
Paul Turnerd3d9dc32011-07-21 09:43:39 -07003725 if (cfs_rq->nr_running == 1) {
Peter Zijlstra3d4b47b2010-11-15 15:47:01 -08003726 list_add_leaf_cfs_rq(cfs_rq);
Paul Turnerd3d9dc32011-07-21 09:43:39 -07003727 check_enqueue_throttle(cfs_rq);
3728 }
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02003729}
3730
Rik van Riel2c13c9192011-02-01 09:48:37 -05003731static void __clear_buddies_last(struct sched_entity *se)
Peter Zijlstra2002c692008-11-11 11:52:33 +01003732{
Rik van Riel2c13c9192011-02-01 09:48:37 -05003733 for_each_sched_entity(se) {
3734 struct cfs_rq *cfs_rq = cfs_rq_of(se);
Peter Zijlstraf1044792012-02-11 06:05:00 +01003735 if (cfs_rq->last != se)
Rik van Riel2c13c9192011-02-01 09:48:37 -05003736 break;
Peter Zijlstraf1044792012-02-11 06:05:00 +01003737
3738 cfs_rq->last = NULL;
Rik van Riel2c13c9192011-02-01 09:48:37 -05003739 }
3740}
Peter Zijlstra2002c692008-11-11 11:52:33 +01003741
Rik van Riel2c13c9192011-02-01 09:48:37 -05003742static void __clear_buddies_next(struct sched_entity *se)
3743{
3744 for_each_sched_entity(se) {
3745 struct cfs_rq *cfs_rq = cfs_rq_of(se);
Peter Zijlstraf1044792012-02-11 06:05:00 +01003746 if (cfs_rq->next != se)
Rik van Riel2c13c9192011-02-01 09:48:37 -05003747 break;
Peter Zijlstraf1044792012-02-11 06:05:00 +01003748
3749 cfs_rq->next = NULL;
Rik van Riel2c13c9192011-02-01 09:48:37 -05003750 }
Peter Zijlstra2002c692008-11-11 11:52:33 +01003751}
3752
Rik van Rielac53db52011-02-01 09:51:03 -05003753static void __clear_buddies_skip(struct sched_entity *se)
3754{
3755 for_each_sched_entity(se) {
3756 struct cfs_rq *cfs_rq = cfs_rq_of(se);
Peter Zijlstraf1044792012-02-11 06:05:00 +01003757 if (cfs_rq->skip != se)
Rik van Rielac53db52011-02-01 09:51:03 -05003758 break;
Peter Zijlstraf1044792012-02-11 06:05:00 +01003759
3760 cfs_rq->skip = NULL;
Rik van Rielac53db52011-02-01 09:51:03 -05003761 }
3762}
3763
Peter Zijlstraa571bbe2009-01-28 14:51:40 +01003764static void clear_buddies(struct cfs_rq *cfs_rq, struct sched_entity *se)
3765{
Rik van Riel2c13c9192011-02-01 09:48:37 -05003766 if (cfs_rq->last == se)
3767 __clear_buddies_last(se);
3768
3769 if (cfs_rq->next == se)
3770 __clear_buddies_next(se);
Rik van Rielac53db52011-02-01 09:51:03 -05003771
3772 if (cfs_rq->skip == se)
3773 __clear_buddies_skip(se);
Peter Zijlstraa571bbe2009-01-28 14:51:40 +01003774}
3775
Peter Zijlstra6c16a6d2012-03-21 13:07:16 -07003776static __always_inline void return_cfs_rq_runtime(struct cfs_rq *cfs_rq);
Paul Turnerd8b49862011-07-21 09:43:41 -07003777
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02003778static void
Peter Zijlstra371fd7e2010-03-24 16:38:48 +01003779dequeue_entity(struct cfs_rq *cfs_rq, struct sched_entity *se, int flags)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02003780{
Dmitry Adamushkoa2a2d682007-10-15 17:00:13 +02003781 /*
3782 * Update run-time statistics of the 'current'.
3783 */
3784 update_curr(cfs_rq);
Vincent Guittot89ee0482016-12-21 16:50:26 +01003785
3786 /*
3787 * When dequeuing a sched_entity, we must:
3788 * - Update loads to have both entity and cfs_rq synced with now.
3789 * - Substract its load from the cfs_rq->runnable_avg.
3790 * - Substract its previous weight from cfs_rq->load.weight.
3791 * - For group entity, update its weight to reflect the new share
3792 * of its group cfs_rq.
3793 */
Vincent Guittotd31b1a62016-11-08 10:53:44 +01003794 update_load_avg(se, UPDATE_TG);
Yuyang Du13962232015-07-15 08:04:41 +08003795 dequeue_entity_load_avg(cfs_rq, se);
Dmitry Adamushkoa2a2d682007-10-15 17:00:13 +02003796
Josh Poimboeuf4fa8d292016-06-17 12:43:26 -05003797 update_stats_dequeue(cfs_rq, se, flags);
Peter Zijlstra67e9fb22007-10-15 17:00:10 +02003798
Peter Zijlstra2002c692008-11-11 11:52:33 +01003799 clear_buddies(cfs_rq, se);
Peter Zijlstra47932412008-11-04 21:25:09 +01003800
Srivatsa Vaddagiri83b699e2007-10-15 17:00:08 +02003801 if (se != cfs_rq->curr)
Dmitry Adamushko30cfdcf2007-10-15 17:00:07 +02003802 __dequeue_entity(cfs_rq, se);
Linus Torvalds17bc14b2012-12-14 07:20:43 -08003803 se->on_rq = 0;
Dmitry Adamushko30cfdcf2007-10-15 17:00:07 +02003804 account_entity_dequeue(cfs_rq, se);
Peter Zijlstra88ec22d2009-12-16 18:04:41 +01003805
3806 /*
Peter Zijlstrab60205c2016-09-20 21:58:12 +02003807 * Normalize after update_curr(); which will also have moved
3808 * min_vruntime if @se is the one holding it back. But before doing
3809 * update_min_vruntime() again, which will discount @se's position and
3810 * can move min_vruntime forward still more.
Peter Zijlstra88ec22d2009-12-16 18:04:41 +01003811 */
Peter Zijlstra371fd7e2010-03-24 16:38:48 +01003812 if (!(flags & DEQUEUE_SLEEP))
Peter Zijlstra88ec22d2009-12-16 18:04:41 +01003813 se->vruntime -= cfs_rq->min_vruntime;
Peter Zijlstra1e876232011-05-17 16:21:10 -07003814
Paul Turnerd8b49862011-07-21 09:43:41 -07003815 /* return excess runtime on last dequeue */
3816 return_cfs_rq_runtime(cfs_rq);
3817
Vincent Guittot89ee0482016-12-21 16:50:26 +01003818 update_cfs_shares(se);
Peter Zijlstrab60205c2016-09-20 21:58:12 +02003819
3820 /*
3821 * Now advance min_vruntime if @se was the entity holding it back,
3822 * except when: DEQUEUE_SAVE && !DEQUEUE_MOVE, in this case we'll be
3823 * put back on, and if we advance min_vruntime, we'll be placed back
3824 * further than we started -- ie. we'll be penalized.
3825 */
3826 if ((flags & (DEQUEUE_SAVE | DEQUEUE_MOVE)) == DEQUEUE_SAVE)
3827 update_min_vruntime(cfs_rq);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02003828}
3829
3830/*
3831 * Preempt the current task with a newly woken task if needed:
3832 */
Peter Zijlstra7c92e542007-09-05 14:32:49 +02003833static void
Ingo Molnar2e09bf52007-10-15 17:00:05 +02003834check_preempt_tick(struct cfs_rq *cfs_rq, struct sched_entity *curr)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02003835{
Peter Zijlstra11697832007-09-05 14:32:49 +02003836 unsigned long ideal_runtime, delta_exec;
Wang Xingchaof4cfb332011-09-16 13:35:52 -04003837 struct sched_entity *se;
3838 s64 delta;
Peter Zijlstra11697832007-09-05 14:32:49 +02003839
Peter Zijlstra6d0f0eb2007-10-15 17:00:05 +02003840 ideal_runtime = sched_slice(cfs_rq, curr);
Peter Zijlstra11697832007-09-05 14:32:49 +02003841 delta_exec = curr->sum_exec_runtime - curr->prev_sum_exec_runtime;
Mike Galbraitha9f3e2b2009-01-28 14:51:39 +01003842 if (delta_exec > ideal_runtime) {
Kirill Tkhai88751252014-06-29 00:03:57 +04003843 resched_curr(rq_of(cfs_rq));
Mike Galbraitha9f3e2b2009-01-28 14:51:39 +01003844 /*
3845 * The current task ran long enough, ensure it doesn't get
3846 * re-elected due to buddy favours.
3847 */
3848 clear_buddies(cfs_rq, curr);
Mike Galbraithf685cea2009-10-23 23:09:22 +02003849 return;
3850 }
3851
3852 /*
3853 * Ensure that a task that missed wakeup preemption by a
3854 * narrow margin doesn't have to wait for a full slice.
3855 * This also mitigates buddy induced latencies under load.
3856 */
Mike Galbraithf685cea2009-10-23 23:09:22 +02003857 if (delta_exec < sysctl_sched_min_granularity)
3858 return;
3859
Wang Xingchaof4cfb332011-09-16 13:35:52 -04003860 se = __pick_first_entity(cfs_rq);
3861 delta = curr->vruntime - se->vruntime;
Mike Galbraithf685cea2009-10-23 23:09:22 +02003862
Wang Xingchaof4cfb332011-09-16 13:35:52 -04003863 if (delta < 0)
3864 return;
Mike Galbraithd7d82942011-01-05 05:41:17 +01003865
Wang Xingchaof4cfb332011-09-16 13:35:52 -04003866 if (delta > ideal_runtime)
Kirill Tkhai88751252014-06-29 00:03:57 +04003867 resched_curr(rq_of(cfs_rq));
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02003868}
3869
Srivatsa Vaddagiri83b699e2007-10-15 17:00:08 +02003870static void
Ingo Molnar8494f412007-08-09 11:16:48 +02003871set_next_entity(struct cfs_rq *cfs_rq, struct sched_entity *se)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02003872{
Srivatsa Vaddagiri83b699e2007-10-15 17:00:08 +02003873 /* 'current' is not kept within the tree. */
3874 if (se->on_rq) {
3875 /*
3876 * Any task has to be enqueued before it get to execute on
3877 * a CPU. So account for the time it spent waiting on the
3878 * runqueue.
3879 */
Josh Poimboeuf4fa8d292016-06-17 12:43:26 -05003880 update_stats_wait_end(cfs_rq, se);
Srivatsa Vaddagiri83b699e2007-10-15 17:00:08 +02003881 __dequeue_entity(cfs_rq, se);
Vincent Guittotd31b1a62016-11-08 10:53:44 +01003882 update_load_avg(se, UPDATE_TG);
Srivatsa Vaddagiri83b699e2007-10-15 17:00:08 +02003883 }
3884
Ingo Molnar79303e92007-08-09 11:16:47 +02003885 update_stats_curr_start(cfs_rq, se);
Ingo Molnar429d43b2007-10-15 17:00:03 +02003886 cfs_rq->curr = se;
Josh Poimboeuf4fa8d292016-06-17 12:43:26 -05003887
Ingo Molnareba1ed42007-10-15 17:00:02 +02003888 /*
3889 * Track our maximum slice length, if the CPU's load is at
3890 * least twice that of our own weight (i.e. dont track it
3891 * when there are only lesser-weight tasks around):
3892 */
Mel Gormancb251762016-02-05 09:08:36 +00003893 if (schedstat_enabled() && rq_of(cfs_rq)->load.weight >= 2*se->load.weight) {
Josh Poimboeuf4fa8d292016-06-17 12:43:26 -05003894 schedstat_set(se->statistics.slice_max,
3895 max((u64)schedstat_val(se->statistics.slice_max),
3896 se->sum_exec_runtime - se->prev_sum_exec_runtime));
Ingo Molnareba1ed42007-10-15 17:00:02 +02003897 }
Josh Poimboeuf4fa8d292016-06-17 12:43:26 -05003898
Peter Zijlstra4a55b452007-09-05 14:32:49 +02003899 se->prev_sum_exec_runtime = se->sum_exec_runtime;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02003900}
3901
Peter Zijlstra3f3a4902008-10-24 11:06:16 +02003902static int
3903wakeup_preempt_entity(struct sched_entity *curr, struct sched_entity *se);
3904
Rik van Rielac53db52011-02-01 09:51:03 -05003905/*
3906 * Pick the next process, keeping these things in mind, in this order:
3907 * 1) keep things fair between processes/task groups
3908 * 2) pick the "next" process, since someone really wants that to run
3909 * 3) pick the "last" process, for cache locality
3910 * 4) do not run the "skip" process, if something else is available
3911 */
Peter Zijlstra678d5712012-02-11 06:05:00 +01003912static struct sched_entity *
3913pick_next_entity(struct cfs_rq *cfs_rq, struct sched_entity *curr)
Peter Zijlstraaa2ac252008-03-14 21:12:12 +01003914{
Peter Zijlstra678d5712012-02-11 06:05:00 +01003915 struct sched_entity *left = __pick_first_entity(cfs_rq);
3916 struct sched_entity *se;
3917
3918 /*
3919 * If curr is set we have to see if its left of the leftmost entity
3920 * still in the tree, provided there was anything in the tree at all.
3921 */
3922 if (!left || (curr && entity_before(curr, left)))
3923 left = curr;
3924
3925 se = left; /* ideally we run the leftmost entity */
Peter Zijlstraf4b67552008-11-04 21:25:07 +01003926
Rik van Rielac53db52011-02-01 09:51:03 -05003927 /*
3928 * Avoid running the skip buddy, if running something else can
3929 * be done without getting too unfair.
3930 */
3931 if (cfs_rq->skip == se) {
Peter Zijlstra678d5712012-02-11 06:05:00 +01003932 struct sched_entity *second;
3933
3934 if (se == curr) {
3935 second = __pick_first_entity(cfs_rq);
3936 } else {
3937 second = __pick_next_entity(se);
3938 if (!second || (curr && entity_before(curr, second)))
3939 second = curr;
3940 }
3941
Rik van Rielac53db52011-02-01 09:51:03 -05003942 if (second && wakeup_preempt_entity(second, left) < 1)
3943 se = second;
3944 }
Peter Zijlstraaa2ac252008-03-14 21:12:12 +01003945
Mike Galbraithf685cea2009-10-23 23:09:22 +02003946 /*
3947 * Prefer last buddy, try to return the CPU to a preempted task.
3948 */
3949 if (cfs_rq->last && wakeup_preempt_entity(cfs_rq->last, left) < 1)
3950 se = cfs_rq->last;
3951
Rik van Rielac53db52011-02-01 09:51:03 -05003952 /*
3953 * Someone really wants this to run. If it's not unfair, run it.
3954 */
3955 if (cfs_rq->next && wakeup_preempt_entity(cfs_rq->next, left) < 1)
3956 se = cfs_rq->next;
3957
Mike Galbraithf685cea2009-10-23 23:09:22 +02003958 clear_buddies(cfs_rq, se);
Peter Zijlstra47932412008-11-04 21:25:09 +01003959
3960 return se;
Peter Zijlstraaa2ac252008-03-14 21:12:12 +01003961}
3962
Peter Zijlstra678d5712012-02-11 06:05:00 +01003963static bool check_cfs_rq_runtime(struct cfs_rq *cfs_rq);
Paul Turnerd3d9dc32011-07-21 09:43:39 -07003964
Ingo Molnarab6cde22007-08-09 11:16:48 +02003965static void put_prev_entity(struct cfs_rq *cfs_rq, struct sched_entity *prev)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02003966{
3967 /*
3968 * If still on the runqueue then deactivate_task()
3969 * was not called and update_curr() has to be done:
3970 */
3971 if (prev->on_rq)
Ingo Molnarb7cc0892007-08-09 11:16:47 +02003972 update_curr(cfs_rq);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02003973
Paul Turnerd3d9dc32011-07-21 09:43:39 -07003974 /* throttle cfs_rqs exceeding runtime */
3975 check_cfs_rq_runtime(cfs_rq);
3976
Josh Poimboeuf4fa8d292016-06-17 12:43:26 -05003977 check_spread(cfs_rq, prev);
Mel Gormancb251762016-02-05 09:08:36 +00003978
Dmitry Adamushko30cfdcf2007-10-15 17:00:07 +02003979 if (prev->on_rq) {
Josh Poimboeuf4fa8d292016-06-17 12:43:26 -05003980 update_stats_wait_start(cfs_rq, prev);
Dmitry Adamushko30cfdcf2007-10-15 17:00:07 +02003981 /* Put 'current' back into the tree. */
3982 __enqueue_entity(cfs_rq, prev);
Paul Turner9d85f212012-10-04 13:18:29 +02003983 /* in !on_rq case, update occurred at dequeue */
Yuyang Du9d89c252015-07-15 08:04:37 +08003984 update_load_avg(prev, 0);
Dmitry Adamushko30cfdcf2007-10-15 17:00:07 +02003985 }
Ingo Molnar429d43b2007-10-15 17:00:03 +02003986 cfs_rq->curr = NULL;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02003987}
3988
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01003989static void
3990entity_tick(struct cfs_rq *cfs_rq, struct sched_entity *curr, int queued)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02003991{
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02003992 /*
Dmitry Adamushko30cfdcf2007-10-15 17:00:07 +02003993 * Update run-time statistics of the 'current'.
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02003994 */
Dmitry Adamushko30cfdcf2007-10-15 17:00:07 +02003995 update_curr(cfs_rq);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02003996
Paul Turner43365bd2010-12-15 19:10:17 -08003997 /*
Paul Turner9d85f212012-10-04 13:18:29 +02003998 * Ensure that runnable average is periodically updated.
3999 */
Vincent Guittotd31b1a62016-11-08 10:53:44 +01004000 update_load_avg(curr, UPDATE_TG);
Vincent Guittot89ee0482016-12-21 16:50:26 +01004001 update_cfs_shares(curr);
Paul Turner9d85f212012-10-04 13:18:29 +02004002
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01004003#ifdef CONFIG_SCHED_HRTICK
4004 /*
4005 * queued ticks are scheduled to match the slice, so don't bother
4006 * validating it and just reschedule.
4007 */
Harvey Harrison983ed7a2008-04-24 18:17:55 -07004008 if (queued) {
Kirill Tkhai88751252014-06-29 00:03:57 +04004009 resched_curr(rq_of(cfs_rq));
Harvey Harrison983ed7a2008-04-24 18:17:55 -07004010 return;
4011 }
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01004012 /*
4013 * don't let the period tick interfere with the hrtick preemption
4014 */
4015 if (!sched_feat(DOUBLE_TICK) &&
4016 hrtimer_active(&rq_of(cfs_rq)->hrtick_timer))
4017 return;
4018#endif
4019
Yong Zhang2c2efae2011-07-29 16:20:33 +08004020 if (cfs_rq->nr_running > 1)
Ingo Molnar2e09bf52007-10-15 17:00:05 +02004021 check_preempt_tick(cfs_rq, curr);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02004022}
4023
Paul Turnerab84d312011-07-21 09:43:28 -07004024
4025/**************************************************
4026 * CFS bandwidth control machinery
4027 */
4028
4029#ifdef CONFIG_CFS_BANDWIDTH
Peter Zijlstra029632f2011-10-25 10:00:11 +02004030
4031#ifdef HAVE_JUMP_LABEL
Ingo Molnarc5905af2012-02-24 08:31:31 +01004032static struct static_key __cfs_bandwidth_used;
Peter Zijlstra029632f2011-10-25 10:00:11 +02004033
4034static inline bool cfs_bandwidth_used(void)
4035{
Ingo Molnarc5905af2012-02-24 08:31:31 +01004036 return static_key_false(&__cfs_bandwidth_used);
Peter Zijlstra029632f2011-10-25 10:00:11 +02004037}
4038
Ben Segall1ee14e62013-10-16 11:16:12 -07004039void cfs_bandwidth_usage_inc(void)
Peter Zijlstra029632f2011-10-25 10:00:11 +02004040{
Ben Segall1ee14e62013-10-16 11:16:12 -07004041 static_key_slow_inc(&__cfs_bandwidth_used);
4042}
4043
4044void cfs_bandwidth_usage_dec(void)
4045{
4046 static_key_slow_dec(&__cfs_bandwidth_used);
Peter Zijlstra029632f2011-10-25 10:00:11 +02004047}
4048#else /* HAVE_JUMP_LABEL */
4049static bool cfs_bandwidth_used(void)
4050{
4051 return true;
4052}
4053
Ben Segall1ee14e62013-10-16 11:16:12 -07004054void cfs_bandwidth_usage_inc(void) {}
4055void cfs_bandwidth_usage_dec(void) {}
Peter Zijlstra029632f2011-10-25 10:00:11 +02004056#endif /* HAVE_JUMP_LABEL */
4057
Paul Turnerab84d312011-07-21 09:43:28 -07004058/*
4059 * default period for cfs group bandwidth.
4060 * default: 0.1s, units: nanoseconds
4061 */
4062static inline u64 default_cfs_period(void)
4063{
4064 return 100000000ULL;
4065}
Paul Turnerec12cb72011-07-21 09:43:30 -07004066
4067static inline u64 sched_cfs_bandwidth_slice(void)
4068{
4069 return (u64)sysctl_sched_cfs_bandwidth_slice * NSEC_PER_USEC;
4070}
4071
Paul Turnera9cf55b2011-07-21 09:43:32 -07004072/*
4073 * Replenish runtime according to assigned quota and update expiration time.
4074 * We use sched_clock_cpu directly instead of rq->clock to avoid adding
4075 * additional synchronization around rq->lock.
4076 *
4077 * requires cfs_b->lock
4078 */
Peter Zijlstra029632f2011-10-25 10:00:11 +02004079void __refill_cfs_bandwidth_runtime(struct cfs_bandwidth *cfs_b)
Paul Turnera9cf55b2011-07-21 09:43:32 -07004080{
4081 u64 now;
4082
4083 if (cfs_b->quota == RUNTIME_INF)
4084 return;
4085
4086 now = sched_clock_cpu(smp_processor_id());
4087 cfs_b->runtime = cfs_b->quota;
4088 cfs_b->runtime_expires = now + ktime_to_ns(cfs_b->period);
4089}
4090
Peter Zijlstra029632f2011-10-25 10:00:11 +02004091static inline struct cfs_bandwidth *tg_cfs_bandwidth(struct task_group *tg)
4092{
4093 return &tg->cfs_bandwidth;
4094}
4095
Paul Turnerf1b17282012-10-04 13:18:31 +02004096/* rq->task_clock normalized against any time this cfs_rq has spent throttled */
4097static inline u64 cfs_rq_clock_task(struct cfs_rq *cfs_rq)
4098{
4099 if (unlikely(cfs_rq->throttle_count))
Xunlei Pang1a99ae32016-05-10 21:03:18 +08004100 return cfs_rq->throttled_clock_task - cfs_rq->throttled_clock_task_time;
Paul Turnerf1b17282012-10-04 13:18:31 +02004101
Frederic Weisbecker78becc22013-04-12 01:51:02 +02004102 return rq_clock_task(rq_of(cfs_rq)) - cfs_rq->throttled_clock_task_time;
Paul Turnerf1b17282012-10-04 13:18:31 +02004103}
4104
Paul Turner85dac902011-07-21 09:43:33 -07004105/* returns 0 on failure to allocate runtime */
4106static int assign_cfs_rq_runtime(struct cfs_rq *cfs_rq)
Paul Turnerec12cb72011-07-21 09:43:30 -07004107{
4108 struct task_group *tg = cfs_rq->tg;
4109 struct cfs_bandwidth *cfs_b = tg_cfs_bandwidth(tg);
Paul Turnera9cf55b2011-07-21 09:43:32 -07004110 u64 amount = 0, min_amount, expires;
Paul Turnerec12cb72011-07-21 09:43:30 -07004111
4112 /* note: this is a positive sum as runtime_remaining <= 0 */
4113 min_amount = sched_cfs_bandwidth_slice() - cfs_rq->runtime_remaining;
4114
4115 raw_spin_lock(&cfs_b->lock);
4116 if (cfs_b->quota == RUNTIME_INF)
4117 amount = min_amount;
Paul Turner58088ad2011-07-21 09:43:31 -07004118 else {
Peter Zijlstra77a4d1a2015-04-15 11:41:57 +02004119 start_cfs_bandwidth(cfs_b);
Paul Turner58088ad2011-07-21 09:43:31 -07004120
4121 if (cfs_b->runtime > 0) {
4122 amount = min(cfs_b->runtime, min_amount);
4123 cfs_b->runtime -= amount;
4124 cfs_b->idle = 0;
4125 }
Paul Turnerec12cb72011-07-21 09:43:30 -07004126 }
Paul Turnera9cf55b2011-07-21 09:43:32 -07004127 expires = cfs_b->runtime_expires;
Paul Turnerec12cb72011-07-21 09:43:30 -07004128 raw_spin_unlock(&cfs_b->lock);
4129
4130 cfs_rq->runtime_remaining += amount;
Paul Turnera9cf55b2011-07-21 09:43:32 -07004131 /*
4132 * we may have advanced our local expiration to account for allowed
4133 * spread between our sched_clock and the one on which runtime was
4134 * issued.
4135 */
4136 if ((s64)(expires - cfs_rq->runtime_expires) > 0)
4137 cfs_rq->runtime_expires = expires;
Paul Turner85dac902011-07-21 09:43:33 -07004138
4139 return cfs_rq->runtime_remaining > 0;
Paul Turnera9cf55b2011-07-21 09:43:32 -07004140}
4141
4142/*
4143 * Note: This depends on the synchronization provided by sched_clock and the
4144 * fact that rq->clock snapshots this value.
4145 */
4146static void expire_cfs_rq_runtime(struct cfs_rq *cfs_rq)
4147{
4148 struct cfs_bandwidth *cfs_b = tg_cfs_bandwidth(cfs_rq->tg);
Paul Turnera9cf55b2011-07-21 09:43:32 -07004149
4150 /* if the deadline is ahead of our clock, nothing to do */
Frederic Weisbecker78becc22013-04-12 01:51:02 +02004151 if (likely((s64)(rq_clock(rq_of(cfs_rq)) - cfs_rq->runtime_expires) < 0))
Paul Turnera9cf55b2011-07-21 09:43:32 -07004152 return;
4153
4154 if (cfs_rq->runtime_remaining < 0)
4155 return;
4156
4157 /*
4158 * If the local deadline has passed we have to consider the
4159 * possibility that our sched_clock is 'fast' and the global deadline
4160 * has not truly expired.
4161 *
4162 * Fortunately we can check determine whether this the case by checking
Ben Segall51f21762014-05-19 15:49:45 -07004163 * whether the global deadline has advanced. It is valid to compare
4164 * cfs_b->runtime_expires without any locks since we only care about
4165 * exact equality, so a partial write will still work.
Paul Turnera9cf55b2011-07-21 09:43:32 -07004166 */
4167
Ben Segall51f21762014-05-19 15:49:45 -07004168 if (cfs_rq->runtime_expires != cfs_b->runtime_expires) {
Paul Turnera9cf55b2011-07-21 09:43:32 -07004169 /* extend local deadline, drift is bounded above by 2 ticks */
4170 cfs_rq->runtime_expires += TICK_NSEC;
4171 } else {
4172 /* global deadline is ahead, expiration has passed */
4173 cfs_rq->runtime_remaining = 0;
4174 }
Paul Turnerec12cb72011-07-21 09:43:30 -07004175}
4176
Peter Zijlstra9dbdb152013-11-18 18:27:06 +01004177static void __account_cfs_rq_runtime(struct cfs_rq *cfs_rq, u64 delta_exec)
Paul Turnerec12cb72011-07-21 09:43:30 -07004178{
Paul Turnera9cf55b2011-07-21 09:43:32 -07004179 /* dock delta_exec before expiring quota (as it could span periods) */
Paul Turnerec12cb72011-07-21 09:43:30 -07004180 cfs_rq->runtime_remaining -= delta_exec;
Paul Turnera9cf55b2011-07-21 09:43:32 -07004181 expire_cfs_rq_runtime(cfs_rq);
4182
4183 if (likely(cfs_rq->runtime_remaining > 0))
Paul Turnerec12cb72011-07-21 09:43:30 -07004184 return;
4185
Paul Turner85dac902011-07-21 09:43:33 -07004186 /*
4187 * if we're unable to extend our runtime we resched so that the active
4188 * hierarchy can be throttled
4189 */
4190 if (!assign_cfs_rq_runtime(cfs_rq) && likely(cfs_rq->curr))
Kirill Tkhai88751252014-06-29 00:03:57 +04004191 resched_curr(rq_of(cfs_rq));
Paul Turnerec12cb72011-07-21 09:43:30 -07004192}
4193
Peter Zijlstra6c16a6d2012-03-21 13:07:16 -07004194static __always_inline
Peter Zijlstra9dbdb152013-11-18 18:27:06 +01004195void account_cfs_rq_runtime(struct cfs_rq *cfs_rq, u64 delta_exec)
Paul Turnerec12cb72011-07-21 09:43:30 -07004196{
Paul Turner56f570e2011-11-07 20:26:33 -08004197 if (!cfs_bandwidth_used() || !cfs_rq->runtime_enabled)
Paul Turnerec12cb72011-07-21 09:43:30 -07004198 return;
4199
4200 __account_cfs_rq_runtime(cfs_rq, delta_exec);
4201}
4202
Paul Turner85dac902011-07-21 09:43:33 -07004203static inline int cfs_rq_throttled(struct cfs_rq *cfs_rq)
4204{
Paul Turner56f570e2011-11-07 20:26:33 -08004205 return cfs_bandwidth_used() && cfs_rq->throttled;
Paul Turner85dac902011-07-21 09:43:33 -07004206}
4207
Paul Turner64660c82011-07-21 09:43:36 -07004208/* check whether cfs_rq, or any parent, is throttled */
4209static inline int throttled_hierarchy(struct cfs_rq *cfs_rq)
4210{
Paul Turner56f570e2011-11-07 20:26:33 -08004211 return cfs_bandwidth_used() && cfs_rq->throttle_count;
Paul Turner64660c82011-07-21 09:43:36 -07004212}
4213
4214/*
4215 * Ensure that neither of the group entities corresponding to src_cpu or
4216 * dest_cpu are members of a throttled hierarchy when performing group
4217 * load-balance operations.
4218 */
4219static inline int throttled_lb_pair(struct task_group *tg,
4220 int src_cpu, int dest_cpu)
4221{
4222 struct cfs_rq *src_cfs_rq, *dest_cfs_rq;
4223
4224 src_cfs_rq = tg->cfs_rq[src_cpu];
4225 dest_cfs_rq = tg->cfs_rq[dest_cpu];
4226
4227 return throttled_hierarchy(src_cfs_rq) ||
4228 throttled_hierarchy(dest_cfs_rq);
4229}
4230
4231/* updated child weight may affect parent so we have to do this bottom up */
4232static int tg_unthrottle_up(struct task_group *tg, void *data)
4233{
4234 struct rq *rq = data;
4235 struct cfs_rq *cfs_rq = tg->cfs_rq[cpu_of(rq)];
4236
4237 cfs_rq->throttle_count--;
Paul Turner64660c82011-07-21 09:43:36 -07004238 if (!cfs_rq->throttle_count) {
Paul Turnerf1b17282012-10-04 13:18:31 +02004239 /* adjust cfs_rq_clock_task() */
Frederic Weisbecker78becc22013-04-12 01:51:02 +02004240 cfs_rq->throttled_clock_task_time += rq_clock_task(rq) -
Paul Turnerf1b17282012-10-04 13:18:31 +02004241 cfs_rq->throttled_clock_task;
Paul Turner64660c82011-07-21 09:43:36 -07004242 }
Paul Turner64660c82011-07-21 09:43:36 -07004243
4244 return 0;
4245}
4246
4247static int tg_throttle_down(struct task_group *tg, void *data)
4248{
4249 struct rq *rq = data;
4250 struct cfs_rq *cfs_rq = tg->cfs_rq[cpu_of(rq)];
4251
Paul Turner82958362012-10-04 13:18:31 +02004252 /* group is entering throttled state, stop time */
4253 if (!cfs_rq->throttle_count)
Frederic Weisbecker78becc22013-04-12 01:51:02 +02004254 cfs_rq->throttled_clock_task = rq_clock_task(rq);
Paul Turner64660c82011-07-21 09:43:36 -07004255 cfs_rq->throttle_count++;
4256
4257 return 0;
4258}
4259
Paul Turnerd3d9dc32011-07-21 09:43:39 -07004260static void throttle_cfs_rq(struct cfs_rq *cfs_rq)
Paul Turner85dac902011-07-21 09:43:33 -07004261{
4262 struct rq *rq = rq_of(cfs_rq);
4263 struct cfs_bandwidth *cfs_b = tg_cfs_bandwidth(cfs_rq->tg);
4264 struct sched_entity *se;
4265 long task_delta, dequeue = 1;
Peter Zijlstra77a4d1a2015-04-15 11:41:57 +02004266 bool empty;
Paul Turner85dac902011-07-21 09:43:33 -07004267
4268 se = cfs_rq->tg->se[cpu_of(rq_of(cfs_rq))];
4269
Paul Turnerf1b17282012-10-04 13:18:31 +02004270 /* freeze hierarchy runnable averages while throttled */
Paul Turner64660c82011-07-21 09:43:36 -07004271 rcu_read_lock();
4272 walk_tg_tree_from(cfs_rq->tg, tg_throttle_down, tg_nop, (void *)rq);
4273 rcu_read_unlock();
Paul Turner85dac902011-07-21 09:43:33 -07004274
4275 task_delta = cfs_rq->h_nr_running;
4276 for_each_sched_entity(se) {
4277 struct cfs_rq *qcfs_rq = cfs_rq_of(se);
4278 /* throttled entity or throttle-on-deactivate */
4279 if (!se->on_rq)
4280 break;
4281
4282 if (dequeue)
4283 dequeue_entity(qcfs_rq, se, DEQUEUE_SLEEP);
4284 qcfs_rq->h_nr_running -= task_delta;
4285
4286 if (qcfs_rq->load.weight)
4287 dequeue = 0;
4288 }
4289
4290 if (!se)
Kirill Tkhai72465442014-05-09 03:00:14 +04004291 sub_nr_running(rq, task_delta);
Paul Turner85dac902011-07-21 09:43:33 -07004292
4293 cfs_rq->throttled = 1;
Frederic Weisbecker78becc22013-04-12 01:51:02 +02004294 cfs_rq->throttled_clock = rq_clock(rq);
Paul Turner85dac902011-07-21 09:43:33 -07004295 raw_spin_lock(&cfs_b->lock);
Cong Wangd49db342015-06-24 12:41:47 -07004296 empty = list_empty(&cfs_b->throttled_cfs_rq);
Peter Zijlstra77a4d1a2015-04-15 11:41:57 +02004297
Ben Segallc06f04c2014-06-20 15:21:20 -07004298 /*
4299 * Add to the _head_ of the list, so that an already-started
4300 * distribute_cfs_runtime will not see us
4301 */
4302 list_add_rcu(&cfs_rq->throttled_list, &cfs_b->throttled_cfs_rq);
Peter Zijlstra77a4d1a2015-04-15 11:41:57 +02004303
4304 /*
4305 * If we're the first throttled task, make sure the bandwidth
4306 * timer is running.
4307 */
4308 if (empty)
4309 start_cfs_bandwidth(cfs_b);
4310
Paul Turner85dac902011-07-21 09:43:33 -07004311 raw_spin_unlock(&cfs_b->lock);
4312}
4313
Peter Zijlstra029632f2011-10-25 10:00:11 +02004314void unthrottle_cfs_rq(struct cfs_rq *cfs_rq)
Paul Turner671fd9d2011-07-21 09:43:34 -07004315{
4316 struct rq *rq = rq_of(cfs_rq);
4317 struct cfs_bandwidth *cfs_b = tg_cfs_bandwidth(cfs_rq->tg);
4318 struct sched_entity *se;
4319 int enqueue = 1;
4320 long task_delta;
4321
Michael Wang22b958d2013-06-04 14:23:39 +08004322 se = cfs_rq->tg->se[cpu_of(rq)];
Paul Turner671fd9d2011-07-21 09:43:34 -07004323
4324 cfs_rq->throttled = 0;
Frederic Weisbecker1a55af22013-04-12 01:51:01 +02004325
4326 update_rq_clock(rq);
4327
Paul Turner671fd9d2011-07-21 09:43:34 -07004328 raw_spin_lock(&cfs_b->lock);
Frederic Weisbecker78becc22013-04-12 01:51:02 +02004329 cfs_b->throttled_time += rq_clock(rq) - cfs_rq->throttled_clock;
Paul Turner671fd9d2011-07-21 09:43:34 -07004330 list_del_rcu(&cfs_rq->throttled_list);
4331 raw_spin_unlock(&cfs_b->lock);
4332
Paul Turner64660c82011-07-21 09:43:36 -07004333 /* update hierarchical throttle state */
4334 walk_tg_tree_from(cfs_rq->tg, tg_nop, tg_unthrottle_up, (void *)rq);
4335
Paul Turner671fd9d2011-07-21 09:43:34 -07004336 if (!cfs_rq->load.weight)
4337 return;
4338
4339 task_delta = cfs_rq->h_nr_running;
4340 for_each_sched_entity(se) {
4341 if (se->on_rq)
4342 enqueue = 0;
4343
4344 cfs_rq = cfs_rq_of(se);
4345 if (enqueue)
4346 enqueue_entity(cfs_rq, se, ENQUEUE_WAKEUP);
4347 cfs_rq->h_nr_running += task_delta;
4348
4349 if (cfs_rq_throttled(cfs_rq))
4350 break;
4351 }
4352
4353 if (!se)
Kirill Tkhai72465442014-05-09 03:00:14 +04004354 add_nr_running(rq, task_delta);
Paul Turner671fd9d2011-07-21 09:43:34 -07004355
4356 /* determine whether we need to wake up potentially idle cpu */
4357 if (rq->curr == rq->idle && rq->cfs.nr_running)
Kirill Tkhai88751252014-06-29 00:03:57 +04004358 resched_curr(rq);
Paul Turner671fd9d2011-07-21 09:43:34 -07004359}
4360
4361static u64 distribute_cfs_runtime(struct cfs_bandwidth *cfs_b,
4362 u64 remaining, u64 expires)
4363{
4364 struct cfs_rq *cfs_rq;
Ben Segallc06f04c2014-06-20 15:21:20 -07004365 u64 runtime;
4366 u64 starting_runtime = remaining;
Paul Turner671fd9d2011-07-21 09:43:34 -07004367
4368 rcu_read_lock();
4369 list_for_each_entry_rcu(cfs_rq, &cfs_b->throttled_cfs_rq,
4370 throttled_list) {
4371 struct rq *rq = rq_of(cfs_rq);
Peter Zijlstra8a8c69c2016-10-04 16:04:35 +02004372 struct rq_flags rf;
Paul Turner671fd9d2011-07-21 09:43:34 -07004373
Peter Zijlstra8a8c69c2016-10-04 16:04:35 +02004374 rq_lock(rq, &rf);
Paul Turner671fd9d2011-07-21 09:43:34 -07004375 if (!cfs_rq_throttled(cfs_rq))
4376 goto next;
4377
4378 runtime = -cfs_rq->runtime_remaining + 1;
4379 if (runtime > remaining)
4380 runtime = remaining;
4381 remaining -= runtime;
4382
4383 cfs_rq->runtime_remaining += runtime;
4384 cfs_rq->runtime_expires = expires;
4385
4386 /* we check whether we're throttled above */
4387 if (cfs_rq->runtime_remaining > 0)
4388 unthrottle_cfs_rq(cfs_rq);
4389
4390next:
Peter Zijlstra8a8c69c2016-10-04 16:04:35 +02004391 rq_unlock(rq, &rf);
Paul Turner671fd9d2011-07-21 09:43:34 -07004392
4393 if (!remaining)
4394 break;
4395 }
4396 rcu_read_unlock();
4397
Ben Segallc06f04c2014-06-20 15:21:20 -07004398 return starting_runtime - remaining;
Paul Turner671fd9d2011-07-21 09:43:34 -07004399}
4400
Paul Turner58088ad2011-07-21 09:43:31 -07004401/*
4402 * Responsible for refilling a task_group's bandwidth and unthrottling its
4403 * cfs_rqs as appropriate. If there has been no activity within the last
4404 * period the timer is deactivated until scheduling resumes; cfs_b->idle is
4405 * used to track this state.
4406 */
4407static int do_sched_cfs_period_timer(struct cfs_bandwidth *cfs_b, int overrun)
4408{
Paul Turner671fd9d2011-07-21 09:43:34 -07004409 u64 runtime, runtime_expires;
Ben Segall51f21762014-05-19 15:49:45 -07004410 int throttled;
Paul Turner58088ad2011-07-21 09:43:31 -07004411
Paul Turner58088ad2011-07-21 09:43:31 -07004412 /* no need to continue the timer with no bandwidth constraint */
4413 if (cfs_b->quota == RUNTIME_INF)
Ben Segall51f21762014-05-19 15:49:45 -07004414 goto out_deactivate;
Paul Turner58088ad2011-07-21 09:43:31 -07004415
Paul Turner671fd9d2011-07-21 09:43:34 -07004416 throttled = !list_empty(&cfs_b->throttled_cfs_rq);
Nikhil Raoe8da1b12011-07-21 09:43:40 -07004417 cfs_b->nr_periods += overrun;
Paul Turner671fd9d2011-07-21 09:43:34 -07004418
Ben Segall51f21762014-05-19 15:49:45 -07004419 /*
4420 * idle depends on !throttled (for the case of a large deficit), and if
4421 * we're going inactive then everything else can be deferred
4422 */
4423 if (cfs_b->idle && !throttled)
4424 goto out_deactivate;
Paul Turnera9cf55b2011-07-21 09:43:32 -07004425
4426 __refill_cfs_bandwidth_runtime(cfs_b);
4427
Paul Turner671fd9d2011-07-21 09:43:34 -07004428 if (!throttled) {
4429 /* mark as potentially idle for the upcoming period */
4430 cfs_b->idle = 1;
Ben Segall51f21762014-05-19 15:49:45 -07004431 return 0;
Paul Turner671fd9d2011-07-21 09:43:34 -07004432 }
Paul Turner58088ad2011-07-21 09:43:31 -07004433
Nikhil Raoe8da1b12011-07-21 09:43:40 -07004434 /* account preceding periods in which throttling occurred */
4435 cfs_b->nr_throttled += overrun;
4436
Paul Turner671fd9d2011-07-21 09:43:34 -07004437 runtime_expires = cfs_b->runtime_expires;
Paul Turner671fd9d2011-07-21 09:43:34 -07004438
4439 /*
Ben Segallc06f04c2014-06-20 15:21:20 -07004440 * This check is repeated as we are holding onto the new bandwidth while
4441 * we unthrottle. This can potentially race with an unthrottled group
4442 * trying to acquire new bandwidth from the global pool. This can result
4443 * in us over-using our runtime if it is all used during this loop, but
4444 * only by limited amounts in that extreme case.
Paul Turner671fd9d2011-07-21 09:43:34 -07004445 */
Ben Segallc06f04c2014-06-20 15:21:20 -07004446 while (throttled && cfs_b->runtime > 0) {
4447 runtime = cfs_b->runtime;
Paul Turner671fd9d2011-07-21 09:43:34 -07004448 raw_spin_unlock(&cfs_b->lock);
4449 /* we can't nest cfs_b->lock while distributing bandwidth */
4450 runtime = distribute_cfs_runtime(cfs_b, runtime,
4451 runtime_expires);
4452 raw_spin_lock(&cfs_b->lock);
4453
4454 throttled = !list_empty(&cfs_b->throttled_cfs_rq);
Ben Segallc06f04c2014-06-20 15:21:20 -07004455
4456 cfs_b->runtime -= min(runtime, cfs_b->runtime);
Paul Turner671fd9d2011-07-21 09:43:34 -07004457 }
4458
Paul Turner671fd9d2011-07-21 09:43:34 -07004459 /*
4460 * While we are ensured activity in the period following an
4461 * unthrottle, this also covers the case in which the new bandwidth is
4462 * insufficient to cover the existing bandwidth deficit. (Forcing the
4463 * timer to remain active while there are any throttled entities.)
4464 */
4465 cfs_b->idle = 0;
Paul Turner58088ad2011-07-21 09:43:31 -07004466
Ben Segall51f21762014-05-19 15:49:45 -07004467 return 0;
4468
4469out_deactivate:
Ben Segall51f21762014-05-19 15:49:45 -07004470 return 1;
Paul Turner58088ad2011-07-21 09:43:31 -07004471}
Paul Turnerd3d9dc32011-07-21 09:43:39 -07004472
Paul Turnerd8b49862011-07-21 09:43:41 -07004473/* a cfs_rq won't donate quota below this amount */
4474static const u64 min_cfs_rq_runtime = 1 * NSEC_PER_MSEC;
4475/* minimum remaining period time to redistribute slack quota */
4476static const u64 min_bandwidth_expiration = 2 * NSEC_PER_MSEC;
4477/* how long we wait to gather additional slack before distributing */
4478static const u64 cfs_bandwidth_slack_period = 5 * NSEC_PER_MSEC;
4479
Ben Segalldb06e782013-10-16 11:16:17 -07004480/*
4481 * Are we near the end of the current quota period?
4482 *
4483 * Requires cfs_b->lock for hrtimer_expires_remaining to be safe against the
Thomas Gleixner4961b6e2015-04-14 21:09:05 +00004484 * hrtimer base being cleared by hrtimer_start. In the case of
Ben Segalldb06e782013-10-16 11:16:17 -07004485 * migrate_hrtimers, base is never cleared, so we are fine.
4486 */
Paul Turnerd8b49862011-07-21 09:43:41 -07004487static int runtime_refresh_within(struct cfs_bandwidth *cfs_b, u64 min_expire)
4488{
4489 struct hrtimer *refresh_timer = &cfs_b->period_timer;
4490 u64 remaining;
4491
4492 /* if the call-back is running a quota refresh is already occurring */
4493 if (hrtimer_callback_running(refresh_timer))
4494 return 1;
4495
4496 /* is a quota refresh about to occur? */
4497 remaining = ktime_to_ns(hrtimer_expires_remaining(refresh_timer));
4498 if (remaining < min_expire)
4499 return 1;
4500
4501 return 0;
4502}
4503
4504static void start_cfs_slack_bandwidth(struct cfs_bandwidth *cfs_b)
4505{
4506 u64 min_left = cfs_bandwidth_slack_period + min_bandwidth_expiration;
4507
4508 /* if there's a quota refresh soon don't bother with slack */
4509 if (runtime_refresh_within(cfs_b, min_left))
4510 return;
4511
Peter Zijlstra4cfafd32015-05-14 12:23:11 +02004512 hrtimer_start(&cfs_b->slack_timer,
4513 ns_to_ktime(cfs_bandwidth_slack_period),
4514 HRTIMER_MODE_REL);
Paul Turnerd8b49862011-07-21 09:43:41 -07004515}
4516
4517/* we know any runtime found here is valid as update_curr() precedes return */
4518static void __return_cfs_rq_runtime(struct cfs_rq *cfs_rq)
4519{
4520 struct cfs_bandwidth *cfs_b = tg_cfs_bandwidth(cfs_rq->tg);
4521 s64 slack_runtime = cfs_rq->runtime_remaining - min_cfs_rq_runtime;
4522
4523 if (slack_runtime <= 0)
4524 return;
4525
4526 raw_spin_lock(&cfs_b->lock);
4527 if (cfs_b->quota != RUNTIME_INF &&
4528 cfs_rq->runtime_expires == cfs_b->runtime_expires) {
4529 cfs_b->runtime += slack_runtime;
4530
4531 /* we are under rq->lock, defer unthrottling using a timer */
4532 if (cfs_b->runtime > sched_cfs_bandwidth_slice() &&
4533 !list_empty(&cfs_b->throttled_cfs_rq))
4534 start_cfs_slack_bandwidth(cfs_b);
4535 }
4536 raw_spin_unlock(&cfs_b->lock);
4537
4538 /* even if it's not valid for return we don't want to try again */
4539 cfs_rq->runtime_remaining -= slack_runtime;
4540}
4541
4542static __always_inline void return_cfs_rq_runtime(struct cfs_rq *cfs_rq)
4543{
Paul Turner56f570e2011-11-07 20:26:33 -08004544 if (!cfs_bandwidth_used())
4545 return;
4546
Paul Turnerfccfdc62011-11-07 20:26:34 -08004547 if (!cfs_rq->runtime_enabled || cfs_rq->nr_running)
Paul Turnerd8b49862011-07-21 09:43:41 -07004548 return;
4549
4550 __return_cfs_rq_runtime(cfs_rq);
4551}
4552
4553/*
4554 * This is done with a timer (instead of inline with bandwidth return) since
4555 * it's necessary to juggle rq->locks to unthrottle their respective cfs_rqs.
4556 */
4557static void do_sched_cfs_slack_timer(struct cfs_bandwidth *cfs_b)
4558{
4559 u64 runtime = 0, slice = sched_cfs_bandwidth_slice();
4560 u64 expires;
4561
4562 /* confirm we're still not at a refresh boundary */
Paul Turnerd8b49862011-07-21 09:43:41 -07004563 raw_spin_lock(&cfs_b->lock);
Ben Segalldb06e782013-10-16 11:16:17 -07004564 if (runtime_refresh_within(cfs_b, min_bandwidth_expiration)) {
4565 raw_spin_unlock(&cfs_b->lock);
4566 return;
4567 }
4568
Ben Segallc06f04c2014-06-20 15:21:20 -07004569 if (cfs_b->quota != RUNTIME_INF && cfs_b->runtime > slice)
Paul Turnerd8b49862011-07-21 09:43:41 -07004570 runtime = cfs_b->runtime;
Ben Segallc06f04c2014-06-20 15:21:20 -07004571
Paul Turnerd8b49862011-07-21 09:43:41 -07004572 expires = cfs_b->runtime_expires;
4573 raw_spin_unlock(&cfs_b->lock);
4574
4575 if (!runtime)
4576 return;
4577
4578 runtime = distribute_cfs_runtime(cfs_b, runtime, expires);
4579
4580 raw_spin_lock(&cfs_b->lock);
4581 if (expires == cfs_b->runtime_expires)
Ben Segallc06f04c2014-06-20 15:21:20 -07004582 cfs_b->runtime -= min(runtime, cfs_b->runtime);
Paul Turnerd8b49862011-07-21 09:43:41 -07004583 raw_spin_unlock(&cfs_b->lock);
4584}
4585
Paul Turnerd3d9dc32011-07-21 09:43:39 -07004586/*
4587 * When a group wakes up we want to make sure that its quota is not already
4588 * expired/exceeded, otherwise it may be allowed to steal additional ticks of
4589 * runtime as update_curr() throttling can not not trigger until it's on-rq.
4590 */
4591static void check_enqueue_throttle(struct cfs_rq *cfs_rq)
4592{
Paul Turner56f570e2011-11-07 20:26:33 -08004593 if (!cfs_bandwidth_used())
4594 return;
4595
Paul Turnerd3d9dc32011-07-21 09:43:39 -07004596 /* an active group must be handled by the update_curr()->put() path */
4597 if (!cfs_rq->runtime_enabled || cfs_rq->curr)
4598 return;
4599
4600 /* ensure the group is not already throttled */
4601 if (cfs_rq_throttled(cfs_rq))
4602 return;
4603
4604 /* update runtime allocation */
4605 account_cfs_rq_runtime(cfs_rq, 0);
4606 if (cfs_rq->runtime_remaining <= 0)
4607 throttle_cfs_rq(cfs_rq);
4608}
4609
Peter Zijlstra55e16d32016-06-22 15:14:26 +02004610static void sync_throttle(struct task_group *tg, int cpu)
4611{
4612 struct cfs_rq *pcfs_rq, *cfs_rq;
4613
4614 if (!cfs_bandwidth_used())
4615 return;
4616
4617 if (!tg->parent)
4618 return;
4619
4620 cfs_rq = tg->cfs_rq[cpu];
4621 pcfs_rq = tg->parent->cfs_rq[cpu];
4622
4623 cfs_rq->throttle_count = pcfs_rq->throttle_count;
Xunlei Pangb8922122016-07-09 15:54:22 +08004624 cfs_rq->throttled_clock_task = rq_clock_task(cpu_rq(cpu));
Peter Zijlstra55e16d32016-06-22 15:14:26 +02004625}
4626
Paul Turnerd3d9dc32011-07-21 09:43:39 -07004627/* conditionally throttle active cfs_rq's from put_prev_entity() */
Peter Zijlstra678d5712012-02-11 06:05:00 +01004628static bool check_cfs_rq_runtime(struct cfs_rq *cfs_rq)
Paul Turnerd3d9dc32011-07-21 09:43:39 -07004629{
Paul Turner56f570e2011-11-07 20:26:33 -08004630 if (!cfs_bandwidth_used())
Peter Zijlstra678d5712012-02-11 06:05:00 +01004631 return false;
Paul Turner56f570e2011-11-07 20:26:33 -08004632
Paul Turnerd3d9dc32011-07-21 09:43:39 -07004633 if (likely(!cfs_rq->runtime_enabled || cfs_rq->runtime_remaining > 0))
Peter Zijlstra678d5712012-02-11 06:05:00 +01004634 return false;
Paul Turnerd3d9dc32011-07-21 09:43:39 -07004635
4636 /*
4637 * it's possible for a throttled entity to be forced into a running
4638 * state (e.g. set_curr_task), in this case we're finished.
4639 */
4640 if (cfs_rq_throttled(cfs_rq))
Peter Zijlstra678d5712012-02-11 06:05:00 +01004641 return true;
Paul Turnerd3d9dc32011-07-21 09:43:39 -07004642
4643 throttle_cfs_rq(cfs_rq);
Peter Zijlstra678d5712012-02-11 06:05:00 +01004644 return true;
Paul Turnerd3d9dc32011-07-21 09:43:39 -07004645}
Peter Zijlstra029632f2011-10-25 10:00:11 +02004646
Peter Zijlstra029632f2011-10-25 10:00:11 +02004647static enum hrtimer_restart sched_cfs_slack_timer(struct hrtimer *timer)
4648{
4649 struct cfs_bandwidth *cfs_b =
4650 container_of(timer, struct cfs_bandwidth, slack_timer);
Peter Zijlstra77a4d1a2015-04-15 11:41:57 +02004651
Peter Zijlstra029632f2011-10-25 10:00:11 +02004652 do_sched_cfs_slack_timer(cfs_b);
4653
4654 return HRTIMER_NORESTART;
4655}
4656
4657static enum hrtimer_restart sched_cfs_period_timer(struct hrtimer *timer)
4658{
4659 struct cfs_bandwidth *cfs_b =
4660 container_of(timer, struct cfs_bandwidth, period_timer);
Peter Zijlstra029632f2011-10-25 10:00:11 +02004661 int overrun;
4662 int idle = 0;
4663
Ben Segall51f21762014-05-19 15:49:45 -07004664 raw_spin_lock(&cfs_b->lock);
Peter Zijlstra029632f2011-10-25 10:00:11 +02004665 for (;;) {
Peter Zijlstra77a4d1a2015-04-15 11:41:57 +02004666 overrun = hrtimer_forward_now(timer, cfs_b->period);
Peter Zijlstra029632f2011-10-25 10:00:11 +02004667 if (!overrun)
4668 break;
4669
4670 idle = do_sched_cfs_period_timer(cfs_b, overrun);
4671 }
Peter Zijlstra4cfafd32015-05-14 12:23:11 +02004672 if (idle)
4673 cfs_b->period_active = 0;
Ben Segall51f21762014-05-19 15:49:45 -07004674 raw_spin_unlock(&cfs_b->lock);
Peter Zijlstra029632f2011-10-25 10:00:11 +02004675
4676 return idle ? HRTIMER_NORESTART : HRTIMER_RESTART;
4677}
4678
4679void init_cfs_bandwidth(struct cfs_bandwidth *cfs_b)
4680{
4681 raw_spin_lock_init(&cfs_b->lock);
4682 cfs_b->runtime = 0;
4683 cfs_b->quota = RUNTIME_INF;
4684 cfs_b->period = ns_to_ktime(default_cfs_period());
4685
4686 INIT_LIST_HEAD(&cfs_b->throttled_cfs_rq);
Peter Zijlstra4cfafd32015-05-14 12:23:11 +02004687 hrtimer_init(&cfs_b->period_timer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS_PINNED);
Peter Zijlstra029632f2011-10-25 10:00:11 +02004688 cfs_b->period_timer.function = sched_cfs_period_timer;
4689 hrtimer_init(&cfs_b->slack_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
4690 cfs_b->slack_timer.function = sched_cfs_slack_timer;
4691}
4692
4693static void init_cfs_rq_runtime(struct cfs_rq *cfs_rq)
4694{
4695 cfs_rq->runtime_enabled = 0;
4696 INIT_LIST_HEAD(&cfs_rq->throttled_list);
4697}
4698
Peter Zijlstra77a4d1a2015-04-15 11:41:57 +02004699void start_cfs_bandwidth(struct cfs_bandwidth *cfs_b)
Peter Zijlstra029632f2011-10-25 10:00:11 +02004700{
Peter Zijlstra4cfafd32015-05-14 12:23:11 +02004701 lockdep_assert_held(&cfs_b->lock);
Peter Zijlstra029632f2011-10-25 10:00:11 +02004702
Peter Zijlstra4cfafd32015-05-14 12:23:11 +02004703 if (!cfs_b->period_active) {
4704 cfs_b->period_active = 1;
4705 hrtimer_forward_now(&cfs_b->period_timer, cfs_b->period);
4706 hrtimer_start_expires(&cfs_b->period_timer, HRTIMER_MODE_ABS_PINNED);
4707 }
Peter Zijlstra029632f2011-10-25 10:00:11 +02004708}
4709
4710static void destroy_cfs_bandwidth(struct cfs_bandwidth *cfs_b)
4711{
Tetsuo Handa7f1a1692014-12-25 15:51:21 +09004712 /* init_cfs_bandwidth() was not called */
4713 if (!cfs_b->throttled_cfs_rq.next)
4714 return;
4715
Peter Zijlstra029632f2011-10-25 10:00:11 +02004716 hrtimer_cancel(&cfs_b->period_timer);
4717 hrtimer_cancel(&cfs_b->slack_timer);
4718}
4719
Peter Zijlstra502ce002017-05-04 15:31:22 +02004720/*
4721 * Both these cpu hotplug callbacks race against unregister_fair_sched_group()
4722 *
4723 * The race is harmless, since modifying bandwidth settings of unhooked group
4724 * bits doesn't do much.
4725 */
4726
4727/* cpu online calback */
Kirill Tkhai0e59bda2014-06-25 12:19:42 +04004728static void __maybe_unused update_runtime_enabled(struct rq *rq)
4729{
Peter Zijlstra502ce002017-05-04 15:31:22 +02004730 struct task_group *tg;
Kirill Tkhai0e59bda2014-06-25 12:19:42 +04004731
Peter Zijlstra502ce002017-05-04 15:31:22 +02004732 lockdep_assert_held(&rq->lock);
4733
4734 rcu_read_lock();
4735 list_for_each_entry_rcu(tg, &task_groups, list) {
4736 struct cfs_bandwidth *cfs_b = &tg->cfs_bandwidth;
4737 struct cfs_rq *cfs_rq = tg->cfs_rq[cpu_of(rq)];
Kirill Tkhai0e59bda2014-06-25 12:19:42 +04004738
4739 raw_spin_lock(&cfs_b->lock);
4740 cfs_rq->runtime_enabled = cfs_b->quota != RUNTIME_INF;
4741 raw_spin_unlock(&cfs_b->lock);
4742 }
Peter Zijlstra502ce002017-05-04 15:31:22 +02004743 rcu_read_unlock();
Kirill Tkhai0e59bda2014-06-25 12:19:42 +04004744}
4745
Peter Zijlstra502ce002017-05-04 15:31:22 +02004746/* cpu offline callback */
Arnd Bergmann38dc3342013-01-25 14:14:22 +00004747static void __maybe_unused unthrottle_offline_cfs_rqs(struct rq *rq)
Peter Zijlstra029632f2011-10-25 10:00:11 +02004748{
Peter Zijlstra502ce002017-05-04 15:31:22 +02004749 struct task_group *tg;
Peter Zijlstra029632f2011-10-25 10:00:11 +02004750
Peter Zijlstra502ce002017-05-04 15:31:22 +02004751 lockdep_assert_held(&rq->lock);
4752
4753 rcu_read_lock();
4754 list_for_each_entry_rcu(tg, &task_groups, list) {
4755 struct cfs_rq *cfs_rq = tg->cfs_rq[cpu_of(rq)];
4756
Peter Zijlstra029632f2011-10-25 10:00:11 +02004757 if (!cfs_rq->runtime_enabled)
4758 continue;
4759
4760 /*
4761 * clock_task is not advancing so we just need to make sure
4762 * there's some valid quota amount
4763 */
Ben Segall51f21762014-05-19 15:49:45 -07004764 cfs_rq->runtime_remaining = 1;
Kirill Tkhai0e59bda2014-06-25 12:19:42 +04004765 /*
4766 * Offline rq is schedulable till cpu is completely disabled
4767 * in take_cpu_down(), so we prevent new cfs throttling here.
4768 */
4769 cfs_rq->runtime_enabled = 0;
4770
Peter Zijlstra029632f2011-10-25 10:00:11 +02004771 if (cfs_rq_throttled(cfs_rq))
4772 unthrottle_cfs_rq(cfs_rq);
4773 }
Peter Zijlstra502ce002017-05-04 15:31:22 +02004774 rcu_read_unlock();
Peter Zijlstra029632f2011-10-25 10:00:11 +02004775}
4776
4777#else /* CONFIG_CFS_BANDWIDTH */
Paul Turnerf1b17282012-10-04 13:18:31 +02004778static inline u64 cfs_rq_clock_task(struct cfs_rq *cfs_rq)
4779{
Frederic Weisbecker78becc22013-04-12 01:51:02 +02004780 return rq_clock_task(rq_of(cfs_rq));
Paul Turnerf1b17282012-10-04 13:18:31 +02004781}
4782
Peter Zijlstra9dbdb152013-11-18 18:27:06 +01004783static void account_cfs_rq_runtime(struct cfs_rq *cfs_rq, u64 delta_exec) {}
Peter Zijlstra678d5712012-02-11 06:05:00 +01004784static bool check_cfs_rq_runtime(struct cfs_rq *cfs_rq) { return false; }
Paul Turnerd3d9dc32011-07-21 09:43:39 -07004785static void check_enqueue_throttle(struct cfs_rq *cfs_rq) {}
Peter Zijlstra55e16d32016-06-22 15:14:26 +02004786static inline void sync_throttle(struct task_group *tg, int cpu) {}
Peter Zijlstra6c16a6d2012-03-21 13:07:16 -07004787static __always_inline void return_cfs_rq_runtime(struct cfs_rq *cfs_rq) {}
Paul Turner85dac902011-07-21 09:43:33 -07004788
4789static inline int cfs_rq_throttled(struct cfs_rq *cfs_rq)
4790{
4791 return 0;
4792}
Paul Turner64660c82011-07-21 09:43:36 -07004793
4794static inline int throttled_hierarchy(struct cfs_rq *cfs_rq)
4795{
4796 return 0;
4797}
4798
4799static inline int throttled_lb_pair(struct task_group *tg,
4800 int src_cpu, int dest_cpu)
4801{
4802 return 0;
4803}
Peter Zijlstra029632f2011-10-25 10:00:11 +02004804
4805void init_cfs_bandwidth(struct cfs_bandwidth *cfs_b) {}
4806
4807#ifdef CONFIG_FAIR_GROUP_SCHED
4808static void init_cfs_rq_runtime(struct cfs_rq *cfs_rq) {}
Paul Turnerab84d312011-07-21 09:43:28 -07004809#endif
4810
Peter Zijlstra029632f2011-10-25 10:00:11 +02004811static inline struct cfs_bandwidth *tg_cfs_bandwidth(struct task_group *tg)
4812{
4813 return NULL;
4814}
4815static inline void destroy_cfs_bandwidth(struct cfs_bandwidth *cfs_b) {}
Kirill Tkhai0e59bda2014-06-25 12:19:42 +04004816static inline void update_runtime_enabled(struct rq *rq) {}
Peter Boonstoppela4c96ae2012-08-09 15:34:47 -07004817static inline void unthrottle_offline_cfs_rqs(struct rq *rq) {}
Peter Zijlstra029632f2011-10-25 10:00:11 +02004818
4819#endif /* CONFIG_CFS_BANDWIDTH */
4820
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02004821/**************************************************
4822 * CFS operations on tasks:
4823 */
4824
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01004825#ifdef CONFIG_SCHED_HRTICK
4826static void hrtick_start_fair(struct rq *rq, struct task_struct *p)
4827{
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01004828 struct sched_entity *se = &p->se;
4829 struct cfs_rq *cfs_rq = cfs_rq_of(se);
4830
Peter Zijlstra9148a3a2016-09-20 22:34:51 +02004831 SCHED_WARN_ON(task_rq(p) != rq);
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01004832
Srivatsa Vaddagiri8bf46a32016-09-16 18:28:51 -07004833 if (rq->cfs.h_nr_running > 1) {
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01004834 u64 slice = sched_slice(cfs_rq, se);
4835 u64 ran = se->sum_exec_runtime - se->prev_sum_exec_runtime;
4836 s64 delta = slice - ran;
4837
4838 if (delta < 0) {
4839 if (rq->curr == p)
Kirill Tkhai88751252014-06-29 00:03:57 +04004840 resched_curr(rq);
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01004841 return;
4842 }
Peter Zijlstra31656512008-07-18 18:01:23 +02004843 hrtick_start(rq, delta);
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01004844 }
4845}
Peter Zijlstraa4c2f002008-10-17 19:27:03 +02004846
4847/*
4848 * called from enqueue/dequeue and updates the hrtick when the
4849 * current task is from our class and nr_running is low enough
4850 * to matter.
4851 */
4852static void hrtick_update(struct rq *rq)
4853{
4854 struct task_struct *curr = rq->curr;
4855
Mike Galbraithb39e66e2011-11-22 15:20:07 +01004856 if (!hrtick_enabled(rq) || curr->sched_class != &fair_sched_class)
Peter Zijlstraa4c2f002008-10-17 19:27:03 +02004857 return;
4858
4859 if (cfs_rq_of(&curr->se)->nr_running < sched_nr_latency)
4860 hrtick_start_fair(rq, curr);
4861}
Dhaval Giani55e12e52008-06-24 23:39:43 +05304862#else /* !CONFIG_SCHED_HRTICK */
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01004863static inline void
4864hrtick_start_fair(struct rq *rq, struct task_struct *p)
4865{
4866}
Peter Zijlstraa4c2f002008-10-17 19:27:03 +02004867
4868static inline void hrtick_update(struct rq *rq)
4869{
4870}
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01004871#endif
4872
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02004873/*
4874 * The enqueue_task method is called before nr_running is
4875 * increased. Here we update the fair scheduling stats and
4876 * then put the task into the rbtree:
4877 */
Thomas Gleixnerea87bb72010-01-20 20:58:57 +00004878static void
Peter Zijlstra371fd7e2010-03-24 16:38:48 +01004879enqueue_task_fair(struct rq *rq, struct task_struct *p, int flags)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02004880{
4881 struct cfs_rq *cfs_rq;
Peter Zijlstra62fb1852008-02-25 17:34:02 +01004882 struct sched_entity *se = &p->se;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02004883
Rafael J. Wysocki8c34ab12016-09-09 23:59:33 +02004884 /*
4885 * If in_iowait is set, the code below may not trigger any cpufreq
4886 * utilization updates, so do it here explicitly with the IOWAIT flag
4887 * passed.
4888 */
4889 if (p->in_iowait)
Viresh Kumar674e7542017-07-28 12:16:38 +05304890 cpufreq_update_util(rq, SCHED_CPUFREQ_IOWAIT);
Rafael J. Wysocki8c34ab12016-09-09 23:59:33 +02004891
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02004892 for_each_sched_entity(se) {
Peter Zijlstra62fb1852008-02-25 17:34:02 +01004893 if (se->on_rq)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02004894 break;
4895 cfs_rq = cfs_rq_of(se);
Peter Zijlstra88ec22d2009-12-16 18:04:41 +01004896 enqueue_entity(cfs_rq, se, flags);
Paul Turner85dac902011-07-21 09:43:33 -07004897
4898 /*
4899 * end evaluation on encountering a throttled cfs_rq
4900 *
4901 * note: in the case of encountering a throttled cfs_rq we will
4902 * post the final h_nr_running increment below.
Peter Zijlstrae210bff2016-06-16 18:51:48 +02004903 */
Paul Turner85dac902011-07-21 09:43:33 -07004904 if (cfs_rq_throttled(cfs_rq))
4905 break;
Paul Turner953bfcd2011-07-21 09:43:27 -07004906 cfs_rq->h_nr_running++;
Paul Turner85dac902011-07-21 09:43:33 -07004907
Peter Zijlstra88ec22d2009-12-16 18:04:41 +01004908 flags = ENQUEUE_WAKEUP;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02004909 }
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01004910
Peter Zijlstra2069dd72010-11-15 15:47:00 -08004911 for_each_sched_entity(se) {
Lin Ming0f317142011-07-22 09:14:31 +08004912 cfs_rq = cfs_rq_of(se);
Paul Turner953bfcd2011-07-21 09:43:27 -07004913 cfs_rq->h_nr_running++;
Peter Zijlstra2069dd72010-11-15 15:47:00 -08004914
Paul Turner85dac902011-07-21 09:43:33 -07004915 if (cfs_rq_throttled(cfs_rq))
4916 break;
4917
Vincent Guittotd31b1a62016-11-08 10:53:44 +01004918 update_load_avg(se, UPDATE_TG);
Vincent Guittot89ee0482016-12-21 16:50:26 +01004919 update_cfs_shares(se);
Peter Zijlstra2069dd72010-11-15 15:47:00 -08004920 }
4921
Yuyang Ducd126af2015-07-15 08:04:36 +08004922 if (!se)
Kirill Tkhai72465442014-05-09 03:00:14 +04004923 add_nr_running(rq, 1);
Yuyang Ducd126af2015-07-15 08:04:36 +08004924
Peter Zijlstraa4c2f002008-10-17 19:27:03 +02004925 hrtick_update(rq);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02004926}
4927
Venkatesh Pallipadi2f368252011-04-14 10:30:53 -07004928static void set_next_buddy(struct sched_entity *se);
4929
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02004930/*
4931 * The dequeue_task method is called before nr_running is
4932 * decreased. We remove the task from the rbtree and
4933 * update the fair scheduling stats:
4934 */
Peter Zijlstra371fd7e2010-03-24 16:38:48 +01004935static void dequeue_task_fair(struct rq *rq, struct task_struct *p, int flags)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02004936{
4937 struct cfs_rq *cfs_rq;
Peter Zijlstra62fb1852008-02-25 17:34:02 +01004938 struct sched_entity *se = &p->se;
Venkatesh Pallipadi2f368252011-04-14 10:30:53 -07004939 int task_sleep = flags & DEQUEUE_SLEEP;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02004940
4941 for_each_sched_entity(se) {
4942 cfs_rq = cfs_rq_of(se);
Peter Zijlstra371fd7e2010-03-24 16:38:48 +01004943 dequeue_entity(cfs_rq, se, flags);
Paul Turner85dac902011-07-21 09:43:33 -07004944
4945 /*
4946 * end evaluation on encountering a throttled cfs_rq
4947 *
4948 * note: in the case of encountering a throttled cfs_rq we will
4949 * post the final h_nr_running decrement below.
4950 */
4951 if (cfs_rq_throttled(cfs_rq))
4952 break;
Paul Turner953bfcd2011-07-21 09:43:27 -07004953 cfs_rq->h_nr_running--;
Peter Zijlstra2069dd72010-11-15 15:47:00 -08004954
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02004955 /* Don't dequeue parent if it has other entities besides us */
Venkatesh Pallipadi2f368252011-04-14 10:30:53 -07004956 if (cfs_rq->load.weight) {
Konstantin Khlebnikov754bd592016-06-16 15:57:15 +03004957 /* Avoid re-evaluating load for this entity: */
4958 se = parent_entity(se);
Venkatesh Pallipadi2f368252011-04-14 10:30:53 -07004959 /*
4960 * Bias pick_next to pick a task from this cfs_rq, as
4961 * p is sleeping when it is within its sched_slice.
4962 */
Konstantin Khlebnikov754bd592016-06-16 15:57:15 +03004963 if (task_sleep && se && !throttled_hierarchy(cfs_rq))
4964 set_next_buddy(se);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02004965 break;
Venkatesh Pallipadi2f368252011-04-14 10:30:53 -07004966 }
Peter Zijlstra371fd7e2010-03-24 16:38:48 +01004967 flags |= DEQUEUE_SLEEP;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02004968 }
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01004969
Peter Zijlstra2069dd72010-11-15 15:47:00 -08004970 for_each_sched_entity(se) {
Lin Ming0f317142011-07-22 09:14:31 +08004971 cfs_rq = cfs_rq_of(se);
Paul Turner953bfcd2011-07-21 09:43:27 -07004972 cfs_rq->h_nr_running--;
Peter Zijlstra2069dd72010-11-15 15:47:00 -08004973
Paul Turner85dac902011-07-21 09:43:33 -07004974 if (cfs_rq_throttled(cfs_rq))
4975 break;
4976
Vincent Guittotd31b1a62016-11-08 10:53:44 +01004977 update_load_avg(se, UPDATE_TG);
Vincent Guittot89ee0482016-12-21 16:50:26 +01004978 update_cfs_shares(se);
Peter Zijlstra2069dd72010-11-15 15:47:00 -08004979 }
4980
Yuyang Ducd126af2015-07-15 08:04:36 +08004981 if (!se)
Kirill Tkhai72465442014-05-09 03:00:14 +04004982 sub_nr_running(rq, 1);
Yuyang Ducd126af2015-07-15 08:04:36 +08004983
Peter Zijlstraa4c2f002008-10-17 19:27:03 +02004984 hrtick_update(rq);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02004985}
4986
Gregory Haskinse7693a32008-01-25 21:08:09 +01004987#ifdef CONFIG_SMP
Peter Zijlstra10e2f1a2016-05-09 10:38:05 +02004988
4989/* Working cpumask for: load_balance, load_balance_newidle. */
4990DEFINE_PER_CPU(cpumask_var_t, load_balance_mask);
4991DEFINE_PER_CPU(cpumask_var_t, select_idle_mask);
4992
Frederic Weisbecker9fd81dd2016-04-19 17:36:51 +02004993#ifdef CONFIG_NO_HZ_COMMON
Peter Zijlstra3289bdb2015-04-14 13:19:42 +02004994/*
4995 * per rq 'load' arrray crap; XXX kill this.
4996 */
4997
4998/*
Peter Zijlstrad937cdc2015-10-19 13:49:30 +02004999 * The exact cpuload calculated at every tick would be:
Peter Zijlstra3289bdb2015-04-14 13:19:42 +02005000 *
Peter Zijlstrad937cdc2015-10-19 13:49:30 +02005001 * load' = (1 - 1/2^i) * load + (1/2^i) * cur_load
5002 *
5003 * If a cpu misses updates for n ticks (as it was idle) and update gets
5004 * called on the n+1-th tick when cpu may be busy, then we have:
5005 *
5006 * load_n = (1 - 1/2^i)^n * load_0
5007 * load_n+1 = (1 - 1/2^i) * load_n + (1/2^i) * cur_load
Peter Zijlstra3289bdb2015-04-14 13:19:42 +02005008 *
5009 * decay_load_missed() below does efficient calculation of
Peter Zijlstrad937cdc2015-10-19 13:49:30 +02005010 *
5011 * load' = (1 - 1/2^i)^n * load
5012 *
5013 * Because x^(n+m) := x^n * x^m we can decompose any x^n in power-of-2 factors.
5014 * This allows us to precompute the above in said factors, thereby allowing the
5015 * reduction of an arbitrary n in O(log_2 n) steps. (See also
5016 * fixed_power_int())
Peter Zijlstra3289bdb2015-04-14 13:19:42 +02005017 *
5018 * The calculation is approximated on a 128 point scale.
Peter Zijlstra3289bdb2015-04-14 13:19:42 +02005019 */
5020#define DEGRADE_SHIFT 7
Peter Zijlstrad937cdc2015-10-19 13:49:30 +02005021
5022static const u8 degrade_zero_ticks[CPU_LOAD_IDX_MAX] = {0, 8, 32, 64, 128};
5023static const u8 degrade_factor[CPU_LOAD_IDX_MAX][DEGRADE_SHIFT + 1] = {
5024 { 0, 0, 0, 0, 0, 0, 0, 0 },
5025 { 64, 32, 8, 0, 0, 0, 0, 0 },
5026 { 96, 72, 40, 12, 1, 0, 0, 0 },
5027 { 112, 98, 75, 43, 15, 1, 0, 0 },
5028 { 120, 112, 98, 76, 45, 16, 2, 0 }
5029};
Peter Zijlstra3289bdb2015-04-14 13:19:42 +02005030
5031/*
5032 * Update cpu_load for any missed ticks, due to tickless idle. The backlog
5033 * would be when CPU is idle and so we just decay the old load without
5034 * adding any new load.
5035 */
5036static unsigned long
5037decay_load_missed(unsigned long load, unsigned long missed_updates, int idx)
5038{
5039 int j = 0;
5040
5041 if (!missed_updates)
5042 return load;
5043
5044 if (missed_updates >= degrade_zero_ticks[idx])
5045 return 0;
5046
5047 if (idx == 1)
5048 return load >> missed_updates;
5049
5050 while (missed_updates) {
5051 if (missed_updates % 2)
5052 load = (load * degrade_factor[idx][j]) >> DEGRADE_SHIFT;
5053
5054 missed_updates >>= 1;
5055 j++;
5056 }
5057 return load;
5058}
Frederic Weisbecker9fd81dd2016-04-19 17:36:51 +02005059#endif /* CONFIG_NO_HZ_COMMON */
Peter Zijlstra3289bdb2015-04-14 13:19:42 +02005060
Byungchul Park59543272015-10-14 18:47:35 +09005061/**
Frederic Weisbeckercee1afc2016-04-13 15:56:50 +02005062 * __cpu_load_update - update the rq->cpu_load[] statistics
Byungchul Park59543272015-10-14 18:47:35 +09005063 * @this_rq: The rq to update statistics for
5064 * @this_load: The current load
5065 * @pending_updates: The number of missed updates
Byungchul Park59543272015-10-14 18:47:35 +09005066 *
Peter Zijlstra3289bdb2015-04-14 13:19:42 +02005067 * Update rq->cpu_load[] statistics. This function is usually called every
Byungchul Park59543272015-10-14 18:47:35 +09005068 * scheduler tick (TICK_NSEC).
5069 *
5070 * This function computes a decaying average:
5071 *
5072 * load[i]' = (1 - 1/2^i) * load[i] + (1/2^i) * load
5073 *
5074 * Because of NOHZ it might not get called on every tick which gives need for
5075 * the @pending_updates argument.
5076 *
5077 * load[i]_n = (1 - 1/2^i) * load[i]_n-1 + (1/2^i) * load_n-1
5078 * = A * load[i]_n-1 + B ; A := (1 - 1/2^i), B := (1/2^i) * load
5079 * = A * (A * load[i]_n-2 + B) + B
5080 * = A * (A * (A * load[i]_n-3 + B) + B) + B
5081 * = A^3 * load[i]_n-3 + (A^2 + A + 1) * B
5082 * = A^n * load[i]_0 + (A^(n-1) + A^(n-2) + ... + 1) * B
5083 * = A^n * load[i]_0 + ((1 - A^n) / (1 - A)) * B
5084 * = (1 - 1/2^i)^n * (load[i]_0 - load) + load
5085 *
5086 * In the above we've assumed load_n := load, which is true for NOHZ_FULL as
5087 * any change in load would have resulted in the tick being turned back on.
5088 *
5089 * For regular NOHZ, this reduces to:
5090 *
5091 * load[i]_n = (1 - 1/2^i)^n * load[i]_0
5092 *
5093 * see decay_load_misses(). For NOHZ_FULL we get to subtract and add the extra
Frederic Weisbecker1f419062016-04-13 15:56:51 +02005094 * term.
Peter Zijlstra3289bdb2015-04-14 13:19:42 +02005095 */
Frederic Weisbecker1f419062016-04-13 15:56:51 +02005096static void cpu_load_update(struct rq *this_rq, unsigned long this_load,
5097 unsigned long pending_updates)
Peter Zijlstra3289bdb2015-04-14 13:19:42 +02005098{
Frederic Weisbecker9fd81dd2016-04-19 17:36:51 +02005099 unsigned long __maybe_unused tickless_load = this_rq->cpu_load[0];
Peter Zijlstra3289bdb2015-04-14 13:19:42 +02005100 int i, scale;
5101
5102 this_rq->nr_load_updates++;
5103
5104 /* Update our load: */
5105 this_rq->cpu_load[0] = this_load; /* Fasttrack for idx 0 */
5106 for (i = 1, scale = 2; i < CPU_LOAD_IDX_MAX; i++, scale += scale) {
5107 unsigned long old_load, new_load;
5108
5109 /* scale is effectively 1 << i now, and >> i divides by scale */
5110
Byungchul Park7400d3b2016-01-15 16:07:49 +09005111 old_load = this_rq->cpu_load[i];
Frederic Weisbecker9fd81dd2016-04-19 17:36:51 +02005112#ifdef CONFIG_NO_HZ_COMMON
Peter Zijlstra3289bdb2015-04-14 13:19:42 +02005113 old_load = decay_load_missed(old_load, pending_updates - 1, i);
Byungchul Park7400d3b2016-01-15 16:07:49 +09005114 if (tickless_load) {
5115 old_load -= decay_load_missed(tickless_load, pending_updates - 1, i);
5116 /*
5117 * old_load can never be a negative value because a
5118 * decayed tickless_load cannot be greater than the
5119 * original tickless_load.
5120 */
5121 old_load += tickless_load;
5122 }
Frederic Weisbecker9fd81dd2016-04-19 17:36:51 +02005123#endif
Peter Zijlstra3289bdb2015-04-14 13:19:42 +02005124 new_load = this_load;
5125 /*
5126 * Round up the averaging division if load is increasing. This
5127 * prevents us from getting stuck on 9 if the load is 10, for
5128 * example.
5129 */
5130 if (new_load > old_load)
5131 new_load += scale - 1;
5132
5133 this_rq->cpu_load[i] = (old_load * (scale - 1) + new_load) >> i;
5134 }
5135
5136 sched_avg_update(this_rq);
5137}
5138
Yuyang Du7ea241a2015-07-15 08:04:42 +08005139/* Used instead of source_load when we know the type == 0 */
Viresh Kumarc7132dd2017-05-24 10:59:54 +05305140static unsigned long weighted_cpuload(struct rq *rq)
Yuyang Du7ea241a2015-07-15 08:04:42 +08005141{
Viresh Kumarc7132dd2017-05-24 10:59:54 +05305142 return cfs_rq_runnable_load_avg(&rq->cfs);
Yuyang Du7ea241a2015-07-15 08:04:42 +08005143}
5144
Peter Zijlstra3289bdb2015-04-14 13:19:42 +02005145#ifdef CONFIG_NO_HZ_COMMON
Frederic Weisbecker1f419062016-04-13 15:56:51 +02005146/*
5147 * There is no sane way to deal with nohz on smp when using jiffies because the
5148 * cpu doing the jiffies update might drift wrt the cpu doing the jiffy reading
5149 * causing off-by-one errors in observed deltas; {0,2} instead of {1,1}.
5150 *
5151 * Therefore we need to avoid the delta approach from the regular tick when
5152 * possible since that would seriously skew the load calculation. This is why we
5153 * use cpu_load_update_periodic() for CPUs out of nohz. However we'll rely on
5154 * jiffies deltas for updates happening while in nohz mode (idle ticks, idle
5155 * loop exit, nohz_idle_balance, nohz full exit...)
5156 *
5157 * This means we might still be one tick off for nohz periods.
5158 */
5159
5160static void cpu_load_update_nohz(struct rq *this_rq,
5161 unsigned long curr_jiffies,
5162 unsigned long load)
Frederic Weisbeckerbe68a682016-01-13 17:01:29 +01005163{
5164 unsigned long pending_updates;
5165
5166 pending_updates = curr_jiffies - this_rq->last_load_update_tick;
5167 if (pending_updates) {
5168 this_rq->last_load_update_tick = curr_jiffies;
5169 /*
5170 * In the regular NOHZ case, we were idle, this means load 0.
5171 * In the NOHZ_FULL case, we were non-idle, we should consider
5172 * its weighted load.
5173 */
Frederic Weisbecker1f419062016-04-13 15:56:51 +02005174 cpu_load_update(this_rq, load, pending_updates);
Frederic Weisbeckerbe68a682016-01-13 17:01:29 +01005175 }
5176}
5177
Peter Zijlstra3289bdb2015-04-14 13:19:42 +02005178/*
Peter Zijlstra3289bdb2015-04-14 13:19:42 +02005179 * Called from nohz_idle_balance() to update the load ratings before doing the
5180 * idle balance.
5181 */
Frederic Weisbeckercee1afc2016-04-13 15:56:50 +02005182static void cpu_load_update_idle(struct rq *this_rq)
Peter Zijlstra3289bdb2015-04-14 13:19:42 +02005183{
Peter Zijlstra3289bdb2015-04-14 13:19:42 +02005184 /*
5185 * bail if there's load or we're actually up-to-date.
5186 */
Viresh Kumarc7132dd2017-05-24 10:59:54 +05305187 if (weighted_cpuload(this_rq))
Peter Zijlstra3289bdb2015-04-14 13:19:42 +02005188 return;
5189
Frederic Weisbecker1f419062016-04-13 15:56:51 +02005190 cpu_load_update_nohz(this_rq, READ_ONCE(jiffies), 0);
Peter Zijlstra3289bdb2015-04-14 13:19:42 +02005191}
5192
5193/*
Frederic Weisbecker1f419062016-04-13 15:56:51 +02005194 * Record CPU load on nohz entry so we know the tickless load to account
5195 * on nohz exit. cpu_load[0] happens then to be updated more frequently
5196 * than other cpu_load[idx] but it should be fine as cpu_load readers
5197 * shouldn't rely into synchronized cpu_load[*] updates.
Peter Zijlstra3289bdb2015-04-14 13:19:42 +02005198 */
Frederic Weisbecker1f419062016-04-13 15:56:51 +02005199void cpu_load_update_nohz_start(void)
Peter Zijlstra3289bdb2015-04-14 13:19:42 +02005200{
5201 struct rq *this_rq = this_rq();
Frederic Weisbecker1f419062016-04-13 15:56:51 +02005202
5203 /*
5204 * This is all lockless but should be fine. If weighted_cpuload changes
5205 * concurrently we'll exit nohz. And cpu_load write can race with
5206 * cpu_load_update_idle() but both updater would be writing the same.
5207 */
Viresh Kumarc7132dd2017-05-24 10:59:54 +05305208 this_rq->cpu_load[0] = weighted_cpuload(this_rq);
Frederic Weisbecker1f419062016-04-13 15:56:51 +02005209}
5210
5211/*
5212 * Account the tickless load in the end of a nohz frame.
5213 */
5214void cpu_load_update_nohz_stop(void)
5215{
Jason Low316c1608d2015-04-28 13:00:20 -07005216 unsigned long curr_jiffies = READ_ONCE(jiffies);
Frederic Weisbecker1f419062016-04-13 15:56:51 +02005217 struct rq *this_rq = this_rq();
5218 unsigned long load;
Peter Zijlstra8a8c69c2016-10-04 16:04:35 +02005219 struct rq_flags rf;
Peter Zijlstra3289bdb2015-04-14 13:19:42 +02005220
5221 if (curr_jiffies == this_rq->last_load_update_tick)
5222 return;
5223
Viresh Kumarc7132dd2017-05-24 10:59:54 +05305224 load = weighted_cpuload(this_rq);
Peter Zijlstra8a8c69c2016-10-04 16:04:35 +02005225 rq_lock(this_rq, &rf);
Matt Flemingb52fad22016-05-03 20:46:54 +01005226 update_rq_clock(this_rq);
Frederic Weisbecker1f419062016-04-13 15:56:51 +02005227 cpu_load_update_nohz(this_rq, curr_jiffies, load);
Peter Zijlstra8a8c69c2016-10-04 16:04:35 +02005228 rq_unlock(this_rq, &rf);
Peter Zijlstra3289bdb2015-04-14 13:19:42 +02005229}
Frederic Weisbecker1f419062016-04-13 15:56:51 +02005230#else /* !CONFIG_NO_HZ_COMMON */
5231static inline void cpu_load_update_nohz(struct rq *this_rq,
5232 unsigned long curr_jiffies,
5233 unsigned long load) { }
5234#endif /* CONFIG_NO_HZ_COMMON */
5235
5236static void cpu_load_update_periodic(struct rq *this_rq, unsigned long load)
5237{
Frederic Weisbecker9fd81dd2016-04-19 17:36:51 +02005238#ifdef CONFIG_NO_HZ_COMMON
Frederic Weisbecker1f419062016-04-13 15:56:51 +02005239 /* See the mess around cpu_load_update_nohz(). */
5240 this_rq->last_load_update_tick = READ_ONCE(jiffies);
Frederic Weisbecker9fd81dd2016-04-19 17:36:51 +02005241#endif
Frederic Weisbecker1f419062016-04-13 15:56:51 +02005242 cpu_load_update(this_rq, load, 1);
5243}
Peter Zijlstra3289bdb2015-04-14 13:19:42 +02005244
5245/*
5246 * Called from scheduler_tick()
5247 */
Frederic Weisbeckercee1afc2016-04-13 15:56:50 +02005248void cpu_load_update_active(struct rq *this_rq)
Peter Zijlstra3289bdb2015-04-14 13:19:42 +02005249{
Viresh Kumarc7132dd2017-05-24 10:59:54 +05305250 unsigned long load = weighted_cpuload(this_rq);
Frederic Weisbecker1f419062016-04-13 15:56:51 +02005251
5252 if (tick_nohz_tick_stopped())
5253 cpu_load_update_nohz(this_rq, READ_ONCE(jiffies), load);
5254 else
5255 cpu_load_update_periodic(this_rq, load);
Peter Zijlstra3289bdb2015-04-14 13:19:42 +02005256}
5257
Peter Zijlstra029632f2011-10-25 10:00:11 +02005258/*
5259 * Return a low guess at the load of a migration-source cpu weighted
5260 * according to the scheduling class and "nice" value.
5261 *
5262 * We want to under-estimate the load of migration sources, to
5263 * balance conservatively.
5264 */
5265static unsigned long source_load(int cpu, int type)
5266{
5267 struct rq *rq = cpu_rq(cpu);
Viresh Kumarc7132dd2017-05-24 10:59:54 +05305268 unsigned long total = weighted_cpuload(rq);
Peter Zijlstra029632f2011-10-25 10:00:11 +02005269
5270 if (type == 0 || !sched_feat(LB_BIAS))
5271 return total;
5272
5273 return min(rq->cpu_load[type-1], total);
5274}
5275
5276/*
5277 * Return a high guess at the load of a migration-target cpu weighted
5278 * according to the scheduling class and "nice" value.
5279 */
5280static unsigned long target_load(int cpu, int type)
5281{
5282 struct rq *rq = cpu_rq(cpu);
Viresh Kumarc7132dd2017-05-24 10:59:54 +05305283 unsigned long total = weighted_cpuload(rq);
Peter Zijlstra029632f2011-10-25 10:00:11 +02005284
5285 if (type == 0 || !sched_feat(LB_BIAS))
5286 return total;
5287
5288 return max(rq->cpu_load[type-1], total);
5289}
5290
Nicolas Pitreced549f2014-05-26 18:19:38 -04005291static unsigned long capacity_of(int cpu)
Peter Zijlstra029632f2011-10-25 10:00:11 +02005292{
Nicolas Pitreced549f2014-05-26 18:19:38 -04005293 return cpu_rq(cpu)->cpu_capacity;
Peter Zijlstra029632f2011-10-25 10:00:11 +02005294}
5295
Vincent Guittotca6d75e2015-02-27 16:54:09 +01005296static unsigned long capacity_orig_of(int cpu)
5297{
5298 return cpu_rq(cpu)->cpu_capacity_orig;
5299}
5300
Peter Zijlstra029632f2011-10-25 10:00:11 +02005301static unsigned long cpu_avg_load_per_task(int cpu)
5302{
5303 struct rq *rq = cpu_rq(cpu);
Jason Low316c1608d2015-04-28 13:00:20 -07005304 unsigned long nr_running = READ_ONCE(rq->cfs.h_nr_running);
Viresh Kumarc7132dd2017-05-24 10:59:54 +05305305 unsigned long load_avg = weighted_cpuload(rq);
Peter Zijlstra029632f2011-10-25 10:00:11 +02005306
5307 if (nr_running)
Alex Shib92486c2013-06-20 10:18:50 +08005308 return load_avg / nr_running;
Peter Zijlstra029632f2011-10-25 10:00:11 +02005309
5310 return 0;
5311}
5312
Peter Zijlstrac58d25f2016-05-12 09:19:59 +02005313static void record_wakee(struct task_struct *p)
5314{
5315 /*
5316 * Only decay a single time; tasks that have less then 1 wakeup per
5317 * jiffy will not have built up many flips.
5318 */
5319 if (time_after(jiffies, current->wakee_flip_decay_ts + HZ)) {
5320 current->wakee_flips >>= 1;
5321 current->wakee_flip_decay_ts = jiffies;
5322 }
5323
5324 if (current->last_wakee != p) {
5325 current->last_wakee = p;
5326 current->wakee_flips++;
5327 }
5328}
5329
Mike Galbraith63b0e9e2015-07-14 17:39:50 +02005330/*
5331 * Detect M:N waker/wakee relationships via a switching-frequency heuristic.
Peter Zijlstrac58d25f2016-05-12 09:19:59 +02005332 *
Mike Galbraith63b0e9e2015-07-14 17:39:50 +02005333 * A waker of many should wake a different task than the one last awakened
Peter Zijlstrac58d25f2016-05-12 09:19:59 +02005334 * at a frequency roughly N times higher than one of its wakees.
5335 *
5336 * In order to determine whether we should let the load spread vs consolidating
5337 * to shared cache, we look for a minimum 'flip' frequency of llc_size in one
5338 * partner, and a factor of lls_size higher frequency in the other.
5339 *
5340 * With both conditions met, we can be relatively sure that the relationship is
5341 * non-monogamous, with partner count exceeding socket size.
5342 *
5343 * Waker/wakee being client/server, worker/dispatcher, interrupt source or
5344 * whatever is irrelevant, spread criteria is apparent partner count exceeds
5345 * socket size.
Mike Galbraith63b0e9e2015-07-14 17:39:50 +02005346 */
Michael Wang62470412013-07-04 12:55:51 +08005347static int wake_wide(struct task_struct *p)
5348{
Mike Galbraith63b0e9e2015-07-14 17:39:50 +02005349 unsigned int master = current->wakee_flips;
5350 unsigned int slave = p->wakee_flips;
Peter Zijlstra7d9ffa82013-07-04 12:56:46 +08005351 int factor = this_cpu_read(sd_llc_size);
Michael Wang62470412013-07-04 12:55:51 +08005352
Mike Galbraith63b0e9e2015-07-14 17:39:50 +02005353 if (master < slave)
5354 swap(master, slave);
5355 if (slave < factor || master < slave * factor)
5356 return 0;
5357 return 1;
Michael Wang62470412013-07-04 12:55:51 +08005358}
5359
Peter Zijlstra90001d62017-07-31 17:50:05 +02005360/*
Peter Zijlstrad153b152017-09-27 11:35:30 +02005361 * The purpose of wake_affine() is to quickly determine on which CPU we can run
5362 * soonest. For the purpose of speed we only consider the waking and previous
5363 * CPU.
Peter Zijlstra90001d62017-07-31 17:50:05 +02005364 *
Peter Zijlstrad153b152017-09-27 11:35:30 +02005365 * wake_affine_idle() - only considers 'now', it check if the waking CPU is (or
5366 * will be) idle.
Peter Zijlstraf2cdd9c2017-10-06 09:23:24 +02005367 *
5368 * wake_affine_weight() - considers the weight to reflect the average
5369 * scheduling latency of the CPUs. This seems to work
5370 * for the overloaded case.
Peter Zijlstra90001d62017-07-31 17:50:05 +02005371 */
Peter Zijlstrad153b152017-09-27 11:35:30 +02005372
Peter Zijlstra90001d62017-07-31 17:50:05 +02005373static bool
Peter Zijlstrad153b152017-09-27 11:35:30 +02005374wake_affine_idle(struct sched_domain *sd, struct task_struct *p,
5375 int this_cpu, int prev_cpu, int sync)
Peter Zijlstra90001d62017-07-31 17:50:05 +02005376{
Peter Zijlstrad153b152017-09-27 11:35:30 +02005377 if (idle_cpu(this_cpu))
Peter Zijlstra90001d62017-07-31 17:50:05 +02005378 return true;
5379
Peter Zijlstrad153b152017-09-27 11:35:30 +02005380 if (sync && cpu_rq(this_cpu)->nr_running == 1)
5381 return true;
Peter Zijlstra90001d62017-07-31 17:50:05 +02005382
Peter Zijlstrad153b152017-09-27 11:35:30 +02005383 return false;
Peter Zijlstra90001d62017-07-31 17:50:05 +02005384}
5385
Peter Zijlstraf2cdd9c2017-10-06 09:23:24 +02005386static bool
5387wake_affine_weight(struct sched_domain *sd, struct task_struct *p,
5388 int this_cpu, int prev_cpu, int sync)
5389{
5390 s64 this_eff_load, prev_eff_load;
5391 unsigned long task_load;
5392
5393 this_eff_load = target_load(this_cpu, sd->wake_idx);
5394 prev_eff_load = source_load(prev_cpu, sd->wake_idx);
5395
5396 if (sync) {
5397 unsigned long current_load = task_h_load(current);
5398
5399 if (current_load > this_eff_load)
5400 return true;
5401
5402 this_eff_load -= current_load;
5403 }
5404
5405 task_load = task_h_load(p);
5406
5407 this_eff_load += task_load;
5408 if (sched_feat(WA_BIAS))
5409 this_eff_load *= 100;
5410 this_eff_load *= capacity_of(prev_cpu);
5411
5412 prev_eff_load -= task_load;
5413 if (sched_feat(WA_BIAS))
5414 prev_eff_load *= 100 + (sd->imbalance_pct - 100) / 2;
5415 prev_eff_load *= capacity_of(this_cpu);
5416
5417 return this_eff_load <= prev_eff_load;
5418}
5419
Morten Rasmussen772bd008c2016-06-22 18:03:13 +01005420static int wake_affine(struct sched_domain *sd, struct task_struct *p,
5421 int prev_cpu, int sync)
Ingo Molnar098fb9d2008-03-16 20:36:10 +01005422{
Rik van Riel3fed3822017-06-23 12:55:29 -04005423 int this_cpu = smp_processor_id();
Peter Zijlstrad153b152017-09-27 11:35:30 +02005424 bool affine = false;
Ingo Molnar098fb9d2008-03-16 20:36:10 +01005425
Peter Zijlstrad153b152017-09-27 11:35:30 +02005426 if (sched_feat(WA_IDLE) && !affine)
5427 affine = wake_affine_idle(sd, p, this_cpu, prev_cpu, sync);
Mike Galbraithb3137bc2008-05-29 11:11:41 +02005428
Peter Zijlstraf2cdd9c2017-10-06 09:23:24 +02005429 if (sched_feat(WA_WEIGHT) && !affine)
5430 affine = wake_affine_weight(sd, p, this_cpu, prev_cpu, sync);
5431
Josh Poimboeufae928822016-06-17 12:43:24 -05005432 schedstat_inc(p->se.statistics.nr_wakeups_affine_attempts);
Rik van Riel3fed3822017-06-23 12:55:29 -04005433 if (affine) {
5434 schedstat_inc(sd->ttwu_move_affine);
5435 schedstat_inc(p->se.statistics.nr_wakeups_affine);
5436 }
Mike Galbraithb3137bc2008-05-29 11:11:41 +02005437
Rik van Riel3fed3822017-06-23 12:55:29 -04005438 return affine;
Ingo Molnar098fb9d2008-03-16 20:36:10 +01005439}
5440
Morten Rasmussen6a0b19c2016-10-14 14:41:08 +01005441static inline int task_util(struct task_struct *p);
5442static int cpu_util_wake(int cpu, struct task_struct *p);
5443
5444static unsigned long capacity_spare_wake(int cpu, struct task_struct *p)
5445{
5446 return capacity_orig_of(cpu) - cpu_util_wake(cpu, p);
5447}
5448
Peter Zijlstraaaee1202009-09-10 13:36:25 +02005449/*
5450 * find_idlest_group finds and returns the least busy CPU group within the
5451 * domain.
5452 */
5453static struct sched_group *
Peter Zijlstra78e7ed52009-09-03 13:16:51 +02005454find_idlest_group(struct sched_domain *sd, struct task_struct *p,
Vincent Guittotc44f2a02013-10-18 13:52:21 +02005455 int this_cpu, int sd_flag)
Gregory Haskinse7693a32008-01-25 21:08:09 +01005456{
Andi Kleenb3bd3de2010-08-10 14:17:51 -07005457 struct sched_group *idlest = NULL, *group = sd->groups;
Morten Rasmussen6a0b19c2016-10-14 14:41:08 +01005458 struct sched_group *most_spare_sg = NULL;
Vincent Guittot6b947802016-12-08 17:56:54 +01005459 unsigned long min_runnable_load = ULONG_MAX, this_runnable_load = 0;
5460 unsigned long min_avg_load = ULONG_MAX, this_avg_load = 0;
Morten Rasmussen6a0b19c2016-10-14 14:41:08 +01005461 unsigned long most_spare = 0, this_spare = 0;
Vincent Guittotc44f2a02013-10-18 13:52:21 +02005462 int load_idx = sd->forkexec_idx;
Vincent Guittot6b947802016-12-08 17:56:54 +01005463 int imbalance_scale = 100 + (sd->imbalance_pct-100)/2;
5464 unsigned long imbalance = scale_load_down(NICE_0_LOAD) *
5465 (sd->imbalance_pct-100) / 100;
Gregory Haskinse7693a32008-01-25 21:08:09 +01005466
Vincent Guittotc44f2a02013-10-18 13:52:21 +02005467 if (sd_flag & SD_BALANCE_WAKE)
5468 load_idx = sd->wake_idx;
5469
Peter Zijlstraaaee1202009-09-10 13:36:25 +02005470 do {
Vincent Guittot6b947802016-12-08 17:56:54 +01005471 unsigned long load, avg_load, runnable_load;
5472 unsigned long spare_cap, max_spare_cap;
Peter Zijlstraaaee1202009-09-10 13:36:25 +02005473 int local_group;
5474 int i;
Gregory Haskinse7693a32008-01-25 21:08:09 +01005475
Peter Zijlstraaaee1202009-09-10 13:36:25 +02005476 /* Skip over this group if it has no CPUs allowed */
Peter Zijlstraae4df9d2017-05-01 11:03:12 +02005477 if (!cpumask_intersects(sched_group_span(group),
Ingo Molnar0c98d342017-02-05 15:38:10 +01005478 &p->cpus_allowed))
Peter Zijlstraaaee1202009-09-10 13:36:25 +02005479 continue;
5480
5481 local_group = cpumask_test_cpu(this_cpu,
Peter Zijlstraae4df9d2017-05-01 11:03:12 +02005482 sched_group_span(group));
Peter Zijlstraaaee1202009-09-10 13:36:25 +02005483
Morten Rasmussen6a0b19c2016-10-14 14:41:08 +01005484 /*
5485 * Tally up the load of all CPUs in the group and find
5486 * the group containing the CPU with most spare capacity.
5487 */
Peter Zijlstraaaee1202009-09-10 13:36:25 +02005488 avg_load = 0;
Vincent Guittot6b947802016-12-08 17:56:54 +01005489 runnable_load = 0;
Morten Rasmussen6a0b19c2016-10-14 14:41:08 +01005490 max_spare_cap = 0;
Peter Zijlstraaaee1202009-09-10 13:36:25 +02005491
Peter Zijlstraae4df9d2017-05-01 11:03:12 +02005492 for_each_cpu(i, sched_group_span(group)) {
Peter Zijlstraaaee1202009-09-10 13:36:25 +02005493 /* Bias balancing toward cpus of our domain */
5494 if (local_group)
5495 load = source_load(i, load_idx);
5496 else
5497 load = target_load(i, load_idx);
5498
Vincent Guittot6b947802016-12-08 17:56:54 +01005499 runnable_load += load;
5500
5501 avg_load += cfs_rq_load_avg(&cpu_rq(i)->cfs);
Morten Rasmussen6a0b19c2016-10-14 14:41:08 +01005502
5503 spare_cap = capacity_spare_wake(i, p);
5504
5505 if (spare_cap > max_spare_cap)
5506 max_spare_cap = spare_cap;
Peter Zijlstraaaee1202009-09-10 13:36:25 +02005507 }
5508
Nicolas Pitre63b2ca32014-05-26 18:19:37 -04005509 /* Adjust by relative CPU capacity of the group */
Vincent Guittot6b947802016-12-08 17:56:54 +01005510 avg_load = (avg_load * SCHED_CAPACITY_SCALE) /
5511 group->sgc->capacity;
5512 runnable_load = (runnable_load * SCHED_CAPACITY_SCALE) /
5513 group->sgc->capacity;
Peter Zijlstraaaee1202009-09-10 13:36:25 +02005514
5515 if (local_group) {
Vincent Guittot6b947802016-12-08 17:56:54 +01005516 this_runnable_load = runnable_load;
5517 this_avg_load = avg_load;
Morten Rasmussen6a0b19c2016-10-14 14:41:08 +01005518 this_spare = max_spare_cap;
5519 } else {
Vincent Guittot6b947802016-12-08 17:56:54 +01005520 if (min_runnable_load > (runnable_load + imbalance)) {
5521 /*
5522 * The runnable load is significantly smaller
5523 * so we can pick this new cpu
5524 */
5525 min_runnable_load = runnable_load;
5526 min_avg_load = avg_load;
5527 idlest = group;
5528 } else if ((runnable_load < (min_runnable_load + imbalance)) &&
5529 (100*min_avg_load > imbalance_scale*avg_load)) {
5530 /*
5531 * The runnable loads are close so take the
5532 * blocked load into account through avg_load.
5533 */
5534 min_avg_load = avg_load;
Morten Rasmussen6a0b19c2016-10-14 14:41:08 +01005535 idlest = group;
5536 }
5537
5538 if (most_spare < max_spare_cap) {
5539 most_spare = max_spare_cap;
5540 most_spare_sg = group;
5541 }
Peter Zijlstraaaee1202009-09-10 13:36:25 +02005542 }
5543 } while (group = group->next, group != sd->groups);
5544
Morten Rasmussen6a0b19c2016-10-14 14:41:08 +01005545 /*
5546 * The cross-over point between using spare capacity or least load
5547 * is too conservative for high utilization tasks on partially
5548 * utilized systems if we require spare_capacity > task_util(p),
5549 * so we allow for some task stuffing by using
5550 * spare_capacity > task_util(p)/2.
Vincent Guittotf519a3f2016-12-08 17:56:53 +01005551 *
5552 * Spare capacity can't be used for fork because the utilization has
5553 * not been set yet, we must first select a rq to compute the initial
5554 * utilization.
Morten Rasmussen6a0b19c2016-10-14 14:41:08 +01005555 */
Vincent Guittotf519a3f2016-12-08 17:56:53 +01005556 if (sd_flag & SD_BALANCE_FORK)
5557 goto skip_spare;
5558
Morten Rasmussen6a0b19c2016-10-14 14:41:08 +01005559 if (this_spare > task_util(p) / 2 &&
Vincent Guittot6b947802016-12-08 17:56:54 +01005560 imbalance_scale*this_spare > 100*most_spare)
Morten Rasmussen6a0b19c2016-10-14 14:41:08 +01005561 return NULL;
Vincent Guittot6b947802016-12-08 17:56:54 +01005562
5563 if (most_spare > task_util(p) / 2)
Morten Rasmussen6a0b19c2016-10-14 14:41:08 +01005564 return most_spare_sg;
5565
Vincent Guittotf519a3f2016-12-08 17:56:53 +01005566skip_spare:
Vincent Guittot6b947802016-12-08 17:56:54 +01005567 if (!idlest)
Peter Zijlstraaaee1202009-09-10 13:36:25 +02005568 return NULL;
Vincent Guittot6b947802016-12-08 17:56:54 +01005569
5570 if (min_runnable_load > (this_runnable_load + imbalance))
5571 return NULL;
5572
5573 if ((this_runnable_load < (min_runnable_load + imbalance)) &&
5574 (100*this_avg_load < imbalance_scale*min_avg_load))
5575 return NULL;
5576
Peter Zijlstraaaee1202009-09-10 13:36:25 +02005577 return idlest;
5578}
5579
5580/*
5581 * find_idlest_cpu - find the idlest cpu among the cpus in group.
5582 */
5583static int
5584find_idlest_cpu(struct sched_group *group, struct task_struct *p, int this_cpu)
5585{
5586 unsigned long load, min_load = ULONG_MAX;
Nicolas Pitre83a0a962014-09-04 11:32:10 -04005587 unsigned int min_exit_latency = UINT_MAX;
5588 u64 latest_idle_timestamp = 0;
5589 int least_loaded_cpu = this_cpu;
5590 int shallowest_idle_cpu = -1;
Peter Zijlstraaaee1202009-09-10 13:36:25 +02005591 int i;
5592
Morten Rasmusseneaecf412016-06-22 18:03:14 +01005593 /* Check if we have any choice: */
5594 if (group->group_weight == 1)
Peter Zijlstraae4df9d2017-05-01 11:03:12 +02005595 return cpumask_first(sched_group_span(group));
Morten Rasmusseneaecf412016-06-22 18:03:14 +01005596
Peter Zijlstraaaee1202009-09-10 13:36:25 +02005597 /* Traverse only the allowed CPUs */
Peter Zijlstraae4df9d2017-05-01 11:03:12 +02005598 for_each_cpu_and(i, sched_group_span(group), &p->cpus_allowed) {
Nicolas Pitre83a0a962014-09-04 11:32:10 -04005599 if (idle_cpu(i)) {
5600 struct rq *rq = cpu_rq(i);
5601 struct cpuidle_state *idle = idle_get_state(rq);
5602 if (idle && idle->exit_latency < min_exit_latency) {
5603 /*
5604 * We give priority to a CPU whose idle state
5605 * has the smallest exit latency irrespective
5606 * of any idle timestamp.
5607 */
5608 min_exit_latency = idle->exit_latency;
5609 latest_idle_timestamp = rq->idle_stamp;
5610 shallowest_idle_cpu = i;
5611 } else if ((!idle || idle->exit_latency == min_exit_latency) &&
5612 rq->idle_stamp > latest_idle_timestamp) {
5613 /*
5614 * If equal or no active idle state, then
5615 * the most recently idled CPU might have
5616 * a warmer cache.
5617 */
5618 latest_idle_timestamp = rq->idle_stamp;
5619 shallowest_idle_cpu = i;
5620 }
Yao Dongdong9f967422014-10-28 04:08:06 +00005621 } else if (shallowest_idle_cpu == -1) {
Viresh Kumarc7132dd2017-05-24 10:59:54 +05305622 load = weighted_cpuload(cpu_rq(i));
Nicolas Pitre83a0a962014-09-04 11:32:10 -04005623 if (load < min_load || (load == min_load && i == this_cpu)) {
5624 min_load = load;
5625 least_loaded_cpu = i;
5626 }
Gregory Haskinse7693a32008-01-25 21:08:09 +01005627 }
5628 }
5629
Nicolas Pitre83a0a962014-09-04 11:32:10 -04005630 return shallowest_idle_cpu != -1 ? shallowest_idle_cpu : least_loaded_cpu;
Peter Zijlstraaaee1202009-09-10 13:36:25 +02005631}
Gregory Haskinse7693a32008-01-25 21:08:09 +01005632
Peter Zijlstra10e2f1a2016-05-09 10:38:05 +02005633#ifdef CONFIG_SCHED_SMT
5634
5635static inline void set_idle_cores(int cpu, int val)
5636{
5637 struct sched_domain_shared *sds;
5638
5639 sds = rcu_dereference(per_cpu(sd_llc_shared, cpu));
5640 if (sds)
5641 WRITE_ONCE(sds->has_idle_cores, val);
5642}
5643
5644static inline bool test_idle_cores(int cpu, bool def)
5645{
5646 struct sched_domain_shared *sds;
5647
5648 sds = rcu_dereference(per_cpu(sd_llc_shared, cpu));
5649 if (sds)
5650 return READ_ONCE(sds->has_idle_cores);
5651
5652 return def;
5653}
5654
5655/*
5656 * Scans the local SMT mask to see if the entire core is idle, and records this
5657 * information in sd_llc_shared->has_idle_cores.
5658 *
5659 * Since SMT siblings share all cache levels, inspecting this limited remote
5660 * state should be fairly cheap.
5661 */
Peter Zijlstra1b568f02016-05-09 10:38:41 +02005662void __update_idle_core(struct rq *rq)
Peter Zijlstra10e2f1a2016-05-09 10:38:05 +02005663{
5664 int core = cpu_of(rq);
5665 int cpu;
5666
5667 rcu_read_lock();
5668 if (test_idle_cores(core, true))
5669 goto unlock;
5670
5671 for_each_cpu(cpu, cpu_smt_mask(core)) {
5672 if (cpu == core)
5673 continue;
5674
5675 if (!idle_cpu(cpu))
5676 goto unlock;
5677 }
5678
5679 set_idle_cores(core, 1);
5680unlock:
5681 rcu_read_unlock();
5682}
5683
5684/*
5685 * Scan the entire LLC domain for idle cores; this dynamically switches off if
5686 * there are no idle cores left in the system; tracked through
5687 * sd_llc->shared->has_idle_cores and enabled through update_idle_core() above.
5688 */
5689static int select_idle_core(struct task_struct *p, struct sched_domain *sd, int target)
5690{
5691 struct cpumask *cpus = this_cpu_cpumask_var_ptr(select_idle_mask);
Peter Zijlstrac743f0a2017-04-14 14:20:05 +02005692 int core, cpu;
Peter Zijlstra10e2f1a2016-05-09 10:38:05 +02005693
Peter Zijlstra1b568f02016-05-09 10:38:41 +02005694 if (!static_branch_likely(&sched_smt_present))
5695 return -1;
5696
Peter Zijlstra10e2f1a2016-05-09 10:38:05 +02005697 if (!test_idle_cores(target, false))
5698 return -1;
5699
Ingo Molnar0c98d342017-02-05 15:38:10 +01005700 cpumask_and(cpus, sched_domain_span(sd), &p->cpus_allowed);
Peter Zijlstra10e2f1a2016-05-09 10:38:05 +02005701
Peter Zijlstrac743f0a2017-04-14 14:20:05 +02005702 for_each_cpu_wrap(core, cpus, target) {
Peter Zijlstra10e2f1a2016-05-09 10:38:05 +02005703 bool idle = true;
5704
5705 for_each_cpu(cpu, cpu_smt_mask(core)) {
5706 cpumask_clear_cpu(cpu, cpus);
5707 if (!idle_cpu(cpu))
5708 idle = false;
5709 }
5710
5711 if (idle)
5712 return core;
5713 }
5714
5715 /*
5716 * Failed to find an idle core; stop looking for one.
5717 */
5718 set_idle_cores(target, 0);
5719
5720 return -1;
5721}
5722
5723/*
5724 * Scan the local SMT mask for idle CPUs.
5725 */
5726static int select_idle_smt(struct task_struct *p, struct sched_domain *sd, int target)
5727{
5728 int cpu;
5729
Peter Zijlstra1b568f02016-05-09 10:38:41 +02005730 if (!static_branch_likely(&sched_smt_present))
5731 return -1;
5732
Peter Zijlstra10e2f1a2016-05-09 10:38:05 +02005733 for_each_cpu(cpu, cpu_smt_mask(target)) {
Ingo Molnar0c98d342017-02-05 15:38:10 +01005734 if (!cpumask_test_cpu(cpu, &p->cpus_allowed))
Peter Zijlstra10e2f1a2016-05-09 10:38:05 +02005735 continue;
5736 if (idle_cpu(cpu))
5737 return cpu;
5738 }
5739
5740 return -1;
5741}
5742
5743#else /* CONFIG_SCHED_SMT */
5744
5745static inline int select_idle_core(struct task_struct *p, struct sched_domain *sd, int target)
5746{
5747 return -1;
5748}
5749
5750static inline int select_idle_smt(struct task_struct *p, struct sched_domain *sd, int target)
5751{
5752 return -1;
5753}
5754
5755#endif /* CONFIG_SCHED_SMT */
5756
5757/*
5758 * Scan the LLC domain for idle CPUs; this is dynamically regulated by
5759 * comparing the average scan cost (tracked in sd->avg_scan_cost) against the
5760 * average idle time for this rq (as found in rq->avg_idle).
5761 */
5762static int select_idle_cpu(struct task_struct *p, struct sched_domain *sd, int target)
5763{
Wanpeng Li9cfb38a2016-10-09 08:04:03 +08005764 struct sched_domain *this_sd;
Peter Zijlstra1ad3aaf2017-05-17 12:53:50 +02005765 u64 avg_cost, avg_idle;
Peter Zijlstra10e2f1a2016-05-09 10:38:05 +02005766 u64 time, cost;
5767 s64 delta;
Peter Zijlstra1ad3aaf2017-05-17 12:53:50 +02005768 int cpu, nr = INT_MAX;
Peter Zijlstra10e2f1a2016-05-09 10:38:05 +02005769
Wanpeng Li9cfb38a2016-10-09 08:04:03 +08005770 this_sd = rcu_dereference(*this_cpu_ptr(&sd_llc));
5771 if (!this_sd)
5772 return -1;
5773
Peter Zijlstra10e2f1a2016-05-09 10:38:05 +02005774 /*
5775 * Due to large variance we need a large fuzz factor; hackbench in
5776 * particularly is sensitive here.
5777 */
Peter Zijlstra1ad3aaf2017-05-17 12:53:50 +02005778 avg_idle = this_rq()->avg_idle / 512;
5779 avg_cost = this_sd->avg_scan_cost + 1;
5780
5781 if (sched_feat(SIS_AVG_CPU) && avg_idle < avg_cost)
Peter Zijlstra10e2f1a2016-05-09 10:38:05 +02005782 return -1;
5783
Peter Zijlstra1ad3aaf2017-05-17 12:53:50 +02005784 if (sched_feat(SIS_PROP)) {
5785 u64 span_avg = sd->span_weight * avg_idle;
5786 if (span_avg > 4*avg_cost)
5787 nr = div_u64(span_avg, avg_cost);
5788 else
5789 nr = 4;
5790 }
5791
Peter Zijlstra10e2f1a2016-05-09 10:38:05 +02005792 time = local_clock();
5793
Peter Zijlstrac743f0a2017-04-14 14:20:05 +02005794 for_each_cpu_wrap(cpu, sched_domain_span(sd), target) {
Peter Zijlstra1ad3aaf2017-05-17 12:53:50 +02005795 if (!--nr)
5796 return -1;
Ingo Molnar0c98d342017-02-05 15:38:10 +01005797 if (!cpumask_test_cpu(cpu, &p->cpus_allowed))
Peter Zijlstra10e2f1a2016-05-09 10:38:05 +02005798 continue;
5799 if (idle_cpu(cpu))
5800 break;
5801 }
5802
5803 time = local_clock() - time;
5804 cost = this_sd->avg_scan_cost;
5805 delta = (s64)(time - cost) / 8;
5806 this_sd->avg_scan_cost += delta;
5807
5808 return cpu;
5809}
5810
5811/*
5812 * Try and locate an idle core/thread in the LLC cache domain.
Peter Zijlstraa50bde52009-11-12 15:55:28 +01005813 */
Morten Rasmussen772bd008c2016-06-22 18:03:13 +01005814static int select_idle_sibling(struct task_struct *p, int prev, int target)
Peter Zijlstraa50bde52009-11-12 15:55:28 +01005815{
Suresh Siddha99bd5e22010-03-31 16:47:45 -07005816 struct sched_domain *sd;
Peter Zijlstra10e2f1a2016-05-09 10:38:05 +02005817 int i;
Mike Galbraithe0a79f52013-01-28 12:19:25 +01005818
5819 if (idle_cpu(target))
5820 return target;
Peter Zijlstraa50bde52009-11-12 15:55:28 +01005821
5822 /*
Peter Zijlstra10e2f1a2016-05-09 10:38:05 +02005823 * If the previous cpu is cache affine and idle, don't be stupid.
Peter Zijlstraa50bde52009-11-12 15:55:28 +01005824 */
Morten Rasmussen772bd008c2016-06-22 18:03:13 +01005825 if (prev != target && cpus_share_cache(prev, target) && idle_cpu(prev))
5826 return prev;
Peter Zijlstraa50bde52009-11-12 15:55:28 +01005827
Peter Zijlstra518cd622011-12-07 15:07:31 +01005828 sd = rcu_dereference(per_cpu(sd_llc, target));
Peter Zijlstra10e2f1a2016-05-09 10:38:05 +02005829 if (!sd)
5830 return target;
Morten Rasmussen772bd008c2016-06-22 18:03:13 +01005831
Peter Zijlstra10e2f1a2016-05-09 10:38:05 +02005832 i = select_idle_core(p, sd, target);
5833 if ((unsigned)i < nr_cpumask_bits)
Gregory Haskinse7693a32008-01-25 21:08:09 +01005834 return i;
Ingo Molnar098fb9d2008-03-16 20:36:10 +01005835
Peter Zijlstra10e2f1a2016-05-09 10:38:05 +02005836 i = select_idle_cpu(p, sd, target);
5837 if ((unsigned)i < nr_cpumask_bits)
5838 return i;
Mike Galbraith970e1782012-06-12 05:18:32 +02005839
Peter Zijlstra10e2f1a2016-05-09 10:38:05 +02005840 i = select_idle_smt(p, sd, target);
5841 if ((unsigned)i < nr_cpumask_bits)
5842 return i;
Linus Torvalds37407ea2012-09-16 12:29:43 -07005843
Peter Zijlstraa50bde52009-11-12 15:55:28 +01005844 return target;
5845}
Dietmar Eggemann231678b2015-08-14 17:23:13 +01005846
Vincent Guittot8bb5b002015-03-04 08:48:47 +01005847/*
Dietmar Eggemann9e91d612015-08-14 17:23:12 +01005848 * cpu_util returns the amount of capacity of a CPU that is used by CFS
Vincent Guittot8bb5b002015-03-04 08:48:47 +01005849 * tasks. The unit of the return value must be the one of capacity so we can
Dietmar Eggemann9e91d612015-08-14 17:23:12 +01005850 * compare the utilization with the capacity of the CPU that is available for
5851 * CFS task (ie cpu_capacity).
Dietmar Eggemann231678b2015-08-14 17:23:13 +01005852 *
5853 * cfs_rq.avg.util_avg is the sum of running time of runnable tasks plus the
5854 * recent utilization of currently non-runnable tasks on a CPU. It represents
5855 * the amount of utilization of a CPU in the range [0..capacity_orig] where
5856 * capacity_orig is the cpu_capacity available at the highest frequency
5857 * (arch_scale_freq_capacity()).
5858 * The utilization of a CPU converges towards a sum equal to or less than the
5859 * current capacity (capacity_curr <= capacity_orig) of the CPU because it is
5860 * the running time on this CPU scaled by capacity_curr.
5861 *
5862 * Nevertheless, cfs_rq.avg.util_avg can be higher than capacity_curr or even
5863 * higher than capacity_orig because of unfortunate rounding in
5864 * cfs.avg.util_avg or just after migrating tasks and new task wakeups until
5865 * the average stabilizes with the new running time. We need to check that the
5866 * utilization stays within the range of [0..capacity_orig] and cap it if
5867 * necessary. Without utilization capping, a group could be seen as overloaded
5868 * (CPU0 utilization at 121% + CPU1 utilization at 80%) whereas CPU1 has 20% of
5869 * available capacity. We allow utilization to overshoot capacity_curr (but not
5870 * capacity_orig) as it useful for predicting the capacity required after task
5871 * migrations (scheduler-driven DVFS).
Vincent Guittot8bb5b002015-03-04 08:48:47 +01005872 */
Dietmar Eggemann9e91d612015-08-14 17:23:12 +01005873static int cpu_util(int cpu)
Vincent Guittot8bb5b002015-03-04 08:48:47 +01005874{
Dietmar Eggemann9e91d612015-08-14 17:23:12 +01005875 unsigned long util = cpu_rq(cpu)->cfs.avg.util_avg;
Vincent Guittot8bb5b002015-03-04 08:48:47 +01005876 unsigned long capacity = capacity_orig_of(cpu);
5877
Dietmar Eggemann231678b2015-08-14 17:23:13 +01005878 return (util >= capacity) ? capacity : util;
Vincent Guittot8bb5b002015-03-04 08:48:47 +01005879}
Peter Zijlstraa50bde52009-11-12 15:55:28 +01005880
Morten Rasmussen32731632016-07-25 14:34:26 +01005881static inline int task_util(struct task_struct *p)
5882{
5883 return p->se.avg.util_avg;
5884}
5885
5886/*
Morten Rasmussen104cb162016-10-14 14:41:07 +01005887 * cpu_util_wake: Compute cpu utilization with any contributions from
5888 * the waking task p removed.
5889 */
5890static int cpu_util_wake(int cpu, struct task_struct *p)
5891{
5892 unsigned long util, capacity;
5893
5894 /* Task has no contribution or is new */
5895 if (cpu != task_cpu(p) || !p->se.avg.last_update_time)
5896 return cpu_util(cpu);
5897
5898 capacity = capacity_orig_of(cpu);
5899 util = max_t(long, cpu_rq(cpu)->cfs.avg.util_avg - task_util(p), 0);
5900
5901 return (util >= capacity) ? capacity : util;
5902}
5903
5904/*
Morten Rasmussen32731632016-07-25 14:34:26 +01005905 * Disable WAKE_AFFINE in the case where task @p doesn't fit in the
5906 * capacity of either the waking CPU @cpu or the previous CPU @prev_cpu.
5907 *
5908 * In that case WAKE_AFFINE doesn't make sense and we'll let
5909 * BALANCE_WAKE sort things out.
5910 */
5911static int wake_cap(struct task_struct *p, int cpu, int prev_cpu)
5912{
5913 long min_cap, max_cap;
5914
5915 min_cap = min(capacity_orig_of(prev_cpu), capacity_orig_of(cpu));
5916 max_cap = cpu_rq(cpu)->rd->max_cpu_capacity;
5917
5918 /* Minimum capacity is close to max, no need to abort wake_affine */
5919 if (max_cap - min_cap < max_cap >> 3)
5920 return 0;
5921
Morten Rasmussen104cb162016-10-14 14:41:07 +01005922 /* Bring task utilization in sync with prev_cpu */
5923 sync_entity_load_avg(&p->se);
5924
Morten Rasmussen32731632016-07-25 14:34:26 +01005925 return min_cap * 1024 < task_util(p) * capacity_margin;
5926}
5927
Peter Zijlstraa50bde52009-11-12 15:55:28 +01005928/*
Morten Rasmussende91b9c2014-02-18 14:14:24 +00005929 * select_task_rq_fair: Select target runqueue for the waking task in domains
5930 * that have the 'sd_flag' flag set. In practice, this is SD_BALANCE_WAKE,
5931 * SD_BALANCE_FORK, or SD_BALANCE_EXEC.
Peter Zijlstraaaee1202009-09-10 13:36:25 +02005932 *
Morten Rasmussende91b9c2014-02-18 14:14:24 +00005933 * Balances load by selecting the idlest cpu in the idlest group, or under
5934 * certain conditions an idle sibling cpu if the domain has SD_WAKE_AFFINE set.
Peter Zijlstraaaee1202009-09-10 13:36:25 +02005935 *
Morten Rasmussende91b9c2014-02-18 14:14:24 +00005936 * Returns the target cpu number.
Peter Zijlstraaaee1202009-09-10 13:36:25 +02005937 *
5938 * preempt must be disabled.
5939 */
Peter Zijlstra0017d732010-03-24 18:34:10 +01005940static int
Peter Zijlstraac66f542013-10-07 11:29:16 +01005941select_task_rq_fair(struct task_struct *p, int prev_cpu, int sd_flag, int wake_flags)
Peter Zijlstraaaee1202009-09-10 13:36:25 +02005942{
Peter Zijlstra29cd8ba2009-09-17 09:01:14 +02005943 struct sched_domain *tmp, *affine_sd = NULL, *sd = NULL;
Peter Zijlstrac88d5912009-09-10 13:50:02 +02005944 int cpu = smp_processor_id();
Mike Galbraith63b0e9e2015-07-14 17:39:50 +02005945 int new_cpu = prev_cpu;
Suresh Siddha99bd5e22010-03-31 16:47:45 -07005946 int want_affine = 0;
Peter Zijlstra5158f4e2009-09-16 13:46:59 +02005947 int sync = wake_flags & WF_SYNC;
Gregory Haskinse7693a32008-01-25 21:08:09 +01005948
Peter Zijlstrac58d25f2016-05-12 09:19:59 +02005949 if (sd_flag & SD_BALANCE_WAKE) {
5950 record_wakee(p);
Morten Rasmussen32731632016-07-25 14:34:26 +01005951 want_affine = !wake_wide(p) && !wake_cap(p, cpu, prev_cpu)
Ingo Molnar0c98d342017-02-05 15:38:10 +01005952 && cpumask_test_cpu(cpu, &p->cpus_allowed);
Peter Zijlstrac58d25f2016-05-12 09:19:59 +02005953 }
Gregory Haskinse7693a32008-01-25 21:08:09 +01005954
Peter Zijlstradce840a2011-04-07 14:09:50 +02005955 rcu_read_lock();
Peter Zijlstraaaee1202009-09-10 13:36:25 +02005956 for_each_domain(cpu, tmp) {
Peter Zijlstrae4f42882009-12-16 18:04:34 +01005957 if (!(tmp->flags & SD_LOAD_BALANCE))
Mike Galbraith63b0e9e2015-07-14 17:39:50 +02005958 break;
Peter Zijlstrae4f42882009-12-16 18:04:34 +01005959
Peter Zijlstraaaee1202009-09-10 13:36:25 +02005960 /*
Suresh Siddha99bd5e22010-03-31 16:47:45 -07005961 * If both cpu and prev_cpu are part of this domain,
5962 * cpu is a valid SD_WAKE_AFFINE target.
Peter Zijlstrafe3bcfe2009-11-12 15:55:29 +01005963 */
Suresh Siddha99bd5e22010-03-31 16:47:45 -07005964 if (want_affine && (tmp->flags & SD_WAKE_AFFINE) &&
5965 cpumask_test_cpu(prev_cpu, sched_domain_span(tmp))) {
5966 affine_sd = tmp;
Alex Shif03542a2012-07-26 08:55:34 +08005967 break;
Peter Zijlstrac88d5912009-09-10 13:50:02 +02005968 }
5969
Alex Shif03542a2012-07-26 08:55:34 +08005970 if (tmp->flags & sd_flag)
Peter Zijlstra29cd8ba2009-09-17 09:01:14 +02005971 sd = tmp;
Mike Galbraith63b0e9e2015-07-14 17:39:50 +02005972 else if (!want_affine)
5973 break;
Peter Zijlstrac88d5912009-09-10 13:50:02 +02005974 }
Peter Zijlstraaaee1202009-09-10 13:36:25 +02005975
Mike Galbraith63b0e9e2015-07-14 17:39:50 +02005976 if (affine_sd) {
5977 sd = NULL; /* Prefer wake_affine over balance flags */
Rik van Riel7d894e62017-06-23 12:55:28 -04005978 if (cpu == prev_cpu)
5979 goto pick_cpu;
5980
5981 if (wake_affine(affine_sd, p, prev_cpu, sync))
Mike Galbraith63b0e9e2015-07-14 17:39:50 +02005982 new_cpu = cpu;
Mike Galbraith8b911ac2010-03-11 17:17:16 +01005983 }
Peter Zijlstra3b640892009-09-16 13:44:33 +02005984
Mike Galbraith63b0e9e2015-07-14 17:39:50 +02005985 if (!sd) {
Rik van Riel7d894e62017-06-23 12:55:28 -04005986 pick_cpu:
Mike Galbraith63b0e9e2015-07-14 17:39:50 +02005987 if (sd_flag & SD_BALANCE_WAKE) /* XXX always ? */
Morten Rasmussen772bd008c2016-06-22 18:03:13 +01005988 new_cpu = select_idle_sibling(p, prev_cpu, new_cpu);
Mike Galbraith63b0e9e2015-07-14 17:39:50 +02005989
5990 } else while (sd) {
Peter Zijlstraaaee1202009-09-10 13:36:25 +02005991 struct sched_group *group;
Peter Zijlstrac88d5912009-09-10 13:50:02 +02005992 int weight;
Peter Zijlstraaaee1202009-09-10 13:36:25 +02005993
Peter Zijlstra0763a662009-09-14 19:37:39 +02005994 if (!(sd->flags & sd_flag)) {
Peter Zijlstraaaee1202009-09-10 13:36:25 +02005995 sd = sd->child;
5996 continue;
5997 }
5998
Vincent Guittotc44f2a02013-10-18 13:52:21 +02005999 group = find_idlest_group(sd, p, cpu, sd_flag);
Peter Zijlstraaaee1202009-09-10 13:36:25 +02006000 if (!group) {
6001 sd = sd->child;
6002 continue;
6003 }
6004
Peter Zijlstrad7c33c42009-09-11 12:45:38 +02006005 new_cpu = find_idlest_cpu(group, p, cpu);
Peter Zijlstraaaee1202009-09-10 13:36:25 +02006006 if (new_cpu == -1 || new_cpu == cpu) {
6007 /* Now try balancing at a lower domain level of cpu */
6008 sd = sd->child;
6009 continue;
6010 }
6011
6012 /* Now try balancing at a lower domain level of new_cpu */
6013 cpu = new_cpu;
Peter Zijlstra669c55e2010-04-16 14:59:29 +02006014 weight = sd->span_weight;
Peter Zijlstraaaee1202009-09-10 13:36:25 +02006015 sd = NULL;
6016 for_each_domain(cpu, tmp) {
Peter Zijlstra669c55e2010-04-16 14:59:29 +02006017 if (weight <= tmp->span_weight)
Peter Zijlstraaaee1202009-09-10 13:36:25 +02006018 break;
Peter Zijlstra0763a662009-09-14 19:37:39 +02006019 if (tmp->flags & sd_flag)
Peter Zijlstraaaee1202009-09-10 13:36:25 +02006020 sd = tmp;
6021 }
6022 /* while loop will break here if sd == NULL */
Gregory Haskinse7693a32008-01-25 21:08:09 +01006023 }
Peter Zijlstradce840a2011-04-07 14:09:50 +02006024 rcu_read_unlock();
Gregory Haskinse7693a32008-01-25 21:08:09 +01006025
Peter Zijlstrac88d5912009-09-10 13:50:02 +02006026 return new_cpu;
Gregory Haskinse7693a32008-01-25 21:08:09 +01006027}
Paul Turner0a74bef2012-10-04 13:18:30 +02006028
6029/*
6030 * Called immediately before a task is migrated to a new cpu; task_cpu(p) and
6031 * cfs_rq_of(p) references at time of call are still valid and identify the
Byungchul Park525628c2015-11-18 09:34:59 +09006032 * previous cpu. The caller guarantees p->pi_lock or task_rq(p)->lock is held.
Paul Turner0a74bef2012-10-04 13:18:30 +02006033 */
xiaofeng.yan5a4fd032015-09-23 14:55:59 +08006034static void migrate_task_rq_fair(struct task_struct *p)
Paul Turner0a74bef2012-10-04 13:18:30 +02006035{
Paul Turneraff3e492012-10-04 13:18:30 +02006036 /*
Peter Zijlstra59efa0b2016-05-10 18:24:37 +02006037 * As blocked tasks retain absolute vruntime the migration needs to
6038 * deal with this by subtracting the old and adding the new
6039 * min_vruntime -- the latter is done by enqueue_entity() when placing
6040 * the task on the new runqueue.
6041 */
6042 if (p->state == TASK_WAKING) {
6043 struct sched_entity *se = &p->se;
6044 struct cfs_rq *cfs_rq = cfs_rq_of(se);
6045 u64 min_vruntime;
6046
6047#ifndef CONFIG_64BIT
6048 u64 min_vruntime_copy;
6049
6050 do {
6051 min_vruntime_copy = cfs_rq->min_vruntime_copy;
6052 smp_rmb();
6053 min_vruntime = cfs_rq->min_vruntime;
6054 } while (min_vruntime != min_vruntime_copy);
6055#else
6056 min_vruntime = cfs_rq->min_vruntime;
6057#endif
6058
6059 se->vruntime -= min_vruntime;
6060 }
6061
6062 /*
Yuyang Du9d89c252015-07-15 08:04:37 +08006063 * We are supposed to update the task to "current" time, then its up to date
6064 * and ready to go to new CPU/cfs_rq. But we have difficulty in getting
6065 * what current time is, so simply throw away the out-of-date time. This
6066 * will result in the wakee task is less decayed, but giving the wakee more
6067 * load sounds not bad.
Paul Turneraff3e492012-10-04 13:18:30 +02006068 */
Yuyang Du9d89c252015-07-15 08:04:37 +08006069 remove_entity_load_avg(&p->se);
6070
6071 /* Tell new CPU we are migrated */
6072 p->se.avg.last_update_time = 0;
Ben Segall3944a922014-05-15 15:59:20 -07006073
6074 /* We have migrated, no longer consider this task hot */
Yuyang Du9d89c252015-07-15 08:04:37 +08006075 p->se.exec_start = 0;
Paul Turner0a74bef2012-10-04 13:18:30 +02006076}
Yuyang Du12695572015-07-15 08:04:40 +08006077
6078static void task_dead_fair(struct task_struct *p)
6079{
6080 remove_entity_load_avg(&p->se);
6081}
Gregory Haskinse7693a32008-01-25 21:08:09 +01006082#endif /* CONFIG_SMP */
6083
Peter Zijlstrae52fb7c2009-01-14 12:39:19 +01006084static unsigned long
6085wakeup_gran(struct sched_entity *curr, struct sched_entity *se)
Peter Zijlstra0bbd3332008-04-19 19:44:57 +02006086{
6087 unsigned long gran = sysctl_sched_wakeup_granularity;
6088
6089 /*
Peter Zijlstrae52fb7c2009-01-14 12:39:19 +01006090 * Since its curr running now, convert the gran from real-time
6091 * to virtual-time in his units.
Mike Galbraith13814d42010-03-11 17:17:04 +01006092 *
6093 * By using 'se' instead of 'curr' we penalize light tasks, so
6094 * they get preempted easier. That is, if 'se' < 'curr' then
6095 * the resulting gran will be larger, therefore penalizing the
6096 * lighter, if otoh 'se' > 'curr' then the resulting gran will
6097 * be smaller, again penalizing the lighter task.
6098 *
6099 * This is especially important for buddies when the leftmost
6100 * task is higher priority than the buddy.
Peter Zijlstra0bbd3332008-04-19 19:44:57 +02006101 */
Shaohua Lif4ad9bd2011-04-08 12:53:09 +08006102 return calc_delta_fair(gran, se);
Peter Zijlstra0bbd3332008-04-19 19:44:57 +02006103}
6104
6105/*
Peter Zijlstra464b7522008-10-24 11:06:15 +02006106 * Should 'se' preempt 'curr'.
6107 *
6108 * |s1
6109 * |s2
6110 * |s3
6111 * g
6112 * |<--->|c
6113 *
6114 * w(c, s1) = -1
6115 * w(c, s2) = 0
6116 * w(c, s3) = 1
6117 *
6118 */
6119static int
6120wakeup_preempt_entity(struct sched_entity *curr, struct sched_entity *se)
6121{
6122 s64 gran, vdiff = curr->vruntime - se->vruntime;
6123
6124 if (vdiff <= 0)
6125 return -1;
6126
Peter Zijlstrae52fb7c2009-01-14 12:39:19 +01006127 gran = wakeup_gran(curr, se);
Peter Zijlstra464b7522008-10-24 11:06:15 +02006128 if (vdiff > gran)
6129 return 1;
6130
6131 return 0;
6132}
6133
Peter Zijlstra02479092008-11-04 21:25:10 +01006134static void set_last_buddy(struct sched_entity *se)
6135{
Venkatesh Pallipadi69c80f32011-04-13 18:21:09 -07006136 if (entity_is_task(se) && unlikely(task_of(se)->policy == SCHED_IDLE))
6137 return;
6138
Daniel Axtensc5ae3662017-05-11 06:11:39 +10006139 for_each_sched_entity(se) {
6140 if (SCHED_WARN_ON(!se->on_rq))
6141 return;
Venkatesh Pallipadi69c80f32011-04-13 18:21:09 -07006142 cfs_rq_of(se)->last = se;
Daniel Axtensc5ae3662017-05-11 06:11:39 +10006143 }
Peter Zijlstra02479092008-11-04 21:25:10 +01006144}
6145
6146static void set_next_buddy(struct sched_entity *se)
6147{
Venkatesh Pallipadi69c80f32011-04-13 18:21:09 -07006148 if (entity_is_task(se) && unlikely(task_of(se)->policy == SCHED_IDLE))
6149 return;
6150
Daniel Axtensc5ae3662017-05-11 06:11:39 +10006151 for_each_sched_entity(se) {
6152 if (SCHED_WARN_ON(!se->on_rq))
6153 return;
Venkatesh Pallipadi69c80f32011-04-13 18:21:09 -07006154 cfs_rq_of(se)->next = se;
Daniel Axtensc5ae3662017-05-11 06:11:39 +10006155 }
Peter Zijlstra02479092008-11-04 21:25:10 +01006156}
6157
Rik van Rielac53db52011-02-01 09:51:03 -05006158static void set_skip_buddy(struct sched_entity *se)
6159{
Venkatesh Pallipadi69c80f32011-04-13 18:21:09 -07006160 for_each_sched_entity(se)
6161 cfs_rq_of(se)->skip = se;
Rik van Rielac53db52011-02-01 09:51:03 -05006162}
6163
Peter Zijlstra464b7522008-10-24 11:06:15 +02006164/*
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02006165 * Preempt the current task with a newly woken task if needed:
6166 */
Peter Zijlstra5a9b86f2009-09-16 13:47:58 +02006167static void check_preempt_wakeup(struct rq *rq, struct task_struct *p, int wake_flags)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02006168{
6169 struct task_struct *curr = rq->curr;
Srivatsa Vaddagiri8651a862007-10-15 17:00:12 +02006170 struct sched_entity *se = &curr->se, *pse = &p->se;
Mike Galbraith03e89e42008-12-16 08:45:30 +01006171 struct cfs_rq *cfs_rq = task_cfs_rq(curr);
Mike Galbraithf685cea2009-10-23 23:09:22 +02006172 int scale = cfs_rq->nr_running >= sched_nr_latency;
Venkatesh Pallipadi2f368252011-04-14 10:30:53 -07006173 int next_buddy_marked = 0;
Mike Galbraith03e89e42008-12-16 08:45:30 +01006174
Ingo Molnar4ae7d5c2008-03-19 01:42:00 +01006175 if (unlikely(se == pse))
6176 return;
6177
Paul Turner5238cdd2011-07-21 09:43:37 -07006178 /*
Kirill Tkhai163122b2014-08-20 13:48:29 +04006179 * This is possible from callers such as attach_tasks(), in which we
Paul Turner5238cdd2011-07-21 09:43:37 -07006180 * unconditionally check_prempt_curr() after an enqueue (which may have
6181 * lead to a throttle). This both saves work and prevents false
6182 * next-buddy nomination below.
6183 */
6184 if (unlikely(throttled_hierarchy(cfs_rq_of(pse))))
6185 return;
6186
Venkatesh Pallipadi2f368252011-04-14 10:30:53 -07006187 if (sched_feat(NEXT_BUDDY) && scale && !(wake_flags & WF_FORK)) {
Mike Galbraith3cb63d52009-09-11 12:01:17 +02006188 set_next_buddy(pse);
Venkatesh Pallipadi2f368252011-04-14 10:30:53 -07006189 next_buddy_marked = 1;
6190 }
Peter Zijlstra57fdc262008-09-23 15:33:45 +02006191
Bharata B Raoaec0a512008-08-28 14:42:49 +05306192 /*
6193 * We can come here with TIF_NEED_RESCHED already set from new task
6194 * wake up path.
Paul Turner5238cdd2011-07-21 09:43:37 -07006195 *
6196 * Note: this also catches the edge-case of curr being in a throttled
6197 * group (e.g. via set_curr_task), since update_curr() (in the
6198 * enqueue of curr) will have resulted in resched being set. This
6199 * prevents us from potentially nominating it as a false LAST_BUDDY
6200 * below.
Bharata B Raoaec0a512008-08-28 14:42:49 +05306201 */
6202 if (test_tsk_need_resched(curr))
6203 return;
6204
Darren Harta2f5c9a2011-02-22 13:04:33 -08006205 /* Idle tasks are by definition preempted by non-idle tasks. */
6206 if (unlikely(curr->policy == SCHED_IDLE) &&
6207 likely(p->policy != SCHED_IDLE))
6208 goto preempt;
6209
Ingo Molnar91c234b2007-10-15 17:00:18 +02006210 /*
Darren Harta2f5c9a2011-02-22 13:04:33 -08006211 * Batch and idle tasks do not preempt non-idle tasks (their preemption
6212 * is driven by the tick):
Ingo Molnar91c234b2007-10-15 17:00:18 +02006213 */
Ingo Molnar8ed92e52012-10-14 14:28:50 +02006214 if (unlikely(p->policy != SCHED_NORMAL) || !sched_feat(WAKEUP_PREEMPTION))
Ingo Molnar91c234b2007-10-15 17:00:18 +02006215 return;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02006216
Peter Zijlstra3a7e73a2009-11-28 18:51:02 +01006217 find_matching_se(&se, &pse);
Paul Turner9bbd7372011-07-05 19:07:21 -07006218 update_curr(cfs_rq_of(se));
Peter Zijlstra3a7e73a2009-11-28 18:51:02 +01006219 BUG_ON(!pse);
Venkatesh Pallipadi2f368252011-04-14 10:30:53 -07006220 if (wakeup_preempt_entity(se, pse) == 1) {
6221 /*
6222 * Bias pick_next to pick the sched entity that is
6223 * triggering this preemption.
6224 */
6225 if (!next_buddy_marked)
6226 set_next_buddy(pse);
Peter Zijlstra3a7e73a2009-11-28 18:51:02 +01006227 goto preempt;
Venkatesh Pallipadi2f368252011-04-14 10:30:53 -07006228 }
Jupyung Leea65ac742009-11-17 18:51:40 +09006229
Peter Zijlstra3a7e73a2009-11-28 18:51:02 +01006230 return;
6231
6232preempt:
Kirill Tkhai88751252014-06-29 00:03:57 +04006233 resched_curr(rq);
Peter Zijlstra3a7e73a2009-11-28 18:51:02 +01006234 /*
6235 * Only set the backward buddy when the current task is still
6236 * on the rq. This can happen when a wakeup gets interleaved
6237 * with schedule on the ->pre_schedule() or idle_balance()
6238 * point, either of which can * drop the rq lock.
6239 *
6240 * Also, during early boot the idle thread is in the fair class,
6241 * for obvious reasons its a bad idea to schedule back to it.
6242 */
6243 if (unlikely(!se->on_rq || curr == rq->idle))
6244 return;
6245
6246 if (sched_feat(LAST_BUDDY) && scale && entity_is_task(se))
6247 set_last_buddy(se);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02006248}
6249
Peter Zijlstra606dba22012-02-11 06:05:00 +01006250static struct task_struct *
Matt Flemingd8ac8972016-09-21 14:38:10 +01006251pick_next_task_fair(struct rq *rq, struct task_struct *prev, struct rq_flags *rf)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02006252{
6253 struct cfs_rq *cfs_rq = &rq->cfs;
6254 struct sched_entity *se;
Peter Zijlstra678d5712012-02-11 06:05:00 +01006255 struct task_struct *p;
Peter Zijlstra37e117c2014-02-14 12:25:08 +01006256 int new_tasks;
Peter Zijlstra678d5712012-02-11 06:05:00 +01006257
Peter Zijlstra6e831252014-02-11 16:11:48 +01006258again:
Peter Zijlstra678d5712012-02-11 06:05:00 +01006259 if (!cfs_rq->nr_running)
Peter Zijlstra38033c32014-01-23 20:32:21 +01006260 goto idle;
Peter Zijlstra678d5712012-02-11 06:05:00 +01006261
Viresh Kumar9674f5c2017-05-24 10:59:55 +05306262#ifdef CONFIG_FAIR_GROUP_SCHED
Peter Zijlstra3f1d2a32014-02-12 10:49:30 +01006263 if (prev->sched_class != &fair_sched_class)
Peter Zijlstra678d5712012-02-11 06:05:00 +01006264 goto simple;
6265
6266 /*
6267 * Because of the set_next_buddy() in dequeue_task_fair() it is rather
6268 * likely that a next task is from the same cgroup as the current.
6269 *
6270 * Therefore attempt to avoid putting and setting the entire cgroup
6271 * hierarchy, only change the part that actually changes.
6272 */
6273
6274 do {
6275 struct sched_entity *curr = cfs_rq->curr;
6276
6277 /*
6278 * Since we got here without doing put_prev_entity() we also
6279 * have to consider cfs_rq->curr. If it is still a runnable
6280 * entity, update_curr() will update its vruntime, otherwise
6281 * forget we've ever seen it.
6282 */
Ben Segall54d27362015-04-06 15:28:10 -07006283 if (curr) {
6284 if (curr->on_rq)
6285 update_curr(cfs_rq);
6286 else
6287 curr = NULL;
Peter Zijlstra678d5712012-02-11 06:05:00 +01006288
Ben Segall54d27362015-04-06 15:28:10 -07006289 /*
6290 * This call to check_cfs_rq_runtime() will do the
6291 * throttle and dequeue its entity in the parent(s).
Viresh Kumar9674f5c2017-05-24 10:59:55 +05306292 * Therefore the nr_running test will indeed
Ben Segall54d27362015-04-06 15:28:10 -07006293 * be correct.
6294 */
Viresh Kumar9674f5c2017-05-24 10:59:55 +05306295 if (unlikely(check_cfs_rq_runtime(cfs_rq))) {
6296 cfs_rq = &rq->cfs;
6297
6298 if (!cfs_rq->nr_running)
6299 goto idle;
6300
Ben Segall54d27362015-04-06 15:28:10 -07006301 goto simple;
Viresh Kumar9674f5c2017-05-24 10:59:55 +05306302 }
Ben Segall54d27362015-04-06 15:28:10 -07006303 }
Peter Zijlstra678d5712012-02-11 06:05:00 +01006304
6305 se = pick_next_entity(cfs_rq, curr);
6306 cfs_rq = group_cfs_rq(se);
6307 } while (cfs_rq);
6308
6309 p = task_of(se);
6310
6311 /*
6312 * Since we haven't yet done put_prev_entity and if the selected task
6313 * is a different task than we started out with, try and touch the
6314 * least amount of cfs_rqs.
6315 */
6316 if (prev != p) {
6317 struct sched_entity *pse = &prev->se;
6318
6319 while (!(cfs_rq = is_same_group(se, pse))) {
6320 int se_depth = se->depth;
6321 int pse_depth = pse->depth;
6322
6323 if (se_depth <= pse_depth) {
6324 put_prev_entity(cfs_rq_of(pse), pse);
6325 pse = parent_entity(pse);
6326 }
6327 if (se_depth >= pse_depth) {
6328 set_next_entity(cfs_rq_of(se), se);
6329 se = parent_entity(se);
6330 }
6331 }
6332
6333 put_prev_entity(cfs_rq, pse);
6334 set_next_entity(cfs_rq, se);
6335 }
6336
6337 if (hrtick_enabled(rq))
6338 hrtick_start_fair(rq, p);
6339
6340 return p;
6341simple:
Peter Zijlstra678d5712012-02-11 06:05:00 +01006342#endif
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02006343
Peter Zijlstra3f1d2a32014-02-12 10:49:30 +01006344 put_prev_task(rq, prev);
Peter Zijlstra606dba22012-02-11 06:05:00 +01006345
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02006346 do {
Peter Zijlstra678d5712012-02-11 06:05:00 +01006347 se = pick_next_entity(cfs_rq, NULL);
Peter Zijlstraf4b67552008-11-04 21:25:07 +01006348 set_next_entity(cfs_rq, se);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02006349 cfs_rq = group_cfs_rq(se);
6350 } while (cfs_rq);
6351
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01006352 p = task_of(se);
Peter Zijlstra678d5712012-02-11 06:05:00 +01006353
Mike Galbraithb39e66e2011-11-22 15:20:07 +01006354 if (hrtick_enabled(rq))
6355 hrtick_start_fair(rq, p);
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01006356
6357 return p;
Peter Zijlstra38033c32014-01-23 20:32:21 +01006358
6359idle:
Matt Fleming46f69fa2016-09-21 14:38:12 +01006360 new_tasks = idle_balance(rq, rf);
6361
Peter Zijlstra37e117c2014-02-14 12:25:08 +01006362 /*
6363 * Because idle_balance() releases (and re-acquires) rq->lock, it is
6364 * possible for any higher priority task to appear. In that case we
6365 * must re-start the pick_next_entity() loop.
6366 */
Kirill Tkhaie4aa3582014-03-06 13:31:55 +04006367 if (new_tasks < 0)
Peter Zijlstra37e117c2014-02-14 12:25:08 +01006368 return RETRY_TASK;
6369
Kirill Tkhaie4aa3582014-03-06 13:31:55 +04006370 if (new_tasks > 0)
Peter Zijlstra38033c32014-01-23 20:32:21 +01006371 goto again;
Peter Zijlstra38033c32014-01-23 20:32:21 +01006372
6373 return NULL;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02006374}
6375
6376/*
6377 * Account for a descheduled task:
6378 */
Ingo Molnar31ee5292007-08-09 11:16:49 +02006379static void put_prev_task_fair(struct rq *rq, struct task_struct *prev)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02006380{
6381 struct sched_entity *se = &prev->se;
6382 struct cfs_rq *cfs_rq;
6383
6384 for_each_sched_entity(se) {
6385 cfs_rq = cfs_rq_of(se);
Ingo Molnarab6cde22007-08-09 11:16:48 +02006386 put_prev_entity(cfs_rq, se);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02006387 }
6388}
6389
Rik van Rielac53db52011-02-01 09:51:03 -05006390/*
6391 * sched_yield() is very simple
6392 *
6393 * The magic of dealing with the ->skip buddy is in pick_next_entity.
6394 */
6395static void yield_task_fair(struct rq *rq)
6396{
6397 struct task_struct *curr = rq->curr;
6398 struct cfs_rq *cfs_rq = task_cfs_rq(curr);
6399 struct sched_entity *se = &curr->se;
6400
6401 /*
6402 * Are we the only task in the tree?
6403 */
6404 if (unlikely(rq->nr_running == 1))
6405 return;
6406
6407 clear_buddies(cfs_rq, se);
6408
6409 if (curr->policy != SCHED_BATCH) {
6410 update_rq_clock(rq);
6411 /*
6412 * Update run-time statistics of the 'current'.
6413 */
6414 update_curr(cfs_rq);
Mike Galbraith916671c2011-11-22 15:21:26 +01006415 /*
6416 * Tell update_rq_clock() that we've just updated,
6417 * so we don't do microscopic update in schedule()
6418 * and double the fastpath cost.
6419 */
Peter Zijlstra9edfbfe2015-01-05 11:18:11 +01006420 rq_clock_skip_update(rq, true);
Rik van Rielac53db52011-02-01 09:51:03 -05006421 }
6422
6423 set_skip_buddy(se);
6424}
6425
Mike Galbraithd95f4122011-02-01 09:50:51 -05006426static bool yield_to_task_fair(struct rq *rq, struct task_struct *p, bool preempt)
6427{
6428 struct sched_entity *se = &p->se;
6429
Paul Turner5238cdd2011-07-21 09:43:37 -07006430 /* throttled hierarchies are not runnable */
6431 if (!se->on_rq || throttled_hierarchy(cfs_rq_of(se)))
Mike Galbraithd95f4122011-02-01 09:50:51 -05006432 return false;
6433
6434 /* Tell the scheduler that we'd really like pse to run next. */
6435 set_next_buddy(se);
6436
Mike Galbraithd95f4122011-02-01 09:50:51 -05006437 yield_task_fair(rq);
6438
6439 return true;
6440}
6441
Peter Williams681f3e62007-10-24 18:23:51 +02006442#ifdef CONFIG_SMP
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02006443/**************************************************
Peter Zijlstrae9c84cb2012-07-03 13:53:26 +02006444 * Fair scheduling class load-balancing methods.
6445 *
6446 * BASICS
6447 *
6448 * The purpose of load-balancing is to achieve the same basic fairness the
6449 * per-cpu scheduler provides, namely provide a proportional amount of compute
6450 * time to each task. This is expressed in the following equation:
6451 *
6452 * W_i,n/P_i == W_j,n/P_j for all i,j (1)
6453 *
6454 * Where W_i,n is the n-th weight average for cpu i. The instantaneous weight
6455 * W_i,0 is defined as:
6456 *
6457 * W_i,0 = \Sum_j w_i,j (2)
6458 *
6459 * 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 +08006460 * is derived from the nice value as per sched_prio_to_weight[].
Peter Zijlstrae9c84cb2012-07-03 13:53:26 +02006461 *
6462 * The weight average is an exponential decay average of the instantaneous
6463 * weight:
6464 *
6465 * W'_i,n = (2^n - 1) / 2^n * W_i,n + 1 / 2^n * W_i,0 (3)
6466 *
Nicolas Pitreced549f2014-05-26 18:19:38 -04006467 * C_i is the compute capacity of cpu i, typically it is the
Peter Zijlstrae9c84cb2012-07-03 13:53:26 +02006468 * fraction of 'recent' time available for SCHED_OTHER task execution. But it
6469 * can also include other factors [XXX].
6470 *
6471 * To achieve this balance we define a measure of imbalance which follows
6472 * directly from (1):
6473 *
Nicolas Pitreced549f2014-05-26 18:19:38 -04006474 * 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 +02006475 *
6476 * We them move tasks around to minimize the imbalance. In the continuous
6477 * function space it is obvious this converges, in the discrete case we get
6478 * a few fun cases generally called infeasible weight scenarios.
6479 *
6480 * [XXX expand on:
6481 * - infeasible weights;
6482 * - local vs global optima in the discrete case. ]
6483 *
6484 *
6485 * SCHED DOMAINS
6486 *
6487 * In order to solve the imbalance equation (4), and avoid the obvious O(n^2)
6488 * for all i,j solution, we create a tree of cpus that follows the hardware
6489 * topology where each level pairs two lower groups (or better). This results
6490 * in O(log n) layers. Furthermore we reduce the number of cpus going up the
6491 * tree to only the first of the previous level and we decrease the frequency
6492 * of load-balance at each level inv. proportional to the number of cpus in
6493 * the groups.
6494 *
6495 * This yields:
6496 *
6497 * log_2 n 1 n
6498 * \Sum { --- * --- * 2^i } = O(n) (5)
6499 * i = 0 2^i 2^i
6500 * `- size of each group
6501 * | | `- number of cpus doing load-balance
6502 * | `- freq
6503 * `- sum over all levels
6504 *
6505 * Coupled with a limit on how many tasks we can migrate every balance pass,
6506 * this makes (5) the runtime complexity of the balancer.
6507 *
6508 * An important property here is that each CPU is still (indirectly) connected
6509 * to every other cpu in at most O(log n) steps:
6510 *
6511 * The adjacency matrix of the resulting graph is given by:
6512 *
Byungchul Park97a71422015-07-05 18:33:48 +09006513 * log_2 n
Peter Zijlstrae9c84cb2012-07-03 13:53:26 +02006514 * A_i,j = \Union (i % 2^k == 0) && i / 2^(k+1) == j / 2^(k+1) (6)
6515 * k = 0
6516 *
6517 * And you'll find that:
6518 *
6519 * A^(log_2 n)_i,j != 0 for all i,j (7)
6520 *
6521 * Showing there's indeed a path between every cpu in at most O(log n) steps.
6522 * The task movement gives a factor of O(m), giving a convergence complexity
6523 * of:
6524 *
6525 * O(nm log n), n := nr_cpus, m := nr_tasks (8)
6526 *
6527 *
6528 * WORK CONSERVING
6529 *
6530 * In order to avoid CPUs going idle while there's still work to do, new idle
6531 * balancing is more aggressive and has the newly idle cpu iterate up the domain
6532 * tree itself instead of relying on other CPUs to bring it work.
6533 *
6534 * This adds some complexity to both (5) and (8) but it reduces the total idle
6535 * time.
6536 *
6537 * [XXX more?]
6538 *
6539 *
6540 * CGROUPS
6541 *
6542 * Cgroups make a horror show out of (2), instead of a simple sum we get:
6543 *
6544 * s_k,i
6545 * W_i,0 = \Sum_j \Prod_k w_k * ----- (9)
6546 * S_k
6547 *
6548 * Where
6549 *
6550 * s_k,i = \Sum_j w_i,j,k and S_k = \Sum_i s_k,i (10)
6551 *
6552 * w_i,j,k is the weight of the j-th runnable task in the k-th cgroup on cpu i.
6553 *
6554 * The big problem is S_k, its a global sum needed to compute a local (W_i)
6555 * property.
6556 *
6557 * [XXX write more on how we solve this.. _after_ merging pjt's patches that
6558 * rewrite all of this once again.]
Byungchul Park97a71422015-07-05 18:33:48 +09006559 */
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02006560
Hiroshi Shimamotoed387b72012-01-31 11:40:32 +09006561static unsigned long __read_mostly max_load_balance_interval = HZ/10;
6562
Peter Zijlstra0ec8aa02013-10-07 11:29:33 +01006563enum fbq_type { regular, remote, all };
6564
Peter Zijlstraddcdf6e2012-02-22 19:27:40 +01006565#define LBF_ALL_PINNED 0x01
Peter Zijlstra367456c2012-02-20 21:49:09 +01006566#define LBF_NEED_BREAK 0x02
Peter Zijlstra62633222013-08-19 12:41:09 +02006567#define LBF_DST_PINNED 0x04
6568#define LBF_SOME_PINNED 0x08
Peter Zijlstraddcdf6e2012-02-22 19:27:40 +01006569
6570struct lb_env {
6571 struct sched_domain *sd;
6572
Peter Zijlstraddcdf6e2012-02-22 19:27:40 +01006573 struct rq *src_rq;
Prashanth Nageshappa85c1e7d2012-06-19 17:47:34 +05306574 int src_cpu;
Peter Zijlstraddcdf6e2012-02-22 19:27:40 +01006575
6576 int dst_cpu;
6577 struct rq *dst_rq;
6578
Srivatsa Vaddagiri88b8dac2012-06-19 17:43:15 +05306579 struct cpumask *dst_grpmask;
6580 int new_dst_cpu;
Peter Zijlstraddcdf6e2012-02-22 19:27:40 +01006581 enum cpu_idle_type idle;
Peter Zijlstrabd939f42012-05-02 14:20:37 +02006582 long imbalance;
Michael Wangb94031302012-07-12 16:10:13 +08006583 /* The set of CPUs under consideration for load-balancing */
6584 struct cpumask *cpus;
6585
Peter Zijlstraddcdf6e2012-02-22 19:27:40 +01006586 unsigned int flags;
Peter Zijlstra367456c2012-02-20 21:49:09 +01006587
6588 unsigned int loop;
6589 unsigned int loop_break;
6590 unsigned int loop_max;
Peter Zijlstra0ec8aa02013-10-07 11:29:33 +01006591
6592 enum fbq_type fbq_type;
Kirill Tkhai163122b2014-08-20 13:48:29 +04006593 struct list_head tasks;
Peter Zijlstraddcdf6e2012-02-22 19:27:40 +01006594};
6595
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01006596/*
Peter Zijlstra029632f2011-10-25 10:00:11 +02006597 * Is this task likely cache-hot:
6598 */
Hillf Danton5d5e2b12014-06-10 10:58:43 +02006599static int task_hot(struct task_struct *p, struct lb_env *env)
Peter Zijlstra029632f2011-10-25 10:00:11 +02006600{
6601 s64 delta;
6602
Kirill Tkhaie5673f22014-08-20 13:48:01 +04006603 lockdep_assert_held(&env->src_rq->lock);
6604
Peter Zijlstra029632f2011-10-25 10:00:11 +02006605 if (p->sched_class != &fair_sched_class)
6606 return 0;
6607
6608 if (unlikely(p->policy == SCHED_IDLE))
6609 return 0;
6610
6611 /*
6612 * Buddy candidates are cache hot:
6613 */
Hillf Danton5d5e2b12014-06-10 10:58:43 +02006614 if (sched_feat(CACHE_HOT_BUDDY) && env->dst_rq->nr_running &&
Peter Zijlstra029632f2011-10-25 10:00:11 +02006615 (&p->se == cfs_rq_of(&p->se)->next ||
6616 &p->se == cfs_rq_of(&p->se)->last))
6617 return 1;
6618
6619 if (sysctl_sched_migration_cost == -1)
6620 return 1;
6621 if (sysctl_sched_migration_cost == 0)
6622 return 0;
6623
Hillf Danton5d5e2b12014-06-10 10:58:43 +02006624 delta = rq_clock_task(env->src_rq) - p->se.exec_start;
Peter Zijlstra029632f2011-10-25 10:00:11 +02006625
6626 return delta < (s64)sysctl_sched_migration_cost;
6627}
6628
Mel Gorman3a7053b2013-10-07 11:29:00 +01006629#ifdef CONFIG_NUMA_BALANCING
Rik van Rielc1ceac62015-05-14 22:59:36 -04006630/*
Srikar Dronamraju2a1ed242015-06-16 17:25:59 +05306631 * Returns 1, if task migration degrades locality
6632 * Returns 0, if task migration improves locality i.e migration preferred.
6633 * Returns -1, if task migration is not affected by locality.
Rik van Rielc1ceac62015-05-14 22:59:36 -04006634 */
Srikar Dronamraju2a1ed242015-06-16 17:25:59 +05306635static int migrate_degrades_locality(struct task_struct *p, struct lb_env *env)
Mel Gorman3a7053b2013-10-07 11:29:00 +01006636{
Rik van Rielb1ad0652014-05-15 13:03:06 -04006637 struct numa_group *numa_group = rcu_dereference(p->numa_group);
Rik van Rielc1ceac62015-05-14 22:59:36 -04006638 unsigned long src_faults, dst_faults;
Mel Gorman3a7053b2013-10-07 11:29:00 +01006639 int src_nid, dst_nid;
6640
Srikar Dronamraju2a595722015-08-11 21:54:21 +05306641 if (!static_branch_likely(&sched_numa_balancing))
Srikar Dronamraju2a1ed242015-06-16 17:25:59 +05306642 return -1;
6643
Srikar Dronamrajuc3b9bc52015-08-11 16:30:12 +05306644 if (!p->numa_faults || !(env->sd->flags & SD_NUMA))
Srikar Dronamraju2a1ed242015-06-16 17:25:59 +05306645 return -1;
Mel Gorman7a0f3082013-10-07 11:29:01 +01006646
6647 src_nid = cpu_to_node(env->src_cpu);
6648 dst_nid = cpu_to_node(env->dst_cpu);
6649
Mel Gorman83e1d2c2013-10-07 11:29:27 +01006650 if (src_nid == dst_nid)
Srikar Dronamraju2a1ed242015-06-16 17:25:59 +05306651 return -1;
Mel Gorman7a0f3082013-10-07 11:29:01 +01006652
Srikar Dronamraju2a1ed242015-06-16 17:25:59 +05306653 /* Migrating away from the preferred node is always bad. */
6654 if (src_nid == p->numa_preferred_nid) {
6655 if (env->src_rq->nr_running > env->src_rq->nr_preferred_running)
6656 return 1;
6657 else
6658 return -1;
6659 }
Mel Gorman83e1d2c2013-10-07 11:29:27 +01006660
Rik van Rielc1ceac62015-05-14 22:59:36 -04006661 /* Encourage migration to the preferred node. */
6662 if (dst_nid == p->numa_preferred_nid)
Srikar Dronamraju2a1ed242015-06-16 17:25:59 +05306663 return 0;
Rik van Rielc1ceac62015-05-14 22:59:36 -04006664
Rik van Riel739294f2017-06-23 12:55:27 -04006665 /* Leaving a core idle is often worse than degrading locality. */
6666 if (env->idle != CPU_NOT_IDLE)
6667 return -1;
6668
Rik van Rielc1ceac62015-05-14 22:59:36 -04006669 if (numa_group) {
6670 src_faults = group_faults(p, src_nid);
6671 dst_faults = group_faults(p, dst_nid);
6672 } else {
6673 src_faults = task_faults(p, src_nid);
6674 dst_faults = task_faults(p, dst_nid);
6675 }
6676
6677 return dst_faults < src_faults;
Mel Gorman7a0f3082013-10-07 11:29:01 +01006678}
6679
Mel Gorman3a7053b2013-10-07 11:29:00 +01006680#else
Srikar Dronamraju2a1ed242015-06-16 17:25:59 +05306681static inline int migrate_degrades_locality(struct task_struct *p,
Mel Gorman3a7053b2013-10-07 11:29:00 +01006682 struct lb_env *env)
6683{
Srikar Dronamraju2a1ed242015-06-16 17:25:59 +05306684 return -1;
Mel Gorman7a0f3082013-10-07 11:29:01 +01006685}
Mel Gorman3a7053b2013-10-07 11:29:00 +01006686#endif
6687
Peter Zijlstra029632f2011-10-25 10:00:11 +02006688/*
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01006689 * can_migrate_task - may task p from runqueue rq be migrated to this_cpu?
6690 */
6691static
Peter Zijlstra8e45cb52012-02-22 12:47:19 +01006692int can_migrate_task(struct task_struct *p, struct lb_env *env)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01006693{
Srikar Dronamraju2a1ed242015-06-16 17:25:59 +05306694 int tsk_cache_hot;
Kirill Tkhaie5673f22014-08-20 13:48:01 +04006695
6696 lockdep_assert_held(&env->src_rq->lock);
6697
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01006698 /*
6699 * We do not migrate tasks that are:
Joonsoo Kimd3198082013-04-23 17:27:40 +09006700 * 1) throttled_lb_pair, or
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01006701 * 2) cannot be migrated to this CPU due to cpus_allowed, or
Joonsoo Kimd3198082013-04-23 17:27:40 +09006702 * 3) running (obviously), or
6703 * 4) are cache-hot on their current CPU.
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01006704 */
Joonsoo Kimd3198082013-04-23 17:27:40 +09006705 if (throttled_lb_pair(task_group(p), env->src_cpu, env->dst_cpu))
6706 return 0;
6707
Ingo Molnar0c98d342017-02-05 15:38:10 +01006708 if (!cpumask_test_cpu(env->dst_cpu, &p->cpus_allowed)) {
Joonsoo Kime02e60c2013-04-23 17:27:42 +09006709 int cpu;
Srivatsa Vaddagiri88b8dac2012-06-19 17:43:15 +05306710
Josh Poimboeufae928822016-06-17 12:43:24 -05006711 schedstat_inc(p->se.statistics.nr_failed_migrations_affine);
Srivatsa Vaddagiri88b8dac2012-06-19 17:43:15 +05306712
Peter Zijlstra62633222013-08-19 12:41:09 +02006713 env->flags |= LBF_SOME_PINNED;
6714
Srivatsa Vaddagiri88b8dac2012-06-19 17:43:15 +05306715 /*
6716 * Remember if this task can be migrated to any other cpu in
6717 * our sched_group. We may want to revisit it if we couldn't
6718 * meet load balance goals by pulling other tasks on src_cpu.
6719 *
Jeffrey Hugo65a44332017-06-07 13:18:57 -06006720 * Avoid computing new_dst_cpu for NEWLY_IDLE or if we have
6721 * already computed one in current iteration.
Srivatsa Vaddagiri88b8dac2012-06-19 17:43:15 +05306722 */
Jeffrey Hugo65a44332017-06-07 13:18:57 -06006723 if (env->idle == CPU_NEWLY_IDLE || (env->flags & LBF_DST_PINNED))
Srivatsa Vaddagiri88b8dac2012-06-19 17:43:15 +05306724 return 0;
6725
Joonsoo Kime02e60c2013-04-23 17:27:42 +09006726 /* Prevent to re-select dst_cpu via env's cpus */
6727 for_each_cpu_and(cpu, env->dst_grpmask, env->cpus) {
Ingo Molnar0c98d342017-02-05 15:38:10 +01006728 if (cpumask_test_cpu(cpu, &p->cpus_allowed)) {
Peter Zijlstra62633222013-08-19 12:41:09 +02006729 env->flags |= LBF_DST_PINNED;
Joonsoo Kime02e60c2013-04-23 17:27:42 +09006730 env->new_dst_cpu = cpu;
6731 break;
6732 }
Srivatsa Vaddagiri88b8dac2012-06-19 17:43:15 +05306733 }
Joonsoo Kime02e60c2013-04-23 17:27:42 +09006734
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01006735 return 0;
6736 }
Srivatsa Vaddagiri88b8dac2012-06-19 17:43:15 +05306737
6738 /* Record that we found atleast one task that could run on dst_cpu */
Peter Zijlstra8e45cb52012-02-22 12:47:19 +01006739 env->flags &= ~LBF_ALL_PINNED;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01006740
Peter Zijlstraddcdf6e2012-02-22 19:27:40 +01006741 if (task_running(env->src_rq, p)) {
Josh Poimboeufae928822016-06-17 12:43:24 -05006742 schedstat_inc(p->se.statistics.nr_failed_migrations_running);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01006743 return 0;
6744 }
6745
6746 /*
6747 * Aggressive migration if:
Mel Gorman3a7053b2013-10-07 11:29:00 +01006748 * 1) destination numa is preferred
6749 * 2) task is cache cold, or
6750 * 3) too many balance attempts have failed.
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01006751 */
Srikar Dronamraju2a1ed242015-06-16 17:25:59 +05306752 tsk_cache_hot = migrate_degrades_locality(p, env);
6753 if (tsk_cache_hot == -1)
6754 tsk_cache_hot = task_hot(p, env);
Mel Gorman3a7053b2013-10-07 11:29:00 +01006755
Srikar Dronamraju2a1ed242015-06-16 17:25:59 +05306756 if (tsk_cache_hot <= 0 ||
Kirill Tkhai7a96c232014-09-22 22:36:12 +04006757 env->sd->nr_balance_failed > env->sd->cache_nice_tries) {
Srikar Dronamraju2a1ed242015-06-16 17:25:59 +05306758 if (tsk_cache_hot == 1) {
Josh Poimboeufae928822016-06-17 12:43:24 -05006759 schedstat_inc(env->sd->lb_hot_gained[env->idle]);
6760 schedstat_inc(p->se.statistics.nr_forced_migrations);
Mel Gorman3a7053b2013-10-07 11:29:00 +01006761 }
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01006762 return 1;
6763 }
6764
Josh Poimboeufae928822016-06-17 12:43:24 -05006765 schedstat_inc(p->se.statistics.nr_failed_migrations_hot);
Zhang Hang4e2dcb72013-04-10 14:04:55 +08006766 return 0;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01006767}
6768
Peter Zijlstra897c3952009-12-17 17:45:42 +01006769/*
Kirill Tkhai163122b2014-08-20 13:48:29 +04006770 * detach_task() -- detach the task for the migration specified in env
Peter Zijlstra897c3952009-12-17 17:45:42 +01006771 */
Kirill Tkhai163122b2014-08-20 13:48:29 +04006772static void detach_task(struct task_struct *p, struct lb_env *env)
6773{
6774 lockdep_assert_held(&env->src_rq->lock);
6775
Kirill Tkhai163122b2014-08-20 13:48:29 +04006776 p->on_rq = TASK_ON_RQ_MIGRATING;
Peter Zijlstra5704ac02017-02-21 17:15:21 +01006777 deactivate_task(env->src_rq, p, DEQUEUE_NOCLOCK);
Kirill Tkhai163122b2014-08-20 13:48:29 +04006778 set_task_cpu(p, env->dst_cpu);
6779}
6780
6781/*
Kirill Tkhaie5673f22014-08-20 13:48:01 +04006782 * detach_one_task() -- tries to dequeue exactly one task from env->src_rq, as
Peter Zijlstra897c3952009-12-17 17:45:42 +01006783 * part of active balancing operations within "domain".
Peter Zijlstra897c3952009-12-17 17:45:42 +01006784 *
Kirill Tkhaie5673f22014-08-20 13:48:01 +04006785 * Returns a task if successful and NULL otherwise.
Peter Zijlstra897c3952009-12-17 17:45:42 +01006786 */
Kirill Tkhaie5673f22014-08-20 13:48:01 +04006787static struct task_struct *detach_one_task(struct lb_env *env)
Peter Zijlstra897c3952009-12-17 17:45:42 +01006788{
6789 struct task_struct *p, *n;
Peter Zijlstra897c3952009-12-17 17:45:42 +01006790
Kirill Tkhaie5673f22014-08-20 13:48:01 +04006791 lockdep_assert_held(&env->src_rq->lock);
6792
Peter Zijlstra367456c2012-02-20 21:49:09 +01006793 list_for_each_entry_safe(p, n, &env->src_rq->cfs_tasks, se.group_node) {
Peter Zijlstra367456c2012-02-20 21:49:09 +01006794 if (!can_migrate_task(p, env))
6795 continue;
Peter Zijlstra897c3952009-12-17 17:45:42 +01006796
Kirill Tkhai163122b2014-08-20 13:48:29 +04006797 detach_task(p, env);
Kirill Tkhaie5673f22014-08-20 13:48:01 +04006798
Peter Zijlstra367456c2012-02-20 21:49:09 +01006799 /*
Kirill Tkhaie5673f22014-08-20 13:48:01 +04006800 * Right now, this is only the second place where
Kirill Tkhai163122b2014-08-20 13:48:29 +04006801 * lb_gained[env->idle] is updated (other is detach_tasks)
Kirill Tkhaie5673f22014-08-20 13:48:01 +04006802 * so we can safely collect stats here rather than
Kirill Tkhai163122b2014-08-20 13:48:29 +04006803 * inside detach_tasks().
Peter Zijlstra367456c2012-02-20 21:49:09 +01006804 */
Josh Poimboeufae928822016-06-17 12:43:24 -05006805 schedstat_inc(env->sd->lb_gained[env->idle]);
Kirill Tkhaie5673f22014-08-20 13:48:01 +04006806 return p;
Peter Zijlstra897c3952009-12-17 17:45:42 +01006807 }
Kirill Tkhaie5673f22014-08-20 13:48:01 +04006808 return NULL;
Peter Zijlstra897c3952009-12-17 17:45:42 +01006809}
6810
Peter Zijlstraeb953082012-04-17 13:38:40 +02006811static const unsigned int sched_nr_migrate_break = 32;
6812
Peter Zijlstra5d6523e2012-03-10 00:07:36 +01006813/*
Kirill Tkhai163122b2014-08-20 13:48:29 +04006814 * detach_tasks() -- tries to detach up to imbalance weighted load from
6815 * busiest_rq, as part of a balancing operation within domain "sd".
Peter Zijlstra5d6523e2012-03-10 00:07:36 +01006816 *
Kirill Tkhai163122b2014-08-20 13:48:29 +04006817 * Returns number of detached tasks if successful and 0 otherwise.
Peter Zijlstra5d6523e2012-03-10 00:07:36 +01006818 */
Kirill Tkhai163122b2014-08-20 13:48:29 +04006819static int detach_tasks(struct lb_env *env)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01006820{
Peter Zijlstra5d6523e2012-03-10 00:07:36 +01006821 struct list_head *tasks = &env->src_rq->cfs_tasks;
6822 struct task_struct *p;
Peter Zijlstra367456c2012-02-20 21:49:09 +01006823 unsigned long load;
Kirill Tkhai163122b2014-08-20 13:48:29 +04006824 int detached = 0;
6825
6826 lockdep_assert_held(&env->src_rq->lock);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01006827
Peter Zijlstrabd939f42012-05-02 14:20:37 +02006828 if (env->imbalance <= 0)
Peter Zijlstra5d6523e2012-03-10 00:07:36 +01006829 return 0;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01006830
Peter Zijlstra5d6523e2012-03-10 00:07:36 +01006831 while (!list_empty(tasks)) {
Yuyang Du985d3a42015-07-06 06:11:51 +08006832 /*
6833 * We don't want to steal all, otherwise we may be treated likewise,
6834 * which could at worst lead to a livelock crash.
6835 */
6836 if (env->idle != CPU_NOT_IDLE && env->src_rq->nr_running <= 1)
6837 break;
6838
Peter Zijlstra5d6523e2012-03-10 00:07:36 +01006839 p = list_first_entry(tasks, struct task_struct, se.group_node);
6840
Peter Zijlstra367456c2012-02-20 21:49:09 +01006841 env->loop++;
6842 /* We've more or less seen every task there is, call it quits */
Peter Zijlstra5d6523e2012-03-10 00:07:36 +01006843 if (env->loop > env->loop_max)
Peter Zijlstra367456c2012-02-20 21:49:09 +01006844 break;
Peter Zijlstra5d6523e2012-03-10 00:07:36 +01006845
6846 /* take a breather every nr_migrate tasks */
Peter Zijlstra367456c2012-02-20 21:49:09 +01006847 if (env->loop > env->loop_break) {
Peter Zijlstraeb953082012-04-17 13:38:40 +02006848 env->loop_break += sched_nr_migrate_break;
Peter Zijlstra8e45cb52012-02-22 12:47:19 +01006849 env->flags |= LBF_NEED_BREAK;
Peter Zijlstraee00e662009-12-17 17:25:20 +01006850 break;
Peter Zijlstraa195f002011-09-22 15:30:18 +02006851 }
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01006852
Joonsoo Kimd3198082013-04-23 17:27:40 +09006853 if (!can_migrate_task(p, env))
Peter Zijlstra367456c2012-02-20 21:49:09 +01006854 goto next;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01006855
Peter Zijlstra367456c2012-02-20 21:49:09 +01006856 load = task_h_load(p);
Peter Zijlstra5d6523e2012-03-10 00:07:36 +01006857
Peter Zijlstraeb953082012-04-17 13:38:40 +02006858 if (sched_feat(LB_MIN) && load < 16 && !env->sd->nr_balance_failed)
Peter Zijlstra367456c2012-02-20 21:49:09 +01006859 goto next;
6860
Peter Zijlstrabd939f42012-05-02 14:20:37 +02006861 if ((load / 2) > env->imbalance)
Peter Zijlstra367456c2012-02-20 21:49:09 +01006862 goto next;
6863
Kirill Tkhai163122b2014-08-20 13:48:29 +04006864 detach_task(p, env);
6865 list_add(&p->se.group_node, &env->tasks);
6866
6867 detached++;
Peter Zijlstrabd939f42012-05-02 14:20:37 +02006868 env->imbalance -= load;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01006869
6870#ifdef CONFIG_PREEMPT
Peter Zijlstraee00e662009-12-17 17:25:20 +01006871 /*
6872 * NEWIDLE balancing is a source of latency, so preemptible
Kirill Tkhai163122b2014-08-20 13:48:29 +04006873 * kernels will stop after the first task is detached to minimize
Peter Zijlstraee00e662009-12-17 17:25:20 +01006874 * the critical section.
6875 */
Peter Zijlstra5d6523e2012-03-10 00:07:36 +01006876 if (env->idle == CPU_NEWLY_IDLE)
Peter Zijlstraee00e662009-12-17 17:25:20 +01006877 break;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01006878#endif
6879
Peter Zijlstraee00e662009-12-17 17:25:20 +01006880 /*
6881 * We only want to steal up to the prescribed amount of
6882 * weighted load.
6883 */
Peter Zijlstrabd939f42012-05-02 14:20:37 +02006884 if (env->imbalance <= 0)
Peter Zijlstraee00e662009-12-17 17:25:20 +01006885 break;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01006886
Peter Zijlstra367456c2012-02-20 21:49:09 +01006887 continue;
6888next:
Peter Zijlstra5d6523e2012-03-10 00:07:36 +01006889 list_move_tail(&p->se.group_node, tasks);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01006890 }
Peter Zijlstra5d6523e2012-03-10 00:07:36 +01006891
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01006892 /*
Kirill Tkhai163122b2014-08-20 13:48:29 +04006893 * Right now, this is one of only two places we collect this stat
6894 * so we can safely collect detach_one_task() stats here rather
6895 * than inside detach_one_task().
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01006896 */
Josh Poimboeufae928822016-06-17 12:43:24 -05006897 schedstat_add(env->sd->lb_gained[env->idle], detached);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01006898
Kirill Tkhai163122b2014-08-20 13:48:29 +04006899 return detached;
6900}
6901
6902/*
6903 * attach_task() -- attach the task detached by detach_task() to its new rq.
6904 */
6905static void attach_task(struct rq *rq, struct task_struct *p)
6906{
6907 lockdep_assert_held(&rq->lock);
6908
6909 BUG_ON(task_rq(p) != rq);
Peter Zijlstra5704ac02017-02-21 17:15:21 +01006910 activate_task(rq, p, ENQUEUE_NOCLOCK);
Joonwoo Park3ea94de2015-11-12 19:38:54 -08006911 p->on_rq = TASK_ON_RQ_QUEUED;
Kirill Tkhai163122b2014-08-20 13:48:29 +04006912 check_preempt_curr(rq, p, 0);
6913}
6914
6915/*
6916 * attach_one_task() -- attaches the task returned from detach_one_task() to
6917 * its new rq.
6918 */
6919static void attach_one_task(struct rq *rq, struct task_struct *p)
6920{
Peter Zijlstra8a8c69c2016-10-04 16:04:35 +02006921 struct rq_flags rf;
6922
6923 rq_lock(rq, &rf);
Peter Zijlstra5704ac02017-02-21 17:15:21 +01006924 update_rq_clock(rq);
Kirill Tkhai163122b2014-08-20 13:48:29 +04006925 attach_task(rq, p);
Peter Zijlstra8a8c69c2016-10-04 16:04:35 +02006926 rq_unlock(rq, &rf);
Kirill Tkhai163122b2014-08-20 13:48:29 +04006927}
6928
6929/*
6930 * attach_tasks() -- attaches all tasks detached by detach_tasks() to their
6931 * new rq.
6932 */
6933static void attach_tasks(struct lb_env *env)
6934{
6935 struct list_head *tasks = &env->tasks;
6936 struct task_struct *p;
Peter Zijlstra8a8c69c2016-10-04 16:04:35 +02006937 struct rq_flags rf;
Kirill Tkhai163122b2014-08-20 13:48:29 +04006938
Peter Zijlstra8a8c69c2016-10-04 16:04:35 +02006939 rq_lock(env->dst_rq, &rf);
Peter Zijlstra5704ac02017-02-21 17:15:21 +01006940 update_rq_clock(env->dst_rq);
Kirill Tkhai163122b2014-08-20 13:48:29 +04006941
6942 while (!list_empty(tasks)) {
6943 p = list_first_entry(tasks, struct task_struct, se.group_node);
6944 list_del_init(&p->se.group_node);
6945
6946 attach_task(env->dst_rq, p);
6947 }
6948
Peter Zijlstra8a8c69c2016-10-04 16:04:35 +02006949 rq_unlock(env->dst_rq, &rf);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01006950}
6951
Peter Zijlstra230059de2009-12-17 17:47:12 +01006952#ifdef CONFIG_FAIR_GROUP_SCHED
Tejun Heoa9e7f652017-04-25 17:43:50 -07006953
6954static inline bool cfs_rq_is_decayed(struct cfs_rq *cfs_rq)
6955{
6956 if (cfs_rq->load.weight)
6957 return false;
6958
6959 if (cfs_rq->avg.load_sum)
6960 return false;
6961
6962 if (cfs_rq->avg.util_sum)
6963 return false;
6964
6965 if (cfs_rq->runnable_load_sum)
6966 return false;
6967
6968 return true;
6969}
6970
Paul Turner48a16752012-10-04 13:18:31 +02006971static void update_blocked_averages(int cpu)
Peter Zijlstra9e3081c2010-11-15 15:47:02 -08006972{
Peter Zijlstra9e3081c2010-11-15 15:47:02 -08006973 struct rq *rq = cpu_rq(cpu);
Tejun Heoa9e7f652017-04-25 17:43:50 -07006974 struct cfs_rq *cfs_rq, *pos;
Peter Zijlstra8a8c69c2016-10-04 16:04:35 +02006975 struct rq_flags rf;
Peter Zijlstra9e3081c2010-11-15 15:47:02 -08006976
Peter Zijlstra8a8c69c2016-10-04 16:04:35 +02006977 rq_lock_irqsave(rq, &rf);
Paul Turner48a16752012-10-04 13:18:31 +02006978 update_rq_clock(rq);
Yuyang Du9d89c252015-07-15 08:04:37 +08006979
Peter Zijlstra9763b672011-07-13 13:09:25 +02006980 /*
6981 * Iterates the task_group tree in a bottom up fashion, see
6982 * list_add_leaf_cfs_rq() for details.
6983 */
Tejun Heoa9e7f652017-04-25 17:43:50 -07006984 for_each_leaf_cfs_rq_safe(rq, cfs_rq, pos) {
Vincent Guittotbc427892017-03-17 14:47:22 +01006985 struct sched_entity *se;
6986
Yuyang Du9d89c252015-07-15 08:04:37 +08006987 /* throttled entities do not contribute to load */
6988 if (throttled_hierarchy(cfs_rq))
6989 continue;
Paul Turner48a16752012-10-04 13:18:31 +02006990
Viresh Kumar3a123bb2017-05-24 10:59:56 +05306991 if (update_cfs_rq_load_avg(cfs_rq_clock_task(cfs_rq), cfs_rq))
Yuyang Du9d89c252015-07-15 08:04:37 +08006992 update_tg_load_avg(cfs_rq, 0);
Vincent Guittot4e516072016-11-08 10:53:46 +01006993
Vincent Guittotbc427892017-03-17 14:47:22 +01006994 /* Propagate pending load changes to the parent, if any: */
6995 se = cfs_rq->tg->se[cpu];
6996 if (se && !skip_blocked_update(se))
6997 update_load_avg(se, 0);
Tejun Heoa9e7f652017-04-25 17:43:50 -07006998
6999 /*
7000 * There can be a lot of idle CPU cgroups. Don't let fully
7001 * decayed cfs_rqs linger on the list.
7002 */
7003 if (cfs_rq_is_decayed(cfs_rq))
7004 list_del_leaf_cfs_rq(cfs_rq);
Yuyang Du9d89c252015-07-15 08:04:37 +08007005 }
Peter Zijlstra8a8c69c2016-10-04 16:04:35 +02007006 rq_unlock_irqrestore(rq, &rf);
Peter Zijlstra9e3081c2010-11-15 15:47:02 -08007007}
7008
Peter Zijlstra9763b672011-07-13 13:09:25 +02007009/*
Vladimir Davydov68520792013-07-15 17:49:19 +04007010 * Compute the hierarchical load factor for cfs_rq and all its ascendants.
Peter Zijlstra9763b672011-07-13 13:09:25 +02007011 * This needs to be done in a top-down fashion because the load of a child
7012 * group is a fraction of its parents load.
7013 */
Vladimir Davydov68520792013-07-15 17:49:19 +04007014static void update_cfs_rq_h_load(struct cfs_rq *cfs_rq)
Peter Zijlstra9763b672011-07-13 13:09:25 +02007015{
Vladimir Davydov68520792013-07-15 17:49:19 +04007016 struct rq *rq = rq_of(cfs_rq);
7017 struct sched_entity *se = cfs_rq->tg->se[cpu_of(rq)];
Peter Zijlstraa35b6462012-08-08 21:46:40 +02007018 unsigned long now = jiffies;
Vladimir Davydov68520792013-07-15 17:49:19 +04007019 unsigned long load;
Peter Zijlstraa35b6462012-08-08 21:46:40 +02007020
Vladimir Davydov68520792013-07-15 17:49:19 +04007021 if (cfs_rq->last_h_load_update == now)
Peter Zijlstraa35b6462012-08-08 21:46:40 +02007022 return;
7023
Vladimir Davydov68520792013-07-15 17:49:19 +04007024 cfs_rq->h_load_next = NULL;
7025 for_each_sched_entity(se) {
7026 cfs_rq = cfs_rq_of(se);
7027 cfs_rq->h_load_next = se;
7028 if (cfs_rq->last_h_load_update == now)
7029 break;
7030 }
Peter Zijlstraa35b6462012-08-08 21:46:40 +02007031
Vladimir Davydov68520792013-07-15 17:49:19 +04007032 if (!se) {
Yuyang Du7ea241a2015-07-15 08:04:42 +08007033 cfs_rq->h_load = cfs_rq_load_avg(cfs_rq);
Vladimir Davydov68520792013-07-15 17:49:19 +04007034 cfs_rq->last_h_load_update = now;
7035 }
7036
7037 while ((se = cfs_rq->h_load_next) != NULL) {
7038 load = cfs_rq->h_load;
Yuyang Du7ea241a2015-07-15 08:04:42 +08007039 load = div64_ul(load * se->avg.load_avg,
7040 cfs_rq_load_avg(cfs_rq) + 1);
Vladimir Davydov68520792013-07-15 17:49:19 +04007041 cfs_rq = group_cfs_rq(se);
7042 cfs_rq->h_load = load;
7043 cfs_rq->last_h_load_update = now;
7044 }
Peter Zijlstra9763b672011-07-13 13:09:25 +02007045}
7046
Peter Zijlstra367456c2012-02-20 21:49:09 +01007047static unsigned long task_h_load(struct task_struct *p)
Peter Zijlstra230059de2009-12-17 17:47:12 +01007048{
Peter Zijlstra367456c2012-02-20 21:49:09 +01007049 struct cfs_rq *cfs_rq = task_cfs_rq(p);
Peter Zijlstra230059de2009-12-17 17:47:12 +01007050
Vladimir Davydov68520792013-07-15 17:49:19 +04007051 update_cfs_rq_h_load(cfs_rq);
Yuyang Du9d89c252015-07-15 08:04:37 +08007052 return div64_ul(p->se.avg.load_avg * cfs_rq->h_load,
Yuyang Du7ea241a2015-07-15 08:04:42 +08007053 cfs_rq_load_avg(cfs_rq) + 1);
Peter Zijlstra230059de2009-12-17 17:47:12 +01007054}
7055#else
Paul Turner48a16752012-10-04 13:18:31 +02007056static inline void update_blocked_averages(int cpu)
Peter Zijlstra9e3081c2010-11-15 15:47:02 -08007057{
Vincent Guittot6c1d47c2015-07-15 08:04:38 +08007058 struct rq *rq = cpu_rq(cpu);
7059 struct cfs_rq *cfs_rq = &rq->cfs;
Peter Zijlstra8a8c69c2016-10-04 16:04:35 +02007060 struct rq_flags rf;
Vincent Guittot6c1d47c2015-07-15 08:04:38 +08007061
Peter Zijlstra8a8c69c2016-10-04 16:04:35 +02007062 rq_lock_irqsave(rq, &rf);
Vincent Guittot6c1d47c2015-07-15 08:04:38 +08007063 update_rq_clock(rq);
Viresh Kumar3a123bb2017-05-24 10:59:56 +05307064 update_cfs_rq_load_avg(cfs_rq_clock_task(cfs_rq), cfs_rq);
Peter Zijlstra8a8c69c2016-10-04 16:04:35 +02007065 rq_unlock_irqrestore(rq, &rf);
Peter Zijlstra9e3081c2010-11-15 15:47:02 -08007066}
7067
Peter Zijlstra367456c2012-02-20 21:49:09 +01007068static unsigned long task_h_load(struct task_struct *p)
7069{
Yuyang Du9d89c252015-07-15 08:04:37 +08007070 return p->se.avg.load_avg;
Peter Zijlstra230059de2009-12-17 17:47:12 +01007071}
7072#endif
7073
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007074/********** Helpers for find_busiest_group ************************/
Rik van Rielcaeb1782014-07-28 14:16:28 -04007075
7076enum group_type {
7077 group_other = 0,
7078 group_imbalanced,
7079 group_overloaded,
7080};
7081
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007082/*
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007083 * sg_lb_stats - stats of a sched_group required for load_balancing
7084 */
7085struct sg_lb_stats {
7086 unsigned long avg_load; /*Avg load across the CPUs of the group */
7087 unsigned long group_load; /* Total load over the CPUs of the group */
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007088 unsigned long sum_weighted_load; /* Weighted load of group's tasks */
Joonsoo Kim56cf5152013-08-06 17:36:43 +09007089 unsigned long load_per_task;
Nicolas Pitre63b2ca32014-05-26 18:19:37 -04007090 unsigned long group_capacity;
Dietmar Eggemann9e91d612015-08-14 17:23:12 +01007091 unsigned long group_util; /* Total utilization of the group */
Peter Zijlstra147c5fc2013-08-19 15:22:57 +02007092 unsigned int sum_nr_running; /* Nr tasks running in the group */
Peter Zijlstra147c5fc2013-08-19 15:22:57 +02007093 unsigned int idle_cpus;
7094 unsigned int group_weight;
Rik van Rielcaeb1782014-07-28 14:16:28 -04007095 enum group_type group_type;
Vincent Guittotea678212015-02-27 16:54:11 +01007096 int group_no_capacity;
Peter Zijlstra0ec8aa02013-10-07 11:29:33 +01007097#ifdef CONFIG_NUMA_BALANCING
7098 unsigned int nr_numa_running;
7099 unsigned int nr_preferred_running;
7100#endif
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007101};
7102
Joonsoo Kim56cf5152013-08-06 17:36:43 +09007103/*
7104 * sd_lb_stats - Structure to store the statistics of a sched_domain
7105 * during load balancing.
7106 */
7107struct sd_lb_stats {
7108 struct sched_group *busiest; /* Busiest group in this sd */
7109 struct sched_group *local; /* Local group in this sd */
Peter Zijlstra90001d62017-07-31 17:50:05 +02007110 unsigned long total_running;
Joonsoo Kim56cf5152013-08-06 17:36:43 +09007111 unsigned long total_load; /* Total load of all groups in sd */
Nicolas Pitre63b2ca32014-05-26 18:19:37 -04007112 unsigned long total_capacity; /* Total capacity of all groups in sd */
Joonsoo Kim56cf5152013-08-06 17:36:43 +09007113 unsigned long avg_load; /* Average load across all groups in sd */
7114
Joonsoo Kim56cf5152013-08-06 17:36:43 +09007115 struct sg_lb_stats busiest_stat;/* Statistics of the busiest group */
Peter Zijlstra147c5fc2013-08-19 15:22:57 +02007116 struct sg_lb_stats local_stat; /* Statistics of the local group */
Joonsoo Kim56cf5152013-08-06 17:36:43 +09007117};
7118
Peter Zijlstra147c5fc2013-08-19 15:22:57 +02007119static inline void init_sd_lb_stats(struct sd_lb_stats *sds)
7120{
7121 /*
7122 * Skimp on the clearing to avoid duplicate work. We can avoid clearing
7123 * local_stat because update_sg_lb_stats() does a full clear/assignment.
7124 * We must however clear busiest_stat::avg_load because
7125 * update_sd_pick_busiest() reads this before assignment.
7126 */
7127 *sds = (struct sd_lb_stats){
7128 .busiest = NULL,
7129 .local = NULL,
Peter Zijlstra90001d62017-07-31 17:50:05 +02007130 .total_running = 0UL,
Peter Zijlstra147c5fc2013-08-19 15:22:57 +02007131 .total_load = 0UL,
Nicolas Pitre63b2ca32014-05-26 18:19:37 -04007132 .total_capacity = 0UL,
Peter Zijlstra147c5fc2013-08-19 15:22:57 +02007133 .busiest_stat = {
7134 .avg_load = 0UL,
Rik van Rielcaeb1782014-07-28 14:16:28 -04007135 .sum_nr_running = 0,
7136 .group_type = group_other,
Peter Zijlstra147c5fc2013-08-19 15:22:57 +02007137 },
7138 };
7139}
7140
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007141/**
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007142 * get_sd_load_idx - Obtain the load index for a given sched domain.
7143 * @sd: The sched_domain whose load_idx is to be obtained.
Kamalesh Babulaled1b7732013-10-13 23:06:15 +05307144 * @idle: The idle status of the CPU for whose sd load_idx is obtained.
Yacine Belkadie69f6182013-07-12 20:45:47 +02007145 *
7146 * Return: The load index.
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007147 */
7148static inline int get_sd_load_idx(struct sched_domain *sd,
7149 enum cpu_idle_type idle)
7150{
7151 int load_idx;
7152
7153 switch (idle) {
7154 case CPU_NOT_IDLE:
7155 load_idx = sd->busy_idx;
7156 break;
7157
7158 case CPU_NEWLY_IDLE:
7159 load_idx = sd->newidle_idx;
7160 break;
7161 default:
7162 load_idx = sd->idle_idx;
7163 break;
7164 }
7165
7166 return load_idx;
7167}
7168
Nicolas Pitreced549f2014-05-26 18:19:38 -04007169static unsigned long scale_rt_capacity(int cpu)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007170{
7171 struct rq *rq = cpu_rq(cpu);
Vincent Guittotb5b48602015-02-27 16:54:08 +01007172 u64 total, used, age_stamp, avg;
Peter Zijlstracadefd32014-02-27 10:40:35 +01007173 s64 delta;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007174
Peter Zijlstrab654f7d2012-05-22 14:04:28 +02007175 /*
7176 * Since we're reading these variables without serialization make sure
7177 * we read them once before doing sanity checks on them.
7178 */
Jason Low316c1608d2015-04-28 13:00:20 -07007179 age_stamp = READ_ONCE(rq->age_stamp);
7180 avg = READ_ONCE(rq->rt_avg);
Peter Zijlstracebde6d2015-01-05 11:18:10 +01007181 delta = __rq_clock_broken(rq) - age_stamp;
Venkatesh Pallipadiaa483802010-10-04 17:03:22 -07007182
Peter Zijlstracadefd32014-02-27 10:40:35 +01007183 if (unlikely(delta < 0))
7184 delta = 0;
7185
7186 total = sched_avg_period() + delta;
Peter Zijlstrab654f7d2012-05-22 14:04:28 +02007187
Vincent Guittotb5b48602015-02-27 16:54:08 +01007188 used = div_u64(avg, total);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007189
Vincent Guittotb5b48602015-02-27 16:54:08 +01007190 if (likely(used < SCHED_CAPACITY_SCALE))
7191 return SCHED_CAPACITY_SCALE - used;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007192
Vincent Guittotb5b48602015-02-27 16:54:08 +01007193 return 1;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007194}
7195
Nicolas Pitreced549f2014-05-26 18:19:38 -04007196static void update_cpu_capacity(struct sched_domain *sd, int cpu)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007197{
Morten Rasmussen8cd56012015-08-14 17:23:10 +01007198 unsigned long capacity = arch_scale_cpu_capacity(sd, cpu);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007199 struct sched_group *sdg = sd->groups;
7200
Vincent Guittotca6d75e2015-02-27 16:54:09 +01007201 cpu_rq(cpu)->cpu_capacity_orig = capacity;
Srivatsa Vaddagiri9d5efe02010-06-08 14:57:02 +10007202
Nicolas Pitreced549f2014-05-26 18:19:38 -04007203 capacity *= scale_rt_capacity(cpu);
Nicolas Pitreca8ce3d2014-05-26 18:19:39 -04007204 capacity >>= SCHED_CAPACITY_SHIFT;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007205
Nicolas Pitreced549f2014-05-26 18:19:38 -04007206 if (!capacity)
7207 capacity = 1;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007208
Nicolas Pitreced549f2014-05-26 18:19:38 -04007209 cpu_rq(cpu)->cpu_capacity = capacity;
7210 sdg->sgc->capacity = capacity;
Morten Rasmussenbf475ce2016-10-14 14:41:09 +01007211 sdg->sgc->min_capacity = capacity;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007212}
7213
Nicolas Pitre63b2ca32014-05-26 18:19:37 -04007214void update_group_capacity(struct sched_domain *sd, int cpu)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007215{
7216 struct sched_domain *child = sd->child;
7217 struct sched_group *group, *sdg = sd->groups;
Morten Rasmussenbf475ce2016-10-14 14:41:09 +01007218 unsigned long capacity, min_capacity;
Vincent Guittot4ec44122011-12-12 20:21:08 +01007219 unsigned long interval;
7220
7221 interval = msecs_to_jiffies(sd->balance_interval);
7222 interval = clamp(interval, 1UL, max_load_balance_interval);
Nicolas Pitre63b2ca32014-05-26 18:19:37 -04007223 sdg->sgc->next_update = jiffies + interval;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007224
7225 if (!child) {
Nicolas Pitreced549f2014-05-26 18:19:38 -04007226 update_cpu_capacity(sd, cpu);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007227 return;
7228 }
7229
Vincent Guittotdc7ff762015-03-03 11:35:03 +01007230 capacity = 0;
Morten Rasmussenbf475ce2016-10-14 14:41:09 +01007231 min_capacity = ULONG_MAX;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007232
Peter Zijlstra74a5ce22012-05-23 18:00:43 +02007233 if (child->flags & SD_OVERLAP) {
7234 /*
7235 * SD_OVERLAP domains cannot assume that child groups
7236 * span the current group.
7237 */
7238
Peter Zijlstraae4df9d2017-05-01 11:03:12 +02007239 for_each_cpu(cpu, sched_group_span(sdg)) {
Nicolas Pitre63b2ca32014-05-26 18:19:37 -04007240 struct sched_group_capacity *sgc;
Srikar Dronamraju9abf24d2013-11-12 22:11:26 +05307241 struct rq *rq = cpu_rq(cpu);
Peter Zijlstra863bffc2013-08-28 11:44:39 +02007242
Srikar Dronamraju9abf24d2013-11-12 22:11:26 +05307243 /*
Nicolas Pitre63b2ca32014-05-26 18:19:37 -04007244 * build_sched_domains() -> init_sched_groups_capacity()
Srikar Dronamraju9abf24d2013-11-12 22:11:26 +05307245 * gets here before we've attached the domains to the
7246 * runqueues.
7247 *
Nicolas Pitreced549f2014-05-26 18:19:38 -04007248 * Use capacity_of(), which is set irrespective of domains
7249 * in update_cpu_capacity().
Srikar Dronamraju9abf24d2013-11-12 22:11:26 +05307250 *
Vincent Guittotdc7ff762015-03-03 11:35:03 +01007251 * This avoids capacity from being 0 and
Srikar Dronamraju9abf24d2013-11-12 22:11:26 +05307252 * causing divide-by-zero issues on boot.
Srikar Dronamraju9abf24d2013-11-12 22:11:26 +05307253 */
7254 if (unlikely(!rq->sd)) {
Nicolas Pitreced549f2014-05-26 18:19:38 -04007255 capacity += capacity_of(cpu);
Morten Rasmussenbf475ce2016-10-14 14:41:09 +01007256 } else {
7257 sgc = rq->sd->groups->sgc;
7258 capacity += sgc->capacity;
Srikar Dronamraju9abf24d2013-11-12 22:11:26 +05307259 }
7260
Morten Rasmussenbf475ce2016-10-14 14:41:09 +01007261 min_capacity = min(capacity, min_capacity);
Peter Zijlstra863bffc2013-08-28 11:44:39 +02007262 }
Peter Zijlstra74a5ce22012-05-23 18:00:43 +02007263 } else {
7264 /*
7265 * !SD_OVERLAP domains can assume that child groups
7266 * span the current group.
Byungchul Park97a71422015-07-05 18:33:48 +09007267 */
Peter Zijlstra74a5ce22012-05-23 18:00:43 +02007268
7269 group = child->groups;
7270 do {
Morten Rasmussenbf475ce2016-10-14 14:41:09 +01007271 struct sched_group_capacity *sgc = group->sgc;
7272
7273 capacity += sgc->capacity;
7274 min_capacity = min(sgc->min_capacity, min_capacity);
Peter Zijlstra74a5ce22012-05-23 18:00:43 +02007275 group = group->next;
7276 } while (group != child->groups);
7277 }
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007278
Nicolas Pitre63b2ca32014-05-26 18:19:37 -04007279 sdg->sgc->capacity = capacity;
Morten Rasmussenbf475ce2016-10-14 14:41:09 +01007280 sdg->sgc->min_capacity = min_capacity;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007281}
7282
Srivatsa Vaddagiri9d5efe02010-06-08 14:57:02 +10007283/*
Vincent Guittotea678212015-02-27 16:54:11 +01007284 * Check whether the capacity of the rq has been noticeably reduced by side
7285 * activity. The imbalance_pct is used for the threshold.
7286 * Return true is the capacity is reduced
Srivatsa Vaddagiri9d5efe02010-06-08 14:57:02 +10007287 */
7288static inline int
Vincent Guittotea678212015-02-27 16:54:11 +01007289check_cpu_capacity(struct rq *rq, struct sched_domain *sd)
Srivatsa Vaddagiri9d5efe02010-06-08 14:57:02 +10007290{
Vincent Guittotea678212015-02-27 16:54:11 +01007291 return ((rq->cpu_capacity * sd->imbalance_pct) <
7292 (rq->cpu_capacity_orig * 100));
Srivatsa Vaddagiri9d5efe02010-06-08 14:57:02 +10007293}
7294
Peter Zijlstra30ce5da2013-08-15 20:29:29 +02007295/*
7296 * Group imbalance indicates (and tries to solve) the problem where balancing
Ingo Molnar0c98d342017-02-05 15:38:10 +01007297 * groups is inadequate due to ->cpus_allowed constraints.
Peter Zijlstra30ce5da2013-08-15 20:29:29 +02007298 *
7299 * Imagine a situation of two groups of 4 cpus each and 4 tasks each with a
7300 * cpumask covering 1 cpu of the first group and 3 cpus of the second group.
7301 * Something like:
7302 *
Ingo Molnar2b4d5b22016-11-23 07:37:00 +01007303 * { 0 1 2 3 } { 4 5 6 7 }
7304 * * * * *
Peter Zijlstra30ce5da2013-08-15 20:29:29 +02007305 *
7306 * If we were to balance group-wise we'd place two tasks in the first group and
7307 * two tasks in the second group. Clearly this is undesired as it will overload
7308 * cpu 3 and leave one of the cpus in the second group unused.
7309 *
7310 * The current solution to this issue is detecting the skew in the first group
Peter Zijlstra62633222013-08-19 12:41:09 +02007311 * by noticing the lower domain failed to reach balance and had difficulty
7312 * moving tasks due to affinity constraints.
Peter Zijlstra30ce5da2013-08-15 20:29:29 +02007313 *
7314 * When this is so detected; this group becomes a candidate for busiest; see
Kamalesh Babulaled1b7732013-10-13 23:06:15 +05307315 * update_sd_pick_busiest(). And calculate_imbalance() and
Peter Zijlstra62633222013-08-19 12:41:09 +02007316 * find_busiest_group() avoid some of the usual balance conditions to allow it
Peter Zijlstra30ce5da2013-08-15 20:29:29 +02007317 * to create an effective group imbalance.
7318 *
7319 * This is a somewhat tricky proposition since the next run might not find the
7320 * group imbalance and decide the groups need to be balanced again. A most
7321 * subtle and fragile situation.
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007322 */
Peter Zijlstra30ce5da2013-08-15 20:29:29 +02007323
Peter Zijlstra62633222013-08-19 12:41:09 +02007324static inline int sg_imbalanced(struct sched_group *group)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007325{
Nicolas Pitre63b2ca32014-05-26 18:19:37 -04007326 return group->sgc->imbalance;
Peter Zijlstra30ce5da2013-08-15 20:29:29 +02007327}
7328
Peter Zijlstrab37d9312013-08-28 11:50:34 +02007329/*
Vincent Guittotea678212015-02-27 16:54:11 +01007330 * group_has_capacity returns true if the group has spare capacity that could
7331 * be used by some tasks.
7332 * We consider that a group has spare capacity if the * number of task is
Dietmar Eggemann9e91d612015-08-14 17:23:12 +01007333 * smaller than the number of CPUs or if the utilization is lower than the
7334 * available capacity for CFS tasks.
Vincent Guittotea678212015-02-27 16:54:11 +01007335 * For the latter, we use a threshold to stabilize the state, to take into
7336 * account the variance of the tasks' load and to return true if the available
7337 * capacity in meaningful for the load balancer.
7338 * As an example, an available capacity of 1% can appear but it doesn't make
7339 * any benefit for the load balance.
Peter Zijlstrab37d9312013-08-28 11:50:34 +02007340 */
Vincent Guittotea678212015-02-27 16:54:11 +01007341static inline bool
7342group_has_capacity(struct lb_env *env, struct sg_lb_stats *sgs)
Peter Zijlstrab37d9312013-08-28 11:50:34 +02007343{
Vincent Guittotea678212015-02-27 16:54:11 +01007344 if (sgs->sum_nr_running < sgs->group_weight)
7345 return true;
Peter Zijlstrab37d9312013-08-28 11:50:34 +02007346
Vincent Guittotea678212015-02-27 16:54:11 +01007347 if ((sgs->group_capacity * 100) >
Dietmar Eggemann9e91d612015-08-14 17:23:12 +01007348 (sgs->group_util * env->sd->imbalance_pct))
Vincent Guittotea678212015-02-27 16:54:11 +01007349 return true;
Peter Zijlstrab37d9312013-08-28 11:50:34 +02007350
Vincent Guittotea678212015-02-27 16:54:11 +01007351 return false;
Peter Zijlstrab37d9312013-08-28 11:50:34 +02007352}
7353
Vincent Guittotea678212015-02-27 16:54:11 +01007354/*
7355 * group_is_overloaded returns true if the group has more tasks than it can
7356 * handle.
7357 * group_is_overloaded is not equals to !group_has_capacity because a group
7358 * with the exact right number of tasks, has no more spare capacity but is not
7359 * overloaded so both group_has_capacity and group_is_overloaded return
7360 * false.
7361 */
7362static inline bool
7363group_is_overloaded(struct lb_env *env, struct sg_lb_stats *sgs)
Rik van Rielcaeb1782014-07-28 14:16:28 -04007364{
Vincent Guittotea678212015-02-27 16:54:11 +01007365 if (sgs->sum_nr_running <= sgs->group_weight)
7366 return false;
7367
7368 if ((sgs->group_capacity * 100) <
Dietmar Eggemann9e91d612015-08-14 17:23:12 +01007369 (sgs->group_util * env->sd->imbalance_pct))
Vincent Guittotea678212015-02-27 16:54:11 +01007370 return true;
7371
7372 return false;
7373}
7374
Morten Rasmussen9e0994c2016-10-14 14:41:10 +01007375/*
7376 * group_smaller_cpu_capacity: Returns true if sched_group sg has smaller
7377 * per-CPU capacity than sched_group ref.
7378 */
7379static inline bool
7380group_smaller_cpu_capacity(struct sched_group *sg, struct sched_group *ref)
7381{
7382 return sg->sgc->min_capacity * capacity_margin <
7383 ref->sgc->min_capacity * 1024;
7384}
7385
Leo Yan79a89f92015-09-15 18:56:45 +08007386static inline enum
7387group_type group_classify(struct sched_group *group,
7388 struct sg_lb_stats *sgs)
Vincent Guittotea678212015-02-27 16:54:11 +01007389{
7390 if (sgs->group_no_capacity)
Rik van Rielcaeb1782014-07-28 14:16:28 -04007391 return group_overloaded;
7392
7393 if (sg_imbalanced(group))
7394 return group_imbalanced;
7395
7396 return group_other;
7397}
7398
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007399/**
7400 * update_sg_lb_stats - Update sched_group's statistics for load balancing.
7401 * @env: The load balancing environment.
7402 * @group: sched_group whose statistics are to be updated.
7403 * @load_idx: Load index of sched_domain of this_cpu for load calc.
7404 * @local_group: Does group contain this_cpu.
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007405 * @sgs: variable to hold the statistics for this group.
Masanari Iidacd3bd4e2014-07-28 12:38:06 +09007406 * @overload: Indicate more than one runnable task for any CPU.
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007407 */
7408static inline void update_sg_lb_stats(struct lb_env *env,
7409 struct sched_group *group, int load_idx,
Tim Chen4486edd2014-06-23 12:16:49 -07007410 int local_group, struct sg_lb_stats *sgs,
7411 bool *overload)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007412{
Peter Zijlstra30ce5da2013-08-15 20:29:29 +02007413 unsigned long load;
Waiman Longa426f992015-11-25 14:09:38 -05007414 int i, nr_running;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007415
Peter Zijlstrab72ff132013-08-28 10:32:32 +02007416 memset(sgs, 0, sizeof(*sgs));
7417
Peter Zijlstraae4df9d2017-05-01 11:03:12 +02007418 for_each_cpu_and(i, sched_group_span(group), env->cpus) {
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007419 struct rq *rq = cpu_rq(i);
7420
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007421 /* Bias balancing toward cpus of our domain */
Peter Zijlstra62633222013-08-19 12:41:09 +02007422 if (local_group)
Peter Zijlstra04f733b2012-05-11 00:12:02 +02007423 load = target_load(i, load_idx);
Peter Zijlstra62633222013-08-19 12:41:09 +02007424 else
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007425 load = source_load(i, load_idx);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007426
7427 sgs->group_load += load;
Dietmar Eggemann9e91d612015-08-14 17:23:12 +01007428 sgs->group_util += cpu_util(i);
Vincent Guittot65fdac02014-08-26 13:06:46 +02007429 sgs->sum_nr_running += rq->cfs.h_nr_running;
Tim Chen4486edd2014-06-23 12:16:49 -07007430
Waiman Longa426f992015-11-25 14:09:38 -05007431 nr_running = rq->nr_running;
7432 if (nr_running > 1)
Tim Chen4486edd2014-06-23 12:16:49 -07007433 *overload = true;
7434
Peter Zijlstra0ec8aa02013-10-07 11:29:33 +01007435#ifdef CONFIG_NUMA_BALANCING
7436 sgs->nr_numa_running += rq->nr_numa_running;
7437 sgs->nr_preferred_running += rq->nr_preferred_running;
7438#endif
Viresh Kumarc7132dd2017-05-24 10:59:54 +05307439 sgs->sum_weighted_load += weighted_cpuload(rq);
Waiman Longa426f992015-11-25 14:09:38 -05007440 /*
7441 * No need to call idle_cpu() if nr_running is not 0
7442 */
7443 if (!nr_running && idle_cpu(i))
Suresh Siddhaaae6d3d2010-09-17 15:02:32 -07007444 sgs->idle_cpus++;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007445 }
7446
Nicolas Pitre63b2ca32014-05-26 18:19:37 -04007447 /* Adjust by relative CPU capacity of the group */
7448 sgs->group_capacity = group->sgc->capacity;
Nicolas Pitreca8ce3d2014-05-26 18:19:39 -04007449 sgs->avg_load = (sgs->group_load*SCHED_CAPACITY_SCALE) / sgs->group_capacity;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007450
Suresh Siddhadd5feea2010-02-23 16:13:52 -08007451 if (sgs->sum_nr_running)
Peter Zijlstra38d0f772013-08-15 19:47:56 +02007452 sgs->load_per_task = sgs->sum_weighted_load / sgs->sum_nr_running;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007453
Suresh Siddhaaae6d3d2010-09-17 15:02:32 -07007454 sgs->group_weight = group->group_weight;
Peter Zijlstrab37d9312013-08-28 11:50:34 +02007455
Vincent Guittotea678212015-02-27 16:54:11 +01007456 sgs->group_no_capacity = group_is_overloaded(env, sgs);
Leo Yan79a89f92015-09-15 18:56:45 +08007457 sgs->group_type = group_classify(group, sgs);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007458}
7459
7460/**
Michael Neuling532cb4c2010-06-08 14:57:02 +10007461 * update_sd_pick_busiest - return 1 on busiest group
Randy Dunlapcd968912012-06-08 13:18:33 -07007462 * @env: The load balancing environment.
Michael Neuling532cb4c2010-06-08 14:57:02 +10007463 * @sds: sched_domain statistics
7464 * @sg: sched_group candidate to be checked for being the busiest
Michael Neulingb6b12292010-06-10 12:06:21 +10007465 * @sgs: sched_group statistics
Michael Neuling532cb4c2010-06-08 14:57:02 +10007466 *
7467 * Determine if @sg is a busier group than the previously selected
7468 * busiest group.
Yacine Belkadie69f6182013-07-12 20:45:47 +02007469 *
7470 * Return: %true if @sg is a busier group than the previously selected
7471 * busiest group. %false otherwise.
Michael Neuling532cb4c2010-06-08 14:57:02 +10007472 */
Peter Zijlstrabd939f42012-05-02 14:20:37 +02007473static bool update_sd_pick_busiest(struct lb_env *env,
Michael Neuling532cb4c2010-06-08 14:57:02 +10007474 struct sd_lb_stats *sds,
7475 struct sched_group *sg,
Peter Zijlstrabd939f42012-05-02 14:20:37 +02007476 struct sg_lb_stats *sgs)
Michael Neuling532cb4c2010-06-08 14:57:02 +10007477{
Rik van Rielcaeb1782014-07-28 14:16:28 -04007478 struct sg_lb_stats *busiest = &sds->busiest_stat;
Michael Neuling532cb4c2010-06-08 14:57:02 +10007479
Rik van Rielcaeb1782014-07-28 14:16:28 -04007480 if (sgs->group_type > busiest->group_type)
Michael Neuling532cb4c2010-06-08 14:57:02 +10007481 return true;
7482
Rik van Rielcaeb1782014-07-28 14:16:28 -04007483 if (sgs->group_type < busiest->group_type)
7484 return false;
7485
7486 if (sgs->avg_load <= busiest->avg_load)
7487 return false;
7488
Morten Rasmussen9e0994c2016-10-14 14:41:10 +01007489 if (!(env->sd->flags & SD_ASYM_CPUCAPACITY))
7490 goto asym_packing;
7491
7492 /*
7493 * Candidate sg has no more than one task per CPU and
7494 * has higher per-CPU capacity. Migrating tasks to less
7495 * capable CPUs may harm throughput. Maximize throughput,
7496 * power/energy consequences are not considered.
7497 */
7498 if (sgs->sum_nr_running <= sgs->group_weight &&
7499 group_smaller_cpu_capacity(sds->local, sg))
7500 return false;
7501
7502asym_packing:
Rik van Rielcaeb1782014-07-28 14:16:28 -04007503 /* This is the busiest node in its class. */
7504 if (!(env->sd->flags & SD_ASYM_PACKING))
Michael Neuling532cb4c2010-06-08 14:57:02 +10007505 return true;
7506
Srikar Dronamraju1f621e02016-04-06 18:47:40 +05307507 /* No ASYM_PACKING if target cpu is already busy */
7508 if (env->idle == CPU_NOT_IDLE)
7509 return true;
Michael Neuling532cb4c2010-06-08 14:57:02 +10007510 /*
Tim Chenafe06ef2016-11-22 12:23:53 -08007511 * ASYM_PACKING needs to move all the work to the highest
7512 * prority CPUs in the group, therefore mark all groups
7513 * of lower priority than ourself as busy.
Michael Neuling532cb4c2010-06-08 14:57:02 +10007514 */
Tim Chenafe06ef2016-11-22 12:23:53 -08007515 if (sgs->sum_nr_running &&
7516 sched_asym_prefer(env->dst_cpu, sg->asym_prefer_cpu)) {
Michael Neuling532cb4c2010-06-08 14:57:02 +10007517 if (!sds->busiest)
7518 return true;
7519
Tim Chenafe06ef2016-11-22 12:23:53 -08007520 /* Prefer to move from lowest priority cpu's work */
7521 if (sched_asym_prefer(sds->busiest->asym_prefer_cpu,
7522 sg->asym_prefer_cpu))
Michael Neuling532cb4c2010-06-08 14:57:02 +10007523 return true;
7524 }
7525
7526 return false;
7527}
7528
Peter Zijlstra0ec8aa02013-10-07 11:29:33 +01007529#ifdef CONFIG_NUMA_BALANCING
7530static inline enum fbq_type fbq_classify_group(struct sg_lb_stats *sgs)
7531{
7532 if (sgs->sum_nr_running > sgs->nr_numa_running)
7533 return regular;
7534 if (sgs->sum_nr_running > sgs->nr_preferred_running)
7535 return remote;
7536 return all;
7537}
7538
7539static inline enum fbq_type fbq_classify_rq(struct rq *rq)
7540{
7541 if (rq->nr_running > rq->nr_numa_running)
7542 return regular;
7543 if (rq->nr_running > rq->nr_preferred_running)
7544 return remote;
7545 return all;
7546}
7547#else
7548static inline enum fbq_type fbq_classify_group(struct sg_lb_stats *sgs)
7549{
7550 return all;
7551}
7552
7553static inline enum fbq_type fbq_classify_rq(struct rq *rq)
7554{
7555 return regular;
7556}
7557#endif /* CONFIG_NUMA_BALANCING */
7558
Michael Neuling532cb4c2010-06-08 14:57:02 +10007559/**
Hui Kang461819a2011-10-11 23:00:59 -04007560 * update_sd_lb_stats - Update sched_domain's statistics for load balancing.
Randy Dunlapcd968912012-06-08 13:18:33 -07007561 * @env: The load balancing environment.
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007562 * @sds: variable to hold the statistics for this sched_domain.
7563 */
Peter Zijlstra0ec8aa02013-10-07 11:29:33 +01007564static inline void update_sd_lb_stats(struct lb_env *env, struct sd_lb_stats *sds)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007565{
Peter Zijlstrabd939f42012-05-02 14:20:37 +02007566 struct sched_domain *child = env->sd->child;
7567 struct sched_group *sg = env->sd->groups;
Srikar Dronamraju05b40e02017-03-22 23:27:50 +05307568 struct sg_lb_stats *local = &sds->local_stat;
Joonsoo Kim56cf5152013-08-06 17:36:43 +09007569 struct sg_lb_stats tmp_sgs;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007570 int load_idx, prefer_sibling = 0;
Tim Chen4486edd2014-06-23 12:16:49 -07007571 bool overload = false;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007572
7573 if (child && child->flags & SD_PREFER_SIBLING)
7574 prefer_sibling = 1;
7575
Peter Zijlstrabd939f42012-05-02 14:20:37 +02007576 load_idx = get_sd_load_idx(env->sd, env->idle);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007577
7578 do {
Joonsoo Kim56cf5152013-08-06 17:36:43 +09007579 struct sg_lb_stats *sgs = &tmp_sgs;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007580 int local_group;
7581
Peter Zijlstraae4df9d2017-05-01 11:03:12 +02007582 local_group = cpumask_test_cpu(env->dst_cpu, sched_group_span(sg));
Joonsoo Kim56cf5152013-08-06 17:36:43 +09007583 if (local_group) {
7584 sds->local = sg;
Srikar Dronamraju05b40e02017-03-22 23:27:50 +05307585 sgs = local;
Peter Zijlstrab72ff132013-08-28 10:32:32 +02007586
7587 if (env->idle != CPU_NEWLY_IDLE ||
Nicolas Pitre63b2ca32014-05-26 18:19:37 -04007588 time_after_eq(jiffies, sg->sgc->next_update))
7589 update_group_capacity(env->sd, env->dst_cpu);
Joonsoo Kim56cf5152013-08-06 17:36:43 +09007590 }
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007591
Tim Chen4486edd2014-06-23 12:16:49 -07007592 update_sg_lb_stats(env, sg, load_idx, local_group, sgs,
7593 &overload);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007594
Peter Zijlstrab72ff132013-08-28 10:32:32 +02007595 if (local_group)
7596 goto next_group;
7597
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007598 /*
7599 * In case the child domain prefers tasks go to siblings
Vincent Guittotea678212015-02-27 16:54:11 +01007600 * first, lower the sg capacity so that we'll try
Nikhil Rao75dd3212010-10-15 13:12:30 -07007601 * and move all the excess tasks away. We lower the capacity
7602 * of a group only if the local group has the capacity to fit
Vincent Guittotea678212015-02-27 16:54:11 +01007603 * these excess tasks. The extra check prevents the case where
7604 * you always pull from the heaviest group when it is already
7605 * under-utilized (possible with a large weight task outweighs
7606 * the tasks on the system).
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007607 */
Peter Zijlstrab72ff132013-08-28 10:32:32 +02007608 if (prefer_sibling && sds->local &&
Srikar Dronamraju05b40e02017-03-22 23:27:50 +05307609 group_has_capacity(env, local) &&
7610 (sgs->sum_nr_running > local->sum_nr_running + 1)) {
Vincent Guittotea678212015-02-27 16:54:11 +01007611 sgs->group_no_capacity = 1;
Leo Yan79a89f92015-09-15 18:56:45 +08007612 sgs->group_type = group_classify(sg, sgs);
Wanpeng Licb0b9f22014-11-05 07:44:50 +08007613 }
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007614
Peter Zijlstrab72ff132013-08-28 10:32:32 +02007615 if (update_sd_pick_busiest(env, sds, sg, sgs)) {
Michael Neuling532cb4c2010-06-08 14:57:02 +10007616 sds->busiest = sg;
Joonsoo Kim56cf5152013-08-06 17:36:43 +09007617 sds->busiest_stat = *sgs;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007618 }
7619
Peter Zijlstrab72ff132013-08-28 10:32:32 +02007620next_group:
7621 /* Now, start updating sd_lb_stats */
Peter Zijlstra90001d62017-07-31 17:50:05 +02007622 sds->total_running += sgs->sum_nr_running;
Peter Zijlstrab72ff132013-08-28 10:32:32 +02007623 sds->total_load += sgs->group_load;
Nicolas Pitre63b2ca32014-05-26 18:19:37 -04007624 sds->total_capacity += sgs->group_capacity;
Peter Zijlstrab72ff132013-08-28 10:32:32 +02007625
Michael Neuling532cb4c2010-06-08 14:57:02 +10007626 sg = sg->next;
Peter Zijlstrabd939f42012-05-02 14:20:37 +02007627 } while (sg != env->sd->groups);
Peter Zijlstra0ec8aa02013-10-07 11:29:33 +01007628
7629 if (env->sd->flags & SD_NUMA)
7630 env->fbq_type = fbq_classify_group(&sds->busiest_stat);
Tim Chen4486edd2014-06-23 12:16:49 -07007631
7632 if (!env->sd->parent) {
7633 /* update overload indicator if we are at root domain */
7634 if (env->dst_rq->rd->overload != overload)
7635 env->dst_rq->rd->overload = overload;
7636 }
Michael Neuling532cb4c2010-06-08 14:57:02 +10007637}
7638
Michael Neuling532cb4c2010-06-08 14:57:02 +10007639/**
7640 * check_asym_packing - Check to see if the group is packed into the
Masanari Iida0ba42a52017-03-07 20:48:02 +09007641 * sched domain.
Michael Neuling532cb4c2010-06-08 14:57:02 +10007642 *
7643 * This is primarily intended to used at the sibling level. Some
7644 * cores like POWER7 prefer to use lower numbered SMT threads. In the
7645 * case of POWER7, it can move to lower SMT modes only when higher
7646 * threads are idle. When in lower SMT modes, the threads will
7647 * perform better since they share less core resources. Hence when we
7648 * have idle threads, we want them to be the higher ones.
7649 *
7650 * This packing function is run on idle threads. It checks to see if
7651 * the busiest CPU in this domain (core in the P7 case) has a higher
7652 * CPU number than the packing function is being run on. Here we are
7653 * assuming lower CPU number will be equivalent to lower a SMT thread
7654 * number.
7655 *
Yacine Belkadie69f6182013-07-12 20:45:47 +02007656 * Return: 1 when packing is required and a task should be moved to
Randy Dunlap46123352017-09-10 09:55:05 -07007657 * this CPU. The amount of the imbalance is returned in env->imbalance.
Michael Neulingb6b12292010-06-10 12:06:21 +10007658 *
Randy Dunlapcd968912012-06-08 13:18:33 -07007659 * @env: The load balancing environment.
Michael Neuling532cb4c2010-06-08 14:57:02 +10007660 * @sds: Statistics of the sched_domain which is to be packed
Michael Neuling532cb4c2010-06-08 14:57:02 +10007661 */
Peter Zijlstrabd939f42012-05-02 14:20:37 +02007662static int check_asym_packing(struct lb_env *env, struct sd_lb_stats *sds)
Michael Neuling532cb4c2010-06-08 14:57:02 +10007663{
7664 int busiest_cpu;
7665
Peter Zijlstrabd939f42012-05-02 14:20:37 +02007666 if (!(env->sd->flags & SD_ASYM_PACKING))
Michael Neuling532cb4c2010-06-08 14:57:02 +10007667 return 0;
7668
Srikar Dronamraju1f621e02016-04-06 18:47:40 +05307669 if (env->idle == CPU_NOT_IDLE)
7670 return 0;
7671
Michael Neuling532cb4c2010-06-08 14:57:02 +10007672 if (!sds->busiest)
7673 return 0;
7674
Tim Chenafe06ef2016-11-22 12:23:53 -08007675 busiest_cpu = sds->busiest->asym_prefer_cpu;
7676 if (sched_asym_prefer(busiest_cpu, env->dst_cpu))
Michael Neuling532cb4c2010-06-08 14:57:02 +10007677 return 0;
7678
Peter Zijlstrabd939f42012-05-02 14:20:37 +02007679 env->imbalance = DIV_ROUND_CLOSEST(
Nicolas Pitre63b2ca32014-05-26 18:19:37 -04007680 sds->busiest_stat.avg_load * sds->busiest_stat.group_capacity,
Nicolas Pitreca8ce3d2014-05-26 18:19:39 -04007681 SCHED_CAPACITY_SCALE);
Peter Zijlstrabd939f42012-05-02 14:20:37 +02007682
Michael Neuling532cb4c2010-06-08 14:57:02 +10007683 return 1;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007684}
7685
7686/**
7687 * fix_small_imbalance - Calculate the minor imbalance that exists
7688 * amongst the groups of a sched_domain, during
7689 * load balancing.
Randy Dunlapcd968912012-06-08 13:18:33 -07007690 * @env: The load balancing environment.
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007691 * @sds: Statistics of the sched_domain whose imbalance is to be calculated.
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007692 */
Peter Zijlstrabd939f42012-05-02 14:20:37 +02007693static inline
7694void fix_small_imbalance(struct lb_env *env, struct sd_lb_stats *sds)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007695{
Nicolas Pitre63b2ca32014-05-26 18:19:37 -04007696 unsigned long tmp, capa_now = 0, capa_move = 0;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007697 unsigned int imbn = 2;
Suresh Siddhadd5feea2010-02-23 16:13:52 -08007698 unsigned long scaled_busy_load_per_task;
Joonsoo Kim56cf5152013-08-06 17:36:43 +09007699 struct sg_lb_stats *local, *busiest;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007700
Joonsoo Kim56cf5152013-08-06 17:36:43 +09007701 local = &sds->local_stat;
7702 busiest = &sds->busiest_stat;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007703
Joonsoo Kim56cf5152013-08-06 17:36:43 +09007704 if (!local->sum_nr_running)
7705 local->load_per_task = cpu_avg_load_per_task(env->dst_cpu);
7706 else if (busiest->load_per_task > local->load_per_task)
7707 imbn = 1;
Suresh Siddhadd5feea2010-02-23 16:13:52 -08007708
Joonsoo Kim56cf5152013-08-06 17:36:43 +09007709 scaled_busy_load_per_task =
Nicolas Pitreca8ce3d2014-05-26 18:19:39 -04007710 (busiest->load_per_task * SCHED_CAPACITY_SCALE) /
Nicolas Pitre63b2ca32014-05-26 18:19:37 -04007711 busiest->group_capacity;
Joonsoo Kim56cf5152013-08-06 17:36:43 +09007712
Vladimir Davydov3029ede2013-09-15 17:49:14 +04007713 if (busiest->avg_load + scaled_busy_load_per_task >=
7714 local->avg_load + (scaled_busy_load_per_task * imbn)) {
Joonsoo Kim56cf5152013-08-06 17:36:43 +09007715 env->imbalance = busiest->load_per_task;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007716 return;
7717 }
7718
7719 /*
7720 * OK, we don't have enough imbalance to justify moving tasks,
Nicolas Pitreced549f2014-05-26 18:19:38 -04007721 * however we may be able to increase total CPU capacity used by
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007722 * moving them.
7723 */
7724
Nicolas Pitre63b2ca32014-05-26 18:19:37 -04007725 capa_now += busiest->group_capacity *
Joonsoo Kim56cf5152013-08-06 17:36:43 +09007726 min(busiest->load_per_task, busiest->avg_load);
Nicolas Pitre63b2ca32014-05-26 18:19:37 -04007727 capa_now += local->group_capacity *
Joonsoo Kim56cf5152013-08-06 17:36:43 +09007728 min(local->load_per_task, local->avg_load);
Nicolas Pitreca8ce3d2014-05-26 18:19:39 -04007729 capa_now /= SCHED_CAPACITY_SCALE;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007730
7731 /* Amount of load we'd subtract */
Vincent Guittota2cd4262014-03-11 17:26:06 +01007732 if (busiest->avg_load > scaled_busy_load_per_task) {
Nicolas Pitre63b2ca32014-05-26 18:19:37 -04007733 capa_move += busiest->group_capacity *
Joonsoo Kim56cf5152013-08-06 17:36:43 +09007734 min(busiest->load_per_task,
Vincent Guittota2cd4262014-03-11 17:26:06 +01007735 busiest->avg_load - scaled_busy_load_per_task);
Joonsoo Kim56cf5152013-08-06 17:36:43 +09007736 }
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007737
7738 /* Amount of load we'd add */
Nicolas Pitre63b2ca32014-05-26 18:19:37 -04007739 if (busiest->avg_load * busiest->group_capacity <
Nicolas Pitreca8ce3d2014-05-26 18:19:39 -04007740 busiest->load_per_task * SCHED_CAPACITY_SCALE) {
Nicolas Pitre63b2ca32014-05-26 18:19:37 -04007741 tmp = (busiest->avg_load * busiest->group_capacity) /
7742 local->group_capacity;
Joonsoo Kim56cf5152013-08-06 17:36:43 +09007743 } else {
Nicolas Pitreca8ce3d2014-05-26 18:19:39 -04007744 tmp = (busiest->load_per_task * SCHED_CAPACITY_SCALE) /
Nicolas Pitre63b2ca32014-05-26 18:19:37 -04007745 local->group_capacity;
Joonsoo Kim56cf5152013-08-06 17:36:43 +09007746 }
Nicolas Pitre63b2ca32014-05-26 18:19:37 -04007747 capa_move += local->group_capacity *
Peter Zijlstra3ae11c92013-08-15 20:37:48 +02007748 min(local->load_per_task, local->avg_load + tmp);
Nicolas Pitreca8ce3d2014-05-26 18:19:39 -04007749 capa_move /= SCHED_CAPACITY_SCALE;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007750
7751 /* Move if we gain throughput */
Nicolas Pitre63b2ca32014-05-26 18:19:37 -04007752 if (capa_move > capa_now)
Joonsoo Kim56cf5152013-08-06 17:36:43 +09007753 env->imbalance = busiest->load_per_task;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007754}
7755
7756/**
7757 * calculate_imbalance - Calculate the amount of imbalance present within the
7758 * groups of a given sched_domain during load balance.
Peter Zijlstrabd939f42012-05-02 14:20:37 +02007759 * @env: load balance environment
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007760 * @sds: statistics of the sched_domain whose imbalance is to be calculated.
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007761 */
Peter Zijlstrabd939f42012-05-02 14:20:37 +02007762static inline void calculate_imbalance(struct lb_env *env, struct sd_lb_stats *sds)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007763{
Suresh Siddhadd5feea2010-02-23 16:13:52 -08007764 unsigned long max_pull, load_above_capacity = ~0UL;
Joonsoo Kim56cf5152013-08-06 17:36:43 +09007765 struct sg_lb_stats *local, *busiest;
Suresh Siddhadd5feea2010-02-23 16:13:52 -08007766
Joonsoo Kim56cf5152013-08-06 17:36:43 +09007767 local = &sds->local_stat;
Joonsoo Kim56cf5152013-08-06 17:36:43 +09007768 busiest = &sds->busiest_stat;
Joonsoo Kim56cf5152013-08-06 17:36:43 +09007769
Rik van Rielcaeb1782014-07-28 14:16:28 -04007770 if (busiest->group_type == group_imbalanced) {
Peter Zijlstra30ce5da2013-08-15 20:29:29 +02007771 /*
7772 * In the group_imb case we cannot rely on group-wide averages
7773 * to ensure cpu-load equilibrium, look at wider averages. XXX
7774 */
Joonsoo Kim56cf5152013-08-06 17:36:43 +09007775 busiest->load_per_task =
7776 min(busiest->load_per_task, sds->avg_load);
Suresh Siddhadd5feea2010-02-23 16:13:52 -08007777 }
7778
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007779 /*
Dietmar Eggemann885e5422016-04-29 20:32:39 +01007780 * Avg load of busiest sg can be less and avg load of local sg can
7781 * be greater than avg load across all sgs of sd because avg load
7782 * factors in sg capacity and sgs with smaller group_type are
7783 * skipped when updating the busiest sg:
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007784 */
Vladimir Davydovb1885552013-09-15 17:49:13 +04007785 if (busiest->avg_load <= sds->avg_load ||
7786 local->avg_load >= sds->avg_load) {
Peter Zijlstrabd939f42012-05-02 14:20:37 +02007787 env->imbalance = 0;
7788 return fix_small_imbalance(env, sds);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007789 }
7790
Peter Zijlstra9a5d9ba2014-07-29 17:15:11 +02007791 /*
7792 * If there aren't any idle cpus, avoid creating some.
7793 */
7794 if (busiest->group_type == group_overloaded &&
7795 local->group_type == group_overloaded) {
Peter Zijlstra1be0eb22016-05-06 12:21:23 +02007796 load_above_capacity = busiest->sum_nr_running * SCHED_CAPACITY_SCALE;
Morten Rasmussencfa10332016-04-29 20:32:40 +01007797 if (load_above_capacity > busiest->group_capacity) {
Vincent Guittotea678212015-02-27 16:54:11 +01007798 load_above_capacity -= busiest->group_capacity;
Dietmar Eggemann26656212016-08-10 11:27:27 +01007799 load_above_capacity *= scale_load_down(NICE_0_LOAD);
Morten Rasmussencfa10332016-04-29 20:32:40 +01007800 load_above_capacity /= busiest->group_capacity;
7801 } else
Vincent Guittotea678212015-02-27 16:54:11 +01007802 load_above_capacity = ~0UL;
Suresh Siddhadd5feea2010-02-23 16:13:52 -08007803 }
7804
7805 /*
7806 * We're trying to get all the cpus to the average_load, so we don't
7807 * want to push ourselves above the average load, nor do we wish to
7808 * reduce the max loaded cpu below the average load. At the same time,
Dietmar Eggemann0a9b23c2016-04-29 20:32:38 +01007809 * we also don't want to reduce the group load below the group
7810 * capacity. Thus we look for the minimum possible imbalance.
Suresh Siddhadd5feea2010-02-23 16:13:52 -08007811 */
Peter Zijlstra30ce5da2013-08-15 20:29:29 +02007812 max_pull = min(busiest->avg_load - sds->avg_load, load_above_capacity);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007813
7814 /* How much load to actually move to equalise the imbalance */
Joonsoo Kim56cf5152013-08-06 17:36:43 +09007815 env->imbalance = min(
Nicolas Pitre63b2ca32014-05-26 18:19:37 -04007816 max_pull * busiest->group_capacity,
7817 (sds->avg_load - local->avg_load) * local->group_capacity
Nicolas Pitreca8ce3d2014-05-26 18:19:39 -04007818 ) / SCHED_CAPACITY_SCALE;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007819
7820 /*
7821 * if *imbalance is less than the average load per runnable task
Lucas De Marchi25985ed2011-03-30 22:57:33 -03007822 * there is no guarantee that any tasks will be moved so we'll have
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007823 * a think about bumping its value to force at least one task to be
7824 * moved
7825 */
Joonsoo Kim56cf5152013-08-06 17:36:43 +09007826 if (env->imbalance < busiest->load_per_task)
Peter Zijlstrabd939f42012-05-02 14:20:37 +02007827 return fix_small_imbalance(env, sds);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007828}
Nikhil Raofab47622010-10-15 13:12:29 -07007829
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007830/******* find_busiest_group() helpers end here *********************/
7831
7832/**
7833 * find_busiest_group - Returns the busiest group within the sched_domain
Dietmar Eggemann0a9b23c2016-04-29 20:32:38 +01007834 * if there is an imbalance.
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007835 *
7836 * Also calculates the amount of weighted load which should be moved
7837 * to restore balance.
7838 *
Randy Dunlapcd968912012-06-08 13:18:33 -07007839 * @env: The load balancing environment.
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007840 *
Yacine Belkadie69f6182013-07-12 20:45:47 +02007841 * Return: - The busiest group if imbalance exists.
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007842 */
Joonsoo Kim56cf5152013-08-06 17:36:43 +09007843static struct sched_group *find_busiest_group(struct lb_env *env)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007844{
Joonsoo Kim56cf5152013-08-06 17:36:43 +09007845 struct sg_lb_stats *local, *busiest;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007846 struct sd_lb_stats sds;
7847
Peter Zijlstra147c5fc2013-08-19 15:22:57 +02007848 init_sd_lb_stats(&sds);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007849
7850 /*
7851 * Compute the various statistics relavent for load balancing at
7852 * this level.
7853 */
Joonsoo Kim23f0d202013-08-06 17:36:42 +09007854 update_sd_lb_stats(env, &sds);
Joonsoo Kim56cf5152013-08-06 17:36:43 +09007855 local = &sds.local_stat;
7856 busiest = &sds.busiest_stat;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007857
Vincent Guittotea678212015-02-27 16:54:11 +01007858 /* ASYM feature bypasses nice load balance check */
Srikar Dronamraju1f621e02016-04-06 18:47:40 +05307859 if (check_asym_packing(env, &sds))
Michael Neuling532cb4c2010-06-08 14:57:02 +10007860 return sds.busiest;
7861
Peter Zijlstracc57aa82011-02-21 18:55:32 +01007862 /* There is no busy sibling group to pull tasks from */
Joonsoo Kim56cf5152013-08-06 17:36:43 +09007863 if (!sds.busiest || busiest->sum_nr_running == 0)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007864 goto out_balanced;
7865
Peter Zijlstra90001d62017-07-31 17:50:05 +02007866 /* XXX broken for overlapping NUMA groups */
Nicolas Pitreca8ce3d2014-05-26 18:19:39 -04007867 sds.avg_load = (SCHED_CAPACITY_SCALE * sds.total_load)
7868 / sds.total_capacity;
Ken Chenb0432d82011-04-07 17:23:22 -07007869
Peter Zijlstra866ab432011-02-21 18:56:47 +01007870 /*
7871 * If the busiest group is imbalanced the below checks don't
Peter Zijlstra30ce5da2013-08-15 20:29:29 +02007872 * work because they assume all things are equal, which typically
Peter Zijlstra866ab432011-02-21 18:56:47 +01007873 * isn't true due to cpus_allowed constraints and the like.
7874 */
Rik van Rielcaeb1782014-07-28 14:16:28 -04007875 if (busiest->group_type == group_imbalanced)
Peter Zijlstra866ab432011-02-21 18:56:47 +01007876 goto force_balance;
7877
Peter Zijlstracc57aa82011-02-21 18:55:32 +01007878 /* SD_BALANCE_NEWIDLE trumps SMP nice when underutilized */
Vincent Guittotea678212015-02-27 16:54:11 +01007879 if (env->idle == CPU_NEWLY_IDLE && group_has_capacity(env, local) &&
7880 busiest->group_no_capacity)
Nikhil Raofab47622010-10-15 13:12:29 -07007881 goto force_balance;
7882
Peter Zijlstracc57aa82011-02-21 18:55:32 +01007883 /*
Zhihui Zhang9c58c792014-09-20 21:24:36 -04007884 * If the local group is busier than the selected busiest group
Peter Zijlstracc57aa82011-02-21 18:55:32 +01007885 * don't try and pull any tasks.
7886 */
Joonsoo Kim56cf5152013-08-06 17:36:43 +09007887 if (local->avg_load >= busiest->avg_load)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007888 goto out_balanced;
7889
Peter Zijlstracc57aa82011-02-21 18:55:32 +01007890 /*
7891 * Don't pull any tasks if this group is already above the domain
7892 * average load.
7893 */
Joonsoo Kim56cf5152013-08-06 17:36:43 +09007894 if (local->avg_load >= sds.avg_load)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007895 goto out_balanced;
7896
Peter Zijlstrabd939f42012-05-02 14:20:37 +02007897 if (env->idle == CPU_IDLE) {
Suresh Siddhaaae6d3d2010-09-17 15:02:32 -07007898 /*
Vincent Guittot43f4d662014-10-01 15:38:55 +02007899 * This cpu is idle. If the busiest group is not overloaded
7900 * and there is no imbalance between this and busiest group
7901 * wrt idle cpus, it is balanced. The imbalance becomes
7902 * significant if the diff is greater than 1 otherwise we
7903 * might end up to just move the imbalance on another group
Suresh Siddhaaae6d3d2010-09-17 15:02:32 -07007904 */
Vincent Guittot43f4d662014-10-01 15:38:55 +02007905 if ((busiest->group_type != group_overloaded) &&
7906 (local->idle_cpus <= (busiest->idle_cpus + 1)))
Suresh Siddhaaae6d3d2010-09-17 15:02:32 -07007907 goto out_balanced;
Peter Zijlstrac186faf2011-02-21 18:52:53 +01007908 } else {
7909 /*
7910 * In the CPU_NEWLY_IDLE, CPU_NOT_IDLE cases, use
7911 * imbalance_pct to be conservative.
7912 */
Joonsoo Kim56cf5152013-08-06 17:36:43 +09007913 if (100 * busiest->avg_load <=
7914 env->sd->imbalance_pct * local->avg_load)
Peter Zijlstrac186faf2011-02-21 18:52:53 +01007915 goto out_balanced;
Suresh Siddhaaae6d3d2010-09-17 15:02:32 -07007916 }
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007917
Nikhil Raofab47622010-10-15 13:12:29 -07007918force_balance:
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007919 /* Looks like there is an imbalance. Compute it */
Peter Zijlstrabd939f42012-05-02 14:20:37 +02007920 calculate_imbalance(env, &sds);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007921 return sds.busiest;
7922
7923out_balanced:
Peter Zijlstrabd939f42012-05-02 14:20:37 +02007924 env->imbalance = 0;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007925 return NULL;
7926}
7927
7928/*
7929 * find_busiest_queue - find the busiest runqueue among the cpus in group.
7930 */
Peter Zijlstrabd939f42012-05-02 14:20:37 +02007931static struct rq *find_busiest_queue(struct lb_env *env,
Michael Wangb94031302012-07-12 16:10:13 +08007932 struct sched_group *group)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007933{
7934 struct rq *busiest = NULL, *rq;
Nicolas Pitreced549f2014-05-26 18:19:38 -04007935 unsigned long busiest_load = 0, busiest_capacity = 1;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007936 int i;
7937
Peter Zijlstraae4df9d2017-05-01 11:03:12 +02007938 for_each_cpu_and(i, sched_group_span(group), env->cpus) {
Vincent Guittotea678212015-02-27 16:54:11 +01007939 unsigned long capacity, wl;
Peter Zijlstra0ec8aa02013-10-07 11:29:33 +01007940 enum fbq_type rt;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007941
Peter Zijlstra0ec8aa02013-10-07 11:29:33 +01007942 rq = cpu_rq(i);
7943 rt = fbq_classify_rq(rq);
7944
7945 /*
7946 * We classify groups/runqueues into three groups:
7947 * - regular: there are !numa tasks
7948 * - remote: there are numa tasks that run on the 'wrong' node
7949 * - all: there is no distinction
7950 *
7951 * In order to avoid migrating ideally placed numa tasks,
7952 * ignore those when there's better options.
7953 *
7954 * If we ignore the actual busiest queue to migrate another
7955 * task, the next balance pass can still reduce the busiest
7956 * queue by moving tasks around inside the node.
7957 *
7958 * If we cannot move enough load due to this classification
7959 * the next pass will adjust the group classification and
7960 * allow migration of more tasks.
7961 *
7962 * Both cases only affect the total convergence complexity.
7963 */
7964 if (rt > env->fbq_type)
7965 continue;
7966
Nicolas Pitreced549f2014-05-26 18:19:38 -04007967 capacity = capacity_of(i);
Srivatsa Vaddagiri9d5efe02010-06-08 14:57:02 +10007968
Viresh Kumarc7132dd2017-05-24 10:59:54 +05307969 wl = weighted_cpuload(rq);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007970
Thomas Gleixner6e40f5b2010-02-16 16:48:56 +01007971 /*
7972 * When comparing with imbalance, use weighted_cpuload()
Nicolas Pitreced549f2014-05-26 18:19:38 -04007973 * which is not scaled with the cpu capacity.
Thomas Gleixner6e40f5b2010-02-16 16:48:56 +01007974 */
Vincent Guittotea678212015-02-27 16:54:11 +01007975
7976 if (rq->nr_running == 1 && wl > env->imbalance &&
7977 !check_cpu_capacity(rq, env->sd))
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007978 continue;
7979
Thomas Gleixner6e40f5b2010-02-16 16:48:56 +01007980 /*
7981 * For the load comparisons with the other cpu's, consider
Nicolas Pitreced549f2014-05-26 18:19:38 -04007982 * the weighted_cpuload() scaled with the cpu capacity, so
7983 * that the load can be moved away from the cpu that is
7984 * potentially running at a lower capacity.
Joonsoo Kim95a79b82013-08-06 17:36:41 +09007985 *
Nicolas Pitreced549f2014-05-26 18:19:38 -04007986 * Thus we're looking for max(wl_i / capacity_i), crosswise
Joonsoo Kim95a79b82013-08-06 17:36:41 +09007987 * multiplication to rid ourselves of the division works out
Nicolas Pitreced549f2014-05-26 18:19:38 -04007988 * to: wl_i * capacity_j > wl_j * capacity_i; where j is
7989 * our previous maximum.
Thomas Gleixner6e40f5b2010-02-16 16:48:56 +01007990 */
Nicolas Pitreced549f2014-05-26 18:19:38 -04007991 if (wl * busiest_capacity > busiest_load * capacity) {
Joonsoo Kim95a79b82013-08-06 17:36:41 +09007992 busiest_load = wl;
Nicolas Pitreced549f2014-05-26 18:19:38 -04007993 busiest_capacity = capacity;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007994 busiest = rq;
7995 }
7996 }
7997
7998 return busiest;
7999}
8000
8001/*
8002 * Max backoff if we encounter pinned tasks. Pretty arbitrary value, but
8003 * so long as it is large enough.
8004 */
8005#define MAX_PINNED_INTERVAL 512
8006
Peter Zijlstrabd939f42012-05-02 14:20:37 +02008007static int need_active_balance(struct lb_env *env)
Peter Zijlstra1af3ed32009-12-23 15:10:31 +01008008{
Peter Zijlstrabd939f42012-05-02 14:20:37 +02008009 struct sched_domain *sd = env->sd;
8010
8011 if (env->idle == CPU_NEWLY_IDLE) {
Michael Neuling532cb4c2010-06-08 14:57:02 +10008012
8013 /*
8014 * ASYM_PACKING needs to force migrate tasks from busy but
Tim Chenafe06ef2016-11-22 12:23:53 -08008015 * lower priority CPUs in order to pack all tasks in the
8016 * highest priority CPUs.
Michael Neuling532cb4c2010-06-08 14:57:02 +10008017 */
Tim Chenafe06ef2016-11-22 12:23:53 -08008018 if ((sd->flags & SD_ASYM_PACKING) &&
8019 sched_asym_prefer(env->dst_cpu, env->src_cpu))
Michael Neuling532cb4c2010-06-08 14:57:02 +10008020 return 1;
Peter Zijlstra1af3ed32009-12-23 15:10:31 +01008021 }
8022
Vincent Guittot1aaf90a2015-02-27 16:54:14 +01008023 /*
8024 * The dst_cpu is idle and the src_cpu CPU has only 1 CFS task.
8025 * It's worth migrating the task if the src_cpu's capacity is reduced
8026 * because of other sched_class or IRQs if more capacity stays
8027 * available on dst_cpu.
8028 */
8029 if ((env->idle != CPU_NOT_IDLE) &&
8030 (env->src_rq->cfs.h_nr_running == 1)) {
8031 if ((check_cpu_capacity(env->src_rq, sd)) &&
8032 (capacity_of(env->src_cpu)*sd->imbalance_pct < capacity_of(env->dst_cpu)*100))
8033 return 1;
8034 }
8035
Peter Zijlstra1af3ed32009-12-23 15:10:31 +01008036 return unlikely(sd->nr_balance_failed > sd->cache_nice_tries+2);
8037}
8038
Tejun Heo969c7922010-05-06 18:49:21 +02008039static int active_load_balance_cpu_stop(void *data);
8040
Joonsoo Kim23f0d202013-08-06 17:36:42 +09008041static int should_we_balance(struct lb_env *env)
8042{
8043 struct sched_group *sg = env->sd->groups;
Joonsoo Kim23f0d202013-08-06 17:36:42 +09008044 int cpu, balance_cpu = -1;
8045
8046 /*
Peter Zijlstra024c9d22017-10-09 10:36:53 +02008047 * Ensure the balancing environment is consistent; can happen
8048 * when the softirq triggers 'during' hotplug.
8049 */
8050 if (!cpumask_test_cpu(env->dst_cpu, env->cpus))
8051 return 0;
8052
8053 /*
Joonsoo Kim23f0d202013-08-06 17:36:42 +09008054 * In the newly idle case, we will allow all the cpu's
8055 * to do the newly idle load balance.
8056 */
8057 if (env->idle == CPU_NEWLY_IDLE)
8058 return 1;
8059
Joonsoo Kim23f0d202013-08-06 17:36:42 +09008060 /* Try to find first idle cpu */
Peter Zijlstrae5c14b12017-05-01 10:47:02 +02008061 for_each_cpu_and(cpu, group_balance_mask(sg), env->cpus) {
Peter Zijlstraaf218122017-05-01 08:51:05 +02008062 if (!idle_cpu(cpu))
Joonsoo Kim23f0d202013-08-06 17:36:42 +09008063 continue;
8064
8065 balance_cpu = cpu;
8066 break;
8067 }
8068
8069 if (balance_cpu == -1)
8070 balance_cpu = group_balance_cpu(sg);
8071
8072 /*
8073 * First idle cpu or the first cpu(busiest) in this sched group
8074 * is eligible for doing load balancing at this and above domains.
8075 */
Joonsoo Kimb0cff9d2013-09-10 15:54:49 +09008076 return balance_cpu == env->dst_cpu;
Joonsoo Kim23f0d202013-08-06 17:36:42 +09008077}
8078
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008079/*
8080 * Check this_cpu to ensure it is balanced within domain. Attempt to move
8081 * tasks if there is an imbalance.
8082 */
8083static int load_balance(int this_cpu, struct rq *this_rq,
8084 struct sched_domain *sd, enum cpu_idle_type idle,
Joonsoo Kim23f0d202013-08-06 17:36:42 +09008085 int *continue_balancing)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008086{
Srivatsa Vaddagiri88b8dac2012-06-19 17:43:15 +05308087 int ld_moved, cur_ld_moved, active_balance = 0;
Peter Zijlstra62633222013-08-19 12:41:09 +02008088 struct sched_domain *sd_parent = sd->parent;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008089 struct sched_group *group;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008090 struct rq *busiest;
Peter Zijlstra8a8c69c2016-10-04 16:04:35 +02008091 struct rq_flags rf;
Christoph Lameter4ba29682014-08-26 19:12:21 -05008092 struct cpumask *cpus = this_cpu_cpumask_var_ptr(load_balance_mask);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008093
Peter Zijlstra8e45cb52012-02-22 12:47:19 +01008094 struct lb_env env = {
8095 .sd = sd,
Peter Zijlstraddcdf6e2012-02-22 19:27:40 +01008096 .dst_cpu = this_cpu,
8097 .dst_rq = this_rq,
Peter Zijlstraae4df9d2017-05-01 11:03:12 +02008098 .dst_grpmask = sched_group_span(sd->groups),
Peter Zijlstra8e45cb52012-02-22 12:47:19 +01008099 .idle = idle,
Peter Zijlstraeb953082012-04-17 13:38:40 +02008100 .loop_break = sched_nr_migrate_break,
Michael Wangb94031302012-07-12 16:10:13 +08008101 .cpus = cpus,
Peter Zijlstra0ec8aa02013-10-07 11:29:33 +01008102 .fbq_type = all,
Kirill Tkhai163122b2014-08-20 13:48:29 +04008103 .tasks = LIST_HEAD_INIT(env.tasks),
Peter Zijlstra8e45cb52012-02-22 12:47:19 +01008104 };
8105
Jeffrey Hugo65a44332017-06-07 13:18:57 -06008106 cpumask_and(cpus, sched_domain_span(sd), cpu_active_mask);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008107
Josh Poimboeufae928822016-06-17 12:43:24 -05008108 schedstat_inc(sd->lb_count[idle]);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008109
8110redo:
Joonsoo Kim23f0d202013-08-06 17:36:42 +09008111 if (!should_we_balance(&env)) {
8112 *continue_balancing = 0;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008113 goto out_balanced;
Joonsoo Kim23f0d202013-08-06 17:36:42 +09008114 }
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008115
Joonsoo Kim23f0d202013-08-06 17:36:42 +09008116 group = find_busiest_group(&env);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008117 if (!group) {
Josh Poimboeufae928822016-06-17 12:43:24 -05008118 schedstat_inc(sd->lb_nobusyg[idle]);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008119 goto out_balanced;
8120 }
8121
Michael Wangb94031302012-07-12 16:10:13 +08008122 busiest = find_busiest_queue(&env, group);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008123 if (!busiest) {
Josh Poimboeufae928822016-06-17 12:43:24 -05008124 schedstat_inc(sd->lb_nobusyq[idle]);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008125 goto out_balanced;
8126 }
8127
Michael Wang78feefc2012-08-06 16:41:59 +08008128 BUG_ON(busiest == env.dst_rq);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008129
Josh Poimboeufae928822016-06-17 12:43:24 -05008130 schedstat_add(sd->lb_imbalance[idle], env.imbalance);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008131
Vincent Guittot1aaf90a2015-02-27 16:54:14 +01008132 env.src_cpu = busiest->cpu;
8133 env.src_rq = busiest;
8134
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008135 ld_moved = 0;
8136 if (busiest->nr_running > 1) {
8137 /*
8138 * Attempt to move tasks. If find_busiest_group has found
8139 * an imbalance but busiest->nr_running <= 1, the group is
8140 * still unbalanced. ld_moved simply stays zero, so it is
8141 * correctly treated as an imbalance.
8142 */
Peter Zijlstra8e45cb52012-02-22 12:47:19 +01008143 env.flags |= LBF_ALL_PINNED;
Peter Zijlstrac82513e2012-04-26 13:12:27 +02008144 env.loop_max = min(sysctl_sched_nr_migrate, busiest->nr_running);
Peter Zijlstra8e45cb52012-02-22 12:47:19 +01008145
Peter Zijlstra5d6523e2012-03-10 00:07:36 +01008146more_balance:
Peter Zijlstra8a8c69c2016-10-04 16:04:35 +02008147 rq_lock_irqsave(busiest, &rf);
Peter Zijlstra3bed5e22016-10-03 16:35:32 +02008148 update_rq_clock(busiest);
Srivatsa Vaddagiri88b8dac2012-06-19 17:43:15 +05308149
8150 /*
8151 * cur_ld_moved - load moved in current iteration
8152 * ld_moved - cumulative load moved across iterations
8153 */
Kirill Tkhai163122b2014-08-20 13:48:29 +04008154 cur_ld_moved = detach_tasks(&env);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008155
8156 /*
Kirill Tkhai163122b2014-08-20 13:48:29 +04008157 * We've detached some tasks from busiest_rq. Every
8158 * task is masked "TASK_ON_RQ_MIGRATING", so we can safely
8159 * unlock busiest->lock, and we are able to be sure
8160 * that nobody can manipulate the tasks in parallel.
8161 * See task_rq_lock() family for the details.
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008162 */
Kirill Tkhai163122b2014-08-20 13:48:29 +04008163
Peter Zijlstra8a8c69c2016-10-04 16:04:35 +02008164 rq_unlock(busiest, &rf);
Kirill Tkhai163122b2014-08-20 13:48:29 +04008165
8166 if (cur_ld_moved) {
8167 attach_tasks(&env);
8168 ld_moved += cur_ld_moved;
8169 }
8170
Peter Zijlstra8a8c69c2016-10-04 16:04:35 +02008171 local_irq_restore(rf.flags);
Srivatsa Vaddagiri88b8dac2012-06-19 17:43:15 +05308172
Joonsoo Kimf1cd0852013-04-23 17:27:37 +09008173 if (env.flags & LBF_NEED_BREAK) {
8174 env.flags &= ~LBF_NEED_BREAK;
8175 goto more_balance;
8176 }
8177
Srivatsa Vaddagiri88b8dac2012-06-19 17:43:15 +05308178 /*
8179 * Revisit (affine) tasks on src_cpu that couldn't be moved to
8180 * us and move them to an alternate dst_cpu in our sched_group
8181 * where they can run. The upper limit on how many times we
8182 * iterate on same src_cpu is dependent on number of cpus in our
8183 * sched_group.
8184 *
8185 * This changes load balance semantics a bit on who can move
8186 * load to a given_cpu. In addition to the given_cpu itself
8187 * (or a ilb_cpu acting on its behalf where given_cpu is
8188 * nohz-idle), we now have balance_cpu in a position to move
8189 * load to given_cpu. In rare situations, this may cause
8190 * conflicts (balance_cpu and given_cpu/ilb_cpu deciding
8191 * _independently_ and at _same_ time to move some load to
8192 * given_cpu) causing exceess load to be moved to given_cpu.
8193 * This however should not happen so much in practice and
8194 * moreover subsequent load balance cycles should correct the
8195 * excess load moved.
8196 */
Peter Zijlstra62633222013-08-19 12:41:09 +02008197 if ((env.flags & LBF_DST_PINNED) && env.imbalance > 0) {
Srivatsa Vaddagiri88b8dac2012-06-19 17:43:15 +05308198
Vladimir Davydov7aff2e32013-09-15 21:30:13 +04008199 /* Prevent to re-select dst_cpu via env's cpus */
8200 cpumask_clear_cpu(env.dst_cpu, env.cpus);
8201
Michael Wang78feefc2012-08-06 16:41:59 +08008202 env.dst_rq = cpu_rq(env.new_dst_cpu);
Srivatsa Vaddagiri88b8dac2012-06-19 17:43:15 +05308203 env.dst_cpu = env.new_dst_cpu;
Peter Zijlstra62633222013-08-19 12:41:09 +02008204 env.flags &= ~LBF_DST_PINNED;
Srivatsa Vaddagiri88b8dac2012-06-19 17:43:15 +05308205 env.loop = 0;
8206 env.loop_break = sched_nr_migrate_break;
Joonsoo Kime02e60c2013-04-23 17:27:42 +09008207
Srivatsa Vaddagiri88b8dac2012-06-19 17:43:15 +05308208 /*
8209 * Go back to "more_balance" rather than "redo" since we
8210 * need to continue with same src_cpu.
8211 */
8212 goto more_balance;
8213 }
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008214
Peter Zijlstra62633222013-08-19 12:41:09 +02008215 /*
8216 * We failed to reach balance because of affinity.
8217 */
8218 if (sd_parent) {
Nicolas Pitre63b2ca32014-05-26 18:19:37 -04008219 int *group_imbalance = &sd_parent->groups->sgc->imbalance;
Peter Zijlstra62633222013-08-19 12:41:09 +02008220
Vincent Guittotafdeee02014-08-26 13:06:44 +02008221 if ((env.flags & LBF_SOME_PINNED) && env.imbalance > 0)
Peter Zijlstra62633222013-08-19 12:41:09 +02008222 *group_imbalance = 1;
Peter Zijlstra62633222013-08-19 12:41:09 +02008223 }
8224
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008225 /* All tasks on this runqueue were pinned by CPU affinity */
Peter Zijlstra8e45cb52012-02-22 12:47:19 +01008226 if (unlikely(env.flags & LBF_ALL_PINNED)) {
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008227 cpumask_clear_cpu(cpu_of(busiest), cpus);
Jeffrey Hugo65a44332017-06-07 13:18:57 -06008228 /*
8229 * Attempting to continue load balancing at the current
8230 * sched_domain level only makes sense if there are
8231 * active CPUs remaining as possible busiest CPUs to
8232 * pull load from which are not contained within the
8233 * destination group that is receiving any migrated
8234 * load.
8235 */
8236 if (!cpumask_subset(cpus, env.dst_grpmask)) {
Prashanth Nageshappabbf18b12012-06-19 17:52:07 +05308237 env.loop = 0;
8238 env.loop_break = sched_nr_migrate_break;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008239 goto redo;
Prashanth Nageshappabbf18b12012-06-19 17:52:07 +05308240 }
Vincent Guittotafdeee02014-08-26 13:06:44 +02008241 goto out_all_pinned;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008242 }
8243 }
8244
8245 if (!ld_moved) {
Josh Poimboeufae928822016-06-17 12:43:24 -05008246 schedstat_inc(sd->lb_failed[idle]);
Venkatesh Pallipadi58b26c42010-09-10 18:19:17 -07008247 /*
8248 * Increment the failure counter only on periodic balance.
8249 * We do not want newidle balance, which can be very
8250 * frequent, pollute the failure counter causing
8251 * excessive cache_hot migrations and active balances.
8252 */
8253 if (idle != CPU_NEWLY_IDLE)
8254 sd->nr_balance_failed++;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008255
Peter Zijlstrabd939f42012-05-02 14:20:37 +02008256 if (need_active_balance(&env)) {
Peter Zijlstra8a8c69c2016-10-04 16:04:35 +02008257 unsigned long flags;
8258
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008259 raw_spin_lock_irqsave(&busiest->lock, flags);
8260
Tejun Heo969c7922010-05-06 18:49:21 +02008261 /* don't kick the active_load_balance_cpu_stop,
8262 * if the curr task on busiest cpu can't be
8263 * moved to this_cpu
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008264 */
Ingo Molnar0c98d342017-02-05 15:38:10 +01008265 if (!cpumask_test_cpu(this_cpu, &busiest->curr->cpus_allowed)) {
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008266 raw_spin_unlock_irqrestore(&busiest->lock,
8267 flags);
Peter Zijlstra8e45cb52012-02-22 12:47:19 +01008268 env.flags |= LBF_ALL_PINNED;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008269 goto out_one_pinned;
8270 }
8271
Tejun Heo969c7922010-05-06 18:49:21 +02008272 /*
8273 * ->active_balance synchronizes accesses to
8274 * ->active_balance_work. Once set, it's cleared
8275 * only after active load balance is finished.
8276 */
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008277 if (!busiest->active_balance) {
8278 busiest->active_balance = 1;
8279 busiest->push_cpu = this_cpu;
8280 active_balance = 1;
8281 }
8282 raw_spin_unlock_irqrestore(&busiest->lock, flags);
Tejun Heo969c7922010-05-06 18:49:21 +02008283
Peter Zijlstrabd939f42012-05-02 14:20:37 +02008284 if (active_balance) {
Tejun Heo969c7922010-05-06 18:49:21 +02008285 stop_one_cpu_nowait(cpu_of(busiest),
8286 active_load_balance_cpu_stop, busiest,
8287 &busiest->active_balance_work);
Peter Zijlstrabd939f42012-05-02 14:20:37 +02008288 }
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008289
Srikar Dronamrajud02c071182016-03-23 17:54:44 +05308290 /* We've kicked active balancing, force task migration. */
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008291 sd->nr_balance_failed = sd->cache_nice_tries+1;
8292 }
8293 } else
8294 sd->nr_balance_failed = 0;
8295
8296 if (likely(!active_balance)) {
8297 /* We were unbalanced, so reset the balancing interval */
8298 sd->balance_interval = sd->min_interval;
8299 } else {
8300 /*
8301 * If we've begun active balancing, start to back off. This
8302 * case may not be covered by the all_pinned logic if there
8303 * is only 1 task on the busy runqueue (because we don't call
Kirill Tkhai163122b2014-08-20 13:48:29 +04008304 * detach_tasks).
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008305 */
8306 if (sd->balance_interval < sd->max_interval)
8307 sd->balance_interval *= 2;
8308 }
8309
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008310 goto out;
8311
8312out_balanced:
Vincent Guittotafdeee02014-08-26 13:06:44 +02008313 /*
8314 * We reach balance although we may have faced some affinity
8315 * constraints. Clear the imbalance flag if it was set.
8316 */
8317 if (sd_parent) {
8318 int *group_imbalance = &sd_parent->groups->sgc->imbalance;
8319
8320 if (*group_imbalance)
8321 *group_imbalance = 0;
8322 }
8323
8324out_all_pinned:
8325 /*
8326 * We reach balance because all tasks are pinned at this level so
8327 * we can't migrate them. Let the imbalance flag set so parent level
8328 * can try to migrate them.
8329 */
Josh Poimboeufae928822016-06-17 12:43:24 -05008330 schedstat_inc(sd->lb_balanced[idle]);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008331
8332 sd->nr_balance_failed = 0;
8333
8334out_one_pinned:
8335 /* tune up the balancing interval */
Peter Zijlstra8e45cb52012-02-22 12:47:19 +01008336 if (((env.flags & LBF_ALL_PINNED) &&
Peter Zijlstra5b54b562011-09-22 15:23:13 +02008337 sd->balance_interval < MAX_PINNED_INTERVAL) ||
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008338 (sd->balance_interval < sd->max_interval))
8339 sd->balance_interval *= 2;
8340
Venkatesh Pallipadi46e49b32011-02-14 14:38:50 -08008341 ld_moved = 0;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008342out:
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008343 return ld_moved;
8344}
8345
Jason Low52a08ef2014-05-08 17:49:22 -07008346static inline unsigned long
8347get_sd_balance_interval(struct sched_domain *sd, int cpu_busy)
8348{
8349 unsigned long interval = sd->balance_interval;
8350
8351 if (cpu_busy)
8352 interval *= sd->busy_factor;
8353
8354 /* scale ms to jiffies */
8355 interval = msecs_to_jiffies(interval);
8356 interval = clamp(interval, 1UL, max_load_balance_interval);
8357
8358 return interval;
8359}
8360
8361static inline void
Leo Yan31851a92016-08-05 14:31:29 +08008362update_next_balance(struct sched_domain *sd, unsigned long *next_balance)
Jason Low52a08ef2014-05-08 17:49:22 -07008363{
8364 unsigned long interval, next;
8365
Leo Yan31851a92016-08-05 14:31:29 +08008366 /* used by idle balance, so cpu_busy = 0 */
8367 interval = get_sd_balance_interval(sd, 0);
Jason Low52a08ef2014-05-08 17:49:22 -07008368 next = sd->last_balance + interval;
8369
8370 if (time_after(*next_balance, next))
8371 *next_balance = next;
8372}
8373
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008374/*
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008375 * idle_balance is called by schedule() if this_cpu is about to become
8376 * idle. Attempts to pull tasks from other CPUs.
8377 */
Matt Fleming46f69fa2016-09-21 14:38:12 +01008378static int idle_balance(struct rq *this_rq, struct rq_flags *rf)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008379{
Jason Low52a08ef2014-05-08 17:49:22 -07008380 unsigned long next_balance = jiffies + HZ;
8381 int this_cpu = this_rq->cpu;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008382 struct sched_domain *sd;
8383 int pulled_task = 0;
Jason Low9bd721c2013-09-13 11:26:52 -07008384 u64 curr_cost = 0;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008385
Peter Zijlstra6e831252014-02-11 16:11:48 +01008386 /*
8387 * We must set idle_stamp _before_ calling idle_balance(), such that we
8388 * measure the duration of idle_balance() as idle time.
8389 */
8390 this_rq->idle_stamp = rq_clock(this_rq);
8391
Matt Fleming46f69fa2016-09-21 14:38:12 +01008392 /*
Peter Zijlstra28004862017-09-07 17:03:50 +02008393 * Do not pull tasks towards !active CPUs...
8394 */
8395 if (!cpu_active(this_cpu))
8396 return 0;
8397
8398 /*
Matt Fleming46f69fa2016-09-21 14:38:12 +01008399 * This is OK, because current is on_cpu, which avoids it being picked
8400 * for load-balance and preemption/IRQs are still disabled avoiding
8401 * further scheduler activity on it and we're being very careful to
8402 * re-start the picking loop.
8403 */
8404 rq_unpin_lock(this_rq, rf);
8405
Tim Chen4486edd2014-06-23 12:16:49 -07008406 if (this_rq->avg_idle < sysctl_sched_migration_cost ||
8407 !this_rq->rd->overload) {
Jason Low52a08ef2014-05-08 17:49:22 -07008408 rcu_read_lock();
8409 sd = rcu_dereference_check_sched_domain(this_rq->sd);
8410 if (sd)
Leo Yan31851a92016-08-05 14:31:29 +08008411 update_next_balance(sd, &next_balance);
Jason Low52a08ef2014-05-08 17:49:22 -07008412 rcu_read_unlock();
8413
Peter Zijlstra6e831252014-02-11 16:11:48 +01008414 goto out;
Jason Low52a08ef2014-05-08 17:49:22 -07008415 }
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008416
Peter Zijlstraf492e122009-12-23 15:29:42 +01008417 raw_spin_unlock(&this_rq->lock);
8418
Paul Turner48a16752012-10-04 13:18:31 +02008419 update_blocked_averages(this_cpu);
Peter Zijlstradce840a2011-04-07 14:09:50 +02008420 rcu_read_lock();
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008421 for_each_domain(this_cpu, sd) {
Joonsoo Kim23f0d202013-08-06 17:36:42 +09008422 int continue_balancing = 1;
Jason Low9bd721c2013-09-13 11:26:52 -07008423 u64 t0, domain_cost;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008424
8425 if (!(sd->flags & SD_LOAD_BALANCE))
8426 continue;
8427
Jason Low52a08ef2014-05-08 17:49:22 -07008428 if (this_rq->avg_idle < curr_cost + sd->max_newidle_lb_cost) {
Leo Yan31851a92016-08-05 14:31:29 +08008429 update_next_balance(sd, &next_balance);
Jason Low9bd721c2013-09-13 11:26:52 -07008430 break;
Jason Low52a08ef2014-05-08 17:49:22 -07008431 }
Jason Low9bd721c2013-09-13 11:26:52 -07008432
Peter Zijlstraf492e122009-12-23 15:29:42 +01008433 if (sd->flags & SD_BALANCE_NEWIDLE) {
Jason Low9bd721c2013-09-13 11:26:52 -07008434 t0 = sched_clock_cpu(this_cpu);
8435
Peter Zijlstraf492e122009-12-23 15:29:42 +01008436 pulled_task = load_balance(this_cpu, this_rq,
Joonsoo Kim23f0d202013-08-06 17:36:42 +09008437 sd, CPU_NEWLY_IDLE,
8438 &continue_balancing);
Jason Low9bd721c2013-09-13 11:26:52 -07008439
8440 domain_cost = sched_clock_cpu(this_cpu) - t0;
8441 if (domain_cost > sd->max_newidle_lb_cost)
8442 sd->max_newidle_lb_cost = domain_cost;
8443
8444 curr_cost += domain_cost;
Peter Zijlstraf492e122009-12-23 15:29:42 +01008445 }
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008446
Leo Yan31851a92016-08-05 14:31:29 +08008447 update_next_balance(sd, &next_balance);
Jason Low39a4d9c2014-04-23 18:30:35 -07008448
8449 /*
8450 * Stop searching for tasks to pull if there are
8451 * now runnable tasks on this rq.
8452 */
8453 if (pulled_task || this_rq->nr_running > 0)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008454 break;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008455 }
Peter Zijlstradce840a2011-04-07 14:09:50 +02008456 rcu_read_unlock();
Peter Zijlstraf492e122009-12-23 15:29:42 +01008457
8458 raw_spin_lock(&this_rq->lock);
8459
Jason Low0e5b5332014-04-28 15:45:54 -07008460 if (curr_cost > this_rq->max_idle_balance_cost)
8461 this_rq->max_idle_balance_cost = curr_cost;
8462
Daniel Lezcanoe5fc6612014-01-17 10:04:02 +01008463 /*
Jason Low0e5b5332014-04-28 15:45:54 -07008464 * While browsing the domains, we released the rq lock, a task could
8465 * have been enqueued in the meantime. Since we're not going idle,
8466 * pretend we pulled a task.
Daniel Lezcanoe5fc6612014-01-17 10:04:02 +01008467 */
Jason Low0e5b5332014-04-28 15:45:54 -07008468 if (this_rq->cfs.h_nr_running && !pulled_task)
Peter Zijlstra6e831252014-02-11 16:11:48 +01008469 pulled_task = 1;
Daniel Lezcanoe5fc6612014-01-17 10:04:02 +01008470
Peter Zijlstra6e831252014-02-11 16:11:48 +01008471out:
Jason Low52a08ef2014-05-08 17:49:22 -07008472 /* Move the next balance forward */
8473 if (time_after(this_rq->next_balance, next_balance))
8474 this_rq->next_balance = next_balance;
8475
Kirill Tkhaie4aa3582014-03-06 13:31:55 +04008476 /* Is there a task of a high priority class? */
Kirill Tkhai46383642014-03-15 02:15:07 +04008477 if (this_rq->nr_running != this_rq->cfs.h_nr_running)
Kirill Tkhaie4aa3582014-03-06 13:31:55 +04008478 pulled_task = -1;
8479
Dietmar Eggemann38c6ade2015-10-20 13:04:41 +01008480 if (pulled_task)
Peter Zijlstra6e831252014-02-11 16:11:48 +01008481 this_rq->idle_stamp = 0;
8482
Matt Fleming46f69fa2016-09-21 14:38:12 +01008483 rq_repin_lock(this_rq, rf);
8484
Daniel Lezcano3c4017c2014-01-17 10:04:03 +01008485 return pulled_task;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008486}
8487
8488/*
Tejun Heo969c7922010-05-06 18:49:21 +02008489 * active_load_balance_cpu_stop is run by cpu stopper. It pushes
8490 * running tasks off the busiest CPU onto idle CPUs. It requires at
8491 * least 1 task to be running on each physical CPU where possible, and
8492 * avoids physical / logical imbalances.
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008493 */
Tejun Heo969c7922010-05-06 18:49:21 +02008494static int active_load_balance_cpu_stop(void *data)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008495{
Tejun Heo969c7922010-05-06 18:49:21 +02008496 struct rq *busiest_rq = data;
8497 int busiest_cpu = cpu_of(busiest_rq);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008498 int target_cpu = busiest_rq->push_cpu;
Tejun Heo969c7922010-05-06 18:49:21 +02008499 struct rq *target_rq = cpu_rq(target_cpu);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008500 struct sched_domain *sd;
Kirill Tkhaie5673f22014-08-20 13:48:01 +04008501 struct task_struct *p = NULL;
Peter Zijlstra8a8c69c2016-10-04 16:04:35 +02008502 struct rq_flags rf;
Tejun Heo969c7922010-05-06 18:49:21 +02008503
Peter Zijlstra8a8c69c2016-10-04 16:04:35 +02008504 rq_lock_irq(busiest_rq, &rf);
Peter Zijlstraedd8e412017-09-07 17:03:51 +02008505 /*
8506 * Between queueing the stop-work and running it is a hole in which
8507 * CPUs can become inactive. We should not move tasks from or to
8508 * inactive CPUs.
8509 */
8510 if (!cpu_active(busiest_cpu) || !cpu_active(target_cpu))
8511 goto out_unlock;
Tejun Heo969c7922010-05-06 18:49:21 +02008512
8513 /* make sure the requested cpu hasn't gone down in the meantime */
8514 if (unlikely(busiest_cpu != smp_processor_id() ||
8515 !busiest_rq->active_balance))
8516 goto out_unlock;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008517
8518 /* Is there any task to move? */
8519 if (busiest_rq->nr_running <= 1)
Tejun Heo969c7922010-05-06 18:49:21 +02008520 goto out_unlock;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008521
8522 /*
8523 * This condition is "impossible", if it occurs
8524 * we need to fix it. Originally reported by
8525 * Bjorn Helgaas on a 128-cpu setup.
8526 */
8527 BUG_ON(busiest_rq == target_rq);
8528
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008529 /* Search for an sd spanning us and the target CPU. */
Peter Zijlstradce840a2011-04-07 14:09:50 +02008530 rcu_read_lock();
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008531 for_each_domain(target_cpu, sd) {
8532 if ((sd->flags & SD_LOAD_BALANCE) &&
8533 cpumask_test_cpu(busiest_cpu, sched_domain_span(sd)))
8534 break;
8535 }
8536
8537 if (likely(sd)) {
Peter Zijlstra8e45cb52012-02-22 12:47:19 +01008538 struct lb_env env = {
8539 .sd = sd,
Peter Zijlstraddcdf6e2012-02-22 19:27:40 +01008540 .dst_cpu = target_cpu,
8541 .dst_rq = target_rq,
8542 .src_cpu = busiest_rq->cpu,
8543 .src_rq = busiest_rq,
Peter Zijlstra8e45cb52012-02-22 12:47:19 +01008544 .idle = CPU_IDLE,
Jeffrey Hugo65a44332017-06-07 13:18:57 -06008545 /*
8546 * can_migrate_task() doesn't need to compute new_dst_cpu
8547 * for active balancing. Since we have CPU_IDLE, but no
8548 * @dst_grpmask we need to make that test go away with lying
8549 * about DST_PINNED.
8550 */
8551 .flags = LBF_DST_PINNED,
Peter Zijlstra8e45cb52012-02-22 12:47:19 +01008552 };
8553
Josh Poimboeufae928822016-06-17 12:43:24 -05008554 schedstat_inc(sd->alb_count);
Peter Zijlstra3bed5e22016-10-03 16:35:32 +02008555 update_rq_clock(busiest_rq);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008556
Kirill Tkhaie5673f22014-08-20 13:48:01 +04008557 p = detach_one_task(&env);
Srikar Dronamrajud02c071182016-03-23 17:54:44 +05308558 if (p) {
Josh Poimboeufae928822016-06-17 12:43:24 -05008559 schedstat_inc(sd->alb_pushed);
Srikar Dronamrajud02c071182016-03-23 17:54:44 +05308560 /* Active balancing done, reset the failure counter. */
8561 sd->nr_balance_failed = 0;
8562 } else {
Josh Poimboeufae928822016-06-17 12:43:24 -05008563 schedstat_inc(sd->alb_failed);
Srikar Dronamrajud02c071182016-03-23 17:54:44 +05308564 }
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008565 }
Peter Zijlstradce840a2011-04-07 14:09:50 +02008566 rcu_read_unlock();
Tejun Heo969c7922010-05-06 18:49:21 +02008567out_unlock:
8568 busiest_rq->active_balance = 0;
Peter Zijlstra8a8c69c2016-10-04 16:04:35 +02008569 rq_unlock(busiest_rq, &rf);
Kirill Tkhaie5673f22014-08-20 13:48:01 +04008570
8571 if (p)
8572 attach_one_task(target_rq, p);
8573
8574 local_irq_enable();
8575
Tejun Heo969c7922010-05-06 18:49:21 +02008576 return 0;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008577}
8578
Mike Galbraithd987fc72011-12-05 10:01:47 +01008579static inline int on_null_domain(struct rq *rq)
8580{
8581 return unlikely(!rcu_dereference_sched(rq->sd));
8582}
8583
Frederic Weisbecker3451d022011-08-10 23:21:01 +02008584#ifdef CONFIG_NO_HZ_COMMON
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07008585/*
8586 * idle load balancing details
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07008587 * - When one of the busy CPUs notice that there may be an idle rebalancing
8588 * needed, they will kick the idle load balancer, which then does idle
8589 * load balancing for all the idle CPUs.
8590 */
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008591static struct {
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07008592 cpumask_var_t idle_cpus_mask;
Suresh Siddha0b005cf2011-12-01 17:07:34 -08008593 atomic_t nr_cpus;
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07008594 unsigned long next_balance; /* in jiffy units */
8595} nohz ____cacheline_aligned;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008596
Daniel Lezcano3dd03372014-01-06 12:34:41 +01008597static inline int find_new_ilb(void)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008598{
Suresh Siddha0b005cf2011-12-01 17:07:34 -08008599 int ilb = cpumask_first(nohz.idle_cpus_mask);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008600
Suresh Siddha786d6dc2011-12-01 17:07:35 -08008601 if (ilb < nr_cpu_ids && idle_cpu(ilb))
8602 return ilb;
8603
8604 return nr_cpu_ids;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008605}
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008606
8607/*
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07008608 * Kick a CPU to do the nohz balancing, if it is time for it. We pick the
8609 * nohz_load_balancer CPU (if there is one) otherwise fallback to any idle
8610 * CPU (if there is one).
8611 */
Daniel Lezcano0aeeeeb2014-01-06 12:34:42 +01008612static void nohz_balancer_kick(void)
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07008613{
8614 int ilb_cpu;
8615
8616 nohz.next_balance++;
8617
Daniel Lezcano3dd03372014-01-06 12:34:41 +01008618 ilb_cpu = find_new_ilb();
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07008619
Suresh Siddha0b005cf2011-12-01 17:07:34 -08008620 if (ilb_cpu >= nr_cpu_ids)
8621 return;
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07008622
Suresh Siddhacd490c52011-12-06 11:26:34 -08008623 if (test_and_set_bit(NOHZ_BALANCE_KICK, nohz_flags(ilb_cpu)))
Suresh Siddha1c792db2011-12-01 17:07:32 -08008624 return;
8625 /*
8626 * Use smp_send_reschedule() instead of resched_cpu().
8627 * This way we generate a sched IPI on the target cpu which
8628 * is idle. And the softirq performing nohz idle load balance
8629 * will be run before returning from the IPI.
8630 */
8631 smp_send_reschedule(ilb_cpu);
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07008632 return;
8633}
8634
Thomas Gleixner20a5c8c2016-03-10 12:54:20 +01008635void nohz_balance_exit_idle(unsigned int cpu)
Suresh Siddha71325962012-01-19 18:28:57 -08008636{
8637 if (unlikely(test_bit(NOHZ_TICK_STOPPED, nohz_flags(cpu)))) {
Mike Galbraithd987fc72011-12-05 10:01:47 +01008638 /*
8639 * Completely isolated CPUs don't ever set, so we must test.
8640 */
8641 if (likely(cpumask_test_cpu(cpu, nohz.idle_cpus_mask))) {
8642 cpumask_clear_cpu(cpu, nohz.idle_cpus_mask);
8643 atomic_dec(&nohz.nr_cpus);
8644 }
Suresh Siddha71325962012-01-19 18:28:57 -08008645 clear_bit(NOHZ_TICK_STOPPED, nohz_flags(cpu));
8646 }
8647}
8648
Suresh Siddha69e1e812011-12-01 17:07:33 -08008649static inline void set_cpu_sd_state_busy(void)
8650{
8651 struct sched_domain *sd;
Preeti U Murthy37dc6b52013-10-30 08:42:52 +05308652 int cpu = smp_processor_id();
Suresh Siddha69e1e812011-12-01 17:07:33 -08008653
Suresh Siddha69e1e812011-12-01 17:07:33 -08008654 rcu_read_lock();
Peter Zijlstra0e369d72016-05-09 10:38:01 +02008655 sd = rcu_dereference(per_cpu(sd_llc, cpu));
Vincent Guittot25f55d92013-04-23 16:59:02 +02008656
8657 if (!sd || !sd->nohz_idle)
8658 goto unlock;
8659 sd->nohz_idle = 0;
8660
Peter Zijlstra0e369d72016-05-09 10:38:01 +02008661 atomic_inc(&sd->shared->nr_busy_cpus);
Vincent Guittot25f55d92013-04-23 16:59:02 +02008662unlock:
Suresh Siddha69e1e812011-12-01 17:07:33 -08008663 rcu_read_unlock();
8664}
8665
8666void set_cpu_sd_state_idle(void)
8667{
8668 struct sched_domain *sd;
Preeti U Murthy37dc6b52013-10-30 08:42:52 +05308669 int cpu = smp_processor_id();
Suresh Siddha69e1e812011-12-01 17:07:33 -08008670
Suresh Siddha69e1e812011-12-01 17:07:33 -08008671 rcu_read_lock();
Peter Zijlstra0e369d72016-05-09 10:38:01 +02008672 sd = rcu_dereference(per_cpu(sd_llc, cpu));
Vincent Guittot25f55d92013-04-23 16:59:02 +02008673
8674 if (!sd || sd->nohz_idle)
8675 goto unlock;
8676 sd->nohz_idle = 1;
8677
Peter Zijlstra0e369d72016-05-09 10:38:01 +02008678 atomic_dec(&sd->shared->nr_busy_cpus);
Vincent Guittot25f55d92013-04-23 16:59:02 +02008679unlock:
Suresh Siddha69e1e812011-12-01 17:07:33 -08008680 rcu_read_unlock();
8681}
8682
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07008683/*
Alex Shic1cc0172012-09-10 15:10:58 +08008684 * This routine will record that the cpu is going idle with tick stopped.
Suresh Siddha0b005cf2011-12-01 17:07:34 -08008685 * This info will be used in performing idle load balancing in the future.
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008686 */
Alex Shic1cc0172012-09-10 15:10:58 +08008687void nohz_balance_enter_idle(int cpu)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008688{
Suresh Siddha71325962012-01-19 18:28:57 -08008689 /*
8690 * If this cpu is going down, then nothing needs to be done.
8691 */
8692 if (!cpu_active(cpu))
8693 return;
8694
Frederic Weisbecker387bc8b2017-06-19 04:12:02 +02008695 /* Spare idle load balancing on CPUs that don't want to be disturbed: */
8696 if (!is_housekeeping_cpu(cpu))
8697 return;
8698
Alex Shic1cc0172012-09-10 15:10:58 +08008699 if (test_bit(NOHZ_TICK_STOPPED, nohz_flags(cpu)))
8700 return;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008701
Mike Galbraithd987fc72011-12-05 10:01:47 +01008702 /*
8703 * If we're a completely isolated CPU, we don't play.
8704 */
8705 if (on_null_domain(cpu_rq(cpu)))
8706 return;
8707
Alex Shic1cc0172012-09-10 15:10:58 +08008708 cpumask_set_cpu(cpu, nohz.idle_cpus_mask);
8709 atomic_inc(&nohz.nr_cpus);
8710 set_bit(NOHZ_TICK_STOPPED, nohz_flags(cpu));
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008711}
8712#endif
8713
8714static DEFINE_SPINLOCK(balancing);
8715
Peter Zijlstra49c022e2011-04-05 10:14:25 +02008716/*
8717 * Scale the max load_balance interval with the number of CPUs in the system.
8718 * This trades load-balance latency on larger machines for less cross talk.
8719 */
Peter Zijlstra029632f2011-10-25 10:00:11 +02008720void update_max_interval(void)
Peter Zijlstra49c022e2011-04-05 10:14:25 +02008721{
8722 max_load_balance_interval = HZ*num_online_cpus()/10;
8723}
8724
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008725/*
8726 * It checks each scheduling domain to see if it is due to be balanced,
8727 * and initiates a balancing operation if so.
8728 *
Libinb9b08532013-04-01 19:14:01 +08008729 * Balancing parameters are set up in init_sched_domains.
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008730 */
Daniel Lezcanof7ed0a82014-01-06 12:34:43 +01008731static void rebalance_domains(struct rq *rq, enum cpu_idle_type idle)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008732{
Joonsoo Kim23f0d202013-08-06 17:36:42 +09008733 int continue_balancing = 1;
Daniel Lezcanof7ed0a82014-01-06 12:34:43 +01008734 int cpu = rq->cpu;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008735 unsigned long interval;
Peter Zijlstra04f733b2012-05-11 00:12:02 +02008736 struct sched_domain *sd;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008737 /* Earliest time when we have to do rebalance again */
8738 unsigned long next_balance = jiffies + 60*HZ;
8739 int update_next_balance = 0;
Jason Lowf48627e2013-09-13 11:26:53 -07008740 int need_serialize, need_decay = 0;
8741 u64 max_cost = 0;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008742
Paul Turner48a16752012-10-04 13:18:31 +02008743 update_blocked_averages(cpu);
Peter Zijlstra2069dd72010-11-15 15:47:00 -08008744
Peter Zijlstradce840a2011-04-07 14:09:50 +02008745 rcu_read_lock();
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008746 for_each_domain(cpu, sd) {
Jason Lowf48627e2013-09-13 11:26:53 -07008747 /*
8748 * Decay the newidle max times here because this is a regular
8749 * visit to all the domains. Decay ~1% per second.
8750 */
8751 if (time_after(jiffies, sd->next_decay_max_lb_cost)) {
8752 sd->max_newidle_lb_cost =
8753 (sd->max_newidle_lb_cost * 253) / 256;
8754 sd->next_decay_max_lb_cost = jiffies + HZ;
8755 need_decay = 1;
8756 }
8757 max_cost += sd->max_newidle_lb_cost;
8758
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008759 if (!(sd->flags & SD_LOAD_BALANCE))
8760 continue;
8761
Jason Lowf48627e2013-09-13 11:26:53 -07008762 /*
8763 * Stop the load balance at this level. There is another
8764 * CPU in our sched group which is doing load balancing more
8765 * actively.
8766 */
8767 if (!continue_balancing) {
8768 if (need_decay)
8769 continue;
8770 break;
8771 }
8772
Jason Low52a08ef2014-05-08 17:49:22 -07008773 interval = get_sd_balance_interval(sd, idle != CPU_IDLE);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008774
8775 need_serialize = sd->flags & SD_SERIALIZE;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008776 if (need_serialize) {
8777 if (!spin_trylock(&balancing))
8778 goto out;
8779 }
8780
8781 if (time_after_eq(jiffies, sd->last_balance + interval)) {
Joonsoo Kim23f0d202013-08-06 17:36:42 +09008782 if (load_balance(cpu, rq, sd, idle, &continue_balancing)) {
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008783 /*
Peter Zijlstra62633222013-08-19 12:41:09 +02008784 * The LBF_DST_PINNED logic could have changed
Joonsoo Kimde5eb2d2013-04-23 17:27:38 +09008785 * env->dst_cpu, so we can't know our idle
8786 * state even if we migrated tasks. Update it.
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008787 */
Joonsoo Kimde5eb2d2013-04-23 17:27:38 +09008788 idle = idle_cpu(cpu) ? CPU_IDLE : CPU_NOT_IDLE;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008789 }
8790 sd->last_balance = jiffies;
Jason Low52a08ef2014-05-08 17:49:22 -07008791 interval = get_sd_balance_interval(sd, idle != CPU_IDLE);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008792 }
8793 if (need_serialize)
8794 spin_unlock(&balancing);
8795out:
8796 if (time_after(next_balance, sd->last_balance + interval)) {
8797 next_balance = sd->last_balance + interval;
8798 update_next_balance = 1;
8799 }
Jason Lowf48627e2013-09-13 11:26:53 -07008800 }
8801 if (need_decay) {
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008802 /*
Jason Lowf48627e2013-09-13 11:26:53 -07008803 * Ensure the rq-wide value also decays but keep it at a
8804 * reasonable floor to avoid funnies with rq->avg_idle.
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008805 */
Jason Lowf48627e2013-09-13 11:26:53 -07008806 rq->max_idle_balance_cost =
8807 max((u64)sysctl_sched_migration_cost, max_cost);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008808 }
Peter Zijlstradce840a2011-04-07 14:09:50 +02008809 rcu_read_unlock();
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008810
8811 /*
8812 * next_balance will be updated only when there is a need.
8813 * When the cpu is attached to null domain for ex, it will not be
8814 * updated.
8815 */
Vincent Guittotc5afb6a2015-08-03 11:55:50 +02008816 if (likely(update_next_balance)) {
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008817 rq->next_balance = next_balance;
Vincent Guittotc5afb6a2015-08-03 11:55:50 +02008818
8819#ifdef CONFIG_NO_HZ_COMMON
8820 /*
8821 * If this CPU has been elected to perform the nohz idle
8822 * balance. Other idle CPUs have already rebalanced with
8823 * nohz_idle_balance() and nohz.next_balance has been
8824 * updated accordingly. This CPU is now running the idle load
8825 * balance for itself and we need to update the
8826 * nohz.next_balance accordingly.
8827 */
8828 if ((idle == CPU_IDLE) && time_after(nohz.next_balance, rq->next_balance))
8829 nohz.next_balance = rq->next_balance;
8830#endif
8831 }
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008832}
8833
Frederic Weisbecker3451d022011-08-10 23:21:01 +02008834#ifdef CONFIG_NO_HZ_COMMON
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07008835/*
Frederic Weisbecker3451d022011-08-10 23:21:01 +02008836 * In CONFIG_NO_HZ_COMMON case, the idle balance kickee will do the
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07008837 * rebalancing for all the cpus for whom scheduler ticks are stopped.
8838 */
Daniel Lezcano208cb162014-01-06 12:34:44 +01008839static void nohz_idle_balance(struct rq *this_rq, enum cpu_idle_type idle)
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07008840{
Daniel Lezcano208cb162014-01-06 12:34:44 +01008841 int this_cpu = this_rq->cpu;
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07008842 struct rq *rq;
8843 int balance_cpu;
Vincent Guittotc5afb6a2015-08-03 11:55:50 +02008844 /* Earliest time when we have to do rebalance again */
8845 unsigned long next_balance = jiffies + 60*HZ;
8846 int update_next_balance = 0;
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07008847
Suresh Siddha1c792db2011-12-01 17:07:32 -08008848 if (idle != CPU_IDLE ||
8849 !test_bit(NOHZ_BALANCE_KICK, nohz_flags(this_cpu)))
8850 goto end;
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07008851
8852 for_each_cpu(balance_cpu, nohz.idle_cpus_mask) {
Suresh Siddha8a6d42d2011-12-06 11:19:37 -08008853 if (balance_cpu == this_cpu || !idle_cpu(balance_cpu))
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07008854 continue;
8855
8856 /*
8857 * If this cpu gets work to do, stop the load balancing
8858 * work being done for other cpus. Next load
8859 * balancing owner will pick it up.
8860 */
Suresh Siddha1c792db2011-12-01 17:07:32 -08008861 if (need_resched())
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07008862 break;
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07008863
Vincent Guittot5ed4f1d2012-09-13 06:11:26 +02008864 rq = cpu_rq(balance_cpu);
8865
Tim Chened61bbc2014-05-20 14:39:27 -07008866 /*
8867 * If time for next balance is due,
8868 * do the balance.
8869 */
8870 if (time_after_eq(jiffies, rq->next_balance)) {
Peter Zijlstra8a8c69c2016-10-04 16:04:35 +02008871 struct rq_flags rf;
8872
8873 rq_lock_irq(rq, &rf);
Tim Chened61bbc2014-05-20 14:39:27 -07008874 update_rq_clock(rq);
Frederic Weisbeckercee1afc2016-04-13 15:56:50 +02008875 cpu_load_update_idle(rq);
Peter Zijlstra8a8c69c2016-10-04 16:04:35 +02008876 rq_unlock_irq(rq, &rf);
8877
Tim Chened61bbc2014-05-20 14:39:27 -07008878 rebalance_domains(rq, CPU_IDLE);
8879 }
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07008880
Vincent Guittotc5afb6a2015-08-03 11:55:50 +02008881 if (time_after(next_balance, rq->next_balance)) {
8882 next_balance = rq->next_balance;
8883 update_next_balance = 1;
8884 }
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07008885 }
Vincent Guittotc5afb6a2015-08-03 11:55:50 +02008886
8887 /*
8888 * next_balance will be updated only when there is a need.
8889 * When the CPU is attached to null domain for ex, it will not be
8890 * updated.
8891 */
8892 if (likely(update_next_balance))
8893 nohz.next_balance = next_balance;
Suresh Siddha1c792db2011-12-01 17:07:32 -08008894end:
8895 clear_bit(NOHZ_BALANCE_KICK, nohz_flags(this_cpu));
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07008896}
8897
8898/*
Suresh Siddha0b005cf2011-12-01 17:07:34 -08008899 * Current heuristic for kicking the idle load balancer in the presence
Vincent Guittot1aaf90a2015-02-27 16:54:14 +01008900 * of an idle cpu in the system.
Suresh Siddha0b005cf2011-12-01 17:07:34 -08008901 * - This rq has more than one task.
Vincent Guittot1aaf90a2015-02-27 16:54:14 +01008902 * - This rq has at least one CFS task and the capacity of the CPU is
8903 * significantly reduced because of RT tasks or IRQs.
8904 * - At parent of LLC scheduler domain level, this cpu's scheduler group has
8905 * multiple busy cpu.
Suresh Siddha0b005cf2011-12-01 17:07:34 -08008906 * - For SD_ASYM_PACKING, if the lower numbered cpu's in the scheduler
8907 * domain span are idle.
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07008908 */
Vincent Guittot1aaf90a2015-02-27 16:54:14 +01008909static inline bool nohz_kick_needed(struct rq *rq)
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07008910{
8911 unsigned long now = jiffies;
Peter Zijlstra0e369d72016-05-09 10:38:01 +02008912 struct sched_domain_shared *sds;
Suresh Siddha0b005cf2011-12-01 17:07:34 -08008913 struct sched_domain *sd;
Tim Chenafe06ef2016-11-22 12:23:53 -08008914 int nr_busy, i, cpu = rq->cpu;
Vincent Guittot1aaf90a2015-02-27 16:54:14 +01008915 bool kick = false;
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07008916
Daniel Lezcano4a725622014-01-06 12:34:39 +01008917 if (unlikely(rq->idle_balance))
Vincent Guittot1aaf90a2015-02-27 16:54:14 +01008918 return false;
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07008919
Suresh Siddha1c792db2011-12-01 17:07:32 -08008920 /*
8921 * We may be recently in ticked or tickless idle mode. At the first
8922 * busy tick after returning from idle, we will update the busy stats.
8923 */
Suresh Siddha69e1e812011-12-01 17:07:33 -08008924 set_cpu_sd_state_busy();
Alex Shic1cc0172012-09-10 15:10:58 +08008925 nohz_balance_exit_idle(cpu);
Suresh Siddha0b005cf2011-12-01 17:07:34 -08008926
8927 /*
8928 * None are in tickless mode and hence no need for NOHZ idle load
8929 * balancing.
8930 */
8931 if (likely(!atomic_read(&nohz.nr_cpus)))
Vincent Guittot1aaf90a2015-02-27 16:54:14 +01008932 return false;
Suresh Siddha1c792db2011-12-01 17:07:32 -08008933
8934 if (time_before(now, nohz.next_balance))
Vincent Guittot1aaf90a2015-02-27 16:54:14 +01008935 return false;
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07008936
Suresh Siddha0b005cf2011-12-01 17:07:34 -08008937 if (rq->nr_running >= 2)
Vincent Guittot1aaf90a2015-02-27 16:54:14 +01008938 return true;
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07008939
Peter Zijlstra067491b2011-12-07 14:32:08 +01008940 rcu_read_lock();
Peter Zijlstra0e369d72016-05-09 10:38:01 +02008941 sds = rcu_dereference(per_cpu(sd_llc_shared, cpu));
8942 if (sds) {
8943 /*
8944 * XXX: write a coherent comment on why we do this.
8945 * See also: http://lkml.kernel.org/r/20111202010832.602203411@sbsiddha-desk.sc.intel.com
8946 */
8947 nr_busy = atomic_read(&sds->nr_busy_cpus);
Vincent Guittot1aaf90a2015-02-27 16:54:14 +01008948 if (nr_busy > 1) {
8949 kick = true;
8950 goto unlock;
8951 }
8952
8953 }
8954
8955 sd = rcu_dereference(rq->sd);
8956 if (sd) {
8957 if ((rq->cfs.h_nr_running >= 1) &&
8958 check_cpu_capacity(rq, sd)) {
8959 kick = true;
8960 goto unlock;
8961 }
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07008962 }
Preeti U Murthy37dc6b52013-10-30 08:42:52 +05308963
8964 sd = rcu_dereference(per_cpu(sd_asym, cpu));
Tim Chenafe06ef2016-11-22 12:23:53 -08008965 if (sd) {
8966 for_each_cpu(i, sched_domain_span(sd)) {
8967 if (i == cpu ||
8968 !cpumask_test_cpu(i, nohz.idle_cpus_mask))
8969 continue;
Preeti U Murthy37dc6b52013-10-30 08:42:52 +05308970
Tim Chenafe06ef2016-11-22 12:23:53 -08008971 if (sched_asym_prefer(i, cpu)) {
8972 kick = true;
8973 goto unlock;
8974 }
8975 }
8976 }
Vincent Guittot1aaf90a2015-02-27 16:54:14 +01008977unlock:
Peter Zijlstra067491b2011-12-07 14:32:08 +01008978 rcu_read_unlock();
Vincent Guittot1aaf90a2015-02-27 16:54:14 +01008979 return kick;
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07008980}
8981#else
Daniel Lezcano208cb162014-01-06 12:34:44 +01008982static void nohz_idle_balance(struct rq *this_rq, enum cpu_idle_type idle) { }
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07008983#endif
8984
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008985/*
8986 * run_rebalance_domains is triggered when needed from the scheduler tick.
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07008987 * Also triggered for nohz idle balancing (with nohz_balancing_kick set).
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008988 */
Emese Revfy0766f782016-06-20 20:42:34 +02008989static __latent_entropy void run_rebalance_domains(struct softirq_action *h)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008990{
Daniel Lezcano208cb162014-01-06 12:34:44 +01008991 struct rq *this_rq = this_rq();
Suresh Siddha6eb57e02011-10-03 15:09:01 -07008992 enum cpu_idle_type idle = this_rq->idle_balance ?
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008993 CPU_IDLE : CPU_NOT_IDLE;
8994
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008995 /*
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07008996 * If this cpu has a pending nohz_balance_kick, then do the
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008997 * balancing on behalf of the other idle cpus whose ticks are
Preeti U Murthyd4573c32015-03-26 18:32:44 +05308998 * stopped. Do nohz_idle_balance *before* rebalance_domains to
8999 * give the idle cpus a chance to load balance. Else we may
9000 * load balance only within the local sched_domain hierarchy
9001 * and abort nohz_idle_balance altogether if we pull some load.
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01009002 */
Daniel Lezcano208cb162014-01-06 12:34:44 +01009003 nohz_idle_balance(this_rq, idle);
Preeti U Murthyd4573c32015-03-26 18:32:44 +05309004 rebalance_domains(this_rq, idle);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01009005}
9006
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01009007/*
9008 * Trigger the SCHED_SOFTIRQ if it is time to do periodic load balancing.
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01009009 */
Daniel Lezcano7caff662014-01-06 12:34:38 +01009010void trigger_load_balance(struct rq *rq)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01009011{
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01009012 /* Don't need to rebalance while attached to NULL domain */
Daniel Lezcanoc7260992014-01-06 12:34:45 +01009013 if (unlikely(on_null_domain(rq)))
9014 return;
9015
9016 if (time_after_eq(jiffies, rq->next_balance))
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01009017 raise_softirq(SCHED_SOFTIRQ);
Frederic Weisbecker3451d022011-08-10 23:21:01 +02009018#ifdef CONFIG_NO_HZ_COMMON
Daniel Lezcanoc7260992014-01-06 12:34:45 +01009019 if (nohz_kick_needed(rq))
Daniel Lezcano0aeeeeb2014-01-06 12:34:42 +01009020 nohz_balancer_kick();
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07009021#endif
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01009022}
9023
Christian Ehrhardt0bcdcf22009-11-30 12:16:46 +01009024static void rq_online_fair(struct rq *rq)
9025{
9026 update_sysctl();
Kirill Tkhai0e59bda2014-06-25 12:19:42 +04009027
9028 update_runtime_enabled(rq);
Christian Ehrhardt0bcdcf22009-11-30 12:16:46 +01009029}
9030
9031static void rq_offline_fair(struct rq *rq)
9032{
9033 update_sysctl();
Peter Boonstoppela4c96ae2012-08-09 15:34:47 -07009034
9035 /* Ensure any throttled groups are reachable by pick_next_task */
9036 unthrottle_offline_cfs_rqs(rq);
Christian Ehrhardt0bcdcf22009-11-30 12:16:46 +01009037}
9038
Dhaval Giani55e12e52008-06-24 23:39:43 +05309039#endif /* CONFIG_SMP */
Peter Williamse1d14842007-10-24 18:23:51 +02009040
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02009041/*
9042 * scheduler tick hitting a task of our scheduling class:
9043 */
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01009044static void task_tick_fair(struct rq *rq, struct task_struct *curr, int queued)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02009045{
9046 struct cfs_rq *cfs_rq;
9047 struct sched_entity *se = &curr->se;
9048
9049 for_each_sched_entity(se) {
9050 cfs_rq = cfs_rq_of(se);
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01009051 entity_tick(cfs_rq, se, queued);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02009052 }
Ben Segall18bf2802012-10-04 12:51:20 +02009053
Srikar Dronamrajub52da862015-10-02 07:48:25 +05309054 if (static_branch_unlikely(&sched_numa_balancing))
Peter Zijlstracbee9f82012-10-25 14:16:43 +02009055 task_tick_numa(rq, curr);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02009056}
9057
9058/*
Peter Zijlstracd29fe62009-11-27 17:32:46 +01009059 * called on fork with the child task as argument from the parent's context
9060 * - child not yet on the tasklist
9061 * - preemption disabled
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02009062 */
Peter Zijlstracd29fe62009-11-27 17:32:46 +01009063static void task_fork_fair(struct task_struct *p)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02009064{
Daisuke Nishimura4fc420c2011-12-15 14:36:55 +09009065 struct cfs_rq *cfs_rq;
9066 struct sched_entity *se = &p->se, *curr;
Peter Zijlstracd29fe62009-11-27 17:32:46 +01009067 struct rq *rq = this_rq();
Peter Zijlstra8a8c69c2016-10-04 16:04:35 +02009068 struct rq_flags rf;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02009069
Peter Zijlstra8a8c69c2016-10-04 16:04:35 +02009070 rq_lock(rq, &rf);
Peter Zijlstra861d0342010-08-19 13:31:43 +02009071 update_rq_clock(rq);
9072
Daisuke Nishimura4fc420c2011-12-15 14:36:55 +09009073 cfs_rq = task_cfs_rq(current);
9074 curr = cfs_rq->curr;
Peter Zijlstrae210bff2016-06-16 18:51:48 +02009075 if (curr) {
9076 update_curr(cfs_rq);
Mike Galbraithb5d9d732009-09-08 11:12:28 +02009077 se->vruntime = curr->vruntime;
Peter Zijlstrae210bff2016-06-16 18:51:48 +02009078 }
Peter Zijlstraaeb73b02007-10-15 17:00:05 +02009079 place_entity(cfs_rq, se, 1);
Peter Zijlstra4d78e7b2007-10-15 17:00:04 +02009080
Peter Zijlstracd29fe62009-11-27 17:32:46 +01009081 if (sysctl_sched_child_runs_first && curr && entity_before(curr, se)) {
Dmitry Adamushko87fefa32007-10-15 17:00:08 +02009082 /*
Ingo Molnaredcb60a2007-10-15 17:00:08 +02009083 * Upon rescheduling, sched_class::put_prev_task() will place
9084 * 'current' within the tree based on its new key value.
9085 */
Peter Zijlstra4d78e7b2007-10-15 17:00:04 +02009086 swap(curr->vruntime, se->vruntime);
Kirill Tkhai88751252014-06-29 00:03:57 +04009087 resched_curr(rq);
Peter Zijlstra4d78e7b2007-10-15 17:00:04 +02009088 }
9089
Peter Zijlstra88ec22d2009-12-16 18:04:41 +01009090 se->vruntime -= cfs_rq->min_vruntime;
Peter Zijlstra8a8c69c2016-10-04 16:04:35 +02009091 rq_unlock(rq, &rf);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02009092}
9093
Steven Rostedtcb469842008-01-25 21:08:22 +01009094/*
9095 * Priority of the task has changed. Check to see if we preempt
9096 * the current task.
9097 */
Peter Zijlstrada7a7352011-01-17 17:03:27 +01009098static void
9099prio_changed_fair(struct rq *rq, struct task_struct *p, int oldprio)
Steven Rostedtcb469842008-01-25 21:08:22 +01009100{
Kirill Tkhaida0c1e62014-08-20 13:47:32 +04009101 if (!task_on_rq_queued(p))
Peter Zijlstrada7a7352011-01-17 17:03:27 +01009102 return;
9103
Steven Rostedtcb469842008-01-25 21:08:22 +01009104 /*
9105 * Reschedule if we are currently running on this runqueue and
9106 * our priority decreased, or if we are not currently running on
9107 * this runqueue and our priority is higher than the current's
9108 */
Peter Zijlstrada7a7352011-01-17 17:03:27 +01009109 if (rq->curr == p) {
Steven Rostedtcb469842008-01-25 21:08:22 +01009110 if (p->prio > oldprio)
Kirill Tkhai88751252014-06-29 00:03:57 +04009111 resched_curr(rq);
Steven Rostedtcb469842008-01-25 21:08:22 +01009112 } else
Peter Zijlstra15afe092008-09-20 23:38:02 +02009113 check_preempt_curr(rq, p, 0);
Steven Rostedtcb469842008-01-25 21:08:22 +01009114}
9115
Byungchul Parkdaa59402015-08-20 20:22:00 +09009116static inline bool vruntime_normalized(struct task_struct *p)
9117{
9118 struct sched_entity *se = &p->se;
9119
9120 /*
9121 * In both the TASK_ON_RQ_QUEUED and TASK_ON_RQ_MIGRATING cases,
9122 * the dequeue_entity(.flags=0) will already have normalized the
9123 * vruntime.
9124 */
9125 if (p->on_rq)
9126 return true;
9127
9128 /*
9129 * When !on_rq, vruntime of the task has usually NOT been normalized.
9130 * But there are some cases where it has already been normalized:
9131 *
9132 * - A forked child which is waiting for being woken up by
9133 * wake_up_new_task().
9134 * - A task which has been woken up by try_to_wake_up() and
9135 * waiting for actually being woken up by sched_ttwu_pending().
9136 */
9137 if (!se->sum_exec_runtime || p->state == TASK_WAKING)
9138 return true;
9139
9140 return false;
9141}
9142
Vincent Guittot09a43ac2016-11-08 10:53:45 +01009143#ifdef CONFIG_FAIR_GROUP_SCHED
9144/*
9145 * Propagate the changes of the sched_entity across the tg tree to make it
9146 * visible to the root
9147 */
9148static void propagate_entity_cfs_rq(struct sched_entity *se)
9149{
9150 struct cfs_rq *cfs_rq;
9151
9152 /* Start to propagate at parent */
9153 se = se->parent;
9154
9155 for_each_sched_entity(se) {
9156 cfs_rq = cfs_rq_of(se);
9157
9158 if (cfs_rq_throttled(cfs_rq))
9159 break;
9160
9161 update_load_avg(se, UPDATE_TG);
9162 }
9163}
9164#else
9165static void propagate_entity_cfs_rq(struct sched_entity *se) { }
9166#endif
9167
Vincent Guittotdf217912016-11-08 10:53:42 +01009168static void detach_entity_cfs_rq(struct sched_entity *se)
Peter Zijlstrada7a7352011-01-17 17:03:27 +01009169{
Peter Zijlstrada7a7352011-01-17 17:03:27 +01009170 struct cfs_rq *cfs_rq = cfs_rq_of(se);
9171
Yuyang Du9d89c252015-07-15 08:04:37 +08009172 /* Catch up with the cfs_rq and remove our load when we leave */
Vincent Guittotd31b1a62016-11-08 10:53:44 +01009173 update_load_avg(se, 0);
Byungchul Parka05e8c52015-08-20 20:21:56 +09009174 detach_entity_load_avg(cfs_rq, se);
Peter Zijlstra7c3edd22016-07-13 10:56:25 +02009175 update_tg_load_avg(cfs_rq, false);
Vincent Guittot09a43ac2016-11-08 10:53:45 +01009176 propagate_entity_cfs_rq(se);
Peter Zijlstrada7a7352011-01-17 17:03:27 +01009177}
9178
Vincent Guittotdf217912016-11-08 10:53:42 +01009179static void attach_entity_cfs_rq(struct sched_entity *se)
Steven Rostedtcb469842008-01-25 21:08:22 +01009180{
Byungchul Parkdaa59402015-08-20 20:22:00 +09009181 struct cfs_rq *cfs_rq = cfs_rq_of(se);
Byungchul Park7855a352015-08-10 18:02:55 +09009182
9183#ifdef CONFIG_FAIR_GROUP_SCHED
Michael wangeb7a59b2014-02-20 11:14:53 +08009184 /*
9185 * Since the real-depth could have been changed (only FAIR
9186 * class maintain depth value), reset depth properly.
9187 */
9188 se->depth = se->parent ? se->parent->depth + 1 : 0;
9189#endif
Byungchul Park7855a352015-08-10 18:02:55 +09009190
Vincent Guittotdf217912016-11-08 10:53:42 +01009191 /* Synchronize entity with its cfs_rq */
Vincent Guittotd31b1a62016-11-08 10:53:44 +01009192 update_load_avg(se, sched_feat(ATTACH_AGE_LOAD) ? 0 : SKIP_AGE_LOAD);
Byungchul Parkdaa59402015-08-20 20:22:00 +09009193 attach_entity_load_avg(cfs_rq, se);
Peter Zijlstra7c3edd22016-07-13 10:56:25 +02009194 update_tg_load_avg(cfs_rq, false);
Vincent Guittot09a43ac2016-11-08 10:53:45 +01009195 propagate_entity_cfs_rq(se);
Vincent Guittotdf217912016-11-08 10:53:42 +01009196}
9197
9198static void detach_task_cfs_rq(struct task_struct *p)
9199{
9200 struct sched_entity *se = &p->se;
9201 struct cfs_rq *cfs_rq = cfs_rq_of(se);
9202
9203 if (!vruntime_normalized(p)) {
9204 /*
9205 * Fix up our vruntime so that the current sleep doesn't
9206 * cause 'unlimited' sleep bonus.
9207 */
9208 place_entity(cfs_rq, se, 0);
9209 se->vruntime -= cfs_rq->min_vruntime;
9210 }
9211
9212 detach_entity_cfs_rq(se);
9213}
9214
9215static void attach_task_cfs_rq(struct task_struct *p)
9216{
9217 struct sched_entity *se = &p->se;
9218 struct cfs_rq *cfs_rq = cfs_rq_of(se);
9219
9220 attach_entity_cfs_rq(se);
Byungchul Park6efdb102015-08-20 20:21:59 +09009221
Byungchul Parkdaa59402015-08-20 20:22:00 +09009222 if (!vruntime_normalized(p))
9223 se->vruntime += cfs_rq->min_vruntime;
9224}
Byungchul Park7855a352015-08-10 18:02:55 +09009225
Byungchul Parkdaa59402015-08-20 20:22:00 +09009226static void switched_from_fair(struct rq *rq, struct task_struct *p)
9227{
9228 detach_task_cfs_rq(p);
9229}
9230
9231static void switched_to_fair(struct rq *rq, struct task_struct *p)
9232{
9233 attach_task_cfs_rq(p);
9234
9235 if (task_on_rq_queued(p)) {
Byungchul Park7855a352015-08-10 18:02:55 +09009236 /*
Byungchul Parkdaa59402015-08-20 20:22:00 +09009237 * We were most likely switched from sched_rt, so
9238 * kick off the schedule if running, otherwise just see
9239 * if we can still preempt the current task.
Byungchul Park7855a352015-08-10 18:02:55 +09009240 */
Byungchul Parkdaa59402015-08-20 20:22:00 +09009241 if (rq->curr == p)
9242 resched_curr(rq);
9243 else
9244 check_preempt_curr(rq, p, 0);
Byungchul Park7855a352015-08-10 18:02:55 +09009245 }
Steven Rostedtcb469842008-01-25 21:08:22 +01009246}
9247
Srivatsa Vaddagiri83b699e2007-10-15 17:00:08 +02009248/* Account for a task changing its policy or group.
9249 *
9250 * This routine is mostly called to set cfs_rq->curr field when a task
9251 * migrates between groups/classes.
9252 */
9253static void set_curr_task_fair(struct rq *rq)
9254{
9255 struct sched_entity *se = &rq->curr->se;
9256
Paul Turnerec12cb72011-07-21 09:43:30 -07009257 for_each_sched_entity(se) {
9258 struct cfs_rq *cfs_rq = cfs_rq_of(se);
9259
9260 set_next_entity(cfs_rq, se);
9261 /* ensure bandwidth has been allocated on our new cfs_rq */
9262 account_cfs_rq_runtime(cfs_rq, 0);
9263 }
Srivatsa Vaddagiri83b699e2007-10-15 17:00:08 +02009264}
9265
Peter Zijlstra029632f2011-10-25 10:00:11 +02009266void init_cfs_rq(struct cfs_rq *cfs_rq)
9267{
Davidlohr Buesobfb06882017-09-08 16:14:55 -07009268 cfs_rq->tasks_timeline = RB_ROOT_CACHED;
Peter Zijlstra029632f2011-10-25 10:00:11 +02009269 cfs_rq->min_vruntime = (u64)(-(1LL << 20));
9270#ifndef CONFIG_64BIT
9271 cfs_rq->min_vruntime_copy = cfs_rq->min_vruntime;
9272#endif
Alex Shi141965c2013-06-26 13:05:39 +08009273#ifdef CONFIG_SMP
Vincent Guittot09a43ac2016-11-08 10:53:45 +01009274#ifdef CONFIG_FAIR_GROUP_SCHED
9275 cfs_rq->propagate_avg = 0;
9276#endif
Yuyang Du9d89c252015-07-15 08:04:37 +08009277 atomic_long_set(&cfs_rq->removed_load_avg, 0);
9278 atomic_long_set(&cfs_rq->removed_util_avg, 0);
Paul Turner9ee474f2012-10-04 13:18:30 +02009279#endif
Peter Zijlstra029632f2011-10-25 10:00:11 +02009280}
9281
Peter Zijlstra810b3812008-02-29 15:21:01 -05009282#ifdef CONFIG_FAIR_GROUP_SCHED
Vincent Guittotea86cb42016-06-17 13:38:55 +02009283static void task_set_group_fair(struct task_struct *p)
9284{
9285 struct sched_entity *se = &p->se;
9286
9287 set_task_rq(p, task_cpu(p));
9288 se->depth = se->parent ? se->parent->depth + 1 : 0;
9289}
9290
Peter Zijlstrabc54da22015-08-31 17:13:55 +02009291static void task_move_group_fair(struct task_struct *p)
Peter Zijlstra810b3812008-02-29 15:21:01 -05009292{
Byungchul Parkdaa59402015-08-20 20:22:00 +09009293 detach_task_cfs_rq(p);
Peter Zijlstrab2b5ce02010-10-15 15:24:15 +02009294 set_task_rq(p, task_cpu(p));
Byungchul Park6efdb102015-08-20 20:21:59 +09009295
9296#ifdef CONFIG_SMP
9297 /* Tell se's cfs_rq has been changed -- migrated */
9298 p->se.avg.last_update_time = 0;
9299#endif
Byungchul Parkdaa59402015-08-20 20:22:00 +09009300 attach_task_cfs_rq(p);
Peter Zijlstra810b3812008-02-29 15:21:01 -05009301}
Peter Zijlstra029632f2011-10-25 10:00:11 +02009302
Vincent Guittotea86cb42016-06-17 13:38:55 +02009303static void task_change_group_fair(struct task_struct *p, int type)
9304{
9305 switch (type) {
9306 case TASK_SET_GROUP:
9307 task_set_group_fair(p);
9308 break;
9309
9310 case TASK_MOVE_GROUP:
9311 task_move_group_fair(p);
9312 break;
9313 }
9314}
9315
Peter Zijlstra029632f2011-10-25 10:00:11 +02009316void free_fair_sched_group(struct task_group *tg)
9317{
9318 int i;
9319
9320 destroy_cfs_bandwidth(tg_cfs_bandwidth(tg));
9321
9322 for_each_possible_cpu(i) {
9323 if (tg->cfs_rq)
9324 kfree(tg->cfs_rq[i]);
Peter Zijlstra6fe1f342016-01-21 22:24:16 +01009325 if (tg->se)
Peter Zijlstra029632f2011-10-25 10:00:11 +02009326 kfree(tg->se[i]);
9327 }
9328
9329 kfree(tg->cfs_rq);
9330 kfree(tg->se);
9331}
9332
9333int alloc_fair_sched_group(struct task_group *tg, struct task_group *parent)
9334{
Peter Zijlstra029632f2011-10-25 10:00:11 +02009335 struct sched_entity *se;
Peter Zijlstrab7fa30c2016-06-09 15:07:50 +02009336 struct cfs_rq *cfs_rq;
Peter Zijlstra029632f2011-10-25 10:00:11 +02009337 int i;
9338
9339 tg->cfs_rq = kzalloc(sizeof(cfs_rq) * nr_cpu_ids, GFP_KERNEL);
9340 if (!tg->cfs_rq)
9341 goto err;
9342 tg->se = kzalloc(sizeof(se) * nr_cpu_ids, GFP_KERNEL);
9343 if (!tg->se)
9344 goto err;
9345
9346 tg->shares = NICE_0_LOAD;
9347
9348 init_cfs_bandwidth(tg_cfs_bandwidth(tg));
9349
9350 for_each_possible_cpu(i) {
9351 cfs_rq = kzalloc_node(sizeof(struct cfs_rq),
9352 GFP_KERNEL, cpu_to_node(i));
9353 if (!cfs_rq)
9354 goto err;
9355
9356 se = kzalloc_node(sizeof(struct sched_entity),
9357 GFP_KERNEL, cpu_to_node(i));
9358 if (!se)
9359 goto err_free_rq;
9360
9361 init_cfs_rq(cfs_rq);
9362 init_tg_cfs_entry(tg, cfs_rq, se, i, parent->se[i]);
Yuyang Du540247f2015-07-15 08:04:39 +08009363 init_entity_runnable_average(se);
Peter Zijlstra029632f2011-10-25 10:00:11 +02009364 }
9365
9366 return 1;
9367
9368err_free_rq:
9369 kfree(cfs_rq);
9370err:
9371 return 0;
9372}
9373
Peter Zijlstra8663e242016-06-22 14:58:02 +02009374void online_fair_sched_group(struct task_group *tg)
9375{
9376 struct sched_entity *se;
9377 struct rq *rq;
9378 int i;
9379
9380 for_each_possible_cpu(i) {
9381 rq = cpu_rq(i);
9382 se = tg->se[i];
9383
9384 raw_spin_lock_irq(&rq->lock);
Peter Zijlstra4126bad2016-10-03 16:20:59 +02009385 update_rq_clock(rq);
Vincent Guittotd0326692016-11-08 10:53:47 +01009386 attach_entity_cfs_rq(se);
Peter Zijlstra55e16d32016-06-22 15:14:26 +02009387 sync_throttle(tg, i);
Peter Zijlstra8663e242016-06-22 14:58:02 +02009388 raw_spin_unlock_irq(&rq->lock);
9389 }
9390}
9391
Peter Zijlstra6fe1f342016-01-21 22:24:16 +01009392void unregister_fair_sched_group(struct task_group *tg)
Peter Zijlstra029632f2011-10-25 10:00:11 +02009393{
Peter Zijlstra029632f2011-10-25 10:00:11 +02009394 unsigned long flags;
Peter Zijlstra6fe1f342016-01-21 22:24:16 +01009395 struct rq *rq;
9396 int cpu;
Peter Zijlstra029632f2011-10-25 10:00:11 +02009397
Peter Zijlstra6fe1f342016-01-21 22:24:16 +01009398 for_each_possible_cpu(cpu) {
9399 if (tg->se[cpu])
9400 remove_entity_load_avg(tg->se[cpu]);
Peter Zijlstra029632f2011-10-25 10:00:11 +02009401
Peter Zijlstra6fe1f342016-01-21 22:24:16 +01009402 /*
9403 * Only empty task groups can be destroyed; so we can speculatively
9404 * check on_list without danger of it being re-added.
9405 */
9406 if (!tg->cfs_rq[cpu]->on_list)
9407 continue;
9408
9409 rq = cpu_rq(cpu);
9410
9411 raw_spin_lock_irqsave(&rq->lock, flags);
9412 list_del_leaf_cfs_rq(tg->cfs_rq[cpu]);
9413 raw_spin_unlock_irqrestore(&rq->lock, flags);
9414 }
Peter Zijlstra029632f2011-10-25 10:00:11 +02009415}
9416
9417void init_tg_cfs_entry(struct task_group *tg, struct cfs_rq *cfs_rq,
9418 struct sched_entity *se, int cpu,
9419 struct sched_entity *parent)
9420{
9421 struct rq *rq = cpu_rq(cpu);
9422
9423 cfs_rq->tg = tg;
9424 cfs_rq->rq = rq;
Peter Zijlstra029632f2011-10-25 10:00:11 +02009425 init_cfs_rq_runtime(cfs_rq);
9426
9427 tg->cfs_rq[cpu] = cfs_rq;
9428 tg->se[cpu] = se;
9429
9430 /* se could be NULL for root_task_group */
9431 if (!se)
9432 return;
9433
Peter Zijlstrafed14d42012-02-11 06:05:00 +01009434 if (!parent) {
Peter Zijlstra029632f2011-10-25 10:00:11 +02009435 se->cfs_rq = &rq->cfs;
Peter Zijlstrafed14d42012-02-11 06:05:00 +01009436 se->depth = 0;
9437 } else {
Peter Zijlstra029632f2011-10-25 10:00:11 +02009438 se->cfs_rq = parent->my_q;
Peter Zijlstrafed14d42012-02-11 06:05:00 +01009439 se->depth = parent->depth + 1;
9440 }
Peter Zijlstra029632f2011-10-25 10:00:11 +02009441
9442 se->my_q = cfs_rq;
Paul Turner0ac9b1c2013-10-16 11:16:27 -07009443 /* guarantee group entities always have weight */
9444 update_load_set(&se->load, NICE_0_LOAD);
Peter Zijlstra029632f2011-10-25 10:00:11 +02009445 se->parent = parent;
9446}
9447
9448static DEFINE_MUTEX(shares_mutex);
9449
9450int sched_group_set_shares(struct task_group *tg, unsigned long shares)
9451{
9452 int i;
Peter Zijlstra029632f2011-10-25 10:00:11 +02009453
9454 /*
9455 * We can't change the weight of the root cgroup.
9456 */
9457 if (!tg->se[0])
9458 return -EINVAL;
9459
9460 shares = clamp(shares, scale_load(MIN_SHARES), scale_load(MAX_SHARES));
9461
9462 mutex_lock(&shares_mutex);
9463 if (tg->shares == shares)
9464 goto done;
9465
9466 tg->shares = shares;
9467 for_each_possible_cpu(i) {
9468 struct rq *rq = cpu_rq(i);
Peter Zijlstra8a8c69c2016-10-04 16:04:35 +02009469 struct sched_entity *se = tg->se[i];
9470 struct rq_flags rf;
Peter Zijlstra029632f2011-10-25 10:00:11 +02009471
Peter Zijlstra029632f2011-10-25 10:00:11 +02009472 /* Propagate contribution to hierarchy */
Peter Zijlstra8a8c69c2016-10-04 16:04:35 +02009473 rq_lock_irqsave(rq, &rf);
Frederic Weisbecker71b1da42013-04-12 01:50:59 +02009474 update_rq_clock(rq);
Vincent Guittot89ee0482016-12-21 16:50:26 +01009475 for_each_sched_entity(se) {
9476 update_load_avg(se, UPDATE_TG);
9477 update_cfs_shares(se);
9478 }
Peter Zijlstra8a8c69c2016-10-04 16:04:35 +02009479 rq_unlock_irqrestore(rq, &rf);
Peter Zijlstra029632f2011-10-25 10:00:11 +02009480 }
9481
9482done:
9483 mutex_unlock(&shares_mutex);
9484 return 0;
9485}
9486#else /* CONFIG_FAIR_GROUP_SCHED */
9487
9488void free_fair_sched_group(struct task_group *tg) { }
9489
9490int alloc_fair_sched_group(struct task_group *tg, struct task_group *parent)
9491{
9492 return 1;
9493}
9494
Peter Zijlstra8663e242016-06-22 14:58:02 +02009495void online_fair_sched_group(struct task_group *tg) { }
9496
Peter Zijlstra6fe1f342016-01-21 22:24:16 +01009497void unregister_fair_sched_group(struct task_group *tg) { }
Peter Zijlstra029632f2011-10-25 10:00:11 +02009498
9499#endif /* CONFIG_FAIR_GROUP_SCHED */
9500
Peter Zijlstra810b3812008-02-29 15:21:01 -05009501
H Hartley Sweeten6d686f42010-01-13 20:21:52 -07009502static unsigned int get_rr_interval_fair(struct rq *rq, struct task_struct *task)
Peter Williams0d721ce2009-09-21 01:31:53 +00009503{
9504 struct sched_entity *se = &task->se;
Peter Williams0d721ce2009-09-21 01:31:53 +00009505 unsigned int rr_interval = 0;
9506
9507 /*
9508 * Time slice is 0 for SCHED_OTHER tasks that are on an otherwise
9509 * idle runqueue:
9510 */
Peter Williams0d721ce2009-09-21 01:31:53 +00009511 if (rq->cfs.load.weight)
Zhu Yanhaia59f4e02013-01-08 12:56:52 +08009512 rr_interval = NS_TO_JIFFIES(sched_slice(cfs_rq_of(se), se));
Peter Williams0d721ce2009-09-21 01:31:53 +00009513
9514 return rr_interval;
9515}
9516
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02009517/*
9518 * All the scheduling class methods:
9519 */
Peter Zijlstra029632f2011-10-25 10:00:11 +02009520const struct sched_class fair_sched_class = {
Ingo Molnar5522d5d2007-10-15 17:00:12 +02009521 .next = &idle_sched_class,
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02009522 .enqueue_task = enqueue_task_fair,
9523 .dequeue_task = dequeue_task_fair,
9524 .yield_task = yield_task_fair,
Mike Galbraithd95f4122011-02-01 09:50:51 -05009525 .yield_to_task = yield_to_task_fair,
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02009526
Ingo Molnar2e09bf52007-10-15 17:00:05 +02009527 .check_preempt_curr = check_preempt_wakeup,
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02009528
9529 .pick_next_task = pick_next_task_fair,
9530 .put_prev_task = put_prev_task_fair,
9531
Peter Williams681f3e62007-10-24 18:23:51 +02009532#ifdef CONFIG_SMP
Li Zefan4ce72a22008-10-22 15:25:26 +08009533 .select_task_rq = select_task_rq_fair,
Paul Turner0a74bef2012-10-04 13:18:30 +02009534 .migrate_task_rq = migrate_task_rq_fair,
Alex Shi141965c2013-06-26 13:05:39 +08009535
Christian Ehrhardt0bcdcf22009-11-30 12:16:46 +01009536 .rq_online = rq_online_fair,
9537 .rq_offline = rq_offline_fair,
Peter Zijlstra88ec22d2009-12-16 18:04:41 +01009538
Yuyang Du12695572015-07-15 08:04:40 +08009539 .task_dead = task_dead_fair,
Peter Zijlstrac5b28032015-05-15 17:43:35 +02009540 .set_cpus_allowed = set_cpus_allowed_common,
Peter Williams681f3e62007-10-24 18:23:51 +02009541#endif
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02009542
Srivatsa Vaddagiri83b699e2007-10-15 17:00:08 +02009543 .set_curr_task = set_curr_task_fair,
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02009544 .task_tick = task_tick_fair,
Peter Zijlstracd29fe62009-11-27 17:32:46 +01009545 .task_fork = task_fork_fair,
Steven Rostedtcb469842008-01-25 21:08:22 +01009546
9547 .prio_changed = prio_changed_fair,
Peter Zijlstrada7a7352011-01-17 17:03:27 +01009548 .switched_from = switched_from_fair,
Steven Rostedtcb469842008-01-25 21:08:22 +01009549 .switched_to = switched_to_fair,
Peter Zijlstra810b3812008-02-29 15:21:01 -05009550
Peter Williams0d721ce2009-09-21 01:31:53 +00009551 .get_rr_interval = get_rr_interval_fair,
9552
Stanislaw Gruszka6e998912014-11-12 16:58:44 +01009553 .update_curr = update_curr_fair,
9554
Peter Zijlstra810b3812008-02-29 15:21:01 -05009555#ifdef CONFIG_FAIR_GROUP_SCHED
Vincent Guittotea86cb42016-06-17 13:38:55 +02009556 .task_change_group = task_change_group_fair,
Peter Zijlstra810b3812008-02-29 15:21:01 -05009557#endif
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02009558};
9559
9560#ifdef CONFIG_SCHED_DEBUG
Peter Zijlstra029632f2011-10-25 10:00:11 +02009561void print_cfs_stats(struct seq_file *m, int cpu)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02009562{
Tejun Heoa9e7f652017-04-25 17:43:50 -07009563 struct cfs_rq *cfs_rq, *pos;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02009564
Peter Zijlstra5973e5b2008-01-25 21:08:34 +01009565 rcu_read_lock();
Tejun Heoa9e7f652017-04-25 17:43:50 -07009566 for_each_leaf_cfs_rq_safe(cpu_rq(cpu), cfs_rq, pos)
Ingo Molnar5cef9ec2007-08-09 11:16:47 +02009567 print_cfs_rq(m, cpu, cfs_rq);
Peter Zijlstra5973e5b2008-01-25 21:08:34 +01009568 rcu_read_unlock();
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02009569}
Srikar Dronamraju397f2372015-06-25 22:51:43 +05309570
9571#ifdef CONFIG_NUMA_BALANCING
9572void show_numa_stats(struct task_struct *p, struct seq_file *m)
9573{
9574 int node;
9575 unsigned long tsf = 0, tpf = 0, gsf = 0, gpf = 0;
9576
9577 for_each_online_node(node) {
9578 if (p->numa_faults) {
9579 tsf = p->numa_faults[task_faults_idx(NUMA_MEM, node, 0)];
9580 tpf = p->numa_faults[task_faults_idx(NUMA_MEM, node, 1)];
9581 }
9582 if (p->numa_group) {
9583 gsf = p->numa_group->faults[task_faults_idx(NUMA_MEM, node, 0)],
9584 gpf = p->numa_group->faults[task_faults_idx(NUMA_MEM, node, 1)];
9585 }
9586 print_numa_stats(m, node, tsf, tpf, gsf, gpf);
9587 }
9588}
9589#endif /* CONFIG_NUMA_BALANCING */
9590#endif /* CONFIG_SCHED_DEBUG */
Peter Zijlstra029632f2011-10-25 10:00:11 +02009591
9592__init void init_sched_fair_class(void)
9593{
9594#ifdef CONFIG_SMP
9595 open_softirq(SCHED_SOFTIRQ, run_rebalance_domains);
9596
Frederic Weisbecker3451d022011-08-10 23:21:01 +02009597#ifdef CONFIG_NO_HZ_COMMON
Diwakar Tundlam554ceca2012-03-07 14:44:26 -08009598 nohz.next_balance = jiffies;
Peter Zijlstra029632f2011-10-25 10:00:11 +02009599 zalloc_cpumask_var(&nohz.idle_cpus_mask, GFP_NOWAIT);
Peter Zijlstra029632f2011-10-25 10:00:11 +02009600#endif
9601#endif /* SMP */
9602
9603}