Blame view

kernel/sched_fair.c 27 KB
bf0f6f24a   Ingo Molnar   sched: cfs core, ...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
  /*
   * Completely Fair Scheduling (CFS) Class (SCHED_NORMAL/SCHED_BATCH)
   *
   *  Copyright (C) 2007 Red Hat, Inc., Ingo Molnar <mingo@redhat.com>
   *
   *  Interactivity improvements by Mike Galbraith
   *  (C) 2007 Mike Galbraith <efault@gmx.de>
   *
   *  Various enhancements by Dmitry Adamushko.
   *  (C) 2007 Dmitry Adamushko <dmitry.adamushko@gmail.com>
   *
   *  Group scheduling enhancements by Srivatsa Vaddagiri
   *  Copyright IBM Corporation, 2007
   *  Author: Srivatsa Vaddagiri <vatsa@linux.vnet.ibm.com>
   *
   *  Scaled math optimizations by Thomas Gleixner
   *  Copyright (C) 2007, Thomas Gleixner <tglx@linutronix.de>
218050855   Peter Zijlstra   sched: adaptive s...
18
19
20
   *
   *  Adaptive scheduling granularity, math enhancements by Peter Zijlstra
   *  Copyright (C) 2007 Red Hat, Inc., Peter Zijlstra <pzijlstr@redhat.com>
bf0f6f24a   Ingo Molnar   sched: cfs core, ...
21
22
23
   */
  
  /*
218050855   Peter Zijlstra   sched: adaptive s...
24
   * Targeted preemption latency for CPU-bound tasks:
722aab0c3   Zou Nan hai   sched: fix minimu...
25
   * (default: 20ms * (1 + ilog(ncpus)), units: nanoseconds)
bf0f6f24a   Ingo Molnar   sched: cfs core, ...
26
   *
218050855   Peter Zijlstra   sched: adaptive s...
27
   * NOTE: this latency value is not the same as the concept of
d274a4cee   Ingo Molnar   sched: update com...
28
29
30
   * 'timeslice length' - timeslices in CFS are of variable length
   * and have no persistent notion like in traditional, time-slice
   * based scheduling concepts.
bf0f6f24a   Ingo Molnar   sched: cfs core, ...
31
   *
d274a4cee   Ingo Molnar   sched: update com...
32
33
   * (to see the precise effective timeslice length of your workload,
   *  run vmstat and monitor the context-switches (cs) field)
bf0f6f24a   Ingo Molnar   sched: cfs core, ...
34
   */
19978ca61   Ingo Molnar   sched: reintroduc...
35
  unsigned int sysctl_sched_latency = 20000000ULL;
2bd8e6d42   Ingo Molnar   sched: use consta...
36
37
  
  /*
b2be5e96d   Peter Zijlstra   sched: reintroduc...
38
   * Minimal preemption granularity for CPU-bound tasks:
722aab0c3   Zou Nan hai   sched: fix minimu...
39
   * (default: 4 msec * (1 + ilog(ncpus)), units: nanoseconds)
2bd8e6d42   Ingo Molnar   sched: use consta...
40
   */
722aab0c3   Zou Nan hai   sched: fix minimu...
41
  unsigned int sysctl_sched_min_granularity = 4000000ULL;
218050855   Peter Zijlstra   sched: adaptive s...
42
43
  
  /*
b2be5e96d   Peter Zijlstra   sched: reintroduc...
44
45
   * is kept at sysctl_sched_latency / sysctl_sched_min_granularity
   */
722aab0c3   Zou Nan hai   sched: fix minimu...
46
  static unsigned int sched_nr_latency = 5;
b2be5e96d   Peter Zijlstra   sched: reintroduc...
47
48
49
50
  
  /*
   * After fork, child runs first. (default) If set to 0 then
   * parent will (try to) run first.
218050855   Peter Zijlstra   sched: adaptive s...
51
   */
b2be5e96d   Peter Zijlstra   sched: reintroduc...
52
  const_debug unsigned int sysctl_sched_child_runs_first = 1;
bf0f6f24a   Ingo Molnar   sched: cfs core, ...
53
54
  
  /*
1799e35d5   Ingo Molnar   sched: add /proc/...
55
56
57
58
59
60
61
62
   * sys_sched_yield() compat mode
   *
   * This option switches the agressive yield implementation of the
   * old scheduler back on.
   */
  unsigned int __read_mostly sysctl_sched_compat_yield;
  
  /*
bf0f6f24a   Ingo Molnar   sched: cfs core, ...
63
   * SCHED_BATCH wake-up granularity.
722aab0c3   Zou Nan hai   sched: fix minimu...
64
   * (default: 10 msec * (1 + ilog(ncpus)), units: nanoseconds)
bf0f6f24a   Ingo Molnar   sched: cfs core, ...
65
66
67
68
69
   *
   * This option delays the preemption effects of decoupled workloads
   * and reduces their over-scheduling. Synchronous workloads will still
   * have immediate wakeup/sleep latencies.
   */
19978ca61   Ingo Molnar   sched: reintroduc...
70
  unsigned int sysctl_sched_batch_wakeup_granularity = 10000000UL;
bf0f6f24a   Ingo Molnar   sched: cfs core, ...
71
72
73
  
  /*
   * SCHED_OTHER wake-up granularity.
722aab0c3   Zou Nan hai   sched: fix minimu...
74
   * (default: 10 msec * (1 + ilog(ncpus)), units: nanoseconds)
bf0f6f24a   Ingo Molnar   sched: cfs core, ...
75
76
77
78
79
   *
   * This option delays the preemption effects of decoupled workloads
   * and reduces their over-scheduling. Synchronous workloads will still
   * have immediate wakeup/sleep latencies.
   */
19978ca61   Ingo Molnar   sched: reintroduc...
80
  unsigned int sysctl_sched_wakeup_granularity = 10000000UL;
bf0f6f24a   Ingo Molnar   sched: cfs core, ...
81

da84d9617   Ingo Molnar   sched: reintroduc...
82
  const_debug unsigned int sysctl_sched_migration_cost = 500000UL;
bf0f6f24a   Ingo Molnar   sched: cfs core, ...
83
84
85
  /**************************************************************
   * CFS operations on generic schedulable entities:
   */
62160e3f4   Ingo Molnar   sched: track cfs_...
86
  #ifdef CONFIG_FAIR_GROUP_SCHED
bf0f6f24a   Ingo Molnar   sched: cfs core, ...
87

62160e3f4   Ingo Molnar   sched: track cfs_...
88
  /* cpu runqueue to which this cfs_rq is attached */
bf0f6f24a   Ingo Molnar   sched: cfs core, ...
89
90
  static inline struct rq *rq_of(struct cfs_rq *cfs_rq)
  {
62160e3f4   Ingo Molnar   sched: track cfs_...
91
  	return cfs_rq->rq;
bf0f6f24a   Ingo Molnar   sched: cfs core, ...
92
  }
62160e3f4   Ingo Molnar   sched: track cfs_...
93
94
  /* An entity is a task if it doesn't "own" a runqueue */
  #define entity_is_task(se)	(!se->my_q)
bf0f6f24a   Ingo Molnar   sched: cfs core, ...
95

62160e3f4   Ingo Molnar   sched: track cfs_...
96
  #else	/* CONFIG_FAIR_GROUP_SCHED */
bf0f6f24a   Ingo Molnar   sched: cfs core, ...
97

62160e3f4   Ingo Molnar   sched: track cfs_...
98
99
100
  static inline struct rq *rq_of(struct cfs_rq *cfs_rq)
  {
  	return container_of(cfs_rq, struct rq, cfs);
bf0f6f24a   Ingo Molnar   sched: cfs core, ...
101
102
103
  }
  
  #define entity_is_task(se)	1
bf0f6f24a   Ingo Molnar   sched: cfs core, ...
104
105
106
107
108
109
110
111
112
113
114
  #endif	/* CONFIG_FAIR_GROUP_SCHED */
  
  static inline struct task_struct *task_of(struct sched_entity *se)
  {
  	return container_of(se, struct task_struct, se);
  }
  
  
  /**************************************************************
   * Scheduling class tree data structure manipulation methods:
   */
0702e3ebc   Ingo Molnar   sched: cleanup: f...
115
  static inline u64 max_vruntime(u64 min_vruntime, u64 vruntime)
02e0431a3   Peter Zijlstra   sched: better min...
116
  {
368059a97   Peter Zijlstra   sched: max_vrunti...
117
118
  	s64 delta = (s64)(vruntime - min_vruntime);
  	if (delta > 0)
02e0431a3   Peter Zijlstra   sched: better min...
119
120
121
122
  		min_vruntime = vruntime;
  
  	return min_vruntime;
  }
0702e3ebc   Ingo Molnar   sched: cleanup: f...
123
  static inline u64 min_vruntime(u64 min_vruntime, u64 vruntime)
