sched: simplify bonus calculation #1
[pandora-kernel.git] / kernel / sched_fair.c
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:
22  * (default: 10 msec, units: nanoseconds)
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  */
34 unsigned int sysctl_sched_granularity __read_mostly = 10000000UL;
35
36 /*
37  * SCHED_BATCH wake-up granularity.
38  * (default: 25 msec, units: nanoseconds)
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  */
44 unsigned int sysctl_sched_batch_wakeup_granularity __read_mostly = 25000000UL;
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  */
54 unsigned int sysctl_sched_wakeup_granularity __read_mostly = 1000000UL;
55
56 unsigned int sysctl_sched_stat_granularity __read_mostly;
57
58 /*
59  * Initialized in sched_init_granularity() [to 5 times the base granularity]:
60  */
61 unsigned int sysctl_sched_runtime_limit __read_mostly;
62
63 /*
64  * Debugging: various feature bits
65  */
66 enum {
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
75 unsigned int sysctl_sched_features __read_mostly =
76                 SCHED_FEAT_FAIR_SLEEPERS        *1 |
77                 SCHED_FEAT_SLEEPER_AVG          *0 |
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
83 extern 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 */
92 static 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 */
98 static 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
106 static inline void
107 set_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
114 static inline struct rq *rq_of(struct cfs_rq *cfs_rq)
115 {
116         return container_of(cfs_rq, struct rq, cfs);
117 }
118
119 static 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
131 static inline void
132 set_cfs_rq_curr(struct cfs_rq *cfs_rq, struct sched_entity *se) { }
133
134 #endif  /* CONFIG_FAIR_GROUP_SCHED */
135
136 static 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  */
149 static 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
190 static 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
201 static inline struct rb_node *first_fair(struct cfs_rq *cfs_rq)
202 {
203         return cfs_rq->rb_leftmost;
204 }
205
206 static 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  */
219 static long
220 niced_granularity(struct sched_entity *curr, unsigned long granularity)
221 {
222         u64 tmp;
223
224         if (likely(curr->load.weight == NICE_0_LOAD))
225                 return granularity;
226         /*
227          * Positive nice levels get the same granularity as nice-0:
228          */
229         if (likely(curr->load.weight < NICE_0_LOAD)) {
230                 tmp = curr->load.weight * (u64)granularity;
231                 return (long) (tmp >> NICE_0_SHIFT);
232         }
233         /*
234          * Negative nice level tasks get linearly finer
235          * granularity:
236          */
237         tmp = curr->load.inv_weight * (u64)granularity;
238
239         /*
240          * It will always fit into 'long':
241          */
242         return (long) (tmp >> WMULT_SHIFT);
243 }
244
245 static inline void
246 limit_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
266 static 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
274 static void
275 add_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  */
286 static inline void
287 __update_curr(struct cfs_rq *cfs_rq, struct sched_entity *curr)
288 {
289         unsigned long delta, delta_exec, delta_fair, delta_mine;
290         struct load_weight *lw = &cfs_rq->load;
291         unsigned long load = lw->weight;
292
293         delta_exec = curr->delta_exec;
294         schedstat_set(curr->exec_max, max((u64)delta_exec, curr->exec_max));
295
296         curr->sum_exec_runtime += delta_exec;
297         cfs_rq->exec_clock += delta_exec;
298
299         if (unlikely(!load))
300                 return;
301
302         delta_fair = calc_delta_fair(delta_exec, lw);
303         delta_mine = calc_delta_mine(delta_exec, curr->load.weight, lw);
304
305         if (cfs_rq->sleeper_bonus > sysctl_sched_granularity) {
306                 delta = calc_delta_mine(delta_exec, curr->load.weight, lw);
307                 delta = min((u64)delta, cfs_rq->sleeper_bonus);
308                 delta = min(delta, (unsigned long)(
309                         (long)sysctl_sched_runtime_limit - curr->wait_runtime));
310                 cfs_rq->sleeper_bonus -= delta;
311                 delta_mine -= delta;
312         }
313
314         cfs_rq->fair_clock += delta_fair;
315         /*
316          * We executed delta_exec amount of time on the CPU,
317          * but we were only entitled to delta_mine amount of
318          * time during that period (if nr_running == 1 then
319          * the two values are equal)
320          * [Note: delta_mine - delta_exec is negative]:
321          */
322         add_wait_runtime(cfs_rq, curr, delta_mine - delta_exec);
323 }
324
325 static void update_curr(struct cfs_rq *cfs_rq)
326 {
327         struct sched_entity *curr = cfs_rq_curr(cfs_rq);
328         unsigned long delta_exec;
329
330         if (unlikely(!curr))
331                 return;
332
333         /*
334          * Get the amount of time the current task was running
335          * since the last time we changed load (this cannot
336          * overflow on 32 bits):
337          */
338         delta_exec = (unsigned long)(rq_of(cfs_rq)->clock - curr->exec_start);
339
340         curr->delta_exec += delta_exec;
341
342         if (unlikely(curr->delta_exec > sysctl_sched_stat_granularity)) {
343                 __update_curr(cfs_rq, curr);
344                 curr->delta_exec = 0;
345         }
346         curr->exec_start = rq_of(cfs_rq)->clock;
347 }
348
349 static inline void
350 update_stats_wait_start(struct cfs_rq *cfs_rq, struct sched_entity *se)
351 {
352         se->wait_start_fair = cfs_rq->fair_clock;
353         schedstat_set(se->wait_start, rq_of(cfs_rq)->clock);
354 }
355
356 /*
357  * We calculate fair deltas here, so protect against the random effects
358  * of a multiplication overflow by capping it to the runtime limit:
359  */
360 #if BITS_PER_LONG == 32
361 static inline unsigned long
362 calc_weighted(unsigned long delta, unsigned long weight, int shift)
363 {
364         u64 tmp = (u64)delta * weight >> shift;
365
366         if (unlikely(tmp > sysctl_sched_runtime_limit*2))
367                 return sysctl_sched_runtime_limit*2;
368         return tmp;
369 }
370 #else
371 static inline unsigned long
372 calc_weighted(unsigned long delta, unsigned long weight, int shift)
373 {
374         return delta * weight >> shift;
375 }
376 #endif
377
378 /*
379  * Task is being enqueued - update stats:
380  */
381 static void update_stats_enqueue(struct cfs_rq *cfs_rq, struct sched_entity *se)
382 {
383         s64 key;
384
385         /*
386          * Are we enqueueing a waiting task? (for current tasks
387          * a dequeue/enqueue event is a NOP)
388          */
389         if (se != cfs_rq_curr(cfs_rq))
390                 update_stats_wait_start(cfs_rq, se);
391         /*
392          * Update the key:
393          */
394         key = cfs_rq->fair_clock;
395
396         /*
397          * Optimize the common nice 0 case:
398          */
399         if (likely(se->load.weight == NICE_0_LOAD)) {
400                 key -= se->wait_runtime;
401         } else {
402                 u64 tmp;
403
404                 if (se->wait_runtime < 0) {
405                         tmp = -se->wait_runtime;
406                         key += (tmp * se->load.inv_weight) >>
407                                         (WMULT_SHIFT - NICE_0_SHIFT);
408                 } else {
409                         tmp = se->wait_runtime;
410                         key -= (tmp * se->load.inv_weight) >>
411                                         (WMULT_SHIFT - NICE_0_SHIFT);
412                 }
413         }
414
415         se->fair_key = key;
416 }
417
418 /*
419  * Note: must be called with a freshly updated rq->fair_clock.
420  */
421 static inline void
422 __update_stats_wait_end(struct cfs_rq *cfs_rq, struct sched_entity *se)
423 {
424         unsigned long delta_fair = se->delta_fair_run;
425
426         schedstat_set(se->wait_max, max(se->wait_max,
427                         rq_of(cfs_rq)->clock - se->wait_start));
428
429         if (unlikely(se->load.weight != NICE_0_LOAD))
430                 delta_fair = calc_weighted(delta_fair, se->load.weight,
431                                                         NICE_0_SHIFT);
432
433         add_wait_runtime(cfs_rq, se, delta_fair);
434 }
435
436 static void
437 update_stats_wait_end(struct cfs_rq *cfs_rq, struct sched_entity *se)
438 {
439         unsigned long delta_fair;
440
441         delta_fair = (unsigned long)min((u64)(2*sysctl_sched_runtime_limit),
442                         (u64)(cfs_rq->fair_clock - se->wait_start_fair));
443
444         se->delta_fair_run += delta_fair;
445         if (unlikely(abs(se->delta_fair_run) >=
446                                 sysctl_sched_stat_granularity)) {
447                 __update_stats_wait_end(cfs_rq, se);
448                 se->delta_fair_run = 0;
449         }
450
451         se->wait_start_fair = 0;
452         schedstat_set(se->wait_start, 0);
453 }
454
455 static inline void
456 update_stats_dequeue(struct cfs_rq *cfs_rq, struct sched_entity *se)
457 {
458         update_curr(cfs_rq);
459         /*
460          * Mark the end of the wait period if dequeueing a
461          * waiting task:
462          */
463         if (se != cfs_rq_curr(cfs_rq))
464                 update_stats_wait_end(cfs_rq, se);
465 }
466
467 /*
468  * We are picking a new current task - update its stats:
469  */
470 static inline void
471 update_stats_curr_start(struct cfs_rq *cfs_rq, struct sched_entity *se)
472 {
473         /*
474          * We are starting a new run period:
475          */
476         se->exec_start = rq_of(cfs_rq)->clock;
477 }
478
479 /*
480  * We are descheduling a task - update its stats:
481  */
482 static inline void
483 update_stats_curr_end(struct cfs_rq *cfs_rq, struct sched_entity *se)
484 {
485         se->exec_start = 0;
486 }
487
488 /**************************************************
489  * Scheduling class queueing methods:
490  */
491
492 static void __enqueue_sleeper(struct cfs_rq *cfs_rq, struct sched_entity *se)
493 {
494         unsigned long load = cfs_rq->load.weight, delta_fair;
495         long prev_runtime;
496
497         /*
498          * Do not boost sleepers if there's too much bonus 'in flight'
499          * already:
500          */
501         if (unlikely(cfs_rq->sleeper_bonus > sysctl_sched_runtime_limit))
502                 return;
503
504         if (sysctl_sched_features & SCHED_FEAT_SLEEPER_LOAD_AVG)
505                 load = rq_of(cfs_rq)->cpu_load[2];
506
507         delta_fair = se->delta_fair_sleep;
508
509         /*
510          * Fix up delta_fair with the effect of us running
511          * during the whole sleep period:
512          */
513         if (sysctl_sched_features & SCHED_FEAT_SLEEPER_AVG)
514                 delta_fair = div64_likely32((u64)delta_fair * load,
515                                                 load + se->load.weight);
516
517         if (unlikely(se->load.weight != NICE_0_LOAD))
518                 delta_fair = calc_weighted(delta_fair, se->load.weight,
519                                                         NICE_0_SHIFT);
520
521         prev_runtime = se->wait_runtime;
522         __add_wait_runtime(cfs_rq, se, delta_fair);
523         schedstat_add(cfs_rq, wait_runtime, se->wait_runtime);
524         delta_fair = se->wait_runtime - prev_runtime;
525
526         /*
527          * Track the amount of bonus we've given to sleepers:
528          */
529         cfs_rq->sleeper_bonus += delta_fair;
530 }
531
532 static void enqueue_sleeper(struct cfs_rq *cfs_rq, struct sched_entity *se)
533 {
534         struct task_struct *tsk = task_of(se);
535         unsigned long delta_fair;
536
537         if ((entity_is_task(se) && tsk->policy == SCHED_BATCH) ||
538                          !(sysctl_sched_features & SCHED_FEAT_FAIR_SLEEPERS))
539                 return;
540
541         delta_fair = (unsigned long)min((u64)(2*sysctl_sched_runtime_limit),
542                 (u64)(cfs_rq->fair_clock - se->sleep_start_fair));
543
544         se->delta_fair_sleep += delta_fair;
545         if (unlikely(abs(se->delta_fair_sleep) >=
546                                 sysctl_sched_stat_granularity)) {
547                 __enqueue_sleeper(cfs_rq, se);
548                 se->delta_fair_sleep = 0;
549         }
550
551         se->sleep_start_fair = 0;
552
553 #ifdef CONFIG_SCHEDSTATS
554         if (se->sleep_start) {
555                 u64 delta = rq_of(cfs_rq)->clock - se->sleep_start;
556
557                 if ((s64)delta < 0)
558                         delta = 0;
559
560                 if (unlikely(delta > se->sleep_max))
561                         se->sleep_max = delta;
562
563                 se->sleep_start = 0;
564                 se->sum_sleep_runtime += delta;
565         }
566         if (se->block_start) {
567                 u64 delta = rq_of(cfs_rq)->clock - se->block_start;
568
569                 if ((s64)delta < 0)
570                         delta = 0;
571
572                 if (unlikely(delta > se->block_max))
573                         se->block_max = delta;
574
575                 se->block_start = 0;
576                 se->sum_sleep_runtime += delta;
577         }
578 #endif
579 }
580
581 static void
582 enqueue_entity(struct cfs_rq *cfs_rq, struct sched_entity *se, int wakeup)
583 {
584         /*
585          * Update the fair clock.
586          */
587         update_curr(cfs_rq);
588
589         if (wakeup)
590                 enqueue_sleeper(cfs_rq, se);
591
592         update_stats_enqueue(cfs_rq, se);
593         __enqueue_entity(cfs_rq, se);
594 }
595
596 static void
597 dequeue_entity(struct cfs_rq *cfs_rq, struct sched_entity *se, int sleep)
598 {
599         update_stats_dequeue(cfs_rq, se);
600         if (sleep) {
601                 se->sleep_start_fair = cfs_rq->fair_clock;
602 #ifdef CONFIG_SCHEDSTATS
603                 if (entity_is_task(se)) {
604                         struct task_struct *tsk = task_of(se);
605
606                         if (tsk->state & TASK_INTERRUPTIBLE)
607                                 se->sleep_start = rq_of(cfs_rq)->clock;
608                         if (tsk->state & TASK_UNINTERRUPTIBLE)
609                                 se->block_start = rq_of(cfs_rq)->clock;
610                 }
611                 cfs_rq->wait_runtime -= se->wait_runtime;
612 #endif
613         }
614         __dequeue_entity(cfs_rq, se);
615 }
616
617 /*
618  * Preempt the current task with a newly woken task if needed:
619  */
620 static void
621 __check_preempt_curr_fair(struct cfs_rq *cfs_rq, struct sched_entity *se,
622                           struct sched_entity *curr, unsigned long granularity)
623 {
624         s64 __delta = curr->fair_key - se->fair_key;
625
626         /*
627          * Take scheduling granularity into account - do not
628          * preempt the current task unless the best task has
629          * a larger than sched_granularity fairness advantage:
630          */
631         if (__delta > niced_granularity(curr, granularity))
632                 resched_task(rq_of(cfs_rq)->curr);
633 }
634
635 static inline void
636 set_next_entity(struct cfs_rq *cfs_rq, struct sched_entity *se)
637 {
638         /*
639          * Any task has to be enqueued before it get to execute on
640          * a CPU. So account for the time it spent waiting on the
641          * runqueue. (note, here we rely on pick_next_task() having
642          * done a put_prev_task_fair() shortly before this, which
643          * updated rq->fair_clock - used by update_stats_wait_end())
644          */
645         update_stats_wait_end(cfs_rq, se);
646         update_stats_curr_start(cfs_rq, se);
647         set_cfs_rq_curr(cfs_rq, se);
648 }
649
650 static struct sched_entity *pick_next_entity(struct cfs_rq *cfs_rq)
651 {
652         struct sched_entity *se = __pick_next_entity(cfs_rq);
653
654         set_next_entity(cfs_rq, se);
655
656         return se;
657 }
658
659 static void put_prev_entity(struct cfs_rq *cfs_rq, struct sched_entity *prev)
660 {
661         /*
662          * If still on the runqueue then deactivate_task()
663          * was not called and update_curr() has to be done:
664          */
665         if (prev->on_rq)
666                 update_curr(cfs_rq);
667
668         update_stats_curr_end(cfs_rq, prev);
669
670         if (prev->on_rq)
671                 update_stats_wait_start(cfs_rq, prev);
672         set_cfs_rq_curr(cfs_rq, NULL);
673 }
674
675 static void entity_tick(struct cfs_rq *cfs_rq, struct sched_entity *curr)
676 {
677         struct sched_entity *next;
678
679         /*
680          * Dequeue and enqueue the task to update its
681          * position within the tree:
682          */
683         dequeue_entity(cfs_rq, curr, 0);
684         enqueue_entity(cfs_rq, curr, 0);
685
686         /*
687          * Reschedule if another task tops the current one.
688          */
689         next = __pick_next_entity(cfs_rq);
690         if (next == curr)
691                 return;
692
693         __check_preempt_curr_fair(cfs_rq, next, curr, sysctl_sched_granularity);
694 }
695
696 /**************************************************
697  * CFS operations on tasks:
698  */
699
700 #ifdef CONFIG_FAIR_GROUP_SCHED
701
702 /* Walk up scheduling entities hierarchy */
703 #define for_each_sched_entity(se) \
704                 for (; se; se = se->parent)
705
706 static inline struct cfs_rq *task_cfs_rq(struct task_struct *p)
707 {
708         return p->se.cfs_rq;
709 }
710
711 /* runqueue on which this entity is (to be) queued */
712 static inline struct cfs_rq *cfs_rq_of(struct sched_entity *se)
713 {
714         return se->cfs_rq;
715 }
716
717 /* runqueue "owned" by this group */
718 static inline struct cfs_rq *group_cfs_rq(struct sched_entity *grp)
719 {
720         return grp->my_q;
721 }
722
723 /* Given a group's cfs_rq on one cpu, return its corresponding cfs_rq on
724  * another cpu ('this_cpu')
725  */
726 static inline struct cfs_rq *cpu_cfs_rq(struct cfs_rq *cfs_rq, int this_cpu)
727 {
728         /* A later patch will take group into account */
729         return &cpu_rq(this_cpu)->cfs;
730 }
731
732 /* Iterate thr' all leaf cfs_rq's on a runqueue */
733 #define for_each_leaf_cfs_rq(rq, cfs_rq) \
734         list_for_each_entry(cfs_rq, &rq->leaf_cfs_rq_list, leaf_cfs_rq_list)
735
736 /* Do the two (enqueued) tasks belong to the same group ? */
737 static inline int is_same_group(struct task_struct *curr, struct task_struct *p)
738 {
739         if (curr->se.cfs_rq == p->se.cfs_rq)
740                 return 1;
741
742         return 0;
743 }
744
745 #else   /* CONFIG_FAIR_GROUP_SCHED */
746
747 #define for_each_sched_entity(se) \
748                 for (; se; se = NULL)
749
750 static inline struct cfs_rq *task_cfs_rq(struct task_struct *p)
751 {
752         return &task_rq(p)->cfs;
753 }
754
755 static inline struct cfs_rq *cfs_rq_of(struct sched_entity *se)
756 {
757         struct task_struct *p = task_of(se);
758         struct rq *rq = task_rq(p);
759
760         return &rq->cfs;
761 }
762
763 /* runqueue "owned" by this group */
764 static inline struct cfs_rq *group_cfs_rq(struct sched_entity *grp)
765 {
766         return NULL;
767 }
768
769 static inline struct cfs_rq *cpu_cfs_rq(struct cfs_rq *cfs_rq, int this_cpu)
770 {
771         return &cpu_rq(this_cpu)->cfs;
772 }
773
774 #define for_each_leaf_cfs_rq(rq, cfs_rq) \
775                 for (cfs_rq = &rq->cfs; cfs_rq; cfs_rq = NULL)
776
777 static inline int is_same_group(struct task_struct *curr, struct task_struct *p)
778 {
779         return 1;
780 }
781
782 #endif  /* CONFIG_FAIR_GROUP_SCHED */
783
784 /*
785  * The enqueue_task method is called before nr_running is
786  * increased. Here we update the fair scheduling stats and
787  * then put the task into the rbtree:
788  */
789 static void enqueue_task_fair(struct rq *rq, struct task_struct *p, int wakeup)
790 {
791         struct cfs_rq *cfs_rq;
792         struct sched_entity *se = &p->se;
793
794         for_each_sched_entity(se) {
795                 if (se->on_rq)
796                         break;
797                 cfs_rq = cfs_rq_of(se);
798                 enqueue_entity(cfs_rq, se, wakeup);
799         }
800 }
801
802 /*
803  * The dequeue_task method is called before nr_running is
804  * decreased. We remove the task from the rbtree and
805  * update the fair scheduling stats:
806  */
807 static void dequeue_task_fair(struct rq *rq, struct task_struct *p, int sleep)
808 {
809         struct cfs_rq *cfs_rq;
810         struct sched_entity *se = &p->se;
811
812         for_each_sched_entity(se) {
813                 cfs_rq = cfs_rq_of(se);
814                 dequeue_entity(cfs_rq, se, sleep);
815                 /* Don't dequeue parent if it has other entities besides us */
816                 if (cfs_rq->load.weight)
817                         break;
818         }
819 }
820
821 /*
822  * sched_yield() support is very simple - we dequeue and enqueue
823  */
824 static void yield_task_fair(struct rq *rq, struct task_struct *p)
825 {
826         struct cfs_rq *cfs_rq = task_cfs_rq(p);
827
828         __update_rq_clock(rq);
829         /*
830          * Dequeue and enqueue the task to update its
831          * position within the tree:
832          */
833         dequeue_entity(cfs_rq, &p->se, 0);
834         enqueue_entity(cfs_rq, &p->se, 0);
835 }
836
837 /*
838  * Preempt the current task with a newly woken task if needed:
839  */
840 static void check_preempt_curr_fair(struct rq *rq, struct task_struct *p)
841 {
842         struct task_struct *curr = rq->curr;
843         struct cfs_rq *cfs_rq = task_cfs_rq(curr);
844         unsigned long gran;
845
846         if (unlikely(rt_prio(p->prio))) {
847                 update_rq_clock(rq);
848                 update_curr(cfs_rq);
849                 resched_task(curr);
850                 return;
851         }
852
853         gran = sysctl_sched_wakeup_granularity;
854         /*
855          * Batch tasks prefer throughput over latency:
856          */
857         if (unlikely(p->policy == SCHED_BATCH))
858                 gran = sysctl_sched_batch_wakeup_granularity;
859
860         if (is_same_group(curr, p))
861                 __check_preempt_curr_fair(cfs_rq, &p->se, &curr->se, gran);
862 }
863
864 static struct task_struct *pick_next_task_fair(struct rq *rq)
865 {
866         struct cfs_rq *cfs_rq = &rq->cfs;
867         struct sched_entity *se;
868
869         if (unlikely(!cfs_rq->nr_running))
870                 return NULL;
871
872         do {
873                 se = pick_next_entity(cfs_rq);
874                 cfs_rq = group_cfs_rq(se);
875         } while (cfs_rq);
876
877         return task_of(se);
878 }
879
880 /*
881  * Account for a descheduled task:
882  */
883 static void put_prev_task_fair(struct rq *rq, struct task_struct *prev)
884 {
885         struct sched_entity *se = &prev->se;
886         struct cfs_rq *cfs_rq;
887
888         for_each_sched_entity(se) {
889                 cfs_rq = cfs_rq_of(se);
890                 put_prev_entity(cfs_rq, se);
891         }
892 }
893
894 /**************************************************
895  * Fair scheduling class load-balancing methods:
896  */
897
898 /*
899  * Load-balancing iterator. Note: while the runqueue stays locked
900  * during the whole iteration, the current task might be
901  * dequeued so the iterator has to be dequeue-safe. Here we
902  * achieve that by always pre-iterating before returning
903  * the current task:
904  */
905 static inline struct task_struct *
906 __load_balance_iterator(struct cfs_rq *cfs_rq, struct rb_node *curr)
907 {
908         struct task_struct *p;
909
910         if (!curr)
911                 return NULL;
912
913         p = rb_entry(curr, struct task_struct, se.run_node);
914         cfs_rq->rb_load_balance_curr = rb_next(curr);
915
916         return p;
917 }
918
919 static struct task_struct *load_balance_start_fair(void *arg)
920 {
921         struct cfs_rq *cfs_rq = arg;
922
923         return __load_balance_iterator(cfs_rq, first_fair(cfs_rq));
924 }
925
926 static struct task_struct *load_balance_next_fair(void *arg)
927 {
928         struct cfs_rq *cfs_rq = arg;
929
930         return __load_balance_iterator(cfs_rq, cfs_rq->rb_load_balance_curr);
931 }
932
933 #ifdef CONFIG_FAIR_GROUP_SCHED
934 static int cfs_rq_best_prio(struct cfs_rq *cfs_rq)
935 {
936         struct sched_entity *curr;
937         struct task_struct *p;
938
939         if (!cfs_rq->nr_running)
940                 return MAX_PRIO;
941
942         curr = __pick_next_entity(cfs_rq);
943         p = task_of(curr);
944
945         return p->prio;
946 }
947 #endif
948
949 static unsigned long
950 load_balance_fair(struct rq *this_rq, int this_cpu, struct rq *busiest,
951                   unsigned long max_nr_move, unsigned long max_load_move,
952                   struct sched_domain *sd, enum cpu_idle_type idle,
953                   int *all_pinned, int *this_best_prio)
954 {
955         struct cfs_rq *busy_cfs_rq;
956         unsigned long load_moved, total_nr_moved = 0, nr_moved;
957         long rem_load_move = max_load_move;
958         struct rq_iterator cfs_rq_iterator;
959
960         cfs_rq_iterator.start = load_balance_start_fair;
961         cfs_rq_iterator.next = load_balance_next_fair;
962
963         for_each_leaf_cfs_rq(busiest, busy_cfs_rq) {
964 #ifdef CONFIG_FAIR_GROUP_SCHED
965                 struct cfs_rq *this_cfs_rq;
966                 long imbalance;
967                 unsigned long maxload;
968
969                 this_cfs_rq = cpu_cfs_rq(busy_cfs_rq, this_cpu);
970
971                 imbalance = busy_cfs_rq->load.weight - this_cfs_rq->load.weight;
972                 /* Don't pull if this_cfs_rq has more load than busy_cfs_rq */
973                 if (imbalance <= 0)
974                         continue;
975
976                 /* Don't pull more than imbalance/2 */
977                 imbalance /= 2;
978                 maxload = min(rem_load_move, imbalance);
979
980                 *this_best_prio = cfs_rq_best_prio(this_cfs_rq);
981 #else
982 # define maxload rem_load_move
983 #endif
984                 /* pass busy_cfs_rq argument into
985                  * load_balance_[start|next]_fair iterators
986                  */
987                 cfs_rq_iterator.arg = busy_cfs_rq;
988                 nr_moved = balance_tasks(this_rq, this_cpu, busiest,
989                                 max_nr_move, maxload, sd, idle, all_pinned,
990                                 &load_moved, this_best_prio, &cfs_rq_iterator);
991
992                 total_nr_moved += nr_moved;
993                 max_nr_move -= nr_moved;
994                 rem_load_move -= load_moved;
995
996                 if (max_nr_move <= 0 || rem_load_move <= 0)
997                         break;
998         }
999
1000         return max_load_move - rem_load_move;
1001 }
1002
1003 /*
1004  * scheduler tick hitting a task of our scheduling class:
1005  */
1006 static void task_tick_fair(struct rq *rq, struct task_struct *curr)
1007 {
1008         struct cfs_rq *cfs_rq;
1009         struct sched_entity *se = &curr->se;
1010
1011         for_each_sched_entity(se) {
1012                 cfs_rq = cfs_rq_of(se);
1013                 entity_tick(cfs_rq, se);
1014         }
1015 }
1016
1017 /*
1018  * Share the fairness runtime between parent and child, thus the
1019  * total amount of pressure for CPU stays equal - new tasks
1020  * get a chance to run but frequent forkers are not allowed to
1021  * monopolize the CPU. Note: the parent runqueue is locked,
1022  * the child is not running yet.
1023  */
1024 static void task_new_fair(struct rq *rq, struct task_struct *p)
1025 {
1026         struct cfs_rq *cfs_rq = task_cfs_rq(p);
1027         struct sched_entity *se = &p->se;
1028
1029         sched_info_queued(p);
1030
1031         update_stats_enqueue(cfs_rq, se);
1032         /*
1033          * Child runs first: we let it run before the parent
1034          * until it reschedules once. We set up the key so that
1035          * it will preempt the parent:
1036          */
1037         p->se.fair_key = current->se.fair_key -
1038                 niced_granularity(&rq->curr->se, sysctl_sched_granularity) - 1;
1039         /*
1040          * The first wait is dominated by the child-runs-first logic,
1041          * so do not credit it with that waiting time yet:
1042          */
1043         if (sysctl_sched_features & SCHED_FEAT_SKIP_INITIAL)
1044                 p->se.wait_start_fair = 0;
1045
1046         /*
1047          * The statistical average of wait_runtime is about
1048          * -granularity/2, so initialize the task with that:
1049          */
1050         if (sysctl_sched_features & SCHED_FEAT_START_DEBIT)
1051                 p->se.wait_runtime = -(sysctl_sched_granularity / 2);
1052
1053         __enqueue_entity(cfs_rq, se);
1054 }
1055
1056 #ifdef CONFIG_FAIR_GROUP_SCHED
1057 /* Account for a task changing its policy or group.
1058  *
1059  * This routine is mostly called to set cfs_rq->curr field when a task
1060  * migrates between groups/classes.
1061  */
1062 static void set_curr_task_fair(struct rq *rq)
1063 {
1064         struct sched_entity *se = &rq->curr->se;
1065
1066         for_each_sched_entity(se)
1067                 set_next_entity(cfs_rq_of(se), se);
1068 }
1069 #else
1070 static void set_curr_task_fair(struct rq *rq)
1071 {
1072 }
1073 #endif
1074
1075 /*
1076  * All the scheduling class methods:
1077  */
1078 struct sched_class fair_sched_class __read_mostly = {
1079         .enqueue_task           = enqueue_task_fair,
1080         .dequeue_task           = dequeue_task_fair,
1081         .yield_task             = yield_task_fair,
1082
1083         .check_preempt_curr     = check_preempt_curr_fair,
1084
1085         .pick_next_task         = pick_next_task_fair,
1086         .put_prev_task          = put_prev_task_fair,
1087
1088         .load_balance           = load_balance_fair,
1089
1090         .set_curr_task          = set_curr_task_fair,
1091         .task_tick              = task_tick_fair,
1092         .task_new               = task_new_fair,
1093 };
1094
1095 #ifdef CONFIG_SCHED_DEBUG
1096 static void print_cfs_stats(struct seq_file *m, int cpu)
1097 {
1098         struct cfs_rq *cfs_rq;
1099
1100         for_each_leaf_cfs_rq(cpu_rq(cpu), cfs_rq)
1101                 print_cfs_rq(m, cpu, cfs_rq);
1102 }
1103 #endif