[PATCH] sched: reduce debug code
[safe/jmp/linux-2.6] / 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: 2 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 = 2000000000ULL/HZ;
35
36 /*
37  * SCHED_BATCH wake-up granularity.
38  * (default: 10 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 =
45                                                         10000000000ULL/HZ;
46
47 /*
48  * SCHED_OTHER wake-up granularity.
49  * (default: 1 msec, units: nanoseconds)
50  *
51  * This option delays the preemption effects of decoupled workloads
52  * and reduces their over-scheduling. Synchronous workloads will still
53  * have immediate wakeup/sleep latencies.
54  */
55 unsigned int sysctl_sched_wakeup_granularity __read_mostly = 1000000000ULL/HZ;
56
57 unsigned int sysctl_sched_stat_granularity __read_mostly;
58
59 /*
60  * Initialized in sched_init_granularity():
61  */
62 unsigned int sysctl_sched_runtime_limit __read_mostly;
63
64 /*
65  * Debugging: various feature bits
66  */
67 enum {
68         SCHED_FEAT_FAIR_SLEEPERS        = 1,
69         SCHED_FEAT_SLEEPER_AVG          = 2,
70         SCHED_FEAT_SLEEPER_LOAD_AVG     = 4,
71         SCHED_FEAT_PRECISE_CPU_LOAD     = 8,
72         SCHED_FEAT_START_DEBIT          = 16,
73         SCHED_FEAT_SKIP_INITIAL         = 32,
74 };
75
76 unsigned int sysctl_sched_features __read_mostly =
77                 SCHED_FEAT_FAIR_SLEEPERS        *1 |
78                 SCHED_FEAT_SLEEPER_AVG          *1 |
79                 SCHED_FEAT_SLEEPER_LOAD_AVG     *1 |
80                 SCHED_FEAT_PRECISE_CPU_LOAD     *1 |
81                 SCHED_FEAT_START_DEBIT          *1 |
82                 SCHED_FEAT_SKIP_INITIAL         *0;
83
84 extern struct sched_class fair_sched_class;
85
86 /**************************************************************
87  * CFS operations on generic schedulable entities:
88  */
89
90 #ifdef CONFIG_FAIR_GROUP_SCHED
91
92 /* cpu runqueue to which this cfs_rq is attached */
93 static inline struct rq *rq_of(struct cfs_rq *cfs_rq)
94 {
95         return cfs_rq->rq;
96 }
97
98 /* currently running entity (if any) on this cfs_rq */
99 static inline struct sched_entity *cfs_rq_curr(struct cfs_rq *cfs_rq)
100 {
101         return cfs_rq->curr;
102 }
103
104 /* An entity is a task if it doesn't "own" a runqueue */
105 #define entity_is_task(se)      (!se->my_q)
106
107 static inline void
108 set_cfs_rq_curr(struct cfs_rq *cfs_rq, struct sched_entity *se)
109 {
110         cfs_rq->curr = se;
111 }
112
113 #else   /* CONFIG_FAIR_GROUP_SCHED */
114
115 static inline struct rq *rq_of(struct cfs_rq *cfs_rq)
116 {
117         return container_of(cfs_rq, struct rq, cfs);
118 }
119
120 static inline struct sched_entity *cfs_rq_curr(struct cfs_rq *cfs_rq)
121 {
122         struct rq *rq = rq_of(cfs_rq);
123
124         if (unlikely(rq->curr->sched_class != &fair_sched_class))
125                 return NULL;
126
127         return &rq->curr->se;
128 }
129
130 #define entity_is_task(se)      1
131
132 static inline void
133 set_cfs_rq_curr(struct cfs_rq *cfs_rq, struct sched_entity *se) { }
134
135 #endif  /* CONFIG_FAIR_GROUP_SCHED */
136
137 static inline struct task_struct *task_of(struct sched_entity *se)
138 {
139         return container_of(se, struct task_struct, se);
140 }
141
142
143 /**************************************************************
144  * Scheduling class tree data structure manipulation methods:
145  */
146
147 /*
148  * Enqueue an entity into the rb-tree:
149  */
150 static inline void
151 __enqueue_entity(struct cfs_rq *cfs_rq, struct sched_entity *se)
152 {
153         struct rb_node **link = &cfs_rq->tasks_timeline.rb_node;
154         struct rb_node *parent = NULL;
155         struct sched_entity *entry;
156         s64 key = se->fair_key;
157         int leftmost = 1;
158
159         /*
160          * Find the right place in the rbtree:
161          */
162         while (*link) {
163                 parent = *link;
164                 entry = rb_entry(parent, struct sched_entity, run_node);
165                 /*
166                  * We dont care about collisions. Nodes with
167                  * the same key stay together.
168                  */
169                 if (key - entry->fair_key < 0) {
170                         link = &parent->rb_left;
171                 } else {
172                         link = &parent->rb_right;
173                         leftmost = 0;
174                 }
175         }
176
177         /*
178          * Maintain a cache of leftmost tree entries (it is frequently
179          * used):
180          */
181         if (leftmost)
182                 cfs_rq->rb_leftmost = &se->run_node;
183
184         rb_link_node(&se->run_node, parent, link);
185         rb_insert_color(&se->run_node, &cfs_rq->tasks_timeline);
186         update_load_add(&cfs_rq->load, se->load.weight);
187         cfs_rq->nr_running++;
188         se->on_rq = 1;
189 }
190
191 static inline void
192 __dequeue_entity(struct cfs_rq *cfs_rq, struct sched_entity *se)
193 {
194         if (cfs_rq->rb_leftmost == &se->run_node)
195                 cfs_rq->rb_leftmost = rb_next(&se->run_node);
196         rb_erase(&se->run_node, &cfs_rq->tasks_timeline);
197         update_load_sub(&cfs_rq->load, se->load.weight);
198         cfs_rq->nr_running--;
199         se->on_rq = 0;
200 }
201
202 static inline struct rb_node *first_fair(struct cfs_rq *cfs_rq)
203 {
204         return cfs_rq->rb_leftmost;
205 }
206
207 static struct sched_entity *__pick_next_entity(struct cfs_rq *cfs_rq)
208 {
209         return rb_entry(first_fair(cfs_rq), struct sched_entity, run_node);
210 }
211
212 /**************************************************************
213  * Scheduling class statistics methods:
214  */
215
216 /*
217  * We rescale the rescheduling granularity of tasks according to their
218  * nice level, but only linearly, not exponentially:
219  */
220 static long
221 niced_granularity(struct sched_entity *curr, unsigned long granularity)
222 {
223         u64 tmp;
224
225         /*
226          * Negative nice levels get the same granularity as nice-0:
227          */
228         if (likely(curr->load.weight >= NICE_0_LOAD))
229                 return granularity;
230         /*
231          * Positive nice level tasks get linearly finer
232          * granularity:
233          */
234         tmp = curr->load.weight * (u64)granularity;
235
236         /*
237          * It will always fit into 'long':
238          */
239         return (long) (tmp >> NICE_0_SHIFT);
240 }
241
242 static inline void
243 limit_wait_runtime(struct cfs_rq *cfs_rq, struct sched_entity *se)
244 {
245         long limit = sysctl_sched_runtime_limit;
246
247         /*
248          * Niced tasks have the same history dynamic range as
249          * non-niced tasks:
250          */
251         if (unlikely(se->wait_runtime > limit)) {
252                 se->wait_runtime = limit;
253                 schedstat_inc(se, wait_runtime_overruns);
254                 schedstat_inc(cfs_rq, wait_runtime_overruns);
255         }
256         if (unlikely(se->wait_runtime < -limit)) {
257                 se->wait_runtime = -limit;
258                 schedstat_inc(se, wait_runtime_underruns);
259                 schedstat_inc(cfs_rq, wait_runtime_underruns);
260         }
261 }
262
263 static inline void
264 __add_wait_runtime(struct cfs_rq *cfs_rq, struct sched_entity *se, long delta)
265 {
266         se->wait_runtime += delta;
267         schedstat_add(se, sum_wait_runtime, delta);
268         limit_wait_runtime(cfs_rq, se);
269 }
270
271 static void
272 add_wait_runtime(struct cfs_rq *cfs_rq, struct sched_entity *se, long delta)
273 {
274         schedstat_add(cfs_rq, wait_runtime, -se->wait_runtime);
275         __add_wait_runtime(cfs_rq, se, delta);
276         schedstat_add(cfs_rq, wait_runtime, se->wait_runtime);
277 }
278
279 /*
280  * Update the current task's runtime statistics. Skip current tasks that
281  * are not in our scheduling class.
282  */
283 static inline void
284 __update_curr(struct cfs_rq *cfs_rq, struct sched_entity *curr, u64 now)
285 {
286         unsigned long delta, delta_exec, delta_fair;
287         long delta_mine;
288         struct load_weight *lw = &cfs_rq->load;
289         unsigned long load = lw->weight;
290
291         if (unlikely(!load))
292                 return;
293
294         delta_exec = curr->delta_exec;
295         schedstat_set(curr->exec_max, max((u64)delta_exec, curr->exec_max));
296
297         curr->sum_exec_runtime += delta_exec;
298         cfs_rq->exec_clock += delta_exec;
299
300         delta_fair = calc_delta_fair(delta_exec, lw);
301         delta_mine = calc_delta_mine(delta_exec, curr->load.weight, lw);
302
303         if (cfs_rq->sleeper_bonus > sysctl_sched_stat_granularity) {
304                 delta = calc_delta_mine(cfs_rq->sleeper_bonus,
305                                         curr->load.weight, lw);
306                 if (unlikely(delta > cfs_rq->sleeper_bonus))
307                         delta = cfs_rq->sleeper_bonus;
308
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
324 static void update_curr(struct cfs_rq *cfs_rq, u64 now)
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          */
337         delta_exec = (unsigned long)(now - curr->exec_start);
338
339         curr->delta_exec += delta_exec;
340
341         if (unlikely(curr->delta_exec > sysctl_sched_stat_granularity)) {
342                 __update_curr(cfs_rq, curr, now);
343                 curr->delta_exec = 0;
344         }
345         curr->exec_start = now;
346 }
347
348 static inline void
349 update_stats_wait_start(struct cfs_rq *cfs_rq, struct sched_entity *se, u64 now)
350 {
351         se->wait_start_fair = cfs_rq->fair_clock;
352         schedstat_set(se->wait_start, now);
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
360 static inline unsigned long
361 calc_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
370 static inline unsigned long
371 calc_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  */
380 static void
381 update_stats_enqueue(struct cfs_rq *cfs_rq, struct sched_entity *se, u64 now)
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, now);
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.weight) >> NICE_0_SHIFT;
411                 }
412         }
413
414         se->fair_key = key;
415 }
416
417 /*
418  * Note: must be called with a freshly updated rq->fair_clock.
419  */
420 static inline void
421 __update_stats_wait_end(struct cfs_rq *cfs_rq, struct sched_entity *se, u64 now)
422 {
423         unsigned long delta_fair = se->delta_fair_run;
424
425         schedstat_set(se->wait_max, max(se->wait_max, now - se->wait_start));
426
427         if (unlikely(se->load.weight != NICE_0_LOAD))
428                 delta_fair = calc_weighted(delta_fair, se->load.weight,
429                                                         NICE_0_SHIFT);
430
431         add_wait_runtime(cfs_rq, se, delta_fair);
432 }
433
434 static void
435 update_stats_wait_end(struct cfs_rq *cfs_rq, struct sched_entity *se, u64 now)
436 {
437         unsigned long delta_fair;
438
439         delta_fair = (unsigned long)min((u64)(2*sysctl_sched_runtime_limit),
440                         (u64)(cfs_rq->fair_clock - se->wait_start_fair));
441
442         se->delta_fair_run += delta_fair;
443         if (unlikely(abs(se->delta_fair_run) >=
444                                 sysctl_sched_stat_granularity)) {
445                 __update_stats_wait_end(cfs_rq, se, now);
446                 se->delta_fair_run = 0;
447         }
448
449         se->wait_start_fair = 0;
450         schedstat_set(se->wait_start, 0);
451 }
452
453 static inline void
454 update_stats_dequeue(struct cfs_rq *cfs_rq, struct sched_entity *se, u64 now)
455 {
456         update_curr(cfs_rq, now);
457         /*
458          * Mark the end of the wait period if dequeueing a
459          * waiting task:
460          */
461         if (se != cfs_rq_curr(cfs_rq))
462                 update_stats_wait_end(cfs_rq, se, now);
463 }
464
465 /*
466  * We are picking a new current task - update its stats:
467  */
468 static inline void
469 update_stats_curr_start(struct cfs_rq *cfs_rq, struct sched_entity *se, u64 now)
470 {
471         /*
472          * We are starting a new run period:
473          */
474         se->exec_start = now;
475 }
476
477 /*
478  * We are descheduling a task - update its stats:
479  */
480 static inline void
481 update_stats_curr_end(struct cfs_rq *cfs_rq, struct sched_entity *se, u64 now)
482 {
483         se->exec_start = 0;
484 }
485
486 /**************************************************
487  * Scheduling class queueing methods:
488  */
489
490 static void
491 __enqueue_sleeper(struct cfs_rq *cfs_rq, struct sched_entity *se, u64 now)
492 {
493         unsigned long load = cfs_rq->load.weight, delta_fair;
494         long prev_runtime;
495
496         if (sysctl_sched_features & SCHED_FEAT_SLEEPER_LOAD_AVG)
497                 load = rq_of(cfs_rq)->cpu_load[2];
498
499         delta_fair = se->delta_fair_sleep;
500
501         /*
502          * Fix up delta_fair with the effect of us running
503          * during the whole sleep period:
504          */
505         if (sysctl_sched_features & SCHED_FEAT_SLEEPER_AVG)
506                 delta_fair = div64_likely32((u64)delta_fair * load,
507                                                 load + se->load.weight);
508
509         if (unlikely(se->load.weight != NICE_0_LOAD))
510                 delta_fair = calc_weighted(delta_fair, se->load.weight,
511                                                         NICE_0_SHIFT);
512
513         prev_runtime = se->wait_runtime;
514         __add_wait_runtime(cfs_rq, se, delta_fair);
515         delta_fair = se->wait_runtime - prev_runtime;
516
517         /*
518          * Track the amount of bonus we've given to sleepers:
519          */
520         cfs_rq->sleeper_bonus += delta_fair;
521
522         schedstat_add(cfs_rq, wait_runtime, se->wait_runtime);
523 }
524
525 static void
526 enqueue_sleeper(struct cfs_rq *cfs_rq, struct sched_entity *se, u64 now)
527 {
528         struct task_struct *tsk = task_of(se);
529         unsigned long delta_fair;
530
531         if ((entity_is_task(se) && tsk->policy == SCHED_BATCH) ||
532                          !(sysctl_sched_features & SCHED_FEAT_FAIR_SLEEPERS))
533                 return;
534
535         delta_fair = (unsigned long)min((u64)(2*sysctl_sched_runtime_limit),
536                 (u64)(cfs_rq->fair_clock - se->sleep_start_fair));
537
538         se->delta_fair_sleep += delta_fair;
539         if (unlikely(abs(se->delta_fair_sleep) >=
540                                 sysctl_sched_stat_granularity)) {
541                 __enqueue_sleeper(cfs_rq, se, now);
542                 se->delta_fair_sleep = 0;
543         }
544
545         se->sleep_start_fair = 0;
546
547 #ifdef CONFIG_SCHEDSTATS
548         if (se->sleep_start) {
549                 u64 delta = now - se->sleep_start;
550
551                 if ((s64)delta < 0)
552                         delta = 0;
553
554                 if (unlikely(delta > se->sleep_max))
555                         se->sleep_max = delta;
556
557                 se->sleep_start = 0;
558                 se->sum_sleep_runtime += delta;
559         }
560         if (se->block_start) {
561                 u64 delta = now - se->block_start;
562
563                 if ((s64)delta < 0)
564                         delta = 0;
565
566                 if (unlikely(delta > se->block_max))
567                         se->block_max = delta;
568
569                 se->block_start = 0;
570                 se->sum_sleep_runtime += delta;
571         }
572 #endif
573 }
574
575 static void
576 enqueue_entity(struct cfs_rq *cfs_rq, struct sched_entity *se,
577                int wakeup, u64 now)
578 {
579         /*
580          * Update the fair clock.
581          */
582         update_curr(cfs_rq, now);
583
584         if (wakeup)
585                 enqueue_sleeper(cfs_rq, se, now);
586
587         update_stats_enqueue(cfs_rq, se, now);
588         __enqueue_entity(cfs_rq, se);
589 }
590
591 static void
592 dequeue_entity(struct cfs_rq *cfs_rq, struct sched_entity *se,
593                int sleep, u64 now)
594 {
595         update_stats_dequeue(cfs_rq, se, now);
596         if (sleep) {
597                 se->sleep_start_fair = cfs_rq->fair_clock;
598 #ifdef CONFIG_SCHEDSTATS
599                 if (entity_is_task(se)) {
600                         struct task_struct *tsk = task_of(se);
601
602                         if (tsk->state & TASK_INTERRUPTIBLE)
603                                 se->sleep_start = now;
604                         if (tsk->state & TASK_UNINTERRUPTIBLE)
605                                 se->block_start = now;
606                 }
607                 cfs_rq->wait_runtime -= se->wait_runtime;
608 #endif
609         }
610         __dequeue_entity(cfs_rq, se);
611 }
612
613 /*
614  * Preempt the current task with a newly woken task if needed:
615  */
616 static void
617 __check_preempt_curr_fair(struct cfs_rq *cfs_rq, struct sched_entity *se,
618                           struct sched_entity *curr, unsigned long granularity)
619 {
620         s64 __delta = curr->fair_key - se->fair_key;
621
622         /*
623          * Take scheduling granularity into account - do not
624          * preempt the current task unless the best task has
625          * a larger than sched_granularity fairness advantage:
626          */
627         if (__delta > niced_granularity(curr, granularity))
628                 resched_task(rq_of(cfs_rq)->curr);
629 }
630
631 static inline void
632 set_next_entity(struct cfs_rq *cfs_rq, struct sched_entity *se, u64 now)
633 {
634         /*
635          * Any task has to be enqueued before it get to execute on
636          * a CPU. So account for the time it spent waiting on the
637          * runqueue. (note, here we rely on pick_next_task() having
638          * done a put_prev_task_fair() shortly before this, which
639          * updated rq->fair_clock - used by update_stats_wait_end())
640          */
641         update_stats_wait_end(cfs_rq, se, now);
642         update_stats_curr_start(cfs_rq, se, now);
643         set_cfs_rq_curr(cfs_rq, se);
644 }
645
646 static struct sched_entity *pick_next_entity(struct cfs_rq *cfs_rq, u64 now)
647 {
648         struct sched_entity *se = __pick_next_entity(cfs_rq);
649
650         set_next_entity(cfs_rq, se, now);
651
652         return se;
653 }
654
655 static void
656 put_prev_entity(struct cfs_rq *cfs_rq, struct sched_entity *prev, u64 now)
657 {
658         /*
659          * If still on the runqueue then deactivate_task()
660          * was not called and update_curr() has to be done:
661          */
662         if (prev->on_rq)
663                 update_curr(cfs_rq, now);
664
665         update_stats_curr_end(cfs_rq, prev, now);
666
667         if (prev->on_rq)
668                 update_stats_wait_start(cfs_rq, prev, now);
669         set_cfs_rq_curr(cfs_rq, NULL);
670 }
671
672 static void entity_tick(struct cfs_rq *cfs_rq, struct sched_entity *curr)
673 {
674         struct rq *rq = rq_of(cfs_rq);
675         struct sched_entity *next;
676         u64 now = __rq_clock(rq);
677
678         /*
679          * Dequeue and enqueue the task to update its
680          * position within the tree:
681          */
682         dequeue_entity(cfs_rq, curr, 0, now);
683         enqueue_entity(cfs_rq, curr, 0, now);
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
705 static 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 */
711 static 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 */
717 static 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  */
725 static 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 ? */
736 static 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
749 static inline struct cfs_rq *task_cfs_rq(struct task_struct *p)
750 {
751         return &task_rq(p)->cfs;
752 }
753
754 static 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 */
763 static inline struct cfs_rq *group_cfs_rq(struct sched_entity *grp)
764 {
765         return NULL;
766 }
767
768 static 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
776 static 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  */
788 static void
789 enqueue_task_fair(struct rq *rq, struct task_struct *p, int wakeup, u64 now)
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, now);
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
808 dequeue_task_fair(struct rq *rq, struct task_struct *p, int sleep, u64 now)
809 {
810         struct cfs_rq *cfs_rq;
811         struct sched_entity *se = &p->se;
812
813         for_each_sched_entity(se) {
814                 cfs_rq = cfs_rq_of(se);
815                 dequeue_entity(cfs_rq, se, sleep, now);
816                 /* Don't dequeue parent if it has other entities besides us */
817                 if (cfs_rq->load.weight)
818                         break;
819         }
820 }
821
822 /*
823  * sched_yield() support is very simple - we dequeue and enqueue
824  */
825 static void yield_task_fair(struct rq *rq, struct task_struct *p)
826 {
827         struct cfs_rq *cfs_rq = task_cfs_rq(p);
828         u64 now = __rq_clock(rq);
829
830         /*
831          * Dequeue and enqueue the task to update its
832          * position within the tree:
833          */
834         dequeue_entity(cfs_rq, &p->se, 0, now);
835         enqueue_entity(cfs_rq, &p->se, 0, now);
836 }
837
838 /*
839  * Preempt the current task with a newly woken task if needed:
840  */
841 static void check_preempt_curr_fair(struct rq *rq, struct task_struct *p)
842 {
843         struct task_struct *curr = rq->curr;
844         struct cfs_rq *cfs_rq = task_cfs_rq(curr);
845         unsigned long gran;
846
847         if (unlikely(rt_prio(p->prio))) {
848                 update_curr(cfs_rq, rq_clock(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, u64 now)
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, now);
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, u64 now)
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, now);
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 static 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 }
946
947 static int
948 load_balance_fair(struct rq *this_rq, int this_cpu, struct rq *busiest,
949                         unsigned long max_nr_move, unsigned long max_load_move,
950                         struct sched_domain *sd, enum cpu_idle_type idle,
951                         int *all_pinned, unsigned long *total_load_moved)
952 {
953         struct cfs_rq *busy_cfs_rq;
954         unsigned long load_moved, total_nr_moved = 0, nr_moved;
955         long rem_load_move = max_load_move;
956         struct rq_iterator cfs_rq_iterator;
957
958         cfs_rq_iterator.start = load_balance_start_fair;
959         cfs_rq_iterator.next = load_balance_next_fair;
960
961         for_each_leaf_cfs_rq(busiest, busy_cfs_rq) {
962                 struct cfs_rq *this_cfs_rq;
963                 long imbalance;
964                 unsigned long maxload;
965                 int this_best_prio, best_prio, best_prio_seen = 0;
966
967                 this_cfs_rq = cpu_cfs_rq(busy_cfs_rq, this_cpu);
968
969                 imbalance = busy_cfs_rq->load.weight -
970                                                  this_cfs_rq->load.weight;
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
979                 this_best_prio = cfs_rq_best_prio(this_cfs_rq);
980                 best_prio = cfs_rq_best_prio(busy_cfs_rq);
981
982                 /*
983                  * Enable handling of the case where there is more than one task
984                  * with the best priority. If the current running task is one
985                  * of those with prio==best_prio we know it won't be moved
986                  * and therefore it's safe to override the skip (based on load)
987                  * of any task we find with that prio.
988                  */
989                 if (cfs_rq_curr(busy_cfs_rq) == &busiest->curr->se)
990                         best_prio_seen = 1;
991
992                 /* pass busy_cfs_rq argument into
993                  * load_balance_[start|next]_fair iterators
994                  */
995                 cfs_rq_iterator.arg = busy_cfs_rq;
996                 nr_moved = balance_tasks(this_rq, this_cpu, busiest,
997                                 max_nr_move, maxload, sd, idle, all_pinned,
998                                 &load_moved, this_best_prio, best_prio,
999                                 best_prio_seen, &cfs_rq_iterator);
1000
1001                 total_nr_moved += nr_moved;
1002                 max_nr_move -= nr_moved;
1003                 rem_load_move -= load_moved;
1004
1005                 if (max_nr_move <= 0 || rem_load_move <= 0)
1006                         break;
1007         }
1008
1009         *total_load_moved = max_load_move - rem_load_move;
1010
1011         return total_nr_moved;
1012 }
1013
1014 /*
1015  * scheduler tick hitting a task of our scheduling class:
1016  */
1017 static void task_tick_fair(struct rq *rq, struct task_struct *curr)
1018 {
1019         struct cfs_rq *cfs_rq;
1020         struct sched_entity *se = &curr->se;
1021
1022         for_each_sched_entity(se) {
1023                 cfs_rq = cfs_rq_of(se);
1024                 entity_tick(cfs_rq, se);
1025         }
1026 }
1027
1028 /*
1029  * Share the fairness runtime between parent and child, thus the
1030  * total amount of pressure for CPU stays equal - new tasks
1031  * get a chance to run but frequent forkers are not allowed to
1032  * monopolize the CPU. Note: the parent runqueue is locked,
1033  * the child is not running yet.
1034  */
1035 static void task_new_fair(struct rq *rq, struct task_struct *p, u64 now)
1036 {
1037         struct cfs_rq *cfs_rq = task_cfs_rq(p);
1038         struct sched_entity *se = &p->se;
1039
1040         sched_info_queued(p);
1041
1042         update_stats_enqueue(cfs_rq, se, now);
1043         /*
1044          * Child runs first: we let it run before the parent
1045          * until it reschedules once. We set up the key so that
1046          * it will preempt the parent:
1047          */
1048         p->se.fair_key = current->se.fair_key -
1049                 niced_granularity(&rq->curr->se, sysctl_sched_granularity) - 1;
1050         /*
1051          * The first wait is dominated by the child-runs-first logic,
1052          * so do not credit it with that waiting time yet:
1053          */
1054         if (sysctl_sched_features & SCHED_FEAT_SKIP_INITIAL)
1055                 p->se.wait_start_fair = 0;
1056
1057         /*
1058          * The statistical average of wait_runtime is about
1059          * -granularity/2, so initialize the task with that:
1060          */
1061         if (sysctl_sched_features & SCHED_FEAT_START_DEBIT)
1062                 p->se.wait_runtime = -(sysctl_sched_granularity / 2);
1063
1064         __enqueue_entity(cfs_rq, se);
1065 }
1066
1067 #ifdef CONFIG_FAIR_GROUP_SCHED
1068 /* Account for a task changing its policy or group.
1069  *
1070  * This routine is mostly called to set cfs_rq->curr field when a task
1071  * migrates between groups/classes.
1072  */
1073 static void set_curr_task_fair(struct rq *rq)
1074 {
1075         struct task_struct *curr = rq->curr;
1076         struct sched_entity *se = &curr->se;
1077         u64 now = rq_clock(rq);
1078         struct cfs_rq *cfs_rq;
1079
1080         for_each_sched_entity(se) {
1081                 cfs_rq = cfs_rq_of(se);
1082                 set_next_entity(cfs_rq, se, now);
1083         }
1084 }
1085 #else
1086 static void set_curr_task_fair(struct rq *rq)
1087 {
1088 }
1089 #endif
1090
1091 /*
1092  * All the scheduling class methods:
1093  */
1094 struct sched_class fair_sched_class __read_mostly = {
1095         .enqueue_task           = enqueue_task_fair,
1096         .dequeue_task           = dequeue_task_fair,
1097         .yield_task             = yield_task_fair,
1098
1099         .check_preempt_curr     = check_preempt_curr_fair,
1100
1101         .pick_next_task         = pick_next_task_fair,
1102         .put_prev_task          = put_prev_task_fair,
1103
1104         .load_balance           = load_balance_fair,
1105
1106         .set_curr_task          = set_curr_task_fair,
1107         .task_tick              = task_tick_fair,
1108         .task_new               = task_new_fair,
1109 };
1110
1111 #ifdef CONFIG_SCHED_DEBUG
1112 void print_cfs_stats(struct seq_file *m, int cpu, u64 now)
1113 {
1114         struct rq *rq = cpu_rq(cpu);
1115         struct cfs_rq *cfs_rq;
1116
1117         for_each_leaf_cfs_rq(rq, cfs_rq)
1118                 print_cfs_rq(m, cpu, cfs_rq, now);
1119 }
1120 #endif