b0ffd246e   Peter Zijlstra   sched: clean up m...
124
125
126
127
128
129
130
  {
  	s64 delta = (s64)(vruntime - min_vruntime);
  	if (delta < 0)
  		min_vruntime = vruntime;
  
  	return min_vruntime;
  }
0702e3ebc   Ingo Molnar   sched: cleanup: f...
131
  static inline s64 entity_key(struct cfs_rq *cfs_rq, struct sched_entity *se)
9014623c0   Peter Zijlstra   sched: handle vru...
132
  {
30cfdcfc5   Dmitry Adamushko   sched: do not kee...
133
  	return se->vruntime - cfs_rq->min_vruntime;
9014623c0   Peter Zijlstra   sched: handle vru...
134
  }
bf0f6f24a   Ingo Molnar   sched: cfs core, ...
135
136
137
  /*
   * Enqueue an entity into the rb-tree:
   */
0702e3ebc   Ingo Molnar   sched: cleanup: f...
138
  static void __enqueue_entity(struct cfs_rq *cfs_rq, struct sched_entity *se)
bf0f6f24a   Ingo Molnar   sched: cfs core, ...
139
140
141
142
  {
  	struct rb_node **link = &cfs_rq->tasks_timeline.rb_node;
  	struct rb_node *parent = NULL;
  	struct sched_entity *entry;
9014623c0   Peter Zijlstra   sched: handle vru...
143
  	s64 key = entity_key(cfs_rq, se);
bf0f6f24a   Ingo Molnar   sched: cfs core, ...
144
145
146
147
148
149
150
151
152
153
154
155
  	int leftmost = 1;
  
  	/*
  	 * Find the right place in the rbtree:
  	 */
  	while (*link) {
  		parent = *link;
  		entry = rb_entry(parent, struct sched_entity, run_node);
  		/*
  		 * We dont care about collisions. Nodes with
  		 * the same key stay together.
  		 */
9014623c0   Peter Zijlstra   sched: handle vru...
156
  		if (key < entity_key(cfs_rq, entry)) {
bf0f6f24a   Ingo Molnar   sched: cfs core, ...
157
158
159
160
161
162
163
164
165
166
167
168
  			link = &parent->rb_left;
  		} else {
  			link = &parent->rb_right;
  			leftmost = 0;
  		}
  	}
  
  	/*
  	 * Maintain a cache of leftmost tree entries (it is frequently
  	 * used):
  	 */
  	if (leftmost)
57cb499df   Ingo Molnar   sched: remove set...
169
  		cfs_rq->rb_leftmost = &se->run_node;
bf0f6f24a   Ingo Molnar   sched: cfs core, ...
170
171
172
  
  	rb_link_node(&se->run_node, parent, link);
  	rb_insert_color(&se->run_node, &cfs_rq->tasks_timeline);
bf0f6f24a   Ingo Molnar   sched: cfs core, ...
173
  }
0702e3ebc   Ingo Molnar   sched: cleanup: f...
174
  static void __dequeue_entity(struct cfs_rq *cfs_rq, struct sched_entity *se)
bf0f6f24a   Ingo Molnar   sched: cfs core, ...
175
176
  {
  	if (cfs_rq->rb_leftmost == &se->run_node)
57cb499df   Ingo Molnar   sched: remove set...
177
  		cfs_rq->rb_leftmost = rb_next(&se->run_node);
e9acbff64   Ingo Molnar   sched: introduce ...
178

bf0f6f24a   Ingo Molnar   sched: cfs core, ...
179
  	rb_erase(&se->run_node, &cfs_rq->tasks_timeline);
bf0f6f24a   Ingo Molnar   sched: cfs core, ...
180
181
182
183
184
185
186
187
188
189
190
  }
  
  static inline struct rb_node *first_fair(struct cfs_rq *cfs_rq)
  {
  	return cfs_rq->rb_leftmost;
  }
  
  static struct sched_entity *__pick_next_entity(struct cfs_rq *cfs_rq)
  {
  	return rb_entry(first_fair(cfs_rq), struct sched_entity, run_node);
  }
aeb73b040   Peter Zijlstra   sched: clean up n...
191
192
193
194
195
196
197
198
199
200
201
202
203
204
  static inline struct sched_entity *__pick_last_entity(struct cfs_rq *cfs_rq)
  {
  	struct rb_node **link = &cfs_rq->tasks_timeline.rb_node;
  	struct sched_entity *se = NULL;
  	struct rb_node *parent;
  
  	while (*link) {
  		parent = *link;
  		se = rb_entry(parent, struct sched_entity, run_node);
  		link = &parent->rb_right;
  	}
  
  	return se;
  }
bf0f6f24a   Ingo Molnar   sched: cfs core, ...
205
206
207
  /**************************************************************
   * Scheduling class statistics methods:
   */
b2be5e96d   Peter Zijlstra   sched: reintroduc...
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
  #ifdef CONFIG_SCHED_DEBUG
  int sched_nr_latency_handler(struct ctl_table *table, int write,
  		struct file *filp, void __user *buffer, size_t *lenp,
  		loff_t *ppos)
  {
  	int ret = proc_dointvec_minmax(table, write, filp, buffer, lenp, ppos);
  
  	if (ret || !write)
  		return ret;
  
  	sched_nr_latency = DIV_ROUND_UP(sysctl_sched_latency,
  					sysctl_sched_min_granularity);
  
  	return 0;
  }
  #endif
647e7cac2   Ingo Molnar   sched: vslice fix...
224
225
226
227
228
229
230
231
232
  
  /*
   * The idea is to set a period in which each task runs once.
   *
   * When there are too many tasks (sysctl_sched_nr_latency) we have to stretch
   * this period because otherwise the slices get too small.
   *
   * p = (nr <= nl) ? l : l*nr/nl
   */
4d78e7b65   Peter Zijlstra   sched: new task p...
233
234
235
  static u64 __sched_period(unsigned long nr_running)
  {
  	u64 period = sysctl_sched_latency;
b2be5e96d   Peter Zijlstra   sched: reintroduc...
236
  	unsigned long nr_latency = sched_nr_latency;
4d78e7b65   Peter Zijlstra   sched: new task p...
237
238
239
240
241
242
243
244
  
  	if (unlikely(nr_running > nr_latency)) {
  		period *= nr_running;
  		do_div(period, nr_latency);
  	}
  
  	return period;
  }
647e7cac2   Ingo Molnar   sched: vslice fix...
245
246
247
248
249
250
  /*
   * We calculate the wall-time slice from the period by taking a part
   * proportional to the weight.
   *
   * s = p*w/rw
   */
6d0f0ebd0   Peter Zijlstra   sched: simplify a...
251
  static u64 sched_slice(struct cfs_rq *cfs_rq, struct sched_entity *se)
218050855   Peter Zijlstra   sched: adaptive s...
252
  {
647e7cac2   Ingo Molnar   sched: vslice fix...
253
  	u64 slice = __sched_period(cfs_rq->nr_running);
218050855   Peter Zijlstra   sched: adaptive s...
254

647e7cac2   Ingo Molnar   sched: vslice fix...
255
256
  	slice *= se->load.weight;
  	do_div(slice, cfs_rq->load.weight);
218050855   Peter Zijlstra   sched: adaptive s...
257

647e7cac2   Ingo Molnar   sched: vslice fix...
258
  	return slice;
bf0f6f24a   Ingo Molnar   sched: cfs core, ...
259
  }
647e7cac2   Ingo Molnar   sched: vslice fix...
260
261
262
263
264
265
  /*
   * We calculate the vruntime slice.
   *
   * vs = s/w = p/rw
   */
  static u64 __sched_vslice(unsigned long rq_weight, unsigned long nr_running)
67e9fb2a3   Peter Zijlstra   sched: add vslice
266
  {
647e7cac2   Ingo Molnar   sched: vslice fix...
267
  	u64 vslice = __sched_period(nr_running);
67e9fb2a3   Peter Zijlstra   sched: add vslice
268

10b777246   Peter Zijlstra   sched: fix vslice
269
  	vslice *= NICE_0_LOAD;
647e7cac2   Ingo Molnar   sched: vslice fix...
270
  	do_div(vslice, rq_weight);
67e9fb2a3   Peter Zijlstra   sched: add vslice
271

647e7cac2   Ingo Molnar   sched: vslice fix...
272
273
  	return vslice;
  }
5f6d858ec   Peter Zijlstra   sched: speed up a...
274

647e7cac2   Ingo Molnar   sched: vslice fix...
275
276
277
278
279
280
281
282
283
  static u64 sched_vslice(struct cfs_rq *cfs_rq)
  {
  	return __sched_vslice(cfs_rq->load.weight, cfs_rq->nr_running);
  }
  
  static u64 sched_vslice_add(struct cfs_rq *cfs_rq, struct sched_entity *se)
  {
  	return __sched_vslice(cfs_rq->load.weight + se->load.weight,
  			cfs_rq->nr_running + 1);
67e9fb2a3   Peter Zijlstra   sched: add vslice
284
  }
