diff options
Diffstat (limited to 'kernel/sched.c')
-rw-r--r-- | kernel/sched.c | 9443 |
1 files changed, 9443 insertions, 0 deletions
diff --git a/kernel/sched.c b/kernel/sched.c new file mode 100644 index 00000000000..f6cf5cbc64b --- /dev/null +++ b/kernel/sched.c | |||
@@ -0,0 +1,9443 @@ | |||
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 <asm/mmu_context.h> | ||
36 | #include <linux/interrupt.h> | ||
37 | #include <linux/capability.h> | ||
38 | #include <linux/completion.h> | ||
39 | #include <linux/kernel_stat.h> | ||
40 | #include <linux/debug_locks.h> | ||
41 | #include <linux/perf_event.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/proc_fs.h> | ||
58 | #include <linux/seq_file.h> | ||
59 | #include <linux/stop_machine.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/unistd.h> | ||
67 | #include <linux/pagemap.h> | ||
68 | #include <linux/hrtimer.h> | ||
69 | #include <linux/tick.h> | ||
70 | #include <linux/debugfs.h> | ||
71 | #include <linux/ctype.h> | ||
72 | #include <linux/ftrace.h> | ||
73 | #include <linux/slab.h> | ||
74 | #include <linux/cpuacct.h> | ||
75 | |||
76 | #include <asm/tlb.h> | ||
77 | #include <asm/irq_regs.h> | ||
78 | #include <asm/mutex.h> | ||
79 | #ifdef CONFIG_PARAVIRT | ||
80 | #include <asm/paravirt.h> | ||
81 | #endif | ||
82 | |||
83 | #include "sched_cpupri.h" | ||
84 | #include "workqueue_sched.h" | ||
85 | #include "sched_autogroup.h" | ||
86 | |||
87 | #define CREATE_TRACE_POINTS | ||
88 | #include <trace/events/sched.h> | ||
89 | |||
90 | /* | ||
91 | * Convert user-nice values [ -20 ... 0 ... 19 ] | ||
92 | * to static priority [ MAX_RT_PRIO..MAX_PRIO-1 ], | ||
93 | * and back. | ||
94 | */ | ||
95 | #define NICE_TO_PRIO(nice) (MAX_RT_PRIO + (nice) + 20) | ||
96 | #define PRIO_TO_NICE(prio) ((prio) - MAX_RT_PRIO - 20) | ||
97 | #define TASK_NICE(p) PRIO_TO_NICE((p)->static_prio) | ||
98 | |||
99 | /* | ||
100 | * 'User priority' is the nice value converted to something we | ||
101 | * can work with better when scaling various scheduler parameters, | ||
102 | * it's a [ 0 ... 39 ] range. | ||
103 | */ | ||
104 | #define USER_PRIO(p) ((p)-MAX_RT_PRIO) | ||
105 | #define TASK_USER_PRIO(p) USER_PRIO((p)->static_prio) | ||
106 | #define MAX_USER_PRIO (USER_PRIO(MAX_PRIO)) | ||
107 | |||
108 | /* | ||
109 | * Helpers for converting nanosecond timing to jiffy resolution | ||
110 | */ | ||
111 | #define NS_TO_JIFFIES(TIME) ((unsigned long)(TIME) / (NSEC_PER_SEC / HZ)) | ||
112 | |||
113 | #define NICE_0_LOAD SCHED_LOAD_SCALE | ||
114 | #define NICE_0_SHIFT SCHED_LOAD_SHIFT | ||
115 | |||
116 | /* | ||
117 | * These are the 'tuning knobs' of the scheduler: | ||
118 | * | ||
119 | * default timeslice is 100 msecs (used only for SCHED_RR tasks). | ||
120 | * Timeslices get refilled after they expire. | ||
121 | */ | ||
122 | #define DEF_TIMESLICE (100 * HZ / 1000) | ||
123 | |||
124 | /* | ||
125 | * single value that denotes runtime == period, ie unlimited time. | ||
126 | */ | ||
127 | #define RUNTIME_INF ((u64)~0ULL) | ||
128 | |||
129 | static inline int rt_policy(int policy) | ||
130 | { | ||
131 | if (policy == SCHED_FIFO || policy == SCHED_RR) | ||
132 | return 1; | ||
133 | return 0; | ||
134 | } | ||
135 | |||
136 | static inline int task_has_rt_policy(struct task_struct *p) | ||
137 | { | ||
138 | return rt_policy(p->policy); | ||
139 | } | ||
140 | |||
141 | /* | ||
142 | * This is the priority-queue data structure of the RT scheduling class: | ||
143 | */ | ||
144 | struct rt_prio_array { | ||
145 | DECLARE_BITMAP(bitmap, MAX_RT_PRIO+1); /* include 1 bit for delimiter */ | ||
146 | struct list_head queue[MAX_RT_PRIO]; | ||
147 | }; | ||
148 | |||
149 | struct rt_bandwidth { | ||
150 | /* nests inside the rq lock: */ | ||
151 | raw_spinlock_t rt_runtime_lock; | ||
152 | ktime_t rt_period; | ||
153 | u64 rt_runtime; | ||
154 | struct hrtimer rt_period_timer; | ||
155 | }; | ||
156 | |||
157 | static struct rt_bandwidth def_rt_bandwidth; | ||
158 | |||
159 | static int do_sched_rt_period_timer(struct rt_bandwidth *rt_b, int overrun); | ||
160 | |||
161 | static enum hrtimer_restart sched_rt_period_timer(struct hrtimer *timer) | ||
162 | { | ||
163 | struct rt_bandwidth *rt_b = | ||
164 | container_of(timer, struct rt_bandwidth, rt_period_timer); | ||
165 | ktime_t now; | ||
166 | int overrun; | ||
167 | int idle = 0; | ||
168 | |||
169 | for (;;) { | ||
170 | now = hrtimer_cb_get_time(timer); | ||
171 | overrun = hrtimer_forward(timer, now, rt_b->rt_period); | ||
172 | |||
173 | if (!overrun) | ||
174 | break; | ||
175 | |||
176 | idle = do_sched_rt_period_timer(rt_b, overrun); | ||
177 | } | ||
178 | |||
179 | return idle ? HRTIMER_NORESTART : HRTIMER_RESTART; | ||
180 | } | ||
181 | |||
182 | static | ||
183 | void init_rt_bandwidth(struct rt_bandwidth *rt_b, u64 period, u64 runtime) | ||
184 | { | ||
185 | rt_b->rt_period = ns_to_ktime(period); | ||
186 | rt_b->rt_runtime = runtime; | ||
187 | |||
188 | raw_spin_lock_init(&rt_b->rt_runtime_lock); | ||
189 | |||
190 | hrtimer_init(&rt_b->rt_period_timer, | ||
191 | CLOCK_MONOTONIC, HRTIMER_MODE_REL); | ||
192 | rt_b->rt_period_timer.function = sched_rt_period_timer; | ||
193 | } | ||
194 | |||
195 | static inline int rt_bandwidth_enabled(void) | ||
196 | { | ||
197 | return sysctl_sched_rt_runtime >= 0; | ||
198 | } | ||
199 | |||
200 | static void start_rt_bandwidth(struct rt_bandwidth *rt_b) | ||
201 | { | ||
202 | ktime_t now; | ||
203 | |||
204 | if (!rt_bandwidth_enabled() || rt_b->rt_runtime == RUNTIME_INF) | ||
205 | return; | ||
206 | |||
207 | if (hrtimer_active(&rt_b->rt_period_timer)) | ||
208 | return; | ||
209 | |||
210 | raw_spin_lock(&rt_b->rt_runtime_lock); | ||
211 | for (;;) { | ||
212 | unsigned long delta; | ||
213 | ktime_t soft, hard; | ||
214 | |||
215 | if (hrtimer_active(&rt_b->rt_period_timer)) | ||
216 | break; | ||
217 | |||
218 | now = hrtimer_cb_get_time(&rt_b->rt_period_timer); | ||
219 | hrtimer_forward(&rt_b->rt_period_timer, now, rt_b->rt_period); | ||
220 | |||
221 | soft = hrtimer_get_softexpires(&rt_b->rt_period_timer); | ||
222 | hard = hrtimer_get_expires(&rt_b->rt_period_timer); | ||
223 | delta = ktime_to_ns(ktime_sub(hard, soft)); | ||
224 | __hrtimer_start_range_ns(&rt_b->rt_period_timer, soft, delta, | ||
225 | HRTIMER_MODE_ABS_PINNED, 0); | ||
226 | } | ||
227 | raw_spin_unlock(&rt_b->rt_runtime_lock); | ||
228 | } | ||
229 | |||
230 | #ifdef CONFIG_RT_GROUP_SCHED | ||
231 | static void destroy_rt_bandwidth(struct rt_bandwidth *rt_b) | ||
232 | { | ||
233 | hrtimer_cancel(&rt_b->rt_period_timer); | ||
234 | } | ||
235 | #endif | ||
236 | |||
237 | /* | ||
238 | * sched_domains_mutex serializes calls to init_sched_domains, | ||
239 | * detach_destroy_domains and partition_sched_domains. | ||
240 | */ | ||
241 | static DEFINE_MUTEX(sched_domains_mutex); | ||
242 | |||
243 | #ifdef CONFIG_CGROUP_SCHED | ||
244 | |||
245 | #include <linux/cgroup.h> | ||
246 | |||
247 | struct cfs_rq; | ||
248 | |||
249 | static LIST_HEAD(task_groups); | ||
250 | |||
251 | /* task group related information */ | ||
252 | struct task_group { | ||
253 | struct cgroup_subsys_state css; | ||
254 | |||
255 | #ifdef CONFIG_FAIR_GROUP_SCHED | ||
256 | /* schedulable entities of this group on each cpu */ | ||
257 | struct sched_entity **se; | ||
258 | /* runqueue "owned" by this group on each cpu */ | ||
259 | struct cfs_rq **cfs_rq; | ||
260 | unsigned long shares; | ||
261 | |||
262 | atomic_t load_weight; | ||
263 | #endif | ||
264 | |||
265 | #ifdef CONFIG_RT_GROUP_SCHED | ||
266 | struct sched_rt_entity **rt_se; | ||
267 | struct rt_rq **rt_rq; | ||
268 | |||
269 | struct rt_bandwidth rt_bandwidth; | ||
270 | #endif | ||
271 | |||
272 | struct rcu_head rcu; | ||
273 | struct list_head list; | ||
274 | |||
275 | struct task_group *parent; | ||
276 | struct list_head siblings; | ||
277 | struct list_head children; | ||
278 | |||
279 | #ifdef CONFIG_SCHED_AUTOGROUP | ||
280 | struct autogroup *autogroup; | ||
281 | #endif | ||
282 | }; | ||
283 | |||
284 | /* task_group_lock serializes the addition/removal of task groups */ | ||
285 | static DEFINE_SPINLOCK(task_group_lock); | ||
286 | |||
287 | #ifdef CONFIG_FAIR_GROUP_SCHED | ||
288 | |||
289 | # define ROOT_TASK_GROUP_LOAD NICE_0_LOAD | ||
290 | |||
291 | /* | ||
292 | * A weight of 0 or 1 can cause arithmetics problems. | ||
293 | * A weight of a cfs_rq is the sum of weights of which entities | ||
294 | * are queued on this cfs_rq, so a weight of a entity should not be | ||
295 | * too large, so as the shares value of a task group. | ||
296 | * (The default weight is 1024 - so there's no practical | ||
297 | * limitation from this.) | ||
298 | */ | ||
299 | #define MIN_SHARES (1UL << 1) | ||
300 | #define MAX_SHARES (1UL << 18) | ||
301 | |||
302 | static int root_task_group_load = ROOT_TASK_GROUP_LOAD; | ||
303 | #endif | ||
304 | |||
305 | /* Default task group. | ||
306 | * Every task in system belong to this group at bootup. | ||
307 | */ | ||
308 | struct task_group root_task_group; | ||
309 | |||
310 | #endif /* CONFIG_CGROUP_SCHED */ | ||
311 | |||
312 | /* CFS-related fields in a runqueue */ | ||
313 | struct cfs_rq { | ||
314 | struct load_weight load; | ||
315 | unsigned long nr_running; | ||
316 | |||
317 | u64 exec_clock; | ||
318 | u64 min_vruntime; | ||
319 | #ifndef CONFIG_64BIT | ||
320 | u64 min_vruntime_copy; | ||
321 | #endif | ||
322 | |||
323 | struct rb_root tasks_timeline; | ||
324 | struct rb_node *rb_leftmost; | ||
325 | |||
326 | struct list_head tasks; | ||
327 | struct list_head *balance_iterator; | ||
328 | |||
329 | /* | ||
330 | * 'curr' points to currently running entity on this cfs_rq. | ||
331 | * It is set to NULL otherwise (i.e when none are currently running). | ||
332 | */ | ||
333 | struct sched_entity *curr, *next, *last, *skip; | ||
334 | |||
335 | #ifdef CONFIG_SCHED_DEBUG | ||
336 | unsigned int nr_spread_over; | ||
337 | #endif | ||
338 | |||
339 | #ifdef CONFIG_FAIR_GROUP_SCHED | ||
340 | struct rq *rq; /* cpu runqueue to which this cfs_rq is attached */ | ||
341 | |||
342 | /* | ||
343 | * leaf cfs_rqs are those that hold tasks (lowest schedulable entity in | ||
344 | * a hierarchy). Non-leaf lrqs hold other higher schedulable entities | ||
345 | * (like users, containers etc.) | ||
346 | * | ||
347 | * leaf_cfs_rq_list ties together list of leaf cfs_rq's in a cpu. This | ||
348 | * list is used during load balance. | ||
349 | */ | ||
350 | int on_list; | ||
351 | struct list_head leaf_cfs_rq_list; | ||
352 | struct task_group *tg; /* group that "owns" this runqueue */ | ||
353 | |||
354 | #ifdef CONFIG_SMP | ||
355 | /* | ||
356 | * the part of load.weight contributed by tasks | ||
357 | */ | ||
358 | unsigned long task_weight; | ||
359 | |||
360 | /* | ||
361 | * h_load = weight * f(tg) | ||
362 | * | ||
363 | * Where f(tg) is the recursive weight fraction assigned to | ||
364 | * this group. | ||
365 | */ | ||
366 | unsigned long h_load; | ||
367 | |||
368 | /* | ||
369 | * Maintaining per-cpu shares distribution for group scheduling | ||
370 | * | ||
371 | * load_stamp is the last time we updated the load average | ||
372 | * load_last is the last time we updated the load average and saw load | ||
373 | * load_unacc_exec_time is currently unaccounted execution time | ||
374 | */ | ||
375 | u64 load_avg; | ||
376 | u64 load_period; | ||
377 | u64 load_stamp, load_last, load_unacc_exec_time; | ||
378 | |||
379 | unsigned long load_contribution; | ||
380 | #endif | ||
381 | #endif | ||
382 | }; | ||
383 | |||
384 | /* Real-Time classes' related field in a runqueue: */ | ||
385 | struct rt_rq { | ||
386 | struct rt_prio_array active; | ||
387 | unsigned long rt_nr_running; | ||
388 | #if defined CONFIG_SMP || defined CONFIG_RT_GROUP_SCHED | ||
389 | struct { | ||
390 | int curr; /* highest queued rt task prio */ | ||
391 | #ifdef CONFIG_SMP | ||
392 | int next; /* next highest */ | ||
393 | #endif | ||
394 | } highest_prio; | ||
395 | #endif | ||
396 | #ifdef CONFIG_SMP | ||
397 | unsigned long rt_nr_migratory; | ||
398 | unsigned long rt_nr_total; | ||
399 | int overloaded; | ||
400 | struct plist_head pushable_tasks; | ||
401 | #endif | ||
402 | int rt_throttled; | ||
403 | u64 rt_time; | ||
404 | u64 rt_runtime; | ||
405 | /* Nests inside the rq lock: */ | ||
406 | raw_spinlock_t rt_runtime_lock; | ||
407 | |||
408 | #ifdef CONFIG_RT_GROUP_SCHED | ||
409 | unsigned long rt_nr_boosted; | ||
410 | |||
411 | struct rq *rq; | ||
412 | struct list_head leaf_rt_rq_list; | ||
413 | struct task_group *tg; | ||
414 | #endif | ||
415 | }; | ||
416 | |||
417 | #ifdef CONFIG_SMP | ||
418 | |||
419 | /* | ||
420 | * We add the notion of a root-domain which will be used to define per-domain | ||
421 | * variables. Each exclusive cpuset essentially defines an island domain by | ||
422 | * fully partitioning the member cpus from any other cpuset. Whenever a new | ||
423 | * exclusive cpuset is created, we also create and attach a new root-domain | ||
424 | * object. | ||
425 | * | ||
426 | */ | ||
427 | struct root_domain { | ||
428 | atomic_t refcount; | ||
429 | atomic_t rto_count; | ||
430 | struct rcu_head rcu; | ||
431 | cpumask_var_t span; | ||
432 | cpumask_var_t online; | ||
433 | |||
434 | /* | ||
435 | * The "RT overload" flag: it gets set if a CPU has more than | ||
436 | * one runnable RT task. | ||
437 | */ | ||
438 | cpumask_var_t rto_mask; | ||
439 | struct cpupri cpupri; | ||
440 | }; | ||
441 | |||
442 | /* | ||
443 | * By default the system creates a single root-domain with all cpus as | ||
444 | * members (mimicking the global state we have today). | ||
445 | */ | ||
446 | static struct root_domain def_root_domain; | ||
447 | |||
448 | #endif /* CONFIG_SMP */ | ||
449 | |||
450 | /* | ||
451 | * This is the main, per-CPU runqueue data structure. | ||
452 | * | ||
453 | * Locking rule: those places that want to lock multiple runqueues | ||
454 | * (such as the load balancing or the thread migration code), lock | ||
455 | * acquire operations must be ordered by ascending &runqueue. | ||
456 | */ | ||
457 | struct rq { | ||
458 | /* runqueue lock: */ | ||
459 | raw_spinlock_t lock; | ||
460 | |||
461 | /* | ||
462 | * nr_running and cpu_load should be in the same cacheline because | ||
463 | * remote CPUs use both these fields when doing load calculation. | ||
464 | */ | ||
465 | unsigned long nr_running; | ||
466 | #define CPU_LOAD_IDX_MAX 5 | ||
467 | unsigned long cpu_load[CPU_LOAD_IDX_MAX]; | ||
468 | unsigned long last_load_update_tick; | ||
469 | #ifdef CONFIG_NO_HZ | ||
470 | u64 nohz_stamp; | ||
471 | unsigned char nohz_balance_kick; | ||
472 | #endif | ||
473 | int skip_clock_update; | ||
474 | |||
475 | /* capture load from *all* tasks on this cpu: */ | ||
476 | struct load_weight load; | ||
477 | unsigned long nr_load_updates; | ||
478 | u64 nr_switches; | ||
479 | |||
480 | struct cfs_rq cfs; | ||
481 | struct rt_rq rt; | ||
482 | |||
483 | #ifdef CONFIG_FAIR_GROUP_SCHED | ||
484 | /* list of leaf cfs_rq on this cpu: */ | ||
485 | struct list_head leaf_cfs_rq_list; | ||
486 | #endif | ||
487 | #ifdef CONFIG_RT_GROUP_SCHED | ||
488 | struct list_head leaf_rt_rq_list; | ||
489 | #endif | ||
490 | |||
491 | /* | ||
492 | * This is part of a global counter where only the total sum | ||
493 | * over all CPUs matters. A task can increase this counter on | ||
494 | * one CPU and if it got migrated afterwards it may decrease | ||
495 | * it on another CPU. Always updated under the runqueue lock: | ||
496 | */ | ||
497 | unsigned long nr_uninterruptible; | ||
498 | |||
499 | struct task_struct *curr, *idle, *stop; | ||
500 | unsigned long next_balance; | ||
501 | struct mm_struct *prev_mm; | ||
502 | |||
503 | u64 clock; | ||
504 | u64 clock_task; | ||
505 | |||
506 | atomic_t nr_iowait; | ||
507 | |||
508 | #ifdef CONFIG_SMP | ||
509 | struct root_domain *rd; | ||
510 | struct sched_domain *sd; | ||
511 | |||
512 | unsigned long cpu_power; | ||
513 | |||
514 | unsigned char idle_at_tick; | ||
515 | /* For active balancing */ | ||
516 | int post_schedule; | ||
517 | int active_balance; | ||
518 | int push_cpu; | ||
519 | struct cpu_stop_work active_balance_work; | ||
520 | /* cpu of this runqueue: */ | ||
521 | int cpu; | ||
522 | int online; | ||
523 | |||
524 | unsigned long avg_load_per_task; | ||
525 | |||
526 | u64 rt_avg; | ||
527 | u64 age_stamp; | ||
528 | u64 idle_stamp; | ||
529 | u64 avg_idle; | ||
530 | #endif | ||
531 | |||
532 | #ifdef CONFIG_IRQ_TIME_ACCOUNTING | ||
533 | u64 prev_irq_time; | ||
534 | #endif | ||
535 | #ifdef CONFIG_PARAVIRT | ||
536 | u64 prev_steal_time; | ||
537 | #endif | ||
538 | #ifdef CONFIG_PARAVIRT_TIME_ACCOUNTING | ||
539 | u64 prev_steal_time_rq; | ||
540 | #endif | ||
541 | |||
542 | /* calc_load related fields */ | ||
543 | unsigned long calc_load_update; | ||
544 | long calc_load_active; | ||
545 | |||
546 | #ifdef CONFIG_SCHED_HRTICK | ||
547 | #ifdef CONFIG_SMP | ||
548 | int hrtick_csd_pending; | ||
549 | struct call_single_data hrtick_csd; | ||
550 | #endif | ||
551 | struct hrtimer hrtick_timer; | ||
552 | #endif | ||
553 | |||
554 | #ifdef CONFIG_SCHEDSTATS | ||
555 | /* latency stats */ | ||
556 | struct sched_info rq_sched_info; | ||
557 | unsigned long long rq_cpu_time; | ||
558 | /* could above be rq->cfs_rq.exec_clock + rq->rt_rq.rt_runtime ? */ | ||
559 | |||
560 | /* sys_sched_yield() stats */ | ||
561 | unsigned int yld_count; | ||
562 | |||
563 | /* schedule() stats */ | ||
564 | unsigned int sched_switch; | ||
565 | unsigned int sched_count; | ||
566 | unsigned int sched_goidle; | ||
567 | |||
568 | /* try_to_wake_up() stats */ | ||
569 | unsigned int ttwu_count; | ||
570 | unsigned int ttwu_local; | ||
571 | #endif | ||
572 | |||
573 | #ifdef CONFIG_SMP | ||
574 | struct task_struct *wake_list; | ||
575 | #endif | ||
576 | }; | ||
577 | |||
578 | static DEFINE_PER_CPU_SHARED_ALIGNED(struct rq, runqueues); | ||
579 | |||
580 | |||
581 | static void check_preempt_curr(struct rq *rq, struct task_struct *p, int flags); | ||
582 | |||
583 | static inline int cpu_of(struct rq *rq) | ||
584 | { | ||
585 | #ifdef CONFIG_SMP | ||
586 | return rq->cpu; | ||
587 | #else | ||
588 | return 0; | ||
589 | #endif | ||
590 | } | ||
591 | |||
592 | #define rcu_dereference_check_sched_domain(p) \ | ||
593 | rcu_dereference_check((p), \ | ||
594 | lockdep_is_held(&sched_domains_mutex)) | ||
595 | |||
596 | /* | ||
597 | * The domain tree (rq->sd) is protected by RCU's quiescent state transition. | ||
598 | * See detach_destroy_domains: synchronize_sched for details. | ||
599 | * | ||
600 | * The domain tree of any CPU may only be accessed from within | ||
601 | * preempt-disabled sections. | ||
602 | */ | ||
603 | #define for_each_domain(cpu, __sd) \ | ||
604 | for (__sd = rcu_dereference_check_sched_domain(cpu_rq(cpu)->sd); __sd; __sd = __sd->parent) | ||
605 | |||
606 | #define cpu_rq(cpu) (&per_cpu(runqueues, (cpu))) | ||
607 | #define this_rq() (&__get_cpu_var(runqueues)) | ||
608 | #define task_rq(p) cpu_rq(task_cpu(p)) | ||
609 | #define cpu_curr(cpu) (cpu_rq(cpu)->curr) | ||
610 | #define raw_rq() (&__raw_get_cpu_var(runqueues)) | ||
611 | |||
612 | #ifdef CONFIG_CGROUP_SCHED | ||
613 | |||
614 | /* | ||
615 | * Return the group to which this tasks belongs. | ||
616 | * | ||
617 | * We use task_subsys_state_check() and extend the RCU verification with | ||
618 | * pi->lock and rq->lock because cpu_cgroup_attach() holds those locks for each | ||
619 | * task it moves into the cgroup. Therefore by holding either of those locks, | ||
620 | * we pin the task to the current cgroup. | ||
621 | */ | ||
622 | static inline struct task_group *task_group(struct task_struct *p) | ||
623 | { | ||
624 | struct task_group *tg; | ||
625 | struct cgroup_subsys_state *css; | ||
626 | |||
627 | css = task_subsys_state_check(p, cpu_cgroup_subsys_id, | ||
628 | lockdep_is_held(&p->pi_lock) || | ||
629 | lockdep_is_held(&task_rq(p)->lock)); | ||
630 | tg = container_of(css, struct task_group, css); | ||
631 | |||
632 | return autogroup_task_group(p, tg); | ||
633 | } | ||
634 | |||
635 | /* Change a task's cfs_rq and parent entity if it moves across CPUs/groups */ | ||
636 | static inline void set_task_rq(struct task_struct *p, unsigned int cpu) | ||
637 | { | ||
638 | #ifdef CONFIG_FAIR_GROUP_SCHED | ||
639 | p->se.cfs_rq = task_group(p)->cfs_rq[cpu]; | ||
640 | p->se.parent = task_group(p)->se[cpu]; | ||
641 | #endif | ||
642 | |||
643 | #ifdef CONFIG_RT_GROUP_SCHED | ||
644 | p->rt.rt_rq = task_group(p)->rt_rq[cpu]; | ||
645 | p->rt.parent = task_group(p)->rt_se[cpu]; | ||
646 | #endif | ||
647 | } | ||
648 | |||
649 | #else /* CONFIG_CGROUP_SCHED */ | ||
650 | |||
651 | static inline void set_task_rq(struct task_struct *p, unsigned int cpu) { } | ||
652 | static inline struct task_group *task_group(struct task_struct *p) | ||
653 | { | ||
654 | return NULL; | ||
655 | } | ||
656 | |||
657 | #endif /* CONFIG_CGROUP_SCHED */ | ||
658 | |||
659 | static void update_rq_clock_task(struct rq *rq, s64 delta); | ||
660 | |||
661 | static void update_rq_clock(struct rq *rq) | ||
662 | { | ||
663 | s64 delta; | ||
664 | |||
665 | if (rq->skip_clock_update > 0) | ||
666 | return; | ||
667 | |||
668 | delta = sched_clock_cpu(cpu_of(rq)) - rq->clock; | ||
669 | rq->clock += delta; | ||
670 | update_rq_clock_task(rq, delta); | ||
671 | } | ||
672 | |||
673 | /* | ||
674 | * Tunables that become constants when CONFIG_SCHED_DEBUG is off: | ||
675 | */ | ||
676 | #ifdef CONFIG_SCHED_DEBUG | ||
677 | # define const_debug __read_mostly | ||
678 | #else | ||
679 | # define const_debug static const | ||
680 | #endif | ||
681 | |||
682 | /** | ||
683 | * runqueue_is_locked - Returns true if the current cpu runqueue is locked | ||
684 | * @cpu: the processor in question. | ||
685 | * | ||
686 | * This interface allows printk to be called with the runqueue lock | ||
687 | * held and know whether or not it is OK to wake up the klogd. | ||
688 | */ | ||
689 | int runqueue_is_locked(int cpu) | ||
690 | { | ||
691 | return raw_spin_is_locked(&cpu_rq(cpu)->lock); | ||
692 | } | ||
693 | |||
694 | /* | ||
695 | * Debugging: various feature bits | ||
696 | */ | ||
697 | |||
698 | #define SCHED_FEAT(name, enabled) \ | ||
699 | __SCHED_FEAT_##name , | ||
700 | |||
701 | enum { | ||
702 | #include "sched_features.h" | ||
703 | }; | ||
704 | |||
705 | #undef SCHED_FEAT | ||
706 | |||
707 | #define SCHED_FEAT(name, enabled) \ | ||
708 | (1UL << __SCHED_FEAT_##name) * enabled | | ||
709 | |||
710 | const_debug unsigned int sysctl_sched_features = | ||
711 | #include "sched_features.h" | ||
712 | 0; | ||
713 | |||
714 | #undef SCHED_FEAT | ||
715 | |||
716 | #ifdef CONFIG_SCHED_DEBUG | ||
717 | #define SCHED_FEAT(name, enabled) \ | ||
718 | #name , | ||
719 | |||
720 | static __read_mostly char *sched_feat_names[] = { | ||
721 | #include "sched_features.h" | ||
722 | NULL | ||
723 | }; | ||
724 | |||
725 | #undef SCHED_FEAT | ||
726 | |||
727 | static int sched_feat_show(struct seq_file *m, void *v) | ||
728 | { | ||
729 | int i; | ||
730 | |||
731 | for (i = 0; sched_feat_names[i]; i++) { | ||
732 | if (!(sysctl_sched_features & (1UL << i))) | ||
733 | seq_puts(m, "NO_"); | ||
734 | seq_printf(m, "%s ", sched_feat_names[i]); | ||
735 | } | ||
736 | seq_puts(m, "\n"); | ||
737 | |||
738 | return 0; | ||
739 | } | ||
740 | |||
741 | static ssize_t | ||
742 | sched_feat_write(struct file *filp, const char __user *ubuf, | ||
743 | size_t cnt, loff_t *ppos) | ||
744 | { | ||
745 | char buf[64]; | ||
746 | char *cmp; | ||
747 | int neg = 0; | ||
748 | int i; | ||
749 | |||
750 | if (cnt > 63) | ||
751 | cnt = 63; | ||
752 | |||
753 | if (copy_from_user(&buf, ubuf, cnt)) | ||
754 | return -EFAULT; | ||
755 | |||
756 | buf[cnt] = 0; | ||
757 | cmp = strstrip(buf); | ||
758 | |||
759 | if (strncmp(cmp, "NO_", 3) == 0) { | ||
760 | neg = 1; | ||
761 | cmp += 3; | ||
762 | } | ||
763 | |||
764 | for (i = 0; sched_feat_names[i]; i++) { | ||
765 | if (strcmp(cmp, sched_feat_names[i]) == 0) { | ||
766 | if (neg) | ||
767 | sysctl_sched_features &= ~(1UL << i); | ||
768 | else | ||
769 | sysctl_sched_features |= (1UL << i); | ||
770 | break; | ||
771 | } | ||
772 | } | ||
773 | |||
774 | if (!sched_feat_names[i]) | ||
775 | return -EINVAL; | ||
776 | |||
777 | *ppos += cnt; | ||
778 | |||
779 | return cnt; | ||
780 | } | ||
781 | |||
782 | static int sched_feat_open(struct inode *inode, struct file *filp) | ||
783 | { | ||
784 | return single_open(filp, sched_feat_show, NULL); | ||
785 | } | ||
786 | |||
787 | static const struct file_operations sched_feat_fops = { | ||
788 | .open = sched_feat_open, | ||
789 | .write = sched_feat_write, | ||
790 | .read = seq_read, | ||
791 | .llseek = seq_lseek, | ||
792 | .release = single_release, | ||
793 | }; | ||
794 | |||
795 | static __init int sched_init_debug(void) | ||
796 | { | ||
797 | debugfs_create_file("sched_features", 0644, NULL, NULL, | ||
798 | &sched_feat_fops); | ||
799 | |||
800 | return 0; | ||
801 | } | ||
802 | late_initcall(sched_init_debug); | ||
803 | |||
804 | #endif | ||
805 | |||
806 | #define sched_feat(x) (sysctl_sched_features & (1UL << __SCHED_FEAT_##x)) | ||
807 | |||
808 | /* | ||
809 | * Number of tasks to iterate in a single balance run. | ||
810 | * Limited because this is done with IRQs disabled. | ||
811 | */ | ||
812 | const_debug unsigned int sysctl_sched_nr_migrate = 32; | ||
813 | |||
814 | /* | ||
815 | * period over which we average the RT time consumption, measured | ||
816 | * in ms. | ||
817 | * | ||
818 | * default: 1s | ||
819 | */ | ||
820 | const_debug unsigned int sysctl_sched_time_avg = MSEC_PER_SEC; | ||
821 | |||
822 | /* | ||
823 | * period over which we measure -rt task cpu usage in us. | ||
824 | * default: 1s | ||
825 | */ | ||
826 | unsigned int sysctl_sched_rt_period = 1000000; | ||
827 | |||
828 | static __read_mostly int scheduler_running; | ||
829 | |||
830 | /* | ||
831 | * part of the period that we allow rt tasks to run in us. | ||
832 | * default: 0.95s | ||
833 | */ | ||
834 | int sysctl_sched_rt_runtime = 950000; | ||
835 | |||
836 | static inline u64 global_rt_period(void) | ||
837 | { | ||
838 | return (u64)sysctl_sched_rt_period * NSEC_PER_USEC; | ||
839 | } | ||
840 | |||
841 | static inline u64 global_rt_runtime(void) | ||
842 | { | ||
843 | if (sysctl_sched_rt_runtime < 0) | ||
844 | return RUNTIME_INF; | ||
845 | |||
846 | return (u64)sysctl_sched_rt_runtime * NSEC_PER_USEC; | ||
847 | } | ||
848 | |||
849 | #ifndef prepare_arch_switch | ||
850 | # define prepare_arch_switch(next) do { } while (0) | ||
851 | #endif | ||
852 | #ifndef finish_arch_switch | ||
853 | # define finish_arch_switch(prev) do { } while (0) | ||
854 | #endif | ||
855 | |||
856 | static inline int task_current(struct rq *rq, struct task_struct *p) | ||
857 | { | ||
858 | return rq->curr == p; | ||
859 | } | ||
860 | |||
861 | static inline int task_running(struct rq *rq, struct task_struct *p) | ||
862 | { | ||
863 | #ifdef CONFIG_SMP | ||
864 | return p->on_cpu; | ||
865 | #else | ||
866 | return task_current(rq, p); | ||
867 | #endif | ||
868 | } | ||
869 | |||
870 | #ifndef __ARCH_WANT_UNLOCKED_CTXSW | ||
871 | static inline void prepare_lock_switch(struct rq *rq, struct task_struct *next) | ||
872 | { | ||
873 | #ifdef CONFIG_SMP | ||
874 | /* | ||
875 | * We can optimise this out completely for !SMP, because the | ||
876 | * SMP rebalancing from interrupt is the only thing that cares | ||
877 | * here. | ||
878 | */ | ||
879 | next->on_cpu = 1; | ||
880 | #endif | ||
881 | } | ||
882 | |||
883 | static inline void finish_lock_switch(struct rq *rq, struct task_struct *prev) | ||
884 | { | ||
885 | #ifdef CONFIG_SMP | ||
886 | /* | ||
887 | * After ->on_cpu is cleared, the task can be moved to a different CPU. | ||
888 | * We must ensure this doesn't happen until the switch is completely | ||
889 | * finished. | ||
890 | */ | ||
891 | smp_wmb(); | ||
892 | prev->on_cpu = 0; | ||
893 | #endif | ||
894 | #ifdef CONFIG_DEBUG_SPINLOCK | ||
895 | /* this is a valid case when another task releases the spinlock */ | ||
896 | rq->lock.owner = current; | ||
897 | #endif | ||
898 | /* | ||
899 | * If we are tracking spinlock dependencies then we have to | ||
900 | * fix up the runqueue lock - which gets 'carried over' from | ||
901 | * prev into current: | ||
902 | */ | ||
903 | spin_acquire(&rq->lock.dep_map, 0, 0, _THIS_IP_); | ||
904 | |||
905 | raw_spin_unlock_irq(&rq->lock); | ||
906 | } | ||
907 | |||
908 | #else /* __ARCH_WANT_UNLOCKED_CTXSW */ | ||
909 | static inline void prepare_lock_switch(struct rq *rq, struct task_struct *next) | ||
910 | { | ||
911 | #ifdef CONFIG_SMP | ||
912 | /* | ||
913 | * We can optimise this out completely for !SMP, because the | ||
914 | * SMP rebalancing from interrupt is the only thing that cares | ||
915 | * here. | ||
916 | */ | ||
917 | next->on_cpu = 1; | ||
918 | #endif | ||
919 | #ifdef __ARCH_WANT_INTERRUPTS_ON_CTXSW | ||
920 | raw_spin_unlock_irq(&rq->lock); | ||
921 | #else | ||
922 | raw_spin_unlock(&rq->lock); | ||
923 | #endif | ||
924 | } | ||
925 | |||
926 | static inline void finish_lock_switch(struct rq *rq, struct task_struct *prev) | ||
927 | { | ||
928 | #ifdef CONFIG_SMP | ||
929 | /* | ||
930 | * After ->on_cpu is cleared, the task can be moved to a different CPU. | ||
931 | * We must ensure this doesn't happen until the switch is completely | ||
932 | * finished. | ||
933 | */ | ||
934 | smp_wmb(); | ||
935 | prev->on_cpu = 0; | ||
936 | #endif | ||
937 | #ifndef __ARCH_WANT_INTERRUPTS_ON_CTXSW | ||
938 | local_irq_enable(); | ||
939 | #endif | ||
940 | } | ||
941 | #endif /* __ARCH_WANT_UNLOCKED_CTXSW */ | ||
942 | |||
943 | /* | ||
944 | * __task_rq_lock - lock the rq @p resides on. | ||
945 | */ | ||
946 | static inline struct rq *__task_rq_lock(struct task_struct *p) | ||
947 | __acquires(rq->lock) | ||
948 | { | ||
949 | struct rq *rq; | ||
950 | |||
951 | lockdep_assert_held(&p->pi_lock); | ||
952 | |||
953 | for (;;) { | ||
954 | rq = task_rq(p); | ||
955 | raw_spin_lock(&rq->lock); | ||
956 | if (likely(rq == task_rq(p))) | ||
957 | return rq; | ||
958 | raw_spin_unlock(&rq->lock); | ||
959 | } | ||
960 | } | ||
961 | |||
962 | /* | ||
963 | * task_rq_lock - lock p->pi_lock and lock the rq @p resides on. | ||
964 | */ | ||
965 | static struct rq *task_rq_lock(struct task_struct *p, unsigned long *flags) | ||
966 | __acquires(p->pi_lock) | ||
967 | __acquires(rq->lock) | ||
968 | { | ||
969 | struct rq *rq; | ||
970 | |||
971 | for (;;) { | ||
972 | raw_spin_lock_irqsave(&p->pi_lock, *flags); | ||
973 | rq = task_rq(p); | ||
974 | raw_spin_lock(&rq->lock); | ||
975 | if (likely(rq == task_rq(p))) | ||
976 | return rq; | ||
977 | raw_spin_unlock(&rq->lock); | ||
978 | raw_spin_unlock_irqrestore(&p->pi_lock, *flags); | ||
979 | } | ||
980 | } | ||
981 | |||
982 | static void __task_rq_unlock(struct rq *rq) | ||
983 | __releases(rq->lock) | ||
984 | { | ||
985 | raw_spin_unlock(&rq->lock); | ||
986 | } | ||
987 | |||
988 | static inline void | ||
989 | task_rq_unlock(struct rq *rq, struct task_struct *p, unsigned long *flags) | ||
990 | __releases(rq->lock) | ||
991 | __releases(p->pi_lock) | ||
992 | { | ||
993 | raw_spin_unlock(&rq->lock); | ||
994 | raw_spin_unlock_irqrestore(&p->pi_lock, *flags); | ||
995 | } | ||
996 | |||
997 | /* | ||
998 | * this_rq_lock - lock this runqueue and disable interrupts. | ||
999 | */ | ||
1000 | static struct rq *this_rq_lock(void) | ||
1001 | __acquires(rq->lock) | ||
1002 | { | ||
1003 | struct rq *rq; | ||
1004 | |||
1005 | local_irq_disable(); | ||
1006 | rq = this_rq(); | ||
1007 | raw_spin_lock(&rq->lock); | ||
1008 | |||
1009 | return rq; | ||
1010 | } | ||
1011 | |||
1012 | #ifdef CONFIG_SCHED_HRTICK | ||
1013 | /* | ||
1014 | * Use HR-timers to deliver accurate preemption points. | ||
1015 | * | ||
1016 | * Its all a bit involved since we cannot program an hrt while holding the | ||
1017 | * rq->lock. So what we do is store a state in in rq->hrtick_* and ask for a | ||
1018 | * reschedule event. | ||
1019 | * | ||
1020 | * When we get rescheduled we reprogram the hrtick_timer outside of the | ||
1021 | * rq->lock. | ||
1022 | */ | ||
1023 | |||
1024 | /* | ||
1025 | * Use hrtick when: | ||
1026 | * - enabled by features | ||
1027 | * - hrtimer is actually high res | ||
1028 | */ | ||
1029 | static inline int hrtick_enabled(struct rq *rq) | ||
1030 | { | ||
1031 | if (!sched_feat(HRTICK)) | ||
1032 | return 0; | ||
1033 | if (!cpu_active(cpu_of(rq))) | ||
1034 | return 0; | ||
1035 | return hrtimer_is_hres_active(&rq->hrtick_timer); | ||
1036 | } | ||
1037 | |||
1038 | static void hrtick_clear(struct rq *rq) | ||
1039 | { | ||
1040 | if (hrtimer_active(&rq->hrtick_timer)) | ||
1041 | hrtimer_cancel(&rq->hrtick_timer); | ||
1042 | } | ||
1043 | |||
1044 | /* | ||
1045 | * High-resolution timer tick. | ||
1046 | * Runs from hardirq context with interrupts disabled. | ||
1047 | */ | ||
1048 | static enum hrtimer_restart hrtick(struct hrtimer *timer) | ||
1049 | { | ||
1050 | struct rq *rq = container_of(timer, struct rq, hrtick_timer); | ||
1051 | |||
1052 | WARN_ON_ONCE(cpu_of(rq) != smp_processor_id()); | ||
1053 | |||
1054 | raw_spin_lock(&rq->lock); | ||
1055 | update_rq_clock(rq); | ||
1056 | rq->curr->sched_class->task_tick(rq, rq->curr, 1); | ||
1057 | raw_spin_unlock(&rq->lock); | ||
1058 | |||
1059 | return HRTIMER_NORESTART; | ||
1060 | } | ||
1061 | |||
1062 | #ifdef CONFIG_SMP | ||
1063 | /* | ||
1064 | * called from hardirq (IPI) context | ||
1065 | */ | ||
1066 | static void __hrtick_start(void *arg) | ||
1067 | { | ||
1068 | struct rq *rq = arg; | ||
1069 | |||
1070 | raw_spin_lock(&rq->lock); | ||
1071 | hrtimer_restart(&rq->hrtick_timer); | ||
1072 | rq->hrtick_csd_pending = 0; | ||
1073 | raw_spin_unlock(&rq->lock); | ||
1074 | } | ||
1075 | |||
1076 | /* | ||
1077 | * Called to set the hrtick timer state. | ||
1078 | * | ||
1079 | * called with rq->lock held and irqs disabled | ||
1080 | */ | ||
1081 | static void hrtick_start(struct rq *rq, u64 delay) | ||
1082 | { | ||
1083 | struct hrtimer *timer = &rq->hrtick_timer; | ||
1084 | ktime_t time = ktime_add_ns(timer->base->get_time(), delay); | ||
1085 | |||
1086 | hrtimer_set_expires(timer, time); | ||
1087 | |||
1088 | if (rq == this_rq()) { | ||
1089 | hrtimer_restart(timer); | ||
1090 | } else if (!rq->hrtick_csd_pending) { | ||
1091 | __smp_call_function_single(cpu_of(rq), &rq->hrtick_csd, 0); | ||
1092 | rq->hrtick_csd_pending = 1; | ||
1093 | } | ||
1094 | } | ||
1095 | |||
1096 | static int | ||
1097 | hotplug_hrtick(struct notifier_block *nfb, unsigned long action, void *hcpu) | ||
1098 | { | ||
1099 | int cpu = (int)(long)hcpu; | ||
1100 | |||
1101 | switch (action) { | ||
1102 | case CPU_UP_CANCELED: | ||
1103 | case CPU_UP_CANCELED_FROZEN: | ||
1104 | case CPU_DOWN_PREPARE: | ||
1105 | case CPU_DOWN_PREPARE_FROZEN: | ||
1106 | case CPU_DEAD: | ||
1107 | case CPU_DEAD_FROZEN: | ||
1108 | hrtick_clear(cpu_rq(cpu)); | ||
1109 | return NOTIFY_OK; | ||
1110 | } | ||
1111 | |||
1112 | return NOTIFY_DONE; | ||
1113 | } | ||
1114 | |||
1115 | static __init void init_hrtick(void) | ||
1116 | { | ||
1117 | hotcpu_notifier(hotplug_hrtick, 0); | ||
1118 | } | ||
1119 | #else | ||
1120 | /* | ||
1121 | * Called to set the hrtick timer state. | ||
1122 | * | ||
1123 | * called with rq->lock held and irqs disabled | ||
1124 | */ | ||
1125 | static void hrtick_start(struct rq *rq, u64 delay) | ||
1126 | { | ||
1127 | __hrtimer_start_range_ns(&rq->hrtick_timer, ns_to_ktime(delay), 0, | ||
1128 | HRTIMER_MODE_REL_PINNED, 0); | ||
1129 | } | ||
1130 | |||
1131 | static inline void init_hrtick(void) | ||
1132 | { | ||
1133 | } | ||
1134 | #endif /* CONFIG_SMP */ | ||
1135 | |||
1136 | static void init_rq_hrtick(struct rq *rq) | ||
1137 | { | ||
1138 | #ifdef CONFIG_SMP | ||
1139 | rq->hrtick_csd_pending = 0; | ||
1140 | |||
1141 | rq->hrtick_csd.flags = 0; | ||
1142 | rq->hrtick_csd.func = __hrtick_start; | ||
1143 | rq->hrtick_csd.info = rq; | ||
1144 | #endif | ||
1145 | |||
1146 | hrtimer_init(&rq->hrtick_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL); | ||
1147 | rq->hrtick_timer.function = hrtick; | ||
1148 | } | ||
1149 | #else /* CONFIG_SCHED_HRTICK */ | ||
1150 | static inline void hrtick_clear(struct rq *rq) | ||
1151 | { | ||
1152 | } | ||
1153 | |||
1154 | static inline void init_rq_hrtick(struct rq *rq) | ||
1155 | { | ||
1156 | } | ||
1157 | |||
1158 | static inline void init_hrtick(void) | ||
1159 | { | ||
1160 | } | ||
1161 | #endif /* CONFIG_SCHED_HRTICK */ | ||
1162 | |||
1163 | /* | ||
1164 | * resched_task - mark a task 'to be rescheduled now'. | ||
1165 | * | ||
1166 | * On UP this means the setting of the need_resched flag, on SMP it | ||
1167 | * might also involve a cross-CPU call to trigger the scheduler on | ||
1168 | * the target CPU. | ||
1169 | */ | ||
1170 | #ifdef CONFIG_SMP | ||
1171 | |||
1172 | #ifndef tsk_is_polling | ||
1173 | #define tsk_is_polling(t) test_tsk_thread_flag(t, TIF_POLLING_NRFLAG) | ||
1174 | #endif | ||
1175 | |||
1176 | static void resched_task(struct task_struct *p) | ||
1177 | { | ||
1178 | int cpu; | ||
1179 | |||
1180 | assert_raw_spin_locked(&task_rq(p)->lock); | ||
1181 | |||
1182 | if (test_tsk_need_resched(p)) | ||
1183 | return; | ||
1184 | |||
1185 | set_tsk_need_resched(p); | ||
1186 | |||
1187 | cpu = task_cpu(p); | ||
1188 | if (cpu == smp_processor_id()) | ||
1189 | return; | ||
1190 | |||
1191 | /* NEED_RESCHED must be visible before we test polling */ | ||
1192 | smp_mb(); | ||
1193 | if (!tsk_is_polling(p)) | ||
1194 | smp_send_reschedule(cpu); | ||
1195 | } | ||
1196 | |||
1197 | static void resched_cpu(int cpu) | ||
1198 | { | ||
1199 | struct rq *rq = cpu_rq(cpu); | ||
1200 | unsigned long flags; | ||
1201 | |||
1202 | if (!raw_spin_trylock_irqsave(&rq->lock, flags)) | ||
1203 | return; | ||
1204 | resched_task(cpu_curr(cpu)); | ||
1205 | raw_spin_unlock_irqrestore(&rq->lock, flags); | ||
1206 | } | ||
1207 | |||
1208 | #ifdef CONFIG_NO_HZ | ||
1209 | /* | ||
1210 | * In the semi idle case, use the nearest busy cpu for migrating timers | ||
1211 | * from an idle cpu. This is good for power-savings. | ||
1212 | * | ||
1213 | * We don't do similar optimization for completely idle system, as | ||
1214 | * selecting an idle cpu will add more delays to the timers than intended | ||
1215 | * (as that cpu's timer base may not be uptodate wrt jiffies etc). | ||
1216 | */ | ||
1217 | int get_nohz_timer_target(void) | ||
1218 | { | ||
1219 | int cpu = smp_processor_id(); | ||
1220 | int i; | ||
1221 | struct sched_domain *sd; | ||
1222 | |||
1223 | rcu_read_lock(); | ||
1224 | for_each_domain(cpu, sd) { | ||
1225 | for_each_cpu(i, sched_domain_span(sd)) { | ||
1226 | if (!idle_cpu(i)) { | ||
1227 | cpu = i; | ||
1228 | goto unlock; | ||
1229 | } | ||
1230 | } | ||
1231 | } | ||
1232 | unlock: | ||
1233 | rcu_read_unlock(); | ||
1234 | return cpu; | ||
1235 | } | ||
1236 | /* | ||
1237 | * When add_timer_on() enqueues a timer into the timer wheel of an | ||
1238 | * idle CPU then this timer might expire before the next timer event | ||
1239 | * which is scheduled to wake up that CPU. In case of a completely | ||
1240 | * idle system the next event might even be infinite time into the | ||
1241 | * future. wake_up_idle_cpu() ensures that the CPU is woken up and | ||
1242 | * leaves the inner idle loop so the newly added timer is taken into | ||
1243 | * account when the CPU goes back to idle and evaluates the timer | ||
1244 | * wheel for the next timer event. | ||
1245 | */ | ||
1246 | void wake_up_idle_cpu(int cpu) | ||
1247 | { | ||
1248 | struct rq *rq = cpu_rq(cpu); | ||
1249 | |||
1250 | if (cpu == smp_processor_id()) | ||
1251 | return; | ||
1252 | |||
1253 | /* | ||
1254 | * This is safe, as this function is called with the timer | ||
1255 | * wheel base lock of (cpu) held. When the CPU is on the way | ||
1256 | * to idle and has not yet set rq->curr to idle then it will | ||
1257 | * be serialized on the timer wheel base lock and take the new | ||
1258 | * timer into account automatically. | ||
1259 | */ | ||
1260 | if (rq->curr != rq->idle) | ||
1261 | return; | ||
1262 | |||
1263 | /* | ||
1264 | * We can set TIF_RESCHED on the idle task of the other CPU | ||
1265 | * lockless. The worst case is that the other CPU runs the | ||
1266 | * idle task through an additional NOOP schedule() | ||
1267 | */ | ||
1268 | set_tsk_need_resched(rq->idle); | ||
1269 | |||
1270 | /* NEED_RESCHED must be visible before we test polling */ | ||
1271 | smp_mb(); | ||
1272 | if (!tsk_is_polling(rq->idle)) | ||
1273 | smp_send_reschedule(cpu); | ||
1274 | } | ||
1275 | |||
1276 | #endif /* CONFIG_NO_HZ */ | ||
1277 | |||
1278 | static u64 sched_avg_period(void) | ||
1279 | { | ||
1280 | return (u64)sysctl_sched_time_avg * NSEC_PER_MSEC / 2; | ||
1281 | } | ||
1282 | |||
1283 | static void sched_avg_update(struct rq *rq) | ||
1284 | { | ||
1285 | s64 period = sched_avg_period(); | ||
1286 | |||
1287 | while ((s64)(rq->clock - rq->age_stamp) > period) { | ||
1288 | /* | ||
1289 | * Inline assembly required to prevent the compiler | ||
1290 | * optimising this loop into a divmod call. | ||
1291 | * See __iter_div_u64_rem() for another example of this. | ||
1292 | */ | ||
1293 | asm("" : "+rm" (rq->age_stamp)); | ||
1294 | rq->age_stamp += period; | ||
1295 | rq->rt_avg /= 2; | ||
1296 | } | ||
1297 | } | ||
1298 | |||
1299 | static void sched_rt_avg_update(struct rq *rq, u64 rt_delta) | ||
1300 | { | ||
1301 | rq->rt_avg += rt_delta; | ||
1302 | sched_avg_update(rq); | ||
1303 | } | ||
1304 | |||
1305 | #else /* !CONFIG_SMP */ | ||
1306 | static void resched_task(struct task_struct *p) | ||
1307 | { | ||
1308 | assert_raw_spin_locked(&task_rq(p)->lock); | ||
1309 | set_tsk_need_resched(p); | ||
1310 | } | ||
1311 | |||
1312 | static void sched_rt_avg_update(struct rq *rq, u64 rt_delta) | ||
1313 | { | ||
1314 | } | ||
1315 | |||
1316 | static void sched_avg_update(struct rq *rq) | ||
1317 | { | ||
1318 | } | ||
1319 | #endif /* CONFIG_SMP */ | ||
1320 | |||
1321 | #if BITS_PER_LONG == 32 | ||
1322 | # define WMULT_CONST (~0UL) | ||
1323 | #else | ||
1324 | # define WMULT_CONST (1UL << 32) | ||
1325 | #endif | ||
1326 | |||
1327 | #define WMULT_SHIFT 32 | ||
1328 | |||
1329 | /* | ||
1330 | * Shift right and round: | ||
1331 | */ | ||
1332 | #define SRR(x, y) (((x) + (1UL << ((y) - 1))) >> (y)) | ||
1333 | |||
1334 | /* | ||
1335 | * delta *= weight / lw | ||
1336 | */ | ||
1337 | static unsigned long | ||
1338 | calc_delta_mine(unsigned long delta_exec, unsigned long weight, | ||
1339 | struct load_weight *lw) | ||
1340 | { | ||
1341 | u64 tmp; | ||
1342 | |||
1343 | /* | ||
1344 | * weight can be less than 2^SCHED_LOAD_RESOLUTION for task group sched | ||
1345 | * entities since MIN_SHARES = 2. Treat weight as 1 if less than | ||
1346 | * 2^SCHED_LOAD_RESOLUTION. | ||
1347 | */ | ||
1348 | if (likely(weight > (1UL << SCHED_LOAD_RESOLUTION))) | ||
1349 | tmp = (u64)delta_exec * scale_load_down(weight); | ||
1350 | else | ||
1351 | tmp = (u64)delta_exec; | ||
1352 | |||
1353 | if (!lw->inv_weight) { | ||
1354 | unsigned long w = scale_load_down(lw->weight); | ||
1355 | |||
1356 | if (BITS_PER_LONG > 32 && unlikely(w >= WMULT_CONST)) | ||
1357 | lw->inv_weight = 1; | ||
1358 | else if (unlikely(!w)) | ||
1359 | lw->inv_weight = WMULT_CONST; | ||
1360 | else | ||
1361 | lw->inv_weight = WMULT_CONST / w; | ||
1362 | } | ||
1363 | |||
1364 | /* | ||
1365 | * Check whether we'd overflow the 64-bit multiplication: | ||
1366 | */ | ||
1367 | if (unlikely(tmp > WMULT_CONST)) | ||
1368 | tmp = SRR(SRR(tmp, WMULT_SHIFT/2) * lw->inv_weight, | ||
1369 | WMULT_SHIFT/2); | ||
1370 | else | ||
1371 | tmp = SRR(tmp * lw->inv_weight, WMULT_SHIFT); | ||
1372 | |||
1373 | return (unsigned long)min(tmp, (u64)(unsigned long)LONG_MAX); | ||
1374 | } | ||
1375 | |||
1376 | static inline void update_load_add(struct load_weight *lw, unsigned long inc) | ||
1377 | { | ||
1378 | lw->weight += inc; | ||
1379 | lw->inv_weight = 0; | ||
1380 | } | ||
1381 | |||
1382 | static inline void update_load_sub(struct load_weight *lw, unsigned long dec) | ||
1383 | { | ||
1384 | lw->weight -= dec; | ||
1385 | lw->inv_weight = 0; | ||
1386 | } | ||
1387 | |||
1388 | static inline void update_load_set(struct load_weight *lw, unsigned long w) | ||
1389 | { | ||
1390 | lw->weight = w; | ||
1391 | lw->inv_weight = 0; | ||
1392 | } | ||
1393 | |||
1394 | /* | ||
1395 | * To aid in avoiding the subversion of "niceness" due to uneven distribution | ||
1396 | * of tasks with abnormal "nice" values across CPUs the contribution that | ||
1397 | * each task makes to its run queue's load is weighted according to its | ||
1398 | * scheduling class and "nice" value. For SCHED_NORMAL tasks this is just a | ||
1399 | * scaled version of the new time slice allocation that they receive on time | ||
1400 | * slice expiry etc. | ||
1401 | */ | ||
1402 | |||
1403 | #define WEIGHT_IDLEPRIO 3 | ||
1404 | #define WMULT_IDLEPRIO 1431655765 | ||
1405 | |||
1406 | /* | ||
1407 | * Nice levels are multiplicative, with a gentle 10% change for every | ||
1408 | * nice level changed. I.e. when a CPU-bound task goes from nice 0 to | ||
1409 | * nice 1, it will get ~10% less CPU time than another CPU-bound task | ||
1410 | * that remained on nice 0. | ||
1411 | * | ||
1412 | * The "10% effect" is relative and cumulative: from _any_ nice level, | ||
1413 | * if you go up 1 level, it's -10% CPU usage, if you go down 1 level | ||
1414 | * it's +10% CPU usage. (to achieve that we use a multiplier of 1.25. | ||
1415 | * If a task goes up by ~10% and another task goes down by ~10% then | ||
1416 | * the relative distance between them is ~25%.) | ||
1417 | */ | ||
1418 | static const int prio_to_weight[40] = { | ||
1419 | /* -20 */ 88761, 71755, 56483, 46273, 36291, | ||
1420 | /* -15 */ 29154, 23254, 18705, 14949, 11916, | ||
1421 | /* -10 */ 9548, 7620, 6100, 4904, 3906, | ||
1422 | /* -5 */ 3121, 2501, 1991, 1586, 1277, | ||
1423 | /* 0 */ 1024, 820, 655, 526, 423, | ||
1424 | /* 5 */ 335, 272, 215, 172, 137, | ||
1425 | /* 10 */ 110, 87, 70, 56, 45, | ||
1426 | /* 15 */ 36, 29, 23, 18, 15, | ||
1427 | }; | ||
1428 | |||
1429 | /* | ||
1430 | * Inverse (2^32/x) values of the prio_to_weight[] array, precalculated. | ||
1431 | * | ||
1432 | * In cases where the weight does not change often, we can use the | ||
1433 | * precalculated inverse to speed up arithmetics by turning divisions | ||
1434 | * into multiplications: | ||
1435 | */ | ||
1436 | static const u32 prio_to_wmult[40] = { | ||
1437 | /* -20 */ 48388, 59856, 76040, 92818, 118348, | ||
1438 | /* -15 */ 147320, 184698, 229616, 287308, 360437, | ||
1439 | /* -10 */ 449829, 563644, 704093, 875809, 1099582, | ||
1440 | /* -5 */ 1376151, 1717300, 2157191, 2708050, 3363326, | ||
1441 | /* 0 */ 4194304, 5237765, 6557202, 8165337, 10153587, | ||
1442 | /* 5 */ 12820798, 15790321, 19976592, 24970740, 31350126, | ||
1443 | /* 10 */ 39045157, 49367440, 61356676, 76695844, 95443717, | ||
1444 | /* 15 */ 119304647, 148102320, 186737708, 238609294, 286331153, | ||
1445 | }; | ||
1446 | |||
1447 | /* Time spent by the tasks of the cpu accounting group executing in ... */ | ||
1448 | enum cpuacct_stat_index { | ||
1449 | CPUACCT_STAT_USER, /* ... user mode */ | ||
1450 | CPUACCT_STAT_SYSTEM, /* ... kernel mode */ | ||
1451 | |||
1452 | CPUACCT_STAT_NSTATS, | ||
1453 | }; | ||
1454 | |||
1455 | #ifdef CONFIG_CGROUP_CPUACCT | ||
1456 | static void cpuacct_charge(struct task_struct *tsk, u64 cputime); | ||
1457 | static void cpuacct_update_stats(struct task_struct *tsk, | ||
1458 | enum cpuacct_stat_index idx, cputime_t val); | ||
1459 | #else | ||
1460 | static inline void cpuacct_charge(struct task_struct *tsk, u64 cputime) {} | ||
1461 | static inline void cpuacct_update_stats(struct task_struct *tsk, | ||
1462 | enum cpuacct_stat_index idx, cputime_t val) {} | ||
1463 | #endif | ||
1464 | |||
1465 | static inline void inc_cpu_load(struct rq *rq, unsigned long load) | ||
1466 | { | ||
1467 | update_load_add(&rq->load, load); | ||
1468 | } | ||
1469 | |||
1470 | static inline void dec_cpu_load(struct rq *rq, unsigned long load) | ||
1471 | { | ||
1472 | update_load_sub(&rq->load, load); | ||
1473 | } | ||
1474 | |||
1475 | #if (defined(CONFIG_SMP) && defined(CONFIG_FAIR_GROUP_SCHED)) || defined(CONFIG_RT_GROUP_SCHED) | ||
1476 | typedef int (*tg_visitor)(struct task_group *, void *); | ||
1477 | |||
1478 | /* | ||
1479 | * Iterate the full tree, calling @down when first entering a node and @up when | ||
1480 | * leaving it for the final time. | ||
1481 | */ | ||
1482 | static int walk_tg_tree(tg_visitor down, tg_visitor up, void *data) | ||
1483 | { | ||
1484 | struct task_group *parent, *child; | ||
1485 | int ret; | ||
1486 | |||
1487 | rcu_read_lock(); | ||
1488 | parent = &root_task_group; | ||
1489 | down: | ||
1490 | ret = (*down)(parent, data); | ||
1491 | if (ret) | ||
1492 | goto out_unlock; | ||
1493 | list_for_each_entry_rcu(child, &parent->children, siblings) { | ||
1494 | parent = child; | ||
1495 | goto down; | ||
1496 | |||
1497 | up: | ||
1498 | continue; | ||
1499 | } | ||
1500 | ret = (*up)(parent, data); | ||
1501 | if (ret) | ||
1502 | goto out_unlock; | ||
1503 | |||
1504 | child = parent; | ||
1505 | parent = parent->parent; | ||
1506 | if (parent) | ||
1507 | goto up; | ||
1508 | out_unlock: | ||
1509 | rcu_read_unlock(); | ||
1510 | |||
1511 | return ret; | ||
1512 | } | ||
1513 | |||
1514 | static int tg_nop(struct task_group *tg, void *data) | ||
1515 | { | ||
1516 | return 0; | ||
1517 | } | ||
1518 | #endif | ||
1519 | |||
1520 | #ifdef CONFIG_SMP | ||
1521 | /* Used instead of source_load when we know the type == 0 */ | ||
1522 | static unsigned long weighted_cpuload(const int cpu) | ||
1523 | { | ||
1524 | return cpu_rq(cpu)->load.weight; | ||
1525 | } | ||
1526 | |||
1527 | /* | ||
1528 | * Return a low guess at the load of a migration-source cpu weighted | ||
1529 | * according to the scheduling class and "nice" value. | ||
1530 | * | ||
1531 | * We want to under-estimate the load of migration sources, to | ||
1532 | * balance conservatively. | ||
1533 | */ | ||
1534 | static unsigned long source_load(int cpu, int type) | ||
1535 | { | ||
1536 | struct rq *rq = cpu_rq(cpu); | ||
1537 | unsigned long total = weighted_cpuload(cpu); | ||
1538 | |||
1539 | if (type == 0 || !sched_feat(LB_BIAS)) | ||
1540 | return total; | ||
1541 | |||
1542 | return min(rq->cpu_load[type-1], total); | ||
1543 | } | ||
1544 | |||
1545 | /* | ||
1546 | * Return a high guess at the load of a migration-target cpu weighted | ||
1547 | * according to the scheduling class and "nice" value. | ||
1548 | */ | ||
1549 | static unsigned long target_load(int cpu, int type) | ||
1550 | { | ||
1551 | struct rq *rq = cpu_rq(cpu); | ||
1552 | unsigned long total = weighted_cpuload(cpu); | ||
1553 | |||
1554 | if (type == 0 || !sched_feat(LB_BIAS)) | ||
1555 | return total; | ||
1556 | |||
1557 | return max(rq->cpu_load[type-1], total); | ||
1558 | } | ||
1559 | |||
1560 | static unsigned long power_of(int cpu) | ||
1561 | { | ||
1562 | return cpu_rq(cpu)->cpu_power; | ||
1563 | } | ||
1564 | |||
1565 | static int task_hot(struct task_struct *p, u64 now, struct sched_domain *sd); | ||
1566 | |||
1567 | static unsigned long cpu_avg_load_per_task(int cpu) | ||
1568 | { | ||
1569 | struct rq *rq = cpu_rq(cpu); | ||
1570 | unsigned long nr_running = ACCESS_ONCE(rq->nr_running); | ||
1571 | |||
1572 | if (nr_running) | ||
1573 | rq->avg_load_per_task = rq->load.weight / nr_running; | ||
1574 | else | ||
1575 | rq->avg_load_per_task = 0; | ||
1576 | |||
1577 | return rq->avg_load_per_task; | ||
1578 | } | ||
1579 | |||
1580 | #ifdef CONFIG_PREEMPT | ||
1581 | |||
1582 | static void double_rq_lock(struct rq *rq1, struct rq *rq2); | ||
1583 | |||
1584 | /* | ||
1585 | * fair double_lock_balance: Safely acquires both rq->locks in a fair | ||
1586 | * way at the expense of forcing extra atomic operations in all | ||
1587 | * invocations. This assures that the double_lock is acquired using the | ||
1588 | * same underlying policy as the spinlock_t on this architecture, which | ||
1589 | * reduces latency compared to the unfair variant below. However, it | ||
1590 | * also adds more overhead and therefore may reduce throughput. | ||
1591 | */ | ||
1592 | static inline int _double_lock_balance(struct rq *this_rq, struct rq *busiest) | ||
1593 | __releases(this_rq->lock) | ||
1594 | __acquires(busiest->lock) | ||
1595 | __acquires(this_rq->lock) | ||
1596 | { | ||
1597 | raw_spin_unlock(&this_rq->lock); | ||
1598 | double_rq_lock(this_rq, busiest); | ||
1599 | |||
1600 | return 1; | ||
1601 | } | ||
1602 | |||
1603 | #else | ||
1604 | /* | ||
1605 | * Unfair double_lock_balance: Optimizes throughput at the expense of | ||
1606 | * latency by eliminating extra atomic operations when the locks are | ||
1607 | * already in proper order on entry. This favors lower cpu-ids and will | ||
1608 | * grant the double lock to lower cpus over higher ids under contention, | ||
1609 | * regardless of entry order into the function. | ||
1610 | */ | ||
1611 | static int _double_lock_balance(struct rq *this_rq, struct rq *busiest) | ||
1612 | __releases(this_rq->lock) | ||
1613 | __acquires(busiest->lock) | ||
1614 | __acquires(this_rq->lock) | ||
1615 | { | ||
1616 | int ret = 0; | ||
1617 | |||
1618 | if (unlikely(!raw_spin_trylock(&busiest->lock))) { | ||
1619 | if (busiest < this_rq) { | ||
1620 | raw_spin_unlock(&this_rq->lock); | ||
1621 | raw_spin_lock(&busiest->lock); | ||
1622 | raw_spin_lock_nested(&this_rq->lock, | ||
1623 | SINGLE_DEPTH_NESTING); | ||
1624 | ret = 1; | ||
1625 | } else | ||
1626 | raw_spin_lock_nested(&busiest->lock, | ||
1627 | SINGLE_DEPTH_NESTING); | ||
1628 | } | ||
1629 | return ret; | ||
1630 | } | ||
1631 | |||
1632 | #endif /* CONFIG_PREEMPT */ | ||
1633 | |||
1634 | /* | ||
1635 | * double_lock_balance - lock the busiest runqueue, this_rq is locked already. | ||
1636 | */ | ||
1637 | static int double_lock_balance(struct rq *this_rq, struct rq *busiest) | ||
1638 | { | ||
1639 | if (unlikely(!irqs_disabled())) { | ||
1640 | /* printk() doesn't work good under rq->lock */ | ||
1641 | raw_spin_unlock(&this_rq->lock); | ||
1642 | BUG_ON(1); | ||
1643 | } | ||
1644 | |||
1645 | return _double_lock_balance(this_rq, busiest); | ||
1646 | } | ||
1647 | |||
1648 | static inline void double_unlock_balance(struct rq *this_rq, struct rq *busiest) | ||
1649 | __releases(busiest->lock) | ||
1650 | { | ||
1651 | raw_spin_unlock(&busiest->lock); | ||
1652 | lock_set_subclass(&this_rq->lock.dep_map, 0, _RET_IP_); | ||
1653 | } | ||
1654 | |||
1655 | /* | ||
1656 | * double_rq_lock - safely lock two runqueues | ||
1657 | * | ||
1658 | * Note this does not disable interrupts like task_rq_lock, | ||
1659 | * you need to do so manually before calling. | ||
1660 | */ | ||
1661 | static void double_rq_lock(struct rq *rq1, struct rq *rq2) | ||
1662 | __acquires(rq1->lock) | ||
1663 | __acquires(rq2->lock) | ||
1664 | { | ||
1665 | BUG_ON(!irqs_disabled()); | ||
1666 | if (rq1 == rq2) { | ||
1667 | raw_spin_lock(&rq1->lock); | ||
1668 | __acquire(rq2->lock); /* Fake it out ;) */ | ||
1669 | } else { | ||
1670 | if (rq1 < rq2) { | ||
1671 | raw_spin_lock(&rq1->lock); | ||
1672 | raw_spin_lock_nested(&rq2->lock, SINGLE_DEPTH_NESTING); | ||
1673 | } else { | ||
1674 | raw_spin_lock(&rq2->lock); | ||
1675 | raw_spin_lock_nested(&rq1->lock, SINGLE_DEPTH_NESTING); | ||
1676 | } | ||
1677 | } | ||
1678 | } | ||
1679 | |||
1680 | /* | ||
1681 | * double_rq_unlock - safely unlock two runqueues | ||
1682 | * | ||
1683 | * Note this does not restore interrupts like task_rq_unlock, | ||
1684 | * you need to do so manually after calling. | ||
1685 | */ | ||
1686 | static void double_rq_unlock(struct rq *rq1, struct rq *rq2) | ||
1687 | __releases(rq1->lock) | ||
1688 | __releases(rq2->lock) | ||
1689 | { | ||
1690 | raw_spin_unlock(&rq1->lock); | ||
1691 | if (rq1 != rq2) | ||
1692 | raw_spin_unlock(&rq2->lock); | ||
1693 | else | ||
1694 | __release(rq2->lock); | ||
1695 | } | ||
1696 | |||
1697 | #else /* CONFIG_SMP */ | ||
1698 | |||
1699 | /* | ||
1700 | * double_rq_lock - safely lock two runqueues | ||
1701 | * | ||
1702 | * Note this does not disable interrupts like task_rq_lock, | ||
1703 | * you need to do so manually before calling. | ||
1704 | */ | ||
1705 | static void double_rq_lock(struct rq *rq1, struct rq *rq2) | ||
1706 | __acquires(rq1->lock) | ||
1707 | __acquires(rq2->lock) | ||
1708 | { | ||
1709 | BUG_ON(!irqs_disabled()); | ||
1710 | BUG_ON(rq1 != rq2); | ||
1711 | raw_spin_lock(&rq1->lock); | ||
1712 | __acquire(rq2->lock); /* Fake it out ;) */ | ||
1713 | } | ||
1714 | |||
1715 | /* | ||
1716 | * double_rq_unlock - safely unlock two runqueues | ||
1717 | * | ||
1718 | * Note this does not restore interrupts like task_rq_unlock, | ||
1719 | * you need to do so manually after calling. | ||
1720 | */ | ||
1721 | static void double_rq_unlock(struct rq *rq1, struct rq *rq2) | ||
1722 | __releases(rq1->lock) | ||
1723 | __releases(rq2->lock) | ||
1724 | { | ||
1725 | BUG_ON(rq1 != rq2); | ||
1726 | raw_spin_unlock(&rq1->lock); | ||
1727 | __release(rq2->lock); | ||
1728 | } | ||
1729 | |||
1730 | #endif | ||
1731 | |||
1732 | static void calc_load_account_idle(struct rq *this_rq); | ||
1733 | static void update_sysctl(void); | ||
1734 | static int get_update_sysctl_factor(void); | ||
1735 | static void update_cpu_load(struct rq *this_rq); | ||
1736 | |||
1737 | static inline void __set_task_cpu(struct task_struct *p, unsigned int cpu) | ||
1738 | { | ||
1739 | set_task_rq(p, cpu); | ||
1740 | #ifdef CONFIG_SMP | ||
1741 | /* | ||
1742 | * After ->cpu is set up to a new value, task_rq_lock(p, ...) can be | ||
1743 | * successfuly executed on another CPU. We must ensure that updates of | ||
1744 | * per-task data have been completed by this moment. | ||
1745 | */ | ||
1746 | smp_wmb(); | ||
1747 | task_thread_info(p)->cpu = cpu; | ||
1748 | #endif | ||
1749 | } | ||
1750 | |||
1751 | static const struct sched_class rt_sched_class; | ||
1752 | |||
1753 | #define sched_class_highest (&stop_sched_class) | ||
1754 | #define for_each_class(class) \ | ||
1755 | for (class = sched_class_highest; class; class = class->next) | ||
1756 | |||
1757 | #include "sched_stats.h" | ||
1758 | |||
1759 | static void inc_nr_running(struct rq *rq) | ||
1760 | { | ||
1761 | rq->nr_running++; | ||
1762 | } | ||
1763 | |||
1764 | static void dec_nr_running(struct rq *rq) | ||
1765 | { | ||
1766 | rq->nr_running--; | ||
1767 | } | ||
1768 | |||
1769 | static void set_load_weight(struct task_struct *p) | ||
1770 | { | ||
1771 | int prio = p->static_prio - MAX_RT_PRIO; | ||
1772 | struct load_weight *load = &p->se.load; | ||
1773 | |||
1774 | /* | ||
1775 | * SCHED_IDLE tasks get minimal weight: | ||
1776 | */ | ||
1777 | if (p->policy == SCHED_IDLE) { | ||
1778 | load->weight = scale_load(WEIGHT_IDLEPRIO); | ||
1779 | load->inv_weight = WMULT_IDLEPRIO; | ||
1780 | return; | ||
1781 | } | ||
1782 | |||
1783 | load->weight = scale_load(prio_to_weight[prio]); | ||
1784 | load->inv_weight = prio_to_wmult[prio]; | ||
1785 | } | ||
1786 | |||
1787 | static void enqueue_task(struct rq *rq, struct task_struct *p, int flags) | ||
1788 | { | ||
1789 | update_rq_clock(rq); | ||
1790 | sched_info_queued(p); | ||
1791 | p->sched_class->enqueue_task(rq, p, flags); | ||
1792 | } | ||
1793 | |||
1794 | static void dequeue_task(struct rq *rq, struct task_struct *p, int flags) | ||
1795 | { | ||
1796 | update_rq_clock(rq); | ||
1797 | sched_info_dequeued(p); | ||
1798 | p->sched_class->dequeue_task(rq, p, flags); | ||
1799 | } | ||
1800 | |||
1801 | /* | ||
1802 | * activate_task - move a task to the runqueue. | ||
1803 | */ | ||
1804 | static void activate_task(struct rq *rq, struct task_struct *p, int flags) | ||
1805 | { | ||
1806 | if (task_contributes_to_load(p)) | ||
1807 | rq->nr_uninterruptible--; | ||
1808 | |||
1809 | enqueue_task(rq, p, flags); | ||
1810 | inc_nr_running(rq); | ||
1811 | } | ||
1812 | |||
1813 | /* | ||
1814 | * deactivate_task - remove a task from the runqueue. | ||
1815 | */ | ||
1816 | static void deactivate_task(struct rq *rq, struct task_struct *p, int flags) | ||
1817 | { | ||
1818 | if (task_contributes_to_load(p)) | ||
1819 | rq->nr_uninterruptible++; | ||
1820 | |||
1821 | dequeue_task(rq, p, flags); | ||
1822 | dec_nr_running(rq); | ||
1823 | } | ||
1824 | |||
1825 | #ifdef CONFIG_IRQ_TIME_ACCOUNTING | ||
1826 | |||
1827 | /* | ||
1828 | * There are no locks covering percpu hardirq/softirq time. | ||
1829 | * They are only modified in account_system_vtime, on corresponding CPU | ||
1830 | * with interrupts disabled. So, writes are safe. | ||
1831 | * They are read and saved off onto struct rq in update_rq_clock(). | ||
1832 | * This may result in other CPU reading this CPU's irq time and can | ||
1833 | * race with irq/account_system_vtime on this CPU. We would either get old | ||
1834 | * or new value with a side effect of accounting a slice of irq time to wrong | ||
1835 | * task when irq is in progress while we read rq->clock. That is a worthy | ||
1836 | * compromise in place of having locks on each irq in account_system_time. | ||
1837 | */ | ||
1838 | static DEFINE_PER_CPU(u64, cpu_hardirq_time); | ||
1839 | static DEFINE_PER_CPU(u64, cpu_softirq_time); | ||
1840 | |||
1841 | static DEFINE_PER_CPU(u64, irq_start_time); | ||
1842 | static int sched_clock_irqtime; | ||
1843 | |||
1844 | void enable_sched_clock_irqtime(void) | ||
1845 | { | ||
1846 | sched_clock_irqtime = 1; | ||
1847 | } | ||
1848 | |||
1849 | void disable_sched_clock_irqtime(void) | ||
1850 | { | ||
1851 | sched_clock_irqtime = 0; | ||
1852 | } | ||
1853 | |||
1854 | #ifndef CONFIG_64BIT | ||
1855 | static DEFINE_PER_CPU(seqcount_t, irq_time_seq); | ||
1856 | |||
1857 | static inline void irq_time_write_begin(void) | ||
1858 | { | ||
1859 | __this_cpu_inc(irq_time_seq.sequence); | ||
1860 | smp_wmb(); | ||
1861 | } | ||
1862 | |||
1863 | static inline void irq_time_write_end(void) | ||
1864 | { | ||
1865 | smp_wmb(); | ||
1866 | __this_cpu_inc(irq_time_seq.sequence); | ||
1867 | } | ||
1868 | |||
1869 | static inline u64 irq_time_read(int cpu) | ||
1870 | { | ||
1871 | u64 irq_time; | ||
1872 | unsigned seq; | ||
1873 | |||
1874 | do { | ||
1875 | seq = read_seqcount_begin(&per_cpu(irq_time_seq, cpu)); | ||
1876 | irq_time = per_cpu(cpu_softirq_time, cpu) + | ||
1877 | per_cpu(cpu_hardirq_time, cpu); | ||
1878 | } while (read_seqcount_retry(&per_cpu(irq_time_seq, cpu), seq)); | ||
1879 | |||
1880 | return irq_time; | ||
1881 | } | ||
1882 | #else /* CONFIG_64BIT */ | ||
1883 | static inline void irq_time_write_begin(void) | ||
1884 | { | ||
1885 | } | ||
1886 | |||
1887 | static inline void irq_time_write_end(void) | ||
1888 | { | ||
1889 | } | ||
1890 | |||
1891 | static inline u64 irq_time_read(int cpu) | ||
1892 | { | ||
1893 | return per_cpu(cpu_softirq_time, cpu) + per_cpu(cpu_hardirq_time, cpu); | ||
1894 | } | ||
1895 | #endif /* CONFIG_64BIT */ | ||
1896 | |||
1897 | /* | ||
1898 | * Called before incrementing preempt_count on {soft,}irq_enter | ||
1899 | * and before decrementing preempt_count on {soft,}irq_exit. | ||
1900 | */ | ||
1901 | void account_system_vtime(struct task_struct *curr) | ||
1902 | { | ||
1903 | unsigned long flags; | ||
1904 | s64 delta; | ||
1905 | int cpu; | ||
1906 | |||
1907 | if (!sched_clock_irqtime) | ||
1908 | return; | ||
1909 | |||
1910 | local_irq_save(flags); | ||
1911 | |||
1912 | cpu = smp_processor_id(); | ||
1913 | delta = sched_clock_cpu(cpu) - __this_cpu_read(irq_start_time); | ||
1914 | __this_cpu_add(irq_start_time, delta); | ||
1915 | |||
1916 | irq_time_write_begin(); | ||
1917 | /* | ||
1918 | * We do not account for softirq time from ksoftirqd here. | ||
1919 | * We want to continue accounting softirq time to ksoftirqd thread | ||
1920 | * in that case, so as not to confuse scheduler with a special task | ||
1921 | * that do not consume any time, but still wants to run. | ||
1922 | */ | ||
1923 | if (hardirq_count()) | ||
1924 | __this_cpu_add(cpu_hardirq_time, delta); | ||
1925 | else if (in_serving_softirq() && curr != this_cpu_ksoftirqd()) | ||
1926 | __this_cpu_add(cpu_softirq_time, delta); | ||
1927 | |||
1928 | irq_time_write_end(); | ||
1929 | local_irq_restore(flags); | ||
1930 | } | ||
1931 | EXPORT_SYMBOL_GPL(account_system_vtime); | ||
1932 | |||
1933 | #endif /* CONFIG_IRQ_TIME_ACCOUNTING */ | ||
1934 | |||
1935 | #ifdef CONFIG_PARAVIRT | ||
1936 | static inline u64 steal_ticks(u64 steal) | ||
1937 | { | ||
1938 | if (unlikely(steal > NSEC_PER_SEC)) | ||
1939 | return div_u64(steal, TICK_NSEC); | ||
1940 | |||
1941 | return __iter_div_u64_rem(steal, TICK_NSEC, &steal); | ||
1942 | } | ||
1943 | #endif | ||
1944 | |||
1945 | static void update_rq_clock_task(struct rq *rq, s64 delta) | ||
1946 | { | ||
1947 | /* | ||
1948 | * In theory, the compile should just see 0 here, and optimize out the call | ||
1949 | * to sched_rt_avg_update. But I don't trust it... | ||
1950 | */ | ||
1951 | #if defined(CONFIG_IRQ_TIME_ACCOUNTING) || defined(CONFIG_PARAVIRT_TIME_ACCOUNTING) | ||
1952 | s64 steal = 0, irq_delta = 0; | ||
1953 | #endif | ||
1954 | #ifdef CONFIG_IRQ_TIME_ACCOUNTING | ||
1955 | irq_delta = irq_time_read(cpu_of(rq)) - rq->prev_irq_time; | ||
1956 | |||
1957 | /* | ||
1958 | * Since irq_time is only updated on {soft,}irq_exit, we might run into | ||
1959 | * this case when a previous update_rq_clock() happened inside a | ||
1960 | * {soft,}irq region. | ||
1961 | * | ||
1962 | * When this happens, we stop ->clock_task and only update the | ||
1963 | * prev_irq_time stamp to account for the part that fit, so that a next | ||
1964 | * update will consume the rest. This ensures ->clock_task is | ||
1965 | * monotonic. | ||
1966 | * | ||
1967 | * It does however cause some slight miss-attribution of {soft,}irq | ||
1968 | * time, a more accurate solution would be to update the irq_time using | ||
1969 | * the current rq->clock timestamp, except that would require using | ||
1970 | * atomic ops. | ||
1971 | */ | ||
1972 | if (irq_delta > delta) | ||
1973 | irq_delta = delta; | ||
1974 | |||
1975 | rq->prev_irq_time += irq_delta; | ||
1976 | delta -= irq_delta; | ||
1977 | #endif | ||
1978 | #ifdef CONFIG_PARAVIRT_TIME_ACCOUNTING | ||
1979 | if (static_branch((¶virt_steal_rq_enabled))) { | ||
1980 | u64 st; | ||
1981 | |||
1982 | steal = paravirt_steal_clock(cpu_of(rq)); | ||
1983 | steal -= rq->prev_steal_time_rq; | ||
1984 | |||
1985 | if (unlikely(steal > delta)) | ||
1986 | steal = delta; | ||
1987 | |||
1988 | st = steal_ticks(steal); | ||
1989 | steal = st * TICK_NSEC; | ||
1990 | |||
1991 | rq->prev_steal_time_rq += steal; | ||
1992 | |||
1993 | delta -= steal; | ||
1994 | } | ||
1995 | #endif | ||
1996 | |||
1997 | rq->clock_task += delta; | ||
1998 | |||
1999 | #if defined(CONFIG_IRQ_TIME_ACCOUNTING) || defined(CONFIG_PARAVIRT_TIME_ACCOUNTING) | ||
2000 | if ((irq_delta + steal) && sched_feat(NONTASK_POWER)) | ||
2001 | sched_rt_avg_update(rq, irq_delta + steal); | ||
2002 | #endif | ||
2003 | } | ||
2004 | |||
2005 | #ifdef CONFIG_IRQ_TIME_ACCOUNTING | ||
2006 | static int irqtime_account_hi_update(void) | ||
2007 | { | ||
2008 | struct cpu_usage_stat *cpustat = &kstat_this_cpu.cpustat; | ||
2009 | unsigned long flags; | ||
2010 | u64 latest_ns; | ||
2011 | int ret = 0; | ||
2012 | |||
2013 | local_irq_save(flags); | ||
2014 | latest_ns = this_cpu_read(cpu_hardirq_time); | ||
2015 | if (cputime64_gt(nsecs_to_cputime64(latest_ns), cpustat->irq)) | ||
2016 | ret = 1; | ||
2017 | local_irq_restore(flags); | ||
2018 | return ret; | ||
2019 | } | ||
2020 | |||
2021 | static int irqtime_account_si_update(void) | ||
2022 | { | ||
2023 | struct cpu_usage_stat *cpustat = &kstat_this_cpu.cpustat; | ||
2024 | unsigned long flags; | ||
2025 | u64 latest_ns; | ||
2026 | int ret = 0; | ||
2027 | |||
2028 | local_irq_save(flags); | ||
2029 | latest_ns = this_cpu_read(cpu_softirq_time); | ||
2030 | if (cputime64_gt(nsecs_to_cputime64(latest_ns), cpustat->softirq)) | ||
2031 | ret = 1; | ||
2032 | local_irq_restore(flags); | ||
2033 | return ret; | ||
2034 | } | ||
2035 | |||
2036 | #else /* CONFIG_IRQ_TIME_ACCOUNTING */ | ||
2037 | |||
2038 | #define sched_clock_irqtime (0) | ||
2039 | |||
2040 | #endif | ||
2041 | |||
2042 | #include "sched_idletask.c" | ||
2043 | #include "sched_fair.c" | ||
2044 | #include "sched_rt.c" | ||
2045 | #include "sched_autogroup.c" | ||
2046 | #include "sched_stoptask.c" | ||
2047 | #ifdef CONFIG_SCHED_DEBUG | ||
2048 | # include "sched_debug.c" | ||
2049 | #endif | ||
2050 | |||
2051 | void sched_set_stop_task(int cpu, struct task_struct *stop) | ||
2052 | { | ||
2053 | struct sched_param param = { .sched_priority = MAX_RT_PRIO - 1 }; | ||
2054 | struct task_struct *old_stop = cpu_rq(cpu)->stop; | ||
2055 | |||
2056 | if (stop) { | ||
2057 | /* | ||
2058 | * Make it appear like a SCHED_FIFO task, its something | ||
2059 | * userspace knows about and won't get confused about. | ||
2060 | * | ||
2061 | * Also, it will make PI more or less work without too | ||
2062 | * much confusion -- but then, stop work should not | ||
2063 | * rely on PI working anyway. | ||
2064 | */ | ||
2065 | sched_setscheduler_nocheck(stop, SCHED_FIFO, ¶m); | ||
2066 | |||
2067 | stop->sched_class = &stop_sched_class; | ||
2068 | } | ||
2069 | |||
2070 | cpu_rq(cpu)->stop = stop; | ||
2071 | |||
2072 | if (old_stop) { | ||
2073 | /* | ||
2074 | * Reset it back to a normal scheduling class so that | ||
2075 | * it can die in pieces. | ||
2076 | */ | ||
2077 | old_stop->sched_class = &rt_sched_class; | ||
2078 | } | ||
2079 | } | ||
2080 | |||
2081 | /* | ||
2082 | * __normal_prio - return the priority that is based on the static prio | ||
2083 | */ | ||
2084 | static inline int __normal_prio(struct task_struct *p) | ||
2085 | { | ||
2086 | return p->static_prio; | ||
2087 | } | ||
2088 | |||
2089 | /* | ||
2090 | * Calculate the expected normal priority: i.e. priority | ||
2091 | * without taking RT-inheritance into account. Might be | ||
2092 | * boosted by interactivity modifiers. Changes upon fork, | ||
2093 | * setprio syscalls, and whenever the interactivity | ||
2094 | * estimator recalculates. | ||
2095 | */ | ||
2096 | static inline int normal_prio(struct task_struct *p) | ||
2097 | { | ||
2098 | int prio; | ||
2099 | |||
2100 | if (task_has_rt_policy(p)) | ||
2101 | prio = MAX_RT_PRIO-1 - p->rt_priority; | ||
2102 | else | ||
2103 | prio = __normal_prio(p); | ||
2104 | return prio; | ||
2105 | } | ||
2106 | |||
2107 | /* | ||
2108 | * Calculate the current priority, i.e. the priority | ||
2109 | * taken into account by the scheduler. This value might | ||
2110 | * be boosted by RT tasks, or might be boosted by | ||
2111 | * interactivity modifiers. Will be RT if the task got | ||
2112 | * RT-boosted. If not then it returns p->normal_prio. | ||
2113 | */ | ||
2114 | static int effective_prio(struct task_struct *p) | ||
2115 | { | ||
2116 | p->normal_prio = normal_prio(p); | ||
2117 | /* | ||
2118 | * If we are RT tasks or we were boosted to RT priority, | ||
2119 | * keep the priority unchanged. Otherwise, update priority | ||
2120 | * to the normal priority: | ||
2121 | */ | ||
2122 | if (!rt_prio(p->prio)) | ||
2123 | return p->normal_prio; | ||
2124 | return p->prio; | ||
2125 | } | ||
2126 | |||
2127 | /** | ||
2128 | * task_curr - is this task currently executing on a CPU? | ||
2129 | * @p: the task in question. | ||
2130 | */ | ||
2131 | inline int task_curr(const struct task_struct *p) | ||
2132 | { | ||
2133 | return cpu_curr(task_cpu(p)) == p; | ||
2134 | } | ||
2135 | |||
2136 | static inline void check_class_changed(struct rq *rq, struct task_struct *p, | ||
2137 | const struct sched_class *prev_class, | ||
2138 | int oldprio) | ||
2139 | { | ||
2140 | if (prev_class != p->sched_class) { | ||
2141 | if (prev_class->switched_from) | ||
2142 | prev_class->switched_from(rq, p); | ||
2143 | p->sched_class->switched_to(rq, p); | ||
2144 | } else if (oldprio != p->prio) | ||
2145 | p->sched_class->prio_changed(rq, p, oldprio); | ||
2146 | } | ||
2147 | |||
2148 | static void check_preempt_curr(struct rq *rq, struct task_struct *p, int flags) | ||
2149 | { | ||
2150 | const struct sched_class *class; | ||
2151 | |||
2152 | if (p->sched_class == rq->curr->sched_class) { | ||
2153 | rq->curr->sched_class->check_preempt_curr(rq, p, flags); | ||
2154 | } else { | ||
2155 | for_each_class(class) { | ||
2156 | if (class == rq->curr->sched_class) | ||
2157 | break; | ||
2158 | if (class == p->sched_class) { | ||
2159 | resched_task(rq->curr); | ||
2160 | break; | ||
2161 | } | ||
2162 | } | ||
2163 | } | ||
2164 | |||
2165 | /* | ||
2166 | * A queue event has occurred, and we're going to schedule. In | ||
2167 | * this case, we can save a useless back to back clock update. | ||
2168 | */ | ||
2169 | if (rq->curr->on_rq && test_tsk_need_resched(rq->curr)) | ||
2170 | rq->skip_clock_update = 1; | ||
2171 | } | ||
2172 | |||
2173 | #ifdef CONFIG_SMP | ||
2174 | /* | ||
2175 | * Is this task likely cache-hot: | ||
2176 | */ | ||
2177 | static int | ||
2178 | task_hot(struct task_struct *p, u64 now, struct sched_domain *sd) | ||
2179 | { | ||
2180 | s64 delta; | ||
2181 | |||
2182 | if (p->sched_class != &fair_sched_class) | ||
2183 | return 0; | ||
2184 | |||
2185 | if (unlikely(p->policy == SCHED_IDLE)) | ||
2186 | return 0; | ||
2187 | |||
2188 | /* | ||
2189 | * Buddy candidates are cache hot: | ||
2190 | */ | ||
2191 | if (sched_feat(CACHE_HOT_BUDDY) && this_rq()->nr_running && | ||
2192 | (&p->se == cfs_rq_of(&p->se)->next || | ||
2193 | &p->se == cfs_rq_of(&p->se)->last)) | ||
2194 | return 1; | ||
2195 | |||
2196 | if (sysctl_sched_migration_cost == -1) | ||
2197 | return 1; | ||
2198 | if (sysctl_sched_migration_cost == 0) | ||
2199 | return 0; | ||
2200 | |||
2201 | delta = now - p->se.exec_start; | ||
2202 | |||
2203 | return delta < (s64)sysctl_sched_migration_cost; | ||
2204 | } | ||
2205 | |||
2206 | void set_task_cpu(struct task_struct *p, unsigned int new_cpu) | ||
2207 | { | ||
2208 | #ifdef CONFIG_SCHED_DEBUG | ||
2209 | /* | ||
2210 | * We should never call set_task_cpu() on a blocked task, | ||
2211 | * ttwu() will sort out the placement. | ||
2212 | */ | ||
2213 | WARN_ON_ONCE(p->state != TASK_RUNNING && p->state != TASK_WAKING && | ||
2214 | !(task_thread_info(p)->preempt_count & PREEMPT_ACTIVE)); | ||
2215 | |||
2216 | #ifdef CONFIG_LOCKDEP | ||
2217 | /* | ||
2218 | * The caller should hold either p->pi_lock or rq->lock, when changing | ||
2219 | * a task's CPU. ->pi_lock for waking tasks, rq->lock for runnable tasks. | ||
2220 | * | ||
2221 | * sched_move_task() holds both and thus holding either pins the cgroup, | ||
2222 | * see set_task_rq(). | ||
2223 | * | ||
2224 | * Furthermore, all task_rq users should acquire both locks, see | ||
2225 | * task_rq_lock(). | ||
2226 | */ | ||
2227 | WARN_ON_ONCE(debug_locks && !(lockdep_is_held(&p->pi_lock) || | ||
2228 | lockdep_is_held(&task_rq(p)->lock))); | ||
2229 | #endif | ||
2230 | #endif | ||
2231 | |||
2232 | trace_sched_migrate_task(p, new_cpu); | ||
2233 | |||
2234 | if (task_cpu(p) != new_cpu) { | ||
2235 | p->se.nr_migrations++; | ||
2236 | perf_sw_event(PERF_COUNT_SW_CPU_MIGRATIONS, 1, NULL, 0); | ||
2237 | } | ||
2238 | |||
2239 | __set_task_cpu(p, new_cpu); | ||
2240 | } | ||
2241 | |||
2242 | struct migration_arg { | ||
2243 | struct task_struct *task; | ||
2244 | int dest_cpu; | ||
2245 | }; | ||
2246 | |||
2247 | static int migration_cpu_stop(void *data); | ||
2248 | |||
2249 | /* | ||
2250 | * wait_task_inactive - wait for a thread to unschedule. | ||
2251 | * | ||
2252 | * If @match_state is nonzero, it's the @p->state value just checked and | ||
2253 | * not expected to change. If it changes, i.e. @p might have woken up, | ||
2254 | * then return zero. When we succeed in waiting for @p to be off its CPU, | ||
2255 | * we return a positive number (its total switch count). If a second call | ||
2256 | * a short while later returns the same number, the caller can be sure that | ||
2257 | * @p has remained unscheduled the whole time. | ||
2258 | * | ||
2259 | * The caller must ensure that the task *will* unschedule sometime soon, | ||
2260 | * else this function might spin for a *long* time. This function can't | ||
2261 | * be called with interrupts off, or it may introduce deadlock with | ||
2262 | * smp_call_function() if an IPI is sent by the same process we are | ||
2263 | * waiting to become inactive. | ||
2264 | */ | ||
2265 | unsigned long wait_task_inactive(struct task_struct *p, long match_state) | ||
2266 | { | ||
2267 | unsigned long flags; | ||
2268 | int running, on_rq; | ||
2269 | unsigned long ncsw; | ||
2270 | struct rq *rq; | ||
2271 | |||
2272 | for (;;) { | ||
2273 | /* | ||
2274 | * We do the initial early heuristics without holding | ||
2275 | * any task-queue locks at all. We'll only try to get | ||
2276 | * the runqueue lock when things look like they will | ||
2277 | * work out! | ||
2278 | */ | ||
2279 | rq = task_rq(p); | ||
2280 | |||
2281 | /* | ||
2282 | * If the task is actively running on another CPU | ||
2283 | * still, just relax and busy-wait without holding | ||
2284 | * any locks. | ||
2285 | * | ||
2286 | * NOTE! Since we don't hold any locks, it's not | ||
2287 | * even sure that "rq" stays as the right runqueue! | ||
2288 | * But we don't care, since "task_running()" will | ||
2289 | * return false if the runqueue has changed and p | ||
2290 | * is actually now running somewhere else! | ||
2291 | */ | ||
2292 | while (task_running(rq, p)) { | ||
2293 | if (match_state && unlikely(p->state != match_state)) | ||
2294 | return 0; | ||
2295 | cpu_relax(); | ||
2296 | } | ||
2297 | |||
2298 | /* | ||
2299 | * Ok, time to look more closely! We need the rq | ||
2300 | * lock now, to be *sure*. If we're wrong, we'll | ||
2301 | * just go back and repeat. | ||
2302 | */ | ||
2303 | rq = task_rq_lock(p, &flags); | ||
2304 | trace_sched_wait_task(p); | ||
2305 | running = task_running(rq, p); | ||
2306 | on_rq = p->on_rq; | ||
2307 | ncsw = 0; | ||
2308 | if (!match_state || p->state == match_state) | ||
2309 | ncsw = p->nvcsw | LONG_MIN; /* sets MSB */ | ||
2310 | task_rq_unlock(rq, p, &flags); | ||
2311 | |||
2312 | /* | ||
2313 | * If it changed from the expected state, bail out now. | ||
2314 | */ | ||
2315 | if (unlikely(!ncsw)) | ||
2316 | break; | ||
2317 | |||
2318 | /* | ||
2319 | * Was it really running after all now that we | ||
2320 | * checked with the proper locks actually held? | ||
2321 | * | ||
2322 | * Oops. Go back and try again.. | ||
2323 | */ | ||
2324 | if (unlikely(running)) { | ||
2325 | cpu_relax(); | ||
2326 | continue; | ||
2327 | } | ||
2328 | |||
2329 | /* | ||
2330 | * It's not enough that it's not actively running, | ||
2331 | * it must be off the runqueue _entirely_, and not | ||
2332 | * preempted! | ||
2333 | * | ||
2334 | * So if it was still runnable (but just not actively | ||
2335 | * running right now), it's preempted, and we should | ||
2336 | * yield - it could be a while. | ||
2337 | */ | ||
2338 | if (unlikely(on_rq)) { | ||
2339 | ktime_t to = ktime_set(0, NSEC_PER_SEC/HZ); | ||
2340 | |||
2341 | set_current_state(TASK_UNINTERRUPTIBLE); | ||
2342 | schedule_hrtimeout(&to, HRTIMER_MODE_REL); | ||
2343 | continue; | ||
2344 | } | ||
2345 | |||
2346 | /* | ||
2347 | * Ahh, all good. It wasn't running, and it wasn't | ||
2348 | * runnable, which means that it will never become | ||
2349 | * running in the future either. We're all done! | ||
2350 | */ | ||
2351 | break; | ||
2352 | } | ||
2353 | |||
2354 | return ncsw; | ||
2355 | } | ||
2356 | |||
2357 | /*** | ||
2358 | * kick_process - kick a running thread to enter/exit the kernel | ||
2359 | * @p: the to-be-kicked thread | ||
2360 | * | ||
2361 | * Cause a process which is running on another CPU to enter | ||
2362 | * kernel-mode, without any delay. (to get signals handled.) | ||
2363 | * | ||
2364 | * NOTE: this function doesn't have to take the runqueue lock, | ||
2365 | * because all it wants to ensure is that the remote task enters | ||
2366 | * the kernel. If the IPI races and the task has been migrated | ||
2367 | * to another CPU then no harm is done and the purpose has been | ||
2368 | * achieved as well. | ||
2369 | */ | ||
2370 | void kick_process(struct task_struct *p) | ||
2371 | { | ||
2372 | int cpu; | ||
2373 | |||
2374 | preempt_disable(); | ||
2375 | cpu = task_cpu(p); | ||
2376 | if ((cpu != smp_processor_id()) && task_curr(p)) | ||
2377 | smp_send_reschedule(cpu); | ||
2378 | preempt_enable(); | ||
2379 | } | ||
2380 | EXPORT_SYMBOL_GPL(kick_process); | ||
2381 | #endif /* CONFIG_SMP */ | ||
2382 | |||
2383 | #ifdef CONFIG_SMP | ||
2384 | /* | ||
2385 | * ->cpus_allowed is protected by both rq->lock and p->pi_lock | ||
2386 | */ | ||
2387 | static int select_fallback_rq(int cpu, struct task_struct *p) | ||
2388 | { | ||
2389 | int dest_cpu; | ||
2390 | const struct cpumask *nodemask = cpumask_of_node(cpu_to_node(cpu)); | ||
2391 | |||
2392 | /* Look for allowed, online CPU in same node. */ | ||
2393 | for_each_cpu_and(dest_cpu, nodemask, cpu_active_mask) | ||
2394 | if (cpumask_test_cpu(dest_cpu, &p->cpus_allowed)) | ||
2395 | return dest_cpu; | ||
2396 | |||
2397 | /* Any allowed, online CPU? */ | ||
2398 | dest_cpu = cpumask_any_and(&p->cpus_allowed, cpu_active_mask); | ||
2399 | if (dest_cpu < nr_cpu_ids) | ||
2400 | return dest_cpu; | ||
2401 | |||
2402 | /* No more Mr. Nice Guy. */ | ||
2403 | dest_cpu = cpuset_cpus_allowed_fallback(p); | ||
2404 | /* | ||
2405 | * Don't tell them about moving exiting tasks or | ||
2406 | * kernel threads (both mm NULL), since they never | ||
2407 | * leave kernel. | ||
2408 | */ | ||
2409 | if (p->mm && printk_ratelimit()) { | ||
2410 | printk(KERN_INFO "process %d (%s) no longer affine to cpu%d\n", | ||
2411 | task_pid_nr(p), p->comm, cpu); | ||
2412 | } | ||
2413 | |||
2414 | return dest_cpu; | ||
2415 | } | ||
2416 | |||
2417 | /* | ||
2418 | * The caller (fork, wakeup) owns p->pi_lock, ->cpus_allowed is stable. | ||
2419 | */ | ||
2420 | static inline | ||
2421 | int select_task_rq(struct task_struct *p, int sd_flags, int wake_flags) | ||
2422 | { | ||
2423 | int cpu = p->sched_class->select_task_rq(p, sd_flags, wake_flags); | ||
2424 | |||
2425 | /* | ||
2426 | * In order not to call set_task_cpu() on a blocking task we need | ||
2427 | * to rely on ttwu() to place the task on a valid ->cpus_allowed | ||
2428 | * cpu. | ||
2429 | * | ||
2430 | * Since this is common to all placement strategies, this lives here. | ||
2431 | * | ||
2432 | * [ this allows ->select_task() to simply return task_cpu(p) and | ||
2433 | * not worry about this generic constraint ] | ||
2434 | */ | ||
2435 | if (unlikely(!cpumask_test_cpu(cpu, &p->cpus_allowed) || | ||
2436 | !cpu_online(cpu))) | ||
2437 | cpu = select_fallback_rq(task_cpu(p), p); | ||
2438 | |||
2439 | return cpu; | ||
2440 | } | ||
2441 | |||
2442 | static void update_avg(u64 *avg, u64 sample) | ||
2443 | { | ||
2444 | s64 diff = sample - *avg; | ||
2445 | *avg += diff >> 3; | ||
2446 | } | ||
2447 | #endif | ||
2448 | |||
2449 | static void | ||
2450 | ttwu_stat(struct task_struct *p, int cpu, int wake_flags) | ||
2451 | { | ||
2452 | #ifdef CONFIG_SCHEDSTATS | ||
2453 | struct rq *rq = this_rq(); | ||
2454 | |||
2455 | #ifdef CONFIG_SMP | ||
2456 | int this_cpu = smp_processor_id(); | ||
2457 | |||
2458 | if (cpu == this_cpu) { | ||
2459 | schedstat_inc(rq, ttwu_local); | ||
2460 | schedstat_inc(p, se.statistics.nr_wakeups_local); | ||
2461 | } else { | ||
2462 | struct sched_domain *sd; | ||
2463 | |||
2464 | schedstat_inc(p, se.statistics.nr_wakeups_remote); | ||
2465 | rcu_read_lock(); | ||
2466 | for_each_domain(this_cpu, sd) { | ||
2467 | if (cpumask_test_cpu(cpu, sched_domain_span(sd))) { | ||
2468 | schedstat_inc(sd, ttwu_wake_remote); | ||
2469 | break; | ||
2470 | } | ||
2471 | } | ||
2472 | rcu_read_unlock(); | ||
2473 | } | ||
2474 | |||
2475 | if (wake_flags & WF_MIGRATED) | ||
2476 | schedstat_inc(p, se.statistics.nr_wakeups_migrate); | ||
2477 | |||
2478 | #endif /* CONFIG_SMP */ | ||
2479 | |||
2480 | schedstat_inc(rq, ttwu_count); | ||
2481 | schedstat_inc(p, se.statistics.nr_wakeups); | ||
2482 | |||
2483 | if (wake_flags & WF_SYNC) | ||
2484 | schedstat_inc(p, se.statistics.nr_wakeups_sync); | ||
2485 | |||
2486 | #endif /* CONFIG_SCHEDSTATS */ | ||
2487 | } | ||
2488 | |||
2489 | static void ttwu_activate(struct rq *rq, struct task_struct *p, int en_flags) | ||
2490 | { | ||
2491 | activate_task(rq, p, en_flags); | ||
2492 | p->on_rq = 1; | ||
2493 | |||
2494 | /* if a worker is waking up, notify workqueue */ | ||
2495 | if (p->flags & PF_WQ_WORKER) | ||
2496 | wq_worker_waking_up(p, cpu_of(rq)); | ||
2497 | } | ||
2498 | |||
2499 | /* | ||
2500 | * Mark the task runnable and perform wakeup-preemption. | ||
2501 | */ | ||
2502 | static void | ||
2503 | ttwu_do_wakeup(struct rq *rq, struct task_struct *p, int wake_flags) | ||
2504 | { | ||
2505 | trace_sched_wakeup(p, true); | ||
2506 | check_preempt_curr(rq, p, wake_flags); | ||
2507 | |||
2508 | p->state = TASK_RUNNING; | ||
2509 | #ifdef CONFIG_SMP | ||
2510 | if (p->sched_class->task_woken) | ||
2511 | p->sched_class->task_woken(rq, p); | ||
2512 | |||
2513 | if (rq->idle_stamp) { | ||
2514 | u64 delta = rq->clock - rq->idle_stamp; | ||
2515 | u64 max = 2*sysctl_sched_migration_cost; | ||
2516 | |||
2517 | if (delta > max) | ||
2518 | rq->avg_idle = max; | ||
2519 | else | ||
2520 | update_avg(&rq->avg_idle, delta); | ||
2521 | rq->idle_stamp = 0; | ||
2522 | } | ||
2523 | #endif | ||
2524 | } | ||
2525 | |||
2526 | static void | ||
2527 | ttwu_do_activate(struct rq *rq, struct task_struct *p, int wake_flags) | ||
2528 | { | ||
2529 | #ifdef CONFIG_SMP | ||
2530 | if (p->sched_contributes_to_load) | ||
2531 | rq->nr_uninterruptible--; | ||
2532 | #endif | ||
2533 | |||
2534 | ttwu_activate(rq, p, ENQUEUE_WAKEUP | ENQUEUE_WAKING); | ||
2535 | ttwu_do_wakeup(rq, p, wake_flags); | ||
2536 | } | ||
2537 | |||
2538 | /* | ||
2539 | * Called in case the task @p isn't fully descheduled from its runqueue, | ||
2540 | * in this case we must do a remote wakeup. Its a 'light' wakeup though, | ||
2541 | * since all we need to do is flip p->state to TASK_RUNNING, since | ||
2542 | * the task is still ->on_rq. | ||
2543 | */ | ||
2544 | static int ttwu_remote(struct task_struct *p, int wake_flags) | ||
2545 | { | ||
2546 | struct rq *rq; | ||
2547 | int ret = 0; | ||
2548 | |||
2549 | rq = __task_rq_lock(p); | ||
2550 | if (p->on_rq) { | ||
2551 | ttwu_do_wakeup(rq, p, wake_flags); | ||
2552 | ret = 1; | ||
2553 | } | ||
2554 | __task_rq_unlock(rq); | ||
2555 | |||
2556 | return ret; | ||
2557 | } | ||
2558 | |||
2559 | #ifdef CONFIG_SMP | ||
2560 | static void sched_ttwu_do_pending(struct task_struct *list) | ||
2561 | { | ||
2562 | struct rq *rq = this_rq(); | ||
2563 | |||
2564 | raw_spin_lock(&rq->lock); | ||
2565 | |||
2566 | while (list) { | ||
2567 | struct task_struct *p = list; | ||
2568 | list = list->wake_entry; | ||
2569 | ttwu_do_activate(rq, p, 0); | ||
2570 | } | ||
2571 | |||
2572 | raw_spin_unlock(&rq->lock); | ||
2573 | } | ||
2574 | |||
2575 | #ifdef CONFIG_HOTPLUG_CPU | ||
2576 | |||
2577 | static void sched_ttwu_pending(void) | ||
2578 | { | ||
2579 | struct rq *rq = this_rq(); | ||
2580 | struct task_struct *list = xchg(&rq->wake_list, NULL); | ||
2581 | |||
2582 | if (!list) | ||
2583 | return; | ||
2584 | |||
2585 | sched_ttwu_do_pending(list); | ||
2586 | } | ||
2587 | |||
2588 | #endif /* CONFIG_HOTPLUG_CPU */ | ||
2589 | |||
2590 | void scheduler_ipi(void) | ||
2591 | { | ||
2592 | struct rq *rq = this_rq(); | ||
2593 | struct task_struct *list = xchg(&rq->wake_list, NULL); | ||
2594 | |||
2595 | if (!list) | ||
2596 | return; | ||
2597 | |||
2598 | /* | ||
2599 | * Not all reschedule IPI handlers call irq_enter/irq_exit, since | ||
2600 | * traditionally all their work was done from the interrupt return | ||
2601 | * path. Now that we actually do some work, we need to make sure | ||
2602 | * we do call them. | ||
2603 | * | ||
2604 | * Some archs already do call them, luckily irq_enter/exit nest | ||
2605 | * properly. | ||
2606 | * | ||
2607 | * Arguably we should visit all archs and update all handlers, | ||
2608 | * however a fair share of IPIs are still resched only so this would | ||
2609 | * somewhat pessimize the simple resched case. | ||
2610 | */ | ||
2611 | irq_enter(); | ||
2612 | sched_ttwu_do_pending(list); | ||
2613 | irq_exit(); | ||
2614 | } | ||
2615 | |||
2616 | static void ttwu_queue_remote(struct task_struct *p, int cpu) | ||
2617 | { | ||
2618 | struct rq *rq = cpu_rq(cpu); | ||
2619 | struct task_struct *next = rq->wake_list; | ||
2620 | |||
2621 | for (;;) { | ||
2622 | struct task_struct *old = next; | ||
2623 | |||
2624 | p->wake_entry = next; | ||
2625 | next = cmpxchg(&rq->wake_list, old, p); | ||
2626 | if (next == old) | ||
2627 | break; | ||
2628 | } | ||
2629 | |||
2630 | if (!next) | ||
2631 | smp_send_reschedule(cpu); | ||
2632 | } | ||
2633 | |||
2634 | #ifdef __ARCH_WANT_INTERRUPTS_ON_CTXSW | ||
2635 | static int ttwu_activate_remote(struct task_struct *p, int wake_flags) | ||
2636 | { | ||
2637 | struct rq *rq; | ||
2638 | int ret = 0; | ||
2639 | |||
2640 | rq = __task_rq_lock(p); | ||
2641 | if (p->on_cpu) { | ||
2642 | ttwu_activate(rq, p, ENQUEUE_WAKEUP); | ||
2643 | ttwu_do_wakeup(rq, p, wake_flags); | ||
2644 | ret = 1; | ||
2645 | } | ||
2646 | __task_rq_unlock(rq); | ||
2647 | |||
2648 | return ret; | ||
2649 | |||
2650 | } | ||
2651 | #endif /* __ARCH_WANT_INTERRUPTS_ON_CTXSW */ | ||
2652 | #endif /* CONFIG_SMP */ | ||
2653 | |||
2654 | static void ttwu_queue(struct task_struct *p, int cpu) | ||
2655 | { | ||
2656 | struct rq *rq = cpu_rq(cpu); | ||
2657 | |||
2658 | #if defined(CONFIG_SMP) | ||
2659 | if (sched_feat(TTWU_QUEUE) && cpu != smp_processor_id()) { | ||
2660 | sched_clock_cpu(cpu); /* sync clocks x-cpu */ | ||
2661 | ttwu_queue_remote(p, cpu); | ||
2662 | return; | ||
2663 | } | ||
2664 | #endif | ||
2665 | |||
2666 | raw_spin_lock(&rq->lock); | ||
2667 | ttwu_do_activate(rq, p, 0); | ||
2668 | raw_spin_unlock(&rq->lock); | ||
2669 | } | ||
2670 | |||
2671 | /** | ||
2672 | * try_to_wake_up - wake up a thread | ||
2673 | * @p: the thread to be awakened | ||
2674 | * @state: the mask of task states that can be woken | ||
2675 | * @wake_flags: wake modifier flags (WF_*) | ||
2676 | * | ||
2677 | * Put it on the run-queue if it's not already there. The "current" | ||
2678 | * thread is always on the run-queue (except when the actual | ||
2679 | * re-schedule is in progress), and as such you're allowed to do | ||
2680 | * the simpler "current->state = TASK_RUNNING" to mark yourself | ||
2681 | * runnable without the overhead of this. | ||
2682 | * | ||
2683 | * Returns %true if @p was woken up, %false if it was already running | ||
2684 | * or @state didn't match @p's state. | ||
2685 | */ | ||
2686 | static int | ||
2687 | try_to_wake_up(struct task_struct *p, unsigned int state, int wake_flags) | ||
2688 | { | ||
2689 | unsigned long flags; | ||
2690 | int cpu, success = 0; | ||
2691 | |||
2692 | smp_wmb(); | ||
2693 | raw_spin_lock_irqsave(&p->pi_lock, flags); | ||
2694 | if (!(p->state & state)) | ||
2695 | goto out; | ||
2696 | |||
2697 | success = 1; /* we're going to change ->state */ | ||
2698 | cpu = task_cpu(p); | ||
2699 | |||
2700 | if (p->on_rq && ttwu_remote(p, wake_flags)) | ||
2701 | goto stat; | ||
2702 | |||
2703 | #ifdef CONFIG_SMP | ||
2704 | /* | ||
2705 | * If the owning (remote) cpu is still in the middle of schedule() with | ||
2706 | * this task as prev, wait until its done referencing the task. | ||
2707 | */ | ||
2708 | while (p->on_cpu) { | ||
2709 | #ifdef __ARCH_WANT_INTERRUPTS_ON_CTXSW | ||
2710 | /* | ||
2711 | * In case the architecture enables interrupts in | ||
2712 | * context_switch(), we cannot busy wait, since that | ||
2713 | * would lead to deadlocks when an interrupt hits and | ||
2714 | * tries to wake up @prev. So bail and do a complete | ||
2715 | * remote wakeup. | ||
2716 | */ | ||
2717 | if (ttwu_activate_remote(p, wake_flags)) | ||
2718 | goto stat; | ||
2719 | #else | ||
2720 | cpu_relax(); | ||
2721 | #endif | ||
2722 | } | ||
2723 | /* | ||
2724 | * Pairs with the smp_wmb() in finish_lock_switch(). | ||
2725 | */ | ||
2726 | smp_rmb(); | ||
2727 | |||
2728 | p->sched_contributes_to_load = !!task_contributes_to_load(p); | ||
2729 | p->state = TASK_WAKING; | ||
2730 | |||
2731 | if (p->sched_class->task_waking) | ||
2732 | p->sched_class->task_waking(p); | ||
2733 | |||
2734 | cpu = select_task_rq(p, SD_BALANCE_WAKE, wake_flags); | ||
2735 | if (task_cpu(p) != cpu) { | ||
2736 | wake_flags |= WF_MIGRATED; | ||
2737 | set_task_cpu(p, cpu); | ||
2738 | } | ||
2739 | #endif /* CONFIG_SMP */ | ||
2740 | |||
2741 | ttwu_queue(p, cpu); | ||
2742 | stat: | ||
2743 | ttwu_stat(p, cpu, wake_flags); | ||
2744 | out: | ||
2745 | raw_spin_unlock_irqrestore(&p->pi_lock, flags); | ||
2746 | |||
2747 | return success; | ||
2748 | } | ||
2749 | |||
2750 | /** | ||
2751 | * try_to_wake_up_local - try to wake up a local task with rq lock held | ||
2752 | * @p: the thread to be awakened | ||
2753 | * | ||
2754 | * Put @p on the run-queue if it's not already there. The caller must | ||
2755 | * ensure that this_rq() is locked, @p is bound to this_rq() and not | ||
2756 | * the current task. | ||
2757 | */ | ||
2758 | static void try_to_wake_up_local(struct task_struct *p) | ||
2759 | { | ||
2760 | struct rq *rq = task_rq(p); | ||
2761 | |||
2762 | BUG_ON(rq != this_rq()); | ||
2763 | BUG_ON(p == current); | ||
2764 | lockdep_assert_held(&rq->lock); | ||
2765 | |||
2766 | if (!raw_spin_trylock(&p->pi_lock)) { | ||
2767 | raw_spin_unlock(&rq->lock); | ||
2768 | raw_spin_lock(&p->pi_lock); | ||
2769 | raw_spin_lock(&rq->lock); | ||
2770 | } | ||
2771 | |||
2772 | if (!(p->state & TASK_NORMAL)) | ||
2773 | goto out; | ||
2774 | |||
2775 | if (!p->on_rq) | ||
2776 | ttwu_activate(rq, p, ENQUEUE_WAKEUP); | ||
2777 | |||
2778 | ttwu_do_wakeup(rq, p, 0); | ||
2779 | ttwu_stat(p, smp_processor_id(), 0); | ||
2780 | out: | ||
2781 | raw_spin_unlock(&p->pi_lock); | ||
2782 | } | ||
2783 | |||
2784 | /** | ||
2785 | * wake_up_process - Wake up a specific process | ||
2786 | * @p: The process to be woken up. | ||
2787 | * | ||
2788 | * Attempt to wake up the nominated process and move it to the set of runnable | ||
2789 | * processes. Returns 1 if the process was woken up, 0 if it was already | ||
2790 | * running. | ||
2791 | * | ||
2792 | * It may be assumed that this function implies a write memory barrier before | ||
2793 | * changing the task state if and only if any tasks are woken up. | ||
2794 | */ | ||
2795 | int wake_up_process(struct task_struct *p) | ||
2796 | { | ||
2797 | return try_to_wake_up(p, TASK_ALL, 0); | ||
2798 | } | ||
2799 | EXPORT_SYMBOL(wake_up_process); | ||
2800 | |||
2801 | int wake_up_state(struct task_struct *p, unsigned int state) | ||
2802 | { | ||
2803 | return try_to_wake_up(p, state, 0); | ||
2804 | } | ||
2805 | |||
2806 | /* | ||
2807 | * Perform scheduler related setup for a newly forked process p. | ||
2808 | * p is forked by current. | ||
2809 | * | ||
2810 | * __sched_fork() is basic setup used by init_idle() too: | ||
2811 | */ | ||
2812 | static void __sched_fork(struct task_struct *p) | ||
2813 | { | ||
2814 | p->on_rq = 0; | ||
2815 | |||
2816 | p->se.on_rq = 0; | ||
2817 | p->se.exec_start = 0; | ||
2818 | p->se.sum_exec_runtime = 0; | ||
2819 | p->se.prev_sum_exec_runtime = 0; | ||
2820 | p->se.nr_migrations = 0; | ||
2821 | p->se.vruntime = 0; | ||
2822 | INIT_LIST_HEAD(&p->se.group_node); | ||
2823 | |||
2824 | #ifdef CONFIG_SCHEDSTATS | ||
2825 | memset(&p->se.statistics, 0, sizeof(p->se.statistics)); | ||
2826 | #endif | ||
2827 | |||
2828 | INIT_LIST_HEAD(&p->rt.run_list); | ||
2829 | |||
2830 | #ifdef CONFIG_PREEMPT_NOTIFIERS | ||
2831 | INIT_HLIST_HEAD(&p->preempt_notifiers); | ||
2832 | #endif | ||
2833 | } | ||
2834 | |||
2835 | /* | ||
2836 | * fork()/clone()-time setup: | ||
2837 | */ | ||
2838 | void sched_fork(struct task_struct *p) | ||
2839 | { | ||
2840 | unsigned long flags; | ||
2841 | int cpu = get_cpu(); | ||
2842 | |||
2843 | __sched_fork(p); | ||
2844 | /* | ||
2845 | * We mark the process as running here. This guarantees that | ||
2846 | * nobody will actually run it, and a signal or other external | ||
2847 | * event cannot wake it up and insert it on the runqueue either. | ||
2848 | */ | ||
2849 | p->state = TASK_RUNNING; | ||
2850 | |||
2851 | /* | ||
2852 | * Revert to default priority/policy on fork if requested. | ||
2853 | */ | ||
2854 | if (unlikely(p->sched_reset_on_fork)) { | ||
2855 | if (p->policy == SCHED_FIFO || p->policy == SCHED_RR) { | ||
2856 | p->policy = SCHED_NORMAL; | ||
2857 | p->normal_prio = p->static_prio; | ||
2858 | } | ||
2859 | |||
2860 | if (PRIO_TO_NICE(p->static_prio) < 0) { | ||
2861 | p->static_prio = NICE_TO_PRIO(0); | ||
2862 | p->normal_prio = p->static_prio; | ||
2863 | set_load_weight(p); | ||
2864 | } | ||
2865 | |||
2866 | /* | ||
2867 | * We don't need the reset flag anymore after the fork. It has | ||
2868 | * fulfilled its duty: | ||
2869 | */ | ||
2870 | p->sched_reset_on_fork = 0; | ||
2871 | } | ||
2872 | |||
2873 | /* | ||
2874 | * Make sure we do not leak PI boosting priority to the child. | ||
2875 | */ | ||
2876 | p->prio = current->normal_prio; | ||
2877 | |||
2878 | if (!rt_prio(p->prio)) | ||
2879 | p->sched_class = &fair_sched_class; | ||
2880 | |||
2881 | if (p->sched_class->task_fork) | ||
2882 | p->sched_class->task_fork(p); | ||
2883 | |||
2884 | /* | ||
2885 | * The child is not yet in the pid-hash so no cgroup attach races, | ||
2886 | * and the cgroup is pinned to this child due to cgroup_fork() | ||
2887 | * is ran before sched_fork(). | ||
2888 | * | ||
2889 | * Silence PROVE_RCU. | ||
2890 | */ | ||
2891 | raw_spin_lock_irqsave(&p->pi_lock, flags); | ||
2892 | set_task_cpu(p, cpu); | ||
2893 | raw_spin_unlock_irqrestore(&p->pi_lock, flags); | ||
2894 | |||
2895 | #if defined(CONFIG_SCHEDSTATS) || defined(CONFIG_TASK_DELAY_ACCT) | ||
2896 | if (likely(sched_info_on())) | ||
2897 | memset(&p->sched_info, 0, sizeof(p->sched_info)); | ||
2898 | #endif | ||
2899 | #if defined(CONFIG_SMP) | ||
2900 | p->on_cpu = 0; | ||
2901 | #endif | ||
2902 | #ifdef CONFIG_PREEMPT_COUNT | ||
2903 | /* Want to start with kernel preemption disabled. */ | ||
2904 | task_thread_info(p)->preempt_count = 1; | ||
2905 | #endif | ||
2906 | #ifdef CONFIG_SMP | ||
2907 | plist_node_init(&p->pushable_tasks, MAX_PRIO); | ||
2908 | #endif | ||
2909 | |||
2910 | put_cpu(); | ||
2911 | } | ||
2912 | |||
2913 | /* | ||
2914 | * wake_up_new_task - wake up a newly created task for the first time. | ||
2915 | * | ||
2916 | * This function will do some initial scheduler statistics housekeeping | ||
2917 | * that must be done for every newly created context, then puts the task | ||
2918 | * on the runqueue and wakes it. | ||
2919 | */ | ||
2920 | void wake_up_new_task(struct task_struct *p) | ||
2921 | { | ||
2922 | unsigned long flags; | ||
2923 | struct rq *rq; | ||
2924 | |||
2925 | raw_spin_lock_irqsave(&p->pi_lock, flags); | ||
2926 | #ifdef CONFIG_SMP | ||
2927 | /* | ||
2928 | * Fork balancing, do it here and not earlier because: | ||
2929 | * - cpus_allowed can change in the fork path | ||
2930 | * - any previously selected cpu might disappear through hotplug | ||
2931 | */ | ||
2932 | set_task_cpu(p, select_task_rq(p, SD_BALANCE_FORK, 0)); | ||
2933 | #endif | ||
2934 | |||
2935 | rq = __task_rq_lock(p); | ||
2936 | activate_task(rq, p, 0); | ||
2937 | p->on_rq = 1; | ||
2938 | trace_sched_wakeup_new(p, true); | ||
2939 | check_preempt_curr(rq, p, WF_FORK); | ||
2940 | #ifdef CONFIG_SMP | ||
2941 | if (p->sched_class->task_woken) | ||
2942 | p->sched_class->task_woken(rq, p); | ||
2943 | #endif | ||
2944 | task_rq_unlock(rq, p, &flags); | ||
2945 | } | ||
2946 | |||
2947 | #ifdef CONFIG_PREEMPT_NOTIFIERS | ||
2948 | |||
2949 | /** | ||
2950 | * preempt_notifier_register - tell me when current is being preempted & rescheduled | ||
2951 | * @notifier: notifier struct to register | ||
2952 | */ | ||
2953 | void preempt_notifier_register(struct preempt_notifier *notifier) | ||
2954 | { | ||
2955 | hlist_add_head(¬ifier->link, ¤t->preempt_notifiers); | ||
2956 | } | ||
2957 | EXPORT_SYMBOL_GPL(preempt_notifier_register); | ||
2958 | |||
2959 | /** | ||
2960 | * preempt_notifier_unregister - no longer interested in preemption notifications | ||
2961 | * @notifier: notifier struct to unregister | ||
2962 | * | ||
2963 | * This is safe to call from within a preemption notifier. | ||
2964 | */ | ||
2965 | void preempt_notifier_unregister(struct preempt_notifier *notifier) | ||
2966 | { | ||
2967 | hlist_del(¬ifier->link); | ||
2968 | } | ||
2969 | EXPORT_SYMBOL_GPL(preempt_notifier_unregister); | ||
2970 | |||
2971 | static void fire_sched_in_preempt_notifiers(struct task_struct *curr) | ||
2972 | { | ||
2973 | struct preempt_notifier *notifier; | ||
2974 | struct hlist_node *node; | ||
2975 | |||
2976 | hlist_for_each_entry(notifier, node, &curr->preempt_notifiers, link) | ||
2977 | notifier->ops->sched_in(notifier, raw_smp_processor_id()); | ||
2978 | } | ||
2979 | |||
2980 | static void | ||
2981 | fire_sched_out_preempt_notifiers(struct task_struct *curr, | ||
2982 | struct task_struct *next) | ||
2983 | { | ||
2984 | struct preempt_notifier *notifier; | ||
2985 | struct hlist_node *node; | ||
2986 | |||
2987 | hlist_for_each_entry(notifier, node, &curr->preempt_notifiers, link) | ||
2988 | notifier->ops->sched_out(notifier, next); | ||
2989 | } | ||
2990 | |||
2991 | #else /* !CONFIG_PREEMPT_NOTIFIERS */ | ||
2992 | |||
2993 | static void fire_sched_in_preempt_notifiers(struct task_struct *curr) | ||
2994 | { | ||
2995 | } | ||
2996 | |||
2997 | static void | ||
2998 | fire_sched_out_preempt_notifiers(struct task_struct *curr, | ||
2999 | struct task_struct *next) | ||
3000 | { | ||
3001 | } | ||
3002 | |||
3003 | #endif /* CONFIG_PREEMPT_NOTIFIERS */ | ||
3004 | |||
3005 | /** | ||
3006 | * prepare_task_switch - prepare to switch tasks | ||
3007 | * @rq: the runqueue preparing to switch | ||
3008 | * @prev: the current task that is being switched out | ||
3009 | * @next: the task we are going to switch to. | ||
3010 | * | ||
3011 | * This is called with the rq lock held and interrupts off. It must | ||
3012 | * be paired with a subsequent finish_task_switch after the context | ||
3013 | * switch. | ||
3014 | * | ||
3015 | * prepare_task_switch sets up locking and calls architecture specific | ||
3016 | * hooks. | ||
3017 | */ | ||
3018 | static inline void | ||
3019 | prepare_task_switch(struct rq *rq, struct task_struct *prev, | ||
3020 | struct task_struct *next) | ||
3021 | { | ||
3022 | sched_info_switch(prev, next); | ||
3023 | perf_event_task_sched_out(prev, next); | ||
3024 | fire_sched_out_preempt_notifiers(prev, next); | ||
3025 | prepare_lock_switch(rq, next); | ||
3026 | prepare_arch_switch(next); | ||
3027 | trace_sched_switch(prev, next); | ||
3028 | } | ||
3029 | |||
3030 | /** | ||
3031 | * finish_task_switch - clean up after a task-switch | ||
3032 | * @rq: runqueue associated with task-switch | ||
3033 | * @prev: the thread we just switched away from. | ||
3034 | * | ||
3035 | * finish_task_switch must be called after the context switch, paired | ||
3036 | * with a prepare_task_switch call before the context switch. | ||
3037 | * finish_task_switch will reconcile locking set up by prepare_task_switch, | ||
3038 | * and do any other architecture-specific cleanup actions. | ||
3039 | * | ||
3040 | * Note that we may have delayed dropping an mm in context_switch(). If | ||
3041 | * so, we finish that here outside of the runqueue lock. (Doing it | ||
3042 | * with the lock held can cause deadlocks; see schedule() for | ||
3043 | * details.) | ||
3044 | */ | ||
3045 | static void finish_task_switch(struct rq *rq, struct task_struct *prev) | ||
3046 | __releases(rq->lock) | ||
3047 | { | ||
3048 | struct mm_struct *mm = rq->prev_mm; | ||
3049 | long prev_state; | ||
3050 | |||
3051 | rq->prev_mm = NULL; | ||
3052 | |||
3053 | /* | ||
3054 | * A task struct has one reference for the use as "current". | ||
3055 | * If a task dies, then it sets TASK_DEAD in tsk->state and calls | ||
3056 | * schedule one last time. The schedule call will never return, and | ||
3057 | * the scheduled task must drop that reference. | ||
3058 | * The test for TASK_DEAD must occur while the runqueue locks are | ||
3059 | * still held, otherwise prev could be scheduled on another cpu, die | ||
3060 | * there before we look at prev->state, and then the reference would | ||
3061 | * be dropped twice. | ||
3062 | * Manfred Spraul <manfred@colorfullife.com> | ||
3063 | */ | ||
3064 | prev_state = prev->state; | ||
3065 | finish_arch_switch(prev); | ||
3066 | #ifdef __ARCH_WANT_INTERRUPTS_ON_CTXSW | ||
3067 | local_irq_disable(); | ||
3068 | #endif /* __ARCH_WANT_INTERRUPTS_ON_CTXSW */ | ||
3069 | perf_event_task_sched_in(prev, current); | ||
3070 | #ifdef __ARCH_WANT_INTERRUPTS_ON_CTXSW | ||
3071 | local_irq_enable(); | ||
3072 | #endif /* __ARCH_WANT_INTERRUPTS_ON_CTXSW */ | ||
3073 | finish_lock_switch(rq, prev); | ||
3074 | |||
3075 | fire_sched_in_preempt_notifiers(current); | ||
3076 | if (mm) | ||
3077 | mmdrop(mm); | ||
3078 | if (unlikely(prev_state == TASK_DEAD)) { | ||
3079 | /* | ||
3080 | * Remove function-return probe instances associated with this | ||
3081 | * task and put them back on the free list. | ||
3082 | */ | ||
3083 | kprobe_flush_task(prev); | ||
3084 | put_task_struct(prev); | ||
3085 | } | ||
3086 | } | ||
3087 | |||
3088 | #ifdef CONFIG_SMP | ||
3089 | |||
3090 | /* assumes rq->lock is held */ | ||
3091 | static inline void pre_schedule(struct rq *rq, struct task_struct *prev) | ||
3092 | { | ||
3093 | if (prev->sched_class->pre_schedule) | ||
3094 | prev->sched_class->pre_schedule(rq, prev); | ||
3095 | } | ||
3096 | |||
3097 | /* rq->lock is NOT held, but preemption is disabled */ | ||
3098 | static inline void post_schedule(struct rq *rq) | ||
3099 | { | ||
3100 | if (rq->post_schedule) { | ||
3101 | unsigned long flags; | ||
3102 | |||
3103 | raw_spin_lock_irqsave(&rq->lock, flags); | ||
3104 | if (rq->curr->sched_class->post_schedule) | ||
3105 | rq->curr->sched_class->post_schedule(rq); | ||
3106 | raw_spin_unlock_irqrestore(&rq->lock, flags); | ||
3107 | |||
3108 | rq->post_schedule = 0; | ||
3109 | } | ||
3110 | } | ||
3111 | |||
3112 | #else | ||
3113 | |||
3114 | static inline void pre_schedule(struct rq *rq, struct task_struct *p) | ||
3115 | { | ||
3116 | } | ||
3117 | |||
3118 | static inline void post_schedule(struct rq *rq) | ||
3119 | { | ||
3120 | } | ||
3121 | |||
3122 | #endif | ||
3123 | |||
3124 | /** | ||
3125 | * schedule_tail - first thing a freshly forked thread must call. | ||
3126 | * @prev: the thread we just switched away from. | ||
3127 | */ | ||
3128 | asmlinkage void schedule_tail(struct task_struct *prev) | ||
3129 | __releases(rq->lock) | ||
3130 | { | ||
3131 | struct rq *rq = this_rq(); | ||
3132 | |||
3133 | finish_task_switch(rq, prev); | ||
3134 | |||
3135 | /* | ||
3136 | * FIXME: do we need to worry about rq being invalidated by the | ||
3137 | * task_switch? | ||
3138 | */ | ||
3139 | post_schedule(rq); | ||
3140 | |||
3141 | #ifdef __ARCH_WANT_UNLOCKED_CTXSW | ||
3142 | /* In this case, finish_task_switch does not reenable preemption */ | ||
3143 | preempt_enable(); | ||
3144 | #endif | ||
3145 | if (current->set_child_tid) | ||
3146 | put_user(task_pid_vnr(current), current->set_child_tid); | ||
3147 | } | ||
3148 | |||
3149 | /* | ||
3150 | * context_switch - switch to the new MM and the new | ||
3151 | * thread's register state. | ||
3152 | */ | ||
3153 | static inline void | ||
3154 | context_switch(struct rq *rq, struct task_struct *prev, | ||
3155 | struct task_struct *next) | ||
3156 | { | ||
3157 | struct mm_struct *mm, *oldmm; | ||
3158 | |||
3159 | prepare_task_switch(rq, prev, next); | ||
3160 | |||
3161 | mm = next->mm; | ||
3162 | oldmm = prev->active_mm; | ||
3163 | /* | ||
3164 | * For paravirt, this is coupled with an exit in switch_to to | ||
3165 | * combine the page table reload and the switch backend into | ||
3166 | * one hypercall. | ||
3167 | */ | ||
3168 | arch_start_context_switch(prev); | ||
3169 | |||
3170 | if (!mm) { | ||
3171 | next->active_mm = oldmm; | ||
3172 | atomic_inc(&oldmm->mm_count); | ||
3173 | enter_lazy_tlb(oldmm, next); | ||
3174 | } else | ||
3175 | switch_mm(oldmm, mm, next); | ||
3176 | |||
3177 | if (!prev->mm) { | ||
3178 | prev->active_mm = NULL; | ||
3179 | rq->prev_mm = oldmm; | ||
3180 | } | ||
3181 | /* | ||
3182 | * Since the runqueue lock will be released by the next | ||
3183 | * task (which is an invalid locking op but in the case | ||
3184 | * of the scheduler it's an obvious special-case), so we | ||
3185 | * do an early lockdep release here: | ||
3186 | */ | ||
3187 | #ifndef __ARCH_WANT_UNLOCKED_CTXSW | ||
3188 | spin_release(&rq->lock.dep_map, 1, _THIS_IP_); | ||
3189 | #endif | ||
3190 | |||
3191 | /* Here we just switch the register state and the stack. */ | ||
3192 | switch_to(prev, next, prev); | ||
3193 | |||
3194 | barrier(); | ||
3195 | /* | ||
3196 | * this_rq must be evaluated again because prev may have moved | ||
3197 | * CPUs since it called schedule(), thus the 'rq' on its stack | ||
3198 | * frame will be invalid. | ||
3199 | */ | ||
3200 | finish_task_switch(this_rq(), prev); | ||
3201 | } | ||
3202 | |||
3203 | /* | ||
3204 | * nr_running, nr_uninterruptible and nr_context_switches: | ||
3205 | * | ||
3206 | * externally visible scheduler statistics: current number of runnable | ||
3207 | * threads, current number of uninterruptible-sleeping threads, total | ||
3208 | * number of context switches performed since bootup. | ||
3209 | */ | ||
3210 | unsigned long nr_running(void) | ||
3211 | { | ||
3212 | unsigned long i, sum = 0; | ||
3213 | |||
3214 | for_each_online_cpu(i) | ||
3215 | sum += cpu_rq(i)->nr_running; | ||
3216 | |||
3217 | return sum; | ||
3218 | } | ||
3219 | |||
3220 | unsigned long nr_uninterruptible(void) | ||
3221 | { | ||
3222 | unsigned long i, sum = 0; | ||
3223 | |||
3224 | for_each_possible_cpu(i) | ||
3225 | sum += cpu_rq(i)->nr_uninterruptible; | ||
3226 | |||
3227 | /* | ||
3228 | * Since we read the counters lockless, it might be slightly | ||
3229 | * inaccurate. Do not allow it to go below zero though: | ||
3230 | */ | ||
3231 | if (unlikely((long)sum < 0)) | ||
3232 | sum = 0; | ||
3233 | |||
3234 | return sum; | ||
3235 | } | ||
3236 | |||
3237 | unsigned long long nr_context_switches(void) | ||
3238 | { | ||
3239 | int i; | ||
3240 | unsigned long long sum = 0; | ||
3241 | |||
3242 | for_each_possible_cpu(i) | ||
3243 | sum += cpu_rq(i)->nr_switches; | ||
3244 | |||
3245 | return sum; | ||
3246 | } | ||
3247 | |||
3248 | unsigned long nr_iowait(void) | ||
3249 | { | ||
3250 | unsigned long i, sum = 0; | ||
3251 | |||
3252 | for_each_possible_cpu(i) | ||
3253 | sum += atomic_read(&cpu_rq(i)->nr_iowait); | ||
3254 | |||
3255 | return sum; | ||
3256 | } | ||
3257 | |||
3258 | unsigned long nr_iowait_cpu(int cpu) | ||
3259 | { | ||
3260 | struct rq *this = cpu_rq(cpu); | ||
3261 | return atomic_read(&this->nr_iowait); | ||
3262 | } | ||
3263 | |||
3264 | unsigned long this_cpu_load(void) | ||
3265 | { | ||
3266 | struct rq *this = this_rq(); | ||
3267 | return this->cpu_load[0]; | ||
3268 | } | ||
3269 | |||
3270 | |||
3271 | /* Variables and functions for calc_load */ | ||
3272 | static atomic_long_t calc_load_tasks; | ||
3273 | static unsigned long calc_load_update; | ||
3274 | unsigned long avenrun[3]; | ||
3275 | EXPORT_SYMBOL(avenrun); | ||
3276 | |||
3277 | static long calc_load_fold_active(struct rq *this_rq) | ||
3278 | { | ||
3279 | long nr_active, delta = 0; | ||
3280 | |||
3281 | nr_active = this_rq->nr_running; | ||
3282 | nr_active += (long) this_rq->nr_uninterruptible; | ||
3283 | |||
3284 | if (nr_active != this_rq->calc_load_active) { | ||
3285 | delta = nr_active - this_rq->calc_load_active; | ||
3286 | this_rq->calc_load_active = nr_active; | ||
3287 | } | ||
3288 | |||
3289 | return delta; | ||
3290 | } | ||
3291 | |||
3292 | static unsigned long | ||
3293 | calc_load(unsigned long load, unsigned long exp, unsigned long active) | ||
3294 | { | ||
3295 | load *= exp; | ||
3296 | load += active * (FIXED_1 - exp); | ||
3297 | load += 1UL << (FSHIFT - 1); | ||
3298 | return load >> FSHIFT; | ||
3299 | } | ||
3300 | |||
3301 | #ifdef CONFIG_NO_HZ | ||
3302 | /* | ||
3303 | * For NO_HZ we delay the active fold to the next LOAD_FREQ update. | ||
3304 | * | ||
3305 | * When making the ILB scale, we should try to pull this in as well. | ||
3306 | */ | ||
3307 | static atomic_long_t calc_load_tasks_idle; | ||
3308 | |||
3309 | static void calc_load_account_idle(struct rq *this_rq) | ||
3310 | { | ||
3311 | long delta; | ||
3312 | |||
3313 | delta = calc_load_fold_active(this_rq); | ||
3314 | if (delta) | ||
3315 | atomic_long_add(delta, &calc_load_tasks_idle); | ||
3316 | } | ||
3317 | |||
3318 | static long calc_load_fold_idle(void) | ||
3319 | { | ||
3320 | long delta = 0; | ||
3321 | |||
3322 | /* | ||
3323 | * Its got a race, we don't care... | ||
3324 | */ | ||
3325 | if (atomic_long_read(&calc_load_tasks_idle)) | ||
3326 | delta = atomic_long_xchg(&calc_load_tasks_idle, 0); | ||
3327 | |||
3328 | return delta; | ||
3329 | } | ||
3330 | |||
3331 | /** | ||
3332 | * fixed_power_int - compute: x^n, in O(log n) time | ||
3333 | * | ||
3334 | * @x: base of the power | ||
3335 | * @frac_bits: fractional bits of @x | ||
3336 | * @n: power to raise @x to. | ||
3337 | * | ||
3338 | * By exploiting the relation between the definition of the natural power | ||
3339 | * function: x^n := x*x*...*x (x multiplied by itself for n times), and | ||
3340 | * the binary encoding of numbers used by computers: n := \Sum n_i * 2^i, | ||
3341 | * (where: n_i \elem {0, 1}, the binary vector representing n), | ||
3342 | * we find: x^n := x^(\Sum n_i * 2^i) := \Prod x^(n_i * 2^i), which is | ||
3343 | * of course trivially computable in O(log_2 n), the length of our binary | ||
3344 | * vector. | ||
3345 | */ | ||
3346 | static unsigned long | ||
3347 | fixed_power_int(unsigned long x, unsigned int frac_bits, unsigned int n) | ||
3348 | { | ||
3349 | unsigned long result = 1UL << frac_bits; | ||
3350 | |||
3351 | if (n) for (;;) { | ||
3352 | if (n & 1) { | ||
3353 | result *= x; | ||
3354 | result += 1UL << (frac_bits - 1); | ||
3355 | result >>= frac_bits; | ||
3356 | } | ||
3357 | n >>= 1; | ||
3358 | if (!n) | ||
3359 | break; | ||
3360 | x *= x; | ||
3361 | x += 1UL << (frac_bits - 1); | ||
3362 | x >>= frac_bits; | ||
3363 | } | ||
3364 | |||
3365 | return result; | ||
3366 | } | ||
3367 | |||
3368 | /* | ||
3369 | * a1 = a0 * e + a * (1 - e) | ||
3370 | * | ||
3371 | * a2 = a1 * e + a * (1 - e) | ||
3372 | * = (a0 * e + a * (1 - e)) * e + a * (1 - e) | ||
3373 | * = a0 * e^2 + a * (1 - e) * (1 + e) | ||
3374 | * | ||
3375 | * a3 = a2 * e + a * (1 - e) | ||
3376 | * = (a0 * e^2 + a * (1 - e) * (1 + e)) * e + a * (1 - e) | ||
3377 | * = a0 * e^3 + a * (1 - e) * (1 + e + e^2) | ||
3378 | * | ||
3379 | * ... | ||
3380 | * | ||
3381 | * an = a0 * e^n + a * (1 - e) * (1 + e + ... + e^n-1) [1] | ||
3382 | * = a0 * e^n + a * (1 - e) * (1 - e^n)/(1 - e) | ||
3383 | * = a0 * e^n + a * (1 - e^n) | ||
3384 | * | ||
3385 | * [1] application of the geometric series: | ||
3386 | * | ||
3387 | * n 1 - x^(n+1) | ||
3388 | * S_n := \Sum x^i = ------------- | ||
3389 | * i=0 1 - x | ||
3390 | */ | ||
3391 | static unsigned long | ||
3392 | calc_load_n(unsigned long load, unsigned long exp, | ||
3393 | unsigned long active, unsigned int n) | ||
3394 | { | ||
3395 | |||
3396 | return calc_load(load, fixed_power_int(exp, FSHIFT, n), active); | ||
3397 | } | ||
3398 | |||
3399 | /* | ||
3400 | * NO_HZ can leave us missing all per-cpu ticks calling | ||
3401 | * calc_load_account_active(), but since an idle CPU folds its delta into | ||
3402 | * calc_load_tasks_idle per calc_load_account_idle(), all we need to do is fold | ||
3403 | * in the pending idle delta if our idle period crossed a load cycle boundary. | ||
3404 | * | ||
3405 | * Once we've updated the global active value, we need to apply the exponential | ||
3406 | * weights adjusted to the number of cycles missed. | ||
3407 | */ | ||
3408 | static void calc_global_nohz(unsigned long ticks) | ||
3409 | { | ||
3410 | long delta, active, n; | ||
3411 | |||
3412 | if (time_before(jiffies, calc_load_update)) | ||
3413 | return; | ||
3414 | |||
3415 | /* | ||
3416 | * If we crossed a calc_load_update boundary, make sure to fold | ||
3417 | * any pending idle changes, the respective CPUs might have | ||
3418 | * missed the tick driven calc_load_account_active() update | ||
3419 | * due to NO_HZ. | ||
3420 | */ | ||
3421 | delta = calc_load_fold_idle(); | ||
3422 | if (delta) | ||
3423 | atomic_long_add(delta, &calc_load_tasks); | ||
3424 | |||
3425 | /* | ||
3426 | * If we were idle for multiple load cycles, apply them. | ||
3427 | */ | ||
3428 | if (ticks >= LOAD_FREQ) { | ||
3429 | n = ticks / LOAD_FREQ; | ||
3430 | |||
3431 | active = atomic_long_read(&calc_load_tasks); | ||
3432 | active = active > 0 ? active * FIXED_1 : 0; | ||
3433 | |||
3434 | avenrun[0] = calc_load_n(avenrun[0], EXP_1, active, n); | ||
3435 | avenrun[1] = calc_load_n(avenrun[1], EXP_5, active, n); | ||
3436 | avenrun[2] = calc_load_n(avenrun[2], EXP_15, active, n); | ||
3437 | |||
3438 | calc_load_update += n * LOAD_FREQ; | ||
3439 | } | ||
3440 | |||
3441 | /* | ||
3442 | * Its possible the remainder of the above division also crosses | ||
3443 | * a LOAD_FREQ period, the regular check in calc_global_load() | ||
3444 | * which comes after this will take care of that. | ||
3445 | * | ||
3446 | * Consider us being 11 ticks before a cycle completion, and us | ||
3447 | * sleeping for 4*LOAD_FREQ + 22 ticks, then the above code will | ||
3448 | * age us 4 cycles, and the test in calc_global_load() will | ||
3449 | * pick up the final one. | ||
3450 | */ | ||
3451 | } | ||
3452 | #else | ||
3453 | static void calc_load_account_idle(struct rq *this_rq) | ||
3454 | { | ||
3455 | } | ||
3456 | |||
3457 | static inline long calc_load_fold_idle(void) | ||
3458 | { | ||
3459 | return 0; | ||
3460 | } | ||
3461 | |||
3462 | static void calc_global_nohz(unsigned long ticks) | ||
3463 | { | ||
3464 | } | ||
3465 | #endif | ||
3466 | |||
3467 | /** | ||
3468 | * get_avenrun - get the load average array | ||
3469 | * @loads: pointer to dest load array | ||
3470 | * @offset: offset to add | ||
3471 | * @shift: shift count to shift the result left | ||
3472 | * | ||
3473 | * These values are estimates at best, so no need for locking. | ||
3474 | */ | ||
3475 | void get_avenrun(unsigned long *loads, unsigned long offset, int shift) | ||
3476 | { | ||
3477 | loads[0] = (avenrun[0] + offset) << shift; | ||
3478 | loads[1] = (avenrun[1] + offset) << shift; | ||
3479 | loads[2] = (avenrun[2] + offset) << shift; | ||
3480 | } | ||
3481 | |||
3482 | /* | ||
3483 | * calc_load - update the avenrun load estimates 10 ticks after the | ||
3484 | * CPUs have updated calc_load_tasks. | ||
3485 | */ | ||
3486 | void calc_global_load(unsigned long ticks) | ||
3487 | { | ||
3488 | long active; | ||
3489 | |||
3490 | calc_global_nohz(ticks); | ||
3491 | |||
3492 | if (time_before(jiffies, calc_load_update + 10)) | ||
3493 | return; | ||
3494 | |||
3495 | active = atomic_long_read(&calc_load_tasks); | ||
3496 | active = active > 0 ? active * FIXED_1 : 0; | ||
3497 | |||
3498 | avenrun[0] = calc_load(avenrun[0], EXP_1, active); | ||
3499 | avenrun[1] = calc_load(avenrun[1], EXP_5, active); | ||
3500 | avenrun[2] = calc_load(avenrun[2], EXP_15, active); | ||
3501 | |||
3502 | calc_load_update += LOAD_FREQ; | ||
3503 | } | ||
3504 | |||
3505 | /* | ||
3506 | * Called from update_cpu_load() to periodically update this CPU's | ||
3507 | * active count. | ||
3508 | */ | ||
3509 | static void calc_load_account_active(struct rq *this_rq) | ||
3510 | { | ||
3511 | long delta; | ||
3512 | |||
3513 | if (time_before(jiffies, this_rq->calc_load_update)) | ||
3514 | return; | ||
3515 | |||
3516 | delta = calc_load_fold_active(this_rq); | ||
3517 | delta += calc_load_fold_idle(); | ||
3518 | if (delta) | ||
3519 | atomic_long_add(delta, &calc_load_tasks); | ||
3520 | |||
3521 | this_rq->calc_load_update += LOAD_FREQ; | ||
3522 | } | ||
3523 | |||
3524 | /* | ||
3525 | * The exact cpuload at various idx values, calculated at every tick would be | ||
3526 | * load = (2^idx - 1) / 2^idx * load + 1 / 2^idx * cur_load | ||
3527 | * | ||
3528 | * If a cpu misses updates for n-1 ticks (as it was idle) and update gets called | ||
3529 | * on nth tick when cpu may be busy, then we have: | ||
3530 | * load = ((2^idx - 1) / 2^idx)^(n-1) * load | ||
3531 | * load = (2^idx - 1) / 2^idx) * load + 1 / 2^idx * cur_load | ||
3532 | * | ||
3533 | * decay_load_missed() below does efficient calculation of | ||
3534 | * load = ((2^idx - 1) / 2^idx)^(n-1) * load | ||
3535 | * avoiding 0..n-1 loop doing load = ((2^idx - 1) / 2^idx) * load | ||
3536 | * | ||
3537 | * The calculation is approximated on a 128 point scale. | ||
3538 | * degrade_zero_ticks is the number of ticks after which load at any | ||
3539 | * particular idx is approximated to be zero. | ||
3540 | * degrade_factor is a precomputed table, a row for each load idx. | ||
3541 | * Each column corresponds to degradation factor for a power of two ticks, | ||
3542 | * based on 128 point scale. | ||
3543 | * Example: | ||
3544 | * row 2, col 3 (=12) says that the degradation at load idx 2 after | ||
3545 | * 8 ticks is 12/128 (which is an approximation of exact factor 3^8/4^8). | ||
3546 | * | ||
3547 | * With this power of 2 load factors, we can degrade the load n times | ||
3548 | * by looking at 1 bits in n and doing as many mult/shift instead of | ||
3549 | * n mult/shifts needed by the exact degradation. | ||
3550 | */ | ||
3551 | #define DEGRADE_SHIFT 7 | ||
3552 | static const unsigned char | ||
3553 | degrade_zero_ticks[CPU_LOAD_IDX_MAX] = {0, 8, 32, 64, 128}; | ||
3554 | static const unsigned char | ||
3555 | degrade_factor[CPU_LOAD_IDX_MAX][DEGRADE_SHIFT + 1] = { | ||
3556 | {0, 0, 0, 0, 0, 0, 0, 0}, | ||
3557 | {64, 32, 8, 0, 0, 0, 0, 0}, | ||
3558 | {96, 72, 40, 12, 1, 0, 0}, | ||
3559 | {112, 98, 75, 43, 15, 1, 0}, | ||
3560 | {120, 112, 98, 76, 45, 16, 2} }; | ||
3561 | |||
3562 | /* | ||
3563 | * Update cpu_load for any missed ticks, due to tickless idle. The backlog | ||
3564 | * would be when CPU is idle and so we just decay the old load without | ||
3565 | * adding any new load. | ||
3566 | */ | ||
3567 | static unsigned long | ||
3568 | decay_load_missed(unsigned long load, unsigned long missed_updates, int idx) | ||
3569 | { | ||
3570 | int j = 0; | ||
3571 | |||
3572 | if (!missed_updates) | ||
3573 | return load; | ||
3574 | |||
3575 | if (missed_updates >= degrade_zero_ticks[idx]) | ||
3576 | return 0; | ||
3577 | |||
3578 | if (idx == 1) | ||
3579 | return load >> missed_updates; | ||
3580 | |||
3581 | while (missed_updates) { | ||
3582 | if (missed_updates % 2) | ||
3583 | load = (load * degrade_factor[idx][j]) >> DEGRADE_SHIFT; | ||
3584 | |||
3585 | missed_updates >>= 1; | ||
3586 | j++; | ||
3587 | } | ||
3588 | return load; | ||
3589 | } | ||
3590 | |||
3591 | /* | ||
3592 | * Update rq->cpu_load[] statistics. This function is usually called every | ||
3593 | * scheduler tick (TICK_NSEC). With tickless idle this will not be called | ||
3594 | * every tick. We fix it up based on jiffies. | ||
3595 | */ | ||
3596 | static void update_cpu_load(struct rq *this_rq) | ||
3597 | { | ||
3598 | unsigned long this_load = this_rq->load.weight; | ||
3599 | unsigned long curr_jiffies = jiffies; | ||
3600 | unsigned long pending_updates; | ||
3601 | int i, scale; | ||
3602 | |||
3603 | this_rq->nr_load_updates++; | ||
3604 | |||
3605 | /* Avoid repeated calls on same jiffy, when moving in and out of idle */ | ||
3606 | if (curr_jiffies == this_rq->last_load_update_tick) | ||
3607 | return; | ||
3608 | |||
3609 | pending_updates = curr_jiffies - this_rq->last_load_update_tick; | ||
3610 | this_rq->last_load_update_tick = curr_jiffies; | ||
3611 | |||
3612 | /* Update our load: */ | ||
3613 | this_rq->cpu_load[0] = this_load; /* Fasttrack for idx 0 */ | ||
3614 | for (i = 1, scale = 2; i < CPU_LOAD_IDX_MAX; i++, scale += scale) { | ||
3615 | unsigned long old_load, new_load; | ||
3616 | |||
3617 | /* scale is effectively 1 << i now, and >> i divides by scale */ | ||
3618 | |||
3619 | old_load = this_rq->cpu_load[i]; | ||
3620 | old_load = decay_load_missed(old_load, pending_updates - 1, i); | ||
3621 | new_load = this_load; | ||
3622 | /* | ||
3623 | * Round up the averaging division if load is increasing. This | ||
3624 | * prevents us from getting stuck on 9 if the load is 10, for | ||
3625 | * example. | ||
3626 | */ | ||
3627 | if (new_load > old_load) | ||
3628 | new_load += scale - 1; | ||
3629 | |||
3630 | this_rq->cpu_load[i] = (old_load * (scale - 1) + new_load) >> i; | ||
3631 | } | ||
3632 | |||
3633 | sched_avg_update(this_rq); | ||
3634 | } | ||
3635 | |||
3636 | static void update_cpu_load_active(struct rq *this_rq) | ||
3637 | { | ||
3638 | update_cpu_load(this_rq); | ||
3639 | |||
3640 | calc_load_account_active(this_rq); | ||
3641 | } | ||
3642 | |||
3643 | #ifdef CONFIG_SMP | ||
3644 | |||
3645 | /* | ||
3646 | * sched_exec - execve() is a valuable balancing opportunity, because at | ||
3647 | * this point the task has the smallest effective memory and cache footprint. | ||
3648 | */ | ||
3649 | void sched_exec(void) | ||
3650 | { | ||
3651 | struct task_struct *p = current; | ||
3652 | unsigned long flags; | ||
3653 | int dest_cpu; | ||
3654 | |||
3655 | raw_spin_lock_irqsave(&p->pi_lock, flags); | ||
3656 | dest_cpu = p->sched_class->select_task_rq(p, SD_BALANCE_EXEC, 0); | ||
3657 | if (dest_cpu == smp_processor_id()) | ||
3658 | goto unlock; | ||
3659 | |||
3660 | if (likely(cpu_active(dest_cpu))) { | ||
3661 | struct migration_arg arg = { p, dest_cpu }; | ||
3662 | |||
3663 | raw_spin_unlock_irqrestore(&p->pi_lock, flags); | ||
3664 | stop_one_cpu(task_cpu(p), migration_cpu_stop, &arg); | ||
3665 | return; | ||
3666 | } | ||
3667 | unlock: | ||
3668 | raw_spin_unlock_irqrestore(&p->pi_lock, flags); | ||
3669 | } | ||
3670 | |||
3671 | #endif | ||
3672 | |||
3673 | DEFINE_PER_CPU(struct kernel_stat, kstat); | ||
3674 | |||
3675 | EXPORT_PER_CPU_SYMBOL(kstat); | ||
3676 | |||
3677 | /* | ||
3678 | * Return any ns on the sched_clock that have not yet been accounted in | ||
3679 | * @p in case that task is currently running. | ||
3680 | * | ||
3681 | * Called with task_rq_lock() held on @rq. | ||
3682 | */ | ||
3683 | static u64 do_task_delta_exec(struct task_struct *p, struct rq *rq) | ||
3684 | { | ||
3685 | u64 ns = 0; | ||
3686 | |||
3687 | if (task_current(rq, p)) { | ||
3688 | update_rq_clock(rq); | ||
3689 | ns = rq->clock_task - p->se.exec_start; | ||
3690 | if ((s64)ns < 0) | ||
3691 | ns = 0; | ||
3692 | } | ||
3693 | |||
3694 | return ns; | ||
3695 | } | ||
3696 | |||
3697 | unsigned long long task_delta_exec(struct task_struct *p) | ||
3698 | { | ||
3699 | unsigned long flags; | ||
3700 | struct rq *rq; | ||
3701 | u64 ns = 0; | ||
3702 | |||
3703 | rq = task_rq_lock(p, &flags); | ||
3704 | ns = do_task_delta_exec(p, rq); | ||
3705 | task_rq_unlock(rq, p, &flags); | ||
3706 | |||
3707 | return ns; | ||
3708 | } | ||
3709 | |||
3710 | /* | ||
3711 | * Return accounted runtime for the task. | ||
3712 | * In case the task is currently running, return the runtime plus current's | ||
3713 | * pending runtime that have not been accounted yet. | ||
3714 | */ | ||
3715 | unsigned long long task_sched_runtime(struct task_struct *p) | ||
3716 | { | ||
3717 | unsigned long flags; | ||
3718 | struct rq *rq; | ||
3719 | u64 ns = 0; | ||
3720 | |||
3721 | rq = task_rq_lock(p, &flags); | ||
3722 | ns = p->se.sum_exec_runtime + do_task_delta_exec(p, rq); | ||
3723 | task_rq_unlock(rq, p, &flags); | ||
3724 | |||
3725 | return ns; | ||
3726 | } | ||
3727 | |||
3728 | /* | ||
3729 | * Account user cpu time to a process. | ||
3730 | * @p: the process that the cpu time gets accounted to | ||
3731 | * @cputime: the cpu time spent in user space since the last update | ||
3732 | * @cputime_scaled: cputime scaled by cpu frequency | ||
3733 | */ | ||
3734 | void account_user_time(struct task_struct *p, cputime_t cputime, | ||
3735 | cputime_t cputime_scaled) | ||
3736 | { | ||
3737 | struct cpu_usage_stat *cpustat = &kstat_this_cpu.cpustat; | ||
3738 | cputime64_t tmp; | ||
3739 | |||
3740 | /* Add user time to process. */ | ||
3741 | p->utime = cputime_add(p->utime, cputime); | ||
3742 | p->utimescaled = cputime_add(p->utimescaled, cputime_scaled); | ||
3743 | account_group_user_time(p, cputime); | ||
3744 | |||
3745 | /* Add user time to cpustat. */ | ||
3746 | tmp = cputime_to_cputime64(cputime); | ||
3747 | if (TASK_NICE(p) > 0) | ||
3748 | cpustat->nice = cputime64_add(cpustat->nice, tmp); | ||
3749 | else | ||
3750 | cpustat->user = cputime64_add(cpustat->user, tmp); | ||
3751 | |||
3752 | cpuacct_update_stats(p, CPUACCT_STAT_USER, cputime); | ||
3753 | /* Account for user time used */ | ||
3754 | acct_update_integrals(p); | ||
3755 | } | ||
3756 | |||
3757 | /* | ||
3758 | * Account guest cpu time to a process. | ||
3759 | * @p: the process that the cpu time gets accounted to | ||
3760 | * @cputime: the cpu time spent in virtual machine since the last update | ||
3761 | * @cputime_scaled: cputime scaled by cpu frequency | ||
3762 | */ | ||
3763 | static void account_guest_time(struct task_struct *p, cputime_t cputime, | ||
3764 | cputime_t cputime_scaled) | ||
3765 | { | ||
3766 | cputime64_t tmp; | ||
3767 | struct cpu_usage_stat *cpustat = &kstat_this_cpu.cpustat; | ||
3768 | |||
3769 | tmp = cputime_to_cputime64(cputime); | ||
3770 | |||
3771 | /* Add guest time to process. */ | ||
3772 | p->utime = cputime_add(p->utime, cputime); | ||
3773 | p->utimescaled = cputime_add(p->utimescaled, cputime_scaled); | ||
3774 | account_group_user_time(p, cputime); | ||
3775 | p->gtime = cputime_add(p->gtime, cputime); | ||
3776 | |||
3777 | /* Add guest time to cpustat. */ | ||
3778 | if (TASK_NICE(p) > 0) { | ||
3779 | cpustat->nice = cputime64_add(cpustat->nice, tmp); | ||
3780 | cpustat->guest_nice = cputime64_add(cpustat->guest_nice, tmp); | ||
3781 | } else { | ||
3782 | cpustat->user = cputime64_add(cpustat->user, tmp); | ||
3783 | cpustat->guest = cputime64_add(cpustat->guest, tmp); | ||
3784 | } | ||
3785 | } | ||
3786 | |||
3787 | /* | ||
3788 | * Account system cpu time to a process and desired cpustat field | ||
3789 | * @p: the process that the cpu time gets accounted to | ||
3790 | * @cputime: the cpu time spent in kernel space since the last update | ||
3791 | * @cputime_scaled: cputime scaled by cpu frequency | ||
3792 | * @target_cputime64: pointer to cpustat field that has to be updated | ||
3793 | */ | ||
3794 | static inline | ||
3795 | void __account_system_time(struct task_struct *p, cputime_t cputime, | ||
3796 | cputime_t cputime_scaled, cputime64_t *target_cputime64) | ||
3797 | { | ||
3798 | cputime64_t tmp = cputime_to_cputime64(cputime); | ||
3799 | |||
3800 | /* Add system time to process. */ | ||
3801 | p->stime = cputime_add(p->stime, cputime); | ||
3802 | p->stimescaled = cputime_add(p->stimescaled, cputime_scaled); | ||
3803 | account_group_system_time(p, cputime); | ||
3804 | |||
3805 | /* Add system time to cpustat. */ | ||
3806 | *target_cputime64 = cputime64_add(*target_cputime64, tmp); | ||
3807 | cpuacct_update_stats(p, CPUACCT_STAT_SYSTEM, cputime); | ||
3808 | |||
3809 | /* Account for system time used */ | ||
3810 | acct_update_integrals(p); | ||
3811 | } | ||
3812 | |||
3813 | /* | ||
3814 | * Account system cpu time to a process. | ||
3815 | * @p: the process that the cpu time gets accounted to | ||
3816 | * @hardirq_offset: the offset to subtract from hardirq_count() | ||
3817 | * @cputime: the cpu time spent in kernel space since the last update | ||
3818 | * @cputime_scaled: cputime scaled by cpu frequency | ||
3819 | */ | ||
3820 | void account_system_time(struct task_struct *p, int hardirq_offset, | ||
3821 | cputime_t cputime, cputime_t cputime_scaled) | ||
3822 | { | ||
3823 | struct cpu_usage_stat *cpustat = &kstat_this_cpu.cpustat; | ||
3824 | cputime64_t *target_cputime64; | ||
3825 | |||
3826 | if ((p->flags & PF_VCPU) && (irq_count() - hardirq_offset == 0)) { | ||
3827 | account_guest_time(p, cputime, cputime_scaled); | ||
3828 | return; | ||
3829 | } | ||
3830 | |||
3831 | if (hardirq_count() - hardirq_offset) | ||
3832 | target_cputime64 = &cpustat->irq; | ||
3833 | else if (in_serving_softirq()) | ||
3834 | target_cputime64 = &cpustat->softirq; | ||
3835 | else | ||
3836 | target_cputime64 = &cpustat->system; | ||
3837 | |||
3838 | __account_system_time(p, cputime, cputime_scaled, target_cputime64); | ||
3839 | } | ||
3840 | |||
3841 | /* | ||
3842 | * Account for involuntary wait time. | ||
3843 | * @cputime: the cpu time spent in involuntary wait | ||
3844 | */ | ||
3845 | void account_steal_time(cputime_t cputime) | ||
3846 | { | ||
3847 | struct cpu_usage_stat *cpustat = &kstat_this_cpu.cpustat; | ||
3848 | cputime64_t cputime64 = cputime_to_cputime64(cputime); | ||
3849 | |||
3850 | cpustat->steal = cputime64_add(cpustat->steal, cputime64); | ||
3851 | } | ||
3852 | |||
3853 | /* | ||
3854 | * Account for idle time. | ||
3855 | * @cputime: the cpu time spent in idle wait | ||
3856 | */ | ||
3857 | void account_idle_time(cputime_t cputime) | ||
3858 | { | ||
3859 | struct cpu_usage_stat *cpustat = &kstat_this_cpu.cpustat; | ||
3860 | cputime64_t cputime64 = cputime_to_cputime64(cputime); | ||
3861 | struct rq *rq = this_rq(); | ||
3862 | |||
3863 | if (atomic_read(&rq->nr_iowait) > 0) | ||
3864 | cpustat->iowait = cputime64_add(cpustat->iowait, cputime64); | ||
3865 | else | ||
3866 | cpustat->idle = cputime64_add(cpustat->idle, cputime64); | ||
3867 | } | ||
3868 | |||
3869 | static __always_inline bool steal_account_process_tick(void) | ||
3870 | { | ||
3871 | #ifdef CONFIG_PARAVIRT | ||
3872 | if (static_branch(¶virt_steal_enabled)) { | ||
3873 | u64 steal, st = 0; | ||
3874 | |||
3875 | steal = paravirt_steal_clock(smp_processor_id()); | ||
3876 | steal -= this_rq()->prev_steal_time; | ||
3877 | |||
3878 | st = steal_ticks(steal); | ||
3879 | this_rq()->prev_steal_time += st * TICK_NSEC; | ||
3880 | |||
3881 | account_steal_time(st); | ||
3882 | return st; | ||
3883 | } | ||
3884 | #endif | ||
3885 | return false; | ||
3886 | } | ||
3887 | |||
3888 | #ifndef CONFIG_VIRT_CPU_ACCOUNTING | ||
3889 | |||
3890 | #ifdef CONFIG_IRQ_TIME_ACCOUNTING | ||
3891 | /* | ||
3892 | * Account a tick to a process and cpustat | ||
3893 | * @p: the process that the cpu time gets accounted to | ||
3894 | * @user_tick: is the tick from userspace | ||
3895 | * @rq: the pointer to rq | ||
3896 | * | ||
3897 | * Tick demultiplexing follows the order | ||
3898 | * - pending hardirq update | ||
3899 | * - pending softirq update | ||
3900 | * - user_time | ||
3901 | * - idle_time | ||
3902 | * - system time | ||
3903 | * - check for guest_time | ||
3904 | * - else account as system_time | ||
3905 | * | ||
3906 | * Check for hardirq is done both for system and user time as there is | ||
3907 | * no timer going off while we are on hardirq and hence we may never get an | ||
3908 | * opportunity to update it solely in system time. | ||
3909 | * p->stime and friends are only updated on system time and not on irq | ||
3910 | * softirq as those do not count in task exec_runtime any more. | ||
3911 | */ | ||
3912 | static void irqtime_account_process_tick(struct task_struct *p, int user_tick, | ||
3913 | struct rq *rq) | ||
3914 | { | ||
3915 | cputime_t one_jiffy_scaled = cputime_to_scaled(cputime_one_jiffy); | ||
3916 | cputime64_t tmp = cputime_to_cputime64(cputime_one_jiffy); | ||
3917 | struct cpu_usage_stat *cpustat = &kstat_this_cpu.cpustat; | ||
3918 | |||
3919 | if (steal_account_process_tick()) | ||
3920 | return; | ||
3921 | |||
3922 | if (irqtime_account_hi_update()) { | ||
3923 | cpustat->irq = cputime64_add(cpustat->irq, tmp); | ||
3924 | } else if (irqtime_account_si_update()) { | ||
3925 | cpustat->softirq = cputime64_add(cpustat->softirq, tmp); | ||
3926 | } else if (this_cpu_ksoftirqd() == p) { | ||
3927 | /* | ||
3928 | * ksoftirqd time do not get accounted in cpu_softirq_time. | ||
3929 | * So, we have to handle it separately here. | ||
3930 | * Also, p->stime needs to be updated for ksoftirqd. | ||
3931 | */ | ||
3932 | __account_system_time(p, cputime_one_jiffy, one_jiffy_scaled, | ||
3933 | &cpustat->softirq); | ||
3934 | } else if (user_tick) { | ||
3935 | account_user_time(p, cputime_one_jiffy, one_jiffy_scaled); | ||
3936 | } else if (p == rq->idle) { | ||
3937 | account_idle_time(cputime_one_jiffy); | ||
3938 | } else if (p->flags & PF_VCPU) { /* System time or guest time */ | ||
3939 | account_guest_time(p, cputime_one_jiffy, one_jiffy_scaled); | ||
3940 | } else { | ||
3941 | __account_system_time(p, cputime_one_jiffy, one_jiffy_scaled, | ||
3942 | &cpustat->system); | ||
3943 | } | ||
3944 | } | ||
3945 | |||
3946 | static void irqtime_account_idle_ticks(int ticks) | ||
3947 | { | ||
3948 | int i; | ||
3949 | struct rq *rq = this_rq(); | ||
3950 | |||
3951 | for (i = 0; i < ticks; i++) | ||
3952 | irqtime_account_process_tick(current, 0, rq); | ||
3953 | } | ||
3954 | #else /* CONFIG_IRQ_TIME_ACCOUNTING */ | ||
3955 | static void irqtime_account_idle_ticks(int ticks) {} | ||
3956 | static void irqtime_account_process_tick(struct task_struct *p, int user_tick, | ||
3957 | struct rq *rq) {} | ||
3958 | #endif /* CONFIG_IRQ_TIME_ACCOUNTING */ | ||
3959 | |||
3960 | /* | ||
3961 | * Account a single tick of cpu time. | ||
3962 | * @p: the process that the cpu time gets accounted to | ||
3963 | * @user_tick: indicates if the tick is a user or a system tick | ||
3964 | */ | ||
3965 | void account_process_tick(struct task_struct *p, int user_tick) | ||
3966 | { | ||
3967 | cputime_t one_jiffy_scaled = cputime_to_scaled(cputime_one_jiffy); | ||
3968 | struct rq *rq = this_rq(); | ||
3969 | |||
3970 | if (sched_clock_irqtime) { | ||
3971 | irqtime_account_process_tick(p, user_tick, rq); | ||
3972 | return; | ||
3973 | } | ||
3974 | |||
3975 | if (steal_account_process_tick()) | ||
3976 | return; | ||
3977 | |||
3978 | if (user_tick) | ||
3979 | account_user_time(p, cputime_one_jiffy, one_jiffy_scaled); | ||
3980 | else if ((p != rq->idle) || (irq_count() != HARDIRQ_OFFSET)) | ||
3981 | account_system_time(p, HARDIRQ_OFFSET, cputime_one_jiffy, | ||
3982 | one_jiffy_scaled); | ||
3983 | else | ||
3984 | account_idle_time(cputime_one_jiffy); | ||
3985 | } | ||
3986 | |||
3987 | /* | ||
3988 | * Account multiple ticks of steal time. | ||
3989 | * @p: the process from which the cpu time has been stolen | ||
3990 | * @ticks: number of stolen ticks | ||
3991 | */ | ||
3992 | void account_steal_ticks(unsigned long ticks) | ||
3993 | { | ||
3994 | account_steal_time(jiffies_to_cputime(ticks)); | ||
3995 | } | ||
3996 | |||
3997 | /* | ||
3998 | * Account multiple ticks of idle time. | ||
3999 | * @ticks: number of stolen ticks | ||
4000 | */ | ||
4001 | void account_idle_ticks(unsigned long ticks) | ||
4002 | { | ||
4003 | |||
4004 | if (sched_clock_irqtime) { | ||
4005 | irqtime_account_idle_ticks(ticks); | ||
4006 | return; | ||
4007 | } | ||
4008 | |||
4009 | account_idle_time(jiffies_to_cputime(ticks)); | ||
4010 | } | ||
4011 | |||
4012 | #endif | ||
4013 | |||
4014 | /* | ||
4015 | * Use precise platform statistics if available: | ||
4016 | */ | ||
4017 | #ifdef CONFIG_VIRT_CPU_ACCOUNTING | ||
4018 | void task_times(struct task_struct *p, cputime_t *ut, cputime_t *st) | ||
4019 | { | ||
4020 | *ut = p->utime; | ||
4021 | *st = p->stime; | ||
4022 | } | ||
4023 | |||
4024 | void thread_group_times(struct task_struct *p, cputime_t *ut, cputime_t *st) | ||
4025 | { | ||
4026 | struct task_cputime cputime; | ||
4027 | |||
4028 | thread_group_cputime(p, &cputime); | ||
4029 | |||
4030 | *ut = cputime.utime; | ||
4031 | *st = cputime.stime; | ||
4032 | } | ||
4033 | #else | ||
4034 | |||
4035 | #ifndef nsecs_to_cputime | ||
4036 | # define nsecs_to_cputime(__nsecs) nsecs_to_jiffies(__nsecs) | ||
4037 | #endif | ||
4038 | |||
4039 | void task_times(struct task_struct *p, cputime_t *ut, cputime_t *st) | ||
4040 | { | ||
4041 | cputime_t rtime, utime = p->utime, total = cputime_add(utime, p->stime); | ||
4042 | |||
4043 | /* | ||
4044 | * Use CFS's precise accounting: | ||
4045 | */ | ||
4046 | rtime = nsecs_to_cputime(p->se.sum_exec_runtime); | ||
4047 | |||
4048 | if (total) { | ||
4049 | u64 temp = rtime; | ||
4050 | |||
4051 | temp *= utime; | ||
4052 | do_div(temp, total); | ||
4053 | utime = (cputime_t)temp; | ||
4054 | } else | ||
4055 | utime = rtime; | ||
4056 | |||
4057 | /* | ||
4058 | * Compare with previous values, to keep monotonicity: | ||
4059 | */ | ||
4060 | p->prev_utime = max(p->prev_utime, utime); | ||
4061 | p->prev_stime = max(p->prev_stime, cputime_sub(rtime, p->prev_utime)); | ||
4062 | |||
4063 | *ut = p->prev_utime; | ||
4064 | *st = p->prev_stime; | ||
4065 | } | ||
4066 | |||
4067 | /* | ||
4068 | * Must be called with siglock held. | ||
4069 | */ | ||
4070 | void thread_group_times(struct task_struct *p, cputime_t *ut, cputime_t *st) | ||
4071 | { | ||
4072 | struct signal_struct *sig = p->signal; | ||
4073 | struct task_cputime cputime; | ||
4074 | cputime_t rtime, utime, total; | ||
4075 | |||
4076 | thread_group_cputime(p, &cputime); | ||
4077 | |||
4078 | total = cputime_add(cputime.utime, cputime.stime); | ||
4079 | rtime = nsecs_to_cputime(cputime.sum_exec_runtime); | ||
4080 | |||
4081 | if (total) { | ||
4082 | u64 temp = rtime; | ||
4083 | |||
4084 | temp *= cputime.utime; | ||
4085 | do_div(temp, total); | ||
4086 | utime = (cputime_t)temp; | ||
4087 | } else | ||
4088 | utime = rtime; | ||
4089 | |||
4090 | sig->prev_utime = max(sig->prev_utime, utime); | ||
4091 | sig->prev_stime = max(sig->prev_stime, | ||
4092 | cputime_sub(rtime, sig->prev_utime)); | ||
4093 | |||
4094 | *ut = sig->prev_utime; | ||
4095 | *st = sig->prev_stime; | ||
4096 | } | ||
4097 | #endif | ||
4098 | |||
4099 | /* | ||
4100 | * This function gets called by the timer code, with HZ frequency. | ||
4101 | * We call it with interrupts disabled. | ||
4102 | */ | ||
4103 | void scheduler_tick(void) | ||
4104 | { | ||
4105 | int cpu = smp_processor_id(); | ||
4106 | struct rq *rq = cpu_rq(cpu); | ||
4107 | struct task_struct *curr = rq->curr; | ||
4108 | |||
4109 | sched_clock_tick(); | ||
4110 | |||
4111 | raw_spin_lock(&rq->lock); | ||
4112 | update_rq_clock(rq); | ||
4113 | update_cpu_load_active(rq); | ||
4114 | curr->sched_class->task_tick(rq, curr, 0); | ||
4115 | raw_spin_unlock(&rq->lock); | ||
4116 | |||
4117 | perf_event_task_tick(); | ||
4118 | |||
4119 | #ifdef CONFIG_SMP | ||
4120 | rq->idle_at_tick = idle_cpu(cpu); | ||
4121 | trigger_load_balance(rq, cpu); | ||
4122 | #endif | ||
4123 | } | ||
4124 | |||
4125 | notrace unsigned long get_parent_ip(unsigned long addr) | ||
4126 | { | ||
4127 | if (in_lock_functions(addr)) { | ||
4128 | addr = CALLER_ADDR2; | ||
4129 | if (in_lock_functions(addr)) | ||
4130 | addr = CALLER_ADDR3; | ||
4131 | } | ||
4132 | return addr; | ||
4133 | } | ||
4134 | |||
4135 | #if defined(CONFIG_PREEMPT) && (defined(CONFIG_DEBUG_PREEMPT) || \ | ||
4136 | defined(CONFIG_PREEMPT_TRACER)) | ||
4137 | |||
4138 | void __kprobes add_preempt_count(int val) | ||
4139 | { | ||
4140 | #ifdef CONFIG_DEBUG_PREEMPT | ||
4141 | /* | ||
4142 | * Underflow? | ||
4143 | */ | ||
4144 | if (DEBUG_LOCKS_WARN_ON((preempt_count() < 0))) | ||
4145 | return; | ||
4146 | #endif | ||
4147 | preempt_count() += val; | ||
4148 | #ifdef CONFIG_DEBUG_PREEMPT | ||
4149 | /* | ||
4150 | * Spinlock count overflowing soon? | ||
4151 | */ | ||
4152 | DEBUG_LOCKS_WARN_ON((preempt_count() & PREEMPT_MASK) >= | ||
4153 | PREEMPT_MASK - 10); | ||
4154 | #endif | ||
4155 | if (preempt_count() == val) | ||
4156 | trace_preempt_off(CALLER_ADDR0, get_parent_ip(CALLER_ADDR1)); | ||
4157 | } | ||
4158 | EXPORT_SYMBOL(add_preempt_count); | ||
4159 | |||
4160 | void __kprobes sub_preempt_count(int val) | ||
4161 | { | ||
4162 | #ifdef CONFIG_DEBUG_PREEMPT | ||
4163 | /* | ||
4164 | * Underflow? | ||
4165 | */ | ||
4166 | if (DEBUG_LOCKS_WARN_ON(val > preempt_count())) | ||
4167 | return; | ||
4168 | /* | ||
4169 | * Is the spinlock portion underflowing? | ||
4170 | */ | ||
4171 | if (DEBUG_LOCKS_WARN_ON((val < PREEMPT_MASK) && | ||
4172 | !(preempt_count() & PREEMPT_MASK))) | ||
4173 | return; | ||
4174 | #endif | ||
4175 | |||
4176 | if (preempt_count() == val) | ||
4177 | trace_preempt_on(CALLER_ADDR0, get_parent_ip(CALLER_ADDR1)); | ||
4178 | preempt_count() -= val; | ||
4179 | } | ||
4180 | EXPORT_SYMBOL(sub_preempt_count); | ||
4181 | |||
4182 | #endif | ||
4183 | |||
4184 | /* | ||
4185 | * Print scheduling while atomic bug: | ||
4186 | */ | ||
4187 | static noinline void __schedule_bug(struct task_struct *prev) | ||
4188 | { | ||
4189 | struct pt_regs *regs = get_irq_regs(); | ||
4190 | |||
4191 | printk(KERN_ERR "BUG: scheduling while atomic: %s/%d/0x%08x\n", | ||
4192 | prev->comm, prev->pid, preempt_count()); | ||
4193 | |||
4194 | debug_show_held_locks(prev); | ||
4195 | print_modules(); | ||
4196 | if (irqs_disabled()) | ||
4197 | print_irqtrace_events(prev); | ||
4198 | |||
4199 | if (regs) | ||
4200 | show_regs(regs); | ||
4201 | else | ||
4202 | dump_stack(); | ||
4203 | } | ||
4204 | |||
4205 | /* | ||
4206 | * Various schedule()-time debugging checks and statistics: | ||
4207 | */ | ||
4208 | static inline void schedule_debug(struct task_struct *prev) | ||
4209 | { | ||
4210 | /* | ||
4211 | * Test if we are atomic. Since do_exit() needs to call into | ||
4212 | * schedule() atomically, we ignore that path for now. | ||
4213 | * Otherwise, whine if we are scheduling when we should not be. | ||
4214 | */ | ||
4215 | if (unlikely(in_atomic_preempt_off() && !prev->exit_state)) | ||
4216 | __schedule_bug(prev); | ||
4217 | |||
4218 | profile_hit(SCHED_PROFILING, __builtin_return_address(0)); | ||
4219 | |||
4220 | schedstat_inc(this_rq(), sched_count); | ||
4221 | } | ||
4222 | |||
4223 | static void put_prev_task(struct rq *rq, struct task_struct *prev) | ||
4224 | { | ||
4225 | if (prev->on_rq || rq->skip_clock_update < 0) | ||
4226 | update_rq_clock(rq); | ||
4227 | prev->sched_class->put_prev_task(rq, prev); | ||
4228 | } | ||
4229 | |||
4230 | /* | ||
4231 | * Pick up the highest-prio task: | ||
4232 | */ | ||
4233 | static inline struct task_struct * | ||
4234 | pick_next_task(struct rq *rq) | ||
4235 | { | ||
4236 | const struct sched_class *class; | ||
4237 | struct task_struct *p; | ||
4238 | |||
4239 | /* | ||
4240 | * Optimization: we know that if all tasks are in | ||
4241 | * the fair class we can call that function directly: | ||
4242 | */ | ||
4243 | if (likely(rq->nr_running == rq->cfs.nr_running)) { | ||
4244 | p = fair_sched_class.pick_next_task(rq); | ||
4245 | if (likely(p)) | ||
4246 | return p; | ||
4247 | } | ||
4248 | |||
4249 | for_each_class(class) { | ||
4250 | p = class->pick_next_task(rq); | ||
4251 | if (p) | ||
4252 | return p; | ||
4253 | } | ||
4254 | |||
4255 | BUG(); /* the idle class will always have a runnable task */ | ||
4256 | } | ||
4257 | |||
4258 | /* | ||
4259 | * __schedule() is the main scheduler function. | ||
4260 | */ | ||
4261 | static void __sched __schedule(void) | ||
4262 | { | ||
4263 | struct task_struct *prev, *next; | ||
4264 | unsigned long *switch_count; | ||
4265 | struct rq *rq; | ||
4266 | int cpu; | ||
4267 | |||
4268 | need_resched: | ||
4269 | preempt_disable(); | ||
4270 | cpu = smp_processor_id(); | ||
4271 | rq = cpu_rq(cpu); | ||
4272 | rcu_note_context_switch(cpu); | ||
4273 | prev = rq->curr; | ||
4274 | |||
4275 | schedule_debug(prev); | ||
4276 | |||
4277 | if (sched_feat(HRTICK)) | ||
4278 | hrtick_clear(rq); | ||
4279 | |||
4280 | raw_spin_lock_irq(&rq->lock); | ||
4281 | |||
4282 | switch_count = &prev->nivcsw; | ||
4283 | if (prev->state && !(preempt_count() & PREEMPT_ACTIVE)) { | ||
4284 | if (unlikely(signal_pending_state(prev->state, prev))) { | ||
4285 | prev->state = TASK_RUNNING; | ||
4286 | } else { | ||
4287 | deactivate_task(rq, prev, DEQUEUE_SLEEP); | ||
4288 | prev->on_rq = 0; | ||
4289 | |||
4290 | /* | ||
4291 | * If a worker went to sleep, notify and ask workqueue | ||
4292 | * whether it wants to wake up a task to maintain | ||
4293 | * concurrency. | ||
4294 | */ | ||
4295 | if (prev->flags & PF_WQ_WORKER) { | ||
4296 | struct task_struct *to_wakeup; | ||
4297 | |||
4298 | to_wakeup = wq_worker_sleeping(prev, cpu); | ||
4299 | if (to_wakeup) | ||
4300 | try_to_wake_up_local(to_wakeup); | ||
4301 | } | ||
4302 | } | ||
4303 | switch_count = &prev->nvcsw; | ||
4304 | } | ||
4305 | |||
4306 | pre_schedule(rq, prev); | ||
4307 | |||
4308 | if (unlikely(!rq->nr_running)) | ||
4309 | idle_balance(cpu, rq); | ||
4310 | |||
4311 | put_prev_task(rq, prev); | ||
4312 | next = pick_next_task(rq); | ||
4313 | clear_tsk_need_resched(prev); | ||
4314 | rq->skip_clock_update = 0; | ||
4315 | |||
4316 | if (likely(prev != next)) { | ||
4317 | rq->nr_switches++; | ||
4318 | rq->curr = next; | ||
4319 | ++*switch_count; | ||
4320 | |||
4321 | context_switch(rq, prev, next); /* unlocks the rq */ | ||
4322 | /* | ||
4323 | * The context switch have flipped the stack from under us | ||
4324 | * and restored the local variables which were saved when | ||
4325 | * this task called schedule() in the past. prev == current | ||
4326 | * is still correct, but it can be moved to another cpu/rq. | ||
4327 | */ | ||
4328 | cpu = smp_processor_id(); | ||
4329 | rq = cpu_rq(cpu); | ||
4330 | } else | ||
4331 | raw_spin_unlock_irq(&rq->lock); | ||
4332 | |||
4333 | post_schedule(rq); | ||
4334 | |||
4335 | preempt_enable_no_resched(); | ||
4336 | if (need_resched()) | ||
4337 | goto need_resched; | ||
4338 | } | ||
4339 | |||
4340 | static inline void sched_submit_work(struct task_struct *tsk) | ||
4341 | { | ||
4342 | if (!tsk->state) | ||
4343 | return; | ||
4344 | /* | ||
4345 | * If we are going to sleep and we have plugged IO queued, | ||
4346 | * make sure to submit it to avoid deadlocks. | ||
4347 | */ | ||
4348 | if (blk_needs_flush_plug(tsk)) | ||
4349 | blk_schedule_flush_plug(tsk); | ||
4350 | } | ||
4351 | |||
4352 | asmlinkage void __sched schedule(void) | ||
4353 | { | ||
4354 | struct task_struct *tsk = current; | ||
4355 | |||
4356 | sched_submit_work(tsk); | ||
4357 | __schedule(); | ||
4358 | } | ||
4359 | EXPORT_SYMBOL(schedule); | ||
4360 | |||
4361 | #ifdef CONFIG_MUTEX_SPIN_ON_OWNER | ||
4362 | |||
4363 | static inline bool owner_running(struct mutex *lock, struct task_struct *owner) | ||
4364 | { | ||
4365 | if (lock->owner != owner) | ||
4366 | return false; | ||
4367 | |||
4368 | /* | ||
4369 | * Ensure we emit the owner->on_cpu, dereference _after_ checking | ||
4370 | * lock->owner still matches owner, if that fails, owner might | ||
4371 | * point to free()d memory, if it still matches, the rcu_read_lock() | ||
4372 | * ensures the memory stays valid. | ||
4373 | */ | ||
4374 | barrier(); | ||
4375 | |||
4376 | return owner->on_cpu; | ||
4377 | } | ||
4378 | |||
4379 | /* | ||
4380 | * Look out! "owner" is an entirely speculative pointer | ||
4381 | * access and not reliable. | ||
4382 | */ | ||
4383 | int mutex_spin_on_owner(struct mutex *lock, struct task_struct *owner) | ||
4384 | { | ||
4385 | if (!sched_feat(OWNER_SPIN)) | ||
4386 | return 0; | ||
4387 | |||
4388 | rcu_read_lock(); | ||
4389 | while (owner_running(lock, owner)) { | ||
4390 | if (need_resched()) | ||
4391 | break; | ||
4392 | |||
4393 | arch_mutex_cpu_relax(); | ||
4394 | } | ||
4395 | rcu_read_unlock(); | ||
4396 | |||
4397 | /* | ||
4398 | * We break out the loop above on need_resched() and when the | ||
4399 | * owner changed, which is a sign for heavy contention. Return | ||
4400 | * success only when lock->owner is NULL. | ||
4401 | */ | ||
4402 | return lock->owner == NULL; | ||
4403 | } | ||
4404 | #endif | ||
4405 | |||
4406 | #ifdef CONFIG_PREEMPT | ||
4407 | /* | ||
4408 | * this is the entry point to schedule() from in-kernel preemption | ||
4409 | * off of preempt_enable. Kernel preemptions off return from interrupt | ||
4410 | * occur there and call schedule directly. | ||
4411 | */ | ||
4412 | asmlinkage void __sched notrace preempt_schedule(void) | ||
4413 | { | ||
4414 | struct thread_info *ti = current_thread_info(); | ||
4415 | |||
4416 | /* | ||
4417 | * If there is a non-zero preempt_count or interrupts are disabled, | ||
4418 | * we do not want to preempt the current task. Just return.. | ||
4419 | */ | ||
4420 | if (likely(ti->preempt_count || irqs_disabled())) | ||
4421 | return; | ||
4422 | |||
4423 | do { | ||
4424 | add_preempt_count_notrace(PREEMPT_ACTIVE); | ||
4425 | __schedule(); | ||
4426 | sub_preempt_count_notrace(PREEMPT_ACTIVE); | ||
4427 | |||
4428 | /* | ||
4429 | * Check again in case we missed a preemption opportunity | ||
4430 | * between schedule and now. | ||
4431 | */ | ||
4432 | barrier(); | ||
4433 | } while (need_resched()); | ||
4434 | } | ||
4435 | EXPORT_SYMBOL(preempt_schedule); | ||
4436 | |||
4437 | /* | ||
4438 | * this is the entry point to schedule() from kernel preemption | ||
4439 | * off of irq context. | ||
4440 | * Note, that this is called and return with irqs disabled. This will | ||
4441 | * protect us against recursive calling from irq. | ||
4442 | */ | ||
4443 | asmlinkage void __sched preempt_schedule_irq(void) | ||
4444 | { | ||
4445 | struct thread_info *ti = current_thread_info(); | ||
4446 | |||
4447 | /* Catch callers which need to be fixed */ | ||
4448 | BUG_ON(ti->preempt_count || !irqs_disabled()); | ||
4449 | |||
4450 | do { | ||
4451 | add_preempt_count(PREEMPT_ACTIVE); | ||
4452 | local_irq_enable(); | ||
4453 | __schedule(); | ||
4454 | local_irq_disable(); | ||
4455 | sub_preempt_count(PREEMPT_ACTIVE); | ||
4456 | |||
4457 | /* | ||
4458 | * Check again in case we missed a preemption opportunity | ||
4459 | * between schedule and now. | ||
4460 | */ | ||
4461 | barrier(); | ||
4462 | } while (need_resched()); | ||
4463 | } | ||
4464 | |||
4465 | #endif /* CONFIG_PREEMPT */ | ||
4466 | |||
4467 | int default_wake_function(wait_queue_t *curr, unsigned mode, int wake_flags, | ||
4468 | void *key) | ||
4469 | { | ||
4470 | return try_to_wake_up(curr->private, mode, wake_flags); | ||
4471 | } | ||
4472 | EXPORT_SYMBOL(default_wake_function); | ||
4473 | |||
4474 | /* | ||
4475 | * The core wakeup function. Non-exclusive wakeups (nr_exclusive == 0) just | ||
4476 | * wake everything up. If it's an exclusive wakeup (nr_exclusive == small +ve | ||
4477 | * number) then we wake all the non-exclusive tasks and one exclusive task. | ||
4478 | * | ||
4479 | * There are circumstances in which we can try to wake a task which has already | ||
4480 | * started to run but is not in state TASK_RUNNING. try_to_wake_up() returns | ||
4481 | * zero in this (rare) case, and we handle it by continuing to scan the queue. | ||
4482 | */ | ||
4483 | static void __wake_up_common(wait_queue_head_t *q, unsigned int mode, | ||
4484 | int nr_exclusive, int wake_flags, void *key) | ||
4485 | { | ||
4486 | wait_queue_t *curr, *next; | ||
4487 | |||
4488 | list_for_each_entry_safe(curr, next, &q->task_list, task_list) { | ||
4489 | unsigned flags = curr->flags; | ||
4490 | |||
4491 | if (curr->func(curr, mode, wake_flags, key) && | ||
4492 | (flags & WQ_FLAG_EXCLUSIVE) && !--nr_exclusive) | ||
4493 | break; | ||
4494 | } | ||
4495 | } | ||
4496 | |||
4497 | /** | ||
4498 | * __wake_up - wake up threads blocked on a waitqueue. | ||
4499 | * @q: the waitqueue | ||
4500 | * @mode: which threads | ||
4501 | * @nr_exclusive: how many wake-one or wake-many threads to wake up | ||
4502 | * @key: is directly passed to the wakeup function | ||
4503 | * | ||
4504 | * It may be assumed that this function implies a write memory barrier before | ||
4505 | * changing the task state if and only if any tasks are woken up. | ||
4506 | */ | ||
4507 | void __wake_up(wait_queue_head_t *q, unsigned int mode, | ||
4508 | int nr_exclusive, void *key) | ||
4509 | { | ||
4510 | unsigned long flags; | ||
4511 | |||
4512 | spin_lock_irqsave(&q->lock, flags); | ||
4513 | __wake_up_common(q, mode, nr_exclusive, 0, key); | ||
4514 | spin_unlock_irqrestore(&q->lock, flags); | ||
4515 | } | ||
4516 | EXPORT_SYMBOL(__wake_up); | ||
4517 | |||
4518 | /* | ||
4519 | * Same as __wake_up but called with the spinlock in wait_queue_head_t held. | ||
4520 | */ | ||
4521 | void __wake_up_locked(wait_queue_head_t *q, unsigned int mode) | ||
4522 | { | ||
4523 | __wake_up_common(q, mode, 1, 0, NULL); | ||
4524 | } | ||
4525 | EXPORT_SYMBOL_GPL(__wake_up_locked); | ||
4526 | |||
4527 | void __wake_up_locked_key(wait_queue_head_t *q, unsigned int mode, void *key) | ||
4528 | { | ||
4529 | __wake_up_common(q, mode, 1, 0, key); | ||
4530 | } | ||
4531 | EXPORT_SYMBOL_GPL(__wake_up_locked_key); | ||
4532 | |||
4533 | /** | ||
4534 | * __wake_up_sync_key - wake up threads blocked on a waitqueue. | ||
4535 | * @q: the waitqueue | ||
4536 | * @mode: which threads | ||
4537 | * @nr_exclusive: how many wake-one or wake-many threads to wake up | ||
4538 | * @key: opaque value to be passed to wakeup targets | ||
4539 | * | ||
4540 | * The sync wakeup differs that the waker knows that it will schedule | ||
4541 | * away soon, so while the target thread will be woken up, it will not | ||
4542 | * be migrated to another CPU - ie. the two threads are 'synchronized' | ||
4543 | * with each other. This can prevent needless bouncing between CPUs. | ||
4544 | * | ||
4545 | * On UP it can prevent extra preemption. | ||
4546 | * | ||
4547 | * It may be assumed that this function implies a write memory barrier before | ||
4548 | * changing the task state if and only if any tasks are woken up. | ||
4549 | */ | ||
4550 | void __wake_up_sync_key(wait_queue_head_t *q, unsigned int mode, | ||
4551 | int nr_exclusive, void *key) | ||
4552 | { | ||
4553 | unsigned long flags; | ||
4554 | int wake_flags = WF_SYNC; | ||
4555 | |||
4556 | if (unlikely(!q)) | ||
4557 | return; | ||
4558 | |||
4559 | if (unlikely(!nr_exclusive)) | ||
4560 | wake_flags = 0; | ||
4561 | |||
4562 | spin_lock_irqsave(&q->lock, flags); | ||
4563 | __wake_up_common(q, mode, nr_exclusive, wake_flags, key); | ||
4564 | spin_unlock_irqrestore(&q->lock, flags); | ||
4565 | } | ||
4566 | EXPORT_SYMBOL_GPL(__wake_up_sync_key); | ||
4567 | |||
4568 | /* | ||
4569 | * __wake_up_sync - see __wake_up_sync_key() | ||
4570 | */ | ||
4571 | void __wake_up_sync(wait_queue_head_t *q, unsigned int mode, int nr_exclusive) | ||
4572 | { | ||
4573 | __wake_up_sync_key(q, mode, nr_exclusive, NULL); | ||
4574 | } | ||
4575 | EXPORT_SYMBOL_GPL(__wake_up_sync); /* For internal use only */ | ||
4576 | |||
4577 | /** | ||
4578 | * complete: - signals a single thread waiting on this completion | ||
4579 | * @x: holds the state of this particular completion | ||
4580 | * | ||
4581 | * This will wake up a single thread waiting on this completion. Threads will be | ||
4582 | * awakened in the same order in which they were queued. | ||
4583 | * | ||
4584 | * See also complete_all(), wait_for_completion() and related routines. | ||
4585 | * | ||
4586 | * It may be assumed that this function implies a write memory barrier before | ||
4587 | * changing the task state if and only if any tasks are woken up. | ||
4588 | */ | ||
4589 | void complete(struct completion *x) | ||
4590 | { | ||
4591 | unsigned long flags; | ||
4592 | |||
4593 | spin_lock_irqsave(&x->wait.lock, flags); | ||
4594 | x->done++; | ||
4595 | __wake_up_common(&x->wait, TASK_NORMAL, 1, 0, NULL); | ||
4596 | spin_unlock_irqrestore(&x->wait.lock, flags); | ||
4597 | } | ||
4598 | EXPORT_SYMBOL(complete); | ||
4599 | |||
4600 | /** | ||
4601 | * complete_all: - signals all threads waiting on this completion | ||
4602 | * @x: holds the state of this particular completion | ||
4603 | * | ||
4604 | * This will wake up all threads waiting on this particular completion event. | ||
4605 | * | ||
4606 | * It may be assumed that this function implies a write memory barrier before | ||
4607 | * changing the task state if and only if any tasks are woken up. | ||
4608 | */ | ||
4609 | void complete_all(struct completion *x) | ||
4610 | { | ||
4611 | unsigned long flags; | ||
4612 | |||
4613 | spin_lock_irqsave(&x->wait.lock, flags); | ||
4614 | x->done += UINT_MAX/2; | ||
4615 | __wake_up_common(&x->wait, TASK_NORMAL, 0, 0, NULL); | ||
4616 | spin_unlock_irqrestore(&x->wait.lock, flags); | ||
4617 | } | ||
4618 | EXPORT_SYMBOL(complete_all); | ||
4619 | |||
4620 | static inline long __sched | ||
4621 | do_wait_for_common(struct completion *x, long timeout, int state) | ||
4622 | { | ||
4623 | if (!x->done) { | ||
4624 | DECLARE_WAITQUEUE(wait, current); | ||
4625 | |||
4626 | __add_wait_queue_tail_exclusive(&x->wait, &wait); | ||
4627 | do { | ||
4628 | if (signal_pending_state(state, current)) { | ||
4629 | timeout = -ERESTARTSYS; | ||
4630 | break; | ||
4631 | } | ||
4632 | __set_current_state(state); | ||
4633 | spin_unlock_irq(&x->wait.lock); | ||
4634 | timeout = schedule_timeout(timeout); | ||
4635 | spin_lock_irq(&x->wait.lock); | ||
4636 | } while (!x->done && timeout); | ||
4637 | __remove_wait_queue(&x->wait, &wait); | ||
4638 | if (!x->done) | ||
4639 | return timeout; | ||
4640 | } | ||
4641 | x->done--; | ||
4642 | return timeout ?: 1; | ||
4643 | } | ||
4644 | |||
4645 | static long __sched | ||
4646 | wait_for_common(struct completion *x, long timeout, int state) | ||
4647 | { | ||
4648 | might_sleep(); | ||
4649 | |||
4650 | spin_lock_irq(&x->wait.lock); | ||
4651 | timeout = do_wait_for_common(x, timeout, state); | ||
4652 | spin_unlock_irq(&x->wait.lock); | ||
4653 | return timeout; | ||
4654 | } | ||
4655 | |||
4656 | /** | ||
4657 | * wait_for_completion: - waits for completion of a task | ||
4658 | * @x: holds the state of this particular completion | ||
4659 | * | ||
4660 | * This waits to be signaled for completion of a specific task. It is NOT | ||
4661 | * interruptible and there is no timeout. | ||
4662 | * | ||
4663 | * See also similar routines (i.e. wait_for_completion_timeout()) with timeout | ||
4664 | * and interrupt capability. Also see complete(). | ||
4665 | */ | ||
4666 | void __sched wait_for_completion(struct completion *x) | ||
4667 | { | ||
4668 | wait_for_common(x, MAX_SCHEDULE_TIMEOUT, TASK_UNINTERRUPTIBLE); | ||
4669 | } | ||
4670 | EXPORT_SYMBOL(wait_for_completion); | ||
4671 | |||
4672 | /** | ||
4673 | * wait_for_completion_timeout: - waits for completion of a task (w/timeout) | ||
4674 | * @x: holds the state of this particular completion | ||
4675 | * @timeout: timeout value in jiffies | ||
4676 | * | ||
4677 | * This waits for either a completion of a specific task to be signaled or for a | ||
4678 | * specified timeout to expire. The timeout is in jiffies. It is not | ||
4679 | * interruptible. | ||
4680 | */ | ||
4681 | unsigned long __sched | ||
4682 | wait_for_completion_timeout(struct completion *x, unsigned long timeout) | ||
4683 | { | ||
4684 | return wait_for_common(x, timeout, TASK_UNINTERRUPTIBLE); | ||
4685 | } | ||
4686 | EXPORT_SYMBOL(wait_for_completion_timeout); | ||
4687 | |||
4688 | /** | ||
4689 | * wait_for_completion_interruptible: - waits for completion of a task (w/intr) | ||
4690 | * @x: holds the state of this particular completion | ||
4691 | * | ||
4692 | * This waits for completion of a specific task to be signaled. It is | ||
4693 | * interruptible. | ||
4694 | */ | ||
4695 | int __sched wait_for_completion_interruptible(struct completion *x) | ||
4696 | { | ||
4697 | long t = wait_for_common(x, MAX_SCHEDULE_TIMEOUT, TASK_INTERRUPTIBLE); | ||
4698 | if (t == -ERESTARTSYS) | ||
4699 | return t; | ||
4700 | return 0; | ||
4701 | } | ||
4702 | EXPORT_SYMBOL(wait_for_completion_interruptible); | ||
4703 | |||
4704 | /** | ||
4705 | * wait_for_completion_interruptible_timeout: - waits for completion (w/(to,intr)) | ||
4706 | * @x: holds the state of this particular completion | ||
4707 | * @timeout: timeout value in jiffies | ||
4708 | * | ||
4709 | * This waits for either a completion of a specific task to be signaled or for a | ||
4710 | * specified timeout to expire. It is interruptible. The timeout is in jiffies. | ||
4711 | */ | ||
4712 | long __sched | ||
4713 | wait_for_completion_interruptible_timeout(struct completion *x, | ||
4714 | unsigned long timeout) | ||
4715 | { | ||
4716 | return wait_for_common(x, timeout, TASK_INTERRUPTIBLE); | ||
4717 | } | ||
4718 | EXPORT_SYMBOL(wait_for_completion_interruptible_timeout); | ||
4719 | |||
4720 | /** | ||
4721 | * wait_for_completion_killable: - waits for completion of a task (killable) | ||
4722 | * @x: holds the state of this particular completion | ||
4723 | * | ||
4724 | * This waits to be signaled for completion of a specific task. It can be | ||
4725 | * interrupted by a kill signal. | ||
4726 | */ | ||
4727 | int __sched wait_for_completion_killable(struct completion *x) | ||
4728 | { | ||
4729 | long t = wait_for_common(x, MAX_SCHEDULE_TIMEOUT, TASK_KILLABLE); | ||
4730 | if (t == -ERESTARTSYS) | ||
4731 | return t; | ||
4732 | return 0; | ||
4733 | } | ||
4734 | EXPORT_SYMBOL(wait_for_completion_killable); | ||
4735 | |||
4736 | /** | ||
4737 | * wait_for_completion_killable_timeout: - waits for completion of a task (w/(to,killable)) | ||
4738 | * @x: holds the state of this particular completion | ||
4739 | * @timeout: timeout value in jiffies | ||
4740 | * | ||
4741 | * This waits for either a completion of a specific task to be | ||
4742 | * signaled or for a specified timeout to expire. It can be | ||
4743 | * interrupted by a kill signal. The timeout is in jiffies. | ||
4744 | */ | ||
4745 | long __sched | ||
4746 | wait_for_completion_killable_timeout(struct completion *x, | ||
4747 | unsigned long timeout) | ||
4748 | { | ||
4749 | return wait_for_common(x, timeout, TASK_KILLABLE); | ||
4750 | } | ||
4751 | EXPORT_SYMBOL(wait_for_completion_killable_timeout); | ||
4752 | |||
4753 | /** | ||
4754 | * try_wait_for_completion - try to decrement a completion without blocking | ||
4755 | * @x: completion structure | ||
4756 | * | ||
4757 | * Returns: 0 if a decrement cannot be done without blocking | ||
4758 | * 1 if a decrement succeeded. | ||
4759 | * | ||
4760 | * If a completion is being used as a counting completion, | ||
4761 | * attempt to decrement the counter without blocking. This | ||
4762 | * enables us to avoid waiting if the resource the completion | ||
4763 | * is protecting is not available. | ||
4764 | */ | ||
4765 | bool try_wait_for_completion(struct completion *x) | ||
4766 | { | ||
4767 | unsigned long flags; | ||
4768 | int ret = 1; | ||
4769 | |||
4770 | spin_lock_irqsave(&x->wait.lock, flags); | ||
4771 | if (!x->done) | ||
4772 | ret = 0; | ||
4773 | else | ||
4774 | x->done--; | ||
4775 | spin_unlock_irqrestore(&x->wait.lock, flags); | ||
4776 | return ret; | ||
4777 | } | ||
4778 | EXPORT_SYMBOL(try_wait_for_completion); | ||
4779 | |||
4780 | /** | ||
4781 | * completion_done - Test to see if a completion has any waiters | ||
4782 | * @x: completion structure | ||
4783 | * | ||
4784 | * Returns: 0 if there are waiters (wait_for_completion() in progress) | ||
4785 | * 1 if there are no waiters. | ||
4786 | * | ||
4787 | */ | ||
4788 | bool completion_done(struct completion *x) | ||
4789 | { | ||
4790 | unsigned long flags; | ||
4791 | int ret = 1; | ||
4792 | |||
4793 | spin_lock_irqsave(&x->wait.lock, flags); | ||
4794 | if (!x->done) | ||
4795 | ret = 0; | ||
4796 | spin_unlock_irqrestore(&x->wait.lock, flags); | ||
4797 | return ret; | ||
4798 | } | ||
4799 | EXPORT_SYMBOL(completion_done); | ||
4800 | |||
4801 | static long __sched | ||
4802 | sleep_on_common(wait_queue_head_t *q, int state, long timeout) | ||
4803 | { | ||
4804 | unsigned long flags; | ||
4805 | wait_queue_t wait; | ||
4806 | |||
4807 | init_waitqueue_entry(&wait, current); | ||
4808 | |||
4809 | __set_current_state(state); | ||
4810 | |||
4811 | spin_lock_irqsave(&q->lock, flags); | ||
4812 | __add_wait_queue(q, &wait); | ||
4813 | spin_unlock(&q->lock); | ||
4814 | timeout = schedule_timeout(timeout); | ||
4815 | spin_lock_irq(&q->lock); | ||
4816 | __remove_wait_queue(q, &wait); | ||
4817 | spin_unlock_irqrestore(&q->lock, flags); | ||
4818 | |||
4819 | return timeout; | ||
4820 | } | ||
4821 | |||
4822 | void __sched interruptible_sleep_on(wait_queue_head_t *q) | ||
4823 | { | ||
4824 | sleep_on_common(q, TASK_INTERRUPTIBLE, MAX_SCHEDULE_TIMEOUT); | ||
4825 | } | ||
4826 | EXPORT_SYMBOL(interruptible_sleep_on); | ||
4827 | |||
4828 | long __sched | ||
4829 | interruptible_sleep_on_timeout(wait_queue_head_t *q, long timeout) | ||
4830 | { | ||
4831 | return sleep_on_common(q, TASK_INTERRUPTIBLE, timeout); | ||
4832 | } | ||
4833 | EXPORT_SYMBOL(interruptible_sleep_on_timeout); | ||
4834 | |||
4835 | void __sched sleep_on(wait_queue_head_t *q) | ||
4836 | { | ||
4837 | sleep_on_common(q, TASK_UNINTERRUPTIBLE, MAX_SCHEDULE_TIMEOUT); | ||
4838 | } | ||
4839 | EXPORT_SYMBOL(sleep_on); | ||
4840 | |||
4841 | long __sched sleep_on_timeout(wait_queue_head_t *q, long timeout) | ||
4842 | { | ||
4843 | return sleep_on_common(q, TASK_UNINTERRUPTIBLE, timeout); | ||
4844 | } | ||
4845 | EXPORT_SYMBOL(sleep_on_timeout); | ||
4846 | |||
4847 | #ifdef CONFIG_RT_MUTEXES | ||
4848 | |||
4849 | /* | ||
4850 | * rt_mutex_setprio - set the current priority of a task | ||
4851 | * @p: task | ||
4852 | * @prio: prio value (kernel-internal form) | ||
4853 | * | ||
4854 | * This function changes the 'effective' priority of a task. It does | ||
4855 | * not touch ->normal_prio like __setscheduler(). | ||
4856 | * | ||
4857 | * Used by the rt_mutex code to implement priority inheritance logic. | ||
4858 | */ | ||
4859 | void rt_mutex_setprio(struct task_struct *p, int prio) | ||
4860 | { | ||
4861 | int oldprio, on_rq, running; | ||
4862 | struct rq *rq; | ||
4863 | const struct sched_class *prev_class; | ||
4864 | |||
4865 | BUG_ON(prio < 0 || prio > MAX_PRIO); | ||
4866 | |||
4867 | rq = __task_rq_lock(p); | ||
4868 | |||
4869 | trace_sched_pi_setprio(p, prio); | ||
4870 | oldprio = p->prio; | ||
4871 | prev_class = p->sched_class; | ||
4872 | on_rq = p->on_rq; | ||
4873 | running = task_current(rq, p); | ||
4874 | if (on_rq) | ||
4875 | dequeue_task(rq, p, 0); | ||
4876 | if (running) | ||
4877 | p->sched_class->put_prev_task(rq, p); | ||
4878 | |||
4879 | if (rt_prio(prio)) | ||
4880 | p->sched_class = &rt_sched_class; | ||
4881 | else | ||
4882 | p->sched_class = &fair_sched_class; | ||
4883 | |||
4884 | p->prio = prio; | ||
4885 | |||
4886 | if (running) | ||
4887 | p->sched_class->set_curr_task(rq); | ||
4888 | if (on_rq) | ||
4889 | enqueue_task(rq, p, oldprio < prio ? ENQUEUE_HEAD : 0); | ||
4890 | |||
4891 | check_class_changed(rq, p, prev_class, oldprio); | ||
4892 | __task_rq_unlock(rq); | ||
4893 | } | ||
4894 | |||
4895 | #endif | ||
4896 | |||
4897 | void set_user_nice(struct task_struct *p, long nice) | ||
4898 | { | ||
4899 | int old_prio, delta, on_rq; | ||
4900 | unsigned long flags; | ||
4901 | struct rq *rq; | ||
4902 | |||
4903 | if (TASK_NICE(p) == nice || nice < -20 || nice > 19) | ||
4904 | return; | ||
4905 | /* | ||
4906 | * We have to be careful, if called from sys_setpriority(), | ||
4907 | * the task might be in the middle of scheduling on another CPU. | ||
4908 | */ | ||
4909 | rq = task_rq_lock(p, &flags); | ||
4910 | /* | ||
4911 | * The RT priorities are set via sched_setscheduler(), but we still | ||
4912 | * allow the 'normal' nice value to be set - but as expected | ||
4913 | * it wont have any effect on scheduling until the task is | ||
4914 | * SCHED_FIFO/SCHED_RR: | ||
4915 | */ | ||
4916 | if (task_has_rt_policy(p)) { | ||
4917 | p->static_prio = NICE_TO_PRIO(nice); | ||
4918 | goto out_unlock; | ||
4919 | } | ||
4920 | on_rq = p->on_rq; | ||
4921 | if (on_rq) | ||
4922 | dequeue_task(rq, p, 0); | ||
4923 | |||
4924 | p->static_prio = NICE_TO_PRIO(nice); | ||
4925 | set_load_weight(p); | ||
4926 | old_prio = p->prio; | ||
4927 | p->prio = effective_prio(p); | ||
4928 | delta = p->prio - old_prio; | ||
4929 | |||
4930 | if (on_rq) { | ||
4931 | enqueue_task(rq, p, 0); | ||
4932 | /* | ||
4933 | * If the task increased its priority or is running and | ||
4934 | * lowered its priority, then reschedule its CPU: | ||
4935 | */ | ||
4936 | if (delta < 0 || (delta > 0 && task_running(rq, p))) | ||
4937 | resched_task(rq->curr); | ||
4938 | } | ||
4939 | out_unlock: | ||
4940 | task_rq_unlock(rq, p, &flags); | ||
4941 | } | ||
4942 | EXPORT_SYMBOL(set_user_nice); | ||
4943 | |||
4944 | /* | ||
4945 | * can_nice - check if a task can reduce its nice value | ||
4946 | * @p: task | ||
4947 | * @nice: nice value | ||
4948 | */ | ||
4949 | int can_nice(const struct task_struct *p, const int nice) | ||
4950 | { | ||
4951 | /* convert nice value [19,-20] to rlimit style value [1,40] */ | ||
4952 | int nice_rlim = 20 - nice; | ||
4953 | |||
4954 | return (nice_rlim <= task_rlimit(p, RLIMIT_NICE) || | ||
4955 | capable(CAP_SYS_NICE)); | ||
4956 | } | ||
4957 | |||
4958 | #ifdef __ARCH_WANT_SYS_NICE | ||
4959 | |||
4960 | /* | ||
4961 | * sys_nice - change the priority of the current process. | ||
4962 | * @increment: priority increment | ||
4963 | * | ||
4964 | * sys_setpriority is a more generic, but much slower function that | ||
4965 | * does similar things. | ||
4966 | */ | ||
4967 | SYSCALL_DEFINE1(nice, int, increment) | ||
4968 | { | ||
4969 | long nice, retval; | ||
4970 | |||
4971 | /* | ||
4972 | * Setpriority might change our priority at the same moment. | ||
4973 | * We don't have to worry. Conceptually one call occurs first | ||
4974 | * and we have a single winner. | ||
4975 | */ | ||
4976 | if (increment < -40) | ||
4977 | increment = -40; | ||
4978 | if (increment > 40) | ||
4979 | increment = 40; | ||
4980 | |||
4981 | nice = TASK_NICE(current) + increment; | ||
4982 | if (nice < -20) | ||
4983 | nice = -20; | ||
4984 | if (nice > 19) | ||
4985 | nice = 19; | ||
4986 | |||
4987 | if (increment < 0 && !can_nice(current, nice)) | ||
4988 | return -EPERM; | ||
4989 | |||
4990 | retval = security_task_setnice(current, nice); | ||
4991 | if (retval) | ||
4992 | return retval; | ||
4993 | |||
4994 | set_user_nice(current, nice); | ||
4995 | return 0; | ||
4996 | } | ||
4997 | |||
4998 | #endif | ||
4999 | |||
5000 | /** | ||
5001 | * task_prio - return the priority value of a given task. | ||
5002 | * @p: the task in question. | ||
5003 | * | ||
5004 | * This is the priority value as seen by users in /proc. | ||
5005 | * RT tasks are offset by -200. Normal tasks are centered | ||
5006 | * around 0, value goes from -16 to +15. | ||
5007 | */ | ||
5008 | int task_prio(const struct task_struct *p) | ||
5009 | { | ||
5010 | return p->prio - MAX_RT_PRIO; | ||
5011 | } | ||
5012 | |||
5013 | /** | ||
5014 | * task_nice - return the nice value of a given task. | ||
5015 | * @p: the task in question. | ||
5016 | */ | ||
5017 | int task_nice(const struct task_struct *p) | ||
5018 | { | ||
5019 | return TASK_NICE(p); | ||
5020 | } | ||
5021 | EXPORT_SYMBOL(task_nice); | ||
5022 | |||
5023 | /** | ||
5024 | * idle_cpu - is a given cpu idle currently? | ||
5025 | * @cpu: the processor in question. | ||
5026 | */ | ||
5027 | int idle_cpu(int cpu) | ||
5028 | { | ||
5029 | return cpu_curr(cpu) == cpu_rq(cpu)->idle; | ||
5030 | } | ||
5031 | |||
5032 | /** | ||
5033 | * idle_task - return the idle task for a given cpu. | ||
5034 | * @cpu: the processor in question. | ||
5035 | */ | ||
5036 | struct task_struct *idle_task(int cpu) | ||
5037 | { | ||
5038 | return cpu_rq(cpu)->idle; | ||
5039 | } | ||
5040 | |||
5041 | /** | ||
5042 | * find_process_by_pid - find a process with a matching PID value. | ||
5043 | * @pid: the pid in question. | ||
5044 | */ | ||
5045 | static struct task_struct *find_process_by_pid(pid_t pid) | ||
5046 | { | ||
5047 | return pid ? find_task_by_vpid(pid) : current; | ||
5048 | } | ||
5049 | |||
5050 | /* Actually do priority change: must hold rq lock. */ | ||
5051 | static void | ||
5052 | __setscheduler(struct rq *rq, struct task_struct *p, int policy, int prio) | ||
5053 | { | ||
5054 | p->policy = policy; | ||
5055 | p->rt_priority = prio; | ||
5056 | p->normal_prio = normal_prio(p); | ||
5057 | /* we are holding p->pi_lock already */ | ||
5058 | p->prio = rt_mutex_getprio(p); | ||
5059 | if (rt_prio(p->prio)) | ||
5060 | p->sched_class = &rt_sched_class; | ||
5061 | else | ||
5062 | p->sched_class = &fair_sched_class; | ||
5063 | set_load_weight(p); | ||
5064 | } | ||
5065 | |||
5066 | /* | ||
5067 | * check the target process has a UID that matches the current process's | ||
5068 | */ | ||
5069 | static bool check_same_owner(struct task_struct *p) | ||
5070 | { | ||
5071 | const struct cred *cred = current_cred(), *pcred; | ||
5072 | bool match; | ||
5073 | |||
5074 | rcu_read_lock(); | ||
5075 | pcred = __task_cred(p); | ||
5076 | if (cred->user->user_ns == pcred->user->user_ns) | ||
5077 | match = (cred->euid == pcred->euid || | ||
5078 | cred->euid == pcred->uid); | ||
5079 | else | ||
5080 | match = false; | ||
5081 | rcu_read_unlock(); | ||
5082 | return match; | ||
5083 | } | ||
5084 | |||
5085 | static int __sched_setscheduler(struct task_struct *p, int policy, | ||
5086 | const struct sched_param *param, bool user) | ||
5087 | { | ||
5088 | int retval, oldprio, oldpolicy = -1, on_rq, running; | ||
5089 | unsigned long flags; | ||
5090 | const struct sched_class *prev_class; | ||
5091 | struct rq *rq; | ||
5092 | int reset_on_fork; | ||
5093 | |||
5094 | /* may grab non-irq protected spin_locks */ | ||
5095 | BUG_ON(in_interrupt()); | ||
5096 | recheck: | ||
5097 | /* double check policy once rq lock held */ | ||
5098 | if (policy < 0) { | ||
5099 | reset_on_fork = p->sched_reset_on_fork; | ||
5100 | policy = oldpolicy = p->policy; | ||
5101 | } else { | ||
5102 | reset_on_fork = !!(policy & SCHED_RESET_ON_FORK); | ||
5103 | policy &= ~SCHED_RESET_ON_FORK; | ||
5104 | |||
5105 | if (policy != SCHED_FIFO && policy != SCHED_RR && | ||
5106 | policy != SCHED_NORMAL && policy != SCHED_BATCH && | ||
5107 | policy != SCHED_IDLE) | ||
5108 | return -EINVAL; | ||
5109 | } | ||
5110 | |||
5111 | /* | ||
5112 | * Valid priorities for SCHED_FIFO and SCHED_RR are | ||
5113 | * 1..MAX_USER_RT_PRIO-1, valid priority for SCHED_NORMAL, | ||
5114 | * SCHED_BATCH and SCHED_IDLE is 0. | ||
5115 | */ | ||
5116 | if (param->sched_priority < 0 || | ||
5117 | (p->mm && param->sched_priority > MAX_USER_RT_PRIO-1) || | ||
5118 | (!p->mm && param->sched_priority > MAX_RT_PRIO-1)) | ||
5119 | return -EINVAL; | ||
5120 | if (rt_policy(policy) != (param->sched_priority != 0)) | ||
5121 | return -EINVAL; | ||
5122 | |||
5123 | /* | ||
5124 | * Allow unprivileged RT tasks to decrease priority: | ||
5125 | */ | ||
5126 | if (user && !capable(CAP_SYS_NICE)) { | ||
5127 | if (rt_policy(policy)) { | ||
5128 | unsigned long rlim_rtprio = | ||
5129 | task_rlimit(p, RLIMIT_RTPRIO); | ||
5130 | |||
5131 | /* can't set/change the rt policy */ | ||
5132 | if (policy != p->policy && !rlim_rtprio) | ||
5133 | return -EPERM; | ||
5134 | |||
5135 | /* can't increase priority */ | ||
5136 | if (param->sched_priority > p->rt_priority && | ||
5137 | param->sched_priority > rlim_rtprio) | ||
5138 | return -EPERM; | ||
5139 | } | ||
5140 | |||
5141 | /* | ||
5142 | * Treat SCHED_IDLE as nice 20. Only allow a switch to | ||
5143 | * SCHED_NORMAL if the RLIMIT_NICE would normally permit it. | ||
5144 | */ | ||
5145 | if (p->policy == SCHED_IDLE && policy != SCHED_IDLE) { | ||
5146 | if (!can_nice(p, TASK_NICE(p))) | ||
5147 | return -EPERM; | ||
5148 | } | ||
5149 | |||
5150 | /* can't change other user's priorities */ | ||
5151 | if (!check_same_owner(p)) | ||
5152 | return -EPERM; | ||
5153 | |||
5154 | /* Normal users shall not reset the sched_reset_on_fork flag */ | ||
5155 | if (p->sched_reset_on_fork && !reset_on_fork) | ||
5156 | return -EPERM; | ||
5157 | } | ||
5158 | |||
5159 | if (user) { | ||
5160 | retval = security_task_setscheduler(p); | ||
5161 | if (retval) | ||
5162 | return retval; | ||
5163 | } | ||
5164 | |||
5165 | /* | ||
5166 | * make sure no PI-waiters arrive (or leave) while we are | ||
5167 | * changing the priority of the task: | ||
5168 | * | ||
5169 | * To be able to change p->policy safely, the appropriate | ||
5170 | * runqueue lock must be held. | ||
5171 | */ | ||
5172 | rq = task_rq_lock(p, &flags); | ||
5173 | |||
5174 | /* | ||
5175 | * Changing the policy of the stop threads its a very bad idea | ||
5176 | */ | ||
5177 | if (p == rq->stop) { | ||
5178 | task_rq_unlock(rq, p, &flags); | ||
5179 | return -EINVAL; | ||
5180 | } | ||
5181 | |||
5182 | /* | ||
5183 | * If not changing anything there's no need to proceed further: | ||
5184 | */ | ||
5185 | if (unlikely(policy == p->policy && (!rt_policy(policy) || | ||
5186 | param->sched_priority == p->rt_priority))) { | ||
5187 | |||
5188 | __task_rq_unlock(rq); | ||
5189 | raw_spin_unlock_irqrestore(&p->pi_lock, flags); | ||
5190 | return 0; | ||
5191 | } | ||
5192 | |||
5193 | #ifdef CONFIG_RT_GROUP_SCHED | ||
5194 | if (user) { | ||
5195 | /* | ||
5196 | * Do not allow realtime tasks into groups that have no runtime | ||
5197 | * assigned. | ||
5198 | */ | ||
5199 | if (rt_bandwidth_enabled() && rt_policy(policy) && | ||
5200 | task_group(p)->rt_bandwidth.rt_runtime == 0 && | ||
5201 | !task_group_is_autogroup(task_group(p))) { | ||
5202 | task_rq_unlock(rq, p, &flags); | ||
5203 | return -EPERM; | ||
5204 | } | ||
5205 | } | ||
5206 | #endif | ||
5207 | |||
5208 | /* recheck policy now with rq lock held */ | ||
5209 | if (unlikely(oldpolicy != -1 && oldpolicy != p->policy)) { | ||
5210 | policy = oldpolicy = -1; | ||
5211 | task_rq_unlock(rq, p, &flags); | ||
5212 | goto recheck; | ||
5213 | } | ||
5214 | on_rq = p->on_rq; | ||
5215 | running = task_current(rq, p); | ||
5216 | if (on_rq) | ||
5217 | deactivate_task(rq, p, 0); | ||
5218 | if (running) | ||
5219 | p->sched_class->put_prev_task(rq, p); | ||
5220 | |||
5221 | p->sched_reset_on_fork = reset_on_fork; | ||
5222 | |||
5223 | oldprio = p->prio; | ||
5224 | prev_class = p->sched_class; | ||
5225 | __setscheduler(rq, p, policy, param->sched_priority); | ||
5226 | |||
5227 | if (running) | ||
5228 | p->sched_class->set_curr_task(rq); | ||
5229 | if (on_rq) | ||
5230 | activate_task(rq, p, 0); | ||
5231 | |||
5232 | check_class_changed(rq, p, prev_class, oldprio); | ||
5233 | task_rq_unlock(rq, p, &flags); | ||
5234 | |||
5235 | rt_mutex_adjust_pi(p); | ||
5236 | |||
5237 | return 0; | ||
5238 | } | ||
5239 | |||
5240 | /** | ||
5241 | * sched_setscheduler - change the scheduling policy and/or RT priority of a thread. | ||
5242 | * @p: the task in question. | ||
5243 | * @policy: new policy. | ||
5244 | * @param: structure containing the new RT priority. | ||
5245 | * | ||
5246 | * NOTE that the task may be already dead. | ||
5247 | */ | ||
5248 | int sched_setscheduler(struct task_struct *p, int policy, | ||
5249 | const struct sched_param *param) | ||
5250 | { | ||
5251 | return __sched_setscheduler(p, policy, param, true); | ||
5252 | } | ||
5253 | EXPORT_SYMBOL_GPL(sched_setscheduler); | ||
5254 | |||
5255 | /** | ||
5256 | * sched_setscheduler_nocheck - change the scheduling policy and/or RT priority of a thread from kernelspace. | ||
5257 | * @p: the task in question. | ||
5258 | * @policy: new policy. | ||
5259 | * @param: structure containing the new RT priority. | ||
5260 | * | ||
5261 | * Just like sched_setscheduler, only don't bother checking if the | ||
5262 | * current context has permission. For example, this is needed in | ||
5263 | * stop_machine(): we create temporary high priority worker threads, | ||
5264 | * but our caller might not have that capability. | ||
5265 | */ | ||
5266 | int sched_setscheduler_nocheck(struct task_struct *p, int policy, | ||
5267 | const struct sched_param *param) | ||
5268 | { | ||
5269 | return __sched_setscheduler(p, policy, param, false); | ||
5270 | } | ||
5271 | |||
5272 | static int | ||
5273 | do_sched_setscheduler(pid_t pid, int policy, struct sched_param __user *param) | ||
5274 | { | ||
5275 | struct sched_param lparam; | ||
5276 | struct task_struct *p; | ||
5277 | int retval; | ||
5278 | |||
5279 | if (!param || pid < 0) | ||
5280 | return -EINVAL; | ||
5281 | if (copy_from_user(&lparam, param, sizeof(struct sched_param))) | ||
5282 | return -EFAULT; | ||
5283 | |||
5284 | rcu_read_lock(); | ||
5285 | retval = -ESRCH; | ||
5286 | p = find_process_by_pid(pid); | ||
5287 | if (p != NULL) | ||
5288 | retval = sched_setscheduler(p, policy, &lparam); | ||
5289 | rcu_read_unlock(); | ||
5290 | |||
5291 | return retval; | ||
5292 | } | ||
5293 | |||
5294 | /** | ||
5295 | * sys_sched_setscheduler - set/change the scheduler policy and RT priority | ||
5296 | * @pid: the pid in question. | ||
5297 | * @policy: new policy. | ||
5298 | * @param: structure containing the new RT priority. | ||
5299 | */ | ||
5300 | SYSCALL_DEFINE3(sched_setscheduler, pid_t, pid, int, policy, | ||
5301 | struct sched_param __user *, param) | ||
5302 | { | ||
5303 | /* negative values for policy are not valid */ | ||
5304 | if (policy < 0) | ||
5305 | return -EINVAL; | ||
5306 | |||
5307 | return do_sched_setscheduler(pid, policy, param); | ||
5308 | } | ||
5309 | |||
5310 | /** | ||
5311 | * sys_sched_setparam - set/change the RT priority of a thread | ||
5312 | * @pid: the pid in question. | ||
5313 | * @param: structure containing the new RT priority. | ||
5314 | */ | ||
5315 | SYSCALL_DEFINE2(sched_setparam, pid_t, pid, struct sched_param __user *, param) | ||
5316 | { | ||
5317 | return do_sched_setscheduler(pid, -1, param); | ||
5318 | } | ||
5319 | |||
5320 | /** | ||
5321 | * sys_sched_getscheduler - get the policy (scheduling class) of a thread | ||
5322 | * @pid: the pid in question. | ||
5323 | */ | ||
5324 | SYSCALL_DEFINE1(sched_getscheduler, pid_t, pid) | ||
5325 | { | ||
5326 | struct task_struct *p; | ||
5327 | int retval; | ||
5328 | |||
5329 | if (pid < 0) | ||
5330 | return -EINVAL; | ||
5331 | |||
5332 | retval = -ESRCH; | ||
5333 | rcu_read_lock(); | ||
5334 | p = find_process_by_pid(pid); | ||
5335 | if (p) { | ||
5336 | retval = security_task_getscheduler(p); | ||
5337 | if (!retval) | ||
5338 | retval = p->policy | ||
5339 | | (p->sched_reset_on_fork ? SCHED_RESET_ON_FORK : 0); | ||
5340 | } | ||
5341 | rcu_read_unlock(); | ||
5342 | return retval; | ||
5343 | } | ||
5344 | |||
5345 | /** | ||
5346 | * sys_sched_getparam - get the RT priority of a thread | ||
5347 | * @pid: the pid in question. | ||
5348 | * @param: structure containing the RT priority. | ||
5349 | */ | ||
5350 | SYSCALL_DEFINE2(sched_getparam, pid_t, pid, struct sched_param __user *, param) | ||
5351 | { | ||
5352 | struct sched_param lp; | ||
5353 | struct task_struct *p; | ||
5354 | int retval; | ||
5355 | |||
5356 | if (!param || pid < 0) | ||
5357 | return -EINVAL; | ||
5358 | |||
5359 | rcu_read_lock(); | ||
5360 | p = find_process_by_pid(pid); | ||
5361 | retval = -ESRCH; | ||
5362 | if (!p) | ||
5363 | goto out_unlock; | ||
5364 | |||
5365 | retval = security_task_getscheduler(p); | ||
5366 | if (retval) | ||
5367 | goto out_unlock; | ||
5368 | |||
5369 | lp.sched_priority = p->rt_priority; | ||
5370 | rcu_read_unlock(); | ||
5371 | |||
5372 | /* | ||
5373 | * This one might sleep, we cannot do it with a spinlock held ... | ||
5374 | */ | ||
5375 | retval = copy_to_user(param, &lp, sizeof(*param)) ? -EFAULT : 0; | ||
5376 | |||
5377 | return retval; | ||
5378 | |||
5379 | out_unlock: | ||
5380 | rcu_read_unlock(); | ||
5381 | return retval; | ||
5382 | } | ||
5383 | |||
5384 | long sched_setaffinity(pid_t pid, const struct cpumask *in_mask) | ||
5385 | { | ||
5386 | cpumask_var_t cpus_allowed, new_mask; | ||
5387 | struct task_struct *p; | ||
5388 | int retval; | ||
5389 | |||
5390 | get_online_cpus(); | ||
5391 | rcu_read_lock(); | ||
5392 | |||
5393 | p = find_process_by_pid(pid); | ||
5394 | if (!p) { | ||
5395 | rcu_read_unlock(); | ||
5396 | put_online_cpus(); | ||
5397 | return -ESRCH; | ||
5398 | } | ||
5399 | |||
5400 | /* Prevent p going away */ | ||
5401 | get_task_struct(p); | ||
5402 | rcu_read_unlock(); | ||
5403 | |||
5404 | if (!alloc_cpumask_var(&cpus_allowed, GFP_KERNEL)) { | ||
5405 | retval = -ENOMEM; | ||
5406 | goto out_put_task; | ||
5407 | } | ||
5408 | if (!alloc_cpumask_var(&new_mask, GFP_KERNEL)) { | ||
5409 | retval = -ENOMEM; | ||
5410 | goto out_free_cpus_allowed; | ||
5411 | } | ||
5412 | retval = -EPERM; | ||
5413 | if (!check_same_owner(p) && !task_ns_capable(p, CAP_SYS_NICE)) | ||
5414 | goto out_unlock; | ||
5415 | |||
5416 | retval = security_task_setscheduler(p); | ||
5417 | if (retval) | ||
5418 | goto out_unlock; | ||
5419 | |||
5420 | cpuset_cpus_allowed(p, cpus_allowed); | ||
5421 | cpumask_and(new_mask, in_mask, cpus_allowed); | ||
5422 | again: | ||
5423 | retval = set_cpus_allowed_ptr(p, new_mask); | ||
5424 | |||
5425 | if (!retval) { | ||
5426 | cpuset_cpus_allowed(p, cpus_allowed); | ||
5427 | if (!cpumask_subset(new_mask, cpus_allowed)) { | ||
5428 | /* | ||
5429 | * We must have raced with a concurrent cpuset | ||
5430 | * update. Just reset the cpus_allowed to the | ||
5431 | * cpuset's cpus_allowed | ||
5432 | */ | ||
5433 | cpumask_copy(new_mask, cpus_allowed); | ||
5434 | goto again; | ||
5435 | } | ||
5436 | } | ||
5437 | out_unlock: | ||
5438 | free_cpumask_var(new_mask); | ||
5439 | out_free_cpus_allowed: | ||
5440 | free_cpumask_var(cpus_allowed); | ||
5441 | out_put_task: | ||
5442 | put_task_struct(p); | ||
5443 | put_online_cpus(); | ||
5444 | return retval; | ||
5445 | } | ||
5446 | |||
5447 | static int get_user_cpu_mask(unsigned long __user *user_mask_ptr, unsigned len, | ||
5448 | struct cpumask *new_mask) | ||
5449 | { | ||
5450 | if (len < cpumask_size()) | ||
5451 | cpumask_clear(new_mask); | ||
5452 | else if (len > cpumask_size()) | ||
5453 | len = cpumask_size(); | ||
5454 | |||
5455 | return copy_from_user(new_mask, user_mask_ptr, len) ? -EFAULT : 0; | ||
5456 | } | ||
5457 | |||
5458 | /** | ||
5459 | * sys_sched_setaffinity - set the cpu affinity of a process | ||
5460 | * @pid: pid of the process | ||
5461 | * @len: length in bytes of the bitmask pointed to by user_mask_ptr | ||
5462 | * @user_mask_ptr: user-space pointer to the new cpu mask | ||
5463 | */ | ||
5464 | SYSCALL_DEFINE3(sched_setaffinity, pid_t, pid, unsigned int, len, | ||
5465 | unsigned long __user *, user_mask_ptr) | ||
5466 | { | ||
5467 | cpumask_var_t new_mask; | ||
5468 | int retval; | ||
5469 | |||
5470 | if (!alloc_cpumask_var(&new_mask, GFP_KERNEL)) | ||
5471 | return -ENOMEM; | ||
5472 | |||
5473 | retval = get_user_cpu_mask(user_mask_ptr, len, new_mask); | ||
5474 | if (retval == 0) | ||
5475 | retval = sched_setaffinity(pid, new_mask); | ||
5476 | free_cpumask_var(new_mask); | ||
5477 | return retval; | ||
5478 | } | ||
5479 | |||
5480 | long sched_getaffinity(pid_t pid, struct cpumask *mask) | ||
5481 | { | ||
5482 | struct task_struct *p; | ||
5483 | unsigned long flags; | ||
5484 | int retval; | ||
5485 | |||
5486 | get_online_cpus(); | ||
5487 | rcu_read_lock(); | ||
5488 | |||
5489 | retval = -ESRCH; | ||
5490 | p = find_process_by_pid(pid); | ||
5491 | if (!p) | ||
5492 | goto out_unlock; | ||
5493 | |||
5494 | retval = security_task_getscheduler(p); | ||
5495 | if (retval) | ||
5496 | goto out_unlock; | ||
5497 | |||
5498 | raw_spin_lock_irqsave(&p->pi_lock, flags); | ||
5499 | cpumask_and(mask, &p->cpus_allowed, cpu_online_mask); | ||
5500 | raw_spin_unlock_irqrestore(&p->pi_lock, flags); | ||
5501 | |||
5502 | out_unlock: | ||
5503 | rcu_read_unlock(); | ||
5504 | put_online_cpus(); | ||
5505 | |||
5506 | return retval; | ||
5507 | } | ||
5508 | |||
5509 | /** | ||
5510 | * sys_sched_getaffinity - get the cpu affinity of a process | ||
5511 | * @pid: pid of the process | ||
5512 | * @len: length in bytes of the bitmask pointed to by user_mask_ptr | ||
5513 | * @user_mask_ptr: user-space pointer to hold the current cpu mask | ||
5514 | */ | ||
5515 | SYSCALL_DEFINE3(sched_getaffinity, pid_t, pid, unsigned int, len, | ||
5516 | unsigned long __user *, user_mask_ptr) | ||
5517 | { | ||
5518 | int ret; | ||
5519 | cpumask_var_t mask; | ||
5520 | |||
5521 | if ((len * BITS_PER_BYTE) < nr_cpu_ids) | ||
5522 | return -EINVAL; | ||
5523 | if (len & (sizeof(unsigned long)-1)) | ||
5524 | return -EINVAL; | ||
5525 | |||
5526 | if (!alloc_cpumask_var(&mask, GFP_KERNEL)) | ||
5527 | return -ENOMEM; | ||
5528 | |||
5529 | ret = sched_getaffinity(pid, mask); | ||
5530 | if (ret == 0) { | ||
5531 | size_t retlen = min_t(size_t, len, cpumask_size()); | ||
5532 | |||
5533 | if (copy_to_user(user_mask_ptr, mask, retlen)) | ||
5534 | ret = -EFAULT; | ||
5535 | else | ||
5536 | ret = retlen; | ||
5537 | } | ||
5538 | free_cpumask_var(mask); | ||
5539 | |||
5540 | return ret; | ||
5541 | } | ||
5542 | |||
5543 | /** | ||
5544 | * sys_sched_yield - yield the current processor to other threads. | ||
5545 | * | ||
5546 | * This function yields the current CPU to other tasks. If there are no | ||
5547 | * other threads running on this CPU then this function will return. | ||
5548 | */ | ||
5549 | SYSCALL_DEFINE0(sched_yield) | ||
5550 | { | ||
5551 | struct rq *rq = this_rq_lock(); | ||
5552 | |||
5553 | schedstat_inc(rq, yld_count); | ||
5554 | current->sched_class->yield_task(rq); | ||
5555 | |||
5556 | /* | ||
5557 | * Since we are going to call schedule() anyway, there's | ||
5558 | * no need to preempt or enable interrupts: | ||
5559 | */ | ||
5560 | __release(rq->lock); | ||
5561 | spin_release(&rq->lock.dep_map, 1, _THIS_IP_); | ||
5562 | do_raw_spin_unlock(&rq->lock); | ||
5563 | preempt_enable_no_resched(); | ||
5564 | |||
5565 | schedule(); | ||
5566 | |||
5567 | return 0; | ||
5568 | } | ||
5569 | |||
5570 | static inline int should_resched(void) | ||
5571 | { | ||
5572 | return need_resched() && !(preempt_count() & PREEMPT_ACTIVE); | ||
5573 | } | ||
5574 | |||
5575 | static void __cond_resched(void) | ||
5576 | { | ||
5577 | add_preempt_count(PREEMPT_ACTIVE); | ||
5578 | __schedule(); | ||
5579 | sub_preempt_count(PREEMPT_ACTIVE); | ||
5580 | } | ||
5581 | |||
5582 | int __sched _cond_resched(void) | ||
5583 | { | ||
5584 | if (should_resched()) { | ||
5585 | __cond_resched(); | ||
5586 | return 1; | ||
5587 | } | ||
5588 | return 0; | ||
5589 | } | ||
5590 | EXPORT_SYMBOL(_cond_resched); | ||
5591 | |||
5592 | /* | ||
5593 | * __cond_resched_lock() - if a reschedule is pending, drop the given lock, | ||
5594 | * call schedule, and on return reacquire the lock. | ||
5595 | * | ||
5596 | * This works OK both with and without CONFIG_PREEMPT. We do strange low-level | ||
5597 | * operations here to prevent schedule() from being called twice (once via | ||
5598 | * spin_unlock(), once by hand). | ||
5599 | */ | ||
5600 | int __cond_resched_lock(spinlock_t *lock) | ||
5601 | { | ||
5602 | int resched = should_resched(); | ||
5603 | int ret = 0; | ||
5604 | |||
5605 | lockdep_assert_held(lock); | ||
5606 | |||
5607 | if (spin_needbreak(lock) || resched) { | ||
5608 | spin_unlock(lock); | ||
5609 | if (resched) | ||
5610 | __cond_resched(); | ||
5611 | else | ||
5612 | cpu_relax(); | ||
5613 | ret = 1; | ||
5614 | spin_lock(lock); | ||
5615 | } | ||
5616 | return ret; | ||
5617 | } | ||
5618 | EXPORT_SYMBOL(__cond_resched_lock); | ||
5619 | |||
5620 | int __sched __cond_resched_softirq(void) | ||
5621 | { | ||
5622 | BUG_ON(!in_softirq()); | ||
5623 | |||
5624 | if (should_resched()) { | ||
5625 | local_bh_enable(); | ||
5626 | __cond_resched(); | ||
5627 | local_bh_disable(); | ||
5628 | return 1; | ||
5629 | } | ||
5630 | return 0; | ||
5631 | } | ||
5632 | EXPORT_SYMBOL(__cond_resched_softirq); | ||
5633 | |||
5634 | /** | ||
5635 | * yield - yield the current processor to other threads. | ||
5636 | * | ||
5637 | * This is a shortcut for kernel-space yielding - it marks the | ||
5638 | * thread runnable and calls sys_sched_yield(). | ||
5639 | */ | ||
5640 | void __sched yield(void) | ||
5641 | { | ||
5642 | set_current_state(TASK_RUNNING); | ||
5643 | sys_sched_yield(); | ||
5644 | } | ||
5645 | EXPORT_SYMBOL(yield); | ||
5646 | |||
5647 | /** | ||
5648 | * yield_to - yield the current processor to another thread in | ||
5649 | * your thread group, or accelerate that thread toward the | ||
5650 | * processor it's on. | ||
5651 | * @p: target task | ||
5652 | * @preempt: whether task preemption is allowed or not | ||
5653 | * | ||
5654 | * It's the caller's job to ensure that the target task struct | ||
5655 | * can't go away on us before we can do any checks. | ||
5656 | * | ||
5657 | * Returns true if we indeed boosted the target task. | ||
5658 | */ | ||
5659 | bool __sched yield_to(struct task_struct *p, bool preempt) | ||
5660 | { | ||
5661 | struct task_struct *curr = current; | ||
5662 | struct rq *rq, *p_rq; | ||
5663 | unsigned long flags; | ||
5664 | bool yielded = 0; | ||
5665 | |||
5666 | local_irq_save(flags); | ||
5667 | rq = this_rq(); | ||
5668 | |||
5669 | again: | ||
5670 | p_rq = task_rq(p); | ||
5671 | double_rq_lock(rq, p_rq); | ||
5672 | while (task_rq(p) != p_rq) { | ||
5673 | double_rq_unlock(rq, p_rq); | ||
5674 | goto again; | ||
5675 | } | ||
5676 | |||
5677 | if (!curr->sched_class->yield_to_task) | ||
5678 | goto out; | ||
5679 | |||
5680 | if (curr->sched_class != p->sched_class) | ||
5681 | goto out; | ||
5682 | |||
5683 | if (task_running(p_rq, p) || p->state) | ||
5684 | goto out; | ||
5685 | |||
5686 | yielded = curr->sched_class->yield_to_task(rq, p, preempt); | ||
5687 | if (yielded) { | ||
5688 | schedstat_inc(rq, yld_count); | ||
5689 | /* | ||
5690 | * Make p's CPU reschedule; pick_next_entity takes care of | ||
5691 | * fairness. | ||
5692 | */ | ||
5693 | if (preempt && rq != p_rq) | ||
5694 | resched_task(p_rq->curr); | ||
5695 | } | ||
5696 | |||
5697 | out: | ||
5698 | double_rq_unlock(rq, p_rq); | ||
5699 | local_irq_restore(flags); | ||
5700 | |||
5701 | if (yielded) | ||
5702 | schedule(); | ||
5703 | |||
5704 | return yielded; | ||
5705 | } | ||
5706 | EXPORT_SYMBOL_GPL(yield_to); | ||
5707 | |||
5708 | /* | ||
5709 | * This task is about to go to sleep on IO. Increment rq->nr_iowait so | ||
5710 | * that process accounting knows that this is a task in IO wait state. | ||
5711 | */ | ||
5712 | void __sched io_schedule(void) | ||
5713 | { | ||
5714 | struct rq *rq = raw_rq(); | ||
5715 | |||
5716 | delayacct_blkio_start(); | ||
5717 | atomic_inc(&rq->nr_iowait); | ||
5718 | blk_flush_plug(current); | ||
5719 | current->in_iowait = 1; | ||
5720 | schedule(); | ||
5721 | current->in_iowait = 0; | ||
5722 | atomic_dec(&rq->nr_iowait); | ||
5723 | delayacct_blkio_end(); | ||
5724 | } | ||
5725 | EXPORT_SYMBOL(io_schedule); | ||
5726 | |||
5727 | long __sched io_schedule_timeout(long timeout) | ||
5728 | { | ||
5729 | struct rq *rq = raw_rq(); | ||
5730 | long ret; | ||
5731 | |||
5732 | delayacct_blkio_start(); | ||
5733 | atomic_inc(&rq->nr_iowait); | ||
5734 | blk_flush_plug(current); | ||
5735 | current->in_iowait = 1; | ||
5736 | ret = schedule_timeout(timeout); | ||
5737 | current->in_iowait = 0; | ||
5738 | atomic_dec(&rq->nr_iowait); | ||
5739 | delayacct_blkio_end(); | ||
5740 | return ret; | ||
5741 | } | ||
5742 | |||
5743 | /** | ||
5744 | * sys_sched_get_priority_max - return maximum RT priority. | ||
5745 | * @policy: scheduling class. | ||
5746 | * | ||
5747 | * this syscall returns the maximum rt_priority that can be used | ||
5748 | * by a given scheduling class. | ||
5749 | */ | ||
5750 | SYSCALL_DEFINE1(sched_get_priority_max, int, policy) | ||
5751 | { | ||
5752 | int ret = -EINVAL; | ||
5753 | |||
5754 | switch (policy) { | ||
5755 | case SCHED_FIFO: | ||
5756 | case SCHED_RR: | ||
5757 | ret = MAX_USER_RT_PRIO-1; | ||
5758 | break; | ||
5759 | case SCHED_NORMAL: | ||
5760 | case SCHED_BATCH: | ||
5761 | case SCHED_IDLE: | ||
5762 | ret = 0; | ||
5763 | break; | ||
5764 | } | ||
5765 | return ret; | ||
5766 | } | ||
5767 | |||
5768 | /** | ||
5769 | * sys_sched_get_priority_min - return minimum RT priority. | ||
5770 | * @policy: scheduling class. | ||
5771 | * | ||
5772 | * this syscall returns the minimum rt_priority that can be used | ||
5773 | * by a given scheduling class. | ||
5774 | */ | ||
5775 | SYSCALL_DEFINE1(sched_get_priority_min, int, policy) | ||
5776 | { | ||
5777 | int ret = -EINVAL; | ||
5778 | |||
5779 | switch (policy) { | ||
5780 | case SCHED_FIFO: | ||
5781 | case SCHED_RR: | ||
5782 | ret = 1; | ||
5783 | break; | ||
5784 | case SCHED_NORMAL: | ||
5785 | case SCHED_BATCH: | ||
5786 | case SCHED_IDLE: | ||
5787 | ret = 0; | ||
5788 | } | ||
5789 | return ret; | ||
5790 | } | ||
5791 | |||
5792 | /** | ||
5793 | * sys_sched_rr_get_interval - return the default timeslice of a process. | ||
5794 | * @pid: pid of the process. | ||
5795 | * @interval: userspace pointer to the timeslice value. | ||
5796 | * | ||
5797 | * this syscall writes the default timeslice value of a given process | ||
5798 | * into the user-space timespec buffer. A value of '0' means infinity. | ||
5799 | */ | ||
5800 | SYSCALL_DEFINE2(sched_rr_get_interval, pid_t, pid, | ||
5801 | struct timespec __user *, interval) | ||
5802 | { | ||
5803 | struct task_struct *p; | ||
5804 | unsigned int time_slice; | ||
5805 | unsigned long flags; | ||
5806 | struct rq *rq; | ||
5807 | int retval; | ||
5808 | struct timespec t; | ||
5809 | |||
5810 | if (pid < 0) | ||
5811 | return -EINVAL; | ||
5812 | |||
5813 | retval = -ESRCH; | ||
5814 | rcu_read_lock(); | ||
5815 | p = find_process_by_pid(pid); | ||
5816 | if (!p) | ||
5817 | goto out_unlock; | ||
5818 | |||
5819 | retval = security_task_getscheduler(p); | ||
5820 | if (retval) | ||
5821 | goto out_unlock; | ||
5822 | |||
5823 | rq = task_rq_lock(p, &flags); | ||
5824 | time_slice = p->sched_class->get_rr_interval(rq, p); | ||
5825 | task_rq_unlock(rq, p, &flags); | ||
5826 | |||
5827 | rcu_read_unlock(); | ||
5828 | jiffies_to_timespec(time_slice, &t); | ||
5829 | retval = copy_to_user(interval, &t, sizeof(t)) ? -EFAULT : 0; | ||
5830 | return retval; | ||
5831 | |||
5832 | out_unlock: | ||
5833 | rcu_read_unlock(); | ||
5834 | return retval; | ||
5835 | } | ||
5836 | |||
5837 | static const char stat_nam[] = TASK_STATE_TO_CHAR_STR; | ||
5838 | |||
5839 | void sched_show_task(struct task_struct *p) | ||
5840 | { | ||
5841 | unsigned long free = 0; | ||
5842 | unsigned state; | ||
5843 | |||
5844 | state = p->state ? __ffs(p->state) + 1 : 0; | ||
5845 | printk(KERN_INFO "%-15.15s %c", p->comm, | ||
5846 | state < sizeof(stat_nam) - 1 ? stat_nam[state] : '?'); | ||
5847 | #if BITS_PER_LONG == 32 | ||
5848 | if (state == TASK_RUNNING) | ||
5849 | printk(KERN_CONT " running "); | ||
5850 | else | ||
5851 | printk(KERN_CONT " %08lx ", thread_saved_pc(p)); | ||
5852 | #else | ||
5853 | if (state == TASK_RUNNING) | ||
5854 | printk(KERN_CONT " running task "); | ||
5855 | else | ||
5856 | printk(KERN_CONT " %016lx ", thread_saved_pc(p)); | ||
5857 | #endif | ||
5858 | #ifdef CONFIG_DEBUG_STACK_USAGE | ||
5859 | free = stack_not_used(p); | ||
5860 | #endif | ||
5861 | printk(KERN_CONT "%5lu %5d %6d 0x%08lx\n", free, | ||
5862 | task_pid_nr(p), task_pid_nr(p->real_parent), | ||
5863 | (unsigned long)task_thread_info(p)->flags); | ||
5864 | |||
5865 | show_stack(p, NULL); | ||
5866 | } | ||
5867 | |||
5868 | void show_state_filter(unsigned long state_filter) | ||
5869 | { | ||
5870 | struct task_struct *g, *p; | ||
5871 | |||
5872 | #if BITS_PER_LONG == 32 | ||
5873 | printk(KERN_INFO | ||
5874 | " task PC stack pid father\n"); | ||
5875 | #else | ||
5876 | printk(KERN_INFO | ||
5877 | " task PC stack pid father\n"); | ||
5878 | #endif | ||
5879 | read_lock(&tasklist_lock); | ||
5880 | do_each_thread(g, p) { | ||
5881 | /* | ||
5882 | * reset the NMI-timeout, listing all files on a slow | ||
5883 | * console might take a lot of time: | ||
5884 | */ | ||
5885 | touch_nmi_watchdog(); | ||
5886 | if (!state_filter || (p->state & state_filter)) | ||
5887 | sched_show_task(p); | ||
5888 | } while_each_thread(g, p); | ||
5889 | |||
5890 | touch_all_softlockup_watchdogs(); | ||
5891 | |||
5892 | #ifdef CONFIG_SCHED_DEBUG | ||
5893 | sysrq_sched_debug_show(); | ||
5894 | #endif | ||
5895 | read_unlock(&tasklist_lock); | ||
5896 | /* | ||
5897 | * Only show locks if all tasks are dumped: | ||
5898 | */ | ||
5899 | if (!state_filter) | ||
5900 | debug_show_all_locks(); | ||
5901 | } | ||
5902 | |||
5903 | void __cpuinit init_idle_bootup_task(struct task_struct *idle) | ||
5904 | { | ||
5905 | idle->sched_class = &idle_sched_class; | ||
5906 | } | ||
5907 | |||
5908 | /** | ||
5909 | * init_idle - set up an idle thread for a given CPU | ||
5910 | * @idle: task in question | ||
5911 | * @cpu: cpu the idle task belongs to | ||
5912 | * | ||
5913 | * NOTE: this function does not set the idle thread's NEED_RESCHED | ||
5914 | * flag, to make booting more robust. | ||
5915 | */ | ||
5916 | void __cpuinit init_idle(struct task_struct *idle, int cpu) | ||
5917 | { | ||
5918 | struct rq *rq = cpu_rq(cpu); | ||
5919 | unsigned long flags; | ||
5920 | |||
5921 | raw_spin_lock_irqsave(&rq->lock, flags); | ||
5922 | |||
5923 | __sched_fork(idle); | ||
5924 | idle->state = TASK_RUNNING; | ||
5925 | idle->se.exec_start = sched_clock(); | ||
5926 | |||
5927 | do_set_cpus_allowed(idle, cpumask_of(cpu)); | ||
5928 | /* | ||
5929 | * We're having a chicken and egg problem, even though we are | ||
5930 | * holding rq->lock, the cpu isn't yet set to this cpu so the | ||
5931 | * lockdep check in task_group() will fail. | ||
5932 | * | ||
5933 | * Similar case to sched_fork(). / Alternatively we could | ||
5934 | * use task_rq_lock() here and obtain the other rq->lock. | ||
5935 | * | ||
5936 | * Silence PROVE_RCU | ||
5937 | */ | ||
5938 | rcu_read_lock(); | ||
5939 | __set_task_cpu(idle, cpu); | ||
5940 | rcu_read_unlock(); | ||
5941 | |||
5942 | rq->curr = rq->idle = idle; | ||
5943 | #if defined(CONFIG_SMP) | ||
5944 | idle->on_cpu = 1; | ||
5945 | #endif | ||
5946 | raw_spin_unlock_irqrestore(&rq->lock, flags); | ||
5947 | |||
5948 | /* Set the preempt count _outside_ the spinlocks! */ | ||
5949 | task_thread_info(idle)->preempt_count = 0; | ||
5950 | |||
5951 | /* | ||
5952 | * The idle tasks have their own, simple scheduling class: | ||
5953 | */ | ||
5954 | idle->sched_class = &idle_sched_class; | ||
5955 | ftrace_graph_init_idle_task(idle, cpu); | ||
5956 | } | ||
5957 | |||
5958 | /* | ||
5959 | * In a system that switches off the HZ timer nohz_cpu_mask | ||
5960 | * indicates which cpus entered this state. This is used | ||
5961 | * in the rcu update to wait only for active cpus. For system | ||
5962 | * which do not switch off the HZ timer nohz_cpu_mask should | ||
5963 | * always be CPU_BITS_NONE. | ||
5964 | */ | ||
5965 | cpumask_var_t nohz_cpu_mask; | ||
5966 | |||
5967 | /* | ||
5968 | * Increase the granularity value when there are more CPUs, | ||
5969 | * because with more CPUs the 'effective latency' as visible | ||
5970 | * to users decreases. But the relationship is not linear, | ||
5971 | * so pick a second-best guess by going with the log2 of the | ||
5972 | * number of CPUs. | ||
5973 | * | ||
5974 | * This idea comes from the SD scheduler of Con Kolivas: | ||
5975 | */ | ||
5976 | static int get_update_sysctl_factor(void) | ||
5977 | { | ||
5978 | unsigned int cpus = min_t(int, num_online_cpus(), 8); | ||
5979 | unsigned int factor; | ||
5980 | |||
5981 | switch (sysctl_sched_tunable_scaling) { | ||
5982 | case SCHED_TUNABLESCALING_NONE: | ||
5983 | factor = 1; | ||
5984 | break; | ||
5985 | case SCHED_TUNABLESCALING_LINEAR: | ||
5986 | factor = cpus; | ||
5987 | break; | ||
5988 | case SCHED_TUNABLESCALING_LOG: | ||
5989 | default: | ||
5990 | factor = 1 + ilog2(cpus); | ||
5991 | break; | ||
5992 | } | ||
5993 | |||
5994 | return factor; | ||
5995 | } | ||
5996 | |||
5997 | static void update_sysctl(void) | ||
5998 | { | ||
5999 | unsigned int factor = get_update_sysctl_factor(); | ||
6000 | |||
6001 | #define SET_SYSCTL(name) \ | ||
6002 | (sysctl_##name = (factor) * normalized_sysctl_##name) | ||
6003 | SET_SYSCTL(sched_min_granularity); | ||
6004 | SET_SYSCTL(sched_latency); | ||
6005 | SET_SYSCTL(sched_wakeup_granularity); | ||
6006 | #undef SET_SYSCTL | ||
6007 | } | ||
6008 | |||
6009 | static inline void sched_init_granularity(void) | ||
6010 | { | ||
6011 | update_sysctl(); | ||
6012 | } | ||
6013 | |||
6014 | #ifdef CONFIG_SMP | ||
6015 | void do_set_cpus_allowed(struct task_struct *p, const struct cpumask *new_mask) | ||
6016 | { | ||
6017 | if (p->sched_class && p->sched_class->set_cpus_allowed) | ||
6018 | p->sched_class->set_cpus_allowed(p, new_mask); | ||
6019 | else { | ||
6020 | cpumask_copy(&p->cpus_allowed, new_mask); | ||
6021 | p->rt.nr_cpus_allowed = cpumask_weight(new_mask); | ||
6022 | } | ||
6023 | } | ||
6024 | |||
6025 | /* | ||
6026 | * This is how migration works: | ||
6027 | * | ||
6028 | * 1) we invoke migration_cpu_stop() on the target CPU using | ||
6029 | * stop_one_cpu(). | ||
6030 | * 2) stopper starts to run (implicitly forcing the migrated thread | ||
6031 | * off the CPU) | ||
6032 | * 3) it checks whether the migrated task is still in the wrong runqueue. | ||
6033 | * 4) if it's in the wrong runqueue then the migration thread removes | ||
6034 | * it and puts it into the right queue. | ||
6035 | * 5) stopper completes and stop_one_cpu() returns and the migration | ||
6036 | * is done. | ||
6037 | */ | ||
6038 | |||
6039 | /* | ||
6040 | * Change a given task's CPU affinity. Migrate the thread to a | ||
6041 | * proper CPU and schedule it away if the CPU it's executing on | ||
6042 | * is removed from the allowed bitmask. | ||
6043 | * | ||
6044 | * NOTE: the caller must have a valid reference to the task, the | ||
6045 | * task must not exit() & deallocate itself prematurely. The | ||
6046 | * call is not atomic; no spinlocks may be held. | ||
6047 | */ | ||
6048 | int set_cpus_allowed_ptr(struct task_struct *p, const struct cpumask *new_mask) | ||
6049 | { | ||
6050 | unsigned long flags; | ||
6051 | struct rq *rq; | ||
6052 | unsigned int dest_cpu; | ||
6053 | int ret = 0; | ||
6054 | |||
6055 | rq = task_rq_lock(p, &flags); | ||
6056 | |||
6057 | if (cpumask_equal(&p->cpus_allowed, new_mask)) | ||
6058 | goto out; | ||
6059 | |||
6060 | if (!cpumask_intersects(new_mask, cpu_active_mask)) { | ||
6061 | ret = -EINVAL; | ||
6062 | goto out; | ||
6063 | } | ||
6064 | |||
6065 | if (unlikely((p->flags & PF_THREAD_BOUND) && p != current)) { | ||
6066 | ret = -EINVAL; | ||
6067 | goto out; | ||
6068 | } | ||
6069 | |||
6070 | do_set_cpus_allowed(p, new_mask); | ||
6071 | |||
6072 | /* Can the task run on the task's current CPU? If so, we're done */ | ||
6073 | if (cpumask_test_cpu(task_cpu(p), new_mask)) | ||
6074 | goto out; | ||
6075 | |||
6076 | dest_cpu = cpumask_any_and(cpu_active_mask, new_mask); | ||
6077 | if (p->on_rq) { | ||
6078 | struct migration_arg arg = { p, dest_cpu }; | ||
6079 | /* Need help from migration thread: drop lock and wait. */ | ||
6080 | task_rq_unlock(rq, p, &flags); | ||
6081 | stop_one_cpu(cpu_of(rq), migration_cpu_stop, &arg); | ||
6082 | tlb_migrate_finish(p->mm); | ||
6083 | return 0; | ||
6084 | } | ||
6085 | out: | ||
6086 | task_rq_unlock(rq, p, &flags); | ||
6087 | |||
6088 | return ret; | ||
6089 | } | ||
6090 | EXPORT_SYMBOL_GPL(set_cpus_allowed_ptr); | ||
6091 | |||
6092 | /* | ||
6093 | * Move (not current) task off this cpu, onto dest cpu. We're doing | ||
6094 | * this because either it can't run here any more (set_cpus_allowed() | ||
6095 | * away from this CPU, or CPU going down), or because we're | ||
6096 | * attempting to rebalance this task on exec (sched_exec). | ||
6097 | * | ||
6098 | * So we race with normal scheduler movements, but that's OK, as long | ||
6099 | * as the task is no longer on this CPU. | ||
6100 | * | ||
6101 | * Returns non-zero if task was successfully migrated. | ||
6102 | */ | ||
6103 | static int __migrate_task(struct task_struct *p, int src_cpu, int dest_cpu) | ||
6104 | { | ||
6105 | struct rq *rq_dest, *rq_src; | ||
6106 | int ret = 0; | ||
6107 | |||
6108 | if (unlikely(!cpu_active(dest_cpu))) | ||
6109 | return ret; | ||
6110 | |||
6111 | rq_src = cpu_rq(src_cpu); | ||
6112 | rq_dest = cpu_rq(dest_cpu); | ||
6113 | |||
6114 | raw_spin_lock(&p->pi_lock); | ||
6115 | double_rq_lock(rq_src, rq_dest); | ||
6116 | /* Already moved. */ | ||
6117 | if (task_cpu(p) != src_cpu) | ||
6118 | goto done; | ||
6119 | /* Affinity changed (again). */ | ||
6120 | if (!cpumask_test_cpu(dest_cpu, &p->cpus_allowed)) | ||
6121 | goto fail; | ||
6122 | |||
6123 | /* | ||
6124 | * If we're not on a rq, the next wake-up will ensure we're | ||
6125 | * placed properly. | ||
6126 | */ | ||
6127 | if (p->on_rq) { | ||
6128 | deactivate_task(rq_src, p, 0); | ||
6129 | set_task_cpu(p, dest_cpu); | ||
6130 | activate_task(rq_dest, p, 0); | ||
6131 | check_preempt_curr(rq_dest, p, 0); | ||
6132 | } | ||
6133 | done: | ||
6134 | ret = 1; | ||
6135 | fail: | ||
6136 | double_rq_unlock(rq_src, rq_dest); | ||
6137 | raw_spin_unlock(&p->pi_lock); | ||
6138 | return ret; | ||
6139 | } | ||
6140 | |||
6141 | /* | ||
6142 | * migration_cpu_stop - this will be executed by a highprio stopper thread | ||
6143 | * and performs thread migration by bumping thread off CPU then | ||
6144 | * 'pushing' onto another runqueue. | ||
6145 | */ | ||
6146 | static int migration_cpu_stop(void *data) | ||
6147 | { | ||
6148 | struct migration_arg *arg = data; | ||
6149 | |||
6150 | /* | ||
6151 | * The original target cpu might have gone down and we might | ||
6152 | * be on another cpu but it doesn't matter. | ||
6153 | */ | ||
6154 | local_irq_disable(); | ||
6155 | __migrate_task(arg->task, raw_smp_processor_id(), arg->dest_cpu); | ||
6156 | local_irq_enable(); | ||
6157 | return 0; | ||
6158 | } | ||
6159 | |||
6160 | #ifdef CONFIG_HOTPLUG_CPU | ||
6161 | |||
6162 | /* | ||
6163 | * Ensures that the idle task is using init_mm right before its cpu goes | ||
6164 | * offline. | ||
6165 | */ | ||
6166 | void idle_task_exit(void) | ||
6167 | { | ||
6168 | struct mm_struct *mm = current->active_mm; | ||
6169 | |||
6170 | BUG_ON(cpu_online(smp_processor_id())); | ||
6171 | |||
6172 | if (mm != &init_mm) | ||
6173 | switch_mm(mm, &init_mm, current); | ||
6174 | mmdrop(mm); | ||
6175 | } | ||
6176 | |||
6177 | /* | ||
6178 | * While a dead CPU has no uninterruptible tasks queued at this point, | ||
6179 | * it might still have a nonzero ->nr_uninterruptible counter, because | ||
6180 | * for performance reasons the counter is not stricly tracking tasks to | ||
6181 | * their home CPUs. So we just add the counter to another CPU's counter, | ||
6182 | * to keep the global sum constant after CPU-down: | ||
6183 | */ | ||
6184 | static void migrate_nr_uninterruptible(struct rq *rq_src) | ||
6185 | { | ||
6186 | struct rq *rq_dest = cpu_rq(cpumask_any(cpu_active_mask)); | ||
6187 | |||
6188 | rq_dest->nr_uninterruptible += rq_src->nr_uninterruptible; | ||
6189 | rq_src->nr_uninterruptible = 0; | ||
6190 | } | ||
6191 | |||
6192 | /* | ||
6193 | * remove the tasks which were accounted by rq from calc_load_tasks. | ||
6194 | */ | ||
6195 | static void calc_global_load_remove(struct rq *rq) | ||
6196 | { | ||
6197 | atomic_long_sub(rq->calc_load_active, &calc_load_tasks); | ||
6198 | rq->calc_load_active = 0; | ||
6199 | } | ||
6200 | |||
6201 | /* | ||
6202 | * Migrate all tasks from the rq, sleeping tasks will be migrated by | ||
6203 | * try_to_wake_up()->select_task_rq(). | ||
6204 | * | ||
6205 | * Called with rq->lock held even though we'er in stop_machine() and | ||
6206 | * there's no concurrency possible, we hold the required locks anyway | ||
6207 | * because of lock validation efforts. | ||
6208 | */ | ||
6209 | static void migrate_tasks(unsigned int dead_cpu) | ||
6210 | { | ||
6211 | struct rq *rq = cpu_rq(dead_cpu); | ||
6212 | struct task_struct *next, *stop = rq->stop; | ||
6213 | int dest_cpu; | ||
6214 | |||
6215 | /* | ||
6216 | * Fudge the rq selection such that the below task selection loop | ||
6217 | * doesn't get stuck on the currently eligible stop task. | ||
6218 | * | ||
6219 | * We're currently inside stop_machine() and the rq is either stuck | ||
6220 | * in the stop_machine_cpu_stop() loop, or we're executing this code, | ||
6221 | * either way we should never end up calling schedule() until we're | ||
6222 | * done here. | ||
6223 | */ | ||
6224 | rq->stop = NULL; | ||
6225 | |||
6226 | for ( ; ; ) { | ||
6227 | /* | ||
6228 | * There's this thread running, bail when that's the only | ||
6229 | * remaining thread. | ||
6230 | */ | ||
6231 | if (rq->nr_running == 1) | ||
6232 | break; | ||
6233 | |||
6234 | next = pick_next_task(rq); | ||
6235 | BUG_ON(!next); | ||
6236 | next->sched_class->put_prev_task(rq, next); | ||
6237 | |||
6238 | /* Find suitable destination for @next, with force if needed. */ | ||
6239 | dest_cpu = select_fallback_rq(dead_cpu, next); | ||
6240 | raw_spin_unlock(&rq->lock); | ||
6241 | |||
6242 | __migrate_task(next, dead_cpu, dest_cpu); | ||
6243 | |||
6244 | raw_spin_lock(&rq->lock); | ||
6245 | } | ||
6246 | |||
6247 | rq->stop = stop; | ||
6248 | } | ||
6249 | |||
6250 | #endif /* CONFIG_HOTPLUG_CPU */ | ||
6251 | |||
6252 | #if defined(CONFIG_SCHED_DEBUG) && defined(CONFIG_SYSCTL) | ||
6253 | |||
6254 | static struct ctl_table sd_ctl_dir[] = { | ||
6255 | { | ||
6256 | .procname = "sched_domain", | ||
6257 | .mode = 0555, | ||
6258 | }, | ||
6259 | {} | ||
6260 | }; | ||
6261 | |||
6262 | static struct ctl_table sd_ctl_root[] = { | ||
6263 | { | ||
6264 | .procname = "kernel", | ||
6265 | .mode = 0555, | ||
6266 | .child = sd_ctl_dir, | ||
6267 | }, | ||
6268 | {} | ||
6269 | }; | ||
6270 | |||
6271 | static struct ctl_table *sd_alloc_ctl_entry(int n) | ||
6272 | { | ||
6273 | struct ctl_table *entry = | ||
6274 | kcalloc(n, sizeof(struct ctl_table), GFP_KERNEL); | ||
6275 | |||
6276 | return entry; | ||
6277 | } | ||
6278 | |||
6279 | static void sd_free_ctl_entry(struct ctl_table **tablep) | ||
6280 | { | ||
6281 | struct ctl_table *entry; | ||
6282 | |||
6283 | /* | ||
6284 | * In the intermediate directories, both the child directory and | ||
6285 | * procname are dynamically allocated and could fail but the mode | ||
6286 | * will always be set. In the lowest directory the names are | ||
6287 | * static strings and all have proc handlers. | ||
6288 | */ | ||
6289 | for (entry = *tablep; entry->mode; entry++) { | ||
6290 | if (entry->child) | ||
6291 | sd_free_ctl_entry(&entry->child); | ||
6292 | if (entry->proc_handler == NULL) | ||
6293 | kfree(entry->procname); | ||
6294 | } | ||
6295 | |||
6296 | kfree(*tablep); | ||
6297 | *tablep = NULL; | ||
6298 | } | ||
6299 | |||
6300 | static void | ||
6301 | set_table_entry(struct ctl_table *entry, | ||
6302 | const char *procname, void *data, int maxlen, | ||
6303 | mode_t mode, proc_handler *proc_handler) | ||
6304 | { | ||
6305 | entry->procname = procname; | ||
6306 | entry->data = data; | ||
6307 | entry->maxlen = maxlen; | ||
6308 | entry->mode = mode; | ||
6309 | entry->proc_handler = proc_handler; | ||
6310 | } | ||
6311 | |||
6312 | static struct ctl_table * | ||
6313 | sd_alloc_ctl_domain_table(struct sched_domain *sd) | ||
6314 | { | ||
6315 | struct ctl_table *table = sd_alloc_ctl_entry(13); | ||
6316 | |||
6317 | if (table == NULL) | ||
6318 | return NULL; | ||
6319 | |||
6320 | set_table_entry(&table[0], "min_interval", &sd->min_interval, | ||
6321 | sizeof(long), 0644, proc_doulongvec_minmax); | ||
6322 | set_table_entry(&table[1], "max_interval", &sd->max_interval, | ||
6323 | sizeof(long), 0644, proc_doulongvec_minmax); | ||
6324 | set_table_entry(&table[2], "busy_idx", &sd->busy_idx, | ||
6325 | sizeof(int), 0644, proc_dointvec_minmax); | ||
6326 | set_table_entry(&table[3], "idle_idx", &sd->idle_idx, | ||
6327 | sizeof(int), 0644, proc_dointvec_minmax); | ||
6328 | set_table_entry(&table[4], "newidle_idx", &sd->newidle_idx, | ||
6329 | sizeof(int), 0644, proc_dointvec_minmax); | ||
6330 | set_table_entry(&table[5], "wake_idx", &sd->wake_idx, | ||
6331 | sizeof(int), 0644, proc_dointvec_minmax); | ||
6332 | set_table_entry(&table[6], "forkexec_idx", &sd->forkexec_idx, | ||
6333 | sizeof(int), 0644, proc_dointvec_minmax); | ||
6334 | set_table_entry(&table[7], "busy_factor", &sd->busy_factor, | ||
6335 | sizeof(int), 0644, proc_dointvec_minmax); | ||
6336 | set_table_entry(&table[8], "imbalance_pct", &sd->imbalance_pct, | ||
6337 | sizeof(int), 0644, proc_dointvec_minmax); | ||
6338 | set_table_entry(&table[9], "cache_nice_tries", | ||
6339 | &sd->cache_nice_tries, | ||
6340 | sizeof(int), 0644, proc_dointvec_minmax); | ||
6341 | set_table_entry(&table[10], "flags", &sd->flags, | ||
6342 | sizeof(int), 0644, proc_dointvec_minmax); | ||
6343 | set_table_entry(&table[11], "name", sd->name, | ||
6344 | CORENAME_MAX_SIZE, 0444, proc_dostring); | ||
6345 | /* &table[12] is terminator */ | ||
6346 | |||
6347 | return table; | ||
6348 | } | ||
6349 | |||
6350 | static ctl_table *sd_alloc_ctl_cpu_table(int cpu) | ||
6351 | { | ||
6352 | struct ctl_table *entry, *table; | ||
6353 | struct sched_domain *sd; | ||
6354 | int domain_num = 0, i; | ||
6355 | char buf[32]; | ||
6356 | |||
6357 | for_each_domain(cpu, sd) | ||
6358 | domain_num++; | ||
6359 | entry = table = sd_alloc_ctl_entry(domain_num + 1); | ||
6360 | if (table == NULL) | ||
6361 | return NULL; | ||
6362 | |||
6363 | i = 0; | ||
6364 | for_each_domain(cpu, sd) { | ||
6365 | snprintf(buf, 32, "domain%d", i); | ||
6366 | entry->procname = kstrdup(buf, GFP_KERNEL); | ||
6367 | entry->mode = 0555; | ||
6368 | entry->child = sd_alloc_ctl_domain_table(sd); | ||
6369 | entry++; | ||
6370 | i++; | ||
6371 | } | ||
6372 | return table; | ||
6373 | } | ||
6374 | |||
6375 | static struct ctl_table_header *sd_sysctl_header; | ||
6376 | static void register_sched_domain_sysctl(void) | ||
6377 | { | ||
6378 | int i, cpu_num = num_possible_cpus(); | ||
6379 | struct ctl_table *entry = sd_alloc_ctl_entry(cpu_num + 1); | ||
6380 | char buf[32]; | ||
6381 | |||
6382 | WARN_ON(sd_ctl_dir[0].child); | ||
6383 | sd_ctl_dir[0].child = entry; | ||
6384 | |||
6385 | if (entry == NULL) | ||
6386 | return; | ||
6387 | |||
6388 | for_each_possible_cpu(i) { | ||
6389 | snprintf(buf, 32, "cpu%d", i); | ||
6390 | entry->procname = kstrdup(buf, GFP_KERNEL); | ||
6391 | entry->mode = 0555; | ||
6392 | entry->child = sd_alloc_ctl_cpu_table(i); | ||
6393 | entry++; | ||
6394 | } | ||
6395 | |||
6396 | WARN_ON(sd_sysctl_header); | ||
6397 | sd_sysctl_header = register_sysctl_table(sd_ctl_root); | ||
6398 | } | ||
6399 | |||
6400 | /* may be called multiple times per register */ | ||
6401 | static void unregister_sched_domain_sysctl(void) | ||
6402 | { | ||
6403 | if (sd_sysctl_header) | ||
6404 | unregister_sysctl_table(sd_sysctl_header); | ||
6405 | sd_sysctl_header = NULL; | ||
6406 | if (sd_ctl_dir[0].child) | ||
6407 | sd_free_ctl_entry(&sd_ctl_dir[0].child); | ||
6408 | } | ||
6409 | #else | ||
6410 | static void register_sched_domain_sysctl(void) | ||
6411 | { | ||
6412 | } | ||
6413 | static void unregister_sched_domain_sysctl(void) | ||
6414 | { | ||
6415 | } | ||
6416 | #endif | ||
6417 | |||
6418 | static void set_rq_online(struct rq *rq) | ||
6419 | { | ||
6420 | if (!rq->online) { | ||
6421 | const struct sched_class *class; | ||
6422 | |||
6423 | cpumask_set_cpu(rq->cpu, rq->rd->online); | ||
6424 | rq->online = 1; | ||
6425 | |||
6426 | for_each_class(class) { | ||
6427 | if (class->rq_online) | ||
6428 | class->rq_online(rq); | ||
6429 | } | ||
6430 | } | ||
6431 | } | ||
6432 | |||
6433 | static void set_rq_offline(struct rq *rq) | ||
6434 | { | ||
6435 | if (rq->online) { | ||
6436 | const struct sched_class *class; | ||
6437 | |||
6438 | for_each_class(class) { | ||
6439 | if (class->rq_offline) | ||
6440 | class->rq_offline(rq); | ||
6441 | } | ||
6442 | |||
6443 | cpumask_clear_cpu(rq->cpu, rq->rd->online); | ||
6444 | rq->online = 0; | ||
6445 | } | ||
6446 | } | ||
6447 | |||
6448 | /* | ||
6449 | * migration_call - callback that gets triggered when a CPU is added. | ||
6450 | * Here we can start up the necessary migration thread for the new CPU. | ||
6451 | */ | ||
6452 | static int __cpuinit | ||
6453 | migration_call(struct notifier_block *nfb, unsigned long action, void *hcpu) | ||
6454 | { | ||
6455 | int cpu = (long)hcpu; | ||
6456 | unsigned long flags; | ||
6457 | struct rq *rq = cpu_rq(cpu); | ||
6458 | |||
6459 | switch (action & ~CPU_TASKS_FROZEN) { | ||
6460 | |||
6461 | case CPU_UP_PREPARE: | ||
6462 | rq->calc_load_update = calc_load_update; | ||
6463 | break; | ||
6464 | |||
6465 | case CPU_ONLINE: | ||
6466 | /* Update our root-domain */ | ||
6467 | raw_spin_lock_irqsave(&rq->lock, flags); | ||
6468 | if (rq->rd) { | ||
6469 | BUG_ON(!cpumask_test_cpu(cpu, rq->rd->span)); | ||
6470 | |||
6471 | set_rq_online(rq); | ||
6472 | } | ||
6473 | raw_spin_unlock_irqrestore(&rq->lock, flags); | ||
6474 | break; | ||
6475 | |||
6476 | #ifdef CONFIG_HOTPLUG_CPU | ||
6477 | case CPU_DYING: | ||
6478 | sched_ttwu_pending(); | ||
6479 | /* Update our root-domain */ | ||
6480 | raw_spin_lock_irqsave(&rq->lock, flags); | ||
6481 | if (rq->rd) { | ||
6482 | BUG_ON(!cpumask_test_cpu(cpu, rq->rd->span)); | ||
6483 | set_rq_offline(rq); | ||
6484 | } | ||
6485 | migrate_tasks(cpu); | ||
6486 | BUG_ON(rq->nr_running != 1); /* the migration thread */ | ||
6487 | raw_spin_unlock_irqrestore(&rq->lock, flags); | ||
6488 | |||
6489 | migrate_nr_uninterruptible(rq); | ||
6490 | calc_global_load_remove(rq); | ||
6491 | break; | ||
6492 | #endif | ||
6493 | } | ||
6494 | |||
6495 | update_max_interval(); | ||
6496 | |||
6497 | return NOTIFY_OK; | ||
6498 | } | ||
6499 | |||
6500 | /* | ||
6501 | * Register at high priority so that task migration (migrate_all_tasks) | ||
6502 | * happens before everything else. This has to be lower priority than | ||
6503 | * the notifier in the perf_event subsystem, though. | ||
6504 | */ | ||
6505 | static struct notifier_block __cpuinitdata migration_notifier = { | ||
6506 | .notifier_call = migration_call, | ||
6507 | .priority = CPU_PRI_MIGRATION, | ||
6508 | }; | ||
6509 | |||
6510 | static int __cpuinit sched_cpu_active(struct notifier_block *nfb, | ||
6511 | unsigned long action, void *hcpu) | ||
6512 | { | ||
6513 | switch (action & ~CPU_TASKS_FROZEN) { | ||
6514 | case CPU_STARTING: | ||
6515 | case CPU_DOWN_FAILED: | ||
6516 | set_cpu_active((long)hcpu, true); | ||
6517 | return NOTIFY_OK; | ||
6518 | default: | ||
6519 | return NOTIFY_DONE; | ||
6520 | } | ||
6521 | } | ||
6522 | |||
6523 | static int __cpuinit sched_cpu_inactive(struct notifier_block *nfb, | ||
6524 | unsigned long action, void *hcpu) | ||
6525 | { | ||
6526 | switch (action & ~CPU_TASKS_FROZEN) { | ||
6527 | case CPU_DOWN_PREPARE: | ||
6528 | set_cpu_active((long)hcpu, false); | ||
6529 | return NOTIFY_OK; | ||
6530 | default: | ||
6531 | return NOTIFY_DONE; | ||
6532 | } | ||
6533 | } | ||
6534 | |||
6535 | static int __init migration_init(void) | ||
6536 | { | ||
6537 | void *cpu = (void *)(long)smp_processor_id(); | ||
6538 | int err; | ||
6539 | |||
6540 | /* Initialize migration for the boot CPU */ | ||
6541 | err = migration_call(&migration_notifier, CPU_UP_PREPARE, cpu); | ||
6542 | BUG_ON(err == NOTIFY_BAD); | ||
6543 | migration_call(&migration_notifier, CPU_ONLINE, cpu); | ||
6544 | register_cpu_notifier(&migration_notifier); | ||
6545 | |||
6546 | /* Register cpu active notifiers */ | ||
6547 | cpu_notifier(sched_cpu_active, CPU_PRI_SCHED_ACTIVE); | ||
6548 | cpu_notifier(sched_cpu_inactive, CPU_PRI_SCHED_INACTIVE); | ||
6549 | |||
6550 | return 0; | ||
6551 | } | ||
6552 | early_initcall(migration_init); | ||
6553 | #endif | ||
6554 | |||
6555 | #ifdef CONFIG_SMP | ||
6556 | |||
6557 | static cpumask_var_t sched_domains_tmpmask; /* sched_domains_mutex */ | ||
6558 | |||
6559 | #ifdef CONFIG_SCHED_DEBUG | ||
6560 | |||
6561 | static __read_mostly int sched_domain_debug_enabled; | ||
6562 | |||
6563 | static int __init sched_domain_debug_setup(char *str) | ||
6564 | { | ||
6565 | sched_domain_debug_enabled = 1; | ||
6566 | |||
6567 | return 0; | ||
6568 | } | ||
6569 | early_param("sched_debug", sched_domain_debug_setup); | ||
6570 | |||
6571 | static int sched_domain_debug_one(struct sched_domain *sd, int cpu, int level, | ||
6572 | struct cpumask *groupmask) | ||
6573 | { | ||
6574 | struct sched_group *group = sd->groups; | ||
6575 | char str[256]; | ||
6576 | |||
6577 | cpulist_scnprintf(str, sizeof(str), sched_domain_span(sd)); | ||
6578 | cpumask_clear(groupmask); | ||
6579 | |||
6580 | printk(KERN_DEBUG "%*s domain %d: ", level, "", level); | ||
6581 | |||
6582 | if (!(sd->flags & SD_LOAD_BALANCE)) { | ||
6583 | printk("does not load-balance\n"); | ||
6584 | if (sd->parent) | ||
6585 | printk(KERN_ERR "ERROR: !SD_LOAD_BALANCE domain" | ||
6586 | " has parent"); | ||
6587 | return -1; | ||
6588 | } | ||
6589 | |||
6590 | printk(KERN_CONT "span %s level %s\n", str, sd->name); | ||
6591 | |||
6592 | if (!cpumask_test_cpu(cpu, sched_domain_span(sd))) { | ||
6593 | printk(KERN_ERR "ERROR: domain->span does not contain " | ||
6594 | "CPU%d\n", cpu); | ||
6595 | } | ||
6596 | if (!cpumask_test_cpu(cpu, sched_group_cpus(group))) { | ||
6597 | printk(KERN_ERR "ERROR: domain->groups does not contain" | ||
6598 | " CPU%d\n", cpu); | ||
6599 | } | ||
6600 | |||
6601 | printk(KERN_DEBUG "%*s groups:", level + 1, ""); | ||
6602 | do { | ||
6603 | if (!group) { | ||
6604 | printk("\n"); | ||
6605 | printk(KERN_ERR "ERROR: group is NULL\n"); | ||
6606 | break; | ||
6607 | } | ||
6608 | |||
6609 | if (!group->sgp->power) { | ||
6610 | printk(KERN_CONT "\n"); | ||
6611 | printk(KERN_ERR "ERROR: domain->cpu_power not " | ||
6612 | "set\n"); | ||
6613 | break; | ||
6614 | } | ||
6615 | |||
6616 | if (!cpumask_weight(sched_group_cpus(group))) { | ||
6617 | printk(KERN_CONT "\n"); | ||
6618 | printk(KERN_ERR "ERROR: empty group\n"); | ||
6619 | break; | ||
6620 | } | ||
6621 | |||
6622 | if (cpumask_intersects(groupmask, sched_group_cpus(group))) { | ||
6623 | printk(KERN_CONT "\n"); | ||
6624 | printk(KERN_ERR "ERROR: repeated CPUs\n"); | ||
6625 | break; | ||
6626 | } | ||
6627 | |||
6628 | cpumask_or(groupmask, groupmask, sched_group_cpus(group)); | ||
6629 | |||
6630 | cpulist_scnprintf(str, sizeof(str), sched_group_cpus(group)); | ||
6631 | |||
6632 | printk(KERN_CONT " %s", str); | ||
6633 | if (group->sgp->power != SCHED_POWER_SCALE) { | ||
6634 | printk(KERN_CONT " (cpu_power = %d)", | ||
6635 | group->sgp->power); | ||
6636 | } | ||
6637 | |||
6638 | group = group->next; | ||
6639 | } while (group != sd->groups); | ||
6640 | printk(KERN_CONT "\n"); | ||
6641 | |||
6642 | if (!cpumask_equal(sched_domain_span(sd), groupmask)) | ||
6643 | printk(KERN_ERR "ERROR: groups don't span domain->span\n"); | ||
6644 | |||
6645 | if (sd->parent && | ||
6646 | !cpumask_subset(groupmask, sched_domain_span(sd->parent))) | ||
6647 | printk(KERN_ERR "ERROR: parent span is not a superset " | ||
6648 | "of domain->span\n"); | ||
6649 | return 0; | ||
6650 | } | ||
6651 | |||
6652 | static void sched_domain_debug(struct sched_domain *sd, int cpu) | ||
6653 | { | ||
6654 | int level = 0; | ||
6655 | |||
6656 | if (!sched_domain_debug_enabled) | ||
6657 | return; | ||
6658 | |||
6659 | if (!sd) { | ||
6660 | printk(KERN_DEBUG "CPU%d attaching NULL sched-domain.\n", cpu); | ||
6661 | return; | ||
6662 | } | ||
6663 | |||
6664 | printk(KERN_DEBUG "CPU%d attaching sched-domain:\n", cpu); | ||
6665 | |||
6666 | for (;;) { | ||
6667 | if (sched_domain_debug_one(sd, cpu, level, sched_domains_tmpmask)) | ||
6668 | break; | ||
6669 | level++; | ||
6670 | sd = sd->parent; | ||
6671 | if (!sd) | ||
6672 | break; | ||
6673 | } | ||
6674 | } | ||
6675 | #else /* !CONFIG_SCHED_DEBUG */ | ||
6676 | # define sched_domain_debug(sd, cpu) do { } while (0) | ||
6677 | #endif /* CONFIG_SCHED_DEBUG */ | ||
6678 | |||
6679 | static int sd_degenerate(struct sched_domain *sd) | ||
6680 | { | ||
6681 | if (cpumask_weight(sched_domain_span(sd)) == 1) | ||
6682 | return 1; | ||
6683 | |||
6684 | /* Following flags need at least 2 groups */ | ||
6685 | if (sd->flags & (SD_LOAD_BALANCE | | ||
6686 | SD_BALANCE_NEWIDLE | | ||
6687 | SD_BALANCE_FORK | | ||
6688 | SD_BALANCE_EXEC | | ||
6689 | SD_SHARE_CPUPOWER | | ||
6690 | SD_SHARE_PKG_RESOURCES)) { | ||
6691 | if (sd->groups != sd->groups->next) | ||
6692 | return 0; | ||
6693 | } | ||
6694 | |||
6695 | /* Following flags don't use groups */ | ||
6696 | if (sd->flags & (SD_WAKE_AFFINE)) | ||
6697 | return 0; | ||
6698 | |||
6699 | return 1; | ||
6700 | } | ||
6701 | |||
6702 | static int | ||
6703 | sd_parent_degenerate(struct sched_domain *sd, struct sched_domain *parent) | ||
6704 | { | ||
6705 | unsigned long cflags = sd->flags, pflags = parent->flags; | ||
6706 | |||
6707 | if (sd_degenerate(parent)) | ||
6708 | return 1; | ||
6709 | |||
6710 | if (!cpumask_equal(sched_domain_span(sd), sched_domain_span(parent))) | ||
6711 | return 0; | ||
6712 | |||
6713 | /* Flags needing groups don't count if only 1 group in parent */ | ||
6714 | if (parent->groups == parent->groups->next) { | ||
6715 | pflags &= ~(SD_LOAD_BALANCE | | ||
6716 | SD_BALANCE_NEWIDLE | | ||
6717 | SD_BALANCE_FORK | | ||
6718 | SD_BALANCE_EXEC | | ||
6719 | SD_SHARE_CPUPOWER | | ||
6720 | SD_SHARE_PKG_RESOURCES); | ||
6721 | if (nr_node_ids == 1) | ||
6722 | pflags &= ~SD_SERIALIZE; | ||
6723 | } | ||
6724 | if (~cflags & pflags) | ||
6725 | return 0; | ||
6726 | |||
6727 | return 1; | ||
6728 | } | ||
6729 | |||
6730 | static void free_rootdomain(struct rcu_head *rcu) | ||
6731 | { | ||
6732 | struct root_domain *rd = container_of(rcu, struct root_domain, rcu); | ||
6733 | |||
6734 | cpupri_cleanup(&rd->cpupri); | ||
6735 | free_cpumask_var(rd->rto_mask); | ||
6736 | free_cpumask_var(rd->online); | ||
6737 | free_cpumask_var(rd->span); | ||
6738 | kfree(rd); | ||
6739 | } | ||
6740 | |||
6741 | static void rq_attach_root(struct rq *rq, struct root_domain *rd) | ||
6742 | { | ||
6743 | struct root_domain *old_rd = NULL; | ||
6744 | unsigned long flags; | ||
6745 | |||
6746 | raw_spin_lock_irqsave(&rq->lock, flags); | ||
6747 | |||
6748 | if (rq->rd) { | ||
6749 | old_rd = rq->rd; | ||
6750 | |||
6751 | if (cpumask_test_cpu(rq->cpu, old_rd->online)) | ||
6752 | set_rq_offline(rq); | ||
6753 | |||
6754 | cpumask_clear_cpu(rq->cpu, old_rd->span); | ||
6755 | |||
6756 | /* | ||
6757 | * If we dont want to free the old_rt yet then | ||
6758 | * set old_rd to NULL to skip the freeing later | ||
6759 | * in this function: | ||
6760 | */ | ||
6761 | if (!atomic_dec_and_test(&old_rd->refcount)) | ||
6762 | old_rd = NULL; | ||
6763 | } | ||
6764 | |||
6765 | atomic_inc(&rd->refcount); | ||
6766 | rq->rd = rd; | ||
6767 | |||
6768 | cpumask_set_cpu(rq->cpu, rd->span); | ||
6769 | if (cpumask_test_cpu(rq->cpu, cpu_active_mask)) | ||
6770 | set_rq_online(rq); | ||
6771 | |||
6772 | raw_spin_unlock_irqrestore(&rq->lock, flags); | ||
6773 | |||
6774 | if (old_rd) | ||
6775 | call_rcu_sched(&old_rd->rcu, free_rootdomain); | ||
6776 | } | ||
6777 | |||
6778 | static int init_rootdomain(struct root_domain *rd) | ||
6779 | { | ||
6780 | memset(rd, 0, sizeof(*rd)); | ||
6781 | |||
6782 | if (!alloc_cpumask_var(&rd->span, GFP_KERNEL)) | ||
6783 | goto out; | ||
6784 | if (!alloc_cpumask_var(&rd->online, GFP_KERNEL)) | ||
6785 | goto free_span; | ||
6786 | if (!alloc_cpumask_var(&rd->rto_mask, GFP_KERNEL)) | ||
6787 | goto free_online; | ||
6788 | |||
6789 | if (cpupri_init(&rd->cpupri) != 0) | ||
6790 | goto free_rto_mask; | ||
6791 | return 0; | ||
6792 | |||
6793 | free_rto_mask: | ||
6794 | free_cpumask_var(rd->rto_mask); | ||
6795 | free_online: | ||
6796 | free_cpumask_var(rd->online); | ||
6797 | free_span: | ||
6798 | free_cpumask_var(rd->span); | ||
6799 | out: | ||
6800 | return -ENOMEM; | ||
6801 | } | ||
6802 | |||
6803 | static void init_defrootdomain(void) | ||
6804 | { | ||
6805 | init_rootdomain(&def_root_domain); | ||
6806 | |||
6807 | atomic_set(&def_root_domain.refcount, 1); | ||
6808 | } | ||
6809 | |||
6810 | static struct root_domain *alloc_rootdomain(void) | ||
6811 | { | ||
6812 | struct root_domain *rd; | ||
6813 | |||
6814 | rd = kmalloc(sizeof(*rd), GFP_KERNEL); | ||
6815 | if (!rd) | ||
6816 | return NULL; | ||
6817 | |||
6818 | if (init_rootdomain(rd) != 0) { | ||
6819 | kfree(rd); | ||
6820 | return NULL; | ||
6821 | } | ||
6822 | |||
6823 | return rd; | ||
6824 | } | ||
6825 | |||
6826 | static void free_sched_groups(struct sched_group *sg, int free_sgp) | ||
6827 | { | ||
6828 | struct sched_group *tmp, *first; | ||
6829 | |||
6830 | if (!sg) | ||
6831 | return; | ||
6832 | |||
6833 | first = sg; | ||
6834 | do { | ||
6835 | tmp = sg->next; | ||
6836 | |||
6837 | if (free_sgp && atomic_dec_and_test(&sg->sgp->ref)) | ||
6838 | kfree(sg->sgp); | ||
6839 | |||
6840 | kfree(sg); | ||
6841 | sg = tmp; | ||
6842 | } while (sg != first); | ||
6843 | } | ||
6844 | |||
6845 | static void free_sched_domain(struct rcu_head *rcu) | ||
6846 | { | ||
6847 | struct sched_domain *sd = container_of(rcu, struct sched_domain, rcu); | ||
6848 | |||
6849 | /* | ||
6850 | * If its an overlapping domain it has private groups, iterate and | ||
6851 | * nuke them all. | ||
6852 | */ | ||
6853 | if (sd->flags & SD_OVERLAP) { | ||
6854 | free_sched_groups(sd->groups, 1); | ||
6855 | } else if (atomic_dec_and_test(&sd->groups->ref)) { | ||
6856 | kfree(sd->groups->sgp); | ||
6857 | kfree(sd->groups); | ||
6858 | } | ||
6859 | kfree(sd); | ||
6860 | } | ||
6861 | |||
6862 | static void destroy_sched_domain(struct sched_domain *sd, int cpu) | ||
6863 | { | ||
6864 | call_rcu(&sd->rcu, free_sched_domain); | ||
6865 | } | ||
6866 | |||
6867 | static void destroy_sched_domains(struct sched_domain *sd, int cpu) | ||
6868 | { | ||
6869 | for (; sd; sd = sd->parent) | ||
6870 | destroy_sched_domain(sd, cpu); | ||
6871 | } | ||
6872 | |||
6873 | /* | ||
6874 | * Attach the domain 'sd' to 'cpu' as its base domain. Callers must | ||
6875 | * hold the hotplug lock. | ||
6876 | */ | ||
6877 | static void | ||
6878 | cpu_attach_domain(struct sched_domain *sd, struct root_domain *rd, int cpu) | ||
6879 | { | ||
6880 | struct rq *rq = cpu_rq(cpu); | ||
6881 | struct sched_domain *tmp; | ||
6882 | |||
6883 | /* Remove the sched domains which do not contribute to scheduling. */ | ||
6884 | for (tmp = sd; tmp; ) { | ||
6885 | struct sched_domain *parent = tmp->parent; | ||
6886 | if (!parent) | ||
6887 | break; | ||
6888 | |||
6889 | if (sd_parent_degenerate(tmp, parent)) { | ||
6890 | tmp->parent = parent->parent; | ||
6891 | if (parent->parent) | ||
6892 | parent->parent->child = tmp; | ||
6893 | destroy_sched_domain(parent, cpu); | ||
6894 | } else | ||
6895 | tmp = tmp->parent; | ||
6896 | } | ||
6897 | |||
6898 | if (sd && sd_degenerate(sd)) { | ||
6899 | tmp = sd; | ||
6900 | sd = sd->parent; | ||
6901 | destroy_sched_domain(tmp, cpu); | ||
6902 | if (sd) | ||
6903 | sd->child = NULL; | ||
6904 | } | ||
6905 | |||
6906 | sched_domain_debug(sd, cpu); | ||
6907 | |||
6908 | rq_attach_root(rq, rd); | ||
6909 | tmp = rq->sd; | ||
6910 | rcu_assign_pointer(rq->sd, sd); | ||
6911 | destroy_sched_domains(tmp, cpu); | ||
6912 | } | ||
6913 | |||
6914 | /* cpus with isolated domains */ | ||
6915 | static cpumask_var_t cpu_isolated_map; | ||
6916 | |||
6917 | /* Setup the mask of cpus configured for isolated domains */ | ||
6918 | static int __init isolated_cpu_setup(char *str) | ||
6919 | { | ||
6920 | alloc_bootmem_cpumask_var(&cpu_isolated_map); | ||
6921 | cpulist_parse(str, cpu_isolated_map); | ||
6922 | return 1; | ||
6923 | } | ||
6924 | |||
6925 | __setup("isolcpus=", isolated_cpu_setup); | ||
6926 | |||
6927 | #define SD_NODES_PER_DOMAIN 16 | ||
6928 | |||
6929 | #ifdef CONFIG_NUMA | ||
6930 | |||
6931 | /** | ||
6932 | * find_next_best_node - find the next node to include in a sched_domain | ||
6933 | * @node: node whose sched_domain we're building | ||
6934 | * @used_nodes: nodes already in the sched_domain | ||
6935 | * | ||
6936 | * Find the next node to include in a given scheduling domain. Simply | ||
6937 | * finds the closest node not already in the @used_nodes map. | ||
6938 | * | ||
6939 | * Should use nodemask_t. | ||
6940 | */ | ||
6941 | static int find_next_best_node(int node, nodemask_t *used_nodes) | ||
6942 | { | ||
6943 | int i, n, val, min_val, best_node = -1; | ||
6944 | |||
6945 | min_val = INT_MAX; | ||
6946 | |||
6947 | for (i = 0; i < nr_node_ids; i++) { | ||
6948 | /* Start at @node */ | ||
6949 | n = (node + i) % nr_node_ids; | ||
6950 | |||
6951 | if (!nr_cpus_node(n)) | ||
6952 | continue; | ||
6953 | |||
6954 | /* Skip already used nodes */ | ||
6955 | if (node_isset(n, *used_nodes)) | ||
6956 | continue; | ||
6957 | |||
6958 | /* Simple min distance search */ | ||
6959 | val = node_distance(node, n); | ||
6960 | |||
6961 | if (val < min_val) { | ||
6962 | min_val = val; | ||
6963 | best_node = n; | ||
6964 | } | ||
6965 | } | ||
6966 | |||
6967 | if (best_node != -1) | ||
6968 | node_set(best_node, *used_nodes); | ||
6969 | return best_node; | ||
6970 | } | ||
6971 | |||
6972 | /** | ||
6973 | * sched_domain_node_span - get a cpumask for a node's sched_domain | ||
6974 | * @node: node whose cpumask we're constructing | ||
6975 | * @span: resulting cpumask | ||
6976 | * | ||
6977 | * Given a node, construct a good cpumask for its sched_domain to span. It | ||
6978 | * should be one that prevents unnecessary balancing, but also spreads tasks | ||
6979 | * out optimally. | ||
6980 | */ | ||
6981 | static void sched_domain_node_span(int node, struct cpumask *span) | ||
6982 | { | ||
6983 | nodemask_t used_nodes; | ||
6984 | int i; | ||
6985 | |||
6986 | cpumask_clear(span); | ||
6987 | nodes_clear(used_nodes); | ||
6988 | |||
6989 | cpumask_or(span, span, cpumask_of_node(node)); | ||
6990 | node_set(node, used_nodes); | ||
6991 | |||
6992 | for (i = 1; i < SD_NODES_PER_DOMAIN; i++) { | ||
6993 | int next_node = find_next_best_node(node, &used_nodes); | ||
6994 | if (next_node < 0) | ||
6995 | break; | ||
6996 | cpumask_or(span, span, cpumask_of_node(next_node)); | ||
6997 | } | ||
6998 | } | ||
6999 | |||
7000 | static const struct cpumask *cpu_node_mask(int cpu) | ||
7001 | { | ||
7002 | lockdep_assert_held(&sched_domains_mutex); | ||
7003 | |||
7004 | sched_domain_node_span(cpu_to_node(cpu), sched_domains_tmpmask); | ||
7005 | |||
7006 | return sched_domains_tmpmask; | ||
7007 | } | ||
7008 | |||
7009 | static const struct cpumask *cpu_allnodes_mask(int cpu) | ||
7010 | { | ||
7011 | return cpu_possible_mask; | ||
7012 | } | ||
7013 | #endif /* CONFIG_NUMA */ | ||
7014 | |||
7015 | static const struct cpumask *cpu_cpu_mask(int cpu) | ||
7016 | { | ||
7017 | return cpumask_of_node(cpu_to_node(cpu)); | ||
7018 | } | ||
7019 | |||
7020 | int sched_smt_power_savings = 0, sched_mc_power_savings = 0; | ||
7021 | |||
7022 | struct sd_data { | ||
7023 | struct sched_domain **__percpu sd; | ||
7024 | struct sched_group **__percpu sg; | ||
7025 | struct sched_group_power **__percpu sgp; | ||
7026 | }; | ||
7027 | |||
7028 | struct s_data { | ||
7029 | struct sched_domain ** __percpu sd; | ||
7030 | struct root_domain *rd; | ||
7031 | }; | ||
7032 | |||
7033 | enum s_alloc { | ||
7034 | sa_rootdomain, | ||
7035 | sa_sd, | ||
7036 | sa_sd_storage, | ||
7037 | sa_none, | ||
7038 | }; | ||
7039 | |||
7040 | struct sched_domain_topology_level; | ||
7041 | |||
7042 | typedef struct sched_domain *(*sched_domain_init_f)(struct sched_domain_topology_level *tl, int cpu); | ||
7043 | typedef const struct cpumask *(*sched_domain_mask_f)(int cpu); | ||
7044 | |||
7045 | #define SDTL_OVERLAP 0x01 | ||
7046 | |||
7047 | struct sched_domain_topology_level { | ||
7048 | sched_domain_init_f init; | ||
7049 | sched_domain_mask_f mask; | ||
7050 | int flags; | ||
7051 | struct sd_data data; | ||
7052 | }; | ||
7053 | |||
7054 | static int | ||
7055 | build_overlap_sched_groups(struct sched_domain *sd, int cpu) | ||
7056 | { | ||
7057 | struct sched_group *first = NULL, *last = NULL, *groups = NULL, *sg; | ||
7058 | const struct cpumask *span = sched_domain_span(sd); | ||
7059 | struct cpumask *covered = sched_domains_tmpmask; | ||
7060 | struct sd_data *sdd = sd->private; | ||
7061 | struct sched_domain *child; | ||
7062 | int i; | ||
7063 | |||
7064 | cpumask_clear(covered); | ||
7065 | |||
7066 | for_each_cpu(i, span) { | ||
7067 | struct cpumask *sg_span; | ||
7068 | |||
7069 | if (cpumask_test_cpu(i, covered)) | ||
7070 | continue; | ||
7071 | |||
7072 | sg = kzalloc_node(sizeof(struct sched_group) + cpumask_size(), | ||
7073 | GFP_KERNEL, cpu_to_node(i)); | ||
7074 | |||
7075 | if (!sg) | ||
7076 | goto fail; | ||
7077 | |||
7078 | sg_span = sched_group_cpus(sg); | ||
7079 | |||
7080 | child = *per_cpu_ptr(sdd->sd, i); | ||
7081 | if (child->child) { | ||
7082 | child = child->child; | ||
7083 | cpumask_copy(sg_span, sched_domain_span(child)); | ||
7084 | } else | ||
7085 | cpumask_set_cpu(i, sg_span); | ||
7086 | |||
7087 | cpumask_or(covered, covered, sg_span); | ||
7088 | |||
7089 | sg->sgp = *per_cpu_ptr(sdd->sgp, cpumask_first(sg_span)); | ||
7090 | atomic_inc(&sg->sgp->ref); | ||
7091 | |||
7092 | if (cpumask_test_cpu(cpu, sg_span)) | ||
7093 | groups = sg; | ||
7094 | |||
7095 | if (!first) | ||
7096 | first = sg; | ||
7097 | if (last) | ||
7098 | last->next = sg; | ||
7099 | last = sg; | ||
7100 | last->next = first; | ||
7101 | } | ||
7102 | sd->groups = groups; | ||
7103 | |||
7104 | return 0; | ||
7105 | |||
7106 | fail: | ||
7107 | free_sched_groups(first, 0); | ||
7108 | |||
7109 | return -ENOMEM; | ||
7110 | } | ||
7111 | |||
7112 | static int get_group(int cpu, struct sd_data *sdd, struct sched_group **sg) | ||
7113 | { | ||
7114 | struct sched_domain *sd = *per_cpu_ptr(sdd->sd, cpu); | ||
7115 | struct sched_domain *child = sd->child; | ||
7116 | |||
7117 | if (child) | ||
7118 | cpu = cpumask_first(sched_domain_span(child)); | ||
7119 | |||
7120 | if (sg) { | ||
7121 | *sg = *per_cpu_ptr(sdd->sg, cpu); | ||
7122 | (*sg)->sgp = *per_cpu_ptr(sdd->sgp, cpu); | ||
7123 | atomic_set(&(*sg)->sgp->ref, 1); /* for claim_allocations */ | ||
7124 | } | ||
7125 | |||
7126 | return cpu; | ||
7127 | } | ||
7128 | |||
7129 | /* | ||
7130 | * build_sched_groups will build a circular linked list of the groups | ||
7131 | * covered by the given span, and will set each group's ->cpumask correctly, | ||
7132 | * and ->cpu_power to 0. | ||
7133 | * | ||
7134 | * Assumes the sched_domain tree is fully constructed | ||
7135 | */ | ||
7136 | static int | ||
7137 | build_sched_groups(struct sched_domain *sd, int cpu) | ||
7138 | { | ||
7139 | struct sched_group *first = NULL, *last = NULL; | ||
7140 | struct sd_data *sdd = sd->private; | ||
7141 | const struct cpumask *span = sched_domain_span(sd); | ||
7142 | struct cpumask *covered; | ||
7143 | int i; | ||
7144 | |||
7145 | get_group(cpu, sdd, &sd->groups); | ||
7146 | atomic_inc(&sd->groups->ref); | ||
7147 | |||
7148 | if (cpu != cpumask_first(sched_domain_span(sd))) | ||
7149 | return 0; | ||
7150 | |||
7151 | lockdep_assert_held(&sched_domains_mutex); | ||
7152 | covered = sched_domains_tmpmask; | ||
7153 | |||
7154 | cpumask_clear(covered); | ||
7155 | |||
7156 | for_each_cpu(i, span) { | ||
7157 | struct sched_group *sg; | ||
7158 | int group = get_group(i, sdd, &sg); | ||
7159 | int j; | ||
7160 | |||
7161 | if (cpumask_test_cpu(i, covered)) | ||
7162 | continue; | ||
7163 | |||
7164 | cpumask_clear(sched_group_cpus(sg)); | ||
7165 | sg->sgp->power = 0; | ||
7166 | |||
7167 | for_each_cpu(j, span) { | ||
7168 | if (get_group(j, sdd, NULL) != group) | ||
7169 | continue; | ||
7170 | |||
7171 | cpumask_set_cpu(j, covered); | ||
7172 | cpumask_set_cpu(j, sched_group_cpus(sg)); | ||
7173 | } | ||
7174 | |||
7175 | if (!first) | ||
7176 | first = sg; | ||
7177 | if (last) | ||
7178 | last->next = sg; | ||
7179 | last = sg; | ||
7180 | } | ||
7181 | last->next = first; | ||
7182 | |||
7183 | return 0; | ||
7184 | } | ||
7185 | |||
7186 | /* | ||
7187 | * Initialize sched groups cpu_power. | ||
7188 | * | ||
7189 | * cpu_power indicates the capacity of sched group, which is used while | ||
7190 | * distributing the load between different sched groups in a sched domain. | ||
7191 | * Typically cpu_power for all the groups in a sched domain will be same unless | ||
7192 | * there are asymmetries in the topology. If there are asymmetries, group | ||
7193 | * having more cpu_power will pickup more load compared to the group having | ||
7194 | * less cpu_power. | ||
7195 | */ | ||
7196 | static void init_sched_groups_power(int cpu, struct sched_domain *sd) | ||
7197 | { | ||
7198 | struct sched_group *sg = sd->groups; | ||
7199 | |||
7200 | WARN_ON(!sd || !sg); | ||
7201 | |||
7202 | do { | ||
7203 | sg->group_weight = cpumask_weight(sched_group_cpus(sg)); | ||
7204 | sg = sg->next; | ||
7205 | } while (sg != sd->groups); | ||
7206 | |||
7207 | if (cpu != group_first_cpu(sg)) | ||
7208 | return; | ||
7209 | |||
7210 | update_group_power(sd, cpu); | ||
7211 | } | ||
7212 | |||
7213 | /* | ||
7214 | * Initializers for schedule domains | ||
7215 | * Non-inlined to reduce accumulated stack pressure in build_sched_domains() | ||
7216 | */ | ||
7217 | |||
7218 | #ifdef CONFIG_SCHED_DEBUG | ||
7219 | # define SD_INIT_NAME(sd, type) sd->name = #type | ||
7220 | #else | ||
7221 | # define SD_INIT_NAME(sd, type) do { } while (0) | ||
7222 | #endif | ||
7223 | |||
7224 | #define SD_INIT_FUNC(type) \ | ||
7225 | static noinline struct sched_domain * \ | ||
7226 | sd_init_##type(struct sched_domain_topology_level *tl, int cpu) \ | ||
7227 | { \ | ||
7228 | struct sched_domain *sd = *per_cpu_ptr(tl->data.sd, cpu); \ | ||
7229 | *sd = SD_##type##_INIT; \ | ||
7230 | SD_INIT_NAME(sd, type); \ | ||
7231 | sd->private = &tl->data; \ | ||
7232 | return sd; \ | ||
7233 | } | ||
7234 | |||
7235 | SD_INIT_FUNC(CPU) | ||
7236 | #ifdef CONFIG_NUMA | ||
7237 | SD_INIT_FUNC(ALLNODES) | ||
7238 | SD_INIT_FUNC(NODE) | ||
7239 | #endif | ||
7240 | #ifdef CONFIG_SCHED_SMT | ||
7241 | SD_INIT_FUNC(SIBLING) | ||
7242 | #endif | ||
7243 | #ifdef CONFIG_SCHED_MC | ||
7244 | SD_INIT_FUNC(MC) | ||
7245 | #endif | ||
7246 | #ifdef CONFIG_SCHED_BOOK | ||
7247 | SD_INIT_FUNC(BOOK) | ||
7248 | #endif | ||
7249 | |||
7250 | static int default_relax_domain_level = -1; | ||
7251 | int sched_domain_level_max; | ||
7252 | |||
7253 | static int __init setup_relax_domain_level(char *str) | ||
7254 | { | ||
7255 | unsigned long val; | ||
7256 | |||
7257 | val = simple_strtoul(str, NULL, 0); | ||
7258 | if (val < sched_domain_level_max) | ||
7259 | default_relax_domain_level = val; | ||
7260 | |||
7261 | return 1; | ||
7262 | } | ||
7263 | __setup("relax_domain_level=", setup_relax_domain_level); | ||
7264 | |||
7265 | static void set_domain_attribute(struct sched_domain *sd, | ||
7266 | struct sched_domain_attr *attr) | ||
7267 | { | ||
7268 | int request; | ||
7269 | |||
7270 | if (!attr || attr->relax_domain_level < 0) { | ||
7271 | if (default_relax_domain_level < 0) | ||
7272 | return; | ||
7273 | else | ||
7274 | request = default_relax_domain_level; | ||
7275 | } else | ||
7276 | request = attr->relax_domain_level; | ||
7277 | if (request < sd->level) { | ||
7278 | /* turn off idle balance on this domain */ | ||
7279 | sd->flags &= ~(SD_BALANCE_WAKE|SD_BALANCE_NEWIDLE); | ||
7280 | } else { | ||
7281 | /* turn on idle balance on this domain */ | ||
7282 | sd->flags |= (SD_BALANCE_WAKE|SD_BALANCE_NEWIDLE); | ||
7283 | } | ||
7284 | } | ||
7285 | |||
7286 | static void __sdt_free(const struct cpumask *cpu_map); | ||
7287 | static int __sdt_alloc(const struct cpumask *cpu_map); | ||
7288 | |||
7289 | static void __free_domain_allocs(struct s_data *d, enum s_alloc what, | ||
7290 | const struct cpumask *cpu_map) | ||
7291 | { | ||
7292 | switch (what) { | ||
7293 | case sa_rootdomain: | ||
7294 | if (!atomic_read(&d->rd->refcount)) | ||
7295 | free_rootdomain(&d->rd->rcu); /* fall through */ | ||
7296 | case sa_sd: | ||
7297 | free_percpu(d->sd); /* fall through */ | ||
7298 | case sa_sd_storage: | ||
7299 | __sdt_free(cpu_map); /* fall through */ | ||
7300 | case sa_none: | ||
7301 | break; | ||
7302 | } | ||
7303 | } | ||
7304 | |||
7305 | static enum s_alloc __visit_domain_allocation_hell(struct s_data *d, | ||
7306 | const struct cpumask *cpu_map) | ||
7307 | { | ||
7308 | memset(d, 0, sizeof(*d)); | ||
7309 | |||
7310 | if (__sdt_alloc(cpu_map)) | ||
7311 | return sa_sd_storage; | ||
7312 | d->sd = alloc_percpu(struct sched_domain *); | ||
7313 | if (!d->sd) | ||
7314 | return sa_sd_storage; | ||
7315 | d->rd = alloc_rootdomain(); | ||
7316 | if (!d->rd) | ||
7317 | return sa_sd; | ||
7318 | return sa_rootdomain; | ||
7319 | } | ||
7320 | |||
7321 | /* | ||
7322 | * NULL the sd_data elements we've used to build the sched_domain and | ||
7323 | * sched_group structure so that the subsequent __free_domain_allocs() | ||
7324 | * will not free the data we're using. | ||
7325 | */ | ||
7326 | static void claim_allocations(int cpu, struct sched_domain *sd) | ||
7327 | { | ||
7328 | struct sd_data *sdd = sd->private; | ||
7329 | |||
7330 | WARN_ON_ONCE(*per_cpu_ptr(sdd->sd, cpu) != sd); | ||
7331 | *per_cpu_ptr(sdd->sd, cpu) = NULL; | ||
7332 | |||
7333 | if (atomic_read(&(*per_cpu_ptr(sdd->sg, cpu))->ref)) | ||
7334 | *per_cpu_ptr(sdd->sg, cpu) = NULL; | ||
7335 | |||
7336 | if (atomic_read(&(*per_cpu_ptr(sdd->sgp, cpu))->ref)) | ||
7337 | *per_cpu_ptr(sdd->sgp, cpu) = NULL; | ||
7338 | } | ||
7339 | |||
7340 | #ifdef CONFIG_SCHED_SMT | ||
7341 | static const struct cpumask *cpu_smt_mask(int cpu) | ||
7342 | { | ||
7343 | return topology_thread_cpumask(cpu); | ||
7344 | } | ||
7345 | #endif | ||
7346 | |||
7347 | /* | ||
7348 | * Topology list, bottom-up. | ||
7349 | */ | ||
7350 | static struct sched_domain_topology_level default_topology[] = { | ||
7351 | #ifdef CONFIG_SCHED_SMT | ||
7352 | { sd_init_SIBLING, cpu_smt_mask, }, | ||
7353 | #endif | ||
7354 | #ifdef CONFIG_SCHED_MC | ||
7355 | { sd_init_MC, cpu_coregroup_mask, }, | ||
7356 | #endif | ||
7357 | #ifdef CONFIG_SCHED_BOOK | ||
7358 | { sd_init_BOOK, cpu_book_mask, }, | ||
7359 | #endif | ||
7360 | { sd_init_CPU, cpu_cpu_mask, }, | ||
7361 | #ifdef CONFIG_NUMA | ||
7362 | { sd_init_NODE, cpu_node_mask, SDTL_OVERLAP, }, | ||
7363 | { sd_init_ALLNODES, cpu_allnodes_mask, }, | ||
7364 | #endif | ||
7365 | { NULL, }, | ||
7366 | }; | ||
7367 | |||
7368 | static struct sched_domain_topology_level *sched_domain_topology = default_topology; | ||
7369 | |||
7370 | static int __sdt_alloc(const struct cpumask *cpu_map) | ||
7371 | { | ||
7372 | struct sched_domain_topology_level *tl; | ||
7373 | int j; | ||
7374 | |||
7375 | for (tl = sched_domain_topology; tl->init; tl++) { | ||
7376 | struct sd_data *sdd = &tl->data; | ||
7377 | |||
7378 | sdd->sd = alloc_percpu(struct sched_domain *); | ||
7379 | if (!sdd->sd) | ||
7380 | return -ENOMEM; | ||
7381 | |||
7382 | sdd->sg = alloc_percpu(struct sched_group *); | ||
7383 | if (!sdd->sg) | ||
7384 | return -ENOMEM; | ||
7385 | |||
7386 | sdd->sgp = alloc_percpu(struct sched_group_power *); | ||
7387 | if (!sdd->sgp) | ||
7388 | return -ENOMEM; | ||
7389 | |||
7390 | for_each_cpu(j, cpu_map) { | ||
7391 | struct sched_domain *sd; | ||
7392 | struct sched_group *sg; | ||
7393 | struct sched_group_power *sgp; | ||
7394 | |||
7395 | sd = kzalloc_node(sizeof(struct sched_domain) + cpumask_size(), | ||
7396 | GFP_KERNEL, cpu_to_node(j)); | ||
7397 | if (!sd) | ||
7398 | return -ENOMEM; | ||
7399 | |||
7400 | *per_cpu_ptr(sdd->sd, j) = sd; | ||
7401 | |||
7402 | sg = kzalloc_node(sizeof(struct sched_group) + cpumask_size(), | ||
7403 | GFP_KERNEL, cpu_to_node(j)); | ||
7404 | if (!sg) | ||
7405 | return -ENOMEM; | ||
7406 | |||
7407 | *per_cpu_ptr(sdd->sg, j) = sg; | ||
7408 | |||
7409 | sgp = kzalloc_node(sizeof(struct sched_group_power), | ||
7410 | GFP_KERNEL, cpu_to_node(j)); | ||
7411 | if (!sgp) | ||
7412 | return -ENOMEM; | ||
7413 | |||
7414 | *per_cpu_ptr(sdd->sgp, j) = sgp; | ||
7415 | } | ||
7416 | } | ||
7417 | |||
7418 | return 0; | ||
7419 | } | ||
7420 | |||
7421 | static void __sdt_free(const struct cpumask *cpu_map) | ||
7422 | { | ||
7423 | struct sched_domain_topology_level *tl; | ||
7424 | int j; | ||
7425 | |||
7426 | for (tl = sched_domain_topology; tl->init; tl++) { | ||
7427 | struct sd_data *sdd = &tl->data; | ||
7428 | |||
7429 | for_each_cpu(j, cpu_map) { | ||
7430 | struct sched_domain *sd = *per_cpu_ptr(sdd->sd, j); | ||
7431 | if (sd && (sd->flags & SD_OVERLAP)) | ||
7432 | free_sched_groups(sd->groups, 0); | ||
7433 | kfree(*per_cpu_ptr(sdd->sd, j)); | ||
7434 | kfree(*per_cpu_ptr(sdd->sg, j)); | ||
7435 | kfree(*per_cpu_ptr(sdd->sgp, j)); | ||
7436 | } | ||
7437 | free_percpu(sdd->sd); | ||
7438 | free_percpu(sdd->sg); | ||
7439 | free_percpu(sdd->sgp); | ||
7440 | } | ||
7441 | } | ||
7442 | |||
7443 | struct sched_domain *build_sched_domain(struct sched_domain_topology_level *tl, | ||
7444 | struct s_data *d, const struct cpumask *cpu_map, | ||
7445 | struct sched_domain_attr *attr, struct sched_domain *child, | ||
7446 | int cpu) | ||
7447 | { | ||
7448 | struct sched_domain *sd = tl->init(tl, cpu); | ||
7449 | if (!sd) | ||
7450 | return child; | ||
7451 | |||
7452 | set_domain_attribute(sd, attr); | ||
7453 | cpumask_and(sched_domain_span(sd), cpu_map, tl->mask(cpu)); | ||
7454 | if (child) { | ||
7455 | sd->level = child->level + 1; | ||
7456 | sched_domain_level_max = max(sched_domain_level_max, sd->level); | ||
7457 | child->parent = sd; | ||
7458 | } | ||
7459 | sd->child = child; | ||
7460 | |||
7461 | return sd; | ||
7462 | } | ||
7463 | |||
7464 | /* | ||
7465 | * Build sched domains for a given set of cpus and attach the sched domains | ||
7466 | * to the individual cpus | ||
7467 | */ | ||
7468 | static int build_sched_domains(const struct cpumask *cpu_map, | ||
7469 | struct sched_domain_attr *attr) | ||
7470 | { | ||
7471 | enum s_alloc alloc_state = sa_none; | ||
7472 | struct sched_domain *sd; | ||
7473 | struct s_data d; | ||
7474 | int i, ret = -ENOMEM; | ||
7475 | |||
7476 | alloc_state = __visit_domain_allocation_hell(&d, cpu_map); | ||
7477 | if (alloc_state != sa_rootdomain) | ||
7478 | goto error; | ||
7479 | |||
7480 | /* Set up domains for cpus specified by the cpu_map. */ | ||
7481 | for_each_cpu(i, cpu_map) { | ||
7482 | struct sched_domain_topology_level *tl; | ||
7483 | |||
7484 | sd = NULL; | ||
7485 | for (tl = sched_domain_topology; tl->init; tl++) { | ||
7486 | sd = build_sched_domain(tl, &d, cpu_map, attr, sd, i); | ||
7487 | if (tl->flags & SDTL_OVERLAP || sched_feat(FORCE_SD_OVERLAP)) | ||
7488 | sd->flags |= SD_OVERLAP; | ||
7489 | if (cpumask_equal(cpu_map, sched_domain_span(sd))) | ||
7490 | break; | ||
7491 | } | ||
7492 | |||
7493 | while (sd->child) | ||
7494 | sd = sd->child; | ||
7495 | |||
7496 | *per_cpu_ptr(d.sd, i) = sd; | ||
7497 | } | ||
7498 | |||
7499 | /* Build the groups for the domains */ | ||
7500 | for_each_cpu(i, cpu_map) { | ||
7501 | for (sd = *per_cpu_ptr(d.sd, i); sd; sd = sd->parent) { | ||
7502 | sd->span_weight = cpumask_weight(sched_domain_span(sd)); | ||
7503 | if (sd->flags & SD_OVERLAP) { | ||
7504 | if (build_overlap_sched_groups(sd, i)) | ||
7505 | goto error; | ||
7506 | } else { | ||
7507 | if (build_sched_groups(sd, i)) | ||
7508 | goto error; | ||
7509 | } | ||
7510 | } | ||
7511 | } | ||
7512 | |||
7513 | /* Calculate CPU power for physical packages and nodes */ | ||
7514 | for (i = nr_cpumask_bits-1; i >= 0; i--) { | ||
7515 | if (!cpumask_test_cpu(i, cpu_map)) | ||
7516 | continue; | ||
7517 | |||
7518 | for (sd = *per_cpu_ptr(d.sd, i); sd; sd = sd->parent) { | ||
7519 | claim_allocations(i, sd); | ||
7520 | init_sched_groups_power(i, sd); | ||
7521 | } | ||
7522 | } | ||
7523 | |||
7524 | /* Attach the domains */ | ||
7525 | rcu_read_lock(); | ||
7526 | for_each_cpu(i, cpu_map) { | ||
7527 | sd = *per_cpu_ptr(d.sd, i); | ||
7528 | cpu_attach_domain(sd, d.rd, i); | ||
7529 | } | ||
7530 | rcu_read_unlock(); | ||
7531 | |||
7532 | ret = 0; | ||
7533 | error: | ||
7534 | __free_domain_allocs(&d, alloc_state, cpu_map); | ||
7535 | return ret; | ||
7536 | } | ||
7537 | |||
7538 | static cpumask_var_t *doms_cur; /* current sched domains */ | ||
7539 | static int ndoms_cur; /* number of sched domains in 'doms_cur' */ | ||
7540 | static struct sched_domain_attr *dattr_cur; | ||
7541 | /* attribues of custom domains in 'doms_cur' */ | ||
7542 | |||
7543 | /* | ||
7544 | * Special case: If a kmalloc of a doms_cur partition (array of | ||
7545 | * cpumask) fails, then fallback to a single sched domain, | ||
7546 | * as determined by the single cpumask fallback_doms. | ||
7547 | */ | ||
7548 | static cpumask_var_t fallback_doms; | ||
7549 | |||
7550 | /* | ||
7551 | * arch_update_cpu_topology lets virtualized architectures update the | ||
7552 | * cpu core maps. It is supposed to return 1 if the topology changed | ||
7553 | * or 0 if it stayed the same. | ||
7554 | */ | ||
7555 | int __attribute__((weak)) arch_update_cpu_topology(void) | ||
7556 | { | ||
7557 | return 0; | ||
7558 | } | ||
7559 | |||
7560 | cpumask_var_t *alloc_sched_domains(unsigned int ndoms) | ||
7561 | { | ||
7562 | int i; | ||
7563 | cpumask_var_t *doms; | ||
7564 | |||
7565 | doms = kmalloc(sizeof(*doms) * ndoms, GFP_KERNEL); | ||
7566 | if (!doms) | ||
7567 | return NULL; | ||
7568 | for (i = 0; i < ndoms; i++) { | ||
7569 | if (!alloc_cpumask_var(&doms[i], GFP_KERNEL)) { | ||
7570 | free_sched_domains(doms, i); | ||
7571 | return NULL; | ||
7572 | } | ||
7573 | } | ||
7574 | return doms; | ||
7575 | } | ||
7576 | |||
7577 | void free_sched_domains(cpumask_var_t doms[], unsigned int ndoms) | ||
7578 | { | ||
7579 | unsigned int i; | ||
7580 | for (i = 0; i < ndoms; i++) | ||
7581 | free_cpumask_var(doms[i]); | ||
7582 | kfree(doms); | ||
7583 | } | ||
7584 | |||
7585 | /* | ||
7586 | * Set up scheduler domains and groups. Callers must hold the hotplug lock. | ||
7587 | * For now this just excludes isolated cpus, but could be used to | ||
7588 | * exclude other special cases in the future. | ||
7589 | */ | ||
7590 | static int init_sched_domains(const struct cpumask *cpu_map) | ||
7591 | { | ||
7592 | int err; | ||
7593 | |||
7594 | arch_update_cpu_topology(); | ||
7595 | ndoms_cur = 1; | ||
7596 | doms_cur = alloc_sched_domains(ndoms_cur); | ||
7597 | if (!doms_cur) | ||
7598 | doms_cur = &fallback_doms; | ||
7599 | cpumask_andnot(doms_cur[0], cpu_map, cpu_isolated_map); | ||
7600 | dattr_cur = NULL; | ||
7601 | err = build_sched_domains(doms_cur[0], NULL); | ||
7602 | register_sched_domain_sysctl(); | ||
7603 | |||
7604 | return err; | ||
7605 | } | ||
7606 | |||
7607 | /* | ||
7608 | * Detach sched domains from a group of cpus specified in cpu_map | ||
7609 | * These cpus will now be attached to the NULL domain | ||
7610 | */ | ||
7611 | static void detach_destroy_domains(const struct cpumask *cpu_map) | ||
7612 | { | ||
7613 | int i; | ||
7614 | |||
7615 | rcu_read_lock(); | ||
7616 | for_each_cpu(i, cpu_map) | ||
7617 | cpu_attach_domain(NULL, &def_root_domain, i); | ||
7618 | rcu_read_unlock(); | ||
7619 | } | ||
7620 | |||
7621 | /* handle null as "default" */ | ||
7622 | static int dattrs_equal(struct sched_domain_attr *cur, int idx_cur, | ||
7623 | struct sched_domain_attr *new, int idx_new) | ||
7624 | { | ||
7625 | struct sched_domain_attr tmp; | ||
7626 | |||
7627 | /* fast path */ | ||
7628 | if (!new && !cur) | ||
7629 | return 1; | ||
7630 | |||
7631 | tmp = SD_ATTR_INIT; | ||
7632 | return !memcmp(cur ? (cur + idx_cur) : &tmp, | ||
7633 | new ? (new + idx_new) : &tmp, | ||
7634 | sizeof(struct sched_domain_attr)); | ||
7635 | } | ||
7636 | |||
7637 | /* | ||
7638 | * Partition sched domains as specified by the 'ndoms_new' | ||
7639 | * cpumasks in the array doms_new[] of cpumasks. This compares | ||
7640 | * doms_new[] to the current sched domain partitioning, doms_cur[]. | ||
7641 | * It destroys each deleted domain and builds each new domain. | ||
7642 | * | ||
7643 | * 'doms_new' is an array of cpumask_var_t's of length 'ndoms_new'. | ||
7644 | * The masks don't intersect (don't overlap.) We should setup one | ||
7645 | * sched domain for each mask. CPUs not in any of the cpumasks will | ||
7646 | * not be load balanced. If the same cpumask appears both in the | ||
7647 | * current 'doms_cur' domains and in the new 'doms_new', we can leave | ||
7648 | * it as it is. | ||
7649 | * | ||
7650 | * The passed in 'doms_new' should be allocated using | ||
7651 | * alloc_sched_domains. This routine takes ownership of it and will | ||
7652 | * free_sched_domains it when done with it. If the caller failed the | ||
7653 | * alloc call, then it can pass in doms_new == NULL && ndoms_new == 1, | ||
7654 | * and partition_sched_domains() will fallback to the single partition | ||
7655 | * 'fallback_doms', it also forces the domains to be rebuilt. | ||
7656 | * | ||
7657 | * If doms_new == NULL it will be replaced with cpu_online_mask. | ||
7658 | * ndoms_new == 0 is a special case for destroying existing domains, | ||
7659 | * and it will not create the default domain. | ||
7660 | * | ||
7661 | * Call with hotplug lock held | ||
7662 | */ | ||
7663 | void partition_sched_domains(int ndoms_new, cpumask_var_t doms_new[], | ||
7664 | struct sched_domain_attr *dattr_new) | ||
7665 | { | ||
7666 | int i, j, n; | ||
7667 | int new_topology; | ||
7668 | |||
7669 | mutex_lock(&sched_domains_mutex); | ||
7670 | |||
7671 | /* always unregister in case we don't destroy any domains */ | ||
7672 | unregister_sched_domain_sysctl(); | ||
7673 | |||
7674 | /* Let architecture update cpu core mappings. */ | ||
7675 | new_topology = arch_update_cpu_topology(); | ||
7676 | |||
7677 | n = doms_new ? ndoms_new : 0; | ||
7678 | |||
7679 | /* Destroy deleted domains */ | ||
7680 | for (i = 0; i < ndoms_cur; i++) { | ||
7681 | for (j = 0; j < n && !new_topology; j++) { | ||
7682 | if (cpumask_equal(doms_cur[i], doms_new[j]) | ||
7683 | && dattrs_equal(dattr_cur, i, dattr_new, j)) | ||
7684 | goto match1; | ||
7685 | } | ||
7686 | /* no match - a current sched domain not in new doms_new[] */ | ||
7687 | detach_destroy_domains(doms_cur[i]); | ||
7688 | match1: | ||
7689 | ; | ||
7690 | } | ||
7691 | |||
7692 | if (doms_new == NULL) { | ||
7693 | ndoms_cur = 0; | ||
7694 | doms_new = &fallback_doms; | ||
7695 | cpumask_andnot(doms_new[0], cpu_active_mask, cpu_isolated_map); | ||
7696 | WARN_ON_ONCE(dattr_new); | ||
7697 | } | ||
7698 | |||
7699 | /* Build new domains */ | ||
7700 | for (i = 0; i < ndoms_new; i++) { | ||
7701 | for (j = 0; j < ndoms_cur && !new_topology; j++) { | ||
7702 | if (cpumask_equal(doms_new[i], doms_cur[j]) | ||
7703 | && dattrs_equal(dattr_new, i, dattr_cur, j)) | ||
7704 | goto match2; | ||
7705 | } | ||
7706 | /* no match - add a new doms_new */ | ||
7707 | build_sched_domains(doms_new[i], dattr_new ? dattr_new + i : NULL); | ||
7708 | match2: | ||
7709 | ; | ||
7710 | } | ||
7711 | |||
7712 | /* Remember the new sched domains */ | ||
7713 | if (doms_cur != &fallback_doms) | ||
7714 | free_sched_domains(doms_cur, ndoms_cur); | ||
7715 | kfree(dattr_cur); /* kfree(NULL) is safe */ | ||
7716 | doms_cur = doms_new; | ||
7717 | dattr_cur = dattr_new; | ||
7718 | ndoms_cur = ndoms_new; | ||
7719 | |||
7720 | register_sched_domain_sysctl(); | ||
7721 | |||
7722 | mutex_unlock(&sched_domains_mutex); | ||
7723 | } | ||
7724 | |||
7725 | #if defined(CONFIG_SCHED_MC) || defined(CONFIG_SCHED_SMT) | ||
7726 | static void reinit_sched_domains(void) | ||
7727 | { | ||
7728 | get_online_cpus(); | ||
7729 | |||
7730 | /* Destroy domains first to force the rebuild */ | ||
7731 | partition_sched_domains(0, NULL, NULL); | ||
7732 | |||
7733 | rebuild_sched_domains(); | ||
7734 | put_online_cpus(); | ||
7735 | } | ||
7736 | |||
7737 | static ssize_t sched_power_savings_store(const char *buf, size_t count, int smt) | ||
7738 | { | ||
7739 | unsigned int level = 0; | ||
7740 | |||
7741 | if (sscanf(buf, "%u", &level) != 1) | ||
7742 | return -EINVAL; | ||
7743 | |||
7744 | /* | ||
7745 | * level is always be positive so don't check for | ||
7746 | * level < POWERSAVINGS_BALANCE_NONE which is 0 | ||
7747 | * What happens on 0 or 1 byte write, | ||
7748 | * need to check for count as well? | ||
7749 | */ | ||
7750 | |||
7751 | if (level >= MAX_POWERSAVINGS_BALANCE_LEVELS) | ||
7752 | return -EINVAL; | ||
7753 | |||
7754 | if (smt) | ||
7755 | sched_smt_power_savings = level; | ||
7756 | else | ||
7757 | sched_mc_power_savings = level; | ||
7758 | |||
7759 | reinit_sched_domains(); | ||
7760 | |||
7761 | return count; | ||
7762 | } | ||
7763 | |||
7764 | #ifdef CONFIG_SCHED_MC | ||
7765 | static ssize_t sched_mc_power_savings_show(struct sysdev_class *class, | ||
7766 | struct sysdev_class_attribute *attr, | ||
7767 | char *page) | ||
7768 | { | ||
7769 | return sprintf(page, "%u\n", sched_mc_power_savings); | ||
7770 | } | ||
7771 | static ssize_t sched_mc_power_savings_store(struct sysdev_class *class, | ||
7772 | struct sysdev_class_attribute *attr, | ||
7773 | const char *buf, size_t count) | ||
7774 | { | ||
7775 | return sched_power_savings_store(buf, count, 0); | ||
7776 | } | ||
7777 | static SYSDEV_CLASS_ATTR(sched_mc_power_savings, 0644, | ||
7778 | sched_mc_power_savings_show, | ||
7779 | sched_mc_power_savings_store); | ||
7780 | #endif | ||
7781 | |||
7782 | #ifdef CONFIG_SCHED_SMT | ||
7783 | static ssize_t sched_smt_power_savings_show(struct sysdev_class *dev, | ||
7784 | struct sysdev_class_attribute *attr, | ||
7785 | char *page) | ||
7786 | { | ||
7787 | return sprintf(page, "%u\n", sched_smt_power_savings); | ||
7788 | } | ||
7789 | static ssize_t sched_smt_power_savings_store(struct sysdev_class *dev, | ||
7790 | struct sysdev_class_attribute *attr, | ||
7791 | const char *buf, size_t count) | ||
7792 | { | ||
7793 | return sched_power_savings_store(buf, count, 1); | ||
7794 | } | ||
7795 | static SYSDEV_CLASS_ATTR(sched_smt_power_savings, 0644, | ||
7796 | sched_smt_power_savings_show, | ||
7797 | sched_smt_power_savings_store); | ||
7798 | #endif | ||
7799 | |||
7800 | int __init sched_create_sysfs_power_savings_entries(struct sysdev_class *cls) | ||
7801 | { | ||
7802 | int err = 0; | ||
7803 | |||
7804 | #ifdef CONFIG_SCHED_SMT | ||
7805 | if (smt_capable()) | ||
7806 | err = sysfs_create_file(&cls->kset.kobj, | ||
7807 | &attr_sched_smt_power_savings.attr); | ||
7808 | #endif | ||
7809 | #ifdef CONFIG_SCHED_MC | ||
7810 | if (!err && mc_capable()) | ||
7811 | err = sysfs_create_file(&cls->kset.kobj, | ||
7812 | &attr_sched_mc_power_savings.attr); | ||
7813 | #endif | ||
7814 | return err; | ||
7815 | } | ||
7816 | #endif /* CONFIG_SCHED_MC || CONFIG_SCHED_SMT */ | ||
7817 | |||
7818 | /* | ||
7819 | * Update cpusets according to cpu_active mask. If cpusets are | ||
7820 | * disabled, cpuset_update_active_cpus() becomes a simple wrapper | ||
7821 | * around partition_sched_domains(). | ||
7822 | */ | ||
7823 | static int cpuset_cpu_active(struct notifier_block *nfb, unsigned long action, | ||
7824 | void *hcpu) | ||
7825 | { | ||
7826 | switch (action & ~CPU_TASKS_FROZEN) { | ||
7827 | case CPU_ONLINE: | ||
7828 | case CPU_DOWN_FAILED: | ||
7829 | cpuset_update_active_cpus(); | ||
7830 | return NOTIFY_OK; | ||
7831 | default: | ||
7832 | return NOTIFY_DONE; | ||
7833 | } | ||
7834 | } | ||
7835 | |||
7836 | static int cpuset_cpu_inactive(struct notifier_block *nfb, unsigned long action, | ||
7837 | void *hcpu) | ||
7838 | { | ||
7839 | switch (action & ~CPU_TASKS_FROZEN) { | ||
7840 | case CPU_DOWN_PREPARE: | ||
7841 | cpuset_update_active_cpus(); | ||
7842 | return NOTIFY_OK; | ||
7843 | default: | ||
7844 | return NOTIFY_DONE; | ||
7845 | } | ||
7846 | } | ||
7847 | |||
7848 | static int update_runtime(struct notifier_block *nfb, | ||
7849 | unsigned long action, void *hcpu) | ||
7850 | { | ||
7851 | int cpu = (int)(long)hcpu; | ||
7852 | |||
7853 | switch (action) { | ||
7854 | case CPU_DOWN_PREPARE: | ||
7855 | case CPU_DOWN_PREPARE_FROZEN: | ||
7856 | disable_runtime(cpu_rq(cpu)); | ||
7857 | return NOTIFY_OK; | ||
7858 | |||
7859 | case CPU_DOWN_FAILED: | ||
7860 | case CPU_DOWN_FAILED_FROZEN: | ||
7861 | case CPU_ONLINE: | ||
7862 | case CPU_ONLINE_FROZEN: | ||
7863 | enable_runtime(cpu_rq(cpu)); | ||
7864 | return NOTIFY_OK; | ||
7865 | |||
7866 | default: | ||
7867 | return NOTIFY_DONE; | ||
7868 | } | ||
7869 | } | ||
7870 | |||
7871 | void __init sched_init_smp(void) | ||
7872 | { | ||
7873 | cpumask_var_t non_isolated_cpus; | ||
7874 | |||
7875 | alloc_cpumask_var(&non_isolated_cpus, GFP_KERNEL); | ||
7876 | alloc_cpumask_var(&fallback_doms, GFP_KERNEL); | ||
7877 | |||
7878 | get_online_cpus(); | ||
7879 | mutex_lock(&sched_domains_mutex); | ||
7880 | init_sched_domains(cpu_active_mask); | ||
7881 | cpumask_andnot(non_isolated_cpus, cpu_possible_mask, cpu_isolated_map); | ||
7882 | if (cpumask_empty(non_isolated_cpus)) | ||
7883 | cpumask_set_cpu(smp_processor_id(), non_isolated_cpus); | ||
7884 | mutex_unlock(&sched_domains_mutex); | ||
7885 | put_online_cpus(); | ||
7886 | |||
7887 | hotcpu_notifier(cpuset_cpu_active, CPU_PRI_CPUSET_ACTIVE); | ||
7888 | hotcpu_notifier(cpuset_cpu_inactive, CPU_PRI_CPUSET_INACTIVE); | ||
7889 | |||
7890 | /* RT runtime code needs to handle some hotplug events */ | ||
7891 | hotcpu_notifier(update_runtime, 0); | ||
7892 | |||
7893 | init_hrtick(); | ||
7894 | |||
7895 | /* Move init over to a non-isolated CPU */ | ||
7896 | if (set_cpus_allowed_ptr(current, non_isolated_cpus) < 0) | ||
7897 | BUG(); | ||
7898 | sched_init_granularity(); | ||
7899 | free_cpumask_var(non_isolated_cpus); | ||
7900 | |||
7901 | init_sched_rt_class(); | ||
7902 | } | ||
7903 | #else | ||
7904 | void __init sched_init_smp(void) | ||
7905 | { | ||
7906 | sched_init_granularity(); | ||
7907 | } | ||
7908 | #endif /* CONFIG_SMP */ | ||
7909 | |||
7910 | const_debug unsigned int sysctl_timer_migration = 1; | ||
7911 | |||
7912 | int in_sched_functions(unsigned long addr) | ||
7913 | { | ||
7914 | return in_lock_functions(addr) || | ||
7915 | (addr >= (unsigned long)__sched_text_start | ||
7916 | && addr < (unsigned long)__sched_text_end); | ||
7917 | } | ||
7918 | |||
7919 | static void init_cfs_rq(struct cfs_rq *cfs_rq) | ||
7920 | { | ||
7921 | cfs_rq->tasks_timeline = RB_ROOT; | ||
7922 | INIT_LIST_HEAD(&cfs_rq->tasks); | ||
7923 | cfs_rq->min_vruntime = (u64)(-(1LL << 20)); | ||
7924 | #ifndef CONFIG_64BIT | ||
7925 | cfs_rq->min_vruntime_copy = cfs_rq->min_vruntime; | ||
7926 | #endif | ||
7927 | } | ||
7928 | |||
7929 | static void init_rt_rq(struct rt_rq *rt_rq, struct rq *rq) | ||
7930 | { | ||
7931 | struct rt_prio_array *array; | ||
7932 | int i; | ||
7933 | |||
7934 | array = &rt_rq->active; | ||
7935 | for (i = 0; i < MAX_RT_PRIO; i++) { | ||
7936 | INIT_LIST_HEAD(array->queue + i); | ||
7937 | __clear_bit(i, array->bitmap); | ||
7938 | } | ||
7939 | /* delimiter for bitsearch: */ | ||
7940 | __set_bit(MAX_RT_PRIO, array->bitmap); | ||
7941 | |||
7942 | #if defined CONFIG_SMP | ||
7943 | rt_rq->highest_prio.curr = MAX_RT_PRIO; | ||
7944 | rt_rq->highest_prio.next = MAX_RT_PRIO; | ||
7945 | rt_rq->rt_nr_migratory = 0; | ||
7946 | rt_rq->overloaded = 0; | ||
7947 | plist_head_init(&rt_rq->pushable_tasks); | ||
7948 | #endif | ||
7949 | |||
7950 | rt_rq->rt_time = 0; | ||
7951 | rt_rq->rt_throttled = 0; | ||
7952 | rt_rq->rt_runtime = 0; | ||
7953 | raw_spin_lock_init(&rt_rq->rt_runtime_lock); | ||
7954 | } | ||
7955 | |||
7956 | #ifdef CONFIG_FAIR_GROUP_SCHED | ||
7957 | static void init_tg_cfs_entry(struct task_group *tg, struct cfs_rq *cfs_rq, | ||
7958 | struct sched_entity *se, int cpu, | ||
7959 | struct sched_entity *parent) | ||
7960 | { | ||
7961 | struct rq *rq = cpu_rq(cpu); | ||
7962 | |||
7963 | cfs_rq->tg = tg; | ||
7964 | cfs_rq->rq = rq; | ||
7965 | #ifdef CONFIG_SMP | ||
7966 | /* allow initial update_cfs_load() to truncate */ | ||
7967 | cfs_rq->load_stamp = 1; | ||
7968 | #endif | ||
7969 | |||
7970 | tg->cfs_rq[cpu] = cfs_rq; | ||
7971 | tg->se[cpu] = se; | ||
7972 | |||
7973 | /* se could be NULL for root_task_group */ | ||
7974 | if (!se) | ||
7975 | return; | ||
7976 | |||
7977 | if (!parent) | ||
7978 | se->cfs_rq = &rq->cfs; | ||
7979 | else | ||
7980 | se->cfs_rq = parent->my_q; | ||
7981 | |||
7982 | se->my_q = cfs_rq; | ||
7983 | update_load_set(&se->load, 0); | ||
7984 | se->parent = parent; | ||
7985 | } | ||
7986 | #endif | ||
7987 | |||
7988 | #ifdef CONFIG_RT_GROUP_SCHED | ||
7989 | static void init_tg_rt_entry(struct task_group *tg, struct rt_rq *rt_rq, | ||
7990 | struct sched_rt_entity *rt_se, int cpu, | ||
7991 | struct sched_rt_entity *parent) | ||
7992 | { | ||
7993 | struct rq *rq = cpu_rq(cpu); | ||
7994 | |||
7995 | rt_rq->highest_prio.curr = MAX_RT_PRIO; | ||
7996 | rt_rq->rt_nr_boosted = 0; | ||
7997 | rt_rq->rq = rq; | ||
7998 | rt_rq->tg = tg; | ||
7999 | |||
8000 | tg->rt_rq[cpu] = rt_rq; | ||
8001 | tg->rt_se[cpu] = rt_se; | ||
8002 | |||
8003 | if (!rt_se) | ||
8004 | return; | ||
8005 | |||
8006 | if (!parent) | ||
8007 | rt_se->rt_rq = &rq->rt; | ||
8008 | else | ||
8009 | rt_se->rt_rq = parent->my_q; | ||
8010 | |||
8011 | rt_se->my_q = rt_rq; | ||
8012 | rt_se->parent = parent; | ||
8013 | INIT_LIST_HEAD(&rt_se->run_list); | ||
8014 | } | ||
8015 | #endif | ||
8016 | |||
8017 | void __init sched_init(void) | ||
8018 | { | ||
8019 | int i, j; | ||
8020 | unsigned long alloc_size = 0, ptr; | ||
8021 | |||
8022 | #ifdef CONFIG_FAIR_GROUP_SCHED | ||
8023 | alloc_size += 2 * nr_cpu_ids * sizeof(void **); | ||
8024 | #endif | ||
8025 | #ifdef CONFIG_RT_GROUP_SCHED | ||
8026 | alloc_size += 2 * nr_cpu_ids * sizeof(void **); | ||
8027 | #endif | ||
8028 | #ifdef CONFIG_CPUMASK_OFFSTACK | ||
8029 | alloc_size += num_possible_cpus() * cpumask_size(); | ||
8030 | #endif | ||
8031 | if (alloc_size) { | ||
8032 | ptr = (unsigned long)kzalloc(alloc_size, GFP_NOWAIT); | ||
8033 | |||
8034 | #ifdef CONFIG_FAIR_GROUP_SCHED | ||
8035 | root_task_group.se = (struct sched_entity **)ptr; | ||
8036 | ptr += nr_cpu_ids * sizeof(void **); | ||
8037 | |||
8038 | root_task_group.cfs_rq = (struct cfs_rq **)ptr; | ||
8039 | ptr += nr_cpu_ids * sizeof(void **); | ||
8040 | |||
8041 | #endif /* CONFIG_FAIR_GROUP_SCHED */ | ||
8042 | #ifdef CONFIG_RT_GROUP_SCHED | ||
8043 | root_task_group.rt_se = (struct sched_rt_entity **)ptr; | ||
8044 | ptr += nr_cpu_ids * sizeof(void **); | ||
8045 | |||
8046 | root_task_group.rt_rq = (struct rt_rq **)ptr; | ||
8047 | ptr += nr_cpu_ids * sizeof(void **); | ||
8048 | |||
8049 | #endif /* CONFIG_RT_GROUP_SCHED */ | ||
8050 | #ifdef CONFIG_CPUMASK_OFFSTACK | ||
8051 | for_each_possible_cpu(i) { | ||
8052 | per_cpu(load_balance_tmpmask, i) = (void *)ptr; | ||
8053 | ptr += cpumask_size(); | ||
8054 | } | ||
8055 | #endif /* CONFIG_CPUMASK_OFFSTACK */ | ||
8056 | } | ||
8057 | |||
8058 | #ifdef CONFIG_SMP | ||
8059 | init_defrootdomain(); | ||
8060 | #endif | ||
8061 | |||
8062 | init_rt_bandwidth(&def_rt_bandwidth, | ||
8063 | global_rt_period(), global_rt_runtime()); | ||
8064 | |||
8065 | #ifdef CONFIG_RT_GROUP_SCHED | ||
8066 | init_rt_bandwidth(&root_task_group.rt_bandwidth, | ||
8067 | global_rt_period(), global_rt_runtime()); | ||
8068 | #endif /* CONFIG_RT_GROUP_SCHED */ | ||
8069 | |||
8070 | #ifdef CONFIG_CGROUP_SCHED | ||
8071 | list_add(&root_task_group.list, &task_groups); | ||
8072 | INIT_LIST_HEAD(&root_task_group.children); | ||
8073 | autogroup_init(&init_task); | ||
8074 | #endif /* CONFIG_CGROUP_SCHED */ | ||
8075 | |||
8076 | for_each_possible_cpu(i) { | ||
8077 | struct rq *rq; | ||
8078 | |||
8079 | rq = cpu_rq(i); | ||
8080 | raw_spin_lock_init(&rq->lock); | ||
8081 | rq->nr_running = 0; | ||
8082 | rq->calc_load_active = 0; | ||
8083 | rq->calc_load_update = jiffies + LOAD_FREQ; | ||
8084 | init_cfs_rq(&rq->cfs); | ||
8085 | init_rt_rq(&rq->rt, rq); | ||
8086 | #ifdef CONFIG_FAIR_GROUP_SCHED | ||
8087 | root_task_group.shares = root_task_group_load; | ||
8088 | INIT_LIST_HEAD(&rq->leaf_cfs_rq_list); | ||
8089 | /* | ||
8090 | * How much cpu bandwidth does root_task_group get? | ||
8091 | * | ||
8092 | * In case of task-groups formed thr' the cgroup filesystem, it | ||
8093 | * gets 100% of the cpu resources in the system. This overall | ||
8094 | * system cpu resource is divided among the tasks of | ||
8095 | * root_task_group and its child task-groups in a fair manner, | ||
8096 | * based on each entity's (task or task-group's) weight | ||
8097 | * (se->load.weight). | ||
8098 | * | ||
8099 | * In other words, if root_task_group has 10 tasks of weight | ||
8100 | * 1024) and two child groups A0 and A1 (of weight 1024 each), | ||
8101 | * then A0's share of the cpu resource is: | ||
8102 | * | ||
8103 | * A0's bandwidth = 1024 / (10*1024 + 1024 + 1024) = 8.33% | ||
8104 | * | ||
8105 | * We achieve this by letting root_task_group's tasks sit | ||
8106 | * directly in rq->cfs (i.e root_task_group->se[] = NULL). | ||
8107 | */ | ||
8108 | init_tg_cfs_entry(&root_task_group, &rq->cfs, NULL, i, NULL); | ||
8109 | #endif /* CONFIG_FAIR_GROUP_SCHED */ | ||
8110 | |||
8111 | rq->rt.rt_runtime = def_rt_bandwidth.rt_runtime; | ||
8112 | #ifdef CONFIG_RT_GROUP_SCHED | ||
8113 | INIT_LIST_HEAD(&rq->leaf_rt_rq_list); | ||
8114 | init_tg_rt_entry(&root_task_group, &rq->rt, NULL, i, NULL); | ||
8115 | #endif | ||
8116 | |||
8117 | for (j = 0; j < CPU_LOAD_IDX_MAX; j++) | ||
8118 | rq->cpu_load[j] = 0; | ||
8119 | |||
8120 | rq->last_load_update_tick = jiffies; | ||
8121 | |||
8122 | #ifdef CONFIG_SMP | ||
8123 | rq->sd = NULL; | ||
8124 | rq->rd = NULL; | ||
8125 | rq->cpu_power = SCHED_POWER_SCALE; | ||
8126 | rq->post_schedule = 0; | ||
8127 | rq->active_balance = 0; | ||
8128 | rq->next_balance = jiffies; | ||
8129 | rq->push_cpu = 0; | ||
8130 | rq->cpu = i; | ||
8131 | rq->online = 0; | ||
8132 | rq->idle_stamp = 0; | ||
8133 | rq->avg_idle = 2*sysctl_sched_migration_cost; | ||
8134 | rq_attach_root(rq, &def_root_domain); | ||
8135 | #ifdef CONFIG_NO_HZ | ||
8136 | rq->nohz_balance_kick = 0; | ||
8137 | init_sched_softirq_csd(&per_cpu(remote_sched_softirq_cb, i)); | ||
8138 | #endif | ||
8139 | #endif | ||
8140 | init_rq_hrtick(rq); | ||
8141 | atomic_set(&rq->nr_iowait, 0); | ||
8142 | } | ||
8143 | |||
8144 | set_load_weight(&init_task); | ||
8145 | |||
8146 | #ifdef CONFIG_PREEMPT_NOTIFIERS | ||
8147 | INIT_HLIST_HEAD(&init_task.preempt_notifiers); | ||
8148 | #endif | ||
8149 | |||
8150 | #ifdef CONFIG_SMP | ||
8151 | open_softirq(SCHED_SOFTIRQ, run_rebalance_domains); | ||
8152 | #endif | ||
8153 | |||
8154 | #ifdef CONFIG_RT_MUTEXES | ||
8155 | plist_head_init(&init_task.pi_waiters); | ||
8156 | #endif | ||
8157 | |||
8158 | /* | ||
8159 | * The boot idle thread does lazy MMU switching as well: | ||
8160 | */ | ||
8161 | atomic_inc(&init_mm.mm_count); | ||
8162 | enter_lazy_tlb(&init_mm, current); | ||
8163 | |||
8164 | /* | ||
8165 | * Make us the idle thread. Technically, schedule() should not be | ||
8166 | * called from this thread, however somewhere below it might be, | ||
8167 | * but because we are the idle thread, we just pick up running again | ||
8168 | * when this runqueue becomes "idle". | ||
8169 | */ | ||
8170 | init_idle(current, smp_processor_id()); | ||
8171 | |||
8172 | calc_load_update = jiffies + LOAD_FREQ; | ||
8173 | |||
8174 | /* | ||
8175 | * During early bootup we pretend to be a normal task: | ||
8176 | */ | ||
8177 | current->sched_class = &fair_sched_class; | ||
8178 | |||
8179 | /* Allocate the nohz_cpu_mask if CONFIG_CPUMASK_OFFSTACK */ | ||
8180 | zalloc_cpumask_var(&nohz_cpu_mask, GFP_NOWAIT); | ||
8181 | #ifdef CONFIG_SMP | ||
8182 | zalloc_cpumask_var(&sched_domains_tmpmask, GFP_NOWAIT); | ||
8183 | #ifdef CONFIG_NO_HZ | ||
8184 | zalloc_cpumask_var(&nohz.idle_cpus_mask, GFP_NOWAIT); | ||
8185 | alloc_cpumask_var(&nohz.grp_idle_mask, GFP_NOWAIT); | ||
8186 | atomic_set(&nohz.load_balancer, nr_cpu_ids); | ||
8187 | atomic_set(&nohz.first_pick_cpu, nr_cpu_ids); | ||
8188 | atomic_set(&nohz.second_pick_cpu, nr_cpu_ids); | ||
8189 | nohz.next_balance = jiffies; | ||
8190 | #endif | ||
8191 | /* May be allocated at isolcpus cmdline parse time */ | ||
8192 | if (cpu_isolated_map == NULL) | ||
8193 | zalloc_cpumask_var(&cpu_isolated_map, GFP_NOWAIT); | ||
8194 | #endif /* SMP */ | ||
8195 | |||
8196 | scheduler_running = 1; | ||
8197 | } | ||
8198 | |||
8199 | #ifdef CONFIG_DEBUG_ATOMIC_SLEEP | ||
8200 | static inline int preempt_count_equals(int preempt_offset) | ||
8201 | { | ||
8202 | int nested = (preempt_count() & ~PREEMPT_ACTIVE) + rcu_preempt_depth(); | ||
8203 | |||
8204 | return (nested == preempt_offset); | ||
8205 | } | ||
8206 | |||
8207 | static int __might_sleep_init_called; | ||
8208 | int __init __might_sleep_init(void) | ||
8209 | { | ||
8210 | __might_sleep_init_called = 1; | ||
8211 | return 0; | ||
8212 | } | ||
8213 | early_initcall(__might_sleep_init); | ||
8214 | |||
8215 | void __might_sleep(const char *file, int line, int preempt_offset) | ||
8216 | { | ||
8217 | static unsigned long prev_jiffy; /* ratelimiting */ | ||
8218 | |||
8219 | if ((preempt_count_equals(preempt_offset) && !irqs_disabled()) || | ||
8220 | oops_in_progress) | ||
8221 | return; | ||
8222 | if (system_state != SYSTEM_RUNNING && | ||
8223 | (!__might_sleep_init_called || system_state != SYSTEM_BOOTING)) | ||
8224 | return; | ||
8225 | if (time_before(jiffies, prev_jiffy + HZ) && prev_jiffy) | ||
8226 | return; | ||
8227 | prev_jiffy = jiffies; | ||
8228 | |||
8229 | printk(KERN_ERR | ||
8230 | "BUG: sleeping function called from invalid context at %s:%d\n", | ||
8231 | file, line); | ||
8232 | printk(KERN_ERR | ||
8233 | "in_atomic(): %d, irqs_disabled(): %d, pid: %d, name: %s\n", | ||
8234 | in_atomic(), irqs_disabled(), | ||
8235 | current->pid, current->comm); | ||
8236 | |||
8237 | debug_show_held_locks(current); | ||
8238 | if (irqs_disabled()) | ||
8239 | print_irqtrace_events(current); | ||
8240 | dump_stack(); | ||
8241 | } | ||
8242 | EXPORT_SYMBOL(__might_sleep); | ||
8243 | #endif | ||
8244 | |||
8245 | #ifdef CONFIG_MAGIC_SYSRQ | ||
8246 | static void normalize_task(struct rq *rq, struct task_struct *p) | ||
8247 | { | ||
8248 | const struct sched_class *prev_class = p->sched_class; | ||
8249 | int old_prio = p->prio; | ||
8250 | int on_rq; | ||
8251 | |||
8252 | on_rq = p->on_rq; | ||
8253 | if (on_rq) | ||
8254 | deactivate_task(rq, p, 0); | ||
8255 | __setscheduler(rq, p, SCHED_NORMAL, 0); | ||
8256 | if (on_rq) { | ||
8257 | activate_task(rq, p, 0); | ||
8258 | resched_task(rq->curr); | ||
8259 | } | ||
8260 | |||
8261 | check_class_changed(rq, p, prev_class, old_prio); | ||
8262 | } | ||
8263 | |||
8264 | void normalize_rt_tasks(void) | ||
8265 | { | ||
8266 | struct task_struct *g, *p; | ||
8267 | unsigned long flags; | ||
8268 | struct rq *rq; | ||
8269 | |||
8270 | read_lock_irqsave(&tasklist_lock, flags); | ||
8271 | do_each_thread(g, p) { | ||
8272 | /* | ||
8273 | * Only normalize user tasks: | ||
8274 | */ | ||
8275 | if (!p->mm) | ||
8276 | continue; | ||
8277 | |||
8278 | p->se.exec_start = 0; | ||
8279 | #ifdef CONFIG_SCHEDSTATS | ||
8280 | p->se.statistics.wait_start = 0; | ||
8281 | p->se.statistics.sleep_start = 0; | ||
8282 | p->se.statistics.block_start = 0; | ||
8283 | #endif | ||
8284 | |||
8285 | if (!rt_task(p)) { | ||
8286 | /* | ||
8287 | * Renice negative nice level userspace | ||
8288 | * tasks back to 0: | ||
8289 | */ | ||
8290 | if (TASK_NICE(p) < 0 && p->mm) | ||
8291 | set_user_nice(p, 0); | ||
8292 | continue; | ||
8293 | } | ||
8294 | |||
8295 | raw_spin_lock(&p->pi_lock); | ||
8296 | rq = __task_rq_lock(p); | ||
8297 | |||
8298 | normalize_task(rq, p); | ||
8299 | |||
8300 | __task_rq_unlock(rq); | ||
8301 | raw_spin_unlock(&p->pi_lock); | ||
8302 | } while_each_thread(g, p); | ||
8303 | |||
8304 | read_unlock_irqrestore(&tasklist_lock, flags); | ||
8305 | } | ||
8306 | |||
8307 | #endif /* CONFIG_MAGIC_SYSRQ */ | ||
8308 | |||
8309 | #if defined(CONFIG_IA64) || defined(CONFIG_KGDB_KDB) | ||
8310 | /* | ||
8311 | * These functions are only useful for the IA64 MCA handling, or kdb. | ||
8312 | * | ||
8313 | * They can only be called when the whole system has been | ||
8314 | * stopped - every CPU needs to be quiescent, and no scheduling | ||
8315 | * activity can take place. Using them for anything else would | ||
8316 | * be a serious bug, and as a result, they aren't even visible | ||
8317 | * under any other configuration. | ||
8318 | */ | ||
8319 | |||
8320 | /** | ||
8321 | * curr_task - return the current task for a given cpu. | ||
8322 | * @cpu: the processor in question. | ||
8323 | * | ||
8324 | * ONLY VALID WHEN THE WHOLE SYSTEM IS STOPPED! | ||
8325 | */ | ||
8326 | struct task_struct *curr_task(int cpu) | ||
8327 | { | ||
8328 | return cpu_curr(cpu); | ||
8329 | } | ||
8330 | |||
8331 | #endif /* defined(CONFIG_IA64) || defined(CONFIG_KGDB_KDB) */ | ||
8332 | |||
8333 | #ifdef CONFIG_IA64 | ||
8334 | /** | ||
8335 | * set_curr_task - set the current task for a given cpu. | ||
8336 | * @cpu: the processor in question. | ||
8337 | * @p: the task pointer to set. | ||
8338 | * | ||
8339 | * Description: This function must only be used when non-maskable interrupts | ||
8340 | * are serviced on a separate stack. It allows the architecture to switch the | ||
8341 | * notion of the current task on a cpu in a non-blocking manner. This function | ||
8342 | * must be called with all CPU's synchronized, and interrupts disabled, the | ||
8343 | * and caller must save the original value of the current task (see | ||
8344 | * curr_task() above) and restore that value before reenabling interrupts and | ||
8345 | * re-starting the system. | ||
8346 | * | ||
8347 | * ONLY VALID WHEN THE WHOLE SYSTEM IS STOPPED! | ||
8348 | */ | ||
8349 | void set_curr_task(int cpu, struct task_struct *p) | ||
8350 | { | ||
8351 | cpu_curr(cpu) = p; | ||
8352 | } | ||
8353 | |||
8354 | #endif | ||
8355 | |||
8356 | #ifdef CONFIG_FAIR_GROUP_SCHED | ||
8357 | static void free_fair_sched_group(struct task_group *tg) | ||
8358 | { | ||
8359 | int i; | ||
8360 | |||
8361 | for_each_possible_cpu(i) { | ||
8362 | if (tg->cfs_rq) | ||
8363 | kfree(tg->cfs_rq[i]); | ||
8364 | if (tg->se) | ||
8365 | kfree(tg->se[i]); | ||
8366 | } | ||
8367 | |||
8368 | kfree(tg->cfs_rq); | ||
8369 | kfree(tg->se); | ||
8370 | } | ||
8371 | |||
8372 | static | ||
8373 | int alloc_fair_sched_group(struct task_group *tg, struct task_group *parent) | ||
8374 | { | ||
8375 | struct cfs_rq *cfs_rq; | ||
8376 | struct sched_entity *se; | ||
8377 | int i; | ||
8378 | |||
8379 | tg->cfs_rq = kzalloc(sizeof(cfs_rq) * nr_cpu_ids, GFP_KERNEL); | ||
8380 | if (!tg->cfs_rq) | ||
8381 | goto err; | ||
8382 | tg->se = kzalloc(sizeof(se) * nr_cpu_ids, GFP_KERNEL); | ||
8383 | if (!tg->se) | ||
8384 | goto err; | ||
8385 | |||
8386 | tg->shares = NICE_0_LOAD; | ||
8387 | |||
8388 | for_each_possible_cpu(i) { | ||
8389 | cfs_rq = kzalloc_node(sizeof(struct cfs_rq), | ||
8390 | GFP_KERNEL, cpu_to_node(i)); | ||
8391 | if (!cfs_rq) | ||
8392 | goto err; | ||
8393 | |||
8394 | se = kzalloc_node(sizeof(struct sched_entity), | ||
8395 | GFP_KERNEL, cpu_to_node(i)); | ||
8396 | if (!se) | ||
8397 | goto err_free_rq; | ||
8398 | |||
8399 | init_cfs_rq(cfs_rq); | ||
8400 | init_tg_cfs_entry(tg, cfs_rq, se, i, parent->se[i]); | ||
8401 | } | ||
8402 | |||
8403 | return 1; | ||
8404 | |||
8405 | err_free_rq: | ||
8406 | kfree(cfs_rq); | ||
8407 | err: | ||
8408 | return 0; | ||
8409 | } | ||
8410 | |||
8411 | static inline void unregister_fair_sched_group(struct task_group *tg, int cpu) | ||
8412 | { | ||
8413 | struct rq *rq = cpu_rq(cpu); | ||
8414 | unsigned long flags; | ||
8415 | |||
8416 | /* | ||
8417 | * Only empty task groups can be destroyed; so we can speculatively | ||
8418 | * check on_list without danger of it being re-added. | ||
8419 | */ | ||
8420 | if (!tg->cfs_rq[cpu]->on_list) | ||
8421 | return; | ||
8422 | |||
8423 | raw_spin_lock_irqsave(&rq->lock, flags); | ||
8424 | list_del_leaf_cfs_rq(tg->cfs_rq[cpu]); | ||
8425 | raw_spin_unlock_irqrestore(&rq->lock, flags); | ||
8426 | } | ||
8427 | #else /* !CONFIG_FAIR_GROUP_SCHED */ | ||
8428 | static inline void free_fair_sched_group(struct task_group *tg) | ||
8429 | { | ||
8430 | } | ||
8431 | |||
8432 | static inline | ||
8433 | int alloc_fair_sched_group(struct task_group *tg, struct task_group *parent) | ||
8434 | { | ||
8435 | return 1; | ||
8436 | } | ||
8437 | |||
8438 | static inline void unregister_fair_sched_group(struct task_group *tg, int cpu) | ||
8439 | { | ||
8440 | } | ||
8441 | #endif /* CONFIG_FAIR_GROUP_SCHED */ | ||
8442 | |||
8443 | #ifdef CONFIG_RT_GROUP_SCHED | ||
8444 | static void free_rt_sched_group(struct task_group *tg) | ||
8445 | { | ||
8446 | int i; | ||
8447 | |||
8448 | if (tg->rt_se) | ||
8449 | destroy_rt_bandwidth(&tg->rt_bandwidth); | ||
8450 | |||
8451 | for_each_possible_cpu(i) { | ||
8452 | if (tg->rt_rq) | ||
8453 | kfree(tg->rt_rq[i]); | ||
8454 | if (tg->rt_se) | ||
8455 | kfree(tg->rt_se[i]); | ||
8456 | } | ||
8457 | |||
8458 | kfree(tg->rt_rq); | ||
8459 | kfree(tg->rt_se); | ||
8460 | } | ||
8461 | |||
8462 | static | ||
8463 | int alloc_rt_sched_group(struct task_group *tg, struct task_group *parent) | ||
8464 | { | ||
8465 | struct rt_rq *rt_rq; | ||
8466 | struct sched_rt_entity *rt_se; | ||
8467 | int i; | ||
8468 | |||
8469 | tg->rt_rq = kzalloc(sizeof(rt_rq) * nr_cpu_ids, GFP_KERNEL); | ||
8470 | if (!tg->rt_rq) | ||
8471 | goto err; | ||
8472 | tg->rt_se = kzalloc(sizeof(rt_se) * nr_cpu_ids, GFP_KERNEL); | ||
8473 | if (!tg->rt_se) | ||
8474 | goto err; | ||
8475 | |||
8476 | init_rt_bandwidth(&tg->rt_bandwidth, | ||
8477 | ktime_to_ns(def_rt_bandwidth.rt_period), 0); | ||
8478 | |||
8479 | for_each_possible_cpu(i) { | ||
8480 | rt_rq = kzalloc_node(sizeof(struct rt_rq), | ||
8481 | GFP_KERNEL, cpu_to_node(i)); | ||
8482 | if (!rt_rq) | ||
8483 | goto err; | ||
8484 | |||
8485 | rt_se = kzalloc_node(sizeof(struct sched_rt_entity), | ||
8486 | GFP_KERNEL, cpu_to_node(i)); | ||
8487 | if (!rt_se) | ||
8488 | goto err_free_rq; | ||
8489 | |||
8490 | init_rt_rq(rt_rq, cpu_rq(i)); | ||
8491 | rt_rq->rt_runtime = tg->rt_bandwidth.rt_runtime; | ||
8492 | init_tg_rt_entry(tg, rt_rq, rt_se, i, parent->rt_se[i]); | ||
8493 | } | ||
8494 | |||
8495 | return 1; | ||
8496 | |||
8497 | err_free_rq: | ||
8498 | kfree(rt_rq); | ||
8499 | err: | ||
8500 | return 0; | ||
8501 | } | ||
8502 | #else /* !CONFIG_RT_GROUP_SCHED */ | ||
8503 | static inline void free_rt_sched_group(struct task_group *tg) | ||
8504 | { | ||
8505 | } | ||
8506 | |||
8507 | static inline | ||
8508 | int alloc_rt_sched_group(struct task_group *tg, struct task_group *parent) | ||
8509 | { | ||
8510 | return 1; | ||
8511 | } | ||
8512 | #endif /* CONFIG_RT_GROUP_SCHED */ | ||
8513 | |||
8514 | #ifdef CONFIG_CGROUP_SCHED | ||
8515 | static void free_sched_group(struct task_group *tg) | ||
8516 | { | ||
8517 | free_fair_sched_group(tg); | ||
8518 | free_rt_sched_group(tg); | ||
8519 | autogroup_free(tg); | ||
8520 | kfree(tg); | ||
8521 | } | ||
8522 | |||
8523 | /* allocate runqueue etc for a new task group */ | ||
8524 | struct task_group *sched_create_group(struct task_group *parent) | ||
8525 | { | ||
8526 | struct task_group *tg; | ||
8527 | unsigned long flags; | ||
8528 | |||
8529 | tg = kzalloc(sizeof(*tg), GFP_KERNEL); | ||
8530 | if (!tg) | ||
8531 | return ERR_PTR(-ENOMEM); | ||
8532 | |||
8533 | if (!alloc_fair_sched_group(tg, parent)) | ||
8534 | goto err; | ||
8535 | |||
8536 | if (!alloc_rt_sched_group(tg, parent)) | ||
8537 | goto err; | ||
8538 | |||
8539 | spin_lock_irqsave(&task_group_lock, flags); | ||
8540 | list_add_rcu(&tg->list, &task_groups); | ||
8541 | |||
8542 | WARN_ON(!parent); /* root should already exist */ | ||
8543 | |||
8544 | tg->parent = parent; | ||
8545 | INIT_LIST_HEAD(&tg->children); | ||
8546 | list_add_rcu(&tg->siblings, &parent->children); | ||
8547 | spin_unlock_irqrestore(&task_group_lock, flags); | ||
8548 | |||
8549 | return tg; | ||
8550 | |||
8551 | err: | ||
8552 | free_sched_group(tg); | ||
8553 | return ERR_PTR(-ENOMEM); | ||
8554 | } | ||
8555 | |||
8556 | /* rcu callback to free various structures associated with a task group */ | ||
8557 | static void free_sched_group_rcu(struct rcu_head *rhp) | ||
8558 | { | ||
8559 | /* now it should be safe to free those cfs_rqs */ | ||
8560 | free_sched_group(container_of(rhp, struct task_group, rcu)); | ||
8561 | } | ||
8562 | |||
8563 | /* Destroy runqueue etc associated with a task group */ | ||
8564 | void sched_destroy_group(struct task_group *tg) | ||
8565 | { | ||
8566 | unsigned long flags; | ||
8567 | int i; | ||
8568 | |||
8569 | /* end participation in shares distribution */ | ||
8570 | for_each_possible_cpu(i) | ||
8571 | unregister_fair_sched_group(tg, i); | ||
8572 | |||
8573 | spin_lock_irqsave(&task_group_lock, flags); | ||
8574 | list_del_rcu(&tg->list); | ||
8575 | list_del_rcu(&tg->siblings); | ||
8576 | spin_unlock_irqrestore(&task_group_lock, flags); | ||
8577 | |||
8578 | /* wait for possible concurrent references to cfs_rqs complete */ | ||
8579 | call_rcu(&tg->rcu, free_sched_group_rcu); | ||
8580 | } | ||
8581 | |||
8582 | /* change task's runqueue when it moves between groups. | ||
8583 | * The caller of this function should have put the task in its new group | ||
8584 | * by now. This function just updates tsk->se.cfs_rq and tsk->se.parent to | ||
8585 | * reflect its new group. | ||
8586 | */ | ||
8587 | void sched_move_task(struct task_struct *tsk) | ||
8588 | { | ||
8589 | int on_rq, running; | ||
8590 | unsigned long flags; | ||
8591 | struct rq *rq; | ||
8592 | |||
8593 | rq = task_rq_lock(tsk, &flags); | ||
8594 | |||
8595 | running = task_current(rq, tsk); | ||
8596 | on_rq = tsk->on_rq; | ||
8597 | |||
8598 | if (on_rq) | ||
8599 | dequeue_task(rq, tsk, 0); | ||
8600 | if (unlikely(running)) | ||
8601 | tsk->sched_class->put_prev_task(rq, tsk); | ||
8602 | |||
8603 | #ifdef CONFIG_FAIR_GROUP_SCHED | ||
8604 | if (tsk->sched_class->task_move_group) | ||
8605 | tsk->sched_class->task_move_group(tsk, on_rq); | ||
8606 | else | ||
8607 | #endif | ||
8608 | set_task_rq(tsk, task_cpu(tsk)); | ||
8609 | |||
8610 | if (unlikely(running)) | ||
8611 | tsk->sched_class->set_curr_task(rq); | ||
8612 | if (on_rq) | ||
8613 | enqueue_task(rq, tsk, 0); | ||
8614 | |||
8615 | task_rq_unlock(rq, tsk, &flags); | ||
8616 | } | ||
8617 | #endif /* CONFIG_CGROUP_SCHED */ | ||
8618 | |||
8619 | #ifdef CONFIG_FAIR_GROUP_SCHED | ||
8620 | static DEFINE_MUTEX(shares_mutex); | ||
8621 | |||
8622 | int sched_group_set_shares(struct task_group *tg, unsigned long shares) | ||
8623 | { | ||
8624 | int i; | ||
8625 | unsigned long flags; | ||
8626 | |||
8627 | /* | ||
8628 | * We can't change the weight of the root cgroup. | ||
8629 | */ | ||
8630 | if (!tg->se[0]) | ||
8631 | return -EINVAL; | ||
8632 | |||
8633 | shares = clamp(shares, scale_load(MIN_SHARES), scale_load(MAX_SHARES)); | ||
8634 | |||
8635 | mutex_lock(&shares_mutex); | ||
8636 | if (tg->shares == shares) | ||
8637 | goto done; | ||
8638 | |||
8639 | tg->shares = shares; | ||
8640 | for_each_possible_cpu(i) { | ||
8641 | struct rq *rq = cpu_rq(i); | ||
8642 | struct sched_entity *se; | ||
8643 | |||
8644 | se = tg->se[i]; | ||
8645 | /* Propagate contribution to hierarchy */ | ||
8646 | raw_spin_lock_irqsave(&rq->lock, flags); | ||
8647 | for_each_sched_entity(se) | ||
8648 | update_cfs_shares(group_cfs_rq(se)); | ||
8649 | raw_spin_unlock_irqrestore(&rq->lock, flags); | ||
8650 | } | ||
8651 | |||
8652 | done: | ||
8653 | mutex_unlock(&shares_mutex); | ||
8654 | return 0; | ||
8655 | } | ||
8656 | |||
8657 | unsigned long sched_group_shares(struct task_group *tg) | ||
8658 | { | ||
8659 | return tg->shares; | ||
8660 | } | ||
8661 | #endif | ||
8662 | |||
8663 | #ifdef CONFIG_RT_GROUP_SCHED | ||
8664 | /* | ||
8665 | * Ensure that the real time constraints are schedulable. | ||
8666 | */ | ||
8667 | static DEFINE_MUTEX(rt_constraints_mutex); | ||
8668 | |||
8669 | static unsigned long to_ratio(u64 period, u64 runtime) | ||
8670 | { | ||
8671 | if (runtime == RUNTIME_INF) | ||
8672 | return 1ULL << 20; | ||
8673 | |||
8674 | return div64_u64(runtime << 20, period); | ||
8675 | } | ||
8676 | |||
8677 | /* Must be called with tasklist_lock held */ | ||
8678 | static inline int tg_has_rt_tasks(struct task_group *tg) | ||
8679 | { | ||
8680 | struct task_struct *g, *p; | ||
8681 | |||
8682 | do_each_thread(g, p) { | ||
8683 | if (rt_task(p) && rt_rq_of_se(&p->rt)->tg == tg) | ||
8684 | return 1; | ||
8685 | } while_each_thread(g, p); | ||
8686 | |||
8687 | return 0; | ||
8688 | } | ||
8689 | |||
8690 | struct rt_schedulable_data { | ||
8691 | struct task_group *tg; | ||
8692 | u64 rt_period; | ||
8693 | u64 rt_runtime; | ||
8694 | }; | ||
8695 | |||
8696 | static int tg_schedulable(struct task_group *tg, void *data) | ||
8697 | { | ||
8698 | struct rt_schedulable_data *d = data; | ||
8699 | struct task_group *child; | ||
8700 | unsigned long total, sum = 0; | ||
8701 | u64 period, runtime; | ||
8702 | |||
8703 | period = ktime_to_ns(tg->rt_bandwidth.rt_period); | ||
8704 | runtime = tg->rt_bandwidth.rt_runtime; | ||
8705 | |||
8706 | if (tg == d->tg) { | ||
8707 | period = d->rt_period; | ||
8708 | runtime = d->rt_runtime; | ||
8709 | } | ||
8710 | |||
8711 | /* | ||
8712 | * Cannot have more runtime than the period. | ||
8713 | */ | ||
8714 | if (runtime > period && runtime != RUNTIME_INF) | ||
8715 | return -EINVAL; | ||
8716 | |||
8717 | /* | ||
8718 | * Ensure we don't starve existing RT tasks. | ||
8719 | */ | ||
8720 | if (rt_bandwidth_enabled() && !runtime && tg_has_rt_tasks(tg)) | ||
8721 | return -EBUSY; | ||
8722 | |||
8723 | total = to_ratio(period, runtime); | ||
8724 | |||
8725 | /* | ||
8726 | * Nobody can have more than the global setting allows. | ||
8727 | */ | ||
8728 | if (total > to_ratio(global_rt_period(), global_rt_runtime())) | ||
8729 | return -EINVAL; | ||
8730 | |||
8731 | /* | ||
8732 | * The sum of our children's runtime should not exceed our own. | ||
8733 | */ | ||
8734 | list_for_each_entry_rcu(child, &tg->children, siblings) { | ||
8735 | period = ktime_to_ns(child->rt_bandwidth.rt_period); | ||
8736 | runtime = child->rt_bandwidth.rt_runtime; | ||
8737 | |||
8738 | if (child == d->tg) { | ||
8739 | period = d->rt_period; | ||
8740 | runtime = d->rt_runtime; | ||
8741 | } | ||
8742 | |||
8743 | sum += to_ratio(period, runtime); | ||
8744 | } | ||
8745 | |||
8746 | if (sum > total) | ||
8747 | return -EINVAL; | ||
8748 | |||
8749 | return 0; | ||
8750 | } | ||
8751 | |||
8752 | static int __rt_schedulable(struct task_group *tg, u64 period, u64 runtime) | ||
8753 | { | ||
8754 | struct rt_schedulable_data data = { | ||
8755 | .tg = tg, | ||
8756 | .rt_period = period, | ||
8757 | .rt_runtime = runtime, | ||
8758 | }; | ||
8759 | |||
8760 | return walk_tg_tree(tg_schedulable, tg_nop, &data); | ||
8761 | } | ||
8762 | |||
8763 | static int tg_set_bandwidth(struct task_group *tg, | ||
8764 | u64 rt_period, u64 rt_runtime) | ||
8765 | { | ||
8766 | int i, err = 0; | ||
8767 | |||
8768 | mutex_lock(&rt_constraints_mutex); | ||
8769 | read_lock(&tasklist_lock); | ||
8770 | err = __rt_schedulable(tg, rt_period, rt_runtime); | ||
8771 | if (err) | ||
8772 | goto unlock; | ||
8773 | |||
8774 | raw_spin_lock_irq(&tg->rt_bandwidth.rt_runtime_lock); | ||
8775 | tg->rt_bandwidth.rt_period = ns_to_ktime(rt_period); | ||
8776 | tg->rt_bandwidth.rt_runtime = rt_runtime; | ||
8777 | |||
8778 | for_each_possible_cpu(i) { | ||
8779 | struct rt_rq *rt_rq = tg->rt_rq[i]; | ||
8780 | |||
8781 | raw_spin_lock(&rt_rq->rt_runtime_lock); | ||
8782 | rt_rq->rt_runtime = rt_runtime; | ||
8783 | raw_spin_unlock(&rt_rq->rt_runtime_lock); | ||
8784 | } | ||
8785 | raw_spin_unlock_irq(&tg->rt_bandwidth.rt_runtime_lock); | ||
8786 | unlock: | ||
8787 | read_unlock(&tasklist_lock); | ||
8788 | mutex_unlock(&rt_constraints_mutex); | ||
8789 | |||
8790 | return err; | ||
8791 | } | ||
8792 | |||
8793 | int sched_group_set_rt_runtime(struct task_group *tg, long rt_runtime_us) | ||
8794 | { | ||
8795 | u64 rt_runtime, rt_period; | ||
8796 | |||
8797 | rt_period = ktime_to_ns(tg->rt_bandwidth.rt_period); | ||
8798 | rt_runtime = (u64)rt_runtime_us * NSEC_PER_USEC; | ||
8799 | if (rt_runtime_us < 0) | ||
8800 | rt_runtime = RUNTIME_INF; | ||
8801 | |||
8802 | return tg_set_bandwidth(tg, rt_period, rt_runtime); | ||
8803 | } | ||
8804 | |||
8805 | long sched_group_rt_runtime(struct task_group *tg) | ||
8806 | { | ||
8807 | u64 rt_runtime_us; | ||
8808 | |||
8809 | if (tg->rt_bandwidth.rt_runtime == RUNTIME_INF) | ||
8810 | return -1; | ||
8811 | |||
8812 | rt_runtime_us = tg->rt_bandwidth.rt_runtime; | ||
8813 | do_div(rt_runtime_us, NSEC_PER_USEC); | ||
8814 | return rt_runtime_us; | ||
8815 | } | ||
8816 | |||
8817 | int sched_group_set_rt_period(struct task_group *tg, long rt_period_us) | ||
8818 | { | ||
8819 | u64 rt_runtime, rt_period; | ||
8820 | |||
8821 | rt_period = (u64)rt_period_us * NSEC_PER_USEC; | ||
8822 | rt_runtime = tg->rt_bandwidth.rt_runtime; | ||
8823 | |||
8824 | if (rt_period == 0) | ||
8825 | return -EINVAL; | ||
8826 | |||
8827 | return tg_set_bandwidth(tg, rt_period, rt_runtime); | ||
8828 | } | ||
8829 | |||
8830 | long sched_group_rt_period(struct task_group *tg) | ||
8831 | { | ||
8832 | u64 rt_period_us; | ||
8833 | |||
8834 | rt_period_us = ktime_to_ns(tg->rt_bandwidth.rt_period); | ||
8835 | do_div(rt_period_us, NSEC_PER_USEC); | ||
8836 | return rt_period_us; | ||
8837 | } | ||
8838 | |||
8839 | static int sched_rt_global_constraints(void) | ||
8840 | { | ||
8841 | u64 runtime, period; | ||
8842 | int ret = 0; | ||
8843 | |||
8844 | if (sysctl_sched_rt_period <= 0) | ||
8845 | return -EINVAL; | ||
8846 | |||
8847 | runtime = global_rt_runtime(); | ||
8848 | period = global_rt_period(); | ||
8849 | |||
8850 | /* | ||
8851 | * Sanity check on the sysctl variables. | ||
8852 | */ | ||
8853 | if (runtime > period && runtime != RUNTIME_INF) | ||
8854 | return -EINVAL; | ||
8855 | |||
8856 | mutex_lock(&rt_constraints_mutex); | ||
8857 | read_lock(&tasklist_lock); | ||
8858 | ret = __rt_schedulable(NULL, 0, 0); | ||
8859 | read_unlock(&tasklist_lock); | ||
8860 | mutex_unlock(&rt_constraints_mutex); | ||
8861 | |||
8862 | return ret; | ||
8863 | } | ||
8864 | |||
8865 | int sched_rt_can_attach(struct task_group *tg, struct task_struct *tsk) | ||
8866 | { | ||
8867 | /* Don't accept realtime tasks when there is no way for them to run */ | ||
8868 | if (rt_task(tsk) && tg->rt_bandwidth.rt_runtime == 0) | ||
8869 | return 0; | ||
8870 | |||
8871 | return 1; | ||
8872 | } | ||
8873 | |||
8874 | #else /* !CONFIG_RT_GROUP_SCHED */ | ||
8875 | static int sched_rt_global_constraints(void) | ||
8876 | { | ||
8877 | unsigned long flags; | ||
8878 | int i; | ||
8879 | |||
8880 | if (sysctl_sched_rt_period <= 0) | ||
8881 | return -EINVAL; | ||
8882 | |||
8883 | /* | ||
8884 | * There's always some RT tasks in the root group | ||
8885 | * -- migration, kstopmachine etc.. | ||
8886 | */ | ||
8887 | if (sysctl_sched_rt_runtime == 0) | ||
8888 | return -EBUSY; | ||
8889 | |||
8890 | raw_spin_lock_irqsave(&def_rt_bandwidth.rt_runtime_lock, flags); | ||
8891 | for_each_possible_cpu(i) { | ||
8892 | struct rt_rq *rt_rq = &cpu_rq(i)->rt; | ||
8893 | |||
8894 | raw_spin_lock(&rt_rq->rt_runtime_lock); | ||
8895 | rt_rq->rt_runtime = global_rt_runtime(); | ||
8896 | raw_spin_unlock(&rt_rq->rt_runtime_lock); | ||
8897 | } | ||
8898 | raw_spin_unlock_irqrestore(&def_rt_bandwidth.rt_runtime_lock, flags); | ||
8899 | |||
8900 | return 0; | ||
8901 | } | ||
8902 | #endif /* CONFIG_RT_GROUP_SCHED */ | ||
8903 | |||
8904 | int sched_rt_handler(struct ctl_table *table, int write, | ||
8905 | void __user *buffer, size_t *lenp, | ||
8906 | loff_t *ppos) | ||
8907 | { | ||
8908 | int ret; | ||
8909 | int old_period, old_runtime; | ||
8910 | static DEFINE_MUTEX(mutex); | ||
8911 | |||
8912 | mutex_lock(&mutex); | ||
8913 | old_period = sysctl_sched_rt_period; | ||
8914 | old_runtime = sysctl_sched_rt_runtime; | ||
8915 | |||
8916 | ret = proc_dointvec(table, write, buffer, lenp, ppos); | ||
8917 | |||
8918 | if (!ret && write) { | ||
8919 | ret = sched_rt_global_constraints(); | ||
8920 | if (ret) { | ||
8921 | sysctl_sched_rt_period = old_period; | ||
8922 | sysctl_sched_rt_runtime = old_runtime; | ||
8923 | } else { | ||
8924 | def_rt_bandwidth.rt_runtime = global_rt_runtime(); | ||
8925 | def_rt_bandwidth.rt_period = | ||
8926 | ns_to_ktime(global_rt_period()); | ||
8927 | } | ||
8928 | } | ||
8929 | mutex_unlock(&mutex); | ||
8930 | |||
8931 | return ret; | ||
8932 | } | ||
8933 | |||
8934 | #ifdef CONFIG_CGROUP_SCHED | ||
8935 | |||
8936 | /* return corresponding task_group object of a cgroup */ | ||
8937 | static inline struct task_group *cgroup_tg(struct cgroup *cgrp) | ||
8938 | { | ||
8939 | return container_of(cgroup_subsys_state(cgrp, cpu_cgroup_subsys_id), | ||
8940 | struct task_group, css); | ||
8941 | } | ||
8942 | |||
8943 | static struct cgroup_subsys_state * | ||
8944 | cpu_cgroup_create(struct cgroup_subsys *ss, struct cgroup *cgrp) | ||
8945 | { | ||
8946 | struct task_group *tg, *parent; | ||
8947 | |||
8948 | if (!cgrp->parent) { | ||
8949 | /* This is early initialization for the top cgroup */ | ||
8950 | return &root_task_group.css; | ||
8951 | } | ||
8952 | |||
8953 | parent = cgroup_tg(cgrp->parent); | ||
8954 | tg = sched_create_group(parent); | ||
8955 | if (IS_ERR(tg)) | ||
8956 | return ERR_PTR(-ENOMEM); | ||
8957 | |||
8958 | return &tg->css; | ||
8959 | } | ||
8960 | |||
8961 | static void | ||
8962 | cpu_cgroup_destroy(struct cgroup_subsys *ss, struct cgroup *cgrp) | ||
8963 | { | ||
8964 | struct task_group *tg = cgroup_tg(cgrp); | ||
8965 | |||
8966 | sched_destroy_group(tg); | ||
8967 | } | ||
8968 | |||
8969 | static int | ||
8970 | cpu_cgroup_allow_attach(struct cgroup *cgrp, struct task_struct *tsk) | ||
8971 | { | ||
8972 | const struct cred *cred = current_cred(), *tcred; | ||
8973 | |||
8974 | tcred = __task_cred(tsk); | ||
8975 | |||
8976 | if ((current != tsk) && !capable(CAP_SYS_NICE) && | ||
8977 | cred->euid != tcred->uid && cred->euid != tcred->suid) | ||
8978 | return -EACCES; | ||
8979 | |||
8980 | return 0; | ||
8981 | } | ||
8982 | |||
8983 | static int | ||
8984 | cpu_cgroup_can_attach_task(struct cgroup *cgrp, struct task_struct *tsk) | ||
8985 | { | ||
8986 | #ifdef CONFIG_RT_GROUP_SCHED | ||
8987 | if (!sched_rt_can_attach(cgroup_tg(cgrp), tsk)) | ||
8988 | return -EINVAL; | ||
8989 | #else | ||
8990 | /* We don't support RT-tasks being in separate groups */ | ||
8991 | if (tsk->sched_class != &fair_sched_class) | ||
8992 | return -EINVAL; | ||
8993 | #endif | ||
8994 | return 0; | ||
8995 | } | ||
8996 | |||
8997 | static void | ||
8998 | cpu_cgroup_attach_task(struct cgroup *cgrp, struct task_struct *tsk) | ||
8999 | { | ||
9000 | sched_move_task(tsk); | ||
9001 | } | ||
9002 | |||
9003 | static void | ||
9004 | cpu_cgroup_exit(struct cgroup_subsys *ss, struct cgroup *cgrp, | ||
9005 | struct cgroup *old_cgrp, struct task_struct *task) | ||
9006 | { | ||
9007 | /* | ||
9008 | * cgroup_exit() is called in the copy_process() failure path. | ||
9009 | * Ignore this case since the task hasn't ran yet, this avoids | ||
9010 | * trying to poke a half freed task state from generic code. | ||
9011 | */ | ||
9012 | if (!(task->flags & PF_EXITING)) | ||
9013 | return; | ||
9014 | |||
9015 | sched_move_task(task); | ||
9016 | } | ||
9017 | |||
9018 | #ifdef CONFIG_FAIR_GROUP_SCHED | ||
9019 | static int cpu_shares_write_u64(struct cgroup *cgrp, struct cftype *cftype, | ||
9020 | u64 shareval) | ||
9021 | { | ||
9022 | return sched_group_set_shares(cgroup_tg(cgrp), scale_load(shareval)); | ||
9023 | } | ||
9024 | |||
9025 | static u64 cpu_shares_read_u64(struct cgroup *cgrp, struct cftype *cft) | ||
9026 | { | ||
9027 | struct task_group *tg = cgroup_tg(cgrp); | ||
9028 | |||
9029 | return (u64) scale_load_down(tg->shares); | ||
9030 | } | ||
9031 | #endif /* CONFIG_FAIR_GROUP_SCHED */ | ||
9032 | |||
9033 | #ifdef CONFIG_RT_GROUP_SCHED | ||
9034 | static int cpu_rt_runtime_write(struct cgroup *cgrp, struct cftype *cft, | ||
9035 | s64 val) | ||
9036 | { | ||
9037 | return sched_group_set_rt_runtime(cgroup_tg(cgrp), val); | ||
9038 | } | ||
9039 | |||
9040 | static s64 cpu_rt_runtime_read(struct cgroup *cgrp, struct cftype *cft) | ||
9041 | { | ||
9042 | return sched_group_rt_runtime(cgroup_tg(cgrp)); | ||
9043 | } | ||
9044 | |||
9045 | static int cpu_rt_period_write_uint(struct cgroup *cgrp, struct cftype *cftype, | ||
9046 | u64 rt_period_us) | ||
9047 | { | ||
9048 | return sched_group_set_rt_period(cgroup_tg(cgrp), rt_period_us); | ||
9049 | } | ||
9050 | |||
9051 | static u64 cpu_rt_period_read_uint(struct cgroup *cgrp, struct cftype *cft) | ||
9052 | { | ||
9053 | return sched_group_rt_period(cgroup_tg(cgrp)); | ||
9054 | } | ||
9055 | #endif /* CONFIG_RT_GROUP_SCHED */ | ||
9056 | |||
9057 | static struct cftype cpu_files[] = { | ||
9058 | #ifdef CONFIG_FAIR_GROUP_SCHED | ||
9059 | { | ||
9060 | .name = "shares", | ||
9061 | .read_u64 = cpu_shares_read_u64, | ||
9062 | .write_u64 = cpu_shares_write_u64, | ||
9063 | }, | ||
9064 | #endif | ||
9065 | #ifdef CONFIG_RT_GROUP_SCHED | ||
9066 | { | ||
9067 | .name = "rt_runtime_us", | ||
9068 | .read_s64 = cpu_rt_runtime_read, | ||
9069 | .write_s64 = cpu_rt_runtime_write, | ||
9070 | }, | ||
9071 | { | ||
9072 | .name = "rt_period_us", | ||
9073 | .read_u64 = cpu_rt_period_read_uint, | ||
9074 | .write_u64 = cpu_rt_period_write_uint, | ||
9075 | }, | ||
9076 | #endif | ||
9077 | }; | ||
9078 | |||
9079 | static int cpu_cgroup_populate(struct cgroup_subsys *ss, struct cgroup *cont) | ||
9080 | { | ||
9081 | return cgroup_add_files(cont, ss, cpu_files, ARRAY_SIZE(cpu_files)); | ||
9082 | } | ||
9083 | |||
9084 | struct cgroup_subsys cpu_cgroup_subsys = { | ||
9085 | .name = "cpu", | ||
9086 | .create = cpu_cgroup_create, | ||
9087 | .destroy = cpu_cgroup_destroy, | ||
9088 | .allow_attach = cpu_cgroup_allow_attach, | ||
9089 | .can_attach_task = cpu_cgroup_can_attach_task, | ||
9090 | .attach_task = cpu_cgroup_attach_task, | ||
9091 | .exit = cpu_cgroup_exit, | ||
9092 | .populate = cpu_cgroup_populate, | ||
9093 | .subsys_id = cpu_cgroup_subsys_id, | ||
9094 | .early_init = 1, | ||
9095 | }; | ||
9096 | |||
9097 | #endif /* CONFIG_CGROUP_SCHED */ | ||
9098 | |||
9099 | #ifdef CONFIG_CGROUP_CPUACCT | ||
9100 | |||
9101 | /* | ||
9102 | * CPU accounting code for task groups. | ||
9103 | * | ||
9104 | * Based on the work by Paul Menage (menage@google.com) and Balbir Singh | ||
9105 | * (balbir@in.ibm.com). | ||
9106 | */ | ||
9107 | |||
9108 | /* track cpu usage of a group of tasks and its child groups */ | ||
9109 | struct cpuacct { | ||
9110 | struct cgroup_subsys_state css; | ||
9111 | /* cpuusage holds pointer to a u64-type object on every cpu */ | ||
9112 | u64 __percpu *cpuusage; | ||
9113 | struct percpu_counter cpustat[CPUACCT_STAT_NSTATS]; | ||
9114 | struct cpuacct *parent; | ||
9115 | struct cpuacct_charge_calls *cpufreq_fn; | ||
9116 | void *cpuacct_data; | ||
9117 | }; | ||
9118 | |||
9119 | static struct cpuacct *cpuacct_root; | ||
9120 | |||
9121 | /* Default calls for cpufreq accounting */ | ||
9122 | static struct cpuacct_charge_calls *cpuacct_cpufreq; | ||
9123 | int cpuacct_register_cpufreq(struct cpuacct_charge_calls *fn) | ||
9124 | { | ||
9125 | cpuacct_cpufreq = fn; | ||
9126 | |||
9127 | /* | ||
9128 | * Root node is created before platform can register callbacks, | ||
9129 | * initalize here. | ||
9130 | */ | ||
9131 | if (cpuacct_root && fn) { | ||
9132 | cpuacct_root->cpufreq_fn = fn; | ||
9133 | if (fn->init) | ||
9134 | fn->init(&cpuacct_root->cpuacct_data); | ||
9135 | } | ||
9136 | return 0; | ||
9137 | } | ||
9138 | |||
9139 | struct cgroup_subsys cpuacct_subsys; | ||
9140 | |||
9141 | /* return cpu accounting group corresponding to this container */ | ||
9142 | static inline struct cpuacct *cgroup_ca(struct cgroup *cgrp) | ||
9143 | { | ||
9144 | return container_of(cgroup_subsys_state(cgrp, cpuacct_subsys_id), | ||
9145 | struct cpuacct, css); | ||
9146 | } | ||
9147 | |||
9148 | /* return cpu accounting group to which this task belongs */ | ||
9149 | static inline struct cpuacct *task_ca(struct task_struct *tsk) | ||
9150 | { | ||
9151 | return container_of(task_subsys_state(tsk, cpuacct_subsys_id), | ||
9152 | struct cpuacct, css); | ||
9153 | } | ||
9154 | |||
9155 | /* create a new cpu accounting group */ | ||
9156 | static struct cgroup_subsys_state *cpuacct_create( | ||
9157 | struct cgroup_subsys *ss, struct cgroup *cgrp) | ||
9158 | { | ||
9159 | struct cpuacct *ca = kzalloc(sizeof(*ca), GFP_KERNEL); | ||
9160 | int i; | ||
9161 | |||
9162 | if (!ca) | ||
9163 | goto out; | ||
9164 | |||
9165 | ca->cpuusage = alloc_percpu(u64); | ||
9166 | if (!ca->cpuusage) | ||
9167 | goto out_free_ca; | ||
9168 | |||
9169 | for (i = 0; i < CPUACCT_STAT_NSTATS; i++) | ||
9170 | if (percpu_counter_init(&ca->cpustat[i], 0)) | ||
9171 | goto out_free_counters; | ||
9172 | |||
9173 | ca->cpufreq_fn = cpuacct_cpufreq; | ||
9174 | |||
9175 | /* If available, have platform code initalize cpu frequency table */ | ||
9176 | if (ca->cpufreq_fn && ca->cpufreq_fn->init) | ||
9177 | ca->cpufreq_fn->init(&ca->cpuacct_data); | ||
9178 | |||
9179 | if (cgrp->parent) | ||
9180 | ca->parent = cgroup_ca(cgrp->parent); | ||
9181 | else | ||
9182 | cpuacct_root = ca; | ||
9183 | |||
9184 | return &ca->css; | ||
9185 | |||
9186 | out_free_counters: | ||
9187 | while (--i >= 0) | ||
9188 | percpu_counter_destroy(&ca->cpustat[i]); | ||
9189 | free_percpu(ca->cpuusage); | ||
9190 | out_free_ca: | ||
9191 | kfree(ca); | ||
9192 | out: | ||
9193 | return ERR_PTR(-ENOMEM); | ||
9194 | } | ||
9195 | |||
9196 | /* destroy an existing cpu accounting group */ | ||
9197 | static void | ||
9198 | cpuacct_destroy(struct cgroup_subsys *ss, struct cgroup *cgrp) | ||
9199 | { | ||
9200 | struct cpuacct *ca = cgroup_ca(cgrp); | ||
9201 | int i; | ||
9202 | |||
9203 | for (i = 0; i < CPUACCT_STAT_NSTATS; i++) | ||
9204 | percpu_counter_destroy(&ca->cpustat[i]); | ||
9205 | free_percpu(ca->cpuusage); | ||
9206 | kfree(ca); | ||
9207 | } | ||
9208 | |||
9209 | static u64 cpuacct_cpuusage_read(struct cpuacct *ca, int cpu) | ||
9210 | { | ||
9211 | u64 *cpuusage = per_cpu_ptr(ca->cpuusage, cpu); | ||
9212 | u64 data; | ||
9213 | |||
9214 | #ifndef CONFIG_64BIT | ||
9215 | /* | ||
9216 | * Take rq->lock to make 64-bit read safe on 32-bit platforms. | ||
9217 | */ | ||
9218 | raw_spin_lock_irq(&cpu_rq(cpu)->lock); | ||
9219 | data = *cpuusage; | ||
9220 | raw_spin_unlock_irq(&cpu_rq(cpu)->lock); | ||
9221 | #else | ||
9222 | data = *cpuusage; | ||
9223 | #endif | ||
9224 | |||
9225 | return data; | ||
9226 | } | ||
9227 | |||
9228 | static void cpuacct_cpuusage_write(struct cpuacct *ca, int cpu, u64 val) | ||
9229 | { | ||
9230 | u64 *cpuusage = per_cpu_ptr(ca->cpuusage, cpu); | ||
9231 | |||
9232 | #ifndef CONFIG_64BIT | ||
9233 | /* | ||
9234 | * Take rq->lock to make 64-bit write safe on 32-bit platforms. | ||
9235 | */ | ||
9236 | raw_spin_lock_irq(&cpu_rq(cpu)->lock); | ||
9237 | *cpuusage = val; | ||
9238 | raw_spin_unlock_irq(&cpu_rq(cpu)->lock); | ||
9239 | #else | ||
9240 | *cpuusage = val; | ||
9241 | #endif | ||
9242 | } | ||
9243 | |||
9244 | /* return total cpu usage (in nanoseconds) of a group */ | ||
9245 | static u64 cpuusage_read(struct cgroup *cgrp, struct cftype *cft) | ||
9246 | { | ||
9247 | struct cpuacct *ca = cgroup_ca(cgrp); | ||
9248 | u64 totalcpuusage = 0; | ||
9249 | int i; | ||
9250 | |||
9251 | for_each_present_cpu(i) | ||
9252 | totalcpuusage += cpuacct_cpuusage_read(ca, i); | ||
9253 | |||
9254 | return totalcpuusage; | ||
9255 | } | ||
9256 | |||
9257 | static int cpuusage_write(struct cgroup *cgrp, struct cftype *cftype, | ||
9258 | u64 reset) | ||
9259 | { | ||
9260 | struct cpuacct *ca = cgroup_ca(cgrp); | ||
9261 | int err = 0; | ||
9262 | int i; | ||
9263 | |||
9264 | if (reset) { | ||
9265 | err = -EINVAL; | ||
9266 | goto out; | ||
9267 | } | ||
9268 | |||
9269 | for_each_present_cpu(i) | ||
9270 | cpuacct_cpuusage_write(ca, i, 0); | ||
9271 | |||
9272 | out: | ||
9273 | return err; | ||
9274 | } | ||
9275 | |||
9276 | static int cpuacct_percpu_seq_read(struct cgroup *cgroup, struct cftype *cft, | ||
9277 | struct seq_file *m) | ||
9278 | { | ||
9279 | struct cpuacct *ca = cgroup_ca(cgroup); | ||
9280 | u64 percpu; | ||
9281 | int i; | ||
9282 | |||
9283 | for_each_present_cpu(i) { | ||
9284 | percpu = cpuacct_cpuusage_read(ca, i); | ||
9285 | seq_printf(m, "%llu ", (unsigned long long) percpu); | ||
9286 | } | ||
9287 | seq_printf(m, "\n"); | ||
9288 | return 0; | ||
9289 | } | ||
9290 | |||
9291 | static const char *cpuacct_stat_desc[] = { | ||
9292 | [CPUACCT_STAT_USER] = "user", | ||
9293 | [CPUACCT_STAT_SYSTEM] = "system", | ||
9294 | }; | ||
9295 | |||
9296 | static int cpuacct_stats_show(struct cgroup *cgrp, struct cftype *cft, | ||
9297 | struct cgroup_map_cb *cb) | ||
9298 | { | ||
9299 | struct cpuacct *ca = cgroup_ca(cgrp); | ||
9300 | int i; | ||
9301 | |||
9302 | for (i = 0; i < CPUACCT_STAT_NSTATS; i++) { | ||
9303 | s64 val = percpu_counter_read(&ca->cpustat[i]); | ||
9304 | val = cputime64_to_clock_t(val); | ||
9305 | cb->fill(cb, cpuacct_stat_desc[i], val); | ||
9306 | } | ||
9307 | return 0; | ||
9308 | } | ||
9309 | |||
9310 | static int cpuacct_cpufreq_show(struct cgroup *cgrp, struct cftype *cft, | ||
9311 | struct cgroup_map_cb *cb) | ||
9312 | { | ||
9313 | struct cpuacct *ca = cgroup_ca(cgrp); | ||
9314 | if (ca->cpufreq_fn && ca->cpufreq_fn->cpufreq_show) | ||
9315 | ca->cpufreq_fn->cpufreq_show(ca->cpuacct_data, cb); | ||
9316 | |||
9317 | return 0; | ||
9318 | } | ||
9319 | |||
9320 | /* return total cpu power usage (milliWatt second) of a group */ | ||
9321 | static u64 cpuacct_powerusage_read(struct cgroup *cgrp, struct cftype *cft) | ||
9322 | { | ||
9323 | int i; | ||
9324 | struct cpuacct *ca = cgroup_ca(cgrp); | ||
9325 | u64 totalpower = 0; | ||
9326 | |||
9327 | if (ca->cpufreq_fn && ca->cpufreq_fn->power_usage) | ||
9328 | for_each_present_cpu(i) { | ||
9329 | totalpower += ca->cpufreq_fn->power_usage( | ||
9330 | ca->cpuacct_data); | ||
9331 | } | ||
9332 | |||
9333 | return totalpower; | ||
9334 | } | ||
9335 | |||
9336 | static struct cftype files[] = { | ||
9337 | { | ||
9338 | .name = "usage", | ||
9339 | .read_u64 = cpuusage_read, | ||
9340 | .write_u64 = cpuusage_write, | ||
9341 | }, | ||
9342 | { | ||
9343 | .name = "usage_percpu", | ||
9344 | .read_seq_string = cpuacct_percpu_seq_read, | ||
9345 | }, | ||
9346 | { | ||
9347 | .name = "stat", | ||
9348 | .read_map = cpuacct_stats_show, | ||
9349 | }, | ||
9350 | { | ||
9351 | .name = "cpufreq", | ||
9352 | .read_map = cpuacct_cpufreq_show, | ||
9353 | }, | ||
9354 | { | ||
9355 | .name = "power", | ||
9356 | .read_u64 = cpuacct_powerusage_read | ||
9357 | }, | ||
9358 | }; | ||
9359 | |||
9360 | static int cpuacct_populate(struct cgroup_subsys *ss, struct cgroup *cgrp) | ||
9361 | { | ||
9362 | return cgroup_add_files(cgrp, ss, files, ARRAY_SIZE(files)); | ||
9363 | } | ||
9364 | |||
9365 | /* | ||
9366 | * charge this task's execution time to its accounting group. | ||
9367 | * | ||
9368 | * called with rq->lock held. | ||
9369 | */ | ||
9370 | static void cpuacct_charge(struct task_struct *tsk, u64 cputime) | ||
9371 | { | ||
9372 | struct cpuacct *ca; | ||
9373 | int cpu; | ||
9374 | |||
9375 | if (unlikely(!cpuacct_subsys.active)) | ||
9376 | return; | ||
9377 | |||
9378 | cpu = task_cpu(tsk); | ||
9379 | |||
9380 | rcu_read_lock(); | ||
9381 | |||
9382 | ca = task_ca(tsk); | ||
9383 | |||
9384 | for (; ca; ca = ca->parent) { | ||
9385 | u64 *cpuusage = per_cpu_ptr(ca->cpuusage, cpu); | ||
9386 | *cpuusage += cputime; | ||
9387 | |||
9388 | /* Call back into platform code to account for CPU speeds */ | ||
9389 | if (ca->cpufreq_fn && ca->cpufreq_fn->charge) | ||
9390 | ca->cpufreq_fn->charge(ca->cpuacct_data, cputime, cpu); | ||
9391 | } | ||
9392 | |||
9393 | rcu_read_unlock(); | ||
9394 | } | ||
9395 | |||
9396 | /* | ||
9397 | * When CONFIG_VIRT_CPU_ACCOUNTING is enabled one jiffy can be very large | ||
9398 | * in cputime_t units. As a result, cpuacct_update_stats calls | ||
9399 | * percpu_counter_add with values large enough to always overflow the | ||
9400 | * per cpu batch limit causing bad SMP scalability. | ||
9401 | * | ||
9402 | * To fix this we scale percpu_counter_batch by cputime_one_jiffy so we | ||
9403 | * batch the same amount of time with CONFIG_VIRT_CPU_ACCOUNTING disabled | ||
9404 | * and enabled. We cap it at INT_MAX which is the largest allowed batch value. | ||
9405 | */ | ||
9406 | #ifdef CONFIG_SMP | ||
9407 | #define CPUACCT_BATCH \ | ||
9408 | min_t(long, percpu_counter_batch * cputime_one_jiffy, INT_MAX) | ||
9409 | #else | ||
9410 | #define CPUACCT_BATCH 0 | ||
9411 | #endif | ||
9412 | |||
9413 | /* | ||
9414 | * Charge the system/user time to the task's accounting group. | ||
9415 | */ | ||
9416 | static void cpuacct_update_stats(struct task_struct *tsk, | ||
9417 | enum cpuacct_stat_index idx, cputime_t val) | ||
9418 | { | ||
9419 | struct cpuacct *ca; | ||
9420 | int batch = CPUACCT_BATCH; | ||
9421 | |||
9422 | if (unlikely(!cpuacct_subsys.active)) | ||
9423 | return; | ||
9424 | |||
9425 | rcu_read_lock(); | ||
9426 | ca = task_ca(tsk); | ||
9427 | |||
9428 | do { | ||
9429 | __percpu_counter_add(&ca->cpustat[idx], val, batch); | ||
9430 | ca = ca->parent; | ||
9431 | } while (ca); | ||
9432 | rcu_read_unlock(); | ||
9433 | } | ||
9434 | |||
9435 | struct cgroup_subsys cpuacct_subsys = { | ||
9436 | .name = "cpuacct", | ||
9437 | .create = cpuacct_create, | ||
9438 | .destroy = cpuacct_destroy, | ||
9439 | .populate = cpuacct_populate, | ||
9440 | .subsys_id = cpuacct_subsys_id, | ||
9441 | }; | ||
9442 | #endif /* CONFIG_CGROUP_CPUACCT */ | ||
9443 | |||