nfsd: fix possible oops on re-insertion of rpcsec_gss modules
[wrt350n-kernel.git] / kernel / sched_fair.c
blob6971db0a716013286c713afae081b0f851e14d18
1 /*
2 * Completely Fair Scheduling (CFS) Class (SCHED_NORMAL/SCHED_BATCH)
4 * Copyright (C) 2007 Red Hat, Inc., Ingo Molnar <mingo@redhat.com>
6 * Interactivity improvements by Mike Galbraith
7 * (C) 2007 Mike Galbraith <efault@gmx.de>
9 * Various enhancements by Dmitry Adamushko.
10 * (C) 2007 Dmitry Adamushko <dmitry.adamushko@gmail.com>
12 * Group scheduling enhancements by Srivatsa Vaddagiri
13 * Copyright IBM Corporation, 2007
14 * Author: Srivatsa Vaddagiri <vatsa@linux.vnet.ibm.com>
16 * Scaled math optimizations by Thomas Gleixner
17 * Copyright (C) 2007, Thomas Gleixner <tglx@linutronix.de>
21 * Preemption granularity:
22 * (default: 2 msec, units: nanoseconds)
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)
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.)
34 unsigned int sysctl_sched_granularity __read_mostly = 2000000000ULL/HZ;
37 * SCHED_BATCH wake-up granularity.
38 * (default: 10 msec, units: nanoseconds)
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.
44 unsigned int sysctl_sched_batch_wakeup_granularity __read_mostly =
45 10000000000ULL/HZ;
48 * SCHED_OTHER wake-up granularity.
49 * (default: 1 msec, units: nanoseconds)
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.
55 unsigned int sysctl_sched_wakeup_granularity __read_mostly = 1000000000ULL/HZ;
57 unsigned int sysctl_sched_stat_granularity __read_mostly;
60 * Initialized in sched_init_granularity():
62 unsigned int sysctl_sched_runtime_limit __read_mostly;
65 * Debugging: various feature bits
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,
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;
84 extern struct sched_class fair_sched_class;
86 /**************************************************************
87 * CFS operations on generic schedulable entities:
90 #ifdef CONFIG_FAIR_GROUP_SCHED
92 /* cpu runqueue to which this cfs_rq is attached */
93 static inline struct rq *rq_of(struct cfs_rq *cfs_rq)
95 return cfs_rq->rq;
98 /* currently running entity (if any) on this cfs_rq */
99 static inline struct sched_entity *cfs_rq_curr(struct cfs_rq *cfs_rq)
101 return cfs_rq->curr;
104 /* An entity is a task if it doesn't "own" a runqueue */
105 #define entity_is_task(se) (!se->my_q)
107 static inline void
108 set_cfs_rq_curr(struct cfs_rq *cfs_rq, struct sched_entity *se)
110 cfs_rq->curr = se;
113 #else /* CONFIG_FAIR_GROUP_SCHED */
115 static inline struct rq *rq_of(struct cfs_rq *cfs_rq)
117 return container_of(cfs_rq, struct rq, cfs);
120 static inline struct sched_entity *cfs_rq_curr(struct cfs_rq *cfs_rq)
122 struct rq *rq = rq_of(cfs_rq);
124 if (unlikely(rq->curr->sched_class != &fair_sched_class))
125 return NULL;
127 return &rq->curr->se;
130 #define entity_is_task(se) 1
132 static inline void
133 set_cfs_rq_curr(struct cfs_rq *cfs_rq, struct sched_entity *se) { }
135 #endif /* CONFIG_FAIR_GROUP_SCHED */
137 static inline struct task_struct *task_of(struct sched_entity *se)
139 return container_of(se, struct task_struct, se);
143 /**************************************************************
144 * Scheduling class tree data structure manipulation methods:
148 * Enqueue an entity into the rb-tree:
150 static inline void
151 __enqueue_entity(struct cfs_rq *cfs_rq, struct sched_entity *se)
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;
160 * Find the right place in the rbtree:
162 while (*link) {
163 parent = *link;
164 entry = rb_entry(parent, struct sched_entity, run_node);
166 * We dont care about collisions. Nodes with
167 * the same key stay together.
169 if (key - entry->fair_key < 0) {
170 link = &parent->rb_left;
171 } else {
172 link = &parent->rb_right;
173 leftmost = 0;
178 * Maintain a cache of leftmost tree entries (it is frequently
179 * used):
181 if (leftmost)
182 cfs_rq->rb_leftmost = &se->run_node;
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;
191 static inline void
192 __dequeue_entity(struct cfs_rq *cfs_rq, struct sched_entity *se)
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;
202 static inline struct rb_node *first_fair(struct cfs_rq *cfs_rq)
204 return cfs_rq->rb_leftmost;
207 static struct sched_entity *__pick_next_entity(struct cfs_rq *cfs_rq)
209 return rb_entry(first_fair(cfs_rq), struct sched_entity, run_node);
212 /**************************************************************
213 * Scheduling class statistics methods:
217 * We rescale the rescheduling granularity of tasks according to their
218 * nice level, but only linearly, not exponentially:
220 static long
221 niced_granularity(struct sched_entity *curr, unsigned long granularity)
223 u64 tmp;
226 * Negative nice levels get the same granularity as nice-0:
228 if (likely(curr->load.weight >= NICE_0_LOAD))
229 return granularity;
231 * Positive nice level tasks get linearly finer
232 * granularity:
234 tmp = curr->load.weight * (u64)granularity;
237 * It will always fit into 'long':
239 return (long) (tmp >> NICE_0_SHIFT);
242 static inline void
243 limit_wait_runtime(struct cfs_rq *cfs_rq, struct sched_entity *se)
245 long limit = sysctl_sched_runtime_limit;
248 * Niced tasks have the same history dynamic range as
249 * non-niced tasks:
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);
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);
263 static inline void
264 __add_wait_runtime(struct cfs_rq *cfs_rq, struct sched_entity *se, long delta)
266 se->wait_runtime += delta;
267 schedstat_add(se, sum_wait_runtime, delta);
268 limit_wait_runtime(cfs_rq, se);
271 static void
272 add_wait_runtime(struct cfs_rq *cfs_rq, struct sched_entity *se, long delta)
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);
280 * Update the current task's runtime statistics. Skip current tasks that
281 * are not in our scheduling class.
283 static inline void
284 __update_curr(struct cfs_rq *cfs_rq, struct sched_entity *curr, u64 now)
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;
291 if (unlikely(!load))
292 return;
294 delta_exec = curr->delta_exec;
295 #ifdef CONFIG_SCHEDSTATS
296 if (unlikely(delta_exec > curr->exec_max))
297 curr->exec_max = delta_exec;
298 #endif
300 curr->sum_exec_runtime += delta_exec;
301 cfs_rq->exec_clock += delta_exec;
303 delta_fair = calc_delta_fair(delta_exec, lw);
304 delta_mine = calc_delta_mine(delta_exec, curr->load.weight, lw);
306 if (cfs_rq->sleeper_bonus > sysctl_sched_stat_granularity) {
307 delta = calc_delta_mine(cfs_rq->sleeper_bonus,
308 curr->load.weight, lw);
309 if (unlikely(delta > cfs_rq->sleeper_bonus))
310 delta = cfs_rq->sleeper_bonus;
312 cfs_rq->sleeper_bonus -= delta;
313 delta_mine -= delta;
316 cfs_rq->fair_clock += delta_fair;
318 * We executed delta_exec amount of time on the CPU,
319 * but we were only entitled to delta_mine amount of
320 * time during that period (if nr_running == 1 then
321 * the two values are equal)
322 * [Note: delta_mine - delta_exec is negative]:
324 add_wait_runtime(cfs_rq, curr, delta_mine - delta_exec);
327 static void update_curr(struct cfs_rq *cfs_rq, u64 now)
329 struct sched_entity *curr = cfs_rq_curr(cfs_rq);
330 unsigned long delta_exec;
332 if (unlikely(!curr))
333 return;
336 * Get the amount of time the current task was running
337 * since the last time we changed load (this cannot
338 * overflow on 32 bits):
340 delta_exec = (unsigned long)(now - curr->exec_start);
342 curr->delta_exec += delta_exec;
344 if (unlikely(curr->delta_exec > sysctl_sched_stat_granularity)) {
345 __update_curr(cfs_rq, curr, now);
346 curr->delta_exec = 0;
348 curr->exec_start = now;
351 static inline void
352 update_stats_wait_start(struct cfs_rq *cfs_rq, struct sched_entity *se, u64 now)
354 se->wait_start_fair = cfs_rq->fair_clock;
355 se->wait_start = now;
359 * We calculate fair deltas here, so protect against the random effects
360 * of a multiplication overflow by capping it to the runtime limit:
362 #if BITS_PER_LONG == 32
363 static inline unsigned long
364 calc_weighted(unsigned long delta, unsigned long weight, int shift)
366 u64 tmp = (u64)delta * weight >> shift;
368 if (unlikely(tmp > sysctl_sched_runtime_limit*2))
369 return sysctl_sched_runtime_limit*2;
370 return tmp;
372 #else
373 static inline unsigned long
374 calc_weighted(unsigned long delta, unsigned long weight, int shift)
376 return delta * weight >> shift;
378 #endif
381 * Task is being enqueued - update stats:
383 static void
384 update_stats_enqueue(struct cfs_rq *cfs_rq, struct sched_entity *se, u64 now)
386 s64 key;
389 * Are we enqueueing a waiting task? (for current tasks
390 * a dequeue/enqueue event is a NOP)
392 if (se != cfs_rq_curr(cfs_rq))
393 update_stats_wait_start(cfs_rq, se, now);
395 * Update the key:
397 key = cfs_rq->fair_clock;
400 * Optimize the common nice 0 case:
402 if (likely(se->load.weight == NICE_0_LOAD)) {
403 key -= se->wait_runtime;
404 } else {
405 u64 tmp;
407 if (se->wait_runtime < 0) {
408 tmp = -se->wait_runtime;
409 key += (tmp * se->load.inv_weight) >>
410 (WMULT_SHIFT - NICE_0_SHIFT);
411 } else {
412 tmp = se->wait_runtime;
413 key -= (tmp * se->load.weight) >> NICE_0_SHIFT;
417 se->fair_key = key;
421 * Note: must be called with a freshly updated rq->fair_clock.
423 static inline void
424 __update_stats_wait_end(struct cfs_rq *cfs_rq, struct sched_entity *se, u64 now)
426 unsigned long delta_fair = se->delta_fair_run;
428 #ifdef CONFIG_SCHEDSTATS
430 s64 delta_wait = now - se->wait_start;
431 if (unlikely(delta_wait > se->wait_max))
432 se->wait_max = delta_wait;
434 #endif
436 if (unlikely(se->load.weight != NICE_0_LOAD))
437 delta_fair = calc_weighted(delta_fair, se->load.weight,
438 NICE_0_SHIFT);
440 add_wait_runtime(cfs_rq, se, delta_fair);
443 static void
444 update_stats_wait_end(struct cfs_rq *cfs_rq, struct sched_entity *se, u64 now)
446 unsigned long delta_fair;
448 delta_fair = (unsigned long)min((u64)(2*sysctl_sched_runtime_limit),
449 (u64)(cfs_rq->fair_clock - se->wait_start_fair));
451 se->delta_fair_run += delta_fair;
452 if (unlikely(abs(se->delta_fair_run) >=
453 sysctl_sched_stat_granularity)) {
454 __update_stats_wait_end(cfs_rq, se, now);
455 se->delta_fair_run = 0;
458 se->wait_start_fair = 0;
459 se->wait_start = 0;
462 static inline void
463 update_stats_dequeue(struct cfs_rq *cfs_rq, struct sched_entity *se, u64 now)
465 update_curr(cfs_rq, now);
467 * Mark the end of the wait period if dequeueing a
468 * waiting task:
470 if (se != cfs_rq_curr(cfs_rq))
471 update_stats_wait_end(cfs_rq, se, now);
475 * We are picking a new current task - update its stats:
477 static inline void
478 update_stats_curr_start(struct cfs_rq *cfs_rq, struct sched_entity *se, u64 now)
481 * We are starting a new run period:
483 se->exec_start = now;
487 * We are descheduling a task - update its stats:
489 static inline void
490 update_stats_curr_end(struct cfs_rq *cfs_rq, struct sched_entity *se, u64 now)
492 se->exec_start = 0;
495 /**************************************************
496 * Scheduling class queueing methods:
499 static void
500 __enqueue_sleeper(struct cfs_rq *cfs_rq, struct sched_entity *se, u64 now)
502 unsigned long load = cfs_rq->load.weight, delta_fair;
503 long prev_runtime;
505 if (sysctl_sched_features & SCHED_FEAT_SLEEPER_LOAD_AVG)
506 load = rq_of(cfs_rq)->cpu_load[2];
508 delta_fair = se->delta_fair_sleep;
511 * Fix up delta_fair with the effect of us running
512 * during the whole sleep period:
514 if (sysctl_sched_features & SCHED_FEAT_SLEEPER_AVG)
515 delta_fair = div64_likely32((u64)delta_fair * load,
516 load + se->load.weight);
518 if (unlikely(se->load.weight != NICE_0_LOAD))
519 delta_fair = calc_weighted(delta_fair, se->load.weight,
520 NICE_0_SHIFT);
522 prev_runtime = se->wait_runtime;
523 __add_wait_runtime(cfs_rq, se, delta_fair);
524 delta_fair = se->wait_runtime - prev_runtime;
527 * Track the amount of bonus we've given to sleepers:
529 cfs_rq->sleeper_bonus += delta_fair;
531 schedstat_add(cfs_rq, wait_runtime, se->wait_runtime);
534 static void
535 enqueue_sleeper(struct cfs_rq *cfs_rq, struct sched_entity *se, u64 now)
537 struct task_struct *tsk = task_of(se);
538 unsigned long delta_fair;
540 if ((entity_is_task(se) && tsk->policy == SCHED_BATCH) ||
541 !(sysctl_sched_features & SCHED_FEAT_FAIR_SLEEPERS))
542 return;
544 delta_fair = (unsigned long)min((u64)(2*sysctl_sched_runtime_limit),
545 (u64)(cfs_rq->fair_clock - se->sleep_start_fair));
547 se->delta_fair_sleep += delta_fair;
548 if (unlikely(abs(se->delta_fair_sleep) >=
549 sysctl_sched_stat_granularity)) {
550 __enqueue_sleeper(cfs_rq, se, now);
551 se->delta_fair_sleep = 0;
554 se->sleep_start_fair = 0;
556 #ifdef CONFIG_SCHEDSTATS
557 if (se->sleep_start) {
558 u64 delta = now - se->sleep_start;
560 if ((s64)delta < 0)
561 delta = 0;
563 if (unlikely(delta > se->sleep_max))
564 se->sleep_max = delta;
566 se->sleep_start = 0;
567 se->sum_sleep_runtime += delta;
569 if (se->block_start) {
570 u64 delta = now - se->block_start;
572 if ((s64)delta < 0)
573 delta = 0;
575 if (unlikely(delta > se->block_max))
576 se->block_max = delta;
578 se->block_start = 0;
579 se->sum_sleep_runtime += delta;
581 #endif
584 static void
585 enqueue_entity(struct cfs_rq *cfs_rq, struct sched_entity *se,
586 int wakeup, u64 now)
589 * Update the fair clock.
591 update_curr(cfs_rq, now);
593 if (wakeup)
594 enqueue_sleeper(cfs_rq, se, now);
596 update_stats_enqueue(cfs_rq, se, now);
597 __enqueue_entity(cfs_rq, se);
600 static void
601 dequeue_entity(struct cfs_rq *cfs_rq, struct sched_entity *se,
602 int sleep, u64 now)
604 update_stats_dequeue(cfs_rq, se, now);
605 if (sleep) {
606 se->sleep_start_fair = cfs_rq->fair_clock;
607 #ifdef CONFIG_SCHEDSTATS
608 if (entity_is_task(se)) {
609 struct task_struct *tsk = task_of(se);
611 if (tsk->state & TASK_INTERRUPTIBLE)
612 se->sleep_start = now;
613 if (tsk->state & TASK_UNINTERRUPTIBLE)
614 se->block_start = now;
616 cfs_rq->wait_runtime -= se->wait_runtime;
617 #endif
619 __dequeue_entity(cfs_rq, se);
623 * Preempt the current task with a newly woken task if needed:
625 static void
626 __check_preempt_curr_fair(struct cfs_rq *cfs_rq, struct sched_entity *se,
627 struct sched_entity *curr, unsigned long granularity)
629 s64 __delta = curr->fair_key - se->fair_key;
632 * Take scheduling granularity into account - do not
633 * preempt the current task unless the best task has
634 * a larger than sched_granularity fairness advantage:
636 if (__delta > niced_granularity(curr, granularity))
637 resched_task(rq_of(cfs_rq)->curr);
640 static inline void
641 set_next_entity(struct cfs_rq *cfs_rq, struct sched_entity *se, u64 now)
644 * Any task has to be enqueued before it get to execute on
645 * a CPU. So account for the time it spent waiting on the
646 * runqueue. (note, here we rely on pick_next_task() having
647 * done a put_prev_task_fair() shortly before this, which
648 * updated rq->fair_clock - used by update_stats_wait_end())
650 update_stats_wait_end(cfs_rq, se, now);
651 update_stats_curr_start(cfs_rq, se, now);
652 set_cfs_rq_curr(cfs_rq, se);
655 static struct sched_entity *pick_next_entity(struct cfs_rq *cfs_rq, u64 now)
657 struct sched_entity *se = __pick_next_entity(cfs_rq);
659 set_next_entity(cfs_rq, se, now);
661 return se;
664 static void
665 put_prev_entity(struct cfs_rq *cfs_rq, struct sched_entity *prev, u64 now)
668 * If still on the runqueue then deactivate_task()
669 * was not called and update_curr() has to be done:
671 if (prev->on_rq)
672 update_curr(cfs_rq, now);
674 update_stats_curr_end(cfs_rq, prev, now);
676 if (prev->on_rq)
677 update_stats_wait_start(cfs_rq, prev, now);
678 set_cfs_rq_curr(cfs_rq, NULL);
681 static void entity_tick(struct cfs_rq *cfs_rq, struct sched_entity *curr)
683 struct rq *rq = rq_of(cfs_rq);
684 struct sched_entity *next;
685 u64 now = __rq_clock(rq);
688 * Dequeue and enqueue the task to update its
689 * position within the tree:
691 dequeue_entity(cfs_rq, curr, 0, now);
692 enqueue_entity(cfs_rq, curr, 0, now);
695 * Reschedule if another task tops the current one.
697 next = __pick_next_entity(cfs_rq);
698 if (next == curr)
699 return;
701 __check_preempt_curr_fair(cfs_rq, next, curr, sysctl_sched_granularity);
704 /**************************************************
705 * CFS operations on tasks:
708 #ifdef CONFIG_FAIR_GROUP_SCHED
710 /* Walk up scheduling entities hierarchy */
711 #define for_each_sched_entity(se) \
712 for (; se; se = se->parent)
714 static inline struct cfs_rq *task_cfs_rq(struct task_struct *p)
716 return p->se.cfs_rq;
719 /* runqueue on which this entity is (to be) queued */
720 static inline struct cfs_rq *cfs_rq_of(struct sched_entity *se)
722 return se->cfs_rq;
725 /* runqueue "owned" by this group */
726 static inline struct cfs_rq *group_cfs_rq(struct sched_entity *grp)
728 return grp->my_q;
731 /* Given a group's cfs_rq on one cpu, return its corresponding cfs_rq on
732 * another cpu ('this_cpu')
734 static inline struct cfs_rq *cpu_cfs_rq(struct cfs_rq *cfs_rq, int this_cpu)
736 /* A later patch will take group into account */
737 return &cpu_rq(this_cpu)->cfs;
740 /* Iterate thr' all leaf cfs_rq's on a runqueue */
741 #define for_each_leaf_cfs_rq(rq, cfs_rq) \
742 list_for_each_entry(cfs_rq, &rq->leaf_cfs_rq_list, leaf_cfs_rq_list)
744 /* Do the two (enqueued) tasks belong to the same group ? */
745 static inline int is_same_group(struct task_struct *curr, struct task_struct *p)
747 if (curr->se.cfs_rq == p->se.cfs_rq)
748 return 1;
750 return 0;
753 #else /* CONFIG_FAIR_GROUP_SCHED */
755 #define for_each_sched_entity(se) \
756 for (; se; se = NULL)
758 static inline struct cfs_rq *task_cfs_rq(struct task_struct *p)
760 return &task_rq(p)->cfs;
763 static inline struct cfs_rq *cfs_rq_of(struct sched_entity *se)
765 struct task_struct *p = task_of(se);
766 struct rq *rq = task_rq(p);
768 return &rq->cfs;
771 /* runqueue "owned" by this group */
772 static inline struct cfs_rq *group_cfs_rq(struct sched_entity *grp)
774 return NULL;
777 static inline struct cfs_rq *cpu_cfs_rq(struct cfs_rq *cfs_rq, int this_cpu)
779 return &cpu_rq(this_cpu)->cfs;
782 #define for_each_leaf_cfs_rq(rq, cfs_rq) \
783 for (cfs_rq = &rq->cfs; cfs_rq; cfs_rq = NULL)
785 static inline int is_same_group(struct task_struct *curr, struct task_struct *p)
787 return 1;
790 #endif /* CONFIG_FAIR_GROUP_SCHED */
793 * The enqueue_task method is called before nr_running is
794 * increased. Here we update the fair scheduling stats and
795 * then put the task into the rbtree:
797 static void
798 enqueue_task_fair(struct rq *rq, struct task_struct *p, int wakeup, u64 now)
800 struct cfs_rq *cfs_rq;
801 struct sched_entity *se = &p->se;
803 for_each_sched_entity(se) {
804 if (se->on_rq)
805 break;
806 cfs_rq = cfs_rq_of(se);
807 enqueue_entity(cfs_rq, se, wakeup, now);
812 * The dequeue_task method is called before nr_running is
813 * decreased. We remove the task from the rbtree and
814 * update the fair scheduling stats:
816 static void
817 dequeue_task_fair(struct rq *rq, struct task_struct *p, int sleep, u64 now)
819 struct cfs_rq *cfs_rq;
820 struct sched_entity *se = &p->se;
822 for_each_sched_entity(se) {
823 cfs_rq = cfs_rq_of(se);
824 dequeue_entity(cfs_rq, se, sleep, now);
825 /* Don't dequeue parent if it has other entities besides us */
826 if (cfs_rq->load.weight)
827 break;
832 * sched_yield() support is very simple - we dequeue and enqueue
834 static void yield_task_fair(struct rq *rq, struct task_struct *p)
836 struct cfs_rq *cfs_rq = task_cfs_rq(p);
837 u64 now = __rq_clock(rq);
840 * Dequeue and enqueue the task to update its
841 * position within the tree:
843 dequeue_entity(cfs_rq, &p->se, 0, now);
844 enqueue_entity(cfs_rq, &p->se, 0, now);
848 * Preempt the current task with a newly woken task if needed:
850 static void check_preempt_curr_fair(struct rq *rq, struct task_struct *p)
852 struct task_struct *curr = rq->curr;
853 struct cfs_rq *cfs_rq = task_cfs_rq(curr);
854 unsigned long gran;
856 if (unlikely(rt_prio(p->prio))) {
857 update_curr(cfs_rq, rq_clock(rq));
858 resched_task(curr);
859 return;
862 gran = sysctl_sched_wakeup_granularity;
864 * Batch tasks prefer throughput over latency:
866 if (unlikely(p->policy == SCHED_BATCH))
867 gran = sysctl_sched_batch_wakeup_granularity;
869 if (is_same_group(curr, p))
870 __check_preempt_curr_fair(cfs_rq, &p->se, &curr->se, gran);
873 static struct task_struct *pick_next_task_fair(struct rq *rq, u64 now)
875 struct cfs_rq *cfs_rq = &rq->cfs;
876 struct sched_entity *se;
878 if (unlikely(!cfs_rq->nr_running))
879 return NULL;
881 do {
882 se = pick_next_entity(cfs_rq, now);
883 cfs_rq = group_cfs_rq(se);
884 } while (cfs_rq);
886 return task_of(se);
890 * Account for a descheduled task:
892 static void put_prev_task_fair(struct rq *rq, struct task_struct *prev, u64 now)
894 struct sched_entity *se = &prev->se;
895 struct cfs_rq *cfs_rq;
897 for_each_sched_entity(se) {
898 cfs_rq = cfs_rq_of(se);
899 put_prev_entity(cfs_rq, se, now);
903 /**************************************************
904 * Fair scheduling class load-balancing methods:
908 * Load-balancing iterator. Note: while the runqueue stays locked
909 * during the whole iteration, the current task might be
910 * dequeued so the iterator has to be dequeue-safe. Here we
911 * achieve that by always pre-iterating before returning
912 * the current task:
914 static inline struct task_struct *
915 __load_balance_iterator(struct cfs_rq *cfs_rq, struct rb_node *curr)
917 struct task_struct *p;
919 if (!curr)
920 return NULL;
922 p = rb_entry(curr, struct task_struct, se.run_node);
923 cfs_rq->rb_load_balance_curr = rb_next(curr);
925 return p;
928 static struct task_struct *load_balance_start_fair(void *arg)
930 struct cfs_rq *cfs_rq = arg;
932 return __load_balance_iterator(cfs_rq, first_fair(cfs_rq));
935 static struct task_struct *load_balance_next_fair(void *arg)
937 struct cfs_rq *cfs_rq = arg;
939 return __load_balance_iterator(cfs_rq, cfs_rq->rb_load_balance_curr);
942 static int cfs_rq_best_prio(struct cfs_rq *cfs_rq)
944 struct sched_entity *curr;
945 struct task_struct *p;
947 if (!cfs_rq->nr_running)
948 return MAX_PRIO;
950 curr = __pick_next_entity(cfs_rq);
951 p = task_of(curr);
953 return p->prio;
956 static int
957 load_balance_fair(struct rq *this_rq, int this_cpu, struct rq *busiest,
958 unsigned long max_nr_move, unsigned long max_load_move,
959 struct sched_domain *sd, enum cpu_idle_type idle,
960 int *all_pinned, unsigned long *total_load_moved)
962 struct cfs_rq *busy_cfs_rq;
963 unsigned long load_moved, total_nr_moved = 0, nr_moved;
964 long rem_load_move = max_load_move;
965 struct rq_iterator cfs_rq_iterator;
967 cfs_rq_iterator.start = load_balance_start_fair;
968 cfs_rq_iterator.next = load_balance_next_fair;
970 for_each_leaf_cfs_rq(busiest, busy_cfs_rq) {
971 struct cfs_rq *this_cfs_rq;
972 long imbalance;
973 unsigned long maxload;
974 int this_best_prio, best_prio, best_prio_seen = 0;
976 this_cfs_rq = cpu_cfs_rq(busy_cfs_rq, this_cpu);
978 imbalance = busy_cfs_rq->load.weight -
979 this_cfs_rq->load.weight;
980 /* Don't pull if this_cfs_rq has more load than busy_cfs_rq */
981 if (imbalance <= 0)
982 continue;
984 /* Don't pull more than imbalance/2 */
985 imbalance /= 2;
986 maxload = min(rem_load_move, imbalance);
988 this_best_prio = cfs_rq_best_prio(this_cfs_rq);
989 best_prio = cfs_rq_best_prio(busy_cfs_rq);
992 * Enable handling of the case where there is more than one task
993 * with the best priority. If the current running task is one
994 * of those with prio==best_prio we know it won't be moved
995 * and therefore it's safe to override the skip (based on load)
996 * of any task we find with that prio.
998 if (cfs_rq_curr(busy_cfs_rq) == &busiest->curr->se)
999 best_prio_seen = 1;
1001 /* pass busy_cfs_rq argument into
1002 * load_balance_[start|next]_fair iterators
1004 cfs_rq_iterator.arg = busy_cfs_rq;
1005 nr_moved = balance_tasks(this_rq, this_cpu, busiest,
1006 max_nr_move, maxload, sd, idle, all_pinned,
1007 &load_moved, this_best_prio, best_prio,
1008 best_prio_seen, &cfs_rq_iterator);
1010 total_nr_moved += nr_moved;
1011 max_nr_move -= nr_moved;
1012 rem_load_move -= load_moved;
1014 if (max_nr_move <= 0 || rem_load_move <= 0)
1015 break;
1018 *total_load_moved = max_load_move - rem_load_move;
1020 return total_nr_moved;
1024 * scheduler tick hitting a task of our scheduling class:
1026 static void task_tick_fair(struct rq *rq, struct task_struct *curr)
1028 struct cfs_rq *cfs_rq;
1029 struct sched_entity *se = &curr->se;
1031 for_each_sched_entity(se) {
1032 cfs_rq = cfs_rq_of(se);
1033 entity_tick(cfs_rq, se);
1038 * Share the fairness runtime between parent and child, thus the
1039 * total amount of pressure for CPU stays equal - new tasks
1040 * get a chance to run but frequent forkers are not allowed to
1041 * monopolize the CPU. Note: the parent runqueue is locked,
1042 * the child is not running yet.
1044 static void task_new_fair(struct rq *rq, struct task_struct *p)
1046 struct cfs_rq *cfs_rq = task_cfs_rq(p);
1047 struct sched_entity *se = &p->se;
1048 u64 now = rq_clock(rq);
1050 sched_info_queued(p);
1052 update_stats_enqueue(cfs_rq, se, now);
1054 * Child runs first: we let it run before the parent
1055 * until it reschedules once. We set up the key so that
1056 * it will preempt the parent:
1058 p->se.fair_key = current->se.fair_key -
1059 niced_granularity(&rq->curr->se, sysctl_sched_granularity) - 1;
1061 * The first wait is dominated by the child-runs-first logic,
1062 * so do not credit it with that waiting time yet:
1064 if (sysctl_sched_features & SCHED_FEAT_SKIP_INITIAL)
1065 p->se.wait_start_fair = 0;
1068 * The statistical average of wait_runtime is about
1069 * -granularity/2, so initialize the task with that:
1071 if (sysctl_sched_features & SCHED_FEAT_START_DEBIT)
1072 p->se.wait_runtime = -(sysctl_sched_granularity / 2);
1074 __enqueue_entity(cfs_rq, se);
1075 inc_nr_running(p, rq, now);
1078 #ifdef CONFIG_FAIR_GROUP_SCHED
1079 /* Account for a task changing its policy or group.
1081 * This routine is mostly called to set cfs_rq->curr field when a task
1082 * migrates between groups/classes.
1084 static void set_curr_task_fair(struct rq *rq)
1086 struct task_struct *curr = rq->curr;
1087 struct sched_entity *se = &curr->se;
1088 u64 now = rq_clock(rq);
1089 struct cfs_rq *cfs_rq;
1091 for_each_sched_entity(se) {
1092 cfs_rq = cfs_rq_of(se);
1093 set_next_entity(cfs_rq, se, now);
1096 #else
1097 static void set_curr_task_fair(struct rq *rq)
1100 #endif
1103 * All the scheduling class methods:
1105 struct sched_class fair_sched_class __read_mostly = {
1106 .enqueue_task = enqueue_task_fair,
1107 .dequeue_task = dequeue_task_fair,
1108 .yield_task = yield_task_fair,
1110 .check_preempt_curr = check_preempt_curr_fair,
1112 .pick_next_task = pick_next_task_fair,
1113 .put_prev_task = put_prev_task_fair,
1115 .load_balance = load_balance_fair,
1117 .set_curr_task = set_curr_task_fair,
1118 .task_tick = task_tick_fair,
1119 .task_new = task_new_fair,
1122 #ifdef CONFIG_SCHED_DEBUG
1123 void print_cfs_stats(struct seq_file *m, int cpu, u64 now)
1125 struct rq *rq = cpu_rq(cpu);
1126 struct cfs_rq *cfs_rq;
1128 for_each_leaf_cfs_rq(rq, cfs_rq)
1129 print_cfs_rq(m, cpu, cfs_rq, now);
1131 #endif