bf0f6f24a   Ingo Molnar   sched: cfs core, ...
285
286
287
288
289
  /*
   * Update the current task's runtime statistics. Skip current tasks that
   * are not in our scheduling class.
   */
  static inline void
8ebc91d93   Ingo Molnar   sched: remove sta...
290
291
  __update_curr(struct cfs_rq *cfs_rq, struct sched_entity *curr,
  	      unsigned long delta_exec)
bf0f6f24a   Ingo Molnar   sched: cfs core, ...
292
  {
bbdba7c0e   Ingo Molnar   sched: remove wai...
293
  	unsigned long delta_exec_weighted;
b0ffd246e   Peter Zijlstra   sched: clean up m...
294
  	u64 vruntime;
bf0f6f24a   Ingo Molnar   sched: cfs core, ...
295

8179ca23d   Ingo Molnar   [PATCH] sched: us...
296
  	schedstat_set(curr->exec_max, max((u64)delta_exec, curr->exec_max));
bf0f6f24a   Ingo Molnar   sched: cfs core, ...
297
298
  
  	curr->sum_exec_runtime += delta_exec;
7a62eabc4   Ingo Molnar   sched: debug: upd...
299
  	schedstat_add(cfs_rq, exec_clock, delta_exec);
e9acbff64   Ingo Molnar   sched: introduce ...
300
301
302
303
304
305
  	delta_exec_weighted = delta_exec;
  	if (unlikely(curr->load.weight != NICE_0_LOAD)) {
  		delta_exec_weighted = calc_delta_fair(delta_exec_weighted,
  							&curr->load);
  	}
  	curr->vruntime += delta_exec_weighted;
02e0431a3   Peter Zijlstra   sched: better min...
306
307
308
309
310
311
  
  	/*
  	 * maintain cfs_rq->min_vruntime to be a monotonic increasing
  	 * value tracking the leftmost vruntime in the tree.
  	 */
  	if (first_fair(cfs_rq)) {
b0ffd246e   Peter Zijlstra   sched: clean up m...
312
313
  		vruntime = min_vruntime(curr->vruntime,
  				__pick_next_entity(cfs_rq)->vruntime);
02e0431a3   Peter Zijlstra   sched: better min...
314
  	} else
b0ffd246e   Peter Zijlstra   sched: clean up m...
315
  		vruntime = curr->vruntime;
02e0431a3   Peter Zijlstra   sched: better min...
316
317
  
  	cfs_rq->min_vruntime =
b0ffd246e   Peter Zijlstra   sched: clean up m...
318
  		max_vruntime(cfs_rq->min_vruntime, vruntime);
bf0f6f24a   Ingo Molnar   sched: cfs core, ...
319
  }
b7cc08965   Ingo Molnar   sched: remove the...
320
  static void update_curr(struct cfs_rq *cfs_rq)
bf0f6f24a   Ingo Molnar   sched: cfs core, ...
321
  {
429d43bcc   Ingo Molnar   sched: cleanup: s...
322
  	struct sched_entity *curr = cfs_rq->curr;
8ebc91d93   Ingo Molnar   sched: remove sta...
323
  	u64 now = rq_of(cfs_rq)->clock;
bf0f6f24a   Ingo Molnar   sched: cfs core, ...
324
325
326
327
328
329
330
331
332
333
  	unsigned long delta_exec;
  
  	if (unlikely(!curr))
  		return;
  
  	/*
  	 * Get the amount of time the current task was running
  	 * since the last time we changed load (this cannot
  	 * overflow on 32 bits):
  	 */
8ebc91d93   Ingo Molnar   sched: remove sta...
334
  	delta_exec = (unsigned long)(now - curr->exec_start);
bf0f6f24a   Ingo Molnar   sched: cfs core, ...
335

8ebc91d93   Ingo Molnar   sched: remove sta...
336
337
  	__update_curr(cfs_rq, curr, delta_exec);
  	curr->exec_start = now;
d842de871   Srivatsa Vaddagiri   sched: cpu accoun...
338
339
340
341
342
343
  
  	if (entity_is_task(curr)) {
  		struct task_struct *curtask = task_of(curr);
  
  		cpuacct_charge(curtask, delta_exec);
  	}
bf0f6f24a   Ingo Molnar   sched: cfs core, ...
344
345
346
  }
  
  static inline void
5870db5b8   Ingo Molnar   sched: remove the...
347
  update_stats_wait_start(struct cfs_rq *cfs_rq, struct sched_entity *se)
bf0f6f24a   Ingo Molnar   sched: cfs core, ...
348
  {
d281918d7   Ingo Molnar   sched: remove 'no...
349
  	schedstat_set(se->wait_start, rq_of(cfs_rq)->clock);
bf0f6f24a   Ingo Molnar   sched: cfs core, ...
350
  }
bf0f6f24a   Ingo Molnar   sched: cfs core, ...
351
352
353
  /*
   * Task is being enqueued - update stats:
   */
d2417e5a3   Ingo Molnar   sched: remove the...
354
  static void update_stats_enqueue(struct cfs_rq *cfs_rq, struct sched_entity *se)
bf0f6f24a   Ingo Molnar   sched: cfs core, ...
355
  {
bf0f6f24a   Ingo Molnar   sched: cfs core, ...
356
357
358
359
  	/*
  	 * Are we enqueueing a waiting task? (for current tasks
  	 * a dequeue/enqueue event is a NOP)
  	 */
429d43bcc   Ingo Molnar   sched: cleanup: s...
360
  	if (se != cfs_rq->curr)
5870db5b8   Ingo Molnar   sched: remove the...
361
  		update_stats_wait_start(cfs_rq, se);
bf0f6f24a   Ingo Molnar   sched: cfs core, ...
362
  }
bf0f6f24a   Ingo Molnar   sched: cfs core, ...
363
  static void
9ef0a9615   Ingo Molnar   sched: remove the...
364
  update_stats_wait_end(struct cfs_rq *cfs_rq, struct sched_entity *se)
bf0f6f24a   Ingo Molnar   sched: cfs core, ...
365
  {
bbdba7c0e   Ingo Molnar   sched: remove wai...
366
367
  	schedstat_set(se->wait_max, max(se->wait_max,
  			rq_of(cfs_rq)->clock - se->wait_start));
6cfb0d5d0   Ingo Molnar   [PATCH] sched: re...
368
  	schedstat_set(se->wait_start, 0);
bf0f6f24a   Ingo Molnar   sched: cfs core, ...
369
370
371
  }
  
  static inline void
19b6a2e37   Ingo Molnar   sched: remove the...
372
  update_stats_dequeue(struct cfs_rq *cfs_rq, struct sched_entity *se)
bf0f6f24a   Ingo Molnar   sched: cfs core, ...
373
  {
bf0f6f24a   Ingo Molnar   sched: cfs core, ...
374
375
376
377
  	/*
  	 * Mark the end of the wait period if dequeueing a
  	 * waiting task:
  	 */
429d43bcc   Ingo Molnar   sched: cleanup: s...
378
  	if (se != cfs_rq->curr)
9ef0a9615   Ingo Molnar   sched: remove the...
379
  		update_stats_wait_end(cfs_rq, se);
bf0f6f24a   Ingo Molnar   sched: cfs core, ...
380
381
382
383
384
385
  }
  
  /*
   * We are picking a new current task - update its stats:
   */
  static inline void
79303e9e0   Ingo Molnar   sched: remove the...
386
  update_stats_curr_start(struct cfs_rq *cfs_rq, struct sched_entity *se)
bf0f6f24a   Ingo Molnar   sched: cfs core, ...
387
388
389
390
  {
  	/*
  	 * We are starting a new run period:
  	 */
d281918d7   Ingo Molnar   sched: remove 'no...
391
  	se->exec_start = rq_of(cfs_rq)->clock;
bf0f6f24a   Ingo Molnar   sched: cfs core, ...
392
  }
bf0f6f24a   Ingo Molnar   sched: cfs core, ...
393
394
395
  /**************************************************
   * Scheduling class queueing methods:
   */
30cfdcfc5   Dmitry Adamushko   sched: do not kee...
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
  static void
  account_entity_enqueue(struct cfs_rq *cfs_rq, struct sched_entity *se)
  {
  	update_load_add(&cfs_rq->load, se->load.weight);
  	cfs_rq->nr_running++;
  	se->on_rq = 1;
  }
  
  static void
  account_entity_dequeue(struct cfs_rq *cfs_rq, struct sched_entity *se)
  {
  	update_load_sub(&cfs_rq->load, se->load.weight);
  	cfs_rq->nr_running--;
  	se->on_rq = 0;
  }
