blob: 8cf26fd7ce586ea5c57fcf65eead4d1ea13e8793 [file] [log] [blame]
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001/*
2 * Completely Fair Scheduling (CFS) Class (SCHED_NORMAL/SCHED_BATCH)
3 *
4 * Copyright (C) 2007 Red Hat, Inc., Ingo Molnar <mingo@redhat.com>
5 *
6 * Interactivity improvements by Mike Galbraith
7 * (C) 2007 Mike Galbraith <efault@gmx.de>
8 *
9 * Various enhancements by Dmitry Adamushko.
10 * (C) 2007 Dmitry Adamushko <dmitry.adamushko@gmail.com>
11 *
12 * Group scheduling enhancements by Srivatsa Vaddagiri
13 * Copyright IBM Corporation, 2007
14 * Author: Srivatsa Vaddagiri <vatsa@linux.vnet.ibm.com>
15 *
16 * Scaled math optimizations by Thomas Gleixner
17 * Copyright (C) 2007, Thomas Gleixner <tglx@linutronix.de>
Peter Zijlstra21805082007-08-25 18:41:53 +020018 *
19 * Adaptive scheduling granularity, math enhancements by Peter Zijlstra
Peter Zijlstra90eec102015-11-16 11:08:45 +010020 * Copyright (C) 2007 Red Hat, Inc., Peter Zijlstra
Ingo Molnarbf0f6f22007-07-09 18:51:58 +020021 */
22
Christian Ehrhardt1983a922009-11-30 12:16:47 +010023#include <linux/sched.h>
Mel Gormancb251762016-02-05 09:08:36 +000024#include <linux/latencytop.h>
Sisir Koppaka3436ae12011-03-26 18:22:55 +053025#include <linux/cpumask.h>
Nicolas Pitre83a0a962014-09-04 11:32:10 -040026#include <linux/cpuidle.h>
Peter Zijlstra029632f2011-10-25 10:00:11 +020027#include <linux/slab.h>
28#include <linux/profile.h>
29#include <linux/interrupt.h>
Peter Zijlstracbee9f82012-10-25 14:16:43 +020030#include <linux/mempolicy.h>
Mel Gormane14808b2012-11-19 10:59:15 +000031#include <linux/migrate.h>
Peter Zijlstracbee9f82012-10-25 14:16:43 +020032#include <linux/task_work.h>
Peter Zijlstra029632f2011-10-25 10:00:11 +020033
34#include <trace/events/sched.h>
35
36#include "sched.h"
Arjan van de Ven97455122008-01-25 21:08:34 +010037
Ingo Molnarbf0f6f22007-07-09 18:51:58 +020038/*
Peter Zijlstra21805082007-08-25 18:41:53 +020039 * Targeted preemption latency for CPU-bound tasks:
Takuya Yoshikawa864616e2010-10-14 16:09:13 +090040 * (default: 6ms * (1 + ilog(ncpus)), units: nanoseconds)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +020041 *
Peter Zijlstra21805082007-08-25 18:41:53 +020042 * NOTE: this latency value is not the same as the concept of
Ingo Molnard274a4c2007-10-15 17:00:14 +020043 * 'timeslice length' - timeslices in CFS are of variable length
44 * and have no persistent notion like in traditional, time-slice
45 * based scheduling concepts.
Ingo Molnarbf0f6f22007-07-09 18:51:58 +020046 *
Ingo Molnard274a4c2007-10-15 17:00:14 +020047 * (to see the precise effective timeslice length of your workload,
48 * run vmstat and monitor the context-switches (cs) field)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +020049 */
Mike Galbraith21406922010-03-11 17:17:15 +010050unsigned int sysctl_sched_latency = 6000000ULL;
51unsigned int normalized_sysctl_sched_latency = 6000000ULL;
Ingo Molnar2bd8e6d2007-10-15 17:00:02 +020052
53/*
Christian Ehrhardt1983a922009-11-30 12:16:47 +010054 * The initial- and re-scaling of tunables is configurable
55 * (default SCHED_TUNABLESCALING_LOG = *(1+ilog(ncpus))
56 *
57 * Options are:
58 * SCHED_TUNABLESCALING_NONE - unscaled, always *1
59 * SCHED_TUNABLESCALING_LOG - scaled logarithmical, *1+ilog(ncpus)
60 * SCHED_TUNABLESCALING_LINEAR - scaled linear, *ncpus
61 */
62enum sched_tunable_scaling sysctl_sched_tunable_scaling
63 = SCHED_TUNABLESCALING_LOG;
64
65/*
Peter Zijlstrab2be5e92007-11-09 22:39:37 +010066 * Minimal preemption granularity for CPU-bound tasks:
Takuya Yoshikawa864616e2010-10-14 16:09:13 +090067 * (default: 0.75 msec * (1 + ilog(ncpus)), units: nanoseconds)
Peter Zijlstrab2be5e92007-11-09 22:39:37 +010068 */
Ingo Molnar0bf377b2010-09-12 08:14:52 +020069unsigned int sysctl_sched_min_granularity = 750000ULL;
70unsigned int normalized_sysctl_sched_min_granularity = 750000ULL;
Peter Zijlstrab2be5e92007-11-09 22:39:37 +010071
72/*
73 * is kept at sysctl_sched_latency / sysctl_sched_min_granularity
74 */
Ingo Molnar0bf377b2010-09-12 08:14:52 +020075static unsigned int sched_nr_latency = 8;
Peter Zijlstrab2be5e92007-11-09 22:39:37 +010076
77/*
Mike Galbraith2bba22c2009-09-09 15:41:37 +020078 * After fork, child runs first. If set to 0 (default) then
Ingo Molnar2bd8e6d2007-10-15 17:00:02 +020079 * parent will (try to) run first.
80 */
Mike Galbraith2bba22c2009-09-09 15:41:37 +020081unsigned int sysctl_sched_child_runs_first __read_mostly;
Peter Zijlstra21805082007-08-25 18:41:53 +020082
83/*
Ingo Molnarbf0f6f22007-07-09 18:51:58 +020084 * SCHED_OTHER wake-up granularity.
Mike Galbraith172e0822009-09-09 15:41:37 +020085 * (default: 1 msec * (1 + ilog(ncpus)), units: nanoseconds)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +020086 *
87 * This option delays the preemption effects of decoupled workloads
88 * and reduces their over-scheduling. Synchronous workloads will still
89 * have immediate wakeup/sleep latencies.
90 */
Mike Galbraith172e0822009-09-09 15:41:37 +020091unsigned int sysctl_sched_wakeup_granularity = 1000000UL;
Christian Ehrhardt0bcdcf22009-11-30 12:16:46 +010092unsigned int normalized_sysctl_sched_wakeup_granularity = 1000000UL;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +020093
Ingo Molnarda84d962007-10-15 17:00:18 +020094const_debug unsigned int sysctl_sched_migration_cost = 500000UL;
95
Paul Turnerec12cb72011-07-21 09:43:30 -070096#ifdef CONFIG_CFS_BANDWIDTH
97/*
98 * Amount of runtime to allocate from global (tg) to local (per-cfs_rq) pool
99 * each time a cfs_rq requests quota.
100 *
101 * Note: in the case that the slice exceeds the runtime remaining (either due
102 * to consumption or the quota being specified to be smaller than the slice)
103 * we will always only issue the remaining available time.
104 *
105 * default: 5 msec, units: microseconds
106 */
107unsigned int sysctl_sched_cfs_bandwidth_slice = 5000UL;
108#endif
109
Morten Rasmussen32731632016-07-25 14:34:26 +0100110/*
111 * The margin used when comparing utilization with CPU capacity:
Morten Rasmussen893c5d22016-10-14 14:41:12 +0100112 * util * margin < capacity * 1024
Morten Rasmussen32731632016-07-25 14:34:26 +0100113 */
114unsigned int capacity_margin = 1280; /* ~20% */
115
Paul Gortmaker85276322013-04-19 15:10:50 -0400116static inline void update_load_add(struct load_weight *lw, unsigned long inc)
117{
118 lw->weight += inc;
119 lw->inv_weight = 0;
120}
121
122static inline void update_load_sub(struct load_weight *lw, unsigned long dec)
123{
124 lw->weight -= dec;
125 lw->inv_weight = 0;
126}
127
128static inline void update_load_set(struct load_weight *lw, unsigned long w)
129{
130 lw->weight = w;
131 lw->inv_weight = 0;
132}
133
Peter Zijlstra029632f2011-10-25 10:00:11 +0200134/*
135 * Increase the granularity value when there are more CPUs,
136 * because with more CPUs the 'effective latency' as visible
137 * to users decreases. But the relationship is not linear,
138 * so pick a second-best guess by going with the log2 of the
139 * number of CPUs.
140 *
141 * This idea comes from the SD scheduler of Con Kolivas:
142 */
Nicholas Mc Guire58ac93e2015-05-15 21:05:42 +0200143static unsigned int get_update_sysctl_factor(void)
Peter Zijlstra029632f2011-10-25 10:00:11 +0200144{
Nicholas Mc Guire58ac93e2015-05-15 21:05:42 +0200145 unsigned int cpus = min_t(unsigned int, num_online_cpus(), 8);
Peter Zijlstra029632f2011-10-25 10:00:11 +0200146 unsigned int factor;
147
148 switch (sysctl_sched_tunable_scaling) {
149 case SCHED_TUNABLESCALING_NONE:
150 factor = 1;
151 break;
152 case SCHED_TUNABLESCALING_LINEAR:
153 factor = cpus;
154 break;
155 case SCHED_TUNABLESCALING_LOG:
156 default:
157 factor = 1 + ilog2(cpus);
158 break;
159 }
160
161 return factor;
162}
163
164static void update_sysctl(void)
165{
166 unsigned int factor = get_update_sysctl_factor();
167
168#define SET_SYSCTL(name) \
169 (sysctl_##name = (factor) * normalized_sysctl_##name)
170 SET_SYSCTL(sched_min_granularity);
171 SET_SYSCTL(sched_latency);
172 SET_SYSCTL(sched_wakeup_granularity);
173#undef SET_SYSCTL
174}
175
176void sched_init_granularity(void)
177{
178 update_sysctl();
179}
180
Peter Zijlstra9dbdb152013-11-18 18:27:06 +0100181#define WMULT_CONST (~0U)
Peter Zijlstra029632f2011-10-25 10:00:11 +0200182#define WMULT_SHIFT 32
183
Peter Zijlstra9dbdb152013-11-18 18:27:06 +0100184static void __update_inv_weight(struct load_weight *lw)
Peter Zijlstra029632f2011-10-25 10:00:11 +0200185{
Peter Zijlstra9dbdb152013-11-18 18:27:06 +0100186 unsigned long w;
Peter Zijlstra029632f2011-10-25 10:00:11 +0200187
Peter Zijlstra9dbdb152013-11-18 18:27:06 +0100188 if (likely(lw->inv_weight))
189 return;
190
191 w = scale_load_down(lw->weight);
192
193 if (BITS_PER_LONG > 32 && unlikely(w >= WMULT_CONST))
194 lw->inv_weight = 1;
195 else if (unlikely(!w))
196 lw->inv_weight = WMULT_CONST;
Peter Zijlstra029632f2011-10-25 10:00:11 +0200197 else
Peter Zijlstra9dbdb152013-11-18 18:27:06 +0100198 lw->inv_weight = WMULT_CONST / w;
199}
Peter Zijlstra029632f2011-10-25 10:00:11 +0200200
Peter Zijlstra9dbdb152013-11-18 18:27:06 +0100201/*
202 * delta_exec * weight / lw.weight
203 * OR
204 * (delta_exec * (weight * lw->inv_weight)) >> WMULT_SHIFT
205 *
Yuyang Du1c3de5e2016-03-30 07:07:51 +0800206 * Either weight := NICE_0_LOAD and lw \e sched_prio_to_wmult[], in which case
Peter Zijlstra9dbdb152013-11-18 18:27:06 +0100207 * we're guaranteed shift stays positive because inv_weight is guaranteed to
208 * fit 32 bits, and NICE_0_LOAD gives another 10 bits; therefore shift >= 22.
209 *
210 * Or, weight =< lw.weight (because lw.weight is the runqueue weight), thus
211 * weight/lw.weight <= 1, and therefore our shift will also be positive.
212 */
213static u64 __calc_delta(u64 delta_exec, unsigned long weight, struct load_weight *lw)
214{
215 u64 fact = scale_load_down(weight);
216 int shift = WMULT_SHIFT;
Peter Zijlstra029632f2011-10-25 10:00:11 +0200217
Peter Zijlstra9dbdb152013-11-18 18:27:06 +0100218 __update_inv_weight(lw);
219
220 if (unlikely(fact >> 32)) {
221 while (fact >> 32) {
222 fact >>= 1;
223 shift--;
224 }
Peter Zijlstra029632f2011-10-25 10:00:11 +0200225 }
226
Peter Zijlstra9dbdb152013-11-18 18:27:06 +0100227 /* hint to use a 32x32->64 mul */
228 fact = (u64)(u32)fact * lw->inv_weight;
Peter Zijlstra029632f2011-10-25 10:00:11 +0200229
Peter Zijlstra9dbdb152013-11-18 18:27:06 +0100230 while (fact >> 32) {
231 fact >>= 1;
232 shift--;
233 }
234
235 return mul_u64_u32_shr(delta_exec, fact, shift);
Peter Zijlstra029632f2011-10-25 10:00:11 +0200236}
237
238
239const struct sched_class fair_sched_class;
Peter Zijlstraa4c2f002008-10-17 19:27:03 +0200240
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200241/**************************************************************
242 * CFS operations on generic schedulable entities:
243 */
244
245#ifdef CONFIG_FAIR_GROUP_SCHED
246
247/* cpu runqueue to which this cfs_rq is attached */
248static inline struct rq *rq_of(struct cfs_rq *cfs_rq)
249{
250 return cfs_rq->rq;
251}
252
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200253/* An entity is a task if it doesn't "own" a runqueue */
254#define entity_is_task(se) (!se->my_q)
255
Peter Zijlstra8f488942009-07-24 12:25:30 +0200256static inline struct task_struct *task_of(struct sched_entity *se)
257{
Peter Zijlstra9148a3a2016-09-20 22:34:51 +0200258 SCHED_WARN_ON(!entity_is_task(se));
Peter Zijlstra8f488942009-07-24 12:25:30 +0200259 return container_of(se, struct task_struct, se);
260}
261
Peter Zijlstrab7581492008-04-19 19:45:00 +0200262/* Walk up scheduling entities hierarchy */
263#define for_each_sched_entity(se) \
264 for (; se; se = se->parent)
265
266static inline struct cfs_rq *task_cfs_rq(struct task_struct *p)
267{
268 return p->se.cfs_rq;
269}
270
271/* runqueue on which this entity is (to be) queued */
272static inline struct cfs_rq *cfs_rq_of(struct sched_entity *se)
273{
274 return se->cfs_rq;
275}
276
277/* runqueue "owned" by this group */
278static inline struct cfs_rq *group_cfs_rq(struct sched_entity *grp)
279{
280 return grp->my_q;
281}
282
Peter Zijlstra3d4b47b2010-11-15 15:47:01 -0800283static inline void list_add_leaf_cfs_rq(struct cfs_rq *cfs_rq)
284{
285 if (!cfs_rq->on_list) {
Vincent Guittot9c2791f2016-11-08 10:53:43 +0100286 struct rq *rq = rq_of(cfs_rq);
287 int cpu = cpu_of(rq);
Paul Turner67e86252010-11-15 15:47:05 -0800288 /*
289 * Ensure we either appear before our parent (if already
290 * enqueued) or force our parent to appear after us when it is
Vincent Guittot9c2791f2016-11-08 10:53:43 +0100291 * enqueued. The fact that we always enqueue bottom-up
292 * reduces this to two cases and a special case for the root
293 * cfs_rq. Furthermore, it also means that we will always reset
294 * tmp_alone_branch either when the branch is connected
295 * to a tree or when we reach the beg of the tree
Paul Turner67e86252010-11-15 15:47:05 -0800296 */
297 if (cfs_rq->tg->parent &&
Vincent Guittot9c2791f2016-11-08 10:53:43 +0100298 cfs_rq->tg->parent->cfs_rq[cpu]->on_list) {
299 /*
300 * If parent is already on the list, we add the child
301 * just before. Thanks to circular linked property of
302 * the list, this means to put the child at the tail
303 * of the list that starts by parent.
304 */
Paul Turner67e86252010-11-15 15:47:05 -0800305 list_add_tail_rcu(&cfs_rq->leaf_cfs_rq_list,
Vincent Guittot9c2791f2016-11-08 10:53:43 +0100306 &(cfs_rq->tg->parent->cfs_rq[cpu]->leaf_cfs_rq_list));
307 /*
308 * The branch is now connected to its tree so we can
309 * reset tmp_alone_branch to the beginning of the
310 * list.
311 */
312 rq->tmp_alone_branch = &rq->leaf_cfs_rq_list;
313 } else if (!cfs_rq->tg->parent) {
314 /*
315 * cfs rq without parent should be put
316 * at the tail of the list.
317 */
318 list_add_tail_rcu(&cfs_rq->leaf_cfs_rq_list,
319 &rq->leaf_cfs_rq_list);
320 /*
321 * We have reach the beg of a tree so we can reset
322 * tmp_alone_branch to the beginning of the list.
323 */
324 rq->tmp_alone_branch = &rq->leaf_cfs_rq_list;
325 } else {
326 /*
327 * The parent has not already been added so we want to
328 * make sure that it will be put after us.
329 * tmp_alone_branch points to the beg of the branch
330 * where we will add parent.
331 */
332 list_add_rcu(&cfs_rq->leaf_cfs_rq_list,
333 rq->tmp_alone_branch);
334 /*
335 * update tmp_alone_branch to points to the new beg
336 * of the branch
337 */
338 rq->tmp_alone_branch = &cfs_rq->leaf_cfs_rq_list;
Paul Turner67e86252010-11-15 15:47:05 -0800339 }
Peter Zijlstra3d4b47b2010-11-15 15:47:01 -0800340
341 cfs_rq->on_list = 1;
342 }
343}
344
345static inline void list_del_leaf_cfs_rq(struct cfs_rq *cfs_rq)
346{
347 if (cfs_rq->on_list) {
348 list_del_rcu(&cfs_rq->leaf_cfs_rq_list);
349 cfs_rq->on_list = 0;
350 }
351}
352
Peter Zijlstrab7581492008-04-19 19:45:00 +0200353/* Iterate thr' all leaf cfs_rq's on a runqueue */
354#define for_each_leaf_cfs_rq(rq, cfs_rq) \
355 list_for_each_entry_rcu(cfs_rq, &rq->leaf_cfs_rq_list, leaf_cfs_rq_list)
356
357/* Do the two (enqueued) entities belong to the same group ? */
Peter Zijlstrafed14d42012-02-11 06:05:00 +0100358static inline struct cfs_rq *
Peter Zijlstrab7581492008-04-19 19:45:00 +0200359is_same_group(struct sched_entity *se, struct sched_entity *pse)
360{
361 if (se->cfs_rq == pse->cfs_rq)
Peter Zijlstrafed14d42012-02-11 06:05:00 +0100362 return se->cfs_rq;
Peter Zijlstrab7581492008-04-19 19:45:00 +0200363
Peter Zijlstrafed14d42012-02-11 06:05:00 +0100364 return NULL;
Peter Zijlstrab7581492008-04-19 19:45:00 +0200365}
366
367static inline struct sched_entity *parent_entity(struct sched_entity *se)
368{
369 return se->parent;
370}
371
Peter Zijlstra464b7522008-10-24 11:06:15 +0200372static void
373find_matching_se(struct sched_entity **se, struct sched_entity **pse)
374{
375 int se_depth, pse_depth;
376
377 /*
378 * preemption test can be made between sibling entities who are in the
379 * same cfs_rq i.e who have a common parent. Walk up the hierarchy of
380 * both tasks until we find their ancestors who are siblings of common
381 * parent.
382 */
383
384 /* First walk up until both entities are at same depth */
Peter Zijlstrafed14d42012-02-11 06:05:00 +0100385 se_depth = (*se)->depth;
386 pse_depth = (*pse)->depth;
Peter Zijlstra464b7522008-10-24 11:06:15 +0200387
388 while (se_depth > pse_depth) {
389 se_depth--;
390 *se = parent_entity(*se);
391 }
392
393 while (pse_depth > se_depth) {
394 pse_depth--;
395 *pse = parent_entity(*pse);
396 }
397
398 while (!is_same_group(*se, *pse)) {
399 *se = parent_entity(*se);
400 *pse = parent_entity(*pse);
401 }
402}
403
Peter Zijlstra8f488942009-07-24 12:25:30 +0200404#else /* !CONFIG_FAIR_GROUP_SCHED */
405
406static inline struct task_struct *task_of(struct sched_entity *se)
407{
408 return container_of(se, struct task_struct, se);
409}
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200410
411static inline struct rq *rq_of(struct cfs_rq *cfs_rq)
412{
413 return container_of(cfs_rq, struct rq, cfs);
414}
415
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200416#define entity_is_task(se) 1
417
Peter Zijlstrab7581492008-04-19 19:45:00 +0200418#define for_each_sched_entity(se) \
419 for (; se; se = NULL)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200420
Peter Zijlstrab7581492008-04-19 19:45:00 +0200421static inline struct cfs_rq *task_cfs_rq(struct task_struct *p)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200422{
Peter Zijlstrab7581492008-04-19 19:45:00 +0200423 return &task_rq(p)->cfs;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200424}
425
Peter Zijlstrab7581492008-04-19 19:45:00 +0200426static inline struct cfs_rq *cfs_rq_of(struct sched_entity *se)
427{
428 struct task_struct *p = task_of(se);
429 struct rq *rq = task_rq(p);
430
431 return &rq->cfs;
432}
433
434/* runqueue "owned" by this group */
435static inline struct cfs_rq *group_cfs_rq(struct sched_entity *grp)
436{
437 return NULL;
438}
439
Peter Zijlstra3d4b47b2010-11-15 15:47:01 -0800440static inline void list_add_leaf_cfs_rq(struct cfs_rq *cfs_rq)
441{
442}
443
444static inline void list_del_leaf_cfs_rq(struct cfs_rq *cfs_rq)
445{
446}
447
Peter Zijlstrab7581492008-04-19 19:45:00 +0200448#define for_each_leaf_cfs_rq(rq, cfs_rq) \
449 for (cfs_rq = &rq->cfs; cfs_rq; cfs_rq = NULL)
450
Peter Zijlstrab7581492008-04-19 19:45:00 +0200451static inline struct sched_entity *parent_entity(struct sched_entity *se)
452{
453 return NULL;
454}
455
Peter Zijlstra464b7522008-10-24 11:06:15 +0200456static inline void
457find_matching_se(struct sched_entity **se, struct sched_entity **pse)
458{
459}
460
Peter Zijlstrab7581492008-04-19 19:45:00 +0200461#endif /* CONFIG_FAIR_GROUP_SCHED */
462
Peter Zijlstra6c16a6d2012-03-21 13:07:16 -0700463static __always_inline
Peter Zijlstra9dbdb152013-11-18 18:27:06 +0100464void account_cfs_rq_runtime(struct cfs_rq *cfs_rq, u64 delta_exec);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200465
466/**************************************************************
467 * Scheduling class tree data structure manipulation methods:
468 */
469
Andrei Epure1bf08232013-03-12 21:12:24 +0200470static inline u64 max_vruntime(u64 max_vruntime, u64 vruntime)
Peter Zijlstra02e04312007-10-15 17:00:07 +0200471{
Andrei Epure1bf08232013-03-12 21:12:24 +0200472 s64 delta = (s64)(vruntime - max_vruntime);
Peter Zijlstra368059a2007-10-15 17:00:11 +0200473 if (delta > 0)
Andrei Epure1bf08232013-03-12 21:12:24 +0200474 max_vruntime = vruntime;
Peter Zijlstra02e04312007-10-15 17:00:07 +0200475
Andrei Epure1bf08232013-03-12 21:12:24 +0200476 return max_vruntime;
Peter Zijlstra02e04312007-10-15 17:00:07 +0200477}
478
Ingo Molnar0702e3e2007-10-15 17:00:14 +0200479static inline u64 min_vruntime(u64 min_vruntime, u64 vruntime)
Peter Zijlstrab0ffd242007-10-15 17:00:12 +0200480{
481 s64 delta = (s64)(vruntime - min_vruntime);
482 if (delta < 0)
483 min_vruntime = vruntime;
484
485 return min_vruntime;
486}
487
Fabio Checconi54fdc582009-07-16 12:32:27 +0200488static inline int entity_before(struct sched_entity *a,
489 struct sched_entity *b)
490{
491 return (s64)(a->vruntime - b->vruntime) < 0;
492}
493
Peter Zijlstra1af5f732008-10-24 11:06:13 +0200494static void update_min_vruntime(struct cfs_rq *cfs_rq)
495{
Peter Zijlstrab60205c2016-09-20 21:58:12 +0200496 struct sched_entity *curr = cfs_rq->curr;
497
Peter Zijlstra1af5f732008-10-24 11:06:13 +0200498 u64 vruntime = cfs_rq->min_vruntime;
499
Peter Zijlstrab60205c2016-09-20 21:58:12 +0200500 if (curr) {
501 if (curr->on_rq)
502 vruntime = curr->vruntime;
503 else
504 curr = NULL;
505 }
Peter Zijlstra1af5f732008-10-24 11:06:13 +0200506
507 if (cfs_rq->rb_leftmost) {
508 struct sched_entity *se = rb_entry(cfs_rq->rb_leftmost,
509 struct sched_entity,
510 run_node);
511
Peter Zijlstrab60205c2016-09-20 21:58:12 +0200512 if (!curr)
Peter Zijlstra1af5f732008-10-24 11:06:13 +0200513 vruntime = se->vruntime;
514 else
515 vruntime = min_vruntime(vruntime, se->vruntime);
516 }
517
Andrei Epure1bf08232013-03-12 21:12:24 +0200518 /* ensure we never gain time by being placed backwards. */
Peter Zijlstra1af5f732008-10-24 11:06:13 +0200519 cfs_rq->min_vruntime = max_vruntime(cfs_rq->min_vruntime, vruntime);
Peter Zijlstra3fe16982011-04-05 17:23:48 +0200520#ifndef CONFIG_64BIT
521 smp_wmb();
522 cfs_rq->min_vruntime_copy = cfs_rq->min_vruntime;
523#endif
Peter Zijlstra1af5f732008-10-24 11:06:13 +0200524}
525
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200526/*
527 * Enqueue an entity into the rb-tree:
528 */
Ingo Molnar0702e3e2007-10-15 17:00:14 +0200529static void __enqueue_entity(struct cfs_rq *cfs_rq, struct sched_entity *se)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200530{
531 struct rb_node **link = &cfs_rq->tasks_timeline.rb_node;
532 struct rb_node *parent = NULL;
533 struct sched_entity *entry;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200534 int leftmost = 1;
535
536 /*
537 * Find the right place in the rbtree:
538 */
539 while (*link) {
540 parent = *link;
541 entry = rb_entry(parent, struct sched_entity, run_node);
542 /*
543 * We dont care about collisions. Nodes with
544 * the same key stay together.
545 */
Stephan Baerwolf2bd2d6f2011-07-20 14:46:59 +0200546 if (entity_before(se, entry)) {
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200547 link = &parent->rb_left;
548 } else {
549 link = &parent->rb_right;
550 leftmost = 0;
551 }
552 }
553
554 /*
555 * Maintain a cache of leftmost tree entries (it is frequently
556 * used):
557 */
Peter Zijlstra1af5f732008-10-24 11:06:13 +0200558 if (leftmost)
Ingo Molnar57cb4992007-10-15 17:00:11 +0200559 cfs_rq->rb_leftmost = &se->run_node;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200560
561 rb_link_node(&se->run_node, parent, link);
562 rb_insert_color(&se->run_node, &cfs_rq->tasks_timeline);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200563}
564
Ingo Molnar0702e3e2007-10-15 17:00:14 +0200565static void __dequeue_entity(struct cfs_rq *cfs_rq, struct sched_entity *se)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200566{
Peter Zijlstra3fe69742008-03-14 20:55:51 +0100567 if (cfs_rq->rb_leftmost == &se->run_node) {
568 struct rb_node *next_node;
Peter Zijlstra3fe69742008-03-14 20:55:51 +0100569
570 next_node = rb_next(&se->run_node);
571 cfs_rq->rb_leftmost = next_node;
Peter Zijlstra3fe69742008-03-14 20:55:51 +0100572 }
Ingo Molnare9acbff2007-10-15 17:00:04 +0200573
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200574 rb_erase(&se->run_node, &cfs_rq->tasks_timeline);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200575}
576
Peter Zijlstra029632f2011-10-25 10:00:11 +0200577struct sched_entity *__pick_first_entity(struct cfs_rq *cfs_rq)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200578{
Peter Zijlstraf4b67552008-11-04 21:25:07 +0100579 struct rb_node *left = cfs_rq->rb_leftmost;
580
581 if (!left)
582 return NULL;
583
584 return rb_entry(left, struct sched_entity, run_node);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200585}
586
Rik van Rielac53db52011-02-01 09:51:03 -0500587static struct sched_entity *__pick_next_entity(struct sched_entity *se)
588{
589 struct rb_node *next = rb_next(&se->run_node);
590
591 if (!next)
592 return NULL;
593
594 return rb_entry(next, struct sched_entity, run_node);
595}
596
597#ifdef CONFIG_SCHED_DEBUG
Peter Zijlstra029632f2011-10-25 10:00:11 +0200598struct sched_entity *__pick_last_entity(struct cfs_rq *cfs_rq)
Peter Zijlstraaeb73b02007-10-15 17:00:05 +0200599{
Ingo Molnar7eee3e62008-02-22 10:32:21 +0100600 struct rb_node *last = rb_last(&cfs_rq->tasks_timeline);
Peter Zijlstraaeb73b02007-10-15 17:00:05 +0200601
Balbir Singh70eee742008-02-22 13:25:53 +0530602 if (!last)
603 return NULL;
Ingo Molnar7eee3e62008-02-22 10:32:21 +0100604
605 return rb_entry(last, struct sched_entity, run_node);
Peter Zijlstraaeb73b02007-10-15 17:00:05 +0200606}
607
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200608/**************************************************************
609 * Scheduling class statistics methods:
610 */
611
Christian Ehrhardtacb4a842009-11-30 12:16:48 +0100612int sched_proc_update_handler(struct ctl_table *table, int write,
Alexey Dobriyan8d65af72009-09-23 15:57:19 -0700613 void __user *buffer, size_t *lenp,
Peter Zijlstrab2be5e92007-11-09 22:39:37 +0100614 loff_t *ppos)
615{
Alexey Dobriyan8d65af72009-09-23 15:57:19 -0700616 int ret = proc_dointvec_minmax(table, write, buffer, lenp, ppos);
Nicholas Mc Guire58ac93e2015-05-15 21:05:42 +0200617 unsigned int factor = get_update_sysctl_factor();
Peter Zijlstrab2be5e92007-11-09 22:39:37 +0100618
619 if (ret || !write)
620 return ret;
621
622 sched_nr_latency = DIV_ROUND_UP(sysctl_sched_latency,
623 sysctl_sched_min_granularity);
624
Christian Ehrhardtacb4a842009-11-30 12:16:48 +0100625#define WRT_SYSCTL(name) \
626 (normalized_sysctl_##name = sysctl_##name / (factor))
627 WRT_SYSCTL(sched_min_granularity);
628 WRT_SYSCTL(sched_latency);
629 WRT_SYSCTL(sched_wakeup_granularity);
Christian Ehrhardtacb4a842009-11-30 12:16:48 +0100630#undef WRT_SYSCTL
631
Peter Zijlstrab2be5e92007-11-09 22:39:37 +0100632 return 0;
633}
634#endif
Ingo Molnar647e7ca2007-10-15 17:00:13 +0200635
636/*
Peter Zijlstraf9c0b092008-10-17 19:27:04 +0200637 * delta /= w
Peter Zijlstraa7be37a2008-06-27 13:41:11 +0200638 */
Peter Zijlstra9dbdb152013-11-18 18:27:06 +0100639static inline u64 calc_delta_fair(u64 delta, struct sched_entity *se)
Peter Zijlstraa7be37a2008-06-27 13:41:11 +0200640{
Peter Zijlstraf9c0b092008-10-17 19:27:04 +0200641 if (unlikely(se->load.weight != NICE_0_LOAD))
Peter Zijlstra9dbdb152013-11-18 18:27:06 +0100642 delta = __calc_delta(delta, NICE_0_LOAD, &se->load);
Peter Zijlstraa7be37a2008-06-27 13:41:11 +0200643
644 return delta;
645}
646
647/*
Ingo Molnar647e7ca2007-10-15 17:00:13 +0200648 * The idea is to set a period in which each task runs once.
649 *
Borislav Petkov532b1852012-08-08 16:16:04 +0200650 * When there are too many tasks (sched_nr_latency) we have to stretch
Ingo Molnar647e7ca2007-10-15 17:00:13 +0200651 * this period because otherwise the slices get too small.
652 *
653 * p = (nr <= nl) ? l : l*nr/nl
654 */
Peter Zijlstra4d78e7b2007-10-15 17:00:04 +0200655static u64 __sched_period(unsigned long nr_running)
656{
Boqun Feng8e2b0bf2015-07-02 22:25:52 +0800657 if (unlikely(nr_running > sched_nr_latency))
658 return nr_running * sysctl_sched_min_granularity;
659 else
660 return sysctl_sched_latency;
Peter Zijlstra4d78e7b2007-10-15 17:00:04 +0200661}
662
Ingo Molnar647e7ca2007-10-15 17:00:13 +0200663/*
664 * We calculate the wall-time slice from the period by taking a part
665 * proportional to the weight.
666 *
Peter Zijlstraf9c0b092008-10-17 19:27:04 +0200667 * s = p*P[w/rw]
Ingo Molnar647e7ca2007-10-15 17:00:13 +0200668 */
Peter Zijlstra6d0f0eb2007-10-15 17:00:05 +0200669static u64 sched_slice(struct cfs_rq *cfs_rq, struct sched_entity *se)
Peter Zijlstra21805082007-08-25 18:41:53 +0200670{
Mike Galbraith0a582442009-01-02 12:16:42 +0100671 u64 slice = __sched_period(cfs_rq->nr_running + !se->on_rq);
Peter Zijlstraf9c0b092008-10-17 19:27:04 +0200672
Mike Galbraith0a582442009-01-02 12:16:42 +0100673 for_each_sched_entity(se) {
Lin Ming6272d682009-01-15 17:17:15 +0100674 struct load_weight *load;
Christian Engelmayer3104bf02009-06-16 10:35:12 +0200675 struct load_weight lw;
Lin Ming6272d682009-01-15 17:17:15 +0100676
677 cfs_rq = cfs_rq_of(se);
678 load = &cfs_rq->load;
Peter Zijlstraf9c0b092008-10-17 19:27:04 +0200679
Mike Galbraith0a582442009-01-02 12:16:42 +0100680 if (unlikely(!se->on_rq)) {
Christian Engelmayer3104bf02009-06-16 10:35:12 +0200681 lw = cfs_rq->load;
Mike Galbraith0a582442009-01-02 12:16:42 +0100682
683 update_load_add(&lw, se->load.weight);
684 load = &lw;
685 }
Peter Zijlstra9dbdb152013-11-18 18:27:06 +0100686 slice = __calc_delta(slice, se->load.weight, load);
Mike Galbraith0a582442009-01-02 12:16:42 +0100687 }
688 return slice;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200689}
690
Ingo Molnar647e7ca2007-10-15 17:00:13 +0200691/*
Andrei Epure660cc002013-03-11 12:03:20 +0200692 * We calculate the vruntime slice of a to-be-inserted task.
Ingo Molnar647e7ca2007-10-15 17:00:13 +0200693 *
Peter Zijlstraf9c0b092008-10-17 19:27:04 +0200694 * vs = s/w
Ingo Molnar647e7ca2007-10-15 17:00:13 +0200695 */
Peter Zijlstraf9c0b092008-10-17 19:27:04 +0200696static u64 sched_vslice(struct cfs_rq *cfs_rq, struct sched_entity *se)
Ingo Molnar647e7ca2007-10-15 17:00:13 +0200697{
Peter Zijlstraf9c0b092008-10-17 19:27:04 +0200698 return calc_delta_fair(sched_slice(cfs_rq, se), se);
Peter Zijlstraa7be37a2008-06-27 13:41:11 +0200699}
700
Alex Shia75cdaa2013-06-20 10:18:47 +0800701#ifdef CONFIG_SMP
Morten Rasmussen772bd008c2016-06-22 18:03:13 +0100702static int select_idle_sibling(struct task_struct *p, int prev_cpu, int cpu);
Mel Gormanfb13c7e2013-10-07 11:29:17 +0100703static unsigned long task_h_load(struct task_struct *p);
704
Yuyang Du9d89c252015-07-15 08:04:37 +0800705/*
706 * We choose a half-life close to 1 scheduling period.
Leo Yan84fb5a12015-09-15 18:57:37 +0800707 * Note: The tables runnable_avg_yN_inv and runnable_avg_yN_sum are
708 * dependent on this value.
Yuyang Du9d89c252015-07-15 08:04:37 +0800709 */
710#define LOAD_AVG_PERIOD 32
711#define LOAD_AVG_MAX 47742 /* maximum possible load avg */
Leo Yan84fb5a12015-09-15 18:57:37 +0800712#define LOAD_AVG_MAX_N 345 /* number of full periods to produce LOAD_AVG_MAX */
Alex Shia75cdaa2013-06-20 10:18:47 +0800713
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
Yuyang Du9d89c252015-07-15 08:04:37 +0800719 sa->last_update_time = 0;
720 /*
721 * sched_avg's period_contrib should be strictly less then 1024, so
722 * we give it 1023 to make sure it is almost a period (1024us), and
723 * will definitely be update (after enqueue).
724 */
725 sa->period_contrib = 1023;
Vincent Guittotb5a9b342016-10-19 14:45:23 +0200726 /*
727 * Tasks are intialized with full load to be seen as heavy tasks until
728 * they get a chance to stabilize to their real load level.
729 * Group entities are intialized with zero load to reflect the fact that
730 * nothing has been attached to the task group yet.
731 */
732 if (entity_is_task(se))
733 sa->load_avg = scale_load_down(se->load.weight);
Yuyang Du9d89c252015-07-15 08:04:37 +0800734 sa->load_sum = sa->load_avg * LOAD_AVG_MAX;
Yuyang Du2b8c41d2016-03-30 04:30:56 +0800735 /*
736 * At this point, util_avg won't be used in select_task_rq_fair anyway
737 */
738 sa->util_avg = 0;
739 sa->util_sum = 0;
Yuyang Du9d89c252015-07-15 08:04:37 +0800740 /* when this task enqueue'ed, it will contribute to its cfs_rq's load_avg */
Alex Shia75cdaa2013-06-20 10:18:47 +0800741}
Yuyang Du7ea241a2015-07-15 08:04:42 +0800742
Peter Zijlstra7dc603c2016-06-16 13:29:28 +0200743static inline u64 cfs_rq_clock_task(struct cfs_rq *cfs_rq);
Vincent Guittotdf217912016-11-08 10:53:42 +0100744static void attach_entity_cfs_rq(struct sched_entity *se);
Peter Zijlstra7dc603c2016-06-16 13:29:28 +0200745
Yuyang Du2b8c41d2016-03-30 04:30:56 +0800746/*
747 * With new tasks being created, their initial util_avgs are extrapolated
748 * based on the cfs_rq's current util_avg:
749 *
750 * util_avg = cfs_rq->util_avg / (cfs_rq->load_avg + 1) * se.load.weight
751 *
752 * However, in many cases, the above util_avg does not give a desired
753 * value. Moreover, the sum of the util_avgs may be divergent, such
754 * as when the series is a harmonic series.
755 *
756 * To solve this problem, we also cap the util_avg of successive tasks to
757 * only 1/2 of the left utilization budget:
758 *
759 * util_avg_cap = (1024 - cfs_rq->avg.util_avg) / 2^n
760 *
761 * where n denotes the nth task.
762 *
763 * For example, a simplest series from the beginning would be like:
764 *
765 * task util_avg: 512, 256, 128, 64, 32, 16, 8, ...
766 * cfs_rq util_avg: 512, 768, 896, 960, 992, 1008, 1016, ...
767 *
768 * Finally, that extrapolated util_avg is clamped to the cap (util_avg_cap)
769 * if util_avg > util_avg_cap.
770 */
771void post_init_entity_util_avg(struct sched_entity *se)
772{
773 struct cfs_rq *cfs_rq = cfs_rq_of(se);
774 struct sched_avg *sa = &se->avg;
Yuyang Du172895e2016-04-05 12:12:27 +0800775 long cap = (long)(SCHED_CAPACITY_SCALE - cfs_rq->avg.util_avg) / 2;
Yuyang Du2b8c41d2016-03-30 04:30:56 +0800776
777 if (cap > 0) {
778 if (cfs_rq->avg.util_avg != 0) {
779 sa->util_avg = cfs_rq->avg.util_avg * se->load.weight;
780 sa->util_avg /= (cfs_rq->avg.load_avg + 1);
781
782 if (sa->util_avg > cap)
783 sa->util_avg = cap;
784 } else {
785 sa->util_avg = cap;
786 }
787 sa->util_sum = sa->util_avg * LOAD_AVG_MAX;
788 }
Peter Zijlstra7dc603c2016-06-16 13:29:28 +0200789
790 if (entity_is_task(se)) {
791 struct task_struct *p = task_of(se);
792 if (p->sched_class != &fair_sched_class) {
793 /*
794 * For !fair tasks do:
795 *
796 update_cfs_rq_load_avg(now, cfs_rq, false);
797 attach_entity_load_avg(cfs_rq, se);
798 switched_from_fair(rq, p);
799 *
800 * such that the next switched_to_fair() has the
801 * expected state.
802 */
Vincent Guittotdf217912016-11-08 10:53:42 +0100803 se->avg.last_update_time = cfs_rq_clock_task(cfs_rq);
Peter Zijlstra7dc603c2016-06-16 13:29:28 +0200804 return;
805 }
806 }
807
Vincent Guittotdf217912016-11-08 10:53:42 +0100808 attach_entity_cfs_rq(se);
Yuyang Du2b8c41d2016-03-30 04:30:56 +0800809}
810
Peter Zijlstra7dc603c2016-06-16 13:29:28 +0200811#else /* !CONFIG_SMP */
Yuyang Du540247f2015-07-15 08:04:39 +0800812void init_entity_runnable_average(struct sched_entity *se)
Alex Shia75cdaa2013-06-20 10:18:47 +0800813{
814}
Yuyang Du2b8c41d2016-03-30 04:30:56 +0800815void post_init_entity_util_avg(struct sched_entity *se)
816{
817}
Peter Zijlstra3d30544f2016-06-21 14:27:50 +0200818static void update_tg_load_avg(struct cfs_rq *cfs_rq, int force)
819{
820}
Peter Zijlstra7dc603c2016-06-16 13:29:28 +0200821#endif /* CONFIG_SMP */
Alex Shia75cdaa2013-06-20 10:18:47 +0800822
Peter Zijlstraa7be37a2008-06-27 13:41:11 +0200823/*
Peter Zijlstra9dbdb152013-11-18 18:27:06 +0100824 * Update the current task's runtime statistics.
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200825 */
Ingo Molnarb7cc0892007-08-09 11:16:47 +0200826static void update_curr(struct cfs_rq *cfs_rq)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200827{
Ingo Molnar429d43b2007-10-15 17:00:03 +0200828 struct sched_entity *curr = cfs_rq->curr;
Frederic Weisbecker78becc22013-04-12 01:51:02 +0200829 u64 now = rq_clock_task(rq_of(cfs_rq));
Peter Zijlstra9dbdb152013-11-18 18:27:06 +0100830 u64 delta_exec;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200831
832 if (unlikely(!curr))
833 return;
834
Peter Zijlstra9dbdb152013-11-18 18:27:06 +0100835 delta_exec = now - curr->exec_start;
836 if (unlikely((s64)delta_exec <= 0))
Peter Zijlstra34f28ec2008-12-16 08:45:31 +0100837 return;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200838
Ingo Molnar8ebc91d2007-10-15 17:00:03 +0200839 curr->exec_start = now;
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +0100840
Peter Zijlstra9dbdb152013-11-18 18:27:06 +0100841 schedstat_set(curr->statistics.exec_max,
842 max(delta_exec, curr->statistics.exec_max));
843
844 curr->sum_exec_runtime += delta_exec;
Josh Poimboeufae928822016-06-17 12:43:24 -0500845 schedstat_add(cfs_rq->exec_clock, delta_exec);
Peter Zijlstra9dbdb152013-11-18 18:27:06 +0100846
847 curr->vruntime += calc_delta_fair(delta_exec, curr);
848 update_min_vruntime(cfs_rq);
849
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +0100850 if (entity_is_task(curr)) {
851 struct task_struct *curtask = task_of(curr);
852
Ingo Molnarf977bb42009-09-13 18:15:54 +0200853 trace_sched_stat_runtime(curtask, delta_exec, curr->vruntime);
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +0100854 cpuacct_charge(curtask, delta_exec);
Frank Mayharf06febc2008-09-12 09:54:39 -0700855 account_group_exec_runtime(curtask, delta_exec);
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +0100856 }
Paul Turnerec12cb72011-07-21 09:43:30 -0700857
858 account_cfs_rq_runtime(cfs_rq, delta_exec);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200859}
860
Stanislaw Gruszka6e998912014-11-12 16:58:44 +0100861static void update_curr_fair(struct rq *rq)
862{
863 update_curr(cfs_rq_of(&rq->curr->se));
864}
865
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200866static inline void
Ingo Molnar5870db52007-08-09 11:16:47 +0200867update_stats_wait_start(struct cfs_rq *cfs_rq, struct sched_entity *se)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200868{
Josh Poimboeuf4fa8d292016-06-17 12:43:26 -0500869 u64 wait_start, prev_wait_start;
870
871 if (!schedstat_enabled())
872 return;
873
874 wait_start = rq_clock(rq_of(cfs_rq));
875 prev_wait_start = schedstat_val(se->statistics.wait_start);
Joonwoo Park3ea94de2015-11-12 19:38:54 -0800876
877 if (entity_is_task(se) && task_on_rq_migrating(task_of(se)) &&
Josh Poimboeuf4fa8d292016-06-17 12:43:26 -0500878 likely(wait_start > prev_wait_start))
879 wait_start -= prev_wait_start;
Joonwoo Park3ea94de2015-11-12 19:38:54 -0800880
Josh Poimboeuf4fa8d292016-06-17 12:43:26 -0500881 schedstat_set(se->statistics.wait_start, wait_start);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200882}
883
Josh Poimboeuf4fa8d292016-06-17 12:43:26 -0500884static inline void
Joonwoo Park3ea94de2015-11-12 19:38:54 -0800885update_stats_wait_end(struct cfs_rq *cfs_rq, struct sched_entity *se)
886{
887 struct task_struct *p;
Mel Gormancb251762016-02-05 09:08:36 +0000888 u64 delta;
889
Josh Poimboeuf4fa8d292016-06-17 12:43:26 -0500890 if (!schedstat_enabled())
891 return;
892
893 delta = rq_clock(rq_of(cfs_rq)) - schedstat_val(se->statistics.wait_start);
Joonwoo Park3ea94de2015-11-12 19:38:54 -0800894
895 if (entity_is_task(se)) {
896 p = task_of(se);
897 if (task_on_rq_migrating(p)) {
898 /*
899 * Preserve migrating task's wait time so wait_start
900 * time stamp can be adjusted to accumulate wait time
901 * prior to migration.
902 */
Josh Poimboeuf4fa8d292016-06-17 12:43:26 -0500903 schedstat_set(se->statistics.wait_start, delta);
Joonwoo Park3ea94de2015-11-12 19:38:54 -0800904 return;
905 }
906 trace_sched_stat_wait(p, delta);
907 }
908
Josh Poimboeuf4fa8d292016-06-17 12:43:26 -0500909 schedstat_set(se->statistics.wait_max,
910 max(schedstat_val(se->statistics.wait_max), delta));
911 schedstat_inc(se->statistics.wait_count);
912 schedstat_add(se->statistics.wait_sum, delta);
913 schedstat_set(se->statistics.wait_start, 0);
Joonwoo Park3ea94de2015-11-12 19:38:54 -0800914}
Joonwoo Park3ea94de2015-11-12 19:38:54 -0800915
Josh Poimboeuf4fa8d292016-06-17 12:43:26 -0500916static inline void
Josh Poimboeuf1a3d0272016-06-17 12:43:23 -0500917update_stats_enqueue_sleeper(struct cfs_rq *cfs_rq, struct sched_entity *se)
918{
919 struct task_struct *tsk = NULL;
Josh Poimboeuf4fa8d292016-06-17 12:43:26 -0500920 u64 sleep_start, block_start;
921
922 if (!schedstat_enabled())
923 return;
924
925 sleep_start = schedstat_val(se->statistics.sleep_start);
926 block_start = schedstat_val(se->statistics.block_start);
Josh Poimboeuf1a3d0272016-06-17 12:43:23 -0500927
928 if (entity_is_task(se))
929 tsk = task_of(se);
930
Josh Poimboeuf4fa8d292016-06-17 12:43:26 -0500931 if (sleep_start) {
932 u64 delta = rq_clock(rq_of(cfs_rq)) - sleep_start;
Josh Poimboeuf1a3d0272016-06-17 12:43:23 -0500933
934 if ((s64)delta < 0)
935 delta = 0;
936
Josh Poimboeuf4fa8d292016-06-17 12:43:26 -0500937 if (unlikely(delta > schedstat_val(se->statistics.sleep_max)))
938 schedstat_set(se->statistics.sleep_max, delta);
Josh Poimboeuf1a3d0272016-06-17 12:43:23 -0500939
Josh Poimboeuf4fa8d292016-06-17 12:43:26 -0500940 schedstat_set(se->statistics.sleep_start, 0);
941 schedstat_add(se->statistics.sum_sleep_runtime, delta);
Josh Poimboeuf1a3d0272016-06-17 12:43:23 -0500942
943 if (tsk) {
944 account_scheduler_latency(tsk, delta >> 10, 1);
945 trace_sched_stat_sleep(tsk, delta);
946 }
947 }
Josh Poimboeuf4fa8d292016-06-17 12:43:26 -0500948 if (block_start) {
949 u64 delta = rq_clock(rq_of(cfs_rq)) - block_start;
Josh Poimboeuf1a3d0272016-06-17 12:43:23 -0500950
951 if ((s64)delta < 0)
952 delta = 0;
953
Josh Poimboeuf4fa8d292016-06-17 12:43:26 -0500954 if (unlikely(delta > schedstat_val(se->statistics.block_max)))
955 schedstat_set(se->statistics.block_max, delta);
Josh Poimboeuf1a3d0272016-06-17 12:43:23 -0500956
Josh Poimboeuf4fa8d292016-06-17 12:43:26 -0500957 schedstat_set(se->statistics.block_start, 0);
958 schedstat_add(se->statistics.sum_sleep_runtime, delta);
Josh Poimboeuf1a3d0272016-06-17 12:43:23 -0500959
960 if (tsk) {
961 if (tsk->in_iowait) {
Josh Poimboeuf4fa8d292016-06-17 12:43:26 -0500962 schedstat_add(se->statistics.iowait_sum, delta);
963 schedstat_inc(se->statistics.iowait_count);
Josh Poimboeuf1a3d0272016-06-17 12:43:23 -0500964 trace_sched_stat_iowait(tsk, delta);
965 }
966
967 trace_sched_stat_blocked(tsk, delta);
968
969 /*
970 * Blocking time is in units of nanosecs, so shift by
971 * 20 to get a milliseconds-range estimation of the
972 * amount of time that the task spent sleeping:
973 */
974 if (unlikely(prof_on == SLEEP_PROFILING)) {
975 profile_hits(SLEEP_PROFILING,
976 (void *)get_wchan(tsk),
977 delta >> 20);
978 }
979 account_scheduler_latency(tsk, delta >> 10, 0);
980 }
981 }
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200982}
983
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200984/*
985 * Task is being enqueued - update stats:
986 */
Mel Gormancb251762016-02-05 09:08:36 +0000987static inline void
Josh Poimboeuf1a3d0272016-06-17 12:43:23 -0500988update_stats_enqueue(struct cfs_rq *cfs_rq, struct sched_entity *se, int flags)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200989{
Josh Poimboeuf4fa8d292016-06-17 12:43:26 -0500990 if (!schedstat_enabled())
991 return;
992
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200993 /*
994 * Are we enqueueing a waiting task? (for current tasks
995 * a dequeue/enqueue event is a NOP)
996 */
Ingo Molnar429d43b2007-10-15 17:00:03 +0200997 if (se != cfs_rq->curr)
Ingo Molnar5870db52007-08-09 11:16:47 +0200998 update_stats_wait_start(cfs_rq, se);
Josh Poimboeuf1a3d0272016-06-17 12:43:23 -0500999
1000 if (flags & ENQUEUE_WAKEUP)
1001 update_stats_enqueue_sleeper(cfs_rq, se);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001002}
1003
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001004static inline void
Mel Gormancb251762016-02-05 09:08:36 +00001005update_stats_dequeue(struct cfs_rq *cfs_rq, struct sched_entity *se, int flags)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001006{
Josh Poimboeuf4fa8d292016-06-17 12:43:26 -05001007
1008 if (!schedstat_enabled())
1009 return;
1010
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001011 /*
1012 * Mark the end of the wait period if dequeueing a
1013 * waiting task:
1014 */
Ingo Molnar429d43b2007-10-15 17:00:03 +02001015 if (se != cfs_rq->curr)
Ingo Molnar9ef0a962007-08-09 11:16:47 +02001016 update_stats_wait_end(cfs_rq, se);
Mel Gormancb251762016-02-05 09:08:36 +00001017
Josh Poimboeuf4fa8d292016-06-17 12:43:26 -05001018 if ((flags & DEQUEUE_SLEEP) && entity_is_task(se)) {
1019 struct task_struct *tsk = task_of(se);
Mel Gormancb251762016-02-05 09:08:36 +00001020
Josh Poimboeuf4fa8d292016-06-17 12:43:26 -05001021 if (tsk->state & TASK_INTERRUPTIBLE)
1022 schedstat_set(se->statistics.sleep_start,
1023 rq_clock(rq_of(cfs_rq)));
1024 if (tsk->state & TASK_UNINTERRUPTIBLE)
1025 schedstat_set(se->statistics.block_start,
1026 rq_clock(rq_of(cfs_rq)));
Mel Gormancb251762016-02-05 09:08:36 +00001027 }
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001028}
1029
1030/*
1031 * We are picking a new current task - update its stats:
1032 */
1033static inline void
Ingo Molnar79303e92007-08-09 11:16:47 +02001034update_stats_curr_start(struct cfs_rq *cfs_rq, struct sched_entity *se)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001035{
1036 /*
1037 * We are starting a new run period:
1038 */
Frederic Weisbecker78becc22013-04-12 01:51:02 +02001039 se->exec_start = rq_clock_task(rq_of(cfs_rq));
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001040}
1041
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001042/**************************************************
1043 * Scheduling class queueing methods:
1044 */
1045
Peter Zijlstracbee9f82012-10-25 14:16:43 +02001046#ifdef CONFIG_NUMA_BALANCING
1047/*
Mel Gorman598f0ec2013-10-07 11:28:55 +01001048 * Approximate time to scan a full NUMA task in ms. The task scan period is
1049 * calculated based on the tasks virtual memory size and
1050 * numa_balancing_scan_size.
Peter Zijlstracbee9f82012-10-25 14:16:43 +02001051 */
Mel Gorman598f0ec2013-10-07 11:28:55 +01001052unsigned int sysctl_numa_balancing_scan_period_min = 1000;
1053unsigned int sysctl_numa_balancing_scan_period_max = 60000;
Peter Zijlstra6e5fb222012-10-25 14:16:45 +02001054
1055/* Portion of address space to scan in MB */
1056unsigned int sysctl_numa_balancing_scan_size = 256;
Peter Zijlstracbee9f82012-10-25 14:16:43 +02001057
Peter Zijlstra4b96a292012-10-25 14:16:47 +02001058/* Scan @scan_size MB every @scan_period after an initial @scan_delay in ms */
1059unsigned int sysctl_numa_balancing_scan_delay = 1000;
1060
Mel Gorman598f0ec2013-10-07 11:28:55 +01001061static unsigned int task_nr_scan_windows(struct task_struct *p)
1062{
1063 unsigned long rss = 0;
1064 unsigned long nr_scan_pages;
1065
1066 /*
1067 * Calculations based on RSS as non-present and empty pages are skipped
1068 * by the PTE scanner and NUMA hinting faults should be trapped based
1069 * on resident pages
1070 */
1071 nr_scan_pages = sysctl_numa_balancing_scan_size << (20 - PAGE_SHIFT);
1072 rss = get_mm_rss(p->mm);
1073 if (!rss)
1074 rss = nr_scan_pages;
1075
1076 rss = round_up(rss, nr_scan_pages);
1077 return rss / nr_scan_pages;
1078}
1079
1080/* For sanitys sake, never scan more PTEs than MAX_SCAN_WINDOW MB/sec. */
1081#define MAX_SCAN_WINDOW 2560
1082
1083static unsigned int task_scan_min(struct task_struct *p)
1084{
Jason Low316c1608d2015-04-28 13:00:20 -07001085 unsigned int scan_size = READ_ONCE(sysctl_numa_balancing_scan_size);
Mel Gorman598f0ec2013-10-07 11:28:55 +01001086 unsigned int scan, floor;
1087 unsigned int windows = 1;
1088
Kirill Tkhai64192652014-10-16 14:39:37 +04001089 if (scan_size < MAX_SCAN_WINDOW)
1090 windows = MAX_SCAN_WINDOW / scan_size;
Mel Gorman598f0ec2013-10-07 11:28:55 +01001091 floor = 1000 / windows;
1092
1093 scan = sysctl_numa_balancing_scan_period_min / task_nr_scan_windows(p);
1094 return max_t(unsigned int, floor, scan);
1095}
1096
1097static unsigned int task_scan_max(struct task_struct *p)
1098{
1099 unsigned int smin = task_scan_min(p);
1100 unsigned int smax;
1101
1102 /* Watch for min being lower than max due to floor calculations */
1103 smax = sysctl_numa_balancing_scan_period_max / task_nr_scan_windows(p);
1104 return max(smin, smax);
1105}
1106
Peter Zijlstra0ec8aa02013-10-07 11:29:33 +01001107static void account_numa_enqueue(struct rq *rq, struct task_struct *p)
1108{
1109 rq->nr_numa_running += (p->numa_preferred_nid != -1);
1110 rq->nr_preferred_running += (p->numa_preferred_nid == task_node(p));
1111}
1112
1113static void account_numa_dequeue(struct rq *rq, struct task_struct *p)
1114{
1115 rq->nr_numa_running -= (p->numa_preferred_nid != -1);
1116 rq->nr_preferred_running -= (p->numa_preferred_nid == task_node(p));
1117}
1118
Peter Zijlstra8c8a7432013-10-07 11:29:21 +01001119struct numa_group {
1120 atomic_t refcount;
1121
1122 spinlock_t lock; /* nr_tasks, tasks */
1123 int nr_tasks;
Mel Gormane29cf082013-10-07 11:29:22 +01001124 pid_t gid;
Rik van Riel4142c3e2016-01-25 17:07:39 -05001125 int active_nodes;
Peter Zijlstra8c8a7432013-10-07 11:29:21 +01001126
1127 struct rcu_head rcu;
Mel Gorman989348b2013-10-07 11:29:40 +01001128 unsigned long total_faults;
Rik van Riel4142c3e2016-01-25 17:07:39 -05001129 unsigned long max_faults_cpu;
Rik van Riel7e2703e2014-01-27 17:03:45 -05001130 /*
1131 * Faults_cpu is used to decide whether memory should move
1132 * towards the CPU. As a consequence, these stats are weighted
1133 * more by CPU use than by memory faults.
1134 */
Rik van Riel50ec8a42014-01-27 17:03:42 -05001135 unsigned long *faults_cpu;
Mel Gorman989348b2013-10-07 11:29:40 +01001136 unsigned long faults[0];
Peter Zijlstra8c8a7432013-10-07 11:29:21 +01001137};
1138
Rik van Rielbe1e4e72014-01-27 17:03:48 -05001139/* Shared or private faults. */
1140#define NR_NUMA_HINT_FAULT_TYPES 2
1141
1142/* Memory and CPU locality */
1143#define NR_NUMA_HINT_FAULT_STATS (NR_NUMA_HINT_FAULT_TYPES * 2)
1144
1145/* Averaged statistics, and temporary buffers. */
1146#define NR_NUMA_HINT_FAULT_BUCKETS (NR_NUMA_HINT_FAULT_STATS * 2)
1147
Mel Gormane29cf082013-10-07 11:29:22 +01001148pid_t task_numa_group_id(struct task_struct *p)
1149{
1150 return p->numa_group ? p->numa_group->gid : 0;
1151}
1152
Iulia Manda44dba3d2014-10-31 02:13:31 +02001153/*
1154 * The averaged statistics, shared & private, memory & cpu,
1155 * occupy the first half of the array. The second half of the
1156 * array is for current counters, which are averaged into the
1157 * first set by task_numa_placement.
1158 */
1159static inline int task_faults_idx(enum numa_faults_stats s, int nid, int priv)
Mel Gormanac8e8952013-10-07 11:29:03 +01001160{
Iulia Manda44dba3d2014-10-31 02:13:31 +02001161 return NR_NUMA_HINT_FAULT_TYPES * (s * nr_node_ids + nid) + priv;
Mel Gormanac8e8952013-10-07 11:29:03 +01001162}
1163
1164static inline unsigned long task_faults(struct task_struct *p, int nid)
1165{
Iulia Manda44dba3d2014-10-31 02:13:31 +02001166 if (!p->numa_faults)
Mel Gormanac8e8952013-10-07 11:29:03 +01001167 return 0;
1168
Iulia Manda44dba3d2014-10-31 02:13:31 +02001169 return p->numa_faults[task_faults_idx(NUMA_MEM, nid, 0)] +
1170 p->numa_faults[task_faults_idx(NUMA_MEM, nid, 1)];
Mel Gormanac8e8952013-10-07 11:29:03 +01001171}
1172
Mel Gorman83e1d2c2013-10-07 11:29:27 +01001173static inline unsigned long group_faults(struct task_struct *p, int nid)
1174{
1175 if (!p->numa_group)
1176 return 0;
1177
Iulia Manda44dba3d2014-10-31 02:13:31 +02001178 return p->numa_group->faults[task_faults_idx(NUMA_MEM, nid, 0)] +
1179 p->numa_group->faults[task_faults_idx(NUMA_MEM, nid, 1)];
Mel Gorman83e1d2c2013-10-07 11:29:27 +01001180}
1181
Rik van Riel20e07de2014-01-27 17:03:43 -05001182static inline unsigned long group_faults_cpu(struct numa_group *group, int nid)
1183{
Iulia Manda44dba3d2014-10-31 02:13:31 +02001184 return group->faults_cpu[task_faults_idx(NUMA_MEM, nid, 0)] +
1185 group->faults_cpu[task_faults_idx(NUMA_MEM, nid, 1)];
Rik van Riel20e07de2014-01-27 17:03:43 -05001186}
1187
Rik van Riel4142c3e2016-01-25 17:07:39 -05001188/*
1189 * A node triggering more than 1/3 as many NUMA faults as the maximum is
1190 * considered part of a numa group's pseudo-interleaving set. Migrations
1191 * between these nodes are slowed down, to allow things to settle down.
1192 */
1193#define ACTIVE_NODE_FRACTION 3
1194
1195static bool numa_is_active_node(int nid, struct numa_group *ng)
1196{
1197 return group_faults_cpu(ng, nid) * ACTIVE_NODE_FRACTION > ng->max_faults_cpu;
1198}
1199
Rik van Riel6c6b1192014-10-17 03:29:52 -04001200/* Handle placement on systems where not all nodes are directly connected. */
1201static unsigned long score_nearby_nodes(struct task_struct *p, int nid,
1202 int maxdist, bool task)
1203{
1204 unsigned long score = 0;
1205 int node;
1206
1207 /*
1208 * All nodes are directly connected, and the same distance
1209 * from each other. No need for fancy placement algorithms.
1210 */
1211 if (sched_numa_topology_type == NUMA_DIRECT)
1212 return 0;
1213
1214 /*
1215 * This code is called for each node, introducing N^2 complexity,
1216 * which should be ok given the number of nodes rarely exceeds 8.
1217 */
1218 for_each_online_node(node) {
1219 unsigned long faults;
1220 int dist = node_distance(nid, node);
1221
1222 /*
1223 * The furthest away nodes in the system are not interesting
1224 * for placement; nid was already counted.
1225 */
1226 if (dist == sched_max_numa_distance || node == nid)
1227 continue;
1228
1229 /*
1230 * On systems with a backplane NUMA topology, compare groups
1231 * of nodes, and move tasks towards the group with the most
1232 * memory accesses. When comparing two nodes at distance
1233 * "hoplimit", only nodes closer by than "hoplimit" are part
1234 * of each group. Skip other nodes.
1235 */
1236 if (sched_numa_topology_type == NUMA_BACKPLANE &&
1237 dist > maxdist)
1238 continue;
1239
1240 /* Add up the faults from nearby nodes. */
1241 if (task)
1242 faults = task_faults(p, node);
1243 else
1244 faults = group_faults(p, node);
1245
1246 /*
1247 * On systems with a glueless mesh NUMA topology, there are
1248 * no fixed "groups of nodes". Instead, nodes that are not
1249 * directly connected bounce traffic through intermediate
1250 * nodes; a numa_group can occupy any set of nodes.
1251 * The further away a node is, the less the faults count.
1252 * This seems to result in good task placement.
1253 */
1254 if (sched_numa_topology_type == NUMA_GLUELESS_MESH) {
1255 faults *= (sched_max_numa_distance - dist);
1256 faults /= (sched_max_numa_distance - LOCAL_DISTANCE);
1257 }
1258
1259 score += faults;
1260 }
1261
1262 return score;
1263}
1264
Mel Gorman83e1d2c2013-10-07 11:29:27 +01001265/*
1266 * These return the fraction of accesses done by a particular task, or
1267 * task group, on a particular numa node. The group weight is given a
1268 * larger multiplier, in order to group tasks together that are almost
1269 * evenly spread out between numa nodes.
1270 */
Rik van Riel7bd95322014-10-17 03:29:51 -04001271static inline unsigned long task_weight(struct task_struct *p, int nid,
1272 int dist)
Mel Gorman83e1d2c2013-10-07 11:29:27 +01001273{
Rik van Riel7bd95322014-10-17 03:29:51 -04001274 unsigned long faults, total_faults;
Mel Gorman83e1d2c2013-10-07 11:29:27 +01001275
Iulia Manda44dba3d2014-10-31 02:13:31 +02001276 if (!p->numa_faults)
Mel Gorman83e1d2c2013-10-07 11:29:27 +01001277 return 0;
1278
1279 total_faults = p->total_numa_faults;
1280
1281 if (!total_faults)
1282 return 0;
1283
Rik van Riel7bd95322014-10-17 03:29:51 -04001284 faults = task_faults(p, nid);
Rik van Riel6c6b1192014-10-17 03:29:52 -04001285 faults += score_nearby_nodes(p, nid, dist, true);
1286
Rik van Riel7bd95322014-10-17 03:29:51 -04001287 return 1000 * faults / total_faults;
Mel Gorman83e1d2c2013-10-07 11:29:27 +01001288}
1289
Rik van Riel7bd95322014-10-17 03:29:51 -04001290static inline unsigned long group_weight(struct task_struct *p, int nid,
1291 int dist)
Mel Gorman83e1d2c2013-10-07 11:29:27 +01001292{
Rik van Riel7bd95322014-10-17 03:29:51 -04001293 unsigned long faults, total_faults;
1294
1295 if (!p->numa_group)
Mel Gorman83e1d2c2013-10-07 11:29:27 +01001296 return 0;
1297
Rik van Riel7bd95322014-10-17 03:29:51 -04001298 total_faults = p->numa_group->total_faults;
1299
1300 if (!total_faults)
1301 return 0;
1302
1303 faults = group_faults(p, nid);
Rik van Riel6c6b1192014-10-17 03:29:52 -04001304 faults += score_nearby_nodes(p, nid, dist, false);
1305
Rik van Riel7bd95322014-10-17 03:29:51 -04001306 return 1000 * faults / total_faults;
Mel Gorman83e1d2c2013-10-07 11:29:27 +01001307}
1308
Rik van Riel10f39042014-01-27 17:03:44 -05001309bool should_numa_migrate_memory(struct task_struct *p, struct page * page,
1310 int src_nid, int dst_cpu)
1311{
1312 struct numa_group *ng = p->numa_group;
1313 int dst_nid = cpu_to_node(dst_cpu);
1314 int last_cpupid, this_cpupid;
1315
1316 this_cpupid = cpu_pid_to_cpupid(dst_cpu, current->pid);
1317
1318 /*
1319 * Multi-stage node selection is used in conjunction with a periodic
1320 * migration fault to build a temporal task<->page relation. By using
1321 * a two-stage filter we remove short/unlikely relations.
1322 *
1323 * Using P(p) ~ n_p / n_t as per frequentist probability, we can equate
1324 * a task's usage of a particular page (n_p) per total usage of this
1325 * page (n_t) (in a given time-span) to a probability.
1326 *
1327 * Our periodic faults will sample this probability and getting the
1328 * same result twice in a row, given these samples are fully
1329 * independent, is then given by P(n)^2, provided our sample period
1330 * is sufficiently short compared to the usage pattern.
1331 *
1332 * This quadric squishes small probabilities, making it less likely we
1333 * act on an unlikely task<->page relation.
1334 */
1335 last_cpupid = page_cpupid_xchg_last(page, this_cpupid);
1336 if (!cpupid_pid_unset(last_cpupid) &&
1337 cpupid_to_nid(last_cpupid) != dst_nid)
1338 return false;
1339
1340 /* Always allow migrate on private faults */
1341 if (cpupid_match_pid(p, last_cpupid))
1342 return true;
1343
1344 /* A shared fault, but p->numa_group has not been set up yet. */
1345 if (!ng)
1346 return true;
1347
1348 /*
Rik van Riel4142c3e2016-01-25 17:07:39 -05001349 * Destination node is much more heavily used than the source
1350 * node? Allow migration.
Rik van Riel10f39042014-01-27 17:03:44 -05001351 */
Rik van Riel4142c3e2016-01-25 17:07:39 -05001352 if (group_faults_cpu(ng, dst_nid) > group_faults_cpu(ng, src_nid) *
1353 ACTIVE_NODE_FRACTION)
Rik van Riel10f39042014-01-27 17:03:44 -05001354 return true;
1355
1356 /*
Rik van Riel4142c3e2016-01-25 17:07:39 -05001357 * Distribute memory according to CPU & memory use on each node,
1358 * with 3/4 hysteresis to avoid unnecessary memory migrations:
1359 *
1360 * faults_cpu(dst) 3 faults_cpu(src)
1361 * --------------- * - > ---------------
1362 * faults_mem(dst) 4 faults_mem(src)
Rik van Riel10f39042014-01-27 17:03:44 -05001363 */
Rik van Riel4142c3e2016-01-25 17:07:39 -05001364 return group_faults_cpu(ng, dst_nid) * group_faults(p, src_nid) * 3 >
1365 group_faults_cpu(ng, src_nid) * group_faults(p, dst_nid) * 4;
Rik van Riel10f39042014-01-27 17:03:44 -05001366}
1367
Mel Gormane6628d52013-10-07 11:29:02 +01001368static unsigned long weighted_cpuload(const int cpu);
Mel Gorman58d081b2013-10-07 11:29:10 +01001369static unsigned long source_load(int cpu, int type);
1370static unsigned long target_load(int cpu, int type);
Nicolas Pitreced549f2014-05-26 18:19:38 -04001371static unsigned long capacity_of(int cpu);
Mel Gorman58d081b2013-10-07 11:29:10 +01001372static long effective_load(struct task_group *tg, int cpu, long wl, long wg);
Mel Gormane6628d52013-10-07 11:29:02 +01001373
Mel Gormanfb13c7e2013-10-07 11:29:17 +01001374/* Cached statistics for all CPUs within a node */
Mel Gorman58d081b2013-10-07 11:29:10 +01001375struct numa_stats {
Mel Gormanfb13c7e2013-10-07 11:29:17 +01001376 unsigned long nr_running;
Mel Gorman58d081b2013-10-07 11:29:10 +01001377 unsigned long load;
Mel Gormanfb13c7e2013-10-07 11:29:17 +01001378
1379 /* Total compute capacity of CPUs on a node */
Nicolas Pitre5ef20ca2014-05-26 18:19:34 -04001380 unsigned long compute_capacity;
Mel Gormanfb13c7e2013-10-07 11:29:17 +01001381
1382 /* Approximate capacity in terms of runnable tasks on a node */
Nicolas Pitre5ef20ca2014-05-26 18:19:34 -04001383 unsigned long task_capacity;
Nicolas Pitre1b6a7492014-05-26 18:19:35 -04001384 int has_free_capacity;
Mel Gorman58d081b2013-10-07 11:29:10 +01001385};
Mel Gormane6628d52013-10-07 11:29:02 +01001386
Mel Gormanfb13c7e2013-10-07 11:29:17 +01001387/*
1388 * XXX borrowed from update_sg_lb_stats
1389 */
1390static void update_numa_stats(struct numa_stats *ns, int nid)
1391{
Rik van Riel83d7f242014-08-04 13:23:28 -04001392 int smt, cpu, cpus = 0;
1393 unsigned long capacity;
Mel Gormanfb13c7e2013-10-07 11:29:17 +01001394
1395 memset(ns, 0, sizeof(*ns));
1396 for_each_cpu(cpu, cpumask_of_node(nid)) {
1397 struct rq *rq = cpu_rq(cpu);
1398
1399 ns->nr_running += rq->nr_running;
1400 ns->load += weighted_cpuload(cpu);
Nicolas Pitreced549f2014-05-26 18:19:38 -04001401 ns->compute_capacity += capacity_of(cpu);
Peter Zijlstra5eca82a2013-11-06 18:47:57 +01001402
1403 cpus++;
Mel Gormanfb13c7e2013-10-07 11:29:17 +01001404 }
1405
Peter Zijlstra5eca82a2013-11-06 18:47:57 +01001406 /*
1407 * If we raced with hotplug and there are no CPUs left in our mask
1408 * the @ns structure is NULL'ed and task_numa_compare() will
1409 * not find this node attractive.
1410 *
Nicolas Pitre1b6a7492014-05-26 18:19:35 -04001411 * We'll either bail at !has_free_capacity, or we'll detect a huge
1412 * imbalance and bail there.
Peter Zijlstra5eca82a2013-11-06 18:47:57 +01001413 */
1414 if (!cpus)
1415 return;
1416
Rik van Riel83d7f242014-08-04 13:23:28 -04001417 /* smt := ceil(cpus / capacity), assumes: 1 < smt_power < 2 */
1418 smt = DIV_ROUND_UP(SCHED_CAPACITY_SCALE * cpus, ns->compute_capacity);
1419 capacity = cpus / smt; /* cores */
1420
1421 ns->task_capacity = min_t(unsigned, capacity,
1422 DIV_ROUND_CLOSEST(ns->compute_capacity, SCHED_CAPACITY_SCALE));
Nicolas Pitre1b6a7492014-05-26 18:19:35 -04001423 ns->has_free_capacity = (ns->nr_running < ns->task_capacity);
Mel Gormanfb13c7e2013-10-07 11:29:17 +01001424}
1425
Mel Gorman58d081b2013-10-07 11:29:10 +01001426struct task_numa_env {
1427 struct task_struct *p;
1428
1429 int src_cpu, src_nid;
1430 int dst_cpu, dst_nid;
1431
1432 struct numa_stats src_stats, dst_stats;
1433
Wanpeng Li40ea2b42013-12-05 19:10:17 +08001434 int imbalance_pct;
Rik van Riel7bd95322014-10-17 03:29:51 -04001435 int dist;
Mel Gormanfb13c7e2013-10-07 11:29:17 +01001436
1437 struct task_struct *best_task;
1438 long best_imp;
Mel Gorman58d081b2013-10-07 11:29:10 +01001439 int best_cpu;
1440};
1441
Mel Gormanfb13c7e2013-10-07 11:29:17 +01001442static void task_numa_assign(struct task_numa_env *env,
1443 struct task_struct *p, long imp)
1444{
1445 if (env->best_task)
1446 put_task_struct(env->best_task);
Oleg Nesterovbac78572016-05-18 21:57:33 +02001447 if (p)
1448 get_task_struct(p);
Mel Gormanfb13c7e2013-10-07 11:29:17 +01001449
1450 env->best_task = p;
1451 env->best_imp = imp;
1452 env->best_cpu = env->dst_cpu;
1453}
1454
Rik van Riel28a21742014-06-23 11:46:13 -04001455static bool load_too_imbalanced(long src_load, long dst_load,
Rik van Riele63da032014-05-14 13:22:21 -04001456 struct task_numa_env *env)
1457{
Rik van Riele4991b22015-05-27 15:04:27 -04001458 long imb, old_imb;
1459 long orig_src_load, orig_dst_load;
Rik van Riel28a21742014-06-23 11:46:13 -04001460 long src_capacity, dst_capacity;
1461
1462 /*
1463 * The load is corrected for the CPU capacity available on each node.
1464 *
1465 * src_load dst_load
1466 * ------------ vs ---------
1467 * src_capacity dst_capacity
1468 */
1469 src_capacity = env->src_stats.compute_capacity;
1470 dst_capacity = env->dst_stats.compute_capacity;
Rik van Riele63da032014-05-14 13:22:21 -04001471
1472 /* We care about the slope of the imbalance, not the direction. */
Rik van Riele4991b22015-05-27 15:04:27 -04001473 if (dst_load < src_load)
1474 swap(dst_load, src_load);
Rik van Riele63da032014-05-14 13:22:21 -04001475
1476 /* Is the difference below the threshold? */
Rik van Riele4991b22015-05-27 15:04:27 -04001477 imb = dst_load * src_capacity * 100 -
1478 src_load * dst_capacity * env->imbalance_pct;
Rik van Riele63da032014-05-14 13:22:21 -04001479 if (imb <= 0)
1480 return false;
1481
1482 /*
1483 * The imbalance is above the allowed threshold.
Rik van Riele4991b22015-05-27 15:04:27 -04001484 * Compare it with the old imbalance.
Rik van Riele63da032014-05-14 13:22:21 -04001485 */
Rik van Riel28a21742014-06-23 11:46:13 -04001486 orig_src_load = env->src_stats.load;
Rik van Riele4991b22015-05-27 15:04:27 -04001487 orig_dst_load = env->dst_stats.load;
Rik van Riel28a21742014-06-23 11:46:13 -04001488
Rik van Riele4991b22015-05-27 15:04:27 -04001489 if (orig_dst_load < orig_src_load)
1490 swap(orig_dst_load, orig_src_load);
Rik van Riele63da032014-05-14 13:22:21 -04001491
Rik van Riele4991b22015-05-27 15:04:27 -04001492 old_imb = orig_dst_load * src_capacity * 100 -
1493 orig_src_load * dst_capacity * env->imbalance_pct;
1494
1495 /* Would this change make things worse? */
1496 return (imb > old_imb);
Rik van Riele63da032014-05-14 13:22:21 -04001497}
1498
Mel Gormanfb13c7e2013-10-07 11:29:17 +01001499/*
1500 * This checks if the overall compute and NUMA accesses of the system would
1501 * be improved if the source tasks was migrated to the target dst_cpu taking
1502 * into account that it might be best if task running on the dst_cpu should
1503 * be exchanged with the source task
1504 */
Rik van Riel887c2902013-10-07 11:29:31 +01001505static void task_numa_compare(struct task_numa_env *env,
1506 long taskimp, long groupimp)
Mel Gormanfb13c7e2013-10-07 11:29:17 +01001507{
1508 struct rq *src_rq = cpu_rq(env->src_cpu);
1509 struct rq *dst_rq = cpu_rq(env->dst_cpu);
1510 struct task_struct *cur;
Rik van Riel28a21742014-06-23 11:46:13 -04001511 long src_load, dst_load;
Mel Gormanfb13c7e2013-10-07 11:29:17 +01001512 long load;
Rik van Riel1c5d3eb2014-06-23 11:46:15 -04001513 long imp = env->p->numa_group ? groupimp : taskimp;
Rik van Riel0132c3e2014-06-23 11:46:16 -04001514 long moveimp = imp;
Rik van Riel7bd95322014-10-17 03:29:51 -04001515 int dist = env->dist;
Mel Gormanfb13c7e2013-10-07 11:29:17 +01001516
1517 rcu_read_lock();
Oleg Nesterovbac78572016-05-18 21:57:33 +02001518 cur = task_rcu_dereference(&dst_rq->curr);
1519 if (cur && ((cur->flags & PF_EXITING) || is_idle_task(cur)))
Mel Gormanfb13c7e2013-10-07 11:29:17 +01001520 cur = NULL;
1521
1522 /*
Peter Zijlstra7af68332014-11-10 10:54:35 +01001523 * Because we have preemption enabled we can get migrated around and
1524 * end try selecting ourselves (current == env->p) as a swap candidate.
1525 */
1526 if (cur == env->p)
1527 goto unlock;
1528
1529 /*
Mel Gormanfb13c7e2013-10-07 11:29:17 +01001530 * "imp" is the fault differential for the source task between the
1531 * source and destination node. Calculate the total differential for
1532 * the source task and potential destination task. The more negative
1533 * the value is, the more rmeote accesses that would be expected to
1534 * be incurred if the tasks were swapped.
1535 */
1536 if (cur) {
1537 /* Skip this swap candidate if cannot move to the source cpu */
1538 if (!cpumask_test_cpu(env->src_cpu, tsk_cpus_allowed(cur)))
1539 goto unlock;
1540
Rik van Riel887c2902013-10-07 11:29:31 +01001541 /*
1542 * If dst and source tasks are in the same NUMA group, or not
Rik van Rielca28aa532013-10-07 11:29:32 +01001543 * in any group then look only at task weights.
Rik van Riel887c2902013-10-07 11:29:31 +01001544 */
Rik van Rielca28aa532013-10-07 11:29:32 +01001545 if (cur->numa_group == env->p->numa_group) {
Rik van Riel7bd95322014-10-17 03:29:51 -04001546 imp = taskimp + task_weight(cur, env->src_nid, dist) -
1547 task_weight(cur, env->dst_nid, dist);
Rik van Rielca28aa532013-10-07 11:29:32 +01001548 /*
1549 * Add some hysteresis to prevent swapping the
1550 * tasks within a group over tiny differences.
1551 */
1552 if (cur->numa_group)
1553 imp -= imp/16;
Rik van Riel887c2902013-10-07 11:29:31 +01001554 } else {
Rik van Rielca28aa532013-10-07 11:29:32 +01001555 /*
1556 * Compare the group weights. If a task is all by
1557 * itself (not part of a group), use the task weight
1558 * instead.
1559 */
Rik van Rielca28aa532013-10-07 11:29:32 +01001560 if (cur->numa_group)
Rik van Riel7bd95322014-10-17 03:29:51 -04001561 imp += group_weight(cur, env->src_nid, dist) -
1562 group_weight(cur, env->dst_nid, dist);
Rik van Rielca28aa532013-10-07 11:29:32 +01001563 else
Rik van Riel7bd95322014-10-17 03:29:51 -04001564 imp += task_weight(cur, env->src_nid, dist) -
1565 task_weight(cur, env->dst_nid, dist);
Rik van Riel887c2902013-10-07 11:29:31 +01001566 }
Mel Gormanfb13c7e2013-10-07 11:29:17 +01001567 }
1568
Rik van Riel0132c3e2014-06-23 11:46:16 -04001569 if (imp <= env->best_imp && moveimp <= env->best_imp)
Mel Gormanfb13c7e2013-10-07 11:29:17 +01001570 goto unlock;
1571
1572 if (!cur) {
1573 /* Is there capacity at our destination? */
Rik van Rielb932c032014-08-04 13:23:27 -04001574 if (env->src_stats.nr_running <= env->src_stats.task_capacity &&
Nicolas Pitre1b6a7492014-05-26 18:19:35 -04001575 !env->dst_stats.has_free_capacity)
Mel Gormanfb13c7e2013-10-07 11:29:17 +01001576 goto unlock;
1577
1578 goto balance;
1579 }
1580
1581 /* Balance doesn't matter much if we're running a task per cpu */
Rik van Riel0132c3e2014-06-23 11:46:16 -04001582 if (imp > env->best_imp && src_rq->nr_running == 1 &&
1583 dst_rq->nr_running == 1)
Mel Gormanfb13c7e2013-10-07 11:29:17 +01001584 goto assign;
1585
1586 /*
1587 * In the overloaded case, try and keep the load balanced.
1588 */
1589balance:
Peter Zijlstrae720fff2014-07-11 16:01:53 +02001590 load = task_h_load(env->p);
1591 dst_load = env->dst_stats.load + load;
1592 src_load = env->src_stats.load - load;
Mel Gormanfb13c7e2013-10-07 11:29:17 +01001593
Rik van Riel0132c3e2014-06-23 11:46:16 -04001594 if (moveimp > imp && moveimp > env->best_imp) {
1595 /*
1596 * If the improvement from just moving env->p direction is
1597 * better than swapping tasks around, check if a move is
1598 * possible. Store a slightly smaller score than moveimp,
1599 * so an actually idle CPU will win.
1600 */
1601 if (!load_too_imbalanced(src_load, dst_load, env)) {
1602 imp = moveimp - 1;
1603 cur = NULL;
1604 goto assign;
1605 }
1606 }
1607
1608 if (imp <= env->best_imp)
1609 goto unlock;
1610
Mel Gormanfb13c7e2013-10-07 11:29:17 +01001611 if (cur) {
Peter Zijlstrae720fff2014-07-11 16:01:53 +02001612 load = task_h_load(cur);
1613 dst_load -= load;
1614 src_load += load;
Mel Gormanfb13c7e2013-10-07 11:29:17 +01001615 }
1616
Rik van Riel28a21742014-06-23 11:46:13 -04001617 if (load_too_imbalanced(src_load, dst_load, env))
Mel Gormanfb13c7e2013-10-07 11:29:17 +01001618 goto unlock;
1619
Rik van Rielba7e5a22014-09-04 16:35:30 -04001620 /*
1621 * One idle CPU per node is evaluated for a task numa move.
1622 * Call select_idle_sibling to maybe find a better one.
1623 */
Peter Zijlstra10e2f1a2016-05-09 10:38:05 +02001624 if (!cur) {
1625 /*
1626 * select_idle_siblings() uses an per-cpu cpumask that
1627 * can be used from IRQ context.
1628 */
1629 local_irq_disable();
Morten Rasmussen772bd008c2016-06-22 18:03:13 +01001630 env->dst_cpu = select_idle_sibling(env->p, env->src_cpu,
1631 env->dst_cpu);
Peter Zijlstra10e2f1a2016-05-09 10:38:05 +02001632 local_irq_enable();
1633 }
Rik van Rielba7e5a22014-09-04 16:35:30 -04001634
Mel Gormanfb13c7e2013-10-07 11:29:17 +01001635assign:
1636 task_numa_assign(env, cur, imp);
1637unlock:
1638 rcu_read_unlock();
1639}
1640
Rik van Riel887c2902013-10-07 11:29:31 +01001641static void task_numa_find_cpu(struct task_numa_env *env,
1642 long taskimp, long groupimp)
Mel Gorman2c8a50a2013-10-07 11:29:18 +01001643{
1644 int cpu;
1645
1646 for_each_cpu(cpu, cpumask_of_node(env->dst_nid)) {
1647 /* Skip this CPU if the source task cannot migrate */
1648 if (!cpumask_test_cpu(cpu, tsk_cpus_allowed(env->p)))
1649 continue;
1650
1651 env->dst_cpu = cpu;
Rik van Riel887c2902013-10-07 11:29:31 +01001652 task_numa_compare(env, taskimp, groupimp);
Mel Gorman2c8a50a2013-10-07 11:29:18 +01001653 }
1654}
1655
Rik van Riel6f9aad02015-05-28 09:52:49 -04001656/* Only move tasks to a NUMA node less busy than the current node. */
1657static bool numa_has_capacity(struct task_numa_env *env)
1658{
1659 struct numa_stats *src = &env->src_stats;
1660 struct numa_stats *dst = &env->dst_stats;
1661
1662 if (src->has_free_capacity && !dst->has_free_capacity)
1663 return false;
1664
1665 /*
1666 * Only consider a task move if the source has a higher load
1667 * than the destination, corrected for CPU capacity on each node.
1668 *
1669 * src->load dst->load
1670 * --------------------- vs ---------------------
1671 * src->compute_capacity dst->compute_capacity
1672 */
Srikar Dronamraju44dcb042015-06-16 17:26:00 +05301673 if (src->load * dst->compute_capacity * env->imbalance_pct >
1674
1675 dst->load * src->compute_capacity * 100)
Rik van Riel6f9aad02015-05-28 09:52:49 -04001676 return true;
1677
1678 return false;
1679}
1680
Mel Gorman58d081b2013-10-07 11:29:10 +01001681static int task_numa_migrate(struct task_struct *p)
Mel Gormane6628d52013-10-07 11:29:02 +01001682{
Mel Gorman58d081b2013-10-07 11:29:10 +01001683 struct task_numa_env env = {
1684 .p = p,
Mel Gormanfb13c7e2013-10-07 11:29:17 +01001685
Mel Gorman58d081b2013-10-07 11:29:10 +01001686 .src_cpu = task_cpu(p),
Ingo Molnarb32e86b2013-10-07 11:29:30 +01001687 .src_nid = task_node(p),
Mel Gormanfb13c7e2013-10-07 11:29:17 +01001688
1689 .imbalance_pct = 112,
1690
1691 .best_task = NULL,
1692 .best_imp = 0,
Rik van Riel4142c3e2016-01-25 17:07:39 -05001693 .best_cpu = -1,
Mel Gorman58d081b2013-10-07 11:29:10 +01001694 };
1695 struct sched_domain *sd;
Rik van Riel887c2902013-10-07 11:29:31 +01001696 unsigned long taskweight, groupweight;
Rik van Riel7bd95322014-10-17 03:29:51 -04001697 int nid, ret, dist;
Rik van Riel887c2902013-10-07 11:29:31 +01001698 long taskimp, groupimp;
Mel Gormane6628d52013-10-07 11:29:02 +01001699
Mel Gorman58d081b2013-10-07 11:29:10 +01001700 /*
Mel Gormanfb13c7e2013-10-07 11:29:17 +01001701 * Pick the lowest SD_NUMA domain, as that would have the smallest
1702 * imbalance and would be the first to start moving tasks about.
1703 *
1704 * And we want to avoid any moving of tasks about, as that would create
1705 * random movement of tasks -- counter the numa conditions we're trying
1706 * to satisfy here.
Mel Gorman58d081b2013-10-07 11:29:10 +01001707 */
Mel Gormane6628d52013-10-07 11:29:02 +01001708 rcu_read_lock();
Mel Gormanfb13c7e2013-10-07 11:29:17 +01001709 sd = rcu_dereference(per_cpu(sd_numa, env.src_cpu));
Rik van Riel46a73e82013-11-11 19:29:25 -05001710 if (sd)
1711 env.imbalance_pct = 100 + (sd->imbalance_pct - 100) / 2;
Mel Gormane6628d52013-10-07 11:29:02 +01001712 rcu_read_unlock();
1713
Rik van Riel46a73e82013-11-11 19:29:25 -05001714 /*
1715 * Cpusets can break the scheduler domain tree into smaller
1716 * balance domains, some of which do not cross NUMA boundaries.
1717 * Tasks that are "trapped" in such domains cannot be migrated
1718 * elsewhere, so there is no point in (re)trying.
1719 */
1720 if (unlikely(!sd)) {
Wanpeng Lide1b3012013-12-12 15:23:24 +08001721 p->numa_preferred_nid = task_node(p);
Rik van Riel46a73e82013-11-11 19:29:25 -05001722 return -EINVAL;
1723 }
1724
Mel Gorman2c8a50a2013-10-07 11:29:18 +01001725 env.dst_nid = p->numa_preferred_nid;
Rik van Riel7bd95322014-10-17 03:29:51 -04001726 dist = env.dist = node_distance(env.src_nid, env.dst_nid);
1727 taskweight = task_weight(p, env.src_nid, dist);
1728 groupweight = group_weight(p, env.src_nid, dist);
1729 update_numa_stats(&env.src_stats, env.src_nid);
1730 taskimp = task_weight(p, env.dst_nid, dist) - taskweight;
1731 groupimp = group_weight(p, env.dst_nid, dist) - groupweight;
Mel Gorman2c8a50a2013-10-07 11:29:18 +01001732 update_numa_stats(&env.dst_stats, env.dst_nid);
Mel Gorman58d081b2013-10-07 11:29:10 +01001733
Rik van Riela43455a2014-06-04 16:09:42 -04001734 /* Try to find a spot on the preferred nid. */
Rik van Riel6f9aad02015-05-28 09:52:49 -04001735 if (numa_has_capacity(&env))
1736 task_numa_find_cpu(&env, taskimp, groupimp);
Rik van Riele1dda8a2013-10-07 11:29:19 +01001737
Rik van Riel9de05d42014-10-09 17:27:47 -04001738 /*
1739 * Look at other nodes in these cases:
1740 * - there is no space available on the preferred_nid
1741 * - the task is part of a numa_group that is interleaved across
1742 * multiple NUMA nodes; in order to better consolidate the group,
1743 * we need to check other locations.
1744 */
Rik van Riel4142c3e2016-01-25 17:07:39 -05001745 if (env.best_cpu == -1 || (p->numa_group && p->numa_group->active_nodes > 1)) {
Mel Gorman2c8a50a2013-10-07 11:29:18 +01001746 for_each_online_node(nid) {
1747 if (nid == env.src_nid || nid == p->numa_preferred_nid)
Mel Gormanfb13c7e2013-10-07 11:29:17 +01001748 continue;
1749
Rik van Riel7bd95322014-10-17 03:29:51 -04001750 dist = node_distance(env.src_nid, env.dst_nid);
Rik van Riel6c6b1192014-10-17 03:29:52 -04001751 if (sched_numa_topology_type == NUMA_BACKPLANE &&
1752 dist != env.dist) {
1753 taskweight = task_weight(p, env.src_nid, dist);
1754 groupweight = group_weight(p, env.src_nid, dist);
1755 }
Rik van Riel7bd95322014-10-17 03:29:51 -04001756
Mel Gorman83e1d2c2013-10-07 11:29:27 +01001757 /* Only consider nodes where both task and groups benefit */
Rik van Riel7bd95322014-10-17 03:29:51 -04001758 taskimp = task_weight(p, nid, dist) - taskweight;
1759 groupimp = group_weight(p, nid, dist) - groupweight;
Rik van Riel887c2902013-10-07 11:29:31 +01001760 if (taskimp < 0 && groupimp < 0)
Mel Gorman2c8a50a2013-10-07 11:29:18 +01001761 continue;
1762
Rik van Riel7bd95322014-10-17 03:29:51 -04001763 env.dist = dist;
Mel Gorman2c8a50a2013-10-07 11:29:18 +01001764 env.dst_nid = nid;
1765 update_numa_stats(&env.dst_stats, env.dst_nid);
Rik van Riel6f9aad02015-05-28 09:52:49 -04001766 if (numa_has_capacity(&env))
1767 task_numa_find_cpu(&env, taskimp, groupimp);
Mel Gorman58d081b2013-10-07 11:29:10 +01001768 }
1769 }
1770
Rik van Riel68d1b022014-04-11 13:00:29 -04001771 /*
1772 * If the task is part of a workload that spans multiple NUMA nodes,
1773 * and is migrating into one of the workload's active nodes, remember
1774 * this node as the task's preferred numa node, so the workload can
1775 * settle down.
1776 * A task that migrated to a second choice node will be better off
1777 * trying for a better one later. Do not set the preferred node here.
1778 */
Rik van Rieldb015da2014-06-23 11:41:34 -04001779 if (p->numa_group) {
Rik van Riel4142c3e2016-01-25 17:07:39 -05001780 struct numa_group *ng = p->numa_group;
1781
Rik van Rieldb015da2014-06-23 11:41:34 -04001782 if (env.best_cpu == -1)
1783 nid = env.src_nid;
1784 else
1785 nid = env.dst_nid;
1786
Rik van Riel4142c3e2016-01-25 17:07:39 -05001787 if (ng->active_nodes > 1 && numa_is_active_node(env.dst_nid, ng))
Rik van Rieldb015da2014-06-23 11:41:34 -04001788 sched_setnuma(p, env.dst_nid);
1789 }
1790
1791 /* No better CPU than the current one was found. */
1792 if (env.best_cpu == -1)
1793 return -EAGAIN;
Peter Zijlstra0ec8aa02013-10-07 11:29:33 +01001794
Rik van Riel04bb2f92013-10-07 11:29:36 +01001795 /*
1796 * Reset the scan period if the task is being rescheduled on an
1797 * alternative node to recheck if the tasks is now properly placed.
1798 */
1799 p->numa_scan_period = task_scan_min(p);
1800
Mel Gormanfb13c7e2013-10-07 11:29:17 +01001801 if (env.best_task == NULL) {
Mel Gorman286549d2014-01-21 15:51:03 -08001802 ret = migrate_task_to(p, env.best_cpu);
1803 if (ret != 0)
1804 trace_sched_stick_numa(p, env.src_cpu, env.best_cpu);
Mel Gormanfb13c7e2013-10-07 11:29:17 +01001805 return ret;
1806 }
1807
1808 ret = migrate_swap(p, env.best_task);
Mel Gorman286549d2014-01-21 15:51:03 -08001809 if (ret != 0)
1810 trace_sched_stick_numa(p, env.src_cpu, task_cpu(env.best_task));
Mel Gormanfb13c7e2013-10-07 11:29:17 +01001811 put_task_struct(env.best_task);
1812 return ret;
Mel Gormane6628d52013-10-07 11:29:02 +01001813}
1814
Mel Gorman6b9a7462013-10-07 11:29:11 +01001815/* Attempt to migrate a task to a CPU on the preferred node. */
1816static void numa_migrate_preferred(struct task_struct *p)
1817{
Rik van Riel5085e2a2014-04-11 13:00:28 -04001818 unsigned long interval = HZ;
1819
Rik van Riel2739d3e2013-10-07 11:29:41 +01001820 /* This task has no NUMA fault statistics yet */
Iulia Manda44dba3d2014-10-31 02:13:31 +02001821 if (unlikely(p->numa_preferred_nid == -1 || !p->numa_faults))
Rik van Riel2739d3e2013-10-07 11:29:41 +01001822 return;
1823
1824 /* Periodically retry migrating the task to the preferred node */
Rik van Riel5085e2a2014-04-11 13:00:28 -04001825 interval = min(interval, msecs_to_jiffies(p->numa_scan_period) / 16);
1826 p->numa_migrate_retry = jiffies + interval;
Rik van Riel2739d3e2013-10-07 11:29:41 +01001827
Mel Gorman6b9a7462013-10-07 11:29:11 +01001828 /* Success if task is already running on preferred CPU */
Wanpeng Lide1b3012013-12-12 15:23:24 +08001829 if (task_node(p) == p->numa_preferred_nid)
Mel Gorman6b9a7462013-10-07 11:29:11 +01001830 return;
1831
Mel Gorman6b9a7462013-10-07 11:29:11 +01001832 /* Otherwise, try migrate to a CPU on the preferred node */
Rik van Riel2739d3e2013-10-07 11:29:41 +01001833 task_numa_migrate(p);
Mel Gorman6b9a7462013-10-07 11:29:11 +01001834}
1835
Rik van Riel04bb2f92013-10-07 11:29:36 +01001836/*
Rik van Riel4142c3e2016-01-25 17:07:39 -05001837 * Find out how many nodes on the workload is actively running on. Do this by
Rik van Riel20e07de2014-01-27 17:03:43 -05001838 * tracking the nodes from which NUMA hinting faults are triggered. This can
1839 * be different from the set of nodes where the workload's memory is currently
1840 * located.
Rik van Riel20e07de2014-01-27 17:03:43 -05001841 */
Rik van Riel4142c3e2016-01-25 17:07:39 -05001842static void numa_group_count_active_nodes(struct numa_group *numa_group)
Rik van Riel20e07de2014-01-27 17:03:43 -05001843{
1844 unsigned long faults, max_faults = 0;
Rik van Riel4142c3e2016-01-25 17:07:39 -05001845 int nid, active_nodes = 0;
Rik van Riel20e07de2014-01-27 17:03:43 -05001846
1847 for_each_online_node(nid) {
1848 faults = group_faults_cpu(numa_group, nid);
1849 if (faults > max_faults)
1850 max_faults = faults;
1851 }
1852
1853 for_each_online_node(nid) {
1854 faults = group_faults_cpu(numa_group, nid);
Rik van Riel4142c3e2016-01-25 17:07:39 -05001855 if (faults * ACTIVE_NODE_FRACTION > max_faults)
1856 active_nodes++;
Rik van Riel20e07de2014-01-27 17:03:43 -05001857 }
Rik van Riel4142c3e2016-01-25 17:07:39 -05001858
1859 numa_group->max_faults_cpu = max_faults;
1860 numa_group->active_nodes = active_nodes;
Rik van Riel20e07de2014-01-27 17:03:43 -05001861}
1862
1863/*
Rik van Riel04bb2f92013-10-07 11:29:36 +01001864 * When adapting the scan rate, the period is divided into NUMA_PERIOD_SLOTS
1865 * increments. The more local the fault statistics are, the higher the scan
Rik van Riela22b4b02014-06-23 11:41:35 -04001866 * period will be for the next scan window. If local/(local+remote) ratio is
1867 * below NUMA_PERIOD_THRESHOLD (where range of ratio is 1..NUMA_PERIOD_SLOTS)
1868 * the scan period will decrease. Aim for 70% local accesses.
Rik van Riel04bb2f92013-10-07 11:29:36 +01001869 */
1870#define NUMA_PERIOD_SLOTS 10
Rik van Riela22b4b02014-06-23 11:41:35 -04001871#define NUMA_PERIOD_THRESHOLD 7
Rik van Riel04bb2f92013-10-07 11:29:36 +01001872
1873/*
1874 * Increase the scan period (slow down scanning) if the majority of
1875 * our memory is already on our local node, or if the majority of
1876 * the page accesses are shared with other processes.
1877 * Otherwise, decrease the scan period.
1878 */
1879static void update_task_scan_period(struct task_struct *p,
1880 unsigned long shared, unsigned long private)
1881{
1882 unsigned int period_slot;
1883 int ratio;
1884 int diff;
1885
1886 unsigned long remote = p->numa_faults_locality[0];
1887 unsigned long local = p->numa_faults_locality[1];
1888
1889 /*
1890 * If there were no record hinting faults then either the task is
1891 * completely idle or all activity is areas that are not of interest
Mel Gorman074c2382015-03-25 15:55:42 -07001892 * to automatic numa balancing. Related to that, if there were failed
1893 * migration then it implies we are migrating too quickly or the local
1894 * node is overloaded. In either case, scan slower
Rik van Riel04bb2f92013-10-07 11:29:36 +01001895 */
Mel Gorman074c2382015-03-25 15:55:42 -07001896 if (local + shared == 0 || p->numa_faults_locality[2]) {
Rik van Riel04bb2f92013-10-07 11:29:36 +01001897 p->numa_scan_period = min(p->numa_scan_period_max,
1898 p->numa_scan_period << 1);
1899
1900 p->mm->numa_next_scan = jiffies +
1901 msecs_to_jiffies(p->numa_scan_period);
1902
1903 return;
1904 }
1905
1906 /*
1907 * Prepare to scale scan period relative to the current period.
1908 * == NUMA_PERIOD_THRESHOLD scan period stays the same
1909 * < NUMA_PERIOD_THRESHOLD scan period decreases (scan faster)
1910 * >= NUMA_PERIOD_THRESHOLD scan period increases (scan slower)
1911 */
1912 period_slot = DIV_ROUND_UP(p->numa_scan_period, NUMA_PERIOD_SLOTS);
1913 ratio = (local * NUMA_PERIOD_SLOTS) / (local + remote);
1914 if (ratio >= NUMA_PERIOD_THRESHOLD) {
1915 int slot = ratio - NUMA_PERIOD_THRESHOLD;
1916 if (!slot)
1917 slot = 1;
1918 diff = slot * period_slot;
1919 } else {
1920 diff = -(NUMA_PERIOD_THRESHOLD - ratio) * period_slot;
1921
1922 /*
1923 * Scale scan rate increases based on sharing. There is an
1924 * inverse relationship between the degree of sharing and
1925 * the adjustment made to the scanning period. Broadly
1926 * speaking the intent is that there is little point
1927 * scanning faster if shared accesses dominate as it may
1928 * simply bounce migrations uselessly
1929 */
Yasuaki Ishimatsu2847c902014-10-22 16:04:35 +09001930 ratio = DIV_ROUND_UP(private * NUMA_PERIOD_SLOTS, (private + shared + 1));
Rik van Riel04bb2f92013-10-07 11:29:36 +01001931 diff = (diff * ratio) / NUMA_PERIOD_SLOTS;
1932 }
1933
1934 p->numa_scan_period = clamp(p->numa_scan_period + diff,
1935 task_scan_min(p), task_scan_max(p));
1936 memset(p->numa_faults_locality, 0, sizeof(p->numa_faults_locality));
1937}
1938
Rik van Riel7e2703e2014-01-27 17:03:45 -05001939/*
1940 * Get the fraction of time the task has been running since the last
1941 * NUMA placement cycle. The scheduler keeps similar statistics, but
1942 * decays those on a 32ms period, which is orders of magnitude off
1943 * from the dozens-of-seconds NUMA balancing period. Use the scheduler
1944 * stats only if the task is so new there are no NUMA statistics yet.
1945 */
1946static u64 numa_get_avg_runtime(struct task_struct *p, u64 *period)
1947{
1948 u64 runtime, delta, now;
1949 /* Use the start of this time slice to avoid calculations. */
1950 now = p->se.exec_start;
1951 runtime = p->se.sum_exec_runtime;
1952
1953 if (p->last_task_numa_placement) {
1954 delta = runtime - p->last_sum_exec_runtime;
1955 *period = now - p->last_task_numa_placement;
1956 } else {
Yuyang Du9d89c252015-07-15 08:04:37 +08001957 delta = p->se.avg.load_sum / p->se.load.weight;
1958 *period = LOAD_AVG_MAX;
Rik van Riel7e2703e2014-01-27 17:03:45 -05001959 }
1960
1961 p->last_sum_exec_runtime = runtime;
1962 p->last_task_numa_placement = now;
1963
1964 return delta;
1965}
1966
Rik van Riel54009412014-10-17 03:29:53 -04001967/*
1968 * Determine the preferred nid for a task in a numa_group. This needs to
1969 * be done in a way that produces consistent results with group_weight,
1970 * otherwise workloads might not converge.
1971 */
1972static int preferred_group_nid(struct task_struct *p, int nid)
1973{
1974 nodemask_t nodes;
1975 int dist;
1976
1977 /* Direct connections between all NUMA nodes. */
1978 if (sched_numa_topology_type == NUMA_DIRECT)
1979 return nid;
1980
1981 /*
1982 * On a system with glueless mesh NUMA topology, group_weight
1983 * scores nodes according to the number of NUMA hinting faults on
1984 * both the node itself, and on nearby nodes.
1985 */
1986 if (sched_numa_topology_type == NUMA_GLUELESS_MESH) {
1987 unsigned long score, max_score = 0;
1988 int node, max_node = nid;
1989
1990 dist = sched_max_numa_distance;
1991
1992 for_each_online_node(node) {
1993 score = group_weight(p, node, dist);
1994 if (score > max_score) {
1995 max_score = score;
1996 max_node = node;
1997 }
1998 }
1999 return max_node;
2000 }
2001
2002 /*
2003 * Finding the preferred nid in a system with NUMA backplane
2004 * interconnect topology is more involved. The goal is to locate
2005 * tasks from numa_groups near each other in the system, and
2006 * untangle workloads from different sides of the system. This requires
2007 * searching down the hierarchy of node groups, recursively searching
2008 * inside the highest scoring group of nodes. The nodemask tricks
2009 * keep the complexity of the search down.
2010 */
2011 nodes = node_online_map;
2012 for (dist = sched_max_numa_distance; dist > LOCAL_DISTANCE; dist--) {
2013 unsigned long max_faults = 0;
Jan Beulich81907472015-01-23 08:25:38 +00002014 nodemask_t max_group = NODE_MASK_NONE;
Rik van Riel54009412014-10-17 03:29:53 -04002015 int a, b;
2016
2017 /* Are there nodes at this distance from each other? */
2018 if (!find_numa_distance(dist))
2019 continue;
2020
2021 for_each_node_mask(a, nodes) {
2022 unsigned long faults = 0;
2023 nodemask_t this_group;
2024 nodes_clear(this_group);
2025
2026 /* Sum group's NUMA faults; includes a==b case. */
2027 for_each_node_mask(b, nodes) {
2028 if (node_distance(a, b) < dist) {
2029 faults += group_faults(p, b);
2030 node_set(b, this_group);
2031 node_clear(b, nodes);
2032 }
2033 }
2034
2035 /* Remember the top group. */
2036 if (faults > max_faults) {
2037 max_faults = faults;
2038 max_group = this_group;
2039 /*
2040 * subtle: at the smallest distance there is
2041 * just one node left in each "group", the
2042 * winner is the preferred nid.
2043 */
2044 nid = a;
2045 }
2046 }
2047 /* Next round, evaluate the nodes within max_group. */
Jan Beulich890a5402015-02-09 12:30:00 +01002048 if (!max_faults)
2049 break;
Rik van Riel54009412014-10-17 03:29:53 -04002050 nodes = max_group;
2051 }
2052 return nid;
2053}
2054
Peter Zijlstracbee9f82012-10-25 14:16:43 +02002055static void task_numa_placement(struct task_struct *p)
2056{
Mel Gorman83e1d2c2013-10-07 11:29:27 +01002057 int seq, nid, max_nid = -1, max_group_nid = -1;
2058 unsigned long max_faults = 0, max_group_faults = 0;
Rik van Riel04bb2f92013-10-07 11:29:36 +01002059 unsigned long fault_types[2] = { 0, 0 };
Rik van Riel7e2703e2014-01-27 17:03:45 -05002060 unsigned long total_faults;
2061 u64 runtime, period;
Mel Gorman7dbd13e2013-10-07 11:29:29 +01002062 spinlock_t *group_lock = NULL;
Peter Zijlstracbee9f82012-10-25 14:16:43 +02002063
Jason Low7e5a2c12015-04-30 17:28:14 -07002064 /*
2065 * The p->mm->numa_scan_seq field gets updated without
2066 * exclusive access. Use READ_ONCE() here to ensure
2067 * that the field is read in a single access:
2068 */
Jason Low316c1608d2015-04-28 13:00:20 -07002069 seq = READ_ONCE(p->mm->numa_scan_seq);
Peter Zijlstracbee9f82012-10-25 14:16:43 +02002070 if (p->numa_scan_seq == seq)
2071 return;
2072 p->numa_scan_seq = seq;
Mel Gorman598f0ec2013-10-07 11:28:55 +01002073 p->numa_scan_period_max = task_scan_max(p);
Peter Zijlstracbee9f82012-10-25 14:16:43 +02002074
Rik van Riel7e2703e2014-01-27 17:03:45 -05002075 total_faults = p->numa_faults_locality[0] +
2076 p->numa_faults_locality[1];
2077 runtime = numa_get_avg_runtime(p, &period);
2078
Mel Gorman7dbd13e2013-10-07 11:29:29 +01002079 /* If the task is part of a group prevent parallel updates to group stats */
2080 if (p->numa_group) {
2081 group_lock = &p->numa_group->lock;
Mike Galbraith60e69ee2014-04-07 10:55:15 +02002082 spin_lock_irq(group_lock);
Mel Gorman7dbd13e2013-10-07 11:29:29 +01002083 }
2084
Mel Gorman688b7582013-10-07 11:28:58 +01002085 /* Find the node with the highest number of faults */
2086 for_each_online_node(nid) {
Iulia Manda44dba3d2014-10-31 02:13:31 +02002087 /* Keep track of the offsets in numa_faults array */
2088 int mem_idx, membuf_idx, cpu_idx, cpubuf_idx;
Mel Gorman83e1d2c2013-10-07 11:29:27 +01002089 unsigned long faults = 0, group_faults = 0;
Iulia Manda44dba3d2014-10-31 02:13:31 +02002090 int priv;
Mel Gorman745d6142013-10-07 11:28:59 +01002091
Rik van Rielbe1e4e72014-01-27 17:03:48 -05002092 for (priv = 0; priv < NR_NUMA_HINT_FAULT_TYPES; priv++) {
Rik van Riel7e2703e2014-01-27 17:03:45 -05002093 long diff, f_diff, f_weight;
Peter Zijlstra8c8a7432013-10-07 11:29:21 +01002094
Iulia Manda44dba3d2014-10-31 02:13:31 +02002095 mem_idx = task_faults_idx(NUMA_MEM, nid, priv);
2096 membuf_idx = task_faults_idx(NUMA_MEMBUF, nid, priv);
2097 cpu_idx = task_faults_idx(NUMA_CPU, nid, priv);
2098 cpubuf_idx = task_faults_idx(NUMA_CPUBUF, nid, priv);
Mel Gorman745d6142013-10-07 11:28:59 +01002099
Mel Gormanac8e8952013-10-07 11:29:03 +01002100 /* Decay existing window, copy faults since last scan */
Iulia Manda44dba3d2014-10-31 02:13:31 +02002101 diff = p->numa_faults[membuf_idx] - p->numa_faults[mem_idx] / 2;
2102 fault_types[priv] += p->numa_faults[membuf_idx];
2103 p->numa_faults[membuf_idx] = 0;
Mel Gormanfb13c7e2013-10-07 11:29:17 +01002104
Rik van Riel7e2703e2014-01-27 17:03:45 -05002105 /*
2106 * Normalize the faults_from, so all tasks in a group
2107 * count according to CPU use, instead of by the raw
2108 * number of faults. Tasks with little runtime have
2109 * little over-all impact on throughput, and thus their
2110 * faults are less important.
2111 */
2112 f_weight = div64_u64(runtime << 16, period + 1);
Iulia Manda44dba3d2014-10-31 02:13:31 +02002113 f_weight = (f_weight * p->numa_faults[cpubuf_idx]) /
Rik van Riel7e2703e2014-01-27 17:03:45 -05002114 (total_faults + 1);
Iulia Manda44dba3d2014-10-31 02:13:31 +02002115 f_diff = f_weight - p->numa_faults[cpu_idx] / 2;
2116 p->numa_faults[cpubuf_idx] = 0;
Rik van Riel50ec8a42014-01-27 17:03:42 -05002117
Iulia Manda44dba3d2014-10-31 02:13:31 +02002118 p->numa_faults[mem_idx] += diff;
2119 p->numa_faults[cpu_idx] += f_diff;
2120 faults += p->numa_faults[mem_idx];
Mel Gorman83e1d2c2013-10-07 11:29:27 +01002121 p->total_numa_faults += diff;
Peter Zijlstra8c8a7432013-10-07 11:29:21 +01002122 if (p->numa_group) {
Iulia Manda44dba3d2014-10-31 02:13:31 +02002123 /*
2124 * safe because we can only change our own group
2125 *
2126 * mem_idx represents the offset for a given
2127 * nid and priv in a specific region because it
2128 * is at the beginning of the numa_faults array.
2129 */
2130 p->numa_group->faults[mem_idx] += diff;
2131 p->numa_group->faults_cpu[mem_idx] += f_diff;
Mel Gorman989348b2013-10-07 11:29:40 +01002132 p->numa_group->total_faults += diff;
Iulia Manda44dba3d2014-10-31 02:13:31 +02002133 group_faults += p->numa_group->faults[mem_idx];
Peter Zijlstra8c8a7432013-10-07 11:29:21 +01002134 }
Mel Gormanac8e8952013-10-07 11:29:03 +01002135 }
2136
Mel Gorman688b7582013-10-07 11:28:58 +01002137 if (faults > max_faults) {
2138 max_faults = faults;
2139 max_nid = nid;
2140 }
Mel Gorman83e1d2c2013-10-07 11:29:27 +01002141
2142 if (group_faults > max_group_faults) {
2143 max_group_faults = group_faults;
2144 max_group_nid = nid;
2145 }
2146 }
2147
Rik van Riel04bb2f92013-10-07 11:29:36 +01002148 update_task_scan_period(p, fault_types[0], fault_types[1]);
2149
Mel Gorman7dbd13e2013-10-07 11:29:29 +01002150 if (p->numa_group) {
Rik van Riel4142c3e2016-01-25 17:07:39 -05002151 numa_group_count_active_nodes(p->numa_group);
Mike Galbraith60e69ee2014-04-07 10:55:15 +02002152 spin_unlock_irq(group_lock);
Rik van Riel54009412014-10-17 03:29:53 -04002153 max_nid = preferred_group_nid(p, max_group_nid);
Mel Gorman688b7582013-10-07 11:28:58 +01002154 }
2155
Rik van Rielbb97fc32014-06-04 16:33:15 -04002156 if (max_faults) {
2157 /* Set the new preferred node */
2158 if (max_nid != p->numa_preferred_nid)
2159 sched_setnuma(p, max_nid);
2160
2161 if (task_node(p) != p->numa_preferred_nid)
2162 numa_migrate_preferred(p);
Mel Gorman3a7053b2013-10-07 11:29:00 +01002163 }
Peter Zijlstracbee9f82012-10-25 14:16:43 +02002164}
2165
Peter Zijlstra8c8a7432013-10-07 11:29:21 +01002166static inline int get_numa_group(struct numa_group *grp)
2167{
2168 return atomic_inc_not_zero(&grp->refcount);
2169}
2170
2171static inline void put_numa_group(struct numa_group *grp)
2172{
2173 if (atomic_dec_and_test(&grp->refcount))
2174 kfree_rcu(grp, rcu);
2175}
2176
Mel Gorman3e6a9412013-10-07 11:29:35 +01002177static void task_numa_group(struct task_struct *p, int cpupid, int flags,
2178 int *priv)
Peter Zijlstra8c8a7432013-10-07 11:29:21 +01002179{
2180 struct numa_group *grp, *my_grp;
2181 struct task_struct *tsk;
2182 bool join = false;
2183 int cpu = cpupid_to_cpu(cpupid);
2184 int i;
2185
2186 if (unlikely(!p->numa_group)) {
2187 unsigned int size = sizeof(struct numa_group) +
Rik van Riel50ec8a42014-01-27 17:03:42 -05002188 4*nr_node_ids*sizeof(unsigned long);
Peter Zijlstra8c8a7432013-10-07 11:29:21 +01002189
2190 grp = kzalloc(size, GFP_KERNEL | __GFP_NOWARN);
2191 if (!grp)
2192 return;
2193
2194 atomic_set(&grp->refcount, 1);
Rik van Riel4142c3e2016-01-25 17:07:39 -05002195 grp->active_nodes = 1;
2196 grp->max_faults_cpu = 0;
Peter Zijlstra8c8a7432013-10-07 11:29:21 +01002197 spin_lock_init(&grp->lock);
Mel Gormane29cf082013-10-07 11:29:22 +01002198 grp->gid = p->pid;
Rik van Riel50ec8a42014-01-27 17:03:42 -05002199 /* Second half of the array tracks nids where faults happen */
Rik van Rielbe1e4e72014-01-27 17:03:48 -05002200 grp->faults_cpu = grp->faults + NR_NUMA_HINT_FAULT_TYPES *
2201 nr_node_ids;
Peter Zijlstra8c8a7432013-10-07 11:29:21 +01002202
Rik van Rielbe1e4e72014-01-27 17:03:48 -05002203 for (i = 0; i < NR_NUMA_HINT_FAULT_STATS * nr_node_ids; i++)
Iulia Manda44dba3d2014-10-31 02:13:31 +02002204 grp->faults[i] = p->numa_faults[i];
Peter Zijlstra8c8a7432013-10-07 11:29:21 +01002205
Mel Gorman989348b2013-10-07 11:29:40 +01002206 grp->total_faults = p->total_numa_faults;
Mel Gorman83e1d2c2013-10-07 11:29:27 +01002207
Peter Zijlstra8c8a7432013-10-07 11:29:21 +01002208 grp->nr_tasks++;
2209 rcu_assign_pointer(p->numa_group, grp);
2210 }
2211
2212 rcu_read_lock();
Jason Low316c1608d2015-04-28 13:00:20 -07002213 tsk = READ_ONCE(cpu_rq(cpu)->curr);
Peter Zijlstra8c8a7432013-10-07 11:29:21 +01002214
2215 if (!cpupid_match_pid(tsk, cpupid))
Peter Zijlstra33547812013-10-09 10:24:48 +02002216 goto no_join;
Peter Zijlstra8c8a7432013-10-07 11:29:21 +01002217
2218 grp = rcu_dereference(tsk->numa_group);
2219 if (!grp)
Peter Zijlstra33547812013-10-09 10:24:48 +02002220 goto no_join;
Peter Zijlstra8c8a7432013-10-07 11:29:21 +01002221
2222 my_grp = p->numa_group;
2223 if (grp == my_grp)
Peter Zijlstra33547812013-10-09 10:24:48 +02002224 goto no_join;
Peter Zijlstra8c8a7432013-10-07 11:29:21 +01002225
2226 /*
2227 * Only join the other group if its bigger; if we're the bigger group,
2228 * the other task will join us.
2229 */
2230 if (my_grp->nr_tasks > grp->nr_tasks)
Peter Zijlstra33547812013-10-09 10:24:48 +02002231 goto no_join;
Peter Zijlstra8c8a7432013-10-07 11:29:21 +01002232
2233 /*
2234 * Tie-break on the grp address.
2235 */
2236 if (my_grp->nr_tasks == grp->nr_tasks && my_grp > grp)
Peter Zijlstra33547812013-10-09 10:24:48 +02002237 goto no_join;
Peter Zijlstra8c8a7432013-10-07 11:29:21 +01002238
Rik van Rieldabe1d92013-10-07 11:29:34 +01002239 /* Always join threads in the same process. */
2240 if (tsk->mm == current->mm)
2241 join = true;
Peter Zijlstra8c8a7432013-10-07 11:29:21 +01002242
Rik van Rieldabe1d92013-10-07 11:29:34 +01002243 /* Simple filter to avoid false positives due to PID collisions */
2244 if (flags & TNF_SHARED)
2245 join = true;
2246
Mel Gorman3e6a9412013-10-07 11:29:35 +01002247 /* Update priv based on whether false sharing was detected */
2248 *priv = !join;
2249
Rik van Rieldabe1d92013-10-07 11:29:34 +01002250 if (join && !get_numa_group(grp))
Peter Zijlstra33547812013-10-09 10:24:48 +02002251 goto no_join;
Peter Zijlstra8c8a7432013-10-07 11:29:21 +01002252
Peter Zijlstra8c8a7432013-10-07 11:29:21 +01002253 rcu_read_unlock();
2254
2255 if (!join)
2256 return;
2257
Mike Galbraith60e69ee2014-04-07 10:55:15 +02002258 BUG_ON(irqs_disabled());
2259 double_lock_irq(&my_grp->lock, &grp->lock);
Peter Zijlstra8c8a7432013-10-07 11:29:21 +01002260
Rik van Rielbe1e4e72014-01-27 17:03:48 -05002261 for (i = 0; i < NR_NUMA_HINT_FAULT_STATS * nr_node_ids; i++) {
Iulia Manda44dba3d2014-10-31 02:13:31 +02002262 my_grp->faults[i] -= p->numa_faults[i];
2263 grp->faults[i] += p->numa_faults[i];
Mel Gorman989348b2013-10-07 11:29:40 +01002264 }
2265 my_grp->total_faults -= p->total_numa_faults;
2266 grp->total_faults += p->total_numa_faults;
2267
Peter Zijlstra8c8a7432013-10-07 11:29:21 +01002268 my_grp->nr_tasks--;
2269 grp->nr_tasks++;
2270
2271 spin_unlock(&my_grp->lock);
Mike Galbraith60e69ee2014-04-07 10:55:15 +02002272 spin_unlock_irq(&grp->lock);
Peter Zijlstra8c8a7432013-10-07 11:29:21 +01002273
2274 rcu_assign_pointer(p->numa_group, grp);
2275
2276 put_numa_group(my_grp);
Peter Zijlstra33547812013-10-09 10:24:48 +02002277 return;
2278
2279no_join:
2280 rcu_read_unlock();
2281 return;
Peter Zijlstra8c8a7432013-10-07 11:29:21 +01002282}
2283
2284void task_numa_free(struct task_struct *p)
2285{
2286 struct numa_group *grp = p->numa_group;
Iulia Manda44dba3d2014-10-31 02:13:31 +02002287 void *numa_faults = p->numa_faults;
Steven Rostedte9dd6852014-05-27 17:02:04 -04002288 unsigned long flags;
2289 int i;
Peter Zijlstra8c8a7432013-10-07 11:29:21 +01002290
2291 if (grp) {
Steven Rostedte9dd6852014-05-27 17:02:04 -04002292 spin_lock_irqsave(&grp->lock, flags);
Rik van Rielbe1e4e72014-01-27 17:03:48 -05002293 for (i = 0; i < NR_NUMA_HINT_FAULT_STATS * nr_node_ids; i++)
Iulia Manda44dba3d2014-10-31 02:13:31 +02002294 grp->faults[i] -= p->numa_faults[i];
Mel Gorman989348b2013-10-07 11:29:40 +01002295 grp->total_faults -= p->total_numa_faults;
2296
Peter Zijlstra8c8a7432013-10-07 11:29:21 +01002297 grp->nr_tasks--;
Steven Rostedte9dd6852014-05-27 17:02:04 -04002298 spin_unlock_irqrestore(&grp->lock, flags);
Andreea-Cristina Bernat35b123e2014-08-22 17:50:43 +03002299 RCU_INIT_POINTER(p->numa_group, NULL);
Peter Zijlstra8c8a7432013-10-07 11:29:21 +01002300 put_numa_group(grp);
2301 }
2302
Iulia Manda44dba3d2014-10-31 02:13:31 +02002303 p->numa_faults = NULL;
Rik van Riel82727012013-10-07 11:29:28 +01002304 kfree(numa_faults);
Peter Zijlstra8c8a7432013-10-07 11:29:21 +01002305}
2306
Peter Zijlstracbee9f82012-10-25 14:16:43 +02002307/*
2308 * Got a PROT_NONE fault for a page on @node.
2309 */
Rik van Riel58b46da2014-01-27 17:03:47 -05002310void task_numa_fault(int last_cpupid, int mem_node, int pages, int flags)
Peter Zijlstracbee9f82012-10-25 14:16:43 +02002311{
2312 struct task_struct *p = current;
Peter Zijlstra6688cc02013-10-07 11:29:24 +01002313 bool migrated = flags & TNF_MIGRATED;
Rik van Riel58b46da2014-01-27 17:03:47 -05002314 int cpu_node = task_node(current);
Rik van Riel792568e2014-04-11 13:00:27 -04002315 int local = !!(flags & TNF_FAULT_LOCAL);
Rik van Riel4142c3e2016-01-25 17:07:39 -05002316 struct numa_group *ng;
Mel Gormanac8e8952013-10-07 11:29:03 +01002317 int priv;
Peter Zijlstracbee9f82012-10-25 14:16:43 +02002318
Srikar Dronamraju2a595722015-08-11 21:54:21 +05302319 if (!static_branch_likely(&sched_numa_balancing))
Mel Gorman1a687c22012-11-22 11:16:36 +00002320 return;
2321
Mel Gorman9ff1d9f2013-10-07 11:29:04 +01002322 /* for example, ksmd faulting in a user's mm */
2323 if (!p->mm)
2324 return;
2325
Mel Gormanf809ca92013-10-07 11:28:57 +01002326 /* Allocate buffer to track faults on a per-node basis */
Iulia Manda44dba3d2014-10-31 02:13:31 +02002327 if (unlikely(!p->numa_faults)) {
2328 int size = sizeof(*p->numa_faults) *
Rik van Rielbe1e4e72014-01-27 17:03:48 -05002329 NR_NUMA_HINT_FAULT_BUCKETS * nr_node_ids;
Mel Gormanf809ca92013-10-07 11:28:57 +01002330
Iulia Manda44dba3d2014-10-31 02:13:31 +02002331 p->numa_faults = kzalloc(size, GFP_KERNEL|__GFP_NOWARN);
2332 if (!p->numa_faults)
Mel Gormanf809ca92013-10-07 11:28:57 +01002333 return;
Mel Gorman745d6142013-10-07 11:28:59 +01002334
Mel Gorman83e1d2c2013-10-07 11:29:27 +01002335 p->total_numa_faults = 0;
Rik van Riel04bb2f92013-10-07 11:29:36 +01002336 memset(p->numa_faults_locality, 0, sizeof(p->numa_faults_locality));
Mel Gormanf809ca92013-10-07 11:28:57 +01002337 }
Peter Zijlstracbee9f82012-10-25 14:16:43 +02002338
Mel Gormanfb003b82012-11-15 09:01:14 +00002339 /*
Peter Zijlstra8c8a7432013-10-07 11:29:21 +01002340 * First accesses are treated as private, otherwise consider accesses
2341 * to be private if the accessing pid has not changed
2342 */
2343 if (unlikely(last_cpupid == (-1 & LAST_CPUPID_MASK))) {
2344 priv = 1;
2345 } else {
2346 priv = cpupid_match_pid(p, last_cpupid);
Peter Zijlstra6688cc02013-10-07 11:29:24 +01002347 if (!priv && !(flags & TNF_NO_GROUP))
Mel Gorman3e6a9412013-10-07 11:29:35 +01002348 task_numa_group(p, last_cpupid, flags, &priv);
Peter Zijlstra8c8a7432013-10-07 11:29:21 +01002349 }
2350
Rik van Riel792568e2014-04-11 13:00:27 -04002351 /*
2352 * If a workload spans multiple NUMA nodes, a shared fault that
2353 * occurs wholly within the set of nodes that the workload is
2354 * actively using should be counted as local. This allows the
2355 * scan rate to slow down when a workload has settled down.
2356 */
Rik van Riel4142c3e2016-01-25 17:07:39 -05002357 ng = p->numa_group;
2358 if (!priv && !local && ng && ng->active_nodes > 1 &&
2359 numa_is_active_node(cpu_node, ng) &&
2360 numa_is_active_node(mem_node, ng))
Rik van Riel792568e2014-04-11 13:00:27 -04002361 local = 1;
2362
Peter Zijlstracbee9f82012-10-25 14:16:43 +02002363 task_numa_placement(p);
Mel Gormanf809ca92013-10-07 11:28:57 +01002364
Rik van Riel2739d3e2013-10-07 11:29:41 +01002365 /*
2366 * Retry task to preferred node migration periodically, in case it
2367 * case it previously failed, or the scheduler moved us.
2368 */
2369 if (time_after(jiffies, p->numa_migrate_retry))
Mel Gorman6b9a7462013-10-07 11:29:11 +01002370 numa_migrate_preferred(p);
2371
Ingo Molnarb32e86b2013-10-07 11:29:30 +01002372 if (migrated)
2373 p->numa_pages_migrated += pages;
Mel Gorman074c2382015-03-25 15:55:42 -07002374 if (flags & TNF_MIGRATE_FAIL)
2375 p->numa_faults_locality[2] += pages;
Ingo Molnarb32e86b2013-10-07 11:29:30 +01002376
Iulia Manda44dba3d2014-10-31 02:13:31 +02002377 p->numa_faults[task_faults_idx(NUMA_MEMBUF, mem_node, priv)] += pages;
2378 p->numa_faults[task_faults_idx(NUMA_CPUBUF, cpu_node, priv)] += pages;
Rik van Riel792568e2014-04-11 13:00:27 -04002379 p->numa_faults_locality[local] += pages;
Peter Zijlstracbee9f82012-10-25 14:16:43 +02002380}
2381
Peter Zijlstra6e5fb222012-10-25 14:16:45 +02002382static void reset_ptenuma_scan(struct task_struct *p)
2383{
Jason Low7e5a2c12015-04-30 17:28:14 -07002384 /*
2385 * We only did a read acquisition of the mmap sem, so
2386 * p->mm->numa_scan_seq is written to without exclusive access
2387 * and the update is not guaranteed to be atomic. That's not
2388 * much of an issue though, since this is just used for
2389 * statistical sampling. Use READ_ONCE/WRITE_ONCE, which are not
2390 * expensive, to avoid any form of compiler optimizations:
2391 */
Jason Low316c1608d2015-04-28 13:00:20 -07002392 WRITE_ONCE(p->mm->numa_scan_seq, READ_ONCE(p->mm->numa_scan_seq) + 1);
Peter Zijlstra6e5fb222012-10-25 14:16:45 +02002393 p->mm->numa_scan_offset = 0;
2394}
2395
Peter Zijlstracbee9f82012-10-25 14:16:43 +02002396/*
2397 * The expensive part of numa migration is done from task_work context.
2398 * Triggered from task_tick_numa().
2399 */
2400void task_numa_work(struct callback_head *work)
2401{
2402 unsigned long migrate, next_scan, now = jiffies;
2403 struct task_struct *p = current;
2404 struct mm_struct *mm = p->mm;
Rik van Riel51170842015-11-05 15:56:23 -05002405 u64 runtime = p->se.sum_exec_runtime;
Peter Zijlstra6e5fb222012-10-25 14:16:45 +02002406 struct vm_area_struct *vma;
Mel Gorman9f406042012-11-14 18:34:32 +00002407 unsigned long start, end;
Mel Gorman598f0ec2013-10-07 11:28:55 +01002408 unsigned long nr_pte_updates = 0;
Rik van Riel4620f8c2015-09-11 09:00:27 -04002409 long pages, virtpages;
Peter Zijlstracbee9f82012-10-25 14:16:43 +02002410
Peter Zijlstra9148a3a2016-09-20 22:34:51 +02002411 SCHED_WARN_ON(p != container_of(work, struct task_struct, numa_work));
Peter Zijlstracbee9f82012-10-25 14:16:43 +02002412
2413 work->next = work; /* protect against double add */
2414 /*
2415 * Who cares about NUMA placement when they're dying.
2416 *
2417 * NOTE: make sure not to dereference p->mm before this check,
2418 * exit_task_work() happens _after_ exit_mm() so we could be called
2419 * without p->mm even though we still had it when we enqueued this
2420 * work.
2421 */
2422 if (p->flags & PF_EXITING)
2423 return;
2424
Mel Gorman930aa172013-10-07 11:29:37 +01002425 if (!mm->numa_next_scan) {
Mel Gorman7e8d16b2013-10-07 11:28:54 +01002426 mm->numa_next_scan = now +
2427 msecs_to_jiffies(sysctl_numa_balancing_scan_delay);
Mel Gormanb8593bf2012-11-21 01:18:23 +00002428 }
2429
2430 /*
Peter Zijlstracbee9f82012-10-25 14:16:43 +02002431 * Enforce maximal scan/migration frequency..
2432 */
2433 migrate = mm->numa_next_scan;
2434 if (time_before(now, migrate))
2435 return;
2436
Mel Gorman598f0ec2013-10-07 11:28:55 +01002437 if (p->numa_scan_period == 0) {
2438 p->numa_scan_period_max = task_scan_max(p);
2439 p->numa_scan_period = task_scan_min(p);
2440 }
Peter Zijlstracbee9f82012-10-25 14:16:43 +02002441
Mel Gormanfb003b82012-11-15 09:01:14 +00002442 next_scan = now + msecs_to_jiffies(p->numa_scan_period);
Peter Zijlstracbee9f82012-10-25 14:16:43 +02002443 if (cmpxchg(&mm->numa_next_scan, migrate, next_scan) != migrate)
2444 return;
2445
Mel Gormane14808b2012-11-19 10:59:15 +00002446 /*
Peter Zijlstra19a78d12013-10-07 11:28:51 +01002447 * Delay this task enough that another task of this mm will likely win
2448 * the next time around.
2449 */
2450 p->node_stamp += 2 * TICK_NSEC;
2451
Mel Gorman9f406042012-11-14 18:34:32 +00002452 start = mm->numa_scan_offset;
2453 pages = sysctl_numa_balancing_scan_size;
2454 pages <<= 20 - PAGE_SHIFT; /* MB in pages */
Rik van Riel4620f8c2015-09-11 09:00:27 -04002455 virtpages = pages * 8; /* Scan up to this much virtual space */
Mel Gorman9f406042012-11-14 18:34:32 +00002456 if (!pages)
2457 return;
Peter Zijlstracbee9f82012-10-25 14:16:43 +02002458
Rik van Riel4620f8c2015-09-11 09:00:27 -04002459
Peter Zijlstra6e5fb222012-10-25 14:16:45 +02002460 down_read(&mm->mmap_sem);
Mel Gorman9f406042012-11-14 18:34:32 +00002461 vma = find_vma(mm, start);
Peter Zijlstra6e5fb222012-10-25 14:16:45 +02002462 if (!vma) {
2463 reset_ptenuma_scan(p);
Mel Gorman9f406042012-11-14 18:34:32 +00002464 start = 0;
Peter Zijlstra6e5fb222012-10-25 14:16:45 +02002465 vma = mm->mmap;
Peter Zijlstracbee9f82012-10-25 14:16:43 +02002466 }
Mel Gorman9f406042012-11-14 18:34:32 +00002467 for (; vma; vma = vma->vm_next) {
Naoya Horiguchi6b79c572015-04-07 14:26:47 -07002468 if (!vma_migratable(vma) || !vma_policy_mof(vma) ||
Mel Gorman8e76d4e2015-06-10 11:15:00 -07002469 is_vm_hugetlb_page(vma) || (vma->vm_flags & VM_MIXEDMAP)) {
Peter Zijlstra6e5fb222012-10-25 14:16:45 +02002470 continue;
Naoya Horiguchi6b79c572015-04-07 14:26:47 -07002471 }
Peter Zijlstra6e5fb222012-10-25 14:16:45 +02002472
Mel Gorman4591ce4f2013-10-07 11:29:13 +01002473 /*
2474 * Shared library pages mapped by multiple processes are not
2475 * migrated as it is expected they are cache replicated. Avoid
2476 * hinting faults in read-only file-backed mappings or the vdso
2477 * as migrating the pages will be of marginal benefit.
2478 */
2479 if (!vma->vm_mm ||
2480 (vma->vm_file && (vma->vm_flags & (VM_READ|VM_WRITE)) == (VM_READ)))
2481 continue;
2482
Mel Gorman3c67f472013-12-18 17:08:40 -08002483 /*
2484 * Skip inaccessible VMAs to avoid any confusion between
2485 * PROT_NONE and NUMA hinting ptes
2486 */
2487 if (!(vma->vm_flags & (VM_READ | VM_EXEC | VM_WRITE)))
2488 continue;
2489
Mel Gorman9f406042012-11-14 18:34:32 +00002490 do {
2491 start = max(start, vma->vm_start);
2492 end = ALIGN(start + (pages << PAGE_SHIFT), HPAGE_SIZE);
2493 end = min(end, vma->vm_end);
Rik van Riel4620f8c2015-09-11 09:00:27 -04002494 nr_pte_updates = change_prot_numa(vma, start, end);
Mel Gorman598f0ec2013-10-07 11:28:55 +01002495
2496 /*
Rik van Riel4620f8c2015-09-11 09:00:27 -04002497 * Try to scan sysctl_numa_balancing_size worth of
2498 * hpages that have at least one present PTE that
2499 * is not already pte-numa. If the VMA contains
2500 * areas that are unused or already full of prot_numa
2501 * PTEs, scan up to virtpages, to skip through those
2502 * areas faster.
Mel Gorman598f0ec2013-10-07 11:28:55 +01002503 */
2504 if (nr_pte_updates)
2505 pages -= (end - start) >> PAGE_SHIFT;
Rik van Riel4620f8c2015-09-11 09:00:27 -04002506 virtpages -= (end - start) >> PAGE_SHIFT;
Peter Zijlstra6e5fb222012-10-25 14:16:45 +02002507
Mel Gorman9f406042012-11-14 18:34:32 +00002508 start = end;
Rik van Riel4620f8c2015-09-11 09:00:27 -04002509 if (pages <= 0 || virtpages <= 0)
Mel Gorman9f406042012-11-14 18:34:32 +00002510 goto out;
Rik van Riel3cf19622014-02-18 17:12:44 -05002511
2512 cond_resched();
Mel Gorman9f406042012-11-14 18:34:32 +00002513 } while (end != vma->vm_end);
Peter Zijlstra6e5fb222012-10-25 14:16:45 +02002514 }
2515
Mel Gorman9f406042012-11-14 18:34:32 +00002516out:
Peter Zijlstra6e5fb222012-10-25 14:16:45 +02002517 /*
Peter Zijlstrac69307d2013-10-07 11:28:41 +01002518 * It is possible to reach the end of the VMA list but the last few
2519 * VMAs are not guaranteed to the vma_migratable. If they are not, we
2520 * would find the !migratable VMA on the next scan but not reset the
2521 * scanner to the start so check it now.
Peter Zijlstra6e5fb222012-10-25 14:16:45 +02002522 */
2523 if (vma)
Mel Gorman9f406042012-11-14 18:34:32 +00002524 mm->numa_scan_offset = start;
Peter Zijlstra6e5fb222012-10-25 14:16:45 +02002525 else
2526 reset_ptenuma_scan(p);
2527 up_read(&mm->mmap_sem);
Rik van Riel51170842015-11-05 15:56:23 -05002528
2529 /*
2530 * Make sure tasks use at least 32x as much time to run other code
2531 * than they used here, to limit NUMA PTE scanning overhead to 3% max.
2532 * Usually update_task_scan_period slows down scanning enough; on an
2533 * overloaded system we need to limit overhead on a per task basis.
2534 */
2535 if (unlikely(p->se.sum_exec_runtime != runtime)) {
2536 u64 diff = p->se.sum_exec_runtime - runtime;
2537 p->node_stamp += 32 * diff;
2538 }
Peter Zijlstracbee9f82012-10-25 14:16:43 +02002539}
2540
2541/*
2542 * Drive the periodic memory faults..
2543 */
2544void task_tick_numa(struct rq *rq, struct task_struct *curr)
2545{
2546 struct callback_head *work = &curr->numa_work;
2547 u64 period, now;
2548
2549 /*
2550 * We don't care about NUMA placement if we don't have memory.
2551 */
2552 if (!curr->mm || (curr->flags & PF_EXITING) || work->next != work)
2553 return;
2554
2555 /*
2556 * Using runtime rather than walltime has the dual advantage that
2557 * we (mostly) drive the selection from busy threads and that the
2558 * task needs to have done some actual work before we bother with
2559 * NUMA placement.
2560 */
2561 now = curr->se.sum_exec_runtime;
2562 period = (u64)curr->numa_scan_period * NSEC_PER_MSEC;
2563
Rik van Riel25b3e5a2015-11-05 15:56:22 -05002564 if (now > curr->node_stamp + period) {
Peter Zijlstra4b96a292012-10-25 14:16:47 +02002565 if (!curr->node_stamp)
Mel Gorman598f0ec2013-10-07 11:28:55 +01002566 curr->numa_scan_period = task_scan_min(curr);
Peter Zijlstra19a78d12013-10-07 11:28:51 +01002567 curr->node_stamp += period;
Peter Zijlstracbee9f82012-10-25 14:16:43 +02002568
2569 if (!time_before(jiffies, curr->mm->numa_next_scan)) {
2570 init_task_work(work, task_numa_work); /* TODO: move this into sched_fork() */
2571 task_work_add(curr, work, true);
2572 }
2573 }
2574}
2575#else
2576static void task_tick_numa(struct rq *rq, struct task_struct *curr)
2577{
2578}
Peter Zijlstra0ec8aa02013-10-07 11:29:33 +01002579
2580static inline void account_numa_enqueue(struct rq *rq, struct task_struct *p)
2581{
2582}
2583
2584static inline void account_numa_dequeue(struct rq *rq, struct task_struct *p)
2585{
2586}
Peter Zijlstracbee9f82012-10-25 14:16:43 +02002587#endif /* CONFIG_NUMA_BALANCING */
2588
Dmitry Adamushko30cfdcf2007-10-15 17:00:07 +02002589static void
2590account_entity_enqueue(struct cfs_rq *cfs_rq, struct sched_entity *se)
2591{
2592 update_load_add(&cfs_rq->load, se->load.weight);
Peter Zijlstrac09595f2008-06-27 13:41:14 +02002593 if (!parent_entity(se))
Peter Zijlstra029632f2011-10-25 10:00:11 +02002594 update_load_add(&rq_of(cfs_rq)->load, se->load.weight);
Peter Zijlstra367456c2012-02-20 21:49:09 +01002595#ifdef CONFIG_SMP
Peter Zijlstra0ec8aa02013-10-07 11:29:33 +01002596 if (entity_is_task(se)) {
2597 struct rq *rq = rq_of(cfs_rq);
2598
2599 account_numa_enqueue(rq, task_of(se));
2600 list_add(&se->group_node, &rq->cfs_tasks);
2601 }
Peter Zijlstra367456c2012-02-20 21:49:09 +01002602#endif
Dmitry Adamushko30cfdcf2007-10-15 17:00:07 +02002603 cfs_rq->nr_running++;
Dmitry Adamushko30cfdcf2007-10-15 17:00:07 +02002604}
2605
2606static void
2607account_entity_dequeue(struct cfs_rq *cfs_rq, struct sched_entity *se)
2608{
2609 update_load_sub(&cfs_rq->load, se->load.weight);
Peter Zijlstrac09595f2008-06-27 13:41:14 +02002610 if (!parent_entity(se))
Peter Zijlstra029632f2011-10-25 10:00:11 +02002611 update_load_sub(&rq_of(cfs_rq)->load, se->load.weight);
Tim Chenbfdb1982016-02-01 14:47:59 -08002612#ifdef CONFIG_SMP
Peter Zijlstra0ec8aa02013-10-07 11:29:33 +01002613 if (entity_is_task(se)) {
2614 account_numa_dequeue(rq_of(cfs_rq), task_of(se));
Bharata B Raob87f1722008-09-25 09:53:54 +05302615 list_del_init(&se->group_node);
Peter Zijlstra0ec8aa02013-10-07 11:29:33 +01002616 }
Tim Chenbfdb1982016-02-01 14:47:59 -08002617#endif
Dmitry Adamushko30cfdcf2007-10-15 17:00:07 +02002618 cfs_rq->nr_running--;
Dmitry Adamushko30cfdcf2007-10-15 17:00:07 +02002619}
2620
Yong Zhang3ff6dca2011-01-24 15:33:52 +08002621#ifdef CONFIG_FAIR_GROUP_SCHED
2622# ifdef CONFIG_SMP
Paul Turner6d5ab292011-01-21 20:45:01 -08002623static long calc_cfs_shares(struct cfs_rq *cfs_rq, struct task_group *tg)
Yong Zhang3ff6dca2011-01-24 15:33:52 +08002624{
Peter Zijlstracf5f0ac2011-10-13 16:52:28 +02002625 long tg_weight, load, shares;
Yong Zhang3ff6dca2011-01-24 15:33:52 +08002626
Peter Zijlstraea1dc6f2016-06-24 16:11:02 +02002627 /*
2628 * This really should be: cfs_rq->avg.load_avg, but instead we use
2629 * cfs_rq->load.weight, which is its upper bound. This helps ramp up
2630 * the shares for small weight interactive tasks.
2631 */
2632 load = scale_load_down(cfs_rq->load.weight);
2633
2634 tg_weight = atomic_long_read(&tg->load_avg);
2635
2636 /* Ensure tg_weight >= load */
2637 tg_weight -= cfs_rq->tg_load_avg_contrib;
2638 tg_weight += load;
Yong Zhang3ff6dca2011-01-24 15:33:52 +08002639
Yong Zhang3ff6dca2011-01-24 15:33:52 +08002640 shares = (tg->shares * load);
Peter Zijlstracf5f0ac2011-10-13 16:52:28 +02002641 if (tg_weight)
2642 shares /= tg_weight;
Yong Zhang3ff6dca2011-01-24 15:33:52 +08002643
2644 if (shares < MIN_SHARES)
2645 shares = MIN_SHARES;
2646 if (shares > tg->shares)
2647 shares = tg->shares;
2648
2649 return shares;
2650}
Yong Zhang3ff6dca2011-01-24 15:33:52 +08002651# else /* CONFIG_SMP */
Paul Turner6d5ab292011-01-21 20:45:01 -08002652static inline long calc_cfs_shares(struct cfs_rq *cfs_rq, struct task_group *tg)
Yong Zhang3ff6dca2011-01-24 15:33:52 +08002653{
2654 return tg->shares;
2655}
Yong Zhang3ff6dca2011-01-24 15:33:52 +08002656# endif /* CONFIG_SMP */
Peter Zijlstraea1dc6f2016-06-24 16:11:02 +02002657
Peter Zijlstra2069dd72010-11-15 15:47:00 -08002658static void reweight_entity(struct cfs_rq *cfs_rq, struct sched_entity *se,
2659 unsigned long weight)
2660{
Paul Turner19e5eeb2010-12-15 19:10:18 -08002661 if (se->on_rq) {
2662 /* commit outstanding execution time */
2663 if (cfs_rq->curr == se)
2664 update_curr(cfs_rq);
Peter Zijlstra2069dd72010-11-15 15:47:00 -08002665 account_entity_dequeue(cfs_rq, se);
Paul Turner19e5eeb2010-12-15 19:10:18 -08002666 }
Peter Zijlstra2069dd72010-11-15 15:47:00 -08002667
2668 update_load_set(&se->load, weight);
2669
2670 if (se->on_rq)
2671 account_entity_enqueue(cfs_rq, se);
2672}
2673
Paul Turner82958362012-10-04 13:18:31 +02002674static inline int throttled_hierarchy(struct cfs_rq *cfs_rq);
2675
Paul Turner6d5ab292011-01-21 20:45:01 -08002676static void update_cfs_shares(struct cfs_rq *cfs_rq)
Peter Zijlstra2069dd72010-11-15 15:47:00 -08002677{
2678 struct task_group *tg;
2679 struct sched_entity *se;
Yong Zhang3ff6dca2011-01-24 15:33:52 +08002680 long shares;
Peter Zijlstra2069dd72010-11-15 15:47:00 -08002681
Peter Zijlstra2069dd72010-11-15 15:47:00 -08002682 tg = cfs_rq->tg;
2683 se = tg->se[cpu_of(rq_of(cfs_rq))];
Paul Turner64660c82011-07-21 09:43:36 -07002684 if (!se || throttled_hierarchy(cfs_rq))
Peter Zijlstra2069dd72010-11-15 15:47:00 -08002685 return;
Yong Zhang3ff6dca2011-01-24 15:33:52 +08002686#ifndef CONFIG_SMP
2687 if (likely(se->load.weight == tg->shares))
2688 return;
2689#endif
Paul Turner6d5ab292011-01-21 20:45:01 -08002690 shares = calc_cfs_shares(cfs_rq, tg);
Peter Zijlstra2069dd72010-11-15 15:47:00 -08002691
2692 reweight_entity(cfs_rq_of(se), se, shares);
2693}
2694#else /* CONFIG_FAIR_GROUP_SCHED */
Paul Turner6d5ab292011-01-21 20:45:01 -08002695static inline void update_cfs_shares(struct cfs_rq *cfs_rq)
Peter Zijlstra2069dd72010-11-15 15:47:00 -08002696{
2697}
2698#endif /* CONFIG_FAIR_GROUP_SCHED */
2699
Alex Shi141965c2013-06-26 13:05:39 +08002700#ifdef CONFIG_SMP
Paul Turner5b51f2f2012-10-04 13:18:32 +02002701/* Precomputed fixed inverse multiplies for multiplication by y^n */
2702static const u32 runnable_avg_yN_inv[] = {
2703 0xffffffff, 0xfa83b2da, 0xf5257d14, 0xefe4b99a, 0xeac0c6e6, 0xe5b906e6,
2704 0xe0ccdeeb, 0xdbfbb796, 0xd744fcc9, 0xd2a81d91, 0xce248c14, 0xc9b9bd85,
2705 0xc5672a10, 0xc12c4cc9, 0xbd08a39e, 0xb8fbaf46, 0xb504f333, 0xb123f581,
2706 0xad583ee9, 0xa9a15ab4, 0xa5fed6a9, 0xa2704302, 0x9ef5325f, 0x9b8d39b9,
2707 0x9837f050, 0x94f4efa8, 0x91c3d373, 0x8ea4398a, 0x8b95c1e3, 0x88980e80,
2708 0x85aac367, 0x82cd8698,
2709};
2710
2711/*
2712 * Precomputed \Sum y^k { 1<=k<=n }. These are floor(true_value) to prevent
2713 * over-estimates when re-combining.
2714 */
2715static const u32 runnable_avg_yN_sum[] = {
2716 0, 1002, 1982, 2941, 3880, 4798, 5697, 6576, 7437, 8279, 9103,
2717 9909,10698,11470,12226,12966,13690,14398,15091,15769,16433,17082,
2718 17718,18340,18949,19545,20128,20698,21256,21802,22336,22859,23371,
2719};
2720
2721/*
Yuyang Du7b20b912016-05-03 05:54:27 +08002722 * Precomputed \Sum y^k { 1<=k<=n, where n%32=0). Values are rolled down to
2723 * lower integers. See Documentation/scheduler/sched-avg.txt how these
2724 * were generated:
2725 */
2726static const u32 __accumulated_sum_N32[] = {
2727 0, 23371, 35056, 40899, 43820, 45281,
2728 46011, 46376, 46559, 46650, 46696, 46719,
2729};
2730
2731/*
Paul Turner9d85f212012-10-04 13:18:29 +02002732 * Approximate:
2733 * val * y^n, where y^32 ~= 0.5 (~1 scheduling period)
2734 */
2735static __always_inline u64 decay_load(u64 val, u64 n)
2736{
Paul Turner5b51f2f2012-10-04 13:18:32 +02002737 unsigned int local_n;
2738
2739 if (!n)
2740 return val;
2741 else if (unlikely(n > LOAD_AVG_PERIOD * 63))
2742 return 0;
2743
2744 /* after bounds checking we can collapse to 32-bit */
2745 local_n = n;
2746
2747 /*
2748 * As y^PERIOD = 1/2, we can combine
Zhihui Zhang9c58c792014-09-20 21:24:36 -04002749 * y^n = 1/2^(n/PERIOD) * y^(n%PERIOD)
2750 * With a look-up table which covers y^n (n<PERIOD)
Paul Turner5b51f2f2012-10-04 13:18:32 +02002751 *
2752 * To achieve constant time decay_load.
2753 */
2754 if (unlikely(local_n >= LOAD_AVG_PERIOD)) {
2755 val >>= local_n / LOAD_AVG_PERIOD;
2756 local_n %= LOAD_AVG_PERIOD;
Paul Turner9d85f212012-10-04 13:18:29 +02002757 }
2758
Yuyang Du9d89c252015-07-15 08:04:37 +08002759 val = mul_u64_u32_shr(val, runnable_avg_yN_inv[local_n], 32);
2760 return val;
Paul Turner5b51f2f2012-10-04 13:18:32 +02002761}
2762
2763/*
2764 * For updates fully spanning n periods, the contribution to runnable
2765 * average will be: \Sum 1024*y^n
2766 *
2767 * We can compute this reasonably efficiently by combining:
2768 * y^PERIOD = 1/2 with precomputed \Sum 1024*y^n {for n <PERIOD}
2769 */
2770static u32 __compute_runnable_contrib(u64 n)
2771{
2772 u32 contrib = 0;
2773
2774 if (likely(n <= LOAD_AVG_PERIOD))
2775 return runnable_avg_yN_sum[n];
2776 else if (unlikely(n >= LOAD_AVG_MAX_N))
2777 return LOAD_AVG_MAX;
2778
Yuyang Du7b20b912016-05-03 05:54:27 +08002779 /* Since n < LOAD_AVG_MAX_N, n/LOAD_AVG_PERIOD < 11 */
2780 contrib = __accumulated_sum_N32[n/LOAD_AVG_PERIOD];
2781 n %= LOAD_AVG_PERIOD;
Paul Turner5b51f2f2012-10-04 13:18:32 +02002782 contrib = decay_load(contrib, n);
2783 return contrib + runnable_avg_yN_sum[n];
Paul Turner9d85f212012-10-04 13:18:29 +02002784}
2785
Peter Zijlstra54a21382015-09-07 15:05:42 +02002786#define cap_scale(v, s) ((v)*(s) >> SCHED_CAPACITY_SHIFT)
Dietmar Eggemanne0f5f3a2015-08-14 17:23:09 +01002787
Paul Turner9d85f212012-10-04 13:18:29 +02002788/*
2789 * We can represent the historical contribution to runnable average as the
2790 * coefficients of a geometric series. To do this we sub-divide our runnable
2791 * history into segments of approximately 1ms (1024us); label the segment that
2792 * occurred N-ms ago p_N, with p_0 corresponding to the current period, e.g.
2793 *
2794 * [<- 1024us ->|<- 1024us ->|<- 1024us ->| ...
2795 * p0 p1 p2
2796 * (now) (~1ms ago) (~2ms ago)
2797 *
2798 * Let u_i denote the fraction of p_i that the entity was runnable.
2799 *
2800 * We then designate the fractions u_i as our co-efficients, yielding the
2801 * following representation of historical load:
2802 * u_0 + u_1*y + u_2*y^2 + u_3*y^3 + ...
2803 *
2804 * We choose y based on the with of a reasonably scheduling period, fixing:
2805 * y^32 = 0.5
2806 *
2807 * This means that the contribution to load ~32ms ago (u_32) will be weighted
2808 * approximately half as much as the contribution to load within the last ms
2809 * (u_0).
2810 *
2811 * When a period "rolls over" and we have new u_0`, multiplying the previous
2812 * sum again by y is sufficient to update:
2813 * load_avg = u_0` + y*(u_0 + u_1*y + u_2*y^2 + ... )
2814 * = u_0 + u_1*y + u_2*y^2 + ... [re-labeling u_i --> u_{i+1}]
2815 */
Yuyang Du9d89c252015-07-15 08:04:37 +08002816static __always_inline int
2817__update_load_avg(u64 now, int cpu, struct sched_avg *sa,
Yuyang Du13962232015-07-15 08:04:41 +08002818 unsigned long weight, int running, struct cfs_rq *cfs_rq)
Paul Turner9d85f212012-10-04 13:18:29 +02002819{
Dietmar Eggemanne0f5f3a2015-08-14 17:23:09 +01002820 u64 delta, scaled_delta, periods;
Yuyang Du9d89c252015-07-15 08:04:37 +08002821 u32 contrib;
Peter Zijlstra6115c792015-09-07 15:09:15 +02002822 unsigned int delta_w, scaled_delta_w, decayed = 0;
Dietmar Eggemann6f2b0452015-09-07 14:57:22 +01002823 unsigned long scale_freq, scale_cpu;
Paul Turner9d85f212012-10-04 13:18:29 +02002824
Yuyang Du9d89c252015-07-15 08:04:37 +08002825 delta = now - sa->last_update_time;
Paul Turner9d85f212012-10-04 13:18:29 +02002826 /*
2827 * This should only happen when time goes backwards, which it
2828 * unfortunately does during sched clock init when we swap over to TSC.
2829 */
2830 if ((s64)delta < 0) {
Yuyang Du9d89c252015-07-15 08:04:37 +08002831 sa->last_update_time = now;
Paul Turner9d85f212012-10-04 13:18:29 +02002832 return 0;
2833 }
2834
2835 /*
2836 * Use 1024ns as the unit of measurement since it's a reasonable
2837 * approximation of 1us and fast to compute.
2838 */
2839 delta >>= 10;
2840 if (!delta)
2841 return 0;
Yuyang Du9d89c252015-07-15 08:04:37 +08002842 sa->last_update_time = now;
Paul Turner9d85f212012-10-04 13:18:29 +02002843
Dietmar Eggemann6f2b0452015-09-07 14:57:22 +01002844 scale_freq = arch_scale_freq_capacity(NULL, cpu);
2845 scale_cpu = arch_scale_cpu_capacity(NULL, cpu);
2846
Paul Turner9d85f212012-10-04 13:18:29 +02002847 /* delta_w is the amount already accumulated against our next period */
Yuyang Du9d89c252015-07-15 08:04:37 +08002848 delta_w = sa->period_contrib;
Paul Turner9d85f212012-10-04 13:18:29 +02002849 if (delta + delta_w >= 1024) {
Paul Turner9d85f212012-10-04 13:18:29 +02002850 decayed = 1;
2851
Yuyang Du9d89c252015-07-15 08:04:37 +08002852 /* how much left for next period will start over, we don't know yet */
2853 sa->period_contrib = 0;
2854
Paul Turner9d85f212012-10-04 13:18:29 +02002855 /*
2856 * Now that we know we're crossing a period boundary, figure
2857 * out how much from delta we need to complete the current
2858 * period and accrue it.
2859 */
2860 delta_w = 1024 - delta_w;
Peter Zijlstra54a21382015-09-07 15:05:42 +02002861 scaled_delta_w = cap_scale(delta_w, scale_freq);
Yuyang Du13962232015-07-15 08:04:41 +08002862 if (weight) {
Dietmar Eggemanne0f5f3a2015-08-14 17:23:09 +01002863 sa->load_sum += weight * scaled_delta_w;
2864 if (cfs_rq) {
2865 cfs_rq->runnable_load_sum +=
2866 weight * scaled_delta_w;
2867 }
Yuyang Du13962232015-07-15 08:04:41 +08002868 }
Vincent Guittot36ee28e2015-02-27 16:54:04 +01002869 if (running)
Peter Zijlstra006cdf02015-09-09 09:06:17 +02002870 sa->util_sum += scaled_delta_w * scale_cpu;
Paul Turner9d85f212012-10-04 13:18:29 +02002871
Paul Turner5b51f2f2012-10-04 13:18:32 +02002872 delta -= delta_w;
Paul Turner9d85f212012-10-04 13:18:29 +02002873
Paul Turner5b51f2f2012-10-04 13:18:32 +02002874 /* Figure out how many additional periods this update spans */
2875 periods = delta / 1024;
2876 delta %= 1024;
2877
Yuyang Du9d89c252015-07-15 08:04:37 +08002878 sa->load_sum = decay_load(sa->load_sum, periods + 1);
Yuyang Du13962232015-07-15 08:04:41 +08002879 if (cfs_rq) {
2880 cfs_rq->runnable_load_sum =
2881 decay_load(cfs_rq->runnable_load_sum, periods + 1);
2882 }
Yuyang Du9d89c252015-07-15 08:04:37 +08002883 sa->util_sum = decay_load((u64)(sa->util_sum), periods + 1);
Paul Turner5b51f2f2012-10-04 13:18:32 +02002884
2885 /* Efficiently calculate \sum (1..n_period) 1024*y^i */
Yuyang Du9d89c252015-07-15 08:04:37 +08002886 contrib = __compute_runnable_contrib(periods);
Peter Zijlstra54a21382015-09-07 15:05:42 +02002887 contrib = cap_scale(contrib, scale_freq);
Yuyang Du13962232015-07-15 08:04:41 +08002888 if (weight) {
Yuyang Du9d89c252015-07-15 08:04:37 +08002889 sa->load_sum += weight * contrib;
Yuyang Du13962232015-07-15 08:04:41 +08002890 if (cfs_rq)
2891 cfs_rq->runnable_load_sum += weight * contrib;
2892 }
Vincent Guittot36ee28e2015-02-27 16:54:04 +01002893 if (running)
Peter Zijlstra006cdf02015-09-09 09:06:17 +02002894 sa->util_sum += contrib * scale_cpu;
Paul Turner9d85f212012-10-04 13:18:29 +02002895 }
2896
2897 /* Remainder of delta accrued against u_0` */
Peter Zijlstra54a21382015-09-07 15:05:42 +02002898 scaled_delta = cap_scale(delta, scale_freq);
Yuyang Du13962232015-07-15 08:04:41 +08002899 if (weight) {
Dietmar Eggemanne0f5f3a2015-08-14 17:23:09 +01002900 sa->load_sum += weight * scaled_delta;
Yuyang Du13962232015-07-15 08:04:41 +08002901 if (cfs_rq)
Dietmar Eggemanne0f5f3a2015-08-14 17:23:09 +01002902 cfs_rq->runnable_load_sum += weight * scaled_delta;
Yuyang Du13962232015-07-15 08:04:41 +08002903 }
Vincent Guittot36ee28e2015-02-27 16:54:04 +01002904 if (running)
Peter Zijlstra006cdf02015-09-09 09:06:17 +02002905 sa->util_sum += scaled_delta * scale_cpu;
Yuyang Du9d89c252015-07-15 08:04:37 +08002906
2907 sa->period_contrib += delta;
2908
2909 if (decayed) {
2910 sa->load_avg = div_u64(sa->load_sum, LOAD_AVG_MAX);
Yuyang Du13962232015-07-15 08:04:41 +08002911 if (cfs_rq) {
2912 cfs_rq->runnable_load_avg =
2913 div_u64(cfs_rq->runnable_load_sum, LOAD_AVG_MAX);
2914 }
Peter Zijlstra006cdf02015-09-09 09:06:17 +02002915 sa->util_avg = sa->util_sum / LOAD_AVG_MAX;
Yuyang Du9d89c252015-07-15 08:04:37 +08002916 }
Paul Turner9d85f212012-10-04 13:18:29 +02002917
2918 return decayed;
2919}
2920
Vincent Guittot09a43ac2016-11-08 10:53:45 +01002921/*
2922 * Signed add and clamp on underflow.
2923 *
2924 * Explicitly do a load-store to ensure the intermediate value never hits
2925 * memory. This allows lockless observations without ever seeing the negative
2926 * values.
2927 */
2928#define add_positive(_ptr, _val) do { \
2929 typeof(_ptr) ptr = (_ptr); \
2930 typeof(_val) val = (_val); \
2931 typeof(*ptr) res, var = READ_ONCE(*ptr); \
2932 \
2933 res = var + val; \
2934 \
2935 if (val < 0 && res > var) \
2936 res = 0; \
2937 \
2938 WRITE_ONCE(*ptr, res); \
2939} while (0)
2940
Paul Turnerc566e8e2012-10-04 13:18:30 +02002941#ifdef CONFIG_FAIR_GROUP_SCHED
Peter Zijlstra7c3edd22016-07-13 10:56:25 +02002942/**
2943 * update_tg_load_avg - update the tg's load avg
2944 * @cfs_rq: the cfs_rq whose avg changed
2945 * @force: update regardless of how small the difference
2946 *
2947 * This function 'ensures': tg->load_avg := \Sum tg->cfs_rq[]->avg.load.
2948 * However, because tg->load_avg is a global value there are performance
2949 * considerations.
2950 *
2951 * In order to avoid having to look at the other cfs_rq's, we use a
2952 * differential update where we store the last value we propagated. This in
2953 * turn allows skipping updates if the differential is 'small'.
2954 *
2955 * Updating tg's load_avg is necessary before update_cfs_share() (which is
2956 * done) and effective_load() (which is not done because it is too costly).
Paul Turnerbb17f652012-10-04 13:18:31 +02002957 */
Yuyang Du9d89c252015-07-15 08:04:37 +08002958static inline void update_tg_load_avg(struct cfs_rq *cfs_rq, int force)
Paul Turnerbb17f652012-10-04 13:18:31 +02002959{
Yuyang Du9d89c252015-07-15 08:04:37 +08002960 long delta = cfs_rq->avg.load_avg - cfs_rq->tg_load_avg_contrib;
Paul Turnerbb17f652012-10-04 13:18:31 +02002961
Waiman Longaa0b7ae2015-12-02 13:41:50 -05002962 /*
2963 * No need to update load_avg for root_task_group as it is not used.
2964 */
2965 if (cfs_rq->tg == &root_task_group)
2966 return;
2967
Yuyang Du9d89c252015-07-15 08:04:37 +08002968 if (force || abs(delta) > cfs_rq->tg_load_avg_contrib / 64) {
2969 atomic_long_add(delta, &cfs_rq->tg->load_avg);
2970 cfs_rq->tg_load_avg_contrib = cfs_rq->avg.load_avg;
Paul Turnerbb17f652012-10-04 13:18:31 +02002971 }
Paul Turner8165e142012-10-04 13:18:31 +02002972}
Dietmar Eggemannf5f97392014-02-26 11:19:33 +00002973
Byungchul Parkad936d82015-10-24 01:16:19 +09002974/*
2975 * Called within set_task_rq() right before setting a task's cpu. The
2976 * caller only guarantees p->pi_lock is held; no other assumptions,
2977 * including the state of rq->lock, should be made.
2978 */
2979void set_task_rq_fair(struct sched_entity *se,
2980 struct cfs_rq *prev, struct cfs_rq *next)
2981{
2982 if (!sched_feat(ATTACH_AGE_LOAD))
2983 return;
2984
2985 /*
2986 * We are supposed to update the task to "current" time, then its up to
2987 * date and ready to go to new CPU/cfs_rq. But we have difficulty in
2988 * getting what current time is, so simply throw away the out-of-date
2989 * time. This will result in the wakee task is less decayed, but giving
2990 * the wakee more load sounds not bad.
2991 */
2992 if (se->avg.last_update_time && prev) {
2993 u64 p_last_update_time;
2994 u64 n_last_update_time;
2995
2996#ifndef CONFIG_64BIT
2997 u64 p_last_update_time_copy;
2998 u64 n_last_update_time_copy;
2999
3000 do {
3001 p_last_update_time_copy = prev->load_last_update_time_copy;
3002 n_last_update_time_copy = next->load_last_update_time_copy;
3003
3004 smp_rmb();
3005
3006 p_last_update_time = prev->avg.last_update_time;
3007 n_last_update_time = next->avg.last_update_time;
3008
3009 } while (p_last_update_time != p_last_update_time_copy ||
3010 n_last_update_time != n_last_update_time_copy);
3011#else
3012 p_last_update_time = prev->avg.last_update_time;
3013 n_last_update_time = next->avg.last_update_time;
3014#endif
3015 __update_load_avg(p_last_update_time, cpu_of(rq_of(prev)),
3016 &se->avg, 0, 0, NULL);
3017 se->avg.last_update_time = n_last_update_time;
3018 }
3019}
Vincent Guittot09a43ac2016-11-08 10:53:45 +01003020
3021/* Take into account change of utilization of a child task group */
3022static inline void
3023update_tg_cfs_util(struct cfs_rq *cfs_rq, struct sched_entity *se)
3024{
3025 struct cfs_rq *gcfs_rq = group_cfs_rq(se);
3026 long delta = gcfs_rq->avg.util_avg - se->avg.util_avg;
3027
3028 /* Nothing to update */
3029 if (!delta)
3030 return;
3031
3032 /* Set new sched_entity's utilization */
3033 se->avg.util_avg = gcfs_rq->avg.util_avg;
3034 se->avg.util_sum = se->avg.util_avg * LOAD_AVG_MAX;
3035
3036 /* Update parent cfs_rq utilization */
3037 add_positive(&cfs_rq->avg.util_avg, delta);
3038 cfs_rq->avg.util_sum = cfs_rq->avg.util_avg * LOAD_AVG_MAX;
3039}
3040
3041/* Take into account change of load of a child task group */
3042static inline void
3043update_tg_cfs_load(struct cfs_rq *cfs_rq, struct sched_entity *se)
3044{
3045 struct cfs_rq *gcfs_rq = group_cfs_rq(se);
3046 long delta, load = gcfs_rq->avg.load_avg;
3047
3048 /*
3049 * If the load of group cfs_rq is null, the load of the
3050 * sched_entity will also be null so we can skip the formula
3051 */
3052 if (load) {
3053 long tg_load;
3054
3055 /* Get tg's load and ensure tg_load > 0 */
3056 tg_load = atomic_long_read(&gcfs_rq->tg->load_avg) + 1;
3057
3058 /* Ensure tg_load >= load and updated with current load*/
3059 tg_load -= gcfs_rq->tg_load_avg_contrib;
3060 tg_load += load;
3061
3062 /*
3063 * We need to compute a correction term in the case that the
3064 * task group is consuming more CPU than a task of equal
3065 * weight. A task with a weight equals to tg->shares will have
3066 * a load less or equal to scale_load_down(tg->shares).
3067 * Similarly, the sched_entities that represent the task group
3068 * at parent level, can't have a load higher than
3069 * scale_load_down(tg->shares). And the Sum of sched_entities'
3070 * load must be <= scale_load_down(tg->shares).
3071 */
3072 if (tg_load > scale_load_down(gcfs_rq->tg->shares)) {
3073 /* scale gcfs_rq's load into tg's shares*/
3074 load *= scale_load_down(gcfs_rq->tg->shares);
3075 load /= tg_load;
3076 }
3077 }
3078
3079 delta = load - se->avg.load_avg;
3080
3081 /* Nothing to update */
3082 if (!delta)
3083 return;
3084
3085 /* Set new sched_entity's load */
3086 se->avg.load_avg = load;
3087 se->avg.load_sum = se->avg.load_avg * LOAD_AVG_MAX;
3088
3089 /* Update parent cfs_rq load */
3090 add_positive(&cfs_rq->avg.load_avg, delta);
3091 cfs_rq->avg.load_sum = cfs_rq->avg.load_avg * LOAD_AVG_MAX;
3092
3093 /*
3094 * If the sched_entity is already enqueued, we also have to update the
3095 * runnable load avg.
3096 */
3097 if (se->on_rq) {
3098 /* Update parent cfs_rq runnable_load_avg */
3099 add_positive(&cfs_rq->runnable_load_avg, delta);
3100 cfs_rq->runnable_load_sum = cfs_rq->runnable_load_avg * LOAD_AVG_MAX;
3101 }
3102}
3103
3104static inline void set_tg_cfs_propagate(struct cfs_rq *cfs_rq)
3105{
3106 cfs_rq->propagate_avg = 1;
3107}
3108
3109static inline int test_and_clear_tg_cfs_propagate(struct sched_entity *se)
3110{
3111 struct cfs_rq *cfs_rq = group_cfs_rq(se);
3112
3113 if (!cfs_rq->propagate_avg)
3114 return 0;
3115
3116 cfs_rq->propagate_avg = 0;
3117 return 1;
3118}
3119
3120/* Update task and its cfs_rq load average */
3121static inline int propagate_entity_load_avg(struct sched_entity *se)
3122{
3123 struct cfs_rq *cfs_rq;
3124
3125 if (entity_is_task(se))
3126 return 0;
3127
3128 if (!test_and_clear_tg_cfs_propagate(se))
3129 return 0;
3130
3131 cfs_rq = cfs_rq_of(se);
3132
3133 set_tg_cfs_propagate(cfs_rq);
3134
3135 update_tg_cfs_util(cfs_rq, se);
3136 update_tg_cfs_load(cfs_rq, se);
3137
3138 return 1;
3139}
3140
Peter Zijlstra6e831252014-02-11 16:11:48 +01003141#else /* CONFIG_FAIR_GROUP_SCHED */
Vincent Guittot09a43ac2016-11-08 10:53:45 +01003142
Yuyang Du9d89c252015-07-15 08:04:37 +08003143static inline void update_tg_load_avg(struct cfs_rq *cfs_rq, int force) {}
Vincent Guittot09a43ac2016-11-08 10:53:45 +01003144
3145static inline int propagate_entity_load_avg(struct sched_entity *se)
3146{
3147 return 0;
3148}
3149
3150static inline void set_tg_cfs_propagate(struct cfs_rq *cfs_rq) {}
3151
Peter Zijlstra6e831252014-02-11 16:11:48 +01003152#endif /* CONFIG_FAIR_GROUP_SCHED */
Paul Turnerc566e8e2012-10-04 13:18:30 +02003153
Steve Mucklea2c6c912016-03-24 15:26:07 -07003154static inline void cfs_rq_util_change(struct cfs_rq *cfs_rq)
Yuyang Du9d89c252015-07-15 08:04:37 +08003155{
Rafael J. Wysocki58919e82016-08-16 22:14:55 +02003156 if (&this_rq()->cfs == cfs_rq) {
Steve Muckle21e96f82016-03-21 17:21:07 -07003157 /*
3158 * There are a few boundary cases this might miss but it should
3159 * get called often enough that that should (hopefully) not be
3160 * a real problem -- added to that it only calls on the local
3161 * CPU, so if we enqueue remotely we'll miss an update, but
3162 * the next tick/schedule should update.
3163 *
3164 * It will not get called when we go idle, because the idle
3165 * thread is a different class (!fair), nor will the utilization
3166 * number include things like RT tasks.
3167 *
3168 * As is, the util number is not freq-invariant (we'd have to
3169 * implement arch_scale_freq_capacity() for that).
3170 *
3171 * See cpu_util().
3172 */
Rafael J. Wysocki12bde332016-08-10 03:11:17 +02003173 cpufreq_update_util(rq_of(cfs_rq), 0);
Steve Muckle21e96f82016-03-21 17:21:07 -07003174 }
Steve Mucklea2c6c912016-03-24 15:26:07 -07003175}
3176
Peter Zijlstra89741892016-06-16 10:50:40 +02003177/*
3178 * Unsigned subtract and clamp on underflow.
3179 *
3180 * Explicitly do a load-store to ensure the intermediate value never hits
3181 * memory. This allows lockless observations without ever seeing the negative
3182 * values.
3183 */
3184#define sub_positive(_ptr, _val) do { \
3185 typeof(_ptr) ptr = (_ptr); \
3186 typeof(*ptr) val = (_val); \
3187 typeof(*ptr) res, var = READ_ONCE(*ptr); \
3188 res = var - val; \
3189 if (res > var) \
3190 res = 0; \
3191 WRITE_ONCE(*ptr, res); \
3192} while (0)
3193
Peter Zijlstra3d30544f2016-06-21 14:27:50 +02003194/**
3195 * update_cfs_rq_load_avg - update the cfs_rq's load/util averages
3196 * @now: current time, as per cfs_rq_clock_task()
3197 * @cfs_rq: cfs_rq to update
3198 * @update_freq: should we call cfs_rq_util_change() or will the call do so
3199 *
3200 * The cfs_rq avg is the direct sum of all its entities (blocked and runnable)
3201 * avg. The immediate corollary is that all (fair) tasks must be attached, see
3202 * post_init_entity_util_avg().
3203 *
3204 * cfs_rq->avg is used for task_h_load() and update_cfs_share() for example.
3205 *
Peter Zijlstra7c3edd22016-07-13 10:56:25 +02003206 * Returns true if the load decayed or we removed load.
3207 *
3208 * Since both these conditions indicate a changed cfs_rq->avg.load we should
3209 * call update_tg_load_avg() when this function returns true.
Peter Zijlstra3d30544f2016-06-21 14:27:50 +02003210 */
Steve Mucklea2c6c912016-03-24 15:26:07 -07003211static inline int
3212update_cfs_rq_load_avg(u64 now, struct cfs_rq *cfs_rq, bool update_freq)
3213{
3214 struct sched_avg *sa = &cfs_rq->avg;
3215 int decayed, removed_load = 0, removed_util = 0;
3216
3217 if (atomic_long_read(&cfs_rq->removed_load_avg)) {
3218 s64 r = atomic_long_xchg(&cfs_rq->removed_load_avg, 0);
Peter Zijlstra89741892016-06-16 10:50:40 +02003219 sub_positive(&sa->load_avg, r);
3220 sub_positive(&sa->load_sum, r * LOAD_AVG_MAX);
Steve Mucklea2c6c912016-03-24 15:26:07 -07003221 removed_load = 1;
3222 }
3223
3224 if (atomic_long_read(&cfs_rq->removed_util_avg)) {
3225 long r = atomic_long_xchg(&cfs_rq->removed_util_avg, 0);
Peter Zijlstra89741892016-06-16 10:50:40 +02003226 sub_positive(&sa->util_avg, r);
3227 sub_positive(&sa->util_sum, r * LOAD_AVG_MAX);
Steve Mucklea2c6c912016-03-24 15:26:07 -07003228 removed_util = 1;
3229 }
3230
3231 decayed = __update_load_avg(now, cpu_of(rq_of(cfs_rq)), sa,
3232 scale_load_down(cfs_rq->load.weight), cfs_rq->curr != NULL, cfs_rq);
3233
3234#ifndef CONFIG_64BIT
3235 smp_wmb();
3236 cfs_rq->load_last_update_time_copy = sa->last_update_time;
3237#endif
3238
3239 if (update_freq && (decayed || removed_util))
3240 cfs_rq_util_change(cfs_rq);
Steve Muckle21e96f82016-03-21 17:21:07 -07003241
Steve Muckle41e0d372016-03-21 17:21:08 -07003242 return decayed || removed_load;
Yuyang Du9d89c252015-07-15 08:04:37 +08003243}
3244
Vincent Guittotd31b1a62016-11-08 10:53:44 +01003245/*
3246 * Optional action to be done while updating the load average
3247 */
3248#define UPDATE_TG 0x1
3249#define SKIP_AGE_LOAD 0x2
3250
Yuyang Du9d89c252015-07-15 08:04:37 +08003251/* Update task and its cfs_rq load average */
Vincent Guittotd31b1a62016-11-08 10:53:44 +01003252static inline void update_load_avg(struct sched_entity *se, int flags)
Paul Turner9d85f212012-10-04 13:18:29 +02003253{
Paul Turner2dac7542012-10-04 13:18:30 +02003254 struct cfs_rq *cfs_rq = cfs_rq_of(se);
Yuyang Du9d89c252015-07-15 08:04:37 +08003255 u64 now = cfs_rq_clock_task(cfs_rq);
Rafael J. Wysocki34e2c552016-02-15 20:20:42 +01003256 struct rq *rq = rq_of(cfs_rq);
3257 int cpu = cpu_of(rq);
Vincent Guittot09a43ac2016-11-08 10:53:45 +01003258 int decayed;
Paul Turner2dac7542012-10-04 13:18:30 +02003259
Paul Turnerf1b17282012-10-04 13:18:31 +02003260 /*
Yuyang Du9d89c252015-07-15 08:04:37 +08003261 * Track task load average for carrying it to new CPU after migrated, and
3262 * track group sched_entity load average for task_h_load calc in migration
Paul Turnerf1b17282012-10-04 13:18:31 +02003263 */
Vincent Guittotd31b1a62016-11-08 10:53:44 +01003264 if (se->avg.last_update_time && !(flags & SKIP_AGE_LOAD)) {
3265 __update_load_avg(now, cpu, &se->avg,
Byungchul Parka05e8c52015-08-20 20:21:56 +09003266 se->on_rq * scale_load_down(se->load.weight),
3267 cfs_rq->curr == se, NULL);
Vincent Guittotd31b1a62016-11-08 10:53:44 +01003268 }
Paul Turnerf1b17282012-10-04 13:18:31 +02003269
Vincent Guittot09a43ac2016-11-08 10:53:45 +01003270 decayed = update_cfs_rq_load_avg(now, cfs_rq, true);
3271 decayed |= propagate_entity_load_avg(se);
3272
3273 if (decayed && (flags & UPDATE_TG))
Yuyang Du9d89c252015-07-15 08:04:37 +08003274 update_tg_load_avg(cfs_rq, 0);
3275}
Paul Turner2dac7542012-10-04 13:18:30 +02003276
Peter Zijlstra3d30544f2016-06-21 14:27:50 +02003277/**
3278 * attach_entity_load_avg - attach this entity to its cfs_rq load avg
3279 * @cfs_rq: cfs_rq to attach to
3280 * @se: sched_entity to attach
3281 *
3282 * Must call update_cfs_rq_load_avg() before this, since we rely on
3283 * cfs_rq->avg.last_update_time being current.
3284 */
Byungchul Parka05e8c52015-08-20 20:21:56 +09003285static void attach_entity_load_avg(struct cfs_rq *cfs_rq, struct sched_entity *se)
3286{
3287 se->avg.last_update_time = cfs_rq->avg.last_update_time;
3288 cfs_rq->avg.load_avg += se->avg.load_avg;
3289 cfs_rq->avg.load_sum += se->avg.load_sum;
3290 cfs_rq->avg.util_avg += se->avg.util_avg;
3291 cfs_rq->avg.util_sum += se->avg.util_sum;
Vincent Guittot09a43ac2016-11-08 10:53:45 +01003292 set_tg_cfs_propagate(cfs_rq);
Steve Mucklea2c6c912016-03-24 15:26:07 -07003293
3294 cfs_rq_util_change(cfs_rq);
Byungchul Parka05e8c52015-08-20 20:21:56 +09003295}
3296
Peter Zijlstra3d30544f2016-06-21 14:27:50 +02003297/**
3298 * detach_entity_load_avg - detach this entity from its cfs_rq load avg
3299 * @cfs_rq: cfs_rq to detach from
3300 * @se: sched_entity to detach
3301 *
3302 * Must call update_cfs_rq_load_avg() before this, since we rely on
3303 * cfs_rq->avg.last_update_time being current.
3304 */
Byungchul Parka05e8c52015-08-20 20:21:56 +09003305static void detach_entity_load_avg(struct cfs_rq *cfs_rq, struct sched_entity *se)
3306{
Byungchul Parka05e8c52015-08-20 20:21:56 +09003307
Peter Zijlstra89741892016-06-16 10:50:40 +02003308 sub_positive(&cfs_rq->avg.load_avg, se->avg.load_avg);
3309 sub_positive(&cfs_rq->avg.load_sum, se->avg.load_sum);
3310 sub_positive(&cfs_rq->avg.util_avg, se->avg.util_avg);
3311 sub_positive(&cfs_rq->avg.util_sum, se->avg.util_sum);
Vincent Guittot09a43ac2016-11-08 10:53:45 +01003312 set_tg_cfs_propagate(cfs_rq);
Steve Mucklea2c6c912016-03-24 15:26:07 -07003313
3314 cfs_rq_util_change(cfs_rq);
Byungchul Parka05e8c52015-08-20 20:21:56 +09003315}
3316
Yuyang Du9d89c252015-07-15 08:04:37 +08003317/* Add the load generated by se into cfs_rq's load average */
3318static inline void
3319enqueue_entity_load_avg(struct cfs_rq *cfs_rq, struct sched_entity *se)
3320{
3321 struct sched_avg *sa = &se->avg;
Yuyang Du9d89c252015-07-15 08:04:37 +08003322
Yuyang Du13962232015-07-15 08:04:41 +08003323 cfs_rq->runnable_load_avg += sa->load_avg;
3324 cfs_rq->runnable_load_sum += sa->load_sum;
3325
Vincent Guittotd31b1a62016-11-08 10:53:44 +01003326 if (!sa->last_update_time) {
Byungchul Parka05e8c52015-08-20 20:21:56 +09003327 attach_entity_load_avg(cfs_rq, se);
Yuyang Du9d89c252015-07-15 08:04:37 +08003328 update_tg_load_avg(cfs_rq, 0);
Vincent Guittotd31b1a62016-11-08 10:53:44 +01003329 }
Paul Turner9ee474f2012-10-04 13:18:30 +02003330}
3331
Yuyang Du13962232015-07-15 08:04:41 +08003332/* Remove the runnable load generated by se from cfs_rq's runnable load average */
3333static inline void
3334dequeue_entity_load_avg(struct cfs_rq *cfs_rq, struct sched_entity *se)
3335{
Yuyang Du13962232015-07-15 08:04:41 +08003336 cfs_rq->runnable_load_avg =
3337 max_t(long, cfs_rq->runnable_load_avg - se->avg.load_avg, 0);
3338 cfs_rq->runnable_load_sum =
Byungchul Parka05e8c52015-08-20 20:21:56 +09003339 max_t(s64, cfs_rq->runnable_load_sum - se->avg.load_sum, 0);
Yuyang Du13962232015-07-15 08:04:41 +08003340}
3341
Yuyang Du0905f042015-12-17 07:34:27 +08003342#ifndef CONFIG_64BIT
3343static inline u64 cfs_rq_last_update_time(struct cfs_rq *cfs_rq)
3344{
3345 u64 last_update_time_copy;
3346 u64 last_update_time;
3347
3348 do {
3349 last_update_time_copy = cfs_rq->load_last_update_time_copy;
3350 smp_rmb();
3351 last_update_time = cfs_rq->avg.last_update_time;
3352 } while (last_update_time != last_update_time_copy);
3353
3354 return last_update_time;
3355}
3356#else
3357static inline u64 cfs_rq_last_update_time(struct cfs_rq *cfs_rq)
3358{
3359 return cfs_rq->avg.last_update_time;
3360}
3361#endif
3362
Paul Turner9ee474f2012-10-04 13:18:30 +02003363/*
Morten Rasmussen104cb162016-10-14 14:41:07 +01003364 * Synchronize entity load avg of dequeued entity without locking
3365 * the previous rq.
3366 */
3367void sync_entity_load_avg(struct sched_entity *se)
3368{
3369 struct cfs_rq *cfs_rq = cfs_rq_of(se);
3370 u64 last_update_time;
3371
3372 last_update_time = cfs_rq_last_update_time(cfs_rq);
3373 __update_load_avg(last_update_time, cpu_of(rq_of(cfs_rq)), &se->avg, 0, 0, NULL);
3374}
3375
3376/*
Yuyang Du9d89c252015-07-15 08:04:37 +08003377 * Task first catches up with cfs_rq, and then subtract
3378 * itself from the cfs_rq (task must be off the queue now).
Paul Turner9ee474f2012-10-04 13:18:30 +02003379 */
Yuyang Du9d89c252015-07-15 08:04:37 +08003380void remove_entity_load_avg(struct sched_entity *se)
Paul Turner9ee474f2012-10-04 13:18:30 +02003381{
Yuyang Du9d89c252015-07-15 08:04:37 +08003382 struct cfs_rq *cfs_rq = cfs_rq_of(se);
Paul Turner9ee474f2012-10-04 13:18:30 +02003383
Yuyang Du0905f042015-12-17 07:34:27 +08003384 /*
Peter Zijlstra7dc603c2016-06-16 13:29:28 +02003385 * tasks cannot exit without having gone through wake_up_new_task() ->
3386 * post_init_entity_util_avg() which will have added things to the
3387 * cfs_rq, so we can remove unconditionally.
3388 *
3389 * Similarly for groups, they will have passed through
3390 * post_init_entity_util_avg() before unregister_sched_fair_group()
3391 * calls this.
Yuyang Du0905f042015-12-17 07:34:27 +08003392 */
Paul Turner9ee474f2012-10-04 13:18:30 +02003393
Morten Rasmussen104cb162016-10-14 14:41:07 +01003394 sync_entity_load_avg(se);
Yuyang Du9d89c252015-07-15 08:04:37 +08003395 atomic_long_add(se->avg.load_avg, &cfs_rq->removed_load_avg);
3396 atomic_long_add(se->avg.util_avg, &cfs_rq->removed_util_avg);
Paul Turner2dac7542012-10-04 13:18:30 +02003397}
Vincent Guittot642dbc32013-04-18 18:34:26 +02003398
Yuyang Du7ea241a2015-07-15 08:04:42 +08003399static inline unsigned long cfs_rq_runnable_load_avg(struct cfs_rq *cfs_rq)
3400{
3401 return cfs_rq->runnable_load_avg;
3402}
3403
3404static inline unsigned long cfs_rq_load_avg(struct cfs_rq *cfs_rq)
3405{
3406 return cfs_rq->avg.load_avg;
3407}
3408
Peter Zijlstra6e831252014-02-11 16:11:48 +01003409static int idle_balance(struct rq *this_rq);
3410
Peter Zijlstra38033c32014-01-23 20:32:21 +01003411#else /* CONFIG_SMP */
3412
Peter Zijlstra01011472016-06-17 11:20:46 +02003413static inline int
3414update_cfs_rq_load_avg(u64 now, struct cfs_rq *cfs_rq, bool update_freq)
3415{
3416 return 0;
3417}
3418
Vincent Guittotd31b1a62016-11-08 10:53:44 +01003419#define UPDATE_TG 0x0
3420#define SKIP_AGE_LOAD 0x0
3421
3422static inline void update_load_avg(struct sched_entity *se, int not_used1)
Rafael J. Wysocki536bd002016-05-06 14:58:43 +02003423{
Rafael J. Wysocki12bde332016-08-10 03:11:17 +02003424 cpufreq_update_util(rq_of(cfs_rq_of(se)), 0);
Rafael J. Wysocki536bd002016-05-06 14:58:43 +02003425}
3426
Yuyang Du9d89c252015-07-15 08:04:37 +08003427static inline void
3428enqueue_entity_load_avg(struct cfs_rq *cfs_rq, struct sched_entity *se) {}
Yuyang Du13962232015-07-15 08:04:41 +08003429static inline void
3430dequeue_entity_load_avg(struct cfs_rq *cfs_rq, struct sched_entity *se) {}
Yuyang Du9d89c252015-07-15 08:04:37 +08003431static inline void remove_entity_load_avg(struct sched_entity *se) {}
Peter Zijlstra6e831252014-02-11 16:11:48 +01003432
Byungchul Parka05e8c52015-08-20 20:21:56 +09003433static inline void
3434attach_entity_load_avg(struct cfs_rq *cfs_rq, struct sched_entity *se) {}
3435static inline void
3436detach_entity_load_avg(struct cfs_rq *cfs_rq, struct sched_entity *se) {}
3437
Peter Zijlstra6e831252014-02-11 16:11:48 +01003438static inline int idle_balance(struct rq *rq)
3439{
3440 return 0;
3441}
3442
Peter Zijlstra38033c32014-01-23 20:32:21 +01003443#endif /* CONFIG_SMP */
Paul Turner9d85f212012-10-04 13:18:29 +02003444
Peter Zijlstraddc97292007-10-15 17:00:10 +02003445static void check_spread(struct cfs_rq *cfs_rq, struct sched_entity *se)
3446{
3447#ifdef CONFIG_SCHED_DEBUG
3448 s64 d = se->vruntime - cfs_rq->min_vruntime;
3449
3450 if (d < 0)
3451 d = -d;
3452
3453 if (d > 3*sysctl_sched_latency)
Josh Poimboeufae928822016-06-17 12:43:24 -05003454 schedstat_inc(cfs_rq->nr_spread_over);
Peter Zijlstraddc97292007-10-15 17:00:10 +02003455#endif
3456}
3457
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02003458static void
Peter Zijlstraaeb73b02007-10-15 17:00:05 +02003459place_entity(struct cfs_rq *cfs_rq, struct sched_entity *se, int initial)
3460{
Peter Zijlstra1af5f732008-10-24 11:06:13 +02003461 u64 vruntime = cfs_rq->min_vruntime;
Peter Zijlstra94dfb5e2007-10-15 17:00:05 +02003462
Peter Zijlstra2cb86002007-11-09 22:39:37 +01003463 /*
3464 * The 'current' period is already promised to the current tasks,
3465 * however the extra weight of the new task will slow them down a
3466 * little, place the new task so that it fits in the slot that
3467 * stays open at the end.
3468 */
Peter Zijlstra94dfb5e2007-10-15 17:00:05 +02003469 if (initial && sched_feat(START_DEBIT))
Peter Zijlstraf9c0b092008-10-17 19:27:04 +02003470 vruntime += sched_vslice(cfs_rq, se);
Peter Zijlstraaeb73b02007-10-15 17:00:05 +02003471
Mike Galbraitha2e7a7e2009-09-18 09:19:25 +02003472 /* sleeps up to a single latency don't count. */
Mike Galbraith5ca98802010-03-11 17:17:17 +01003473 if (!initial) {
Mike Galbraitha2e7a7e2009-09-18 09:19:25 +02003474 unsigned long thresh = sysctl_sched_latency;
Peter Zijlstraa7be37a2008-06-27 13:41:11 +02003475
Mike Galbraitha2e7a7e2009-09-18 09:19:25 +02003476 /*
Mike Galbraitha2e7a7e2009-09-18 09:19:25 +02003477 * Halve their sleep time's effect, to allow
3478 * for a gentler effect of sleepers:
3479 */
3480 if (sched_feat(GENTLE_FAIR_SLEEPERS))
3481 thresh >>= 1;
Ingo Molnar51e03042009-09-16 08:54:45 +02003482
Mike Galbraitha2e7a7e2009-09-18 09:19:25 +02003483 vruntime -= thresh;
Peter Zijlstraaeb73b02007-10-15 17:00:05 +02003484 }
3485
Mike Galbraithb5d9d732009-09-08 11:12:28 +02003486 /* ensure we never gain time by being placed backwards. */
Viresh Kumar16c8f1c2012-11-08 13:33:46 +05303487 se->vruntime = max_vruntime(se->vruntime, vruntime);
Peter Zijlstraaeb73b02007-10-15 17:00:05 +02003488}
3489
Paul Turnerd3d9dc32011-07-21 09:43:39 -07003490static void check_enqueue_throttle(struct cfs_rq *cfs_rq);
3491
Mel Gormancb251762016-02-05 09:08:36 +00003492static inline void check_schedstat_required(void)
3493{
3494#ifdef CONFIG_SCHEDSTATS
3495 if (schedstat_enabled())
3496 return;
3497
3498 /* Force schedstat enabled if a dependent tracepoint is active */
3499 if (trace_sched_stat_wait_enabled() ||
3500 trace_sched_stat_sleep_enabled() ||
3501 trace_sched_stat_iowait_enabled() ||
3502 trace_sched_stat_blocked_enabled() ||
3503 trace_sched_stat_runtime_enabled()) {
Josh Poimboeufeda8dca2016-06-13 02:32:09 -05003504 printk_deferred_once("Scheduler tracepoints stat_sleep, stat_iowait, "
Mel Gormancb251762016-02-05 09:08:36 +00003505 "stat_blocked and stat_runtime require the "
3506 "kernel parameter schedstats=enabled or "
3507 "kernel.sched_schedstats=1\n");
3508 }
3509#endif
3510}
3511
Peter Zijlstrab5179ac2016-05-11 16:10:34 +02003512
3513/*
3514 * MIGRATION
3515 *
3516 * dequeue
3517 * update_curr()
3518 * update_min_vruntime()
3519 * vruntime -= min_vruntime
3520 *
3521 * enqueue
3522 * update_curr()
3523 * update_min_vruntime()
3524 * vruntime += min_vruntime
3525 *
3526 * this way the vruntime transition between RQs is done when both
3527 * min_vruntime are up-to-date.
3528 *
3529 * WAKEUP (remote)
3530 *
Peter Zijlstra59efa0b2016-05-10 18:24:37 +02003531 * ->migrate_task_rq_fair() (p->state == TASK_WAKING)
Peter Zijlstrab5179ac2016-05-11 16:10:34 +02003532 * vruntime -= min_vruntime
3533 *
3534 * enqueue
3535 * update_curr()
3536 * update_min_vruntime()
3537 * vruntime += min_vruntime
3538 *
3539 * this way we don't have the most up-to-date min_vruntime on the originating
3540 * CPU and an up-to-date min_vruntime on the destination CPU.
3541 */
3542
Peter Zijlstraaeb73b02007-10-15 17:00:05 +02003543static void
Peter Zijlstra88ec22d2009-12-16 18:04:41 +01003544enqueue_entity(struct cfs_rq *cfs_rq, struct sched_entity *se, int flags)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02003545{
Peter Zijlstra2f950352016-05-11 19:27:56 +02003546 bool renorm = !(flags & ENQUEUE_WAKEUP) || (flags & ENQUEUE_MIGRATED);
3547 bool curr = cfs_rq->curr == se;
Peter Zijlstra3a47d512016-03-09 13:04:03 +01003548
Ingo Molnar53d3bc72016-05-11 08:25:53 +02003549 /*
Peter Zijlstra2f950352016-05-11 19:27:56 +02003550 * If we're the current task, we must renormalise before calling
3551 * update_curr().
Ingo Molnar53d3bc72016-05-11 08:25:53 +02003552 */
Peter Zijlstra2f950352016-05-11 19:27:56 +02003553 if (renorm && curr)
3554 se->vruntime += cfs_rq->min_vruntime;
3555
Ingo Molnarb7cc0892007-08-09 11:16:47 +02003556 update_curr(cfs_rq);
Peter Zijlstra2f950352016-05-11 19:27:56 +02003557
3558 /*
3559 * Otherwise, renormalise after, such that we're placed at the current
3560 * moment in time, instead of some random moment in the past. Being
3561 * placed in the past could significantly boost this task to the
3562 * fairness detriment of existing tasks.
3563 */
3564 if (renorm && !curr)
3565 se->vruntime += cfs_rq->min_vruntime;
3566
Vincent Guittotd31b1a62016-11-08 10:53:44 +01003567 update_load_avg(se, UPDATE_TG);
Yuyang Du9d89c252015-07-15 08:04:37 +08003568 enqueue_entity_load_avg(cfs_rq, se);
Linus Torvalds17bc14b2012-12-14 07:20:43 -08003569 account_entity_enqueue(cfs_rq, se);
3570 update_cfs_shares(cfs_rq);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02003571
Josh Poimboeuf1a3d0272016-06-17 12:43:23 -05003572 if (flags & ENQUEUE_WAKEUP)
Peter Zijlstraaeb73b02007-10-15 17:00:05 +02003573 place_entity(cfs_rq, se, 0);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02003574
Mel Gormancb251762016-02-05 09:08:36 +00003575 check_schedstat_required();
Josh Poimboeuf4fa8d292016-06-17 12:43:26 -05003576 update_stats_enqueue(cfs_rq, se, flags);
3577 check_spread(cfs_rq, se);
Peter Zijlstra2f950352016-05-11 19:27:56 +02003578 if (!curr)
Srivatsa Vaddagiri83b699e2007-10-15 17:00:08 +02003579 __enqueue_entity(cfs_rq, se);
Peter Zijlstra2069dd72010-11-15 15:47:00 -08003580 se->on_rq = 1;
Peter Zijlstra3d4b47b2010-11-15 15:47:01 -08003581
Paul Turnerd3d9dc32011-07-21 09:43:39 -07003582 if (cfs_rq->nr_running == 1) {
Peter Zijlstra3d4b47b2010-11-15 15:47:01 -08003583 list_add_leaf_cfs_rq(cfs_rq);
Paul Turnerd3d9dc32011-07-21 09:43:39 -07003584 check_enqueue_throttle(cfs_rq);
3585 }
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02003586}
3587
Rik van Riel2c13c9192011-02-01 09:48:37 -05003588static void __clear_buddies_last(struct sched_entity *se)
Peter Zijlstra2002c692008-11-11 11:52:33 +01003589{
Rik van Riel2c13c9192011-02-01 09:48:37 -05003590 for_each_sched_entity(se) {
3591 struct cfs_rq *cfs_rq = cfs_rq_of(se);
Peter Zijlstraf1044792012-02-11 06:05:00 +01003592 if (cfs_rq->last != se)
Rik van Riel2c13c9192011-02-01 09:48:37 -05003593 break;
Peter Zijlstraf1044792012-02-11 06:05:00 +01003594
3595 cfs_rq->last = NULL;
Rik van Riel2c13c9192011-02-01 09:48:37 -05003596 }
3597}
Peter Zijlstra2002c692008-11-11 11:52:33 +01003598
Rik van Riel2c13c9192011-02-01 09:48:37 -05003599static void __clear_buddies_next(struct sched_entity *se)
3600{
3601 for_each_sched_entity(se) {
3602 struct cfs_rq *cfs_rq = cfs_rq_of(se);
Peter Zijlstraf1044792012-02-11 06:05:00 +01003603 if (cfs_rq->next != se)
Rik van Riel2c13c9192011-02-01 09:48:37 -05003604 break;
Peter Zijlstraf1044792012-02-11 06:05:00 +01003605
3606 cfs_rq->next = NULL;
Rik van Riel2c13c9192011-02-01 09:48:37 -05003607 }
Peter Zijlstra2002c692008-11-11 11:52:33 +01003608}
3609
Rik van Rielac53db52011-02-01 09:51:03 -05003610static void __clear_buddies_skip(struct sched_entity *se)
3611{
3612 for_each_sched_entity(se) {
3613 struct cfs_rq *cfs_rq = cfs_rq_of(se);
Peter Zijlstraf1044792012-02-11 06:05:00 +01003614 if (cfs_rq->skip != se)
Rik van Rielac53db52011-02-01 09:51:03 -05003615 break;
Peter Zijlstraf1044792012-02-11 06:05:00 +01003616
3617 cfs_rq->skip = NULL;
Rik van Rielac53db52011-02-01 09:51:03 -05003618 }
3619}
3620
Peter Zijlstraa571bbe2009-01-28 14:51:40 +01003621static void clear_buddies(struct cfs_rq *cfs_rq, struct sched_entity *se)
3622{
Rik van Riel2c13c9192011-02-01 09:48:37 -05003623 if (cfs_rq->last == se)
3624 __clear_buddies_last(se);
3625
3626 if (cfs_rq->next == se)
3627 __clear_buddies_next(se);
Rik van Rielac53db52011-02-01 09:51:03 -05003628
3629 if (cfs_rq->skip == se)
3630 __clear_buddies_skip(se);
Peter Zijlstraa571bbe2009-01-28 14:51:40 +01003631}
3632
Peter Zijlstra6c16a6d2012-03-21 13:07:16 -07003633static __always_inline void return_cfs_rq_runtime(struct cfs_rq *cfs_rq);
Paul Turnerd8b49862011-07-21 09:43:41 -07003634
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02003635static void
Peter Zijlstra371fd7e2010-03-24 16:38:48 +01003636dequeue_entity(struct cfs_rq *cfs_rq, struct sched_entity *se, int flags)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02003637{
Dmitry Adamushkoa2a2d682007-10-15 17:00:13 +02003638 /*
3639 * Update run-time statistics of the 'current'.
3640 */
3641 update_curr(cfs_rq);
Vincent Guittotd31b1a62016-11-08 10:53:44 +01003642 update_load_avg(se, UPDATE_TG);
Yuyang Du13962232015-07-15 08:04:41 +08003643 dequeue_entity_load_avg(cfs_rq, se);
Dmitry Adamushkoa2a2d682007-10-15 17:00:13 +02003644
Josh Poimboeuf4fa8d292016-06-17 12:43:26 -05003645 update_stats_dequeue(cfs_rq, se, flags);
Peter Zijlstra67e9fb22007-10-15 17:00:10 +02003646
Peter Zijlstra2002c692008-11-11 11:52:33 +01003647 clear_buddies(cfs_rq, se);
Peter Zijlstra47932412008-11-04 21:25:09 +01003648
Srivatsa Vaddagiri83b699e2007-10-15 17:00:08 +02003649 if (se != cfs_rq->curr)
Dmitry Adamushko30cfdcf2007-10-15 17:00:07 +02003650 __dequeue_entity(cfs_rq, se);
Linus Torvalds17bc14b2012-12-14 07:20:43 -08003651 se->on_rq = 0;
Dmitry Adamushko30cfdcf2007-10-15 17:00:07 +02003652 account_entity_dequeue(cfs_rq, se);
Peter Zijlstra88ec22d2009-12-16 18:04:41 +01003653
3654 /*
Peter Zijlstrab60205c2016-09-20 21:58:12 +02003655 * Normalize after update_curr(); which will also have moved
3656 * min_vruntime if @se is the one holding it back. But before doing
3657 * update_min_vruntime() again, which will discount @se's position and
3658 * can move min_vruntime forward still more.
Peter Zijlstra88ec22d2009-12-16 18:04:41 +01003659 */
Peter Zijlstra371fd7e2010-03-24 16:38:48 +01003660 if (!(flags & DEQUEUE_SLEEP))
Peter Zijlstra88ec22d2009-12-16 18:04:41 +01003661 se->vruntime -= cfs_rq->min_vruntime;
Peter Zijlstra1e876232011-05-17 16:21:10 -07003662
Paul Turnerd8b49862011-07-21 09:43:41 -07003663 /* return excess runtime on last dequeue */
3664 return_cfs_rq_runtime(cfs_rq);
3665
Linus Torvalds17bc14b2012-12-14 07:20:43 -08003666 update_cfs_shares(cfs_rq);
Peter Zijlstrab60205c2016-09-20 21:58:12 +02003667
3668 /*
3669 * Now advance min_vruntime if @se was the entity holding it back,
3670 * except when: DEQUEUE_SAVE && !DEQUEUE_MOVE, in this case we'll be
3671 * put back on, and if we advance min_vruntime, we'll be placed back
3672 * further than we started -- ie. we'll be penalized.
3673 */
3674 if ((flags & (DEQUEUE_SAVE | DEQUEUE_MOVE)) == DEQUEUE_SAVE)
3675 update_min_vruntime(cfs_rq);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02003676}
3677
3678/*
3679 * Preempt the current task with a newly woken task if needed:
3680 */
Peter Zijlstra7c92e542007-09-05 14:32:49 +02003681static void
Ingo Molnar2e09bf52007-10-15 17:00:05 +02003682check_preempt_tick(struct cfs_rq *cfs_rq, struct sched_entity *curr)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02003683{
Peter Zijlstra11697832007-09-05 14:32:49 +02003684 unsigned long ideal_runtime, delta_exec;
Wang Xingchaof4cfb332011-09-16 13:35:52 -04003685 struct sched_entity *se;
3686 s64 delta;
Peter Zijlstra11697832007-09-05 14:32:49 +02003687
Peter Zijlstra6d0f0eb2007-10-15 17:00:05 +02003688 ideal_runtime = sched_slice(cfs_rq, curr);
Peter Zijlstra11697832007-09-05 14:32:49 +02003689 delta_exec = curr->sum_exec_runtime - curr->prev_sum_exec_runtime;
Mike Galbraitha9f3e2b2009-01-28 14:51:39 +01003690 if (delta_exec > ideal_runtime) {
Kirill Tkhai88751252014-06-29 00:03:57 +04003691 resched_curr(rq_of(cfs_rq));
Mike Galbraitha9f3e2b2009-01-28 14:51:39 +01003692 /*
3693 * The current task ran long enough, ensure it doesn't get
3694 * re-elected due to buddy favours.
3695 */
3696 clear_buddies(cfs_rq, curr);
Mike Galbraithf685cea2009-10-23 23:09:22 +02003697 return;
3698 }
3699
3700 /*
3701 * Ensure that a task that missed wakeup preemption by a
3702 * narrow margin doesn't have to wait for a full slice.
3703 * This also mitigates buddy induced latencies under load.
3704 */
Mike Galbraithf685cea2009-10-23 23:09:22 +02003705 if (delta_exec < sysctl_sched_min_granularity)
3706 return;
3707
Wang Xingchaof4cfb332011-09-16 13:35:52 -04003708 se = __pick_first_entity(cfs_rq);
3709 delta = curr->vruntime - se->vruntime;
Mike Galbraithf685cea2009-10-23 23:09:22 +02003710
Wang Xingchaof4cfb332011-09-16 13:35:52 -04003711 if (delta < 0)
3712 return;
Mike Galbraithd7d82942011-01-05 05:41:17 +01003713
Wang Xingchaof4cfb332011-09-16 13:35:52 -04003714 if (delta > ideal_runtime)
Kirill Tkhai88751252014-06-29 00:03:57 +04003715 resched_curr(rq_of(cfs_rq));
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02003716}
3717
Srivatsa Vaddagiri83b699e2007-10-15 17:00:08 +02003718static void
Ingo Molnar8494f412007-08-09 11:16:48 +02003719set_next_entity(struct cfs_rq *cfs_rq, struct sched_entity *se)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02003720{
Srivatsa Vaddagiri83b699e2007-10-15 17:00:08 +02003721 /* 'current' is not kept within the tree. */
3722 if (se->on_rq) {
3723 /*
3724 * Any task has to be enqueued before it get to execute on
3725 * a CPU. So account for the time it spent waiting on the
3726 * runqueue.
3727 */
Josh Poimboeuf4fa8d292016-06-17 12:43:26 -05003728 update_stats_wait_end(cfs_rq, se);
Srivatsa Vaddagiri83b699e2007-10-15 17:00:08 +02003729 __dequeue_entity(cfs_rq, se);
Vincent Guittotd31b1a62016-11-08 10:53:44 +01003730 update_load_avg(se, UPDATE_TG);
Srivatsa Vaddagiri83b699e2007-10-15 17:00:08 +02003731 }
3732
Ingo Molnar79303e92007-08-09 11:16:47 +02003733 update_stats_curr_start(cfs_rq, se);
Ingo Molnar429d43b2007-10-15 17:00:03 +02003734 cfs_rq->curr = se;
Josh Poimboeuf4fa8d292016-06-17 12:43:26 -05003735
Ingo Molnareba1ed42007-10-15 17:00:02 +02003736 /*
3737 * Track our maximum slice length, if the CPU's load is at
3738 * least twice that of our own weight (i.e. dont track it
3739 * when there are only lesser-weight tasks around):
3740 */
Mel Gormancb251762016-02-05 09:08:36 +00003741 if (schedstat_enabled() && rq_of(cfs_rq)->load.weight >= 2*se->load.weight) {
Josh Poimboeuf4fa8d292016-06-17 12:43:26 -05003742 schedstat_set(se->statistics.slice_max,
3743 max((u64)schedstat_val(se->statistics.slice_max),
3744 se->sum_exec_runtime - se->prev_sum_exec_runtime));
Ingo Molnareba1ed42007-10-15 17:00:02 +02003745 }
Josh Poimboeuf4fa8d292016-06-17 12:43:26 -05003746
Peter Zijlstra4a55b452007-09-05 14:32:49 +02003747 se->prev_sum_exec_runtime = se->sum_exec_runtime;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02003748}
3749
Peter Zijlstra3f3a4902008-10-24 11:06:16 +02003750static int
3751wakeup_preempt_entity(struct sched_entity *curr, struct sched_entity *se);
3752
Rik van Rielac53db52011-02-01 09:51:03 -05003753/*
3754 * Pick the next process, keeping these things in mind, in this order:
3755 * 1) keep things fair between processes/task groups
3756 * 2) pick the "next" process, since someone really wants that to run
3757 * 3) pick the "last" process, for cache locality
3758 * 4) do not run the "skip" process, if something else is available
3759 */
Peter Zijlstra678d5712012-02-11 06:05:00 +01003760static struct sched_entity *
3761pick_next_entity(struct cfs_rq *cfs_rq, struct sched_entity *curr)
Peter Zijlstraaa2ac252008-03-14 21:12:12 +01003762{
Peter Zijlstra678d5712012-02-11 06:05:00 +01003763 struct sched_entity *left = __pick_first_entity(cfs_rq);
3764 struct sched_entity *se;
3765
3766 /*
3767 * If curr is set we have to see if its left of the leftmost entity
3768 * still in the tree, provided there was anything in the tree at all.
3769 */
3770 if (!left || (curr && entity_before(curr, left)))
3771 left = curr;
3772
3773 se = left; /* ideally we run the leftmost entity */
Peter Zijlstraf4b67552008-11-04 21:25:07 +01003774
Rik van Rielac53db52011-02-01 09:51:03 -05003775 /*
3776 * Avoid running the skip buddy, if running something else can
3777 * be done without getting too unfair.
3778 */
3779 if (cfs_rq->skip == se) {
Peter Zijlstra678d5712012-02-11 06:05:00 +01003780 struct sched_entity *second;
3781
3782 if (se == curr) {
3783 second = __pick_first_entity(cfs_rq);
3784 } else {
3785 second = __pick_next_entity(se);
3786 if (!second || (curr && entity_before(curr, second)))
3787 second = curr;
3788 }
3789
Rik van Rielac53db52011-02-01 09:51:03 -05003790 if (second && wakeup_preempt_entity(second, left) < 1)
3791 se = second;
3792 }
Peter Zijlstraaa2ac252008-03-14 21:12:12 +01003793
Mike Galbraithf685cea2009-10-23 23:09:22 +02003794 /*
3795 * Prefer last buddy, try to return the CPU to a preempted task.
3796 */
3797 if (cfs_rq->last && wakeup_preempt_entity(cfs_rq->last, left) < 1)
3798 se = cfs_rq->last;
3799
Rik van Rielac53db52011-02-01 09:51:03 -05003800 /*
3801 * Someone really wants this to run. If it's not unfair, run it.
3802 */
3803 if (cfs_rq->next && wakeup_preempt_entity(cfs_rq->next, left) < 1)
3804 se = cfs_rq->next;
3805
Mike Galbraithf685cea2009-10-23 23:09:22 +02003806 clear_buddies(cfs_rq, se);
Peter Zijlstra47932412008-11-04 21:25:09 +01003807
3808 return se;
Peter Zijlstraaa2ac252008-03-14 21:12:12 +01003809}
3810
Peter Zijlstra678d5712012-02-11 06:05:00 +01003811static bool check_cfs_rq_runtime(struct cfs_rq *cfs_rq);
Paul Turnerd3d9dc32011-07-21 09:43:39 -07003812
Ingo Molnarab6cde22007-08-09 11:16:48 +02003813static void put_prev_entity(struct cfs_rq *cfs_rq, struct sched_entity *prev)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02003814{
3815 /*
3816 * If still on the runqueue then deactivate_task()
3817 * was not called and update_curr() has to be done:
3818 */
3819 if (prev->on_rq)
Ingo Molnarb7cc0892007-08-09 11:16:47 +02003820 update_curr(cfs_rq);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02003821
Paul Turnerd3d9dc32011-07-21 09:43:39 -07003822 /* throttle cfs_rqs exceeding runtime */
3823 check_cfs_rq_runtime(cfs_rq);
3824
Josh Poimboeuf4fa8d292016-06-17 12:43:26 -05003825 check_spread(cfs_rq, prev);
Mel Gormancb251762016-02-05 09:08:36 +00003826
Dmitry Adamushko30cfdcf2007-10-15 17:00:07 +02003827 if (prev->on_rq) {
Josh Poimboeuf4fa8d292016-06-17 12:43:26 -05003828 update_stats_wait_start(cfs_rq, prev);
Dmitry Adamushko30cfdcf2007-10-15 17:00:07 +02003829 /* Put 'current' back into the tree. */
3830 __enqueue_entity(cfs_rq, prev);
Paul Turner9d85f212012-10-04 13:18:29 +02003831 /* in !on_rq case, update occurred at dequeue */
Yuyang Du9d89c252015-07-15 08:04:37 +08003832 update_load_avg(prev, 0);
Dmitry Adamushko30cfdcf2007-10-15 17:00:07 +02003833 }
Ingo Molnar429d43b2007-10-15 17:00:03 +02003834 cfs_rq->curr = NULL;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02003835}
3836
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01003837static void
3838entity_tick(struct cfs_rq *cfs_rq, struct sched_entity *curr, int queued)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02003839{
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02003840 /*
Dmitry Adamushko30cfdcf2007-10-15 17:00:07 +02003841 * Update run-time statistics of the 'current'.
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02003842 */
Dmitry Adamushko30cfdcf2007-10-15 17:00:07 +02003843 update_curr(cfs_rq);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02003844
Paul Turner43365bd2010-12-15 19:10:17 -08003845 /*
Paul Turner9d85f212012-10-04 13:18:29 +02003846 * Ensure that runnable average is periodically updated.
3847 */
Vincent Guittotd31b1a62016-11-08 10:53:44 +01003848 update_load_avg(curr, UPDATE_TG);
Peter Zijlstrabf0bd942013-07-26 23:48:42 +02003849 update_cfs_shares(cfs_rq);
Paul Turner9d85f212012-10-04 13:18:29 +02003850
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01003851#ifdef CONFIG_SCHED_HRTICK
3852 /*
3853 * queued ticks are scheduled to match the slice, so don't bother
3854 * validating it and just reschedule.
3855 */
Harvey Harrison983ed7a2008-04-24 18:17:55 -07003856 if (queued) {
Kirill Tkhai88751252014-06-29 00:03:57 +04003857 resched_curr(rq_of(cfs_rq));
Harvey Harrison983ed7a2008-04-24 18:17:55 -07003858 return;
3859 }
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01003860 /*
3861 * don't let the period tick interfere with the hrtick preemption
3862 */
3863 if (!sched_feat(DOUBLE_TICK) &&
3864 hrtimer_active(&rq_of(cfs_rq)->hrtick_timer))
3865 return;
3866#endif
3867
Yong Zhang2c2efae2011-07-29 16:20:33 +08003868 if (cfs_rq->nr_running > 1)
Ingo Molnar2e09bf52007-10-15 17:00:05 +02003869 check_preempt_tick(cfs_rq, curr);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02003870}
3871
Paul Turnerab84d312011-07-21 09:43:28 -07003872
3873/**************************************************
3874 * CFS bandwidth control machinery
3875 */
3876
3877#ifdef CONFIG_CFS_BANDWIDTH
Peter Zijlstra029632f2011-10-25 10:00:11 +02003878
3879#ifdef HAVE_JUMP_LABEL
Ingo Molnarc5905af2012-02-24 08:31:31 +01003880static struct static_key __cfs_bandwidth_used;
Peter Zijlstra029632f2011-10-25 10:00:11 +02003881
3882static inline bool cfs_bandwidth_used(void)
3883{
Ingo Molnarc5905af2012-02-24 08:31:31 +01003884 return static_key_false(&__cfs_bandwidth_used);
Peter Zijlstra029632f2011-10-25 10:00:11 +02003885}
3886
Ben Segall1ee14e62013-10-16 11:16:12 -07003887void cfs_bandwidth_usage_inc(void)
Peter Zijlstra029632f2011-10-25 10:00:11 +02003888{
Ben Segall1ee14e62013-10-16 11:16:12 -07003889 static_key_slow_inc(&__cfs_bandwidth_used);
3890}
3891
3892void cfs_bandwidth_usage_dec(void)
3893{
3894 static_key_slow_dec(&__cfs_bandwidth_used);
Peter Zijlstra029632f2011-10-25 10:00:11 +02003895}
3896#else /* HAVE_JUMP_LABEL */
3897static bool cfs_bandwidth_used(void)
3898{
3899 return true;
3900}
3901
Ben Segall1ee14e62013-10-16 11:16:12 -07003902void cfs_bandwidth_usage_inc(void) {}
3903void cfs_bandwidth_usage_dec(void) {}
Peter Zijlstra029632f2011-10-25 10:00:11 +02003904#endif /* HAVE_JUMP_LABEL */
3905
Paul Turnerab84d312011-07-21 09:43:28 -07003906/*
3907 * default period for cfs group bandwidth.
3908 * default: 0.1s, units: nanoseconds
3909 */
3910static inline u64 default_cfs_period(void)
3911{
3912 return 100000000ULL;
3913}
Paul Turnerec12cb72011-07-21 09:43:30 -07003914
3915static inline u64 sched_cfs_bandwidth_slice(void)
3916{
3917 return (u64)sysctl_sched_cfs_bandwidth_slice * NSEC_PER_USEC;
3918}
3919
Paul Turnera9cf55b2011-07-21 09:43:32 -07003920/*
3921 * Replenish runtime according to assigned quota and update expiration time.
3922 * We use sched_clock_cpu directly instead of rq->clock to avoid adding
3923 * additional synchronization around rq->lock.
3924 *
3925 * requires cfs_b->lock
3926 */
Peter Zijlstra029632f2011-10-25 10:00:11 +02003927void __refill_cfs_bandwidth_runtime(struct cfs_bandwidth *cfs_b)
Paul Turnera9cf55b2011-07-21 09:43:32 -07003928{
3929 u64 now;
3930
3931 if (cfs_b->quota == RUNTIME_INF)
3932 return;
3933
3934 now = sched_clock_cpu(smp_processor_id());
3935 cfs_b->runtime = cfs_b->quota;
3936 cfs_b->runtime_expires = now + ktime_to_ns(cfs_b->period);
3937}
3938
Peter Zijlstra029632f2011-10-25 10:00:11 +02003939static inline struct cfs_bandwidth *tg_cfs_bandwidth(struct task_group *tg)
3940{
3941 return &tg->cfs_bandwidth;
3942}
3943
Paul Turnerf1b17282012-10-04 13:18:31 +02003944/* rq->task_clock normalized against any time this cfs_rq has spent throttled */
3945static inline u64 cfs_rq_clock_task(struct cfs_rq *cfs_rq)
3946{
3947 if (unlikely(cfs_rq->throttle_count))
Xunlei Pang1a99ae32016-05-10 21:03:18 +08003948 return cfs_rq->throttled_clock_task - cfs_rq->throttled_clock_task_time;
Paul Turnerf1b17282012-10-04 13:18:31 +02003949
Frederic Weisbecker78becc22013-04-12 01:51:02 +02003950 return rq_clock_task(rq_of(cfs_rq)) - cfs_rq->throttled_clock_task_time;
Paul Turnerf1b17282012-10-04 13:18:31 +02003951}
3952
Paul Turner85dac902011-07-21 09:43:33 -07003953/* returns 0 on failure to allocate runtime */
3954static int assign_cfs_rq_runtime(struct cfs_rq *cfs_rq)
Paul Turnerec12cb72011-07-21 09:43:30 -07003955{
3956 struct task_group *tg = cfs_rq->tg;
3957 struct cfs_bandwidth *cfs_b = tg_cfs_bandwidth(tg);
Paul Turnera9cf55b2011-07-21 09:43:32 -07003958 u64 amount = 0, min_amount, expires;
Paul Turnerec12cb72011-07-21 09:43:30 -07003959
3960 /* note: this is a positive sum as runtime_remaining <= 0 */
3961 min_amount = sched_cfs_bandwidth_slice() - cfs_rq->runtime_remaining;
3962
3963 raw_spin_lock(&cfs_b->lock);
3964 if (cfs_b->quota == RUNTIME_INF)
3965 amount = min_amount;
Paul Turner58088ad2011-07-21 09:43:31 -07003966 else {
Peter Zijlstra77a4d1a2015-04-15 11:41:57 +02003967 start_cfs_bandwidth(cfs_b);
Paul Turner58088ad2011-07-21 09:43:31 -07003968
3969 if (cfs_b->runtime > 0) {
3970 amount = min(cfs_b->runtime, min_amount);
3971 cfs_b->runtime -= amount;
3972 cfs_b->idle = 0;
3973 }
Paul Turnerec12cb72011-07-21 09:43:30 -07003974 }
Paul Turnera9cf55b2011-07-21 09:43:32 -07003975 expires = cfs_b->runtime_expires;
Paul Turnerec12cb72011-07-21 09:43:30 -07003976 raw_spin_unlock(&cfs_b->lock);
3977
3978 cfs_rq->runtime_remaining += amount;
Paul Turnera9cf55b2011-07-21 09:43:32 -07003979 /*
3980 * we may have advanced our local expiration to account for allowed
3981 * spread between our sched_clock and the one on which runtime was
3982 * issued.
3983 */
3984 if ((s64)(expires - cfs_rq->runtime_expires) > 0)
3985 cfs_rq->runtime_expires = expires;
Paul Turner85dac902011-07-21 09:43:33 -07003986
3987 return cfs_rq->runtime_remaining > 0;
Paul Turnera9cf55b2011-07-21 09:43:32 -07003988}
3989
3990/*
3991 * Note: This depends on the synchronization provided by sched_clock and the
3992 * fact that rq->clock snapshots this value.
3993 */
3994static void expire_cfs_rq_runtime(struct cfs_rq *cfs_rq)
3995{
3996 struct cfs_bandwidth *cfs_b = tg_cfs_bandwidth(cfs_rq->tg);
Paul Turnera9cf55b2011-07-21 09:43:32 -07003997
3998 /* if the deadline is ahead of our clock, nothing to do */
Frederic Weisbecker78becc22013-04-12 01:51:02 +02003999 if (likely((s64)(rq_clock(rq_of(cfs_rq)) - cfs_rq->runtime_expires) < 0))
Paul Turnera9cf55b2011-07-21 09:43:32 -07004000 return;
4001
4002 if (cfs_rq->runtime_remaining < 0)
4003 return;
4004
4005 /*
4006 * If the local deadline has passed we have to consider the
4007 * possibility that our sched_clock is 'fast' and the global deadline
4008 * has not truly expired.
4009 *
4010 * Fortunately we can check determine whether this the case by checking
Ben Segall51f21762014-05-19 15:49:45 -07004011 * whether the global deadline has advanced. It is valid to compare
4012 * cfs_b->runtime_expires without any locks since we only care about
4013 * exact equality, so a partial write will still work.
Paul Turnera9cf55b2011-07-21 09:43:32 -07004014 */
4015
Ben Segall51f21762014-05-19 15:49:45 -07004016 if (cfs_rq->runtime_expires != cfs_b->runtime_expires) {
Paul Turnera9cf55b2011-07-21 09:43:32 -07004017 /* extend local deadline, drift is bounded above by 2 ticks */
4018 cfs_rq->runtime_expires += TICK_NSEC;
4019 } else {
4020 /* global deadline is ahead, expiration has passed */
4021 cfs_rq->runtime_remaining = 0;
4022 }
Paul Turnerec12cb72011-07-21 09:43:30 -07004023}
4024
Peter Zijlstra9dbdb152013-11-18 18:27:06 +01004025static void __account_cfs_rq_runtime(struct cfs_rq *cfs_rq, u64 delta_exec)
Paul Turnerec12cb72011-07-21 09:43:30 -07004026{
Paul Turnera9cf55b2011-07-21 09:43:32 -07004027 /* dock delta_exec before expiring quota (as it could span periods) */
Paul Turnerec12cb72011-07-21 09:43:30 -07004028 cfs_rq->runtime_remaining -= delta_exec;
Paul Turnera9cf55b2011-07-21 09:43:32 -07004029 expire_cfs_rq_runtime(cfs_rq);
4030
4031 if (likely(cfs_rq->runtime_remaining > 0))
Paul Turnerec12cb72011-07-21 09:43:30 -07004032 return;
4033
Paul Turner85dac902011-07-21 09:43:33 -07004034 /*
4035 * if we're unable to extend our runtime we resched so that the active
4036 * hierarchy can be throttled
4037 */
4038 if (!assign_cfs_rq_runtime(cfs_rq) && likely(cfs_rq->curr))
Kirill Tkhai88751252014-06-29 00:03:57 +04004039 resched_curr(rq_of(cfs_rq));
Paul Turnerec12cb72011-07-21 09:43:30 -07004040}
4041
Peter Zijlstra6c16a6d2012-03-21 13:07:16 -07004042static __always_inline
Peter Zijlstra9dbdb152013-11-18 18:27:06 +01004043void account_cfs_rq_runtime(struct cfs_rq *cfs_rq, u64 delta_exec)
Paul Turnerec12cb72011-07-21 09:43:30 -07004044{
Paul Turner56f570e2011-11-07 20:26:33 -08004045 if (!cfs_bandwidth_used() || !cfs_rq->runtime_enabled)
Paul Turnerec12cb72011-07-21 09:43:30 -07004046 return;
4047
4048 __account_cfs_rq_runtime(cfs_rq, delta_exec);
4049}
4050
Paul Turner85dac902011-07-21 09:43:33 -07004051static inline int cfs_rq_throttled(struct cfs_rq *cfs_rq)
4052{
Paul Turner56f570e2011-11-07 20:26:33 -08004053 return cfs_bandwidth_used() && cfs_rq->throttled;
Paul Turner85dac902011-07-21 09:43:33 -07004054}
4055
Paul Turner64660c82011-07-21 09:43:36 -07004056/* check whether cfs_rq, or any parent, is throttled */
4057static inline int throttled_hierarchy(struct cfs_rq *cfs_rq)
4058{
Paul Turner56f570e2011-11-07 20:26:33 -08004059 return cfs_bandwidth_used() && cfs_rq->throttle_count;
Paul Turner64660c82011-07-21 09:43:36 -07004060}
4061
4062/*
4063 * Ensure that neither of the group entities corresponding to src_cpu or
4064 * dest_cpu are members of a throttled hierarchy when performing group
4065 * load-balance operations.
4066 */
4067static inline int throttled_lb_pair(struct task_group *tg,
4068 int src_cpu, int dest_cpu)
4069{
4070 struct cfs_rq *src_cfs_rq, *dest_cfs_rq;
4071
4072 src_cfs_rq = tg->cfs_rq[src_cpu];
4073 dest_cfs_rq = tg->cfs_rq[dest_cpu];
4074
4075 return throttled_hierarchy(src_cfs_rq) ||
4076 throttled_hierarchy(dest_cfs_rq);
4077}
4078
4079/* updated child weight may affect parent so we have to do this bottom up */
4080static int tg_unthrottle_up(struct task_group *tg, void *data)
4081{
4082 struct rq *rq = data;
4083 struct cfs_rq *cfs_rq = tg->cfs_rq[cpu_of(rq)];
4084
4085 cfs_rq->throttle_count--;
Paul Turner64660c82011-07-21 09:43:36 -07004086 if (!cfs_rq->throttle_count) {
Paul Turnerf1b17282012-10-04 13:18:31 +02004087 /* adjust cfs_rq_clock_task() */
Frederic Weisbecker78becc22013-04-12 01:51:02 +02004088 cfs_rq->throttled_clock_task_time += rq_clock_task(rq) -
Paul Turnerf1b17282012-10-04 13:18:31 +02004089 cfs_rq->throttled_clock_task;
Paul Turner64660c82011-07-21 09:43:36 -07004090 }
Paul Turner64660c82011-07-21 09:43:36 -07004091
4092 return 0;
4093}
4094
4095static int tg_throttle_down(struct task_group *tg, void *data)
4096{
4097 struct rq *rq = data;
4098 struct cfs_rq *cfs_rq = tg->cfs_rq[cpu_of(rq)];
4099
Paul Turner82958362012-10-04 13:18:31 +02004100 /* group is entering throttled state, stop time */
4101 if (!cfs_rq->throttle_count)
Frederic Weisbecker78becc22013-04-12 01:51:02 +02004102 cfs_rq->throttled_clock_task = rq_clock_task(rq);
Paul Turner64660c82011-07-21 09:43:36 -07004103 cfs_rq->throttle_count++;
4104
4105 return 0;
4106}
4107
Paul Turnerd3d9dc32011-07-21 09:43:39 -07004108static void throttle_cfs_rq(struct cfs_rq *cfs_rq)
Paul Turner85dac902011-07-21 09:43:33 -07004109{
4110 struct rq *rq = rq_of(cfs_rq);
4111 struct cfs_bandwidth *cfs_b = tg_cfs_bandwidth(cfs_rq->tg);
4112 struct sched_entity *se;
4113 long task_delta, dequeue = 1;
Peter Zijlstra77a4d1a2015-04-15 11:41:57 +02004114 bool empty;
Paul Turner85dac902011-07-21 09:43:33 -07004115
4116 se = cfs_rq->tg->se[cpu_of(rq_of(cfs_rq))];
4117
Paul Turnerf1b17282012-10-04 13:18:31 +02004118 /* freeze hierarchy runnable averages while throttled */
Paul Turner64660c82011-07-21 09:43:36 -07004119 rcu_read_lock();
4120 walk_tg_tree_from(cfs_rq->tg, tg_throttle_down, tg_nop, (void *)rq);
4121 rcu_read_unlock();
Paul Turner85dac902011-07-21 09:43:33 -07004122
4123 task_delta = cfs_rq->h_nr_running;
4124 for_each_sched_entity(se) {
4125 struct cfs_rq *qcfs_rq = cfs_rq_of(se);
4126 /* throttled entity or throttle-on-deactivate */
4127 if (!se->on_rq)
4128 break;
4129
4130 if (dequeue)
4131 dequeue_entity(qcfs_rq, se, DEQUEUE_SLEEP);
4132 qcfs_rq->h_nr_running -= task_delta;
4133
4134 if (qcfs_rq->load.weight)
4135 dequeue = 0;
4136 }
4137
4138 if (!se)
Kirill Tkhai72465442014-05-09 03:00:14 +04004139 sub_nr_running(rq, task_delta);
Paul Turner85dac902011-07-21 09:43:33 -07004140
4141 cfs_rq->throttled = 1;
Frederic Weisbecker78becc22013-04-12 01:51:02 +02004142 cfs_rq->throttled_clock = rq_clock(rq);
Paul Turner85dac902011-07-21 09:43:33 -07004143 raw_spin_lock(&cfs_b->lock);
Cong Wangd49db342015-06-24 12:41:47 -07004144 empty = list_empty(&cfs_b->throttled_cfs_rq);
Peter Zijlstra77a4d1a2015-04-15 11:41:57 +02004145
Ben Segallc06f04c2014-06-20 15:21:20 -07004146 /*
4147 * Add to the _head_ of the list, so that an already-started
4148 * distribute_cfs_runtime will not see us
4149 */
4150 list_add_rcu(&cfs_rq->throttled_list, &cfs_b->throttled_cfs_rq);
Peter Zijlstra77a4d1a2015-04-15 11:41:57 +02004151
4152 /*
4153 * If we're the first throttled task, make sure the bandwidth
4154 * timer is running.
4155 */
4156 if (empty)
4157 start_cfs_bandwidth(cfs_b);
4158
Paul Turner85dac902011-07-21 09:43:33 -07004159 raw_spin_unlock(&cfs_b->lock);
4160}
4161
Peter Zijlstra029632f2011-10-25 10:00:11 +02004162void unthrottle_cfs_rq(struct cfs_rq *cfs_rq)
Paul Turner671fd9d2011-07-21 09:43:34 -07004163{
4164 struct rq *rq = rq_of(cfs_rq);
4165 struct cfs_bandwidth *cfs_b = tg_cfs_bandwidth(cfs_rq->tg);
4166 struct sched_entity *se;
4167 int enqueue = 1;
4168 long task_delta;
4169
Michael Wang22b958d2013-06-04 14:23:39 +08004170 se = cfs_rq->tg->se[cpu_of(rq)];
Paul Turner671fd9d2011-07-21 09:43:34 -07004171
4172 cfs_rq->throttled = 0;
Frederic Weisbecker1a55af22013-04-12 01:51:01 +02004173
4174 update_rq_clock(rq);
4175
Paul Turner671fd9d2011-07-21 09:43:34 -07004176 raw_spin_lock(&cfs_b->lock);
Frederic Weisbecker78becc22013-04-12 01:51:02 +02004177 cfs_b->throttled_time += rq_clock(rq) - cfs_rq->throttled_clock;
Paul Turner671fd9d2011-07-21 09:43:34 -07004178 list_del_rcu(&cfs_rq->throttled_list);
4179 raw_spin_unlock(&cfs_b->lock);
4180
Paul Turner64660c82011-07-21 09:43:36 -07004181 /* update hierarchical throttle state */
4182 walk_tg_tree_from(cfs_rq->tg, tg_nop, tg_unthrottle_up, (void *)rq);
4183
Paul Turner671fd9d2011-07-21 09:43:34 -07004184 if (!cfs_rq->load.weight)
4185 return;
4186
4187 task_delta = cfs_rq->h_nr_running;
4188 for_each_sched_entity(se) {
4189 if (se->on_rq)
4190 enqueue = 0;
4191
4192 cfs_rq = cfs_rq_of(se);
4193 if (enqueue)
4194 enqueue_entity(cfs_rq, se, ENQUEUE_WAKEUP);
4195 cfs_rq->h_nr_running += task_delta;
4196
4197 if (cfs_rq_throttled(cfs_rq))
4198 break;
4199 }
4200
4201 if (!se)
Kirill Tkhai72465442014-05-09 03:00:14 +04004202 add_nr_running(rq, task_delta);
Paul Turner671fd9d2011-07-21 09:43:34 -07004203
4204 /* determine whether we need to wake up potentially idle cpu */
4205 if (rq->curr == rq->idle && rq->cfs.nr_running)
Kirill Tkhai88751252014-06-29 00:03:57 +04004206 resched_curr(rq);
Paul Turner671fd9d2011-07-21 09:43:34 -07004207}
4208
4209static u64 distribute_cfs_runtime(struct cfs_bandwidth *cfs_b,
4210 u64 remaining, u64 expires)
4211{
4212 struct cfs_rq *cfs_rq;
Ben Segallc06f04c2014-06-20 15:21:20 -07004213 u64 runtime;
4214 u64 starting_runtime = remaining;
Paul Turner671fd9d2011-07-21 09:43:34 -07004215
4216 rcu_read_lock();
4217 list_for_each_entry_rcu(cfs_rq, &cfs_b->throttled_cfs_rq,
4218 throttled_list) {
4219 struct rq *rq = rq_of(cfs_rq);
4220
4221 raw_spin_lock(&rq->lock);
4222 if (!cfs_rq_throttled(cfs_rq))
4223 goto next;
4224
4225 runtime = -cfs_rq->runtime_remaining + 1;
4226 if (runtime > remaining)
4227 runtime = remaining;
4228 remaining -= runtime;
4229
4230 cfs_rq->runtime_remaining += runtime;
4231 cfs_rq->runtime_expires = expires;
4232
4233 /* we check whether we're throttled above */
4234 if (cfs_rq->runtime_remaining > 0)
4235 unthrottle_cfs_rq(cfs_rq);
4236
4237next:
4238 raw_spin_unlock(&rq->lock);
4239
4240 if (!remaining)
4241 break;
4242 }
4243 rcu_read_unlock();
4244
Ben Segallc06f04c2014-06-20 15:21:20 -07004245 return starting_runtime - remaining;
Paul Turner671fd9d2011-07-21 09:43:34 -07004246}
4247
Paul Turner58088ad2011-07-21 09:43:31 -07004248/*
4249 * Responsible for refilling a task_group's bandwidth and unthrottling its
4250 * cfs_rqs as appropriate. If there has been no activity within the last
4251 * period the timer is deactivated until scheduling resumes; cfs_b->idle is
4252 * used to track this state.
4253 */
4254static int do_sched_cfs_period_timer(struct cfs_bandwidth *cfs_b, int overrun)
4255{
Paul Turner671fd9d2011-07-21 09:43:34 -07004256 u64 runtime, runtime_expires;
Ben Segall51f21762014-05-19 15:49:45 -07004257 int throttled;
Paul Turner58088ad2011-07-21 09:43:31 -07004258
Paul Turner58088ad2011-07-21 09:43:31 -07004259 /* no need to continue the timer with no bandwidth constraint */
4260 if (cfs_b->quota == RUNTIME_INF)
Ben Segall51f21762014-05-19 15:49:45 -07004261 goto out_deactivate;
Paul Turner58088ad2011-07-21 09:43:31 -07004262
Paul Turner671fd9d2011-07-21 09:43:34 -07004263 throttled = !list_empty(&cfs_b->throttled_cfs_rq);
Nikhil Raoe8da1b12011-07-21 09:43:40 -07004264 cfs_b->nr_periods += overrun;
Paul Turner671fd9d2011-07-21 09:43:34 -07004265
Ben Segall51f21762014-05-19 15:49:45 -07004266 /*
4267 * idle depends on !throttled (for the case of a large deficit), and if
4268 * we're going inactive then everything else can be deferred
4269 */
4270 if (cfs_b->idle && !throttled)
4271 goto out_deactivate;
Paul Turnera9cf55b2011-07-21 09:43:32 -07004272
4273 __refill_cfs_bandwidth_runtime(cfs_b);
4274
Paul Turner671fd9d2011-07-21 09:43:34 -07004275 if (!throttled) {
4276 /* mark as potentially idle for the upcoming period */
4277 cfs_b->idle = 1;
Ben Segall51f21762014-05-19 15:49:45 -07004278 return 0;
Paul Turner671fd9d2011-07-21 09:43:34 -07004279 }
Paul Turner58088ad2011-07-21 09:43:31 -07004280
Nikhil Raoe8da1b12011-07-21 09:43:40 -07004281 /* account preceding periods in which throttling occurred */
4282 cfs_b->nr_throttled += overrun;
4283
Paul Turner671fd9d2011-07-21 09:43:34 -07004284 runtime_expires = cfs_b->runtime_expires;
Paul Turner671fd9d2011-07-21 09:43:34 -07004285
4286 /*
Ben Segallc06f04c2014-06-20 15:21:20 -07004287 * This check is repeated as we are holding onto the new bandwidth while
4288 * we unthrottle. This can potentially race with an unthrottled group
4289 * trying to acquire new bandwidth from the global pool. This can result
4290 * in us over-using our runtime if it is all used during this loop, but
4291 * only by limited amounts in that extreme case.
Paul Turner671fd9d2011-07-21 09:43:34 -07004292 */
Ben Segallc06f04c2014-06-20 15:21:20 -07004293 while (throttled && cfs_b->runtime > 0) {
4294 runtime = cfs_b->runtime;
Paul Turner671fd9d2011-07-21 09:43:34 -07004295 raw_spin_unlock(&cfs_b->lock);
4296 /* we can't nest cfs_b->lock while distributing bandwidth */
4297 runtime = distribute_cfs_runtime(cfs_b, runtime,
4298 runtime_expires);
4299 raw_spin_lock(&cfs_b->lock);
4300
4301 throttled = !list_empty(&cfs_b->throttled_cfs_rq);
Ben Segallc06f04c2014-06-20 15:21:20 -07004302
4303 cfs_b->runtime -= min(runtime, cfs_b->runtime);
Paul Turner671fd9d2011-07-21 09:43:34 -07004304 }
4305
Paul Turner671fd9d2011-07-21 09:43:34 -07004306 /*
4307 * While we are ensured activity in the period following an
4308 * unthrottle, this also covers the case in which the new bandwidth is
4309 * insufficient to cover the existing bandwidth deficit. (Forcing the
4310 * timer to remain active while there are any throttled entities.)
4311 */
4312 cfs_b->idle = 0;
Paul Turner58088ad2011-07-21 09:43:31 -07004313
Ben Segall51f21762014-05-19 15:49:45 -07004314 return 0;
4315
4316out_deactivate:
Ben Segall51f21762014-05-19 15:49:45 -07004317 return 1;
Paul Turner58088ad2011-07-21 09:43:31 -07004318}
Paul Turnerd3d9dc32011-07-21 09:43:39 -07004319
Paul Turnerd8b49862011-07-21 09:43:41 -07004320/* a cfs_rq won't donate quota below this amount */
4321static const u64 min_cfs_rq_runtime = 1 * NSEC_PER_MSEC;
4322/* minimum remaining period time to redistribute slack quota */
4323static const u64 min_bandwidth_expiration = 2 * NSEC_PER_MSEC;
4324/* how long we wait to gather additional slack before distributing */
4325static const u64 cfs_bandwidth_slack_period = 5 * NSEC_PER_MSEC;
4326
Ben Segalldb06e782013-10-16 11:16:17 -07004327/*
4328 * Are we near the end of the current quota period?
4329 *
4330 * Requires cfs_b->lock for hrtimer_expires_remaining to be safe against the
Thomas Gleixner4961b6e2015-04-14 21:09:05 +00004331 * hrtimer base being cleared by hrtimer_start. In the case of
Ben Segalldb06e782013-10-16 11:16:17 -07004332 * migrate_hrtimers, base is never cleared, so we are fine.
4333 */
Paul Turnerd8b49862011-07-21 09:43:41 -07004334static int runtime_refresh_within(struct cfs_bandwidth *cfs_b, u64 min_expire)
4335{
4336 struct hrtimer *refresh_timer = &cfs_b->period_timer;
4337 u64 remaining;
4338
4339 /* if the call-back is running a quota refresh is already occurring */
4340 if (hrtimer_callback_running(refresh_timer))
4341 return 1;
4342
4343 /* is a quota refresh about to occur? */
4344 remaining = ktime_to_ns(hrtimer_expires_remaining(refresh_timer));
4345 if (remaining < min_expire)
4346 return 1;
4347
4348 return 0;
4349}
4350
4351static void start_cfs_slack_bandwidth(struct cfs_bandwidth *cfs_b)
4352{
4353 u64 min_left = cfs_bandwidth_slack_period + min_bandwidth_expiration;
4354
4355 /* if there's a quota refresh soon don't bother with slack */
4356 if (runtime_refresh_within(cfs_b, min_left))
4357 return;
4358
Peter Zijlstra4cfafd32015-05-14 12:23:11 +02004359 hrtimer_start(&cfs_b->slack_timer,
4360 ns_to_ktime(cfs_bandwidth_slack_period),
4361 HRTIMER_MODE_REL);
Paul Turnerd8b49862011-07-21 09:43:41 -07004362}
4363
4364/* we know any runtime found here is valid as update_curr() precedes return */
4365static void __return_cfs_rq_runtime(struct cfs_rq *cfs_rq)
4366{
4367 struct cfs_bandwidth *cfs_b = tg_cfs_bandwidth(cfs_rq->tg);
4368 s64 slack_runtime = cfs_rq->runtime_remaining - min_cfs_rq_runtime;
4369
4370 if (slack_runtime <= 0)
4371 return;
4372
4373 raw_spin_lock(&cfs_b->lock);
4374 if (cfs_b->quota != RUNTIME_INF &&
4375 cfs_rq->runtime_expires == cfs_b->runtime_expires) {
4376 cfs_b->runtime += slack_runtime;
4377
4378 /* we are under rq->lock, defer unthrottling using a timer */
4379 if (cfs_b->runtime > sched_cfs_bandwidth_slice() &&
4380 !list_empty(&cfs_b->throttled_cfs_rq))
4381 start_cfs_slack_bandwidth(cfs_b);
4382 }
4383 raw_spin_unlock(&cfs_b->lock);
4384
4385 /* even if it's not valid for return we don't want to try again */
4386 cfs_rq->runtime_remaining -= slack_runtime;
4387}
4388
4389static __always_inline void return_cfs_rq_runtime(struct cfs_rq *cfs_rq)
4390{
Paul Turner56f570e2011-11-07 20:26:33 -08004391 if (!cfs_bandwidth_used())
4392 return;
4393
Paul Turnerfccfdc62011-11-07 20:26:34 -08004394 if (!cfs_rq->runtime_enabled || cfs_rq->nr_running)
Paul Turnerd8b49862011-07-21 09:43:41 -07004395 return;
4396
4397 __return_cfs_rq_runtime(cfs_rq);
4398}
4399
4400/*
4401 * This is done with a timer (instead of inline with bandwidth return) since
4402 * it's necessary to juggle rq->locks to unthrottle their respective cfs_rqs.
4403 */
4404static void do_sched_cfs_slack_timer(struct cfs_bandwidth *cfs_b)
4405{
4406 u64 runtime = 0, slice = sched_cfs_bandwidth_slice();
4407 u64 expires;
4408
4409 /* confirm we're still not at a refresh boundary */
Paul Turnerd8b49862011-07-21 09:43:41 -07004410 raw_spin_lock(&cfs_b->lock);
Ben Segalldb06e782013-10-16 11:16:17 -07004411 if (runtime_refresh_within(cfs_b, min_bandwidth_expiration)) {
4412 raw_spin_unlock(&cfs_b->lock);
4413 return;
4414 }
4415
Ben Segallc06f04c2014-06-20 15:21:20 -07004416 if (cfs_b->quota != RUNTIME_INF && cfs_b->runtime > slice)
Paul Turnerd8b49862011-07-21 09:43:41 -07004417 runtime = cfs_b->runtime;
Ben Segallc06f04c2014-06-20 15:21:20 -07004418
Paul Turnerd8b49862011-07-21 09:43:41 -07004419 expires = cfs_b->runtime_expires;
4420 raw_spin_unlock(&cfs_b->lock);
4421
4422 if (!runtime)
4423 return;
4424
4425 runtime = distribute_cfs_runtime(cfs_b, runtime, expires);
4426
4427 raw_spin_lock(&cfs_b->lock);
4428 if (expires == cfs_b->runtime_expires)
Ben Segallc06f04c2014-06-20 15:21:20 -07004429 cfs_b->runtime -= min(runtime, cfs_b->runtime);
Paul Turnerd8b49862011-07-21 09:43:41 -07004430 raw_spin_unlock(&cfs_b->lock);
4431}
4432
Paul Turnerd3d9dc32011-07-21 09:43:39 -07004433/*
4434 * When a group wakes up we want to make sure that its quota is not already
4435 * expired/exceeded, otherwise it may be allowed to steal additional ticks of
4436 * runtime as update_curr() throttling can not not trigger until it's on-rq.
4437 */
4438static void check_enqueue_throttle(struct cfs_rq *cfs_rq)
4439{
Paul Turner56f570e2011-11-07 20:26:33 -08004440 if (!cfs_bandwidth_used())
4441 return;
4442
Paul Turnerd3d9dc32011-07-21 09:43:39 -07004443 /* an active group must be handled by the update_curr()->put() path */
4444 if (!cfs_rq->runtime_enabled || cfs_rq->curr)
4445 return;
4446
4447 /* ensure the group is not already throttled */
4448 if (cfs_rq_throttled(cfs_rq))
4449 return;
4450
4451 /* update runtime allocation */
4452 account_cfs_rq_runtime(cfs_rq, 0);
4453 if (cfs_rq->runtime_remaining <= 0)
4454 throttle_cfs_rq(cfs_rq);
4455}
4456
Peter Zijlstra55e16d32016-06-22 15:14:26 +02004457static void sync_throttle(struct task_group *tg, int cpu)
4458{
4459 struct cfs_rq *pcfs_rq, *cfs_rq;
4460
4461 if (!cfs_bandwidth_used())
4462 return;
4463
4464 if (!tg->parent)
4465 return;
4466
4467 cfs_rq = tg->cfs_rq[cpu];
4468 pcfs_rq = tg->parent->cfs_rq[cpu];
4469
4470 cfs_rq->throttle_count = pcfs_rq->throttle_count;
Xunlei Pangb8922122016-07-09 15:54:22 +08004471 cfs_rq->throttled_clock_task = rq_clock_task(cpu_rq(cpu));
Peter Zijlstra55e16d32016-06-22 15:14:26 +02004472}
4473
Paul Turnerd3d9dc32011-07-21 09:43:39 -07004474/* conditionally throttle active cfs_rq's from put_prev_entity() */
Peter Zijlstra678d5712012-02-11 06:05:00 +01004475static bool check_cfs_rq_runtime(struct cfs_rq *cfs_rq)
Paul Turnerd3d9dc32011-07-21 09:43:39 -07004476{
Paul Turner56f570e2011-11-07 20:26:33 -08004477 if (!cfs_bandwidth_used())
Peter Zijlstra678d5712012-02-11 06:05:00 +01004478 return false;
Paul Turner56f570e2011-11-07 20:26:33 -08004479
Paul Turnerd3d9dc32011-07-21 09:43:39 -07004480 if (likely(!cfs_rq->runtime_enabled || cfs_rq->runtime_remaining > 0))
Peter Zijlstra678d5712012-02-11 06:05:00 +01004481 return false;
Paul Turnerd3d9dc32011-07-21 09:43:39 -07004482
4483 /*
4484 * it's possible for a throttled entity to be forced into a running
4485 * state (e.g. set_curr_task), in this case we're finished.
4486 */
4487 if (cfs_rq_throttled(cfs_rq))
Peter Zijlstra678d5712012-02-11 06:05:00 +01004488 return true;
Paul Turnerd3d9dc32011-07-21 09:43:39 -07004489
4490 throttle_cfs_rq(cfs_rq);
Peter Zijlstra678d5712012-02-11 06:05:00 +01004491 return true;
Paul Turnerd3d9dc32011-07-21 09:43:39 -07004492}
Peter Zijlstra029632f2011-10-25 10:00:11 +02004493
Peter Zijlstra029632f2011-10-25 10:00:11 +02004494static enum hrtimer_restart sched_cfs_slack_timer(struct hrtimer *timer)
4495{
4496 struct cfs_bandwidth *cfs_b =
4497 container_of(timer, struct cfs_bandwidth, slack_timer);
Peter Zijlstra77a4d1a2015-04-15 11:41:57 +02004498
Peter Zijlstra029632f2011-10-25 10:00:11 +02004499 do_sched_cfs_slack_timer(cfs_b);
4500
4501 return HRTIMER_NORESTART;
4502}
4503
4504static enum hrtimer_restart sched_cfs_period_timer(struct hrtimer *timer)
4505{
4506 struct cfs_bandwidth *cfs_b =
4507 container_of(timer, struct cfs_bandwidth, period_timer);
Peter Zijlstra029632f2011-10-25 10:00:11 +02004508 int overrun;
4509 int idle = 0;
4510
Ben Segall51f21762014-05-19 15:49:45 -07004511 raw_spin_lock(&cfs_b->lock);
Peter Zijlstra029632f2011-10-25 10:00:11 +02004512 for (;;) {
Peter Zijlstra77a4d1a2015-04-15 11:41:57 +02004513 overrun = hrtimer_forward_now(timer, cfs_b->period);
Peter Zijlstra029632f2011-10-25 10:00:11 +02004514 if (!overrun)
4515 break;
4516
4517 idle = do_sched_cfs_period_timer(cfs_b, overrun);
4518 }
Peter Zijlstra4cfafd32015-05-14 12:23:11 +02004519 if (idle)
4520 cfs_b->period_active = 0;
Ben Segall51f21762014-05-19 15:49:45 -07004521 raw_spin_unlock(&cfs_b->lock);
Peter Zijlstra029632f2011-10-25 10:00:11 +02004522
4523 return idle ? HRTIMER_NORESTART : HRTIMER_RESTART;
4524}
4525
4526void init_cfs_bandwidth(struct cfs_bandwidth *cfs_b)
4527{
4528 raw_spin_lock_init(&cfs_b->lock);
4529 cfs_b->runtime = 0;
4530 cfs_b->quota = RUNTIME_INF;
4531 cfs_b->period = ns_to_ktime(default_cfs_period());
4532
4533 INIT_LIST_HEAD(&cfs_b->throttled_cfs_rq);
Peter Zijlstra4cfafd32015-05-14 12:23:11 +02004534 hrtimer_init(&cfs_b->period_timer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS_PINNED);
Peter Zijlstra029632f2011-10-25 10:00:11 +02004535 cfs_b->period_timer.function = sched_cfs_period_timer;
4536 hrtimer_init(&cfs_b->slack_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
4537 cfs_b->slack_timer.function = sched_cfs_slack_timer;
4538}
4539
4540static void init_cfs_rq_runtime(struct cfs_rq *cfs_rq)
4541{
4542 cfs_rq->runtime_enabled = 0;
4543 INIT_LIST_HEAD(&cfs_rq->throttled_list);
4544}
4545
Peter Zijlstra77a4d1a2015-04-15 11:41:57 +02004546void start_cfs_bandwidth(struct cfs_bandwidth *cfs_b)
Peter Zijlstra029632f2011-10-25 10:00:11 +02004547{
Peter Zijlstra4cfafd32015-05-14 12:23:11 +02004548 lockdep_assert_held(&cfs_b->lock);
Peter Zijlstra029632f2011-10-25 10:00:11 +02004549
Peter Zijlstra4cfafd32015-05-14 12:23:11 +02004550 if (!cfs_b->period_active) {
4551 cfs_b->period_active = 1;
4552 hrtimer_forward_now(&cfs_b->period_timer, cfs_b->period);
4553 hrtimer_start_expires(&cfs_b->period_timer, HRTIMER_MODE_ABS_PINNED);
4554 }
Peter Zijlstra029632f2011-10-25 10:00:11 +02004555}
4556
4557static void destroy_cfs_bandwidth(struct cfs_bandwidth *cfs_b)
4558{
Tetsuo Handa7f1a1692014-12-25 15:51:21 +09004559 /* init_cfs_bandwidth() was not called */
4560 if (!cfs_b->throttled_cfs_rq.next)
4561 return;
4562
Peter Zijlstra029632f2011-10-25 10:00:11 +02004563 hrtimer_cancel(&cfs_b->period_timer);
4564 hrtimer_cancel(&cfs_b->slack_timer);
4565}
4566
Kirill Tkhai0e59bda2014-06-25 12:19:42 +04004567static void __maybe_unused update_runtime_enabled(struct rq *rq)
4568{
4569 struct cfs_rq *cfs_rq;
4570
4571 for_each_leaf_cfs_rq(rq, cfs_rq) {
4572 struct cfs_bandwidth *cfs_b = &cfs_rq->tg->cfs_bandwidth;
4573
4574 raw_spin_lock(&cfs_b->lock);
4575 cfs_rq->runtime_enabled = cfs_b->quota != RUNTIME_INF;
4576 raw_spin_unlock(&cfs_b->lock);
4577 }
4578}
4579
Arnd Bergmann38dc3342013-01-25 14:14:22 +00004580static void __maybe_unused unthrottle_offline_cfs_rqs(struct rq *rq)
Peter Zijlstra029632f2011-10-25 10:00:11 +02004581{
4582 struct cfs_rq *cfs_rq;
4583
4584 for_each_leaf_cfs_rq(rq, cfs_rq) {
Peter Zijlstra029632f2011-10-25 10:00:11 +02004585 if (!cfs_rq->runtime_enabled)
4586 continue;
4587
4588 /*
4589 * clock_task is not advancing so we just need to make sure
4590 * there's some valid quota amount
4591 */
Ben Segall51f21762014-05-19 15:49:45 -07004592 cfs_rq->runtime_remaining = 1;
Kirill Tkhai0e59bda2014-06-25 12:19:42 +04004593 /*
4594 * Offline rq is schedulable till cpu is completely disabled
4595 * in take_cpu_down(), so we prevent new cfs throttling here.
4596 */
4597 cfs_rq->runtime_enabled = 0;
4598
Peter Zijlstra029632f2011-10-25 10:00:11 +02004599 if (cfs_rq_throttled(cfs_rq))
4600 unthrottle_cfs_rq(cfs_rq);
4601 }
4602}
4603
4604#else /* CONFIG_CFS_BANDWIDTH */
Paul Turnerf1b17282012-10-04 13:18:31 +02004605static inline u64 cfs_rq_clock_task(struct cfs_rq *cfs_rq)
4606{
Frederic Weisbecker78becc22013-04-12 01:51:02 +02004607 return rq_clock_task(rq_of(cfs_rq));
Paul Turnerf1b17282012-10-04 13:18:31 +02004608}
4609
Peter Zijlstra9dbdb152013-11-18 18:27:06 +01004610static void account_cfs_rq_runtime(struct cfs_rq *cfs_rq, u64 delta_exec) {}
Peter Zijlstra678d5712012-02-11 06:05:00 +01004611static bool check_cfs_rq_runtime(struct cfs_rq *cfs_rq) { return false; }
Paul Turnerd3d9dc32011-07-21 09:43:39 -07004612static void check_enqueue_throttle(struct cfs_rq *cfs_rq) {}
Peter Zijlstra55e16d32016-06-22 15:14:26 +02004613static inline void sync_throttle(struct task_group *tg, int cpu) {}
Peter Zijlstra6c16a6d2012-03-21 13:07:16 -07004614static __always_inline void return_cfs_rq_runtime(struct cfs_rq *cfs_rq) {}
Paul Turner85dac902011-07-21 09:43:33 -07004615
4616static inline int cfs_rq_throttled(struct cfs_rq *cfs_rq)
4617{
4618 return 0;
4619}
Paul Turner64660c82011-07-21 09:43:36 -07004620
4621static inline int throttled_hierarchy(struct cfs_rq *cfs_rq)
4622{
4623 return 0;
4624}
4625
4626static inline int throttled_lb_pair(struct task_group *tg,
4627 int src_cpu, int dest_cpu)
4628{
4629 return 0;
4630}
Peter Zijlstra029632f2011-10-25 10:00:11 +02004631
4632void init_cfs_bandwidth(struct cfs_bandwidth *cfs_b) {}
4633
4634#ifdef CONFIG_FAIR_GROUP_SCHED
4635static void init_cfs_rq_runtime(struct cfs_rq *cfs_rq) {}
Paul Turnerab84d312011-07-21 09:43:28 -07004636#endif
4637
Peter Zijlstra029632f2011-10-25 10:00:11 +02004638static inline struct cfs_bandwidth *tg_cfs_bandwidth(struct task_group *tg)
4639{
4640 return NULL;
4641}
4642static inline void destroy_cfs_bandwidth(struct cfs_bandwidth *cfs_b) {}
Kirill Tkhai0e59bda2014-06-25 12:19:42 +04004643static inline void update_runtime_enabled(struct rq *rq) {}
Peter Boonstoppela4c96ae2012-08-09 15:34:47 -07004644static inline void unthrottle_offline_cfs_rqs(struct rq *rq) {}
Peter Zijlstra029632f2011-10-25 10:00:11 +02004645
4646#endif /* CONFIG_CFS_BANDWIDTH */
4647
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02004648/**************************************************
4649 * CFS operations on tasks:
4650 */
4651
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01004652#ifdef CONFIG_SCHED_HRTICK
4653static void hrtick_start_fair(struct rq *rq, struct task_struct *p)
4654{
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01004655 struct sched_entity *se = &p->se;
4656 struct cfs_rq *cfs_rq = cfs_rq_of(se);
4657
Peter Zijlstra9148a3a2016-09-20 22:34:51 +02004658 SCHED_WARN_ON(task_rq(p) != rq);
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01004659
Srivatsa Vaddagiri8bf46a32016-09-16 18:28:51 -07004660 if (rq->cfs.h_nr_running > 1) {
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01004661 u64 slice = sched_slice(cfs_rq, se);
4662 u64 ran = se->sum_exec_runtime - se->prev_sum_exec_runtime;
4663 s64 delta = slice - ran;
4664
4665 if (delta < 0) {
4666 if (rq->curr == p)
Kirill Tkhai88751252014-06-29 00:03:57 +04004667 resched_curr(rq);
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01004668 return;
4669 }
Peter Zijlstra31656512008-07-18 18:01:23 +02004670 hrtick_start(rq, delta);
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01004671 }
4672}
Peter Zijlstraa4c2f002008-10-17 19:27:03 +02004673
4674/*
4675 * called from enqueue/dequeue and updates the hrtick when the
4676 * current task is from our class and nr_running is low enough
4677 * to matter.
4678 */
4679static void hrtick_update(struct rq *rq)
4680{
4681 struct task_struct *curr = rq->curr;
4682
Mike Galbraithb39e66e2011-11-22 15:20:07 +01004683 if (!hrtick_enabled(rq) || curr->sched_class != &fair_sched_class)
Peter Zijlstraa4c2f002008-10-17 19:27:03 +02004684 return;
4685
4686 if (cfs_rq_of(&curr->se)->nr_running < sched_nr_latency)
4687 hrtick_start_fair(rq, curr);
4688}
Dhaval Giani55e12e52008-06-24 23:39:43 +05304689#else /* !CONFIG_SCHED_HRTICK */
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01004690static inline void
4691hrtick_start_fair(struct rq *rq, struct task_struct *p)
4692{
4693}
Peter Zijlstraa4c2f002008-10-17 19:27:03 +02004694
4695static inline void hrtick_update(struct rq *rq)
4696{
4697}
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01004698#endif
4699
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02004700/*
4701 * The enqueue_task method is called before nr_running is
4702 * increased. Here we update the fair scheduling stats and
4703 * then put the task into the rbtree:
4704 */
Thomas Gleixnerea87bb72010-01-20 20:58:57 +00004705static void
Peter Zijlstra371fd7e2010-03-24 16:38:48 +01004706enqueue_task_fair(struct rq *rq, struct task_struct *p, int flags)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02004707{
4708 struct cfs_rq *cfs_rq;
Peter Zijlstra62fb1852008-02-25 17:34:02 +01004709 struct sched_entity *se = &p->se;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02004710
Rafael J. Wysocki8c34ab12016-09-09 23:59:33 +02004711 /*
4712 * If in_iowait is set, the code below may not trigger any cpufreq
4713 * utilization updates, so do it here explicitly with the IOWAIT flag
4714 * passed.
4715 */
4716 if (p->in_iowait)
4717 cpufreq_update_this_cpu(rq, SCHED_CPUFREQ_IOWAIT);
4718
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02004719 for_each_sched_entity(se) {
Peter Zijlstra62fb1852008-02-25 17:34:02 +01004720 if (se->on_rq)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02004721 break;
4722 cfs_rq = cfs_rq_of(se);
Peter Zijlstra88ec22d2009-12-16 18:04:41 +01004723 enqueue_entity(cfs_rq, se, flags);
Paul Turner85dac902011-07-21 09:43:33 -07004724
4725 /*
4726 * end evaluation on encountering a throttled cfs_rq
4727 *
4728 * note: in the case of encountering a throttled cfs_rq we will
4729 * post the final h_nr_running increment below.
Peter Zijlstrae210bff2016-06-16 18:51:48 +02004730 */
Paul Turner85dac902011-07-21 09:43:33 -07004731 if (cfs_rq_throttled(cfs_rq))
4732 break;
Paul Turner953bfcd2011-07-21 09:43:27 -07004733 cfs_rq->h_nr_running++;
Paul Turner85dac902011-07-21 09:43:33 -07004734
Peter Zijlstra88ec22d2009-12-16 18:04:41 +01004735 flags = ENQUEUE_WAKEUP;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02004736 }
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01004737
Peter Zijlstra2069dd72010-11-15 15:47:00 -08004738 for_each_sched_entity(se) {
Lin Ming0f317142011-07-22 09:14:31 +08004739 cfs_rq = cfs_rq_of(se);
Paul Turner953bfcd2011-07-21 09:43:27 -07004740 cfs_rq->h_nr_running++;
Peter Zijlstra2069dd72010-11-15 15:47:00 -08004741
Paul Turner85dac902011-07-21 09:43:33 -07004742 if (cfs_rq_throttled(cfs_rq))
4743 break;
4744
Vincent Guittotd31b1a62016-11-08 10:53:44 +01004745 update_load_avg(se, UPDATE_TG);
Linus Torvalds17bc14b2012-12-14 07:20:43 -08004746 update_cfs_shares(cfs_rq);
Peter Zijlstra2069dd72010-11-15 15:47:00 -08004747 }
4748
Yuyang Ducd126af2015-07-15 08:04:36 +08004749 if (!se)
Kirill Tkhai72465442014-05-09 03:00:14 +04004750 add_nr_running(rq, 1);
Yuyang Ducd126af2015-07-15 08:04:36 +08004751
Peter Zijlstraa4c2f002008-10-17 19:27:03 +02004752 hrtick_update(rq);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02004753}
4754
Venkatesh Pallipadi2f368252011-04-14 10:30:53 -07004755static void set_next_buddy(struct sched_entity *se);
4756
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02004757/*
4758 * The dequeue_task method is called before nr_running is
4759 * decreased. We remove the task from the rbtree and
4760 * update the fair scheduling stats:
4761 */
Peter Zijlstra371fd7e2010-03-24 16:38:48 +01004762static void dequeue_task_fair(struct rq *rq, struct task_struct *p, int flags)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02004763{
4764 struct cfs_rq *cfs_rq;
Peter Zijlstra62fb1852008-02-25 17:34:02 +01004765 struct sched_entity *se = &p->se;
Venkatesh Pallipadi2f368252011-04-14 10:30:53 -07004766 int task_sleep = flags & DEQUEUE_SLEEP;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02004767
4768 for_each_sched_entity(se) {
4769 cfs_rq = cfs_rq_of(se);
Peter Zijlstra371fd7e2010-03-24 16:38:48 +01004770 dequeue_entity(cfs_rq, se, flags);
Paul Turner85dac902011-07-21 09:43:33 -07004771
4772 /*
4773 * end evaluation on encountering a throttled cfs_rq
4774 *
4775 * note: in the case of encountering a throttled cfs_rq we will
4776 * post the final h_nr_running decrement below.
4777 */
4778 if (cfs_rq_throttled(cfs_rq))
4779 break;
Paul Turner953bfcd2011-07-21 09:43:27 -07004780 cfs_rq->h_nr_running--;
Peter Zijlstra2069dd72010-11-15 15:47:00 -08004781
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02004782 /* Don't dequeue parent if it has other entities besides us */
Venkatesh Pallipadi2f368252011-04-14 10:30:53 -07004783 if (cfs_rq->load.weight) {
Konstantin Khlebnikov754bd592016-06-16 15:57:15 +03004784 /* Avoid re-evaluating load for this entity: */
4785 se = parent_entity(se);
Venkatesh Pallipadi2f368252011-04-14 10:30:53 -07004786 /*
4787 * Bias pick_next to pick a task from this cfs_rq, as
4788 * p is sleeping when it is within its sched_slice.
4789 */
Konstantin Khlebnikov754bd592016-06-16 15:57:15 +03004790 if (task_sleep && se && !throttled_hierarchy(cfs_rq))
4791 set_next_buddy(se);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02004792 break;
Venkatesh Pallipadi2f368252011-04-14 10:30:53 -07004793 }
Peter Zijlstra371fd7e2010-03-24 16:38:48 +01004794 flags |= DEQUEUE_SLEEP;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02004795 }
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01004796
Peter Zijlstra2069dd72010-11-15 15:47:00 -08004797 for_each_sched_entity(se) {
Lin Ming0f317142011-07-22 09:14:31 +08004798 cfs_rq = cfs_rq_of(se);
Paul Turner953bfcd2011-07-21 09:43:27 -07004799 cfs_rq->h_nr_running--;
Peter Zijlstra2069dd72010-11-15 15:47:00 -08004800
Paul Turner85dac902011-07-21 09:43:33 -07004801 if (cfs_rq_throttled(cfs_rq))
4802 break;
4803
Vincent Guittotd31b1a62016-11-08 10:53:44 +01004804 update_load_avg(se, UPDATE_TG);
Linus Torvalds17bc14b2012-12-14 07:20:43 -08004805 update_cfs_shares(cfs_rq);
Peter Zijlstra2069dd72010-11-15 15:47:00 -08004806 }
4807
Yuyang Ducd126af2015-07-15 08:04:36 +08004808 if (!se)
Kirill Tkhai72465442014-05-09 03:00:14 +04004809 sub_nr_running(rq, 1);
Yuyang Ducd126af2015-07-15 08:04:36 +08004810
Peter Zijlstraa4c2f002008-10-17 19:27:03 +02004811 hrtick_update(rq);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02004812}
4813
Gregory Haskinse7693a32008-01-25 21:08:09 +01004814#ifdef CONFIG_SMP
Peter Zijlstra10e2f1a2016-05-09 10:38:05 +02004815
4816/* Working cpumask for: load_balance, load_balance_newidle. */
4817DEFINE_PER_CPU(cpumask_var_t, load_balance_mask);
4818DEFINE_PER_CPU(cpumask_var_t, select_idle_mask);
4819
Frederic Weisbecker9fd81dd2016-04-19 17:36:51 +02004820#ifdef CONFIG_NO_HZ_COMMON
Peter Zijlstra3289bdb2015-04-14 13:19:42 +02004821/*
4822 * per rq 'load' arrray crap; XXX kill this.
4823 */
4824
4825/*
Peter Zijlstrad937cdc2015-10-19 13:49:30 +02004826 * The exact cpuload calculated at every tick would be:
Peter Zijlstra3289bdb2015-04-14 13:19:42 +02004827 *
Peter Zijlstrad937cdc2015-10-19 13:49:30 +02004828 * load' = (1 - 1/2^i) * load + (1/2^i) * cur_load
4829 *
4830 * If a cpu misses updates for n ticks (as it was idle) and update gets
4831 * called on the n+1-th tick when cpu may be busy, then we have:
4832 *
4833 * load_n = (1 - 1/2^i)^n * load_0
4834 * load_n+1 = (1 - 1/2^i) * load_n + (1/2^i) * cur_load
Peter Zijlstra3289bdb2015-04-14 13:19:42 +02004835 *
4836 * decay_load_missed() below does efficient calculation of
Peter Zijlstrad937cdc2015-10-19 13:49:30 +02004837 *
4838 * load' = (1 - 1/2^i)^n * load
4839 *
4840 * Because x^(n+m) := x^n * x^m we can decompose any x^n in power-of-2 factors.
4841 * This allows us to precompute the above in said factors, thereby allowing the
4842 * reduction of an arbitrary n in O(log_2 n) steps. (See also
4843 * fixed_power_int())
Peter Zijlstra3289bdb2015-04-14 13:19:42 +02004844 *
4845 * The calculation is approximated on a 128 point scale.
Peter Zijlstra3289bdb2015-04-14 13:19:42 +02004846 */
4847#define DEGRADE_SHIFT 7
Peter Zijlstrad937cdc2015-10-19 13:49:30 +02004848
4849static const u8 degrade_zero_ticks[CPU_LOAD_IDX_MAX] = {0, 8, 32, 64, 128};
4850static const u8 degrade_factor[CPU_LOAD_IDX_MAX][DEGRADE_SHIFT + 1] = {
4851 { 0, 0, 0, 0, 0, 0, 0, 0 },
4852 { 64, 32, 8, 0, 0, 0, 0, 0 },
4853 { 96, 72, 40, 12, 1, 0, 0, 0 },
4854 { 112, 98, 75, 43, 15, 1, 0, 0 },
4855 { 120, 112, 98, 76, 45, 16, 2, 0 }
4856};
Peter Zijlstra3289bdb2015-04-14 13:19:42 +02004857
4858/*
4859 * Update cpu_load for any missed ticks, due to tickless idle. The backlog
4860 * would be when CPU is idle and so we just decay the old load without
4861 * adding any new load.
4862 */
4863static unsigned long
4864decay_load_missed(unsigned long load, unsigned long missed_updates, int idx)
4865{
4866 int j = 0;
4867
4868 if (!missed_updates)
4869 return load;
4870
4871 if (missed_updates >= degrade_zero_ticks[idx])
4872 return 0;
4873
4874 if (idx == 1)
4875 return load >> missed_updates;
4876
4877 while (missed_updates) {
4878 if (missed_updates % 2)
4879 load = (load * degrade_factor[idx][j]) >> DEGRADE_SHIFT;
4880
4881 missed_updates >>= 1;
4882 j++;
4883 }
4884 return load;
4885}
Frederic Weisbecker9fd81dd2016-04-19 17:36:51 +02004886#endif /* CONFIG_NO_HZ_COMMON */
Peter Zijlstra3289bdb2015-04-14 13:19:42 +02004887
Byungchul Park59543272015-10-14 18:47:35 +09004888/**
Frederic Weisbeckercee1afc2016-04-13 15:56:50 +02004889 * __cpu_load_update - update the rq->cpu_load[] statistics
Byungchul Park59543272015-10-14 18:47:35 +09004890 * @this_rq: The rq to update statistics for
4891 * @this_load: The current load
4892 * @pending_updates: The number of missed updates
Byungchul Park59543272015-10-14 18:47:35 +09004893 *
Peter Zijlstra3289bdb2015-04-14 13:19:42 +02004894 * Update rq->cpu_load[] statistics. This function is usually called every
Byungchul Park59543272015-10-14 18:47:35 +09004895 * scheduler tick (TICK_NSEC).
4896 *
4897 * This function computes a decaying average:
4898 *
4899 * load[i]' = (1 - 1/2^i) * load[i] + (1/2^i) * load
4900 *
4901 * Because of NOHZ it might not get called on every tick which gives need for
4902 * the @pending_updates argument.
4903 *
4904 * load[i]_n = (1 - 1/2^i) * load[i]_n-1 + (1/2^i) * load_n-1
4905 * = A * load[i]_n-1 + B ; A := (1 - 1/2^i), B := (1/2^i) * load
4906 * = A * (A * load[i]_n-2 + B) + B
4907 * = A * (A * (A * load[i]_n-3 + B) + B) + B
4908 * = A^3 * load[i]_n-3 + (A^2 + A + 1) * B
4909 * = A^n * load[i]_0 + (A^(n-1) + A^(n-2) + ... + 1) * B
4910 * = A^n * load[i]_0 + ((1 - A^n) / (1 - A)) * B
4911 * = (1 - 1/2^i)^n * (load[i]_0 - load) + load
4912 *
4913 * In the above we've assumed load_n := load, which is true for NOHZ_FULL as
4914 * any change in load would have resulted in the tick being turned back on.
4915 *
4916 * For regular NOHZ, this reduces to:
4917 *
4918 * load[i]_n = (1 - 1/2^i)^n * load[i]_0
4919 *
4920 * see decay_load_misses(). For NOHZ_FULL we get to subtract and add the extra
Frederic Weisbecker1f419062016-04-13 15:56:51 +02004921 * term.
Peter Zijlstra3289bdb2015-04-14 13:19:42 +02004922 */
Frederic Weisbecker1f419062016-04-13 15:56:51 +02004923static void cpu_load_update(struct rq *this_rq, unsigned long this_load,
4924 unsigned long pending_updates)
Peter Zijlstra3289bdb2015-04-14 13:19:42 +02004925{
Frederic Weisbecker9fd81dd2016-04-19 17:36:51 +02004926 unsigned long __maybe_unused tickless_load = this_rq->cpu_load[0];
Peter Zijlstra3289bdb2015-04-14 13:19:42 +02004927 int i, scale;
4928
4929 this_rq->nr_load_updates++;
4930
4931 /* Update our load: */
4932 this_rq->cpu_load[0] = this_load; /* Fasttrack for idx 0 */
4933 for (i = 1, scale = 2; i < CPU_LOAD_IDX_MAX; i++, scale += scale) {
4934 unsigned long old_load, new_load;
4935
4936 /* scale is effectively 1 << i now, and >> i divides by scale */
4937
Byungchul Park7400d3b2016-01-15 16:07:49 +09004938 old_load = this_rq->cpu_load[i];
Frederic Weisbecker9fd81dd2016-04-19 17:36:51 +02004939#ifdef CONFIG_NO_HZ_COMMON
Peter Zijlstra3289bdb2015-04-14 13:19:42 +02004940 old_load = decay_load_missed(old_load, pending_updates - 1, i);
Byungchul Park7400d3b2016-01-15 16:07:49 +09004941 if (tickless_load) {
4942 old_load -= decay_load_missed(tickless_load, pending_updates - 1, i);
4943 /*
4944 * old_load can never be a negative value because a
4945 * decayed tickless_load cannot be greater than the
4946 * original tickless_load.
4947 */
4948 old_load += tickless_load;
4949 }
Frederic Weisbecker9fd81dd2016-04-19 17:36:51 +02004950#endif
Peter Zijlstra3289bdb2015-04-14 13:19:42 +02004951 new_load = this_load;
4952 /*
4953 * Round up the averaging division if load is increasing. This
4954 * prevents us from getting stuck on 9 if the load is 10, for
4955 * example.
4956 */
4957 if (new_load > old_load)
4958 new_load += scale - 1;
4959
4960 this_rq->cpu_load[i] = (old_load * (scale - 1) + new_load) >> i;
4961 }
4962
4963 sched_avg_update(this_rq);
4964}
4965
Yuyang Du7ea241a2015-07-15 08:04:42 +08004966/* Used instead of source_load when we know the type == 0 */
4967static unsigned long weighted_cpuload(const int cpu)
4968{
4969 return cfs_rq_runnable_load_avg(&cpu_rq(cpu)->cfs);
4970}
4971
Peter Zijlstra3289bdb2015-04-14 13:19:42 +02004972#ifdef CONFIG_NO_HZ_COMMON
Frederic Weisbecker1f419062016-04-13 15:56:51 +02004973/*
4974 * There is no sane way to deal with nohz on smp when using jiffies because the
4975 * cpu doing the jiffies update might drift wrt the cpu doing the jiffy reading
4976 * causing off-by-one errors in observed deltas; {0,2} instead of {1,1}.
4977 *
4978 * Therefore we need to avoid the delta approach from the regular tick when
4979 * possible since that would seriously skew the load calculation. This is why we
4980 * use cpu_load_update_periodic() for CPUs out of nohz. However we'll rely on
4981 * jiffies deltas for updates happening while in nohz mode (idle ticks, idle
4982 * loop exit, nohz_idle_balance, nohz full exit...)
4983 *
4984 * This means we might still be one tick off for nohz periods.
4985 */
4986
4987static void cpu_load_update_nohz(struct rq *this_rq,
4988 unsigned long curr_jiffies,
4989 unsigned long load)
Frederic Weisbeckerbe68a682016-01-13 17:01:29 +01004990{
4991 unsigned long pending_updates;
4992
4993 pending_updates = curr_jiffies - this_rq->last_load_update_tick;
4994 if (pending_updates) {
4995 this_rq->last_load_update_tick = curr_jiffies;
4996 /*
4997 * In the regular NOHZ case, we were idle, this means load 0.
4998 * In the NOHZ_FULL case, we were non-idle, we should consider
4999 * its weighted load.
5000 */
Frederic Weisbecker1f419062016-04-13 15:56:51 +02005001 cpu_load_update(this_rq, load, pending_updates);
Frederic Weisbeckerbe68a682016-01-13 17:01:29 +01005002 }
5003}
5004
Peter Zijlstra3289bdb2015-04-14 13:19:42 +02005005/*
Peter Zijlstra3289bdb2015-04-14 13:19:42 +02005006 * Called from nohz_idle_balance() to update the load ratings before doing the
5007 * idle balance.
5008 */
Frederic Weisbeckercee1afc2016-04-13 15:56:50 +02005009static void cpu_load_update_idle(struct rq *this_rq)
Peter Zijlstra3289bdb2015-04-14 13:19:42 +02005010{
Peter Zijlstra3289bdb2015-04-14 13:19:42 +02005011 /*
5012 * bail if there's load or we're actually up-to-date.
5013 */
Frederic Weisbeckerbe68a682016-01-13 17:01:29 +01005014 if (weighted_cpuload(cpu_of(this_rq)))
Peter Zijlstra3289bdb2015-04-14 13:19:42 +02005015 return;
5016
Frederic Weisbecker1f419062016-04-13 15:56:51 +02005017 cpu_load_update_nohz(this_rq, READ_ONCE(jiffies), 0);
Peter Zijlstra3289bdb2015-04-14 13:19:42 +02005018}
5019
5020/*
Frederic Weisbecker1f419062016-04-13 15:56:51 +02005021 * Record CPU load on nohz entry so we know the tickless load to account
5022 * on nohz exit. cpu_load[0] happens then to be updated more frequently
5023 * than other cpu_load[idx] but it should be fine as cpu_load readers
5024 * shouldn't rely into synchronized cpu_load[*] updates.
Peter Zijlstra3289bdb2015-04-14 13:19:42 +02005025 */
Frederic Weisbecker1f419062016-04-13 15:56:51 +02005026void cpu_load_update_nohz_start(void)
Peter Zijlstra3289bdb2015-04-14 13:19:42 +02005027{
5028 struct rq *this_rq = this_rq();
Frederic Weisbecker1f419062016-04-13 15:56:51 +02005029
5030 /*
5031 * This is all lockless but should be fine. If weighted_cpuload changes
5032 * concurrently we'll exit nohz. And cpu_load write can race with
5033 * cpu_load_update_idle() but both updater would be writing the same.
5034 */
5035 this_rq->cpu_load[0] = weighted_cpuload(cpu_of(this_rq));
5036}
5037
5038/*
5039 * Account the tickless load in the end of a nohz frame.
5040 */
5041void cpu_load_update_nohz_stop(void)
5042{
Jason Low316c1608d2015-04-28 13:00:20 -07005043 unsigned long curr_jiffies = READ_ONCE(jiffies);
Frederic Weisbecker1f419062016-04-13 15:56:51 +02005044 struct rq *this_rq = this_rq();
5045 unsigned long load;
Peter Zijlstra3289bdb2015-04-14 13:19:42 +02005046
5047 if (curr_jiffies == this_rq->last_load_update_tick)
5048 return;
5049
Frederic Weisbecker1f419062016-04-13 15:56:51 +02005050 load = weighted_cpuload(cpu_of(this_rq));
Peter Zijlstra3289bdb2015-04-14 13:19:42 +02005051 raw_spin_lock(&this_rq->lock);
Matt Flemingb52fad22016-05-03 20:46:54 +01005052 update_rq_clock(this_rq);
Frederic Weisbecker1f419062016-04-13 15:56:51 +02005053 cpu_load_update_nohz(this_rq, curr_jiffies, load);
Peter Zijlstra3289bdb2015-04-14 13:19:42 +02005054 raw_spin_unlock(&this_rq->lock);
5055}
Frederic Weisbecker1f419062016-04-13 15:56:51 +02005056#else /* !CONFIG_NO_HZ_COMMON */
5057static inline void cpu_load_update_nohz(struct rq *this_rq,
5058 unsigned long curr_jiffies,
5059 unsigned long load) { }
5060#endif /* CONFIG_NO_HZ_COMMON */
5061
5062static void cpu_load_update_periodic(struct rq *this_rq, unsigned long load)
5063{
Frederic Weisbecker9fd81dd2016-04-19 17:36:51 +02005064#ifdef CONFIG_NO_HZ_COMMON
Frederic Weisbecker1f419062016-04-13 15:56:51 +02005065 /* See the mess around cpu_load_update_nohz(). */
5066 this_rq->last_load_update_tick = READ_ONCE(jiffies);
Frederic Weisbecker9fd81dd2016-04-19 17:36:51 +02005067#endif
Frederic Weisbecker1f419062016-04-13 15:56:51 +02005068 cpu_load_update(this_rq, load, 1);
5069}
Peter Zijlstra3289bdb2015-04-14 13:19:42 +02005070
5071/*
5072 * Called from scheduler_tick()
5073 */
Frederic Weisbeckercee1afc2016-04-13 15:56:50 +02005074void cpu_load_update_active(struct rq *this_rq)
Peter Zijlstra3289bdb2015-04-14 13:19:42 +02005075{
Yuyang Du7ea241a2015-07-15 08:04:42 +08005076 unsigned long load = weighted_cpuload(cpu_of(this_rq));
Frederic Weisbecker1f419062016-04-13 15:56:51 +02005077
5078 if (tick_nohz_tick_stopped())
5079 cpu_load_update_nohz(this_rq, READ_ONCE(jiffies), load);
5080 else
5081 cpu_load_update_periodic(this_rq, load);
Peter Zijlstra3289bdb2015-04-14 13:19:42 +02005082}
5083
Peter Zijlstra029632f2011-10-25 10:00:11 +02005084/*
5085 * Return a low guess at the load of a migration-source cpu weighted
5086 * according to the scheduling class and "nice" value.
5087 *
5088 * We want to under-estimate the load of migration sources, to
5089 * balance conservatively.
5090 */
5091static unsigned long source_load(int cpu, int type)
5092{
5093 struct rq *rq = cpu_rq(cpu);
5094 unsigned long total = weighted_cpuload(cpu);
5095
5096 if (type == 0 || !sched_feat(LB_BIAS))
5097 return total;
5098
5099 return min(rq->cpu_load[type-1], total);
5100}
5101
5102/*
5103 * Return a high guess at the load of a migration-target cpu weighted
5104 * according to the scheduling class and "nice" value.
5105 */
5106static unsigned long target_load(int cpu, int type)
5107{
5108 struct rq *rq = cpu_rq(cpu);
5109 unsigned long total = weighted_cpuload(cpu);
5110
5111 if (type == 0 || !sched_feat(LB_BIAS))
5112 return total;
5113
5114 return max(rq->cpu_load[type-1], total);
5115}
5116
Nicolas Pitreced549f2014-05-26 18:19:38 -04005117static unsigned long capacity_of(int cpu)
Peter Zijlstra029632f2011-10-25 10:00:11 +02005118{
Nicolas Pitreced549f2014-05-26 18:19:38 -04005119 return cpu_rq(cpu)->cpu_capacity;
Peter Zijlstra029632f2011-10-25 10:00:11 +02005120}
5121
Vincent Guittotca6d75e2015-02-27 16:54:09 +01005122static unsigned long capacity_orig_of(int cpu)
5123{
5124 return cpu_rq(cpu)->cpu_capacity_orig;
5125}
5126
Peter Zijlstra029632f2011-10-25 10:00:11 +02005127static unsigned long cpu_avg_load_per_task(int cpu)
5128{
5129 struct rq *rq = cpu_rq(cpu);
Jason Low316c1608d2015-04-28 13:00:20 -07005130 unsigned long nr_running = READ_ONCE(rq->cfs.h_nr_running);
Yuyang Du7ea241a2015-07-15 08:04:42 +08005131 unsigned long load_avg = weighted_cpuload(cpu);
Peter Zijlstra029632f2011-10-25 10:00:11 +02005132
5133 if (nr_running)
Alex Shib92486c2013-06-20 10:18:50 +08005134 return load_avg / nr_running;
Peter Zijlstra029632f2011-10-25 10:00:11 +02005135
5136 return 0;
5137}
5138
Peter Zijlstrabb3469a2008-06-27 13:41:27 +02005139#ifdef CONFIG_FAIR_GROUP_SCHED
Peter Zijlstraf5bfb7d2008-06-27 13:41:39 +02005140/*
5141 * effective_load() calculates the load change as seen from the root_task_group
5142 *
5143 * Adding load to a group doesn't make a group heavier, but can cause movement
5144 * of group shares between cpus. Assuming the shares were perfectly aligned one
5145 * can calculate the shift in shares.
Peter Zijlstracf5f0ac2011-10-13 16:52:28 +02005146 *
5147 * Calculate the effective load difference if @wl is added (subtracted) to @tg
5148 * on this @cpu and results in a total addition (subtraction) of @wg to the
5149 * total group weight.
5150 *
5151 * Given a runqueue weight distribution (rw_i) we can compute a shares
5152 * distribution (s_i) using:
5153 *
5154 * s_i = rw_i / \Sum rw_j (1)
5155 *
5156 * Suppose we have 4 CPUs and our @tg is a direct child of the root group and
5157 * has 7 equal weight tasks, distributed as below (rw_i), with the resulting
5158 * shares distribution (s_i):
5159 *
5160 * rw_i = { 2, 4, 1, 0 }
5161 * s_i = { 2/7, 4/7, 1/7, 0 }
5162 *
5163 * As per wake_affine() we're interested in the load of two CPUs (the CPU the
5164 * task used to run on and the CPU the waker is running on), we need to
5165 * compute the effect of waking a task on either CPU and, in case of a sync
5166 * wakeup, compute the effect of the current task going to sleep.
5167 *
5168 * So for a change of @wl to the local @cpu with an overall group weight change
5169 * of @wl we can compute the new shares distribution (s'_i) using:
5170 *
5171 * s'_i = (rw_i + @wl) / (@wg + \Sum rw_j) (2)
5172 *
5173 * Suppose we're interested in CPUs 0 and 1, and want to compute the load
5174 * differences in waking a task to CPU 0. The additional task changes the
5175 * weight and shares distributions like:
5176 *
5177 * rw'_i = { 3, 4, 1, 0 }
5178 * s'_i = { 3/8, 4/8, 1/8, 0 }
5179 *
5180 * We can then compute the difference in effective weight by using:
5181 *
5182 * dw_i = S * (s'_i - s_i) (3)
5183 *
5184 * Where 'S' is the group weight as seen by its parent.
5185 *
5186 * Therefore the effective change in loads on CPU 0 would be 5/56 (3/8 - 2/7)
5187 * times the weight of the group. The effect on CPU 1 would be -4/56 (4/8 -
5188 * 4/7) times the weight of the group.
Peter Zijlstraf5bfb7d2008-06-27 13:41:39 +02005189 */
Peter Zijlstra2069dd72010-11-15 15:47:00 -08005190static long effective_load(struct task_group *tg, int cpu, long wl, long wg)
Peter Zijlstrabb3469a2008-06-27 13:41:27 +02005191{
Peter Zijlstra4be9daa2008-06-27 13:41:30 +02005192 struct sched_entity *se = tg->se[cpu];
Peter Zijlstraf1d239f2008-06-27 13:41:38 +02005193
Rik van Riel9722c2d2014-01-06 11:39:12 +00005194 if (!tg->parent) /* the trivial, non-cgroup case */
Peter Zijlstraf1d239f2008-06-27 13:41:38 +02005195 return wl;
5196
Peter Zijlstra4be9daa2008-06-27 13:41:30 +02005197 for_each_sched_entity(se) {
Peter Zijlstra7dd49122016-06-24 15:53:54 +02005198 struct cfs_rq *cfs_rq = se->my_q;
5199 long W, w = cfs_rq_load_avg(cfs_rq);
Peter Zijlstrabb3469a2008-06-27 13:41:27 +02005200
Peter Zijlstra7dd49122016-06-24 15:53:54 +02005201 tg = cfs_rq->tg;
Peter Zijlstra4be9daa2008-06-27 13:41:30 +02005202
Peter Zijlstracf5f0ac2011-10-13 16:52:28 +02005203 /*
5204 * W = @wg + \Sum rw_j
5205 */
Peter Zijlstra7dd49122016-06-24 15:53:54 +02005206 W = wg + atomic_long_read(&tg->load_avg);
5207
5208 /* Ensure \Sum rw_j >= rw_i */
5209 W -= cfs_rq->tg_load_avg_contrib;
5210 W += w;
Peter Zijlstra4be9daa2008-06-27 13:41:30 +02005211
Peter Zijlstracf5f0ac2011-10-13 16:52:28 +02005212 /*
5213 * w = rw_i + @wl
5214 */
Peter Zijlstra7dd49122016-06-24 15:53:54 +02005215 w += wl;
Peter Zijlstra940959e2008-09-23 15:33:42 +02005216
Peter Zijlstracf5f0ac2011-10-13 16:52:28 +02005217 /*
5218 * wl = S * s'_i; see (2)
5219 */
5220 if (W > 0 && w < W)
Dietmar Eggemannab522e32016-08-22 15:00:41 +01005221 wl = (w * (long)scale_load_down(tg->shares)) / W;
Paul Turner977dda72011-01-14 17:57:50 -08005222 else
Dietmar Eggemannab522e32016-08-22 15:00:41 +01005223 wl = scale_load_down(tg->shares);
Peter Zijlstra940959e2008-09-23 15:33:42 +02005224
Peter Zijlstracf5f0ac2011-10-13 16:52:28 +02005225 /*
5226 * Per the above, wl is the new se->load.weight value; since
5227 * those are clipped to [MIN_SHARES, ...) do so now. See
5228 * calc_cfs_shares().
5229 */
Paul Turner977dda72011-01-14 17:57:50 -08005230 if (wl < MIN_SHARES)
5231 wl = MIN_SHARES;
Peter Zijlstracf5f0ac2011-10-13 16:52:28 +02005232
5233 /*
5234 * wl = dw_i = S * (s'_i - s_i); see (3)
5235 */
Yuyang Du9d89c252015-07-15 08:04:37 +08005236 wl -= se->avg.load_avg;
Peter Zijlstracf5f0ac2011-10-13 16:52:28 +02005237
5238 /*
5239 * Recursively apply this logic to all parent groups to compute
5240 * the final effective load change on the root group. Since
5241 * only the @tg group gets extra weight, all parent groups can
5242 * only redistribute existing shares. @wl is the shift in shares
5243 * resulting from this level per the above.
5244 */
Peter Zijlstra4be9daa2008-06-27 13:41:30 +02005245 wg = 0;
Peter Zijlstra4be9daa2008-06-27 13:41:30 +02005246 }
5247
5248 return wl;
Peter Zijlstrabb3469a2008-06-27 13:41:27 +02005249}
5250#else
Peter Zijlstra4be9daa2008-06-27 13:41:30 +02005251
Mel Gorman58d081b2013-10-07 11:29:10 +01005252static long effective_load(struct task_group *tg, int cpu, long wl, long wg)
Peter Zijlstra4be9daa2008-06-27 13:41:30 +02005253{
Peter Zijlstra83378262008-06-27 13:41:37 +02005254 return wl;
Peter Zijlstrabb3469a2008-06-27 13:41:27 +02005255}
Peter Zijlstra4be9daa2008-06-27 13:41:30 +02005256
Peter Zijlstrabb3469a2008-06-27 13:41:27 +02005257#endif
5258
Peter Zijlstrac58d25f2016-05-12 09:19:59 +02005259static void record_wakee(struct task_struct *p)
5260{
5261 /*
5262 * Only decay a single time; tasks that have less then 1 wakeup per
5263 * jiffy will not have built up many flips.
5264 */
5265 if (time_after(jiffies, current->wakee_flip_decay_ts + HZ)) {
5266 current->wakee_flips >>= 1;
5267 current->wakee_flip_decay_ts = jiffies;
5268 }
5269
5270 if (current->last_wakee != p) {
5271 current->last_wakee = p;
5272 current->wakee_flips++;
5273 }
5274}
5275
Mike Galbraith63b0e9e2015-07-14 17:39:50 +02005276/*
5277 * Detect M:N waker/wakee relationships via a switching-frequency heuristic.
Peter Zijlstrac58d25f2016-05-12 09:19:59 +02005278 *
Mike Galbraith63b0e9e2015-07-14 17:39:50 +02005279 * A waker of many should wake a different task than the one last awakened
Peter Zijlstrac58d25f2016-05-12 09:19:59 +02005280 * at a frequency roughly N times higher than one of its wakees.
5281 *
5282 * In order to determine whether we should let the load spread vs consolidating
5283 * to shared cache, we look for a minimum 'flip' frequency of llc_size in one
5284 * partner, and a factor of lls_size higher frequency in the other.
5285 *
5286 * With both conditions met, we can be relatively sure that the relationship is
5287 * non-monogamous, with partner count exceeding socket size.
5288 *
5289 * Waker/wakee being client/server, worker/dispatcher, interrupt source or
5290 * whatever is irrelevant, spread criteria is apparent partner count exceeds
5291 * socket size.
Mike Galbraith63b0e9e2015-07-14 17:39:50 +02005292 */
Michael Wang62470412013-07-04 12:55:51 +08005293static int wake_wide(struct task_struct *p)
5294{
Mike Galbraith63b0e9e2015-07-14 17:39:50 +02005295 unsigned int master = current->wakee_flips;
5296 unsigned int slave = p->wakee_flips;
Peter Zijlstra7d9ffa82013-07-04 12:56:46 +08005297 int factor = this_cpu_read(sd_llc_size);
Michael Wang62470412013-07-04 12:55:51 +08005298
Mike Galbraith63b0e9e2015-07-14 17:39:50 +02005299 if (master < slave)
5300 swap(master, slave);
5301 if (slave < factor || master < slave * factor)
5302 return 0;
5303 return 1;
Michael Wang62470412013-07-04 12:55:51 +08005304}
5305
Morten Rasmussen772bd008c2016-06-22 18:03:13 +01005306static int wake_affine(struct sched_domain *sd, struct task_struct *p,
5307 int prev_cpu, int sync)
Ingo Molnar098fb9d2008-03-16 20:36:10 +01005308{
Paul Turnere37b6a72011-01-21 20:44:59 -08005309 s64 this_load, load;
Vincent Guittotbd61c982014-08-26 13:06:50 +02005310 s64 this_eff_load, prev_eff_load;
Morten Rasmussen772bd008c2016-06-22 18:03:13 +01005311 int idx, this_cpu;
Peter Zijlstrac88d5912009-09-10 13:50:02 +02005312 struct task_group *tg;
Peter Zijlstra83378262008-06-27 13:41:37 +02005313 unsigned long weight;
Mike Galbraithb3137bc2008-05-29 11:11:41 +02005314 int balanced;
Ingo Molnar098fb9d2008-03-16 20:36:10 +01005315
Peter Zijlstrac88d5912009-09-10 13:50:02 +02005316 idx = sd->wake_idx;
5317 this_cpu = smp_processor_id();
Peter Zijlstrac88d5912009-09-10 13:50:02 +02005318 load = source_load(prev_cpu, idx);
5319 this_load = target_load(this_cpu, idx);
Ingo Molnar098fb9d2008-03-16 20:36:10 +01005320
5321 /*
Ingo Molnar098fb9d2008-03-16 20:36:10 +01005322 * If sync wakeup then subtract the (maximum possible)
5323 * effect of the currently running task from the load
5324 * of the current CPU:
5325 */
Peter Zijlstra83378262008-06-27 13:41:37 +02005326 if (sync) {
5327 tg = task_group(current);
Yuyang Du9d89c252015-07-15 08:04:37 +08005328 weight = current->se.avg.load_avg;
Ingo Molnar098fb9d2008-03-16 20:36:10 +01005329
Peter Zijlstrac88d5912009-09-10 13:50:02 +02005330 this_load += effective_load(tg, this_cpu, -weight, -weight);
Peter Zijlstra83378262008-06-27 13:41:37 +02005331 load += effective_load(tg, prev_cpu, 0, -weight);
5332 }
5333
5334 tg = task_group(p);
Yuyang Du9d89c252015-07-15 08:04:37 +08005335 weight = p->se.avg.load_avg;
Peter Zijlstra83378262008-06-27 13:41:37 +02005336
Peter Zijlstra71a29aa2009-09-07 18:28:05 +02005337 /*
5338 * In low-load situations, where prev_cpu is idle and this_cpu is idle
Peter Zijlstrac88d5912009-09-10 13:50:02 +02005339 * due to the sync cause above having dropped this_load to 0, we'll
5340 * always have an imbalance, but there's really nothing you can do
5341 * about that, so that's good too.
Peter Zijlstra71a29aa2009-09-07 18:28:05 +02005342 *
5343 * Otherwise check if either cpus are near enough in load to allow this
5344 * task to be woken on this_cpu.
5345 */
Vincent Guittotbd61c982014-08-26 13:06:50 +02005346 this_eff_load = 100;
5347 this_eff_load *= capacity_of(prev_cpu);
Peter Zijlstrae51fd5e2010-05-31 12:37:30 +02005348
Vincent Guittotbd61c982014-08-26 13:06:50 +02005349 prev_eff_load = 100 + (sd->imbalance_pct - 100) / 2;
5350 prev_eff_load *= capacity_of(this_cpu);
5351
5352 if (this_load > 0) {
Peter Zijlstrae51fd5e2010-05-31 12:37:30 +02005353 this_eff_load *= this_load +
5354 effective_load(tg, this_cpu, weight, weight);
5355
Peter Zijlstrae51fd5e2010-05-31 12:37:30 +02005356 prev_eff_load *= load + effective_load(tg, prev_cpu, 0, weight);
Vincent Guittotbd61c982014-08-26 13:06:50 +02005357 }
Peter Zijlstrae51fd5e2010-05-31 12:37:30 +02005358
Vincent Guittotbd61c982014-08-26 13:06:50 +02005359 balanced = this_eff_load <= prev_eff_load;
Mike Galbraithb3137bc2008-05-29 11:11:41 +02005360
Josh Poimboeufae928822016-06-17 12:43:24 -05005361 schedstat_inc(p->se.statistics.nr_wakeups_affine_attempts);
Mike Galbraithb3137bc2008-05-29 11:11:41 +02005362
Vincent Guittot05bfb652014-08-26 13:06:45 +02005363 if (!balanced)
5364 return 0;
Ingo Molnar098fb9d2008-03-16 20:36:10 +01005365
Josh Poimboeufae928822016-06-17 12:43:24 -05005366 schedstat_inc(sd->ttwu_move_affine);
5367 schedstat_inc(p->se.statistics.nr_wakeups_affine);
Vincent Guittot05bfb652014-08-26 13:06:45 +02005368
5369 return 1;
Ingo Molnar098fb9d2008-03-16 20:36:10 +01005370}
5371
Morten Rasmussen6a0b19c2016-10-14 14:41:08 +01005372static inline int task_util(struct task_struct *p);
5373static int cpu_util_wake(int cpu, struct task_struct *p);
5374
5375static unsigned long capacity_spare_wake(int cpu, struct task_struct *p)
5376{
5377 return capacity_orig_of(cpu) - cpu_util_wake(cpu, p);
5378}
5379
Peter Zijlstraaaee1202009-09-10 13:36:25 +02005380/*
5381 * find_idlest_group finds and returns the least busy CPU group within the
5382 * domain.
5383 */
5384static struct sched_group *
Peter Zijlstra78e7ed52009-09-03 13:16:51 +02005385find_idlest_group(struct sched_domain *sd, struct task_struct *p,
Vincent Guittotc44f2a02013-10-18 13:52:21 +02005386 int this_cpu, int sd_flag)
Gregory Haskinse7693a32008-01-25 21:08:09 +01005387{
Andi Kleenb3bd3de2010-08-10 14:17:51 -07005388 struct sched_group *idlest = NULL, *group = sd->groups;
Morten Rasmussen6a0b19c2016-10-14 14:41:08 +01005389 struct sched_group *most_spare_sg = NULL;
Peter Zijlstraaaee1202009-09-10 13:36:25 +02005390 unsigned long min_load = ULONG_MAX, this_load = 0;
Morten Rasmussen6a0b19c2016-10-14 14:41:08 +01005391 unsigned long most_spare = 0, this_spare = 0;
Vincent Guittotc44f2a02013-10-18 13:52:21 +02005392 int load_idx = sd->forkexec_idx;
Peter Zijlstraaaee1202009-09-10 13:36:25 +02005393 int imbalance = 100 + (sd->imbalance_pct-100)/2;
Gregory Haskinse7693a32008-01-25 21:08:09 +01005394
Vincent Guittotc44f2a02013-10-18 13:52:21 +02005395 if (sd_flag & SD_BALANCE_WAKE)
5396 load_idx = sd->wake_idx;
5397
Peter Zijlstraaaee1202009-09-10 13:36:25 +02005398 do {
Morten Rasmussen6a0b19c2016-10-14 14:41:08 +01005399 unsigned long load, avg_load, spare_cap, max_spare_cap;
Peter Zijlstraaaee1202009-09-10 13:36:25 +02005400 int local_group;
5401 int i;
Gregory Haskinse7693a32008-01-25 21:08:09 +01005402
Peter Zijlstraaaee1202009-09-10 13:36:25 +02005403 /* Skip over this group if it has no CPUs allowed */
5404 if (!cpumask_intersects(sched_group_cpus(group),
Peter Zijlstrafa17b502011-06-16 12:23:22 +02005405 tsk_cpus_allowed(p)))
Peter Zijlstraaaee1202009-09-10 13:36:25 +02005406 continue;
5407
5408 local_group = cpumask_test_cpu(this_cpu,
5409 sched_group_cpus(group));
5410
Morten Rasmussen6a0b19c2016-10-14 14:41:08 +01005411 /*
5412 * Tally up the load of all CPUs in the group and find
5413 * the group containing the CPU with most spare capacity.
5414 */
Peter Zijlstraaaee1202009-09-10 13:36:25 +02005415 avg_load = 0;
Morten Rasmussen6a0b19c2016-10-14 14:41:08 +01005416 max_spare_cap = 0;
Peter Zijlstraaaee1202009-09-10 13:36:25 +02005417
5418 for_each_cpu(i, sched_group_cpus(group)) {
5419 /* Bias balancing toward cpus of our domain */
5420 if (local_group)
5421 load = source_load(i, load_idx);
5422 else
5423 load = target_load(i, load_idx);
5424
5425 avg_load += load;
Morten Rasmussen6a0b19c2016-10-14 14:41:08 +01005426
5427 spare_cap = capacity_spare_wake(i, p);
5428
5429 if (spare_cap > max_spare_cap)
5430 max_spare_cap = spare_cap;
Peter Zijlstraaaee1202009-09-10 13:36:25 +02005431 }
5432
Nicolas Pitre63b2ca32014-05-26 18:19:37 -04005433 /* Adjust by relative CPU capacity of the group */
Nicolas Pitreca8ce3d2014-05-26 18:19:39 -04005434 avg_load = (avg_load * SCHED_CAPACITY_SCALE) / group->sgc->capacity;
Peter Zijlstraaaee1202009-09-10 13:36:25 +02005435
5436 if (local_group) {
5437 this_load = avg_load;
Morten Rasmussen6a0b19c2016-10-14 14:41:08 +01005438 this_spare = max_spare_cap;
5439 } else {
5440 if (avg_load < min_load) {
5441 min_load = avg_load;
5442 idlest = group;
5443 }
5444
5445 if (most_spare < max_spare_cap) {
5446 most_spare = max_spare_cap;
5447 most_spare_sg = group;
5448 }
Peter Zijlstraaaee1202009-09-10 13:36:25 +02005449 }
5450 } while (group = group->next, group != sd->groups);
5451
Morten Rasmussen6a0b19c2016-10-14 14:41:08 +01005452 /*
5453 * The cross-over point between using spare capacity or least load
5454 * is too conservative for high utilization tasks on partially
5455 * utilized systems if we require spare_capacity > task_util(p),
5456 * so we allow for some task stuffing by using
5457 * spare_capacity > task_util(p)/2.
5458 */
5459 if (this_spare > task_util(p) / 2 &&
5460 imbalance*this_spare > 100*most_spare)
5461 return NULL;
5462 else if (most_spare > task_util(p) / 2)
5463 return most_spare_sg;
5464
Peter Zijlstraaaee1202009-09-10 13:36:25 +02005465 if (!idlest || 100*this_load < imbalance*min_load)
5466 return NULL;
5467 return idlest;
5468}
5469
5470/*
5471 * find_idlest_cpu - find the idlest cpu among the cpus in group.
5472 */
5473static int
5474find_idlest_cpu(struct sched_group *group, struct task_struct *p, int this_cpu)
5475{
5476 unsigned long load, min_load = ULONG_MAX;
Nicolas Pitre83a0a962014-09-04 11:32:10 -04005477 unsigned int min_exit_latency = UINT_MAX;
5478 u64 latest_idle_timestamp = 0;
5479 int least_loaded_cpu = this_cpu;
5480 int shallowest_idle_cpu = -1;
Peter Zijlstraaaee1202009-09-10 13:36:25 +02005481 int i;
5482
Morten Rasmusseneaecf412016-06-22 18:03:14 +01005483 /* Check if we have any choice: */
5484 if (group->group_weight == 1)
5485 return cpumask_first(sched_group_cpus(group));
5486
Peter Zijlstraaaee1202009-09-10 13:36:25 +02005487 /* Traverse only the allowed CPUs */
Peter Zijlstrafa17b502011-06-16 12:23:22 +02005488 for_each_cpu_and(i, sched_group_cpus(group), tsk_cpus_allowed(p)) {
Nicolas Pitre83a0a962014-09-04 11:32:10 -04005489 if (idle_cpu(i)) {
5490 struct rq *rq = cpu_rq(i);
5491 struct cpuidle_state *idle = idle_get_state(rq);
5492 if (idle && idle->exit_latency < min_exit_latency) {
5493 /*
5494 * We give priority to a CPU whose idle state
5495 * has the smallest exit latency irrespective
5496 * of any idle timestamp.
5497 */
5498 min_exit_latency = idle->exit_latency;
5499 latest_idle_timestamp = rq->idle_stamp;
5500 shallowest_idle_cpu = i;
5501 } else if ((!idle || idle->exit_latency == min_exit_latency) &&
5502 rq->idle_stamp > latest_idle_timestamp) {
5503 /*
5504 * If equal or no active idle state, then
5505 * the most recently idled CPU might have
5506 * a warmer cache.
5507 */
5508 latest_idle_timestamp = rq->idle_stamp;
5509 shallowest_idle_cpu = i;
5510 }
Yao Dongdong9f967422014-10-28 04:08:06 +00005511 } else if (shallowest_idle_cpu == -1) {
Nicolas Pitre83a0a962014-09-04 11:32:10 -04005512 load = weighted_cpuload(i);
5513 if (load < min_load || (load == min_load && i == this_cpu)) {
5514 min_load = load;
5515 least_loaded_cpu = i;
5516 }
Gregory Haskinse7693a32008-01-25 21:08:09 +01005517 }
5518 }
5519
Nicolas Pitre83a0a962014-09-04 11:32:10 -04005520 return shallowest_idle_cpu != -1 ? shallowest_idle_cpu : least_loaded_cpu;
Peter Zijlstraaaee1202009-09-10 13:36:25 +02005521}
Gregory Haskinse7693a32008-01-25 21:08:09 +01005522
Peter Zijlstraaaee1202009-09-10 13:36:25 +02005523/*
Peter Zijlstra10e2f1a2016-05-09 10:38:05 +02005524 * Implement a for_each_cpu() variant that starts the scan at a given cpu
5525 * (@start), and wraps around.
5526 *
5527 * This is used to scan for idle CPUs; such that not all CPUs looking for an
5528 * idle CPU find the same CPU. The down-side is that tasks tend to cycle
5529 * through the LLC domain.
5530 *
5531 * Especially tbench is found sensitive to this.
Peter Zijlstraa50bde52009-11-12 15:55:28 +01005532 */
Peter Zijlstra10e2f1a2016-05-09 10:38:05 +02005533
5534static int cpumask_next_wrap(int n, const struct cpumask *mask, int start, int *wrapped)
5535{
5536 int next;
5537
5538again:
5539 next = find_next_bit(cpumask_bits(mask), nr_cpumask_bits, n+1);
5540
5541 if (*wrapped) {
5542 if (next >= start)
5543 return nr_cpumask_bits;
5544 } else {
5545 if (next >= nr_cpumask_bits) {
5546 *wrapped = 1;
5547 n = -1;
5548 goto again;
5549 }
5550 }
5551
5552 return next;
5553}
5554
5555#define for_each_cpu_wrap(cpu, mask, start, wrap) \
5556 for ((wrap) = 0, (cpu) = (start)-1; \
5557 (cpu) = cpumask_next_wrap((cpu), (mask), (start), &(wrap)), \
5558 (cpu) < nr_cpumask_bits; )
5559
5560#ifdef CONFIG_SCHED_SMT
5561
5562static inline void set_idle_cores(int cpu, int val)
5563{
5564 struct sched_domain_shared *sds;
5565
5566 sds = rcu_dereference(per_cpu(sd_llc_shared, cpu));
5567 if (sds)
5568 WRITE_ONCE(sds->has_idle_cores, val);
5569}
5570
5571static inline bool test_idle_cores(int cpu, bool def)
5572{
5573 struct sched_domain_shared *sds;
5574
5575 sds = rcu_dereference(per_cpu(sd_llc_shared, cpu));
5576 if (sds)
5577 return READ_ONCE(sds->has_idle_cores);
5578
5579 return def;
5580}
5581
5582/*
5583 * Scans the local SMT mask to see if the entire core is idle, and records this
5584 * information in sd_llc_shared->has_idle_cores.
5585 *
5586 * Since SMT siblings share all cache levels, inspecting this limited remote
5587 * state should be fairly cheap.
5588 */
Peter Zijlstra1b568f02016-05-09 10:38:41 +02005589void __update_idle_core(struct rq *rq)
Peter Zijlstra10e2f1a2016-05-09 10:38:05 +02005590{
5591 int core = cpu_of(rq);
5592 int cpu;
5593
5594 rcu_read_lock();
5595 if (test_idle_cores(core, true))
5596 goto unlock;
5597
5598 for_each_cpu(cpu, cpu_smt_mask(core)) {
5599 if (cpu == core)
5600 continue;
5601
5602 if (!idle_cpu(cpu))
5603 goto unlock;
5604 }
5605
5606 set_idle_cores(core, 1);
5607unlock:
5608 rcu_read_unlock();
5609}
5610
5611/*
5612 * Scan the entire LLC domain for idle cores; this dynamically switches off if
5613 * there are no idle cores left in the system; tracked through
5614 * sd_llc->shared->has_idle_cores and enabled through update_idle_core() above.
5615 */
5616static int select_idle_core(struct task_struct *p, struct sched_domain *sd, int target)
5617{
5618 struct cpumask *cpus = this_cpu_cpumask_var_ptr(select_idle_mask);
5619 int core, cpu, wrap;
5620
Peter Zijlstra1b568f02016-05-09 10:38:41 +02005621 if (!static_branch_likely(&sched_smt_present))
5622 return -1;
5623
Peter Zijlstra10e2f1a2016-05-09 10:38:05 +02005624 if (!test_idle_cores(target, false))
5625 return -1;
5626
5627 cpumask_and(cpus, sched_domain_span(sd), tsk_cpus_allowed(p));
5628
5629 for_each_cpu_wrap(core, cpus, target, wrap) {
5630 bool idle = true;
5631
5632 for_each_cpu(cpu, cpu_smt_mask(core)) {
5633 cpumask_clear_cpu(cpu, cpus);
5634 if (!idle_cpu(cpu))
5635 idle = false;
5636 }
5637
5638 if (idle)
5639 return core;
5640 }
5641
5642 /*
5643 * Failed to find an idle core; stop looking for one.
5644 */
5645 set_idle_cores(target, 0);
5646
5647 return -1;
5648}
5649
5650/*
5651 * Scan the local SMT mask for idle CPUs.
5652 */
5653static int select_idle_smt(struct task_struct *p, struct sched_domain *sd, int target)
5654{
5655 int cpu;
5656
Peter Zijlstra1b568f02016-05-09 10:38:41 +02005657 if (!static_branch_likely(&sched_smt_present))
5658 return -1;
5659
Peter Zijlstra10e2f1a2016-05-09 10:38:05 +02005660 for_each_cpu(cpu, cpu_smt_mask(target)) {
5661 if (!cpumask_test_cpu(cpu, tsk_cpus_allowed(p)))
5662 continue;
5663 if (idle_cpu(cpu))
5664 return cpu;
5665 }
5666
5667 return -1;
5668}
5669
5670#else /* CONFIG_SCHED_SMT */
5671
5672static inline int select_idle_core(struct task_struct *p, struct sched_domain *sd, int target)
5673{
5674 return -1;
5675}
5676
5677static inline int select_idle_smt(struct task_struct *p, struct sched_domain *sd, int target)
5678{
5679 return -1;
5680}
5681
5682#endif /* CONFIG_SCHED_SMT */
5683
5684/*
5685 * Scan the LLC domain for idle CPUs; this is dynamically regulated by
5686 * comparing the average scan cost (tracked in sd->avg_scan_cost) against the
5687 * average idle time for this rq (as found in rq->avg_idle).
5688 */
5689static int select_idle_cpu(struct task_struct *p, struct sched_domain *sd, int target)
5690{
Wanpeng Li9cfb38a2016-10-09 08:04:03 +08005691 struct sched_domain *this_sd;
5692 u64 avg_cost, avg_idle = this_rq()->avg_idle;
Peter Zijlstra10e2f1a2016-05-09 10:38:05 +02005693 u64 time, cost;
5694 s64 delta;
5695 int cpu, wrap;
5696
Wanpeng Li9cfb38a2016-10-09 08:04:03 +08005697 this_sd = rcu_dereference(*this_cpu_ptr(&sd_llc));
5698 if (!this_sd)
5699 return -1;
5700
5701 avg_cost = this_sd->avg_scan_cost;
5702
Peter Zijlstra10e2f1a2016-05-09 10:38:05 +02005703 /*
5704 * Due to large variance we need a large fuzz factor; hackbench in
5705 * particularly is sensitive here.
5706 */
5707 if ((avg_idle / 512) < avg_cost)
5708 return -1;
5709
5710 time = local_clock();
5711
5712 for_each_cpu_wrap(cpu, sched_domain_span(sd), target, wrap) {
5713 if (!cpumask_test_cpu(cpu, tsk_cpus_allowed(p)))
5714 continue;
5715 if (idle_cpu(cpu))
5716 break;
5717 }
5718
5719 time = local_clock() - time;
5720 cost = this_sd->avg_scan_cost;
5721 delta = (s64)(time - cost) / 8;
5722 this_sd->avg_scan_cost += delta;
5723
5724 return cpu;
5725}
5726
5727/*
5728 * Try and locate an idle core/thread in the LLC cache domain.
Peter Zijlstraa50bde52009-11-12 15:55:28 +01005729 */
Morten Rasmussen772bd008c2016-06-22 18:03:13 +01005730static int select_idle_sibling(struct task_struct *p, int prev, int target)
Peter Zijlstraa50bde52009-11-12 15:55:28 +01005731{
Suresh Siddha99bd5e22010-03-31 16:47:45 -07005732 struct sched_domain *sd;
Peter Zijlstra10e2f1a2016-05-09 10:38:05 +02005733 int i;
Mike Galbraithe0a79f52013-01-28 12:19:25 +01005734
5735 if (idle_cpu(target))
5736 return target;
Peter Zijlstraa50bde52009-11-12 15:55:28 +01005737
5738 /*
Peter Zijlstra10e2f1a2016-05-09 10:38:05 +02005739 * If the previous cpu is cache affine and idle, don't be stupid.
Peter Zijlstraa50bde52009-11-12 15:55:28 +01005740 */
Morten Rasmussen772bd008c2016-06-22 18:03:13 +01005741 if (prev != target && cpus_share_cache(prev, target) && idle_cpu(prev))
5742 return prev;
Peter Zijlstraa50bde52009-11-12 15:55:28 +01005743
Peter Zijlstra518cd622011-12-07 15:07:31 +01005744 sd = rcu_dereference(per_cpu(sd_llc, target));
Peter Zijlstra10e2f1a2016-05-09 10:38:05 +02005745 if (!sd)
5746 return target;
Morten Rasmussen772bd008c2016-06-22 18:03:13 +01005747
Peter Zijlstra10e2f1a2016-05-09 10:38:05 +02005748 i = select_idle_core(p, sd, target);
5749 if ((unsigned)i < nr_cpumask_bits)
Gregory Haskinse7693a32008-01-25 21:08:09 +01005750 return i;
Ingo Molnar098fb9d2008-03-16 20:36:10 +01005751
Peter Zijlstra10e2f1a2016-05-09 10:38:05 +02005752 i = select_idle_cpu(p, sd, target);
5753 if ((unsigned)i < nr_cpumask_bits)
5754 return i;
Mike Galbraith970e1782012-06-12 05:18:32 +02005755
Peter Zijlstra10e2f1a2016-05-09 10:38:05 +02005756 i = select_idle_smt(p, sd, target);
5757 if ((unsigned)i < nr_cpumask_bits)
5758 return i;
Linus Torvalds37407ea2012-09-16 12:29:43 -07005759
Peter Zijlstraa50bde52009-11-12 15:55:28 +01005760 return target;
5761}
Dietmar Eggemann231678b2015-08-14 17:23:13 +01005762
Vincent Guittot8bb5b002015-03-04 08:48:47 +01005763/*
Dietmar Eggemann9e91d612015-08-14 17:23:12 +01005764 * cpu_util returns the amount of capacity of a CPU that is used by CFS
Vincent Guittot8bb5b002015-03-04 08:48:47 +01005765 * tasks. The unit of the return value must be the one of capacity so we can
Dietmar Eggemann9e91d612015-08-14 17:23:12 +01005766 * compare the utilization with the capacity of the CPU that is available for
5767 * CFS task (ie cpu_capacity).
Dietmar Eggemann231678b2015-08-14 17:23:13 +01005768 *
5769 * cfs_rq.avg.util_avg is the sum of running time of runnable tasks plus the
5770 * recent utilization of currently non-runnable tasks on a CPU. It represents
5771 * the amount of utilization of a CPU in the range [0..capacity_orig] where
5772 * capacity_orig is the cpu_capacity available at the highest frequency
5773 * (arch_scale_freq_capacity()).
5774 * The utilization of a CPU converges towards a sum equal to or less than the
5775 * current capacity (capacity_curr <= capacity_orig) of the CPU because it is
5776 * the running time on this CPU scaled by capacity_curr.
5777 *
5778 * Nevertheless, cfs_rq.avg.util_avg can be higher than capacity_curr or even
5779 * higher than capacity_orig because of unfortunate rounding in
5780 * cfs.avg.util_avg or just after migrating tasks and new task wakeups until
5781 * the average stabilizes with the new running time. We need to check that the
5782 * utilization stays within the range of [0..capacity_orig] and cap it if
5783 * necessary. Without utilization capping, a group could be seen as overloaded
5784 * (CPU0 utilization at 121% + CPU1 utilization at 80%) whereas CPU1 has 20% of
5785 * available capacity. We allow utilization to overshoot capacity_curr (but not
5786 * capacity_orig) as it useful for predicting the capacity required after task
5787 * migrations (scheduler-driven DVFS).
Vincent Guittot8bb5b002015-03-04 08:48:47 +01005788 */
Dietmar Eggemann9e91d612015-08-14 17:23:12 +01005789static int cpu_util(int cpu)
Vincent Guittot8bb5b002015-03-04 08:48:47 +01005790{
Dietmar Eggemann9e91d612015-08-14 17:23:12 +01005791 unsigned long util = cpu_rq(cpu)->cfs.avg.util_avg;
Vincent Guittot8bb5b002015-03-04 08:48:47 +01005792 unsigned long capacity = capacity_orig_of(cpu);
5793
Dietmar Eggemann231678b2015-08-14 17:23:13 +01005794 return (util >= capacity) ? capacity : util;
Vincent Guittot8bb5b002015-03-04 08:48:47 +01005795}
Peter Zijlstraa50bde52009-11-12 15:55:28 +01005796
Morten Rasmussen32731632016-07-25 14:34:26 +01005797static inline int task_util(struct task_struct *p)
5798{
5799 return p->se.avg.util_avg;
5800}
5801
5802/*
Morten Rasmussen104cb162016-10-14 14:41:07 +01005803 * cpu_util_wake: Compute cpu utilization with any contributions from
5804 * the waking task p removed.
5805 */
5806static int cpu_util_wake(int cpu, struct task_struct *p)
5807{
5808 unsigned long util, capacity;
5809
5810 /* Task has no contribution or is new */
5811 if (cpu != task_cpu(p) || !p->se.avg.last_update_time)
5812 return cpu_util(cpu);
5813
5814 capacity = capacity_orig_of(cpu);
5815 util = max_t(long, cpu_rq(cpu)->cfs.avg.util_avg - task_util(p), 0);
5816
5817 return (util >= capacity) ? capacity : util;
5818}
5819
5820/*
Morten Rasmussen32731632016-07-25 14:34:26 +01005821 * Disable WAKE_AFFINE in the case where task @p doesn't fit in the
5822 * capacity of either the waking CPU @cpu or the previous CPU @prev_cpu.
5823 *
5824 * In that case WAKE_AFFINE doesn't make sense and we'll let
5825 * BALANCE_WAKE sort things out.
5826 */
5827static int wake_cap(struct task_struct *p, int cpu, int prev_cpu)
5828{
5829 long min_cap, max_cap;
5830
5831 min_cap = min(capacity_orig_of(prev_cpu), capacity_orig_of(cpu));
5832 max_cap = cpu_rq(cpu)->rd->max_cpu_capacity;
5833
5834 /* Minimum capacity is close to max, no need to abort wake_affine */
5835 if (max_cap - min_cap < max_cap >> 3)
5836 return 0;
5837
Morten Rasmussen104cb162016-10-14 14:41:07 +01005838 /* Bring task utilization in sync with prev_cpu */
5839 sync_entity_load_avg(&p->se);
5840
Morten Rasmussen32731632016-07-25 14:34:26 +01005841 return min_cap * 1024 < task_util(p) * capacity_margin;
5842}
5843
Peter Zijlstraa50bde52009-11-12 15:55:28 +01005844/*
Morten Rasmussende91b9c2014-02-18 14:14:24 +00005845 * select_task_rq_fair: Select target runqueue for the waking task in domains
5846 * that have the 'sd_flag' flag set. In practice, this is SD_BALANCE_WAKE,
5847 * SD_BALANCE_FORK, or SD_BALANCE_EXEC.
Peter Zijlstraaaee1202009-09-10 13:36:25 +02005848 *
Morten Rasmussende91b9c2014-02-18 14:14:24 +00005849 * Balances load by selecting the idlest cpu in the idlest group, or under
5850 * certain conditions an idle sibling cpu if the domain has SD_WAKE_AFFINE set.
Peter Zijlstraaaee1202009-09-10 13:36:25 +02005851 *
Morten Rasmussende91b9c2014-02-18 14:14:24 +00005852 * Returns the target cpu number.
Peter Zijlstraaaee1202009-09-10 13:36:25 +02005853 *
5854 * preempt must be disabled.
5855 */
Peter Zijlstra0017d732010-03-24 18:34:10 +01005856static int
Peter Zijlstraac66f542013-10-07 11:29:16 +01005857select_task_rq_fair(struct task_struct *p, int prev_cpu, int sd_flag, int wake_flags)
Peter Zijlstraaaee1202009-09-10 13:36:25 +02005858{
Peter Zijlstra29cd8ba2009-09-17 09:01:14 +02005859 struct sched_domain *tmp, *affine_sd = NULL, *sd = NULL;
Peter Zijlstrac88d5912009-09-10 13:50:02 +02005860 int cpu = smp_processor_id();
Mike Galbraith63b0e9e2015-07-14 17:39:50 +02005861 int new_cpu = prev_cpu;
Suresh Siddha99bd5e22010-03-31 16:47:45 -07005862 int want_affine = 0;
Peter Zijlstra5158f4e2009-09-16 13:46:59 +02005863 int sync = wake_flags & WF_SYNC;
Gregory Haskinse7693a32008-01-25 21:08:09 +01005864
Peter Zijlstrac58d25f2016-05-12 09:19:59 +02005865 if (sd_flag & SD_BALANCE_WAKE) {
5866 record_wakee(p);
Morten Rasmussen32731632016-07-25 14:34:26 +01005867 want_affine = !wake_wide(p) && !wake_cap(p, cpu, prev_cpu)
5868 && cpumask_test_cpu(cpu, tsk_cpus_allowed(p));
Peter Zijlstrac58d25f2016-05-12 09:19:59 +02005869 }
Gregory Haskinse7693a32008-01-25 21:08:09 +01005870
Peter Zijlstradce840a2011-04-07 14:09:50 +02005871 rcu_read_lock();
Peter Zijlstraaaee1202009-09-10 13:36:25 +02005872 for_each_domain(cpu, tmp) {
Peter Zijlstrae4f42882009-12-16 18:04:34 +01005873 if (!(tmp->flags & SD_LOAD_BALANCE))
Mike Galbraith63b0e9e2015-07-14 17:39:50 +02005874 break;
Peter Zijlstrae4f42882009-12-16 18:04:34 +01005875
Peter Zijlstraaaee1202009-09-10 13:36:25 +02005876 /*
Suresh Siddha99bd5e22010-03-31 16:47:45 -07005877 * If both cpu and prev_cpu are part of this domain,
5878 * cpu is a valid SD_WAKE_AFFINE target.
Peter Zijlstrafe3bcfe2009-11-12 15:55:29 +01005879 */
Suresh Siddha99bd5e22010-03-31 16:47:45 -07005880 if (want_affine && (tmp->flags & SD_WAKE_AFFINE) &&
5881 cpumask_test_cpu(prev_cpu, sched_domain_span(tmp))) {
5882 affine_sd = tmp;
Alex Shif03542a2012-07-26 08:55:34 +08005883 break;
Peter Zijlstrac88d5912009-09-10 13:50:02 +02005884 }
5885
Alex Shif03542a2012-07-26 08:55:34 +08005886 if (tmp->flags & sd_flag)
Peter Zijlstra29cd8ba2009-09-17 09:01:14 +02005887 sd = tmp;
Mike Galbraith63b0e9e2015-07-14 17:39:50 +02005888 else if (!want_affine)
5889 break;
Peter Zijlstrac88d5912009-09-10 13:50:02 +02005890 }
Peter Zijlstraaaee1202009-09-10 13:36:25 +02005891
Mike Galbraith63b0e9e2015-07-14 17:39:50 +02005892 if (affine_sd) {
5893 sd = NULL; /* Prefer wake_affine over balance flags */
Morten Rasmussen772bd008c2016-06-22 18:03:13 +01005894 if (cpu != prev_cpu && wake_affine(affine_sd, p, prev_cpu, sync))
Mike Galbraith63b0e9e2015-07-14 17:39:50 +02005895 new_cpu = cpu;
Mike Galbraith8b911ac2010-03-11 17:17:16 +01005896 }
Peter Zijlstra3b640892009-09-16 13:44:33 +02005897
Mike Galbraith63b0e9e2015-07-14 17:39:50 +02005898 if (!sd) {
5899 if (sd_flag & SD_BALANCE_WAKE) /* XXX always ? */
Morten Rasmussen772bd008c2016-06-22 18:03:13 +01005900 new_cpu = select_idle_sibling(p, prev_cpu, new_cpu);
Mike Galbraith63b0e9e2015-07-14 17:39:50 +02005901
5902 } else while (sd) {
Peter Zijlstraaaee1202009-09-10 13:36:25 +02005903 struct sched_group *group;
Peter Zijlstrac88d5912009-09-10 13:50:02 +02005904 int weight;
Peter Zijlstraaaee1202009-09-10 13:36:25 +02005905
Peter Zijlstra0763a662009-09-14 19:37:39 +02005906 if (!(sd->flags & sd_flag)) {
Peter Zijlstraaaee1202009-09-10 13:36:25 +02005907 sd = sd->child;
5908 continue;
5909 }
5910
Vincent Guittotc44f2a02013-10-18 13:52:21 +02005911 group = find_idlest_group(sd, p, cpu, sd_flag);
Peter Zijlstraaaee1202009-09-10 13:36:25 +02005912 if (!group) {
5913 sd = sd->child;
5914 continue;
5915 }
5916
Peter Zijlstrad7c33c42009-09-11 12:45:38 +02005917 new_cpu = find_idlest_cpu(group, p, cpu);
Peter Zijlstraaaee1202009-09-10 13:36:25 +02005918 if (new_cpu == -1 || new_cpu == cpu) {
5919 /* Now try balancing at a lower domain level of cpu */
5920 sd = sd->child;
5921 continue;
5922 }
5923
5924 /* Now try balancing at a lower domain level of new_cpu */
5925 cpu = new_cpu;
Peter Zijlstra669c55e2010-04-16 14:59:29 +02005926 weight = sd->span_weight;
Peter Zijlstraaaee1202009-09-10 13:36:25 +02005927 sd = NULL;
5928 for_each_domain(cpu, tmp) {
Peter Zijlstra669c55e2010-04-16 14:59:29 +02005929 if (weight <= tmp->span_weight)
Peter Zijlstraaaee1202009-09-10 13:36:25 +02005930 break;
Peter Zijlstra0763a662009-09-14 19:37:39 +02005931 if (tmp->flags & sd_flag)
Peter Zijlstraaaee1202009-09-10 13:36:25 +02005932 sd = tmp;
5933 }
5934 /* while loop will break here if sd == NULL */
Gregory Haskinse7693a32008-01-25 21:08:09 +01005935 }
Peter Zijlstradce840a2011-04-07 14:09:50 +02005936 rcu_read_unlock();
Gregory Haskinse7693a32008-01-25 21:08:09 +01005937
Peter Zijlstrac88d5912009-09-10 13:50:02 +02005938 return new_cpu;
Gregory Haskinse7693a32008-01-25 21:08:09 +01005939}
Paul Turner0a74bef2012-10-04 13:18:30 +02005940
5941/*
5942 * Called immediately before a task is migrated to a new cpu; task_cpu(p) and
5943 * cfs_rq_of(p) references at time of call are still valid and identify the
Byungchul Park525628c2015-11-18 09:34:59 +09005944 * previous cpu. The caller guarantees p->pi_lock or task_rq(p)->lock is held.
Paul Turner0a74bef2012-10-04 13:18:30 +02005945 */
xiaofeng.yan5a4fd032015-09-23 14:55:59 +08005946static void migrate_task_rq_fair(struct task_struct *p)
Paul Turner0a74bef2012-10-04 13:18:30 +02005947{
Paul Turneraff3e492012-10-04 13:18:30 +02005948 /*
Peter Zijlstra59efa0b2016-05-10 18:24:37 +02005949 * As blocked tasks retain absolute vruntime the migration needs to
5950 * deal with this by subtracting the old and adding the new
5951 * min_vruntime -- the latter is done by enqueue_entity() when placing
5952 * the task on the new runqueue.
5953 */
5954 if (p->state == TASK_WAKING) {
5955 struct sched_entity *se = &p->se;
5956 struct cfs_rq *cfs_rq = cfs_rq_of(se);
5957 u64 min_vruntime;
5958
5959#ifndef CONFIG_64BIT
5960 u64 min_vruntime_copy;
5961
5962 do {
5963 min_vruntime_copy = cfs_rq->min_vruntime_copy;
5964 smp_rmb();
5965 min_vruntime = cfs_rq->min_vruntime;
5966 } while (min_vruntime != min_vruntime_copy);
5967#else
5968 min_vruntime = cfs_rq->min_vruntime;
5969#endif
5970
5971 se->vruntime -= min_vruntime;
5972 }
5973
5974 /*
Yuyang Du9d89c252015-07-15 08:04:37 +08005975 * We are supposed to update the task to "current" time, then its up to date
5976 * and ready to go to new CPU/cfs_rq. But we have difficulty in getting
5977 * what current time is, so simply throw away the out-of-date time. This
5978 * will result in the wakee task is less decayed, but giving the wakee more
5979 * load sounds not bad.
Paul Turneraff3e492012-10-04 13:18:30 +02005980 */
Yuyang Du9d89c252015-07-15 08:04:37 +08005981 remove_entity_load_avg(&p->se);
5982
5983 /* Tell new CPU we are migrated */
5984 p->se.avg.last_update_time = 0;
Ben Segall3944a922014-05-15 15:59:20 -07005985
5986 /* We have migrated, no longer consider this task hot */
Yuyang Du9d89c252015-07-15 08:04:37 +08005987 p->se.exec_start = 0;
Paul Turner0a74bef2012-10-04 13:18:30 +02005988}
Yuyang Du12695572015-07-15 08:04:40 +08005989
5990static void task_dead_fair(struct task_struct *p)
5991{
5992 remove_entity_load_avg(&p->se);
5993}
Gregory Haskinse7693a32008-01-25 21:08:09 +01005994#endif /* CONFIG_SMP */
5995
Peter Zijlstrae52fb7c2009-01-14 12:39:19 +01005996static unsigned long
5997wakeup_gran(struct sched_entity *curr, struct sched_entity *se)
Peter Zijlstra0bbd3332008-04-19 19:44:57 +02005998{
5999 unsigned long gran = sysctl_sched_wakeup_granularity;
6000
6001 /*
Peter Zijlstrae52fb7c2009-01-14 12:39:19 +01006002 * Since its curr running now, convert the gran from real-time
6003 * to virtual-time in his units.
Mike Galbraith13814d42010-03-11 17:17:04 +01006004 *
6005 * By using 'se' instead of 'curr' we penalize light tasks, so
6006 * they get preempted easier. That is, if 'se' < 'curr' then
6007 * the resulting gran will be larger, therefore penalizing the
6008 * lighter, if otoh 'se' > 'curr' then the resulting gran will
6009 * be smaller, again penalizing the lighter task.
6010 *
6011 * This is especially important for buddies when the leftmost
6012 * task is higher priority than the buddy.
Peter Zijlstra0bbd3332008-04-19 19:44:57 +02006013 */
Shaohua Lif4ad9bd2011-04-08 12:53:09 +08006014 return calc_delta_fair(gran, se);
Peter Zijlstra0bbd3332008-04-19 19:44:57 +02006015}
6016
6017/*
Peter Zijlstra464b7522008-10-24 11:06:15 +02006018 * Should 'se' preempt 'curr'.
6019 *
6020 * |s1
6021 * |s2
6022 * |s3
6023 * g
6024 * |<--->|c
6025 *
6026 * w(c, s1) = -1
6027 * w(c, s2) = 0
6028 * w(c, s3) = 1
6029 *
6030 */
6031static int
6032wakeup_preempt_entity(struct sched_entity *curr, struct sched_entity *se)
6033{
6034 s64 gran, vdiff = curr->vruntime - se->vruntime;
6035
6036 if (vdiff <= 0)
6037 return -1;
6038
Peter Zijlstrae52fb7c2009-01-14 12:39:19 +01006039 gran = wakeup_gran(curr, se);
Peter Zijlstra464b7522008-10-24 11:06:15 +02006040 if (vdiff > gran)
6041 return 1;
6042
6043 return 0;
6044}
6045
Peter Zijlstra02479092008-11-04 21:25:10 +01006046static void set_last_buddy(struct sched_entity *se)
6047{
Venkatesh Pallipadi69c80f32011-04-13 18:21:09 -07006048 if (entity_is_task(se) && unlikely(task_of(se)->policy == SCHED_IDLE))
6049 return;
6050
6051 for_each_sched_entity(se)
6052 cfs_rq_of(se)->last = se;
Peter Zijlstra02479092008-11-04 21:25:10 +01006053}
6054
6055static void set_next_buddy(struct sched_entity *se)
6056{
Venkatesh Pallipadi69c80f32011-04-13 18:21:09 -07006057 if (entity_is_task(se) && unlikely(task_of(se)->policy == SCHED_IDLE))
6058 return;
6059
6060 for_each_sched_entity(se)
6061 cfs_rq_of(se)->next = se;
Peter Zijlstra02479092008-11-04 21:25:10 +01006062}
6063
Rik van Rielac53db52011-02-01 09:51:03 -05006064static void set_skip_buddy(struct sched_entity *se)
6065{
Venkatesh Pallipadi69c80f32011-04-13 18:21:09 -07006066 for_each_sched_entity(se)
6067 cfs_rq_of(se)->skip = se;
Rik van Rielac53db52011-02-01 09:51:03 -05006068}
6069
Peter Zijlstra464b7522008-10-24 11:06:15 +02006070/*
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02006071 * Preempt the current task with a newly woken task if needed:
6072 */
Peter Zijlstra5a9b86f2009-09-16 13:47:58 +02006073static void check_preempt_wakeup(struct rq *rq, struct task_struct *p, int wake_flags)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02006074{
6075 struct task_struct *curr = rq->curr;
Srivatsa Vaddagiri8651a862007-10-15 17:00:12 +02006076 struct sched_entity *se = &curr->se, *pse = &p->se;
Mike Galbraith03e89e42008-12-16 08:45:30 +01006077 struct cfs_rq *cfs_rq = task_cfs_rq(curr);
Mike Galbraithf685cea2009-10-23 23:09:22 +02006078 int scale = cfs_rq->nr_running >= sched_nr_latency;
Venkatesh Pallipadi2f368252011-04-14 10:30:53 -07006079 int next_buddy_marked = 0;
Mike Galbraith03e89e42008-12-16 08:45:30 +01006080
Ingo Molnar4ae7d5c2008-03-19 01:42:00 +01006081 if (unlikely(se == pse))
6082 return;
6083
Paul Turner5238cdd2011-07-21 09:43:37 -07006084 /*
Kirill Tkhai163122b2014-08-20 13:48:29 +04006085 * This is possible from callers such as attach_tasks(), in which we
Paul Turner5238cdd2011-07-21 09:43:37 -07006086 * unconditionally check_prempt_curr() after an enqueue (which may have
6087 * lead to a throttle). This both saves work and prevents false
6088 * next-buddy nomination below.
6089 */
6090 if (unlikely(throttled_hierarchy(cfs_rq_of(pse))))
6091 return;
6092
Venkatesh Pallipadi2f368252011-04-14 10:30:53 -07006093 if (sched_feat(NEXT_BUDDY) && scale && !(wake_flags & WF_FORK)) {
Mike Galbraith3cb63d52009-09-11 12:01:17 +02006094 set_next_buddy(pse);
Venkatesh Pallipadi2f368252011-04-14 10:30:53 -07006095 next_buddy_marked = 1;
6096 }
Peter Zijlstra57fdc262008-09-23 15:33:45 +02006097
Bharata B Raoaec0a512008-08-28 14:42:49 +05306098 /*
6099 * We can come here with TIF_NEED_RESCHED already set from new task
6100 * wake up path.
Paul Turner5238cdd2011-07-21 09:43:37 -07006101 *
6102 * Note: this also catches the edge-case of curr being in a throttled
6103 * group (e.g. via set_curr_task), since update_curr() (in the
6104 * enqueue of curr) will have resulted in resched being set. This
6105 * prevents us from potentially nominating it as a false LAST_BUDDY
6106 * below.
Bharata B Raoaec0a512008-08-28 14:42:49 +05306107 */
6108 if (test_tsk_need_resched(curr))
6109 return;
6110
Darren Harta2f5c9a2011-02-22 13:04:33 -08006111 /* Idle tasks are by definition preempted by non-idle tasks. */
6112 if (unlikely(curr->policy == SCHED_IDLE) &&
6113 likely(p->policy != SCHED_IDLE))
6114 goto preempt;
6115
Ingo Molnar91c234b2007-10-15 17:00:18 +02006116 /*
Darren Harta2f5c9a2011-02-22 13:04:33 -08006117 * Batch and idle tasks do not preempt non-idle tasks (their preemption
6118 * is driven by the tick):
Ingo Molnar91c234b2007-10-15 17:00:18 +02006119 */
Ingo Molnar8ed92e52012-10-14 14:28:50 +02006120 if (unlikely(p->policy != SCHED_NORMAL) || !sched_feat(WAKEUP_PREEMPTION))
Ingo Molnar91c234b2007-10-15 17:00:18 +02006121 return;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02006122
Peter Zijlstra3a7e73a2009-11-28 18:51:02 +01006123 find_matching_se(&se, &pse);
Paul Turner9bbd7372011-07-05 19:07:21 -07006124 update_curr(cfs_rq_of(se));
Peter Zijlstra3a7e73a2009-11-28 18:51:02 +01006125 BUG_ON(!pse);
Venkatesh Pallipadi2f368252011-04-14 10:30:53 -07006126 if (wakeup_preempt_entity(se, pse) == 1) {
6127 /*
6128 * Bias pick_next to pick the sched entity that is
6129 * triggering this preemption.
6130 */
6131 if (!next_buddy_marked)
6132 set_next_buddy(pse);
Peter Zijlstra3a7e73a2009-11-28 18:51:02 +01006133 goto preempt;
Venkatesh Pallipadi2f368252011-04-14 10:30:53 -07006134 }
Jupyung Leea65ac742009-11-17 18:51:40 +09006135
Peter Zijlstra3a7e73a2009-11-28 18:51:02 +01006136 return;
6137
6138preempt:
Kirill Tkhai88751252014-06-29 00:03:57 +04006139 resched_curr(rq);
Peter Zijlstra3a7e73a2009-11-28 18:51:02 +01006140 /*
6141 * Only set the backward buddy when the current task is still
6142 * on the rq. This can happen when a wakeup gets interleaved
6143 * with schedule on the ->pre_schedule() or idle_balance()
6144 * point, either of which can * drop the rq lock.
6145 *
6146 * Also, during early boot the idle thread is in the fair class,
6147 * for obvious reasons its a bad idea to schedule back to it.
6148 */
6149 if (unlikely(!se->on_rq || curr == rq->idle))
6150 return;
6151
6152 if (sched_feat(LAST_BUDDY) && scale && entity_is_task(se))
6153 set_last_buddy(se);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02006154}
6155
Peter Zijlstra606dba22012-02-11 06:05:00 +01006156static struct task_struct *
Peter Zijlstrae7904a22015-08-01 19:25:08 +02006157pick_next_task_fair(struct rq *rq, struct task_struct *prev, struct pin_cookie cookie)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02006158{
6159 struct cfs_rq *cfs_rq = &rq->cfs;
6160 struct sched_entity *se;
Peter Zijlstra678d5712012-02-11 06:05:00 +01006161 struct task_struct *p;
Peter Zijlstra37e117c2014-02-14 12:25:08 +01006162 int new_tasks;
Peter Zijlstra678d5712012-02-11 06:05:00 +01006163
Peter Zijlstra6e831252014-02-11 16:11:48 +01006164again:
Peter Zijlstra678d5712012-02-11 06:05:00 +01006165#ifdef CONFIG_FAIR_GROUP_SCHED
6166 if (!cfs_rq->nr_running)
Peter Zijlstra38033c32014-01-23 20:32:21 +01006167 goto idle;
Peter Zijlstra678d5712012-02-11 06:05:00 +01006168
Peter Zijlstra3f1d2a32014-02-12 10:49:30 +01006169 if (prev->sched_class != &fair_sched_class)
Peter Zijlstra678d5712012-02-11 06:05:00 +01006170 goto simple;
6171
6172 /*
6173 * Because of the set_next_buddy() in dequeue_task_fair() it is rather
6174 * likely that a next task is from the same cgroup as the current.
6175 *
6176 * Therefore attempt to avoid putting and setting the entire cgroup
6177 * hierarchy, only change the part that actually changes.
6178 */
6179
6180 do {
6181 struct sched_entity *curr = cfs_rq->curr;
6182
6183 /*
6184 * Since we got here without doing put_prev_entity() we also
6185 * have to consider cfs_rq->curr. If it is still a runnable
6186 * entity, update_curr() will update its vruntime, otherwise
6187 * forget we've ever seen it.
6188 */
Ben Segall54d27362015-04-06 15:28:10 -07006189 if (curr) {
6190 if (curr->on_rq)
6191 update_curr(cfs_rq);
6192 else
6193 curr = NULL;
Peter Zijlstra678d5712012-02-11 06:05:00 +01006194
Ben Segall54d27362015-04-06 15:28:10 -07006195 /*
6196 * This call to check_cfs_rq_runtime() will do the
6197 * throttle and dequeue its entity in the parent(s).
6198 * Therefore the 'simple' nr_running test will indeed
6199 * be correct.
6200 */
6201 if (unlikely(check_cfs_rq_runtime(cfs_rq)))
6202 goto simple;
6203 }
Peter Zijlstra678d5712012-02-11 06:05:00 +01006204
6205 se = pick_next_entity(cfs_rq, curr);
6206 cfs_rq = group_cfs_rq(se);
6207 } while (cfs_rq);
6208
6209 p = task_of(se);
6210
6211 /*
6212 * Since we haven't yet done put_prev_entity and if the selected task
6213 * is a different task than we started out with, try and touch the
6214 * least amount of cfs_rqs.
6215 */
6216 if (prev != p) {
6217 struct sched_entity *pse = &prev->se;
6218
6219 while (!(cfs_rq = is_same_group(se, pse))) {
6220 int se_depth = se->depth;
6221 int pse_depth = pse->depth;
6222
6223 if (se_depth <= pse_depth) {
6224 put_prev_entity(cfs_rq_of(pse), pse);
6225 pse = parent_entity(pse);
6226 }
6227 if (se_depth >= pse_depth) {
6228 set_next_entity(cfs_rq_of(se), se);
6229 se = parent_entity(se);
6230 }
6231 }
6232
6233 put_prev_entity(cfs_rq, pse);
6234 set_next_entity(cfs_rq, se);
6235 }
6236
6237 if (hrtick_enabled(rq))
6238 hrtick_start_fair(rq, p);
6239
6240 return p;
6241simple:
6242 cfs_rq = &rq->cfs;
6243#endif
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02006244
Tim Blechmann36ace272009-11-24 11:55:45 +01006245 if (!cfs_rq->nr_running)
Peter Zijlstra38033c32014-01-23 20:32:21 +01006246 goto idle;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02006247
Peter Zijlstra3f1d2a32014-02-12 10:49:30 +01006248 put_prev_task(rq, prev);
Peter Zijlstra606dba22012-02-11 06:05:00 +01006249
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02006250 do {
Peter Zijlstra678d5712012-02-11 06:05:00 +01006251 se = pick_next_entity(cfs_rq, NULL);
Peter Zijlstraf4b67552008-11-04 21:25:07 +01006252 set_next_entity(cfs_rq, se);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02006253 cfs_rq = group_cfs_rq(se);
6254 } while (cfs_rq);
6255
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01006256 p = task_of(se);
Peter Zijlstra678d5712012-02-11 06:05:00 +01006257
Mike Galbraithb39e66e2011-11-22 15:20:07 +01006258 if (hrtick_enabled(rq))
6259 hrtick_start_fair(rq, p);
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01006260
6261 return p;
Peter Zijlstra38033c32014-01-23 20:32:21 +01006262
6263idle:
Peter Zijlstracbce1a62015-06-11 14:46:54 +02006264 /*
6265 * This is OK, because current is on_cpu, which avoids it being picked
6266 * for load-balance and preemption/IRQs are still disabled avoiding
6267 * further scheduler activity on it and we're being very careful to
6268 * re-start the picking loop.
6269 */
Peter Zijlstrae7904a22015-08-01 19:25:08 +02006270 lockdep_unpin_lock(&rq->lock, cookie);
Kirill Tkhaie4aa3582014-03-06 13:31:55 +04006271 new_tasks = idle_balance(rq);
Peter Zijlstrae7904a22015-08-01 19:25:08 +02006272 lockdep_repin_lock(&rq->lock, cookie);
Peter Zijlstra37e117c2014-02-14 12:25:08 +01006273 /*
6274 * Because idle_balance() releases (and re-acquires) rq->lock, it is
6275 * possible for any higher priority task to appear. In that case we
6276 * must re-start the pick_next_entity() loop.
6277 */
Kirill Tkhaie4aa3582014-03-06 13:31:55 +04006278 if (new_tasks < 0)
Peter Zijlstra37e117c2014-02-14 12:25:08 +01006279 return RETRY_TASK;
6280
Kirill Tkhaie4aa3582014-03-06 13:31:55 +04006281 if (new_tasks > 0)
Peter Zijlstra38033c32014-01-23 20:32:21 +01006282 goto again;
Peter Zijlstra38033c32014-01-23 20:32:21 +01006283
6284 return NULL;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02006285}
6286
6287/*
6288 * Account for a descheduled task:
6289 */
Ingo Molnar31ee5292007-08-09 11:16:49 +02006290static void put_prev_task_fair(struct rq *rq, struct task_struct *prev)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02006291{
6292 struct sched_entity *se = &prev->se;
6293 struct cfs_rq *cfs_rq;
6294
6295 for_each_sched_entity(se) {
6296 cfs_rq = cfs_rq_of(se);
Ingo Molnarab6cde22007-08-09 11:16:48 +02006297 put_prev_entity(cfs_rq, se);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02006298 }
6299}
6300
Rik van Rielac53db52011-02-01 09:51:03 -05006301/*
6302 * sched_yield() is very simple
6303 *
6304 * The magic of dealing with the ->skip buddy is in pick_next_entity.
6305 */
6306static void yield_task_fair(struct rq *rq)
6307{
6308 struct task_struct *curr = rq->curr;
6309 struct cfs_rq *cfs_rq = task_cfs_rq(curr);
6310 struct sched_entity *se = &curr->se;
6311
6312 /*
6313 * Are we the only task in the tree?
6314 */
6315 if (unlikely(rq->nr_running == 1))
6316 return;
6317
6318 clear_buddies(cfs_rq, se);
6319
6320 if (curr->policy != SCHED_BATCH) {
6321 update_rq_clock(rq);
6322 /*
6323 * Update run-time statistics of the 'current'.
6324 */
6325 update_curr(cfs_rq);
Mike Galbraith916671c2011-11-22 15:21:26 +01006326 /*
6327 * Tell update_rq_clock() that we've just updated,
6328 * so we don't do microscopic update in schedule()
6329 * and double the fastpath cost.
6330 */
Peter Zijlstra9edfbfe2015-01-05 11:18:11 +01006331 rq_clock_skip_update(rq, true);
Rik van Rielac53db52011-02-01 09:51:03 -05006332 }
6333
6334 set_skip_buddy(se);
6335}
6336
Mike Galbraithd95f4122011-02-01 09:50:51 -05006337static bool yield_to_task_fair(struct rq *rq, struct task_struct *p, bool preempt)
6338{
6339 struct sched_entity *se = &p->se;
6340
Paul Turner5238cdd2011-07-21 09:43:37 -07006341 /* throttled hierarchies are not runnable */
6342 if (!se->on_rq || throttled_hierarchy(cfs_rq_of(se)))
Mike Galbraithd95f4122011-02-01 09:50:51 -05006343 return false;
6344
6345 /* Tell the scheduler that we'd really like pse to run next. */
6346 set_next_buddy(se);
6347
Mike Galbraithd95f4122011-02-01 09:50:51 -05006348 yield_task_fair(rq);
6349
6350 return true;
6351}
6352
Peter Williams681f3e62007-10-24 18:23:51 +02006353#ifdef CONFIG_SMP
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02006354/**************************************************
Peter Zijlstrae9c84cb2012-07-03 13:53:26 +02006355 * Fair scheduling class load-balancing methods.
6356 *
6357 * BASICS
6358 *
6359 * The purpose of load-balancing is to achieve the same basic fairness the
6360 * per-cpu scheduler provides, namely provide a proportional amount of compute
6361 * time to each task. This is expressed in the following equation:
6362 *
6363 * W_i,n/P_i == W_j,n/P_j for all i,j (1)
6364 *
6365 * Where W_i,n is the n-th weight average for cpu i. The instantaneous weight
6366 * W_i,0 is defined as:
6367 *
6368 * W_i,0 = \Sum_j w_i,j (2)
6369 *
6370 * 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 +08006371 * is derived from the nice value as per sched_prio_to_weight[].
Peter Zijlstrae9c84cb2012-07-03 13:53:26 +02006372 *
6373 * The weight average is an exponential decay average of the instantaneous
6374 * weight:
6375 *
6376 * W'_i,n = (2^n - 1) / 2^n * W_i,n + 1 / 2^n * W_i,0 (3)
6377 *
Nicolas Pitreced549f2014-05-26 18:19:38 -04006378 * C_i is the compute capacity of cpu i, typically it is the
Peter Zijlstrae9c84cb2012-07-03 13:53:26 +02006379 * fraction of 'recent' time available for SCHED_OTHER task execution. But it
6380 * can also include other factors [XXX].
6381 *
6382 * To achieve this balance we define a measure of imbalance which follows
6383 * directly from (1):
6384 *
Nicolas Pitreced549f2014-05-26 18:19:38 -04006385 * 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 +02006386 *
6387 * We them move tasks around to minimize the imbalance. In the continuous
6388 * function space it is obvious this converges, in the discrete case we get
6389 * a few fun cases generally called infeasible weight scenarios.
6390 *
6391 * [XXX expand on:
6392 * - infeasible weights;
6393 * - local vs global optima in the discrete case. ]
6394 *
6395 *
6396 * SCHED DOMAINS
6397 *
6398 * In order to solve the imbalance equation (4), and avoid the obvious O(n^2)
6399 * for all i,j solution, we create a tree of cpus that follows the hardware
6400 * topology where each level pairs two lower groups (or better). This results
6401 * in O(log n) layers. Furthermore we reduce the number of cpus going up the
6402 * tree to only the first of the previous level and we decrease the frequency
6403 * of load-balance at each level inv. proportional to the number of cpus in
6404 * the groups.
6405 *
6406 * This yields:
6407 *
6408 * log_2 n 1 n
6409 * \Sum { --- * --- * 2^i } = O(n) (5)
6410 * i = 0 2^i 2^i
6411 * `- size of each group
6412 * | | `- number of cpus doing load-balance
6413 * | `- freq
6414 * `- sum over all levels
6415 *
6416 * Coupled with a limit on how many tasks we can migrate every balance pass,
6417 * this makes (5) the runtime complexity of the balancer.
6418 *
6419 * An important property here is that each CPU is still (indirectly) connected
6420 * to every other cpu in at most O(log n) steps:
6421 *
6422 * The adjacency matrix of the resulting graph is given by:
6423 *
Byungchul Park97a71422015-07-05 18:33:48 +09006424 * log_2 n
Peter Zijlstrae9c84cb2012-07-03 13:53:26 +02006425 * A_i,j = \Union (i % 2^k == 0) && i / 2^(k+1) == j / 2^(k+1) (6)
6426 * k = 0
6427 *
6428 * And you'll find that:
6429 *
6430 * A^(log_2 n)_i,j != 0 for all i,j (7)
6431 *
6432 * Showing there's indeed a path between every cpu in at most O(log n) steps.
6433 * The task movement gives a factor of O(m), giving a convergence complexity
6434 * of:
6435 *
6436 * O(nm log n), n := nr_cpus, m := nr_tasks (8)
6437 *
6438 *
6439 * WORK CONSERVING
6440 *
6441 * In order to avoid CPUs going idle while there's still work to do, new idle
6442 * balancing is more aggressive and has the newly idle cpu iterate up the domain
6443 * tree itself instead of relying on other CPUs to bring it work.
6444 *
6445 * This adds some complexity to both (5) and (8) but it reduces the total idle
6446 * time.
6447 *
6448 * [XXX more?]
6449 *
6450 *
6451 * CGROUPS
6452 *
6453 * Cgroups make a horror show out of (2), instead of a simple sum we get:
6454 *
6455 * s_k,i
6456 * W_i,0 = \Sum_j \Prod_k w_k * ----- (9)
6457 * S_k
6458 *
6459 * Where
6460 *
6461 * s_k,i = \Sum_j w_i,j,k and S_k = \Sum_i s_k,i (10)
6462 *
6463 * w_i,j,k is the weight of the j-th runnable task in the k-th cgroup on cpu i.
6464 *
6465 * The big problem is S_k, its a global sum needed to compute a local (W_i)
6466 * property.
6467 *
6468 * [XXX write more on how we solve this.. _after_ merging pjt's patches that
6469 * rewrite all of this once again.]
Byungchul Park97a71422015-07-05 18:33:48 +09006470 */
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02006471
Hiroshi Shimamotoed387b72012-01-31 11:40:32 +09006472static unsigned long __read_mostly max_load_balance_interval = HZ/10;
6473
Peter Zijlstra0ec8aa02013-10-07 11:29:33 +01006474enum fbq_type { regular, remote, all };
6475
Peter Zijlstraddcdf6e2012-02-22 19:27:40 +01006476#define LBF_ALL_PINNED 0x01
Peter Zijlstra367456c2012-02-20 21:49:09 +01006477#define LBF_NEED_BREAK 0x02
Peter Zijlstra62633222013-08-19 12:41:09 +02006478#define LBF_DST_PINNED 0x04
6479#define LBF_SOME_PINNED 0x08
Peter Zijlstraddcdf6e2012-02-22 19:27:40 +01006480
6481struct lb_env {
6482 struct sched_domain *sd;
6483
Peter Zijlstraddcdf6e2012-02-22 19:27:40 +01006484 struct rq *src_rq;
Prashanth Nageshappa85c1e7d2012-06-19 17:47:34 +05306485 int src_cpu;
Peter Zijlstraddcdf6e2012-02-22 19:27:40 +01006486
6487 int dst_cpu;
6488 struct rq *dst_rq;
6489
Srivatsa Vaddagiri88b8dac2012-06-19 17:43:15 +05306490 struct cpumask *dst_grpmask;
6491 int new_dst_cpu;
Peter Zijlstraddcdf6e2012-02-22 19:27:40 +01006492 enum cpu_idle_type idle;
Peter Zijlstrabd939f42012-05-02 14:20:37 +02006493 long imbalance;
Michael Wangb94031302012-07-12 16:10:13 +08006494 /* The set of CPUs under consideration for load-balancing */
6495 struct cpumask *cpus;
6496
Peter Zijlstraddcdf6e2012-02-22 19:27:40 +01006497 unsigned int flags;
Peter Zijlstra367456c2012-02-20 21:49:09 +01006498
6499 unsigned int loop;
6500 unsigned int loop_break;
6501 unsigned int loop_max;
Peter Zijlstra0ec8aa02013-10-07 11:29:33 +01006502
6503 enum fbq_type fbq_type;
Kirill Tkhai163122b2014-08-20 13:48:29 +04006504 struct list_head tasks;
Peter Zijlstraddcdf6e2012-02-22 19:27:40 +01006505};
6506
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01006507/*
Peter Zijlstra029632f2011-10-25 10:00:11 +02006508 * Is this task likely cache-hot:
6509 */
Hillf Danton5d5e2b12014-06-10 10:58:43 +02006510static int task_hot(struct task_struct *p, struct lb_env *env)
Peter Zijlstra029632f2011-10-25 10:00:11 +02006511{
6512 s64 delta;
6513
Kirill Tkhaie5673f22014-08-20 13:48:01 +04006514 lockdep_assert_held(&env->src_rq->lock);
6515
Peter Zijlstra029632f2011-10-25 10:00:11 +02006516 if (p->sched_class != &fair_sched_class)
6517 return 0;
6518
6519 if (unlikely(p->policy == SCHED_IDLE))
6520 return 0;
6521
6522 /*
6523 * Buddy candidates are cache hot:
6524 */
Hillf Danton5d5e2b12014-06-10 10:58:43 +02006525 if (sched_feat(CACHE_HOT_BUDDY) && env->dst_rq->nr_running &&
Peter Zijlstra029632f2011-10-25 10:00:11 +02006526 (&p->se == cfs_rq_of(&p->se)->next ||
6527 &p->se == cfs_rq_of(&p->se)->last))
6528 return 1;
6529
6530 if (sysctl_sched_migration_cost == -1)
6531 return 1;
6532 if (sysctl_sched_migration_cost == 0)
6533 return 0;
6534
Hillf Danton5d5e2b12014-06-10 10:58:43 +02006535 delta = rq_clock_task(env->src_rq) - p->se.exec_start;
Peter Zijlstra029632f2011-10-25 10:00:11 +02006536
6537 return delta < (s64)sysctl_sched_migration_cost;
6538}
6539
Mel Gorman3a7053b2013-10-07 11:29:00 +01006540#ifdef CONFIG_NUMA_BALANCING
Rik van Rielc1ceac62015-05-14 22:59:36 -04006541/*
Srikar Dronamraju2a1ed242015-06-16 17:25:59 +05306542 * Returns 1, if task migration degrades locality
6543 * Returns 0, if task migration improves locality i.e migration preferred.
6544 * Returns -1, if task migration is not affected by locality.
Rik van Rielc1ceac62015-05-14 22:59:36 -04006545 */
Srikar Dronamraju2a1ed242015-06-16 17:25:59 +05306546static int migrate_degrades_locality(struct task_struct *p, struct lb_env *env)
Mel Gorman3a7053b2013-10-07 11:29:00 +01006547{
Rik van Rielb1ad0652014-05-15 13:03:06 -04006548 struct numa_group *numa_group = rcu_dereference(p->numa_group);
Rik van Rielc1ceac62015-05-14 22:59:36 -04006549 unsigned long src_faults, dst_faults;
Mel Gorman3a7053b2013-10-07 11:29:00 +01006550 int src_nid, dst_nid;
6551
Srikar Dronamraju2a595722015-08-11 21:54:21 +05306552 if (!static_branch_likely(&sched_numa_balancing))
Srikar Dronamraju2a1ed242015-06-16 17:25:59 +05306553 return -1;
6554
Srikar Dronamrajuc3b9bc52015-08-11 16:30:12 +05306555 if (!p->numa_faults || !(env->sd->flags & SD_NUMA))
Srikar Dronamraju2a1ed242015-06-16 17:25:59 +05306556 return -1;
Mel Gorman7a0f3082013-10-07 11:29:01 +01006557
6558 src_nid = cpu_to_node(env->src_cpu);
6559 dst_nid = cpu_to_node(env->dst_cpu);
6560
Mel Gorman83e1d2c2013-10-07 11:29:27 +01006561 if (src_nid == dst_nid)
Srikar Dronamraju2a1ed242015-06-16 17:25:59 +05306562 return -1;
Mel Gorman7a0f3082013-10-07 11:29:01 +01006563
Srikar Dronamraju2a1ed242015-06-16 17:25:59 +05306564 /* Migrating away from the preferred node is always bad. */
6565 if (src_nid == p->numa_preferred_nid) {
6566 if (env->src_rq->nr_running > env->src_rq->nr_preferred_running)
6567 return 1;
6568 else
6569 return -1;
6570 }
Mel Gorman83e1d2c2013-10-07 11:29:27 +01006571
Rik van Rielc1ceac62015-05-14 22:59:36 -04006572 /* Encourage migration to the preferred node. */
6573 if (dst_nid == p->numa_preferred_nid)
Srikar Dronamraju2a1ed242015-06-16 17:25:59 +05306574 return 0;
Rik van Rielc1ceac62015-05-14 22:59:36 -04006575
6576 if (numa_group) {
6577 src_faults = group_faults(p, src_nid);
6578 dst_faults = group_faults(p, dst_nid);
6579 } else {
6580 src_faults = task_faults(p, src_nid);
6581 dst_faults = task_faults(p, dst_nid);
6582 }
6583
6584 return dst_faults < src_faults;
Mel Gorman7a0f3082013-10-07 11:29:01 +01006585}
6586
Mel Gorman3a7053b2013-10-07 11:29:00 +01006587#else
Srikar Dronamraju2a1ed242015-06-16 17:25:59 +05306588static inline int migrate_degrades_locality(struct task_struct *p,
Mel Gorman3a7053b2013-10-07 11:29:00 +01006589 struct lb_env *env)
6590{
Srikar Dronamraju2a1ed242015-06-16 17:25:59 +05306591 return -1;
Mel Gorman7a0f3082013-10-07 11:29:01 +01006592}
Mel Gorman3a7053b2013-10-07 11:29:00 +01006593#endif
6594
Peter Zijlstra029632f2011-10-25 10:00:11 +02006595/*
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01006596 * can_migrate_task - may task p from runqueue rq be migrated to this_cpu?
6597 */
6598static
Peter Zijlstra8e45cb52012-02-22 12:47:19 +01006599int can_migrate_task(struct task_struct *p, struct lb_env *env)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01006600{
Srikar Dronamraju2a1ed242015-06-16 17:25:59 +05306601 int tsk_cache_hot;
Kirill Tkhaie5673f22014-08-20 13:48:01 +04006602
6603 lockdep_assert_held(&env->src_rq->lock);
6604
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01006605 /*
6606 * We do not migrate tasks that are:
Joonsoo Kimd3198082013-04-23 17:27:40 +09006607 * 1) throttled_lb_pair, or
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01006608 * 2) cannot be migrated to this CPU due to cpus_allowed, or
Joonsoo Kimd3198082013-04-23 17:27:40 +09006609 * 3) running (obviously), or
6610 * 4) are cache-hot on their current CPU.
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01006611 */
Joonsoo Kimd3198082013-04-23 17:27:40 +09006612 if (throttled_lb_pair(task_group(p), env->src_cpu, env->dst_cpu))
6613 return 0;
6614
Peter Zijlstraddcdf6e2012-02-22 19:27:40 +01006615 if (!cpumask_test_cpu(env->dst_cpu, tsk_cpus_allowed(p))) {
Joonsoo Kime02e60c2013-04-23 17:27:42 +09006616 int cpu;
Srivatsa Vaddagiri88b8dac2012-06-19 17:43:15 +05306617
Josh Poimboeufae928822016-06-17 12:43:24 -05006618 schedstat_inc(p->se.statistics.nr_failed_migrations_affine);
Srivatsa Vaddagiri88b8dac2012-06-19 17:43:15 +05306619
Peter Zijlstra62633222013-08-19 12:41:09 +02006620 env->flags |= LBF_SOME_PINNED;
6621
Srivatsa Vaddagiri88b8dac2012-06-19 17:43:15 +05306622 /*
6623 * Remember if this task can be migrated to any other cpu in
6624 * our sched_group. We may want to revisit it if we couldn't
6625 * meet load balance goals by pulling other tasks on src_cpu.
6626 *
6627 * Also avoid computing new_dst_cpu if we have already computed
6628 * one in current iteration.
6629 */
Peter Zijlstra62633222013-08-19 12:41:09 +02006630 if (!env->dst_grpmask || (env->flags & LBF_DST_PINNED))
Srivatsa Vaddagiri88b8dac2012-06-19 17:43:15 +05306631 return 0;
6632
Joonsoo Kime02e60c2013-04-23 17:27:42 +09006633 /* Prevent to re-select dst_cpu via env's cpus */
6634 for_each_cpu_and(cpu, env->dst_grpmask, env->cpus) {
6635 if (cpumask_test_cpu(cpu, tsk_cpus_allowed(p))) {
Peter Zijlstra62633222013-08-19 12:41:09 +02006636 env->flags |= LBF_DST_PINNED;
Joonsoo Kime02e60c2013-04-23 17:27:42 +09006637 env->new_dst_cpu = cpu;
6638 break;
6639 }
Srivatsa Vaddagiri88b8dac2012-06-19 17:43:15 +05306640 }
Joonsoo Kime02e60c2013-04-23 17:27:42 +09006641
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01006642 return 0;
6643 }
Srivatsa Vaddagiri88b8dac2012-06-19 17:43:15 +05306644
6645 /* Record that we found atleast one task that could run on dst_cpu */
Peter Zijlstra8e45cb52012-02-22 12:47:19 +01006646 env->flags &= ~LBF_ALL_PINNED;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01006647
Peter Zijlstraddcdf6e2012-02-22 19:27:40 +01006648 if (task_running(env->src_rq, p)) {
Josh Poimboeufae928822016-06-17 12:43:24 -05006649 schedstat_inc(p->se.statistics.nr_failed_migrations_running);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01006650 return 0;
6651 }
6652
6653 /*
6654 * Aggressive migration if:
Mel Gorman3a7053b2013-10-07 11:29:00 +01006655 * 1) destination numa is preferred
6656 * 2) task is cache cold, or
6657 * 3) too many balance attempts have failed.
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01006658 */
Srikar Dronamraju2a1ed242015-06-16 17:25:59 +05306659 tsk_cache_hot = migrate_degrades_locality(p, env);
6660 if (tsk_cache_hot == -1)
6661 tsk_cache_hot = task_hot(p, env);
Mel Gorman3a7053b2013-10-07 11:29:00 +01006662
Srikar Dronamraju2a1ed242015-06-16 17:25:59 +05306663 if (tsk_cache_hot <= 0 ||
Kirill Tkhai7a96c232014-09-22 22:36:12 +04006664 env->sd->nr_balance_failed > env->sd->cache_nice_tries) {
Srikar Dronamraju2a1ed242015-06-16 17:25:59 +05306665 if (tsk_cache_hot == 1) {
Josh Poimboeufae928822016-06-17 12:43:24 -05006666 schedstat_inc(env->sd->lb_hot_gained[env->idle]);
6667 schedstat_inc(p->se.statistics.nr_forced_migrations);
Mel Gorman3a7053b2013-10-07 11:29:00 +01006668 }
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01006669 return 1;
6670 }
6671
Josh Poimboeufae928822016-06-17 12:43:24 -05006672 schedstat_inc(p->se.statistics.nr_failed_migrations_hot);
Zhang Hang4e2dcb72013-04-10 14:04:55 +08006673 return 0;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01006674}
6675
Peter Zijlstra897c3952009-12-17 17:45:42 +01006676/*
Kirill Tkhai163122b2014-08-20 13:48:29 +04006677 * detach_task() -- detach the task for the migration specified in env
Peter Zijlstra897c3952009-12-17 17:45:42 +01006678 */
Kirill Tkhai163122b2014-08-20 13:48:29 +04006679static void detach_task(struct task_struct *p, struct lb_env *env)
6680{
6681 lockdep_assert_held(&env->src_rq->lock);
6682
Kirill Tkhai163122b2014-08-20 13:48:29 +04006683 p->on_rq = TASK_ON_RQ_MIGRATING;
Joonwoo Park3ea94de2015-11-12 19:38:54 -08006684 deactivate_task(env->src_rq, p, 0);
Kirill Tkhai163122b2014-08-20 13:48:29 +04006685 set_task_cpu(p, env->dst_cpu);
6686}
6687
6688/*
Kirill Tkhaie5673f22014-08-20 13:48:01 +04006689 * detach_one_task() -- tries to dequeue exactly one task from env->src_rq, as
Peter Zijlstra897c3952009-12-17 17:45:42 +01006690 * part of active balancing operations within "domain".
Peter Zijlstra897c3952009-12-17 17:45:42 +01006691 *
Kirill Tkhaie5673f22014-08-20 13:48:01 +04006692 * Returns a task if successful and NULL otherwise.
Peter Zijlstra897c3952009-12-17 17:45:42 +01006693 */
Kirill Tkhaie5673f22014-08-20 13:48:01 +04006694static struct task_struct *detach_one_task(struct lb_env *env)
Peter Zijlstra897c3952009-12-17 17:45:42 +01006695{
6696 struct task_struct *p, *n;
Peter Zijlstra897c3952009-12-17 17:45:42 +01006697
Kirill Tkhaie5673f22014-08-20 13:48:01 +04006698 lockdep_assert_held(&env->src_rq->lock);
6699
Peter Zijlstra367456c2012-02-20 21:49:09 +01006700 list_for_each_entry_safe(p, n, &env->src_rq->cfs_tasks, se.group_node) {
Peter Zijlstra367456c2012-02-20 21:49:09 +01006701 if (!can_migrate_task(p, env))
6702 continue;
Peter Zijlstra897c3952009-12-17 17:45:42 +01006703
Kirill Tkhai163122b2014-08-20 13:48:29 +04006704 detach_task(p, env);
Kirill Tkhaie5673f22014-08-20 13:48:01 +04006705
Peter Zijlstra367456c2012-02-20 21:49:09 +01006706 /*
Kirill Tkhaie5673f22014-08-20 13:48:01 +04006707 * Right now, this is only the second place where
Kirill Tkhai163122b2014-08-20 13:48:29 +04006708 * lb_gained[env->idle] is updated (other is detach_tasks)
Kirill Tkhaie5673f22014-08-20 13:48:01 +04006709 * so we can safely collect stats here rather than
Kirill Tkhai163122b2014-08-20 13:48:29 +04006710 * inside detach_tasks().
Peter Zijlstra367456c2012-02-20 21:49:09 +01006711 */
Josh Poimboeufae928822016-06-17 12:43:24 -05006712 schedstat_inc(env->sd->lb_gained[env->idle]);
Kirill Tkhaie5673f22014-08-20 13:48:01 +04006713 return p;
Peter Zijlstra897c3952009-12-17 17:45:42 +01006714 }
Kirill Tkhaie5673f22014-08-20 13:48:01 +04006715 return NULL;
Peter Zijlstra897c3952009-12-17 17:45:42 +01006716}
6717
Peter Zijlstraeb953082012-04-17 13:38:40 +02006718static const unsigned int sched_nr_migrate_break = 32;
6719
Peter Zijlstra5d6523e2012-03-10 00:07:36 +01006720/*
Kirill Tkhai163122b2014-08-20 13:48:29 +04006721 * detach_tasks() -- tries to detach up to imbalance weighted load from
6722 * busiest_rq, as part of a balancing operation within domain "sd".
Peter Zijlstra5d6523e2012-03-10 00:07:36 +01006723 *
Kirill Tkhai163122b2014-08-20 13:48:29 +04006724 * Returns number of detached tasks if successful and 0 otherwise.
Peter Zijlstra5d6523e2012-03-10 00:07:36 +01006725 */
Kirill Tkhai163122b2014-08-20 13:48:29 +04006726static int detach_tasks(struct lb_env *env)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01006727{
Peter Zijlstra5d6523e2012-03-10 00:07:36 +01006728 struct list_head *tasks = &env->src_rq->cfs_tasks;
6729 struct task_struct *p;
Peter Zijlstra367456c2012-02-20 21:49:09 +01006730 unsigned long load;
Kirill Tkhai163122b2014-08-20 13:48:29 +04006731 int detached = 0;
6732
6733 lockdep_assert_held(&env->src_rq->lock);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01006734
Peter Zijlstrabd939f42012-05-02 14:20:37 +02006735 if (env->imbalance <= 0)
Peter Zijlstra5d6523e2012-03-10 00:07:36 +01006736 return 0;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01006737
Peter Zijlstra5d6523e2012-03-10 00:07:36 +01006738 while (!list_empty(tasks)) {
Yuyang Du985d3a42015-07-06 06:11:51 +08006739 /*
6740 * We don't want to steal all, otherwise we may be treated likewise,
6741 * which could at worst lead to a livelock crash.
6742 */
6743 if (env->idle != CPU_NOT_IDLE && env->src_rq->nr_running <= 1)
6744 break;
6745
Peter Zijlstra5d6523e2012-03-10 00:07:36 +01006746 p = list_first_entry(tasks, struct task_struct, se.group_node);
6747
Peter Zijlstra367456c2012-02-20 21:49:09 +01006748 env->loop++;
6749 /* We've more or less seen every task there is, call it quits */
Peter Zijlstra5d6523e2012-03-10 00:07:36 +01006750 if (env->loop > env->loop_max)
Peter Zijlstra367456c2012-02-20 21:49:09 +01006751 break;
Peter Zijlstra5d6523e2012-03-10 00:07:36 +01006752
6753 /* take a breather every nr_migrate tasks */
Peter Zijlstra367456c2012-02-20 21:49:09 +01006754 if (env->loop > env->loop_break) {
Peter Zijlstraeb953082012-04-17 13:38:40 +02006755 env->loop_break += sched_nr_migrate_break;
Peter Zijlstra8e45cb52012-02-22 12:47:19 +01006756 env->flags |= LBF_NEED_BREAK;
Peter Zijlstraee00e662009-12-17 17:25:20 +01006757 break;
Peter Zijlstraa195f002011-09-22 15:30:18 +02006758 }
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01006759
Joonsoo Kimd3198082013-04-23 17:27:40 +09006760 if (!can_migrate_task(p, env))
Peter Zijlstra367456c2012-02-20 21:49:09 +01006761 goto next;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01006762
Peter Zijlstra367456c2012-02-20 21:49:09 +01006763 load = task_h_load(p);
Peter Zijlstra5d6523e2012-03-10 00:07:36 +01006764
Peter Zijlstraeb953082012-04-17 13:38:40 +02006765 if (sched_feat(LB_MIN) && load < 16 && !env->sd->nr_balance_failed)
Peter Zijlstra367456c2012-02-20 21:49:09 +01006766 goto next;
6767
Peter Zijlstrabd939f42012-05-02 14:20:37 +02006768 if ((load / 2) > env->imbalance)
Peter Zijlstra367456c2012-02-20 21:49:09 +01006769 goto next;
6770
Kirill Tkhai163122b2014-08-20 13:48:29 +04006771 detach_task(p, env);
6772 list_add(&p->se.group_node, &env->tasks);
6773
6774 detached++;
Peter Zijlstrabd939f42012-05-02 14:20:37 +02006775 env->imbalance -= load;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01006776
6777#ifdef CONFIG_PREEMPT
Peter Zijlstraee00e662009-12-17 17:25:20 +01006778 /*
6779 * NEWIDLE balancing is a source of latency, so preemptible
Kirill Tkhai163122b2014-08-20 13:48:29 +04006780 * kernels will stop after the first task is detached to minimize
Peter Zijlstraee00e662009-12-17 17:25:20 +01006781 * the critical section.
6782 */
Peter Zijlstra5d6523e2012-03-10 00:07:36 +01006783 if (env->idle == CPU_NEWLY_IDLE)
Peter Zijlstraee00e662009-12-17 17:25:20 +01006784 break;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01006785#endif
6786
Peter Zijlstraee00e662009-12-17 17:25:20 +01006787 /*
6788 * We only want to steal up to the prescribed amount of
6789 * weighted load.
6790 */
Peter Zijlstrabd939f42012-05-02 14:20:37 +02006791 if (env->imbalance <= 0)
Peter Zijlstraee00e662009-12-17 17:25:20 +01006792 break;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01006793
Peter Zijlstra367456c2012-02-20 21:49:09 +01006794 continue;
6795next:
Peter Zijlstra5d6523e2012-03-10 00:07:36 +01006796 list_move_tail(&p->se.group_node, tasks);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01006797 }
Peter Zijlstra5d6523e2012-03-10 00:07:36 +01006798
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01006799 /*
Kirill Tkhai163122b2014-08-20 13:48:29 +04006800 * Right now, this is one of only two places we collect this stat
6801 * so we can safely collect detach_one_task() stats here rather
6802 * than inside detach_one_task().
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01006803 */
Josh Poimboeufae928822016-06-17 12:43:24 -05006804 schedstat_add(env->sd->lb_gained[env->idle], detached);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01006805
Kirill Tkhai163122b2014-08-20 13:48:29 +04006806 return detached;
6807}
6808
6809/*
6810 * attach_task() -- attach the task detached by detach_task() to its new rq.
6811 */
6812static void attach_task(struct rq *rq, struct task_struct *p)
6813{
6814 lockdep_assert_held(&rq->lock);
6815
6816 BUG_ON(task_rq(p) != rq);
Kirill Tkhai163122b2014-08-20 13:48:29 +04006817 activate_task(rq, p, 0);
Joonwoo Park3ea94de2015-11-12 19:38:54 -08006818 p->on_rq = TASK_ON_RQ_QUEUED;
Kirill Tkhai163122b2014-08-20 13:48:29 +04006819 check_preempt_curr(rq, p, 0);
6820}
6821
6822/*
6823 * attach_one_task() -- attaches the task returned from detach_one_task() to
6824 * its new rq.
6825 */
6826static void attach_one_task(struct rq *rq, struct task_struct *p)
6827{
6828 raw_spin_lock(&rq->lock);
6829 attach_task(rq, p);
6830 raw_spin_unlock(&rq->lock);
6831}
6832
6833/*
6834 * attach_tasks() -- attaches all tasks detached by detach_tasks() to their
6835 * new rq.
6836 */
6837static void attach_tasks(struct lb_env *env)
6838{
6839 struct list_head *tasks = &env->tasks;
6840 struct task_struct *p;
6841
6842 raw_spin_lock(&env->dst_rq->lock);
6843
6844 while (!list_empty(tasks)) {
6845 p = list_first_entry(tasks, struct task_struct, se.group_node);
6846 list_del_init(&p->se.group_node);
6847
6848 attach_task(env->dst_rq, p);
6849 }
6850
6851 raw_spin_unlock(&env->dst_rq->lock);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01006852}
6853
Peter Zijlstra230059de2009-12-17 17:47:12 +01006854#ifdef CONFIG_FAIR_GROUP_SCHED
Paul Turner48a16752012-10-04 13:18:31 +02006855static void update_blocked_averages(int cpu)
Peter Zijlstra9e3081c2010-11-15 15:47:02 -08006856{
Peter Zijlstra9e3081c2010-11-15 15:47:02 -08006857 struct rq *rq = cpu_rq(cpu);
Paul Turner48a16752012-10-04 13:18:31 +02006858 struct cfs_rq *cfs_rq;
6859 unsigned long flags;
Peter Zijlstra9e3081c2010-11-15 15:47:02 -08006860
Paul Turner48a16752012-10-04 13:18:31 +02006861 raw_spin_lock_irqsave(&rq->lock, flags);
6862 update_rq_clock(rq);
Yuyang Du9d89c252015-07-15 08:04:37 +08006863
Peter Zijlstra9763b672011-07-13 13:09:25 +02006864 /*
6865 * Iterates the task_group tree in a bottom up fashion, see
6866 * list_add_leaf_cfs_rq() for details.
6867 */
Paul Turner64660c82011-07-21 09:43:36 -07006868 for_each_leaf_cfs_rq(rq, cfs_rq) {
Yuyang Du9d89c252015-07-15 08:04:37 +08006869 /* throttled entities do not contribute to load */
6870 if (throttled_hierarchy(cfs_rq))
6871 continue;
Paul Turner48a16752012-10-04 13:18:31 +02006872
Steve Mucklea2c6c912016-03-24 15:26:07 -07006873 if (update_cfs_rq_load_avg(cfs_rq_clock_task(cfs_rq), cfs_rq, true))
Yuyang Du9d89c252015-07-15 08:04:37 +08006874 update_tg_load_avg(cfs_rq, 0);
6875 }
Paul Turner48a16752012-10-04 13:18:31 +02006876 raw_spin_unlock_irqrestore(&rq->lock, flags);
Peter Zijlstra9e3081c2010-11-15 15:47:02 -08006877}
6878
Peter Zijlstra9763b672011-07-13 13:09:25 +02006879/*
Vladimir Davydov68520792013-07-15 17:49:19 +04006880 * Compute the hierarchical load factor for cfs_rq and all its ascendants.
Peter Zijlstra9763b672011-07-13 13:09:25 +02006881 * This needs to be done in a top-down fashion because the load of a child
6882 * group is a fraction of its parents load.
6883 */
Vladimir Davydov68520792013-07-15 17:49:19 +04006884static void update_cfs_rq_h_load(struct cfs_rq *cfs_rq)
Peter Zijlstra9763b672011-07-13 13:09:25 +02006885{
Vladimir Davydov68520792013-07-15 17:49:19 +04006886 struct rq *rq = rq_of(cfs_rq);
6887 struct sched_entity *se = cfs_rq->tg->se[cpu_of(rq)];
Peter Zijlstraa35b6462012-08-08 21:46:40 +02006888 unsigned long now = jiffies;
Vladimir Davydov68520792013-07-15 17:49:19 +04006889 unsigned long load;
Peter Zijlstraa35b6462012-08-08 21:46:40 +02006890
Vladimir Davydov68520792013-07-15 17:49:19 +04006891 if (cfs_rq->last_h_load_update == now)
Peter Zijlstraa35b6462012-08-08 21:46:40 +02006892 return;
6893
Vladimir Davydov68520792013-07-15 17:49:19 +04006894 cfs_rq->h_load_next = NULL;
6895 for_each_sched_entity(se) {
6896 cfs_rq = cfs_rq_of(se);
6897 cfs_rq->h_load_next = se;
6898 if (cfs_rq->last_h_load_update == now)
6899 break;
6900 }
Peter Zijlstraa35b6462012-08-08 21:46:40 +02006901
Vladimir Davydov68520792013-07-15 17:49:19 +04006902 if (!se) {
Yuyang Du7ea241a2015-07-15 08:04:42 +08006903 cfs_rq->h_load = cfs_rq_load_avg(cfs_rq);
Vladimir Davydov68520792013-07-15 17:49:19 +04006904 cfs_rq->last_h_load_update = now;
6905 }
6906
6907 while ((se = cfs_rq->h_load_next) != NULL) {
6908 load = cfs_rq->h_load;
Yuyang Du7ea241a2015-07-15 08:04:42 +08006909 load = div64_ul(load * se->avg.load_avg,
6910 cfs_rq_load_avg(cfs_rq) + 1);
Vladimir Davydov68520792013-07-15 17:49:19 +04006911 cfs_rq = group_cfs_rq(se);
6912 cfs_rq->h_load = load;
6913 cfs_rq->last_h_load_update = now;
6914 }
Peter Zijlstra9763b672011-07-13 13:09:25 +02006915}
6916
Peter Zijlstra367456c2012-02-20 21:49:09 +01006917static unsigned long task_h_load(struct task_struct *p)
Peter Zijlstra230059de2009-12-17 17:47:12 +01006918{
Peter Zijlstra367456c2012-02-20 21:49:09 +01006919 struct cfs_rq *cfs_rq = task_cfs_rq(p);
Peter Zijlstra230059de2009-12-17 17:47:12 +01006920
Vladimir Davydov68520792013-07-15 17:49:19 +04006921 update_cfs_rq_h_load(cfs_rq);
Yuyang Du9d89c252015-07-15 08:04:37 +08006922 return div64_ul(p->se.avg.load_avg * cfs_rq->h_load,
Yuyang Du7ea241a2015-07-15 08:04:42 +08006923 cfs_rq_load_avg(cfs_rq) + 1);
Peter Zijlstra230059de2009-12-17 17:47:12 +01006924}
6925#else
Paul Turner48a16752012-10-04 13:18:31 +02006926static inline void update_blocked_averages(int cpu)
Peter Zijlstra9e3081c2010-11-15 15:47:02 -08006927{
Vincent Guittot6c1d47c2015-07-15 08:04:38 +08006928 struct rq *rq = cpu_rq(cpu);
6929 struct cfs_rq *cfs_rq = &rq->cfs;
6930 unsigned long flags;
6931
6932 raw_spin_lock_irqsave(&rq->lock, flags);
6933 update_rq_clock(rq);
Steve Mucklea2c6c912016-03-24 15:26:07 -07006934 update_cfs_rq_load_avg(cfs_rq_clock_task(cfs_rq), cfs_rq, true);
Vincent Guittot6c1d47c2015-07-15 08:04:38 +08006935 raw_spin_unlock_irqrestore(&rq->lock, flags);
Peter Zijlstra9e3081c2010-11-15 15:47:02 -08006936}
6937
Peter Zijlstra367456c2012-02-20 21:49:09 +01006938static unsigned long task_h_load(struct task_struct *p)
6939{
Yuyang Du9d89c252015-07-15 08:04:37 +08006940 return p->se.avg.load_avg;
Peter Zijlstra230059de2009-12-17 17:47:12 +01006941}
6942#endif
6943
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01006944/********** Helpers for find_busiest_group ************************/
Rik van Rielcaeb1782014-07-28 14:16:28 -04006945
6946enum group_type {
6947 group_other = 0,
6948 group_imbalanced,
6949 group_overloaded,
6950};
6951
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01006952/*
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01006953 * sg_lb_stats - stats of a sched_group required for load_balancing
6954 */
6955struct sg_lb_stats {
6956 unsigned long avg_load; /*Avg load across the CPUs of the group */
6957 unsigned long group_load; /* Total load over the CPUs of the group */
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01006958 unsigned long sum_weighted_load; /* Weighted load of group's tasks */
Joonsoo Kim56cf5152013-08-06 17:36:43 +09006959 unsigned long load_per_task;
Nicolas Pitre63b2ca32014-05-26 18:19:37 -04006960 unsigned long group_capacity;
Dietmar Eggemann9e91d612015-08-14 17:23:12 +01006961 unsigned long group_util; /* Total utilization of the group */
Peter Zijlstra147c5fc2013-08-19 15:22:57 +02006962 unsigned int sum_nr_running; /* Nr tasks running in the group */
Peter Zijlstra147c5fc2013-08-19 15:22:57 +02006963 unsigned int idle_cpus;
6964 unsigned int group_weight;
Rik van Rielcaeb1782014-07-28 14:16:28 -04006965 enum group_type group_type;
Vincent Guittotea678212015-02-27 16:54:11 +01006966 int group_no_capacity;
Peter Zijlstra0ec8aa02013-10-07 11:29:33 +01006967#ifdef CONFIG_NUMA_BALANCING
6968 unsigned int nr_numa_running;
6969 unsigned int nr_preferred_running;
6970#endif
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01006971};
6972
Joonsoo Kim56cf5152013-08-06 17:36:43 +09006973/*
6974 * sd_lb_stats - Structure to store the statistics of a sched_domain
6975 * during load balancing.
6976 */
6977struct sd_lb_stats {
6978 struct sched_group *busiest; /* Busiest group in this sd */
6979 struct sched_group *local; /* Local group in this sd */
6980 unsigned long total_load; /* Total load of all groups in sd */
Nicolas Pitre63b2ca32014-05-26 18:19:37 -04006981 unsigned long total_capacity; /* Total capacity of all groups in sd */
Joonsoo Kim56cf5152013-08-06 17:36:43 +09006982 unsigned long avg_load; /* Average load across all groups in sd */
6983
Joonsoo Kim56cf5152013-08-06 17:36:43 +09006984 struct sg_lb_stats busiest_stat;/* Statistics of the busiest group */
Peter Zijlstra147c5fc2013-08-19 15:22:57 +02006985 struct sg_lb_stats local_stat; /* Statistics of the local group */
Joonsoo Kim56cf5152013-08-06 17:36:43 +09006986};
6987
Peter Zijlstra147c5fc2013-08-19 15:22:57 +02006988static inline void init_sd_lb_stats(struct sd_lb_stats *sds)
6989{
6990 /*
6991 * Skimp on the clearing to avoid duplicate work. We can avoid clearing
6992 * local_stat because update_sg_lb_stats() does a full clear/assignment.
6993 * We must however clear busiest_stat::avg_load because
6994 * update_sd_pick_busiest() reads this before assignment.
6995 */
6996 *sds = (struct sd_lb_stats){
6997 .busiest = NULL,
6998 .local = NULL,
6999 .total_load = 0UL,
Nicolas Pitre63b2ca32014-05-26 18:19:37 -04007000 .total_capacity = 0UL,
Peter Zijlstra147c5fc2013-08-19 15:22:57 +02007001 .busiest_stat = {
7002 .avg_load = 0UL,
Rik van Rielcaeb1782014-07-28 14:16:28 -04007003 .sum_nr_running = 0,
7004 .group_type = group_other,
Peter Zijlstra147c5fc2013-08-19 15:22:57 +02007005 },
7006 };
7007}
7008
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007009/**
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007010 * get_sd_load_idx - Obtain the load index for a given sched domain.
7011 * @sd: The sched_domain whose load_idx is to be obtained.
Kamalesh Babulaled1b7732013-10-13 23:06:15 +05307012 * @idle: The idle status of the CPU for whose sd load_idx is obtained.
Yacine Belkadie69f6182013-07-12 20:45:47 +02007013 *
7014 * Return: The load index.
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007015 */
7016static inline int get_sd_load_idx(struct sched_domain *sd,
7017 enum cpu_idle_type idle)
7018{
7019 int load_idx;
7020
7021 switch (idle) {
7022 case CPU_NOT_IDLE:
7023 load_idx = sd->busy_idx;
7024 break;
7025
7026 case CPU_NEWLY_IDLE:
7027 load_idx = sd->newidle_idx;
7028 break;
7029 default:
7030 load_idx = sd->idle_idx;
7031 break;
7032 }
7033
7034 return load_idx;
7035}
7036
Nicolas Pitreced549f2014-05-26 18:19:38 -04007037static unsigned long scale_rt_capacity(int cpu)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007038{
7039 struct rq *rq = cpu_rq(cpu);
Vincent Guittotb5b48602015-02-27 16:54:08 +01007040 u64 total, used, age_stamp, avg;
Peter Zijlstracadefd32014-02-27 10:40:35 +01007041 s64 delta;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007042
Peter Zijlstrab654f7d2012-05-22 14:04:28 +02007043 /*
7044 * Since we're reading these variables without serialization make sure
7045 * we read them once before doing sanity checks on them.
7046 */
Jason Low316c1608d2015-04-28 13:00:20 -07007047 age_stamp = READ_ONCE(rq->age_stamp);
7048 avg = READ_ONCE(rq->rt_avg);
Peter Zijlstracebde6d2015-01-05 11:18:10 +01007049 delta = __rq_clock_broken(rq) - age_stamp;
Venkatesh Pallipadiaa483802010-10-04 17:03:22 -07007050
Peter Zijlstracadefd32014-02-27 10:40:35 +01007051 if (unlikely(delta < 0))
7052 delta = 0;
7053
7054 total = sched_avg_period() + delta;
Peter Zijlstrab654f7d2012-05-22 14:04:28 +02007055
Vincent Guittotb5b48602015-02-27 16:54:08 +01007056 used = div_u64(avg, total);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007057
Vincent Guittotb5b48602015-02-27 16:54:08 +01007058 if (likely(used < SCHED_CAPACITY_SCALE))
7059 return SCHED_CAPACITY_SCALE - used;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007060
Vincent Guittotb5b48602015-02-27 16:54:08 +01007061 return 1;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007062}
7063
Nicolas Pitreced549f2014-05-26 18:19:38 -04007064static void update_cpu_capacity(struct sched_domain *sd, int cpu)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007065{
Morten Rasmussen8cd56012015-08-14 17:23:10 +01007066 unsigned long capacity = arch_scale_cpu_capacity(sd, cpu);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007067 struct sched_group *sdg = sd->groups;
7068
Vincent Guittotca6d75e2015-02-27 16:54:09 +01007069 cpu_rq(cpu)->cpu_capacity_orig = capacity;
Srivatsa Vaddagiri9d5efe02010-06-08 14:57:02 +10007070
Nicolas Pitreced549f2014-05-26 18:19:38 -04007071 capacity *= scale_rt_capacity(cpu);
Nicolas Pitreca8ce3d2014-05-26 18:19:39 -04007072 capacity >>= SCHED_CAPACITY_SHIFT;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007073
Nicolas Pitreced549f2014-05-26 18:19:38 -04007074 if (!capacity)
7075 capacity = 1;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007076
Nicolas Pitreced549f2014-05-26 18:19:38 -04007077 cpu_rq(cpu)->cpu_capacity = capacity;
7078 sdg->sgc->capacity = capacity;
Morten Rasmussenbf475ce2016-10-14 14:41:09 +01007079 sdg->sgc->min_capacity = capacity;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007080}
7081
Nicolas Pitre63b2ca32014-05-26 18:19:37 -04007082void update_group_capacity(struct sched_domain *sd, int cpu)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007083{
7084 struct sched_domain *child = sd->child;
7085 struct sched_group *group, *sdg = sd->groups;
Morten Rasmussenbf475ce2016-10-14 14:41:09 +01007086 unsigned long capacity, min_capacity;
Vincent Guittot4ec44122011-12-12 20:21:08 +01007087 unsigned long interval;
7088
7089 interval = msecs_to_jiffies(sd->balance_interval);
7090 interval = clamp(interval, 1UL, max_load_balance_interval);
Nicolas Pitre63b2ca32014-05-26 18:19:37 -04007091 sdg->sgc->next_update = jiffies + interval;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007092
7093 if (!child) {
Nicolas Pitreced549f2014-05-26 18:19:38 -04007094 update_cpu_capacity(sd, cpu);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007095 return;
7096 }
7097
Vincent Guittotdc7ff762015-03-03 11:35:03 +01007098 capacity = 0;
Morten Rasmussenbf475ce2016-10-14 14:41:09 +01007099 min_capacity = ULONG_MAX;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007100
Peter Zijlstra74a5ce22012-05-23 18:00:43 +02007101 if (child->flags & SD_OVERLAP) {
7102 /*
7103 * SD_OVERLAP domains cannot assume that child groups
7104 * span the current group.
7105 */
7106
Peter Zijlstra863bffc2013-08-28 11:44:39 +02007107 for_each_cpu(cpu, sched_group_cpus(sdg)) {
Nicolas Pitre63b2ca32014-05-26 18:19:37 -04007108 struct sched_group_capacity *sgc;
Srikar Dronamraju9abf24d2013-11-12 22:11:26 +05307109 struct rq *rq = cpu_rq(cpu);
Peter Zijlstra863bffc2013-08-28 11:44:39 +02007110
Srikar Dronamraju9abf24d2013-11-12 22:11:26 +05307111 /*
Nicolas Pitre63b2ca32014-05-26 18:19:37 -04007112 * build_sched_domains() -> init_sched_groups_capacity()
Srikar Dronamraju9abf24d2013-11-12 22:11:26 +05307113 * gets here before we've attached the domains to the
7114 * runqueues.
7115 *
Nicolas Pitreced549f2014-05-26 18:19:38 -04007116 * Use capacity_of(), which is set irrespective of domains
7117 * in update_cpu_capacity().
Srikar Dronamraju9abf24d2013-11-12 22:11:26 +05307118 *
Vincent Guittotdc7ff762015-03-03 11:35:03 +01007119 * This avoids capacity from being 0 and
Srikar Dronamraju9abf24d2013-11-12 22:11:26 +05307120 * causing divide-by-zero issues on boot.
Srikar Dronamraju9abf24d2013-11-12 22:11:26 +05307121 */
7122 if (unlikely(!rq->sd)) {
Nicolas Pitreced549f2014-05-26 18:19:38 -04007123 capacity += capacity_of(cpu);
Morten Rasmussenbf475ce2016-10-14 14:41:09 +01007124 } else {
7125 sgc = rq->sd->groups->sgc;
7126 capacity += sgc->capacity;
Srikar Dronamraju9abf24d2013-11-12 22:11:26 +05307127 }
7128
Morten Rasmussenbf475ce2016-10-14 14:41:09 +01007129 min_capacity = min(capacity, min_capacity);
Peter Zijlstra863bffc2013-08-28 11:44:39 +02007130 }
Peter Zijlstra74a5ce22012-05-23 18:00:43 +02007131 } else {
7132 /*
7133 * !SD_OVERLAP domains can assume that child groups
7134 * span the current group.
Byungchul Park97a71422015-07-05 18:33:48 +09007135 */
Peter Zijlstra74a5ce22012-05-23 18:00:43 +02007136
7137 group = child->groups;
7138 do {
Morten Rasmussenbf475ce2016-10-14 14:41:09 +01007139 struct sched_group_capacity *sgc = group->sgc;
7140
7141 capacity += sgc->capacity;
7142 min_capacity = min(sgc->min_capacity, min_capacity);
Peter Zijlstra74a5ce22012-05-23 18:00:43 +02007143 group = group->next;
7144 } while (group != child->groups);
7145 }
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007146
Nicolas Pitre63b2ca32014-05-26 18:19:37 -04007147 sdg->sgc->capacity = capacity;
Morten Rasmussenbf475ce2016-10-14 14:41:09 +01007148 sdg->sgc->min_capacity = min_capacity;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007149}
7150
Srivatsa Vaddagiri9d5efe02010-06-08 14:57:02 +10007151/*
Vincent Guittotea678212015-02-27 16:54:11 +01007152 * Check whether the capacity of the rq has been noticeably reduced by side
7153 * activity. The imbalance_pct is used for the threshold.
7154 * Return true is the capacity is reduced
Srivatsa Vaddagiri9d5efe02010-06-08 14:57:02 +10007155 */
7156static inline int
Vincent Guittotea678212015-02-27 16:54:11 +01007157check_cpu_capacity(struct rq *rq, struct sched_domain *sd)
Srivatsa Vaddagiri9d5efe02010-06-08 14:57:02 +10007158{
Vincent Guittotea678212015-02-27 16:54:11 +01007159 return ((rq->cpu_capacity * sd->imbalance_pct) <
7160 (rq->cpu_capacity_orig * 100));
Srivatsa Vaddagiri9d5efe02010-06-08 14:57:02 +10007161}
7162
Peter Zijlstra30ce5da2013-08-15 20:29:29 +02007163/*
7164 * Group imbalance indicates (and tries to solve) the problem where balancing
7165 * groups is inadequate due to tsk_cpus_allowed() constraints.
7166 *
7167 * Imagine a situation of two groups of 4 cpus each and 4 tasks each with a
7168 * cpumask covering 1 cpu of the first group and 3 cpus of the second group.
7169 * Something like:
7170 *
7171 * { 0 1 2 3 } { 4 5 6 7 }
7172 * * * * *
7173 *
7174 * If we were to balance group-wise we'd place two tasks in the first group and
7175 * two tasks in the second group. Clearly this is undesired as it will overload
7176 * cpu 3 and leave one of the cpus in the second group unused.
7177 *
7178 * The current solution to this issue is detecting the skew in the first group
Peter Zijlstra62633222013-08-19 12:41:09 +02007179 * by noticing the lower domain failed to reach balance and had difficulty
7180 * moving tasks due to affinity constraints.
Peter Zijlstra30ce5da2013-08-15 20:29:29 +02007181 *
7182 * When this is so detected; this group becomes a candidate for busiest; see
Kamalesh Babulaled1b7732013-10-13 23:06:15 +05307183 * update_sd_pick_busiest(). And calculate_imbalance() and
Peter Zijlstra62633222013-08-19 12:41:09 +02007184 * find_busiest_group() avoid some of the usual balance conditions to allow it
Peter Zijlstra30ce5da2013-08-15 20:29:29 +02007185 * to create an effective group imbalance.
7186 *
7187 * This is a somewhat tricky proposition since the next run might not find the
7188 * group imbalance and decide the groups need to be balanced again. A most
7189 * subtle and fragile situation.
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007190 */
Peter Zijlstra30ce5da2013-08-15 20:29:29 +02007191
Peter Zijlstra62633222013-08-19 12:41:09 +02007192static inline int sg_imbalanced(struct sched_group *group)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007193{
Nicolas Pitre63b2ca32014-05-26 18:19:37 -04007194 return group->sgc->imbalance;
Peter Zijlstra30ce5da2013-08-15 20:29:29 +02007195}
7196
Peter Zijlstrab37d9312013-08-28 11:50:34 +02007197/*
Vincent Guittotea678212015-02-27 16:54:11 +01007198 * group_has_capacity returns true if the group has spare capacity that could
7199 * be used by some tasks.
7200 * We consider that a group has spare capacity if the * number of task is
Dietmar Eggemann9e91d612015-08-14 17:23:12 +01007201 * smaller than the number of CPUs or if the utilization is lower than the
7202 * available capacity for CFS tasks.
Vincent Guittotea678212015-02-27 16:54:11 +01007203 * For the latter, we use a threshold to stabilize the state, to take into
7204 * account the variance of the tasks' load and to return true if the available
7205 * capacity in meaningful for the load balancer.
7206 * As an example, an available capacity of 1% can appear but it doesn't make
7207 * any benefit for the load balance.
Peter Zijlstrab37d9312013-08-28 11:50:34 +02007208 */
Vincent Guittotea678212015-02-27 16:54:11 +01007209static inline bool
7210group_has_capacity(struct lb_env *env, struct sg_lb_stats *sgs)
Peter Zijlstrab37d9312013-08-28 11:50:34 +02007211{
Vincent Guittotea678212015-02-27 16:54:11 +01007212 if (sgs->sum_nr_running < sgs->group_weight)
7213 return true;
Peter Zijlstrab37d9312013-08-28 11:50:34 +02007214
Vincent Guittotea678212015-02-27 16:54:11 +01007215 if ((sgs->group_capacity * 100) >
Dietmar Eggemann9e91d612015-08-14 17:23:12 +01007216 (sgs->group_util * env->sd->imbalance_pct))
Vincent Guittotea678212015-02-27 16:54:11 +01007217 return true;
Peter Zijlstrab37d9312013-08-28 11:50:34 +02007218
Vincent Guittotea678212015-02-27 16:54:11 +01007219 return false;
Peter Zijlstrab37d9312013-08-28 11:50:34 +02007220}
7221
Vincent Guittotea678212015-02-27 16:54:11 +01007222/*
7223 * group_is_overloaded returns true if the group has more tasks than it can
7224 * handle.
7225 * group_is_overloaded is not equals to !group_has_capacity because a group
7226 * with the exact right number of tasks, has no more spare capacity but is not
7227 * overloaded so both group_has_capacity and group_is_overloaded return
7228 * false.
7229 */
7230static inline bool
7231group_is_overloaded(struct lb_env *env, struct sg_lb_stats *sgs)
Rik van Rielcaeb1782014-07-28 14:16:28 -04007232{
Vincent Guittotea678212015-02-27 16:54:11 +01007233 if (sgs->sum_nr_running <= sgs->group_weight)
7234 return false;
7235
7236 if ((sgs->group_capacity * 100) <
Dietmar Eggemann9e91d612015-08-14 17:23:12 +01007237 (sgs->group_util * env->sd->imbalance_pct))
Vincent Guittotea678212015-02-27 16:54:11 +01007238 return true;
7239
7240 return false;
7241}
7242
Morten Rasmussen9e0994c2016-10-14 14:41:10 +01007243/*
7244 * group_smaller_cpu_capacity: Returns true if sched_group sg has smaller
7245 * per-CPU capacity than sched_group ref.
7246 */
7247static inline bool
7248group_smaller_cpu_capacity(struct sched_group *sg, struct sched_group *ref)
7249{
7250 return sg->sgc->min_capacity * capacity_margin <
7251 ref->sgc->min_capacity * 1024;
7252}
7253
Leo Yan79a89f92015-09-15 18:56:45 +08007254static inline enum
7255group_type group_classify(struct sched_group *group,
7256 struct sg_lb_stats *sgs)
Vincent Guittotea678212015-02-27 16:54:11 +01007257{
7258 if (sgs->group_no_capacity)
Rik van Rielcaeb1782014-07-28 14:16:28 -04007259 return group_overloaded;
7260
7261 if (sg_imbalanced(group))
7262 return group_imbalanced;
7263
7264 return group_other;
7265}
7266
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007267/**
7268 * update_sg_lb_stats - Update sched_group's statistics for load balancing.
7269 * @env: The load balancing environment.
7270 * @group: sched_group whose statistics are to be updated.
7271 * @load_idx: Load index of sched_domain of this_cpu for load calc.
7272 * @local_group: Does group contain this_cpu.
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007273 * @sgs: variable to hold the statistics for this group.
Masanari Iidacd3bd4e2014-07-28 12:38:06 +09007274 * @overload: Indicate more than one runnable task for any CPU.
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007275 */
7276static inline void update_sg_lb_stats(struct lb_env *env,
7277 struct sched_group *group, int load_idx,
Tim Chen4486edd2014-06-23 12:16:49 -07007278 int local_group, struct sg_lb_stats *sgs,
7279 bool *overload)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007280{
Peter Zijlstra30ce5da2013-08-15 20:29:29 +02007281 unsigned long load;
Waiman Longa426f992015-11-25 14:09:38 -05007282 int i, nr_running;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007283
Peter Zijlstrab72ff132013-08-28 10:32:32 +02007284 memset(sgs, 0, sizeof(*sgs));
7285
Michael Wangb94031302012-07-12 16:10:13 +08007286 for_each_cpu_and(i, sched_group_cpus(group), env->cpus) {
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007287 struct rq *rq = cpu_rq(i);
7288
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007289 /* Bias balancing toward cpus of our domain */
Peter Zijlstra62633222013-08-19 12:41:09 +02007290 if (local_group)
Peter Zijlstra04f733b2012-05-11 00:12:02 +02007291 load = target_load(i, load_idx);
Peter Zijlstra62633222013-08-19 12:41:09 +02007292 else
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007293 load = source_load(i, load_idx);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007294
7295 sgs->group_load += load;
Dietmar Eggemann9e91d612015-08-14 17:23:12 +01007296 sgs->group_util += cpu_util(i);
Vincent Guittot65fdac02014-08-26 13:06:46 +02007297 sgs->sum_nr_running += rq->cfs.h_nr_running;
Tim Chen4486edd2014-06-23 12:16:49 -07007298
Waiman Longa426f992015-11-25 14:09:38 -05007299 nr_running = rq->nr_running;
7300 if (nr_running > 1)
Tim Chen4486edd2014-06-23 12:16:49 -07007301 *overload = true;
7302
Peter Zijlstra0ec8aa02013-10-07 11:29:33 +01007303#ifdef CONFIG_NUMA_BALANCING
7304 sgs->nr_numa_running += rq->nr_numa_running;
7305 sgs->nr_preferred_running += rq->nr_preferred_running;
7306#endif
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007307 sgs->sum_weighted_load += weighted_cpuload(i);
Waiman Longa426f992015-11-25 14:09:38 -05007308 /*
7309 * No need to call idle_cpu() if nr_running is not 0
7310 */
7311 if (!nr_running && idle_cpu(i))
Suresh Siddhaaae6d3d2010-09-17 15:02:32 -07007312 sgs->idle_cpus++;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007313 }
7314
Nicolas Pitre63b2ca32014-05-26 18:19:37 -04007315 /* Adjust by relative CPU capacity of the group */
7316 sgs->group_capacity = group->sgc->capacity;
Nicolas Pitreca8ce3d2014-05-26 18:19:39 -04007317 sgs->avg_load = (sgs->group_load*SCHED_CAPACITY_SCALE) / sgs->group_capacity;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007318
Suresh Siddhadd5feea2010-02-23 16:13:52 -08007319 if (sgs->sum_nr_running)
Peter Zijlstra38d0f772013-08-15 19:47:56 +02007320 sgs->load_per_task = sgs->sum_weighted_load / sgs->sum_nr_running;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007321
Suresh Siddhaaae6d3d2010-09-17 15:02:32 -07007322 sgs->group_weight = group->group_weight;
Peter Zijlstrab37d9312013-08-28 11:50:34 +02007323
Vincent Guittotea678212015-02-27 16:54:11 +01007324 sgs->group_no_capacity = group_is_overloaded(env, sgs);
Leo Yan79a89f92015-09-15 18:56:45 +08007325 sgs->group_type = group_classify(group, sgs);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007326}
7327
7328/**
Michael Neuling532cb4c2010-06-08 14:57:02 +10007329 * update_sd_pick_busiest - return 1 on busiest group
Randy Dunlapcd968912012-06-08 13:18:33 -07007330 * @env: The load balancing environment.
Michael Neuling532cb4c2010-06-08 14:57:02 +10007331 * @sds: sched_domain statistics
7332 * @sg: sched_group candidate to be checked for being the busiest
Michael Neulingb6b12292010-06-10 12:06:21 +10007333 * @sgs: sched_group statistics
Michael Neuling532cb4c2010-06-08 14:57:02 +10007334 *
7335 * Determine if @sg is a busier group than the previously selected
7336 * busiest group.
Yacine Belkadie69f6182013-07-12 20:45:47 +02007337 *
7338 * Return: %true if @sg is a busier group than the previously selected
7339 * busiest group. %false otherwise.
Michael Neuling532cb4c2010-06-08 14:57:02 +10007340 */
Peter Zijlstrabd939f42012-05-02 14:20:37 +02007341static bool update_sd_pick_busiest(struct lb_env *env,
Michael Neuling532cb4c2010-06-08 14:57:02 +10007342 struct sd_lb_stats *sds,
7343 struct sched_group *sg,
Peter Zijlstrabd939f42012-05-02 14:20:37 +02007344 struct sg_lb_stats *sgs)
Michael Neuling532cb4c2010-06-08 14:57:02 +10007345{
Rik van Rielcaeb1782014-07-28 14:16:28 -04007346 struct sg_lb_stats *busiest = &sds->busiest_stat;
Michael Neuling532cb4c2010-06-08 14:57:02 +10007347
Rik van Rielcaeb1782014-07-28 14:16:28 -04007348 if (sgs->group_type > busiest->group_type)
Michael Neuling532cb4c2010-06-08 14:57:02 +10007349 return true;
7350
Rik van Rielcaeb1782014-07-28 14:16:28 -04007351 if (sgs->group_type < busiest->group_type)
7352 return false;
7353
7354 if (sgs->avg_load <= busiest->avg_load)
7355 return false;
7356
Morten Rasmussen9e0994c2016-10-14 14:41:10 +01007357 if (!(env->sd->flags & SD_ASYM_CPUCAPACITY))
7358 goto asym_packing;
7359
7360 /*
7361 * Candidate sg has no more than one task per CPU and
7362 * has higher per-CPU capacity. Migrating tasks to less
7363 * capable CPUs may harm throughput. Maximize throughput,
7364 * power/energy consequences are not considered.
7365 */
7366 if (sgs->sum_nr_running <= sgs->group_weight &&
7367 group_smaller_cpu_capacity(sds->local, sg))
7368 return false;
7369
7370asym_packing:
Rik van Rielcaeb1782014-07-28 14:16:28 -04007371 /* This is the busiest node in its class. */
7372 if (!(env->sd->flags & SD_ASYM_PACKING))
Michael Neuling532cb4c2010-06-08 14:57:02 +10007373 return true;
7374
Srikar Dronamraju1f621e02016-04-06 18:47:40 +05307375 /* No ASYM_PACKING if target cpu is already busy */
7376 if (env->idle == CPU_NOT_IDLE)
7377 return true;
Michael Neuling532cb4c2010-06-08 14:57:02 +10007378 /*
7379 * ASYM_PACKING needs to move all the work to the lowest
7380 * numbered CPUs in the group, therefore mark all groups
7381 * higher than ourself as busy.
7382 */
Rik van Rielcaeb1782014-07-28 14:16:28 -04007383 if (sgs->sum_nr_running && env->dst_cpu < group_first_cpu(sg)) {
Michael Neuling532cb4c2010-06-08 14:57:02 +10007384 if (!sds->busiest)
7385 return true;
7386
Srikar Dronamraju1f621e02016-04-06 18:47:40 +05307387 /* Prefer to move from highest possible cpu's work */
7388 if (group_first_cpu(sds->busiest) < group_first_cpu(sg))
Michael Neuling532cb4c2010-06-08 14:57:02 +10007389 return true;
7390 }
7391
7392 return false;
7393}
7394
Peter Zijlstra0ec8aa02013-10-07 11:29:33 +01007395#ifdef CONFIG_NUMA_BALANCING
7396static inline enum fbq_type fbq_classify_group(struct sg_lb_stats *sgs)
7397{
7398 if (sgs->sum_nr_running > sgs->nr_numa_running)
7399 return regular;
7400 if (sgs->sum_nr_running > sgs->nr_preferred_running)
7401 return remote;
7402 return all;
7403}
7404
7405static inline enum fbq_type fbq_classify_rq(struct rq *rq)
7406{
7407 if (rq->nr_running > rq->nr_numa_running)
7408 return regular;
7409 if (rq->nr_running > rq->nr_preferred_running)
7410 return remote;
7411 return all;
7412}
7413#else
7414static inline enum fbq_type fbq_classify_group(struct sg_lb_stats *sgs)
7415{
7416 return all;
7417}
7418
7419static inline enum fbq_type fbq_classify_rq(struct rq *rq)
7420{
7421 return regular;
7422}
7423#endif /* CONFIG_NUMA_BALANCING */
7424
Michael Neuling532cb4c2010-06-08 14:57:02 +10007425/**
Hui Kang461819a2011-10-11 23:00:59 -04007426 * update_sd_lb_stats - Update sched_domain's statistics for load balancing.
Randy Dunlapcd968912012-06-08 13:18:33 -07007427 * @env: The load balancing environment.
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007428 * @sds: variable to hold the statistics for this sched_domain.
7429 */
Peter Zijlstra0ec8aa02013-10-07 11:29:33 +01007430static inline void update_sd_lb_stats(struct lb_env *env, struct sd_lb_stats *sds)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007431{
Peter Zijlstrabd939f42012-05-02 14:20:37 +02007432 struct sched_domain *child = env->sd->child;
7433 struct sched_group *sg = env->sd->groups;
Joonsoo Kim56cf5152013-08-06 17:36:43 +09007434 struct sg_lb_stats tmp_sgs;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007435 int load_idx, prefer_sibling = 0;
Tim Chen4486edd2014-06-23 12:16:49 -07007436 bool overload = false;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007437
7438 if (child && child->flags & SD_PREFER_SIBLING)
7439 prefer_sibling = 1;
7440
Peter Zijlstrabd939f42012-05-02 14:20:37 +02007441 load_idx = get_sd_load_idx(env->sd, env->idle);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007442
7443 do {
Joonsoo Kim56cf5152013-08-06 17:36:43 +09007444 struct sg_lb_stats *sgs = &tmp_sgs;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007445 int local_group;
7446
Peter Zijlstrabd939f42012-05-02 14:20:37 +02007447 local_group = cpumask_test_cpu(env->dst_cpu, sched_group_cpus(sg));
Joonsoo Kim56cf5152013-08-06 17:36:43 +09007448 if (local_group) {
7449 sds->local = sg;
7450 sgs = &sds->local_stat;
Peter Zijlstrab72ff132013-08-28 10:32:32 +02007451
7452 if (env->idle != CPU_NEWLY_IDLE ||
Nicolas Pitre63b2ca32014-05-26 18:19:37 -04007453 time_after_eq(jiffies, sg->sgc->next_update))
7454 update_group_capacity(env->sd, env->dst_cpu);
Joonsoo Kim56cf5152013-08-06 17:36:43 +09007455 }
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007456
Tim Chen4486edd2014-06-23 12:16:49 -07007457 update_sg_lb_stats(env, sg, load_idx, local_group, sgs,
7458 &overload);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007459
Peter Zijlstrab72ff132013-08-28 10:32:32 +02007460 if (local_group)
7461 goto next_group;
7462
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007463 /*
7464 * In case the child domain prefers tasks go to siblings
Vincent Guittotea678212015-02-27 16:54:11 +01007465 * first, lower the sg capacity so that we'll try
Nikhil Rao75dd3212010-10-15 13:12:30 -07007466 * and move all the excess tasks away. We lower the capacity
7467 * of a group only if the local group has the capacity to fit
Vincent Guittotea678212015-02-27 16:54:11 +01007468 * these excess tasks. The extra check prevents the case where
7469 * you always pull from the heaviest group when it is already
7470 * under-utilized (possible with a large weight task outweighs
7471 * the tasks on the system).
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007472 */
Peter Zijlstrab72ff132013-08-28 10:32:32 +02007473 if (prefer_sibling && sds->local &&
Vincent Guittotea678212015-02-27 16:54:11 +01007474 group_has_capacity(env, &sds->local_stat) &&
7475 (sgs->sum_nr_running > 1)) {
7476 sgs->group_no_capacity = 1;
Leo Yan79a89f92015-09-15 18:56:45 +08007477 sgs->group_type = group_classify(sg, sgs);
Wanpeng Licb0b9f22014-11-05 07:44:50 +08007478 }
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007479
Peter Zijlstrab72ff132013-08-28 10:32:32 +02007480 if (update_sd_pick_busiest(env, sds, sg, sgs)) {
Michael Neuling532cb4c2010-06-08 14:57:02 +10007481 sds->busiest = sg;
Joonsoo Kim56cf5152013-08-06 17:36:43 +09007482 sds->busiest_stat = *sgs;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007483 }
7484
Peter Zijlstrab72ff132013-08-28 10:32:32 +02007485next_group:
7486 /* Now, start updating sd_lb_stats */
7487 sds->total_load += sgs->group_load;
Nicolas Pitre63b2ca32014-05-26 18:19:37 -04007488 sds->total_capacity += sgs->group_capacity;
Peter Zijlstrab72ff132013-08-28 10:32:32 +02007489
Michael Neuling532cb4c2010-06-08 14:57:02 +10007490 sg = sg->next;
Peter Zijlstrabd939f42012-05-02 14:20:37 +02007491 } while (sg != env->sd->groups);
Peter Zijlstra0ec8aa02013-10-07 11:29:33 +01007492
7493 if (env->sd->flags & SD_NUMA)
7494 env->fbq_type = fbq_classify_group(&sds->busiest_stat);
Tim Chen4486edd2014-06-23 12:16:49 -07007495
7496 if (!env->sd->parent) {
7497 /* update overload indicator if we are at root domain */
7498 if (env->dst_rq->rd->overload != overload)
7499 env->dst_rq->rd->overload = overload;
7500 }
7501
Michael Neuling532cb4c2010-06-08 14:57:02 +10007502}
7503
Michael Neuling532cb4c2010-06-08 14:57:02 +10007504/**
7505 * check_asym_packing - Check to see if the group is packed into the
7506 * sched doman.
7507 *
7508 * This is primarily intended to used at the sibling level. Some
7509 * cores like POWER7 prefer to use lower numbered SMT threads. In the
7510 * case of POWER7, it can move to lower SMT modes only when higher
7511 * threads are idle. When in lower SMT modes, the threads will
7512 * perform better since they share less core resources. Hence when we
7513 * have idle threads, we want them to be the higher ones.
7514 *
7515 * This packing function is run on idle threads. It checks to see if
7516 * the busiest CPU in this domain (core in the P7 case) has a higher
7517 * CPU number than the packing function is being run on. Here we are
7518 * assuming lower CPU number will be equivalent to lower a SMT thread
7519 * number.
7520 *
Yacine Belkadie69f6182013-07-12 20:45:47 +02007521 * Return: 1 when packing is required and a task should be moved to
Michael Neulingb6b12292010-06-10 12:06:21 +10007522 * this CPU. The amount of the imbalance is returned in *imbalance.
7523 *
Randy Dunlapcd968912012-06-08 13:18:33 -07007524 * @env: The load balancing environment.
Michael Neuling532cb4c2010-06-08 14:57:02 +10007525 * @sds: Statistics of the sched_domain which is to be packed
Michael Neuling532cb4c2010-06-08 14:57:02 +10007526 */
Peter Zijlstrabd939f42012-05-02 14:20:37 +02007527static int check_asym_packing(struct lb_env *env, struct sd_lb_stats *sds)
Michael Neuling532cb4c2010-06-08 14:57:02 +10007528{
7529 int busiest_cpu;
7530
Peter Zijlstrabd939f42012-05-02 14:20:37 +02007531 if (!(env->sd->flags & SD_ASYM_PACKING))
Michael Neuling532cb4c2010-06-08 14:57:02 +10007532 return 0;
7533
Srikar Dronamraju1f621e02016-04-06 18:47:40 +05307534 if (env->idle == CPU_NOT_IDLE)
7535 return 0;
7536
Michael Neuling532cb4c2010-06-08 14:57:02 +10007537 if (!sds->busiest)
7538 return 0;
7539
7540 busiest_cpu = group_first_cpu(sds->busiest);
Peter Zijlstrabd939f42012-05-02 14:20:37 +02007541 if (env->dst_cpu > busiest_cpu)
Michael Neuling532cb4c2010-06-08 14:57:02 +10007542 return 0;
7543
Peter Zijlstrabd939f42012-05-02 14:20:37 +02007544 env->imbalance = DIV_ROUND_CLOSEST(
Nicolas Pitre63b2ca32014-05-26 18:19:37 -04007545 sds->busiest_stat.avg_load * sds->busiest_stat.group_capacity,
Nicolas Pitreca8ce3d2014-05-26 18:19:39 -04007546 SCHED_CAPACITY_SCALE);
Peter Zijlstrabd939f42012-05-02 14:20:37 +02007547
Michael Neuling532cb4c2010-06-08 14:57:02 +10007548 return 1;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007549}
7550
7551/**
7552 * fix_small_imbalance - Calculate the minor imbalance that exists
7553 * amongst the groups of a sched_domain, during
7554 * load balancing.
Randy Dunlapcd968912012-06-08 13:18:33 -07007555 * @env: The load balancing environment.
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007556 * @sds: Statistics of the sched_domain whose imbalance is to be calculated.
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007557 */
Peter Zijlstrabd939f42012-05-02 14:20:37 +02007558static inline
7559void fix_small_imbalance(struct lb_env *env, struct sd_lb_stats *sds)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007560{
Nicolas Pitre63b2ca32014-05-26 18:19:37 -04007561 unsigned long tmp, capa_now = 0, capa_move = 0;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007562 unsigned int imbn = 2;
Suresh Siddhadd5feea2010-02-23 16:13:52 -08007563 unsigned long scaled_busy_load_per_task;
Joonsoo Kim56cf5152013-08-06 17:36:43 +09007564 struct sg_lb_stats *local, *busiest;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007565
Joonsoo Kim56cf5152013-08-06 17:36:43 +09007566 local = &sds->local_stat;
7567 busiest = &sds->busiest_stat;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007568
Joonsoo Kim56cf5152013-08-06 17:36:43 +09007569 if (!local->sum_nr_running)
7570 local->load_per_task = cpu_avg_load_per_task(env->dst_cpu);
7571 else if (busiest->load_per_task > local->load_per_task)
7572 imbn = 1;
Suresh Siddhadd5feea2010-02-23 16:13:52 -08007573
Joonsoo Kim56cf5152013-08-06 17:36:43 +09007574 scaled_busy_load_per_task =
Nicolas Pitreca8ce3d2014-05-26 18:19:39 -04007575 (busiest->load_per_task * SCHED_CAPACITY_SCALE) /
Nicolas Pitre63b2ca32014-05-26 18:19:37 -04007576 busiest->group_capacity;
Joonsoo Kim56cf5152013-08-06 17:36:43 +09007577
Vladimir Davydov3029ede2013-09-15 17:49:14 +04007578 if (busiest->avg_load + scaled_busy_load_per_task >=
7579 local->avg_load + (scaled_busy_load_per_task * imbn)) {
Joonsoo Kim56cf5152013-08-06 17:36:43 +09007580 env->imbalance = busiest->load_per_task;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007581 return;
7582 }
7583
7584 /*
7585 * OK, we don't have enough imbalance to justify moving tasks,
Nicolas Pitreced549f2014-05-26 18:19:38 -04007586 * however we may be able to increase total CPU capacity used by
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007587 * moving them.
7588 */
7589
Nicolas Pitre63b2ca32014-05-26 18:19:37 -04007590 capa_now += busiest->group_capacity *
Joonsoo Kim56cf5152013-08-06 17:36:43 +09007591 min(busiest->load_per_task, busiest->avg_load);
Nicolas Pitre63b2ca32014-05-26 18:19:37 -04007592 capa_now += local->group_capacity *
Joonsoo Kim56cf5152013-08-06 17:36:43 +09007593 min(local->load_per_task, local->avg_load);
Nicolas Pitreca8ce3d2014-05-26 18:19:39 -04007594 capa_now /= SCHED_CAPACITY_SCALE;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007595
7596 /* Amount of load we'd subtract */
Vincent Guittota2cd4262014-03-11 17:26:06 +01007597 if (busiest->avg_load > scaled_busy_load_per_task) {
Nicolas Pitre63b2ca32014-05-26 18:19:37 -04007598 capa_move += busiest->group_capacity *
Joonsoo Kim56cf5152013-08-06 17:36:43 +09007599 min(busiest->load_per_task,
Vincent Guittota2cd4262014-03-11 17:26:06 +01007600 busiest->avg_load - scaled_busy_load_per_task);
Joonsoo Kim56cf5152013-08-06 17:36:43 +09007601 }
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007602
7603 /* Amount of load we'd add */
Nicolas Pitre63b2ca32014-05-26 18:19:37 -04007604 if (busiest->avg_load * busiest->group_capacity <
Nicolas Pitreca8ce3d2014-05-26 18:19:39 -04007605 busiest->load_per_task * SCHED_CAPACITY_SCALE) {
Nicolas Pitre63b2ca32014-05-26 18:19:37 -04007606 tmp = (busiest->avg_load * busiest->group_capacity) /
7607 local->group_capacity;
Joonsoo Kim56cf5152013-08-06 17:36:43 +09007608 } else {
Nicolas Pitreca8ce3d2014-05-26 18:19:39 -04007609 tmp = (busiest->load_per_task * SCHED_CAPACITY_SCALE) /
Nicolas Pitre63b2ca32014-05-26 18:19:37 -04007610 local->group_capacity;
Joonsoo Kim56cf5152013-08-06 17:36:43 +09007611 }
Nicolas Pitre63b2ca32014-05-26 18:19:37 -04007612 capa_move += local->group_capacity *
Peter Zijlstra3ae11c92013-08-15 20:37:48 +02007613 min(local->load_per_task, local->avg_load + tmp);
Nicolas Pitreca8ce3d2014-05-26 18:19:39 -04007614 capa_move /= SCHED_CAPACITY_SCALE;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007615
7616 /* Move if we gain throughput */
Nicolas Pitre63b2ca32014-05-26 18:19:37 -04007617 if (capa_move > capa_now)
Joonsoo Kim56cf5152013-08-06 17:36:43 +09007618 env->imbalance = busiest->load_per_task;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007619}
7620
7621/**
7622 * calculate_imbalance - Calculate the amount of imbalance present within the
7623 * groups of a given sched_domain during load balance.
Peter Zijlstrabd939f42012-05-02 14:20:37 +02007624 * @env: load balance environment
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007625 * @sds: statistics of the sched_domain whose imbalance is to be calculated.
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007626 */
Peter Zijlstrabd939f42012-05-02 14:20:37 +02007627static inline void calculate_imbalance(struct lb_env *env, struct sd_lb_stats *sds)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007628{
Suresh Siddhadd5feea2010-02-23 16:13:52 -08007629 unsigned long max_pull, load_above_capacity = ~0UL;
Joonsoo Kim56cf5152013-08-06 17:36:43 +09007630 struct sg_lb_stats *local, *busiest;
Suresh Siddhadd5feea2010-02-23 16:13:52 -08007631
Joonsoo Kim56cf5152013-08-06 17:36:43 +09007632 local = &sds->local_stat;
Joonsoo Kim56cf5152013-08-06 17:36:43 +09007633 busiest = &sds->busiest_stat;
Joonsoo Kim56cf5152013-08-06 17:36:43 +09007634
Rik van Rielcaeb1782014-07-28 14:16:28 -04007635 if (busiest->group_type == group_imbalanced) {
Peter Zijlstra30ce5da2013-08-15 20:29:29 +02007636 /*
7637 * In the group_imb case we cannot rely on group-wide averages
7638 * to ensure cpu-load equilibrium, look at wider averages. XXX
7639 */
Joonsoo Kim56cf5152013-08-06 17:36:43 +09007640 busiest->load_per_task =
7641 min(busiest->load_per_task, sds->avg_load);
Suresh Siddhadd5feea2010-02-23 16:13:52 -08007642 }
7643
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007644 /*
Dietmar Eggemann885e5422016-04-29 20:32:39 +01007645 * Avg load of busiest sg can be less and avg load of local sg can
7646 * be greater than avg load across all sgs of sd because avg load
7647 * factors in sg capacity and sgs with smaller group_type are
7648 * skipped when updating the busiest sg:
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007649 */
Vladimir Davydovb1885552013-09-15 17:49:13 +04007650 if (busiest->avg_load <= sds->avg_load ||
7651 local->avg_load >= sds->avg_load) {
Peter Zijlstrabd939f42012-05-02 14:20:37 +02007652 env->imbalance = 0;
7653 return fix_small_imbalance(env, sds);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007654 }
7655
Peter Zijlstra9a5d9ba2014-07-29 17:15:11 +02007656 /*
7657 * If there aren't any idle cpus, avoid creating some.
7658 */
7659 if (busiest->group_type == group_overloaded &&
7660 local->group_type == group_overloaded) {
Peter Zijlstra1be0eb22016-05-06 12:21:23 +02007661 load_above_capacity = busiest->sum_nr_running * SCHED_CAPACITY_SCALE;
Morten Rasmussencfa10332016-04-29 20:32:40 +01007662 if (load_above_capacity > busiest->group_capacity) {
Vincent Guittotea678212015-02-27 16:54:11 +01007663 load_above_capacity -= busiest->group_capacity;
Dietmar Eggemann26656212016-08-10 11:27:27 +01007664 load_above_capacity *= scale_load_down(NICE_0_LOAD);
Morten Rasmussencfa10332016-04-29 20:32:40 +01007665 load_above_capacity /= busiest->group_capacity;
7666 } else
Vincent Guittotea678212015-02-27 16:54:11 +01007667 load_above_capacity = ~0UL;
Suresh Siddhadd5feea2010-02-23 16:13:52 -08007668 }
7669
7670 /*
7671 * We're trying to get all the cpus to the average_load, so we don't
7672 * want to push ourselves above the average load, nor do we wish to
7673 * reduce the max loaded cpu below the average load. At the same time,
Dietmar Eggemann0a9b23c2016-04-29 20:32:38 +01007674 * we also don't want to reduce the group load below the group
7675 * capacity. Thus we look for the minimum possible imbalance.
Suresh Siddhadd5feea2010-02-23 16:13:52 -08007676 */
Peter Zijlstra30ce5da2013-08-15 20:29:29 +02007677 max_pull = min(busiest->avg_load - sds->avg_load, load_above_capacity);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007678
7679 /* How much load to actually move to equalise the imbalance */
Joonsoo Kim56cf5152013-08-06 17:36:43 +09007680 env->imbalance = min(
Nicolas Pitre63b2ca32014-05-26 18:19:37 -04007681 max_pull * busiest->group_capacity,
7682 (sds->avg_load - local->avg_load) * local->group_capacity
Nicolas Pitreca8ce3d2014-05-26 18:19:39 -04007683 ) / SCHED_CAPACITY_SCALE;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007684
7685 /*
7686 * if *imbalance is less than the average load per runnable task
Lucas De Marchi25985ed2011-03-30 22:57:33 -03007687 * there is no guarantee that any tasks will be moved so we'll have
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007688 * a think about bumping its value to force at least one task to be
7689 * moved
7690 */
Joonsoo Kim56cf5152013-08-06 17:36:43 +09007691 if (env->imbalance < busiest->load_per_task)
Peter Zijlstrabd939f42012-05-02 14:20:37 +02007692 return fix_small_imbalance(env, sds);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007693}
Nikhil Raofab47622010-10-15 13:12:29 -07007694
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007695/******* find_busiest_group() helpers end here *********************/
7696
7697/**
7698 * find_busiest_group - Returns the busiest group within the sched_domain
Dietmar Eggemann0a9b23c2016-04-29 20:32:38 +01007699 * if there is an imbalance.
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007700 *
7701 * Also calculates the amount of weighted load which should be moved
7702 * to restore balance.
7703 *
Randy Dunlapcd968912012-06-08 13:18:33 -07007704 * @env: The load balancing environment.
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007705 *
Yacine Belkadie69f6182013-07-12 20:45:47 +02007706 * Return: - The busiest group if imbalance exists.
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007707 */
Joonsoo Kim56cf5152013-08-06 17:36:43 +09007708static struct sched_group *find_busiest_group(struct lb_env *env)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007709{
Joonsoo Kim56cf5152013-08-06 17:36:43 +09007710 struct sg_lb_stats *local, *busiest;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007711 struct sd_lb_stats sds;
7712
Peter Zijlstra147c5fc2013-08-19 15:22:57 +02007713 init_sd_lb_stats(&sds);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007714
7715 /*
7716 * Compute the various statistics relavent for load balancing at
7717 * this level.
7718 */
Joonsoo Kim23f0d202013-08-06 17:36:42 +09007719 update_sd_lb_stats(env, &sds);
Joonsoo Kim56cf5152013-08-06 17:36:43 +09007720 local = &sds.local_stat;
7721 busiest = &sds.busiest_stat;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007722
Vincent Guittotea678212015-02-27 16:54:11 +01007723 /* ASYM feature bypasses nice load balance check */
Srikar Dronamraju1f621e02016-04-06 18:47:40 +05307724 if (check_asym_packing(env, &sds))
Michael Neuling532cb4c2010-06-08 14:57:02 +10007725 return sds.busiest;
7726
Peter Zijlstracc57aa82011-02-21 18:55:32 +01007727 /* There is no busy sibling group to pull tasks from */
Joonsoo Kim56cf5152013-08-06 17:36:43 +09007728 if (!sds.busiest || busiest->sum_nr_running == 0)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007729 goto out_balanced;
7730
Nicolas Pitreca8ce3d2014-05-26 18:19:39 -04007731 sds.avg_load = (SCHED_CAPACITY_SCALE * sds.total_load)
7732 / sds.total_capacity;
Ken Chenb0432d82011-04-07 17:23:22 -07007733
Peter Zijlstra866ab432011-02-21 18:56:47 +01007734 /*
7735 * If the busiest group is imbalanced the below checks don't
Peter Zijlstra30ce5da2013-08-15 20:29:29 +02007736 * work because they assume all things are equal, which typically
Peter Zijlstra866ab432011-02-21 18:56:47 +01007737 * isn't true due to cpus_allowed constraints and the like.
7738 */
Rik van Rielcaeb1782014-07-28 14:16:28 -04007739 if (busiest->group_type == group_imbalanced)
Peter Zijlstra866ab432011-02-21 18:56:47 +01007740 goto force_balance;
7741
Peter Zijlstracc57aa82011-02-21 18:55:32 +01007742 /* SD_BALANCE_NEWIDLE trumps SMP nice when underutilized */
Vincent Guittotea678212015-02-27 16:54:11 +01007743 if (env->idle == CPU_NEWLY_IDLE && group_has_capacity(env, local) &&
7744 busiest->group_no_capacity)
Nikhil Raofab47622010-10-15 13:12:29 -07007745 goto force_balance;
7746
Peter Zijlstracc57aa82011-02-21 18:55:32 +01007747 /*
Zhihui Zhang9c58c792014-09-20 21:24:36 -04007748 * If the local group is busier than the selected busiest group
Peter Zijlstracc57aa82011-02-21 18:55:32 +01007749 * don't try and pull any tasks.
7750 */
Joonsoo Kim56cf5152013-08-06 17:36:43 +09007751 if (local->avg_load >= busiest->avg_load)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007752 goto out_balanced;
7753
Peter Zijlstracc57aa82011-02-21 18:55:32 +01007754 /*
7755 * Don't pull any tasks if this group is already above the domain
7756 * average load.
7757 */
Joonsoo Kim56cf5152013-08-06 17:36:43 +09007758 if (local->avg_load >= sds.avg_load)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007759 goto out_balanced;
7760
Peter Zijlstrabd939f42012-05-02 14:20:37 +02007761 if (env->idle == CPU_IDLE) {
Suresh Siddhaaae6d3d2010-09-17 15:02:32 -07007762 /*
Vincent Guittot43f4d662014-10-01 15:38:55 +02007763 * This cpu is idle. If the busiest group is not overloaded
7764 * and there is no imbalance between this and busiest group
7765 * wrt idle cpus, it is balanced. The imbalance becomes
7766 * significant if the diff is greater than 1 otherwise we
7767 * might end up to just move the imbalance on another group
Suresh Siddhaaae6d3d2010-09-17 15:02:32 -07007768 */
Vincent Guittot43f4d662014-10-01 15:38:55 +02007769 if ((busiest->group_type != group_overloaded) &&
7770 (local->idle_cpus <= (busiest->idle_cpus + 1)))
Suresh Siddhaaae6d3d2010-09-17 15:02:32 -07007771 goto out_balanced;
Peter Zijlstrac186faf2011-02-21 18:52:53 +01007772 } else {
7773 /*
7774 * In the CPU_NEWLY_IDLE, CPU_NOT_IDLE cases, use
7775 * imbalance_pct to be conservative.
7776 */
Joonsoo Kim56cf5152013-08-06 17:36:43 +09007777 if (100 * busiest->avg_load <=
7778 env->sd->imbalance_pct * local->avg_load)
Peter Zijlstrac186faf2011-02-21 18:52:53 +01007779 goto out_balanced;
Suresh Siddhaaae6d3d2010-09-17 15:02:32 -07007780 }
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007781
Nikhil Raofab47622010-10-15 13:12:29 -07007782force_balance:
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007783 /* Looks like there is an imbalance. Compute it */
Peter Zijlstrabd939f42012-05-02 14:20:37 +02007784 calculate_imbalance(env, &sds);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007785 return sds.busiest;
7786
7787out_balanced:
Peter Zijlstrabd939f42012-05-02 14:20:37 +02007788 env->imbalance = 0;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007789 return NULL;
7790}
7791
7792/*
7793 * find_busiest_queue - find the busiest runqueue among the cpus in group.
7794 */
Peter Zijlstrabd939f42012-05-02 14:20:37 +02007795static struct rq *find_busiest_queue(struct lb_env *env,
Michael Wangb94031302012-07-12 16:10:13 +08007796 struct sched_group *group)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007797{
7798 struct rq *busiest = NULL, *rq;
Nicolas Pitreced549f2014-05-26 18:19:38 -04007799 unsigned long busiest_load = 0, busiest_capacity = 1;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007800 int i;
7801
Peter Zijlstra6906a402013-08-19 15:20:21 +02007802 for_each_cpu_and(i, sched_group_cpus(group), env->cpus) {
Vincent Guittotea678212015-02-27 16:54:11 +01007803 unsigned long capacity, wl;
Peter Zijlstra0ec8aa02013-10-07 11:29:33 +01007804 enum fbq_type rt;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007805
Peter Zijlstra0ec8aa02013-10-07 11:29:33 +01007806 rq = cpu_rq(i);
7807 rt = fbq_classify_rq(rq);
7808
7809 /*
7810 * We classify groups/runqueues into three groups:
7811 * - regular: there are !numa tasks
7812 * - remote: there are numa tasks that run on the 'wrong' node
7813 * - all: there is no distinction
7814 *
7815 * In order to avoid migrating ideally placed numa tasks,
7816 * ignore those when there's better options.
7817 *
7818 * If we ignore the actual busiest queue to migrate another
7819 * task, the next balance pass can still reduce the busiest
7820 * queue by moving tasks around inside the node.
7821 *
7822 * If we cannot move enough load due to this classification
7823 * the next pass will adjust the group classification and
7824 * allow migration of more tasks.
7825 *
7826 * Both cases only affect the total convergence complexity.
7827 */
7828 if (rt > env->fbq_type)
7829 continue;
7830
Nicolas Pitreced549f2014-05-26 18:19:38 -04007831 capacity = capacity_of(i);
Srivatsa Vaddagiri9d5efe02010-06-08 14:57:02 +10007832
Thomas Gleixner6e40f5b2010-02-16 16:48:56 +01007833 wl = weighted_cpuload(i);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007834
Thomas Gleixner6e40f5b2010-02-16 16:48:56 +01007835 /*
7836 * When comparing with imbalance, use weighted_cpuload()
Nicolas Pitreced549f2014-05-26 18:19:38 -04007837 * which is not scaled with the cpu capacity.
Thomas Gleixner6e40f5b2010-02-16 16:48:56 +01007838 */
Vincent Guittotea678212015-02-27 16:54:11 +01007839
7840 if (rq->nr_running == 1 && wl > env->imbalance &&
7841 !check_cpu_capacity(rq, env->sd))
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007842 continue;
7843
Thomas Gleixner6e40f5b2010-02-16 16:48:56 +01007844 /*
7845 * For the load comparisons with the other cpu's, consider
Nicolas Pitreced549f2014-05-26 18:19:38 -04007846 * the weighted_cpuload() scaled with the cpu capacity, so
7847 * that the load can be moved away from the cpu that is
7848 * potentially running at a lower capacity.
Joonsoo Kim95a79b82013-08-06 17:36:41 +09007849 *
Nicolas Pitreced549f2014-05-26 18:19:38 -04007850 * Thus we're looking for max(wl_i / capacity_i), crosswise
Joonsoo Kim95a79b82013-08-06 17:36:41 +09007851 * multiplication to rid ourselves of the division works out
Nicolas Pitreced549f2014-05-26 18:19:38 -04007852 * to: wl_i * capacity_j > wl_j * capacity_i; where j is
7853 * our previous maximum.
Thomas Gleixner6e40f5b2010-02-16 16:48:56 +01007854 */
Nicolas Pitreced549f2014-05-26 18:19:38 -04007855 if (wl * busiest_capacity > busiest_load * capacity) {
Joonsoo Kim95a79b82013-08-06 17:36:41 +09007856 busiest_load = wl;
Nicolas Pitreced549f2014-05-26 18:19:38 -04007857 busiest_capacity = capacity;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007858 busiest = rq;
7859 }
7860 }
7861
7862 return busiest;
7863}
7864
7865/*
7866 * Max backoff if we encounter pinned tasks. Pretty arbitrary value, but
7867 * so long as it is large enough.
7868 */
7869#define MAX_PINNED_INTERVAL 512
7870
Peter Zijlstrabd939f42012-05-02 14:20:37 +02007871static int need_active_balance(struct lb_env *env)
Peter Zijlstra1af3ed32009-12-23 15:10:31 +01007872{
Peter Zijlstrabd939f42012-05-02 14:20:37 +02007873 struct sched_domain *sd = env->sd;
7874
7875 if (env->idle == CPU_NEWLY_IDLE) {
Michael Neuling532cb4c2010-06-08 14:57:02 +10007876
7877 /*
7878 * ASYM_PACKING needs to force migrate tasks from busy but
7879 * higher numbered CPUs in order to pack all tasks in the
7880 * lowest numbered CPUs.
7881 */
Peter Zijlstrabd939f42012-05-02 14:20:37 +02007882 if ((sd->flags & SD_ASYM_PACKING) && env->src_cpu > env->dst_cpu)
Michael Neuling532cb4c2010-06-08 14:57:02 +10007883 return 1;
Peter Zijlstra1af3ed32009-12-23 15:10:31 +01007884 }
7885
Vincent Guittot1aaf90a2015-02-27 16:54:14 +01007886 /*
7887 * The dst_cpu is idle and the src_cpu CPU has only 1 CFS task.
7888 * It's worth migrating the task if the src_cpu's capacity is reduced
7889 * because of other sched_class or IRQs if more capacity stays
7890 * available on dst_cpu.
7891 */
7892 if ((env->idle != CPU_NOT_IDLE) &&
7893 (env->src_rq->cfs.h_nr_running == 1)) {
7894 if ((check_cpu_capacity(env->src_rq, sd)) &&
7895 (capacity_of(env->src_cpu)*sd->imbalance_pct < capacity_of(env->dst_cpu)*100))
7896 return 1;
7897 }
7898
Peter Zijlstra1af3ed32009-12-23 15:10:31 +01007899 return unlikely(sd->nr_balance_failed > sd->cache_nice_tries+2);
7900}
7901
Tejun Heo969c7922010-05-06 18:49:21 +02007902static int active_load_balance_cpu_stop(void *data);
7903
Joonsoo Kim23f0d202013-08-06 17:36:42 +09007904static int should_we_balance(struct lb_env *env)
7905{
7906 struct sched_group *sg = env->sd->groups;
7907 struct cpumask *sg_cpus, *sg_mask;
7908 int cpu, balance_cpu = -1;
7909
7910 /*
7911 * In the newly idle case, we will allow all the cpu's
7912 * to do the newly idle load balance.
7913 */
7914 if (env->idle == CPU_NEWLY_IDLE)
7915 return 1;
7916
7917 sg_cpus = sched_group_cpus(sg);
7918 sg_mask = sched_group_mask(sg);
7919 /* Try to find first idle cpu */
7920 for_each_cpu_and(cpu, sg_cpus, env->cpus) {
7921 if (!cpumask_test_cpu(cpu, sg_mask) || !idle_cpu(cpu))
7922 continue;
7923
7924 balance_cpu = cpu;
7925 break;
7926 }
7927
7928 if (balance_cpu == -1)
7929 balance_cpu = group_balance_cpu(sg);
7930
7931 /*
7932 * First idle cpu or the first cpu(busiest) in this sched group
7933 * is eligible for doing load balancing at this and above domains.
7934 */
Joonsoo Kimb0cff9d2013-09-10 15:54:49 +09007935 return balance_cpu == env->dst_cpu;
Joonsoo Kim23f0d202013-08-06 17:36:42 +09007936}
7937
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007938/*
7939 * Check this_cpu to ensure it is balanced within domain. Attempt to move
7940 * tasks if there is an imbalance.
7941 */
7942static int load_balance(int this_cpu, struct rq *this_rq,
7943 struct sched_domain *sd, enum cpu_idle_type idle,
Joonsoo Kim23f0d202013-08-06 17:36:42 +09007944 int *continue_balancing)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007945{
Srivatsa Vaddagiri88b8dac2012-06-19 17:43:15 +05307946 int ld_moved, cur_ld_moved, active_balance = 0;
Peter Zijlstra62633222013-08-19 12:41:09 +02007947 struct sched_domain *sd_parent = sd->parent;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007948 struct sched_group *group;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007949 struct rq *busiest;
7950 unsigned long flags;
Christoph Lameter4ba29682014-08-26 19:12:21 -05007951 struct cpumask *cpus = this_cpu_cpumask_var_ptr(load_balance_mask);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007952
Peter Zijlstra8e45cb52012-02-22 12:47:19 +01007953 struct lb_env env = {
7954 .sd = sd,
Peter Zijlstraddcdf6e2012-02-22 19:27:40 +01007955 .dst_cpu = this_cpu,
7956 .dst_rq = this_rq,
Srivatsa Vaddagiri88b8dac2012-06-19 17:43:15 +05307957 .dst_grpmask = sched_group_cpus(sd->groups),
Peter Zijlstra8e45cb52012-02-22 12:47:19 +01007958 .idle = idle,
Peter Zijlstraeb953082012-04-17 13:38:40 +02007959 .loop_break = sched_nr_migrate_break,
Michael Wangb94031302012-07-12 16:10:13 +08007960 .cpus = cpus,
Peter Zijlstra0ec8aa02013-10-07 11:29:33 +01007961 .fbq_type = all,
Kirill Tkhai163122b2014-08-20 13:48:29 +04007962 .tasks = LIST_HEAD_INIT(env.tasks),
Peter Zijlstra8e45cb52012-02-22 12:47:19 +01007963 };
7964
Joonsoo Kimcfc03112013-04-23 17:27:39 +09007965 /*
7966 * For NEWLY_IDLE load_balancing, we don't need to consider
7967 * other cpus in our group
7968 */
Joonsoo Kime02e60c2013-04-23 17:27:42 +09007969 if (idle == CPU_NEWLY_IDLE)
Joonsoo Kimcfc03112013-04-23 17:27:39 +09007970 env.dst_grpmask = NULL;
Joonsoo Kimcfc03112013-04-23 17:27:39 +09007971
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007972 cpumask_copy(cpus, cpu_active_mask);
7973
Josh Poimboeufae928822016-06-17 12:43:24 -05007974 schedstat_inc(sd->lb_count[idle]);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007975
7976redo:
Joonsoo Kim23f0d202013-08-06 17:36:42 +09007977 if (!should_we_balance(&env)) {
7978 *continue_balancing = 0;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007979 goto out_balanced;
Joonsoo Kim23f0d202013-08-06 17:36:42 +09007980 }
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007981
Joonsoo Kim23f0d202013-08-06 17:36:42 +09007982 group = find_busiest_group(&env);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007983 if (!group) {
Josh Poimboeufae928822016-06-17 12:43:24 -05007984 schedstat_inc(sd->lb_nobusyg[idle]);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007985 goto out_balanced;
7986 }
7987
Michael Wangb94031302012-07-12 16:10:13 +08007988 busiest = find_busiest_queue(&env, group);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007989 if (!busiest) {
Josh Poimboeufae928822016-06-17 12:43:24 -05007990 schedstat_inc(sd->lb_nobusyq[idle]);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007991 goto out_balanced;
7992 }
7993
Michael Wang78feefc2012-08-06 16:41:59 +08007994 BUG_ON(busiest == env.dst_rq);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007995
Josh Poimboeufae928822016-06-17 12:43:24 -05007996 schedstat_add(sd->lb_imbalance[idle], env.imbalance);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01007997
Vincent Guittot1aaf90a2015-02-27 16:54:14 +01007998 env.src_cpu = busiest->cpu;
7999 env.src_rq = busiest;
8000
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008001 ld_moved = 0;
8002 if (busiest->nr_running > 1) {
8003 /*
8004 * Attempt to move tasks. If find_busiest_group has found
8005 * an imbalance but busiest->nr_running <= 1, the group is
8006 * still unbalanced. ld_moved simply stays zero, so it is
8007 * correctly treated as an imbalance.
8008 */
Peter Zijlstra8e45cb52012-02-22 12:47:19 +01008009 env.flags |= LBF_ALL_PINNED;
Peter Zijlstrac82513e2012-04-26 13:12:27 +02008010 env.loop_max = min(sysctl_sched_nr_migrate, busiest->nr_running);
Peter Zijlstra8e45cb52012-02-22 12:47:19 +01008011
Peter Zijlstra5d6523e2012-03-10 00:07:36 +01008012more_balance:
Kirill Tkhai163122b2014-08-20 13:48:29 +04008013 raw_spin_lock_irqsave(&busiest->lock, flags);
Srivatsa Vaddagiri88b8dac2012-06-19 17:43:15 +05308014
8015 /*
8016 * cur_ld_moved - load moved in current iteration
8017 * ld_moved - cumulative load moved across iterations
8018 */
Kirill Tkhai163122b2014-08-20 13:48:29 +04008019 cur_ld_moved = detach_tasks(&env);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008020
8021 /*
Kirill Tkhai163122b2014-08-20 13:48:29 +04008022 * We've detached some tasks from busiest_rq. Every
8023 * task is masked "TASK_ON_RQ_MIGRATING", so we can safely
8024 * unlock busiest->lock, and we are able to be sure
8025 * that nobody can manipulate the tasks in parallel.
8026 * See task_rq_lock() family for the details.
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008027 */
Kirill Tkhai163122b2014-08-20 13:48:29 +04008028
8029 raw_spin_unlock(&busiest->lock);
8030
8031 if (cur_ld_moved) {
8032 attach_tasks(&env);
8033 ld_moved += cur_ld_moved;
8034 }
8035
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008036 local_irq_restore(flags);
Srivatsa Vaddagiri88b8dac2012-06-19 17:43:15 +05308037
Joonsoo Kimf1cd0852013-04-23 17:27:37 +09008038 if (env.flags & LBF_NEED_BREAK) {
8039 env.flags &= ~LBF_NEED_BREAK;
8040 goto more_balance;
8041 }
8042
Srivatsa Vaddagiri88b8dac2012-06-19 17:43:15 +05308043 /*
8044 * Revisit (affine) tasks on src_cpu that couldn't be moved to
8045 * us and move them to an alternate dst_cpu in our sched_group
8046 * where they can run. The upper limit on how many times we
8047 * iterate on same src_cpu is dependent on number of cpus in our
8048 * sched_group.
8049 *
8050 * This changes load balance semantics a bit on who can move
8051 * load to a given_cpu. In addition to the given_cpu itself
8052 * (or a ilb_cpu acting on its behalf where given_cpu is
8053 * nohz-idle), we now have balance_cpu in a position to move
8054 * load to given_cpu. In rare situations, this may cause
8055 * conflicts (balance_cpu and given_cpu/ilb_cpu deciding
8056 * _independently_ and at _same_ time to move some load to
8057 * given_cpu) causing exceess load to be moved to given_cpu.
8058 * This however should not happen so much in practice and
8059 * moreover subsequent load balance cycles should correct the
8060 * excess load moved.
8061 */
Peter Zijlstra62633222013-08-19 12:41:09 +02008062 if ((env.flags & LBF_DST_PINNED) && env.imbalance > 0) {
Srivatsa Vaddagiri88b8dac2012-06-19 17:43:15 +05308063
Vladimir Davydov7aff2e32013-09-15 21:30:13 +04008064 /* Prevent to re-select dst_cpu via env's cpus */
8065 cpumask_clear_cpu(env.dst_cpu, env.cpus);
8066
Michael Wang78feefc2012-08-06 16:41:59 +08008067 env.dst_rq = cpu_rq(env.new_dst_cpu);
Srivatsa Vaddagiri88b8dac2012-06-19 17:43:15 +05308068 env.dst_cpu = env.new_dst_cpu;
Peter Zijlstra62633222013-08-19 12:41:09 +02008069 env.flags &= ~LBF_DST_PINNED;
Srivatsa Vaddagiri88b8dac2012-06-19 17:43:15 +05308070 env.loop = 0;
8071 env.loop_break = sched_nr_migrate_break;
Joonsoo Kime02e60c2013-04-23 17:27:42 +09008072
Srivatsa Vaddagiri88b8dac2012-06-19 17:43:15 +05308073 /*
8074 * Go back to "more_balance" rather than "redo" since we
8075 * need to continue with same src_cpu.
8076 */
8077 goto more_balance;
8078 }
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008079
Peter Zijlstra62633222013-08-19 12:41:09 +02008080 /*
8081 * We failed to reach balance because of affinity.
8082 */
8083 if (sd_parent) {
Nicolas Pitre63b2ca32014-05-26 18:19:37 -04008084 int *group_imbalance = &sd_parent->groups->sgc->imbalance;
Peter Zijlstra62633222013-08-19 12:41:09 +02008085
Vincent Guittotafdeee02014-08-26 13:06:44 +02008086 if ((env.flags & LBF_SOME_PINNED) && env.imbalance > 0)
Peter Zijlstra62633222013-08-19 12:41:09 +02008087 *group_imbalance = 1;
Peter Zijlstra62633222013-08-19 12:41:09 +02008088 }
8089
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008090 /* All tasks on this runqueue were pinned by CPU affinity */
Peter Zijlstra8e45cb52012-02-22 12:47:19 +01008091 if (unlikely(env.flags & LBF_ALL_PINNED)) {
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008092 cpumask_clear_cpu(cpu_of(busiest), cpus);
Prashanth Nageshappabbf18b12012-06-19 17:52:07 +05308093 if (!cpumask_empty(cpus)) {
8094 env.loop = 0;
8095 env.loop_break = sched_nr_migrate_break;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008096 goto redo;
Prashanth Nageshappabbf18b12012-06-19 17:52:07 +05308097 }
Vincent Guittotafdeee02014-08-26 13:06:44 +02008098 goto out_all_pinned;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008099 }
8100 }
8101
8102 if (!ld_moved) {
Josh Poimboeufae928822016-06-17 12:43:24 -05008103 schedstat_inc(sd->lb_failed[idle]);
Venkatesh Pallipadi58b26c42010-09-10 18:19:17 -07008104 /*
8105 * Increment the failure counter only on periodic balance.
8106 * We do not want newidle balance, which can be very
8107 * frequent, pollute the failure counter causing
8108 * excessive cache_hot migrations and active balances.
8109 */
8110 if (idle != CPU_NEWLY_IDLE)
8111 sd->nr_balance_failed++;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008112
Peter Zijlstrabd939f42012-05-02 14:20:37 +02008113 if (need_active_balance(&env)) {
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008114 raw_spin_lock_irqsave(&busiest->lock, flags);
8115
Tejun Heo969c7922010-05-06 18:49:21 +02008116 /* don't kick the active_load_balance_cpu_stop,
8117 * if the curr task on busiest cpu can't be
8118 * moved to this_cpu
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008119 */
8120 if (!cpumask_test_cpu(this_cpu,
Peter Zijlstrafa17b502011-06-16 12:23:22 +02008121 tsk_cpus_allowed(busiest->curr))) {
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008122 raw_spin_unlock_irqrestore(&busiest->lock,
8123 flags);
Peter Zijlstra8e45cb52012-02-22 12:47:19 +01008124 env.flags |= LBF_ALL_PINNED;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008125 goto out_one_pinned;
8126 }
8127
Tejun Heo969c7922010-05-06 18:49:21 +02008128 /*
8129 * ->active_balance synchronizes accesses to
8130 * ->active_balance_work. Once set, it's cleared
8131 * only after active load balance is finished.
8132 */
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008133 if (!busiest->active_balance) {
8134 busiest->active_balance = 1;
8135 busiest->push_cpu = this_cpu;
8136 active_balance = 1;
8137 }
8138 raw_spin_unlock_irqrestore(&busiest->lock, flags);
Tejun Heo969c7922010-05-06 18:49:21 +02008139
Peter Zijlstrabd939f42012-05-02 14:20:37 +02008140 if (active_balance) {
Tejun Heo969c7922010-05-06 18:49:21 +02008141 stop_one_cpu_nowait(cpu_of(busiest),
8142 active_load_balance_cpu_stop, busiest,
8143 &busiest->active_balance_work);
Peter Zijlstrabd939f42012-05-02 14:20:37 +02008144 }
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008145
Srikar Dronamrajud02c071182016-03-23 17:54:44 +05308146 /* We've kicked active balancing, force task migration. */
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008147 sd->nr_balance_failed = sd->cache_nice_tries+1;
8148 }
8149 } else
8150 sd->nr_balance_failed = 0;
8151
8152 if (likely(!active_balance)) {
8153 /* We were unbalanced, so reset the balancing interval */
8154 sd->balance_interval = sd->min_interval;
8155 } else {
8156 /*
8157 * If we've begun active balancing, start to back off. This
8158 * case may not be covered by the all_pinned logic if there
8159 * is only 1 task on the busy runqueue (because we don't call
Kirill Tkhai163122b2014-08-20 13:48:29 +04008160 * detach_tasks).
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008161 */
8162 if (sd->balance_interval < sd->max_interval)
8163 sd->balance_interval *= 2;
8164 }
8165
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008166 goto out;
8167
8168out_balanced:
Vincent Guittotafdeee02014-08-26 13:06:44 +02008169 /*
8170 * We reach balance although we may have faced some affinity
8171 * constraints. Clear the imbalance flag if it was set.
8172 */
8173 if (sd_parent) {
8174 int *group_imbalance = &sd_parent->groups->sgc->imbalance;
8175
8176 if (*group_imbalance)
8177 *group_imbalance = 0;
8178 }
8179
8180out_all_pinned:
8181 /*
8182 * We reach balance because all tasks are pinned at this level so
8183 * we can't migrate them. Let the imbalance flag set so parent level
8184 * can try to migrate them.
8185 */
Josh Poimboeufae928822016-06-17 12:43:24 -05008186 schedstat_inc(sd->lb_balanced[idle]);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008187
8188 sd->nr_balance_failed = 0;
8189
8190out_one_pinned:
8191 /* tune up the balancing interval */
Peter Zijlstra8e45cb52012-02-22 12:47:19 +01008192 if (((env.flags & LBF_ALL_PINNED) &&
Peter Zijlstra5b54b562011-09-22 15:23:13 +02008193 sd->balance_interval < MAX_PINNED_INTERVAL) ||
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008194 (sd->balance_interval < sd->max_interval))
8195 sd->balance_interval *= 2;
8196
Venkatesh Pallipadi46e49b32011-02-14 14:38:50 -08008197 ld_moved = 0;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008198out:
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008199 return ld_moved;
8200}
8201
Jason Low52a08ef2014-05-08 17:49:22 -07008202static inline unsigned long
8203get_sd_balance_interval(struct sched_domain *sd, int cpu_busy)
8204{
8205 unsigned long interval = sd->balance_interval;
8206
8207 if (cpu_busy)
8208 interval *= sd->busy_factor;
8209
8210 /* scale ms to jiffies */
8211 interval = msecs_to_jiffies(interval);
8212 interval = clamp(interval, 1UL, max_load_balance_interval);
8213
8214 return interval;
8215}
8216
8217static inline void
Leo Yan31851a92016-08-05 14:31:29 +08008218update_next_balance(struct sched_domain *sd, unsigned long *next_balance)
Jason Low52a08ef2014-05-08 17:49:22 -07008219{
8220 unsigned long interval, next;
8221
Leo Yan31851a92016-08-05 14:31:29 +08008222 /* used by idle balance, so cpu_busy = 0 */
8223 interval = get_sd_balance_interval(sd, 0);
Jason Low52a08ef2014-05-08 17:49:22 -07008224 next = sd->last_balance + interval;
8225
8226 if (time_after(*next_balance, next))
8227 *next_balance = next;
8228}
8229
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008230/*
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008231 * idle_balance is called by schedule() if this_cpu is about to become
8232 * idle. Attempts to pull tasks from other CPUs.
8233 */
Peter Zijlstra6e831252014-02-11 16:11:48 +01008234static int idle_balance(struct rq *this_rq)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008235{
Jason Low52a08ef2014-05-08 17:49:22 -07008236 unsigned long next_balance = jiffies + HZ;
8237 int this_cpu = this_rq->cpu;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008238 struct sched_domain *sd;
8239 int pulled_task = 0;
Jason Low9bd721c2013-09-13 11:26:52 -07008240 u64 curr_cost = 0;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008241
Peter Zijlstra6e831252014-02-11 16:11:48 +01008242 /*
8243 * We must set idle_stamp _before_ calling idle_balance(), such that we
8244 * measure the duration of idle_balance() as idle time.
8245 */
8246 this_rq->idle_stamp = rq_clock(this_rq);
8247
Tim Chen4486edd2014-06-23 12:16:49 -07008248 if (this_rq->avg_idle < sysctl_sched_migration_cost ||
8249 !this_rq->rd->overload) {
Jason Low52a08ef2014-05-08 17:49:22 -07008250 rcu_read_lock();
8251 sd = rcu_dereference_check_sched_domain(this_rq->sd);
8252 if (sd)
Leo Yan31851a92016-08-05 14:31:29 +08008253 update_next_balance(sd, &next_balance);
Jason Low52a08ef2014-05-08 17:49:22 -07008254 rcu_read_unlock();
8255
Peter Zijlstra6e831252014-02-11 16:11:48 +01008256 goto out;
Jason Low52a08ef2014-05-08 17:49:22 -07008257 }
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008258
Peter Zijlstraf492e122009-12-23 15:29:42 +01008259 raw_spin_unlock(&this_rq->lock);
8260
Paul Turner48a16752012-10-04 13:18:31 +02008261 update_blocked_averages(this_cpu);
Peter Zijlstradce840a2011-04-07 14:09:50 +02008262 rcu_read_lock();
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008263 for_each_domain(this_cpu, sd) {
Joonsoo Kim23f0d202013-08-06 17:36:42 +09008264 int continue_balancing = 1;
Jason Low9bd721c2013-09-13 11:26:52 -07008265 u64 t0, domain_cost;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008266
8267 if (!(sd->flags & SD_LOAD_BALANCE))
8268 continue;
8269
Jason Low52a08ef2014-05-08 17:49:22 -07008270 if (this_rq->avg_idle < curr_cost + sd->max_newidle_lb_cost) {
Leo Yan31851a92016-08-05 14:31:29 +08008271 update_next_balance(sd, &next_balance);
Jason Low9bd721c2013-09-13 11:26:52 -07008272 break;
Jason Low52a08ef2014-05-08 17:49:22 -07008273 }
Jason Low9bd721c2013-09-13 11:26:52 -07008274
Peter Zijlstraf492e122009-12-23 15:29:42 +01008275 if (sd->flags & SD_BALANCE_NEWIDLE) {
Jason Low9bd721c2013-09-13 11:26:52 -07008276 t0 = sched_clock_cpu(this_cpu);
8277
Peter Zijlstraf492e122009-12-23 15:29:42 +01008278 pulled_task = load_balance(this_cpu, this_rq,
Joonsoo Kim23f0d202013-08-06 17:36:42 +09008279 sd, CPU_NEWLY_IDLE,
8280 &continue_balancing);
Jason Low9bd721c2013-09-13 11:26:52 -07008281
8282 domain_cost = sched_clock_cpu(this_cpu) - t0;
8283 if (domain_cost > sd->max_newidle_lb_cost)
8284 sd->max_newidle_lb_cost = domain_cost;
8285
8286 curr_cost += domain_cost;
Peter Zijlstraf492e122009-12-23 15:29:42 +01008287 }
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008288
Leo Yan31851a92016-08-05 14:31:29 +08008289 update_next_balance(sd, &next_balance);
Jason Low39a4d9c2014-04-23 18:30:35 -07008290
8291 /*
8292 * Stop searching for tasks to pull if there are
8293 * now runnable tasks on this rq.
8294 */
8295 if (pulled_task || this_rq->nr_running > 0)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008296 break;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008297 }
Peter Zijlstradce840a2011-04-07 14:09:50 +02008298 rcu_read_unlock();
Peter Zijlstraf492e122009-12-23 15:29:42 +01008299
8300 raw_spin_lock(&this_rq->lock);
8301
Jason Low0e5b5332014-04-28 15:45:54 -07008302 if (curr_cost > this_rq->max_idle_balance_cost)
8303 this_rq->max_idle_balance_cost = curr_cost;
8304
Daniel Lezcanoe5fc6612014-01-17 10:04:02 +01008305 /*
Jason Low0e5b5332014-04-28 15:45:54 -07008306 * While browsing the domains, we released the rq lock, a task could
8307 * have been enqueued in the meantime. Since we're not going idle,
8308 * pretend we pulled a task.
Daniel Lezcanoe5fc6612014-01-17 10:04:02 +01008309 */
Jason Low0e5b5332014-04-28 15:45:54 -07008310 if (this_rq->cfs.h_nr_running && !pulled_task)
Peter Zijlstra6e831252014-02-11 16:11:48 +01008311 pulled_task = 1;
Daniel Lezcanoe5fc6612014-01-17 10:04:02 +01008312
Peter Zijlstra6e831252014-02-11 16:11:48 +01008313out:
Jason Low52a08ef2014-05-08 17:49:22 -07008314 /* Move the next balance forward */
8315 if (time_after(this_rq->next_balance, next_balance))
8316 this_rq->next_balance = next_balance;
8317
Kirill Tkhaie4aa3582014-03-06 13:31:55 +04008318 /* Is there a task of a high priority class? */
Kirill Tkhai46383642014-03-15 02:15:07 +04008319 if (this_rq->nr_running != this_rq->cfs.h_nr_running)
Kirill Tkhaie4aa3582014-03-06 13:31:55 +04008320 pulled_task = -1;
8321
Dietmar Eggemann38c6ade2015-10-20 13:04:41 +01008322 if (pulled_task)
Peter Zijlstra6e831252014-02-11 16:11:48 +01008323 this_rq->idle_stamp = 0;
8324
Daniel Lezcano3c4017c2014-01-17 10:04:03 +01008325 return pulled_task;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008326}
8327
8328/*
Tejun Heo969c7922010-05-06 18:49:21 +02008329 * active_load_balance_cpu_stop is run by cpu stopper. It pushes
8330 * running tasks off the busiest CPU onto idle CPUs. It requires at
8331 * least 1 task to be running on each physical CPU where possible, and
8332 * avoids physical / logical imbalances.
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008333 */
Tejun Heo969c7922010-05-06 18:49:21 +02008334static int active_load_balance_cpu_stop(void *data)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008335{
Tejun Heo969c7922010-05-06 18:49:21 +02008336 struct rq *busiest_rq = data;
8337 int busiest_cpu = cpu_of(busiest_rq);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008338 int target_cpu = busiest_rq->push_cpu;
Tejun Heo969c7922010-05-06 18:49:21 +02008339 struct rq *target_rq = cpu_rq(target_cpu);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008340 struct sched_domain *sd;
Kirill Tkhaie5673f22014-08-20 13:48:01 +04008341 struct task_struct *p = NULL;
Tejun Heo969c7922010-05-06 18:49:21 +02008342
8343 raw_spin_lock_irq(&busiest_rq->lock);
8344
8345 /* make sure the requested cpu hasn't gone down in the meantime */
8346 if (unlikely(busiest_cpu != smp_processor_id() ||
8347 !busiest_rq->active_balance))
8348 goto out_unlock;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008349
8350 /* Is there any task to move? */
8351 if (busiest_rq->nr_running <= 1)
Tejun Heo969c7922010-05-06 18:49:21 +02008352 goto out_unlock;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008353
8354 /*
8355 * This condition is "impossible", if it occurs
8356 * we need to fix it. Originally reported by
8357 * Bjorn Helgaas on a 128-cpu setup.
8358 */
8359 BUG_ON(busiest_rq == target_rq);
8360
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008361 /* Search for an sd spanning us and the target CPU. */
Peter Zijlstradce840a2011-04-07 14:09:50 +02008362 rcu_read_lock();
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008363 for_each_domain(target_cpu, sd) {
8364 if ((sd->flags & SD_LOAD_BALANCE) &&
8365 cpumask_test_cpu(busiest_cpu, sched_domain_span(sd)))
8366 break;
8367 }
8368
8369 if (likely(sd)) {
Peter Zijlstra8e45cb52012-02-22 12:47:19 +01008370 struct lb_env env = {
8371 .sd = sd,
Peter Zijlstraddcdf6e2012-02-22 19:27:40 +01008372 .dst_cpu = target_cpu,
8373 .dst_rq = target_rq,
8374 .src_cpu = busiest_rq->cpu,
8375 .src_rq = busiest_rq,
Peter Zijlstra8e45cb52012-02-22 12:47:19 +01008376 .idle = CPU_IDLE,
8377 };
8378
Josh Poimboeufae928822016-06-17 12:43:24 -05008379 schedstat_inc(sd->alb_count);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008380
Kirill Tkhaie5673f22014-08-20 13:48:01 +04008381 p = detach_one_task(&env);
Srikar Dronamrajud02c071182016-03-23 17:54:44 +05308382 if (p) {
Josh Poimboeufae928822016-06-17 12:43:24 -05008383 schedstat_inc(sd->alb_pushed);
Srikar Dronamrajud02c071182016-03-23 17:54:44 +05308384 /* Active balancing done, reset the failure counter. */
8385 sd->nr_balance_failed = 0;
8386 } else {
Josh Poimboeufae928822016-06-17 12:43:24 -05008387 schedstat_inc(sd->alb_failed);
Srikar Dronamrajud02c071182016-03-23 17:54:44 +05308388 }
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008389 }
Peter Zijlstradce840a2011-04-07 14:09:50 +02008390 rcu_read_unlock();
Tejun Heo969c7922010-05-06 18:49:21 +02008391out_unlock:
8392 busiest_rq->active_balance = 0;
Kirill Tkhaie5673f22014-08-20 13:48:01 +04008393 raw_spin_unlock(&busiest_rq->lock);
8394
8395 if (p)
8396 attach_one_task(target_rq, p);
8397
8398 local_irq_enable();
8399
Tejun Heo969c7922010-05-06 18:49:21 +02008400 return 0;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008401}
8402
Mike Galbraithd987fc72011-12-05 10:01:47 +01008403static inline int on_null_domain(struct rq *rq)
8404{
8405 return unlikely(!rcu_dereference_sched(rq->sd));
8406}
8407
Frederic Weisbecker3451d022011-08-10 23:21:01 +02008408#ifdef CONFIG_NO_HZ_COMMON
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07008409/*
8410 * idle load balancing details
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07008411 * - When one of the busy CPUs notice that there may be an idle rebalancing
8412 * needed, they will kick the idle load balancer, which then does idle
8413 * load balancing for all the idle CPUs.
8414 */
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008415static struct {
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07008416 cpumask_var_t idle_cpus_mask;
Suresh Siddha0b005cf2011-12-01 17:07:34 -08008417 atomic_t nr_cpus;
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07008418 unsigned long next_balance; /* in jiffy units */
8419} nohz ____cacheline_aligned;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008420
Daniel Lezcano3dd03372014-01-06 12:34:41 +01008421static inline int find_new_ilb(void)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008422{
Suresh Siddha0b005cf2011-12-01 17:07:34 -08008423 int ilb = cpumask_first(nohz.idle_cpus_mask);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008424
Suresh Siddha786d6dc2011-12-01 17:07:35 -08008425 if (ilb < nr_cpu_ids && idle_cpu(ilb))
8426 return ilb;
8427
8428 return nr_cpu_ids;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008429}
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008430
8431/*
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07008432 * Kick a CPU to do the nohz balancing, if it is time for it. We pick the
8433 * nohz_load_balancer CPU (if there is one) otherwise fallback to any idle
8434 * CPU (if there is one).
8435 */
Daniel Lezcano0aeeeeb2014-01-06 12:34:42 +01008436static void nohz_balancer_kick(void)
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07008437{
8438 int ilb_cpu;
8439
8440 nohz.next_balance++;
8441
Daniel Lezcano3dd03372014-01-06 12:34:41 +01008442 ilb_cpu = find_new_ilb();
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07008443
Suresh Siddha0b005cf2011-12-01 17:07:34 -08008444 if (ilb_cpu >= nr_cpu_ids)
8445 return;
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07008446
Suresh Siddhacd490c52011-12-06 11:26:34 -08008447 if (test_and_set_bit(NOHZ_BALANCE_KICK, nohz_flags(ilb_cpu)))
Suresh Siddha1c792db2011-12-01 17:07:32 -08008448 return;
8449 /*
8450 * Use smp_send_reschedule() instead of resched_cpu().
8451 * This way we generate a sched IPI on the target cpu which
8452 * is idle. And the softirq performing nohz idle load balance
8453 * will be run before returning from the IPI.
8454 */
8455 smp_send_reschedule(ilb_cpu);
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07008456 return;
8457}
8458
Thomas Gleixner20a5c8c2016-03-10 12:54:20 +01008459void nohz_balance_exit_idle(unsigned int cpu)
Suresh Siddha71325962012-01-19 18:28:57 -08008460{
8461 if (unlikely(test_bit(NOHZ_TICK_STOPPED, nohz_flags(cpu)))) {
Mike Galbraithd987fc72011-12-05 10:01:47 +01008462 /*
8463 * Completely isolated CPUs don't ever set, so we must test.
8464 */
8465 if (likely(cpumask_test_cpu(cpu, nohz.idle_cpus_mask))) {
8466 cpumask_clear_cpu(cpu, nohz.idle_cpus_mask);
8467 atomic_dec(&nohz.nr_cpus);
8468 }
Suresh Siddha71325962012-01-19 18:28:57 -08008469 clear_bit(NOHZ_TICK_STOPPED, nohz_flags(cpu));
8470 }
8471}
8472
Suresh Siddha69e1e812011-12-01 17:07:33 -08008473static inline void set_cpu_sd_state_busy(void)
8474{
8475 struct sched_domain *sd;
Preeti U Murthy37dc6b52013-10-30 08:42:52 +05308476 int cpu = smp_processor_id();
Suresh Siddha69e1e812011-12-01 17:07:33 -08008477
Suresh Siddha69e1e812011-12-01 17:07:33 -08008478 rcu_read_lock();
Peter Zijlstra0e369d72016-05-09 10:38:01 +02008479 sd = rcu_dereference(per_cpu(sd_llc, cpu));
Vincent Guittot25f55d92013-04-23 16:59:02 +02008480
8481 if (!sd || !sd->nohz_idle)
8482 goto unlock;
8483 sd->nohz_idle = 0;
8484
Peter Zijlstra0e369d72016-05-09 10:38:01 +02008485 atomic_inc(&sd->shared->nr_busy_cpus);
Vincent Guittot25f55d92013-04-23 16:59:02 +02008486unlock:
Suresh Siddha69e1e812011-12-01 17:07:33 -08008487 rcu_read_unlock();
8488}
8489
8490void set_cpu_sd_state_idle(void)
8491{
8492 struct sched_domain *sd;
Preeti U Murthy37dc6b52013-10-30 08:42:52 +05308493 int cpu = smp_processor_id();
Suresh Siddha69e1e812011-12-01 17:07:33 -08008494
Suresh Siddha69e1e812011-12-01 17:07:33 -08008495 rcu_read_lock();
Peter Zijlstra0e369d72016-05-09 10:38:01 +02008496 sd = rcu_dereference(per_cpu(sd_llc, cpu));
Vincent Guittot25f55d92013-04-23 16:59:02 +02008497
8498 if (!sd || sd->nohz_idle)
8499 goto unlock;
8500 sd->nohz_idle = 1;
8501
Peter Zijlstra0e369d72016-05-09 10:38:01 +02008502 atomic_dec(&sd->shared->nr_busy_cpus);
Vincent Guittot25f55d92013-04-23 16:59:02 +02008503unlock:
Suresh Siddha69e1e812011-12-01 17:07:33 -08008504 rcu_read_unlock();
8505}
8506
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07008507/*
Alex Shic1cc0172012-09-10 15:10:58 +08008508 * This routine will record that the cpu is going idle with tick stopped.
Suresh Siddha0b005cf2011-12-01 17:07:34 -08008509 * This info will be used in performing idle load balancing in the future.
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008510 */
Alex Shic1cc0172012-09-10 15:10:58 +08008511void nohz_balance_enter_idle(int cpu)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008512{
Suresh Siddha71325962012-01-19 18:28:57 -08008513 /*
8514 * If this cpu is going down, then nothing needs to be done.
8515 */
8516 if (!cpu_active(cpu))
8517 return;
8518
Alex Shic1cc0172012-09-10 15:10:58 +08008519 if (test_bit(NOHZ_TICK_STOPPED, nohz_flags(cpu)))
8520 return;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008521
Mike Galbraithd987fc72011-12-05 10:01:47 +01008522 /*
8523 * If we're a completely isolated CPU, we don't play.
8524 */
8525 if (on_null_domain(cpu_rq(cpu)))
8526 return;
8527
Alex Shic1cc0172012-09-10 15:10:58 +08008528 cpumask_set_cpu(cpu, nohz.idle_cpus_mask);
8529 atomic_inc(&nohz.nr_cpus);
8530 set_bit(NOHZ_TICK_STOPPED, nohz_flags(cpu));
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008531}
8532#endif
8533
8534static DEFINE_SPINLOCK(balancing);
8535
Peter Zijlstra49c022e2011-04-05 10:14:25 +02008536/*
8537 * Scale the max load_balance interval with the number of CPUs in the system.
8538 * This trades load-balance latency on larger machines for less cross talk.
8539 */
Peter Zijlstra029632f2011-10-25 10:00:11 +02008540void update_max_interval(void)
Peter Zijlstra49c022e2011-04-05 10:14:25 +02008541{
8542 max_load_balance_interval = HZ*num_online_cpus()/10;
8543}
8544
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008545/*
8546 * It checks each scheduling domain to see if it is due to be balanced,
8547 * and initiates a balancing operation if so.
8548 *
Libinb9b08532013-04-01 19:14:01 +08008549 * Balancing parameters are set up in init_sched_domains.
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008550 */
Daniel Lezcanof7ed0a82014-01-06 12:34:43 +01008551static void rebalance_domains(struct rq *rq, enum cpu_idle_type idle)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008552{
Joonsoo Kim23f0d202013-08-06 17:36:42 +09008553 int continue_balancing = 1;
Daniel Lezcanof7ed0a82014-01-06 12:34:43 +01008554 int cpu = rq->cpu;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008555 unsigned long interval;
Peter Zijlstra04f733b2012-05-11 00:12:02 +02008556 struct sched_domain *sd;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008557 /* Earliest time when we have to do rebalance again */
8558 unsigned long next_balance = jiffies + 60*HZ;
8559 int update_next_balance = 0;
Jason Lowf48627e2013-09-13 11:26:53 -07008560 int need_serialize, need_decay = 0;
8561 u64 max_cost = 0;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008562
Paul Turner48a16752012-10-04 13:18:31 +02008563 update_blocked_averages(cpu);
Peter Zijlstra2069dd72010-11-15 15:47:00 -08008564
Peter Zijlstradce840a2011-04-07 14:09:50 +02008565 rcu_read_lock();
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008566 for_each_domain(cpu, sd) {
Jason Lowf48627e2013-09-13 11:26:53 -07008567 /*
8568 * Decay the newidle max times here because this is a regular
8569 * visit to all the domains. Decay ~1% per second.
8570 */
8571 if (time_after(jiffies, sd->next_decay_max_lb_cost)) {
8572 sd->max_newidle_lb_cost =
8573 (sd->max_newidle_lb_cost * 253) / 256;
8574 sd->next_decay_max_lb_cost = jiffies + HZ;
8575 need_decay = 1;
8576 }
8577 max_cost += sd->max_newidle_lb_cost;
8578
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008579 if (!(sd->flags & SD_LOAD_BALANCE))
8580 continue;
8581
Jason Lowf48627e2013-09-13 11:26:53 -07008582 /*
8583 * Stop the load balance at this level. There is another
8584 * CPU in our sched group which is doing load balancing more
8585 * actively.
8586 */
8587 if (!continue_balancing) {
8588 if (need_decay)
8589 continue;
8590 break;
8591 }
8592
Jason Low52a08ef2014-05-08 17:49:22 -07008593 interval = get_sd_balance_interval(sd, idle != CPU_IDLE);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008594
8595 need_serialize = sd->flags & SD_SERIALIZE;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008596 if (need_serialize) {
8597 if (!spin_trylock(&balancing))
8598 goto out;
8599 }
8600
8601 if (time_after_eq(jiffies, sd->last_balance + interval)) {
Joonsoo Kim23f0d202013-08-06 17:36:42 +09008602 if (load_balance(cpu, rq, sd, idle, &continue_balancing)) {
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008603 /*
Peter Zijlstra62633222013-08-19 12:41:09 +02008604 * The LBF_DST_PINNED logic could have changed
Joonsoo Kimde5eb2d2013-04-23 17:27:38 +09008605 * env->dst_cpu, so we can't know our idle
8606 * state even if we migrated tasks. Update it.
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008607 */
Joonsoo Kimde5eb2d2013-04-23 17:27:38 +09008608 idle = idle_cpu(cpu) ? CPU_IDLE : CPU_NOT_IDLE;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008609 }
8610 sd->last_balance = jiffies;
Jason Low52a08ef2014-05-08 17:49:22 -07008611 interval = get_sd_balance_interval(sd, idle != CPU_IDLE);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008612 }
8613 if (need_serialize)
8614 spin_unlock(&balancing);
8615out:
8616 if (time_after(next_balance, sd->last_balance + interval)) {
8617 next_balance = sd->last_balance + interval;
8618 update_next_balance = 1;
8619 }
Jason Lowf48627e2013-09-13 11:26:53 -07008620 }
8621 if (need_decay) {
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008622 /*
Jason Lowf48627e2013-09-13 11:26:53 -07008623 * Ensure the rq-wide value also decays but keep it at a
8624 * reasonable floor to avoid funnies with rq->avg_idle.
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008625 */
Jason Lowf48627e2013-09-13 11:26:53 -07008626 rq->max_idle_balance_cost =
8627 max((u64)sysctl_sched_migration_cost, max_cost);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008628 }
Peter Zijlstradce840a2011-04-07 14:09:50 +02008629 rcu_read_unlock();
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008630
8631 /*
8632 * next_balance will be updated only when there is a need.
8633 * When the cpu is attached to null domain for ex, it will not be
8634 * updated.
8635 */
Vincent Guittotc5afb6a2015-08-03 11:55:50 +02008636 if (likely(update_next_balance)) {
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008637 rq->next_balance = next_balance;
Vincent Guittotc5afb6a2015-08-03 11:55:50 +02008638
8639#ifdef CONFIG_NO_HZ_COMMON
8640 /*
8641 * If this CPU has been elected to perform the nohz idle
8642 * balance. Other idle CPUs have already rebalanced with
8643 * nohz_idle_balance() and nohz.next_balance has been
8644 * updated accordingly. This CPU is now running the idle load
8645 * balance for itself and we need to update the
8646 * nohz.next_balance accordingly.
8647 */
8648 if ((idle == CPU_IDLE) && time_after(nohz.next_balance, rq->next_balance))
8649 nohz.next_balance = rq->next_balance;
8650#endif
8651 }
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008652}
8653
Frederic Weisbecker3451d022011-08-10 23:21:01 +02008654#ifdef CONFIG_NO_HZ_COMMON
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07008655/*
Frederic Weisbecker3451d022011-08-10 23:21:01 +02008656 * In CONFIG_NO_HZ_COMMON case, the idle balance kickee will do the
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07008657 * rebalancing for all the cpus for whom scheduler ticks are stopped.
8658 */
Daniel Lezcano208cb162014-01-06 12:34:44 +01008659static void nohz_idle_balance(struct rq *this_rq, enum cpu_idle_type idle)
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07008660{
Daniel Lezcano208cb162014-01-06 12:34:44 +01008661 int this_cpu = this_rq->cpu;
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07008662 struct rq *rq;
8663 int balance_cpu;
Vincent Guittotc5afb6a2015-08-03 11:55:50 +02008664 /* Earliest time when we have to do rebalance again */
8665 unsigned long next_balance = jiffies + 60*HZ;
8666 int update_next_balance = 0;
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07008667
Suresh Siddha1c792db2011-12-01 17:07:32 -08008668 if (idle != CPU_IDLE ||
8669 !test_bit(NOHZ_BALANCE_KICK, nohz_flags(this_cpu)))
8670 goto end;
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07008671
8672 for_each_cpu(balance_cpu, nohz.idle_cpus_mask) {
Suresh Siddha8a6d42d2011-12-06 11:19:37 -08008673 if (balance_cpu == this_cpu || !idle_cpu(balance_cpu))
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07008674 continue;
8675
8676 /*
8677 * If this cpu gets work to do, stop the load balancing
8678 * work being done for other cpus. Next load
8679 * balancing owner will pick it up.
8680 */
Suresh Siddha1c792db2011-12-01 17:07:32 -08008681 if (need_resched())
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07008682 break;
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07008683
Vincent Guittot5ed4f1d2012-09-13 06:11:26 +02008684 rq = cpu_rq(balance_cpu);
8685
Tim Chened61bbc2014-05-20 14:39:27 -07008686 /*
8687 * If time for next balance is due,
8688 * do the balance.
8689 */
8690 if (time_after_eq(jiffies, rq->next_balance)) {
8691 raw_spin_lock_irq(&rq->lock);
8692 update_rq_clock(rq);
Frederic Weisbeckercee1afc2016-04-13 15:56:50 +02008693 cpu_load_update_idle(rq);
Tim Chened61bbc2014-05-20 14:39:27 -07008694 raw_spin_unlock_irq(&rq->lock);
8695 rebalance_domains(rq, CPU_IDLE);
8696 }
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07008697
Vincent Guittotc5afb6a2015-08-03 11:55:50 +02008698 if (time_after(next_balance, rq->next_balance)) {
8699 next_balance = rq->next_balance;
8700 update_next_balance = 1;
8701 }
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07008702 }
Vincent Guittotc5afb6a2015-08-03 11:55:50 +02008703
8704 /*
8705 * next_balance will be updated only when there is a need.
8706 * When the CPU is attached to null domain for ex, it will not be
8707 * updated.
8708 */
8709 if (likely(update_next_balance))
8710 nohz.next_balance = next_balance;
Suresh Siddha1c792db2011-12-01 17:07:32 -08008711end:
8712 clear_bit(NOHZ_BALANCE_KICK, nohz_flags(this_cpu));
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07008713}
8714
8715/*
Suresh Siddha0b005cf2011-12-01 17:07:34 -08008716 * Current heuristic for kicking the idle load balancer in the presence
Vincent Guittot1aaf90a2015-02-27 16:54:14 +01008717 * of an idle cpu in the system.
Suresh Siddha0b005cf2011-12-01 17:07:34 -08008718 * - This rq has more than one task.
Vincent Guittot1aaf90a2015-02-27 16:54:14 +01008719 * - This rq has at least one CFS task and the capacity of the CPU is
8720 * significantly reduced because of RT tasks or IRQs.
8721 * - At parent of LLC scheduler domain level, this cpu's scheduler group has
8722 * multiple busy cpu.
Suresh Siddha0b005cf2011-12-01 17:07:34 -08008723 * - For SD_ASYM_PACKING, if the lower numbered cpu's in the scheduler
8724 * domain span are idle.
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07008725 */
Vincent Guittot1aaf90a2015-02-27 16:54:14 +01008726static inline bool nohz_kick_needed(struct rq *rq)
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07008727{
8728 unsigned long now = jiffies;
Peter Zijlstra0e369d72016-05-09 10:38:01 +02008729 struct sched_domain_shared *sds;
Suresh Siddha0b005cf2011-12-01 17:07:34 -08008730 struct sched_domain *sd;
Daniel Lezcano4a725622014-01-06 12:34:39 +01008731 int nr_busy, cpu = rq->cpu;
Vincent Guittot1aaf90a2015-02-27 16:54:14 +01008732 bool kick = false;
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07008733
Daniel Lezcano4a725622014-01-06 12:34:39 +01008734 if (unlikely(rq->idle_balance))
Vincent Guittot1aaf90a2015-02-27 16:54:14 +01008735 return false;
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07008736
Suresh Siddha1c792db2011-12-01 17:07:32 -08008737 /*
8738 * We may be recently in ticked or tickless idle mode. At the first
8739 * busy tick after returning from idle, we will update the busy stats.
8740 */
Suresh Siddha69e1e812011-12-01 17:07:33 -08008741 set_cpu_sd_state_busy();
Alex Shic1cc0172012-09-10 15:10:58 +08008742 nohz_balance_exit_idle(cpu);
Suresh Siddha0b005cf2011-12-01 17:07:34 -08008743
8744 /*
8745 * None are in tickless mode and hence no need for NOHZ idle load
8746 * balancing.
8747 */
8748 if (likely(!atomic_read(&nohz.nr_cpus)))
Vincent Guittot1aaf90a2015-02-27 16:54:14 +01008749 return false;
Suresh Siddha1c792db2011-12-01 17:07:32 -08008750
8751 if (time_before(now, nohz.next_balance))
Vincent Guittot1aaf90a2015-02-27 16:54:14 +01008752 return false;
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07008753
Suresh Siddha0b005cf2011-12-01 17:07:34 -08008754 if (rq->nr_running >= 2)
Vincent Guittot1aaf90a2015-02-27 16:54:14 +01008755 return true;
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07008756
Peter Zijlstra067491b2011-12-07 14:32:08 +01008757 rcu_read_lock();
Peter Zijlstra0e369d72016-05-09 10:38:01 +02008758 sds = rcu_dereference(per_cpu(sd_llc_shared, cpu));
8759 if (sds) {
8760 /*
8761 * XXX: write a coherent comment on why we do this.
8762 * See also: http://lkml.kernel.org/r/20111202010832.602203411@sbsiddha-desk.sc.intel.com
8763 */
8764 nr_busy = atomic_read(&sds->nr_busy_cpus);
Vincent Guittot1aaf90a2015-02-27 16:54:14 +01008765 if (nr_busy > 1) {
8766 kick = true;
8767 goto unlock;
8768 }
8769
8770 }
8771
8772 sd = rcu_dereference(rq->sd);
8773 if (sd) {
8774 if ((rq->cfs.h_nr_running >= 1) &&
8775 check_cpu_capacity(rq, sd)) {
8776 kick = true;
8777 goto unlock;
8778 }
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07008779 }
Preeti U Murthy37dc6b52013-10-30 08:42:52 +05308780
8781 sd = rcu_dereference(per_cpu(sd_asym, cpu));
Preeti U Murthy37dc6b52013-10-30 08:42:52 +05308782 if (sd && (cpumask_first_and(nohz.idle_cpus_mask,
Vincent Guittot1aaf90a2015-02-27 16:54:14 +01008783 sched_domain_span(sd)) < cpu)) {
8784 kick = true;
8785 goto unlock;
8786 }
Preeti U Murthy37dc6b52013-10-30 08:42:52 +05308787
Vincent Guittot1aaf90a2015-02-27 16:54:14 +01008788unlock:
Peter Zijlstra067491b2011-12-07 14:32:08 +01008789 rcu_read_unlock();
Vincent Guittot1aaf90a2015-02-27 16:54:14 +01008790 return kick;
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07008791}
8792#else
Daniel Lezcano208cb162014-01-06 12:34:44 +01008793static void nohz_idle_balance(struct rq *this_rq, enum cpu_idle_type idle) { }
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07008794#endif
8795
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008796/*
8797 * run_rebalance_domains is triggered when needed from the scheduler tick.
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07008798 * Also triggered for nohz idle balancing (with nohz_balancing_kick set).
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008799 */
Emese Revfy0766f782016-06-20 20:42:34 +02008800static __latent_entropy void run_rebalance_domains(struct softirq_action *h)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008801{
Daniel Lezcano208cb162014-01-06 12:34:44 +01008802 struct rq *this_rq = this_rq();
Suresh Siddha6eb57e02011-10-03 15:09:01 -07008803 enum cpu_idle_type idle = this_rq->idle_balance ?
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008804 CPU_IDLE : CPU_NOT_IDLE;
8805
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008806 /*
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07008807 * If this cpu has a pending nohz_balance_kick, then do the
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008808 * balancing on behalf of the other idle cpus whose ticks are
Preeti U Murthyd4573c32015-03-26 18:32:44 +05308809 * stopped. Do nohz_idle_balance *before* rebalance_domains to
8810 * give the idle cpus a chance to load balance. Else we may
8811 * load balance only within the local sched_domain hierarchy
8812 * and abort nohz_idle_balance altogether if we pull some load.
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008813 */
Daniel Lezcano208cb162014-01-06 12:34:44 +01008814 nohz_idle_balance(this_rq, idle);
Preeti U Murthyd4573c32015-03-26 18:32:44 +05308815 rebalance_domains(this_rq, idle);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008816}
8817
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008818/*
8819 * Trigger the SCHED_SOFTIRQ if it is time to do periodic load balancing.
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008820 */
Daniel Lezcano7caff662014-01-06 12:34:38 +01008821void trigger_load_balance(struct rq *rq)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008822{
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008823 /* Don't need to rebalance while attached to NULL domain */
Daniel Lezcanoc7260992014-01-06 12:34:45 +01008824 if (unlikely(on_null_domain(rq)))
8825 return;
8826
8827 if (time_after_eq(jiffies, rq->next_balance))
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008828 raise_softirq(SCHED_SOFTIRQ);
Frederic Weisbecker3451d022011-08-10 23:21:01 +02008829#ifdef CONFIG_NO_HZ_COMMON
Daniel Lezcanoc7260992014-01-06 12:34:45 +01008830 if (nohz_kick_needed(rq))
Daniel Lezcano0aeeeeb2014-01-06 12:34:42 +01008831 nohz_balancer_kick();
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07008832#endif
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01008833}
8834
Christian Ehrhardt0bcdcf22009-11-30 12:16:46 +01008835static void rq_online_fair(struct rq *rq)
8836{
8837 update_sysctl();
Kirill Tkhai0e59bda2014-06-25 12:19:42 +04008838
8839 update_runtime_enabled(rq);
Christian Ehrhardt0bcdcf22009-11-30 12:16:46 +01008840}
8841
8842static void rq_offline_fair(struct rq *rq)
8843{
8844 update_sysctl();
Peter Boonstoppela4c96ae2012-08-09 15:34:47 -07008845
8846 /* Ensure any throttled groups are reachable by pick_next_task */
8847 unthrottle_offline_cfs_rqs(rq);
Christian Ehrhardt0bcdcf22009-11-30 12:16:46 +01008848}
8849
Dhaval Giani55e12e52008-06-24 23:39:43 +05308850#endif /* CONFIG_SMP */
Peter Williamse1d14842007-10-24 18:23:51 +02008851
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02008852/*
8853 * scheduler tick hitting a task of our scheduling class:
8854 */
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01008855static void task_tick_fair(struct rq *rq, struct task_struct *curr, int queued)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02008856{
8857 struct cfs_rq *cfs_rq;
8858 struct sched_entity *se = &curr->se;
8859
8860 for_each_sched_entity(se) {
8861 cfs_rq = cfs_rq_of(se);
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01008862 entity_tick(cfs_rq, se, queued);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02008863 }
Ben Segall18bf2802012-10-04 12:51:20 +02008864
Srikar Dronamrajub52da862015-10-02 07:48:25 +05308865 if (static_branch_unlikely(&sched_numa_balancing))
Peter Zijlstracbee9f82012-10-25 14:16:43 +02008866 task_tick_numa(rq, curr);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02008867}
8868
8869/*
Peter Zijlstracd29fe62009-11-27 17:32:46 +01008870 * called on fork with the child task as argument from the parent's context
8871 * - child not yet on the tasklist
8872 * - preemption disabled
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02008873 */
Peter Zijlstracd29fe62009-11-27 17:32:46 +01008874static void task_fork_fair(struct task_struct *p)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02008875{
Daisuke Nishimura4fc420c2011-12-15 14:36:55 +09008876 struct cfs_rq *cfs_rq;
8877 struct sched_entity *se = &p->se, *curr;
Peter Zijlstracd29fe62009-11-27 17:32:46 +01008878 struct rq *rq = this_rq();
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02008879
Peter Zijlstrae210bff2016-06-16 18:51:48 +02008880 raw_spin_lock(&rq->lock);
Peter Zijlstra861d0342010-08-19 13:31:43 +02008881 update_rq_clock(rq);
8882
Daisuke Nishimura4fc420c2011-12-15 14:36:55 +09008883 cfs_rq = task_cfs_rq(current);
8884 curr = cfs_rq->curr;
Peter Zijlstrae210bff2016-06-16 18:51:48 +02008885 if (curr) {
8886 update_curr(cfs_rq);
Mike Galbraithb5d9d732009-09-08 11:12:28 +02008887 se->vruntime = curr->vruntime;
Peter Zijlstrae210bff2016-06-16 18:51:48 +02008888 }
Peter Zijlstraaeb73b02007-10-15 17:00:05 +02008889 place_entity(cfs_rq, se, 1);
Peter Zijlstra4d78e7b2007-10-15 17:00:04 +02008890
Peter Zijlstracd29fe62009-11-27 17:32:46 +01008891 if (sysctl_sched_child_runs_first && curr && entity_before(curr, se)) {
Dmitry Adamushko87fefa32007-10-15 17:00:08 +02008892 /*
Ingo Molnaredcb60a2007-10-15 17:00:08 +02008893 * Upon rescheduling, sched_class::put_prev_task() will place
8894 * 'current' within the tree based on its new key value.
8895 */
Peter Zijlstra4d78e7b2007-10-15 17:00:04 +02008896 swap(curr->vruntime, se->vruntime);
Kirill Tkhai88751252014-06-29 00:03:57 +04008897 resched_curr(rq);
Peter Zijlstra4d78e7b2007-10-15 17:00:04 +02008898 }
8899
Peter Zijlstra88ec22d2009-12-16 18:04:41 +01008900 se->vruntime -= cfs_rq->min_vruntime;
Peter Zijlstrae210bff2016-06-16 18:51:48 +02008901 raw_spin_unlock(&rq->lock);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02008902}
8903
Steven Rostedtcb469842008-01-25 21:08:22 +01008904/*
8905 * Priority of the task has changed. Check to see if we preempt
8906 * the current task.
8907 */
Peter Zijlstrada7a7352011-01-17 17:03:27 +01008908static void
8909prio_changed_fair(struct rq *rq, struct task_struct *p, int oldprio)
Steven Rostedtcb469842008-01-25 21:08:22 +01008910{
Kirill Tkhaida0c1e62014-08-20 13:47:32 +04008911 if (!task_on_rq_queued(p))
Peter Zijlstrada7a7352011-01-17 17:03:27 +01008912 return;
8913
Steven Rostedtcb469842008-01-25 21:08:22 +01008914 /*
8915 * Reschedule if we are currently running on this runqueue and
8916 * our priority decreased, or if we are not currently running on
8917 * this runqueue and our priority is higher than the current's
8918 */
Peter Zijlstrada7a7352011-01-17 17:03:27 +01008919 if (rq->curr == p) {
Steven Rostedtcb469842008-01-25 21:08:22 +01008920 if (p->prio > oldprio)
Kirill Tkhai88751252014-06-29 00:03:57 +04008921 resched_curr(rq);
Steven Rostedtcb469842008-01-25 21:08:22 +01008922 } else
Peter Zijlstra15afe092008-09-20 23:38:02 +02008923 check_preempt_curr(rq, p, 0);
Steven Rostedtcb469842008-01-25 21:08:22 +01008924}
8925
Byungchul Parkdaa59402015-08-20 20:22:00 +09008926static inline bool vruntime_normalized(struct task_struct *p)
8927{
8928 struct sched_entity *se = &p->se;
8929
8930 /*
8931 * In both the TASK_ON_RQ_QUEUED and TASK_ON_RQ_MIGRATING cases,
8932 * the dequeue_entity(.flags=0) will already have normalized the
8933 * vruntime.
8934 */
8935 if (p->on_rq)
8936 return true;
8937
8938 /*
8939 * When !on_rq, vruntime of the task has usually NOT been normalized.
8940 * But there are some cases where it has already been normalized:
8941 *
8942 * - A forked child which is waiting for being woken up by
8943 * wake_up_new_task().
8944 * - A task which has been woken up by try_to_wake_up() and
8945 * waiting for actually being woken up by sched_ttwu_pending().
8946 */
8947 if (!se->sum_exec_runtime || p->state == TASK_WAKING)
8948 return true;
8949
8950 return false;
8951}
8952
Vincent Guittot09a43ac2016-11-08 10:53:45 +01008953#ifdef CONFIG_FAIR_GROUP_SCHED
8954/*
8955 * Propagate the changes of the sched_entity across the tg tree to make it
8956 * visible to the root
8957 */
8958static void propagate_entity_cfs_rq(struct sched_entity *se)
8959{
8960 struct cfs_rq *cfs_rq;
8961
8962 /* Start to propagate at parent */
8963 se = se->parent;
8964
8965 for_each_sched_entity(se) {
8966 cfs_rq = cfs_rq_of(se);
8967
8968 if (cfs_rq_throttled(cfs_rq))
8969 break;
8970
8971 update_load_avg(se, UPDATE_TG);
8972 }
8973}
8974#else
8975static void propagate_entity_cfs_rq(struct sched_entity *se) { }
8976#endif
8977
Vincent Guittotdf217912016-11-08 10:53:42 +01008978static void detach_entity_cfs_rq(struct sched_entity *se)
Peter Zijlstrada7a7352011-01-17 17:03:27 +01008979{
Peter Zijlstrada7a7352011-01-17 17:03:27 +01008980 struct cfs_rq *cfs_rq = cfs_rq_of(se);
8981
Yuyang Du9d89c252015-07-15 08:04:37 +08008982 /* Catch up with the cfs_rq and remove our load when we leave */
Vincent Guittotd31b1a62016-11-08 10:53:44 +01008983 update_load_avg(se, 0);
Byungchul Parka05e8c52015-08-20 20:21:56 +09008984 detach_entity_load_avg(cfs_rq, se);
Peter Zijlstra7c3edd22016-07-13 10:56:25 +02008985 update_tg_load_avg(cfs_rq, false);
Vincent Guittot09a43ac2016-11-08 10:53:45 +01008986 propagate_entity_cfs_rq(se);
Peter Zijlstrada7a7352011-01-17 17:03:27 +01008987}
8988
Vincent Guittotdf217912016-11-08 10:53:42 +01008989static void attach_entity_cfs_rq(struct sched_entity *se)
Steven Rostedtcb469842008-01-25 21:08:22 +01008990{
Byungchul Parkdaa59402015-08-20 20:22:00 +09008991 struct cfs_rq *cfs_rq = cfs_rq_of(se);
Byungchul Park7855a352015-08-10 18:02:55 +09008992
8993#ifdef CONFIG_FAIR_GROUP_SCHED
Michael wangeb7a59b2014-02-20 11:14:53 +08008994 /*
8995 * Since the real-depth could have been changed (only FAIR
8996 * class maintain depth value), reset depth properly.
8997 */
8998 se->depth = se->parent ? se->parent->depth + 1 : 0;
8999#endif
Byungchul Park7855a352015-08-10 18:02:55 +09009000
Vincent Guittotdf217912016-11-08 10:53:42 +01009001 /* Synchronize entity with its cfs_rq */
Vincent Guittotd31b1a62016-11-08 10:53:44 +01009002 update_load_avg(se, sched_feat(ATTACH_AGE_LOAD) ? 0 : SKIP_AGE_LOAD);
Byungchul Parkdaa59402015-08-20 20:22:00 +09009003 attach_entity_load_avg(cfs_rq, se);
Peter Zijlstra7c3edd22016-07-13 10:56:25 +02009004 update_tg_load_avg(cfs_rq, false);
Vincent Guittot09a43ac2016-11-08 10:53:45 +01009005 propagate_entity_cfs_rq(se);
Vincent Guittotdf217912016-11-08 10:53:42 +01009006}
9007
9008static void detach_task_cfs_rq(struct task_struct *p)
9009{
9010 struct sched_entity *se = &p->se;
9011 struct cfs_rq *cfs_rq = cfs_rq_of(se);
9012
9013 if (!vruntime_normalized(p)) {
9014 /*
9015 * Fix up our vruntime so that the current sleep doesn't
9016 * cause 'unlimited' sleep bonus.
9017 */
9018 place_entity(cfs_rq, se, 0);
9019 se->vruntime -= cfs_rq->min_vruntime;
9020 }
9021
9022 detach_entity_cfs_rq(se);
9023}
9024
9025static void attach_task_cfs_rq(struct task_struct *p)
9026{
9027 struct sched_entity *se = &p->se;
9028 struct cfs_rq *cfs_rq = cfs_rq_of(se);
9029
9030 attach_entity_cfs_rq(se);
Byungchul Park6efdb102015-08-20 20:21:59 +09009031
Byungchul Parkdaa59402015-08-20 20:22:00 +09009032 if (!vruntime_normalized(p))
9033 se->vruntime += cfs_rq->min_vruntime;
9034}
Byungchul Park7855a352015-08-10 18:02:55 +09009035
Byungchul Parkdaa59402015-08-20 20:22:00 +09009036static void switched_from_fair(struct rq *rq, struct task_struct *p)
9037{
9038 detach_task_cfs_rq(p);
9039}
9040
9041static void switched_to_fair(struct rq *rq, struct task_struct *p)
9042{
9043 attach_task_cfs_rq(p);
9044
9045 if (task_on_rq_queued(p)) {
Byungchul Park7855a352015-08-10 18:02:55 +09009046 /*
Byungchul Parkdaa59402015-08-20 20:22:00 +09009047 * We were most likely switched from sched_rt, so
9048 * kick off the schedule if running, otherwise just see
9049 * if we can still preempt the current task.
Byungchul Park7855a352015-08-10 18:02:55 +09009050 */
Byungchul Parkdaa59402015-08-20 20:22:00 +09009051 if (rq->curr == p)
9052 resched_curr(rq);
9053 else
9054 check_preempt_curr(rq, p, 0);
Byungchul Park7855a352015-08-10 18:02:55 +09009055 }
Steven Rostedtcb469842008-01-25 21:08:22 +01009056}
9057
Srivatsa Vaddagiri83b699e2007-10-15 17:00:08 +02009058/* Account for a task changing its policy or group.
9059 *
9060 * This routine is mostly called to set cfs_rq->curr field when a task
9061 * migrates between groups/classes.
9062 */
9063static void set_curr_task_fair(struct rq *rq)
9064{
9065 struct sched_entity *se = &rq->curr->se;
9066
Paul Turnerec12cb72011-07-21 09:43:30 -07009067 for_each_sched_entity(se) {
9068 struct cfs_rq *cfs_rq = cfs_rq_of(se);
9069
9070 set_next_entity(cfs_rq, se);
9071 /* ensure bandwidth has been allocated on our new cfs_rq */
9072 account_cfs_rq_runtime(cfs_rq, 0);
9073 }
Srivatsa Vaddagiri83b699e2007-10-15 17:00:08 +02009074}
9075
Peter Zijlstra029632f2011-10-25 10:00:11 +02009076void init_cfs_rq(struct cfs_rq *cfs_rq)
9077{
9078 cfs_rq->tasks_timeline = RB_ROOT;
Peter Zijlstra029632f2011-10-25 10:00:11 +02009079 cfs_rq->min_vruntime = (u64)(-(1LL << 20));
9080#ifndef CONFIG_64BIT
9081 cfs_rq->min_vruntime_copy = cfs_rq->min_vruntime;
9082#endif
Alex Shi141965c2013-06-26 13:05:39 +08009083#ifdef CONFIG_SMP
Vincent Guittot09a43ac2016-11-08 10:53:45 +01009084#ifdef CONFIG_FAIR_GROUP_SCHED
9085 cfs_rq->propagate_avg = 0;
9086#endif
Yuyang Du9d89c252015-07-15 08:04:37 +08009087 atomic_long_set(&cfs_rq->removed_load_avg, 0);
9088 atomic_long_set(&cfs_rq->removed_util_avg, 0);
Paul Turner9ee474f2012-10-04 13:18:30 +02009089#endif
Peter Zijlstra029632f2011-10-25 10:00:11 +02009090}
9091
Peter Zijlstra810b3812008-02-29 15:21:01 -05009092#ifdef CONFIG_FAIR_GROUP_SCHED
Vincent Guittotea86cb42016-06-17 13:38:55 +02009093static void task_set_group_fair(struct task_struct *p)
9094{
9095 struct sched_entity *se = &p->se;
9096
9097 set_task_rq(p, task_cpu(p));
9098 se->depth = se->parent ? se->parent->depth + 1 : 0;
9099}
9100
Peter Zijlstrabc54da22015-08-31 17:13:55 +02009101static void task_move_group_fair(struct task_struct *p)
Peter Zijlstra810b3812008-02-29 15:21:01 -05009102{
Byungchul Parkdaa59402015-08-20 20:22:00 +09009103 detach_task_cfs_rq(p);
Peter Zijlstrab2b5ce02010-10-15 15:24:15 +02009104 set_task_rq(p, task_cpu(p));
Byungchul Park6efdb102015-08-20 20:21:59 +09009105
9106#ifdef CONFIG_SMP
9107 /* Tell se's cfs_rq has been changed -- migrated */
9108 p->se.avg.last_update_time = 0;
9109#endif
Byungchul Parkdaa59402015-08-20 20:22:00 +09009110 attach_task_cfs_rq(p);
Peter Zijlstra810b3812008-02-29 15:21:01 -05009111}
Peter Zijlstra029632f2011-10-25 10:00:11 +02009112
Vincent Guittotea86cb42016-06-17 13:38:55 +02009113static void task_change_group_fair(struct task_struct *p, int type)
9114{
9115 switch (type) {
9116 case TASK_SET_GROUP:
9117 task_set_group_fair(p);
9118 break;
9119
9120 case TASK_MOVE_GROUP:
9121 task_move_group_fair(p);
9122 break;
9123 }
9124}
9125
Peter Zijlstra029632f2011-10-25 10:00:11 +02009126void free_fair_sched_group(struct task_group *tg)
9127{
9128 int i;
9129
9130 destroy_cfs_bandwidth(tg_cfs_bandwidth(tg));
9131
9132 for_each_possible_cpu(i) {
9133 if (tg->cfs_rq)
9134 kfree(tg->cfs_rq[i]);
Peter Zijlstra6fe1f342016-01-21 22:24:16 +01009135 if (tg->se)
Peter Zijlstra029632f2011-10-25 10:00:11 +02009136 kfree(tg->se[i]);
9137 }
9138
9139 kfree(tg->cfs_rq);
9140 kfree(tg->se);
9141}
9142
9143int alloc_fair_sched_group(struct task_group *tg, struct task_group *parent)
9144{
Peter Zijlstra029632f2011-10-25 10:00:11 +02009145 struct sched_entity *se;
Peter Zijlstrab7fa30c2016-06-09 15:07:50 +02009146 struct cfs_rq *cfs_rq;
Peter Zijlstra029632f2011-10-25 10:00:11 +02009147 int i;
9148
9149 tg->cfs_rq = kzalloc(sizeof(cfs_rq) * nr_cpu_ids, GFP_KERNEL);
9150 if (!tg->cfs_rq)
9151 goto err;
9152 tg->se = kzalloc(sizeof(se) * nr_cpu_ids, GFP_KERNEL);
9153 if (!tg->se)
9154 goto err;
9155
9156 tg->shares = NICE_0_LOAD;
9157
9158 init_cfs_bandwidth(tg_cfs_bandwidth(tg));
9159
9160 for_each_possible_cpu(i) {
9161 cfs_rq = kzalloc_node(sizeof(struct cfs_rq),
9162 GFP_KERNEL, cpu_to_node(i));
9163 if (!cfs_rq)
9164 goto err;
9165
9166 se = kzalloc_node(sizeof(struct sched_entity),
9167 GFP_KERNEL, cpu_to_node(i));
9168 if (!se)
9169 goto err_free_rq;
9170
9171 init_cfs_rq(cfs_rq);
9172 init_tg_cfs_entry(tg, cfs_rq, se, i, parent->se[i]);
Yuyang Du540247f2015-07-15 08:04:39 +08009173 init_entity_runnable_average(se);
Peter Zijlstra029632f2011-10-25 10:00:11 +02009174 }
9175
9176 return 1;
9177
9178err_free_rq:
9179 kfree(cfs_rq);
9180err:
9181 return 0;
9182}
9183
Peter Zijlstra8663e242016-06-22 14:58:02 +02009184void online_fair_sched_group(struct task_group *tg)
9185{
9186 struct sched_entity *se;
9187 struct rq *rq;
9188 int i;
9189
9190 for_each_possible_cpu(i) {
9191 rq = cpu_rq(i);
9192 se = tg->se[i];
9193
9194 raw_spin_lock_irq(&rq->lock);
9195 post_init_entity_util_avg(se);
Peter Zijlstra55e16d32016-06-22 15:14:26 +02009196 sync_throttle(tg, i);
Peter Zijlstra8663e242016-06-22 14:58:02 +02009197 raw_spin_unlock_irq(&rq->lock);
9198 }
9199}
9200
Peter Zijlstra6fe1f342016-01-21 22:24:16 +01009201void unregister_fair_sched_group(struct task_group *tg)
Peter Zijlstra029632f2011-10-25 10:00:11 +02009202{
Peter Zijlstra029632f2011-10-25 10:00:11 +02009203 unsigned long flags;
Peter Zijlstra6fe1f342016-01-21 22:24:16 +01009204 struct rq *rq;
9205 int cpu;
Peter Zijlstra029632f2011-10-25 10:00:11 +02009206
Peter Zijlstra6fe1f342016-01-21 22:24:16 +01009207 for_each_possible_cpu(cpu) {
9208 if (tg->se[cpu])
9209 remove_entity_load_avg(tg->se[cpu]);
Peter Zijlstra029632f2011-10-25 10:00:11 +02009210
Peter Zijlstra6fe1f342016-01-21 22:24:16 +01009211 /*
9212 * Only empty task groups can be destroyed; so we can speculatively
9213 * check on_list without danger of it being re-added.
9214 */
9215 if (!tg->cfs_rq[cpu]->on_list)
9216 continue;
9217
9218 rq = cpu_rq(cpu);
9219
9220 raw_spin_lock_irqsave(&rq->lock, flags);
9221 list_del_leaf_cfs_rq(tg->cfs_rq[cpu]);
9222 raw_spin_unlock_irqrestore(&rq->lock, flags);
9223 }
Peter Zijlstra029632f2011-10-25 10:00:11 +02009224}
9225
9226void init_tg_cfs_entry(struct task_group *tg, struct cfs_rq *cfs_rq,
9227 struct sched_entity *se, int cpu,
9228 struct sched_entity *parent)
9229{
9230 struct rq *rq = cpu_rq(cpu);
9231
9232 cfs_rq->tg = tg;
9233 cfs_rq->rq = rq;
Peter Zijlstra029632f2011-10-25 10:00:11 +02009234 init_cfs_rq_runtime(cfs_rq);
9235
9236 tg->cfs_rq[cpu] = cfs_rq;
9237 tg->se[cpu] = se;
9238
9239 /* se could be NULL for root_task_group */
9240 if (!se)
9241 return;
9242
Peter Zijlstrafed14d42012-02-11 06:05:00 +01009243 if (!parent) {
Peter Zijlstra029632f2011-10-25 10:00:11 +02009244 se->cfs_rq = &rq->cfs;
Peter Zijlstrafed14d42012-02-11 06:05:00 +01009245 se->depth = 0;
9246 } else {
Peter Zijlstra029632f2011-10-25 10:00:11 +02009247 se->cfs_rq = parent->my_q;
Peter Zijlstrafed14d42012-02-11 06:05:00 +01009248 se->depth = parent->depth + 1;
9249 }
Peter Zijlstra029632f2011-10-25 10:00:11 +02009250
9251 se->my_q = cfs_rq;
Paul Turner0ac9b1c2013-10-16 11:16:27 -07009252 /* guarantee group entities always have weight */
9253 update_load_set(&se->load, NICE_0_LOAD);
Peter Zijlstra029632f2011-10-25 10:00:11 +02009254 se->parent = parent;
9255}
9256
9257static DEFINE_MUTEX(shares_mutex);
9258
9259int sched_group_set_shares(struct task_group *tg, unsigned long shares)
9260{
9261 int i;
9262 unsigned long flags;
9263
9264 /*
9265 * We can't change the weight of the root cgroup.
9266 */
9267 if (!tg->se[0])
9268 return -EINVAL;
9269
9270 shares = clamp(shares, scale_load(MIN_SHARES), scale_load(MAX_SHARES));
9271
9272 mutex_lock(&shares_mutex);
9273 if (tg->shares == shares)
9274 goto done;
9275
9276 tg->shares = shares;
9277 for_each_possible_cpu(i) {
9278 struct rq *rq = cpu_rq(i);
9279 struct sched_entity *se;
9280
9281 se = tg->se[i];
9282 /* Propagate contribution to hierarchy */
9283 raw_spin_lock_irqsave(&rq->lock, flags);
Frederic Weisbecker71b1da42013-04-12 01:50:59 +02009284
9285 /* Possible calls to update_curr() need rq clock */
9286 update_rq_clock(rq);
Linus Torvalds17bc14b2012-12-14 07:20:43 -08009287 for_each_sched_entity(se)
Peter Zijlstra029632f2011-10-25 10:00:11 +02009288 update_cfs_shares(group_cfs_rq(se));
9289 raw_spin_unlock_irqrestore(&rq->lock, flags);
9290 }
9291
9292done:
9293 mutex_unlock(&shares_mutex);
9294 return 0;
9295}
9296#else /* CONFIG_FAIR_GROUP_SCHED */
9297
9298void free_fair_sched_group(struct task_group *tg) { }
9299
9300int alloc_fair_sched_group(struct task_group *tg, struct task_group *parent)
9301{
9302 return 1;
9303}
9304
Peter Zijlstra8663e242016-06-22 14:58:02 +02009305void online_fair_sched_group(struct task_group *tg) { }
9306
Peter Zijlstra6fe1f342016-01-21 22:24:16 +01009307void unregister_fair_sched_group(struct task_group *tg) { }
Peter Zijlstra029632f2011-10-25 10:00:11 +02009308
9309#endif /* CONFIG_FAIR_GROUP_SCHED */
9310
Peter Zijlstra810b3812008-02-29 15:21:01 -05009311
H Hartley Sweeten6d686f42010-01-13 20:21:52 -07009312static unsigned int get_rr_interval_fair(struct rq *rq, struct task_struct *task)
Peter Williams0d721ce2009-09-21 01:31:53 +00009313{
9314 struct sched_entity *se = &task->se;
Peter Williams0d721ce2009-09-21 01:31:53 +00009315 unsigned int rr_interval = 0;
9316
9317 /*
9318 * Time slice is 0 for SCHED_OTHER tasks that are on an otherwise
9319 * idle runqueue:
9320 */
Peter Williams0d721ce2009-09-21 01:31:53 +00009321 if (rq->cfs.load.weight)
Zhu Yanhaia59f4e02013-01-08 12:56:52 +08009322 rr_interval = NS_TO_JIFFIES(sched_slice(cfs_rq_of(se), se));
Peter Williams0d721ce2009-09-21 01:31:53 +00009323
9324 return rr_interval;
9325}
9326
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02009327/*
9328 * All the scheduling class methods:
9329 */
Peter Zijlstra029632f2011-10-25 10:00:11 +02009330const struct sched_class fair_sched_class = {
Ingo Molnar5522d5d2007-10-15 17:00:12 +02009331 .next = &idle_sched_class,
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02009332 .enqueue_task = enqueue_task_fair,
9333 .dequeue_task = dequeue_task_fair,
9334 .yield_task = yield_task_fair,
Mike Galbraithd95f4122011-02-01 09:50:51 -05009335 .yield_to_task = yield_to_task_fair,
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02009336
Ingo Molnar2e09bf52007-10-15 17:00:05 +02009337 .check_preempt_curr = check_preempt_wakeup,
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02009338
9339 .pick_next_task = pick_next_task_fair,
9340 .put_prev_task = put_prev_task_fair,
9341
Peter Williams681f3e62007-10-24 18:23:51 +02009342#ifdef CONFIG_SMP
Li Zefan4ce72a22008-10-22 15:25:26 +08009343 .select_task_rq = select_task_rq_fair,
Paul Turner0a74bef2012-10-04 13:18:30 +02009344 .migrate_task_rq = migrate_task_rq_fair,
Alex Shi141965c2013-06-26 13:05:39 +08009345
Christian Ehrhardt0bcdcf22009-11-30 12:16:46 +01009346 .rq_online = rq_online_fair,
9347 .rq_offline = rq_offline_fair,
Peter Zijlstra88ec22d2009-12-16 18:04:41 +01009348
Yuyang Du12695572015-07-15 08:04:40 +08009349 .task_dead = task_dead_fair,
Peter Zijlstrac5b28032015-05-15 17:43:35 +02009350 .set_cpus_allowed = set_cpus_allowed_common,
Peter Williams681f3e62007-10-24 18:23:51 +02009351#endif
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02009352
Srivatsa Vaddagiri83b699e2007-10-15 17:00:08 +02009353 .set_curr_task = set_curr_task_fair,
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02009354 .task_tick = task_tick_fair,
Peter Zijlstracd29fe62009-11-27 17:32:46 +01009355 .task_fork = task_fork_fair,
Steven Rostedtcb469842008-01-25 21:08:22 +01009356
9357 .prio_changed = prio_changed_fair,
Peter Zijlstrada7a7352011-01-17 17:03:27 +01009358 .switched_from = switched_from_fair,
Steven Rostedtcb469842008-01-25 21:08:22 +01009359 .switched_to = switched_to_fair,
Peter Zijlstra810b3812008-02-29 15:21:01 -05009360
Peter Williams0d721ce2009-09-21 01:31:53 +00009361 .get_rr_interval = get_rr_interval_fair,
9362
Stanislaw Gruszka6e998912014-11-12 16:58:44 +01009363 .update_curr = update_curr_fair,
9364
Peter Zijlstra810b3812008-02-29 15:21:01 -05009365#ifdef CONFIG_FAIR_GROUP_SCHED
Vincent Guittotea86cb42016-06-17 13:38:55 +02009366 .task_change_group = task_change_group_fair,
Peter Zijlstra810b3812008-02-29 15:21:01 -05009367#endif
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02009368};
9369
9370#ifdef CONFIG_SCHED_DEBUG
Peter Zijlstra029632f2011-10-25 10:00:11 +02009371void print_cfs_stats(struct seq_file *m, int cpu)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02009372{
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02009373 struct cfs_rq *cfs_rq;
9374
Peter Zijlstra5973e5b2008-01-25 21:08:34 +01009375 rcu_read_lock();
Ingo Molnarc3b64f12007-08-09 11:16:51 +02009376 for_each_leaf_cfs_rq(cpu_rq(cpu), cfs_rq)
Ingo Molnar5cef9ec2007-08-09 11:16:47 +02009377 print_cfs_rq(m, cpu, cfs_rq);
Peter Zijlstra5973e5b2008-01-25 21:08:34 +01009378 rcu_read_unlock();
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02009379}
Srikar Dronamraju397f2372015-06-25 22:51:43 +05309380
9381#ifdef CONFIG_NUMA_BALANCING
9382void show_numa_stats(struct task_struct *p, struct seq_file *m)
9383{
9384 int node;
9385 unsigned long tsf = 0, tpf = 0, gsf = 0, gpf = 0;
9386
9387 for_each_online_node(node) {
9388 if (p->numa_faults) {
9389 tsf = p->numa_faults[task_faults_idx(NUMA_MEM, node, 0)];
9390 tpf = p->numa_faults[task_faults_idx(NUMA_MEM, node, 1)];
9391 }
9392 if (p->numa_group) {
9393 gsf = p->numa_group->faults[task_faults_idx(NUMA_MEM, node, 0)],
9394 gpf = p->numa_group->faults[task_faults_idx(NUMA_MEM, node, 1)];
9395 }
9396 print_numa_stats(m, node, tsf, tpf, gsf, gpf);
9397 }
9398}
9399#endif /* CONFIG_NUMA_BALANCING */
9400#endif /* CONFIG_SCHED_DEBUG */
Peter Zijlstra029632f2011-10-25 10:00:11 +02009401
9402__init void init_sched_fair_class(void)
9403{
9404#ifdef CONFIG_SMP
9405 open_softirq(SCHED_SOFTIRQ, run_rebalance_domains);
9406
Frederic Weisbecker3451d022011-08-10 23:21:01 +02009407#ifdef CONFIG_NO_HZ_COMMON
Diwakar Tundlam554ceca2012-03-07 14:44:26 -08009408 nohz.next_balance = jiffies;
Peter Zijlstra029632f2011-10-25 10:00:11 +02009409 zalloc_cpumask_var(&nohz.idle_cpus_mask, GFP_NOWAIT);
Peter Zijlstra029632f2011-10-25 10:00:11 +02009410#endif
9411#endif /* SMP */
9412
9413}