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