2396af69b   Ingo Molnar   sched: remove the...
411
  static void enqueue_sleeper(struct cfs_rq *cfs_rq, struct sched_entity *se)
bf0f6f24a   Ingo Molnar   sched: cfs core, ...
412
  {
bf0f6f24a   Ingo Molnar   sched: cfs core, ...
413
414
  #ifdef CONFIG_SCHEDSTATS
  	if (se->sleep_start) {
d281918d7   Ingo Molnar   sched: remove 'no...
415
  		u64 delta = rq_of(cfs_rq)->clock - se->sleep_start;
bf0f6f24a   Ingo Molnar   sched: cfs core, ...
416
417
418
419
420
421
422
423
424
425
426
  
  		if ((s64)delta < 0)
  			delta = 0;
  
  		if (unlikely(delta > se->sleep_max))
  			se->sleep_max = delta;
  
  		se->sleep_start = 0;
  		se->sum_sleep_runtime += delta;
  	}
  	if (se->block_start) {
d281918d7   Ingo Molnar   sched: remove 'no...
427
  		u64 delta = rq_of(cfs_rq)->clock - se->block_start;
bf0f6f24a   Ingo Molnar   sched: cfs core, ...
428
429
430
431
432
433
434
435
436
  
  		if ((s64)delta < 0)
  			delta = 0;
  
  		if (unlikely(delta > se->block_max))
  			se->block_max = delta;
  
  		se->block_start = 0;
  		se->sum_sleep_runtime += delta;
30084fbd1   Ingo Molnar   sched: fix profil...
437
438
439
440
441
442
443
  
  		/*
  		 * Blocking time is in units of nanosecs, so shift by 20 to
  		 * get a milliseconds-range estimation of the amount of
  		 * time that the task spent sleeping:
  		 */
  		if (unlikely(prof_on == SLEEP_PROFILING)) {
e22f5bbf8   Ingo Molnar   sched: remove wai...
444
  			struct task_struct *tsk = task_of(se);
30084fbd1   Ingo Molnar   sched: fix profil...
445
446
447
  			profile_hits(SLEEP_PROFILING, (void *)get_wchan(tsk),
  				     delta >> 20);
  		}
bf0f6f24a   Ingo Molnar   sched: cfs core, ...
448
449
450
  	}
  #endif
  }
ddc972975   Peter Zijlstra   sched debug: chec...
451
452
453
454
455
456
457
458
459
460
461
462
  static void check_spread(struct cfs_rq *cfs_rq, struct sched_entity *se)
  {
  #ifdef CONFIG_SCHED_DEBUG
  	s64 d = se->vruntime - cfs_rq->min_vruntime;
  
  	if (d < 0)
  		d = -d;
  
  	if (d > 3*sysctl_sched_latency)
  		schedstat_inc(cfs_rq, nr_spread_over);
  #endif
  }
bf0f6f24a   Ingo Molnar   sched: cfs core, ...
463
  static void
aeb73b040   Peter Zijlstra   sched: clean up n...
464
465
  place_entity(struct cfs_rq *cfs_rq, struct sched_entity *se, int initial)
  {
67e9fb2a3   Peter Zijlstra   sched: add vslice
466
  	u64 vruntime;
aeb73b040   Peter Zijlstra   sched: clean up n...
467

67e9fb2a3   Peter Zijlstra   sched: add vslice
468
  	vruntime = cfs_rq->min_vruntime;
94dfb5e75   Peter Zijlstra   sched: add tree b...
469

06877c33f   Ingo Molnar   sched: cleanup: r...
470
  	if (sched_feat(TREE_AVG)) {
94dfb5e75   Peter Zijlstra   sched: add tree b...
471
472
  		struct sched_entity *last = __pick_last_entity(cfs_rq);
  		if (last) {
67e9fb2a3   Peter Zijlstra   sched: add vslice
473
474
  			vruntime += last->vruntime;
  			vruntime >>= 1;
94dfb5e75   Peter Zijlstra   sched: add tree b...
475
  		}
67e9fb2a3   Peter Zijlstra   sched: add vslice
476
  	} else if (sched_feat(APPROX_AVG) && cfs_rq->nr_running)
647e7cac2   Ingo Molnar   sched: vslice fix...
477
  		vruntime += sched_vslice(cfs_rq)/2;
94dfb5e75   Peter Zijlstra   sched: add tree b...
478

2cb8600e6   Peter Zijlstra   sched: documentat...
479
480
481
482
483
484
  	/*
  	 * The 'current' period is already promised to the current tasks,
  	 * however the extra weight of the new task will slow them down a
  	 * little, place the new task so that it fits in the slot that
  	 * stays open at the end.
  	 */
94dfb5e75   Peter Zijlstra   sched: add tree b...
485
  	if (initial && sched_feat(START_DEBIT))
647e7cac2   Ingo Molnar   sched: vslice fix...
486
  		vruntime += sched_vslice_add(cfs_rq, se);
aeb73b040   Peter Zijlstra   sched: clean up n...
487

8465e792e   Ingo Molnar   sched: entity_key...
488
  	if (!initial) {
2cb8600e6   Peter Zijlstra   sched: documentat...
489
  		/* sleeps upto a single latency don't count. */
6cbf1c126   Ingo Molnar   sched: do not hur...
490
  		if (sched_feat(NEW_FAIR_SLEEPERS) && entity_is_task(se))
94359f05c   Ingo Molnar   sched: undo some ...
491
  			vruntime -= sysctl_sched_latency;
2cb8600e6   Peter Zijlstra   sched: documentat...
492
493
  		/* ensure we never gain time by being placed backwards. */
  		vruntime = max_vruntime(se->vruntime, vruntime);
aeb73b040   Peter Zijlstra   sched: clean up n...
494
  	}
67e9fb2a3   Peter Zijlstra   sched: add vslice
495
  	se->vruntime = vruntime;
aeb73b040   Peter Zijlstra   sched: clean up n...
496
497
498
  }
  
  static void
83b699ed2   Srivatsa Vaddagiri   sched: revert rec...
499
  enqueue_entity(struct cfs_rq *cfs_rq, struct sched_entity *se, int wakeup)
bf0f6f24a   Ingo Molnar   sched: cfs core, ...
500
501
  {
  	/*
a2a2d6807   Dmitry Adamushko   sched: cleanup, m...
502
  	 * Update run-time statistics of the 'current'.
bf0f6f24a   Ingo Molnar   sched: cfs core, ...
503
  	 */
b7cc08965   Ingo Molnar   sched: remove the...
504
  	update_curr(cfs_rq);
bf0f6f24a   Ingo Molnar   sched: cfs core, ...
505

e9acbff64   Ingo Molnar   sched: introduce ...
506
  	if (wakeup) {
aeb73b040   Peter Zijlstra   sched: clean up n...
507
  		place_entity(cfs_rq, se, 0);
2396af69b   Ingo Molnar   sched: remove the...
508
  		enqueue_sleeper(cfs_rq, se);
e9acbff64   Ingo Molnar   sched: introduce ...
509
  	}
bf0f6f24a   Ingo Molnar   sched: cfs core, ...
510

d2417e5a3   Ingo Molnar   sched: remove the...
511
  	update_stats_enqueue(cfs_rq, se);
ddc972975   Peter Zijlstra   sched debug: chec...
512
  	check_spread(cfs_rq, se);
83b699ed2   Srivatsa Vaddagiri   sched: revert rec...
513
514
  	if (se != cfs_rq->curr)
  		__enqueue_entity(cfs_rq, se);
30cfdcfc5   Dmitry Adamushko   sched: do not kee...
515
  	account_entity_enqueue(cfs_rq, se);
bf0f6f24a   Ingo Molnar   sched: cfs core, ...
516
517
518
  }
  
  static void
525c2716a   Ingo Molnar   sched: remove the...
519
  dequeue_entity(struct cfs_rq *cfs_rq, struct sched_entity *se, int sleep)
