]> git.proxmox.com Git - mirror_ubuntu-kernels.git/blame - kernel/sched.c
sched: use alloc_cpumask_var() instead of alloc_bootmem_cpumask_var()
[mirror_ubuntu-kernels.git] / kernel / sched.c
CommitLineData
1da177e4
LT
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
c31f2e8a
IM
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
b9131769
IM
25 * 2007-11-29 RT balancing improvements by Steven Rostedt, Gregory Haskins,
26 * Thomas Gleixner, Mike Kravetz
1da177e4
LT
27 */
28
29#include <linux/mm.h>
30#include <linux/module.h>
31#include <linux/nmi.h>
32#include <linux/init.h>
dff06c15 33#include <linux/uaccess.h>
1da177e4
LT
34#include <linux/highmem.h>
35#include <linux/smp_lock.h>
36#include <asm/mmu_context.h>
37#include <linux/interrupt.h>
c59ede7b 38#include <linux/capability.h>
1da177e4
LT
39#include <linux/completion.h>
40#include <linux/kernel_stat.h>
9a11b49a 41#include <linux/debug_locks.h>
1da177e4
LT
42#include <linux/security.h>
43#include <linux/notifier.h>
44#include <linux/profile.h>
7dfb7103 45#include <linux/freezer.h>
198e2f18 46#include <linux/vmalloc.h>
1da177e4
LT
47#include <linux/blkdev.h>
48#include <linux/delay.h>
b488893a 49#include <linux/pid_namespace.h>
1da177e4
LT
50#include <linux/smp.h>
51#include <linux/threads.h>
52#include <linux/timer.h>
53#include <linux/rcupdate.h>
54#include <linux/cpu.h>
55#include <linux/cpuset.h>
56#include <linux/percpu.h>
57#include <linux/kthread.h>
b5aadf7f 58#include <linux/proc_fs.h>
1da177e4 59#include <linux/seq_file.h>
e692ab53 60#include <linux/sysctl.h>
1da177e4
LT
61#include <linux/syscalls.h>
62#include <linux/times.h>
8f0ab514 63#include <linux/tsacct_kern.h>
c6fd91f0 64#include <linux/kprobes.h>
0ff92245 65#include <linux/delayacct.h>
5517d86b 66#include <linux/reciprocal_div.h>
dff06c15 67#include <linux/unistd.h>
f5ff8422 68#include <linux/pagemap.h>
8f4d37ec 69#include <linux/hrtimer.h>
30914a58 70#include <linux/tick.h>
f00b45c1
PZ
71#include <linux/debugfs.h>
72#include <linux/ctype.h>
6cd8a4bb 73#include <linux/ftrace.h>
1da177e4 74
5517d86b 75#include <asm/tlb.h>
838225b4 76#include <asm/irq_regs.h>
1da177e4 77
6e0534f2
GH
78#include "sched_cpupri.h"
79
a8d154b0 80#define CREATE_TRACE_POINTS
ad8d75ff 81#include <trace/events/sched.h>
a8d154b0 82
1da177e4
LT
83/*
84 * Convert user-nice values [ -20 ... 0 ... 19 ]
85 * to static priority [ MAX_RT_PRIO..MAX_PRIO-1 ],
86 * and back.
87 */
88#define NICE_TO_PRIO(nice) (MAX_RT_PRIO + (nice) + 20)
89#define PRIO_TO_NICE(prio) ((prio) - MAX_RT_PRIO - 20)
90#define TASK_NICE(p) PRIO_TO_NICE((p)->static_prio)
91
92/*
93 * 'User priority' is the nice value converted to something we
94 * can work with better when scaling various scheduler parameters,
95 * it's a [ 0 ... 39 ] range.
96 */
97#define USER_PRIO(p) ((p)-MAX_RT_PRIO)
98#define TASK_USER_PRIO(p) USER_PRIO((p)->static_prio)
99#define MAX_USER_PRIO (USER_PRIO(MAX_PRIO))
100
101/*
d7876a08 102 * Helpers for converting nanosecond timing to jiffy resolution
1da177e4 103 */
d6322faf 104#define NS_TO_JIFFIES(TIME) ((unsigned long)(TIME) / (NSEC_PER_SEC / HZ))
1da177e4 105
6aa645ea
IM
106#define NICE_0_LOAD SCHED_LOAD_SCALE
107#define NICE_0_SHIFT SCHED_LOAD_SHIFT
108
1da177e4
LT
109/*
110 * These are the 'tuning knobs' of the scheduler:
111 *
a4ec24b4 112 * default timeslice is 100 msecs (used only for SCHED_RR tasks).
1da177e4
LT
113 * Timeslices get refilled after they expire.
114 */
1da177e4 115#define DEF_TIMESLICE (100 * HZ / 1000)
2dd73a4f 116
d0b27fa7
PZ
117/*
118 * single value that denotes runtime == period, ie unlimited time.
119 */
120#define RUNTIME_INF ((u64)~0ULL)
121
5517d86b 122#ifdef CONFIG_SMP
fd2ab30b
SN
123
124static void double_rq_lock(struct rq *rq1, struct rq *rq2);
125
5517d86b
ED
126/*
127 * Divide a load by a sched group cpu_power : (load / sg->__cpu_power)
128 * Since cpu_power is a 'constant', we can use a reciprocal divide.
129 */
130static inline u32 sg_div_cpu_power(const struct sched_group *sg, u32 load)
131{
132 return reciprocal_divide(load, sg->reciprocal_cpu_power);
133}
134
135/*
136 * Each time a sched group cpu_power is changed,
137 * we must compute its reciprocal value
138 */
139static inline void sg_inc_cpu_power(struct sched_group *sg, u32 val)
140{
141 sg->__cpu_power += val;
142 sg->reciprocal_cpu_power = reciprocal_value(sg->__cpu_power);
143}
144#endif
145
e05606d3
IM
146static inline int rt_policy(int policy)
147{
3f33a7ce 148 if (unlikely(policy == SCHED_FIFO || policy == SCHED_RR))
e05606d3
IM
149 return 1;
150 return 0;
151}
152
153static inline int task_has_rt_policy(struct task_struct *p)
154{
155 return rt_policy(p->policy);
156}
157
1da177e4 158/*
6aa645ea 159 * This is the priority-queue data structure of the RT scheduling class:
1da177e4 160 */
6aa645ea
IM
161struct rt_prio_array {
162 DECLARE_BITMAP(bitmap, MAX_RT_PRIO+1); /* include 1 bit for delimiter */
163 struct list_head queue[MAX_RT_PRIO];
164};
165
d0b27fa7 166struct rt_bandwidth {
ea736ed5
IM
167 /* nests inside the rq lock: */
168 spinlock_t rt_runtime_lock;
169 ktime_t rt_period;
170 u64 rt_runtime;
171 struct hrtimer rt_period_timer;
d0b27fa7
PZ
172};
173
174static struct rt_bandwidth def_rt_bandwidth;
175
176static int do_sched_rt_period_timer(struct rt_bandwidth *rt_b, int overrun);
177
178static enum hrtimer_restart sched_rt_period_timer(struct hrtimer *timer)
179{
180 struct rt_bandwidth *rt_b =
181 container_of(timer, struct rt_bandwidth, rt_period_timer);
182 ktime_t now;
183 int overrun;
184 int idle = 0;
185
186 for (;;) {
187 now = hrtimer_cb_get_time(timer);
188 overrun = hrtimer_forward(timer, now, rt_b->rt_period);
189
190 if (!overrun)
191 break;
192
193 idle = do_sched_rt_period_timer(rt_b, overrun);
194 }
195
196 return idle ? HRTIMER_NORESTART : HRTIMER_RESTART;
197}
198
199static
200void init_rt_bandwidth(struct rt_bandwidth *rt_b, u64 period, u64 runtime)
201{
202 rt_b->rt_period = ns_to_ktime(period);
203 rt_b->rt_runtime = runtime;
204
ac086bc2
PZ
205 spin_lock_init(&rt_b->rt_runtime_lock);
206
d0b27fa7
PZ
207 hrtimer_init(&rt_b->rt_period_timer,
208 CLOCK_MONOTONIC, HRTIMER_MODE_REL);
209 rt_b->rt_period_timer.function = sched_rt_period_timer;
d0b27fa7
PZ
210}
211
c8bfff6d
KH
212static inline int rt_bandwidth_enabled(void)
213{
214 return sysctl_sched_rt_runtime >= 0;
d0b27fa7
PZ
215}
216
217static void start_rt_bandwidth(struct rt_bandwidth *rt_b)
218{
219 ktime_t now;
220
cac64d00 221 if (!rt_bandwidth_enabled() || rt_b->rt_runtime == RUNTIME_INF)
d0b27fa7
PZ
222 return;
223
224 if (hrtimer_active(&rt_b->rt_period_timer))
225 return;
226
227 spin_lock(&rt_b->rt_runtime_lock);
228 for (;;) {
7f1e2ca9
PZ
229 unsigned long delta;
230 ktime_t soft, hard;
231
d0b27fa7
PZ
232 if (hrtimer_active(&rt_b->rt_period_timer))
233 break;
234
235 now = hrtimer_cb_get_time(&rt_b->rt_period_timer);
236 hrtimer_forward(&rt_b->rt_period_timer, now, rt_b->rt_period);
7f1e2ca9
PZ
237
238 soft = hrtimer_get_softexpires(&rt_b->rt_period_timer);
239 hard = hrtimer_get_expires(&rt_b->rt_period_timer);
240 delta = ktime_to_ns(ktime_sub(hard, soft));
241 __hrtimer_start_range_ns(&rt_b->rt_period_timer, soft, delta,
242 HRTIMER_MODE_ABS, 0);
d0b27fa7
PZ
243 }
244 spin_unlock(&rt_b->rt_runtime_lock);
245}
246
247#ifdef CONFIG_RT_GROUP_SCHED
248static void destroy_rt_bandwidth(struct rt_bandwidth *rt_b)
249{
250 hrtimer_cancel(&rt_b->rt_period_timer);
251}
252#endif
253
712555ee
HC
254/*
255 * sched_domains_mutex serializes calls to arch_init_sched_domains,
256 * detach_destroy_domains and partition_sched_domains.
257 */
258static DEFINE_MUTEX(sched_domains_mutex);
259
052f1dc7 260#ifdef CONFIG_GROUP_SCHED
29f59db3 261
68318b8e
SV
262#include <linux/cgroup.h>
263
29f59db3
SV
264struct cfs_rq;
265
6f505b16
PZ
266static LIST_HEAD(task_groups);
267
29f59db3 268/* task group related information */
4cf86d77 269struct task_group {
052f1dc7 270#ifdef CONFIG_CGROUP_SCHED
68318b8e
SV
271 struct cgroup_subsys_state css;
272#endif
052f1dc7 273
6c415b92
AB
274#ifdef CONFIG_USER_SCHED
275 uid_t uid;
276#endif
277
052f1dc7 278#ifdef CONFIG_FAIR_GROUP_SCHED
29f59db3
SV
279 /* schedulable entities of this group on each cpu */
280 struct sched_entity **se;
281 /* runqueue "owned" by this group on each cpu */
282 struct cfs_rq **cfs_rq;
283 unsigned long shares;
052f1dc7
PZ
284#endif
285
286#ifdef CONFIG_RT_GROUP_SCHED
287 struct sched_rt_entity **rt_se;
288 struct rt_rq **rt_rq;
289
d0b27fa7 290 struct rt_bandwidth rt_bandwidth;
052f1dc7 291#endif
6b2d7700 292
ae8393e5 293 struct rcu_head rcu;
6f505b16 294 struct list_head list;
f473aa5e
PZ
295
296 struct task_group *parent;
297 struct list_head siblings;
298 struct list_head children;
29f59db3
SV
299};
300
354d60c2 301#ifdef CONFIG_USER_SCHED
eff766a6 302
6c415b92
AB
303/* Helper function to pass uid information to create_sched_user() */
304void set_tg_uid(struct user_struct *user)
305{
306 user->tg->uid = user->uid;
307}
308
eff766a6
PZ
309/*
310 * Root task group.
311 * Every UID task group (including init_task_group aka UID-0) will
312 * be a child to this group.
313 */
314struct task_group root_task_group;
315
052f1dc7 316#ifdef CONFIG_FAIR_GROUP_SCHED
29f59db3
SV
317/* Default task group's sched entity on each cpu */
318static DEFINE_PER_CPU(struct sched_entity, init_sched_entity);
319/* Default task group's cfs_rq on each cpu */
320static DEFINE_PER_CPU(struct cfs_rq, init_cfs_rq) ____cacheline_aligned_in_smp;
6d6bc0ad 321#endif /* CONFIG_FAIR_GROUP_SCHED */
052f1dc7
PZ
322
323#ifdef CONFIG_RT_GROUP_SCHED
324static DEFINE_PER_CPU(struct sched_rt_entity, init_sched_rt_entity);
325static DEFINE_PER_CPU(struct rt_rq, init_rt_rq) ____cacheline_aligned_in_smp;
6d6bc0ad 326#endif /* CONFIG_RT_GROUP_SCHED */
9a7e0b18 327#else /* !CONFIG_USER_SCHED */
eff766a6 328#define root_task_group init_task_group
9a7e0b18 329#endif /* CONFIG_USER_SCHED */
6f505b16 330
8ed36996 331/* task_group_lock serializes add/remove of task groups and also changes to
ec2c507f
SV
332 * a task group's cpu shares.
333 */
8ed36996 334static DEFINE_SPINLOCK(task_group_lock);
ec2c507f 335
57310a98
PZ
336#ifdef CONFIG_SMP
337static int root_task_group_empty(void)
338{
339 return list_empty(&root_task_group.children);
340}
341#endif
342
052f1dc7 343#ifdef CONFIG_FAIR_GROUP_SCHED
052f1dc7
PZ
344#ifdef CONFIG_USER_SCHED
345# define INIT_TASK_GROUP_LOAD (2*NICE_0_LOAD)
6d6bc0ad 346#else /* !CONFIG_USER_SCHED */
052f1dc7 347# define INIT_TASK_GROUP_LOAD NICE_0_LOAD
6d6bc0ad 348#endif /* CONFIG_USER_SCHED */
052f1dc7 349
cb4ad1ff 350/*
2e084786
LJ
351 * A weight of 0 or 1 can cause arithmetics problems.
352 * A weight of a cfs_rq is the sum of weights of which entities
353 * are queued on this cfs_rq, so a weight of a entity should not be
354 * too large, so as the shares value of a task group.
cb4ad1ff
MX
355 * (The default weight is 1024 - so there's no practical
356 * limitation from this.)
357 */
18d95a28 358#define MIN_SHARES 2
2e084786 359#define MAX_SHARES (1UL << 18)
18d95a28 360
052f1dc7
PZ
361static int init_task_group_load = INIT_TASK_GROUP_LOAD;
362#endif
363
29f59db3 364/* Default task group.
3a252015 365 * Every task in system belong to this group at bootup.
29f59db3 366 */
434d53b0 367struct task_group init_task_group;
29f59db3
SV
368
369/* return group to which a task belongs */
4cf86d77 370static inline struct task_group *task_group(struct task_struct *p)
29f59db3 371{
4cf86d77 372 struct task_group *tg;
9b5b7751 373
052f1dc7 374#ifdef CONFIG_USER_SCHED
c69e8d9c
DH
375 rcu_read_lock();
376 tg = __task_cred(p)->user->tg;
377 rcu_read_unlock();
052f1dc7 378#elif defined(CONFIG_CGROUP_SCHED)
68318b8e
SV
379 tg = container_of(task_subsys_state(p, cpu_cgroup_subsys_id),
380 struct task_group, css);
24e377a8 381#else
41a2d6cf 382 tg = &init_task_group;
24e377a8 383#endif
9b5b7751 384 return tg;
29f59db3
SV
385}
386
387/* Change a task's cfs_rq and parent entity if it moves across CPUs/groups */
6f505b16 388static inline void set_task_rq(struct task_struct *p, unsigned int cpu)
29f59db3 389{
052f1dc7 390#ifdef CONFIG_FAIR_GROUP_SCHED
ce96b5ac
DA
391 p->se.cfs_rq = task_group(p)->cfs_rq[cpu];
392 p->se.parent = task_group(p)->se[cpu];
052f1dc7 393#endif
6f505b16 394
052f1dc7 395#ifdef CONFIG_RT_GROUP_SCHED
6f505b16
PZ
396 p->rt.rt_rq = task_group(p)->rt_rq[cpu];
397 p->rt.parent = task_group(p)->rt_se[cpu];
052f1dc7 398#endif
29f59db3
SV
399}
400
401#else
402
57310a98
PZ
403#ifdef CONFIG_SMP
404static int root_task_group_empty(void)
405{
406 return 1;
407}
408#endif
409
6f505b16 410static inline void set_task_rq(struct task_struct *p, unsigned int cpu) { }
83378269
PZ
411static inline struct task_group *task_group(struct task_struct *p)
412{
413 return NULL;
414}
29f59db3 415
052f1dc7 416#endif /* CONFIG_GROUP_SCHED */
29f59db3 417
6aa645ea
IM
418/* CFS-related fields in a runqueue */
419struct cfs_rq {
420 struct load_weight load;
421 unsigned long nr_running;
422
6aa645ea 423 u64 exec_clock;
e9acbff6 424 u64 min_vruntime;
6aa645ea
IM
425
426 struct rb_root tasks_timeline;
427 struct rb_node *rb_leftmost;
4a55bd5e
PZ
428
429 struct list_head tasks;
430 struct list_head *balance_iterator;
431
432 /*
433 * 'curr' points to currently running entity on this cfs_rq.
6aa645ea
IM
434 * It is set to NULL otherwise (i.e when none are currently running).
435 */
4793241b 436 struct sched_entity *curr, *next, *last;
ddc97297 437
5ac5c4d6 438 unsigned int nr_spread_over;
ddc97297 439
62160e3f 440#ifdef CONFIG_FAIR_GROUP_SCHED
6aa645ea
IM
441 struct rq *rq; /* cpu runqueue to which this cfs_rq is attached */
442
41a2d6cf
IM
443 /*
444 * leaf cfs_rqs are those that hold tasks (lowest schedulable entity in
6aa645ea
IM
445 * a hierarchy). Non-leaf lrqs hold other higher schedulable entities
446 * (like users, containers etc.)
447 *
448 * leaf_cfs_rq_list ties together list of leaf cfs_rq's in a cpu. This
449 * list is used during load balance.
450 */
41a2d6cf
IM
451 struct list_head leaf_cfs_rq_list;
452 struct task_group *tg; /* group that "owns" this runqueue */
c09595f6
PZ
453
454#ifdef CONFIG_SMP
c09595f6 455 /*
c8cba857 456 * the part of load.weight contributed by tasks
c09595f6 457 */
c8cba857 458 unsigned long task_weight;
c09595f6 459
c8cba857
PZ
460 /*
461 * h_load = weight * f(tg)
462 *
463 * Where f(tg) is the recursive weight fraction assigned to
464 * this group.
465 */
466 unsigned long h_load;
c09595f6 467
c8cba857
PZ
468 /*
469 * this cpu's part of tg->shares
470 */
471 unsigned long shares;
f1d239f7
PZ
472
473 /*
474 * load.weight at the time we set shares
475 */
476 unsigned long rq_weight;
c09595f6 477#endif
6aa645ea
IM
478#endif
479};
1da177e4 480
6aa645ea
IM
481/* Real-Time classes' related field in a runqueue: */
482struct rt_rq {
483 struct rt_prio_array active;
63489e45 484 unsigned long rt_nr_running;
052f1dc7 485#if defined CONFIG_SMP || defined CONFIG_RT_GROUP_SCHED
e864c499
GH
486 struct {
487 int curr; /* highest queued rt task prio */
398a153b 488#ifdef CONFIG_SMP
e864c499 489 int next; /* next highest */
398a153b 490#endif
e864c499 491 } highest_prio;
6f505b16 492#endif
fa85ae24 493#ifdef CONFIG_SMP
73fe6aae 494 unsigned long rt_nr_migratory;
a22d7fc1 495 int overloaded;
917b627d 496 struct plist_head pushable_tasks;
fa85ae24 497#endif
6f505b16 498 int rt_throttled;
fa85ae24 499 u64 rt_time;
ac086bc2 500 u64 rt_runtime;
ea736ed5 501 /* Nests inside the rq lock: */
ac086bc2 502 spinlock_t rt_runtime_lock;
6f505b16 503
052f1dc7 504#ifdef CONFIG_RT_GROUP_SCHED
23b0fdfc
PZ
505 unsigned long rt_nr_boosted;
506
6f505b16
PZ
507 struct rq *rq;
508 struct list_head leaf_rt_rq_list;
509 struct task_group *tg;
510 struct sched_rt_entity *rt_se;
511#endif
6aa645ea
IM
512};
513
57d885fe
GH
514#ifdef CONFIG_SMP
515
516/*
517 * We add the notion of a root-domain which will be used to define per-domain
0eab9146
IM
518 * variables. Each exclusive cpuset essentially defines an island domain by
519 * fully partitioning the member cpus from any other cpuset. Whenever a new
57d885fe
GH
520 * exclusive cpuset is created, we also create and attach a new root-domain
521 * object.
522 *
57d885fe
GH
523 */
524struct root_domain {
525 atomic_t refcount;
c6c4927b
RR
526 cpumask_var_t span;
527 cpumask_var_t online;
637f5085 528
0eab9146 529 /*
637f5085
GH
530 * The "RT overload" flag: it gets set if a CPU has more than
531 * one runnable RT task.
532 */
c6c4927b 533 cpumask_var_t rto_mask;
0eab9146 534 atomic_t rto_count;
6e0534f2
GH
535#ifdef CONFIG_SMP
536 struct cpupri cpupri;
537#endif
7a09b1a2
VS
538#if defined(CONFIG_SCHED_MC) || defined(CONFIG_SCHED_SMT)
539 /*
540 * Preferred wake up cpu nominated by sched_mc balance that will be
541 * used when most cpus are idle in the system indicating overall very
542 * low system utilisation. Triggered at POWERSAVINGS_BALANCE_WAKEUP(2)
543 */
544 unsigned int sched_mc_preferred_wakeup_cpu;
545#endif
57d885fe
GH
546};
547
dc938520
GH
548/*
549 * By default the system creates a single root-domain with all cpus as
550 * members (mimicking the global state we have today).
551 */
57d885fe
GH
552static struct root_domain def_root_domain;
553
554#endif
555
1da177e4
LT
556/*
557 * This is the main, per-CPU runqueue data structure.
558 *
559 * Locking rule: those places that want to lock multiple runqueues
560 * (such as the load balancing or the thread migration code), lock
561 * acquire operations must be ordered by ascending &runqueue.
562 */
70b97a7f 563struct rq {
d8016491
IM
564 /* runqueue lock: */
565 spinlock_t lock;
1da177e4
LT
566
567 /*
568 * nr_running and cpu_load should be in the same cacheline because
569 * remote CPUs use both these fields when doing load calculation.
570 */
571 unsigned long nr_running;
6aa645ea
IM
572 #define CPU_LOAD_IDX_MAX 5
573 unsigned long cpu_load[CPU_LOAD_IDX_MAX];
46cb4b7c 574#ifdef CONFIG_NO_HZ
15934a37 575 unsigned long last_tick_seen;
46cb4b7c
SS
576 unsigned char in_nohz_recently;
577#endif
d8016491
IM
578 /* capture load from *all* tasks on this cpu: */
579 struct load_weight load;
6aa645ea
IM
580 unsigned long nr_load_updates;
581 u64 nr_switches;
582
583 struct cfs_rq cfs;
6f505b16 584 struct rt_rq rt;
6f505b16 585
6aa645ea 586#ifdef CONFIG_FAIR_GROUP_SCHED
d8016491
IM
587 /* list of leaf cfs_rq on this cpu: */
588 struct list_head leaf_cfs_rq_list;
052f1dc7
PZ
589#endif
590#ifdef CONFIG_RT_GROUP_SCHED
6f505b16 591 struct list_head leaf_rt_rq_list;
1da177e4 592#endif
1da177e4
LT
593
594 /*
595 * This is part of a global counter where only the total sum
596 * over all CPUs matters. A task can increase this counter on
597 * one CPU and if it got migrated afterwards it may decrease
598 * it on another CPU. Always updated under the runqueue lock:
599 */
600 unsigned long nr_uninterruptible;
601
36c8b586 602 struct task_struct *curr, *idle;
c9819f45 603 unsigned long next_balance;
1da177e4 604 struct mm_struct *prev_mm;
6aa645ea 605
3e51f33f 606 u64 clock;
6aa645ea 607
1da177e4
LT
608 atomic_t nr_iowait;
609
610#ifdef CONFIG_SMP
0eab9146 611 struct root_domain *rd;
1da177e4
LT
612 struct sched_domain *sd;
613
a0a522ce 614 unsigned char idle_at_tick;
1da177e4
LT
615 /* For active balancing */
616 int active_balance;
617 int push_cpu;
d8016491
IM
618 /* cpu of this runqueue: */
619 int cpu;
1f11eb6a 620 int online;
1da177e4 621
a8a51d5e 622 unsigned long avg_load_per_task;
1da177e4 623
36c8b586 624 struct task_struct *migration_thread;
1da177e4
LT
625 struct list_head migration_queue;
626#endif
627
dce48a84
TG
628 /* calc_load related fields */
629 unsigned long calc_load_update;
630 long calc_load_active;
631
8f4d37ec 632#ifdef CONFIG_SCHED_HRTICK
31656519
PZ
633#ifdef CONFIG_SMP
634 int hrtick_csd_pending;
635 struct call_single_data hrtick_csd;
636#endif
8f4d37ec
PZ
637 struct hrtimer hrtick_timer;
638#endif
639
1da177e4
LT
640#ifdef CONFIG_SCHEDSTATS
641 /* latency stats */
642 struct sched_info rq_sched_info;
9c2c4802
KC
643 unsigned long long rq_cpu_time;
644 /* could above be rq->cfs_rq.exec_clock + rq->rt_rq.rt_runtime ? */
1da177e4
LT
645
646 /* sys_sched_yield() stats */
480b9434 647 unsigned int yld_count;
1da177e4
LT
648
649 /* schedule() stats */
480b9434
KC
650 unsigned int sched_switch;
651 unsigned int sched_count;
652 unsigned int sched_goidle;
1da177e4
LT
653
654 /* try_to_wake_up() stats */
480b9434
KC
655 unsigned int ttwu_count;
656 unsigned int ttwu_local;
b8efb561
IM
657
658 /* BKL stats */
480b9434 659 unsigned int bkl_count;
1da177e4
LT
660#endif
661};
662
f34e3b61 663static DEFINE_PER_CPU_SHARED_ALIGNED(struct rq, runqueues);
1da177e4 664
15afe09b 665static inline void check_preempt_curr(struct rq *rq, struct task_struct *p, int sync)
dd41f596 666{
15afe09b 667 rq->curr->sched_class->check_preempt_curr(rq, p, sync);
dd41f596
IM
668}
669
0a2966b4
CL
670static inline int cpu_of(struct rq *rq)
671{
672#ifdef CONFIG_SMP
673 return rq->cpu;
674#else
675 return 0;
676#endif
677}
678
674311d5
NP
679/*
680 * The domain tree (rq->sd) is protected by RCU's quiescent state transition.
1a20ff27 681 * See detach_destroy_domains: synchronize_sched for details.
674311d5
NP
682 *
683 * The domain tree of any CPU may only be accessed from within
684 * preempt-disabled sections.
685 */
48f24c4d
IM
686#define for_each_domain(cpu, __sd) \
687 for (__sd = rcu_dereference(cpu_rq(cpu)->sd); __sd; __sd = __sd->parent)
1da177e4
LT
688
689#define cpu_rq(cpu) (&per_cpu(runqueues, (cpu)))
690#define this_rq() (&__get_cpu_var(runqueues))
691#define task_rq(p) cpu_rq(task_cpu(p))
692#define cpu_curr(cpu) (cpu_rq(cpu)->curr)
693
3e51f33f
PZ
694static inline void update_rq_clock(struct rq *rq)
695{
696 rq->clock = sched_clock_cpu(cpu_of(rq));
697}
698
bf5c91ba
IM
699/*
700 * Tunables that become constants when CONFIG_SCHED_DEBUG is off:
701 */
702#ifdef CONFIG_SCHED_DEBUG
703# define const_debug __read_mostly
704#else
705# define const_debug static const
706#endif
707
017730c1
IM
708/**
709 * runqueue_is_locked
710 *
711 * Returns true if the current cpu runqueue is locked.
712 * This interface allows printk to be called with the runqueue lock
713 * held and know whether or not it is OK to wake up the klogd.
714 */
715int runqueue_is_locked(void)
716{
717 int cpu = get_cpu();
718 struct rq *rq = cpu_rq(cpu);
719 int ret;
720
721 ret = spin_is_locked(&rq->lock);
722 put_cpu();
723 return ret;
724}
725
bf5c91ba
IM
726/*
727 * Debugging: various feature bits
728 */
f00b45c1
PZ
729
730#define SCHED_FEAT(name, enabled) \
731 __SCHED_FEAT_##name ,
732
bf5c91ba 733enum {
f00b45c1 734#include "sched_features.h"
bf5c91ba
IM
735};
736
f00b45c1
PZ
737#undef SCHED_FEAT
738
739#define SCHED_FEAT(name, enabled) \
740 (1UL << __SCHED_FEAT_##name) * enabled |
741
bf5c91ba 742const_debug unsigned int sysctl_sched_features =
f00b45c1
PZ
743#include "sched_features.h"
744 0;
745
746#undef SCHED_FEAT
747
748#ifdef CONFIG_SCHED_DEBUG
749#define SCHED_FEAT(name, enabled) \
750 #name ,
751
983ed7a6 752static __read_mostly char *sched_feat_names[] = {
f00b45c1
PZ
753#include "sched_features.h"
754 NULL
755};
756
757#undef SCHED_FEAT
758
34f3a814 759static int sched_feat_show(struct seq_file *m, void *v)
f00b45c1 760{
f00b45c1
PZ
761 int i;
762
763 for (i = 0; sched_feat_names[i]; i++) {
34f3a814
LZ
764 if (!(sysctl_sched_features & (1UL << i)))
765 seq_puts(m, "NO_");
766 seq_printf(m, "%s ", sched_feat_names[i]);
f00b45c1 767 }
34f3a814 768 seq_puts(m, "\n");
f00b45c1 769
34f3a814 770 return 0;
f00b45c1
PZ
771}
772
773static ssize_t
774sched_feat_write(struct file *filp, const char __user *ubuf,
775 size_t cnt, loff_t *ppos)
776{
777 char buf[64];
778 char *cmp = buf;
779 int neg = 0;
780 int i;
781
782 if (cnt > 63)
783 cnt = 63;
784
785 if (copy_from_user(&buf, ubuf, cnt))
786 return -EFAULT;
787
788 buf[cnt] = 0;
789
c24b7c52 790 if (strncmp(buf, "NO_", 3) == 0) {
f00b45c1
PZ
791 neg = 1;
792 cmp += 3;
793 }
794
795 for (i = 0; sched_feat_names[i]; i++) {
796 int len = strlen(sched_feat_names[i]);
797
798 if (strncmp(cmp, sched_feat_names[i], len) == 0) {
799 if (neg)
800 sysctl_sched_features &= ~(1UL << i);
801 else
802 sysctl_sched_features |= (1UL << i);
803 break;
804 }
805 }
806
807 if (!sched_feat_names[i])
808 return -EINVAL;
809
810 filp->f_pos += cnt;
811
812 return cnt;
813}
814
34f3a814
LZ
815static int sched_feat_open(struct inode *inode, struct file *filp)
816{
817 return single_open(filp, sched_feat_show, NULL);
818}
819
f00b45c1 820static struct file_operations sched_feat_fops = {
34f3a814
LZ
821 .open = sched_feat_open,
822 .write = sched_feat_write,
823 .read = seq_read,
824 .llseek = seq_lseek,
825 .release = single_release,
f00b45c1
PZ
826};
827
828static __init int sched_init_debug(void)
829{
f00b45c1
PZ
830 debugfs_create_file("sched_features", 0644, NULL, NULL,
831 &sched_feat_fops);
832
833 return 0;
834}
835late_initcall(sched_init_debug);
836
837#endif
838
839#define sched_feat(x) (sysctl_sched_features & (1UL << __SCHED_FEAT_##x))
bf5c91ba 840
b82d9fdd
PZ
841/*
842 * Number of tasks to iterate in a single balance run.
843 * Limited because this is done with IRQs disabled.
844 */
845const_debug unsigned int sysctl_sched_nr_migrate = 32;
846
2398f2c6
PZ
847/*
848 * ratelimit for updating the group shares.
55cd5340 849 * default: 0.25ms
2398f2c6 850 */
55cd5340 851unsigned int sysctl_sched_shares_ratelimit = 250000;
2398f2c6 852
ffda12a1
PZ
853/*
854 * Inject some fuzzyness into changing the per-cpu group shares
855 * this avoids remote rq-locks at the expense of fairness.
856 * default: 4
857 */
858unsigned int sysctl_sched_shares_thresh = 4;
859
fa85ae24 860/*
9f0c1e56 861 * period over which we measure -rt task cpu usage in us.
fa85ae24
PZ
862 * default: 1s
863 */
9f0c1e56 864unsigned int sysctl_sched_rt_period = 1000000;
fa85ae24 865
6892b75e
IM
866static __read_mostly int scheduler_running;
867
9f0c1e56
PZ
868/*
869 * part of the period that we allow rt tasks to run in us.
870 * default: 0.95s
871 */
872int sysctl_sched_rt_runtime = 950000;
fa85ae24 873
d0b27fa7
PZ
874static inline u64 global_rt_period(void)
875{
876 return (u64)sysctl_sched_rt_period * NSEC_PER_USEC;
877}
878
879static inline u64 global_rt_runtime(void)
880{
e26873bb 881 if (sysctl_sched_rt_runtime < 0)
d0b27fa7
PZ
882 return RUNTIME_INF;
883
884 return (u64)sysctl_sched_rt_runtime * NSEC_PER_USEC;
885}
fa85ae24 886
1da177e4 887#ifndef prepare_arch_switch
4866cde0
NP
888# define prepare_arch_switch(next) do { } while (0)
889#endif
890#ifndef finish_arch_switch
891# define finish_arch_switch(prev) do { } while (0)
892#endif
893
051a1d1a
DA
894static inline int task_current(struct rq *rq, struct task_struct *p)
895{
896 return rq->curr == p;
897}
898
4866cde0 899#ifndef __ARCH_WANT_UNLOCKED_CTXSW
70b97a7f 900static inline int task_running(struct rq *rq, struct task_struct *p)
4866cde0 901{
051a1d1a 902 return task_current(rq, p);
4866cde0
NP
903}
904
70b97a7f 905static inline void prepare_lock_switch(struct rq *rq, struct task_struct *next)
4866cde0
NP
906{
907}
908
70b97a7f 909static inline void finish_lock_switch(struct rq *rq, struct task_struct *prev)
4866cde0 910{
da04c035
IM
911#ifdef CONFIG_DEBUG_SPINLOCK
912 /* this is a valid case when another task releases the spinlock */
913 rq->lock.owner = current;
914#endif
8a25d5de
IM
915 /*
916 * If we are tracking spinlock dependencies then we have to
917 * fix up the runqueue lock - which gets 'carried over' from
918 * prev into current:
919 */
920 spin_acquire(&rq->lock.dep_map, 0, 0, _THIS_IP_);
921
4866cde0
NP
922 spin_unlock_irq(&rq->lock);
923}
924
925#else /* __ARCH_WANT_UNLOCKED_CTXSW */
70b97a7f 926static inline int task_running(struct rq *rq, struct task_struct *p)
4866cde0
NP
927{
928#ifdef CONFIG_SMP
929 return p->oncpu;
930#else
051a1d1a 931 return task_current(rq, p);
4866cde0
NP
932#endif
933}
934
70b97a7f 935static inline void prepare_lock_switch(struct rq *rq, struct task_struct *next)
4866cde0
NP
936{
937#ifdef CONFIG_SMP
938 /*
939 * We can optimise this out completely for !SMP, because the
940 * SMP rebalancing from interrupt is the only thing that cares
941 * here.
942 */
943 next->oncpu = 1;
944#endif
945#ifdef __ARCH_WANT_INTERRUPTS_ON_CTXSW
946 spin_unlock_irq(&rq->lock);
947#else
948 spin_unlock(&rq->lock);
949#endif
950}
951
70b97a7f 952static inline void finish_lock_switch(struct rq *rq, struct task_struct *prev)
4866cde0
NP
953{
954#ifdef CONFIG_SMP
955 /*
956 * After ->oncpu is cleared, the task can be moved to a different CPU.
957 * We must ensure this doesn't happen until the switch is completely
958 * finished.
959 */
960 smp_wmb();
961 prev->oncpu = 0;
962#endif
963#ifndef __ARCH_WANT_INTERRUPTS_ON_CTXSW
964 local_irq_enable();
1da177e4 965#endif
4866cde0
NP
966}
967#endif /* __ARCH_WANT_UNLOCKED_CTXSW */
1da177e4 968
b29739f9
IM
969/*
970 * __task_rq_lock - lock the runqueue a given task resides on.
971 * Must be called interrupts disabled.
972 */
70b97a7f 973static inline struct rq *__task_rq_lock(struct task_struct *p)
b29739f9
IM
974 __acquires(rq->lock)
975{
3a5c359a
AK
976 for (;;) {
977 struct rq *rq = task_rq(p);
978 spin_lock(&rq->lock);
979 if (likely(rq == task_rq(p)))
980 return rq;
b29739f9 981 spin_unlock(&rq->lock);
b29739f9 982 }
b29739f9
IM
983}
984
1da177e4
LT
985/*
986 * task_rq_lock - lock the runqueue a given task resides on and disable
41a2d6cf 987 * interrupts. Note the ordering: we can safely lookup the task_rq without
1da177e4
LT
988 * explicitly disabling preemption.
989 */
70b97a7f 990static struct rq *task_rq_lock(struct task_struct *p, unsigned long *flags)
1da177e4
LT
991 __acquires(rq->lock)
992{
70b97a7f 993 struct rq *rq;
1da177e4 994
3a5c359a
AK
995 for (;;) {
996 local_irq_save(*flags);
997 rq = task_rq(p);
998 spin_lock(&rq->lock);
999 if (likely(rq == task_rq(p)))
1000 return rq;
1da177e4 1001 spin_unlock_irqrestore(&rq->lock, *flags);
1da177e4 1002 }
1da177e4
LT
1003}
1004
ad474cac
ON
1005void task_rq_unlock_wait(struct task_struct *p)
1006{
1007 struct rq *rq = task_rq(p);
1008
1009 smp_mb(); /* spin-unlock-wait is not a full memory barrier */
1010 spin_unlock_wait(&rq->lock);
1011}
1012
a9957449 1013static void __task_rq_unlock(struct rq *rq)
b29739f9
IM
1014 __releases(rq->lock)
1015{
1016 spin_unlock(&rq->lock);
1017}
1018
70b97a7f 1019static inline void task_rq_unlock(struct rq *rq, unsigned long *flags)
1da177e4
LT
1020 __releases(rq->lock)
1021{
1022 spin_unlock_irqrestore(&rq->lock, *flags);
1023}
1024
1da177e4 1025/*
cc2a73b5 1026 * this_rq_lock - lock this runqueue and disable interrupts.
1da177e4 1027 */
a9957449 1028static struct rq *this_rq_lock(void)
1da177e4
LT
1029 __acquires(rq->lock)
1030{
70b97a7f 1031 struct rq *rq;
1da177e4
LT
1032
1033 local_irq_disable();
1034 rq = this_rq();
1035 spin_lock(&rq->lock);
1036
1037 return rq;
1038}
1039
8f4d37ec
PZ
1040#ifdef CONFIG_SCHED_HRTICK
1041/*
1042 * Use HR-timers to deliver accurate preemption points.
1043 *
1044 * Its all a bit involved since we cannot program an hrt while holding the
1045 * rq->lock. So what we do is store a state in in rq->hrtick_* and ask for a
1046 * reschedule event.
1047 *
1048 * When we get rescheduled we reprogram the hrtick_timer outside of the
1049 * rq->lock.
1050 */
8f4d37ec
PZ
1051
1052/*
1053 * Use hrtick when:
1054 * - enabled by features
1055 * - hrtimer is actually high res
1056 */
1057static inline int hrtick_enabled(struct rq *rq)
1058{
1059 if (!sched_feat(HRTICK))
1060 return 0;
ba42059f 1061 if (!cpu_active(cpu_of(rq)))
b328ca18 1062 return 0;
8f4d37ec
PZ
1063 return hrtimer_is_hres_active(&rq->hrtick_timer);
1064}
1065
8f4d37ec
PZ
1066static void hrtick_clear(struct rq *rq)
1067{
1068 if (hrtimer_active(&rq->hrtick_timer))
1069 hrtimer_cancel(&rq->hrtick_timer);
1070}
1071
8f4d37ec
PZ
1072/*
1073 * High-resolution timer tick.
1074 * Runs from hardirq context with interrupts disabled.
1075 */
1076static enum hrtimer_restart hrtick(struct hrtimer *timer)
1077{
1078 struct rq *rq = container_of(timer, struct rq, hrtick_timer);
1079
1080 WARN_ON_ONCE(cpu_of(rq) != smp_processor_id());
1081
1082 spin_lock(&rq->lock);
3e51f33f 1083 update_rq_clock(rq);
8f4d37ec
PZ
1084 rq->curr->sched_class->task_tick(rq, rq->curr, 1);
1085 spin_unlock(&rq->lock);
1086
1087 return HRTIMER_NORESTART;
1088}
1089
95e904c7 1090#ifdef CONFIG_SMP
31656519
PZ
1091/*
1092 * called from hardirq (IPI) context
1093 */
1094static void __hrtick_start(void *arg)
b328ca18 1095{
31656519 1096 struct rq *rq = arg;
b328ca18 1097
31656519
PZ
1098 spin_lock(&rq->lock);
1099 hrtimer_restart(&rq->hrtick_timer);
1100 rq->hrtick_csd_pending = 0;
1101 spin_unlock(&rq->lock);
b328ca18
PZ
1102}
1103
31656519
PZ
1104/*
1105 * Called to set the hrtick timer state.
1106 *
1107 * called with rq->lock held and irqs disabled
1108 */
1109static void hrtick_start(struct rq *rq, u64 delay)
b328ca18 1110{
31656519
PZ
1111 struct hrtimer *timer = &rq->hrtick_timer;
1112 ktime_t time = ktime_add_ns(timer->base->get_time(), delay);
b328ca18 1113
cc584b21 1114 hrtimer_set_expires(timer, time);
31656519
PZ
1115
1116 if (rq == this_rq()) {
1117 hrtimer_restart(timer);
1118 } else if (!rq->hrtick_csd_pending) {
6e275637 1119 __smp_call_function_single(cpu_of(rq), &rq->hrtick_csd, 0);
31656519
PZ
1120 rq->hrtick_csd_pending = 1;
1121 }
b328ca18
PZ
1122}
1123
1124static int
1125hotplug_hrtick(struct notifier_block *nfb, unsigned long action, void *hcpu)
1126{
1127 int cpu = (int)(long)hcpu;
1128
1129 switch (action) {
1130 case CPU_UP_CANCELED:
1131 case CPU_UP_CANCELED_FROZEN:
1132 case CPU_DOWN_PREPARE:
1133 case CPU_DOWN_PREPARE_FROZEN:
1134 case CPU_DEAD:
1135 case CPU_DEAD_FROZEN:
31656519 1136 hrtick_clear(cpu_rq(cpu));
b328ca18
PZ
1137 return NOTIFY_OK;
1138 }
1139
1140 return NOTIFY_DONE;
1141}
1142
fa748203 1143static __init void init_hrtick(void)
b328ca18
PZ
1144{
1145 hotcpu_notifier(hotplug_hrtick, 0);
1146}
31656519
PZ
1147#else
1148/*
1149 * Called to set the hrtick timer state.
1150 *
1151 * called with rq->lock held and irqs disabled
1152 */
1153static void hrtick_start(struct rq *rq, u64 delay)
1154{
7f1e2ca9
PZ
1155 __hrtimer_start_range_ns(&rq->hrtick_timer, ns_to_ktime(delay), 0,
1156 HRTIMER_MODE_REL, 0);
31656519 1157}
b328ca18 1158
006c75f1 1159static inline void init_hrtick(void)
8f4d37ec 1160{
8f4d37ec 1161}
31656519 1162#endif /* CONFIG_SMP */
8f4d37ec 1163
31656519 1164static void init_rq_hrtick(struct rq *rq)
8f4d37ec 1165{
31656519
PZ
1166#ifdef CONFIG_SMP
1167 rq->hrtick_csd_pending = 0;
8f4d37ec 1168
31656519
PZ
1169 rq->hrtick_csd.flags = 0;
1170 rq->hrtick_csd.func = __hrtick_start;
1171 rq->hrtick_csd.info = rq;
1172#endif
8f4d37ec 1173
31656519
PZ
1174 hrtimer_init(&rq->hrtick_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
1175 rq->hrtick_timer.function = hrtick;
8f4d37ec 1176}
006c75f1 1177#else /* CONFIG_SCHED_HRTICK */
8f4d37ec
PZ
1178static inline void hrtick_clear(struct rq *rq)
1179{
1180}
1181
8f4d37ec
PZ
1182static inline void init_rq_hrtick(struct rq *rq)
1183{
1184}
1185
b328ca18
PZ
1186static inline void init_hrtick(void)
1187{
1188}
006c75f1 1189#endif /* CONFIG_SCHED_HRTICK */
8f4d37ec 1190
c24d20db
IM
1191/*
1192 * resched_task - mark a task 'to be rescheduled now'.
1193 *
1194 * On UP this means the setting of the need_resched flag, on SMP it
1195 * might also involve a cross-CPU call to trigger the scheduler on
1196 * the target CPU.
1197 */
1198#ifdef CONFIG_SMP
1199
1200#ifndef tsk_is_polling
1201#define tsk_is_polling(t) test_tsk_thread_flag(t, TIF_POLLING_NRFLAG)
1202#endif
1203
31656519 1204static void resched_task(struct task_struct *p)
c24d20db
IM
1205{
1206 int cpu;
1207
1208 assert_spin_locked(&task_rq(p)->lock);
1209
5ed0cec0 1210 if (test_tsk_need_resched(p))
c24d20db
IM
1211 return;
1212
5ed0cec0 1213 set_tsk_need_resched(p);
c24d20db
IM
1214
1215 cpu = task_cpu(p);
1216 if (cpu == smp_processor_id())
1217 return;
1218
1219 /* NEED_RESCHED must be visible before we test polling */
1220 smp_mb();
1221 if (!tsk_is_polling(p))
1222 smp_send_reschedule(cpu);
1223}
1224
1225static void resched_cpu(int cpu)
1226{
1227 struct rq *rq = cpu_rq(cpu);
1228 unsigned long flags;
1229
1230 if (!spin_trylock_irqsave(&rq->lock, flags))
1231 return;
1232 resched_task(cpu_curr(cpu));
1233 spin_unlock_irqrestore(&rq->lock, flags);
1234}
06d8308c
TG
1235
1236#ifdef CONFIG_NO_HZ
1237/*
1238 * When add_timer_on() enqueues a timer into the timer wheel of an
1239 * idle CPU then this timer might expire before the next timer event
1240 * which is scheduled to wake up that CPU. In case of a completely
1241 * idle system the next event might even be infinite time into the
1242 * future. wake_up_idle_cpu() ensures that the CPU is woken up and
1243 * leaves the inner idle loop so the newly added timer is taken into
1244 * account when the CPU goes back to idle and evaluates the timer
1245 * wheel for the next timer event.
1246 */
1247void wake_up_idle_cpu(int cpu)
1248{
1249 struct rq *rq = cpu_rq(cpu);
1250
1251 if (cpu == smp_processor_id())
1252 return;
1253
1254 /*
1255 * This is safe, as this function is called with the timer
1256 * wheel base lock of (cpu) held. When the CPU is on the way
1257 * to idle and has not yet set rq->curr to idle then it will
1258 * be serialized on the timer wheel base lock and take the new
1259 * timer into account automatically.
1260 */
1261 if (rq->curr != rq->idle)
1262 return;
1263
1264 /*
1265 * We can set TIF_RESCHED on the idle task of the other CPU
1266 * lockless. The worst case is that the other CPU runs the
1267 * idle task through an additional NOOP schedule()
1268 */
5ed0cec0 1269 set_tsk_need_resched(rq->idle);
06d8308c
TG
1270
1271 /* NEED_RESCHED must be visible before we test polling */
1272 smp_mb();
1273 if (!tsk_is_polling(rq->idle))
1274 smp_send_reschedule(cpu);
1275}
6d6bc0ad 1276#endif /* CONFIG_NO_HZ */
06d8308c 1277
6d6bc0ad 1278#else /* !CONFIG_SMP */
31656519 1279static void resched_task(struct task_struct *p)
c24d20db
IM
1280{
1281 assert_spin_locked(&task_rq(p)->lock);
31656519 1282 set_tsk_need_resched(p);
c24d20db 1283}
6d6bc0ad 1284#endif /* CONFIG_SMP */
c24d20db 1285
45bf76df
IM
1286#if BITS_PER_LONG == 32
1287# define WMULT_CONST (~0UL)
1288#else
1289# define WMULT_CONST (1UL << 32)
1290#endif
1291
1292#define WMULT_SHIFT 32
1293
194081eb
IM
1294/*
1295 * Shift right and round:
1296 */
cf2ab469 1297#define SRR(x, y) (((x) + (1UL << ((y) - 1))) >> (y))
194081eb 1298
a7be37ac
PZ
1299/*
1300 * delta *= weight / lw
1301 */
cb1c4fc9 1302static unsigned long
45bf76df
IM
1303calc_delta_mine(unsigned long delta_exec, unsigned long weight,
1304 struct load_weight *lw)
1305{
1306 u64 tmp;
1307
7a232e03
LJ
1308 if (!lw->inv_weight) {
1309 if (BITS_PER_LONG > 32 && unlikely(lw->weight >= WMULT_CONST))
1310 lw->inv_weight = 1;
1311 else
1312 lw->inv_weight = 1 + (WMULT_CONST-lw->weight/2)
1313 / (lw->weight+1);
1314 }
45bf76df
IM
1315
1316 tmp = (u64)delta_exec * weight;
1317 /*
1318 * Check whether we'd overflow the 64-bit multiplication:
1319 */
194081eb 1320 if (unlikely(tmp > WMULT_CONST))
cf2ab469 1321 tmp = SRR(SRR(tmp, WMULT_SHIFT/2) * lw->inv_weight,
194081eb
IM
1322 WMULT_SHIFT/2);
1323 else
cf2ab469 1324 tmp = SRR(tmp * lw->inv_weight, WMULT_SHIFT);
45bf76df 1325
ecf691da 1326 return (unsigned long)min(tmp, (u64)(unsigned long)LONG_MAX);
45bf76df
IM
1327}
1328
1091985b 1329static inline void update_load_add(struct load_weight *lw, unsigned long inc)
45bf76df
IM
1330{
1331 lw->weight += inc;
e89996ae 1332 lw->inv_weight = 0;
45bf76df
IM
1333}
1334
1091985b 1335static inline void update_load_sub(struct load_weight *lw, unsigned long dec)
45bf76df
IM
1336{
1337 lw->weight -= dec;
e89996ae 1338 lw->inv_weight = 0;
45bf76df
IM
1339}
1340
2dd73a4f
PW
1341/*
1342 * To aid in avoiding the subversion of "niceness" due to uneven distribution
1343 * of tasks with abnormal "nice" values across CPUs the contribution that
1344 * each task makes to its run queue's load is weighted according to its
41a2d6cf 1345 * scheduling class and "nice" value. For SCHED_NORMAL tasks this is just a
2dd73a4f
PW
1346 * scaled version of the new time slice allocation that they receive on time
1347 * slice expiry etc.
1348 */
1349
cce7ade8
PZ
1350#define WEIGHT_IDLEPRIO 3
1351#define WMULT_IDLEPRIO 1431655765
dd41f596
IM
1352
1353/*
1354 * Nice levels are multiplicative, with a gentle 10% change for every
1355 * nice level changed. I.e. when a CPU-bound task goes from nice 0 to
1356 * nice 1, it will get ~10% less CPU time than another CPU-bound task
1357 * that remained on nice 0.
1358 *
1359 * The "10% effect" is relative and cumulative: from _any_ nice level,
1360 * if you go up 1 level, it's -10% CPU usage, if you go down 1 level
f9153ee6
IM
1361 * it's +10% CPU usage. (to achieve that we use a multiplier of 1.25.
1362 * If a task goes up by ~10% and another task goes down by ~10% then
1363 * the relative distance between them is ~25%.)
dd41f596
IM
1364 */
1365static const int prio_to_weight[40] = {
254753dc
IM
1366 /* -20 */ 88761, 71755, 56483, 46273, 36291,
1367 /* -15 */ 29154, 23254, 18705, 14949, 11916,
1368 /* -10 */ 9548, 7620, 6100, 4904, 3906,
1369 /* -5 */ 3121, 2501, 1991, 1586, 1277,
1370 /* 0 */ 1024, 820, 655, 526, 423,
1371 /* 5 */ 335, 272, 215, 172, 137,
1372 /* 10 */ 110, 87, 70, 56, 45,
1373 /* 15 */ 36, 29, 23, 18, 15,
dd41f596
IM
1374};
1375
5714d2de
IM
1376/*
1377 * Inverse (2^32/x) values of the prio_to_weight[] array, precalculated.
1378 *
1379 * In cases where the weight does not change often, we can use the
1380 * precalculated inverse to speed up arithmetics by turning divisions
1381 * into multiplications:
1382 */
dd41f596 1383static const u32 prio_to_wmult[40] = {
254753dc
IM
1384 /* -20 */ 48388, 59856, 76040, 92818, 118348,
1385 /* -15 */ 147320, 184698, 229616, 287308, 360437,
1386 /* -10 */ 449829, 563644, 704093, 875809, 1099582,
1387 /* -5 */ 1376151, 1717300, 2157191, 2708050, 3363326,
1388 /* 0 */ 4194304, 5237765, 6557202, 8165337, 10153587,
1389 /* 5 */ 12820798, 15790321, 19976592, 24970740, 31350126,
1390 /* 10 */ 39045157, 49367440, 61356676, 76695844, 95443717,
1391 /* 15 */ 119304647, 148102320, 186737708, 238609294, 286331153,
dd41f596 1392};
2dd73a4f 1393
dd41f596
IM
1394static void activate_task(struct rq *rq, struct task_struct *p, int wakeup);
1395
1396/*
1397 * runqueue iterator, to support SMP load-balancing between different
1398 * scheduling classes, without having to expose their internal data
1399 * structures to the load-balancing proper:
1400 */
1401struct rq_iterator {
1402 void *arg;
1403 struct task_struct *(*start)(void *);
1404 struct task_struct *(*next)(void *);
1405};
1406
e1d1484f
PW
1407#ifdef CONFIG_SMP
1408static unsigned long
1409balance_tasks(struct rq *this_rq, int this_cpu, struct rq *busiest,
1410 unsigned long max_load_move, struct sched_domain *sd,
1411 enum cpu_idle_type idle, int *all_pinned,
1412 int *this_best_prio, struct rq_iterator *iterator);
1413
1414static int
1415iter_move_one_task(struct rq *this_rq, int this_cpu, struct rq *busiest,
1416 struct sched_domain *sd, enum cpu_idle_type idle,
1417 struct rq_iterator *iterator);
e1d1484f 1418#endif
dd41f596 1419
ef12fefa
BR
1420/* Time spent by the tasks of the cpu accounting group executing in ... */
1421enum cpuacct_stat_index {
1422 CPUACCT_STAT_USER, /* ... user mode */
1423 CPUACCT_STAT_SYSTEM, /* ... kernel mode */
1424
1425 CPUACCT_STAT_NSTATS,
1426};
1427
d842de87
SV
1428#ifdef CONFIG_CGROUP_CPUACCT
1429static void cpuacct_charge(struct task_struct *tsk, u64 cputime);
ef12fefa
BR
1430static void cpuacct_update_stats(struct task_struct *tsk,
1431 enum cpuacct_stat_index idx, cputime_t val);
d842de87
SV
1432#else
1433static inline void cpuacct_charge(struct task_struct *tsk, u64 cputime) {}
ef12fefa
BR
1434static inline void cpuacct_update_stats(struct task_struct *tsk,
1435 enum cpuacct_stat_index idx, cputime_t val) {}
d842de87
SV
1436#endif
1437
18d95a28
PZ
1438static inline void inc_cpu_load(struct rq *rq, unsigned long load)
1439{
1440 update_load_add(&rq->load, load);
1441}
1442
1443static inline void dec_cpu_load(struct rq *rq, unsigned long load)
1444{
1445 update_load_sub(&rq->load, load);
1446}
1447
7940ca36 1448#if (defined(CONFIG_SMP) && defined(CONFIG_FAIR_GROUP_SCHED)) || defined(CONFIG_RT_GROUP_SCHED)
eb755805 1449typedef int (*tg_visitor)(struct task_group *, void *);
c09595f6
PZ
1450
1451/*
1452 * Iterate the full tree, calling @down when first entering a node and @up when
1453 * leaving it for the final time.
1454 */
eb755805 1455static int walk_tg_tree(tg_visitor down, tg_visitor up, void *data)
c09595f6
PZ
1456{
1457 struct task_group *parent, *child;
eb755805 1458 int ret;
c09595f6
PZ
1459
1460 rcu_read_lock();
1461 parent = &root_task_group;
1462down:
eb755805
PZ
1463 ret = (*down)(parent, data);
1464 if (ret)
1465 goto out_unlock;
c09595f6
PZ
1466 list_for_each_entry_rcu(child, &parent->children, siblings) {
1467 parent = child;
1468 goto down;
1469
1470up:
1471 continue;
1472 }
eb755805
PZ
1473 ret = (*up)(parent, data);
1474 if (ret)
1475 goto out_unlock;
c09595f6
PZ
1476
1477 child = parent;
1478 parent = parent->parent;
1479 if (parent)
1480 goto up;
eb755805 1481out_unlock:
c09595f6 1482 rcu_read_unlock();
eb755805
PZ
1483
1484 return ret;
c09595f6
PZ
1485}
1486
eb755805
PZ
1487static int tg_nop(struct task_group *tg, void *data)
1488{
1489 return 0;
c09595f6 1490}
eb755805
PZ
1491#endif
1492
1493#ifdef CONFIG_SMP
1494static unsigned long source_load(int cpu, int type);
1495static unsigned long target_load(int cpu, int type);
1496static int task_hot(struct task_struct *p, u64 now, struct sched_domain *sd);
1497
1498static unsigned long cpu_avg_load_per_task(int cpu)
1499{
1500 struct rq *rq = cpu_rq(cpu);
af6d596f 1501 unsigned long nr_running = ACCESS_ONCE(rq->nr_running);
eb755805 1502
4cd42620
SR
1503 if (nr_running)
1504 rq->avg_load_per_task = rq->load.weight / nr_running;
a2d47777
BS
1505 else
1506 rq->avg_load_per_task = 0;
eb755805
PZ
1507
1508 return rq->avg_load_per_task;
1509}
1510
1511#ifdef CONFIG_FAIR_GROUP_SCHED
c09595f6 1512
c09595f6
PZ
1513static void __set_se_shares(struct sched_entity *se, unsigned long shares);
1514
1515/*
1516 * Calculate and set the cpu's group shares.
1517 */
1518static void
ffda12a1
PZ
1519update_group_shares_cpu(struct task_group *tg, int cpu,
1520 unsigned long sd_shares, unsigned long sd_rq_weight)
18d95a28 1521{
c09595f6
PZ
1522 unsigned long shares;
1523 unsigned long rq_weight;
1524
c8cba857 1525 if (!tg->se[cpu])
c09595f6
PZ
1526 return;
1527
ec4e0e2f 1528 rq_weight = tg->cfs_rq[cpu]->rq_weight;
c8cba857 1529
c09595f6
PZ
1530 /*
1531 * \Sum shares * rq_weight
1532 * shares = -----------------------
1533 * \Sum rq_weight
1534 *
1535 */
ec4e0e2f 1536 shares = (sd_shares * rq_weight) / sd_rq_weight;
ffda12a1 1537 shares = clamp_t(unsigned long, shares, MIN_SHARES, MAX_SHARES);
c09595f6 1538
ffda12a1
PZ
1539 if (abs(shares - tg->se[cpu]->load.weight) >
1540 sysctl_sched_shares_thresh) {
1541 struct rq *rq = cpu_rq(cpu);
1542 unsigned long flags;
c09595f6 1543
ffda12a1 1544 spin_lock_irqsave(&rq->lock, flags);
ec4e0e2f 1545 tg->cfs_rq[cpu]->shares = shares;
c09595f6 1546
ffda12a1
PZ
1547 __set_se_shares(tg->se[cpu], shares);
1548 spin_unlock_irqrestore(&rq->lock, flags);
1549 }
18d95a28 1550}
c09595f6
PZ
1551
1552/*
c8cba857
PZ
1553 * Re-compute the task group their per cpu shares over the given domain.
1554 * This needs to be done in a bottom-up fashion because the rq weight of a
1555 * parent group depends on the shares of its child groups.
c09595f6 1556 */
eb755805 1557static int tg_shares_up(struct task_group *tg, void *data)
c09595f6 1558{
ec4e0e2f 1559 unsigned long weight, rq_weight = 0;
c8cba857 1560 unsigned long shares = 0;
eb755805 1561 struct sched_domain *sd = data;
c8cba857 1562 int i;
c09595f6 1563
758b2cdc 1564 for_each_cpu(i, sched_domain_span(sd)) {
ec4e0e2f
KC
1565 /*
1566 * If there are currently no tasks on the cpu pretend there
1567 * is one of average load so that when a new task gets to
1568 * run here it will not get delayed by group starvation.
1569 */
1570 weight = tg->cfs_rq[i]->load.weight;
1571 if (!weight)
1572 weight = NICE_0_LOAD;
1573
1574 tg->cfs_rq[i]->rq_weight = weight;
1575 rq_weight += weight;
c8cba857 1576 shares += tg->cfs_rq[i]->shares;
c09595f6 1577 }
c09595f6 1578
c8cba857
PZ
1579 if ((!shares && rq_weight) || shares > tg->shares)
1580 shares = tg->shares;
1581
1582 if (!sd->parent || !(sd->parent->flags & SD_LOAD_BALANCE))
1583 shares = tg->shares;
c09595f6 1584
758b2cdc 1585 for_each_cpu(i, sched_domain_span(sd))
ffda12a1 1586 update_group_shares_cpu(tg, i, shares, rq_weight);
eb755805
PZ
1587
1588 return 0;
c09595f6
PZ
1589}
1590
1591/*
c8cba857
PZ
1592 * Compute the cpu's hierarchical load factor for each task group.
1593 * This needs to be done in a top-down fashion because the load of a child
1594 * group is a fraction of its parents load.
c09595f6 1595 */
eb755805 1596static int tg_load_down(struct task_group *tg, void *data)
c09595f6 1597{
c8cba857 1598 unsigned long load;
eb755805 1599 long cpu = (long)data;
c09595f6 1600
c8cba857
PZ
1601 if (!tg->parent) {
1602 load = cpu_rq(cpu)->load.weight;
1603 } else {
1604 load = tg->parent->cfs_rq[cpu]->h_load;
1605 load *= tg->cfs_rq[cpu]->shares;
1606 load /= tg->parent->cfs_rq[cpu]->load.weight + 1;
1607 }
c09595f6 1608
c8cba857 1609 tg->cfs_rq[cpu]->h_load = load;
c09595f6 1610
eb755805 1611 return 0;
c09595f6
PZ
1612}
1613
c8cba857 1614static void update_shares(struct sched_domain *sd)
4d8d595d 1615{
2398f2c6
PZ
1616 u64 now = cpu_clock(raw_smp_processor_id());
1617 s64 elapsed = now - sd->last_update;
1618
1619 if (elapsed >= (s64)(u64)sysctl_sched_shares_ratelimit) {
1620 sd->last_update = now;
eb755805 1621 walk_tg_tree(tg_nop, tg_shares_up, sd);
2398f2c6 1622 }
4d8d595d
PZ
1623}
1624
3e5459b4
PZ
1625static void update_shares_locked(struct rq *rq, struct sched_domain *sd)
1626{
1627 spin_unlock(&rq->lock);
1628 update_shares(sd);
1629 spin_lock(&rq->lock);
1630}
1631
eb755805 1632static void update_h_load(long cpu)
c09595f6 1633{
eb755805 1634 walk_tg_tree(tg_load_down, tg_nop, (void *)cpu);
c09595f6
PZ
1635}
1636
c09595f6
PZ
1637#else
1638
c8cba857 1639static inline void update_shares(struct sched_domain *sd)
4d8d595d
PZ
1640{
1641}
1642
3e5459b4
PZ
1643static inline void update_shares_locked(struct rq *rq, struct sched_domain *sd)
1644{
1645}
1646
18d95a28
PZ
1647#endif
1648
8f45e2b5
GH
1649#ifdef CONFIG_PREEMPT
1650
70574a99 1651/*
8f45e2b5
GH
1652 * fair double_lock_balance: Safely acquires both rq->locks in a fair
1653 * way at the expense of forcing extra atomic operations in all
1654 * invocations. This assures that the double_lock is acquired using the
1655 * same underlying policy as the spinlock_t on this architecture, which
1656 * reduces latency compared to the unfair variant below. However, it
1657 * also adds more overhead and therefore may reduce throughput.
70574a99 1658 */
8f45e2b5
GH
1659static inline int _double_lock_balance(struct rq *this_rq, struct rq *busiest)
1660 __releases(this_rq->lock)
1661 __acquires(busiest->lock)
1662 __acquires(this_rq->lock)
1663{
1664 spin_unlock(&this_rq->lock);
1665 double_rq_lock(this_rq, busiest);
1666
1667 return 1;
1668}
1669
1670#else
1671/*
1672 * Unfair double_lock_balance: Optimizes throughput at the expense of
1673 * latency by eliminating extra atomic operations when the locks are
1674 * already in proper order on entry. This favors lower cpu-ids and will
1675 * grant the double lock to lower cpus over higher ids under contention,
1676 * regardless of entry order into the function.
1677 */
1678static int _double_lock_balance(struct rq *this_rq, struct rq *busiest)
70574a99
AD
1679 __releases(this_rq->lock)
1680 __acquires(busiest->lock)
1681 __acquires(this_rq->lock)
1682{
1683 int ret = 0;
1684
70574a99
AD
1685 if (unlikely(!spin_trylock(&busiest->lock))) {
1686 if (busiest < this_rq) {
1687 spin_unlock(&this_rq->lock);
1688 spin_lock(&busiest->lock);
1689 spin_lock_nested(&this_rq->lock, SINGLE_DEPTH_NESTING);
1690 ret = 1;
1691 } else
1692 spin_lock_nested(&busiest->lock, SINGLE_DEPTH_NESTING);
1693 }
1694 return ret;
1695}
1696
8f45e2b5
GH
1697#endif /* CONFIG_PREEMPT */
1698
1699/*
1700 * double_lock_balance - lock the busiest runqueue, this_rq is locked already.
1701 */
1702static int double_lock_balance(struct rq *this_rq, struct rq *busiest)
1703{
1704 if (unlikely(!irqs_disabled())) {
1705 /* printk() doesn't work good under rq->lock */
1706 spin_unlock(&this_rq->lock);
1707 BUG_ON(1);
1708 }
1709
1710 return _double_lock_balance(this_rq, busiest);
1711}
1712
70574a99
AD
1713static inline void double_unlock_balance(struct rq *this_rq, struct rq *busiest)
1714 __releases(busiest->lock)
1715{
1716 spin_unlock(&busiest->lock);
1717 lock_set_subclass(&this_rq->lock.dep_map, 0, _RET_IP_);
1718}
18d95a28
PZ
1719#endif
1720
30432094 1721#ifdef CONFIG_FAIR_GROUP_SCHED
34e83e85
IM
1722static void cfs_rq_set_shares(struct cfs_rq *cfs_rq, unsigned long shares)
1723{
30432094 1724#ifdef CONFIG_SMP
34e83e85
IM
1725 cfs_rq->shares = shares;
1726#endif
1727}
30432094 1728#endif
e7693a36 1729
dce48a84
TG
1730static void calc_load_account_active(struct rq *this_rq);
1731
dd41f596 1732#include "sched_stats.h"
dd41f596 1733#include "sched_idletask.c"
5522d5d5
IM
1734#include "sched_fair.c"
1735#include "sched_rt.c"
dd41f596
IM
1736#ifdef CONFIG_SCHED_DEBUG
1737# include "sched_debug.c"
1738#endif
1739
1740#define sched_class_highest (&rt_sched_class)
1f11eb6a
GH
1741#define for_each_class(class) \
1742 for (class = sched_class_highest; class; class = class->next)
dd41f596 1743
c09595f6 1744static void inc_nr_running(struct rq *rq)
9c217245
IM
1745{
1746 rq->nr_running++;
9c217245
IM
1747}
1748
c09595f6 1749static void dec_nr_running(struct rq *rq)
9c217245
IM
1750{
1751 rq->nr_running--;
9c217245
IM
1752}
1753
45bf76df
IM
1754static void set_load_weight(struct task_struct *p)
1755{
1756 if (task_has_rt_policy(p)) {
dd41f596
IM
1757 p->se.load.weight = prio_to_weight[0] * 2;
1758 p->se.load.inv_weight = prio_to_wmult[0] >> 1;
1759 return;
1760 }
45bf76df 1761
dd41f596
IM
1762 /*
1763 * SCHED_IDLE tasks get minimal weight:
1764 */
1765 if (p->policy == SCHED_IDLE) {
1766 p->se.load.weight = WEIGHT_IDLEPRIO;
1767 p->se.load.inv_weight = WMULT_IDLEPRIO;
1768 return;
1769 }
71f8bd46 1770
dd41f596
IM
1771 p->se.load.weight = prio_to_weight[p->static_prio - MAX_RT_PRIO];
1772 p->se.load.inv_weight = prio_to_wmult[p->static_prio - MAX_RT_PRIO];
71f8bd46
IM
1773}
1774
2087a1ad
GH
1775static void update_avg(u64 *avg, u64 sample)
1776{
1777 s64 diff = sample - *avg;
1778 *avg += diff >> 3;
1779}
1780
8159f87e 1781static void enqueue_task(struct rq *rq, struct task_struct *p, int wakeup)
71f8bd46 1782{
831451ac
PZ
1783 if (wakeup)
1784 p->se.start_runtime = p->se.sum_exec_runtime;
1785
dd41f596 1786 sched_info_queued(p);
fd390f6a 1787 p->sched_class->enqueue_task(rq, p, wakeup);
dd41f596 1788 p->se.on_rq = 1;
71f8bd46
IM
1789}
1790
69be72c1 1791static void dequeue_task(struct rq *rq, struct task_struct *p, int sleep)
71f8bd46 1792{
831451ac
PZ
1793 if (sleep) {
1794 if (p->se.last_wakeup) {
1795 update_avg(&p->se.avg_overlap,
1796 p->se.sum_exec_runtime - p->se.last_wakeup);
1797 p->se.last_wakeup = 0;
1798 } else {
1799 update_avg(&p->se.avg_wakeup,
1800 sysctl_sched_wakeup_granularity);
1801 }
2087a1ad
GH
1802 }
1803
46ac22ba 1804 sched_info_dequeued(p);
f02231e5 1805 p->sched_class->dequeue_task(rq, p, sleep);
dd41f596 1806 p->se.on_rq = 0;
71f8bd46
IM
1807}
1808
14531189 1809/*
dd41f596 1810 * __normal_prio - return the priority that is based on the static prio
14531189 1811 */
14531189
IM
1812static inline int __normal_prio(struct task_struct *p)
1813{
dd41f596 1814 return p->static_prio;
14531189
IM
1815}
1816
b29739f9
IM
1817/*
1818 * Calculate the expected normal priority: i.e. priority
1819 * without taking RT-inheritance into account. Might be
1820 * boosted by interactivity modifiers. Changes upon fork,
1821 * setprio syscalls, and whenever the interactivity
1822 * estimator recalculates.
1823 */
36c8b586 1824static inline int normal_prio(struct task_struct *p)
b29739f9
IM
1825{
1826 int prio;
1827
e05606d3 1828 if (task_has_rt_policy(p))
b29739f9
IM
1829 prio = MAX_RT_PRIO-1 - p->rt_priority;
1830 else
1831 prio = __normal_prio(p);
1832 return prio;
1833}
1834
1835/*
1836 * Calculate the current priority, i.e. the priority
1837 * taken into account by the scheduler. This value might
1838 * be boosted by RT tasks, or might be boosted by
1839 * interactivity modifiers. Will be RT if the task got
1840 * RT-boosted. If not then it returns p->normal_prio.
1841 */
36c8b586 1842static int effective_prio(struct task_struct *p)
b29739f9
IM
1843{
1844 p->normal_prio = normal_prio(p);
1845 /*
1846 * If we are RT tasks or we were boosted to RT priority,
1847 * keep the priority unchanged. Otherwise, update priority
1848 * to the normal priority:
1849 */
1850 if (!rt_prio(p->prio))
1851 return p->normal_prio;
1852 return p->prio;
1853}
1854
1da177e4 1855/*
dd41f596 1856 * activate_task - move a task to the runqueue.
1da177e4 1857 */
dd41f596 1858static void activate_task(struct rq *rq, struct task_struct *p, int wakeup)
1da177e4 1859{
d9514f6c 1860 if (task_contributes_to_load(p))
dd41f596 1861 rq->nr_uninterruptible--;
1da177e4 1862
8159f87e 1863 enqueue_task(rq, p, wakeup);
c09595f6 1864 inc_nr_running(rq);
1da177e4
LT
1865}
1866
1da177e4
LT
1867/*
1868 * deactivate_task - remove a task from the runqueue.
1869 */
2e1cb74a 1870static void deactivate_task(struct rq *rq, struct task_struct *p, int sleep)
1da177e4 1871{
d9514f6c 1872 if (task_contributes_to_load(p))
dd41f596
IM
1873 rq->nr_uninterruptible++;
1874
69be72c1 1875 dequeue_task(rq, p, sleep);
c09595f6 1876 dec_nr_running(rq);
1da177e4
LT
1877}
1878
1da177e4
LT
1879/**
1880 * task_curr - is this task currently executing on a CPU?
1881 * @p: the task in question.
1882 */
36c8b586 1883inline int task_curr(const struct task_struct *p)
1da177e4
LT
1884{
1885 return cpu_curr(task_cpu(p)) == p;
1886}
1887
dd41f596
IM
1888static inline void __set_task_cpu(struct task_struct *p, unsigned int cpu)
1889{
6f505b16 1890 set_task_rq(p, cpu);
dd41f596 1891#ifdef CONFIG_SMP
ce96b5ac
DA
1892 /*
1893 * After ->cpu is set up to a new value, task_rq_lock(p, ...) can be
1894 * successfuly executed on another CPU. We must ensure that updates of
1895 * per-task data have been completed by this moment.
1896 */
1897 smp_wmb();
dd41f596 1898 task_thread_info(p)->cpu = cpu;
dd41f596 1899#endif
2dd73a4f
PW
1900}
1901
cb469845
SR
1902static inline void check_class_changed(struct rq *rq, struct task_struct *p,
1903 const struct sched_class *prev_class,
1904 int oldprio, int running)
1905{
1906 if (prev_class != p->sched_class) {
1907 if (prev_class->switched_from)
1908 prev_class->switched_from(rq, p, running);
1909 p->sched_class->switched_to(rq, p, running);
1910 } else
1911 p->sched_class->prio_changed(rq, p, oldprio, running);
1912}
1913
1da177e4 1914#ifdef CONFIG_SMP
c65cc870 1915
e958b360
TG
1916/* Used instead of source_load when we know the type == 0 */
1917static unsigned long weighted_cpuload(const int cpu)
1918{
1919 return cpu_rq(cpu)->load.weight;
1920}
1921
cc367732
IM
1922/*
1923 * Is this task likely cache-hot:
1924 */
e7693a36 1925static int
cc367732
IM
1926task_hot(struct task_struct *p, u64 now, struct sched_domain *sd)
1927{
1928 s64 delta;
1929
f540a608
IM
1930 /*
1931 * Buddy candidates are cache hot:
1932 */
4793241b
PZ
1933 if (sched_feat(CACHE_HOT_BUDDY) &&
1934 (&p->se == cfs_rq_of(&p->se)->next ||
1935 &p->se == cfs_rq_of(&p->se)->last))
f540a608
IM
1936 return 1;
1937
cc367732
IM
1938 if (p->sched_class != &fair_sched_class)
1939 return 0;
1940
6bc1665b
IM
1941 if (sysctl_sched_migration_cost == -1)
1942 return 1;
1943 if (sysctl_sched_migration_cost == 0)
1944 return 0;
1945
cc367732
IM
1946 delta = now - p->se.exec_start;
1947
1948 return delta < (s64)sysctl_sched_migration_cost;
1949}
1950
1951
dd41f596 1952void set_task_cpu(struct task_struct *p, unsigned int new_cpu)
c65cc870 1953{
dd41f596
IM
1954 int old_cpu = task_cpu(p);
1955 struct rq *old_rq = cpu_rq(old_cpu), *new_rq = cpu_rq(new_cpu);
2830cf8c
SV
1956 struct cfs_rq *old_cfsrq = task_cfs_rq(p),
1957 *new_cfsrq = cpu_cfs_rq(old_cfsrq, new_cpu);
bbdba7c0 1958 u64 clock_offset;
dd41f596
IM
1959
1960 clock_offset = old_rq->clock - new_rq->clock;
6cfb0d5d 1961
de1d7286 1962 trace_sched_migrate_task(p, new_cpu);
cbc34ed1 1963
6cfb0d5d
IM
1964#ifdef CONFIG_SCHEDSTATS
1965 if (p->se.wait_start)
1966 p->se.wait_start -= clock_offset;
dd41f596
IM
1967 if (p->se.sleep_start)
1968 p->se.sleep_start -= clock_offset;
1969 if (p->se.block_start)
1970 p->se.block_start -= clock_offset;
cc367732
IM
1971 if (old_cpu != new_cpu) {
1972 schedstat_inc(p, se.nr_migrations);
1973 if (task_hot(p, old_rq->clock, NULL))
1974 schedstat_inc(p, se.nr_forced2_migrations);
1975 }
6cfb0d5d 1976#endif
2830cf8c
SV
1977 p->se.vruntime -= old_cfsrq->min_vruntime -
1978 new_cfsrq->min_vruntime;
dd41f596
IM
1979
1980 __set_task_cpu(p, new_cpu);
c65cc870
IM
1981}
1982
70b97a7f 1983struct migration_req {
1da177e4 1984 struct list_head list;
1da177e4 1985
36c8b586 1986 struct task_struct *task;
1da177e4
LT
1987 int dest_cpu;
1988
1da177e4 1989 struct completion done;
70b97a7f 1990};
1da177e4
LT
1991
1992/*
1993 * The task's runqueue lock must be held.
1994 * Returns true if you have to wait for migration thread.
1995 */
36c8b586 1996static int
70b97a7f 1997migrate_task(struct task_struct *p, int dest_cpu, struct migration_req *req)
1da177e4 1998{
70b97a7f 1999 struct rq *rq = task_rq(p);
1da177e4
LT
2000
2001 /*
2002 * If the task is not on a runqueue (and not running), then
2003 * it is sufficient to simply update the task's cpu field.
2004 */
dd41f596 2005 if (!p->se.on_rq && !task_running(rq, p)) {
1da177e4
LT
2006 set_task_cpu(p, dest_cpu);
2007 return 0;
2008 }
2009
2010 init_completion(&req->done);
1da177e4
LT
2011 req->task = p;
2012 req->dest_cpu = dest_cpu;
2013 list_add(&req->list, &rq->migration_queue);
48f24c4d 2014
1da177e4
LT
2015 return 1;
2016}
2017
a26b89f0
MM
2018/*
2019 * wait_task_context_switch - wait for a thread to complete at least one
2020 * context switch.
2021 *
2022 * @p must not be current.
2023 */
2024void wait_task_context_switch(struct task_struct *p)
2025{
2026 unsigned long nvcsw, nivcsw, flags;
2027 int running;
2028 struct rq *rq;
2029
2030 nvcsw = p->nvcsw;
2031 nivcsw = p->nivcsw;
2032 for (;;) {
2033 /*
2034 * The runqueue is assigned before the actual context
2035 * switch. We need to take the runqueue lock.
2036 *
2037 * We could check initially without the lock but it is
2038 * very likely that we need to take the lock in every
2039 * iteration.
2040 */
2041 rq = task_rq_lock(p, &flags);
2042 running = task_running(rq, p);
2043 task_rq_unlock(rq, &flags);
2044
2045 if (likely(!running))
2046 break;
2047 /*
2048 * The switch count is incremented before the actual
2049 * context switch. We thus wait for two switches to be
2050 * sure at least one completed.
2051 */
2052 if ((p->nvcsw - nvcsw) > 1)
2053 break;
2054 if ((p->nivcsw - nivcsw) > 1)
2055 break;
2056
2057 cpu_relax();
2058 }
2059}
2060
1da177e4
LT
2061/*
2062 * wait_task_inactive - wait for a thread to unschedule.
2063 *
85ba2d86
RM
2064 * If @match_state is nonzero, it's the @p->state value just checked and
2065 * not expected to change. If it changes, i.e. @p might have woken up,
2066 * then return zero. When we succeed in waiting for @p to be off its CPU,
2067 * we return a positive number (its total switch count). If a second call
2068 * a short while later returns the same number, the caller can be sure that
2069 * @p has remained unscheduled the whole time.
2070 *
1da177e4
LT
2071 * The caller must ensure that the task *will* unschedule sometime soon,
2072 * else this function might spin for a *long* time. This function can't
2073 * be called with interrupts off, or it may introduce deadlock with
2074 * smp_call_function() if an IPI is sent by the same process we are
2075 * waiting to become inactive.
2076 */
85ba2d86 2077unsigned long wait_task_inactive(struct task_struct *p, long match_state)
1da177e4
LT
2078{
2079 unsigned long flags;
dd41f596 2080 int running, on_rq;
85ba2d86 2081 unsigned long ncsw;
70b97a7f 2082 struct rq *rq;
1da177e4 2083
3a5c359a
AK
2084 for (;;) {
2085 /*
2086 * We do the initial early heuristics without holding
2087 * any task-queue locks at all. We'll only try to get
2088 * the runqueue lock when things look like they will
2089 * work out!
2090 */
2091 rq = task_rq(p);
fa490cfd 2092
3a5c359a
AK
2093 /*
2094 * If the task is actively running on another CPU
2095 * still, just relax and busy-wait without holding
2096 * any locks.
2097 *
2098 * NOTE! Since we don't hold any locks, it's not
2099 * even sure that "rq" stays as the right runqueue!
2100 * But we don't care, since "task_running()" will
2101 * return false if the runqueue has changed and p
2102 * is actually now running somewhere else!
2103 */
85ba2d86
RM
2104 while (task_running(rq, p)) {
2105 if (match_state && unlikely(p->state != match_state))
2106 return 0;
3a5c359a 2107 cpu_relax();
85ba2d86 2108 }
fa490cfd 2109
3a5c359a
AK
2110 /*
2111 * Ok, time to look more closely! We need the rq
2112 * lock now, to be *sure*. If we're wrong, we'll
2113 * just go back and repeat.
2114 */
2115 rq = task_rq_lock(p, &flags);
0a16b607 2116 trace_sched_wait_task(rq, p);
3a5c359a
AK
2117 running = task_running(rq, p);
2118 on_rq = p->se.on_rq;
85ba2d86 2119 ncsw = 0;
f31e11d8 2120 if (!match_state || p->state == match_state)
93dcf55f 2121 ncsw = p->nvcsw | LONG_MIN; /* sets MSB */
3a5c359a 2122 task_rq_unlock(rq, &flags);
fa490cfd 2123
85ba2d86
RM
2124 /*
2125 * If it changed from the expected state, bail out now.
2126 */
2127 if (unlikely(!ncsw))
2128 break;
2129
3a5c359a
AK
2130 /*
2131 * Was it really running after all now that we
2132 * checked with the proper locks actually held?
2133 *
2134 * Oops. Go back and try again..
2135 */
2136 if (unlikely(running)) {
2137 cpu_relax();
2138 continue;
2139 }
fa490cfd 2140
3a5c359a
AK
2141 /*
2142 * It's not enough that it's not actively running,
2143 * it must be off the runqueue _entirely_, and not
2144 * preempted!
2145 *
80dd99b3 2146 * So if it was still runnable (but just not actively
3a5c359a
AK
2147 * running right now), it's preempted, and we should
2148 * yield - it could be a while.
2149 */
2150 if (unlikely(on_rq)) {
2151 schedule_timeout_uninterruptible(1);
2152 continue;
2153 }
fa490cfd 2154
3a5c359a
AK
2155 /*
2156 * Ahh, all good. It wasn't running, and it wasn't
2157 * runnable, which means that it will never become
2158 * running in the future either. We're all done!
2159 */
2160 break;
2161 }
85ba2d86
RM
2162
2163 return ncsw;
1da177e4
LT
2164}
2165
2166/***
2167 * kick_process - kick a running thread to enter/exit the kernel
2168 * @p: the to-be-kicked thread
2169 *
2170 * Cause a process which is running on another CPU to enter
2171 * kernel-mode, without any delay. (to get signals handled.)
2172 *
2173 * NOTE: this function doesnt have to take the runqueue lock,
2174 * because all it wants to ensure is that the remote task enters
2175 * the kernel. If the IPI races and the task has been migrated
2176 * to another CPU then no harm is done and the purpose has been
2177 * achieved as well.
2178 */
36c8b586 2179void kick_process(struct task_struct *p)
1da177e4
LT
2180{
2181 int cpu;
2182
2183 preempt_disable();
2184 cpu = task_cpu(p);
2185 if ((cpu != smp_processor_id()) && task_curr(p))
2186 smp_send_reschedule(cpu);
2187 preempt_enable();
2188}
2189
2190/*
2dd73a4f
PW
2191 * Return a low guess at the load of a migration-source cpu weighted
2192 * according to the scheduling class and "nice" value.
1da177e4
LT
2193 *
2194 * We want to under-estimate the load of migration sources, to
2195 * balance conservatively.
2196 */
a9957449 2197static unsigned long source_load(int cpu, int type)
1da177e4 2198{
70b97a7f 2199 struct rq *rq = cpu_rq(cpu);
dd41f596 2200 unsigned long total = weighted_cpuload(cpu);
2dd73a4f 2201
93b75217 2202 if (type == 0 || !sched_feat(LB_BIAS))
dd41f596 2203 return total;
b910472d 2204
dd41f596 2205 return min(rq->cpu_load[type-1], total);
1da177e4
LT
2206}
2207
2208/*
2dd73a4f
PW
2209 * Return a high guess at the load of a migration-target cpu weighted
2210 * according to the scheduling class and "nice" value.
1da177e4 2211 */
a9957449 2212static unsigned long target_load(int cpu, int type)
1da177e4 2213{
70b97a7f 2214 struct rq *rq = cpu_rq(cpu);
dd41f596 2215 unsigned long total = weighted_cpuload(cpu);
2dd73a4f 2216
93b75217 2217 if (type == 0 || !sched_feat(LB_BIAS))
dd41f596 2218 return total;
3b0bd9bc 2219
dd41f596 2220 return max(rq->cpu_load[type-1], total);
2dd73a4f
PW
2221}
2222
147cbb4b
NP
2223/*
2224 * find_idlest_group finds and returns the least busy CPU group within the
2225 * domain.
2226 */
2227static struct sched_group *
2228find_idlest_group(struct sched_domain *sd, struct task_struct *p, int this_cpu)
2229{
2230 struct sched_group *idlest = NULL, *this = NULL, *group = sd->groups;
2231 unsigned long min_load = ULONG_MAX, this_load = 0;
2232 int load_idx = sd->forkexec_idx;
2233 int imbalance = 100 + (sd->imbalance_pct-100)/2;
2234
2235 do {
2236 unsigned long load, avg_load;
2237 int local_group;
2238 int i;
2239
da5a5522 2240 /* Skip over this group if it has no CPUs allowed */
758b2cdc
RR
2241 if (!cpumask_intersects(sched_group_cpus(group),
2242 &p->cpus_allowed))
3a5c359a 2243 continue;
da5a5522 2244
758b2cdc
RR
2245 local_group = cpumask_test_cpu(this_cpu,
2246 sched_group_cpus(group));
147cbb4b
NP
2247
2248 /* Tally up the load of all CPUs in the group */
2249 avg_load = 0;
2250
758b2cdc 2251 for_each_cpu(i, sched_group_cpus(group)) {
147cbb4b
NP
2252 /* Bias balancing toward cpus of our domain */
2253 if (local_group)
2254 load = source_load(i, load_idx);
2255 else
2256 load = target_load(i, load_idx);
2257
2258 avg_load += load;
2259 }
2260
2261 /* Adjust by relative CPU power of the group */
5517d86b
ED
2262 avg_load = sg_div_cpu_power(group,
2263 avg_load * SCHED_LOAD_SCALE);
147cbb4b
NP
2264
2265 if (local_group) {
2266 this_load = avg_load;
2267 this = group;
2268 } else if (avg_load < min_load) {
2269 min_load = avg_load;
2270 idlest = group;
2271 }
3a5c359a 2272 } while (group = group->next, group != sd->groups);
147cbb4b
NP
2273
2274 if (!idlest || 100*this_load < imbalance*min_load)
2275 return NULL;
2276 return idlest;
2277}
2278
2279/*
0feaece9 2280 * find_idlest_cpu - find the idlest cpu among the cpus in group.
147cbb4b 2281 */
95cdf3b7 2282static int
758b2cdc 2283find_idlest_cpu(struct sched_group *group, struct task_struct *p, int this_cpu)
147cbb4b
NP
2284{
2285 unsigned long load, min_load = ULONG_MAX;
2286 int idlest = -1;
2287 int i;
2288
da5a5522 2289 /* Traverse only the allowed CPUs */
758b2cdc 2290 for_each_cpu_and(i, sched_group_cpus(group), &p->cpus_allowed) {
2dd73a4f 2291 load = weighted_cpuload(i);
147cbb4b
NP
2292
2293 if (load < min_load || (load == min_load && i == this_cpu)) {
2294 min_load = load;
2295 idlest = i;
2296 }
2297 }
2298
2299 return idlest;
2300}
2301
476d139c
NP
2302/*
2303 * sched_balance_self: balance the current task (running on cpu) in domains
2304 * that have the 'flag' flag set. In practice, this is SD_BALANCE_FORK and
2305 * SD_BALANCE_EXEC.
2306 *
2307 * Balance, ie. select the least loaded group.
2308 *
2309 * Returns the target CPU number, or the same CPU if no balancing is needed.
2310 *
2311 * preempt must be disabled.
2312 */
2313static int sched_balance_self(int cpu, int flag)
2314{
2315 struct task_struct *t = current;
2316 struct sched_domain *tmp, *sd = NULL;
147cbb4b 2317
c96d145e 2318 for_each_domain(cpu, tmp) {
9761eea8
IM
2319 /*
2320 * If power savings logic is enabled for a domain, stop there.
2321 */
5c45bf27
SS
2322 if (tmp->flags & SD_POWERSAVINGS_BALANCE)
2323 break;
476d139c
NP
2324 if (tmp->flags & flag)
2325 sd = tmp;
c96d145e 2326 }
476d139c 2327
039a1c41
PZ
2328 if (sd)
2329 update_shares(sd);
2330
476d139c 2331 while (sd) {
476d139c 2332 struct sched_group *group;
1a848870
SS
2333 int new_cpu, weight;
2334
2335 if (!(sd->flags & flag)) {
2336 sd = sd->child;
2337 continue;
2338 }
476d139c 2339
476d139c 2340 group = find_idlest_group(sd, t, cpu);
1a848870
SS
2341 if (!group) {
2342 sd = sd->child;
2343 continue;
2344 }
476d139c 2345
758b2cdc 2346 new_cpu = find_idlest_cpu(group, t, cpu);
1a848870
SS
2347 if (new_cpu == -1 || new_cpu == cpu) {
2348 /* Now try balancing at a lower domain level of cpu */
2349 sd = sd->child;
2350 continue;
2351 }
476d139c 2352
1a848870 2353 /* Now try balancing at a lower domain level of new_cpu */
476d139c 2354 cpu = new_cpu;
758b2cdc 2355 weight = cpumask_weight(sched_domain_span(sd));
476d139c 2356 sd = NULL;
476d139c 2357 for_each_domain(cpu, tmp) {
758b2cdc 2358 if (weight <= cpumask_weight(sched_domain_span(tmp)))
476d139c
NP
2359 break;
2360 if (tmp->flags & flag)
2361 sd = tmp;
2362 }
2363 /* while loop will break here if sd == NULL */
2364 }
2365
2366 return cpu;
2367}
2368
2369#endif /* CONFIG_SMP */
1da177e4 2370
1da177e4
LT
2371/***
2372 * try_to_wake_up - wake up a thread
2373 * @p: the to-be-woken-up thread
2374 * @state: the mask of task states that can be woken
2375 * @sync: do a synchronous wakeup?
2376 *
2377 * Put it on the run-queue if it's not already there. The "current"
2378 * thread is always on the run-queue (except when the actual
2379 * re-schedule is in progress), and as such you're allowed to do
2380 * the simpler "current->state = TASK_RUNNING" to mark yourself
2381 * runnable without the overhead of this.
2382 *
2383 * returns failure only if the task is already active.
2384 */
36c8b586 2385static int try_to_wake_up(struct task_struct *p, unsigned int state, int sync)
1da177e4 2386{
cc367732 2387 int cpu, orig_cpu, this_cpu, success = 0;
1da177e4
LT
2388 unsigned long flags;
2389 long old_state;
70b97a7f 2390 struct rq *rq;
1da177e4 2391
b85d0667
IM
2392 if (!sched_feat(SYNC_WAKEUPS))
2393 sync = 0;
2394
2398f2c6 2395#ifdef CONFIG_SMP
57310a98 2396 if (sched_feat(LB_WAKEUP_UPDATE) && !root_task_group_empty()) {
2398f2c6
PZ
2397 struct sched_domain *sd;
2398
2399 this_cpu = raw_smp_processor_id();
2400 cpu = task_cpu(p);
2401
2402 for_each_domain(this_cpu, sd) {
758b2cdc 2403 if (cpumask_test_cpu(cpu, sched_domain_span(sd))) {
2398f2c6
PZ
2404 update_shares(sd);
2405 break;
2406 }
2407 }
2408 }
2409#endif
2410
04e2f174 2411 smp_wmb();
1da177e4 2412 rq = task_rq_lock(p, &flags);
03e89e45 2413 update_rq_clock(rq);
1da177e4
LT
2414 old_state = p->state;
2415 if (!(old_state & state))
2416 goto out;
2417
dd41f596 2418 if (p->se.on_rq)
1da177e4
LT
2419 goto out_running;
2420
2421 cpu = task_cpu(p);
cc367732 2422 orig_cpu = cpu;
1da177e4
LT
2423 this_cpu = smp_processor_id();
2424
2425#ifdef CONFIG_SMP
2426 if (unlikely(task_running(rq, p)))
2427 goto out_activate;
2428
5d2f5a61
DA
2429 cpu = p->sched_class->select_task_rq(p, sync);
2430 if (cpu != orig_cpu) {
2431 set_task_cpu(p, cpu);
1da177e4
LT
2432 task_rq_unlock(rq, &flags);
2433 /* might preempt at this point */
2434 rq = task_rq_lock(p, &flags);
2435 old_state = p->state;
2436 if (!(old_state & state))
2437 goto out;
dd41f596 2438 if (p->se.on_rq)
1da177e4
LT
2439 goto out_running;
2440
2441 this_cpu = smp_processor_id();
2442 cpu = task_cpu(p);
2443 }
2444
e7693a36
GH
2445#ifdef CONFIG_SCHEDSTATS
2446 schedstat_inc(rq, ttwu_count);
2447 if (cpu == this_cpu)
2448 schedstat_inc(rq, ttwu_local);
2449 else {
2450 struct sched_domain *sd;
2451 for_each_domain(this_cpu, sd) {
758b2cdc 2452 if (cpumask_test_cpu(cpu, sched_domain_span(sd))) {
e7693a36
GH
2453 schedstat_inc(sd, ttwu_wake_remote);
2454 break;
2455 }
2456 }
2457 }
6d6bc0ad 2458#endif /* CONFIG_SCHEDSTATS */
e7693a36 2459
1da177e4
LT
2460out_activate:
2461#endif /* CONFIG_SMP */
cc367732
IM
2462 schedstat_inc(p, se.nr_wakeups);
2463 if (sync)
2464 schedstat_inc(p, se.nr_wakeups_sync);
2465 if (orig_cpu != cpu)
2466 schedstat_inc(p, se.nr_wakeups_migrate);
2467 if (cpu == this_cpu)
2468 schedstat_inc(p, se.nr_wakeups_local);
2469 else
2470 schedstat_inc(p, se.nr_wakeups_remote);
dd41f596 2471 activate_task(rq, p, 1);
1da177e4
LT
2472 success = 1;
2473
831451ac
PZ
2474 /*
2475 * Only attribute actual wakeups done by this task.
2476 */
2477 if (!in_interrupt()) {
2478 struct sched_entity *se = &current->se;
2479 u64 sample = se->sum_exec_runtime;
2480
2481 if (se->last_wakeup)
2482 sample -= se->last_wakeup;
2483 else
2484 sample -= se->start_runtime;
2485 update_avg(&se->avg_wakeup, sample);
2486
2487 se->last_wakeup = se->sum_exec_runtime;
2488 }
2489
1da177e4 2490out_running:
468a15bb 2491 trace_sched_wakeup(rq, p, success);
15afe09b 2492 check_preempt_curr(rq, p, sync);
4ae7d5ce 2493
1da177e4 2494 p->state = TASK_RUNNING;
9a897c5a
SR
2495#ifdef CONFIG_SMP
2496 if (p->sched_class->task_wake_up)
2497 p->sched_class->task_wake_up(rq, p);
2498#endif
1da177e4
LT
2499out:
2500 task_rq_unlock(rq, &flags);
2501
2502 return success;
2503}
2504
50fa610a
DH
2505/**
2506 * wake_up_process - Wake up a specific process
2507 * @p: The process to be woken up.
2508 *
2509 * Attempt to wake up the nominated process and move it to the set of runnable
2510 * processes. Returns 1 if the process was woken up, 0 if it was already
2511 * running.
2512 *
2513 * It may be assumed that this function implies a write memory barrier before
2514 * changing the task state if and only if any tasks are woken up.
2515 */
7ad5b3a5 2516int wake_up_process(struct task_struct *p)
1da177e4 2517{
d9514f6c 2518 return try_to_wake_up(p, TASK_ALL, 0);
1da177e4 2519}
1da177e4
LT
2520EXPORT_SYMBOL(wake_up_process);
2521
7ad5b3a5 2522int wake_up_state(struct task_struct *p, unsigned int state)
1da177e4
LT
2523{
2524 return try_to_wake_up(p, state, 0);
2525}
2526
1da177e4
LT
2527/*
2528 * Perform scheduler related setup for a newly forked process p.
2529 * p is forked by current.
dd41f596
IM
2530 *
2531 * __sched_fork() is basic setup used by init_idle() too:
2532 */
2533static void __sched_fork(struct task_struct *p)
2534{
dd41f596
IM
2535 p->se.exec_start = 0;
2536 p->se.sum_exec_runtime = 0;
f6cf891c 2537 p->se.prev_sum_exec_runtime = 0;
4ae7d5ce
IM
2538 p->se.last_wakeup = 0;
2539 p->se.avg_overlap = 0;
831451ac
PZ
2540 p->se.start_runtime = 0;
2541 p->se.avg_wakeup = sysctl_sched_wakeup_granularity;
6cfb0d5d
IM
2542
2543#ifdef CONFIG_SCHEDSTATS
2544 p->se.wait_start = 0;
dd41f596
IM
2545 p->se.sum_sleep_runtime = 0;
2546 p->se.sleep_start = 0;
dd41f596
IM
2547 p->se.block_start = 0;
2548 p->se.sleep_max = 0;
2549 p->se.block_max = 0;
2550 p->se.exec_max = 0;
eba1ed4b 2551 p->se.slice_max = 0;
dd41f596 2552 p->se.wait_max = 0;
6cfb0d5d 2553#endif
476d139c 2554
fa717060 2555 INIT_LIST_HEAD(&p->rt.run_list);
dd41f596 2556 p->se.on_rq = 0;
4a55bd5e 2557 INIT_LIST_HEAD(&p->se.group_node);
476d139c 2558
e107be36
AK
2559#ifdef CONFIG_PREEMPT_NOTIFIERS
2560 INIT_HLIST_HEAD(&p->preempt_notifiers);
2561#endif
2562
1da177e4
LT
2563 /*
2564 * We mark the process as running here, but have not actually
2565 * inserted it onto the runqueue yet. This guarantees that
2566 * nobody will actually run it, and a signal or other external
2567 * event cannot wake it up and insert it on the runqueue either.
2568 */
2569 p->state = TASK_RUNNING;
dd41f596
IM
2570}
2571
2572/*
2573 * fork()/clone()-time setup:
2574 */
2575void sched_fork(struct task_struct *p, int clone_flags)
2576{
2577 int cpu = get_cpu();
2578
2579 __sched_fork(p);
2580
2581#ifdef CONFIG_SMP
2582 cpu = sched_balance_self(cpu, SD_BALANCE_FORK);
2583#endif
02e4bac2 2584 set_task_cpu(p, cpu);
b29739f9
IM
2585
2586 /*
2587 * Make sure we do not leak PI boosting priority to the child:
2588 */
2589 p->prio = current->normal_prio;
2ddbf952
HS
2590 if (!rt_prio(p->prio))
2591 p->sched_class = &fair_sched_class;
b29739f9 2592
52f17b6c 2593#if defined(CONFIG_SCHEDSTATS) || defined(CONFIG_TASK_DELAY_ACCT)
dd41f596 2594 if (likely(sched_info_on()))
52f17b6c 2595 memset(&p->sched_info, 0, sizeof(p->sched_info));
1da177e4 2596#endif
d6077cb8 2597#if defined(CONFIG_SMP) && defined(__ARCH_WANT_UNLOCKED_CTXSW)
4866cde0
NP
2598 p->oncpu = 0;
2599#endif
1da177e4 2600#ifdef CONFIG_PREEMPT
4866cde0 2601 /* Want to start with kernel preemption disabled. */
a1261f54 2602 task_thread_info(p)->preempt_count = 1;
1da177e4 2603#endif
917b627d
GH
2604 plist_node_init(&p->pushable_tasks, MAX_PRIO);
2605
476d139c 2606 put_cpu();
1da177e4
LT
2607}
2608
2609/*
2610 * wake_up_new_task - wake up a newly created task for the first time.
2611 *
2612 * This function will do some initial scheduler statistics housekeeping
2613 * that must be done for every newly created context, then puts the task
2614 * on the runqueue and wakes it.
2615 */
7ad5b3a5 2616void wake_up_new_task(struct task_struct *p, unsigned long clone_flags)
1da177e4
LT
2617{
2618 unsigned long flags;
dd41f596 2619 struct rq *rq;
1da177e4
LT
2620
2621 rq = task_rq_lock(p, &flags);
147cbb4b 2622 BUG_ON(p->state != TASK_RUNNING);
a8e504d2 2623 update_rq_clock(rq);
1da177e4
LT
2624
2625 p->prio = effective_prio(p);
2626
b9dca1e0 2627 if (!p->sched_class->task_new || !current->se.on_rq) {
dd41f596 2628 activate_task(rq, p, 0);
1da177e4 2629 } else {
1da177e4 2630 /*
dd41f596
IM
2631 * Let the scheduling class do new task startup
2632 * management (if any):
1da177e4 2633 */
ee0827d8 2634 p->sched_class->task_new(rq, p);
c09595f6 2635 inc_nr_running(rq);
1da177e4 2636 }
c71dd42d 2637 trace_sched_wakeup_new(rq, p, 1);
15afe09b 2638 check_preempt_curr(rq, p, 0);
9a897c5a
SR
2639#ifdef CONFIG_SMP
2640 if (p->sched_class->task_wake_up)
2641 p->sched_class->task_wake_up(rq, p);
2642#endif
dd41f596 2643 task_rq_unlock(rq, &flags);
1da177e4
LT
2644}
2645
e107be36
AK
2646#ifdef CONFIG_PREEMPT_NOTIFIERS
2647
2648/**
80dd99b3 2649 * preempt_notifier_register - tell me when current is being preempted & rescheduled
421cee29 2650 * @notifier: notifier struct to register
e107be36
AK
2651 */
2652void preempt_notifier_register(struct preempt_notifier *notifier)
2653{
2654 hlist_add_head(&notifier->link, &current->preempt_notifiers);
2655}
2656EXPORT_SYMBOL_GPL(preempt_notifier_register);
2657
2658/**
2659 * preempt_notifier_unregister - no longer interested in preemption notifications
421cee29 2660 * @notifier: notifier struct to unregister
e107be36
AK
2661 *
2662 * This is safe to call from within a preemption notifier.
2663 */
2664void preempt_notifier_unregister(struct preempt_notifier *notifier)
2665{
2666 hlist_del(&notifier->link);
2667}
2668EXPORT_SYMBOL_GPL(preempt_notifier_unregister);
2669
2670static void fire_sched_in_preempt_notifiers(struct task_struct *curr)
2671{
2672 struct preempt_notifier *notifier;
2673 struct hlist_node *node;
2674
2675 hlist_for_each_entry(notifier, node, &curr->preempt_notifiers, link)
2676 notifier->ops->sched_in(notifier, raw_smp_processor_id());
2677}
2678
2679static void
2680fire_sched_out_preempt_notifiers(struct task_struct *curr,
2681 struct task_struct *next)
2682{
2683 struct preempt_notifier *notifier;
2684 struct hlist_node *node;
2685
2686 hlist_for_each_entry(notifier, node, &curr->preempt_notifiers, link)
2687 notifier->ops->sched_out(notifier, next);
2688}
2689
6d6bc0ad 2690#else /* !CONFIG_PREEMPT_NOTIFIERS */
e107be36
AK
2691
2692static void fire_sched_in_preempt_notifiers(struct task_struct *curr)
2693{
2694}
2695
2696static void
2697fire_sched_out_preempt_notifiers(struct task_struct *curr,
2698 struct task_struct *next)
2699{
2700}
2701
6d6bc0ad 2702#endif /* CONFIG_PREEMPT_NOTIFIERS */
e107be36 2703
4866cde0
NP
2704/**
2705 * prepare_task_switch - prepare to switch tasks
2706 * @rq: the runqueue preparing to switch
421cee29 2707 * @prev: the current task that is being switched out
4866cde0
NP
2708 * @next: the task we are going to switch to.
2709 *
2710 * This is called with the rq lock held and interrupts off. It must
2711 * be paired with a subsequent finish_task_switch after the context
2712 * switch.
2713 *
2714 * prepare_task_switch sets up locking and calls architecture specific
2715 * hooks.
2716 */
e107be36
AK
2717static inline void
2718prepare_task_switch(struct rq *rq, struct task_struct *prev,
2719 struct task_struct *next)
4866cde0 2720{
e107be36 2721 fire_sched_out_preempt_notifiers(prev, next);
4866cde0
NP
2722 prepare_lock_switch(rq, next);
2723 prepare_arch_switch(next);
2724}
2725
1da177e4
LT
2726/**
2727 * finish_task_switch - clean up after a task-switch
344babaa 2728 * @rq: runqueue associated with task-switch
1da177e4
LT
2729 * @prev: the thread we just switched away from.
2730 *
4866cde0
NP
2731 * finish_task_switch must be called after the context switch, paired
2732 * with a prepare_task_switch call before the context switch.
2733 * finish_task_switch will reconcile locking set up by prepare_task_switch,
2734 * and do any other architecture-specific cleanup actions.
1da177e4
LT
2735 *
2736 * Note that we may have delayed dropping an mm in context_switch(). If
41a2d6cf 2737 * so, we finish that here outside of the runqueue lock. (Doing it
1da177e4
LT
2738 * with the lock held can cause deadlocks; see schedule() for
2739 * details.)
2740 */
a9957449 2741static void finish_task_switch(struct rq *rq, struct task_struct *prev)
1da177e4
LT
2742 __releases(rq->lock)
2743{
1da177e4 2744 struct mm_struct *mm = rq->prev_mm;
55a101f8 2745 long prev_state;
967fc046
GH
2746#ifdef CONFIG_SMP
2747 int post_schedule = 0;
2748
2749 if (current->sched_class->needs_post_schedule)
2750 post_schedule = current->sched_class->needs_post_schedule(rq);
2751#endif
1da177e4
LT
2752
2753 rq->prev_mm = NULL;
2754
2755 /*
2756 * A task struct has one reference for the use as "current".
c394cc9f 2757 * If a task dies, then it sets TASK_DEAD in tsk->state and calls
55a101f8
ON
2758 * schedule one last time. The schedule call will never return, and
2759 * the scheduled task must drop that reference.
c394cc9f 2760 * The test for TASK_DEAD must occur while the runqueue locks are
1da177e4
LT
2761 * still held, otherwise prev could be scheduled on another cpu, die
2762 * there before we look at prev->state, and then the reference would
2763 * be dropped twice.
2764 * Manfred Spraul <manfred@colorfullife.com>
2765 */
55a101f8 2766 prev_state = prev->state;
4866cde0
NP
2767 finish_arch_switch(prev);
2768 finish_lock_switch(rq, prev);
9a897c5a 2769#ifdef CONFIG_SMP
967fc046 2770 if (post_schedule)
9a897c5a
SR
2771 current->sched_class->post_schedule(rq);
2772#endif
e8fa1362 2773
e107be36 2774 fire_sched_in_preempt_notifiers(current);
1da177e4
LT
2775 if (mm)
2776 mmdrop(mm);
c394cc9f 2777 if (unlikely(prev_state == TASK_DEAD)) {
c6fd91f0 2778 /*
2779 * Remove function-return probe instances associated with this
2780 * task and put them back on the free list.
9761eea8 2781 */
c6fd91f0 2782 kprobe_flush_task(prev);
1da177e4 2783 put_task_struct(prev);
c6fd91f0 2784 }
1da177e4
LT
2785}
2786
2787/**
2788 * schedule_tail - first thing a freshly forked thread must call.
2789 * @prev: the thread we just switched away from.
2790 */
36c8b586 2791asmlinkage void schedule_tail(struct task_struct *prev)
1da177e4
LT
2792 __releases(rq->lock)
2793{
70b97a7f
IM
2794 struct rq *rq = this_rq();
2795
4866cde0
NP
2796 finish_task_switch(rq, prev);
2797#ifdef __ARCH_WANT_UNLOCKED_CTXSW
2798 /* In this case, finish_task_switch does not reenable preemption */
2799 preempt_enable();
2800#endif
1da177e4 2801 if (current->set_child_tid)
b488893a 2802 put_user(task_pid_vnr(current), current->set_child_tid);
1da177e4
LT
2803}
2804
2805/*
2806 * context_switch - switch to the new MM and the new
2807 * thread's register state.
2808 */
dd41f596 2809static inline void
70b97a7f 2810context_switch(struct rq *rq, struct task_struct *prev,
36c8b586 2811 struct task_struct *next)
1da177e4 2812{
dd41f596 2813 struct mm_struct *mm, *oldmm;
1da177e4 2814
e107be36 2815 prepare_task_switch(rq, prev, next);
0a16b607 2816 trace_sched_switch(rq, prev, next);
dd41f596
IM
2817 mm = next->mm;
2818 oldmm = prev->active_mm;
9226d125
ZA
2819 /*
2820 * For paravirt, this is coupled with an exit in switch_to to
2821 * combine the page table reload and the switch backend into
2822 * one hypercall.
2823 */
224101ed 2824 arch_start_context_switch(prev);
9226d125 2825
dd41f596 2826 if (unlikely(!mm)) {
1da177e4
LT
2827 next->active_mm = oldmm;
2828 atomic_inc(&oldmm->mm_count);
2829 enter_lazy_tlb(oldmm, next);
2830 } else
2831 switch_mm(oldmm, mm, next);
2832
dd41f596 2833 if (unlikely(!prev->mm)) {
1da177e4 2834 prev->active_mm = NULL;
1da177e4
LT
2835 rq->prev_mm = oldmm;
2836 }
3a5f5e48
IM
2837 /*
2838 * Since the runqueue lock will be released by the next
2839 * task (which is an invalid locking op but in the case
2840 * of the scheduler it's an obvious special-case), so we
2841 * do an early lockdep release here:
2842 */
2843#ifndef __ARCH_WANT_UNLOCKED_CTXSW
8a25d5de 2844 spin_release(&rq->lock.dep_map, 1, _THIS_IP_);
3a5f5e48 2845#endif
1da177e4
LT
2846
2847 /* Here we just switch the register state and the stack. */
2848 switch_to(prev, next, prev);
2849
dd41f596
IM
2850 barrier();
2851 /*
2852 * this_rq must be evaluated again because prev may have moved
2853 * CPUs since it called schedule(), thus the 'rq' on its stack
2854 * frame will be invalid.
2855 */
2856 finish_task_switch(this_rq(), prev);
1da177e4
LT
2857}
2858
2859/*
2860 * nr_running, nr_uninterruptible and nr_context_switches:
2861 *
2862 * externally visible scheduler statistics: current number of runnable
2863 * threads, current number of uninterruptible-sleeping threads, total
2864 * number of context switches performed since bootup.
2865 */
2866unsigned long nr_running(void)
2867{
2868 unsigned long i, sum = 0;
2869
2870 for_each_online_cpu(i)
2871 sum += cpu_rq(i)->nr_running;
2872
2873 return sum;
2874}
2875
2876unsigned long nr_uninterruptible(void)
2877{
2878 unsigned long i, sum = 0;
2879
0a945022 2880 for_each_possible_cpu(i)
1da177e4
LT
2881 sum += cpu_rq(i)->nr_uninterruptible;
2882
2883 /*
2884 * Since we read the counters lockless, it might be slightly
2885 * inaccurate. Do not allow it to go below zero though:
2886 */
2887 if (unlikely((long)sum < 0))
2888 sum = 0;
2889
2890 return sum;
2891}
2892
2893unsigned long long nr_context_switches(void)
2894{
cc94abfc
SR
2895 int i;
2896 unsigned long long sum = 0;
1da177e4 2897
0a945022 2898 for_each_possible_cpu(i)
1da177e4
LT
2899 sum += cpu_rq(i)->nr_switches;
2900
2901 return sum;
2902}
2903
2904unsigned long nr_iowait(void)
2905{
2906 unsigned long i, sum = 0;
2907
0a945022 2908 for_each_possible_cpu(i)
1da177e4
LT
2909 sum += atomic_read(&cpu_rq(i)->nr_iowait);
2910
2911 return sum;
2912}
2913
dce48a84
TG
2914/* Variables and functions for calc_load */
2915static atomic_long_t calc_load_tasks;
2916static unsigned long calc_load_update;
2917unsigned long avenrun[3];
2918EXPORT_SYMBOL(avenrun);
2919
2d02494f
TG
2920/**
2921 * get_avenrun - get the load average array
2922 * @loads: pointer to dest load array
2923 * @offset: offset to add
2924 * @shift: shift count to shift the result left
2925 *
2926 * These values are estimates at best, so no need for locking.
2927 */
2928void get_avenrun(unsigned long *loads, unsigned long offset, int shift)
2929{
2930 loads[0] = (avenrun[0] + offset) << shift;
2931 loads[1] = (avenrun[1] + offset) << shift;
2932 loads[2] = (avenrun[2] + offset) << shift;
2933}
2934
dce48a84
TG
2935static unsigned long
2936calc_load(unsigned long load, unsigned long exp, unsigned long active)
db1b1fef 2937{
dce48a84
TG
2938 load *= exp;
2939 load += active * (FIXED_1 - exp);
2940 return load >> FSHIFT;
2941}
db1b1fef 2942
dce48a84
TG
2943/*
2944 * calc_load - update the avenrun load estimates 10 ticks after the
2945 * CPUs have updated calc_load_tasks.
2946 */
2947void calc_global_load(void)
2948{
2949 unsigned long upd = calc_load_update + 10;
2950 long active;
2951
2952 if (time_before(jiffies, upd))
2953 return;
db1b1fef 2954
dce48a84
TG
2955 active = atomic_long_read(&calc_load_tasks);
2956 active = active > 0 ? active * FIXED_1 : 0;
db1b1fef 2957
dce48a84
TG
2958 avenrun[0] = calc_load(avenrun[0], EXP_1, active);
2959 avenrun[1] = calc_load(avenrun[1], EXP_5, active);
2960 avenrun[2] = calc_load(avenrun[2], EXP_15, active);
2961
2962 calc_load_update += LOAD_FREQ;
2963}
2964
2965/*
2966 * Either called from update_cpu_load() or from a cpu going idle
2967 */
2968static void calc_load_account_active(struct rq *this_rq)
2969{
2970 long nr_active, delta;
2971
2972 nr_active = this_rq->nr_running;
2973 nr_active += (long) this_rq->nr_uninterruptible;
2974
2975 if (nr_active != this_rq->calc_load_active) {
2976 delta = nr_active - this_rq->calc_load_active;
2977 this_rq->calc_load_active = nr_active;
2978 atomic_long_add(delta, &calc_load_tasks);
2979 }
db1b1fef
JS
2980}
2981
48f24c4d 2982/*
dd41f596
IM
2983 * Update rq->cpu_load[] statistics. This function is usually called every
2984 * scheduler tick (TICK_NSEC).
48f24c4d 2985 */
dd41f596 2986static void update_cpu_load(struct rq *this_rq)
48f24c4d 2987{
495eca49 2988 unsigned long this_load = this_rq->load.weight;
dd41f596
IM
2989 int i, scale;
2990
2991 this_rq->nr_load_updates++;
dd41f596
IM
2992
2993 /* Update our load: */
2994 for (i = 0, scale = 1; i < CPU_LOAD_IDX_MAX; i++, scale += scale) {
2995 unsigned long old_load, new_load;
2996
2997 /* scale is effectively 1 << i now, and >> i divides by scale */
2998
2999 old_load = this_rq->cpu_load[i];
3000 new_load = this_load;
a25707f3
IM
3001 /*
3002 * Round up the averaging division if load is increasing. This
3003 * prevents us from getting stuck on 9 if the load is 10, for
3004 * example.
3005 */
3006 if (new_load > old_load)
3007 new_load += scale-1;
dd41f596
IM
3008 this_rq->cpu_load[i] = (old_load*(scale-1) + new_load) >> i;
3009 }
dce48a84
TG
3010
3011 if (time_after_eq(jiffies, this_rq->calc_load_update)) {
3012 this_rq->calc_load_update += LOAD_FREQ;
3013 calc_load_account_active(this_rq);
3014 }
48f24c4d
IM
3015}
3016
dd41f596
IM
3017#ifdef CONFIG_SMP
3018
1da177e4
LT
3019/*
3020 * double_rq_lock - safely lock two runqueues
3021 *
3022 * Note this does not disable interrupts like task_rq_lock,
3023 * you need to do so manually before calling.
3024 */
70b97a7f 3025static void double_rq_lock(struct rq *rq1, struct rq *rq2)
1da177e4
LT
3026 __acquires(rq1->lock)
3027 __acquires(rq2->lock)
3028{
054b9108 3029 BUG_ON(!irqs_disabled());
1da177e4
LT
3030 if (rq1 == rq2) {
3031 spin_lock(&rq1->lock);
3032 __acquire(rq2->lock); /* Fake it out ;) */
3033 } else {
c96d145e 3034 if (rq1 < rq2) {
1da177e4 3035 spin_lock(&rq1->lock);
5e710e37 3036 spin_lock_nested(&rq2->lock, SINGLE_DEPTH_NESTING);
1da177e4
LT
3037 } else {
3038 spin_lock(&rq2->lock);
5e710e37 3039 spin_lock_nested(&rq1->lock, SINGLE_DEPTH_NESTING);
1da177e4
LT
3040 }
3041 }
6e82a3be
IM
3042 update_rq_clock(rq1);
3043 update_rq_clock(rq2);
1da177e4
LT
3044}
3045
3046/*
3047 * double_rq_unlock - safely unlock two runqueues
3048 *
3049 * Note this does not restore interrupts like task_rq_unlock,
3050 * you need to do so manually after calling.
3051 */
70b97a7f 3052static void double_rq_unlock(struct rq *rq1, struct rq *rq2)
1da177e4
LT
3053 __releases(rq1->lock)
3054 __releases(rq2->lock)
3055{
3056 spin_unlock(&rq1->lock);
3057 if (rq1 != rq2)
3058 spin_unlock(&rq2->lock);
3059 else
3060 __release(rq2->lock);
3061}
3062
1da177e4
LT
3063/*
3064 * If dest_cpu is allowed for this process, migrate the task to it.
3065 * This is accomplished by forcing the cpu_allowed mask to only
41a2d6cf 3066 * allow dest_cpu, which will force the cpu onto dest_cpu. Then
1da177e4
LT
3067 * the cpu_allowed mask is restored.
3068 */
36c8b586 3069static void sched_migrate_task(struct task_struct *p, int dest_cpu)
1da177e4 3070{
70b97a7f 3071 struct migration_req req;
1da177e4 3072 unsigned long flags;
70b97a7f 3073 struct rq *rq;
1da177e4
LT
3074
3075 rq = task_rq_lock(p, &flags);
96f874e2 3076 if (!cpumask_test_cpu(dest_cpu, &p->cpus_allowed)
e761b772 3077 || unlikely(!cpu_active(dest_cpu)))
1da177e4
LT
3078 goto out;
3079
3080 /* force the process onto the specified CPU */
3081 if (migrate_task(p, dest_cpu, &req)) {
3082 /* Need to wait for migration thread (might exit: take ref). */
3083 struct task_struct *mt = rq->migration_thread;
36c8b586 3084
1da177e4
LT
3085 get_task_struct(mt);
3086 task_rq_unlock(rq, &flags);
3087 wake_up_process(mt);
3088 put_task_struct(mt);
3089 wait_for_completion(&req.done);
36c8b586 3090
1da177e4
LT
3091 return;
3092 }
3093out:
3094 task_rq_unlock(rq, &flags);
3095}
3096
3097/*
476d139c
NP
3098 * sched_exec - execve() is a valuable balancing opportunity, because at
3099 * this point the task has the smallest effective memory and cache footprint.
1da177e4
LT
3100 */
3101void sched_exec(void)
3102{
1da177e4 3103 int new_cpu, this_cpu = get_cpu();
476d139c 3104 new_cpu = sched_balance_self(this_cpu, SD_BALANCE_EXEC);
1da177e4 3105 put_cpu();
476d139c
NP
3106 if (new_cpu != this_cpu)
3107 sched_migrate_task(current, new_cpu);
1da177e4
LT
3108}
3109
3110/*
3111 * pull_task - move a task from a remote runqueue to the local runqueue.
3112 * Both runqueues must be locked.
3113 */
dd41f596
IM
3114static void pull_task(struct rq *src_rq, struct task_struct *p,
3115 struct rq *this_rq, int this_cpu)
1da177e4 3116{
2e1cb74a 3117 deactivate_task(src_rq, p, 0);
1da177e4 3118 set_task_cpu(p, this_cpu);
dd41f596 3119 activate_task(this_rq, p, 0);
1da177e4
LT
3120 /*
3121 * Note that idle threads have a prio of MAX_PRIO, for this test
3122 * to be always true for them.
3123 */
15afe09b 3124 check_preempt_curr(this_rq, p, 0);
1da177e4
LT
3125}
3126
3127/*
3128 * can_migrate_task - may task p from runqueue rq be migrated to this_cpu?
3129 */
858119e1 3130static
70b97a7f 3131int can_migrate_task(struct task_struct *p, struct rq *rq, int this_cpu,
d15bcfdb 3132 struct sched_domain *sd, enum cpu_idle_type idle,
95cdf3b7 3133 int *all_pinned)
1da177e4 3134{
708dc512 3135 int tsk_cache_hot = 0;
1da177e4
LT
3136 /*
3137 * We do not migrate tasks that are:
3138 * 1) running (obviously), or
3139 * 2) cannot be migrated to this CPU due to cpus_allowed, or
3140 * 3) are cache-hot on their current CPU.
3141 */
96f874e2 3142 if (!cpumask_test_cpu(this_cpu, &p->cpus_allowed)) {
cc367732 3143 schedstat_inc(p, se.nr_failed_migrations_affine);
1da177e4 3144 return 0;
cc367732 3145 }
81026794
NP
3146 *all_pinned = 0;
3147
cc367732
IM
3148 if (task_running(rq, p)) {
3149 schedstat_inc(p, se.nr_failed_migrations_running);
81026794 3150 return 0;
cc367732 3151 }
1da177e4 3152
da84d961
IM
3153 /*
3154 * Aggressive migration if:
3155 * 1) task is cache cold, or
3156 * 2) too many balance attempts have failed.
3157 */
3158
708dc512
LH
3159 tsk_cache_hot = task_hot(p, rq->clock, sd);
3160 if (!tsk_cache_hot ||
3161 sd->nr_balance_failed > sd->cache_nice_tries) {
da84d961 3162#ifdef CONFIG_SCHEDSTATS
708dc512 3163 if (tsk_cache_hot) {
da84d961 3164 schedstat_inc(sd, lb_hot_gained[idle]);
cc367732
IM
3165 schedstat_inc(p, se.nr_forced_migrations);
3166 }
da84d961
IM
3167#endif
3168 return 1;
3169 }
3170
708dc512 3171 if (tsk_cache_hot) {
cc367732 3172 schedstat_inc(p, se.nr_failed_migrations_hot);
da84d961 3173 return 0;
cc367732 3174 }
1da177e4
LT
3175 return 1;
3176}
3177
e1d1484f
PW
3178static unsigned long
3179balance_tasks(struct rq *this_rq, int this_cpu, struct rq *busiest,
3180 unsigned long max_load_move, struct sched_domain *sd,
3181 enum cpu_idle_type idle, int *all_pinned,
3182 int *this_best_prio, struct rq_iterator *iterator)
1da177e4 3183{
051c6764 3184 int loops = 0, pulled = 0, pinned = 0;
dd41f596
IM
3185 struct task_struct *p;
3186 long rem_load_move = max_load_move;
1da177e4 3187
e1d1484f 3188 if (max_load_move == 0)
1da177e4
LT
3189 goto out;
3190
81026794
NP
3191 pinned = 1;
3192
1da177e4 3193 /*
dd41f596 3194 * Start the load-balancing iterator:
1da177e4 3195 */
dd41f596
IM
3196 p = iterator->start(iterator->arg);
3197next:
b82d9fdd 3198 if (!p || loops++ > sysctl_sched_nr_migrate)
1da177e4 3199 goto out;
051c6764
PZ
3200
3201 if ((p->se.load.weight >> 1) > rem_load_move ||
dd41f596 3202 !can_migrate_task(p, busiest, this_cpu, sd, idle, &pinned)) {
dd41f596
IM
3203 p = iterator->next(iterator->arg);
3204 goto next;
1da177e4
LT
3205 }
3206
dd41f596 3207 pull_task(busiest, p, this_rq, this_cpu);
1da177e4 3208 pulled++;
dd41f596 3209 rem_load_move -= p->se.load.weight;
1da177e4 3210
7e96fa58
GH
3211#ifdef CONFIG_PREEMPT
3212 /*
3213 * NEWIDLE balancing is a source of latency, so preemptible kernels
3214 * will stop after the first task is pulled to minimize the critical
3215 * section.
3216 */
3217 if (idle == CPU_NEWLY_IDLE)
3218 goto out;
3219#endif
3220
2dd73a4f 3221 /*
b82d9fdd 3222 * We only want to steal up to the prescribed amount of weighted load.
2dd73a4f 3223 */
e1d1484f 3224 if (rem_load_move > 0) {
a4ac01c3
PW
3225 if (p->prio < *this_best_prio)
3226 *this_best_prio = p->prio;
dd41f596
IM
3227 p = iterator->next(iterator->arg);
3228 goto next;
1da177e4
LT
3229 }
3230out:
3231 /*
e1d1484f 3232 * Right now, this is one of only two places pull_task() is called,
1da177e4
LT
3233 * so we can safely collect pull_task() stats here rather than
3234 * inside pull_task().
3235 */
3236 schedstat_add(sd, lb_gained[idle], pulled);
81026794
NP
3237
3238 if (all_pinned)
3239 *all_pinned = pinned;
e1d1484f
PW
3240
3241 return max_load_move - rem_load_move;
1da177e4
LT
3242}
3243
dd41f596 3244/*
43010659
PW
3245 * move_tasks tries to move up to max_load_move weighted load from busiest to
3246 * this_rq, as part of a balancing operation within domain "sd".
3247 * Returns 1 if successful and 0 otherwise.
dd41f596
IM
3248 *
3249 * Called with both runqueues locked.
3250 */
3251static int move_tasks(struct rq *this_rq, int this_cpu, struct rq *busiest,
43010659 3252 unsigned long max_load_move,
dd41f596
IM
3253 struct sched_domain *sd, enum cpu_idle_type idle,
3254 int *all_pinned)
3255{
5522d5d5 3256 const struct sched_class *class = sched_class_highest;
43010659 3257 unsigned long total_load_moved = 0;
a4ac01c3 3258 int this_best_prio = this_rq->curr->prio;
dd41f596
IM
3259
3260 do {
43010659
PW
3261 total_load_moved +=
3262 class->load_balance(this_rq, this_cpu, busiest,
e1d1484f 3263 max_load_move - total_load_moved,
a4ac01c3 3264 sd, idle, all_pinned, &this_best_prio);
dd41f596 3265 class = class->next;
c4acb2c0 3266
7e96fa58
GH
3267#ifdef CONFIG_PREEMPT
3268 /*
3269 * NEWIDLE balancing is a source of latency, so preemptible
3270 * kernels will stop after the first task is pulled to minimize
3271 * the critical section.
3272 */
c4acb2c0
GH
3273 if (idle == CPU_NEWLY_IDLE && this_rq->nr_running)
3274 break;
7e96fa58 3275#endif
43010659 3276 } while (class && max_load_move > total_load_moved);
dd41f596 3277
43010659
PW
3278 return total_load_moved > 0;
3279}
3280
e1d1484f
PW
3281static int
3282iter_move_one_task(struct rq *this_rq, int this_cpu, struct rq *busiest,
3283 struct sched_domain *sd, enum cpu_idle_type idle,
3284 struct rq_iterator *iterator)
3285{
3286 struct task_struct *p = iterator->start(iterator->arg);
3287 int pinned = 0;
3288
3289 while (p) {
3290 if (can_migrate_task(p, busiest, this_cpu, sd, idle, &pinned)) {
3291 pull_task(busiest, p, this_rq, this_cpu);
3292 /*
3293 * Right now, this is only the second place pull_task()
3294 * is called, so we can safely collect pull_task()
3295 * stats here rather than inside pull_task().
3296 */
3297 schedstat_inc(sd, lb_gained[idle]);
3298
3299 return 1;
3300 }
3301 p = iterator->next(iterator->arg);
3302 }
3303
3304 return 0;
3305}
3306
43010659
PW
3307/*
3308 * move_one_task tries to move exactly one task from busiest to this_rq, as
3309 * part of active balancing operations within "domain".
3310 * Returns 1 if successful and 0 otherwise.
3311 *
3312 * Called with both runqueues locked.
3313 */
3314static int move_one_task(struct rq *this_rq, int this_cpu, struct rq *busiest,
3315 struct sched_domain *sd, enum cpu_idle_type idle)
3316{
5522d5d5 3317 const struct sched_class *class;
43010659
PW
3318
3319 for (class = sched_class_highest; class; class = class->next)
e1d1484f 3320 if (class->move_one_task(this_rq, this_cpu, busiest, sd, idle))
43010659
PW
3321 return 1;
3322
3323 return 0;
dd41f596 3324}
67bb6c03 3325/********** Helpers for find_busiest_group ************************/
1da177e4 3326/*
222d656d
GS
3327 * sd_lb_stats - Structure to store the statistics of a sched_domain
3328 * during load balancing.
1da177e4 3329 */
222d656d
GS
3330struct sd_lb_stats {
3331 struct sched_group *busiest; /* Busiest group in this sd */
3332 struct sched_group *this; /* Local group in this sd */
3333 unsigned long total_load; /* Total load of all groups in sd */
3334 unsigned long total_pwr; /* Total power of all groups in sd */
3335 unsigned long avg_load; /* Average load across all groups in sd */
3336
3337 /** Statistics of this group */
3338 unsigned long this_load;
3339 unsigned long this_load_per_task;
3340 unsigned long this_nr_running;
3341
3342 /* Statistics of the busiest group */
3343 unsigned long max_load;
3344 unsigned long busiest_load_per_task;
3345 unsigned long busiest_nr_running;
3346
3347 int group_imb; /* Is there imbalance in this sd */
5c45bf27 3348#if defined(CONFIG_SCHED_MC) || defined(CONFIG_SCHED_SMT)
222d656d
GS
3349 int power_savings_balance; /* Is powersave balance needed for this sd */
3350 struct sched_group *group_min; /* Least loaded group in sd */
3351 struct sched_group *group_leader; /* Group which relieves group_min */
3352 unsigned long min_load_per_task; /* load_per_task in group_min */
3353 unsigned long leader_nr_running; /* Nr running of group_leader */
3354 unsigned long min_nr_running; /* Nr running of group_min */
5c45bf27 3355#endif
222d656d 3356};
1da177e4 3357
d5ac537e 3358/*
381be78f
GS
3359 * sg_lb_stats - stats of a sched_group required for load_balancing
3360 */
3361struct sg_lb_stats {
3362 unsigned long avg_load; /*Avg load across the CPUs of the group */
3363 unsigned long group_load; /* Total load over the CPUs of the group */
3364 unsigned long sum_nr_running; /* Nr tasks running in the group */
3365 unsigned long sum_weighted_load; /* Weighted load of group's tasks */
3366 unsigned long group_capacity;
3367 int group_imb; /* Is there an imbalance in the group ? */
3368};
408ed066 3369
67bb6c03
GS
3370/**
3371 * group_first_cpu - Returns the first cpu in the cpumask of a sched_group.
3372 * @group: The group whose first cpu is to be returned.
3373 */
3374static inline unsigned int group_first_cpu(struct sched_group *group)
3375{
3376 return cpumask_first(sched_group_cpus(group));
3377}
3378
3379/**
3380 * get_sd_load_idx - Obtain the load index for a given sched domain.
3381 * @sd: The sched_domain whose load_idx is to be obtained.
3382 * @idle: The Idle status of the CPU for whose sd load_icx is obtained.
3383 */
3384static inline int get_sd_load_idx(struct sched_domain *sd,
3385 enum cpu_idle_type idle)
3386{
3387 int load_idx;
3388
3389 switch (idle) {
3390 case CPU_NOT_IDLE:
7897986b 3391 load_idx = sd->busy_idx;
67bb6c03
GS
3392 break;
3393
3394 case CPU_NEWLY_IDLE:
7897986b 3395 load_idx = sd->newidle_idx;
67bb6c03
GS
3396 break;
3397 default:
7897986b 3398 load_idx = sd->idle_idx;
67bb6c03
GS
3399 break;
3400 }
1da177e4 3401
67bb6c03
GS
3402 return load_idx;
3403}
1da177e4 3404
1da177e4 3405
c071df18
GS
3406#if defined(CONFIG_SCHED_MC) || defined(CONFIG_SCHED_SMT)
3407/**
3408 * init_sd_power_savings_stats - Initialize power savings statistics for
3409 * the given sched_domain, during load balancing.
3410 *
3411 * @sd: Sched domain whose power-savings statistics are to be initialized.
3412 * @sds: Variable containing the statistics for sd.
3413 * @idle: Idle status of the CPU at which we're performing load-balancing.
3414 */
3415static inline void init_sd_power_savings_stats(struct sched_domain *sd,
3416 struct sd_lb_stats *sds, enum cpu_idle_type idle)
3417{
3418 /*
3419 * Busy processors will not participate in power savings
3420 * balance.
3421 */
3422 if (idle == CPU_NOT_IDLE || !(sd->flags & SD_POWERSAVINGS_BALANCE))
3423 sds->power_savings_balance = 0;
3424 else {
3425 sds->power_savings_balance = 1;
3426 sds->min_nr_running = ULONG_MAX;
3427 sds->leader_nr_running = 0;
3428 }
3429}
783609c6 3430
c071df18
GS
3431/**
3432 * update_sd_power_savings_stats - Update the power saving stats for a
3433 * sched_domain while performing load balancing.
3434 *
3435 * @group: sched_group belonging to the sched_domain under consideration.
3436 * @sds: Variable containing the statistics of the sched_domain
3437 * @local_group: Does group contain the CPU for which we're performing
3438 * load balancing ?
3439 * @sgs: Variable containing the statistics of the group.
3440 */
3441static inline void update_sd_power_savings_stats(struct sched_group *group,
3442 struct sd_lb_stats *sds, int local_group, struct sg_lb_stats *sgs)
3443{
408ed066 3444
c071df18
GS
3445 if (!sds->power_savings_balance)
3446 return;
1da177e4 3447
c071df18
GS
3448 /*
3449 * If the local group is idle or completely loaded
3450 * no need to do power savings balance at this domain
3451 */
3452 if (local_group && (sds->this_nr_running >= sgs->group_capacity ||
3453 !sds->this_nr_running))
3454 sds->power_savings_balance = 0;
2dd73a4f 3455
c071df18
GS
3456 /*
3457 * If a group is already running at full capacity or idle,
3458 * don't include that group in power savings calculations
3459 */
3460 if (!sds->power_savings_balance ||
3461 sgs->sum_nr_running >= sgs->group_capacity ||
3462 !sgs->sum_nr_running)
3463 return;
5969fe06 3464
c071df18
GS
3465 /*
3466 * Calculate the group which has the least non-idle load.
3467 * This is the group from where we need to pick up the load
3468 * for saving power
3469 */
3470 if ((sgs->sum_nr_running < sds->min_nr_running) ||
3471 (sgs->sum_nr_running == sds->min_nr_running &&
3472 group_first_cpu(group) > group_first_cpu(sds->group_min))) {
3473 sds->group_min = group;
3474 sds->min_nr_running = sgs->sum_nr_running;
3475 sds->min_load_per_task = sgs->sum_weighted_load /
3476 sgs->sum_nr_running;
3477 }
783609c6 3478
c071df18
GS
3479 /*
3480 * Calculate the group which is almost near its
3481 * capacity but still has some space to pick up some load
3482 * from other group and save more power
3483 */
3484 if (sgs->sum_nr_running > sgs->group_capacity - 1)
3485 return;
1da177e4 3486
c071df18
GS
3487 if (sgs->sum_nr_running > sds->leader_nr_running ||
3488 (sgs->sum_nr_running == sds->leader_nr_running &&
3489 group_first_cpu(group) < group_first_cpu(sds->group_leader))) {
3490 sds->group_leader = group;
3491 sds->leader_nr_running = sgs->sum_nr_running;
3492 }
3493}
408ed066 3494
c071df18 3495/**
d5ac537e 3496 * check_power_save_busiest_group - see if there is potential for some power-savings balance
c071df18
GS
3497 * @sds: Variable containing the statistics of the sched_domain
3498 * under consideration.
3499 * @this_cpu: Cpu at which we're currently performing load-balancing.
3500 * @imbalance: Variable to store the imbalance.
3501 *
d5ac537e
RD
3502 * Description:
3503 * Check if we have potential to perform some power-savings balance.
3504 * If yes, set the busiest group to be the least loaded group in the
3505 * sched_domain, so that it's CPUs can be put to idle.
3506 *
c071df18
GS
3507 * Returns 1 if there is potential to perform power-savings balance.
3508 * Else returns 0.
3509 */
3510static inline int check_power_save_busiest_group(struct sd_lb_stats *sds,
3511 int this_cpu, unsigned long *imbalance)
3512{
3513 if (!sds->power_savings_balance)
3514 return 0;
1da177e4 3515
c071df18
GS
3516 if (sds->this != sds->group_leader ||
3517 sds->group_leader == sds->group_min)
3518 return 0;
783609c6 3519
c071df18
GS
3520 *imbalance = sds->min_load_per_task;
3521 sds->busiest = sds->group_min;
1da177e4 3522
c071df18
GS
3523 if (sched_mc_power_savings >= POWERSAVINGS_BALANCE_WAKEUP) {
3524 cpu_rq(this_cpu)->rd->sched_mc_preferred_wakeup_cpu =
3525 group_first_cpu(sds->group_leader);
3526 }
3527
3528 return 1;
1da177e4 3529
c071df18
GS
3530}
3531#else /* CONFIG_SCHED_MC || CONFIG_SCHED_SMT */
3532static inline void init_sd_power_savings_stats(struct sched_domain *sd,
3533 struct sd_lb_stats *sds, enum cpu_idle_type idle)
3534{
3535 return;
3536}
408ed066 3537
c071df18
GS
3538static inline void update_sd_power_savings_stats(struct sched_group *group,
3539 struct sd_lb_stats *sds, int local_group, struct sg_lb_stats *sgs)
3540{
3541 return;
3542}
3543
3544static inline int check_power_save_busiest_group(struct sd_lb_stats *sds,
3545 int this_cpu, unsigned long *imbalance)
3546{
3547 return 0;
3548}
3549#endif /* CONFIG_SCHED_MC || CONFIG_SCHED_SMT */
3550
3551
1f8c553d
GS
3552/**
3553 * update_sg_lb_stats - Update sched_group's statistics for load balancing.
3554 * @group: sched_group whose statistics are to be updated.
3555 * @this_cpu: Cpu for which load balance is currently performed.
3556 * @idle: Idle status of this_cpu
3557 * @load_idx: Load index of sched_domain of this_cpu for load calc.
3558 * @sd_idle: Idle status of the sched_domain containing group.
3559 * @local_group: Does group contain this_cpu.
3560 * @cpus: Set of cpus considered for load balancing.
3561 * @balance: Should we balance.
3562 * @sgs: variable to hold the statistics for this group.
3563 */
3564static inline void update_sg_lb_stats(struct sched_group *group, int this_cpu,
3565 enum cpu_idle_type idle, int load_idx, int *sd_idle,
3566 int local_group, const struct cpumask *cpus,
3567 int *balance, struct sg_lb_stats *sgs)
3568{
3569 unsigned long load, max_cpu_load, min_cpu_load;
3570 int i;
3571 unsigned int balance_cpu = -1, first_idle_cpu = 0;
3572 unsigned long sum_avg_load_per_task;
3573 unsigned long avg_load_per_task;
3574
3575 if (local_group)
3576 balance_cpu = group_first_cpu(group);
3577
3578 /* Tally up the load of all CPUs in the group */
3579 sum_avg_load_per_task = avg_load_per_task = 0;
3580 max_cpu_load = 0;
3581 min_cpu_load = ~0UL;
408ed066 3582
1f8c553d
GS
3583 for_each_cpu_and(i, sched_group_cpus(group), cpus) {
3584 struct rq *rq = cpu_rq(i);
908a7c1b 3585
1f8c553d
GS
3586 if (*sd_idle && rq->nr_running)
3587 *sd_idle = 0;
5c45bf27 3588
1f8c553d 3589 /* Bias balancing toward cpus of our domain */
1da177e4 3590 if (local_group) {
1f8c553d
GS
3591 if (idle_cpu(i) && !first_idle_cpu) {
3592 first_idle_cpu = 1;
3593 balance_cpu = i;
3594 }
3595
3596 load = target_load(i, load_idx);
3597 } else {
3598 load = source_load(i, load_idx);
3599 if (load > max_cpu_load)
3600 max_cpu_load = load;
3601 if (min_cpu_load > load)
3602 min_cpu_load = load;
1da177e4 3603 }
5c45bf27 3604
1f8c553d
GS
3605 sgs->group_load += load;
3606 sgs->sum_nr_running += rq->nr_running;
3607 sgs->sum_weighted_load += weighted_cpuload(i);
5c45bf27 3608
1f8c553d
GS
3609 sum_avg_load_per_task += cpu_avg_load_per_task(i);
3610 }
5c45bf27 3611
1f8c553d
GS
3612 /*
3613 * First idle cpu or the first cpu(busiest) in this sched group
3614 * is eligible for doing load balancing at this and above
3615 * domains. In the newly idle case, we will allow all the cpu's
3616 * to do the newly idle load balance.
3617 */
3618 if (idle != CPU_NEWLY_IDLE && local_group &&
3619 balance_cpu != this_cpu && balance) {
3620 *balance = 0;
3621 return;
3622 }
5c45bf27 3623
1f8c553d
GS
3624 /* Adjust by relative CPU power of the group */
3625 sgs->avg_load = sg_div_cpu_power(group,
3626 sgs->group_load * SCHED_LOAD_SCALE);
5c45bf27 3627
1f8c553d
GS
3628
3629 /*
3630 * Consider the group unbalanced when the imbalance is larger
3631 * than the average weight of two tasks.
3632 *
3633 * APZ: with cgroup the avg task weight can vary wildly and
3634 * might not be a suitable number - should we keep a
3635 * normalized nr_running number somewhere that negates
3636 * the hierarchy?
3637 */
3638 avg_load_per_task = sg_div_cpu_power(group,
3639 sum_avg_load_per_task * SCHED_LOAD_SCALE);
3640
3641 if ((max_cpu_load - min_cpu_load) > 2*avg_load_per_task)
3642 sgs->group_imb = 1;
3643
3644 sgs->group_capacity = group->__cpu_power / SCHED_LOAD_SCALE;
3645
3646}
dd41f596 3647
37abe198
GS
3648/**
3649 * update_sd_lb_stats - Update sched_group's statistics for load balancing.
3650 * @sd: sched_domain whose statistics are to be updated.
3651 * @this_cpu: Cpu for which load balance is currently performed.
3652 * @idle: Idle status of this_cpu
3653 * @sd_idle: Idle status of the sched_domain containing group.
3654 * @cpus: Set of cpus considered for load balancing.
3655 * @balance: Should we balance.
3656 * @sds: variable to hold the statistics for this sched_domain.
1da177e4 3657 */
37abe198
GS
3658static inline void update_sd_lb_stats(struct sched_domain *sd, int this_cpu,
3659 enum cpu_idle_type idle, int *sd_idle,
3660 const struct cpumask *cpus, int *balance,
3661 struct sd_lb_stats *sds)
1da177e4 3662{
222d656d 3663 struct sched_group *group = sd->groups;
37abe198 3664 struct sg_lb_stats sgs;
222d656d
GS
3665 int load_idx;
3666
c071df18 3667 init_sd_power_savings_stats(sd, sds, idle);
67bb6c03 3668 load_idx = get_sd_load_idx(sd, idle);
1da177e4
LT
3669
3670 do {
1da177e4 3671 int local_group;
1da177e4 3672
758b2cdc
RR
3673 local_group = cpumask_test_cpu(this_cpu,
3674 sched_group_cpus(group));
381be78f 3675 memset(&sgs, 0, sizeof(sgs));
1f8c553d
GS
3676 update_sg_lb_stats(group, this_cpu, idle, load_idx, sd_idle,
3677 local_group, cpus, balance, &sgs);
1da177e4 3678
37abe198
GS
3679 if (local_group && balance && !(*balance))
3680 return;
783609c6 3681
37abe198
GS
3682 sds->total_load += sgs.group_load;
3683 sds->total_pwr += group->__cpu_power;
1da177e4 3684
1da177e4 3685 if (local_group) {
37abe198
GS
3686 sds->this_load = sgs.avg_load;
3687 sds->this = group;
3688 sds->this_nr_running = sgs.sum_nr_running;
3689 sds->this_load_per_task = sgs.sum_weighted_load;
3690 } else if (sgs.avg_load > sds->max_load &&
381be78f
GS
3691 (sgs.sum_nr_running > sgs.group_capacity ||
3692 sgs.group_imb)) {
37abe198
GS
3693 sds->max_load = sgs.avg_load;
3694 sds->busiest = group;
3695 sds->busiest_nr_running = sgs.sum_nr_running;
3696 sds->busiest_load_per_task = sgs.sum_weighted_load;
3697 sds->group_imb = sgs.group_imb;
48f24c4d 3698 }
5c45bf27 3699
c071df18 3700 update_sd_power_savings_stats(group, sds, local_group, &sgs);
1da177e4
LT
3701 group = group->next;
3702 } while (group != sd->groups);
3703
37abe198 3704}
1da177e4 3705
2e6f44ae
GS
3706/**
3707 * fix_small_imbalance - Calculate the minor imbalance that exists
dbc523a3
GS
3708 * amongst the groups of a sched_domain, during
3709 * load balancing.
2e6f44ae
GS
3710 * @sds: Statistics of the sched_domain whose imbalance is to be calculated.
3711 * @this_cpu: The cpu at whose sched_domain we're performing load-balance.
3712 * @imbalance: Variable to store the imbalance.
3713 */
3714static inline void fix_small_imbalance(struct sd_lb_stats *sds,
3715 int this_cpu, unsigned long *imbalance)
3716{
3717 unsigned long tmp, pwr_now = 0, pwr_move = 0;
3718 unsigned int imbn = 2;
3719
3720 if (sds->this_nr_running) {
3721 sds->this_load_per_task /= sds->this_nr_running;
3722 if (sds->busiest_load_per_task >
3723 sds->this_load_per_task)
3724 imbn = 1;
3725 } else
3726 sds->this_load_per_task =
3727 cpu_avg_load_per_task(this_cpu);
1da177e4 3728
2e6f44ae
GS
3729 if (sds->max_load - sds->this_load + sds->busiest_load_per_task >=
3730 sds->busiest_load_per_task * imbn) {
3731 *imbalance = sds->busiest_load_per_task;
3732 return;
3733 }
908a7c1b 3734
1da177e4 3735 /*
2e6f44ae
GS
3736 * OK, we don't have enough imbalance to justify moving tasks,
3737 * however we may be able to increase total CPU power used by
3738 * moving them.
1da177e4 3739 */
2dd73a4f 3740
2e6f44ae
GS
3741 pwr_now += sds->busiest->__cpu_power *
3742 min(sds->busiest_load_per_task, sds->max_load);
3743 pwr_now += sds->this->__cpu_power *
3744 min(sds->this_load_per_task, sds->this_load);
3745 pwr_now /= SCHED_LOAD_SCALE;
3746
3747 /* Amount of load we'd subtract */
3748 tmp = sg_div_cpu_power(sds->busiest,
3749 sds->busiest_load_per_task * SCHED_LOAD_SCALE);
3750 if (sds->max_load > tmp)
3751 pwr_move += sds->busiest->__cpu_power *
3752 min(sds->busiest_load_per_task, sds->max_load - tmp);
3753
3754 /* Amount of load we'd add */
3755 if (sds->max_load * sds->busiest->__cpu_power <
3756 sds->busiest_load_per_task * SCHED_LOAD_SCALE)
3757 tmp = sg_div_cpu_power(sds->this,
3758 sds->max_load * sds->busiest->__cpu_power);
3759 else
3760 tmp = sg_div_cpu_power(sds->this,
3761 sds->busiest_load_per_task * SCHED_LOAD_SCALE);
3762 pwr_move += sds->this->__cpu_power *
3763 min(sds->this_load_per_task, sds->this_load + tmp);
3764 pwr_move /= SCHED_LOAD_SCALE;
3765
3766 /* Move if we gain throughput */
3767 if (pwr_move > pwr_now)
3768 *imbalance = sds->busiest_load_per_task;
3769}
dbc523a3
GS
3770
3771/**
3772 * calculate_imbalance - Calculate the amount of imbalance present within the
3773 * groups of a given sched_domain during load balance.
3774 * @sds: statistics of the sched_domain whose imbalance is to be calculated.
3775 * @this_cpu: Cpu for which currently load balance is being performed.
3776 * @imbalance: The variable to store the imbalance.
3777 */
3778static inline void calculate_imbalance(struct sd_lb_stats *sds, int this_cpu,
3779 unsigned long *imbalance)
3780{
3781 unsigned long max_pull;
2dd73a4f
PW
3782 /*
3783 * In the presence of smp nice balancing, certain scenarios can have
3784 * max load less than avg load(as we skip the groups at or below
3785 * its cpu_power, while calculating max_load..)
3786 */
dbc523a3 3787 if (sds->max_load < sds->avg_load) {
2dd73a4f 3788 *imbalance = 0;
dbc523a3 3789 return fix_small_imbalance(sds, this_cpu, imbalance);
2dd73a4f 3790 }
0c117f1b
SS
3791
3792 /* Don't want to pull so many tasks that a group would go idle */
dbc523a3
GS
3793 max_pull = min(sds->max_load - sds->avg_load,
3794 sds->max_load - sds->busiest_load_per_task);
0c117f1b 3795
1da177e4 3796 /* How much load to actually move to equalise the imbalance */
dbc523a3
GS
3797 *imbalance = min(max_pull * sds->busiest->__cpu_power,
3798 (sds->avg_load - sds->this_load) * sds->this->__cpu_power)
1da177e4
LT
3799 / SCHED_LOAD_SCALE;
3800
2dd73a4f
PW
3801 /*
3802 * if *imbalance is less than the average load per runnable task
3803 * there is no gaurantee that any tasks will be moved so we'll have
3804 * a think about bumping its value to force at least one task to be
3805 * moved
3806 */
dbc523a3
GS
3807 if (*imbalance < sds->busiest_load_per_task)
3808 return fix_small_imbalance(sds, this_cpu, imbalance);
1da177e4 3809
dbc523a3 3810}
37abe198 3811/******* find_busiest_group() helpers end here *********************/
1da177e4 3812
b7bb4c9b
GS
3813/**
3814 * find_busiest_group - Returns the busiest group within the sched_domain
3815 * if there is an imbalance. If there isn't an imbalance, and
3816 * the user has opted for power-savings, it returns a group whose
3817 * CPUs can be put to idle by rebalancing those tasks elsewhere, if
3818 * such a group exists.
3819 *
3820 * Also calculates the amount of weighted load which should be moved
3821 * to restore balance.
3822 *
3823 * @sd: The sched_domain whose busiest group is to be returned.
3824 * @this_cpu: The cpu for which load balancing is currently being performed.
3825 * @imbalance: Variable which stores amount of weighted load which should
3826 * be moved to restore balance/put a group to idle.
3827 * @idle: The idle status of this_cpu.
3828 * @sd_idle: The idleness of sd
3829 * @cpus: The set of CPUs under consideration for load-balancing.
3830 * @balance: Pointer to a variable indicating if this_cpu
3831 * is the appropriate cpu to perform load balancing at this_level.
3832 *
3833 * Returns: - the busiest group if imbalance exists.
3834 * - If no imbalance and user has opted for power-savings balance,
3835 * return the least loaded group whose CPUs can be
3836 * put to idle by rebalancing its tasks onto our group.
37abe198
GS
3837 */
3838static struct sched_group *
3839find_busiest_group(struct sched_domain *sd, int this_cpu,
3840 unsigned long *imbalance, enum cpu_idle_type idle,
3841 int *sd_idle, const struct cpumask *cpus, int *balance)
3842{
3843 struct sd_lb_stats sds;
1da177e4 3844
37abe198 3845 memset(&sds, 0, sizeof(sds));
1da177e4 3846
37abe198
GS
3847 /*
3848 * Compute the various statistics relavent for load balancing at
3849 * this level.
3850 */
3851 update_sd_lb_stats(sd, this_cpu, idle, sd_idle, cpus,
3852 balance, &sds);
3853
b7bb4c9b
GS
3854 /* Cases where imbalance does not exist from POV of this_cpu */
3855 /* 1) this_cpu is not the appropriate cpu to perform load balancing
3856 * at this level.
3857 * 2) There is no busy sibling group to pull from.
3858 * 3) This group is the busiest group.
3859 * 4) This group is more busy than the avg busieness at this
3860 * sched_domain.
3861 * 5) The imbalance is within the specified limit.
3862 * 6) Any rebalance would lead to ping-pong
3863 */
37abe198
GS
3864 if (balance && !(*balance))
3865 goto ret;
1da177e4 3866
b7bb4c9b
GS
3867 if (!sds.busiest || sds.busiest_nr_running == 0)
3868 goto out_balanced;
1da177e4 3869
b7bb4c9b 3870 if (sds.this_load >= sds.max_load)
1da177e4 3871 goto out_balanced;
1da177e4 3872
222d656d 3873 sds.avg_load = (SCHED_LOAD_SCALE * sds.total_load) / sds.total_pwr;
1da177e4 3874
b7bb4c9b
GS
3875 if (sds.this_load >= sds.avg_load)
3876 goto out_balanced;
3877
3878 if (100 * sds.max_load <= sd->imbalance_pct * sds.this_load)
1da177e4
LT
3879 goto out_balanced;
3880
222d656d
GS
3881 sds.busiest_load_per_task /= sds.busiest_nr_running;
3882 if (sds.group_imb)
3883 sds.busiest_load_per_task =
3884 min(sds.busiest_load_per_task, sds.avg_load);
908a7c1b 3885
1da177e4
LT
3886 /*
3887 * We're trying to get all the cpus to the average_load, so we don't
3888 * want to push ourselves above the average load, nor do we wish to
3889 * reduce the max loaded cpu below the average load, as either of these
3890 * actions would just result in more rebalancing later, and ping-pong
3891 * tasks around. Thus we look for the minimum possible imbalance.
3892 * Negative imbalances (*we* are more loaded than anyone else) will
3893 * be counted as no imbalance for these purposes -- we can't fix that
41a2d6cf 3894 * by pulling tasks to us. Be careful of negative numbers as they'll
1da177e4
LT
3895 * appear as very large values with unsigned longs.
3896 */
222d656d 3897 if (sds.max_load <= sds.busiest_load_per_task)
2dd73a4f
PW
3898 goto out_balanced;
3899
dbc523a3
GS
3900 /* Looks like there is an imbalance. Compute it */
3901 calculate_imbalance(&sds, this_cpu, imbalance);
222d656d 3902 return sds.busiest;
1da177e4
LT
3903
3904out_balanced:
c071df18
GS
3905 /*
3906 * There is no obvious imbalance. But check if we can do some balancing
3907 * to save power.
3908 */
3909 if (check_power_save_busiest_group(&sds, this_cpu, imbalance))
3910 return sds.busiest;
783609c6 3911ret:
1da177e4
LT
3912 *imbalance = 0;
3913 return NULL;
3914}
3915
3916/*
3917 * find_busiest_queue - find the busiest runqueue among the cpus in group.
3918 */
70b97a7f 3919static struct rq *
d15bcfdb 3920find_busiest_queue(struct sched_group *group, enum cpu_idle_type idle,
96f874e2 3921 unsigned long imbalance, const struct cpumask *cpus)
1da177e4 3922{
70b97a7f 3923 struct rq *busiest = NULL, *rq;
2dd73a4f 3924 unsigned long max_load = 0;
1da177e4
LT
3925 int i;
3926
758b2cdc 3927 for_each_cpu(i, sched_group_cpus(group)) {
dd41f596 3928 unsigned long wl;
0a2966b4 3929
96f874e2 3930 if (!cpumask_test_cpu(i, cpus))
0a2966b4
CL
3931 continue;
3932
48f24c4d 3933 rq = cpu_rq(i);
dd41f596 3934 wl = weighted_cpuload(i);
2dd73a4f 3935
dd41f596 3936 if (rq->nr_running == 1 && wl > imbalance)
2dd73a4f 3937 continue;
1da177e4 3938
dd41f596
IM
3939 if (wl > max_load) {
3940 max_load = wl;
48f24c4d 3941 busiest = rq;
1da177e4
LT
3942 }
3943 }
3944
3945 return busiest;
3946}
3947
77391d71
NP
3948/*
3949 * Max backoff if we encounter pinned tasks. Pretty arbitrary value, but
3950 * so long as it is large enough.
3951 */
3952#define MAX_PINNED_INTERVAL 512
3953
df7c8e84
RR
3954/* Working cpumask for load_balance and load_balance_newidle. */
3955static DEFINE_PER_CPU(cpumask_var_t, load_balance_tmpmask);
3956
1da177e4
LT
3957/*
3958 * Check this_cpu to ensure it is balanced within domain. Attempt to move
3959 * tasks if there is an imbalance.
1da177e4 3960 */
70b97a7f 3961static int load_balance(int this_cpu, struct rq *this_rq,
d15bcfdb 3962 struct sched_domain *sd, enum cpu_idle_type idle,
df7c8e84 3963 int *balance)
1da177e4 3964{
43010659 3965 int ld_moved, all_pinned = 0, active_balance = 0, sd_idle = 0;
1da177e4 3966 struct sched_group *group;
1da177e4 3967 unsigned long imbalance;
70b97a7f 3968 struct rq *busiest;
fe2eea3f 3969 unsigned long flags;
df7c8e84 3970 struct cpumask *cpus = __get_cpu_var(load_balance_tmpmask);
5969fe06 3971
96f874e2 3972 cpumask_setall(cpus);
7c16ec58 3973
89c4710e
SS
3974 /*
3975 * When power savings policy is enabled for the parent domain, idle
3976 * sibling can pick up load irrespective of busy siblings. In this case,
dd41f596 3977 * let the state of idle sibling percolate up as CPU_IDLE, instead of
d15bcfdb 3978 * portraying it as CPU_NOT_IDLE.
89c4710e 3979 */
d15bcfdb 3980 if (idle != CPU_NOT_IDLE && sd->flags & SD_SHARE_CPUPOWER &&
89c4710e 3981 !test_sd_parent(sd, SD_POWERSAVINGS_BALANCE))
5969fe06 3982 sd_idle = 1;
1da177e4 3983
2d72376b 3984 schedstat_inc(sd, lb_count[idle]);
1da177e4 3985
0a2966b4 3986redo:
c8cba857 3987 update_shares(sd);
0a2966b4 3988 group = find_busiest_group(sd, this_cpu, &imbalance, idle, &sd_idle,
7c16ec58 3989 cpus, balance);
783609c6 3990
06066714 3991 if (*balance == 0)
783609c6 3992 goto out_balanced;
783609c6 3993
1da177e4
LT
3994 if (!group) {
3995 schedstat_inc(sd, lb_nobusyg[idle]);
3996 goto out_balanced;
3997 }
3998
7c16ec58 3999 busiest = find_busiest_queue(group, idle, imbalance, cpus);
1da177e4
LT
4000 if (!busiest) {
4001 schedstat_inc(sd, lb_nobusyq[idle]);
4002 goto out_balanced;
4003 }
4004
db935dbd 4005 BUG_ON(busiest == this_rq);
1da177e4
LT
4006
4007 schedstat_add(sd, lb_imbalance[idle], imbalance);
4008
43010659 4009 ld_moved = 0;
1da177e4
LT
4010 if (busiest->nr_running > 1) {
4011 /*
4012 * Attempt to move tasks. If find_busiest_group has found
4013 * an imbalance but busiest->nr_running <= 1, the group is
43010659 4014 * still unbalanced. ld_moved simply stays zero, so it is
1da177e4
LT
4015 * correctly treated as an imbalance.
4016 */
fe2eea3f 4017 local_irq_save(flags);
e17224bf 4018 double_rq_lock(this_rq, busiest);
43010659 4019 ld_moved = move_tasks(this_rq, this_cpu, busiest,
48f24c4d 4020 imbalance, sd, idle, &all_pinned);
e17224bf 4021 double_rq_unlock(this_rq, busiest);
fe2eea3f 4022 local_irq_restore(flags);
81026794 4023
46cb4b7c
SS
4024 /*
4025 * some other cpu did the load balance for us.
4026 */
43010659 4027 if (ld_moved && this_cpu != smp_processor_id())
46cb4b7c
SS
4028 resched_cpu(this_cpu);
4029
81026794 4030 /* All tasks on this runqueue were pinned by CPU affinity */
0a2966b4 4031 if (unlikely(all_pinned)) {
96f874e2
RR
4032 cpumask_clear_cpu(cpu_of(busiest), cpus);
4033 if (!cpumask_empty(cpus))
0a2966b4 4034 goto redo;
81026794 4035 goto out_balanced;
0a2966b4 4036 }
1da177e4 4037 }
81026794 4038
43010659 4039 if (!ld_moved) {
1da177e4
LT
4040 schedstat_inc(sd, lb_failed[idle]);
4041 sd->nr_balance_failed++;
4042
4043 if (unlikely(sd->nr_balance_failed > sd->cache_nice_tries+2)) {
1da177e4 4044
fe2eea3f 4045 spin_lock_irqsave(&busiest->lock, flags);
fa3b6ddc
SS
4046
4047 /* don't kick the migration_thread, if the curr
4048 * task on busiest cpu can't be moved to this_cpu
4049 */
96f874e2
RR
4050 if (!cpumask_test_cpu(this_cpu,
4051 &busiest->curr->cpus_allowed)) {
fe2eea3f 4052 spin_unlock_irqrestore(&busiest->lock, flags);
fa3b6ddc
SS
4053 all_pinned = 1;
4054 goto out_one_pinned;
4055 }
4056
1da177e4
LT
4057 if (!busiest->active_balance) {
4058 busiest->active_balance = 1;
4059 busiest->push_cpu = this_cpu;
81026794 4060 active_balance = 1;
1da177e4 4061 }
fe2eea3f 4062 spin_unlock_irqrestore(&busiest->lock, flags);
81026794 4063 if (active_balance)
1da177e4
LT
4064 wake_up_process(busiest->migration_thread);
4065
4066 /*
4067 * We've kicked active balancing, reset the failure
4068 * counter.
4069 */
39507451 4070 sd->nr_balance_failed = sd->cache_nice_tries+1;
1da177e4 4071 }
81026794 4072 } else
1da177e4
LT
4073 sd->nr_balance_failed = 0;
4074
81026794 4075 if (likely(!active_balance)) {
1da177e4
LT
4076 /* We were unbalanced, so reset the balancing interval */
4077 sd->balance_interval = sd->min_interval;
81026794
NP
4078 } else {
4079 /*
4080 * If we've begun active balancing, start to back off. This
4081 * case may not be covered by the all_pinned logic if there
4082 * is only 1 task on the busy runqueue (because we don't call
4083 * move_tasks).
4084 */
4085 if (sd->balance_interval < sd->max_interval)
4086 sd->balance_interval *= 2;
1da177e4
LT
4087 }
4088
43010659 4089 if (!ld_moved && !sd_idle && sd->flags & SD_SHARE_CPUPOWER &&
89c4710e 4090 !test_sd_parent(sd, SD_POWERSAVINGS_BALANCE))
c09595f6
PZ
4091 ld_moved = -1;
4092
4093 goto out;
1da177e4
LT
4094
4095out_balanced:
1da177e4
LT
4096 schedstat_inc(sd, lb_balanced[idle]);
4097
16cfb1c0 4098 sd->nr_balance_failed = 0;
fa3b6ddc
SS
4099
4100out_one_pinned:
1da177e4 4101 /* tune up the balancing interval */
77391d71
NP
4102 if ((all_pinned && sd->balance_interval < MAX_PINNED_INTERVAL) ||
4103 (sd->balance_interval < sd->max_interval))
1da177e4
LT
4104 sd->balance_interval *= 2;
4105
48f24c4d 4106 if (!sd_idle && sd->flags & SD_SHARE_CPUPOWER &&
89c4710e 4107 !test_sd_parent(sd, SD_POWERSAVINGS_BALANCE))
c09595f6
PZ
4108 ld_moved = -1;
4109 else
4110 ld_moved = 0;
4111out:
c8cba857
PZ
4112 if (ld_moved)
4113 update_shares(sd);
c09595f6 4114 return ld_moved;
1da177e4
LT
4115}
4116
4117/*
4118 * Check this_cpu to ensure it is balanced within domain. Attempt to move
4119 * tasks if there is an imbalance.
4120 *
d15bcfdb 4121 * Called from schedule when this_rq is about to become idle (CPU_NEWLY_IDLE).
1da177e4
LT
4122 * this_rq is locked.
4123 */
48f24c4d 4124static int
df7c8e84 4125load_balance_newidle(int this_cpu, struct rq *this_rq, struct sched_domain *sd)
1da177e4
LT
4126{
4127 struct sched_group *group;
70b97a7f 4128 struct rq *busiest = NULL;
1da177e4 4129 unsigned long imbalance;
43010659 4130 int ld_moved = 0;
5969fe06 4131 int sd_idle = 0;
969bb4e4 4132 int all_pinned = 0;
df7c8e84 4133 struct cpumask *cpus = __get_cpu_var(load_balance_tmpmask);
7c16ec58 4134
96f874e2 4135 cpumask_setall(cpus);
5969fe06 4136
89c4710e
SS
4137 /*
4138 * When power savings policy is enabled for the parent domain, idle
4139 * sibling can pick up load irrespective of busy siblings. In this case,
4140 * let the state of idle sibling percolate up as IDLE, instead of
d15bcfdb 4141 * portraying it as CPU_NOT_IDLE.
89c4710e
SS
4142 */
4143 if (sd->flags & SD_SHARE_CPUPOWER &&
4144 !test_sd_parent(sd, SD_POWERSAVINGS_BALANCE))
5969fe06 4145 sd_idle = 1;
1da177e4 4146
2d72376b 4147 schedstat_inc(sd, lb_count[CPU_NEWLY_IDLE]);
0a2966b4 4148redo:
3e5459b4 4149 update_shares_locked(this_rq, sd);
d15bcfdb 4150 group = find_busiest_group(sd, this_cpu, &imbalance, CPU_NEWLY_IDLE,
7c16ec58 4151 &sd_idle, cpus, NULL);
1da177e4 4152 if (!group) {
d15bcfdb 4153 schedstat_inc(sd, lb_nobusyg[CPU_NEWLY_IDLE]);
16cfb1c0 4154 goto out_balanced;
1da177e4
LT
4155 }
4156
7c16ec58 4157 busiest = find_busiest_queue(group, CPU_NEWLY_IDLE, imbalance, cpus);
db935dbd 4158 if (!busiest) {
d15bcfdb 4159 schedstat_inc(sd, lb_nobusyq[CPU_NEWLY_IDLE]);
16cfb1c0 4160 goto out_balanced;
1da177e4
LT
4161 }
4162
db935dbd
NP
4163 BUG_ON(busiest == this_rq);
4164
d15bcfdb 4165 schedstat_add(sd, lb_imbalance[CPU_NEWLY_IDLE], imbalance);
d6d5cfaf 4166
43010659 4167 ld_moved = 0;
d6d5cfaf
NP
4168 if (busiest->nr_running > 1) {
4169 /* Attempt to move tasks */
4170 double_lock_balance(this_rq, busiest);
6e82a3be
IM
4171 /* this_rq->clock is already updated */
4172 update_rq_clock(busiest);
43010659 4173 ld_moved = move_tasks(this_rq, this_cpu, busiest,
969bb4e4
SS
4174 imbalance, sd, CPU_NEWLY_IDLE,
4175 &all_pinned);
1b12bbc7 4176 double_unlock_balance(this_rq, busiest);
0a2966b4 4177
969bb4e4 4178 if (unlikely(all_pinned)) {
96f874e2
RR
4179 cpumask_clear_cpu(cpu_of(busiest), cpus);
4180 if (!cpumask_empty(cpus))
0a2966b4
CL
4181 goto redo;
4182 }
d6d5cfaf
NP
4183 }
4184
43010659 4185 if (!ld_moved) {
36dffab6 4186 int active_balance = 0;
ad273b32 4187
d15bcfdb 4188 schedstat_inc(sd, lb_failed[CPU_NEWLY_IDLE]);
89c4710e
SS
4189 if (!sd_idle && sd->flags & SD_SHARE_CPUPOWER &&
4190 !test_sd_parent(sd, SD_POWERSAVINGS_BALANCE))
5969fe06 4191 return -1;
ad273b32
VS
4192
4193 if (sched_mc_power_savings < POWERSAVINGS_BALANCE_WAKEUP)
4194 return -1;
4195
4196 if (sd->nr_balance_failed++ < 2)
4197 return -1;
4198
4199 /*
4200 * The only task running in a non-idle cpu can be moved to this
4201 * cpu in an attempt to completely freeup the other CPU
4202 * package. The same method used to move task in load_balance()
4203 * have been extended for load_balance_newidle() to speedup
4204 * consolidation at sched_mc=POWERSAVINGS_BALANCE_WAKEUP (2)
4205 *
4206 * The package power saving logic comes from
4207 * find_busiest_group(). If there are no imbalance, then
4208 * f_b_g() will return NULL. However when sched_mc={1,2} then
4209 * f_b_g() will select a group from which a running task may be
4210 * pulled to this cpu in order to make the other package idle.
4211 * If there is no opportunity to make a package idle and if
4212 * there are no imbalance, then f_b_g() will return NULL and no
4213 * action will be taken in load_balance_newidle().
4214 *
4215 * Under normal task pull operation due to imbalance, there
4216 * will be more than one task in the source run queue and
4217 * move_tasks() will succeed. ld_moved will be true and this
4218 * active balance code will not be triggered.
4219 */
4220
4221 /* Lock busiest in correct order while this_rq is held */
4222 double_lock_balance(this_rq, busiest);
4223
4224 /*
4225 * don't kick the migration_thread, if the curr
4226 * task on busiest cpu can't be moved to this_cpu
4227 */
6ca09dfc 4228 if (!cpumask_test_cpu(this_cpu, &busiest->curr->cpus_allowed)) {
ad273b32
VS
4229 double_unlock_balance(this_rq, busiest);
4230 all_pinned = 1;
4231 return ld_moved;
4232 }
4233
4234 if (!busiest->active_balance) {
4235 busiest->active_balance = 1;
4236 busiest->push_cpu = this_cpu;
4237 active_balance = 1;
4238 }
4239
4240 double_unlock_balance(this_rq, busiest);
da8d5089
PZ
4241 /*
4242 * Should not call ttwu while holding a rq->lock
4243 */
4244 spin_unlock(&this_rq->lock);
ad273b32
VS
4245 if (active_balance)
4246 wake_up_process(busiest->migration_thread);
da8d5089 4247 spin_lock(&this_rq->lock);
ad273b32 4248
5969fe06 4249 } else
16cfb1c0 4250 sd->nr_balance_failed = 0;
1da177e4 4251
3e5459b4 4252 update_shares_locked(this_rq, sd);
43010659 4253 return ld_moved;
16cfb1c0
NP
4254
4255out_balanced:
d15bcfdb 4256 schedstat_inc(sd, lb_balanced[CPU_NEWLY_IDLE]);
48f24c4d 4257 if (!sd_idle && sd->flags & SD_SHARE_CPUPOWER &&
89c4710e 4258 !test_sd_parent(sd, SD_POWERSAVINGS_BALANCE))
5969fe06 4259 return -1;
16cfb1c0 4260 sd->nr_balance_failed = 0;
48f24c4d 4261
16cfb1c0 4262 return 0;
1da177e4
LT
4263}
4264
4265/*
4266 * idle_balance is called by schedule() if this_cpu is about to become
4267 * idle. Attempts to pull tasks from other CPUs.
4268 */
70b97a7f 4269static void idle_balance(int this_cpu, struct rq *this_rq)
1da177e4
LT
4270{
4271 struct sched_domain *sd;
efbe027e 4272 int pulled_task = 0;
dd41f596 4273 unsigned long next_balance = jiffies + HZ;
1da177e4
LT
4274
4275 for_each_domain(this_cpu, sd) {
92c4ca5c
CL
4276 unsigned long interval;
4277
4278 if (!(sd->flags & SD_LOAD_BALANCE))
4279 continue;
4280
4281 if (sd->flags & SD_BALANCE_NEWIDLE)
48f24c4d 4282 /* If we've pulled tasks over stop searching: */
7c16ec58 4283 pulled_task = load_balance_newidle(this_cpu, this_rq,
df7c8e84 4284 sd);
92c4ca5c
CL
4285
4286 interval = msecs_to_jiffies(sd->balance_interval);
4287 if (time_after(next_balance, sd->last_balance + interval))
4288 next_balance = sd->last_balance + interval;
4289 if (pulled_task)
4290 break;
1da177e4 4291 }
dd41f596 4292 if (pulled_task || time_after(jiffies, this_rq->next_balance)) {
1bd77f2d
CL
4293 /*
4294 * We are going idle. next_balance may be set based on
4295 * a busy processor. So reset next_balance.
4296 */
4297 this_rq->next_balance = next_balance;
dd41f596 4298 }
1da177e4
LT
4299}
4300
4301/*
4302 * active_load_balance is run by migration threads. It pushes running tasks
4303 * off the busiest CPU onto idle CPUs. It requires at least 1 task to be
4304 * running on each physical CPU where possible, and avoids physical /
4305 * logical imbalances.
4306 *
4307 * Called with busiest_rq locked.
4308 */
70b97a7f 4309static void active_load_balance(struct rq *busiest_rq, int busiest_cpu)
1da177e4 4310{
39507451 4311 int target_cpu = busiest_rq->push_cpu;
70b97a7f
IM
4312 struct sched_domain *sd;
4313 struct rq *target_rq;
39507451 4314
48f24c4d 4315 /* Is there any task to move? */
39507451 4316 if (busiest_rq->nr_running <= 1)
39507451
NP
4317 return;
4318
4319 target_rq = cpu_rq(target_cpu);
1da177e4
LT
4320
4321 /*
39507451 4322 * This condition is "impossible", if it occurs
41a2d6cf 4323 * we need to fix it. Originally reported by
39507451 4324 * Bjorn Helgaas on a 128-cpu setup.
1da177e4 4325 */
39507451 4326 BUG_ON(busiest_rq == target_rq);
1da177e4 4327
39507451
NP
4328 /* move a task from busiest_rq to target_rq */
4329 double_lock_balance(busiest_rq, target_rq);
6e82a3be
IM
4330 update_rq_clock(busiest_rq);
4331 update_rq_clock(target_rq);
39507451
NP
4332
4333 /* Search for an sd spanning us and the target CPU. */
c96d145e 4334 for_each_domain(target_cpu, sd) {
39507451 4335 if ((sd->flags & SD_LOAD_BALANCE) &&
758b2cdc 4336 cpumask_test_cpu(busiest_cpu, sched_domain_span(sd)))
39507451 4337 break;
c96d145e 4338 }
39507451 4339
48f24c4d 4340 if (likely(sd)) {
2d72376b 4341 schedstat_inc(sd, alb_count);
39507451 4342
43010659
PW
4343 if (move_one_task(target_rq, target_cpu, busiest_rq,
4344 sd, CPU_IDLE))
48f24c4d
IM
4345 schedstat_inc(sd, alb_pushed);
4346 else
4347 schedstat_inc(sd, alb_failed);
4348 }
1b12bbc7 4349 double_unlock_balance(busiest_rq, target_rq);
1da177e4
LT
4350}
4351
46cb4b7c
SS
4352#ifdef CONFIG_NO_HZ
4353static struct {
4354 atomic_t load_balancer;
7d1e6a9b 4355 cpumask_var_t cpu_mask;
f711f609 4356 cpumask_var_t ilb_grp_nohz_mask;
46cb4b7c
SS
4357} nohz ____cacheline_aligned = {
4358 .load_balancer = ATOMIC_INIT(-1),
46cb4b7c
SS
4359};
4360
f711f609
GS
4361#if defined(CONFIG_SCHED_MC) || defined(CONFIG_SCHED_SMT)
4362/**
4363 * lowest_flag_domain - Return lowest sched_domain containing flag.
4364 * @cpu: The cpu whose lowest level of sched domain is to
4365 * be returned.
4366 * @flag: The flag to check for the lowest sched_domain
4367 * for the given cpu.
4368 *
4369 * Returns the lowest sched_domain of a cpu which contains the given flag.
4370 */
4371static inline struct sched_domain *lowest_flag_domain(int cpu, int flag)
4372{
4373 struct sched_domain *sd;
4374
4375 for_each_domain(cpu, sd)
4376 if (sd && (sd->flags & flag))
4377 break;
4378
4379 return sd;
4380}
4381
4382/**
4383 * for_each_flag_domain - Iterates over sched_domains containing the flag.
4384 * @cpu: The cpu whose domains we're iterating over.
4385 * @sd: variable holding the value of the power_savings_sd
4386 * for cpu.
4387 * @flag: The flag to filter the sched_domains to be iterated.
4388 *
4389 * Iterates over all the scheduler domains for a given cpu that has the 'flag'
4390 * set, starting from the lowest sched_domain to the highest.
4391 */
4392#define for_each_flag_domain(cpu, sd, flag) \
4393 for (sd = lowest_flag_domain(cpu, flag); \
4394 (sd && (sd->flags & flag)); sd = sd->parent)
4395
4396/**
4397 * is_semi_idle_group - Checks if the given sched_group is semi-idle.
4398 * @ilb_group: group to be checked for semi-idleness
4399 *
4400 * Returns: 1 if the group is semi-idle. 0 otherwise.
4401 *
4402 * We define a sched_group to be semi idle if it has atleast one idle-CPU
4403 * and atleast one non-idle CPU. This helper function checks if the given
4404 * sched_group is semi-idle or not.
4405 */
4406static inline int is_semi_idle_group(struct sched_group *ilb_group)
4407{
4408 cpumask_and(nohz.ilb_grp_nohz_mask, nohz.cpu_mask,
4409 sched_group_cpus(ilb_group));
4410
4411 /*
4412 * A sched_group is semi-idle when it has atleast one busy cpu
4413 * and atleast one idle cpu.
4414 */
4415 if (cpumask_empty(nohz.ilb_grp_nohz_mask))
4416 return 0;
4417
4418 if (cpumask_equal(nohz.ilb_grp_nohz_mask, sched_group_cpus(ilb_group)))
4419 return 0;
4420
4421 return 1;
4422}
4423/**
4424 * find_new_ilb - Finds the optimum idle load balancer for nomination.
4425 * @cpu: The cpu which is nominating a new idle_load_balancer.
4426 *
4427 * Returns: Returns the id of the idle load balancer if it exists,
4428 * Else, returns >= nr_cpu_ids.
4429 *
4430 * This algorithm picks the idle load balancer such that it belongs to a
4431 * semi-idle powersavings sched_domain. The idea is to try and avoid
4432 * completely idle packages/cores just for the purpose of idle load balancing
4433 * when there are other idle cpu's which are better suited for that job.
4434 */
4435static int find_new_ilb(int cpu)
4436{
4437 struct sched_domain *sd;
4438 struct sched_group *ilb_group;
4439
4440 /*
4441 * Have idle load balancer selection from semi-idle packages only
4442 * when power-aware load balancing is enabled
4443 */
4444 if (!(sched_smt_power_savings || sched_mc_power_savings))
4445 goto out_done;
4446
4447 /*
4448 * Optimize for the case when we have no idle CPUs or only one
4449 * idle CPU. Don't walk the sched_domain hierarchy in such cases
4450 */
4451 if (cpumask_weight(nohz.cpu_mask) < 2)
4452 goto out_done;
4453
4454 for_each_flag_domain(cpu, sd, SD_POWERSAVINGS_BALANCE) {
4455 ilb_group = sd->groups;
4456
4457 do {
4458 if (is_semi_idle_group(ilb_group))
4459 return cpumask_first(nohz.ilb_grp_nohz_mask);
4460
4461 ilb_group = ilb_group->next;
4462
4463 } while (ilb_group != sd->groups);
4464 }
4465
4466out_done:
4467 return cpumask_first(nohz.cpu_mask);
4468}
4469#else /* (CONFIG_SCHED_MC || CONFIG_SCHED_SMT) */
4470static inline int find_new_ilb(int call_cpu)
4471{
6e29ec57 4472 return cpumask_first(nohz.cpu_mask);
f711f609
GS
4473}
4474#endif
4475
7835b98b 4476/*
46cb4b7c
SS
4477 * This routine will try to nominate the ilb (idle load balancing)
4478 * owner among the cpus whose ticks are stopped. ilb owner will do the idle
4479 * load balancing on behalf of all those cpus. If all the cpus in the system
4480 * go into this tickless mode, then there will be no ilb owner (as there is
4481 * no need for one) and all the cpus will sleep till the next wakeup event
4482 * arrives...
4483 *
4484 * For the ilb owner, tick is not stopped. And this tick will be used
4485 * for idle load balancing. ilb owner will still be part of
4486 * nohz.cpu_mask..
7835b98b 4487 *
46cb4b7c
SS
4488 * While stopping the tick, this cpu will become the ilb owner if there
4489 * is no other owner. And will be the owner till that cpu becomes busy
4490 * or if all cpus in the system stop their ticks at which point
4491 * there is no need for ilb owner.
4492 *
4493 * When the ilb owner becomes busy, it nominates another owner, during the
4494 * next busy scheduler_tick()
4495 */
4496int select_nohz_load_balancer(int stop_tick)
4497{
4498 int cpu = smp_processor_id();
4499
4500 if (stop_tick) {
46cb4b7c
SS
4501 cpu_rq(cpu)->in_nohz_recently = 1;
4502
483b4ee6
SS
4503 if (!cpu_active(cpu)) {
4504 if (atomic_read(&nohz.load_balancer) != cpu)
4505 return 0;
4506
4507 /*
4508 * If we are going offline and still the leader,
4509 * give up!
4510 */
46cb4b7c
SS
4511 if (atomic_cmpxchg(&nohz.load_balancer, cpu, -1) != cpu)
4512 BUG();
483b4ee6 4513
46cb4b7c
SS
4514 return 0;
4515 }
4516
483b4ee6
SS
4517 cpumask_set_cpu(cpu, nohz.cpu_mask);
4518
46cb4b7c 4519 /* time for ilb owner also to sleep */
7d1e6a9b 4520 if (cpumask_weight(nohz.cpu_mask) == num_online_cpus()) {
46cb4b7c
SS
4521 if (atomic_read(&nohz.load_balancer) == cpu)
4522 atomic_set(&nohz.load_balancer, -1);
4523 return 0;
4524 }
4525
4526 if (atomic_read(&nohz.load_balancer) == -1) {
4527 /* make me the ilb owner */
4528 if (atomic_cmpxchg(&nohz.load_balancer, -1, cpu) == -1)
4529 return 1;
e790fb0b
GS
4530 } else if (atomic_read(&nohz.load_balancer) == cpu) {
4531 int new_ilb;
4532
4533 if (!(sched_smt_power_savings ||
4534 sched_mc_power_savings))
4535 return 1;
4536 /*
4537 * Check to see if there is a more power-efficient
4538 * ilb.
4539 */
4540 new_ilb = find_new_ilb(cpu);
4541 if (new_ilb < nr_cpu_ids && new_ilb != cpu) {
4542 atomic_set(&nohz.load_balancer, -1);
4543 resched_cpu(new_ilb);
4544 return 0;
4545 }
46cb4b7c 4546 return 1;
e790fb0b 4547 }
46cb4b7c 4548 } else {
7d1e6a9b 4549 if (!cpumask_test_cpu(cpu, nohz.cpu_mask))
46cb4b7c
SS
4550 return 0;
4551
7d1e6a9b 4552 cpumask_clear_cpu(cpu, nohz.cpu_mask);
46cb4b7c
SS
4553
4554 if (atomic_read(&nohz.load_balancer) == cpu)
4555 if (atomic_cmpxchg(&nohz.load_balancer, cpu, -1) != cpu)
4556 BUG();
4557 }
4558 return 0;
4559}
4560#endif
4561
4562static DEFINE_SPINLOCK(balancing);
4563
4564/*
7835b98b
CL
4565 * It checks each scheduling domain to see if it is due to be balanced,
4566 * and initiates a balancing operation if so.
4567 *
4568 * Balancing parameters are set up in arch_init_sched_domains.
4569 */
a9957449 4570static void rebalance_domains(int cpu, enum cpu_idle_type idle)
7835b98b 4571{
46cb4b7c
SS
4572 int balance = 1;
4573 struct rq *rq = cpu_rq(cpu);
7835b98b
CL
4574 unsigned long interval;
4575 struct sched_domain *sd;
46cb4b7c 4576 /* Earliest time when we have to do rebalance again */
c9819f45 4577 unsigned long next_balance = jiffies + 60*HZ;
f549da84 4578 int update_next_balance = 0;
d07355f5 4579 int need_serialize;
1da177e4 4580
46cb4b7c 4581 for_each_domain(cpu, sd) {
1da177e4
LT
4582 if (!(sd->flags & SD_LOAD_BALANCE))
4583 continue;
4584
4585 interval = sd->balance_interval;
d15bcfdb 4586 if (idle != CPU_IDLE)
1da177e4
LT
4587 interval *= sd->busy_factor;
4588
4589 /* scale ms to jiffies */
4590 interval = msecs_to_jiffies(interval);
4591 if (unlikely(!interval))
4592 interval = 1;
dd41f596
IM
4593 if (interval > HZ*NR_CPUS/10)
4594 interval = HZ*NR_CPUS/10;
4595
d07355f5 4596 need_serialize = sd->flags & SD_SERIALIZE;
1da177e4 4597
d07355f5 4598 if (need_serialize) {
08c183f3
CL
4599 if (!spin_trylock(&balancing))
4600 goto out;
4601 }
4602
c9819f45 4603 if (time_after_eq(jiffies, sd->last_balance + interval)) {
df7c8e84 4604 if (load_balance(cpu, rq, sd, idle, &balance)) {
fa3b6ddc
SS
4605 /*
4606 * We've pulled tasks over so either we're no
5969fe06
NP
4607 * longer idle, or one of our SMT siblings is
4608 * not idle.
4609 */
d15bcfdb 4610 idle = CPU_NOT_IDLE;
1da177e4 4611 }
1bd77f2d 4612 sd->last_balance = jiffies;
1da177e4 4613 }
d07355f5 4614 if (need_serialize)
08c183f3
CL
4615 spin_unlock(&balancing);
4616out:
f549da84 4617 if (time_after(next_balance, sd->last_balance + interval)) {
c9819f45 4618 next_balance = sd->last_balance + interval;
f549da84
SS
4619 update_next_balance = 1;
4620 }
783609c6
SS
4621
4622 /*
4623 * Stop the load balance at this level. There is another
4624 * CPU in our sched group which is doing load balancing more
4625 * actively.
4626 */
4627 if (!balance)
4628 break;
1da177e4 4629 }
f549da84
SS
4630
4631 /*
4632 * next_balance will be updated only when there is a need.
4633 * When the cpu is attached to null domain for ex, it will not be
4634 * updated.
4635 */
4636 if (likely(update_next_balance))
4637 rq->next_balance = next_balance;
46cb4b7c
SS
4638}
4639
4640/*
4641 * run_rebalance_domains is triggered when needed from the scheduler tick.
4642 * In CONFIG_NO_HZ case, the idle load balance owner will do the
4643 * rebalancing for all the cpus for whom scheduler ticks are stopped.
4644 */
4645static void run_rebalance_domains(struct softirq_action *h)
4646{
dd41f596
IM
4647 int this_cpu = smp_processor_id();
4648 struct rq *this_rq = cpu_rq(this_cpu);
4649 enum cpu_idle_type idle = this_rq->idle_at_tick ?
4650 CPU_IDLE : CPU_NOT_IDLE;
46cb4b7c 4651
dd41f596 4652 rebalance_domains(this_cpu, idle);
46cb4b7c
SS
4653
4654#ifdef CONFIG_NO_HZ
4655 /*
4656 * If this cpu is the owner for idle load balancing, then do the
4657 * balancing on behalf of the other idle cpus whose ticks are
4658 * stopped.
4659 */
dd41f596
IM
4660 if (this_rq->idle_at_tick &&
4661 atomic_read(&nohz.load_balancer) == this_cpu) {
46cb4b7c
SS
4662 struct rq *rq;
4663 int balance_cpu;
4664
7d1e6a9b
RR
4665 for_each_cpu(balance_cpu, nohz.cpu_mask) {
4666 if (balance_cpu == this_cpu)
4667 continue;
4668
46cb4b7c
SS
4669 /*
4670 * If this cpu gets work to do, stop the load balancing
4671 * work being done for other cpus. Next load
4672 * balancing owner will pick it up.
4673 */
4674 if (need_resched())
4675 break;
4676
de0cf899 4677 rebalance_domains(balance_cpu, CPU_IDLE);
46cb4b7c
SS
4678
4679 rq = cpu_rq(balance_cpu);
dd41f596
IM
4680 if (time_after(this_rq->next_balance, rq->next_balance))
4681 this_rq->next_balance = rq->next_balance;
46cb4b7c
SS
4682 }
4683 }
4684#endif
4685}
4686
8a0be9ef
FW
4687static inline int on_null_domain(int cpu)
4688{
4689 return !rcu_dereference(cpu_rq(cpu)->sd);
4690}
4691
46cb4b7c
SS
4692/*
4693 * Trigger the SCHED_SOFTIRQ if it is time to do periodic load balancing.
4694 *
4695 * In case of CONFIG_NO_HZ, this is the place where we nominate a new
4696 * idle load balancing owner or decide to stop the periodic load balancing,
4697 * if the whole system is idle.
4698 */
dd41f596 4699static inline void trigger_load_balance(struct rq *rq, int cpu)
46cb4b7c 4700{
46cb4b7c
SS
4701#ifdef CONFIG_NO_HZ
4702 /*
4703 * If we were in the nohz mode recently and busy at the current
4704 * scheduler tick, then check if we need to nominate new idle
4705 * load balancer.
4706 */
4707 if (rq->in_nohz_recently && !rq->idle_at_tick) {
4708 rq->in_nohz_recently = 0;
4709
4710 if (atomic_read(&nohz.load_balancer) == cpu) {
7d1e6a9b 4711 cpumask_clear_cpu(cpu, nohz.cpu_mask);
46cb4b7c
SS
4712 atomic_set(&nohz.load_balancer, -1);
4713 }
4714
4715 if (atomic_read(&nohz.load_balancer) == -1) {
f711f609 4716 int ilb = find_new_ilb(cpu);
46cb4b7c 4717
434d53b0 4718 if (ilb < nr_cpu_ids)
46cb4b7c
SS
4719 resched_cpu(ilb);
4720 }
4721 }
4722
4723 /*
4724 * If this cpu is idle and doing idle load balancing for all the
4725 * cpus with ticks stopped, is it time for that to stop?
4726 */
4727 if (rq->idle_at_tick && atomic_read(&nohz.load_balancer) == cpu &&
7d1e6a9b 4728 cpumask_weight(nohz.cpu_mask) == num_online_cpus()) {
46cb4b7c
SS
4729 resched_cpu(cpu);
4730 return;
4731 }
4732
4733 /*
4734 * If this cpu is idle and the idle load balancing is done by
4735 * someone else, then no need raise the SCHED_SOFTIRQ
4736 */
4737 if (rq->idle_at_tick && atomic_read(&nohz.load_balancer) != cpu &&
7d1e6a9b 4738 cpumask_test_cpu(cpu, nohz.cpu_mask))
46cb4b7c
SS
4739 return;
4740#endif
8a0be9ef
FW
4741 /* Don't need to rebalance while attached to NULL domain */
4742 if (time_after_eq(jiffies, rq->next_balance) &&
4743 likely(!on_null_domain(cpu)))
46cb4b7c 4744 raise_softirq(SCHED_SOFTIRQ);
1da177e4 4745}
dd41f596
IM
4746
4747#else /* CONFIG_SMP */
4748
1da177e4
LT
4749/*
4750 * on UP we do not need to balance between CPUs:
4751 */
70b97a7f 4752static inline void idle_balance(int cpu, struct rq *rq)
1da177e4
LT
4753{
4754}
dd41f596 4755
1da177e4
LT
4756#endif
4757
1da177e4
LT
4758DEFINE_PER_CPU(struct kernel_stat, kstat);
4759
4760EXPORT_PER_CPU_SYMBOL(kstat);
4761
4762/*
c5f8d995 4763 * Return any ns on the sched_clock that have not yet been accounted in
f06febc9 4764 * @p in case that task is currently running.
c5f8d995
HS
4765 *
4766 * Called with task_rq_lock() held on @rq.
1da177e4 4767 */
c5f8d995
HS
4768static u64 do_task_delta_exec(struct task_struct *p, struct rq *rq)
4769{
4770 u64 ns = 0;
4771
4772 if (task_current(rq, p)) {
4773 update_rq_clock(rq);
4774 ns = rq->clock - p->se.exec_start;
4775 if ((s64)ns < 0)
4776 ns = 0;
4777 }
4778
4779 return ns;
4780}
4781
bb34d92f 4782unsigned long long task_delta_exec(struct task_struct *p)
1da177e4 4783{
1da177e4 4784 unsigned long flags;
41b86e9c 4785 struct rq *rq;
bb34d92f 4786 u64 ns = 0;
48f24c4d 4787
41b86e9c 4788 rq = task_rq_lock(p, &flags);
c5f8d995
HS
4789 ns = do_task_delta_exec(p, rq);
4790 task_rq_unlock(rq, &flags);
1508487e 4791
c5f8d995
HS
4792 return ns;
4793}
f06febc9 4794
c5f8d995
HS
4795/*
4796 * Return accounted runtime for the task.
4797 * In case the task is currently running, return the runtime plus current's
4798 * pending runtime that have not been accounted yet.
4799 */
4800unsigned long long task_sched_runtime(struct task_struct *p)
4801{
4802 unsigned long flags;
4803 struct rq *rq;
4804 u64 ns = 0;
4805
4806 rq = task_rq_lock(p, &flags);
4807 ns = p->se.sum_exec_runtime + do_task_delta_exec(p, rq);
4808 task_rq_unlock(rq, &flags);
4809
4810 return ns;
4811}
48f24c4d 4812
c5f8d995
HS
4813/*
4814 * Return sum_exec_runtime for the thread group.
4815 * In case the task is currently running, return the sum plus current's
4816 * pending runtime that have not been accounted yet.
4817 *
4818 * Note that the thread group might have other running tasks as well,
4819 * so the return value not includes other pending runtime that other
4820 * running tasks might have.
4821 */
4822unsigned long long thread_group_sched_runtime(struct task_struct *p)
4823{
4824 struct task_cputime totals;
4825 unsigned long flags;
4826 struct rq *rq;
4827 u64 ns;
4828
4829 rq = task_rq_lock(p, &flags);
4830 thread_group_cputime(p, &totals);
4831 ns = totals.sum_exec_runtime + do_task_delta_exec(p, rq);
41b86e9c 4832 task_rq_unlock(rq, &flags);
48f24c4d 4833
1da177e4
LT
4834 return ns;
4835}
4836
1da177e4
LT
4837/*
4838 * Account user cpu time to a process.
4839 * @p: the process that the cpu time gets accounted to
1da177e4 4840 * @cputime: the cpu time spent in user space since the last update
457533a7 4841 * @cputime_scaled: cputime scaled by cpu frequency
1da177e4 4842 */
457533a7
MS
4843void account_user_time(struct task_struct *p, cputime_t cputime,
4844 cputime_t cputime_scaled)
1da177e4
LT
4845{
4846 struct cpu_usage_stat *cpustat = &kstat_this_cpu.cpustat;
4847 cputime64_t tmp;
4848
457533a7 4849 /* Add user time to process. */
1da177e4 4850 p->utime = cputime_add(p->utime, cputime);
457533a7 4851 p->utimescaled = cputime_add(p->utimescaled, cputime_scaled);
f06febc9 4852 account_group_user_time(p, cputime);
1da177e4
LT
4853
4854 /* Add user time to cpustat. */
4855 tmp = cputime_to_cputime64(cputime);
4856 if (TASK_NICE(p) > 0)
4857 cpustat->nice = cputime64_add(cpustat->nice, tmp);
4858 else
4859 cpustat->user = cputime64_add(cpustat->user, tmp);
ef12fefa
BR
4860
4861 cpuacct_update_stats(p, CPUACCT_STAT_USER, cputime);
49b5cf34
JL
4862 /* Account for user time used */
4863 acct_update_integrals(p);
1da177e4
LT
4864}
4865
94886b84
LV
4866/*
4867 * Account guest cpu time to a process.
4868 * @p: the process that the cpu time gets accounted to
4869 * @cputime: the cpu time spent in virtual machine since the last update
457533a7 4870 * @cputime_scaled: cputime scaled by cpu frequency
94886b84 4871 */
457533a7
MS
4872static void account_guest_time(struct task_struct *p, cputime_t cputime,
4873 cputime_t cputime_scaled)
94886b84
LV
4874{
4875 cputime64_t tmp;
4876 struct cpu_usage_stat *cpustat = &kstat_this_cpu.cpustat;
4877
4878 tmp = cputime_to_cputime64(cputime);
4879
457533a7 4880 /* Add guest time to process. */
94886b84 4881 p->utime = cputime_add(p->utime, cputime);
457533a7 4882 p->utimescaled = cputime_add(p->utimescaled, cputime_scaled);
f06febc9 4883 account_group_user_time(p, cputime);
94886b84
LV
4884 p->gtime = cputime_add(p->gtime, cputime);
4885
457533a7 4886 /* Add guest time to cpustat. */
94886b84
LV
4887 cpustat->user = cputime64_add(cpustat->user, tmp);
4888 cpustat->guest = cputime64_add(cpustat->guest, tmp);
4889}
4890
1da177e4
LT
4891/*
4892 * Account system cpu time to a process.
4893 * @p: the process that the cpu time gets accounted to
4894 * @hardirq_offset: the offset to subtract from hardirq_count()
4895 * @cputime: the cpu time spent in kernel space since the last update
457533a7 4896 * @cputime_scaled: cputime scaled by cpu frequency
1da177e4
LT
4897 */
4898void account_system_time(struct task_struct *p, int hardirq_offset,
457533a7 4899 cputime_t cputime, cputime_t cputime_scaled)
1da177e4
LT
4900{
4901 struct cpu_usage_stat *cpustat = &kstat_this_cpu.cpustat;
1da177e4
LT
4902 cputime64_t tmp;
4903
983ed7a6 4904 if ((p->flags & PF_VCPU) && (irq_count() - hardirq_offset == 0)) {
457533a7 4905 account_guest_time(p, cputime, cputime_scaled);
983ed7a6
HH
4906 return;
4907 }
94886b84 4908
457533a7 4909 /* Add system time to process. */
1da177e4 4910 p->stime = cputime_add(p->stime, cputime);
457533a7 4911 p->stimescaled = cputime_add(p->stimescaled, cputime_scaled);
f06febc9 4912 account_group_system_time(p, cputime);
1da177e4
LT
4913
4914 /* Add system time to cpustat. */
4915 tmp = cputime_to_cputime64(cputime);
4916 if (hardirq_count() - hardirq_offset)
4917 cpustat->irq = cputime64_add(cpustat->irq, tmp);
4918 else if (softirq_count())
4919 cpustat->softirq = cputime64_add(cpustat->softirq, tmp);
1da177e4 4920 else
79741dd3
MS
4921 cpustat->system = cputime64_add(cpustat->system, tmp);
4922
ef12fefa
BR
4923 cpuacct_update_stats(p, CPUACCT_STAT_SYSTEM, cputime);
4924
1da177e4
LT
4925 /* Account for system time used */
4926 acct_update_integrals(p);
1da177e4
LT
4927}
4928
c66f08be 4929/*
1da177e4 4930 * Account for involuntary wait time.
1da177e4 4931 * @steal: the cpu time spent in involuntary wait
c66f08be 4932 */
79741dd3 4933void account_steal_time(cputime_t cputime)
c66f08be 4934{
79741dd3
MS
4935 struct cpu_usage_stat *cpustat = &kstat_this_cpu.cpustat;
4936 cputime64_t cputime64 = cputime_to_cputime64(cputime);
4937
4938 cpustat->steal = cputime64_add(cpustat->steal, cputime64);
c66f08be
MN
4939}
4940
1da177e4 4941/*
79741dd3
MS
4942 * Account for idle time.
4943 * @cputime: the cpu time spent in idle wait
1da177e4 4944 */
79741dd3 4945void account_idle_time(cputime_t cputime)
1da177e4
LT
4946{
4947 struct cpu_usage_stat *cpustat = &kstat_this_cpu.cpustat;
79741dd3 4948 cputime64_t cputime64 = cputime_to_cputime64(cputime);
70b97a7f 4949 struct rq *rq = this_rq();
1da177e4 4950
79741dd3
MS
4951 if (atomic_read(&rq->nr_iowait) > 0)
4952 cpustat->iowait = cputime64_add(cpustat->iowait, cputime64);
4953 else
4954 cpustat->idle = cputime64_add(cpustat->idle, cputime64);
1da177e4
LT
4955}
4956
79741dd3
MS
4957#ifndef CONFIG_VIRT_CPU_ACCOUNTING
4958
4959/*
4960 * Account a single tick of cpu time.
4961 * @p: the process that the cpu time gets accounted to
4962 * @user_tick: indicates if the tick is a user or a system tick
4963 */
4964void account_process_tick(struct task_struct *p, int user_tick)
4965{
4966 cputime_t one_jiffy = jiffies_to_cputime(1);
4967 cputime_t one_jiffy_scaled = cputime_to_scaled(one_jiffy);
4968 struct rq *rq = this_rq();
4969
4970 if (user_tick)
4971 account_user_time(p, one_jiffy, one_jiffy_scaled);
f5f293a4 4972 else if ((p != rq->idle) || (irq_count() != HARDIRQ_OFFSET))
79741dd3
MS
4973 account_system_time(p, HARDIRQ_OFFSET, one_jiffy,
4974 one_jiffy_scaled);
4975 else
4976 account_idle_time(one_jiffy);
4977}
4978
4979/*
4980 * Account multiple ticks of steal time.
4981 * @p: the process from which the cpu time has been stolen
4982 * @ticks: number of stolen ticks
4983 */
4984void account_steal_ticks(unsigned long ticks)
4985{
4986 account_steal_time(jiffies_to_cputime(ticks));
4987}
4988
4989/*
4990 * Account multiple ticks of idle time.
4991 * @ticks: number of stolen ticks
4992 */
4993void account_idle_ticks(unsigned long ticks)
4994{
4995 account_idle_time(jiffies_to_cputime(ticks));
1da177e4
LT
4996}
4997
79741dd3
MS
4998#endif
4999
49048622
BS
5000/*
5001 * Use precise platform statistics if available:
5002 */
5003#ifdef CONFIG_VIRT_CPU_ACCOUNTING
5004cputime_t task_utime(struct task_struct *p)
5005{
5006 return p->utime;
5007}
5008
5009cputime_t task_stime(struct task_struct *p)
5010{
5011 return p->stime;
5012}
5013#else
5014cputime_t task_utime(struct task_struct *p)
5015{
5016 clock_t utime = cputime_to_clock_t(p->utime),
5017 total = utime + cputime_to_clock_t(p->stime);
5018 u64 temp;
5019
5020 /*
5021 * Use CFS's precise accounting:
5022 */
5023 temp = (u64)nsec_to_clock_t(p->se.sum_exec_runtime);
5024
5025 if (total) {
5026 temp *= utime;
5027 do_div(temp, total);
5028 }
5029 utime = (clock_t)temp;
5030
5031 p->prev_utime = max(p->prev_utime, clock_t_to_cputime(utime));
5032 return p->prev_utime;
5033}
5034
5035cputime_t task_stime(struct task_struct *p)
5036{
5037 clock_t stime;
5038
5039 /*
5040 * Use CFS's precise accounting. (we subtract utime from
5041 * the total, to make sure the total observed by userspace
5042 * grows monotonically - apps rely on that):
5043 */
5044 stime = nsec_to_clock_t(p->se.sum_exec_runtime) -
5045 cputime_to_clock_t(task_utime(p));
5046
5047 if (stime >= 0)
5048 p->prev_stime = max(p->prev_stime, clock_t_to_cputime(stime));
5049
5050 return p->prev_stime;
5051}
5052#endif
5053
5054inline cputime_t task_gtime(struct task_struct *p)
5055{
5056 return p->gtime;
5057}
5058
7835b98b
CL
5059/*
5060 * This function gets called by the timer code, with HZ frequency.
5061 * We call it with interrupts disabled.
5062 *
5063 * It also gets called by the fork code, when changing the parent's
5064 * timeslices.
5065 */
5066void scheduler_tick(void)
5067{
7835b98b
CL
5068 int cpu = smp_processor_id();
5069 struct rq *rq = cpu_rq(cpu);
dd41f596 5070 struct task_struct *curr = rq->curr;
3e51f33f
PZ
5071
5072 sched_clock_tick();
dd41f596
IM
5073
5074 spin_lock(&rq->lock);
3e51f33f 5075 update_rq_clock(rq);
f1a438d8 5076 update_cpu_load(rq);
fa85ae24 5077 curr->sched_class->task_tick(rq, curr, 0);
dd41f596 5078 spin_unlock(&rq->lock);
7835b98b 5079
e418e1c2 5080#ifdef CONFIG_SMP
dd41f596
IM
5081 rq->idle_at_tick = idle_cpu(cpu);
5082 trigger_load_balance(rq, cpu);
e418e1c2 5083#endif
1da177e4
LT
5084}
5085
132380a0 5086notrace unsigned long get_parent_ip(unsigned long addr)
6cd8a4bb
SR
5087{
5088 if (in_lock_functions(addr)) {
5089 addr = CALLER_ADDR2;
5090 if (in_lock_functions(addr))
5091 addr = CALLER_ADDR3;
5092 }
5093 return addr;
5094}
1da177e4 5095
7e49fcce
SR
5096#if defined(CONFIG_PREEMPT) && (defined(CONFIG_DEBUG_PREEMPT) || \
5097 defined(CONFIG_PREEMPT_TRACER))
5098
43627582 5099void __kprobes add_preempt_count(int val)
1da177e4 5100{
6cd8a4bb 5101#ifdef CONFIG_DEBUG_PREEMPT
1da177e4
LT
5102 /*
5103 * Underflow?
5104 */
9a11b49a
IM
5105 if (DEBUG_LOCKS_WARN_ON((preempt_count() < 0)))
5106 return;
6cd8a4bb 5107#endif
1da177e4 5108 preempt_count() += val;
6cd8a4bb 5109#ifdef CONFIG_DEBUG_PREEMPT
1da177e4
LT
5110 /*
5111 * Spinlock count overflowing soon?
5112 */
33859f7f
MOS
5113 DEBUG_LOCKS_WARN_ON((preempt_count() & PREEMPT_MASK) >=
5114 PREEMPT_MASK - 10);
6cd8a4bb
SR
5115#endif
5116 if (preempt_count() == val)
5117 trace_preempt_off(CALLER_ADDR0, get_parent_ip(CALLER_ADDR1));
1da177e4
LT
5118}
5119EXPORT_SYMBOL(add_preempt_count);
5120
43627582 5121void __kprobes sub_preempt_count(int val)
1da177e4 5122{
6cd8a4bb 5123#ifdef CONFIG_DEBUG_PREEMPT
1da177e4
LT
5124 /*
5125 * Underflow?
5126 */
01e3eb82 5127 if (DEBUG_LOCKS_WARN_ON(val > preempt_count()))
9a11b49a 5128 return;
1da177e4
LT
5129 /*
5130 * Is the spinlock portion underflowing?
5131 */
9a11b49a
IM
5132 if (DEBUG_LOCKS_WARN_ON((val < PREEMPT_MASK) &&
5133 !(preempt_count() & PREEMPT_MASK)))
5134 return;
6cd8a4bb 5135#endif
9a11b49a 5136
6cd8a4bb
SR
5137 if (preempt_count() == val)
5138 trace_preempt_on(CALLER_ADDR0, get_parent_ip(CALLER_ADDR1));
1da177e4
LT
5139 preempt_count() -= val;
5140}
5141EXPORT_SYMBOL(sub_preempt_count);
5142
5143#endif
5144
5145/*
dd41f596 5146 * Print scheduling while atomic bug:
1da177e4 5147 */
dd41f596 5148static noinline void __schedule_bug(struct task_struct *prev)
1da177e4 5149{
838225b4
SS
5150 struct pt_regs *regs = get_irq_regs();
5151
5152 printk(KERN_ERR "BUG: scheduling while atomic: %s/%d/0x%08x\n",
5153 prev->comm, prev->pid, preempt_count());
5154
dd41f596 5155 debug_show_held_locks(prev);
e21f5b15 5156 print_modules();
dd41f596
IM
5157 if (irqs_disabled())
5158 print_irqtrace_events(prev);
838225b4
SS
5159
5160 if (regs)
5161 show_regs(regs);
5162 else
5163 dump_stack();
dd41f596 5164}
1da177e4 5165
dd41f596
IM
5166/*
5167 * Various schedule()-time debugging checks and statistics:
5168 */
5169static inline void schedule_debug(struct task_struct *prev)
5170{
1da177e4 5171 /*
41a2d6cf 5172 * Test if we are atomic. Since do_exit() needs to call into
1da177e4
LT
5173 * schedule() atomically, we ignore that path for now.
5174 * Otherwise, whine if we are scheduling when we should not be.
5175 */
3f33a7ce 5176 if (unlikely(in_atomic_preempt_off() && !prev->exit_state))
dd41f596
IM
5177 __schedule_bug(prev);
5178
1da177e4
LT
5179 profile_hit(SCHED_PROFILING, __builtin_return_address(0));
5180
2d72376b 5181 schedstat_inc(this_rq(), sched_count);
b8efb561
IM
5182#ifdef CONFIG_SCHEDSTATS
5183 if (unlikely(prev->lock_depth >= 0)) {
2d72376b
IM
5184 schedstat_inc(this_rq(), bkl_count);
5185 schedstat_inc(prev, sched_info.bkl_count);
b8efb561
IM
5186 }
5187#endif
dd41f596
IM
5188}
5189
df1c99d4
MG
5190static void put_prev_task(struct rq *rq, struct task_struct *prev)
5191{
5192 if (prev->state == TASK_RUNNING) {
5193 u64 runtime = prev->se.sum_exec_runtime;
5194
5195 runtime -= prev->se.prev_sum_exec_runtime;
5196 runtime = min_t(u64, runtime, 2*sysctl_sched_migration_cost);
5197
5198 /*
5199 * In order to avoid avg_overlap growing stale when we are
5200 * indeed overlapping and hence not getting put to sleep, grow
5201 * the avg_overlap on preemption.
5202 *
5203 * We use the average preemption runtime because that
5204 * correlates to the amount of cache footprint a task can
5205 * build up.
5206 */
5207 update_avg(&prev->se.avg_overlap, runtime);
5208 }
5209 prev->sched_class->put_prev_task(rq, prev);
5210}
5211
dd41f596
IM
5212/*
5213 * Pick up the highest-prio task:
5214 */
5215static inline struct task_struct *
b67802ea 5216pick_next_task(struct rq *rq)
dd41f596 5217{
5522d5d5 5218 const struct sched_class *class;
dd41f596 5219 struct task_struct *p;
1da177e4
LT
5220
5221 /*
dd41f596
IM
5222 * Optimization: we know that if all tasks are in
5223 * the fair class we can call that function directly:
1da177e4 5224 */
dd41f596 5225 if (likely(rq->nr_running == rq->cfs.nr_running)) {
fb8d4724 5226 p = fair_sched_class.pick_next_task(rq);
dd41f596
IM
5227 if (likely(p))
5228 return p;
1da177e4
LT
5229 }
5230
dd41f596
IM
5231 class = sched_class_highest;
5232 for ( ; ; ) {
fb8d4724 5233 p = class->pick_next_task(rq);
dd41f596
IM
5234 if (p)
5235 return p;
5236 /*
5237 * Will never be NULL as the idle class always
5238 * returns a non-NULL p:
5239 */
5240 class = class->next;
5241 }
5242}
1da177e4 5243
dd41f596
IM
5244/*
5245 * schedule() is the main scheduler function.
5246 */
ff743345 5247asmlinkage void __sched schedule(void)
dd41f596
IM
5248{
5249 struct task_struct *prev, *next;
67ca7bde 5250 unsigned long *switch_count;
dd41f596 5251 struct rq *rq;
31656519 5252 int cpu;
dd41f596 5253
ff743345
PZ
5254need_resched:
5255 preempt_disable();
dd41f596
IM
5256 cpu = smp_processor_id();
5257 rq = cpu_rq(cpu);
5258 rcu_qsctr_inc(cpu);
5259 prev = rq->curr;
5260 switch_count = &prev->nivcsw;
5261
5262 release_kernel_lock(prev);
5263need_resched_nonpreemptible:
5264
5265 schedule_debug(prev);
1da177e4 5266
31656519 5267 if (sched_feat(HRTICK))
f333fdc9 5268 hrtick_clear(rq);
8f4d37ec 5269
8cd162ce 5270 spin_lock_irq(&rq->lock);
3e51f33f 5271 update_rq_clock(rq);
1e819950 5272 clear_tsk_need_resched(prev);
1da177e4 5273
1da177e4 5274 if (prev->state && !(preempt_count() & PREEMPT_ACTIVE)) {
16882c1e 5275 if (unlikely(signal_pending_state(prev->state, prev)))
1da177e4 5276 prev->state = TASK_RUNNING;
16882c1e 5277 else
2e1cb74a 5278 deactivate_task(rq, prev, 1);
dd41f596 5279 switch_count = &prev->nvcsw;
1da177e4
LT
5280 }
5281
9a897c5a
SR
5282#ifdef CONFIG_SMP
5283 if (prev->sched_class->pre_schedule)
5284 prev->sched_class->pre_schedule(rq, prev);
5285#endif
f65eda4f 5286
dd41f596 5287 if (unlikely(!rq->nr_running))
1da177e4 5288 idle_balance(cpu, rq);
1da177e4 5289
df1c99d4 5290 put_prev_task(rq, prev);
b67802ea 5291 next = pick_next_task(rq);
1da177e4 5292
1da177e4 5293 if (likely(prev != next)) {
673a90a1
DS
5294 sched_info_switch(prev, next);
5295
1da177e4
LT
5296 rq->nr_switches++;
5297 rq->curr = next;
5298 ++*switch_count;
5299
dd41f596 5300 context_switch(rq, prev, next); /* unlocks the rq */
8f4d37ec
PZ
5301 /*
5302 * the context switch might have flipped the stack from under
5303 * us, hence refresh the local variables.
5304 */
5305 cpu = smp_processor_id();
5306 rq = cpu_rq(cpu);
1da177e4
LT
5307 } else
5308 spin_unlock_irq(&rq->lock);
5309
8f4d37ec 5310 if (unlikely(reacquire_kernel_lock(current) < 0))
1da177e4 5311 goto need_resched_nonpreemptible;
8f4d37ec 5312
1da177e4 5313 preempt_enable_no_resched();
ff743345 5314 if (need_resched())
1da177e4
LT
5315 goto need_resched;
5316}
1da177e4
LT
5317EXPORT_SYMBOL(schedule);
5318
0d66bf6d
PZ
5319#ifdef CONFIG_SMP
5320/*
5321 * Look out! "owner" is an entirely speculative pointer
5322 * access and not reliable.
5323 */
5324int mutex_spin_on_owner(struct mutex *lock, struct thread_info *owner)
5325{
5326 unsigned int cpu;
5327 struct rq *rq;
5328
5329 if (!sched_feat(OWNER_SPIN))
5330 return 0;
5331
5332#ifdef CONFIG_DEBUG_PAGEALLOC
5333 /*
5334 * Need to access the cpu field knowing that
5335 * DEBUG_PAGEALLOC could have unmapped it if
5336 * the mutex owner just released it and exited.
5337 */
5338 if (probe_kernel_address(&owner->cpu, cpu))
5339 goto out;
5340#else
5341 cpu = owner->cpu;
5342#endif
5343
5344 /*
5345 * Even if the access succeeded (likely case),
5346 * the cpu field may no longer be valid.
5347 */
5348 if (cpu >= nr_cpumask_bits)
5349 goto out;
5350
5351 /*
5352 * We need to validate that we can do a
5353 * get_cpu() and that we have the percpu area.
5354 */
5355 if (!cpu_online(cpu))
5356 goto out;
5357
5358 rq = cpu_rq(cpu);
5359
5360 for (;;) {
5361 /*
5362 * Owner changed, break to re-assess state.
5363 */
5364 if (lock->owner != owner)
5365 break;
5366
5367 /*
5368 * Is that owner really running on that cpu?
5369 */
5370 if (task_thread_info(rq->curr) != owner || need_resched())
5371 return 0;
5372
5373 cpu_relax();
5374 }
5375out:
5376 return 1;
5377}
5378#endif
5379
1da177e4
LT
5380#ifdef CONFIG_PREEMPT
5381/*
2ed6e34f 5382 * this is the entry point to schedule() from in-kernel preemption
41a2d6cf 5383 * off of preempt_enable. Kernel preemptions off return from interrupt
1da177e4
LT
5384 * occur there and call schedule directly.
5385 */
5386asmlinkage void __sched preempt_schedule(void)
5387{
5388 struct thread_info *ti = current_thread_info();
6478d880 5389
1da177e4
LT
5390 /*
5391 * If there is a non-zero preempt_count or interrupts are disabled,
41a2d6cf 5392 * we do not want to preempt the current task. Just return..
1da177e4 5393 */
beed33a8 5394 if (likely(ti->preempt_count || irqs_disabled()))
1da177e4
LT
5395 return;
5396
3a5c359a
AK
5397 do {
5398 add_preempt_count(PREEMPT_ACTIVE);
3a5c359a 5399 schedule();
3a5c359a 5400 sub_preempt_count(PREEMPT_ACTIVE);
1da177e4 5401
3a5c359a
AK
5402 /*
5403 * Check again in case we missed a preemption opportunity
5404 * between schedule and now.
5405 */
5406 barrier();
5ed0cec0 5407 } while (need_resched());
1da177e4 5408}
1da177e4
LT
5409EXPORT_SYMBOL(preempt_schedule);
5410
5411/*
2ed6e34f 5412 * this is the entry point to schedule() from kernel preemption
1da177e4
LT
5413 * off of irq context.
5414 * Note, that this is called and return with irqs disabled. This will
5415 * protect us against recursive calling from irq.
5416 */
5417asmlinkage void __sched preempt_schedule_irq(void)
5418{
5419 struct thread_info *ti = current_thread_info();
6478d880 5420
2ed6e34f 5421 /* Catch callers which need to be fixed */
1da177e4
LT
5422 BUG_ON(ti->preempt_count || !irqs_disabled());
5423
3a5c359a
AK
5424 do {
5425 add_preempt_count(PREEMPT_ACTIVE);
3a5c359a
AK
5426 local_irq_enable();
5427 schedule();
5428 local_irq_disable();
3a5c359a 5429 sub_preempt_count(PREEMPT_ACTIVE);
1da177e4 5430
3a5c359a
AK
5431 /*
5432 * Check again in case we missed a preemption opportunity
5433 * between schedule and now.
5434 */
5435 barrier();
5ed0cec0 5436 } while (need_resched());
1da177e4
LT
5437}
5438
5439#endif /* CONFIG_PREEMPT */
5440
95cdf3b7
IM
5441int default_wake_function(wait_queue_t *curr, unsigned mode, int sync,
5442 void *key)
1da177e4 5443{
48f24c4d 5444 return try_to_wake_up(curr->private, mode, sync);
1da177e4 5445}
1da177e4
LT
5446EXPORT_SYMBOL(default_wake_function);
5447
5448/*
41a2d6cf
IM
5449 * The core wakeup function. Non-exclusive wakeups (nr_exclusive == 0) just
5450 * wake everything up. If it's an exclusive wakeup (nr_exclusive == small +ve
1da177e4
LT
5451 * number) then we wake all the non-exclusive tasks and one exclusive task.
5452 *
5453 * There are circumstances in which we can try to wake a task which has already
41a2d6cf 5454 * started to run but is not in state TASK_RUNNING. try_to_wake_up() returns
1da177e4
LT
5455 * zero in this (rare) case, and we handle it by continuing to scan the queue.
5456 */
78ddb08f 5457static void __wake_up_common(wait_queue_head_t *q, unsigned int mode,
777c6c5f 5458 int nr_exclusive, int sync, void *key)
1da177e4 5459{
2e45874c 5460 wait_queue_t *curr, *next;
1da177e4 5461
2e45874c 5462 list_for_each_entry_safe(curr, next, &q->task_list, task_list) {
48f24c4d
IM
5463 unsigned flags = curr->flags;
5464
1da177e4 5465 if (curr->func(curr, mode, sync, key) &&
48f24c4d 5466 (flags & WQ_FLAG_EXCLUSIVE) && !--nr_exclusive)
1da177e4
LT
5467 break;
5468 }
5469}
5470
5471/**
5472 * __wake_up - wake up threads blocked on a waitqueue.
5473 * @q: the waitqueue
5474 * @mode: which threads
5475 * @nr_exclusive: how many wake-one or wake-many threads to wake up
67be2dd1 5476 * @key: is directly passed to the wakeup function
50fa610a
DH
5477 *
5478 * It may be assumed that this function implies a write memory barrier before
5479 * changing the task state if and only if any tasks are woken up.
1da177e4 5480 */
7ad5b3a5 5481void __wake_up(wait_queue_head_t *q, unsigned int mode,
95cdf3b7 5482 int nr_exclusive, void *key)
1da177e4
LT
5483{
5484 unsigned long flags;
5485
5486 spin_lock_irqsave(&q->lock, flags);
5487 __wake_up_common(q, mode, nr_exclusive, 0, key);
5488 spin_unlock_irqrestore(&q->lock, flags);
5489}
1da177e4
LT
5490EXPORT_SYMBOL(__wake_up);
5491
5492/*
5493 * Same as __wake_up but called with the spinlock in wait_queue_head_t held.
5494 */
7ad5b3a5 5495void __wake_up_locked(wait_queue_head_t *q, unsigned int mode)
1da177e4
LT
5496{
5497 __wake_up_common(q, mode, 1, 0, NULL);
5498}
5499
4ede816a
DL
5500void __wake_up_locked_key(wait_queue_head_t *q, unsigned int mode, void *key)
5501{
5502 __wake_up_common(q, mode, 1, 0, key);
5503}
5504
1da177e4 5505/**
4ede816a 5506 * __wake_up_sync_key - wake up threads blocked on a waitqueue.
1da177e4
LT
5507 * @q: the waitqueue
5508 * @mode: which threads
5509 * @nr_exclusive: how many wake-one or wake-many threads to wake up
4ede816a 5510 * @key: opaque value to be passed to wakeup targets
1da177e4
LT
5511 *
5512 * The sync wakeup differs that the waker knows that it will schedule
5513 * away soon, so while the target thread will be woken up, it will not
5514 * be migrated to another CPU - ie. the two threads are 'synchronized'
5515 * with each other. This can prevent needless bouncing between CPUs.
5516 *
5517 * On UP it can prevent extra preemption.
50fa610a
DH
5518 *
5519 * It may be assumed that this function implies a write memory barrier before
5520 * changing the task state if and only if any tasks are woken up.
1da177e4 5521 */
4ede816a
DL
5522void __wake_up_sync_key(wait_queue_head_t *q, unsigned int mode,
5523 int nr_exclusive, void *key)
1da177e4
LT
5524{
5525 unsigned long flags;
5526 int sync = 1;
5527
5528 if (unlikely(!q))
5529 return;
5530
5531 if (unlikely(!nr_exclusive))
5532 sync = 0;
5533
5534 spin_lock_irqsave(&q->lock, flags);
4ede816a 5535 __wake_up_common(q, mode, nr_exclusive, sync, key);
1da177e4
LT
5536 spin_unlock_irqrestore(&q->lock, flags);
5537}
4ede816a
DL
5538EXPORT_SYMBOL_GPL(__wake_up_sync_key);
5539
5540/*
5541 * __wake_up_sync - see __wake_up_sync_key()
5542 */
5543void __wake_up_sync(wait_queue_head_t *q, unsigned int mode, int nr_exclusive)
5544{
5545 __wake_up_sync_key(q, mode, nr_exclusive, NULL);
5546}
1da177e4
LT
5547EXPORT_SYMBOL_GPL(__wake_up_sync); /* For internal use only */
5548
65eb3dc6
KD
5549/**
5550 * complete: - signals a single thread waiting on this completion
5551 * @x: holds the state of this particular completion
5552 *
5553 * This will wake up a single thread waiting on this completion. Threads will be
5554 * awakened in the same order in which they were queued.
5555 *
5556 * See also complete_all(), wait_for_completion() and related routines.
50fa610a
DH
5557 *
5558 * It may be assumed that this function implies a write memory barrier before
5559 * changing the task state if and only if any tasks are woken up.
65eb3dc6 5560 */
b15136e9 5561void complete(struct completion *x)
1da177e4
LT
5562{
5563 unsigned long flags;
5564
5565 spin_lock_irqsave(&x->wait.lock, flags);
5566 x->done++;
d9514f6c 5567 __wake_up_common(&x->wait, TASK_NORMAL, 1, 0, NULL);
1da177e4
LT
5568 spin_unlock_irqrestore(&x->wait.lock, flags);
5569}
5570EXPORT_SYMBOL(complete);
5571
65eb3dc6
KD
5572/**
5573 * complete_all: - signals all threads waiting on this completion
5574 * @x: holds the state of this particular completion
5575 *
5576 * This will wake up all threads waiting on this particular completion event.
50fa610a
DH
5577 *
5578 * It may be assumed that this function implies a write memory barrier before
5579 * changing the task state if and only if any tasks are woken up.
65eb3dc6 5580 */
b15136e9 5581void complete_all(struct completion *x)
1da177e4
LT
5582{
5583 unsigned long flags;
5584
5585 spin_lock_irqsave(&x->wait.lock, flags);
5586 x->done += UINT_MAX/2;
d9514f6c 5587 __wake_up_common(&x->wait, TASK_NORMAL, 0, 0, NULL);
1da177e4
LT
5588 spin_unlock_irqrestore(&x->wait.lock, flags);
5589}
5590EXPORT_SYMBOL(complete_all);
5591
8cbbe86d
AK
5592static inline long __sched
5593do_wait_for_common(struct completion *x, long timeout, int state)
1da177e4 5594{
1da177e4
LT
5595 if (!x->done) {
5596 DECLARE_WAITQUEUE(wait, current);
5597
5598 wait.flags |= WQ_FLAG_EXCLUSIVE;
5599 __add_wait_queue_tail(&x->wait, &wait);
5600 do {
94d3d824 5601 if (signal_pending_state(state, current)) {
ea71a546
ON
5602 timeout = -ERESTARTSYS;
5603 break;
8cbbe86d
AK
5604 }
5605 __set_current_state(state);
1da177e4
LT
5606 spin_unlock_irq(&x->wait.lock);
5607 timeout = schedule_timeout(timeout);
5608 spin_lock_irq(&x->wait.lock);
ea71a546 5609 } while (!x->done && timeout);
1da177e4 5610 __remove_wait_queue(&x->wait, &wait);
ea71a546
ON
5611 if (!x->done)
5612 return timeout;
1da177e4
LT
5613 }
5614 x->done--;
ea71a546 5615 return timeout ?: 1;
1da177e4 5616}
1da177e4 5617
8cbbe86d
AK
5618static long __sched
5619wait_for_common(struct completion *x, long timeout, int state)
1da177e4 5620{
1da177e4
LT
5621 might_sleep();
5622
5623 spin_lock_irq(&x->wait.lock);
8cbbe86d 5624 timeout = do_wait_for_common(x, timeout, state);
1da177e4 5625 spin_unlock_irq(&x->wait.lock);
8cbbe86d
AK
5626 return timeout;
5627}
1da177e4 5628
65eb3dc6
KD
5629/**
5630 * wait_for_completion: - waits for completion of a task
5631 * @x: holds the state of this particular completion
5632 *
5633 * This waits to be signaled for completion of a specific task. It is NOT
5634 * interruptible and there is no timeout.
5635 *
5636 * See also similar routines (i.e. wait_for_completion_timeout()) with timeout
5637 * and interrupt capability. Also see complete().
5638 */
b15136e9 5639void __sched wait_for_completion(struct completion *x)
8cbbe86d
AK
5640{
5641 wait_for_common(x, MAX_SCHEDULE_TIMEOUT, TASK_UNINTERRUPTIBLE);
1da177e4 5642}
8cbbe86d 5643EXPORT_SYMBOL(wait_for_completion);
1da177e4 5644
65eb3dc6
KD
5645/**
5646 * wait_for_completion_timeout: - waits for completion of a task (w/timeout)
5647 * @x: holds the state of this particular completion
5648 * @timeout: timeout value in jiffies
5649 *
5650 * This waits for either a completion of a specific task to be signaled or for a
5651 * specified timeout to expire. The timeout is in jiffies. It is not
5652 * interruptible.
5653 */
b15136e9 5654unsigned long __sched
8cbbe86d 5655wait_for_completion_timeout(struct completion *x, unsigned long timeout)
1da177e4 5656{
8cbbe86d 5657 return wait_for_common(x, timeout, TASK_UNINTERRUPTIBLE);
1da177e4 5658}
8cbbe86d 5659EXPORT_SYMBOL(wait_for_completion_timeout);
1da177e4 5660
65eb3dc6
KD
5661/**
5662 * wait_for_completion_interruptible: - waits for completion of a task (w/intr)
5663 * @x: holds the state of this particular completion
5664 *
5665 * This waits for completion of a specific task to be signaled. It is
5666 * interruptible.
5667 */
8cbbe86d 5668int __sched wait_for_completion_interruptible(struct completion *x)
0fec171c 5669{
51e97990
AK
5670 long t = wait_for_common(x, MAX_SCHEDULE_TIMEOUT, TASK_INTERRUPTIBLE);
5671 if (t == -ERESTARTSYS)
5672 return t;
5673 return 0;
0fec171c 5674}
8cbbe86d 5675EXPORT_SYMBOL(wait_for_completion_interruptible);
1da177e4 5676
65eb3dc6
KD
5677/**
5678 * wait_for_completion_interruptible_timeout: - waits for completion (w/(to,intr))
5679 * @x: holds the state of this particular completion
5680 * @timeout: timeout value in jiffies
5681 *
5682 * This waits for either a completion of a specific task to be signaled or for a
5683 * specified timeout to expire. It is interruptible. The timeout is in jiffies.
5684 */
b15136e9 5685unsigned long __sched
8cbbe86d
AK
5686wait_for_completion_interruptible_timeout(struct completion *x,
5687 unsigned long timeout)
0fec171c 5688{
8cbbe86d 5689 return wait_for_common(x, timeout, TASK_INTERRUPTIBLE);
0fec171c 5690}
8cbbe86d 5691EXPORT_SYMBOL(wait_for_completion_interruptible_timeout);
1da177e4 5692
65eb3dc6
KD
5693/**
5694 * wait_for_completion_killable: - waits for completion of a task (killable)
5695 * @x: holds the state of this particular completion
5696 *
5697 * This waits to be signaled for completion of a specific task. It can be
5698 * interrupted by a kill signal.
5699 */
009e577e
MW
5700int __sched wait_for_completion_killable(struct completion *x)
5701{
5702 long t = wait_for_common(x, MAX_SCHEDULE_TIMEOUT, TASK_KILLABLE);
5703 if (t == -ERESTARTSYS)
5704 return t;
5705 return 0;
5706}
5707EXPORT_SYMBOL(wait_for_completion_killable);
5708
be4de352
DC
5709/**
5710 * try_wait_for_completion - try to decrement a completion without blocking
5711 * @x: completion structure
5712 *
5713 * Returns: 0 if a decrement cannot be done without blocking
5714 * 1 if a decrement succeeded.
5715 *
5716 * If a completion is being used as a counting completion,
5717 * attempt to decrement the counter without blocking. This
5718 * enables us to avoid waiting if the resource the completion
5719 * is protecting is not available.
5720 */
5721bool try_wait_for_completion(struct completion *x)
5722{
5723 int ret = 1;
5724
5725 spin_lock_irq(&x->wait.lock);
5726 if (!x->done)
5727 ret = 0;
5728 else
5729 x->done--;
5730 spin_unlock_irq(&x->wait.lock);
5731 return ret;
5732}
5733EXPORT_SYMBOL(try_wait_for_completion);
5734
5735/**
5736 * completion_done - Test to see if a completion has any waiters
5737 * @x: completion structure
5738 *
5739 * Returns: 0 if there are waiters (wait_for_completion() in progress)
5740 * 1 if there are no waiters.
5741 *
5742 */
5743bool completion_done(struct completion *x)
5744{
5745 int ret = 1;
5746
5747 spin_lock_irq(&x->wait.lock);
5748 if (!x->done)
5749 ret = 0;
5750 spin_unlock_irq(&x->wait.lock);
5751 return ret;
5752}
5753EXPORT_SYMBOL(completion_done);
5754
8cbbe86d
AK
5755static long __sched
5756sleep_on_common(wait_queue_head_t *q, int state, long timeout)
1da177e4 5757{
0fec171c
IM
5758 unsigned long flags;
5759 wait_queue_t wait;
5760
5761 init_waitqueue_entry(&wait, current);
1da177e4 5762
8cbbe86d 5763 __set_current_state(state);
1da177e4 5764
8cbbe86d
AK
5765 spin_lock_irqsave(&q->lock, flags);
5766 __add_wait_queue(q, &wait);
5767 spin_unlock(&q->lock);
5768 timeout = schedule_timeout(timeout);
5769 spin_lock_irq(&q->lock);
5770 __remove_wait_queue(q, &wait);
5771 spin_unlock_irqrestore(&q->lock, flags);
5772
5773 return timeout;
5774}
5775
5776void __sched interruptible_sleep_on(wait_queue_head_t *q)
5777{
5778 sleep_on_common(q, TASK_INTERRUPTIBLE, MAX_SCHEDULE_TIMEOUT);
1da177e4 5779}
1da177e4
LT
5780EXPORT_SYMBOL(interruptible_sleep_on);
5781
0fec171c 5782long __sched
95cdf3b7 5783interruptible_sleep_on_timeout(wait_queue_head_t *q, long timeout)
1da177e4 5784{
8cbbe86d 5785 return sleep_on_common(q, TASK_INTERRUPTIBLE, timeout);
1da177e4 5786}
1da177e4
LT
5787EXPORT_SYMBOL(interruptible_sleep_on_timeout);
5788
0fec171c 5789void __sched sleep_on(wait_queue_head_t *q)
1da177e4 5790{
8cbbe86d 5791 sleep_on_common(q, TASK_UNINTERRUPTIBLE, MAX_SCHEDULE_TIMEOUT);
1da177e4 5792}
1da177e4
LT
5793EXPORT_SYMBOL(sleep_on);
5794
0fec171c 5795long __sched sleep_on_timeout(wait_queue_head_t *q, long timeout)
1da177e4 5796{
8cbbe86d 5797 return sleep_on_common(q, TASK_UNINTERRUPTIBLE, timeout);
1da177e4 5798}
1da177e4
LT
5799EXPORT_SYMBOL(sleep_on_timeout);
5800
b29739f9
IM
5801#ifdef CONFIG_RT_MUTEXES
5802
5803/*
5804 * rt_mutex_setprio - set the current priority of a task
5805 * @p: task
5806 * @prio: prio value (kernel-internal form)
5807 *
5808 * This function changes the 'effective' priority of a task. It does
5809 * not touch ->normal_prio like __setscheduler().
5810 *
5811 * Used by the rt_mutex code to implement priority inheritance logic.
5812 */
36c8b586 5813void rt_mutex_setprio(struct task_struct *p, int prio)
b29739f9
IM
5814{
5815 unsigned long flags;
83b699ed 5816 int oldprio, on_rq, running;
70b97a7f 5817 struct rq *rq;
cb469845 5818 const struct sched_class *prev_class = p->sched_class;
b29739f9
IM
5819
5820 BUG_ON(prio < 0 || prio > MAX_PRIO);
5821
5822 rq = task_rq_lock(p, &flags);
a8e504d2 5823 update_rq_clock(rq);
b29739f9 5824
d5f9f942 5825 oldprio = p->prio;
dd41f596 5826 on_rq = p->se.on_rq;
051a1d1a 5827 running = task_current(rq, p);
0e1f3483 5828 if (on_rq)
69be72c1 5829 dequeue_task(rq, p, 0);
0e1f3483
HS
5830 if (running)
5831 p->sched_class->put_prev_task(rq, p);
dd41f596
IM
5832
5833 if (rt_prio(prio))
5834 p->sched_class = &rt_sched_class;
5835 else
5836 p->sched_class = &fair_sched_class;
5837
b29739f9
IM
5838 p->prio = prio;
5839
0e1f3483
HS
5840 if (running)
5841 p->sched_class->set_curr_task(rq);
dd41f596 5842 if (on_rq) {
8159f87e 5843 enqueue_task(rq, p, 0);
cb469845
SR
5844
5845 check_class_changed(rq, p, prev_class, oldprio, running);
b29739f9
IM
5846 }
5847 task_rq_unlock(rq, &flags);
5848}
5849
5850#endif
5851
36c8b586 5852void set_user_nice(struct task_struct *p, long nice)
1da177e4 5853{
dd41f596 5854 int old_prio, delta, on_rq;
1da177e4 5855 unsigned long flags;
70b97a7f 5856 struct rq *rq;
1da177e4
LT
5857
5858 if (TASK_NICE(p) == nice || nice < -20 || nice > 19)
5859 return;
5860 /*
5861 * We have to be careful, if called from sys_setpriority(),
5862 * the task might be in the middle of scheduling on another CPU.
5863 */
5864 rq = task_rq_lock(p, &flags);
a8e504d2 5865 update_rq_clock(rq);
1da177e4
LT
5866 /*
5867 * The RT priorities are set via sched_setscheduler(), but we still
5868 * allow the 'normal' nice value to be set - but as expected
5869 * it wont have any effect on scheduling until the task is
dd41f596 5870 * SCHED_FIFO/SCHED_RR:
1da177e4 5871 */
e05606d3 5872 if (task_has_rt_policy(p)) {
1da177e4
LT
5873 p->static_prio = NICE_TO_PRIO(nice);
5874 goto out_unlock;
5875 }
dd41f596 5876 on_rq = p->se.on_rq;
c09595f6 5877 if (on_rq)
69be72c1 5878 dequeue_task(rq, p, 0);
1da177e4 5879
1da177e4 5880 p->static_prio = NICE_TO_PRIO(nice);
2dd73a4f 5881 set_load_weight(p);
b29739f9
IM
5882 old_prio = p->prio;
5883 p->prio = effective_prio(p);
5884 delta = p->prio - old_prio;
1da177e4 5885
dd41f596 5886 if (on_rq) {
8159f87e 5887 enqueue_task(rq, p, 0);
1da177e4 5888 /*
d5f9f942
AM
5889 * If the task increased its priority or is running and
5890 * lowered its priority, then reschedule its CPU:
1da177e4 5891 */
d5f9f942 5892 if (delta < 0 || (delta > 0 && task_running(rq, p)))
1da177e4
LT
5893 resched_task(rq->curr);
5894 }
5895out_unlock:
5896 task_rq_unlock(rq, &flags);
5897}
1da177e4
LT
5898EXPORT_SYMBOL(set_user_nice);
5899
e43379f1
MM
5900/*
5901 * can_nice - check if a task can reduce its nice value
5902 * @p: task
5903 * @nice: nice value
5904 */
36c8b586 5905int can_nice(const struct task_struct *p, const int nice)
e43379f1 5906{
024f4747
MM
5907 /* convert nice value [19,-20] to rlimit style value [1,40] */
5908 int nice_rlim = 20 - nice;
48f24c4d 5909
e43379f1
MM
5910 return (nice_rlim <= p->signal->rlim[RLIMIT_NICE].rlim_cur ||
5911 capable(CAP_SYS_NICE));
5912}
5913
1da177e4
LT
5914#ifdef __ARCH_WANT_SYS_NICE
5915
5916/*
5917 * sys_nice - change the priority of the current process.
5918 * @increment: priority increment
5919 *
5920 * sys_setpriority is a more generic, but much slower function that
5921 * does similar things.
5922 */
5add95d4 5923SYSCALL_DEFINE1(nice, int, increment)
1da177e4 5924{
48f24c4d 5925 long nice, retval;
1da177e4
LT
5926
5927 /*
5928 * Setpriority might change our priority at the same moment.
5929 * We don't have to worry. Conceptually one call occurs first
5930 * and we have a single winner.
5931 */
e43379f1
MM
5932 if (increment < -40)
5933 increment = -40;
1da177e4
LT
5934 if (increment > 40)
5935 increment = 40;
5936
2b8f836f 5937 nice = TASK_NICE(current) + increment;
1da177e4
LT
5938 if (nice < -20)
5939 nice = -20;
5940 if (nice > 19)
5941 nice = 19;
5942
e43379f1
MM
5943 if (increment < 0 && !can_nice(current, nice))
5944 return -EPERM;
5945
1da177e4
LT
5946 retval = security_task_setnice(current, nice);
5947 if (retval)
5948 return retval;
5949
5950 set_user_nice(current, nice);
5951 return 0;
5952}
5953
5954#endif
5955
5956/**
5957 * task_prio - return the priority value of a given task.
5958 * @p: the task in question.
5959 *
5960 * This is the priority value as seen by users in /proc.
5961 * RT tasks are offset by -200. Normal tasks are centered
5962 * around 0, value goes from -16 to +15.
5963 */
36c8b586 5964int task_prio(const struct task_struct *p)
1da177e4
LT
5965{
5966 return p->prio - MAX_RT_PRIO;
5967}
5968
5969/**
5970 * task_nice - return the nice value of a given task.
5971 * @p: the task in question.
5972 */
36c8b586 5973int task_nice(const struct task_struct *p)
1da177e4
LT
5974{
5975 return TASK_NICE(p);
5976}
150d8bed 5977EXPORT_SYMBOL(task_nice);
1da177e4
LT
5978
5979/**
5980 * idle_cpu - is a given cpu idle currently?
5981 * @cpu: the processor in question.
5982 */
5983int idle_cpu(int cpu)
5984{
5985 return cpu_curr(cpu) == cpu_rq(cpu)->idle;
5986}
5987
1da177e4
LT
5988/**
5989 * idle_task - return the idle task for a given cpu.
5990 * @cpu: the processor in question.
5991 */
36c8b586 5992struct task_struct *idle_task(int cpu)
1da177e4
LT
5993{
5994 return cpu_rq(cpu)->idle;
5995}
5996
5997/**
5998 * find_process_by_pid - find a process with a matching PID value.
5999 * @pid: the pid in question.
6000 */
a9957449 6001static struct task_struct *find_process_by_pid(pid_t pid)
1da177e4 6002{
228ebcbe 6003 return pid ? find_task_by_vpid(pid) : current;
1da177e4
LT
6004}
6005
6006/* Actually do priority change: must hold rq lock. */
dd41f596
IM
6007static void
6008__setscheduler(struct rq *rq, struct task_struct *p, int policy, int prio)
1da177e4 6009{
dd41f596 6010 BUG_ON(p->se.on_rq);
48f24c4d 6011
1da177e4 6012 p->policy = policy;
dd41f596
IM
6013 switch (p->policy) {
6014 case SCHED_NORMAL:
6015 case SCHED_BATCH:
6016 case SCHED_IDLE:
6017 p->sched_class = &fair_sched_class;
6018 break;
6019 case SCHED_FIFO:
6020 case SCHED_RR:
6021 p->sched_class = &rt_sched_class;
6022 break;
6023 }
6024
1da177e4 6025 p->rt_priority = prio;
b29739f9
IM
6026 p->normal_prio = normal_prio(p);
6027 /* we are holding p->pi_lock already */
6028 p->prio = rt_mutex_getprio(p);
2dd73a4f 6029 set_load_weight(p);
1da177e4
LT
6030}
6031
c69e8d9c
DH
6032/*
6033 * check the target process has a UID that matches the current process's
6034 */
6035static bool check_same_owner(struct task_struct *p)
6036{
6037 const struct cred *cred = current_cred(), *pcred;
6038 bool match;
6039
6040 rcu_read_lock();
6041 pcred = __task_cred(p);
6042 match = (cred->euid == pcred->euid ||
6043 cred->euid == pcred->uid);
6044 rcu_read_unlock();
6045 return match;
6046}
6047
961ccddd
RR
6048static int __sched_setscheduler(struct task_struct *p, int policy,
6049 struct sched_param *param, bool user)
1da177e4 6050{
83b699ed 6051 int retval, oldprio, oldpolicy = -1, on_rq, running;
1da177e4 6052 unsigned long flags;
cb469845 6053 const struct sched_class *prev_class = p->sched_class;
70b97a7f 6054 struct rq *rq;
1da177e4 6055
66e5393a
SR
6056 /* may grab non-irq protected spin_locks */
6057 BUG_ON(in_interrupt());
1da177e4
LT
6058recheck:
6059 /* double check policy once rq lock held */
6060 if (policy < 0)
6061 policy = oldpolicy = p->policy;
6062 else if (policy != SCHED_FIFO && policy != SCHED_RR &&
dd41f596
IM
6063 policy != SCHED_NORMAL && policy != SCHED_BATCH &&
6064 policy != SCHED_IDLE)
b0a9499c 6065 return -EINVAL;
1da177e4
LT
6066 /*
6067 * Valid priorities for SCHED_FIFO and SCHED_RR are
dd41f596
IM
6068 * 1..MAX_USER_RT_PRIO-1, valid priority for SCHED_NORMAL,
6069 * SCHED_BATCH and SCHED_IDLE is 0.
1da177e4
LT
6070 */
6071 if (param->sched_priority < 0 ||
95cdf3b7 6072 (p->mm && param->sched_priority > MAX_USER_RT_PRIO-1) ||
d46523ea 6073 (!p->mm && param->sched_priority > MAX_RT_PRIO-1))
1da177e4 6074 return -EINVAL;
e05606d3 6075 if (rt_policy(policy) != (param->sched_priority != 0))
1da177e4
LT
6076 return -EINVAL;
6077
37e4ab3f
OC
6078 /*
6079 * Allow unprivileged RT tasks to decrease priority:
6080 */
961ccddd 6081 if (user && !capable(CAP_SYS_NICE)) {
e05606d3 6082 if (rt_policy(policy)) {
8dc3e909 6083 unsigned long rlim_rtprio;
8dc3e909
ON
6084
6085 if (!lock_task_sighand(p, &flags))
6086 return -ESRCH;
6087 rlim_rtprio = p->signal->rlim[RLIMIT_RTPRIO].rlim_cur;
6088 unlock_task_sighand(p, &flags);
6089
6090 /* can't set/change the rt policy */
6091 if (policy != p->policy && !rlim_rtprio)
6092 return -EPERM;
6093
6094 /* can't increase priority */
6095 if (param->sched_priority > p->rt_priority &&
6096 param->sched_priority > rlim_rtprio)
6097 return -EPERM;
6098 }
dd41f596
IM
6099 /*
6100 * Like positive nice levels, dont allow tasks to
6101 * move out of SCHED_IDLE either:
6102 */
6103 if (p->policy == SCHED_IDLE && policy != SCHED_IDLE)
6104 return -EPERM;
5fe1d75f 6105
37e4ab3f 6106 /* can't change other user's priorities */
c69e8d9c 6107 if (!check_same_owner(p))
37e4ab3f
OC
6108 return -EPERM;
6109 }
1da177e4 6110
725aad24 6111 if (user) {
b68aa230 6112#ifdef CONFIG_RT_GROUP_SCHED
725aad24
JF
6113 /*
6114 * Do not allow realtime tasks into groups that have no runtime
6115 * assigned.
6116 */
9a7e0b18
PZ
6117 if (rt_bandwidth_enabled() && rt_policy(policy) &&
6118 task_group(p)->rt_bandwidth.rt_runtime == 0)
725aad24 6119 return -EPERM;
b68aa230
PZ
6120#endif
6121
725aad24
JF
6122 retval = security_task_setscheduler(p, policy, param);
6123 if (retval)
6124 return retval;
6125 }
6126
b29739f9
IM
6127 /*
6128 * make sure no PI-waiters arrive (or leave) while we are
6129 * changing the priority of the task:
6130 */
6131 spin_lock_irqsave(&p->pi_lock, flags);
1da177e4
LT
6132 /*
6133 * To be able to change p->policy safely, the apropriate
6134 * runqueue lock must be held.
6135 */
b29739f9 6136 rq = __task_rq_lock(p);
1da177e4
LT
6137 /* recheck policy now with rq lock held */
6138 if (unlikely(oldpolicy != -1 && oldpolicy != p->policy)) {
6139 policy = oldpolicy = -1;
b29739f9
IM
6140 __task_rq_unlock(rq);
6141 spin_unlock_irqrestore(&p->pi_lock, flags);
1da177e4
LT
6142 goto recheck;
6143 }
2daa3577 6144 update_rq_clock(rq);
dd41f596 6145 on_rq = p->se.on_rq;
051a1d1a 6146 running = task_current(rq, p);
0e1f3483 6147 if (on_rq)
2e1cb74a 6148 deactivate_task(rq, p, 0);
0e1f3483
HS
6149 if (running)
6150 p->sched_class->put_prev_task(rq, p);
f6b53205 6151
1da177e4 6152 oldprio = p->prio;
dd41f596 6153 __setscheduler(rq, p, policy, param->sched_priority);
f6b53205 6154
0e1f3483
HS
6155 if (running)
6156 p->sched_class->set_curr_task(rq);
dd41f596
IM
6157 if (on_rq) {
6158 activate_task(rq, p, 0);
cb469845
SR
6159
6160 check_class_changed(rq, p, prev_class, oldprio, running);
1da177e4 6161 }
b29739f9
IM
6162 __task_rq_unlock(rq);
6163 spin_unlock_irqrestore(&p->pi_lock, flags);
6164
95e02ca9
TG
6165 rt_mutex_adjust_pi(p);
6166
1da177e4
LT
6167 return 0;
6168}
961ccddd
RR
6169
6170/**
6171 * sched_setscheduler - change the scheduling policy and/or RT priority of a thread.
6172 * @p: the task in question.
6173 * @policy: new policy.
6174 * @param: structure containing the new RT priority.
6175 *
6176 * NOTE that the task may be already dead.
6177 */
6178int sched_setscheduler(struct task_struct *p, int policy,
6179 struct sched_param *param)
6180{
6181 return __sched_setscheduler(p, policy, param, true);
6182}
1da177e4
LT
6183EXPORT_SYMBOL_GPL(sched_setscheduler);
6184
961ccddd
RR
6185/**
6186 * sched_setscheduler_nocheck - change the scheduling policy and/or RT priority of a thread from kernelspace.
6187 * @p: the task in question.
6188 * @policy: new policy.
6189 * @param: structure containing the new RT priority.
6190 *
6191 * Just like sched_setscheduler, only don't bother checking if the
6192 * current context has permission. For example, this is needed in
6193 * stop_machine(): we create temporary high priority worker threads,
6194 * but our caller might not have that capability.
6195 */
6196int sched_setscheduler_nocheck(struct task_struct *p, int policy,
6197 struct sched_param *param)
6198{
6199 return __sched_setscheduler(p, policy, param, false);
6200}
6201
95cdf3b7
IM
6202static int
6203do_sched_setscheduler(pid_t pid, int policy, struct sched_param __user *param)
1da177e4 6204{
1da177e4
LT
6205 struct sched_param lparam;
6206 struct task_struct *p;
36c8b586 6207 int retval;
1da177e4
LT
6208
6209 if (!param || pid < 0)
6210 return -EINVAL;
6211 if (copy_from_user(&lparam, param, sizeof(struct sched_param)))
6212 return -EFAULT;
5fe1d75f
ON
6213
6214 rcu_read_lock();
6215 retval = -ESRCH;
1da177e4 6216 p = find_process_by_pid(pid);
5fe1d75f
ON
6217 if (p != NULL)
6218 retval = sched_setscheduler(p, policy, &lparam);
6219 rcu_read_unlock();
36c8b586 6220
1da177e4
LT
6221 return retval;
6222}
6223
6224/**
6225 * sys_sched_setscheduler - set/change the scheduler policy and RT priority
6226 * @pid: the pid in question.
6227 * @policy: new policy.
6228 * @param: structure containing the new RT priority.
6229 */
5add95d4
HC
6230SYSCALL_DEFINE3(sched_setscheduler, pid_t, pid, int, policy,
6231 struct sched_param __user *, param)
1da177e4 6232{
c21761f1
JB
6233 /* negative values for policy are not valid */
6234 if (policy < 0)
6235 return -EINVAL;
6236
1da177e4
LT
6237 return do_sched_setscheduler(pid, policy, param);
6238}
6239
6240/**
6241 * sys_sched_setparam - set/change the RT priority of a thread
6242 * @pid: the pid in question.
6243 * @param: structure containing the new RT priority.
6244 */
5add95d4 6245SYSCALL_DEFINE2(sched_setparam, pid_t, pid, struct sched_param __user *, param)
1da177e4
LT
6246{
6247 return do_sched_setscheduler(pid, -1, param);
6248}
6249
6250/**
6251 * sys_sched_getscheduler - get the policy (scheduling class) of a thread
6252 * @pid: the pid in question.
6253 */
5add95d4 6254SYSCALL_DEFINE1(sched_getscheduler, pid_t, pid)
1da177e4 6255{
36c8b586 6256 struct task_struct *p;
3a5c359a 6257 int retval;
1da177e4
LT
6258
6259 if (pid < 0)
3a5c359a 6260 return -EINVAL;
1da177e4
LT
6261
6262 retval = -ESRCH;
6263 read_lock(&tasklist_lock);
6264 p = find_process_by_pid(pid);
6265 if (p) {
6266 retval = security_task_getscheduler(p);
6267 if (!retval)
6268 retval = p->policy;
6269 }
6270 read_unlock(&tasklist_lock);
1da177e4
LT
6271 return retval;
6272}
6273
6274/**
6275 * sys_sched_getscheduler - get the RT priority of a thread
6276 * @pid: the pid in question.
6277 * @param: structure containing the RT priority.
6278 */
5add95d4 6279SYSCALL_DEFINE2(sched_getparam, pid_t, pid, struct sched_param __user *, param)
1da177e4
LT
6280{
6281 struct sched_param lp;
36c8b586 6282 struct task_struct *p;
3a5c359a 6283 int retval;
1da177e4
LT
6284
6285 if (!param || pid < 0)
3a5c359a 6286 return -EINVAL;
1da177e4
LT
6287
6288 read_lock(&tasklist_lock);
6289 p = find_process_by_pid(pid);
6290 retval = -ESRCH;
6291 if (!p)
6292 goto out_unlock;
6293
6294 retval = security_task_getscheduler(p);
6295 if (retval)
6296 goto out_unlock;
6297
6298 lp.sched_priority = p->rt_priority;
6299 read_unlock(&tasklist_lock);
6300
6301 /*
6302 * This one might sleep, we cannot do it with a spinlock held ...
6303 */
6304 retval = copy_to_user(param, &lp, sizeof(*param)) ? -EFAULT : 0;
6305
1da177e4
LT
6306 return retval;
6307
6308out_unlock:
6309 read_unlock(&tasklist_lock);
6310 return retval;
6311}
6312
96f874e2 6313long sched_setaffinity(pid_t pid, const struct cpumask *in_mask)
1da177e4 6314{
5a16f3d3 6315 cpumask_var_t cpus_allowed, new_mask;
36c8b586
IM
6316 struct task_struct *p;
6317 int retval;
1da177e4 6318
95402b38 6319 get_online_cpus();
1da177e4
LT
6320 read_lock(&tasklist_lock);
6321
6322 p = find_process_by_pid(pid);
6323 if (!p) {
6324 read_unlock(&tasklist_lock);
95402b38 6325 put_online_cpus();
1da177e4
LT
6326 return -ESRCH;
6327 }
6328
6329 /*
6330 * It is not safe to call set_cpus_allowed with the
41a2d6cf 6331 * tasklist_lock held. We will bump the task_struct's
1da177e4
LT
6332 * usage count and then drop tasklist_lock.
6333 */
6334 get_task_struct(p);
6335 read_unlock(&tasklist_lock);
6336
5a16f3d3
RR
6337 if (!alloc_cpumask_var(&cpus_allowed, GFP_KERNEL)) {
6338 retval = -ENOMEM;
6339 goto out_put_task;
6340 }
6341 if (!alloc_cpumask_var(&new_mask, GFP_KERNEL)) {
6342 retval = -ENOMEM;
6343 goto out_free_cpus_allowed;
6344 }
1da177e4 6345 retval = -EPERM;
c69e8d9c 6346 if (!check_same_owner(p) && !capable(CAP_SYS_NICE))
1da177e4
LT
6347 goto out_unlock;
6348
e7834f8f
DQ
6349 retval = security_task_setscheduler(p, 0, NULL);
6350 if (retval)
6351 goto out_unlock;
6352
5a16f3d3
RR
6353 cpuset_cpus_allowed(p, cpus_allowed);
6354 cpumask_and(new_mask, in_mask, cpus_allowed);
8707d8b8 6355 again:
5a16f3d3 6356 retval = set_cpus_allowed_ptr(p, new_mask);
1da177e4 6357
8707d8b8 6358 if (!retval) {
5a16f3d3
RR
6359 cpuset_cpus_allowed(p, cpus_allowed);
6360 if (!cpumask_subset(new_mask, cpus_allowed)) {
8707d8b8
PM
6361 /*
6362 * We must have raced with a concurrent cpuset
6363 * update. Just reset the cpus_allowed to the
6364 * cpuset's cpus_allowed
6365 */
5a16f3d3 6366 cpumask_copy(new_mask, cpus_allowed);
8707d8b8
PM
6367 goto again;
6368 }
6369 }
1da177e4 6370out_unlock:
5a16f3d3
RR
6371 free_cpumask_var(new_mask);
6372out_free_cpus_allowed:
6373 free_cpumask_var(cpus_allowed);
6374out_put_task:
1da177e4 6375 put_task_struct(p);
95402b38 6376 put_online_cpus();
1da177e4
LT
6377 return retval;
6378}
6379
6380static int get_user_cpu_mask(unsigned long __user *user_mask_ptr, unsigned len,
96f874e2 6381 struct cpumask *new_mask)
1da177e4 6382{
96f874e2
RR
6383 if (len < cpumask_size())
6384 cpumask_clear(new_mask);
6385 else if (len > cpumask_size())
6386 len = cpumask_size();
6387
1da177e4
LT
6388 return copy_from_user(new_mask, user_mask_ptr, len) ? -EFAULT : 0;
6389}
6390
6391/**
6392 * sys_sched_setaffinity - set the cpu affinity of a process
6393 * @pid: pid of the process
6394 * @len: length in bytes of the bitmask pointed to by user_mask_ptr
6395 * @user_mask_ptr: user-space pointer to the new cpu mask
6396 */
5add95d4
HC
6397SYSCALL_DEFINE3(sched_setaffinity, pid_t, pid, unsigned int, len,
6398 unsigned long __user *, user_mask_ptr)
1da177e4 6399{
5a16f3d3 6400 cpumask_var_t new_mask;
1da177e4
LT
6401 int retval;
6402
5a16f3d3
RR
6403 if (!alloc_cpumask_var(&new_mask, GFP_KERNEL))
6404 return -ENOMEM;
1da177e4 6405
5a16f3d3
RR
6406 retval = get_user_cpu_mask(user_mask_ptr, len, new_mask);
6407 if (retval == 0)
6408 retval = sched_setaffinity(pid, new_mask);
6409 free_cpumask_var(new_mask);
6410 return retval;
1da177e4
LT
6411}
6412
96f874e2 6413long sched_getaffinity(pid_t pid, struct cpumask *mask)
1da177e4 6414{
36c8b586 6415 struct task_struct *p;
1da177e4 6416 int retval;
1da177e4 6417
95402b38 6418 get_online_cpus();
1da177e4
LT
6419 read_lock(&tasklist_lock);
6420
6421 retval = -ESRCH;
6422 p = find_process_by_pid(pid);
6423 if (!p)
6424 goto out_unlock;
6425
e7834f8f
DQ
6426 retval = security_task_getscheduler(p);
6427 if (retval)
6428 goto out_unlock;
6429
96f874e2 6430 cpumask_and(mask, &p->cpus_allowed, cpu_online_mask);
1da177e4
LT
6431
6432out_unlock:
6433 read_unlock(&tasklist_lock);
95402b38 6434 put_online_cpus();
1da177e4 6435
9531b62f 6436 return retval;
1da177e4
LT
6437}
6438
6439/**
6440 * sys_sched_getaffinity - get the cpu affinity of a process
6441 * @pid: pid of the process
6442 * @len: length in bytes of the bitmask pointed to by user_mask_ptr
6443 * @user_mask_ptr: user-space pointer to hold the current cpu mask
6444 */
5add95d4
HC
6445SYSCALL_DEFINE3(sched_getaffinity, pid_t, pid, unsigned int, len,
6446 unsigned long __user *, user_mask_ptr)
1da177e4
LT
6447{
6448 int ret;
f17c8607 6449 cpumask_var_t mask;
1da177e4 6450
f17c8607 6451 if (len < cpumask_size())
1da177e4
LT
6452 return -EINVAL;
6453
f17c8607
RR
6454 if (!alloc_cpumask_var(&mask, GFP_KERNEL))
6455 return -ENOMEM;
1da177e4 6456
f17c8607
RR
6457 ret = sched_getaffinity(pid, mask);
6458 if (ret == 0) {
6459 if (copy_to_user(user_mask_ptr, mask, cpumask_size()))
6460 ret = -EFAULT;
6461 else
6462 ret = cpumask_size();
6463 }
6464 free_cpumask_var(mask);
1da177e4 6465
f17c8607 6466 return ret;
1da177e4
LT
6467}
6468
6469/**
6470 * sys_sched_yield - yield the current processor to other threads.
6471 *
dd41f596
IM
6472 * This function yields the current CPU to other tasks. If there are no
6473 * other threads running on this CPU then this function will return.
1da177e4 6474 */
5add95d4 6475SYSCALL_DEFINE0(sched_yield)
1da177e4 6476{
70b97a7f 6477 struct rq *rq = this_rq_lock();
1da177e4 6478
2d72376b 6479 schedstat_inc(rq, yld_count);
4530d7ab 6480 current->sched_class->yield_task(rq);
1da177e4
LT
6481
6482 /*
6483 * Since we are going to call schedule() anyway, there's
6484 * no need to preempt or enable interrupts:
6485 */
6486 __release(rq->lock);
8a25d5de 6487 spin_release(&rq->lock.dep_map, 1, _THIS_IP_);
1da177e4
LT
6488 _raw_spin_unlock(&rq->lock);
6489 preempt_enable_no_resched();
6490
6491 schedule();
6492
6493 return 0;
6494}
6495
e7b38404 6496static void __cond_resched(void)
1da177e4 6497{
8e0a43d8
IM
6498#ifdef CONFIG_DEBUG_SPINLOCK_SLEEP
6499 __might_sleep(__FILE__, __LINE__);
6500#endif
5bbcfd90
IM
6501 /*
6502 * The BKS might be reacquired before we have dropped
6503 * PREEMPT_ACTIVE, which could trigger a second
6504 * cond_resched() call.
6505 */
1da177e4
LT
6506 do {
6507 add_preempt_count(PREEMPT_ACTIVE);
6508 schedule();
6509 sub_preempt_count(PREEMPT_ACTIVE);
6510 } while (need_resched());
6511}
6512
02b67cc3 6513int __sched _cond_resched(void)
1da177e4 6514{
9414232f
IM
6515 if (need_resched() && !(preempt_count() & PREEMPT_ACTIVE) &&
6516 system_state == SYSTEM_RUNNING) {
1da177e4
LT
6517 __cond_resched();
6518 return 1;
6519 }
6520 return 0;
6521}
02b67cc3 6522EXPORT_SYMBOL(_cond_resched);
1da177e4
LT
6523
6524/*
6525 * cond_resched_lock() - if a reschedule is pending, drop the given lock,
6526 * call schedule, and on return reacquire the lock.
6527 *
41a2d6cf 6528 * This works OK both with and without CONFIG_PREEMPT. We do strange low-level
1da177e4
LT
6529 * operations here to prevent schedule() from being called twice (once via
6530 * spin_unlock(), once by hand).
6531 */
95cdf3b7 6532int cond_resched_lock(spinlock_t *lock)
1da177e4 6533{
95c354fe 6534 int resched = need_resched() && system_state == SYSTEM_RUNNING;
6df3cecb
JK
6535 int ret = 0;
6536
95c354fe 6537 if (spin_needbreak(lock) || resched) {
1da177e4 6538 spin_unlock(lock);
95c354fe
NP
6539 if (resched && need_resched())
6540 __cond_resched();
6541 else
6542 cpu_relax();
6df3cecb 6543 ret = 1;
1da177e4 6544 spin_lock(lock);
1da177e4 6545 }
6df3cecb 6546 return ret;
1da177e4 6547}
1da177e4
LT
6548EXPORT_SYMBOL(cond_resched_lock);
6549
6550int __sched cond_resched_softirq(void)
6551{
6552 BUG_ON(!in_softirq());
6553
9414232f 6554 if (need_resched() && system_state == SYSTEM_RUNNING) {
98d82567 6555 local_bh_enable();
1da177e4
LT
6556 __cond_resched();
6557 local_bh_disable();
6558 return 1;
6559 }
6560 return 0;
6561}
1da177e4
LT
6562EXPORT_SYMBOL(cond_resched_softirq);
6563
1da177e4
LT
6564/**
6565 * yield - yield the current processor to other threads.
6566 *
72fd4a35 6567 * This is a shortcut for kernel-space yielding - it marks the
1da177e4
LT
6568 * thread runnable and calls sys_sched_yield().
6569 */
6570void __sched yield(void)
6571{
6572 set_current_state(TASK_RUNNING);
6573 sys_sched_yield();
6574}
1da177e4
LT
6575EXPORT_SYMBOL(yield);
6576
6577/*
41a2d6cf 6578 * This task is about to go to sleep on IO. Increment rq->nr_iowait so
1da177e4
LT
6579 * that process accounting knows that this is a task in IO wait state.
6580 *
6581 * But don't do that if it is a deliberate, throttling IO wait (this task
6582 * has set its backing_dev_info: the queue against which it should throttle)
6583 */
6584void __sched io_schedule(void)
6585{
70b97a7f 6586 struct rq *rq = &__raw_get_cpu_var(runqueues);
1da177e4 6587
0ff92245 6588 delayacct_blkio_start();
1da177e4
LT
6589 atomic_inc(&rq->nr_iowait);
6590 schedule();
6591 atomic_dec(&rq->nr_iowait);
0ff92245 6592 delayacct_blkio_end();
1da177e4 6593}
1da177e4
LT
6594EXPORT_SYMBOL(io_schedule);
6595
6596long __sched io_schedule_timeout(long timeout)
6597{
70b97a7f 6598 struct rq *rq = &__raw_get_cpu_var(runqueues);
1da177e4
LT
6599 long ret;
6600
0ff92245 6601 delayacct_blkio_start();
1da177e4
LT
6602 atomic_inc(&rq->nr_iowait);
6603 ret = schedule_timeout(timeout);
6604 atomic_dec(&rq->nr_iowait);
0ff92245 6605 delayacct_blkio_end();
1da177e4
LT
6606 return ret;
6607}
6608
6609/**
6610 * sys_sched_get_priority_max - return maximum RT priority.
6611 * @policy: scheduling class.
6612 *
6613 * this syscall returns the maximum rt_priority that can be used
6614 * by a given scheduling class.
6615 */
5add95d4 6616SYSCALL_DEFINE1(sched_get_priority_max, int, policy)
1da177e4
LT
6617{
6618 int ret = -EINVAL;
6619
6620 switch (policy) {
6621 case SCHED_FIFO:
6622 case SCHED_RR:
6623 ret = MAX_USER_RT_PRIO-1;
6624 break;
6625 case SCHED_NORMAL:
b0a9499c 6626 case SCHED_BATCH:
dd41f596 6627 case SCHED_IDLE:
1da177e4
LT
6628 ret = 0;
6629 break;
6630 }
6631 return ret;
6632}
6633
6634/**
6635 * sys_sched_get_priority_min - return minimum RT priority.
6636 * @policy: scheduling class.
6637 *
6638 * this syscall returns the minimum rt_priority that can be used
6639 * by a given scheduling class.
6640 */
5add95d4 6641SYSCALL_DEFINE1(sched_get_priority_min, int, policy)
1da177e4
LT
6642{
6643 int ret = -EINVAL;
6644
6645 switch (policy) {
6646 case SCHED_FIFO:
6647 case SCHED_RR:
6648 ret = 1;
6649 break;
6650 case SCHED_NORMAL:
b0a9499c 6651 case SCHED_BATCH:
dd41f596 6652 case SCHED_IDLE:
1da177e4
LT
6653 ret = 0;
6654 }
6655 return ret;
6656}
6657
6658/**
6659 * sys_sched_rr_get_interval - return the default timeslice of a process.
6660 * @pid: pid of the process.
6661 * @interval: userspace pointer to the timeslice value.
6662 *
6663 * this syscall writes the default timeslice value of a given process
6664 * into the user-space timespec buffer. A value of '0' means infinity.
6665 */
17da2bd9 6666SYSCALL_DEFINE2(sched_rr_get_interval, pid_t, pid,
754fe8d2 6667 struct timespec __user *, interval)
1da177e4 6668{
36c8b586 6669 struct task_struct *p;
a4ec24b4 6670 unsigned int time_slice;
3a5c359a 6671 int retval;
1da177e4 6672 struct timespec t;
1da177e4
LT
6673
6674 if (pid < 0)
3a5c359a 6675 return -EINVAL;
1da177e4
LT
6676
6677 retval = -ESRCH;
6678 read_lock(&tasklist_lock);
6679 p = find_process_by_pid(pid);
6680 if (!p)
6681 goto out_unlock;
6682
6683 retval = security_task_getscheduler(p);
6684 if (retval)
6685 goto out_unlock;
6686
77034937
IM
6687 /*
6688 * Time slice is 0 for SCHED_FIFO tasks and for SCHED_OTHER
6689 * tasks that are on an otherwise idle runqueue:
6690 */
6691 time_slice = 0;
6692 if (p->policy == SCHED_RR) {
a4ec24b4 6693 time_slice = DEF_TIMESLICE;
1868f958 6694 } else if (p->policy != SCHED_FIFO) {
a4ec24b4
DA
6695 struct sched_entity *se = &p->se;
6696 unsigned long flags;
6697 struct rq *rq;
6698
6699 rq = task_rq_lock(p, &flags);
77034937
IM
6700 if (rq->cfs.load.weight)
6701 time_slice = NS_TO_JIFFIES(sched_slice(&rq->cfs, se));
a4ec24b4
DA
6702 task_rq_unlock(rq, &flags);
6703 }
1da177e4 6704 read_unlock(&tasklist_lock);
a4ec24b4 6705 jiffies_to_timespec(time_slice, &t);
1da177e4 6706 retval = copy_to_user(interval, &t, sizeof(t)) ? -EFAULT : 0;
1da177e4 6707 return retval;
3a5c359a 6708
1da177e4
LT
6709out_unlock:
6710 read_unlock(&tasklist_lock);
6711 return retval;
6712}
6713
7c731e0a 6714static const char stat_nam[] = TASK_STATE_TO_CHAR_STR;
36c8b586 6715
82a1fcb9 6716void sched_show_task(struct task_struct *p)
1da177e4 6717{
1da177e4 6718 unsigned long free = 0;
36c8b586 6719 unsigned state;
1da177e4 6720
1da177e4 6721 state = p->state ? __ffs(p->state) + 1 : 0;
cc4ea795 6722 printk(KERN_INFO "%-13.13s %c", p->comm,
2ed6e34f 6723 state < sizeof(stat_nam) - 1 ? stat_nam[state] : '?');
4bd77321 6724#if BITS_PER_LONG == 32
1da177e4 6725 if (state == TASK_RUNNING)
cc4ea795 6726 printk(KERN_CONT " running ");
1da177e4 6727 else
cc4ea795 6728 printk(KERN_CONT " %08lx ", thread_saved_pc(p));
1da177e4
LT
6729#else
6730 if (state == TASK_RUNNING)
cc4ea795 6731 printk(KERN_CONT " running task ");
1da177e4 6732 else
cc4ea795 6733 printk(KERN_CONT " %016lx ", thread_saved_pc(p));
1da177e4
LT
6734#endif
6735#ifdef CONFIG_DEBUG_STACK_USAGE
7c9f8861 6736 free = stack_not_used(p);
1da177e4 6737#endif
aa47b7e0
DR
6738 printk(KERN_CONT "%5lu %5d %6d 0x%08lx\n", free,
6739 task_pid_nr(p), task_pid_nr(p->real_parent),
6740 (unsigned long)task_thread_info(p)->flags);
1da177e4 6741
5fb5e6de 6742 show_stack(p, NULL);
1da177e4
LT
6743}
6744
e59e2ae2 6745void show_state_filter(unsigned long state_filter)
1da177e4 6746{
36c8b586 6747 struct task_struct *g, *p;
1da177e4 6748
4bd77321
IM
6749#if BITS_PER_LONG == 32
6750 printk(KERN_INFO
6751 " task PC stack pid father\n");
1da177e4 6752#else
4bd77321
IM
6753 printk(KERN_INFO
6754 " task PC stack pid father\n");
1da177e4
LT
6755#endif
6756 read_lock(&tasklist_lock);
6757 do_each_thread(g, p) {
6758 /*
6759 * reset the NMI-timeout, listing all files on a slow
6760 * console might take alot of time:
6761 */
6762 touch_nmi_watchdog();
39bc89fd 6763 if (!state_filter || (p->state & state_filter))
82a1fcb9 6764 sched_show_task(p);
1da177e4
LT
6765 } while_each_thread(g, p);
6766
04c9167f
JF
6767 touch_all_softlockup_watchdogs();
6768
dd41f596
IM
6769#ifdef CONFIG_SCHED_DEBUG
6770 sysrq_sched_debug_show();
6771#endif
1da177e4 6772 read_unlock(&tasklist_lock);
e59e2ae2
IM
6773 /*
6774 * Only show locks if all tasks are dumped:
6775 */
6776 if (state_filter == -1)
6777 debug_show_all_locks();
1da177e4
LT
6778}
6779
1df21055
IM
6780void __cpuinit init_idle_bootup_task(struct task_struct *idle)
6781{
dd41f596 6782 idle->sched_class = &idle_sched_class;
1df21055
IM
6783}
6784
f340c0d1
IM
6785/**
6786 * init_idle - set up an idle thread for a given CPU
6787 * @idle: task in question
6788 * @cpu: cpu the idle task belongs to
6789 *
6790 * NOTE: this function does not set the idle thread's NEED_RESCHED
6791 * flag, to make booting more robust.
6792 */
5c1e1767 6793void __cpuinit init_idle(struct task_struct *idle, int cpu)
1da177e4 6794{
70b97a7f 6795 struct rq *rq = cpu_rq(cpu);
1da177e4
LT
6796 unsigned long flags;
6797
5cbd54ef
IM
6798 spin_lock_irqsave(&rq->lock, flags);
6799
dd41f596
IM
6800 __sched_fork(idle);
6801 idle->se.exec_start = sched_clock();
6802
b29739f9 6803 idle->prio = idle->normal_prio = MAX_PRIO;
96f874e2 6804 cpumask_copy(&idle->cpus_allowed, cpumask_of(cpu));
dd41f596 6805 __set_task_cpu(idle, cpu);
1da177e4 6806
1da177e4 6807 rq->curr = rq->idle = idle;
4866cde0
NP
6808#if defined(CONFIG_SMP) && defined(__ARCH_WANT_UNLOCKED_CTXSW)
6809 idle->oncpu = 1;
6810#endif
1da177e4
LT
6811 spin_unlock_irqrestore(&rq->lock, flags);
6812
6813 /* Set the preempt count _outside_ the spinlocks! */
8e3e076c
LT
6814#if defined(CONFIG_PREEMPT)
6815 task_thread_info(idle)->preempt_count = (idle->lock_depth >= 0);
6816#else
a1261f54 6817 task_thread_info(idle)->preempt_count = 0;
8e3e076c 6818#endif
dd41f596
IM
6819 /*
6820 * The idle tasks have their own, simple scheduling class:
6821 */
6822 idle->sched_class = &idle_sched_class;
fb52607a 6823 ftrace_graph_init_task(idle);
1da177e4
LT
6824}
6825
6826/*
6827 * In a system that switches off the HZ timer nohz_cpu_mask
6828 * indicates which cpus entered this state. This is used
6829 * in the rcu update to wait only for active cpus. For system
6830 * which do not switch off the HZ timer nohz_cpu_mask should
6a7b3dc3 6831 * always be CPU_BITS_NONE.
1da177e4 6832 */
6a7b3dc3 6833cpumask_var_t nohz_cpu_mask;
1da177e4 6834
19978ca6
IM
6835/*
6836 * Increase the granularity value when there are more CPUs,
6837 * because with more CPUs the 'effective latency' as visible
6838 * to users decreases. But the relationship is not linear,
6839 * so pick a second-best guess by going with the log2 of the
6840 * number of CPUs.
6841 *
6842 * This idea comes from the SD scheduler of Con Kolivas:
6843 */
6844static inline void sched_init_granularity(void)
6845{
6846 unsigned int factor = 1 + ilog2(num_online_cpus());
6847 const unsigned long limit = 200000000;
6848
6849 sysctl_sched_min_granularity *= factor;
6850 if (sysctl_sched_min_granularity > limit)
6851 sysctl_sched_min_granularity = limit;
6852
6853 sysctl_sched_latency *= factor;
6854 if (sysctl_sched_latency > limit)
6855 sysctl_sched_latency = limit;
6856
6857 sysctl_sched_wakeup_granularity *= factor;
55cd5340
PZ
6858
6859 sysctl_sched_shares_ratelimit *= factor;
19978ca6
IM
6860}
6861
1da177e4
LT
6862#ifdef CONFIG_SMP
6863/*
6864 * This is how migration works:
6865 *
70b97a7f 6866 * 1) we queue a struct migration_req structure in the source CPU's
1da177e4
LT
6867 * runqueue and wake up that CPU's migration thread.
6868 * 2) we down() the locked semaphore => thread blocks.
6869 * 3) migration thread wakes up (implicitly it forces the migrated
6870 * thread off the CPU)
6871 * 4) it gets the migration request and checks whether the migrated
6872 * task is still in the wrong runqueue.
6873 * 5) if it's in the wrong runqueue then the migration thread removes
6874 * it and puts it into the right queue.
6875 * 6) migration thread up()s the semaphore.
6876 * 7) we wake up and the migration is done.
6877 */
6878
6879/*
6880 * Change a given task's CPU affinity. Migrate the thread to a
6881 * proper CPU and schedule it away if the CPU it's executing on
6882 * is removed from the allowed bitmask.
6883 *
6884 * NOTE: the caller must have a valid reference to the task, the
41a2d6cf 6885 * task must not exit() & deallocate itself prematurely. The
1da177e4
LT
6886 * call is not atomic; no spinlocks may be held.
6887 */
96f874e2 6888int set_cpus_allowed_ptr(struct task_struct *p, const struct cpumask *new_mask)
1da177e4 6889{
70b97a7f 6890 struct migration_req req;
1da177e4 6891 unsigned long flags;
70b97a7f 6892 struct rq *rq;
48f24c4d 6893 int ret = 0;
1da177e4
LT
6894
6895 rq = task_rq_lock(p, &flags);
96f874e2 6896 if (!cpumask_intersects(new_mask, cpu_online_mask)) {
1da177e4
LT
6897 ret = -EINVAL;
6898 goto out;
6899 }
6900
9985b0ba 6901 if (unlikely((p->flags & PF_THREAD_BOUND) && p != current &&
96f874e2 6902 !cpumask_equal(&p->cpus_allowed, new_mask))) {
9985b0ba
DR
6903 ret = -EINVAL;
6904 goto out;
6905 }
6906
73fe6aae 6907 if (p->sched_class->set_cpus_allowed)
cd8ba7cd 6908 p->sched_class->set_cpus_allowed(p, new_mask);
73fe6aae 6909 else {
96f874e2
RR
6910 cpumask_copy(&p->cpus_allowed, new_mask);
6911 p->rt.nr_cpus_allowed = cpumask_weight(new_mask);
73fe6aae
GH
6912 }
6913
1da177e4 6914 /* Can the task run on the task's current CPU? If so, we're done */
96f874e2 6915 if (cpumask_test_cpu(task_cpu(p), new_mask))
1da177e4
LT
6916 goto out;
6917
1e5ce4f4 6918 if (migrate_task(p, cpumask_any_and(cpu_online_mask, new_mask), &req)) {
1da177e4
LT
6919 /* Need help from migration thread: drop lock and wait. */
6920 task_rq_unlock(rq, &flags);
6921 wake_up_process(rq->migration_thread);
6922 wait_for_completion(&req.done);
6923 tlb_migrate_finish(p->mm);
6924 return 0;
6925 }
6926out:
6927 task_rq_unlock(rq, &flags);
48f24c4d 6928
1da177e4
LT
6929 return ret;
6930}
cd8ba7cd 6931EXPORT_SYMBOL_GPL(set_cpus_allowed_ptr);
1da177e4
LT
6932
6933/*
41a2d6cf 6934 * Move (not current) task off this cpu, onto dest cpu. We're doing
1da177e4
LT
6935 * this because either it can't run here any more (set_cpus_allowed()
6936 * away from this CPU, or CPU going down), or because we're
6937 * attempting to rebalance this task on exec (sched_exec).
6938 *
6939 * So we race with normal scheduler movements, but that's OK, as long
6940 * as the task is no longer on this CPU.
efc30814
KK
6941 *
6942 * Returns non-zero if task was successfully migrated.
1da177e4 6943 */
efc30814 6944static int __migrate_task(struct task_struct *p, int src_cpu, int dest_cpu)
1da177e4 6945{
70b97a7f 6946 struct rq *rq_dest, *rq_src;
dd41f596 6947 int ret = 0, on_rq;
1da177e4 6948
e761b772 6949 if (unlikely(!cpu_active(dest_cpu)))
efc30814 6950 return ret;
1da177e4
LT
6951
6952 rq_src = cpu_rq(src_cpu);
6953 rq_dest = cpu_rq(dest_cpu);
6954
6955 double_rq_lock(rq_src, rq_dest);
6956 /* Already moved. */
6957 if (task_cpu(p) != src_cpu)
b1e38734 6958 goto done;
1da177e4 6959 /* Affinity changed (again). */
96f874e2 6960 if (!cpumask_test_cpu(dest_cpu, &p->cpus_allowed))
b1e38734 6961 goto fail;
1da177e4 6962
dd41f596 6963 on_rq = p->se.on_rq;
6e82a3be 6964 if (on_rq)
2e1cb74a 6965 deactivate_task(rq_src, p, 0);
6e82a3be 6966
1da177e4 6967 set_task_cpu(p, dest_cpu);
dd41f596
IM
6968 if (on_rq) {
6969 activate_task(rq_dest, p, 0);
15afe09b 6970 check_preempt_curr(rq_dest, p, 0);
1da177e4 6971 }
b1e38734 6972done:
efc30814 6973 ret = 1;
b1e38734 6974fail:
1da177e4 6975 double_rq_unlock(rq_src, rq_dest);
efc30814 6976 return ret;
1da177e4
LT
6977}
6978
6979/*
6980 * migration_thread - this is a highprio system thread that performs
6981 * thread migration by bumping thread off CPU then 'pushing' onto
6982 * another runqueue.
6983 */
95cdf3b7 6984static int migration_thread(void *data)
1da177e4 6985{
1da177e4 6986 int cpu = (long)data;
70b97a7f 6987 struct rq *rq;
1da177e4
LT
6988
6989 rq = cpu_rq(cpu);
6990 BUG_ON(rq->migration_thread != current);
6991
6992 set_current_state(TASK_INTERRUPTIBLE);
6993 while (!kthread_should_stop()) {
70b97a7f 6994 struct migration_req *req;
1da177e4 6995 struct list_head *head;
1da177e4 6996
1da177e4
LT
6997 spin_lock_irq(&rq->lock);
6998
6999 if (cpu_is_offline(cpu)) {
7000 spin_unlock_irq(&rq->lock);
7001 goto wait_to_die;
7002 }
7003
7004 if (rq->active_balance) {
7005 active_load_balance(rq, cpu);
7006 rq->active_balance = 0;
7007 }
7008
7009 head = &rq->migration_queue;
7010
7011 if (list_empty(head)) {
7012 spin_unlock_irq(&rq->lock);
7013 schedule();
7014 set_current_state(TASK_INTERRUPTIBLE);
7015 continue;
7016 }
70b97a7f 7017 req = list_entry(head->next, struct migration_req, list);
1da177e4
LT
7018 list_del_init(head->next);
7019
674311d5
NP
7020 spin_unlock(&rq->lock);
7021 __migrate_task(req->task, cpu, req->dest_cpu);
7022 local_irq_enable();
1da177e4
LT
7023
7024 complete(&req->done);
7025 }
7026 __set_current_state(TASK_RUNNING);
7027 return 0;
7028
7029wait_to_die:
7030 /* Wait for kthread_stop */
7031 set_current_state(TASK_INTERRUPTIBLE);
7032 while (!kthread_should_stop()) {
7033 schedule();
7034 set_current_state(TASK_INTERRUPTIBLE);
7035 }
7036 __set_current_state(TASK_RUNNING);
7037 return 0;
7038}
7039
7040#ifdef CONFIG_HOTPLUG_CPU
f7b4cddc
ON
7041
7042static int __migrate_task_irq(struct task_struct *p, int src_cpu, int dest_cpu)
7043{
7044 int ret;
7045
7046 local_irq_disable();
7047 ret = __migrate_task(p, src_cpu, dest_cpu);
7048 local_irq_enable();
7049 return ret;
7050}
7051
054b9108 7052/*
3a4fa0a2 7053 * Figure out where task on dead CPU should go, use force if necessary.
054b9108 7054 */
48f24c4d 7055static void move_task_off_dead_cpu(int dead_cpu, struct task_struct *p)
1da177e4 7056{
70b97a7f 7057 int dest_cpu;
6ca09dfc 7058 const struct cpumask *nodemask = cpumask_of_node(cpu_to_node(dead_cpu));
e76bd8d9
RR
7059
7060again:
7061 /* Look for allowed, online CPU in same node. */
7062 for_each_cpu_and(dest_cpu, nodemask, cpu_online_mask)
7063 if (cpumask_test_cpu(dest_cpu, &p->cpus_allowed))
7064 goto move;
7065
7066 /* Any allowed, online CPU? */
7067 dest_cpu = cpumask_any_and(&p->cpus_allowed, cpu_online_mask);
7068 if (dest_cpu < nr_cpu_ids)
7069 goto move;
7070
7071 /* No more Mr. Nice Guy. */
7072 if (dest_cpu >= nr_cpu_ids) {
e76bd8d9
RR
7073 cpuset_cpus_allowed_locked(p, &p->cpus_allowed);
7074 dest_cpu = cpumask_any_and(cpu_online_mask, &p->cpus_allowed);
1da177e4 7075
e76bd8d9
RR
7076 /*
7077 * Don't tell them about moving exiting tasks or
7078 * kernel threads (both mm NULL), since they never
7079 * leave kernel.
7080 */
7081 if (p->mm && printk_ratelimit()) {
7082 printk(KERN_INFO "process %d (%s) no "
7083 "longer affine to cpu%d\n",
7084 task_pid_nr(p), p->comm, dead_cpu);
3a5c359a 7085 }
e76bd8d9
RR
7086 }
7087
7088move:
7089 /* It can have affinity changed while we were choosing. */
7090 if (unlikely(!__migrate_task_irq(p, dead_cpu, dest_cpu)))
7091 goto again;
1da177e4
LT
7092}
7093
7094/*
7095 * While a dead CPU has no uninterruptible tasks queued at this point,
7096 * it might still have a nonzero ->nr_uninterruptible counter, because
7097 * for performance reasons the counter is not stricly tracking tasks to
7098 * their home CPUs. So we just add the counter to another CPU's counter,
7099 * to keep the global sum constant after CPU-down:
7100 */
70b97a7f 7101static void migrate_nr_uninterruptible(struct rq *rq_src)
1da177e4 7102{
1e5ce4f4 7103 struct rq *rq_dest = cpu_rq(cpumask_any(cpu_online_mask));
1da177e4
LT
7104 unsigned long flags;
7105
7106 local_irq_save(flags);
7107 double_rq_lock(rq_src, rq_dest);
7108 rq_dest->nr_uninterruptible += rq_src->nr_uninterruptible;
7109 rq_src->nr_uninterruptible = 0;
7110 double_rq_unlock(rq_src, rq_dest);
7111 local_irq_restore(flags);
7112}
7113
7114/* Run through task list and migrate tasks from the dead cpu. */
7115static void migrate_live_tasks(int src_cpu)
7116{
48f24c4d 7117 struct task_struct *p, *t;
1da177e4 7118
f7b4cddc 7119 read_lock(&tasklist_lock);
1da177e4 7120
48f24c4d
IM
7121 do_each_thread(t, p) {
7122 if (p == current)
1da177e4
LT
7123 continue;
7124
48f24c4d
IM
7125 if (task_cpu(p) == src_cpu)
7126 move_task_off_dead_cpu(src_cpu, p);
7127 } while_each_thread(t, p);
1da177e4 7128
f7b4cddc 7129 read_unlock(&tasklist_lock);
1da177e4
LT
7130}
7131
dd41f596
IM
7132/*
7133 * Schedules idle task to be the next runnable task on current CPU.
94bc9a7b
DA
7134 * It does so by boosting its priority to highest possible.
7135 * Used by CPU offline code.
1da177e4
LT
7136 */
7137void sched_idle_next(void)
7138{
48f24c4d 7139 int this_cpu = smp_processor_id();
70b97a7f 7140 struct rq *rq = cpu_rq(this_cpu);
1da177e4
LT
7141 struct task_struct *p = rq->idle;
7142 unsigned long flags;
7143
7144 /* cpu has to be offline */
48f24c4d 7145 BUG_ON(cpu_online(this_cpu));
1da177e4 7146
48f24c4d
IM
7147 /*
7148 * Strictly not necessary since rest of the CPUs are stopped by now
7149 * and interrupts disabled on the current cpu.
1da177e4
LT
7150 */
7151 spin_lock_irqsave(&rq->lock, flags);
7152
dd41f596 7153 __setscheduler(rq, p, SCHED_FIFO, MAX_RT_PRIO-1);
48f24c4d 7154
94bc9a7b
DA
7155 update_rq_clock(rq);
7156 activate_task(rq, p, 0);
1da177e4
LT
7157
7158 spin_unlock_irqrestore(&rq->lock, flags);
7159}
7160
48f24c4d
IM
7161/*
7162 * Ensures that the idle task is using init_mm right before its cpu goes
1da177e4
LT
7163 * offline.
7164 */
7165void idle_task_exit(void)
7166{
7167 struct mm_struct *mm = current->active_mm;
7168
7169 BUG_ON(cpu_online(smp_processor_id()));
7170
7171 if (mm != &init_mm)
7172 switch_mm(mm, &init_mm, current);
7173 mmdrop(mm);
7174}
7175
054b9108 7176/* called under rq->lock with disabled interrupts */
36c8b586 7177static void migrate_dead(unsigned int dead_cpu, struct task_struct *p)
1da177e4 7178{
70b97a7f 7179 struct rq *rq = cpu_rq(dead_cpu);
1da177e4
LT
7180
7181 /* Must be exiting, otherwise would be on tasklist. */
270f722d 7182 BUG_ON(!p->exit_state);
1da177e4
LT
7183
7184 /* Cannot have done final schedule yet: would have vanished. */
c394cc9f 7185 BUG_ON(p->state == TASK_DEAD);
1da177e4 7186
48f24c4d 7187 get_task_struct(p);
1da177e4
LT
7188
7189 /*
7190 * Drop lock around migration; if someone else moves it,
41a2d6cf 7191 * that's OK. No task can be added to this CPU, so iteration is
1da177e4
LT
7192 * fine.
7193 */
f7b4cddc 7194 spin_unlock_irq(&rq->lock);
48f24c4d 7195 move_task_off_dead_cpu(dead_cpu, p);
f7b4cddc 7196 spin_lock_irq(&rq->lock);
1da177e4 7197
48f24c4d 7198 put_task_struct(p);
1da177e4
LT
7199}
7200
7201/* release_task() removes task from tasklist, so we won't find dead tasks. */
7202static void migrate_dead_tasks(unsigned int dead_cpu)
7203{
70b97a7f 7204 struct rq *rq = cpu_rq(dead_cpu);
dd41f596 7205 struct task_struct *next;
48f24c4d 7206
dd41f596
IM
7207 for ( ; ; ) {
7208 if (!rq->nr_running)
7209 break;
a8e504d2 7210 update_rq_clock(rq);
b67802ea 7211 next = pick_next_task(rq);
dd41f596
IM
7212 if (!next)
7213 break;
79c53799 7214 next->sched_class->put_prev_task(rq, next);
dd41f596 7215 migrate_dead(dead_cpu, next);
e692ab53 7216
1da177e4
LT
7217 }
7218}
dce48a84
TG
7219
7220/*
7221 * remove the tasks which were accounted by rq from calc_load_tasks.
7222 */
7223static void calc_global_load_remove(struct rq *rq)
7224{
7225 atomic_long_sub(rq->calc_load_active, &calc_load_tasks);
7226}
1da177e4
LT
7227#endif /* CONFIG_HOTPLUG_CPU */
7228
e692ab53
NP
7229#if defined(CONFIG_SCHED_DEBUG) && defined(CONFIG_SYSCTL)
7230
7231static struct ctl_table sd_ctl_dir[] = {
e0361851
AD
7232 {
7233 .procname = "sched_domain",
c57baf1e 7234 .mode = 0555,
e0361851 7235 },
38605cae 7236 {0, },
e692ab53
NP
7237};
7238
7239static struct ctl_table sd_ctl_root[] = {
e0361851 7240 {
c57baf1e 7241 .ctl_name = CTL_KERN,
e0361851 7242 .procname = "kernel",
c57baf1e 7243 .mode = 0555,
e0361851
AD
7244 .child = sd_ctl_dir,
7245 },
38605cae 7246 {0, },
e692ab53
NP
7247};
7248
7249static struct ctl_table *sd_alloc_ctl_entry(int n)
7250{
7251 struct ctl_table *entry =
5cf9f062 7252 kcalloc(n, sizeof(struct ctl_table), GFP_KERNEL);
e692ab53 7253
e692ab53
NP
7254 return entry;
7255}
7256
6382bc90
MM
7257static void sd_free_ctl_entry(struct ctl_table **tablep)
7258{
cd790076 7259 struct ctl_table *entry;
6382bc90 7260
cd790076
MM
7261 /*
7262 * In the intermediate directories, both the child directory and
7263 * procname are dynamically allocated and could fail but the mode
41a2d6cf 7264 * will always be set. In the lowest directory the names are
cd790076
MM
7265 * static strings and all have proc handlers.
7266 */
7267 for (entry = *tablep; entry->mode; entry++) {
6382bc90
MM
7268 if (entry->child)
7269 sd_free_ctl_entry(&entry->child);
cd790076
MM
7270 if (entry->proc_handler == NULL)
7271 kfree(entry->procname);
7272 }
6382bc90
MM
7273
7274 kfree(*tablep);
7275 *tablep = NULL;
7276}
7277
e692ab53 7278static void
e0361851 7279set_table_entry(struct ctl_table *entry,
e692ab53
NP
7280 const char *procname, void *data, int maxlen,
7281 mode_t mode, proc_handler *proc_handler)
7282{
e692ab53
NP
7283 entry->procname = procname;
7284 entry->data = data;
7285 entry->maxlen = maxlen;
7286 entry->mode = mode;
7287 entry->proc_handler = proc_handler;
7288}
7289
7290static struct ctl_table *
7291sd_alloc_ctl_domain_table(struct sched_domain *sd)
7292{
a5d8c348 7293 struct ctl_table *table = sd_alloc_ctl_entry(13);
e692ab53 7294
ad1cdc1d
MM
7295 if (table == NULL)
7296 return NULL;
7297
e0361851 7298 set_table_entry(&table[0], "min_interval", &sd->min_interval,
e692ab53 7299 sizeof(long), 0644, proc_doulongvec_minmax);
e0361851 7300 set_table_entry(&table[1], "max_interval", &sd->max_interval,
e692ab53 7301 sizeof(long), 0644, proc_doulongvec_minmax);
e0361851 7302 set_table_entry(&table[2], "busy_idx", &sd->busy_idx,
e692ab53 7303 sizeof(int), 0644, proc_dointvec_minmax);
e0361851 7304 set_table_entry(&table[3], "idle_idx", &sd->idle_idx,
e692ab53 7305 sizeof(int), 0644, proc_dointvec_minmax);
e0361851 7306 set_table_entry(&table[4], "newidle_idx", &sd->newidle_idx,
e692ab53 7307 sizeof(int), 0644, proc_dointvec_minmax);
e0361851 7308 set_table_entry(&table[5], "wake_idx", &sd->wake_idx,
e692ab53 7309 sizeof(int), 0644, proc_dointvec_minmax);
e0361851 7310 set_table_entry(&table[6], "forkexec_idx", &sd->forkexec_idx,
e692ab53 7311 sizeof(int), 0644, proc_dointvec_minmax);
e0361851 7312 set_table_entry(&table[7], "busy_factor", &sd->busy_factor,
e692ab53 7313 sizeof(int), 0644, proc_dointvec_minmax);
e0361851 7314 set_table_entry(&table[8], "imbalance_pct", &sd->imbalance_pct,
e692ab53 7315 sizeof(int), 0644, proc_dointvec_minmax);
ace8b3d6 7316 set_table_entry(&table[9], "cache_nice_tries",
e692ab53
NP
7317 &sd->cache_nice_tries,
7318 sizeof(int), 0644, proc_dointvec_minmax);
ace8b3d6 7319 set_table_entry(&table[10], "flags", &sd->flags,
e692ab53 7320 sizeof(int), 0644, proc_dointvec_minmax);
a5d8c348
IM
7321 set_table_entry(&table[11], "name", sd->name,
7322 CORENAME_MAX_SIZE, 0444, proc_dostring);
7323 /* &table[12] is terminator */
e692ab53
NP
7324
7325 return table;
7326}
7327
9a4e7159 7328static ctl_table *sd_alloc_ctl_cpu_table(int cpu)
e692ab53
NP
7329{
7330 struct ctl_table *entry, *table;
7331 struct sched_domain *sd;
7332 int domain_num = 0, i;
7333 char buf[32];
7334
7335 for_each_domain(cpu, sd)
7336 domain_num++;
7337 entry = table = sd_alloc_ctl_entry(domain_num + 1);
ad1cdc1d
MM
7338 if (table == NULL)
7339 return NULL;
e692ab53
NP
7340
7341 i = 0;
7342 for_each_domain(cpu, sd) {
7343 snprintf(buf, 32, "domain%d", i);
e692ab53 7344 entry->procname = kstrdup(buf, GFP_KERNEL);
c57baf1e 7345 entry->mode = 0555;
e692ab53
NP
7346 entry->child = sd_alloc_ctl_domain_table(sd);
7347 entry++;
7348 i++;
7349 }
7350 return table;
7351}
7352
7353static struct ctl_table_header *sd_sysctl_header;
6382bc90 7354static void register_sched_domain_sysctl(void)
e692ab53
NP
7355{
7356 int i, cpu_num = num_online_cpus();
7357 struct ctl_table *entry = sd_alloc_ctl_entry(cpu_num + 1);
7358 char buf[32];
7359
7378547f
MM
7360 WARN_ON(sd_ctl_dir[0].child);
7361 sd_ctl_dir[0].child = entry;
7362
ad1cdc1d
MM
7363 if (entry == NULL)
7364 return;
7365
97b6ea7b 7366 for_each_online_cpu(i) {
e692ab53 7367 snprintf(buf, 32, "cpu%d", i);
e692ab53 7368 entry->procname = kstrdup(buf, GFP_KERNEL);
c57baf1e 7369 entry->mode = 0555;
e692ab53 7370 entry->child = sd_alloc_ctl_cpu_table(i);
97b6ea7b 7371 entry++;
e692ab53 7372 }
7378547f
MM
7373
7374 WARN_ON(sd_sysctl_header);
e692ab53
NP
7375 sd_sysctl_header = register_sysctl_table(sd_ctl_root);
7376}
6382bc90 7377
7378547f 7378/* may be called multiple times per register */
6382bc90
MM
7379static void unregister_sched_domain_sysctl(void)
7380{
7378547f
MM
7381 if (sd_sysctl_header)
7382 unregister_sysctl_table(sd_sysctl_header);
6382bc90 7383 sd_sysctl_header = NULL;
7378547f
MM
7384 if (sd_ctl_dir[0].child)
7385 sd_free_ctl_entry(&sd_ctl_dir[0].child);
6382bc90 7386}
e692ab53 7387#else
6382bc90
MM
7388static void register_sched_domain_sysctl(void)
7389{
7390}
7391static void unregister_sched_domain_sysctl(void)
e692ab53
NP
7392{
7393}
7394#endif
7395
1f11eb6a
GH
7396static void set_rq_online(struct rq *rq)
7397{
7398 if (!rq->online) {
7399 const struct sched_class *class;
7400
c6c4927b 7401 cpumask_set_cpu(rq->cpu, rq->rd->online);
1f11eb6a
GH
7402 rq->online = 1;
7403
7404 for_each_class(class) {
7405 if (class->rq_online)
7406 class->rq_online(rq);
7407 }
7408 }
7409}
7410
7411static void set_rq_offline(struct rq *rq)
7412{
7413 if (rq->online) {
7414 const struct sched_class *class;
7415
7416 for_each_class(class) {
7417 if (class->rq_offline)
7418 class->rq_offline(rq);
7419 }
7420
c6c4927b 7421 cpumask_clear_cpu(rq->cpu, rq->rd->online);
1f11eb6a
GH
7422 rq->online = 0;
7423 }
7424}
7425
1da177e4
LT
7426/*
7427 * migration_call - callback that gets triggered when a CPU is added.
7428 * Here we can start up the necessary migration thread for the new CPU.
7429 */
48f24c4d
IM
7430static int __cpuinit
7431migration_call(struct notifier_block *nfb, unsigned long action, void *hcpu)
1da177e4 7432{
1da177e4 7433 struct task_struct *p;
48f24c4d 7434 int cpu = (long)hcpu;
1da177e4 7435 unsigned long flags;
70b97a7f 7436 struct rq *rq;
1da177e4
LT
7437
7438 switch (action) {
5be9361c 7439
1da177e4 7440 case CPU_UP_PREPARE:
8bb78442 7441 case CPU_UP_PREPARE_FROZEN:
dd41f596 7442 p = kthread_create(migration_thread, hcpu, "migration/%d", cpu);
1da177e4
LT
7443 if (IS_ERR(p))
7444 return NOTIFY_BAD;
1da177e4
LT
7445 kthread_bind(p, cpu);
7446 /* Must be high prio: stop_machine expects to yield to it. */
7447 rq = task_rq_lock(p, &flags);
dd41f596 7448 __setscheduler(rq, p, SCHED_FIFO, MAX_RT_PRIO-1);
1da177e4
LT
7449 task_rq_unlock(rq, &flags);
7450 cpu_rq(cpu)->migration_thread = p;
7451 break;
48f24c4d 7452
1da177e4 7453 case CPU_ONLINE:
8bb78442 7454 case CPU_ONLINE_FROZEN:
3a4fa0a2 7455 /* Strictly unnecessary, as first user will wake it. */
1da177e4 7456 wake_up_process(cpu_rq(cpu)->migration_thread);
1f94ef59
GH
7457
7458 /* Update our root-domain */
7459 rq = cpu_rq(cpu);
7460 spin_lock_irqsave(&rq->lock, flags);
dce48a84
TG
7461 rq->calc_load_update = calc_load_update;
7462 rq->calc_load_active = 0;
1f94ef59 7463 if (rq->rd) {
c6c4927b 7464 BUG_ON(!cpumask_test_cpu(cpu, rq->rd->span));
1f11eb6a
GH
7465
7466 set_rq_online(rq);
1f94ef59
GH
7467 }
7468 spin_unlock_irqrestore(&rq->lock, flags);
1da177e4 7469 break;
48f24c4d 7470
1da177e4
LT
7471#ifdef CONFIG_HOTPLUG_CPU
7472 case CPU_UP_CANCELED:
8bb78442 7473 case CPU_UP_CANCELED_FROZEN:
fc75cdfa
HC
7474 if (!cpu_rq(cpu)->migration_thread)
7475 break;
41a2d6cf 7476 /* Unbind it from offline cpu so it can run. Fall thru. */
a4c4af7c 7477 kthread_bind(cpu_rq(cpu)->migration_thread,
1e5ce4f4 7478 cpumask_any(cpu_online_mask));
1da177e4
LT
7479 kthread_stop(cpu_rq(cpu)->migration_thread);
7480 cpu_rq(cpu)->migration_thread = NULL;
7481 break;
48f24c4d 7482
1da177e4 7483 case CPU_DEAD:
8bb78442 7484 case CPU_DEAD_FROZEN:
470fd646 7485 cpuset_lock(); /* around calls to cpuset_cpus_allowed_lock() */
1da177e4
LT
7486 migrate_live_tasks(cpu);
7487 rq = cpu_rq(cpu);
7488 kthread_stop(rq->migration_thread);
7489 rq->migration_thread = NULL;
7490 /* Idle task back to normal (off runqueue, low prio) */
d2da272a 7491 spin_lock_irq(&rq->lock);
a8e504d2 7492 update_rq_clock(rq);
2e1cb74a 7493 deactivate_task(rq, rq->idle, 0);
1da177e4 7494 rq->idle->static_prio = MAX_PRIO;
dd41f596
IM
7495 __setscheduler(rq, rq->idle, SCHED_NORMAL, 0);
7496 rq->idle->sched_class = &idle_sched_class;
1da177e4 7497 migrate_dead_tasks(cpu);
d2da272a 7498 spin_unlock_irq(&rq->lock);
470fd646 7499 cpuset_unlock();
1da177e4
LT
7500 migrate_nr_uninterruptible(rq);
7501 BUG_ON(rq->nr_running != 0);
dce48a84 7502 calc_global_load_remove(rq);
41a2d6cf
IM
7503 /*
7504 * No need to migrate the tasks: it was best-effort if
7505 * they didn't take sched_hotcpu_mutex. Just wake up
7506 * the requestors.
7507 */
1da177e4
LT
7508 spin_lock_irq(&rq->lock);
7509 while (!list_empty(&rq->migration_queue)) {
70b97a7f
IM
7510 struct migration_req *req;
7511
1da177e4 7512 req = list_entry(rq->migration_queue.next,
70b97a7f 7513 struct migration_req, list);
1da177e4 7514 list_del_init(&req->list);
9a2bd244 7515 spin_unlock_irq(&rq->lock);
1da177e4 7516 complete(&req->done);
9a2bd244 7517 spin_lock_irq(&rq->lock);
1da177e4
LT
7518 }
7519 spin_unlock_irq(&rq->lock);
7520 break;
57d885fe 7521
08f503b0
GH
7522 case CPU_DYING:
7523 case CPU_DYING_FROZEN:
57d885fe
GH
7524 /* Update our root-domain */
7525 rq = cpu_rq(cpu);
7526 spin_lock_irqsave(&rq->lock, flags);
7527 if (rq->rd) {
c6c4927b 7528 BUG_ON(!cpumask_test_cpu(cpu, rq->rd->span));
1f11eb6a 7529 set_rq_offline(rq);
57d885fe
GH
7530 }
7531 spin_unlock_irqrestore(&rq->lock, flags);
7532 break;
1da177e4
LT
7533#endif
7534 }
7535 return NOTIFY_OK;
7536}
7537
7538/* Register at highest priority so that task migration (migrate_all_tasks)
7539 * happens before everything else.
7540 */
26c2143b 7541static struct notifier_block __cpuinitdata migration_notifier = {
1da177e4
LT
7542 .notifier_call = migration_call,
7543 .priority = 10
7544};
7545
7babe8db 7546static int __init migration_init(void)
1da177e4
LT
7547{
7548 void *cpu = (void *)(long)smp_processor_id();
07dccf33 7549 int err;
48f24c4d
IM
7550
7551 /* Start one for the boot CPU: */
07dccf33
AM
7552 err = migration_call(&migration_notifier, CPU_UP_PREPARE, cpu);
7553 BUG_ON(err == NOTIFY_BAD);
1da177e4
LT
7554 migration_call(&migration_notifier, CPU_ONLINE, cpu);
7555 register_cpu_notifier(&migration_notifier);
7babe8db
EGM
7556
7557 return err;
1da177e4 7558}
7babe8db 7559early_initcall(migration_init);
1da177e4
LT
7560#endif
7561
7562#ifdef CONFIG_SMP
476f3534 7563
3e9830dc 7564#ifdef CONFIG_SCHED_DEBUG
4dcf6aff 7565
7c16ec58 7566static int sched_domain_debug_one(struct sched_domain *sd, int cpu, int level,
96f874e2 7567 struct cpumask *groupmask)
1da177e4 7568{
4dcf6aff 7569 struct sched_group *group = sd->groups;
434d53b0 7570 char str[256];
1da177e4 7571
968ea6d8 7572 cpulist_scnprintf(str, sizeof(str), sched_domain_span(sd));
96f874e2 7573 cpumask_clear(groupmask);
4dcf6aff
IM
7574
7575 printk(KERN_DEBUG "%*s domain %d: ", level, "", level);
7576
7577 if (!(sd->flags & SD_LOAD_BALANCE)) {
7578 printk("does not load-balance\n");
7579 if (sd->parent)
7580 printk(KERN_ERR "ERROR: !SD_LOAD_BALANCE domain"
7581 " has parent");
7582 return -1;
41c7ce9a
NP
7583 }
7584
eefd796a 7585 printk(KERN_CONT "span %s level %s\n", str, sd->name);
4dcf6aff 7586
758b2cdc 7587 if (!cpumask_test_cpu(cpu, sched_domain_span(sd))) {
4dcf6aff
IM
7588 printk(KERN_ERR "ERROR: domain->span does not contain "
7589 "CPU%d\n", cpu);
7590 }
758b2cdc 7591 if (!cpumask_test_cpu(cpu, sched_group_cpus(group))) {
4dcf6aff
IM
7592 printk(KERN_ERR "ERROR: domain->groups does not contain"
7593 " CPU%d\n", cpu);
7594 }
1da177e4 7595
4dcf6aff 7596 printk(KERN_DEBUG "%*s groups:", level + 1, "");
1da177e4 7597 do {
4dcf6aff
IM
7598 if (!group) {
7599 printk("\n");
7600 printk(KERN_ERR "ERROR: group is NULL\n");
1da177e4
LT
7601 break;
7602 }
7603
4dcf6aff
IM
7604 if (!group->__cpu_power) {
7605 printk(KERN_CONT "\n");
7606 printk(KERN_ERR "ERROR: domain->cpu_power not "
7607 "set\n");
7608 break;
7609 }
1da177e4 7610
758b2cdc 7611 if (!cpumask_weight(sched_group_cpus(group))) {
4dcf6aff
IM
7612 printk(KERN_CONT "\n");
7613 printk(KERN_ERR "ERROR: empty group\n");
7614 break;
7615 }
1da177e4 7616
758b2cdc 7617 if (cpumask_intersects(groupmask, sched_group_cpus(group))) {
4dcf6aff
IM
7618 printk(KERN_CONT "\n");
7619 printk(KERN_ERR "ERROR: repeated CPUs\n");
7620 break;
7621 }
1da177e4 7622
758b2cdc 7623 cpumask_or(groupmask, groupmask, sched_group_cpus(group));
1da177e4 7624
968ea6d8 7625 cpulist_scnprintf(str, sizeof(str), sched_group_cpus(group));
381512cf
GS
7626
7627 printk(KERN_CONT " %s", str);
7628 if (group->__cpu_power != SCHED_LOAD_SCALE) {
7629 printk(KERN_CONT " (__cpu_power = %d)",
7630 group->__cpu_power);
7631 }
1da177e4 7632
4dcf6aff
IM
7633 group = group->next;
7634 } while (group != sd->groups);
7635 printk(KERN_CONT "\n");
1da177e4 7636
758b2cdc 7637 if (!cpumask_equal(sched_domain_span(sd), groupmask))
4dcf6aff 7638 printk(KERN_ERR "ERROR: groups don't span domain->span\n");
1da177e4 7639
758b2cdc
RR
7640 if (sd->parent &&
7641 !cpumask_subset(groupmask, sched_domain_span(sd->parent)))
4dcf6aff
IM
7642 printk(KERN_ERR "ERROR: parent span is not a superset "
7643 "of domain->span\n");
7644 return 0;
7645}
1da177e4 7646
4dcf6aff
IM
7647static void sched_domain_debug(struct sched_domain *sd, int cpu)
7648{
d5dd3db1 7649 cpumask_var_t groupmask;
4dcf6aff 7650 int level = 0;
1da177e4 7651
4dcf6aff
IM
7652 if (!sd) {
7653 printk(KERN_DEBUG "CPU%d attaching NULL sched-domain.\n", cpu);
7654 return;
7655 }
1da177e4 7656
4dcf6aff
IM
7657 printk(KERN_DEBUG "CPU%d attaching sched-domain:\n", cpu);
7658
d5dd3db1 7659 if (!alloc_cpumask_var(&groupmask, GFP_KERNEL)) {
7c16ec58
MT
7660 printk(KERN_DEBUG "Cannot load-balance (out of memory)\n");
7661 return;
7662 }
7663
4dcf6aff 7664 for (;;) {
7c16ec58 7665 if (sched_domain_debug_one(sd, cpu, level, groupmask))
4dcf6aff 7666 break;
1da177e4
LT
7667 level++;
7668 sd = sd->parent;
33859f7f 7669 if (!sd)
4dcf6aff
IM
7670 break;
7671 }
d5dd3db1 7672 free_cpumask_var(groupmask);
1da177e4 7673}
6d6bc0ad 7674#else /* !CONFIG_SCHED_DEBUG */
48f24c4d 7675# define sched_domain_debug(sd, cpu) do { } while (0)
6d6bc0ad 7676#endif /* CONFIG_SCHED_DEBUG */
1da177e4 7677
1a20ff27 7678static int sd_degenerate(struct sched_domain *sd)
245af2c7 7679{
758b2cdc 7680 if (cpumask_weight(sched_domain_span(sd)) == 1)
245af2c7
SS
7681 return 1;
7682
7683 /* Following flags need at least 2 groups */
7684 if (sd->flags & (SD_LOAD_BALANCE |
7685 SD_BALANCE_NEWIDLE |
7686 SD_BALANCE_FORK |
89c4710e
SS
7687 SD_BALANCE_EXEC |
7688 SD_SHARE_CPUPOWER |
7689 SD_SHARE_PKG_RESOURCES)) {
245af2c7
SS
7690 if (sd->groups != sd->groups->next)
7691 return 0;
7692 }
7693
7694 /* Following flags don't use groups */
7695 if (sd->flags & (SD_WAKE_IDLE |
7696 SD_WAKE_AFFINE |
7697 SD_WAKE_BALANCE))
7698 return 0;
7699
7700 return 1;
7701}
7702
48f24c4d
IM
7703static int
7704sd_parent_degenerate(struct sched_domain *sd, struct sched_domain *parent)
245af2c7
SS
7705{
7706 unsigned long cflags = sd->flags, pflags = parent->flags;
7707
7708 if (sd_degenerate(parent))
7709 return 1;
7710
758b2cdc 7711 if (!cpumask_equal(sched_domain_span(sd), sched_domain_span(parent)))
245af2c7
SS
7712 return 0;
7713
7714 /* Does parent contain flags not in child? */
7715 /* WAKE_BALANCE is a subset of WAKE_AFFINE */
7716 if (cflags & SD_WAKE_AFFINE)
7717 pflags &= ~SD_WAKE_BALANCE;
7718 /* Flags needing groups don't count if only 1 group in parent */
7719 if (parent->groups == parent->groups->next) {
7720 pflags &= ~(SD_LOAD_BALANCE |
7721 SD_BALANCE_NEWIDLE |
7722 SD_BALANCE_FORK |
89c4710e
SS
7723 SD_BALANCE_EXEC |
7724 SD_SHARE_CPUPOWER |
7725 SD_SHARE_PKG_RESOURCES);
5436499e
KC
7726 if (nr_node_ids == 1)
7727 pflags &= ~SD_SERIALIZE;
245af2c7
SS
7728 }
7729 if (~cflags & pflags)
7730 return 0;
7731
7732 return 1;
7733}
7734
c6c4927b
RR
7735static void free_rootdomain(struct root_domain *rd)
7736{
68e74568
RR
7737 cpupri_cleanup(&rd->cpupri);
7738
c6c4927b
RR
7739 free_cpumask_var(rd->rto_mask);
7740 free_cpumask_var(rd->online);
7741 free_cpumask_var(rd->span);
7742 kfree(rd);
7743}
7744
57d885fe
GH
7745static void rq_attach_root(struct rq *rq, struct root_domain *rd)
7746{
a0490fa3 7747 struct root_domain *old_rd = NULL;
57d885fe 7748 unsigned long flags;
57d885fe
GH
7749
7750 spin_lock_irqsave(&rq->lock, flags);
7751
7752 if (rq->rd) {
a0490fa3 7753 old_rd = rq->rd;
57d885fe 7754
c6c4927b 7755 if (cpumask_test_cpu(rq->cpu, old_rd->online))
1f11eb6a 7756 set_rq_offline(rq);
57d885fe 7757
c6c4927b 7758 cpumask_clear_cpu(rq->cpu, old_rd->span);
dc938520 7759
a0490fa3
IM
7760 /*
7761 * If we dont want to free the old_rt yet then
7762 * set old_rd to NULL to skip the freeing later
7763 * in this function:
7764 */
7765 if (!atomic_dec_and_test(&old_rd->refcount))
7766 old_rd = NULL;
57d885fe
GH
7767 }
7768
7769 atomic_inc(&rd->refcount);
7770 rq->rd = rd;
7771
c6c4927b
RR
7772 cpumask_set_cpu(rq->cpu, rd->span);
7773 if (cpumask_test_cpu(rq->cpu, cpu_online_mask))
1f11eb6a 7774 set_rq_online(rq);
57d885fe
GH
7775
7776 spin_unlock_irqrestore(&rq->lock, flags);
a0490fa3
IM
7777
7778 if (old_rd)
7779 free_rootdomain(old_rd);
57d885fe
GH
7780}
7781
db2f59c8 7782static int __init_refok init_rootdomain(struct root_domain *rd, bool bootmem)
57d885fe 7783{
36b7b6d4
PE
7784 gfp_t gfp = GFP_KERNEL;
7785
57d885fe
GH
7786 memset(rd, 0, sizeof(*rd));
7787
36b7b6d4
PE
7788 if (bootmem)
7789 gfp = GFP_NOWAIT;
c6c4927b 7790
36b7b6d4 7791 if (!alloc_cpumask_var(&rd->span, gfp))
0c910d28 7792 goto out;
36b7b6d4 7793 if (!alloc_cpumask_var(&rd->online, gfp))
c6c4927b 7794 goto free_span;
36b7b6d4 7795 if (!alloc_cpumask_var(&rd->rto_mask, gfp))
c6c4927b 7796 goto free_online;
6e0534f2 7797
68e74568
RR
7798 if (cpupri_init(&rd->cpupri, false) != 0)
7799 goto free_rto_mask;
c6c4927b 7800 return 0;
6e0534f2 7801
68e74568
RR
7802free_rto_mask:
7803 free_cpumask_var(rd->rto_mask);
c6c4927b
RR
7804free_online:
7805 free_cpumask_var(rd->online);
7806free_span:
7807 free_cpumask_var(rd->span);
0c910d28 7808out:
c6c4927b 7809 return -ENOMEM;
57d885fe
GH
7810}
7811
7812static void init_defrootdomain(void)
7813{
c6c4927b
RR
7814 init_rootdomain(&def_root_domain, true);
7815
57d885fe
GH
7816 atomic_set(&def_root_domain.refcount, 1);
7817}
7818
dc938520 7819static struct root_domain *alloc_rootdomain(void)
57d885fe
GH
7820{
7821 struct root_domain *rd;
7822
7823 rd = kmalloc(sizeof(*rd), GFP_KERNEL);
7824 if (!rd)
7825 return NULL;
7826
c6c4927b
RR
7827 if (init_rootdomain(rd, false) != 0) {
7828 kfree(rd);
7829 return NULL;
7830 }
57d885fe
GH
7831
7832 return rd;
7833}
7834
1da177e4 7835/*
0eab9146 7836 * Attach the domain 'sd' to 'cpu' as its base domain. Callers must
1da177e4
LT
7837 * hold the hotplug lock.
7838 */
0eab9146
IM
7839static void
7840cpu_attach_domain(struct sched_domain *sd, struct root_domain *rd, int cpu)
1da177e4 7841{
70b97a7f 7842 struct rq *rq = cpu_rq(cpu);
245af2c7
SS
7843 struct sched_domain *tmp;
7844
7845 /* Remove the sched domains which do not contribute to scheduling. */
f29c9b1c 7846 for (tmp = sd; tmp; ) {
245af2c7
SS
7847 struct sched_domain *parent = tmp->parent;
7848 if (!parent)
7849 break;
f29c9b1c 7850
1a848870 7851 if (sd_parent_degenerate(tmp, parent)) {
245af2c7 7852 tmp->parent = parent->parent;
1a848870
SS
7853 if (parent->parent)
7854 parent->parent->child = tmp;
f29c9b1c
LZ
7855 } else
7856 tmp = tmp->parent;
245af2c7
SS
7857 }
7858
1a848870 7859 if (sd && sd_degenerate(sd)) {
245af2c7 7860 sd = sd->parent;
1a848870
SS
7861 if (sd)
7862 sd->child = NULL;
7863 }
1da177e4
LT
7864
7865 sched_domain_debug(sd, cpu);
7866
57d885fe 7867 rq_attach_root(rq, rd);
674311d5 7868 rcu_assign_pointer(rq->sd, sd);
1da177e4
LT
7869}
7870
7871/* cpus with isolated domains */
dcc30a35 7872static cpumask_var_t cpu_isolated_map;
1da177e4
LT
7873
7874/* Setup the mask of cpus configured for isolated domains */
7875static int __init isolated_cpu_setup(char *str)
7876{
968ea6d8 7877 cpulist_parse(str, cpu_isolated_map);
1da177e4
LT
7878 return 1;
7879}
7880
8927f494 7881__setup("isolcpus=", isolated_cpu_setup);
1da177e4
LT
7882
7883/*
6711cab4
SS
7884 * init_sched_build_groups takes the cpumask we wish to span, and a pointer
7885 * to a function which identifies what group(along with sched group) a CPU
96f874e2
RR
7886 * belongs to. The return value of group_fn must be a >= 0 and < nr_cpu_ids
7887 * (due to the fact that we keep track of groups covered with a struct cpumask).
1da177e4
LT
7888 *
7889 * init_sched_build_groups will build a circular linked list of the groups
7890 * covered by the given span, and will set each group's ->cpumask correctly,
7891 * and ->cpu_power to 0.
7892 */
a616058b 7893static void
96f874e2
RR
7894init_sched_build_groups(const struct cpumask *span,
7895 const struct cpumask *cpu_map,
7896 int (*group_fn)(int cpu, const struct cpumask *cpu_map,
7c16ec58 7897 struct sched_group **sg,
96f874e2
RR
7898 struct cpumask *tmpmask),
7899 struct cpumask *covered, struct cpumask *tmpmask)
1da177e4
LT
7900{
7901 struct sched_group *first = NULL, *last = NULL;
1da177e4
LT
7902 int i;
7903
96f874e2 7904 cpumask_clear(covered);
7c16ec58 7905
abcd083a 7906 for_each_cpu(i, span) {
6711cab4 7907 struct sched_group *sg;
7c16ec58 7908 int group = group_fn(i, cpu_map, &sg, tmpmask);
1da177e4
LT
7909 int j;
7910
758b2cdc 7911 if (cpumask_test_cpu(i, covered))
1da177e4
LT
7912 continue;
7913
758b2cdc 7914 cpumask_clear(sched_group_cpus(sg));
5517d86b 7915 sg->__cpu_power = 0;
1da177e4 7916
abcd083a 7917 for_each_cpu(j, span) {
7c16ec58 7918 if (group_fn(j, cpu_map, NULL, tmpmask) != group)
1da177e4
LT
7919 continue;
7920
96f874e2 7921 cpumask_set_cpu(j, covered);
758b2cdc 7922 cpumask_set_cpu(j, sched_group_cpus(sg));
1da177e4
LT
7923 }
7924 if (!first)
7925 first = sg;
7926 if (last)
7927 last->next = sg;
7928 last = sg;
7929 }
7930 last->next = first;
7931}
7932
9c1cfda2 7933#define SD_NODES_PER_DOMAIN 16
1da177e4 7934
9c1cfda2 7935#ifdef CONFIG_NUMA
198e2f18 7936
9c1cfda2
JH
7937/**
7938 * find_next_best_node - find the next node to include in a sched_domain
7939 * @node: node whose sched_domain we're building
7940 * @used_nodes: nodes already in the sched_domain
7941 *
41a2d6cf 7942 * Find the next node to include in a given scheduling domain. Simply
9c1cfda2
JH
7943 * finds the closest node not already in the @used_nodes map.
7944 *
7945 * Should use nodemask_t.
7946 */
c5f59f08 7947static int find_next_best_node(int node, nodemask_t *used_nodes)
9c1cfda2
JH
7948{
7949 int i, n, val, min_val, best_node = 0;
7950
7951 min_val = INT_MAX;
7952
076ac2af 7953 for (i = 0; i < nr_node_ids; i++) {
9c1cfda2 7954 /* Start at @node */
076ac2af 7955 n = (node + i) % nr_node_ids;
9c1cfda2
JH
7956
7957 if (!nr_cpus_node(n))
7958 continue;
7959
7960 /* Skip already used nodes */
c5f59f08 7961 if (node_isset(n, *used_nodes))
9c1cfda2
JH
7962 continue;
7963
7964 /* Simple min distance search */
7965 val = node_distance(node, n);
7966
7967 if (val < min_val) {
7968 min_val = val;
7969 best_node = n;
7970 }
7971 }
7972
c5f59f08 7973 node_set(best_node, *used_nodes);
9c1cfda2
JH
7974 return best_node;
7975}
7976
7977/**
7978 * sched_domain_node_span - get a cpumask for a node's sched_domain
7979 * @node: node whose cpumask we're constructing
73486722 7980 * @span: resulting cpumask
9c1cfda2 7981 *
41a2d6cf 7982 * Given a node, construct a good cpumask for its sched_domain to span. It
9c1cfda2
JH
7983 * should be one that prevents unnecessary balancing, but also spreads tasks
7984 * out optimally.
7985 */
96f874e2 7986static void sched_domain_node_span(int node, struct cpumask *span)
9c1cfda2 7987{
c5f59f08 7988 nodemask_t used_nodes;
48f24c4d 7989 int i;
9c1cfda2 7990
6ca09dfc 7991 cpumask_clear(span);
c5f59f08 7992 nodes_clear(used_nodes);
9c1cfda2 7993
6ca09dfc 7994 cpumask_or(span, span, cpumask_of_node(node));
c5f59f08 7995 node_set(node, used_nodes);
9c1cfda2
JH
7996
7997 for (i = 1; i < SD_NODES_PER_DOMAIN; i++) {
c5f59f08 7998 int next_node = find_next_best_node(node, &used_nodes);
48f24c4d 7999
6ca09dfc 8000 cpumask_or(span, span, cpumask_of_node(next_node));
9c1cfda2 8001 }
9c1cfda2 8002}
6d6bc0ad 8003#endif /* CONFIG_NUMA */
9c1cfda2 8004
5c45bf27 8005int sched_smt_power_savings = 0, sched_mc_power_savings = 0;
48f24c4d 8006
6c99e9ad
RR
8007/*
8008 * The cpus mask in sched_group and sched_domain hangs off the end.
4200efd9
IM
8009 *
8010 * ( See the the comments in include/linux/sched.h:struct sched_group
8011 * and struct sched_domain. )
6c99e9ad
RR
8012 */
8013struct static_sched_group {
8014 struct sched_group sg;
8015 DECLARE_BITMAP(cpus, CONFIG_NR_CPUS);
8016};
8017
8018struct static_sched_domain {
8019 struct sched_domain sd;
8020 DECLARE_BITMAP(span, CONFIG_NR_CPUS);
8021};
8022
9c1cfda2 8023/*
48f24c4d 8024 * SMT sched-domains:
9c1cfda2 8025 */
1da177e4 8026#ifdef CONFIG_SCHED_SMT
6c99e9ad
RR
8027static DEFINE_PER_CPU(struct static_sched_domain, cpu_domains);
8028static DEFINE_PER_CPU(struct static_sched_group, sched_group_cpus);
48f24c4d 8029
41a2d6cf 8030static int
96f874e2
RR
8031cpu_to_cpu_group(int cpu, const struct cpumask *cpu_map,
8032 struct sched_group **sg, struct cpumask *unused)
1da177e4 8033{
6711cab4 8034 if (sg)
6c99e9ad 8035 *sg = &per_cpu(sched_group_cpus, cpu).sg;
1da177e4
LT
8036 return cpu;
8037}
6d6bc0ad 8038#endif /* CONFIG_SCHED_SMT */
1da177e4 8039
48f24c4d
IM
8040/*
8041 * multi-core sched-domains:
8042 */
1e9f28fa 8043#ifdef CONFIG_SCHED_MC
6c99e9ad
RR
8044static DEFINE_PER_CPU(struct static_sched_domain, core_domains);
8045static DEFINE_PER_CPU(struct static_sched_group, sched_group_core);
6d6bc0ad 8046#endif /* CONFIG_SCHED_MC */
1e9f28fa
SS
8047
8048#if defined(CONFIG_SCHED_MC) && defined(CONFIG_SCHED_SMT)
41a2d6cf 8049static int
96f874e2
RR
8050cpu_to_core_group(int cpu, const struct cpumask *cpu_map,
8051 struct sched_group **sg, struct cpumask *mask)
1e9f28fa 8052{
6711cab4 8053 int group;
7c16ec58 8054
c69fc56d 8055 cpumask_and(mask, topology_thread_cpumask(cpu), cpu_map);
96f874e2 8056 group = cpumask_first(mask);
6711cab4 8057 if (sg)
6c99e9ad 8058 *sg = &per_cpu(sched_group_core, group).sg;
6711cab4 8059 return group;
1e9f28fa
SS
8060}
8061#elif defined(CONFIG_SCHED_MC)
41a2d6cf 8062static int
96f874e2
RR
8063cpu_to_core_group(int cpu, const struct cpumask *cpu_map,
8064 struct sched_group **sg, struct cpumask *unused)
1e9f28fa 8065{
6711cab4 8066 if (sg)
6c99e9ad 8067 *sg = &per_cpu(sched_group_core, cpu).sg;
1e9f28fa
SS
8068 return cpu;
8069}
8070#endif
8071
6c99e9ad
RR
8072static DEFINE_PER_CPU(struct static_sched_domain, phys_domains);
8073static DEFINE_PER_CPU(struct static_sched_group, sched_group_phys);
48f24c4d 8074
41a2d6cf 8075static int
96f874e2
RR
8076cpu_to_phys_group(int cpu, const struct cpumask *cpu_map,
8077 struct sched_group **sg, struct cpumask *mask)
1da177e4 8078{
6711cab4 8079 int group;
48f24c4d 8080#ifdef CONFIG_SCHED_MC
6ca09dfc 8081 cpumask_and(mask, cpu_coregroup_mask(cpu), cpu_map);
96f874e2 8082 group = cpumask_first(mask);
1e9f28fa 8083#elif defined(CONFIG_SCHED_SMT)
c69fc56d 8084 cpumask_and(mask, topology_thread_cpumask(cpu), cpu_map);
96f874e2 8085 group = cpumask_first(mask);
1da177e4 8086#else
6711cab4 8087 group = cpu;
1da177e4 8088#endif
6711cab4 8089 if (sg)
6c99e9ad 8090 *sg = &per_cpu(sched_group_phys, group).sg;
6711cab4 8091 return group;
1da177e4
LT
8092}
8093
8094#ifdef CONFIG_NUMA
1da177e4 8095/*
9c1cfda2
JH
8096 * The init_sched_build_groups can't handle what we want to do with node
8097 * groups, so roll our own. Now each node has its own list of groups which
8098 * gets dynamically allocated.
1da177e4 8099 */
62ea9ceb 8100static DEFINE_PER_CPU(struct static_sched_domain, node_domains);
434d53b0 8101static struct sched_group ***sched_group_nodes_bycpu;
1da177e4 8102
62ea9ceb 8103static DEFINE_PER_CPU(struct static_sched_domain, allnodes_domains);
6c99e9ad 8104static DEFINE_PER_CPU(struct static_sched_group, sched_group_allnodes);
9c1cfda2 8105
96f874e2
RR
8106static int cpu_to_allnodes_group(int cpu, const struct cpumask *cpu_map,
8107 struct sched_group **sg,
8108 struct cpumask *nodemask)
9c1cfda2 8109{
6711cab4
SS
8110 int group;
8111
6ca09dfc 8112 cpumask_and(nodemask, cpumask_of_node(cpu_to_node(cpu)), cpu_map);
96f874e2 8113 group = cpumask_first(nodemask);
6711cab4
SS
8114
8115 if (sg)
6c99e9ad 8116 *sg = &per_cpu(sched_group_allnodes, group).sg;
6711cab4 8117 return group;
1da177e4 8118}
6711cab4 8119
08069033
SS
8120static void init_numa_sched_groups_power(struct sched_group *group_head)
8121{
8122 struct sched_group *sg = group_head;
8123 int j;
8124
8125 if (!sg)
8126 return;
3a5c359a 8127 do {
758b2cdc 8128 for_each_cpu(j, sched_group_cpus(sg)) {
3a5c359a 8129 struct sched_domain *sd;
08069033 8130
6c99e9ad 8131 sd = &per_cpu(phys_domains, j).sd;
13318a71 8132 if (j != group_first_cpu(sd->groups)) {
3a5c359a
AK
8133 /*
8134 * Only add "power" once for each
8135 * physical package.
8136 */
8137 continue;
8138 }
08069033 8139
3a5c359a
AK
8140 sg_inc_cpu_power(sg, sd->groups->__cpu_power);
8141 }
8142 sg = sg->next;
8143 } while (sg != group_head);
08069033 8144}
6d6bc0ad 8145#endif /* CONFIG_NUMA */
1da177e4 8146
a616058b 8147#ifdef CONFIG_NUMA
51888ca2 8148/* Free memory allocated for various sched_group structures */
96f874e2
RR
8149static void free_sched_groups(const struct cpumask *cpu_map,
8150 struct cpumask *nodemask)
51888ca2 8151{
a616058b 8152 int cpu, i;
51888ca2 8153
abcd083a 8154 for_each_cpu(cpu, cpu_map) {
51888ca2
SV
8155 struct sched_group **sched_group_nodes
8156 = sched_group_nodes_bycpu[cpu];
8157
51888ca2
SV
8158 if (!sched_group_nodes)
8159 continue;
8160
076ac2af 8161 for (i = 0; i < nr_node_ids; i++) {
51888ca2
SV
8162 struct sched_group *oldsg, *sg = sched_group_nodes[i];
8163
6ca09dfc 8164 cpumask_and(nodemask, cpumask_of_node(i), cpu_map);
96f874e2 8165 if (cpumask_empty(nodemask))
51888ca2
SV
8166 continue;
8167
8168 if (sg == NULL)
8169 continue;
8170 sg = sg->next;
8171next_sg:
8172 oldsg = sg;
8173 sg = sg->next;
8174 kfree(oldsg);
8175 if (oldsg != sched_group_nodes[i])
8176 goto next_sg;
8177 }
8178 kfree(sched_group_nodes);
8179 sched_group_nodes_bycpu[cpu] = NULL;
8180 }
51888ca2 8181}
6d6bc0ad 8182#else /* !CONFIG_NUMA */
96f874e2
RR
8183static void free_sched_groups(const struct cpumask *cpu_map,
8184 struct cpumask *nodemask)
a616058b
SS
8185{
8186}
6d6bc0ad 8187#endif /* CONFIG_NUMA */
51888ca2 8188
89c4710e
SS
8189/*
8190 * Initialize sched groups cpu_power.
8191 *
8192 * cpu_power indicates the capacity of sched group, which is used while
8193 * distributing the load between different sched groups in a sched domain.
8194 * Typically cpu_power for all the groups in a sched domain will be same unless
8195 * there are asymmetries in the topology. If there are asymmetries, group
8196 * having more cpu_power will pickup more load compared to the group having
8197 * less cpu_power.
8198 *
8199 * cpu_power will be a multiple of SCHED_LOAD_SCALE. This multiple represents
8200 * the maximum number of tasks a group can handle in the presence of other idle
8201 * or lightly loaded groups in the same sched domain.
8202 */
8203static void init_sched_groups_power(int cpu, struct sched_domain *sd)
8204{
8205 struct sched_domain *child;
8206 struct sched_group *group;
8207
8208 WARN_ON(!sd || !sd->groups);
8209
13318a71 8210 if (cpu != group_first_cpu(sd->groups))
89c4710e
SS
8211 return;
8212
8213 child = sd->child;
8214
5517d86b
ED
8215 sd->groups->__cpu_power = 0;
8216
89c4710e
SS
8217 /*
8218 * For perf policy, if the groups in child domain share resources
8219 * (for example cores sharing some portions of the cache hierarchy
8220 * or SMT), then set this domain groups cpu_power such that each group
8221 * can handle only one task, when there are other idle groups in the
8222 * same sched domain.
8223 */
8224 if (!child || (!(sd->flags & SD_POWERSAVINGS_BALANCE) &&
8225 (child->flags &
8226 (SD_SHARE_CPUPOWER | SD_SHARE_PKG_RESOURCES)))) {
5517d86b 8227 sg_inc_cpu_power(sd->groups, SCHED_LOAD_SCALE);
89c4710e
SS
8228 return;
8229 }
8230
89c4710e
SS
8231 /*
8232 * add cpu_power of each child group to this groups cpu_power
8233 */
8234 group = child->groups;
8235 do {
5517d86b 8236 sg_inc_cpu_power(sd->groups, group->__cpu_power);
89c4710e
SS
8237 group = group->next;
8238 } while (group != child->groups);
8239}
8240
7c16ec58
MT
8241/*
8242 * Initializers for schedule domains
8243 * Non-inlined to reduce accumulated stack pressure in build_sched_domains()
8244 */
8245
a5d8c348
IM
8246#ifdef CONFIG_SCHED_DEBUG
8247# define SD_INIT_NAME(sd, type) sd->name = #type
8248#else
8249# define SD_INIT_NAME(sd, type) do { } while (0)
8250#endif
8251
7c16ec58 8252#define SD_INIT(sd, type) sd_init_##type(sd)
a5d8c348 8253
7c16ec58
MT
8254#define SD_INIT_FUNC(type) \
8255static noinline void sd_init_##type(struct sched_domain *sd) \
8256{ \
8257 memset(sd, 0, sizeof(*sd)); \
8258 *sd = SD_##type##_INIT; \
1d3504fc 8259 sd->level = SD_LV_##type; \
a5d8c348 8260 SD_INIT_NAME(sd, type); \
7c16ec58
MT
8261}
8262
8263SD_INIT_FUNC(CPU)
8264#ifdef CONFIG_NUMA
8265 SD_INIT_FUNC(ALLNODES)
8266 SD_INIT_FUNC(NODE)
8267#endif
8268#ifdef CONFIG_SCHED_SMT
8269 SD_INIT_FUNC(SIBLING)
8270#endif
8271#ifdef CONFIG_SCHED_MC
8272 SD_INIT_FUNC(MC)
8273#endif
8274
1d3504fc
HS
8275static int default_relax_domain_level = -1;
8276
8277static int __init setup_relax_domain_level(char *str)
8278{
30e0e178
LZ
8279 unsigned long val;
8280
8281 val = simple_strtoul(str, NULL, 0);
8282 if (val < SD_LV_MAX)
8283 default_relax_domain_level = val;
8284
1d3504fc
HS
8285 return 1;
8286}
8287__setup("relax_domain_level=", setup_relax_domain_level);
8288
8289static void set_domain_attribute(struct sched_domain *sd,
8290 struct sched_domain_attr *attr)
8291{
8292 int request;
8293
8294 if (!attr || attr->relax_domain_level < 0) {
8295 if (default_relax_domain_level < 0)
8296 return;
8297 else
8298 request = default_relax_domain_level;
8299 } else
8300 request = attr->relax_domain_level;
8301 if (request < sd->level) {
8302 /* turn off idle balance on this domain */
8303 sd->flags &= ~(SD_WAKE_IDLE|SD_BALANCE_NEWIDLE);
8304 } else {
8305 /* turn on idle balance on this domain */
8306 sd->flags |= (SD_WAKE_IDLE_FAR|SD_BALANCE_NEWIDLE);
8307 }
8308}
8309
1da177e4 8310/*
1a20ff27
DG
8311 * Build sched domains for a given set of cpus and attach the sched domains
8312 * to the individual cpus
1da177e4 8313 */
96f874e2 8314static int __build_sched_domains(const struct cpumask *cpu_map,
1d3504fc 8315 struct sched_domain_attr *attr)
1da177e4 8316{
3404c8d9 8317 int i, err = -ENOMEM;
57d885fe 8318 struct root_domain *rd;
3404c8d9
RR
8319 cpumask_var_t nodemask, this_sibling_map, this_core_map, send_covered,
8320 tmpmask;
d1b55138 8321#ifdef CONFIG_NUMA
3404c8d9 8322 cpumask_var_t domainspan, covered, notcovered;
d1b55138 8323 struct sched_group **sched_group_nodes = NULL;
6711cab4 8324 int sd_allnodes = 0;
d1b55138 8325
3404c8d9
RR
8326 if (!alloc_cpumask_var(&domainspan, GFP_KERNEL))
8327 goto out;
8328 if (!alloc_cpumask_var(&covered, GFP_KERNEL))
8329 goto free_domainspan;
8330 if (!alloc_cpumask_var(&notcovered, GFP_KERNEL))
8331 goto free_covered;
8332#endif
8333
8334 if (!alloc_cpumask_var(&nodemask, GFP_KERNEL))
8335 goto free_notcovered;
8336 if (!alloc_cpumask_var(&this_sibling_map, GFP_KERNEL))
8337 goto free_nodemask;
8338 if (!alloc_cpumask_var(&this_core_map, GFP_KERNEL))
8339 goto free_this_sibling_map;
8340 if (!alloc_cpumask_var(&send_covered, GFP_KERNEL))
8341 goto free_this_core_map;
8342 if (!alloc_cpumask_var(&tmpmask, GFP_KERNEL))
8343 goto free_send_covered;
8344
8345#ifdef CONFIG_NUMA
d1b55138
JH
8346 /*
8347 * Allocate the per-node list of sched groups
8348 */
076ac2af 8349 sched_group_nodes = kcalloc(nr_node_ids, sizeof(struct sched_group *),
41a2d6cf 8350 GFP_KERNEL);
d1b55138
JH
8351 if (!sched_group_nodes) {
8352 printk(KERN_WARNING "Can not alloc sched group node list\n");
3404c8d9 8353 goto free_tmpmask;
d1b55138 8354 }
d1b55138 8355#endif
1da177e4 8356
dc938520 8357 rd = alloc_rootdomain();
57d885fe
GH
8358 if (!rd) {
8359 printk(KERN_WARNING "Cannot alloc root domain\n");
3404c8d9 8360 goto free_sched_groups;
57d885fe
GH
8361 }
8362
7c16ec58 8363#ifdef CONFIG_NUMA
96f874e2 8364 sched_group_nodes_bycpu[cpumask_first(cpu_map)] = sched_group_nodes;
7c16ec58
MT
8365#endif
8366
1da177e4 8367 /*
1a20ff27 8368 * Set up domains for cpus specified by the cpu_map.
1da177e4 8369 */
abcd083a 8370 for_each_cpu(i, cpu_map) {
1da177e4 8371 struct sched_domain *sd = NULL, *p;
1da177e4 8372
6ca09dfc 8373 cpumask_and(nodemask, cpumask_of_node(cpu_to_node(i)), cpu_map);
1da177e4
LT
8374
8375#ifdef CONFIG_NUMA
96f874e2
RR
8376 if (cpumask_weight(cpu_map) >
8377 SD_NODES_PER_DOMAIN*cpumask_weight(nodemask)) {
62ea9ceb 8378 sd = &per_cpu(allnodes_domains, i).sd;
7c16ec58 8379 SD_INIT(sd, ALLNODES);
1d3504fc 8380 set_domain_attribute(sd, attr);
758b2cdc 8381 cpumask_copy(sched_domain_span(sd), cpu_map);
7c16ec58 8382 cpu_to_allnodes_group(i, cpu_map, &sd->groups, tmpmask);
9c1cfda2 8383 p = sd;
6711cab4 8384 sd_allnodes = 1;
9c1cfda2
JH
8385 } else
8386 p = NULL;
8387
62ea9ceb 8388 sd = &per_cpu(node_domains, i).sd;
7c16ec58 8389 SD_INIT(sd, NODE);
1d3504fc 8390 set_domain_attribute(sd, attr);
758b2cdc 8391 sched_domain_node_span(cpu_to_node(i), sched_domain_span(sd));
9c1cfda2 8392 sd->parent = p;
1a848870
SS
8393 if (p)
8394 p->child = sd;
758b2cdc
RR
8395 cpumask_and(sched_domain_span(sd),
8396 sched_domain_span(sd), cpu_map);
1da177e4
LT
8397#endif
8398
8399 p = sd;
6c99e9ad 8400 sd = &per_cpu(phys_domains, i).sd;
7c16ec58 8401 SD_INIT(sd, CPU);
1d3504fc 8402 set_domain_attribute(sd, attr);
758b2cdc 8403 cpumask_copy(sched_domain_span(sd), nodemask);
1da177e4 8404 sd->parent = p;
1a848870
SS
8405 if (p)
8406 p->child = sd;
7c16ec58 8407 cpu_to_phys_group(i, cpu_map, &sd->groups, tmpmask);
1da177e4 8408
1e9f28fa
SS
8409#ifdef CONFIG_SCHED_MC
8410 p = sd;
6c99e9ad 8411 sd = &per_cpu(core_domains, i).sd;
7c16ec58 8412 SD_INIT(sd, MC);
1d3504fc 8413 set_domain_attribute(sd, attr);
6ca09dfc
MT
8414 cpumask_and(sched_domain_span(sd), cpu_map,
8415 cpu_coregroup_mask(i));
1e9f28fa 8416 sd->parent = p;
1a848870 8417 p->child = sd;
7c16ec58 8418 cpu_to_core_group(i, cpu_map, &sd->groups, tmpmask);
1e9f28fa
SS
8419#endif
8420
1da177e4
LT
8421#ifdef CONFIG_SCHED_SMT
8422 p = sd;
6c99e9ad 8423 sd = &per_cpu(cpu_domains, i).sd;
7c16ec58 8424 SD_INIT(sd, SIBLING);
1d3504fc 8425 set_domain_attribute(sd, attr);
758b2cdc 8426 cpumask_and(sched_domain_span(sd),
c69fc56d 8427 topology_thread_cpumask(i), cpu_map);
1da177e4 8428 sd->parent = p;
1a848870 8429 p->child = sd;
7c16ec58 8430 cpu_to_cpu_group(i, cpu_map, &sd->groups, tmpmask);
1da177e4
LT
8431#endif
8432 }
8433
8434#ifdef CONFIG_SCHED_SMT
8435 /* Set up CPU (sibling) groups */
abcd083a 8436 for_each_cpu(i, cpu_map) {
96f874e2 8437 cpumask_and(this_sibling_map,
c69fc56d 8438 topology_thread_cpumask(i), cpu_map);
96f874e2 8439 if (i != cpumask_first(this_sibling_map))
1da177e4
LT
8440 continue;
8441
dd41f596 8442 init_sched_build_groups(this_sibling_map, cpu_map,
7c16ec58
MT
8443 &cpu_to_cpu_group,
8444 send_covered, tmpmask);
1da177e4
LT
8445 }
8446#endif
8447
1e9f28fa
SS
8448#ifdef CONFIG_SCHED_MC
8449 /* Set up multi-core groups */
abcd083a 8450 for_each_cpu(i, cpu_map) {
6ca09dfc 8451 cpumask_and(this_core_map, cpu_coregroup_mask(i), cpu_map);
96f874e2 8452 if (i != cpumask_first(this_core_map))
1e9f28fa 8453 continue;
7c16ec58 8454
dd41f596 8455 init_sched_build_groups(this_core_map, cpu_map,
7c16ec58
MT
8456 &cpu_to_core_group,
8457 send_covered, tmpmask);
1e9f28fa
SS
8458 }
8459#endif
8460
1da177e4 8461 /* Set up physical groups */
076ac2af 8462 for (i = 0; i < nr_node_ids; i++) {
6ca09dfc 8463 cpumask_and(nodemask, cpumask_of_node(i), cpu_map);
96f874e2 8464 if (cpumask_empty(nodemask))
1da177e4
LT
8465 continue;
8466
7c16ec58
MT
8467 init_sched_build_groups(nodemask, cpu_map,
8468 &cpu_to_phys_group,
8469 send_covered, tmpmask);
1da177e4
LT
8470 }
8471
8472#ifdef CONFIG_NUMA
8473 /* Set up node groups */
7c16ec58 8474 if (sd_allnodes) {
7c16ec58
MT
8475 init_sched_build_groups(cpu_map, cpu_map,
8476 &cpu_to_allnodes_group,
8477 send_covered, tmpmask);
8478 }
9c1cfda2 8479
076ac2af 8480 for (i = 0; i < nr_node_ids; i++) {
9c1cfda2
JH
8481 /* Set up node groups */
8482 struct sched_group *sg, *prev;
9c1cfda2
JH
8483 int j;
8484
96f874e2 8485 cpumask_clear(covered);
6ca09dfc 8486 cpumask_and(nodemask, cpumask_of_node(i), cpu_map);
96f874e2 8487 if (cpumask_empty(nodemask)) {
d1b55138 8488 sched_group_nodes[i] = NULL;
9c1cfda2 8489 continue;
d1b55138 8490 }
9c1cfda2 8491
4bdbaad3 8492 sched_domain_node_span(i, domainspan);
96f874e2 8493 cpumask_and(domainspan, domainspan, cpu_map);
9c1cfda2 8494
6c99e9ad
RR
8495 sg = kmalloc_node(sizeof(struct sched_group) + cpumask_size(),
8496 GFP_KERNEL, i);
51888ca2
SV
8497 if (!sg) {
8498 printk(KERN_WARNING "Can not alloc domain group for "
8499 "node %d\n", i);
8500 goto error;
8501 }
9c1cfda2 8502 sched_group_nodes[i] = sg;
abcd083a 8503 for_each_cpu(j, nodemask) {
9c1cfda2 8504 struct sched_domain *sd;
9761eea8 8505
62ea9ceb 8506 sd = &per_cpu(node_domains, j).sd;
9c1cfda2 8507 sd->groups = sg;
9c1cfda2 8508 }
5517d86b 8509 sg->__cpu_power = 0;
758b2cdc 8510 cpumask_copy(sched_group_cpus(sg), nodemask);
51888ca2 8511 sg->next = sg;
96f874e2 8512 cpumask_or(covered, covered, nodemask);
9c1cfda2
JH
8513 prev = sg;
8514
076ac2af 8515 for (j = 0; j < nr_node_ids; j++) {
076ac2af 8516 int n = (i + j) % nr_node_ids;
9c1cfda2 8517
96f874e2
RR
8518 cpumask_complement(notcovered, covered);
8519 cpumask_and(tmpmask, notcovered, cpu_map);
8520 cpumask_and(tmpmask, tmpmask, domainspan);
8521 if (cpumask_empty(tmpmask))
9c1cfda2
JH
8522 break;
8523
6ca09dfc 8524 cpumask_and(tmpmask, tmpmask, cpumask_of_node(n));
96f874e2 8525 if (cpumask_empty(tmpmask))
9c1cfda2
JH
8526 continue;
8527
6c99e9ad
RR
8528 sg = kmalloc_node(sizeof(struct sched_group) +
8529 cpumask_size(),
15f0b676 8530 GFP_KERNEL, i);
9c1cfda2
JH
8531 if (!sg) {
8532 printk(KERN_WARNING
8533 "Can not alloc domain group for node %d\n", j);
51888ca2 8534 goto error;
9c1cfda2 8535 }
5517d86b 8536 sg->__cpu_power = 0;
758b2cdc 8537 cpumask_copy(sched_group_cpus(sg), tmpmask);
51888ca2 8538 sg->next = prev->next;
96f874e2 8539 cpumask_or(covered, covered, tmpmask);
9c1cfda2
JH
8540 prev->next = sg;
8541 prev = sg;
8542 }
9c1cfda2 8543 }
1da177e4
LT
8544#endif
8545
8546 /* Calculate CPU power for physical packages and nodes */
5c45bf27 8547#ifdef CONFIG_SCHED_SMT
abcd083a 8548 for_each_cpu(i, cpu_map) {
6c99e9ad 8549 struct sched_domain *sd = &per_cpu(cpu_domains, i).sd;
dd41f596 8550
89c4710e 8551 init_sched_groups_power(i, sd);
5c45bf27 8552 }
1da177e4 8553#endif
1e9f28fa 8554#ifdef CONFIG_SCHED_MC
abcd083a 8555 for_each_cpu(i, cpu_map) {
6c99e9ad 8556 struct sched_domain *sd = &per_cpu(core_domains, i).sd;
dd41f596 8557
89c4710e 8558 init_sched_groups_power(i, sd);
5c45bf27
SS
8559 }
8560#endif
1e9f28fa 8561
abcd083a 8562 for_each_cpu(i, cpu_map) {
6c99e9ad 8563 struct sched_domain *sd = &per_cpu(phys_domains, i).sd;
dd41f596 8564
89c4710e 8565 init_sched_groups_power(i, sd);
1da177e4
LT
8566 }
8567
9c1cfda2 8568#ifdef CONFIG_NUMA
076ac2af 8569 for (i = 0; i < nr_node_ids; i++)
08069033 8570 init_numa_sched_groups_power(sched_group_nodes[i]);
9c1cfda2 8571
6711cab4
SS
8572 if (sd_allnodes) {
8573 struct sched_group *sg;
f712c0c7 8574
96f874e2 8575 cpu_to_allnodes_group(cpumask_first(cpu_map), cpu_map, &sg,
7c16ec58 8576 tmpmask);
f712c0c7
SS
8577 init_numa_sched_groups_power(sg);
8578 }
9c1cfda2
JH
8579#endif
8580
1da177e4 8581 /* Attach the domains */
abcd083a 8582 for_each_cpu(i, cpu_map) {
1da177e4
LT
8583 struct sched_domain *sd;
8584#ifdef CONFIG_SCHED_SMT
6c99e9ad 8585 sd = &per_cpu(cpu_domains, i).sd;
1e9f28fa 8586#elif defined(CONFIG_SCHED_MC)
6c99e9ad 8587 sd = &per_cpu(core_domains, i).sd;
1da177e4 8588#else
6c99e9ad 8589 sd = &per_cpu(phys_domains, i).sd;
1da177e4 8590#endif
57d885fe 8591 cpu_attach_domain(sd, rd, i);
1da177e4 8592 }
51888ca2 8593
3404c8d9
RR
8594 err = 0;
8595
8596free_tmpmask:
8597 free_cpumask_var(tmpmask);
8598free_send_covered:
8599 free_cpumask_var(send_covered);
8600free_this_core_map:
8601 free_cpumask_var(this_core_map);
8602free_this_sibling_map:
8603 free_cpumask_var(this_sibling_map);
8604free_nodemask:
8605 free_cpumask_var(nodemask);
8606free_notcovered:
8607#ifdef CONFIG_NUMA
8608 free_cpumask_var(notcovered);
8609free_covered:
8610 free_cpumask_var(covered);
8611free_domainspan:
8612 free_cpumask_var(domainspan);
8613out:
8614#endif
8615 return err;
8616
8617free_sched_groups:
8618#ifdef CONFIG_NUMA
8619 kfree(sched_group_nodes);
8620#endif
8621 goto free_tmpmask;
51888ca2 8622
a616058b 8623#ifdef CONFIG_NUMA
51888ca2 8624error:
7c16ec58 8625 free_sched_groups(cpu_map, tmpmask);
c6c4927b 8626 free_rootdomain(rd);
3404c8d9 8627 goto free_tmpmask;
a616058b 8628#endif
1da177e4 8629}
029190c5 8630
96f874e2 8631static int build_sched_domains(const struct cpumask *cpu_map)
1d3504fc
HS
8632{
8633 return __build_sched_domains(cpu_map, NULL);
8634}
8635
96f874e2 8636static struct cpumask *doms_cur; /* current sched domains */
029190c5 8637static int ndoms_cur; /* number of sched domains in 'doms_cur' */
4285f594
IM
8638static struct sched_domain_attr *dattr_cur;
8639 /* attribues of custom domains in 'doms_cur' */
029190c5
PJ
8640
8641/*
8642 * Special case: If a kmalloc of a doms_cur partition (array of
4212823f
RR
8643 * cpumask) fails, then fallback to a single sched domain,
8644 * as determined by the single cpumask fallback_doms.
029190c5 8645 */
4212823f 8646static cpumask_var_t fallback_doms;
029190c5 8647
ee79d1bd
HC
8648/*
8649 * arch_update_cpu_topology lets virtualized architectures update the
8650 * cpu core maps. It is supposed to return 1 if the topology changed
8651 * or 0 if it stayed the same.
8652 */
8653int __attribute__((weak)) arch_update_cpu_topology(void)
22e52b07 8654{
ee79d1bd 8655 return 0;
22e52b07
HC
8656}
8657
1a20ff27 8658/*
41a2d6cf 8659 * Set up scheduler domains and groups. Callers must hold the hotplug lock.
029190c5
PJ
8660 * For now this just excludes isolated cpus, but could be used to
8661 * exclude other special cases in the future.
1a20ff27 8662 */
96f874e2 8663static int arch_init_sched_domains(const struct cpumask *cpu_map)
1a20ff27 8664{
7378547f
MM
8665 int err;
8666
22e52b07 8667 arch_update_cpu_topology();
029190c5 8668 ndoms_cur = 1;
96f874e2 8669 doms_cur = kmalloc(cpumask_size(), GFP_KERNEL);
029190c5 8670 if (!doms_cur)
4212823f 8671 doms_cur = fallback_doms;
dcc30a35 8672 cpumask_andnot(doms_cur, cpu_map, cpu_isolated_map);
1d3504fc 8673 dattr_cur = NULL;
7378547f 8674 err = build_sched_domains(doms_cur);
6382bc90 8675 register_sched_domain_sysctl();
7378547f
MM
8676
8677 return err;
1a20ff27
DG
8678}
8679
96f874e2
RR
8680static void arch_destroy_sched_domains(const struct cpumask *cpu_map,
8681 struct cpumask *tmpmask)
1da177e4 8682{
7c16ec58 8683 free_sched_groups(cpu_map, tmpmask);
9c1cfda2 8684}
1da177e4 8685
1a20ff27
DG
8686/*
8687 * Detach sched domains from a group of cpus specified in cpu_map
8688 * These cpus will now be attached to the NULL domain
8689 */
96f874e2 8690static void detach_destroy_domains(const struct cpumask *cpu_map)
1a20ff27 8691{
96f874e2
RR
8692 /* Save because hotplug lock held. */
8693 static DECLARE_BITMAP(tmpmask, CONFIG_NR_CPUS);
1a20ff27
DG
8694 int i;
8695
abcd083a 8696 for_each_cpu(i, cpu_map)
57d885fe 8697 cpu_attach_domain(NULL, &def_root_domain, i);
1a20ff27 8698 synchronize_sched();
96f874e2 8699 arch_destroy_sched_domains(cpu_map, to_cpumask(tmpmask));
1a20ff27
DG
8700}
8701
1d3504fc
HS
8702/* handle null as "default" */
8703static int dattrs_equal(struct sched_domain_attr *cur, int idx_cur,
8704 struct sched_domain_attr *new, int idx_new)
8705{
8706 struct sched_domain_attr tmp;
8707
8708 /* fast path */
8709 if (!new && !cur)
8710 return 1;
8711
8712 tmp = SD_ATTR_INIT;
8713 return !memcmp(cur ? (cur + idx_cur) : &tmp,
8714 new ? (new + idx_new) : &tmp,
8715 sizeof(struct sched_domain_attr));
8716}
8717
029190c5
PJ
8718/*
8719 * Partition sched domains as specified by the 'ndoms_new'
41a2d6cf 8720 * cpumasks in the array doms_new[] of cpumasks. This compares
029190c5
PJ
8721 * doms_new[] to the current sched domain partitioning, doms_cur[].
8722 * It destroys each deleted domain and builds each new domain.
8723 *
96f874e2 8724 * 'doms_new' is an array of cpumask's of length 'ndoms_new'.
41a2d6cf
IM
8725 * The masks don't intersect (don't overlap.) We should setup one
8726 * sched domain for each mask. CPUs not in any of the cpumasks will
8727 * not be load balanced. If the same cpumask appears both in the
029190c5
PJ
8728 * current 'doms_cur' domains and in the new 'doms_new', we can leave
8729 * it as it is.
8730 *
41a2d6cf
IM
8731 * The passed in 'doms_new' should be kmalloc'd. This routine takes
8732 * ownership of it and will kfree it when done with it. If the caller
700018e0
LZ
8733 * failed the kmalloc call, then it can pass in doms_new == NULL &&
8734 * ndoms_new == 1, and partition_sched_domains() will fallback to
8735 * the single partition 'fallback_doms', it also forces the domains
8736 * to be rebuilt.
029190c5 8737 *
96f874e2 8738 * If doms_new == NULL it will be replaced with cpu_online_mask.
700018e0
LZ
8739 * ndoms_new == 0 is a special case for destroying existing domains,
8740 * and it will not create the default domain.
dfb512ec 8741 *
029190c5
PJ
8742 * Call with hotplug lock held
8743 */
96f874e2
RR
8744/* FIXME: Change to struct cpumask *doms_new[] */
8745void partition_sched_domains(int ndoms_new, struct cpumask *doms_new,
1d3504fc 8746 struct sched_domain_attr *dattr_new)
029190c5 8747{
dfb512ec 8748 int i, j, n;
d65bd5ec 8749 int new_topology;
029190c5 8750
712555ee 8751 mutex_lock(&sched_domains_mutex);
a1835615 8752
7378547f
MM
8753 /* always unregister in case we don't destroy any domains */
8754 unregister_sched_domain_sysctl();
8755
d65bd5ec
HC
8756 /* Let architecture update cpu core mappings. */
8757 new_topology = arch_update_cpu_topology();
8758
dfb512ec 8759 n = doms_new ? ndoms_new : 0;
029190c5
PJ
8760
8761 /* Destroy deleted domains */
8762 for (i = 0; i < ndoms_cur; i++) {
d65bd5ec 8763 for (j = 0; j < n && !new_topology; j++) {
96f874e2 8764 if (cpumask_equal(&doms_cur[i], &doms_new[j])
1d3504fc 8765 && dattrs_equal(dattr_cur, i, dattr_new, j))
029190c5
PJ
8766 goto match1;
8767 }
8768 /* no match - a current sched domain not in new doms_new[] */
8769 detach_destroy_domains(doms_cur + i);
8770match1:
8771 ;
8772 }
8773
e761b772
MK
8774 if (doms_new == NULL) {
8775 ndoms_cur = 0;
4212823f 8776 doms_new = fallback_doms;
dcc30a35 8777 cpumask_andnot(&doms_new[0], cpu_online_mask, cpu_isolated_map);
faa2f98f 8778 WARN_ON_ONCE(dattr_new);
e761b772
MK
8779 }
8780
029190c5
PJ
8781 /* Build new domains */
8782 for (i = 0; i < ndoms_new; i++) {
d65bd5ec 8783 for (j = 0; j < ndoms_cur && !new_topology; j++) {
96f874e2 8784 if (cpumask_equal(&doms_new[i], &doms_cur[j])
1d3504fc 8785 && dattrs_equal(dattr_new, i, dattr_cur, j))
029190c5
PJ
8786 goto match2;
8787 }
8788 /* no match - add a new doms_new */
1d3504fc
HS
8789 __build_sched_domains(doms_new + i,
8790 dattr_new ? dattr_new + i : NULL);
029190c5
PJ
8791match2:
8792 ;
8793 }
8794
8795 /* Remember the new sched domains */
4212823f 8796 if (doms_cur != fallback_doms)
029190c5 8797 kfree(doms_cur);
1d3504fc 8798 kfree(dattr_cur); /* kfree(NULL) is safe */
029190c5 8799 doms_cur = doms_new;
1d3504fc 8800 dattr_cur = dattr_new;
029190c5 8801 ndoms_cur = ndoms_new;
7378547f
MM
8802
8803 register_sched_domain_sysctl();
a1835615 8804
712555ee 8805 mutex_unlock(&sched_domains_mutex);
029190c5
PJ
8806}
8807
5c45bf27 8808#if defined(CONFIG_SCHED_MC) || defined(CONFIG_SCHED_SMT)
c70f22d2 8809static void arch_reinit_sched_domains(void)
5c45bf27 8810{
95402b38 8811 get_online_cpus();
dfb512ec
MK
8812
8813 /* Destroy domains first to force the rebuild */
8814 partition_sched_domains(0, NULL, NULL);
8815
e761b772 8816 rebuild_sched_domains();
95402b38 8817 put_online_cpus();
5c45bf27
SS
8818}
8819
8820static ssize_t sched_power_savings_store(const char *buf, size_t count, int smt)
8821{
afb8a9b7 8822 unsigned int level = 0;
5c45bf27 8823
afb8a9b7
GS
8824 if (sscanf(buf, "%u", &level) != 1)
8825 return -EINVAL;
8826
8827 /*
8828 * level is always be positive so don't check for
8829 * level < POWERSAVINGS_BALANCE_NONE which is 0
8830 * What happens on 0 or 1 byte write,
8831 * need to check for count as well?
8832 */
8833
8834 if (level >= MAX_POWERSAVINGS_BALANCE_LEVELS)
5c45bf27
SS
8835 return -EINVAL;
8836
8837 if (smt)
afb8a9b7 8838 sched_smt_power_savings = level;
5c45bf27 8839 else
afb8a9b7 8840 sched_mc_power_savings = level;
5c45bf27 8841
c70f22d2 8842 arch_reinit_sched_domains();
5c45bf27 8843
c70f22d2 8844 return count;
5c45bf27
SS
8845}
8846
5c45bf27 8847#ifdef CONFIG_SCHED_MC
f718cd4a
AK
8848static ssize_t sched_mc_power_savings_show(struct sysdev_class *class,
8849 char *page)
5c45bf27
SS
8850{
8851 return sprintf(page, "%u\n", sched_mc_power_savings);
8852}
f718cd4a 8853static ssize_t sched_mc_power_savings_store(struct sysdev_class *class,
48f24c4d 8854 const char *buf, size_t count)
5c45bf27
SS
8855{
8856 return sched_power_savings_store(buf, count, 0);
8857}
f718cd4a
AK
8858static SYSDEV_CLASS_ATTR(sched_mc_power_savings, 0644,
8859 sched_mc_power_savings_show,
8860 sched_mc_power_savings_store);
5c45bf27
SS
8861#endif
8862
8863#ifdef CONFIG_SCHED_SMT
f718cd4a
AK
8864static ssize_t sched_smt_power_savings_show(struct sysdev_class *dev,
8865 char *page)
5c45bf27
SS
8866{
8867 return sprintf(page, "%u\n", sched_smt_power_savings);
8868}
f718cd4a 8869static ssize_t sched_smt_power_savings_store(struct sysdev_class *dev,
48f24c4d 8870 const char *buf, size_t count)
5c45bf27
SS
8871{
8872 return sched_power_savings_store(buf, count, 1);
8873}
f718cd4a
AK
8874static SYSDEV_CLASS_ATTR(sched_smt_power_savings, 0644,
8875 sched_smt_power_savings_show,
6707de00
AB
8876 sched_smt_power_savings_store);
8877#endif
8878
39aac648 8879int __init sched_create_sysfs_power_savings_entries(struct sysdev_class *cls)
6707de00
AB
8880{
8881 int err = 0;
8882
8883#ifdef CONFIG_SCHED_SMT
8884 if (smt_capable())
8885 err = sysfs_create_file(&cls->kset.kobj,
8886 &attr_sched_smt_power_savings.attr);
8887#endif
8888#ifdef CONFIG_SCHED_MC
8889 if (!err && mc_capable())
8890 err = sysfs_create_file(&cls->kset.kobj,
8891 &attr_sched_mc_power_savings.attr);
8892#endif
8893 return err;
8894}
6d6bc0ad 8895#endif /* CONFIG_SCHED_MC || CONFIG_SCHED_SMT */
5c45bf27 8896
e761b772 8897#ifndef CONFIG_CPUSETS
1da177e4 8898/*
e761b772
MK
8899 * Add online and remove offline CPUs from the scheduler domains.
8900 * When cpusets are enabled they take over this function.
1da177e4
LT
8901 */
8902static int update_sched_domains(struct notifier_block *nfb,
8903 unsigned long action, void *hcpu)
e761b772
MK
8904{
8905 switch (action) {
8906 case CPU_ONLINE:
8907 case CPU_ONLINE_FROZEN:
8908 case CPU_DEAD:
8909 case CPU_DEAD_FROZEN:
dfb512ec 8910 partition_sched_domains(1, NULL, NULL);
e761b772
MK
8911 return NOTIFY_OK;
8912
8913 default:
8914 return NOTIFY_DONE;
8915 }
8916}
8917#endif
8918
8919static int update_runtime(struct notifier_block *nfb,
8920 unsigned long action, void *hcpu)
1da177e4 8921{
7def2be1
PZ
8922 int cpu = (int)(long)hcpu;
8923
1da177e4 8924 switch (action) {
1da177e4 8925 case CPU_DOWN_PREPARE:
8bb78442 8926 case CPU_DOWN_PREPARE_FROZEN:
7def2be1 8927 disable_runtime(cpu_rq(cpu));
1da177e4
LT
8928 return NOTIFY_OK;
8929
1da177e4 8930 case CPU_DOWN_FAILED:
8bb78442 8931 case CPU_DOWN_FAILED_FROZEN:
1da177e4 8932 case CPU_ONLINE:
8bb78442 8933 case CPU_ONLINE_FROZEN:
7def2be1 8934 enable_runtime(cpu_rq(cpu));
e761b772
MK
8935 return NOTIFY_OK;
8936
1da177e4
LT
8937 default:
8938 return NOTIFY_DONE;
8939 }
1da177e4 8940}
1da177e4
LT
8941
8942void __init sched_init_smp(void)
8943{
dcc30a35
RR
8944 cpumask_var_t non_isolated_cpus;
8945
8946 alloc_cpumask_var(&non_isolated_cpus, GFP_KERNEL);
5c1e1767 8947
434d53b0
MT
8948#if defined(CONFIG_NUMA)
8949 sched_group_nodes_bycpu = kzalloc(nr_cpu_ids * sizeof(void **),
8950 GFP_KERNEL);
8951 BUG_ON(sched_group_nodes_bycpu == NULL);
8952#endif
95402b38 8953 get_online_cpus();
712555ee 8954 mutex_lock(&sched_domains_mutex);
dcc30a35
RR
8955 arch_init_sched_domains(cpu_online_mask);
8956 cpumask_andnot(non_isolated_cpus, cpu_possible_mask, cpu_isolated_map);
8957 if (cpumask_empty(non_isolated_cpus))
8958 cpumask_set_cpu(smp_processor_id(), non_isolated_cpus);
712555ee 8959 mutex_unlock(&sched_domains_mutex);
95402b38 8960 put_online_cpus();
e761b772
MK
8961
8962#ifndef CONFIG_CPUSETS
1da177e4
LT
8963 /* XXX: Theoretical race here - CPU may be hotplugged now */
8964 hotcpu_notifier(update_sched_domains, 0);
e761b772
MK
8965#endif
8966
8967 /* RT runtime code needs to handle some hotplug events */
8968 hotcpu_notifier(update_runtime, 0);
8969
b328ca18 8970 init_hrtick();
5c1e1767
NP
8971
8972 /* Move init over to a non-isolated CPU */
dcc30a35 8973 if (set_cpus_allowed_ptr(current, non_isolated_cpus) < 0)
5c1e1767 8974 BUG();
19978ca6 8975 sched_init_granularity();
dcc30a35 8976 free_cpumask_var(non_isolated_cpus);
4212823f
RR
8977
8978 alloc_cpumask_var(&fallback_doms, GFP_KERNEL);
0e3900e6 8979 init_sched_rt_class();
1da177e4
LT
8980}
8981#else
8982void __init sched_init_smp(void)
8983{
19978ca6 8984 sched_init_granularity();
1da177e4
LT
8985}
8986#endif /* CONFIG_SMP */
8987
8988int in_sched_functions(unsigned long addr)
8989{
1da177e4
LT
8990 return in_lock_functions(addr) ||
8991 (addr >= (unsigned long)__sched_text_start
8992 && addr < (unsigned long)__sched_text_end);
8993}
8994
a9957449 8995static void init_cfs_rq(struct cfs_rq *cfs_rq, struct rq *rq)
dd41f596
IM
8996{
8997 cfs_rq->tasks_timeline = RB_ROOT;
4a55bd5e 8998 INIT_LIST_HEAD(&cfs_rq->tasks);
dd41f596
IM
8999#ifdef CONFIG_FAIR_GROUP_SCHED
9000 cfs_rq->rq = rq;
9001#endif
67e9fb2a 9002 cfs_rq->min_vruntime = (u64)(-(1LL << 20));
dd41f596
IM
9003}
9004
fa85ae24
PZ
9005static void init_rt_rq(struct rt_rq *rt_rq, struct rq *rq)
9006{
9007 struct rt_prio_array *array;
9008 int i;
9009
9010 array = &rt_rq->active;
9011 for (i = 0; i < MAX_RT_PRIO; i++) {
9012 INIT_LIST_HEAD(array->queue + i);
9013 __clear_bit(i, array->bitmap);
9014 }
9015 /* delimiter for bitsearch: */
9016 __set_bit(MAX_RT_PRIO, array->bitmap);
9017
052f1dc7 9018#if defined CONFIG_SMP || defined CONFIG_RT_GROUP_SCHED
e864c499 9019 rt_rq->highest_prio.curr = MAX_RT_PRIO;
398a153b 9020#ifdef CONFIG_SMP
e864c499 9021 rt_rq->highest_prio.next = MAX_RT_PRIO;
48d5e258 9022#endif
48d5e258 9023#endif
fa85ae24
PZ
9024#ifdef CONFIG_SMP
9025 rt_rq->rt_nr_migratory = 0;
fa85ae24 9026 rt_rq->overloaded = 0;
917b627d 9027 plist_head_init(&rq->rt.pushable_tasks, &rq->lock);
fa85ae24
PZ
9028#endif
9029
9030 rt_rq->rt_time = 0;
9031 rt_rq->rt_throttled = 0;
ac086bc2
PZ
9032 rt_rq->rt_runtime = 0;
9033 spin_lock_init(&rt_rq->rt_runtime_lock);
6f505b16 9034
052f1dc7 9035#ifdef CONFIG_RT_GROUP_SCHED
23b0fdfc 9036 rt_rq->rt_nr_boosted = 0;
6f505b16
PZ
9037 rt_rq->rq = rq;
9038#endif
fa85ae24
PZ
9039}
9040
6f505b16 9041#ifdef CONFIG_FAIR_GROUP_SCHED
ec7dc8ac
DG
9042static void init_tg_cfs_entry(struct task_group *tg, struct cfs_rq *cfs_rq,
9043 struct sched_entity *se, int cpu, int add,
9044 struct sched_entity *parent)
6f505b16 9045{
ec7dc8ac 9046 struct rq *rq = cpu_rq(cpu);
6f505b16
PZ
9047 tg->cfs_rq[cpu] = cfs_rq;
9048 init_cfs_rq(cfs_rq, rq);
9049 cfs_rq->tg = tg;
9050 if (add)
9051 list_add(&cfs_rq->leaf_cfs_rq_list, &rq->leaf_cfs_rq_list);
9052
9053 tg->se[cpu] = se;
354d60c2
DG
9054 /* se could be NULL for init_task_group */
9055 if (!se)
9056 return;
9057
ec7dc8ac
DG
9058 if (!parent)
9059 se->cfs_rq = &rq->cfs;
9060 else
9061 se->cfs_rq = parent->my_q;
9062
6f505b16
PZ
9063 se->my_q = cfs_rq;
9064 se->load.weight = tg->shares;
e05510d0 9065 se->load.inv_weight = 0;
ec7dc8ac 9066 se->parent = parent;
6f505b16 9067}
052f1dc7 9068#endif
6f505b16 9069
052f1dc7 9070#ifdef CONFIG_RT_GROUP_SCHED
ec7dc8ac
DG
9071static void init_tg_rt_entry(struct task_group *tg, struct rt_rq *rt_rq,
9072 struct sched_rt_entity *rt_se, int cpu, int add,
9073 struct sched_rt_entity *parent)
6f505b16 9074{
ec7dc8ac
DG
9075 struct rq *rq = cpu_rq(cpu);
9076
6f505b16
PZ
9077 tg->rt_rq[cpu] = rt_rq;
9078 init_rt_rq(rt_rq, rq);
9079 rt_rq->tg = tg;
9080 rt_rq->rt_se = rt_se;
ac086bc2 9081 rt_rq->rt_runtime = tg->rt_bandwidth.rt_runtime;
6f505b16
PZ
9082 if (add)
9083 list_add(&rt_rq->leaf_rt_rq_list, &rq->leaf_rt_rq_list);
9084
9085 tg->rt_se[cpu] = rt_se;
354d60c2
DG
9086 if (!rt_se)
9087 return;
9088
ec7dc8ac
DG
9089 if (!parent)
9090 rt_se->rt_rq = &rq->rt;
9091 else
9092 rt_se->rt_rq = parent->my_q;
9093
6f505b16 9094 rt_se->my_q = rt_rq;
ec7dc8ac 9095 rt_se->parent = parent;
6f505b16
PZ
9096 INIT_LIST_HEAD(&rt_se->run_list);
9097}
9098#endif
9099
1da177e4
LT
9100void __init sched_init(void)
9101{
dd41f596 9102 int i, j;
434d53b0
MT
9103 unsigned long alloc_size = 0, ptr;
9104
9105#ifdef CONFIG_FAIR_GROUP_SCHED
9106 alloc_size += 2 * nr_cpu_ids * sizeof(void **);
9107#endif
9108#ifdef CONFIG_RT_GROUP_SCHED
9109 alloc_size += 2 * nr_cpu_ids * sizeof(void **);
eff766a6
PZ
9110#endif
9111#ifdef CONFIG_USER_SCHED
9112 alloc_size *= 2;
df7c8e84
RR
9113#endif
9114#ifdef CONFIG_CPUMASK_OFFSTACK
8c083f08 9115 alloc_size += num_possible_cpus() * cpumask_size();
434d53b0
MT
9116#endif
9117 /*
9118 * As sched_init() is called before page_alloc is setup,
9119 * we use alloc_bootmem().
9120 */
9121 if (alloc_size) {
36b7b6d4 9122 ptr = (unsigned long)kzalloc(alloc_size, GFP_NOWAIT);
434d53b0
MT
9123
9124#ifdef CONFIG_FAIR_GROUP_SCHED
9125 init_task_group.se = (struct sched_entity **)ptr;
9126 ptr += nr_cpu_ids * sizeof(void **);
9127
9128 init_task_group.cfs_rq = (struct cfs_rq **)ptr;
9129 ptr += nr_cpu_ids * sizeof(void **);
eff766a6
PZ
9130
9131#ifdef CONFIG_USER_SCHED
9132 root_task_group.se = (struct sched_entity **)ptr;
9133 ptr += nr_cpu_ids * sizeof(void **);
9134
9135 root_task_group.cfs_rq = (struct cfs_rq **)ptr;
9136 ptr += nr_cpu_ids * sizeof(void **);
6d6bc0ad
DG
9137#endif /* CONFIG_USER_SCHED */
9138#endif /* CONFIG_FAIR_GROUP_SCHED */
434d53b0
MT
9139#ifdef CONFIG_RT_GROUP_SCHED
9140 init_task_group.rt_se = (struct sched_rt_entity **)ptr;
9141 ptr += nr_cpu_ids * sizeof(void **);
9142
9143 init_task_group.rt_rq = (struct rt_rq **)ptr;
eff766a6
PZ
9144 ptr += nr_cpu_ids * sizeof(void **);
9145
9146#ifdef CONFIG_USER_SCHED
9147 root_task_group.rt_se = (struct sched_rt_entity **)ptr;
9148 ptr += nr_cpu_ids * sizeof(void **);
9149
9150 root_task_group.rt_rq = (struct rt_rq **)ptr;
9151 ptr += nr_cpu_ids * sizeof(void **);
6d6bc0ad
DG
9152#endif /* CONFIG_USER_SCHED */
9153#endif /* CONFIG_RT_GROUP_SCHED */
df7c8e84
RR
9154#ifdef CONFIG_CPUMASK_OFFSTACK
9155 for_each_possible_cpu(i) {
9156 per_cpu(load_balance_tmpmask, i) = (void *)ptr;
9157 ptr += cpumask_size();
9158 }
9159#endif /* CONFIG_CPUMASK_OFFSTACK */
434d53b0 9160 }
dd41f596 9161
57d885fe
GH
9162#ifdef CONFIG_SMP
9163 init_defrootdomain();
9164#endif
9165
d0b27fa7
PZ
9166 init_rt_bandwidth(&def_rt_bandwidth,
9167 global_rt_period(), global_rt_runtime());
9168
9169#ifdef CONFIG_RT_GROUP_SCHED
9170 init_rt_bandwidth(&init_task_group.rt_bandwidth,
9171 global_rt_period(), global_rt_runtime());
eff766a6
PZ
9172#ifdef CONFIG_USER_SCHED
9173 init_rt_bandwidth(&root_task_group.rt_bandwidth,
9174 global_rt_period(), RUNTIME_INF);
6d6bc0ad
DG
9175#endif /* CONFIG_USER_SCHED */
9176#endif /* CONFIG_RT_GROUP_SCHED */
d0b27fa7 9177
052f1dc7 9178#ifdef CONFIG_GROUP_SCHED
6f505b16 9179 list_add(&init_task_group.list, &task_groups);
f473aa5e
PZ
9180 INIT_LIST_HEAD(&init_task_group.children);
9181
9182#ifdef CONFIG_USER_SCHED
9183 INIT_LIST_HEAD(&root_task_group.children);
9184 init_task_group.parent = &root_task_group;
9185 list_add(&init_task_group.siblings, &root_task_group.children);
6d6bc0ad
DG
9186#endif /* CONFIG_USER_SCHED */
9187#endif /* CONFIG_GROUP_SCHED */
6f505b16 9188
0a945022 9189 for_each_possible_cpu(i) {
70b97a7f 9190 struct rq *rq;
1da177e4
LT
9191
9192 rq = cpu_rq(i);
9193 spin_lock_init(&rq->lock);
7897986b 9194 rq->nr_running = 0;
dce48a84
TG
9195 rq->calc_load_active = 0;
9196 rq->calc_load_update = jiffies + LOAD_FREQ;
dd41f596 9197 init_cfs_rq(&rq->cfs, rq);
6f505b16 9198 init_rt_rq(&rq->rt, rq);
dd41f596 9199#ifdef CONFIG_FAIR_GROUP_SCHED
4cf86d77 9200 init_task_group.shares = init_task_group_load;
6f505b16 9201 INIT_LIST_HEAD(&rq->leaf_cfs_rq_list);
354d60c2
DG
9202#ifdef CONFIG_CGROUP_SCHED
9203 /*
9204 * How much cpu bandwidth does init_task_group get?
9205 *
9206 * In case of task-groups formed thr' the cgroup filesystem, it
9207 * gets 100% of the cpu resources in the system. This overall
9208 * system cpu resource is divided among the tasks of
9209 * init_task_group and its child task-groups in a fair manner,
9210 * based on each entity's (task or task-group's) weight
9211 * (se->load.weight).
9212 *
9213 * In other words, if init_task_group has 10 tasks of weight
9214 * 1024) and two child groups A0 and A1 (of weight 1024 each),
9215 * then A0's share of the cpu resource is:
9216 *
9217 * A0's bandwidth = 1024 / (10*1024 + 1024 + 1024) = 8.33%
9218 *
9219 * We achieve this by letting init_task_group's tasks sit
9220 * directly in rq->cfs (i.e init_task_group->se[] = NULL).
9221 */
ec7dc8ac 9222 init_tg_cfs_entry(&init_task_group, &rq->cfs, NULL, i, 1, NULL);
354d60c2 9223#elif defined CONFIG_USER_SCHED
eff766a6
PZ
9224 root_task_group.shares = NICE_0_LOAD;
9225 init_tg_cfs_entry(&root_task_group, &rq->cfs, NULL, i, 0, NULL);
354d60c2
DG
9226 /*
9227 * In case of task-groups formed thr' the user id of tasks,
9228 * init_task_group represents tasks belonging to root user.
9229 * Hence it forms a sibling of all subsequent groups formed.
9230 * In this case, init_task_group gets only a fraction of overall
9231 * system cpu resource, based on the weight assigned to root
9232 * user's cpu share (INIT_TASK_GROUP_LOAD). This is accomplished
9233 * by letting tasks of init_task_group sit in a separate cfs_rq
9234 * (init_cfs_rq) and having one entity represent this group of
9235 * tasks in rq->cfs (i.e init_task_group->se[] != NULL).
9236 */
ec7dc8ac 9237 init_tg_cfs_entry(&init_task_group,
6f505b16 9238 &per_cpu(init_cfs_rq, i),
eff766a6
PZ
9239 &per_cpu(init_sched_entity, i), i, 1,
9240 root_task_group.se[i]);
6f505b16 9241
052f1dc7 9242#endif
354d60c2
DG
9243#endif /* CONFIG_FAIR_GROUP_SCHED */
9244
9245 rq->rt.rt_runtime = def_rt_bandwidth.rt_runtime;
052f1dc7 9246#ifdef CONFIG_RT_GROUP_SCHED
6f505b16 9247 INIT_LIST_HEAD(&rq->leaf_rt_rq_list);
354d60c2 9248#ifdef CONFIG_CGROUP_SCHED
ec7dc8ac 9249 init_tg_rt_entry(&init_task_group, &rq->rt, NULL, i, 1, NULL);
354d60c2 9250#elif defined CONFIG_USER_SCHED
eff766a6 9251 init_tg_rt_entry(&root_task_group, &rq->rt, NULL, i, 0, NULL);
ec7dc8ac 9252 init_tg_rt_entry(&init_task_group,
6f505b16 9253 &per_cpu(init_rt_rq, i),
eff766a6
PZ
9254 &per_cpu(init_sched_rt_entity, i), i, 1,
9255 root_task_group.rt_se[i]);
354d60c2 9256#endif
dd41f596 9257#endif
1da177e4 9258
dd41f596
IM
9259 for (j = 0; j < CPU_LOAD_IDX_MAX; j++)
9260 rq->cpu_load[j] = 0;
1da177e4 9261#ifdef CONFIG_SMP
41c7ce9a 9262 rq->sd = NULL;
57d885fe 9263 rq->rd = NULL;
1da177e4 9264 rq->active_balance = 0;
dd41f596 9265 rq->next_balance = jiffies;
1da177e4 9266 rq->push_cpu = 0;
0a2966b4 9267 rq->cpu = i;
1f11eb6a 9268 rq->online = 0;
1da177e4
LT
9269 rq->migration_thread = NULL;
9270 INIT_LIST_HEAD(&rq->migration_queue);
dc938520 9271 rq_attach_root(rq, &def_root_domain);
1da177e4 9272#endif
8f4d37ec 9273 init_rq_hrtick(rq);
1da177e4 9274 atomic_set(&rq->nr_iowait, 0);
1da177e4
LT
9275 }
9276
2dd73a4f 9277 set_load_weight(&init_task);
b50f60ce 9278
e107be36
AK
9279#ifdef CONFIG_PREEMPT_NOTIFIERS
9280 INIT_HLIST_HEAD(&init_task.preempt_notifiers);
9281#endif
9282
c9819f45 9283#ifdef CONFIG_SMP
962cf36c 9284 open_softirq(SCHED_SOFTIRQ, run_rebalance_domains);
c9819f45
CL
9285#endif
9286
b50f60ce
HC
9287#ifdef CONFIG_RT_MUTEXES
9288 plist_head_init(&init_task.pi_waiters, &init_task.pi_lock);
9289#endif
9290
1da177e4
LT
9291 /*
9292 * The boot idle thread does lazy MMU switching as well:
9293 */
9294 atomic_inc(&init_mm.mm_count);
9295 enter_lazy_tlb(&init_mm, current);
9296
9297 /*
9298 * Make us the idle thread. Technically, schedule() should not be
9299 * called from this thread, however somewhere below it might be,
9300 * but because we are the idle thread, we just pick up running again
9301 * when this runqueue becomes "idle".
9302 */
9303 init_idle(current, smp_processor_id());
dce48a84
TG
9304
9305 calc_load_update = jiffies + LOAD_FREQ;
9306
dd41f596
IM
9307 /*
9308 * During early bootup we pretend to be a normal task:
9309 */
9310 current->sched_class = &fair_sched_class;
6892b75e 9311
6a7b3dc3 9312 /* Allocate the nohz_cpu_mask if CONFIG_CPUMASK_OFFSTACK */
4bdddf8f 9313 alloc_cpumask_var(&nohz_cpu_mask, GFP_NOWAIT);
bf4d83f6 9314#ifdef CONFIG_SMP
7d1e6a9b 9315#ifdef CONFIG_NO_HZ
4bdddf8f
PE
9316 alloc_cpumask_var(&nohz.cpu_mask, GFP_NOWAIT);
9317 alloc_cpumask_var(&nohz.ilb_grp_nohz_mask, GFP_NOWAIT);
7d1e6a9b 9318#endif
4bdddf8f 9319 alloc_cpumask_var(&cpu_isolated_map, GFP_NOWAIT);
bf4d83f6 9320#endif /* SMP */
6a7b3dc3 9321
6892b75e 9322 scheduler_running = 1;
1da177e4
LT
9323}
9324
9325#ifdef CONFIG_DEBUG_SPINLOCK_SLEEP
9326void __might_sleep(char *file, int line)
9327{
48f24c4d 9328#ifdef in_atomic
1da177e4
LT
9329 static unsigned long prev_jiffy; /* ratelimiting */
9330
aef745fc
IM
9331 if ((!in_atomic() && !irqs_disabled()) ||
9332 system_state != SYSTEM_RUNNING || oops_in_progress)
9333 return;
9334 if (time_before(jiffies, prev_jiffy + HZ) && prev_jiffy)
9335 return;
9336 prev_jiffy = jiffies;
9337
9338 printk(KERN_ERR
9339 "BUG: sleeping function called from invalid context at %s:%d\n",
9340 file, line);
9341 printk(KERN_ERR
9342 "in_atomic(): %d, irqs_disabled(): %d, pid: %d, name: %s\n",
9343 in_atomic(), irqs_disabled(),
9344 current->pid, current->comm);
9345
9346 debug_show_held_locks(current);
9347 if (irqs_disabled())
9348 print_irqtrace_events(current);
9349 dump_stack();
1da177e4
LT
9350#endif
9351}
9352EXPORT_SYMBOL(__might_sleep);
9353#endif
9354
9355#ifdef CONFIG_MAGIC_SYSRQ
3a5e4dc1
AK
9356static void normalize_task(struct rq *rq, struct task_struct *p)
9357{
9358 int on_rq;
3e51f33f 9359
3a5e4dc1
AK
9360 update_rq_clock(rq);
9361 on_rq = p->se.on_rq;
9362 if (on_rq)
9363 deactivate_task(rq, p, 0);
9364 __setscheduler(rq, p, SCHED_NORMAL, 0);
9365 if (on_rq) {
9366 activate_task(rq, p, 0);
9367 resched_task(rq->curr);
9368 }
9369}
9370
1da177e4
LT
9371void normalize_rt_tasks(void)
9372{
a0f98a1c 9373 struct task_struct *g, *p;
1da177e4 9374 unsigned long flags;
70b97a7f 9375 struct rq *rq;
1da177e4 9376
4cf5d77a 9377 read_lock_irqsave(&tasklist_lock, flags);
a0f98a1c 9378 do_each_thread(g, p) {
178be793
IM
9379 /*
9380 * Only normalize user tasks:
9381 */
9382 if (!p->mm)
9383 continue;
9384
6cfb0d5d 9385 p->se.exec_start = 0;
6cfb0d5d 9386#ifdef CONFIG_SCHEDSTATS
dd41f596 9387 p->se.wait_start = 0;
dd41f596 9388 p->se.sleep_start = 0;
dd41f596 9389 p->se.block_start = 0;
6cfb0d5d 9390#endif
dd41f596
IM
9391
9392 if (!rt_task(p)) {
9393 /*
9394 * Renice negative nice level userspace
9395 * tasks back to 0:
9396 */
9397 if (TASK_NICE(p) < 0 && p->mm)
9398 set_user_nice(p, 0);
1da177e4 9399 continue;
dd41f596 9400 }
1da177e4 9401
4cf5d77a 9402 spin_lock(&p->pi_lock);
b29739f9 9403 rq = __task_rq_lock(p);
1da177e4 9404
178be793 9405 normalize_task(rq, p);
3a5e4dc1 9406
b29739f9 9407 __task_rq_unlock(rq);
4cf5d77a 9408 spin_unlock(&p->pi_lock);
a0f98a1c
IM
9409 } while_each_thread(g, p);
9410
4cf5d77a 9411 read_unlock_irqrestore(&tasklist_lock, flags);
1da177e4
LT
9412}
9413
9414#endif /* CONFIG_MAGIC_SYSRQ */
1df5c10a
LT
9415
9416#ifdef CONFIG_IA64
9417/*
9418 * These functions are only useful for the IA64 MCA handling.
9419 *
9420 * They can only be called when the whole system has been
9421 * stopped - every CPU needs to be quiescent, and no scheduling
9422 * activity can take place. Using them for anything else would
9423 * be a serious bug, and as a result, they aren't even visible
9424 * under any other configuration.
9425 */
9426
9427/**
9428 * curr_task - return the current task for a given cpu.
9429 * @cpu: the processor in question.
9430 *
9431 * ONLY VALID WHEN THE WHOLE SYSTEM IS STOPPED!
9432 */
36c8b586 9433struct task_struct *curr_task(int cpu)
1df5c10a
LT
9434{
9435 return cpu_curr(cpu);
9436}
9437
9438/**
9439 * set_curr_task - set the current task for a given cpu.
9440 * @cpu: the processor in question.
9441 * @p: the task pointer to set.
9442 *
9443 * Description: This function must only be used when non-maskable interrupts
41a2d6cf
IM
9444 * are serviced on a separate stack. It allows the architecture to switch the
9445 * notion of the current task on a cpu in a non-blocking manner. This function
1df5c10a
LT
9446 * must be called with all CPU's synchronized, and interrupts disabled, the
9447 * and caller must save the original value of the current task (see
9448 * curr_task() above) and restore that value before reenabling interrupts and
9449 * re-starting the system.
9450 *
9451 * ONLY VALID WHEN THE WHOLE SYSTEM IS STOPPED!
9452 */
36c8b586 9453void set_curr_task(int cpu, struct task_struct *p)
1df5c10a
LT
9454{
9455 cpu_curr(cpu) = p;
9456}
9457
9458#endif
29f59db3 9459
bccbe08a
PZ
9460#ifdef CONFIG_FAIR_GROUP_SCHED
9461static void free_fair_sched_group(struct task_group *tg)
6f505b16
PZ
9462{
9463 int i;
9464
9465 for_each_possible_cpu(i) {
9466 if (tg->cfs_rq)
9467 kfree(tg->cfs_rq[i]);
9468 if (tg->se)
9469 kfree(tg->se[i]);
6f505b16
PZ
9470 }
9471
9472 kfree(tg->cfs_rq);
9473 kfree(tg->se);
6f505b16
PZ
9474}
9475
ec7dc8ac
DG
9476static
9477int alloc_fair_sched_group(struct task_group *tg, struct task_group *parent)
29f59db3 9478{
29f59db3 9479 struct cfs_rq *cfs_rq;
eab17229 9480 struct sched_entity *se;
9b5b7751 9481 struct rq *rq;
29f59db3
SV
9482 int i;
9483
434d53b0 9484 tg->cfs_rq = kzalloc(sizeof(cfs_rq) * nr_cpu_ids, GFP_KERNEL);
29f59db3
SV
9485 if (!tg->cfs_rq)
9486 goto err;
434d53b0 9487 tg->se = kzalloc(sizeof(se) * nr_cpu_ids, GFP_KERNEL);
29f59db3
SV
9488 if (!tg->se)
9489 goto err;
052f1dc7
PZ
9490
9491 tg->shares = NICE_0_LOAD;
29f59db3
SV
9492
9493 for_each_possible_cpu(i) {
9b5b7751 9494 rq = cpu_rq(i);
29f59db3 9495
eab17229
LZ
9496 cfs_rq = kzalloc_node(sizeof(struct cfs_rq),
9497 GFP_KERNEL, cpu_to_node(i));
29f59db3
SV
9498 if (!cfs_rq)
9499 goto err;
9500
eab17229
LZ
9501 se = kzalloc_node(sizeof(struct sched_entity),
9502 GFP_KERNEL, cpu_to_node(i));
29f59db3
SV
9503 if (!se)
9504 goto err;
9505
eab17229 9506 init_tg_cfs_entry(tg, cfs_rq, se, i, 0, parent->se[i]);
bccbe08a
PZ
9507 }
9508
9509 return 1;
9510
9511 err:
9512 return 0;
9513}
9514
9515static inline void register_fair_sched_group(struct task_group *tg, int cpu)
9516{
9517 list_add_rcu(&tg->cfs_rq[cpu]->leaf_cfs_rq_list,
9518 &cpu_rq(cpu)->leaf_cfs_rq_list);
9519}
9520
9521static inline void unregister_fair_sched_group(struct task_group *tg, int cpu)
9522{
9523 list_del_rcu(&tg->cfs_rq[cpu]->leaf_cfs_rq_list);
9524}
6d6bc0ad 9525#else /* !CONFG_FAIR_GROUP_SCHED */
bccbe08a
PZ
9526static inline void free_fair_sched_group(struct task_group *tg)
9527{
9528}
9529
ec7dc8ac
DG
9530static inline
9531int alloc_fair_sched_group(struct task_group *tg, struct task_group *parent)
bccbe08a
PZ
9532{
9533 return 1;
9534}
9535
9536static inline void register_fair_sched_group(struct task_group *tg, int cpu)
9537{
9538}
9539
9540static inline void unregister_fair_sched_group(struct task_group *tg, int cpu)
9541{
9542}
6d6bc0ad 9543#endif /* CONFIG_FAIR_GROUP_SCHED */
052f1dc7
PZ
9544
9545#ifdef CONFIG_RT_GROUP_SCHED
bccbe08a
PZ
9546static void free_rt_sched_group(struct task_group *tg)
9547{
9548 int i;
9549
d0b27fa7
PZ
9550 destroy_rt_bandwidth(&tg->rt_bandwidth);
9551
bccbe08a
PZ
9552 for_each_possible_cpu(i) {
9553 if (tg->rt_rq)
9554 kfree(tg->rt_rq[i]);
9555 if (tg->rt_se)
9556 kfree(tg->rt_se[i]);
9557 }
9558
9559 kfree(tg->rt_rq);
9560 kfree(tg->rt_se);
9561}
9562
ec7dc8ac
DG
9563static
9564int alloc_rt_sched_group(struct task_group *tg, struct task_group *parent)
bccbe08a
PZ
9565{
9566 struct rt_rq *rt_rq;
eab17229 9567 struct sched_rt_entity *rt_se;
bccbe08a
PZ
9568 struct rq *rq;
9569 int i;
9570
434d53b0 9571 tg->rt_rq = kzalloc(sizeof(rt_rq) * nr_cpu_ids, GFP_KERNEL);
bccbe08a
PZ
9572 if (!tg->rt_rq)
9573 goto err;
434d53b0 9574 tg->rt_se = kzalloc(sizeof(rt_se) * nr_cpu_ids, GFP_KERNEL);
bccbe08a
PZ
9575 if (!tg->rt_se)
9576 goto err;
9577
d0b27fa7
PZ
9578 init_rt_bandwidth(&tg->rt_bandwidth,
9579 ktime_to_ns(def_rt_bandwidth.rt_period), 0);
bccbe08a
PZ
9580
9581 for_each_possible_cpu(i) {
9582 rq = cpu_rq(i);
9583
eab17229
LZ
9584 rt_rq = kzalloc_node(sizeof(struct rt_rq),
9585 GFP_KERNEL, cpu_to_node(i));
6f505b16
PZ
9586 if (!rt_rq)
9587 goto err;
29f59db3 9588
eab17229
LZ
9589 rt_se = kzalloc_node(sizeof(struct sched_rt_entity),
9590 GFP_KERNEL, cpu_to_node(i));
6f505b16
PZ
9591 if (!rt_se)
9592 goto err;
29f59db3 9593
eab17229 9594 init_tg_rt_entry(tg, rt_rq, rt_se, i, 0, parent->rt_se[i]);
29f59db3
SV
9595 }
9596
bccbe08a
PZ
9597 return 1;
9598
9599 err:
9600 return 0;
9601}
9602
9603static inline void register_rt_sched_group(struct task_group *tg, int cpu)
9604{
9605 list_add_rcu(&tg->rt_rq[cpu]->leaf_rt_rq_list,
9606 &cpu_rq(cpu)->leaf_rt_rq_list);
9607}
9608
9609static inline void unregister_rt_sched_group(struct task_group *tg, int cpu)
9610{
9611 list_del_rcu(&tg->rt_rq[cpu]->leaf_rt_rq_list);
9612}
6d6bc0ad 9613#else /* !CONFIG_RT_GROUP_SCHED */
bccbe08a
PZ
9614static inline void free_rt_sched_group(struct task_group *tg)
9615{
9616}
9617
ec7dc8ac
DG
9618static inline
9619int alloc_rt_sched_group(struct task_group *tg, struct task_group *parent)
bccbe08a
PZ
9620{
9621 return 1;
9622}
9623
9624static inline void register_rt_sched_group(struct task_group *tg, int cpu)
9625{
9626}
9627
9628static inline void unregister_rt_sched_group(struct task_group *tg, int cpu)
9629{
9630}
6d6bc0ad 9631#endif /* CONFIG_RT_GROUP_SCHED */
bccbe08a 9632
d0b27fa7 9633#ifdef CONFIG_GROUP_SCHED
bccbe08a
PZ
9634static void free_sched_group(struct task_group *tg)
9635{
9636 free_fair_sched_group(tg);
9637 free_rt_sched_group(tg);
9638 kfree(tg);
9639}
9640
9641/* allocate runqueue etc for a new task group */
ec7dc8ac 9642struct task_group *sched_create_group(struct task_group *parent)
bccbe08a
PZ
9643{
9644 struct task_group *tg;
9645 unsigned long flags;
9646 int i;
9647
9648 tg = kzalloc(sizeof(*tg), GFP_KERNEL);
9649 if (!tg)
9650 return ERR_PTR(-ENOMEM);
9651
ec7dc8ac 9652 if (!alloc_fair_sched_group(tg, parent))
bccbe08a
PZ
9653 goto err;
9654
ec7dc8ac 9655 if (!alloc_rt_sched_group(tg, parent))
bccbe08a
PZ
9656 goto err;
9657
8ed36996 9658 spin_lock_irqsave(&task_group_lock, flags);
9b5b7751 9659 for_each_possible_cpu(i) {
bccbe08a
PZ
9660 register_fair_sched_group(tg, i);
9661 register_rt_sched_group(tg, i);
9b5b7751 9662 }
6f505b16 9663 list_add_rcu(&tg->list, &task_groups);
f473aa5e
PZ
9664
9665 WARN_ON(!parent); /* root should already exist */
9666
9667 tg->parent = parent;
f473aa5e 9668 INIT_LIST_HEAD(&tg->children);
09f2724a 9669 list_add_rcu(&tg->siblings, &parent->children);
8ed36996 9670 spin_unlock_irqrestore(&task_group_lock, flags);
29f59db3 9671
9b5b7751 9672 return tg;
29f59db3
SV
9673
9674err:
6f505b16 9675 free_sched_group(tg);
29f59db3
SV
9676 return ERR_PTR(-ENOMEM);
9677}
9678
9b5b7751 9679/* rcu callback to free various structures associated with a task group */
6f505b16 9680static void free_sched_group_rcu(struct rcu_head *rhp)
29f59db3 9681{
29f59db3 9682 /* now it should be safe to free those cfs_rqs */
6f505b16 9683 free_sched_group(container_of(rhp, struct task_group, rcu));
29f59db3
SV
9684}
9685
9b5b7751 9686/* Destroy runqueue etc associated with a task group */
4cf86d77 9687void sched_destroy_group(struct task_group *tg)
29f59db3 9688{
8ed36996 9689 unsigned long flags;
9b5b7751 9690 int i;
29f59db3 9691
8ed36996 9692 spin_lock_irqsave(&task_group_lock, flags);
9b5b7751 9693 for_each_possible_cpu(i) {
bccbe08a
PZ
9694 unregister_fair_sched_group(tg, i);
9695 unregister_rt_sched_group(tg, i);
9b5b7751 9696 }
6f505b16 9697 list_del_rcu(&tg->list);
f473aa5e 9698 list_del_rcu(&tg->siblings);
8ed36996 9699 spin_unlock_irqrestore(&task_group_lock, flags);
9b5b7751 9700
9b5b7751 9701 /* wait for possible concurrent references to cfs_rqs complete */
6f505b16 9702 call_rcu(&tg->rcu, free_sched_group_rcu);
29f59db3
SV
9703}
9704
9b5b7751 9705/* change task's runqueue when it moves between groups.
3a252015
IM
9706 * The caller of this function should have put the task in its new group
9707 * by now. This function just updates tsk->se.cfs_rq and tsk->se.parent to
9708 * reflect its new group.
9b5b7751
SV
9709 */
9710void sched_move_task(struct task_struct *tsk)
29f59db3
SV
9711{
9712 int on_rq, running;
9713 unsigned long flags;
9714 struct rq *rq;
9715
9716 rq = task_rq_lock(tsk, &flags);
9717
29f59db3
SV
9718 update_rq_clock(rq);
9719
051a1d1a 9720 running = task_current(rq, tsk);
29f59db3
SV
9721 on_rq = tsk->se.on_rq;
9722
0e1f3483 9723 if (on_rq)
29f59db3 9724 dequeue_task(rq, tsk, 0);
0e1f3483
HS
9725 if (unlikely(running))
9726 tsk->sched_class->put_prev_task(rq, tsk);
29f59db3 9727
6f505b16 9728 set_task_rq(tsk, task_cpu(tsk));
29f59db3 9729
810b3817
PZ
9730#ifdef CONFIG_FAIR_GROUP_SCHED
9731 if (tsk->sched_class->moved_group)
9732 tsk->sched_class->moved_group(tsk);
9733#endif
9734
0e1f3483
HS
9735 if (unlikely(running))
9736 tsk->sched_class->set_curr_task(rq);
9737 if (on_rq)
7074badb 9738 enqueue_task(rq, tsk, 0);
29f59db3 9739
29f59db3
SV
9740 task_rq_unlock(rq, &flags);
9741}
6d6bc0ad 9742#endif /* CONFIG_GROUP_SCHED */
29f59db3 9743
052f1dc7 9744#ifdef CONFIG_FAIR_GROUP_SCHED
c09595f6 9745static void __set_se_shares(struct sched_entity *se, unsigned long shares)
29f59db3
SV
9746{
9747 struct cfs_rq *cfs_rq = se->cfs_rq;
29f59db3
SV
9748 int on_rq;
9749
29f59db3 9750 on_rq = se->on_rq;
62fb1851 9751 if (on_rq)
29f59db3
SV
9752 dequeue_entity(cfs_rq, se, 0);
9753
9754 se->load.weight = shares;
e05510d0 9755 se->load.inv_weight = 0;
29f59db3 9756
62fb1851 9757 if (on_rq)
29f59db3 9758 enqueue_entity(cfs_rq, se, 0);
c09595f6 9759}
62fb1851 9760
c09595f6
PZ
9761static void set_se_shares(struct sched_entity *se, unsigned long shares)
9762{
9763 struct cfs_rq *cfs_rq = se->cfs_rq;
9764 struct rq *rq = cfs_rq->rq;
9765 unsigned long flags;
9766
9767 spin_lock_irqsave(&rq->lock, flags);
9768 __set_se_shares(se, shares);
9769 spin_unlock_irqrestore(&rq->lock, flags);
29f59db3
SV
9770}
9771
8ed36996
PZ
9772static DEFINE_MUTEX(shares_mutex);
9773
4cf86d77 9774int sched_group_set_shares(struct task_group *tg, unsigned long shares)
29f59db3
SV
9775{
9776 int i;
8ed36996 9777 unsigned long flags;
c61935fd 9778
ec7dc8ac
DG
9779 /*
9780 * We can't change the weight of the root cgroup.
9781 */
9782 if (!tg->se[0])
9783 return -EINVAL;
9784
18d95a28
PZ
9785 if (shares < MIN_SHARES)
9786 shares = MIN_SHARES;
cb4ad1ff
MX
9787 else if (shares > MAX_SHARES)
9788 shares = MAX_SHARES;
62fb1851 9789
8ed36996 9790 mutex_lock(&shares_mutex);
9b5b7751 9791 if (tg->shares == shares)
5cb350ba 9792 goto done;
29f59db3 9793
8ed36996 9794 spin_lock_irqsave(&task_group_lock, flags);
bccbe08a
PZ
9795 for_each_possible_cpu(i)
9796 unregister_fair_sched_group(tg, i);
f473aa5e 9797 list_del_rcu(&tg->siblings);
8ed36996 9798 spin_unlock_irqrestore(&task_group_lock, flags);
6b2d7700
SV
9799
9800 /* wait for any ongoing reference to this group to finish */
9801 synchronize_sched();
9802
9803 /*
9804 * Now we are free to modify the group's share on each cpu
9805 * w/o tripping rebalance_share or load_balance_fair.
9806 */
9b5b7751 9807 tg->shares = shares;
c09595f6
PZ
9808 for_each_possible_cpu(i) {
9809 /*
9810 * force a rebalance
9811 */
9812 cfs_rq_set_shares(tg->cfs_rq[i], 0);
cb4ad1ff 9813 set_se_shares(tg->se[i], shares);
c09595f6 9814 }
29f59db3 9815
6b2d7700
SV
9816 /*
9817 * Enable load balance activity on this group, by inserting it back on
9818 * each cpu's rq->leaf_cfs_rq_list.
9819 */
8ed36996 9820 spin_lock_irqsave(&task_group_lock, flags);
bccbe08a
PZ
9821 for_each_possible_cpu(i)
9822 register_fair_sched_group(tg, i);
f473aa5e 9823 list_add_rcu(&tg->siblings, &tg->parent->children);
8ed36996 9824 spin_unlock_irqrestore(&task_group_lock, flags);
5cb350ba 9825done:
8ed36996 9826 mutex_unlock(&shares_mutex);
9b5b7751 9827 return 0;
29f59db3
SV
9828}
9829
5cb350ba
DG
9830unsigned long sched_group_shares(struct task_group *tg)
9831{
9832 return tg->shares;
9833}
052f1dc7 9834#endif
5cb350ba 9835
052f1dc7 9836#ifdef CONFIG_RT_GROUP_SCHED
6f505b16 9837/*
9f0c1e56 9838 * Ensure that the real time constraints are schedulable.
6f505b16 9839 */
9f0c1e56
PZ
9840static DEFINE_MUTEX(rt_constraints_mutex);
9841
9842static unsigned long to_ratio(u64 period, u64 runtime)
9843{
9844 if (runtime == RUNTIME_INF)
9a7e0b18 9845 return 1ULL << 20;
9f0c1e56 9846
9a7e0b18 9847 return div64_u64(runtime << 20, period);
9f0c1e56
PZ
9848}
9849
9a7e0b18
PZ
9850/* Must be called with tasklist_lock held */
9851static inline int tg_has_rt_tasks(struct task_group *tg)
b40b2e8e 9852{
9a7e0b18 9853 struct task_struct *g, *p;
b40b2e8e 9854
9a7e0b18
PZ
9855 do_each_thread(g, p) {
9856 if (rt_task(p) && rt_rq_of_se(&p->rt)->tg == tg)
9857 return 1;
9858 } while_each_thread(g, p);
b40b2e8e 9859
9a7e0b18
PZ
9860 return 0;
9861}
b40b2e8e 9862
9a7e0b18
PZ
9863struct rt_schedulable_data {
9864 struct task_group *tg;
9865 u64 rt_period;
9866 u64 rt_runtime;
9867};
b40b2e8e 9868
9a7e0b18
PZ
9869static int tg_schedulable(struct task_group *tg, void *data)
9870{
9871 struct rt_schedulable_data *d = data;
9872 struct task_group *child;
9873 unsigned long total, sum = 0;
9874 u64 period, runtime;
b40b2e8e 9875
9a7e0b18
PZ
9876 period = ktime_to_ns(tg->rt_bandwidth.rt_period);
9877 runtime = tg->rt_bandwidth.rt_runtime;
b40b2e8e 9878
9a7e0b18
PZ
9879 if (tg == d->tg) {
9880 period = d->rt_period;
9881 runtime = d->rt_runtime;
b40b2e8e 9882 }
b40b2e8e 9883
98a4826b
PZ
9884#ifdef CONFIG_USER_SCHED
9885 if (tg == &root_task_group) {
9886 period = global_rt_period();
9887 runtime = global_rt_runtime();
9888 }
9889#endif
9890
4653f803
PZ
9891 /*
9892 * Cannot have more runtime than the period.
9893 */
9894 if (runtime > period && runtime != RUNTIME_INF)
9895 return -EINVAL;
6f505b16 9896
4653f803
PZ
9897 /*
9898 * Ensure we don't starve existing RT tasks.
9899 */
9a7e0b18
PZ
9900 if (rt_bandwidth_enabled() && !runtime && tg_has_rt_tasks(tg))
9901 return -EBUSY;
6f505b16 9902
9a7e0b18 9903 total = to_ratio(period, runtime);
6f505b16 9904
4653f803
PZ
9905 /*
9906 * Nobody can have more than the global setting allows.
9907 */
9908 if (total > to_ratio(global_rt_period(), global_rt_runtime()))
9909 return -EINVAL;
6f505b16 9910
4653f803
PZ
9911 /*
9912 * The sum of our children's runtime should not exceed our own.
9913 */
9a7e0b18
PZ
9914 list_for_each_entry_rcu(child, &tg->children, siblings) {
9915 period = ktime_to_ns(child->rt_bandwidth.rt_period);
9916 runtime = child->rt_bandwidth.rt_runtime;
6f505b16 9917
9a7e0b18
PZ
9918 if (child == d->tg) {
9919 period = d->rt_period;
9920 runtime = d->rt_runtime;
9921 }
6f505b16 9922
9a7e0b18 9923 sum += to_ratio(period, runtime);
9f0c1e56 9924 }
6f505b16 9925
9a7e0b18
PZ
9926 if (sum > total)
9927 return -EINVAL;
9928
9929 return 0;
6f505b16
PZ
9930}
9931
9a7e0b18 9932static int __rt_schedulable(struct task_group *tg, u64 period, u64 runtime)
521f1a24 9933{
9a7e0b18
PZ
9934 struct rt_schedulable_data data = {
9935 .tg = tg,
9936 .rt_period = period,
9937 .rt_runtime = runtime,
9938 };
9939
9940 return walk_tg_tree(tg_schedulable, tg_nop, &data);
521f1a24
DG
9941}
9942
d0b27fa7
PZ
9943static int tg_set_bandwidth(struct task_group *tg,
9944 u64 rt_period, u64 rt_runtime)
6f505b16 9945{
ac086bc2 9946 int i, err = 0;
9f0c1e56 9947
9f0c1e56 9948 mutex_lock(&rt_constraints_mutex);
521f1a24 9949 read_lock(&tasklist_lock);
9a7e0b18
PZ
9950 err = __rt_schedulable(tg, rt_period, rt_runtime);
9951 if (err)
9f0c1e56 9952 goto unlock;
ac086bc2
PZ
9953
9954 spin_lock_irq(&tg->rt_bandwidth.rt_runtime_lock);
d0b27fa7
PZ
9955 tg->rt_bandwidth.rt_period = ns_to_ktime(rt_period);
9956 tg->rt_bandwidth.rt_runtime = rt_runtime;
ac086bc2
PZ
9957
9958 for_each_possible_cpu(i) {
9959 struct rt_rq *rt_rq = tg->rt_rq[i];
9960
9961 spin_lock(&rt_rq->rt_runtime_lock);
9962 rt_rq->rt_runtime = rt_runtime;
9963 spin_unlock(&rt_rq->rt_runtime_lock);
9964 }
9965 spin_unlock_irq(&tg->rt_bandwidth.rt_runtime_lock);
9f0c1e56 9966 unlock:
521f1a24 9967 read_unlock(&tasklist_lock);
9f0c1e56
PZ
9968 mutex_unlock(&rt_constraints_mutex);
9969
9970 return err;
6f505b16
PZ
9971}
9972
d0b27fa7
PZ
9973int sched_group_set_rt_runtime(struct task_group *tg, long rt_runtime_us)
9974{
9975 u64 rt_runtime, rt_period;
9976
9977 rt_period = ktime_to_ns(tg->rt_bandwidth.rt_period);
9978 rt_runtime = (u64)rt_runtime_us * NSEC_PER_USEC;
9979 if (rt_runtime_us < 0)
9980 rt_runtime = RUNTIME_INF;
9981
9982 return tg_set_bandwidth(tg, rt_period, rt_runtime);
9983}
9984
9f0c1e56
PZ
9985long sched_group_rt_runtime(struct task_group *tg)
9986{
9987 u64 rt_runtime_us;
9988
d0b27fa7 9989 if (tg->rt_bandwidth.rt_runtime == RUNTIME_INF)
9f0c1e56
PZ
9990 return -1;
9991
d0b27fa7 9992 rt_runtime_us = tg->rt_bandwidth.rt_runtime;
9f0c1e56
PZ
9993 do_div(rt_runtime_us, NSEC_PER_USEC);
9994 return rt_runtime_us;
9995}
d0b27fa7
PZ
9996
9997int sched_group_set_rt_period(struct task_group *tg, long rt_period_us)
9998{
9999 u64 rt_runtime, rt_period;
10000
10001 rt_period = (u64)rt_period_us * NSEC_PER_USEC;
10002 rt_runtime = tg->rt_bandwidth.rt_runtime;
10003
619b0488
R
10004 if (rt_period == 0)
10005 return -EINVAL;
10006
d0b27fa7
PZ
10007 return tg_set_bandwidth(tg, rt_period, rt_runtime);
10008}
10009
10010long sched_group_rt_period(struct task_group *tg)
10011{
10012 u64 rt_period_us;
10013
10014 rt_period_us = ktime_to_ns(tg->rt_bandwidth.rt_period);
10015 do_div(rt_period_us, NSEC_PER_USEC);
10016 return rt_period_us;
10017}
10018
10019static int sched_rt_global_constraints(void)
10020{
4653f803 10021 u64 runtime, period;
d0b27fa7
PZ
10022 int ret = 0;
10023
ec5d4989
HS
10024 if (sysctl_sched_rt_period <= 0)
10025 return -EINVAL;
10026
4653f803
PZ
10027 runtime = global_rt_runtime();
10028 period = global_rt_period();
10029
10030 /*
10031 * Sanity check on the sysctl variables.
10032 */
10033 if (runtime > period && runtime != RUNTIME_INF)
10034 return -EINVAL;
10b612f4 10035
d0b27fa7 10036 mutex_lock(&rt_constraints_mutex);
9a7e0b18 10037 read_lock(&tasklist_lock);
4653f803 10038 ret = __rt_schedulable(NULL, 0, 0);
9a7e0b18 10039 read_unlock(&tasklist_lock);
d0b27fa7
PZ
10040 mutex_unlock(&rt_constraints_mutex);
10041
10042 return ret;
10043}
54e99124
DG
10044
10045int sched_rt_can_attach(struct task_group *tg, struct task_struct *tsk)
10046{
10047 /* Don't accept realtime tasks when there is no way for them to run */
10048 if (rt_task(tsk) && tg->rt_bandwidth.rt_runtime == 0)
10049 return 0;
10050
10051 return 1;
10052}
10053
6d6bc0ad 10054#else /* !CONFIG_RT_GROUP_SCHED */
d0b27fa7
PZ
10055static int sched_rt_global_constraints(void)
10056{
ac086bc2
PZ
10057 unsigned long flags;
10058 int i;
10059
ec5d4989
HS
10060 if (sysctl_sched_rt_period <= 0)
10061 return -EINVAL;
10062
60aa605d
PZ
10063 /*
10064 * There's always some RT tasks in the root group
10065 * -- migration, kstopmachine etc..
10066 */
10067 if (sysctl_sched_rt_runtime == 0)
10068 return -EBUSY;
10069
ac086bc2
PZ
10070 spin_lock_irqsave(&def_rt_bandwidth.rt_runtime_lock, flags);
10071 for_each_possible_cpu(i) {
10072 struct rt_rq *rt_rq = &cpu_rq(i)->rt;
10073
10074 spin_lock(&rt_rq->rt_runtime_lock);
10075 rt_rq->rt_runtime = global_rt_runtime();
10076 spin_unlock(&rt_rq->rt_runtime_lock);
10077 }
10078 spin_unlock_irqrestore(&def_rt_bandwidth.rt_runtime_lock, flags);
10079
d0b27fa7
PZ
10080 return 0;
10081}
6d6bc0ad 10082#endif /* CONFIG_RT_GROUP_SCHED */
d0b27fa7
PZ
10083
10084int sched_rt_handler(struct ctl_table *table, int write,
10085 struct file *filp, void __user *buffer, size_t *lenp,
10086 loff_t *ppos)
10087{
10088 int ret;
10089 int old_period, old_runtime;
10090 static DEFINE_MUTEX(mutex);
10091
10092 mutex_lock(&mutex);
10093 old_period = sysctl_sched_rt_period;
10094 old_runtime = sysctl_sched_rt_runtime;
10095
10096 ret = proc_dointvec(table, write, filp, buffer, lenp, ppos);
10097
10098 if (!ret && write) {
10099 ret = sched_rt_global_constraints();
10100 if (ret) {
10101 sysctl_sched_rt_period = old_period;
10102 sysctl_sched_rt_runtime = old_runtime;
10103 } else {
10104 def_rt_bandwidth.rt_runtime = global_rt_runtime();
10105 def_rt_bandwidth.rt_period =
10106 ns_to_ktime(global_rt_period());
10107 }
10108 }
10109 mutex_unlock(&mutex);
10110
10111 return ret;
10112}
68318b8e 10113
052f1dc7 10114#ifdef CONFIG_CGROUP_SCHED
68318b8e
SV
10115
10116/* return corresponding task_group object of a cgroup */
2b01dfe3 10117static inline struct task_group *cgroup_tg(struct cgroup *cgrp)
68318b8e 10118{
2b01dfe3
PM
10119 return container_of(cgroup_subsys_state(cgrp, cpu_cgroup_subsys_id),
10120 struct task_group, css);
68318b8e
SV
10121}
10122
10123static struct cgroup_subsys_state *
2b01dfe3 10124cpu_cgroup_create(struct cgroup_subsys *ss, struct cgroup *cgrp)
68318b8e 10125{
ec7dc8ac 10126 struct task_group *tg, *parent;
68318b8e 10127
2b01dfe3 10128 if (!cgrp->parent) {
68318b8e 10129 /* This is early initialization for the top cgroup */
68318b8e
SV
10130 return &init_task_group.css;
10131 }
10132
ec7dc8ac
DG
10133 parent = cgroup_tg(cgrp->parent);
10134 tg = sched_create_group(parent);
68318b8e
SV
10135 if (IS_ERR(tg))
10136 return ERR_PTR(-ENOMEM);
10137
68318b8e
SV
10138 return &tg->css;
10139}
10140
41a2d6cf
IM
10141static void
10142cpu_cgroup_destroy(struct cgroup_subsys *ss, struct cgroup *cgrp)
68318b8e 10143{
2b01dfe3 10144 struct task_group *tg = cgroup_tg(cgrp);
68318b8e
SV
10145
10146 sched_destroy_group(tg);
10147}
10148
41a2d6cf
IM
10149static int
10150cpu_cgroup_can_attach(struct cgroup_subsys *ss, struct cgroup *cgrp,
10151 struct task_struct *tsk)
68318b8e 10152{
b68aa230 10153#ifdef CONFIG_RT_GROUP_SCHED
54e99124 10154 if (!sched_rt_can_attach(cgroup_tg(cgrp), tsk))
b68aa230
PZ
10155 return -EINVAL;
10156#else
68318b8e
SV
10157 /* We don't support RT-tasks being in separate groups */
10158 if (tsk->sched_class != &fair_sched_class)
10159 return -EINVAL;
b68aa230 10160#endif
68318b8e
SV
10161
10162 return 0;
10163}
10164
10165static void
2b01dfe3 10166cpu_cgroup_attach(struct cgroup_subsys *ss, struct cgroup *cgrp,
68318b8e
SV
10167 struct cgroup *old_cont, struct task_struct *tsk)
10168{
10169 sched_move_task(tsk);
10170}
10171
052f1dc7 10172#ifdef CONFIG_FAIR_GROUP_SCHED
f4c753b7 10173static int cpu_shares_write_u64(struct cgroup *cgrp, struct cftype *cftype,
2b01dfe3 10174 u64 shareval)
68318b8e 10175{
2b01dfe3 10176 return sched_group_set_shares(cgroup_tg(cgrp), shareval);
68318b8e
SV
10177}
10178
f4c753b7 10179static u64 cpu_shares_read_u64(struct cgroup *cgrp, struct cftype *cft)
68318b8e 10180{
2b01dfe3 10181 struct task_group *tg = cgroup_tg(cgrp);
68318b8e
SV
10182
10183 return (u64) tg->shares;
10184}
6d6bc0ad 10185#endif /* CONFIG_FAIR_GROUP_SCHED */
68318b8e 10186
052f1dc7 10187#ifdef CONFIG_RT_GROUP_SCHED
0c70814c 10188static int cpu_rt_runtime_write(struct cgroup *cgrp, struct cftype *cft,
06ecb27c 10189 s64 val)
6f505b16 10190{
06ecb27c 10191 return sched_group_set_rt_runtime(cgroup_tg(cgrp), val);
6f505b16
PZ
10192}
10193
06ecb27c 10194static s64 cpu_rt_runtime_read(struct cgroup *cgrp, struct cftype *cft)
6f505b16 10195{
06ecb27c 10196 return sched_group_rt_runtime(cgroup_tg(cgrp));
6f505b16 10197}
d0b27fa7
PZ
10198
10199static int cpu_rt_period_write_uint(struct cgroup *cgrp, struct cftype *cftype,
10200 u64 rt_period_us)
10201{
10202 return sched_group_set_rt_period(cgroup_tg(cgrp), rt_period_us);
10203}
10204
10205static u64 cpu_rt_period_read_uint(struct cgroup *cgrp, struct cftype *cft)
10206{
10207 return sched_group_rt_period(cgroup_tg(cgrp));
10208}
6d6bc0ad 10209#endif /* CONFIG_RT_GROUP_SCHED */
6f505b16 10210
fe5c7cc2 10211static struct cftype cpu_files[] = {
052f1dc7 10212#ifdef CONFIG_FAIR_GROUP_SCHED
fe5c7cc2
PM
10213 {
10214 .name = "shares",
f4c753b7
PM
10215 .read_u64 = cpu_shares_read_u64,
10216 .write_u64 = cpu_shares_write_u64,
fe5c7cc2 10217 },
052f1dc7
PZ
10218#endif
10219#ifdef CONFIG_RT_GROUP_SCHED
6f505b16 10220 {
9f0c1e56 10221 .name = "rt_runtime_us",
06ecb27c
PM
10222 .read_s64 = cpu_rt_runtime_read,
10223 .write_s64 = cpu_rt_runtime_write,
6f505b16 10224 },
d0b27fa7
PZ
10225 {
10226 .name = "rt_period_us",
f4c753b7
PM
10227 .read_u64 = cpu_rt_period_read_uint,
10228 .write_u64 = cpu_rt_period_write_uint,
d0b27fa7 10229 },
052f1dc7 10230#endif
68318b8e
SV
10231};
10232
10233static int cpu_cgroup_populate(struct cgroup_subsys *ss, struct cgroup *cont)
10234{
fe5c7cc2 10235 return cgroup_add_files(cont, ss, cpu_files, ARRAY_SIZE(cpu_files));
68318b8e
SV
10236}
10237
10238struct cgroup_subsys cpu_cgroup_subsys = {
38605cae
IM
10239 .name = "cpu",
10240 .create = cpu_cgroup_create,
10241 .destroy = cpu_cgroup_destroy,
10242 .can_attach = cpu_cgroup_can_attach,
10243 .attach = cpu_cgroup_attach,
10244 .populate = cpu_cgroup_populate,
10245 .subsys_id = cpu_cgroup_subsys_id,
68318b8e
SV
10246 .early_init = 1,
10247};
10248
052f1dc7 10249#endif /* CONFIG_CGROUP_SCHED */
d842de87
SV
10250
10251#ifdef CONFIG_CGROUP_CPUACCT
10252
10253/*
10254 * CPU accounting code for task groups.
10255 *
10256 * Based on the work by Paul Menage (menage@google.com) and Balbir Singh
10257 * (balbir@in.ibm.com).
10258 */
10259
934352f2 10260/* track cpu usage of a group of tasks and its child groups */
d842de87
SV
10261struct cpuacct {
10262 struct cgroup_subsys_state css;
10263 /* cpuusage holds pointer to a u64-type object on every cpu */
10264 u64 *cpuusage;
ef12fefa 10265 struct percpu_counter cpustat[CPUACCT_STAT_NSTATS];
934352f2 10266 struct cpuacct *parent;
d842de87
SV
10267};
10268
10269struct cgroup_subsys cpuacct_subsys;
10270
10271/* return cpu accounting group corresponding to this container */
32cd756a 10272static inline struct cpuacct *cgroup_ca(struct cgroup *cgrp)
d842de87 10273{
32cd756a 10274 return container_of(cgroup_subsys_state(cgrp, cpuacct_subsys_id),
d842de87
SV
10275 struct cpuacct, css);
10276}
10277
10278/* return cpu accounting group to which this task belongs */
10279static inline struct cpuacct *task_ca(struct task_struct *tsk)
10280{
10281 return container_of(task_subsys_state(tsk, cpuacct_subsys_id),
10282 struct cpuacct, css);
10283}
10284
10285/* create a new cpu accounting group */
10286static struct cgroup_subsys_state *cpuacct_create(
32cd756a 10287 struct cgroup_subsys *ss, struct cgroup *cgrp)
d842de87
SV
10288{
10289 struct cpuacct *ca = kzalloc(sizeof(*ca), GFP_KERNEL);
ef12fefa 10290 int i;
d842de87
SV
10291
10292 if (!ca)
ef12fefa 10293 goto out;
d842de87
SV
10294
10295 ca->cpuusage = alloc_percpu(u64);
ef12fefa
BR
10296 if (!ca->cpuusage)
10297 goto out_free_ca;
10298
10299 for (i = 0; i < CPUACCT_STAT_NSTATS; i++)
10300 if (percpu_counter_init(&ca->cpustat[i], 0))
10301 goto out_free_counters;
d842de87 10302
934352f2
BR
10303 if (cgrp->parent)
10304 ca->parent = cgroup_ca(cgrp->parent);
10305
d842de87 10306 return &ca->css;
ef12fefa
BR
10307
10308out_free_counters:
10309 while (--i >= 0)
10310 percpu_counter_destroy(&ca->cpustat[i]);
10311 free_percpu(ca->cpuusage);
10312out_free_ca:
10313 kfree(ca);
10314out:
10315 return ERR_PTR(-ENOMEM);
d842de87
SV
10316}
10317
10318/* destroy an existing cpu accounting group */
41a2d6cf 10319static void
32cd756a 10320cpuacct_destroy(struct cgroup_subsys *ss, struct cgroup *cgrp)
d842de87 10321{
32cd756a 10322 struct cpuacct *ca = cgroup_ca(cgrp);
ef12fefa 10323 int i;
d842de87 10324
ef12fefa
BR
10325 for (i = 0; i < CPUACCT_STAT_NSTATS; i++)
10326 percpu_counter_destroy(&ca->cpustat[i]);
d842de87
SV
10327 free_percpu(ca->cpuusage);
10328 kfree(ca);
10329}
10330
720f5498
KC
10331static u64 cpuacct_cpuusage_read(struct cpuacct *ca, int cpu)
10332{
b36128c8 10333 u64 *cpuusage = per_cpu_ptr(ca->cpuusage, cpu);
720f5498
KC
10334 u64 data;
10335
10336#ifndef CONFIG_64BIT
10337 /*
10338 * Take rq->lock to make 64-bit read safe on 32-bit platforms.
10339 */
10340 spin_lock_irq(&cpu_rq(cpu)->lock);
10341 data = *cpuusage;
10342 spin_unlock_irq(&cpu_rq(cpu)->lock);
10343#else
10344 data = *cpuusage;
10345#endif
10346
10347 return data;
10348}
10349
10350static void cpuacct_cpuusage_write(struct cpuacct *ca, int cpu, u64 val)
10351{
b36128c8 10352 u64 *cpuusage = per_cpu_ptr(ca->cpuusage, cpu);
720f5498
KC
10353
10354#ifndef CONFIG_64BIT
10355 /*
10356 * Take rq->lock to make 64-bit write safe on 32-bit platforms.
10357 */
10358 spin_lock_irq(&cpu_rq(cpu)->lock);
10359 *cpuusage = val;
10360 spin_unlock_irq(&cpu_rq(cpu)->lock);
10361#else
10362 *cpuusage = val;
10363#endif
10364}
10365
d842de87 10366/* return total cpu usage (in nanoseconds) of a group */
32cd756a 10367static u64 cpuusage_read(struct cgroup *cgrp, struct cftype *cft)
d842de87 10368{
32cd756a 10369 struct cpuacct *ca = cgroup_ca(cgrp);
d842de87
SV
10370 u64 totalcpuusage = 0;
10371 int i;
10372
720f5498
KC
10373 for_each_present_cpu(i)
10374 totalcpuusage += cpuacct_cpuusage_read(ca, i);
d842de87
SV
10375
10376 return totalcpuusage;
10377}
10378
0297b803
DG
10379static int cpuusage_write(struct cgroup *cgrp, struct cftype *cftype,
10380 u64 reset)
10381{
10382 struct cpuacct *ca = cgroup_ca(cgrp);
10383 int err = 0;
10384 int i;
10385
10386 if (reset) {
10387 err = -EINVAL;
10388 goto out;
10389 }
10390
720f5498
KC
10391 for_each_present_cpu(i)
10392 cpuacct_cpuusage_write(ca, i, 0);
0297b803 10393
0297b803
DG
10394out:
10395 return err;
10396}
10397
e9515c3c
KC
10398static int cpuacct_percpu_seq_read(struct cgroup *cgroup, struct cftype *cft,
10399 struct seq_file *m)
10400{
10401 struct cpuacct *ca = cgroup_ca(cgroup);
10402 u64 percpu;
10403 int i;
10404
10405 for_each_present_cpu(i) {
10406 percpu = cpuacct_cpuusage_read(ca, i);
10407 seq_printf(m, "%llu ", (unsigned long long) percpu);
10408 }
10409 seq_printf(m, "\n");
10410 return 0;
10411}
10412
ef12fefa
BR
10413static const char *cpuacct_stat_desc[] = {
10414 [CPUACCT_STAT_USER] = "user",
10415 [CPUACCT_STAT_SYSTEM] = "system",
10416};
10417
10418static int cpuacct_stats_show(struct cgroup *cgrp, struct cftype *cft,
10419 struct cgroup_map_cb *cb)
10420{
10421 struct cpuacct *ca = cgroup_ca(cgrp);
10422 int i;
10423
10424 for (i = 0; i < CPUACCT_STAT_NSTATS; i++) {
10425 s64 val = percpu_counter_read(&ca->cpustat[i]);
10426 val = cputime64_to_clock_t(val);
10427 cb->fill(cb, cpuacct_stat_desc[i], val);
10428 }
10429 return 0;
10430}
10431
d842de87
SV
10432static struct cftype files[] = {
10433 {
10434 .name = "usage",
f4c753b7
PM
10435 .read_u64 = cpuusage_read,
10436 .write_u64 = cpuusage_write,
d842de87 10437 },
e9515c3c
KC
10438 {
10439 .name = "usage_percpu",
10440 .read_seq_string = cpuacct_percpu_seq_read,
10441 },
ef12fefa
BR
10442 {
10443 .name = "stat",
10444 .read_map = cpuacct_stats_show,
10445 },
d842de87
SV
10446};
10447
32cd756a 10448static int cpuacct_populate(struct cgroup_subsys *ss, struct cgroup *cgrp)
d842de87 10449{
32cd756a 10450 return cgroup_add_files(cgrp, ss, files, ARRAY_SIZE(files));
d842de87
SV
10451}
10452
10453/*
10454 * charge this task's execution time to its accounting group.
10455 *
10456 * called with rq->lock held.
10457 */
10458static void cpuacct_charge(struct task_struct *tsk, u64 cputime)
10459{
10460 struct cpuacct *ca;
934352f2 10461 int cpu;
d842de87 10462
c40c6f85 10463 if (unlikely(!cpuacct_subsys.active))
d842de87
SV
10464 return;
10465
934352f2 10466 cpu = task_cpu(tsk);
a18b83b7
BR
10467
10468 rcu_read_lock();
10469
d842de87 10470 ca = task_ca(tsk);
d842de87 10471
934352f2 10472 for (; ca; ca = ca->parent) {
b36128c8 10473 u64 *cpuusage = per_cpu_ptr(ca->cpuusage, cpu);
d842de87
SV
10474 *cpuusage += cputime;
10475 }
a18b83b7
BR
10476
10477 rcu_read_unlock();
d842de87
SV
10478}
10479
ef12fefa
BR
10480/*
10481 * Charge the system/user time to the task's accounting group.
10482 */
10483static void cpuacct_update_stats(struct task_struct *tsk,
10484 enum cpuacct_stat_index idx, cputime_t val)
10485{
10486 struct cpuacct *ca;
10487
10488 if (unlikely(!cpuacct_subsys.active))
10489 return;
10490
10491 rcu_read_lock();
10492 ca = task_ca(tsk);
10493
10494 do {
10495 percpu_counter_add(&ca->cpustat[idx], val);
10496 ca = ca->parent;
10497 } while (ca);
10498 rcu_read_unlock();
10499}
10500
d842de87
SV
10501struct cgroup_subsys cpuacct_subsys = {
10502 .name = "cpuacct",
10503 .create = cpuacct_create,
10504 .destroy = cpuacct_destroy,
10505 .populate = cpuacct_populate,
10506 .subsys_id = cpuacct_subsys_id,
10507};
10508#endif /* CONFIG_CGROUP_CPUACCT */