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