bf0f6f24a   Ingo Molnar   sched: cfs core, ...
520
  {
a2a2d6807   Dmitry Adamushko   sched: cleanup, m...
521
522
523
524
  	/*
  	 * Update run-time statistics of the 'current'.
  	 */
  	update_curr(cfs_rq);
19b6a2e37   Ingo Molnar   sched: remove the...
525
  	update_stats_dequeue(cfs_rq, se);
db36cc7d6   Dmitry Adamushko   sched: clean up s...
526
  	if (sleep) {
67e9fb2a3   Peter Zijlstra   sched: add vslice
527
  #ifdef CONFIG_SCHEDSTATS
bf0f6f24a   Ingo Molnar   sched: cfs core, ...
528
529
530
531
  		if (entity_is_task(se)) {
  			struct task_struct *tsk = task_of(se);
  
  			if (tsk->state & TASK_INTERRUPTIBLE)
d281918d7   Ingo Molnar   sched: remove 'no...
532
  				se->sleep_start = rq_of(cfs_rq)->clock;
bf0f6f24a   Ingo Molnar   sched: cfs core, ...
533
  			if (tsk->state & TASK_UNINTERRUPTIBLE)
d281918d7   Ingo Molnar   sched: remove 'no...
534
  				se->block_start = rq_of(cfs_rq)->clock;
bf0f6f24a   Ingo Molnar   sched: cfs core, ...
535
  		}
db36cc7d6   Dmitry Adamushko   sched: clean up s...
536
  #endif
67e9fb2a3   Peter Zijlstra   sched: add vslice
537
  	}
83b699ed2   Srivatsa Vaddagiri   sched: revert rec...
538
  	if (se != cfs_rq->curr)
30cfdcfc5   Dmitry Adamushko   sched: do not kee...
539
540
  		__dequeue_entity(cfs_rq, se);
  	account_entity_dequeue(cfs_rq, se);
bf0f6f24a   Ingo Molnar   sched: cfs core, ...
541
542
543
544
545
  }
  
  /*
   * Preempt the current task with a newly woken task if needed:
   */
7c92e54f6   Peter Zijlstra   sched: simplify _...
546
  static void
2e09bf556   Ingo Molnar   sched: wakeup gra...
547
  check_preempt_tick(struct cfs_rq *cfs_rq, struct sched_entity *curr)
bf0f6f24a   Ingo Molnar   sched: cfs core, ...
548
  {
116978308   Peter Zijlstra   sched: fix ideal_...
549
  	unsigned long ideal_runtime, delta_exec;
6d0f0ebd0   Peter Zijlstra   sched: simplify a...
550
  	ideal_runtime = sched_slice(cfs_rq, curr);
116978308   Peter Zijlstra   sched: fix ideal_...
551
  	delta_exec = curr->sum_exec_runtime - curr->prev_sum_exec_runtime;
3e3e13f39   Ingo Molnar   sched: remove PRE...
552
  	if (delta_exec > ideal_runtime)
bf0f6f24a   Ingo Molnar   sched: cfs core, ...
553
554
  		resched_task(rq_of(cfs_rq)->curr);
  }
83b699ed2   Srivatsa Vaddagiri   sched: revert rec...
555
  static void
8494f412e   Ingo Molnar   sched: remove the...
556
  set_next_entity(struct cfs_rq *cfs_rq, struct sched_entity *se)
bf0f6f24a   Ingo Molnar   sched: cfs core, ...
557
  {
83b699ed2   Srivatsa Vaddagiri   sched: revert rec...
558
559
560
561
562
563
564
565
566
567
  	/* 'current' is not kept within the tree. */
  	if (se->on_rq) {
  		/*
  		 * Any task has to be enqueued before it get to execute on
  		 * a CPU. So account for the time it spent waiting on the
  		 * runqueue.
  		 */
  		update_stats_wait_end(cfs_rq, se);
  		__dequeue_entity(cfs_rq, se);
  	}
79303e9e0   Ingo Molnar   sched: remove the...
568
  	update_stats_curr_start(cfs_rq, se);
429d43bcc   Ingo Molnar   sched: cleanup: s...
569
  	cfs_rq->curr = se;
eba1ed4b7   Ingo Molnar   sched: debug: tra...
570
571
572
573
574
575
  #ifdef CONFIG_SCHEDSTATS
  	/*
  	 * Track our maximum slice length, if the CPU's load is at
  	 * least twice that of our own weight (i.e. dont track it
  	 * when there are only lesser-weight tasks around):
  	 */
495eca494   Dmitry Adamushko   sched: clean up s...
576
  	if (rq_of(cfs_rq)->load.weight >= 2*se->load.weight) {
eba1ed4b7   Ingo Molnar   sched: debug: tra...
577
578
579
580
  		se->slice_max = max(se->slice_max,
  			se->sum_exec_runtime - se->prev_sum_exec_runtime);
  	}
  #endif
4a55b4503   Peter Zijlstra   sched: improve pr...
581
  	se->prev_sum_exec_runtime = se->sum_exec_runtime;
bf0f6f24a   Ingo Molnar   sched: cfs core, ...
582
  }
9948f4b2a   Ingo Molnar   sched: remove the...
583
  static struct sched_entity *pick_next_entity(struct cfs_rq *cfs_rq)
bf0f6f24a   Ingo Molnar   sched: cfs core, ...
584
  {
08ec3df51   Dmitry Adamushko   sched: fix __pick...
585
  	struct sched_entity *se = NULL;
bf0f6f24a   Ingo Molnar   sched: cfs core, ...
586

08ec3df51   Dmitry Adamushko   sched: fix __pick...
587
588
589
590
  	if (first_fair(cfs_rq)) {
  		se = __pick_next_entity(cfs_rq);
  		set_next_entity(cfs_rq, se);
  	}
bf0f6f24a   Ingo Molnar   sched: cfs core, ...
591
592
593
  
  	return se;
  }
ab6cde269   Ingo Molnar   sched: remove the...
594
  static void put_prev_entity(struct cfs_rq *cfs_rq, struct sched_entity *prev)
bf0f6f24a   Ingo Molnar   sched: cfs core, ...
595
596
597
598
599
600
  {
  	/*
  	 * If still on the runqueue then deactivate_task()
  	 * was not called and update_curr() has to be done:
  	 */
  	if (prev->on_rq)
b7cc08965   Ingo Molnar   sched: remove the...
601
  		update_curr(cfs_rq);
bf0f6f24a   Ingo Molnar   sched: cfs core, ...
602

ddc972975   Peter Zijlstra   sched debug: chec...
603
  	check_spread(cfs_rq, prev);
30cfdcfc5   Dmitry Adamushko   sched: do not kee...
604
  	if (prev->on_rq) {
5870db5b8   Ingo Molnar   sched: remove the...
605
  		update_stats_wait_start(cfs_rq, prev);
30cfdcfc5   Dmitry Adamushko   sched: do not kee...
606
607
608
  		/* Put 'current' back into the tree. */
  		__enqueue_entity(cfs_rq, prev);
  	}
429d43bcc   Ingo Molnar   sched: cleanup: s...
609
  	cfs_rq->curr = NULL;
bf0f6f24a   Ingo Molnar   sched: cfs core, ...
610
611
612
613
  }
  
  static void entity_tick(struct cfs_rq *cfs_rq, struct sched_entity *curr)
  {
bf0f6f24a   Ingo Molnar   sched: cfs core, ...
614
  	/*
30cfdcfc5   Dmitry Adamushko   sched: do not kee...
615
  	 * Update run-time statistics of the 'current'.
bf0f6f24a   Ingo Molnar   sched: cfs core, ...
616
  	 */
30cfdcfc5   Dmitry Adamushko   sched: do not kee...
617
  	update_curr(cfs_rq);
bf0f6f24a   Ingo Molnar   sched: cfs core, ...
618

ce6c13113   Peter Zijlstra   sched: disable fo...
619
  	if (cfs_rq->nr_running > 1 || !sched_feat(WAKEUP_PREEMPT))
2e09bf556   Ingo Molnar   sched: wakeup gra...
620
  		check_preempt_tick(cfs_rq, curr);
bf0f6f24a   Ingo Molnar   sched: cfs core, ...
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
  }
  
  /**************************************************
   * CFS operations on tasks:
   */
  
  #ifdef CONFIG_FAIR_GROUP_SCHED
  
  /* Walk up scheduling entities hierarchy */
  #define for_each_sched_entity(se) \
  		for (; se; se = se->parent)
  
  static inline struct cfs_rq *task_cfs_rq(struct task_struct *p)
  {
  	return p->se.cfs_rq;
  }
  
  /* runqueue on which this entity is (to be) queued */
  static inline struct cfs_rq *cfs_rq_of(struct sched_entity *se)
  {
  	return se->cfs_rq;
  }
  
  /* runqueue "owned" by this group */
  static inline struct cfs_rq *group_cfs_rq(struct sched_entity *grp)
  {
  	return grp->my_q;
  }
  
  /* Given a group's cfs_rq on one cpu, return its corresponding cfs_rq on
   * another cpu ('this_cpu')
   */
  static inline struct cfs_rq *cpu_cfs_rq(struct cfs_rq *cfs_rq, int this_cpu)
  {
29f59db3a   Srivatsa Vaddagiri   sched: group-sche...
655
  	return cfs_rq->tg->cfs_rq[this_cpu];
bf0f6f24a   Ingo Molnar   sched: cfs core, ...
656
657
658
659
660
  }
  
  /* Iterate thr' all leaf cfs_rq's on a runqueue */
  #define for_each_leaf_cfs_rq(rq, cfs_rq) \
  	list_for_each_entry(cfs_rq, &rq->leaf_cfs_rq_list, leaf_cfs_rq_list)
