]>
Commit | Line | Data |
---|---|---|
1 | /* | |
2 | * kernel/sched.c | |
3 | * | |
4 | * Kernel scheduler and related syscalls | |
5 | * | |
6 | * Copyright (C) 1991-2002 Linus Torvalds | |
7 | * | |
8 | * 1996-12-23 Modified by Dave Grothe to fix bugs in semaphores and | |
9 | * make semaphores SMP safe | |
10 | * 1998-11-19 Implemented schedule_timeout() and related stuff | |
11 | * by Andrea Arcangeli | |
12 | * 2002-01-04 New ultra-scalable O(1) scheduler by Ingo Molnar: | |
13 | * hybrid priority-list and round-robin design with | |
14 | * an array-switch method of distributing timeslices | |
15 | * and per-CPU runqueues. Cleanups and useful suggestions | |
16 | * by Davide Libenzi, preemptible kernel bits by Robert Love. | |
17 | * 2003-09-03 Interactivity tuning by Con Kolivas. | |
18 | * 2004-04-02 Scheduler domains code by Nick Piggin | |
19 | * 2007-04-15 Work begun on replacing all interactivity tuning with a | |
20 | * fair scheduling design by Con Kolivas. | |
21 | * 2007-05-05 Load balancing (smp-nice) and other improvements | |
22 | * by Peter Williams | |
23 | * 2007-05-06 Interactivity improvements to CFS by Mike Galbraith | |
24 | * 2007-07-01 Group scheduling enhancements by Srivatsa Vaddagiri | |
25 | * 2007-11-29 RT balancing improvements by Steven Rostedt, Gregory Haskins, | |
26 | * Thomas Gleixner, Mike Kravetz | |
27 | */ | |
28 | ||
29 | #include <linux/mm.h> | |
30 | #include <linux/module.h> | |
31 | #include <linux/nmi.h> | |
32 | #include <linux/init.h> | |
33 | #include <linux/uaccess.h> | |
34 | #include <linux/highmem.h> | |
35 | #include <linux/smp_lock.h> | |
36 | #include <asm/mmu_context.h> | |
37 | #include <linux/interrupt.h> | |
38 | #include <linux/capability.h> | |
39 | #include <linux/completion.h> | |
40 | #include <linux/kernel_stat.h> | |
41 | #include <linux/debug_locks.h> | |
42 | #include <linux/security.h> | |
43 | #include <linux/notifier.h> | |
44 | #include <linux/profile.h> | |
45 | #include <linux/freezer.h> | |
46 | #include <linux/vmalloc.h> | |
47 | #include <linux/blkdev.h> | |
48 | #include <linux/delay.h> | |
49 | #include <linux/pid_namespace.h> | |
50 | #include <linux/smp.h> | |
51 | #include <linux/threads.h> | |
52 | #include <linux/timer.h> | |
53 | #include <linux/rcupdate.h> | |
54 | #include <linux/cpu.h> | |
55 | #include <linux/cpuset.h> | |
56 | #include <linux/percpu.h> | |
57 | #include <linux/kthread.h> | |
58 | #include <linux/proc_fs.h> | |
59 | #include <linux/seq_file.h> | |
60 | #include <linux/sysctl.h> | |
61 | #include <linux/syscalls.h> | |
62 | #include <linux/times.h> | |
63 | #include <linux/tsacct_kern.h> | |
64 | #include <linux/kprobes.h> | |
65 | #include <linux/delayacct.h> | |
66 | #include <linux/reciprocal_div.h> | |
67 | #include <linux/unistd.h> | |
68 | #include <linux/pagemap.h> | |
69 | #include <linux/hrtimer.h> | |
70 | #include <linux/tick.h> | |
71 | #include <linux/debugfs.h> | |
72 | #include <linux/ctype.h> | |
73 | #include <linux/ftrace.h> | |
74 | ||
75 | #include <asm/tlb.h> | |
76 | #include <asm/irq_regs.h> | |
77 | ||
78 | #include "sched_cpupri.h" | |
79 | ||
80 | #define CREATE_TRACE_POINTS | |
81 | #include <trace/events/sched.h> | |
82 | ||
83 | /* | |
84 | * Convert user-nice values [ -20 ... 0 ... 19 ] | |
85 | * to static priority [ MAX_RT_PRIO..MAX_PRIO-1 ], | |
86 | * and back. | |
87 | */ | |
88 | #define NICE_TO_PRIO(nice) (MAX_RT_PRIO + (nice) + 20) | |
89 | #define PRIO_TO_NICE(prio) ((prio) - MAX_RT_PRIO - 20) | |
90 | #define TASK_NICE(p) PRIO_TO_NICE((p)->static_prio) | |
91 | ||
92 | /* | |
93 | * 'User priority' is the nice value converted to something we | |
94 | * can work with better when scaling various scheduler parameters, | |
95 | * it's a [ 0 ... 39 ] range. | |
96 | */ | |
97 | #define USER_PRIO(p) ((p)-MAX_RT_PRIO) | |
98 | #define TASK_USER_PRIO(p) USER_PRIO((p)->static_prio) | |
99 | #define MAX_USER_PRIO (USER_PRIO(MAX_PRIO)) | |
100 | ||
101 | /* | |
102 | * Helpers for converting nanosecond timing to jiffy resolution | |
103 | */ | |
104 | #define NS_TO_JIFFIES(TIME) ((unsigned long)(TIME) / (NSEC_PER_SEC / HZ)) | |
105 | ||
106 | #define NICE_0_LOAD SCHED_LOAD_SCALE | |
107 | #define NICE_0_SHIFT SCHED_LOAD_SHIFT | |
108 | ||
109 | /* | |
110 | * These are the 'tuning knobs' of the scheduler: | |
111 | * | |
112 | * default timeslice is 100 msecs (used only for SCHED_RR tasks). | |
113 | * Timeslices get refilled after they expire. | |
114 | */ | |
115 | #define DEF_TIMESLICE (100 * HZ / 1000) | |
116 | ||
117 | /* | |
118 | * single value that denotes runtime == period, ie unlimited time. | |
119 | */ | |
120 | #define RUNTIME_INF ((u64)~0ULL) | |
121 | ||
122 | #ifdef CONFIG_SMP | |
123 | ||
124 | static void double_rq_lock(struct rq *rq1, struct rq *rq2); | |
125 | ||
126 | /* | |
127 | * Divide a load by a sched group cpu_power : (load / sg->__cpu_power) | |
128 | * Since cpu_power is a 'constant', we can use a reciprocal divide. | |
129 | */ | |
130 | static inline u32 sg_div_cpu_power(const struct sched_group *sg, u32 load) | |
131 | { | |
132 | return reciprocal_divide(load, sg->reciprocal_cpu_power); | |
133 | } | |
134 | ||
135 | /* | |
136 | * Each time a sched group cpu_power is changed, | |
137 | * we must compute its reciprocal value | |
138 | */ | |
139 | static inline void sg_inc_cpu_power(struct sched_group *sg, u32 val) | |
140 | { | |
141 | sg->__cpu_power += val; | |
142 | sg->reciprocal_cpu_power = reciprocal_value(sg->__cpu_power); | |
143 | } | |
144 | #endif | |
145 | ||
146 | static inline int rt_policy(int policy) | |
147 | { | |
148 | if (unlikely(policy == SCHED_FIFO || policy == SCHED_RR)) | |
149 | return 1; | |
150 | return 0; | |
151 | } | |
152 | ||
153 | static inline int task_has_rt_policy(struct task_struct *p) | |
154 | { | |
155 | return rt_policy(p->policy); | |
156 | } | |
157 | ||
158 | /* | |
159 | * This is the priority-queue data structure of the RT scheduling class: | |
160 | */ | |
161 | struct rt_prio_array { | |
162 | DECLARE_BITMAP(bitmap, MAX_RT_PRIO+1); /* include 1 bit for delimiter */ | |
163 | struct list_head queue[MAX_RT_PRIO]; | |
164 | }; | |
165 | ||
166 | struct rt_bandwidth { | |
167 | /* nests inside the rq lock: */ | |
168 | spinlock_t rt_runtime_lock; | |
169 | ktime_t rt_period; | |
170 | u64 rt_runtime; | |
171 | struct hrtimer rt_period_timer; | |
172 | }; | |
173 | ||
174 | static struct rt_bandwidth def_rt_bandwidth; | |
175 | ||
176 | static int do_sched_rt_period_timer(struct rt_bandwidth *rt_b, int overrun); | |
177 | ||
178 | static enum hrtimer_restart sched_rt_period_timer(struct hrtimer *timer) | |
179 | { | |
180 | struct rt_bandwidth *rt_b = | |
181 | container_of(timer, struct rt_bandwidth, rt_period_timer); | |
182 | ktime_t now; | |
183 | int overrun; | |
184 | int idle = 0; | |
185 | ||
186 | for (;;) { | |
187 | now = hrtimer_cb_get_time(timer); | |
188 | overrun = hrtimer_forward(timer, now, rt_b->rt_period); | |
189 | ||
190 | if (!overrun) | |
191 | break; | |
192 | ||
193 | idle = do_sched_rt_period_timer(rt_b, overrun); | |
194 | } | |
195 | ||
196 | return idle ? HRTIMER_NORESTART : HRTIMER_RESTART; | |
197 | } | |
198 | ||
199 | static | |
200 | void init_rt_bandwidth(struct rt_bandwidth *rt_b, u64 period, u64 runtime) | |
201 | { | |
202 | rt_b->rt_period = ns_to_ktime(period); | |
203 | rt_b->rt_runtime = runtime; | |
204 | ||
205 | spin_lock_init(&rt_b->rt_runtime_lock); | |
206 | ||
207 | hrtimer_init(&rt_b->rt_period_timer, | |
208 | CLOCK_MONOTONIC, HRTIMER_MODE_REL); | |
209 | rt_b->rt_period_timer.function = sched_rt_period_timer; | |
210 | } | |
211 | ||
212 | static inline int rt_bandwidth_enabled(void) | |
213 | { | |
214 | return sysctl_sched_rt_runtime >= 0; | |
215 | } | |
216 | ||
217 | static void start_rt_bandwidth(struct rt_bandwidth *rt_b) | |
218 | { | |
219 | ktime_t now; | |
220 | ||
221 | if (!rt_bandwidth_enabled() || rt_b->rt_runtime == RUNTIME_INF) | |
222 | return; | |
223 | ||
224 | if (hrtimer_active(&rt_b->rt_period_timer)) | |
225 | return; | |
226 | ||
227 | spin_lock(&rt_b->rt_runtime_lock); | |
228 | for (;;) { | |
229 | unsigned long delta; | |
230 | ktime_t soft, hard; | |
231 | ||
232 | if (hrtimer_active(&rt_b->rt_period_timer)) | |
233 | break; | |
234 | ||
235 | now = hrtimer_cb_get_time(&rt_b->rt_period_timer); | |
236 | hrtimer_forward(&rt_b->rt_period_timer, now, rt_b->rt_period); | |
237 | ||
238 | soft = hrtimer_get_softexpires(&rt_b->rt_period_timer); | |
239 | hard = hrtimer_get_expires(&rt_b->rt_period_timer); | |
240 | delta = ktime_to_ns(ktime_sub(hard, soft)); | |
241 | __hrtimer_start_range_ns(&rt_b->rt_period_timer, soft, delta, | |
242 | HRTIMER_MODE_ABS, 0); | |
243 | } | |
244 | spin_unlock(&rt_b->rt_runtime_lock); | |
245 | } | |
246 | ||
247 | #ifdef CONFIG_RT_GROUP_SCHED | |
248 | static void destroy_rt_bandwidth(struct rt_bandwidth *rt_b) | |
249 | { | |
250 | hrtimer_cancel(&rt_b->rt_period_timer); | |
251 | } | |
252 | #endif | |
253 | ||
254 | /* | |
255 | * sched_domains_mutex serializes calls to arch_init_sched_domains, | |
256 | * detach_destroy_domains and partition_sched_domains. | |
257 | */ | |
258 | static DEFINE_MUTEX(sched_domains_mutex); | |
259 | ||
260 | #ifdef CONFIG_GROUP_SCHED | |
261 | ||
262 | #include <linux/cgroup.h> | |
263 | ||
264 | struct cfs_rq; | |
265 | ||
266 | static LIST_HEAD(task_groups); | |
267 | ||
268 | /* task group related information */ | |
269 | struct task_group { | |
270 | #ifdef CONFIG_CGROUP_SCHED | |
271 | struct cgroup_subsys_state css; | |
272 | #endif | |
273 | ||
274 | #ifdef CONFIG_USER_SCHED | |
275 | uid_t uid; | |
276 | #endif | |
277 | ||
278 | #ifdef CONFIG_FAIR_GROUP_SCHED | |
279 | /* schedulable entities of this group on each cpu */ | |
280 | struct sched_entity **se; | |
281 | /* runqueue "owned" by this group on each cpu */ | |
282 | struct cfs_rq **cfs_rq; | |
283 | unsigned long shares; | |
284 | #endif | |
285 | ||
286 | #ifdef CONFIG_RT_GROUP_SCHED | |
287 | struct sched_rt_entity **rt_se; | |
288 | struct rt_rq **rt_rq; | |
289 | ||
290 | struct rt_bandwidth rt_bandwidth; | |
291 | #endif | |
292 | ||
293 | struct rcu_head rcu; | |
294 | struct list_head list; | |
295 | ||
296 | struct task_group *parent; | |
297 | struct list_head siblings; | |
298 | struct list_head children; | |
299 | }; | |
300 | ||
301 | #ifdef CONFIG_USER_SCHED | |
302 | ||
303 | /* Helper function to pass uid information to create_sched_user() */ | |
304 | void set_tg_uid(struct user_struct *user) | |
305 | { | |
306 | user->tg->uid = user->uid; | |
307 | } | |
308 | ||
309 | /* | |
310 | * Root task group. | |
311 | * Every UID task group (including init_task_group aka UID-0) will | |
312 | * be a child to this group. | |
313 | */ | |
314 | struct task_group root_task_group; | |
315 | ||
316 | #ifdef CONFIG_FAIR_GROUP_SCHED | |
317 | /* Default task group's sched entity on each cpu */ | |
318 | static DEFINE_PER_CPU(struct sched_entity, init_sched_entity); | |
319 | /* Default task group's cfs_rq on each cpu */ | |
320 | static DEFINE_PER_CPU(struct cfs_rq, init_cfs_rq) ____cacheline_aligned_in_smp; | |
321 | #endif /* CONFIG_FAIR_GROUP_SCHED */ | |
322 | ||
323 | #ifdef CONFIG_RT_GROUP_SCHED | |
324 | static DEFINE_PER_CPU(struct sched_rt_entity, init_sched_rt_entity); | |
325 | static DEFINE_PER_CPU(struct rt_rq, init_rt_rq) ____cacheline_aligned_in_smp; | |
326 | #endif /* CONFIG_RT_GROUP_SCHED */ | |
327 | #else /* !CONFIG_USER_SCHED */ | |
328 | #define root_task_group init_task_group | |
329 | #endif /* CONFIG_USER_SCHED */ | |
330 | ||
331 | /* task_group_lock serializes add/remove of task groups and also changes to | |
332 | * a task group's cpu shares. | |
333 | */ | |
334 | static DEFINE_SPINLOCK(task_group_lock); | |
335 | ||
336 | #ifdef CONFIG_SMP | |
337 | static int root_task_group_empty(void) | |
338 | { | |
339 | return list_empty(&root_task_group.children); | |
340 | } | |
341 | #endif | |
342 | ||
343 | #ifdef CONFIG_FAIR_GROUP_SCHED | |
344 | #ifdef CONFIG_USER_SCHED | |
345 | # define INIT_TASK_GROUP_LOAD (2*NICE_0_LOAD) | |
346 | #else /* !CONFIG_USER_SCHED */ | |
347 | # define INIT_TASK_GROUP_LOAD NICE_0_LOAD | |
348 | #endif /* CONFIG_USER_SCHED */ | |
349 | ||
350 | /* | |
351 | * A weight of 0 or 1 can cause arithmetics problems. | |
352 | * A weight of a cfs_rq is the sum of weights of which entities | |
353 | * are queued on this cfs_rq, so a weight of a entity should not be | |
354 | * too large, so as the shares value of a task group. | |
355 | * (The default weight is 1024 - so there's no practical | |
356 | * limitation from this.) | |
357 | */ | |
358 | #define MIN_SHARES 2 | |
359 | #define MAX_SHARES (1UL << 18) | |
360 | ||
361 | static int init_task_group_load = INIT_TASK_GROUP_LOAD; | |
362 | #endif | |
363 | ||
364 | /* Default task group. | |
365 | * Every task in system belong to this group at bootup. | |
366 | */ | |
367 | struct task_group init_task_group; | |
368 | ||
369 | /* return group to which a task belongs */ | |
370 | static inline struct task_group *task_group(struct task_struct *p) | |
371 | { | |
372 | struct task_group *tg; | |
373 | ||
374 | #ifdef CONFIG_USER_SCHED | |
375 | rcu_read_lock(); | |
376 | tg = __task_cred(p)->user->tg; | |
377 | rcu_read_unlock(); | |
378 | #elif defined(CONFIG_CGROUP_SCHED) | |
379 | tg = container_of(task_subsys_state(p, cpu_cgroup_subsys_id), | |
380 | struct task_group, css); | |
381 | #else | |
382 | tg = &init_task_group; | |
383 | #endif | |
384 | return tg; | |
385 | } | |
386 | ||
387 | /* Change a task's cfs_rq and parent entity if it moves across CPUs/groups */ | |
388 | static inline void set_task_rq(struct task_struct *p, unsigned int cpu) | |
389 | { | |
390 | #ifdef CONFIG_FAIR_GROUP_SCHED | |
391 | p->se.cfs_rq = task_group(p)->cfs_rq[cpu]; | |
392 | p->se.parent = task_group(p)->se[cpu]; | |
393 | #endif | |
394 | ||
395 | #ifdef CONFIG_RT_GROUP_SCHED | |
396 | p->rt.rt_rq = task_group(p)->rt_rq[cpu]; | |
397 | p->rt.parent = task_group(p)->rt_se[cpu]; | |
398 | #endif | |
399 | } | |
400 | ||
401 | #else | |
402 | ||
403 | #ifdef CONFIG_SMP | |
404 | static int root_task_group_empty(void) | |
405 | { | |
406 | return 1; | |
407 | } | |
408 | #endif | |
409 | ||
410 | static inline void set_task_rq(struct task_struct *p, unsigned int cpu) { } | |
411 | static inline struct task_group *task_group(struct task_struct *p) | |
412 | { | |
413 | return NULL; | |
414 | } | |
415 | ||
416 | #endif /* CONFIG_GROUP_SCHED */ | |
417 | ||
418 | /* CFS-related fields in a runqueue */ | |
419 | struct cfs_rq { | |
420 | struct load_weight load; | |
421 | unsigned long nr_running; | |
422 | ||
423 | u64 exec_clock; | |
424 | u64 min_vruntime; | |
425 | ||
426 | struct rb_root tasks_timeline; | |
427 | struct rb_node *rb_leftmost; | |
428 | ||
429 | struct list_head tasks; | |
430 | struct list_head *balance_iterator; | |
431 | ||
432 | /* | |
433 | * 'curr' points to currently running entity on this cfs_rq. | |
434 | * It is set to NULL otherwise (i.e when none are currently running). | |
435 | */ | |
436 | struct sched_entity *curr, *next, *last; | |
437 | ||
438 | unsigned int nr_spread_over; | |
439 | ||
440 | #ifdef CONFIG_FAIR_GROUP_SCHED | |
441 | struct rq *rq; /* cpu runqueue to which this cfs_rq is attached */ | |
442 | ||
443 | /* | |
444 | * leaf cfs_rqs are those that hold tasks (lowest schedulable entity in | |
445 | * a hierarchy). Non-leaf lrqs hold other higher schedulable entities | |
446 | * (like users, containers etc.) | |
447 | * | |
448 | * leaf_cfs_rq_list ties together list of leaf cfs_rq's in a cpu. This | |
449 | * list is used during load balance. | |
450 | */ | |
451 | struct list_head leaf_cfs_rq_list; | |
452 | struct task_group *tg; /* group that "owns" this runqueue */ | |
453 | ||
454 | #ifdef CONFIG_SMP | |
455 | /* | |
456 | * the part of load.weight contributed by tasks | |
457 | */ | |
458 | unsigned long task_weight; | |
459 | ||
460 | /* | |
461 | * h_load = weight * f(tg) | |
462 | * | |
463 | * Where f(tg) is the recursive weight fraction assigned to | |
464 | * this group. | |
465 | */ | |
466 | unsigned long h_load; | |
467 | ||
468 | /* | |
469 | * this cpu's part of tg->shares | |
470 | */ | |
471 | unsigned long shares; | |
472 | ||
473 | /* | |
474 | * load.weight at the time we set shares | |
475 | */ | |
476 | unsigned long rq_weight; | |
477 | #endif | |
478 | #endif | |
479 | }; | |
480 | ||
481 | /* Real-Time classes' related field in a runqueue: */ | |
482 | struct rt_rq { | |
483 | struct rt_prio_array active; | |
484 | unsigned long rt_nr_running; | |
485 | #if defined CONFIG_SMP || defined CONFIG_RT_GROUP_SCHED | |
486 | struct { | |
487 | int curr; /* highest queued rt task prio */ | |
488 | #ifdef CONFIG_SMP | |
489 | int next; /* next highest */ | |
490 | #endif | |
491 | } highest_prio; | |
492 | #endif | |
493 | #ifdef CONFIG_SMP | |
494 | unsigned long rt_nr_migratory; | |
495 | int overloaded; | |
496 | struct plist_head pushable_tasks; | |
497 | #endif | |
498 | int rt_throttled; | |
499 | u64 rt_time; | |
500 | u64 rt_runtime; | |
501 | /* Nests inside the rq lock: */ | |
502 | spinlock_t rt_runtime_lock; | |
503 | ||
504 | #ifdef CONFIG_RT_GROUP_SCHED | |
505 | unsigned long rt_nr_boosted; | |
506 | ||
507 | struct rq *rq; | |
508 | struct list_head leaf_rt_rq_list; | |
509 | struct task_group *tg; | |
510 | struct sched_rt_entity *rt_se; | |
511 | #endif | |
512 | }; | |
513 | ||
514 | #ifdef CONFIG_SMP | |
515 | ||
516 | /* | |
517 | * We add the notion of a root-domain which will be used to define per-domain | |
518 | * variables. Each exclusive cpuset essentially defines an island domain by | |
519 | * fully partitioning the member cpus from any other cpuset. Whenever a new | |
520 | * exclusive cpuset is created, we also create and attach a new root-domain | |
521 | * object. | |
522 | * | |
523 | */ | |
524 | struct root_domain { | |
525 | atomic_t refcount; | |
526 | cpumask_var_t span; | |
527 | cpumask_var_t online; | |
528 | ||
529 | /* | |
530 | * The "RT overload" flag: it gets set if a CPU has more than | |
531 | * one runnable RT task. | |
532 | */ | |
533 | cpumask_var_t rto_mask; | |
534 | atomic_t rto_count; | |
535 | #ifdef CONFIG_SMP | |
536 | struct cpupri cpupri; | |
537 | #endif | |
538 | #if defined(CONFIG_SCHED_MC) || defined(CONFIG_SCHED_SMT) | |
539 | /* | |
540 | * Preferred wake up cpu nominated by sched_mc balance that will be | |
541 | * used when most cpus are idle in the system indicating overall very | |
542 | * low system utilisation. Triggered at POWERSAVINGS_BALANCE_WAKEUP(2) | |
543 | */ | |
544 | unsigned int sched_mc_preferred_wakeup_cpu; | |
545 | #endif | |
546 | }; | |
547 | ||
548 | /* | |
549 | * By default the system creates a single root-domain with all cpus as | |
550 | * members (mimicking the global state we have today). | |
551 | */ | |
552 | static struct root_domain def_root_domain; | |
553 | ||
554 | #endif | |
555 | ||
556 | /* | |
557 | * This is the main, per-CPU runqueue data structure. | |
558 | * | |
559 | * Locking rule: those places that want to lock multiple runqueues | |
560 | * (such as the load balancing or the thread migration code), lock | |
561 | * acquire operations must be ordered by ascending &runqueue. | |
562 | */ | |
563 | struct rq { | |
564 | /* runqueue lock: */ | |
565 | spinlock_t lock; | |
566 | ||
567 | /* | |
568 | * nr_running and cpu_load should be in the same cacheline because | |
569 | * remote CPUs use both these fields when doing load calculation. | |
570 | */ | |
571 | unsigned long nr_running; | |
572 | #define CPU_LOAD_IDX_MAX 5 | |
573 | unsigned long cpu_load[CPU_LOAD_IDX_MAX]; | |
574 | #ifdef CONFIG_NO_HZ | |
575 | unsigned long last_tick_seen; | |
576 | unsigned char in_nohz_recently; | |
577 | #endif | |
578 | /* capture load from *all* tasks on this cpu: */ | |
579 | struct load_weight load; | |
580 | unsigned long nr_load_updates; | |
581 | u64 nr_switches; | |
582 | ||
583 | struct cfs_rq cfs; | |
584 | struct rt_rq rt; | |
585 | ||
586 | #ifdef CONFIG_FAIR_GROUP_SCHED | |
587 | /* list of leaf cfs_rq on this cpu: */ | |
588 | struct list_head leaf_cfs_rq_list; | |
589 | #endif | |
590 | #ifdef CONFIG_RT_GROUP_SCHED | |
591 | struct list_head leaf_rt_rq_list; | |
592 | #endif | |
593 | ||
594 | /* | |
595 | * This is part of a global counter where only the total sum | |
596 | * over all CPUs matters. A task can increase this counter on | |
597 | * one CPU and if it got migrated afterwards it may decrease | |
598 | * it on another CPU. Always updated under the runqueue lock: | |
599 | */ | |
600 | unsigned long nr_uninterruptible; | |
601 | ||
602 | struct task_struct *curr, *idle; | |
603 | unsigned long next_balance; | |
604 | struct mm_struct *prev_mm; | |
605 | ||
606 | u64 clock; | |
607 | ||
608 | atomic_t nr_iowait; | |
609 | ||
610 | #ifdef CONFIG_SMP | |
611 | struct root_domain *rd; | |
612 | struct sched_domain *sd; | |
613 | ||
614 | unsigned char idle_at_tick; | |
615 | /* For active balancing */ | |
616 | int active_balance; | |
617 | int push_cpu; | |
618 | /* cpu of this runqueue: */ | |
619 | int cpu; | |
620 | int online; | |
621 | ||
622 | unsigned long avg_load_per_task; | |
623 | ||
624 | struct task_struct *migration_thread; | |
625 | struct list_head migration_queue; | |
626 | #endif | |
627 | ||
628 | /* calc_load related fields */ | |
629 | unsigned long calc_load_update; | |
630 | long calc_load_active; | |
631 | ||
632 | #ifdef CONFIG_SCHED_HRTICK | |
633 | #ifdef CONFIG_SMP | |
634 | int hrtick_csd_pending; | |
635 | struct call_single_data hrtick_csd; | |
636 | #endif | |
637 | struct hrtimer hrtick_timer; | |
638 | #endif | |
639 | ||
640 | #ifdef CONFIG_SCHEDSTATS | |
641 | /* latency stats */ | |
642 | struct sched_info rq_sched_info; | |
643 | unsigned long long rq_cpu_time; | |
644 | /* could above be rq->cfs_rq.exec_clock + rq->rt_rq.rt_runtime ? */ | |
645 | ||
646 | /* sys_sched_yield() stats */ | |
647 | unsigned int yld_count; | |
648 | ||
649 | /* schedule() stats */ | |
650 | unsigned int sched_switch; | |
651 | unsigned int sched_count; | |
652 | unsigned int sched_goidle; | |
653 | ||
654 | /* try_to_wake_up() stats */ | |
655 | unsigned int ttwu_count; | |
656 | unsigned int ttwu_local; | |
657 | ||
658 | /* BKL stats */ | |
659 | unsigned int bkl_count; | |
660 | #endif | |
661 | }; | |
662 | ||
663 | static DEFINE_PER_CPU_SHARED_ALIGNED(struct rq, runqueues); | |
664 | ||
665 | static inline void check_preempt_curr(struct rq *rq, struct task_struct *p, int sync) | |
666 | { | |
667 | rq->curr->sched_class->check_preempt_curr(rq, p, sync); | |
668 | } | |
669 | ||
670 | static inline int cpu_of(struct rq *rq) | |
671 | { | |
672 | #ifdef CONFIG_SMP | |
673 | return rq->cpu; | |
674 | #else | |
675 | return 0; | |
676 | #endif | |
677 | } | |
678 | ||
679 | /* | |
680 | * The domain tree (rq->sd) is protected by RCU's quiescent state transition. | |
681 | * See detach_destroy_domains: synchronize_sched for details. | |
682 | * | |
683 | * The domain tree of any CPU may only be accessed from within | |
684 | * preempt-disabled sections. | |
685 | */ | |
686 | #define for_each_domain(cpu, __sd) \ | |
687 | for (__sd = rcu_dereference(cpu_rq(cpu)->sd); __sd; __sd = __sd->parent) | |
688 | ||
689 | #define cpu_rq(cpu) (&per_cpu(runqueues, (cpu))) | |
690 | #define this_rq() (&__get_cpu_var(runqueues)) | |
691 | #define task_rq(p) cpu_rq(task_cpu(p)) | |
692 | #define cpu_curr(cpu) (cpu_rq(cpu)->curr) | |
693 | ||
694 | static inline void update_rq_clock(struct rq *rq) | |
695 | { | |
696 | rq->clock = sched_clock_cpu(cpu_of(rq)); | |
697 | } | |
698 | ||
699 | /* | |
700 | * Tunables that become constants when CONFIG_SCHED_DEBUG is off: | |
701 | */ | |
702 | #ifdef CONFIG_SCHED_DEBUG | |
703 | # define const_debug __read_mostly | |
704 | #else | |
705 | # define const_debug static const | |
706 | #endif | |
707 | ||
708 | /** | |
709 | * runqueue_is_locked | |
710 | * | |
711 | * Returns true if the current cpu runqueue is locked. | |
712 | * This interface allows printk to be called with the runqueue lock | |
713 | * held and know whether or not it is OK to wake up the klogd. | |
714 | */ | |
715 | int runqueue_is_locked(void) | |
716 | { | |
717 | int cpu = get_cpu(); | |
718 | struct rq *rq = cpu_rq(cpu); | |
719 | int ret; | |
720 | ||
721 | ret = spin_is_locked(&rq->lock); | |
722 | put_cpu(); | |
723 | return ret; | |
724 | } | |
725 | ||
726 | /* | |
727 | * Debugging: various feature bits | |
728 | */ | |
729 | ||
730 | #define SCHED_FEAT(name, enabled) \ | |
731 | __SCHED_FEAT_##name , | |
732 | ||
733 | enum { | |
734 | #include "sched_features.h" | |
735 | }; | |
736 | ||
737 | #undef SCHED_FEAT | |
738 | ||
739 | #define SCHED_FEAT(name, enabled) \ | |
740 | (1UL << __SCHED_FEAT_##name) * enabled | | |
741 | ||
742 | const_debug unsigned int sysctl_sched_features = | |
743 | #include "sched_features.h" | |
744 | 0; | |
745 | ||
746 | #undef SCHED_FEAT | |
747 | ||
748 | #ifdef CONFIG_SCHED_DEBUG | |
749 | #define SCHED_FEAT(name, enabled) \ | |
750 | #name , | |
751 | ||
752 | static __read_mostly char *sched_feat_names[] = { | |
753 | #include "sched_features.h" | |
754 | NULL | |
755 | }; | |
756 | ||
757 | #undef SCHED_FEAT | |
758 | ||
759 | static int sched_feat_show(struct seq_file *m, void *v) | |
760 | { | |
761 | int i; | |
762 | ||
763 | for (i = 0; sched_feat_names[i]; i++) { | |
764 | if (!(sysctl_sched_features & (1UL << i))) | |
765 | seq_puts(m, "NO_"); | |
766 | seq_printf(m, "%s ", sched_feat_names[i]); | |
767 | } | |
768 | seq_puts(m, "\n"); | |
769 | ||
770 | return 0; | |
771 | } | |
772 | ||
773 | static ssize_t | |
774 | sched_feat_write(struct file *filp, const char __user *ubuf, | |
775 | size_t cnt, loff_t *ppos) | |
776 | { | |
777 | char buf[64]; | |
778 | char *cmp = buf; | |
779 | int neg = 0; | |
780 | int i; | |
781 | ||
782 | if (cnt > 63) | |
783 | cnt = 63; | |
784 | ||
785 | if (copy_from_user(&buf, ubuf, cnt)) | |
786 | return -EFAULT; | |
787 | ||
788 | buf[cnt] = 0; | |
789 | ||
790 | if (strncmp(buf, "NO_", 3) == 0) { | |
791 | neg = 1; | |
792 | cmp += 3; | |
793 | } | |
794 | ||
795 | for (i = 0; sched_feat_names[i]; i++) { | |
796 | int len = strlen(sched_feat_names[i]); | |
797 | ||
798 | if (strncmp(cmp, sched_feat_names[i], len) == 0) { | |
799 | if (neg) | |
800 | sysctl_sched_features &= ~(1UL << i); | |
801 | else | |
802 | sysctl_sched_features |= (1UL << i); | |
803 | break; | |
804 | } | |
805 | } | |
806 | ||
807 | if (!sched_feat_names[i]) | |
808 | return -EINVAL; | |
809 | ||
810 | filp->f_pos += cnt; | |
811 | ||
812 | return cnt; | |
813 | } | |
814 | ||
815 | static int sched_feat_open(struct inode *inode, struct file *filp) | |
816 | { | |
817 | return single_open(filp, sched_feat_show, NULL); | |
818 | } | |
819 | ||
820 | static struct file_operations sched_feat_fops = { | |
821 | .open = sched_feat_open, | |
822 | .write = sched_feat_write, | |
823 | .read = seq_read, | |
824 | .llseek = seq_lseek, | |
825 | .release = single_release, | |
826 | }; | |
827 | ||
828 | static __init int sched_init_debug(void) | |
829 | { | |
830 | debugfs_create_file("sched_features", 0644, NULL, NULL, | |
831 | &sched_feat_fops); | |
832 | ||
833 | return 0; | |
834 | } | |
835 | late_initcall(sched_init_debug); | |
836 | ||
837 | #endif | |
838 | ||
839 | #define sched_feat(x) (sysctl_sched_features & (1UL << __SCHED_FEAT_##x)) | |
840 | ||
841 | /* | |
842 | * Number of tasks to iterate in a single balance run. | |
843 | * Limited because this is done with IRQs disabled. | |
844 | */ | |
845 | const_debug unsigned int sysctl_sched_nr_migrate = 32; | |
846 | ||
847 | /* | |
848 | * ratelimit for updating the group shares. | |
849 | * default: 0.25ms | |
850 | */ | |
851 | unsigned int sysctl_sched_shares_ratelimit = 250000; | |
852 | ||
853 | /* | |
854 | * Inject some fuzzyness into changing the per-cpu group shares | |
855 | * this avoids remote rq-locks at the expense of fairness. | |
856 | * default: 4 | |
857 | */ | |
858 | unsigned int sysctl_sched_shares_thresh = 4; | |
859 | ||
860 | /* | |
861 | * period over which we measure -rt task cpu usage in us. | |
862 | * default: 1s | |
863 | */ | |
864 | unsigned int sysctl_sched_rt_period = 1000000; | |
865 | ||
866 | static __read_mostly int scheduler_running; | |
867 | ||
868 | /* | |
869 | * part of the period that we allow rt tasks to run in us. | |
870 | * default: 0.95s | |
871 | */ | |
872 | int sysctl_sched_rt_runtime = 950000; | |
873 | ||
874 | static inline u64 global_rt_period(void) | |
875 | { | |
876 | return (u64)sysctl_sched_rt_period * NSEC_PER_USEC; | |
877 | } | |
878 | ||
879 | static inline u64 global_rt_runtime(void) | |
880 | { | |
881 | if (sysctl_sched_rt_runtime < 0) | |
882 | return RUNTIME_INF; | |
883 | ||
884 | return (u64)sysctl_sched_rt_runtime * NSEC_PER_USEC; | |
885 | } | |
886 | ||
887 | #ifndef prepare_arch_switch | |
888 | # define prepare_arch_switch(next) do { } while (0) | |
889 | #endif | |
890 | #ifndef finish_arch_switch | |
891 | # define finish_arch_switch(prev) do { } while (0) | |
892 | #endif | |
893 | ||
894 | static inline int task_current(struct rq *rq, struct task_struct *p) | |
895 | { | |
896 | return rq->curr == p; | |
897 | } | |
898 | ||
899 | #ifndef __ARCH_WANT_UNLOCKED_CTXSW | |
900 | static inline int task_running(struct rq *rq, struct task_struct *p) | |
901 | { | |
902 | return task_current(rq, p); | |
903 | } | |
904 | ||
905 | static inline void prepare_lock_switch(struct rq *rq, struct task_struct *next) | |
906 | { | |
907 | } | |
908 | ||
909 | static inline void finish_lock_switch(struct rq *rq, struct task_struct *prev) | |
910 | { | |
911 | #ifdef CONFIG_DEBUG_SPINLOCK | |
912 | /* this is a valid case when another task releases the spinlock */ | |
913 | rq->lock.owner = current; | |
914 | #endif | |
915 | /* | |
916 | * If we are tracking spinlock dependencies then we have to | |
917 | * fix up the runqueue lock - which gets 'carried over' from | |
918 | * prev into current: | |
919 | */ | |
920 | spin_acquire(&rq->lock.dep_map, 0, 0, _THIS_IP_); | |
921 | ||
922 | spin_unlock_irq(&rq->lock); | |
923 | } | |
924 | ||
925 | #else /* __ARCH_WANT_UNLOCKED_CTXSW */ | |
926 | static inline int task_running(struct rq *rq, struct task_struct *p) | |
927 | { | |
928 | #ifdef CONFIG_SMP | |
929 | return p->oncpu; | |
930 | #else | |
931 | return task_current(rq, p); | |
932 | #endif | |
933 | } | |
934 | ||
935 | static inline void prepare_lock_switch(struct rq *rq, struct task_struct *next) | |
936 | { | |
937 | #ifdef CONFIG_SMP | |
938 | /* | |
939 | * We can optimise this out completely for !SMP, because the | |
940 | * SMP rebalancing from interrupt is the only thing that cares | |
941 | * here. | |
942 | */ | |
943 | next->oncpu = 1; | |
944 | #endif | |
945 | #ifdef __ARCH_WANT_INTERRUPTS_ON_CTXSW | |
946 | spin_unlock_irq(&rq->lock); | |
947 | #else | |
948 | spin_unlock(&rq->lock); | |
949 | #endif | |
950 | } | |
951 | ||
952 | static inline void finish_lock_switch(struct rq *rq, struct task_struct *prev) | |
953 | { | |
954 | #ifdef CONFIG_SMP | |
955 | /* | |
956 | * After ->oncpu is cleared, the task can be moved to a different CPU. | |
957 | * We must ensure this doesn't happen until the switch is completely | |
958 | * finished. | |
959 | */ | |
960 | smp_wmb(); | |
961 | prev->oncpu = 0; | |
962 | #endif | |
963 | #ifndef __ARCH_WANT_INTERRUPTS_ON_CTXSW | |
964 | local_irq_enable(); | |
965 | #endif | |
966 | } | |
967 | #endif /* __ARCH_WANT_UNLOCKED_CTXSW */ | |
968 | ||
969 | /* | |
970 | * __task_rq_lock - lock the runqueue a given task resides on. | |
971 | * Must be called interrupts disabled. | |
972 | */ | |
973 | static inline struct rq *__task_rq_lock(struct task_struct *p) | |
974 | __acquires(rq->lock) | |
975 | { | |
976 | for (;;) { | |
977 | struct rq *rq = task_rq(p); | |
978 | spin_lock(&rq->lock); | |
979 | if (likely(rq == task_rq(p))) | |
980 | return rq; | |
981 | spin_unlock(&rq->lock); | |
982 | } | |
983 | } | |
984 | ||
985 | /* | |
986 | * task_rq_lock - lock the runqueue a given task resides on and disable | |
987 | * interrupts. Note the ordering: we can safely lookup the task_rq without | |
988 | * explicitly disabling preemption. | |
989 | */ | |
990 | static struct rq *task_rq_lock(struct task_struct *p, unsigned long *flags) | |
991 | __acquires(rq->lock) | |
992 | { | |
993 | struct rq *rq; | |
994 | ||
995 | for (;;) { | |
996 | local_irq_save(*flags); | |
997 | rq = task_rq(p); | |
998 | spin_lock(&rq->lock); | |
999 | if (likely(rq == task_rq(p))) | |
1000 | return rq; | |
1001 | spin_unlock_irqrestore(&rq->lock, *flags); | |
1002 | } | |
1003 | } | |
1004 | ||
1005 | void task_rq_unlock_wait(struct task_struct *p) | |
1006 | { | |
1007 | struct rq *rq = task_rq(p); | |
1008 | ||
1009 | smp_mb(); /* spin-unlock-wait is not a full memory barrier */ | |
1010 | spin_unlock_wait(&rq->lock); | |
1011 | } | |
1012 | ||
1013 | static void __task_rq_unlock(struct rq *rq) | |
1014 | __releases(rq->lock) | |
1015 | { | |
1016 | spin_unlock(&rq->lock); | |
1017 | } | |
1018 | ||
1019 | static inline void task_rq_unlock(struct rq *rq, unsigned long *flags) | |
1020 | __releases(rq->lock) | |
1021 | { | |
1022 | spin_unlock_irqrestore(&rq->lock, *flags); | |
1023 | } | |
1024 | ||
1025 | /* | |
1026 | * this_rq_lock - lock this runqueue and disable interrupts. | |
1027 | */ | |
1028 | static struct rq *this_rq_lock(void) | |
1029 | __acquires(rq->lock) | |
1030 | { | |
1031 | struct rq *rq; | |
1032 | ||
1033 | local_irq_disable(); | |
1034 | rq = this_rq(); | |
1035 | spin_lock(&rq->lock); | |
1036 | ||
1037 | return rq; | |
1038 | } | |
1039 | ||
1040 | #ifdef CONFIG_SCHED_HRTICK | |
1041 | /* | |
1042 | * Use HR-timers to deliver accurate preemption points. | |
1043 | * | |
1044 | * Its all a bit involved since we cannot program an hrt while holding the | |
1045 | * rq->lock. So what we do is store a state in in rq->hrtick_* and ask for a | |
1046 | * reschedule event. | |
1047 | * | |
1048 | * When we get rescheduled we reprogram the hrtick_timer outside of the | |
1049 | * rq->lock. | |
1050 | */ | |
1051 | ||
1052 | /* | |
1053 | * Use hrtick when: | |
1054 | * - enabled by features | |
1055 | * - hrtimer is actually high res | |
1056 | */ | |
1057 | static inline int hrtick_enabled(struct rq *rq) | |
1058 | { | |
1059 | if (!sched_feat(HRTICK)) | |
1060 | return 0; | |
1061 | if (!cpu_active(cpu_of(rq))) | |
1062 | return 0; | |
1063 | return hrtimer_is_hres_active(&rq->hrtick_timer); | |
1064 | } | |
1065 | ||
1066 | static void hrtick_clear(struct rq *rq) | |
1067 | { | |
1068 | if (hrtimer_active(&rq->hrtick_timer)) | |
1069 | hrtimer_cancel(&rq->hrtick_timer); | |
1070 | } | |
1071 | ||
1072 | /* | |
1073 | * High-resolution timer tick. | |
1074 | * Runs from hardirq context with interrupts disabled. | |
1075 | */ | |
1076 | static enum hrtimer_restart hrtick(struct hrtimer *timer) | |
1077 | { | |
1078 | struct rq *rq = container_of(timer, struct rq, hrtick_timer); | |
1079 | ||
1080 | WARN_ON_ONCE(cpu_of(rq) != smp_processor_id()); | |
1081 | ||
1082 | spin_lock(&rq->lock); | |
1083 | update_rq_clock(rq); | |
1084 | rq->curr->sched_class->task_tick(rq, rq->curr, 1); | |
1085 | spin_unlock(&rq->lock); | |
1086 | ||
1087 | return HRTIMER_NORESTART; | |
1088 | } | |
1089 | ||
1090 | #ifdef CONFIG_SMP | |
1091 | /* | |
1092 | * called from hardirq (IPI) context | |
1093 | */ | |
1094 | static void __hrtick_start(void *arg) | |
1095 | { | |
1096 | struct rq *rq = arg; | |
1097 | ||
1098 | spin_lock(&rq->lock); | |
1099 | hrtimer_restart(&rq->hrtick_timer); | |
1100 | rq->hrtick_csd_pending = 0; | |
1101 | spin_unlock(&rq->lock); | |
1102 | } | |
1103 | ||
1104 | /* | |
1105 | * Called to set the hrtick timer state. | |
1106 | * | |
1107 | * called with rq->lock held and irqs disabled | |
1108 | */ | |
1109 | static void hrtick_start(struct rq *rq, u64 delay) | |
1110 | { | |
1111 | struct hrtimer *timer = &rq->hrtick_timer; | |
1112 | ktime_t time = ktime_add_ns(timer->base->get_time(), delay); | |
1113 | ||
1114 | hrtimer_set_expires(timer, time); | |
1115 | ||
1116 | if (rq == this_rq()) { | |
1117 | hrtimer_restart(timer); | |
1118 | } else if (!rq->hrtick_csd_pending) { | |
1119 | __smp_call_function_single(cpu_of(rq), &rq->hrtick_csd, 0); | |
1120 | rq->hrtick_csd_pending = 1; | |
1121 | } | |
1122 | } | |
1123 | ||
1124 | static int | |
1125 | hotplug_hrtick(struct notifier_block *nfb, unsigned long action, void *hcpu) | |
1126 | { | |
1127 | int cpu = (int)(long)hcpu; | |
1128 | ||
1129 | switch (action) { | |
1130 | case CPU_UP_CANCELED: | |
1131 | case CPU_UP_CANCELED_FROZEN: | |
1132 | case CPU_DOWN_PREPARE: | |
1133 | case CPU_DOWN_PREPARE_FROZEN: | |
1134 | case CPU_DEAD: | |
1135 | case CPU_DEAD_FROZEN: | |
1136 | hrtick_clear(cpu_rq(cpu)); | |
1137 | return NOTIFY_OK; | |
1138 | } | |
1139 | ||
1140 | return NOTIFY_DONE; | |
1141 | } | |
1142 | ||
1143 | static __init void init_hrtick(void) | |
1144 | { | |
1145 | hotcpu_notifier(hotplug_hrtick, 0); | |
1146 | } | |
1147 | #else | |
1148 | /* | |
1149 | * Called to set the hrtick timer state. | |
1150 | * | |
1151 | * called with rq->lock held and irqs disabled | |
1152 | */ | |
1153 | static void hrtick_start(struct rq *rq, u64 delay) | |
1154 | { | |
1155 | __hrtimer_start_range_ns(&rq->hrtick_timer, ns_to_ktime(delay), 0, | |
1156 | HRTIMER_MODE_REL, 0); | |
1157 | } | |
1158 | ||
1159 | static inline void init_hrtick(void) | |
1160 | { | |
1161 | } | |
1162 | #endif /* CONFIG_SMP */ | |
1163 | ||
1164 | static void init_rq_hrtick(struct rq *rq) | |
1165 | { | |
1166 | #ifdef CONFIG_SMP | |
1167 | rq->hrtick_csd_pending = 0; | |
1168 | ||
1169 | rq->hrtick_csd.flags = 0; | |
1170 | rq->hrtick_csd.func = __hrtick_start; | |
1171 | rq->hrtick_csd.info = rq; | |
1172 | #endif | |
1173 | ||
1174 | hrtimer_init(&rq->hrtick_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL); | |
1175 | rq->hrtick_timer.function = hrtick; | |
1176 | } | |
1177 | #else /* CONFIG_SCHED_HRTICK */ | |
1178 | static inline void hrtick_clear(struct rq *rq) | |
1179 | { | |
1180 | } | |
1181 | ||
1182 | static inline void init_rq_hrtick(struct rq *rq) | |
1183 | { | |
1184 | } | |
1185 | ||
1186 | static inline void init_hrtick(void) | |
1187 | { | |
1188 | } | |
1189 | #endif /* CONFIG_SCHED_HRTICK */ | |
1190 | ||
1191 | /* | |
1192 | * resched_task - mark a task 'to be rescheduled now'. | |
1193 | * | |
1194 | * On UP this means the setting of the need_resched flag, on SMP it | |
1195 | * might also involve a cross-CPU call to trigger the scheduler on | |
1196 | * the target CPU. | |
1197 | */ | |
1198 | #ifdef CONFIG_SMP | |
1199 | ||
1200 | #ifndef tsk_is_polling | |
1201 | #define tsk_is_polling(t) test_tsk_thread_flag(t, TIF_POLLING_NRFLAG) | |
1202 | #endif | |
1203 | ||
1204 | static void resched_task(struct task_struct *p) | |
1205 | { | |
1206 | int cpu; | |
1207 | ||
1208 | assert_spin_locked(&task_rq(p)->lock); | |
1209 | ||
1210 | if (test_tsk_need_resched(p)) | |
1211 | return; | |
1212 | ||
1213 | set_tsk_need_resched(p); | |
1214 | ||
1215 | cpu = task_cpu(p); | |
1216 | if (cpu == smp_processor_id()) | |
1217 | return; | |
1218 | ||
1219 | /* NEED_RESCHED must be visible before we test polling */ | |
1220 | smp_mb(); | |
1221 | if (!tsk_is_polling(p)) | |
1222 | smp_send_reschedule(cpu); | |
1223 | } | |
1224 | ||
1225 | static void resched_cpu(int cpu) | |
1226 | { | |
1227 | struct rq *rq = cpu_rq(cpu); | |
1228 | unsigned long flags; | |
1229 | ||
1230 | if (!spin_trylock_irqsave(&rq->lock, flags)) | |
1231 | return; | |
1232 | resched_task(cpu_curr(cpu)); | |
1233 | spin_unlock_irqrestore(&rq->lock, flags); | |
1234 | } | |
1235 | ||
1236 | #ifdef CONFIG_NO_HZ | |
1237 | /* | |
1238 | * When add_timer_on() enqueues a timer into the timer wheel of an | |
1239 | * idle CPU then this timer might expire before the next timer event | |
1240 | * which is scheduled to wake up that CPU. In case of a completely | |
1241 | * idle system the next event might even be infinite time into the | |
1242 | * future. wake_up_idle_cpu() ensures that the CPU is woken up and | |
1243 | * leaves the inner idle loop so the newly added timer is taken into | |
1244 | * account when the CPU goes back to idle and evaluates the timer | |
1245 | * wheel for the next timer event. | |
1246 | */ | |
1247 | void wake_up_idle_cpu(int cpu) | |
1248 | { | |
1249 | struct rq *rq = cpu_rq(cpu); | |
1250 | ||
1251 | if (cpu == smp_processor_id()) | |
1252 | return; | |
1253 | ||
1254 | /* | |
1255 | * This is safe, as this function is called with the timer | |
1256 | * wheel base lock of (cpu) held. When the CPU is on the way | |
1257 | * to idle and has not yet set rq->curr to idle then it will | |
1258 | * be serialized on the timer wheel base lock and take the new | |
1259 | * timer into account automatically. | |
1260 | */ | |
1261 | if (rq->curr != rq->idle) | |
1262 | return; | |
1263 | ||
1264 | /* | |
1265 | * We can set TIF_RESCHED on the idle task of the other CPU | |
1266 | * lockless. The worst case is that the other CPU runs the | |
1267 | * idle task through an additional NOOP schedule() | |
1268 | */ | |
1269 | set_tsk_need_resched(rq->idle); | |
1270 | ||
1271 | /* NEED_RESCHED must be visible before we test polling */ | |
1272 | smp_mb(); | |
1273 | if (!tsk_is_polling(rq->idle)) | |
1274 | smp_send_reschedule(cpu); | |
1275 | } | |
1276 | #endif /* CONFIG_NO_HZ */ | |
1277 | ||
1278 | #else /* !CONFIG_SMP */ | |
1279 | static void resched_task(struct task_struct *p) | |
1280 | { | |
1281 | assert_spin_locked(&task_rq(p)->lock); | |
1282 | set_tsk_need_resched(p); | |
1283 | } | |
1284 | #endif /* CONFIG_SMP */ | |
1285 | ||
1286 | #if BITS_PER_LONG == 32 | |
1287 | # define WMULT_CONST (~0UL) | |
1288 | #else | |
1289 | # define WMULT_CONST (1UL << 32) | |
1290 | #endif | |
1291 | ||
1292 | #define WMULT_SHIFT 32 | |
1293 | ||
1294 | /* | |
1295 | * Shift right and round: | |
1296 | */ | |
1297 | #define SRR(x, y) (((x) + (1UL << ((y) - 1))) >> (y)) | |
1298 | ||
1299 | /* | |
1300 | * delta *= weight / lw | |
1301 | */ | |
1302 | static unsigned long | |
1303 | calc_delta_mine(unsigned long delta_exec, unsigned long weight, | |
1304 | struct load_weight *lw) | |
1305 | { | |
1306 | u64 tmp; | |
1307 | ||
1308 | if (!lw->inv_weight) { | |
1309 | if (BITS_PER_LONG > 32 && unlikely(lw->weight >= WMULT_CONST)) | |
1310 | lw->inv_weight = 1; | |
1311 | else | |
1312 | lw->inv_weight = 1 + (WMULT_CONST-lw->weight/2) | |
1313 | / (lw->weight+1); | |
1314 | } | |
1315 | ||
1316 | tmp = (u64)delta_exec * weight; | |
1317 | /* | |
1318 | * Check whether we'd overflow the 64-bit multiplication: | |
1319 | */ | |
1320 | if (unlikely(tmp > WMULT_CONST)) | |
1321 | tmp = SRR(SRR(tmp, WMULT_SHIFT/2) * lw->inv_weight, | |
1322 | WMULT_SHIFT/2); | |
1323 | else | |
1324 | tmp = SRR(tmp * lw->inv_weight, WMULT_SHIFT); | |
1325 | ||
1326 | return (unsigned long)min(tmp, (u64)(unsigned long)LONG_MAX); | |
1327 | } | |
1328 | ||
1329 | static inline void update_load_add(struct load_weight *lw, unsigned long inc) | |
1330 | { | |
1331 | lw->weight += inc; | |
1332 | lw->inv_weight = 0; | |
1333 | } | |
1334 | ||
1335 | static inline void update_load_sub(struct load_weight *lw, unsigned long dec) | |
1336 | { | |
1337 | lw->weight -= dec; | |
1338 | lw->inv_weight = 0; | |
1339 | } | |
1340 | ||
1341 | /* | |
1342 | * To aid in avoiding the subversion of "niceness" due to uneven distribution | |
1343 | * of tasks with abnormal "nice" values across CPUs the contribution that | |
1344 | * each task makes to its run queue's load is weighted according to its | |
1345 | * scheduling class and "nice" value. For SCHED_NORMAL tasks this is just a | |
1346 | * scaled version of the new time slice allocation that they receive on time | |
1347 | * slice expiry etc. | |
1348 | */ | |
1349 | ||
1350 | #define WEIGHT_IDLEPRIO 3 | |
1351 | #define WMULT_IDLEPRIO 1431655765 | |
1352 | ||
1353 | /* | |
1354 | * Nice levels are multiplicative, with a gentle 10% change for every | |
1355 | * nice level changed. I.e. when a CPU-bound task goes from nice 0 to | |
1356 | * nice 1, it will get ~10% less CPU time than another CPU-bound task | |
1357 | * that remained on nice 0. | |
1358 | * | |
1359 | * The "10% effect" is relative and cumulative: from _any_ nice level, | |
1360 | * if you go up 1 level, it's -10% CPU usage, if you go down 1 level | |
1361 | * it's +10% CPU usage. (to achieve that we use a multiplier of 1.25. | |
1362 | * If a task goes up by ~10% and another task goes down by ~10% then | |
1363 | * the relative distance between them is ~25%.) | |
1364 | */ | |
1365 | static const int prio_to_weight[40] = { | |
1366 | /* -20 */ 88761, 71755, 56483, 46273, 36291, | |
1367 | /* -15 */ 29154, 23254, 18705, 14949, 11916, | |
1368 | /* -10 */ 9548, 7620, 6100, 4904, 3906, | |
1369 | /* -5 */ 3121, 2501, 1991, 1586, 1277, | |
1370 | /* 0 */ 1024, 820, 655, 526, 423, | |
1371 | /* 5 */ 335, 272, 215, 172, 137, | |
1372 | /* 10 */ 110, 87, 70, 56, 45, | |
1373 | /* 15 */ 36, 29, 23, 18, 15, | |
1374 | }; | |
1375 | ||
1376 | /* | |
1377 | * Inverse (2^32/x) values of the prio_to_weight[] array, precalculated. | |
1378 | * | |
1379 | * In cases where the weight does not change often, we can use the | |
1380 | * precalculated inverse to speed up arithmetics by turning divisions | |
1381 | * into multiplications: | |
1382 | */ | |
1383 | static const u32 prio_to_wmult[40] = { | |
1384 | /* -20 */ 48388, 59856, 76040, 92818, 118348, | |
1385 | /* -15 */ 147320, 184698, 229616, 287308, 360437, | |
1386 | /* -10 */ 449829, 563644, 704093, 875809, 1099582, | |
1387 | /* -5 */ 1376151, 1717300, 2157191, 2708050, 3363326, | |
1388 | /* 0 */ 4194304, 5237765, 6557202, 8165337, 10153587, | |
1389 | /* 5 */ 12820798, 15790321, 19976592, 24970740, 31350126, | |
1390 | /* 10 */ 39045157, 49367440, 61356676, 76695844, 95443717, | |
1391 | /* 15 */ 119304647, 148102320, 186737708, 238609294, 286331153, | |
1392 | }; | |
1393 | ||
1394 | static void activate_task(struct rq *rq, struct task_struct *p, int wakeup); | |
1395 | ||
1396 | /* | |
1397 | * runqueue iterator, to support SMP load-balancing between different | |
1398 | * scheduling classes, without having to expose their internal data | |
1399 | * structures to the load-balancing proper: | |
1400 | */ | |
1401 | struct rq_iterator { | |
1402 | void *arg; | |
1403 | struct task_struct *(*start)(void *); | |
1404 | struct task_struct *(*next)(void *); | |
1405 | }; | |
1406 | ||
1407 | #ifdef CONFIG_SMP | |
1408 | static unsigned long | |
1409 | balance_tasks(struct rq *this_rq, int this_cpu, struct rq *busiest, | |
1410 | unsigned long max_load_move, struct sched_domain *sd, | |
1411 | enum cpu_idle_type idle, int *all_pinned, | |
1412 | int *this_best_prio, struct rq_iterator *iterator); | |
1413 | ||
1414 | static int | |
1415 | iter_move_one_task(struct rq *this_rq, int this_cpu, struct rq *busiest, | |
1416 | struct sched_domain *sd, enum cpu_idle_type idle, | |
1417 | struct rq_iterator *iterator); | |
1418 | #endif | |
1419 | ||
1420 | /* Time spent by the tasks of the cpu accounting group executing in ... */ | |
1421 | enum cpuacct_stat_index { | |
1422 | CPUACCT_STAT_USER, /* ... user mode */ | |
1423 | CPUACCT_STAT_SYSTEM, /* ... kernel mode */ | |
1424 | ||
1425 | CPUACCT_STAT_NSTATS, | |
1426 | }; | |
1427 | ||
1428 | #ifdef CONFIG_CGROUP_CPUACCT | |
1429 | static void cpuacct_charge(struct task_struct *tsk, u64 cputime); | |
1430 | static void cpuacct_update_stats(struct task_struct *tsk, | |
1431 | enum cpuacct_stat_index idx, cputime_t val); | |
1432 | #else | |
1433 | static inline void cpuacct_charge(struct task_struct *tsk, u64 cputime) {} | |
1434 | static inline void cpuacct_update_stats(struct task_struct *tsk, | |
1435 | enum cpuacct_stat_index idx, cputime_t val) {} | |
1436 | #endif | |
1437 | ||
1438 | static inline void inc_cpu_load(struct rq *rq, unsigned long load) | |
1439 | { | |
1440 | update_load_add(&rq->load, load); | |
1441 | } | |
1442 | ||
1443 | static inline void dec_cpu_load(struct rq *rq, unsigned long load) | |
1444 | { | |
1445 | update_load_sub(&rq->load, load); | |
1446 | } | |
1447 | ||
1448 | #if (defined(CONFIG_SMP) && defined(CONFIG_FAIR_GROUP_SCHED)) || defined(CONFIG_RT_GROUP_SCHED) | |
1449 | typedef int (*tg_visitor)(struct task_group *, void *); | |
1450 | ||
1451 | /* | |
1452 | * Iterate the full tree, calling @down when first entering a node and @up when | |
1453 | * leaving it for the final time. | |
1454 | */ | |
1455 | static int walk_tg_tree(tg_visitor down, tg_visitor up, void *data) | |
1456 | { | |
1457 | struct task_group *parent, *child; | |
1458 | int ret; | |
1459 | ||
1460 | rcu_read_lock(); | |
1461 | parent = &root_task_group; | |
1462 | down: | |
1463 | ret = (*down)(parent, data); | |
1464 | if (ret) | |
1465 | goto out_unlock; | |
1466 | list_for_each_entry_rcu(child, &parent->children, siblings) { | |
1467 | parent = child; | |
1468 | goto down; | |
1469 | ||
1470 | up: | |
1471 | continue; | |
1472 | } | |
1473 | ret = (*up)(parent, data); | |
1474 | if (ret) | |
1475 | goto out_unlock; | |
1476 | ||
1477 | child = parent; | |
1478 | parent = parent->parent; | |
1479 | if (parent) | |
1480 | goto up; | |
1481 | out_unlock: | |
1482 | rcu_read_unlock(); | |
1483 | ||
1484 | return ret; | |
1485 | } | |
1486 | ||
1487 | static int tg_nop(struct task_group *tg, void *data) | |
1488 | { | |
1489 | return 0; | |
1490 | } | |
1491 | #endif | |
1492 | ||
1493 | #ifdef CONFIG_SMP | |
1494 | static unsigned long source_load(int cpu, int type); | |
1495 | static unsigned long target_load(int cpu, int type); | |
1496 | static int task_hot(struct task_struct *p, u64 now, struct sched_domain *sd); | |
1497 | ||
1498 | static unsigned long cpu_avg_load_per_task(int cpu) | |
1499 | { | |
1500 | struct rq *rq = cpu_rq(cpu); | |
1501 | unsigned long nr_running = ACCESS_ONCE(rq->nr_running); | |
1502 | ||
1503 | if (nr_running) | |
1504 | rq->avg_load_per_task = rq->load.weight / nr_running; | |
1505 | else | |
1506 | rq->avg_load_per_task = 0; | |
1507 | ||
1508 | return rq->avg_load_per_task; | |
1509 | } | |
1510 | ||
1511 | #ifdef CONFIG_FAIR_GROUP_SCHED | |
1512 | ||
1513 | static void __set_se_shares(struct sched_entity *se, unsigned long shares); | |
1514 | ||
1515 | /* | |
1516 | * Calculate and set the cpu's group shares. | |
1517 | */ | |
1518 | static void | |
1519 | update_group_shares_cpu(struct task_group *tg, int cpu, | |
1520 | unsigned long sd_shares, unsigned long sd_rq_weight) | |
1521 | { | |
1522 | unsigned long shares; | |
1523 | unsigned long rq_weight; | |
1524 | ||
1525 | if (!tg->se[cpu]) | |
1526 | return; | |
1527 | ||
1528 | rq_weight = tg->cfs_rq[cpu]->rq_weight; | |
1529 | ||
1530 | /* | |
1531 | * \Sum shares * rq_weight | |
1532 | * shares = ----------------------- | |
1533 | * \Sum rq_weight | |
1534 | * | |
1535 | */ | |
1536 | shares = (sd_shares * rq_weight) / sd_rq_weight; | |
1537 | shares = clamp_t(unsigned long, shares, MIN_SHARES, MAX_SHARES); | |
1538 | ||
1539 | if (abs(shares - tg->se[cpu]->load.weight) > | |
1540 | sysctl_sched_shares_thresh) { | |
1541 | struct rq *rq = cpu_rq(cpu); | |
1542 | unsigned long flags; | |
1543 | ||
1544 | spin_lock_irqsave(&rq->lock, flags); | |
1545 | tg->cfs_rq[cpu]->shares = shares; | |
1546 | ||
1547 | __set_se_shares(tg->se[cpu], shares); | |
1548 | spin_unlock_irqrestore(&rq->lock, flags); | |
1549 | } | |
1550 | } | |
1551 | ||
1552 | /* | |
1553 | * Re-compute the task group their per cpu shares over the given domain. | |
1554 | * This needs to be done in a bottom-up fashion because the rq weight of a | |
1555 | * parent group depends on the shares of its child groups. | |
1556 | */ | |
1557 | static int tg_shares_up(struct task_group *tg, void *data) | |
1558 | { | |
1559 | unsigned long weight, rq_weight = 0; | |
1560 | unsigned long shares = 0; | |
1561 | struct sched_domain *sd = data; | |
1562 | int i; | |
1563 | ||
1564 | for_each_cpu(i, sched_domain_span(sd)) { | |
1565 | /* | |
1566 | * If there are currently no tasks on the cpu pretend there | |
1567 | * is one of average load so that when a new task gets to | |
1568 | * run here it will not get delayed by group starvation. | |
1569 | */ | |
1570 | weight = tg->cfs_rq[i]->load.weight; | |
1571 | if (!weight) | |
1572 | weight = NICE_0_LOAD; | |
1573 | ||
1574 | tg->cfs_rq[i]->rq_weight = weight; | |
1575 | rq_weight += weight; | |
1576 | shares += tg->cfs_rq[i]->shares; | |
1577 | } | |
1578 | ||
1579 | if ((!shares && rq_weight) || shares > tg->shares) | |
1580 | shares = tg->shares; | |
1581 | ||
1582 | if (!sd->parent || !(sd->parent->flags & SD_LOAD_BALANCE)) | |
1583 | shares = tg->shares; | |
1584 | ||
1585 | for_each_cpu(i, sched_domain_span(sd)) | |
1586 | update_group_shares_cpu(tg, i, shares, rq_weight); | |
1587 | ||
1588 | return 0; | |
1589 | } | |
1590 | ||
1591 | /* | |
1592 | * Compute the cpu's hierarchical load factor for each task group. | |
1593 | * This needs to be done in a top-down fashion because the load of a child | |
1594 | * group is a fraction of its parents load. | |
1595 | */ | |
1596 | static int tg_load_down(struct task_group *tg, void *data) | |
1597 | { | |
1598 | unsigned long load; | |
1599 | long cpu = (long)data; | |
1600 | ||
1601 | if (!tg->parent) { | |
1602 | load = cpu_rq(cpu)->load.weight; | |
1603 | } else { | |
1604 | load = tg->parent->cfs_rq[cpu]->h_load; | |
1605 | load *= tg->cfs_rq[cpu]->shares; | |
1606 | load /= tg->parent->cfs_rq[cpu]->load.weight + 1; | |
1607 | } | |
1608 | ||
1609 | tg->cfs_rq[cpu]->h_load = load; | |
1610 | ||
1611 | return 0; | |
1612 | } | |
1613 | ||
1614 | static void update_shares(struct sched_domain *sd) | |
1615 | { | |
1616 | u64 now = cpu_clock(raw_smp_processor_id()); | |
1617 | s64 elapsed = now - sd->last_update; | |
1618 | ||
1619 | if (elapsed >= (s64)(u64)sysctl_sched_shares_ratelimit) { | |
1620 | sd->last_update = now; | |
1621 | walk_tg_tree(tg_nop, tg_shares_up, sd); | |
1622 | } | |
1623 | } | |
1624 | ||
1625 | static void update_shares_locked(struct rq *rq, struct sched_domain *sd) | |
1626 | { | |
1627 | spin_unlock(&rq->lock); | |
1628 | update_shares(sd); | |
1629 | spin_lock(&rq->lock); | |
1630 | } | |
1631 | ||
1632 | static void update_h_load(long cpu) | |
1633 | { | |
1634 | walk_tg_tree(tg_load_down, tg_nop, (void *)cpu); | |
1635 | } | |
1636 | ||
1637 | #else | |
1638 | ||
1639 | static inline void update_shares(struct sched_domain *sd) | |
1640 | { | |
1641 | } | |
1642 | ||
1643 | static inline void update_shares_locked(struct rq *rq, struct sched_domain *sd) | |
1644 | { | |
1645 | } | |
1646 | ||
1647 | #endif | |
1648 | ||
1649 | #ifdef CONFIG_PREEMPT | |
1650 | ||
1651 | /* | |
1652 | * fair double_lock_balance: Safely acquires both rq->locks in a fair | |
1653 | * way at the expense of forcing extra atomic operations in all | |
1654 | * invocations. This assures that the double_lock is acquired using the | |
1655 | * same underlying policy as the spinlock_t on this architecture, which | |
1656 | * reduces latency compared to the unfair variant below. However, it | |
1657 | * also adds more overhead and therefore may reduce throughput. | |
1658 | */ | |
1659 | static inline int _double_lock_balance(struct rq *this_rq, struct rq *busiest) | |
1660 | __releases(this_rq->lock) | |
1661 | __acquires(busiest->lock) | |
1662 | __acquires(this_rq->lock) | |
1663 | { | |
1664 | spin_unlock(&this_rq->lock); | |
1665 | double_rq_lock(this_rq, busiest); | |
1666 | ||
1667 | return 1; | |
1668 | } | |
1669 | ||
1670 | #else | |
1671 | /* | |
1672 | * Unfair double_lock_balance: Optimizes throughput at the expense of | |
1673 | * latency by eliminating extra atomic operations when the locks are | |
1674 | * already in proper order on entry. This favors lower cpu-ids and will | |
1675 | * grant the double lock to lower cpus over higher ids under contention, | |
1676 | * regardless of entry order into the function. | |
1677 | */ | |
1678 | static int _double_lock_balance(struct rq *this_rq, struct rq *busiest) | |
1679 | __releases(this_rq->lock) | |
1680 | __acquires(busiest->lock) | |
1681 | __acquires(this_rq->lock) | |
1682 | { | |
1683 | int ret = 0; | |
1684 | ||
1685 | if (unlikely(!spin_trylock(&busiest->lock))) { | |
1686 | if (busiest < this_rq) { | |
1687 | spin_unlock(&this_rq->lock); | |
1688 | spin_lock(&busiest->lock); | |
1689 | spin_lock_nested(&this_rq->lock, SINGLE_DEPTH_NESTING); | |
1690 | ret = 1; | |
1691 | } else | |
1692 | spin_lock_nested(&busiest->lock, SINGLE_DEPTH_NESTING); | |
1693 | } | |
1694 | return ret; | |
1695 | } | |
1696 | ||
1697 | #endif /* CONFIG_PREEMPT */ | |
1698 | ||
1699 | /* | |
1700 | * double_lock_balance - lock the busiest runqueue, this_rq is locked already. | |
1701 | */ | |
1702 | static int double_lock_balance(struct rq *this_rq, struct rq *busiest) | |
1703 | { | |
1704 | if (unlikely(!irqs_disabled())) { | |
1705 | /* printk() doesn't work good under rq->lock */ | |
1706 | spin_unlock(&this_rq->lock); | |
1707 | BUG_ON(1); | |
1708 | } | |
1709 | ||
1710 | return _double_lock_balance(this_rq, busiest); | |
1711 | } | |
1712 | ||
1713 | static inline void double_unlock_balance(struct rq *this_rq, struct rq *busiest) | |
1714 | __releases(busiest->lock) | |
1715 | { | |
1716 | spin_unlock(&busiest->lock); | |
1717 | lock_set_subclass(&this_rq->lock.dep_map, 0, _RET_IP_); | |
1718 | } | |
1719 | #endif | |
1720 | ||
1721 | #ifdef CONFIG_FAIR_GROUP_SCHED | |
1722 | static void cfs_rq_set_shares(struct cfs_rq *cfs_rq, unsigned long shares) | |
1723 | { | |
1724 | #ifdef CONFIG_SMP | |
1725 | cfs_rq->shares = shares; | |
1726 | #endif | |
1727 | } | |
1728 | #endif | |
1729 | ||
1730 | static void calc_load_account_active(struct rq *this_rq); | |
1731 | ||
1732 | #include "sched_stats.h" | |
1733 | #include "sched_idletask.c" | |
1734 | #include "sched_fair.c" | |
1735 | #include "sched_rt.c" | |
1736 | #ifdef CONFIG_SCHED_DEBUG | |
1737 | # include "sched_debug.c" | |
1738 | #endif | |
1739 | ||
1740 | #define sched_class_highest (&rt_sched_class) | |
1741 | #define for_each_class(class) \ | |
1742 | for (class = sched_class_highest; class; class = class->next) | |
1743 | ||
1744 | static void inc_nr_running(struct rq *rq) | |
1745 | { | |
1746 | rq->nr_running++; | |
1747 | } | |
1748 | ||
1749 | static void dec_nr_running(struct rq *rq) | |
1750 | { | |
1751 | rq->nr_running--; | |
1752 | } | |
1753 | ||
1754 | static void set_load_weight(struct task_struct *p) | |
1755 | { | |
1756 | if (task_has_rt_policy(p)) { | |
1757 | p->se.load.weight = prio_to_weight[0] * 2; | |
1758 | p->se.load.inv_weight = prio_to_wmult[0] >> 1; | |
1759 | return; | |
1760 | } | |
1761 | ||
1762 | /* | |
1763 | * SCHED_IDLE tasks get minimal weight: | |
1764 | */ | |
1765 | if (p->policy == SCHED_IDLE) { | |
1766 | p->se.load.weight = WEIGHT_IDLEPRIO; | |
1767 | p->se.load.inv_weight = WMULT_IDLEPRIO; | |
1768 | return; | |
1769 | } | |
1770 | ||
1771 | p->se.load.weight = prio_to_weight[p->static_prio - MAX_RT_PRIO]; | |
1772 | p->se.load.inv_weight = prio_to_wmult[p->static_prio - MAX_RT_PRIO]; | |
1773 | } | |
1774 | ||
1775 | static void update_avg(u64 *avg, u64 sample) | |
1776 | { | |
1777 | s64 diff = sample - *avg; | |
1778 | *avg += diff >> 3; | |
1779 | } | |
1780 | ||
1781 | static void enqueue_task(struct rq *rq, struct task_struct *p, int wakeup) | |
1782 | { | |
1783 | if (wakeup) | |
1784 | p->se.start_runtime = p->se.sum_exec_runtime; | |
1785 | ||
1786 | sched_info_queued(p); | |
1787 | p->sched_class->enqueue_task(rq, p, wakeup); | |
1788 | p->se.on_rq = 1; | |
1789 | } | |
1790 | ||
1791 | static void dequeue_task(struct rq *rq, struct task_struct *p, int sleep) | |
1792 | { | |
1793 | if (sleep) { | |
1794 | if (p->se.last_wakeup) { | |
1795 | update_avg(&p->se.avg_overlap, | |
1796 | p->se.sum_exec_runtime - p->se.last_wakeup); | |
1797 | p->se.last_wakeup = 0; | |
1798 | } else { | |
1799 | update_avg(&p->se.avg_wakeup, | |
1800 | sysctl_sched_wakeup_granularity); | |
1801 | } | |
1802 | } | |
1803 | ||
1804 | sched_info_dequeued(p); | |
1805 | p->sched_class->dequeue_task(rq, p, sleep); | |
1806 | p->se.on_rq = 0; | |
1807 | } | |
1808 | ||
1809 | /* | |
1810 | * __normal_prio - return the priority that is based on the static prio | |
1811 | */ | |
1812 | static inline int __normal_prio(struct task_struct *p) | |
1813 | { | |
1814 | return p->static_prio; | |
1815 | } | |
1816 | ||
1817 | /* | |
1818 | * Calculate the expected normal priority: i.e. priority | |
1819 | * without taking RT-inheritance into account. Might be | |
1820 | * boosted by interactivity modifiers. Changes upon fork, | |
1821 | * setprio syscalls, and whenever the interactivity | |
1822 | * estimator recalculates. | |
1823 | */ | |
1824 | static inline int normal_prio(struct task_struct *p) | |
1825 | { | |
1826 | int prio; | |
1827 | ||
1828 | if (task_has_rt_policy(p)) | |
1829 | prio = MAX_RT_PRIO-1 - p->rt_priority; | |
1830 | else | |
1831 | prio = __normal_prio(p); | |
1832 | return prio; | |
1833 | } | |
1834 | ||
1835 | /* | |
1836 | * Calculate the current priority, i.e. the priority | |
1837 | * taken into account by the scheduler. This value might | |
1838 | * be boosted by RT tasks, or might be boosted by | |
1839 | * interactivity modifiers. Will be RT if the task got | |
1840 | * RT-boosted. If not then it returns p->normal_prio. | |
1841 | */ | |
1842 | static int effective_prio(struct task_struct *p) | |
1843 | { | |
1844 | p->normal_prio = normal_prio(p); | |
1845 | /* | |
1846 | * If we are RT tasks or we were boosted to RT priority, | |
1847 | * keep the priority unchanged. Otherwise, update priority | |
1848 | * to the normal priority: | |
1849 | */ | |
1850 | if (!rt_prio(p->prio)) | |
1851 | return p->normal_prio; | |
1852 | return p->prio; | |
1853 | } | |
1854 | ||
1855 | /* | |
1856 | * activate_task - move a task to the runqueue. | |
1857 | */ | |
1858 | static void activate_task(struct rq *rq, struct task_struct *p, int wakeup) | |
1859 | { | |
1860 | if (task_contributes_to_load(p)) | |
1861 | rq->nr_uninterruptible--; | |
1862 | ||
1863 | enqueue_task(rq, p, wakeup); | |
1864 | inc_nr_running(rq); | |
1865 | } | |
1866 | ||
1867 | /* | |
1868 | * deactivate_task - remove a task from the runqueue. | |
1869 | */ | |
1870 | static void deactivate_task(struct rq *rq, struct task_struct *p, int sleep) | |
1871 | { | |
1872 | if (task_contributes_to_load(p)) | |
1873 | rq->nr_uninterruptible++; | |
1874 | ||
1875 | dequeue_task(rq, p, sleep); | |
1876 | dec_nr_running(rq); | |
1877 | } | |
1878 | ||
1879 | /** | |
1880 | * task_curr - is this task currently executing on a CPU? | |
1881 | * @p: the task in question. | |
1882 | */ | |
1883 | inline int task_curr(const struct task_struct *p) | |
1884 | { | |
1885 | return cpu_curr(task_cpu(p)) == p; | |
1886 | } | |
1887 | ||
1888 | static inline void __set_task_cpu(struct task_struct *p, unsigned int cpu) | |
1889 | { | |
1890 | set_task_rq(p, cpu); | |
1891 | #ifdef CONFIG_SMP | |
1892 | /* | |
1893 | * After ->cpu is set up to a new value, task_rq_lock(p, ...) can be | |
1894 | * successfuly executed on another CPU. We must ensure that updates of | |
1895 | * per-task data have been completed by this moment. | |
1896 | */ | |
1897 | smp_wmb(); | |
1898 | task_thread_info(p)->cpu = cpu; | |
1899 | #endif | |
1900 | } | |
1901 | ||
1902 | static inline void check_class_changed(struct rq *rq, struct task_struct *p, | |
1903 | const struct sched_class *prev_class, | |
1904 | int oldprio, int running) | |
1905 | { | |
1906 | if (prev_class != p->sched_class) { | |
1907 | if (prev_class->switched_from) | |
1908 | prev_class->switched_from(rq, p, running); | |
1909 | p->sched_class->switched_to(rq, p, running); | |
1910 | } else | |
1911 | p->sched_class->prio_changed(rq, p, oldprio, running); | |
1912 | } | |
1913 | ||
1914 | #ifdef CONFIG_SMP | |
1915 | ||
1916 | /* Used instead of source_load when we know the type == 0 */ | |
1917 | static unsigned long weighted_cpuload(const int cpu) | |
1918 | { | |
1919 | return cpu_rq(cpu)->load.weight; | |
1920 | } | |
1921 | ||
1922 | /* | |
1923 | * Is this task likely cache-hot: | |
1924 | */ | |
1925 | static int | |
1926 | task_hot(struct task_struct *p, u64 now, struct sched_domain *sd) | |
1927 | { | |
1928 | s64 delta; | |
1929 | ||
1930 | /* | |
1931 | * Buddy candidates are cache hot: | |
1932 | */ | |
1933 | if (sched_feat(CACHE_HOT_BUDDY) && | |
1934 | (&p->se == cfs_rq_of(&p->se)->next || | |
1935 | &p->se == cfs_rq_of(&p->se)->last)) | |
1936 | return 1; | |
1937 | ||
1938 | if (p->sched_class != &fair_sched_class) | |
1939 | return 0; | |
1940 | ||
1941 | if (sysctl_sched_migration_cost == -1) | |
1942 | return 1; | |
1943 | if (sysctl_sched_migration_cost == 0) | |
1944 | return 0; | |
1945 | ||
1946 | delta = now - p->se.exec_start; | |
1947 | ||
1948 | return delta < (s64)sysctl_sched_migration_cost; | |
1949 | } | |
1950 | ||
1951 | ||
1952 | void set_task_cpu(struct task_struct *p, unsigned int new_cpu) | |
1953 | { | |
1954 | int old_cpu = task_cpu(p); | |
1955 | struct rq *old_rq = cpu_rq(old_cpu), *new_rq = cpu_rq(new_cpu); | |
1956 | struct cfs_rq *old_cfsrq = task_cfs_rq(p), | |
1957 | *new_cfsrq = cpu_cfs_rq(old_cfsrq, new_cpu); | |
1958 | u64 clock_offset; | |
1959 | ||
1960 | clock_offset = old_rq->clock - new_rq->clock; | |
1961 | ||
1962 | trace_sched_migrate_task(p, new_cpu); | |
1963 | ||
1964 | #ifdef CONFIG_SCHEDSTATS | |
1965 | if (p->se.wait_start) | |
1966 | p->se.wait_start -= clock_offset; | |
1967 | if (p->se.sleep_start) | |
1968 | p->se.sleep_start -= clock_offset; | |
1969 | if (p->se.block_start) | |
1970 | p->se.block_start -= clock_offset; | |
1971 | if (old_cpu != new_cpu) { | |
1972 | schedstat_inc(p, se.nr_migrations); | |
1973 | if (task_hot(p, old_rq->clock, NULL)) | |
1974 | schedstat_inc(p, se.nr_forced2_migrations); | |
1975 | } | |
1976 | #endif | |
1977 | p->se.vruntime -= old_cfsrq->min_vruntime - | |
1978 | new_cfsrq->min_vruntime; | |
1979 | ||
1980 | __set_task_cpu(p, new_cpu); | |
1981 | } | |
1982 | ||
1983 | struct migration_req { | |
1984 | struct list_head list; | |
1985 | ||
1986 | struct task_struct *task; | |
1987 | int dest_cpu; | |
1988 | ||
1989 | struct completion done; | |
1990 | }; | |
1991 | ||
1992 | /* | |
1993 | * The task's runqueue lock must be held. | |
1994 | * Returns true if you have to wait for migration thread. | |
1995 | */ | |
1996 | static int | |
1997 | migrate_task(struct task_struct *p, int dest_cpu, struct migration_req *req) | |
1998 | { | |
1999 | struct rq *rq = task_rq(p); | |
2000 | ||
2001 | /* | |
2002 | * If the task is not on a runqueue (and not running), then | |
2003 | * it is sufficient to simply update the task's cpu field. | |
2004 | */ | |
2005 | if (!p->se.on_rq && !task_running(rq, p)) { | |
2006 | set_task_cpu(p, dest_cpu); | |
2007 | return 0; | |
2008 | } | |
2009 | ||
2010 | init_completion(&req->done); | |
2011 | req->task = p; | |
2012 | req->dest_cpu = dest_cpu; | |
2013 | list_add(&req->list, &rq->migration_queue); | |
2014 | ||
2015 | return 1; | |
2016 | } | |
2017 | ||
2018 | /* | |
2019 | * wait_task_context_switch - wait for a thread to complete at least one | |
2020 | * context switch. | |
2021 | * | |
2022 | * @p must not be current. | |
2023 | */ | |
2024 | void wait_task_context_switch(struct task_struct *p) | |
2025 | { | |
2026 | unsigned long nvcsw, nivcsw, flags; | |
2027 | int running; | |
2028 | struct rq *rq; | |
2029 | ||
2030 | nvcsw = p->nvcsw; | |
2031 | nivcsw = p->nivcsw; | |
2032 | for (;;) { | |
2033 | /* | |
2034 | * The runqueue is assigned before the actual context | |
2035 | * switch. We need to take the runqueue lock. | |
2036 | * | |
2037 | * We could check initially without the lock but it is | |
2038 | * very likely that we need to take the lock in every | |
2039 | * iteration. | |
2040 | */ | |
2041 | rq = task_rq_lock(p, &flags); | |
2042 | running = task_running(rq, p); | |
2043 | task_rq_unlock(rq, &flags); | |
2044 | ||
2045 | if (likely(!running)) | |
2046 | break; | |
2047 | /* | |
2048 | * The switch count is incremented before the actual | |
2049 | * context switch. We thus wait for two switches to be | |
2050 | * sure at least one completed. | |
2051 | */ | |
2052 | if ((p->nvcsw - nvcsw) > 1) | |
2053 | break; | |
2054 | if ((p->nivcsw - nivcsw) > 1) | |
2055 | break; | |
2056 | ||
2057 | cpu_relax(); | |
2058 | } | |
2059 | } | |
2060 | ||
2061 | /* | |
2062 | * wait_task_inactive - wait for a thread to unschedule. | |
2063 | * | |
2064 | * If @match_state is nonzero, it's the @p->state value just checked and | |
2065 | * not expected to change. If it changes, i.e. @p might have woken up, | |
2066 | * then return zero. When we succeed in waiting for @p to be off its CPU, | |
2067 | * we return a positive number (its total switch count). If a second call | |
2068 | * a short while later returns the same number, the caller can be sure that | |
2069 | * @p has remained unscheduled the whole time. | |
2070 | * | |
2071 | * The caller must ensure that the task *will* unschedule sometime soon, | |
2072 | * else this function might spin for a *long* time. This function can't | |
2073 | * be called with interrupts off, or it may introduce deadlock with | |
2074 | * smp_call_function() if an IPI is sent by the same process we are | |
2075 | * waiting to become inactive. | |
2076 | */ | |
2077 | unsigned long wait_task_inactive(struct task_struct *p, long match_state) | |
2078 | { | |
2079 | unsigned long flags; | |
2080 | int running, on_rq; | |
2081 | unsigned long ncsw; | |
2082 | struct rq *rq; | |
2083 | ||
2084 | for (;;) { | |
2085 | /* | |
2086 | * We do the initial early heuristics without holding | |
2087 | * any task-queue locks at all. We'll only try to get | |
2088 | * the runqueue lock when things look like they will | |
2089 | * work out! | |
2090 | */ | |
2091 | rq = task_rq(p); | |
2092 | ||
2093 | /* | |
2094 | * If the task is actively running on another CPU | |
2095 | * still, just relax and busy-wait without holding | |
2096 | * any locks. | |
2097 | * | |
2098 | * NOTE! Since we don't hold any locks, it's not | |
2099 | * even sure that "rq" stays as the right runqueue! | |
2100 | * But we don't care, since "task_running()" will | |
2101 | * return false if the runqueue has changed and p | |
2102 | * is actually now running somewhere else! | |
2103 | */ | |
2104 | while (task_running(rq, p)) { | |
2105 | if (match_state && unlikely(p->state != match_state)) | |
2106 | return 0; | |
2107 | cpu_relax(); | |
2108 | } | |
2109 | ||
2110 | /* | |
2111 | * Ok, time to look more closely! We need the rq | |
2112 | * lock now, to be *sure*. If we're wrong, we'll | |
2113 | * just go back and repeat. | |
2114 | */ | |
2115 | rq = task_rq_lock(p, &flags); | |
2116 | trace_sched_wait_task(rq, p); | |
2117 | running = task_running(rq, p); | |
2118 | on_rq = p->se.on_rq; | |
2119 | ncsw = 0; | |
2120 | if (!match_state || p->state == match_state) | |
2121 | ncsw = p->nvcsw | LONG_MIN; /* sets MSB */ | |
2122 | task_rq_unlock(rq, &flags); | |
2123 | ||
2124 | /* | |
2125 | * If it changed from the expected state, bail out now. | |
2126 | */ | |
2127 | if (unlikely(!ncsw)) | |
2128 | break; | |
2129 | ||
2130 | /* | |
2131 | * Was it really running after all now that we | |
2132 | * checked with the proper locks actually held? | |
2133 | * | |
2134 | * Oops. Go back and try again.. | |
2135 | */ | |
2136 | if (unlikely(running)) { | |
2137 | cpu_relax(); | |
2138 | continue; | |
2139 | } | |
2140 | ||
2141 | /* | |
2142 | * It's not enough that it's not actively running, | |
2143 | * it must be off the runqueue _entirely_, and not | |
2144 | * preempted! | |
2145 | * | |
2146 | * So if it was still runnable (but just not actively | |
2147 | * running right now), it's preempted, and we should | |
2148 | * yield - it could be a while. | |
2149 | */ | |
2150 | if (unlikely(on_rq)) { | |
2151 | schedule_timeout_uninterruptible(1); | |
2152 | continue; | |
2153 | } | |
2154 | ||
2155 | /* | |
2156 | * Ahh, all good. It wasn't running, and it wasn't | |
2157 | * runnable, which means that it will never become | |
2158 | * running in the future either. We're all done! | |
2159 | */ | |
2160 | break; | |
2161 | } | |
2162 | ||
2163 | return ncsw; | |
2164 | } | |
2165 | ||
2166 | /*** | |
2167 | * kick_process - kick a running thread to enter/exit the kernel | |
2168 | * @p: the to-be-kicked thread | |
2169 | * | |
2170 | * Cause a process which is running on another CPU to enter | |
2171 | * kernel-mode, without any delay. (to get signals handled.) | |
2172 | * | |
2173 | * NOTE: this function doesnt have to take the runqueue lock, | |
2174 | * because all it wants to ensure is that the remote task enters | |
2175 | * the kernel. If the IPI races and the task has been migrated | |
2176 | * to another CPU then no harm is done and the purpose has been | |
2177 | * achieved as well. | |
2178 | */ | |
2179 | void kick_process(struct task_struct *p) | |
2180 | { | |
2181 | int cpu; | |
2182 | ||
2183 | preempt_disable(); | |
2184 | cpu = task_cpu(p); | |
2185 | if ((cpu != smp_processor_id()) && task_curr(p)) | |
2186 | smp_send_reschedule(cpu); | |
2187 | preempt_enable(); | |
2188 | } | |
2189 | ||
2190 | /* | |
2191 | * Return a low guess at the load of a migration-source cpu weighted | |
2192 | * according to the scheduling class and "nice" value. | |
2193 | * | |
2194 | * We want to under-estimate the load of migration sources, to | |
2195 | * balance conservatively. | |
2196 | */ | |
2197 | static unsigned long source_load(int cpu, int type) | |
2198 | { | |
2199 | struct rq *rq = cpu_rq(cpu); | |
2200 | unsigned long total = weighted_cpuload(cpu); | |
2201 | ||
2202 | if (type == 0 || !sched_feat(LB_BIAS)) | |
2203 | return total; | |
2204 | ||
2205 | return min(rq->cpu_load[type-1], total); | |
2206 | } | |
2207 | ||
2208 | /* | |
2209 | * Return a high guess at the load of a migration-target cpu weighted | |
2210 | * according to the scheduling class and "nice" value. | |
2211 | */ | |
2212 | static unsigned long target_load(int cpu, int type) | |
2213 | { | |
2214 | struct rq *rq = cpu_rq(cpu); | |
2215 | unsigned long total = weighted_cpuload(cpu); | |
2216 | ||
2217 | if (type == 0 || !sched_feat(LB_BIAS)) | |
2218 | return total; | |
2219 | ||
2220 | return max(rq->cpu_load[type-1], total); | |
2221 | } | |
2222 | ||
2223 | /* | |
2224 | * find_idlest_group finds and returns the least busy CPU group within the | |
2225 | * domain. | |
2226 | */ | |
2227 | static struct sched_group * | |
2228 | find_idlest_group(struct sched_domain *sd, struct task_struct *p, int this_cpu) | |
2229 | { | |
2230 | struct sched_group *idlest = NULL, *this = NULL, *group = sd->groups; | |
2231 | unsigned long min_load = ULONG_MAX, this_load = 0; | |
2232 | int load_idx = sd->forkexec_idx; | |
2233 | int imbalance = 100 + (sd->imbalance_pct-100)/2; | |
2234 | ||
2235 | do { | |
2236 | unsigned long load, avg_load; | |
2237 | int local_group; | |
2238 | int i; | |
2239 | ||
2240 | /* Skip over this group if it has no CPUs allowed */ | |
2241 | if (!cpumask_intersects(sched_group_cpus(group), | |
2242 | &p->cpus_allowed)) | |
2243 | continue; | |
2244 | ||
2245 | local_group = cpumask_test_cpu(this_cpu, | |
2246 | sched_group_cpus(group)); | |
2247 | ||
2248 | /* Tally up the load of all CPUs in the group */ | |
2249 | avg_load = 0; | |
2250 | ||
2251 | for_each_cpu(i, sched_group_cpus(group)) { | |
2252 | /* Bias balancing toward cpus of our domain */ | |
2253 | if (local_group) | |
2254 | load = source_load(i, load_idx); | |
2255 | else | |
2256 | load = target_load(i, load_idx); | |
2257 | ||
2258 | avg_load += load; | |
2259 | } | |
2260 | ||
2261 | /* Adjust by relative CPU power of the group */ | |
2262 | avg_load = sg_div_cpu_power(group, | |
2263 | avg_load * SCHED_LOAD_SCALE); | |
2264 | ||
2265 | if (local_group) { | |
2266 | this_load = avg_load; | |
2267 | this = group; | |
2268 | } else if (avg_load < min_load) { | |
2269 | min_load = avg_load; | |
2270 | idlest = group; | |
2271 | } | |
2272 | } while (group = group->next, group != sd->groups); | |
2273 | ||
2274 | if (!idlest || 100*this_load < imbalance*min_load) | |
2275 | return NULL; | |
2276 | return idlest; | |
2277 | } | |
2278 | ||
2279 | /* | |
2280 | * find_idlest_cpu - find the idlest cpu among the cpus in group. | |
2281 | */ | |
2282 | static int | |
2283 | find_idlest_cpu(struct sched_group *group, struct task_struct *p, int this_cpu) | |
2284 | { | |
2285 | unsigned long load, min_load = ULONG_MAX; | |
2286 | int idlest = -1; | |
2287 | int i; | |
2288 | ||
2289 | /* Traverse only the allowed CPUs */ | |
2290 | for_each_cpu_and(i, sched_group_cpus(group), &p->cpus_allowed) { | |
2291 | load = weighted_cpuload(i); | |
2292 | ||
2293 | if (load < min_load || (load == min_load && i == this_cpu)) { | |
2294 | min_load = load; | |
2295 | idlest = i; | |
2296 | } | |
2297 | } | |
2298 | ||
2299 | return idlest; | |
2300 | } | |
2301 | ||
2302 | /* | |
2303 | * sched_balance_self: balance the current task (running on cpu) in domains | |
2304 | * that have the 'flag' flag set. In practice, this is SD_BALANCE_FORK and | |
2305 | * SD_BALANCE_EXEC. | |
2306 | * | |
2307 | * Balance, ie. select the least loaded group. | |
2308 | * | |
2309 | * Returns the target CPU number, or the same CPU if no balancing is needed. | |
2310 | * | |
2311 | * preempt must be disabled. | |
2312 | */ | |
2313 | static int sched_balance_self(int cpu, int flag) | |
2314 | { | |
2315 | struct task_struct *t = current; | |
2316 | struct sched_domain *tmp, *sd = NULL; | |
2317 | ||
2318 | for_each_domain(cpu, tmp) { | |
2319 | /* | |
2320 | * If power savings logic is enabled for a domain, stop there. | |
2321 | */ | |
2322 | if (tmp->flags & SD_POWERSAVINGS_BALANCE) | |
2323 | break; | |
2324 | if (tmp->flags & flag) | |
2325 | sd = tmp; | |
2326 | } | |
2327 | ||
2328 | if (sd) | |
2329 | update_shares(sd); | |
2330 | ||
2331 | while (sd) { | |
2332 | struct sched_group *group; | |
2333 | int new_cpu, weight; | |
2334 | ||
2335 | if (!(sd->flags & flag)) { | |
2336 | sd = sd->child; | |
2337 | continue; | |
2338 | } | |
2339 | ||
2340 | group = find_idlest_group(sd, t, cpu); | |
2341 | if (!group) { | |
2342 | sd = sd->child; | |
2343 | continue; | |
2344 | } | |
2345 | ||
2346 | new_cpu = find_idlest_cpu(group, t, cpu); | |
2347 | if (new_cpu == -1 || new_cpu == cpu) { | |
2348 | /* Now try balancing at a lower domain level of cpu */ | |
2349 | sd = sd->child; | |
2350 | continue; | |
2351 | } | |
2352 | ||
2353 | /* Now try balancing at a lower domain level of new_cpu */ | |
2354 | cpu = new_cpu; | |
2355 | weight = cpumask_weight(sched_domain_span(sd)); | |
2356 | sd = NULL; | |
2357 | for_each_domain(cpu, tmp) { | |
2358 | if (weight <= cpumask_weight(sched_domain_span(tmp))) | |
2359 | break; | |
2360 | if (tmp->flags & flag) | |
2361 | sd = tmp; | |
2362 | } | |
2363 | /* while loop will break here if sd == NULL */ | |
2364 | } | |
2365 | ||
2366 | return cpu; | |
2367 | } | |
2368 | ||
2369 | #endif /* CONFIG_SMP */ | |
2370 | ||
2371 | /*** | |
2372 | * try_to_wake_up - wake up a thread | |
2373 | * @p: the to-be-woken-up thread | |
2374 | * @state: the mask of task states that can be woken | |
2375 | * @sync: do a synchronous wakeup? | |
2376 | * | |
2377 | * Put it on the run-queue if it's not already there. The "current" | |
2378 | * thread is always on the run-queue (except when the actual | |
2379 | * re-schedule is in progress), and as such you're allowed to do | |
2380 | * the simpler "current->state = TASK_RUNNING" to mark yourself | |
2381 | * runnable without the overhead of this. | |
2382 | * | |
2383 | * returns failure only if the task is already active. | |
2384 | */ | |
2385 | static int try_to_wake_up(struct task_struct *p, unsigned int state, int sync) | |
2386 | { | |
2387 | int cpu, orig_cpu, this_cpu, success = 0; | |
2388 | unsigned long flags; | |
2389 | long old_state; | |
2390 | struct rq *rq; | |
2391 | ||
2392 | if (!sched_feat(SYNC_WAKEUPS)) | |
2393 | sync = 0; | |
2394 | ||
2395 | #ifdef CONFIG_SMP | |
2396 | if (sched_feat(LB_WAKEUP_UPDATE) && !root_task_group_empty()) { | |
2397 | struct sched_domain *sd; | |
2398 | ||
2399 | this_cpu = raw_smp_processor_id(); | |
2400 | cpu = task_cpu(p); | |
2401 | ||
2402 | for_each_domain(this_cpu, sd) { | |
2403 | if (cpumask_test_cpu(cpu, sched_domain_span(sd))) { | |
2404 | update_shares(sd); | |
2405 | break; | |
2406 | } | |
2407 | } | |
2408 | } | |
2409 | #endif | |
2410 | ||
2411 | smp_wmb(); | |
2412 | rq = task_rq_lock(p, &flags); | |
2413 | update_rq_clock(rq); | |
2414 | old_state = p->state; | |
2415 | if (!(old_state & state)) | |
2416 | goto out; | |
2417 | ||
2418 | if (p->se.on_rq) | |
2419 | goto out_running; | |
2420 | ||
2421 | cpu = task_cpu(p); | |
2422 | orig_cpu = cpu; | |
2423 | this_cpu = smp_processor_id(); | |
2424 | ||
2425 | #ifdef CONFIG_SMP | |
2426 | if (unlikely(task_running(rq, p))) | |
2427 | goto out_activate; | |
2428 | ||
2429 | cpu = p->sched_class->select_task_rq(p, sync); | |
2430 | if (cpu != orig_cpu) { | |
2431 | set_task_cpu(p, cpu); | |
2432 | task_rq_unlock(rq, &flags); | |
2433 | /* might preempt at this point */ | |
2434 | rq = task_rq_lock(p, &flags); | |
2435 | old_state = p->state; | |
2436 | if (!(old_state & state)) | |
2437 | goto out; | |
2438 | if (p->se.on_rq) | |
2439 | goto out_running; | |
2440 | ||
2441 | this_cpu = smp_processor_id(); | |
2442 | cpu = task_cpu(p); | |
2443 | } | |
2444 | ||
2445 | #ifdef CONFIG_SCHEDSTATS | |
2446 | schedstat_inc(rq, ttwu_count); | |
2447 | if (cpu == this_cpu) | |
2448 | schedstat_inc(rq, ttwu_local); | |
2449 | else { | |
2450 | struct sched_domain *sd; | |
2451 | for_each_domain(this_cpu, sd) { | |
2452 | if (cpumask_test_cpu(cpu, sched_domain_span(sd))) { | |
2453 | schedstat_inc(sd, ttwu_wake_remote); | |
2454 | break; | |
2455 | } | |
2456 | } | |
2457 | } | |
2458 | #endif /* CONFIG_SCHEDSTATS */ | |
2459 | ||
2460 | out_activate: | |
2461 | #endif /* CONFIG_SMP */ | |
2462 | schedstat_inc(p, se.nr_wakeups); | |
2463 | if (sync) | |
2464 | schedstat_inc(p, se.nr_wakeups_sync); | |
2465 | if (orig_cpu != cpu) | |
2466 | schedstat_inc(p, se.nr_wakeups_migrate); | |
2467 | if (cpu == this_cpu) | |
2468 | schedstat_inc(p, se.nr_wakeups_local); | |
2469 | else | |
2470 | schedstat_inc(p, se.nr_wakeups_remote); | |
2471 | activate_task(rq, p, 1); | |
2472 | success = 1; | |
2473 | ||
2474 | /* | |
2475 | * Only attribute actual wakeups done by this task. | |
2476 | */ | |
2477 | if (!in_interrupt()) { | |
2478 | struct sched_entity *se = ¤t->se; | |
2479 | u64 sample = se->sum_exec_runtime; | |
2480 | ||
2481 | if (se->last_wakeup) | |
2482 | sample -= se->last_wakeup; | |
2483 | else | |
2484 | sample -= se->start_runtime; | |
2485 | update_avg(&se->avg_wakeup, sample); | |
2486 | ||
2487 | se->last_wakeup = se->sum_exec_runtime; | |
2488 | } | |
2489 | ||
2490 | out_running: | |
2491 | trace_sched_wakeup(rq, p, success); | |
2492 | check_preempt_curr(rq, p, sync); | |
2493 | ||
2494 | p->state = TASK_RUNNING; | |
2495 | #ifdef CONFIG_SMP | |
2496 | if (p->sched_class->task_wake_up) | |
2497 | p->sched_class->task_wake_up(rq, p); | |
2498 | #endif | |
2499 | out: | |
2500 | task_rq_unlock(rq, &flags); | |
2501 | ||
2502 | return success; | |
2503 | } | |
2504 | ||
2505 | /** | |
2506 | * wake_up_process - Wake up a specific process | |
2507 | * @p: The process to be woken up. | |
2508 | * | |
2509 | * Attempt to wake up the nominated process and move it to the set of runnable | |
2510 | * processes. Returns 1 if the process was woken up, 0 if it was already | |
2511 | * running. | |
2512 | * | |
2513 | * It may be assumed that this function implies a write memory barrier before | |
2514 | * changing the task state if and only if any tasks are woken up. | |
2515 | */ | |
2516 | int wake_up_process(struct task_struct *p) | |
2517 | { | |
2518 | return try_to_wake_up(p, TASK_ALL, 0); | |
2519 | } | |
2520 | EXPORT_SYMBOL(wake_up_process); | |
2521 | ||
2522 | int wake_up_state(struct task_struct *p, unsigned int state) | |
2523 | { | |
2524 | return try_to_wake_up(p, state, 0); | |
2525 | } | |
2526 | ||
2527 | /* | |
2528 | * Perform scheduler related setup for a newly forked process p. | |
2529 | * p is forked by current. | |
2530 | * | |
2531 | * __sched_fork() is basic setup used by init_idle() too: | |
2532 | */ | |
2533 | static void __sched_fork(struct task_struct *p) | |
2534 | { | |
2535 | p->se.exec_start = 0; | |
2536 | p->se.sum_exec_runtime = 0; | |
2537 | p->se.prev_sum_exec_runtime = 0; | |
2538 | p->se.last_wakeup = 0; | |
2539 | p->se.avg_overlap = 0; | |
2540 | p->se.start_runtime = 0; | |
2541 | p->se.avg_wakeup = sysctl_sched_wakeup_granularity; | |
2542 | ||
2543 | #ifdef CONFIG_SCHEDSTATS | |
2544 | p->se.wait_start = 0; | |
2545 | p->se.sum_sleep_runtime = 0; | |
2546 | p->se.sleep_start = 0; | |
2547 | p->se.block_start = 0; | |
2548 | p->se.sleep_max = 0; | |
2549 | p->se.block_max = 0; | |
2550 | p->se.exec_max = 0; | |
2551 | p->se.slice_max = 0; | |
2552 | p->se.wait_max = 0; | |
2553 | #endif | |
2554 | ||
2555 | INIT_LIST_HEAD(&p->rt.run_list); | |
2556 | p->se.on_rq = 0; | |
2557 | INIT_LIST_HEAD(&p->se.group_node); | |
2558 | ||
2559 | #ifdef CONFIG_PREEMPT_NOTIFIERS | |
2560 | INIT_HLIST_HEAD(&p->preempt_notifiers); | |
2561 | #endif | |
2562 | ||
2563 | /* | |
2564 | * We mark the process as running here, but have not actually | |
2565 | * inserted it onto the runqueue yet. This guarantees that | |
2566 | * nobody will actually run it, and a signal or other external | |
2567 | * event cannot wake it up and insert it on the runqueue either. | |
2568 | */ | |
2569 | p->state = TASK_RUNNING; | |
2570 | } | |
2571 | ||
2572 | /* | |
2573 | * fork()/clone()-time setup: | |
2574 | */ | |
2575 | void sched_fork(struct task_struct *p, int clone_flags) | |
2576 | { | |
2577 | int cpu = get_cpu(); | |
2578 | ||
2579 | __sched_fork(p); | |
2580 | ||
2581 | #ifdef CONFIG_SMP | |
2582 | cpu = sched_balance_self(cpu, SD_BALANCE_FORK); | |
2583 | #endif | |
2584 | set_task_cpu(p, cpu); | |
2585 | ||
2586 | /* | |
2587 | * Make sure we do not leak PI boosting priority to the child: | |
2588 | */ | |
2589 | p->prio = current->normal_prio; | |
2590 | if (!rt_prio(p->prio)) | |
2591 | p->sched_class = &fair_sched_class; | |
2592 | ||
2593 | #if defined(CONFIG_SCHEDSTATS) || defined(CONFIG_TASK_DELAY_ACCT) | |
2594 | if (likely(sched_info_on())) | |
2595 | memset(&p->sched_info, 0, sizeof(p->sched_info)); | |
2596 | #endif | |
2597 | #if defined(CONFIG_SMP) && defined(__ARCH_WANT_UNLOCKED_CTXSW) | |
2598 | p->oncpu = 0; | |
2599 | #endif | |
2600 | #ifdef CONFIG_PREEMPT | |
2601 | /* Want to start with kernel preemption disabled. */ | |
2602 | task_thread_info(p)->preempt_count = 1; | |
2603 | #endif | |
2604 | plist_node_init(&p->pushable_tasks, MAX_PRIO); | |
2605 | ||
2606 | put_cpu(); | |
2607 | } | |
2608 | ||
2609 | /* | |
2610 | * wake_up_new_task - wake up a newly created task for the first time. | |
2611 | * | |
2612 | * This function will do some initial scheduler statistics housekeeping | |
2613 | * that must be done for every newly created context, then puts the task | |
2614 | * on the runqueue and wakes it. | |
2615 | */ | |
2616 | void wake_up_new_task(struct task_struct *p, unsigned long clone_flags) | |
2617 | { | |
2618 | unsigned long flags; | |
2619 | struct rq *rq; | |
2620 | ||
2621 | rq = task_rq_lock(p, &flags); | |
2622 | BUG_ON(p->state != TASK_RUNNING); | |
2623 | update_rq_clock(rq); | |
2624 | ||
2625 | p->prio = effective_prio(p); | |
2626 | ||
2627 | if (!p->sched_class->task_new || !current->se.on_rq) { | |
2628 | activate_task(rq, p, 0); | |
2629 | } else { | |
2630 | /* | |
2631 | * Let the scheduling class do new task startup | |
2632 | * management (if any): | |
2633 | */ | |
2634 | p->sched_class->task_new(rq, p); | |
2635 | inc_nr_running(rq); | |
2636 | } | |
2637 | trace_sched_wakeup_new(rq, p, 1); | |
2638 | check_preempt_curr(rq, p, 0); | |
2639 | #ifdef CONFIG_SMP | |
2640 | if (p->sched_class->task_wake_up) | |
2641 | p->sched_class->task_wake_up(rq, p); | |
2642 | #endif | |
2643 | task_rq_unlock(rq, &flags); | |
2644 | } | |
2645 | ||
2646 | #ifdef CONFIG_PREEMPT_NOTIFIERS | |
2647 | ||
2648 | /** | |
2649 | * preempt_notifier_register - tell me when current is being preempted & rescheduled | |
2650 | * @notifier: notifier struct to register | |
2651 | */ | |
2652 | void preempt_notifier_register(struct preempt_notifier *notifier) | |
2653 | { | |
2654 | hlist_add_head(¬ifier->link, ¤t->preempt_notifiers); | |
2655 | } | |
2656 | EXPORT_SYMBOL_GPL(preempt_notifier_register); | |
2657 | ||
2658 | /** | |
2659 | * preempt_notifier_unregister - no longer interested in preemption notifications | |
2660 | * @notifier: notifier struct to unregister | |
2661 | * | |
2662 | * This is safe to call from within a preemption notifier. | |
2663 | */ | |
2664 | void preempt_notifier_unregister(struct preempt_notifier *notifier) | |
2665 | { | |
2666 | hlist_del(¬ifier->link); | |
2667 | } | |
2668 | EXPORT_SYMBOL_GPL(preempt_notifier_unregister); | |
2669 | ||
2670 | static void fire_sched_in_preempt_notifiers(struct task_struct *curr) | |
2671 | { | |
2672 | struct preempt_notifier *notifier; | |
2673 | struct hlist_node *node; | |
2674 | ||
2675 | hlist_for_each_entry(notifier, node, &curr->preempt_notifiers, link) | |
2676 | notifier->ops->sched_in(notifier, raw_smp_processor_id()); | |
2677 | } | |
2678 | ||
2679 | static void | |
2680 | fire_sched_out_preempt_notifiers(struct task_struct *curr, | |
2681 | struct task_struct *next) | |
2682 | { | |
2683 | struct preempt_notifier *notifier; | |
2684 | struct hlist_node *node; | |
2685 | ||
2686 | hlist_for_each_entry(notifier, node, &curr->preempt_notifiers, link) | |
2687 | notifier->ops->sched_out(notifier, next); | |
2688 | } | |
2689 | ||
2690 | #else /* !CONFIG_PREEMPT_NOTIFIERS */ | |
2691 | ||
2692 | static void fire_sched_in_preempt_notifiers(struct task_struct *curr) | |
2693 | { | |
2694 | } | |
2695 | ||
2696 | static void | |
2697 | fire_sched_out_preempt_notifiers(struct task_struct *curr, | |
2698 | struct task_struct *next) | |
2699 | { | |
2700 | } | |
2701 | ||
2702 | #endif /* CONFIG_PREEMPT_NOTIFIERS */ | |
2703 | ||
2704 | /** | |
2705 | * prepare_task_switch - prepare to switch tasks | |
2706 | * @rq: the runqueue preparing to switch | |
2707 | * @prev: the current task that is being switched out | |
2708 | * @next: the task we are going to switch to. | |
2709 | * | |
2710 | * This is called with the rq lock held and interrupts off. It must | |
2711 | * be paired with a subsequent finish_task_switch after the context | |
2712 | * switch. | |
2713 | * | |
2714 | * prepare_task_switch sets up locking and calls architecture specific | |
2715 | * hooks. | |
2716 | */ | |
2717 | static inline void | |
2718 | prepare_task_switch(struct rq *rq, struct task_struct *prev, | |
2719 | struct task_struct *next) | |
2720 | { | |
2721 | fire_sched_out_preempt_notifiers(prev, next); | |
2722 | prepare_lock_switch(rq, next); | |
2723 | prepare_arch_switch(next); | |
2724 | } | |
2725 | ||
2726 | /** | |
2727 | * finish_task_switch - clean up after a task-switch | |
2728 | * @rq: runqueue associated with task-switch | |
2729 | * @prev: the thread we just switched away from. | |
2730 | * | |
2731 | * finish_task_switch must be called after the context switch, paired | |
2732 | * with a prepare_task_switch call before the context switch. | |
2733 | * finish_task_switch will reconcile locking set up by prepare_task_switch, | |
2734 | * and do any other architecture-specific cleanup actions. | |
2735 | * | |
2736 | * Note that we may have delayed dropping an mm in context_switch(). If | |
2737 | * so, we finish that here outside of the runqueue lock. (Doing it | |
2738 | * with the lock held can cause deadlocks; see schedule() for | |
2739 | * details.) | |
2740 | */ | |
2741 | static void finish_task_switch(struct rq *rq, struct task_struct *prev) | |
2742 | __releases(rq->lock) | |
2743 | { | |
2744 | struct mm_struct *mm = rq->prev_mm; | |
2745 | long prev_state; | |
2746 | #ifdef CONFIG_SMP | |
2747 | int post_schedule = 0; | |
2748 | ||
2749 | if (current->sched_class->needs_post_schedule) | |
2750 | post_schedule = current->sched_class->needs_post_schedule(rq); | |
2751 | #endif | |
2752 | ||
2753 | rq->prev_mm = NULL; | |
2754 | ||
2755 | /* | |
2756 | * A task struct has one reference for the use as "current". | |
2757 | * If a task dies, then it sets TASK_DEAD in tsk->state and calls | |
2758 | * schedule one last time. The schedule call will never return, and | |
2759 | * the scheduled task must drop that reference. | |
2760 | * The test for TASK_DEAD must occur while the runqueue locks are | |
2761 | * still held, otherwise prev could be scheduled on another cpu, die | |
2762 | * there before we look at prev->state, and then the reference would | |
2763 | * be dropped twice. | |
2764 | * Manfred Spraul <manfred@colorfullife.com> | |
2765 | */ | |
2766 | prev_state = prev->state; | |
2767 | finish_arch_switch(prev); | |
2768 | finish_lock_switch(rq, prev); | |
2769 | #ifdef CONFIG_SMP | |
2770 | if (post_schedule) | |
2771 | current->sched_class->post_schedule(rq); | |
2772 | #endif | |
2773 | ||
2774 | fire_sched_in_preempt_notifiers(current); | |
2775 | if (mm) | |
2776 | mmdrop(mm); | |
2777 | if (unlikely(prev_state == TASK_DEAD)) { | |
2778 | /* | |
2779 | * Remove function-return probe instances associated with this | |
2780 | * task and put them back on the free list. | |
2781 | */ | |
2782 | kprobe_flush_task(prev); | |
2783 | put_task_struct(prev); | |
2784 | } | |
2785 | } | |
2786 | ||
2787 | /** | |
2788 | * schedule_tail - first thing a freshly forked thread must call. | |
2789 | * @prev: the thread we just switched away from. | |
2790 | */ | |
2791 | asmlinkage void schedule_tail(struct task_struct *prev) | |
2792 | __releases(rq->lock) | |
2793 | { | |
2794 | struct rq *rq = this_rq(); | |
2795 | ||
2796 | finish_task_switch(rq, prev); | |
2797 | #ifdef __ARCH_WANT_UNLOCKED_CTXSW | |
2798 | /* In this case, finish_task_switch does not reenable preemption */ | |
2799 | preempt_enable(); | |
2800 | #endif | |
2801 | if (current->set_child_tid) | |
2802 | put_user(task_pid_vnr(current), current->set_child_tid); | |
2803 | } | |
2804 | ||
2805 | /* | |
2806 | * context_switch - switch to the new MM and the new | |
2807 | * thread's register state. | |
2808 | */ | |
2809 | static inline void | |
2810 | context_switch(struct rq *rq, struct task_struct *prev, | |
2811 | struct task_struct *next) | |
2812 | { | |
2813 | struct mm_struct *mm, *oldmm; | |
2814 | ||
2815 | prepare_task_switch(rq, prev, next); | |
2816 | trace_sched_switch(rq, prev, next); | |
2817 | mm = next->mm; | |
2818 | oldmm = prev->active_mm; | |
2819 | /* | |
2820 | * For paravirt, this is coupled with an exit in switch_to to | |
2821 | * combine the page table reload and the switch backend into | |
2822 | * one hypercall. | |
2823 | */ | |
2824 | arch_start_context_switch(prev); | |
2825 | ||
2826 | if (unlikely(!mm)) { | |
2827 | next->active_mm = oldmm; | |
2828 | atomic_inc(&oldmm->mm_count); | |
2829 | enter_lazy_tlb(oldmm, next); | |
2830 | } else | |
2831 | switch_mm(oldmm, mm, next); | |
2832 | ||
2833 | if (unlikely(!prev->mm)) { | |
2834 | prev->active_mm = NULL; | |
2835 | rq->prev_mm = oldmm; | |
2836 | } | |
2837 | /* | |
2838 | * Since the runqueue lock will be released by the next | |
2839 | * task (which is an invalid locking op but in the case | |
2840 | * of the scheduler it's an obvious special-case), so we | |
2841 | * do an early lockdep release here: | |
2842 | */ | |
2843 | #ifndef __ARCH_WANT_UNLOCKED_CTXSW | |
2844 | spin_release(&rq->lock.dep_map, 1, _THIS_IP_); | |
2845 | #endif | |
2846 | ||
2847 | /* Here we just switch the register state and the stack. */ | |
2848 | switch_to(prev, next, prev); | |
2849 | ||
2850 | barrier(); | |
2851 | /* | |
2852 | * this_rq must be evaluated again because prev may have moved | |
2853 | * CPUs since it called schedule(), thus the 'rq' on its stack | |
2854 | * frame will be invalid. | |
2855 | */ | |
2856 | finish_task_switch(this_rq(), prev); | |
2857 | } | |
2858 | ||
2859 | /* | |
2860 | * nr_running, nr_uninterruptible and nr_context_switches: | |
2861 | * | |
2862 | * externally visible scheduler statistics: current number of runnable | |
2863 | * threads, current number of uninterruptible-sleeping threads, total | |
2864 | * number of context switches performed since bootup. | |
2865 | */ | |
2866 | unsigned long nr_running(void) | |
2867 | { | |
2868 | unsigned long i, sum = 0; | |
2869 | ||
2870 | for_each_online_cpu(i) | |
2871 | sum += cpu_rq(i)->nr_running; | |
2872 | ||
2873 | return sum; | |
2874 | } | |
2875 | ||
2876 | unsigned long nr_uninterruptible(void) | |
2877 | { | |
2878 | unsigned long i, sum = 0; | |
2879 | ||
2880 | for_each_possible_cpu(i) | |
2881 | sum += cpu_rq(i)->nr_uninterruptible; | |
2882 | ||
2883 | /* | |
2884 | * Since we read the counters lockless, it might be slightly | |
2885 | * inaccurate. Do not allow it to go below zero though: | |
2886 | */ | |
2887 | if (unlikely((long)sum < 0)) | |
2888 | sum = 0; | |
2889 | ||
2890 | return sum; | |
2891 | } | |
2892 | ||
2893 | unsigned long long nr_context_switches(void) | |
2894 | { | |
2895 | int i; | |
2896 | unsigned long long sum = 0; | |
2897 | ||
2898 | for_each_possible_cpu(i) | |
2899 | sum += cpu_rq(i)->nr_switches; | |
2900 | ||
2901 | return sum; | |
2902 | } | |
2903 | ||
2904 | unsigned long nr_iowait(void) | |
2905 | { | |
2906 | unsigned long i, sum = 0; | |
2907 | ||
2908 | for_each_possible_cpu(i) | |
2909 | sum += atomic_read(&cpu_rq(i)->nr_iowait); | |
2910 | ||
2911 | return sum; | |
2912 | } | |
2913 | ||
2914 | /* Variables and functions for calc_load */ | |
2915 | static atomic_long_t calc_load_tasks; | |
2916 | static unsigned long calc_load_update; | |
2917 | unsigned long avenrun[3]; | |
2918 | EXPORT_SYMBOL(avenrun); | |
2919 | ||
2920 | /** | |
2921 | * get_avenrun - get the load average array | |
2922 | * @loads: pointer to dest load array | |
2923 | * @offset: offset to add | |
2924 | * @shift: shift count to shift the result left | |
2925 | * | |
2926 | * These values are estimates at best, so no need for locking. | |
2927 | */ | |
2928 | void get_avenrun(unsigned long *loads, unsigned long offset, int shift) | |
2929 | { | |
2930 | loads[0] = (avenrun[0] + offset) << shift; | |
2931 | loads[1] = (avenrun[1] + offset) << shift; | |
2932 | loads[2] = (avenrun[2] + offset) << shift; | |
2933 | } | |
2934 | ||
2935 | static unsigned long | |
2936 | calc_load(unsigned long load, unsigned long exp, unsigned long active) | |
2937 | { | |
2938 | load *= exp; | |
2939 | load += active * (FIXED_1 - exp); | |
2940 | return load >> FSHIFT; | |
2941 | } | |
2942 | ||
2943 | /* | |
2944 | * calc_load - update the avenrun load estimates 10 ticks after the | |
2945 | * CPUs have updated calc_load_tasks. | |
2946 | */ | |
2947 | void calc_global_load(void) | |
2948 | { | |
2949 | unsigned long upd = calc_load_update + 10; | |
2950 | long active; | |
2951 | ||
2952 | if (time_before(jiffies, upd)) | |
2953 | return; | |
2954 | ||
2955 | active = atomic_long_read(&calc_load_tasks); | |
2956 | active = active > 0 ? active * FIXED_1 : 0; | |
2957 | ||
2958 | avenrun[0] = calc_load(avenrun[0], EXP_1, active); | |
2959 | avenrun[1] = calc_load(avenrun[1], EXP_5, active); | |
2960 | avenrun[2] = calc_load(avenrun[2], EXP_15, active); | |
2961 | ||
2962 | calc_load_update += LOAD_FREQ; | |
2963 | } | |
2964 | ||
2965 | /* | |
2966 | * Either called from update_cpu_load() or from a cpu going idle | |
2967 | */ | |
2968 | static void calc_load_account_active(struct rq *this_rq) | |
2969 | { | |
2970 | long nr_active, delta; | |
2971 | ||
2972 | nr_active = this_rq->nr_running; | |
2973 | nr_active += (long) this_rq->nr_uninterruptible; | |
2974 | ||
2975 | if (nr_active != this_rq->calc_load_active) { | |
2976 | delta = nr_active - this_rq->calc_load_active; | |
2977 | this_rq->calc_load_active = nr_active; | |
2978 | atomic_long_add(delta, &calc_load_tasks); | |
2979 | } | |
2980 | } | |
2981 | ||
2982 | /* | |
2983 | * Update rq->cpu_load[] statistics. This function is usually called every | |
2984 | * scheduler tick (TICK_NSEC). | |
2985 | */ | |
2986 | static void update_cpu_load(struct rq *this_rq) | |
2987 | { | |
2988 | unsigned long this_load = this_rq->load.weight; | |
2989 | int i, scale; | |
2990 | ||
2991 | this_rq->nr_load_updates++; | |
2992 | ||
2993 | /* Update our load: */ | |
2994 | for (i = 0, scale = 1; i < CPU_LOAD_IDX_MAX; i++, scale += scale) { | |
2995 | unsigned long old_load, new_load; | |
2996 | ||
2997 | /* scale is effectively 1 << i now, and >> i divides by scale */ | |
2998 | ||
2999 | old_load = this_rq->cpu_load[i]; | |
3000 | new_load = this_load; | |
3001 | /* | |
3002 | * Round up the averaging division if load is increasing. This | |
3003 | * prevents us from getting stuck on 9 if the load is 10, for | |
3004 | * example. | |
3005 | */ | |
3006 | if (new_load > old_load) | |
3007 | new_load += scale-1; | |
3008 | this_rq->cpu_load[i] = (old_load*(scale-1) + new_load) >> i; | |
3009 | } | |
3010 | ||
3011 | if (time_after_eq(jiffies, this_rq->calc_load_update)) { | |
3012 | this_rq->calc_load_update += LOAD_FREQ; | |
3013 | calc_load_account_active(this_rq); | |
3014 | } | |
3015 | } | |
3016 | ||
3017 | #ifdef CONFIG_SMP | |
3018 | ||
3019 | /* | |
3020 | * double_rq_lock - safely lock two runqueues | |
3021 | * | |
3022 | * Note this does not disable interrupts like task_rq_lock, | |
3023 | * you need to do so manually before calling. | |
3024 | */ | |
3025 | static void double_rq_lock(struct rq *rq1, struct rq *rq2) | |
3026 | __acquires(rq1->lock) | |
3027 | __acquires(rq2->lock) | |
3028 | { | |
3029 | BUG_ON(!irqs_disabled()); | |
3030 | if (rq1 == rq2) { | |
3031 | spin_lock(&rq1->lock); | |
3032 | __acquire(rq2->lock); /* Fake it out ;) */ | |
3033 | } else { | |
3034 | if (rq1 < rq2) { | |
3035 | spin_lock(&rq1->lock); | |
3036 | spin_lock_nested(&rq2->lock, SINGLE_DEPTH_NESTING); | |
3037 | } else { | |
3038 | spin_lock(&rq2->lock); | |
3039 | spin_lock_nested(&rq1->lock, SINGLE_DEPTH_NESTING); | |
3040 | } | |
3041 | } | |
3042 | update_rq_clock(rq1); | |
3043 | update_rq_clock(rq2); | |
3044 | } | |
3045 | ||
3046 | /* | |
3047 | * double_rq_unlock - safely unlock two runqueues | |
3048 | * | |
3049 | * Note this does not restore interrupts like task_rq_unlock, | |
3050 | * you need to do so manually after calling. | |
3051 | */ | |
3052 | static void double_rq_unlock(struct rq *rq1, struct rq *rq2) | |
3053 | __releases(rq1->lock) | |
3054 | __releases(rq2->lock) | |
3055 | { | |
3056 | spin_unlock(&rq1->lock); | |
3057 | if (rq1 != rq2) | |
3058 | spin_unlock(&rq2->lock); | |
3059 | else | |
3060 | __release(rq2->lock); | |
3061 | } | |
3062 | ||
3063 | /* | |
3064 | * If dest_cpu is allowed for this process, migrate the task to it. | |
3065 | * This is accomplished by forcing the cpu_allowed mask to only | |
3066 | * allow dest_cpu, which will force the cpu onto dest_cpu. Then | |
3067 | * the cpu_allowed mask is restored. | |
3068 | */ | |
3069 | static void sched_migrate_task(struct task_struct *p, int dest_cpu) | |
3070 | { | |
3071 | struct migration_req req; | |
3072 | unsigned long flags; | |
3073 | struct rq *rq; | |
3074 | ||
3075 | rq = task_rq_lock(p, &flags); | |
3076 | if (!cpumask_test_cpu(dest_cpu, &p->cpus_allowed) | |
3077 | || unlikely(!cpu_active(dest_cpu))) | |
3078 | goto out; | |
3079 | ||
3080 | /* force the process onto the specified CPU */ | |
3081 | if (migrate_task(p, dest_cpu, &req)) { | |
3082 | /* Need to wait for migration thread (might exit: take ref). */ | |
3083 | struct task_struct *mt = rq->migration_thread; | |
3084 | ||
3085 | get_task_struct(mt); | |
3086 | task_rq_unlock(rq, &flags); | |
3087 | wake_up_process(mt); | |
3088 | put_task_struct(mt); | |
3089 | wait_for_completion(&req.done); | |
3090 | ||
3091 | return; | |
3092 | } | |
3093 | out: | |
3094 | task_rq_unlock(rq, &flags); | |
3095 | } | |
3096 | ||
3097 | /* | |
3098 | * sched_exec - execve() is a valuable balancing opportunity, because at | |
3099 | * this point the task has the smallest effective memory and cache footprint. | |
3100 | */ | |
3101 | void sched_exec(void) | |
3102 | { | |
3103 | int new_cpu, this_cpu = get_cpu(); | |
3104 | new_cpu = sched_balance_self(this_cpu, SD_BALANCE_EXEC); | |
3105 | put_cpu(); | |
3106 | if (new_cpu != this_cpu) | |
3107 | sched_migrate_task(current, new_cpu); | |
3108 | } | |
3109 | ||
3110 | /* | |
3111 | * pull_task - move a task from a remote runqueue to the local runqueue. | |
3112 | * Both runqueues must be locked. | |
3113 | */ | |
3114 | static void pull_task(struct rq *src_rq, struct task_struct *p, | |
3115 | struct rq *this_rq, int this_cpu) | |
3116 | { | |
3117 | deactivate_task(src_rq, p, 0); | |
3118 | set_task_cpu(p, this_cpu); | |
3119 | activate_task(this_rq, p, 0); | |
3120 | /* | |
3121 | * Note that idle threads have a prio of MAX_PRIO, for this test | |
3122 | * to be always true for them. | |
3123 | */ | |
3124 | check_preempt_curr(this_rq, p, 0); | |
3125 | } | |
3126 | ||
3127 | /* | |
3128 | * can_migrate_task - may task p from runqueue rq be migrated to this_cpu? | |
3129 | */ | |
3130 | static | |
3131 | int can_migrate_task(struct task_struct *p, struct rq *rq, int this_cpu, | |
3132 | struct sched_domain *sd, enum cpu_idle_type idle, | |
3133 | int *all_pinned) | |
3134 | { | |
3135 | int tsk_cache_hot = 0; | |
3136 | /* | |
3137 | * We do not migrate tasks that are: | |
3138 | * 1) running (obviously), or | |
3139 | * 2) cannot be migrated to this CPU due to cpus_allowed, or | |
3140 | * 3) are cache-hot on their current CPU. | |
3141 | */ | |
3142 | if (!cpumask_test_cpu(this_cpu, &p->cpus_allowed)) { | |
3143 | schedstat_inc(p, se.nr_failed_migrations_affine); | |
3144 | return 0; | |
3145 | } | |
3146 | *all_pinned = 0; | |
3147 | ||
3148 | if (task_running(rq, p)) { | |
3149 | schedstat_inc(p, se.nr_failed_migrations_running); | |
3150 | return 0; | |
3151 | } | |
3152 | ||
3153 | /* | |
3154 | * Aggressive migration if: | |
3155 | * 1) task is cache cold, or | |
3156 | * 2) too many balance attempts have failed. | |
3157 | */ | |
3158 | ||
3159 | tsk_cache_hot = task_hot(p, rq->clock, sd); | |
3160 | if (!tsk_cache_hot || | |
3161 | sd->nr_balance_failed > sd->cache_nice_tries) { | |
3162 | #ifdef CONFIG_SCHEDSTATS | |
3163 | if (tsk_cache_hot) { | |
3164 | schedstat_inc(sd, lb_hot_gained[idle]); | |
3165 | schedstat_inc(p, se.nr_forced_migrations); | |
3166 | } | |
3167 | #endif | |
3168 | return 1; | |
3169 | } | |
3170 | ||
3171 | if (tsk_cache_hot) { | |
3172 | schedstat_inc(p, se.nr_failed_migrations_hot); | |
3173 | return 0; | |
3174 | } | |
3175 | return 1; | |
3176 | } | |
3177 | ||
3178 | static unsigned long | |
3179 | balance_tasks(struct rq *this_rq, int this_cpu, struct rq *busiest, | |
3180 | unsigned long max_load_move, struct sched_domain *sd, | |
3181 | enum cpu_idle_type idle, int *all_pinned, | |
3182 | int *this_best_prio, struct rq_iterator *iterator) | |
3183 | { | |
3184 | int loops = 0, pulled = 0, pinned = 0; | |
3185 | struct task_struct *p; | |
3186 | long rem_load_move = max_load_move; | |
3187 | ||
3188 | if (max_load_move == 0) | |
3189 | goto out; | |
3190 | ||
3191 | pinned = 1; | |
3192 | ||
3193 | /* | |
3194 | * Start the load-balancing iterator: | |
3195 | */ | |
3196 | p = iterator->start(iterator->arg); | |
3197 | next: | |
3198 | if (!p || loops++ > sysctl_sched_nr_migrate) | |
3199 | goto out; | |
3200 | ||
3201 | if ((p->se.load.weight >> 1) > rem_load_move || | |
3202 | !can_migrate_task(p, busiest, this_cpu, sd, idle, &pinned)) { | |
3203 | p = iterator->next(iterator->arg); | |
3204 | goto next; | |
3205 | } | |
3206 | ||
3207 | pull_task(busiest, p, this_rq, this_cpu); | |
3208 | pulled++; | |
3209 | rem_load_move -= p->se.load.weight; | |
3210 | ||
3211 | #ifdef CONFIG_PREEMPT | |
3212 | /* | |
3213 | * NEWIDLE balancing is a source of latency, so preemptible kernels | |
3214 | * will stop after the first task is pulled to minimize the critical | |
3215 | * section. | |
3216 | */ | |
3217 | if (idle == CPU_NEWLY_IDLE) | |
3218 | goto out; | |
3219 | #endif | |
3220 | ||
3221 | /* | |
3222 | * We only want to steal up to the prescribed amount of weighted load. | |
3223 | */ | |
3224 | if (rem_load_move > 0) { | |
3225 | if (p->prio < *this_best_prio) | |
3226 | *this_best_prio = p->prio; | |
3227 | p = iterator->next(iterator->arg); | |
3228 | goto next; | |
3229 | } | |
3230 | out: | |
3231 | /* | |
3232 | * Right now, this is one of only two places pull_task() is called, | |
3233 | * so we can safely collect pull_task() stats here rather than | |
3234 | * inside pull_task(). | |
3235 | */ | |
3236 | schedstat_add(sd, lb_gained[idle], pulled); | |
3237 | ||
3238 | if (all_pinned) | |
3239 | *all_pinned = pinned; | |
3240 | ||
3241 | return max_load_move - rem_load_move; | |
3242 | } | |
3243 | ||
3244 | /* | |
3245 | * move_tasks tries to move up to max_load_move weighted load from busiest to | |
3246 | * this_rq, as part of a balancing operation within domain "sd". | |
3247 | * Returns 1 if successful and 0 otherwise. | |
3248 | * | |
3249 | * Called with both runqueues locked. | |
3250 | */ | |
3251 | static int move_tasks(struct rq *this_rq, int this_cpu, struct rq *busiest, | |
3252 | unsigned long max_load_move, | |
3253 | struct sched_domain *sd, enum cpu_idle_type idle, | |
3254 | int *all_pinned) | |
3255 | { | |
3256 | const struct sched_class *class = sched_class_highest; | |
3257 | unsigned long total_load_moved = 0; | |
3258 | int this_best_prio = this_rq->curr->prio; | |
3259 | ||
3260 | do { | |
3261 | total_load_moved += | |
3262 | class->load_balance(this_rq, this_cpu, busiest, | |
3263 | max_load_move - total_load_moved, | |
3264 | sd, idle, all_pinned, &this_best_prio); | |
3265 | class = class->next; | |
3266 | ||
3267 | #ifdef CONFIG_PREEMPT | |
3268 | /* | |
3269 | * NEWIDLE balancing is a source of latency, so preemptible | |
3270 | * kernels will stop after the first task is pulled to minimize | |
3271 | * the critical section. | |
3272 | */ | |
3273 | if (idle == CPU_NEWLY_IDLE && this_rq->nr_running) | |
3274 | break; | |
3275 | #endif | |
3276 | } while (class && max_load_move > total_load_moved); | |
3277 | ||
3278 | return total_load_moved > 0; | |
3279 | } | |
3280 | ||
3281 | static int | |
3282 | iter_move_one_task(struct rq *this_rq, int this_cpu, struct rq *busiest, | |
3283 | struct sched_domain *sd, enum cpu_idle_type idle, | |
3284 | struct rq_iterator *iterator) | |
3285 | { | |
3286 | struct task_struct *p = iterator->start(iterator->arg); | |
3287 | int pinned = 0; | |
3288 | ||
3289 | while (p) { | |
3290 | if (can_migrate_task(p, busiest, this_cpu, sd, idle, &pinned)) { | |
3291 | pull_task(busiest, p, this_rq, this_cpu); | |
3292 | /* | |
3293 | * Right now, this is only the second place pull_task() | |
3294 | * is called, so we can safely collect pull_task() | |
3295 | * stats here rather than inside pull_task(). | |
3296 | */ | |
3297 | schedstat_inc(sd, lb_gained[idle]); | |
3298 | ||
3299 | return 1; | |
3300 | } | |
3301 | p = iterator->next(iterator->arg); | |
3302 | } | |
3303 | ||
3304 | return 0; | |
3305 | } | |
3306 | ||
3307 | /* | |
3308 | * move_one_task tries to move exactly one task from busiest to this_rq, as | |
3309 | * part of active balancing operations within "domain". | |
3310 | * Returns 1 if successful and 0 otherwise. | |
3311 | * | |
3312 | * Called with both runqueues locked. | |
3313 | */ | |
3314 | static int move_one_task(struct rq *this_rq, int this_cpu, struct rq *busiest, | |
3315 | struct sched_domain *sd, enum cpu_idle_type idle) | |
3316 | { | |
3317 | const struct sched_class *class; | |
3318 | ||
3319 | for (class = sched_class_highest; class; class = class->next) | |
3320 | if (class->move_one_task(this_rq, this_cpu, busiest, sd, idle)) | |
3321 | return 1; | |
3322 | ||
3323 | return 0; | |
3324 | } | |
3325 | /********** Helpers for find_busiest_group ************************/ | |
3326 | /* | |
3327 | * sd_lb_stats - Structure to store the statistics of a sched_domain | |
3328 | * during load balancing. | |
3329 | */ | |
3330 | struct sd_lb_stats { | |
3331 | struct sched_group *busiest; /* Busiest group in this sd */ | |
3332 | struct sched_group *this; /* Local group in this sd */ | |
3333 | unsigned long total_load; /* Total load of all groups in sd */ | |
3334 | unsigned long total_pwr; /* Total power of all groups in sd */ | |
3335 | unsigned long avg_load; /* Average load across all groups in sd */ | |
3336 | ||
3337 | /** Statistics of this group */ | |
3338 | unsigned long this_load; | |
3339 | unsigned long this_load_per_task; | |
3340 | unsigned long this_nr_running; | |
3341 | ||
3342 | /* Statistics of the busiest group */ | |
3343 | unsigned long max_load; | |
3344 | unsigned long busiest_load_per_task; | |
3345 | unsigned long busiest_nr_running; | |
3346 | ||
3347 | int group_imb; /* Is there imbalance in this sd */ | |
3348 | #if defined(CONFIG_SCHED_MC) || defined(CONFIG_SCHED_SMT) | |
3349 | int power_savings_balance; /* Is powersave balance needed for this sd */ | |
3350 | struct sched_group *group_min; /* Least loaded group in sd */ | |
3351 | struct sched_group *group_leader; /* Group which relieves group_min */ | |
3352 | unsigned long min_load_per_task; /* load_per_task in group_min */ | |
3353 | unsigned long leader_nr_running; /* Nr running of group_leader */ | |
3354 | unsigned long min_nr_running; /* Nr running of group_min */ | |
3355 | #endif | |
3356 | }; | |
3357 | ||
3358 | /* | |
3359 | * sg_lb_stats - stats of a sched_group required for load_balancing | |
3360 | */ | |
3361 | struct sg_lb_stats { | |
3362 | unsigned long avg_load; /*Avg load across the CPUs of the group */ | |
3363 | unsigned long group_load; /* Total load over the CPUs of the group */ | |
3364 | unsigned long sum_nr_running; /* Nr tasks running in the group */ | |
3365 | unsigned long sum_weighted_load; /* Weighted load of group's tasks */ | |
3366 | unsigned long group_capacity; | |
3367 | int group_imb; /* Is there an imbalance in the group ? */ | |
3368 | }; | |
3369 | ||
3370 | /** | |
3371 | * group_first_cpu - Returns the first cpu in the cpumask of a sched_group. | |
3372 | * @group: The group whose first cpu is to be returned. | |
3373 | */ | |
3374 | static inline unsigned int group_first_cpu(struct sched_group *group) | |
3375 | { | |
3376 | return cpumask_first(sched_group_cpus(group)); | |
3377 | } | |
3378 | ||
3379 | /** | |
3380 | * get_sd_load_idx - Obtain the load index for a given sched domain. | |
3381 | * @sd: The sched_domain whose load_idx is to be obtained. | |
3382 | * @idle: The Idle status of the CPU for whose sd load_icx is obtained. | |
3383 | */ | |
3384 | static inline int get_sd_load_idx(struct sched_domain *sd, | |
3385 | enum cpu_idle_type idle) | |
3386 | { | |
3387 | int load_idx; | |
3388 | ||
3389 | switch (idle) { | |
3390 | case CPU_NOT_IDLE: | |
3391 | load_idx = sd->busy_idx; | |
3392 | break; | |
3393 | ||
3394 | case CPU_NEWLY_IDLE: | |
3395 | load_idx = sd->newidle_idx; | |
3396 | break; | |
3397 | default: | |
3398 | load_idx = sd->idle_idx; | |
3399 | break; | |
3400 | } | |
3401 | ||
3402 | return load_idx; | |
3403 | } | |
3404 | ||
3405 | ||
3406 | #if defined(CONFIG_SCHED_MC) || defined(CONFIG_SCHED_SMT) | |
3407 | /** | |
3408 | * init_sd_power_savings_stats - Initialize power savings statistics for | |
3409 | * the given sched_domain, during load balancing. | |
3410 | * | |
3411 | * @sd: Sched domain whose power-savings statistics are to be initialized. | |
3412 | * @sds: Variable containing the statistics for sd. | |
3413 | * @idle: Idle status of the CPU at which we're performing load-balancing. | |
3414 | */ | |
3415 | static inline void init_sd_power_savings_stats(struct sched_domain *sd, | |
3416 | struct sd_lb_stats *sds, enum cpu_idle_type idle) | |
3417 | { | |
3418 | /* | |
3419 | * Busy processors will not participate in power savings | |
3420 | * balance. | |
3421 | */ | |
3422 | if (idle == CPU_NOT_IDLE || !(sd->flags & SD_POWERSAVINGS_BALANCE)) | |
3423 | sds->power_savings_balance = 0; | |
3424 | else { | |
3425 | sds->power_savings_balance = 1; | |
3426 | sds->min_nr_running = ULONG_MAX; | |
3427 | sds->leader_nr_running = 0; | |
3428 | } | |
3429 | } | |
3430 | ||
3431 | /** | |
3432 | * update_sd_power_savings_stats - Update the power saving stats for a | |
3433 | * sched_domain while performing load balancing. | |
3434 | * | |
3435 | * @group: sched_group belonging to the sched_domain under consideration. | |
3436 | * @sds: Variable containing the statistics of the sched_domain | |
3437 | * @local_group: Does group contain the CPU for which we're performing | |
3438 | * load balancing ? | |
3439 | * @sgs: Variable containing the statistics of the group. | |
3440 | */ | |
3441 | static inline void update_sd_power_savings_stats(struct sched_group *group, | |
3442 | struct sd_lb_stats *sds, int local_group, struct sg_lb_stats *sgs) | |
3443 | { | |
3444 | ||
3445 | if (!sds->power_savings_balance) | |
3446 | return; | |
3447 | ||
3448 | /* | |
3449 | * If the local group is idle or completely loaded | |
3450 | * no need to do power savings balance at this domain | |
3451 | */ | |
3452 | if (local_group && (sds->this_nr_running >= sgs->group_capacity || | |
3453 | !sds->this_nr_running)) | |
3454 | sds->power_savings_balance = 0; | |
3455 | ||
3456 | /* | |
3457 | * If a group is already running at full capacity or idle, | |
3458 | * don't include that group in power savings calculations | |
3459 | */ | |
3460 | if (!sds->power_savings_balance || | |
3461 | sgs->sum_nr_running >= sgs->group_capacity || | |
3462 | !sgs->sum_nr_running) | |
3463 | return; | |
3464 | ||
3465 | /* | |
3466 | * Calculate the group which has the least non-idle load. | |
3467 | * This is the group from where we need to pick up the load | |
3468 | * for saving power | |
3469 | */ | |
3470 | if ((sgs->sum_nr_running < sds->min_nr_running) || | |
3471 | (sgs->sum_nr_running == sds->min_nr_running && | |
3472 | group_first_cpu(group) > group_first_cpu(sds->group_min))) { | |
3473 | sds->group_min = group; | |
3474 | sds->min_nr_running = sgs->sum_nr_running; | |
3475 | sds->min_load_per_task = sgs->sum_weighted_load / | |
3476 | sgs->sum_nr_running; | |
3477 | } | |
3478 | ||
3479 | /* | |
3480 | * Calculate the group which is almost near its | |
3481 | * capacity but still has some space to pick up some load | |
3482 | * from other group and save more power | |
3483 | */ | |
3484 | if (sgs->sum_nr_running > sgs->group_capacity - 1) | |
3485 | return; | |
3486 | ||
3487 | if (sgs->sum_nr_running > sds->leader_nr_running || | |
3488 | (sgs->sum_nr_running == sds->leader_nr_running && | |
3489 | group_first_cpu(group) < group_first_cpu(sds->group_leader))) { | |
3490 | sds->group_leader = group; | |
3491 | sds->leader_nr_running = sgs->sum_nr_running; | |
3492 | } | |
3493 | } | |
3494 | ||
3495 | /** | |
3496 | * check_power_save_busiest_group - see if there is potential for some power-savings balance | |
3497 | * @sds: Variable containing the statistics of the sched_domain | |
3498 | * under consideration. | |
3499 | * @this_cpu: Cpu at which we're currently performing load-balancing. | |
3500 | * @imbalance: Variable to store the imbalance. | |
3501 | * | |
3502 | * Description: | |
3503 | * Check if we have potential to perform some power-savings balance. | |
3504 | * If yes, set the busiest group to be the least loaded group in the | |
3505 | * sched_domain, so that it's CPUs can be put to idle. | |
3506 | * | |
3507 | * Returns 1 if there is potential to perform power-savings balance. | |
3508 | * Else returns 0. | |
3509 | */ | |
3510 | static inline int check_power_save_busiest_group(struct sd_lb_stats *sds, | |
3511 | int this_cpu, unsigned long *imbalance) | |
3512 | { | |
3513 | if (!sds->power_savings_balance) | |
3514 | return 0; | |
3515 | ||
3516 | if (sds->this != sds->group_leader || | |
3517 | sds->group_leader == sds->group_min) | |
3518 | return 0; | |
3519 | ||
3520 | *imbalance = sds->min_load_per_task; | |
3521 | sds->busiest = sds->group_min; | |
3522 | ||
3523 | if (sched_mc_power_savings >= POWERSAVINGS_BALANCE_WAKEUP) { | |
3524 | cpu_rq(this_cpu)->rd->sched_mc_preferred_wakeup_cpu = | |
3525 | group_first_cpu(sds->group_leader); | |
3526 | } | |
3527 | ||
3528 | return 1; | |
3529 | ||
3530 | } | |
3531 | #else /* CONFIG_SCHED_MC || CONFIG_SCHED_SMT */ | |
3532 | static inline void init_sd_power_savings_stats(struct sched_domain *sd, | |
3533 | struct sd_lb_stats *sds, enum cpu_idle_type idle) | |
3534 | { | |
3535 | return; | |
3536 | } | |
3537 | ||
3538 | static inline void update_sd_power_savings_stats(struct sched_group *group, | |
3539 | struct sd_lb_stats *sds, int local_group, struct sg_lb_stats *sgs) | |
3540 | { | |
3541 | return; | |
3542 | } | |
3543 | ||
3544 | static inline int check_power_save_busiest_group(struct sd_lb_stats *sds, | |
3545 | int this_cpu, unsigned long *imbalance) | |
3546 | { | |
3547 | return 0; | |
3548 | } | |
3549 | #endif /* CONFIG_SCHED_MC || CONFIG_SCHED_SMT */ | |
3550 | ||
3551 | ||
3552 | /** | |
3553 | * update_sg_lb_stats - Update sched_group's statistics for load balancing. | |
3554 | * @group: sched_group whose statistics are to be updated. | |
3555 | * @this_cpu: Cpu for which load balance is currently performed. | |
3556 | * @idle: Idle status of this_cpu | |
3557 | * @load_idx: Load index of sched_domain of this_cpu for load calc. | |
3558 | * @sd_idle: Idle status of the sched_domain containing group. | |
3559 | * @local_group: Does group contain this_cpu. | |
3560 | * @cpus: Set of cpus considered for load balancing. | |
3561 | * @balance: Should we balance. | |
3562 | * @sgs: variable to hold the statistics for this group. | |
3563 | */ | |
3564 | static inline void update_sg_lb_stats(struct sched_group *group, int this_cpu, | |
3565 | enum cpu_idle_type idle, int load_idx, int *sd_idle, | |
3566 | int local_group, const struct cpumask *cpus, | |
3567 | int *balance, struct sg_lb_stats *sgs) | |
3568 | { | |
3569 | unsigned long load, max_cpu_load, min_cpu_load; | |
3570 | int i; | |
3571 | unsigned int balance_cpu = -1, first_idle_cpu = 0; | |
3572 | unsigned long sum_avg_load_per_task; | |
3573 | unsigned long avg_load_per_task; | |
3574 | ||
3575 | if (local_group) | |
3576 | balance_cpu = group_first_cpu(group); | |
3577 | ||
3578 | /* Tally up the load of all CPUs in the group */ | |
3579 | sum_avg_load_per_task = avg_load_per_task = 0; | |
3580 | max_cpu_load = 0; | |
3581 | min_cpu_load = ~0UL; | |
3582 | ||
3583 | for_each_cpu_and(i, sched_group_cpus(group), cpus) { | |
3584 | struct rq *rq = cpu_rq(i); | |
3585 | ||
3586 | if (*sd_idle && rq->nr_running) | |
3587 | *sd_idle = 0; | |
3588 | ||
3589 | /* Bias balancing toward cpus of our domain */ | |
3590 | if (local_group) { | |
3591 | if (idle_cpu(i) && !first_idle_cpu) { | |
3592 | first_idle_cpu = 1; | |
3593 | balance_cpu = i; | |
3594 | } | |
3595 | ||
3596 | load = target_load(i, load_idx); | |
3597 | } else { | |
3598 | load = source_load(i, load_idx); | |
3599 | if (load > max_cpu_load) | |
3600 | max_cpu_load = load; | |
3601 | if (min_cpu_load > load) | |
3602 | min_cpu_load = load; | |
3603 | } | |
3604 | ||
3605 | sgs->group_load += load; | |
3606 | sgs->sum_nr_running += rq->nr_running; | |
3607 | sgs->sum_weighted_load += weighted_cpuload(i); | |
3608 | ||
3609 | sum_avg_load_per_task += cpu_avg_load_per_task(i); | |
3610 | } | |
3611 | ||
3612 | /* | |
3613 | * First idle cpu or the first cpu(busiest) in this sched group | |
3614 | * is eligible for doing load balancing at this and above | |
3615 | * domains. In the newly idle case, we will allow all the cpu's | |
3616 | * to do the newly idle load balance. | |
3617 | */ | |
3618 | if (idle != CPU_NEWLY_IDLE && local_group && | |
3619 | balance_cpu != this_cpu && balance) { | |
3620 | *balance = 0; | |
3621 | return; | |
3622 | } | |
3623 | ||
3624 | /* Adjust by relative CPU power of the group */ | |
3625 | sgs->avg_load = sg_div_cpu_power(group, | |
3626 | sgs->group_load * SCHED_LOAD_SCALE); | |
3627 | ||
3628 | ||
3629 | /* | |
3630 | * Consider the group unbalanced when the imbalance is larger | |
3631 | * than the average weight of two tasks. | |
3632 | * | |
3633 | * APZ: with cgroup the avg task weight can vary wildly and | |
3634 | * might not be a suitable number - should we keep a | |
3635 | * normalized nr_running number somewhere that negates | |
3636 | * the hierarchy? | |
3637 | */ | |
3638 | avg_load_per_task = sg_div_cpu_power(group, | |
3639 | sum_avg_load_per_task * SCHED_LOAD_SCALE); | |
3640 | ||
3641 | if ((max_cpu_load - min_cpu_load) > 2*avg_load_per_task) | |
3642 | sgs->group_imb = 1; | |
3643 | ||
3644 | sgs->group_capacity = group->__cpu_power / SCHED_LOAD_SCALE; | |
3645 | ||
3646 | } | |
3647 | ||
3648 | /** | |
3649 | * update_sd_lb_stats - Update sched_group's statistics for load balancing. | |
3650 | * @sd: sched_domain whose statistics are to be updated. | |
3651 | * @this_cpu: Cpu for which load balance is currently performed. | |
3652 | * @idle: Idle status of this_cpu | |
3653 | * @sd_idle: Idle status of the sched_domain containing group. | |
3654 | * @cpus: Set of cpus considered for load balancing. | |
3655 | * @balance: Should we balance. | |
3656 | * @sds: variable to hold the statistics for this sched_domain. | |
3657 | */ | |
3658 | static inline void update_sd_lb_stats(struct sched_domain *sd, int this_cpu, | |
3659 | enum cpu_idle_type idle, int *sd_idle, | |
3660 | const struct cpumask *cpus, int *balance, | |
3661 | struct sd_lb_stats *sds) | |
3662 | { | |
3663 | struct sched_group *group = sd->groups; | |
3664 | struct sg_lb_stats sgs; | |
3665 | int load_idx; | |
3666 | ||
3667 | init_sd_power_savings_stats(sd, sds, idle); | |
3668 | load_idx = get_sd_load_idx(sd, idle); | |
3669 | ||
3670 | do { | |
3671 | int local_group; | |
3672 | ||
3673 | local_group = cpumask_test_cpu(this_cpu, | |
3674 | sched_group_cpus(group)); | |
3675 | memset(&sgs, 0, sizeof(sgs)); | |
3676 | update_sg_lb_stats(group, this_cpu, idle, load_idx, sd_idle, | |
3677 | local_group, cpus, balance, &sgs); | |
3678 | ||
3679 | if (local_group && balance && !(*balance)) | |
3680 | return; | |
3681 | ||
3682 | sds->total_load += sgs.group_load; | |
3683 | sds->total_pwr += group->__cpu_power; | |
3684 | ||
3685 | if (local_group) { | |
3686 | sds->this_load = sgs.avg_load; | |
3687 | sds->this = group; | |
3688 | sds->this_nr_running = sgs.sum_nr_running; | |
3689 | sds->this_load_per_task = sgs.sum_weighted_load; | |
3690 | } else if (sgs.avg_load > sds->max_load && | |
3691 | (sgs.sum_nr_running > sgs.group_capacity || | |
3692 | sgs.group_imb)) { | |
3693 | sds->max_load = sgs.avg_load; | |
3694 | sds->busiest = group; | |
3695 | sds->busiest_nr_running = sgs.sum_nr_running; | |
3696 | sds->busiest_load_per_task = sgs.sum_weighted_load; | |
3697 | sds->group_imb = sgs.group_imb; | |
3698 | } | |
3699 | ||
3700 | update_sd_power_savings_stats(group, sds, local_group, &sgs); | |
3701 | group = group->next; | |
3702 | } while (group != sd->groups); | |
3703 | ||
3704 | } | |
3705 | ||
3706 | /** | |
3707 | * fix_small_imbalance - Calculate the minor imbalance that exists | |
3708 | * amongst the groups of a sched_domain, during | |
3709 | * load balancing. | |
3710 | * @sds: Statistics of the sched_domain whose imbalance is to be calculated. | |
3711 | * @this_cpu: The cpu at whose sched_domain we're performing load-balance. | |
3712 | * @imbalance: Variable to store the imbalance. | |
3713 | */ | |
3714 | static inline void fix_small_imbalance(struct sd_lb_stats *sds, | |
3715 | int this_cpu, unsigned long *imbalance) | |
3716 | { | |
3717 | unsigned long tmp, pwr_now = 0, pwr_move = 0; | |
3718 | unsigned int imbn = 2; | |
3719 | ||
3720 | if (sds->this_nr_running) { | |
3721 | sds->this_load_per_task /= sds->this_nr_running; | |
3722 | if (sds->busiest_load_per_task > | |
3723 | sds->this_load_per_task) | |
3724 | imbn = 1; | |
3725 | } else | |
3726 | sds->this_load_per_task = | |
3727 | cpu_avg_load_per_task(this_cpu); | |
3728 | ||
3729 | if (sds->max_load - sds->this_load + sds->busiest_load_per_task >= | |
3730 | sds->busiest_load_per_task * imbn) { | |
3731 | *imbalance = sds->busiest_load_per_task; | |
3732 | return; | |
3733 | } | |
3734 | ||
3735 | /* | |
3736 | * OK, we don't have enough imbalance to justify moving tasks, | |
3737 | * however we may be able to increase total CPU power used by | |
3738 | * moving them. | |
3739 | */ | |
3740 | ||
3741 | pwr_now += sds->busiest->__cpu_power * | |
3742 | min(sds->busiest_load_per_task, sds->max_load); | |
3743 | pwr_now += sds->this->__cpu_power * | |
3744 | min(sds->this_load_per_task, sds->this_load); | |
3745 | pwr_now /= SCHED_LOAD_SCALE; | |
3746 | ||
3747 | /* Amount of load we'd subtract */ | |
3748 | tmp = sg_div_cpu_power(sds->busiest, | |
3749 | sds->busiest_load_per_task * SCHED_LOAD_SCALE); | |
3750 | if (sds->max_load > tmp) | |
3751 | pwr_move += sds->busiest->__cpu_power * | |
3752 | min(sds->busiest_load_per_task, sds->max_load - tmp); | |
3753 | ||
3754 | /* Amount of load we'd add */ | |
3755 | if (sds->max_load * sds->busiest->__cpu_power < | |
3756 | sds->busiest_load_per_task * SCHED_LOAD_SCALE) | |
3757 | tmp = sg_div_cpu_power(sds->this, | |
3758 | sds->max_load * sds->busiest->__cpu_power); | |
3759 | else | |
3760 | tmp = sg_div_cpu_power(sds->this, | |
3761 | sds->busiest_load_per_task * SCHED_LOAD_SCALE); | |
3762 | pwr_move += sds->this->__cpu_power * | |
3763 | min(sds->this_load_per_task, sds->this_load + tmp); | |
3764 | pwr_move /= SCHED_LOAD_SCALE; | |
3765 | ||
3766 | /* Move if we gain throughput */ | |
3767 | if (pwr_move > pwr_now) | |
3768 | *imbalance = sds->busiest_load_per_task; | |
3769 | } | |
3770 | ||
3771 | /** | |
3772 | * calculate_imbalance - Calculate the amount of imbalance present within the | |
3773 | * groups of a given sched_domain during load balance. | |
3774 | * @sds: statistics of the sched_domain whose imbalance is to be calculated. | |
3775 | * @this_cpu: Cpu for which currently load balance is being performed. | |
3776 | * @imbalance: The variable to store the imbalance. | |
3777 | */ | |
3778 | static inline void calculate_imbalance(struct sd_lb_stats *sds, int this_cpu, | |
3779 | unsigned long *imbalance) | |
3780 | { | |
3781 | unsigned long max_pull; | |
3782 | /* | |
3783 | * In the presence of smp nice balancing, certain scenarios can have | |
3784 | * max load less than avg load(as we skip the groups at or below | |
3785 | * its cpu_power, while calculating max_load..) | |
3786 | */ | |
3787 | if (sds->max_load < sds->avg_load) { | |
3788 | *imbalance = 0; | |
3789 | return fix_small_imbalance(sds, this_cpu, imbalance); | |
3790 | } | |
3791 | ||
3792 | /* Don't want to pull so many tasks that a group would go idle */ | |
3793 | max_pull = min(sds->max_load - sds->avg_load, | |
3794 | sds->max_load - sds->busiest_load_per_task); | |
3795 | ||
3796 | /* How much load to actually move to equalise the imbalance */ | |
3797 | *imbalance = min(max_pull * sds->busiest->__cpu_power, | |
3798 | (sds->avg_load - sds->this_load) * sds->this->__cpu_power) | |
3799 | / SCHED_LOAD_SCALE; | |
3800 | ||
3801 | /* | |
3802 | * if *imbalance is less than the average load per runnable task | |
3803 | * there is no gaurantee that any tasks will be moved so we'll have | |
3804 | * a think about bumping its value to force at least one task to be | |
3805 | * moved | |
3806 | */ | |
3807 | if (*imbalance < sds->busiest_load_per_task) | |
3808 | return fix_small_imbalance(sds, this_cpu, imbalance); | |
3809 | ||
3810 | } | |
3811 | /******* find_busiest_group() helpers end here *********************/ | |
3812 | ||
3813 | /** | |
3814 | * find_busiest_group - Returns the busiest group within the sched_domain | |
3815 | * if there is an imbalance. If there isn't an imbalance, and | |
3816 | * the user has opted for power-savings, it returns a group whose | |
3817 | * CPUs can be put to idle by rebalancing those tasks elsewhere, if | |
3818 | * such a group exists. | |
3819 | * | |
3820 | * Also calculates the amount of weighted load which should be moved | |
3821 | * to restore balance. | |
3822 | * | |
3823 | * @sd: The sched_domain whose busiest group is to be returned. | |
3824 | * @this_cpu: The cpu for which load balancing is currently being performed. | |
3825 | * @imbalance: Variable which stores amount of weighted load which should | |
3826 | * be moved to restore balance/put a group to idle. | |
3827 | * @idle: The idle status of this_cpu. | |
3828 | * @sd_idle: The idleness of sd | |
3829 | * @cpus: The set of CPUs under consideration for load-balancing. | |
3830 | * @balance: Pointer to a variable indicating if this_cpu | |
3831 | * is the appropriate cpu to perform load balancing at this_level. | |
3832 | * | |
3833 | * Returns: - the busiest group if imbalance exists. | |
3834 | * - If no imbalance and user has opted for power-savings balance, | |
3835 | * return the least loaded group whose CPUs can be | |
3836 | * put to idle by rebalancing its tasks onto our group. | |
3837 | */ | |
3838 | static struct sched_group * | |
3839 | find_busiest_group(struct sched_domain *sd, int this_cpu, | |
3840 | unsigned long *imbalance, enum cpu_idle_type idle, | |
3841 | int *sd_idle, const struct cpumask *cpus, int *balance) | |
3842 | { | |
3843 | struct sd_lb_stats sds; | |
3844 | ||
3845 | memset(&sds, 0, sizeof(sds)); | |
3846 | ||
3847 | /* | |
3848 | * Compute the various statistics relavent for load balancing at | |
3849 | * this level. | |
3850 | */ | |
3851 | update_sd_lb_stats(sd, this_cpu, idle, sd_idle, cpus, | |
3852 | balance, &sds); | |
3853 | ||
3854 | /* Cases where imbalance does not exist from POV of this_cpu */ | |
3855 | /* 1) this_cpu is not the appropriate cpu to perform load balancing | |
3856 | * at this level. | |
3857 | * 2) There is no busy sibling group to pull from. | |
3858 | * 3) This group is the busiest group. | |
3859 | * 4) This group is more busy than the avg busieness at this | |
3860 | * sched_domain. | |
3861 | * 5) The imbalance is within the specified limit. | |
3862 | * 6) Any rebalance would lead to ping-pong | |
3863 | */ | |
3864 | if (balance && !(*balance)) | |
3865 | goto ret; | |
3866 | ||
3867 | if (!sds.busiest || sds.busiest_nr_running == 0) | |
3868 | goto out_balanced; | |
3869 | ||
3870 | if (sds.this_load >= sds.max_load) | |
3871 | goto out_balanced; | |
3872 | ||
3873 | sds.avg_load = (SCHED_LOAD_SCALE * sds.total_load) / sds.total_pwr; | |
3874 | ||
3875 | if (sds.this_load >= sds.avg_load) | |
3876 | goto out_balanced; | |
3877 | ||
3878 | if (100 * sds.max_load <= sd->imbalance_pct * sds.this_load) | |
3879 | goto out_balanced; | |
3880 | ||
3881 | sds.busiest_load_per_task /= sds.busiest_nr_running; | |
3882 | if (sds.group_imb) | |
3883 | sds.busiest_load_per_task = | |
3884 | min(sds.busiest_load_per_task, sds.avg_load); | |
3885 | ||
3886 | /* | |
3887 | * We're trying to get all the cpus to the average_load, so we don't | |
3888 | * want to push ourselves above the average load, nor do we wish to | |
3889 | * reduce the max loaded cpu below the average load, as either of these | |
3890 | * actions would just result in more rebalancing later, and ping-pong | |
3891 | * tasks around. Thus we look for the minimum possible imbalance. | |
3892 | * Negative imbalances (*we* are more loaded than anyone else) will | |
3893 | * be counted as no imbalance for these purposes -- we can't fix that | |
3894 | * by pulling tasks to us. Be careful of negative numbers as they'll | |
3895 | * appear as very large values with unsigned longs. | |
3896 | */ | |
3897 | if (sds.max_load <= sds.busiest_load_per_task) | |
3898 | goto out_balanced; | |
3899 | ||
3900 | /* Looks like there is an imbalance. Compute it */ | |
3901 | calculate_imbalance(&sds, this_cpu, imbalance); | |
3902 | return sds.busiest; | |
3903 | ||
3904 | out_balanced: | |
3905 | /* | |
3906 | * There is no obvious imbalance. But check if we can do some balancing | |
3907 | * to save power. | |
3908 | */ | |
3909 | if (check_power_save_busiest_group(&sds, this_cpu, imbalance)) | |
3910 | return sds.busiest; | |
3911 | ret: | |
3912 | *imbalance = 0; | |
3913 | return NULL; | |
3914 | } | |
3915 | ||
3916 | /* | |
3917 | * find_busiest_queue - find the busiest runqueue among the cpus in group. | |
3918 | */ | |
3919 | static struct rq * | |
3920 | find_busiest_queue(struct sched_group *group, enum cpu_idle_type idle, | |
3921 | unsigned long imbalance, const struct cpumask *cpus) | |
3922 | { | |
3923 | struct rq *busiest = NULL, *rq; | |
3924 | unsigned long max_load = 0; | |
3925 | int i; | |
3926 | ||
3927 | for_each_cpu(i, sched_group_cpus(group)) { | |
3928 | unsigned long wl; | |
3929 | ||
3930 | if (!cpumask_test_cpu(i, cpus)) | |
3931 | continue; | |
3932 | ||
3933 | rq = cpu_rq(i); | |
3934 | wl = weighted_cpuload(i); | |
3935 | ||
3936 | if (rq->nr_running == 1 && wl > imbalance) | |
3937 | continue; | |
3938 | ||
3939 | if (wl > max_load) { | |
3940 | max_load = wl; | |
3941 | busiest = rq; | |
3942 | } | |
3943 | } | |
3944 | ||
3945 | return busiest; | |
3946 | } | |
3947 | ||
3948 | /* | |
3949 | * Max backoff if we encounter pinned tasks. Pretty arbitrary value, but | |
3950 | * so long as it is large enough. | |
3951 | */ | |
3952 | #define MAX_PINNED_INTERVAL 512 | |
3953 | ||
3954 | /* Working cpumask for load_balance and load_balance_newidle. */ | |
3955 | static DEFINE_PER_CPU(cpumask_var_t, load_balance_tmpmask); | |
3956 | ||
3957 | /* | |
3958 | * Check this_cpu to ensure it is balanced within domain. Attempt to move | |
3959 | * tasks if there is an imbalance. | |
3960 | */ | |
3961 | static int load_balance(int this_cpu, struct rq *this_rq, | |
3962 | struct sched_domain *sd, enum cpu_idle_type idle, | |
3963 | int *balance) | |
3964 | { | |
3965 | int ld_moved, all_pinned = 0, active_balance = 0, sd_idle = 0; | |
3966 | struct sched_group *group; | |
3967 | unsigned long imbalance; | |
3968 | struct rq *busiest; | |
3969 | unsigned long flags; | |
3970 | struct cpumask *cpus = __get_cpu_var(load_balance_tmpmask); | |
3971 | ||
3972 | cpumask_setall(cpus); | |
3973 | ||
3974 | /* | |
3975 | * When power savings policy is enabled for the parent domain, idle | |
3976 | * sibling can pick up load irrespective of busy siblings. In this case, | |
3977 | * let the state of idle sibling percolate up as CPU_IDLE, instead of | |
3978 | * portraying it as CPU_NOT_IDLE. | |
3979 | */ | |
3980 | if (idle != CPU_NOT_IDLE && sd->flags & SD_SHARE_CPUPOWER && | |
3981 | !test_sd_parent(sd, SD_POWERSAVINGS_BALANCE)) | |
3982 | sd_idle = 1; | |
3983 | ||
3984 | schedstat_inc(sd, lb_count[idle]); | |
3985 | ||
3986 | redo: | |
3987 | update_shares(sd); | |
3988 | group = find_busiest_group(sd, this_cpu, &imbalance, idle, &sd_idle, | |
3989 | cpus, balance); | |
3990 | ||
3991 | if (*balance == 0) | |
3992 | goto out_balanced; | |
3993 | ||
3994 | if (!group) { | |
3995 | schedstat_inc(sd, lb_nobusyg[idle]); | |
3996 | goto out_balanced; | |
3997 | } | |
3998 | ||
3999 | busiest = find_busiest_queue(group, idle, imbalance, cpus); | |
4000 | if (!busiest) { | |
4001 | schedstat_inc(sd, lb_nobusyq[idle]); | |
4002 | goto out_balanced; | |
4003 | } | |
4004 | ||
4005 | BUG_ON(busiest == this_rq); | |
4006 | ||
4007 | schedstat_add(sd, lb_imbalance[idle], imbalance); | |
4008 | ||
4009 | ld_moved = 0; | |
4010 | if (busiest->nr_running > 1) { | |
4011 | /* | |
4012 | * Attempt to move tasks. If find_busiest_group has found | |
4013 | * an imbalance but busiest->nr_running <= 1, the group is | |
4014 | * still unbalanced. ld_moved simply stays zero, so it is | |
4015 | * correctly treated as an imbalance. | |
4016 | */ | |
4017 | local_irq_save(flags); | |
4018 | double_rq_lock(this_rq, busiest); | |
4019 | ld_moved = move_tasks(this_rq, this_cpu, busiest, | |
4020 | imbalance, sd, idle, &all_pinned); | |
4021 | double_rq_unlock(this_rq, busiest); | |
4022 | local_irq_restore(flags); | |
4023 | ||
4024 | /* | |
4025 | * some other cpu did the load balance for us. | |
4026 | */ | |
4027 | if (ld_moved && this_cpu != smp_processor_id()) | |
4028 | resched_cpu(this_cpu); | |
4029 | ||
4030 | /* All tasks on this runqueue were pinned by CPU affinity */ | |
4031 | if (unlikely(all_pinned)) { | |
4032 | cpumask_clear_cpu(cpu_of(busiest), cpus); | |
4033 | if (!cpumask_empty(cpus)) | |
4034 | goto redo; | |
4035 | goto out_balanced; | |
4036 | } | |
4037 | } | |
4038 | ||
4039 | if (!ld_moved) { | |
4040 | schedstat_inc(sd, lb_failed[idle]); | |
4041 | sd->nr_balance_failed++; | |
4042 | ||
4043 | if (unlikely(sd->nr_balance_failed > sd->cache_nice_tries+2)) { | |
4044 | ||
4045 | spin_lock_irqsave(&busiest->lock, flags); | |
4046 | ||
4047 | /* don't kick the migration_thread, if the curr | |
4048 | * task on busiest cpu can't be moved to this_cpu | |
4049 | */ | |
4050 | if (!cpumask_test_cpu(this_cpu, | |
4051 | &busiest->curr->cpus_allowed)) { | |
4052 | spin_unlock_irqrestore(&busiest->lock, flags); | |
4053 | all_pinned = 1; | |
4054 | goto out_one_pinned; | |
4055 | } | |
4056 | ||
4057 | if (!busiest->active_balance) { | |
4058 | busiest->active_balance = 1; | |
4059 | busiest->push_cpu = this_cpu; | |
4060 | active_balance = 1; | |
4061 | } | |
4062 | spin_unlock_irqrestore(&busiest->lock, flags); | |
4063 | if (active_balance) | |
4064 | wake_up_process(busiest->migration_thread); | |
4065 | ||
4066 | /* | |
4067 | * We've kicked active balancing, reset the failure | |
4068 | * counter. | |
4069 | */ | |
4070 | sd->nr_balance_failed = sd->cache_nice_tries+1; | |
4071 | } | |
4072 | } else | |
4073 | sd->nr_balance_failed = 0; | |
4074 | ||
4075 | if (likely(!active_balance)) { | |
4076 | /* We were unbalanced, so reset the balancing interval */ | |
4077 | sd->balance_interval = sd->min_interval; | |
4078 | } else { | |
4079 | /* | |
4080 | * If we've begun active balancing, start to back off. This | |
4081 | * case may not be covered by the all_pinned logic if there | |
4082 | * is only 1 task on the busy runqueue (because we don't call | |
4083 | * move_tasks). | |
4084 | */ | |
4085 | if (sd->balance_interval < sd->max_interval) | |
4086 | sd->balance_interval *= 2; | |
4087 | } | |
4088 | ||
4089 | if (!ld_moved && !sd_idle && sd->flags & SD_SHARE_CPUPOWER && | |
4090 | !test_sd_parent(sd, SD_POWERSAVINGS_BALANCE)) | |
4091 | ld_moved = -1; | |
4092 | ||
4093 | goto out; | |
4094 | ||
4095 | out_balanced: | |
4096 | schedstat_inc(sd, lb_balanced[idle]); | |
4097 | ||
4098 | sd->nr_balance_failed = 0; | |
4099 | ||
4100 | out_one_pinned: | |
4101 | /* tune up the balancing interval */ | |
4102 | if ((all_pinned && sd->balance_interval < MAX_PINNED_INTERVAL) || | |
4103 | (sd->balance_interval < sd->max_interval)) | |
4104 | sd->balance_interval *= 2; | |
4105 | ||
4106 | if (!sd_idle && sd->flags & SD_SHARE_CPUPOWER && | |
4107 | !test_sd_parent(sd, SD_POWERSAVINGS_BALANCE)) | |
4108 | ld_moved = -1; | |
4109 | else | |
4110 | ld_moved = 0; | |
4111 | out: | |
4112 | if (ld_moved) | |
4113 | update_shares(sd); | |
4114 | return ld_moved; | |
4115 | } | |
4116 | ||
4117 | /* | |
4118 | * Check this_cpu to ensure it is balanced within domain. Attempt to move | |
4119 | * tasks if there is an imbalance. | |
4120 | * | |
4121 | * Called from schedule when this_rq is about to become idle (CPU_NEWLY_IDLE). | |
4122 | * this_rq is locked. | |
4123 | */ | |
4124 | static int | |
4125 | load_balance_newidle(int this_cpu, struct rq *this_rq, struct sched_domain *sd) | |
4126 | { | |
4127 | struct sched_group *group; | |
4128 | struct rq *busiest = NULL; | |
4129 | unsigned long imbalance; | |
4130 | int ld_moved = 0; | |
4131 | int sd_idle = 0; | |
4132 | int all_pinned = 0; | |
4133 | struct cpumask *cpus = __get_cpu_var(load_balance_tmpmask); | |
4134 | ||
4135 | cpumask_setall(cpus); | |
4136 | ||
4137 | /* | |
4138 | * When power savings policy is enabled for the parent domain, idle | |
4139 | * sibling can pick up load irrespective of busy siblings. In this case, | |
4140 | * let the state of idle sibling percolate up as IDLE, instead of | |
4141 | * portraying it as CPU_NOT_IDLE. | |
4142 | */ | |
4143 | if (sd->flags & SD_SHARE_CPUPOWER && | |
4144 | !test_sd_parent(sd, SD_POWERSAVINGS_BALANCE)) | |
4145 | sd_idle = 1; | |
4146 | ||
4147 | schedstat_inc(sd, lb_count[CPU_NEWLY_IDLE]); | |
4148 | redo: | |
4149 | update_shares_locked(this_rq, sd); | |
4150 | group = find_busiest_group(sd, this_cpu, &imbalance, CPU_NEWLY_IDLE, | |
4151 | &sd_idle, cpus, NULL); | |
4152 | if (!group) { | |
4153 | schedstat_inc(sd, lb_nobusyg[CPU_NEWLY_IDLE]); | |
4154 | goto out_balanced; | |
4155 | } | |
4156 | ||
4157 | busiest = find_busiest_queue(group, CPU_NEWLY_IDLE, imbalance, cpus); | |
4158 | if (!busiest) { | |
4159 | schedstat_inc(sd, lb_nobusyq[CPU_NEWLY_IDLE]); | |
4160 | goto out_balanced; | |
4161 | } | |
4162 | ||
4163 | BUG_ON(busiest == this_rq); | |
4164 | ||
4165 | schedstat_add(sd, lb_imbalance[CPU_NEWLY_IDLE], imbalance); | |
4166 | ||
4167 | ld_moved = 0; | |
4168 | if (busiest->nr_running > 1) { | |
4169 | /* Attempt to move tasks */ | |
4170 | double_lock_balance(this_rq, busiest); | |
4171 | /* this_rq->clock is already updated */ | |
4172 | update_rq_clock(busiest); | |
4173 | ld_moved = move_tasks(this_rq, this_cpu, busiest, | |
4174 | imbalance, sd, CPU_NEWLY_IDLE, | |
4175 | &all_pinned); | |
4176 | double_unlock_balance(this_rq, busiest); | |
4177 | ||
4178 | if (unlikely(all_pinned)) { | |
4179 | cpumask_clear_cpu(cpu_of(busiest), cpus); | |
4180 | if (!cpumask_empty(cpus)) | |
4181 | goto redo; | |
4182 | } | |
4183 | } | |
4184 | ||
4185 | if (!ld_moved) { | |
4186 | int active_balance = 0; | |
4187 | ||
4188 | schedstat_inc(sd, lb_failed[CPU_NEWLY_IDLE]); | |
4189 | if (!sd_idle && sd->flags & SD_SHARE_CPUPOWER && | |
4190 | !test_sd_parent(sd, SD_POWERSAVINGS_BALANCE)) | |
4191 | return -1; | |
4192 | ||
4193 | if (sched_mc_power_savings < POWERSAVINGS_BALANCE_WAKEUP) | |
4194 | return -1; | |
4195 | ||
4196 | if (sd->nr_balance_failed++ < 2) | |
4197 | return -1; | |
4198 | ||
4199 | /* | |
4200 | * The only task running in a non-idle cpu can be moved to this | |
4201 | * cpu in an attempt to completely freeup the other CPU | |
4202 | * package. The same method used to move task in load_balance() | |
4203 | * have been extended for load_balance_newidle() to speedup | |
4204 | * consolidation at sched_mc=POWERSAVINGS_BALANCE_WAKEUP (2) | |
4205 | * | |
4206 | * The package power saving logic comes from | |
4207 | * find_busiest_group(). If there are no imbalance, then | |
4208 | * f_b_g() will return NULL. However when sched_mc={1,2} then | |
4209 | * f_b_g() will select a group from which a running task may be | |
4210 | * pulled to this cpu in order to make the other package idle. | |
4211 | * If there is no opportunity to make a package idle and if | |
4212 | * there are no imbalance, then f_b_g() will return NULL and no | |
4213 | * action will be taken in load_balance_newidle(). | |
4214 | * | |
4215 | * Under normal task pull operation due to imbalance, there | |
4216 | * will be more than one task in the source run queue and | |
4217 | * move_tasks() will succeed. ld_moved will be true and this | |
4218 | * active balance code will not be triggered. | |
4219 | */ | |
4220 | ||
4221 | /* Lock busiest in correct order while this_rq is held */ | |
4222 | double_lock_balance(this_rq, busiest); | |
4223 | ||
4224 | /* | |
4225 | * don't kick the migration_thread, if the curr | |
4226 | * task on busiest cpu can't be moved to this_cpu | |
4227 | */ | |
4228 | if (!cpumask_test_cpu(this_cpu, &busiest->curr->cpus_allowed)) { | |
4229 | double_unlock_balance(this_rq, busiest); | |
4230 | all_pinned = 1; | |
4231 | return ld_moved; | |
4232 | } | |
4233 | ||
4234 | if (!busiest->active_balance) { | |
4235 | busiest->active_balance = 1; | |
4236 | busiest->push_cpu = this_cpu; | |
4237 | active_balance = 1; | |
4238 | } | |
4239 | ||
4240 | double_unlock_balance(this_rq, busiest); | |
4241 | /* | |
4242 | * Should not call ttwu while holding a rq->lock | |
4243 | */ | |
4244 | spin_unlock(&this_rq->lock); | |
4245 | if (active_balance) | |
4246 | wake_up_process(busiest->migration_thread); | |
4247 | spin_lock(&this_rq->lock); | |
4248 | ||
4249 | } else | |
4250 | sd->nr_balance_failed = 0; | |
4251 | ||
4252 | update_shares_locked(this_rq, sd); | |
4253 | return ld_moved; | |
4254 | ||
4255 | out_balanced: | |
4256 | schedstat_inc(sd, lb_balanced[CPU_NEWLY_IDLE]); | |
4257 | if (!sd_idle && sd->flags & SD_SHARE_CPUPOWER && | |
4258 | !test_sd_parent(sd, SD_POWERSAVINGS_BALANCE)) | |
4259 | return -1; | |
4260 | sd->nr_balance_failed = 0; | |
4261 | ||
4262 | return 0; | |
4263 | } | |
4264 | ||
4265 | /* | |
4266 | * idle_balance is called by schedule() if this_cpu is about to become | |
4267 | * idle. Attempts to pull tasks from other CPUs. | |
4268 | */ | |
4269 | static void idle_balance(int this_cpu, struct rq *this_rq) | |
4270 | { | |
4271 | struct sched_domain *sd; | |
4272 | int pulled_task = 0; | |
4273 | unsigned long next_balance = jiffies + HZ; | |
4274 | ||
4275 | for_each_domain(this_cpu, sd) { | |
4276 | unsigned long interval; | |
4277 | ||
4278 | if (!(sd->flags & SD_LOAD_BALANCE)) | |
4279 | continue; | |
4280 | ||
4281 | if (sd->flags & SD_BALANCE_NEWIDLE) | |
4282 | /* If we've pulled tasks over stop searching: */ | |
4283 | pulled_task = load_balance_newidle(this_cpu, this_rq, | |
4284 | sd); | |
4285 | ||
4286 | interval = msecs_to_jiffies(sd->balance_interval); | |
4287 | if (time_after(next_balance, sd->last_balance + interval)) | |
4288 | next_balance = sd->last_balance + interval; | |
4289 | if (pulled_task) | |
4290 | break; | |
4291 | } | |
4292 | if (pulled_task || time_after(jiffies, this_rq->next_balance)) { | |
4293 | /* | |
4294 | * We are going idle. next_balance may be set based on | |
4295 | * a busy processor. So reset next_balance. | |
4296 | */ | |
4297 | this_rq->next_balance = next_balance; | |
4298 | } | |
4299 | } | |
4300 | ||
4301 | /* | |
4302 | * active_load_balance is run by migration threads. It pushes running tasks | |
4303 | * off the busiest CPU onto idle CPUs. It requires at least 1 task to be | |
4304 | * running on each physical CPU where possible, and avoids physical / | |
4305 | * logical imbalances. | |
4306 | * | |
4307 | * Called with busiest_rq locked. | |
4308 | */ | |
4309 | static void active_load_balance(struct rq *busiest_rq, int busiest_cpu) | |
4310 | { | |
4311 | int target_cpu = busiest_rq->push_cpu; | |
4312 | struct sched_domain *sd; | |
4313 | struct rq *target_rq; | |
4314 | ||
4315 | /* Is there any task to move? */ | |
4316 | if (busiest_rq->nr_running <= 1) | |
4317 | return; | |
4318 | ||
4319 | target_rq = cpu_rq(target_cpu); | |
4320 | ||
4321 | /* | |
4322 | * This condition is "impossible", if it occurs | |
4323 | * we need to fix it. Originally reported by | |
4324 | * Bjorn Helgaas on a 128-cpu setup. | |
4325 | */ | |
4326 | BUG_ON(busiest_rq == target_rq); | |
4327 | ||
4328 | /* move a task from busiest_rq to target_rq */ | |
4329 | double_lock_balance(busiest_rq, target_rq); | |
4330 | update_rq_clock(busiest_rq); | |
4331 | update_rq_clock(target_rq); | |
4332 | ||
4333 | /* Search for an sd spanning us and the target CPU. */ | |
4334 | for_each_domain(target_cpu, sd) { | |
4335 | if ((sd->flags & SD_LOAD_BALANCE) && | |
4336 | cpumask_test_cpu(busiest_cpu, sched_domain_span(sd))) | |
4337 | break; | |
4338 | } | |
4339 | ||
4340 | if (likely(sd)) { | |
4341 | schedstat_inc(sd, alb_count); | |
4342 | ||
4343 | if (move_one_task(target_rq, target_cpu, busiest_rq, | |
4344 | sd, CPU_IDLE)) | |
4345 | schedstat_inc(sd, alb_pushed); | |
4346 | else | |
4347 | schedstat_inc(sd, alb_failed); | |
4348 | } | |
4349 | double_unlock_balance(busiest_rq, target_rq); | |
4350 | } | |
4351 | ||
4352 | #ifdef CONFIG_NO_HZ | |
4353 | static struct { | |
4354 | atomic_t load_balancer; | |
4355 | cpumask_var_t cpu_mask; | |
4356 | cpumask_var_t ilb_grp_nohz_mask; | |
4357 | } nohz ____cacheline_aligned = { | |
4358 | .load_balancer = ATOMIC_INIT(-1), | |
4359 | }; | |
4360 | ||
4361 | #if defined(CONFIG_SCHED_MC) || defined(CONFIG_SCHED_SMT) | |
4362 | /** | |
4363 | * lowest_flag_domain - Return lowest sched_domain containing flag. | |
4364 | * @cpu: The cpu whose lowest level of sched domain is to | |
4365 | * be returned. | |
4366 | * @flag: The flag to check for the lowest sched_domain | |
4367 | * for the given cpu. | |
4368 | * | |
4369 | * Returns the lowest sched_domain of a cpu which contains the given flag. | |
4370 | */ | |
4371 | static inline struct sched_domain *lowest_flag_domain(int cpu, int flag) | |
4372 | { | |
4373 | struct sched_domain *sd; | |
4374 | ||
4375 | for_each_domain(cpu, sd) | |
4376 | if (sd && (sd->flags & flag)) | |
4377 | break; | |
4378 | ||
4379 | return sd; | |
4380 | } | |
4381 | ||
4382 | /** | |
4383 | * for_each_flag_domain - Iterates over sched_domains containing the flag. | |
4384 | * @cpu: The cpu whose domains we're iterating over. | |
4385 | * @sd: variable holding the value of the power_savings_sd | |
4386 | * for cpu. | |
4387 | * @flag: The flag to filter the sched_domains to be iterated. | |
4388 | * | |
4389 | * Iterates over all the scheduler domains for a given cpu that has the 'flag' | |
4390 | * set, starting from the lowest sched_domain to the highest. | |
4391 | */ | |
4392 | #define for_each_flag_domain(cpu, sd, flag) \ | |
4393 | for (sd = lowest_flag_domain(cpu, flag); \ | |
4394 | (sd && (sd->flags & flag)); sd = sd->parent) | |
4395 | ||
4396 | /** | |
4397 | * is_semi_idle_group - Checks if the given sched_group is semi-idle. | |
4398 | * @ilb_group: group to be checked for semi-idleness | |
4399 | * | |
4400 | * Returns: 1 if the group is semi-idle. 0 otherwise. | |
4401 | * | |
4402 | * We define a sched_group to be semi idle if it has atleast one idle-CPU | |
4403 | * and atleast one non-idle CPU. This helper function checks if the given | |
4404 | * sched_group is semi-idle or not. | |
4405 | */ | |
4406 | static inline int is_semi_idle_group(struct sched_group *ilb_group) | |
4407 | { | |
4408 | cpumask_and(nohz.ilb_grp_nohz_mask, nohz.cpu_mask, | |
4409 | sched_group_cpus(ilb_group)); | |
4410 | ||
4411 | /* | |
4412 | * A sched_group is semi-idle when it has atleast one busy cpu | |
4413 | * and atleast one idle cpu. | |
4414 | */ | |
4415 | if (cpumask_empty(nohz.ilb_grp_nohz_mask)) | |
4416 | return 0; | |
4417 | ||
4418 | if (cpumask_equal(nohz.ilb_grp_nohz_mask, sched_group_cpus(ilb_group))) | |
4419 | return 0; | |
4420 | ||
4421 | return 1; | |
4422 | } | |
4423 | /** | |
4424 | * find_new_ilb - Finds the optimum idle load balancer for nomination. | |
4425 | * @cpu: The cpu which is nominating a new idle_load_balancer. | |
4426 | * | |
4427 | * Returns: Returns the id of the idle load balancer if it exists, | |
4428 | * Else, returns >= nr_cpu_ids. | |
4429 | * | |
4430 | * This algorithm picks the idle load balancer such that it belongs to a | |
4431 | * semi-idle powersavings sched_domain. The idea is to try and avoid | |
4432 | * completely idle packages/cores just for the purpose of idle load balancing | |
4433 | * when there are other idle cpu's which are better suited for that job. | |
4434 | */ | |
4435 | static int find_new_ilb(int cpu) | |
4436 | { | |
4437 | struct sched_domain *sd; | |
4438 | struct sched_group *ilb_group; | |
4439 | ||
4440 | /* | |
4441 | * Have idle load balancer selection from semi-idle packages only | |
4442 | * when power-aware load balancing is enabled | |
4443 | */ | |
4444 | if (!(sched_smt_power_savings || sched_mc_power_savings)) | |
4445 | goto out_done; | |
4446 | ||
4447 | /* | |
4448 | * Optimize for the case when we have no idle CPUs or only one | |
4449 | * idle CPU. Don't walk the sched_domain hierarchy in such cases | |
4450 | */ | |
4451 | if (cpumask_weight(nohz.cpu_mask) < 2) | |
4452 | goto out_done; | |
4453 | ||
4454 | for_each_flag_domain(cpu, sd, SD_POWERSAVINGS_BALANCE) { | |
4455 | ilb_group = sd->groups; | |
4456 | ||
4457 | do { | |
4458 | if (is_semi_idle_group(ilb_group)) | |
4459 | return cpumask_first(nohz.ilb_grp_nohz_mask); | |
4460 | ||
4461 | ilb_group = ilb_group->next; | |
4462 | ||
4463 | } while (ilb_group != sd->groups); | |
4464 | } | |
4465 | ||
4466 | out_done: | |
4467 | return cpumask_first(nohz.cpu_mask); | |
4468 | } | |
4469 | #else /* (CONFIG_SCHED_MC || CONFIG_SCHED_SMT) */ | |
4470 | static inline int find_new_ilb(int call_cpu) | |
4471 | { | |
4472 | return cpumask_first(nohz.cpu_mask); | |
4473 | } | |
4474 | #endif | |
4475 | ||
4476 | /* | |
4477 | * This routine will try to nominate the ilb (idle load balancing) | |
4478 | * owner among the cpus whose ticks are stopped. ilb owner will do the idle | |
4479 | * load balancing on behalf of all those cpus. If all the cpus in the system | |
4480 | * go into this tickless mode, then there will be no ilb owner (as there is | |
4481 | * no need for one) and all the cpus will sleep till the next wakeup event | |
4482 | * arrives... | |
4483 | * | |
4484 | * For the ilb owner, tick is not stopped. And this tick will be used | |
4485 | * for idle load balancing. ilb owner will still be part of | |
4486 | * nohz.cpu_mask.. | |
4487 | * | |
4488 | * While stopping the tick, this cpu will become the ilb owner if there | |
4489 | * is no other owner. And will be the owner till that cpu becomes busy | |
4490 | * or if all cpus in the system stop their ticks at which point | |
4491 | * there is no need for ilb owner. | |
4492 | * | |
4493 | * When the ilb owner becomes busy, it nominates another owner, during the | |
4494 | * next busy scheduler_tick() | |
4495 | */ | |
4496 | int select_nohz_load_balancer(int stop_tick) | |
4497 | { | |
4498 | int cpu = smp_processor_id(); | |
4499 | ||
4500 | if (stop_tick) { | |
4501 | cpu_rq(cpu)->in_nohz_recently = 1; | |
4502 | ||
4503 | if (!cpu_active(cpu)) { | |
4504 | if (atomic_read(&nohz.load_balancer) != cpu) | |
4505 | return 0; | |
4506 | ||
4507 | /* | |
4508 | * If we are going offline and still the leader, | |
4509 | * give up! | |
4510 | */ | |
4511 | if (atomic_cmpxchg(&nohz.load_balancer, cpu, -1) != cpu) | |
4512 | BUG(); | |
4513 | ||
4514 | return 0; | |
4515 | } | |
4516 | ||
4517 | cpumask_set_cpu(cpu, nohz.cpu_mask); | |
4518 | ||
4519 | /* time for ilb owner also to sleep */ | |
4520 | if (cpumask_weight(nohz.cpu_mask) == num_online_cpus()) { | |
4521 | if (atomic_read(&nohz.load_balancer) == cpu) | |
4522 | atomic_set(&nohz.load_balancer, -1); | |
4523 | return 0; | |
4524 | } | |
4525 | ||
4526 | if (atomic_read(&nohz.load_balancer) == -1) { | |
4527 | /* make me the ilb owner */ | |
4528 | if (atomic_cmpxchg(&nohz.load_balancer, -1, cpu) == -1) | |
4529 | return 1; | |
4530 | } else if (atomic_read(&nohz.load_balancer) == cpu) { | |
4531 | int new_ilb; | |
4532 | ||
4533 | if (!(sched_smt_power_savings || | |
4534 | sched_mc_power_savings)) | |
4535 | return 1; | |
4536 | /* | |
4537 | * Check to see if there is a more power-efficient | |
4538 | * ilb. | |
4539 | */ | |
4540 | new_ilb = find_new_ilb(cpu); | |
4541 | if (new_ilb < nr_cpu_ids && new_ilb != cpu) { | |
4542 | atomic_set(&nohz.load_balancer, -1); | |
4543 | resched_cpu(new_ilb); | |
4544 | return 0; | |
4545 | } | |
4546 | return 1; | |
4547 | } | |
4548 | } else { | |
4549 | if (!cpumask_test_cpu(cpu, nohz.cpu_mask)) | |
4550 | return 0; | |
4551 | ||
4552 | cpumask_clear_cpu(cpu, nohz.cpu_mask); | |
4553 | ||
4554 | if (atomic_read(&nohz.load_balancer) == cpu) | |
4555 | if (atomic_cmpxchg(&nohz.load_balancer, cpu, -1) != cpu) | |
4556 | BUG(); | |
4557 | } | |
4558 | return 0; | |
4559 | } | |
4560 | #endif | |
4561 | ||
4562 | static DEFINE_SPINLOCK(balancing); | |
4563 | ||
4564 | /* | |
4565 | * It checks each scheduling domain to see if it is due to be balanced, | |
4566 | * and initiates a balancing operation if so. | |
4567 | * | |
4568 | * Balancing parameters are set up in arch_init_sched_domains. | |
4569 | */ | |
4570 | static void rebalance_domains(int cpu, enum cpu_idle_type idle) | |
4571 | { | |
4572 | int balance = 1; | |
4573 | struct rq *rq = cpu_rq(cpu); | |
4574 | unsigned long interval; | |
4575 | struct sched_domain *sd; | |
4576 | /* Earliest time when we have to do rebalance again */ | |
4577 | unsigned long next_balance = jiffies + 60*HZ; | |
4578 | int update_next_balance = 0; | |
4579 | int need_serialize; | |
4580 | ||
4581 | for_each_domain(cpu, sd) { | |
4582 | if (!(sd->flags & SD_LOAD_BALANCE)) | |
4583 | continue; | |
4584 | ||
4585 | interval = sd->balance_interval; | |
4586 | if (idle != CPU_IDLE) | |
4587 | interval *= sd->busy_factor; | |
4588 | ||
4589 | /* scale ms to jiffies */ | |
4590 | interval = msecs_to_jiffies(interval); | |
4591 | if (unlikely(!interval)) | |
4592 | interval = 1; | |
4593 | if (interval > HZ*NR_CPUS/10) | |
4594 | interval = HZ*NR_CPUS/10; | |
4595 | ||
4596 | need_serialize = sd->flags & SD_SERIALIZE; | |
4597 | ||
4598 | if (need_serialize) { | |
4599 | if (!spin_trylock(&balancing)) | |
4600 | goto out; | |
4601 | } | |
4602 | ||
4603 | if (time_after_eq(jiffies, sd->last_balance + interval)) { | |
4604 | if (load_balance(cpu, rq, sd, idle, &balance)) { | |
4605 | /* | |
4606 | * We've pulled tasks over so either we're no | |
4607 | * longer idle, or one of our SMT siblings is | |
4608 | * not idle. | |
4609 | */ | |
4610 | idle = CPU_NOT_IDLE; | |
4611 | } | |
4612 | sd->last_balance = jiffies; | |
4613 | } | |
4614 | if (need_serialize) | |
4615 | spin_unlock(&balancing); | |
4616 | out: | |
4617 | if (time_after(next_balance, sd->last_balance + interval)) { | |
4618 | next_balance = sd->last_balance + interval; | |
4619 | update_next_balance = 1; | |
4620 | } | |
4621 | ||
4622 | /* | |
4623 | * Stop the load balance at this level. There is another | |
4624 | * CPU in our sched group which is doing load balancing more | |
4625 | * actively. | |
4626 | */ | |
4627 | if (!balance) | |
4628 | break; | |
4629 | } | |
4630 | ||
4631 | /* | |
4632 | * next_balance will be updated only when there is a need. | |
4633 | * When the cpu is attached to null domain for ex, it will not be | |
4634 | * updated. | |
4635 | */ | |
4636 | if (likely(update_next_balance)) | |
4637 | rq->next_balance = next_balance; | |
4638 | } | |
4639 | ||
4640 | /* | |
4641 | * run_rebalance_domains is triggered when needed from the scheduler tick. | |
4642 | * In CONFIG_NO_HZ case, the idle load balance owner will do the | |
4643 | * rebalancing for all the cpus for whom scheduler ticks are stopped. | |
4644 | */ | |
4645 | static void run_rebalance_domains(struct softirq_action *h) | |
4646 | { | |
4647 | int this_cpu = smp_processor_id(); | |
4648 | struct rq *this_rq = cpu_rq(this_cpu); | |
4649 | enum cpu_idle_type idle = this_rq->idle_at_tick ? | |
4650 | CPU_IDLE : CPU_NOT_IDLE; | |
4651 | ||
4652 | rebalance_domains(this_cpu, idle); | |
4653 | ||
4654 | #ifdef CONFIG_NO_HZ | |
4655 | /* | |
4656 | * If this cpu is the owner for idle load balancing, then do the | |
4657 | * balancing on behalf of the other idle cpus whose ticks are | |
4658 | * stopped. | |
4659 | */ | |
4660 | if (this_rq->idle_at_tick && | |
4661 | atomic_read(&nohz.load_balancer) == this_cpu) { | |
4662 | struct rq *rq; | |
4663 | int balance_cpu; | |
4664 | ||
4665 | for_each_cpu(balance_cpu, nohz.cpu_mask) { | |
4666 | if (balance_cpu == this_cpu) | |
4667 | continue; | |
4668 | ||
4669 | /* | |
4670 | * If this cpu gets work to do, stop the load balancing | |
4671 | * work being done for other cpus. Next load | |
4672 | * balancing owner will pick it up. | |
4673 | */ | |
4674 | if (need_resched()) | |
4675 | break; | |
4676 | ||
4677 | rebalance_domains(balance_cpu, CPU_IDLE); | |
4678 | ||
4679 | rq = cpu_rq(balance_cpu); | |
4680 | if (time_after(this_rq->next_balance, rq->next_balance)) | |
4681 | this_rq->next_balance = rq->next_balance; | |
4682 | } | |
4683 | } | |
4684 | #endif | |
4685 | } | |
4686 | ||
4687 | static inline int on_null_domain(int cpu) | |
4688 | { | |
4689 | return !rcu_dereference(cpu_rq(cpu)->sd); | |
4690 | } | |
4691 | ||
4692 | /* | |
4693 | * Trigger the SCHED_SOFTIRQ if it is time to do periodic load balancing. | |
4694 | * | |
4695 | * In case of CONFIG_NO_HZ, this is the place where we nominate a new | |
4696 | * idle load balancing owner or decide to stop the periodic load balancing, | |
4697 | * if the whole system is idle. | |
4698 | */ | |
4699 | static inline void trigger_load_balance(struct rq *rq, int cpu) | |
4700 | { | |
4701 | #ifdef CONFIG_NO_HZ | |
4702 | /* | |
4703 | * If we were in the nohz mode recently and busy at the current | |
4704 | * scheduler tick, then check if we need to nominate new idle | |
4705 | * load balancer. | |
4706 | */ | |
4707 | if (rq->in_nohz_recently && !rq->idle_at_tick) { | |
4708 | rq->in_nohz_recently = 0; | |
4709 | ||
4710 | if (atomic_read(&nohz.load_balancer) == cpu) { | |
4711 | cpumask_clear_cpu(cpu, nohz.cpu_mask); | |
4712 | atomic_set(&nohz.load_balancer, -1); | |
4713 | } | |
4714 | ||
4715 | if (atomic_read(&nohz.load_balancer) == -1) { | |
4716 | int ilb = find_new_ilb(cpu); | |
4717 | ||
4718 | if (ilb < nr_cpu_ids) | |
4719 | resched_cpu(ilb); | |
4720 | } | |
4721 | } | |
4722 | ||
4723 | /* | |
4724 | * If this cpu is idle and doing idle load balancing for all the | |
4725 | * cpus with ticks stopped, is it time for that to stop? | |
4726 | */ | |
4727 | if (rq->idle_at_tick && atomic_read(&nohz.load_balancer) == cpu && | |
4728 | cpumask_weight(nohz.cpu_mask) == num_online_cpus()) { | |
4729 | resched_cpu(cpu); | |
4730 | return; | |
4731 | } | |
4732 | ||
4733 | /* | |
4734 | * If this cpu is idle and the idle load balancing is done by | |
4735 | * someone else, then no need raise the SCHED_SOFTIRQ | |
4736 | */ | |
4737 | if (rq->idle_at_tick && atomic_read(&nohz.load_balancer) != cpu && | |
4738 | cpumask_test_cpu(cpu, nohz.cpu_mask)) | |
4739 | return; | |
4740 | #endif | |
4741 | /* Don't need to rebalance while attached to NULL domain */ | |
4742 | if (time_after_eq(jiffies, rq->next_balance) && | |
4743 | likely(!on_null_domain(cpu))) | |
4744 | raise_softirq(SCHED_SOFTIRQ); | |
4745 | } | |
4746 | ||
4747 | #else /* CONFIG_SMP */ | |
4748 | ||
4749 | /* | |
4750 | * on UP we do not need to balance between CPUs: | |
4751 | */ | |
4752 | static inline void idle_balance(int cpu, struct rq *rq) | |
4753 | { | |
4754 | } | |
4755 | ||
4756 | #endif | |
4757 | ||
4758 | DEFINE_PER_CPU(struct kernel_stat, kstat); | |
4759 | ||
4760 | EXPORT_PER_CPU_SYMBOL(kstat); | |
4761 | ||
4762 | /* | |
4763 | * Return any ns on the sched_clock that have not yet been accounted in | |
4764 | * @p in case that task is currently running. | |
4765 | * | |
4766 | * Called with task_rq_lock() held on @rq. | |
4767 | */ | |
4768 | static u64 do_task_delta_exec(struct task_struct *p, struct rq *rq) | |
4769 | { | |
4770 | u64 ns = 0; | |
4771 | ||
4772 | if (task_current(rq, p)) { | |
4773 | update_rq_clock(rq); | |
4774 | ns = rq->clock - p->se.exec_start; | |
4775 | if ((s64)ns < 0) | |
4776 | ns = 0; | |
4777 | } | |
4778 | ||
4779 | return ns; | |
4780 | } | |
4781 | ||
4782 | unsigned long long task_delta_exec(struct task_struct *p) | |
4783 | { | |
4784 | unsigned long flags; | |
4785 | struct rq *rq; | |
4786 | u64 ns = 0; | |
4787 | ||
4788 | rq = task_rq_lock(p, &flags); | |
4789 | ns = do_task_delta_exec(p, rq); | |
4790 | task_rq_unlock(rq, &flags); | |
4791 | ||
4792 | return ns; | |
4793 | } | |
4794 | ||
4795 | /* | |
4796 | * Return accounted runtime for the task. | |
4797 | * In case the task is currently running, return the runtime plus current's | |
4798 | * pending runtime that have not been accounted yet. | |
4799 | */ | |
4800 | unsigned long long task_sched_runtime(struct task_struct *p) | |
4801 | { | |
4802 | unsigned long flags; | |
4803 | struct rq *rq; | |
4804 | u64 ns = 0; | |
4805 | ||
4806 | rq = task_rq_lock(p, &flags); | |
4807 | ns = p->se.sum_exec_runtime + do_task_delta_exec(p, rq); | |
4808 | task_rq_unlock(rq, &flags); | |
4809 | ||
4810 | return ns; | |
4811 | } | |
4812 | ||
4813 | /* | |
4814 | * Return sum_exec_runtime for the thread group. | |
4815 | * In case the task is currently running, return the sum plus current's | |
4816 | * pending runtime that have not been accounted yet. | |
4817 | * | |
4818 | * Note that the thread group might have other running tasks as well, | |
4819 | * so the return value not includes other pending runtime that other | |
4820 | * running tasks might have. | |
4821 | */ | |
4822 | unsigned long long thread_group_sched_runtime(struct task_struct *p) | |
4823 | { | |
4824 | struct task_cputime totals; | |
4825 | unsigned long flags; | |
4826 | struct rq *rq; | |
4827 | u64 ns; | |
4828 | ||
4829 | rq = task_rq_lock(p, &flags); | |
4830 | thread_group_cputime(p, &totals); | |
4831 | ns = totals.sum_exec_runtime + do_task_delta_exec(p, rq); | |
4832 | task_rq_unlock(rq, &flags); | |
4833 | ||
4834 | return ns; | |
4835 | } | |
4836 | ||
4837 | /* | |
4838 | * Account user cpu time to a process. | |
4839 | * @p: the process that the cpu time gets accounted to | |
4840 | * @cputime: the cpu time spent in user space since the last update | |
4841 | * @cputime_scaled: cputime scaled by cpu frequency | |
4842 | */ | |
4843 | void account_user_time(struct task_struct *p, cputime_t cputime, | |
4844 | cputime_t cputime_scaled) | |
4845 | { | |
4846 | struct cpu_usage_stat *cpustat = &kstat_this_cpu.cpustat; | |
4847 | cputime64_t tmp; | |
4848 | ||
4849 | /* Add user time to process. */ | |
4850 | p->utime = cputime_add(p->utime, cputime); | |
4851 | p->utimescaled = cputime_add(p->utimescaled, cputime_scaled); | |
4852 | account_group_user_time(p, cputime); | |
4853 | ||
4854 | /* Add user time to cpustat. */ | |
4855 | tmp = cputime_to_cputime64(cputime); | |
4856 | if (TASK_NICE(p) > 0) | |
4857 | cpustat->nice = cputime64_add(cpustat->nice, tmp); | |
4858 | else | |
4859 | cpustat->user = cputime64_add(cpustat->user, tmp); | |
4860 | ||
4861 | cpuacct_update_stats(p, CPUACCT_STAT_USER, cputime); | |
4862 | /* Account for user time used */ | |
4863 | acct_update_integrals(p); | |
4864 | } | |
4865 | ||
4866 | /* | |
4867 | * Account guest cpu time to a process. | |
4868 | * @p: the process that the cpu time gets accounted to | |
4869 | * @cputime: the cpu time spent in virtual machine since the last update | |
4870 | * @cputime_scaled: cputime scaled by cpu frequency | |
4871 | */ | |
4872 | static void account_guest_time(struct task_struct *p, cputime_t cputime, | |
4873 | cputime_t cputime_scaled) | |
4874 | { | |
4875 | cputime64_t tmp; | |
4876 | struct cpu_usage_stat *cpustat = &kstat_this_cpu.cpustat; | |
4877 | ||
4878 | tmp = cputime_to_cputime64(cputime); | |
4879 | ||
4880 | /* Add guest time to process. */ | |
4881 | p->utime = cputime_add(p->utime, cputime); | |
4882 | p->utimescaled = cputime_add(p->utimescaled, cputime_scaled); | |
4883 | account_group_user_time(p, cputime); | |
4884 | p->gtime = cputime_add(p->gtime, cputime); | |
4885 | ||
4886 | /* Add guest time to cpustat. */ | |
4887 | cpustat->user = cputime64_add(cpustat->user, tmp); | |
4888 | cpustat->guest = cputime64_add(cpustat->guest, tmp); | |
4889 | } | |
4890 | ||
4891 | /* | |
4892 | * Account system cpu time to a process. | |
4893 | * @p: the process that the cpu time gets accounted to | |
4894 | * @hardirq_offset: the offset to subtract from hardirq_count() | |
4895 | * @cputime: the cpu time spent in kernel space since the last update | |
4896 | * @cputime_scaled: cputime scaled by cpu frequency | |
4897 | */ | |
4898 | void account_system_time(struct task_struct *p, int hardirq_offset, | |
4899 | cputime_t cputime, cputime_t cputime_scaled) | |
4900 | { | |
4901 | struct cpu_usage_stat *cpustat = &kstat_this_cpu.cpustat; | |
4902 | cputime64_t tmp; | |
4903 | ||
4904 | if ((p->flags & PF_VCPU) && (irq_count() - hardirq_offset == 0)) { | |
4905 | account_guest_time(p, cputime, cputime_scaled); | |
4906 | return; | |
4907 | } | |
4908 | ||
4909 | /* Add system time to process. */ | |
4910 | p->stime = cputime_add(p->stime, cputime); | |
4911 | p->stimescaled = cputime_add(p->stimescaled, cputime_scaled); | |
4912 | account_group_system_time(p, cputime); | |
4913 | ||
4914 | /* Add system time to cpustat. */ | |
4915 | tmp = cputime_to_cputime64(cputime); | |
4916 | if (hardirq_count() - hardirq_offset) | |
4917 | cpustat->irq = cputime64_add(cpustat->irq, tmp); | |
4918 | else if (softirq_count()) | |
4919 | cpustat->softirq = cputime64_add(cpustat->softirq, tmp); | |
4920 | else | |
4921 | cpustat->system = cputime64_add(cpustat->system, tmp); | |
4922 | ||
4923 | cpuacct_update_stats(p, CPUACCT_STAT_SYSTEM, cputime); | |
4924 | ||
4925 | /* Account for system time used */ | |
4926 | acct_update_integrals(p); | |
4927 | } | |
4928 | ||
4929 | /* | |
4930 | * Account for involuntary wait time. | |
4931 | * @steal: the cpu time spent in involuntary wait | |
4932 | */ | |
4933 | void account_steal_time(cputime_t cputime) | |
4934 | { | |
4935 | struct cpu_usage_stat *cpustat = &kstat_this_cpu.cpustat; | |
4936 | cputime64_t cputime64 = cputime_to_cputime64(cputime); | |
4937 | ||
4938 | cpustat->steal = cputime64_add(cpustat->steal, cputime64); | |
4939 | } | |
4940 | ||
4941 | /* | |
4942 | * Account for idle time. | |
4943 | * @cputime: the cpu time spent in idle wait | |
4944 | */ | |
4945 | void account_idle_time(cputime_t cputime) | |
4946 | { | |
4947 | struct cpu_usage_stat *cpustat = &kstat_this_cpu.cpustat; | |
4948 | cputime64_t cputime64 = cputime_to_cputime64(cputime); | |
4949 | struct rq *rq = this_rq(); | |
4950 | ||
4951 | if (atomic_read(&rq->nr_iowait) > 0) | |
4952 | cpustat->iowait = cputime64_add(cpustat->iowait, cputime64); | |
4953 | else | |
4954 | cpustat->idle = cputime64_add(cpustat->idle, cputime64); | |
4955 | } | |
4956 | ||
4957 | #ifndef CONFIG_VIRT_CPU_ACCOUNTING | |
4958 | ||
4959 | /* | |
4960 | * Account a single tick of cpu time. | |
4961 | * @p: the process that the cpu time gets accounted to | |
4962 | * @user_tick: indicates if the tick is a user or a system tick | |
4963 | */ | |
4964 | void account_process_tick(struct task_struct *p, int user_tick) | |
4965 | { | |
4966 | cputime_t one_jiffy = jiffies_to_cputime(1); | |
4967 | cputime_t one_jiffy_scaled = cputime_to_scaled(one_jiffy); | |
4968 | struct rq *rq = this_rq(); | |
4969 | ||
4970 | if (user_tick) | |
4971 | account_user_time(p, one_jiffy, one_jiffy_scaled); | |
4972 | else if ((p != rq->idle) || (irq_count() != HARDIRQ_OFFSET)) | |
4973 | account_system_time(p, HARDIRQ_OFFSET, one_jiffy, | |
4974 | one_jiffy_scaled); | |
4975 | else | |
4976 | account_idle_time(one_jiffy); | |
4977 | } | |
4978 | ||
4979 | /* | |
4980 | * Account multiple ticks of steal time. | |
4981 | * @p: the process from which the cpu time has been stolen | |
4982 | * @ticks: number of stolen ticks | |
4983 | */ | |
4984 | void account_steal_ticks(unsigned long ticks) | |
4985 | { | |
4986 | account_steal_time(jiffies_to_cputime(ticks)); | |
4987 | } | |
4988 | ||
4989 | /* | |
4990 | * Account multiple ticks of idle time. | |
4991 | * @ticks: number of stolen ticks | |
4992 | */ | |
4993 | void account_idle_ticks(unsigned long ticks) | |
4994 | { | |
4995 | account_idle_time(jiffies_to_cputime(ticks)); | |
4996 | } | |
4997 | ||
4998 | #endif | |
4999 | ||
5000 | /* | |
5001 | * Use precise platform statistics if available: | |
5002 | */ | |
5003 | #ifdef CONFIG_VIRT_CPU_ACCOUNTING | |
5004 | cputime_t task_utime(struct task_struct *p) | |
5005 | { | |
5006 | return p->utime; | |
5007 | } | |
5008 | ||
5009 | cputime_t task_stime(struct task_struct *p) | |
5010 | { | |
5011 | return p->stime; | |
5012 | } | |
5013 | #else | |
5014 | cputime_t task_utime(struct task_struct *p) | |
5015 | { | |
5016 | clock_t utime = cputime_to_clock_t(p->utime), | |
5017 | total = utime + cputime_to_clock_t(p->stime); | |
5018 | u64 temp; | |
5019 | ||
5020 | /* | |
5021 | * Use CFS's precise accounting: | |
5022 | */ | |
5023 | temp = (u64)nsec_to_clock_t(p->se.sum_exec_runtime); | |
5024 | ||
5025 | if (total) { | |
5026 | temp *= utime; | |
5027 | do_div(temp, total); | |
5028 | } | |
5029 | utime = (clock_t)temp; | |
5030 | ||
5031 | p->prev_utime = max(p->prev_utime, clock_t_to_cputime(utime)); | |
5032 | return p->prev_utime; | |
5033 | } | |
5034 | ||
5035 | cputime_t task_stime(struct task_struct *p) | |
5036 | { | |
5037 | clock_t stime; | |
5038 | ||
5039 | /* | |
5040 | * Use CFS's precise accounting. (we subtract utime from | |
5041 | * the total, to make sure the total observed by userspace | |
5042 | * grows monotonically - apps rely on that): | |
5043 | */ | |
5044 | stime = nsec_to_clock_t(p->se.sum_exec_runtime) - | |
5045 | cputime_to_clock_t(task_utime(p)); | |
5046 | ||
5047 | if (stime >= 0) | |
5048 | p->prev_stime = max(p->prev_stime, clock_t_to_cputime(stime)); | |
5049 | ||
5050 | return p->prev_stime; | |
5051 | } | |
5052 | #endif | |
5053 | ||
5054 | inline cputime_t task_gtime(struct task_struct *p) | |
5055 | { | |
5056 | return p->gtime; | |
5057 | } | |
5058 | ||
5059 | /* | |
5060 | * This function gets called by the timer code, with HZ frequency. | |
5061 | * We call it with interrupts disabled. | |
5062 | * | |
5063 | * It also gets called by the fork code, when changing the parent's | |
5064 | * timeslices. | |
5065 | */ | |
5066 | void scheduler_tick(void) | |
5067 | { | |
5068 | int cpu = smp_processor_id(); | |
5069 | struct rq *rq = cpu_rq(cpu); | |
5070 | struct task_struct *curr = rq->curr; | |
5071 | ||
5072 | sched_clock_tick(); | |
5073 | ||
5074 | spin_lock(&rq->lock); | |
5075 | update_rq_clock(rq); | |
5076 | update_cpu_load(rq); | |
5077 | curr->sched_class->task_tick(rq, curr, 0); | |
5078 | spin_unlock(&rq->lock); | |
5079 | ||
5080 | #ifdef CONFIG_SMP | |
5081 | rq->idle_at_tick = idle_cpu(cpu); | |
5082 | trigger_load_balance(rq, cpu); | |
5083 | #endif | |
5084 | } | |
5085 | ||
5086 | notrace unsigned long get_parent_ip(unsigned long addr) | |
5087 | { | |
5088 | if (in_lock_functions(addr)) { | |
5089 | addr = CALLER_ADDR2; | |
5090 | if (in_lock_functions(addr)) | |
5091 | addr = CALLER_ADDR3; | |
5092 | } | |
5093 | return addr; | |
5094 | } | |
5095 | ||
5096 | #if defined(CONFIG_PREEMPT) && (defined(CONFIG_DEBUG_PREEMPT) || \ | |
5097 | defined(CONFIG_PREEMPT_TRACER)) | |
5098 | ||
5099 | void __kprobes add_preempt_count(int val) | |
5100 | { | |
5101 | #ifdef CONFIG_DEBUG_PREEMPT | |
5102 | /* | |
5103 | * Underflow? | |
5104 | */ | |
5105 | if (DEBUG_LOCKS_WARN_ON((preempt_count() < 0))) | |
5106 | return; | |
5107 | #endif | |
5108 | preempt_count() += val; | |
5109 | #ifdef CONFIG_DEBUG_PREEMPT | |
5110 | /* | |
5111 | * Spinlock count overflowing soon? | |
5112 | */ | |
5113 | DEBUG_LOCKS_WARN_ON((preempt_count() & PREEMPT_MASK) >= | |
5114 | PREEMPT_MASK - 10); | |
5115 | #endif | |
5116 | if (preempt_count() == val) | |
5117 | trace_preempt_off(CALLER_ADDR0, get_parent_ip(CALLER_ADDR1)); | |
5118 | } | |
5119 | EXPORT_SYMBOL(add_preempt_count); | |
5120 | ||
5121 | void __kprobes sub_preempt_count(int val) | |
5122 | { | |
5123 | #ifdef CONFIG_DEBUG_PREEMPT | |
5124 | /* | |
5125 | * Underflow? | |
5126 | */ | |
5127 | if (DEBUG_LOCKS_WARN_ON(val > preempt_count())) | |
5128 | return; | |
5129 | /* | |
5130 | * Is the spinlock portion underflowing? | |
5131 | */ | |
5132 | if (DEBUG_LOCKS_WARN_ON((val < PREEMPT_MASK) && | |
5133 | !(preempt_count() & PREEMPT_MASK))) | |
5134 | return; | |
5135 | #endif | |
5136 | ||
5137 | if (preempt_count() == val) | |
5138 | trace_preempt_on(CALLER_ADDR0, get_parent_ip(CALLER_ADDR1)); | |
5139 | preempt_count() -= val; | |
5140 | } | |
5141 | EXPORT_SYMBOL(sub_preempt_count); | |
5142 | ||
5143 | #endif | |
5144 | ||
5145 | /* | |
5146 | * Print scheduling while atomic bug: | |
5147 | */ | |
5148 | static noinline void __schedule_bug(struct task_struct *prev) | |
5149 | { | |
5150 | struct pt_regs *regs = get_irq_regs(); | |
5151 | ||
5152 | printk(KERN_ERR "BUG: scheduling while atomic: %s/%d/0x%08x\n", | |
5153 | prev->comm, prev->pid, preempt_count()); | |
5154 | ||
5155 | debug_show_held_locks(prev); | |
5156 | print_modules(); | |
5157 | if (irqs_disabled()) | |
5158 | print_irqtrace_events(prev); | |
5159 | ||
5160 | if (regs) | |
5161 | show_regs(regs); | |
5162 | else | |
5163 | dump_stack(); | |
5164 | } | |
5165 | ||
5166 | /* | |
5167 | * Various schedule()-time debugging checks and statistics: | |
5168 | */ | |
5169 | static inline void schedule_debug(struct task_struct *prev) | |
5170 | { | |
5171 | /* | |
5172 | * Test if we are atomic. Since do_exit() needs to call into | |
5173 | * schedule() atomically, we ignore that path for now. | |
5174 | * Otherwise, whine if we are scheduling when we should not be. | |
5175 | */ | |
5176 | if (unlikely(in_atomic_preempt_off() && !prev->exit_state)) | |
5177 | __schedule_bug(prev); | |
5178 | ||
5179 | profile_hit(SCHED_PROFILING, __builtin_return_address(0)); | |
5180 | ||
5181 | schedstat_inc(this_rq(), sched_count); | |
5182 | #ifdef CONFIG_SCHEDSTATS | |
5183 | if (unlikely(prev->lock_depth >= 0)) { | |
5184 | schedstat_inc(this_rq(), bkl_count); | |
5185 | schedstat_inc(prev, sched_info.bkl_count); | |
5186 | } | |
5187 | #endif | |
5188 | } | |
5189 | ||
5190 | static void put_prev_task(struct rq *rq, struct task_struct *prev) | |
5191 | { | |
5192 | if (prev->state == TASK_RUNNING) { | |
5193 | u64 runtime = prev->se.sum_exec_runtime; | |
5194 | ||
5195 | runtime -= prev->se.prev_sum_exec_runtime; | |
5196 | runtime = min_t(u64, runtime, 2*sysctl_sched_migration_cost); | |
5197 | ||
5198 | /* | |
5199 | * In order to avoid avg_overlap growing stale when we are | |
5200 | * indeed overlapping and hence not getting put to sleep, grow | |
5201 | * the avg_overlap on preemption. | |
5202 | * | |
5203 | * We use the average preemption runtime because that | |
5204 | * correlates to the amount of cache footprint a task can | |
5205 | * build up. | |
5206 | */ | |
5207 | update_avg(&prev->se.avg_overlap, runtime); | |
5208 | } | |
5209 | prev->sched_class->put_prev_task(rq, prev); | |
5210 | } | |
5211 | ||
5212 | /* | |
5213 | * Pick up the highest-prio task: | |
5214 | */ | |
5215 | static inline struct task_struct * | |
5216 | pick_next_task(struct rq *rq) | |
5217 | { | |
5218 | const struct sched_class *class; | |
5219 | struct task_struct *p; | |
5220 | ||
5221 | /* | |
5222 | * Optimization: we know that if all tasks are in | |
5223 | * the fair class we can call that function directly: | |
5224 | */ | |
5225 | if (likely(rq->nr_running == rq->cfs.nr_running)) { | |
5226 | p = fair_sched_class.pick_next_task(rq); | |
5227 | if (likely(p)) | |
5228 | return p; | |
5229 | } | |
5230 | ||
5231 | class = sched_class_highest; | |
5232 | for ( ; ; ) { | |
5233 | p = class->pick_next_task(rq); | |
5234 | if (p) | |
5235 | return p; | |
5236 | /* | |
5237 | * Will never be NULL as the idle class always | |
5238 | * returns a non-NULL p: | |
5239 | */ | |
5240 | class = class->next; | |
5241 | } | |
5242 | } | |
5243 | ||
5244 | /* | |
5245 | * schedule() is the main scheduler function. | |
5246 | */ | |
5247 | asmlinkage void __sched schedule(void) | |
5248 | { | |
5249 | struct task_struct *prev, *next; | |
5250 | unsigned long *switch_count; | |
5251 | struct rq *rq; | |
5252 | int cpu; | |
5253 | ||
5254 | need_resched: | |
5255 | preempt_disable(); | |
5256 | cpu = smp_processor_id(); | |
5257 | rq = cpu_rq(cpu); | |
5258 | rcu_qsctr_inc(cpu); | |
5259 | prev = rq->curr; | |
5260 | switch_count = &prev->nivcsw; | |
5261 | ||
5262 | release_kernel_lock(prev); | |
5263 | need_resched_nonpreemptible: | |
5264 | ||
5265 | schedule_debug(prev); | |
5266 | ||
5267 | if (sched_feat(HRTICK)) | |
5268 | hrtick_clear(rq); | |
5269 | ||
5270 | spin_lock_irq(&rq->lock); | |
5271 | update_rq_clock(rq); | |
5272 | clear_tsk_need_resched(prev); | |
5273 | ||
5274 | if (prev->state && !(preempt_count() & PREEMPT_ACTIVE)) { | |
5275 | if (unlikely(signal_pending_state(prev->state, prev))) | |
5276 | prev->state = TASK_RUNNING; | |
5277 | else | |
5278 | deactivate_task(rq, prev, 1); | |
5279 | switch_count = &prev->nvcsw; | |
5280 | } | |
5281 | ||
5282 | #ifdef CONFIG_SMP | |
5283 | if (prev->sched_class->pre_schedule) | |
5284 | prev->sched_class->pre_schedule(rq, prev); | |
5285 | #endif | |
5286 | ||
5287 | if (unlikely(!rq->nr_running)) | |
5288 | idle_balance(cpu, rq); | |
5289 | ||
5290 | put_prev_task(rq, prev); | |
5291 | next = pick_next_task(rq); | |
5292 | ||
5293 | if (likely(prev != next)) { | |
5294 | sched_info_switch(prev, next); | |
5295 | ||
5296 | rq->nr_switches++; | |
5297 | rq->curr = next; | |
5298 | ++*switch_count; | |
5299 | ||
5300 | context_switch(rq, prev, next); /* unlocks the rq */ | |
5301 | /* | |
5302 | * the context switch might have flipped the stack from under | |
5303 | * us, hence refresh the local variables. | |
5304 | */ | |
5305 | cpu = smp_processor_id(); | |
5306 | rq = cpu_rq(cpu); | |
5307 | } else | |
5308 | spin_unlock_irq(&rq->lock); | |
5309 | ||
5310 | if (unlikely(reacquire_kernel_lock(current) < 0)) | |
5311 | goto need_resched_nonpreemptible; | |
5312 | ||
5313 | preempt_enable_no_resched(); | |
5314 | if (need_resched()) | |
5315 | goto need_resched; | |
5316 | } | |
5317 | EXPORT_SYMBOL(schedule); | |
5318 | ||
5319 | #ifdef CONFIG_SMP | |
5320 | /* | |
5321 | * Look out! "owner" is an entirely speculative pointer | |
5322 | * access and not reliable. | |
5323 | */ | |
5324 | int mutex_spin_on_owner(struct mutex *lock, struct thread_info *owner) | |
5325 | { | |
5326 | unsigned int cpu; | |
5327 | struct rq *rq; | |
5328 | ||
5329 | if (!sched_feat(OWNER_SPIN)) | |
5330 | return 0; | |
5331 | ||
5332 | #ifdef CONFIG_DEBUG_PAGEALLOC | |
5333 | /* | |
5334 | * Need to access the cpu field knowing that | |
5335 | * DEBUG_PAGEALLOC could have unmapped it if | |
5336 | * the mutex owner just released it and exited. | |
5337 | */ | |
5338 | if (probe_kernel_address(&owner->cpu, cpu)) | |
5339 | goto out; | |
5340 | #else | |
5341 | cpu = owner->cpu; | |
5342 | #endif | |
5343 | ||
5344 | /* | |
5345 | * Even if the access succeeded (likely case), | |
5346 | * the cpu field may no longer be valid. | |
5347 | */ | |
5348 | if (cpu >= nr_cpumask_bits) | |
5349 | goto out; | |
5350 | ||
5351 | /* | |
5352 | * We need to validate that we can do a | |
5353 | * get_cpu() and that we have the percpu area. | |
5354 | */ | |
5355 | if (!cpu_online(cpu)) | |
5356 | goto out; | |
5357 | ||
5358 | rq = cpu_rq(cpu); | |
5359 | ||
5360 | for (;;) { | |
5361 | /* | |
5362 | * Owner changed, break to re-assess state. | |
5363 | */ | |
5364 | if (lock->owner != owner) | |
5365 | break; | |
5366 | ||
5367 | /* | |
5368 | * Is that owner really running on that cpu? | |
5369 | */ | |
5370 | if (task_thread_info(rq->curr) != owner || need_resched()) | |
5371 | return 0; | |
5372 | ||
5373 | cpu_relax(); | |
5374 | } | |
5375 | out: | |
5376 | return 1; | |
5377 | } | |
5378 | #endif | |
5379 | ||
5380 | #ifdef CONFIG_PREEMPT | |
5381 | /* | |
5382 | * this is the entry point to schedule() from in-kernel preemption | |
5383 | * off of preempt_enable. Kernel preemptions off return from interrupt | |
5384 | * occur there and call schedule directly. | |
5385 | */ | |
5386 | asmlinkage void __sched preempt_schedule(void) | |
5387 | { | |
5388 | struct thread_info *ti = current_thread_info(); | |
5389 | ||
5390 | /* | |
5391 | * If there is a non-zero preempt_count or interrupts are disabled, | |
5392 | * we do not want to preempt the current task. Just return.. | |
5393 | */ | |
5394 | if (likely(ti->preempt_count || irqs_disabled())) | |
5395 | return; | |
5396 | ||
5397 | do { | |
5398 | add_preempt_count(PREEMPT_ACTIVE); | |
5399 | schedule(); | |
5400 | sub_preempt_count(PREEMPT_ACTIVE); | |
5401 | ||
5402 | /* | |
5403 | * Check again in case we missed a preemption opportunity | |
5404 | * between schedule and now. | |
5405 | */ | |
5406 | barrier(); | |
5407 | } while (need_resched()); | |
5408 | } | |
5409 | EXPORT_SYMBOL(preempt_schedule); | |
5410 | ||
5411 | /* | |
5412 | * this is the entry point to schedule() from kernel preemption | |
5413 | * off of irq context. | |
5414 | * Note, that this is called and return with irqs disabled. This will | |
5415 | * protect us against recursive calling from irq. | |
5416 | */ | |
5417 | asmlinkage void __sched preempt_schedule_irq(void) | |
5418 | { | |
5419 | struct thread_info *ti = current_thread_info(); | |
5420 | ||
5421 | /* Catch callers which need to be fixed */ | |
5422 | BUG_ON(ti->preempt_count || !irqs_disabled()); | |
5423 | ||
5424 | do { | |
5425 | add_preempt_count(PREEMPT_ACTIVE); | |
5426 | local_irq_enable(); | |
5427 | schedule(); | |
5428 | local_irq_disable(); | |
5429 | sub_preempt_count(PREEMPT_ACTIVE); | |
5430 | ||
5431 | /* | |
5432 | * Check again in case we missed a preemption opportunity | |
5433 | * between schedule and now. | |
5434 | */ | |
5435 | barrier(); | |
5436 | } while (need_resched()); | |
5437 | } | |
5438 | ||
5439 | #endif /* CONFIG_PREEMPT */ | |
5440 | ||
5441 | int default_wake_function(wait_queue_t *curr, unsigned mode, int sync, | |
5442 | void *key) | |
5443 | { | |
5444 | return try_to_wake_up(curr->private, mode, sync); | |
5445 | } | |
5446 | EXPORT_SYMBOL(default_wake_function); | |
5447 | ||
5448 | /* | |
5449 | * The core wakeup function. Non-exclusive wakeups (nr_exclusive == 0) just | |
5450 | * wake everything up. If it's an exclusive wakeup (nr_exclusive == small +ve | |
5451 | * number) then we wake all the non-exclusive tasks and one exclusive task. | |
5452 | * | |
5453 | * There are circumstances in which we can try to wake a task which has already | |
5454 | * started to run but is not in state TASK_RUNNING. try_to_wake_up() returns | |
5455 | * zero in this (rare) case, and we handle it by continuing to scan the queue. | |
5456 | */ | |
5457 | static void __wake_up_common(wait_queue_head_t *q, unsigned int mode, | |
5458 | int nr_exclusive, int sync, void *key) | |
5459 | { | |
5460 | wait_queue_t *curr, *next; | |
5461 | ||
5462 | list_for_each_entry_safe(curr, next, &q->task_list, task_list) { | |
5463 | unsigned flags = curr->flags; | |
5464 | ||
5465 | if (curr->func(curr, mode, sync, key) && | |
5466 | (flags & WQ_FLAG_EXCLUSIVE) && !--nr_exclusive) | |
5467 | break; | |
5468 | } | |
5469 | } | |
5470 | ||
5471 | /** | |
5472 | * __wake_up - wake up threads blocked on a waitqueue. | |
5473 | * @q: the waitqueue | |
5474 | * @mode: which threads | |
5475 | * @nr_exclusive: how many wake-one or wake-many threads to wake up | |
5476 | * @key: is directly passed to the wakeup function | |
5477 | * | |
5478 | * It may be assumed that this function implies a write memory barrier before | |
5479 | * changing the task state if and only if any tasks are woken up. | |
5480 | */ | |
5481 | void __wake_up(wait_queue_head_t *q, unsigned int mode, | |
5482 | int nr_exclusive, void *key) | |
5483 | { | |
5484 | unsigned long flags; | |
5485 | ||
5486 | spin_lock_irqsave(&q->lock, flags); | |
5487 | __wake_up_common(q, mode, nr_exclusive, 0, key); | |
5488 | spin_unlock_irqrestore(&q->lock, flags); | |
5489 | } | |
5490 | EXPORT_SYMBOL(__wake_up); | |
5491 | ||
5492 | /* | |
5493 | * Same as __wake_up but called with the spinlock in wait_queue_head_t held. | |
5494 | */ | |
5495 | void __wake_up_locked(wait_queue_head_t *q, unsigned int mode) | |
5496 | { | |
5497 | __wake_up_common(q, mode, 1, 0, NULL); | |
5498 | } | |
5499 | ||
5500 | void __wake_up_locked_key(wait_queue_head_t *q, unsigned int mode, void *key) | |
5501 | { | |
5502 | __wake_up_common(q, mode, 1, 0, key); | |
5503 | } | |
5504 | ||
5505 | /** | |
5506 | * __wake_up_sync_key - wake up threads blocked on a waitqueue. | |
5507 | * @q: the waitqueue | |
5508 | * @mode: which threads | |
5509 | * @nr_exclusive: how many wake-one or wake-many threads to wake up | |
5510 | * @key: opaque value to be passed to wakeup targets | |
5511 | * | |
5512 | * The sync wakeup differs that the waker knows that it will schedule | |
5513 | * away soon, so while the target thread will be woken up, it will not | |
5514 | * be migrated to another CPU - ie. the two threads are 'synchronized' | |
5515 | * with each other. This can prevent needless bouncing between CPUs. | |
5516 | * | |
5517 | * On UP it can prevent extra preemption. | |
5518 | * | |
5519 | * It may be assumed that this function implies a write memory barrier before | |
5520 | * changing the task state if and only if any tasks are woken up. | |
5521 | */ | |
5522 | void __wake_up_sync_key(wait_queue_head_t *q, unsigned int mode, | |
5523 | int nr_exclusive, void *key) | |
5524 | { | |
5525 | unsigned long flags; | |
5526 | int sync = 1; | |
5527 | ||
5528 | if (unlikely(!q)) | |
5529 | return; | |
5530 | ||
5531 | if (unlikely(!nr_exclusive)) | |
5532 | sync = 0; | |
5533 | ||
5534 | spin_lock_irqsave(&q->lock, flags); | |
5535 | __wake_up_common(q, mode, nr_exclusive, sync, key); | |
5536 | spin_unlock_irqrestore(&q->lock, flags); | |
5537 | } | |
5538 | EXPORT_SYMBOL_GPL(__wake_up_sync_key); | |
5539 | ||
5540 | /* | |
5541 | * __wake_up_sync - see __wake_up_sync_key() | |
5542 | */ | |
5543 | void __wake_up_sync(wait_queue_head_t *q, unsigned int mode, int nr_exclusive) | |
5544 | { | |
5545 | __wake_up_sync_key(q, mode, nr_exclusive, NULL); | |
5546 | } | |
5547 | EXPORT_SYMBOL_GPL(__wake_up_sync); /* For internal use only */ | |
5548 | ||
5549 | /** | |
5550 | * complete: - signals a single thread waiting on this completion | |
5551 | * @x: holds the state of this particular completion | |
5552 | * | |
5553 | * This will wake up a single thread waiting on this completion. Threads will be | |
5554 | * awakened in the same order in which they were queued. | |
5555 | * | |
5556 | * See also complete_all(), wait_for_completion() and related routines. | |
5557 | * | |
5558 | * It may be assumed that this function implies a write memory barrier before | |
5559 | * changing the task state if and only if any tasks are woken up. | |
5560 | */ | |
5561 | void complete(struct completion *x) | |
5562 | { | |
5563 | unsigned long flags; | |
5564 | ||
5565 | spin_lock_irqsave(&x->wait.lock, flags); | |
5566 | x->done++; | |
5567 | __wake_up_common(&x->wait, TASK_NORMAL, 1, 0, NULL); | |
5568 | spin_unlock_irqrestore(&x->wait.lock, flags); | |
5569 | } | |
5570 | EXPORT_SYMBOL(complete); | |
5571 | ||
5572 | /** | |
5573 | * complete_all: - signals all threads waiting on this completion | |
5574 | * @x: holds the state of this particular completion | |
5575 | * | |
5576 | * This will wake up all threads waiting on this particular completion event. | |
5577 | * | |
5578 | * It may be assumed that this function implies a write memory barrier before | |
5579 | * changing the task state if and only if any tasks are woken up. | |
5580 | */ | |
5581 | void complete_all(struct completion *x) | |
5582 | { | |
5583 | unsigned long flags; | |
5584 | ||
5585 | spin_lock_irqsave(&x->wait.lock, flags); | |
5586 | x->done += UINT_MAX/2; | |
5587 | __wake_up_common(&x->wait, TASK_NORMAL, 0, 0, NULL); | |
5588 | spin_unlock_irqrestore(&x->wait.lock, flags); | |
5589 | } | |
5590 | EXPORT_SYMBOL(complete_all); | |
5591 | ||
5592 | static inline long __sched | |
5593 | do_wait_for_common(struct completion *x, long timeout, int state) | |
5594 | { | |
5595 | if (!x->done) { | |
5596 | DECLARE_WAITQUEUE(wait, current); | |
5597 | ||
5598 | wait.flags |= WQ_FLAG_EXCLUSIVE; | |
5599 | __add_wait_queue_tail(&x->wait, &wait); | |
5600 | do { | |
5601 | if (signal_pending_state(state, current)) { | |
5602 | timeout = -ERESTARTSYS; | |
5603 | break; | |
5604 | } | |
5605 | __set_current_state(state); | |
5606 | spin_unlock_irq(&x->wait.lock); | |
5607 | timeout = schedule_timeout(timeout); | |
5608 | spin_lock_irq(&x->wait.lock); | |
5609 | } while (!x->done && timeout); | |
5610 | __remove_wait_queue(&x->wait, &wait); | |
5611 | if (!x->done) | |
5612 | return timeout; | |
5613 | } | |
5614 | x->done--; | |
5615 | return timeout ?: 1; | |
5616 | } | |
5617 | ||
5618 | static long __sched | |
5619 | wait_for_common(struct completion *x, long timeout, int state) | |
5620 | { | |
5621 | might_sleep(); | |
5622 | ||
5623 | spin_lock_irq(&x->wait.lock); | |
5624 | timeout = do_wait_for_common(x, timeout, state); | |
5625 | spin_unlock_irq(&x->wait.lock); | |
5626 | return timeout; | |
5627 | } | |
5628 | ||
5629 | /** | |
5630 | * wait_for_completion: - waits for completion of a task | |
5631 | * @x: holds the state of this particular completion | |
5632 | * | |
5633 | * This waits to be signaled for completion of a specific task. It is NOT | |
5634 | * interruptible and there is no timeout. | |
5635 | * | |
5636 | * See also similar routines (i.e. wait_for_completion_timeout()) with timeout | |
5637 | * and interrupt capability. Also see complete(). | |
5638 | */ | |
5639 | void __sched wait_for_completion(struct completion *x) | |
5640 | { | |
5641 | wait_for_common(x, MAX_SCHEDULE_TIMEOUT, TASK_UNINTERRUPTIBLE); | |
5642 | } | |
5643 | EXPORT_SYMBOL(wait_for_completion); | |
5644 | ||
5645 | /** | |
5646 | * wait_for_completion_timeout: - waits for completion of a task (w/timeout) | |
5647 | * @x: holds the state of this particular completion | |
5648 | * @timeout: timeout value in jiffies | |
5649 | * | |
5650 | * This waits for either a completion of a specific task to be signaled or for a | |
5651 | * specified timeout to expire. The timeout is in jiffies. It is not | |
5652 | * interruptible. | |
5653 | */ | |
5654 | unsigned long __sched | |
5655 | wait_for_completion_timeout(struct completion *x, unsigned long timeout) | |
5656 | { | |
5657 | return wait_for_common(x, timeout, TASK_UNINTERRUPTIBLE); | |
5658 | } | |
5659 | EXPORT_SYMBOL(wait_for_completion_timeout); | |
5660 | ||
5661 | /** | |
5662 | * wait_for_completion_interruptible: - waits for completion of a task (w/intr) | |
5663 | * @x: holds the state of this particular completion | |
5664 | * | |
5665 | * This waits for completion of a specific task to be signaled. It is | |
5666 | * interruptible. | |
5667 | */ | |
5668 | int __sched wait_for_completion_interruptible(struct completion *x) | |
5669 | { | |
5670 | long t = wait_for_common(x, MAX_SCHEDULE_TIMEOUT, TASK_INTERRUPTIBLE); | |
5671 | if (t == -ERESTARTSYS) | |
5672 | return t; | |
5673 | return 0; | |
5674 | } | |
5675 | EXPORT_SYMBOL(wait_for_completion_interruptible); | |
5676 | ||
5677 | /** | |
5678 | * wait_for_completion_interruptible_timeout: - waits for completion (w/(to,intr)) | |
5679 | * @x: holds the state of this particular completion | |
5680 | * @timeout: timeout value in jiffies | |
5681 | * | |
5682 | * This waits for either a completion of a specific task to be signaled or for a | |
5683 | * specified timeout to expire. It is interruptible. The timeout is in jiffies. | |
5684 | */ | |
5685 | unsigned long __sched | |
5686 | wait_for_completion_interruptible_timeout(struct completion *x, | |
5687 | unsigned long timeout) | |
5688 | { | |
5689 | return wait_for_common(x, timeout, TASK_INTERRUPTIBLE); | |
5690 | } | |
5691 | EXPORT_SYMBOL(wait_for_completion_interruptible_timeout); | |
5692 | ||
5693 | /** | |
5694 | * wait_for_completion_killable: - waits for completion of a task (killable) | |
5695 | * @x: holds the state of this particular completion | |
5696 | * | |
5697 | * This waits to be signaled for completion of a specific task. It can be | |
5698 | * interrupted by a kill signal. | |
5699 | */ | |
5700 | int __sched wait_for_completion_killable(struct completion *x) | |
5701 | { | |
5702 | long t = wait_for_common(x, MAX_SCHEDULE_TIMEOUT, TASK_KILLABLE); | |
5703 | if (t == -ERESTARTSYS) | |
5704 | return t; | |
5705 | return 0; | |
5706 | } | |
5707 | EXPORT_SYMBOL(wait_for_completion_killable); | |
5708 | ||
5709 | /** | |
5710 | * try_wait_for_completion - try to decrement a completion without blocking | |
5711 | * @x: completion structure | |
5712 | * | |
5713 | * Returns: 0 if a decrement cannot be done without blocking | |
5714 | * 1 if a decrement succeeded. | |
5715 | * | |
5716 | * If a completion is being used as a counting completion, | |
5717 | * attempt to decrement the counter without blocking. This | |
5718 | * enables us to avoid waiting if the resource the completion | |
5719 | * is protecting is not available. | |
5720 | */ | |
5721 | bool try_wait_for_completion(struct completion *x) | |
5722 | { | |
5723 | int ret = 1; | |
5724 | ||
5725 | spin_lock_irq(&x->wait.lock); | |
5726 | if (!x->done) | |
5727 | ret = 0; | |
5728 | else | |
5729 | x->done--; | |
5730 | spin_unlock_irq(&x->wait.lock); | |
5731 | return ret; | |
5732 | } | |
5733 | EXPORT_SYMBOL(try_wait_for_completion); | |
5734 | ||
5735 | /** | |
5736 | * completion_done - Test to see if a completion has any waiters | |
5737 | * @x: completion structure | |
5738 | * | |
5739 | * Returns: 0 if there are waiters (wait_for_completion() in progress) | |
5740 | * 1 if there are no waiters. | |
5741 | * | |
5742 | */ | |
5743 | bool completion_done(struct completion *x) | |
5744 | { | |
5745 | int ret = 1; | |
5746 | ||
5747 | spin_lock_irq(&x->wait.lock); | |
5748 | if (!x->done) | |
5749 | ret = 0; | |
5750 | spin_unlock_irq(&x->wait.lock); | |
5751 | return ret; | |
5752 | } | |
5753 | EXPORT_SYMBOL(completion_done); | |
5754 | ||
5755 | static long __sched | |
5756 | sleep_on_common(wait_queue_head_t *q, int state, long timeout) | |
5757 | { | |
5758 | unsigned long flags; | |
5759 | wait_queue_t wait; | |
5760 | ||
5761 | init_waitqueue_entry(&wait, current); | |
5762 | ||
5763 | __set_current_state(state); | |
5764 | ||
5765 | spin_lock_irqsave(&q->lock, flags); | |
5766 | __add_wait_queue(q, &wait); | |
5767 | spin_unlock(&q->lock); | |
5768 | timeout = schedule_timeout(timeout); | |
5769 | spin_lock_irq(&q->lock); | |
5770 | __remove_wait_queue(q, &wait); | |
5771 | spin_unlock_irqrestore(&q->lock, flags); | |
5772 | ||
5773 | return timeout; | |
5774 | } | |
5775 | ||
5776 | void __sched interruptible_sleep_on(wait_queue_head_t *q) | |
5777 | { | |
5778 | sleep_on_common(q, TASK_INTERRUPTIBLE, MAX_SCHEDULE_TIMEOUT); | |
5779 | } | |
5780 | EXPORT_SYMBOL(interruptible_sleep_on); | |
5781 | ||
5782 | long __sched | |
5783 | interruptible_sleep_on_timeout(wait_queue_head_t *q, long timeout) | |
5784 | { | |
5785 | return sleep_on_common(q, TASK_INTERRUPTIBLE, timeout); | |
5786 | } | |
5787 | EXPORT_SYMBOL(interruptible_sleep_on_timeout); | |
5788 | ||
5789 | void __sched sleep_on(wait_queue_head_t *q) | |
5790 | { | |
5791 | sleep_on_common(q, TASK_UNINTERRUPTIBLE, MAX_SCHEDULE_TIMEOUT); | |
5792 | } | |
5793 | EXPORT_SYMBOL(sleep_on); | |
5794 | ||
5795 | long __sched sleep_on_timeout(wait_queue_head_t *q, long timeout) | |
5796 | { | |
5797 | return sleep_on_common(q, TASK_UNINTERRUPTIBLE, timeout); | |
5798 | } | |
5799 | EXPORT_SYMBOL(sleep_on_timeout); | |
5800 | ||
5801 | #ifdef CONFIG_RT_MUTEXES | |
5802 | ||
5803 | /* | |
5804 | * rt_mutex_setprio - set the current priority of a task | |
5805 | * @p: task | |
5806 | * @prio: prio value (kernel-internal form) | |
5807 | * | |
5808 | * This function changes the 'effective' priority of a task. It does | |
5809 | * not touch ->normal_prio like __setscheduler(). | |
5810 | * | |
5811 | * Used by the rt_mutex code to implement priority inheritance logic. | |
5812 | */ | |
5813 | void rt_mutex_setprio(struct task_struct *p, int prio) | |
5814 | { | |
5815 | unsigned long flags; | |
5816 | int oldprio, on_rq, running; | |
5817 | struct rq *rq; | |
5818 | const struct sched_class *prev_class = p->sched_class; | |
5819 | ||
5820 | BUG_ON(prio < 0 || prio > MAX_PRIO); | |
5821 | ||
5822 | rq = task_rq_lock(p, &flags); | |
5823 | update_rq_clock(rq); | |
5824 | ||
5825 | oldprio = p->prio; | |
5826 | on_rq = p->se.on_rq; | |
5827 | running = task_current(rq, p); | |
5828 | if (on_rq) | |
5829 | dequeue_task(rq, p, 0); | |
5830 | if (running) | |
5831 | p->sched_class->put_prev_task(rq, p); | |
5832 | ||
5833 | if (rt_prio(prio)) | |
5834 | p->sched_class = &rt_sched_class; | |
5835 | else | |
5836 | p->sched_class = &fair_sched_class; | |
5837 | ||
5838 | p->prio = prio; | |
5839 | ||
5840 | if (running) | |
5841 | p->sched_class->set_curr_task(rq); | |
5842 | if (on_rq) { | |
5843 | enqueue_task(rq, p, 0); | |
5844 | ||
5845 | check_class_changed(rq, p, prev_class, oldprio, running); | |
5846 | } | |
5847 | task_rq_unlock(rq, &flags); | |
5848 | } | |
5849 | ||
5850 | #endif | |
5851 | ||
5852 | void set_user_nice(struct task_struct *p, long nice) | |
5853 | { | |
5854 | int old_prio, delta, on_rq; | |
5855 | unsigned long flags; | |
5856 | struct rq *rq; | |
5857 | ||
5858 | if (TASK_NICE(p) == nice || nice < -20 || nice > 19) | |
5859 | return; | |
5860 | /* | |
5861 | * We have to be careful, if called from sys_setpriority(), | |
5862 | * the task might be in the middle of scheduling on another CPU. | |
5863 | */ | |
5864 | rq = task_rq_lock(p, &flags); | |
5865 | update_rq_clock(rq); | |
5866 | /* | |
5867 | * The RT priorities are set via sched_setscheduler(), but we still | |
5868 | * allow the 'normal' nice value to be set - but as expected | |
5869 | * it wont have any effect on scheduling until the task is | |
5870 | * SCHED_FIFO/SCHED_RR: | |
5871 | */ | |
5872 | if (task_has_rt_policy(p)) { | |
5873 | p->static_prio = NICE_TO_PRIO(nice); | |
5874 | goto out_unlock; | |
5875 | } | |
5876 | on_rq = p->se.on_rq; | |
5877 | if (on_rq) | |
5878 | dequeue_task(rq, p, 0); | |
5879 | ||
5880 | p->static_prio = NICE_TO_PRIO(nice); | |
5881 | set_load_weight(p); | |
5882 | old_prio = p->prio; | |
5883 | p->prio = effective_prio(p); | |
5884 | delta = p->prio - old_prio; | |
5885 | ||
5886 | if (on_rq) { | |
5887 | enqueue_task(rq, p, 0); | |
5888 | /* | |
5889 | * If the task increased its priority or is running and | |
5890 | * lowered its priority, then reschedule its CPU: | |
5891 | */ | |
5892 | if (delta < 0 || (delta > 0 && task_running(rq, p))) | |
5893 | resched_task(rq->curr); | |
5894 | } | |
5895 | out_unlock: | |
5896 | task_rq_unlock(rq, &flags); | |
5897 | } | |
5898 | EXPORT_SYMBOL(set_user_nice); | |
5899 | ||
5900 | /* | |
5901 | * can_nice - check if a task can reduce its nice value | |
5902 | * @p: task | |
5903 | * @nice: nice value | |
5904 | */ | |
5905 | int can_nice(const struct task_struct *p, const int nice) | |
5906 | { | |
5907 | /* convert nice value [19,-20] to rlimit style value [1,40] */ | |
5908 | int nice_rlim = 20 - nice; | |
5909 | ||
5910 | return (nice_rlim <= p->signal->rlim[RLIMIT_NICE].rlim_cur || | |
5911 | capable(CAP_SYS_NICE)); | |
5912 | } | |
5913 | ||
5914 | #ifdef __ARCH_WANT_SYS_NICE | |
5915 | ||
5916 | /* | |
5917 | * sys_nice - change the priority of the current process. | |
5918 | * @increment: priority increment | |
5919 | * | |
5920 | * sys_setpriority is a more generic, but much slower function that | |
5921 | * does similar things. | |
5922 | */ | |
5923 | SYSCALL_DEFINE1(nice, int, increment) | |
5924 | { | |
5925 | long nice, retval; | |
5926 | ||
5927 | /* | |
5928 | * Setpriority might change our priority at the same moment. | |
5929 | * We don't have to worry. Conceptually one call occurs first | |
5930 | * and we have a single winner. | |
5931 | */ | |
5932 | if (increment < -40) | |
5933 | increment = -40; | |
5934 | if (increment > 40) | |
5935 | increment = 40; | |
5936 | ||
5937 | nice = TASK_NICE(current) + increment; | |
5938 | if (nice < -20) | |
5939 | nice = -20; | |
5940 | if (nice > 19) | |
5941 | nice = 19; | |
5942 | ||
5943 | if (increment < 0 && !can_nice(current, nice)) | |
5944 | return -EPERM; | |
5945 | ||
5946 | retval = security_task_setnice(current, nice); | |
5947 | if (retval) | |
5948 | return retval; | |
5949 | ||
5950 | set_user_nice(current, nice); | |
5951 | return 0; | |
5952 | } | |
5953 | ||
5954 | #endif | |
5955 | ||
5956 | /** | |
5957 | * task_prio - return the priority value of a given task. | |
5958 | * @p: the task in question. | |
5959 | * | |
5960 | * This is the priority value as seen by users in /proc. | |
5961 | * RT tasks are offset by -200. Normal tasks are centered | |
5962 | * around 0, value goes from -16 to +15. | |
5963 | */ | |
5964 | int task_prio(const struct task_struct *p) | |
5965 | { | |
5966 | return p->prio - MAX_RT_PRIO; | |
5967 | } | |
5968 | ||
5969 | /** | |
5970 | * task_nice - return the nice value of a given task. | |
5971 | * @p: the task in question. | |
5972 | */ | |
5973 | int task_nice(const struct task_struct *p) | |
5974 | { | |
5975 | return TASK_NICE(p); | |
5976 | } | |
5977 | EXPORT_SYMBOL(task_nice); | |
5978 | ||
5979 | /** | |
5980 | * idle_cpu - is a given cpu idle currently? | |
5981 | * @cpu: the processor in question. | |
5982 | */ | |
5983 | int idle_cpu(int cpu) | |
5984 | { | |
5985 | return cpu_curr(cpu) == cpu_rq(cpu)->idle; | |
5986 | } | |
5987 | ||
5988 | /** | |
5989 | * idle_task - return the idle task for a given cpu. | |
5990 | * @cpu: the processor in question. | |
5991 | */ | |
5992 | struct task_struct *idle_task(int cpu) | |
5993 | { | |
5994 | return cpu_rq(cpu)->idle; | |
5995 | } | |
5996 | ||
5997 | /** | |
5998 | * find_process_by_pid - find a process with a matching PID value. | |
5999 | * @pid: the pid in question. | |
6000 | */ | |
6001 | static struct task_struct *find_process_by_pid(pid_t pid) | |
6002 | { | |
6003 | return pid ? find_task_by_vpid(pid) : current; | |
6004 | } | |
6005 | ||
6006 | /* Actually do priority change: must hold rq lock. */ | |
6007 | static void | |
6008 | __setscheduler(struct rq *rq, struct task_struct *p, int policy, int prio) | |
6009 | { | |
6010 | BUG_ON(p->se.on_rq); | |
6011 | ||
6012 | p->policy = policy; | |
6013 | switch (p->policy) { | |
6014 | case SCHED_NORMAL: | |
6015 | case SCHED_BATCH: | |
6016 | case SCHED_IDLE: | |
6017 | p->sched_class = &fair_sched_class; | |
6018 | break; | |
6019 | case SCHED_FIFO: | |
6020 | case SCHED_RR: | |
6021 | p->sched_class = &rt_sched_class; | |
6022 | break; | |
6023 | } | |
6024 | ||
6025 | p->rt_priority = prio; | |
6026 | p->normal_prio = normal_prio(p); | |
6027 | /* we are holding p->pi_lock already */ | |
6028 | p->prio = rt_mutex_getprio(p); | |
6029 | set_load_weight(p); | |
6030 | } | |
6031 | ||
6032 | /* | |
6033 | * check the target process has a UID that matches the current process's | |
6034 | */ | |
6035 | static bool check_same_owner(struct task_struct *p) | |
6036 | { | |
6037 | const struct cred *cred = current_cred(), *pcred; | |
6038 | bool match; | |
6039 | ||
6040 | rcu_read_lock(); | |
6041 | pcred = __task_cred(p); | |
6042 | match = (cred->euid == pcred->euid || | |
6043 | cred->euid == pcred->uid); | |
6044 | rcu_read_unlock(); | |
6045 | return match; | |
6046 | } | |
6047 | ||
6048 | static int __sched_setscheduler(struct task_struct *p, int policy, | |
6049 | struct sched_param *param, bool user) | |
6050 | { | |
6051 | int retval, oldprio, oldpolicy = -1, on_rq, running; | |
6052 | unsigned long flags; | |
6053 | const struct sched_class *prev_class = p->sched_class; | |
6054 | struct rq *rq; | |
6055 | ||
6056 | /* may grab non-irq protected spin_locks */ | |
6057 | BUG_ON(in_interrupt()); | |
6058 | recheck: | |
6059 | /* double check policy once rq lock held */ | |
6060 | if (policy < 0) | |
6061 | policy = oldpolicy = p->policy; | |
6062 | else if (policy != SCHED_FIFO && policy != SCHED_RR && | |
6063 | policy != SCHED_NORMAL && policy != SCHED_BATCH && | |
6064 | policy != SCHED_IDLE) | |
6065 | return -EINVAL; | |
6066 | /* | |
6067 | * Valid priorities for SCHED_FIFO and SCHED_RR are | |
6068 | * 1..MAX_USER_RT_PRIO-1, valid priority for SCHED_NORMAL, | |
6069 | * SCHED_BATCH and SCHED_IDLE is 0. | |
6070 | */ | |
6071 | if (param->sched_priority < 0 || | |
6072 | (p->mm && param->sched_priority > MAX_USER_RT_PRIO-1) || | |
6073 | (!p->mm && param->sched_priority > MAX_RT_PRIO-1)) | |
6074 | return -EINVAL; | |
6075 | if (rt_policy(policy) != (param->sched_priority != 0)) | |
6076 | return -EINVAL; | |
6077 | ||
6078 | /* | |
6079 | * Allow unprivileged RT tasks to decrease priority: | |
6080 | */ | |
6081 | if (user && !capable(CAP_SYS_NICE)) { | |
6082 | if (rt_policy(policy)) { | |
6083 | unsigned long rlim_rtprio; | |
6084 | ||
6085 | if (!lock_task_sighand(p, &flags)) | |
6086 | return -ESRCH; | |
6087 | rlim_rtprio = p->signal->rlim[RLIMIT_RTPRIO].rlim_cur; | |
6088 | unlock_task_sighand(p, &flags); | |
6089 | ||
6090 | /* can't set/change the rt policy */ | |
6091 | if (policy != p->policy && !rlim_rtprio) | |
6092 | return -EPERM; | |
6093 | ||
6094 | /* can't increase priority */ | |
6095 | if (param->sched_priority > p->rt_priority && | |
6096 | param->sched_priority > rlim_rtprio) | |
6097 | return -EPERM; | |
6098 | } | |
6099 | /* | |
6100 | * Like positive nice levels, dont allow tasks to | |
6101 | * move out of SCHED_IDLE either: | |
6102 | */ | |
6103 | if (p->policy == SCHED_IDLE && policy != SCHED_IDLE) | |
6104 | return -EPERM; | |
6105 | ||
6106 | /* can't change other user's priorities */ | |
6107 | if (!check_same_owner(p)) | |
6108 | return -EPERM; | |
6109 | } | |
6110 | ||
6111 | if (user) { | |
6112 | #ifdef CONFIG_RT_GROUP_SCHED | |
6113 | /* | |
6114 | * Do not allow realtime tasks into groups that have no runtime | |
6115 | * assigned. | |
6116 | */ | |
6117 | if (rt_bandwidth_enabled() && rt_policy(policy) && | |
6118 | task_group(p)->rt_bandwidth.rt_runtime == 0) | |
6119 | return -EPERM; | |
6120 | #endif | |
6121 | ||
6122 | retval = security_task_setscheduler(p, policy, param); | |
6123 | if (retval) | |
6124 | return retval; | |
6125 | } | |
6126 | ||
6127 | /* | |
6128 | * make sure no PI-waiters arrive (or leave) while we are | |
6129 | * changing the priority of the task: | |
6130 | */ | |
6131 | spin_lock_irqsave(&p->pi_lock, flags); | |
6132 | /* | |
6133 | * To be able to change p->policy safely, the apropriate | |
6134 | * runqueue lock must be held. | |
6135 | */ | |
6136 | rq = __task_rq_lock(p); | |
6137 | /* recheck policy now with rq lock held */ | |
6138 | if (unlikely(oldpolicy != -1 && oldpolicy != p->policy)) { | |
6139 | policy = oldpolicy = -1; | |
6140 | __task_rq_unlock(rq); | |
6141 | spin_unlock_irqrestore(&p->pi_lock, flags); | |
6142 | goto recheck; | |
6143 | } | |
6144 | update_rq_clock(rq); | |
6145 | on_rq = p->se.on_rq; | |
6146 | running = task_current(rq, p); | |
6147 | if (on_rq) | |
6148 | deactivate_task(rq, p, 0); | |
6149 | if (running) | |
6150 | p->sched_class->put_prev_task(rq, p); | |
6151 | ||
6152 | oldprio = p->prio; | |
6153 | __setscheduler(rq, p, policy, param->sched_priority); | |
6154 | ||
6155 | if (running) | |
6156 | p->sched_class->set_curr_task(rq); | |
6157 | if (on_rq) { | |
6158 | activate_task(rq, p, 0); | |
6159 | ||
6160 | check_class_changed(rq, p, prev_class, oldprio, running); | |
6161 | } | |
6162 | __task_rq_unlock(rq); | |
6163 | spin_unlock_irqrestore(&p->pi_lock, flags); | |
6164 | ||
6165 | rt_mutex_adjust_pi(p); | |
6166 | ||
6167 | return 0; | |
6168 | } | |
6169 | ||
6170 | /** | |
6171 | * sched_setscheduler - change the scheduling policy and/or RT priority of a thread. | |
6172 | * @p: the task in question. | |
6173 | * @policy: new policy. | |
6174 | * @param: structure containing the new RT priority. | |
6175 | * | |
6176 | * NOTE that the task may be already dead. | |
6177 | */ | |
6178 | int sched_setscheduler(struct task_struct *p, int policy, | |
6179 | struct sched_param *param) | |
6180 | { | |
6181 | return __sched_setscheduler(p, policy, param, true); | |
6182 | } | |
6183 | EXPORT_SYMBOL_GPL(sched_setscheduler); | |
6184 | ||
6185 | /** | |
6186 | * sched_setscheduler_nocheck - change the scheduling policy and/or RT priority of a thread from kernelspace. | |
6187 | * @p: the task in question. | |
6188 | * @policy: new policy. | |
6189 | * @param: structure containing the new RT priority. | |
6190 | * | |
6191 | * Just like sched_setscheduler, only don't bother checking if the | |
6192 | * current context has permission. For example, this is needed in | |
6193 | * stop_machine(): we create temporary high priority worker threads, | |
6194 | * but our caller might not have that capability. | |
6195 | */ | |
6196 | int sched_setscheduler_nocheck(struct task_struct *p, int policy, | |
6197 | struct sched_param *param) | |
6198 | { | |
6199 | return __sched_setscheduler(p, policy, param, false); | |
6200 | } | |
6201 | ||
6202 | static int | |
6203 | do_sched_setscheduler(pid_t pid, int policy, struct sched_param __user *param) | |
6204 | { | |
6205 | struct sched_param lparam; | |
6206 | struct task_struct *p; | |
6207 | int retval; | |
6208 | ||
6209 | if (!param || pid < 0) | |
6210 | return -EINVAL; | |
6211 | if (copy_from_user(&lparam, param, sizeof(struct sched_param))) | |
6212 | return -EFAULT; | |
6213 | ||
6214 | rcu_read_lock(); | |
6215 | retval = -ESRCH; | |
6216 | p = find_process_by_pid(pid); | |
6217 | if (p != NULL) | |
6218 | retval = sched_setscheduler(p, policy, &lparam); | |
6219 | rcu_read_unlock(); | |
6220 | ||
6221 | return retval; | |
6222 | } | |
6223 | ||
6224 | /** | |
6225 | * sys_sched_setscheduler - set/change the scheduler policy and RT priority | |
6226 | * @pid: the pid in question. | |
6227 | * @policy: new policy. | |
6228 | * @param: structure containing the new RT priority. | |
6229 | */ | |
6230 | SYSCALL_DEFINE3(sched_setscheduler, pid_t, pid, int, policy, | |
6231 | struct sched_param __user *, param) | |
6232 | { | |
6233 | /* negative values for policy are not valid */ | |
6234 | if (policy < 0) | |
6235 | return -EINVAL; | |
6236 | ||
6237 | return do_sched_setscheduler(pid, policy, param); | |
6238 | } | |
6239 | ||
6240 | /** | |
6241 | * sys_sched_setparam - set/change the RT priority of a thread | |
6242 | * @pid: the pid in question. | |
6243 | * @param: structure containing the new RT priority. | |
6244 | */ | |
6245 | SYSCALL_DEFINE2(sched_setparam, pid_t, pid, struct sched_param __user *, param) | |
6246 | { | |
6247 | return do_sched_setscheduler(pid, -1, param); | |
6248 | } | |
6249 | ||
6250 | /** | |
6251 | * sys_sched_getscheduler - get the policy (scheduling class) of a thread | |
6252 | * @pid: the pid in question. | |
6253 | */ | |
6254 | SYSCALL_DEFINE1(sched_getscheduler, pid_t, pid) | |
6255 | { | |
6256 | struct task_struct *p; | |
6257 | int retval; | |
6258 | ||
6259 | if (pid < 0) | |
6260 | return -EINVAL; | |
6261 | ||
6262 | retval = -ESRCH; | |
6263 | read_lock(&tasklist_lock); | |
6264 | p = find_process_by_pid(pid); | |
6265 | if (p) { | |
6266 | retval = security_task_getscheduler(p); | |
6267 | if (!retval) | |
6268 | retval = p->policy; | |
6269 | } | |
6270 | read_unlock(&tasklist_lock); | |
6271 | return retval; | |
6272 | } | |
6273 | ||
6274 | /** | |
6275 | * sys_sched_getscheduler - get the RT priority of a thread | |
6276 | * @pid: the pid in question. | |
6277 | * @param: structure containing the RT priority. | |
6278 | */ | |
6279 | SYSCALL_DEFINE2(sched_getparam, pid_t, pid, struct sched_param __user *, param) | |
6280 | { | |
6281 | struct sched_param lp; | |
6282 | struct task_struct *p; | |
6283 | int retval; | |
6284 | ||
6285 | if (!param || pid < 0) | |
6286 | return -EINVAL; | |
6287 | ||
6288 | read_lock(&tasklist_lock); | |
6289 | p = find_process_by_pid(pid); | |
6290 | retval = -ESRCH; | |
6291 | if (!p) | |
6292 | goto out_unlock; | |
6293 | ||
6294 | retval = security_task_getscheduler(p); | |
6295 | if (retval) | |
6296 | goto out_unlock; | |
6297 | ||
6298 | lp.sched_priority = p->rt_priority; | |
6299 | read_unlock(&tasklist_lock); | |
6300 | ||
6301 | /* | |
6302 | * This one might sleep, we cannot do it with a spinlock held ... | |
6303 | */ | |
6304 | retval = copy_to_user(param, &lp, sizeof(*param)) ? -EFAULT : 0; | |
6305 | ||
6306 | return retval; | |
6307 | ||
6308 | out_unlock: | |
6309 | read_unlock(&tasklist_lock); | |
6310 | return retval; | |
6311 | } | |
6312 | ||
6313 | long sched_setaffinity(pid_t pid, const struct cpumask *in_mask) | |
6314 | { | |
6315 | cpumask_var_t cpus_allowed, new_mask; | |
6316 | struct task_struct *p; | |
6317 | int retval; | |
6318 | ||
6319 | get_online_cpus(); | |
6320 | read_lock(&tasklist_lock); | |
6321 | ||
6322 | p = find_process_by_pid(pid); | |
6323 | if (!p) { | |
6324 | read_unlock(&tasklist_lock); | |
6325 | put_online_cpus(); | |
6326 | return -ESRCH; | |
6327 | } | |
6328 | ||
6329 | /* | |
6330 | * It is not safe to call set_cpus_allowed with the | |
6331 | * tasklist_lock held. We will bump the task_struct's | |
6332 | * usage count and then drop tasklist_lock. | |
6333 | */ | |
6334 | get_task_struct(p); | |
6335 | read_unlock(&tasklist_lock); | |
6336 | ||
6337 | if (!alloc_cpumask_var(&cpus_allowed, GFP_KERNEL)) { | |
6338 | retval = -ENOMEM; | |
6339 | goto out_put_task; | |
6340 | } | |
6341 | if (!alloc_cpumask_var(&new_mask, GFP_KERNEL)) { | |
6342 | retval = -ENOMEM; | |
6343 | goto out_free_cpus_allowed; | |
6344 | } | |
6345 | retval = -EPERM; | |
6346 | if (!check_same_owner(p) && !capable(CAP_SYS_NICE)) | |
6347 | goto out_unlock; | |
6348 | ||
6349 | retval = security_task_setscheduler(p, 0, NULL); | |
6350 | if (retval) | |
6351 | goto out_unlock; | |
6352 | ||
6353 | cpuset_cpus_allowed(p, cpus_allowed); | |
6354 | cpumask_and(new_mask, in_mask, cpus_allowed); | |
6355 | again: | |
6356 | retval = set_cpus_allowed_ptr(p, new_mask); | |
6357 | ||
6358 | if (!retval) { | |
6359 | cpuset_cpus_allowed(p, cpus_allowed); | |
6360 | if (!cpumask_subset(new_mask, cpus_allowed)) { | |
6361 | /* | |
6362 | * We must have raced with a concurrent cpuset | |
6363 | * update. Just reset the cpus_allowed to the | |
6364 | * cpuset's cpus_allowed | |
6365 | */ | |
6366 | cpumask_copy(new_mask, cpus_allowed); | |
6367 | goto again; | |
6368 | } | |
6369 | } | |
6370 | out_unlock: | |
6371 | free_cpumask_var(new_mask); | |
6372 | out_free_cpus_allowed: | |
6373 | free_cpumask_var(cpus_allowed); | |
6374 | out_put_task: | |
6375 | put_task_struct(p); | |
6376 | put_online_cpus(); | |
6377 | return retval; | |
6378 | } | |
6379 | ||
6380 | static int get_user_cpu_mask(unsigned long __user *user_mask_ptr, unsigned len, | |
6381 | struct cpumask *new_mask) | |
6382 | { | |
6383 | if (len < cpumask_size()) | |
6384 | cpumask_clear(new_mask); | |
6385 | else if (len > cpumask_size()) | |
6386 | len = cpumask_size(); | |
6387 | ||
6388 | return copy_from_user(new_mask, user_mask_ptr, len) ? -EFAULT : 0; | |
6389 | } | |
6390 | ||
6391 | /** | |
6392 | * sys_sched_setaffinity - set the cpu affinity of a process | |
6393 | * @pid: pid of the process | |
6394 | * @len: length in bytes of the bitmask pointed to by user_mask_ptr | |
6395 | * @user_mask_ptr: user-space pointer to the new cpu mask | |
6396 | */ | |
6397 | SYSCALL_DEFINE3(sched_setaffinity, pid_t, pid, unsigned int, len, | |
6398 | unsigned long __user *, user_mask_ptr) | |
6399 | { | |
6400 | cpumask_var_t new_mask; | |
6401 | int retval; | |
6402 | ||
6403 | if (!alloc_cpumask_var(&new_mask, GFP_KERNEL)) | |
6404 | return -ENOMEM; | |
6405 | ||
6406 | retval = get_user_cpu_mask(user_mask_ptr, len, new_mask); | |
6407 | if (retval == 0) | |
6408 | retval = sched_setaffinity(pid, new_mask); | |
6409 | free_cpumask_var(new_mask); | |
6410 | return retval; | |
6411 | } | |
6412 | ||
6413 | long sched_getaffinity(pid_t pid, struct cpumask *mask) | |
6414 | { | |
6415 | struct task_struct *p; | |
6416 | int retval; | |
6417 | ||
6418 | get_online_cpus(); | |
6419 | read_lock(&tasklist_lock); | |
6420 | ||
6421 | retval = -ESRCH; | |
6422 | p = find_process_by_pid(pid); | |
6423 | if (!p) | |
6424 | goto out_unlock; | |
6425 | ||
6426 | retval = security_task_getscheduler(p); | |
6427 | if (retval) | |
6428 | goto out_unlock; | |
6429 | ||
6430 | cpumask_and(mask, &p->cpus_allowed, cpu_online_mask); | |
6431 | ||
6432 | out_unlock: | |
6433 | read_unlock(&tasklist_lock); | |
6434 | put_online_cpus(); | |
6435 | ||
6436 | return retval; | |
6437 | } | |
6438 | ||
6439 | /** | |
6440 | * sys_sched_getaffinity - get the cpu affinity of a process | |
6441 | * @pid: pid of the process | |
6442 | * @len: length in bytes of the bitmask pointed to by user_mask_ptr | |
6443 | * @user_mask_ptr: user-space pointer to hold the current cpu mask | |
6444 | */ | |
6445 | SYSCALL_DEFINE3(sched_getaffinity, pid_t, pid, unsigned int, len, | |
6446 | unsigned long __user *, user_mask_ptr) | |
6447 | { | |
6448 | int ret; | |
6449 | cpumask_var_t mask; | |
6450 | ||
6451 | if (len < cpumask_size()) | |
6452 | return -EINVAL; | |
6453 | ||
6454 | if (!alloc_cpumask_var(&mask, GFP_KERNEL)) | |
6455 | return -ENOMEM; | |
6456 | ||
6457 | ret = sched_getaffinity(pid, mask); | |
6458 | if (ret == 0) { | |
6459 | if (copy_to_user(user_mask_ptr, mask, cpumask_size())) | |
6460 | ret = -EFAULT; | |
6461 | else | |
6462 | ret = cpumask_size(); | |
6463 | } | |
6464 | free_cpumask_var(mask); | |
6465 | ||
6466 | return ret; | |
6467 | } | |
6468 | ||
6469 | /** | |
6470 | * sys_sched_yield - yield the current processor to other threads. | |
6471 | * | |
6472 | * This function yields the current CPU to other tasks. If there are no | |
6473 | * other threads running on this CPU then this function will return. | |
6474 | */ | |
6475 | SYSCALL_DEFINE0(sched_yield) | |
6476 | { | |
6477 | struct rq *rq = this_rq_lock(); | |
6478 | ||
6479 | schedstat_inc(rq, yld_count); | |
6480 | current->sched_class->yield_task(rq); | |
6481 | ||
6482 | /* | |
6483 | * Since we are going to call schedule() anyway, there's | |
6484 | * no need to preempt or enable interrupts: | |
6485 | */ | |
6486 | __release(rq->lock); | |
6487 | spin_release(&rq->lock.dep_map, 1, _THIS_IP_); | |
6488 | _raw_spin_unlock(&rq->lock); | |
6489 | preempt_enable_no_resched(); | |
6490 | ||
6491 | schedule(); | |
6492 | ||
6493 | return 0; | |
6494 | } | |
6495 | ||
6496 | static void __cond_resched(void) | |
6497 | { | |
6498 | #ifdef CONFIG_DEBUG_SPINLOCK_SLEEP | |
6499 | __might_sleep(__FILE__, __LINE__); | |
6500 | #endif | |
6501 | /* | |
6502 | * The BKS might be reacquired before we have dropped | |
6503 | * PREEMPT_ACTIVE, which could trigger a second | |
6504 | * cond_resched() call. | |
6505 | */ | |
6506 | do { | |
6507 | add_preempt_count(PREEMPT_ACTIVE); | |
6508 | schedule(); | |
6509 | sub_preempt_count(PREEMPT_ACTIVE); | |
6510 | } while (need_resched()); | |
6511 | } | |
6512 | ||
6513 | int __sched _cond_resched(void) | |
6514 | { | |
6515 | if (need_resched() && !(preempt_count() & PREEMPT_ACTIVE) && | |
6516 | system_state == SYSTEM_RUNNING) { | |
6517 | __cond_resched(); | |
6518 | return 1; | |
6519 | } | |
6520 | return 0; | |
6521 | } | |
6522 | EXPORT_SYMBOL(_cond_resched); | |
6523 | ||
6524 | /* | |
6525 | * cond_resched_lock() - if a reschedule is pending, drop the given lock, | |
6526 | * call schedule, and on return reacquire the lock. | |
6527 | * | |
6528 | * This works OK both with and without CONFIG_PREEMPT. We do strange low-level | |
6529 | * operations here to prevent schedule() from being called twice (once via | |
6530 | * spin_unlock(), once by hand). | |
6531 | */ | |
6532 | int cond_resched_lock(spinlock_t *lock) | |
6533 | { | |
6534 | int resched = need_resched() && system_state == SYSTEM_RUNNING; | |
6535 | int ret = 0; | |
6536 | ||
6537 | if (spin_needbreak(lock) || resched) { | |
6538 | spin_unlock(lock); | |
6539 | if (resched && need_resched()) | |
6540 | __cond_resched(); | |
6541 | else | |
6542 | cpu_relax(); | |
6543 | ret = 1; | |
6544 | spin_lock(lock); | |
6545 | } | |
6546 | return ret; | |
6547 | } | |
6548 | EXPORT_SYMBOL(cond_resched_lock); | |
6549 | ||
6550 | int __sched cond_resched_softirq(void) | |
6551 | { | |
6552 | BUG_ON(!in_softirq()); | |
6553 | ||
6554 | if (need_resched() && system_state == SYSTEM_RUNNING) { | |
6555 | local_bh_enable(); | |
6556 | __cond_resched(); | |
6557 | local_bh_disable(); | |
6558 | return 1; | |
6559 | } | |
6560 | return 0; | |
6561 | } | |
6562 | EXPORT_SYMBOL(cond_resched_softirq); | |
6563 | ||
6564 | /** | |
6565 | * yield - yield the current processor to other threads. | |
6566 | * | |
6567 | * This is a shortcut for kernel-space yielding - it marks the | |
6568 | * thread runnable and calls sys_sched_yield(). | |
6569 | */ | |
6570 | void __sched yield(void) | |
6571 | { | |
6572 | set_current_state(TASK_RUNNING); | |
6573 | sys_sched_yield(); | |
6574 | } | |
6575 | EXPORT_SYMBOL(yield); | |
6576 | ||
6577 | /* | |
6578 | * This task is about to go to sleep on IO. Increment rq->nr_iowait so | |
6579 | * that process accounting knows that this is a task in IO wait state. | |
6580 | * | |
6581 | * But don't do that if it is a deliberate, throttling IO wait (this task | |
6582 | * has set its backing_dev_info: the queue against which it should throttle) | |
6583 | */ | |
6584 | void __sched io_schedule(void) | |
6585 | { | |
6586 | struct rq *rq = &__raw_get_cpu_var(runqueues); | |
6587 | ||
6588 | delayacct_blkio_start(); | |
6589 | atomic_inc(&rq->nr_iowait); | |
6590 | schedule(); | |
6591 | atomic_dec(&rq->nr_iowait); | |
6592 | delayacct_blkio_end(); | |
6593 | } | |
6594 | EXPORT_SYMBOL(io_schedule); | |
6595 | ||
6596 | long __sched io_schedule_timeout(long timeout) | |
6597 | { | |
6598 | struct rq *rq = &__raw_get_cpu_var(runqueues); | |
6599 | long ret; | |
6600 | ||
6601 | delayacct_blkio_start(); | |
6602 | atomic_inc(&rq->nr_iowait); | |
6603 | ret = schedule_timeout(timeout); | |
6604 | atomic_dec(&rq->nr_iowait); | |
6605 | delayacct_blkio_end(); | |
6606 | return ret; | |
6607 | } | |
6608 | ||
6609 | /** | |
6610 | * sys_sched_get_priority_max - return maximum RT priority. | |
6611 | * @policy: scheduling class. | |
6612 | * | |
6613 | * this syscall returns the maximum rt_priority that can be used | |
6614 | * by a given scheduling class. | |
6615 | */ | |
6616 | SYSCALL_DEFINE1(sched_get_priority_max, int, policy) | |
6617 | { | |
6618 | int ret = -EINVAL; | |
6619 | ||
6620 | switch (policy) { | |
6621 | case SCHED_FIFO: | |
6622 | case SCHED_RR: | |
6623 | ret = MAX_USER_RT_PRIO-1; | |
6624 | break; | |
6625 | case SCHED_NORMAL: | |
6626 | case SCHED_BATCH: | |
6627 | case SCHED_IDLE: | |
6628 | ret = 0; | |
6629 | break; | |
6630 | } | |
6631 | return ret; | |
6632 | } | |
6633 | ||
6634 | /** | |
6635 | * sys_sched_get_priority_min - return minimum RT priority. | |
6636 | * @policy: scheduling class. | |
6637 | * | |
6638 | * this syscall returns the minimum rt_priority that can be used | |
6639 | * by a given scheduling class. | |
6640 | */ | |
6641 | SYSCALL_DEFINE1(sched_get_priority_min, int, policy) | |
6642 | { | |
6643 | int ret = -EINVAL; | |
6644 | ||
6645 | switch (policy) { | |
6646 | case SCHED_FIFO: | |
6647 | case SCHED_RR: | |
6648 | ret = 1; | |
6649 | break; | |
6650 | case SCHED_NORMAL: | |
6651 | case SCHED_BATCH: | |
6652 | case SCHED_IDLE: | |
6653 | ret = 0; | |
6654 | } | |
6655 | return ret; | |
6656 | } | |
6657 | ||
6658 | /** | |
6659 | * sys_sched_rr_get_interval - return the default timeslice of a process. | |
6660 | * @pid: pid of the process. | |
6661 | * @interval: userspace pointer to the timeslice value. | |
6662 | * | |
6663 | * this syscall writes the default timeslice value of a given process | |
6664 | * into the user-space timespec buffer. A value of '0' means infinity. | |
6665 | */ | |
6666 | SYSCALL_DEFINE2(sched_rr_get_interval, pid_t, pid, | |
6667 | struct timespec __user *, interval) | |
6668 | { | |
6669 | struct task_struct *p; | |
6670 | unsigned int time_slice; | |
6671 | int retval; | |
6672 | struct timespec t; | |
6673 | ||
6674 | if (pid < 0) | |
6675 | return -EINVAL; | |
6676 | ||
6677 | retval = -ESRCH; | |
6678 | read_lock(&tasklist_lock); | |
6679 | p = find_process_by_pid(pid); | |
6680 | if (!p) | |
6681 | goto out_unlock; | |
6682 | ||
6683 | retval = security_task_getscheduler(p); | |
6684 | if (retval) | |
6685 | goto out_unlock; | |
6686 | ||
6687 | /* | |
6688 | * Time slice is 0 for SCHED_FIFO tasks and for SCHED_OTHER | |
6689 | * tasks that are on an otherwise idle runqueue: | |
6690 | */ | |
6691 | time_slice = 0; | |
6692 | if (p->policy == SCHED_RR) { | |
6693 | time_slice = DEF_TIMESLICE; | |
6694 | } else if (p->policy != SCHED_FIFO) { | |
6695 | struct sched_entity *se = &p->se; | |
6696 | unsigned long flags; | |
6697 | struct rq *rq; | |
6698 | ||
6699 | rq = task_rq_lock(p, &flags); | |
6700 | if (rq->cfs.load.weight) | |
6701 | time_slice = NS_TO_JIFFIES(sched_slice(&rq->cfs, se)); | |
6702 | task_rq_unlock(rq, &flags); | |
6703 | } | |
6704 | read_unlock(&tasklist_lock); | |
6705 | jiffies_to_timespec(time_slice, &t); | |
6706 | retval = copy_to_user(interval, &t, sizeof(t)) ? -EFAULT : 0; | |
6707 | return retval; | |
6708 | ||
6709 | out_unlock: | |
6710 | read_unlock(&tasklist_lock); | |
6711 | return retval; | |
6712 | } | |
6713 | ||
6714 | static const char stat_nam[] = TASK_STATE_TO_CHAR_STR; | |
6715 | ||
6716 | void sched_show_task(struct task_struct *p) | |
6717 | { | |
6718 | unsigned long free = 0; | |
6719 | unsigned state; | |
6720 | ||
6721 | state = p->state ? __ffs(p->state) + 1 : 0; | |
6722 | printk(KERN_INFO "%-13.13s %c", p->comm, | |
6723 | state < sizeof(stat_nam) - 1 ? stat_nam[state] : '?'); | |
6724 | #if BITS_PER_LONG == 32 | |
6725 | if (state == TASK_RUNNING) | |
6726 | printk(KERN_CONT " running "); | |
6727 | else | |
6728 | printk(KERN_CONT " %08lx ", thread_saved_pc(p)); | |
6729 | #else | |
6730 | if (state == TASK_RUNNING) | |
6731 | printk(KERN_CONT " running task "); | |
6732 | else | |
6733 | printk(KERN_CONT " %016lx ", thread_saved_pc(p)); | |
6734 | #endif | |
6735 | #ifdef CONFIG_DEBUG_STACK_USAGE | |
6736 | free = stack_not_used(p); | |
6737 | #endif | |
6738 | printk(KERN_CONT "%5lu %5d %6d 0x%08lx\n", free, | |
6739 | task_pid_nr(p), task_pid_nr(p->real_parent), | |
6740 | (unsigned long)task_thread_info(p)->flags); | |
6741 | ||
6742 | show_stack(p, NULL); | |
6743 | } | |
6744 | ||
6745 | void show_state_filter(unsigned long state_filter) | |
6746 | { | |
6747 | struct task_struct *g, *p; | |
6748 | ||
6749 | #if BITS_PER_LONG == 32 | |
6750 | printk(KERN_INFO | |
6751 | " task PC stack pid father\n"); | |
6752 | #else | |
6753 | printk(KERN_INFO | |
6754 | " task PC stack pid father\n"); | |
6755 | #endif | |
6756 | read_lock(&tasklist_lock); | |
6757 | do_each_thread(g, p) { | |
6758 | /* | |
6759 | * reset the NMI-timeout, listing all files on a slow | |
6760 | * console might take alot of time: | |
6761 | */ | |
6762 | touch_nmi_watchdog(); | |
6763 | if (!state_filter || (p->state & state_filter)) | |
6764 | sched_show_task(p); | |
6765 | } while_each_thread(g, p); | |
6766 | ||
6767 | touch_all_softlockup_watchdogs(); | |
6768 | ||
6769 | #ifdef CONFIG_SCHED_DEBUG | |
6770 | sysrq_sched_debug_show(); | |
6771 | #endif | |
6772 | read_unlock(&tasklist_lock); | |
6773 | /* | |
6774 | * Only show locks if all tasks are dumped: | |
6775 | */ | |
6776 | if (state_filter == -1) | |
6777 | debug_show_all_locks(); | |
6778 | } | |
6779 | ||
6780 | void __cpuinit init_idle_bootup_task(struct task_struct *idle) | |
6781 | { | |
6782 | idle->sched_class = &idle_sched_class; | |
6783 | } | |
6784 | ||
6785 | /** | |
6786 | * init_idle - set up an idle thread for a given CPU | |
6787 | * @idle: task in question | |
6788 | * @cpu: cpu the idle task belongs to | |
6789 | * | |
6790 | * NOTE: this function does not set the idle thread's NEED_RESCHED | |
6791 | * flag, to make booting more robust. | |
6792 | */ | |
6793 | void __cpuinit init_idle(struct task_struct *idle, int cpu) | |
6794 | { | |
6795 | struct rq *rq = cpu_rq(cpu); | |
6796 | unsigned long flags; | |
6797 | ||
6798 | spin_lock_irqsave(&rq->lock, flags); | |
6799 | ||
6800 | __sched_fork(idle); | |
6801 | idle->se.exec_start = sched_clock(); | |
6802 | ||
6803 | idle->prio = idle->normal_prio = MAX_PRIO; | |
6804 | cpumask_copy(&idle->cpus_allowed, cpumask_of(cpu)); | |
6805 | __set_task_cpu(idle, cpu); | |
6806 | ||
6807 | rq->curr = rq->idle = idle; | |
6808 | #if defined(CONFIG_SMP) && defined(__ARCH_WANT_UNLOCKED_CTXSW) | |
6809 | idle->oncpu = 1; | |
6810 | #endif | |
6811 | spin_unlock_irqrestore(&rq->lock, flags); | |
6812 | ||
6813 | /* Set the preempt count _outside_ the spinlocks! */ | |
6814 | #if defined(CONFIG_PREEMPT) | |
6815 | task_thread_info(idle)->preempt_count = (idle->lock_depth >= 0); | |
6816 | #else | |
6817 | task_thread_info(idle)->preempt_count = 0; | |
6818 | #endif | |
6819 | /* | |
6820 | * The idle tasks have their own, simple scheduling class: | |
6821 | */ | |
6822 | idle->sched_class = &idle_sched_class; | |
6823 | ftrace_graph_init_task(idle); | |
6824 | } | |
6825 | ||
6826 | /* | |
6827 | * In a system that switches off the HZ timer nohz_cpu_mask | |
6828 | * indicates which cpus entered this state. This is used | |
6829 | * in the rcu update to wait only for active cpus. For system | |
6830 | * which do not switch off the HZ timer nohz_cpu_mask should | |
6831 | * always be CPU_BITS_NONE. | |
6832 | */ | |
6833 | cpumask_var_t nohz_cpu_mask; | |
6834 | ||
6835 | /* | |
6836 | * Increase the granularity value when there are more CPUs, | |
6837 | * because with more CPUs the 'effective latency' as visible | |
6838 | * to users decreases. But the relationship is not linear, | |
6839 | * so pick a second-best guess by going with the log2 of the | |
6840 | * number of CPUs. | |
6841 | * | |
6842 | * This idea comes from the SD scheduler of Con Kolivas: | |
6843 | */ | |
6844 | static inline void sched_init_granularity(void) | |
6845 | { | |
6846 | unsigned int factor = 1 + ilog2(num_online_cpus()); | |
6847 | const unsigned long limit = 200000000; | |
6848 | ||
6849 | sysctl_sched_min_granularity *= factor; | |
6850 | if (sysctl_sched_min_granularity > limit) | |
6851 | sysctl_sched_min_granularity = limit; | |
6852 | ||
6853 | sysctl_sched_latency *= factor; | |
6854 | if (sysctl_sched_latency > limit) | |
6855 | sysctl_sched_latency = limit; | |
6856 | ||
6857 | sysctl_sched_wakeup_granularity *= factor; | |
6858 | ||
6859 | sysctl_sched_shares_ratelimit *= factor; | |
6860 | } | |
6861 | ||
6862 | #ifdef CONFIG_SMP | |
6863 | /* | |
6864 | * This is how migration works: | |
6865 | * | |
6866 | * 1) we queue a struct migration_req structure in the source CPU's | |
6867 | * runqueue and wake up that CPU's migration thread. | |
6868 | * 2) we down() the locked semaphore => thread blocks. | |
6869 | * 3) migration thread wakes up (implicitly it forces the migrated | |
6870 | * thread off the CPU) | |
6871 | * 4) it gets the migration request and checks whether the migrated | |
6872 | * task is still in the wrong runqueue. | |
6873 | * 5) if it's in the wrong runqueue then the migration thread removes | |
6874 | * it and puts it into the right queue. | |
6875 | * 6) migration thread up()s the semaphore. | |
6876 | * 7) we wake up and the migration is done. | |
6877 | */ | |
6878 | ||
6879 | /* | |
6880 | * Change a given task's CPU affinity. Migrate the thread to a | |
6881 | * proper CPU and schedule it away if the CPU it's executing on | |
6882 | * is removed from the allowed bitmask. | |
6883 | * | |
6884 | * NOTE: the caller must have a valid reference to the task, the | |
6885 | * task must not exit() & deallocate itself prematurely. The | |
6886 | * call is not atomic; no spinlocks may be held. | |
6887 | */ | |
6888 | int set_cpus_allowed_ptr(struct task_struct *p, const struct cpumask *new_mask) | |
6889 | { | |
6890 | struct migration_req req; | |
6891 | unsigned long flags; | |
6892 | struct rq *rq; | |
6893 | int ret = 0; | |
6894 | ||
6895 | rq = task_rq_lock(p, &flags); | |
6896 | if (!cpumask_intersects(new_mask, cpu_online_mask)) { | |
6897 | ret = -EINVAL; | |
6898 | goto out; | |
6899 | } | |
6900 | ||
6901 | if (unlikely((p->flags & PF_THREAD_BOUND) && p != current && | |
6902 | !cpumask_equal(&p->cpus_allowed, new_mask))) { | |
6903 | ret = -EINVAL; | |
6904 | goto out; | |
6905 | } | |
6906 | ||
6907 | if (p->sched_class->set_cpus_allowed) | |
6908 | p->sched_class->set_cpus_allowed(p, new_mask); | |
6909 | else { | |
6910 | cpumask_copy(&p->cpus_allowed, new_mask); | |
6911 | p->rt.nr_cpus_allowed = cpumask_weight(new_mask); | |
6912 | } | |
6913 | ||
6914 | /* Can the task run on the task's current CPU? If so, we're done */ | |
6915 | if (cpumask_test_cpu(task_cpu(p), new_mask)) | |
6916 | goto out; | |
6917 | ||
6918 | if (migrate_task(p, cpumask_any_and(cpu_online_mask, new_mask), &req)) { | |
6919 | /* Need help from migration thread: drop lock and wait. */ | |
6920 | task_rq_unlock(rq, &flags); | |
6921 | wake_up_process(rq->migration_thread); | |
6922 | wait_for_completion(&req.done); | |
6923 | tlb_migrate_finish(p->mm); | |
6924 | return 0; | |
6925 | } | |
6926 | out: | |
6927 | task_rq_unlock(rq, &flags); | |
6928 | ||
6929 | return ret; | |
6930 | } | |
6931 | EXPORT_SYMBOL_GPL(set_cpus_allowed_ptr); | |
6932 | ||
6933 | /* | |
6934 | * Move (not current) task off this cpu, onto dest cpu. We're doing | |
6935 | * this because either it can't run here any more (set_cpus_allowed() | |
6936 | * away from this CPU, or CPU going down), or because we're | |
6937 | * attempting to rebalance this task on exec (sched_exec). | |
6938 | * | |
6939 | * So we race with normal scheduler movements, but that's OK, as long | |
6940 | * as the task is no longer on this CPU. | |
6941 | * | |
6942 | * Returns non-zero if task was successfully migrated. | |
6943 | */ | |
6944 | static int __migrate_task(struct task_struct *p, int src_cpu, int dest_cpu) | |
6945 | { | |
6946 | struct rq *rq_dest, *rq_src; | |
6947 | int ret = 0, on_rq; | |
6948 | ||
6949 | if (unlikely(!cpu_active(dest_cpu))) | |
6950 | return ret; | |
6951 | ||
6952 | rq_src = cpu_rq(src_cpu); | |
6953 | rq_dest = cpu_rq(dest_cpu); | |
6954 | ||
6955 | double_rq_lock(rq_src, rq_dest); | |
6956 | /* Already moved. */ | |
6957 | if (task_cpu(p) != src_cpu) | |
6958 | goto done; | |
6959 | /* Affinity changed (again). */ | |
6960 | if (!cpumask_test_cpu(dest_cpu, &p->cpus_allowed)) | |
6961 | goto fail; | |
6962 | ||
6963 | on_rq = p->se.on_rq; | |
6964 | if (on_rq) | |
6965 | deactivate_task(rq_src, p, 0); | |
6966 | ||
6967 | set_task_cpu(p, dest_cpu); | |
6968 | if (on_rq) { | |
6969 | activate_task(rq_dest, p, 0); | |
6970 | check_preempt_curr(rq_dest, p, 0); | |
6971 | } | |
6972 | done: | |
6973 | ret = 1; | |
6974 | fail: | |
6975 | double_rq_unlock(rq_src, rq_dest); | |
6976 | return ret; | |
6977 | } | |
6978 | ||
6979 | /* | |
6980 | * migration_thread - this is a highprio system thread that performs | |
6981 | * thread migration by bumping thread off CPU then 'pushing' onto | |
6982 | * another runqueue. | |
6983 | */ | |
6984 | static int migration_thread(void *data) | |
6985 | { | |
6986 | int cpu = (long)data; | |
6987 | struct rq *rq; | |
6988 | ||
6989 | rq = cpu_rq(cpu); | |
6990 | BUG_ON(rq->migration_thread != current); | |
6991 | ||
6992 | set_current_state(TASK_INTERRUPTIBLE); | |
6993 | while (!kthread_should_stop()) { | |
6994 | struct migration_req *req; | |
6995 | struct list_head *head; | |
6996 | ||
6997 | spin_lock_irq(&rq->lock); | |
6998 | ||
6999 | if (cpu_is_offline(cpu)) { | |
7000 | spin_unlock_irq(&rq->lock); | |
7001 | goto wait_to_die; | |
7002 | } | |
7003 | ||
7004 | if (rq->active_balance) { | |
7005 | active_load_balance(rq, cpu); | |
7006 | rq->active_balance = 0; | |
7007 | } | |
7008 | ||
7009 | head = &rq->migration_queue; | |
7010 | ||
7011 | if (list_empty(head)) { | |
7012 | spin_unlock_irq(&rq->lock); | |
7013 | schedule(); | |
7014 | set_current_state(TASK_INTERRUPTIBLE); | |
7015 | continue; | |
7016 | } | |
7017 | req = list_entry(head->next, struct migration_req, list); | |
7018 | list_del_init(head->next); | |
7019 | ||
7020 | spin_unlock(&rq->lock); | |
7021 | __migrate_task(req->task, cpu, req->dest_cpu); | |
7022 | local_irq_enable(); | |
7023 | ||
7024 | complete(&req->done); | |
7025 | } | |
7026 | __set_current_state(TASK_RUNNING); | |
7027 | return 0; | |
7028 | ||
7029 | wait_to_die: | |
7030 | /* Wait for kthread_stop */ | |
7031 | set_current_state(TASK_INTERRUPTIBLE); | |
7032 | while (!kthread_should_stop()) { | |
7033 | schedule(); | |
7034 | set_current_state(TASK_INTERRUPTIBLE); | |
7035 | } | |
7036 | __set_current_state(TASK_RUNNING); | |
7037 | return 0; | |
7038 | } | |
7039 | ||
7040 | #ifdef CONFIG_HOTPLUG_CPU | |
7041 | ||
7042 | static int __migrate_task_irq(struct task_struct *p, int src_cpu, int dest_cpu) | |
7043 | { | |
7044 | int ret; | |
7045 | ||
7046 | local_irq_disable(); | |
7047 | ret = __migrate_task(p, src_cpu, dest_cpu); | |
7048 | local_irq_enable(); | |
7049 | return ret; | |
7050 | } | |
7051 | ||
7052 | /* | |
7053 | * Figure out where task on dead CPU should go, use force if necessary. | |
7054 | */ | |
7055 | static void move_task_off_dead_cpu(int dead_cpu, struct task_struct *p) | |
7056 | { | |
7057 | int dest_cpu; | |
7058 | const struct cpumask *nodemask = cpumask_of_node(cpu_to_node(dead_cpu)); | |
7059 | ||
7060 | again: | |
7061 | /* Look for allowed, online CPU in same node. */ | |
7062 | for_each_cpu_and(dest_cpu, nodemask, cpu_online_mask) | |
7063 | if (cpumask_test_cpu(dest_cpu, &p->cpus_allowed)) | |
7064 | goto move; | |
7065 | ||
7066 | /* Any allowed, online CPU? */ | |
7067 | dest_cpu = cpumask_any_and(&p->cpus_allowed, cpu_online_mask); | |
7068 | if (dest_cpu < nr_cpu_ids) | |
7069 | goto move; | |
7070 | ||
7071 | /* No more Mr. Nice Guy. */ | |
7072 | if (dest_cpu >= nr_cpu_ids) { | |
7073 | cpuset_cpus_allowed_locked(p, &p->cpus_allowed); | |
7074 | dest_cpu = cpumask_any_and(cpu_online_mask, &p->cpus_allowed); | |
7075 | ||
7076 | /* | |
7077 | * Don't tell them about moving exiting tasks or | |
7078 | * kernel threads (both mm NULL), since they never | |
7079 | * leave kernel. | |
7080 | */ | |
7081 | if (p->mm && printk_ratelimit()) { | |
7082 | printk(KERN_INFO "process %d (%s) no " | |
7083 | "longer affine to cpu%d\n", | |
7084 | task_pid_nr(p), p->comm, dead_cpu); | |
7085 | } | |
7086 | } | |
7087 | ||
7088 | move: | |
7089 | /* It can have affinity changed while we were choosing. */ | |
7090 | if (unlikely(!__migrate_task_irq(p, dead_cpu, dest_cpu))) | |
7091 | goto again; | |
7092 | } | |
7093 | ||
7094 | /* | |
7095 | * While a dead CPU has no uninterruptible tasks queued at this point, | |
7096 | * it might still have a nonzero ->nr_uninterruptible counter, because | |
7097 | * for performance reasons the counter is not stricly tracking tasks to | |
7098 | * their home CPUs. So we just add the counter to another CPU's counter, | |
7099 | * to keep the global sum constant after CPU-down: | |
7100 | */ | |
7101 | static void migrate_nr_uninterruptible(struct rq *rq_src) | |
7102 | { | |
7103 | struct rq *rq_dest = cpu_rq(cpumask_any(cpu_online_mask)); | |
7104 | unsigned long flags; | |
7105 | ||
7106 | local_irq_save(flags); | |
7107 | double_rq_lock(rq_src, rq_dest); | |
7108 | rq_dest->nr_uninterruptible += rq_src->nr_uninterruptible; | |
7109 | rq_src->nr_uninterruptible = 0; | |
7110 | double_rq_unlock(rq_src, rq_dest); | |
7111 | local_irq_restore(flags); | |
7112 | } | |
7113 | ||
7114 | /* Run through task list and migrate tasks from the dead cpu. */ | |
7115 | static void migrate_live_tasks(int src_cpu) | |
7116 | { | |
7117 | struct task_struct *p, *t; | |
7118 | ||
7119 | read_lock(&tasklist_lock); | |
7120 | ||
7121 | do_each_thread(t, p) { | |
7122 | if (p == current) | |
7123 | continue; | |
7124 | ||
7125 | if (task_cpu(p) == src_cpu) | |
7126 | move_task_off_dead_cpu(src_cpu, p); | |
7127 | } while_each_thread(t, p); | |
7128 | ||
7129 | read_unlock(&tasklist_lock); | |
7130 | } | |
7131 | ||
7132 | /* | |
7133 | * Schedules idle task to be the next runnable task on current CPU. | |
7134 | * It does so by boosting its priority to highest possible. | |
7135 | * Used by CPU offline code. | |
7136 | */ | |
7137 | void sched_idle_next(void) | |
7138 | { | |
7139 | int this_cpu = smp_processor_id(); | |
7140 | struct rq *rq = cpu_rq(this_cpu); | |
7141 | struct task_struct *p = rq->idle; | |
7142 | unsigned long flags; | |
7143 | ||
7144 | /* cpu has to be offline */ | |
7145 | BUG_ON(cpu_online(this_cpu)); | |
7146 | ||
7147 | /* | |
7148 | * Strictly not necessary since rest of the CPUs are stopped by now | |
7149 | * and interrupts disabled on the current cpu. | |
7150 | */ | |
7151 | spin_lock_irqsave(&rq->lock, flags); | |
7152 | ||
7153 | __setscheduler(rq, p, SCHED_FIFO, MAX_RT_PRIO-1); | |
7154 | ||
7155 | update_rq_clock(rq); | |
7156 | activate_task(rq, p, 0); | |
7157 | ||
7158 | spin_unlock_irqrestore(&rq->lock, flags); | |
7159 | } | |
7160 | ||
7161 | /* | |
7162 | * Ensures that the idle task is using init_mm right before its cpu goes | |
7163 | * offline. | |
7164 | */ | |
7165 | void idle_task_exit(void) | |
7166 | { | |
7167 | struct mm_struct *mm = current->active_mm; | |
7168 | ||
7169 | BUG_ON(cpu_online(smp_processor_id())); | |
7170 | ||
7171 | if (mm != &init_mm) | |
7172 | switch_mm(mm, &init_mm, current); | |
7173 | mmdrop(mm); | |
7174 | } | |
7175 | ||
7176 | /* called under rq->lock with disabled interrupts */ | |
7177 | static void migrate_dead(unsigned int dead_cpu, struct task_struct *p) | |
7178 | { | |
7179 | struct rq *rq = cpu_rq(dead_cpu); | |
7180 | ||
7181 | /* Must be exiting, otherwise would be on tasklist. */ | |
7182 | BUG_ON(!p->exit_state); | |
7183 | ||
7184 | /* Cannot have done final schedule yet: would have vanished. */ | |
7185 | BUG_ON(p->state == TASK_DEAD); | |
7186 | ||
7187 | get_task_struct(p); | |
7188 | ||
7189 | /* | |
7190 | * Drop lock around migration; if someone else moves it, | |
7191 | * that's OK. No task can be added to this CPU, so iteration is | |
7192 | * fine. | |
7193 | */ | |
7194 | spin_unlock_irq(&rq->lock); | |
7195 | move_task_off_dead_cpu(dead_cpu, p); | |
7196 | spin_lock_irq(&rq->lock); | |
7197 | ||
7198 | put_task_struct(p); | |
7199 | } | |
7200 | ||
7201 | /* release_task() removes task from tasklist, so we won't find dead tasks. */ | |
7202 | static void migrate_dead_tasks(unsigned int dead_cpu) | |
7203 | { | |
7204 | struct rq *rq = cpu_rq(dead_cpu); | |
7205 | struct task_struct *next; | |
7206 | ||
7207 | for ( ; ; ) { | |
7208 | if (!rq->nr_running) | |
7209 | break; | |
7210 | update_rq_clock(rq); | |
7211 | next = pick_next_task(rq); | |
7212 | if (!next) | |
7213 | break; | |
7214 | next->sched_class->put_prev_task(rq, next); | |
7215 | migrate_dead(dead_cpu, next); | |
7216 | ||
7217 | } | |
7218 | } | |
7219 | ||
7220 | /* | |
7221 | * remove the tasks which were accounted by rq from calc_load_tasks. | |
7222 | */ | |
7223 | static void calc_global_load_remove(struct rq *rq) | |
7224 | { | |
7225 | atomic_long_sub(rq->calc_load_active, &calc_load_tasks); | |
7226 | } | |
7227 | #endif /* CONFIG_HOTPLUG_CPU */ | |
7228 | ||
7229 | #if defined(CONFIG_SCHED_DEBUG) && defined(CONFIG_SYSCTL) | |
7230 | ||
7231 | static struct ctl_table sd_ctl_dir[] = { | |
7232 | { | |
7233 | .procname = "sched_domain", | |
7234 | .mode = 0555, | |
7235 | }, | |
7236 | {0, }, | |
7237 | }; | |
7238 | ||
7239 | static struct ctl_table sd_ctl_root[] = { | |
7240 | { | |
7241 | .ctl_name = CTL_KERN, | |
7242 | .procname = "kernel", | |
7243 | .mode = 0555, | |
7244 | .child = sd_ctl_dir, | |
7245 | }, | |
7246 | {0, }, | |
7247 | }; | |
7248 | ||
7249 | static struct ctl_table *sd_alloc_ctl_entry(int n) | |
7250 | { | |
7251 | struct ctl_table *entry = | |
7252 | kcalloc(n, sizeof(struct ctl_table), GFP_KERNEL); | |
7253 | ||
7254 | return entry; | |
7255 | } | |
7256 | ||
7257 | static void sd_free_ctl_entry(struct ctl_table **tablep) | |
7258 | { | |
7259 | struct ctl_table *entry; | |
7260 | ||
7261 | /* | |
7262 | * In the intermediate directories, both the child directory and | |
7263 | * procname are dynamically allocated and could fail but the mode | |
7264 | * will always be set. In the lowest directory the names are | |
7265 | * static strings and all have proc handlers. | |
7266 | */ | |
7267 | for (entry = *tablep; entry->mode; entry++) { | |
7268 | if (entry->child) | |
7269 | sd_free_ctl_entry(&entry->child); | |
7270 | if (entry->proc_handler == NULL) | |
7271 | kfree(entry->procname); | |
7272 | } | |
7273 | ||
7274 | kfree(*tablep); | |
7275 | *tablep = NULL; | |
7276 | } | |
7277 | ||
7278 | static void | |
7279 | set_table_entry(struct ctl_table *entry, | |
7280 | const char *procname, void *data, int maxlen, | |
7281 | mode_t mode, proc_handler *proc_handler) | |
7282 | { | |
7283 | entry->procname = procname; | |
7284 | entry->data = data; | |
7285 | entry->maxlen = maxlen; | |
7286 | entry->mode = mode; | |
7287 | entry->proc_handler = proc_handler; | |
7288 | } | |
7289 | ||
7290 | static struct ctl_table * | |
7291 | sd_alloc_ctl_domain_table(struct sched_domain *sd) | |
7292 | { | |
7293 | struct ctl_table *table = sd_alloc_ctl_entry(13); | |
7294 | ||
7295 | if (table == NULL) | |
7296 | return NULL; | |
7297 | ||
7298 | set_table_entry(&table[0], "min_interval", &sd->min_interval, | |
7299 | sizeof(long), 0644, proc_doulongvec_minmax); | |
7300 | set_table_entry(&table[1], "max_interval", &sd->max_interval, | |
7301 | sizeof(long), 0644, proc_doulongvec_minmax); | |
7302 | set_table_entry(&table[2], "busy_idx", &sd->busy_idx, | |
7303 | sizeof(int), 0644, proc_dointvec_minmax); | |
7304 | set_table_entry(&table[3], "idle_idx", &sd->idle_idx, | |
7305 | sizeof(int), 0644, proc_dointvec_minmax); | |
7306 | set_table_entry(&table[4], "newidle_idx", &sd->newidle_idx, | |
7307 | sizeof(int), 0644, proc_dointvec_minmax); | |
7308 | set_table_entry(&table[5], "wake_idx", &sd->wake_idx, | |
7309 | sizeof(int), 0644, proc_dointvec_minmax); | |
7310 | set_table_entry(&table[6], "forkexec_idx", &sd->forkexec_idx, | |
7311 | sizeof(int), 0644, proc_dointvec_minmax); | |
7312 | set_table_entry(&table[7], "busy_factor", &sd->busy_factor, | |
7313 | sizeof(int), 0644, proc_dointvec_minmax); | |
7314 | set_table_entry(&table[8], "imbalance_pct", &sd->imbalance_pct, | |
7315 | sizeof(int), 0644, proc_dointvec_minmax); | |
7316 | set_table_entry(&table[9], "cache_nice_tries", | |
7317 | &sd->cache_nice_tries, | |
7318 | sizeof(int), 0644, proc_dointvec_minmax); | |
7319 | set_table_entry(&table[10], "flags", &sd->flags, | |
7320 | sizeof(int), 0644, proc_dointvec_minmax); | |
7321 | set_table_entry(&table[11], "name", sd->name, | |
7322 | CORENAME_MAX_SIZE, 0444, proc_dostring); | |
7323 | /* &table[12] is terminator */ | |
7324 | ||
7325 | return table; | |
7326 | } | |
7327 | ||
7328 | static ctl_table *sd_alloc_ctl_cpu_table(int cpu) | |
7329 | { | |
7330 | struct ctl_table *entry, *table; | |
7331 | struct sched_domain *sd; | |
7332 | int domain_num = 0, i; | |
7333 | char buf[32]; | |
7334 | ||
7335 | for_each_domain(cpu, sd) | |
7336 | domain_num++; | |
7337 | entry = table = sd_alloc_ctl_entry(domain_num + 1); | |
7338 | if (table == NULL) | |
7339 | return NULL; | |
7340 | ||
7341 | i = 0; | |
7342 | for_each_domain(cpu, sd) { | |
7343 | snprintf(buf, 32, "domain%d", i); | |
7344 | entry->procname = kstrdup(buf, GFP_KERNEL); | |
7345 | entry->mode = 0555; | |
7346 | entry->child = sd_alloc_ctl_domain_table(sd); | |
7347 | entry++; | |
7348 | i++; | |
7349 | } | |
7350 | return table; | |
7351 | } | |
7352 | ||
7353 | static struct ctl_table_header *sd_sysctl_header; | |
7354 | static void register_sched_domain_sysctl(void) | |
7355 | { | |
7356 | int i, cpu_num = num_online_cpus(); | |
7357 | struct ctl_table *entry = sd_alloc_ctl_entry(cpu_num + 1); | |
7358 | char buf[32]; | |
7359 | ||
7360 | WARN_ON(sd_ctl_dir[0].child); | |
7361 | sd_ctl_dir[0].child = entry; | |
7362 | ||
7363 | if (entry == NULL) | |
7364 | return; | |
7365 | ||
7366 | for_each_online_cpu(i) { | |
7367 | snprintf(buf, 32, "cpu%d", i); | |
7368 | entry->procname = kstrdup(buf, GFP_KERNEL); | |
7369 | entry->mode = 0555; | |
7370 | entry->child = sd_alloc_ctl_cpu_table(i); | |
7371 | entry++; | |
7372 | } | |
7373 | ||
7374 | WARN_ON(sd_sysctl_header); | |
7375 | sd_sysctl_header = register_sysctl_table(sd_ctl_root); | |
7376 | } | |
7377 | ||
7378 | /* may be called multiple times per register */ | |
7379 | static void unregister_sched_domain_sysctl(void) | |
7380 | { | |
7381 | if (sd_sysctl_header) | |
7382 | unregister_sysctl_table(sd_sysctl_header); | |
7383 | sd_sysctl_header = NULL; | |
7384 | if (sd_ctl_dir[0].child) | |
7385 | sd_free_ctl_entry(&sd_ctl_dir[0].child); | |
7386 | } | |
7387 | #else | |
7388 | static void register_sched_domain_sysctl(void) | |
7389 | { | |
7390 | } | |
7391 | static void unregister_sched_domain_sysctl(void) | |
7392 | { | |
7393 | } | |
7394 | #endif | |
7395 | ||
7396 | static void set_rq_online(struct rq *rq) | |
7397 | { | |
7398 | if (!rq->online) { | |
7399 | const struct sched_class *class; | |
7400 | ||
7401 | cpumask_set_cpu(rq->cpu, rq->rd->online); | |
7402 | rq->online = 1; | |
7403 | ||
7404 | for_each_class(class) { | |
7405 | if (class->rq_online) | |
7406 | class->rq_online(rq); | |
7407 | } | |
7408 | } | |
7409 | } | |
7410 | ||
7411 | static void set_rq_offline(struct rq *rq) | |
7412 | { | |
7413 | if (rq->online) { | |
7414 | const struct sched_class *class; | |
7415 | ||
7416 | for_each_class(class) { | |
7417 | if (class->rq_offline) | |
7418 | class->rq_offline(rq); | |
7419 | } | |
7420 | ||
7421 | cpumask_clear_cpu(rq->cpu, rq->rd->online); | |
7422 | rq->online = 0; | |
7423 | } | |
7424 | } | |
7425 | ||
7426 | /* | |
7427 | * migration_call - callback that gets triggered when a CPU is added. | |
7428 | * Here we can start up the necessary migration thread for the new CPU. | |
7429 | */ | |
7430 | static int __cpuinit | |
7431 | migration_call(struct notifier_block *nfb, unsigned long action, void *hcpu) | |
7432 | { | |
7433 | struct task_struct *p; | |
7434 | int cpu = (long)hcpu; | |
7435 | unsigned long flags; | |
7436 | struct rq *rq; | |
7437 | ||
7438 | switch (action) { | |
7439 | ||
7440 | case CPU_UP_PREPARE: | |
7441 | case CPU_UP_PREPARE_FROZEN: | |
7442 | p = kthread_create(migration_thread, hcpu, "migration/%d", cpu); | |
7443 | if (IS_ERR(p)) | |
7444 | return NOTIFY_BAD; | |
7445 | kthread_bind(p, cpu); | |
7446 | /* Must be high prio: stop_machine expects to yield to it. */ | |
7447 | rq = task_rq_lock(p, &flags); | |
7448 | __setscheduler(rq, p, SCHED_FIFO, MAX_RT_PRIO-1); | |
7449 | task_rq_unlock(rq, &flags); | |
7450 | cpu_rq(cpu)->migration_thread = p; | |
7451 | break; | |
7452 | ||
7453 | case CPU_ONLINE: | |
7454 | case CPU_ONLINE_FROZEN: | |
7455 | /* Strictly unnecessary, as first user will wake it. */ | |
7456 | wake_up_process(cpu_rq(cpu)->migration_thread); | |
7457 | ||
7458 | /* Update our root-domain */ | |
7459 | rq = cpu_rq(cpu); | |
7460 | spin_lock_irqsave(&rq->lock, flags); | |
7461 | rq->calc_load_update = calc_load_update; | |
7462 | rq->calc_load_active = 0; | |
7463 | if (rq->rd) { | |
7464 | BUG_ON(!cpumask_test_cpu(cpu, rq->rd->span)); | |
7465 | ||
7466 | set_rq_online(rq); | |
7467 | } | |
7468 | spin_unlock_irqrestore(&rq->lock, flags); | |
7469 | break; | |
7470 | ||
7471 | #ifdef CONFIG_HOTPLUG_CPU | |
7472 | case CPU_UP_CANCELED: | |
7473 | case CPU_UP_CANCELED_FROZEN: | |
7474 | if (!cpu_rq(cpu)->migration_thread) | |
7475 | break; | |
7476 | /* Unbind it from offline cpu so it can run. Fall thru. */ | |
7477 | kthread_bind(cpu_rq(cpu)->migration_thread, | |
7478 | cpumask_any(cpu_online_mask)); | |
7479 | kthread_stop(cpu_rq(cpu)->migration_thread); | |
7480 | cpu_rq(cpu)->migration_thread = NULL; | |
7481 | break; | |
7482 | ||
7483 | case CPU_DEAD: | |
7484 | case CPU_DEAD_FROZEN: | |
7485 | cpuset_lock(); /* around calls to cpuset_cpus_allowed_lock() */ | |
7486 | migrate_live_tasks(cpu); | |
7487 | rq = cpu_rq(cpu); | |
7488 | kthread_stop(rq->migration_thread); | |
7489 | rq->migration_thread = NULL; | |
7490 | /* Idle task back to normal (off runqueue, low prio) */ | |
7491 | spin_lock_irq(&rq->lock); | |
7492 | update_rq_clock(rq); | |
7493 | deactivate_task(rq, rq->idle, 0); | |
7494 | rq->idle->static_prio = MAX_PRIO; | |
7495 | __setscheduler(rq, rq->idle, SCHED_NORMAL, 0); | |
7496 | rq->idle->sched_class = &idle_sched_class; | |
7497 | migrate_dead_tasks(cpu); | |
7498 | spin_unlock_irq(&rq->lock); | |
7499 | cpuset_unlock(); | |
7500 | migrate_nr_uninterruptible(rq); | |
7501 | BUG_ON(rq->nr_running != 0); | |
7502 | calc_global_load_remove(rq); | |
7503 | /* | |
7504 | * No need to migrate the tasks: it was best-effort if | |
7505 | * they didn't take sched_hotcpu_mutex. Just wake up | |
7506 | * the requestors. | |
7507 | */ | |
7508 | spin_lock_irq(&rq->lock); | |
7509 | while (!list_empty(&rq->migration_queue)) { | |
7510 | struct migration_req *req; | |
7511 | ||
7512 | req = list_entry(rq->migration_queue.next, | |
7513 | struct migration_req, list); | |
7514 | list_del_init(&req->list); | |
7515 | spin_unlock_irq(&rq->lock); | |
7516 | complete(&req->done); | |
7517 | spin_lock_irq(&rq->lock); | |
7518 | } | |
7519 | spin_unlock_irq(&rq->lock); | |
7520 | break; | |
7521 | ||
7522 | case CPU_DYING: | |
7523 | case CPU_DYING_FROZEN: | |
7524 | /* Update our root-domain */ | |
7525 | rq = cpu_rq(cpu); | |
7526 | spin_lock_irqsave(&rq->lock, flags); | |
7527 | if (rq->rd) { | |
7528 | BUG_ON(!cpumask_test_cpu(cpu, rq->rd->span)); | |
7529 | set_rq_offline(rq); | |
7530 | } | |
7531 | spin_unlock_irqrestore(&rq->lock, flags); | |
7532 | break; | |
7533 | #endif | |
7534 | } | |
7535 | return NOTIFY_OK; | |
7536 | } | |
7537 | ||
7538 | /* Register at highest priority so that task migration (migrate_all_tasks) | |
7539 | * happens before everything else. | |
7540 | */ | |
7541 | static struct notifier_block __cpuinitdata migration_notifier = { | |
7542 | .notifier_call = migration_call, | |
7543 | .priority = 10 | |
7544 | }; | |
7545 | ||
7546 | static int __init migration_init(void) | |
7547 | { | |
7548 | void *cpu = (void *)(long)smp_processor_id(); | |
7549 | int err; | |
7550 | ||
7551 | /* Start one for the boot CPU: */ | |
7552 | err = migration_call(&migration_notifier, CPU_UP_PREPARE, cpu); | |
7553 | BUG_ON(err == NOTIFY_BAD); | |
7554 | migration_call(&migration_notifier, CPU_ONLINE, cpu); | |
7555 | register_cpu_notifier(&migration_notifier); | |
7556 | ||
7557 | return err; | |
7558 | } | |
7559 | early_initcall(migration_init); | |
7560 | #endif | |
7561 | ||
7562 | #ifdef CONFIG_SMP | |
7563 | ||
7564 | #ifdef CONFIG_SCHED_DEBUG | |
7565 | ||
7566 | static int sched_domain_debug_one(struct sched_domain *sd, int cpu, int level, | |
7567 | struct cpumask *groupmask) | |
7568 | { | |
7569 | struct sched_group *group = sd->groups; | |
7570 | char str[256]; | |
7571 | ||
7572 | cpulist_scnprintf(str, sizeof(str), sched_domain_span(sd)); | |
7573 | cpumask_clear(groupmask); | |
7574 | ||
7575 | printk(KERN_DEBUG "%*s domain %d: ", level, "", level); | |
7576 | ||
7577 | if (!(sd->flags & SD_LOAD_BALANCE)) { | |
7578 | printk("does not load-balance\n"); | |
7579 | if (sd->parent) | |
7580 | printk(KERN_ERR "ERROR: !SD_LOAD_BALANCE domain" | |
7581 | " has parent"); | |
7582 | return -1; | |
7583 | } | |
7584 | ||
7585 | printk(KERN_CONT "span %s level %s\n", str, sd->name); | |
7586 | ||
7587 | if (!cpumask_test_cpu(cpu, sched_domain_span(sd))) { | |
7588 | printk(KERN_ERR "ERROR: domain->span does not contain " | |
7589 | "CPU%d\n", cpu); | |
7590 | } | |
7591 | if (!cpumask_test_cpu(cpu, sched_group_cpus(group))) { | |
7592 | printk(KERN_ERR "ERROR: domain->groups does not contain" | |
7593 | " CPU%d\n", cpu); | |
7594 | } | |
7595 | ||
7596 | printk(KERN_DEBUG "%*s groups:", level + 1, ""); | |
7597 | do { | |
7598 | if (!group) { | |
7599 | printk("\n"); | |
7600 | printk(KERN_ERR "ERROR: group is NULL\n"); | |
7601 | break; | |
7602 | } | |
7603 | ||
7604 | if (!group->__cpu_power) { | |
7605 | printk(KERN_CONT "\n"); | |
7606 | printk(KERN_ERR "ERROR: domain->cpu_power not " | |
7607 | "set\n"); | |
7608 | break; | |
7609 | } | |
7610 | ||
7611 | if (!cpumask_weight(sched_group_cpus(group))) { | |
7612 | printk(KERN_CONT "\n"); | |
7613 | printk(KERN_ERR "ERROR: empty group\n"); | |
7614 | break; | |
7615 | } | |
7616 | ||
7617 | if (cpumask_intersects(groupmask, sched_group_cpus(group))) { | |
7618 | printk(KERN_CONT "\n"); | |
7619 | printk(KERN_ERR "ERROR: repeated CPUs\n"); | |
7620 | break; | |
7621 | } | |
7622 | ||
7623 | cpumask_or(groupmask, groupmask, sched_group_cpus(group)); | |
7624 | ||
7625 | cpulist_scnprintf(str, sizeof(str), sched_group_cpus(group)); | |
7626 | ||
7627 | printk(KERN_CONT " %s", str); | |
7628 | if (group->__cpu_power != SCHED_LOAD_SCALE) { | |
7629 | printk(KERN_CONT " (__cpu_power = %d)", | |
7630 | group->__cpu_power); | |
7631 | } | |
7632 | ||
7633 | group = group->next; | |
7634 | } while (group != sd->groups); | |
7635 | printk(KERN_CONT "\n"); | |
7636 | ||
7637 | if (!cpumask_equal(sched_domain_span(sd), groupmask)) | |
7638 | printk(KERN_ERR "ERROR: groups don't span domain->span\n"); | |
7639 | ||
7640 | if (sd->parent && | |
7641 | !cpumask_subset(groupmask, sched_domain_span(sd->parent))) | |
7642 | printk(KERN_ERR "ERROR: parent span is not a superset " | |
7643 | "of domain->span\n"); | |
7644 | return 0; | |
7645 | } | |
7646 | ||
7647 | static void sched_domain_debug(struct sched_domain *sd, int cpu) | |
7648 | { | |
7649 | cpumask_var_t groupmask; | |
7650 | int level = 0; | |
7651 | ||
7652 | if (!sd) { | |
7653 | printk(KERN_DEBUG "CPU%d attaching NULL sched-domain.\n", cpu); | |
7654 | return; | |
7655 | } | |
7656 | ||
7657 | printk(KERN_DEBUG "CPU%d attaching sched-domain:\n", cpu); | |
7658 | ||
7659 | if (!alloc_cpumask_var(&groupmask, GFP_KERNEL)) { | |
7660 | printk(KERN_DEBUG "Cannot load-balance (out of memory)\n"); | |
7661 | return; | |
7662 | } | |
7663 | ||
7664 | for (;;) { | |
7665 | if (sched_domain_debug_one(sd, cpu, level, groupmask)) | |
7666 | break; | |
7667 | level++; | |
7668 | sd = sd->parent; | |
7669 | if (!sd) | |
7670 | break; | |
7671 | } | |
7672 | free_cpumask_var(groupmask); | |
7673 | } | |
7674 | #else /* !CONFIG_SCHED_DEBUG */ | |
7675 | # define sched_domain_debug(sd, cpu) do { } while (0) | |
7676 | #endif /* CONFIG_SCHED_DEBUG */ | |
7677 | ||
7678 | static int sd_degenerate(struct sched_domain *sd) | |
7679 | { | |
7680 | if (cpumask_weight(sched_domain_span(sd)) == 1) | |
7681 | return 1; | |
7682 | ||
7683 | /* Following flags need at least 2 groups */ | |
7684 | if (sd->flags & (SD_LOAD_BALANCE | | |
7685 | SD_BALANCE_NEWIDLE | | |
7686 | SD_BALANCE_FORK | | |
7687 | SD_BALANCE_EXEC | | |
7688 | SD_SHARE_CPUPOWER | | |
7689 | SD_SHARE_PKG_RESOURCES)) { | |
7690 | if (sd->groups != sd->groups->next) | |
7691 | return 0; | |
7692 | } | |
7693 | ||
7694 | /* Following flags don't use groups */ | |
7695 | if (sd->flags & (SD_WAKE_IDLE | | |
7696 | SD_WAKE_AFFINE | | |
7697 | SD_WAKE_BALANCE)) | |
7698 | return 0; | |
7699 | ||
7700 | return 1; | |
7701 | } | |
7702 | ||
7703 | static int | |
7704 | sd_parent_degenerate(struct sched_domain *sd, struct sched_domain *parent) | |
7705 | { | |
7706 | unsigned long cflags = sd->flags, pflags = parent->flags; | |
7707 | ||
7708 | if (sd_degenerate(parent)) | |
7709 | return 1; | |
7710 | ||
7711 | if (!cpumask_equal(sched_domain_span(sd), sched_domain_span(parent))) | |
7712 | return 0; | |
7713 | ||
7714 | /* Does parent contain flags not in child? */ | |
7715 | /* WAKE_BALANCE is a subset of WAKE_AFFINE */ | |
7716 | if (cflags & SD_WAKE_AFFINE) | |
7717 | pflags &= ~SD_WAKE_BALANCE; | |
7718 | /* Flags needing groups don't count if only 1 group in parent */ | |
7719 | if (parent->groups == parent->groups->next) { | |
7720 | pflags &= ~(SD_LOAD_BALANCE | | |
7721 | SD_BALANCE_NEWIDLE | | |
7722 | SD_BALANCE_FORK | | |
7723 | SD_BALANCE_EXEC | | |
7724 | SD_SHARE_CPUPOWER | | |
7725 | SD_SHARE_PKG_RESOURCES); | |
7726 | if (nr_node_ids == 1) | |
7727 | pflags &= ~SD_SERIALIZE; | |
7728 | } | |
7729 | if (~cflags & pflags) | |
7730 | return 0; | |
7731 | ||
7732 | return 1; | |
7733 | } | |
7734 | ||
7735 | static void free_rootdomain(struct root_domain *rd) | |
7736 | { | |
7737 | cpupri_cleanup(&rd->cpupri); | |
7738 | ||
7739 | free_cpumask_var(rd->rto_mask); | |
7740 | free_cpumask_var(rd->online); | |
7741 | free_cpumask_var(rd->span); | |
7742 | kfree(rd); | |
7743 | } | |
7744 | ||
7745 | static void rq_attach_root(struct rq *rq, struct root_domain *rd) | |
7746 | { | |
7747 | struct root_domain *old_rd = NULL; | |
7748 | unsigned long flags; | |
7749 | ||
7750 | spin_lock_irqsave(&rq->lock, flags); | |
7751 | ||
7752 | if (rq->rd) { | |
7753 | old_rd = rq->rd; | |
7754 | ||
7755 | if (cpumask_test_cpu(rq->cpu, old_rd->online)) | |
7756 | set_rq_offline(rq); | |
7757 | ||
7758 | cpumask_clear_cpu(rq->cpu, old_rd->span); | |
7759 | ||
7760 | /* | |
7761 | * If we dont want to free the old_rt yet then | |
7762 | * set old_rd to NULL to skip the freeing later | |
7763 | * in this function: | |
7764 | */ | |
7765 | if (!atomic_dec_and_test(&old_rd->refcount)) | |
7766 | old_rd = NULL; | |
7767 | } | |
7768 | ||
7769 | atomic_inc(&rd->refcount); | |
7770 | rq->rd = rd; | |
7771 | ||
7772 | cpumask_set_cpu(rq->cpu, rd->span); | |
7773 | if (cpumask_test_cpu(rq->cpu, cpu_online_mask)) | |
7774 | set_rq_online(rq); | |
7775 | ||
7776 | spin_unlock_irqrestore(&rq->lock, flags); | |
7777 | ||
7778 | if (old_rd) | |
7779 | free_rootdomain(old_rd); | |
7780 | } | |
7781 | ||
7782 | static int __init_refok init_rootdomain(struct root_domain *rd, bool bootmem) | |
7783 | { | |
7784 | gfp_t gfp = GFP_KERNEL; | |
7785 | ||
7786 | memset(rd, 0, sizeof(*rd)); | |
7787 | ||
7788 | if (bootmem) | |
7789 | gfp = GFP_NOWAIT; | |
7790 | ||
7791 | if (!alloc_cpumask_var(&rd->span, gfp)) | |
7792 | goto out; | |
7793 | if (!alloc_cpumask_var(&rd->online, gfp)) | |
7794 | goto free_span; | |
7795 | if (!alloc_cpumask_var(&rd->rto_mask, gfp)) | |
7796 | goto free_online; | |
7797 | ||
7798 | if (cpupri_init(&rd->cpupri, false) != 0) | |
7799 | goto free_rto_mask; | |
7800 | return 0; | |
7801 | ||
7802 | free_rto_mask: | |
7803 | free_cpumask_var(rd->rto_mask); | |
7804 | free_online: | |
7805 | free_cpumask_var(rd->online); | |
7806 | free_span: | |
7807 | free_cpumask_var(rd->span); | |
7808 | out: | |
7809 | return -ENOMEM; | |
7810 | } | |
7811 | ||
7812 | static void init_defrootdomain(void) | |
7813 | { | |
7814 | init_rootdomain(&def_root_domain, true); | |
7815 | ||
7816 | atomic_set(&def_root_domain.refcount, 1); | |
7817 | } | |
7818 | ||
7819 | static struct root_domain *alloc_rootdomain(void) | |
7820 | { | |
7821 | struct root_domain *rd; | |
7822 | ||
7823 | rd = kmalloc(sizeof(*rd), GFP_KERNEL); | |
7824 | if (!rd) | |
7825 | return NULL; | |
7826 | ||
7827 | if (init_rootdomain(rd, false) != 0) { | |
7828 | kfree(rd); | |
7829 | return NULL; | |
7830 | } | |
7831 | ||
7832 | return rd; | |
7833 | } | |
7834 | ||
7835 | /* | |
7836 | * Attach the domain 'sd' to 'cpu' as its base domain. Callers must | |
7837 | * hold the hotplug lock. | |
7838 | */ | |
7839 | static void | |
7840 | cpu_attach_domain(struct sched_domain *sd, struct root_domain *rd, int cpu) | |
7841 | { | |
7842 | struct rq *rq = cpu_rq(cpu); | |
7843 | struct sched_domain *tmp; | |
7844 | ||
7845 | /* Remove the sched domains which do not contribute to scheduling. */ | |
7846 | for (tmp = sd; tmp; ) { | |
7847 | struct sched_domain *parent = tmp->parent; | |
7848 | if (!parent) | |
7849 | break; | |
7850 | ||
7851 | if (sd_parent_degenerate(tmp, parent)) { | |
7852 | tmp->parent = parent->parent; | |
7853 | if (parent->parent) | |
7854 | parent->parent->child = tmp; | |
7855 | } else | |
7856 | tmp = tmp->parent; | |
7857 | } | |
7858 | ||
7859 | if (sd && sd_degenerate(sd)) { | |
7860 | sd = sd->parent; | |
7861 | if (sd) | |
7862 | sd->child = NULL; | |
7863 | } | |
7864 | ||
7865 | sched_domain_debug(sd, cpu); | |
7866 | ||
7867 | rq_attach_root(rq, rd); | |
7868 | rcu_assign_pointer(rq->sd, sd); | |
7869 | } | |
7870 | ||
7871 | /* cpus with isolated domains */ | |
7872 | static cpumask_var_t cpu_isolated_map; | |
7873 | ||
7874 | /* Setup the mask of cpus configured for isolated domains */ | |
7875 | static int __init isolated_cpu_setup(char *str) | |
7876 | { | |
7877 | cpulist_parse(str, cpu_isolated_map); | |
7878 | return 1; | |
7879 | } | |
7880 | ||
7881 | __setup("isolcpus=", isolated_cpu_setup); | |
7882 | ||
7883 | /* | |
7884 | * init_sched_build_groups takes the cpumask we wish to span, and a pointer | |
7885 | * to a function which identifies what group(along with sched group) a CPU | |
7886 | * belongs to. The return value of group_fn must be a >= 0 and < nr_cpu_ids | |
7887 | * (due to the fact that we keep track of groups covered with a struct cpumask). | |
7888 | * | |
7889 | * init_sched_build_groups will build a circular linked list of the groups | |
7890 | * covered by the given span, and will set each group's ->cpumask correctly, | |
7891 | * and ->cpu_power to 0. | |
7892 | */ | |
7893 | static void | |
7894 | init_sched_build_groups(const struct cpumask *span, | |
7895 | const struct cpumask *cpu_map, | |
7896 | int (*group_fn)(int cpu, const struct cpumask *cpu_map, | |
7897 | struct sched_group **sg, | |
7898 | struct cpumask *tmpmask), | |
7899 | struct cpumask *covered, struct cpumask *tmpmask) | |
7900 | { | |
7901 | struct sched_group *first = NULL, *last = NULL; | |
7902 | int i; | |
7903 | ||
7904 | cpumask_clear(covered); | |
7905 | ||
7906 | for_each_cpu(i, span) { | |
7907 | struct sched_group *sg; | |
7908 | int group = group_fn(i, cpu_map, &sg, tmpmask); | |
7909 | int j; | |
7910 | ||
7911 | if (cpumask_test_cpu(i, covered)) | |
7912 | continue; | |
7913 | ||
7914 | cpumask_clear(sched_group_cpus(sg)); | |
7915 | sg->__cpu_power = 0; | |
7916 | ||
7917 | for_each_cpu(j, span) { | |
7918 | if (group_fn(j, cpu_map, NULL, tmpmask) != group) | |
7919 | continue; | |
7920 | ||
7921 | cpumask_set_cpu(j, covered); | |
7922 | cpumask_set_cpu(j, sched_group_cpus(sg)); | |
7923 | } | |
7924 | if (!first) | |
7925 | first = sg; | |
7926 | if (last) | |
7927 | last->next = sg; | |
7928 | last = sg; | |
7929 | } | |
7930 | last->next = first; | |
7931 | } | |
7932 | ||
7933 | #define SD_NODES_PER_DOMAIN 16 | |
7934 | ||
7935 | #ifdef CONFIG_NUMA | |
7936 | ||
7937 | /** | |
7938 | * find_next_best_node - find the next node to include in a sched_domain | |
7939 | * @node: node whose sched_domain we're building | |
7940 | * @used_nodes: nodes already in the sched_domain | |
7941 | * | |
7942 | * Find the next node to include in a given scheduling domain. Simply | |
7943 | * finds the closest node not already in the @used_nodes map. | |
7944 | * | |
7945 | * Should use nodemask_t. | |
7946 | */ | |
7947 | static int find_next_best_node(int node, nodemask_t *used_nodes) | |
7948 | { | |
7949 | int i, n, val, min_val, best_node = 0; | |
7950 | ||
7951 | min_val = INT_MAX; | |
7952 | ||
7953 | for (i = 0; i < nr_node_ids; i++) { | |
7954 | /* Start at @node */ | |
7955 | n = (node + i) % nr_node_ids; | |
7956 | ||
7957 | if (!nr_cpus_node(n)) | |
7958 | continue; | |
7959 | ||
7960 | /* Skip already used nodes */ | |
7961 | if (node_isset(n, *used_nodes)) | |
7962 | continue; | |
7963 | ||
7964 | /* Simple min distance search */ | |
7965 | val = node_distance(node, n); | |
7966 | ||
7967 | if (val < min_val) { | |
7968 | min_val = val; | |
7969 | best_node = n; | |
7970 | } | |
7971 | } | |
7972 | ||
7973 | node_set(best_node, *used_nodes); | |
7974 | return best_node; | |
7975 | } | |
7976 | ||
7977 | /** | |
7978 | * sched_domain_node_span - get a cpumask for a node's sched_domain | |
7979 | * @node: node whose cpumask we're constructing | |
7980 | * @span: resulting cpumask | |
7981 | * | |
7982 | * Given a node, construct a good cpumask for its sched_domain to span. It | |
7983 | * should be one that prevents unnecessary balancing, but also spreads tasks | |
7984 | * out optimally. | |
7985 | */ | |
7986 | static void sched_domain_node_span(int node, struct cpumask *span) | |
7987 | { | |
7988 | nodemask_t used_nodes; | |
7989 | int i; | |
7990 | ||
7991 | cpumask_clear(span); | |
7992 | nodes_clear(used_nodes); | |
7993 | ||
7994 | cpumask_or(span, span, cpumask_of_node(node)); | |
7995 | node_set(node, used_nodes); | |
7996 | ||
7997 | for (i = 1; i < SD_NODES_PER_DOMAIN; i++) { | |
7998 | int next_node = find_next_best_node(node, &used_nodes); | |
7999 | ||
8000 | cpumask_or(span, span, cpumask_of_node(next_node)); | |
8001 | } | |
8002 | } | |
8003 | #endif /* CONFIG_NUMA */ | |
8004 | ||
8005 | int sched_smt_power_savings = 0, sched_mc_power_savings = 0; | |
8006 | ||
8007 | /* | |
8008 | * The cpus mask in sched_group and sched_domain hangs off the end. | |
8009 | * | |
8010 | * ( See the the comments in include/linux/sched.h:struct sched_group | |
8011 | * and struct sched_domain. ) | |
8012 | */ | |
8013 | struct static_sched_group { | |
8014 | struct sched_group sg; | |
8015 | DECLARE_BITMAP(cpus, CONFIG_NR_CPUS); | |
8016 | }; | |
8017 | ||
8018 | struct static_sched_domain { | |
8019 | struct sched_domain sd; | |
8020 | DECLARE_BITMAP(span, CONFIG_NR_CPUS); | |
8021 | }; | |
8022 | ||
8023 | /* | |
8024 | * SMT sched-domains: | |
8025 | */ | |
8026 | #ifdef CONFIG_SCHED_SMT | |
8027 | static DEFINE_PER_CPU(struct static_sched_domain, cpu_domains); | |
8028 | static DEFINE_PER_CPU(struct static_sched_group, sched_group_cpus); | |
8029 | ||
8030 | static int | |
8031 | cpu_to_cpu_group(int cpu, const struct cpumask *cpu_map, | |
8032 | struct sched_group **sg, struct cpumask *unused) | |
8033 | { | |
8034 | if (sg) | |
8035 | *sg = &per_cpu(sched_group_cpus, cpu).sg; | |
8036 | return cpu; | |
8037 | } | |
8038 | #endif /* CONFIG_SCHED_SMT */ | |
8039 | ||
8040 | /* | |
8041 | * multi-core sched-domains: | |
8042 | */ | |
8043 | #ifdef CONFIG_SCHED_MC | |
8044 | static DEFINE_PER_CPU(struct static_sched_domain, core_domains); | |
8045 | static DEFINE_PER_CPU(struct static_sched_group, sched_group_core); | |
8046 | #endif /* CONFIG_SCHED_MC */ | |
8047 | ||
8048 | #if defined(CONFIG_SCHED_MC) && defined(CONFIG_SCHED_SMT) | |
8049 | static int | |
8050 | cpu_to_core_group(int cpu, const struct cpumask *cpu_map, | |
8051 | struct sched_group **sg, struct cpumask *mask) | |
8052 | { | |
8053 | int group; | |
8054 | ||
8055 | cpumask_and(mask, topology_thread_cpumask(cpu), cpu_map); | |
8056 | group = cpumask_first(mask); | |
8057 | if (sg) | |
8058 | *sg = &per_cpu(sched_group_core, group).sg; | |
8059 | return group; | |
8060 | } | |
8061 | #elif defined(CONFIG_SCHED_MC) | |
8062 | static int | |
8063 | cpu_to_core_group(int cpu, const struct cpumask *cpu_map, | |
8064 | struct sched_group **sg, struct cpumask *unused) | |
8065 | { | |
8066 | if (sg) | |
8067 | *sg = &per_cpu(sched_group_core, cpu).sg; | |
8068 | return cpu; | |
8069 | } | |
8070 | #endif | |
8071 | ||
8072 | static DEFINE_PER_CPU(struct static_sched_domain, phys_domains); | |
8073 | static DEFINE_PER_CPU(struct static_sched_group, sched_group_phys); | |
8074 | ||
8075 | static int | |
8076 | cpu_to_phys_group(int cpu, const struct cpumask *cpu_map, | |
8077 | struct sched_group **sg, struct cpumask *mask) | |
8078 | { | |
8079 | int group; | |
8080 | #ifdef CONFIG_SCHED_MC | |
8081 | cpumask_and(mask, cpu_coregroup_mask(cpu), cpu_map); | |
8082 | group = cpumask_first(mask); | |
8083 | #elif defined(CONFIG_SCHED_SMT) | |
8084 | cpumask_and(mask, topology_thread_cpumask(cpu), cpu_map); | |
8085 | group = cpumask_first(mask); | |
8086 | #else | |
8087 | group = cpu; | |
8088 | #endif | |
8089 | if (sg) | |
8090 | *sg = &per_cpu(sched_group_phys, group).sg; | |
8091 | return group; | |
8092 | } | |
8093 | ||
8094 | #ifdef CONFIG_NUMA | |
8095 | /* | |
8096 | * The init_sched_build_groups can't handle what we want to do with node | |
8097 | * groups, so roll our own. Now each node has its own list of groups which | |
8098 | * gets dynamically allocated. | |
8099 | */ | |
8100 | static DEFINE_PER_CPU(struct static_sched_domain, node_domains); | |
8101 | static struct sched_group ***sched_group_nodes_bycpu; | |
8102 | ||
8103 | static DEFINE_PER_CPU(struct static_sched_domain, allnodes_domains); | |
8104 | static DEFINE_PER_CPU(struct static_sched_group, sched_group_allnodes); | |
8105 | ||
8106 | static int cpu_to_allnodes_group(int cpu, const struct cpumask *cpu_map, | |
8107 | struct sched_group **sg, | |
8108 | struct cpumask *nodemask) | |
8109 | { | |
8110 | int group; | |
8111 | ||
8112 | cpumask_and(nodemask, cpumask_of_node(cpu_to_node(cpu)), cpu_map); | |
8113 | group = cpumask_first(nodemask); | |
8114 | ||
8115 | if (sg) | |
8116 | *sg = &per_cpu(sched_group_allnodes, group).sg; | |
8117 | return group; | |
8118 | } | |
8119 | ||
8120 | static void init_numa_sched_groups_power(struct sched_group *group_head) | |
8121 | { | |
8122 | struct sched_group *sg = group_head; | |
8123 | int j; | |
8124 | ||
8125 | if (!sg) | |
8126 | return; | |
8127 | do { | |
8128 | for_each_cpu(j, sched_group_cpus(sg)) { | |
8129 | struct sched_domain *sd; | |
8130 | ||
8131 | sd = &per_cpu(phys_domains, j).sd; | |
8132 | if (j != group_first_cpu(sd->groups)) { | |
8133 | /* | |
8134 | * Only add "power" once for each | |
8135 | * physical package. | |
8136 | */ | |
8137 | continue; | |
8138 | } | |
8139 | ||
8140 | sg_inc_cpu_power(sg, sd->groups->__cpu_power); | |
8141 | } | |
8142 | sg = sg->next; | |
8143 | } while (sg != group_head); | |
8144 | } | |
8145 | #endif /* CONFIG_NUMA */ | |
8146 | ||
8147 | #ifdef CONFIG_NUMA | |
8148 | /* Free memory allocated for various sched_group structures */ | |
8149 | static void free_sched_groups(const struct cpumask *cpu_map, | |
8150 | struct cpumask *nodemask) | |
8151 | { | |
8152 | int cpu, i; | |
8153 | ||
8154 | for_each_cpu(cpu, cpu_map) { | |
8155 | struct sched_group **sched_group_nodes | |
8156 | = sched_group_nodes_bycpu[cpu]; | |
8157 | ||
8158 | if (!sched_group_nodes) | |
8159 | continue; | |
8160 | ||
8161 | for (i = 0; i < nr_node_ids; i++) { | |
8162 | struct sched_group *oldsg, *sg = sched_group_nodes[i]; | |
8163 | ||
8164 | cpumask_and(nodemask, cpumask_of_node(i), cpu_map); | |
8165 | if (cpumask_empty(nodemask)) | |
8166 | continue; | |
8167 | ||
8168 | if (sg == NULL) | |
8169 | continue; | |
8170 | sg = sg->next; | |
8171 | next_sg: | |
8172 | oldsg = sg; | |
8173 | sg = sg->next; | |
8174 | kfree(oldsg); | |
8175 | if (oldsg != sched_group_nodes[i]) | |
8176 | goto next_sg; | |
8177 | } | |
8178 | kfree(sched_group_nodes); | |
8179 | sched_group_nodes_bycpu[cpu] = NULL; | |
8180 | } | |
8181 | } | |
8182 | #else /* !CONFIG_NUMA */ | |
8183 | static void free_sched_groups(const struct cpumask *cpu_map, | |
8184 | struct cpumask *nodemask) | |
8185 | { | |
8186 | } | |
8187 | #endif /* CONFIG_NUMA */ | |
8188 | ||
8189 | /* | |
8190 | * Initialize sched groups cpu_power. | |
8191 | * | |
8192 | * cpu_power indicates the capacity of sched group, which is used while | |
8193 | * distributing the load between different sched groups in a sched domain. | |
8194 | * Typically cpu_power for all the groups in a sched domain will be same unless | |
8195 | * there are asymmetries in the topology. If there are asymmetries, group | |
8196 | * having more cpu_power will pickup more load compared to the group having | |
8197 | * less cpu_power. | |
8198 | * | |
8199 | * cpu_power will be a multiple of SCHED_LOAD_SCALE. This multiple represents | |
8200 | * the maximum number of tasks a group can handle in the presence of other idle | |
8201 | * or lightly loaded groups in the same sched domain. | |
8202 | */ | |
8203 | static void init_sched_groups_power(int cpu, struct sched_domain *sd) | |
8204 | { | |
8205 | struct sched_domain *child; | |
8206 | struct sched_group *group; | |
8207 | ||
8208 | WARN_ON(!sd || !sd->groups); | |
8209 | ||
8210 | if (cpu != group_first_cpu(sd->groups)) | |
8211 | return; | |
8212 | ||
8213 | child = sd->child; | |
8214 | ||
8215 | sd->groups->__cpu_power = 0; | |
8216 | ||
8217 | /* | |
8218 | * For perf policy, if the groups in child domain share resources | |
8219 | * (for example cores sharing some portions of the cache hierarchy | |
8220 | * or SMT), then set this domain groups cpu_power such that each group | |
8221 | * can handle only one task, when there are other idle groups in the | |
8222 | * same sched domain. | |
8223 | */ | |
8224 | if (!child || (!(sd->flags & SD_POWERSAVINGS_BALANCE) && | |
8225 | (child->flags & | |
8226 | (SD_SHARE_CPUPOWER | SD_SHARE_PKG_RESOURCES)))) { | |
8227 | sg_inc_cpu_power(sd->groups, SCHED_LOAD_SCALE); | |
8228 | return; | |
8229 | } | |
8230 | ||
8231 | /* | |
8232 | * add cpu_power of each child group to this groups cpu_power | |
8233 | */ | |
8234 | group = child->groups; | |
8235 | do { | |
8236 | sg_inc_cpu_power(sd->groups, group->__cpu_power); | |
8237 | group = group->next; | |
8238 | } while (group != child->groups); | |
8239 | } | |
8240 | ||
8241 | /* | |
8242 | * Initializers for schedule domains | |
8243 | * Non-inlined to reduce accumulated stack pressure in build_sched_domains() | |
8244 | */ | |
8245 | ||
8246 | #ifdef CONFIG_SCHED_DEBUG | |
8247 | # define SD_INIT_NAME(sd, type) sd->name = #type | |
8248 | #else | |
8249 | # define SD_INIT_NAME(sd, type) do { } while (0) | |
8250 | #endif | |
8251 | ||
8252 | #define SD_INIT(sd, type) sd_init_##type(sd) | |
8253 | ||
8254 | #define SD_INIT_FUNC(type) \ | |
8255 | static noinline void sd_init_##type(struct sched_domain *sd) \ | |
8256 | { \ | |
8257 | memset(sd, 0, sizeof(*sd)); \ | |
8258 | *sd = SD_##type##_INIT; \ | |
8259 | sd->level = SD_LV_##type; \ | |
8260 | SD_INIT_NAME(sd, type); \ | |
8261 | } | |
8262 | ||
8263 | SD_INIT_FUNC(CPU) | |
8264 | #ifdef CONFIG_NUMA | |
8265 | SD_INIT_FUNC(ALLNODES) | |
8266 | SD_INIT_FUNC(NODE) | |
8267 | #endif | |
8268 | #ifdef CONFIG_SCHED_SMT | |
8269 | SD_INIT_FUNC(SIBLING) | |
8270 | #endif | |
8271 | #ifdef CONFIG_SCHED_MC | |
8272 | SD_INIT_FUNC(MC) | |
8273 | #endif | |
8274 | ||
8275 | static int default_relax_domain_level = -1; | |
8276 | ||
8277 | static int __init setup_relax_domain_level(char *str) | |
8278 | { | |
8279 | unsigned long val; | |
8280 | ||
8281 | val = simple_strtoul(str, NULL, 0); | |
8282 | if (val < SD_LV_MAX) | |
8283 | default_relax_domain_level = val; | |
8284 | ||
8285 | return 1; | |
8286 | } | |
8287 | __setup("relax_domain_level=", setup_relax_domain_level); | |
8288 | ||
8289 | static void set_domain_attribute(struct sched_domain *sd, | |
8290 | struct sched_domain_attr *attr) | |
8291 | { | |
8292 | int request; | |
8293 | ||
8294 | if (!attr || attr->relax_domain_level < 0) { | |
8295 | if (default_relax_domain_level < 0) | |
8296 | return; | |
8297 | else | |
8298 | request = default_relax_domain_level; | |
8299 | } else | |
8300 | request = attr->relax_domain_level; | |
8301 | if (request < sd->level) { | |
8302 | /* turn off idle balance on this domain */ | |
8303 | sd->flags &= ~(SD_WAKE_IDLE|SD_BALANCE_NEWIDLE); | |
8304 | } else { | |
8305 | /* turn on idle balance on this domain */ | |
8306 | sd->flags |= (SD_WAKE_IDLE_FAR|SD_BALANCE_NEWIDLE); | |
8307 | } | |
8308 | } | |
8309 | ||
8310 | /* | |
8311 | * Build sched domains for a given set of cpus and attach the sched domains | |
8312 | * to the individual cpus | |
8313 | */ | |
8314 | static int __build_sched_domains(const struct cpumask *cpu_map, | |
8315 | struct sched_domain_attr *attr) | |
8316 | { | |
8317 | int i, err = -ENOMEM; | |
8318 | struct root_domain *rd; | |
8319 | cpumask_var_t nodemask, this_sibling_map, this_core_map, send_covered, | |
8320 | tmpmask; | |
8321 | #ifdef CONFIG_NUMA | |
8322 | cpumask_var_t domainspan, covered, notcovered; | |
8323 | struct sched_group **sched_group_nodes = NULL; | |
8324 | int sd_allnodes = 0; | |
8325 | ||
8326 | if (!alloc_cpumask_var(&domainspan, GFP_KERNEL)) | |
8327 | goto out; | |
8328 | if (!alloc_cpumask_var(&covered, GFP_KERNEL)) | |
8329 | goto free_domainspan; | |
8330 | if (!alloc_cpumask_var(¬covered, GFP_KERNEL)) | |
8331 | goto free_covered; | |
8332 | #endif | |
8333 | ||
8334 | if (!alloc_cpumask_var(&nodemask, GFP_KERNEL)) | |
8335 | goto free_notcovered; | |
8336 | if (!alloc_cpumask_var(&this_sibling_map, GFP_KERNEL)) | |
8337 | goto free_nodemask; | |
8338 | if (!alloc_cpumask_var(&this_core_map, GFP_KERNEL)) | |
8339 | goto free_this_sibling_map; | |
8340 | if (!alloc_cpumask_var(&send_covered, GFP_KERNEL)) | |
8341 | goto free_this_core_map; | |
8342 | if (!alloc_cpumask_var(&tmpmask, GFP_KERNEL)) | |
8343 | goto free_send_covered; | |
8344 | ||
8345 | #ifdef CONFIG_NUMA | |
8346 | /* | |
8347 | * Allocate the per-node list of sched groups | |
8348 | */ | |
8349 | sched_group_nodes = kcalloc(nr_node_ids, sizeof(struct sched_group *), | |
8350 | GFP_KERNEL); | |
8351 | if (!sched_group_nodes) { | |
8352 | printk(KERN_WARNING "Can not alloc sched group node list\n"); | |
8353 | goto free_tmpmask; | |
8354 | } | |
8355 | #endif | |
8356 | ||
8357 | rd = alloc_rootdomain(); | |
8358 | if (!rd) { | |
8359 | printk(KERN_WARNING "Cannot alloc root domain\n"); | |
8360 | goto free_sched_groups; | |
8361 | } | |
8362 | ||
8363 | #ifdef CONFIG_NUMA | |
8364 | sched_group_nodes_bycpu[cpumask_first(cpu_map)] = sched_group_nodes; | |
8365 | #endif | |
8366 | ||
8367 | /* | |
8368 | * Set up domains for cpus specified by the cpu_map. | |
8369 | */ | |
8370 | for_each_cpu(i, cpu_map) { | |
8371 | struct sched_domain *sd = NULL, *p; | |
8372 | ||
8373 | cpumask_and(nodemask, cpumask_of_node(cpu_to_node(i)), cpu_map); | |
8374 | ||
8375 | #ifdef CONFIG_NUMA | |
8376 | if (cpumask_weight(cpu_map) > | |
8377 | SD_NODES_PER_DOMAIN*cpumask_weight(nodemask)) { | |
8378 | sd = &per_cpu(allnodes_domains, i).sd; | |
8379 | SD_INIT(sd, ALLNODES); | |
8380 | set_domain_attribute(sd, attr); | |
8381 | cpumask_copy(sched_domain_span(sd), cpu_map); | |
8382 | cpu_to_allnodes_group(i, cpu_map, &sd->groups, tmpmask); | |
8383 | p = sd; | |
8384 | sd_allnodes = 1; | |
8385 | } else | |
8386 | p = NULL; | |
8387 | ||
8388 | sd = &per_cpu(node_domains, i).sd; | |
8389 | SD_INIT(sd, NODE); | |
8390 | set_domain_attribute(sd, attr); | |
8391 | sched_domain_node_span(cpu_to_node(i), sched_domain_span(sd)); | |
8392 | sd->parent = p; | |
8393 | if (p) | |
8394 | p->child = sd; | |
8395 | cpumask_and(sched_domain_span(sd), | |
8396 | sched_domain_span(sd), cpu_map); | |
8397 | #endif | |
8398 | ||
8399 | p = sd; | |
8400 | sd = &per_cpu(phys_domains, i).sd; | |
8401 | SD_INIT(sd, CPU); | |
8402 | set_domain_attribute(sd, attr); | |
8403 | cpumask_copy(sched_domain_span(sd), nodemask); | |
8404 | sd->parent = p; | |
8405 | if (p) | |
8406 | p->child = sd; | |
8407 | cpu_to_phys_group(i, cpu_map, &sd->groups, tmpmask); | |
8408 | ||
8409 | #ifdef CONFIG_SCHED_MC | |
8410 | p = sd; | |
8411 | sd = &per_cpu(core_domains, i).sd; | |
8412 | SD_INIT(sd, MC); | |
8413 | set_domain_attribute(sd, attr); | |
8414 | cpumask_and(sched_domain_span(sd), cpu_map, | |
8415 | cpu_coregroup_mask(i)); | |
8416 | sd->parent = p; | |
8417 | p->child = sd; | |
8418 | cpu_to_core_group(i, cpu_map, &sd->groups, tmpmask); | |
8419 | #endif | |
8420 | ||
8421 | #ifdef CONFIG_SCHED_SMT | |
8422 | p = sd; | |
8423 | sd = &per_cpu(cpu_domains, i).sd; | |
8424 | SD_INIT(sd, SIBLING); | |
8425 | set_domain_attribute(sd, attr); | |
8426 | cpumask_and(sched_domain_span(sd), | |
8427 | topology_thread_cpumask(i), cpu_map); | |
8428 | sd->parent = p; | |
8429 | p->child = sd; | |
8430 | cpu_to_cpu_group(i, cpu_map, &sd->groups, tmpmask); | |
8431 | #endif | |
8432 | } | |
8433 | ||
8434 | #ifdef CONFIG_SCHED_SMT | |
8435 | /* Set up CPU (sibling) groups */ | |
8436 | for_each_cpu(i, cpu_map) { | |
8437 | cpumask_and(this_sibling_map, | |
8438 | topology_thread_cpumask(i), cpu_map); | |
8439 | if (i != cpumask_first(this_sibling_map)) | |
8440 | continue; | |
8441 | ||
8442 | init_sched_build_groups(this_sibling_map, cpu_map, | |
8443 | &cpu_to_cpu_group, | |
8444 | send_covered, tmpmask); | |
8445 | } | |
8446 | #endif | |
8447 | ||
8448 | #ifdef CONFIG_SCHED_MC | |
8449 | /* Set up multi-core groups */ | |
8450 | for_each_cpu(i, cpu_map) { | |
8451 | cpumask_and(this_core_map, cpu_coregroup_mask(i), cpu_map); | |
8452 | if (i != cpumask_first(this_core_map)) | |
8453 | continue; | |
8454 | ||
8455 | init_sched_build_groups(this_core_map, cpu_map, | |
8456 | &cpu_to_core_group, | |
8457 | send_covered, tmpmask); | |
8458 | } | |
8459 | #endif | |
8460 | ||
8461 | /* Set up physical groups */ | |
8462 | for (i = 0; i < nr_node_ids; i++) { | |
8463 | cpumask_and(nodemask, cpumask_of_node(i), cpu_map); | |
8464 | if (cpumask_empty(nodemask)) | |
8465 | continue; | |
8466 | ||
8467 | init_sched_build_groups(nodemask, cpu_map, | |
8468 | &cpu_to_phys_group, | |
8469 | send_covered, tmpmask); | |
8470 | } | |
8471 | ||
8472 | #ifdef CONFIG_NUMA | |
8473 | /* Set up node groups */ | |
8474 | if (sd_allnodes) { | |
8475 | init_sched_build_groups(cpu_map, cpu_map, | |
8476 | &cpu_to_allnodes_group, | |
8477 | send_covered, tmpmask); | |
8478 | } | |
8479 | ||
8480 | for (i = 0; i < nr_node_ids; i++) { | |
8481 | /* Set up node groups */ | |
8482 | struct sched_group *sg, *prev; | |
8483 | int j; | |
8484 | ||
8485 | cpumask_clear(covered); | |
8486 | cpumask_and(nodemask, cpumask_of_node(i), cpu_map); | |
8487 | if (cpumask_empty(nodemask)) { | |
8488 | sched_group_nodes[i] = NULL; | |
8489 | continue; | |
8490 | } | |
8491 | ||
8492 | sched_domain_node_span(i, domainspan); | |
8493 | cpumask_and(domainspan, domainspan, cpu_map); | |
8494 | ||
8495 | sg = kmalloc_node(sizeof(struct sched_group) + cpumask_size(), | |
8496 | GFP_KERNEL, i); | |
8497 | if (!sg) { | |
8498 | printk(KERN_WARNING "Can not alloc domain group for " | |
8499 | "node %d\n", i); | |
8500 | goto error; | |
8501 | } | |
8502 | sched_group_nodes[i] = sg; | |
8503 | for_each_cpu(j, nodemask) { | |
8504 | struct sched_domain *sd; | |
8505 | ||
8506 | sd = &per_cpu(node_domains, j).sd; | |
8507 | sd->groups = sg; | |
8508 | } | |
8509 | sg->__cpu_power = 0; | |
8510 | cpumask_copy(sched_group_cpus(sg), nodemask); | |
8511 | sg->next = sg; | |
8512 | cpumask_or(covered, covered, nodemask); | |
8513 | prev = sg; | |
8514 | ||
8515 | for (j = 0; j < nr_node_ids; j++) { | |
8516 | int n = (i + j) % nr_node_ids; | |
8517 | ||
8518 | cpumask_complement(notcovered, covered); | |
8519 | cpumask_and(tmpmask, notcovered, cpu_map); | |
8520 | cpumask_and(tmpmask, tmpmask, domainspan); | |
8521 | if (cpumask_empty(tmpmask)) | |
8522 | break; | |
8523 | ||
8524 | cpumask_and(tmpmask, tmpmask, cpumask_of_node(n)); | |
8525 | if (cpumask_empty(tmpmask)) | |
8526 | continue; | |
8527 | ||
8528 | sg = kmalloc_node(sizeof(struct sched_group) + | |
8529 | cpumask_size(), | |
8530 | GFP_KERNEL, i); | |
8531 | if (!sg) { | |
8532 | printk(KERN_WARNING | |
8533 | "Can not alloc domain group for node %d\n", j); | |
8534 | goto error; | |
8535 | } | |
8536 | sg->__cpu_power = 0; | |
8537 | cpumask_copy(sched_group_cpus(sg), tmpmask); | |
8538 | sg->next = prev->next; | |
8539 | cpumask_or(covered, covered, tmpmask); | |
8540 | prev->next = sg; | |
8541 | prev = sg; | |
8542 | } | |
8543 | } | |
8544 | #endif | |
8545 | ||
8546 | /* Calculate CPU power for physical packages and nodes */ | |
8547 | #ifdef CONFIG_SCHED_SMT | |
8548 | for_each_cpu(i, cpu_map) { | |
8549 | struct sched_domain *sd = &per_cpu(cpu_domains, i).sd; | |
8550 | ||
8551 | init_sched_groups_power(i, sd); | |
8552 | } | |
8553 | #endif | |
8554 | #ifdef CONFIG_SCHED_MC | |
8555 | for_each_cpu(i, cpu_map) { | |
8556 | struct sched_domain *sd = &per_cpu(core_domains, i).sd; | |
8557 | ||
8558 | init_sched_groups_power(i, sd); | |
8559 | } | |
8560 | #endif | |
8561 | ||
8562 | for_each_cpu(i, cpu_map) { | |
8563 | struct sched_domain *sd = &per_cpu(phys_domains, i).sd; | |
8564 | ||
8565 | init_sched_groups_power(i, sd); | |
8566 | } | |
8567 | ||
8568 | #ifdef CONFIG_NUMA | |
8569 | for (i = 0; i < nr_node_ids; i++) | |
8570 | init_numa_sched_groups_power(sched_group_nodes[i]); | |
8571 | ||
8572 | if (sd_allnodes) { | |
8573 | struct sched_group *sg; | |
8574 | ||
8575 | cpu_to_allnodes_group(cpumask_first(cpu_map), cpu_map, &sg, | |
8576 | tmpmask); | |
8577 | init_numa_sched_groups_power(sg); | |
8578 | } | |
8579 | #endif | |
8580 | ||
8581 | /* Attach the domains */ | |
8582 | for_each_cpu(i, cpu_map) { | |
8583 | struct sched_domain *sd; | |
8584 | #ifdef CONFIG_SCHED_SMT | |
8585 | sd = &per_cpu(cpu_domains, i).sd; | |
8586 | #elif defined(CONFIG_SCHED_MC) | |
8587 | sd = &per_cpu(core_domains, i).sd; | |
8588 | #else | |
8589 | sd = &per_cpu(phys_domains, i).sd; | |
8590 | #endif | |
8591 | cpu_attach_domain(sd, rd, i); | |
8592 | } | |
8593 | ||
8594 | err = 0; | |
8595 | ||
8596 | free_tmpmask: | |
8597 | free_cpumask_var(tmpmask); | |
8598 | free_send_covered: | |
8599 | free_cpumask_var(send_covered); | |
8600 | free_this_core_map: | |
8601 | free_cpumask_var(this_core_map); | |
8602 | free_this_sibling_map: | |
8603 | free_cpumask_var(this_sibling_map); | |
8604 | free_nodemask: | |
8605 | free_cpumask_var(nodemask); | |
8606 | free_notcovered: | |
8607 | #ifdef CONFIG_NUMA | |
8608 | free_cpumask_var(notcovered); | |
8609 | free_covered: | |
8610 | free_cpumask_var(covered); | |
8611 | free_domainspan: | |
8612 | free_cpumask_var(domainspan); | |
8613 | out: | |
8614 | #endif | |
8615 | return err; | |
8616 | ||
8617 | free_sched_groups: | |
8618 | #ifdef CONFIG_NUMA | |
8619 | kfree(sched_group_nodes); | |
8620 | #endif | |
8621 | goto free_tmpmask; | |
8622 | ||
8623 | #ifdef CONFIG_NUMA | |
8624 | error: | |
8625 | free_sched_groups(cpu_map, tmpmask); | |
8626 | free_rootdomain(rd); | |
8627 | goto free_tmpmask; | |
8628 | #endif | |
8629 | } | |
8630 | ||
8631 | static int build_sched_domains(const struct cpumask *cpu_map) | |
8632 | { | |
8633 | return __build_sched_domains(cpu_map, NULL); | |
8634 | } | |
8635 | ||
8636 | static struct cpumask *doms_cur; /* current sched domains */ | |
8637 | static int ndoms_cur; /* number of sched domains in 'doms_cur' */ | |
8638 | static struct sched_domain_attr *dattr_cur; | |
8639 | /* attribues of custom domains in 'doms_cur' */ | |
8640 | ||
8641 | /* | |
8642 | * Special case: If a kmalloc of a doms_cur partition (array of | |
8643 | * cpumask) fails, then fallback to a single sched domain, | |
8644 | * as determined by the single cpumask fallback_doms. | |
8645 | */ | |
8646 | static cpumask_var_t fallback_doms; | |
8647 | ||
8648 | /* | |
8649 | * arch_update_cpu_topology lets virtualized architectures update the | |
8650 | * cpu core maps. It is supposed to return 1 if the topology changed | |
8651 | * or 0 if it stayed the same. | |
8652 | */ | |
8653 | int __attribute__((weak)) arch_update_cpu_topology(void) | |
8654 | { | |
8655 | return 0; | |
8656 | } | |
8657 | ||
8658 | /* | |
8659 | * Set up scheduler domains and groups. Callers must hold the hotplug lock. | |
8660 | * For now this just excludes isolated cpus, but could be used to | |
8661 | * exclude other special cases in the future. | |
8662 | */ | |
8663 | static int arch_init_sched_domains(const struct cpumask *cpu_map) | |
8664 | { | |
8665 | int err; | |
8666 | ||
8667 | arch_update_cpu_topology(); | |
8668 | ndoms_cur = 1; | |
8669 | doms_cur = kmalloc(cpumask_size(), GFP_KERNEL); | |
8670 | if (!doms_cur) | |
8671 | doms_cur = fallback_doms; | |
8672 | cpumask_andnot(doms_cur, cpu_map, cpu_isolated_map); | |
8673 | dattr_cur = NULL; | |
8674 | err = build_sched_domains(doms_cur); | |
8675 | register_sched_domain_sysctl(); | |
8676 | ||
8677 | return err; | |
8678 | } | |
8679 | ||
8680 | static void arch_destroy_sched_domains(const struct cpumask *cpu_map, | |
8681 | struct cpumask *tmpmask) | |
8682 | { | |
8683 | free_sched_groups(cpu_map, tmpmask); | |
8684 | } | |
8685 | ||
8686 | /* | |
8687 | * Detach sched domains from a group of cpus specified in cpu_map | |
8688 | * These cpus will now be attached to the NULL domain | |
8689 | */ | |
8690 | static void detach_destroy_domains(const struct cpumask *cpu_map) | |
8691 | { | |
8692 | /* Save because hotplug lock held. */ | |
8693 | static DECLARE_BITMAP(tmpmask, CONFIG_NR_CPUS); | |
8694 | int i; | |
8695 | ||
8696 | for_each_cpu(i, cpu_map) | |
8697 | cpu_attach_domain(NULL, &def_root_domain, i); | |
8698 | synchronize_sched(); | |
8699 | arch_destroy_sched_domains(cpu_map, to_cpumask(tmpmask)); | |
8700 | } | |
8701 | ||
8702 | /* handle null as "default" */ | |
8703 | static int dattrs_equal(struct sched_domain_attr *cur, int idx_cur, | |
8704 | struct sched_domain_attr *new, int idx_new) | |
8705 | { | |
8706 | struct sched_domain_attr tmp; | |
8707 | ||
8708 | /* fast path */ | |
8709 | if (!new && !cur) | |
8710 | return 1; | |
8711 | ||
8712 | tmp = SD_ATTR_INIT; | |
8713 | return !memcmp(cur ? (cur + idx_cur) : &tmp, | |
8714 | new ? (new + idx_new) : &tmp, | |
8715 | sizeof(struct sched_domain_attr)); | |
8716 | } | |
8717 | ||
8718 | /* | |
8719 | * Partition sched domains as specified by the 'ndoms_new' | |
8720 | * cpumasks in the array doms_new[] of cpumasks. This compares | |
8721 | * doms_new[] to the current sched domain partitioning, doms_cur[]. | |
8722 | * It destroys each deleted domain and builds each new domain. | |
8723 | * | |
8724 | * 'doms_new' is an array of cpumask's of length 'ndoms_new'. | |
8725 | * The masks don't intersect (don't overlap.) We should setup one | |
8726 | * sched domain for each mask. CPUs not in any of the cpumasks will | |
8727 | * not be load balanced. If the same cpumask appears both in the | |
8728 | * current 'doms_cur' domains and in the new 'doms_new', we can leave | |
8729 | * it as it is. | |
8730 | * | |
8731 | * The passed in 'doms_new' should be kmalloc'd. This routine takes | |
8732 | * ownership of it and will kfree it when done with it. If the caller | |
8733 | * failed the kmalloc call, then it can pass in doms_new == NULL && | |
8734 | * ndoms_new == 1, and partition_sched_domains() will fallback to | |
8735 | * the single partition 'fallback_doms', it also forces the domains | |
8736 | * to be rebuilt. | |
8737 | * | |
8738 | * If doms_new == NULL it will be replaced with cpu_online_mask. | |
8739 | * ndoms_new == 0 is a special case for destroying existing domains, | |
8740 | * and it will not create the default domain. | |
8741 | * | |
8742 | * Call with hotplug lock held | |
8743 | */ | |
8744 | /* FIXME: Change to struct cpumask *doms_new[] */ | |
8745 | void partition_sched_domains(int ndoms_new, struct cpumask *doms_new, | |
8746 | struct sched_domain_attr *dattr_new) | |
8747 | { | |
8748 | int i, j, n; | |
8749 | int new_topology; | |
8750 | ||
8751 | mutex_lock(&sched_domains_mutex); | |
8752 | ||
8753 | /* always unregister in case we don't destroy any domains */ | |
8754 | unregister_sched_domain_sysctl(); | |
8755 | ||
8756 | /* Let architecture update cpu core mappings. */ | |
8757 | new_topology = arch_update_cpu_topology(); | |
8758 | ||
8759 | n = doms_new ? ndoms_new : 0; | |
8760 | ||
8761 | /* Destroy deleted domains */ | |
8762 | for (i = 0; i < ndoms_cur; i++) { | |
8763 | for (j = 0; j < n && !new_topology; j++) { | |
8764 | if (cpumask_equal(&doms_cur[i], &doms_new[j]) | |
8765 | && dattrs_equal(dattr_cur, i, dattr_new, j)) | |
8766 | goto match1; | |
8767 | } | |
8768 | /* no match - a current sched domain not in new doms_new[] */ | |
8769 | detach_destroy_domains(doms_cur + i); | |
8770 | match1: | |
8771 | ; | |
8772 | } | |
8773 | ||
8774 | if (doms_new == NULL) { | |
8775 | ndoms_cur = 0; | |
8776 | doms_new = fallback_doms; | |
8777 | cpumask_andnot(&doms_new[0], cpu_online_mask, cpu_isolated_map); | |
8778 | WARN_ON_ONCE(dattr_new); | |
8779 | } | |
8780 | ||
8781 | /* Build new domains */ | |
8782 | for (i = 0; i < ndoms_new; i++) { | |
8783 | for (j = 0; j < ndoms_cur && !new_topology; j++) { | |
8784 | if (cpumask_equal(&doms_new[i], &doms_cur[j]) | |
8785 | && dattrs_equal(dattr_new, i, dattr_cur, j)) | |
8786 | goto match2; | |
8787 | } | |
8788 | /* no match - add a new doms_new */ | |
8789 | __build_sched_domains(doms_new + i, | |
8790 | dattr_new ? dattr_new + i : NULL); | |
8791 | match2: | |
8792 | ; | |
8793 | } | |
8794 | ||
8795 | /* Remember the new sched domains */ | |
8796 | if (doms_cur != fallback_doms) | |
8797 | kfree(doms_cur); | |
8798 | kfree(dattr_cur); /* kfree(NULL) is safe */ | |
8799 | doms_cur = doms_new; | |
8800 | dattr_cur = dattr_new; | |
8801 | ndoms_cur = ndoms_new; | |
8802 | ||
8803 | register_sched_domain_sysctl(); | |
8804 | ||
8805 | mutex_unlock(&sched_domains_mutex); | |
8806 | } | |
8807 | ||
8808 | #if defined(CONFIG_SCHED_MC) || defined(CONFIG_SCHED_SMT) | |
8809 | static void arch_reinit_sched_domains(void) | |
8810 | { | |
8811 | get_online_cpus(); | |
8812 | ||
8813 | /* Destroy domains first to force the rebuild */ | |
8814 | partition_sched_domains(0, NULL, NULL); | |
8815 | ||
8816 | rebuild_sched_domains(); | |
8817 | put_online_cpus(); | |
8818 | } | |
8819 | ||
8820 | static ssize_t sched_power_savings_store(const char *buf, size_t count, int smt) | |
8821 | { | |
8822 | unsigned int level = 0; | |
8823 | ||
8824 | if (sscanf(buf, "%u", &level) != 1) | |
8825 | return -EINVAL; | |
8826 | ||
8827 | /* | |
8828 | * level is always be positive so don't check for | |
8829 | * level < POWERSAVINGS_BALANCE_NONE which is 0 | |
8830 | * What happens on 0 or 1 byte write, | |
8831 | * need to check for count as well? | |
8832 | */ | |
8833 | ||
8834 | if (level >= MAX_POWERSAVINGS_BALANCE_LEVELS) | |
8835 | return -EINVAL; | |
8836 | ||
8837 | if (smt) | |
8838 | sched_smt_power_savings = level; | |
8839 | else | |
8840 | sched_mc_power_savings = level; | |
8841 | ||
8842 | arch_reinit_sched_domains(); | |
8843 | ||
8844 | return count; | |
8845 | } | |
8846 | ||
8847 | #ifdef CONFIG_SCHED_MC | |
8848 | static ssize_t sched_mc_power_savings_show(struct sysdev_class *class, | |
8849 | char *page) | |
8850 | { | |
8851 | return sprintf(page, "%u\n", sched_mc_power_savings); | |
8852 | } | |
8853 | static ssize_t sched_mc_power_savings_store(struct sysdev_class *class, | |
8854 | const char *buf, size_t count) | |
8855 | { | |
8856 | return sched_power_savings_store(buf, count, 0); | |
8857 | } | |
8858 | static SYSDEV_CLASS_ATTR(sched_mc_power_savings, 0644, | |
8859 | sched_mc_power_savings_show, | |
8860 | sched_mc_power_savings_store); | |
8861 | #endif | |
8862 | ||
8863 | #ifdef CONFIG_SCHED_SMT | |
8864 | static ssize_t sched_smt_power_savings_show(struct sysdev_class *dev, | |
8865 | char *page) | |
8866 | { | |
8867 | return sprintf(page, "%u\n", sched_smt_power_savings); | |
8868 | } | |
8869 | static ssize_t sched_smt_power_savings_store(struct sysdev_class *dev, | |
8870 | const char *buf, size_t count) | |
8871 | { | |
8872 | return sched_power_savings_store(buf, count, 1); | |
8873 | } | |
8874 | static SYSDEV_CLASS_ATTR(sched_smt_power_savings, 0644, | |
8875 | sched_smt_power_savings_show, | |
8876 | sched_smt_power_savings_store); | |
8877 | #endif | |
8878 | ||
8879 | int __init sched_create_sysfs_power_savings_entries(struct sysdev_class *cls) | |
8880 | { | |
8881 | int err = 0; | |
8882 | ||
8883 | #ifdef CONFIG_SCHED_SMT | |
8884 | if (smt_capable()) | |
8885 | err = sysfs_create_file(&cls->kset.kobj, | |
8886 | &attr_sched_smt_power_savings.attr); | |
8887 | #endif | |
8888 | #ifdef CONFIG_SCHED_MC | |
8889 | if (!err && mc_capable()) | |
8890 | err = sysfs_create_file(&cls->kset.kobj, | |
8891 | &attr_sched_mc_power_savings.attr); | |
8892 | #endif | |
8893 | return err; | |
8894 | } | |
8895 | #endif /* CONFIG_SCHED_MC || CONFIG_SCHED_SMT */ | |
8896 | ||
8897 | #ifndef CONFIG_CPUSETS | |
8898 | /* | |
8899 | * Add online and remove offline CPUs from the scheduler domains. | |
8900 | * When cpusets are enabled they take over this function. | |
8901 | */ | |
8902 | static int update_sched_domains(struct notifier_block *nfb, | |
8903 | unsigned long action, void *hcpu) | |
8904 | { | |
8905 | switch (action) { | |
8906 | case CPU_ONLINE: | |
8907 | case CPU_ONLINE_FROZEN: | |
8908 | case CPU_DEAD: | |
8909 | case CPU_DEAD_FROZEN: | |
8910 | partition_sched_domains(1, NULL, NULL); | |
8911 | return NOTIFY_OK; | |
8912 | ||
8913 | default: | |
8914 | return NOTIFY_DONE; | |
8915 | } | |
8916 | } | |
8917 | #endif | |
8918 | ||
8919 | static int update_runtime(struct notifier_block *nfb, | |
8920 | unsigned long action, void *hcpu) | |
8921 | { | |
8922 | int cpu = (int)(long)hcpu; | |
8923 | ||
8924 | switch (action) { | |
8925 | case CPU_DOWN_PREPARE: | |
8926 | case CPU_DOWN_PREPARE_FROZEN: | |
8927 | disable_runtime(cpu_rq(cpu)); | |
8928 | return NOTIFY_OK; | |
8929 | ||
8930 | case CPU_DOWN_FAILED: | |
8931 | case CPU_DOWN_FAILED_FROZEN: | |
8932 | case CPU_ONLINE: | |
8933 | case CPU_ONLINE_FROZEN: | |
8934 | enable_runtime(cpu_rq(cpu)); | |
8935 | return NOTIFY_OK; | |
8936 | ||
8937 | default: | |
8938 | return NOTIFY_DONE; | |
8939 | } | |
8940 | } | |
8941 | ||
8942 | void __init sched_init_smp(void) | |
8943 | { | |
8944 | cpumask_var_t non_isolated_cpus; | |
8945 | ||
8946 | alloc_cpumask_var(&non_isolated_cpus, GFP_KERNEL); | |
8947 | ||
8948 | #if defined(CONFIG_NUMA) | |
8949 | sched_group_nodes_bycpu = kzalloc(nr_cpu_ids * sizeof(void **), | |
8950 | GFP_KERNEL); | |
8951 | BUG_ON(sched_group_nodes_bycpu == NULL); | |
8952 | #endif | |
8953 | get_online_cpus(); | |
8954 | mutex_lock(&sched_domains_mutex); | |
8955 | arch_init_sched_domains(cpu_online_mask); | |
8956 | cpumask_andnot(non_isolated_cpus, cpu_possible_mask, cpu_isolated_map); | |
8957 | if (cpumask_empty(non_isolated_cpus)) | |
8958 | cpumask_set_cpu(smp_processor_id(), non_isolated_cpus); | |
8959 | mutex_unlock(&sched_domains_mutex); | |
8960 | put_online_cpus(); | |
8961 | ||
8962 | #ifndef CONFIG_CPUSETS | |
8963 | /* XXX: Theoretical race here - CPU may be hotplugged now */ | |
8964 | hotcpu_notifier(update_sched_domains, 0); | |
8965 | #endif | |
8966 | ||
8967 | /* RT runtime code needs to handle some hotplug events */ | |
8968 | hotcpu_notifier(update_runtime, 0); | |
8969 | ||
8970 | init_hrtick(); | |
8971 | ||
8972 | /* Move init over to a non-isolated CPU */ | |
8973 | if (set_cpus_allowed_ptr(current, non_isolated_cpus) < 0) | |
8974 | BUG(); | |
8975 | sched_init_granularity(); | |
8976 | free_cpumask_var(non_isolated_cpus); | |
8977 | ||
8978 | alloc_cpumask_var(&fallback_doms, GFP_KERNEL); | |
8979 | init_sched_rt_class(); | |
8980 | } | |
8981 | #else | |
8982 | void __init sched_init_smp(void) | |
8983 | { | |
8984 | sched_init_granularity(); | |
8985 | } | |
8986 | #endif /* CONFIG_SMP */ | |
8987 | ||
8988 | int in_sched_functions(unsigned long addr) | |
8989 | { | |
8990 | return in_lock_functions(addr) || | |
8991 | (addr >= (unsigned long)__sched_text_start | |
8992 | && addr < (unsigned long)__sched_text_end); | |
8993 | } | |
8994 | ||
8995 | static void init_cfs_rq(struct cfs_rq *cfs_rq, struct rq *rq) | |
8996 | { | |
8997 | cfs_rq->tasks_timeline = RB_ROOT; | |
8998 | INIT_LIST_HEAD(&cfs_rq->tasks); | |
8999 | #ifdef CONFIG_FAIR_GROUP_SCHED | |
9000 | cfs_rq->rq = rq; | |
9001 | #endif | |
9002 | cfs_rq->min_vruntime = (u64)(-(1LL << 20)); | |
9003 | } | |
9004 | ||
9005 | static void init_rt_rq(struct rt_rq *rt_rq, struct rq *rq) | |
9006 | { | |
9007 | struct rt_prio_array *array; | |
9008 | int i; | |
9009 | ||
9010 | array = &rt_rq->active; | |
9011 | for (i = 0; i < MAX_RT_PRIO; i++) { | |
9012 | INIT_LIST_HEAD(array->queue + i); | |
9013 | __clear_bit(i, array->bitmap); | |
9014 | } | |
9015 | /* delimiter for bitsearch: */ | |
9016 | __set_bit(MAX_RT_PRIO, array->bitmap); | |
9017 | ||
9018 | #if defined CONFIG_SMP || defined CONFIG_RT_GROUP_SCHED | |
9019 | rt_rq->highest_prio.curr = MAX_RT_PRIO; | |
9020 | #ifdef CONFIG_SMP | |
9021 | rt_rq->highest_prio.next = MAX_RT_PRIO; | |
9022 | #endif | |
9023 | #endif | |
9024 | #ifdef CONFIG_SMP | |
9025 | rt_rq->rt_nr_migratory = 0; | |
9026 | rt_rq->overloaded = 0; | |
9027 | plist_head_init(&rq->rt.pushable_tasks, &rq->lock); | |
9028 | #endif | |
9029 | ||
9030 | rt_rq->rt_time = 0; | |
9031 | rt_rq->rt_throttled = 0; | |
9032 | rt_rq->rt_runtime = 0; | |
9033 | spin_lock_init(&rt_rq->rt_runtime_lock); | |
9034 | ||
9035 | #ifdef CONFIG_RT_GROUP_SCHED | |
9036 | rt_rq->rt_nr_boosted = 0; | |
9037 | rt_rq->rq = rq; | |
9038 | #endif | |
9039 | } | |
9040 | ||
9041 | #ifdef CONFIG_FAIR_GROUP_SCHED | |
9042 | static void init_tg_cfs_entry(struct task_group *tg, struct cfs_rq *cfs_rq, | |
9043 | struct sched_entity *se, int cpu, int add, | |
9044 | struct sched_entity *parent) | |
9045 | { | |
9046 | struct rq *rq = cpu_rq(cpu); | |
9047 | tg->cfs_rq[cpu] = cfs_rq; | |
9048 | init_cfs_rq(cfs_rq, rq); | |
9049 | cfs_rq->tg = tg; | |
9050 | if (add) | |
9051 | list_add(&cfs_rq->leaf_cfs_rq_list, &rq->leaf_cfs_rq_list); | |
9052 | ||
9053 | tg->se[cpu] = se; | |
9054 | /* se could be NULL for init_task_group */ | |
9055 | if (!se) | |
9056 | return; | |
9057 | ||
9058 | if (!parent) | |
9059 | se->cfs_rq = &rq->cfs; | |
9060 | else | |
9061 | se->cfs_rq = parent->my_q; | |
9062 | ||
9063 | se->my_q = cfs_rq; | |
9064 | se->load.weight = tg->shares; | |
9065 | se->load.inv_weight = 0; | |
9066 | se->parent = parent; | |
9067 | } | |
9068 | #endif | |
9069 | ||
9070 | #ifdef CONFIG_RT_GROUP_SCHED | |
9071 | static void init_tg_rt_entry(struct task_group *tg, struct rt_rq *rt_rq, | |
9072 | struct sched_rt_entity *rt_se, int cpu, int add, | |
9073 | struct sched_rt_entity *parent) | |
9074 | { | |
9075 | struct rq *rq = cpu_rq(cpu); | |
9076 | ||
9077 | tg->rt_rq[cpu] = rt_rq; | |
9078 | init_rt_rq(rt_rq, rq); | |
9079 | rt_rq->tg = tg; | |
9080 | rt_rq->rt_se = rt_se; | |
9081 | rt_rq->rt_runtime = tg->rt_bandwidth.rt_runtime; | |
9082 | if (add) | |
9083 | list_add(&rt_rq->leaf_rt_rq_list, &rq->leaf_rt_rq_list); | |
9084 | ||
9085 | tg->rt_se[cpu] = rt_se; | |
9086 | if (!rt_se) | |
9087 | return; | |
9088 | ||
9089 | if (!parent) | |
9090 | rt_se->rt_rq = &rq->rt; | |
9091 | else | |
9092 | rt_se->rt_rq = parent->my_q; | |
9093 | ||
9094 | rt_se->my_q = rt_rq; | |
9095 | rt_se->parent = parent; | |
9096 | INIT_LIST_HEAD(&rt_se->run_list); | |
9097 | } | |
9098 | #endif | |
9099 | ||
9100 | void __init sched_init(void) | |
9101 | { | |
9102 | int i, j; | |
9103 | unsigned long alloc_size = 0, ptr; | |
9104 | ||
9105 | #ifdef CONFIG_FAIR_GROUP_SCHED | |
9106 | alloc_size += 2 * nr_cpu_ids * sizeof(void **); | |
9107 | #endif | |
9108 | #ifdef CONFIG_RT_GROUP_SCHED | |
9109 | alloc_size += 2 * nr_cpu_ids * sizeof(void **); | |
9110 | #endif | |
9111 | #ifdef CONFIG_USER_SCHED | |
9112 | alloc_size *= 2; | |
9113 | #endif | |
9114 | #ifdef CONFIG_CPUMASK_OFFSTACK | |
9115 | alloc_size += num_possible_cpus() * cpumask_size(); | |
9116 | #endif | |
9117 | /* | |
9118 | * As sched_init() is called before page_alloc is setup, | |
9119 | * we use alloc_bootmem(). | |
9120 | */ | |
9121 | if (alloc_size) { | |
9122 | ptr = (unsigned long)kzalloc(alloc_size, GFP_NOWAIT); | |
9123 | ||
9124 | #ifdef CONFIG_FAIR_GROUP_SCHED | |
9125 | init_task_group.se = (struct sched_entity **)ptr; | |
9126 | ptr += nr_cpu_ids * sizeof(void **); | |
9127 | ||
9128 | init_task_group.cfs_rq = (struct cfs_rq **)ptr; | |
9129 | ptr += nr_cpu_ids * sizeof(void **); | |
9130 | ||
9131 | #ifdef CONFIG_USER_SCHED | |
9132 | root_task_group.se = (struct sched_entity **)ptr; | |
9133 | ptr += nr_cpu_ids * sizeof(void **); | |
9134 | ||
9135 | root_task_group.cfs_rq = (struct cfs_rq **)ptr; | |
9136 | ptr += nr_cpu_ids * sizeof(void **); | |
9137 | #endif /* CONFIG_USER_SCHED */ | |
9138 | #endif /* CONFIG_FAIR_GROUP_SCHED */ | |
9139 | #ifdef CONFIG_RT_GROUP_SCHED | |
9140 | init_task_group.rt_se = (struct sched_rt_entity **)ptr; | |
9141 | ptr += nr_cpu_ids * sizeof(void **); | |
9142 | ||
9143 | init_task_group.rt_rq = (struct rt_rq **)ptr; | |
9144 | ptr += nr_cpu_ids * sizeof(void **); | |
9145 | ||
9146 | #ifdef CONFIG_USER_SCHED | |
9147 | root_task_group.rt_se = (struct sched_rt_entity **)ptr; | |
9148 | ptr += nr_cpu_ids * sizeof(void **); | |
9149 | ||
9150 | root_task_group.rt_rq = (struct rt_rq **)ptr; | |
9151 | ptr += nr_cpu_ids * sizeof(void **); | |
9152 | #endif /* CONFIG_USER_SCHED */ | |
9153 | #endif /* CONFIG_RT_GROUP_SCHED */ | |
9154 | #ifdef CONFIG_CPUMASK_OFFSTACK | |
9155 | for_each_possible_cpu(i) { | |
9156 | per_cpu(load_balance_tmpmask, i) = (void *)ptr; | |
9157 | ptr += cpumask_size(); | |
9158 | } | |
9159 | #endif /* CONFIG_CPUMASK_OFFSTACK */ | |
9160 | } | |
9161 | ||
9162 | #ifdef CONFIG_SMP | |
9163 | init_defrootdomain(); | |
9164 | #endif | |
9165 | ||
9166 | init_rt_bandwidth(&def_rt_bandwidth, | |
9167 | global_rt_period(), global_rt_runtime()); | |
9168 | ||
9169 | #ifdef CONFIG_RT_GROUP_SCHED | |
9170 | init_rt_bandwidth(&init_task_group.rt_bandwidth, | |
9171 | global_rt_period(), global_rt_runtime()); | |
9172 | #ifdef CONFIG_USER_SCHED | |
9173 | init_rt_bandwidth(&root_task_group.rt_bandwidth, | |
9174 | global_rt_period(), RUNTIME_INF); | |
9175 | #endif /* CONFIG_USER_SCHED */ | |
9176 | #endif /* CONFIG_RT_GROUP_SCHED */ | |
9177 | ||
9178 | #ifdef CONFIG_GROUP_SCHED | |
9179 | list_add(&init_task_group.list, &task_groups); | |
9180 | INIT_LIST_HEAD(&init_task_group.children); | |
9181 | ||
9182 | #ifdef CONFIG_USER_SCHED | |
9183 | INIT_LIST_HEAD(&root_task_group.children); | |
9184 | init_task_group.parent = &root_task_group; | |
9185 | list_add(&init_task_group.siblings, &root_task_group.children); | |
9186 | #endif /* CONFIG_USER_SCHED */ | |
9187 | #endif /* CONFIG_GROUP_SCHED */ | |
9188 | ||
9189 | for_each_possible_cpu(i) { | |
9190 | struct rq *rq; | |
9191 | ||
9192 | rq = cpu_rq(i); | |
9193 | spin_lock_init(&rq->lock); | |
9194 | rq->nr_running = 0; | |
9195 | rq->calc_load_active = 0; | |
9196 | rq->calc_load_update = jiffies + LOAD_FREQ; | |
9197 | init_cfs_rq(&rq->cfs, rq); | |
9198 | init_rt_rq(&rq->rt, rq); | |
9199 | #ifdef CONFIG_FAIR_GROUP_SCHED | |
9200 | init_task_group.shares = init_task_group_load; | |
9201 | INIT_LIST_HEAD(&rq->leaf_cfs_rq_list); | |
9202 | #ifdef CONFIG_CGROUP_SCHED | |
9203 | /* | |
9204 | * How much cpu bandwidth does init_task_group get? | |
9205 | * | |
9206 | * In case of task-groups formed thr' the cgroup filesystem, it | |
9207 | * gets 100% of the cpu resources in the system. This overall | |
9208 | * system cpu resource is divided among the tasks of | |
9209 | * init_task_group and its child task-groups in a fair manner, | |
9210 | * based on each entity's (task or task-group's) weight | |
9211 | * (se->load.weight). | |
9212 | * | |
9213 | * In other words, if init_task_group has 10 tasks of weight | |
9214 | * 1024) and two child groups A0 and A1 (of weight 1024 each), | |
9215 | * then A0's share of the cpu resource is: | |
9216 | * | |
9217 | * A0's bandwidth = 1024 / (10*1024 + 1024 + 1024) = 8.33% | |
9218 | * | |
9219 | * We achieve this by letting init_task_group's tasks sit | |
9220 | * directly in rq->cfs (i.e init_task_group->se[] = NULL). | |
9221 | */ | |
9222 | init_tg_cfs_entry(&init_task_group, &rq->cfs, NULL, i, 1, NULL); | |
9223 | #elif defined CONFIG_USER_SCHED | |
9224 | root_task_group.shares = NICE_0_LOAD; | |
9225 | init_tg_cfs_entry(&root_task_group, &rq->cfs, NULL, i, 0, NULL); | |
9226 | /* | |
9227 | * In case of task-groups formed thr' the user id of tasks, | |
9228 | * init_task_group represents tasks belonging to root user. | |
9229 | * Hence it forms a sibling of all subsequent groups formed. | |
9230 | * In this case, init_task_group gets only a fraction of overall | |
9231 | * system cpu resource, based on the weight assigned to root | |
9232 | * user's cpu share (INIT_TASK_GROUP_LOAD). This is accomplished | |
9233 | * by letting tasks of init_task_group sit in a separate cfs_rq | |
9234 | * (init_cfs_rq) and having one entity represent this group of | |
9235 | * tasks in rq->cfs (i.e init_task_group->se[] != NULL). | |
9236 | */ | |
9237 | init_tg_cfs_entry(&init_task_group, | |
9238 | &per_cpu(init_cfs_rq, i), | |
9239 | &per_cpu(init_sched_entity, i), i, 1, | |
9240 | root_task_group.se[i]); | |
9241 | ||
9242 | #endif | |
9243 | #endif /* CONFIG_FAIR_GROUP_SCHED */ | |
9244 | ||
9245 | rq->rt.rt_runtime = def_rt_bandwidth.rt_runtime; | |
9246 | #ifdef CONFIG_RT_GROUP_SCHED | |
9247 | INIT_LIST_HEAD(&rq->leaf_rt_rq_list); | |
9248 | #ifdef CONFIG_CGROUP_SCHED | |
9249 | init_tg_rt_entry(&init_task_group, &rq->rt, NULL, i, 1, NULL); | |
9250 | #elif defined CONFIG_USER_SCHED | |
9251 | init_tg_rt_entry(&root_task_group, &rq->rt, NULL, i, 0, NULL); | |
9252 | init_tg_rt_entry(&init_task_group, | |
9253 | &per_cpu(init_rt_rq, i), | |
9254 | &per_cpu(init_sched_rt_entity, i), i, 1, | |
9255 | root_task_group.rt_se[i]); | |
9256 | #endif | |
9257 | #endif | |
9258 | ||
9259 | for (j = 0; j < CPU_LOAD_IDX_MAX; j++) | |
9260 | rq->cpu_load[j] = 0; | |
9261 | #ifdef CONFIG_SMP | |
9262 | rq->sd = NULL; | |
9263 | rq->rd = NULL; | |
9264 | rq->active_balance = 0; | |
9265 | rq->next_balance = jiffies; | |
9266 | rq->push_cpu = 0; | |
9267 | rq->cpu = i; | |
9268 | rq->online = 0; | |
9269 | rq->migration_thread = NULL; | |
9270 | INIT_LIST_HEAD(&rq->migration_queue); | |
9271 | rq_attach_root(rq, &def_root_domain); | |
9272 | #endif | |
9273 | init_rq_hrtick(rq); | |
9274 | atomic_set(&rq->nr_iowait, 0); | |
9275 | } | |
9276 | ||
9277 | set_load_weight(&init_task); | |
9278 | ||
9279 | #ifdef CONFIG_PREEMPT_NOTIFIERS | |
9280 | INIT_HLIST_HEAD(&init_task.preempt_notifiers); | |
9281 | #endif | |
9282 | ||
9283 | #ifdef CONFIG_SMP | |
9284 | open_softirq(SCHED_SOFTIRQ, run_rebalance_domains); | |
9285 | #endif | |
9286 | ||
9287 | #ifdef CONFIG_RT_MUTEXES | |
9288 | plist_head_init(&init_task.pi_waiters, &init_task.pi_lock); | |
9289 | #endif | |
9290 | ||
9291 | /* | |
9292 | * The boot idle thread does lazy MMU switching as well: | |
9293 | */ | |
9294 | atomic_inc(&init_mm.mm_count); | |
9295 | enter_lazy_tlb(&init_mm, current); | |
9296 | ||
9297 | /* | |
9298 | * Make us the idle thread. Technically, schedule() should not be | |
9299 | * called from this thread, however somewhere below it might be, | |
9300 | * but because we are the idle thread, we just pick up running again | |
9301 | * when this runqueue becomes "idle". | |
9302 | */ | |
9303 | init_idle(current, smp_processor_id()); | |
9304 | ||
9305 | calc_load_update = jiffies + LOAD_FREQ; | |
9306 | ||
9307 | /* | |
9308 | * During early bootup we pretend to be a normal task: | |
9309 | */ | |
9310 | current->sched_class = &fair_sched_class; | |
9311 | ||
9312 | /* Allocate the nohz_cpu_mask if CONFIG_CPUMASK_OFFSTACK */ | |
9313 | alloc_bootmem_cpumask_var(&nohz_cpu_mask); | |
9314 | #ifdef CONFIG_SMP | |
9315 | #ifdef CONFIG_NO_HZ | |
9316 | alloc_bootmem_cpumask_var(&nohz.cpu_mask); | |
9317 | alloc_bootmem_cpumask_var(&nohz.ilb_grp_nohz_mask); | |
9318 | #endif | |
9319 | alloc_bootmem_cpumask_var(&cpu_isolated_map); | |
9320 | #endif /* SMP */ | |
9321 | ||
9322 | scheduler_running = 1; | |
9323 | } | |
9324 | ||
9325 | #ifdef CONFIG_DEBUG_SPINLOCK_SLEEP | |
9326 | void __might_sleep(char *file, int line) | |
9327 | { | |
9328 | #ifdef in_atomic | |
9329 | static unsigned long prev_jiffy; /* ratelimiting */ | |
9330 | ||
9331 | if ((!in_atomic() && !irqs_disabled()) || | |
9332 | system_state != SYSTEM_RUNNING || oops_in_progress) | |
9333 | return; | |
9334 | if (time_before(jiffies, prev_jiffy + HZ) && prev_jiffy) | |
9335 | return; | |
9336 | prev_jiffy = jiffies; | |
9337 | ||
9338 | printk(KERN_ERR | |
9339 | "BUG: sleeping function called from invalid context at %s:%d\n", | |
9340 | file, line); | |
9341 | printk(KERN_ERR | |
9342 | "in_atomic(): %d, irqs_disabled(): %d, pid: %d, name: %s\n", | |
9343 | in_atomic(), irqs_disabled(), | |
9344 | current->pid, current->comm); | |
9345 | ||
9346 | debug_show_held_locks(current); | |
9347 | if (irqs_disabled()) | |
9348 | print_irqtrace_events(current); | |
9349 | dump_stack(); | |
9350 | #endif | |
9351 | } | |
9352 | EXPORT_SYMBOL(__might_sleep); | |
9353 | #endif | |
9354 | ||
9355 | #ifdef CONFIG_MAGIC_SYSRQ | |
9356 | static void normalize_task(struct rq *rq, struct task_struct *p) | |
9357 | { | |
9358 | int on_rq; | |
9359 | ||
9360 | update_rq_clock(rq); | |
9361 | on_rq = p->se.on_rq; | |
9362 | if (on_rq) | |
9363 | deactivate_task(rq, p, 0); | |
9364 | __setscheduler(rq, p, SCHED_NORMAL, 0); | |
9365 | if (on_rq) { | |
9366 | activate_task(rq, p, 0); | |
9367 | resched_task(rq->curr); | |
9368 | } | |
9369 | } | |
9370 | ||
9371 | void normalize_rt_tasks(void) | |
9372 | { | |
9373 | struct task_struct *g, *p; | |
9374 | unsigned long flags; | |
9375 | struct rq *rq; | |
9376 | ||
9377 | read_lock_irqsave(&tasklist_lock, flags); | |
9378 | do_each_thread(g, p) { | |
9379 | /* | |
9380 | * Only normalize user tasks: | |
9381 | */ | |
9382 | if (!p->mm) | |
9383 | continue; | |
9384 | ||
9385 | p->se.exec_start = 0; | |
9386 | #ifdef CONFIG_SCHEDSTATS | |
9387 | p->se.wait_start = 0; | |
9388 | p->se.sleep_start = 0; | |
9389 | p->se.block_start = 0; | |
9390 | #endif | |
9391 | ||
9392 | if (!rt_task(p)) { | |
9393 | /* | |
9394 | * Renice negative nice level userspace | |
9395 | * tasks back to 0: | |
9396 | */ | |
9397 | if (TASK_NICE(p) < 0 && p->mm) | |
9398 | set_user_nice(p, 0); | |
9399 | continue; | |
9400 | } | |
9401 | ||
9402 | spin_lock(&p->pi_lock); | |
9403 | rq = __task_rq_lock(p); | |
9404 | ||
9405 | normalize_task(rq, p); | |
9406 | ||
9407 | __task_rq_unlock(rq); | |
9408 | spin_unlock(&p->pi_lock); | |
9409 | } while_each_thread(g, p); | |
9410 | ||
9411 | read_unlock_irqrestore(&tasklist_lock, flags); | |
9412 | } | |
9413 | ||
9414 | #endif /* CONFIG_MAGIC_SYSRQ */ | |
9415 | ||
9416 | #ifdef CONFIG_IA64 | |
9417 | /* | |
9418 | * These functions are only useful for the IA64 MCA handling. | |
9419 | * | |
9420 | * They can only be called when the whole system has been | |
9421 | * stopped - every CPU needs to be quiescent, and no scheduling | |
9422 | * activity can take place. Using them for anything else would | |
9423 | * be a serious bug, and as a result, they aren't even visible | |
9424 | * under any other configuration. | |
9425 | */ | |
9426 | ||
9427 | /** | |
9428 | * curr_task - return the current task for a given cpu. | |
9429 | * @cpu: the processor in question. | |
9430 | * | |
9431 | * ONLY VALID WHEN THE WHOLE SYSTEM IS STOPPED! | |
9432 | */ | |
9433 | struct task_struct *curr_task(int cpu) | |
9434 | { | |
9435 | return cpu_curr(cpu); | |
9436 | } | |
9437 | ||
9438 | /** | |
9439 | * set_curr_task - set the current task for a given cpu. | |
9440 | * @cpu: the processor in question. | |
9441 | * @p: the task pointer to set. | |
9442 | * | |
9443 | * Description: This function must only be used when non-maskable interrupts | |
9444 | * are serviced on a separate stack. It allows the architecture to switch the | |
9445 | * notion of the current task on a cpu in a non-blocking manner. This function | |
9446 | * must be called with all CPU's synchronized, and interrupts disabled, the | |
9447 | * and caller must save the original value of the current task (see | |
9448 | * curr_task() above) and restore that value before reenabling interrupts and | |
9449 | * re-starting the system. | |
9450 | * | |
9451 | * ONLY VALID WHEN THE WHOLE SYSTEM IS STOPPED! | |
9452 | */ | |
9453 | void set_curr_task(int cpu, struct task_struct *p) | |
9454 | { | |
9455 | cpu_curr(cpu) = p; | |
9456 | } | |
9457 | ||
9458 | #endif | |
9459 | ||
9460 | #ifdef CONFIG_FAIR_GROUP_SCHED | |
9461 | static void free_fair_sched_group(struct task_group *tg) | |
9462 | { | |
9463 | int i; | |
9464 | ||
9465 | for_each_possible_cpu(i) { | |
9466 | if (tg->cfs_rq) | |
9467 | kfree(tg->cfs_rq[i]); | |
9468 | if (tg->se) | |
9469 | kfree(tg->se[i]); | |
9470 | } | |
9471 | ||
9472 | kfree(tg->cfs_rq); | |
9473 | kfree(tg->se); | |
9474 | } | |
9475 | ||
9476 | static | |
9477 | int alloc_fair_sched_group(struct task_group *tg, struct task_group *parent) | |
9478 | { | |
9479 | struct cfs_rq *cfs_rq; | |
9480 | struct sched_entity *se; | |
9481 | struct rq *rq; | |
9482 | int i; | |
9483 | ||
9484 | tg->cfs_rq = kzalloc(sizeof(cfs_rq) * nr_cpu_ids, GFP_KERNEL); | |
9485 | if (!tg->cfs_rq) | |
9486 | goto err; | |
9487 | tg->se = kzalloc(sizeof(se) * nr_cpu_ids, GFP_KERNEL); | |
9488 | if (!tg->se) | |
9489 | goto err; | |
9490 | ||
9491 | tg->shares = NICE_0_LOAD; | |
9492 | ||
9493 | for_each_possible_cpu(i) { | |
9494 | rq = cpu_rq(i); | |
9495 | ||
9496 | cfs_rq = kzalloc_node(sizeof(struct cfs_rq), | |
9497 | GFP_KERNEL, cpu_to_node(i)); | |
9498 | if (!cfs_rq) | |
9499 | goto err; | |
9500 | ||
9501 | se = kzalloc_node(sizeof(struct sched_entity), | |
9502 | GFP_KERNEL, cpu_to_node(i)); | |
9503 | if (!se) | |
9504 | goto err; | |
9505 | ||
9506 | init_tg_cfs_entry(tg, cfs_rq, se, i, 0, parent->se[i]); | |
9507 | } | |
9508 | ||
9509 | return 1; | |
9510 | ||
9511 | err: | |
9512 | return 0; | |
9513 | } | |
9514 | ||
9515 | static inline void register_fair_sched_group(struct task_group *tg, int cpu) | |
9516 | { | |
9517 | list_add_rcu(&tg->cfs_rq[cpu]->leaf_cfs_rq_list, | |
9518 | &cpu_rq(cpu)->leaf_cfs_rq_list); | |
9519 | } | |
9520 | ||
9521 | static inline void unregister_fair_sched_group(struct task_group *tg, int cpu) | |
9522 | { | |
9523 | list_del_rcu(&tg->cfs_rq[cpu]->leaf_cfs_rq_list); | |
9524 | } | |
9525 | #else /* !CONFG_FAIR_GROUP_SCHED */ | |
9526 | static inline void free_fair_sched_group(struct task_group *tg) | |
9527 | { | |
9528 | } | |
9529 | ||
9530 | static inline | |
9531 | int alloc_fair_sched_group(struct task_group *tg, struct task_group *parent) | |
9532 | { | |
9533 | return 1; | |
9534 | } | |
9535 | ||
9536 | static inline void register_fair_sched_group(struct task_group *tg, int cpu) | |
9537 | { | |
9538 | } | |
9539 | ||
9540 | static inline void unregister_fair_sched_group(struct task_group *tg, int cpu) | |
9541 | { | |
9542 | } | |
9543 | #endif /* CONFIG_FAIR_GROUP_SCHED */ | |
9544 | ||
9545 | #ifdef CONFIG_RT_GROUP_SCHED | |
9546 | static void free_rt_sched_group(struct task_group *tg) | |
9547 | { | |
9548 | int i; | |
9549 | ||
9550 | destroy_rt_bandwidth(&tg->rt_bandwidth); | |
9551 | ||
9552 | for_each_possible_cpu(i) { | |
9553 | if (tg->rt_rq) | |
9554 | kfree(tg->rt_rq[i]); | |
9555 | if (tg->rt_se) | |
9556 | kfree(tg->rt_se[i]); | |
9557 | } | |
9558 | ||
9559 | kfree(tg->rt_rq); | |
9560 | kfree(tg->rt_se); | |
9561 | } | |
9562 | ||
9563 | static | |
9564 | int alloc_rt_sched_group(struct task_group *tg, struct task_group *parent) | |
9565 | { | |
9566 | struct rt_rq *rt_rq; | |
9567 | struct sched_rt_entity *rt_se; | |
9568 | struct rq *rq; | |
9569 | int i; | |
9570 | ||
9571 | tg->rt_rq = kzalloc(sizeof(rt_rq) * nr_cpu_ids, GFP_KERNEL); | |
9572 | if (!tg->rt_rq) | |
9573 | goto err; | |
9574 | tg->rt_se = kzalloc(sizeof(rt_se) * nr_cpu_ids, GFP_KERNEL); | |
9575 | if (!tg->rt_se) | |
9576 | goto err; | |
9577 | ||
9578 | init_rt_bandwidth(&tg->rt_bandwidth, | |
9579 | ktime_to_ns(def_rt_bandwidth.rt_period), 0); | |
9580 | ||
9581 | for_each_possible_cpu(i) { | |
9582 | rq = cpu_rq(i); | |
9583 | ||
9584 | rt_rq = kzalloc_node(sizeof(struct rt_rq), | |
9585 | GFP_KERNEL, cpu_to_node(i)); | |
9586 | if (!rt_rq) | |
9587 | goto err; | |
9588 | ||
9589 | rt_se = kzalloc_node(sizeof(struct sched_rt_entity), | |
9590 | GFP_KERNEL, cpu_to_node(i)); | |
9591 | if (!rt_se) | |
9592 | goto err; | |
9593 | ||
9594 | init_tg_rt_entry(tg, rt_rq, rt_se, i, 0, parent->rt_se[i]); | |
9595 | } | |
9596 | ||
9597 | return 1; | |
9598 | ||
9599 | err: | |
9600 | return 0; | |
9601 | } | |
9602 | ||
9603 | static inline void register_rt_sched_group(struct task_group *tg, int cpu) | |
9604 | { | |
9605 | list_add_rcu(&tg->rt_rq[cpu]->leaf_rt_rq_list, | |
9606 | &cpu_rq(cpu)->leaf_rt_rq_list); | |
9607 | } | |
9608 | ||
9609 | static inline void unregister_rt_sched_group(struct task_group *tg, int cpu) | |
9610 | { | |
9611 | list_del_rcu(&tg->rt_rq[cpu]->leaf_rt_rq_list); | |
9612 | } | |
9613 | #else /* !CONFIG_RT_GROUP_SCHED */ | |
9614 | static inline void free_rt_sched_group(struct task_group *tg) | |
9615 | { | |
9616 | } | |
9617 | ||
9618 | static inline | |
9619 | int alloc_rt_sched_group(struct task_group *tg, struct task_group *parent) | |
9620 | { | |
9621 | return 1; | |
9622 | } | |
9623 | ||
9624 | static inline void register_rt_sched_group(struct task_group *tg, int cpu) | |
9625 | { | |
9626 | } | |
9627 | ||
9628 | static inline void unregister_rt_sched_group(struct task_group *tg, int cpu) | |
9629 | { | |
9630 | } | |
9631 | #endif /* CONFIG_RT_GROUP_SCHED */ | |
9632 | ||
9633 | #ifdef CONFIG_GROUP_SCHED | |
9634 | static void free_sched_group(struct task_group *tg) | |
9635 | { | |
9636 | free_fair_sched_group(tg); | |
9637 | free_rt_sched_group(tg); | |
9638 | kfree(tg); | |
9639 | } | |
9640 | ||
9641 | /* allocate runqueue etc for a new task group */ | |
9642 | struct task_group *sched_create_group(struct task_group *parent) | |
9643 | { | |
9644 | struct task_group *tg; | |
9645 | unsigned long flags; | |
9646 | int i; | |
9647 | ||
9648 | tg = kzalloc(sizeof(*tg), GFP_KERNEL); | |
9649 | if (!tg) | |
9650 | return ERR_PTR(-ENOMEM); | |
9651 | ||
9652 | if (!alloc_fair_sched_group(tg, parent)) | |
9653 | goto err; | |
9654 | ||
9655 | if (!alloc_rt_sched_group(tg, parent)) | |
9656 | goto err; | |
9657 | ||
9658 | spin_lock_irqsave(&task_group_lock, flags); | |
9659 | for_each_possible_cpu(i) { | |
9660 | register_fair_sched_group(tg, i); | |
9661 | register_rt_sched_group(tg, i); | |
9662 | } | |
9663 | list_add_rcu(&tg->list, &task_groups); | |
9664 | ||
9665 | WARN_ON(!parent); /* root should already exist */ | |
9666 | ||
9667 | tg->parent = parent; | |
9668 | INIT_LIST_HEAD(&tg->children); | |
9669 | list_add_rcu(&tg->siblings, &parent->children); | |
9670 | spin_unlock_irqrestore(&task_group_lock, flags); | |
9671 | ||
9672 | return tg; | |
9673 | ||
9674 | err: | |
9675 | free_sched_group(tg); | |
9676 | return ERR_PTR(-ENOMEM); | |
9677 | } | |
9678 | ||
9679 | /* rcu callback to free various structures associated with a task group */ | |
9680 | static void free_sched_group_rcu(struct rcu_head *rhp) | |
9681 | { | |
9682 | /* now it should be safe to free those cfs_rqs */ | |
9683 | free_sched_group(container_of(rhp, struct task_group, rcu)); | |
9684 | } | |
9685 | ||
9686 | /* Destroy runqueue etc associated with a task group */ | |
9687 | void sched_destroy_group(struct task_group *tg) | |
9688 | { | |
9689 | unsigned long flags; | |
9690 | int i; | |
9691 | ||
9692 | spin_lock_irqsave(&task_group_lock, flags); | |
9693 | for_each_possible_cpu(i) { | |
9694 | unregister_fair_sched_group(tg, i); | |
9695 | unregister_rt_sched_group(tg, i); | |
9696 | } | |
9697 | list_del_rcu(&tg->list); | |
9698 | list_del_rcu(&tg->siblings); | |
9699 | spin_unlock_irqrestore(&task_group_lock, flags); | |
9700 | ||
9701 | /* wait for possible concurrent references to cfs_rqs complete */ | |
9702 | call_rcu(&tg->rcu, free_sched_group_rcu); | |
9703 | } | |
9704 | ||
9705 | /* change task's runqueue when it moves between groups. | |
9706 | * The caller of this function should have put the task in its new group | |
9707 | * by now. This function just updates tsk->se.cfs_rq and tsk->se.parent to | |
9708 | * reflect its new group. | |
9709 | */ | |
9710 | void sched_move_task(struct task_struct *tsk) | |
9711 | { | |
9712 | int on_rq, running; | |
9713 | unsigned long flags; | |
9714 | struct rq *rq; | |
9715 | ||
9716 | rq = task_rq_lock(tsk, &flags); | |
9717 | ||
9718 | update_rq_clock(rq); | |
9719 | ||
9720 | running = task_current(rq, tsk); | |
9721 | on_rq = tsk->se.on_rq; | |
9722 | ||
9723 | if (on_rq) | |
9724 | dequeue_task(rq, tsk, 0); | |
9725 | if (unlikely(running)) | |
9726 | tsk->sched_class->put_prev_task(rq, tsk); | |
9727 | ||
9728 | set_task_rq(tsk, task_cpu(tsk)); | |
9729 | ||
9730 | #ifdef CONFIG_FAIR_GROUP_SCHED | |
9731 | if (tsk->sched_class->moved_group) | |
9732 | tsk->sched_class->moved_group(tsk); | |
9733 | #endif | |
9734 | ||
9735 | if (unlikely(running)) | |
9736 | tsk->sched_class->set_curr_task(rq); | |
9737 | if (on_rq) | |
9738 | enqueue_task(rq, tsk, 0); | |
9739 | ||
9740 | task_rq_unlock(rq, &flags); | |
9741 | } | |
9742 | #endif /* CONFIG_GROUP_SCHED */ | |
9743 | ||
9744 | #ifdef CONFIG_FAIR_GROUP_SCHED | |
9745 | static void __set_se_shares(struct sched_entity *se, unsigned long shares) | |
9746 | { | |
9747 | struct cfs_rq *cfs_rq = se->cfs_rq; | |
9748 | int on_rq; | |
9749 | ||
9750 | on_rq = se->on_rq; | |
9751 | if (on_rq) | |
9752 | dequeue_entity(cfs_rq, se, 0); | |
9753 | ||
9754 | se->load.weight = shares; | |
9755 | se->load.inv_weight = 0; | |
9756 | ||
9757 | if (on_rq) | |
9758 | enqueue_entity(cfs_rq, se, 0); | |
9759 | } | |
9760 | ||
9761 | static void set_se_shares(struct sched_entity *se, unsigned long shares) | |
9762 | { | |
9763 | struct cfs_rq *cfs_rq = se->cfs_rq; | |
9764 | struct rq *rq = cfs_rq->rq; | |
9765 | unsigned long flags; | |
9766 | ||
9767 | spin_lock_irqsave(&rq->lock, flags); | |
9768 | __set_se_shares(se, shares); | |
9769 | spin_unlock_irqrestore(&rq->lock, flags); | |
9770 | } | |
9771 | ||
9772 | static DEFINE_MUTEX(shares_mutex); | |
9773 | ||
9774 | int sched_group_set_shares(struct task_group *tg, unsigned long shares) | |
9775 | { | |
9776 | int i; | |
9777 | unsigned long flags; | |
9778 | ||
9779 | /* | |
9780 | * We can't change the weight of the root cgroup. | |
9781 | */ | |
9782 | if (!tg->se[0]) | |
9783 | return -EINVAL; | |
9784 | ||
9785 | if (shares < MIN_SHARES) | |
9786 | shares = MIN_SHARES; | |
9787 | else if (shares > MAX_SHARES) | |
9788 | shares = MAX_SHARES; | |
9789 | ||
9790 | mutex_lock(&shares_mutex); | |
9791 | if (tg->shares == shares) | |
9792 | goto done; | |
9793 | ||
9794 | spin_lock_irqsave(&task_group_lock, flags); | |
9795 | for_each_possible_cpu(i) | |
9796 | unregister_fair_sched_group(tg, i); | |
9797 | list_del_rcu(&tg->siblings); | |
9798 | spin_unlock_irqrestore(&task_group_lock, flags); | |
9799 | ||
9800 | /* wait for any ongoing reference to this group to finish */ | |
9801 | synchronize_sched(); | |
9802 | ||
9803 | /* | |
9804 | * Now we are free to modify the group's share on each cpu | |
9805 | * w/o tripping rebalance_share or load_balance_fair. | |
9806 | */ | |
9807 | tg->shares = shares; | |
9808 | for_each_possible_cpu(i) { | |
9809 | /* | |
9810 | * force a rebalance | |
9811 | */ | |
9812 | cfs_rq_set_shares(tg->cfs_rq[i], 0); | |
9813 | set_se_shares(tg->se[i], shares); | |
9814 | } | |
9815 | ||
9816 | /* | |
9817 | * Enable load balance activity on this group, by inserting it back on | |
9818 | * each cpu's rq->leaf_cfs_rq_list. | |
9819 | */ | |
9820 | spin_lock_irqsave(&task_group_lock, flags); | |
9821 | for_each_possible_cpu(i) | |
9822 | register_fair_sched_group(tg, i); | |
9823 | list_add_rcu(&tg->siblings, &tg->parent->children); | |
9824 | spin_unlock_irqrestore(&task_group_lock, flags); | |
9825 | done: | |
9826 | mutex_unlock(&shares_mutex); | |
9827 | return 0; | |
9828 | } | |
9829 | ||
9830 | unsigned long sched_group_shares(struct task_group *tg) | |
9831 | { | |
9832 | return tg->shares; | |
9833 | } | |
9834 | #endif | |
9835 | ||
9836 | #ifdef CONFIG_RT_GROUP_SCHED | |
9837 | /* | |
9838 | * Ensure that the real time constraints are schedulable. | |
9839 | */ | |
9840 | static DEFINE_MUTEX(rt_constraints_mutex); | |
9841 | ||
9842 | static unsigned long to_ratio(u64 period, u64 runtime) | |
9843 | { | |
9844 | if (runtime == RUNTIME_INF) | |
9845 | return 1ULL << 20; | |
9846 | ||
9847 | return div64_u64(runtime << 20, period); | |
9848 | } | |
9849 | ||
9850 | /* Must be called with tasklist_lock held */ | |
9851 | static inline int tg_has_rt_tasks(struct task_group *tg) | |
9852 | { | |
9853 | struct task_struct *g, *p; | |
9854 | ||
9855 | do_each_thread(g, p) { | |
9856 | if (rt_task(p) && rt_rq_of_se(&p->rt)->tg == tg) | |
9857 | return 1; | |
9858 | } while_each_thread(g, p); | |
9859 | ||
9860 | return 0; | |
9861 | } | |
9862 | ||
9863 | struct rt_schedulable_data { | |
9864 | struct task_group *tg; | |
9865 | u64 rt_period; | |
9866 | u64 rt_runtime; | |
9867 | }; | |
9868 | ||
9869 | static int tg_schedulable(struct task_group *tg, void *data) | |
9870 | { | |
9871 | struct rt_schedulable_data *d = data; | |
9872 | struct task_group *child; | |
9873 | unsigned long total, sum = 0; | |
9874 | u64 period, runtime; | |
9875 | ||
9876 | period = ktime_to_ns(tg->rt_bandwidth.rt_period); | |
9877 | runtime = tg->rt_bandwidth.rt_runtime; | |
9878 | ||
9879 | if (tg == d->tg) { | |
9880 | period = d->rt_period; | |
9881 | runtime = d->rt_runtime; | |
9882 | } | |
9883 | ||
9884 | #ifdef CONFIG_USER_SCHED | |
9885 | if (tg == &root_task_group) { | |
9886 | period = global_rt_period(); | |
9887 | runtime = global_rt_runtime(); | |
9888 | } | |
9889 | #endif | |
9890 | ||
9891 | /* | |
9892 | * Cannot have more runtime than the period. | |
9893 | */ | |
9894 | if (runtime > period && runtime != RUNTIME_INF) | |
9895 | return -EINVAL; | |
9896 | ||
9897 | /* | |
9898 | * Ensure we don't starve existing RT tasks. | |
9899 | */ | |
9900 | if (rt_bandwidth_enabled() && !runtime && tg_has_rt_tasks(tg)) | |
9901 | return -EBUSY; | |
9902 | ||
9903 | total = to_ratio(period, runtime); | |
9904 | ||
9905 | /* | |
9906 | * Nobody can have more than the global setting allows. | |
9907 | */ | |
9908 | if (total > to_ratio(global_rt_period(), global_rt_runtime())) | |
9909 | return -EINVAL; | |
9910 | ||
9911 | /* | |
9912 | * The sum of our children's runtime should not exceed our own. | |
9913 | */ | |
9914 | list_for_each_entry_rcu(child, &tg->children, siblings) { | |
9915 | period = ktime_to_ns(child->rt_bandwidth.rt_period); | |
9916 | runtime = child->rt_bandwidth.rt_runtime; | |
9917 | ||
9918 | if (child == d->tg) { | |
9919 | period = d->rt_period; | |
9920 | runtime = d->rt_runtime; | |
9921 | } | |
9922 | ||
9923 | sum += to_ratio(period, runtime); | |
9924 | } | |
9925 | ||
9926 | if (sum > total) | |
9927 | return -EINVAL; | |
9928 | ||
9929 | return 0; | |
9930 | } | |
9931 | ||
9932 | static int __rt_schedulable(struct task_group *tg, u64 period, u64 runtime) | |
9933 | { | |
9934 | struct rt_schedulable_data data = { | |
9935 | .tg = tg, | |
9936 | .rt_period = period, | |
9937 | .rt_runtime = runtime, | |
9938 | }; | |
9939 | ||
9940 | return walk_tg_tree(tg_schedulable, tg_nop, &data); | |
9941 | } | |
9942 | ||
9943 | static int tg_set_bandwidth(struct task_group *tg, | |
9944 | u64 rt_period, u64 rt_runtime) | |
9945 | { | |
9946 | int i, err = 0; | |
9947 | ||
9948 | mutex_lock(&rt_constraints_mutex); | |
9949 | read_lock(&tasklist_lock); | |
9950 | err = __rt_schedulable(tg, rt_period, rt_runtime); | |
9951 | if (err) | |
9952 | goto unlock; | |
9953 | ||
9954 | spin_lock_irq(&tg->rt_bandwidth.rt_runtime_lock); | |
9955 | tg->rt_bandwidth.rt_period = ns_to_ktime(rt_period); | |
9956 | tg->rt_bandwidth.rt_runtime = rt_runtime; | |
9957 | ||
9958 | for_each_possible_cpu(i) { | |
9959 | struct rt_rq *rt_rq = tg->rt_rq[i]; | |
9960 | ||
9961 | spin_lock(&rt_rq->rt_runtime_lock); | |
9962 | rt_rq->rt_runtime = rt_runtime; | |
9963 | spin_unlock(&rt_rq->rt_runtime_lock); | |
9964 | } | |
9965 | spin_unlock_irq(&tg->rt_bandwidth.rt_runtime_lock); | |
9966 | unlock: | |
9967 | read_unlock(&tasklist_lock); | |
9968 | mutex_unlock(&rt_constraints_mutex); | |
9969 | ||
9970 | return err; | |
9971 | } | |
9972 | ||
9973 | int sched_group_set_rt_runtime(struct task_group *tg, long rt_runtime_us) | |
9974 | { | |
9975 | u64 rt_runtime, rt_period; | |
9976 | ||
9977 | rt_period = ktime_to_ns(tg->rt_bandwidth.rt_period); | |
9978 | rt_runtime = (u64)rt_runtime_us * NSEC_PER_USEC; | |
9979 | if (rt_runtime_us < 0) | |
9980 | rt_runtime = RUNTIME_INF; | |
9981 | ||
9982 | return tg_set_bandwidth(tg, rt_period, rt_runtime); | |
9983 | } | |
9984 | ||
9985 | long sched_group_rt_runtime(struct task_group *tg) | |
9986 | { | |
9987 | u64 rt_runtime_us; | |
9988 | ||
9989 | if (tg->rt_bandwidth.rt_runtime == RUNTIME_INF) | |
9990 | return -1; | |
9991 | ||
9992 | rt_runtime_us = tg->rt_bandwidth.rt_runtime; | |
9993 | do_div(rt_runtime_us, NSEC_PER_USEC); | |
9994 | return rt_runtime_us; | |
9995 | } | |
9996 | ||
9997 | int sched_group_set_rt_period(struct task_group *tg, long rt_period_us) | |
9998 | { | |
9999 | u64 rt_runtime, rt_period; | |
10000 | ||
10001 | rt_period = (u64)rt_period_us * NSEC_PER_USEC; | |
10002 | rt_runtime = tg->rt_bandwidth.rt_runtime; | |
10003 | ||
10004 | if (rt_period == 0) | |
10005 | return -EINVAL; | |
10006 | ||
10007 | return tg_set_bandwidth(tg, rt_period, rt_runtime); | |
10008 | } | |
10009 | ||
10010 | long sched_group_rt_period(struct task_group *tg) | |
10011 | { | |
10012 | u64 rt_period_us; | |
10013 | ||
10014 | rt_period_us = ktime_to_ns(tg->rt_bandwidth.rt_period); | |
10015 | do_div(rt_period_us, NSEC_PER_USEC); | |
10016 | return rt_period_us; | |
10017 | } | |
10018 | ||
10019 | static int sched_rt_global_constraints(void) | |
10020 | { | |
10021 | u64 runtime, period; | |
10022 | int ret = 0; | |
10023 | ||
10024 | if (sysctl_sched_rt_period <= 0) | |
10025 | return -EINVAL; | |
10026 | ||
10027 | runtime = global_rt_runtime(); | |
10028 | period = global_rt_period(); | |
10029 | ||
10030 | /* | |
10031 | * Sanity check on the sysctl variables. | |
10032 | */ | |
10033 | if (runtime > period && runtime != RUNTIME_INF) | |
10034 | return -EINVAL; | |
10035 | ||
10036 | mutex_lock(&rt_constraints_mutex); | |
10037 | read_lock(&tasklist_lock); | |
10038 | ret = __rt_schedulable(NULL, 0, 0); | |
10039 | read_unlock(&tasklist_lock); | |
10040 | mutex_unlock(&rt_constraints_mutex); | |
10041 | ||
10042 | return ret; | |
10043 | } | |
10044 | ||
10045 | int sched_rt_can_attach(struct task_group *tg, struct task_struct *tsk) | |
10046 | { | |
10047 | /* Don't accept realtime tasks when there is no way for them to run */ | |
10048 | if (rt_task(tsk) && tg->rt_bandwidth.rt_runtime == 0) | |
10049 | return 0; | |
10050 | ||
10051 | return 1; | |
10052 | } | |
10053 | ||
10054 | #else /* !CONFIG_RT_GROUP_SCHED */ | |
10055 | static int sched_rt_global_constraints(void) | |
10056 | { | |
10057 | unsigned long flags; | |
10058 | int i; | |
10059 | ||
10060 | if (sysctl_sched_rt_period <= 0) | |
10061 | return -EINVAL; | |
10062 | ||
10063 | /* | |
10064 | * There's always some RT tasks in the root group | |
10065 | * -- migration, kstopmachine etc.. | |
10066 | */ | |
10067 | if (sysctl_sched_rt_runtime == 0) | |
10068 | return -EBUSY; | |
10069 | ||
10070 | spin_lock_irqsave(&def_rt_bandwidth.rt_runtime_lock, flags); | |
10071 | for_each_possible_cpu(i) { | |
10072 | struct rt_rq *rt_rq = &cpu_rq(i)->rt; | |
10073 | ||
10074 | spin_lock(&rt_rq->rt_runtime_lock); | |
10075 | rt_rq->rt_runtime = global_rt_runtime(); | |
10076 | spin_unlock(&rt_rq->rt_runtime_lock); | |
10077 | } | |
10078 | spin_unlock_irqrestore(&def_rt_bandwidth.rt_runtime_lock, flags); | |
10079 | ||
10080 | return 0; | |
10081 | } | |
10082 | #endif /* CONFIG_RT_GROUP_SCHED */ | |
10083 | ||
10084 | int sched_rt_handler(struct ctl_table *table, int write, | |
10085 | struct file *filp, void __user *buffer, size_t *lenp, | |
10086 | loff_t *ppos) | |
10087 | { | |
10088 | int ret; | |
10089 | int old_period, old_runtime; | |
10090 | static DEFINE_MUTEX(mutex); | |
10091 | ||
10092 | mutex_lock(&mutex); | |
10093 | old_period = sysctl_sched_rt_period; | |
10094 | old_runtime = sysctl_sched_rt_runtime; | |
10095 | ||
10096 | ret = proc_dointvec(table, write, filp, buffer, lenp, ppos); | |
10097 | ||
10098 | if (!ret && write) { | |
10099 | ret = sched_rt_global_constraints(); | |
10100 | if (ret) { | |
10101 | sysctl_sched_rt_period = old_period; | |
10102 | sysctl_sched_rt_runtime = old_runtime; | |
10103 | } else { | |
10104 | def_rt_bandwidth.rt_runtime = global_rt_runtime(); | |
10105 | def_rt_bandwidth.rt_period = | |
10106 | ns_to_ktime(global_rt_period()); | |
10107 | } | |
10108 | } | |
10109 | mutex_unlock(&mutex); | |
10110 | ||
10111 | return ret; | |
10112 | } | |
10113 | ||
10114 | #ifdef CONFIG_CGROUP_SCHED | |
10115 | ||
10116 | /* return corresponding task_group object of a cgroup */ | |
10117 | static inline struct task_group *cgroup_tg(struct cgroup *cgrp) | |
10118 | { | |
10119 | return container_of(cgroup_subsys_state(cgrp, cpu_cgroup_subsys_id), | |
10120 | struct task_group, css); | |
10121 | } | |
10122 | ||
10123 | static struct cgroup_subsys_state * | |
10124 | cpu_cgroup_create(struct cgroup_subsys *ss, struct cgroup *cgrp) | |
10125 | { | |
10126 | struct task_group *tg, *parent; | |
10127 | ||
10128 | if (!cgrp->parent) { | |
10129 | /* This is early initialization for the top cgroup */ | |
10130 | return &init_task_group.css; | |
10131 | } | |
10132 | ||
10133 | parent = cgroup_tg(cgrp->parent); | |
10134 | tg = sched_create_group(parent); | |
10135 | if (IS_ERR(tg)) | |
10136 | return ERR_PTR(-ENOMEM); | |
10137 | ||
10138 | return &tg->css; | |
10139 | } | |
10140 | ||
10141 | static void | |
10142 | cpu_cgroup_destroy(struct cgroup_subsys *ss, struct cgroup *cgrp) | |
10143 | { | |
10144 | struct task_group *tg = cgroup_tg(cgrp); | |
10145 | ||
10146 | sched_destroy_group(tg); | |
10147 | } | |
10148 | ||
10149 | static int | |
10150 | cpu_cgroup_can_attach(struct cgroup_subsys *ss, struct cgroup *cgrp, | |
10151 | struct task_struct *tsk) | |
10152 | { | |
10153 | #ifdef CONFIG_RT_GROUP_SCHED | |
10154 | if (!sched_rt_can_attach(cgroup_tg(cgrp), tsk)) | |
10155 | return -EINVAL; | |
10156 | #else | |
10157 | /* We don't support RT-tasks being in separate groups */ | |
10158 | if (tsk->sched_class != &fair_sched_class) | |
10159 | return -EINVAL; | |
10160 | #endif | |
10161 | ||
10162 | return 0; | |
10163 | } | |
10164 | ||
10165 | static void | |
10166 | cpu_cgroup_attach(struct cgroup_subsys *ss, struct cgroup *cgrp, | |
10167 | struct cgroup *old_cont, struct task_struct *tsk) | |
10168 | { | |
10169 | sched_move_task(tsk); | |
10170 | } | |
10171 | ||
10172 | #ifdef CONFIG_FAIR_GROUP_SCHED | |
10173 | static int cpu_shares_write_u64(struct cgroup *cgrp, struct cftype *cftype, | |
10174 | u64 shareval) | |
10175 | { | |
10176 | return sched_group_set_shares(cgroup_tg(cgrp), shareval); | |
10177 | } | |
10178 | ||
10179 | static u64 cpu_shares_read_u64(struct cgroup *cgrp, struct cftype *cft) | |
10180 | { | |
10181 | struct task_group *tg = cgroup_tg(cgrp); | |
10182 | ||
10183 | return (u64) tg->shares; | |
10184 | } | |
10185 | #endif /* CONFIG_FAIR_GROUP_SCHED */ | |
10186 | ||
10187 | #ifdef CONFIG_RT_GROUP_SCHED | |
10188 | static int cpu_rt_runtime_write(struct cgroup *cgrp, struct cftype *cft, | |
10189 | s64 val) | |
10190 | { | |
10191 | return sched_group_set_rt_runtime(cgroup_tg(cgrp), val); | |
10192 | } | |
10193 | ||
10194 | static s64 cpu_rt_runtime_read(struct cgroup *cgrp, struct cftype *cft) | |
10195 | { | |
10196 | return sched_group_rt_runtime(cgroup_tg(cgrp)); | |
10197 | } | |
10198 | ||
10199 | static int cpu_rt_period_write_uint(struct cgroup *cgrp, struct cftype *cftype, | |
10200 | u64 rt_period_us) | |
10201 | { | |
10202 | return sched_group_set_rt_period(cgroup_tg(cgrp), rt_period_us); | |
10203 | } | |
10204 | ||
10205 | static u64 cpu_rt_period_read_uint(struct cgroup *cgrp, struct cftype *cft) | |
10206 | { | |
10207 | return sched_group_rt_period(cgroup_tg(cgrp)); | |
10208 | } | |
10209 | #endif /* CONFIG_RT_GROUP_SCHED */ | |
10210 | ||
10211 | static struct cftype cpu_files[] = { | |
10212 | #ifdef CONFIG_FAIR_GROUP_SCHED | |
10213 | { | |
10214 | .name = "shares", | |
10215 | .read_u64 = cpu_shares_read_u64, | |
10216 | .write_u64 = cpu_shares_write_u64, | |
10217 | }, | |
10218 | #endif | |
10219 | #ifdef CONFIG_RT_GROUP_SCHED | |
10220 | { | |
10221 | .name = "rt_runtime_us", | |
10222 | .read_s64 = cpu_rt_runtime_read, | |
10223 | .write_s64 = cpu_rt_runtime_write, | |
10224 | }, | |
10225 | { | |
10226 | .name = "rt_period_us", | |
10227 | .read_u64 = cpu_rt_period_read_uint, | |
10228 | .write_u64 = cpu_rt_period_write_uint, | |
10229 | }, | |
10230 | #endif | |
10231 | }; | |
10232 | ||
10233 | static int cpu_cgroup_populate(struct cgroup_subsys *ss, struct cgroup *cont) | |
10234 | { | |
10235 | return cgroup_add_files(cont, ss, cpu_files, ARRAY_SIZE(cpu_files)); | |
10236 | } | |
10237 | ||
10238 | struct cgroup_subsys cpu_cgroup_subsys = { | |
10239 | .name = "cpu", | |
10240 | .create = cpu_cgroup_create, | |
10241 | .destroy = cpu_cgroup_destroy, | |
10242 | .can_attach = cpu_cgroup_can_attach, | |
10243 | .attach = cpu_cgroup_attach, | |
10244 | .populate = cpu_cgroup_populate, | |
10245 | .subsys_id = cpu_cgroup_subsys_id, | |
10246 | .early_init = 1, | |
10247 | }; | |
10248 | ||
10249 | #endif /* CONFIG_CGROUP_SCHED */ | |
10250 | ||
10251 | #ifdef CONFIG_CGROUP_CPUACCT | |
10252 | ||
10253 | /* | |
10254 | * CPU accounting code for task groups. | |
10255 | * | |
10256 | * Based on the work by Paul Menage (menage@google.com) and Balbir Singh | |
10257 | * (balbir@in.ibm.com). | |
10258 | */ | |
10259 | ||
10260 | /* track cpu usage of a group of tasks and its child groups */ | |
10261 | struct cpuacct { | |
10262 | struct cgroup_subsys_state css; | |
10263 | /* cpuusage holds pointer to a u64-type object on every cpu */ | |
10264 | u64 *cpuusage; | |
10265 | struct percpu_counter cpustat[CPUACCT_STAT_NSTATS]; | |
10266 | struct cpuacct *parent; | |
10267 | }; | |
10268 | ||
10269 | struct cgroup_subsys cpuacct_subsys; | |
10270 | ||
10271 | /* return cpu accounting group corresponding to this container */ | |
10272 | static inline struct cpuacct *cgroup_ca(struct cgroup *cgrp) | |
10273 | { | |
10274 | return container_of(cgroup_subsys_state(cgrp, cpuacct_subsys_id), | |
10275 | struct cpuacct, css); | |
10276 | } | |
10277 | ||
10278 | /* return cpu accounting group to which this task belongs */ | |
10279 | static inline struct cpuacct *task_ca(struct task_struct *tsk) | |
10280 | { | |
10281 | return container_of(task_subsys_state(tsk, cpuacct_subsys_id), | |
10282 | struct cpuacct, css); | |
10283 | } | |
10284 | ||
10285 | /* create a new cpu accounting group */ | |
10286 | static struct cgroup_subsys_state *cpuacct_create( | |
10287 | struct cgroup_subsys *ss, struct cgroup *cgrp) | |
10288 | { | |
10289 | struct cpuacct *ca = kzalloc(sizeof(*ca), GFP_KERNEL); | |
10290 | int i; | |
10291 | ||
10292 | if (!ca) | |
10293 | goto out; | |
10294 | ||
10295 | ca->cpuusage = alloc_percpu(u64); | |
10296 | if (!ca->cpuusage) | |
10297 | goto out_free_ca; | |
10298 | ||
10299 | for (i = 0; i < CPUACCT_STAT_NSTATS; i++) | |
10300 | if (percpu_counter_init(&ca->cpustat[i], 0)) | |
10301 | goto out_free_counters; | |
10302 | ||
10303 | if (cgrp->parent) | |
10304 | ca->parent = cgroup_ca(cgrp->parent); | |
10305 | ||
10306 | return &ca->css; | |
10307 | ||
10308 | out_free_counters: | |
10309 | while (--i >= 0) | |
10310 | percpu_counter_destroy(&ca->cpustat[i]); | |
10311 | free_percpu(ca->cpuusage); | |
10312 | out_free_ca: | |
10313 | kfree(ca); | |
10314 | out: | |
10315 | return ERR_PTR(-ENOMEM); | |
10316 | } | |
10317 | ||
10318 | /* destroy an existing cpu accounting group */ | |
10319 | static void | |
10320 | cpuacct_destroy(struct cgroup_subsys *ss, struct cgroup *cgrp) | |
10321 | { | |
10322 | struct cpuacct *ca = cgroup_ca(cgrp); | |
10323 | int i; | |
10324 | ||
10325 | for (i = 0; i < CPUACCT_STAT_NSTATS; i++) | |
10326 | percpu_counter_destroy(&ca->cpustat[i]); | |
10327 | free_percpu(ca->cpuusage); | |
10328 | kfree(ca); | |
10329 | } | |
10330 | ||
10331 | static u64 cpuacct_cpuusage_read(struct cpuacct *ca, int cpu) | |
10332 | { | |
10333 | u64 *cpuusage = per_cpu_ptr(ca->cpuusage, cpu); | |
10334 | u64 data; | |
10335 | ||
10336 | #ifndef CONFIG_64BIT | |
10337 | /* | |
10338 | * Take rq->lock to make 64-bit read safe on 32-bit platforms. | |
10339 | */ | |
10340 | spin_lock_irq(&cpu_rq(cpu)->lock); | |
10341 | data = *cpuusage; | |
10342 | spin_unlock_irq(&cpu_rq(cpu)->lock); | |
10343 | #else | |
10344 | data = *cpuusage; | |
10345 | #endif | |
10346 | ||
10347 | return data; | |
10348 | } | |
10349 | ||
10350 | static void cpuacct_cpuusage_write(struct cpuacct *ca, int cpu, u64 val) | |
10351 | { | |
10352 | u64 *cpuusage = per_cpu_ptr(ca->cpuusage, cpu); | |
10353 | ||
10354 | #ifndef CONFIG_64BIT | |
10355 | /* | |
10356 | * Take rq->lock to make 64-bit write safe on 32-bit platforms. | |
10357 | */ | |
10358 | spin_lock_irq(&cpu_rq(cpu)->lock); | |
10359 | *cpuusage = val; | |
10360 | spin_unlock_irq(&cpu_rq(cpu)->lock); | |
10361 | #else | |
10362 | *cpuusage = val; | |
10363 | #endif | |
10364 | } | |
10365 | ||
10366 | /* return total cpu usage (in nanoseconds) of a group */ | |
10367 | static u64 cpuusage_read(struct cgroup *cgrp, struct cftype *cft) | |
10368 | { | |
10369 | struct cpuacct *ca = cgroup_ca(cgrp); | |
10370 | u64 totalcpuusage = 0; | |
10371 | int i; | |
10372 | ||
10373 | for_each_present_cpu(i) | |
10374 | totalcpuusage += cpuacct_cpuusage_read(ca, i); | |
10375 | ||
10376 | return totalcpuusage; | |
10377 | } | |
10378 | ||
10379 | static int cpuusage_write(struct cgroup *cgrp, struct cftype *cftype, | |
10380 | u64 reset) | |
10381 | { | |
10382 | struct cpuacct *ca = cgroup_ca(cgrp); | |
10383 | int err = 0; | |
10384 | int i; | |
10385 | ||
10386 | if (reset) { | |
10387 | err = -EINVAL; | |
10388 | goto out; | |
10389 | } | |
10390 | ||
10391 | for_each_present_cpu(i) | |
10392 | cpuacct_cpuusage_write(ca, i, 0); | |
10393 | ||
10394 | out: | |
10395 | return err; | |
10396 | } | |
10397 | ||
10398 | static int cpuacct_percpu_seq_read(struct cgroup *cgroup, struct cftype *cft, | |
10399 | struct seq_file *m) | |
10400 | { | |
10401 | struct cpuacct *ca = cgroup_ca(cgroup); | |
10402 | u64 percpu; | |
10403 | int i; | |
10404 | ||
10405 | for_each_present_cpu(i) { | |
10406 | percpu = cpuacct_cpuusage_read(ca, i); | |
10407 | seq_printf(m, "%llu ", (unsigned long long) percpu); | |
10408 | } | |
10409 | seq_printf(m, "\n"); | |
10410 | return 0; | |
10411 | } | |
10412 | ||
10413 | static const char *cpuacct_stat_desc[] = { | |
10414 | [CPUACCT_STAT_USER] = "user", | |
10415 | [CPUACCT_STAT_SYSTEM] = "system", | |
10416 | }; | |
10417 | ||
10418 | static int cpuacct_stats_show(struct cgroup *cgrp, struct cftype *cft, | |
10419 | struct cgroup_map_cb *cb) | |
10420 | { | |
10421 | struct cpuacct *ca = cgroup_ca(cgrp); | |
10422 | int i; | |
10423 | ||
10424 | for (i = 0; i < CPUACCT_STAT_NSTATS; i++) { | |
10425 | s64 val = percpu_counter_read(&ca->cpustat[i]); | |
10426 | val = cputime64_to_clock_t(val); | |
10427 | cb->fill(cb, cpuacct_stat_desc[i], val); | |
10428 | } | |
10429 | return 0; | |
10430 | } | |
10431 | ||
10432 | static struct cftype files[] = { | |
10433 | { | |
10434 | .name = "usage", | |
10435 | .read_u64 = cpuusage_read, | |
10436 | .write_u64 = cpuusage_write, | |
10437 | }, | |
10438 | { | |
10439 | .name = "usage_percpu", | |
10440 | .read_seq_string = cpuacct_percpu_seq_read, | |
10441 | }, | |
10442 | { | |
10443 | .name = "stat", | |
10444 | .read_map = cpuacct_stats_show, | |
10445 | }, | |
10446 | }; | |
10447 | ||
10448 | static int cpuacct_populate(struct cgroup_subsys *ss, struct cgroup *cgrp) | |
10449 | { | |
10450 | return cgroup_add_files(cgrp, ss, files, ARRAY_SIZE(files)); | |
10451 | } | |
10452 | ||
10453 | /* | |
10454 | * charge this task's execution time to its accounting group. | |
10455 | * | |
10456 | * called with rq->lock held. | |
10457 | */ | |
10458 | static void cpuacct_charge(struct task_struct *tsk, u64 cputime) | |
10459 | { | |
10460 | struct cpuacct *ca; | |
10461 | int cpu; | |
10462 | ||
10463 | if (unlikely(!cpuacct_subsys.active)) | |
10464 | return; | |
10465 | ||
10466 | cpu = task_cpu(tsk); | |
10467 | ||
10468 | rcu_read_lock(); | |
10469 | ||
10470 | ca = task_ca(tsk); | |
10471 | ||
10472 | for (; ca; ca = ca->parent) { | |
10473 | u64 *cpuusage = per_cpu_ptr(ca->cpuusage, cpu); | |
10474 | *cpuusage += cputime; | |
10475 | } | |
10476 | ||
10477 | rcu_read_unlock(); | |
10478 | } | |
10479 | ||
10480 | /* | |
10481 | * Charge the system/user time to the task's accounting group. | |
10482 | */ | |
10483 | static void cpuacct_update_stats(struct task_struct *tsk, | |
10484 | enum cpuacct_stat_index idx, cputime_t val) | |
10485 | { | |
10486 | struct cpuacct *ca; | |
10487 | ||
10488 | if (unlikely(!cpuacct_subsys.active)) | |
10489 | return; | |
10490 | ||
10491 | rcu_read_lock(); | |
10492 | ca = task_ca(tsk); | |
10493 | ||
10494 | do { | |
10495 | percpu_counter_add(&ca->cpustat[idx], val); | |
10496 | ca = ca->parent; | |
10497 | } while (ca); | |
10498 | rcu_read_unlock(); | |
10499 | } | |
10500 | ||
10501 | struct cgroup_subsys cpuacct_subsys = { | |
10502 | .name = "cpuacct", | |
10503 | .create = cpuacct_create, | |
10504 | .destroy = cpuacct_destroy, | |
10505 | .populate = cpuacct_populate, | |
10506 | .subsys_id = cpuacct_subsys_id, | |
10507 | }; | |
10508 | #endif /* CONFIG_CGROUP_CPUACCT */ |