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