fad095a7b   Srivatsa Vaddagiri   sched: group sche...
661
662
663
  /* Do the two (enqueued) entities belong to the same group ? */
  static inline int
  is_same_group(struct sched_entity *se, struct sched_entity *pse)
bf0f6f24a   Ingo Molnar   sched: cfs core, ...
664
  {
fad095a7b   Srivatsa Vaddagiri   sched: group sche...
665
  	if (se->cfs_rq == pse->cfs_rq)
bf0f6f24a   Ingo Molnar   sched: cfs core, ...
666
667
668
669
  		return 1;
  
  	return 0;
  }
fad095a7b   Srivatsa Vaddagiri   sched: group sche...
670
671
672
673
  static inline struct sched_entity *parent_entity(struct sched_entity *se)
  {
  	return se->parent;
  }
bf0f6f24a   Ingo Molnar   sched: cfs core, ...
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
  #else	/* CONFIG_FAIR_GROUP_SCHED */
  
  #define for_each_sched_entity(se) \
  		for (; se; se = NULL)
  
  static inline struct cfs_rq *task_cfs_rq(struct task_struct *p)
  {
  	return &task_rq(p)->cfs;
  }
  
  static inline struct cfs_rq *cfs_rq_of(struct sched_entity *se)
  {
  	struct task_struct *p = task_of(se);
  	struct rq *rq = task_rq(p);
  
  	return &rq->cfs;
  }
  
  /* runqueue "owned" by this group */
  static inline struct cfs_rq *group_cfs_rq(struct sched_entity *grp)
  {
  	return NULL;
  }
  
  static inline struct cfs_rq *cpu_cfs_rq(struct cfs_rq *cfs_rq, int this_cpu)
  {
  	return &cpu_rq(this_cpu)->cfs;
  }
  
  #define for_each_leaf_cfs_rq(rq, cfs_rq) \
  		for (cfs_rq = &rq->cfs; cfs_rq; cfs_rq = NULL)
fad095a7b   Srivatsa Vaddagiri   sched: group sche...
705
706
  static inline int
  is_same_group(struct sched_entity *se, struct sched_entity *pse)
bf0f6f24a   Ingo Molnar   sched: cfs core, ...
707
708
709
  {
  	return 1;
  }
fad095a7b   Srivatsa Vaddagiri   sched: group sche...
710
711
712
713
  static inline struct sched_entity *parent_entity(struct sched_entity *se)
  {
  	return NULL;
  }
bf0f6f24a   Ingo Molnar   sched: cfs core, ...
714
715
716
717
718
719
720
  #endif	/* CONFIG_FAIR_GROUP_SCHED */
  
  /*
   * The enqueue_task method is called before nr_running is
   * increased. Here we update the fair scheduling stats and
   * then put the task into the rbtree:
   */
fd390f6a0   Ingo Molnar   sched: remove the...
721
  static void enqueue_task_fair(struct rq *rq, struct task_struct *p, int wakeup)
bf0f6f24a   Ingo Molnar   sched: cfs core, ...
722
723
724
725
726
727
728
729
  {
  	struct cfs_rq *cfs_rq;
  	struct sched_entity *se = &p->se;
  
  	for_each_sched_entity(se) {
  		if (se->on_rq)
  			break;
  		cfs_rq = cfs_rq_of(se);
83b699ed2   Srivatsa Vaddagiri   sched: revert rec...
730
  		enqueue_entity(cfs_rq, se, wakeup);
b9fa3df33   Srivatsa Vaddagiri   sched: group sche...
731
  		wakeup = 1;
bf0f6f24a   Ingo Molnar   sched: cfs core, ...
732
733
734
735
736
737
738
739
  	}
  }
  
  /*
   * The dequeue_task method is called before nr_running is
   * decreased. We remove the task from the rbtree and
   * update the fair scheduling stats:
   */
f02231e51   Ingo Molnar   sched: remove the...
740
  static void dequeue_task_fair(struct rq *rq, struct task_struct *p, int sleep)
bf0f6f24a   Ingo Molnar   sched: cfs core, ...
741
742
743
744
745
746
  {
  	struct cfs_rq *cfs_rq;
  	struct sched_entity *se = &p->se;
  
  	for_each_sched_entity(se) {
  		cfs_rq = cfs_rq_of(se);
525c2716a   Ingo Molnar   sched: remove the...
747
  		dequeue_entity(cfs_rq, se, sleep);
bf0f6f24a   Ingo Molnar   sched: cfs core, ...
748
749
750
  		/* Don't dequeue parent if it has other entities besides us */
  		if (cfs_rq->load.weight)
  			break;
b9fa3df33   Srivatsa Vaddagiri   sched: group sche...
751
  		sleep = 1;
bf0f6f24a   Ingo Molnar   sched: cfs core, ...
752
753
754
755
  	}
  }
  
  /*
1799e35d5   Ingo Molnar   sched: add /proc/...
756
757
758
   * sched_yield() support is very simple - we dequeue and enqueue.
   *
   * If compat_yield is turned on then we requeue to the end of the tree.
bf0f6f24a   Ingo Molnar   sched: cfs core, ...
759
   */
4530d7ab0   Dmitry Adamushko   sched: simplify s...
760
  static void yield_task_fair(struct rq *rq)
bf0f6f24a   Ingo Molnar   sched: cfs core, ...
761
  {
db292ca30   Ingo Molnar   sched: default to...
762
763
764
  	struct task_struct *curr = rq->curr;
  	struct cfs_rq *cfs_rq = task_cfs_rq(curr);
  	struct sched_entity *rightmost, *se = &curr->se;
bf0f6f24a   Ingo Molnar   sched: cfs core, ...
765
766
  
  	/*
1799e35d5   Ingo Molnar   sched: add /proc/...
767
768
769
770
  	 * Are we the only task in the tree?
  	 */
  	if (unlikely(cfs_rq->nr_running == 1))
  		return;
db292ca30   Ingo Molnar   sched: default to...
771
  	if (likely(!sysctl_sched_compat_yield) && curr->policy != SCHED_BATCH) {
1799e35d5   Ingo Molnar   sched: add /proc/...
772
773
  		__update_rq_clock(rq);
  		/*
a2a2d6807   Dmitry Adamushko   sched: cleanup, m...
774
  		 * Update run-time statistics of the 'current'.
1799e35d5   Ingo Molnar   sched: add /proc/...
775
  		 */
2b1e315dd   Dmitry Adamushko   sched: yield fix
776
  		update_curr(cfs_rq);
1799e35d5   Ingo Molnar   sched: add /proc/...
777
778
779
780
781
  
  		return;
  	}
  	/*
  	 * Find the rightmost entry in the rbtree:
bf0f6f24a   Ingo Molnar   sched: cfs core, ...
782
  	 */
2b1e315dd   Dmitry Adamushko   sched: yield fix
783
  	rightmost = __pick_last_entity(cfs_rq);
1799e35d5   Ingo Molnar   sched: add /proc/...
784
785
786
  	/*
  	 * Already in the rightmost position?
  	 */
2b1e315dd   Dmitry Adamushko   sched: yield fix
787
  	if (unlikely(rightmost->vruntime < se->vruntime))
1799e35d5   Ingo Molnar   sched: add /proc/...
788
789
790
791
  		return;
  
  	/*
  	 * Minimally necessary key value to be last in the tree:
2b1e315dd   Dmitry Adamushko   sched: yield fix
792
793
  	 * Upon rescheduling, sched_class::put_prev_task() will place
  	 * 'current' within the tree based on its new key value.
1799e35d5   Ingo Molnar   sched: add /proc/...
794
  	 */
30cfdcfc5   Dmitry Adamushko   sched: do not kee...
795
  	se->vruntime = rightmost->vruntime + 1;
bf0f6f24a   Ingo Molnar   sched: cfs core, ...
796
797
798
799
800
  }
  
  /*
   * Preempt the current task with a newly woken task if needed:
   */
2e09bf556   Ingo Molnar   sched: wakeup gra...
801
  static void check_preempt_wakeup(struct rq *rq, struct task_struct *p)
