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