blob: cf3e816db80f3cafa964d5e48553131e69f22231 [file] [log] [blame]
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001/*
2 * Completely Fair Scheduling (CFS) Class (SCHED_NORMAL/SCHED_BATCH)
3 *
4 * Copyright (C) 2007 Red Hat, Inc., Ingo Molnar <mingo@redhat.com>
5 *
6 * Interactivity improvements by Mike Galbraith
7 * (C) 2007 Mike Galbraith <efault@gmx.de>
8 *
9 * Various enhancements by Dmitry Adamushko.
10 * (C) 2007 Dmitry Adamushko <dmitry.adamushko@gmail.com>
11 *
12 * Group scheduling enhancements by Srivatsa Vaddagiri
13 * Copyright IBM Corporation, 2007
14 * Author: Srivatsa Vaddagiri <vatsa@linux.vnet.ibm.com>
15 *
16 * Scaled math optimizations by Thomas Gleixner
17 * Copyright (C) 2007, Thomas Gleixner <tglx@linutronix.de>
Peter Zijlstra21805082007-08-25 18:41:53 +020018 *
19 * Adaptive scheduling granularity, math enhancements by Peter Zijlstra
Peter Zijlstra90eec102015-11-16 11:08:45 +010020 * Copyright (C) 2007 Red Hat, Inc., Peter Zijlstra
Ingo Molnarbf0f6f22007-07-09 18:51:58 +020021 */
22
Ingo Molnar589ee622017-02-04 00:16:44 +010023#include <linux/sched/mm.h>
Ingo Molnar105ab3d2017-02-01 16:36:40 +010024#include <linux/sched/topology.h>
25
Mel Gormancb251762016-02-05 09:08:36 +000026#include <linux/latencytop.h>
Sisir Koppaka3436ae12011-03-26 18:22:55 +053027#include <linux/cpumask.h>
Nicolas Pitre83a0a962014-09-04 11:32:10 -040028#include <linux/cpuidle.h>
Peter Zijlstra029632f2011-10-25 10:00:11 +020029#include <linux/slab.h>
30#include <linux/profile.h>
31#include <linux/interrupt.h>
Peter Zijlstracbee9f82012-10-25 14:16:43 +020032#include <linux/mempolicy.h>
Mel Gormane14808b2012-11-19 10:59:15 +000033#include <linux/migrate.h>
Peter Zijlstracbee9f82012-10-25 14:16:43 +020034#include <linux/task_work.h>
Peter Zijlstra029632f2011-10-25 10:00:11 +020035
36#include <trace/events/sched.h>
37
38#include "sched.h"
Arjan van de Ven97455122008-01-25 21:08:34 +010039
Ingo Molnarbf0f6f22007-07-09 18:51:58 +020040/*
Peter Zijlstra21805082007-08-25 18:41:53 +020041 * Targeted preemption latency for CPU-bound tasks:
Ingo Molnarbf0f6f22007-07-09 18:51:58 +020042 *
Peter Zijlstra21805082007-08-25 18:41:53 +020043 * NOTE: this latency value is not the same as the concept of
Ingo Molnard274a4c2007-10-15 17:00:14 +020044 * 'timeslice length' - timeslices in CFS are of variable length
45 * and have no persistent notion like in traditional, time-slice
46 * based scheduling concepts.
Ingo Molnarbf0f6f22007-07-09 18:51:58 +020047 *
Ingo Molnard274a4c2007-10-15 17:00:14 +020048 * (to see the precise effective timeslice length of your workload,
49 * run vmstat and monitor the context-switches (cs) field)
Ingo Molnar2b4d5b22016-11-23 07:37:00 +010050 *
51 * (default: 6ms * (1 + ilog(ncpus)), units: nanoseconds)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +020052 */
Ingo Molnar2b4d5b22016-11-23 07:37:00 +010053unsigned int sysctl_sched_latency = 6000000ULL;
54unsigned int normalized_sysctl_sched_latency = 6000000ULL;
Ingo Molnar2bd8e6d2007-10-15 17:00:02 +020055
56/*
Christian Ehrhardt1983a922009-11-30 12:16:47 +010057 * The initial- and re-scaling of tunables is configurable
Christian Ehrhardt1983a922009-11-30 12:16:47 +010058 *
59 * Options are:
Ingo Molnar2b4d5b22016-11-23 07:37:00 +010060 *
61 * SCHED_TUNABLESCALING_NONE - unscaled, always *1
62 * SCHED_TUNABLESCALING_LOG - scaled logarithmical, *1+ilog(ncpus)
63 * SCHED_TUNABLESCALING_LINEAR - scaled linear, *ncpus
64 *
65 * (default SCHED_TUNABLESCALING_LOG = *(1+ilog(ncpus))
Christian Ehrhardt1983a922009-11-30 12:16:47 +010066 */
Ingo Molnar2b4d5b22016-11-23 07:37:00 +010067enum sched_tunable_scaling sysctl_sched_tunable_scaling = SCHED_TUNABLESCALING_LOG;
Christian Ehrhardt1983a922009-11-30 12:16:47 +010068
69/*
Peter Zijlstrab2be5e92007-11-09 22:39:37 +010070 * Minimal preemption granularity for CPU-bound tasks:
Ingo Molnar2b4d5b22016-11-23 07:37:00 +010071 *
Takuya Yoshikawa864616e2010-10-14 16:09:13 +090072 * (default: 0.75 msec * (1 + ilog(ncpus)), units: nanoseconds)
Peter Zijlstrab2be5e92007-11-09 22:39:37 +010073 */
Ingo Molnar2b4d5b22016-11-23 07:37:00 +010074unsigned int sysctl_sched_min_granularity = 750000ULL;
75unsigned int normalized_sysctl_sched_min_granularity = 750000ULL;
Peter Zijlstrab2be5e92007-11-09 22:39:37 +010076
77/*
Ingo Molnar2b4d5b22016-11-23 07:37:00 +010078 * This value is kept at sysctl_sched_latency/sysctl_sched_min_granularity
Peter Zijlstrab2be5e92007-11-09 22:39:37 +010079 */
Ingo Molnar0bf377b2010-09-12 08:14:52 +020080static unsigned int sched_nr_latency = 8;
Peter Zijlstrab2be5e92007-11-09 22:39:37 +010081
82/*
Mike Galbraith2bba22c2009-09-09 15:41:37 +020083 * After fork, child runs first. If set to 0 (default) then
Ingo Molnar2bd8e6d2007-10-15 17:00:02 +020084 * parent will (try to) run first.
85 */
Mike Galbraith2bba22c2009-09-09 15:41:37 +020086unsigned int sysctl_sched_child_runs_first __read_mostly;
Peter Zijlstra21805082007-08-25 18:41:53 +020087
88/*
Ingo Molnarbf0f6f22007-07-09 18:51:58 +020089 * SCHED_OTHER wake-up granularity.
Ingo Molnarbf0f6f22007-07-09 18:51:58 +020090 *
91 * This option delays the preemption effects of decoupled workloads
92 * and reduces their over-scheduling. Synchronous workloads will still
93 * have immediate wakeup/sleep latencies.
Ingo Molnar2b4d5b22016-11-23 07:37:00 +010094 *
95 * (default: 1 msec * (1 + ilog(ncpus)), units: nanoseconds)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +020096 */
Ingo Molnar2b4d5b22016-11-23 07:37:00 +010097unsigned int sysctl_sched_wakeup_granularity = 1000000UL;
98unsigned int normalized_sysctl_sched_wakeup_granularity = 1000000UL;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +020099
Ingo Molnar2b4d5b22016-11-23 07:37:00 +0100100const_debug unsigned int sysctl_sched_migration_cost = 500000UL;
Ingo Molnarda84d962007-10-15 17:00:18 +0200101
Tim Chenafe06ef2016-11-22 12:23:53 -0800102#ifdef CONFIG_SMP
103/*
104 * For asym packing, by default the lower numbered cpu has higher priority.
105 */
106int __weak arch_asym_cpu_priority(int cpu)
107{
108 return -cpu;
109}
110#endif
111
Paul Turnerec12cb72011-07-21 09:43:30 -0700112#ifdef CONFIG_CFS_BANDWIDTH
113/*
114 * Amount of runtime to allocate from global (tg) to local (per-cfs_rq) pool
115 * each time a cfs_rq requests quota.
116 *
117 * Note: in the case that the slice exceeds the runtime remaining (either due
118 * to consumption or the quota being specified to be smaller than the slice)
119 * we will always only issue the remaining available time.
120 *
Ingo Molnar2b4d5b22016-11-23 07:37:00 +0100121 * (default: 5 msec, units: microseconds)
122 */
123unsigned int sysctl_sched_cfs_bandwidth_slice = 5000UL;
Paul Turnerec12cb72011-07-21 09:43:30 -0700124#endif
125
Morten Rasmussen32731632016-07-25 14:34:26 +0100126/*
127 * The margin used when comparing utilization with CPU capacity:
Morten Rasmussen893c5d22016-10-14 14:41:12 +0100128 * util * margin < capacity * 1024
Ingo Molnar2b4d5b22016-11-23 07:37:00 +0100129 *
130 * (default: ~20%)
Morten Rasmussen32731632016-07-25 14:34:26 +0100131 */
Ingo Molnar2b4d5b22016-11-23 07:37:00 +0100132unsigned int capacity_margin = 1280;
Morten Rasmussen32731632016-07-25 14:34:26 +0100133
Paul Gortmaker85276322013-04-19 15:10:50 -0400134static inline void update_load_add(struct load_weight *lw, unsigned long inc)
135{
136 lw->weight += inc;
137 lw->inv_weight = 0;
138}
139
140static inline void update_load_sub(struct load_weight *lw, unsigned long dec)
141{
142 lw->weight -= dec;
143 lw->inv_weight = 0;
144}
145
146static inline void update_load_set(struct load_weight *lw, unsigned long w)
147{
148 lw->weight = w;
149 lw->inv_weight = 0;
150}
151
Peter Zijlstra029632f2011-10-25 10:00:11 +0200152/*
153 * Increase the granularity value when there are more CPUs,
154 * because with more CPUs the 'effective latency' as visible
155 * to users decreases. But the relationship is not linear,
156 * so pick a second-best guess by going with the log2 of the
157 * number of CPUs.
158 *
159 * This idea comes from the SD scheduler of Con Kolivas:
160 */
Nicholas Mc Guire58ac93e2015-05-15 21:05:42 +0200161static unsigned int get_update_sysctl_factor(void)
Peter Zijlstra029632f2011-10-25 10:00:11 +0200162{
Nicholas Mc Guire58ac93e2015-05-15 21:05:42 +0200163 unsigned int cpus = min_t(unsigned int, num_online_cpus(), 8);
Peter Zijlstra029632f2011-10-25 10:00:11 +0200164 unsigned int factor;
165
166 switch (sysctl_sched_tunable_scaling) {
167 case SCHED_TUNABLESCALING_NONE:
168 factor = 1;
169 break;
170 case SCHED_TUNABLESCALING_LINEAR:
171 factor = cpus;
172 break;
173 case SCHED_TUNABLESCALING_LOG:
174 default:
175 factor = 1 + ilog2(cpus);
176 break;
177 }
178
179 return factor;
180}
181
182static void update_sysctl(void)
183{
184 unsigned int factor = get_update_sysctl_factor();
185
186#define SET_SYSCTL(name) \
187 (sysctl_##name = (factor) * normalized_sysctl_##name)
188 SET_SYSCTL(sched_min_granularity);
189 SET_SYSCTL(sched_latency);
190 SET_SYSCTL(sched_wakeup_granularity);
191#undef SET_SYSCTL
192}
193
194void sched_init_granularity(void)
195{
196 update_sysctl();
197}
198
Peter Zijlstra9dbdb152013-11-18 18:27:06 +0100199#define WMULT_CONST (~0U)
Peter Zijlstra029632f2011-10-25 10:00:11 +0200200#define WMULT_SHIFT 32
201
Peter Zijlstra9dbdb152013-11-18 18:27:06 +0100202static void __update_inv_weight(struct load_weight *lw)
Peter Zijlstra029632f2011-10-25 10:00:11 +0200203{
Peter Zijlstra9dbdb152013-11-18 18:27:06 +0100204 unsigned long w;
Peter Zijlstra029632f2011-10-25 10:00:11 +0200205
Peter Zijlstra9dbdb152013-11-18 18:27:06 +0100206 if (likely(lw->inv_weight))
207 return;
208
209 w = scale_load_down(lw->weight);
210
211 if (BITS_PER_LONG > 32 && unlikely(w >= WMULT_CONST))
212 lw->inv_weight = 1;
213 else if (unlikely(!w))
214 lw->inv_weight = WMULT_CONST;
Peter Zijlstra029632f2011-10-25 10:00:11 +0200215 else
Peter Zijlstra9dbdb152013-11-18 18:27:06 +0100216 lw->inv_weight = WMULT_CONST / w;
217}
Peter Zijlstra029632f2011-10-25 10:00:11 +0200218
Peter Zijlstra9dbdb152013-11-18 18:27:06 +0100219/*
220 * delta_exec * weight / lw.weight
221 * OR
222 * (delta_exec * (weight * lw->inv_weight)) >> WMULT_SHIFT
223 *
Yuyang Du1c3de5e2016-03-30 07:07:51 +0800224 * Either weight := NICE_0_LOAD and lw \e sched_prio_to_wmult[], in which case
Peter Zijlstra9dbdb152013-11-18 18:27:06 +0100225 * we're guaranteed shift stays positive because inv_weight is guaranteed to
226 * fit 32 bits, and NICE_0_LOAD gives another 10 bits; therefore shift >= 22.
227 *
228 * Or, weight =< lw.weight (because lw.weight is the runqueue weight), thus
229 * weight/lw.weight <= 1, and therefore our shift will also be positive.
230 */
231static u64 __calc_delta(u64 delta_exec, unsigned long weight, struct load_weight *lw)
232{
233 u64 fact = scale_load_down(weight);
234 int shift = WMULT_SHIFT;
Peter Zijlstra029632f2011-10-25 10:00:11 +0200235
Peter Zijlstra9dbdb152013-11-18 18:27:06 +0100236 __update_inv_weight(lw);
237
238 if (unlikely(fact >> 32)) {
239 while (fact >> 32) {
240 fact >>= 1;
241 shift--;
242 }
Peter Zijlstra029632f2011-10-25 10:00:11 +0200243 }
244
Peter Zijlstra9dbdb152013-11-18 18:27:06 +0100245 /* hint to use a 32x32->64 mul */
246 fact = (u64)(u32)fact * lw->inv_weight;
Peter Zijlstra029632f2011-10-25 10:00:11 +0200247
Peter Zijlstra9dbdb152013-11-18 18:27:06 +0100248 while (fact >> 32) {
249 fact >>= 1;
250 shift--;
251 }
252
253 return mul_u64_u32_shr(delta_exec, fact, shift);
Peter Zijlstra029632f2011-10-25 10:00:11 +0200254}
255
256
257const struct sched_class fair_sched_class;
Peter Zijlstraa4c2f002008-10-17 19:27:03 +0200258
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200259/**************************************************************
260 * CFS operations on generic schedulable entities:
261 */
262
263#ifdef CONFIG_FAIR_GROUP_SCHED
264
265/* cpu runqueue to which this cfs_rq is attached */
266static inline struct rq *rq_of(struct cfs_rq *cfs_rq)
267{
268 return cfs_rq->rq;
269}
270
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200271/* An entity is a task if it doesn't "own" a runqueue */
272#define entity_is_task(se) (!se->my_q)
273
Peter Zijlstra8f488942009-07-24 12:25:30 +0200274static inline struct task_struct *task_of(struct sched_entity *se)
275{
Peter Zijlstra9148a3a2016-09-20 22:34:51 +0200276 SCHED_WARN_ON(!entity_is_task(se));
Peter Zijlstra8f488942009-07-24 12:25:30 +0200277 return container_of(se, struct task_struct, se);
278}
279
Peter Zijlstrab7581492008-04-19 19:45:00 +0200280/* Walk up scheduling entities hierarchy */
281#define for_each_sched_entity(se) \
282 for (; se; se = se->parent)
283
284static inline struct cfs_rq *task_cfs_rq(struct task_struct *p)
285{
286 return p->se.cfs_rq;
287}
288
289/* runqueue on which this entity is (to be) queued */
290static inline struct cfs_rq *cfs_rq_of(struct sched_entity *se)
291{
292 return se->cfs_rq;
293}
294
295/* runqueue "owned" by this group */
296static inline struct cfs_rq *group_cfs_rq(struct sched_entity *grp)
297{
298 return grp->my_q;
299}
300
Peter Zijlstra3d4b47b2010-11-15 15:47:01 -0800301static inline void list_add_leaf_cfs_rq(struct cfs_rq *cfs_rq)
302{
303 if (!cfs_rq->on_list) {
Vincent Guittot9c2791f2016-11-08 10:53:43 +0100304 struct rq *rq = rq_of(cfs_rq);
305 int cpu = cpu_of(rq);
Paul Turner67e86252010-11-15 15:47:05 -0800306 /*
307 * Ensure we either appear before our parent (if already
308 * enqueued) or force our parent to appear after us when it is
Vincent Guittot9c2791f2016-11-08 10:53:43 +0100309 * enqueued. The fact that we always enqueue bottom-up
310 * reduces this to two cases and a special case for the root
311 * cfs_rq. Furthermore, it also means that we will always reset
312 * tmp_alone_branch either when the branch is connected
313 * to a tree or when we reach the beg of the tree
Paul Turner67e86252010-11-15 15:47:05 -0800314 */
315 if (cfs_rq->tg->parent &&
Vincent Guittot9c2791f2016-11-08 10:53:43 +0100316 cfs_rq->tg->parent->cfs_rq[cpu]->on_list) {
317 /*
318 * If parent is already on the list, we add the child
319 * just before. Thanks to circular linked property of
320 * the list, this means to put the child at the tail
321 * of the list that starts by parent.
322 */
Paul Turner67e86252010-11-15 15:47:05 -0800323 list_add_tail_rcu(&cfs_rq->leaf_cfs_rq_list,
Vincent Guittot9c2791f2016-11-08 10:53:43 +0100324 &(cfs_rq->tg->parent->cfs_rq[cpu]->leaf_cfs_rq_list));
325 /*
326 * The branch is now connected to its tree so we can
327 * reset tmp_alone_branch to the beginning of the
328 * list.
329 */
330 rq->tmp_alone_branch = &rq->leaf_cfs_rq_list;
331 } else if (!cfs_rq->tg->parent) {
332 /*
333 * cfs rq without parent should be put
334 * at the tail of the list.
335 */
336 list_add_tail_rcu(&cfs_rq->leaf_cfs_rq_list,
337 &rq->leaf_cfs_rq_list);
338 /*
339 * We have reach the beg of a tree so we can reset
340 * tmp_alone_branch to the beginning of the list.
341 */
342 rq->tmp_alone_branch = &rq->leaf_cfs_rq_list;
343 } else {
344 /*
345 * The parent has not already been added so we want to
346 * make sure that it will be put after us.
347 * tmp_alone_branch points to the beg of the branch
348 * where we will add parent.
349 */
350 list_add_rcu(&cfs_rq->leaf_cfs_rq_list,
351 rq->tmp_alone_branch);
352 /*
353 * update tmp_alone_branch to points to the new beg
354 * of the branch
355 */
356 rq->tmp_alone_branch = &cfs_rq->leaf_cfs_rq_list;
Paul Turner67e86252010-11-15 15:47:05 -0800357 }
Peter Zijlstra3d4b47b2010-11-15 15:47:01 -0800358
359 cfs_rq->on_list = 1;
360 }
361}
362
363static inline void list_del_leaf_cfs_rq(struct cfs_rq *cfs_rq)
364{
365 if (cfs_rq->on_list) {
366 list_del_rcu(&cfs_rq->leaf_cfs_rq_list);
367 cfs_rq->on_list = 0;
368 }
369}
370
Peter Zijlstrab7581492008-04-19 19:45:00 +0200371/* Iterate thr' all leaf cfs_rq's on a runqueue */
Tejun Heoa9e7f652017-04-25 17:43:50 -0700372#define for_each_leaf_cfs_rq_safe(rq, cfs_rq, pos) \
373 list_for_each_entry_safe(cfs_rq, pos, &rq->leaf_cfs_rq_list, \
374 leaf_cfs_rq_list)
Peter Zijlstrab7581492008-04-19 19:45:00 +0200375
376/* Do the two (enqueued) entities belong to the same group ? */
Peter Zijlstrafed14d42012-02-11 06:05:00 +0100377static inline struct cfs_rq *
Peter Zijlstrab7581492008-04-19 19:45:00 +0200378is_same_group(struct sched_entity *se, struct sched_entity *pse)
379{
380 if (se->cfs_rq == pse->cfs_rq)
Peter Zijlstrafed14d42012-02-11 06:05:00 +0100381 return se->cfs_rq;
Peter Zijlstrab7581492008-04-19 19:45:00 +0200382
Peter Zijlstrafed14d42012-02-11 06:05:00 +0100383 return NULL;
Peter Zijlstrab7581492008-04-19 19:45:00 +0200384}
385
386static inline struct sched_entity *parent_entity(struct sched_entity *se)
387{
388 return se->parent;
389}
390
Peter Zijlstra464b7522008-10-24 11:06:15 +0200391static void
392find_matching_se(struct sched_entity **se, struct sched_entity **pse)
393{
394 int se_depth, pse_depth;
395
396 /*
397 * preemption test can be made between sibling entities who are in the
398 * same cfs_rq i.e who have a common parent. Walk up the hierarchy of
399 * both tasks until we find their ancestors who are siblings of common
400 * parent.
401 */
402
403 /* First walk up until both entities are at same depth */
Peter Zijlstrafed14d42012-02-11 06:05:00 +0100404 se_depth = (*se)->depth;
405 pse_depth = (*pse)->depth;
Peter Zijlstra464b7522008-10-24 11:06:15 +0200406
407 while (se_depth > pse_depth) {
408 se_depth--;
409 *se = parent_entity(*se);
410 }
411
412 while (pse_depth > se_depth) {
413 pse_depth--;
414 *pse = parent_entity(*pse);
415 }
416
417 while (!is_same_group(*se, *pse)) {
418 *se = parent_entity(*se);
419 *pse = parent_entity(*pse);
420 }
421}
422
Peter Zijlstra8f488942009-07-24 12:25:30 +0200423#else /* !CONFIG_FAIR_GROUP_SCHED */
424
425static inline struct task_struct *task_of(struct sched_entity *se)
426{
427 return container_of(se, struct task_struct, se);
428}
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200429
430static inline struct rq *rq_of(struct cfs_rq *cfs_rq)
431{
432 return container_of(cfs_rq, struct rq, cfs);
433}
434
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200435#define entity_is_task(se) 1
436
Peter Zijlstrab7581492008-04-19 19:45:00 +0200437#define for_each_sched_entity(se) \
438 for (; se; se = NULL)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200439
Peter Zijlstrab7581492008-04-19 19:45:00 +0200440static inline struct cfs_rq *task_cfs_rq(struct task_struct *p)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200441{
Peter Zijlstrab7581492008-04-19 19:45:00 +0200442 return &task_rq(p)->cfs;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200443}
444
Peter Zijlstrab7581492008-04-19 19:45:00 +0200445static inline struct cfs_rq *cfs_rq_of(struct sched_entity *se)
446{
447 struct task_struct *p = task_of(se);
448 struct rq *rq = task_rq(p);
449
450 return &rq->cfs;
451}
452
453/* runqueue "owned" by this group */
454static inline struct cfs_rq *group_cfs_rq(struct sched_entity *grp)
455{
456 return NULL;
457}
458
Peter Zijlstra3d4b47b2010-11-15 15:47:01 -0800459static inline void list_add_leaf_cfs_rq(struct cfs_rq *cfs_rq)
460{
461}
462
463static inline void list_del_leaf_cfs_rq(struct cfs_rq *cfs_rq)
464{
465}
466
Tejun Heoa9e7f652017-04-25 17:43:50 -0700467#define for_each_leaf_cfs_rq_safe(rq, cfs_rq, pos) \
468 for (cfs_rq = &rq->cfs, pos = NULL; cfs_rq; cfs_rq = pos)
Peter Zijlstrab7581492008-04-19 19:45:00 +0200469
Peter Zijlstrab7581492008-04-19 19:45:00 +0200470static inline struct sched_entity *parent_entity(struct sched_entity *se)
471{
472 return NULL;
473}
474
Peter Zijlstra464b7522008-10-24 11:06:15 +0200475static inline void
476find_matching_se(struct sched_entity **se, struct sched_entity **pse)
477{
478}
479
Peter Zijlstrab7581492008-04-19 19:45:00 +0200480#endif /* CONFIG_FAIR_GROUP_SCHED */
481
Peter Zijlstra6c16a6d2012-03-21 13:07:16 -0700482static __always_inline
Peter Zijlstra9dbdb152013-11-18 18:27:06 +0100483void account_cfs_rq_runtime(struct cfs_rq *cfs_rq, u64 delta_exec);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200484
485/**************************************************************
486 * Scheduling class tree data structure manipulation methods:
487 */
488
Andrei Epure1bf08232013-03-12 21:12:24 +0200489static inline u64 max_vruntime(u64 max_vruntime, u64 vruntime)
Peter Zijlstra02e04312007-10-15 17:00:07 +0200490{
Andrei Epure1bf08232013-03-12 21:12:24 +0200491 s64 delta = (s64)(vruntime - max_vruntime);
Peter Zijlstra368059a2007-10-15 17:00:11 +0200492 if (delta > 0)
Andrei Epure1bf08232013-03-12 21:12:24 +0200493 max_vruntime = vruntime;
Peter Zijlstra02e04312007-10-15 17:00:07 +0200494
Andrei Epure1bf08232013-03-12 21:12:24 +0200495 return max_vruntime;
Peter Zijlstra02e04312007-10-15 17:00:07 +0200496}
497
Ingo Molnar0702e3e2007-10-15 17:00:14 +0200498static inline u64 min_vruntime(u64 min_vruntime, u64 vruntime)
Peter Zijlstrab0ffd242007-10-15 17:00:12 +0200499{
500 s64 delta = (s64)(vruntime - min_vruntime);
501 if (delta < 0)
502 min_vruntime = vruntime;
503
504 return min_vruntime;
505}
506
Fabio Checconi54fdc582009-07-16 12:32:27 +0200507static inline int entity_before(struct sched_entity *a,
508 struct sched_entity *b)
509{
510 return (s64)(a->vruntime - b->vruntime) < 0;
511}
512
Peter Zijlstra1af5f732008-10-24 11:06:13 +0200513static void update_min_vruntime(struct cfs_rq *cfs_rq)
514{
Peter Zijlstrab60205c2016-09-20 21:58:12 +0200515 struct sched_entity *curr = cfs_rq->curr;
Davidlohr Buesobfb06882017-09-08 16:14:55 -0700516 struct rb_node *leftmost = rb_first_cached(&cfs_rq->tasks_timeline);
Peter Zijlstrab60205c2016-09-20 21:58:12 +0200517
Peter Zijlstra1af5f732008-10-24 11:06:13 +0200518 u64 vruntime = cfs_rq->min_vruntime;
519
Peter Zijlstrab60205c2016-09-20 21:58:12 +0200520 if (curr) {
521 if (curr->on_rq)
522 vruntime = curr->vruntime;
523 else
524 curr = NULL;
525 }
Peter Zijlstra1af5f732008-10-24 11:06:13 +0200526
Davidlohr Buesobfb06882017-09-08 16:14:55 -0700527 if (leftmost) { /* non-empty tree */
528 struct sched_entity *se;
529 se = rb_entry(leftmost, struct sched_entity, run_node);
Peter Zijlstra1af5f732008-10-24 11:06:13 +0200530
Peter Zijlstrab60205c2016-09-20 21:58:12 +0200531 if (!curr)
Peter Zijlstra1af5f732008-10-24 11:06:13 +0200532 vruntime = se->vruntime;
533 else
534 vruntime = min_vruntime(vruntime, se->vruntime);
535 }
536
Andrei Epure1bf08232013-03-12 21:12:24 +0200537 /* ensure we never gain time by being placed backwards. */
Peter Zijlstra1af5f732008-10-24 11:06:13 +0200538 cfs_rq->min_vruntime = max_vruntime(cfs_rq->min_vruntime, vruntime);
Peter Zijlstra3fe16982011-04-05 17:23:48 +0200539#ifndef CONFIG_64BIT
540 smp_wmb();
541 cfs_rq->min_vruntime_copy = cfs_rq->min_vruntime;
542#endif
Peter Zijlstra1af5f732008-10-24 11:06:13 +0200543}
544
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200545/*
546 * Enqueue an entity into the rb-tree:
547 */
Ingo Molnar0702e3e2007-10-15 17:00:14 +0200548static void __enqueue_entity(struct cfs_rq *cfs_rq, struct sched_entity *se)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200549{
Davidlohr Buesobfb06882017-09-08 16:14:55 -0700550 struct rb_node **link = &cfs_rq->tasks_timeline.rb_root.rb_node;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200551 struct rb_node *parent = NULL;
552 struct sched_entity *entry;
Davidlohr Buesobfb06882017-09-08 16:14:55 -0700553 bool leftmost = true;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200554
555 /*
556 * Find the right place in the rbtree:
557 */
558 while (*link) {
559 parent = *link;
560 entry = rb_entry(parent, struct sched_entity, run_node);
561 /*
562 * We dont care about collisions. Nodes with
563 * the same key stay together.
564 */
Stephan Baerwolf2bd2d6f2011-07-20 14:46:59 +0200565 if (entity_before(se, entry)) {
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200566 link = &parent->rb_left;
567 } else {
568 link = &parent->rb_right;
Davidlohr Buesobfb06882017-09-08 16:14:55 -0700569 leftmost = false;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200570 }
571 }
572
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200573 rb_link_node(&se->run_node, parent, link);
Davidlohr Buesobfb06882017-09-08 16:14:55 -0700574 rb_insert_color_cached(&se->run_node,
575 &cfs_rq->tasks_timeline, leftmost);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200576}
577
Ingo Molnar0702e3e2007-10-15 17:00:14 +0200578static void __dequeue_entity(struct cfs_rq *cfs_rq, struct sched_entity *se)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200579{
Davidlohr Buesobfb06882017-09-08 16:14:55 -0700580 rb_erase_cached(&se->run_node, &cfs_rq->tasks_timeline);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200581}
582
Peter Zijlstra029632f2011-10-25 10:00:11 +0200583struct sched_entity *__pick_first_entity(struct cfs_rq *cfs_rq)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200584{
Davidlohr Buesobfb06882017-09-08 16:14:55 -0700585 struct rb_node *left = rb_first_cached(&cfs_rq->tasks_timeline);
Peter Zijlstraf4b67552008-11-04 21:25:07 +0100586
587 if (!left)
588 return NULL;
589
590 return rb_entry(left, struct sched_entity, run_node);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200591}
592
Rik van Rielac53db52011-02-01 09:51:03 -0500593static struct sched_entity *__pick_next_entity(struct sched_entity *se)
594{
595 struct rb_node *next = rb_next(&se->run_node);
596
597 if (!next)
598 return NULL;
599
600 return rb_entry(next, struct sched_entity, run_node);
601}
602
603#ifdef CONFIG_SCHED_DEBUG
Peter Zijlstra029632f2011-10-25 10:00:11 +0200604struct sched_entity *__pick_last_entity(struct cfs_rq *cfs_rq)
Peter Zijlstraaeb73b02007-10-15 17:00:05 +0200605{
Davidlohr Buesobfb06882017-09-08 16:14:55 -0700606 struct rb_node *last = rb_last(&cfs_rq->tasks_timeline.rb_root);
Peter Zijlstraaeb73b02007-10-15 17:00:05 +0200607
Balbir Singh70eee742008-02-22 13:25:53 +0530608 if (!last)
609 return NULL;
Ingo Molnar7eee3e62008-02-22 10:32:21 +0100610
611 return rb_entry(last, struct sched_entity, run_node);
Peter Zijlstraaeb73b02007-10-15 17:00:05 +0200612}
613
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200614/**************************************************************
615 * Scheduling class statistics methods:
616 */
617
Christian Ehrhardtacb4a842009-11-30 12:16:48 +0100618int sched_proc_update_handler(struct ctl_table *table, int write,
Alexey Dobriyan8d65af72009-09-23 15:57:19 -0700619 void __user *buffer, size_t *lenp,
Peter Zijlstrab2be5e92007-11-09 22:39:37 +0100620 loff_t *ppos)
621{
Alexey Dobriyan8d65af72009-09-23 15:57:19 -0700622 int ret = proc_dointvec_minmax(table, write, buffer, lenp, ppos);
Nicholas Mc Guire58ac93e2015-05-15 21:05:42 +0200623 unsigned int factor = get_update_sysctl_factor();
Peter Zijlstrab2be5e92007-11-09 22:39:37 +0100624
625 if (ret || !write)
626 return ret;
627
628 sched_nr_latency = DIV_ROUND_UP(sysctl_sched_latency,
629 sysctl_sched_min_granularity);
630
Christian Ehrhardtacb4a842009-11-30 12:16:48 +0100631#define WRT_SYSCTL(name) \
632 (normalized_sysctl_##name = sysctl_##name / (factor))
633 WRT_SYSCTL(sched_min_granularity);
634 WRT_SYSCTL(sched_latency);
635 WRT_SYSCTL(sched_wakeup_granularity);
Christian Ehrhardtacb4a842009-11-30 12:16:48 +0100636#undef WRT_SYSCTL
637
Peter Zijlstrab2be5e92007-11-09 22:39:37 +0100638 return 0;
639}
640#endif
Ingo Molnar647e7ca2007-10-15 17:00:13 +0200641
642/*
Peter Zijlstraf9c0b092008-10-17 19:27:04 +0200643 * delta /= w
Peter Zijlstraa7be37a2008-06-27 13:41:11 +0200644 */
Peter Zijlstra9dbdb152013-11-18 18:27:06 +0100645static inline u64 calc_delta_fair(u64 delta, struct sched_entity *se)
Peter Zijlstraa7be37a2008-06-27 13:41:11 +0200646{
Peter Zijlstraf9c0b092008-10-17 19:27:04 +0200647 if (unlikely(se->load.weight != NICE_0_LOAD))
Peter Zijlstra9dbdb152013-11-18 18:27:06 +0100648 delta = __calc_delta(delta, NICE_0_LOAD, &se->load);
Peter Zijlstraa7be37a2008-06-27 13:41:11 +0200649
650 return delta;
651}
652
653/*
Ingo Molnar647e7ca2007-10-15 17:00:13 +0200654 * The idea is to set a period in which each task runs once.
655 *
Borislav Petkov532b1852012-08-08 16:16:04 +0200656 * When there are too many tasks (sched_nr_latency) we have to stretch
Ingo Molnar647e7ca2007-10-15 17:00:13 +0200657 * this period because otherwise the slices get too small.
658 *
659 * p = (nr <= nl) ? l : l*nr/nl
660 */
Peter Zijlstra4d78e7b2007-10-15 17:00:04 +0200661static u64 __sched_period(unsigned long nr_running)
662{
Boqun Feng8e2b0bf2015-07-02 22:25:52 +0800663 if (unlikely(nr_running > sched_nr_latency))
664 return nr_running * sysctl_sched_min_granularity;
665 else
666 return sysctl_sched_latency;
Peter Zijlstra4d78e7b2007-10-15 17:00:04 +0200667}
668
Ingo Molnar647e7ca2007-10-15 17:00:13 +0200669/*
670 * We calculate the wall-time slice from the period by taking a part
671 * proportional to the weight.
672 *
Peter Zijlstraf9c0b092008-10-17 19:27:04 +0200673 * s = p*P[w/rw]
Ingo Molnar647e7ca2007-10-15 17:00:13 +0200674 */
Peter Zijlstra6d0f0eb2007-10-15 17:00:05 +0200675static u64 sched_slice(struct cfs_rq *cfs_rq, struct sched_entity *se)
Peter Zijlstra21805082007-08-25 18:41:53 +0200676{
Mike Galbraith0a582442009-01-02 12:16:42 +0100677 u64 slice = __sched_period(cfs_rq->nr_running + !se->on_rq);
Peter Zijlstraf9c0b092008-10-17 19:27:04 +0200678
Mike Galbraith0a582442009-01-02 12:16:42 +0100679 for_each_sched_entity(se) {
Lin Ming6272d682009-01-15 17:17:15 +0100680 struct load_weight *load;
Christian Engelmayer3104bf02009-06-16 10:35:12 +0200681 struct load_weight lw;
Lin Ming6272d682009-01-15 17:17:15 +0100682
683 cfs_rq = cfs_rq_of(se);
684 load = &cfs_rq->load;
Peter Zijlstraf9c0b092008-10-17 19:27:04 +0200685
Mike Galbraith0a582442009-01-02 12:16:42 +0100686 if (unlikely(!se->on_rq)) {
Christian Engelmayer3104bf02009-06-16 10:35:12 +0200687 lw = cfs_rq->load;
Mike Galbraith0a582442009-01-02 12:16:42 +0100688
689 update_load_add(&lw, se->load.weight);
690 load = &lw;
691 }
Peter Zijlstra9dbdb152013-11-18 18:27:06 +0100692 slice = __calc_delta(slice, se->load.weight, load);
Mike Galbraith0a582442009-01-02 12:16:42 +0100693 }
694 return slice;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200695}
696
Ingo Molnar647e7ca2007-10-15 17:00:13 +0200697/*
Andrei Epure660cc002013-03-11 12:03:20 +0200698 * We calculate the vruntime slice of a to-be-inserted task.
Ingo Molnar647e7ca2007-10-15 17:00:13 +0200699 *
Peter Zijlstraf9c0b092008-10-17 19:27:04 +0200700 * vs = s/w
Ingo Molnar647e7ca2007-10-15 17:00:13 +0200701 */
Peter Zijlstraf9c0b092008-10-17 19:27:04 +0200702static u64 sched_vslice(struct cfs_rq *cfs_rq, struct sched_entity *se)
Ingo Molnar647e7ca2007-10-15 17:00:13 +0200703{
Peter Zijlstraf9c0b092008-10-17 19:27:04 +0200704 return calc_delta_fair(sched_slice(cfs_rq, se), se);
Peter Zijlstraa7be37a2008-06-27 13:41:11 +0200705}
706
Alex Shia75cdaa2013-06-20 10:18:47 +0800707#ifdef CONFIG_SMP
Peter Zijlstra283e2ed2017-04-11 11:08:42 +0200708
709#include "sched-pelt.h"
710
Morten Rasmussen772bd008c2016-06-22 18:03:13 +0100711static int select_idle_sibling(struct task_struct *p, int prev_cpu, int cpu);
Mel Gormanfb13c7e2013-10-07 11:29:17 +0100712static unsigned long task_h_load(struct task_struct *p);
713
Yuyang Du540247f2015-07-15 08:04:39 +0800714/* Give new sched_entity start runnable values to heavy its load in infant time */
715void init_entity_runnable_average(struct sched_entity *se)
Alex Shia75cdaa2013-06-20 10:18:47 +0800716{
Yuyang Du540247f2015-07-15 08:04:39 +0800717 struct sched_avg *sa = &se->avg;
Alex Shia75cdaa2013-06-20 10:18:47 +0800718
Peter Zijlstraf2079342017-05-12 14:16:30 +0200719 memset(sa, 0, sizeof(*sa));
720
Vincent Guittotb5a9b342016-10-19 14:45:23 +0200721 /*
722 * Tasks are intialized with full load to be seen as heavy tasks until
723 * they get a chance to stabilize to their real load level.
724 * Group entities are intialized with zero load to reflect the fact that
725 * nothing has been attached to the task group yet.
726 */
727 if (entity_is_task(se))
Peter Zijlstra1ea6c462017-05-06 15:59:54 +0200728 sa->runnable_load_avg = sa->load_avg = scale_load_down(se->load.weight);
Peter Zijlstra1ea6c462017-05-06 15:59:54 +0200729
Peter Zijlstraf2079342017-05-12 14:16:30 +0200730 se->runnable_weight = se->load.weight;
731
Yuyang Du9d89c252015-07-15 08:04:37 +0800732 /* when this task enqueue'ed, it will contribute to its cfs_rq's load_avg */
Alex Shia75cdaa2013-06-20 10:18:47 +0800733}
Yuyang Du7ea241a2015-07-15 08:04:42 +0800734
Peter Zijlstra7dc603c2016-06-16 13:29:28 +0200735static inline u64 cfs_rq_clock_task(struct cfs_rq *cfs_rq);
Vincent Guittotdf217912016-11-08 10:53:42 +0100736static void attach_entity_cfs_rq(struct sched_entity *se);
Peter Zijlstra7dc603c2016-06-16 13:29:28 +0200737
Yuyang Du2b8c41d2016-03-30 04:30:56 +0800738/*
739 * With new tasks being created, their initial util_avgs are extrapolated
740 * based on the cfs_rq's current util_avg:
741 *
742 * util_avg = cfs_rq->util_avg / (cfs_rq->load_avg + 1) * se.load.weight
743 *
744 * However, in many cases, the above util_avg does not give a desired
745 * value. Moreover, the sum of the util_avgs may be divergent, such
746 * as when the series is a harmonic series.
747 *
748 * To solve this problem, we also cap the util_avg of successive tasks to
749 * only 1/2 of the left utilization budget:
750 *
751 * util_avg_cap = (1024 - cfs_rq->avg.util_avg) / 2^n
752 *
753 * where n denotes the nth task.
754 *
755 * For example, a simplest series from the beginning would be like:
756 *
757 * task util_avg: 512, 256, 128, 64, 32, 16, 8, ...
758 * cfs_rq util_avg: 512, 768, 896, 960, 992, 1008, 1016, ...
759 *
760 * Finally, that extrapolated util_avg is clamped to the cap (util_avg_cap)
761 * if util_avg > util_avg_cap.
762 */
763void post_init_entity_util_avg(struct sched_entity *se)
764{
765 struct cfs_rq *cfs_rq = cfs_rq_of(se);
766 struct sched_avg *sa = &se->avg;
Yuyang Du172895e2016-04-05 12:12:27 +0800767 long cap = (long)(SCHED_CAPACITY_SCALE - cfs_rq->avg.util_avg) / 2;
Yuyang Du2b8c41d2016-03-30 04:30:56 +0800768
769 if (cap > 0) {
770 if (cfs_rq->avg.util_avg != 0) {
771 sa->util_avg = cfs_rq->avg.util_avg * se->load.weight;
772 sa->util_avg /= (cfs_rq->avg.load_avg + 1);
773
774 if (sa->util_avg > cap)
775 sa->util_avg = cap;
776 } else {
777 sa->util_avg = cap;
778 }
Yuyang Du2b8c41d2016-03-30 04:30:56 +0800779 }
Peter Zijlstra7dc603c2016-06-16 13:29:28 +0200780
781 if (entity_is_task(se)) {
782 struct task_struct *p = task_of(se);
783 if (p->sched_class != &fair_sched_class) {
784 /*
785 * For !fair tasks do:
786 *
Viresh Kumar3a123bb2017-05-24 10:59:56 +0530787 update_cfs_rq_load_avg(now, cfs_rq);
Peter Zijlstra7dc603c2016-06-16 13:29:28 +0200788 attach_entity_load_avg(cfs_rq, se);
789 switched_from_fair(rq, p);
790 *
791 * such that the next switched_to_fair() has the
792 * expected state.
793 */
Vincent Guittotdf217912016-11-08 10:53:42 +0100794 se->avg.last_update_time = cfs_rq_clock_task(cfs_rq);
Peter Zijlstra7dc603c2016-06-16 13:29:28 +0200795 return;
796 }
797 }
798
Vincent Guittotdf217912016-11-08 10:53:42 +0100799 attach_entity_cfs_rq(se);
Yuyang Du2b8c41d2016-03-30 04:30:56 +0800800}
801
Peter Zijlstra7dc603c2016-06-16 13:29:28 +0200802#else /* !CONFIG_SMP */
Yuyang Du540247f2015-07-15 08:04:39 +0800803void init_entity_runnable_average(struct sched_entity *se)
Alex Shia75cdaa2013-06-20 10:18:47 +0800804{
805}
Yuyang Du2b8c41d2016-03-30 04:30:56 +0800806void post_init_entity_util_avg(struct sched_entity *se)
807{
808}
Peter Zijlstra3d30544f2016-06-21 14:27:50 +0200809static void update_tg_load_avg(struct cfs_rq *cfs_rq, int force)
810{
811}
Peter Zijlstra7dc603c2016-06-16 13:29:28 +0200812#endif /* CONFIG_SMP */
Alex Shia75cdaa2013-06-20 10:18:47 +0800813
Peter Zijlstraa7be37a2008-06-27 13:41:11 +0200814/*
Peter Zijlstra9dbdb152013-11-18 18:27:06 +0100815 * Update the current task's runtime statistics.
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200816 */
Ingo Molnarb7cc0892007-08-09 11:16:47 +0200817static void update_curr(struct cfs_rq *cfs_rq)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200818{
Ingo Molnar429d43b2007-10-15 17:00:03 +0200819 struct sched_entity *curr = cfs_rq->curr;
Frederic Weisbecker78becc22013-04-12 01:51:02 +0200820 u64 now = rq_clock_task(rq_of(cfs_rq));
Peter Zijlstra9dbdb152013-11-18 18:27:06 +0100821 u64 delta_exec;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200822
823 if (unlikely(!curr))
824 return;
825
Peter Zijlstra9dbdb152013-11-18 18:27:06 +0100826 delta_exec = now - curr->exec_start;
827 if (unlikely((s64)delta_exec <= 0))
Peter Zijlstra34f28ec2008-12-16 08:45:31 +0100828 return;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200829
Ingo Molnar8ebc91d2007-10-15 17:00:03 +0200830 curr->exec_start = now;
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +0100831
Peter Zijlstra9dbdb152013-11-18 18:27:06 +0100832 schedstat_set(curr->statistics.exec_max,
833 max(delta_exec, curr->statistics.exec_max));
834
835 curr->sum_exec_runtime += delta_exec;
Josh Poimboeufae928822016-06-17 12:43:24 -0500836 schedstat_add(cfs_rq->exec_clock, delta_exec);
Peter Zijlstra9dbdb152013-11-18 18:27:06 +0100837
838 curr->vruntime += calc_delta_fair(delta_exec, curr);
839 update_min_vruntime(cfs_rq);
840
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +0100841 if (entity_is_task(curr)) {
842 struct task_struct *curtask = task_of(curr);
843
Ingo Molnarf977bb42009-09-13 18:15:54 +0200844 trace_sched_stat_runtime(curtask, delta_exec, curr->vruntime);
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +0100845 cpuacct_charge(curtask, delta_exec);
Frank Mayharf06febc2008-09-12 09:54:39 -0700846 account_group_exec_runtime(curtask, delta_exec);
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +0100847 }
Paul Turnerec12cb72011-07-21 09:43:30 -0700848
849 account_cfs_rq_runtime(cfs_rq, delta_exec);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200850}
851
Stanislaw Gruszka6e998912014-11-12 16:58:44 +0100852static void update_curr_fair(struct rq *rq)
853{
854 update_curr(cfs_rq_of(&rq->curr->se));
855}
856
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200857static inline void
Ingo Molnar5870db52007-08-09 11:16:47 +0200858update_stats_wait_start(struct cfs_rq *cfs_rq, struct sched_entity *se)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200859{
Josh Poimboeuf4fa8d292016-06-17 12:43:26 -0500860 u64 wait_start, prev_wait_start;
861
862 if (!schedstat_enabled())
863 return;
864
865 wait_start = rq_clock(rq_of(cfs_rq));
866 prev_wait_start = schedstat_val(se->statistics.wait_start);
Joonwoo Park3ea94de2015-11-12 19:38:54 -0800867
868 if (entity_is_task(se) && task_on_rq_migrating(task_of(se)) &&
Josh Poimboeuf4fa8d292016-06-17 12:43:26 -0500869 likely(wait_start > prev_wait_start))
870 wait_start -= prev_wait_start;
Joonwoo Park3ea94de2015-11-12 19:38:54 -0800871
Josh Poimboeuf4fa8d292016-06-17 12:43:26 -0500872 schedstat_set(se->statistics.wait_start, wait_start);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200873}
874
Josh Poimboeuf4fa8d292016-06-17 12:43:26 -0500875static inline void
Joonwoo Park3ea94de2015-11-12 19:38:54 -0800876update_stats_wait_end(struct cfs_rq *cfs_rq, struct sched_entity *se)
877{
878 struct task_struct *p;
Mel Gormancb251762016-02-05 09:08:36 +0000879 u64 delta;
880
Josh Poimboeuf4fa8d292016-06-17 12:43:26 -0500881 if (!schedstat_enabled())
882 return;
883
884 delta = rq_clock(rq_of(cfs_rq)) - schedstat_val(se->statistics.wait_start);
Joonwoo Park3ea94de2015-11-12 19:38:54 -0800885
886 if (entity_is_task(se)) {
887 p = task_of(se);
888 if (task_on_rq_migrating(p)) {
889 /*
890 * Preserve migrating task's wait time so wait_start
891 * time stamp can be adjusted to accumulate wait time
892 * prior to migration.
893 */
Josh Poimboeuf4fa8d292016-06-17 12:43:26 -0500894 schedstat_set(se->statistics.wait_start, delta);
Joonwoo Park3ea94de2015-11-12 19:38:54 -0800895 return;
896 }
897 trace_sched_stat_wait(p, delta);
898 }
899
Josh Poimboeuf4fa8d292016-06-17 12:43:26 -0500900 schedstat_set(se->statistics.wait_max,
901 max(schedstat_val(se->statistics.wait_max), delta));
902 schedstat_inc(se->statistics.wait_count);
903 schedstat_add(se->statistics.wait_sum, delta);
904 schedstat_set(se->statistics.wait_start, 0);
Joonwoo Park3ea94de2015-11-12 19:38:54 -0800905}
Joonwoo Park3ea94de2015-11-12 19:38:54 -0800906
Josh Poimboeuf4fa8d292016-06-17 12:43:26 -0500907static inline void
Josh Poimboeuf1a3d0272016-06-17 12:43:23 -0500908update_stats_enqueue_sleeper(struct cfs_rq *cfs_rq, struct sched_entity *se)
909{
910 struct task_struct *tsk = NULL;
Josh Poimboeuf4fa8d292016-06-17 12:43:26 -0500911 u64 sleep_start, block_start;
912
913 if (!schedstat_enabled())
914 return;
915
916 sleep_start = schedstat_val(se->statistics.sleep_start);
917 block_start = schedstat_val(se->statistics.block_start);
Josh Poimboeuf1a3d0272016-06-17 12:43:23 -0500918
919 if (entity_is_task(se))
920 tsk = task_of(se);
921
Josh Poimboeuf4fa8d292016-06-17 12:43:26 -0500922 if (sleep_start) {
923 u64 delta = rq_clock(rq_of(cfs_rq)) - sleep_start;
Josh Poimboeuf1a3d0272016-06-17 12:43:23 -0500924
925 if ((s64)delta < 0)
926 delta = 0;
927
Josh Poimboeuf4fa8d292016-06-17 12:43:26 -0500928 if (unlikely(delta > schedstat_val(se->statistics.sleep_max)))
929 schedstat_set(se->statistics.sleep_max, delta);
Josh Poimboeuf1a3d0272016-06-17 12:43:23 -0500930
Josh Poimboeuf4fa8d292016-06-17 12:43:26 -0500931 schedstat_set(se->statistics.sleep_start, 0);
932 schedstat_add(se->statistics.sum_sleep_runtime, delta);
Josh Poimboeuf1a3d0272016-06-17 12:43:23 -0500933
934 if (tsk) {
935 account_scheduler_latency(tsk, delta >> 10, 1);
936 trace_sched_stat_sleep(tsk, delta);
937 }
938 }
Josh Poimboeuf4fa8d292016-06-17 12:43:26 -0500939 if (block_start) {
940 u64 delta = rq_clock(rq_of(cfs_rq)) - block_start;
Josh Poimboeuf1a3d0272016-06-17 12:43:23 -0500941
942 if ((s64)delta < 0)
943 delta = 0;
944
Josh Poimboeuf4fa8d292016-06-17 12:43:26 -0500945 if (unlikely(delta > schedstat_val(se->statistics.block_max)))
946 schedstat_set(se->statistics.block_max, delta);
Josh Poimboeuf1a3d0272016-06-17 12:43:23 -0500947
Josh Poimboeuf4fa8d292016-06-17 12:43:26 -0500948 schedstat_set(se->statistics.block_start, 0);
949 schedstat_add(se->statistics.sum_sleep_runtime, delta);
Josh Poimboeuf1a3d0272016-06-17 12:43:23 -0500950
951 if (tsk) {
952 if (tsk->in_iowait) {
Josh Poimboeuf4fa8d292016-06-17 12:43:26 -0500953 schedstat_add(se->statistics.iowait_sum, delta);
954 schedstat_inc(se->statistics.iowait_count);
Josh Poimboeuf1a3d0272016-06-17 12:43:23 -0500955 trace_sched_stat_iowait(tsk, delta);
956 }
957
958 trace_sched_stat_blocked(tsk, delta);
959
960 /*
961 * Blocking time is in units of nanosecs, so shift by
962 * 20 to get a milliseconds-range estimation of the
963 * amount of time that the task spent sleeping:
964 */
965 if (unlikely(prof_on == SLEEP_PROFILING)) {
966 profile_hits(SLEEP_PROFILING,
967 (void *)get_wchan(tsk),
968 delta >> 20);
969 }
970 account_scheduler_latency(tsk, delta >> 10, 0);
971 }
972 }
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200973}
974
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200975/*
976 * Task is being enqueued - update stats:
977 */
Mel Gormancb251762016-02-05 09:08:36 +0000978static inline void
Josh Poimboeuf1a3d0272016-06-17 12:43:23 -0500979update_stats_enqueue(struct cfs_rq *cfs_rq, struct sched_entity *se, int flags)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200980{
Josh Poimboeuf4fa8d292016-06-17 12:43:26 -0500981 if (!schedstat_enabled())
982 return;
983
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200984 /*
985 * Are we enqueueing a waiting task? (for current tasks
986 * a dequeue/enqueue event is a NOP)
987 */
Ingo Molnar429d43b2007-10-15 17:00:03 +0200988 if (se != cfs_rq->curr)
Ingo Molnar5870db52007-08-09 11:16:47 +0200989 update_stats_wait_start(cfs_rq, se);
Josh Poimboeuf1a3d0272016-06-17 12:43:23 -0500990
991 if (flags & ENQUEUE_WAKEUP)
992 update_stats_enqueue_sleeper(cfs_rq, se);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200993}
994
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200995static inline void
Mel Gormancb251762016-02-05 09:08:36 +0000996update_stats_dequeue(struct cfs_rq *cfs_rq, struct sched_entity *se, int flags)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200997{
Josh Poimboeuf4fa8d292016-06-17 12:43:26 -0500998
999 if (!schedstat_enabled())
1000 return;
1001
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001002 /*
1003 * Mark the end of the wait period if dequeueing a
1004 * waiting task:
1005 */
Ingo Molnar429d43b2007-10-15 17:00:03 +02001006 if (se != cfs_rq->curr)
Ingo Molnar9ef0a962007-08-09 11:16:47 +02001007 update_stats_wait_end(cfs_rq, se);
Mel Gormancb251762016-02-05 09:08:36 +00001008
Josh Poimboeuf4fa8d292016-06-17 12:43:26 -05001009 if ((flags & DEQUEUE_SLEEP) && entity_is_task(se)) {
1010 struct task_struct *tsk = task_of(se);
Mel Gormancb251762016-02-05 09:08:36 +00001011
Josh Poimboeuf4fa8d292016-06-17 12:43:26 -05001012 if (tsk->state & TASK_INTERRUPTIBLE)
1013 schedstat_set(se->statistics.sleep_start,
1014 rq_clock(rq_of(cfs_rq)));
1015 if (tsk->state & TASK_UNINTERRUPTIBLE)
1016 schedstat_set(se->statistics.block_start,
1017 rq_clock(rq_of(cfs_rq)));
Mel Gormancb251762016-02-05 09:08:36 +00001018 }
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001019}
1020
1021/*
1022 * We are picking a new current task - update its stats:
1023 */
1024static inline void
Ingo Molnar79303e92007-08-09 11:16:47 +02001025update_stats_curr_start(struct cfs_rq *cfs_rq, struct sched_entity *se)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001026{
1027 /*
1028 * We are starting a new run period:
1029 */
Frederic Weisbecker78becc22013-04-12 01:51:02 +02001030 se->exec_start = rq_clock_task(rq_of(cfs_rq));
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001031}
1032
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001033/**************************************************
1034 * Scheduling class queueing methods:
1035 */
1036
Peter Zijlstracbee9f82012-10-25 14:16:43 +02001037#ifdef CONFIG_NUMA_BALANCING
1038/*
Mel Gorman598f0ec2013-10-07 11:28:55 +01001039 * Approximate time to scan a full NUMA task in ms. The task scan period is
1040 * calculated based on the tasks virtual memory size and
1041 * numa_balancing_scan_size.
Peter Zijlstracbee9f82012-10-25 14:16:43 +02001042 */
Mel Gorman598f0ec2013-10-07 11:28:55 +01001043unsigned int sysctl_numa_balancing_scan_period_min = 1000;
1044unsigned int sysctl_numa_balancing_scan_period_max = 60000;
Peter Zijlstra6e5fb222012-10-25 14:16:45 +02001045
1046/* Portion of address space to scan in MB */
1047unsigned int sysctl_numa_balancing_scan_size = 256;
Peter Zijlstracbee9f82012-10-25 14:16:43 +02001048
Peter Zijlstra4b96a292012-10-25 14:16:47 +02001049/* Scan @scan_size MB every @scan_period after an initial @scan_delay in ms */
1050unsigned int sysctl_numa_balancing_scan_delay = 1000;
1051
Rik van Rielb5dd77c2017-07-31 15:28:47 -04001052struct numa_group {
1053 atomic_t refcount;
1054
1055 spinlock_t lock; /* nr_tasks, tasks */
1056 int nr_tasks;
1057 pid_t gid;
1058 int active_nodes;
1059
1060 struct rcu_head rcu;
1061 unsigned long total_faults;
1062 unsigned long max_faults_cpu;
1063 /*
1064 * Faults_cpu is used to decide whether memory should move
1065 * towards the CPU. As a consequence, these stats are weighted
1066 * more by CPU use than by memory faults.
1067 */
1068 unsigned long *faults_cpu;
1069 unsigned long faults[0];
1070};
1071
1072static inline unsigned long group_faults_priv(struct numa_group *ng);
1073static inline unsigned long group_faults_shared(struct numa_group *ng);
1074
Mel Gorman598f0ec2013-10-07 11:28:55 +01001075static unsigned int task_nr_scan_windows(struct task_struct *p)
1076{
1077 unsigned long rss = 0;
1078 unsigned long nr_scan_pages;
1079
1080 /*
1081 * Calculations based on RSS as non-present and empty pages are skipped
1082 * by the PTE scanner and NUMA hinting faults should be trapped based
1083 * on resident pages
1084 */
1085 nr_scan_pages = sysctl_numa_balancing_scan_size << (20 - PAGE_SHIFT);
1086 rss = get_mm_rss(p->mm);
1087 if (!rss)
1088 rss = nr_scan_pages;
1089
1090 rss = round_up(rss, nr_scan_pages);
1091 return rss / nr_scan_pages;
1092}
1093
1094/* For sanitys sake, never scan more PTEs than MAX_SCAN_WINDOW MB/sec. */
1095#define MAX_SCAN_WINDOW 2560
1096
1097static unsigned int task_scan_min(struct task_struct *p)
1098{
Jason Low316c1608d2015-04-28 13:00:20 -07001099 unsigned int scan_size = READ_ONCE(sysctl_numa_balancing_scan_size);
Mel Gorman598f0ec2013-10-07 11:28:55 +01001100 unsigned int scan, floor;
1101 unsigned int windows = 1;
1102
Kirill Tkhai64192652014-10-16 14:39:37 +04001103 if (scan_size < MAX_SCAN_WINDOW)
1104 windows = MAX_SCAN_WINDOW / scan_size;
Mel Gorman598f0ec2013-10-07 11:28:55 +01001105 floor = 1000 / windows;
1106
1107 scan = sysctl_numa_balancing_scan_period_min / task_nr_scan_windows(p);
1108 return max_t(unsigned int, floor, scan);
1109}
1110
Rik van Rielb5dd77c2017-07-31 15:28:47 -04001111static unsigned int task_scan_start(struct task_struct *p)
1112{
1113 unsigned long smin = task_scan_min(p);
1114 unsigned long period = smin;
1115
1116 /* Scale the maximum scan period with the amount of shared memory. */
1117 if (p->numa_group) {
1118 struct numa_group *ng = p->numa_group;
1119 unsigned long shared = group_faults_shared(ng);
1120 unsigned long private = group_faults_priv(ng);
1121
1122 period *= atomic_read(&ng->refcount);
1123 period *= shared + 1;
1124 period /= private + shared + 1;
1125 }
1126
1127 return max(smin, period);
1128}
1129
Mel Gorman598f0ec2013-10-07 11:28:55 +01001130static unsigned int task_scan_max(struct task_struct *p)
1131{
Rik van Rielb5dd77c2017-07-31 15:28:47 -04001132 unsigned long smin = task_scan_min(p);
1133 unsigned long smax;
Mel Gorman598f0ec2013-10-07 11:28:55 +01001134
1135 /* Watch for min being lower than max due to floor calculations */
1136 smax = sysctl_numa_balancing_scan_period_max / task_nr_scan_windows(p);
Rik van Rielb5dd77c2017-07-31 15:28:47 -04001137
1138 /* Scale the maximum scan period with the amount of shared memory. */
1139 if (p->numa_group) {
1140 struct numa_group *ng = p->numa_group;
1141 unsigned long shared = group_faults_shared(ng);
1142 unsigned long private = group_faults_priv(ng);
1143 unsigned long period = smax;
1144
1145 period *= atomic_read(&ng->refcount);
1146 period *= shared + 1;
1147 period /= private + shared + 1;
1148
1149 smax = max(smax, period);
1150 }
1151
Mel Gorman598f0ec2013-10-07 11:28:55 +01001152 return max(smin, smax);
1153}
1154
Peter Zijlstra0ec8aa02013-10-07 11:29:33 +01001155static void account_numa_enqueue(struct rq *rq, struct task_struct *p)
1156{
1157 rq->nr_numa_running += (p->numa_preferred_nid != -1);
1158 rq->nr_preferred_running += (p->numa_preferred_nid == task_node(p));
1159}
1160
1161static void account_numa_dequeue(struct rq *rq, struct task_struct *p)
1162{
1163 rq->nr_numa_running -= (p->numa_preferred_nid != -1);
1164 rq->nr_preferred_running -= (p->numa_preferred_nid == task_node(p));
1165}
1166
Rik van Rielbe1e4e72014-01-27 17:03:48 -05001167/* Shared or private faults. */
1168#define NR_NUMA_HINT_FAULT_TYPES 2
1169
1170/* Memory and CPU locality */
1171#define NR_NUMA_HINT_FAULT_STATS (NR_NUMA_HINT_FAULT_TYPES * 2)
1172
1173/* Averaged statistics, and temporary buffers. */
1174#define NR_NUMA_HINT_FAULT_BUCKETS (NR_NUMA_HINT_FAULT_STATS * 2)
1175
Mel Gormane29cf082013-10-07 11:29:22 +01001176pid_t task_numa_group_id(struct task_struct *p)
1177{
1178 return p->numa_group ? p->numa_group->gid : 0;
1179}
1180
Iulia Manda44dba3d2014-10-31 02:13:31 +02001181/*
1182 * The averaged statistics, shared & private, memory & cpu,
1183 * occupy the first half of the array. The second half of the
1184 * array is for current counters, which are averaged into the
1185 * first set by task_numa_placement.
1186 */
1187static inline int task_faults_idx(enum numa_faults_stats s, int nid, int priv)
Mel Gormanac8e8952013-10-07 11:29:03 +01001188{
Iulia Manda44dba3d2014-10-31 02:13:31 +02001189 return NR_NUMA_HINT_FAULT_TYPES * (s * nr_node_ids + nid) + priv;
Mel Gormanac8e8952013-10-07 11:29:03 +01001190}
1191
1192static inline unsigned long task_faults(struct task_struct *p, int nid)
1193{
Iulia Manda44dba3d2014-10-31 02:13:31 +02001194 if (!p->numa_faults)
Mel Gormanac8e8952013-10-07 11:29:03 +01001195 return 0;
1196
Iulia Manda44dba3d2014-10-31 02:13:31 +02001197 return p->numa_faults[task_faults_idx(NUMA_MEM, nid, 0)] +
1198 p->numa_faults[task_faults_idx(NUMA_MEM, nid, 1)];
Mel Gormanac8e8952013-10-07 11:29:03 +01001199}
1200
Mel Gorman83e1d2c2013-10-07 11:29:27 +01001201static inline unsigned long group_faults(struct task_struct *p, int nid)
1202{
1203 if (!p->numa_group)
1204 return 0;
1205
Iulia Manda44dba3d2014-10-31 02:13:31 +02001206 return p->numa_group->faults[task_faults_idx(NUMA_MEM, nid, 0)] +
1207 p->numa_group->faults[task_faults_idx(NUMA_MEM, nid, 1)];
Mel Gorman83e1d2c2013-10-07 11:29:27 +01001208}
1209
Rik van Riel20e07de2014-01-27 17:03:43 -05001210static inline unsigned long group_faults_cpu(struct numa_group *group, int nid)
1211{
Iulia Manda44dba3d2014-10-31 02:13:31 +02001212 return group->faults_cpu[task_faults_idx(NUMA_MEM, nid, 0)] +
1213 group->faults_cpu[task_faults_idx(NUMA_MEM, nid, 1)];
Rik van Riel20e07de2014-01-27 17:03:43 -05001214}
1215
Rik van Rielb5dd77c2017-07-31 15:28:47 -04001216static inline unsigned long group_faults_priv(struct numa_group *ng)
1217{
1218 unsigned long faults = 0;
1219 int node;
1220
1221 for_each_online_node(node) {
1222 faults += ng->faults[task_faults_idx(NUMA_MEM, node, 1)];
1223 }
1224
1225 return faults;
1226}
1227
1228static inline unsigned long group_faults_shared(struct numa_group *ng)
1229{
1230 unsigned long faults = 0;
1231 int node;
1232
1233 for_each_online_node(node) {
1234 faults += ng->faults[task_faults_idx(NUMA_MEM, node, 0)];
1235 }
1236
1237 return faults;
1238}
1239
Rik van Riel4142c3e2016-01-25 17:07:39 -05001240/*
1241 * A node triggering more than 1/3 as many NUMA faults as the maximum is
1242 * considered part of a numa group's pseudo-interleaving set. Migrations
1243 * between these nodes are slowed down, to allow things to settle down.
1244 */
1245#define ACTIVE_NODE_FRACTION 3
1246
1247static bool numa_is_active_node(int nid, struct numa_group *ng)
1248{
1249 return group_faults_cpu(ng, nid) * ACTIVE_NODE_FRACTION > ng->max_faults_cpu;
1250}
1251
Rik van Riel6c6b1192014-10-17 03:29:52 -04001252/* Handle placement on systems where not all nodes are directly connected. */
1253static unsigned long score_nearby_nodes(struct task_struct *p, int nid,
1254 int maxdist, bool task)
1255{
1256 unsigned long score = 0;
1257 int node;
1258
1259 /*
1260 * All nodes are directly connected, and the same distance
1261 * from each other. No need for fancy placement algorithms.
1262 */
1263 if (sched_numa_topology_type == NUMA_DIRECT)
1264 return 0;
1265
1266 /*
1267 * This code is called for each node, introducing N^2 complexity,
1268 * which should be ok given the number of nodes rarely exceeds 8.
1269 */
1270 for_each_online_node(node) {
1271 unsigned long faults;
1272 int dist = node_distance(nid, node);
1273
1274 /*
1275 * The furthest away nodes in the system are not interesting
1276 * for placement; nid was already counted.
1277 */
1278 if (dist == sched_max_numa_distance || node == nid)
1279 continue;
1280
1281 /*
1282 * On systems with a backplane NUMA topology, compare groups
1283 * of nodes, and move tasks towards the group with the most
1284 * memory accesses. When comparing two nodes at distance
1285 * "hoplimit", only nodes closer by than "hoplimit" are part
1286 * of each group. Skip other nodes.
1287 */
1288 if (sched_numa_topology_type == NUMA_BACKPLANE &&
1289 dist > maxdist)
1290 continue;
1291
1292 /* Add up the faults from nearby nodes. */
1293 if (task)
1294 faults = task_faults(p, node);
1295 else
1296 faults = group_faults(p, node);
1297
1298 /*
1299 * On systems with a glueless mesh NUMA topology, there are
1300 * no fixed "groups of nodes". Instead, nodes that are not
1301 * directly connected bounce traffic through intermediate
1302 * nodes; a numa_group can occupy any set of nodes.
1303 * The further away a node is, the less the faults count.
1304 * This seems to result in good task placement.
1305 */
1306 if (sched_numa_topology_type == NUMA_GLUELESS_MESH) {
1307 faults *= (sched_max_numa_distance - dist);
1308 faults /= (sched_max_numa_distance - LOCAL_DISTANCE);
1309 }
1310
1311 score += faults;
1312 }
1313
1314 return score;
1315}
1316
Mel Gorman83e1d2c2013-10-07 11:29:27 +01001317/*
1318 * These return the fraction of accesses done by a particular task, or
1319 * task group, on a particular numa node. The group weight is given a
1320 * larger multiplier, in order to group tasks together that are almost
1321 * evenly spread out between numa nodes.
1322 */
Rik van Riel7bd95322014-10-17 03:29:51 -04001323static inline unsigned long task_weight(struct task_struct *p, int nid,
1324 int dist)
Mel Gorman83e1d2c2013-10-07 11:29:27 +01001325{
Rik van Riel7bd95322014-10-17 03:29:51 -04001326 unsigned long faults, total_faults;
Mel Gorman83e1d2c2013-10-07 11:29:27 +01001327
Iulia Manda44dba3d2014-10-31 02:13:31 +02001328 if (!p->numa_faults)
Mel Gorman83e1d2c2013-10-07 11:29:27 +01001329 return 0;
1330
1331 total_faults = p->total_numa_faults;
1332
1333 if (!total_faults)
1334 return 0;
1335
Rik van Riel7bd95322014-10-17 03:29:51 -04001336 faults = task_faults(p, nid);
Rik van Riel6c6b1192014-10-17 03:29:52 -04001337 faults += score_nearby_nodes(p, nid, dist, true);
1338
Rik van Riel7bd95322014-10-17 03:29:51 -04001339 return 1000 * faults / total_faults;
Mel Gorman83e1d2c2013-10-07 11:29:27 +01001340}
1341
Rik van Riel7bd95322014-10-17 03:29:51 -04001342static inline unsigned long group_weight(struct task_struct *p, int nid,
1343 int dist)
Mel Gorman83e1d2c2013-10-07 11:29:27 +01001344{
Rik van Riel7bd95322014-10-17 03:29:51 -04001345 unsigned long faults, total_faults;
1346
1347 if (!p->numa_group)
Mel Gorman83e1d2c2013-10-07 11:29:27 +01001348 return 0;
1349
Rik van Riel7bd95322014-10-17 03:29:51 -04001350 total_faults = p->numa_group->total_faults;
1351
1352 if (!total_faults)
1353 return 0;
1354
1355 faults = group_faults(p, nid);
Rik van Riel6c6b1192014-10-17 03:29:52 -04001356 faults += score_nearby_nodes(p, nid, dist, false);
1357
Rik van Riel7bd95322014-10-17 03:29:51 -04001358 return 1000 * faults / total_faults;
Mel Gorman83e1d2c2013-10-07 11:29:27 +01001359}
1360
Rik van Riel10f39042014-01-27 17:03:44 -05001361bool should_numa_migrate_memory(struct task_struct *p, struct page * page,
1362 int src_nid, int dst_cpu)
1363{
1364 struct numa_group *ng = p->numa_group;
1365 int dst_nid = cpu_to_node(dst_cpu);
1366 int last_cpupid, this_cpupid;
1367
1368 this_cpupid = cpu_pid_to_cpupid(dst_cpu, current->pid);
1369
1370 /*
1371 * Multi-stage node selection is used in conjunction with a periodic
1372 * migration fault to build a temporal task<->page relation. By using
1373 * a two-stage filter we remove short/unlikely relations.
1374 *
1375 * Using P(p) ~ n_p / n_t as per frequentist probability, we can equate
1376 * a task's usage of a particular page (n_p) per total usage of this
1377 * page (n_t) (in a given time-span) to a probability.
1378 *
1379 * Our periodic faults will sample this probability and getting the
1380 * same result twice in a row, given these samples are fully
1381 * independent, is then given by P(n)^2, provided our sample period
1382 * is sufficiently short compared to the usage pattern.
1383 *
1384 * This quadric squishes small probabilities, making it less likely we
1385 * act on an unlikely task<->page relation.
1386 */
1387 last_cpupid = page_cpupid_xchg_last(page, this_cpupid);
1388 if (!cpupid_pid_unset(last_cpupid) &&
1389 cpupid_to_nid(last_cpupid) != dst_nid)
1390 return false;
1391
1392 /* Always allow migrate on private faults */
1393 if (cpupid_match_pid(p, last_cpupid))
1394 return true;
1395
1396 /* A shared fault, but p->numa_group has not been set up yet. */
1397 if (!ng)
1398 return true;
1399
1400 /*
Rik van Riel4142c3e2016-01-25 17:07:39 -05001401 * Destination node is much more heavily used than the source
1402 * node? Allow migration.
Rik van Riel10f39042014-01-27 17:03:44 -05001403 */
Rik van Riel4142c3e2016-01-25 17:07:39 -05001404 if (group_faults_cpu(ng, dst_nid) > group_faults_cpu(ng, src_nid) *
1405 ACTIVE_NODE_FRACTION)
Rik van Riel10f39042014-01-27 17:03:44 -05001406 return true;
1407
1408 /*
Rik van Riel4142c3e2016-01-25 17:07:39 -05001409 * Distribute memory according to CPU & memory use on each node,
1410 * with 3/4 hysteresis to avoid unnecessary memory migrations:
1411 *
1412 * faults_cpu(dst) 3 faults_cpu(src)
1413 * --------------- * - > ---------------
1414 * faults_mem(dst) 4 faults_mem(src)
Rik van Riel10f39042014-01-27 17:03:44 -05001415 */
Rik van Riel4142c3e2016-01-25 17:07:39 -05001416 return group_faults_cpu(ng, dst_nid) * group_faults(p, src_nid) * 3 >
1417 group_faults_cpu(ng, src_nid) * group_faults(p, dst_nid) * 4;
Rik van Riel10f39042014-01-27 17:03:44 -05001418}
1419
Viresh Kumarc7132dd2017-05-24 10:59:54 +05301420static unsigned long weighted_cpuload(struct rq *rq);
Mel Gorman58d081b2013-10-07 11:29:10 +01001421static unsigned long source_load(int cpu, int type);
1422static unsigned long target_load(int cpu, int type);
Nicolas Pitreced549f2014-05-26 18:19:38 -04001423static unsigned long capacity_of(int cpu);
Mel Gormane6628d52013-10-07 11:29:02 +01001424
Mel Gormanfb13c7e2013-10-07 11:29:17 +01001425/* Cached statistics for all CPUs within a node */
Mel Gorman58d081b2013-10-07 11:29:10 +01001426struct numa_stats {
Mel Gormanfb13c7e2013-10-07 11:29:17 +01001427 unsigned long nr_running;
Mel Gorman58d081b2013-10-07 11:29:10 +01001428 unsigned long load;
Mel Gormanfb13c7e2013-10-07 11:29:17 +01001429
1430 /* Total compute capacity of CPUs on a node */
Nicolas Pitre5ef20ca2014-05-26 18:19:34 -04001431 unsigned long compute_capacity;
Mel Gormanfb13c7e2013-10-07 11:29:17 +01001432
1433 /* Approximate capacity in terms of runnable tasks on a node */
Nicolas Pitre5ef20ca2014-05-26 18:19:34 -04001434 unsigned long task_capacity;
Nicolas Pitre1b6a7492014-05-26 18:19:35 -04001435 int has_free_capacity;
Mel Gorman58d081b2013-10-07 11:29:10 +01001436};
Mel Gormane6628d52013-10-07 11:29:02 +01001437
Mel Gormanfb13c7e2013-10-07 11:29:17 +01001438/*
1439 * XXX borrowed from update_sg_lb_stats
1440 */
1441static void update_numa_stats(struct numa_stats *ns, int nid)
1442{
Rik van Riel83d7f242014-08-04 13:23:28 -04001443 int smt, cpu, cpus = 0;
1444 unsigned long capacity;
Mel Gormanfb13c7e2013-10-07 11:29:17 +01001445
1446 memset(ns, 0, sizeof(*ns));
1447 for_each_cpu(cpu, cpumask_of_node(nid)) {
1448 struct rq *rq = cpu_rq(cpu);
1449
1450 ns->nr_running += rq->nr_running;
Viresh Kumarc7132dd2017-05-24 10:59:54 +05301451 ns->load += weighted_cpuload(rq);
Nicolas Pitreced549f2014-05-26 18:19:38 -04001452 ns->compute_capacity += capacity_of(cpu);
Peter Zijlstra5eca82a2013-11-06 18:47:57 +01001453
1454 cpus++;
Mel Gormanfb13c7e2013-10-07 11:29:17 +01001455 }
1456
Peter Zijlstra5eca82a2013-11-06 18:47:57 +01001457 /*
1458 * If we raced with hotplug and there are no CPUs left in our mask
1459 * the @ns structure is NULL'ed and task_numa_compare() will
1460 * not find this node attractive.
1461 *
Nicolas Pitre1b6a7492014-05-26 18:19:35 -04001462 * We'll either bail at !has_free_capacity, or we'll detect a huge
1463 * imbalance and bail there.
Peter Zijlstra5eca82a2013-11-06 18:47:57 +01001464 */
1465 if (!cpus)
1466 return;
1467
Rik van Riel83d7f242014-08-04 13:23:28 -04001468 /* smt := ceil(cpus / capacity), assumes: 1 < smt_power < 2 */
1469 smt = DIV_ROUND_UP(SCHED_CAPACITY_SCALE * cpus, ns->compute_capacity);
1470 capacity = cpus / smt; /* cores */
1471
1472 ns->task_capacity = min_t(unsigned, capacity,
1473 DIV_ROUND_CLOSEST(ns->compute_capacity, SCHED_CAPACITY_SCALE));
Nicolas Pitre1b6a7492014-05-26 18:19:35 -04001474 ns->has_free_capacity = (ns->nr_running < ns->task_capacity);
Mel Gormanfb13c7e2013-10-07 11:29:17 +01001475}
1476
Mel Gorman58d081b2013-10-07 11:29:10 +01001477struct task_numa_env {
1478 struct task_struct *p;
1479
1480 int src_cpu, src_nid;
1481 int dst_cpu, dst_nid;
1482
1483 struct numa_stats src_stats, dst_stats;
1484
Wanpeng Li40ea2b42013-12-05 19:10:17 +08001485 int imbalance_pct;
Rik van Riel7bd95322014-10-17 03:29:51 -04001486 int dist;
Mel Gormanfb13c7e2013-10-07 11:29:17 +01001487
1488 struct task_struct *best_task;
1489 long best_imp;
Mel Gorman58d081b2013-10-07 11:29:10 +01001490 int best_cpu;
1491};
1492
Mel Gormanfb13c7e2013-10-07 11:29:17 +01001493static void task_numa_assign(struct task_numa_env *env,
1494 struct task_struct *p, long imp)
1495{
1496 if (env->best_task)
1497 put_task_struct(env->best_task);
Oleg Nesterovbac78572016-05-18 21:57:33 +02001498 if (p)
1499 get_task_struct(p);
Mel Gormanfb13c7e2013-10-07 11:29:17 +01001500
1501 env->best_task = p;
1502 env->best_imp = imp;
1503 env->best_cpu = env->dst_cpu;
1504}
1505
Rik van Riel28a21742014-06-23 11:46:13 -04001506static bool load_too_imbalanced(long src_load, long dst_load,
Rik van Riele63da032014-05-14 13:22:21 -04001507 struct task_numa_env *env)
1508{
Rik van Riele4991b22015-05-27 15:04:27 -04001509 long imb, old_imb;
1510 long orig_src_load, orig_dst_load;
Rik van Riel28a21742014-06-23 11:46:13 -04001511 long src_capacity, dst_capacity;
1512
1513 /*
1514 * The load is corrected for the CPU capacity available on each node.
1515 *
1516 * src_load dst_load
1517 * ------------ vs ---------
1518 * src_capacity dst_capacity
1519 */
1520 src_capacity = env->src_stats.compute_capacity;
1521 dst_capacity = env->dst_stats.compute_capacity;
Rik van Riele63da032014-05-14 13:22:21 -04001522
1523 /* We care about the slope of the imbalance, not the direction. */
Rik van Riele4991b22015-05-27 15:04:27 -04001524 if (dst_load < src_load)
1525 swap(dst_load, src_load);
Rik van Riele63da032014-05-14 13:22:21 -04001526
1527 /* Is the difference below the threshold? */
Rik van Riele4991b22015-05-27 15:04:27 -04001528 imb = dst_load * src_capacity * 100 -
1529 src_load * dst_capacity * env->imbalance_pct;
Rik van Riele63da032014-05-14 13:22:21 -04001530 if (imb <= 0)
1531 return false;
1532
1533 /*
1534 * The imbalance is above the allowed threshold.
Rik van Riele4991b22015-05-27 15:04:27 -04001535 * Compare it with the old imbalance.
Rik van Riele63da032014-05-14 13:22:21 -04001536 */
Rik van Riel28a21742014-06-23 11:46:13 -04001537 orig_src_load = env->src_stats.load;
Rik van Riele4991b22015-05-27 15:04:27 -04001538 orig_dst_load = env->dst_stats.load;
Rik van Riel28a21742014-06-23 11:46:13 -04001539
Rik van Riele4991b22015-05-27 15:04:27 -04001540 if (orig_dst_load < orig_src_load)
1541 swap(orig_dst_load, orig_src_load);
Rik van Riele63da032014-05-14 13:22:21 -04001542
Rik van Riele4991b22015-05-27 15:04:27 -04001543 old_imb = orig_dst_load * src_capacity * 100 -
1544 orig_src_load * dst_capacity * env->imbalance_pct;
1545
1546 /* Would this change make things worse? */
1547 return (imb > old_imb);
Rik van Riele63da032014-05-14 13:22:21 -04001548}
1549
Mel Gormanfb13c7e2013-10-07 11:29:17 +01001550/*
1551 * This checks if the overall compute and NUMA accesses of the system would
1552 * be improved if the source tasks was migrated to the target dst_cpu taking
1553 * into account that it might be best if task running on the dst_cpu should
1554 * be exchanged with the source task
1555 */
Rik van Riel887c2902013-10-07 11:29:31 +01001556static void task_numa_compare(struct task_numa_env *env,
1557 long taskimp, long groupimp)
Mel Gormanfb13c7e2013-10-07 11:29:17 +01001558{
1559 struct rq *src_rq = cpu_rq(env->src_cpu);
1560 struct rq *dst_rq = cpu_rq(env->dst_cpu);
1561 struct task_struct *cur;
Rik van Riel28a21742014-06-23 11:46:13 -04001562 long src_load, dst_load;
Mel Gormanfb13c7e2013-10-07 11:29:17 +01001563 long load;
Rik van Riel1c5d3eb2014-06-23 11:46:15 -04001564 long imp = env->p->numa_group ? groupimp : taskimp;
Rik van Riel0132c3e2014-06-23 11:46:16 -04001565 long moveimp = imp;
Rik van Riel7bd95322014-10-17 03:29:51 -04001566 int dist = env->dist;
Mel Gormanfb13c7e2013-10-07 11:29:17 +01001567
1568 rcu_read_lock();
Oleg Nesterovbac78572016-05-18 21:57:33 +02001569 cur = task_rcu_dereference(&dst_rq->curr);
1570 if (cur && ((cur->flags & PF_EXITING) || is_idle_task(cur)))
Mel Gormanfb13c7e2013-10-07 11:29:17 +01001571 cur = NULL;
1572
1573 /*
Peter Zijlstra7af68332014-11-10 10:54:35 +01001574 * Because we have preemption enabled we can get migrated around and
1575 * end try selecting ourselves (current == env->p) as a swap candidate.
1576 */
1577 if (cur == env->p)
1578 goto unlock;
1579
1580 /*
Mel Gormanfb13c7e2013-10-07 11:29:17 +01001581 * "imp" is the fault differential for the source task between the
1582 * source and destination node. Calculate the total differential for
1583 * the source task and potential destination task. The more negative
1584 * the value is, the more rmeote accesses that would be expected to
1585 * be incurred if the tasks were swapped.
1586 */
1587 if (cur) {
1588 /* Skip this swap candidate if cannot move to the source cpu */
Ingo Molnar0c98d342017-02-05 15:38:10 +01001589 if (!cpumask_test_cpu(env->src_cpu, &cur->cpus_allowed))
Mel Gormanfb13c7e2013-10-07 11:29:17 +01001590 goto unlock;
1591
Rik van Riel887c2902013-10-07 11:29:31 +01001592 /*
1593 * If dst and source tasks are in the same NUMA group, or not
Rik van Rielca28aa532013-10-07 11:29:32 +01001594 * in any group then look only at task weights.
Rik van Riel887c2902013-10-07 11:29:31 +01001595 */
Rik van Rielca28aa532013-10-07 11:29:32 +01001596 if (cur->numa_group == env->p->numa_group) {
Rik van Riel7bd95322014-10-17 03:29:51 -04001597 imp = taskimp + task_weight(cur, env->src_nid, dist) -
1598 task_weight(cur, env->dst_nid, dist);
Rik van Rielca28aa532013-10-07 11:29:32 +01001599 /*
1600 * Add some hysteresis to prevent swapping the
1601 * tasks within a group over tiny differences.
1602 */
1603 if (cur->numa_group)
1604 imp -= imp/16;
Rik van Riel887c2902013-10-07 11:29:31 +01001605 } else {
Rik van Rielca28aa532013-10-07 11:29:32 +01001606 /*
1607 * Compare the group weights. If a task is all by
1608 * itself (not part of a group), use the task weight
1609 * instead.
1610 */
Rik van Rielca28aa532013-10-07 11:29:32 +01001611 if (cur->numa_group)
Rik van Riel7bd95322014-10-17 03:29:51 -04001612 imp += group_weight(cur, env->src_nid, dist) -
1613 group_weight(cur, env->dst_nid, dist);
Rik van Rielca28aa532013-10-07 11:29:32 +01001614 else
Rik van Riel7bd95322014-10-17 03:29:51 -04001615 imp += task_weight(cur, env->src_nid, dist) -
1616 task_weight(cur, env->dst_nid, dist);
Rik van Riel887c2902013-10-07 11:29:31 +01001617 }
Mel Gormanfb13c7e2013-10-07 11:29:17 +01001618 }
1619
Rik van Riel0132c3e2014-06-23 11:46:16 -04001620 if (imp <= env->best_imp && moveimp <= env->best_imp)
Mel Gormanfb13c7e2013-10-07 11:29:17 +01001621 goto unlock;
1622
1623 if (!cur) {
1624 /* Is there capacity at our destination? */
Rik van Rielb932c032014-08-04 13:23:27 -04001625 if (env->src_stats.nr_running <= env->src_stats.task_capacity &&
Nicolas Pitre1b6a7492014-05-26 18:19:35 -04001626 !env->dst_stats.has_free_capacity)
Mel Gormanfb13c7e2013-10-07 11:29:17 +01001627 goto unlock;
1628
1629 goto balance;
1630 }
1631
1632 /* Balance doesn't matter much if we're running a task per cpu */
Rik van Riel0132c3e2014-06-23 11:46:16 -04001633 if (imp > env->best_imp && src_rq->nr_running == 1 &&
1634 dst_rq->nr_running == 1)
Mel Gormanfb13c7e2013-10-07 11:29:17 +01001635 goto assign;
1636
1637 /*
1638 * In the overloaded case, try and keep the load balanced.
1639 */
1640balance:
Peter Zijlstrae720fff2014-07-11 16:01:53 +02001641 load = task_h_load(env->p);
1642 dst_load = env->dst_stats.load + load;
1643 src_load = env->src_stats.load - load;
Mel Gormanfb13c7e2013-10-07 11:29:17 +01001644
Rik van Riel0132c3e2014-06-23 11:46:16 -04001645 if (moveimp > imp && moveimp > env->best_imp) {
1646 /*
1647 * If the improvement from just moving env->p direction is
1648 * better than swapping tasks around, check if a move is
1649 * possible. Store a slightly smaller score than moveimp,
1650 * so an actually idle CPU will win.
1651 */
1652 if (!load_too_imbalanced(src_load, dst_load, env)) {
1653 imp = moveimp - 1;
1654 cur = NULL;
1655 goto assign;
1656 }
1657 }
1658
1659 if (imp <= env->best_imp)
1660 goto unlock;
1661
Mel Gormanfb13c7e2013-10-07 11:29:17 +01001662 if (cur) {
Peter Zijlstrae720fff2014-07-11 16:01:53 +02001663 load = task_h_load(cur);
1664 dst_load -= load;
1665 src_load += load;
Mel Gormanfb13c7e2013-10-07 11:29:17 +01001666 }
1667
Rik van Riel28a21742014-06-23 11:46:13 -04001668 if (load_too_imbalanced(src_load, dst_load, env))
Mel Gormanfb13c7e2013-10-07 11:29:17 +01001669 goto unlock;
1670
Rik van Rielba7e5a22014-09-04 16:35:30 -04001671 /*
1672 * One idle CPU per node is evaluated for a task numa move.
1673 * Call select_idle_sibling to maybe find a better one.
1674 */
Peter Zijlstra10e2f1a2016-05-09 10:38:05 +02001675 if (!cur) {
1676 /*
1677 * select_idle_siblings() uses an per-cpu cpumask that
1678 * can be used from IRQ context.
1679 */
1680 local_irq_disable();
Morten Rasmussen772bd008c2016-06-22 18:03:13 +01001681 env->dst_cpu = select_idle_sibling(env->p, env->src_cpu,
1682 env->dst_cpu);
Peter Zijlstra10e2f1a2016-05-09 10:38:05 +02001683 local_irq_enable();
1684 }
Rik van Rielba7e5a22014-09-04 16:35:30 -04001685
Mel Gormanfb13c7e2013-10-07 11:29:17 +01001686assign:
1687 task_numa_assign(env, cur, imp);
1688unlock:
1689 rcu_read_unlock();
1690}
1691
Rik van Riel887c2902013-10-07 11:29:31 +01001692static void task_numa_find_cpu(struct task_numa_env *env,
1693 long taskimp, long groupimp)
Mel Gorman2c8a50a2013-10-07 11:29:18 +01001694{
1695 int cpu;
1696
1697 for_each_cpu(cpu, cpumask_of_node(env->dst_nid)) {
1698 /* Skip this CPU if the source task cannot migrate */
Ingo Molnar0c98d342017-02-05 15:38:10 +01001699 if (!cpumask_test_cpu(cpu, &env->p->cpus_allowed))
Mel Gorman2c8a50a2013-10-07 11:29:18 +01001700 continue;
1701
1702 env->dst_cpu = cpu;
Rik van Riel887c2902013-10-07 11:29:31 +01001703 task_numa_compare(env, taskimp, groupimp);
Mel Gorman2c8a50a2013-10-07 11:29:18 +01001704 }
1705}
1706
Rik van Riel6f9aad02015-05-28 09:52:49 -04001707/* Only move tasks to a NUMA node less busy than the current node. */
1708static bool numa_has_capacity(struct task_numa_env *env)
1709{
1710 struct numa_stats *src = &env->src_stats;
1711 struct numa_stats *dst = &env->dst_stats;
1712
1713 if (src->has_free_capacity && !dst->has_free_capacity)
1714 return false;
1715
1716 /*
1717 * Only consider a task move if the source has a higher load
1718 * than the destination, corrected for CPU capacity on each node.
1719 *
1720 * src->load dst->load
1721 * --------------------- vs ---------------------
1722 * src->compute_capacity dst->compute_capacity
1723 */
Srikar Dronamraju44dcb042015-06-16 17:26:00 +05301724 if (src->load * dst->compute_capacity * env->imbalance_pct >
1725
1726 dst->load * src->compute_capacity * 100)
Rik van Riel6f9aad02015-05-28 09:52:49 -04001727 return true;
1728
1729 return false;
1730}
1731
Mel Gorman58d081b2013-10-07 11:29:10 +01001732static int task_numa_migrate(struct task_struct *p)
Mel Gormane6628d52013-10-07 11:29:02 +01001733{
Mel Gorman58d081b2013-10-07 11:29:10 +01001734 struct task_numa_env env = {
1735 .p = p,
Mel Gormanfb13c7e2013-10-07 11:29:17 +01001736
Mel Gorman58d081b2013-10-07 11:29:10 +01001737 .src_cpu = task_cpu(p),
Ingo Molnarb32e86b2013-10-07 11:29:30 +01001738 .src_nid = task_node(p),
Mel Gormanfb13c7e2013-10-07 11:29:17 +01001739
1740 .imbalance_pct = 112,
1741
1742 .best_task = NULL,
1743 .best_imp = 0,
Rik van Riel4142c3e2016-01-25 17:07:39 -05001744 .best_cpu = -1,
Mel Gorman58d081b2013-10-07 11:29:10 +01001745 };
1746 struct sched_domain *sd;
Rik van Riel887c2902013-10-07 11:29:31 +01001747 unsigned long taskweight, groupweight;
Rik van Riel7bd95322014-10-17 03:29:51 -04001748 int nid, ret, dist;
Rik van Riel887c2902013-10-07 11:29:31 +01001749 long taskimp, groupimp;
Mel Gormane6628d52013-10-07 11:29:02 +01001750
Mel Gorman58d081b2013-10-07 11:29:10 +01001751 /*
Mel Gormanfb13c7e2013-10-07 11:29:17 +01001752 * Pick the lowest SD_NUMA domain, as that would have the smallest
1753 * imbalance and would be the first to start moving tasks about.
1754 *
1755 * And we want to avoid any moving of tasks about, as that would create
1756 * random movement of tasks -- counter the numa conditions we're trying
1757 * to satisfy here.
Mel Gorman58d081b2013-10-07 11:29:10 +01001758 */
Mel Gormane6628d52013-10-07 11:29:02 +01001759 rcu_read_lock();
Mel Gormanfb13c7e2013-10-07 11:29:17 +01001760 sd = rcu_dereference(per_cpu(sd_numa, env.src_cpu));
Rik van Riel46a73e82013-11-11 19:29:25 -05001761 if (sd)
1762 env.imbalance_pct = 100 + (sd->imbalance_pct - 100) / 2;
Mel Gormane6628d52013-10-07 11:29:02 +01001763 rcu_read_unlock();
1764
Rik van Riel46a73e82013-11-11 19:29:25 -05001765 /*
1766 * Cpusets can break the scheduler domain tree into smaller
1767 * balance domains, some of which do not cross NUMA boundaries.
1768 * Tasks that are "trapped" in such domains cannot be migrated
1769 * elsewhere, so there is no point in (re)trying.
1770 */
1771 if (unlikely(!sd)) {
Wanpeng Lide1b3012013-12-12 15:23:24 +08001772 p->numa_preferred_nid = task_node(p);
Rik van Riel46a73e82013-11-11 19:29:25 -05001773 return -EINVAL;
1774 }
1775
Mel Gorman2c8a50a2013-10-07 11:29:18 +01001776 env.dst_nid = p->numa_preferred_nid;
Rik van Riel7bd95322014-10-17 03:29:51 -04001777 dist = env.dist = node_distance(env.src_nid, env.dst_nid);
1778 taskweight = task_weight(p, env.src_nid, dist);
1779 groupweight = group_weight(p, env.src_nid, dist);
1780 update_numa_stats(&env.src_stats, env.src_nid);
1781 taskimp = task_weight(p, env.dst_nid, dist) - taskweight;
1782 groupimp = group_weight(p, env.dst_nid, dist) - groupweight;
Mel Gorman2c8a50a2013-10-07 11:29:18 +01001783 update_numa_stats(&env.dst_stats, env.dst_nid);
Mel Gorman58d081b2013-10-07 11:29:10 +01001784
Rik van Riela43455a2014-06-04 16:09:42 -04001785 /* Try to find a spot on the preferred nid. */
Rik van Riel6f9aad02015-05-28 09:52:49 -04001786 if (numa_has_capacity(&env))
1787 task_numa_find_cpu(&env, taskimp, groupimp);
Rik van Riele1dda8a2013-10-07 11:29:19 +01001788
Rik van Riel9de05d42014-10-09 17:27:47 -04001789 /*
1790 * Look at other nodes in these cases:
1791 * - there is no space available on the preferred_nid
1792 * - the task is part of a numa_group that is interleaved across
1793 * multiple NUMA nodes; in order to better consolidate the group,
1794 * we need to check other locations.
1795 */
Rik van Riel4142c3e2016-01-25 17:07:39 -05001796 if (env.best_cpu == -1 || (p->numa_group && p->numa_group->active_nodes > 1)) {
Mel Gorman2c8a50a2013-10-07 11:29:18 +01001797 for_each_online_node(nid) {
1798 if (nid == env.src_nid || nid == p->numa_preferred_nid)
Mel Gormanfb13c7e2013-10-07 11:29:17 +01001799 continue;
1800
Rik van Riel7bd95322014-10-17 03:29:51 -04001801 dist = node_distance(env.src_nid, env.dst_nid);
Rik van Riel6c6b1192014-10-17 03:29:52 -04001802 if (sched_numa_topology_type == NUMA_BACKPLANE &&
1803 dist != env.dist) {
1804 taskweight = task_weight(p, env.src_nid, dist);
1805 groupweight = group_weight(p, env.src_nid, dist);
1806 }
Rik van Riel7bd95322014-10-17 03:29:51 -04001807
Mel Gorman83e1d2c2013-10-07 11:29:27 +01001808 /* Only consider nodes where both task and groups benefit */
Rik van Riel7bd95322014-10-17 03:29:51 -04001809 taskimp = task_weight(p, nid, dist) - taskweight;
1810 groupimp = group_weight(p, nid, dist) - groupweight;
Rik van Riel887c2902013-10-07 11:29:31 +01001811 if (taskimp < 0 && groupimp < 0)
Mel Gorman2c8a50a2013-10-07 11:29:18 +01001812 continue;
1813
Rik van Riel7bd95322014-10-17 03:29:51 -04001814 env.dist = dist;
Mel Gorman2c8a50a2013-10-07 11:29:18 +01001815 env.dst_nid = nid;
1816 update_numa_stats(&env.dst_stats, env.dst_nid);
Rik van Riel6f9aad02015-05-28 09:52:49 -04001817 if (numa_has_capacity(&env))
1818 task_numa_find_cpu(&env, taskimp, groupimp);
Mel Gorman58d081b2013-10-07 11:29:10 +01001819 }
1820 }
1821
Rik van Riel68d1b022014-04-11 13:00:29 -04001822 /*
1823 * If the task is part of a workload that spans multiple NUMA nodes,
1824 * and is migrating into one of the workload's active nodes, remember
1825 * this node as the task's preferred numa node, so the workload can
1826 * settle down.
1827 * A task that migrated to a second choice node will be better off
1828 * trying for a better one later. Do not set the preferred node here.
1829 */
Rik van Rieldb015da2014-06-23 11:41:34 -04001830 if (p->numa_group) {
Rik van Riel4142c3e2016-01-25 17:07:39 -05001831 struct numa_group *ng = p->numa_group;
1832
Rik van Rieldb015da2014-06-23 11:41:34 -04001833 if (env.best_cpu == -1)
1834 nid = env.src_nid;
1835 else
1836 nid = env.dst_nid;
1837
Rik van Riel4142c3e2016-01-25 17:07:39 -05001838 if (ng->active_nodes > 1 && numa_is_active_node(env.dst_nid, ng))
Rik van Rieldb015da2014-06-23 11:41:34 -04001839 sched_setnuma(p, env.dst_nid);
1840 }
1841
1842 /* No better CPU than the current one was found. */
1843 if (env.best_cpu == -1)
1844 return -EAGAIN;
Peter Zijlstra0ec8aa02013-10-07 11:29:33 +01001845
Rik van Riel04bb2f92013-10-07 11:29:36 +01001846 /*
1847 * Reset the scan period if the task is being rescheduled on an
1848 * alternative node to recheck if the tasks is now properly placed.
1849 */
Rik van Rielb5dd77c2017-07-31 15:28:47 -04001850 p->numa_scan_period = task_scan_start(p);
Rik van Riel04bb2f92013-10-07 11:29:36 +01001851
Mel Gormanfb13c7e2013-10-07 11:29:17 +01001852 if (env.best_task == NULL) {
Mel Gorman286549d2014-01-21 15:51:03 -08001853 ret = migrate_task_to(p, env.best_cpu);
1854 if (ret != 0)
1855 trace_sched_stick_numa(p, env.src_cpu, env.best_cpu);
Mel Gormanfb13c7e2013-10-07 11:29:17 +01001856 return ret;
1857 }
1858
1859 ret = migrate_swap(p, env.best_task);
Mel Gorman286549d2014-01-21 15:51:03 -08001860 if (ret != 0)
1861 trace_sched_stick_numa(p, env.src_cpu, task_cpu(env.best_task));
Mel Gormanfb13c7e2013-10-07 11:29:17 +01001862 put_task_struct(env.best_task);
1863 return ret;
Mel Gormane6628d52013-10-07 11:29:02 +01001864}
1865
Mel Gorman6b9a7462013-10-07 11:29:11 +01001866/* Attempt to migrate a task to a CPU on the preferred node. */
1867static void numa_migrate_preferred(struct task_struct *p)
1868{
Rik van Riel5085e2a2014-04-11 13:00:28 -04001869 unsigned long interval = HZ;
1870
Rik van Riel2739d3e2013-10-07 11:29:41 +01001871 /* This task has no NUMA fault statistics yet */
Iulia Manda44dba3d2014-10-31 02:13:31 +02001872 if (unlikely(p->numa_preferred_nid == -1 || !p->numa_faults))
Rik van Riel2739d3e2013-10-07 11:29:41 +01001873 return;
1874
1875 /* Periodically retry migrating the task to the preferred node */
Rik van Riel5085e2a2014-04-11 13:00:28 -04001876 interval = min(interval, msecs_to_jiffies(p->numa_scan_period) / 16);
1877 p->numa_migrate_retry = jiffies + interval;
Rik van Riel2739d3e2013-10-07 11:29:41 +01001878
Mel Gorman6b9a7462013-10-07 11:29:11 +01001879 /* Success if task is already running on preferred CPU */
Wanpeng Lide1b3012013-12-12 15:23:24 +08001880 if (task_node(p) == p->numa_preferred_nid)
Mel Gorman6b9a7462013-10-07 11:29:11 +01001881 return;
1882
Mel Gorman6b9a7462013-10-07 11:29:11 +01001883 /* Otherwise, try migrate to a CPU on the preferred node */
Rik van Riel2739d3e2013-10-07 11:29:41 +01001884 task_numa_migrate(p);
Mel Gorman6b9a7462013-10-07 11:29:11 +01001885}
1886
Rik van Riel04bb2f92013-10-07 11:29:36 +01001887/*
Rik van Riel4142c3e2016-01-25 17:07:39 -05001888 * Find out how many nodes on the workload is actively running on. Do this by
Rik van Riel20e07de2014-01-27 17:03:43 -05001889 * tracking the nodes from which NUMA hinting faults are triggered. This can
1890 * be different from the set of nodes where the workload's memory is currently
1891 * located.
Rik van Riel20e07de2014-01-27 17:03:43 -05001892 */
Rik van Riel4142c3e2016-01-25 17:07:39 -05001893static void numa_group_count_active_nodes(struct numa_group *numa_group)
Rik van Riel20e07de2014-01-27 17:03:43 -05001894{
1895 unsigned long faults, max_faults = 0;
Rik van Riel4142c3e2016-01-25 17:07:39 -05001896 int nid, active_nodes = 0;
Rik van Riel20e07de2014-01-27 17:03:43 -05001897
1898 for_each_online_node(nid) {
1899 faults = group_faults_cpu(numa_group, nid);
1900 if (faults > max_faults)
1901 max_faults = faults;
1902 }
1903
1904 for_each_online_node(nid) {
1905 faults = group_faults_cpu(numa_group, nid);
Rik van Riel4142c3e2016-01-25 17:07:39 -05001906 if (faults * ACTIVE_NODE_FRACTION > max_faults)
1907 active_nodes++;
Rik van Riel20e07de2014-01-27 17:03:43 -05001908 }
Rik van Riel4142c3e2016-01-25 17:07:39 -05001909
1910 numa_group->max_faults_cpu = max_faults;
1911 numa_group->active_nodes = active_nodes;
Rik van Riel20e07de2014-01-27 17:03:43 -05001912}
1913
1914/*
Rik van Riel04bb2f92013-10-07 11:29:36 +01001915 * When adapting the scan rate, the period is divided into NUMA_PERIOD_SLOTS
1916 * increments. The more local the fault statistics are, the higher the scan
Rik van Riela22b4b02014-06-23 11:41:35 -04001917 * period will be for the next scan window. If local/(local+remote) ratio is
1918 * below NUMA_PERIOD_THRESHOLD (where range of ratio is 1..NUMA_PERIOD_SLOTS)
1919 * the scan period will decrease. Aim for 70% local accesses.
Rik van Riel04bb2f92013-10-07 11:29:36 +01001920 */
1921#define NUMA_PERIOD_SLOTS 10
Rik van Riela22b4b02014-06-23 11:41:35 -04001922#define NUMA_PERIOD_THRESHOLD 7
Rik van Riel04bb2f92013-10-07 11:29:36 +01001923
1924/*
1925 * Increase the scan period (slow down scanning) if the majority of
1926 * our memory is already on our local node, or if the majority of
1927 * the page accesses are shared with other processes.
1928 * Otherwise, decrease the scan period.
1929 */
1930static void update_task_scan_period(struct task_struct *p,
1931 unsigned long shared, unsigned long private)
1932{
1933 unsigned int period_slot;
Rik van Riel37ec97de2017-07-31 15:28:46 -04001934 int lr_ratio, ps_ratio;
Rik van Riel04bb2f92013-10-07 11:29:36 +01001935 int diff;
1936
1937 unsigned long remote = p->numa_faults_locality[0];
1938 unsigned long local = p->numa_faults_locality[1];
1939
1940 /*
1941 * If there were no record hinting faults then either the task is
1942 * completely idle or all activity is areas that are not of interest
Mel Gorman074c2382015-03-25 15:55:42 -07001943 * to automatic numa balancing. Related to that, if there were failed
1944 * migration then it implies we are migrating too quickly or the local
1945 * node is overloaded. In either case, scan slower
Rik van Riel04bb2f92013-10-07 11:29:36 +01001946 */
Mel Gorman074c2382015-03-25 15:55:42 -07001947 if (local + shared == 0 || p->numa_faults_locality[2]) {
Rik van Riel04bb2f92013-10-07 11:29:36 +01001948 p->numa_scan_period = min(p->numa_scan_period_max,
1949 p->numa_scan_period << 1);
1950
1951 p->mm->numa_next_scan = jiffies +
1952 msecs_to_jiffies(p->numa_scan_period);
1953
1954 return;
1955 }
1956
1957 /*
1958 * Prepare to scale scan period relative to the current period.
1959 * == NUMA_PERIOD_THRESHOLD scan period stays the same
1960 * < NUMA_PERIOD_THRESHOLD scan period decreases (scan faster)
1961 * >= NUMA_PERIOD_THRESHOLD scan period increases (scan slower)
1962 */
1963 period_slot = DIV_ROUND_UP(p->numa_scan_period, NUMA_PERIOD_SLOTS);
Rik van Riel37ec97de2017-07-31 15:28:46 -04001964 lr_ratio = (local * NUMA_PERIOD_SLOTS) / (local + remote);
1965 ps_ratio = (private * NUMA_PERIOD_SLOTS) / (private + shared);
1966
1967 if (ps_ratio >= NUMA_PERIOD_THRESHOLD) {
1968 /*
1969 * Most memory accesses are local. There is no need to
1970 * do fast NUMA scanning, since memory is already local.
1971 */
1972 int slot = ps_ratio - NUMA_PERIOD_THRESHOLD;
1973 if (!slot)
1974 slot = 1;
1975 diff = slot * period_slot;
1976 } else if (lr_ratio >= NUMA_PERIOD_THRESHOLD) {
1977 /*
1978 * Most memory accesses are shared with other tasks.
1979 * There is no point in continuing fast NUMA scanning,
1980 * since other tasks may just move the memory elsewhere.
1981 */
1982 int slot = lr_ratio - NUMA_PERIOD_THRESHOLD;
Rik van Riel04bb2f92013-10-07 11:29:36 +01001983 if (!slot)
1984 slot = 1;
1985 diff = slot * period_slot;
1986 } else {
Rik van Riel04bb2f92013-10-07 11:29:36 +01001987 /*
Rik van Riel37ec97de2017-07-31 15:28:46 -04001988 * Private memory faults exceed (SLOTS-THRESHOLD)/SLOTS,
1989 * yet they are not on the local NUMA node. Speed up
1990 * NUMA scanning to get the memory moved over.
Rik van Riel04bb2f92013-10-07 11:29:36 +01001991 */
Rik van Riel37ec97de2017-07-31 15:28:46 -04001992 int ratio = max(lr_ratio, ps_ratio);
1993 diff = -(NUMA_PERIOD_THRESHOLD - ratio) * period_slot;
Rik van Riel04bb2f92013-10-07 11:29:36 +01001994 }
1995
1996 p->numa_scan_period = clamp(p->numa_scan_period + diff,
1997 task_scan_min(p), task_scan_max(p));
1998 memset(p->numa_faults_locality, 0, sizeof(p->numa_faults_locality));
1999}
2000
Rik van Riel7e2703e2014-01-27 17:03:45 -05002001/*
2002 * Get the fraction of time the task has been running since the last
2003 * NUMA placement cycle. The scheduler keeps similar statistics, but
2004 * decays those on a 32ms period, which is orders of magnitude off
2005 * from the dozens-of-seconds NUMA balancing period. Use the scheduler
2006 * stats only if the task is so new there are no NUMA statistics yet.
2007 */
2008static u64 numa_get_avg_runtime(struct task_struct *p, u64 *period)
2009{
2010 u64 runtime, delta, now;
2011 /* Use the start of this time slice to avoid calculations. */
2012 now = p->se.exec_start;
2013 runtime = p->se.sum_exec_runtime;
2014
2015 if (p->last_task_numa_placement) {
2016 delta = runtime - p->last_sum_exec_runtime;
2017 *period = now - p->last_task_numa_placement;
2018 } else {
Peter Zijlstrac7b50212017-05-06 16:42:08 +02002019 delta = p->se.avg.load_sum;
Yuyang Du9d89c252015-07-15 08:04:37 +08002020 *period = LOAD_AVG_MAX;
Rik van Riel7e2703e2014-01-27 17:03:45 -05002021 }
2022
2023 p->last_sum_exec_runtime = runtime;
2024 p->last_task_numa_placement = now;
2025
2026 return delta;
2027}
2028
Rik van Riel54009412014-10-17 03:29:53 -04002029/*
2030 * Determine the preferred nid for a task in a numa_group. This needs to
2031 * be done in a way that produces consistent results with group_weight,
2032 * otherwise workloads might not converge.
2033 */
2034static int preferred_group_nid(struct task_struct *p, int nid)
2035{
2036 nodemask_t nodes;
2037 int dist;
2038
2039 /* Direct connections between all NUMA nodes. */
2040 if (sched_numa_topology_type == NUMA_DIRECT)
2041 return nid;
2042
2043 /*
2044 * On a system with glueless mesh NUMA topology, group_weight
2045 * scores nodes according to the number of NUMA hinting faults on
2046 * both the node itself, and on nearby nodes.
2047 */
2048 if (sched_numa_topology_type == NUMA_GLUELESS_MESH) {
2049 unsigned long score, max_score = 0;
2050 int node, max_node = nid;
2051
2052 dist = sched_max_numa_distance;
2053
2054 for_each_online_node(node) {
2055 score = group_weight(p, node, dist);
2056 if (score > max_score) {
2057 max_score = score;
2058 max_node = node;
2059 }
2060 }
2061 return max_node;
2062 }
2063
2064 /*
2065 * Finding the preferred nid in a system with NUMA backplane
2066 * interconnect topology is more involved. The goal is to locate
2067 * tasks from numa_groups near each other in the system, and
2068 * untangle workloads from different sides of the system. This requires
2069 * searching down the hierarchy of node groups, recursively searching
2070 * inside the highest scoring group of nodes. The nodemask tricks
2071 * keep the complexity of the search down.
2072 */
2073 nodes = node_online_map;
2074 for (dist = sched_max_numa_distance; dist > LOCAL_DISTANCE; dist--) {
2075 unsigned long max_faults = 0;
Jan Beulich81907472015-01-23 08:25:38 +00002076 nodemask_t max_group = NODE_MASK_NONE;
Rik van Riel54009412014-10-17 03:29:53 -04002077 int a, b;
2078
2079 /* Are there nodes at this distance from each other? */
2080 if (!find_numa_distance(dist))
2081 continue;
2082
2083 for_each_node_mask(a, nodes) {
2084 unsigned long faults = 0;
2085 nodemask_t this_group;
2086 nodes_clear(this_group);
2087
2088 /* Sum group's NUMA faults; includes a==b case. */
2089 for_each_node_mask(b, nodes) {
2090 if (node_distance(a, b) < dist) {
2091 faults += group_faults(p, b);
2092 node_set(b, this_group);
2093 node_clear(b, nodes);
2094 }
2095 }
2096
2097 /* Remember the top group. */
2098 if (faults > max_faults) {
2099 max_faults = faults;
2100 max_group = this_group;
2101 /*
2102 * subtle: at the smallest distance there is
2103 * just one node left in each "group", the
2104 * winner is the preferred nid.
2105 */
2106 nid = a;
2107 }
2108 }
2109 /* Next round, evaluate the nodes within max_group. */
Jan Beulich890a5402015-02-09 12:30:00 +01002110 if (!max_faults)
2111 break;
Rik van Riel54009412014-10-17 03:29:53 -04002112 nodes = max_group;
2113 }
2114 return nid;
2115}
2116
Peter Zijlstracbee9f82012-10-25 14:16:43 +02002117static void task_numa_placement(struct task_struct *p)
2118{
Mel Gorman83e1d2c2013-10-07 11:29:27 +01002119 int seq, nid, max_nid = -1, max_group_nid = -1;
2120 unsigned long max_faults = 0, max_group_faults = 0;
Rik van Riel04bb2f92013-10-07 11:29:36 +01002121 unsigned long fault_types[2] = { 0, 0 };
Rik van Riel7e2703e2014-01-27 17:03:45 -05002122 unsigned long total_faults;
2123 u64 runtime, period;
Mel Gorman7dbd13e2013-10-07 11:29:29 +01002124 spinlock_t *group_lock = NULL;
Peter Zijlstracbee9f82012-10-25 14:16:43 +02002125
Jason Low7e5a2c12015-04-30 17:28:14 -07002126 /*
2127 * The p->mm->numa_scan_seq field gets updated without
2128 * exclusive access. Use READ_ONCE() here to ensure
2129 * that the field is read in a single access:
2130 */
Jason Low316c1608d2015-04-28 13:00:20 -07002131 seq = READ_ONCE(p->mm->numa_scan_seq);
Peter Zijlstracbee9f82012-10-25 14:16:43 +02002132 if (p->numa_scan_seq == seq)
2133 return;
2134 p->numa_scan_seq = seq;
Mel Gorman598f0ec2013-10-07 11:28:55 +01002135 p->numa_scan_period_max = task_scan_max(p);
Peter Zijlstracbee9f82012-10-25 14:16:43 +02002136
Rik van Riel7e2703e2014-01-27 17:03:45 -05002137 total_faults = p->numa_faults_locality[0] +
2138 p->numa_faults_locality[1];
2139 runtime = numa_get_avg_runtime(p, &period);
2140
Mel Gorman7dbd13e2013-10-07 11:29:29 +01002141 /* If the task is part of a group prevent parallel updates to group stats */
2142 if (p->numa_group) {
2143 group_lock = &p->numa_group->lock;
Mike Galbraith60e69ee2014-04-07 10:55:15 +02002144 spin_lock_irq(group_lock);
Mel Gorman7dbd13e2013-10-07 11:29:29 +01002145 }
2146
Mel Gorman688b7582013-10-07 11:28:58 +01002147 /* Find the node with the highest number of faults */
2148 for_each_online_node(nid) {
Iulia Manda44dba3d2014-10-31 02:13:31 +02002149 /* Keep track of the offsets in numa_faults array */
2150 int mem_idx, membuf_idx, cpu_idx, cpubuf_idx;
Mel Gorman83e1d2c2013-10-07 11:29:27 +01002151 unsigned long faults = 0, group_faults = 0;
Iulia Manda44dba3d2014-10-31 02:13:31 +02002152 int priv;
Mel Gorman745d6142013-10-07 11:28:59 +01002153
Rik van Rielbe1e4e72014-01-27 17:03:48 -05002154 for (priv = 0; priv < NR_NUMA_HINT_FAULT_TYPES; priv++) {
Rik van Riel7e2703e2014-01-27 17:03:45 -05002155 long diff, f_diff, f_weight;
Peter Zijlstra8c8a7432013-10-07 11:29:21 +01002156
Iulia Manda44dba3d2014-10-31 02:13:31 +02002157 mem_idx = task_faults_idx(NUMA_MEM, nid, priv);
2158 membuf_idx = task_faults_idx(NUMA_MEMBUF, nid, priv);
2159 cpu_idx = task_faults_idx(NUMA_CPU, nid, priv);
2160 cpubuf_idx = task_faults_idx(NUMA_CPUBUF, nid, priv);
Mel Gorman745d6142013-10-07 11:28:59 +01002161
Mel Gormanac8e8952013-10-07 11:29:03 +01002162 /* Decay existing window, copy faults since last scan */
Iulia Manda44dba3d2014-10-31 02:13:31 +02002163 diff = p->numa_faults[membuf_idx] - p->numa_faults[mem_idx] / 2;
2164 fault_types[priv] += p->numa_faults[membuf_idx];
2165 p->numa_faults[membuf_idx] = 0;
Mel Gormanfb13c7e2013-10-07 11:29:17 +01002166
Rik van Riel7e2703e2014-01-27 17:03:45 -05002167 /*
2168 * Normalize the faults_from, so all tasks in a group
2169 * count according to CPU use, instead of by the raw
2170 * number of faults. Tasks with little runtime have
2171 * little over-all impact on throughput, and thus their
2172 * faults are less important.
2173 */
2174 f_weight = div64_u64(runtime << 16, period + 1);
Iulia Manda44dba3d2014-10-31 02:13:31 +02002175 f_weight = (f_weight * p->numa_faults[cpubuf_idx]) /
Rik van Riel7e2703e2014-01-27 17:03:45 -05002176 (total_faults + 1);
Iulia Manda44dba3d2014-10-31 02:13:31 +02002177 f_diff = f_weight - p->numa_faults[cpu_idx] / 2;
2178 p->numa_faults[cpubuf_idx] = 0;
Rik van Riel50ec8a42014-01-27 17:03:42 -05002179
Iulia Manda44dba3d2014-10-31 02:13:31 +02002180 p->numa_faults[mem_idx] += diff;
2181 p->numa_faults[cpu_idx] += f_diff;
2182 faults += p->numa_faults[mem_idx];
Mel Gorman83e1d2c2013-10-07 11:29:27 +01002183 p->total_numa_faults += diff;
Peter Zijlstra8c8a7432013-10-07 11:29:21 +01002184 if (p->numa_group) {
Iulia Manda44dba3d2014-10-31 02:13:31 +02002185 /*
2186 * safe because we can only change our own group
2187 *
2188 * mem_idx represents the offset for a given
2189 * nid and priv in a specific region because it
2190 * is at the beginning of the numa_faults array.
2191 */
2192 p->numa_group->faults[mem_idx] += diff;
2193 p->numa_group->faults_cpu[mem_idx] += f_diff;
Mel Gorman989348b2013-10-07 11:29:40 +01002194 p->numa_group->total_faults += diff;
Iulia Manda44dba3d2014-10-31 02:13:31 +02002195 group_faults += p->numa_group->faults[mem_idx];
Peter Zijlstra8c8a7432013-10-07 11:29:21 +01002196 }
Mel Gormanac8e8952013-10-07 11:29:03 +01002197 }
2198
Mel Gorman688b7582013-10-07 11:28:58 +01002199 if (faults > max_faults) {
2200 max_faults = faults;
2201 max_nid = nid;
2202 }
Mel Gorman83e1d2c2013-10-07 11:29:27 +01002203
2204 if (group_faults > max_group_faults) {
2205 max_group_faults = group_faults;
2206 max_group_nid = nid;
2207 }
2208 }
2209
Rik van Riel04bb2f92013-10-07 11:29:36 +01002210 update_task_scan_period(p, fault_types[0], fault_types[1]);
2211
Mel Gorman7dbd13e2013-10-07 11:29:29 +01002212 if (p->numa_group) {
Rik van Riel4142c3e2016-01-25 17:07:39 -05002213 numa_group_count_active_nodes(p->numa_group);
Mike Galbraith60e69ee2014-04-07 10:55:15 +02002214 spin_unlock_irq(group_lock);
Rik van Riel54009412014-10-17 03:29:53 -04002215 max_nid = preferred_group_nid(p, max_group_nid);
Mel Gorman688b7582013-10-07 11:28:58 +01002216 }
2217
Rik van Rielbb97fc32014-06-04 16:33:15 -04002218 if (max_faults) {
2219 /* Set the new preferred node */
2220 if (max_nid != p->numa_preferred_nid)
2221 sched_setnuma(p, max_nid);
2222
2223 if (task_node(p) != p->numa_preferred_nid)
2224 numa_migrate_preferred(p);
Mel Gorman3a7053b2013-10-07 11:29:00 +01002225 }
Peter Zijlstracbee9f82012-10-25 14:16:43 +02002226}
2227
Peter Zijlstra8c8a7432013-10-07 11:29:21 +01002228static inline int get_numa_group(struct numa_group *grp)
2229{
2230 return atomic_inc_not_zero(&grp->refcount);
2231}
2232
2233static inline void put_numa_group(struct numa_group *grp)
2234{
2235 if (atomic_dec_and_test(&grp->refcount))
2236 kfree_rcu(grp, rcu);
2237}
2238
Mel Gorman3e6a9412013-10-07 11:29:35 +01002239static void task_numa_group(struct task_struct *p, int cpupid, int flags,
2240 int *priv)
Peter Zijlstra8c8a7432013-10-07 11:29:21 +01002241{
2242 struct numa_group *grp, *my_grp;
2243 struct task_struct *tsk;
2244 bool join = false;
2245 int cpu = cpupid_to_cpu(cpupid);
2246 int i;
2247
2248 if (unlikely(!p->numa_group)) {
2249 unsigned int size = sizeof(struct numa_group) +
Rik van Riel50ec8a42014-01-27 17:03:42 -05002250 4*nr_node_ids*sizeof(unsigned long);
Peter Zijlstra8c8a7432013-10-07 11:29:21 +01002251
2252 grp = kzalloc(size, GFP_KERNEL | __GFP_NOWARN);
2253 if (!grp)
2254 return;
2255
2256 atomic_set(&grp->refcount, 1);
Rik van Riel4142c3e2016-01-25 17:07:39 -05002257 grp->active_nodes = 1;
2258 grp->max_faults_cpu = 0;
Peter Zijlstra8c8a7432013-10-07 11:29:21 +01002259 spin_lock_init(&grp->lock);
Mel Gormane29cf082013-10-07 11:29:22 +01002260 grp->gid = p->pid;
Rik van Riel50ec8a42014-01-27 17:03:42 -05002261 /* Second half of the array tracks nids where faults happen */
Rik van Rielbe1e4e72014-01-27 17:03:48 -05002262 grp->faults_cpu = grp->faults + NR_NUMA_HINT_FAULT_TYPES *
2263 nr_node_ids;
Peter Zijlstra8c8a7432013-10-07 11:29:21 +01002264
Rik van Rielbe1e4e72014-01-27 17:03:48 -05002265 for (i = 0; i < NR_NUMA_HINT_FAULT_STATS * nr_node_ids; i++)
Iulia Manda44dba3d2014-10-31 02:13:31 +02002266 grp->faults[i] = p->numa_faults[i];
Peter Zijlstra8c8a7432013-10-07 11:29:21 +01002267
Mel Gorman989348b2013-10-07 11:29:40 +01002268 grp->total_faults = p->total_numa_faults;
Mel Gorman83e1d2c2013-10-07 11:29:27 +01002269
Peter Zijlstra8c8a7432013-10-07 11:29:21 +01002270 grp->nr_tasks++;
2271 rcu_assign_pointer(p->numa_group, grp);
2272 }
2273
2274 rcu_read_lock();
Jason Low316c1608d2015-04-28 13:00:20 -07002275 tsk = READ_ONCE(cpu_rq(cpu)->curr);
Peter Zijlstra8c8a7432013-10-07 11:29:21 +01002276
2277 if (!cpupid_match_pid(tsk, cpupid))
Peter Zijlstra33547812013-10-09 10:24:48 +02002278 goto no_join;
Peter Zijlstra8c8a7432013-10-07 11:29:21 +01002279
2280 grp = rcu_dereference(tsk->numa_group);
2281 if (!grp)
Peter Zijlstra33547812013-10-09 10:24:48 +02002282 goto no_join;
Peter Zijlstra8c8a7432013-10-07 11:29:21 +01002283
2284 my_grp = p->numa_group;
2285 if (grp == my_grp)
Peter Zijlstra33547812013-10-09 10:24:48 +02002286 goto no_join;
Peter Zijlstra8c8a7432013-10-07 11:29:21 +01002287
2288 /*
2289 * Only join the other group if its bigger; if we're the bigger group,
2290 * the other task will join us.
2291 */
2292 if (my_grp->nr_tasks > grp->nr_tasks)
Peter Zijlstra33547812013-10-09 10:24:48 +02002293 goto no_join;
Peter Zijlstra8c8a7432013-10-07 11:29:21 +01002294
2295 /*
2296 * Tie-break on the grp address.
2297 */
2298 if (my_grp->nr_tasks == grp->nr_tasks && my_grp > grp)
Peter Zijlstra33547812013-10-09 10:24:48 +02002299 goto no_join;
Peter Zijlstra8c8a7432013-10-07 11:29:21 +01002300
Rik van Rieldabe1d92013-10-07 11:29:34 +01002301 /* Always join threads in the same process. */
2302 if (tsk->mm == current->mm)
2303 join = true;
Peter Zijlstra8c8a7432013-10-07 11:29:21 +01002304
Rik van Rieldabe1d92013-10-07 11:29:34 +01002305 /* Simple filter to avoid false positives due to PID collisions */
2306 if (flags & TNF_SHARED)
2307 join = true;
2308
Mel Gorman3e6a9412013-10-07 11:29:35 +01002309 /* Update priv based on whether false sharing was detected */
2310 *priv = !join;
2311
Rik van Rieldabe1d92013-10-07 11:29:34 +01002312 if (join && !get_numa_group(grp))
Peter Zijlstra33547812013-10-09 10:24:48 +02002313 goto no_join;
Peter Zijlstra8c8a7432013-10-07 11:29:21 +01002314
Peter Zijlstra8c8a7432013-10-07 11:29:21 +01002315 rcu_read_unlock();
2316
2317 if (!join)
2318 return;
2319
Mike Galbraith60e69ee2014-04-07 10:55:15 +02002320 BUG_ON(irqs_disabled());
2321 double_lock_irq(&my_grp->lock, &grp->lock);
Peter Zijlstra8c8a7432013-10-07 11:29:21 +01002322
Rik van Rielbe1e4e72014-01-27 17:03:48 -05002323 for (i = 0; i < NR_NUMA_HINT_FAULT_STATS * nr_node_ids; i++) {
Iulia Manda44dba3d2014-10-31 02:13:31 +02002324 my_grp->faults[i] -= p->numa_faults[i];
2325 grp->faults[i] += p->numa_faults[i];
Mel Gorman989348b2013-10-07 11:29:40 +01002326 }
2327 my_grp->total_faults -= p->total_numa_faults;
2328 grp->total_faults += p->total_numa_faults;
2329
Peter Zijlstra8c8a7432013-10-07 11:29:21 +01002330 my_grp->nr_tasks--;
2331 grp->nr_tasks++;
2332
2333 spin_unlock(&my_grp->lock);
Mike Galbraith60e69ee2014-04-07 10:55:15 +02002334 spin_unlock_irq(&grp->lock);
Peter Zijlstra8c8a7432013-10-07 11:29:21 +01002335
2336 rcu_assign_pointer(p->numa_group, grp);
2337
2338 put_numa_group(my_grp);
Peter Zijlstra33547812013-10-09 10:24:48 +02002339 return;
2340
2341no_join:
2342 rcu_read_unlock();
2343 return;
Peter Zijlstra8c8a7432013-10-07 11:29:21 +01002344}
2345
2346void task_numa_free(struct task_struct *p)
2347{
2348 struct numa_group *grp = p->numa_group;
Iulia Manda44dba3d2014-10-31 02:13:31 +02002349 void *numa_faults = p->numa_faults;
Steven Rostedte9dd6852014-05-27 17:02:04 -04002350 unsigned long flags;
2351 int i;
Peter Zijlstra8c8a7432013-10-07 11:29:21 +01002352
2353 if (grp) {
Steven Rostedte9dd6852014-05-27 17:02:04 -04002354 spin_lock_irqsave(&grp->lock, flags);
Rik van Rielbe1e4e72014-01-27 17:03:48 -05002355 for (i = 0; i < NR_NUMA_HINT_FAULT_STATS * nr_node_ids; i++)
Iulia Manda44dba3d2014-10-31 02:13:31 +02002356 grp->faults[i] -= p->numa_faults[i];
Mel Gorman989348b2013-10-07 11:29:40 +01002357 grp->total_faults -= p->total_numa_faults;
2358
Peter Zijlstra8c8a7432013-10-07 11:29:21 +01002359 grp->nr_tasks--;
Steven Rostedte9dd6852014-05-27 17:02:04 -04002360 spin_unlock_irqrestore(&grp->lock, flags);
Andreea-Cristina Bernat35b123e2014-08-22 17:50:43 +03002361 RCU_INIT_POINTER(p->numa_group, NULL);
Peter Zijlstra8c8a7432013-10-07 11:29:21 +01002362 put_numa_group(grp);
2363 }
2364
Iulia Manda44dba3d2014-10-31 02:13:31 +02002365 p->numa_faults = NULL;
Rik van Riel82727012013-10-07 11:29:28 +01002366 kfree(numa_faults);
Peter Zijlstra8c8a7432013-10-07 11:29:21 +01002367}
2368
Peter Zijlstracbee9f82012-10-25 14:16:43 +02002369/*
2370 * Got a PROT_NONE fault for a page on @node.
2371 */
Rik van Riel58b46da2014-01-27 17:03:47 -05002372void task_numa_fault(int last_cpupid, int mem_node, int pages, int flags)
Peter Zijlstracbee9f82012-10-25 14:16:43 +02002373{
2374 struct task_struct *p = current;
Peter Zijlstra6688cc02013-10-07 11:29:24 +01002375 bool migrated = flags & TNF_MIGRATED;
Rik van Riel58b46da2014-01-27 17:03:47 -05002376 int cpu_node = task_node(current);
Rik van Riel792568e2014-04-11 13:00:27 -04002377 int local = !!(flags & TNF_FAULT_LOCAL);
Rik van Riel4142c3e2016-01-25 17:07:39 -05002378 struct numa_group *ng;
Mel Gormanac8e8952013-10-07 11:29:03 +01002379 int priv;
Peter Zijlstracbee9f82012-10-25 14:16:43 +02002380
Srikar Dronamraju2a595722015-08-11 21:54:21 +05302381 if (!static_branch_likely(&sched_numa_balancing))
Mel Gorman1a687c22012-11-22 11:16:36 +00002382 return;
2383
Mel Gorman9ff1d9f2013-10-07 11:29:04 +01002384 /* for example, ksmd faulting in a user's mm */
2385 if (!p->mm)
2386 return;
2387
Mel Gormanf809ca92013-10-07 11:28:57 +01002388 /* Allocate buffer to track faults on a per-node basis */
Iulia Manda44dba3d2014-10-31 02:13:31 +02002389 if (unlikely(!p->numa_faults)) {
2390 int size = sizeof(*p->numa_faults) *
Rik van Rielbe1e4e72014-01-27 17:03:48 -05002391 NR_NUMA_HINT_FAULT_BUCKETS * nr_node_ids;
Mel Gormanf809ca92013-10-07 11:28:57 +01002392
Iulia Manda44dba3d2014-10-31 02:13:31 +02002393 p->numa_faults = kzalloc(size, GFP_KERNEL|__GFP_NOWARN);
2394 if (!p->numa_faults)
Mel Gormanf809ca92013-10-07 11:28:57 +01002395 return;
Mel Gorman745d6142013-10-07 11:28:59 +01002396
Mel Gorman83e1d2c2013-10-07 11:29:27 +01002397 p->total_numa_faults = 0;
Rik van Riel04bb2f92013-10-07 11:29:36 +01002398 memset(p->numa_faults_locality, 0, sizeof(p->numa_faults_locality));
Mel Gormanf809ca92013-10-07 11:28:57 +01002399 }
Peter Zijlstracbee9f82012-10-25 14:16:43 +02002400
Mel Gormanfb003b82012-11-15 09:01:14 +00002401 /*
Peter Zijlstra8c8a7432013-10-07 11:29:21 +01002402 * First accesses are treated as private, otherwise consider accesses
2403 * to be private if the accessing pid has not changed
2404 */
2405 if (unlikely(last_cpupid == (-1 & LAST_CPUPID_MASK))) {
2406 priv = 1;
2407 } else {
2408 priv = cpupid_match_pid(p, last_cpupid);
Peter Zijlstra6688cc02013-10-07 11:29:24 +01002409 if (!priv && !(flags & TNF_NO_GROUP))
Mel Gorman3e6a9412013-10-07 11:29:35 +01002410 task_numa_group(p, last_cpupid, flags, &priv);
Peter Zijlstra8c8a7432013-10-07 11:29:21 +01002411 }
2412
Rik van Riel792568e2014-04-11 13:00:27 -04002413 /*
2414 * If a workload spans multiple NUMA nodes, a shared fault that
2415 * occurs wholly within the set of nodes that the workload is
2416 * actively using should be counted as local. This allows the
2417 * scan rate to slow down when a workload has settled down.
2418 */
Rik van Riel4142c3e2016-01-25 17:07:39 -05002419 ng = p->numa_group;
2420 if (!priv && !local && ng && ng->active_nodes > 1 &&
2421 numa_is_active_node(cpu_node, ng) &&
2422 numa_is_active_node(mem_node, ng))
Rik van Riel792568e2014-04-11 13:00:27 -04002423 local = 1;
2424
Peter Zijlstracbee9f82012-10-25 14:16:43 +02002425 task_numa_placement(p);
Mel Gormanf809ca92013-10-07 11:28:57 +01002426
Rik van Riel2739d3e2013-10-07 11:29:41 +01002427 /*
2428 * Retry task to preferred node migration periodically, in case it
2429 * case it previously failed, or the scheduler moved us.
2430 */
2431 if (time_after(jiffies, p->numa_migrate_retry))
Mel Gorman6b9a7462013-10-07 11:29:11 +01002432 numa_migrate_preferred(p);
2433
Ingo Molnarb32e86b2013-10-07 11:29:30 +01002434 if (migrated)
2435 p->numa_pages_migrated += pages;
Mel Gorman074c2382015-03-25 15:55:42 -07002436 if (flags & TNF_MIGRATE_FAIL)
2437 p->numa_faults_locality[2] += pages;
Ingo Molnarb32e86b2013-10-07 11:29:30 +01002438
Iulia Manda44dba3d2014-10-31 02:13:31 +02002439 p->numa_faults[task_faults_idx(NUMA_MEMBUF, mem_node, priv)] += pages;
2440 p->numa_faults[task_faults_idx(NUMA_CPUBUF, cpu_node, priv)] += pages;
Rik van Riel792568e2014-04-11 13:00:27 -04002441 p->numa_faults_locality[local] += pages;
Peter Zijlstracbee9f82012-10-25 14:16:43 +02002442}
2443
Peter Zijlstra6e5fb222012-10-25 14:16:45 +02002444static void reset_ptenuma_scan(struct task_struct *p)
2445{
Jason Low7e5a2c12015-04-30 17:28:14 -07002446 /*
2447 * We only did a read acquisition of the mmap sem, so
2448 * p->mm->numa_scan_seq is written to without exclusive access
2449 * and the update is not guaranteed to be atomic. That's not
2450 * much of an issue though, since this is just used for
2451 * statistical sampling. Use READ_ONCE/WRITE_ONCE, which are not
2452 * expensive, to avoid any form of compiler optimizations:
2453 */
Jason Low316c1608d2015-04-28 13:00:20 -07002454 WRITE_ONCE(p->mm->numa_scan_seq, READ_ONCE(p->mm->numa_scan_seq) + 1);
Peter Zijlstra6e5fb222012-10-25 14:16:45 +02002455 p->mm->numa_scan_offset = 0;
2456}
2457
Peter Zijlstracbee9f82012-10-25 14:16:43 +02002458/*
2459 * The expensive part of numa migration is done from task_work context.
2460 * Triggered from task_tick_numa().
2461 */
2462void task_numa_work(struct callback_head *work)
2463{
2464 unsigned long migrate, next_scan, now = jiffies;
2465 struct task_struct *p = current;
2466 struct mm_struct *mm = p->mm;
Rik van Riel51170842015-11-05 15:56:23 -05002467 u64 runtime = p->se.sum_exec_runtime;
Peter Zijlstra6e5fb222012-10-25 14:16:45 +02002468 struct vm_area_struct *vma;
Mel Gorman9f406042012-11-14 18:34:32 +00002469 unsigned long start, end;
Mel Gorman598f0ec2013-10-07 11:28:55 +01002470 unsigned long nr_pte_updates = 0;
Rik van Riel4620f8c2015-09-11 09:00:27 -04002471 long pages, virtpages;
Peter Zijlstracbee9f82012-10-25 14:16:43 +02002472
Peter Zijlstra9148a3a2016-09-20 22:34:51 +02002473 SCHED_WARN_ON(p != container_of(work, struct task_struct, numa_work));
Peter Zijlstracbee9f82012-10-25 14:16:43 +02002474
2475 work->next = work; /* protect against double add */
2476 /*
2477 * Who cares about NUMA placement when they're dying.
2478 *
2479 * NOTE: make sure not to dereference p->mm before this check,
2480 * exit_task_work() happens _after_ exit_mm() so we could be called
2481 * without p->mm even though we still had it when we enqueued this
2482 * work.
2483 */
2484 if (p->flags & PF_EXITING)
2485 return;
2486
Mel Gorman930aa172013-10-07 11:29:37 +01002487 if (!mm->numa_next_scan) {
Mel Gorman7e8d16b2013-10-07 11:28:54 +01002488 mm->numa_next_scan = now +
2489 msecs_to_jiffies(sysctl_numa_balancing_scan_delay);
Mel Gormanb8593bf2012-11-21 01:18:23 +00002490 }
2491
2492 /*
Peter Zijlstracbee9f82012-10-25 14:16:43 +02002493 * Enforce maximal scan/migration frequency..
2494 */
2495 migrate = mm->numa_next_scan;
2496 if (time_before(now, migrate))
2497 return;
2498
Mel Gorman598f0ec2013-10-07 11:28:55 +01002499 if (p->numa_scan_period == 0) {
2500 p->numa_scan_period_max = task_scan_max(p);
Rik van Rielb5dd77c2017-07-31 15:28:47 -04002501 p->numa_scan_period = task_scan_start(p);
Mel Gorman598f0ec2013-10-07 11:28:55 +01002502 }
Peter Zijlstracbee9f82012-10-25 14:16:43 +02002503
Mel Gormanfb003b82012-11-15 09:01:14 +00002504 next_scan = now + msecs_to_jiffies(p->numa_scan_period);
Peter Zijlstracbee9f82012-10-25 14:16:43 +02002505 if (cmpxchg(&mm->numa_next_scan, migrate, next_scan) != migrate)
2506 return;
2507
Mel Gormane14808b2012-11-19 10:59:15 +00002508 /*
Peter Zijlstra19a78d12013-10-07 11:28:51 +01002509 * Delay this task enough that another task of this mm will likely win
2510 * the next time around.
2511 */
2512 p->node_stamp += 2 * TICK_NSEC;
2513
Mel Gorman9f406042012-11-14 18:34:32 +00002514 start = mm->numa_scan_offset;
2515 pages = sysctl_numa_balancing_scan_size;
2516 pages <<= 20 - PAGE_SHIFT; /* MB in pages */
Rik van Riel4620f8c2015-09-11 09:00:27 -04002517 virtpages = pages * 8; /* Scan up to this much virtual space */
Mel Gorman9f406042012-11-14 18:34:32 +00002518 if (!pages)
2519 return;
Peter Zijlstracbee9f82012-10-25 14:16:43 +02002520
Rik van Riel4620f8c2015-09-11 09:00:27 -04002521
Vlastimil Babka8655d542017-05-15 15:13:16 +02002522 if (!down_read_trylock(&mm->mmap_sem))
2523 return;
Mel Gorman9f406042012-11-14 18:34:32 +00002524 vma = find_vma(mm, start);
Peter Zijlstra6e5fb222012-10-25 14:16:45 +02002525 if (!vma) {
2526 reset_ptenuma_scan(p);
Mel Gorman9f406042012-11-14 18:34:32 +00002527 start = 0;
Peter Zijlstra6e5fb222012-10-25 14:16:45 +02002528 vma = mm->mmap;
Peter Zijlstracbee9f82012-10-25 14:16:43 +02002529 }
Mel Gorman9f406042012-11-14 18:34:32 +00002530 for (; vma; vma = vma->vm_next) {
Naoya Horiguchi6b79c572015-04-07 14:26:47 -07002531 if (!vma_migratable(vma) || !vma_policy_mof(vma) ||
Mel Gorman8e76d4e2015-06-10 11:15:00 -07002532 is_vm_hugetlb_page(vma) || (vma->vm_flags & VM_MIXEDMAP)) {
Peter Zijlstra6e5fb222012-10-25 14:16:45 +02002533 continue;
Naoya Horiguchi6b79c572015-04-07 14:26:47 -07002534 }
Peter Zijlstra6e5fb222012-10-25 14:16:45 +02002535
Mel Gorman4591ce4f2013-10-07 11:29:13 +01002536 /*
2537 * Shared library pages mapped by multiple processes are not
2538 * migrated as it is expected they are cache replicated. Avoid
2539 * hinting faults in read-only file-backed mappings or the vdso
2540 * as migrating the pages will be of marginal benefit.
2541 */
2542 if (!vma->vm_mm ||
2543 (vma->vm_file && (vma->vm_flags & (VM_READ|VM_WRITE)) == (VM_READ)))
2544 continue;
2545
Mel Gorman3c67f472013-12-18 17:08:40 -08002546 /*
2547 * Skip inaccessible VMAs to avoid any confusion between
2548 * PROT_NONE and NUMA hinting ptes
2549 */
2550 if (!(vma->vm_flags & (VM_READ | VM_EXEC | VM_WRITE)))
2551 continue;
2552
Mel Gorman9f406042012-11-14 18:34:32 +00002553 do {
2554 start = max(start, vma->vm_start);
2555 end = ALIGN(start + (pages << PAGE_SHIFT), HPAGE_SIZE);
2556 end = min(end, vma->vm_end);
Rik van Riel4620f8c2015-09-11 09:00:27 -04002557 nr_pte_updates = change_prot_numa(vma, start, end);
Mel Gorman598f0ec2013-10-07 11:28:55 +01002558
2559 /*
Rik van Riel4620f8c2015-09-11 09:00:27 -04002560 * Try to scan sysctl_numa_balancing_size worth of
2561 * hpages that have at least one present PTE that
2562 * is not already pte-numa. If the VMA contains
2563 * areas that are unused or already full of prot_numa
2564 * PTEs, scan up to virtpages, to skip through those
2565 * areas faster.
Mel Gorman598f0ec2013-10-07 11:28:55 +01002566 */
2567 if (nr_pte_updates)
2568 pages -= (end - start) >> PAGE_SHIFT;
Rik van Riel4620f8c2015-09-11 09:00:27 -04002569 virtpages -= (end - start) >> PAGE_SHIFT;
Peter Zijlstra6e5fb222012-10-25 14:16:45 +02002570
Mel Gorman9f406042012-11-14 18:34:32 +00002571 start = end;
Rik van Riel4620f8c2015-09-11 09:00:27 -04002572 if (pages <= 0 || virtpages <= 0)
Mel Gorman9f406042012-11-14 18:34:32 +00002573 goto out;
Rik van Riel3cf19622014-02-18 17:12:44 -05002574
2575 cond_resched();
Mel Gorman9f406042012-11-14 18:34:32 +00002576 } while (end != vma->vm_end);
Peter Zijlstra6e5fb222012-10-25 14:16:45 +02002577 }
2578
Mel Gorman9f406042012-11-14 18:34:32 +00002579out:
Peter Zijlstra6e5fb222012-10-25 14:16:45 +02002580 /*
Peter Zijlstrac69307d2013-10-07 11:28:41 +01002581 * It is possible to reach the end of the VMA list but the last few
2582 * VMAs are not guaranteed to the vma_migratable. If they are not, we
2583 * would find the !migratable VMA on the next scan but not reset the
2584 * scanner to the start so check it now.
Peter Zijlstra6e5fb222012-10-25 14:16:45 +02002585 */
2586 if (vma)
Mel Gorman9f406042012-11-14 18:34:32 +00002587 mm->numa_scan_offset = start;
Peter Zijlstra6e5fb222012-10-25 14:16:45 +02002588 else
2589 reset_ptenuma_scan(p);
2590 up_read(&mm->mmap_sem);
Rik van Riel51170842015-11-05 15:56:23 -05002591
2592 /*
2593 * Make sure tasks use at least 32x as much time to run other code
2594 * than they used here, to limit NUMA PTE scanning overhead to 3% max.
2595 * Usually update_task_scan_period slows down scanning enough; on an
2596 * overloaded system we need to limit overhead on a per task basis.
2597 */
2598 if (unlikely(p->se.sum_exec_runtime != runtime)) {
2599 u64 diff = p->se.sum_exec_runtime - runtime;
2600 p->node_stamp += 32 * diff;
2601 }
Peter Zijlstracbee9f82012-10-25 14:16:43 +02002602}
2603
2604/*
2605 * Drive the periodic memory faults..
2606 */
2607void task_tick_numa(struct rq *rq, struct task_struct *curr)
2608{
2609 struct callback_head *work = &curr->numa_work;
2610 u64 period, now;
2611
2612 /*
2613 * We don't care about NUMA placement if we don't have memory.
2614 */
2615 if (!curr->mm || (curr->flags & PF_EXITING) || work->next != work)
2616 return;
2617
2618 /*
2619 * Using runtime rather than walltime has the dual advantage that
2620 * we (mostly) drive the selection from busy threads and that the
2621 * task needs to have done some actual work before we bother with
2622 * NUMA placement.
2623 */
2624 now = curr->se.sum_exec_runtime;
2625 period = (u64)curr->numa_scan_period * NSEC_PER_MSEC;
2626
Rik van Riel25b3e5a2015-11-05 15:56:22 -05002627 if (now > curr->node_stamp + period) {
Peter Zijlstra4b96a292012-10-25 14:16:47 +02002628 if (!curr->node_stamp)
Rik van Rielb5dd77c2017-07-31 15:28:47 -04002629 curr->numa_scan_period = task_scan_start(curr);
Peter Zijlstra19a78d12013-10-07 11:28:51 +01002630 curr->node_stamp += period;
Peter Zijlstracbee9f82012-10-25 14:16:43 +02002631
2632 if (!time_before(jiffies, curr->mm->numa_next_scan)) {
2633 init_task_work(work, task_numa_work); /* TODO: move this into sched_fork() */
2634 task_work_add(curr, work, true);
2635 }
2636 }
2637}
Rik van Riel3fed3822017-06-23 12:55:29 -04002638
Peter Zijlstracbee9f82012-10-25 14:16:43 +02002639#else
2640static void task_tick_numa(struct rq *rq, struct task_struct *curr)
2641{
2642}
Peter Zijlstra0ec8aa02013-10-07 11:29:33 +01002643
2644static inline void account_numa_enqueue(struct rq *rq, struct task_struct *p)
2645{
2646}
2647
2648static inline void account_numa_dequeue(struct rq *rq, struct task_struct *p)
2649{
2650}
Rik van Riel3fed3822017-06-23 12:55:29 -04002651
Peter Zijlstracbee9f82012-10-25 14:16:43 +02002652#endif /* CONFIG_NUMA_BALANCING */
2653
Dmitry Adamushko30cfdcf2007-10-15 17:00:07 +02002654static void
2655account_entity_enqueue(struct cfs_rq *cfs_rq, struct sched_entity *se)
2656{
2657 update_load_add(&cfs_rq->load, se->load.weight);
Peter Zijlstrac09595f2008-06-27 13:41:14 +02002658 if (!parent_entity(se))
Peter Zijlstra029632f2011-10-25 10:00:11 +02002659 update_load_add(&rq_of(cfs_rq)->load, se->load.weight);
Peter Zijlstra367456c2012-02-20 21:49:09 +01002660#ifdef CONFIG_SMP
Peter Zijlstra0ec8aa02013-10-07 11:29:33 +01002661 if (entity_is_task(se)) {
2662 struct rq *rq = rq_of(cfs_rq);
2663
2664 account_numa_enqueue(rq, task_of(se));
2665 list_add(&se->group_node, &rq->cfs_tasks);
2666 }
Peter Zijlstra367456c2012-02-20 21:49:09 +01002667#endif
Dmitry Adamushko30cfdcf2007-10-15 17:00:07 +02002668 cfs_rq->nr_running++;
Dmitry Adamushko30cfdcf2007-10-15 17:00:07 +02002669}
2670
2671static void
2672account_entity_dequeue(struct cfs_rq *cfs_rq, struct sched_entity *se)
2673{
2674 update_load_sub(&cfs_rq->load, se->load.weight);
Peter Zijlstrac09595f2008-06-27 13:41:14 +02002675 if (!parent_entity(se))
Peter Zijlstra029632f2011-10-25 10:00:11 +02002676 update_load_sub(&rq_of(cfs_rq)->load, se->load.weight);
Tim Chenbfdb1982016-02-01 14:47:59 -08002677#ifdef CONFIG_SMP
Peter Zijlstra0ec8aa02013-10-07 11:29:33 +01002678 if (entity_is_task(se)) {
2679 account_numa_dequeue(rq_of(cfs_rq), task_of(se));
Bharata B Raob87f1722008-09-25 09:53:54 +05302680 list_del_init(&se->group_node);
Peter Zijlstra0ec8aa02013-10-07 11:29:33 +01002681 }
Tim Chenbfdb1982016-02-01 14:47:59 -08002682#endif
Dmitry Adamushko30cfdcf2007-10-15 17:00:07 +02002683 cfs_rq->nr_running--;
Dmitry Adamushko30cfdcf2007-10-15 17:00:07 +02002684}
2685
Peter Zijlstra8d5b9022017-08-24 17:45:35 +02002686/*
2687 * Signed add and clamp on underflow.
2688 *
2689 * Explicitly do a load-store to ensure the intermediate value never hits
2690 * memory. This allows lockless observations without ever seeing the negative
2691 * values.
2692 */
2693#define add_positive(_ptr, _val) do { \
2694 typeof(_ptr) ptr = (_ptr); \
2695 typeof(_val) val = (_val); \
2696 typeof(*ptr) res, var = READ_ONCE(*ptr); \
2697 \
2698 res = var + val; \
2699 \
2700 if (val < 0 && res > var) \
2701 res = 0; \
2702 \
2703 WRITE_ONCE(*ptr, res); \
2704} while (0)
2705
2706/*
2707 * Unsigned subtract and clamp on underflow.
2708 *
2709 * Explicitly do a load-store to ensure the intermediate value never hits
2710 * memory. This allows lockless observations without ever seeing the negative
2711 * values.
2712 */
2713#define sub_positive(_ptr, _val) do { \
2714 typeof(_ptr) ptr = (_ptr); \
2715 typeof(*ptr) val = (_val); \
2716 typeof(*ptr) res, var = READ_ONCE(*ptr); \
2717 res = var - val; \
2718 if (res > var) \
2719 res = 0; \
2720 WRITE_ONCE(*ptr, res); \
2721} while (0)
2722
2723#ifdef CONFIG_SMP
2724/*
Peter Zijlstra1ea6c462017-05-06 15:59:54 +02002725 * XXX we want to get rid of these helpers and use the full load resolution.
Peter Zijlstra8d5b9022017-08-24 17:45:35 +02002726 */
2727static inline long se_weight(struct sched_entity *se)
2728{
2729 return scale_load_down(se->load.weight);
2730}
2731
Peter Zijlstra1ea6c462017-05-06 15:59:54 +02002732static inline long se_runnable(struct sched_entity *se)
2733{
2734 return scale_load_down(se->runnable_weight);
2735}
2736
Peter Zijlstra8d5b9022017-08-24 17:45:35 +02002737static inline void
2738enqueue_runnable_load_avg(struct cfs_rq *cfs_rq, struct sched_entity *se)
2739{
Peter Zijlstra1ea6c462017-05-06 15:59:54 +02002740 cfs_rq->runnable_weight += se->runnable_weight;
2741
2742 cfs_rq->avg.runnable_load_avg += se->avg.runnable_load_avg;
2743 cfs_rq->avg.runnable_load_sum += se_runnable(se) * se->avg.runnable_load_sum;
Peter Zijlstra8d5b9022017-08-24 17:45:35 +02002744}
2745
2746static inline void
2747dequeue_runnable_load_avg(struct cfs_rq *cfs_rq, struct sched_entity *se)
2748{
Peter Zijlstra1ea6c462017-05-06 15:59:54 +02002749 cfs_rq->runnable_weight -= se->runnable_weight;
2750
2751 sub_positive(&cfs_rq->avg.runnable_load_avg, se->avg.runnable_load_avg);
2752 sub_positive(&cfs_rq->avg.runnable_load_sum,
2753 se_runnable(se) * se->avg.runnable_load_sum);
Peter Zijlstra8d5b9022017-08-24 17:45:35 +02002754}
2755
2756static inline void
2757enqueue_load_avg(struct cfs_rq *cfs_rq, struct sched_entity *se)
2758{
2759 cfs_rq->avg.load_avg += se->avg.load_avg;
2760 cfs_rq->avg.load_sum += se_weight(se) * se->avg.load_sum;
2761}
2762
2763static inline void
2764dequeue_load_avg(struct cfs_rq *cfs_rq, struct sched_entity *se)
2765{
2766 sub_positive(&cfs_rq->avg.load_avg, se->avg.load_avg);
2767 sub_positive(&cfs_rq->avg.load_sum, se_weight(se) * se->avg.load_sum);
2768}
2769#else
2770static inline void
2771enqueue_runnable_load_avg(struct cfs_rq *cfs_rq, struct sched_entity *se) { }
2772static inline void
2773dequeue_runnable_load_avg(struct cfs_rq *cfs_rq, struct sched_entity *se) { }
2774static inline void
2775enqueue_load_avg(struct cfs_rq *cfs_rq, struct sched_entity *se) { }
2776static inline void
2777dequeue_load_avg(struct cfs_rq *cfs_rq, struct sched_entity *se) { }
2778#endif
2779
Vincent Guittot90593932017-05-17 11:50:45 +02002780static void reweight_entity(struct cfs_rq *cfs_rq, struct sched_entity *se,
Peter Zijlstra1ea6c462017-05-06 15:59:54 +02002781 unsigned long weight, unsigned long runnable)
Vincent Guittot90593932017-05-17 11:50:45 +02002782{
2783 if (se->on_rq) {
2784 /* commit outstanding execution time */
2785 if (cfs_rq->curr == se)
2786 update_curr(cfs_rq);
2787 account_entity_dequeue(cfs_rq, se);
2788 dequeue_runnable_load_avg(cfs_rq, se);
2789 }
2790 dequeue_load_avg(cfs_rq, se);
2791
Peter Zijlstra1ea6c462017-05-06 15:59:54 +02002792 se->runnable_weight = runnable;
Vincent Guittot90593932017-05-17 11:50:45 +02002793 update_load_set(&se->load, weight);
2794
2795#ifdef CONFIG_SMP
Peter Zijlstra1ea6c462017-05-06 15:59:54 +02002796 do {
2797 u32 divider = LOAD_AVG_MAX - 1024 + se->avg.period_contrib;
2798
2799 se->avg.load_avg = div_u64(se_weight(se) * se->avg.load_sum, divider);
2800 se->avg.runnable_load_avg =
2801 div_u64(se_runnable(se) * se->avg.runnable_load_sum, divider);
2802 } while (0);
Vincent Guittot90593932017-05-17 11:50:45 +02002803#endif
2804
2805 enqueue_load_avg(cfs_rq, se);
2806 if (se->on_rq) {
2807 account_entity_enqueue(cfs_rq, se);
2808 enqueue_runnable_load_avg(cfs_rq, se);
2809 }
2810}
2811
2812void reweight_task(struct task_struct *p, int prio)
2813{
2814 struct sched_entity *se = &p->se;
2815 struct cfs_rq *cfs_rq = cfs_rq_of(se);
2816 struct load_weight *load = &se->load;
2817 unsigned long weight = scale_load(sched_prio_to_weight[prio]);
2818
Peter Zijlstra1ea6c462017-05-06 15:59:54 +02002819 reweight_entity(cfs_rq, se, weight, weight);
Vincent Guittot90593932017-05-17 11:50:45 +02002820 load->inv_weight = sched_prio_to_wmult[prio];
2821}
2822
Yong Zhang3ff6dca2011-01-24 15:33:52 +08002823#ifdef CONFIG_FAIR_GROUP_SCHED
2824# ifdef CONFIG_SMP
Peter Zijlstracef27402017-05-09 11:04:07 +02002825/*
2826 * All this does is approximate the hierarchical proportion which includes that
2827 * global sum we all love to hate.
2828 *
2829 * That is, the weight of a group entity, is the proportional share of the
2830 * group weight based on the group runqueue weights. That is:
2831 *
2832 * tg->weight * grq->load.weight
2833 * ge->load.weight = ----------------------------- (1)
2834 * \Sum grq->load.weight
2835 *
2836 * Now, because computing that sum is prohibitively expensive to compute (been
2837 * there, done that) we approximate it with this average stuff. The average
2838 * moves slower and therefore the approximation is cheaper and more stable.
2839 *
2840 * So instead of the above, we substitute:
2841 *
2842 * grq->load.weight -> grq->avg.load_avg (2)
2843 *
2844 * which yields the following:
2845 *
2846 * tg->weight * grq->avg.load_avg
2847 * ge->load.weight = ------------------------------ (3)
2848 * tg->load_avg
2849 *
2850 * Where: tg->load_avg ~= \Sum grq->avg.load_avg
2851 *
2852 * That is shares_avg, and it is right (given the approximation (2)).
2853 *
2854 * The problem with it is that because the average is slow -- it was designed
2855 * to be exactly that of course -- this leads to transients in boundary
2856 * conditions. In specific, the case where the group was idle and we start the
2857 * one task. It takes time for our CPU's grq->avg.load_avg to build up,
2858 * yielding bad latency etc..
2859 *
2860 * Now, in that special case (1) reduces to:
2861 *
2862 * tg->weight * grq->load.weight
Peter Zijlstra17de4ee2017-08-24 13:06:35 +02002863 * ge->load.weight = ----------------------------- = tg->weight (4)
Peter Zijlstracef27402017-05-09 11:04:07 +02002864 * grp->load.weight
2865 *
2866 * That is, the sum collapses because all other CPUs are idle; the UP scenario.
2867 *
2868 * So what we do is modify our approximation (3) to approach (4) in the (near)
2869 * UP case, like:
2870 *
2871 * ge->load.weight =
2872 *
2873 * tg->weight * grq->load.weight
2874 * --------------------------------------------------- (5)
2875 * tg->load_avg - grq->avg.load_avg + grq->load.weight
2876 *
Peter Zijlstra17de4ee2017-08-24 13:06:35 +02002877 * But because grq->load.weight can drop to 0, resulting in a divide by zero,
2878 * we need to use grq->avg.load_avg as its lower bound, which then gives:
2879 *
2880 *
2881 * tg->weight * grq->load.weight
2882 * ge->load.weight = ----------------------------- (6)
2883 * tg_load_avg'
2884 *
2885 * Where:
2886 *
2887 * tg_load_avg' = tg->load_avg - grq->avg.load_avg +
2888 * max(grq->load.weight, grq->avg.load_avg)
Peter Zijlstracef27402017-05-09 11:04:07 +02002889 *
2890 * And that is shares_weight and is icky. In the (near) UP case it approaches
2891 * (4) while in the normal case it approaches (3). It consistently
2892 * overestimates the ge->load.weight and therefore:
2893 *
2894 * \Sum ge->load.weight >= tg->weight
2895 *
2896 * hence icky!
2897 */
Josef Bacik2c8e4dc2017-08-03 11:13:39 -04002898static long calc_group_shares(struct cfs_rq *cfs_rq)
Yong Zhang3ff6dca2011-01-24 15:33:52 +08002899{
Peter Zijlstra7c80cfc2017-05-06 16:03:17 +02002900 long tg_weight, tg_shares, load, shares;
2901 struct task_group *tg = cfs_rq->tg;
2902
2903 tg_shares = READ_ONCE(tg->shares);
Yong Zhang3ff6dca2011-01-24 15:33:52 +08002904
Peter Zijlstra3d4b60d2017-05-11 18:16:06 +02002905 load = max(scale_load_down(cfs_rq->load.weight), cfs_rq->avg.load_avg);
Peter Zijlstraea1dc6f2016-06-24 16:11:02 +02002906
2907 tg_weight = atomic_long_read(&tg->load_avg);
2908
2909 /* Ensure tg_weight >= load */
2910 tg_weight -= cfs_rq->tg_load_avg_contrib;
2911 tg_weight += load;
Yong Zhang3ff6dca2011-01-24 15:33:52 +08002912
Peter Zijlstra7c80cfc2017-05-06 16:03:17 +02002913 shares = (tg_shares * load);
Peter Zijlstracf5f0ac2011-10-13 16:52:28 +02002914 if (tg_weight)
2915 shares /= tg_weight;
Yong Zhang3ff6dca2011-01-24 15:33:52 +08002916
Dietmar Eggemannb8fd8422017-01-11 11:29:47 +00002917 /*
2918 * MIN_SHARES has to be unscaled here to support per-CPU partitioning
2919 * of a group with small tg->shares value. It is a floor value which is
2920 * assigned as a minimum load.weight to the sched_entity representing
2921 * the group on a CPU.
2922 *
2923 * E.g. on 64-bit for a group with tg->shares of scale_load(15)=15*1024
2924 * on an 8-core system with 8 tasks each runnable on one CPU shares has
2925 * to be 15*1024*1/8=1920 instead of scale_load(MIN_SHARES)=2*1024. In
2926 * case no task is runnable on a CPU MIN_SHARES=2 should be returned
2927 * instead of 0.
2928 */
Peter Zijlstra7c80cfc2017-05-06 16:03:17 +02002929 return clamp_t(long, shares, MIN_SHARES, tg_shares);
Yong Zhang3ff6dca2011-01-24 15:33:52 +08002930}
Josef Bacik2c8e4dc2017-08-03 11:13:39 -04002931
2932/*
Peter Zijlstra17de4ee2017-08-24 13:06:35 +02002933 * This calculates the effective runnable weight for a group entity based on
2934 * the group entity weight calculated above.
Josef Bacik2c8e4dc2017-08-03 11:13:39 -04002935 *
Peter Zijlstra17de4ee2017-08-24 13:06:35 +02002936 * Because of the above approximation (2), our group entity weight is
2937 * an load_avg based ratio (3). This means that it includes blocked load and
2938 * does not represent the runnable weight.
Josef Bacik2c8e4dc2017-08-03 11:13:39 -04002939 *
Peter Zijlstra17de4ee2017-08-24 13:06:35 +02002940 * Approximate the group entity's runnable weight per ratio from the group
2941 * runqueue:
2942 *
2943 * grq->avg.runnable_load_avg
2944 * ge->runnable_weight = ge->load.weight * -------------------------- (7)
2945 * grq->avg.load_avg
2946 *
2947 * However, analogous to above, since the avg numbers are slow, this leads to
2948 * transients in the from-idle case. Instead we use:
2949 *
2950 * ge->runnable_weight = ge->load.weight *
2951 *
2952 * max(grq->avg.runnable_load_avg, grq->runnable_weight)
2953 * ----------------------------------------------------- (8)
2954 * max(grq->avg.load_avg, grq->load.weight)
2955 *
2956 * Where these max() serve both to use the 'instant' values to fix the slow
2957 * from-idle and avoid the /0 on to-idle, similar to (6).
Josef Bacik2c8e4dc2017-08-03 11:13:39 -04002958 */
2959static long calc_group_runnable(struct cfs_rq *cfs_rq, long shares)
2960{
Peter Zijlstra17de4ee2017-08-24 13:06:35 +02002961 long runnable, load_avg;
2962
2963 load_avg = max(cfs_rq->avg.load_avg,
2964 scale_load_down(cfs_rq->load.weight));
2965
2966 runnable = max(cfs_rq->avg.runnable_load_avg,
2967 scale_load_down(cfs_rq->runnable_weight));
Josef Bacik2c8e4dc2017-08-03 11:13:39 -04002968
2969 runnable *= shares;
2970 if (load_avg)
2971 runnable /= load_avg;
Peter Zijlstra17de4ee2017-08-24 13:06:35 +02002972
Josef Bacik2c8e4dc2017-08-03 11:13:39 -04002973 return clamp_t(long, runnable, MIN_SHARES, shares);
2974}
Yong Zhang3ff6dca2011-01-24 15:33:52 +08002975# endif /* CONFIG_SMP */
Peter Zijlstraea1dc6f2016-06-24 16:11:02 +02002976
Paul Turner82958362012-10-04 13:18:31 +02002977static inline int throttled_hierarchy(struct cfs_rq *cfs_rq);
2978
Peter Zijlstra1ea6c462017-05-06 15:59:54 +02002979/*
2980 * Recomputes the group entity based on the current state of its group
2981 * runqueue.
2982 */
2983static void update_cfs_group(struct sched_entity *se)
Peter Zijlstra2069dd72010-11-15 15:47:00 -08002984{
Peter Zijlstra1ea6c462017-05-06 15:59:54 +02002985 struct cfs_rq *gcfs_rq = group_cfs_rq(se);
2986 long shares, runnable;
Peter Zijlstra2069dd72010-11-15 15:47:00 -08002987
Peter Zijlstra1ea6c462017-05-06 15:59:54 +02002988 if (!gcfs_rq)
Peter Zijlstra2069dd72010-11-15 15:47:00 -08002989 return;
Vincent Guittot89ee0482016-12-21 16:50:26 +01002990
Peter Zijlstra1ea6c462017-05-06 15:59:54 +02002991 if (throttled_hierarchy(gcfs_rq))
Vincent Guittot89ee0482016-12-21 16:50:26 +01002992 return;
2993
Yong Zhang3ff6dca2011-01-24 15:33:52 +08002994#ifndef CONFIG_SMP
Peter Zijlstra1ea6c462017-05-06 15:59:54 +02002995 runnable = shares = READ_ONCE(gcfs_rq->tg->shares);
Peter Zijlstra7c80cfc2017-05-06 16:03:17 +02002996
2997 if (likely(se->load.weight == shares))
Yong Zhang3ff6dca2011-01-24 15:33:52 +08002998 return;
Peter Zijlstra7c80cfc2017-05-06 16:03:17 +02002999#else
Josef Bacik2c8e4dc2017-08-03 11:13:39 -04003000 shares = calc_group_shares(gcfs_rq);
3001 runnable = calc_group_runnable(gcfs_rq, shares);
Yong Zhang3ff6dca2011-01-24 15:33:52 +08003002#endif
Peter Zijlstra2069dd72010-11-15 15:47:00 -08003003
Peter Zijlstra1ea6c462017-05-06 15:59:54 +02003004 reweight_entity(cfs_rq_of(se), se, shares, runnable);
Peter Zijlstra2069dd72010-11-15 15:47:00 -08003005}
Vincent Guittot89ee0482016-12-21 16:50:26 +01003006
Peter Zijlstra2069dd72010-11-15 15:47:00 -08003007#else /* CONFIG_FAIR_GROUP_SCHED */
Peter Zijlstra1ea6c462017-05-06 15:59:54 +02003008static inline void update_cfs_group(struct sched_entity *se)
Peter Zijlstra2069dd72010-11-15 15:47:00 -08003009{
3010}
3011#endif /* CONFIG_FAIR_GROUP_SCHED */
3012
Viresh Kumara030d732017-05-24 10:59:52 +05303013static inline void cfs_rq_util_change(struct cfs_rq *cfs_rq)
3014{
Linus Torvalds43964402017-09-05 12:19:08 -07003015 struct rq *rq = rq_of(cfs_rq);
3016
3017 if (&rq->cfs == cfs_rq) {
Viresh Kumara030d732017-05-24 10:59:52 +05303018 /*
3019 * There are a few boundary cases this might miss but it should
3020 * get called often enough that that should (hopefully) not be
3021 * a real problem -- added to that it only calls on the local
3022 * CPU, so if we enqueue remotely we'll miss an update, but
3023 * the next tick/schedule should update.
3024 *
3025 * It will not get called when we go idle, because the idle
3026 * thread is a different class (!fair), nor will the utilization
3027 * number include things like RT tasks.
3028 *
3029 * As is, the util number is not freq-invariant (we'd have to
3030 * implement arch_scale_freq_capacity() for that).
3031 *
3032 * See cpu_util().
3033 */
Linus Torvalds43964402017-09-05 12:19:08 -07003034 cpufreq_update_util(rq, 0);
Viresh Kumara030d732017-05-24 10:59:52 +05303035 }
3036}
3037
Alex Shi141965c2013-06-26 13:05:39 +08003038#ifdef CONFIG_SMP
Paul Turner5b51f2f2012-10-04 13:18:32 +02003039/*
Paul Turner9d85f212012-10-04 13:18:29 +02003040 * Approximate:
3041 * val * y^n, where y^32 ~= 0.5 (~1 scheduling period)
3042 */
Yuyang Dua481db32017-02-13 05:44:23 +08003043static u64 decay_load(u64 val, u64 n)
Paul Turner9d85f212012-10-04 13:18:29 +02003044{
Paul Turner5b51f2f2012-10-04 13:18:32 +02003045 unsigned int local_n;
3046
Peter Zijlstra05296e72017-03-31 10:51:41 +02003047 if (unlikely(n > LOAD_AVG_PERIOD * 63))
Paul Turner5b51f2f2012-10-04 13:18:32 +02003048 return 0;
3049
3050 /* after bounds checking we can collapse to 32-bit */
3051 local_n = n;
3052
3053 /*
3054 * As y^PERIOD = 1/2, we can combine
Zhihui Zhang9c58c792014-09-20 21:24:36 -04003055 * y^n = 1/2^(n/PERIOD) * y^(n%PERIOD)
3056 * With a look-up table which covers y^n (n<PERIOD)
Paul Turner5b51f2f2012-10-04 13:18:32 +02003057 *
3058 * To achieve constant time decay_load.
3059 */
3060 if (unlikely(local_n >= LOAD_AVG_PERIOD)) {
3061 val >>= local_n / LOAD_AVG_PERIOD;
3062 local_n %= LOAD_AVG_PERIOD;
Paul Turner9d85f212012-10-04 13:18:29 +02003063 }
3064
Yuyang Du9d89c252015-07-15 08:04:37 +08003065 val = mul_u64_u32_shr(val, runnable_avg_yN_inv[local_n], 32);
3066 return val;
Paul Turner5b51f2f2012-10-04 13:18:32 +02003067}
3068
Peter Zijlstra05296e72017-03-31 10:51:41 +02003069static u32 __accumulate_pelt_segments(u64 periods, u32 d1, u32 d3)
Paul Turner5b51f2f2012-10-04 13:18:32 +02003070{
Peter Zijlstra05296e72017-03-31 10:51:41 +02003071 u32 c1, c2, c3 = d3; /* y^0 == 1 */
Paul Turner5b51f2f2012-10-04 13:18:32 +02003072
Yuyang Dua481db32017-02-13 05:44:23 +08003073 /*
Peter Zijlstra3841cdc2017-04-10 12:47:33 +02003074 * c1 = d1 y^p
Yuyang Dua481db32017-02-13 05:44:23 +08003075 */
Peter Zijlstra05296e72017-03-31 10:51:41 +02003076 c1 = decay_load((u64)d1, periods);
Yuyang Dua481db32017-02-13 05:44:23 +08003077
Yuyang Dua481db32017-02-13 05:44:23 +08003078 /*
Peter Zijlstra3841cdc2017-04-10 12:47:33 +02003079 * p-1
Peter Zijlstra05296e72017-03-31 10:51:41 +02003080 * c2 = 1024 \Sum y^n
3081 * n=1
Yuyang Dua481db32017-02-13 05:44:23 +08003082 *
Peter Zijlstra05296e72017-03-31 10:51:41 +02003083 * inf inf
3084 * = 1024 ( \Sum y^n - \Sum y^n - y^0 )
Peter Zijlstra3841cdc2017-04-10 12:47:33 +02003085 * n=0 n=p
Yuyang Dua481db32017-02-13 05:44:23 +08003086 */
Peter Zijlstra05296e72017-03-31 10:51:41 +02003087 c2 = LOAD_AVG_MAX - decay_load(LOAD_AVG_MAX, periods) - 1024;
Yuyang Dua481db32017-02-13 05:44:23 +08003088
3089 return c1 + c2 + c3;
Paul Turner9d85f212012-10-04 13:18:29 +02003090}
3091
Peter Zijlstra54a21382015-09-07 15:05:42 +02003092#define cap_scale(v, s) ((v)*(s) >> SCHED_CAPACITY_SHIFT)
Dietmar Eggemanne0f5f3a2015-08-14 17:23:09 +01003093
Paul Turner9d85f212012-10-04 13:18:29 +02003094/*
Yuyang Dua481db32017-02-13 05:44:23 +08003095 * Accumulate the three separate parts of the sum; d1 the remainder
3096 * of the last (incomplete) period, d2 the span of full periods and d3
3097 * the remainder of the (incomplete) current period.
3098 *
3099 * d1 d2 d3
3100 * ^ ^ ^
3101 * | | |
3102 * |<->|<----------------->|<--->|
3103 * ... |---x---|------| ... |------|-----x (now)
3104 *
Peter Zijlstra3841cdc2017-04-10 12:47:33 +02003105 * p-1
3106 * u' = (u + d1) y^p + 1024 \Sum y^n + d3 y^0
3107 * n=1
Yuyang Dua481db32017-02-13 05:44:23 +08003108 *
Peter Zijlstra3841cdc2017-04-10 12:47:33 +02003109 * = u y^p + (Step 1)
Yuyang Dua481db32017-02-13 05:44:23 +08003110 *
Peter Zijlstra3841cdc2017-04-10 12:47:33 +02003111 * p-1
3112 * d1 y^p + 1024 \Sum y^n + d3 y^0 (Step 2)
3113 * n=1
Yuyang Dua481db32017-02-13 05:44:23 +08003114 */
3115static __always_inline u32
3116accumulate_sum(u64 delta, int cpu, struct sched_avg *sa,
Peter Zijlstra1ea6c462017-05-06 15:59:54 +02003117 unsigned long load, unsigned long runnable, int running)
Yuyang Dua481db32017-02-13 05:44:23 +08003118{
3119 unsigned long scale_freq, scale_cpu;
Peter Zijlstra05296e72017-03-31 10:51:41 +02003120 u32 contrib = (u32)delta; /* p == 0 -> delta < 1024 */
Yuyang Dua481db32017-02-13 05:44:23 +08003121 u64 periods;
Yuyang Dua481db32017-02-13 05:44:23 +08003122
3123 scale_freq = arch_scale_freq_capacity(NULL, cpu);
3124 scale_cpu = arch_scale_cpu_capacity(NULL, cpu);
3125
3126 delta += sa->period_contrib;
3127 periods = delta / 1024; /* A period is 1024us (~1ms) */
3128
3129 /*
3130 * Step 1: decay old *_sum if we crossed period boundaries.
3131 */
3132 if (periods) {
3133 sa->load_sum = decay_load(sa->load_sum, periods);
Peter Zijlstra1ea6c462017-05-06 15:59:54 +02003134 sa->runnable_load_sum =
3135 decay_load(sa->runnable_load_sum, periods);
Yuyang Dua481db32017-02-13 05:44:23 +08003136 sa->util_sum = decay_load((u64)(sa->util_sum), periods);
Yuyang Dua481db32017-02-13 05:44:23 +08003137
Peter Zijlstra05296e72017-03-31 10:51:41 +02003138 /*
3139 * Step 2
3140 */
3141 delta %= 1024;
3142 contrib = __accumulate_pelt_segments(periods,
3143 1024 - sa->period_contrib, delta);
3144 }
Yuyang Dua481db32017-02-13 05:44:23 +08003145 sa->period_contrib = delta;
3146
3147 contrib = cap_scale(contrib, scale_freq);
Peter Zijlstra1ea6c462017-05-06 15:59:54 +02003148 if (load)
3149 sa->load_sum += load * contrib;
3150 if (runnable)
3151 sa->runnable_load_sum += runnable * contrib;
Yuyang Dua481db32017-02-13 05:44:23 +08003152 if (running)
3153 sa->util_sum += contrib * scale_cpu;
3154
3155 return periods;
3156}
3157
3158/*
Paul Turner9d85f212012-10-04 13:18:29 +02003159 * We can represent the historical contribution to runnable average as the
3160 * coefficients of a geometric series. To do this we sub-divide our runnable
3161 * history into segments of approximately 1ms (1024us); label the segment that
3162 * occurred N-ms ago p_N, with p_0 corresponding to the current period, e.g.
3163 *
3164 * [<- 1024us ->|<- 1024us ->|<- 1024us ->| ...
3165 * p0 p1 p2
3166 * (now) (~1ms ago) (~2ms ago)
3167 *
3168 * Let u_i denote the fraction of p_i that the entity was runnable.
3169 *
3170 * We then designate the fractions u_i as our co-efficients, yielding the
3171 * following representation of historical load:
3172 * u_0 + u_1*y + u_2*y^2 + u_3*y^3 + ...
3173 *
3174 * We choose y based on the with of a reasonably scheduling period, fixing:
3175 * y^32 = 0.5
3176 *
3177 * This means that the contribution to load ~32ms ago (u_32) will be weighted
3178 * approximately half as much as the contribution to load within the last ms
3179 * (u_0).
3180 *
3181 * When a period "rolls over" and we have new u_0`, multiplying the previous
3182 * sum again by y is sufficient to update:
3183 * load_avg = u_0` + y*(u_0 + u_1*y + u_2*y^2 + ... )
3184 * = u_0 + u_1*y + u_2*y^2 + ... [re-labeling u_i --> u_{i+1}]
3185 */
Yuyang Du9d89c252015-07-15 08:04:37 +08003186static __always_inline int
Peter Zijlstrac7b50212017-05-06 16:42:08 +02003187___update_load_sum(u64 now, int cpu, struct sched_avg *sa,
Peter Zijlstra1ea6c462017-05-06 15:59:54 +02003188 unsigned long load, unsigned long runnable, int running)
Paul Turner9d85f212012-10-04 13:18:29 +02003189{
Yuyang Dua481db32017-02-13 05:44:23 +08003190 u64 delta;
Paul Turner9d85f212012-10-04 13:18:29 +02003191
Yuyang Du9d89c252015-07-15 08:04:37 +08003192 delta = now - sa->last_update_time;
Paul Turner9d85f212012-10-04 13:18:29 +02003193 /*
3194 * This should only happen when time goes backwards, which it
3195 * unfortunately does during sched clock init when we swap over to TSC.
3196 */
3197 if ((s64)delta < 0) {
Yuyang Du9d89c252015-07-15 08:04:37 +08003198 sa->last_update_time = now;
Paul Turner9d85f212012-10-04 13:18:29 +02003199 return 0;
3200 }
3201
3202 /*
3203 * Use 1024ns as the unit of measurement since it's a reasonable
3204 * approximation of 1us and fast to compute.
3205 */
3206 delta >>= 10;
3207 if (!delta)
3208 return 0;
Peter Zijlstrabb0bd042017-04-10 13:20:45 +02003209
3210 sa->last_update_time += delta << 10;
Paul Turner9d85f212012-10-04 13:18:29 +02003211
Yuyang Dua481db32017-02-13 05:44:23 +08003212 /*
Vincent Guittotf235a542017-07-01 07:06:13 +02003213 * running is a subset of runnable (weight) so running can't be set if
3214 * runnable is clear. But there are some corner cases where the current
3215 * se has been already dequeued but cfs_rq->curr still points to it.
3216 * This means that weight will be 0 but not running for a sched_entity
3217 * but also for a cfs_rq if the latter becomes idle. As an example,
3218 * this happens during idle_balance() which calls
3219 * update_blocked_averages()
3220 */
Peter Zijlstra1ea6c462017-05-06 15:59:54 +02003221 if (!load)
3222 runnable = running = 0;
Vincent Guittotf235a542017-07-01 07:06:13 +02003223
3224 /*
Yuyang Dua481db32017-02-13 05:44:23 +08003225 * Now we know we crossed measurement unit boundaries. The *_avg
3226 * accrues by two steps:
3227 *
3228 * Step 1: accumulate *_sum since last_update_time. If we haven't
3229 * crossed period boundaries, finish.
3230 */
Peter Zijlstra1ea6c462017-05-06 15:59:54 +02003231 if (!accumulate_sum(delta, cpu, sa, load, runnable, running))
Yuyang Dua481db32017-02-13 05:44:23 +08003232 return 0;
Dietmar Eggemann6f2b0452015-09-07 14:57:22 +01003233
Peter Zijlstrac7b50212017-05-06 16:42:08 +02003234 return 1;
3235}
3236
3237static __always_inline void
Peter Zijlstra1ea6c462017-05-06 15:59:54 +02003238___update_load_avg(struct sched_avg *sa, unsigned long load, unsigned long runnable)
Peter Zijlstrac7b50212017-05-06 16:42:08 +02003239{
3240 u32 divider = LOAD_AVG_MAX - 1024 + sa->period_contrib;
3241
Yuyang Dua481db32017-02-13 05:44:23 +08003242 /*
3243 * Step 2: update *_avg.
3244 */
Peter Zijlstra1ea6c462017-05-06 15:59:54 +02003245 sa->load_avg = div_u64(load * sa->load_sum, divider);
3246 sa->runnable_load_avg = div_u64(runnable * sa->runnable_load_sum, divider);
Peter Zijlstrac7b50212017-05-06 16:42:08 +02003247 sa->util_avg = sa->util_sum / divider;
Paul Turner9d85f212012-10-04 13:18:29 +02003248}
3249
Peter Zijlstrac7b50212017-05-06 16:42:08 +02003250/*
Peter Zijlstrac7b50212017-05-06 16:42:08 +02003251 * sched_entity:
3252 *
Peter Zijlstra1ea6c462017-05-06 15:59:54 +02003253 * task:
3254 * se_runnable() == se_weight()
3255 *
3256 * group: [ see update_cfs_group() ]
3257 * se_weight() = tg->weight * grq->load_avg / tg->load_avg
3258 * se_runnable() = se_weight(se) * grq->runnable_load_avg / grq->load_avg
3259 *
Peter Zijlstrac7b50212017-05-06 16:42:08 +02003260 * load_sum := runnable_sum
3261 * load_avg = se_weight(se) * runnable_avg
3262 *
Peter Zijlstra1ea6c462017-05-06 15:59:54 +02003263 * runnable_load_sum := runnable_sum
3264 * runnable_load_avg = se_runnable(se) * runnable_avg
3265 *
3266 * XXX collapse load_sum and runnable_load_sum
3267 *
Peter Zijlstrac7b50212017-05-06 16:42:08 +02003268 * cfq_rs:
3269 *
3270 * load_sum = \Sum se_weight(se) * se->avg.load_sum
3271 * load_avg = \Sum se->avg.load_avg
Peter Zijlstra1ea6c462017-05-06 15:59:54 +02003272 *
3273 * runnable_load_sum = \Sum se_runnable(se) * se->avg.runnable_load_sum
3274 * runnable_load_avg = \Sum se->avg.runable_load_avg
Peter Zijlstrac7b50212017-05-06 16:42:08 +02003275 */
3276
Peter Zijlstra0ccb9772017-03-28 11:08:20 +02003277static int
3278__update_load_avg_blocked_se(u64 now, int cpu, struct sched_entity *se)
3279{
Peter Zijlstra1ea6c462017-05-06 15:59:54 +02003280 if (entity_is_task(se))
3281 se->runnable_weight = se->load.weight;
3282
3283 if (___update_load_sum(now, cpu, &se->avg, 0, 0, 0)) {
3284 ___update_load_avg(&se->avg, se_weight(se), se_runnable(se));
Peter Zijlstrac7b50212017-05-06 16:42:08 +02003285 return 1;
3286 }
3287
3288 return 0;
Peter Zijlstra0ccb9772017-03-28 11:08:20 +02003289}
3290
3291static int
3292__update_load_avg_se(u64 now, int cpu, struct cfs_rq *cfs_rq, struct sched_entity *se)
3293{
Peter Zijlstra1ea6c462017-05-06 15:59:54 +02003294 if (entity_is_task(se))
3295 se->runnable_weight = se->load.weight;
Peter Zijlstrac7b50212017-05-06 16:42:08 +02003296
Peter Zijlstra1ea6c462017-05-06 15:59:54 +02003297 if (___update_load_sum(now, cpu, &se->avg, !!se->on_rq, !!se->on_rq,
3298 cfs_rq->curr == se)) {
3299
3300 ___update_load_avg(&se->avg, se_weight(se), se_runnable(se));
Peter Zijlstrac7b50212017-05-06 16:42:08 +02003301 return 1;
3302 }
3303
3304 return 0;
Peter Zijlstra0ccb9772017-03-28 11:08:20 +02003305}
3306
3307static int
3308__update_load_avg_cfs_rq(u64 now, int cpu, struct cfs_rq *cfs_rq)
3309{
Peter Zijlstrac7b50212017-05-06 16:42:08 +02003310 if (___update_load_sum(now, cpu, &cfs_rq->avg,
3311 scale_load_down(cfs_rq->load.weight),
Peter Zijlstra1ea6c462017-05-06 15:59:54 +02003312 scale_load_down(cfs_rq->runnable_weight),
3313 cfs_rq->curr != NULL)) {
3314
3315 ___update_load_avg(&cfs_rq->avg, 1, 1);
Peter Zijlstrac7b50212017-05-06 16:42:08 +02003316 return 1;
3317 }
3318
3319 return 0;
Peter Zijlstra0ccb9772017-03-28 11:08:20 +02003320}
3321
Paul Turnerc566e8e2012-10-04 13:18:30 +02003322#ifdef CONFIG_FAIR_GROUP_SCHED
Peter Zijlstra7c3edd22016-07-13 10:56:25 +02003323/**
3324 * update_tg_load_avg - update the tg's load avg
3325 * @cfs_rq: the cfs_rq whose avg changed
3326 * @force: update regardless of how small the difference
3327 *
3328 * This function 'ensures': tg->load_avg := \Sum tg->cfs_rq[]->avg.load.
3329 * However, because tg->load_avg is a global value there are performance
3330 * considerations.
3331 *
3332 * In order to avoid having to look at the other cfs_rq's, we use a
3333 * differential update where we store the last value we propagated. This in
3334 * turn allows skipping updates if the differential is 'small'.
3335 *
Rik van Riel815abf52017-06-23 12:55:30 -04003336 * Updating tg's load_avg is necessary before update_cfs_share().
Paul Turnerbb17f652012-10-04 13:18:31 +02003337 */
Yuyang Du9d89c252015-07-15 08:04:37 +08003338static inline void update_tg_load_avg(struct cfs_rq *cfs_rq, int force)
Paul Turnerbb17f652012-10-04 13:18:31 +02003339{
Yuyang Du9d89c252015-07-15 08:04:37 +08003340 long delta = cfs_rq->avg.load_avg - cfs_rq->tg_load_avg_contrib;
Paul Turnerbb17f652012-10-04 13:18:31 +02003341
Waiman Longaa0b7ae2015-12-02 13:41:50 -05003342 /*
3343 * No need to update load_avg for root_task_group as it is not used.
3344 */
3345 if (cfs_rq->tg == &root_task_group)
3346 return;
3347
Yuyang Du9d89c252015-07-15 08:04:37 +08003348 if (force || abs(delta) > cfs_rq->tg_load_avg_contrib / 64) {
3349 atomic_long_add(delta, &cfs_rq->tg->load_avg);
3350 cfs_rq->tg_load_avg_contrib = cfs_rq->avg.load_avg;
Paul Turnerbb17f652012-10-04 13:18:31 +02003351 }
Paul Turner8165e142012-10-04 13:18:31 +02003352}
Dietmar Eggemannf5f97392014-02-26 11:19:33 +00003353
Byungchul Parkad936d82015-10-24 01:16:19 +09003354/*
3355 * Called within set_task_rq() right before setting a task's cpu. The
3356 * caller only guarantees p->pi_lock is held; no other assumptions,
3357 * including the state of rq->lock, should be made.
3358 */
3359void set_task_rq_fair(struct sched_entity *se,
3360 struct cfs_rq *prev, struct cfs_rq *next)
3361{
Peter Zijlstra0ccb9772017-03-28 11:08:20 +02003362 u64 p_last_update_time;
3363 u64 n_last_update_time;
3364
Byungchul Parkad936d82015-10-24 01:16:19 +09003365 if (!sched_feat(ATTACH_AGE_LOAD))
3366 return;
3367
3368 /*
3369 * We are supposed to update the task to "current" time, then its up to
3370 * date and ready to go to new CPU/cfs_rq. But we have difficulty in
3371 * getting what current time is, so simply throw away the out-of-date
3372 * time. This will result in the wakee task is less decayed, but giving
3373 * the wakee more load sounds not bad.
3374 */
Peter Zijlstra0ccb9772017-03-28 11:08:20 +02003375 if (!(se->avg.last_update_time && prev))
3376 return;
Byungchul Parkad936d82015-10-24 01:16:19 +09003377
3378#ifndef CONFIG_64BIT
Peter Zijlstra0ccb9772017-03-28 11:08:20 +02003379 {
Byungchul Parkad936d82015-10-24 01:16:19 +09003380 u64 p_last_update_time_copy;
3381 u64 n_last_update_time_copy;
3382
3383 do {
3384 p_last_update_time_copy = prev->load_last_update_time_copy;
3385 n_last_update_time_copy = next->load_last_update_time_copy;
3386
3387 smp_rmb();
3388
3389 p_last_update_time = prev->avg.last_update_time;
3390 n_last_update_time = next->avg.last_update_time;
3391
3392 } while (p_last_update_time != p_last_update_time_copy ||
3393 n_last_update_time != n_last_update_time_copy);
Byungchul Parkad936d82015-10-24 01:16:19 +09003394 }
Peter Zijlstra0ccb9772017-03-28 11:08:20 +02003395#else
3396 p_last_update_time = prev->avg.last_update_time;
3397 n_last_update_time = next->avg.last_update_time;
3398#endif
3399 __update_load_avg_blocked_se(p_last_update_time, cpu_of(rq_of(prev)), se);
3400 se->avg.last_update_time = n_last_update_time;
Byungchul Parkad936d82015-10-24 01:16:19 +09003401}
Vincent Guittot09a43ac2016-11-08 10:53:45 +01003402
Peter Zijlstra0e2d2aa2017-05-08 17:30:46 +02003403
3404/*
3405 * When on migration a sched_entity joins/leaves the PELT hierarchy, we need to
3406 * propagate its contribution. The key to this propagation is the invariant
3407 * that for each group:
3408 *
3409 * ge->avg == grq->avg (1)
3410 *
3411 * _IFF_ we look at the pure running and runnable sums. Because they
3412 * represent the very same entity, just at different points in the hierarchy.
3413 *
3414 *
3415 * Per the above update_tg_cfs_util() is trivial (and still 'wrong') and
3416 * simply copies the running sum over.
3417 *
3418 * However, update_tg_cfs_runnable() is more complex. So we have:
3419 *
3420 * ge->avg.load_avg = ge->load.weight * ge->avg.runnable_avg (2)
3421 *
3422 * And since, like util, the runnable part should be directly transferable,
3423 * the following would _appear_ to be the straight forward approach:
3424 *
3425 * grq->avg.load_avg = grq->load.weight * grq->avg.running_avg (3)
3426 *
3427 * And per (1) we have:
3428 *
3429 * ge->avg.running_avg == grq->avg.running_avg
3430 *
3431 * Which gives:
3432 *
3433 * ge->load.weight * grq->avg.load_avg
3434 * ge->avg.load_avg = ----------------------------------- (4)
3435 * grq->load.weight
3436 *
3437 * Except that is wrong!
3438 *
3439 * Because while for entities historical weight is not important and we
3440 * really only care about our future and therefore can consider a pure
3441 * runnable sum, runqueues can NOT do this.
3442 *
3443 * We specifically want runqueues to have a load_avg that includes
3444 * historical weights. Those represent the blocked load, the load we expect
3445 * to (shortly) return to us. This only works by keeping the weights as
3446 * integral part of the sum. We therefore cannot decompose as per (3).
3447 *
3448 * OK, so what then?
3449 *
3450 *
3451 * Another way to look at things is:
3452 *
3453 * grq->avg.load_avg = \Sum se->avg.load_avg
3454 *
3455 * Therefore, per (2):
3456 *
3457 * grq->avg.load_avg = \Sum se->load.weight * se->avg.runnable_avg
3458 *
3459 * And the very thing we're propagating is a change in that sum (someone
3460 * joined/left). So we can easily know the runnable change, which would be, per
3461 * (2) the already tracked se->load_avg divided by the corresponding
3462 * se->weight.
3463 *
3464 * Basically (4) but in differential form:
3465 *
3466 * d(runnable_avg) += se->avg.load_avg / se->load.weight
3467 * (5)
3468 * ge->avg.load_avg += ge->load.weight * d(runnable_avg)
3469 */
3470
Vincent Guittot09a43ac2016-11-08 10:53:45 +01003471static inline void
Peter Zijlstra0e2d2aa2017-05-08 17:30:46 +02003472update_tg_cfs_util(struct cfs_rq *cfs_rq, struct sched_entity *se, struct cfs_rq *gcfs_rq)
Vincent Guittot09a43ac2016-11-08 10:53:45 +01003473{
Vincent Guittot09a43ac2016-11-08 10:53:45 +01003474 long delta = gcfs_rq->avg.util_avg - se->avg.util_avg;
3475
3476 /* Nothing to update */
3477 if (!delta)
3478 return;
3479
3480 /* Set new sched_entity's utilization */
3481 se->avg.util_avg = gcfs_rq->avg.util_avg;
3482 se->avg.util_sum = se->avg.util_avg * LOAD_AVG_MAX;
3483
3484 /* Update parent cfs_rq utilization */
3485 add_positive(&cfs_rq->avg.util_avg, delta);
3486 cfs_rq->avg.util_sum = cfs_rq->avg.util_avg * LOAD_AVG_MAX;
3487}
3488
Vincent Guittot09a43ac2016-11-08 10:53:45 +01003489static inline void
Peter Zijlstra0e2d2aa2017-05-08 17:30:46 +02003490update_tg_cfs_runnable(struct cfs_rq *cfs_rq, struct sched_entity *se, struct cfs_rq *gcfs_rq)
Vincent Guittot09a43ac2016-11-08 10:53:45 +01003491{
Peter Zijlstra0e2d2aa2017-05-08 17:30:46 +02003492 long runnable_sum = gcfs_rq->prop_runnable_sum;
Peter Zijlstra1ea6c462017-05-06 15:59:54 +02003493 long runnable_load_avg, load_avg;
3494 s64 runnable_load_sum, load_sum;
Vincent Guittot09a43ac2016-11-08 10:53:45 +01003495
Peter Zijlstra0e2d2aa2017-05-08 17:30:46 +02003496 if (!runnable_sum)
Vincent Guittot09a43ac2016-11-08 10:53:45 +01003497 return;
3498
Peter Zijlstra0e2d2aa2017-05-08 17:30:46 +02003499 gcfs_rq->prop_runnable_sum = 0;
Vincent Guittot09a43ac2016-11-08 10:53:45 +01003500
Peter Zijlstra0e2d2aa2017-05-08 17:30:46 +02003501 load_sum = (s64)se_weight(se) * runnable_sum;
3502 load_avg = div_s64(load_sum, LOAD_AVG_MAX);
Vincent Guittot09a43ac2016-11-08 10:53:45 +01003503
Peter Zijlstra0e2d2aa2017-05-08 17:30:46 +02003504 add_positive(&se->avg.load_sum, runnable_sum);
3505 add_positive(&se->avg.load_avg, load_avg);
3506
3507 add_positive(&cfs_rq->avg.load_avg, load_avg);
3508 add_positive(&cfs_rq->avg.load_sum, load_sum);
3509
Peter Zijlstra1ea6c462017-05-06 15:59:54 +02003510 runnable_load_sum = (s64)se_runnable(se) * runnable_sum;
3511 runnable_load_avg = div_s64(runnable_load_sum, LOAD_AVG_MAX);
3512
3513 add_positive(&se->avg.runnable_load_sum, runnable_sum);
3514 add_positive(&se->avg.runnable_load_avg, runnable_load_avg);
3515
Vincent Guittot09a43ac2016-11-08 10:53:45 +01003516 if (se->on_rq) {
Peter Zijlstra1ea6c462017-05-06 15:59:54 +02003517 add_positive(&cfs_rq->avg.runnable_load_avg, runnable_load_avg);
3518 add_positive(&cfs_rq->avg.runnable_load_sum, runnable_load_sum);
Vincent Guittot09a43ac2016-11-08 10:53:45 +01003519 }
3520}
3521
Peter Zijlstra0e2d2aa2017-05-08 17:30:46 +02003522static inline void add_tg_cfs_propagate(struct cfs_rq *cfs_rq, long runnable_sum)
Vincent Guittot09a43ac2016-11-08 10:53:45 +01003523{
Peter Zijlstra0e2d2aa2017-05-08 17:30:46 +02003524 cfs_rq->propagate = 1;
3525 cfs_rq->prop_runnable_sum += runnable_sum;
Vincent Guittot09a43ac2016-11-08 10:53:45 +01003526}
3527
3528/* Update task and its cfs_rq load average */
3529static inline int propagate_entity_load_avg(struct sched_entity *se)
3530{
Peter Zijlstra0e2d2aa2017-05-08 17:30:46 +02003531 struct cfs_rq *cfs_rq, *gcfs_rq;
Vincent Guittot09a43ac2016-11-08 10:53:45 +01003532
3533 if (entity_is_task(se))
3534 return 0;
3535
Peter Zijlstra0e2d2aa2017-05-08 17:30:46 +02003536 gcfs_rq = group_cfs_rq(se);
3537 if (!gcfs_rq->propagate)
Vincent Guittot09a43ac2016-11-08 10:53:45 +01003538 return 0;
3539
Peter Zijlstra0e2d2aa2017-05-08 17:30:46 +02003540 gcfs_rq->propagate = 0;
3541
Vincent Guittot09a43ac2016-11-08 10:53:45 +01003542 cfs_rq = cfs_rq_of(se);
3543
Peter Zijlstra0e2d2aa2017-05-08 17:30:46 +02003544 add_tg_cfs_propagate(cfs_rq, gcfs_rq->prop_runnable_sum);
Vincent Guittot09a43ac2016-11-08 10:53:45 +01003545
Peter Zijlstra0e2d2aa2017-05-08 17:30:46 +02003546 update_tg_cfs_util(cfs_rq, se, gcfs_rq);
3547 update_tg_cfs_runnable(cfs_rq, se, gcfs_rq);
Vincent Guittot09a43ac2016-11-08 10:53:45 +01003548
3549 return 1;
3550}
3551
Vincent Guittotbc427892017-03-17 14:47:22 +01003552/*
3553 * Check if we need to update the load and the utilization of a blocked
3554 * group_entity:
3555 */
3556static inline bool skip_blocked_update(struct sched_entity *se)
3557{
3558 struct cfs_rq *gcfs_rq = group_cfs_rq(se);
3559
3560 /*
3561 * If sched_entity still have not zero load or utilization, we have to
3562 * decay it:
3563 */
3564 if (se->avg.load_avg || se->avg.util_avg)
3565 return false;
3566
3567 /*
3568 * If there is a pending propagation, we have to update the load and
3569 * the utilization of the sched_entity:
3570 */
Peter Zijlstra0e2d2aa2017-05-08 17:30:46 +02003571 if (gcfs_rq->propagate)
Vincent Guittotbc427892017-03-17 14:47:22 +01003572 return false;
3573
3574 /*
3575 * Otherwise, the load and the utilization of the sched_entity is
3576 * already zero and there is no pending propagation, so it will be a
3577 * waste of time to try to decay it:
3578 */
3579 return true;
3580}
3581
Peter Zijlstra6e831252014-02-11 16:11:48 +01003582#else /* CONFIG_FAIR_GROUP_SCHED */
Vincent Guittot09a43ac2016-11-08 10:53:45 +01003583
Yuyang Du9d89c252015-07-15 08:04:37 +08003584static inline void update_tg_load_avg(struct cfs_rq *cfs_rq, int force) {}
Vincent Guittot09a43ac2016-11-08 10:53:45 +01003585
3586static inline int propagate_entity_load_avg(struct sched_entity *se)
3587{
3588 return 0;
3589}
3590
Peter Zijlstra0e2d2aa2017-05-08 17:30:46 +02003591static inline void add_tg_cfs_propagate(struct cfs_rq *cfs_rq, long runnable_sum) {}
Vincent Guittot09a43ac2016-11-08 10:53:45 +01003592
Peter Zijlstra6e831252014-02-11 16:11:48 +01003593#endif /* CONFIG_FAIR_GROUP_SCHED */
Paul Turnerc566e8e2012-10-04 13:18:30 +02003594
Peter Zijlstra3d30544f2016-06-21 14:27:50 +02003595/**
3596 * update_cfs_rq_load_avg - update the cfs_rq's load/util averages
3597 * @now: current time, as per cfs_rq_clock_task()
3598 * @cfs_rq: cfs_rq to update
Peter Zijlstra3d30544f2016-06-21 14:27:50 +02003599 *
3600 * The cfs_rq avg is the direct sum of all its entities (blocked and runnable)
3601 * avg. The immediate corollary is that all (fair) tasks must be attached, see
3602 * post_init_entity_util_avg().
3603 *
3604 * cfs_rq->avg is used for task_h_load() and update_cfs_share() for example.
3605 *
Peter Zijlstra7c3edd22016-07-13 10:56:25 +02003606 * Returns true if the load decayed or we removed load.
3607 *
3608 * Since both these conditions indicate a changed cfs_rq->avg.load we should
3609 * call update_tg_load_avg() when this function returns true.
Peter Zijlstra3d30544f2016-06-21 14:27:50 +02003610 */
Steve Mucklea2c6c912016-03-24 15:26:07 -07003611static inline int
Viresh Kumar3a123bb2017-05-24 10:59:56 +05303612update_cfs_rq_load_avg(u64 now, struct cfs_rq *cfs_rq)
Steve Mucklea2c6c912016-03-24 15:26:07 -07003613{
Peter Zijlstra0e2d2aa2017-05-08 17:30:46 +02003614 unsigned long removed_load = 0, removed_util = 0, removed_runnable_sum = 0;
Steve Mucklea2c6c912016-03-24 15:26:07 -07003615 struct sched_avg *sa = &cfs_rq->avg;
Peter Zijlstra2a2f5d4e2017-05-08 16:51:41 +02003616 int decayed = 0;
Steve Mucklea2c6c912016-03-24 15:26:07 -07003617
Peter Zijlstra2a2f5d4e2017-05-08 16:51:41 +02003618 if (cfs_rq->removed.nr) {
3619 unsigned long r;
Peter Zijlstra9a2dd582017-05-12 14:18:10 +02003620 u32 divider = LOAD_AVG_MAX - 1024 + sa->period_contrib;
Peter Zijlstra2a2f5d4e2017-05-08 16:51:41 +02003621
3622 raw_spin_lock(&cfs_rq->removed.lock);
3623 swap(cfs_rq->removed.util_avg, removed_util);
3624 swap(cfs_rq->removed.load_avg, removed_load);
Peter Zijlstra0e2d2aa2017-05-08 17:30:46 +02003625 swap(cfs_rq->removed.runnable_sum, removed_runnable_sum);
Peter Zijlstra2a2f5d4e2017-05-08 16:51:41 +02003626 cfs_rq->removed.nr = 0;
3627 raw_spin_unlock(&cfs_rq->removed.lock);
3628
Peter Zijlstra2a2f5d4e2017-05-08 16:51:41 +02003629 r = removed_load;
Peter Zijlstra89741892016-06-16 10:50:40 +02003630 sub_positive(&sa->load_avg, r);
Peter Zijlstra9a2dd582017-05-12 14:18:10 +02003631 sub_positive(&sa->load_sum, r * divider);
Steve Mucklea2c6c912016-03-24 15:26:07 -07003632
Peter Zijlstra2a2f5d4e2017-05-08 16:51:41 +02003633 r = removed_util;
Peter Zijlstra89741892016-06-16 10:50:40 +02003634 sub_positive(&sa->util_avg, r);
Peter Zijlstra9a2dd582017-05-12 14:18:10 +02003635 sub_positive(&sa->util_sum, r * divider);
Peter Zijlstra2a2f5d4e2017-05-08 16:51:41 +02003636
Peter Zijlstra0e2d2aa2017-05-08 17:30:46 +02003637 add_tg_cfs_propagate(cfs_rq, -(long)removed_runnable_sum);
Peter Zijlstra2a2f5d4e2017-05-08 16:51:41 +02003638
3639 decayed = 1;
Steve Mucklea2c6c912016-03-24 15:26:07 -07003640 }
3641
Peter Zijlstra2a2f5d4e2017-05-08 16:51:41 +02003642 decayed |= __update_load_avg_cfs_rq(now, cpu_of(rq_of(cfs_rq)), cfs_rq);
Steve Mucklea2c6c912016-03-24 15:26:07 -07003643
3644#ifndef CONFIG_64BIT
3645 smp_wmb();
3646 cfs_rq->load_last_update_time_copy = sa->last_update_time;
3647#endif
3648
Peter Zijlstra2a2f5d4e2017-05-08 16:51:41 +02003649 if (decayed)
Steve Mucklea2c6c912016-03-24 15:26:07 -07003650 cfs_rq_util_change(cfs_rq);
Steve Muckle21e96f82016-03-21 17:21:07 -07003651
Peter Zijlstra2a2f5d4e2017-05-08 16:51:41 +02003652 return decayed;
Yuyang Du9d89c252015-07-15 08:04:37 +08003653}
3654
Peter Zijlstra3d30544f2016-06-21 14:27:50 +02003655/**
3656 * attach_entity_load_avg - attach this entity to its cfs_rq load avg
3657 * @cfs_rq: cfs_rq to attach to
3658 * @se: sched_entity to attach
3659 *
3660 * Must call update_cfs_rq_load_avg() before this, since we rely on
3661 * cfs_rq->avg.last_update_time being current.
3662 */
Byungchul Parka05e8c52015-08-20 20:21:56 +09003663static void attach_entity_load_avg(struct cfs_rq *cfs_rq, struct sched_entity *se)
3664{
Peter Zijlstraf2079342017-05-12 14:16:30 +02003665 u32 divider = LOAD_AVG_MAX - 1024 + cfs_rq->avg.period_contrib;
3666
3667 /*
3668 * When we attach the @se to the @cfs_rq, we must align the decay
3669 * window because without that, really weird and wonderful things can
3670 * happen.
3671 *
3672 * XXX illustrate
3673 */
Byungchul Parka05e8c52015-08-20 20:21:56 +09003674 se->avg.last_update_time = cfs_rq->avg.last_update_time;
Peter Zijlstraf2079342017-05-12 14:16:30 +02003675 se->avg.period_contrib = cfs_rq->avg.period_contrib;
3676
3677 /*
3678 * Hell(o) Nasty stuff.. we need to recompute _sum based on the new
3679 * period_contrib. This isn't strictly correct, but since we're
3680 * entirely outside of the PELT hierarchy, nobody cares if we truncate
3681 * _sum a little.
3682 */
3683 se->avg.util_sum = se->avg.util_avg * divider;
3684
3685 se->avg.load_sum = divider;
3686 if (se_weight(se)) {
3687 se->avg.load_sum =
3688 div_u64(se->avg.load_avg * se->avg.load_sum, se_weight(se));
3689 }
3690
3691 se->avg.runnable_load_sum = se->avg.load_sum;
3692
Peter Zijlstra8d5b9022017-08-24 17:45:35 +02003693 enqueue_load_avg(cfs_rq, se);
Byungchul Parka05e8c52015-08-20 20:21:56 +09003694 cfs_rq->avg.util_avg += se->avg.util_avg;
3695 cfs_rq->avg.util_sum += se->avg.util_sum;
Peter Zijlstra0e2d2aa2017-05-08 17:30:46 +02003696
3697 add_tg_cfs_propagate(cfs_rq, se->avg.load_sum);
Steve Mucklea2c6c912016-03-24 15:26:07 -07003698
3699 cfs_rq_util_change(cfs_rq);
Byungchul Parka05e8c52015-08-20 20:21:56 +09003700}
3701
Peter Zijlstra3d30544f2016-06-21 14:27:50 +02003702/**
3703 * detach_entity_load_avg - detach this entity from its cfs_rq load avg
3704 * @cfs_rq: cfs_rq to detach from
3705 * @se: sched_entity to detach
3706 *
3707 * Must call update_cfs_rq_load_avg() before this, since we rely on
3708 * cfs_rq->avg.last_update_time being current.
3709 */
Byungchul Parka05e8c52015-08-20 20:21:56 +09003710static void detach_entity_load_avg(struct cfs_rq *cfs_rq, struct sched_entity *se)
3711{
Peter Zijlstra8d5b9022017-08-24 17:45:35 +02003712 dequeue_load_avg(cfs_rq, se);
Peter Zijlstra89741892016-06-16 10:50:40 +02003713 sub_positive(&cfs_rq->avg.util_avg, se->avg.util_avg);
3714 sub_positive(&cfs_rq->avg.util_sum, se->avg.util_sum);
Peter Zijlstra0e2d2aa2017-05-08 17:30:46 +02003715
3716 add_tg_cfs_propagate(cfs_rq, -se->avg.load_sum);
Steve Mucklea2c6c912016-03-24 15:26:07 -07003717
3718 cfs_rq_util_change(cfs_rq);
Byungchul Parka05e8c52015-08-20 20:21:56 +09003719}
3720
Peter Zijlstrab382a532017-05-06 17:37:03 +02003721/*
3722 * Optional action to be done while updating the load average
3723 */
3724#define UPDATE_TG 0x1
3725#define SKIP_AGE_LOAD 0x2
3726#define DO_ATTACH 0x4
3727
3728/* Update task and its cfs_rq load average */
3729static inline void update_load_avg(struct cfs_rq *cfs_rq, struct sched_entity *se, int flags)
3730{
3731 u64 now = cfs_rq_clock_task(cfs_rq);
3732 struct rq *rq = rq_of(cfs_rq);
3733 int cpu = cpu_of(rq);
3734 int decayed;
3735
3736 /*
3737 * Track task load average for carrying it to new CPU after migrated, and
3738 * track group sched_entity load average for task_h_load calc in migration
3739 */
3740 if (se->avg.last_update_time && !(flags & SKIP_AGE_LOAD))
3741 __update_load_avg_se(now, cpu, cfs_rq, se);
3742
3743 decayed = update_cfs_rq_load_avg(now, cfs_rq);
3744 decayed |= propagate_entity_load_avg(se);
3745
3746 if (!se->avg.last_update_time && (flags & DO_ATTACH)) {
3747
3748 attach_entity_load_avg(cfs_rq, se);
3749 update_tg_load_avg(cfs_rq, 0);
3750
3751 } else if (decayed && (flags & UPDATE_TG))
3752 update_tg_load_avg(cfs_rq, 0);
3753}
3754
Yuyang Du0905f042015-12-17 07:34:27 +08003755#ifndef CONFIG_64BIT
3756static inline u64 cfs_rq_last_update_time(struct cfs_rq *cfs_rq)
3757{
3758 u64 last_update_time_copy;
3759 u64 last_update_time;
3760
3761 do {
3762 last_update_time_copy = cfs_rq->load_last_update_time_copy;
3763 smp_rmb();
3764 last_update_time = cfs_rq->avg.last_update_time;
3765 } while (last_update_time != last_update_time_copy);
3766
3767 return last_update_time;
3768}
3769#else
3770static inline u64 cfs_rq_last_update_time(struct cfs_rq *cfs_rq)
3771{
3772 return cfs_rq->avg.last_update_time;
3773}
3774#endif
3775
Paul Turner9ee474f2012-10-04 13:18:30 +02003776/*
Morten Rasmussen104cb162016-10-14 14:41:07 +01003777 * Synchronize entity load avg of dequeued entity without locking
3778 * the previous rq.
3779 */
3780void sync_entity_load_avg(struct sched_entity *se)
3781{
3782 struct cfs_rq *cfs_rq = cfs_rq_of(se);
3783 u64 last_update_time;
3784
3785 last_update_time = cfs_rq_last_update_time(cfs_rq);
Peter Zijlstra0ccb9772017-03-28 11:08:20 +02003786 __update_load_avg_blocked_se(last_update_time, cpu_of(rq_of(cfs_rq)), se);
Morten Rasmussen104cb162016-10-14 14:41:07 +01003787}
3788
3789/*
Yuyang Du9d89c252015-07-15 08:04:37 +08003790 * Task first catches up with cfs_rq, and then subtract
3791 * itself from the cfs_rq (task must be off the queue now).
Paul Turner9ee474f2012-10-04 13:18:30 +02003792 */
Yuyang Du9d89c252015-07-15 08:04:37 +08003793void remove_entity_load_avg(struct sched_entity *se)
Paul Turner9ee474f2012-10-04 13:18:30 +02003794{
Yuyang Du9d89c252015-07-15 08:04:37 +08003795 struct cfs_rq *cfs_rq = cfs_rq_of(se);
Peter Zijlstra2a2f5d4e2017-05-08 16:51:41 +02003796 unsigned long flags;
Paul Turner9ee474f2012-10-04 13:18:30 +02003797
Yuyang Du0905f042015-12-17 07:34:27 +08003798 /*
Peter Zijlstra7dc603c2016-06-16 13:29:28 +02003799 * tasks cannot exit without having gone through wake_up_new_task() ->
3800 * post_init_entity_util_avg() which will have added things to the
3801 * cfs_rq, so we can remove unconditionally.
3802 *
3803 * Similarly for groups, they will have passed through
3804 * post_init_entity_util_avg() before unregister_sched_fair_group()
3805 * calls this.
Yuyang Du0905f042015-12-17 07:34:27 +08003806 */
Paul Turner9ee474f2012-10-04 13:18:30 +02003807
Morten Rasmussen104cb162016-10-14 14:41:07 +01003808 sync_entity_load_avg(se);
Peter Zijlstra2a2f5d4e2017-05-08 16:51:41 +02003809
3810 raw_spin_lock_irqsave(&cfs_rq->removed.lock, flags);
3811 ++cfs_rq->removed.nr;
3812 cfs_rq->removed.util_avg += se->avg.util_avg;
3813 cfs_rq->removed.load_avg += se->avg.load_avg;
Peter Zijlstra0e2d2aa2017-05-08 17:30:46 +02003814 cfs_rq->removed.runnable_sum += se->avg.load_sum; /* == runnable_sum */
Peter Zijlstra2a2f5d4e2017-05-08 16:51:41 +02003815 raw_spin_unlock_irqrestore(&cfs_rq->removed.lock, flags);
Paul Turner2dac7542012-10-04 13:18:30 +02003816}
Vincent Guittot642dbc32013-04-18 18:34:26 +02003817
Yuyang Du7ea241a2015-07-15 08:04:42 +08003818static inline unsigned long cfs_rq_runnable_load_avg(struct cfs_rq *cfs_rq)
3819{
Peter Zijlstra1ea6c462017-05-06 15:59:54 +02003820 return cfs_rq->avg.runnable_load_avg;
Yuyang Du7ea241a2015-07-15 08:04:42 +08003821}
3822
3823static inline unsigned long cfs_rq_load_avg(struct cfs_rq *cfs_rq)
3824{
3825 return cfs_rq->avg.load_avg;
3826}
3827
Matt Fleming46f69fa2016-09-21 14:38:12 +01003828static int idle_balance(struct rq *this_rq, struct rq_flags *rf);
Peter Zijlstra6e831252014-02-11 16:11:48 +01003829
Peter Zijlstra38033c32014-01-23 20:32:21 +01003830#else /* CONFIG_SMP */
3831
Peter Zijlstra01011472016-06-17 11:20:46 +02003832static inline int
Viresh Kumar3a123bb2017-05-24 10:59:56 +05303833update_cfs_rq_load_avg(u64 now, struct cfs_rq *cfs_rq)
Peter Zijlstra01011472016-06-17 11:20:46 +02003834{
3835 return 0;
3836}
3837
Vincent Guittotd31b1a62016-11-08 10:53:44 +01003838#define UPDATE_TG 0x0
3839#define SKIP_AGE_LOAD 0x0
Peter Zijlstrab382a532017-05-06 17:37:03 +02003840#define DO_ATTACH 0x0
Vincent Guittotd31b1a62016-11-08 10:53:44 +01003841
Peter Zijlstra88c06162017-05-06 17:32:43 +02003842static inline void update_load_avg(struct cfs_rq *cfs_rq, struct sched_entity *se, int not_used1)
Rafael J. Wysocki536bd002016-05-06 14:58:43 +02003843{
Peter Zijlstra88c06162017-05-06 17:32:43 +02003844 cfs_rq_util_change(cfs_rq);
Rafael J. Wysocki536bd002016-05-06 14:58:43 +02003845}
3846
Yuyang Du9d89c252015-07-15 08:04:37 +08003847static inline void remove_entity_load_avg(struct sched_entity *se) {}
Peter Zijlstra6e831252014-02-11 16:11:48 +01003848
Byungchul Parka05e8c52015-08-20 20:21:56 +09003849static inline void
3850attach_entity_load_avg(struct cfs_rq *cfs_rq, struct sched_entity *se) {}
3851static inline void
3852detach_entity_load_avg(struct cfs_rq *cfs_rq, struct sched_entity *se) {}
3853
Matt Fleming46f69fa2016-09-21 14:38:12 +01003854static inline int idle_balance(struct rq *rq, struct rq_flags *rf)
Peter Zijlstra6e831252014-02-11 16:11:48 +01003855{
3856 return 0;
3857}
3858
Peter Zijlstra38033c32014-01-23 20:32:21 +01003859#endif /* CONFIG_SMP */
Paul Turner9d85f212012-10-04 13:18:29 +02003860
Peter Zijlstraddc97292007-10-15 17:00:10 +02003861static void check_spread(struct cfs_rq *cfs_rq, struct sched_entity *se)
3862{
3863#ifdef CONFIG_SCHED_DEBUG
3864 s64 d = se->vruntime - cfs_rq->min_vruntime;
3865
3866 if (d < 0)
3867 d = -d;
3868
3869 if (d > 3*sysctl_sched_latency)
Josh Poimboeufae928822016-06-17 12:43:24 -05003870 schedstat_inc(cfs_rq->nr_spread_over);
Peter Zijlstraddc97292007-10-15 17:00:10 +02003871#endif
3872}
3873
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02003874static void
Peter Zijlstraaeb73b02007-10-15 17:00:05 +02003875place_entity(struct cfs_rq *cfs_rq, struct sched_entity *se, int initial)
3876{
Peter Zijlstra1af5f732008-10-24 11:06:13 +02003877 u64 vruntime = cfs_rq->min_vruntime;
Peter Zijlstra94dfb5e2007-10-15 17:00:05 +02003878
Peter Zijlstra2cb86002007-11-09 22:39:37 +01003879 /*
3880 * The 'current' period is already promised to the current tasks,
3881 * however the extra weight of the new task will slow them down a
3882 * little, place the new task so that it fits in the slot that
3883 * stays open at the end.
3884 */
Peter Zijlstra94dfb5e2007-10-15 17:00:05 +02003885 if (initial && sched_feat(START_DEBIT))
Peter Zijlstraf9c0b092008-10-17 19:27:04 +02003886 vruntime += sched_vslice(cfs_rq, se);
Peter Zijlstraaeb73b02007-10-15 17:00:05 +02003887
Mike Galbraitha2e7a7e2009-09-18 09:19:25 +02003888 /* sleeps up to a single latency don't count. */
Mike Galbraith5ca98802010-03-11 17:17:17 +01003889 if (!initial) {
Mike Galbraitha2e7a7e2009-09-18 09:19:25 +02003890 unsigned long thresh = sysctl_sched_latency;
Peter Zijlstraa7be37a2008-06-27 13:41:11 +02003891
Mike Galbraitha2e7a7e2009-09-18 09:19:25 +02003892 /*
Mike Galbraitha2e7a7e2009-09-18 09:19:25 +02003893 * Halve their sleep time's effect, to allow
3894 * for a gentler effect of sleepers:
3895 */
3896 if (sched_feat(GENTLE_FAIR_SLEEPERS))
3897 thresh >>= 1;
Ingo Molnar51e03042009-09-16 08:54:45 +02003898
Mike Galbraitha2e7a7e2009-09-18 09:19:25 +02003899 vruntime -= thresh;
Peter Zijlstraaeb73b02007-10-15 17:00:05 +02003900 }
3901
Mike Galbraithb5d9d732009-09-08 11:12:28 +02003902 /* ensure we never gain time by being placed backwards. */
Viresh Kumar16c8f1c2012-11-08 13:33:46 +05303903 se->vruntime = max_vruntime(se->vruntime, vruntime);
Peter Zijlstraaeb73b02007-10-15 17:00:05 +02003904}
3905
Paul Turnerd3d9dc32011-07-21 09:43:39 -07003906static void check_enqueue_throttle(struct cfs_rq *cfs_rq);
3907
Mel Gormancb251762016-02-05 09:08:36 +00003908static inline void check_schedstat_required(void)
3909{
3910#ifdef CONFIG_SCHEDSTATS
3911 if (schedstat_enabled())
3912 return;
3913
3914 /* Force schedstat enabled if a dependent tracepoint is active */
3915 if (trace_sched_stat_wait_enabled() ||
3916 trace_sched_stat_sleep_enabled() ||
3917 trace_sched_stat_iowait_enabled() ||
3918 trace_sched_stat_blocked_enabled() ||
3919 trace_sched_stat_runtime_enabled()) {
Josh Poimboeufeda8dca2016-06-13 02:32:09 -05003920 printk_deferred_once("Scheduler tracepoints stat_sleep, stat_iowait, "
Mel Gormancb251762016-02-05 09:08:36 +00003921 "stat_blocked and stat_runtime require the "
Marcin Nowakowskif67abed2017-06-09 10:00:29 +02003922 "kernel parameter schedstats=enable or "
Mel Gormancb251762016-02-05 09:08:36 +00003923 "kernel.sched_schedstats=1\n");
3924 }
3925#endif
3926}
3927
Peter Zijlstrab5179ac2016-05-11 16:10:34 +02003928
3929/*
3930 * MIGRATION
3931 *
3932 * dequeue
3933 * update_curr()
3934 * update_min_vruntime()
3935 * vruntime -= min_vruntime
3936 *
3937 * enqueue
3938 * update_curr()
3939 * update_min_vruntime()
3940 * vruntime += min_vruntime
3941 *
3942 * this way the vruntime transition between RQs is done when both
3943 * min_vruntime are up-to-date.
3944 *
3945 * WAKEUP (remote)
3946 *
Peter Zijlstra59efa0b2016-05-10 18:24:37 +02003947 * ->migrate_task_rq_fair() (p->state == TASK_WAKING)
Peter Zijlstrab5179ac2016-05-11 16:10:34 +02003948 * vruntime -= min_vruntime
3949 *
3950 * enqueue
3951 * update_curr()
3952 * update_min_vruntime()
3953 * vruntime += min_vruntime
3954 *
3955 * this way we don't have the most up-to-date min_vruntime on the originating
3956 * CPU and an up-to-date min_vruntime on the destination CPU.
3957 */
3958
Peter Zijlstraaeb73b02007-10-15 17:00:05 +02003959static void
Peter Zijlstra88ec22d2009-12-16 18:04:41 +01003960enqueue_entity(struct cfs_rq *cfs_rq, struct sched_entity *se, int flags)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02003961{
Peter Zijlstra2f950352016-05-11 19:27:56 +02003962 bool renorm = !(flags & ENQUEUE_WAKEUP) || (flags & ENQUEUE_MIGRATED);
3963 bool curr = cfs_rq->curr == se;
Peter Zijlstra3a47d512016-03-09 13:04:03 +01003964
Ingo Molnar53d3bc72016-05-11 08:25:53 +02003965 /*
Peter Zijlstra2f950352016-05-11 19:27:56 +02003966 * If we're the current task, we must renormalise before calling
3967 * update_curr().
Ingo Molnar53d3bc72016-05-11 08:25:53 +02003968 */
Peter Zijlstra2f950352016-05-11 19:27:56 +02003969 if (renorm && curr)
3970 se->vruntime += cfs_rq->min_vruntime;
3971
Ingo Molnarb7cc0892007-08-09 11:16:47 +02003972 update_curr(cfs_rq);
Peter Zijlstra2f950352016-05-11 19:27:56 +02003973
3974 /*
3975 * Otherwise, renormalise after, such that we're placed at the current
3976 * moment in time, instead of some random moment in the past. Being
3977 * placed in the past could significantly boost this task to the
3978 * fairness detriment of existing tasks.
3979 */
3980 if (renorm && !curr)
3981 se->vruntime += cfs_rq->min_vruntime;
3982
Vincent Guittot89ee0482016-12-21 16:50:26 +01003983 /*
3984 * When enqueuing a sched_entity, we must:
3985 * - Update loads to have both entity and cfs_rq synced with now.
3986 * - Add its load to cfs_rq->runnable_avg
3987 * - For group_entity, update its weight to reflect the new share of
3988 * its group cfs_rq
3989 * - Add its new weight to cfs_rq->load.weight
3990 */
Peter Zijlstrab382a532017-05-06 17:37:03 +02003991 update_load_avg(cfs_rq, se, UPDATE_TG | DO_ATTACH);
Peter Zijlstra1ea6c462017-05-06 15:59:54 +02003992 update_cfs_group(se);
Peter Zijlstrab5b3e352017-08-24 17:38:30 +02003993 enqueue_runnable_load_avg(cfs_rq, se);
Linus Torvalds17bc14b2012-12-14 07:20:43 -08003994 account_entity_enqueue(cfs_rq, se);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02003995
Josh Poimboeuf1a3d0272016-06-17 12:43:23 -05003996 if (flags & ENQUEUE_WAKEUP)
Peter Zijlstraaeb73b02007-10-15 17:00:05 +02003997 place_entity(cfs_rq, se, 0);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02003998
Mel Gormancb251762016-02-05 09:08:36 +00003999 check_schedstat_required();
Josh Poimboeuf4fa8d292016-06-17 12:43:26 -05004000 update_stats_enqueue(cfs_rq, se, flags);
4001 check_spread(cfs_rq, se);
Peter Zijlstra2f950352016-05-11 19:27:56 +02004002 if (!curr)
Srivatsa Vaddagiri83b699e2007-10-15 17:00:08 +02004003 __enqueue_entity(cfs_rq, se);
Peter Zijlstra2069dd72010-11-15 15:47:00 -08004004 se->on_rq = 1;
Peter Zijlstra3d4b47b2010-11-15 15:47:01 -08004005
Paul Turnerd3d9dc32011-07-21 09:43:39 -07004006 if (cfs_rq->nr_running == 1) {
Peter Zijlstra3d4b47b2010-11-15 15:47:01 -08004007 list_add_leaf_cfs_rq(cfs_rq);
Paul Turnerd3d9dc32011-07-21 09:43:39 -07004008 check_enqueue_throttle(cfs_rq);
4009 }
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02004010}
4011
Rik van Riel2c13c9192011-02-01 09:48:37 -05004012static void __clear_buddies_last(struct sched_entity *se)
Peter Zijlstra2002c692008-11-11 11:52:33 +01004013{
Rik van Riel2c13c9192011-02-01 09:48:37 -05004014 for_each_sched_entity(se) {
4015 struct cfs_rq *cfs_rq = cfs_rq_of(se);
Peter Zijlstraf1044792012-02-11 06:05:00 +01004016 if (cfs_rq->last != se)
Rik van Riel2c13c9192011-02-01 09:48:37 -05004017 break;
Peter Zijlstraf1044792012-02-11 06:05:00 +01004018
4019 cfs_rq->last = NULL;
Rik van Riel2c13c9192011-02-01 09:48:37 -05004020 }
4021}
Peter Zijlstra2002c692008-11-11 11:52:33 +01004022
Rik van Riel2c13c9192011-02-01 09:48:37 -05004023static void __clear_buddies_next(struct sched_entity *se)
4024{
4025 for_each_sched_entity(se) {
4026 struct cfs_rq *cfs_rq = cfs_rq_of(se);
Peter Zijlstraf1044792012-02-11 06:05:00 +01004027 if (cfs_rq->next != se)
Rik van Riel2c13c9192011-02-01 09:48:37 -05004028 break;
Peter Zijlstraf1044792012-02-11 06:05:00 +01004029
4030 cfs_rq->next = NULL;
Rik van Riel2c13c9192011-02-01 09:48:37 -05004031 }
Peter Zijlstra2002c692008-11-11 11:52:33 +01004032}
4033
Rik van Rielac53db52011-02-01 09:51:03 -05004034static void __clear_buddies_skip(struct sched_entity *se)
4035{
4036 for_each_sched_entity(se) {
4037 struct cfs_rq *cfs_rq = cfs_rq_of(se);
Peter Zijlstraf1044792012-02-11 06:05:00 +01004038 if (cfs_rq->skip != se)
Rik van Rielac53db52011-02-01 09:51:03 -05004039 break;
Peter Zijlstraf1044792012-02-11 06:05:00 +01004040
4041 cfs_rq->skip = NULL;
Rik van Rielac53db52011-02-01 09:51:03 -05004042 }
4043}
4044
Peter Zijlstraa571bbe2009-01-28 14:51:40 +01004045static void clear_buddies(struct cfs_rq *cfs_rq, struct sched_entity *se)
4046{
Rik van Riel2c13c9192011-02-01 09:48:37 -05004047 if (cfs_rq->last == se)
4048 __clear_buddies_last(se);
4049
4050 if (cfs_rq->next == se)
4051 __clear_buddies_next(se);
Rik van Rielac53db52011-02-01 09:51:03 -05004052
4053 if (cfs_rq->skip == se)
4054 __clear_buddies_skip(se);
Peter Zijlstraa571bbe2009-01-28 14:51:40 +01004055}
4056
Peter Zijlstra6c16a6d2012-03-21 13:07:16 -07004057static __always_inline void return_cfs_rq_runtime(struct cfs_rq *cfs_rq);
Paul Turnerd8b49862011-07-21 09:43:41 -07004058
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02004059static void
Peter Zijlstra371fd7e2010-03-24 16:38:48 +01004060dequeue_entity(struct cfs_rq *cfs_rq, struct sched_entity *se, int flags)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02004061{
Dmitry Adamushkoa2a2d682007-10-15 17:00:13 +02004062 /*
4063 * Update run-time statistics of the 'current'.
4064 */
4065 update_curr(cfs_rq);
Vincent Guittot89ee0482016-12-21 16:50:26 +01004066
4067 /*
4068 * When dequeuing a sched_entity, we must:
4069 * - Update loads to have both entity and cfs_rq synced with now.
4070 * - Substract its load from the cfs_rq->runnable_avg.
4071 * - Substract its previous weight from cfs_rq->load.weight.
4072 * - For group entity, update its weight to reflect the new share
4073 * of its group cfs_rq.
4074 */
Peter Zijlstra88c06162017-05-06 17:32:43 +02004075 update_load_avg(cfs_rq, se, UPDATE_TG);
Peter Zijlstrab5b3e352017-08-24 17:38:30 +02004076 dequeue_runnable_load_avg(cfs_rq, se);
Dmitry Adamushkoa2a2d682007-10-15 17:00:13 +02004077
Josh Poimboeuf4fa8d292016-06-17 12:43:26 -05004078 update_stats_dequeue(cfs_rq, se, flags);
Peter Zijlstra67e9fb22007-10-15 17:00:10 +02004079
Peter Zijlstra2002c692008-11-11 11:52:33 +01004080 clear_buddies(cfs_rq, se);
Peter Zijlstra47932412008-11-04 21:25:09 +01004081
Srivatsa Vaddagiri83b699e2007-10-15 17:00:08 +02004082 if (se != cfs_rq->curr)
Dmitry Adamushko30cfdcf2007-10-15 17:00:07 +02004083 __dequeue_entity(cfs_rq, se);
Linus Torvalds17bc14b2012-12-14 07:20:43 -08004084 se->on_rq = 0;
Dmitry Adamushko30cfdcf2007-10-15 17:00:07 +02004085 account_entity_dequeue(cfs_rq, se);
Peter Zijlstra88ec22d2009-12-16 18:04:41 +01004086
4087 /*
Peter Zijlstrab60205c2016-09-20 21:58:12 +02004088 * Normalize after update_curr(); which will also have moved
4089 * min_vruntime if @se is the one holding it back. But before doing
4090 * update_min_vruntime() again, which will discount @se's position and
4091 * can move min_vruntime forward still more.
Peter Zijlstra88ec22d2009-12-16 18:04:41 +01004092 */
Peter Zijlstra371fd7e2010-03-24 16:38:48 +01004093 if (!(flags & DEQUEUE_SLEEP))
Peter Zijlstra88ec22d2009-12-16 18:04:41 +01004094 se->vruntime -= cfs_rq->min_vruntime;
Peter Zijlstra1e876232011-05-17 16:21:10 -07004095
Paul Turnerd8b49862011-07-21 09:43:41 -07004096 /* return excess runtime on last dequeue */
4097 return_cfs_rq_runtime(cfs_rq);
4098
Peter Zijlstra1ea6c462017-05-06 15:59:54 +02004099 update_cfs_group(se);
Peter Zijlstrab60205c2016-09-20 21:58:12 +02004100
4101 /*
4102 * Now advance min_vruntime if @se was the entity holding it back,
4103 * except when: DEQUEUE_SAVE && !DEQUEUE_MOVE, in this case we'll be
4104 * put back on, and if we advance min_vruntime, we'll be placed back
4105 * further than we started -- ie. we'll be penalized.
4106 */
4107 if ((flags & (DEQUEUE_SAVE | DEQUEUE_MOVE)) == DEQUEUE_SAVE)
4108 update_min_vruntime(cfs_rq);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02004109}
4110
4111/*
4112 * Preempt the current task with a newly woken task if needed:
4113 */
Peter Zijlstra7c92e542007-09-05 14:32:49 +02004114static void
Ingo Molnar2e09bf52007-10-15 17:00:05 +02004115check_preempt_tick(struct cfs_rq *cfs_rq, struct sched_entity *curr)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02004116{
Peter Zijlstra11697832007-09-05 14:32:49 +02004117 unsigned long ideal_runtime, delta_exec;
Wang Xingchaof4cfb332011-09-16 13:35:52 -04004118 struct sched_entity *se;
4119 s64 delta;
Peter Zijlstra11697832007-09-05 14:32:49 +02004120
Peter Zijlstra6d0f0eb2007-10-15 17:00:05 +02004121 ideal_runtime = sched_slice(cfs_rq, curr);
Peter Zijlstra11697832007-09-05 14:32:49 +02004122 delta_exec = curr->sum_exec_runtime - curr->prev_sum_exec_runtime;
Mike Galbraitha9f3e2b2009-01-28 14:51:39 +01004123 if (delta_exec > ideal_runtime) {
Kirill Tkhai88751252014-06-29 00:03:57 +04004124 resched_curr(rq_of(cfs_rq));
Mike Galbraitha9f3e2b2009-01-28 14:51:39 +01004125 /*
4126 * The current task ran long enough, ensure it doesn't get
4127 * re-elected due to buddy favours.
4128 */
4129 clear_buddies(cfs_rq, curr);
Mike Galbraithf685cea2009-10-23 23:09:22 +02004130 return;
4131 }
4132
4133 /*
4134 * Ensure that a task that missed wakeup preemption by a
4135 * narrow margin doesn't have to wait for a full slice.
4136 * This also mitigates buddy induced latencies under load.
4137 */
Mike Galbraithf685cea2009-10-23 23:09:22 +02004138 if (delta_exec < sysctl_sched_min_granularity)
4139 return;
4140
Wang Xingchaof4cfb332011-09-16 13:35:52 -04004141 se = __pick_first_entity(cfs_rq);
4142 delta = curr->vruntime - se->vruntime;
Mike Galbraithf685cea2009-10-23 23:09:22 +02004143
Wang Xingchaof4cfb332011-09-16 13:35:52 -04004144 if (delta < 0)
4145 return;
Mike Galbraithd7d82942011-01-05 05:41:17 +01004146
Wang Xingchaof4cfb332011-09-16 13:35:52 -04004147 if (delta > ideal_runtime)
Kirill Tkhai88751252014-06-29 00:03:57 +04004148 resched_curr(rq_of(cfs_rq));
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02004149}
4150
Srivatsa Vaddagiri83b699e2007-10-15 17:00:08 +02004151static void
Ingo Molnar8494f412007-08-09 11:16:48 +02004152set_next_entity(struct cfs_rq *cfs_rq, struct sched_entity *se)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02004153{
Srivatsa Vaddagiri83b699e2007-10-15 17:00:08 +02004154 /* 'current' is not kept within the tree. */
4155 if (se->on_rq) {
4156 /*
4157 * Any task has to be enqueued before it get to execute on
4158 * a CPU. So account for the time it spent waiting on the
4159 * runqueue.
4160 */
Josh Poimboeuf4fa8d292016-06-17 12:43:26 -05004161 update_stats_wait_end(cfs_rq, se);
Srivatsa Vaddagiri83b699e2007-10-15 17:00:08 +02004162 __dequeue_entity(cfs_rq, se);
Peter Zijlstra88c06162017-05-06 17:32:43 +02004163 update_load_avg(cfs_rq, se, UPDATE_TG);
Srivatsa Vaddagiri83b699e2007-10-15 17:00:08 +02004164 }
4165
Ingo Molnar79303e92007-08-09 11:16:47 +02004166 update_stats_curr_start(cfs_rq, se);
Ingo Molnar429d43b2007-10-15 17:00:03 +02004167 cfs_rq->curr = se;
Josh Poimboeuf4fa8d292016-06-17 12:43:26 -05004168
Ingo Molnareba1ed42007-10-15 17:00:02 +02004169 /*
4170 * Track our maximum slice length, if the CPU's load is at
4171 * least twice that of our own weight (i.e. dont track it
4172 * when there are only lesser-weight tasks around):
4173 */
Mel Gormancb251762016-02-05 09:08:36 +00004174 if (schedstat_enabled() && rq_of(cfs_rq)->load.weight >= 2*se->load.weight) {
Josh Poimboeuf4fa8d292016-06-17 12:43:26 -05004175 schedstat_set(se->statistics.slice_max,
4176 max((u64)schedstat_val(se->statistics.slice_max),
4177 se->sum_exec_runtime - se->prev_sum_exec_runtime));
Ingo Molnareba1ed42007-10-15 17:00:02 +02004178 }
Josh Poimboeuf4fa8d292016-06-17 12:43:26 -05004179
Peter Zijlstra4a55b452007-09-05 14:32:49 +02004180 se->prev_sum_exec_runtime = se->sum_exec_runtime;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02004181}
4182
Peter Zijlstra3f3a4902008-10-24 11:06:16 +02004183static int
4184wakeup_preempt_entity(struct sched_entity *curr, struct sched_entity *se);
4185
Rik van Rielac53db52011-02-01 09:51:03 -05004186/*
4187 * Pick the next process, keeping these things in mind, in this order:
4188 * 1) keep things fair between processes/task groups
4189 * 2) pick the "next" process, since someone really wants that to run
4190 * 3) pick the "last" process, for cache locality
4191 * 4) do not run the "skip" process, if something else is available
4192 */
Peter Zijlstra678d5712012-02-11 06:05:00 +01004193static struct sched_entity *
4194pick_next_entity(struct cfs_rq *cfs_rq, struct sched_entity *curr)
Peter Zijlstraaa2ac252008-03-14 21:12:12 +01004195{
Peter Zijlstra678d5712012-02-11 06:05:00 +01004196 struct sched_entity *left = __pick_first_entity(cfs_rq);
4197 struct sched_entity *se;
4198
4199 /*
4200 * If curr is set we have to see if its left of the leftmost entity
4201 * still in the tree, provided there was anything in the tree at all.
4202 */
4203 if (!left || (curr && entity_before(curr, left)))
4204 left = curr;
4205
4206 se = left; /* ideally we run the leftmost entity */
Peter Zijlstraf4b67552008-11-04 21:25:07 +01004207
Rik van Rielac53db52011-02-01 09:51:03 -05004208 /*
4209 * Avoid running the skip buddy, if running something else can
4210 * be done without getting too unfair.
4211 */
4212 if (cfs_rq->skip == se) {
Peter Zijlstra678d5712012-02-11 06:05:00 +01004213 struct sched_entity *second;
4214
4215 if (se == curr) {
4216 second = __pick_first_entity(cfs_rq);
4217 } else {
4218 second = __pick_next_entity(se);
4219 if (!second || (curr && entity_before(curr, second)))
4220 second = curr;
4221 }
4222
Rik van Rielac53db52011-02-01 09:51:03 -05004223 if (second && wakeup_preempt_entity(second, left) < 1)
4224 se = second;
4225 }
Peter Zijlstraaa2ac252008-03-14 21:12:12 +01004226
Mike Galbraithf685cea2009-10-23 23:09:22 +02004227 /*
4228 * Prefer last buddy, try to return the CPU to a preempted task.
4229 */
4230 if (cfs_rq->last && wakeup_preempt_entity(cfs_rq->last, left) < 1)
4231 se = cfs_rq->last;
4232
Rik van Rielac53db52011-02-01 09:51:03 -05004233 /*
4234 * Someone really wants this to run. If it's not unfair, run it.
4235 */
4236 if (cfs_rq->next && wakeup_preempt_entity(cfs_rq->next, left) < 1)
4237 se = cfs_rq->next;
4238
Mike Galbraithf685cea2009-10-23 23:09:22 +02004239 clear_buddies(cfs_rq, se);
Peter Zijlstra47932412008-11-04 21:25:09 +01004240
4241 return se;
Peter Zijlstraaa2ac252008-03-14 21:12:12 +01004242}
4243
Peter Zijlstra678d5712012-02-11 06:05:00 +01004244static bool check_cfs_rq_runtime(struct cfs_rq *cfs_rq);
Paul Turnerd3d9dc32011-07-21 09:43:39 -07004245
Ingo Molnarab6cde22007-08-09 11:16:48 +02004246static void put_prev_entity(struct cfs_rq *cfs_rq, struct sched_entity *prev)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02004247{
4248 /*
4249 * If still on the runqueue then deactivate_task()
4250 * was not called and update_curr() has to be done:
4251 */
4252 if (prev->on_rq)
Ingo Molnarb7cc0892007-08-09 11:16:47 +02004253 update_curr(cfs_rq);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02004254
Paul Turnerd3d9dc32011-07-21 09:43:39 -07004255 /* throttle cfs_rqs exceeding runtime */
4256 check_cfs_rq_runtime(cfs_rq);
4257
Josh Poimboeuf4fa8d292016-06-17 12:43:26 -05004258 check_spread(cfs_rq, prev);
Mel Gormancb251762016-02-05 09:08:36 +00004259
Dmitry Adamushko30cfdcf2007-10-15 17:00:07 +02004260 if (prev->on_rq) {
Josh Poimboeuf4fa8d292016-06-17 12:43:26 -05004261 update_stats_wait_start(cfs_rq, prev);
Dmitry Adamushko30cfdcf2007-10-15 17:00:07 +02004262 /* Put 'current' back into the tree. */
4263 __enqueue_entity(cfs_rq, prev);
Paul Turner9d85f212012-10-04 13:18:29 +02004264 /* in !on_rq case, update occurred at dequeue */
Peter Zijlstra88c06162017-05-06 17:32:43 +02004265 update_load_avg(cfs_rq, prev, 0);
Dmitry Adamushko30cfdcf2007-10-15 17:00:07 +02004266 }
Ingo Molnar429d43b2007-10-15 17:00:03 +02004267 cfs_rq->curr = NULL;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02004268}
4269
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01004270static void
4271entity_tick(struct cfs_rq *cfs_rq, struct sched_entity *curr, int queued)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02004272{
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02004273 /*
Dmitry Adamushko30cfdcf2007-10-15 17:00:07 +02004274 * Update run-time statistics of the 'current'.
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02004275 */
Dmitry Adamushko30cfdcf2007-10-15 17:00:07 +02004276 update_curr(cfs_rq);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02004277
Paul Turner43365bd2010-12-15 19:10:17 -08004278 /*
Paul Turner9d85f212012-10-04 13:18:29 +02004279 * Ensure that runnable average is periodically updated.
4280 */
Peter Zijlstra88c06162017-05-06 17:32:43 +02004281 update_load_avg(cfs_rq, curr, UPDATE_TG);
Peter Zijlstra1ea6c462017-05-06 15:59:54 +02004282 update_cfs_group(curr);
Paul Turner9d85f212012-10-04 13:18:29 +02004283
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01004284#ifdef CONFIG_SCHED_HRTICK
4285 /*
4286 * queued ticks are scheduled to match the slice, so don't bother
4287 * validating it and just reschedule.
4288 */
Harvey Harrison983ed7a2008-04-24 18:17:55 -07004289 if (queued) {
Kirill Tkhai88751252014-06-29 00:03:57 +04004290 resched_curr(rq_of(cfs_rq));
Harvey Harrison983ed7a2008-04-24 18:17:55 -07004291 return;
4292 }
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01004293 /*
4294 * don't let the period tick interfere with the hrtick preemption
4295 */
4296 if (!sched_feat(DOUBLE_TICK) &&
4297 hrtimer_active(&rq_of(cfs_rq)->hrtick_timer))
4298 return;
4299#endif
4300
Yong Zhang2c2efae2011-07-29 16:20:33 +08004301 if (cfs_rq->nr_running > 1)
Ingo Molnar2e09bf52007-10-15 17:00:05 +02004302 check_preempt_tick(cfs_rq, curr);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02004303}
4304
Paul Turnerab84d312011-07-21 09:43:28 -07004305
4306/**************************************************
4307 * CFS bandwidth control machinery
4308 */
4309
4310#ifdef CONFIG_CFS_BANDWIDTH
Peter Zijlstra029632f2011-10-25 10:00:11 +02004311
4312#ifdef HAVE_JUMP_LABEL
Ingo Molnarc5905af2012-02-24 08:31:31 +01004313static struct static_key __cfs_bandwidth_used;
Peter Zijlstra029632f2011-10-25 10:00:11 +02004314
4315static inline bool cfs_bandwidth_used(void)
4316{
Ingo Molnarc5905af2012-02-24 08:31:31 +01004317 return static_key_false(&__cfs_bandwidth_used);
Peter Zijlstra029632f2011-10-25 10:00:11 +02004318}
4319
Ben Segall1ee14e62013-10-16 11:16:12 -07004320void cfs_bandwidth_usage_inc(void)
Peter Zijlstra029632f2011-10-25 10:00:11 +02004321{
Ben Segall1ee14e62013-10-16 11:16:12 -07004322 static_key_slow_inc(&__cfs_bandwidth_used);
4323}
4324
4325void cfs_bandwidth_usage_dec(void)
4326{
4327 static_key_slow_dec(&__cfs_bandwidth_used);
Peter Zijlstra029632f2011-10-25 10:00:11 +02004328}
4329#else /* HAVE_JUMP_LABEL */
4330static bool cfs_bandwidth_used(void)
4331{
4332 return true;
4333}
4334
Ben Segall1ee14e62013-10-16 11:16:12 -07004335void cfs_bandwidth_usage_inc(void) {}
4336void cfs_bandwidth_usage_dec(void) {}
Peter Zijlstra029632f2011-10-25 10:00:11 +02004337#endif /* HAVE_JUMP_LABEL */
4338
Paul Turnerab84d312011-07-21 09:43:28 -07004339/*
4340 * default period for cfs group bandwidth.
4341 * default: 0.1s, units: nanoseconds
4342 */
4343static inline u64 default_cfs_period(void)
4344{
4345 return 100000000ULL;
4346}
Paul Turnerec12cb72011-07-21 09:43:30 -07004347
4348static inline u64 sched_cfs_bandwidth_slice(void)
4349{
4350 return (u64)sysctl_sched_cfs_bandwidth_slice * NSEC_PER_USEC;
4351}
4352
Paul Turnera9cf55b2011-07-21 09:43:32 -07004353/*
4354 * Replenish runtime according to assigned quota and update expiration time.
4355 * We use sched_clock_cpu directly instead of rq->clock to avoid adding
4356 * additional synchronization around rq->lock.
4357 *
4358 * requires cfs_b->lock
4359 */
Peter Zijlstra029632f2011-10-25 10:00:11 +02004360void __refill_cfs_bandwidth_runtime(struct cfs_bandwidth *cfs_b)
Paul Turnera9cf55b2011-07-21 09:43:32 -07004361{
4362 u64 now;
4363
4364 if (cfs_b->quota == RUNTIME_INF)
4365 return;
4366
4367 now = sched_clock_cpu(smp_processor_id());
4368 cfs_b->runtime = cfs_b->quota;
4369 cfs_b->runtime_expires = now + ktime_to_ns(cfs_b->period);
4370}
4371
Peter Zijlstra029632f2011-10-25 10:00:11 +02004372static inline struct cfs_bandwidth *tg_cfs_bandwidth(struct task_group *tg)
4373{
4374 return &tg->cfs_bandwidth;
4375}
4376
Paul Turnerf1b17282012-10-04 13:18:31 +02004377/* rq->task_clock normalized against any time this cfs_rq has spent throttled */
4378static inline u64 cfs_rq_clock_task(struct cfs_rq *cfs_rq)
4379{
4380 if (unlikely(cfs_rq->throttle_count))
Xunlei Pang1a99ae32016-05-10 21:03:18 +08004381 return cfs_rq->throttled_clock_task - cfs_rq->throttled_clock_task_time;
Paul Turnerf1b17282012-10-04 13:18:31 +02004382
Frederic Weisbecker78becc22013-04-12 01:51:02 +02004383 return rq_clock_task(rq_of(cfs_rq)) - cfs_rq->throttled_clock_task_time;
Paul Turnerf1b17282012-10-04 13:18:31 +02004384}
4385
Paul Turner85dac902011-07-21 09:43:33 -07004386/* returns 0 on failure to allocate runtime */
4387static int assign_cfs_rq_runtime(struct cfs_rq *cfs_rq)
Paul Turnerec12cb72011-07-21 09:43:30 -07004388{
4389 struct task_group *tg = cfs_rq->tg;
4390 struct cfs_bandwidth *cfs_b = tg_cfs_bandwidth(tg);
Paul Turnera9cf55b2011-07-21 09:43:32 -07004391 u64 amount = 0, min_amount, expires;
Paul Turnerec12cb72011-07-21 09:43:30 -07004392
4393 /* note: this is a positive sum as runtime_remaining <= 0 */
4394 min_amount = sched_cfs_bandwidth_slice() - cfs_rq->runtime_remaining;
4395
4396 raw_spin_lock(&cfs_b->lock);
4397 if (cfs_b->quota == RUNTIME_INF)
4398 amount = min_amount;
Paul Turner58088ad2011-07-21 09:43:31 -07004399 else {
Peter Zijlstra77a4d1a2015-04-15 11:41:57 +02004400 start_cfs_bandwidth(cfs_b);
Paul Turner58088ad2011-07-21 09:43:31 -07004401
4402 if (cfs_b->runtime > 0) {
4403 amount = min(cfs_b->runtime, min_amount);
4404 cfs_b->runtime -= amount;
4405 cfs_b->idle = 0;
4406 }
Paul Turnerec12cb72011-07-21 09:43:30 -07004407 }
Paul Turnera9cf55b2011-07-21 09:43:32 -07004408 expires = cfs_b->runtime_expires;
Paul Turnerec12cb72011-07-21 09:43:30 -07004409 raw_spin_unlock(&cfs_b->lock);
4410
4411 cfs_rq->runtime_remaining += amount;
Paul Turnera9cf55b2011-07-21 09:43:32 -07004412 /*
4413 * we may have advanced our local expiration to account for allowed
4414 * spread between our sched_clock and the one on which runtime was
4415 * issued.
4416 */
4417 if ((s64)(expires - cfs_rq->runtime_expires) > 0)
4418 cfs_rq->runtime_expires = expires;
Paul Turner85dac902011-07-21 09:43:33 -07004419
4420 return cfs_rq->runtime_remaining > 0;
Paul Turnera9cf55b2011-07-21 09:43:32 -07004421}
4422
4423/*
4424 * Note: This depends on the synchronization provided by sched_clock and the
4425 * fact that rq->clock snapshots this value.
4426 */
4427static void expire_cfs_rq_runtime(struct cfs_rq *cfs_rq)
4428{
4429 struct cfs_bandwidth *cfs_b = tg_cfs_bandwidth(cfs_rq->tg);
Paul Turnera9cf55b2011-07-21 09:43:32 -07004430
4431 /* if the deadline is ahead of our clock, nothing to do */
Frederic Weisbecker78becc22013-04-12 01:51:02 +02004432 if (likely((s64)(rq_clock(rq_of(cfs_rq)) - cfs_rq->runtime_expires) < 0))
Paul Turnera9cf55b2011-07-21 09:43:32 -07004433 return;
4434
4435 if (cfs_rq->runtime_remaining < 0)
4436 return;
4437
4438 /*
4439 * If the local deadline has passed we have to consider the
4440 * possibility that our sched_clock is 'fast' and the global deadline
4441 * has not truly expired.
4442 *
4443 * Fortunately we can check determine whether this the case by checking
Ben Segall51f21762014-05-19 15:49:45 -07004444 * whether the global deadline has advanced. It is valid to compare
4445 * cfs_b->runtime_expires without any locks since we only care about
4446 * exact equality, so a partial write will still work.
Paul Turnera9cf55b2011-07-21 09:43:32 -07004447 */
4448
Ben Segall51f21762014-05-19 15:49:45 -07004449 if (cfs_rq->runtime_expires != cfs_b->runtime_expires) {
Paul Turnera9cf55b2011-07-21 09:43:32 -07004450 /* extend local deadline, drift is bounded above by 2 ticks */
4451 cfs_rq->runtime_expires += TICK_NSEC;
4452 } else {
4453 /* global deadline is ahead, expiration has passed */
4454 cfs_rq->runtime_remaining = 0;
4455 }
Paul Turnerec12cb72011-07-21 09:43:30 -07004456}
4457
Peter Zijlstra9dbdb152013-11-18 18:27:06 +01004458static void __account_cfs_rq_runtime(struct cfs_rq *cfs_rq, u64 delta_exec)
Paul Turnerec12cb72011-07-21 09:43:30 -07004459{
Paul Turnera9cf55b2011-07-21 09:43:32 -07004460 /* dock delta_exec before expiring quota (as it could span periods) */
Paul Turnerec12cb72011-07-21 09:43:30 -07004461 cfs_rq->runtime_remaining -= delta_exec;
Paul Turnera9cf55b2011-07-21 09:43:32 -07004462 expire_cfs_rq_runtime(cfs_rq);
4463
4464 if (likely(cfs_rq->runtime_remaining > 0))
Paul Turnerec12cb72011-07-21 09:43:30 -07004465 return;
4466
Paul Turner85dac902011-07-21 09:43:33 -07004467 /*
4468 * if we're unable to extend our runtime we resched so that the active
4469 * hierarchy can be throttled
4470 */
4471 if (!assign_cfs_rq_runtime(cfs_rq) && likely(cfs_rq->curr))
Kirill Tkhai88751252014-06-29 00:03:57 +04004472 resched_curr(rq_of(cfs_rq));
Paul Turnerec12cb72011-07-21 09:43:30 -07004473}
4474
Peter Zijlstra6c16a6d2012-03-21 13:07:16 -07004475static __always_inline
Peter Zijlstra9dbdb152013-11-18 18:27:06 +01004476void account_cfs_rq_runtime(struct cfs_rq *cfs_rq, u64 delta_exec)
Paul Turnerec12cb72011-07-21 09:43:30 -07004477{
Paul Turner56f570e2011-11-07 20:26:33 -08004478 if (!cfs_bandwidth_used() || !cfs_rq->runtime_enabled)
Paul Turnerec12cb72011-07-21 09:43:30 -07004479 return;
4480
4481 __account_cfs_rq_runtime(cfs_rq, delta_exec);
4482}
4483
Paul Turner85dac902011-07-21 09:43:33 -07004484static inline int cfs_rq_throttled(struct cfs_rq *cfs_rq)
4485{
Paul Turner56f570e2011-11-07 20:26:33 -08004486 return cfs_bandwidth_used() && cfs_rq->throttled;
Paul Turner85dac902011-07-21 09:43:33 -07004487}
4488
Paul Turner64660c82011-07-21 09:43:36 -07004489/* check whether cfs_rq, or any parent, is throttled */
4490static inline int throttled_hierarchy(struct cfs_rq *cfs_rq)
4491{
Paul Turner56f570e2011-11-07 20:26:33 -08004492 return cfs_bandwidth_used() && cfs_rq->throttle_count;
Paul Turner64660c82011-07-21 09:43:36 -07004493}
4494
4495/*
4496 * Ensure that neither of the group entities corresponding to src_cpu or
4497 * dest_cpu are members of a throttled hierarchy when performing group
4498 * load-balance operations.
4499 */
4500static inline int throttled_lb_pair(struct task_group *tg,
4501 int src_cpu, int dest_cpu)
4502{
4503 struct cfs_rq *src_cfs_rq, *dest_cfs_rq;
4504
4505 src_cfs_rq = tg->cfs_rq[src_cpu];
4506 dest_cfs_rq = tg->cfs_rq[dest_cpu];
4507
4508 return throttled_hierarchy(src_cfs_rq) ||
4509 throttled_hierarchy(dest_cfs_rq);
4510}
4511
4512/* updated child weight may affect parent so we have to do this bottom up */
4513static int tg_unthrottle_up(struct task_group *tg, void *data)
4514{
4515 struct rq *rq = data;
4516 struct cfs_rq *cfs_rq = tg->cfs_rq[cpu_of(rq)];
4517
4518 cfs_rq->throttle_count--;
Paul Turner64660c82011-07-21 09:43:36 -07004519 if (!cfs_rq->throttle_count) {
Paul Turnerf1b17282012-10-04 13:18:31 +02004520 /* adjust cfs_rq_clock_task() */
Frederic Weisbecker78becc22013-04-12 01:51:02 +02004521 cfs_rq->throttled_clock_task_time += rq_clock_task(rq) -
Paul Turnerf1b17282012-10-04 13:18:31 +02004522 cfs_rq->throttled_clock_task;
Paul Turner64660c82011-07-21 09:43:36 -07004523 }
Paul Turner64660c82011-07-21 09:43:36 -07004524
4525 return 0;
4526}
4527
4528static int tg_throttle_down(struct task_group *tg, void *data)
4529{
4530 struct rq *rq = data;
4531 struct cfs_rq *cfs_rq = tg->cfs_rq[cpu_of(rq)];
4532
Paul Turner82958362012-10-04 13:18:31 +02004533 /* group is entering throttled state, stop time */
4534 if (!cfs_rq->throttle_count)
Frederic Weisbecker78becc22013-04-12 01:51:02 +02004535 cfs_rq->throttled_clock_task = rq_clock_task(rq);
Paul Turner64660c82011-07-21 09:43:36 -07004536 cfs_rq->throttle_count++;
4537
4538 return 0;
4539}
4540
Paul Turnerd3d9dc32011-07-21 09:43:39 -07004541static void throttle_cfs_rq(struct cfs_rq *cfs_rq)
Paul Turner85dac902011-07-21 09:43:33 -07004542{
4543 struct rq *rq = rq_of(cfs_rq);
4544 struct cfs_bandwidth *cfs_b = tg_cfs_bandwidth(cfs_rq->tg);
4545 struct sched_entity *se;
4546 long task_delta, dequeue = 1;
Peter Zijlstra77a4d1a2015-04-15 11:41:57 +02004547 bool empty;
Paul Turner85dac902011-07-21 09:43:33 -07004548
4549 se = cfs_rq->tg->se[cpu_of(rq_of(cfs_rq))];
4550
Paul Turnerf1b17282012-10-04 13:18:31 +02004551 /* freeze hierarchy runnable averages while throttled */
Paul Turner64660c82011-07-21 09:43:36 -07004552 rcu_read_lock();
4553 walk_tg_tree_from(cfs_rq->tg, tg_throttle_down, tg_nop, (void *)rq);
4554 rcu_read_unlock();
Paul Turner85dac902011-07-21 09:43:33 -07004555
4556 task_delta = cfs_rq->h_nr_running;
4557 for_each_sched_entity(se) {
4558 struct cfs_rq *qcfs_rq = cfs_rq_of(se);
4559 /* throttled entity or throttle-on-deactivate */
4560 if (!se->on_rq)
4561 break;
4562
4563 if (dequeue)
4564 dequeue_entity(qcfs_rq, se, DEQUEUE_SLEEP);
4565 qcfs_rq->h_nr_running -= task_delta;
4566
4567 if (qcfs_rq->load.weight)
4568 dequeue = 0;
4569 }
4570
4571 if (!se)
Kirill Tkhai72465442014-05-09 03:00:14 +04004572 sub_nr_running(rq, task_delta);
Paul Turner85dac902011-07-21 09:43:33 -07004573
4574 cfs_rq->throttled = 1;
Frederic Weisbecker78becc22013-04-12 01:51:02 +02004575 cfs_rq->throttled_clock = rq_clock(rq);
Paul Turner85dac902011-07-21 09:43:33 -07004576 raw_spin_lock(&cfs_b->lock);
Cong Wangd49db342015-06-24 12:41:47 -07004577 empty = list_empty(&cfs_b->throttled_cfs_rq);
Peter Zijlstra77a4d1a2015-04-15 11:41:57 +02004578
Ben Segallc06f04c2014-06-20 15:21:20 -07004579 /*
4580 * Add to the _head_ of the list, so that an already-started
4581 * distribute_cfs_runtime will not see us
4582 */
4583 list_add_rcu(&cfs_rq->throttled_list, &cfs_b->throttled_cfs_rq);
Peter Zijlstra77a4d1a2015-04-15 11:41:57 +02004584
4585 /*
4586 * If we're the first throttled task, make sure the bandwidth
4587 * timer is running.
4588 */
4589 if (empty)
4590 start_cfs_bandwidth(cfs_b);
4591
Paul Turner85dac902011-07-21 09:43:33 -07004592 raw_spin_unlock(&cfs_b->lock);
4593}
4594
Peter Zijlstra029632f2011-10-25 10:00:11 +02004595void unthrottle_cfs_rq(struct cfs_rq *cfs_rq)
Paul Turner671fd9d2011-07-21 09:43:34 -07004596{
4597 struct rq *rq = rq_of(cfs_rq);
4598 struct cfs_bandwidth *cfs_b = tg_cfs_bandwidth(cfs_rq->tg);
4599 struct sched_entity *se;
4600 int enqueue = 1;
4601 long task_delta;
4602
Michael Wang22b958d2013-06-04 14:23:39 +08004603 se = cfs_rq->tg->se[cpu_of(rq)];
Paul Turner671fd9d2011-07-21 09:43:34 -07004604
4605 cfs_rq->throttled = 0;
Frederic Weisbecker1a55af22013-04-12 01:51:01 +02004606
4607 update_rq_clock(rq);
4608
Paul Turner671fd9d2011-07-21 09:43:34 -07004609 raw_spin_lock(&cfs_b->lock);
Frederic Weisbecker78becc22013-04-12 01:51:02 +02004610 cfs_b->throttled_time += rq_clock(rq) - cfs_rq->throttled_clock;
Paul Turner671fd9d2011-07-21 09:43:34 -07004611 list_del_rcu(&cfs_rq->throttled_list);
4612 raw_spin_unlock(&cfs_b->lock);
4613
Paul Turner64660c82011-07-21 09:43:36 -07004614 /* update hierarchical throttle state */
4615 walk_tg_tree_from(cfs_rq->tg, tg_nop, tg_unthrottle_up, (void *)rq);
4616
Paul Turner671fd9d2011-07-21 09:43:34 -07004617 if (!cfs_rq->load.weight)
4618 return;
4619
4620 task_delta = cfs_rq->h_nr_running;
4621 for_each_sched_entity(se) {
4622 if (se->on_rq)
4623 enqueue = 0;
4624
4625 cfs_rq = cfs_rq_of(se);
4626 if (enqueue)
4627 enqueue_entity(cfs_rq, se, ENQUEUE_WAKEUP);
4628 cfs_rq->h_nr_running += task_delta;
4629
4630 if (cfs_rq_throttled(cfs_rq))
4631 break;
4632 }
4633
4634 if (!se)
Kirill Tkhai72465442014-05-09 03:00:14 +04004635 add_nr_running(rq, task_delta);
Paul Turner671fd9d2011-07-21 09:43:34 -07004636
4637 /* determine whether we need to wake up potentially idle cpu */
4638 if (rq->curr == rq->idle && rq->cfs.nr_running)
Kirill Tkhai88751252014-06-29 00:03:57 +04004639 resched_curr(rq);
Paul Turner671fd9d2011-07-21 09:43:34 -07004640}
4641
4642static u64 distribute_cfs_runtime(struct cfs_bandwidth *cfs_b,
4643 u64 remaining, u64 expires)
4644{
4645 struct cfs_rq *cfs_rq;
Ben Segallc06f04c2014-06-20 15:21:20 -07004646 u64 runtime;
4647 u64 starting_runtime = remaining;
Paul Turner671fd9d2011-07-21 09:43:34 -07004648
4649 rcu_read_lock();
4650 list_for_each_entry_rcu(cfs_rq, &cfs_b->throttled_cfs_rq,
4651 throttled_list) {
4652 struct rq *rq = rq_of(cfs_rq);
Peter Zijlstra8a8c69c2016-10-04 16:04:35 +02004653 struct rq_flags rf;
Paul Turner671fd9d2011-07-21 09:43:34 -07004654
Peter Zijlstra8a8c69c2016-10-04 16:04:35 +02004655 rq_lock(rq, &rf);
Paul Turner671fd9d2011-07-21 09:43:34 -07004656 if (!cfs_rq_throttled(cfs_rq))
4657 goto next;
4658
4659 runtime = -cfs_rq->runtime_remaining + 1;
4660 if (runtime > remaining)
4661 runtime = remaining;
4662 remaining -= runtime;
4663
4664 cfs_rq->runtime_remaining += runtime;
4665 cfs_rq->runtime_expires = expires;
4666
4667 /* we check whether we're throttled above */
4668 if (cfs_rq->runtime_remaining > 0)
4669 unthrottle_cfs_rq(cfs_rq);
4670
4671next:
Peter Zijlstra8a8c69c2016-10-04 16:04:35 +02004672 rq_unlock(rq, &rf);
Paul Turner671fd9d2011-07-21 09:43:34 -07004673
4674 if (!remaining)
4675 break;
4676 }
4677 rcu_read_unlock();
4678
Ben Segallc06f04c2014-06-20 15:21:20 -07004679 return starting_runtime - remaining;
Paul Turner671fd9d2011-07-21 09:43:34 -07004680}
4681
Paul Turner58088ad2011-07-21 09:43:31 -07004682/*
4683 * Responsible for refilling a task_group's bandwidth and unthrottling its
4684 * cfs_rqs as appropriate. If there has been no activity within the last
4685 * period the timer is deactivated until scheduling resumes; cfs_b->idle is
4686 * used to track this state.
4687 */
4688static int do_sched_cfs_period_timer(struct cfs_bandwidth *cfs_b, int overrun)
4689{
Paul Turner671fd9d2011-07-21 09:43:34 -07004690 u64 runtime, runtime_expires;
Ben Segall51f21762014-05-19 15:49:45 -07004691 int throttled;
Paul Turner58088ad2011-07-21 09:43:31 -07004692
Paul Turner58088ad2011-07-21 09:43:31 -07004693 /* no need to continue the timer with no bandwidth constraint */
4694 if (cfs_b->quota == RUNTIME_INF)
Ben Segall51f21762014-05-19 15:49:45 -07004695 goto out_deactivate;
Paul Turner58088ad2011-07-21 09:43:31 -07004696
Paul Turner671fd9d2011-07-21 09:43:34 -07004697 throttled = !list_empty(&cfs_b->throttled_cfs_rq);
Nikhil Raoe8da1b12011-07-21 09:43:40 -07004698 cfs_b->nr_periods += overrun;
Paul Turner671fd9d2011-07-21 09:43:34 -07004699
Ben Segall51f21762014-05-19 15:49:45 -07004700 /*
4701 * idle depends on !throttled (for the case of a large deficit), and if
4702 * we're going inactive then everything else can be deferred
4703 */
4704 if (cfs_b->idle && !throttled)
4705 goto out_deactivate;
Paul Turnera9cf55b2011-07-21 09:43:32 -07004706
4707 __refill_cfs_bandwidth_runtime(cfs_b);
4708
Paul Turner671fd9d2011-07-21 09:43:34 -07004709 if (!throttled) {
4710 /* mark as potentially idle for the upcoming period */
4711 cfs_b->idle = 1;
Ben Segall51f21762014-05-19 15:49:45 -07004712 return 0;
Paul Turner671fd9d2011-07-21 09:43:34 -07004713 }
Paul Turner58088ad2011-07-21 09:43:31 -07004714
Nikhil Raoe8da1b12011-07-21 09:43:40 -07004715 /* account preceding periods in which throttling occurred */
4716 cfs_b->nr_throttled += overrun;
4717
Paul Turner671fd9d2011-07-21 09:43:34 -07004718 runtime_expires = cfs_b->runtime_expires;
Paul Turner671fd9d2011-07-21 09:43:34 -07004719
4720 /*
Ben Segallc06f04c2014-06-20 15:21:20 -07004721 * This check is repeated as we are holding onto the new bandwidth while
4722 * we unthrottle. This can potentially race with an unthrottled group
4723 * trying to acquire new bandwidth from the global pool. This can result
4724 * in us over-using our runtime if it is all used during this loop, but
4725 * only by limited amounts in that extreme case.
Paul Turner671fd9d2011-07-21 09:43:34 -07004726 */
Ben Segallc06f04c2014-06-20 15:21:20 -07004727 while (throttled && cfs_b->runtime > 0) {
4728 runtime = cfs_b->runtime;
Paul Turner671fd9d2011-07-21 09:43:34 -07004729 raw_spin_unlock(&cfs_b->lock);
4730 /* we can't nest cfs_b->lock while distributing bandwidth */
4731 runtime = distribute_cfs_runtime(cfs_b, runtime,
4732 runtime_expires);
4733 raw_spin_lock(&cfs_b->lock);
4734
4735 throttled = !list_empty(&cfs_b->throttled_cfs_rq);
Ben Segallc06f04c2014-06-20 15:21:20 -07004736
4737 cfs_b->runtime -= min(runtime, cfs_b->runtime);
Paul Turner671fd9d2011-07-21 09:43:34 -07004738 }
4739
Paul Turner671fd9d2011-07-21 09:43:34 -07004740 /*
4741 * While we are ensured activity in the period following an
4742 * unthrottle, this also covers the case in which the new bandwidth is
4743 * insufficient to cover the existing bandwidth deficit. (Forcing the
4744 * timer to remain active while there are any throttled entities.)
4745 */
4746 cfs_b->idle = 0;
Paul Turner58088ad2011-07-21 09:43:31 -07004747
Ben Segall51f21762014-05-19 15:49:45 -07004748 return 0;
4749
4750out_deactivate:
Ben Segall51f21762014-05-19 15:49:45 -07004751 return 1;
Paul Turner58088ad2011-07-21 09:43:31 -07004752}
Paul Turnerd3d9dc32011-07-21 09:43:39 -07004753
Paul Turnerd8b49862011-07-21 09:43:41 -07004754/* a cfs_rq won't donate quota below this amount */
4755static const u64 min_cfs_rq_runtime = 1 * NSEC_PER_MSEC;
4756/* minimum remaining period time to redistribute slack quota */
4757static const u64 min_bandwidth_expiration = 2 * NSEC_PER_MSEC;
4758/* how long we wait to gather additional slack before distributing */
4759static const u64 cfs_bandwidth_slack_period = 5 * NSEC_PER_MSEC;
4760
Ben Segalldb06e782013-10-16 11:16:17 -07004761/*
4762 * Are we near the end of the current quota period?
4763 *
4764 * Requires cfs_b->lock for hrtimer_expires_remaining to be safe against the
Thomas Gleixner4961b6e2015-04-14 21:09:05 +00004765 * hrtimer base being cleared by hrtimer_start. In the case of
Ben Segalldb06e782013-10-16 11:16:17 -07004766 * migrate_hrtimers, base is never cleared, so we are fine.
4767 */
Paul Turnerd8b49862011-07-21 09:43:41 -07004768static int runtime_refresh_within(struct cfs_bandwidth *cfs_b, u64 min_expire)
4769{
4770 struct hrtimer *refresh_timer = &cfs_b->period_timer;
4771 u64 remaining;
4772
4773 /* if the call-back is running a quota refresh is already occurring */
4774 if (hrtimer_callback_running(refresh_timer))
4775 return 1;
4776
4777 /* is a quota refresh about to occur? */
4778 remaining = ktime_to_ns(hrtimer_expires_remaining(refresh_timer));
4779 if (remaining < min_expire)
4780 return 1;
4781
4782 return 0;
4783}
4784
4785static void start_cfs_slack_bandwidth(struct cfs_bandwidth *cfs_b)
4786{
4787 u64 min_left = cfs_bandwidth_slack_period + min_bandwidth_expiration;
4788
4789 /* if there's a quota refresh soon don't bother with slack */
4790 if (runtime_refresh_within(cfs_b, min_left))
4791 return;
4792
Peter Zijlstra4cfafd32015-05-14 12:23:11 +02004793 hrtimer_start(&cfs_b->slack_timer,
4794 ns_to_ktime(cfs_bandwidth_slack_period),
4795 HRTIMER_MODE_REL);
Paul Turnerd8b49862011-07-21 09:43:41 -07004796}
4797
4798/* we know any runtime found here is valid as update_curr() precedes return */
4799static void __return_cfs_rq_runtime(struct cfs_rq *cfs_rq)
4800{
4801 struct cfs_bandwidth *cfs_b = tg_cfs_bandwidth(cfs_rq->tg);
4802 s64 slack_runtime = cfs_rq->runtime_remaining - min_cfs_rq_runtime;
4803
4804 if (slack_runtime <= 0)
4805 return;
4806
4807 raw_spin_lock(&cfs_b->lock);
4808 if (cfs_b->quota != RUNTIME_INF &&
4809 cfs_rq->runtime_expires == cfs_b->runtime_expires) {
4810 cfs_b->runtime += slack_runtime;
4811
4812 /* we are under rq->lock, defer unthrottling using a timer */
4813 if (cfs_b->runtime > sched_cfs_bandwidth_slice() &&
4814 !list_empty(&cfs_b->throttled_cfs_rq))
4815 start_cfs_slack_bandwidth(cfs_b);
4816 }
4817 raw_spin_unlock(&cfs_b->lock);
4818
4819 /* even if it's not valid for return we don't want to try again */
4820 cfs_rq->runtime_remaining -= slack_runtime;
4821}
4822
4823static __always_inline void return_cfs_rq_runtime(struct cfs_rq *cfs_rq)
4824{
Paul Turner56f570e2011-11-07 20:26:33 -08004825 if (!cfs_bandwidth_used())
4826 return;
4827
Paul Turnerfccfdc62011-11-07 20:26:34 -08004828 if (!cfs_rq->runtime_enabled || cfs_rq->nr_running)
Paul Turnerd8b49862011-07-21 09:43:41 -07004829 return;
4830
4831 __return_cfs_rq_runtime(cfs_rq);
4832}
4833
4834/*
4835 * This is done with a timer (instead of inline with bandwidth return) since
4836 * it's necessary to juggle rq->locks to unthrottle their respective cfs_rqs.
4837 */
4838static void do_sched_cfs_slack_timer(struct cfs_bandwidth *cfs_b)
4839{
4840 u64 runtime = 0, slice = sched_cfs_bandwidth_slice();
4841 u64 expires;
4842
4843 /* confirm we're still not at a refresh boundary */
Paul Turnerd8b49862011-07-21 09:43:41 -07004844 raw_spin_lock(&cfs_b->lock);
Ben Segalldb06e782013-10-16 11:16:17 -07004845 if (runtime_refresh_within(cfs_b, min_bandwidth_expiration)) {
4846 raw_spin_unlock(&cfs_b->lock);
4847 return;
4848 }
4849
Ben Segallc06f04c2014-06-20 15:21:20 -07004850 if (cfs_b->quota != RUNTIME_INF && cfs_b->runtime > slice)
Paul Turnerd8b49862011-07-21 09:43:41 -07004851 runtime = cfs_b->runtime;
Ben Segallc06f04c2014-06-20 15:21:20 -07004852
Paul Turnerd8b49862011-07-21 09:43:41 -07004853 expires = cfs_b->runtime_expires;
4854 raw_spin_unlock(&cfs_b->lock);
4855
4856 if (!runtime)
4857 return;
4858
4859 runtime = distribute_cfs_runtime(cfs_b, runtime, expires);
4860
4861 raw_spin_lock(&cfs_b->lock);
4862 if (expires == cfs_b->runtime_expires)
Ben Segallc06f04c2014-06-20 15:21:20 -07004863 cfs_b->runtime -= min(runtime, cfs_b->runtime);
Paul Turnerd8b49862011-07-21 09:43:41 -07004864 raw_spin_unlock(&cfs_b->lock);
4865}
4866
Paul Turnerd3d9dc32011-07-21 09:43:39 -07004867/*
4868 * When a group wakes up we want to make sure that its quota is not already
4869 * expired/exceeded, otherwise it may be allowed to steal additional ticks of
4870 * runtime as update_curr() throttling can not not trigger until it's on-rq.
4871 */
4872static void check_enqueue_throttle(struct cfs_rq *cfs_rq)
4873{
Paul Turner56f570e2011-11-07 20:26:33 -08004874 if (!cfs_bandwidth_used())
4875 return;
4876
Paul Turnerd3d9dc32011-07-21 09:43:39 -07004877 /* an active group must be handled by the update_curr()->put() path */
4878 if (!cfs_rq->runtime_enabled || cfs_rq->curr)
4879 return;
4880
4881 /* ensure the group is not already throttled */
4882 if (cfs_rq_throttled(cfs_rq))
4883 return;
4884
4885 /* update runtime allocation */
4886 account_cfs_rq_runtime(cfs_rq, 0);
4887 if (cfs_rq->runtime_remaining <= 0)
4888 throttle_cfs_rq(cfs_rq);
4889}
4890
Peter Zijlstra55e16d32016-06-22 15:14:26 +02004891static void sync_throttle(struct task_group *tg, int cpu)
4892{
4893 struct cfs_rq *pcfs_rq, *cfs_rq;
4894
4895 if (!cfs_bandwidth_used())
4896 return;
4897
4898 if (!tg->parent)
4899 return;
4900
4901 cfs_rq = tg->cfs_rq[cpu];
4902 pcfs_rq = tg->parent->cfs_rq[cpu];
4903
4904 cfs_rq->throttle_count = pcfs_rq->throttle_count;
Xunlei Pangb8922122016-07-09 15:54:22 +08004905 cfs_rq->throttled_clock_task = rq_clock_task(cpu_rq(cpu));
Peter Zijlstra55e16d32016-06-22 15:14:26 +02004906}
4907
Paul Turnerd3d9dc32011-07-21 09:43:39 -07004908/* conditionally throttle active cfs_rq's from put_prev_entity() */
Peter Zijlstra678d5712012-02-11 06:05:00 +01004909static bool check_cfs_rq_runtime(struct cfs_rq *cfs_rq)
Paul Turnerd3d9dc32011-07-21 09:43:39 -07004910{
Paul Turner56f570e2011-11-07 20:26:33 -08004911 if (!cfs_bandwidth_used())
Peter Zijlstra678d5712012-02-11 06:05:00 +01004912 return false;
Paul Turner56f570e2011-11-07 20:26:33 -08004913
Paul Turnerd3d9dc32011-07-21 09:43:39 -07004914 if (likely(!cfs_rq->runtime_enabled || cfs_rq->runtime_remaining > 0))
Peter Zijlstra678d5712012-02-11 06:05:00 +01004915 return false;
Paul Turnerd3d9dc32011-07-21 09:43:39 -07004916
4917 /*
4918 * it's possible for a throttled entity to be forced into a running
4919 * state (e.g. set_curr_task), in this case we're finished.
4920 */
4921 if (cfs_rq_throttled(cfs_rq))
Peter Zijlstra678d5712012-02-11 06:05:00 +01004922 return true;
Paul Turnerd3d9dc32011-07-21 09:43:39 -07004923
4924 throttle_cfs_rq(cfs_rq);
Peter Zijlstra678d5712012-02-11 06:05:00 +01004925 return true;
Paul Turnerd3d9dc32011-07-21 09:43:39 -07004926}
Peter Zijlstra029632f2011-10-25 10:00:11 +02004927
Peter Zijlstra029632f2011-10-25 10:00:11 +02004928static enum hrtimer_restart sched_cfs_slack_timer(struct hrtimer *timer)
4929{
4930 struct cfs_bandwidth *cfs_b =
4931 container_of(timer, struct cfs_bandwidth, slack_timer);
Peter Zijlstra77a4d1a2015-04-15 11:41:57 +02004932
Peter Zijlstra029632f2011-10-25 10:00:11 +02004933 do_sched_cfs_slack_timer(cfs_b);
4934
4935 return HRTIMER_NORESTART;
4936}
4937
4938static enum hrtimer_restart sched_cfs_period_timer(struct hrtimer *timer)
4939{
4940 struct cfs_bandwidth *cfs_b =
4941 container_of(timer, struct cfs_bandwidth, period_timer);
Peter Zijlstra029632f2011-10-25 10:00:11 +02004942 int overrun;
4943 int idle = 0;
4944
Ben Segall51f21762014-05-19 15:49:45 -07004945 raw_spin_lock(&cfs_b->lock);
Peter Zijlstra029632f2011-10-25 10:00:11 +02004946 for (;;) {
Peter Zijlstra77a4d1a2015-04-15 11:41:57 +02004947 overrun = hrtimer_forward_now(timer, cfs_b->period);
Peter Zijlstra029632f2011-10-25 10:00:11 +02004948 if (!overrun)
4949 break;
4950
4951 idle = do_sched_cfs_period_timer(cfs_b, overrun);
4952 }
Peter Zijlstra4cfafd32015-05-14 12:23:11 +02004953 if (idle)
4954 cfs_b->period_active = 0;
Ben Segall51f21762014-05-19 15:49:45 -07004955 raw_spin_unlock(&cfs_b->lock);
Peter Zijlstra029632f2011-10-25 10:00:11 +02004956
4957 return idle ? HRTIMER_NORESTART : HRTIMER_RESTART;
4958}
4959
4960void init_cfs_bandwidth(struct cfs_bandwidth *cfs_b)
4961{
4962 raw_spin_lock_init(&cfs_b->lock);
4963 cfs_b->runtime = 0;
4964 cfs_b->quota = RUNTIME_INF;
4965 cfs_b->period = ns_to_ktime(default_cfs_period());
4966
4967 INIT_LIST_HEAD(&cfs_b->throttled_cfs_rq);
Peter Zijlstra4cfafd32015-05-14 12:23:11 +02004968 hrtimer_init(&cfs_b->period_timer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS_PINNED);
Peter Zijlstra029632f2011-10-25 10:00:11 +02004969 cfs_b->period_timer.function = sched_cfs_period_timer;
4970 hrtimer_init(&cfs_b->slack_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
4971 cfs_b->slack_timer.function = sched_cfs_slack_timer;
4972}
4973
4974static void init_cfs_rq_runtime(struct cfs_rq *cfs_rq)
4975{
4976 cfs_rq->runtime_enabled = 0;
4977 INIT_LIST_HEAD(&cfs_rq->throttled_list);
4978}
4979
Peter Zijlstra77a4d1a2015-04-15 11:41:57 +02004980void start_cfs_bandwidth(struct cfs_bandwidth *cfs_b)
Peter Zijlstra029632f2011-10-25 10:00:11 +02004981{
Peter Zijlstra4cfafd32015-05-14 12:23:11 +02004982 lockdep_assert_held(&cfs_b->lock);
Peter Zijlstra029632f2011-10-25 10:00:11 +02004983
Peter Zijlstra4cfafd32015-05-14 12:23:11 +02004984 if (!cfs_b->period_active) {
4985 cfs_b->period_active = 1;
4986 hrtimer_forward_now(&cfs_b->period_timer, cfs_b->period);
4987 hrtimer_start_expires(&cfs_b->period_timer, HRTIMER_MODE_ABS_PINNED);
4988 }
Peter Zijlstra029632f2011-10-25 10:00:11 +02004989}
4990
4991static void destroy_cfs_bandwidth(struct cfs_bandwidth *cfs_b)
4992{
Tetsuo Handa7f1a1692014-12-25 15:51:21 +09004993 /* init_cfs_bandwidth() was not called */
4994 if (!cfs_b->throttled_cfs_rq.next)
4995 return;
4996
Peter Zijlstra029632f2011-10-25 10:00:11 +02004997 hrtimer_cancel(&cfs_b->period_timer);
4998 hrtimer_cancel(&cfs_b->slack_timer);
4999}
5000
Peter Zijlstra502ce002017-05-04 15:31:22 +02005001/*
5002 * Both these cpu hotplug callbacks race against unregister_fair_sched_group()
5003 *
5004 * The race is harmless, since modifying bandwidth settings of unhooked group
5005 * bits doesn't do much.
5006 */
5007
5008/* cpu online calback */
Kirill Tkhai0e59bda2014-06-25 12:19:42 +04005009static void __maybe_unused update_runtime_enabled(struct rq *rq)
5010{
Peter Zijlstra502ce002017-05-04 15:31:22 +02005011 struct task_group *tg;
Kirill Tkhai0e59bda2014-06-25 12:19:42 +04005012
Peter Zijlstra502ce002017-05-04 15:31:22 +02005013 lockdep_assert_held(&rq->lock);
5014
5015 rcu_read_lock();
5016 list_for_each_entry_rcu(tg, &task_groups, list) {
5017 struct cfs_bandwidth *cfs_b = &tg->cfs_bandwidth;
5018 struct cfs_rq *cfs_rq = tg->cfs_rq[cpu_of(rq)];
Kirill Tkhai0e59bda2014-06-25 12:19:42 +04005019
5020 raw_spin_lock(&cfs_b->lock);
5021 cfs_rq->runtime_enabled = cfs_b->quota != RUNTIME_INF;
5022 raw_spin_unlock(&cfs_b->lock);
5023 }
Peter Zijlstra502ce002017-05-04 15:31:22 +02005024 rcu_read_unlock();
Kirill Tkhai0e59bda2014-06-25 12:19:42 +04005025}
5026
Peter Zijlstra502ce002017-05-04 15:31:22 +02005027/* cpu offline callback */
Arnd Bergmann38dc3342013-01-25 14:14:22 +00005028static void __maybe_unused unthrottle_offline_cfs_rqs(struct rq *rq)
Peter Zijlstra029632f2011-10-25 10:00:11 +02005029{
Peter Zijlstra502ce002017-05-04 15:31:22 +02005030 struct task_group *tg;
Peter Zijlstra029632f2011-10-25 10:00:11 +02005031
Peter Zijlstra502ce002017-05-04 15:31:22 +02005032 lockdep_assert_held(&rq->lock);
5033
5034 rcu_read_lock();
5035 list_for_each_entry_rcu(tg, &task_groups, list) {
5036 struct cfs_rq *cfs_rq = tg->cfs_rq[cpu_of(rq)];
5037
Peter Zijlstra029632f2011-10-25 10:00:11 +02005038 if (!cfs_rq->runtime_enabled)
5039 continue;
5040
5041 /*
5042 * clock_task is not advancing so we just need to make sure
5043 * there's some valid quota amount
5044 */
Ben Segall51f21762014-05-19 15:49:45 -07005045 cfs_rq->runtime_remaining = 1;
Kirill Tkhai0e59bda2014-06-25 12:19:42 +04005046 /*
5047 * Offline rq is schedulable till cpu is completely disabled
5048 * in take_cpu_down(), so we prevent new cfs throttling here.
5049 */
5050 cfs_rq->runtime_enabled = 0;
5051
Peter Zijlstra029632f2011-10-25 10:00:11 +02005052 if (cfs_rq_throttled(cfs_rq))
5053 unthrottle_cfs_rq(cfs_rq);
5054 }
Peter Zijlstra502ce002017-05-04 15:31:22 +02005055 rcu_read_unlock();
Peter Zijlstra029632f2011-10-25 10:00:11 +02005056}
5057
5058#else /* CONFIG_CFS_BANDWIDTH */
Paul Turnerf1b17282012-10-04 13:18:31 +02005059static inline u64 cfs_rq_clock_task(struct cfs_rq *cfs_rq)
5060{
Frederic Weisbecker78becc22013-04-12 01:51:02 +02005061 return rq_clock_task(rq_of(cfs_rq));
Paul Turnerf1b17282012-10-04 13:18:31 +02005062}
5063
Peter Zijlstra9dbdb152013-11-18 18:27:06 +01005064static void account_cfs_rq_runtime(struct cfs_rq *cfs_rq, u64 delta_exec) {}
Peter Zijlstra678d5712012-02-11 06:05:00 +01005065static bool check_cfs_rq_runtime(struct cfs_rq *cfs_rq) { return false; }
Paul Turnerd3d9dc32011-07-21 09:43:39 -07005066static void check_enqueue_throttle(struct cfs_rq *cfs_rq) {}
Peter Zijlstra55e16d32016-06-22 15:14:26 +02005067static inline void sync_throttle(struct task_group *tg, int cpu) {}
Peter Zijlstra6c16a6d2012-03-21 13:07:16 -07005068static __always_inline void return_cfs_rq_runtime(struct cfs_rq *cfs_rq) {}
Paul Turner85dac902011-07-21 09:43:33 -07005069
5070static inline int cfs_rq_throttled(struct cfs_rq *cfs_rq)
5071{
5072 return 0;
5073}
Paul Turner64660c82011-07-21 09:43:36 -07005074
5075static inline int throttled_hierarchy(struct cfs_rq *cfs_rq)
5076{
5077 return 0;
5078}
5079
5080static inline int throttled_lb_pair(struct task_group *tg,
5081 int src_cpu, int dest_cpu)
5082{
5083 return 0;
5084}
Peter Zijlstra029632f2011-10-25 10:00:11 +02005085
5086void init_cfs_bandwidth(struct cfs_bandwidth *cfs_b) {}
5087
5088#ifdef CONFIG_FAIR_GROUP_SCHED
5089static void init_cfs_rq_runtime(struct cfs_rq *cfs_rq) {}
Paul Turnerab84d312011-07-21 09:43:28 -07005090#endif
5091
Peter Zijlstra029632f2011-10-25 10:00:11 +02005092static inline struct cfs_bandwidth *tg_cfs_bandwidth(struct task_group *tg)
5093{
5094 return NULL;
5095}
5096static inline void destroy_cfs_bandwidth(struct cfs_bandwidth *cfs_b) {}
Kirill Tkhai0e59bda2014-06-25 12:19:42 +04005097static inline void update_runtime_enabled(struct rq *rq) {}
Peter Boonstoppela4c96ae2012-08-09 15:34:47 -07005098static inline void unthrottle_offline_cfs_rqs(struct rq *rq) {}
Peter Zijlstra029632f2011-10-25 10:00:11 +02005099
5100#endif /* CONFIG_CFS_BANDWIDTH */
5101
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02005102/**************************************************
5103 * CFS operations on tasks:
5104 */
5105
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01005106#ifdef CONFIG_SCHED_HRTICK
5107static void hrtick_start_fair(struct rq *rq, struct task_struct *p)
5108{
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01005109 struct sched_entity *se = &p->se;
5110 struct cfs_rq *cfs_rq = cfs_rq_of(se);
5111
Peter Zijlstra9148a3a2016-09-20 22:34:51 +02005112 SCHED_WARN_ON(task_rq(p) != rq);
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01005113
Srivatsa Vaddagiri8bf46a32016-09-16 18:28:51 -07005114 if (rq->cfs.h_nr_running > 1) {
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01005115 u64 slice = sched_slice(cfs_rq, se);
5116 u64 ran = se->sum_exec_runtime - se->prev_sum_exec_runtime;
5117 s64 delta = slice - ran;
5118
5119 if (delta < 0) {
5120 if (rq->curr == p)
Kirill Tkhai88751252014-06-29 00:03:57 +04005121 resched_curr(rq);
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01005122 return;
5123 }
Peter Zijlstra31656512008-07-18 18:01:23 +02005124 hrtick_start(rq, delta);
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01005125 }
5126}
Peter Zijlstraa4c2f002008-10-17 19:27:03 +02005127
5128/*
5129 * called from enqueue/dequeue and updates the hrtick when the
5130 * current task is from our class and nr_running is low enough
5131 * to matter.
5132 */
5133static void hrtick_update(struct rq *rq)
5134{
5135 struct task_struct *curr = rq->curr;
5136
Mike Galbraithb39e66e2011-11-22 15:20:07 +01005137 if (!hrtick_enabled(rq) || curr->sched_class != &fair_sched_class)
Peter Zijlstraa4c2f002008-10-17 19:27:03 +02005138 return;
5139
5140 if (cfs_rq_of(&curr->se)->nr_running < sched_nr_latency)
5141 hrtick_start_fair(rq, curr);
5142}
Dhaval Giani55e12e52008-06-24 23:39:43 +05305143#else /* !CONFIG_SCHED_HRTICK */
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01005144static inline void
5145hrtick_start_fair(struct rq *rq, struct task_struct *p)
5146{
5147}
Peter Zijlstraa4c2f002008-10-17 19:27:03 +02005148
5149static inline void hrtick_update(struct rq *rq)
5150{
5151}
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01005152#endif
5153
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02005154/*
5155 * The enqueue_task method is called before nr_running is
5156 * increased. Here we update the fair scheduling stats and
5157 * then put the task into the rbtree:
5158 */
Thomas Gleixnerea87bb72010-01-20 20:58:57 +00005159static void
Peter Zijlstra371fd7e2010-03-24 16:38:48 +01005160enqueue_task_fair(struct rq *rq, struct task_struct *p, int flags)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02005161{
5162 struct cfs_rq *cfs_rq;
Peter Zijlstra62fb1852008-02-25 17:34:02 +01005163 struct sched_entity *se = &p->se;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02005164
Rafael J. Wysocki8c34ab12016-09-09 23:59:33 +02005165 /*
5166 * If in_iowait is set, the code below may not trigger any cpufreq
5167 * utilization updates, so do it here explicitly with the IOWAIT flag
5168 * passed.
5169 */
5170 if (p->in_iowait)
Viresh Kumar674e7542017-07-28 12:16:38 +05305171 cpufreq_update_util(rq, SCHED_CPUFREQ_IOWAIT);
Rafael J. Wysocki8c34ab12016-09-09 23:59:33 +02005172
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02005173 for_each_sched_entity(se) {
Peter Zijlstra62fb1852008-02-25 17:34:02 +01005174 if (se->on_rq)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02005175 break;
5176 cfs_rq = cfs_rq_of(se);
Peter Zijlstra88ec22d2009-12-16 18:04:41 +01005177 enqueue_entity(cfs_rq, se, flags);
Paul Turner85dac902011-07-21 09:43:33 -07005178
5179 /*
5180 * end evaluation on encountering a throttled cfs_rq
5181 *
5182 * note: in the case of encountering a throttled cfs_rq we will
5183 * post the final h_nr_running increment below.
Peter Zijlstrae210bff2016-06-16 18:51:48 +02005184 */
Paul Turner85dac902011-07-21 09:43:33 -07005185 if (cfs_rq_throttled(cfs_rq))
5186 break;
Paul Turner953bfcd2011-07-21 09:43:27 -07005187 cfs_rq->h_nr_running++;
Paul Turner85dac902011-07-21 09:43:33 -07005188
Peter Zijlstra88ec22d2009-12-16 18:04:41 +01005189 flags = ENQUEUE_WAKEUP;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02005190 }
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01005191
Peter Zijlstra2069dd72010-11-15 15:47:00 -08005192 for_each_sched_entity(se) {
Lin Ming0f317142011-07-22 09:14:31 +08005193 cfs_rq = cfs_rq_of(se);
Paul Turner953bfcd2011-07-21 09:43:27 -07005194 cfs_rq->h_nr_running++;
Peter Zijlstra2069dd72010-11-15 15:47:00 -08005195
Paul Turner85dac902011-07-21 09:43:33 -07005196 if (cfs_rq_throttled(cfs_rq))
5197 break;
5198
Peter Zijlstra88c06162017-05-06 17:32:43 +02005199 update_load_avg(cfs_rq, se, UPDATE_TG);
Peter Zijlstra1ea6c462017-05-06 15:59:54 +02005200 update_cfs_group(se);
Peter Zijlstra2069dd72010-11-15 15:47:00 -08005201 }
5202
Yuyang Ducd126af2015-07-15 08:04:36 +08005203 if (!se)
Kirill Tkhai72465442014-05-09 03:00:14 +04005204 add_nr_running(rq, 1);
Yuyang Ducd126af2015-07-15 08:04:36 +08005205
Peter Zijlstraa4c2f002008-10-17 19:27:03 +02005206 hrtick_update(rq);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02005207}
5208
Venkatesh Pallipadi2f368252011-04-14 10:30:53 -07005209static void set_next_buddy(struct sched_entity *se);
5210
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02005211/*
5212 * The dequeue_task method is called before nr_running is
5213 * decreased. We remove the task from the rbtree and
5214 * update the fair scheduling stats:
5215 */
Peter Zijlstra371fd7e2010-03-24 16:38:48 +01005216static void dequeue_task_fair(struct rq *rq, struct task_struct *p, int flags)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02005217{
5218 struct cfs_rq *cfs_rq;
Peter Zijlstra62fb1852008-02-25 17:34:02 +01005219 struct sched_entity *se = &p->se;
Venkatesh Pallipadi2f368252011-04-14 10:30:53 -07005220 int task_sleep = flags & DEQUEUE_SLEEP;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02005221
5222 for_each_sched_entity(se) {
5223 cfs_rq = cfs_rq_of(se);
Peter Zijlstra371fd7e2010-03-24 16:38:48 +01005224 dequeue_entity(cfs_rq, se, flags);
Paul Turner85dac902011-07-21 09:43:33 -07005225
5226 /*
5227 * end evaluation on encountering a throttled cfs_rq
5228 *
5229 * note: in the case of encountering a throttled cfs_rq we will
5230 * post the final h_nr_running decrement below.
5231 */
5232 if (cfs_rq_throttled(cfs_rq))
5233 break;
Paul Turner953bfcd2011-07-21 09:43:27 -07005234 cfs_rq->h_nr_running--;
Peter Zijlstra2069dd72010-11-15 15:47:00 -08005235
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02005236 /* Don't dequeue parent if it has other entities besides us */
Venkatesh Pallipadi2f368252011-04-14 10:30:53 -07005237 if (cfs_rq->load.weight) {
Konstantin Khlebnikov754bd592016-06-16 15:57:15 +03005238 /* Avoid re-evaluating load for this entity: */
5239 se = parent_entity(se);
Venkatesh Pallipadi2f368252011-04-14 10:30:53 -07005240 /*
5241 * Bias pick_next to pick a task from this cfs_rq, as
5242 * p is sleeping when it is within its sched_slice.
5243 */
Konstantin Khlebnikov754bd592016-06-16 15:57:15 +03005244 if (task_sleep && se && !throttled_hierarchy(cfs_rq))
5245 set_next_buddy(se);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02005246 break;
Venkatesh Pallipadi2f368252011-04-14 10:30:53 -07005247 }
Peter Zijlstra371fd7e2010-03-24 16:38:48 +01005248 flags |= DEQUEUE_SLEEP;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02005249 }
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01005250
Peter Zijlstra2069dd72010-11-15 15:47:00 -08005251 for_each_sched_entity(se) {
Lin Ming0f317142011-07-22 09:14:31 +08005252 cfs_rq = cfs_rq_of(se);
Paul Turner953bfcd2011-07-21 09:43:27 -07005253 cfs_rq->h_nr_running--;
Peter Zijlstra2069dd72010-11-15 15:47:00 -08005254
Paul Turner85dac902011-07-21 09:43:33 -07005255 if (cfs_rq_throttled(cfs_rq))
5256 break;
5257
Peter Zijlstra88c06162017-05-06 17:32:43 +02005258 update_load_avg(cfs_rq, se, UPDATE_TG);
Peter Zijlstra1ea6c462017-05-06 15:59:54 +02005259 update_cfs_group(se);
Peter Zijlstra2069dd72010-11-15 15:47:00 -08005260 }
5261
Yuyang Ducd126af2015-07-15 08:04:36 +08005262 if (!se)
Kirill Tkhai72465442014-05-09 03:00:14 +04005263 sub_nr_running(rq, 1);
Yuyang Ducd126af2015-07-15 08:04:36 +08005264
Peter Zijlstraa4c2f002008-10-17 19:27:03 +02005265 hrtick_update(rq);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02005266}
5267
Gregory Haskinse7693a32008-01-25 21:08:09 +01005268#ifdef CONFIG_SMP
Peter Zijlstra10e2f1a2016-05-09 10:38:05 +02005269
5270/* Working cpumask for: load_balance, load_balance_newidle. */
5271DEFINE_PER_CPU(cpumask_var_t, load_balance_mask);
5272DEFINE_PER_CPU(cpumask_var_t, select_idle_mask);
5273
Frederic Weisbecker9fd81dd2016-04-19 17:36:51 +02005274#ifdef CONFIG_NO_HZ_COMMON
Peter Zijlstra3289bdb2015-04-14 13:19:42 +02005275/*
5276 * per rq 'load' arrray crap; XXX kill this.
5277 */
5278
5279/*
Peter Zijlstrad937cdc2015-10-19 13:49:30 +02005280 * The exact cpuload calculated at every tick would be:
Peter Zijlstra3289bdb2015-04-14 13:19:42 +02005281 *
Peter Zijlstrad937cdc2015-10-19 13:49:30 +02005282 * load' = (1 - 1/2^i) * load + (1/2^i) * cur_load
5283 *
5284 * If a cpu misses updates for n ticks (as it was idle) and update gets
5285 * called on the n+1-th tick when cpu may be busy, then we have:
5286 *
5287 * load_n = (1 - 1/2^i)^n * load_0
5288 * load_n+1 = (1 - 1/2^i) * load_n + (1/2^i) * cur_load
Peter Zijlstra3289bdb2015-04-14 13:19:42 +02005289 *
5290 * decay_load_missed() below does efficient calculation of
Peter Zijlstrad937cdc2015-10-19 13:49:30 +02005291 *
5292 * load' = (1 - 1/2^i)^n * load
5293 *
5294 * Because x^(n+m) := x^n * x^m we can decompose any x^n in power-of-2 factors.
5295 * This allows us to precompute the above in said factors, thereby allowing the
5296 * reduction of an arbitrary n in O(log_2 n) steps. (See also
5297 * fixed_power_int())
Peter Zijlstra3289bdb2015-04-14 13:19:42 +02005298 *
5299 * The calculation is approximated on a 128 point scale.
Peter Zijlstra3289bdb2015-04-14 13:19:42 +02005300 */
5301#define DEGRADE_SHIFT 7
Peter Zijlstrad937cdc2015-10-19 13:49:30 +02005302
5303static const u8 degrade_zero_ticks[CPU_LOAD_IDX_MAX] = {0, 8, 32, 64, 128};
5304static const u8 degrade_factor[CPU_LOAD_IDX_MAX][DEGRADE_SHIFT + 1] = {
5305 { 0, 0, 0, 0, 0, 0, 0, 0 },
5306 { 64, 32, 8, 0, 0, 0, 0, 0 },
5307 { 96, 72, 40, 12, 1, 0, 0, 0 },
5308 { 112, 98, 75, 43, 15, 1, 0, 0 },
5309 { 120, 112, 98, 76, 45, 16, 2, 0 }
5310};
Peter Zijlstra3289bdb2015-04-14 13:19:42 +02005311
5312/*
5313 * Update cpu_load for any missed ticks, due to tickless idle. The backlog
5314 * would be when CPU is idle and so we just decay the old load without
5315 * adding any new load.
5316 */
5317static unsigned long
5318decay_load_missed(unsigned long load, unsigned long missed_updates, int idx)
5319{
5320 int j = 0;
5321
5322 if (!missed_updates)
5323 return load;
5324
5325 if (missed_updates >= degrade_zero_ticks[idx])
5326 return 0;
5327
5328 if (idx == 1)
5329 return load >> missed_updates;
5330
5331 while (missed_updates) {
5332 if (missed_updates % 2)
5333 load = (load * degrade_factor[idx][j]) >> DEGRADE_SHIFT;
5334
5335 missed_updates >>= 1;
5336 j++;
5337 }
5338 return load;
5339}
Frederic Weisbecker9fd81dd2016-04-19 17:36:51 +02005340#endif /* CONFIG_NO_HZ_COMMON */
Peter Zijlstra3289bdb2015-04-14 13:19:42 +02005341
Byungchul Park59543272015-10-14 18:47:35 +09005342/**
Frederic Weisbeckercee1afc2016-04-13 15:56:50 +02005343 * __cpu_load_update - update the rq->cpu_load[] statistics
Byungchul Park59543272015-10-14 18:47:35 +09005344 * @this_rq: The rq to update statistics for
5345 * @this_load: The current load
5346 * @pending_updates: The number of missed updates
Byungchul Park59543272015-10-14 18:47:35 +09005347 *
Peter Zijlstra3289bdb2015-04-14 13:19:42 +02005348 * Update rq->cpu_load[] statistics. This function is usually called every
Byungchul Park59543272015-10-14 18:47:35 +09005349 * scheduler tick (TICK_NSEC).
5350 *
5351 * This function computes a decaying average:
5352 *
5353 * load[i]' = (1 - 1/2^i) * load[i] + (1/2^i) * load
5354 *
5355 * Because of NOHZ it might not get called on every tick which gives need for
5356 * the @pending_updates argument.
5357 *
5358 * load[i]_n = (1 - 1/2^i) * load[i]_n-1 + (1/2^i) * load_n-1
5359 * = A * load[i]_n-1 + B ; A := (1 - 1/2^i), B := (1/2^i) * load
5360 * = A * (A * load[i]_n-2 + B) + B
5361 * = A * (A * (A * load[i]_n-3 + B) + B) + B
5362 * = A^3 * load[i]_n-3 + (A^2 + A + 1) * B
5363 * = A^n * load[i]_0 + (A^(n-1) + A^(n-2) + ... + 1) * B
5364 * = A^n * load[i]_0 + ((1 - A^n) / (1 - A)) * B
5365 * = (1 - 1/2^i)^n * (load[i]_0 - load) + load
5366 *
5367 * In the above we've assumed load_n := load, which is true for NOHZ_FULL as
5368 * any change in load would have resulted in the tick being turned back on.
5369 *
5370 * For regular NOHZ, this reduces to:
5371 *
5372 * load[i]_n = (1 - 1/2^i)^n * load[i]_0
5373 *
5374 * see decay_load_misses(). For NOHZ_FULL we get to subtract and add the extra
Frederic Weisbecker1f419062016-04-13 15:56:51 +02005375 * term.
Peter Zijlstra3289bdb2015-04-14 13:19:42 +02005376 */
Frederic Weisbecker1f419062016-04-13 15:56:51 +02005377static void cpu_load_update(struct rq *this_rq, unsigned long this_load,
5378 unsigned long pending_updates)
Peter Zijlstra3289bdb2015-04-14 13:19:42 +02005379{
Frederic Weisbecker9fd81dd2016-04-19 17:36:51 +02005380 unsigned long __maybe_unused tickless_load = this_rq->cpu_load[0];
Peter Zijlstra3289bdb2015-04-14 13:19:42 +02005381 int i, scale;
5382
5383 this_rq->nr_load_updates++;
5384
5385 /* Update our load: */
5386 this_rq->cpu_load[0] = this_load; /* Fasttrack for idx 0 */
5387 for (i = 1, scale = 2; i < CPU_LOAD_IDX_MAX; i++, scale += scale) {
5388 unsigned long old_load, new_load;
5389
5390 /* scale is effectively 1 << i now, and >> i divides by scale */
5391
Byungchul Park7400d3b2016-01-15 16:07:49 +09005392 old_load = this_rq->cpu_load[i];
Frederic Weisbecker9fd81dd2016-04-19 17:36:51 +02005393#ifdef CONFIG_NO_HZ_COMMON
Peter Zijlstra3289bdb2015-04-14 13:19:42 +02005394 old_load = decay_load_missed(old_load, pending_updates - 1, i);
Byungchul Park7400d3b2016-01-15 16:07:49 +09005395 if (tickless_load) {
5396 old_load -= decay_load_missed(tickless_load, pending_updates - 1, i);
5397 /*
5398 * old_load can never be a negative value because a
5399 * decayed tickless_load cannot be greater than the
5400 * original tickless_load.
5401 */
5402 old_load += tickless_load;
5403 }
Frederic Weisbecker9fd81dd2016-04-19 17:36:51 +02005404#endif
Peter Zijlstra3289bdb2015-04-14 13:19:42 +02005405 new_load = this_load;
5406 /*
5407 * Round up the averaging division if load is increasing. This
5408 * prevents us from getting stuck on 9 if the load is 10, for
5409 * example.
5410 */
5411 if (new_load > old_load)
5412 new_load += scale - 1;
5413
5414 this_rq->cpu_load[i] = (old_load * (scale - 1) + new_load) >> i;
5415 }
5416
5417 sched_avg_update(this_rq);
5418}
5419
Yuyang Du7ea241a2015-07-15 08:04:42 +08005420/* Used instead of source_load when we know the type == 0 */
Viresh Kumarc7132dd2017-05-24 10:59:54 +05305421static unsigned long weighted_cpuload(struct rq *rq)
Yuyang Du7ea241a2015-07-15 08:04:42 +08005422{
Viresh Kumarc7132dd2017-05-24 10:59:54 +05305423 return cfs_rq_runnable_load_avg(&rq->cfs);
Yuyang Du7ea241a2015-07-15 08:04:42 +08005424}
5425
Peter Zijlstra3289bdb2015-04-14 13:19:42 +02005426#ifdef CONFIG_NO_HZ_COMMON
Frederic Weisbecker1f419062016-04-13 15:56:51 +02005427/*
5428 * There is no sane way to deal with nohz on smp when using jiffies because the
5429 * cpu doing the jiffies update might drift wrt the cpu doing the jiffy reading
5430 * causing off-by-one errors in observed deltas; {0,2} instead of {1,1}.
5431 *
5432 * Therefore we need to avoid the delta approach from the regular tick when
5433 * possible since that would seriously skew the load calculation. This is why we
5434 * use cpu_load_update_periodic() for CPUs out of nohz. However we'll rely on
5435 * jiffies deltas for updates happening while in nohz mode (idle ticks, idle
5436 * loop exit, nohz_idle_balance, nohz full exit...)
5437 *
5438 * This means we might still be one tick off for nohz periods.
5439 */
5440
5441static void cpu_load_update_nohz(struct rq *this_rq,
5442 unsigned long curr_jiffies,
5443 unsigned long load)
Frederic Weisbeckerbe68a682016-01-13 17:01:29 +01005444{
5445 unsigned long pending_updates;
5446
5447 pending_updates = curr_jiffies - this_rq->last_load_update_tick;
5448 if (pending_updates) {
5449 this_rq->last_load_update_tick = curr_jiffies;
5450 /*
5451 * In the regular NOHZ case, we were idle, this means load 0.
5452 * In the NOHZ_FULL case, we were non-idle, we should consider
5453 * its weighted load.
5454 */
Frederic Weisbecker1f419062016-04-13 15:56:51 +02005455 cpu_load_update(this_rq, load, pending_updates);
Frederic Weisbeckerbe68a682016-01-13 17:01:29 +01005456 }
5457}
5458
Peter Zijlstra3289bdb2015-04-14 13:19:42 +02005459/*
Peter Zijlstra3289bdb2015-04-14 13:19:42 +02005460 * Called from nohz_idle_balance() to update the load ratings before doing the
5461 * idle balance.
5462 */
Frederic Weisbeckercee1afc2016-04-13 15:56:50 +02005463static void cpu_load_update_idle(struct rq *this_rq)
Peter Zijlstra3289bdb2015-04-14 13:19:42 +02005464{
Peter Zijlstra3289bdb2015-04-14 13:19:42 +02005465 /*
5466 * bail if there's load or we're actually up-to-date.
5467 */
Viresh Kumarc7132dd2017-05-24 10:59:54 +05305468 if (weighted_cpuload(this_rq))
Peter Zijlstra3289bdb2015-04-14 13:19:42 +02005469 return;
5470
Frederic Weisbecker1f419062016-04-13 15:56:51 +02005471 cpu_load_update_nohz(this_rq, READ_ONCE(jiffies), 0);
Peter Zijlstra3289bdb2015-04-14 13:19:42 +02005472}
5473
5474/*
Frederic Weisbecker1f419062016-04-13 15:56:51 +02005475 * Record CPU load on nohz entry so we know the tickless load to account
5476 * on nohz exit. cpu_load[0] happens then to be updated more frequently
5477 * than other cpu_load[idx] but it should be fine as cpu_load readers
5478 * shouldn't rely into synchronized cpu_load[*] updates.
Peter Zijlstra3289bdb2015-04-14 13:19:42 +02005479 */
Frederic Weisbecker1f419062016-04-13 15:56:51 +02005480void cpu_load_update_nohz_start(void)
Peter Zijlstra3289bdb2015-04-14 13:19:42 +02005481{
5482 struct rq *this_rq = this_rq();
Frederic Weisbecker1f419062016-04-13 15:56:51 +02005483
5484 /*
5485 * This is all lockless but should be fine. If weighted_cpuload changes
5486 * concurrently we'll exit nohz. And cpu_load write can race with
5487 * cpu_load_update_idle() but both updater would be writing the same.
5488 */
Viresh Kumarc7132dd2017-05-24 10:59:54 +05305489 this_rq->cpu_load[0] = weighted_cpuload(this_rq);
Frederic Weisbecker1f419062016-04-13 15:56:51 +02005490}
5491
5492/*
5493 * Account the tickless load in the end of a nohz frame.
5494 */
5495void cpu_load_update_nohz_stop(void)
5496{
Jason Low316c1608d2015-04-28 13:00:20 -07005497 unsigned long curr_jiffies = READ_ONCE(jiffies);
Frederic Weisbecker1f419062016-04-13 15:56:51 +02005498 struct rq *this_rq = this_rq();
5499 unsigned long load;
Peter Zijlstra8a8c69c2016-10-04 16:04:35 +02005500 struct rq_flags rf;
Peter Zijlstra3289bdb2015-04-14 13:19:42 +02005501
5502 if (curr_jiffies == this_rq->last_load_update_tick)
5503 return;
5504
Viresh Kumarc7132dd2017-05-24 10:59:54 +05305505 load = weighted_cpuload(this_rq);
Peter Zijlstra8a8c69c2016-10-04 16:04:35 +02005506 rq_lock(this_rq, &rf);
Matt Flemingb52fad22016-05-03 20:46:54 +01005507 update_rq_clock(this_rq);
Frederic Weisbecker1f419062016-04-13 15:56:51 +02005508 cpu_load_update_nohz(this_rq, curr_jiffies, load);
Peter Zijlstra8a8c69c2016-10-04 16:04:35 +02005509 rq_unlock(this_rq, &rf);
Peter Zijlstra3289bdb2015-04-14 13:19:42 +02005510}
Frederic Weisbecker1f419062016-04-13 15:56:51 +02005511#else /* !CONFIG_NO_HZ_COMMON */
5512static inline void cpu_load_update_nohz(struct rq *this_rq,
5513 unsigned long curr_jiffies,
5514 unsigned long load) { }
5515#endif /* CONFIG_NO_HZ_COMMON */
5516
5517static void cpu_load_update_periodic(struct rq *this_rq, unsigned long load)
5518{
Frederic Weisbecker9fd81dd2016-04-19 17:36:51 +02005519#ifdef CONFIG_NO_HZ_COMMON
Frederic Weisbecker1f419062016-04-13 15:56:51 +02005520 /* See the mess around cpu_load_update_nohz(). */
5521 this_rq->last_load_update_tick = READ_ONCE(jiffies);
Frederic Weisbecker9fd81dd2016-04-19 17:36:51 +02005522#endif
Frederic Weisbecker1f419062016-04-13 15:56:51 +02005523 cpu_load_update(this_rq, load, 1);
5524}
Peter Zijlstra3289bdb2015-04-14 13:19:42 +02005525
5526/*
5527 * Called from scheduler_tick()
5528 */
Frederic Weisbeckercee1afc2016-04-13 15:56:50 +02005529void cpu_load_update_active(struct rq *this_rq)
Peter Zijlstra3289bdb2015-04-14 13:19:42 +02005530{
Viresh Kumarc7132dd2017-05-24 10:59:54 +05305531 unsigned long load = weighted_cpuload(this_rq);
Frederic Weisbecker1f419062016-04-13 15:56:51 +02005532
5533 if (tick_nohz_tick_stopped())
5534 cpu_load_update_nohz(this_rq, READ_ONCE(jiffies), load);
5535 else
5536 cpu_load_update_periodic(this_rq, load);
Peter Zijlstra3289bdb2015-04-14 13:19:42 +02005537}
5538
Peter Zijlstra029632f2011-10-25 10:00:11 +02005539/*
5540 * Return a low guess at the load of a migration-source cpu weighted
5541 * according to the scheduling class and "nice" value.
5542 *
5543 * We want to under-estimate the load of migration sources, to
5544 * balance conservatively.
5545 */
5546static unsigned long source_load(int cpu, int type)
5547{
5548 struct rq *rq = cpu_rq(cpu);
Viresh Kumarc7132dd2017-05-24 10:59:54 +05305549 unsigned long total = weighted_cpuload(rq);
Peter Zijlstra029632f2011-10-25 10:00:11 +02005550
5551 if (type == 0 || !sched_feat(LB_BIAS))
5552 return total;
5553
5554 return min(rq->cpu_load[type-1], total);
5555}
5556
5557/*
5558 * Return a high guess at the load of a migration-target cpu weighted
5559 * according to the scheduling class and "nice" value.
5560 */
5561static unsigned long target_load(int cpu, int type)
5562{
5563 struct rq *rq = cpu_rq(cpu);
Viresh Kumarc7132dd2017-05-24 10:59:54 +05305564 unsigned long total = weighted_cpuload(rq);
Peter Zijlstra029632f2011-10-25 10:00:11 +02005565
5566 if (type == 0 || !sched_feat(LB_BIAS))
5567 return total;
5568
5569 return max(rq->cpu_load[type-1], total);
5570}
5571
Nicolas Pitreced549f2014-05-26 18:19:38 -04005572static unsigned long capacity_of(int cpu)
Peter Zijlstra029632f2011-10-25 10:00:11 +02005573{
Nicolas Pitreced549f2014-05-26 18:19:38 -04005574 return cpu_rq(cpu)->cpu_capacity;
Peter Zijlstra029632f2011-10-25 10:00:11 +02005575}
5576
Vincent Guittotca6d75e2015-02-27 16:54:09 +01005577static unsigned long capacity_orig_of(int cpu)
5578{
5579 return cpu_rq(cpu)->cpu_capacity_orig;
5580}
5581
Peter Zijlstra029632f2011-10-25 10:00:11 +02005582static unsigned long cpu_avg_load_per_task(int cpu)
5583{
5584 struct rq *rq = cpu_rq(cpu);
Jason Low316c1608d2015-04-28 13:00:20 -07005585 unsigned long nr_running = READ_ONCE(rq->cfs.h_nr_running);
Viresh Kumarc7132dd2017-05-24 10:59:54 +05305586 unsigned long load_avg = weighted_cpuload(rq);
Peter Zijlstra029632f2011-10-25 10:00:11 +02005587
5588 if (nr_running)
Alex Shib92486c2013-06-20 10:18:50 +08005589 return load_avg / nr_running;
Peter Zijlstra029632f2011-10-25 10:00:11 +02005590
5591 return 0;
5592}
5593
Peter Zijlstrac58d25f2016-05-12 09:19:59 +02005594static void record_wakee(struct task_struct *p)
5595{
5596 /*
5597 * Only decay a single time; tasks that have less then 1 wakeup per
5598 * jiffy will not have built up many flips.
5599 */
5600 if (time_after(jiffies, current->wakee_flip_decay_ts + HZ)) {
5601 current->wakee_flips >>= 1;
5602 current->wakee_flip_decay_ts = jiffies;
5603 }
5604
5605 if (current->last_wakee != p) {
5606 current->last_wakee = p;
5607 current->wakee_flips++;
5608 }
5609}
5610
Mike Galbraith63b0e9e2015-07-14 17:39:50 +02005611/*
5612 * Detect M:N waker/wakee relationships via a switching-frequency heuristic.
Peter Zijlstrac58d25f2016-05-12 09:19:59 +02005613 *
Mike Galbraith63b0e9e2015-07-14 17:39:50 +02005614 * A waker of many should wake a different task than the one last awakened
Peter Zijlstrac58d25f2016-05-12 09:19:59 +02005615 * at a frequency roughly N times higher than one of its wakees.
5616 *
5617 * In order to determine whether we should let the load spread vs consolidating
5618 * to shared cache, we look for a minimum 'flip' frequency of llc_size in one
5619 * partner, and a factor of lls_size higher frequency in the other.
5620 *
5621 * With both conditions met, we can be relatively sure that the relationship is
5622 * non-monogamous, with partner count exceeding socket size.
5623 *
5624 * Waker/wakee being client/server, worker/dispatcher, interrupt source or
5625 * whatever is irrelevant, spread criteria is apparent partner count exceeds
5626 * socket size.
Mike Galbraith63b0e9e2015-07-14 17:39:50 +02005627 */
Michael Wang62470412013-07-04 12:55:51 +08005628static int wake_wide(struct task_struct *p)
5629{
Mike Galbraith63b0e9e2015-07-14 17:39:50 +02005630 unsigned int master = current->wakee_flips;
5631 unsigned int slave = p->wakee_flips;
Peter Zijlstra7d9ffa82013-07-04 12:56:46 +08005632 int factor = this_cpu_read(sd_llc_size);
Michael Wang62470412013-07-04 12:55:51 +08005633
Mike Galbraith63b0e9e2015-07-14 17:39:50 +02005634 if (master < slave)
5635 swap(master, slave);
5636 if (slave < factor || master < slave * factor)
5637 return 0;
5638 return 1;
Michael Wang62470412013-07-04 12:55:51 +08005639}
5640
Peter Zijlstra90001d62017-07-31 17:50:05 +02005641/*
Peter Zijlstrad153b152017-09-27 11:35:30 +02005642 * The purpose of wake_affine() is to quickly determine on which CPU we can run
5643 * soonest. For the purpose of speed we only consider the waking and previous
5644 * CPU.
Peter Zijlstra90001d62017-07-31 17:50:05 +02005645 *
Peter Zijlstrad153b152017-09-27 11:35:30 +02005646 * wake_affine_idle() - only considers 'now', it check if the waking CPU is (or
5647 * will be) idle.
Peter Zijlstraf2cdd9c2017-10-06 09:23:24 +02005648 *
5649 * wake_affine_weight() - considers the weight to reflect the average
5650 * scheduling latency of the CPUs. This seems to work
5651 * for the overloaded case.
Peter Zijlstra90001d62017-07-31 17:50:05 +02005652 */
Peter Zijlstra90001d62017-07-31 17:50:05 +02005653
Peter Zijlstra90001d62017-07-31 17:50:05 +02005654static bool
Peter Zijlstrad153b152017-09-27 11:35:30 +02005655wake_affine_idle(struct sched_domain *sd, struct task_struct *p,
5656 int this_cpu, int prev_cpu, int sync)
Peter Zijlstra90001d62017-07-31 17:50:05 +02005657{
Peter Zijlstrad153b152017-09-27 11:35:30 +02005658 if (idle_cpu(this_cpu))
Peter Zijlstra90001d62017-07-31 17:50:05 +02005659 return true;
Peter Zijlstra90001d62017-07-31 17:50:05 +02005660
Peter Zijlstrad153b152017-09-27 11:35:30 +02005661 if (sync && cpu_rq(this_cpu)->nr_running == 1)
5662 return true;
Peter Zijlstra90001d62017-07-31 17:50:05 +02005663
Peter Zijlstrad153b152017-09-27 11:35:30 +02005664 return false;
Peter Zijlstra90001d62017-07-31 17:50:05 +02005665}
5666
Peter Zijlstra90001d62017-07-31 17:50:05 +02005667static bool
Peter Zijlstraf2cdd9c2017-10-06 09:23:24 +02005668wake_affine_weight(struct sched_domain *sd, struct task_struct *p,
5669 int this_cpu, int prev_cpu, int sync)
Peter Zijlstra90001d62017-07-31 17:50:05 +02005670{
Peter Zijlstra90001d62017-07-31 17:50:05 +02005671 s64 this_eff_load, prev_eff_load;
5672 unsigned long task_load;
5673
Peter Zijlstraf2cdd9c2017-10-06 09:23:24 +02005674 this_eff_load = target_load(this_cpu, sd->wake_idx);
5675 prev_eff_load = source_load(prev_cpu, sd->wake_idx);
Peter Zijlstra90001d62017-07-31 17:50:05 +02005676
Peter Zijlstra90001d62017-07-31 17:50:05 +02005677 if (sync) {
5678 unsigned long current_load = task_h_load(current);
5679
Peter Zijlstraf2cdd9c2017-10-06 09:23:24 +02005680 if (current_load > this_eff_load)
Peter Zijlstra90001d62017-07-31 17:50:05 +02005681 return true;
5682
Peter Zijlstraf2cdd9c2017-10-06 09:23:24 +02005683 this_eff_load -= current_load;
Peter Zijlstra90001d62017-07-31 17:50:05 +02005684 }
5685
Peter Zijlstra90001d62017-07-31 17:50:05 +02005686 task_load = task_h_load(p);
5687
Peter Zijlstraf2cdd9c2017-10-06 09:23:24 +02005688 this_eff_load += task_load;
5689 if (sched_feat(WA_BIAS))
5690 this_eff_load *= 100;
5691 this_eff_load *= capacity_of(prev_cpu);
Peter Zijlstra90001d62017-07-31 17:50:05 +02005692
Peter Zijlstraf2cdd9c2017-10-06 09:23:24 +02005693 prev_eff_load -= task_load;
5694 if (sched_feat(WA_BIAS))
5695 prev_eff_load *= 100 + (sd->imbalance_pct - 100) / 2;
5696 prev_eff_load *= capacity_of(this_cpu);
Peter Zijlstra90001d62017-07-31 17:50:05 +02005697
5698 return this_eff_load <= prev_eff_load;
5699}
5700
Morten Rasmussen772bd008c2016-06-22 18:03:13 +01005701static int wake_affine(struct sched_domain *sd, struct task_struct *p,
5702 int prev_cpu, int sync)
Ingo Molnar098fb9d2008-03-16 20:36:10 +01005703{
Rik van Riel3fed3822017-06-23 12:55:29 -04005704 int this_cpu = smp_processor_id();
Peter Zijlstrad153b152017-09-27 11:35:30 +02005705 bool affine = false;
Ingo Molnar098fb9d2008-03-16 20:36:10 +01005706
Peter Zijlstrad153b152017-09-27 11:35:30 +02005707 if (sched_feat(WA_IDLE) && !affine)
5708 affine = wake_affine_idle(sd, p, this_cpu, prev_cpu, sync);
Peter Zijlstra90001d62017-07-31 17:50:05 +02005709
Peter Zijlstraf2cdd9c2017-10-06 09:23:24 +02005710 if (sched_feat(WA_WEIGHT) && !affine)
5711 affine = wake_affine_weight(sd, p, this_cpu, prev_cpu, sync);
Mike Galbraithb3137bc2008-05-29 11:11:41 +02005712
Josh Poimboeufae928822016-06-17 12:43:24 -05005713 schedstat_inc(p->se.statistics.nr_wakeups_affine_attempts);
Rik van Riel3fed3822017-06-23 12:55:29 -04005714 if (affine) {
5715 schedstat_inc(sd->ttwu_move_affine);
5716 schedstat_inc(p->se.statistics.nr_wakeups_affine);
5717 }
Mike Galbraithb3137bc2008-05-29 11:11:41 +02005718
Rik van Riel3fed3822017-06-23 12:55:29 -04005719 return affine;
Ingo Molnar098fb9d2008-03-16 20:36:10 +01005720}
5721
Morten Rasmussen6a0b19c2016-10-14 14:41:08 +01005722static inline int task_util(struct task_struct *p);
5723static int cpu_util_wake(int cpu, struct task_struct *p);
5724
5725static unsigned long capacity_spare_wake(int cpu, struct task_struct *p)
5726{
5727 return capacity_orig_of(cpu) - cpu_util_wake(cpu, p);
5728}
5729
Peter Zijlstraaaee1202009-09-10 13:36:25 +02005730/*
5731 * find_idlest_group finds and returns the least busy CPU group within the
5732 * domain.
5733 */
5734static struct sched_group *
Peter Zijlstra78e7ed52009-09-03 13:16:51 +02005735find_idlest_group(struct sched_domain *sd, struct task_struct *p,
Vincent Guittotc44f2a02013-10-18 13:52:21 +02005736 int this_cpu, int sd_flag)
Gregory Haskinse7693a32008-01-25 21:08:09 +01005737{
Andi Kleenb3bd3de2010-08-10 14:17:51 -07005738 struct sched_group *idlest = NULL, *group = sd->groups;
Morten Rasmussen6a0b19c2016-10-14 14:41:08 +01005739 struct sched_group *most_spare_sg = NULL;
Vincent Guittot6b947802016-12-08 17:56:54 +01005740 unsigned long min_runnable_load = ULONG_MAX, this_runnable_load = 0;
5741 unsigned long min_avg_load = ULONG_MAX, this_avg_load = 0;
Morten Rasmussen6a0b19c2016-10-14 14:41:08 +01005742 unsigned long most_spare = 0, this_spare = 0;
Vincent Guittotc44f2a02013-10-18 13:52:21 +02005743 int load_idx = sd->forkexec_idx;
Vincent Guittot6b947802016-12-08 17:56:54 +01005744 int imbalance_scale = 100 + (sd->imbalance_pct-100)/2;
5745 unsigned long imbalance = scale_load_down(NICE_0_LOAD) *
5746 (sd->imbalance_pct-100) / 100;
Gregory Haskinse7693a32008-01-25 21:08:09 +01005747
Vincent Guittotc44f2a02013-10-18 13:52:21 +02005748 if (sd_flag & SD_BALANCE_WAKE)
5749 load_idx = sd->wake_idx;
5750
Peter Zijlstraaaee1202009-09-10 13:36:25 +02005751 do {
Vincent Guittot6b947802016-12-08 17:56:54 +01005752 unsigned long load, avg_load, runnable_load;
5753 unsigned long spare_cap, max_spare_cap;
Peter Zijlstraaaee1202009-09-10 13:36:25 +02005754 int local_group;
5755 int i;
Gregory Haskinse7693a32008-01-25 21:08:09 +01005756
Peter Zijlstraaaee1202009-09-10 13:36:25 +02005757 /* Skip over this group if it has no CPUs allowed */
Peter Zijlstraae4df9d2017-05-01 11:03:12 +02005758 if (!cpumask_intersects(sched_group_span(group),
Ingo Molnar0c98d342017-02-05 15:38:10 +01005759 &p->cpus_allowed))
Peter Zijlstraaaee1202009-09-10 13:36:25 +02005760 continue;
5761
5762 local_group = cpumask_test_cpu(this_cpu,
Peter Zijlstraae4df9d2017-05-01 11:03:12 +02005763 sched_group_span(group));
Peter Zijlstraaaee1202009-09-10 13:36:25 +02005764
Morten Rasmussen6a0b19c2016-10-14 14:41:08 +01005765 /*
5766 * Tally up the load of all CPUs in the group and find
5767 * the group containing the CPU with most spare capacity.
5768 */
Peter Zijlstraaaee1202009-09-10 13:36:25 +02005769 avg_load = 0;
Vincent Guittot6b947802016-12-08 17:56:54 +01005770 runnable_load = 0;
Morten Rasmussen6a0b19c2016-10-14 14:41:08 +01005771 max_spare_cap = 0;
Peter Zijlstraaaee1202009-09-10 13:36:25 +02005772
Peter Zijlstraae4df9d2017-05-01 11:03:12 +02005773 for_each_cpu(i, sched_group_span(group)) {
Peter Zijlstraaaee1202009-09-10 13:36:25 +02005774 /* Bias balancing toward cpus of our domain */
5775 if (local_group)
5776 load = source_load(i, load_idx);
5777 else
5778 load = target_load(i, load_idx);
5779
Vincent Guittot6b947802016-12-08 17:56:54 +01005780 runnable_load += load;
5781
5782 avg_load += cfs_rq_load_avg(&cpu_rq(i)->cfs);
Morten Rasmussen6a0b19c2016-10-14 14:41:08 +01005783
5784 spare_cap = capacity_spare_wake(i, p);
5785
5786 if (spare_cap > max_spare_cap)
5787 max_spare_cap = spare_cap;
Peter Zijlstraaaee1202009-09-10 13:36:25 +02005788 }
5789
Nicolas Pitre63b2ca32014-05-26 18:19:37 -04005790 /* Adjust by relative CPU capacity of the group */
Vincent Guittot6b947802016-12-08 17:56:54 +01005791 avg_load = (avg_load * SCHED_CAPACITY_SCALE) /
5792 group->sgc->capacity;
5793 runnable_load = (runnable_load * SCHED_CAPACITY_SCALE) /
5794 group->sgc->capacity;
Peter Zijlstraaaee1202009-09-10 13:36:25 +02005795
5796 if (local_group) {
Vincent Guittot6b947802016-12-08 17:56:54 +01005797 this_runnable_load = runnable_load;
5798 this_avg_load = avg_load;
Morten Rasmussen6a0b19c2016-10-14 14:41:08 +01005799 this_spare = max_spare_cap;
5800 } else {
Vincent Guittot6b947802016-12-08 17:56:54 +01005801 if (min_runnable_load > (runnable_load + imbalance)) {
5802 /*
5803 * The runnable load is significantly smaller
5804 * so we can pick this new cpu
5805 */
5806 min_runnable_load = runnable_load;
5807 min_avg_load = avg_load;
5808 idlest = group;
5809 } else if ((runnable_load < (min_runnable_load + imbalance)) &&
5810 (100*min_avg_load > imbalance_scale*avg_load)) {
5811 /*
5812 * The runnable loads are close so take the
5813 * blocked load into account through avg_load.
5814 */
5815 min_avg_load = avg_load;
Morten Rasmussen6a0b19c2016-10-14 14:41:08 +01005816 idlest = group;
5817 }
5818
5819 if (most_spare < max_spare_cap) {
5820 most_spare = max_spare_cap;
5821 most_spare_sg = group;
5822 }
Peter Zijlstraaaee1202009-09-10 13:36:25 +02005823 }
5824 } while (group = group->next, group != sd->groups);
5825
Morten Rasmussen6a0b19c2016-10-14 14:41:08 +01005826 /*
5827 * The cross-over point between using spare capacity or least load
5828 * is too conservative for high utilization tasks on partially
5829 * utilized systems if we require spare_capacity > task_util(p),
5830 * so we allow for some task stuffing by using
5831 * spare_capacity > task_util(p)/2.
Vincent Guittotf519a3f2016-12-08 17:56:53 +01005832 *
5833 * Spare capacity can't be used for fork because the utilization has
5834 * not been set yet, we must first select a rq to compute the initial
5835 * utilization.
Morten Rasmussen6a0b19c2016-10-14 14:41:08 +01005836 */
Vincent Guittotf519a3f2016-12-08 17:56:53 +01005837 if (sd_flag & SD_BALANCE_FORK)
5838 goto skip_spare;
5839
Morten Rasmussen6a0b19c2016-10-14 14:41:08 +01005840 if (this_spare > task_util(p) / 2 &&
Vincent Guittot6b947802016-12-08 17:56:54 +01005841 imbalance_scale*this_spare > 100*most_spare)
Morten Rasmussen6a0b19c2016-10-14 14:41:08 +01005842 return NULL;
Vincent Guittot6b947802016-12-08 17:56:54 +01005843
5844 if (most_spare > task_util(p) / 2)
Morten Rasmussen6a0b19c2016-10-14 14:41:08 +01005845 return most_spare_sg;
5846
Vincent Guittotf519a3f2016-12-08 17:56:53 +01005847skip_spare:
Vincent Guittot6b947802016-12-08 17:56:54 +01005848 if (!idlest)
Peter Zijlstraaaee1202009-09-10 13:36:25 +02005849 return NULL;
Vincent Guittot6b947802016-12-08 17:56:54 +01005850
5851 if (min_runnable_load > (this_runnable_load + imbalance))
5852 return NULL;
5853
5854 if ((this_runnable_load < (min_runnable_load + imbalance)) &&
5855 (100*this_avg_load < imbalance_scale*min_avg_load))
5856 return NULL;
5857
Peter Zijlstraaaee1202009-09-10 13:36:25 +02005858 return idlest;
5859}
5860
5861/*
5862 * find_idlest_cpu - find the idlest cpu among the cpus in group.
5863 */
5864static int
5865find_idlest_cpu(struct sched_group *group, struct task_struct *p, int this_cpu)
5866{
5867 unsigned long load, min_load = ULONG_MAX;
Nicolas Pitre83a0a962014-09-04 11:32:10 -04005868 unsigned int min_exit_latency = UINT_MAX;
5869 u64 latest_idle_timestamp = 0;
5870 int least_loaded_cpu = this_cpu;
5871 int shallowest_idle_cpu = -1;
Peter Zijlstraaaee1202009-09-10 13:36:25 +02005872 int i;
5873
Morten Rasmusseneaecf412016-06-22 18:03:14 +01005874 /* Check if we have any choice: */
5875 if (group->group_weight == 1)
Peter Zijlstraae4df9d2017-05-01 11:03:12 +02005876 return cpumask_first(sched_group_span(group));
Morten Rasmusseneaecf412016-06-22 18:03:14 +01005877
Peter Zijlstraaaee1202009-09-10 13:36:25 +02005878 /* Traverse only the allowed CPUs */
Peter Zijlstraae4df9d2017-05-01 11:03:12 +02005879 for_each_cpu_and(i, sched_group_span(group), &p->cpus_allowed) {
Nicolas Pitre83a0a962014-09-04 11:32:10 -04005880 if (idle_cpu(i)) {
5881 struct rq *rq = cpu_rq(i);
5882 struct cpuidle_state *idle = idle_get_state(rq);
5883 if (idle && idle->exit_latency < min_exit_latency) {
5884 /*
5885 * We give priority to a CPU whose idle state
5886 * has the smallest exit latency irrespective
5887 * of any idle timestamp.
5888 */
5889 min_exit_latency = idle->exit_latency;
5890 latest_idle_timestamp = rq->idle_stamp;
5891 shallowest_idle_cpu = i;
5892 } else if ((!idle || idle->exit_latency == min_exit_latency) &&
5893 rq->idle_stamp > latest_idle_timestamp) {
5894 /*
5895 * If equal or no active idle state, then
5896 * the most recently idled CPU might have
5897 * a warmer cache.
5898 */
5899 latest_idle_timestamp = rq->idle_stamp;
5900 shallowest_idle_cpu = i;
5901 }
Yao Dongdong9f967422014-10-28 04:08:06 +00005902 } else if (shallowest_idle_cpu == -1) {
Viresh Kumarc7132dd2017-05-24 10:59:54 +05305903 load = weighted_cpuload(cpu_rq(i));
Nicolas Pitre83a0a962014-09-04 11:32:10 -04005904 if (load < min_load || (load == min_load && i == this_cpu)) {
5905 min_load = load;
5906 least_loaded_cpu = i;
5907 }
Gregory Haskinse7693a32008-01-25 21:08:09 +01005908 }
5909 }
5910
Nicolas Pitre83a0a962014-09-04 11:32:10 -04005911 return shallowest_idle_cpu != -1 ? shallowest_idle_cpu : least_loaded_cpu;
Peter Zijlstraaaee1202009-09-10 13:36:25 +02005912}
Gregory Haskinse7693a32008-01-25 21:08:09 +01005913
Peter Zijlstra10e2f1a2016-05-09 10:38:05 +02005914#ifdef CONFIG_SCHED_SMT
5915
5916static inline void set_idle_cores(int cpu, int val)
5917{
5918 struct sched_domain_shared *sds;
5919
5920 sds = rcu_dereference(per_cpu(sd_llc_shared, cpu));
5921 if (sds)
5922 WRITE_ONCE(sds->has_idle_cores, val);
5923}
5924
5925static inline bool test_idle_cores(int cpu, bool def)
5926{
5927 struct sched_domain_shared *sds;
5928
5929 sds = rcu_dereference(per_cpu(sd_llc_shared, cpu));
5930 if (sds)
5931 return READ_ONCE(sds->has_idle_cores);
5932
5933 return def;
5934}
5935
5936/*
5937 * Scans the local SMT mask to see if the entire core is idle, and records this
5938 * information in sd_llc_shared->has_idle_cores.
5939 *
5940 * Since SMT siblings share all cache levels, inspecting this limited remote
5941 * state should be fairly cheap.
5942 */
Peter Zijlstra1b568f02016-05-09 10:38:41 +02005943void __update_idle_core(struct rq *rq)
Peter Zijlstra10e2f1a2016-05-09 10:38:05 +02005944{
5945 int core = cpu_of(rq);
5946 int cpu;
5947
5948 rcu_read_lock();
5949 if (test_idle_cores(core, true))
5950 goto unlock;
5951
5952 for_each_cpu(cpu, cpu_smt_mask(core)) {
5953 if (cpu == core)
5954 continue;
5955
5956 if (!idle_cpu(cpu))
5957 goto unlock;
5958 }
5959
5960 set_idle_cores(core, 1);
5961unlock:
5962 rcu_read_unlock();
5963}
5964
5965/*
5966 * Scan the entire LLC domain for idle cores; this dynamically switches off if
5967 * there are no idle cores left in the system; tracked through
5968 * sd_llc->shared->has_idle_cores and enabled through update_idle_core() above.
5969 */
5970static int select_idle_core(struct task_struct *p, struct sched_domain *sd, int target)
5971{
5972 struct cpumask *cpus = this_cpu_cpumask_var_ptr(select_idle_mask);
Peter Zijlstrac743f0a2017-04-14 14:20:05 +02005973 int core, cpu;
Peter Zijlstra10e2f1a2016-05-09 10:38:05 +02005974
Peter Zijlstra1b568f02016-05-09 10:38:41 +02005975 if (!static_branch_likely(&sched_smt_present))
5976 return -1;
5977
Peter Zijlstra10e2f1a2016-05-09 10:38:05 +02005978 if (!test_idle_cores(target, false))
5979 return -1;
5980
Ingo Molnar0c98d342017-02-05 15:38:10 +01005981 cpumask_and(cpus, sched_domain_span(sd), &p->cpus_allowed);
Peter Zijlstra10e2f1a2016-05-09 10:38:05 +02005982
Peter Zijlstrac743f0a2017-04-14 14:20:05 +02005983 for_each_cpu_wrap(core, cpus, target) {
Peter Zijlstra10e2f1a2016-05-09 10:38:05 +02005984 bool idle = true;
5985
5986 for_each_cpu(cpu, cpu_smt_mask(core)) {
5987 cpumask_clear_cpu(cpu, cpus);
5988 if (!idle_cpu(cpu))
5989 idle = false;
5990 }
5991
5992 if (idle)
5993 return core;
5994 }
5995
5996 /*
5997 * Failed to find an idle core; stop looking for one.
5998 */
5999 set_idle_cores(target, 0);
6000
6001 return -1;
6002}
6003
6004/*
6005 * Scan the local SMT mask for idle CPUs.
6006 */
6007static int select_idle_smt(struct task_struct *p, struct sched_domain *sd, int target)
6008{
6009 int cpu;
6010
Peter Zijlstra1b568f02016-05-09 10:38:41 +02006011 if (!static_branch_likely(&sched_smt_present))
6012 return -1;
6013
Peter Zijlstra10e2f1a2016-05-09 10:38:05 +02006014 for_each_cpu(cpu, cpu_smt_mask(target)) {
Ingo Molnar0c98d342017-02-05 15:38:10 +01006015 if (!cpumask_test_cpu(cpu, &p->cpus_allowed))
Peter Zijlstra10e2f1a2016-05-09 10:38:05 +02006016 continue;
6017 if (idle_cpu(cpu))
6018 return cpu;
6019 }
6020
6021 return -1;
6022}
6023
6024#else /* CONFIG_SCHED_SMT */
6025
6026static inline int select_idle_core(struct task_struct *p, struct sched_domain *sd, int target)
6027{
6028 return -1;
6029}
6030
6031static inline int select_idle_smt(struct task_struct *p, struct sched_domain *sd, int target)
6032{
6033 return -1;
6034}
6035
6036#endif /* CONFIG_SCHED_SMT */
6037
6038/*
6039 * Scan the LLC domain for idle CPUs; this is dynamically regulated by
6040 * comparing the average scan cost (tracked in sd->avg_scan_cost) against the
6041 * average idle time for this rq (as found in rq->avg_idle).
6042 */
6043static int select_idle_cpu(struct task_struct *p, struct sched_domain *sd, int target)
6044{
Wanpeng Li9cfb38a2016-10-09 08:04:03 +08006045 struct sched_domain *this_sd;
Peter Zijlstra1ad3aaf2017-05-17 12:53:50 +02006046 u64 avg_cost, avg_idle;
Peter Zijlstra10e2f1a2016-05-09 10:38:05 +02006047 u64 time, cost;
6048 s64 delta;
Peter Zijlstra1ad3aaf2017-05-17 12:53:50 +02006049 int cpu, nr = INT_MAX;
Peter Zijlstra10e2f1a2016-05-09 10:38:05 +02006050
Wanpeng Li9cfb38a2016-10-09 08:04:03 +08006051 this_sd = rcu_dereference(*this_cpu_ptr(&sd_llc));
6052 if (!this_sd)
6053 return -1;
6054
Peter Zijlstra10e2f1a2016-05-09 10:38:05 +02006055 /*
6056 * Due to large variance we need a large fuzz factor; hackbench in
6057 * particularly is sensitive here.
6058 */
Peter Zijlstra1ad3aaf2017-05-17 12:53:50 +02006059 avg_idle = this_rq()->avg_idle / 512;
6060 avg_cost = this_sd->avg_scan_cost + 1;
6061
6062 if (sched_feat(SIS_AVG_CPU) && avg_idle < avg_cost)
Peter Zijlstra10e2f1a2016-05-09 10:38:05 +02006063 return -1;
6064
Peter Zijlstra1ad3aaf2017-05-17 12:53:50 +02006065 if (sched_feat(SIS_PROP)) {
6066 u64 span_avg = sd->span_weight * avg_idle;
6067 if (span_avg > 4*avg_cost)
6068 nr = div_u64(span_avg, avg_cost);
6069 else
6070 nr = 4;
6071 }
6072
Peter Zijlstra10e2f1a2016-05-09 10:38:05 +02006073 time = local_clock();
6074
Peter Zijlstrac743f0a2017-04-14 14:20:05 +02006075 for_each_cpu_wrap(cpu, sched_domain_span(sd), target) {
Peter Zijlstra1ad3aaf2017-05-17 12:53:50 +02006076 if (!--nr)
6077 return -1;
Ingo Molnar0c98d342017-02-05 15:38:10 +01006078 if (!cpumask_test_cpu(cpu, &p->cpus_allowed))
Peter Zijlstra10e2f1a2016-05-09 10:38:05 +02006079 continue;
6080 if (idle_cpu(cpu))
6081 break;
6082 }
6083
6084 time = local_clock() - time;
6085 cost = this_sd->avg_scan_cost;
6086 delta = (s64)(time - cost) / 8;
6087 this_sd->avg_scan_cost += delta;
6088
6089 return cpu;
6090}
6091
6092/*
6093 * Try and locate an idle core/thread in the LLC cache domain.
Peter Zijlstraa50bde52009-11-12 15:55:28 +01006094 */
Morten Rasmussen772bd008c2016-06-22 18:03:13 +01006095static int select_idle_sibling(struct task_struct *p, int prev, int target)
Peter Zijlstraa50bde52009-11-12 15:55:28 +01006096{
Suresh Siddha99bd5e22010-03-31 16:47:45 -07006097 struct sched_domain *sd;
Peter Zijlstra10e2f1a2016-05-09 10:38:05 +02006098 int i;
Mike Galbraithe0a79f52013-01-28 12:19:25 +01006099
6100 if (idle_cpu(target))
6101 return target;
Peter Zijlstraa50bde52009-11-12 15:55:28 +01006102
6103 /*
Peter Zijlstra10e2f1a2016-05-09 10:38:05 +02006104 * If the previous cpu is cache affine and idle, don't be stupid.
Peter Zijlstraa50bde52009-11-12 15:55:28 +01006105 */
Morten Rasmussen772bd008c2016-06-22 18:03:13 +01006106 if (prev != target && cpus_share_cache(prev, target) && idle_cpu(prev))
6107 return prev;
Peter Zijlstraa50bde52009-11-12 15:55:28 +01006108
Peter Zijlstra518cd622011-12-07 15:07:31 +01006109 sd = rcu_dereference(per_cpu(sd_llc, target));
Peter Zijlstra10e2f1a2016-05-09 10:38:05 +02006110 if (!sd)
6111 return target;
Morten Rasmussen772bd008c2016-06-22 18:03:13 +01006112
Peter Zijlstra10e2f1a2016-05-09 10:38:05 +02006113 i = select_idle_core(p, sd, target);
6114 if ((unsigned)i < nr_cpumask_bits)
Gregory Haskinse7693a32008-01-25 21:08:09 +01006115 return i;
Ingo Molnar098fb9d2008-03-16 20:36:10 +01006116
Peter Zijlstra10e2f1a2016-05-09 10:38:05 +02006117 i = select_idle_cpu(p, sd, target);
6118 if ((unsigned)i < nr_cpumask_bits)
6119 return i;
Mike Galbraith970e1782012-06-12 05:18:32 +02006120
Peter Zijlstra10e2f1a2016-05-09 10:38:05 +02006121 i = select_idle_smt(p, sd, target);
6122 if ((unsigned)i < nr_cpumask_bits)
6123 return i;
Linus Torvalds37407ea2012-09-16 12:29:43 -07006124
Peter Zijlstraa50bde52009-11-12 15:55:28 +01006125 return target;
6126}
Dietmar Eggemann231678b2015-08-14 17:23:13 +01006127
Vincent Guittot8bb5b002015-03-04 08:48:47 +01006128/*
Dietmar Eggemann9e91d612015-08-14 17:23:12 +01006129 * cpu_util returns the amount of capacity of a CPU that is used by CFS
Vincent Guittot8bb5b002015-03-04 08:48:47 +01006130 * tasks. The unit of the return value must be the one of capacity so we can
Dietmar Eggemann9e91d612015-08-14 17:23:12 +01006131 * compare the utilization with the capacity of the CPU that is available for
6132 * CFS task (ie cpu_capacity).
Dietmar Eggemann231678b2015-08-14 17:23:13 +01006133 *
6134 * cfs_rq.avg.util_avg is the sum of running time of runnable tasks plus the
6135 * recent utilization of currently non-runnable tasks on a CPU. It represents
6136 * the amount of utilization of a CPU in the range [0..capacity_orig] where
6137 * capacity_orig is the cpu_capacity available at the highest frequency
6138 * (arch_scale_freq_capacity()).
6139 * The utilization of a CPU converges towards a sum equal to or less than the
6140 * current capacity (capacity_curr <= capacity_orig) of the CPU because it is
6141 * the running time on this CPU scaled by capacity_curr.
6142 *
6143 * Nevertheless, cfs_rq.avg.util_avg can be higher than capacity_curr or even
6144 * higher than capacity_orig because of unfortunate rounding in
6145 * cfs.avg.util_avg or just after migrating tasks and new task wakeups until
6146 * the average stabilizes with the new running time. We need to check that the
6147 * utilization stays within the range of [0..capacity_orig] and cap it if
6148 * necessary. Without utilization capping, a group could be seen as overloaded
6149 * (CPU0 utilization at 121% + CPU1 utilization at 80%) whereas CPU1 has 20% of
6150 * available capacity. We allow utilization to overshoot capacity_curr (but not
6151 * capacity_orig) as it useful for predicting the capacity required after task
6152 * migrations (scheduler-driven DVFS).
Vincent Guittot8bb5b002015-03-04 08:48:47 +01006153 */
Dietmar Eggemann9e91d612015-08-14 17:23:12 +01006154static int cpu_util(int cpu)
Vincent Guittot8bb5b002015-03-04 08:48:47 +01006155{
Dietmar Eggemann9e91d612015-08-14 17:23:12 +01006156 unsigned long util = cpu_rq(cpu)->cfs.avg.util_avg;
Vincent Guittot8bb5b002015-03-04 08:48:47 +01006157 unsigned long capacity = capacity_orig_of(cpu);
6158
Dietmar Eggemann231678b2015-08-14 17:23:13 +01006159 return (util >= capacity) ? capacity : util;
Vincent Guittot8bb5b002015-03-04 08:48:47 +01006160}
Peter Zijlstraa50bde52009-11-12 15:55:28 +01006161
Morten Rasmussen32731632016-07-25 14:34:26 +01006162static inline int task_util(struct task_struct *p)
6163{
6164 return p->se.avg.util_avg;
6165}
6166
6167/*
Morten Rasmussen104cb162016-10-14 14:41:07 +01006168 * cpu_util_wake: Compute cpu utilization with any contributions from
6169 * the waking task p removed.
6170 */
6171static int cpu_util_wake(int cpu, struct task_struct *p)
6172{
6173 unsigned long util, capacity;
6174
6175 /* Task has no contribution or is new */
6176 if (cpu != task_cpu(p) || !p->se.avg.last_update_time)
6177 return cpu_util(cpu);
6178
6179 capacity = capacity_orig_of(cpu);
6180 util = max_t(long, cpu_rq(cpu)->cfs.avg.util_avg - task_util(p), 0);
6181
6182 return (util >= capacity) ? capacity : util;
6183}
6184
6185/*
Morten Rasmussen32731632016-07-25 14:34:26 +01006186 * Disable WAKE_AFFINE in the case where task @p doesn't fit in the
6187 * capacity of either the waking CPU @cpu or the previous CPU @prev_cpu.
6188 *
6189 * In that case WAKE_AFFINE doesn't make sense and we'll let
6190 * BALANCE_WAKE sort things out.
6191 */
6192static int wake_cap(struct task_struct *p, int cpu, int prev_cpu)
6193{
6194 long min_cap, max_cap;
6195
6196 min_cap = min(capacity_orig_of(prev_cpu), capacity_orig_of(cpu));
6197 max_cap = cpu_rq(cpu)->rd->max_cpu_capacity;
6198
6199 /* Minimum capacity is close to max, no need to abort wake_affine */
6200 if (max_cap - min_cap < max_cap >> 3)
6201 return 0;
6202
Morten Rasmussen104cb162016-10-14 14:41:07 +01006203 /* Bring task utilization in sync with prev_cpu */
6204 sync_entity_load_avg(&p->se);
6205
Morten Rasmussen32731632016-07-25 14:34:26 +01006206 return min_cap * 1024 < task_util(p) * capacity_margin;
6207}
6208
Peter Zijlstraa50bde52009-11-12 15:55:28 +01006209/*
Morten Rasmussende91b9c2014-02-18 14:14:24 +00006210 * select_task_rq_fair: Select target runqueue for the waking task in domains
6211 * that have the 'sd_flag' flag set. In practice, this is SD_BALANCE_WAKE,
6212 * SD_BALANCE_FORK, or SD_BALANCE_EXEC.
Peter Zijlstraaaee1202009-09-10 13:36:25 +02006213 *
Morten Rasmussende91b9c2014-02-18 14:14:24 +00006214 * Balances load by selecting the idlest cpu in the idlest group, or under
6215 * certain conditions an idle sibling cpu if the domain has SD_WAKE_AFFINE set.
Peter Zijlstraaaee1202009-09-10 13:36:25 +02006216 *
Morten Rasmussende91b9c2014-02-18 14:14:24 +00006217 * Returns the target cpu number.
Peter Zijlstraaaee1202009-09-10 13:36:25 +02006218 *
6219 * preempt must be disabled.
6220 */
Peter Zijlstra0017d732010-03-24 18:34:10 +01006221static int
Peter Zijlstraac66f542013-10-07 11:29:16 +01006222select_task_rq_fair(struct task_struct *p, int prev_cpu, int sd_flag, int wake_flags)
Peter Zijlstraaaee1202009-09-10 13:36:25 +02006223{
Peter Zijlstra29cd8ba2009-09-17 09:01:14 +02006224 struct sched_domain *tmp, *affine_sd = NULL, *sd = NULL;
Peter Zijlstrac88d5912009-09-10 13:50:02 +02006225 int cpu = smp_processor_id();
Mike Galbraith63b0e9e2015-07-14 17:39:50 +02006226 int new_cpu = prev_cpu;
Suresh Siddha99bd5e22010-03-31 16:47:45 -07006227 int want_affine = 0;
Peter Zijlstra5158f4e2009-09-16 13:46:59 +02006228 int sync = wake_flags & WF_SYNC;
Gregory Haskinse7693a32008-01-25 21:08:09 +01006229
Peter Zijlstrac58d25f2016-05-12 09:19:59 +02006230 if (sd_flag & SD_BALANCE_WAKE) {
6231 record_wakee(p);
Morten Rasmussen32731632016-07-25 14:34:26 +01006232 want_affine = !wake_wide(p) && !wake_cap(p, cpu, prev_cpu)
Ingo Molnar0c98d342017-02-05 15:38:10 +01006233 && cpumask_test_cpu(cpu, &p->cpus_allowed);
Peter Zijlstrac58d25f2016-05-12 09:19:59 +02006234 }
Gregory Haskinse7693a32008-01-25 21:08:09 +01006235
Peter Zijlstradce840a2011-04-07 14:09:50 +02006236 rcu_read_lock();
Peter Zijlstraaaee1202009-09-10 13:36:25 +02006237 for_each_domain(cpu, tmp) {
Peter Zijlstrae4f42882009-12-16 18:04:34 +01006238 if (!(tmp->flags & SD_LOAD_BALANCE))
Mike Galbraith63b0e9e2015-07-14 17:39:50 +02006239 break;
Peter Zijlstrae4f42882009-12-16 18:04:34 +01006240
Peter Zijlstraaaee1202009-09-10 13:36:25 +02006241 /*
Suresh Siddha99bd5e22010-03-31 16:47:45 -07006242 * If both cpu and prev_cpu are part of this domain,
6243 * cpu is a valid SD_WAKE_AFFINE target.
Peter Zijlstrafe3bcfe2009-11-12 15:55:29 +01006244 */
Suresh Siddha99bd5e22010-03-31 16:47:45 -07006245 if (want_affine && (tmp->flags & SD_WAKE_AFFINE) &&
6246 cpumask_test_cpu(prev_cpu, sched_domain_span(tmp))) {
6247 affine_sd = tmp;
Alex Shif03542a2012-07-26 08:55:34 +08006248 break;
Peter Zijlstrac88d5912009-09-10 13:50:02 +02006249 }
6250
Alex Shif03542a2012-07-26 08:55:34 +08006251 if (tmp->flags & sd_flag)
Peter Zijlstra29cd8ba2009-09-17 09:01:14 +02006252 sd = tmp;
Mike Galbraith63b0e9e2015-07-14 17:39:50 +02006253 else if (!want_affine)
6254 break;
Peter Zijlstrac88d5912009-09-10 13:50:02 +02006255 }
Peter Zijlstraaaee1202009-09-10 13:36:25 +02006256
Mike Galbraith63b0e9e2015-07-14 17:39:50 +02006257 if (affine_sd) {
6258 sd = NULL; /* Prefer wake_affine over balance flags */
Rik van Riel7d894e62017-06-23 12:55:28 -04006259 if (cpu == prev_cpu)
6260 goto pick_cpu;
6261
6262 if (wake_affine(affine_sd, p, prev_cpu, sync))
Mike Galbraith63b0e9e2015-07-14 17:39:50 +02006263 new_cpu = cpu;
Mike Galbraith8b911ac2010-03-11 17:17:16 +01006264 }
Peter Zijlstra3b640892009-09-16 13:44:33 +02006265
Brendan Jackmanea16f0e2017-10-05 11:55:51 +01006266 if (sd && !(sd_flag & SD_BALANCE_FORK)) {
6267 /*
6268 * We're going to need the task's util for capacity_spare_wake
6269 * in find_idlest_group. Sync it up to prev_cpu's
6270 * last_update_time.
6271 */
6272 sync_entity_load_avg(&p->se);
6273 }
6274
Mike Galbraith63b0e9e2015-07-14 17:39:50 +02006275 if (!sd) {
Brendan Jackmanea16f0e2017-10-05 11:55:51 +01006276pick_cpu:
Mike Galbraith63b0e9e2015-07-14 17:39:50 +02006277 if (sd_flag & SD_BALANCE_WAKE) /* XXX always ? */
Morten Rasmussen772bd008c2016-06-22 18:03:13 +01006278 new_cpu = select_idle_sibling(p, prev_cpu, new_cpu);
Mike Galbraith63b0e9e2015-07-14 17:39:50 +02006279
6280 } else while (sd) {
Peter Zijlstraaaee1202009-09-10 13:36:25 +02006281 struct sched_group *group;
Peter Zijlstrac88d5912009-09-10 13:50:02 +02006282 int weight;
Peter Zijlstraaaee1202009-09-10 13:36:25 +02006283
Peter Zijlstra0763a662009-09-14 19:37:39 +02006284 if (!(sd->flags & sd_flag)) {
Peter Zijlstraaaee1202009-09-10 13:36:25 +02006285 sd = sd->child;
6286 continue;
6287 }
6288
Vincent Guittotc44f2a02013-10-18 13:52:21 +02006289 group = find_idlest_group(sd, p, cpu, sd_flag);
Peter Zijlstraaaee1202009-09-10 13:36:25 +02006290 if (!group) {
6291 sd = sd->child;
6292 continue;
6293 }
6294
Peter Zijlstrad7c33c42009-09-11 12:45:38 +02006295 new_cpu = find_idlest_cpu(group, p, cpu);
Peter Zijlstraaaee1202009-09-10 13:36:25 +02006296 if (new_cpu == -1 || new_cpu == cpu) {
6297 /* Now try balancing at a lower domain level of cpu */
6298 sd = sd->child;
6299 continue;
6300 }
6301
6302 /* Now try balancing at a lower domain level of new_cpu */
6303 cpu = new_cpu;
Peter Zijlstra669c55e2010-04-16 14:59:29 +02006304 weight = sd->span_weight;
Peter Zijlstraaaee1202009-09-10 13:36:25 +02006305 sd = NULL;
6306 for_each_domain(cpu, tmp) {
Peter Zijlstra669c55e2010-04-16 14:59:29 +02006307 if (weight <= tmp->span_weight)
Peter Zijlstraaaee1202009-09-10 13:36:25 +02006308 break;
Peter Zijlstra0763a662009-09-14 19:37:39 +02006309 if (tmp->flags & sd_flag)
Peter Zijlstraaaee1202009-09-10 13:36:25 +02006310 sd = tmp;
6311 }
6312 /* while loop will break here if sd == NULL */
Gregory Haskinse7693a32008-01-25 21:08:09 +01006313 }
Peter Zijlstradce840a2011-04-07 14:09:50 +02006314 rcu_read_unlock();
Gregory Haskinse7693a32008-01-25 21:08:09 +01006315
Peter Zijlstrac88d5912009-09-10 13:50:02 +02006316 return new_cpu;
Gregory Haskinse7693a32008-01-25 21:08:09 +01006317}
Paul Turner0a74bef2012-10-04 13:18:30 +02006318
Peter Zijlstra144d8482017-05-11 17:57:24 +02006319static void detach_entity_cfs_rq(struct sched_entity *se);
6320
Paul Turner0a74bef2012-10-04 13:18:30 +02006321/*
6322 * Called immediately before a task is migrated to a new cpu; task_cpu(p) and
6323 * cfs_rq_of(p) references at time of call are still valid and identify the
Byungchul Park525628c2015-11-18 09:34:59 +09006324 * previous cpu. The caller guarantees p->pi_lock or task_rq(p)->lock is held.
Paul Turner0a74bef2012-10-04 13:18:30 +02006325 */
xiaofeng.yan5a4fd032015-09-23 14:55:59 +08006326static void migrate_task_rq_fair(struct task_struct *p)
Paul Turner0a74bef2012-10-04 13:18:30 +02006327{
Paul Turneraff3e492012-10-04 13:18:30 +02006328 /*
Peter Zijlstra59efa0b2016-05-10 18:24:37 +02006329 * As blocked tasks retain absolute vruntime the migration needs to
6330 * deal with this by subtracting the old and adding the new
6331 * min_vruntime -- the latter is done by enqueue_entity() when placing
6332 * the task on the new runqueue.
6333 */
6334 if (p->state == TASK_WAKING) {
6335 struct sched_entity *se = &p->se;
6336 struct cfs_rq *cfs_rq = cfs_rq_of(se);
6337 u64 min_vruntime;
6338
6339#ifndef CONFIG_64BIT
6340 u64 min_vruntime_copy;
6341
6342 do {
6343 min_vruntime_copy = cfs_rq->min_vruntime_copy;
6344 smp_rmb();
6345 min_vruntime = cfs_rq->min_vruntime;
6346 } while (min_vruntime != min_vruntime_copy);
6347#else
6348 min_vruntime = cfs_rq->min_vruntime;
6349#endif
6350
6351 se->vruntime -= min_vruntime;
6352 }
6353
Peter Zijlstra144d8482017-05-11 17:57:24 +02006354 if (p->on_rq == TASK_ON_RQ_MIGRATING) {
6355 /*
6356 * In case of TASK_ON_RQ_MIGRATING we in fact hold the 'old'
6357 * rq->lock and can modify state directly.
6358 */
6359 lockdep_assert_held(&task_rq(p)->lock);
6360 detach_entity_cfs_rq(&p->se);
6361
6362 } else {
6363 /*
6364 * We are supposed to update the task to "current" time, then
6365 * its up to date and ready to go to new CPU/cfs_rq. But we
6366 * have difficulty in getting what current time is, so simply
6367 * throw away the out-of-date time. This will result in the
6368 * wakee task is less decayed, but giving the wakee more load
6369 * sounds not bad.
6370 */
6371 remove_entity_load_avg(&p->se);
6372 }
Yuyang Du9d89c252015-07-15 08:04:37 +08006373
6374 /* Tell new CPU we are migrated */
6375 p->se.avg.last_update_time = 0;
Ben Segall3944a922014-05-15 15:59:20 -07006376
6377 /* We have migrated, no longer consider this task hot */
Yuyang Du9d89c252015-07-15 08:04:37 +08006378 p->se.exec_start = 0;
Paul Turner0a74bef2012-10-04 13:18:30 +02006379}
Yuyang Du12695572015-07-15 08:04:40 +08006380
6381static void task_dead_fair(struct task_struct *p)
6382{
6383 remove_entity_load_avg(&p->se);
6384}
Gregory Haskinse7693a32008-01-25 21:08:09 +01006385#endif /* CONFIG_SMP */
6386
Peter Zijlstrae52fb7c2009-01-14 12:39:19 +01006387static unsigned long
6388wakeup_gran(struct sched_entity *curr, struct sched_entity *se)
Peter Zijlstra0bbd3332008-04-19 19:44:57 +02006389{
6390 unsigned long gran = sysctl_sched_wakeup_granularity;
6391
6392 /*
Peter Zijlstrae52fb7c2009-01-14 12:39:19 +01006393 * Since its curr running now, convert the gran from real-time
6394 * to virtual-time in his units.
Mike Galbraith13814d42010-03-11 17:17:04 +01006395 *
6396 * By using 'se' instead of 'curr' we penalize light tasks, so
6397 * they get preempted easier. That is, if 'se' < 'curr' then
6398 * the resulting gran will be larger, therefore penalizing the
6399 * lighter, if otoh 'se' > 'curr' then the resulting gran will
6400 * be smaller, again penalizing the lighter task.
6401 *
6402 * This is especially important for buddies when the leftmost
6403 * task is higher priority than the buddy.
Peter Zijlstra0bbd3332008-04-19 19:44:57 +02006404 */
Shaohua Lif4ad9bd2011-04-08 12:53:09 +08006405 return calc_delta_fair(gran, se);
Peter Zijlstra0bbd3332008-04-19 19:44:57 +02006406}
6407
6408/*
Peter Zijlstra464b7522008-10-24 11:06:15 +02006409 * Should 'se' preempt 'curr'.
6410 *
6411 * |s1
6412 * |s2
6413 * |s3
6414 * g
6415 * |<--->|c
6416 *
6417 * w(c, s1) = -1
6418 * w(c, s2) = 0
6419 * w(c, s3) = 1
6420 *
6421 */
6422static int
6423wakeup_preempt_entity(struct sched_entity *curr, struct sched_entity *se)
6424{
6425 s64 gran, vdiff = curr->vruntime - se->vruntime;
6426
6427 if (vdiff <= 0)
6428 return -1;
6429
Peter Zijlstrae52fb7c2009-01-14 12:39:19 +01006430 gran = wakeup_gran(curr, se);
Peter Zijlstra464b7522008-10-24 11:06:15 +02006431 if (vdiff > gran)
6432 return 1;
6433
6434 return 0;
6435}
6436
Peter Zijlstra02479092008-11-04 21:25:10 +01006437static void set_last_buddy(struct sched_entity *se)
6438{
Venkatesh Pallipadi69c80f32011-04-13 18:21:09 -07006439 if (entity_is_task(se) && unlikely(task_of(se)->policy == SCHED_IDLE))
6440 return;
6441
Daniel Axtensc5ae3662017-05-11 06:11:39 +10006442 for_each_sched_entity(se) {
6443 if (SCHED_WARN_ON(!se->on_rq))
6444 return;
Venkatesh Pallipadi69c80f32011-04-13 18:21:09 -07006445 cfs_rq_of(se)->last = se;
Daniel Axtensc5ae3662017-05-11 06:11:39 +10006446 }
Peter Zijlstra02479092008-11-04 21:25:10 +01006447}
6448
6449static void set_next_buddy(struct sched_entity *se)
6450{
Venkatesh Pallipadi69c80f32011-04-13 18:21:09 -07006451 if (entity_is_task(se) && unlikely(task_of(se)->policy == SCHED_IDLE))
6452 return;
6453
Daniel Axtensc5ae3662017-05-11 06:11:39 +10006454 for_each_sched_entity(se) {
6455 if (SCHED_WARN_ON(!se->on_rq))
6456 return;
Venkatesh Pallipadi69c80f32011-04-13 18:21:09 -07006457 cfs_rq_of(se)->next = se;
Daniel Axtensc5ae3662017-05-11 06:11:39 +10006458 }
Peter Zijlstra02479092008-11-04 21:25:10 +01006459}
6460
Rik van Rielac53db52011-02-01 09:51:03 -05006461static void set_skip_buddy(struct sched_entity *se)
6462{
Venkatesh Pallipadi69c80f32011-04-13 18:21:09 -07006463 for_each_sched_entity(se)
6464 cfs_rq_of(se)->skip = se;
Rik van Rielac53db52011-02-01 09:51:03 -05006465}
6466
Peter Zijlstra464b7522008-10-24 11:06:15 +02006467/*
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02006468 * Preempt the current task with a newly woken task if needed:
6469 */
Peter Zijlstra5a9b86f2009-09-16 13:47:58 +02006470static void check_preempt_wakeup(struct rq *rq, struct task_struct *p, int wake_flags)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02006471{
6472 struct task_struct *curr = rq->curr;
Srivatsa Vaddagiri8651a862007-10-15 17:00:12 +02006473 struct sched_entity *se = &curr->se, *pse = &p->se;
Mike Galbraith03e89e42008-12-16 08:45:30 +01006474 struct cfs_rq *cfs_rq = task_cfs_rq(curr);
Mike Galbraithf685cea2009-10-23 23:09:22 +02006475 int scale = cfs_rq->nr_running >= sched_nr_latency;
Venkatesh Pallipadi2f368252011-04-14 10:30:53 -07006476 int next_buddy_marked = 0;
Mike Galbraith03e89e42008-12-16 08:45:30 +01006477
Ingo Molnar4ae7d5c2008-03-19 01:42:00 +01006478 if (unlikely(se == pse))
6479 return;
6480
Paul Turner5238cdd2011-07-21 09:43:37 -07006481 /*
Kirill Tkhai163122b2014-08-20 13:48:29 +04006482 * This is possible from callers such as attach_tasks(), in which we
Paul Turner5238cdd2011-07-21 09:43:37 -07006483 * unconditionally check_prempt_curr() after an enqueue (which may have
6484 * lead to a throttle). This both saves work and prevents false
6485 * next-buddy nomination below.
6486 */
6487 if (unlikely(throttled_hierarchy(cfs_rq_of(pse))))
6488 return;
6489
Venkatesh Pallipadi2f368252011-04-14 10:30:53 -07006490 if (sched_feat(NEXT_BUDDY) && scale && !(wake_flags & WF_FORK)) {
Mike Galbraith3cb63d52009-09-11 12:01:17 +02006491 set_next_buddy(pse);
Venkatesh Pallipadi2f368252011-04-14 10:30:53 -07006492 next_buddy_marked = 1;
6493 }
Peter Zijlstra57fdc262008-09-23 15:33:45 +02006494
Bharata B Raoaec0a512008-08-28 14:42:49 +05306495 /*
6496 * We can come here with TIF_NEED_RESCHED already set from new task
6497 * wake up path.
Paul Turner5238cdd2011-07-21 09:43:37 -07006498 *
6499 * Note: this also catches the edge-case of curr being in a throttled
6500 * group (e.g. via set_curr_task), since update_curr() (in the
6501 * enqueue of curr) will have resulted in resched being set. This
6502 * prevents us from potentially nominating it as a false LAST_BUDDY
6503 * below.
Bharata B Raoaec0a512008-08-28 14:42:49 +05306504 */
6505 if (test_tsk_need_resched(curr))
6506 return;
6507
Darren Harta2f5c9a2011-02-22 13:04:33 -08006508 /* Idle tasks are by definition preempted by non-idle tasks. */
6509 if (unlikely(curr->policy == SCHED_IDLE) &&
6510 likely(p->policy != SCHED_IDLE))
6511 goto preempt;
6512
Ingo Molnar91c234b2007-10-15 17:00:18 +02006513 /*
Darren Harta2f5c9a2011-02-22 13:04:33 -08006514 * Batch and idle tasks do not preempt non-idle tasks (their preemption
6515 * is driven by the tick):
Ingo Molnar91c234b2007-10-15 17:00:18 +02006516 */
Ingo Molnar8ed92e52012-10-14 14:28:50 +02006517 if (unlikely(p->policy != SCHED_NORMAL) || !sched_feat(WAKEUP_PREEMPTION))
Ingo Molnar91c234b2007-10-15 17:00:18 +02006518 return;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02006519
Peter Zijlstra3a7e73a2009-11-28 18:51:02 +01006520 find_matching_se(&se, &pse);
Paul Turner9bbd7372011-07-05 19:07:21 -07006521 update_curr(cfs_rq_of(se));
Peter Zijlstra3a7e73a2009-11-28 18:51:02 +01006522 BUG_ON(!pse);
Venkatesh Pallipadi2f368252011-04-14 10:30:53 -07006523 if (wakeup_preempt_entity(se, pse) == 1) {
6524 /*
6525 * Bias pick_next to pick the sched entity that is
6526 * triggering this preemption.
6527 */
6528 if (!next_buddy_marked)
6529 set_next_buddy(pse);
Peter Zijlstra3a7e73a2009-11-28 18:51:02 +01006530 goto preempt;
Venkatesh Pallipadi2f368252011-04-14 10:30:53 -07006531 }
Jupyung Leea65ac742009-11-17 18:51:40 +09006532
Peter Zijlstra3a7e73a2009-11-28 18:51:02 +01006533 return;
6534
6535preempt:
Kirill Tkhai88751252014-06-29 00:03:57 +04006536 resched_curr(rq);
Peter Zijlstra3a7e73a2009-11-28 18:51:02 +01006537 /*
6538 * Only set the backward buddy when the current task is still
6539 * on the rq. This can happen when a wakeup gets interleaved
6540 * with schedule on the ->pre_schedule() or idle_balance()
6541 * point, either of which can * drop the rq lock.
6542 *
6543 * Also, during early boot the idle thread is in the fair class,
6544 * for obvious reasons its a bad idea to schedule back to it.
6545 */
6546 if (unlikely(!se->on_rq || curr == rq->idle))
6547 return;
6548
6549 if (sched_feat(LAST_BUDDY) && scale && entity_is_task(se))
6550 set_last_buddy(se);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02006551}
6552
Peter Zijlstra606dba22012-02-11 06:05:00 +01006553static struct task_struct *
Matt Flemingd8ac8972016-09-21 14:38:10 +01006554pick_next_task_fair(struct rq *rq, struct task_struct *prev, struct rq_flags *rf)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02006555{
6556 struct cfs_rq *cfs_rq = &rq->cfs;
6557 struct sched_entity *se;
Peter Zijlstra678d5712012-02-11 06:05:00 +01006558 struct task_struct *p;
Peter Zijlstra37e117c2014-02-14 12:25:08 +01006559 int new_tasks;
Peter Zijlstra678d5712012-02-11 06:05:00 +01006560
Peter Zijlstra6e831252014-02-11 16:11:48 +01006561again:
Peter Zijlstra678d5712012-02-11 06:05:00 +01006562 if (!cfs_rq->nr_running)
Peter Zijlstra38033c32014-01-23 20:32:21 +01006563 goto idle;
Peter Zijlstra678d5712012-02-11 06:05:00 +01006564
Viresh Kumar9674f5c2017-05-24 10:59:55 +05306565#ifdef CONFIG_FAIR_GROUP_SCHED
Peter Zijlstra3f1d2a32014-02-12 10:49:30 +01006566 if (prev->sched_class != &fair_sched_class)
Peter Zijlstra678d5712012-02-11 06:05:00 +01006567 goto simple;
6568
6569 /*
6570 * Because of the set_next_buddy() in dequeue_task_fair() it is rather
6571 * likely that a next task is from the same cgroup as the current.
6572 *
6573 * Therefore attempt to avoid putting and setting the entire cgroup
6574 * hierarchy, only change the part that actually changes.
6575 */
6576
6577 do {
6578 struct sched_entity *curr = cfs_rq->curr;
6579
6580 /*
6581 * Since we got here without doing put_prev_entity() we also
6582 * have to consider cfs_rq->curr. If it is still a runnable
6583 * entity, update_curr() will update its vruntime, otherwise
6584 * forget we've ever seen it.
6585 */
Ben Segall54d27362015-04-06 15:28:10 -07006586 if (curr) {
6587 if (curr->on_rq)
6588 update_curr(cfs_rq);
6589 else
6590 curr = NULL;
Peter Zijlstra678d5712012-02-11 06:05:00 +01006591
Ben Segall54d27362015-04-06 15:28:10 -07006592 /*
6593 * This call to check_cfs_rq_runtime() will do the
6594 * throttle and dequeue its entity in the parent(s).
Viresh Kumar9674f5c2017-05-24 10:59:55 +05306595 * Therefore the nr_running test will indeed
Ben Segall54d27362015-04-06 15:28:10 -07006596 * be correct.
6597 */
Viresh Kumar9674f5c2017-05-24 10:59:55 +05306598 if (unlikely(check_cfs_rq_runtime(cfs_rq))) {
6599 cfs_rq = &rq->cfs;
6600
6601 if (!cfs_rq->nr_running)
6602 goto idle;
6603
Ben Segall54d27362015-04-06 15:28:10 -07006604 goto simple;
Viresh Kumar9674f5c2017-05-24 10:59:55 +05306605 }
Ben Segall54d27362015-04-06 15:28:10 -07006606 }
Peter Zijlstra678d5712012-02-11 06:05:00 +01006607
6608 se = pick_next_entity(cfs_rq, curr);
6609 cfs_rq = group_cfs_rq(se);
6610 } while (cfs_rq);
6611
6612 p = task_of(se);
6613
6614 /*
6615 * Since we haven't yet done put_prev_entity and if the selected task
6616 * is a different task than we started out with, try and touch the
6617 * least amount of cfs_rqs.
6618 */
6619 if (prev != p) {
6620 struct sched_entity *pse = &prev->se;
6621
6622 while (!(cfs_rq = is_same_group(se, pse))) {
6623 int se_depth = se->depth;
6624 int pse_depth = pse->depth;
6625
6626 if (se_depth <= pse_depth) {
6627 put_prev_entity(cfs_rq_of(pse), pse);
6628 pse = parent_entity(pse);
6629 }
6630 if (se_depth >= pse_depth) {
6631 set_next_entity(cfs_rq_of(se), se);
6632 se = parent_entity(se);
6633 }
6634 }
6635
6636 put_prev_entity(cfs_rq, pse);
6637 set_next_entity(cfs_rq, se);
6638 }
6639
Uladzislau Rezki93824902017-09-13 12:24:30 +02006640 goto done;
Peter Zijlstra678d5712012-02-11 06:05:00 +01006641simple:
Peter Zijlstra678d5712012-02-11 06:05:00 +01006642#endif
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02006643
Peter Zijlstra3f1d2a32014-02-12 10:49:30 +01006644 put_prev_task(rq, prev);
Peter Zijlstra606dba22012-02-11 06:05:00 +01006645
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02006646 do {
Peter Zijlstra678d5712012-02-11 06:05:00 +01006647 se = pick_next_entity(cfs_rq, NULL);
Peter Zijlstraf4b67552008-11-04 21:25:07 +01006648 set_next_entity(cfs_rq, se);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02006649 cfs_rq = group_cfs_rq(se);
6650 } while (cfs_rq);
6651
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01006652 p = task_of(se);
Peter Zijlstra678d5712012-02-11 06:05:00 +01006653
Uladzislau Rezki93824902017-09-13 12:24:30 +02006654done: __maybe_unused
6655#ifdef CONFIG_SMP
6656 /*
6657 * Move the next running task to the front of
6658 * the list, so our cfs_tasks list becomes MRU
6659 * one.
6660 */
6661 list_move(&p->se.group_node, &rq->cfs_tasks);
6662#endif
6663
Mike Galbraithb39e66e2011-11-22 15:20:07 +01006664 if (hrtick_enabled(rq))
6665 hrtick_start_fair(rq, p);
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01006666
6667 return p;
Peter Zijlstra38033c32014-01-23 20:32:21 +01006668
6669idle:
Matt Fleming46f69fa2016-09-21 14:38:12 +01006670 new_tasks = idle_balance(rq, rf);
6671
Peter Zijlstra37e117c2014-02-14 12:25:08 +01006672 /*
6673 * Because idle_balance() releases (and re-acquires) rq->lock, it is
6674 * possible for any higher priority task to appear. In that case we
6675 * must re-start the pick_next_entity() loop.
6676 */
Kirill Tkhaie4aa3582014-03-06 13:31:55 +04006677 if (new_tasks < 0)
Peter Zijlstra37e117c2014-02-14 12:25:08 +01006678 return RETRY_TASK;
6679
Kirill Tkhaie4aa3582014-03-06 13:31:55 +04006680 if (new_tasks > 0)
Peter Zijlstra38033c32014-01-23 20:32:21 +01006681 goto again;
Peter Zijlstra38033c32014-01-23 20:32:21 +01006682
6683 return NULL;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02006684}
6685
6686/*
6687 * Account for a descheduled task:
6688 */
Ingo Molnar31ee5292007-08-09 11:16:49 +02006689static void put_prev_task_fair(struct rq *rq, struct task_struct *prev)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02006690{
6691 struct sched_entity *se = &prev->se;
6692 struct cfs_rq *cfs_rq;
6693
6694 for_each_sched_entity(se) {
6695 cfs_rq = cfs_rq_of(se);
Ingo Molnarab6cde22007-08-09 11:16:48 +02006696 put_prev_entity(cfs_rq, se);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02006697 }
6698}
6699
Rik van Rielac53db52011-02-01 09:51:03 -05006700/*
6701 * sched_yield() is very simple
6702 *
6703 * The magic of dealing with the ->skip buddy is in pick_next_entity.
6704 */
6705static void yield_task_fair(struct rq *rq)
6706{
6707 struct task_struct *curr = rq->curr;
6708 struct cfs_rq *cfs_rq = task_cfs_rq(curr);
6709 struct sched_entity *se = &curr->se;
6710
6711 /*
6712 * Are we the only task in the tree?
6713 */
6714 if (unlikely(rq->nr_running == 1))
6715 return;
6716
6717 clear_buddies(cfs_rq, se);
6718
6719 if (curr->policy != SCHED_BATCH) {
6720 update_rq_clock(rq);
6721 /*
6722 * Update run-time statistics of the 'current'.
6723 */
6724 update_curr(cfs_rq);
Mike Galbraith916671c2011-11-22 15:21:26 +01006725 /*
6726 * Tell update_rq_clock() that we've just updated,
6727 * so we don't do microscopic update in schedule()
6728 * and double the fastpath cost.
6729 */
Peter Zijlstra9edfbfe2015-01-05 11:18:11 +01006730 rq_clock_skip_update(rq, true);
Rik van Rielac53db52011-02-01 09:51:03 -05006731 }
6732
6733 set_skip_buddy(se);
6734}
6735
Mike Galbraithd95f4122011-02-01 09:50:51 -05006736static bool yield_to_task_fair(struct rq *rq, struct task_struct *p, bool preempt)
6737{
6738 struct sched_entity *se = &p->se;
6739
Paul Turner5238cdd2011-07-21 09:43:37 -07006740 /* throttled hierarchies are not runnable */
6741 if (!se->on_rq || throttled_hierarchy(cfs_rq_of(se)))
Mike Galbraithd95f4122011-02-01 09:50:51 -05006742 return false;
6743
6744 /* Tell the scheduler that we'd really like pse to run next. */
6745 set_next_buddy(se);
6746
Mike Galbraithd95f4122011-02-01 09:50:51 -05006747 yield_task_fair(rq);
6748
6749 return true;
6750}
6751
Peter Williams681f3e62007-10-24 18:23:51 +02006752#ifdef CONFIG_SMP
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02006753/**************************************************
Peter Zijlstrae9c84cb2012-07-03 13:53:26 +02006754 * Fair scheduling class load-balancing methods.
6755 *
6756 * BASICS
6757 *
6758 * The purpose of load-balancing is to achieve the same basic fairness the
6759 * per-cpu scheduler provides, namely provide a proportional amount of compute
6760 * time to each task. This is expressed in the following equation:
6761 *
6762 * W_i,n/P_i == W_j,n/P_j for all i,j (1)
6763 *
6764 * Where W_i,n is the n-th weight average for cpu i. The instantaneous weight
6765 * W_i,0 is defined as:
6766 *
6767 * W_i,0 = \Sum_j w_i,j (2)
6768 *
6769 * 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 +08006770 * is derived from the nice value as per sched_prio_to_weight[].
Peter Zijlstrae9c84cb2012-07-03 13:53:26 +02006771 *
6772 * The weight average is an exponential decay average of the instantaneous
6773 * weight:
6774 *
6775 * W'_i,n = (2^n - 1) / 2^n * W_i,n + 1 / 2^n * W_i,0 (3)
6776 *
Nicolas Pitreced549f2014-05-26 18:19:38 -04006777 * C_i is the compute capacity of cpu i, typically it is the
Peter Zijlstrae9c84cb2012-07-03 13:53:26 +02006778 * fraction of 'recent' time available for SCHED_OTHER task execution. But it
6779 * can also include other factors [XXX].
6780 *
6781 * To achieve this balance we define a measure of imbalance which follows
6782 * directly from (1):
6783 *
Nicolas Pitreced549f2014-05-26 18:19:38 -04006784 * 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 +02006785 *
6786 * We them move tasks around to minimize the imbalance. In the continuous
6787 * function space it is obvious this converges, in the discrete case we get
6788 * a few fun cases generally called infeasible weight scenarios.
6789 *
6790 * [XXX expand on:
6791 * - infeasible weights;
6792 * - local vs global optima in the discrete case. ]
6793 *
6794 *
6795 * SCHED DOMAINS
6796 *
6797 * In order to solve the imbalance equation (4), and avoid the obvious O(n^2)
6798 * for all i,j solution, we create a tree of cpus that follows the hardware
6799 * topology where each level pairs two lower groups (or better). This results
6800 * in O(log n) layers. Furthermore we reduce the number of cpus going up the
6801 * tree to only the first of the previous level and we decrease the frequency
6802 * of load-balance at each level inv. proportional to the number of cpus in
6803 * the groups.
6804 *
6805 * This yields:
6806 *
6807 * log_2 n 1 n
6808 * \Sum { --- * --- * 2^i } = O(n) (5)
6809 * i = 0 2^i 2^i
6810 * `- size of each group
6811 * | | `- number of cpus doing load-balance
6812 * | `- freq
6813 * `- sum over all levels
6814 *
6815 * Coupled with a limit on how many tasks we can migrate every balance pass,
6816 * this makes (5) the runtime complexity of the balancer.
6817 *
6818 * An important property here is that each CPU is still (indirectly) connected
6819 * to every other cpu in at most O(log n) steps:
6820 *
6821 * The adjacency matrix of the resulting graph is given by:
6822 *
Byungchul Park97a71422015-07-05 18:33:48 +09006823 * log_2 n
Peter Zijlstrae9c84cb2012-07-03 13:53:26 +02006824 * A_i,j = \Union (i % 2^k == 0) && i / 2^(k+1) == j / 2^(k+1) (6)
6825 * k = 0
6826 *
6827 * And you'll find that:
6828 *
6829 * A^(log_2 n)_i,j != 0 for all i,j (7)
6830 *
6831 * Showing there's indeed a path between every cpu in at most O(log n) steps.
6832 * The task movement gives a factor of O(m), giving a convergence complexity
6833 * of:
6834 *
6835 * O(nm log n), n := nr_cpus, m := nr_tasks (8)
6836 *
6837 *
6838 * WORK CONSERVING
6839 *
6840 * In order to avoid CPUs going idle while there's still work to do, new idle
6841 * balancing is more aggressive and has the newly idle cpu iterate up the domain
6842 * tree itself instead of relying on other CPUs to bring it work.
6843 *
6844 * This adds some complexity to both (5) and (8) but it reduces the total idle
6845 * time.
6846 *
6847 * [XXX more?]
6848 *
6849 *
6850 * CGROUPS
6851 *
6852 * Cgroups make a horror show out of (2), instead of a simple sum we get:
6853 *
6854 * s_k,i
6855 * W_i,0 = \Sum_j \Prod_k w_k * ----- (9)
6856 * S_k
6857 *
6858 * Where
6859 *
6860 * s_k,i = \Sum_j w_i,j,k and S_k = \Sum_i s_k,i (10)
6861 *
6862 * w_i,j,k is the weight of the j-th runnable task in the k-th cgroup on cpu i.
6863 *
6864 * The big problem is S_k, its a global sum needed to compute a local (W_i)
6865 * property.
6866 *
6867 * [XXX write more on how we solve this.. _after_ merging pjt's patches that
6868 * rewrite all of this once again.]
Byungchul Park97a71422015-07-05 18:33:48 +09006869 */
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02006870
Hiroshi Shimamotoed387b72012-01-31 11:40:32 +09006871static unsigned long __read_mostly max_load_balance_interval = HZ/10;
6872
Peter Zijlstra0ec8aa02013-10-07 11:29:33 +01006873enum fbq_type { regular, remote, all };
6874
Peter Zijlstraddcdf6e2012-02-22 19:27:40 +01006875#define LBF_ALL_PINNED 0x01
Peter Zijlstra367456c2012-02-20 21:49:09 +01006876#define LBF_NEED_BREAK 0x02
Peter Zijlstra62633222013-08-19 12:41:09 +02006877#define LBF_DST_PINNED 0x04
6878#define LBF_SOME_PINNED 0x08
Peter Zijlstraddcdf6e2012-02-22 19:27:40 +01006879
6880struct lb_env {
6881 struct sched_domain *sd;
6882
Peter Zijlstraddcdf6e2012-02-22 19:27:40 +01006883 struct rq *src_rq;
Prashanth Nageshappa85c1e7d2012-06-19 17:47:34 +05306884 int src_cpu;
Peter Zijlstraddcdf6e2012-02-22 19:27:40 +01006885
6886 int dst_cpu;
6887 struct rq *dst_rq;
6888
Srivatsa Vaddagiri88b8dac2012-06-19 17:43:15 +05306889 struct cpumask *dst_grpmask;
6890 int new_dst_cpu;
Peter Zijlstraddcdf6e2012-02-22 19:27:40 +01006891 enum cpu_idle_type idle;
Peter Zijlstrabd939f42012-05-02 14:20:37 +02006892 long imbalance;
Michael Wangb94031302012-07-12 16:10:13 +08006893 /* The set of CPUs under consideration for load-balancing */
6894 struct cpumask *cpus;
6895
Peter Zijlstraddcdf6e2012-02-22 19:27:40 +01006896 unsigned int flags;
Peter Zijlstra367456c2012-02-20 21:49:09 +01006897
6898 unsigned int loop;
6899 unsigned int loop_break;
6900 unsigned int loop_max;
Peter Zijlstra0ec8aa02013-10-07 11:29:33 +01006901
6902 enum fbq_type fbq_type;
Kirill Tkhai163122b2014-08-20 13:48:29 +04006903 struct list_head tasks;
Peter Zijlstraddcdf6e2012-02-22 19:27:40 +01006904};
6905
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01006906/*
Peter Zijlstra029632f2011-10-25 10:00:11 +02006907 * Is this task likely cache-hot:
6908 */
Hillf Danton5d5e2b12014-06-10 10:58:43 +02006909static int task_hot(struct task_struct *p, struct lb_env *env)
Peter Zijlstra029632f2011-10-25 10:00:11 +02006910{
6911 s64 delta;
6912
Kirill Tkhaie5673f22014-08-20 13:48:01 +04006913 lockdep_assert_held(&env->src_rq->lock);
6914
Peter Zijlstra029632f2011-10-25 10:00:11 +02006915 if (p->sched_class != &fair_sched_class)
6916 return 0;
6917
6918 if (unlikely(p->policy == SCHED_IDLE))
6919 return 0;
6920
6921 /*
6922 * Buddy candidates are cache hot:
6923 */
Hillf Danton5d5e2b12014-06-10 10:58:43 +02006924 if (sched_feat(CACHE_HOT_BUDDY) && env->dst_rq->nr_running &&
Peter Zijlstra029632f2011-10-25 10:00:11 +02006925 (&p->se == cfs_rq_of(&p->se)->next ||
6926 &p->se == cfs_rq_of(&p->se)->last))
6927 return 1;
6928
6929 if (sysctl_sched_migration_cost == -1)
6930 return 1;
6931 if (sysctl_sched_migration_cost == 0)
6932 return 0;
6933
Hillf Danton5d5e2b12014-06-10 10:58:43 +02006934 delta = rq_clock_task(env->src_rq) - p->se.exec_start;
Peter Zijlstra029632f2011-10-25 10:00:11 +02006935
6936 return delta < (s64)sysctl_sched_migration_cost;
6937}
6938
Mel Gorman3a7053b2013-10-07 11:29:00 +01006939#ifdef CONFIG_NUMA_BALANCING
Rik van Rielc1ceac62015-05-14 22:59:36 -04006940/*
Srikar Dronamraju2a1ed242015-06-16 17:25:59 +05306941 * Returns 1, if task migration degrades locality
6942 * Returns 0, if task migration improves locality i.e migration preferred.
6943 * Returns -1, if task migration is not affected by locality.
Rik van Rielc1ceac62015-05-14 22:59:36 -04006944 */
Srikar Dronamraju2a1ed242015-06-16 17:25:59 +05306945static int migrate_degrades_locality(struct task_struct *p, struct lb_env *env)
Mel Gorman3a7053b2013-10-07 11:29:00 +01006946{
Rik van Rielb1ad0652014-05-15 13:03:06 -04006947 struct numa_group *numa_group = rcu_dereference(p->numa_group);
Rik van Rielc1ceac62015-05-14 22:59:36 -04006948 unsigned long src_faults, dst_faults;
Mel Gorman3a7053b2013-10-07 11:29:00 +01006949 int src_nid, dst_nid;
6950
Srikar Dronamraju2a595722015-08-11 21:54:21 +05306951 if (!static_branch_likely(&sched_numa_balancing))
Srikar Dronamraju2a1ed242015-06-16 17:25:59 +05306952 return -1;
6953
Srikar Dronamrajuc3b9bc52015-08-11 16:30:12 +05306954 if (!p->numa_faults || !(env->sd->flags & SD_NUMA))
Srikar Dronamraju2a1ed242015-06-16 17:25:59 +05306955 return -1;
Mel Gorman7a0f3082013-10-07 11:29:01 +01006956
6957 src_nid = cpu_to_node(env->src_cpu);
6958 dst_nid = cpu_to_node(env->dst_cpu);
6959
Mel Gorman83e1d2c2013-10-07 11:29:27 +01006960 if (src_nid == dst_nid)
Srikar Dronamraju2a1ed242015-06-16 17:25:59 +05306961 return -1;
Mel Gorman7a0f3082013-10-07 11:29:01 +01006962
Srikar Dronamraju2a1ed242015-06-16 17:25:59 +05306963 /* Migrating away from the preferred node is always bad. */
6964 if (src_nid == p->numa_preferred_nid) {
6965 if (env->src_rq->nr_running > env->src_rq->nr_preferred_running)
6966 return 1;
6967 else
6968 return -1;
6969 }
Mel Gorman83e1d2c2013-10-07 11:29:27 +01006970
Rik van Rielc1ceac62015-05-14 22:59:36 -04006971 /* Encourage migration to the preferred node. */
6972 if (dst_nid == p->numa_preferred_nid)
Srikar Dronamraju2a1ed242015-06-16 17:25:59 +05306973 return 0;
Rik van Rielc1ceac62015-05-14 22:59:36 -04006974
Rik van Riel739294f2017-06-23 12:55:27 -04006975 /* Leaving a core idle is often worse than degrading locality. */
6976 if (env->idle != CPU_NOT_IDLE)
6977 return -1;
6978
Rik van Rielc1ceac62015-05-14 22:59:36 -04006979 if (numa_group) {
6980 src_faults = group_faults(p, src_nid);
6981 dst_faults = group_faults(p, dst_nid);
6982 } else {
6983 src_faults = task_faults(p, src_nid);
6984 dst_faults = task_faults(p, dst_nid);
6985 }
6986
6987 return dst_faults < src_faults;
Mel Gorman7a0f3082013-10-07 11:29:01 +01006988}
6989
Mel Gorman3a7053b2013-10-07 11:29:00 +01006990#else
Srikar Dronamraju2a1ed242015-06-16 17:25:59 +05306991static inline int migrate_degrades_locality(struct task_struct *p,
Mel Gorman3a7053b2013-10-07 11:29:00 +01006992 struct lb_env *env)
6993{
Srikar Dronamraju2a1ed242015-06-16 17:25:59 +05306994 return -1;
Mel Gorman7a0f3082013-10-07 11:29:01 +01006995}
Mel Gorman3a7053b2013-10-07 11:29:00 +01006996#endif
6997
Peter Zijlstra029632f2011-10-25 10:00:11 +02006998/*
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01006999 * can_migrate_task - may task p from runqueue rq be migrated to this_cpu?
7000 */
7001static
Peter Zijlstra8e45cb52012-02-22 12:47:19 +01007002int can_migrate_task(struct task_struct *p, struct lb_env *env)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007003{
Srikar Dronamraju2a1ed242015-06-16 17:25:59 +05307004 int tsk_cache_hot;
Kirill Tkhaie5673f22014-08-20 13:48:01 +04007005
7006 lockdep_assert_held(&env->src_rq->lock);
7007
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007008 /*
7009 * We do not migrate tasks that are:
Joonsoo Kimd3198082013-04-23 17:27:40 +09007010 * 1) throttled_lb_pair, or
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007011 * 2) cannot be migrated to this CPU due to cpus_allowed, or
Joonsoo Kimd3198082013-04-23 17:27:40 +09007012 * 3) running (obviously), or
7013 * 4) are cache-hot on their current CPU.
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007014 */
Joonsoo Kimd3198082013-04-23 17:27:40 +09007015 if (throttled_lb_pair(task_group(p), env->src_cpu, env->dst_cpu))
7016 return 0;
7017
Ingo Molnar0c98d342017-02-05 15:38:10 +01007018 if (!cpumask_test_cpu(env->dst_cpu, &p->cpus_allowed)) {
Joonsoo Kime02e60c2013-04-23 17:27:42 +09007019 int cpu;
Srivatsa Vaddagiri88b8dac2012-06-19 17:43:15 +05307020
Josh Poimboeufae928822016-06-17 12:43:24 -05007021 schedstat_inc(p->se.statistics.nr_failed_migrations_affine);
Srivatsa Vaddagiri88b8dac2012-06-19 17:43:15 +05307022
Peter Zijlstra62633222013-08-19 12:41:09 +02007023 env->flags |= LBF_SOME_PINNED;
7024
Srivatsa Vaddagiri88b8dac2012-06-19 17:43:15 +05307025 /*
7026 * Remember if this task can be migrated to any other cpu in
7027 * our sched_group. We may want to revisit it if we couldn't
7028 * meet load balance goals by pulling other tasks on src_cpu.
7029 *
Jeffrey Hugo65a44332017-06-07 13:18:57 -06007030 * Avoid computing new_dst_cpu for NEWLY_IDLE or if we have
7031 * already computed one in current iteration.
Srivatsa Vaddagiri88b8dac2012-06-19 17:43:15 +05307032 */
Jeffrey Hugo65a44332017-06-07 13:18:57 -06007033 if (env->idle == CPU_NEWLY_IDLE || (env->flags & LBF_DST_PINNED))
Srivatsa Vaddagiri88b8dac2012-06-19 17:43:15 +05307034 return 0;
7035
Joonsoo Kime02e60c2013-04-23 17:27:42 +09007036 /* Prevent to re-select dst_cpu via env's cpus */
7037 for_each_cpu_and(cpu, env->dst_grpmask, env->cpus) {
Ingo Molnar0c98d342017-02-05 15:38:10 +01007038 if (cpumask_test_cpu(cpu, &p->cpus_allowed)) {
Peter Zijlstra62633222013-08-19 12:41:09 +02007039 env->flags |= LBF_DST_PINNED;
Joonsoo Kime02e60c2013-04-23 17:27:42 +09007040 env->new_dst_cpu = cpu;
7041 break;
7042 }
Srivatsa Vaddagiri88b8dac2012-06-19 17:43:15 +05307043 }
Joonsoo Kime02e60c2013-04-23 17:27:42 +09007044
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007045 return 0;
7046 }
Srivatsa Vaddagiri88b8dac2012-06-19 17:43:15 +05307047
7048 /* Record that we found atleast one task that could run on dst_cpu */
Peter Zijlstra8e45cb52012-02-22 12:47:19 +01007049 env->flags &= ~LBF_ALL_PINNED;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007050
Peter Zijlstraddcdf6e2012-02-22 19:27:40 +01007051 if (task_running(env->src_rq, p)) {
Josh Poimboeufae928822016-06-17 12:43:24 -05007052 schedstat_inc(p->se.statistics.nr_failed_migrations_running);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007053 return 0;
7054 }
7055
7056 /*
7057 * Aggressive migration if:
Mel Gorman3a7053b2013-10-07 11:29:00 +01007058 * 1) destination numa is preferred
7059 * 2) task is cache cold, or
7060 * 3) too many balance attempts have failed.
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007061 */
Srikar Dronamraju2a1ed242015-06-16 17:25:59 +05307062 tsk_cache_hot = migrate_degrades_locality(p, env);
7063 if (tsk_cache_hot == -1)
7064 tsk_cache_hot = task_hot(p, env);
Mel Gorman3a7053b2013-10-07 11:29:00 +01007065
Srikar Dronamraju2a1ed242015-06-16 17:25:59 +05307066 if (tsk_cache_hot <= 0 ||
Kirill Tkhai7a96c232014-09-22 22:36:12 +04007067 env->sd->nr_balance_failed > env->sd->cache_nice_tries) {
Srikar Dronamraju2a1ed242015-06-16 17:25:59 +05307068 if (tsk_cache_hot == 1) {
Josh Poimboeufae928822016-06-17 12:43:24 -05007069 schedstat_inc(env->sd->lb_hot_gained[env->idle]);
7070 schedstat_inc(p->se.statistics.nr_forced_migrations);
Mel Gorman3a7053b2013-10-07 11:29:00 +01007071 }
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007072 return 1;
7073 }
7074
Josh Poimboeufae928822016-06-17 12:43:24 -05007075 schedstat_inc(p->se.statistics.nr_failed_migrations_hot);
Zhang Hang4e2dcb72013-04-10 14:04:55 +08007076 return 0;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007077}
7078
Peter Zijlstra897c3952009-12-17 17:45:42 +01007079/*
Kirill Tkhai163122b2014-08-20 13:48:29 +04007080 * detach_task() -- detach the task for the migration specified in env
Peter Zijlstra897c3952009-12-17 17:45:42 +01007081 */
Kirill Tkhai163122b2014-08-20 13:48:29 +04007082static void detach_task(struct task_struct *p, struct lb_env *env)
7083{
7084 lockdep_assert_held(&env->src_rq->lock);
7085
Kirill Tkhai163122b2014-08-20 13:48:29 +04007086 p->on_rq = TASK_ON_RQ_MIGRATING;
Peter Zijlstra5704ac02017-02-21 17:15:21 +01007087 deactivate_task(env->src_rq, p, DEQUEUE_NOCLOCK);
Kirill Tkhai163122b2014-08-20 13:48:29 +04007088 set_task_cpu(p, env->dst_cpu);
7089}
7090
7091/*
Kirill Tkhaie5673f22014-08-20 13:48:01 +04007092 * detach_one_task() -- tries to dequeue exactly one task from env->src_rq, as
Peter Zijlstra897c3952009-12-17 17:45:42 +01007093 * part of active balancing operations within "domain".
Peter Zijlstra897c3952009-12-17 17:45:42 +01007094 *
Kirill Tkhaie5673f22014-08-20 13:48:01 +04007095 * Returns a task if successful and NULL otherwise.
Peter Zijlstra897c3952009-12-17 17:45:42 +01007096 */
Kirill Tkhaie5673f22014-08-20 13:48:01 +04007097static struct task_struct *detach_one_task(struct lb_env *env)
Peter Zijlstra897c3952009-12-17 17:45:42 +01007098{
Uladzislau Rezki93824902017-09-13 12:24:30 +02007099 struct task_struct *p;
Peter Zijlstra897c3952009-12-17 17:45:42 +01007100
Kirill Tkhaie5673f22014-08-20 13:48:01 +04007101 lockdep_assert_held(&env->src_rq->lock);
7102
Uladzislau Rezki93824902017-09-13 12:24:30 +02007103 list_for_each_entry_reverse(p,
7104 &env->src_rq->cfs_tasks, se.group_node) {
Peter Zijlstra367456c2012-02-20 21:49:09 +01007105 if (!can_migrate_task(p, env))
7106 continue;
Peter Zijlstra897c3952009-12-17 17:45:42 +01007107
Kirill Tkhai163122b2014-08-20 13:48:29 +04007108 detach_task(p, env);
Kirill Tkhaie5673f22014-08-20 13:48:01 +04007109
Peter Zijlstra367456c2012-02-20 21:49:09 +01007110 /*
Kirill Tkhaie5673f22014-08-20 13:48:01 +04007111 * Right now, this is only the second place where
Kirill Tkhai163122b2014-08-20 13:48:29 +04007112 * lb_gained[env->idle] is updated (other is detach_tasks)
Kirill Tkhaie5673f22014-08-20 13:48:01 +04007113 * so we can safely collect stats here rather than
Kirill Tkhai163122b2014-08-20 13:48:29 +04007114 * inside detach_tasks().
Peter Zijlstra367456c2012-02-20 21:49:09 +01007115 */
Josh Poimboeufae928822016-06-17 12:43:24 -05007116 schedstat_inc(env->sd->lb_gained[env->idle]);
Kirill Tkhaie5673f22014-08-20 13:48:01 +04007117 return p;
Peter Zijlstra897c3952009-12-17 17:45:42 +01007118 }
Kirill Tkhaie5673f22014-08-20 13:48:01 +04007119 return NULL;
Peter Zijlstra897c3952009-12-17 17:45:42 +01007120}
7121
Peter Zijlstraeb953082012-04-17 13:38:40 +02007122static const unsigned int sched_nr_migrate_break = 32;
7123
Peter Zijlstra5d6523e2012-03-10 00:07:36 +01007124/*
Kirill Tkhai163122b2014-08-20 13:48:29 +04007125 * detach_tasks() -- tries to detach up to imbalance weighted load from
7126 * busiest_rq, as part of a balancing operation within domain "sd".
Peter Zijlstra5d6523e2012-03-10 00:07:36 +01007127 *
Kirill Tkhai163122b2014-08-20 13:48:29 +04007128 * Returns number of detached tasks if successful and 0 otherwise.
Peter Zijlstra5d6523e2012-03-10 00:07:36 +01007129 */
Kirill Tkhai163122b2014-08-20 13:48:29 +04007130static int detach_tasks(struct lb_env *env)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007131{
Peter Zijlstra5d6523e2012-03-10 00:07:36 +01007132 struct list_head *tasks = &env->src_rq->cfs_tasks;
7133 struct task_struct *p;
Peter Zijlstra367456c2012-02-20 21:49:09 +01007134 unsigned long load;
Kirill Tkhai163122b2014-08-20 13:48:29 +04007135 int detached = 0;
7136
7137 lockdep_assert_held(&env->src_rq->lock);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007138
Peter Zijlstrabd939f42012-05-02 14:20:37 +02007139 if (env->imbalance <= 0)
Peter Zijlstra5d6523e2012-03-10 00:07:36 +01007140 return 0;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007141
Peter Zijlstra5d6523e2012-03-10 00:07:36 +01007142 while (!list_empty(tasks)) {
Yuyang Du985d3a42015-07-06 06:11:51 +08007143 /*
7144 * We don't want to steal all, otherwise we may be treated likewise,
7145 * which could at worst lead to a livelock crash.
7146 */
7147 if (env->idle != CPU_NOT_IDLE && env->src_rq->nr_running <= 1)
7148 break;
7149
Uladzislau Rezki93824902017-09-13 12:24:30 +02007150 p = list_last_entry(tasks, struct task_struct, se.group_node);
Peter Zijlstra5d6523e2012-03-10 00:07:36 +01007151
Peter Zijlstra367456c2012-02-20 21:49:09 +01007152 env->loop++;
7153 /* We've more or less seen every task there is, call it quits */
Peter Zijlstra5d6523e2012-03-10 00:07:36 +01007154 if (env->loop > env->loop_max)
Peter Zijlstra367456c2012-02-20 21:49:09 +01007155 break;
Peter Zijlstra5d6523e2012-03-10 00:07:36 +01007156
7157 /* take a breather every nr_migrate tasks */
Peter Zijlstra367456c2012-02-20 21:49:09 +01007158 if (env->loop > env->loop_break) {
Peter Zijlstraeb953082012-04-17 13:38:40 +02007159 env->loop_break += sched_nr_migrate_break;
Peter Zijlstra8e45cb52012-02-22 12:47:19 +01007160 env->flags |= LBF_NEED_BREAK;
Peter Zijlstraee00e662009-12-17 17:25:20 +01007161 break;
Peter Zijlstraa195f002011-09-22 15:30:18 +02007162 }
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007163
Joonsoo Kimd3198082013-04-23 17:27:40 +09007164 if (!can_migrate_task(p, env))
Peter Zijlstra367456c2012-02-20 21:49:09 +01007165 goto next;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007166
Peter Zijlstra367456c2012-02-20 21:49:09 +01007167 load = task_h_load(p);
Peter Zijlstra5d6523e2012-03-10 00:07:36 +01007168
Peter Zijlstraeb953082012-04-17 13:38:40 +02007169 if (sched_feat(LB_MIN) && load < 16 && !env->sd->nr_balance_failed)
Peter Zijlstra367456c2012-02-20 21:49:09 +01007170 goto next;
7171
Peter Zijlstrabd939f42012-05-02 14:20:37 +02007172 if ((load / 2) > env->imbalance)
Peter Zijlstra367456c2012-02-20 21:49:09 +01007173 goto next;
7174
Kirill Tkhai163122b2014-08-20 13:48:29 +04007175 detach_task(p, env);
7176 list_add(&p->se.group_node, &env->tasks);
7177
7178 detached++;
Peter Zijlstrabd939f42012-05-02 14:20:37 +02007179 env->imbalance -= load;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007180
7181#ifdef CONFIG_PREEMPT
Peter Zijlstraee00e662009-12-17 17:25:20 +01007182 /*
7183 * NEWIDLE balancing is a source of latency, so preemptible
Kirill Tkhai163122b2014-08-20 13:48:29 +04007184 * kernels will stop after the first task is detached to minimize
Peter Zijlstraee00e662009-12-17 17:25:20 +01007185 * the critical section.
7186 */
Peter Zijlstra5d6523e2012-03-10 00:07:36 +01007187 if (env->idle == CPU_NEWLY_IDLE)
Peter Zijlstraee00e662009-12-17 17:25:20 +01007188 break;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007189#endif
7190
Peter Zijlstraee00e662009-12-17 17:25:20 +01007191 /*
7192 * We only want to steal up to the prescribed amount of
7193 * weighted load.
7194 */
Peter Zijlstrabd939f42012-05-02 14:20:37 +02007195 if (env->imbalance <= 0)
Peter Zijlstraee00e662009-12-17 17:25:20 +01007196 break;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007197
Peter Zijlstra367456c2012-02-20 21:49:09 +01007198 continue;
7199next:
Uladzislau Rezki93824902017-09-13 12:24:30 +02007200 list_move(&p->se.group_node, tasks);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007201 }
Peter Zijlstra5d6523e2012-03-10 00:07:36 +01007202
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007203 /*
Kirill Tkhai163122b2014-08-20 13:48:29 +04007204 * Right now, this is one of only two places we collect this stat
7205 * so we can safely collect detach_one_task() stats here rather
7206 * than inside detach_one_task().
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007207 */
Josh Poimboeufae928822016-06-17 12:43:24 -05007208 schedstat_add(env->sd->lb_gained[env->idle], detached);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007209
Kirill Tkhai163122b2014-08-20 13:48:29 +04007210 return detached;
7211}
7212
7213/*
7214 * attach_task() -- attach the task detached by detach_task() to its new rq.
7215 */
7216static void attach_task(struct rq *rq, struct task_struct *p)
7217{
7218 lockdep_assert_held(&rq->lock);
7219
7220 BUG_ON(task_rq(p) != rq);
Peter Zijlstra5704ac02017-02-21 17:15:21 +01007221 activate_task(rq, p, ENQUEUE_NOCLOCK);
Joonwoo Park3ea94de2015-11-12 19:38:54 -08007222 p->on_rq = TASK_ON_RQ_QUEUED;
Kirill Tkhai163122b2014-08-20 13:48:29 +04007223 check_preempt_curr(rq, p, 0);
7224}
7225
7226/*
7227 * attach_one_task() -- attaches the task returned from detach_one_task() to
7228 * its new rq.
7229 */
7230static void attach_one_task(struct rq *rq, struct task_struct *p)
7231{
Peter Zijlstra8a8c69c2016-10-04 16:04:35 +02007232 struct rq_flags rf;
7233
7234 rq_lock(rq, &rf);
Peter Zijlstra5704ac02017-02-21 17:15:21 +01007235 update_rq_clock(rq);
Kirill Tkhai163122b2014-08-20 13:48:29 +04007236 attach_task(rq, p);
Peter Zijlstra8a8c69c2016-10-04 16:04:35 +02007237 rq_unlock(rq, &rf);
Kirill Tkhai163122b2014-08-20 13:48:29 +04007238}
7239
7240/*
7241 * attach_tasks() -- attaches all tasks detached by detach_tasks() to their
7242 * new rq.
7243 */
7244static void attach_tasks(struct lb_env *env)
7245{
7246 struct list_head *tasks = &env->tasks;
7247 struct task_struct *p;
Peter Zijlstra8a8c69c2016-10-04 16:04:35 +02007248 struct rq_flags rf;
Kirill Tkhai163122b2014-08-20 13:48:29 +04007249
Peter Zijlstra8a8c69c2016-10-04 16:04:35 +02007250 rq_lock(env->dst_rq, &rf);
Peter Zijlstra5704ac02017-02-21 17:15:21 +01007251 update_rq_clock(env->dst_rq);
Kirill Tkhai163122b2014-08-20 13:48:29 +04007252
7253 while (!list_empty(tasks)) {
7254 p = list_first_entry(tasks, struct task_struct, se.group_node);
7255 list_del_init(&p->se.group_node);
7256
7257 attach_task(env->dst_rq, p);
7258 }
7259
Peter Zijlstra8a8c69c2016-10-04 16:04:35 +02007260 rq_unlock(env->dst_rq, &rf);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007261}
7262
Peter Zijlstra230059de2009-12-17 17:47:12 +01007263#ifdef CONFIG_FAIR_GROUP_SCHED
Tejun Heoa9e7f652017-04-25 17:43:50 -07007264
7265static inline bool cfs_rq_is_decayed(struct cfs_rq *cfs_rq)
7266{
7267 if (cfs_rq->load.weight)
7268 return false;
7269
7270 if (cfs_rq->avg.load_sum)
7271 return false;
7272
7273 if (cfs_rq->avg.util_sum)
7274 return false;
7275
Peter Zijlstra1ea6c462017-05-06 15:59:54 +02007276 if (cfs_rq->avg.runnable_load_sum)
Tejun Heoa9e7f652017-04-25 17:43:50 -07007277 return false;
7278
7279 return true;
7280}
7281
Paul Turner48a16752012-10-04 13:18:31 +02007282static void update_blocked_averages(int cpu)
Peter Zijlstra9e3081c2010-11-15 15:47:02 -08007283{
Peter Zijlstra9e3081c2010-11-15 15:47:02 -08007284 struct rq *rq = cpu_rq(cpu);
Tejun Heoa9e7f652017-04-25 17:43:50 -07007285 struct cfs_rq *cfs_rq, *pos;
Peter Zijlstra8a8c69c2016-10-04 16:04:35 +02007286 struct rq_flags rf;
Peter Zijlstra9e3081c2010-11-15 15:47:02 -08007287
Peter Zijlstra8a8c69c2016-10-04 16:04:35 +02007288 rq_lock_irqsave(rq, &rf);
Paul Turner48a16752012-10-04 13:18:31 +02007289 update_rq_clock(rq);
Yuyang Du9d89c252015-07-15 08:04:37 +08007290
Peter Zijlstra9763b672011-07-13 13:09:25 +02007291 /*
7292 * Iterates the task_group tree in a bottom up fashion, see
7293 * list_add_leaf_cfs_rq() for details.
7294 */
Tejun Heoa9e7f652017-04-25 17:43:50 -07007295 for_each_leaf_cfs_rq_safe(rq, cfs_rq, pos) {
Vincent Guittotbc427892017-03-17 14:47:22 +01007296 struct sched_entity *se;
7297
Yuyang Du9d89c252015-07-15 08:04:37 +08007298 /* throttled entities do not contribute to load */
7299 if (throttled_hierarchy(cfs_rq))
7300 continue;
Paul Turner48a16752012-10-04 13:18:31 +02007301
Viresh Kumar3a123bb2017-05-24 10:59:56 +05307302 if (update_cfs_rq_load_avg(cfs_rq_clock_task(cfs_rq), cfs_rq))
Yuyang Du9d89c252015-07-15 08:04:37 +08007303 update_tg_load_avg(cfs_rq, 0);
Vincent Guittot4e516072016-11-08 10:53:46 +01007304
Vincent Guittotbc427892017-03-17 14:47:22 +01007305 /* Propagate pending load changes to the parent, if any: */
7306 se = cfs_rq->tg->se[cpu];
7307 if (se && !skip_blocked_update(se))
Peter Zijlstra88c06162017-05-06 17:32:43 +02007308 update_load_avg(cfs_rq_of(se), se, 0);
Tejun Heoa9e7f652017-04-25 17:43:50 -07007309
7310 /*
7311 * There can be a lot of idle CPU cgroups. Don't let fully
7312 * decayed cfs_rqs linger on the list.
7313 */
7314 if (cfs_rq_is_decayed(cfs_rq))
7315 list_del_leaf_cfs_rq(cfs_rq);
Yuyang Du9d89c252015-07-15 08:04:37 +08007316 }
Peter Zijlstra8a8c69c2016-10-04 16:04:35 +02007317 rq_unlock_irqrestore(rq, &rf);
Peter Zijlstra9e3081c2010-11-15 15:47:02 -08007318}
7319
Peter Zijlstra9763b672011-07-13 13:09:25 +02007320/*
Vladimir Davydov68520792013-07-15 17:49:19 +04007321 * Compute the hierarchical load factor for cfs_rq and all its ascendants.
Peter Zijlstra9763b672011-07-13 13:09:25 +02007322 * This needs to be done in a top-down fashion because the load of a child
7323 * group is a fraction of its parents load.
7324 */
Vladimir Davydov68520792013-07-15 17:49:19 +04007325static void update_cfs_rq_h_load(struct cfs_rq *cfs_rq)
Peter Zijlstra9763b672011-07-13 13:09:25 +02007326{
Vladimir Davydov68520792013-07-15 17:49:19 +04007327 struct rq *rq = rq_of(cfs_rq);
7328 struct sched_entity *se = cfs_rq->tg->se[cpu_of(rq)];
Peter Zijlstraa35b6462012-08-08 21:46:40 +02007329 unsigned long now = jiffies;
Vladimir Davydov68520792013-07-15 17:49:19 +04007330 unsigned long load;
Peter Zijlstraa35b6462012-08-08 21:46:40 +02007331
Vladimir Davydov68520792013-07-15 17:49:19 +04007332 if (cfs_rq->last_h_load_update == now)
Peter Zijlstraa35b6462012-08-08 21:46:40 +02007333 return;
7334
Vladimir Davydov68520792013-07-15 17:49:19 +04007335 cfs_rq->h_load_next = NULL;
7336 for_each_sched_entity(se) {
7337 cfs_rq = cfs_rq_of(se);
7338 cfs_rq->h_load_next = se;
7339 if (cfs_rq->last_h_load_update == now)
7340 break;
7341 }
Peter Zijlstraa35b6462012-08-08 21:46:40 +02007342
Vladimir Davydov68520792013-07-15 17:49:19 +04007343 if (!se) {
Yuyang Du7ea241a2015-07-15 08:04:42 +08007344 cfs_rq->h_load = cfs_rq_load_avg(cfs_rq);
Vladimir Davydov68520792013-07-15 17:49:19 +04007345 cfs_rq->last_h_load_update = now;
7346 }
7347
7348 while ((se = cfs_rq->h_load_next) != NULL) {
7349 load = cfs_rq->h_load;
Yuyang Du7ea241a2015-07-15 08:04:42 +08007350 load = div64_ul(load * se->avg.load_avg,
7351 cfs_rq_load_avg(cfs_rq) + 1);
Vladimir Davydov68520792013-07-15 17:49:19 +04007352 cfs_rq = group_cfs_rq(se);
7353 cfs_rq->h_load = load;
7354 cfs_rq->last_h_load_update = now;
7355 }
Peter Zijlstra9763b672011-07-13 13:09:25 +02007356}
7357
Peter Zijlstra367456c2012-02-20 21:49:09 +01007358static unsigned long task_h_load(struct task_struct *p)
Peter Zijlstra230059de2009-12-17 17:47:12 +01007359{
Peter Zijlstra367456c2012-02-20 21:49:09 +01007360 struct cfs_rq *cfs_rq = task_cfs_rq(p);
Peter Zijlstra230059de2009-12-17 17:47:12 +01007361
Vladimir Davydov68520792013-07-15 17:49:19 +04007362 update_cfs_rq_h_load(cfs_rq);
Yuyang Du9d89c252015-07-15 08:04:37 +08007363 return div64_ul(p->se.avg.load_avg * cfs_rq->h_load,
Yuyang Du7ea241a2015-07-15 08:04:42 +08007364 cfs_rq_load_avg(cfs_rq) + 1);
Peter Zijlstra230059de2009-12-17 17:47:12 +01007365}
7366#else
Paul Turner48a16752012-10-04 13:18:31 +02007367static inline void update_blocked_averages(int cpu)
Peter Zijlstra9e3081c2010-11-15 15:47:02 -08007368{
Vincent Guittot6c1d47c2015-07-15 08:04:38 +08007369 struct rq *rq = cpu_rq(cpu);
7370 struct cfs_rq *cfs_rq = &rq->cfs;
Peter Zijlstra8a8c69c2016-10-04 16:04:35 +02007371 struct rq_flags rf;
Vincent Guittot6c1d47c2015-07-15 08:04:38 +08007372
Peter Zijlstra8a8c69c2016-10-04 16:04:35 +02007373 rq_lock_irqsave(rq, &rf);
Vincent Guittot6c1d47c2015-07-15 08:04:38 +08007374 update_rq_clock(rq);
Viresh Kumar3a123bb2017-05-24 10:59:56 +05307375 update_cfs_rq_load_avg(cfs_rq_clock_task(cfs_rq), cfs_rq);
Peter Zijlstra8a8c69c2016-10-04 16:04:35 +02007376 rq_unlock_irqrestore(rq, &rf);
Peter Zijlstra9e3081c2010-11-15 15:47:02 -08007377}
7378
Peter Zijlstra367456c2012-02-20 21:49:09 +01007379static unsigned long task_h_load(struct task_struct *p)
7380{
Yuyang Du9d89c252015-07-15 08:04:37 +08007381 return p->se.avg.load_avg;
Peter Zijlstra230059de2009-12-17 17:47:12 +01007382}
7383#endif
7384
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007385/********** Helpers for find_busiest_group ************************/
Rik van Rielcaeb1782014-07-28 14:16:28 -04007386
7387enum group_type {
7388 group_other = 0,
7389 group_imbalanced,
7390 group_overloaded,
7391};
7392
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007393/*
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007394 * sg_lb_stats - stats of a sched_group required for load_balancing
7395 */
7396struct sg_lb_stats {
7397 unsigned long avg_load; /*Avg load across the CPUs of the group */
7398 unsigned long group_load; /* Total load over the CPUs of the group */
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007399 unsigned long sum_weighted_load; /* Weighted load of group's tasks */
Joonsoo Kim56cf5152013-08-06 17:36:43 +09007400 unsigned long load_per_task;
Nicolas Pitre63b2ca32014-05-26 18:19:37 -04007401 unsigned long group_capacity;
Dietmar Eggemann9e91d612015-08-14 17:23:12 +01007402 unsigned long group_util; /* Total utilization of the group */
Peter Zijlstra147c5fc2013-08-19 15:22:57 +02007403 unsigned int sum_nr_running; /* Nr tasks running in the group */
Peter Zijlstra147c5fc2013-08-19 15:22:57 +02007404 unsigned int idle_cpus;
7405 unsigned int group_weight;
Rik van Rielcaeb1782014-07-28 14:16:28 -04007406 enum group_type group_type;
Vincent Guittotea678212015-02-27 16:54:11 +01007407 int group_no_capacity;
Peter Zijlstra0ec8aa02013-10-07 11:29:33 +01007408#ifdef CONFIG_NUMA_BALANCING
7409 unsigned int nr_numa_running;
7410 unsigned int nr_preferred_running;
7411#endif
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007412};
7413
Joonsoo Kim56cf5152013-08-06 17:36:43 +09007414/*
7415 * sd_lb_stats - Structure to store the statistics of a sched_domain
7416 * during load balancing.
7417 */
7418struct sd_lb_stats {
7419 struct sched_group *busiest; /* Busiest group in this sd */
7420 struct sched_group *local; /* Local group in this sd */
Peter Zijlstra90001d62017-07-31 17:50:05 +02007421 unsigned long total_running;
Joonsoo Kim56cf5152013-08-06 17:36:43 +09007422 unsigned long total_load; /* Total load of all groups in sd */
Nicolas Pitre63b2ca32014-05-26 18:19:37 -04007423 unsigned long total_capacity; /* Total capacity of all groups in sd */
Joonsoo Kim56cf5152013-08-06 17:36:43 +09007424 unsigned long avg_load; /* Average load across all groups in sd */
7425
Joonsoo Kim56cf5152013-08-06 17:36:43 +09007426 struct sg_lb_stats busiest_stat;/* Statistics of the busiest group */
Peter Zijlstra147c5fc2013-08-19 15:22:57 +02007427 struct sg_lb_stats local_stat; /* Statistics of the local group */
Joonsoo Kim56cf5152013-08-06 17:36:43 +09007428};
7429
Peter Zijlstra147c5fc2013-08-19 15:22:57 +02007430static inline void init_sd_lb_stats(struct sd_lb_stats *sds)
7431{
7432 /*
7433 * Skimp on the clearing to avoid duplicate work. We can avoid clearing
7434 * local_stat because update_sg_lb_stats() does a full clear/assignment.
7435 * We must however clear busiest_stat::avg_load because
7436 * update_sd_pick_busiest() reads this before assignment.
7437 */
7438 *sds = (struct sd_lb_stats){
7439 .busiest = NULL,
7440 .local = NULL,
Peter Zijlstra90001d62017-07-31 17:50:05 +02007441 .total_running = 0UL,
Peter Zijlstra147c5fc2013-08-19 15:22:57 +02007442 .total_load = 0UL,
Nicolas Pitre63b2ca32014-05-26 18:19:37 -04007443 .total_capacity = 0UL,
Peter Zijlstra147c5fc2013-08-19 15:22:57 +02007444 .busiest_stat = {
7445 .avg_load = 0UL,
Rik van Rielcaeb1782014-07-28 14:16:28 -04007446 .sum_nr_running = 0,
7447 .group_type = group_other,
Peter Zijlstra147c5fc2013-08-19 15:22:57 +02007448 },
7449 };
7450}
7451
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007452/**
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007453 * get_sd_load_idx - Obtain the load index for a given sched domain.
7454 * @sd: The sched_domain whose load_idx is to be obtained.
Kamalesh Babulaled1b7732013-10-13 23:06:15 +05307455 * @idle: The idle status of the CPU for whose sd load_idx is obtained.
Yacine Belkadie69f6182013-07-12 20:45:47 +02007456 *
7457 * Return: The load index.
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007458 */
7459static inline int get_sd_load_idx(struct sched_domain *sd,
7460 enum cpu_idle_type idle)
7461{
7462 int load_idx;
7463
7464 switch (idle) {
7465 case CPU_NOT_IDLE:
7466 load_idx = sd->busy_idx;
7467 break;
7468
7469 case CPU_NEWLY_IDLE:
7470 load_idx = sd->newidle_idx;
7471 break;
7472 default:
7473 load_idx = sd->idle_idx;
7474 break;
7475 }
7476
7477 return load_idx;
7478}
7479
Nicolas Pitreced549f2014-05-26 18:19:38 -04007480static unsigned long scale_rt_capacity(int cpu)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007481{
7482 struct rq *rq = cpu_rq(cpu);
Vincent Guittotb5b48602015-02-27 16:54:08 +01007483 u64 total, used, age_stamp, avg;
Peter Zijlstracadefd32014-02-27 10:40:35 +01007484 s64 delta;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007485
Peter Zijlstrab654f7d2012-05-22 14:04:28 +02007486 /*
7487 * Since we're reading these variables without serialization make sure
7488 * we read them once before doing sanity checks on them.
7489 */
Jason Low316c1608d2015-04-28 13:00:20 -07007490 age_stamp = READ_ONCE(rq->age_stamp);
7491 avg = READ_ONCE(rq->rt_avg);
Peter Zijlstracebde6d2015-01-05 11:18:10 +01007492 delta = __rq_clock_broken(rq) - age_stamp;
Venkatesh Pallipadiaa483802010-10-04 17:03:22 -07007493
Peter Zijlstracadefd32014-02-27 10:40:35 +01007494 if (unlikely(delta < 0))
7495 delta = 0;
7496
7497 total = sched_avg_period() + delta;
Peter Zijlstrab654f7d2012-05-22 14:04:28 +02007498
Vincent Guittotb5b48602015-02-27 16:54:08 +01007499 used = div_u64(avg, total);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007500
Vincent Guittotb5b48602015-02-27 16:54:08 +01007501 if (likely(used < SCHED_CAPACITY_SCALE))
7502 return SCHED_CAPACITY_SCALE - used;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007503
Vincent Guittotb5b48602015-02-27 16:54:08 +01007504 return 1;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007505}
7506
Nicolas Pitreced549f2014-05-26 18:19:38 -04007507static void update_cpu_capacity(struct sched_domain *sd, int cpu)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007508{
Morten Rasmussen8cd56012015-08-14 17:23:10 +01007509 unsigned long capacity = arch_scale_cpu_capacity(sd, cpu);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007510 struct sched_group *sdg = sd->groups;
7511
Vincent Guittotca6d75e2015-02-27 16:54:09 +01007512 cpu_rq(cpu)->cpu_capacity_orig = capacity;
Srivatsa Vaddagiri9d5efe02010-06-08 14:57:02 +10007513
Nicolas Pitreced549f2014-05-26 18:19:38 -04007514 capacity *= scale_rt_capacity(cpu);
Nicolas Pitreca8ce3d2014-05-26 18:19:39 -04007515 capacity >>= SCHED_CAPACITY_SHIFT;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007516
Nicolas Pitreced549f2014-05-26 18:19:38 -04007517 if (!capacity)
7518 capacity = 1;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007519
Nicolas Pitreced549f2014-05-26 18:19:38 -04007520 cpu_rq(cpu)->cpu_capacity = capacity;
7521 sdg->sgc->capacity = capacity;
Morten Rasmussenbf475ce2016-10-14 14:41:09 +01007522 sdg->sgc->min_capacity = capacity;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007523}
7524
Nicolas Pitre63b2ca32014-05-26 18:19:37 -04007525void update_group_capacity(struct sched_domain *sd, int cpu)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007526{
7527 struct sched_domain *child = sd->child;
7528 struct sched_group *group, *sdg = sd->groups;
Morten Rasmussenbf475ce2016-10-14 14:41:09 +01007529 unsigned long capacity, min_capacity;
Vincent Guittot4ec44122011-12-12 20:21:08 +01007530 unsigned long interval;
7531
7532 interval = msecs_to_jiffies(sd->balance_interval);
7533 interval = clamp(interval, 1UL, max_load_balance_interval);
Nicolas Pitre63b2ca32014-05-26 18:19:37 -04007534 sdg->sgc->next_update = jiffies + interval;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007535
7536 if (!child) {
Nicolas Pitreced549f2014-05-26 18:19:38 -04007537 update_cpu_capacity(sd, cpu);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007538 return;
7539 }
7540
Vincent Guittotdc7ff762015-03-03 11:35:03 +01007541 capacity = 0;
Morten Rasmussenbf475ce2016-10-14 14:41:09 +01007542 min_capacity = ULONG_MAX;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007543
Peter Zijlstra74a5ce22012-05-23 18:00:43 +02007544 if (child->flags & SD_OVERLAP) {
7545 /*
7546 * SD_OVERLAP domains cannot assume that child groups
7547 * span the current group.
7548 */
7549
Peter Zijlstraae4df9d2017-05-01 11:03:12 +02007550 for_each_cpu(cpu, sched_group_span(sdg)) {
Nicolas Pitre63b2ca32014-05-26 18:19:37 -04007551 struct sched_group_capacity *sgc;
Srikar Dronamraju9abf24d2013-11-12 22:11:26 +05307552 struct rq *rq = cpu_rq(cpu);
Peter Zijlstra863bffc2013-08-28 11:44:39 +02007553
Srikar Dronamraju9abf24d2013-11-12 22:11:26 +05307554 /*
Nicolas Pitre63b2ca32014-05-26 18:19:37 -04007555 * build_sched_domains() -> init_sched_groups_capacity()
Srikar Dronamraju9abf24d2013-11-12 22:11:26 +05307556 * gets here before we've attached the domains to the
7557 * runqueues.
7558 *
Nicolas Pitreced549f2014-05-26 18:19:38 -04007559 * Use capacity_of(), which is set irrespective of domains
7560 * in update_cpu_capacity().
Srikar Dronamraju9abf24d2013-11-12 22:11:26 +05307561 *
Vincent Guittotdc7ff762015-03-03 11:35:03 +01007562 * This avoids capacity from being 0 and
Srikar Dronamraju9abf24d2013-11-12 22:11:26 +05307563 * causing divide-by-zero issues on boot.
Srikar Dronamraju9abf24d2013-11-12 22:11:26 +05307564 */
7565 if (unlikely(!rq->sd)) {
Nicolas Pitreced549f2014-05-26 18:19:38 -04007566 capacity += capacity_of(cpu);
Morten Rasmussenbf475ce2016-10-14 14:41:09 +01007567 } else {
7568 sgc = rq->sd->groups->sgc;
7569 capacity += sgc->capacity;
Srikar Dronamraju9abf24d2013-11-12 22:11:26 +05307570 }
7571
Morten Rasmussenbf475ce2016-10-14 14:41:09 +01007572 min_capacity = min(capacity, min_capacity);
Peter Zijlstra863bffc2013-08-28 11:44:39 +02007573 }
Peter Zijlstra74a5ce22012-05-23 18:00:43 +02007574 } else {
7575 /*
7576 * !SD_OVERLAP domains can assume that child groups
7577 * span the current group.
Byungchul Park97a71422015-07-05 18:33:48 +09007578 */
Peter Zijlstra74a5ce22012-05-23 18:00:43 +02007579
7580 group = child->groups;
7581 do {
Morten Rasmussenbf475ce2016-10-14 14:41:09 +01007582 struct sched_group_capacity *sgc = group->sgc;
7583
7584 capacity += sgc->capacity;
7585 min_capacity = min(sgc->min_capacity, min_capacity);
Peter Zijlstra74a5ce22012-05-23 18:00:43 +02007586 group = group->next;
7587 } while (group != child->groups);
7588 }
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007589
Nicolas Pitre63b2ca32014-05-26 18:19:37 -04007590 sdg->sgc->capacity = capacity;
Morten Rasmussenbf475ce2016-10-14 14:41:09 +01007591 sdg->sgc->min_capacity = min_capacity;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007592}
7593
Srivatsa Vaddagiri9d5efe02010-06-08 14:57:02 +10007594/*
Vincent Guittotea678212015-02-27 16:54:11 +01007595 * Check whether the capacity of the rq has been noticeably reduced by side
7596 * activity. The imbalance_pct is used for the threshold.
7597 * Return true is the capacity is reduced
Srivatsa Vaddagiri9d5efe02010-06-08 14:57:02 +10007598 */
7599static inline int
Vincent Guittotea678212015-02-27 16:54:11 +01007600check_cpu_capacity(struct rq *rq, struct sched_domain *sd)
Srivatsa Vaddagiri9d5efe02010-06-08 14:57:02 +10007601{
Vincent Guittotea678212015-02-27 16:54:11 +01007602 return ((rq->cpu_capacity * sd->imbalance_pct) <
7603 (rq->cpu_capacity_orig * 100));
Srivatsa Vaddagiri9d5efe02010-06-08 14:57:02 +10007604}
7605
Peter Zijlstra30ce5da2013-08-15 20:29:29 +02007606/*
7607 * Group imbalance indicates (and tries to solve) the problem where balancing
Ingo Molnar0c98d342017-02-05 15:38:10 +01007608 * groups is inadequate due to ->cpus_allowed constraints.
Peter Zijlstra30ce5da2013-08-15 20:29:29 +02007609 *
7610 * Imagine a situation of two groups of 4 cpus each and 4 tasks each with a
7611 * cpumask covering 1 cpu of the first group and 3 cpus of the second group.
7612 * Something like:
7613 *
Ingo Molnar2b4d5b22016-11-23 07:37:00 +01007614 * { 0 1 2 3 } { 4 5 6 7 }
7615 * * * * *
Peter Zijlstra30ce5da2013-08-15 20:29:29 +02007616 *
7617 * If we were to balance group-wise we'd place two tasks in the first group and
7618 * two tasks in the second group. Clearly this is undesired as it will overload
7619 * cpu 3 and leave one of the cpus in the second group unused.
7620 *
7621 * The current solution to this issue is detecting the skew in the first group
Peter Zijlstra62633222013-08-19 12:41:09 +02007622 * by noticing the lower domain failed to reach balance and had difficulty
7623 * moving tasks due to affinity constraints.
Peter Zijlstra30ce5da2013-08-15 20:29:29 +02007624 *
7625 * When this is so detected; this group becomes a candidate for busiest; see
Kamalesh Babulaled1b7732013-10-13 23:06:15 +05307626 * update_sd_pick_busiest(). And calculate_imbalance() and
Peter Zijlstra62633222013-08-19 12:41:09 +02007627 * find_busiest_group() avoid some of the usual balance conditions to allow it
Peter Zijlstra30ce5da2013-08-15 20:29:29 +02007628 * to create an effective group imbalance.
7629 *
7630 * This is a somewhat tricky proposition since the next run might not find the
7631 * group imbalance and decide the groups need to be balanced again. A most
7632 * subtle and fragile situation.
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007633 */
Peter Zijlstra30ce5da2013-08-15 20:29:29 +02007634
Peter Zijlstra62633222013-08-19 12:41:09 +02007635static inline int sg_imbalanced(struct sched_group *group)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007636{
Nicolas Pitre63b2ca32014-05-26 18:19:37 -04007637 return group->sgc->imbalance;
Peter Zijlstra30ce5da2013-08-15 20:29:29 +02007638}
7639
Peter Zijlstrab37d9312013-08-28 11:50:34 +02007640/*
Vincent Guittotea678212015-02-27 16:54:11 +01007641 * group_has_capacity returns true if the group has spare capacity that could
7642 * be used by some tasks.
7643 * We consider that a group has spare capacity if the * number of task is
Dietmar Eggemann9e91d612015-08-14 17:23:12 +01007644 * smaller than the number of CPUs or if the utilization is lower than the
7645 * available capacity for CFS tasks.
Vincent Guittotea678212015-02-27 16:54:11 +01007646 * For the latter, we use a threshold to stabilize the state, to take into
7647 * account the variance of the tasks' load and to return true if the available
7648 * capacity in meaningful for the load balancer.
7649 * As an example, an available capacity of 1% can appear but it doesn't make
7650 * any benefit for the load balance.
Peter Zijlstrab37d9312013-08-28 11:50:34 +02007651 */
Vincent Guittotea678212015-02-27 16:54:11 +01007652static inline bool
7653group_has_capacity(struct lb_env *env, struct sg_lb_stats *sgs)
Peter Zijlstrab37d9312013-08-28 11:50:34 +02007654{
Vincent Guittotea678212015-02-27 16:54:11 +01007655 if (sgs->sum_nr_running < sgs->group_weight)
7656 return true;
Peter Zijlstrab37d9312013-08-28 11:50:34 +02007657
Vincent Guittotea678212015-02-27 16:54:11 +01007658 if ((sgs->group_capacity * 100) >
Dietmar Eggemann9e91d612015-08-14 17:23:12 +01007659 (sgs->group_util * env->sd->imbalance_pct))
Vincent Guittotea678212015-02-27 16:54:11 +01007660 return true;
Peter Zijlstrab37d9312013-08-28 11:50:34 +02007661
Vincent Guittotea678212015-02-27 16:54:11 +01007662 return false;
Peter Zijlstrab37d9312013-08-28 11:50:34 +02007663}
7664
Vincent Guittotea678212015-02-27 16:54:11 +01007665/*
7666 * group_is_overloaded returns true if the group has more tasks than it can
7667 * handle.
7668 * group_is_overloaded is not equals to !group_has_capacity because a group
7669 * with the exact right number of tasks, has no more spare capacity but is not
7670 * overloaded so both group_has_capacity and group_is_overloaded return
7671 * false.
7672 */
7673static inline bool
7674group_is_overloaded(struct lb_env *env, struct sg_lb_stats *sgs)
Rik van Rielcaeb1782014-07-28 14:16:28 -04007675{
Vincent Guittotea678212015-02-27 16:54:11 +01007676 if (sgs->sum_nr_running <= sgs->group_weight)
7677 return false;
7678
7679 if ((sgs->group_capacity * 100) <
Dietmar Eggemann9e91d612015-08-14 17:23:12 +01007680 (sgs->group_util * env->sd->imbalance_pct))
Vincent Guittotea678212015-02-27 16:54:11 +01007681 return true;
7682
7683 return false;
7684}
7685
Morten Rasmussen9e0994c2016-10-14 14:41:10 +01007686/*
7687 * group_smaller_cpu_capacity: Returns true if sched_group sg has smaller
7688 * per-CPU capacity than sched_group ref.
7689 */
7690static inline bool
7691group_smaller_cpu_capacity(struct sched_group *sg, struct sched_group *ref)
7692{
7693 return sg->sgc->min_capacity * capacity_margin <
7694 ref->sgc->min_capacity * 1024;
7695}
7696
Leo Yan79a89f92015-09-15 18:56:45 +08007697static inline enum
7698group_type group_classify(struct sched_group *group,
7699 struct sg_lb_stats *sgs)
Vincent Guittotea678212015-02-27 16:54:11 +01007700{
7701 if (sgs->group_no_capacity)
Rik van Rielcaeb1782014-07-28 14:16:28 -04007702 return group_overloaded;
7703
7704 if (sg_imbalanced(group))
7705 return group_imbalanced;
7706
7707 return group_other;
7708}
7709
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007710/**
7711 * update_sg_lb_stats - Update sched_group's statistics for load balancing.
7712 * @env: The load balancing environment.
7713 * @group: sched_group whose statistics are to be updated.
7714 * @load_idx: Load index of sched_domain of this_cpu for load calc.
7715 * @local_group: Does group contain this_cpu.
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007716 * @sgs: variable to hold the statistics for this group.
Masanari Iidacd3bd4e2014-07-28 12:38:06 +09007717 * @overload: Indicate more than one runnable task for any CPU.
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007718 */
7719static inline void update_sg_lb_stats(struct lb_env *env,
7720 struct sched_group *group, int load_idx,
Tim Chen4486edd2014-06-23 12:16:49 -07007721 int local_group, struct sg_lb_stats *sgs,
7722 bool *overload)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007723{
Peter Zijlstra30ce5da2013-08-15 20:29:29 +02007724 unsigned long load;
Waiman Longa426f992015-11-25 14:09:38 -05007725 int i, nr_running;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007726
Peter Zijlstrab72ff132013-08-28 10:32:32 +02007727 memset(sgs, 0, sizeof(*sgs));
7728
Peter Zijlstraae4df9d2017-05-01 11:03:12 +02007729 for_each_cpu_and(i, sched_group_span(group), env->cpus) {
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007730 struct rq *rq = cpu_rq(i);
7731
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007732 /* Bias balancing toward cpus of our domain */
Peter Zijlstra62633222013-08-19 12:41:09 +02007733 if (local_group)
Peter Zijlstra04f733b2012-05-11 00:12:02 +02007734 load = target_load(i, load_idx);
Peter Zijlstra62633222013-08-19 12:41:09 +02007735 else
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007736 load = source_load(i, load_idx);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007737
7738 sgs->group_load += load;
Dietmar Eggemann9e91d612015-08-14 17:23:12 +01007739 sgs->group_util += cpu_util(i);
Vincent Guittot65fdac02014-08-26 13:06:46 +02007740 sgs->sum_nr_running += rq->cfs.h_nr_running;
Tim Chen4486edd2014-06-23 12:16:49 -07007741
Waiman Longa426f992015-11-25 14:09:38 -05007742 nr_running = rq->nr_running;
7743 if (nr_running > 1)
Tim Chen4486edd2014-06-23 12:16:49 -07007744 *overload = true;
7745
Peter Zijlstra0ec8aa02013-10-07 11:29:33 +01007746#ifdef CONFIG_NUMA_BALANCING
7747 sgs->nr_numa_running += rq->nr_numa_running;
7748 sgs->nr_preferred_running += rq->nr_preferred_running;
7749#endif
Viresh Kumarc7132dd2017-05-24 10:59:54 +05307750 sgs->sum_weighted_load += weighted_cpuload(rq);
Waiman Longa426f992015-11-25 14:09:38 -05007751 /*
7752 * No need to call idle_cpu() if nr_running is not 0
7753 */
7754 if (!nr_running && idle_cpu(i))
Suresh Siddhaaae6d3d2010-09-17 15:02:32 -07007755 sgs->idle_cpus++;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007756 }
7757
Nicolas Pitre63b2ca32014-05-26 18:19:37 -04007758 /* Adjust by relative CPU capacity of the group */
7759 sgs->group_capacity = group->sgc->capacity;
Nicolas Pitreca8ce3d2014-05-26 18:19:39 -04007760 sgs->avg_load = (sgs->group_load*SCHED_CAPACITY_SCALE) / sgs->group_capacity;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007761
Suresh Siddhadd5feea2010-02-23 16:13:52 -08007762 if (sgs->sum_nr_running)
Peter Zijlstra38d0f772013-08-15 19:47:56 +02007763 sgs->load_per_task = sgs->sum_weighted_load / sgs->sum_nr_running;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007764
Suresh Siddhaaae6d3d2010-09-17 15:02:32 -07007765 sgs->group_weight = group->group_weight;
Peter Zijlstrab37d9312013-08-28 11:50:34 +02007766
Vincent Guittotea678212015-02-27 16:54:11 +01007767 sgs->group_no_capacity = group_is_overloaded(env, sgs);
Leo Yan79a89f92015-09-15 18:56:45 +08007768 sgs->group_type = group_classify(group, sgs);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007769}
7770
7771/**
Michael Neuling532cb4c2010-06-08 14:57:02 +10007772 * update_sd_pick_busiest - return 1 on busiest group
Randy Dunlapcd968912012-06-08 13:18:33 -07007773 * @env: The load balancing environment.
Michael Neuling532cb4c2010-06-08 14:57:02 +10007774 * @sds: sched_domain statistics
7775 * @sg: sched_group candidate to be checked for being the busiest
Michael Neulingb6b12292010-06-10 12:06:21 +10007776 * @sgs: sched_group statistics
Michael Neuling532cb4c2010-06-08 14:57:02 +10007777 *
7778 * Determine if @sg is a busier group than the previously selected
7779 * busiest group.
Yacine Belkadie69f6182013-07-12 20:45:47 +02007780 *
7781 * Return: %true if @sg is a busier group than the previously selected
7782 * busiest group. %false otherwise.
Michael Neuling532cb4c2010-06-08 14:57:02 +10007783 */
Peter Zijlstrabd939f42012-05-02 14:20:37 +02007784static bool update_sd_pick_busiest(struct lb_env *env,
Michael Neuling532cb4c2010-06-08 14:57:02 +10007785 struct sd_lb_stats *sds,
7786 struct sched_group *sg,
Peter Zijlstrabd939f42012-05-02 14:20:37 +02007787 struct sg_lb_stats *sgs)
Michael Neuling532cb4c2010-06-08 14:57:02 +10007788{
Rik van Rielcaeb1782014-07-28 14:16:28 -04007789 struct sg_lb_stats *busiest = &sds->busiest_stat;
Michael Neuling532cb4c2010-06-08 14:57:02 +10007790
Rik van Rielcaeb1782014-07-28 14:16:28 -04007791 if (sgs->group_type > busiest->group_type)
Michael Neuling532cb4c2010-06-08 14:57:02 +10007792 return true;
7793
Rik van Rielcaeb1782014-07-28 14:16:28 -04007794 if (sgs->group_type < busiest->group_type)
7795 return false;
7796
7797 if (sgs->avg_load <= busiest->avg_load)
7798 return false;
7799
Morten Rasmussen9e0994c2016-10-14 14:41:10 +01007800 if (!(env->sd->flags & SD_ASYM_CPUCAPACITY))
7801 goto asym_packing;
7802
7803 /*
7804 * Candidate sg has no more than one task per CPU and
7805 * has higher per-CPU capacity. Migrating tasks to less
7806 * capable CPUs may harm throughput. Maximize throughput,
7807 * power/energy consequences are not considered.
7808 */
7809 if (sgs->sum_nr_running <= sgs->group_weight &&
7810 group_smaller_cpu_capacity(sds->local, sg))
7811 return false;
7812
7813asym_packing:
Rik van Rielcaeb1782014-07-28 14:16:28 -04007814 /* This is the busiest node in its class. */
7815 if (!(env->sd->flags & SD_ASYM_PACKING))
Michael Neuling532cb4c2010-06-08 14:57:02 +10007816 return true;
7817
Srikar Dronamraju1f621e02016-04-06 18:47:40 +05307818 /* No ASYM_PACKING if target cpu is already busy */
7819 if (env->idle == CPU_NOT_IDLE)
7820 return true;
Michael Neuling532cb4c2010-06-08 14:57:02 +10007821 /*
Tim Chenafe06ef2016-11-22 12:23:53 -08007822 * ASYM_PACKING needs to move all the work to the highest
7823 * prority CPUs in the group, therefore mark all groups
7824 * of lower priority than ourself as busy.
Michael Neuling532cb4c2010-06-08 14:57:02 +10007825 */
Tim Chenafe06ef2016-11-22 12:23:53 -08007826 if (sgs->sum_nr_running &&
7827 sched_asym_prefer(env->dst_cpu, sg->asym_prefer_cpu)) {
Michael Neuling532cb4c2010-06-08 14:57:02 +10007828 if (!sds->busiest)
7829 return true;
7830
Tim Chenafe06ef2016-11-22 12:23:53 -08007831 /* Prefer to move from lowest priority cpu's work */
7832 if (sched_asym_prefer(sds->busiest->asym_prefer_cpu,
7833 sg->asym_prefer_cpu))
Michael Neuling532cb4c2010-06-08 14:57:02 +10007834 return true;
7835 }
7836
7837 return false;
7838}
7839
Peter Zijlstra0ec8aa02013-10-07 11:29:33 +01007840#ifdef CONFIG_NUMA_BALANCING
7841static inline enum fbq_type fbq_classify_group(struct sg_lb_stats *sgs)
7842{
7843 if (sgs->sum_nr_running > sgs->nr_numa_running)
7844 return regular;
7845 if (sgs->sum_nr_running > sgs->nr_preferred_running)
7846 return remote;
7847 return all;
7848}
7849
7850static inline enum fbq_type fbq_classify_rq(struct rq *rq)
7851{
7852 if (rq->nr_running > rq->nr_numa_running)
7853 return regular;
7854 if (rq->nr_running > rq->nr_preferred_running)
7855 return remote;
7856 return all;
7857}
7858#else
7859static inline enum fbq_type fbq_classify_group(struct sg_lb_stats *sgs)
7860{
7861 return all;
7862}
7863
7864static inline enum fbq_type fbq_classify_rq(struct rq *rq)
7865{
7866 return regular;
7867}
7868#endif /* CONFIG_NUMA_BALANCING */
7869
Michael Neuling532cb4c2010-06-08 14:57:02 +10007870/**
Hui Kang461819a2011-10-11 23:00:59 -04007871 * update_sd_lb_stats - Update sched_domain's statistics for load balancing.
Randy Dunlapcd968912012-06-08 13:18:33 -07007872 * @env: The load balancing environment.
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007873 * @sds: variable to hold the statistics for this sched_domain.
7874 */
Peter Zijlstra0ec8aa02013-10-07 11:29:33 +01007875static inline void update_sd_lb_stats(struct lb_env *env, struct sd_lb_stats *sds)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007876{
Peter Zijlstrabd939f42012-05-02 14:20:37 +02007877 struct sched_domain *child = env->sd->child;
7878 struct sched_group *sg = env->sd->groups;
Srikar Dronamraju05b40e02017-03-22 23:27:50 +05307879 struct sg_lb_stats *local = &sds->local_stat;
Joonsoo Kim56cf5152013-08-06 17:36:43 +09007880 struct sg_lb_stats tmp_sgs;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007881 int load_idx, prefer_sibling = 0;
Tim Chen4486edd2014-06-23 12:16:49 -07007882 bool overload = false;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007883
7884 if (child && child->flags & SD_PREFER_SIBLING)
7885 prefer_sibling = 1;
7886
Peter Zijlstrabd939f42012-05-02 14:20:37 +02007887 load_idx = get_sd_load_idx(env->sd, env->idle);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007888
7889 do {
Joonsoo Kim56cf5152013-08-06 17:36:43 +09007890 struct sg_lb_stats *sgs = &tmp_sgs;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007891 int local_group;
7892
Peter Zijlstraae4df9d2017-05-01 11:03:12 +02007893 local_group = cpumask_test_cpu(env->dst_cpu, sched_group_span(sg));
Joonsoo Kim56cf5152013-08-06 17:36:43 +09007894 if (local_group) {
7895 sds->local = sg;
Srikar Dronamraju05b40e02017-03-22 23:27:50 +05307896 sgs = local;
Peter Zijlstrab72ff132013-08-28 10:32:32 +02007897
7898 if (env->idle != CPU_NEWLY_IDLE ||
Nicolas Pitre63b2ca32014-05-26 18:19:37 -04007899 time_after_eq(jiffies, sg->sgc->next_update))
7900 update_group_capacity(env->sd, env->dst_cpu);
Joonsoo Kim56cf5152013-08-06 17:36:43 +09007901 }
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007902
Tim Chen4486edd2014-06-23 12:16:49 -07007903 update_sg_lb_stats(env, sg, load_idx, local_group, sgs,
7904 &overload);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007905
Peter Zijlstrab72ff132013-08-28 10:32:32 +02007906 if (local_group)
7907 goto next_group;
7908
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007909 /*
7910 * In case the child domain prefers tasks go to siblings
Vincent Guittotea678212015-02-27 16:54:11 +01007911 * first, lower the sg capacity so that we'll try
Nikhil Rao75dd3212010-10-15 13:12:30 -07007912 * and move all the excess tasks away. We lower the capacity
7913 * of a group only if the local group has the capacity to fit
Vincent Guittotea678212015-02-27 16:54:11 +01007914 * these excess tasks. The extra check prevents the case where
7915 * you always pull from the heaviest group when it is already
7916 * under-utilized (possible with a large weight task outweighs
7917 * the tasks on the system).
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007918 */
Peter Zijlstrab72ff132013-08-28 10:32:32 +02007919 if (prefer_sibling && sds->local &&
Srikar Dronamraju05b40e02017-03-22 23:27:50 +05307920 group_has_capacity(env, local) &&
7921 (sgs->sum_nr_running > local->sum_nr_running + 1)) {
Vincent Guittotea678212015-02-27 16:54:11 +01007922 sgs->group_no_capacity = 1;
Leo Yan79a89f92015-09-15 18:56:45 +08007923 sgs->group_type = group_classify(sg, sgs);
Wanpeng Licb0b9f22014-11-05 07:44:50 +08007924 }
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007925
Peter Zijlstrab72ff132013-08-28 10:32:32 +02007926 if (update_sd_pick_busiest(env, sds, sg, sgs)) {
Michael Neuling532cb4c2010-06-08 14:57:02 +10007927 sds->busiest = sg;
Joonsoo Kim56cf5152013-08-06 17:36:43 +09007928 sds->busiest_stat = *sgs;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007929 }
7930
Peter Zijlstrab72ff132013-08-28 10:32:32 +02007931next_group:
7932 /* Now, start updating sd_lb_stats */
Peter Zijlstra90001d62017-07-31 17:50:05 +02007933 sds->total_running += sgs->sum_nr_running;
Peter Zijlstrab72ff132013-08-28 10:32:32 +02007934 sds->total_load += sgs->group_load;
Nicolas Pitre63b2ca32014-05-26 18:19:37 -04007935 sds->total_capacity += sgs->group_capacity;
Peter Zijlstrab72ff132013-08-28 10:32:32 +02007936
Michael Neuling532cb4c2010-06-08 14:57:02 +10007937 sg = sg->next;
Peter Zijlstrabd939f42012-05-02 14:20:37 +02007938 } while (sg != env->sd->groups);
Peter Zijlstra0ec8aa02013-10-07 11:29:33 +01007939
7940 if (env->sd->flags & SD_NUMA)
7941 env->fbq_type = fbq_classify_group(&sds->busiest_stat);
Tim Chen4486edd2014-06-23 12:16:49 -07007942
7943 if (!env->sd->parent) {
7944 /* update overload indicator if we are at root domain */
7945 if (env->dst_rq->rd->overload != overload)
7946 env->dst_rq->rd->overload = overload;
7947 }
Michael Neuling532cb4c2010-06-08 14:57:02 +10007948}
7949
Michael Neuling532cb4c2010-06-08 14:57:02 +10007950/**
7951 * check_asym_packing - Check to see if the group is packed into the
Masanari Iida0ba42a52017-03-07 20:48:02 +09007952 * sched domain.
Michael Neuling532cb4c2010-06-08 14:57:02 +10007953 *
7954 * This is primarily intended to used at the sibling level. Some
7955 * cores like POWER7 prefer to use lower numbered SMT threads. In the
7956 * case of POWER7, it can move to lower SMT modes only when higher
7957 * threads are idle. When in lower SMT modes, the threads will
7958 * perform better since they share less core resources. Hence when we
7959 * have idle threads, we want them to be the higher ones.
7960 *
7961 * This packing function is run on idle threads. It checks to see if
7962 * the busiest CPU in this domain (core in the P7 case) has a higher
7963 * CPU number than the packing function is being run on. Here we are
7964 * assuming lower CPU number will be equivalent to lower a SMT thread
7965 * number.
7966 *
Yacine Belkadie69f6182013-07-12 20:45:47 +02007967 * Return: 1 when packing is required and a task should be moved to
Randy Dunlap46123352017-09-10 09:55:05 -07007968 * this CPU. The amount of the imbalance is returned in env->imbalance.
Michael Neulingb6b12292010-06-10 12:06:21 +10007969 *
Randy Dunlapcd968912012-06-08 13:18:33 -07007970 * @env: The load balancing environment.
Michael Neuling532cb4c2010-06-08 14:57:02 +10007971 * @sds: Statistics of the sched_domain which is to be packed
Michael Neuling532cb4c2010-06-08 14:57:02 +10007972 */
Peter Zijlstrabd939f42012-05-02 14:20:37 +02007973static int check_asym_packing(struct lb_env *env, struct sd_lb_stats *sds)
Michael Neuling532cb4c2010-06-08 14:57:02 +10007974{
7975 int busiest_cpu;
7976
Peter Zijlstrabd939f42012-05-02 14:20:37 +02007977 if (!(env->sd->flags & SD_ASYM_PACKING))
Michael Neuling532cb4c2010-06-08 14:57:02 +10007978 return 0;
7979
Srikar Dronamraju1f621e02016-04-06 18:47:40 +05307980 if (env->idle == CPU_NOT_IDLE)
7981 return 0;
7982
Michael Neuling532cb4c2010-06-08 14:57:02 +10007983 if (!sds->busiest)
7984 return 0;
7985
Tim Chenafe06ef2016-11-22 12:23:53 -08007986 busiest_cpu = sds->busiest->asym_prefer_cpu;
7987 if (sched_asym_prefer(busiest_cpu, env->dst_cpu))
Michael Neuling532cb4c2010-06-08 14:57:02 +10007988 return 0;
7989
Peter Zijlstrabd939f42012-05-02 14:20:37 +02007990 env->imbalance = DIV_ROUND_CLOSEST(
Nicolas Pitre63b2ca32014-05-26 18:19:37 -04007991 sds->busiest_stat.avg_load * sds->busiest_stat.group_capacity,
Nicolas Pitreca8ce3d2014-05-26 18:19:39 -04007992 SCHED_CAPACITY_SCALE);
Peter Zijlstrabd939f42012-05-02 14:20:37 +02007993
Michael Neuling532cb4c2010-06-08 14:57:02 +10007994 return 1;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007995}
7996
7997/**
7998 * fix_small_imbalance - Calculate the minor imbalance that exists
7999 * amongst the groups of a sched_domain, during
8000 * load balancing.
Randy Dunlapcd968912012-06-08 13:18:33 -07008001 * @env: The load balancing environment.
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008002 * @sds: Statistics of the sched_domain whose imbalance is to be calculated.
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008003 */
Peter Zijlstrabd939f42012-05-02 14:20:37 +02008004static inline
8005void fix_small_imbalance(struct lb_env *env, struct sd_lb_stats *sds)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008006{
Nicolas Pitre63b2ca32014-05-26 18:19:37 -04008007 unsigned long tmp, capa_now = 0, capa_move = 0;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008008 unsigned int imbn = 2;
Suresh Siddhadd5feea2010-02-23 16:13:52 -08008009 unsigned long scaled_busy_load_per_task;
Joonsoo Kim56cf5152013-08-06 17:36:43 +09008010 struct sg_lb_stats *local, *busiest;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008011
Joonsoo Kim56cf5152013-08-06 17:36:43 +09008012 local = &sds->local_stat;
8013 busiest = &sds->busiest_stat;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008014
Joonsoo Kim56cf5152013-08-06 17:36:43 +09008015 if (!local->sum_nr_running)
8016 local->load_per_task = cpu_avg_load_per_task(env->dst_cpu);
8017 else if (busiest->load_per_task > local->load_per_task)
8018 imbn = 1;
Suresh Siddhadd5feea2010-02-23 16:13:52 -08008019
Joonsoo Kim56cf5152013-08-06 17:36:43 +09008020 scaled_busy_load_per_task =
Nicolas Pitreca8ce3d2014-05-26 18:19:39 -04008021 (busiest->load_per_task * SCHED_CAPACITY_SCALE) /
Nicolas Pitre63b2ca32014-05-26 18:19:37 -04008022 busiest->group_capacity;
Joonsoo Kim56cf5152013-08-06 17:36:43 +09008023
Vladimir Davydov3029ede2013-09-15 17:49:14 +04008024 if (busiest->avg_load + scaled_busy_load_per_task >=
8025 local->avg_load + (scaled_busy_load_per_task * imbn)) {
Joonsoo Kim56cf5152013-08-06 17:36:43 +09008026 env->imbalance = busiest->load_per_task;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008027 return;
8028 }
8029
8030 /*
8031 * OK, we don't have enough imbalance to justify moving tasks,
Nicolas Pitreced549f2014-05-26 18:19:38 -04008032 * however we may be able to increase total CPU capacity used by
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008033 * moving them.
8034 */
8035
Nicolas Pitre63b2ca32014-05-26 18:19:37 -04008036 capa_now += busiest->group_capacity *
Joonsoo Kim56cf5152013-08-06 17:36:43 +09008037 min(busiest->load_per_task, busiest->avg_load);
Nicolas Pitre63b2ca32014-05-26 18:19:37 -04008038 capa_now += local->group_capacity *
Joonsoo Kim56cf5152013-08-06 17:36:43 +09008039 min(local->load_per_task, local->avg_load);
Nicolas Pitreca8ce3d2014-05-26 18:19:39 -04008040 capa_now /= SCHED_CAPACITY_SCALE;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008041
8042 /* Amount of load we'd subtract */
Vincent Guittota2cd4262014-03-11 17:26:06 +01008043 if (busiest->avg_load > scaled_busy_load_per_task) {
Nicolas Pitre63b2ca32014-05-26 18:19:37 -04008044 capa_move += busiest->group_capacity *
Joonsoo Kim56cf5152013-08-06 17:36:43 +09008045 min(busiest->load_per_task,
Vincent Guittota2cd4262014-03-11 17:26:06 +01008046 busiest->avg_load - scaled_busy_load_per_task);
Joonsoo Kim56cf5152013-08-06 17:36:43 +09008047 }
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008048
8049 /* Amount of load we'd add */
Nicolas Pitre63b2ca32014-05-26 18:19:37 -04008050 if (busiest->avg_load * busiest->group_capacity <
Nicolas Pitreca8ce3d2014-05-26 18:19:39 -04008051 busiest->load_per_task * SCHED_CAPACITY_SCALE) {
Nicolas Pitre63b2ca32014-05-26 18:19:37 -04008052 tmp = (busiest->avg_load * busiest->group_capacity) /
8053 local->group_capacity;
Joonsoo Kim56cf5152013-08-06 17:36:43 +09008054 } else {
Nicolas Pitreca8ce3d2014-05-26 18:19:39 -04008055 tmp = (busiest->load_per_task * SCHED_CAPACITY_SCALE) /
Nicolas Pitre63b2ca32014-05-26 18:19:37 -04008056 local->group_capacity;
Joonsoo Kim56cf5152013-08-06 17:36:43 +09008057 }
Nicolas Pitre63b2ca32014-05-26 18:19:37 -04008058 capa_move += local->group_capacity *
Peter Zijlstra3ae11c92013-08-15 20:37:48 +02008059 min(local->load_per_task, local->avg_load + tmp);
Nicolas Pitreca8ce3d2014-05-26 18:19:39 -04008060 capa_move /= SCHED_CAPACITY_SCALE;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008061
8062 /* Move if we gain throughput */
Nicolas Pitre63b2ca32014-05-26 18:19:37 -04008063 if (capa_move > capa_now)
Joonsoo Kim56cf5152013-08-06 17:36:43 +09008064 env->imbalance = busiest->load_per_task;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008065}
8066
8067/**
8068 * calculate_imbalance - Calculate the amount of imbalance present within the
8069 * groups of a given sched_domain during load balance.
Peter Zijlstrabd939f42012-05-02 14:20:37 +02008070 * @env: load balance environment
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008071 * @sds: statistics of the sched_domain whose imbalance is to be calculated.
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008072 */
Peter Zijlstrabd939f42012-05-02 14:20:37 +02008073static inline void calculate_imbalance(struct lb_env *env, struct sd_lb_stats *sds)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008074{
Suresh Siddhadd5feea2010-02-23 16:13:52 -08008075 unsigned long max_pull, load_above_capacity = ~0UL;
Joonsoo Kim56cf5152013-08-06 17:36:43 +09008076 struct sg_lb_stats *local, *busiest;
Suresh Siddhadd5feea2010-02-23 16:13:52 -08008077
Joonsoo Kim56cf5152013-08-06 17:36:43 +09008078 local = &sds->local_stat;
Joonsoo Kim56cf5152013-08-06 17:36:43 +09008079 busiest = &sds->busiest_stat;
Joonsoo Kim56cf5152013-08-06 17:36:43 +09008080
Rik van Rielcaeb1782014-07-28 14:16:28 -04008081 if (busiest->group_type == group_imbalanced) {
Peter Zijlstra30ce5da2013-08-15 20:29:29 +02008082 /*
8083 * In the group_imb case we cannot rely on group-wide averages
8084 * to ensure cpu-load equilibrium, look at wider averages. XXX
8085 */
Joonsoo Kim56cf5152013-08-06 17:36:43 +09008086 busiest->load_per_task =
8087 min(busiest->load_per_task, sds->avg_load);
Suresh Siddhadd5feea2010-02-23 16:13:52 -08008088 }
8089
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008090 /*
Dietmar Eggemann885e5422016-04-29 20:32:39 +01008091 * Avg load of busiest sg can be less and avg load of local sg can
8092 * be greater than avg load across all sgs of sd because avg load
8093 * factors in sg capacity and sgs with smaller group_type are
8094 * skipped when updating the busiest sg:
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008095 */
Vladimir Davydovb1885552013-09-15 17:49:13 +04008096 if (busiest->avg_load <= sds->avg_load ||
8097 local->avg_load >= sds->avg_load) {
Peter Zijlstrabd939f42012-05-02 14:20:37 +02008098 env->imbalance = 0;
8099 return fix_small_imbalance(env, sds);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008100 }
8101
Peter Zijlstra9a5d9ba2014-07-29 17:15:11 +02008102 /*
8103 * If there aren't any idle cpus, avoid creating some.
8104 */
8105 if (busiest->group_type == group_overloaded &&
8106 local->group_type == group_overloaded) {
Peter Zijlstra1be0eb22016-05-06 12:21:23 +02008107 load_above_capacity = busiest->sum_nr_running * SCHED_CAPACITY_SCALE;
Morten Rasmussencfa10332016-04-29 20:32:40 +01008108 if (load_above_capacity > busiest->group_capacity) {
Vincent Guittotea678212015-02-27 16:54:11 +01008109 load_above_capacity -= busiest->group_capacity;
Dietmar Eggemann26656212016-08-10 11:27:27 +01008110 load_above_capacity *= scale_load_down(NICE_0_LOAD);
Morten Rasmussencfa10332016-04-29 20:32:40 +01008111 load_above_capacity /= busiest->group_capacity;
8112 } else
Vincent Guittotea678212015-02-27 16:54:11 +01008113 load_above_capacity = ~0UL;
Suresh Siddhadd5feea2010-02-23 16:13:52 -08008114 }
8115
8116 /*
8117 * We're trying to get all the cpus to the average_load, so we don't
8118 * want to push ourselves above the average load, nor do we wish to
8119 * reduce the max loaded cpu below the average load. At the same time,
Dietmar Eggemann0a9b23c2016-04-29 20:32:38 +01008120 * we also don't want to reduce the group load below the group
8121 * capacity. Thus we look for the minimum possible imbalance.
Suresh Siddhadd5feea2010-02-23 16:13:52 -08008122 */
Peter Zijlstra30ce5da2013-08-15 20:29:29 +02008123 max_pull = min(busiest->avg_load - sds->avg_load, load_above_capacity);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008124
8125 /* How much load to actually move to equalise the imbalance */
Joonsoo Kim56cf5152013-08-06 17:36:43 +09008126 env->imbalance = min(
Nicolas Pitre63b2ca32014-05-26 18:19:37 -04008127 max_pull * busiest->group_capacity,
8128 (sds->avg_load - local->avg_load) * local->group_capacity
Nicolas Pitreca8ce3d2014-05-26 18:19:39 -04008129 ) / SCHED_CAPACITY_SCALE;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008130
8131 /*
8132 * if *imbalance is less than the average load per runnable task
Lucas De Marchi25985ed2011-03-30 22:57:33 -03008133 * there is no guarantee that any tasks will be moved so we'll have
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008134 * a think about bumping its value to force at least one task to be
8135 * moved
8136 */
Joonsoo Kim56cf5152013-08-06 17:36:43 +09008137 if (env->imbalance < busiest->load_per_task)
Peter Zijlstrabd939f42012-05-02 14:20:37 +02008138 return fix_small_imbalance(env, sds);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008139}
Nikhil Raofab47622010-10-15 13:12:29 -07008140
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008141/******* find_busiest_group() helpers end here *********************/
8142
8143/**
8144 * find_busiest_group - Returns the busiest group within the sched_domain
Dietmar Eggemann0a9b23c2016-04-29 20:32:38 +01008145 * if there is an imbalance.
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008146 *
8147 * Also calculates the amount of weighted load which should be moved
8148 * to restore balance.
8149 *
Randy Dunlapcd968912012-06-08 13:18:33 -07008150 * @env: The load balancing environment.
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008151 *
Yacine Belkadie69f6182013-07-12 20:45:47 +02008152 * Return: - The busiest group if imbalance exists.
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008153 */
Joonsoo Kim56cf5152013-08-06 17:36:43 +09008154static struct sched_group *find_busiest_group(struct lb_env *env)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008155{
Joonsoo Kim56cf5152013-08-06 17:36:43 +09008156 struct sg_lb_stats *local, *busiest;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008157 struct sd_lb_stats sds;
8158
Peter Zijlstra147c5fc2013-08-19 15:22:57 +02008159 init_sd_lb_stats(&sds);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008160
8161 /*
8162 * Compute the various statistics relavent for load balancing at
8163 * this level.
8164 */
Joonsoo Kim23f0d202013-08-06 17:36:42 +09008165 update_sd_lb_stats(env, &sds);
Joonsoo Kim56cf5152013-08-06 17:36:43 +09008166 local = &sds.local_stat;
8167 busiest = &sds.busiest_stat;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008168
Vincent Guittotea678212015-02-27 16:54:11 +01008169 /* ASYM feature bypasses nice load balance check */
Srikar Dronamraju1f621e02016-04-06 18:47:40 +05308170 if (check_asym_packing(env, &sds))
Michael Neuling532cb4c2010-06-08 14:57:02 +10008171 return sds.busiest;
8172
Peter Zijlstracc57aa82011-02-21 18:55:32 +01008173 /* There is no busy sibling group to pull tasks from */
Joonsoo Kim56cf5152013-08-06 17:36:43 +09008174 if (!sds.busiest || busiest->sum_nr_running == 0)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008175 goto out_balanced;
8176
Peter Zijlstra90001d62017-07-31 17:50:05 +02008177 /* XXX broken for overlapping NUMA groups */
Nicolas Pitreca8ce3d2014-05-26 18:19:39 -04008178 sds.avg_load = (SCHED_CAPACITY_SCALE * sds.total_load)
8179 / sds.total_capacity;
Ken Chenb0432d82011-04-07 17:23:22 -07008180
Peter Zijlstra866ab432011-02-21 18:56:47 +01008181 /*
8182 * If the busiest group is imbalanced the below checks don't
Peter Zijlstra30ce5da2013-08-15 20:29:29 +02008183 * work because they assume all things are equal, which typically
Peter Zijlstra866ab432011-02-21 18:56:47 +01008184 * isn't true due to cpus_allowed constraints and the like.
8185 */
Rik van Rielcaeb1782014-07-28 14:16:28 -04008186 if (busiest->group_type == group_imbalanced)
Peter Zijlstra866ab432011-02-21 18:56:47 +01008187 goto force_balance;
8188
Brendan Jackman583ffd92017-10-05 11:58:54 +01008189 /*
8190 * When dst_cpu is idle, prevent SMP nice and/or asymmetric group
8191 * capacities from resulting in underutilization due to avg_load.
8192 */
8193 if (env->idle != CPU_NOT_IDLE && group_has_capacity(env, local) &&
Vincent Guittotea678212015-02-27 16:54:11 +01008194 busiest->group_no_capacity)
Nikhil Raofab47622010-10-15 13:12:29 -07008195 goto force_balance;
8196
Peter Zijlstracc57aa82011-02-21 18:55:32 +01008197 /*
Zhihui Zhang9c58c792014-09-20 21:24:36 -04008198 * If the local group is busier than the selected busiest group
Peter Zijlstracc57aa82011-02-21 18:55:32 +01008199 * don't try and pull any tasks.
8200 */
Joonsoo Kim56cf5152013-08-06 17:36:43 +09008201 if (local->avg_load >= busiest->avg_load)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008202 goto out_balanced;
8203
Peter Zijlstracc57aa82011-02-21 18:55:32 +01008204 /*
8205 * Don't pull any tasks if this group is already above the domain
8206 * average load.
8207 */
Joonsoo Kim56cf5152013-08-06 17:36:43 +09008208 if (local->avg_load >= sds.avg_load)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008209 goto out_balanced;
8210
Peter Zijlstrabd939f42012-05-02 14:20:37 +02008211 if (env->idle == CPU_IDLE) {
Suresh Siddhaaae6d3d2010-09-17 15:02:32 -07008212 /*
Vincent Guittot43f4d662014-10-01 15:38:55 +02008213 * This cpu is idle. If the busiest group is not overloaded
8214 * and there is no imbalance between this and busiest group
8215 * wrt idle cpus, it is balanced. The imbalance becomes
8216 * significant if the diff is greater than 1 otherwise we
8217 * might end up to just move the imbalance on another group
Suresh Siddhaaae6d3d2010-09-17 15:02:32 -07008218 */
Vincent Guittot43f4d662014-10-01 15:38:55 +02008219 if ((busiest->group_type != group_overloaded) &&
8220 (local->idle_cpus <= (busiest->idle_cpus + 1)))
Suresh Siddhaaae6d3d2010-09-17 15:02:32 -07008221 goto out_balanced;
Peter Zijlstrac186faf2011-02-21 18:52:53 +01008222 } else {
8223 /*
8224 * In the CPU_NEWLY_IDLE, CPU_NOT_IDLE cases, use
8225 * imbalance_pct to be conservative.
8226 */
Joonsoo Kim56cf5152013-08-06 17:36:43 +09008227 if (100 * busiest->avg_load <=
8228 env->sd->imbalance_pct * local->avg_load)
Peter Zijlstrac186faf2011-02-21 18:52:53 +01008229 goto out_balanced;
Suresh Siddhaaae6d3d2010-09-17 15:02:32 -07008230 }
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008231
Nikhil Raofab47622010-10-15 13:12:29 -07008232force_balance:
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008233 /* Looks like there is an imbalance. Compute it */
Peter Zijlstrabd939f42012-05-02 14:20:37 +02008234 calculate_imbalance(env, &sds);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008235 return sds.busiest;
8236
8237out_balanced:
Peter Zijlstrabd939f42012-05-02 14:20:37 +02008238 env->imbalance = 0;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008239 return NULL;
8240}
8241
8242/*
8243 * find_busiest_queue - find the busiest runqueue among the cpus in group.
8244 */
Peter Zijlstrabd939f42012-05-02 14:20:37 +02008245static struct rq *find_busiest_queue(struct lb_env *env,
Michael Wangb94031302012-07-12 16:10:13 +08008246 struct sched_group *group)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008247{
8248 struct rq *busiest = NULL, *rq;
Nicolas Pitreced549f2014-05-26 18:19:38 -04008249 unsigned long busiest_load = 0, busiest_capacity = 1;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008250 int i;
8251
Peter Zijlstraae4df9d2017-05-01 11:03:12 +02008252 for_each_cpu_and(i, sched_group_span(group), env->cpus) {
Vincent Guittotea678212015-02-27 16:54:11 +01008253 unsigned long capacity, wl;
Peter Zijlstra0ec8aa02013-10-07 11:29:33 +01008254 enum fbq_type rt;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008255
Peter Zijlstra0ec8aa02013-10-07 11:29:33 +01008256 rq = cpu_rq(i);
8257 rt = fbq_classify_rq(rq);
8258
8259 /*
8260 * We classify groups/runqueues into three groups:
8261 * - regular: there are !numa tasks
8262 * - remote: there are numa tasks that run on the 'wrong' node
8263 * - all: there is no distinction
8264 *
8265 * In order to avoid migrating ideally placed numa tasks,
8266 * ignore those when there's better options.
8267 *
8268 * If we ignore the actual busiest queue to migrate another
8269 * task, the next balance pass can still reduce the busiest
8270 * queue by moving tasks around inside the node.
8271 *
8272 * If we cannot move enough load due to this classification
8273 * the next pass will adjust the group classification and
8274 * allow migration of more tasks.
8275 *
8276 * Both cases only affect the total convergence complexity.
8277 */
8278 if (rt > env->fbq_type)
8279 continue;
8280
Nicolas Pitreced549f2014-05-26 18:19:38 -04008281 capacity = capacity_of(i);
Srivatsa Vaddagiri9d5efe02010-06-08 14:57:02 +10008282
Viresh Kumarc7132dd2017-05-24 10:59:54 +05308283 wl = weighted_cpuload(rq);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008284
Thomas Gleixner6e40f5b2010-02-16 16:48:56 +01008285 /*
8286 * When comparing with imbalance, use weighted_cpuload()
Nicolas Pitreced549f2014-05-26 18:19:38 -04008287 * which is not scaled with the cpu capacity.
Thomas Gleixner6e40f5b2010-02-16 16:48:56 +01008288 */
Vincent Guittotea678212015-02-27 16:54:11 +01008289
8290 if (rq->nr_running == 1 && wl > env->imbalance &&
8291 !check_cpu_capacity(rq, env->sd))
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008292 continue;
8293
Thomas Gleixner6e40f5b2010-02-16 16:48:56 +01008294 /*
8295 * For the load comparisons with the other cpu's, consider
Nicolas Pitreced549f2014-05-26 18:19:38 -04008296 * the weighted_cpuload() scaled with the cpu capacity, so
8297 * that the load can be moved away from the cpu that is
8298 * potentially running at a lower capacity.
Joonsoo Kim95a79b82013-08-06 17:36:41 +09008299 *
Nicolas Pitreced549f2014-05-26 18:19:38 -04008300 * Thus we're looking for max(wl_i / capacity_i), crosswise
Joonsoo Kim95a79b82013-08-06 17:36:41 +09008301 * multiplication to rid ourselves of the division works out
Nicolas Pitreced549f2014-05-26 18:19:38 -04008302 * to: wl_i * capacity_j > wl_j * capacity_i; where j is
8303 * our previous maximum.
Thomas Gleixner6e40f5b2010-02-16 16:48:56 +01008304 */
Nicolas Pitreced549f2014-05-26 18:19:38 -04008305 if (wl * busiest_capacity > busiest_load * capacity) {
Joonsoo Kim95a79b82013-08-06 17:36:41 +09008306 busiest_load = wl;
Nicolas Pitreced549f2014-05-26 18:19:38 -04008307 busiest_capacity = capacity;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008308 busiest = rq;
8309 }
8310 }
8311
8312 return busiest;
8313}
8314
8315/*
8316 * Max backoff if we encounter pinned tasks. Pretty arbitrary value, but
8317 * so long as it is large enough.
8318 */
8319#define MAX_PINNED_INTERVAL 512
8320
Peter Zijlstrabd939f42012-05-02 14:20:37 +02008321static int need_active_balance(struct lb_env *env)
Peter Zijlstra1af3ed32009-12-23 15:10:31 +01008322{
Peter Zijlstrabd939f42012-05-02 14:20:37 +02008323 struct sched_domain *sd = env->sd;
8324
8325 if (env->idle == CPU_NEWLY_IDLE) {
Michael Neuling532cb4c2010-06-08 14:57:02 +10008326
8327 /*
8328 * ASYM_PACKING needs to force migrate tasks from busy but
Tim Chenafe06ef2016-11-22 12:23:53 -08008329 * lower priority CPUs in order to pack all tasks in the
8330 * highest priority CPUs.
Michael Neuling532cb4c2010-06-08 14:57:02 +10008331 */
Tim Chenafe06ef2016-11-22 12:23:53 -08008332 if ((sd->flags & SD_ASYM_PACKING) &&
8333 sched_asym_prefer(env->dst_cpu, env->src_cpu))
Michael Neuling532cb4c2010-06-08 14:57:02 +10008334 return 1;
Peter Zijlstra1af3ed32009-12-23 15:10:31 +01008335 }
8336
Vincent Guittot1aaf90a2015-02-27 16:54:14 +01008337 /*
8338 * The dst_cpu is idle and the src_cpu CPU has only 1 CFS task.
8339 * It's worth migrating the task if the src_cpu's capacity is reduced
8340 * because of other sched_class or IRQs if more capacity stays
8341 * available on dst_cpu.
8342 */
8343 if ((env->idle != CPU_NOT_IDLE) &&
8344 (env->src_rq->cfs.h_nr_running == 1)) {
8345 if ((check_cpu_capacity(env->src_rq, sd)) &&
8346 (capacity_of(env->src_cpu)*sd->imbalance_pct < capacity_of(env->dst_cpu)*100))
8347 return 1;
8348 }
8349
Peter Zijlstra1af3ed32009-12-23 15:10:31 +01008350 return unlikely(sd->nr_balance_failed > sd->cache_nice_tries+2);
8351}
8352
Tejun Heo969c7922010-05-06 18:49:21 +02008353static int active_load_balance_cpu_stop(void *data);
8354
Joonsoo Kim23f0d202013-08-06 17:36:42 +09008355static int should_we_balance(struct lb_env *env)
8356{
8357 struct sched_group *sg = env->sd->groups;
Joonsoo Kim23f0d202013-08-06 17:36:42 +09008358 int cpu, balance_cpu = -1;
8359
8360 /*
Peter Zijlstra024c9d22017-10-09 10:36:53 +02008361 * Ensure the balancing environment is consistent; can happen
8362 * when the softirq triggers 'during' hotplug.
8363 */
8364 if (!cpumask_test_cpu(env->dst_cpu, env->cpus))
8365 return 0;
8366
8367 /*
Joonsoo Kim23f0d202013-08-06 17:36:42 +09008368 * In the newly idle case, we will allow all the cpu's
8369 * to do the newly idle load balance.
8370 */
8371 if (env->idle == CPU_NEWLY_IDLE)
8372 return 1;
8373
Joonsoo Kim23f0d202013-08-06 17:36:42 +09008374 /* Try to find first idle cpu */
Peter Zijlstrae5c14b12017-05-01 10:47:02 +02008375 for_each_cpu_and(cpu, group_balance_mask(sg), env->cpus) {
Peter Zijlstraaf218122017-05-01 08:51:05 +02008376 if (!idle_cpu(cpu))
Joonsoo Kim23f0d202013-08-06 17:36:42 +09008377 continue;
8378
8379 balance_cpu = cpu;
8380 break;
8381 }
8382
8383 if (balance_cpu == -1)
8384 balance_cpu = group_balance_cpu(sg);
8385
8386 /*
8387 * First idle cpu or the first cpu(busiest) in this sched group
8388 * is eligible for doing load balancing at this and above domains.
8389 */
Joonsoo Kimb0cff9d2013-09-10 15:54:49 +09008390 return balance_cpu == env->dst_cpu;
Joonsoo Kim23f0d202013-08-06 17:36:42 +09008391}
8392
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008393/*
8394 * Check this_cpu to ensure it is balanced within domain. Attempt to move
8395 * tasks if there is an imbalance.
8396 */
8397static int load_balance(int this_cpu, struct rq *this_rq,
8398 struct sched_domain *sd, enum cpu_idle_type idle,
Joonsoo Kim23f0d202013-08-06 17:36:42 +09008399 int *continue_balancing)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008400{
Srivatsa Vaddagiri88b8dac2012-06-19 17:43:15 +05308401 int ld_moved, cur_ld_moved, active_balance = 0;
Peter Zijlstra62633222013-08-19 12:41:09 +02008402 struct sched_domain *sd_parent = sd->parent;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008403 struct sched_group *group;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008404 struct rq *busiest;
Peter Zijlstra8a8c69c2016-10-04 16:04:35 +02008405 struct rq_flags rf;
Christoph Lameter4ba29682014-08-26 19:12:21 -05008406 struct cpumask *cpus = this_cpu_cpumask_var_ptr(load_balance_mask);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008407
Peter Zijlstra8e45cb52012-02-22 12:47:19 +01008408 struct lb_env env = {
8409 .sd = sd,
Peter Zijlstraddcdf6e2012-02-22 19:27:40 +01008410 .dst_cpu = this_cpu,
8411 .dst_rq = this_rq,
Peter Zijlstraae4df9d2017-05-01 11:03:12 +02008412 .dst_grpmask = sched_group_span(sd->groups),
Peter Zijlstra8e45cb52012-02-22 12:47:19 +01008413 .idle = idle,
Peter Zijlstraeb953082012-04-17 13:38:40 +02008414 .loop_break = sched_nr_migrate_break,
Michael Wangb94031302012-07-12 16:10:13 +08008415 .cpus = cpus,
Peter Zijlstra0ec8aa02013-10-07 11:29:33 +01008416 .fbq_type = all,
Kirill Tkhai163122b2014-08-20 13:48:29 +04008417 .tasks = LIST_HEAD_INIT(env.tasks),
Peter Zijlstra8e45cb52012-02-22 12:47:19 +01008418 };
8419
Jeffrey Hugo65a44332017-06-07 13:18:57 -06008420 cpumask_and(cpus, sched_domain_span(sd), cpu_active_mask);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008421
Josh Poimboeufae928822016-06-17 12:43:24 -05008422 schedstat_inc(sd->lb_count[idle]);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008423
8424redo:
Joonsoo Kim23f0d202013-08-06 17:36:42 +09008425 if (!should_we_balance(&env)) {
8426 *continue_balancing = 0;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008427 goto out_balanced;
Joonsoo Kim23f0d202013-08-06 17:36:42 +09008428 }
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008429
Joonsoo Kim23f0d202013-08-06 17:36:42 +09008430 group = find_busiest_group(&env);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008431 if (!group) {
Josh Poimboeufae928822016-06-17 12:43:24 -05008432 schedstat_inc(sd->lb_nobusyg[idle]);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008433 goto out_balanced;
8434 }
8435
Michael Wangb94031302012-07-12 16:10:13 +08008436 busiest = find_busiest_queue(&env, group);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008437 if (!busiest) {
Josh Poimboeufae928822016-06-17 12:43:24 -05008438 schedstat_inc(sd->lb_nobusyq[idle]);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008439 goto out_balanced;
8440 }
8441
Michael Wang78feefc2012-08-06 16:41:59 +08008442 BUG_ON(busiest == env.dst_rq);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008443
Josh Poimboeufae928822016-06-17 12:43:24 -05008444 schedstat_add(sd->lb_imbalance[idle], env.imbalance);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008445
Vincent Guittot1aaf90a2015-02-27 16:54:14 +01008446 env.src_cpu = busiest->cpu;
8447 env.src_rq = busiest;
8448
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008449 ld_moved = 0;
8450 if (busiest->nr_running > 1) {
8451 /*
8452 * Attempt to move tasks. If find_busiest_group has found
8453 * an imbalance but busiest->nr_running <= 1, the group is
8454 * still unbalanced. ld_moved simply stays zero, so it is
8455 * correctly treated as an imbalance.
8456 */
Peter Zijlstra8e45cb52012-02-22 12:47:19 +01008457 env.flags |= LBF_ALL_PINNED;
Peter Zijlstrac82513e2012-04-26 13:12:27 +02008458 env.loop_max = min(sysctl_sched_nr_migrate, busiest->nr_running);
Peter Zijlstra8e45cb52012-02-22 12:47:19 +01008459
Peter Zijlstra5d6523e2012-03-10 00:07:36 +01008460more_balance:
Peter Zijlstra8a8c69c2016-10-04 16:04:35 +02008461 rq_lock_irqsave(busiest, &rf);
Peter Zijlstra3bed5e22016-10-03 16:35:32 +02008462 update_rq_clock(busiest);
Srivatsa Vaddagiri88b8dac2012-06-19 17:43:15 +05308463
8464 /*
8465 * cur_ld_moved - load moved in current iteration
8466 * ld_moved - cumulative load moved across iterations
8467 */
Kirill Tkhai163122b2014-08-20 13:48:29 +04008468 cur_ld_moved = detach_tasks(&env);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008469
8470 /*
Kirill Tkhai163122b2014-08-20 13:48:29 +04008471 * We've detached some tasks from busiest_rq. Every
8472 * task is masked "TASK_ON_RQ_MIGRATING", so we can safely
8473 * unlock busiest->lock, and we are able to be sure
8474 * that nobody can manipulate the tasks in parallel.
8475 * See task_rq_lock() family for the details.
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008476 */
Kirill Tkhai163122b2014-08-20 13:48:29 +04008477
Peter Zijlstra8a8c69c2016-10-04 16:04:35 +02008478 rq_unlock(busiest, &rf);
Kirill Tkhai163122b2014-08-20 13:48:29 +04008479
8480 if (cur_ld_moved) {
8481 attach_tasks(&env);
8482 ld_moved += cur_ld_moved;
8483 }
8484
Peter Zijlstra8a8c69c2016-10-04 16:04:35 +02008485 local_irq_restore(rf.flags);
Srivatsa Vaddagiri88b8dac2012-06-19 17:43:15 +05308486
Joonsoo Kimf1cd0852013-04-23 17:27:37 +09008487 if (env.flags & LBF_NEED_BREAK) {
8488 env.flags &= ~LBF_NEED_BREAK;
8489 goto more_balance;
8490 }
8491
Srivatsa Vaddagiri88b8dac2012-06-19 17:43:15 +05308492 /*
8493 * Revisit (affine) tasks on src_cpu that couldn't be moved to
8494 * us and move them to an alternate dst_cpu in our sched_group
8495 * where they can run. The upper limit on how many times we
8496 * iterate on same src_cpu is dependent on number of cpus in our
8497 * sched_group.
8498 *
8499 * This changes load balance semantics a bit on who can move
8500 * load to a given_cpu. In addition to the given_cpu itself
8501 * (or a ilb_cpu acting on its behalf where given_cpu is
8502 * nohz-idle), we now have balance_cpu in a position to move
8503 * load to given_cpu. In rare situations, this may cause
8504 * conflicts (balance_cpu and given_cpu/ilb_cpu deciding
8505 * _independently_ and at _same_ time to move some load to
8506 * given_cpu) causing exceess load to be moved to given_cpu.
8507 * This however should not happen so much in practice and
8508 * moreover subsequent load balance cycles should correct the
8509 * excess load moved.
8510 */
Peter Zijlstra62633222013-08-19 12:41:09 +02008511 if ((env.flags & LBF_DST_PINNED) && env.imbalance > 0) {
Srivatsa Vaddagiri88b8dac2012-06-19 17:43:15 +05308512
Vladimir Davydov7aff2e32013-09-15 21:30:13 +04008513 /* Prevent to re-select dst_cpu via env's cpus */
8514 cpumask_clear_cpu(env.dst_cpu, env.cpus);
8515
Michael Wang78feefc2012-08-06 16:41:59 +08008516 env.dst_rq = cpu_rq(env.new_dst_cpu);
Srivatsa Vaddagiri88b8dac2012-06-19 17:43:15 +05308517 env.dst_cpu = env.new_dst_cpu;
Peter Zijlstra62633222013-08-19 12:41:09 +02008518 env.flags &= ~LBF_DST_PINNED;
Srivatsa Vaddagiri88b8dac2012-06-19 17:43:15 +05308519 env.loop = 0;
8520 env.loop_break = sched_nr_migrate_break;
Joonsoo Kime02e60c2013-04-23 17:27:42 +09008521
Srivatsa Vaddagiri88b8dac2012-06-19 17:43:15 +05308522 /*
8523 * Go back to "more_balance" rather than "redo" since we
8524 * need to continue with same src_cpu.
8525 */
8526 goto more_balance;
8527 }
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008528
Peter Zijlstra62633222013-08-19 12:41:09 +02008529 /*
8530 * We failed to reach balance because of affinity.
8531 */
8532 if (sd_parent) {
Nicolas Pitre63b2ca32014-05-26 18:19:37 -04008533 int *group_imbalance = &sd_parent->groups->sgc->imbalance;
Peter Zijlstra62633222013-08-19 12:41:09 +02008534
Vincent Guittotafdeee02014-08-26 13:06:44 +02008535 if ((env.flags & LBF_SOME_PINNED) && env.imbalance > 0)
Peter Zijlstra62633222013-08-19 12:41:09 +02008536 *group_imbalance = 1;
Peter Zijlstra62633222013-08-19 12:41:09 +02008537 }
8538
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008539 /* All tasks on this runqueue were pinned by CPU affinity */
Peter Zijlstra8e45cb52012-02-22 12:47:19 +01008540 if (unlikely(env.flags & LBF_ALL_PINNED)) {
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008541 cpumask_clear_cpu(cpu_of(busiest), cpus);
Jeffrey Hugo65a44332017-06-07 13:18:57 -06008542 /*
8543 * Attempting to continue load balancing at the current
8544 * sched_domain level only makes sense if there are
8545 * active CPUs remaining as possible busiest CPUs to
8546 * pull load from which are not contained within the
8547 * destination group that is receiving any migrated
8548 * load.
8549 */
8550 if (!cpumask_subset(cpus, env.dst_grpmask)) {
Prashanth Nageshappabbf18b12012-06-19 17:52:07 +05308551 env.loop = 0;
8552 env.loop_break = sched_nr_migrate_break;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008553 goto redo;
Prashanth Nageshappabbf18b12012-06-19 17:52:07 +05308554 }
Vincent Guittotafdeee02014-08-26 13:06:44 +02008555 goto out_all_pinned;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008556 }
8557 }
8558
8559 if (!ld_moved) {
Josh Poimboeufae928822016-06-17 12:43:24 -05008560 schedstat_inc(sd->lb_failed[idle]);
Venkatesh Pallipadi58b26c42010-09-10 18:19:17 -07008561 /*
8562 * Increment the failure counter only on periodic balance.
8563 * We do not want newidle balance, which can be very
8564 * frequent, pollute the failure counter causing
8565 * excessive cache_hot migrations and active balances.
8566 */
8567 if (idle != CPU_NEWLY_IDLE)
8568 sd->nr_balance_failed++;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008569
Peter Zijlstrabd939f42012-05-02 14:20:37 +02008570 if (need_active_balance(&env)) {
Peter Zijlstra8a8c69c2016-10-04 16:04:35 +02008571 unsigned long flags;
8572
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008573 raw_spin_lock_irqsave(&busiest->lock, flags);
8574
Tejun Heo969c7922010-05-06 18:49:21 +02008575 /* don't kick the active_load_balance_cpu_stop,
8576 * if the curr task on busiest cpu can't be
8577 * moved to this_cpu
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008578 */
Ingo Molnar0c98d342017-02-05 15:38:10 +01008579 if (!cpumask_test_cpu(this_cpu, &busiest->curr->cpus_allowed)) {
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008580 raw_spin_unlock_irqrestore(&busiest->lock,
8581 flags);
Peter Zijlstra8e45cb52012-02-22 12:47:19 +01008582 env.flags |= LBF_ALL_PINNED;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008583 goto out_one_pinned;
8584 }
8585
Tejun Heo969c7922010-05-06 18:49:21 +02008586 /*
8587 * ->active_balance synchronizes accesses to
8588 * ->active_balance_work. Once set, it's cleared
8589 * only after active load balance is finished.
8590 */
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008591 if (!busiest->active_balance) {
8592 busiest->active_balance = 1;
8593 busiest->push_cpu = this_cpu;
8594 active_balance = 1;
8595 }
8596 raw_spin_unlock_irqrestore(&busiest->lock, flags);
Tejun Heo969c7922010-05-06 18:49:21 +02008597
Peter Zijlstrabd939f42012-05-02 14:20:37 +02008598 if (active_balance) {
Tejun Heo969c7922010-05-06 18:49:21 +02008599 stop_one_cpu_nowait(cpu_of(busiest),
8600 active_load_balance_cpu_stop, busiest,
8601 &busiest->active_balance_work);
Peter Zijlstrabd939f42012-05-02 14:20:37 +02008602 }
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008603
Srikar Dronamrajud02c071182016-03-23 17:54:44 +05308604 /* We've kicked active balancing, force task migration. */
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008605 sd->nr_balance_failed = sd->cache_nice_tries+1;
8606 }
8607 } else
8608 sd->nr_balance_failed = 0;
8609
8610 if (likely(!active_balance)) {
8611 /* We were unbalanced, so reset the balancing interval */
8612 sd->balance_interval = sd->min_interval;
8613 } else {
8614 /*
8615 * If we've begun active balancing, start to back off. This
8616 * case may not be covered by the all_pinned logic if there
8617 * is only 1 task on the busy runqueue (because we don't call
Kirill Tkhai163122b2014-08-20 13:48:29 +04008618 * detach_tasks).
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008619 */
8620 if (sd->balance_interval < sd->max_interval)
8621 sd->balance_interval *= 2;
8622 }
8623
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008624 goto out;
8625
8626out_balanced:
Vincent Guittotafdeee02014-08-26 13:06:44 +02008627 /*
8628 * We reach balance although we may have faced some affinity
8629 * constraints. Clear the imbalance flag if it was set.
8630 */
8631 if (sd_parent) {
8632 int *group_imbalance = &sd_parent->groups->sgc->imbalance;
8633
8634 if (*group_imbalance)
8635 *group_imbalance = 0;
8636 }
8637
8638out_all_pinned:
8639 /*
8640 * We reach balance because all tasks are pinned at this level so
8641 * we can't migrate them. Let the imbalance flag set so parent level
8642 * can try to migrate them.
8643 */
Josh Poimboeufae928822016-06-17 12:43:24 -05008644 schedstat_inc(sd->lb_balanced[idle]);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008645
8646 sd->nr_balance_failed = 0;
8647
8648out_one_pinned:
8649 /* tune up the balancing interval */
Peter Zijlstra8e45cb52012-02-22 12:47:19 +01008650 if (((env.flags & LBF_ALL_PINNED) &&
Peter Zijlstra5b54b562011-09-22 15:23:13 +02008651 sd->balance_interval < MAX_PINNED_INTERVAL) ||
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008652 (sd->balance_interval < sd->max_interval))
8653 sd->balance_interval *= 2;
8654
Venkatesh Pallipadi46e49b32011-02-14 14:38:50 -08008655 ld_moved = 0;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008656out:
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008657 return ld_moved;
8658}
8659
Jason Low52a08ef2014-05-08 17:49:22 -07008660static inline unsigned long
8661get_sd_balance_interval(struct sched_domain *sd, int cpu_busy)
8662{
8663 unsigned long interval = sd->balance_interval;
8664
8665 if (cpu_busy)
8666 interval *= sd->busy_factor;
8667
8668 /* scale ms to jiffies */
8669 interval = msecs_to_jiffies(interval);
8670 interval = clamp(interval, 1UL, max_load_balance_interval);
8671
8672 return interval;
8673}
8674
8675static inline void
Leo Yan31851a92016-08-05 14:31:29 +08008676update_next_balance(struct sched_domain *sd, unsigned long *next_balance)
Jason Low52a08ef2014-05-08 17:49:22 -07008677{
8678 unsigned long interval, next;
8679
Leo Yan31851a92016-08-05 14:31:29 +08008680 /* used by idle balance, so cpu_busy = 0 */
8681 interval = get_sd_balance_interval(sd, 0);
Jason Low52a08ef2014-05-08 17:49:22 -07008682 next = sd->last_balance + interval;
8683
8684 if (time_after(*next_balance, next))
8685 *next_balance = next;
8686}
8687
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008688/*
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008689 * idle_balance is called by schedule() if this_cpu is about to become
8690 * idle. Attempts to pull tasks from other CPUs.
8691 */
Matt Fleming46f69fa2016-09-21 14:38:12 +01008692static int idle_balance(struct rq *this_rq, struct rq_flags *rf)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008693{
Jason Low52a08ef2014-05-08 17:49:22 -07008694 unsigned long next_balance = jiffies + HZ;
8695 int this_cpu = this_rq->cpu;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008696 struct sched_domain *sd;
8697 int pulled_task = 0;
Jason Low9bd721c2013-09-13 11:26:52 -07008698 u64 curr_cost = 0;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008699
Peter Zijlstra6e831252014-02-11 16:11:48 +01008700 /*
8701 * We must set idle_stamp _before_ calling idle_balance(), such that we
8702 * measure the duration of idle_balance() as idle time.
8703 */
8704 this_rq->idle_stamp = rq_clock(this_rq);
8705
Matt Fleming46f69fa2016-09-21 14:38:12 +01008706 /*
Peter Zijlstra28004862017-09-07 17:03:50 +02008707 * Do not pull tasks towards !active CPUs...
8708 */
8709 if (!cpu_active(this_cpu))
8710 return 0;
8711
8712 /*
Matt Fleming46f69fa2016-09-21 14:38:12 +01008713 * This is OK, because current is on_cpu, which avoids it being picked
8714 * for load-balance and preemption/IRQs are still disabled avoiding
8715 * further scheduler activity on it and we're being very careful to
8716 * re-start the picking loop.
8717 */
8718 rq_unpin_lock(this_rq, rf);
8719
Tim Chen4486edd2014-06-23 12:16:49 -07008720 if (this_rq->avg_idle < sysctl_sched_migration_cost ||
8721 !this_rq->rd->overload) {
Jason Low52a08ef2014-05-08 17:49:22 -07008722 rcu_read_lock();
8723 sd = rcu_dereference_check_sched_domain(this_rq->sd);
8724 if (sd)
Leo Yan31851a92016-08-05 14:31:29 +08008725 update_next_balance(sd, &next_balance);
Jason Low52a08ef2014-05-08 17:49:22 -07008726 rcu_read_unlock();
8727
Peter Zijlstra6e831252014-02-11 16:11:48 +01008728 goto out;
Jason Low52a08ef2014-05-08 17:49:22 -07008729 }
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008730
Peter Zijlstraf492e122009-12-23 15:29:42 +01008731 raw_spin_unlock(&this_rq->lock);
8732
Paul Turner48a16752012-10-04 13:18:31 +02008733 update_blocked_averages(this_cpu);
Peter Zijlstradce840a2011-04-07 14:09:50 +02008734 rcu_read_lock();
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008735 for_each_domain(this_cpu, sd) {
Joonsoo Kim23f0d202013-08-06 17:36:42 +09008736 int continue_balancing = 1;
Jason Low9bd721c2013-09-13 11:26:52 -07008737 u64 t0, domain_cost;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008738
8739 if (!(sd->flags & SD_LOAD_BALANCE))
8740 continue;
8741
Jason Low52a08ef2014-05-08 17:49:22 -07008742 if (this_rq->avg_idle < curr_cost + sd->max_newidle_lb_cost) {
Leo Yan31851a92016-08-05 14:31:29 +08008743 update_next_balance(sd, &next_balance);
Jason Low9bd721c2013-09-13 11:26:52 -07008744 break;
Jason Low52a08ef2014-05-08 17:49:22 -07008745 }
Jason Low9bd721c2013-09-13 11:26:52 -07008746
Peter Zijlstraf492e122009-12-23 15:29:42 +01008747 if (sd->flags & SD_BALANCE_NEWIDLE) {
Jason Low9bd721c2013-09-13 11:26:52 -07008748 t0 = sched_clock_cpu(this_cpu);
8749
Peter Zijlstraf492e122009-12-23 15:29:42 +01008750 pulled_task = load_balance(this_cpu, this_rq,
Joonsoo Kim23f0d202013-08-06 17:36:42 +09008751 sd, CPU_NEWLY_IDLE,
8752 &continue_balancing);
Jason Low9bd721c2013-09-13 11:26:52 -07008753
8754 domain_cost = sched_clock_cpu(this_cpu) - t0;
8755 if (domain_cost > sd->max_newidle_lb_cost)
8756 sd->max_newidle_lb_cost = domain_cost;
8757
8758 curr_cost += domain_cost;
Peter Zijlstraf492e122009-12-23 15:29:42 +01008759 }
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008760
Leo Yan31851a92016-08-05 14:31:29 +08008761 update_next_balance(sd, &next_balance);
Jason Low39a4d9c2014-04-23 18:30:35 -07008762
8763 /*
8764 * Stop searching for tasks to pull if there are
8765 * now runnable tasks on this rq.
8766 */
8767 if (pulled_task || this_rq->nr_running > 0)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008768 break;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008769 }
Peter Zijlstradce840a2011-04-07 14:09:50 +02008770 rcu_read_unlock();
Peter Zijlstraf492e122009-12-23 15:29:42 +01008771
8772 raw_spin_lock(&this_rq->lock);
8773
Jason Low0e5b5332014-04-28 15:45:54 -07008774 if (curr_cost > this_rq->max_idle_balance_cost)
8775 this_rq->max_idle_balance_cost = curr_cost;
8776
Daniel Lezcanoe5fc6612014-01-17 10:04:02 +01008777 /*
Jason Low0e5b5332014-04-28 15:45:54 -07008778 * While browsing the domains, we released the rq lock, a task could
8779 * have been enqueued in the meantime. Since we're not going idle,
8780 * pretend we pulled a task.
Daniel Lezcanoe5fc6612014-01-17 10:04:02 +01008781 */
Jason Low0e5b5332014-04-28 15:45:54 -07008782 if (this_rq->cfs.h_nr_running && !pulled_task)
Peter Zijlstra6e831252014-02-11 16:11:48 +01008783 pulled_task = 1;
Daniel Lezcanoe5fc6612014-01-17 10:04:02 +01008784
Peter Zijlstra6e831252014-02-11 16:11:48 +01008785out:
Jason Low52a08ef2014-05-08 17:49:22 -07008786 /* Move the next balance forward */
8787 if (time_after(this_rq->next_balance, next_balance))
8788 this_rq->next_balance = next_balance;
8789
Kirill Tkhaie4aa3582014-03-06 13:31:55 +04008790 /* Is there a task of a high priority class? */
Kirill Tkhai46383642014-03-15 02:15:07 +04008791 if (this_rq->nr_running != this_rq->cfs.h_nr_running)
Kirill Tkhaie4aa3582014-03-06 13:31:55 +04008792 pulled_task = -1;
8793
Dietmar Eggemann38c6ade2015-10-20 13:04:41 +01008794 if (pulled_task)
Peter Zijlstra6e831252014-02-11 16:11:48 +01008795 this_rq->idle_stamp = 0;
8796
Matt Fleming46f69fa2016-09-21 14:38:12 +01008797 rq_repin_lock(this_rq, rf);
8798
Daniel Lezcano3c4017c2014-01-17 10:04:03 +01008799 return pulled_task;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008800}
8801
8802/*
Tejun Heo969c7922010-05-06 18:49:21 +02008803 * active_load_balance_cpu_stop is run by cpu stopper. It pushes
8804 * running tasks off the busiest CPU onto idle CPUs. It requires at
8805 * least 1 task to be running on each physical CPU where possible, and
8806 * avoids physical / logical imbalances.
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008807 */
Tejun Heo969c7922010-05-06 18:49:21 +02008808static int active_load_balance_cpu_stop(void *data)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008809{
Tejun Heo969c7922010-05-06 18:49:21 +02008810 struct rq *busiest_rq = data;
8811 int busiest_cpu = cpu_of(busiest_rq);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008812 int target_cpu = busiest_rq->push_cpu;
Tejun Heo969c7922010-05-06 18:49:21 +02008813 struct rq *target_rq = cpu_rq(target_cpu);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008814 struct sched_domain *sd;
Kirill Tkhaie5673f22014-08-20 13:48:01 +04008815 struct task_struct *p = NULL;
Peter Zijlstra8a8c69c2016-10-04 16:04:35 +02008816 struct rq_flags rf;
Tejun Heo969c7922010-05-06 18:49:21 +02008817
Peter Zijlstra8a8c69c2016-10-04 16:04:35 +02008818 rq_lock_irq(busiest_rq, &rf);
Peter Zijlstraedd8e412017-09-07 17:03:51 +02008819 /*
8820 * Between queueing the stop-work and running it is a hole in which
8821 * CPUs can become inactive. We should not move tasks from or to
8822 * inactive CPUs.
8823 */
8824 if (!cpu_active(busiest_cpu) || !cpu_active(target_cpu))
8825 goto out_unlock;
Tejun Heo969c7922010-05-06 18:49:21 +02008826
8827 /* make sure the requested cpu hasn't gone down in the meantime */
8828 if (unlikely(busiest_cpu != smp_processor_id() ||
8829 !busiest_rq->active_balance))
8830 goto out_unlock;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008831
8832 /* Is there any task to move? */
8833 if (busiest_rq->nr_running <= 1)
Tejun Heo969c7922010-05-06 18:49:21 +02008834 goto out_unlock;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008835
8836 /*
8837 * This condition is "impossible", if it occurs
8838 * we need to fix it. Originally reported by
8839 * Bjorn Helgaas on a 128-cpu setup.
8840 */
8841 BUG_ON(busiest_rq == target_rq);
8842
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008843 /* Search for an sd spanning us and the target CPU. */
Peter Zijlstradce840a2011-04-07 14:09:50 +02008844 rcu_read_lock();
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008845 for_each_domain(target_cpu, sd) {
8846 if ((sd->flags & SD_LOAD_BALANCE) &&
8847 cpumask_test_cpu(busiest_cpu, sched_domain_span(sd)))
8848 break;
8849 }
8850
8851 if (likely(sd)) {
Peter Zijlstra8e45cb52012-02-22 12:47:19 +01008852 struct lb_env env = {
8853 .sd = sd,
Peter Zijlstraddcdf6e2012-02-22 19:27:40 +01008854 .dst_cpu = target_cpu,
8855 .dst_rq = target_rq,
8856 .src_cpu = busiest_rq->cpu,
8857 .src_rq = busiest_rq,
Peter Zijlstra8e45cb52012-02-22 12:47:19 +01008858 .idle = CPU_IDLE,
Jeffrey Hugo65a44332017-06-07 13:18:57 -06008859 /*
8860 * can_migrate_task() doesn't need to compute new_dst_cpu
8861 * for active balancing. Since we have CPU_IDLE, but no
8862 * @dst_grpmask we need to make that test go away with lying
8863 * about DST_PINNED.
8864 */
8865 .flags = LBF_DST_PINNED,
Peter Zijlstra8e45cb52012-02-22 12:47:19 +01008866 };
8867
Josh Poimboeufae928822016-06-17 12:43:24 -05008868 schedstat_inc(sd->alb_count);
Peter Zijlstra3bed5e22016-10-03 16:35:32 +02008869 update_rq_clock(busiest_rq);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008870
Kirill Tkhaie5673f22014-08-20 13:48:01 +04008871 p = detach_one_task(&env);
Srikar Dronamrajud02c071182016-03-23 17:54:44 +05308872 if (p) {
Josh Poimboeufae928822016-06-17 12:43:24 -05008873 schedstat_inc(sd->alb_pushed);
Srikar Dronamrajud02c071182016-03-23 17:54:44 +05308874 /* Active balancing done, reset the failure counter. */
8875 sd->nr_balance_failed = 0;
8876 } else {
Josh Poimboeufae928822016-06-17 12:43:24 -05008877 schedstat_inc(sd->alb_failed);
Srikar Dronamrajud02c071182016-03-23 17:54:44 +05308878 }
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008879 }
Peter Zijlstradce840a2011-04-07 14:09:50 +02008880 rcu_read_unlock();
Tejun Heo969c7922010-05-06 18:49:21 +02008881out_unlock:
8882 busiest_rq->active_balance = 0;
Peter Zijlstra8a8c69c2016-10-04 16:04:35 +02008883 rq_unlock(busiest_rq, &rf);
Kirill Tkhaie5673f22014-08-20 13:48:01 +04008884
8885 if (p)
8886 attach_one_task(target_rq, p);
8887
8888 local_irq_enable();
8889
Tejun Heo969c7922010-05-06 18:49:21 +02008890 return 0;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008891}
8892
Mike Galbraithd987fc72011-12-05 10:01:47 +01008893static inline int on_null_domain(struct rq *rq)
8894{
8895 return unlikely(!rcu_dereference_sched(rq->sd));
8896}
8897
Frederic Weisbecker3451d022011-08-10 23:21:01 +02008898#ifdef CONFIG_NO_HZ_COMMON
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07008899/*
8900 * idle load balancing details
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07008901 * - When one of the busy CPUs notice that there may be an idle rebalancing
8902 * needed, they will kick the idle load balancer, which then does idle
8903 * load balancing for all the idle CPUs.
8904 */
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008905static struct {
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07008906 cpumask_var_t idle_cpus_mask;
Suresh Siddha0b005cf2011-12-01 17:07:34 -08008907 atomic_t nr_cpus;
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07008908 unsigned long next_balance; /* in jiffy units */
8909} nohz ____cacheline_aligned;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008910
Daniel Lezcano3dd03372014-01-06 12:34:41 +01008911static inline int find_new_ilb(void)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008912{
Suresh Siddha0b005cf2011-12-01 17:07:34 -08008913 int ilb = cpumask_first(nohz.idle_cpus_mask);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008914
Suresh Siddha786d6dc2011-12-01 17:07:35 -08008915 if (ilb < nr_cpu_ids && idle_cpu(ilb))
8916 return ilb;
8917
8918 return nr_cpu_ids;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008919}
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008920
8921/*
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07008922 * Kick a CPU to do the nohz balancing, if it is time for it. We pick the
8923 * nohz_load_balancer CPU (if there is one) otherwise fallback to any idle
8924 * CPU (if there is one).
8925 */
Daniel Lezcano0aeeeeb2014-01-06 12:34:42 +01008926static void nohz_balancer_kick(void)
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07008927{
8928 int ilb_cpu;
8929
8930 nohz.next_balance++;
8931
Daniel Lezcano3dd03372014-01-06 12:34:41 +01008932 ilb_cpu = find_new_ilb();
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07008933
Suresh Siddha0b005cf2011-12-01 17:07:34 -08008934 if (ilb_cpu >= nr_cpu_ids)
8935 return;
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07008936
Suresh Siddhacd490c52011-12-06 11:26:34 -08008937 if (test_and_set_bit(NOHZ_BALANCE_KICK, nohz_flags(ilb_cpu)))
Suresh Siddha1c792db2011-12-01 17:07:32 -08008938 return;
8939 /*
8940 * Use smp_send_reschedule() instead of resched_cpu().
8941 * This way we generate a sched IPI on the target cpu which
8942 * is idle. And the softirq performing nohz idle load balance
8943 * will be run before returning from the IPI.
8944 */
8945 smp_send_reschedule(ilb_cpu);
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07008946 return;
8947}
8948
Thomas Gleixner20a5c8c2016-03-10 12:54:20 +01008949void nohz_balance_exit_idle(unsigned int cpu)
Suresh Siddha71325962012-01-19 18:28:57 -08008950{
8951 if (unlikely(test_bit(NOHZ_TICK_STOPPED, nohz_flags(cpu)))) {
Mike Galbraithd987fc72011-12-05 10:01:47 +01008952 /*
8953 * Completely isolated CPUs don't ever set, so we must test.
8954 */
8955 if (likely(cpumask_test_cpu(cpu, nohz.idle_cpus_mask))) {
8956 cpumask_clear_cpu(cpu, nohz.idle_cpus_mask);
8957 atomic_dec(&nohz.nr_cpus);
8958 }
Suresh Siddha71325962012-01-19 18:28:57 -08008959 clear_bit(NOHZ_TICK_STOPPED, nohz_flags(cpu));
8960 }
8961}
8962
Suresh Siddha69e1e812011-12-01 17:07:33 -08008963static inline void set_cpu_sd_state_busy(void)
8964{
8965 struct sched_domain *sd;
Preeti U Murthy37dc6b52013-10-30 08:42:52 +05308966 int cpu = smp_processor_id();
Suresh Siddha69e1e812011-12-01 17:07:33 -08008967
Suresh Siddha69e1e812011-12-01 17:07:33 -08008968 rcu_read_lock();
Peter Zijlstra0e369d72016-05-09 10:38:01 +02008969 sd = rcu_dereference(per_cpu(sd_llc, cpu));
Vincent Guittot25f55d92013-04-23 16:59:02 +02008970
8971 if (!sd || !sd->nohz_idle)
8972 goto unlock;
8973 sd->nohz_idle = 0;
8974
Peter Zijlstra0e369d72016-05-09 10:38:01 +02008975 atomic_inc(&sd->shared->nr_busy_cpus);
Vincent Guittot25f55d92013-04-23 16:59:02 +02008976unlock:
Suresh Siddha69e1e812011-12-01 17:07:33 -08008977 rcu_read_unlock();
8978}
8979
8980void set_cpu_sd_state_idle(void)
8981{
8982 struct sched_domain *sd;
Preeti U Murthy37dc6b52013-10-30 08:42:52 +05308983 int cpu = smp_processor_id();
Suresh Siddha69e1e812011-12-01 17:07:33 -08008984
Suresh Siddha69e1e812011-12-01 17:07:33 -08008985 rcu_read_lock();
Peter Zijlstra0e369d72016-05-09 10:38:01 +02008986 sd = rcu_dereference(per_cpu(sd_llc, cpu));
Vincent Guittot25f55d92013-04-23 16:59:02 +02008987
8988 if (!sd || sd->nohz_idle)
8989 goto unlock;
8990 sd->nohz_idle = 1;
8991
Peter Zijlstra0e369d72016-05-09 10:38:01 +02008992 atomic_dec(&sd->shared->nr_busy_cpus);
Vincent Guittot25f55d92013-04-23 16:59:02 +02008993unlock:
Suresh Siddha69e1e812011-12-01 17:07:33 -08008994 rcu_read_unlock();
8995}
8996
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07008997/*
Alex Shic1cc0172012-09-10 15:10:58 +08008998 * This routine will record that the cpu is going idle with tick stopped.
Suresh Siddha0b005cf2011-12-01 17:07:34 -08008999 * This info will be used in performing idle load balancing in the future.
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01009000 */
Alex Shic1cc0172012-09-10 15:10:58 +08009001void nohz_balance_enter_idle(int cpu)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01009002{
Suresh Siddha71325962012-01-19 18:28:57 -08009003 /*
9004 * If this cpu is going down, then nothing needs to be done.
9005 */
9006 if (!cpu_active(cpu))
9007 return;
9008
Frederic Weisbecker387bc8b2017-06-19 04:12:02 +02009009 /* Spare idle load balancing on CPUs that don't want to be disturbed: */
9010 if (!is_housekeeping_cpu(cpu))
9011 return;
9012
Alex Shic1cc0172012-09-10 15:10:58 +08009013 if (test_bit(NOHZ_TICK_STOPPED, nohz_flags(cpu)))
9014 return;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01009015
Mike Galbraithd987fc72011-12-05 10:01:47 +01009016 /*
9017 * If we're a completely isolated CPU, we don't play.
9018 */
9019 if (on_null_domain(cpu_rq(cpu)))
9020 return;
9021
Alex Shic1cc0172012-09-10 15:10:58 +08009022 cpumask_set_cpu(cpu, nohz.idle_cpus_mask);
9023 atomic_inc(&nohz.nr_cpus);
9024 set_bit(NOHZ_TICK_STOPPED, nohz_flags(cpu));
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01009025}
9026#endif
9027
9028static DEFINE_SPINLOCK(balancing);
9029
Peter Zijlstra49c022e2011-04-05 10:14:25 +02009030/*
9031 * Scale the max load_balance interval with the number of CPUs in the system.
9032 * This trades load-balance latency on larger machines for less cross talk.
9033 */
Peter Zijlstra029632f2011-10-25 10:00:11 +02009034void update_max_interval(void)
Peter Zijlstra49c022e2011-04-05 10:14:25 +02009035{
9036 max_load_balance_interval = HZ*num_online_cpus()/10;
9037}
9038
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01009039/*
9040 * It checks each scheduling domain to see if it is due to be balanced,
9041 * and initiates a balancing operation if so.
9042 *
Libinb9b08532013-04-01 19:14:01 +08009043 * Balancing parameters are set up in init_sched_domains.
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01009044 */
Daniel Lezcanof7ed0a82014-01-06 12:34:43 +01009045static void rebalance_domains(struct rq *rq, enum cpu_idle_type idle)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01009046{
Joonsoo Kim23f0d202013-08-06 17:36:42 +09009047 int continue_balancing = 1;
Daniel Lezcanof7ed0a82014-01-06 12:34:43 +01009048 int cpu = rq->cpu;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01009049 unsigned long interval;
Peter Zijlstra04f733b2012-05-11 00:12:02 +02009050 struct sched_domain *sd;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01009051 /* Earliest time when we have to do rebalance again */
9052 unsigned long next_balance = jiffies + 60*HZ;
9053 int update_next_balance = 0;
Jason Lowf48627e2013-09-13 11:26:53 -07009054 int need_serialize, need_decay = 0;
9055 u64 max_cost = 0;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01009056
Paul Turner48a16752012-10-04 13:18:31 +02009057 update_blocked_averages(cpu);
Peter Zijlstra2069dd72010-11-15 15:47:00 -08009058
Peter Zijlstradce840a2011-04-07 14:09:50 +02009059 rcu_read_lock();
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01009060 for_each_domain(cpu, sd) {
Jason Lowf48627e2013-09-13 11:26:53 -07009061 /*
9062 * Decay the newidle max times here because this is a regular
9063 * visit to all the domains. Decay ~1% per second.
9064 */
9065 if (time_after(jiffies, sd->next_decay_max_lb_cost)) {
9066 sd->max_newidle_lb_cost =
9067 (sd->max_newidle_lb_cost * 253) / 256;
9068 sd->next_decay_max_lb_cost = jiffies + HZ;
9069 need_decay = 1;
9070 }
9071 max_cost += sd->max_newidle_lb_cost;
9072
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01009073 if (!(sd->flags & SD_LOAD_BALANCE))
9074 continue;
9075
Jason Lowf48627e2013-09-13 11:26:53 -07009076 /*
9077 * Stop the load balance at this level. There is another
9078 * CPU in our sched group which is doing load balancing more
9079 * actively.
9080 */
9081 if (!continue_balancing) {
9082 if (need_decay)
9083 continue;
9084 break;
9085 }
9086
Jason Low52a08ef2014-05-08 17:49:22 -07009087 interval = get_sd_balance_interval(sd, idle != CPU_IDLE);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01009088
9089 need_serialize = sd->flags & SD_SERIALIZE;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01009090 if (need_serialize) {
9091 if (!spin_trylock(&balancing))
9092 goto out;
9093 }
9094
9095 if (time_after_eq(jiffies, sd->last_balance + interval)) {
Joonsoo Kim23f0d202013-08-06 17:36:42 +09009096 if (load_balance(cpu, rq, sd, idle, &continue_balancing)) {
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01009097 /*
Peter Zijlstra62633222013-08-19 12:41:09 +02009098 * The LBF_DST_PINNED logic could have changed
Joonsoo Kimde5eb2d2013-04-23 17:27:38 +09009099 * env->dst_cpu, so we can't know our idle
9100 * state even if we migrated tasks. Update it.
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01009101 */
Joonsoo Kimde5eb2d2013-04-23 17:27:38 +09009102 idle = idle_cpu(cpu) ? CPU_IDLE : CPU_NOT_IDLE;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01009103 }
9104 sd->last_balance = jiffies;
Jason Low52a08ef2014-05-08 17:49:22 -07009105 interval = get_sd_balance_interval(sd, idle != CPU_IDLE);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01009106 }
9107 if (need_serialize)
9108 spin_unlock(&balancing);
9109out:
9110 if (time_after(next_balance, sd->last_balance + interval)) {
9111 next_balance = sd->last_balance + interval;
9112 update_next_balance = 1;
9113 }
Jason Lowf48627e2013-09-13 11:26:53 -07009114 }
9115 if (need_decay) {
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01009116 /*
Jason Lowf48627e2013-09-13 11:26:53 -07009117 * Ensure the rq-wide value also decays but keep it at a
9118 * reasonable floor to avoid funnies with rq->avg_idle.
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01009119 */
Jason Lowf48627e2013-09-13 11:26:53 -07009120 rq->max_idle_balance_cost =
9121 max((u64)sysctl_sched_migration_cost, max_cost);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01009122 }
Peter Zijlstradce840a2011-04-07 14:09:50 +02009123 rcu_read_unlock();
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01009124
9125 /*
9126 * next_balance will be updated only when there is a need.
9127 * When the cpu is attached to null domain for ex, it will not be
9128 * updated.
9129 */
Vincent Guittotc5afb6a2015-08-03 11:55:50 +02009130 if (likely(update_next_balance)) {
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01009131 rq->next_balance = next_balance;
Vincent Guittotc5afb6a2015-08-03 11:55:50 +02009132
9133#ifdef CONFIG_NO_HZ_COMMON
9134 /*
9135 * If this CPU has been elected to perform the nohz idle
9136 * balance. Other idle CPUs have already rebalanced with
9137 * nohz_idle_balance() and nohz.next_balance has been
9138 * updated accordingly. This CPU is now running the idle load
9139 * balance for itself and we need to update the
9140 * nohz.next_balance accordingly.
9141 */
9142 if ((idle == CPU_IDLE) && time_after(nohz.next_balance, rq->next_balance))
9143 nohz.next_balance = rq->next_balance;
9144#endif
9145 }
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01009146}
9147
Frederic Weisbecker3451d022011-08-10 23:21:01 +02009148#ifdef CONFIG_NO_HZ_COMMON
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07009149/*
Frederic Weisbecker3451d022011-08-10 23:21:01 +02009150 * In CONFIG_NO_HZ_COMMON case, the idle balance kickee will do the
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07009151 * rebalancing for all the cpus for whom scheduler ticks are stopped.
9152 */
Daniel Lezcano208cb162014-01-06 12:34:44 +01009153static void nohz_idle_balance(struct rq *this_rq, enum cpu_idle_type idle)
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07009154{
Daniel Lezcano208cb162014-01-06 12:34:44 +01009155 int this_cpu = this_rq->cpu;
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07009156 struct rq *rq;
9157 int balance_cpu;
Vincent Guittotc5afb6a2015-08-03 11:55:50 +02009158 /* Earliest time when we have to do rebalance again */
9159 unsigned long next_balance = jiffies + 60*HZ;
9160 int update_next_balance = 0;
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07009161
Suresh Siddha1c792db2011-12-01 17:07:32 -08009162 if (idle != CPU_IDLE ||
9163 !test_bit(NOHZ_BALANCE_KICK, nohz_flags(this_cpu)))
9164 goto end;
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07009165
9166 for_each_cpu(balance_cpu, nohz.idle_cpus_mask) {
Suresh Siddha8a6d42d2011-12-06 11:19:37 -08009167 if (balance_cpu == this_cpu || !idle_cpu(balance_cpu))
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07009168 continue;
9169
9170 /*
9171 * If this cpu gets work to do, stop the load balancing
9172 * work being done for other cpus. Next load
9173 * balancing owner will pick it up.
9174 */
Suresh Siddha1c792db2011-12-01 17:07:32 -08009175 if (need_resched())
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07009176 break;
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07009177
Vincent Guittot5ed4f1d2012-09-13 06:11:26 +02009178 rq = cpu_rq(balance_cpu);
9179
Tim Chened61bbc2014-05-20 14:39:27 -07009180 /*
9181 * If time for next balance is due,
9182 * do the balance.
9183 */
9184 if (time_after_eq(jiffies, rq->next_balance)) {
Peter Zijlstra8a8c69c2016-10-04 16:04:35 +02009185 struct rq_flags rf;
9186
9187 rq_lock_irq(rq, &rf);
Tim Chened61bbc2014-05-20 14:39:27 -07009188 update_rq_clock(rq);
Frederic Weisbeckercee1afc2016-04-13 15:56:50 +02009189 cpu_load_update_idle(rq);
Peter Zijlstra8a8c69c2016-10-04 16:04:35 +02009190 rq_unlock_irq(rq, &rf);
9191
Tim Chened61bbc2014-05-20 14:39:27 -07009192 rebalance_domains(rq, CPU_IDLE);
9193 }
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07009194
Vincent Guittotc5afb6a2015-08-03 11:55:50 +02009195 if (time_after(next_balance, rq->next_balance)) {
9196 next_balance = rq->next_balance;
9197 update_next_balance = 1;
9198 }
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07009199 }
Vincent Guittotc5afb6a2015-08-03 11:55:50 +02009200
9201 /*
9202 * next_balance will be updated only when there is a need.
9203 * When the CPU is attached to null domain for ex, it will not be
9204 * updated.
9205 */
9206 if (likely(update_next_balance))
9207 nohz.next_balance = next_balance;
Suresh Siddha1c792db2011-12-01 17:07:32 -08009208end:
9209 clear_bit(NOHZ_BALANCE_KICK, nohz_flags(this_cpu));
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07009210}
9211
9212/*
Suresh Siddha0b005cf2011-12-01 17:07:34 -08009213 * Current heuristic for kicking the idle load balancer in the presence
Vincent Guittot1aaf90a2015-02-27 16:54:14 +01009214 * of an idle cpu in the system.
Suresh Siddha0b005cf2011-12-01 17:07:34 -08009215 * - This rq has more than one task.
Vincent Guittot1aaf90a2015-02-27 16:54:14 +01009216 * - This rq has at least one CFS task and the capacity of the CPU is
9217 * significantly reduced because of RT tasks or IRQs.
9218 * - At parent of LLC scheduler domain level, this cpu's scheduler group has
9219 * multiple busy cpu.
Suresh Siddha0b005cf2011-12-01 17:07:34 -08009220 * - For SD_ASYM_PACKING, if the lower numbered cpu's in the scheduler
9221 * domain span are idle.
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07009222 */
Vincent Guittot1aaf90a2015-02-27 16:54:14 +01009223static inline bool nohz_kick_needed(struct rq *rq)
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07009224{
9225 unsigned long now = jiffies;
Peter Zijlstra0e369d72016-05-09 10:38:01 +02009226 struct sched_domain_shared *sds;
Suresh Siddha0b005cf2011-12-01 17:07:34 -08009227 struct sched_domain *sd;
Tim Chenafe06ef2016-11-22 12:23:53 -08009228 int nr_busy, i, cpu = rq->cpu;
Vincent Guittot1aaf90a2015-02-27 16:54:14 +01009229 bool kick = false;
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07009230
Daniel Lezcano4a725622014-01-06 12:34:39 +01009231 if (unlikely(rq->idle_balance))
Vincent Guittot1aaf90a2015-02-27 16:54:14 +01009232 return false;
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07009233
Suresh Siddha1c792db2011-12-01 17:07:32 -08009234 /*
9235 * We may be recently in ticked or tickless idle mode. At the first
9236 * busy tick after returning from idle, we will update the busy stats.
9237 */
Suresh Siddha69e1e812011-12-01 17:07:33 -08009238 set_cpu_sd_state_busy();
Alex Shic1cc0172012-09-10 15:10:58 +08009239 nohz_balance_exit_idle(cpu);
Suresh Siddha0b005cf2011-12-01 17:07:34 -08009240
9241 /*
9242 * None are in tickless mode and hence no need for NOHZ idle load
9243 * balancing.
9244 */
9245 if (likely(!atomic_read(&nohz.nr_cpus)))
Vincent Guittot1aaf90a2015-02-27 16:54:14 +01009246 return false;
Suresh Siddha1c792db2011-12-01 17:07:32 -08009247
9248 if (time_before(now, nohz.next_balance))
Vincent Guittot1aaf90a2015-02-27 16:54:14 +01009249 return false;
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07009250
Suresh Siddha0b005cf2011-12-01 17:07:34 -08009251 if (rq->nr_running >= 2)
Vincent Guittot1aaf90a2015-02-27 16:54:14 +01009252 return true;
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07009253
Peter Zijlstra067491b2011-12-07 14:32:08 +01009254 rcu_read_lock();
Peter Zijlstra0e369d72016-05-09 10:38:01 +02009255 sds = rcu_dereference(per_cpu(sd_llc_shared, cpu));
9256 if (sds) {
9257 /*
9258 * XXX: write a coherent comment on why we do this.
9259 * See also: http://lkml.kernel.org/r/20111202010832.602203411@sbsiddha-desk.sc.intel.com
9260 */
9261 nr_busy = atomic_read(&sds->nr_busy_cpus);
Vincent Guittot1aaf90a2015-02-27 16:54:14 +01009262 if (nr_busy > 1) {
9263 kick = true;
9264 goto unlock;
9265 }
9266
9267 }
9268
9269 sd = rcu_dereference(rq->sd);
9270 if (sd) {
9271 if ((rq->cfs.h_nr_running >= 1) &&
9272 check_cpu_capacity(rq, sd)) {
9273 kick = true;
9274 goto unlock;
9275 }
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07009276 }
Preeti U Murthy37dc6b52013-10-30 08:42:52 +05309277
9278 sd = rcu_dereference(per_cpu(sd_asym, cpu));
Tim Chenafe06ef2016-11-22 12:23:53 -08009279 if (sd) {
9280 for_each_cpu(i, sched_domain_span(sd)) {
9281 if (i == cpu ||
9282 !cpumask_test_cpu(i, nohz.idle_cpus_mask))
9283 continue;
Preeti U Murthy37dc6b52013-10-30 08:42:52 +05309284
Tim Chenafe06ef2016-11-22 12:23:53 -08009285 if (sched_asym_prefer(i, cpu)) {
9286 kick = true;
9287 goto unlock;
9288 }
9289 }
9290 }
Vincent Guittot1aaf90a2015-02-27 16:54:14 +01009291unlock:
Peter Zijlstra067491b2011-12-07 14:32:08 +01009292 rcu_read_unlock();
Vincent Guittot1aaf90a2015-02-27 16:54:14 +01009293 return kick;
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07009294}
9295#else
Daniel Lezcano208cb162014-01-06 12:34:44 +01009296static void nohz_idle_balance(struct rq *this_rq, enum cpu_idle_type idle) { }
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07009297#endif
9298
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01009299/*
9300 * run_rebalance_domains is triggered when needed from the scheduler tick.
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07009301 * Also triggered for nohz idle balancing (with nohz_balancing_kick set).
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01009302 */
Emese Revfy0766f782016-06-20 20:42:34 +02009303static __latent_entropy void run_rebalance_domains(struct softirq_action *h)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01009304{
Daniel Lezcano208cb162014-01-06 12:34:44 +01009305 struct rq *this_rq = this_rq();
Suresh Siddha6eb57e02011-10-03 15:09:01 -07009306 enum cpu_idle_type idle = this_rq->idle_balance ?
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01009307 CPU_IDLE : CPU_NOT_IDLE;
9308
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01009309 /*
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07009310 * If this cpu has a pending nohz_balance_kick, then do the
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01009311 * balancing on behalf of the other idle cpus whose ticks are
Preeti U Murthyd4573c32015-03-26 18:32:44 +05309312 * stopped. Do nohz_idle_balance *before* rebalance_domains to
9313 * give the idle cpus a chance to load balance. Else we may
9314 * load balance only within the local sched_domain hierarchy
9315 * and abort nohz_idle_balance altogether if we pull some load.
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01009316 */
Daniel Lezcano208cb162014-01-06 12:34:44 +01009317 nohz_idle_balance(this_rq, idle);
Preeti U Murthyd4573c32015-03-26 18:32:44 +05309318 rebalance_domains(this_rq, idle);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01009319}
9320
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01009321/*
9322 * Trigger the SCHED_SOFTIRQ if it is time to do periodic load balancing.
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01009323 */
Daniel Lezcano7caff662014-01-06 12:34:38 +01009324void trigger_load_balance(struct rq *rq)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01009325{
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01009326 /* Don't need to rebalance while attached to NULL domain */
Daniel Lezcanoc7260992014-01-06 12:34:45 +01009327 if (unlikely(on_null_domain(rq)))
9328 return;
9329
9330 if (time_after_eq(jiffies, rq->next_balance))
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01009331 raise_softirq(SCHED_SOFTIRQ);
Frederic Weisbecker3451d022011-08-10 23:21:01 +02009332#ifdef CONFIG_NO_HZ_COMMON
Daniel Lezcanoc7260992014-01-06 12:34:45 +01009333 if (nohz_kick_needed(rq))
Daniel Lezcano0aeeeeb2014-01-06 12:34:42 +01009334 nohz_balancer_kick();
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07009335#endif
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01009336}
9337
Christian Ehrhardt0bcdcf22009-11-30 12:16:46 +01009338static void rq_online_fair(struct rq *rq)
9339{
9340 update_sysctl();
Kirill Tkhai0e59bda2014-06-25 12:19:42 +04009341
9342 update_runtime_enabled(rq);
Christian Ehrhardt0bcdcf22009-11-30 12:16:46 +01009343}
9344
9345static void rq_offline_fair(struct rq *rq)
9346{
9347 update_sysctl();
Peter Boonstoppela4c96ae2012-08-09 15:34:47 -07009348
9349 /* Ensure any throttled groups are reachable by pick_next_task */
9350 unthrottle_offline_cfs_rqs(rq);
Christian Ehrhardt0bcdcf22009-11-30 12:16:46 +01009351}
9352
Dhaval Giani55e12e52008-06-24 23:39:43 +05309353#endif /* CONFIG_SMP */
Peter Williamse1d14842007-10-24 18:23:51 +02009354
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02009355/*
9356 * scheduler tick hitting a task of our scheduling class:
9357 */
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01009358static void task_tick_fair(struct rq *rq, struct task_struct *curr, int queued)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02009359{
9360 struct cfs_rq *cfs_rq;
9361 struct sched_entity *se = &curr->se;
9362
9363 for_each_sched_entity(se) {
9364 cfs_rq = cfs_rq_of(se);
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01009365 entity_tick(cfs_rq, se, queued);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02009366 }
Ben Segall18bf2802012-10-04 12:51:20 +02009367
Srikar Dronamrajub52da862015-10-02 07:48:25 +05309368 if (static_branch_unlikely(&sched_numa_balancing))
Peter Zijlstracbee9f82012-10-25 14:16:43 +02009369 task_tick_numa(rq, curr);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02009370}
9371
9372/*
Peter Zijlstracd29fe62009-11-27 17:32:46 +01009373 * called on fork with the child task as argument from the parent's context
9374 * - child not yet on the tasklist
9375 * - preemption disabled
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02009376 */
Peter Zijlstracd29fe62009-11-27 17:32:46 +01009377static void task_fork_fair(struct task_struct *p)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02009378{
Daisuke Nishimura4fc420c2011-12-15 14:36:55 +09009379 struct cfs_rq *cfs_rq;
9380 struct sched_entity *se = &p->se, *curr;
Peter Zijlstracd29fe62009-11-27 17:32:46 +01009381 struct rq *rq = this_rq();
Peter Zijlstra8a8c69c2016-10-04 16:04:35 +02009382 struct rq_flags rf;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02009383
Peter Zijlstra8a8c69c2016-10-04 16:04:35 +02009384 rq_lock(rq, &rf);
Peter Zijlstra861d0342010-08-19 13:31:43 +02009385 update_rq_clock(rq);
9386
Daisuke Nishimura4fc420c2011-12-15 14:36:55 +09009387 cfs_rq = task_cfs_rq(current);
9388 curr = cfs_rq->curr;
Peter Zijlstrae210bff2016-06-16 18:51:48 +02009389 if (curr) {
9390 update_curr(cfs_rq);
Mike Galbraithb5d9d732009-09-08 11:12:28 +02009391 se->vruntime = curr->vruntime;
Peter Zijlstrae210bff2016-06-16 18:51:48 +02009392 }
Peter Zijlstraaeb73b02007-10-15 17:00:05 +02009393 place_entity(cfs_rq, se, 1);
Peter Zijlstra4d78e7b2007-10-15 17:00:04 +02009394
Peter Zijlstracd29fe62009-11-27 17:32:46 +01009395 if (sysctl_sched_child_runs_first && curr && entity_before(curr, se)) {
Dmitry Adamushko87fefa32007-10-15 17:00:08 +02009396 /*
Ingo Molnaredcb60a2007-10-15 17:00:08 +02009397 * Upon rescheduling, sched_class::put_prev_task() will place
9398 * 'current' within the tree based on its new key value.
9399 */
Peter Zijlstra4d78e7b2007-10-15 17:00:04 +02009400 swap(curr->vruntime, se->vruntime);
Kirill Tkhai88751252014-06-29 00:03:57 +04009401 resched_curr(rq);
Peter Zijlstra4d78e7b2007-10-15 17:00:04 +02009402 }
9403
Peter Zijlstra88ec22d2009-12-16 18:04:41 +01009404 se->vruntime -= cfs_rq->min_vruntime;
Peter Zijlstra8a8c69c2016-10-04 16:04:35 +02009405 rq_unlock(rq, &rf);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02009406}
9407
Steven Rostedtcb469842008-01-25 21:08:22 +01009408/*
9409 * Priority of the task has changed. Check to see if we preempt
9410 * the current task.
9411 */
Peter Zijlstrada7a7352011-01-17 17:03:27 +01009412static void
9413prio_changed_fair(struct rq *rq, struct task_struct *p, int oldprio)
Steven Rostedtcb469842008-01-25 21:08:22 +01009414{
Kirill Tkhaida0c1e62014-08-20 13:47:32 +04009415 if (!task_on_rq_queued(p))
Peter Zijlstrada7a7352011-01-17 17:03:27 +01009416 return;
9417
Steven Rostedtcb469842008-01-25 21:08:22 +01009418 /*
9419 * Reschedule if we are currently running on this runqueue and
9420 * our priority decreased, or if we are not currently running on
9421 * this runqueue and our priority is higher than the current's
9422 */
Peter Zijlstrada7a7352011-01-17 17:03:27 +01009423 if (rq->curr == p) {
Steven Rostedtcb469842008-01-25 21:08:22 +01009424 if (p->prio > oldprio)
Kirill Tkhai88751252014-06-29 00:03:57 +04009425 resched_curr(rq);
Steven Rostedtcb469842008-01-25 21:08:22 +01009426 } else
Peter Zijlstra15afe092008-09-20 23:38:02 +02009427 check_preempt_curr(rq, p, 0);
Steven Rostedtcb469842008-01-25 21:08:22 +01009428}
9429
Byungchul Parkdaa59402015-08-20 20:22:00 +09009430static inline bool vruntime_normalized(struct task_struct *p)
9431{
9432 struct sched_entity *se = &p->se;
9433
9434 /*
9435 * In both the TASK_ON_RQ_QUEUED and TASK_ON_RQ_MIGRATING cases,
9436 * the dequeue_entity(.flags=0) will already have normalized the
9437 * vruntime.
9438 */
9439 if (p->on_rq)
9440 return true;
9441
9442 /*
9443 * When !on_rq, vruntime of the task has usually NOT been normalized.
9444 * But there are some cases where it has already been normalized:
9445 *
9446 * - A forked child which is waiting for being woken up by
9447 * wake_up_new_task().
9448 * - A task which has been woken up by try_to_wake_up() and
9449 * waiting for actually being woken up by sched_ttwu_pending().
9450 */
9451 if (!se->sum_exec_runtime || p->state == TASK_WAKING)
9452 return true;
9453
9454 return false;
9455}
9456
Vincent Guittot09a43ac2016-11-08 10:53:45 +01009457#ifdef CONFIG_FAIR_GROUP_SCHED
9458/*
9459 * Propagate the changes of the sched_entity across the tg tree to make it
9460 * visible to the root
9461 */
9462static void propagate_entity_cfs_rq(struct sched_entity *se)
9463{
9464 struct cfs_rq *cfs_rq;
9465
9466 /* Start to propagate at parent */
9467 se = se->parent;
9468
9469 for_each_sched_entity(se) {
9470 cfs_rq = cfs_rq_of(se);
9471
9472 if (cfs_rq_throttled(cfs_rq))
9473 break;
9474
Peter Zijlstra88c06162017-05-06 17:32:43 +02009475 update_load_avg(cfs_rq, se, UPDATE_TG);
Vincent Guittot09a43ac2016-11-08 10:53:45 +01009476 }
9477}
9478#else
9479static void propagate_entity_cfs_rq(struct sched_entity *se) { }
9480#endif
9481
Vincent Guittotdf217912016-11-08 10:53:42 +01009482static void detach_entity_cfs_rq(struct sched_entity *se)
Peter Zijlstrada7a7352011-01-17 17:03:27 +01009483{
Peter Zijlstrada7a7352011-01-17 17:03:27 +01009484 struct cfs_rq *cfs_rq = cfs_rq_of(se);
9485
Yuyang Du9d89c252015-07-15 08:04:37 +08009486 /* Catch up with the cfs_rq and remove our load when we leave */
Peter Zijlstra88c06162017-05-06 17:32:43 +02009487 update_load_avg(cfs_rq, se, 0);
Byungchul Parka05e8c52015-08-20 20:21:56 +09009488 detach_entity_load_avg(cfs_rq, se);
Peter Zijlstra7c3edd22016-07-13 10:56:25 +02009489 update_tg_load_avg(cfs_rq, false);
Vincent Guittot09a43ac2016-11-08 10:53:45 +01009490 propagate_entity_cfs_rq(se);
Peter Zijlstrada7a7352011-01-17 17:03:27 +01009491}
9492
Vincent Guittotdf217912016-11-08 10:53:42 +01009493static void attach_entity_cfs_rq(struct sched_entity *se)
Steven Rostedtcb469842008-01-25 21:08:22 +01009494{
Byungchul Parkdaa59402015-08-20 20:22:00 +09009495 struct cfs_rq *cfs_rq = cfs_rq_of(se);
Byungchul Park7855a352015-08-10 18:02:55 +09009496
9497#ifdef CONFIG_FAIR_GROUP_SCHED
Michael wangeb7a59b2014-02-20 11:14:53 +08009498 /*
9499 * Since the real-depth could have been changed (only FAIR
9500 * class maintain depth value), reset depth properly.
9501 */
9502 se->depth = se->parent ? se->parent->depth + 1 : 0;
9503#endif
Byungchul Park7855a352015-08-10 18:02:55 +09009504
Vincent Guittotdf217912016-11-08 10:53:42 +01009505 /* Synchronize entity with its cfs_rq */
Peter Zijlstra88c06162017-05-06 17:32:43 +02009506 update_load_avg(cfs_rq, se, sched_feat(ATTACH_AGE_LOAD) ? 0 : SKIP_AGE_LOAD);
Byungchul Parkdaa59402015-08-20 20:22:00 +09009507 attach_entity_load_avg(cfs_rq, se);
Peter Zijlstra7c3edd22016-07-13 10:56:25 +02009508 update_tg_load_avg(cfs_rq, false);
Vincent Guittot09a43ac2016-11-08 10:53:45 +01009509 propagate_entity_cfs_rq(se);
Vincent Guittotdf217912016-11-08 10:53:42 +01009510}
9511
9512static void detach_task_cfs_rq(struct task_struct *p)
9513{
9514 struct sched_entity *se = &p->se;
9515 struct cfs_rq *cfs_rq = cfs_rq_of(se);
9516
9517 if (!vruntime_normalized(p)) {
9518 /*
9519 * Fix up our vruntime so that the current sleep doesn't
9520 * cause 'unlimited' sleep bonus.
9521 */
9522 place_entity(cfs_rq, se, 0);
9523 se->vruntime -= cfs_rq->min_vruntime;
9524 }
9525
9526 detach_entity_cfs_rq(se);
9527}
9528
9529static void attach_task_cfs_rq(struct task_struct *p)
9530{
9531 struct sched_entity *se = &p->se;
9532 struct cfs_rq *cfs_rq = cfs_rq_of(se);
9533
9534 attach_entity_cfs_rq(se);
Byungchul Park6efdb102015-08-20 20:21:59 +09009535
Byungchul Parkdaa59402015-08-20 20:22:00 +09009536 if (!vruntime_normalized(p))
9537 se->vruntime += cfs_rq->min_vruntime;
9538}
Byungchul Park7855a352015-08-10 18:02:55 +09009539
Byungchul Parkdaa59402015-08-20 20:22:00 +09009540static void switched_from_fair(struct rq *rq, struct task_struct *p)
9541{
9542 detach_task_cfs_rq(p);
9543}
9544
9545static void switched_to_fair(struct rq *rq, struct task_struct *p)
9546{
9547 attach_task_cfs_rq(p);
9548
9549 if (task_on_rq_queued(p)) {
Byungchul Park7855a352015-08-10 18:02:55 +09009550 /*
Byungchul Parkdaa59402015-08-20 20:22:00 +09009551 * We were most likely switched from sched_rt, so
9552 * kick off the schedule if running, otherwise just see
9553 * if we can still preempt the current task.
Byungchul Park7855a352015-08-10 18:02:55 +09009554 */
Byungchul Parkdaa59402015-08-20 20:22:00 +09009555 if (rq->curr == p)
9556 resched_curr(rq);
9557 else
9558 check_preempt_curr(rq, p, 0);
Byungchul Park7855a352015-08-10 18:02:55 +09009559 }
Steven Rostedtcb469842008-01-25 21:08:22 +01009560}
9561
Srivatsa Vaddagiri83b699e2007-10-15 17:00:08 +02009562/* Account for a task changing its policy or group.
9563 *
9564 * This routine is mostly called to set cfs_rq->curr field when a task
9565 * migrates between groups/classes.
9566 */
9567static void set_curr_task_fair(struct rq *rq)
9568{
9569 struct sched_entity *se = &rq->curr->se;
9570
Paul Turnerec12cb72011-07-21 09:43:30 -07009571 for_each_sched_entity(se) {
9572 struct cfs_rq *cfs_rq = cfs_rq_of(se);
9573
9574 set_next_entity(cfs_rq, se);
9575 /* ensure bandwidth has been allocated on our new cfs_rq */
9576 account_cfs_rq_runtime(cfs_rq, 0);
9577 }
Srivatsa Vaddagiri83b699e2007-10-15 17:00:08 +02009578}
9579
Peter Zijlstra029632f2011-10-25 10:00:11 +02009580void init_cfs_rq(struct cfs_rq *cfs_rq)
9581{
Davidlohr Buesobfb06882017-09-08 16:14:55 -07009582 cfs_rq->tasks_timeline = RB_ROOT_CACHED;
Peter Zijlstra029632f2011-10-25 10:00:11 +02009583 cfs_rq->min_vruntime = (u64)(-(1LL << 20));
9584#ifndef CONFIG_64BIT
9585 cfs_rq->min_vruntime_copy = cfs_rq->min_vruntime;
9586#endif
Alex Shi141965c2013-06-26 13:05:39 +08009587#ifdef CONFIG_SMP
Peter Zijlstra2a2f5d4e2017-05-08 16:51:41 +02009588 raw_spin_lock_init(&cfs_rq->removed.lock);
Paul Turner9ee474f2012-10-04 13:18:30 +02009589#endif
Peter Zijlstra029632f2011-10-25 10:00:11 +02009590}
9591
Peter Zijlstra810b3812008-02-29 15:21:01 -05009592#ifdef CONFIG_FAIR_GROUP_SCHED
Vincent Guittotea86cb42016-06-17 13:38:55 +02009593static void task_set_group_fair(struct task_struct *p)
9594{
9595 struct sched_entity *se = &p->se;
9596
9597 set_task_rq(p, task_cpu(p));
9598 se->depth = se->parent ? se->parent->depth + 1 : 0;
9599}
9600
Peter Zijlstrabc54da22015-08-31 17:13:55 +02009601static void task_move_group_fair(struct task_struct *p)
Peter Zijlstra810b3812008-02-29 15:21:01 -05009602{
Byungchul Parkdaa59402015-08-20 20:22:00 +09009603 detach_task_cfs_rq(p);
Peter Zijlstrab2b5ce02010-10-15 15:24:15 +02009604 set_task_rq(p, task_cpu(p));
Byungchul Park6efdb102015-08-20 20:21:59 +09009605
9606#ifdef CONFIG_SMP
9607 /* Tell se's cfs_rq has been changed -- migrated */
9608 p->se.avg.last_update_time = 0;
9609#endif
Byungchul Parkdaa59402015-08-20 20:22:00 +09009610 attach_task_cfs_rq(p);
Peter Zijlstra810b3812008-02-29 15:21:01 -05009611}
Peter Zijlstra029632f2011-10-25 10:00:11 +02009612
Vincent Guittotea86cb42016-06-17 13:38:55 +02009613static void task_change_group_fair(struct task_struct *p, int type)
9614{
9615 switch (type) {
9616 case TASK_SET_GROUP:
9617 task_set_group_fair(p);
9618 break;
9619
9620 case TASK_MOVE_GROUP:
9621 task_move_group_fair(p);
9622 break;
9623 }
9624}
9625
Peter Zijlstra029632f2011-10-25 10:00:11 +02009626void free_fair_sched_group(struct task_group *tg)
9627{
9628 int i;
9629
9630 destroy_cfs_bandwidth(tg_cfs_bandwidth(tg));
9631
9632 for_each_possible_cpu(i) {
9633 if (tg->cfs_rq)
9634 kfree(tg->cfs_rq[i]);
Peter Zijlstra6fe1f342016-01-21 22:24:16 +01009635 if (tg->se)
Peter Zijlstra029632f2011-10-25 10:00:11 +02009636 kfree(tg->se[i]);
9637 }
9638
9639 kfree(tg->cfs_rq);
9640 kfree(tg->se);
9641}
9642
9643int alloc_fair_sched_group(struct task_group *tg, struct task_group *parent)
9644{
Peter Zijlstra029632f2011-10-25 10:00:11 +02009645 struct sched_entity *se;
Peter Zijlstrab7fa30c2016-06-09 15:07:50 +02009646 struct cfs_rq *cfs_rq;
Peter Zijlstra029632f2011-10-25 10:00:11 +02009647 int i;
9648
9649 tg->cfs_rq = kzalloc(sizeof(cfs_rq) * nr_cpu_ids, GFP_KERNEL);
9650 if (!tg->cfs_rq)
9651 goto err;
9652 tg->se = kzalloc(sizeof(se) * nr_cpu_ids, GFP_KERNEL);
9653 if (!tg->se)
9654 goto err;
9655
9656 tg->shares = NICE_0_LOAD;
9657
9658 init_cfs_bandwidth(tg_cfs_bandwidth(tg));
9659
9660 for_each_possible_cpu(i) {
9661 cfs_rq = kzalloc_node(sizeof(struct cfs_rq),
9662 GFP_KERNEL, cpu_to_node(i));
9663 if (!cfs_rq)
9664 goto err;
9665
9666 se = kzalloc_node(sizeof(struct sched_entity),
9667 GFP_KERNEL, cpu_to_node(i));
9668 if (!se)
9669 goto err_free_rq;
9670
9671 init_cfs_rq(cfs_rq);
9672 init_tg_cfs_entry(tg, cfs_rq, se, i, parent->se[i]);
Yuyang Du540247f2015-07-15 08:04:39 +08009673 init_entity_runnable_average(se);
Peter Zijlstra029632f2011-10-25 10:00:11 +02009674 }
9675
9676 return 1;
9677
9678err_free_rq:
9679 kfree(cfs_rq);
9680err:
9681 return 0;
9682}
9683
Peter Zijlstra8663e242016-06-22 14:58:02 +02009684void online_fair_sched_group(struct task_group *tg)
9685{
9686 struct sched_entity *se;
9687 struct rq *rq;
9688 int i;
9689
9690 for_each_possible_cpu(i) {
9691 rq = cpu_rq(i);
9692 se = tg->se[i];
9693
9694 raw_spin_lock_irq(&rq->lock);
Peter Zijlstra4126bad2016-10-03 16:20:59 +02009695 update_rq_clock(rq);
Vincent Guittotd0326692016-11-08 10:53:47 +01009696 attach_entity_cfs_rq(se);
Peter Zijlstra55e16d32016-06-22 15:14:26 +02009697 sync_throttle(tg, i);
Peter Zijlstra8663e242016-06-22 14:58:02 +02009698 raw_spin_unlock_irq(&rq->lock);
9699 }
9700}
9701
Peter Zijlstra6fe1f342016-01-21 22:24:16 +01009702void unregister_fair_sched_group(struct task_group *tg)
Peter Zijlstra029632f2011-10-25 10:00:11 +02009703{
Peter Zijlstra029632f2011-10-25 10:00:11 +02009704 unsigned long flags;
Peter Zijlstra6fe1f342016-01-21 22:24:16 +01009705 struct rq *rq;
9706 int cpu;
Peter Zijlstra029632f2011-10-25 10:00:11 +02009707
Peter Zijlstra6fe1f342016-01-21 22:24:16 +01009708 for_each_possible_cpu(cpu) {
9709 if (tg->se[cpu])
9710 remove_entity_load_avg(tg->se[cpu]);
Peter Zijlstra029632f2011-10-25 10:00:11 +02009711
Peter Zijlstra6fe1f342016-01-21 22:24:16 +01009712 /*
9713 * Only empty task groups can be destroyed; so we can speculatively
9714 * check on_list without danger of it being re-added.
9715 */
9716 if (!tg->cfs_rq[cpu]->on_list)
9717 continue;
9718
9719 rq = cpu_rq(cpu);
9720
9721 raw_spin_lock_irqsave(&rq->lock, flags);
9722 list_del_leaf_cfs_rq(tg->cfs_rq[cpu]);
9723 raw_spin_unlock_irqrestore(&rq->lock, flags);
9724 }
Peter Zijlstra029632f2011-10-25 10:00:11 +02009725}
9726
9727void init_tg_cfs_entry(struct task_group *tg, struct cfs_rq *cfs_rq,
9728 struct sched_entity *se, int cpu,
9729 struct sched_entity *parent)
9730{
9731 struct rq *rq = cpu_rq(cpu);
9732
9733 cfs_rq->tg = tg;
9734 cfs_rq->rq = rq;
Peter Zijlstra029632f2011-10-25 10:00:11 +02009735 init_cfs_rq_runtime(cfs_rq);
9736
9737 tg->cfs_rq[cpu] = cfs_rq;
9738 tg->se[cpu] = se;
9739
9740 /* se could be NULL for root_task_group */
9741 if (!se)
9742 return;
9743
Peter Zijlstrafed14d42012-02-11 06:05:00 +01009744 if (!parent) {
Peter Zijlstra029632f2011-10-25 10:00:11 +02009745 se->cfs_rq = &rq->cfs;
Peter Zijlstrafed14d42012-02-11 06:05:00 +01009746 se->depth = 0;
9747 } else {
Peter Zijlstra029632f2011-10-25 10:00:11 +02009748 se->cfs_rq = parent->my_q;
Peter Zijlstrafed14d42012-02-11 06:05:00 +01009749 se->depth = parent->depth + 1;
9750 }
Peter Zijlstra029632f2011-10-25 10:00:11 +02009751
9752 se->my_q = cfs_rq;
Paul Turner0ac9b1c2013-10-16 11:16:27 -07009753 /* guarantee group entities always have weight */
9754 update_load_set(&se->load, NICE_0_LOAD);
Peter Zijlstra029632f2011-10-25 10:00:11 +02009755 se->parent = parent;
9756}
9757
9758static DEFINE_MUTEX(shares_mutex);
9759
9760int sched_group_set_shares(struct task_group *tg, unsigned long shares)
9761{
9762 int i;
Peter Zijlstra029632f2011-10-25 10:00:11 +02009763
9764 /*
9765 * We can't change the weight of the root cgroup.
9766 */
9767 if (!tg->se[0])
9768 return -EINVAL;
9769
9770 shares = clamp(shares, scale_load(MIN_SHARES), scale_load(MAX_SHARES));
9771
9772 mutex_lock(&shares_mutex);
9773 if (tg->shares == shares)
9774 goto done;
9775
9776 tg->shares = shares;
9777 for_each_possible_cpu(i) {
9778 struct rq *rq = cpu_rq(i);
Peter Zijlstra8a8c69c2016-10-04 16:04:35 +02009779 struct sched_entity *se = tg->se[i];
9780 struct rq_flags rf;
Peter Zijlstra029632f2011-10-25 10:00:11 +02009781
Peter Zijlstra029632f2011-10-25 10:00:11 +02009782 /* Propagate contribution to hierarchy */
Peter Zijlstra8a8c69c2016-10-04 16:04:35 +02009783 rq_lock_irqsave(rq, &rf);
Frederic Weisbecker71b1da42013-04-12 01:50:59 +02009784 update_rq_clock(rq);
Vincent Guittot89ee0482016-12-21 16:50:26 +01009785 for_each_sched_entity(se) {
Peter Zijlstra88c06162017-05-06 17:32:43 +02009786 update_load_avg(cfs_rq_of(se), se, UPDATE_TG);
Peter Zijlstra1ea6c462017-05-06 15:59:54 +02009787 update_cfs_group(se);
Vincent Guittot89ee0482016-12-21 16:50:26 +01009788 }
Peter Zijlstra8a8c69c2016-10-04 16:04:35 +02009789 rq_unlock_irqrestore(rq, &rf);
Peter Zijlstra029632f2011-10-25 10:00:11 +02009790 }
9791
9792done:
9793 mutex_unlock(&shares_mutex);
9794 return 0;
9795}
9796#else /* CONFIG_FAIR_GROUP_SCHED */
9797
9798void free_fair_sched_group(struct task_group *tg) { }
9799
9800int alloc_fair_sched_group(struct task_group *tg, struct task_group *parent)
9801{
9802 return 1;
9803}
9804
Peter Zijlstra8663e242016-06-22 14:58:02 +02009805void online_fair_sched_group(struct task_group *tg) { }
9806
Peter Zijlstra6fe1f342016-01-21 22:24:16 +01009807void unregister_fair_sched_group(struct task_group *tg) { }
Peter Zijlstra029632f2011-10-25 10:00:11 +02009808
9809#endif /* CONFIG_FAIR_GROUP_SCHED */
9810
Peter Zijlstra810b3812008-02-29 15:21:01 -05009811
H Hartley Sweeten6d686f42010-01-13 20:21:52 -07009812static unsigned int get_rr_interval_fair(struct rq *rq, struct task_struct *task)
Peter Williams0d721ce2009-09-21 01:31:53 +00009813{
9814 struct sched_entity *se = &task->se;
Peter Williams0d721ce2009-09-21 01:31:53 +00009815 unsigned int rr_interval = 0;
9816
9817 /*
9818 * Time slice is 0 for SCHED_OTHER tasks that are on an otherwise
9819 * idle runqueue:
9820 */
Peter Williams0d721ce2009-09-21 01:31:53 +00009821 if (rq->cfs.load.weight)
Zhu Yanhaia59f4e02013-01-08 12:56:52 +08009822 rr_interval = NS_TO_JIFFIES(sched_slice(cfs_rq_of(se), se));
Peter Williams0d721ce2009-09-21 01:31:53 +00009823
9824 return rr_interval;
9825}
9826
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02009827/*
9828 * All the scheduling class methods:
9829 */
Peter Zijlstra029632f2011-10-25 10:00:11 +02009830const struct sched_class fair_sched_class = {
Ingo Molnar5522d5d2007-10-15 17:00:12 +02009831 .next = &idle_sched_class,
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02009832 .enqueue_task = enqueue_task_fair,
9833 .dequeue_task = dequeue_task_fair,
9834 .yield_task = yield_task_fair,
Mike Galbraithd95f4122011-02-01 09:50:51 -05009835 .yield_to_task = yield_to_task_fair,
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02009836
Ingo Molnar2e09bf52007-10-15 17:00:05 +02009837 .check_preempt_curr = check_preempt_wakeup,
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02009838
9839 .pick_next_task = pick_next_task_fair,
9840 .put_prev_task = put_prev_task_fair,
9841
Peter Williams681f3e62007-10-24 18:23:51 +02009842#ifdef CONFIG_SMP
Li Zefan4ce72a22008-10-22 15:25:26 +08009843 .select_task_rq = select_task_rq_fair,
Paul Turner0a74bef2012-10-04 13:18:30 +02009844 .migrate_task_rq = migrate_task_rq_fair,
Alex Shi141965c2013-06-26 13:05:39 +08009845
Christian Ehrhardt0bcdcf22009-11-30 12:16:46 +01009846 .rq_online = rq_online_fair,
9847 .rq_offline = rq_offline_fair,
Peter Zijlstra88ec22d2009-12-16 18:04:41 +01009848
Yuyang Du12695572015-07-15 08:04:40 +08009849 .task_dead = task_dead_fair,
Peter Zijlstrac5b28032015-05-15 17:43:35 +02009850 .set_cpus_allowed = set_cpus_allowed_common,
Peter Williams681f3e62007-10-24 18:23:51 +02009851#endif
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02009852
Srivatsa Vaddagiri83b699e2007-10-15 17:00:08 +02009853 .set_curr_task = set_curr_task_fair,
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02009854 .task_tick = task_tick_fair,
Peter Zijlstracd29fe62009-11-27 17:32:46 +01009855 .task_fork = task_fork_fair,
Steven Rostedtcb469842008-01-25 21:08:22 +01009856
9857 .prio_changed = prio_changed_fair,
Peter Zijlstrada7a7352011-01-17 17:03:27 +01009858 .switched_from = switched_from_fair,
Steven Rostedtcb469842008-01-25 21:08:22 +01009859 .switched_to = switched_to_fair,
Peter Zijlstra810b3812008-02-29 15:21:01 -05009860
Peter Williams0d721ce2009-09-21 01:31:53 +00009861 .get_rr_interval = get_rr_interval_fair,
9862
Stanislaw Gruszka6e998912014-11-12 16:58:44 +01009863 .update_curr = update_curr_fair,
9864
Peter Zijlstra810b3812008-02-29 15:21:01 -05009865#ifdef CONFIG_FAIR_GROUP_SCHED
Vincent Guittotea86cb42016-06-17 13:38:55 +02009866 .task_change_group = task_change_group_fair,
Peter Zijlstra810b3812008-02-29 15:21:01 -05009867#endif
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02009868};
9869
9870#ifdef CONFIG_SCHED_DEBUG
Peter Zijlstra029632f2011-10-25 10:00:11 +02009871void print_cfs_stats(struct seq_file *m, int cpu)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02009872{
Tejun Heoa9e7f652017-04-25 17:43:50 -07009873 struct cfs_rq *cfs_rq, *pos;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02009874
Peter Zijlstra5973e5b2008-01-25 21:08:34 +01009875 rcu_read_lock();
Tejun Heoa9e7f652017-04-25 17:43:50 -07009876 for_each_leaf_cfs_rq_safe(cpu_rq(cpu), cfs_rq, pos)
Ingo Molnar5cef9ec2007-08-09 11:16:47 +02009877 print_cfs_rq(m, cpu, cfs_rq);
Peter Zijlstra5973e5b2008-01-25 21:08:34 +01009878 rcu_read_unlock();
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02009879}
Srikar Dronamraju397f2372015-06-25 22:51:43 +05309880
9881#ifdef CONFIG_NUMA_BALANCING
9882void show_numa_stats(struct task_struct *p, struct seq_file *m)
9883{
9884 int node;
9885 unsigned long tsf = 0, tpf = 0, gsf = 0, gpf = 0;
9886
9887 for_each_online_node(node) {
9888 if (p->numa_faults) {
9889 tsf = p->numa_faults[task_faults_idx(NUMA_MEM, node, 0)];
9890 tpf = p->numa_faults[task_faults_idx(NUMA_MEM, node, 1)];
9891 }
9892 if (p->numa_group) {
9893 gsf = p->numa_group->faults[task_faults_idx(NUMA_MEM, node, 0)],
9894 gpf = p->numa_group->faults[task_faults_idx(NUMA_MEM, node, 1)];
9895 }
9896 print_numa_stats(m, node, tsf, tpf, gsf, gpf);
9897 }
9898}
9899#endif /* CONFIG_NUMA_BALANCING */
9900#endif /* CONFIG_SCHED_DEBUG */
Peter Zijlstra029632f2011-10-25 10:00:11 +02009901
9902__init void init_sched_fair_class(void)
9903{
9904#ifdef CONFIG_SMP
9905 open_softirq(SCHED_SOFTIRQ, run_rebalance_domains);
9906
Frederic Weisbecker3451d022011-08-10 23:21:01 +02009907#ifdef CONFIG_NO_HZ_COMMON
Diwakar Tundlam554ceca2012-03-07 14:44:26 -08009908 nohz.next_balance = jiffies;
Peter Zijlstra029632f2011-10-25 10:00:11 +02009909 zalloc_cpumask_var(&nohz.idle_cpus_mask, GFP_NOWAIT);
Peter Zijlstra029632f2011-10-25 10:00:11 +02009910#endif
9911#endif /* SMP */
9912
9913}