bf0f6f24a   Ingo Molnar   sched: cfs core, ...
802
803
  {
  	struct task_struct *curr = rq->curr;
fad095a7b   Srivatsa Vaddagiri   sched: group sche...
804
  	struct cfs_rq *cfs_rq = task_cfs_rq(curr);
8651a86c3   Srivatsa Vaddagiri   sched: group sche...
805
  	struct sched_entity *se = &curr->se, *pse = &p->se;
502d26b52   Ingo Molnar   sched: clean up t...
806
  	unsigned long gran;
bf0f6f24a   Ingo Molnar   sched: cfs core, ...
807
808
  
  	if (unlikely(rt_prio(p->prio))) {
a8e504d2a   Ingo Molnar   sched: eliminate ...
809
  		update_rq_clock(rq);
b7cc08965   Ingo Molnar   sched: remove the...
810
  		update_curr(cfs_rq);
bf0f6f24a   Ingo Molnar   sched: cfs core, ...
811
812
813
  		resched_task(curr);
  		return;
  	}
91c234b4e   Ingo Molnar   sched: do not wak...
814
815
816
817
818
819
  	/*
  	 * Batch tasks do not preempt (their preemption is driven by
  	 * the tick):
  	 */
  	if (unlikely(p->policy == SCHED_BATCH))
  		return;
bf0f6f24a   Ingo Molnar   sched: cfs core, ...
820

77d9cc44b   Ingo Molnar   sched: clean up t...
821
822
  	if (!sched_feat(WAKEUP_PREEMPT))
  		return;
8651a86c3   Srivatsa Vaddagiri   sched: group sche...
823

77d9cc44b   Ingo Molnar   sched: clean up t...
824
825
826
  	while (!is_same_group(se, pse)) {
  		se = parent_entity(se);
  		pse = parent_entity(pse);
ce6c13113   Peter Zijlstra   sched: disable fo...
827
  	}
77d9cc44b   Ingo Molnar   sched: clean up t...
828

77d9cc44b   Ingo Molnar   sched: clean up t...
829
830
831
  	gran = sysctl_sched_wakeup_granularity;
  	if (unlikely(se->load.weight != NICE_0_LOAD))
  		gran = calc_delta_fair(gran, &se->load);
502d26b52   Ingo Molnar   sched: clean up t...
832
  	if (pse->vruntime + gran < se->vruntime)
77d9cc44b   Ingo Molnar   sched: clean up t...
833
  		resched_task(curr);
bf0f6f24a   Ingo Molnar   sched: cfs core, ...
834
  }
fb8d47240   Ingo Molnar   sched: remove the...
835
  static struct task_struct *pick_next_task_fair(struct rq *rq)
bf0f6f24a   Ingo Molnar   sched: cfs core, ...
836
837
838
839
840
841
842
843
  {
  	struct cfs_rq *cfs_rq = &rq->cfs;
  	struct sched_entity *se;
  
  	if (unlikely(!cfs_rq->nr_running))
  		return NULL;
  
  	do {
9948f4b2a   Ingo Molnar   sched: remove the...
844
  		se = pick_next_entity(cfs_rq);
bf0f6f24a   Ingo Molnar   sched: cfs core, ...
845
846
847
848
849
850
851
852
853
  		cfs_rq = group_cfs_rq(se);
  	} while (cfs_rq);
  
  	return task_of(se);
  }
  
  /*
   * Account for a descheduled task:
   */
31ee529cc   Ingo Molnar   sched: remove the...
854
  static void put_prev_task_fair(struct rq *rq, struct task_struct *prev)
bf0f6f24a   Ingo Molnar   sched: cfs core, ...
855
856
857
858
859
860
  {
  	struct sched_entity *se = &prev->se;
  	struct cfs_rq *cfs_rq;
  
  	for_each_sched_entity(se) {
  		cfs_rq = cfs_rq_of(se);
ab6cde269   Ingo Molnar   sched: remove the...
861
  		put_prev_entity(cfs_rq, se);
bf0f6f24a   Ingo Molnar   sched: cfs core, ...
862
863
  	}
  }
681f3e685   Peter Williams   sched: isolate SM...
864
  #ifdef CONFIG_SMP
bf0f6f24a   Ingo Molnar   sched: cfs core, ...
865
866
867
868
869
870
871
872
873
874
875
  /**************************************************
   * Fair scheduling class load-balancing methods:
   */
  
  /*
   * Load-balancing iterator. Note: while the runqueue stays locked
   * during the whole iteration, the current task might be
   * dequeued so the iterator has to be dequeue-safe. Here we
   * achieve that by always pre-iterating before returning
   * the current task:
   */
a9957449b   Alexey Dobriyan   sched: uninline s...
876
  static struct task_struct *
bf0f6f24a   Ingo Molnar   sched: cfs core, ...
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
  __load_balance_iterator(struct cfs_rq *cfs_rq, struct rb_node *curr)
  {
  	struct task_struct *p;
  
  	if (!curr)
  		return NULL;
  
  	p = rb_entry(curr, struct task_struct, se.run_node);
  	cfs_rq->rb_load_balance_curr = rb_next(curr);
  
  	return p;
  }
  
  static struct task_struct *load_balance_start_fair(void *arg)
  {
  	struct cfs_rq *cfs_rq = arg;
  
  	return __load_balance_iterator(cfs_rq, first_fair(cfs_rq));
  }
  
  static struct task_struct *load_balance_next_fair(void *arg)
  {
  	struct cfs_rq *cfs_rq = arg;
  
  	return __load_balance_iterator(cfs_rq, cfs_rq->rb_load_balance_curr);
  }
a4ac01c36   Peter Williams   sched: fix bug in...
903
  #ifdef CONFIG_FAIR_GROUP_SCHED
bf0f6f24a   Ingo Molnar   sched: cfs core, ...
904
905
906
907
908
909
910
  static int cfs_rq_best_prio(struct cfs_rq *cfs_rq)
  {
  	struct sched_entity *curr;
  	struct task_struct *p;
  
  	if (!cfs_rq->nr_running)
  		return MAX_PRIO;
9b5b77512   Srivatsa Vaddagiri   sched: clean up c...
911
912
913
  	curr = cfs_rq->curr;
  	if (!curr)
  		curr = __pick_next_entity(cfs_rq);
bf0f6f24a   Ingo Molnar   sched: cfs core, ...
914
915
916
917
  	p = task_of(curr);
  
  	return p->prio;
  }
a4ac01c36   Peter Williams   sched: fix bug in...
918
  #endif
bf0f6f24a   Ingo Molnar   sched: cfs core, ...
919

430106592   Peter Williams   sched: simplify m...
920
  static unsigned long
bf0f6f24a   Ingo Molnar   sched: cfs core, ...
921
  load_balance_fair(struct rq *this_rq, int this_cpu, struct rq *busiest,
e1d1484f7   Peter Williams   sched: reduce bal...
922
  		  unsigned long max_load_move,
a4ac01c36   Peter Williams   sched: fix bug in...
923
924
  		  struct sched_domain *sd, enum cpu_idle_type idle,
  		  int *all_pinned, int *this_best_prio)
bf0f6f24a   Ingo Molnar   sched: cfs core, ...
925
926
  {
  	struct cfs_rq *busy_cfs_rq;
bf0f6f24a   Ingo Molnar   sched: cfs core, ...
927
928
929
930
931
932
933
  	long rem_load_move = max_load_move;
  	struct rq_iterator cfs_rq_iterator;
  
  	cfs_rq_iterator.start = load_balance_start_fair;
  	cfs_rq_iterator.next = load_balance_next_fair;
  
  	for_each_leaf_cfs_rq(busiest, busy_cfs_rq) {
a4ac01c36   Peter Williams   sched: fix bug in...
934
  #ifdef CONFIG_FAIR_GROUP_SCHED
bf0f6f24a   Ingo Molnar   sched: cfs core, ...
935
  		struct cfs_rq *this_cfs_rq;
e56f31aad   Ingo Molnar   sched: fix typo i...
936
  		long imbalance;
bf0f6f24a   Ingo Molnar   sched: cfs core, ...
937
  		unsigned long maxload;
bf0f6f24a   Ingo Molnar   sched: cfs core, ...
938
939
  
  		this_cfs_rq = cpu_cfs_rq(busy_cfs_rq, this_cpu);
e56f31aad   Ingo Molnar   sched: fix typo i...
940
  		imbalance = busy_cfs_rq->load.weight - this_cfs_rq->load.weight;
bf0f6f24a   Ingo Molnar   sched: cfs core, ...
941
942
943
944
945
946
947
  		/* Don't pull if this_cfs_rq has more load than busy_cfs_rq */
  		if (imbalance <= 0)
  			continue;
  
  		/* Don't pull more than imbalance/2 */
  		imbalance /= 2;
  		maxload = min(rem_load_move, imbalance);
a4ac01c36   Peter Williams   sched: fix bug in...
948
949
  		*this_best_prio = cfs_rq_best_prio(this_cfs_rq);
  #else
e56f31aad   Ingo Molnar   sched: fix typo i...
950
  # define maxload rem_load_move
a4ac01c36   Peter Williams   sched: fix bug in...
951
  #endif
e1d1484f7   Peter Williams   sched: reduce bal...
952
953
  		/*
  		 * pass busy_cfs_rq argument into
bf0f6f24a   Ingo Molnar   sched: cfs core, ...
954
955
956
  		 * load_balance_[start|next]_fair iterators
  		 */
  		cfs_rq_iterator.arg = busy_cfs_rq;
e1d1484f7   Peter Williams   sched: reduce bal...
957
958
959
960
  		rem_load_move -= balance_tasks(this_rq, this_cpu, busiest,
  					       maxload, sd, idle, all_pinned,
  					       this_best_prio,
  					       &cfs_rq_iterator);
bf0f6f24a   Ingo Molnar   sched: cfs core, ...
961

e1d1484f7   Peter Williams   sched: reduce bal...
962
  		if (rem_load_move <= 0)
bf0f6f24a   Ingo Molnar   sched: cfs core, ...
963
964
  			break;
  	}
430106592   Peter Williams   sched: simplify m...
965
  	return max_load_move - rem_load_move;
bf0f6f24a   Ingo Molnar   sched: cfs core, ...
966
  }
