]> bbs.cooldavid.org Git - net-next-2.6.git/blame - kernel/sched_fair.c
sched: track cfs_rq->curr on !group-scheduling too
[net-next-2.6.git] / kernel / sched_fair.c
CommitLineData
bf0f6f24
IM
1/*
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>
21805085
PZ
18 *
19 * Adaptive scheduling granularity, math enhancements by Peter Zijlstra
20 * Copyright (C) 2007 Red Hat, Inc., Peter Zijlstra <pzijlstr@redhat.com>
bf0f6f24
IM
21 */
22
2bd8e6d4
IM
23/*
24 * Tunables that become constants when CONFIG_SCHED_DEBUG is off:
25 */
26#ifdef CONFIG_SCHED_DEBUG
27# define const_debug __read_mostly
28#else
29# define const_debug static const
30#endif
31
bf0f6f24 32/*
21805085
PZ
33 * Targeted preemption latency for CPU-bound tasks:
34 * (default: 20ms, units: nanoseconds)
bf0f6f24 35 *
21805085
PZ
36 * NOTE: this latency value is not the same as the concept of
37 * 'timeslice length' - timeslices in CFS are of variable length.
38 * (to see the precise effective timeslice length of your workload,
39 * run vmstat and monitor the context-switches field)
bf0f6f24
IM
40 *
41 * On SMP systems the value of this is multiplied by the log2 of the
42 * number of CPUs. (i.e. factor 2x on 2-way systems, 3x on 4-way
43 * systems, 4x on 8-way systems, 5x on 16-way systems, etc.)
21805085 44 * Targeted preemption latency for CPU-bound tasks:
bf0f6f24 45 */
2bd8e6d4
IM
46const_debug unsigned int sysctl_sched_latency = 20000000ULL;
47
48/*
49 * After fork, child runs first. (default) If set to 0 then
50 * parent will (try to) run first.
51 */
52const_debug unsigned int sysctl_sched_child_runs_first = 1;
21805085
PZ
53
54/*
55 * Minimal preemption granularity for CPU-bound tasks:
56 * (default: 2 msec, units: nanoseconds)
57 */
172ac3db 58unsigned int sysctl_sched_min_granularity __read_mostly = 2000000ULL;
bf0f6f24 59
1799e35d
IM
60/*
61 * sys_sched_yield() compat mode
62 *
63 * This option switches the agressive yield implementation of the
64 * old scheduler back on.
65 */
66unsigned int __read_mostly sysctl_sched_compat_yield;
67
bf0f6f24
IM
68/*
69 * SCHED_BATCH wake-up granularity.
71fd3714 70 * (default: 25 msec, units: nanoseconds)
bf0f6f24
IM
71 *
72 * This option delays the preemption effects of decoupled workloads
73 * and reduces their over-scheduling. Synchronous workloads will still
74 * have immediate wakeup/sleep latencies.
75 */
2bd8e6d4 76const_debug unsigned int sysctl_sched_batch_wakeup_granularity = 25000000UL;
bf0f6f24
IM
77
78/*
79 * SCHED_OTHER wake-up granularity.
80 * (default: 1 msec, units: nanoseconds)
81 *
82 * This option delays the preemption effects of decoupled workloads
83 * and reduces their over-scheduling. Synchronous workloads will still
84 * have immediate wakeup/sleep latencies.
85 */
2bd8e6d4 86const_debug unsigned int sysctl_sched_wakeup_granularity = 1000000UL;
bf0f6f24 87
bf0f6f24
IM
88unsigned int sysctl_sched_runtime_limit __read_mostly;
89
90/*
91 * Debugging: various feature bits
92 */
93enum {
94 SCHED_FEAT_FAIR_SLEEPERS = 1,
95 SCHED_FEAT_SLEEPER_AVG = 2,
96 SCHED_FEAT_SLEEPER_LOAD_AVG = 4,
a25707f3
IM
97 SCHED_FEAT_START_DEBIT = 8,
98 SCHED_FEAT_SKIP_INITIAL = 16,
bf0f6f24
IM
99};
100
2bd8e6d4 101const_debug unsigned int sysctl_sched_features =
bf0f6f24 102 SCHED_FEAT_FAIR_SLEEPERS *1 |
5d2b3d36 103 SCHED_FEAT_SLEEPER_AVG *0 |
bf0f6f24 104 SCHED_FEAT_SLEEPER_LOAD_AVG *1 |
bf0f6f24
IM
105 SCHED_FEAT_START_DEBIT *1 |
106 SCHED_FEAT_SKIP_INITIAL *0;
107
108extern struct sched_class fair_sched_class;
109
110/**************************************************************
111 * CFS operations on generic schedulable entities:
112 */
113
bf0f6f24
IM
114/* currently running entity (if any) on this cfs_rq */
115static inline struct sched_entity *cfs_rq_curr(struct cfs_rq *cfs_rq)
116{
117 return cfs_rq->curr;
118}
119
bf0f6f24
IM
120static inline void
121set_cfs_rq_curr(struct cfs_rq *cfs_rq, struct sched_entity *se)
122{
123 cfs_rq->curr = se;
124}
125
62160e3f 126#ifdef CONFIG_FAIR_GROUP_SCHED
bf0f6f24 127
62160e3f 128/* cpu runqueue to which this cfs_rq is attached */
bf0f6f24
IM
129static inline struct rq *rq_of(struct cfs_rq *cfs_rq)
130{
62160e3f 131 return cfs_rq->rq;
bf0f6f24
IM
132}
133
62160e3f
IM
134/* An entity is a task if it doesn't "own" a runqueue */
135#define entity_is_task(se) (!se->my_q)
bf0f6f24 136
62160e3f 137#else /* CONFIG_FAIR_GROUP_SCHED */
bf0f6f24 138
62160e3f
IM
139static inline struct rq *rq_of(struct cfs_rq *cfs_rq)
140{
141 return container_of(cfs_rq, struct rq, cfs);
bf0f6f24
IM
142}
143
144#define entity_is_task(se) 1
145
bf0f6f24
IM
146#endif /* CONFIG_FAIR_GROUP_SCHED */
147
148static inline struct task_struct *task_of(struct sched_entity *se)
149{
150 return container_of(se, struct task_struct, se);
151}
152
153
154/**************************************************************
155 * Scheduling class tree data structure manipulation methods:
156 */
157
158/*
159 * Enqueue an entity into the rb-tree:
160 */
161static inline void
162__enqueue_entity(struct cfs_rq *cfs_rq, struct sched_entity *se)
163{
164 struct rb_node **link = &cfs_rq->tasks_timeline.rb_node;
165 struct rb_node *parent = NULL;
166 struct sched_entity *entry;
167 s64 key = se->fair_key;
168 int leftmost = 1;
169
170 /*
171 * Find the right place in the rbtree:
172 */
173 while (*link) {
174 parent = *link;
175 entry = rb_entry(parent, struct sched_entity, run_node);
176 /*
177 * We dont care about collisions. Nodes with
178 * the same key stay together.
179 */
180 if (key - entry->fair_key < 0) {
181 link = &parent->rb_left;
182 } else {
183 link = &parent->rb_right;
184 leftmost = 0;
185 }
186 }
187
188 /*
189 * Maintain a cache of leftmost tree entries (it is frequently
190 * used):
191 */
192 if (leftmost)
193 cfs_rq->rb_leftmost = &se->run_node;
194
195 rb_link_node(&se->run_node, parent, link);
196 rb_insert_color(&se->run_node, &cfs_rq->tasks_timeline);
197 update_load_add(&cfs_rq->load, se->load.weight);
198 cfs_rq->nr_running++;
199 se->on_rq = 1;
a206c072
IM
200
201 schedstat_add(cfs_rq, wait_runtime, se->wait_runtime);
bf0f6f24
IM
202}
203
204static inline void
205__dequeue_entity(struct cfs_rq *cfs_rq, struct sched_entity *se)
206{
207 if (cfs_rq->rb_leftmost == &se->run_node)
208 cfs_rq->rb_leftmost = rb_next(&se->run_node);
209 rb_erase(&se->run_node, &cfs_rq->tasks_timeline);
210 update_load_sub(&cfs_rq->load, se->load.weight);
211 cfs_rq->nr_running--;
212 se->on_rq = 0;
a206c072
IM
213
214 schedstat_add(cfs_rq, wait_runtime, -se->wait_runtime);
bf0f6f24
IM
215}
216
217static inline struct rb_node *first_fair(struct cfs_rq *cfs_rq)
218{
219 return cfs_rq->rb_leftmost;
220}
221
222static struct sched_entity *__pick_next_entity(struct cfs_rq *cfs_rq)
223{
224 return rb_entry(first_fair(cfs_rq), struct sched_entity, run_node);
225}
226
227/**************************************************************
228 * Scheduling class statistics methods:
229 */
230
21805085
PZ
231/*
232 * Calculate the preemption granularity needed to schedule every
233 * runnable task once per sysctl_sched_latency amount of time.
234 * (down to a sensible low limit on granularity)
235 *
236 * For example, if there are 2 tasks running and latency is 10 msecs,
237 * we switch tasks every 5 msecs. If we have 3 tasks running, we have
238 * to switch tasks every 3.33 msecs to get a 10 msecs observed latency
239 * for each task. We do finer and finer scheduling up to until we
240 * reach the minimum granularity value.
241 *
242 * To achieve this we use the following dynamic-granularity rule:
243 *
244 * gran = lat/nr - lat/nr/nr
245 *
246 * This comes out of the following equations:
247 *
248 * kA1 + gran = kB1
249 * kB2 + gran = kA2
250 * kA2 = kA1
251 * kB2 = kB1 - d + d/nr
252 * lat = d * nr
253 *
254 * Where 'k' is key, 'A' is task A (waiting), 'B' is task B (running),
255 * '1' is start of time, '2' is end of time, 'd' is delay between
256 * 1 and 2 (during which task B was running), 'nr' is number of tasks
257 * running, 'lat' is the the period of each task. ('lat' is the
258 * sched_latency that we aim for.)
259 */
260static long
261sched_granularity(struct cfs_rq *cfs_rq)
262{
263 unsigned int gran = sysctl_sched_latency;
264 unsigned int nr = cfs_rq->nr_running;
265
266 if (nr > 1) {
267 gran = gran/nr - gran/nr/nr;
172ac3db 268 gran = max(gran, sysctl_sched_min_granularity);
21805085
PZ
269 }
270
271 return gran;
272}
273
bf0f6f24
IM
274/*
275 * We rescale the rescheduling granularity of tasks according to their
276 * nice level, but only linearly, not exponentially:
277 */
278static long
279niced_granularity(struct sched_entity *curr, unsigned long granularity)
280{
281 u64 tmp;
282
7cff8cf6
IM
283 if (likely(curr->load.weight == NICE_0_LOAD))
284 return granularity;
bf0f6f24 285 /*
7cff8cf6 286 * Positive nice levels get the same granularity as nice-0:
bf0f6f24 287 */
7cff8cf6
IM
288 if (likely(curr->load.weight < NICE_0_LOAD)) {
289 tmp = curr->load.weight * (u64)granularity;
290 return (long) (tmp >> NICE_0_SHIFT);
291 }
bf0f6f24 292 /*
7cff8cf6 293 * Negative nice level tasks get linearly finer
bf0f6f24
IM
294 * granularity:
295 */
7cff8cf6 296 tmp = curr->load.inv_weight * (u64)granularity;
bf0f6f24
IM
297
298 /*
299 * It will always fit into 'long':
300 */
a0dc7260 301 return (long) (tmp >> (WMULT_SHIFT-NICE_0_SHIFT));
bf0f6f24
IM
302}
303
304static inline void
305limit_wait_runtime(struct cfs_rq *cfs_rq, struct sched_entity *se)
306{
307 long limit = sysctl_sched_runtime_limit;
308
309 /*
310 * Niced tasks have the same history dynamic range as
311 * non-niced tasks:
312 */
313 if (unlikely(se->wait_runtime > limit)) {
314 se->wait_runtime = limit;
315 schedstat_inc(se, wait_runtime_overruns);
316 schedstat_inc(cfs_rq, wait_runtime_overruns);
317 }
318 if (unlikely(se->wait_runtime < -limit)) {
319 se->wait_runtime = -limit;
320 schedstat_inc(se, wait_runtime_underruns);
321 schedstat_inc(cfs_rq, wait_runtime_underruns);
322 }
323}
324
325static inline void
326__add_wait_runtime(struct cfs_rq *cfs_rq, struct sched_entity *se, long delta)
327{
328 se->wait_runtime += delta;
329 schedstat_add(se, sum_wait_runtime, delta);
330 limit_wait_runtime(cfs_rq, se);
331}
332
333static void
334add_wait_runtime(struct cfs_rq *cfs_rq, struct sched_entity *se, long delta)
335{
336 schedstat_add(cfs_rq, wait_runtime, -se->wait_runtime);
337 __add_wait_runtime(cfs_rq, se, delta);
338 schedstat_add(cfs_rq, wait_runtime, se->wait_runtime);
339}
340
341/*
342 * Update the current task's runtime statistics. Skip current tasks that
343 * are not in our scheduling class.
344 */
345static inline void
8ebc91d9
IM
346__update_curr(struct cfs_rq *cfs_rq, struct sched_entity *curr,
347 unsigned long delta_exec)
bf0f6f24 348{
8ebc91d9 349 unsigned long delta, delta_fair, delta_mine;
bf0f6f24
IM
350 struct load_weight *lw = &cfs_rq->load;
351 unsigned long load = lw->weight;
352
8179ca23 353 schedstat_set(curr->exec_max, max((u64)delta_exec, curr->exec_max));
bf0f6f24
IM
354
355 curr->sum_exec_runtime += delta_exec;
356 cfs_rq->exec_clock += delta_exec;
357
fd8bb43e
IM
358 if (unlikely(!load))
359 return;
360
bf0f6f24
IM
361 delta_fair = calc_delta_fair(delta_exec, lw);
362 delta_mine = calc_delta_mine(delta_exec, curr->load.weight, lw);
363
5f01d519 364 if (cfs_rq->sleeper_bonus > sysctl_sched_min_granularity) {
ea0aa3b2 365 delta = min((u64)delta_mine, cfs_rq->sleeper_bonus);
b2133c8b
IM
366 delta = min(delta, (unsigned long)(
367 (long)sysctl_sched_runtime_limit - curr->wait_runtime));
bf0f6f24
IM
368 cfs_rq->sleeper_bonus -= delta;
369 delta_mine -= delta;
370 }
371
372 cfs_rq->fair_clock += delta_fair;
373 /*
374 * We executed delta_exec amount of time on the CPU,
375 * but we were only entitled to delta_mine amount of
376 * time during that period (if nr_running == 1 then
377 * the two values are equal)
378 * [Note: delta_mine - delta_exec is negative]:
379 */
380 add_wait_runtime(cfs_rq, curr, delta_mine - delta_exec);
381}
382
b7cc0896 383static void update_curr(struct cfs_rq *cfs_rq)
bf0f6f24
IM
384{
385 struct sched_entity *curr = cfs_rq_curr(cfs_rq);
8ebc91d9 386 u64 now = rq_of(cfs_rq)->clock;
bf0f6f24
IM
387 unsigned long delta_exec;
388
389 if (unlikely(!curr))
390 return;
391
392 /*
393 * Get the amount of time the current task was running
394 * since the last time we changed load (this cannot
395 * overflow on 32 bits):
396 */
8ebc91d9 397 delta_exec = (unsigned long)(now - curr->exec_start);
bf0f6f24 398
8ebc91d9
IM
399 __update_curr(cfs_rq, curr, delta_exec);
400 curr->exec_start = now;
bf0f6f24
IM
401}
402
403static inline void
5870db5b 404update_stats_wait_start(struct cfs_rq *cfs_rq, struct sched_entity *se)
bf0f6f24
IM
405{
406 se->wait_start_fair = cfs_rq->fair_clock;
d281918d 407 schedstat_set(se->wait_start, rq_of(cfs_rq)->clock);
bf0f6f24
IM
408}
409
410/*
411 * We calculate fair deltas here, so protect against the random effects
412 * of a multiplication overflow by capping it to the runtime limit:
413 */
414#if BITS_PER_LONG == 32
415static inline unsigned long
416calc_weighted(unsigned long delta, unsigned long weight, int shift)
417{
418 u64 tmp = (u64)delta * weight >> shift;
419
420 if (unlikely(tmp > sysctl_sched_runtime_limit*2))
421 return sysctl_sched_runtime_limit*2;
422 return tmp;
423}
424#else
425static inline unsigned long
426calc_weighted(unsigned long delta, unsigned long weight, int shift)
427{
428 return delta * weight >> shift;
429}
430#endif
431
432/*
433 * Task is being enqueued - update stats:
434 */
d2417e5a 435static void update_stats_enqueue(struct cfs_rq *cfs_rq, struct sched_entity *se)
bf0f6f24
IM
436{
437 s64 key;
438
439 /*
440 * Are we enqueueing a waiting task? (for current tasks
441 * a dequeue/enqueue event is a NOP)
442 */
443 if (se != cfs_rq_curr(cfs_rq))
5870db5b 444 update_stats_wait_start(cfs_rq, se);
bf0f6f24
IM
445 /*
446 * Update the key:
447 */
448 key = cfs_rq->fair_clock;
449
450 /*
451 * Optimize the common nice 0 case:
452 */
453 if (likely(se->load.weight == NICE_0_LOAD)) {
454 key -= se->wait_runtime;
455 } else {
456 u64 tmp;
457
458 if (se->wait_runtime < 0) {
459 tmp = -se->wait_runtime;
460 key += (tmp * se->load.inv_weight) >>
461 (WMULT_SHIFT - NICE_0_SHIFT);
462 } else {
463 tmp = se->wait_runtime;
a69edb55
IM
464 key -= (tmp * se->load.inv_weight) >>
465 (WMULT_SHIFT - NICE_0_SHIFT);
bf0f6f24
IM
466 }
467 }
468
469 se->fair_key = key;
470}
471
472/*
473 * Note: must be called with a freshly updated rq->fair_clock.
474 */
475static inline void
8ebc91d9
IM
476__update_stats_wait_end(struct cfs_rq *cfs_rq, struct sched_entity *se,
477 unsigned long delta_fair)
bf0f6f24 478{
d281918d
IM
479 schedstat_set(se->wait_max, max(se->wait_max,
480 rq_of(cfs_rq)->clock - se->wait_start));
bf0f6f24
IM
481
482 if (unlikely(se->load.weight != NICE_0_LOAD))
483 delta_fair = calc_weighted(delta_fair, se->load.weight,
484 NICE_0_SHIFT);
485
486 add_wait_runtime(cfs_rq, se, delta_fair);
487}
488
489static void
9ef0a961 490update_stats_wait_end(struct cfs_rq *cfs_rq, struct sched_entity *se)
bf0f6f24
IM
491{
492 unsigned long delta_fair;
493
b77d69db
IM
494 if (unlikely(!se->wait_start_fair))
495 return;
496
bf0f6f24
IM
497 delta_fair = (unsigned long)min((u64)(2*sysctl_sched_runtime_limit),
498 (u64)(cfs_rq->fair_clock - se->wait_start_fair));
499
8ebc91d9 500 __update_stats_wait_end(cfs_rq, se, delta_fair);
bf0f6f24
IM
501
502 se->wait_start_fair = 0;
6cfb0d5d 503 schedstat_set(se->wait_start, 0);
bf0f6f24
IM
504}
505
506static inline void
19b6a2e3 507update_stats_dequeue(struct cfs_rq *cfs_rq, struct sched_entity *se)
bf0f6f24 508{
b7cc0896 509 update_curr(cfs_rq);
bf0f6f24
IM
510 /*
511 * Mark the end of the wait period if dequeueing a
512 * waiting task:
513 */
514 if (se != cfs_rq_curr(cfs_rq))
9ef0a961 515 update_stats_wait_end(cfs_rq, se);
bf0f6f24
IM
516}
517
518/*
519 * We are picking a new current task - update its stats:
520 */
521static inline void
79303e9e 522update_stats_curr_start(struct cfs_rq *cfs_rq, struct sched_entity *se)
bf0f6f24
IM
523{
524 /*
525 * We are starting a new run period:
526 */
d281918d 527 se->exec_start = rq_of(cfs_rq)->clock;
bf0f6f24
IM
528}
529
530/*
531 * We are descheduling a task - update its stats:
532 */
533static inline void
c7e9b5b2 534update_stats_curr_end(struct cfs_rq *cfs_rq, struct sched_entity *se)
bf0f6f24
IM
535{
536 se->exec_start = 0;
537}
538
539/**************************************************
540 * Scheduling class queueing methods:
541 */
542
8ebc91d9
IM
543static void __enqueue_sleeper(struct cfs_rq *cfs_rq, struct sched_entity *se,
544 unsigned long delta_fair)
bf0f6f24 545{
8ebc91d9 546 unsigned long load = cfs_rq->load.weight;
bf0f6f24
IM
547 long prev_runtime;
548
b2133c8b
IM
549 /*
550 * Do not boost sleepers if there's too much bonus 'in flight'
551 * already:
552 */
553 if (unlikely(cfs_rq->sleeper_bonus > sysctl_sched_runtime_limit))
554 return;
555
bf0f6f24
IM
556 if (sysctl_sched_features & SCHED_FEAT_SLEEPER_LOAD_AVG)
557 load = rq_of(cfs_rq)->cpu_load[2];
558
bf0f6f24
IM
559 /*
560 * Fix up delta_fair with the effect of us running
561 * during the whole sleep period:
562 */
563 if (sysctl_sched_features & SCHED_FEAT_SLEEPER_AVG)
564 delta_fair = div64_likely32((u64)delta_fair * load,
565 load + se->load.weight);
566
567 if (unlikely(se->load.weight != NICE_0_LOAD))
568 delta_fair = calc_weighted(delta_fair, se->load.weight,
569 NICE_0_SHIFT);
570
571 prev_runtime = se->wait_runtime;
572 __add_wait_runtime(cfs_rq, se, delta_fair);
573 delta_fair = se->wait_runtime - prev_runtime;
574
575 /*
576 * Track the amount of bonus we've given to sleepers:
577 */
578 cfs_rq->sleeper_bonus += delta_fair;
bf0f6f24
IM
579}
580
2396af69 581static void enqueue_sleeper(struct cfs_rq *cfs_rq, struct sched_entity *se)
bf0f6f24
IM
582{
583 struct task_struct *tsk = task_of(se);
584 unsigned long delta_fair;
585
586 if ((entity_is_task(se) && tsk->policy == SCHED_BATCH) ||
587 !(sysctl_sched_features & SCHED_FEAT_FAIR_SLEEPERS))
588 return;
589
590 delta_fair = (unsigned long)min((u64)(2*sysctl_sched_runtime_limit),
591 (u64)(cfs_rq->fair_clock - se->sleep_start_fair));
592
8ebc91d9 593 __enqueue_sleeper(cfs_rq, se, delta_fair);
bf0f6f24
IM
594
595 se->sleep_start_fair = 0;
596
597#ifdef CONFIG_SCHEDSTATS
598 if (se->sleep_start) {
d281918d 599 u64 delta = rq_of(cfs_rq)->clock - se->sleep_start;
bf0f6f24
IM
600
601 if ((s64)delta < 0)
602 delta = 0;
603
604 if (unlikely(delta > se->sleep_max))
605 se->sleep_max = delta;
606
607 se->sleep_start = 0;
608 se->sum_sleep_runtime += delta;
609 }
610 if (se->block_start) {
d281918d 611 u64 delta = rq_of(cfs_rq)->clock - se->block_start;
bf0f6f24
IM
612
613 if ((s64)delta < 0)
614 delta = 0;
615
616 if (unlikely(delta > se->block_max))
617 se->block_max = delta;
618
619 se->block_start = 0;
620 se->sum_sleep_runtime += delta;
30084fbd
IM
621
622 /*
623 * Blocking time is in units of nanosecs, so shift by 20 to
624 * get a milliseconds-range estimation of the amount of
625 * time that the task spent sleeping:
626 */
627 if (unlikely(prof_on == SLEEP_PROFILING)) {
628 profile_hits(SLEEP_PROFILING, (void *)get_wchan(tsk),
629 delta >> 20);
630 }
bf0f6f24
IM
631 }
632#endif
633}
634
635static void
668031ca 636enqueue_entity(struct cfs_rq *cfs_rq, struct sched_entity *se, int wakeup)
bf0f6f24
IM
637{
638 /*
639 * Update the fair clock.
640 */
b7cc0896 641 update_curr(cfs_rq);
bf0f6f24
IM
642
643 if (wakeup)
2396af69 644 enqueue_sleeper(cfs_rq, se);
bf0f6f24 645
d2417e5a 646 update_stats_enqueue(cfs_rq, se);
bf0f6f24
IM
647 __enqueue_entity(cfs_rq, se);
648}
649
650static void
525c2716 651dequeue_entity(struct cfs_rq *cfs_rq, struct sched_entity *se, int sleep)
bf0f6f24 652{
19b6a2e3 653 update_stats_dequeue(cfs_rq, se);
bf0f6f24
IM
654 if (sleep) {
655 se->sleep_start_fair = cfs_rq->fair_clock;
656#ifdef CONFIG_SCHEDSTATS
657 if (entity_is_task(se)) {
658 struct task_struct *tsk = task_of(se);
659
660 if (tsk->state & TASK_INTERRUPTIBLE)
d281918d 661 se->sleep_start = rq_of(cfs_rq)->clock;
bf0f6f24 662 if (tsk->state & TASK_UNINTERRUPTIBLE)
d281918d 663 se->block_start = rq_of(cfs_rq)->clock;
bf0f6f24 664 }
bf0f6f24
IM
665#endif
666 }
667 __dequeue_entity(cfs_rq, se);
668}
669
670/*
671 * Preempt the current task with a newly woken task if needed:
672 */
7c92e54f 673static void
bf0f6f24
IM
674__check_preempt_curr_fair(struct cfs_rq *cfs_rq, struct sched_entity *se,
675 struct sched_entity *curr, unsigned long granularity)
676{
677 s64 __delta = curr->fair_key - se->fair_key;
11697830
PZ
678 unsigned long ideal_runtime, delta_exec;
679
680 /*
681 * ideal_runtime is compared against sum_exec_runtime, which is
682 * walltime, hence do not scale.
683 */
684 ideal_runtime = max(sysctl_sched_latency / cfs_rq->nr_running,
685 (unsigned long)sysctl_sched_min_granularity);
686
687 /*
688 * If we executed more than what the latency constraint suggests,
689 * reduce the rescheduling granularity. This way the total latency
690 * of how much a task is not scheduled converges to
691 * sysctl_sched_latency:
692 */
693 delta_exec = curr->sum_exec_runtime - curr->prev_sum_exec_runtime;
694 if (delta_exec > ideal_runtime)
695 granularity = 0;
bf0f6f24
IM
696
697 /*
698 * Take scheduling granularity into account - do not
699 * preempt the current task unless the best task has
700 * a larger than sched_granularity fairness advantage:
11697830
PZ
701 *
702 * scale granularity as key space is in fair_clock.
bf0f6f24 703 */
4a55b450 704 if (__delta > niced_granularity(curr, granularity))
bf0f6f24
IM
705 resched_task(rq_of(cfs_rq)->curr);
706}
707
708static inline void
8494f412 709set_next_entity(struct cfs_rq *cfs_rq, struct sched_entity *se)
bf0f6f24
IM
710{
711 /*
712 * Any task has to be enqueued before it get to execute on
713 * a CPU. So account for the time it spent waiting on the
714 * runqueue. (note, here we rely on pick_next_task() having
715 * done a put_prev_task_fair() shortly before this, which
716 * updated rq->fair_clock - used by update_stats_wait_end())
717 */
9ef0a961 718 update_stats_wait_end(cfs_rq, se);
79303e9e 719 update_stats_curr_start(cfs_rq, se);
bf0f6f24 720 set_cfs_rq_curr(cfs_rq, se);
eba1ed4b
IM
721#ifdef CONFIG_SCHEDSTATS
722 /*
723 * Track our maximum slice length, if the CPU's load is at
724 * least twice that of our own weight (i.e. dont track it
725 * when there are only lesser-weight tasks around):
726 */
727 if (rq_of(cfs_rq)->ls.load.weight >= 2*se->load.weight) {
728 se->slice_max = max(se->slice_max,
729 se->sum_exec_runtime - se->prev_sum_exec_runtime);
730 }
731#endif
4a55b450 732 se->prev_sum_exec_runtime = se->sum_exec_runtime;
bf0f6f24
IM
733}
734
9948f4b2 735static struct sched_entity *pick_next_entity(struct cfs_rq *cfs_rq)
bf0f6f24
IM
736{
737 struct sched_entity *se = __pick_next_entity(cfs_rq);
738
8494f412 739 set_next_entity(cfs_rq, se);
bf0f6f24
IM
740
741 return se;
742}
743
ab6cde26 744static void put_prev_entity(struct cfs_rq *cfs_rq, struct sched_entity *prev)
bf0f6f24
IM
745{
746 /*
747 * If still on the runqueue then deactivate_task()
748 * was not called and update_curr() has to be done:
749 */
750 if (prev->on_rq)
b7cc0896 751 update_curr(cfs_rq);
bf0f6f24 752
c7e9b5b2 753 update_stats_curr_end(cfs_rq, prev);
bf0f6f24
IM
754
755 if (prev->on_rq)
5870db5b 756 update_stats_wait_start(cfs_rq, prev);
bf0f6f24
IM
757 set_cfs_rq_curr(cfs_rq, NULL);
758}
759
760static void entity_tick(struct cfs_rq *cfs_rq, struct sched_entity *curr)
761{
bf0f6f24 762 struct sched_entity *next;
c1b3da3e 763
bf0f6f24
IM
764 /*
765 * Dequeue and enqueue the task to update its
766 * position within the tree:
767 */
525c2716 768 dequeue_entity(cfs_rq, curr, 0);
668031ca 769 enqueue_entity(cfs_rq, curr, 0);
bf0f6f24
IM
770
771 /*
772 * Reschedule if another task tops the current one.
773 */
774 next = __pick_next_entity(cfs_rq);
775 if (next == curr)
776 return;
777
11697830
PZ
778 __check_preempt_curr_fair(cfs_rq, next, curr,
779 sched_granularity(cfs_rq));
bf0f6f24
IM
780}
781
782/**************************************************
783 * CFS operations on tasks:
784 */
785
786#ifdef CONFIG_FAIR_GROUP_SCHED
787
788/* Walk up scheduling entities hierarchy */
789#define for_each_sched_entity(se) \
790 for (; se; se = se->parent)
791
792static inline struct cfs_rq *task_cfs_rq(struct task_struct *p)
793{
794 return p->se.cfs_rq;
795}
796
797/* runqueue on which this entity is (to be) queued */
798static inline struct cfs_rq *cfs_rq_of(struct sched_entity *se)
799{
800 return se->cfs_rq;
801}
802
803/* runqueue "owned" by this group */
804static inline struct cfs_rq *group_cfs_rq(struct sched_entity *grp)
805{
806 return grp->my_q;
807}
808
809/* Given a group's cfs_rq on one cpu, return its corresponding cfs_rq on
810 * another cpu ('this_cpu')
811 */
812static inline struct cfs_rq *cpu_cfs_rq(struct cfs_rq *cfs_rq, int this_cpu)
813{
814 /* A later patch will take group into account */
815 return &cpu_rq(this_cpu)->cfs;
816}
817
818/* Iterate thr' all leaf cfs_rq's on a runqueue */
819#define for_each_leaf_cfs_rq(rq, cfs_rq) \
820 list_for_each_entry(cfs_rq, &rq->leaf_cfs_rq_list, leaf_cfs_rq_list)
821
822/* Do the two (enqueued) tasks belong to the same group ? */
823static inline int is_same_group(struct task_struct *curr, struct task_struct *p)
824{
825 if (curr->se.cfs_rq == p->se.cfs_rq)
826 return 1;
827
828 return 0;
829}
830
831#else /* CONFIG_FAIR_GROUP_SCHED */
832
833#define for_each_sched_entity(se) \
834 for (; se; se = NULL)
835
836static inline struct cfs_rq *task_cfs_rq(struct task_struct *p)
837{
838 return &task_rq(p)->cfs;
839}
840
841static inline struct cfs_rq *cfs_rq_of(struct sched_entity *se)
842{
843 struct task_struct *p = task_of(se);
844 struct rq *rq = task_rq(p);
845
846 return &rq->cfs;
847}
848
849/* runqueue "owned" by this group */
850static inline struct cfs_rq *group_cfs_rq(struct sched_entity *grp)
851{
852 return NULL;
853}
854
855static inline struct cfs_rq *cpu_cfs_rq(struct cfs_rq *cfs_rq, int this_cpu)
856{
857 return &cpu_rq(this_cpu)->cfs;
858}
859
860#define for_each_leaf_cfs_rq(rq, cfs_rq) \
861 for (cfs_rq = &rq->cfs; cfs_rq; cfs_rq = NULL)
862
863static inline int is_same_group(struct task_struct *curr, struct task_struct *p)
864{
865 return 1;
866}
867
868#endif /* CONFIG_FAIR_GROUP_SCHED */
869
870/*
871 * The enqueue_task method is called before nr_running is
872 * increased. Here we update the fair scheduling stats and
873 * then put the task into the rbtree:
874 */
fd390f6a 875static void enqueue_task_fair(struct rq *rq, struct task_struct *p, int wakeup)
bf0f6f24
IM
876{
877 struct cfs_rq *cfs_rq;
878 struct sched_entity *se = &p->se;
879
880 for_each_sched_entity(se) {
881 if (se->on_rq)
882 break;
883 cfs_rq = cfs_rq_of(se);
668031ca 884 enqueue_entity(cfs_rq, se, wakeup);
bf0f6f24
IM
885 }
886}
887
888/*
889 * The dequeue_task method is called before nr_running is
890 * decreased. We remove the task from the rbtree and
891 * update the fair scheduling stats:
892 */
f02231e5 893static void dequeue_task_fair(struct rq *rq, struct task_struct *p, int sleep)
bf0f6f24
IM
894{
895 struct cfs_rq *cfs_rq;
896 struct sched_entity *se = &p->se;
897
898 for_each_sched_entity(se) {
899 cfs_rq = cfs_rq_of(se);
525c2716 900 dequeue_entity(cfs_rq, se, sleep);
bf0f6f24
IM
901 /* Don't dequeue parent if it has other entities besides us */
902 if (cfs_rq->load.weight)
903 break;
904 }
905}
906
907/*
1799e35d
IM
908 * sched_yield() support is very simple - we dequeue and enqueue.
909 *
910 * If compat_yield is turned on then we requeue to the end of the tree.
bf0f6f24
IM
911 */
912static void yield_task_fair(struct rq *rq, struct task_struct *p)
913{
914 struct cfs_rq *cfs_rq = task_cfs_rq(p);
1799e35d
IM
915 struct rb_node **link = &cfs_rq->tasks_timeline.rb_node;
916 struct sched_entity *rightmost, *se = &p->se;
917 struct rb_node *parent;
bf0f6f24
IM
918
919 /*
1799e35d
IM
920 * Are we the only task in the tree?
921 */
922 if (unlikely(cfs_rq->nr_running == 1))
923 return;
924
925 if (likely(!sysctl_sched_compat_yield)) {
926 __update_rq_clock(rq);
927 /*
928 * Dequeue and enqueue the task to update its
929 * position within the tree:
930 */
931 dequeue_entity(cfs_rq, &p->se, 0);
932 enqueue_entity(cfs_rq, &p->se, 0);
933
934 return;
935 }
936 /*
937 * Find the rightmost entry in the rbtree:
bf0f6f24 938 */
1799e35d
IM
939 do {
940 parent = *link;
941 link = &parent->rb_right;
942 } while (*link);
943
944 rightmost = rb_entry(parent, struct sched_entity, run_node);
945 /*
946 * Already in the rightmost position?
947 */
948 if (unlikely(rightmost == se))
949 return;
950
951 /*
952 * Minimally necessary key value to be last in the tree:
953 */
954 se->fair_key = rightmost->fair_key + 1;
955
956 if (cfs_rq->rb_leftmost == &se->run_node)
957 cfs_rq->rb_leftmost = rb_next(&se->run_node);
958 /*
959 * Relink the task to the rightmost position:
960 */
961 rb_erase(&se->run_node, &cfs_rq->tasks_timeline);
962 rb_link_node(&se->run_node, parent, link);
963 rb_insert_color(&se->run_node, &cfs_rq->tasks_timeline);
bf0f6f24
IM
964}
965
966/*
967 * Preempt the current task with a newly woken task if needed:
968 */
969static void check_preempt_curr_fair(struct rq *rq, struct task_struct *p)
970{
971 struct task_struct *curr = rq->curr;
972 struct cfs_rq *cfs_rq = task_cfs_rq(curr);
973 unsigned long gran;
974
975 if (unlikely(rt_prio(p->prio))) {
a8e504d2 976 update_rq_clock(rq);
b7cc0896 977 update_curr(cfs_rq);
bf0f6f24
IM
978 resched_task(curr);
979 return;
980 }
981
982 gran = sysctl_sched_wakeup_granularity;
983 /*
984 * Batch tasks prefer throughput over latency:
985 */
986 if (unlikely(p->policy == SCHED_BATCH))
987 gran = sysctl_sched_batch_wakeup_granularity;
988
989 if (is_same_group(curr, p))
990 __check_preempt_curr_fair(cfs_rq, &p->se, &curr->se, gran);
991}
992
fb8d4724 993static struct task_struct *pick_next_task_fair(struct rq *rq)
bf0f6f24
IM
994{
995 struct cfs_rq *cfs_rq = &rq->cfs;
996 struct sched_entity *se;
997
998 if (unlikely(!cfs_rq->nr_running))
999 return NULL;
1000
1001 do {
9948f4b2 1002 se = pick_next_entity(cfs_rq);
bf0f6f24
IM
1003 cfs_rq = group_cfs_rq(se);
1004 } while (cfs_rq);
1005
1006 return task_of(se);
1007}
1008
1009/*
1010 * Account for a descheduled task:
1011 */
31ee529c 1012static void put_prev_task_fair(struct rq *rq, struct task_struct *prev)
bf0f6f24
IM
1013{
1014 struct sched_entity *se = &prev->se;
1015 struct cfs_rq *cfs_rq;
1016
1017 for_each_sched_entity(se) {
1018 cfs_rq = cfs_rq_of(se);
ab6cde26 1019 put_prev_entity(cfs_rq, se);
bf0f6f24
IM
1020 }
1021}
1022
1023/**************************************************
1024 * Fair scheduling class load-balancing methods:
1025 */
1026
1027/*
1028 * Load-balancing iterator. Note: while the runqueue stays locked
1029 * during the whole iteration, the current task might be
1030 * dequeued so the iterator has to be dequeue-safe. Here we
1031 * achieve that by always pre-iterating before returning
1032 * the current task:
1033 */
1034static inline struct task_struct *
1035__load_balance_iterator(struct cfs_rq *cfs_rq, struct rb_node *curr)
1036{
1037 struct task_struct *p;
1038
1039 if (!curr)
1040 return NULL;
1041
1042 p = rb_entry(curr, struct task_struct, se.run_node);
1043 cfs_rq->rb_load_balance_curr = rb_next(curr);
1044
1045 return p;
1046}
1047
1048static struct task_struct *load_balance_start_fair(void *arg)
1049{
1050 struct cfs_rq *cfs_rq = arg;
1051
1052 return __load_balance_iterator(cfs_rq, first_fair(cfs_rq));
1053}
1054
1055static struct task_struct *load_balance_next_fair(void *arg)
1056{
1057 struct cfs_rq *cfs_rq = arg;
1058
1059 return __load_balance_iterator(cfs_rq, cfs_rq->rb_load_balance_curr);
1060}
1061
a4ac01c3 1062#ifdef CONFIG_FAIR_GROUP_SCHED
bf0f6f24
IM
1063static int cfs_rq_best_prio(struct cfs_rq *cfs_rq)
1064{
1065 struct sched_entity *curr;
1066 struct task_struct *p;
1067
1068 if (!cfs_rq->nr_running)
1069 return MAX_PRIO;
1070
1071 curr = __pick_next_entity(cfs_rq);
1072 p = task_of(curr);
1073
1074 return p->prio;
1075}
a4ac01c3 1076#endif
bf0f6f24 1077
43010659 1078static unsigned long
bf0f6f24 1079load_balance_fair(struct rq *this_rq, int this_cpu, struct rq *busiest,
a4ac01c3
PW
1080 unsigned long max_nr_move, unsigned long max_load_move,
1081 struct sched_domain *sd, enum cpu_idle_type idle,
1082 int *all_pinned, int *this_best_prio)
bf0f6f24
IM
1083{
1084 struct cfs_rq *busy_cfs_rq;
1085 unsigned long load_moved, total_nr_moved = 0, nr_moved;
1086 long rem_load_move = max_load_move;
1087 struct rq_iterator cfs_rq_iterator;
1088
1089 cfs_rq_iterator.start = load_balance_start_fair;
1090 cfs_rq_iterator.next = load_balance_next_fair;
1091
1092 for_each_leaf_cfs_rq(busiest, busy_cfs_rq) {
a4ac01c3 1093#ifdef CONFIG_FAIR_GROUP_SCHED
bf0f6f24 1094 struct cfs_rq *this_cfs_rq;
e56f31aa 1095 long imbalance;
bf0f6f24 1096 unsigned long maxload;
bf0f6f24
IM
1097
1098 this_cfs_rq = cpu_cfs_rq(busy_cfs_rq, this_cpu);
1099
e56f31aa 1100 imbalance = busy_cfs_rq->load.weight - this_cfs_rq->load.weight;
bf0f6f24
IM
1101 /* Don't pull if this_cfs_rq has more load than busy_cfs_rq */
1102 if (imbalance <= 0)
1103 continue;
1104
1105 /* Don't pull more than imbalance/2 */
1106 imbalance /= 2;
1107 maxload = min(rem_load_move, imbalance);
1108
a4ac01c3
PW
1109 *this_best_prio = cfs_rq_best_prio(this_cfs_rq);
1110#else
e56f31aa 1111# define maxload rem_load_move
a4ac01c3 1112#endif
bf0f6f24
IM
1113 /* pass busy_cfs_rq argument into
1114 * load_balance_[start|next]_fair iterators
1115 */
1116 cfs_rq_iterator.arg = busy_cfs_rq;
1117 nr_moved = balance_tasks(this_rq, this_cpu, busiest,
1118 max_nr_move, maxload, sd, idle, all_pinned,
a4ac01c3 1119 &load_moved, this_best_prio, &cfs_rq_iterator);
bf0f6f24
IM
1120
1121 total_nr_moved += nr_moved;
1122 max_nr_move -= nr_moved;
1123 rem_load_move -= load_moved;
1124
1125 if (max_nr_move <= 0 || rem_load_move <= 0)
1126 break;
1127 }
1128
43010659 1129 return max_load_move - rem_load_move;
bf0f6f24
IM
1130}
1131
1132/*
1133 * scheduler tick hitting a task of our scheduling class:
1134 */
1135static void task_tick_fair(struct rq *rq, struct task_struct *curr)
1136{
1137 struct cfs_rq *cfs_rq;
1138 struct sched_entity *se = &curr->se;
1139
1140 for_each_sched_entity(se) {
1141 cfs_rq = cfs_rq_of(se);
1142 entity_tick(cfs_rq, se);
1143 }
1144}
1145
1146/*
1147 * Share the fairness runtime between parent and child, thus the
1148 * total amount of pressure for CPU stays equal - new tasks
1149 * get a chance to run but frequent forkers are not allowed to
1150 * monopolize the CPU. Note: the parent runqueue is locked,
1151 * the child is not running yet.
1152 */
ee0827d8 1153static void task_new_fair(struct rq *rq, struct task_struct *p)
bf0f6f24
IM
1154{
1155 struct cfs_rq *cfs_rq = task_cfs_rq(p);
7109c442 1156 struct sched_entity *se = &p->se, *curr = cfs_rq_curr(cfs_rq);
bf0f6f24
IM
1157
1158 sched_info_queued(p);
1159
7109c442 1160 update_curr(cfs_rq);
d2417e5a 1161 update_stats_enqueue(cfs_rq, se);
bf0f6f24
IM
1162 /*
1163 * Child runs first: we let it run before the parent
1164 * until it reschedules once. We set up the key so that
1165 * it will preempt the parent:
1166 */
9f508f82 1167 se->fair_key = curr->fair_key -
7109c442 1168 niced_granularity(curr, sched_granularity(cfs_rq)) - 1;
bf0f6f24
IM
1169 /*
1170 * The first wait is dominated by the child-runs-first logic,
1171 * so do not credit it with that waiting time yet:
1172 */
1173 if (sysctl_sched_features & SCHED_FEAT_SKIP_INITIAL)
9f508f82 1174 se->wait_start_fair = 0;
bf0f6f24
IM
1175
1176 /*
1177 * The statistical average of wait_runtime is about
1178 * -granularity/2, so initialize the task with that:
1179 */
a206c072 1180 if (sysctl_sched_features & SCHED_FEAT_START_DEBIT)
9f508f82 1181 se->wait_runtime = -(sched_granularity(cfs_rq) / 2);
bf0f6f24
IM
1182
1183 __enqueue_entity(cfs_rq, se);
bb61c210 1184 resched_task(rq->curr);
bf0f6f24
IM
1185}
1186
1187#ifdef CONFIG_FAIR_GROUP_SCHED
1188/* Account for a task changing its policy or group.
1189 *
1190 * This routine is mostly called to set cfs_rq->curr field when a task
1191 * migrates between groups/classes.
1192 */
1193static void set_curr_task_fair(struct rq *rq)
1194{
7c6c16f3 1195 struct sched_entity *se = &rq->curr->se;
a8e504d2 1196
c3b64f1e
IM
1197 for_each_sched_entity(se)
1198 set_next_entity(cfs_rq_of(se), se);
bf0f6f24
IM
1199}
1200#else
1201static void set_curr_task_fair(struct rq *rq)
1202{
1203}
1204#endif
1205
1206/*
1207 * All the scheduling class methods:
1208 */
1209struct sched_class fair_sched_class __read_mostly = {
1210 .enqueue_task = enqueue_task_fair,
1211 .dequeue_task = dequeue_task_fair,
1212 .yield_task = yield_task_fair,
1213
1214 .check_preempt_curr = check_preempt_curr_fair,
1215
1216 .pick_next_task = pick_next_task_fair,
1217 .put_prev_task = put_prev_task_fair,
1218
1219 .load_balance = load_balance_fair,
1220
1221 .set_curr_task = set_curr_task_fair,
1222 .task_tick = task_tick_fair,
1223 .task_new = task_new_fair,
1224};
1225
1226#ifdef CONFIG_SCHED_DEBUG
5cef9eca 1227static void print_cfs_stats(struct seq_file *m, int cpu)
bf0f6f24 1228{
bf0f6f24
IM
1229 struct cfs_rq *cfs_rq;
1230
c3b64f1e 1231 for_each_leaf_cfs_rq(cpu_rq(cpu), cfs_rq)
5cef9eca 1232 print_cfs_rq(m, cpu, cfs_rq);
bf0f6f24
IM
1233}
1234#endif