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