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