e1d1484f7   Peter Williams   sched: reduce bal...
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
  static int
  move_one_task_fair(struct rq *this_rq, int this_cpu, struct rq *busiest,
  		   struct sched_domain *sd, enum cpu_idle_type idle)
  {
  	struct cfs_rq *busy_cfs_rq;
  	struct rq_iterator cfs_rq_iterator;
  
  	cfs_rq_iterator.start = load_balance_start_fair;
  	cfs_rq_iterator.next = load_balance_next_fair;
  
  	for_each_leaf_cfs_rq(busiest, busy_cfs_rq) {
  		/*
  		 * pass busy_cfs_rq argument into
  		 * load_balance_[start|next]_fair iterators
  		 */
  		cfs_rq_iterator.arg = busy_cfs_rq;
  		if (iter_move_one_task(this_rq, this_cpu, busiest, sd, idle,
  				       &cfs_rq_iterator))
  		    return 1;
  	}
  
  	return 0;
  }
681f3e685   Peter Williams   sched: isolate SM...
990
  #endif
e1d1484f7   Peter Williams   sched: reduce bal...
991

bf0f6f24a   Ingo Molnar   sched: cfs core, ...
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
  /*
   * scheduler tick hitting a task of our scheduling class:
   */
  static void task_tick_fair(struct rq *rq, struct task_struct *curr)
  {
  	struct cfs_rq *cfs_rq;
  	struct sched_entity *se = &curr->se;
  
  	for_each_sched_entity(se) {
  		cfs_rq = cfs_rq_of(se);
  		entity_tick(cfs_rq, se);
  	}
  }
8eb172d94   Ingo Molnar   sched: fix style ...
1005
  #define swap(a, b) do { typeof(a) tmp = (a); (a) = (b); (b) = tmp; } while (0)
4d78e7b65   Peter Zijlstra   sched: new task p...
1006

bf0f6f24a   Ingo Molnar   sched: cfs core, ...
1007
1008
1009
1010
1011
1012
1013
  /*
   * Share the fairness runtime between parent and child, thus the
   * total amount of pressure for CPU stays equal - new tasks
   * get a chance to run but frequent forkers are not allowed to
   * monopolize the CPU. Note: the parent runqueue is locked,
   * the child is not running yet.
   */
ee0827d8b   Ingo Molnar   sched: remove the...
1014
  static void task_new_fair(struct rq *rq, struct task_struct *p)
bf0f6f24a   Ingo Molnar   sched: cfs core, ...
1015
1016
  {
  	struct cfs_rq *cfs_rq = task_cfs_rq(p);
429d43bcc   Ingo Molnar   sched: cleanup: s...
1017
  	struct sched_entity *se = &p->se, *curr = cfs_rq->curr;
00bf7bfc2   Ingo Molnar   sched: fix: move ...
1018
  	int this_cpu = smp_processor_id();
bf0f6f24a   Ingo Molnar   sched: cfs core, ...
1019
1020
  
  	sched_info_queued(p);
7109c4429   Ting Yang   sched: call updat...
1021
  	update_curr(cfs_rq);
aeb73b040   Peter Zijlstra   sched: clean up n...
1022
  	place_entity(cfs_rq, se, 1);
4d78e7b65   Peter Zijlstra   sched: new task p...
1023

3c90e6e99   Srivatsa Vaddagiri   sched: fix copy_n...
1024
  	/* 'curr' will be NULL if the child belongs to a different group */
00bf7bfc2   Ingo Molnar   sched: fix: move ...
1025
  	if (sysctl_sched_child_runs_first && this_cpu == task_cpu(p) &&
3c90e6e99   Srivatsa Vaddagiri   sched: fix copy_n...
1026
  			curr && curr->vruntime < se->vruntime) {
87fefa381   Dmitry Adamushko   sched: optimize t...
1027
  		/*
edcb60a30   Ingo Molnar   sched: kernel/sch...
1028
1029
1030
  		 * Upon rescheduling, sched_class::put_prev_task() will place
  		 * 'current' within the tree based on its new key value.
  		 */
4d78e7b65   Peter Zijlstra   sched: new task p...
1031
  		swap(curr->vruntime, se->vruntime);
4d78e7b65   Peter Zijlstra   sched: new task p...
1032
  	}
bf0f6f24a   Ingo Molnar   sched: cfs core, ...
1033

b9dca1e0f   Srivatsa Vaddagiri   sched: fix new ta...
1034
  	enqueue_task_fair(rq, p, 0);
bb61c2108   Ingo Molnar   sched: resched ta...
1035
  	resched_task(rq->curr);
bf0f6f24a   Ingo Molnar   sched: cfs core, ...
1036
  }
83b699ed2   Srivatsa Vaddagiri   sched: revert rec...
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
  /* Account for a task changing its policy or group.
   *
   * This routine is mostly called to set cfs_rq->curr field when a task
   * migrates between groups/classes.
   */
  static void set_curr_task_fair(struct rq *rq)
  {
  	struct sched_entity *se = &rq->curr->se;
  
  	for_each_sched_entity(se)
  		set_next_entity(cfs_rq_of(se), se);
  }
bf0f6f24a   Ingo Molnar   sched: cfs core, ...
1049
1050
1051
  /*
   * All the scheduling class methods:
   */
5522d5d5f   Ingo Molnar   sched: mark sched...
1052
1053
  static const struct sched_class fair_sched_class = {
  	.next			= &idle_sched_class,
bf0f6f24a   Ingo Molnar   sched: cfs core, ...
1054
1055
1056
  	.enqueue_task		= enqueue_task_fair,
  	.dequeue_task		= dequeue_task_fair,
  	.yield_task		= yield_task_fair,
2e09bf556   Ingo Molnar   sched: wakeup gra...
1057
  	.check_preempt_curr	= check_preempt_wakeup,
bf0f6f24a   Ingo Molnar   sched: cfs core, ...
1058
1059
1060
  
  	.pick_next_task		= pick_next_task_fair,
  	.put_prev_task		= put_prev_task_fair,
681f3e685   Peter Williams   sched: isolate SM...
1061
  #ifdef CONFIG_SMP
bf0f6f24a   Ingo Molnar   sched: cfs core, ...
1062
  	.load_balance		= load_balance_fair,
e1d1484f7   Peter Williams   sched: reduce bal...
1063
  	.move_one_task		= move_one_task_fair,
681f3e685   Peter Williams   sched: isolate SM...
1064
  #endif
bf0f6f24a   Ingo Molnar   sched: cfs core, ...
1065

83b699ed2   Srivatsa Vaddagiri   sched: revert rec...
1066
  	.set_curr_task          = set_curr_task_fair,
bf0f6f24a   Ingo Molnar   sched: cfs core, ...
1067
1068
1069
1070
1071
  	.task_tick		= task_tick_fair,
  	.task_new		= task_new_fair,
  };
  
  #ifdef CONFIG_SCHED_DEBUG
5cef9eca3   Ingo Molnar   sched: remove the...
1072
  static void print_cfs_stats(struct seq_file *m, int cpu)
bf0f6f24a   Ingo Molnar   sched: cfs core, ...
1073
  {
bf0f6f24a   Ingo Molnar   sched: cfs core, ...
1074
  	struct cfs_rq *cfs_rq;
75c28ace9   Srivatsa Vaddagiri   sched: print &rq-...
1075
1076
1077
  #ifdef CONFIG_FAIR_GROUP_SCHED
  	print_cfs_rq(m, cpu, &cpu_rq(cpu)->cfs);
  #endif
c3b64f1e4   Ingo Molnar   sched: clean up s...
1078
  	for_each_leaf_cfs_rq(cpu_rq(cpu), cfs_rq)
5cef9eca3   Ingo Molnar   sched: remove the...
1079
  		print_cfs_rq(m, cpu, cfs_rq);
bf0f6f24a   Ingo Molnar   sched: cfs core, ...
1080
1081
  }
  #endif