blob: 12cc09cf51478ad6120a7fd5a92009b895ba564a [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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
Ingo Molnarc31f2e82007-07-09 18:52:01 +020019 * 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
Ingo Molnarb9131762008-01-25 21:08:19 +010025 * 2007-11-29 RT balancing improvements by Steven Rostedt, Gregory Haskins,
26 * Thomas Gleixner, Mike Kravetz
Linus Torvalds1da177e2005-04-16 15:20:36 -070027 */
28
29#include <linux/mm.h>
30#include <linux/module.h>
31#include <linux/nmi.h>
32#include <linux/init.h>
Ingo Molnardff06c12007-07-09 18:52:00 +020033#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070034#include <linux/highmem.h>
35#include <linux/smp_lock.h>
36#include <asm/mmu_context.h>
37#include <linux/interrupt.h>
Randy.Dunlapc59ede72006-01-11 12:17:46 -080038#include <linux/capability.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070039#include <linux/completion.h>
40#include <linux/kernel_stat.h>
Ingo Molnar9a11b49a2006-07-03 00:24:33 -070041#include <linux/debug_locks.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070042#include <linux/security.h>
43#include <linux/notifier.h>
44#include <linux/profile.h>
Nigel Cunningham7dfb7102006-12-06 20:34:23 -080045#include <linux/freezer.h>
akpm@osdl.org198e2f12006-01-12 01:05:30 -080046#include <linux/vmalloc.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070047#include <linux/blkdev.h>
48#include <linux/delay.h>
Pavel Emelyanovb4888932007-10-18 23:40:14 -070049#include <linux/pid_namespace.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070050#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>
Alexey Dobriyanb5aadf72008-10-06 13:23:43 +040058#include <linux/proc_fs.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070059#include <linux/seq_file.h>
Nick Piggine692ab52007-07-26 13:40:43 +020060#include <linux/sysctl.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070061#include <linux/syscalls.h>
62#include <linux/times.h>
Jay Lan8f0ab512006-09-30 23:28:59 -070063#include <linux/tsacct_kern.h>
bibo maoc6fd91f2006-03-26 01:38:20 -080064#include <linux/kprobes.h>
Shailabh Nagar0ff92242006-07-14 00:24:37 -070065#include <linux/delayacct.h>
Eric Dumazet5517d862007-05-08 00:32:57 -070066#include <linux/reciprocal_div.h>
Ingo Molnardff06c12007-07-09 18:52:00 +020067#include <linux/unistd.h>
Jens Axboef5ff8422007-09-21 09:19:54 +020068#include <linux/pagemap.h>
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +010069#include <linux/hrtimer.h>
Reynes Philippe30914a52008-03-17 16:19:05 -070070#include <linux/tick.h>
Peter Zijlstraf00b45c2008-04-19 19:45:00 +020071#include <linux/debugfs.h>
72#include <linux/ctype.h>
Steven Rostedt6cd8a4b2008-05-12 21:20:42 +020073#include <linux/ftrace.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070074
Eric Dumazet5517d862007-05-08 00:32:57 -070075#include <asm/tlb.h>
Satyam Sharma838225b2007-10-24 18:23:50 +020076#include <asm/irq_regs.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070077
Gregory Haskins6e0534f2008-05-12 21:21:01 +020078#include "sched_cpupri.h"
79
Steven Rostedta8d154b2009-04-10 09:36:00 -040080#define CREATE_TRACE_POINTS
Steven Rostedtad8d75f2009-04-14 19:39:12 -040081#include <trace/events/sched.h>
Steven Rostedta8d154b2009-04-10 09:36:00 -040082
Linus Torvalds1da177e2005-04-16 15:20:36 -070083/*
84 * Convert user-nice values [ -20 ... 0 ... 19 ]
85 * to static priority [ MAX_RT_PRIO..MAX_PRIO-1 ],
86 * and back.
87 */
88#define NICE_TO_PRIO(nice) (MAX_RT_PRIO + (nice) + 20)
89#define PRIO_TO_NICE(prio) ((prio) - MAX_RT_PRIO - 20)
90#define TASK_NICE(p) PRIO_TO_NICE((p)->static_prio)
91
92/*
93 * 'User priority' is the nice value converted to something we
94 * can work with better when scaling various scheduler parameters,
95 * it's a [ 0 ... 39 ] range.
96 */
97#define USER_PRIO(p) ((p)-MAX_RT_PRIO)
98#define TASK_USER_PRIO(p) USER_PRIO((p)->static_prio)
99#define MAX_USER_PRIO (USER_PRIO(MAX_PRIO))
100
101/*
Ingo Molnard7876a02008-01-25 21:08:19 +0100102 * Helpers for converting nanosecond timing to jiffy resolution
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 */
Eric Dumazetd6322fa2007-11-09 22:39:38 +0100104#define NS_TO_JIFFIES(TIME) ((unsigned long)(TIME) / (NSEC_PER_SEC / HZ))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200106#define NICE_0_LOAD SCHED_LOAD_SCALE
107#define NICE_0_SHIFT SCHED_LOAD_SHIFT
108
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109/*
110 * These are the 'tuning knobs' of the scheduler:
111 *
Dmitry Adamushkoa4ec24b2007-10-15 17:00:13 +0200112 * default timeslice is 100 msecs (used only for SCHED_RR tasks).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113 * Timeslices get refilled after they expire.
114 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115#define DEF_TIMESLICE (100 * HZ / 1000)
Peter Williams2dd73a42006-06-27 02:54:34 -0700116
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +0200117/*
118 * single value that denotes runtime == period, ie unlimited time.
119 */
120#define RUNTIME_INF ((u64)~0ULL)
121
Eric Dumazet5517d862007-05-08 00:32:57 -0700122#ifdef CONFIG_SMP
Steven Noonanfd2ab302009-01-11 01:04:22 -0800123
124static void double_rq_lock(struct rq *rq1, struct rq *rq2);
125
Eric Dumazet5517d862007-05-08 00:32:57 -0700126/*
127 * Divide a load by a sched group cpu_power : (load / sg->__cpu_power)
128 * Since cpu_power is a 'constant', we can use a reciprocal divide.
129 */
130static inline u32 sg_div_cpu_power(const struct sched_group *sg, u32 load)
131{
132 return reciprocal_divide(load, sg->reciprocal_cpu_power);
133}
134
135/*
136 * Each time a sched group cpu_power is changed,
137 * we must compute its reciprocal value
138 */
139static inline void sg_inc_cpu_power(struct sched_group *sg, u32 val)
140{
141 sg->__cpu_power += val;
142 sg->reciprocal_cpu_power = reciprocal_value(sg->__cpu_power);
143}
144#endif
145
Ingo Molnare05606d2007-07-09 18:51:59 +0200146static inline int rt_policy(int policy)
147{
Roel Kluin3f33a7c2008-05-13 23:44:11 +0200148 if (unlikely(policy == SCHED_FIFO || policy == SCHED_RR))
Ingo Molnare05606d2007-07-09 18:51:59 +0200149 return 1;
150 return 0;
151}
152
153static inline int task_has_rt_policy(struct task_struct *p)
154{
155 return rt_policy(p->policy);
156}
157
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158/*
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200159 * This is the priority-queue data structure of the RT scheduling class:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160 */
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200161struct rt_prio_array {
162 DECLARE_BITMAP(bitmap, MAX_RT_PRIO+1); /* include 1 bit for delimiter */
163 struct list_head queue[MAX_RT_PRIO];
164};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +0200166struct rt_bandwidth {
Ingo Molnarea736ed2008-03-25 13:51:45 +0100167 /* nests inside the rq lock: */
168 spinlock_t rt_runtime_lock;
169 ktime_t rt_period;
170 u64 rt_runtime;
171 struct hrtimer rt_period_timer;
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +0200172};
173
174static struct rt_bandwidth def_rt_bandwidth;
175
176static int do_sched_rt_period_timer(struct rt_bandwidth *rt_b, int overrun);
177
178static enum hrtimer_restart sched_rt_period_timer(struct hrtimer *timer)
179{
180 struct rt_bandwidth *rt_b =
181 container_of(timer, struct rt_bandwidth, rt_period_timer);
182 ktime_t now;
183 int overrun;
184 int idle = 0;
185
186 for (;;) {
187 now = hrtimer_cb_get_time(timer);
188 overrun = hrtimer_forward(timer, now, rt_b->rt_period);
189
190 if (!overrun)
191 break;
192
193 idle = do_sched_rt_period_timer(rt_b, overrun);
194 }
195
196 return idle ? HRTIMER_NORESTART : HRTIMER_RESTART;
197}
198
199static
200void init_rt_bandwidth(struct rt_bandwidth *rt_b, u64 period, u64 runtime)
201{
202 rt_b->rt_period = ns_to_ktime(period);
203 rt_b->rt_runtime = runtime;
204
Peter Zijlstraac086bc2008-04-19 19:44:58 +0200205 spin_lock_init(&rt_b->rt_runtime_lock);
206
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +0200207 hrtimer_init(&rt_b->rt_period_timer,
208 CLOCK_MONOTONIC, HRTIMER_MODE_REL);
209 rt_b->rt_period_timer.function = sched_rt_period_timer;
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +0200210}
211
Krzysztof Heltc8bfff62008-09-05 23:46:19 +0200212static inline int rt_bandwidth_enabled(void)
213{
214 return sysctl_sched_rt_runtime >= 0;
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +0200215}
216
217static void start_rt_bandwidth(struct rt_bandwidth *rt_b)
218{
219 ktime_t now;
220
Hiroshi Shimamotocac64d02009-02-25 09:59:26 -0800221 if (!rt_bandwidth_enabled() || rt_b->rt_runtime == RUNTIME_INF)
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +0200222 return;
223
224 if (hrtimer_active(&rt_b->rt_period_timer))
225 return;
226
227 spin_lock(&rt_b->rt_runtime_lock);
228 for (;;) {
Peter Zijlstra7f1e2ca2009-03-13 12:21:27 +0100229 unsigned long delta;
230 ktime_t soft, hard;
231
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +0200232 if (hrtimer_active(&rt_b->rt_period_timer))
233 break;
234
235 now = hrtimer_cb_get_time(&rt_b->rt_period_timer);
236 hrtimer_forward(&rt_b->rt_period_timer, now, rt_b->rt_period);
Peter Zijlstra7f1e2ca2009-03-13 12:21:27 +0100237
238 soft = hrtimer_get_softexpires(&rt_b->rt_period_timer);
239 hard = hrtimer_get_expires(&rt_b->rt_period_timer);
240 delta = ktime_to_ns(ktime_sub(hard, soft));
241 __hrtimer_start_range_ns(&rt_b->rt_period_timer, soft, delta,
242 HRTIMER_MODE_ABS, 0);
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +0200243 }
244 spin_unlock(&rt_b->rt_runtime_lock);
245}
246
247#ifdef CONFIG_RT_GROUP_SCHED
248static void destroy_rt_bandwidth(struct rt_bandwidth *rt_b)
249{
250 hrtimer_cancel(&rt_b->rt_period_timer);
251}
252#endif
253
Heiko Carstens712555e2008-04-28 11:33:07 +0200254/*
255 * sched_domains_mutex serializes calls to arch_init_sched_domains,
256 * detach_destroy_domains and partition_sched_domains.
257 */
258static DEFINE_MUTEX(sched_domains_mutex);
259
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100260#ifdef CONFIG_GROUP_SCHED
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +0200261
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -0700262#include <linux/cgroup.h>
263
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +0200264struct cfs_rq;
265
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100266static LIST_HEAD(task_groups);
267
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +0200268/* task group related information */
Ingo Molnar4cf86d72007-10-15 17:00:14 +0200269struct task_group {
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100270#ifdef CONFIG_CGROUP_SCHED
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -0700271 struct cgroup_subsys_state css;
272#endif
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100273
Arun R Bharadwaj6c415b92008-12-01 20:49:05 +0530274#ifdef CONFIG_USER_SCHED
275 uid_t uid;
276#endif
277
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100278#ifdef CONFIG_FAIR_GROUP_SCHED
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +0200279 /* schedulable entities of this group on each cpu */
280 struct sched_entity **se;
281 /* runqueue "owned" by this group on each cpu */
282 struct cfs_rq **cfs_rq;
283 unsigned long shares;
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100284#endif
285
286#ifdef CONFIG_RT_GROUP_SCHED
287 struct sched_rt_entity **rt_se;
288 struct rt_rq **rt_rq;
289
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +0200290 struct rt_bandwidth rt_bandwidth;
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100291#endif
Srivatsa Vaddagiri6b2d7702008-01-25 21:08:00 +0100292
Srivatsa Vaddagiriae8393e2007-10-29 21:18:11 +0100293 struct rcu_head rcu;
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100294 struct list_head list;
Peter Zijlstraf473aa52008-04-19 19:45:00 +0200295
296 struct task_group *parent;
297 struct list_head siblings;
298 struct list_head children;
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +0200299};
300
Dhaval Giani354d60c2008-04-19 19:44:59 +0200301#ifdef CONFIG_USER_SCHED
Peter Zijlstraeff766a2008-04-19 19:45:00 +0200302
Arun R Bharadwaj6c415b92008-12-01 20:49:05 +0530303/* Helper function to pass uid information to create_sched_user() */
304void set_tg_uid(struct user_struct *user)
305{
306 user->tg->uid = user->uid;
307}
308
Peter Zijlstraeff766a2008-04-19 19:45:00 +0200309/*
310 * Root task group.
311 * Every UID task group (including init_task_group aka UID-0) will
312 * be a child to this group.
313 */
314struct task_group root_task_group;
315
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100316#ifdef CONFIG_FAIR_GROUP_SCHED
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +0200317/* Default task group's sched entity on each cpu */
318static DEFINE_PER_CPU(struct sched_entity, init_sched_entity);
319/* Default task group's cfs_rq on each cpu */
320static DEFINE_PER_CPU(struct cfs_rq, init_cfs_rq) ____cacheline_aligned_in_smp;
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +0200321#endif /* CONFIG_FAIR_GROUP_SCHED */
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100322
323#ifdef CONFIG_RT_GROUP_SCHED
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100324static DEFINE_PER_CPU(struct sched_rt_entity, init_sched_rt_entity);
325static DEFINE_PER_CPU(struct rt_rq, init_rt_rq) ____cacheline_aligned_in_smp;
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +0200326#endif /* CONFIG_RT_GROUP_SCHED */
Peter Zijlstra9a7e0b12008-08-19 12:33:06 +0200327#else /* !CONFIG_USER_SCHED */
Peter Zijlstraeff766a2008-04-19 19:45:00 +0200328#define root_task_group init_task_group
Peter Zijlstra9a7e0b12008-08-19 12:33:06 +0200329#endif /* CONFIG_USER_SCHED */
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100330
Peter Zijlstra8ed36992008-02-13 15:45:39 +0100331/* task_group_lock serializes add/remove of task groups and also changes to
Srivatsa Vaddagiriec2c5072008-01-25 21:07:59 +0100332 * a task group's cpu shares.
333 */
Peter Zijlstra8ed36992008-02-13 15:45:39 +0100334static DEFINE_SPINLOCK(task_group_lock);
Srivatsa Vaddagiriec2c5072008-01-25 21:07:59 +0100335
Peter Zijlstra57310a92009-03-09 13:56:21 +0100336#ifdef CONFIG_SMP
337static int root_task_group_empty(void)
338{
339 return list_empty(&root_task_group.children);
340}
341#endif
342
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100343#ifdef CONFIG_FAIR_GROUP_SCHED
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100344#ifdef CONFIG_USER_SCHED
Ingo Molnar0eab9142008-01-25 21:08:19 +0100345# define INIT_TASK_GROUP_LOAD (2*NICE_0_LOAD)
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +0200346#else /* !CONFIG_USER_SCHED */
Srivatsa Vaddagiri93f992c2008-01-25 21:07:59 +0100347# define INIT_TASK_GROUP_LOAD NICE_0_LOAD
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +0200348#endif /* CONFIG_USER_SCHED */
Srivatsa Vaddagiri24e377a2007-10-15 17:00:09 +0200349
Miao Xiecb4ad1f2008-04-28 12:54:56 +0800350/*
Lai Jiangshan2e084782008-06-12 16:42:58 +0800351 * A weight of 0 or 1 can cause arithmetics problems.
352 * A weight of a cfs_rq is the sum of weights of which entities
353 * are queued on this cfs_rq, so a weight of a entity should not be
354 * too large, so as the shares value of a task group.
Miao Xiecb4ad1f2008-04-28 12:54:56 +0800355 * (The default weight is 1024 - so there's no practical
356 * limitation from this.)
357 */
Peter Zijlstra18d95a22008-04-19 19:45:00 +0200358#define MIN_SHARES 2
Lai Jiangshan2e084782008-06-12 16:42:58 +0800359#define MAX_SHARES (1UL << 18)
Peter Zijlstra18d95a22008-04-19 19:45:00 +0200360
Srivatsa Vaddagiri93f992c2008-01-25 21:07:59 +0100361static int init_task_group_load = INIT_TASK_GROUP_LOAD;
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100362#endif
363
364/* Default task group.
365 * Every task in system belong to this group at bootup.
366 */
Mike Travis434d53b2008-04-04 18:11:04 -0700367struct task_group init_task_group;
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +0200368
369/* return group to which a task belongs */
Ingo Molnar4cf86d72007-10-15 17:00:14 +0200370static inline struct task_group *task_group(struct task_struct *p)
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +0200371{
Ingo Molnar4cf86d72007-10-15 17:00:14 +0200372 struct task_group *tg;
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +0200373
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100374#ifdef CONFIG_USER_SCHED
David Howellsc69e8d92008-11-14 10:39:19 +1100375 rcu_read_lock();
376 tg = __task_cred(p)->user->tg;
377 rcu_read_unlock();
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100378#elif defined(CONFIG_CGROUP_SCHED)
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -0700379 tg = container_of(task_subsys_state(p, cpu_cgroup_subsys_id),
380 struct task_group, css);
Srivatsa Vaddagiri24e377a2007-10-15 17:00:09 +0200381#else
Ingo Molnar41a2d6c2007-12-05 15:46:09 +0100382 tg = &init_task_group;
Srivatsa Vaddagiri24e377a2007-10-15 17:00:09 +0200383#endif
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +0200384 return tg;
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +0200385}
386
387/* Change a task's cfs_rq and parent entity if it moves across CPUs/groups */
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100388static inline void set_task_rq(struct task_struct *p, unsigned int cpu)
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +0200389{
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100390#ifdef CONFIG_FAIR_GROUP_SCHED
Dmitry Adamushkoce96b5a2007-11-15 20:57:40 +0100391 p->se.cfs_rq = task_group(p)->cfs_rq[cpu];
392 p->se.parent = task_group(p)->se[cpu];
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100393#endif
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100394
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100395#ifdef CONFIG_RT_GROUP_SCHED
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100396 p->rt.rt_rq = task_group(p)->rt_rq[cpu];
397 p->rt.parent = task_group(p)->rt_se[cpu];
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100398#endif
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +0200399}
400
401#else
402
Peter Zijlstra57310a92009-03-09 13:56:21 +0100403#ifdef CONFIG_SMP
404static int root_task_group_empty(void)
405{
406 return 1;
407}
408#endif
409
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100410static inline void set_task_rq(struct task_struct *p, unsigned int cpu) { }
Peter Zijlstra83378262008-06-27 13:41:37 +0200411static inline struct task_group *task_group(struct task_struct *p)
412{
413 return NULL;
414}
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +0200415
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100416#endif /* CONFIG_GROUP_SCHED */
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +0200417
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200418/* CFS-related fields in a runqueue */
419struct cfs_rq {
420 struct load_weight load;
421 unsigned long nr_running;
422
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200423 u64 exec_clock;
Ingo Molnare9acbff2007-10-15 17:00:04 +0200424 u64 min_vruntime;
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200425
426 struct rb_root tasks_timeline;
427 struct rb_node *rb_leftmost;
Peter Zijlstra4a55bd52008-04-19 19:45:00 +0200428
429 struct list_head tasks;
430 struct list_head *balance_iterator;
431
432 /*
433 * 'curr' points to currently running entity on this cfs_rq.
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200434 * It is set to NULL otherwise (i.e when none are currently running).
435 */
Peter Zijlstra47932412008-11-04 21:25:09 +0100436 struct sched_entity *curr, *next, *last;
Peter Zijlstraddc97292007-10-15 17:00:10 +0200437
Peter Zijlstra5ac5c4d2008-11-10 10:46:32 +0100438 unsigned int nr_spread_over;
Peter Zijlstraddc97292007-10-15 17:00:10 +0200439
Ingo Molnar62160e32007-10-15 17:00:03 +0200440#ifdef CONFIG_FAIR_GROUP_SCHED
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200441 struct rq *rq; /* cpu runqueue to which this cfs_rq is attached */
442
Ingo Molnar41a2d6c2007-12-05 15:46:09 +0100443 /*
444 * leaf cfs_rqs are those that hold tasks (lowest schedulable entity in
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200445 * a hierarchy). Non-leaf lrqs hold other higher schedulable entities
446 * (like users, containers etc.)
447 *
448 * leaf_cfs_rq_list ties together list of leaf cfs_rq's in a cpu. This
449 * list is used during load balance.
450 */
Ingo Molnar41a2d6c2007-12-05 15:46:09 +0100451 struct list_head leaf_cfs_rq_list;
452 struct task_group *tg; /* group that "owns" this runqueue */
Peter Zijlstrac09595f2008-06-27 13:41:14 +0200453
454#ifdef CONFIG_SMP
Peter Zijlstrac09595f2008-06-27 13:41:14 +0200455 /*
Peter Zijlstrac8cba852008-06-27 13:41:23 +0200456 * the part of load.weight contributed by tasks
Peter Zijlstrac09595f2008-06-27 13:41:14 +0200457 */
Peter Zijlstrac8cba852008-06-27 13:41:23 +0200458 unsigned long task_weight;
Peter Zijlstrac09595f2008-06-27 13:41:14 +0200459
Peter Zijlstrac8cba852008-06-27 13:41:23 +0200460 /*
461 * h_load = weight * f(tg)
462 *
463 * Where f(tg) is the recursive weight fraction assigned to
464 * this group.
465 */
466 unsigned long h_load;
Peter Zijlstrac09595f2008-06-27 13:41:14 +0200467
Peter Zijlstrac8cba852008-06-27 13:41:23 +0200468 /*
469 * this cpu's part of tg->shares
470 */
471 unsigned long shares;
Peter Zijlstraf1d239f2008-06-27 13:41:38 +0200472
473 /*
474 * load.weight at the time we set shares
475 */
476 unsigned long rq_weight;
Peter Zijlstrac09595f2008-06-27 13:41:14 +0200477#endif
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200478#endif
479};
480
481/* Real-Time classes' related field in a runqueue: */
482struct rt_rq {
483 struct rt_prio_array active;
Steven Rostedt63489e42008-01-25 21:08:03 +0100484 unsigned long rt_nr_running;
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100485#if defined CONFIG_SMP || defined CONFIG_RT_GROUP_SCHED
Gregory Haskinse864c492008-12-29 09:39:49 -0500486 struct {
487 int curr; /* highest queued rt task prio */
Gregory Haskins398a1532009-01-14 09:10:04 -0500488#ifdef CONFIG_SMP
Gregory Haskinse864c492008-12-29 09:39:49 -0500489 int next; /* next highest */
Gregory Haskins398a1532009-01-14 09:10:04 -0500490#endif
Gregory Haskinse864c492008-12-29 09:39:49 -0500491 } highest_prio;
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100492#endif
Peter Zijlstrafa85ae22008-01-25 21:08:29 +0100493#ifdef CONFIG_SMP
Gregory Haskins73fe6aa2008-01-25 21:08:07 +0100494 unsigned long rt_nr_migratory;
Gregory Haskinsa22d7fc2008-01-25 21:08:12 +0100495 int overloaded;
Gregory Haskins917b6272008-12-29 09:39:53 -0500496 struct plist_head pushable_tasks;
Peter Zijlstrafa85ae22008-01-25 21:08:29 +0100497#endif
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100498 int rt_throttled;
Peter Zijlstrafa85ae22008-01-25 21:08:29 +0100499 u64 rt_time;
Peter Zijlstraac086bc2008-04-19 19:44:58 +0200500 u64 rt_runtime;
Ingo Molnarea736ed2008-03-25 13:51:45 +0100501 /* Nests inside the rq lock: */
Peter Zijlstraac086bc2008-04-19 19:44:58 +0200502 spinlock_t rt_runtime_lock;
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100503
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100504#ifdef CONFIG_RT_GROUP_SCHED
Peter Zijlstra23b0fdf2008-02-13 15:45:39 +0100505 unsigned long rt_nr_boosted;
506
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100507 struct rq *rq;
508 struct list_head leaf_rt_rq_list;
509 struct task_group *tg;
510 struct sched_rt_entity *rt_se;
511#endif
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200512};
513
Gregory Haskins57d885f2008-01-25 21:08:18 +0100514#ifdef CONFIG_SMP
515
516/*
517 * We add the notion of a root-domain which will be used to define per-domain
Ingo Molnar0eab9142008-01-25 21:08:19 +0100518 * variables. Each exclusive cpuset essentially defines an island domain by
519 * fully partitioning the member cpus from any other cpuset. Whenever a new
Gregory Haskins57d885f2008-01-25 21:08:18 +0100520 * exclusive cpuset is created, we also create and attach a new root-domain
521 * object.
522 *
Gregory Haskins57d885f2008-01-25 21:08:18 +0100523 */
524struct root_domain {
525 atomic_t refcount;
Rusty Russellc6c49272008-11-25 02:35:05 +1030526 cpumask_var_t span;
527 cpumask_var_t online;
Gregory Haskins637f5082008-01-25 21:08:18 +0100528
Ingo Molnar0eab9142008-01-25 21:08:19 +0100529 /*
Gregory Haskins637f5082008-01-25 21:08:18 +0100530 * The "RT overload" flag: it gets set if a CPU has more than
531 * one runnable RT task.
532 */
Rusty Russellc6c49272008-11-25 02:35:05 +1030533 cpumask_var_t rto_mask;
Ingo Molnar0eab9142008-01-25 21:08:19 +0100534 atomic_t rto_count;
Gregory Haskins6e0534f2008-05-12 21:21:01 +0200535#ifdef CONFIG_SMP
536 struct cpupri cpupri;
537#endif
Vaidyanathan Srinivasan7a09b1a2008-12-18 23:26:22 +0530538#if defined(CONFIG_SCHED_MC) || defined(CONFIG_SCHED_SMT)
539 /*
540 * Preferred wake up cpu nominated by sched_mc balance that will be
541 * used when most cpus are idle in the system indicating overall very
542 * low system utilisation. Triggered at POWERSAVINGS_BALANCE_WAKEUP(2)
543 */
544 unsigned int sched_mc_preferred_wakeup_cpu;
545#endif
Gregory Haskins57d885f2008-01-25 21:08:18 +0100546};
547
Gregory Haskinsdc938522008-01-25 21:08:26 +0100548/*
549 * By default the system creates a single root-domain with all cpus as
550 * members (mimicking the global state we have today).
551 */
Gregory Haskins57d885f2008-01-25 21:08:18 +0100552static struct root_domain def_root_domain;
553
554#endif
555
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200556/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 * This is the main, per-CPU runqueue data structure.
558 *
559 * Locking rule: those places that want to lock multiple runqueues
560 * (such as the load balancing or the thread migration code), lock
561 * acquire operations must be ordered by ascending &runqueue.
562 */
Ingo Molnar70b97a72006-07-03 00:25:42 -0700563struct rq {
Ingo Molnard8016492007-10-18 21:32:55 +0200564 /* runqueue lock: */
565 spinlock_t lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566
567 /*
568 * nr_running and cpu_load should be in the same cacheline because
569 * remote CPUs use both these fields when doing load calculation.
570 */
571 unsigned long nr_running;
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200572 #define CPU_LOAD_IDX_MAX 5
573 unsigned long cpu_load[CPU_LOAD_IDX_MAX];
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -0700574#ifdef CONFIG_NO_HZ
Guillaume Chazarain15934a32008-04-19 19:44:57 +0200575 unsigned long last_tick_seen;
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -0700576 unsigned char in_nohz_recently;
577#endif
Ingo Molnard8016492007-10-18 21:32:55 +0200578 /* capture load from *all* tasks on this cpu: */
579 struct load_weight load;
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200580 unsigned long nr_load_updates;
581 u64 nr_switches;
582
583 struct cfs_rq cfs;
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100584 struct rt_rq rt;
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100585
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200586#ifdef CONFIG_FAIR_GROUP_SCHED
Ingo Molnard8016492007-10-18 21:32:55 +0200587 /* list of leaf cfs_rq on this cpu: */
588 struct list_head leaf_cfs_rq_list;
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100589#endif
590#ifdef CONFIG_RT_GROUP_SCHED
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100591 struct list_head leaf_rt_rq_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593
594 /*
595 * This is part of a global counter where only the total sum
596 * over all CPUs matters. A task can increase this counter on
597 * one CPU and if it got migrated afterwards it may decrease
598 * it on another CPU. Always updated under the runqueue lock:
599 */
600 unsigned long nr_uninterruptible;
601
Ingo Molnar36c8b582006-07-03 00:25:41 -0700602 struct task_struct *curr, *idle;
Christoph Lameterc9819f42006-12-10 02:20:25 -0800603 unsigned long next_balance;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604 struct mm_struct *prev_mm;
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200605
Peter Zijlstra3e51f332008-05-03 18:29:28 +0200606 u64 clock;
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200607
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608 atomic_t nr_iowait;
609
610#ifdef CONFIG_SMP
Ingo Molnar0eab9142008-01-25 21:08:19 +0100611 struct root_domain *rd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 struct sched_domain *sd;
613
Henrik Austada0a522c2009-02-13 20:35:45 +0100614 unsigned char idle_at_tick;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615 /* For active balancing */
616 int active_balance;
617 int push_cpu;
Ingo Molnard8016492007-10-18 21:32:55 +0200618 /* cpu of this runqueue: */
619 int cpu;
Gregory Haskins1f11eb62008-06-04 15:04:05 -0400620 int online;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621
Peter Zijlstraa8a51d52008-06-27 13:41:26 +0200622 unsigned long avg_load_per_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623
Ingo Molnar36c8b582006-07-03 00:25:41 -0700624 struct task_struct *migration_thread;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625 struct list_head migration_queue;
626#endif
627
Thomas Gleixnerdce48a82009-04-11 10:43:41 +0200628 /* calc_load related fields */
629 unsigned long calc_load_update;
630 long calc_load_active;
631
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +0100632#ifdef CONFIG_SCHED_HRTICK
Peter Zijlstra31656512008-07-18 18:01:23 +0200633#ifdef CONFIG_SMP
634 int hrtick_csd_pending;
635 struct call_single_data hrtick_csd;
636#endif
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +0100637 struct hrtimer hrtick_timer;
638#endif
639
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640#ifdef CONFIG_SCHEDSTATS
641 /* latency stats */
642 struct sched_info rq_sched_info;
Ken Chen9c2c4802008-12-16 23:41:22 -0800643 unsigned long long rq_cpu_time;
644 /* could above be rq->cfs_rq.exec_clock + rq->rt_rq.rt_runtime ? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645
646 /* sys_sched_yield() stats */
Ken Chen480b9432007-10-18 21:32:56 +0200647 unsigned int yld_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648
649 /* schedule() stats */
Ken Chen480b9432007-10-18 21:32:56 +0200650 unsigned int sched_switch;
651 unsigned int sched_count;
652 unsigned int sched_goidle;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653
654 /* try_to_wake_up() stats */
Ken Chen480b9432007-10-18 21:32:56 +0200655 unsigned int ttwu_count;
656 unsigned int ttwu_local;
Ingo Molnarb8efb562007-10-15 17:00:10 +0200657
658 /* BKL stats */
Ken Chen480b9432007-10-18 21:32:56 +0200659 unsigned int bkl_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660#endif
661};
662
Fenghua Yuf34e3b62007-07-19 01:48:13 -0700663static DEFINE_PER_CPU_SHARED_ALIGNED(struct rq, runqueues);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664
Peter Zijlstra15afe092008-09-20 23:38:02 +0200665static inline void check_preempt_curr(struct rq *rq, struct task_struct *p, int sync)
Ingo Molnardd41f592007-07-09 18:51:59 +0200666{
Peter Zijlstra15afe092008-09-20 23:38:02 +0200667 rq->curr->sched_class->check_preempt_curr(rq, p, sync);
Ingo Molnardd41f592007-07-09 18:51:59 +0200668}
669
Christoph Lameter0a2966b2006-09-25 23:30:51 -0700670static inline int cpu_of(struct rq *rq)
671{
672#ifdef CONFIG_SMP
673 return rq->cpu;
674#else
675 return 0;
676#endif
677}
678
Ingo Molnar20d315d2007-07-09 18:51:58 +0200679/*
Nick Piggin674311d2005-06-25 14:57:27 -0700680 * The domain tree (rq->sd) is protected by RCU's quiescent state transition.
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -0700681 * See detach_destroy_domains: synchronize_sched for details.
Nick Piggin674311d2005-06-25 14:57:27 -0700682 *
683 * The domain tree of any CPU may only be accessed from within
684 * preempt-disabled sections.
685 */
Ingo Molnar48f24c42006-07-03 00:25:40 -0700686#define for_each_domain(cpu, __sd) \
687 for (__sd = rcu_dereference(cpu_rq(cpu)->sd); __sd; __sd = __sd->parent)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688
689#define cpu_rq(cpu) (&per_cpu(runqueues, (cpu)))
690#define this_rq() (&__get_cpu_var(runqueues))
691#define task_rq(p) cpu_rq(task_cpu(p))
692#define cpu_curr(cpu) (cpu_rq(cpu)->curr)
693
Peter Zijlstra3e51f332008-05-03 18:29:28 +0200694static inline void update_rq_clock(struct rq *rq)
695{
696 rq->clock = sched_clock_cpu(cpu_of(rq));
697}
698
Ingo Molnare436d802007-07-19 21:28:35 +0200699/*
Ingo Molnarbf5c91b2007-10-15 17:00:04 +0200700 * Tunables that become constants when CONFIG_SCHED_DEBUG is off:
701 */
702#ifdef CONFIG_SCHED_DEBUG
703# define const_debug __read_mostly
704#else
705# define const_debug static const
706#endif
707
Ingo Molnar017730c2008-05-12 21:20:52 +0200708/**
709 * runqueue_is_locked
710 *
711 * Returns true if the current cpu runqueue is locked.
712 * This interface allows printk to be called with the runqueue lock
713 * held and know whether or not it is OK to wake up the klogd.
714 */
715int runqueue_is_locked(void)
716{
717 int cpu = get_cpu();
718 struct rq *rq = cpu_rq(cpu);
719 int ret;
720
721 ret = spin_is_locked(&rq->lock);
722 put_cpu();
723 return ret;
724}
725
Ingo Molnarbf5c91b2007-10-15 17:00:04 +0200726/*
727 * Debugging: various feature bits
728 */
Peter Zijlstraf00b45c2008-04-19 19:45:00 +0200729
730#define SCHED_FEAT(name, enabled) \
731 __SCHED_FEAT_##name ,
732
Ingo Molnarbf5c91b2007-10-15 17:00:04 +0200733enum {
Peter Zijlstraf00b45c2008-04-19 19:45:00 +0200734#include "sched_features.h"
Ingo Molnarbf5c91b2007-10-15 17:00:04 +0200735};
736
Peter Zijlstraf00b45c2008-04-19 19:45:00 +0200737#undef SCHED_FEAT
Ingo Molnarbf5c91b2007-10-15 17:00:04 +0200738
Peter Zijlstraf00b45c2008-04-19 19:45:00 +0200739#define SCHED_FEAT(name, enabled) \
740 (1UL << __SCHED_FEAT_##name) * enabled |
741
742const_debug unsigned int sysctl_sched_features =
743#include "sched_features.h"
744 0;
745
746#undef SCHED_FEAT
747
748#ifdef CONFIG_SCHED_DEBUG
749#define SCHED_FEAT(name, enabled) \
750 #name ,
751
Harvey Harrison983ed7a2008-04-24 18:17:55 -0700752static __read_mostly char *sched_feat_names[] = {
Peter Zijlstraf00b45c2008-04-19 19:45:00 +0200753#include "sched_features.h"
754 NULL
755};
756
757#undef SCHED_FEAT
758
Li Zefan34f3a812008-10-30 15:23:32 +0800759static int sched_feat_show(struct seq_file *m, void *v)
Peter Zijlstraf00b45c2008-04-19 19:45:00 +0200760{
Peter Zijlstraf00b45c2008-04-19 19:45:00 +0200761 int i;
762
763 for (i = 0; sched_feat_names[i]; i++) {
Li Zefan34f3a812008-10-30 15:23:32 +0800764 if (!(sysctl_sched_features & (1UL << i)))
765 seq_puts(m, "NO_");
766 seq_printf(m, "%s ", sched_feat_names[i]);
Peter Zijlstraf00b45c2008-04-19 19:45:00 +0200767 }
Li Zefan34f3a812008-10-30 15:23:32 +0800768 seq_puts(m, "\n");
Peter Zijlstraf00b45c2008-04-19 19:45:00 +0200769
Li Zefan34f3a812008-10-30 15:23:32 +0800770 return 0;
Peter Zijlstraf00b45c2008-04-19 19:45:00 +0200771}
772
773static ssize_t
774sched_feat_write(struct file *filp, const char __user *ubuf,
775 size_t cnt, loff_t *ppos)
776{
777 char buf[64];
778 char *cmp = buf;
779 int neg = 0;
780 int i;
781
782 if (cnt > 63)
783 cnt = 63;
784
785 if (copy_from_user(&buf, ubuf, cnt))
786 return -EFAULT;
787
788 buf[cnt] = 0;
789
Ingo Molnarc24b7c52008-04-18 10:55:34 +0200790 if (strncmp(buf, "NO_", 3) == 0) {
Peter Zijlstraf00b45c2008-04-19 19:45:00 +0200791 neg = 1;
792 cmp += 3;
793 }
794
795 for (i = 0; sched_feat_names[i]; i++) {
796 int len = strlen(sched_feat_names[i]);
797
798 if (strncmp(cmp, sched_feat_names[i], len) == 0) {
799 if (neg)
800 sysctl_sched_features &= ~(1UL << i);
801 else
802 sysctl_sched_features |= (1UL << i);
803 break;
804 }
805 }
806
807 if (!sched_feat_names[i])
808 return -EINVAL;
809
810 filp->f_pos += cnt;
811
812 return cnt;
813}
814
Li Zefan34f3a812008-10-30 15:23:32 +0800815static int sched_feat_open(struct inode *inode, struct file *filp)
816{
817 return single_open(filp, sched_feat_show, NULL);
818}
819
Peter Zijlstraf00b45c2008-04-19 19:45:00 +0200820static struct file_operations sched_feat_fops = {
Li Zefan34f3a812008-10-30 15:23:32 +0800821 .open = sched_feat_open,
822 .write = sched_feat_write,
823 .read = seq_read,
824 .llseek = seq_lseek,
825 .release = single_release,
Peter Zijlstraf00b45c2008-04-19 19:45:00 +0200826};
827
828static __init int sched_init_debug(void)
829{
Peter Zijlstraf00b45c2008-04-19 19:45:00 +0200830 debugfs_create_file("sched_features", 0644, NULL, NULL,
831 &sched_feat_fops);
832
833 return 0;
834}
835late_initcall(sched_init_debug);
836
837#endif
838
839#define sched_feat(x) (sysctl_sched_features & (1UL << __SCHED_FEAT_##x))
Ingo Molnarbf5c91b2007-10-15 17:00:04 +0200840
841/*
Peter Zijlstrab82d9fd2007-11-09 22:39:39 +0100842 * Number of tasks to iterate in a single balance run.
843 * Limited because this is done with IRQs disabled.
844 */
845const_debug unsigned int sysctl_sched_nr_migrate = 32;
846
847/*
Peter Zijlstra2398f2c2008-06-27 13:41:35 +0200848 * ratelimit for updating the group shares.
Peter Zijlstra55cd5342008-08-04 08:54:26 +0200849 * default: 0.25ms
Peter Zijlstra2398f2c2008-06-27 13:41:35 +0200850 */
Peter Zijlstra55cd5342008-08-04 08:54:26 +0200851unsigned int sysctl_sched_shares_ratelimit = 250000;
Peter Zijlstra2398f2c2008-06-27 13:41:35 +0200852
853/*
Peter Zijlstraffda12a2008-10-17 19:27:02 +0200854 * Inject some fuzzyness into changing the per-cpu group shares
855 * this avoids remote rq-locks at the expense of fairness.
856 * default: 4
857 */
858unsigned int sysctl_sched_shares_thresh = 4;
859
860/*
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +0100861 * period over which we measure -rt task cpu usage in us.
Peter Zijlstrafa85ae22008-01-25 21:08:29 +0100862 * default: 1s
863 */
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +0100864unsigned int sysctl_sched_rt_period = 1000000;
Peter Zijlstrafa85ae22008-01-25 21:08:29 +0100865
Ingo Molnar6892b752008-02-13 14:02:36 +0100866static __read_mostly int scheduler_running;
867
Peter Zijlstrafa85ae22008-01-25 21:08:29 +0100868/*
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +0100869 * part of the period that we allow rt tasks to run in us.
870 * default: 0.95s
Peter Zijlstrafa85ae22008-01-25 21:08:29 +0100871 */
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +0100872int sysctl_sched_rt_runtime = 950000;
873
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +0200874static inline u64 global_rt_period(void)
875{
876 return (u64)sysctl_sched_rt_period * NSEC_PER_USEC;
877}
878
879static inline u64 global_rt_runtime(void)
880{
roel kluine26873b2008-07-22 16:51:15 -0400881 if (sysctl_sched_rt_runtime < 0)
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +0200882 return RUNTIME_INF;
883
884 return (u64)sysctl_sched_rt_runtime * NSEC_PER_USEC;
885}
Peter Zijlstrafa85ae22008-01-25 21:08:29 +0100886
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887#ifndef prepare_arch_switch
Nick Piggin4866cde2005-06-25 14:57:23 -0700888# define prepare_arch_switch(next) do { } while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889#endif
Nick Piggin4866cde2005-06-25 14:57:23 -0700890#ifndef finish_arch_switch
891# define finish_arch_switch(prev) do { } while (0)
892#endif
893
Dmitry Adamushko051a1d12007-12-18 15:21:13 +0100894static inline int task_current(struct rq *rq, struct task_struct *p)
895{
896 return rq->curr == p;
897}
898
Nick Piggin4866cde2005-06-25 14:57:23 -0700899#ifndef __ARCH_WANT_UNLOCKED_CTXSW
Ingo Molnar70b97a72006-07-03 00:25:42 -0700900static inline int task_running(struct rq *rq, struct task_struct *p)
Nick Piggin4866cde2005-06-25 14:57:23 -0700901{
Dmitry Adamushko051a1d12007-12-18 15:21:13 +0100902 return task_current(rq, p);
Nick Piggin4866cde2005-06-25 14:57:23 -0700903}
904
Ingo Molnar70b97a72006-07-03 00:25:42 -0700905static inline void prepare_lock_switch(struct rq *rq, struct task_struct *next)
Nick Piggin4866cde2005-06-25 14:57:23 -0700906{
907}
908
Ingo Molnar70b97a72006-07-03 00:25:42 -0700909static inline void finish_lock_switch(struct rq *rq, struct task_struct *prev)
Nick Piggin4866cde2005-06-25 14:57:23 -0700910{
Ingo Molnarda04c032005-09-13 11:17:59 +0200911#ifdef CONFIG_DEBUG_SPINLOCK
912 /* this is a valid case when another task releases the spinlock */
913 rq->lock.owner = current;
914#endif
Ingo Molnar8a25d5d2006-07-03 00:24:54 -0700915 /*
916 * If we are tracking spinlock dependencies then we have to
917 * fix up the runqueue lock - which gets 'carried over' from
918 * prev into current:
919 */
920 spin_acquire(&rq->lock.dep_map, 0, 0, _THIS_IP_);
921
Nick Piggin4866cde2005-06-25 14:57:23 -0700922 spin_unlock_irq(&rq->lock);
923}
924
925#else /* __ARCH_WANT_UNLOCKED_CTXSW */
Ingo Molnar70b97a72006-07-03 00:25:42 -0700926static inline int task_running(struct rq *rq, struct task_struct *p)
Nick Piggin4866cde2005-06-25 14:57:23 -0700927{
928#ifdef CONFIG_SMP
929 return p->oncpu;
930#else
Dmitry Adamushko051a1d12007-12-18 15:21:13 +0100931 return task_current(rq, p);
Nick Piggin4866cde2005-06-25 14:57:23 -0700932#endif
933}
934
Ingo Molnar70b97a72006-07-03 00:25:42 -0700935static inline void prepare_lock_switch(struct rq *rq, struct task_struct *next)
Nick Piggin4866cde2005-06-25 14:57:23 -0700936{
937#ifdef CONFIG_SMP
938 /*
939 * We can optimise this out completely for !SMP, because the
940 * SMP rebalancing from interrupt is the only thing that cares
941 * here.
942 */
943 next->oncpu = 1;
944#endif
945#ifdef __ARCH_WANT_INTERRUPTS_ON_CTXSW
946 spin_unlock_irq(&rq->lock);
947#else
948 spin_unlock(&rq->lock);
949#endif
950}
951
Ingo Molnar70b97a72006-07-03 00:25:42 -0700952static inline void finish_lock_switch(struct rq *rq, struct task_struct *prev)
Nick Piggin4866cde2005-06-25 14:57:23 -0700953{
954#ifdef CONFIG_SMP
955 /*
956 * After ->oncpu is cleared, the task can be moved to a different CPU.
957 * We must ensure this doesn't happen until the switch is completely
958 * finished.
959 */
960 smp_wmb();
961 prev->oncpu = 0;
962#endif
963#ifndef __ARCH_WANT_INTERRUPTS_ON_CTXSW
964 local_irq_enable();
965#endif
966}
967#endif /* __ARCH_WANT_UNLOCKED_CTXSW */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968
969/*
Ingo Molnarb29739f2006-06-27 02:54:51 -0700970 * __task_rq_lock - lock the runqueue a given task resides on.
971 * Must be called interrupts disabled.
972 */
Ingo Molnar70b97a72006-07-03 00:25:42 -0700973static inline struct rq *__task_rq_lock(struct task_struct *p)
Ingo Molnarb29739f2006-06-27 02:54:51 -0700974 __acquires(rq->lock)
975{
Andi Kleen3a5c3592007-10-15 17:00:14 +0200976 for (;;) {
977 struct rq *rq = task_rq(p);
978 spin_lock(&rq->lock);
979 if (likely(rq == task_rq(p)))
980 return rq;
Ingo Molnarb29739f2006-06-27 02:54:51 -0700981 spin_unlock(&rq->lock);
Ingo Molnarb29739f2006-06-27 02:54:51 -0700982 }
Ingo Molnarb29739f2006-06-27 02:54:51 -0700983}
984
985/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986 * task_rq_lock - lock the runqueue a given task resides on and disable
Ingo Molnar41a2d6c2007-12-05 15:46:09 +0100987 * interrupts. Note the ordering: we can safely lookup the task_rq without
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988 * explicitly disabling preemption.
989 */
Ingo Molnar70b97a72006-07-03 00:25:42 -0700990static struct rq *task_rq_lock(struct task_struct *p, unsigned long *flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991 __acquires(rq->lock)
992{
Ingo Molnar70b97a72006-07-03 00:25:42 -0700993 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994
Andi Kleen3a5c3592007-10-15 17:00:14 +0200995 for (;;) {
996 local_irq_save(*flags);
997 rq = task_rq(p);
998 spin_lock(&rq->lock);
999 if (likely(rq == task_rq(p)))
1000 return rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001 spin_unlock_irqrestore(&rq->lock, *flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003}
1004
Oleg Nesterovad474ca2008-11-10 15:39:30 +01001005void task_rq_unlock_wait(struct task_struct *p)
1006{
1007 struct rq *rq = task_rq(p);
1008
1009 smp_mb(); /* spin-unlock-wait is not a full memory barrier */
1010 spin_unlock_wait(&rq->lock);
1011}
1012
Alexey Dobriyana9957442007-10-15 17:00:13 +02001013static void __task_rq_unlock(struct rq *rq)
Ingo Molnarb29739f2006-06-27 02:54:51 -07001014 __releases(rq->lock)
1015{
1016 spin_unlock(&rq->lock);
1017}
1018
Ingo Molnar70b97a72006-07-03 00:25:42 -07001019static inline void task_rq_unlock(struct rq *rq, unsigned long *flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020 __releases(rq->lock)
1021{
1022 spin_unlock_irqrestore(&rq->lock, *flags);
1023}
1024
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025/*
Robert P. J. Daycc2a73b2006-12-10 02:20:00 -08001026 * this_rq_lock - lock this runqueue and disable interrupts.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027 */
Alexey Dobriyana9957442007-10-15 17:00:13 +02001028static struct rq *this_rq_lock(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029 __acquires(rq->lock)
1030{
Ingo Molnar70b97a72006-07-03 00:25:42 -07001031 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032
1033 local_irq_disable();
1034 rq = this_rq();
1035 spin_lock(&rq->lock);
1036
1037 return rq;
1038}
1039
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01001040#ifdef CONFIG_SCHED_HRTICK
1041/*
1042 * Use HR-timers to deliver accurate preemption points.
1043 *
1044 * Its all a bit involved since we cannot program an hrt while holding the
1045 * rq->lock. So what we do is store a state in in rq->hrtick_* and ask for a
1046 * reschedule event.
1047 *
1048 * When we get rescheduled we reprogram the hrtick_timer outside of the
1049 * rq->lock.
1050 */
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01001051
1052/*
1053 * Use hrtick when:
1054 * - enabled by features
1055 * - hrtimer is actually high res
1056 */
1057static inline int hrtick_enabled(struct rq *rq)
1058{
1059 if (!sched_feat(HRTICK))
1060 return 0;
Ingo Molnarba420592008-07-20 11:02:06 +02001061 if (!cpu_active(cpu_of(rq)))
Peter Zijlstrab328ca12008-04-29 10:02:46 +02001062 return 0;
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01001063 return hrtimer_is_hres_active(&rq->hrtick_timer);
1064}
1065
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01001066static void hrtick_clear(struct rq *rq)
1067{
1068 if (hrtimer_active(&rq->hrtick_timer))
1069 hrtimer_cancel(&rq->hrtick_timer);
1070}
1071
1072/*
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01001073 * High-resolution timer tick.
1074 * Runs from hardirq context with interrupts disabled.
1075 */
1076static enum hrtimer_restart hrtick(struct hrtimer *timer)
1077{
1078 struct rq *rq = container_of(timer, struct rq, hrtick_timer);
1079
1080 WARN_ON_ONCE(cpu_of(rq) != smp_processor_id());
1081
1082 spin_lock(&rq->lock);
Peter Zijlstra3e51f332008-05-03 18:29:28 +02001083 update_rq_clock(rq);
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01001084 rq->curr->sched_class->task_tick(rq, rq->curr, 1);
1085 spin_unlock(&rq->lock);
1086
1087 return HRTIMER_NORESTART;
1088}
1089
Rabin Vincent95e904c2008-05-11 05:55:33 +05301090#ifdef CONFIG_SMP
Peter Zijlstra31656512008-07-18 18:01:23 +02001091/*
1092 * called from hardirq (IPI) context
1093 */
1094static void __hrtick_start(void *arg)
Peter Zijlstrab328ca12008-04-29 10:02:46 +02001095{
Peter Zijlstra31656512008-07-18 18:01:23 +02001096 struct rq *rq = arg;
Peter Zijlstrab328ca12008-04-29 10:02:46 +02001097
Peter Zijlstra31656512008-07-18 18:01:23 +02001098 spin_lock(&rq->lock);
1099 hrtimer_restart(&rq->hrtick_timer);
1100 rq->hrtick_csd_pending = 0;
1101 spin_unlock(&rq->lock);
Peter Zijlstrab328ca12008-04-29 10:02:46 +02001102}
1103
Peter Zijlstra31656512008-07-18 18:01:23 +02001104/*
1105 * Called to set the hrtick timer state.
1106 *
1107 * called with rq->lock held and irqs disabled
1108 */
1109static void hrtick_start(struct rq *rq, u64 delay)
Peter Zijlstrab328ca12008-04-29 10:02:46 +02001110{
Peter Zijlstra31656512008-07-18 18:01:23 +02001111 struct hrtimer *timer = &rq->hrtick_timer;
1112 ktime_t time = ktime_add_ns(timer->base->get_time(), delay);
Peter Zijlstrab328ca12008-04-29 10:02:46 +02001113
Arjan van de Vencc584b22008-09-01 15:02:30 -07001114 hrtimer_set_expires(timer, time);
Peter Zijlstra31656512008-07-18 18:01:23 +02001115
1116 if (rq == this_rq()) {
1117 hrtimer_restart(timer);
1118 } else if (!rq->hrtick_csd_pending) {
Peter Zijlstra6e275632009-02-25 13:59:48 +01001119 __smp_call_function_single(cpu_of(rq), &rq->hrtick_csd, 0);
Peter Zijlstra31656512008-07-18 18:01:23 +02001120 rq->hrtick_csd_pending = 1;
1121 }
Peter Zijlstrab328ca12008-04-29 10:02:46 +02001122}
1123
1124static int
1125hotplug_hrtick(struct notifier_block *nfb, unsigned long action, void *hcpu)
1126{
1127 int cpu = (int)(long)hcpu;
1128
1129 switch (action) {
1130 case CPU_UP_CANCELED:
1131 case CPU_UP_CANCELED_FROZEN:
1132 case CPU_DOWN_PREPARE:
1133 case CPU_DOWN_PREPARE_FROZEN:
1134 case CPU_DEAD:
1135 case CPU_DEAD_FROZEN:
Peter Zijlstra31656512008-07-18 18:01:23 +02001136 hrtick_clear(cpu_rq(cpu));
Peter Zijlstrab328ca12008-04-29 10:02:46 +02001137 return NOTIFY_OK;
1138 }
1139
1140 return NOTIFY_DONE;
1141}
1142
Rakib Mullickfa748202008-09-22 14:55:45 -07001143static __init void init_hrtick(void)
Peter Zijlstrab328ca12008-04-29 10:02:46 +02001144{
1145 hotcpu_notifier(hotplug_hrtick, 0);
1146}
Peter Zijlstra31656512008-07-18 18:01:23 +02001147#else
1148/*
1149 * Called to set the hrtick timer state.
1150 *
1151 * called with rq->lock held and irqs disabled
1152 */
1153static void hrtick_start(struct rq *rq, u64 delay)
1154{
Peter Zijlstra7f1e2ca2009-03-13 12:21:27 +01001155 __hrtimer_start_range_ns(&rq->hrtick_timer, ns_to_ktime(delay), 0,
1156 HRTIMER_MODE_REL, 0);
Peter Zijlstra31656512008-07-18 18:01:23 +02001157}
1158
Andrew Morton006c75f2008-09-22 14:55:46 -07001159static inline void init_hrtick(void)
Peter Zijlstra31656512008-07-18 18:01:23 +02001160{
1161}
Rabin Vincent95e904c2008-05-11 05:55:33 +05301162#endif /* CONFIG_SMP */
Peter Zijlstrab328ca12008-04-29 10:02:46 +02001163
1164static void init_rq_hrtick(struct rq *rq)
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01001165{
Peter Zijlstra31656512008-07-18 18:01:23 +02001166#ifdef CONFIG_SMP
1167 rq->hrtick_csd_pending = 0;
1168
1169 rq->hrtick_csd.flags = 0;
1170 rq->hrtick_csd.func = __hrtick_start;
1171 rq->hrtick_csd.info = rq;
1172#endif
1173
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01001174 hrtimer_init(&rq->hrtick_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
1175 rq->hrtick_timer.function = hrtick;
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01001176}
Andrew Morton006c75f2008-09-22 14:55:46 -07001177#else /* CONFIG_SCHED_HRTICK */
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01001178static inline void hrtick_clear(struct rq *rq)
1179{
1180}
1181
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01001182static inline void init_rq_hrtick(struct rq *rq)
1183{
1184}
1185
Peter Zijlstrab328ca12008-04-29 10:02:46 +02001186static inline void init_hrtick(void)
1187{
1188}
Andrew Morton006c75f2008-09-22 14:55:46 -07001189#endif /* CONFIG_SCHED_HRTICK */
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01001190
Ingo Molnar1b9f19c2007-07-09 18:51:59 +02001191/*
Ingo Molnarc24d20d2007-07-09 18:51:59 +02001192 * resched_task - mark a task 'to be rescheduled now'.
1193 *
1194 * On UP this means the setting of the need_resched flag, on SMP it
1195 * might also involve a cross-CPU call to trigger the scheduler on
1196 * the target CPU.
1197 */
1198#ifdef CONFIG_SMP
1199
1200#ifndef tsk_is_polling
1201#define tsk_is_polling(t) test_tsk_thread_flag(t, TIF_POLLING_NRFLAG)
1202#endif
1203
Peter Zijlstra31656512008-07-18 18:01:23 +02001204static void resched_task(struct task_struct *p)
Ingo Molnarc24d20d2007-07-09 18:51:59 +02001205{
1206 int cpu;
1207
1208 assert_spin_locked(&task_rq(p)->lock);
1209
Lai Jiangshan5ed0cec2009-03-06 19:40:20 +08001210 if (test_tsk_need_resched(p))
Ingo Molnarc24d20d2007-07-09 18:51:59 +02001211 return;
1212
Lai Jiangshan5ed0cec2009-03-06 19:40:20 +08001213 set_tsk_need_resched(p);
Ingo Molnarc24d20d2007-07-09 18:51:59 +02001214
1215 cpu = task_cpu(p);
1216 if (cpu == smp_processor_id())
1217 return;
1218
1219 /* NEED_RESCHED must be visible before we test polling */
1220 smp_mb();
1221 if (!tsk_is_polling(p))
1222 smp_send_reschedule(cpu);
1223}
1224
1225static void resched_cpu(int cpu)
1226{
1227 struct rq *rq = cpu_rq(cpu);
1228 unsigned long flags;
1229
1230 if (!spin_trylock_irqsave(&rq->lock, flags))
1231 return;
1232 resched_task(cpu_curr(cpu));
1233 spin_unlock_irqrestore(&rq->lock, flags);
1234}
Thomas Gleixner06d83082008-03-22 09:20:24 +01001235
1236#ifdef CONFIG_NO_HZ
1237/*
1238 * When add_timer_on() enqueues a timer into the timer wheel of an
1239 * idle CPU then this timer might expire before the next timer event
1240 * which is scheduled to wake up that CPU. In case of a completely
1241 * idle system the next event might even be infinite time into the
1242 * future. wake_up_idle_cpu() ensures that the CPU is woken up and
1243 * leaves the inner idle loop so the newly added timer is taken into
1244 * account when the CPU goes back to idle and evaluates the timer
1245 * wheel for the next timer event.
1246 */
1247void wake_up_idle_cpu(int cpu)
1248{
1249 struct rq *rq = cpu_rq(cpu);
1250
1251 if (cpu == smp_processor_id())
1252 return;
1253
1254 /*
1255 * This is safe, as this function is called with the timer
1256 * wheel base lock of (cpu) held. When the CPU is on the way
1257 * to idle and has not yet set rq->curr to idle then it will
1258 * be serialized on the timer wheel base lock and take the new
1259 * timer into account automatically.
1260 */
1261 if (rq->curr != rq->idle)
1262 return;
1263
1264 /*
1265 * We can set TIF_RESCHED on the idle task of the other CPU
1266 * lockless. The worst case is that the other CPU runs the
1267 * idle task through an additional NOOP schedule()
1268 */
Lai Jiangshan5ed0cec2009-03-06 19:40:20 +08001269 set_tsk_need_resched(rq->idle);
Thomas Gleixner06d83082008-03-22 09:20:24 +01001270
1271 /* NEED_RESCHED must be visible before we test polling */
1272 smp_mb();
1273 if (!tsk_is_polling(rq->idle))
1274 smp_send_reschedule(cpu);
1275}
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +02001276#endif /* CONFIG_NO_HZ */
Thomas Gleixner06d83082008-03-22 09:20:24 +01001277
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +02001278#else /* !CONFIG_SMP */
Peter Zijlstra31656512008-07-18 18:01:23 +02001279static void resched_task(struct task_struct *p)
Ingo Molnarc24d20d2007-07-09 18:51:59 +02001280{
1281 assert_spin_locked(&task_rq(p)->lock);
Peter Zijlstra31656512008-07-18 18:01:23 +02001282 set_tsk_need_resched(p);
Ingo Molnarc24d20d2007-07-09 18:51:59 +02001283}
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +02001284#endif /* CONFIG_SMP */
Ingo Molnarc24d20d2007-07-09 18:51:59 +02001285
Ingo Molnar45bf76d2007-07-09 18:51:59 +02001286#if BITS_PER_LONG == 32
1287# define WMULT_CONST (~0UL)
1288#else
1289# define WMULT_CONST (1UL << 32)
1290#endif
1291
1292#define WMULT_SHIFT 32
1293
Ingo Molnar194081e2007-08-09 11:16:51 +02001294/*
1295 * Shift right and round:
1296 */
Ingo Molnarcf2ab462007-09-05 14:32:49 +02001297#define SRR(x, y) (((x) + (1UL << ((y) - 1))) >> (y))
Ingo Molnar194081e2007-08-09 11:16:51 +02001298
Peter Zijlstraa7be37a2008-06-27 13:41:11 +02001299/*
1300 * delta *= weight / lw
1301 */
Ingo Molnarcb1c4fc2007-08-02 17:41:40 +02001302static unsigned long
Ingo Molnar45bf76d2007-07-09 18:51:59 +02001303calc_delta_mine(unsigned long delta_exec, unsigned long weight,
1304 struct load_weight *lw)
1305{
1306 u64 tmp;
1307
Lai Jiangshan7a232e02008-06-12 16:43:07 +08001308 if (!lw->inv_weight) {
1309 if (BITS_PER_LONG > 32 && unlikely(lw->weight >= WMULT_CONST))
1310 lw->inv_weight = 1;
1311 else
1312 lw->inv_weight = 1 + (WMULT_CONST-lw->weight/2)
1313 / (lw->weight+1);
1314 }
Ingo Molnar45bf76d2007-07-09 18:51:59 +02001315
1316 tmp = (u64)delta_exec * weight;
1317 /*
1318 * Check whether we'd overflow the 64-bit multiplication:
1319 */
Ingo Molnar194081e2007-08-09 11:16:51 +02001320 if (unlikely(tmp > WMULT_CONST))
Ingo Molnarcf2ab462007-09-05 14:32:49 +02001321 tmp = SRR(SRR(tmp, WMULT_SHIFT/2) * lw->inv_weight,
Ingo Molnar194081e2007-08-09 11:16:51 +02001322 WMULT_SHIFT/2);
1323 else
Ingo Molnarcf2ab462007-09-05 14:32:49 +02001324 tmp = SRR(tmp * lw->inv_weight, WMULT_SHIFT);
Ingo Molnar45bf76d2007-07-09 18:51:59 +02001325
Ingo Molnarecf691d2007-08-02 17:41:40 +02001326 return (unsigned long)min(tmp, (u64)(unsigned long)LONG_MAX);
Ingo Molnar45bf76d2007-07-09 18:51:59 +02001327}
1328
Ingo Molnar10919852007-10-15 17:00:04 +02001329static inline void update_load_add(struct load_weight *lw, unsigned long inc)
Ingo Molnar45bf76d2007-07-09 18:51:59 +02001330{
1331 lw->weight += inc;
Ingo Molnare89996a2008-03-14 23:48:28 +01001332 lw->inv_weight = 0;
Ingo Molnar45bf76d2007-07-09 18:51:59 +02001333}
1334
Ingo Molnar10919852007-10-15 17:00:04 +02001335static inline void update_load_sub(struct load_weight *lw, unsigned long dec)
Ingo Molnar45bf76d2007-07-09 18:51:59 +02001336{
1337 lw->weight -= dec;
Ingo Molnare89996a2008-03-14 23:48:28 +01001338 lw->inv_weight = 0;
Ingo Molnar45bf76d2007-07-09 18:51:59 +02001339}
1340
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341/*
Peter Williams2dd73a42006-06-27 02:54:34 -07001342 * To aid in avoiding the subversion of "niceness" due to uneven distribution
1343 * of tasks with abnormal "nice" values across CPUs the contribution that
1344 * each task makes to its run queue's load is weighted according to its
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01001345 * scheduling class and "nice" value. For SCHED_NORMAL tasks this is just a
Peter Williams2dd73a42006-06-27 02:54:34 -07001346 * scaled version of the new time slice allocation that they receive on time
1347 * slice expiry etc.
1348 */
1349
Peter Zijlstracce7ade2009-01-15 14:53:37 +01001350#define WEIGHT_IDLEPRIO 3
1351#define WMULT_IDLEPRIO 1431655765
Ingo Molnardd41f592007-07-09 18:51:59 +02001352
1353/*
1354 * Nice levels are multiplicative, with a gentle 10% change for every
1355 * nice level changed. I.e. when a CPU-bound task goes from nice 0 to
1356 * nice 1, it will get ~10% less CPU time than another CPU-bound task
1357 * that remained on nice 0.
1358 *
1359 * The "10% effect" is relative and cumulative: from _any_ nice level,
1360 * if you go up 1 level, it's -10% CPU usage, if you go down 1 level
Ingo Molnarf9153ee2007-07-16 09:46:30 +02001361 * it's +10% CPU usage. (to achieve that we use a multiplier of 1.25.
1362 * If a task goes up by ~10% and another task goes down by ~10% then
1363 * the relative distance between them is ~25%.)
Ingo Molnardd41f592007-07-09 18:51:59 +02001364 */
1365static const int prio_to_weight[40] = {
Ingo Molnar254753d2007-08-09 11:16:51 +02001366 /* -20 */ 88761, 71755, 56483, 46273, 36291,
1367 /* -15 */ 29154, 23254, 18705, 14949, 11916,
1368 /* -10 */ 9548, 7620, 6100, 4904, 3906,
1369 /* -5 */ 3121, 2501, 1991, 1586, 1277,
1370 /* 0 */ 1024, 820, 655, 526, 423,
1371 /* 5 */ 335, 272, 215, 172, 137,
1372 /* 10 */ 110, 87, 70, 56, 45,
1373 /* 15 */ 36, 29, 23, 18, 15,
Ingo Molnardd41f592007-07-09 18:51:59 +02001374};
1375
Ingo Molnar5714d2d2007-07-16 09:46:31 +02001376/*
1377 * Inverse (2^32/x) values of the prio_to_weight[] array, precalculated.
1378 *
1379 * In cases where the weight does not change often, we can use the
1380 * precalculated inverse to speed up arithmetics by turning divisions
1381 * into multiplications:
1382 */
Ingo Molnardd41f592007-07-09 18:51:59 +02001383static const u32 prio_to_wmult[40] = {
Ingo Molnar254753d2007-08-09 11:16:51 +02001384 /* -20 */ 48388, 59856, 76040, 92818, 118348,
1385 /* -15 */ 147320, 184698, 229616, 287308, 360437,
1386 /* -10 */ 449829, 563644, 704093, 875809, 1099582,
1387 /* -5 */ 1376151, 1717300, 2157191, 2708050, 3363326,
1388 /* 0 */ 4194304, 5237765, 6557202, 8165337, 10153587,
1389 /* 5 */ 12820798, 15790321, 19976592, 24970740, 31350126,
1390 /* 10 */ 39045157, 49367440, 61356676, 76695844, 95443717,
1391 /* 15 */ 119304647, 148102320, 186737708, 238609294, 286331153,
Ingo Molnardd41f592007-07-09 18:51:59 +02001392};
Peter Williams2dd73a42006-06-27 02:54:34 -07001393
Ingo Molnardd41f592007-07-09 18:51:59 +02001394static void activate_task(struct rq *rq, struct task_struct *p, int wakeup);
1395
1396/*
1397 * runqueue iterator, to support SMP load-balancing between different
1398 * scheduling classes, without having to expose their internal data
1399 * structures to the load-balancing proper:
1400 */
1401struct rq_iterator {
1402 void *arg;
1403 struct task_struct *(*start)(void *);
1404 struct task_struct *(*next)(void *);
1405};
1406
Peter Williamse1d14842007-10-24 18:23:51 +02001407#ifdef CONFIG_SMP
1408static unsigned long
1409balance_tasks(struct rq *this_rq, int this_cpu, struct rq *busiest,
1410 unsigned long max_load_move, struct sched_domain *sd,
1411 enum cpu_idle_type idle, int *all_pinned,
1412 int *this_best_prio, struct rq_iterator *iterator);
1413
1414static int
1415iter_move_one_task(struct rq *this_rq, int this_cpu, struct rq *busiest,
1416 struct sched_domain *sd, enum cpu_idle_type idle,
1417 struct rq_iterator *iterator);
Peter Williamse1d14842007-10-24 18:23:51 +02001418#endif
Ingo Molnardd41f592007-07-09 18:51:59 +02001419
Bharata B Raoef12fef2009-03-31 10:02:22 +05301420/* Time spent by the tasks of the cpu accounting group executing in ... */
1421enum cpuacct_stat_index {
1422 CPUACCT_STAT_USER, /* ... user mode */
1423 CPUACCT_STAT_SYSTEM, /* ... kernel mode */
1424
1425 CPUACCT_STAT_NSTATS,
1426};
1427
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01001428#ifdef CONFIG_CGROUP_CPUACCT
1429static void cpuacct_charge(struct task_struct *tsk, u64 cputime);
Bharata B Raoef12fef2009-03-31 10:02:22 +05301430static void cpuacct_update_stats(struct task_struct *tsk,
1431 enum cpuacct_stat_index idx, cputime_t val);
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01001432#else
1433static inline void cpuacct_charge(struct task_struct *tsk, u64 cputime) {}
Bharata B Raoef12fef2009-03-31 10:02:22 +05301434static inline void cpuacct_update_stats(struct task_struct *tsk,
1435 enum cpuacct_stat_index idx, cputime_t val) {}
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01001436#endif
1437
Peter Zijlstra18d95a22008-04-19 19:45:00 +02001438static inline void inc_cpu_load(struct rq *rq, unsigned long load)
1439{
1440 update_load_add(&rq->load, load);
1441}
1442
1443static inline void dec_cpu_load(struct rq *rq, unsigned long load)
1444{
1445 update_load_sub(&rq->load, load);
1446}
1447
Ingo Molnar7940ca32008-08-19 13:40:47 +02001448#if (defined(CONFIG_SMP) && defined(CONFIG_FAIR_GROUP_SCHED)) || defined(CONFIG_RT_GROUP_SCHED)
Peter Zijlstraeb755802008-08-19 12:33:05 +02001449typedef int (*tg_visitor)(struct task_group *, void *);
1450
1451/*
1452 * Iterate the full tree, calling @down when first entering a node and @up when
1453 * leaving it for the final time.
1454 */
1455static int walk_tg_tree(tg_visitor down, tg_visitor up, void *data)
1456{
1457 struct task_group *parent, *child;
1458 int ret;
1459
1460 rcu_read_lock();
1461 parent = &root_task_group;
1462down:
1463 ret = (*down)(parent, data);
1464 if (ret)
1465 goto out_unlock;
1466 list_for_each_entry_rcu(child, &parent->children, siblings) {
1467 parent = child;
1468 goto down;
1469
1470up:
1471 continue;
1472 }
1473 ret = (*up)(parent, data);
1474 if (ret)
1475 goto out_unlock;
1476
1477 child = parent;
1478 parent = parent->parent;
1479 if (parent)
1480 goto up;
1481out_unlock:
1482 rcu_read_unlock();
1483
1484 return ret;
1485}
1486
1487static int tg_nop(struct task_group *tg, void *data)
1488{
1489 return 0;
1490}
1491#endif
1492
Gregory Haskinse7693a32008-01-25 21:08:09 +01001493#ifdef CONFIG_SMP
1494static unsigned long source_load(int cpu, int type);
1495static unsigned long target_load(int cpu, int type);
Gregory Haskinse7693a32008-01-25 21:08:09 +01001496static int task_hot(struct task_struct *p, u64 now, struct sched_domain *sd);
Peter Zijlstrac09595f2008-06-27 13:41:14 +02001497
Peter Zijlstraa8a51d52008-06-27 13:41:26 +02001498static unsigned long cpu_avg_load_per_task(int cpu)
1499{
1500 struct rq *rq = cpu_rq(cpu);
Ingo Molnaraf6d5962008-11-29 20:45:15 +01001501 unsigned long nr_running = ACCESS_ONCE(rq->nr_running);
Peter Zijlstraa8a51d52008-06-27 13:41:26 +02001502
Steven Rostedt4cd42622008-11-26 21:04:24 -05001503 if (nr_running)
1504 rq->avg_load_per_task = rq->load.weight / nr_running;
Balbir Singha2d47772008-11-12 16:19:00 +05301505 else
1506 rq->avg_load_per_task = 0;
Peter Zijlstraa8a51d52008-06-27 13:41:26 +02001507
1508 return rq->avg_load_per_task;
1509}
1510
Peter Zijlstrac09595f2008-06-27 13:41:14 +02001511#ifdef CONFIG_FAIR_GROUP_SCHED
1512
Peter Zijlstrac09595f2008-06-27 13:41:14 +02001513static void __set_se_shares(struct sched_entity *se, unsigned long shares);
1514
1515/*
1516 * Calculate and set the cpu's group shares.
1517 */
1518static void
Peter Zijlstraffda12a2008-10-17 19:27:02 +02001519update_group_shares_cpu(struct task_group *tg, int cpu,
1520 unsigned long sd_shares, unsigned long sd_rq_weight)
Peter Zijlstrac09595f2008-06-27 13:41:14 +02001521{
Peter Zijlstrac09595f2008-06-27 13:41:14 +02001522 unsigned long shares;
1523 unsigned long rq_weight;
1524
Peter Zijlstrac8cba852008-06-27 13:41:23 +02001525 if (!tg->se[cpu])
Peter Zijlstrac09595f2008-06-27 13:41:14 +02001526 return;
1527
Ken Chenec4e0e22008-11-18 22:41:57 -08001528 rq_weight = tg->cfs_rq[cpu]->rq_weight;
Peter Zijlstrac8cba852008-06-27 13:41:23 +02001529
Peter Zijlstrac09595f2008-06-27 13:41:14 +02001530 /*
1531 * \Sum shares * rq_weight
1532 * shares = -----------------------
1533 * \Sum rq_weight
1534 *
1535 */
Ken Chenec4e0e22008-11-18 22:41:57 -08001536 shares = (sd_shares * rq_weight) / sd_rq_weight;
Peter Zijlstraffda12a2008-10-17 19:27:02 +02001537 shares = clamp_t(unsigned long, shares, MIN_SHARES, MAX_SHARES);
Peter Zijlstrac09595f2008-06-27 13:41:14 +02001538
Peter Zijlstraffda12a2008-10-17 19:27:02 +02001539 if (abs(shares - tg->se[cpu]->load.weight) >
1540 sysctl_sched_shares_thresh) {
1541 struct rq *rq = cpu_rq(cpu);
1542 unsigned long flags;
Peter Zijlstrac09595f2008-06-27 13:41:14 +02001543
Peter Zijlstraffda12a2008-10-17 19:27:02 +02001544 spin_lock_irqsave(&rq->lock, flags);
Ken Chenec4e0e22008-11-18 22:41:57 -08001545 tg->cfs_rq[cpu]->shares = shares;
Peter Zijlstrac09595f2008-06-27 13:41:14 +02001546
Peter Zijlstraffda12a2008-10-17 19:27:02 +02001547 __set_se_shares(tg->se[cpu], shares);
1548 spin_unlock_irqrestore(&rq->lock, flags);
1549 }
Peter Zijlstrac09595f2008-06-27 13:41:14 +02001550}
1551
1552/*
Peter Zijlstrac8cba852008-06-27 13:41:23 +02001553 * Re-compute the task group their per cpu shares over the given domain.
1554 * This needs to be done in a bottom-up fashion because the rq weight of a
1555 * parent group depends on the shares of its child groups.
Peter Zijlstrac09595f2008-06-27 13:41:14 +02001556 */
Peter Zijlstraeb755802008-08-19 12:33:05 +02001557static int tg_shares_up(struct task_group *tg, void *data)
Peter Zijlstrac09595f2008-06-27 13:41:14 +02001558{
Ken Chenec4e0e22008-11-18 22:41:57 -08001559 unsigned long weight, rq_weight = 0;
Peter Zijlstrac8cba852008-06-27 13:41:23 +02001560 unsigned long shares = 0;
Peter Zijlstraeb755802008-08-19 12:33:05 +02001561 struct sched_domain *sd = data;
Peter Zijlstrac09595f2008-06-27 13:41:14 +02001562 int i;
1563
Rusty Russell758b2cd2008-11-25 02:35:04 +10301564 for_each_cpu(i, sched_domain_span(sd)) {
Ken Chenec4e0e22008-11-18 22:41:57 -08001565 /*
1566 * If there are currently no tasks on the cpu pretend there
1567 * is one of average load so that when a new task gets to
1568 * run here it will not get delayed by group starvation.
1569 */
1570 weight = tg->cfs_rq[i]->load.weight;
1571 if (!weight)
1572 weight = NICE_0_LOAD;
1573
1574 tg->cfs_rq[i]->rq_weight = weight;
1575 rq_weight += weight;
Peter Zijlstrac8cba852008-06-27 13:41:23 +02001576 shares += tg->cfs_rq[i]->shares;
1577 }
1578
1579 if ((!shares && rq_weight) || shares > tg->shares)
1580 shares = tg->shares;
1581
1582 if (!sd->parent || !(sd->parent->flags & SD_LOAD_BALANCE))
1583 shares = tg->shares;
1584
Rusty Russell758b2cd2008-11-25 02:35:04 +10301585 for_each_cpu(i, sched_domain_span(sd))
Peter Zijlstraffda12a2008-10-17 19:27:02 +02001586 update_group_shares_cpu(tg, i, shares, rq_weight);
Peter Zijlstraeb755802008-08-19 12:33:05 +02001587
1588 return 0;
Peter Zijlstrac09595f2008-06-27 13:41:14 +02001589}
1590
1591/*
Peter Zijlstrac8cba852008-06-27 13:41:23 +02001592 * Compute the cpu's hierarchical load factor for each task group.
1593 * This needs to be done in a top-down fashion because the load of a child
1594 * group is a fraction of its parents load.
Peter Zijlstrac09595f2008-06-27 13:41:14 +02001595 */
Peter Zijlstraeb755802008-08-19 12:33:05 +02001596static int tg_load_down(struct task_group *tg, void *data)
Peter Zijlstrac09595f2008-06-27 13:41:14 +02001597{
Peter Zijlstrac8cba852008-06-27 13:41:23 +02001598 unsigned long load;
Peter Zijlstraeb755802008-08-19 12:33:05 +02001599 long cpu = (long)data;
Peter Zijlstrac09595f2008-06-27 13:41:14 +02001600
Peter Zijlstrac8cba852008-06-27 13:41:23 +02001601 if (!tg->parent) {
1602 load = cpu_rq(cpu)->load.weight;
1603 } else {
1604 load = tg->parent->cfs_rq[cpu]->h_load;
1605 load *= tg->cfs_rq[cpu]->shares;
1606 load /= tg->parent->cfs_rq[cpu]->load.weight + 1;
1607 }
1608
1609 tg->cfs_rq[cpu]->h_load = load;
Peter Zijlstrac09595f2008-06-27 13:41:14 +02001610
Peter Zijlstraeb755802008-08-19 12:33:05 +02001611 return 0;
Peter Zijlstra4d8d5952008-06-27 13:41:19 +02001612}
1613
Peter Zijlstrac8cba852008-06-27 13:41:23 +02001614static void update_shares(struct sched_domain *sd)
Peter Zijlstrac09595f2008-06-27 13:41:14 +02001615{
Peter Zijlstra2398f2c2008-06-27 13:41:35 +02001616 u64 now = cpu_clock(raw_smp_processor_id());
1617 s64 elapsed = now - sd->last_update;
1618
1619 if (elapsed >= (s64)(u64)sysctl_sched_shares_ratelimit) {
1620 sd->last_update = now;
Peter Zijlstraeb755802008-08-19 12:33:05 +02001621 walk_tg_tree(tg_nop, tg_shares_up, sd);
Peter Zijlstra2398f2c2008-06-27 13:41:35 +02001622 }
Peter Zijlstrac09595f2008-06-27 13:41:14 +02001623}
1624
Peter Zijlstra3e5459b2008-06-27 13:41:24 +02001625static void update_shares_locked(struct rq *rq, struct sched_domain *sd)
1626{
1627 spin_unlock(&rq->lock);
1628 update_shares(sd);
1629 spin_lock(&rq->lock);
1630}
1631
Peter Zijlstraeb755802008-08-19 12:33:05 +02001632static void update_h_load(long cpu)
Peter Zijlstrac09595f2008-06-27 13:41:14 +02001633{
Peter Zijlstraeb755802008-08-19 12:33:05 +02001634 walk_tg_tree(tg_load_down, tg_nop, (void *)cpu);
Peter Zijlstrac09595f2008-06-27 13:41:14 +02001635}
1636
Peter Zijlstrac09595f2008-06-27 13:41:14 +02001637#else
1638
Peter Zijlstrac8cba852008-06-27 13:41:23 +02001639static inline void update_shares(struct sched_domain *sd)
Peter Zijlstrac09595f2008-06-27 13:41:14 +02001640{
1641}
1642
Peter Zijlstra3e5459b2008-06-27 13:41:24 +02001643static inline void update_shares_locked(struct rq *rq, struct sched_domain *sd)
1644{
1645}
1646
Peter Zijlstrac09595f2008-06-27 13:41:14 +02001647#endif
1648
Gregory Haskins8f45e2b2008-12-29 09:39:51 -05001649#ifdef CONFIG_PREEMPT
1650
Alexey Dobriyan70574a92008-11-28 22:08:00 +03001651/*
Gregory Haskins8f45e2b2008-12-29 09:39:51 -05001652 * fair double_lock_balance: Safely acquires both rq->locks in a fair
1653 * way at the expense of forcing extra atomic operations in all
1654 * invocations. This assures that the double_lock is acquired using the
1655 * same underlying policy as the spinlock_t on this architecture, which
1656 * reduces latency compared to the unfair variant below. However, it
1657 * also adds more overhead and therefore may reduce throughput.
Alexey Dobriyan70574a92008-11-28 22:08:00 +03001658 */
Gregory Haskins8f45e2b2008-12-29 09:39:51 -05001659static inline int _double_lock_balance(struct rq *this_rq, struct rq *busiest)
1660 __releases(this_rq->lock)
1661 __acquires(busiest->lock)
1662 __acquires(this_rq->lock)
1663{
1664 spin_unlock(&this_rq->lock);
1665 double_rq_lock(this_rq, busiest);
1666
1667 return 1;
1668}
1669
1670#else
1671/*
1672 * Unfair double_lock_balance: Optimizes throughput at the expense of
1673 * latency by eliminating extra atomic operations when the locks are
1674 * already in proper order on entry. This favors lower cpu-ids and will
1675 * grant the double lock to lower cpus over higher ids under contention,
1676 * regardless of entry order into the function.
1677 */
1678static int _double_lock_balance(struct rq *this_rq, struct rq *busiest)
Alexey Dobriyan70574a92008-11-28 22:08:00 +03001679 __releases(this_rq->lock)
1680 __acquires(busiest->lock)
1681 __acquires(this_rq->lock)
1682{
1683 int ret = 0;
1684
Alexey Dobriyan70574a92008-11-28 22:08:00 +03001685 if (unlikely(!spin_trylock(&busiest->lock))) {
1686 if (busiest < this_rq) {
1687 spin_unlock(&this_rq->lock);
1688 spin_lock(&busiest->lock);
1689 spin_lock_nested(&this_rq->lock, SINGLE_DEPTH_NESTING);
1690 ret = 1;
1691 } else
1692 spin_lock_nested(&busiest->lock, SINGLE_DEPTH_NESTING);
1693 }
1694 return ret;
1695}
1696
Gregory Haskins8f45e2b2008-12-29 09:39:51 -05001697#endif /* CONFIG_PREEMPT */
1698
1699/*
1700 * double_lock_balance - lock the busiest runqueue, this_rq is locked already.
1701 */
1702static int double_lock_balance(struct rq *this_rq, struct rq *busiest)
1703{
1704 if (unlikely(!irqs_disabled())) {
1705 /* printk() doesn't work good under rq->lock */
1706 spin_unlock(&this_rq->lock);
1707 BUG_ON(1);
1708 }
1709
1710 return _double_lock_balance(this_rq, busiest);
1711}
1712
Alexey Dobriyan70574a92008-11-28 22:08:00 +03001713static inline void double_unlock_balance(struct rq *this_rq, struct rq *busiest)
1714 __releases(busiest->lock)
1715{
1716 spin_unlock(&busiest->lock);
1717 lock_set_subclass(&this_rq->lock.dep_map, 0, _RET_IP_);
1718}
Peter Zijlstra18d95a22008-04-19 19:45:00 +02001719#endif
Peter Zijlstra18d95a22008-04-19 19:45:00 +02001720
1721#ifdef CONFIG_FAIR_GROUP_SCHED
1722static void cfs_rq_set_shares(struct cfs_rq *cfs_rq, unsigned long shares)
1723{
Vegard Nossum30432092008-06-27 21:35:50 +02001724#ifdef CONFIG_SMP
Ingo Molnar34e83e82008-06-27 15:42:36 +02001725 cfs_rq->shares = shares;
1726#endif
Peter Zijlstra18d95a22008-04-19 19:45:00 +02001727}
1728#endif
1729
Thomas Gleixnerdce48a82009-04-11 10:43:41 +02001730static void calc_load_account_active(struct rq *this_rq);
1731
Ingo Molnardd41f592007-07-09 18:51:59 +02001732#include "sched_stats.h"
Ingo Molnardd41f592007-07-09 18:51:59 +02001733#include "sched_idletask.c"
Ingo Molnar5522d5d2007-10-15 17:00:12 +02001734#include "sched_fair.c"
1735#include "sched_rt.c"
Ingo Molnardd41f592007-07-09 18:51:59 +02001736#ifdef CONFIG_SCHED_DEBUG
1737# include "sched_debug.c"
1738#endif
1739
1740#define sched_class_highest (&rt_sched_class)
Gregory Haskins1f11eb62008-06-04 15:04:05 -04001741#define for_each_class(class) \
1742 for (class = sched_class_highest; class; class = class->next)
Ingo Molnardd41f592007-07-09 18:51:59 +02001743
Peter Zijlstrac09595f2008-06-27 13:41:14 +02001744static void inc_nr_running(struct rq *rq)
Ingo Molnar6363ca52008-05-29 11:28:57 +02001745{
1746 rq->nr_running++;
Ingo Molnar6363ca52008-05-29 11:28:57 +02001747}
1748
Peter Zijlstrac09595f2008-06-27 13:41:14 +02001749static void dec_nr_running(struct rq *rq)
Ingo Molnar9c217242007-08-02 17:41:40 +02001750{
1751 rq->nr_running--;
Ingo Molnar9c217242007-08-02 17:41:40 +02001752}
1753
Ingo Molnar45bf76d2007-07-09 18:51:59 +02001754static void set_load_weight(struct task_struct *p)
1755{
1756 if (task_has_rt_policy(p)) {
Ingo Molnardd41f592007-07-09 18:51:59 +02001757 p->se.load.weight = prio_to_weight[0] * 2;
1758 p->se.load.inv_weight = prio_to_wmult[0] >> 1;
1759 return;
1760 }
1761
1762 /*
1763 * SCHED_IDLE tasks get minimal weight:
1764 */
1765 if (p->policy == SCHED_IDLE) {
1766 p->se.load.weight = WEIGHT_IDLEPRIO;
1767 p->se.load.inv_weight = WMULT_IDLEPRIO;
1768 return;
1769 }
1770
1771 p->se.load.weight = prio_to_weight[p->static_prio - MAX_RT_PRIO];
1772 p->se.load.inv_weight = prio_to_wmult[p->static_prio - MAX_RT_PRIO];
Ingo Molnar45bf76d2007-07-09 18:51:59 +02001773}
1774
Gregory Haskins2087a1a2008-06-27 14:30:00 -06001775static void update_avg(u64 *avg, u64 sample)
1776{
1777 s64 diff = sample - *avg;
1778 *avg += diff >> 3;
1779}
1780
Ingo Molnar8159f872007-08-09 11:16:49 +02001781static void enqueue_task(struct rq *rq, struct task_struct *p, int wakeup)
Ingo Molnar71f8bd42007-07-09 18:51:59 +02001782{
Peter Zijlstra831451a2009-01-14 12:39:18 +01001783 if (wakeup)
1784 p->se.start_runtime = p->se.sum_exec_runtime;
1785
Ingo Molnar71f8bd42007-07-09 18:51:59 +02001786 sched_info_queued(p);
Ingo Molnarfd390f62007-08-09 11:16:48 +02001787 p->sched_class->enqueue_task(rq, p, wakeup);
Ingo Molnardd41f592007-07-09 18:51:59 +02001788 p->se.on_rq = 1;
1789}
1790
Ingo Molnar69be72c2007-08-09 11:16:49 +02001791static void dequeue_task(struct rq *rq, struct task_struct *p, int sleep)
Ingo Molnardd41f592007-07-09 18:51:59 +02001792{
Peter Zijlstra831451a2009-01-14 12:39:18 +01001793 if (sleep) {
1794 if (p->se.last_wakeup) {
1795 update_avg(&p->se.avg_overlap,
1796 p->se.sum_exec_runtime - p->se.last_wakeup);
1797 p->se.last_wakeup = 0;
1798 } else {
1799 update_avg(&p->se.avg_wakeup,
1800 sysctl_sched_wakeup_granularity);
1801 }
Gregory Haskins2087a1a2008-06-27 14:30:00 -06001802 }
1803
Ankita Garg46ac22b2008-07-01 14:30:06 +05301804 sched_info_dequeued(p);
Ingo Molnarf02231e2007-08-09 11:16:48 +02001805 p->sched_class->dequeue_task(rq, p, sleep);
Ingo Molnardd41f592007-07-09 18:51:59 +02001806 p->se.on_rq = 0;
Ingo Molnar71f8bd42007-07-09 18:51:59 +02001807}
1808
1809/*
Ingo Molnardd41f592007-07-09 18:51:59 +02001810 * __normal_prio - return the priority that is based on the static prio
Ingo Molnar71f8bd42007-07-09 18:51:59 +02001811 */
Ingo Molnar14531182007-07-09 18:51:59 +02001812static inline int __normal_prio(struct task_struct *p)
1813{
Ingo Molnardd41f592007-07-09 18:51:59 +02001814 return p->static_prio;
Ingo Molnar14531182007-07-09 18:51:59 +02001815}
1816
1817/*
Ingo Molnarb29739f2006-06-27 02:54:51 -07001818 * Calculate the expected normal priority: i.e. priority
1819 * without taking RT-inheritance into account. Might be
1820 * boosted by interactivity modifiers. Changes upon fork,
1821 * setprio syscalls, and whenever the interactivity
1822 * estimator recalculates.
1823 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07001824static inline int normal_prio(struct task_struct *p)
Ingo Molnarb29739f2006-06-27 02:54:51 -07001825{
1826 int prio;
1827
Ingo Molnare05606d2007-07-09 18:51:59 +02001828 if (task_has_rt_policy(p))
Ingo Molnarb29739f2006-06-27 02:54:51 -07001829 prio = MAX_RT_PRIO-1 - p->rt_priority;
1830 else
1831 prio = __normal_prio(p);
1832 return prio;
1833}
1834
1835/*
1836 * Calculate the current priority, i.e. the priority
1837 * taken into account by the scheduler. This value might
1838 * be boosted by RT tasks, or might be boosted by
1839 * interactivity modifiers. Will be RT if the task got
1840 * RT-boosted. If not then it returns p->normal_prio.
1841 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07001842static int effective_prio(struct task_struct *p)
Ingo Molnarb29739f2006-06-27 02:54:51 -07001843{
1844 p->normal_prio = normal_prio(p);
1845 /*
1846 * If we are RT tasks or we were boosted to RT priority,
1847 * keep the priority unchanged. Otherwise, update priority
1848 * to the normal priority:
1849 */
1850 if (!rt_prio(p->prio))
1851 return p->normal_prio;
1852 return p->prio;
1853}
1854
1855/*
Ingo Molnardd41f592007-07-09 18:51:59 +02001856 * activate_task - move a task to the runqueue.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001857 */
Ingo Molnardd41f592007-07-09 18:51:59 +02001858static void activate_task(struct rq *rq, struct task_struct *p, int wakeup)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001859{
Matthew Wilcoxd9514f62007-12-06 11:07:07 -05001860 if (task_contributes_to_load(p))
Ingo Molnardd41f592007-07-09 18:51:59 +02001861 rq->nr_uninterruptible--;
1862
Ingo Molnar8159f872007-08-09 11:16:49 +02001863 enqueue_task(rq, p, wakeup);
Peter Zijlstrac09595f2008-06-27 13:41:14 +02001864 inc_nr_running(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001865}
1866
1867/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001868 * deactivate_task - remove a task from the runqueue.
1869 */
Ingo Molnar2e1cb742007-08-09 11:16:49 +02001870static void deactivate_task(struct rq *rq, struct task_struct *p, int sleep)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001871{
Matthew Wilcoxd9514f62007-12-06 11:07:07 -05001872 if (task_contributes_to_load(p))
Ingo Molnardd41f592007-07-09 18:51:59 +02001873 rq->nr_uninterruptible++;
1874
Ingo Molnar69be72c2007-08-09 11:16:49 +02001875 dequeue_task(rq, p, sleep);
Peter Zijlstrac09595f2008-06-27 13:41:14 +02001876 dec_nr_running(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001877}
1878
Linus Torvalds1da177e2005-04-16 15:20:36 -07001879/**
1880 * task_curr - is this task currently executing on a CPU?
1881 * @p: the task in question.
1882 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07001883inline int task_curr(const struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001884{
1885 return cpu_curr(task_cpu(p)) == p;
1886}
1887
Ingo Molnardd41f592007-07-09 18:51:59 +02001888static inline void __set_task_cpu(struct task_struct *p, unsigned int cpu)
1889{
Peter Zijlstra6f505b12008-01-25 21:08:30 +01001890 set_task_rq(p, cpu);
Ingo Molnardd41f592007-07-09 18:51:59 +02001891#ifdef CONFIG_SMP
Dmitry Adamushkoce96b5a2007-11-15 20:57:40 +01001892 /*
1893 * After ->cpu is set up to a new value, task_rq_lock(p, ...) can be
1894 * successfuly executed on another CPU. We must ensure that updates of
1895 * per-task data have been completed by this moment.
1896 */
1897 smp_wmb();
Ingo Molnardd41f592007-07-09 18:51:59 +02001898 task_thread_info(p)->cpu = cpu;
Ingo Molnardd41f592007-07-09 18:51:59 +02001899#endif
Peter Williams2dd73a42006-06-27 02:54:34 -07001900}
1901
Steven Rostedtcb469842008-01-25 21:08:22 +01001902static inline void check_class_changed(struct rq *rq, struct task_struct *p,
1903 const struct sched_class *prev_class,
1904 int oldprio, int running)
1905{
1906 if (prev_class != p->sched_class) {
1907 if (prev_class->switched_from)
1908 prev_class->switched_from(rq, p, running);
1909 p->sched_class->switched_to(rq, p, running);
1910 } else
1911 p->sched_class->prio_changed(rq, p, oldprio, running);
1912}
1913
Linus Torvalds1da177e2005-04-16 15:20:36 -07001914#ifdef CONFIG_SMP
Ingo Molnarc65cc872007-07-09 18:51:58 +02001915
Thomas Gleixnere958b362008-06-04 23:22:32 +02001916/* Used instead of source_load when we know the type == 0 */
1917static unsigned long weighted_cpuload(const int cpu)
1918{
1919 return cpu_rq(cpu)->load.weight;
1920}
1921
Ingo Molnarcc367732007-10-15 17:00:18 +02001922/*
1923 * Is this task likely cache-hot:
1924 */
Gregory Haskinse7693a32008-01-25 21:08:09 +01001925static int
Ingo Molnarcc367732007-10-15 17:00:18 +02001926task_hot(struct task_struct *p, u64 now, struct sched_domain *sd)
1927{
1928 s64 delta;
1929
Ingo Molnarf540a602008-03-15 17:10:34 +01001930 /*
1931 * Buddy candidates are cache hot:
1932 */
Peter Zijlstra47932412008-11-04 21:25:09 +01001933 if (sched_feat(CACHE_HOT_BUDDY) &&
1934 (&p->se == cfs_rq_of(&p->se)->next ||
1935 &p->se == cfs_rq_of(&p->se)->last))
Ingo Molnarf540a602008-03-15 17:10:34 +01001936 return 1;
1937
Ingo Molnarcc367732007-10-15 17:00:18 +02001938 if (p->sched_class != &fair_sched_class)
1939 return 0;
1940
Ingo Molnar6bc16652007-10-15 17:00:18 +02001941 if (sysctl_sched_migration_cost == -1)
1942 return 1;
1943 if (sysctl_sched_migration_cost == 0)
1944 return 0;
1945
Ingo Molnarcc367732007-10-15 17:00:18 +02001946 delta = now - p->se.exec_start;
1947
1948 return delta < (s64)sysctl_sched_migration_cost;
1949}
1950
1951
Ingo Molnardd41f592007-07-09 18:51:59 +02001952void set_task_cpu(struct task_struct *p, unsigned int new_cpu)
Ingo Molnarc65cc872007-07-09 18:51:58 +02001953{
Ingo Molnardd41f592007-07-09 18:51:59 +02001954 int old_cpu = task_cpu(p);
1955 struct rq *old_rq = cpu_rq(old_cpu), *new_rq = cpu_rq(new_cpu);
Srivatsa Vaddagiri2830cf82007-10-15 17:00:12 +02001956 struct cfs_rq *old_cfsrq = task_cfs_rq(p),
1957 *new_cfsrq = cpu_cfs_rq(old_cfsrq, new_cpu);
Ingo Molnarbbdba7c2007-10-15 17:00:06 +02001958 u64 clock_offset;
Ingo Molnardd41f592007-07-09 18:51:59 +02001959
1960 clock_offset = old_rq->clock - new_rq->clock;
Ingo Molnar6cfb0d52007-08-02 17:41:40 +02001961
Mathieu Desnoyersde1d7282009-05-05 16:49:59 +08001962 trace_sched_migrate_task(p, new_cpu);
Peter Zijlstracbc34ed2008-12-10 08:08:22 +01001963
Ingo Molnar6cfb0d52007-08-02 17:41:40 +02001964#ifdef CONFIG_SCHEDSTATS
1965 if (p->se.wait_start)
1966 p->se.wait_start -= clock_offset;
Ingo Molnardd41f592007-07-09 18:51:59 +02001967 if (p->se.sleep_start)
1968 p->se.sleep_start -= clock_offset;
1969 if (p->se.block_start)
1970 p->se.block_start -= clock_offset;
Ingo Molnarcc367732007-10-15 17:00:18 +02001971 if (old_cpu != new_cpu) {
1972 schedstat_inc(p, se.nr_migrations);
1973 if (task_hot(p, old_rq->clock, NULL))
1974 schedstat_inc(p, se.nr_forced2_migrations);
1975 }
Ingo Molnar6cfb0d52007-08-02 17:41:40 +02001976#endif
Srivatsa Vaddagiri2830cf82007-10-15 17:00:12 +02001977 p->se.vruntime -= old_cfsrq->min_vruntime -
1978 new_cfsrq->min_vruntime;
Ingo Molnardd41f592007-07-09 18:51:59 +02001979
1980 __set_task_cpu(p, new_cpu);
Ingo Molnarc65cc872007-07-09 18:51:58 +02001981}
1982
Ingo Molnar70b97a72006-07-03 00:25:42 -07001983struct migration_req {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001984 struct list_head list;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001985
Ingo Molnar36c8b582006-07-03 00:25:41 -07001986 struct task_struct *task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001987 int dest_cpu;
1988
Linus Torvalds1da177e2005-04-16 15:20:36 -07001989 struct completion done;
Ingo Molnar70b97a72006-07-03 00:25:42 -07001990};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001991
1992/*
1993 * The task's runqueue lock must be held.
1994 * Returns true if you have to wait for migration thread.
1995 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07001996static int
Ingo Molnar70b97a72006-07-03 00:25:42 -07001997migrate_task(struct task_struct *p, int dest_cpu, struct migration_req *req)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001998{
Ingo Molnar70b97a72006-07-03 00:25:42 -07001999 struct rq *rq = task_rq(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002000
2001 /*
2002 * If the task is not on a runqueue (and not running), then
2003 * it is sufficient to simply update the task's cpu field.
2004 */
Ingo Molnardd41f592007-07-09 18:51:59 +02002005 if (!p->se.on_rq && !task_running(rq, p)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002006 set_task_cpu(p, dest_cpu);
2007 return 0;
2008 }
2009
2010 init_completion(&req->done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002011 req->task = p;
2012 req->dest_cpu = dest_cpu;
2013 list_add(&req->list, &rq->migration_queue);
Ingo Molnar48f24c42006-07-03 00:25:40 -07002014
Linus Torvalds1da177e2005-04-16 15:20:36 -07002015 return 1;
2016}
2017
2018/*
Markus Metzgera26b89f2009-04-03 16:43:34 +02002019 * wait_task_context_switch - wait for a thread to complete at least one
2020 * context switch.
2021 *
2022 * @p must not be current.
2023 */
2024void wait_task_context_switch(struct task_struct *p)
2025{
2026 unsigned long nvcsw, nivcsw, flags;
2027 int running;
2028 struct rq *rq;
2029
2030 nvcsw = p->nvcsw;
2031 nivcsw = p->nivcsw;
2032 for (;;) {
2033 /*
2034 * The runqueue is assigned before the actual context
2035 * switch. We need to take the runqueue lock.
2036 *
2037 * We could check initially without the lock but it is
2038 * very likely that we need to take the lock in every
2039 * iteration.
2040 */
2041 rq = task_rq_lock(p, &flags);
2042 running = task_running(rq, p);
2043 task_rq_unlock(rq, &flags);
2044
2045 if (likely(!running))
2046 break;
2047 /*
2048 * The switch count is incremented before the actual
2049 * context switch. We thus wait for two switches to be
2050 * sure at least one completed.
2051 */
2052 if ((p->nvcsw - nvcsw) > 1)
2053 break;
2054 if ((p->nivcsw - nivcsw) > 1)
2055 break;
2056
2057 cpu_relax();
2058 }
2059}
2060
2061/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002062 * wait_task_inactive - wait for a thread to unschedule.
2063 *
Roland McGrath85ba2d82008-07-25 19:45:58 -07002064 * If @match_state is nonzero, it's the @p->state value just checked and
2065 * not expected to change. If it changes, i.e. @p might have woken up,
2066 * then return zero. When we succeed in waiting for @p to be off its CPU,
2067 * we return a positive number (its total switch count). If a second call
2068 * a short while later returns the same number, the caller can be sure that
2069 * @p has remained unscheduled the whole time.
2070 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002071 * The caller must ensure that the task *will* unschedule sometime soon,
2072 * else this function might spin for a *long* time. This function can't
2073 * be called with interrupts off, or it may introduce deadlock with
2074 * smp_call_function() if an IPI is sent by the same process we are
2075 * waiting to become inactive.
2076 */
Roland McGrath85ba2d82008-07-25 19:45:58 -07002077unsigned long wait_task_inactive(struct task_struct *p, long match_state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002078{
2079 unsigned long flags;
Ingo Molnardd41f592007-07-09 18:51:59 +02002080 int running, on_rq;
Roland McGrath85ba2d82008-07-25 19:45:58 -07002081 unsigned long ncsw;
Ingo Molnar70b97a72006-07-03 00:25:42 -07002082 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002083
Andi Kleen3a5c3592007-10-15 17:00:14 +02002084 for (;;) {
2085 /*
2086 * We do the initial early heuristics without holding
2087 * any task-queue locks at all. We'll only try to get
2088 * the runqueue lock when things look like they will
2089 * work out!
2090 */
2091 rq = task_rq(p);
Linus Torvaldsfa490cf2007-06-18 09:34:40 -07002092
Andi Kleen3a5c3592007-10-15 17:00:14 +02002093 /*
2094 * If the task is actively running on another CPU
2095 * still, just relax and busy-wait without holding
2096 * any locks.
2097 *
2098 * NOTE! Since we don't hold any locks, it's not
2099 * even sure that "rq" stays as the right runqueue!
2100 * But we don't care, since "task_running()" will
2101 * return false if the runqueue has changed and p
2102 * is actually now running somewhere else!
2103 */
Roland McGrath85ba2d82008-07-25 19:45:58 -07002104 while (task_running(rq, p)) {
2105 if (match_state && unlikely(p->state != match_state))
2106 return 0;
Andi Kleen3a5c3592007-10-15 17:00:14 +02002107 cpu_relax();
Roland McGrath85ba2d82008-07-25 19:45:58 -07002108 }
Linus Torvaldsfa490cf2007-06-18 09:34:40 -07002109
Andi Kleen3a5c3592007-10-15 17:00:14 +02002110 /*
2111 * Ok, time to look more closely! We need the rq
2112 * lock now, to be *sure*. If we're wrong, we'll
2113 * just go back and repeat.
2114 */
2115 rq = task_rq_lock(p, &flags);
Mathieu Desnoyers0a16b602008-07-18 12:16:17 -04002116 trace_sched_wait_task(rq, p);
Andi Kleen3a5c3592007-10-15 17:00:14 +02002117 running = task_running(rq, p);
2118 on_rq = p->se.on_rq;
Roland McGrath85ba2d82008-07-25 19:45:58 -07002119 ncsw = 0;
Oleg Nesterovf31e11d2008-08-20 16:54:44 -07002120 if (!match_state || p->state == match_state)
Oleg Nesterov93dcf552008-08-20 16:54:44 -07002121 ncsw = p->nvcsw | LONG_MIN; /* sets MSB */
Andi Kleen3a5c3592007-10-15 17:00:14 +02002122 task_rq_unlock(rq, &flags);
Linus Torvaldsfa490cf2007-06-18 09:34:40 -07002123
Andi Kleen3a5c3592007-10-15 17:00:14 +02002124 /*
Roland McGrath85ba2d82008-07-25 19:45:58 -07002125 * If it changed from the expected state, bail out now.
2126 */
2127 if (unlikely(!ncsw))
2128 break;
2129
2130 /*
Andi Kleen3a5c3592007-10-15 17:00:14 +02002131 * Was it really running after all now that we
2132 * checked with the proper locks actually held?
2133 *
2134 * Oops. Go back and try again..
2135 */
2136 if (unlikely(running)) {
2137 cpu_relax();
2138 continue;
2139 }
2140
2141 /*
2142 * It's not enough that it's not actively running,
2143 * it must be off the runqueue _entirely_, and not
2144 * preempted!
2145 *
Luis Henriques80dd99b2009-03-16 19:58:09 +00002146 * So if it was still runnable (but just not actively
Andi Kleen3a5c3592007-10-15 17:00:14 +02002147 * running right now), it's preempted, and we should
2148 * yield - it could be a while.
2149 */
2150 if (unlikely(on_rq)) {
2151 schedule_timeout_uninterruptible(1);
2152 continue;
2153 }
2154
2155 /*
2156 * Ahh, all good. It wasn't running, and it wasn't
2157 * runnable, which means that it will never become
2158 * running in the future either. We're all done!
2159 */
2160 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002161 }
Roland McGrath85ba2d82008-07-25 19:45:58 -07002162
2163 return ncsw;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002164}
2165
2166/***
2167 * kick_process - kick a running thread to enter/exit the kernel
2168 * @p: the to-be-kicked thread
2169 *
2170 * Cause a process which is running on another CPU to enter
2171 * kernel-mode, without any delay. (to get signals handled.)
2172 *
2173 * NOTE: this function doesnt have to take the runqueue lock,
2174 * because all it wants to ensure is that the remote task enters
2175 * the kernel. If the IPI races and the task has been migrated
2176 * to another CPU then no harm is done and the purpose has been
2177 * achieved as well.
2178 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07002179void kick_process(struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002180{
2181 int cpu;
2182
2183 preempt_disable();
2184 cpu = task_cpu(p);
2185 if ((cpu != smp_processor_id()) && task_curr(p))
2186 smp_send_reschedule(cpu);
2187 preempt_enable();
2188}
2189
2190/*
Peter Williams2dd73a42006-06-27 02:54:34 -07002191 * Return a low guess at the load of a migration-source cpu weighted
2192 * according to the scheduling class and "nice" value.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002193 *
2194 * We want to under-estimate the load of migration sources, to
2195 * balance conservatively.
2196 */
Alexey Dobriyana9957442007-10-15 17:00:13 +02002197static unsigned long source_load(int cpu, int type)
Con Kolivasb9104722005-11-08 21:38:55 -08002198{
Ingo Molnar70b97a72006-07-03 00:25:42 -07002199 struct rq *rq = cpu_rq(cpu);
Ingo Molnardd41f592007-07-09 18:51:59 +02002200 unsigned long total = weighted_cpuload(cpu);
Nick Piggina2000572006-02-10 01:51:02 -08002201
Peter Zijlstra93b75212008-06-27 13:41:33 +02002202 if (type == 0 || !sched_feat(LB_BIAS))
Ingo Molnardd41f592007-07-09 18:51:59 +02002203 return total;
Peter Williams2dd73a42006-06-27 02:54:34 -07002204
Ingo Molnardd41f592007-07-09 18:51:59 +02002205 return min(rq->cpu_load[type-1], total);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002206}
2207
2208/*
Peter Williams2dd73a42006-06-27 02:54:34 -07002209 * Return a high guess at the load of a migration-target cpu weighted
2210 * according to the scheduling class and "nice" value.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002211 */
Alexey Dobriyana9957442007-10-15 17:00:13 +02002212static unsigned long target_load(int cpu, int type)
Con Kolivasb9104722005-11-08 21:38:55 -08002213{
Ingo Molnar70b97a72006-07-03 00:25:42 -07002214 struct rq *rq = cpu_rq(cpu);
Ingo Molnardd41f592007-07-09 18:51:59 +02002215 unsigned long total = weighted_cpuload(cpu);
Nick Piggina2000572006-02-10 01:51:02 -08002216
Peter Zijlstra93b75212008-06-27 13:41:33 +02002217 if (type == 0 || !sched_feat(LB_BIAS))
Ingo Molnardd41f592007-07-09 18:51:59 +02002218 return total;
Peter Williams2dd73a42006-06-27 02:54:34 -07002219
Ingo Molnardd41f592007-07-09 18:51:59 +02002220 return max(rq->cpu_load[type-1], total);
Peter Williams2dd73a42006-06-27 02:54:34 -07002221}
2222
2223/*
Nick Piggin147cbb42005-06-25 14:57:19 -07002224 * find_idlest_group finds and returns the least busy CPU group within the
2225 * domain.
2226 */
2227static struct sched_group *
2228find_idlest_group(struct sched_domain *sd, struct task_struct *p, int this_cpu)
2229{
2230 struct sched_group *idlest = NULL, *this = NULL, *group = sd->groups;
2231 unsigned long min_load = ULONG_MAX, this_load = 0;
2232 int load_idx = sd->forkexec_idx;
2233 int imbalance = 100 + (sd->imbalance_pct-100)/2;
2234
2235 do {
2236 unsigned long load, avg_load;
2237 int local_group;
2238 int i;
2239
M.Baris Demirayda5a5522005-09-10 00:26:09 -07002240 /* Skip over this group if it has no CPUs allowed */
Rusty Russell758b2cd2008-11-25 02:35:04 +10302241 if (!cpumask_intersects(sched_group_cpus(group),
2242 &p->cpus_allowed))
Andi Kleen3a5c3592007-10-15 17:00:14 +02002243 continue;
M.Baris Demirayda5a5522005-09-10 00:26:09 -07002244
Rusty Russell758b2cd2008-11-25 02:35:04 +10302245 local_group = cpumask_test_cpu(this_cpu,
2246 sched_group_cpus(group));
Nick Piggin147cbb42005-06-25 14:57:19 -07002247
2248 /* Tally up the load of all CPUs in the group */
2249 avg_load = 0;
2250
Rusty Russell758b2cd2008-11-25 02:35:04 +10302251 for_each_cpu(i, sched_group_cpus(group)) {
Nick Piggin147cbb42005-06-25 14:57:19 -07002252 /* Bias balancing toward cpus of our domain */
2253 if (local_group)
2254 load = source_load(i, load_idx);
2255 else
2256 load = target_load(i, load_idx);
2257
2258 avg_load += load;
2259 }
2260
2261 /* Adjust by relative CPU power of the group */
Eric Dumazet5517d862007-05-08 00:32:57 -07002262 avg_load = sg_div_cpu_power(group,
2263 avg_load * SCHED_LOAD_SCALE);
Nick Piggin147cbb42005-06-25 14:57:19 -07002264
2265 if (local_group) {
2266 this_load = avg_load;
2267 this = group;
2268 } else if (avg_load < min_load) {
2269 min_load = avg_load;
2270 idlest = group;
2271 }
Andi Kleen3a5c3592007-10-15 17:00:14 +02002272 } while (group = group->next, group != sd->groups);
Nick Piggin147cbb42005-06-25 14:57:19 -07002273
2274 if (!idlest || 100*this_load < imbalance*min_load)
2275 return NULL;
2276 return idlest;
2277}
2278
2279/*
Satoru Takeuchi0feaece2006-10-03 01:14:10 -07002280 * find_idlest_cpu - find the idlest cpu among the cpus in group.
Nick Piggin147cbb42005-06-25 14:57:19 -07002281 */
Ingo Molnar95cdf3b2005-09-10 00:26:11 -07002282static int
Rusty Russell758b2cd2008-11-25 02:35:04 +10302283find_idlest_cpu(struct sched_group *group, struct task_struct *p, int this_cpu)
Nick Piggin147cbb42005-06-25 14:57:19 -07002284{
2285 unsigned long load, min_load = ULONG_MAX;
2286 int idlest = -1;
2287 int i;
2288
M.Baris Demirayda5a5522005-09-10 00:26:09 -07002289 /* Traverse only the allowed CPUs */
Rusty Russell758b2cd2008-11-25 02:35:04 +10302290 for_each_cpu_and(i, sched_group_cpus(group), &p->cpus_allowed) {
Peter Williams2dd73a42006-06-27 02:54:34 -07002291 load = weighted_cpuload(i);
Nick Piggin147cbb42005-06-25 14:57:19 -07002292
2293 if (load < min_load || (load == min_load && i == this_cpu)) {
2294 min_load = load;
2295 idlest = i;
2296 }
2297 }
2298
2299 return idlest;
2300}
2301
Nick Piggin476d1392005-06-25 14:57:29 -07002302/*
2303 * sched_balance_self: balance the current task (running on cpu) in domains
2304 * that have the 'flag' flag set. In practice, this is SD_BALANCE_FORK and
2305 * SD_BALANCE_EXEC.
2306 *
2307 * Balance, ie. select the least loaded group.
2308 *
2309 * Returns the target CPU number, or the same CPU if no balancing is needed.
2310 *
2311 * preempt must be disabled.
2312 */
2313static int sched_balance_self(int cpu, int flag)
2314{
2315 struct task_struct *t = current;
2316 struct sched_domain *tmp, *sd = NULL;
Nick Piggin147cbb42005-06-25 14:57:19 -07002317
Chen, Kenneth Wc96d1452006-06-27 02:54:28 -07002318 for_each_domain(cpu, tmp) {
Ingo Molnar9761eea2007-07-09 18:52:00 +02002319 /*
2320 * If power savings logic is enabled for a domain, stop there.
2321 */
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07002322 if (tmp->flags & SD_POWERSAVINGS_BALANCE)
2323 break;
Nick Piggin476d1392005-06-25 14:57:29 -07002324 if (tmp->flags & flag)
2325 sd = tmp;
Chen, Kenneth Wc96d1452006-06-27 02:54:28 -07002326 }
Nick Piggin476d1392005-06-25 14:57:29 -07002327
Peter Zijlstra039a1c42008-06-27 13:41:25 +02002328 if (sd)
2329 update_shares(sd);
2330
Nick Piggin476d1392005-06-25 14:57:29 -07002331 while (sd) {
Nick Piggin476d1392005-06-25 14:57:29 -07002332 struct sched_group *group;
Siddha, Suresh B1a848872006-10-03 01:14:08 -07002333 int new_cpu, weight;
2334
2335 if (!(sd->flags & flag)) {
2336 sd = sd->child;
2337 continue;
2338 }
Nick Piggin476d1392005-06-25 14:57:29 -07002339
Nick Piggin476d1392005-06-25 14:57:29 -07002340 group = find_idlest_group(sd, t, cpu);
Siddha, Suresh B1a848872006-10-03 01:14:08 -07002341 if (!group) {
2342 sd = sd->child;
2343 continue;
2344 }
Nick Piggin476d1392005-06-25 14:57:29 -07002345
Rusty Russell758b2cd2008-11-25 02:35:04 +10302346 new_cpu = find_idlest_cpu(group, t, cpu);
Siddha, Suresh B1a848872006-10-03 01:14:08 -07002347 if (new_cpu == -1 || new_cpu == cpu) {
2348 /* Now try balancing at a lower domain level of cpu */
2349 sd = sd->child;
2350 continue;
2351 }
Nick Piggin476d1392005-06-25 14:57:29 -07002352
Siddha, Suresh B1a848872006-10-03 01:14:08 -07002353 /* Now try balancing at a lower domain level of new_cpu */
Nick Piggin476d1392005-06-25 14:57:29 -07002354 cpu = new_cpu;
Rusty Russell758b2cd2008-11-25 02:35:04 +10302355 weight = cpumask_weight(sched_domain_span(sd));
Nick Piggin476d1392005-06-25 14:57:29 -07002356 sd = NULL;
Nick Piggin476d1392005-06-25 14:57:29 -07002357 for_each_domain(cpu, tmp) {
Rusty Russell758b2cd2008-11-25 02:35:04 +10302358 if (weight <= cpumask_weight(sched_domain_span(tmp)))
Nick Piggin476d1392005-06-25 14:57:29 -07002359 break;
2360 if (tmp->flags & flag)
2361 sd = tmp;
2362 }
2363 /* while loop will break here if sd == NULL */
2364 }
2365
2366 return cpu;
2367}
2368
2369#endif /* CONFIG_SMP */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002370
Linus Torvalds1da177e2005-04-16 15:20:36 -07002371/***
2372 * try_to_wake_up - wake up a thread
2373 * @p: the to-be-woken-up thread
2374 * @state: the mask of task states that can be woken
2375 * @sync: do a synchronous wakeup?
2376 *
2377 * Put it on the run-queue if it's not already there. The "current"
2378 * thread is always on the run-queue (except when the actual
2379 * re-schedule is in progress), and as such you're allowed to do
2380 * the simpler "current->state = TASK_RUNNING" to mark yourself
2381 * runnable without the overhead of this.
2382 *
2383 * returns failure only if the task is already active.
2384 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07002385static int try_to_wake_up(struct task_struct *p, unsigned int state, int sync)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002386{
Ingo Molnarcc367732007-10-15 17:00:18 +02002387 int cpu, orig_cpu, this_cpu, success = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002388 unsigned long flags;
2389 long old_state;
Ingo Molnar70b97a72006-07-03 00:25:42 -07002390 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002391
Ingo Molnarb85d0662008-03-16 20:03:22 +01002392 if (!sched_feat(SYNC_WAKEUPS))
2393 sync = 0;
2394
Peter Zijlstra2398f2c2008-06-27 13:41:35 +02002395#ifdef CONFIG_SMP
Peter Zijlstra57310a92009-03-09 13:56:21 +01002396 if (sched_feat(LB_WAKEUP_UPDATE) && !root_task_group_empty()) {
Peter Zijlstra2398f2c2008-06-27 13:41:35 +02002397 struct sched_domain *sd;
2398
2399 this_cpu = raw_smp_processor_id();
2400 cpu = task_cpu(p);
2401
2402 for_each_domain(this_cpu, sd) {
Rusty Russell758b2cd2008-11-25 02:35:04 +10302403 if (cpumask_test_cpu(cpu, sched_domain_span(sd))) {
Peter Zijlstra2398f2c2008-06-27 13:41:35 +02002404 update_shares(sd);
2405 break;
2406 }
2407 }
2408 }
2409#endif
2410
Linus Torvalds04e2f172008-02-23 18:05:03 -08002411 smp_wmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002412 rq = task_rq_lock(p, &flags);
Mike Galbraith03e89e42008-12-16 08:45:30 +01002413 update_rq_clock(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002414 old_state = p->state;
2415 if (!(old_state & state))
2416 goto out;
2417
Ingo Molnardd41f592007-07-09 18:51:59 +02002418 if (p->se.on_rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002419 goto out_running;
2420
2421 cpu = task_cpu(p);
Ingo Molnarcc367732007-10-15 17:00:18 +02002422 orig_cpu = cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002423 this_cpu = smp_processor_id();
2424
2425#ifdef CONFIG_SMP
2426 if (unlikely(task_running(rq, p)))
2427 goto out_activate;
2428
Dmitry Adamushko5d2f5a62008-01-25 21:08:21 +01002429 cpu = p->sched_class->select_task_rq(p, sync);
2430 if (cpu != orig_cpu) {
2431 set_task_cpu(p, cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002432 task_rq_unlock(rq, &flags);
2433 /* might preempt at this point */
2434 rq = task_rq_lock(p, &flags);
2435 old_state = p->state;
2436 if (!(old_state & state))
2437 goto out;
Ingo Molnardd41f592007-07-09 18:51:59 +02002438 if (p->se.on_rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002439 goto out_running;
2440
2441 this_cpu = smp_processor_id();
2442 cpu = task_cpu(p);
2443 }
2444
Gregory Haskinse7693a32008-01-25 21:08:09 +01002445#ifdef CONFIG_SCHEDSTATS
2446 schedstat_inc(rq, ttwu_count);
2447 if (cpu == this_cpu)
2448 schedstat_inc(rq, ttwu_local);
2449 else {
2450 struct sched_domain *sd;
2451 for_each_domain(this_cpu, sd) {
Rusty Russell758b2cd2008-11-25 02:35:04 +10302452 if (cpumask_test_cpu(cpu, sched_domain_span(sd))) {
Gregory Haskinse7693a32008-01-25 21:08:09 +01002453 schedstat_inc(sd, ttwu_wake_remote);
2454 break;
2455 }
2456 }
2457 }
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +02002458#endif /* CONFIG_SCHEDSTATS */
Gregory Haskinse7693a32008-01-25 21:08:09 +01002459
Linus Torvalds1da177e2005-04-16 15:20:36 -07002460out_activate:
2461#endif /* CONFIG_SMP */
Ingo Molnarcc367732007-10-15 17:00:18 +02002462 schedstat_inc(p, se.nr_wakeups);
2463 if (sync)
2464 schedstat_inc(p, se.nr_wakeups_sync);
2465 if (orig_cpu != cpu)
2466 schedstat_inc(p, se.nr_wakeups_migrate);
2467 if (cpu == this_cpu)
2468 schedstat_inc(p, se.nr_wakeups_local);
2469 else
2470 schedstat_inc(p, se.nr_wakeups_remote);
Ingo Molnardd41f592007-07-09 18:51:59 +02002471 activate_task(rq, p, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002472 success = 1;
2473
Peter Zijlstra831451a2009-01-14 12:39:18 +01002474 /*
2475 * Only attribute actual wakeups done by this task.
2476 */
2477 if (!in_interrupt()) {
2478 struct sched_entity *se = &current->se;
2479 u64 sample = se->sum_exec_runtime;
2480
2481 if (se->last_wakeup)
2482 sample -= se->last_wakeup;
2483 else
2484 sample -= se->start_runtime;
2485 update_avg(&se->avg_wakeup, sample);
2486
2487 se->last_wakeup = se->sum_exec_runtime;
2488 }
2489
Linus Torvalds1da177e2005-04-16 15:20:36 -07002490out_running:
Peter Zijlstra468a15b2008-12-16 08:07:03 +01002491 trace_sched_wakeup(rq, p, success);
Peter Zijlstra15afe092008-09-20 23:38:02 +02002492 check_preempt_curr(rq, p, sync);
Ingo Molnar4ae7d5c2008-03-19 01:42:00 +01002493
Linus Torvalds1da177e2005-04-16 15:20:36 -07002494 p->state = TASK_RUNNING;
Steven Rostedt9a897c52008-01-25 21:08:22 +01002495#ifdef CONFIG_SMP
2496 if (p->sched_class->task_wake_up)
2497 p->sched_class->task_wake_up(rq, p);
2498#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002499out:
2500 task_rq_unlock(rq, &flags);
2501
2502 return success;
2503}
2504
David Howells50fa6102009-04-28 15:01:38 +01002505/**
2506 * wake_up_process - Wake up a specific process
2507 * @p: The process to be woken up.
2508 *
2509 * Attempt to wake up the nominated process and move it to the set of runnable
2510 * processes. Returns 1 if the process was woken up, 0 if it was already
2511 * running.
2512 *
2513 * It may be assumed that this function implies a write memory barrier before
2514 * changing the task state if and only if any tasks are woken up.
2515 */
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -08002516int wake_up_process(struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002517{
Matthew Wilcoxd9514f62007-12-06 11:07:07 -05002518 return try_to_wake_up(p, TASK_ALL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002519}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002520EXPORT_SYMBOL(wake_up_process);
2521
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -08002522int wake_up_state(struct task_struct *p, unsigned int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002523{
2524 return try_to_wake_up(p, state, 0);
2525}
2526
Linus Torvalds1da177e2005-04-16 15:20:36 -07002527/*
2528 * Perform scheduler related setup for a newly forked process p.
2529 * p is forked by current.
Ingo Molnardd41f592007-07-09 18:51:59 +02002530 *
2531 * __sched_fork() is basic setup used by init_idle() too:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002532 */
Ingo Molnardd41f592007-07-09 18:51:59 +02002533static void __sched_fork(struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002534{
Ingo Molnardd41f592007-07-09 18:51:59 +02002535 p->se.exec_start = 0;
2536 p->se.sum_exec_runtime = 0;
Ingo Molnarf6cf8912007-08-28 12:53:24 +02002537 p->se.prev_sum_exec_runtime = 0;
Ingo Molnar4ae7d5c2008-03-19 01:42:00 +01002538 p->se.last_wakeup = 0;
2539 p->se.avg_overlap = 0;
Peter Zijlstra831451a2009-01-14 12:39:18 +01002540 p->se.start_runtime = 0;
2541 p->se.avg_wakeup = sysctl_sched_wakeup_granularity;
Ingo Molnar6cfb0d52007-08-02 17:41:40 +02002542
2543#ifdef CONFIG_SCHEDSTATS
2544 p->se.wait_start = 0;
Ingo Molnardd41f592007-07-09 18:51:59 +02002545 p->se.sum_sleep_runtime = 0;
2546 p->se.sleep_start = 0;
Ingo Molnardd41f592007-07-09 18:51:59 +02002547 p->se.block_start = 0;
2548 p->se.sleep_max = 0;
2549 p->se.block_max = 0;
2550 p->se.exec_max = 0;
Ingo Molnareba1ed42007-10-15 17:00:02 +02002551 p->se.slice_max = 0;
Ingo Molnardd41f592007-07-09 18:51:59 +02002552 p->se.wait_max = 0;
Ingo Molnar6cfb0d52007-08-02 17:41:40 +02002553#endif
Nick Piggin476d1392005-06-25 14:57:29 -07002554
Peter Zijlstrafa717062008-01-25 21:08:27 +01002555 INIT_LIST_HEAD(&p->rt.run_list);
Ingo Molnardd41f592007-07-09 18:51:59 +02002556 p->se.on_rq = 0;
Peter Zijlstra4a55bd52008-04-19 19:45:00 +02002557 INIT_LIST_HEAD(&p->se.group_node);
Nick Piggin476d1392005-06-25 14:57:29 -07002558
Avi Kivitye107be32007-07-26 13:40:43 +02002559#ifdef CONFIG_PREEMPT_NOTIFIERS
2560 INIT_HLIST_HEAD(&p->preempt_notifiers);
2561#endif
2562
Linus Torvalds1da177e2005-04-16 15:20:36 -07002563 /*
2564 * We mark the process as running here, but have not actually
2565 * inserted it onto the runqueue yet. This guarantees that
2566 * nobody will actually run it, and a signal or other external
2567 * event cannot wake it up and insert it on the runqueue either.
2568 */
2569 p->state = TASK_RUNNING;
Ingo Molnardd41f592007-07-09 18:51:59 +02002570}
2571
2572/*
2573 * fork()/clone()-time setup:
2574 */
2575void sched_fork(struct task_struct *p, int clone_flags)
2576{
2577 int cpu = get_cpu();
2578
2579 __sched_fork(p);
2580
2581#ifdef CONFIG_SMP
2582 cpu = sched_balance_self(cpu, SD_BALANCE_FORK);
2583#endif
Ingo Molnar02e4bac2007-10-15 17:00:11 +02002584 set_task_cpu(p, cpu);
Ingo Molnarb29739f2006-06-27 02:54:51 -07002585
2586 /*
2587 * Make sure we do not leak PI boosting priority to the child:
2588 */
2589 p->prio = current->normal_prio;
Hiroshi Shimamoto2ddbf952007-10-15 17:00:11 +02002590 if (!rt_prio(p->prio))
2591 p->sched_class = &fair_sched_class;
Ingo Molnarb29739f2006-06-27 02:54:51 -07002592
Chandra Seetharaman52f17b62006-07-14 00:24:38 -07002593#if defined(CONFIG_SCHEDSTATS) || defined(CONFIG_TASK_DELAY_ACCT)
Ingo Molnardd41f592007-07-09 18:51:59 +02002594 if (likely(sched_info_on()))
Chandra Seetharaman52f17b62006-07-14 00:24:38 -07002595 memset(&p->sched_info, 0, sizeof(p->sched_info));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002596#endif
Chen, Kenneth Wd6077cb2006-02-14 13:53:10 -08002597#if defined(CONFIG_SMP) && defined(__ARCH_WANT_UNLOCKED_CTXSW)
Nick Piggin4866cde2005-06-25 14:57:23 -07002598 p->oncpu = 0;
2599#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002600#ifdef CONFIG_PREEMPT
Nick Piggin4866cde2005-06-25 14:57:23 -07002601 /* Want to start with kernel preemption disabled. */
Al Viroa1261f52005-11-13 16:06:55 -08002602 task_thread_info(p)->preempt_count = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002603#endif
Gregory Haskins917b6272008-12-29 09:39:53 -05002604 plist_node_init(&p->pushable_tasks, MAX_PRIO);
2605
Nick Piggin476d1392005-06-25 14:57:29 -07002606 put_cpu();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002607}
2608
2609/*
2610 * wake_up_new_task - wake up a newly created task for the first time.
2611 *
2612 * This function will do some initial scheduler statistics housekeeping
2613 * that must be done for every newly created context, then puts the task
2614 * on the runqueue and wakes it.
2615 */
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -08002616void wake_up_new_task(struct task_struct *p, unsigned long clone_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002617{
2618 unsigned long flags;
Ingo Molnardd41f592007-07-09 18:51:59 +02002619 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002620
2621 rq = task_rq_lock(p, &flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002622 BUG_ON(p->state != TASK_RUNNING);
Ingo Molnara8e504d2007-08-09 11:16:47 +02002623 update_rq_clock(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002624
2625 p->prio = effective_prio(p);
2626
Srivatsa Vaddagirib9dca1e2007-10-17 16:55:11 +02002627 if (!p->sched_class->task_new || !current->se.on_rq) {
Ingo Molnardd41f592007-07-09 18:51:59 +02002628 activate_task(rq, p, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002629 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002630 /*
Ingo Molnardd41f592007-07-09 18:51:59 +02002631 * Let the scheduling class do new task startup
2632 * management (if any):
Linus Torvalds1da177e2005-04-16 15:20:36 -07002633 */
Ingo Molnaree0827d2007-08-09 11:16:49 +02002634 p->sched_class->task_new(rq, p);
Peter Zijlstrac09595f2008-06-27 13:41:14 +02002635 inc_nr_running(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002636 }
Ingo Molnarc71dd422008-12-19 01:09:51 +01002637 trace_sched_wakeup_new(rq, p, 1);
Peter Zijlstra15afe092008-09-20 23:38:02 +02002638 check_preempt_curr(rq, p, 0);
Steven Rostedt9a897c52008-01-25 21:08:22 +01002639#ifdef CONFIG_SMP
2640 if (p->sched_class->task_wake_up)
2641 p->sched_class->task_wake_up(rq, p);
2642#endif
Ingo Molnardd41f592007-07-09 18:51:59 +02002643 task_rq_unlock(rq, &flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002644}
2645
Avi Kivitye107be32007-07-26 13:40:43 +02002646#ifdef CONFIG_PREEMPT_NOTIFIERS
2647
2648/**
Luis Henriques80dd99b2009-03-16 19:58:09 +00002649 * preempt_notifier_register - tell me when current is being preempted & rescheduled
Randy Dunlap421cee22007-07-31 00:37:50 -07002650 * @notifier: notifier struct to register
Avi Kivitye107be32007-07-26 13:40:43 +02002651 */
2652void preempt_notifier_register(struct preempt_notifier *notifier)
2653{
2654 hlist_add_head(&notifier->link, &current->preempt_notifiers);
2655}
2656EXPORT_SYMBOL_GPL(preempt_notifier_register);
2657
2658/**
2659 * preempt_notifier_unregister - no longer interested in preemption notifications
Randy Dunlap421cee22007-07-31 00:37:50 -07002660 * @notifier: notifier struct to unregister
Avi Kivitye107be32007-07-26 13:40:43 +02002661 *
2662 * This is safe to call from within a preemption notifier.
2663 */
2664void preempt_notifier_unregister(struct preempt_notifier *notifier)
2665{
2666 hlist_del(&notifier->link);
2667}
2668EXPORT_SYMBOL_GPL(preempt_notifier_unregister);
2669
2670static void fire_sched_in_preempt_notifiers(struct task_struct *curr)
2671{
2672 struct preempt_notifier *notifier;
2673 struct hlist_node *node;
2674
2675 hlist_for_each_entry(notifier, node, &curr->preempt_notifiers, link)
2676 notifier->ops->sched_in(notifier, raw_smp_processor_id());
2677}
2678
2679static void
2680fire_sched_out_preempt_notifiers(struct task_struct *curr,
2681 struct task_struct *next)
2682{
2683 struct preempt_notifier *notifier;
2684 struct hlist_node *node;
2685
2686 hlist_for_each_entry(notifier, node, &curr->preempt_notifiers, link)
2687 notifier->ops->sched_out(notifier, next);
2688}
2689
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +02002690#else /* !CONFIG_PREEMPT_NOTIFIERS */
Avi Kivitye107be32007-07-26 13:40:43 +02002691
2692static void fire_sched_in_preempt_notifiers(struct task_struct *curr)
2693{
2694}
2695
2696static void
2697fire_sched_out_preempt_notifiers(struct task_struct *curr,
2698 struct task_struct *next)
2699{
2700}
2701
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +02002702#endif /* CONFIG_PREEMPT_NOTIFIERS */
Avi Kivitye107be32007-07-26 13:40:43 +02002703
Linus Torvalds1da177e2005-04-16 15:20:36 -07002704/**
Nick Piggin4866cde2005-06-25 14:57:23 -07002705 * prepare_task_switch - prepare to switch tasks
2706 * @rq: the runqueue preparing to switch
Randy Dunlap421cee22007-07-31 00:37:50 -07002707 * @prev: the current task that is being switched out
Nick Piggin4866cde2005-06-25 14:57:23 -07002708 * @next: the task we are going to switch to.
2709 *
2710 * This is called with the rq lock held and interrupts off. It must
2711 * be paired with a subsequent finish_task_switch after the context
2712 * switch.
2713 *
2714 * prepare_task_switch sets up locking and calls architecture specific
2715 * hooks.
2716 */
Avi Kivitye107be32007-07-26 13:40:43 +02002717static inline void
2718prepare_task_switch(struct rq *rq, struct task_struct *prev,
2719 struct task_struct *next)
Nick Piggin4866cde2005-06-25 14:57:23 -07002720{
Avi Kivitye107be32007-07-26 13:40:43 +02002721 fire_sched_out_preempt_notifiers(prev, next);
Nick Piggin4866cde2005-06-25 14:57:23 -07002722 prepare_lock_switch(rq, next);
2723 prepare_arch_switch(next);
2724}
2725
2726/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07002727 * finish_task_switch - clean up after a task-switch
Jeff Garzik344baba2005-09-07 01:15:17 -04002728 * @rq: runqueue associated with task-switch
Linus Torvalds1da177e2005-04-16 15:20:36 -07002729 * @prev: the thread we just switched away from.
2730 *
Nick Piggin4866cde2005-06-25 14:57:23 -07002731 * finish_task_switch must be called after the context switch, paired
2732 * with a prepare_task_switch call before the context switch.
2733 * finish_task_switch will reconcile locking set up by prepare_task_switch,
2734 * and do any other architecture-specific cleanup actions.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002735 *
2736 * Note that we may have delayed dropping an mm in context_switch(). If
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01002737 * so, we finish that here outside of the runqueue lock. (Doing it
Linus Torvalds1da177e2005-04-16 15:20:36 -07002738 * with the lock held can cause deadlocks; see schedule() for
2739 * details.)
2740 */
Alexey Dobriyana9957442007-10-15 17:00:13 +02002741static void finish_task_switch(struct rq *rq, struct task_struct *prev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002742 __releases(rq->lock)
2743{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002744 struct mm_struct *mm = rq->prev_mm;
Oleg Nesterov55a101f2006-09-29 02:01:10 -07002745 long prev_state;
Gregory Haskins967fc042008-12-29 09:39:52 -05002746#ifdef CONFIG_SMP
2747 int post_schedule = 0;
2748
2749 if (current->sched_class->needs_post_schedule)
2750 post_schedule = current->sched_class->needs_post_schedule(rq);
2751#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002752
2753 rq->prev_mm = NULL;
2754
2755 /*
2756 * A task struct has one reference for the use as "current".
Oleg Nesterovc394cc92006-09-29 02:01:11 -07002757 * If a task dies, then it sets TASK_DEAD in tsk->state and calls
Oleg Nesterov55a101f2006-09-29 02:01:10 -07002758 * schedule one last time. The schedule call will never return, and
2759 * the scheduled task must drop that reference.
Oleg Nesterovc394cc92006-09-29 02:01:11 -07002760 * The test for TASK_DEAD must occur while the runqueue locks are
Linus Torvalds1da177e2005-04-16 15:20:36 -07002761 * still held, otherwise prev could be scheduled on another cpu, die
2762 * there before we look at prev->state, and then the reference would
2763 * be dropped twice.
2764 * Manfred Spraul <manfred@colorfullife.com>
2765 */
Oleg Nesterov55a101f2006-09-29 02:01:10 -07002766 prev_state = prev->state;
Nick Piggin4866cde2005-06-25 14:57:23 -07002767 finish_arch_switch(prev);
2768 finish_lock_switch(rq, prev);
Steven Rostedt9a897c52008-01-25 21:08:22 +01002769#ifdef CONFIG_SMP
Gregory Haskins967fc042008-12-29 09:39:52 -05002770 if (post_schedule)
Steven Rostedt9a897c52008-01-25 21:08:22 +01002771 current->sched_class->post_schedule(rq);
2772#endif
Steven Rostedte8fa1362008-01-25 21:08:05 +01002773
Avi Kivitye107be32007-07-26 13:40:43 +02002774 fire_sched_in_preempt_notifiers(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002775 if (mm)
2776 mmdrop(mm);
Oleg Nesterovc394cc92006-09-29 02:01:11 -07002777 if (unlikely(prev_state == TASK_DEAD)) {
bibo maoc6fd91f2006-03-26 01:38:20 -08002778 /*
2779 * Remove function-return probe instances associated with this
2780 * task and put them back on the free list.
Ingo Molnar9761eea2007-07-09 18:52:00 +02002781 */
bibo maoc6fd91f2006-03-26 01:38:20 -08002782 kprobe_flush_task(prev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002783 put_task_struct(prev);
bibo maoc6fd91f2006-03-26 01:38:20 -08002784 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002785}
2786
2787/**
2788 * schedule_tail - first thing a freshly forked thread must call.
2789 * @prev: the thread we just switched away from.
2790 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07002791asmlinkage void schedule_tail(struct task_struct *prev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002792 __releases(rq->lock)
2793{
Ingo Molnar70b97a72006-07-03 00:25:42 -07002794 struct rq *rq = this_rq();
2795
Nick Piggin4866cde2005-06-25 14:57:23 -07002796 finish_task_switch(rq, prev);
2797#ifdef __ARCH_WANT_UNLOCKED_CTXSW
2798 /* In this case, finish_task_switch does not reenable preemption */
2799 preempt_enable();
2800#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002801 if (current->set_child_tid)
Pavel Emelyanovb4888932007-10-18 23:40:14 -07002802 put_user(task_pid_vnr(current), current->set_child_tid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002803}
2804
2805/*
2806 * context_switch - switch to the new MM and the new
2807 * thread's register state.
2808 */
Ingo Molnardd41f592007-07-09 18:51:59 +02002809static inline void
Ingo Molnar70b97a72006-07-03 00:25:42 -07002810context_switch(struct rq *rq, struct task_struct *prev,
Ingo Molnar36c8b582006-07-03 00:25:41 -07002811 struct task_struct *next)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002812{
Ingo Molnardd41f592007-07-09 18:51:59 +02002813 struct mm_struct *mm, *oldmm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002814
Avi Kivitye107be32007-07-26 13:40:43 +02002815 prepare_task_switch(rq, prev, next);
Mathieu Desnoyers0a16b602008-07-18 12:16:17 -04002816 trace_sched_switch(rq, prev, next);
Ingo Molnardd41f592007-07-09 18:51:59 +02002817 mm = next->mm;
2818 oldmm = prev->active_mm;
Zachary Amsden9226d122007-02-13 13:26:21 +01002819 /*
2820 * For paravirt, this is coupled with an exit in switch_to to
2821 * combine the page table reload and the switch backend into
2822 * one hypercall.
2823 */
Jeremy Fitzhardinge224101e2009-02-18 11:18:57 -08002824 arch_start_context_switch(prev);
Zachary Amsden9226d122007-02-13 13:26:21 +01002825
Ingo Molnardd41f592007-07-09 18:51:59 +02002826 if (unlikely(!mm)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002827 next->active_mm = oldmm;
2828 atomic_inc(&oldmm->mm_count);
2829 enter_lazy_tlb(oldmm, next);
2830 } else
2831 switch_mm(oldmm, mm, next);
2832
Ingo Molnardd41f592007-07-09 18:51:59 +02002833 if (unlikely(!prev->mm)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002834 prev->active_mm = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002835 rq->prev_mm = oldmm;
2836 }
Ingo Molnar3a5f5e42006-07-14 00:24:27 -07002837 /*
2838 * Since the runqueue lock will be released by the next
2839 * task (which is an invalid locking op but in the case
2840 * of the scheduler it's an obvious special-case), so we
2841 * do an early lockdep release here:
2842 */
2843#ifndef __ARCH_WANT_UNLOCKED_CTXSW
Ingo Molnar8a25d5d2006-07-03 00:24:54 -07002844 spin_release(&rq->lock.dep_map, 1, _THIS_IP_);
Ingo Molnar3a5f5e42006-07-14 00:24:27 -07002845#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002846
2847 /* Here we just switch the register state and the stack. */
2848 switch_to(prev, next, prev);
2849
Ingo Molnardd41f592007-07-09 18:51:59 +02002850 barrier();
2851 /*
2852 * this_rq must be evaluated again because prev may have moved
2853 * CPUs since it called schedule(), thus the 'rq' on its stack
2854 * frame will be invalid.
2855 */
2856 finish_task_switch(this_rq(), prev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002857}
2858
2859/*
2860 * nr_running, nr_uninterruptible and nr_context_switches:
2861 *
2862 * externally visible scheduler statistics: current number of runnable
2863 * threads, current number of uninterruptible-sleeping threads, total
2864 * number of context switches performed since bootup.
2865 */
2866unsigned long nr_running(void)
2867{
2868 unsigned long i, sum = 0;
2869
2870 for_each_online_cpu(i)
2871 sum += cpu_rq(i)->nr_running;
2872
2873 return sum;
2874}
2875
2876unsigned long nr_uninterruptible(void)
2877{
2878 unsigned long i, sum = 0;
2879
KAMEZAWA Hiroyuki0a945022006-03-28 01:56:37 -08002880 for_each_possible_cpu(i)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002881 sum += cpu_rq(i)->nr_uninterruptible;
2882
2883 /*
2884 * Since we read the counters lockless, it might be slightly
2885 * inaccurate. Do not allow it to go below zero though:
2886 */
2887 if (unlikely((long)sum < 0))
2888 sum = 0;
2889
2890 return sum;
2891}
2892
2893unsigned long long nr_context_switches(void)
2894{
Steven Rostedtcc94abf2006-06-27 02:54:31 -07002895 int i;
2896 unsigned long long sum = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002897
KAMEZAWA Hiroyuki0a945022006-03-28 01:56:37 -08002898 for_each_possible_cpu(i)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002899 sum += cpu_rq(i)->nr_switches;
2900
2901 return sum;
2902}
2903
2904unsigned long nr_iowait(void)
2905{
2906 unsigned long i, sum = 0;
2907
KAMEZAWA Hiroyuki0a945022006-03-28 01:56:37 -08002908 for_each_possible_cpu(i)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002909 sum += atomic_read(&cpu_rq(i)->nr_iowait);
2910
2911 return sum;
2912}
2913
Thomas Gleixnerdce48a82009-04-11 10:43:41 +02002914/* Variables and functions for calc_load */
2915static atomic_long_t calc_load_tasks;
2916static unsigned long calc_load_update;
2917unsigned long avenrun[3];
2918EXPORT_SYMBOL(avenrun);
2919
Thomas Gleixner2d024942009-05-02 20:08:52 +02002920/**
2921 * get_avenrun - get the load average array
2922 * @loads: pointer to dest load array
2923 * @offset: offset to add
2924 * @shift: shift count to shift the result left
2925 *
2926 * These values are estimates at best, so no need for locking.
2927 */
2928void get_avenrun(unsigned long *loads, unsigned long offset, int shift)
2929{
2930 loads[0] = (avenrun[0] + offset) << shift;
2931 loads[1] = (avenrun[1] + offset) << shift;
2932 loads[2] = (avenrun[2] + offset) << shift;
2933}
2934
Thomas Gleixnerdce48a82009-04-11 10:43:41 +02002935static unsigned long
2936calc_load(unsigned long load, unsigned long exp, unsigned long active)
Jack Steinerdb1b1fe2006-03-31 02:31:21 -08002937{
Thomas Gleixnerdce48a82009-04-11 10:43:41 +02002938 load *= exp;
2939 load += active * (FIXED_1 - exp);
2940 return load >> FSHIFT;
2941}
Jack Steinerdb1b1fe2006-03-31 02:31:21 -08002942
Thomas Gleixnerdce48a82009-04-11 10:43:41 +02002943/*
2944 * calc_load - update the avenrun load estimates 10 ticks after the
2945 * CPUs have updated calc_load_tasks.
2946 */
2947void calc_global_load(void)
2948{
2949 unsigned long upd = calc_load_update + 10;
2950 long active;
2951
2952 if (time_before(jiffies, upd))
2953 return;
2954
2955 active = atomic_long_read(&calc_load_tasks);
2956 active = active > 0 ? active * FIXED_1 : 0;
2957
2958 avenrun[0] = calc_load(avenrun[0], EXP_1, active);
2959 avenrun[1] = calc_load(avenrun[1], EXP_5, active);
2960 avenrun[2] = calc_load(avenrun[2], EXP_15, active);
2961
2962 calc_load_update += LOAD_FREQ;
2963}
2964
2965/*
2966 * Either called from update_cpu_load() or from a cpu going idle
2967 */
2968static void calc_load_account_active(struct rq *this_rq)
2969{
2970 long nr_active, delta;
2971
2972 nr_active = this_rq->nr_running;
2973 nr_active += (long) this_rq->nr_uninterruptible;
2974
2975 if (nr_active != this_rq->calc_load_active) {
2976 delta = nr_active - this_rq->calc_load_active;
2977 this_rq->calc_load_active = nr_active;
2978 atomic_long_add(delta, &calc_load_tasks);
Jack Steinerdb1b1fe2006-03-31 02:31:21 -08002979 }
Jack Steinerdb1b1fe2006-03-31 02:31:21 -08002980}
2981
Linus Torvalds1da177e2005-04-16 15:20:36 -07002982/*
Ingo Molnardd41f592007-07-09 18:51:59 +02002983 * Update rq->cpu_load[] statistics. This function is usually called every
2984 * scheduler tick (TICK_NSEC).
Ingo Molnar48f24c42006-07-03 00:25:40 -07002985 */
Ingo Molnardd41f592007-07-09 18:51:59 +02002986static void update_cpu_load(struct rq *this_rq)
Ingo Molnar48f24c42006-07-03 00:25:40 -07002987{
Dmitry Adamushko495eca42007-10-15 17:00:06 +02002988 unsigned long this_load = this_rq->load.weight;
Ingo Molnardd41f592007-07-09 18:51:59 +02002989 int i, scale;
2990
2991 this_rq->nr_load_updates++;
Ingo Molnardd41f592007-07-09 18:51:59 +02002992
2993 /* Update our load: */
2994 for (i = 0, scale = 1; i < CPU_LOAD_IDX_MAX; i++, scale += scale) {
2995 unsigned long old_load, new_load;
2996
2997 /* scale is effectively 1 << i now, and >> i divides by scale */
2998
2999 old_load = this_rq->cpu_load[i];
3000 new_load = this_load;
Ingo Molnara25707f2007-10-15 17:00:03 +02003001 /*
3002 * Round up the averaging division if load is increasing. This
3003 * prevents us from getting stuck on 9 if the load is 10, for
3004 * example.
3005 */
3006 if (new_load > old_load)
3007 new_load += scale-1;
Ingo Molnardd41f592007-07-09 18:51:59 +02003008 this_rq->cpu_load[i] = (old_load*(scale-1) + new_load) >> i;
3009 }
Thomas Gleixnerdce48a82009-04-11 10:43:41 +02003010
3011 if (time_after_eq(jiffies, this_rq->calc_load_update)) {
3012 this_rq->calc_load_update += LOAD_FREQ;
3013 calc_load_account_active(this_rq);
3014 }
Ingo Molnar48f24c42006-07-03 00:25:40 -07003015}
3016
Ingo Molnardd41f592007-07-09 18:51:59 +02003017#ifdef CONFIG_SMP
3018
Ingo Molnar48f24c42006-07-03 00:25:40 -07003019/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003020 * double_rq_lock - safely lock two runqueues
3021 *
3022 * Note this does not disable interrupts like task_rq_lock,
3023 * you need to do so manually before calling.
3024 */
Ingo Molnar70b97a72006-07-03 00:25:42 -07003025static void double_rq_lock(struct rq *rq1, struct rq *rq2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003026 __acquires(rq1->lock)
3027 __acquires(rq2->lock)
3028{
Kirill Korotaev054b9102006-12-10 02:20:11 -08003029 BUG_ON(!irqs_disabled());
Linus Torvalds1da177e2005-04-16 15:20:36 -07003030 if (rq1 == rq2) {
3031 spin_lock(&rq1->lock);
3032 __acquire(rq2->lock); /* Fake it out ;) */
3033 } else {
Chen, Kenneth Wc96d1452006-06-27 02:54:28 -07003034 if (rq1 < rq2) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003035 spin_lock(&rq1->lock);
Peter Zijlstra5e710e32008-07-30 13:26:57 +02003036 spin_lock_nested(&rq2->lock, SINGLE_DEPTH_NESTING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003037 } else {
3038 spin_lock(&rq2->lock);
Peter Zijlstra5e710e32008-07-30 13:26:57 +02003039 spin_lock_nested(&rq1->lock, SINGLE_DEPTH_NESTING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003040 }
3041 }
Ingo Molnar6e82a3b2007-08-09 11:16:51 +02003042 update_rq_clock(rq1);
3043 update_rq_clock(rq2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003044}
3045
3046/*
3047 * double_rq_unlock - safely unlock two runqueues
3048 *
3049 * Note this does not restore interrupts like task_rq_unlock,
3050 * you need to do so manually after calling.
3051 */
Ingo Molnar70b97a72006-07-03 00:25:42 -07003052static void double_rq_unlock(struct rq *rq1, struct rq *rq2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003053 __releases(rq1->lock)
3054 __releases(rq2->lock)
3055{
3056 spin_unlock(&rq1->lock);
3057 if (rq1 != rq2)
3058 spin_unlock(&rq2->lock);
3059 else
3060 __release(rq2->lock);
3061}
3062
3063/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003064 * If dest_cpu is allowed for this process, migrate the task to it.
3065 * This is accomplished by forcing the cpu_allowed mask to only
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01003066 * allow dest_cpu, which will force the cpu onto dest_cpu. Then
Linus Torvalds1da177e2005-04-16 15:20:36 -07003067 * the cpu_allowed mask is restored.
3068 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07003069static void sched_migrate_task(struct task_struct *p, int dest_cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003070{
Ingo Molnar70b97a72006-07-03 00:25:42 -07003071 struct migration_req req;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003072 unsigned long flags;
Ingo Molnar70b97a72006-07-03 00:25:42 -07003073 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003074
3075 rq = task_rq_lock(p, &flags);
Rusty Russell96f874e2008-11-25 02:35:14 +10303076 if (!cpumask_test_cpu(dest_cpu, &p->cpus_allowed)
Max Krasnyanskye761b772008-07-15 04:43:49 -07003077 || unlikely(!cpu_active(dest_cpu)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003078 goto out;
3079
3080 /* force the process onto the specified CPU */
3081 if (migrate_task(p, dest_cpu, &req)) {
3082 /* Need to wait for migration thread (might exit: take ref). */
3083 struct task_struct *mt = rq->migration_thread;
Ingo Molnar36c8b582006-07-03 00:25:41 -07003084
Linus Torvalds1da177e2005-04-16 15:20:36 -07003085 get_task_struct(mt);
3086 task_rq_unlock(rq, &flags);
3087 wake_up_process(mt);
3088 put_task_struct(mt);
3089 wait_for_completion(&req.done);
Ingo Molnar36c8b582006-07-03 00:25:41 -07003090
Linus Torvalds1da177e2005-04-16 15:20:36 -07003091 return;
3092 }
3093out:
3094 task_rq_unlock(rq, &flags);
3095}
3096
3097/*
Nick Piggin476d1392005-06-25 14:57:29 -07003098 * sched_exec - execve() is a valuable balancing opportunity, because at
3099 * this point the task has the smallest effective memory and cache footprint.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003100 */
3101void sched_exec(void)
3102{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003103 int new_cpu, this_cpu = get_cpu();
Nick Piggin476d1392005-06-25 14:57:29 -07003104 new_cpu = sched_balance_self(this_cpu, SD_BALANCE_EXEC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003105 put_cpu();
Nick Piggin476d1392005-06-25 14:57:29 -07003106 if (new_cpu != this_cpu)
3107 sched_migrate_task(current, new_cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003108}
3109
3110/*
3111 * pull_task - move a task from a remote runqueue to the local runqueue.
3112 * Both runqueues must be locked.
3113 */
Ingo Molnardd41f592007-07-09 18:51:59 +02003114static void pull_task(struct rq *src_rq, struct task_struct *p,
3115 struct rq *this_rq, int this_cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003116{
Ingo Molnar2e1cb742007-08-09 11:16:49 +02003117 deactivate_task(src_rq, p, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003118 set_task_cpu(p, this_cpu);
Ingo Molnardd41f592007-07-09 18:51:59 +02003119 activate_task(this_rq, p, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003120 /*
3121 * Note that idle threads have a prio of MAX_PRIO, for this test
3122 * to be always true for them.
3123 */
Peter Zijlstra15afe092008-09-20 23:38:02 +02003124 check_preempt_curr(this_rq, p, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003125}
3126
3127/*
3128 * can_migrate_task - may task p from runqueue rq be migrated to this_cpu?
3129 */
Arjan van de Ven858119e2006-01-14 13:20:43 -08003130static
Ingo Molnar70b97a72006-07-03 00:25:42 -07003131int can_migrate_task(struct task_struct *p, struct rq *rq, int this_cpu,
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003132 struct sched_domain *sd, enum cpu_idle_type idle,
Ingo Molnar95cdf3b2005-09-10 00:26:11 -07003133 int *all_pinned)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003134{
Luis Henriques708dc512009-03-16 19:59:02 +00003135 int tsk_cache_hot = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003136 /*
3137 * We do not migrate tasks that are:
3138 * 1) running (obviously), or
3139 * 2) cannot be migrated to this CPU due to cpus_allowed, or
3140 * 3) are cache-hot on their current CPU.
3141 */
Rusty Russell96f874e2008-11-25 02:35:14 +10303142 if (!cpumask_test_cpu(this_cpu, &p->cpus_allowed)) {
Ingo Molnarcc367732007-10-15 17:00:18 +02003143 schedstat_inc(p, se.nr_failed_migrations_affine);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003144 return 0;
Ingo Molnarcc367732007-10-15 17:00:18 +02003145 }
Nick Piggin81026792005-06-25 14:57:07 -07003146 *all_pinned = 0;
3147
Ingo Molnarcc367732007-10-15 17:00:18 +02003148 if (task_running(rq, p)) {
3149 schedstat_inc(p, se.nr_failed_migrations_running);
Nick Piggin81026792005-06-25 14:57:07 -07003150 return 0;
Ingo Molnarcc367732007-10-15 17:00:18 +02003151 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003152
Ingo Molnarda84d962007-10-15 17:00:18 +02003153 /*
3154 * Aggressive migration if:
3155 * 1) task is cache cold, or
3156 * 2) too many balance attempts have failed.
3157 */
3158
Luis Henriques708dc512009-03-16 19:59:02 +00003159 tsk_cache_hot = task_hot(p, rq->clock, sd);
3160 if (!tsk_cache_hot ||
3161 sd->nr_balance_failed > sd->cache_nice_tries) {
Ingo Molnarda84d962007-10-15 17:00:18 +02003162#ifdef CONFIG_SCHEDSTATS
Luis Henriques708dc512009-03-16 19:59:02 +00003163 if (tsk_cache_hot) {
Ingo Molnarda84d962007-10-15 17:00:18 +02003164 schedstat_inc(sd, lb_hot_gained[idle]);
Ingo Molnarcc367732007-10-15 17:00:18 +02003165 schedstat_inc(p, se.nr_forced_migrations);
3166 }
Ingo Molnarda84d962007-10-15 17:00:18 +02003167#endif
3168 return 1;
3169 }
3170
Luis Henriques708dc512009-03-16 19:59:02 +00003171 if (tsk_cache_hot) {
Ingo Molnarcc367732007-10-15 17:00:18 +02003172 schedstat_inc(p, se.nr_failed_migrations_hot);
Ingo Molnarda84d962007-10-15 17:00:18 +02003173 return 0;
Ingo Molnarcc367732007-10-15 17:00:18 +02003174 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003175 return 1;
3176}
3177
Peter Williamse1d14842007-10-24 18:23:51 +02003178static unsigned long
3179balance_tasks(struct rq *this_rq, int this_cpu, struct rq *busiest,
3180 unsigned long max_load_move, struct sched_domain *sd,
3181 enum cpu_idle_type idle, int *all_pinned,
3182 int *this_best_prio, struct rq_iterator *iterator)
Ingo Molnardd41f592007-07-09 18:51:59 +02003183{
Peter Zijlstra051c6762008-06-27 13:41:31 +02003184 int loops = 0, pulled = 0, pinned = 0;
Ingo Molnardd41f592007-07-09 18:51:59 +02003185 struct task_struct *p;
3186 long rem_load_move = max_load_move;
3187
Peter Williamse1d14842007-10-24 18:23:51 +02003188 if (max_load_move == 0)
Ingo Molnardd41f592007-07-09 18:51:59 +02003189 goto out;
3190
3191 pinned = 1;
3192
3193 /*
3194 * Start the load-balancing iterator:
3195 */
3196 p = iterator->start(iterator->arg);
3197next:
Peter Zijlstrab82d9fd2007-11-09 22:39:39 +01003198 if (!p || loops++ > sysctl_sched_nr_migrate)
Ingo Molnardd41f592007-07-09 18:51:59 +02003199 goto out;
Peter Zijlstra051c6762008-06-27 13:41:31 +02003200
3201 if ((p->se.load.weight >> 1) > rem_load_move ||
Ingo Molnardd41f592007-07-09 18:51:59 +02003202 !can_migrate_task(p, busiest, this_cpu, sd, idle, &pinned)) {
Ingo Molnardd41f592007-07-09 18:51:59 +02003203 p = iterator->next(iterator->arg);
3204 goto next;
3205 }
3206
3207 pull_task(busiest, p, this_rq, this_cpu);
3208 pulled++;
3209 rem_load_move -= p->se.load.weight;
3210
Gregory Haskins7e96fa52008-12-29 09:39:50 -05003211#ifdef CONFIG_PREEMPT
3212 /*
3213 * NEWIDLE balancing is a source of latency, so preemptible kernels
3214 * will stop after the first task is pulled to minimize the critical
3215 * section.
3216 */
3217 if (idle == CPU_NEWLY_IDLE)
3218 goto out;
3219#endif
3220
Ingo Molnardd41f592007-07-09 18:51:59 +02003221 /*
Peter Zijlstrab82d9fd2007-11-09 22:39:39 +01003222 * We only want to steal up to the prescribed amount of weighted load.
Ingo Molnardd41f592007-07-09 18:51:59 +02003223 */
Peter Williamse1d14842007-10-24 18:23:51 +02003224 if (rem_load_move > 0) {
Peter Williamsa4ac01c2007-08-09 11:16:46 +02003225 if (p->prio < *this_best_prio)
3226 *this_best_prio = p->prio;
Ingo Molnardd41f592007-07-09 18:51:59 +02003227 p = iterator->next(iterator->arg);
3228 goto next;
3229 }
3230out:
3231 /*
Peter Williamse1d14842007-10-24 18:23:51 +02003232 * Right now, this is one of only two places pull_task() is called,
Ingo Molnardd41f592007-07-09 18:51:59 +02003233 * so we can safely collect pull_task() stats here rather than
3234 * inside pull_task().
3235 */
3236 schedstat_add(sd, lb_gained[idle], pulled);
3237
3238 if (all_pinned)
3239 *all_pinned = pinned;
Peter Williamse1d14842007-10-24 18:23:51 +02003240
3241 return max_load_move - rem_load_move;
Ingo Molnardd41f592007-07-09 18:51:59 +02003242}
Ingo Molnar48f24c42006-07-03 00:25:40 -07003243
Linus Torvalds1da177e2005-04-16 15:20:36 -07003244/*
Peter Williams43010652007-08-09 11:16:46 +02003245 * move_tasks tries to move up to max_load_move weighted load from busiest to
3246 * this_rq, as part of a balancing operation within domain "sd".
3247 * Returns 1 if successful and 0 otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003248 *
3249 * Called with both runqueues locked.
3250 */
Ingo Molnar70b97a72006-07-03 00:25:42 -07003251static int move_tasks(struct rq *this_rq, int this_cpu, struct rq *busiest,
Peter Williams43010652007-08-09 11:16:46 +02003252 unsigned long max_load_move,
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003253 struct sched_domain *sd, enum cpu_idle_type idle,
Peter Williams2dd73a42006-06-27 02:54:34 -07003254 int *all_pinned)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003255{
Ingo Molnar5522d5d2007-10-15 17:00:12 +02003256 const struct sched_class *class = sched_class_highest;
Peter Williams43010652007-08-09 11:16:46 +02003257 unsigned long total_load_moved = 0;
Peter Williamsa4ac01c2007-08-09 11:16:46 +02003258 int this_best_prio = this_rq->curr->prio;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003259
Ingo Molnardd41f592007-07-09 18:51:59 +02003260 do {
Peter Williams43010652007-08-09 11:16:46 +02003261 total_load_moved +=
3262 class->load_balance(this_rq, this_cpu, busiest,
Peter Williamse1d14842007-10-24 18:23:51 +02003263 max_load_move - total_load_moved,
Peter Williamsa4ac01c2007-08-09 11:16:46 +02003264 sd, idle, all_pinned, &this_best_prio);
Ingo Molnardd41f592007-07-09 18:51:59 +02003265 class = class->next;
Gregory Haskinsc4acb2c2008-06-27 14:29:55 -06003266
Gregory Haskins7e96fa52008-12-29 09:39:50 -05003267#ifdef CONFIG_PREEMPT
3268 /*
3269 * NEWIDLE balancing is a source of latency, so preemptible
3270 * kernels will stop after the first task is pulled to minimize
3271 * the critical section.
3272 */
Gregory Haskinsc4acb2c2008-06-27 14:29:55 -06003273 if (idle == CPU_NEWLY_IDLE && this_rq->nr_running)
3274 break;
Gregory Haskins7e96fa52008-12-29 09:39:50 -05003275#endif
Peter Williams43010652007-08-09 11:16:46 +02003276 } while (class && max_load_move > total_load_moved);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003277
Peter Williams43010652007-08-09 11:16:46 +02003278 return total_load_moved > 0;
3279}
3280
Peter Williamse1d14842007-10-24 18:23:51 +02003281static int
3282iter_move_one_task(struct rq *this_rq, int this_cpu, struct rq *busiest,
3283 struct sched_domain *sd, enum cpu_idle_type idle,
3284 struct rq_iterator *iterator)
3285{
3286 struct task_struct *p = iterator->start(iterator->arg);
3287 int pinned = 0;
3288
3289 while (p) {
3290 if (can_migrate_task(p, busiest, this_cpu, sd, idle, &pinned)) {
3291 pull_task(busiest, p, this_rq, this_cpu);
3292 /*
3293 * Right now, this is only the second place pull_task()
3294 * is called, so we can safely collect pull_task()
3295 * stats here rather than inside pull_task().
3296 */
3297 schedstat_inc(sd, lb_gained[idle]);
3298
3299 return 1;
3300 }
3301 p = iterator->next(iterator->arg);
3302 }
3303
3304 return 0;
3305}
3306
Peter Williams43010652007-08-09 11:16:46 +02003307/*
3308 * move_one_task tries to move exactly one task from busiest to this_rq, as
3309 * part of active balancing operations within "domain".
3310 * Returns 1 if successful and 0 otherwise.
3311 *
3312 * Called with both runqueues locked.
3313 */
3314static int move_one_task(struct rq *this_rq, int this_cpu, struct rq *busiest,
3315 struct sched_domain *sd, enum cpu_idle_type idle)
3316{
Ingo Molnar5522d5d2007-10-15 17:00:12 +02003317 const struct sched_class *class;
Peter Williams43010652007-08-09 11:16:46 +02003318
3319 for (class = sched_class_highest; class; class = class->next)
Peter Williamse1d14842007-10-24 18:23:51 +02003320 if (class->move_one_task(this_rq, this_cpu, busiest, sd, idle))
Peter Williams43010652007-08-09 11:16:46 +02003321 return 1;
3322
3323 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003324}
Gautham R Shenoy67bb6c02009-03-25 14:43:35 +05303325/********** Helpers for find_busiest_group ************************/
Randy Dunlapd5ac5372009-03-28 21:52:47 -07003326/*
Gautham R Shenoy222d6562009-03-25 14:43:56 +05303327 * sd_lb_stats - Structure to store the statistics of a sched_domain
3328 * during load balancing.
3329 */
3330struct sd_lb_stats {
3331 struct sched_group *busiest; /* Busiest group in this sd */
3332 struct sched_group *this; /* Local group in this sd */
3333 unsigned long total_load; /* Total load of all groups in sd */
3334 unsigned long total_pwr; /* Total power of all groups in sd */
3335 unsigned long avg_load; /* Average load across all groups in sd */
3336
3337 /** Statistics of this group */
3338 unsigned long this_load;
3339 unsigned long this_load_per_task;
3340 unsigned long this_nr_running;
3341
3342 /* Statistics of the busiest group */
3343 unsigned long max_load;
3344 unsigned long busiest_load_per_task;
3345 unsigned long busiest_nr_running;
3346
3347 int group_imb; /* Is there imbalance in this sd */
3348#if defined(CONFIG_SCHED_MC) || defined(CONFIG_SCHED_SMT)
3349 int power_savings_balance; /* Is powersave balance needed for this sd */
3350 struct sched_group *group_min; /* Least loaded group in sd */
3351 struct sched_group *group_leader; /* Group which relieves group_min */
3352 unsigned long min_load_per_task; /* load_per_task in group_min */
3353 unsigned long leader_nr_running; /* Nr running of group_leader */
3354 unsigned long min_nr_running; /* Nr running of group_min */
3355#endif
3356};
Linus Torvalds1da177e2005-04-16 15:20:36 -07003357
3358/*
Gautham R Shenoy381be782009-03-25 14:43:46 +05303359 * sg_lb_stats - stats of a sched_group required for load_balancing
3360 */
3361struct sg_lb_stats {
3362 unsigned long avg_load; /*Avg load across the CPUs of the group */
3363 unsigned long group_load; /* Total load over the CPUs of the group */
3364 unsigned long sum_nr_running; /* Nr tasks running in the group */
3365 unsigned long sum_weighted_load; /* Weighted load of group's tasks */
3366 unsigned long group_capacity;
3367 int group_imb; /* Is there an imbalance in the group ? */
3368};
3369
3370/**
Gautham R Shenoy67bb6c02009-03-25 14:43:35 +05303371 * group_first_cpu - Returns the first cpu in the cpumask of a sched_group.
3372 * @group: The group whose first cpu is to be returned.
3373 */
3374static inline unsigned int group_first_cpu(struct sched_group *group)
3375{
3376 return cpumask_first(sched_group_cpus(group));
3377}
3378
3379/**
3380 * get_sd_load_idx - Obtain the load index for a given sched domain.
3381 * @sd: The sched_domain whose load_idx is to be obtained.
3382 * @idle: The Idle status of the CPU for whose sd load_icx is obtained.
3383 */
3384static inline int get_sd_load_idx(struct sched_domain *sd,
3385 enum cpu_idle_type idle)
3386{
3387 int load_idx;
3388
3389 switch (idle) {
3390 case CPU_NOT_IDLE:
3391 load_idx = sd->busy_idx;
3392 break;
3393
3394 case CPU_NEWLY_IDLE:
3395 load_idx = sd->newidle_idx;
3396 break;
3397 default:
3398 load_idx = sd->idle_idx;
3399 break;
3400 }
3401
3402 return load_idx;
3403}
Gautham R Shenoy1f8c5532009-03-25 14:43:51 +05303404
3405
Gautham R Shenoyc071df12009-03-25 14:44:22 +05303406#if defined(CONFIG_SCHED_MC) || defined(CONFIG_SCHED_SMT)
3407/**
3408 * init_sd_power_savings_stats - Initialize power savings statistics for
3409 * the given sched_domain, during load balancing.
3410 *
3411 * @sd: Sched domain whose power-savings statistics are to be initialized.
3412 * @sds: Variable containing the statistics for sd.
3413 * @idle: Idle status of the CPU at which we're performing load-balancing.
3414 */
3415static inline void init_sd_power_savings_stats(struct sched_domain *sd,
3416 struct sd_lb_stats *sds, enum cpu_idle_type idle)
3417{
3418 /*
3419 * Busy processors will not participate in power savings
3420 * balance.
3421 */
3422 if (idle == CPU_NOT_IDLE || !(sd->flags & SD_POWERSAVINGS_BALANCE))
3423 sds->power_savings_balance = 0;
3424 else {
3425 sds->power_savings_balance = 1;
3426 sds->min_nr_running = ULONG_MAX;
3427 sds->leader_nr_running = 0;
3428 }
3429}
3430
3431/**
3432 * update_sd_power_savings_stats - Update the power saving stats for a
3433 * sched_domain while performing load balancing.
3434 *
3435 * @group: sched_group belonging to the sched_domain under consideration.
3436 * @sds: Variable containing the statistics of the sched_domain
3437 * @local_group: Does group contain the CPU for which we're performing
3438 * load balancing ?
3439 * @sgs: Variable containing the statistics of the group.
3440 */
3441static inline void update_sd_power_savings_stats(struct sched_group *group,
3442 struct sd_lb_stats *sds, int local_group, struct sg_lb_stats *sgs)
3443{
3444
3445 if (!sds->power_savings_balance)
3446 return;
3447
3448 /*
3449 * If the local group is idle or completely loaded
3450 * no need to do power savings balance at this domain
3451 */
3452 if (local_group && (sds->this_nr_running >= sgs->group_capacity ||
3453 !sds->this_nr_running))
3454 sds->power_savings_balance = 0;
3455
3456 /*
3457 * If a group is already running at full capacity or idle,
3458 * don't include that group in power savings calculations
3459 */
3460 if (!sds->power_savings_balance ||
3461 sgs->sum_nr_running >= sgs->group_capacity ||
3462 !sgs->sum_nr_running)
3463 return;
3464
3465 /*
3466 * Calculate the group which has the least non-idle load.
3467 * This is the group from where we need to pick up the load
3468 * for saving power
3469 */
3470 if ((sgs->sum_nr_running < sds->min_nr_running) ||
3471 (sgs->sum_nr_running == sds->min_nr_running &&
3472 group_first_cpu(group) > group_first_cpu(sds->group_min))) {
3473 sds->group_min = group;
3474 sds->min_nr_running = sgs->sum_nr_running;
3475 sds->min_load_per_task = sgs->sum_weighted_load /
3476 sgs->sum_nr_running;
3477 }
3478
3479 /*
3480 * Calculate the group which is almost near its
3481 * capacity but still has some space to pick up some load
3482 * from other group and save more power
3483 */
3484 if (sgs->sum_nr_running > sgs->group_capacity - 1)
3485 return;
3486
3487 if (sgs->sum_nr_running > sds->leader_nr_running ||
3488 (sgs->sum_nr_running == sds->leader_nr_running &&
3489 group_first_cpu(group) < group_first_cpu(sds->group_leader))) {
3490 sds->group_leader = group;
3491 sds->leader_nr_running = sgs->sum_nr_running;
3492 }
3493}
3494
3495/**
Randy Dunlapd5ac5372009-03-28 21:52:47 -07003496 * check_power_save_busiest_group - see if there is potential for some power-savings balance
Gautham R Shenoyc071df12009-03-25 14:44:22 +05303497 * @sds: Variable containing the statistics of the sched_domain
3498 * under consideration.
3499 * @this_cpu: Cpu at which we're currently performing load-balancing.
3500 * @imbalance: Variable to store the imbalance.
3501 *
Randy Dunlapd5ac5372009-03-28 21:52:47 -07003502 * Description:
3503 * Check if we have potential to perform some power-savings balance.
3504 * If yes, set the busiest group to be the least loaded group in the
3505 * sched_domain, so that it's CPUs can be put to idle.
3506 *
Gautham R Shenoyc071df12009-03-25 14:44:22 +05303507 * Returns 1 if there is potential to perform power-savings balance.
3508 * Else returns 0.
3509 */
3510static inline int check_power_save_busiest_group(struct sd_lb_stats *sds,
3511 int this_cpu, unsigned long *imbalance)
3512{
3513 if (!sds->power_savings_balance)
3514 return 0;
3515
3516 if (sds->this != sds->group_leader ||
3517 sds->group_leader == sds->group_min)
3518 return 0;
3519
3520 *imbalance = sds->min_load_per_task;
3521 sds->busiest = sds->group_min;
3522
3523 if (sched_mc_power_savings >= POWERSAVINGS_BALANCE_WAKEUP) {
3524 cpu_rq(this_cpu)->rd->sched_mc_preferred_wakeup_cpu =
3525 group_first_cpu(sds->group_leader);
3526 }
3527
3528 return 1;
3529
3530}
3531#else /* CONFIG_SCHED_MC || CONFIG_SCHED_SMT */
3532static inline void init_sd_power_savings_stats(struct sched_domain *sd,
3533 struct sd_lb_stats *sds, enum cpu_idle_type idle)
3534{
3535 return;
3536}
3537
3538static inline void update_sd_power_savings_stats(struct sched_group *group,
3539 struct sd_lb_stats *sds, int local_group, struct sg_lb_stats *sgs)
3540{
3541 return;
3542}
3543
3544static inline int check_power_save_busiest_group(struct sd_lb_stats *sds,
3545 int this_cpu, unsigned long *imbalance)
3546{
3547 return 0;
3548}
3549#endif /* CONFIG_SCHED_MC || CONFIG_SCHED_SMT */
3550
3551
Gautham R Shenoy1f8c5532009-03-25 14:43:51 +05303552/**
3553 * update_sg_lb_stats - Update sched_group's statistics for load balancing.
3554 * @group: sched_group whose statistics are to be updated.
3555 * @this_cpu: Cpu for which load balance is currently performed.
3556 * @idle: Idle status of this_cpu
3557 * @load_idx: Load index of sched_domain of this_cpu for load calc.
3558 * @sd_idle: Idle status of the sched_domain containing group.
3559 * @local_group: Does group contain this_cpu.
3560 * @cpus: Set of cpus considered for load balancing.
3561 * @balance: Should we balance.
3562 * @sgs: variable to hold the statistics for this group.
3563 */
3564static inline void update_sg_lb_stats(struct sched_group *group, int this_cpu,
3565 enum cpu_idle_type idle, int load_idx, int *sd_idle,
3566 int local_group, const struct cpumask *cpus,
3567 int *balance, struct sg_lb_stats *sgs)
3568{
3569 unsigned long load, max_cpu_load, min_cpu_load;
3570 int i;
3571 unsigned int balance_cpu = -1, first_idle_cpu = 0;
3572 unsigned long sum_avg_load_per_task;
3573 unsigned long avg_load_per_task;
3574
3575 if (local_group)
3576 balance_cpu = group_first_cpu(group);
3577
3578 /* Tally up the load of all CPUs in the group */
3579 sum_avg_load_per_task = avg_load_per_task = 0;
3580 max_cpu_load = 0;
3581 min_cpu_load = ~0UL;
3582
3583 for_each_cpu_and(i, sched_group_cpus(group), cpus) {
3584 struct rq *rq = cpu_rq(i);
3585
3586 if (*sd_idle && rq->nr_running)
3587 *sd_idle = 0;
3588
3589 /* Bias balancing toward cpus of our domain */
3590 if (local_group) {
3591 if (idle_cpu(i) && !first_idle_cpu) {
3592 first_idle_cpu = 1;
3593 balance_cpu = i;
3594 }
3595
3596 load = target_load(i, load_idx);
3597 } else {
3598 load = source_load(i, load_idx);
3599 if (load > max_cpu_load)
3600 max_cpu_load = load;
3601 if (min_cpu_load > load)
3602 min_cpu_load = load;
3603 }
3604
3605 sgs->group_load += load;
3606 sgs->sum_nr_running += rq->nr_running;
3607 sgs->sum_weighted_load += weighted_cpuload(i);
3608
3609 sum_avg_load_per_task += cpu_avg_load_per_task(i);
3610 }
3611
3612 /*
3613 * First idle cpu or the first cpu(busiest) in this sched group
3614 * is eligible for doing load balancing at this and above
3615 * domains. In the newly idle case, we will allow all the cpu's
3616 * to do the newly idle load balance.
3617 */
3618 if (idle != CPU_NEWLY_IDLE && local_group &&
3619 balance_cpu != this_cpu && balance) {
3620 *balance = 0;
3621 return;
3622 }
3623
3624 /* Adjust by relative CPU power of the group */
3625 sgs->avg_load = sg_div_cpu_power(group,
3626 sgs->group_load * SCHED_LOAD_SCALE);
3627
3628
3629 /*
3630 * Consider the group unbalanced when the imbalance is larger
3631 * than the average weight of two tasks.
3632 *
3633 * APZ: with cgroup the avg task weight can vary wildly and
3634 * might not be a suitable number - should we keep a
3635 * normalized nr_running number somewhere that negates
3636 * the hierarchy?
3637 */
3638 avg_load_per_task = sg_div_cpu_power(group,
3639 sum_avg_load_per_task * SCHED_LOAD_SCALE);
3640
3641 if ((max_cpu_load - min_cpu_load) > 2*avg_load_per_task)
3642 sgs->group_imb = 1;
3643
3644 sgs->group_capacity = group->__cpu_power / SCHED_LOAD_SCALE;
3645
3646}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003647
Gautham R Shenoy37abe192009-03-25 14:44:01 +05303648/**
3649 * update_sd_lb_stats - Update sched_group's statistics for load balancing.
3650 * @sd: sched_domain whose statistics are to be updated.
3651 * @this_cpu: Cpu for which load balance is currently performed.
3652 * @idle: Idle status of this_cpu
3653 * @sd_idle: Idle status of the sched_domain containing group.
3654 * @cpus: Set of cpus considered for load balancing.
3655 * @balance: Should we balance.
3656 * @sds: variable to hold the statistics for this sched_domain.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003657 */
Gautham R Shenoy37abe192009-03-25 14:44:01 +05303658static inline void update_sd_lb_stats(struct sched_domain *sd, int this_cpu,
3659 enum cpu_idle_type idle, int *sd_idle,
3660 const struct cpumask *cpus, int *balance,
3661 struct sd_lb_stats *sds)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003662{
Gautham R Shenoy222d6562009-03-25 14:43:56 +05303663 struct sched_group *group = sd->groups;
Gautham R Shenoy37abe192009-03-25 14:44:01 +05303664 struct sg_lb_stats sgs;
Gautham R Shenoy222d6562009-03-25 14:43:56 +05303665 int load_idx;
3666
Gautham R Shenoyc071df12009-03-25 14:44:22 +05303667 init_sd_power_savings_stats(sd, sds, idle);
Gautham R Shenoy67bb6c02009-03-25 14:43:35 +05303668 load_idx = get_sd_load_idx(sd, idle);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003669
3670 do {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003671 int local_group;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003672
Rusty Russell758b2cd2008-11-25 02:35:04 +10303673 local_group = cpumask_test_cpu(this_cpu,
3674 sched_group_cpus(group));
Gautham R Shenoy381be782009-03-25 14:43:46 +05303675 memset(&sgs, 0, sizeof(sgs));
Gautham R Shenoy1f8c5532009-03-25 14:43:51 +05303676 update_sg_lb_stats(group, this_cpu, idle, load_idx, sd_idle,
3677 local_group, cpus, balance, &sgs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003678
Gautham R Shenoy37abe192009-03-25 14:44:01 +05303679 if (local_group && balance && !(*balance))
3680 return;
Siddha, Suresh B783609c2006-12-10 02:20:33 -08003681
Gautham R Shenoy37abe192009-03-25 14:44:01 +05303682 sds->total_load += sgs.group_load;
3683 sds->total_pwr += group->__cpu_power;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003684
Linus Torvalds1da177e2005-04-16 15:20:36 -07003685 if (local_group) {
Gautham R Shenoy37abe192009-03-25 14:44:01 +05303686 sds->this_load = sgs.avg_load;
3687 sds->this = group;
3688 sds->this_nr_running = sgs.sum_nr_running;
3689 sds->this_load_per_task = sgs.sum_weighted_load;
3690 } else if (sgs.avg_load > sds->max_load &&
Gautham R Shenoy381be782009-03-25 14:43:46 +05303691 (sgs.sum_nr_running > sgs.group_capacity ||
3692 sgs.group_imb)) {
Gautham R Shenoy37abe192009-03-25 14:44:01 +05303693 sds->max_load = sgs.avg_load;
3694 sds->busiest = group;
3695 sds->busiest_nr_running = sgs.sum_nr_running;
3696 sds->busiest_load_per_task = sgs.sum_weighted_load;
3697 sds->group_imb = sgs.group_imb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003698 }
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07003699
Gautham R Shenoyc071df12009-03-25 14:44:22 +05303700 update_sd_power_savings_stats(group, sds, local_group, &sgs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003701 group = group->next;
3702 } while (group != sd->groups);
3703
Gautham R Shenoy37abe192009-03-25 14:44:01 +05303704}
Gautham R Shenoy2e6f44a2009-03-25 14:44:06 +05303705
3706/**
3707 * fix_small_imbalance - Calculate the minor imbalance that exists
Gautham R Shenoydbc523a2009-03-25 14:44:12 +05303708 * amongst the groups of a sched_domain, during
3709 * load balancing.
Gautham R Shenoy2e6f44a2009-03-25 14:44:06 +05303710 * @sds: Statistics of the sched_domain whose imbalance is to be calculated.
3711 * @this_cpu: The cpu at whose sched_domain we're performing load-balance.
3712 * @imbalance: Variable to store the imbalance.
3713 */
3714static inline void fix_small_imbalance(struct sd_lb_stats *sds,
3715 int this_cpu, unsigned long *imbalance)
3716{
3717 unsigned long tmp, pwr_now = 0, pwr_move = 0;
3718 unsigned int imbn = 2;
3719
3720 if (sds->this_nr_running) {
3721 sds->this_load_per_task /= sds->this_nr_running;
3722 if (sds->busiest_load_per_task >
3723 sds->this_load_per_task)
3724 imbn = 1;
3725 } else
3726 sds->this_load_per_task =
3727 cpu_avg_load_per_task(this_cpu);
3728
3729 if (sds->max_load - sds->this_load + sds->busiest_load_per_task >=
3730 sds->busiest_load_per_task * imbn) {
3731 *imbalance = sds->busiest_load_per_task;
3732 return;
3733 }
3734
3735 /*
3736 * OK, we don't have enough imbalance to justify moving tasks,
3737 * however we may be able to increase total CPU power used by
3738 * moving them.
3739 */
3740
3741 pwr_now += sds->busiest->__cpu_power *
3742 min(sds->busiest_load_per_task, sds->max_load);
3743 pwr_now += sds->this->__cpu_power *
3744 min(sds->this_load_per_task, sds->this_load);
3745 pwr_now /= SCHED_LOAD_SCALE;
3746
3747 /* Amount of load we'd subtract */
3748 tmp = sg_div_cpu_power(sds->busiest,
3749 sds->busiest_load_per_task * SCHED_LOAD_SCALE);
3750 if (sds->max_load > tmp)
3751 pwr_move += sds->busiest->__cpu_power *
3752 min(sds->busiest_load_per_task, sds->max_load - tmp);
3753
3754 /* Amount of load we'd add */
3755 if (sds->max_load * sds->busiest->__cpu_power <
3756 sds->busiest_load_per_task * SCHED_LOAD_SCALE)
3757 tmp = sg_div_cpu_power(sds->this,
3758 sds->max_load * sds->busiest->__cpu_power);
3759 else
3760 tmp = sg_div_cpu_power(sds->this,
3761 sds->busiest_load_per_task * SCHED_LOAD_SCALE);
3762 pwr_move += sds->this->__cpu_power *
3763 min(sds->this_load_per_task, sds->this_load + tmp);
3764 pwr_move /= SCHED_LOAD_SCALE;
3765
3766 /* Move if we gain throughput */
3767 if (pwr_move > pwr_now)
3768 *imbalance = sds->busiest_load_per_task;
3769}
Gautham R Shenoydbc523a2009-03-25 14:44:12 +05303770
3771/**
3772 * calculate_imbalance - Calculate the amount of imbalance present within the
3773 * groups of a given sched_domain during load balance.
3774 * @sds: statistics of the sched_domain whose imbalance is to be calculated.
3775 * @this_cpu: Cpu for which currently load balance is being performed.
3776 * @imbalance: The variable to store the imbalance.
3777 */
3778static inline void calculate_imbalance(struct sd_lb_stats *sds, int this_cpu,
3779 unsigned long *imbalance)
3780{
3781 unsigned long max_pull;
3782 /*
3783 * In the presence of smp nice balancing, certain scenarios can have
3784 * max load less than avg load(as we skip the groups at or below
3785 * its cpu_power, while calculating max_load..)
3786 */
3787 if (sds->max_load < sds->avg_load) {
3788 *imbalance = 0;
3789 return fix_small_imbalance(sds, this_cpu, imbalance);
3790 }
3791
3792 /* Don't want to pull so many tasks that a group would go idle */
3793 max_pull = min(sds->max_load - sds->avg_load,
3794 sds->max_load - sds->busiest_load_per_task);
3795
3796 /* How much load to actually move to equalise the imbalance */
3797 *imbalance = min(max_pull * sds->busiest->__cpu_power,
3798 (sds->avg_load - sds->this_load) * sds->this->__cpu_power)
3799 / SCHED_LOAD_SCALE;
3800
3801 /*
3802 * if *imbalance is less than the average load per runnable task
3803 * there is no gaurantee that any tasks will be moved so we'll have
3804 * a think about bumping its value to force at least one task to be
3805 * moved
3806 */
3807 if (*imbalance < sds->busiest_load_per_task)
3808 return fix_small_imbalance(sds, this_cpu, imbalance);
3809
3810}
Gautham R Shenoy37abe192009-03-25 14:44:01 +05303811/******* find_busiest_group() helpers end here *********************/
3812
Gautham R Shenoyb7bb4c92009-03-25 14:44:27 +05303813/**
3814 * find_busiest_group - Returns the busiest group within the sched_domain
3815 * if there is an imbalance. If there isn't an imbalance, and
3816 * the user has opted for power-savings, it returns a group whose
3817 * CPUs can be put to idle by rebalancing those tasks elsewhere, if
3818 * such a group exists.
3819 *
3820 * Also calculates the amount of weighted load which should be moved
3821 * to restore balance.
3822 *
3823 * @sd: The sched_domain whose busiest group is to be returned.
3824 * @this_cpu: The cpu for which load balancing is currently being performed.
3825 * @imbalance: Variable which stores amount of weighted load which should
3826 * be moved to restore balance/put a group to idle.
3827 * @idle: The idle status of this_cpu.
3828 * @sd_idle: The idleness of sd
3829 * @cpus: The set of CPUs under consideration for load-balancing.
3830 * @balance: Pointer to a variable indicating if this_cpu
3831 * is the appropriate cpu to perform load balancing at this_level.
3832 *
3833 * Returns: - the busiest group if imbalance exists.
3834 * - If no imbalance and user has opted for power-savings balance,
3835 * return the least loaded group whose CPUs can be
3836 * put to idle by rebalancing its tasks onto our group.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003837 */
3838static struct sched_group *
3839find_busiest_group(struct sched_domain *sd, int this_cpu,
3840 unsigned long *imbalance, enum cpu_idle_type idle,
3841 int *sd_idle, const struct cpumask *cpus, int *balance)
3842{
Gautham R Shenoy37abe192009-03-25 14:44:01 +05303843 struct sd_lb_stats sds;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003844
Gautham R Shenoy37abe192009-03-25 14:44:01 +05303845 memset(&sds, 0, sizeof(sds));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003846
Gautham R Shenoy37abe192009-03-25 14:44:01 +05303847 /*
3848 * Compute the various statistics relavent for load balancing at
3849 * this level.
3850 */
3851 update_sd_lb_stats(sd, this_cpu, idle, sd_idle, cpus,
3852 balance, &sds);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003853
Gautham R Shenoyb7bb4c92009-03-25 14:44:27 +05303854 /* Cases where imbalance does not exist from POV of this_cpu */
3855 /* 1) this_cpu is not the appropriate cpu to perform load balancing
3856 * at this level.
3857 * 2) There is no busy sibling group to pull from.
3858 * 3) This group is the busiest group.
3859 * 4) This group is more busy than the avg busieness at this
3860 * sched_domain.
3861 * 5) The imbalance is within the specified limit.
3862 * 6) Any rebalance would lead to ping-pong
3863 */
Gautham R Shenoy37abe192009-03-25 14:44:01 +05303864 if (balance && !(*balance))
3865 goto ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003866
Gautham R Shenoyb7bb4c92009-03-25 14:44:27 +05303867 if (!sds.busiest || sds.busiest_nr_running == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003868 goto out_balanced;
3869
Gautham R Shenoyb7bb4c92009-03-25 14:44:27 +05303870 if (sds.this_load >= sds.max_load)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003871 goto out_balanced;
3872
Gautham R Shenoy222d6562009-03-25 14:43:56 +05303873 sds.avg_load = (SCHED_LOAD_SCALE * sds.total_load) / sds.total_pwr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003874
Gautham R Shenoyb7bb4c92009-03-25 14:44:27 +05303875 if (sds.this_load >= sds.avg_load)
3876 goto out_balanced;
3877
3878 if (100 * sds.max_load <= sd->imbalance_pct * sds.this_load)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003879 goto out_balanced;
3880
Gautham R Shenoy222d6562009-03-25 14:43:56 +05303881 sds.busiest_load_per_task /= sds.busiest_nr_running;
3882 if (sds.group_imb)
3883 sds.busiest_load_per_task =
3884 min(sds.busiest_load_per_task, sds.avg_load);
Ken Chen908a7c12007-10-17 16:55:11 +02003885
Linus Torvalds1da177e2005-04-16 15:20:36 -07003886 /*
3887 * We're trying to get all the cpus to the average_load, so we don't
3888 * want to push ourselves above the average load, nor do we wish to
3889 * reduce the max loaded cpu below the average load, as either of these
3890 * actions would just result in more rebalancing later, and ping-pong
3891 * tasks around. Thus we look for the minimum possible imbalance.
3892 * Negative imbalances (*we* are more loaded than anyone else) will
3893 * be counted as no imbalance for these purposes -- we can't fix that
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01003894 * by pulling tasks to us. Be careful of negative numbers as they'll
Linus Torvalds1da177e2005-04-16 15:20:36 -07003895 * appear as very large values with unsigned longs.
3896 */
Gautham R Shenoy222d6562009-03-25 14:43:56 +05303897 if (sds.max_load <= sds.busiest_load_per_task)
Peter Williams2dd73a42006-06-27 02:54:34 -07003898 goto out_balanced;
3899
Gautham R Shenoydbc523a2009-03-25 14:44:12 +05303900 /* Looks like there is an imbalance. Compute it */
3901 calculate_imbalance(&sds, this_cpu, imbalance);
Gautham R Shenoy222d6562009-03-25 14:43:56 +05303902 return sds.busiest;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003903
3904out_balanced:
Gautham R Shenoyc071df12009-03-25 14:44:22 +05303905 /*
3906 * There is no obvious imbalance. But check if we can do some balancing
3907 * to save power.
3908 */
3909 if (check_power_save_busiest_group(&sds, this_cpu, imbalance))
3910 return sds.busiest;
Siddha, Suresh B783609c2006-12-10 02:20:33 -08003911ret:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003912 *imbalance = 0;
3913 return NULL;
3914}
3915
3916/*
3917 * find_busiest_queue - find the busiest runqueue among the cpus in group.
3918 */
Ingo Molnar70b97a72006-07-03 00:25:42 -07003919static struct rq *
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003920find_busiest_queue(struct sched_group *group, enum cpu_idle_type idle,
Rusty Russell96f874e2008-11-25 02:35:14 +10303921 unsigned long imbalance, const struct cpumask *cpus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003922{
Ingo Molnar70b97a72006-07-03 00:25:42 -07003923 struct rq *busiest = NULL, *rq;
Peter Williams2dd73a42006-06-27 02:54:34 -07003924 unsigned long max_load = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003925 int i;
3926
Rusty Russell758b2cd2008-11-25 02:35:04 +10303927 for_each_cpu(i, sched_group_cpus(group)) {
Ingo Molnardd41f592007-07-09 18:51:59 +02003928 unsigned long wl;
Christoph Lameter0a2966b2006-09-25 23:30:51 -07003929
Rusty Russell96f874e2008-11-25 02:35:14 +10303930 if (!cpumask_test_cpu(i, cpus))
Christoph Lameter0a2966b2006-09-25 23:30:51 -07003931 continue;
3932
Ingo Molnar48f24c42006-07-03 00:25:40 -07003933 rq = cpu_rq(i);
Ingo Molnardd41f592007-07-09 18:51:59 +02003934 wl = weighted_cpuload(i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003935
Ingo Molnardd41f592007-07-09 18:51:59 +02003936 if (rq->nr_running == 1 && wl > imbalance)
Peter Williams2dd73a42006-06-27 02:54:34 -07003937 continue;
3938
Ingo Molnardd41f592007-07-09 18:51:59 +02003939 if (wl > max_load) {
3940 max_load = wl;
Ingo Molnar48f24c42006-07-03 00:25:40 -07003941 busiest = rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003942 }
3943 }
3944
3945 return busiest;
3946}
3947
3948/*
Nick Piggin77391d72005-06-25 14:57:30 -07003949 * Max backoff if we encounter pinned tasks. Pretty arbitrary value, but
3950 * so long as it is large enough.
3951 */
3952#define MAX_PINNED_INTERVAL 512
3953
Rusty Russelldf7c8e82009-03-19 15:22:20 +10303954/* Working cpumask for load_balance and load_balance_newidle. */
3955static DEFINE_PER_CPU(cpumask_var_t, load_balance_tmpmask);
3956
Nick Piggin77391d72005-06-25 14:57:30 -07003957/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003958 * Check this_cpu to ensure it is balanced within domain. Attempt to move
3959 * tasks if there is an imbalance.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003960 */
Ingo Molnar70b97a72006-07-03 00:25:42 -07003961static int load_balance(int this_cpu, struct rq *this_rq,
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003962 struct sched_domain *sd, enum cpu_idle_type idle,
Rusty Russelldf7c8e82009-03-19 15:22:20 +10303963 int *balance)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003964{
Peter Williams43010652007-08-09 11:16:46 +02003965 int ld_moved, all_pinned = 0, active_balance = 0, sd_idle = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003966 struct sched_group *group;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003967 unsigned long imbalance;
Ingo Molnar70b97a72006-07-03 00:25:42 -07003968 struct rq *busiest;
Christoph Lameterfe2eea32006-12-10 02:20:21 -08003969 unsigned long flags;
Rusty Russelldf7c8e82009-03-19 15:22:20 +10303970 struct cpumask *cpus = __get_cpu_var(load_balance_tmpmask);
Nick Piggin5969fe02005-09-10 00:26:19 -07003971
Rusty Russell96f874e2008-11-25 02:35:14 +10303972 cpumask_setall(cpus);
Mike Travis7c16ec52008-04-04 18:11:11 -07003973
Siddha, Suresh B89c47102006-10-03 01:14:09 -07003974 /*
3975 * When power savings policy is enabled for the parent domain, idle
3976 * sibling can pick up load irrespective of busy siblings. In this case,
Ingo Molnardd41f592007-07-09 18:51:59 +02003977 * let the state of idle sibling percolate up as CPU_IDLE, instead of
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003978 * portraying it as CPU_NOT_IDLE.
Siddha, Suresh B89c47102006-10-03 01:14:09 -07003979 */
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003980 if (idle != CPU_NOT_IDLE && sd->flags & SD_SHARE_CPUPOWER &&
Siddha, Suresh B89c47102006-10-03 01:14:09 -07003981 !test_sd_parent(sd, SD_POWERSAVINGS_BALANCE))
Nick Piggin5969fe02005-09-10 00:26:19 -07003982 sd_idle = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003983
Ingo Molnar2d723762007-10-15 17:00:12 +02003984 schedstat_inc(sd, lb_count[idle]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003985
Christoph Lameter0a2966b2006-09-25 23:30:51 -07003986redo:
Peter Zijlstrac8cba852008-06-27 13:41:23 +02003987 update_shares(sd);
Christoph Lameter0a2966b2006-09-25 23:30:51 -07003988 group = find_busiest_group(sd, this_cpu, &imbalance, idle, &sd_idle,
Mike Travis7c16ec52008-04-04 18:11:11 -07003989 cpus, balance);
Siddha, Suresh B783609c2006-12-10 02:20:33 -08003990
Chen, Kenneth W06066712006-12-10 02:20:35 -08003991 if (*balance == 0)
Siddha, Suresh B783609c2006-12-10 02:20:33 -08003992 goto out_balanced;
Siddha, Suresh B783609c2006-12-10 02:20:33 -08003993
Linus Torvalds1da177e2005-04-16 15:20:36 -07003994 if (!group) {
3995 schedstat_inc(sd, lb_nobusyg[idle]);
3996 goto out_balanced;
3997 }
3998
Mike Travis7c16ec52008-04-04 18:11:11 -07003999 busiest = find_busiest_queue(group, idle, imbalance, cpus);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004000 if (!busiest) {
4001 schedstat_inc(sd, lb_nobusyq[idle]);
4002 goto out_balanced;
4003 }
4004
Nick Piggindb935db2005-06-25 14:57:11 -07004005 BUG_ON(busiest == this_rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004006
4007 schedstat_add(sd, lb_imbalance[idle], imbalance);
4008
Peter Williams43010652007-08-09 11:16:46 +02004009 ld_moved = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004010 if (busiest->nr_running > 1) {
4011 /*
4012 * Attempt to move tasks. If find_busiest_group has found
4013 * an imbalance but busiest->nr_running <= 1, the group is
Peter Williams43010652007-08-09 11:16:46 +02004014 * still unbalanced. ld_moved simply stays zero, so it is
Linus Torvalds1da177e2005-04-16 15:20:36 -07004015 * correctly treated as an imbalance.
4016 */
Christoph Lameterfe2eea32006-12-10 02:20:21 -08004017 local_irq_save(flags);
Nick Piggine17224b2005-09-10 00:26:18 -07004018 double_rq_lock(this_rq, busiest);
Peter Williams43010652007-08-09 11:16:46 +02004019 ld_moved = move_tasks(this_rq, this_cpu, busiest,
Ingo Molnar48f24c42006-07-03 00:25:40 -07004020 imbalance, sd, idle, &all_pinned);
Nick Piggine17224b2005-09-10 00:26:18 -07004021 double_rq_unlock(this_rq, busiest);
Christoph Lameterfe2eea32006-12-10 02:20:21 -08004022 local_irq_restore(flags);
Nick Piggin81026792005-06-25 14:57:07 -07004023
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07004024 /*
4025 * some other cpu did the load balance for us.
4026 */
Peter Williams43010652007-08-09 11:16:46 +02004027 if (ld_moved && this_cpu != smp_processor_id())
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07004028 resched_cpu(this_cpu);
4029
Nick Piggin81026792005-06-25 14:57:07 -07004030 /* All tasks on this runqueue were pinned by CPU affinity */
Christoph Lameter0a2966b2006-09-25 23:30:51 -07004031 if (unlikely(all_pinned)) {
Rusty Russell96f874e2008-11-25 02:35:14 +10304032 cpumask_clear_cpu(cpu_of(busiest), cpus);
4033 if (!cpumask_empty(cpus))
Christoph Lameter0a2966b2006-09-25 23:30:51 -07004034 goto redo;
Nick Piggin81026792005-06-25 14:57:07 -07004035 goto out_balanced;
Christoph Lameter0a2966b2006-09-25 23:30:51 -07004036 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004037 }
Nick Piggin81026792005-06-25 14:57:07 -07004038
Peter Williams43010652007-08-09 11:16:46 +02004039 if (!ld_moved) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004040 schedstat_inc(sd, lb_failed[idle]);
4041 sd->nr_balance_failed++;
4042
4043 if (unlikely(sd->nr_balance_failed > sd->cache_nice_tries+2)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004044
Christoph Lameterfe2eea32006-12-10 02:20:21 -08004045 spin_lock_irqsave(&busiest->lock, flags);
Siddha, Suresh Bfa3b6dd2005-09-10 00:26:21 -07004046
4047 /* don't kick the migration_thread, if the curr
4048 * task on busiest cpu can't be moved to this_cpu
4049 */
Rusty Russell96f874e2008-11-25 02:35:14 +10304050 if (!cpumask_test_cpu(this_cpu,
4051 &busiest->curr->cpus_allowed)) {
Christoph Lameterfe2eea32006-12-10 02:20:21 -08004052 spin_unlock_irqrestore(&busiest->lock, flags);
Siddha, Suresh Bfa3b6dd2005-09-10 00:26:21 -07004053 all_pinned = 1;
4054 goto out_one_pinned;
4055 }
4056
Linus Torvalds1da177e2005-04-16 15:20:36 -07004057 if (!busiest->active_balance) {
4058 busiest->active_balance = 1;
4059 busiest->push_cpu = this_cpu;
Nick Piggin81026792005-06-25 14:57:07 -07004060 active_balance = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004061 }
Christoph Lameterfe2eea32006-12-10 02:20:21 -08004062 spin_unlock_irqrestore(&busiest->lock, flags);
Nick Piggin81026792005-06-25 14:57:07 -07004063 if (active_balance)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004064 wake_up_process(busiest->migration_thread);
4065
4066 /*
4067 * We've kicked active balancing, reset the failure
4068 * counter.
4069 */
Nick Piggin39507452005-06-25 14:57:09 -07004070 sd->nr_balance_failed = sd->cache_nice_tries+1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004071 }
Nick Piggin81026792005-06-25 14:57:07 -07004072 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07004073 sd->nr_balance_failed = 0;
4074
Nick Piggin81026792005-06-25 14:57:07 -07004075 if (likely(!active_balance)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004076 /* We were unbalanced, so reset the balancing interval */
4077 sd->balance_interval = sd->min_interval;
Nick Piggin81026792005-06-25 14:57:07 -07004078 } else {
4079 /*
4080 * If we've begun active balancing, start to back off. This
4081 * case may not be covered by the all_pinned logic if there
4082 * is only 1 task on the busy runqueue (because we don't call
4083 * move_tasks).
4084 */
4085 if (sd->balance_interval < sd->max_interval)
4086 sd->balance_interval *= 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004087 }
4088
Peter Williams43010652007-08-09 11:16:46 +02004089 if (!ld_moved && !sd_idle && sd->flags & SD_SHARE_CPUPOWER &&
Siddha, Suresh B89c47102006-10-03 01:14:09 -07004090 !test_sd_parent(sd, SD_POWERSAVINGS_BALANCE))
Peter Zijlstrac09595f2008-06-27 13:41:14 +02004091 ld_moved = -1;
4092
4093 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004094
4095out_balanced:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004096 schedstat_inc(sd, lb_balanced[idle]);
4097
Nick Piggin16cfb1c2005-06-25 14:57:08 -07004098 sd->nr_balance_failed = 0;
Siddha, Suresh Bfa3b6dd2005-09-10 00:26:21 -07004099
4100out_one_pinned:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004101 /* tune up the balancing interval */
Nick Piggin77391d72005-06-25 14:57:30 -07004102 if ((all_pinned && sd->balance_interval < MAX_PINNED_INTERVAL) ||
4103 (sd->balance_interval < sd->max_interval))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004104 sd->balance_interval *= 2;
4105
Ingo Molnar48f24c42006-07-03 00:25:40 -07004106 if (!sd_idle && sd->flags & SD_SHARE_CPUPOWER &&
Siddha, Suresh B89c47102006-10-03 01:14:09 -07004107 !test_sd_parent(sd, SD_POWERSAVINGS_BALANCE))
Peter Zijlstrac09595f2008-06-27 13:41:14 +02004108 ld_moved = -1;
4109 else
4110 ld_moved = 0;
4111out:
Peter Zijlstrac8cba852008-06-27 13:41:23 +02004112 if (ld_moved)
4113 update_shares(sd);
Peter Zijlstrac09595f2008-06-27 13:41:14 +02004114 return ld_moved;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004115}
4116
4117/*
4118 * Check this_cpu to ensure it is balanced within domain. Attempt to move
4119 * tasks if there is an imbalance.
4120 *
Ingo Molnard15bcfd2007-07-09 18:51:57 +02004121 * Called from schedule when this_rq is about to become idle (CPU_NEWLY_IDLE).
Linus Torvalds1da177e2005-04-16 15:20:36 -07004122 * this_rq is locked.
4123 */
Ingo Molnar48f24c42006-07-03 00:25:40 -07004124static int
Rusty Russelldf7c8e82009-03-19 15:22:20 +10304125load_balance_newidle(int this_cpu, struct rq *this_rq, struct sched_domain *sd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004126{
4127 struct sched_group *group;
Ingo Molnar70b97a72006-07-03 00:25:42 -07004128 struct rq *busiest = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004129 unsigned long imbalance;
Peter Williams43010652007-08-09 11:16:46 +02004130 int ld_moved = 0;
Nick Piggin5969fe02005-09-10 00:26:19 -07004131 int sd_idle = 0;
Suresh Siddha969bb4e2007-07-19 21:28:35 +02004132 int all_pinned = 0;
Rusty Russelldf7c8e82009-03-19 15:22:20 +10304133 struct cpumask *cpus = __get_cpu_var(load_balance_tmpmask);
Mike Travis7c16ec52008-04-04 18:11:11 -07004134
Rusty Russell96f874e2008-11-25 02:35:14 +10304135 cpumask_setall(cpus);
Nick Piggin5969fe02005-09-10 00:26:19 -07004136
Siddha, Suresh B89c47102006-10-03 01:14:09 -07004137 /*
4138 * When power savings policy is enabled for the parent domain, idle
4139 * sibling can pick up load irrespective of busy siblings. In this case,
4140 * let the state of idle sibling percolate up as IDLE, instead of
Ingo Molnard15bcfd2007-07-09 18:51:57 +02004141 * portraying it as CPU_NOT_IDLE.
Siddha, Suresh B89c47102006-10-03 01:14:09 -07004142 */
4143 if (sd->flags & SD_SHARE_CPUPOWER &&
4144 !test_sd_parent(sd, SD_POWERSAVINGS_BALANCE))
Nick Piggin5969fe02005-09-10 00:26:19 -07004145 sd_idle = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004146
Ingo Molnar2d723762007-10-15 17:00:12 +02004147 schedstat_inc(sd, lb_count[CPU_NEWLY_IDLE]);
Christoph Lameter0a2966b2006-09-25 23:30:51 -07004148redo:
Peter Zijlstra3e5459b2008-06-27 13:41:24 +02004149 update_shares_locked(this_rq, sd);
Ingo Molnard15bcfd2007-07-09 18:51:57 +02004150 group = find_busiest_group(sd, this_cpu, &imbalance, CPU_NEWLY_IDLE,
Mike Travis7c16ec52008-04-04 18:11:11 -07004151 &sd_idle, cpus, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004152 if (!group) {
Ingo Molnard15bcfd2007-07-09 18:51:57 +02004153 schedstat_inc(sd, lb_nobusyg[CPU_NEWLY_IDLE]);
Nick Piggin16cfb1c2005-06-25 14:57:08 -07004154 goto out_balanced;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004155 }
4156
Mike Travis7c16ec52008-04-04 18:11:11 -07004157 busiest = find_busiest_queue(group, CPU_NEWLY_IDLE, imbalance, cpus);
Nick Piggindb935db2005-06-25 14:57:11 -07004158 if (!busiest) {
Ingo Molnard15bcfd2007-07-09 18:51:57 +02004159 schedstat_inc(sd, lb_nobusyq[CPU_NEWLY_IDLE]);
Nick Piggin16cfb1c2005-06-25 14:57:08 -07004160 goto out_balanced;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004161 }
4162
Nick Piggindb935db2005-06-25 14:57:11 -07004163 BUG_ON(busiest == this_rq);
4164
Ingo Molnard15bcfd2007-07-09 18:51:57 +02004165 schedstat_add(sd, lb_imbalance[CPU_NEWLY_IDLE], imbalance);
Nick Piggind6d5cfa2005-09-10 00:26:16 -07004166
Peter Williams43010652007-08-09 11:16:46 +02004167 ld_moved = 0;
Nick Piggind6d5cfa2005-09-10 00:26:16 -07004168 if (busiest->nr_running > 1) {
4169 /* Attempt to move tasks */
4170 double_lock_balance(this_rq, busiest);
Ingo Molnar6e82a3b2007-08-09 11:16:51 +02004171 /* this_rq->clock is already updated */
4172 update_rq_clock(busiest);
Peter Williams43010652007-08-09 11:16:46 +02004173 ld_moved = move_tasks(this_rq, this_cpu, busiest,
Suresh Siddha969bb4e2007-07-19 21:28:35 +02004174 imbalance, sd, CPU_NEWLY_IDLE,
4175 &all_pinned);
Peter Zijlstra1b12bbc2008-08-11 09:30:22 +02004176 double_unlock_balance(this_rq, busiest);
Christoph Lameter0a2966b2006-09-25 23:30:51 -07004177
Suresh Siddha969bb4e2007-07-19 21:28:35 +02004178 if (unlikely(all_pinned)) {
Rusty Russell96f874e2008-11-25 02:35:14 +10304179 cpumask_clear_cpu(cpu_of(busiest), cpus);
4180 if (!cpumask_empty(cpus))
Christoph Lameter0a2966b2006-09-25 23:30:51 -07004181 goto redo;
4182 }
Nick Piggind6d5cfa2005-09-10 00:26:16 -07004183 }
4184
Peter Williams43010652007-08-09 11:16:46 +02004185 if (!ld_moved) {
Vaidyanathan Srinivasan36dffab2008-12-20 10:06:38 +05304186 int active_balance = 0;
Vaidyanathan Srinivasanad273b32008-12-18 23:26:36 +05304187
Ingo Molnard15bcfd2007-07-09 18:51:57 +02004188 schedstat_inc(sd, lb_failed[CPU_NEWLY_IDLE]);
Siddha, Suresh B89c47102006-10-03 01:14:09 -07004189 if (!sd_idle && sd->flags & SD_SHARE_CPUPOWER &&
4190 !test_sd_parent(sd, SD_POWERSAVINGS_BALANCE))
Nick Piggin5969fe02005-09-10 00:26:19 -07004191 return -1;
Vaidyanathan Srinivasanad273b32008-12-18 23:26:36 +05304192
4193 if (sched_mc_power_savings < POWERSAVINGS_BALANCE_WAKEUP)
4194 return -1;
4195
4196 if (sd->nr_balance_failed++ < 2)
4197 return -1;
4198
4199 /*
4200 * The only task running in a non-idle cpu can be moved to this
4201 * cpu in an attempt to completely freeup the other CPU
4202 * package. The same method used to move task in load_balance()
4203 * have been extended for load_balance_newidle() to speedup
4204 * consolidation at sched_mc=POWERSAVINGS_BALANCE_WAKEUP (2)
4205 *
4206 * The package power saving logic comes from
4207 * find_busiest_group(). If there are no imbalance, then
4208 * f_b_g() will return NULL. However when sched_mc={1,2} then
4209 * f_b_g() will select a group from which a running task may be
4210 * pulled to this cpu in order to make the other package idle.
4211 * If there is no opportunity to make a package idle and if
4212 * there are no imbalance, then f_b_g() will return NULL and no
4213 * action will be taken in load_balance_newidle().
4214 *
4215 * Under normal task pull operation due to imbalance, there
4216 * will be more than one task in the source run queue and
4217 * move_tasks() will succeed. ld_moved will be true and this
4218 * active balance code will not be triggered.
4219 */
4220
4221 /* Lock busiest in correct order while this_rq is held */
4222 double_lock_balance(this_rq, busiest);
4223
4224 /*
4225 * don't kick the migration_thread, if the curr
4226 * task on busiest cpu can't be moved to this_cpu
4227 */
Mike Travis6ca09df2008-12-31 18:08:45 -08004228 if (!cpumask_test_cpu(this_cpu, &busiest->curr->cpus_allowed)) {
Vaidyanathan Srinivasanad273b32008-12-18 23:26:36 +05304229 double_unlock_balance(this_rq, busiest);
4230 all_pinned = 1;
4231 return ld_moved;
4232 }
4233
4234 if (!busiest->active_balance) {
4235 busiest->active_balance = 1;
4236 busiest->push_cpu = this_cpu;
4237 active_balance = 1;
4238 }
4239
4240 double_unlock_balance(this_rq, busiest);
Peter Zijlstrada8d5082009-01-07 15:28:57 +01004241 /*
4242 * Should not call ttwu while holding a rq->lock
4243 */
4244 spin_unlock(&this_rq->lock);
Vaidyanathan Srinivasanad273b32008-12-18 23:26:36 +05304245 if (active_balance)
4246 wake_up_process(busiest->migration_thread);
Peter Zijlstrada8d5082009-01-07 15:28:57 +01004247 spin_lock(&this_rq->lock);
Vaidyanathan Srinivasanad273b32008-12-18 23:26:36 +05304248
Nick Piggin5969fe02005-09-10 00:26:19 -07004249 } else
Nick Piggin16cfb1c2005-06-25 14:57:08 -07004250 sd->nr_balance_failed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004251
Peter Zijlstra3e5459b2008-06-27 13:41:24 +02004252 update_shares_locked(this_rq, sd);
Peter Williams43010652007-08-09 11:16:46 +02004253 return ld_moved;
Nick Piggin16cfb1c2005-06-25 14:57:08 -07004254
4255out_balanced:
Ingo Molnard15bcfd2007-07-09 18:51:57 +02004256 schedstat_inc(sd, lb_balanced[CPU_NEWLY_IDLE]);
Ingo Molnar48f24c42006-07-03 00:25:40 -07004257 if (!sd_idle && sd->flags & SD_SHARE_CPUPOWER &&
Siddha, Suresh B89c47102006-10-03 01:14:09 -07004258 !test_sd_parent(sd, SD_POWERSAVINGS_BALANCE))
Nick Piggin5969fe02005-09-10 00:26:19 -07004259 return -1;
Nick Piggin16cfb1c2005-06-25 14:57:08 -07004260 sd->nr_balance_failed = 0;
Ingo Molnar48f24c42006-07-03 00:25:40 -07004261
Nick Piggin16cfb1c2005-06-25 14:57:08 -07004262 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004263}
4264
4265/*
4266 * idle_balance is called by schedule() if this_cpu is about to become
4267 * idle. Attempts to pull tasks from other CPUs.
4268 */
Ingo Molnar70b97a72006-07-03 00:25:42 -07004269static void idle_balance(int this_cpu, struct rq *this_rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004270{
4271 struct sched_domain *sd;
Vaidyanathan Srinivasanefbe0272008-12-08 20:52:49 +05304272 int pulled_task = 0;
Ingo Molnardd41f592007-07-09 18:51:59 +02004273 unsigned long next_balance = jiffies + HZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004274
4275 for_each_domain(this_cpu, sd) {
Christoph Lameter92c4ca52007-06-23 17:16:33 -07004276 unsigned long interval;
4277
4278 if (!(sd->flags & SD_LOAD_BALANCE))
4279 continue;
4280
4281 if (sd->flags & SD_BALANCE_NEWIDLE)
Ingo Molnar48f24c42006-07-03 00:25:40 -07004282 /* If we've pulled tasks over stop searching: */
Mike Travis7c16ec52008-04-04 18:11:11 -07004283 pulled_task = load_balance_newidle(this_cpu, this_rq,
Rusty Russelldf7c8e82009-03-19 15:22:20 +10304284 sd);
Christoph Lameter92c4ca52007-06-23 17:16:33 -07004285
4286 interval = msecs_to_jiffies(sd->balance_interval);
4287 if (time_after(next_balance, sd->last_balance + interval))
4288 next_balance = sd->last_balance + interval;
4289 if (pulled_task)
4290 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004291 }
Ingo Molnardd41f592007-07-09 18:51:59 +02004292 if (pulled_task || time_after(jiffies, this_rq->next_balance)) {
Christoph Lameter1bd77f22006-12-10 02:20:27 -08004293 /*
4294 * We are going idle. next_balance may be set based on
4295 * a busy processor. So reset next_balance.
4296 */
4297 this_rq->next_balance = next_balance;
Ingo Molnardd41f592007-07-09 18:51:59 +02004298 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004299}
4300
4301/*
4302 * active_load_balance is run by migration threads. It pushes running tasks
4303 * off the busiest CPU onto idle CPUs. It requires at least 1 task to be
4304 * running on each physical CPU where possible, and avoids physical /
4305 * logical imbalances.
4306 *
4307 * Called with busiest_rq locked.
4308 */
Ingo Molnar70b97a72006-07-03 00:25:42 -07004309static void active_load_balance(struct rq *busiest_rq, int busiest_cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004310{
Nick Piggin39507452005-06-25 14:57:09 -07004311 int target_cpu = busiest_rq->push_cpu;
Ingo Molnar70b97a72006-07-03 00:25:42 -07004312 struct sched_domain *sd;
4313 struct rq *target_rq;
Nick Piggin39507452005-06-25 14:57:09 -07004314
Ingo Molnar48f24c42006-07-03 00:25:40 -07004315 /* Is there any task to move? */
Nick Piggin39507452005-06-25 14:57:09 -07004316 if (busiest_rq->nr_running <= 1)
Nick Piggin39507452005-06-25 14:57:09 -07004317 return;
4318
4319 target_rq = cpu_rq(target_cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004320
4321 /*
Nick Piggin39507452005-06-25 14:57:09 -07004322 * This condition is "impossible", if it occurs
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01004323 * we need to fix it. Originally reported by
Nick Piggin39507452005-06-25 14:57:09 -07004324 * Bjorn Helgaas on a 128-cpu setup.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004325 */
Nick Piggin39507452005-06-25 14:57:09 -07004326 BUG_ON(busiest_rq == target_rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004327
Nick Piggin39507452005-06-25 14:57:09 -07004328 /* move a task from busiest_rq to target_rq */
4329 double_lock_balance(busiest_rq, target_rq);
Ingo Molnar6e82a3b2007-08-09 11:16:51 +02004330 update_rq_clock(busiest_rq);
4331 update_rq_clock(target_rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004332
Nick Piggin39507452005-06-25 14:57:09 -07004333 /* Search for an sd spanning us and the target CPU. */
Chen, Kenneth Wc96d1452006-06-27 02:54:28 -07004334 for_each_domain(target_cpu, sd) {
Nick Piggin39507452005-06-25 14:57:09 -07004335 if ((sd->flags & SD_LOAD_BALANCE) &&
Rusty Russell758b2cd2008-11-25 02:35:04 +10304336 cpumask_test_cpu(busiest_cpu, sched_domain_span(sd)))
Nick Piggin39507452005-06-25 14:57:09 -07004337 break;
Chen, Kenneth Wc96d1452006-06-27 02:54:28 -07004338 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004339
Ingo Molnar48f24c42006-07-03 00:25:40 -07004340 if (likely(sd)) {
Ingo Molnar2d723762007-10-15 17:00:12 +02004341 schedstat_inc(sd, alb_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004342
Peter Williams43010652007-08-09 11:16:46 +02004343 if (move_one_task(target_rq, target_cpu, busiest_rq,
4344 sd, CPU_IDLE))
Ingo Molnar48f24c42006-07-03 00:25:40 -07004345 schedstat_inc(sd, alb_pushed);
4346 else
4347 schedstat_inc(sd, alb_failed);
4348 }
Peter Zijlstra1b12bbc2008-08-11 09:30:22 +02004349 double_unlock_balance(busiest_rq, target_rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004350}
4351
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07004352#ifdef CONFIG_NO_HZ
4353static struct {
4354 atomic_t load_balancer;
Rusty Russell7d1e6a92008-11-25 02:35:09 +10304355 cpumask_var_t cpu_mask;
Gautham R Shenoyf711f602009-04-14 10:25:30 +05304356 cpumask_var_t ilb_grp_nohz_mask;
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07004357} nohz ____cacheline_aligned = {
4358 .load_balancer = ATOMIC_INIT(-1),
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07004359};
4360
Gautham R Shenoyf711f602009-04-14 10:25:30 +05304361#if defined(CONFIG_SCHED_MC) || defined(CONFIG_SCHED_SMT)
4362/**
4363 * lowest_flag_domain - Return lowest sched_domain containing flag.
4364 * @cpu: The cpu whose lowest level of sched domain is to
4365 * be returned.
4366 * @flag: The flag to check for the lowest sched_domain
4367 * for the given cpu.
4368 *
4369 * Returns the lowest sched_domain of a cpu which contains the given flag.
4370 */
4371static inline struct sched_domain *lowest_flag_domain(int cpu, int flag)
4372{
4373 struct sched_domain *sd;
4374
4375 for_each_domain(cpu, sd)
4376 if (sd && (sd->flags & flag))
4377 break;
4378
4379 return sd;
4380}
4381
4382/**
4383 * for_each_flag_domain - Iterates over sched_domains containing the flag.
4384 * @cpu: The cpu whose domains we're iterating over.
4385 * @sd: variable holding the value of the power_savings_sd
4386 * for cpu.
4387 * @flag: The flag to filter the sched_domains to be iterated.
4388 *
4389 * Iterates over all the scheduler domains for a given cpu that has the 'flag'
4390 * set, starting from the lowest sched_domain to the highest.
4391 */
4392#define for_each_flag_domain(cpu, sd, flag) \
4393 for (sd = lowest_flag_domain(cpu, flag); \
4394 (sd && (sd->flags & flag)); sd = sd->parent)
4395
4396/**
4397 * is_semi_idle_group - Checks if the given sched_group is semi-idle.
4398 * @ilb_group: group to be checked for semi-idleness
4399 *
4400 * Returns: 1 if the group is semi-idle. 0 otherwise.
4401 *
4402 * We define a sched_group to be semi idle if it has atleast one idle-CPU
4403 * and atleast one non-idle CPU. This helper function checks if the given
4404 * sched_group is semi-idle or not.
4405 */
4406static inline int is_semi_idle_group(struct sched_group *ilb_group)
4407{
4408 cpumask_and(nohz.ilb_grp_nohz_mask, nohz.cpu_mask,
4409 sched_group_cpus(ilb_group));
4410
4411 /*
4412 * A sched_group is semi-idle when it has atleast one busy cpu
4413 * and atleast one idle cpu.
4414 */
4415 if (cpumask_empty(nohz.ilb_grp_nohz_mask))
4416 return 0;
4417
4418 if (cpumask_equal(nohz.ilb_grp_nohz_mask, sched_group_cpus(ilb_group)))
4419 return 0;
4420
4421 return 1;
4422}
4423/**
4424 * find_new_ilb - Finds the optimum idle load balancer for nomination.
4425 * @cpu: The cpu which is nominating a new idle_load_balancer.
4426 *
4427 * Returns: Returns the id of the idle load balancer if it exists,
4428 * Else, returns >= nr_cpu_ids.
4429 *
4430 * This algorithm picks the idle load balancer such that it belongs to a
4431 * semi-idle powersavings sched_domain. The idea is to try and avoid
4432 * completely idle packages/cores just for the purpose of idle load balancing
4433 * when there are other idle cpu's which are better suited for that job.
4434 */
4435static int find_new_ilb(int cpu)
4436{
4437 struct sched_domain *sd;
4438 struct sched_group *ilb_group;
4439
4440 /*
4441 * Have idle load balancer selection from semi-idle packages only
4442 * when power-aware load balancing is enabled
4443 */
4444 if (!(sched_smt_power_savings || sched_mc_power_savings))
4445 goto out_done;
4446
4447 /*
4448 * Optimize for the case when we have no idle CPUs or only one
4449 * idle CPU. Don't walk the sched_domain hierarchy in such cases
4450 */
4451 if (cpumask_weight(nohz.cpu_mask) < 2)
4452 goto out_done;
4453
4454 for_each_flag_domain(cpu, sd, SD_POWERSAVINGS_BALANCE) {
4455 ilb_group = sd->groups;
4456
4457 do {
4458 if (is_semi_idle_group(ilb_group))
4459 return cpumask_first(nohz.ilb_grp_nohz_mask);
4460
4461 ilb_group = ilb_group->next;
4462
4463 } while (ilb_group != sd->groups);
4464 }
4465
4466out_done:
4467 return cpumask_first(nohz.cpu_mask);
4468}
4469#else /* (CONFIG_SCHED_MC || CONFIG_SCHED_SMT) */
4470static inline int find_new_ilb(int call_cpu)
4471{
Gautham R Shenoy6e29ec52009-04-21 08:40:49 +05304472 return cpumask_first(nohz.cpu_mask);
Gautham R Shenoyf711f602009-04-14 10:25:30 +05304473}
4474#endif
4475
Christoph Lameter7835b982006-12-10 02:20:22 -08004476/*
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07004477 * This routine will try to nominate the ilb (idle load balancing)
4478 * owner among the cpus whose ticks are stopped. ilb owner will do the idle
4479 * load balancing on behalf of all those cpus. If all the cpus in the system
4480 * go into this tickless mode, then there will be no ilb owner (as there is
4481 * no need for one) and all the cpus will sleep till the next wakeup event
4482 * arrives...
Christoph Lameter7835b982006-12-10 02:20:22 -08004483 *
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07004484 * For the ilb owner, tick is not stopped. And this tick will be used
4485 * for idle load balancing. ilb owner will still be part of
4486 * nohz.cpu_mask..
4487 *
4488 * While stopping the tick, this cpu will become the ilb owner if there
4489 * is no other owner. And will be the owner till that cpu becomes busy
4490 * or if all cpus in the system stop their ticks at which point
4491 * there is no need for ilb owner.
4492 *
4493 * When the ilb owner becomes busy, it nominates another owner, during the
4494 * next busy scheduler_tick()
4495 */
4496int select_nohz_load_balancer(int stop_tick)
4497{
4498 int cpu = smp_processor_id();
4499
4500 if (stop_tick) {
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07004501 cpu_rq(cpu)->in_nohz_recently = 1;
4502
Suresh Siddha483b4ee2009-02-04 11:59:44 -08004503 if (!cpu_active(cpu)) {
4504 if (atomic_read(&nohz.load_balancer) != cpu)
4505 return 0;
4506
4507 /*
4508 * If we are going offline and still the leader,
4509 * give up!
4510 */
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07004511 if (atomic_cmpxchg(&nohz.load_balancer, cpu, -1) != cpu)
4512 BUG();
Suresh Siddha483b4ee2009-02-04 11:59:44 -08004513
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07004514 return 0;
4515 }
4516
Suresh Siddha483b4ee2009-02-04 11:59:44 -08004517 cpumask_set_cpu(cpu, nohz.cpu_mask);
4518
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07004519 /* time for ilb owner also to sleep */
Rusty Russell7d1e6a92008-11-25 02:35:09 +10304520 if (cpumask_weight(nohz.cpu_mask) == num_online_cpus()) {
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07004521 if (atomic_read(&nohz.load_balancer) == cpu)
4522 atomic_set(&nohz.load_balancer, -1);
4523 return 0;
4524 }
4525
4526 if (atomic_read(&nohz.load_balancer) == -1) {
4527 /* make me the ilb owner */
4528 if (atomic_cmpxchg(&nohz.load_balancer, -1, cpu) == -1)
4529 return 1;
Gautham R Shenoye790fb02009-04-14 10:25:35 +05304530 } else if (atomic_read(&nohz.load_balancer) == cpu) {
4531 int new_ilb;
4532
4533 if (!(sched_smt_power_savings ||
4534 sched_mc_power_savings))
4535 return 1;
4536 /*
4537 * Check to see if there is a more power-efficient
4538 * ilb.
4539 */
4540 new_ilb = find_new_ilb(cpu);
4541 if (new_ilb < nr_cpu_ids && new_ilb != cpu) {
4542 atomic_set(&nohz.load_balancer, -1);
4543 resched_cpu(new_ilb);
4544 return 0;
4545 }
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07004546 return 1;
Gautham R Shenoye790fb02009-04-14 10:25:35 +05304547 }
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07004548 } else {
Rusty Russell7d1e6a92008-11-25 02:35:09 +10304549 if (!cpumask_test_cpu(cpu, nohz.cpu_mask))
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07004550 return 0;
4551
Rusty Russell7d1e6a92008-11-25 02:35:09 +10304552 cpumask_clear_cpu(cpu, nohz.cpu_mask);
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07004553
4554 if (atomic_read(&nohz.load_balancer) == cpu)
4555 if (atomic_cmpxchg(&nohz.load_balancer, cpu, -1) != cpu)
4556 BUG();
4557 }
4558 return 0;
4559}
4560#endif
4561
4562static DEFINE_SPINLOCK(balancing);
4563
4564/*
Christoph Lameter7835b982006-12-10 02:20:22 -08004565 * It checks each scheduling domain to see if it is due to be balanced,
4566 * and initiates a balancing operation if so.
4567 *
4568 * Balancing parameters are set up in arch_init_sched_domains.
4569 */
Alexey Dobriyana9957442007-10-15 17:00:13 +02004570static void rebalance_domains(int cpu, enum cpu_idle_type idle)
Christoph Lameter7835b982006-12-10 02:20:22 -08004571{
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07004572 int balance = 1;
4573 struct rq *rq = cpu_rq(cpu);
Christoph Lameter7835b982006-12-10 02:20:22 -08004574 unsigned long interval;
4575 struct sched_domain *sd;
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07004576 /* Earliest time when we have to do rebalance again */
Christoph Lameterc9819f42006-12-10 02:20:25 -08004577 unsigned long next_balance = jiffies + 60*HZ;
Suresh Siddhaf549da82007-08-23 15:18:02 +02004578 int update_next_balance = 0;
Dmitry Adamushkod07355f2008-05-12 21:21:15 +02004579 int need_serialize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004580
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07004581 for_each_domain(cpu, sd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004582 if (!(sd->flags & SD_LOAD_BALANCE))
4583 continue;
4584
4585 interval = sd->balance_interval;
Ingo Molnard15bcfd2007-07-09 18:51:57 +02004586 if (idle != CPU_IDLE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004587 interval *= sd->busy_factor;
4588
4589 /* scale ms to jiffies */
4590 interval = msecs_to_jiffies(interval);
4591 if (unlikely(!interval))
4592 interval = 1;
Ingo Molnardd41f592007-07-09 18:51:59 +02004593 if (interval > HZ*NR_CPUS/10)
4594 interval = HZ*NR_CPUS/10;
4595
Dmitry Adamushkod07355f2008-05-12 21:21:15 +02004596 need_serialize = sd->flags & SD_SERIALIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004597
Dmitry Adamushkod07355f2008-05-12 21:21:15 +02004598 if (need_serialize) {
Christoph Lameter08c183f2006-12-10 02:20:29 -08004599 if (!spin_trylock(&balancing))
4600 goto out;
4601 }
4602
Christoph Lameterc9819f42006-12-10 02:20:25 -08004603 if (time_after_eq(jiffies, sd->last_balance + interval)) {
Rusty Russelldf7c8e82009-03-19 15:22:20 +10304604 if (load_balance(cpu, rq, sd, idle, &balance)) {
Siddha, Suresh Bfa3b6dd2005-09-10 00:26:21 -07004605 /*
4606 * We've pulled tasks over so either we're no
Nick Piggin5969fe02005-09-10 00:26:19 -07004607 * longer idle, or one of our SMT siblings is
4608 * not idle.
4609 */
Ingo Molnard15bcfd2007-07-09 18:51:57 +02004610 idle = CPU_NOT_IDLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004611 }
Christoph Lameter1bd77f22006-12-10 02:20:27 -08004612 sd->last_balance = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004613 }
Dmitry Adamushkod07355f2008-05-12 21:21:15 +02004614 if (need_serialize)
Christoph Lameter08c183f2006-12-10 02:20:29 -08004615 spin_unlock(&balancing);
4616out:
Suresh Siddhaf549da82007-08-23 15:18:02 +02004617 if (time_after(next_balance, sd->last_balance + interval)) {
Christoph Lameterc9819f42006-12-10 02:20:25 -08004618 next_balance = sd->last_balance + interval;
Suresh Siddhaf549da82007-08-23 15:18:02 +02004619 update_next_balance = 1;
4620 }
Siddha, Suresh B783609c2006-12-10 02:20:33 -08004621
4622 /*
4623 * Stop the load balance at this level. There is another
4624 * CPU in our sched group which is doing load balancing more
4625 * actively.
4626 */
4627 if (!balance)
4628 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004629 }
Suresh Siddhaf549da82007-08-23 15:18:02 +02004630
4631 /*
4632 * next_balance will be updated only when there is a need.
4633 * When the cpu is attached to null domain for ex, it will not be
4634 * updated.
4635 */
4636 if (likely(update_next_balance))
4637 rq->next_balance = next_balance;
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07004638}
4639
4640/*
4641 * run_rebalance_domains is triggered when needed from the scheduler tick.
4642 * In CONFIG_NO_HZ case, the idle load balance owner will do the
4643 * rebalancing for all the cpus for whom scheduler ticks are stopped.
4644 */
4645static void run_rebalance_domains(struct softirq_action *h)
4646{
Ingo Molnardd41f592007-07-09 18:51:59 +02004647 int this_cpu = smp_processor_id();
4648 struct rq *this_rq = cpu_rq(this_cpu);
4649 enum cpu_idle_type idle = this_rq->idle_at_tick ?
4650 CPU_IDLE : CPU_NOT_IDLE;
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07004651
Ingo Molnardd41f592007-07-09 18:51:59 +02004652 rebalance_domains(this_cpu, idle);
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07004653
4654#ifdef CONFIG_NO_HZ
4655 /*
4656 * If this cpu is the owner for idle load balancing, then do the
4657 * balancing on behalf of the other idle cpus whose ticks are
4658 * stopped.
4659 */
Ingo Molnardd41f592007-07-09 18:51:59 +02004660 if (this_rq->idle_at_tick &&
4661 atomic_read(&nohz.load_balancer) == this_cpu) {
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07004662 struct rq *rq;
4663 int balance_cpu;
4664
Rusty Russell7d1e6a92008-11-25 02:35:09 +10304665 for_each_cpu(balance_cpu, nohz.cpu_mask) {
4666 if (balance_cpu == this_cpu)
4667 continue;
4668
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07004669 /*
4670 * If this cpu gets work to do, stop the load balancing
4671 * work being done for other cpus. Next load
4672 * balancing owner will pick it up.
4673 */
4674 if (need_resched())
4675 break;
4676
Oleg Nesterovde0cf892007-08-12 18:08:19 +02004677 rebalance_domains(balance_cpu, CPU_IDLE);
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07004678
4679 rq = cpu_rq(balance_cpu);
Ingo Molnardd41f592007-07-09 18:51:59 +02004680 if (time_after(this_rq->next_balance, rq->next_balance))
4681 this_rq->next_balance = rq->next_balance;
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07004682 }
4683 }
4684#endif
4685}
4686
Frederic Weisbecker8a0be9e2009-03-05 01:27:02 +01004687static inline int on_null_domain(int cpu)
4688{
4689 return !rcu_dereference(cpu_rq(cpu)->sd);
4690}
4691
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07004692/*
4693 * Trigger the SCHED_SOFTIRQ if it is time to do periodic load balancing.
4694 *
4695 * In case of CONFIG_NO_HZ, this is the place where we nominate a new
4696 * idle load balancing owner or decide to stop the periodic load balancing,
4697 * if the whole system is idle.
4698 */
Ingo Molnardd41f592007-07-09 18:51:59 +02004699static inline void trigger_load_balance(struct rq *rq, int cpu)
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07004700{
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07004701#ifdef CONFIG_NO_HZ
4702 /*
4703 * If we were in the nohz mode recently and busy at the current
4704 * scheduler tick, then check if we need to nominate new idle
4705 * load balancer.
4706 */
4707 if (rq->in_nohz_recently && !rq->idle_at_tick) {
4708 rq->in_nohz_recently = 0;
4709
4710 if (atomic_read(&nohz.load_balancer) == cpu) {
Rusty Russell7d1e6a92008-11-25 02:35:09 +10304711 cpumask_clear_cpu(cpu, nohz.cpu_mask);
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07004712 atomic_set(&nohz.load_balancer, -1);
4713 }
4714
4715 if (atomic_read(&nohz.load_balancer) == -1) {
Gautham R Shenoyf711f602009-04-14 10:25:30 +05304716 int ilb = find_new_ilb(cpu);
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07004717
Mike Travis434d53b2008-04-04 18:11:04 -07004718 if (ilb < nr_cpu_ids)
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07004719 resched_cpu(ilb);
4720 }
4721 }
4722
4723 /*
4724 * If this cpu is idle and doing idle load balancing for all the
4725 * cpus with ticks stopped, is it time for that to stop?
4726 */
4727 if (rq->idle_at_tick && atomic_read(&nohz.load_balancer) == cpu &&
Rusty Russell7d1e6a92008-11-25 02:35:09 +10304728 cpumask_weight(nohz.cpu_mask) == num_online_cpus()) {
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07004729 resched_cpu(cpu);
4730 return;
4731 }
4732
4733 /*
4734 * If this cpu is idle and the idle load balancing is done by
4735 * someone else, then no need raise the SCHED_SOFTIRQ
4736 */
4737 if (rq->idle_at_tick && atomic_read(&nohz.load_balancer) != cpu &&
Rusty Russell7d1e6a92008-11-25 02:35:09 +10304738 cpumask_test_cpu(cpu, nohz.cpu_mask))
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07004739 return;
4740#endif
Frederic Weisbecker8a0be9e2009-03-05 01:27:02 +01004741 /* Don't need to rebalance while attached to NULL domain */
4742 if (time_after_eq(jiffies, rq->next_balance) &&
4743 likely(!on_null_domain(cpu)))
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07004744 raise_softirq(SCHED_SOFTIRQ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004745}
Ingo Molnardd41f592007-07-09 18:51:59 +02004746
4747#else /* CONFIG_SMP */
4748
Linus Torvalds1da177e2005-04-16 15:20:36 -07004749/*
4750 * on UP we do not need to balance between CPUs:
4751 */
Ingo Molnar70b97a72006-07-03 00:25:42 -07004752static inline void idle_balance(int cpu, struct rq *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004753{
4754}
Ingo Molnardd41f592007-07-09 18:51:59 +02004755
Linus Torvalds1da177e2005-04-16 15:20:36 -07004756#endif
4757
Linus Torvalds1da177e2005-04-16 15:20:36 -07004758DEFINE_PER_CPU(struct kernel_stat, kstat);
4759
4760EXPORT_PER_CPU_SYMBOL(kstat);
4761
4762/*
Hidetoshi Setoc5f8d992009-03-31 16:56:03 +09004763 * Return any ns on the sched_clock that have not yet been accounted in
Frank Mayharf06febc2008-09-12 09:54:39 -07004764 * @p in case that task is currently running.
Hidetoshi Setoc5f8d992009-03-31 16:56:03 +09004765 *
4766 * Called with task_rq_lock() held on @rq.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004767 */
Hidetoshi Setoc5f8d992009-03-31 16:56:03 +09004768static u64 do_task_delta_exec(struct task_struct *p, struct rq *rq)
4769{
4770 u64 ns = 0;
4771
4772 if (task_current(rq, p)) {
4773 update_rq_clock(rq);
4774 ns = rq->clock - p->se.exec_start;
4775 if ((s64)ns < 0)
4776 ns = 0;
4777 }
4778
4779 return ns;
4780}
4781
Frank Mayharbb34d922008-09-12 09:54:39 -07004782unsigned long long task_delta_exec(struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004783{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004784 unsigned long flags;
Ingo Molnar41b86e92007-07-09 18:51:58 +02004785 struct rq *rq;
Frank Mayharbb34d922008-09-12 09:54:39 -07004786 u64 ns = 0;
Ingo Molnar48f24c42006-07-03 00:25:40 -07004787
Ingo Molnar41b86e92007-07-09 18:51:58 +02004788 rq = task_rq_lock(p, &flags);
Hidetoshi Setoc5f8d992009-03-31 16:56:03 +09004789 ns = do_task_delta_exec(p, rq);
4790 task_rq_unlock(rq, &flags);
Ingo Molnar15084872008-09-30 08:28:17 +02004791
Hidetoshi Setoc5f8d992009-03-31 16:56:03 +09004792 return ns;
4793}
Frank Mayharf06febc2008-09-12 09:54:39 -07004794
Hidetoshi Setoc5f8d992009-03-31 16:56:03 +09004795/*
4796 * Return accounted runtime for the task.
4797 * In case the task is currently running, return the runtime plus current's
4798 * pending runtime that have not been accounted yet.
4799 */
4800unsigned long long task_sched_runtime(struct task_struct *p)
4801{
4802 unsigned long flags;
4803 struct rq *rq;
4804 u64 ns = 0;
Ingo Molnar48f24c42006-07-03 00:25:40 -07004805
Hidetoshi Setoc5f8d992009-03-31 16:56:03 +09004806 rq = task_rq_lock(p, &flags);
4807 ns = p->se.sum_exec_runtime + do_task_delta_exec(p, rq);
4808 task_rq_unlock(rq, &flags);
4809
4810 return ns;
4811}
4812
4813/*
4814 * Return sum_exec_runtime for the thread group.
4815 * In case the task is currently running, return the sum plus current's
4816 * pending runtime that have not been accounted yet.
4817 *
4818 * Note that the thread group might have other running tasks as well,
4819 * so the return value not includes other pending runtime that other
4820 * running tasks might have.
4821 */
4822unsigned long long thread_group_sched_runtime(struct task_struct *p)
4823{
4824 struct task_cputime totals;
4825 unsigned long flags;
4826 struct rq *rq;
4827 u64 ns;
4828
4829 rq = task_rq_lock(p, &flags);
4830 thread_group_cputime(p, &totals);
4831 ns = totals.sum_exec_runtime + do_task_delta_exec(p, rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004832 task_rq_unlock(rq, &flags);
4833
4834 return ns;
4835}
4836
4837/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07004838 * Account user cpu time to a process.
4839 * @p: the process that the cpu time gets accounted to
Linus Torvalds1da177e2005-04-16 15:20:36 -07004840 * @cputime: the cpu time spent in user space since the last update
Martin Schwidefsky457533a2008-12-31 15:11:37 +01004841 * @cputime_scaled: cputime scaled by cpu frequency
Linus Torvalds1da177e2005-04-16 15:20:36 -07004842 */
Martin Schwidefsky457533a2008-12-31 15:11:37 +01004843void account_user_time(struct task_struct *p, cputime_t cputime,
4844 cputime_t cputime_scaled)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004845{
4846 struct cpu_usage_stat *cpustat = &kstat_this_cpu.cpustat;
4847 cputime64_t tmp;
4848
Martin Schwidefsky457533a2008-12-31 15:11:37 +01004849 /* Add user time to process. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004850 p->utime = cputime_add(p->utime, cputime);
Martin Schwidefsky457533a2008-12-31 15:11:37 +01004851 p->utimescaled = cputime_add(p->utimescaled, cputime_scaled);
Frank Mayharf06febc2008-09-12 09:54:39 -07004852 account_group_user_time(p, cputime);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004853
4854 /* Add user time to cpustat. */
4855 tmp = cputime_to_cputime64(cputime);
4856 if (TASK_NICE(p) > 0)
4857 cpustat->nice = cputime64_add(cpustat->nice, tmp);
4858 else
4859 cpustat->user = cputime64_add(cpustat->user, tmp);
Bharata B Raoef12fef2009-03-31 10:02:22 +05304860
4861 cpuacct_update_stats(p, CPUACCT_STAT_USER, cputime);
Jonathan Lim49b5cf32008-07-25 01:48:40 -07004862 /* Account for user time used */
4863 acct_update_integrals(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004864}
4865
4866/*
Laurent Vivier94886b82007-10-15 17:00:19 +02004867 * Account guest cpu time to a process.
4868 * @p: the process that the cpu time gets accounted to
4869 * @cputime: the cpu time spent in virtual machine since the last update
Martin Schwidefsky457533a2008-12-31 15:11:37 +01004870 * @cputime_scaled: cputime scaled by cpu frequency
Laurent Vivier94886b82007-10-15 17:00:19 +02004871 */
Martin Schwidefsky457533a2008-12-31 15:11:37 +01004872static void account_guest_time(struct task_struct *p, cputime_t cputime,
4873 cputime_t cputime_scaled)
Laurent Vivier94886b82007-10-15 17:00:19 +02004874{
4875 cputime64_t tmp;
4876 struct cpu_usage_stat *cpustat = &kstat_this_cpu.cpustat;
4877
4878 tmp = cputime_to_cputime64(cputime);
4879
Martin Schwidefsky457533a2008-12-31 15:11:37 +01004880 /* Add guest time to process. */
Laurent Vivier94886b82007-10-15 17:00:19 +02004881 p->utime = cputime_add(p->utime, cputime);
Martin Schwidefsky457533a2008-12-31 15:11:37 +01004882 p->utimescaled = cputime_add(p->utimescaled, cputime_scaled);
Frank Mayharf06febc2008-09-12 09:54:39 -07004883 account_group_user_time(p, cputime);
Laurent Vivier94886b82007-10-15 17:00:19 +02004884 p->gtime = cputime_add(p->gtime, cputime);
4885
Martin Schwidefsky457533a2008-12-31 15:11:37 +01004886 /* Add guest time to cpustat. */
Laurent Vivier94886b82007-10-15 17:00:19 +02004887 cpustat->user = cputime64_add(cpustat->user, tmp);
4888 cpustat->guest = cputime64_add(cpustat->guest, tmp);
4889}
4890
4891/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07004892 * Account system cpu time to a process.
4893 * @p: the process that the cpu time gets accounted to
4894 * @hardirq_offset: the offset to subtract from hardirq_count()
4895 * @cputime: the cpu time spent in kernel space since the last update
Martin Schwidefsky457533a2008-12-31 15:11:37 +01004896 * @cputime_scaled: cputime scaled by cpu frequency
Linus Torvalds1da177e2005-04-16 15:20:36 -07004897 */
4898void account_system_time(struct task_struct *p, int hardirq_offset,
Martin Schwidefsky457533a2008-12-31 15:11:37 +01004899 cputime_t cputime, cputime_t cputime_scaled)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004900{
4901 struct cpu_usage_stat *cpustat = &kstat_this_cpu.cpustat;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004902 cputime64_t tmp;
4903
Harvey Harrison983ed7a2008-04-24 18:17:55 -07004904 if ((p->flags & PF_VCPU) && (irq_count() - hardirq_offset == 0)) {
Martin Schwidefsky457533a2008-12-31 15:11:37 +01004905 account_guest_time(p, cputime, cputime_scaled);
Harvey Harrison983ed7a2008-04-24 18:17:55 -07004906 return;
4907 }
Laurent Vivier94886b82007-10-15 17:00:19 +02004908
Martin Schwidefsky457533a2008-12-31 15:11:37 +01004909 /* Add system time to process. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004910 p->stime = cputime_add(p->stime, cputime);
Martin Schwidefsky457533a2008-12-31 15:11:37 +01004911 p->stimescaled = cputime_add(p->stimescaled, cputime_scaled);
Frank Mayharf06febc2008-09-12 09:54:39 -07004912 account_group_system_time(p, cputime);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004913
4914 /* Add system time to cpustat. */
4915 tmp = cputime_to_cputime64(cputime);
4916 if (hardirq_count() - hardirq_offset)
4917 cpustat->irq = cputime64_add(cpustat->irq, tmp);
4918 else if (softirq_count())
4919 cpustat->softirq = cputime64_add(cpustat->softirq, tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004920 else
Martin Schwidefsky79741dd2008-12-31 15:11:38 +01004921 cpustat->system = cputime64_add(cpustat->system, tmp);
4922
Bharata B Raoef12fef2009-03-31 10:02:22 +05304923 cpuacct_update_stats(p, CPUACCT_STAT_SYSTEM, cputime);
4924
Linus Torvalds1da177e2005-04-16 15:20:36 -07004925 /* Account for system time used */
4926 acct_update_integrals(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004927}
4928
4929/*
4930 * Account for involuntary wait time.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004931 * @steal: the cpu time spent in involuntary wait
Linus Torvalds1da177e2005-04-16 15:20:36 -07004932 */
Martin Schwidefsky79741dd2008-12-31 15:11:38 +01004933void account_steal_time(cputime_t cputime)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004934{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004935 struct cpu_usage_stat *cpustat = &kstat_this_cpu.cpustat;
Martin Schwidefsky79741dd2008-12-31 15:11:38 +01004936 cputime64_t cputime64 = cputime_to_cputime64(cputime);
4937
4938 cpustat->steal = cputime64_add(cpustat->steal, cputime64);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004939}
4940
Christoph Lameter7835b982006-12-10 02:20:22 -08004941/*
Martin Schwidefsky79741dd2008-12-31 15:11:38 +01004942 * Account for idle time.
4943 * @cputime: the cpu time spent in idle wait
Linus Torvalds1da177e2005-04-16 15:20:36 -07004944 */
Martin Schwidefsky79741dd2008-12-31 15:11:38 +01004945void account_idle_time(cputime_t cputime)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004946{
4947 struct cpu_usage_stat *cpustat = &kstat_this_cpu.cpustat;
Martin Schwidefsky79741dd2008-12-31 15:11:38 +01004948 cputime64_t cputime64 = cputime_to_cputime64(cputime);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004949 struct rq *rq = this_rq();
4950
Martin Schwidefsky79741dd2008-12-31 15:11:38 +01004951 if (atomic_read(&rq->nr_iowait) > 0)
4952 cpustat->iowait = cputime64_add(cpustat->iowait, cputime64);
4953 else
4954 cpustat->idle = cputime64_add(cpustat->idle, cputime64);
Christoph Lameter7835b982006-12-10 02:20:22 -08004955}
4956
Martin Schwidefsky79741dd2008-12-31 15:11:38 +01004957#ifndef CONFIG_VIRT_CPU_ACCOUNTING
4958
4959/*
4960 * Account a single tick of cpu time.
4961 * @p: the process that the cpu time gets accounted to
4962 * @user_tick: indicates if the tick is a user or a system tick
4963 */
4964void account_process_tick(struct task_struct *p, int user_tick)
4965{
4966 cputime_t one_jiffy = jiffies_to_cputime(1);
4967 cputime_t one_jiffy_scaled = cputime_to_scaled(one_jiffy);
4968 struct rq *rq = this_rq();
4969
4970 if (user_tick)
4971 account_user_time(p, one_jiffy, one_jiffy_scaled);
Eric Dumazetf5f293a2009-04-29 14:44:49 +02004972 else if ((p != rq->idle) || (irq_count() != HARDIRQ_OFFSET))
Martin Schwidefsky79741dd2008-12-31 15:11:38 +01004973 account_system_time(p, HARDIRQ_OFFSET, one_jiffy,
4974 one_jiffy_scaled);
4975 else
4976 account_idle_time(one_jiffy);
4977}
4978
4979/*
4980 * Account multiple ticks of steal time.
4981 * @p: the process from which the cpu time has been stolen
4982 * @ticks: number of stolen ticks
4983 */
4984void account_steal_ticks(unsigned long ticks)
4985{
4986 account_steal_time(jiffies_to_cputime(ticks));
4987}
4988
4989/*
4990 * Account multiple ticks of idle time.
4991 * @ticks: number of stolen ticks
4992 */
4993void account_idle_ticks(unsigned long ticks)
4994{
4995 account_idle_time(jiffies_to_cputime(ticks));
4996}
4997
4998#endif
4999
Christoph Lameter7835b982006-12-10 02:20:22 -08005000/*
Balbir Singh49048622008-09-05 18:12:23 +02005001 * Use precise platform statistics if available:
5002 */
5003#ifdef CONFIG_VIRT_CPU_ACCOUNTING
5004cputime_t task_utime(struct task_struct *p)
5005{
5006 return p->utime;
5007}
5008
5009cputime_t task_stime(struct task_struct *p)
5010{
5011 return p->stime;
5012}
5013#else
5014cputime_t task_utime(struct task_struct *p)
5015{
5016 clock_t utime = cputime_to_clock_t(p->utime),
5017 total = utime + cputime_to_clock_t(p->stime);
5018 u64 temp;
5019
5020 /*
5021 * Use CFS's precise accounting:
5022 */
5023 temp = (u64)nsec_to_clock_t(p->se.sum_exec_runtime);
5024
5025 if (total) {
5026 temp *= utime;
5027 do_div(temp, total);
5028 }
5029 utime = (clock_t)temp;
5030
5031 p->prev_utime = max(p->prev_utime, clock_t_to_cputime(utime));
5032 return p->prev_utime;
5033}
5034
5035cputime_t task_stime(struct task_struct *p)
5036{
5037 clock_t stime;
5038
5039 /*
5040 * Use CFS's precise accounting. (we subtract utime from
5041 * the total, to make sure the total observed by userspace
5042 * grows monotonically - apps rely on that):
5043 */
5044 stime = nsec_to_clock_t(p->se.sum_exec_runtime) -
5045 cputime_to_clock_t(task_utime(p));
5046
5047 if (stime >= 0)
5048 p->prev_stime = max(p->prev_stime, clock_t_to_cputime(stime));
5049
5050 return p->prev_stime;
5051}
5052#endif
5053
5054inline cputime_t task_gtime(struct task_struct *p)
5055{
5056 return p->gtime;
5057}
5058
5059/*
Christoph Lameter7835b982006-12-10 02:20:22 -08005060 * This function gets called by the timer code, with HZ frequency.
5061 * We call it with interrupts disabled.
5062 *
5063 * It also gets called by the fork code, when changing the parent's
5064 * timeslices.
5065 */
5066void scheduler_tick(void)
5067{
Christoph Lameter7835b982006-12-10 02:20:22 -08005068 int cpu = smp_processor_id();
5069 struct rq *rq = cpu_rq(cpu);
Ingo Molnardd41f592007-07-09 18:51:59 +02005070 struct task_struct *curr = rq->curr;
Peter Zijlstra3e51f332008-05-03 18:29:28 +02005071
5072 sched_clock_tick();
Christoph Lameter7835b982006-12-10 02:20:22 -08005073
Ingo Molnardd41f592007-07-09 18:51:59 +02005074 spin_lock(&rq->lock);
Peter Zijlstra3e51f332008-05-03 18:29:28 +02005075 update_rq_clock(rq);
Ingo Molnarf1a438d2007-08-09 11:16:45 +02005076 update_cpu_load(rq);
Peter Zijlstrafa85ae22008-01-25 21:08:29 +01005077 curr->sched_class->task_tick(rq, curr, 0);
Ingo Molnardd41f592007-07-09 18:51:59 +02005078 spin_unlock(&rq->lock);
5079
Christoph Lametere418e1c2006-12-10 02:20:23 -08005080#ifdef CONFIG_SMP
Ingo Molnardd41f592007-07-09 18:51:59 +02005081 rq->idle_at_tick = idle_cpu(cpu);
5082 trigger_load_balance(rq, cpu);
Christoph Lametere418e1c2006-12-10 02:20:23 -08005083#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07005084}
5085
Lai Jiangshan132380a2009-04-02 14:18:25 +08005086notrace unsigned long get_parent_ip(unsigned long addr)
Steven Rostedt6cd8a4b2008-05-12 21:20:42 +02005087{
5088 if (in_lock_functions(addr)) {
5089 addr = CALLER_ADDR2;
5090 if (in_lock_functions(addr))
5091 addr = CALLER_ADDR3;
5092 }
5093 return addr;
5094}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005095
Steven Rostedt7e49fcc2009-01-22 19:01:40 -05005096#if defined(CONFIG_PREEMPT) && (defined(CONFIG_DEBUG_PREEMPT) || \
5097 defined(CONFIG_PREEMPT_TRACER))
5098
Srinivasa Ds43627582008-02-23 15:24:04 -08005099void __kprobes add_preempt_count(int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005100{
Steven Rostedt6cd8a4b2008-05-12 21:20:42 +02005101#ifdef CONFIG_DEBUG_PREEMPT
Linus Torvalds1da177e2005-04-16 15:20:36 -07005102 /*
5103 * Underflow?
5104 */
Ingo Molnar9a11b49a2006-07-03 00:24:33 -07005105 if (DEBUG_LOCKS_WARN_ON((preempt_count() < 0)))
5106 return;
Steven Rostedt6cd8a4b2008-05-12 21:20:42 +02005107#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07005108 preempt_count() += val;
Steven Rostedt6cd8a4b2008-05-12 21:20:42 +02005109#ifdef CONFIG_DEBUG_PREEMPT
Linus Torvalds1da177e2005-04-16 15:20:36 -07005110 /*
5111 * Spinlock count overflowing soon?
5112 */
Miguel Ojeda Sandonis33859f72006-12-10 02:20:38 -08005113 DEBUG_LOCKS_WARN_ON((preempt_count() & PREEMPT_MASK) >=
5114 PREEMPT_MASK - 10);
Steven Rostedt6cd8a4b2008-05-12 21:20:42 +02005115#endif
5116 if (preempt_count() == val)
5117 trace_preempt_off(CALLER_ADDR0, get_parent_ip(CALLER_ADDR1));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005118}
5119EXPORT_SYMBOL(add_preempt_count);
5120
Srinivasa Ds43627582008-02-23 15:24:04 -08005121void __kprobes sub_preempt_count(int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005122{
Steven Rostedt6cd8a4b2008-05-12 21:20:42 +02005123#ifdef CONFIG_DEBUG_PREEMPT
Linus Torvalds1da177e2005-04-16 15:20:36 -07005124 /*
5125 * Underflow?
5126 */
Ingo Molnar01e3eb82009-01-12 13:00:50 +01005127 if (DEBUG_LOCKS_WARN_ON(val > preempt_count()))
Ingo Molnar9a11b49a2006-07-03 00:24:33 -07005128 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005129 /*
5130 * Is the spinlock portion underflowing?
5131 */
Ingo Molnar9a11b49a2006-07-03 00:24:33 -07005132 if (DEBUG_LOCKS_WARN_ON((val < PREEMPT_MASK) &&
5133 !(preempt_count() & PREEMPT_MASK)))
5134 return;
Steven Rostedt6cd8a4b2008-05-12 21:20:42 +02005135#endif
Ingo Molnar9a11b49a2006-07-03 00:24:33 -07005136
Steven Rostedt6cd8a4b2008-05-12 21:20:42 +02005137 if (preempt_count() == val)
5138 trace_preempt_on(CALLER_ADDR0, get_parent_ip(CALLER_ADDR1));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005139 preempt_count() -= val;
5140}
5141EXPORT_SYMBOL(sub_preempt_count);
5142
5143#endif
5144
5145/*
Ingo Molnardd41f592007-07-09 18:51:59 +02005146 * Print scheduling while atomic bug:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005147 */
Ingo Molnardd41f592007-07-09 18:51:59 +02005148static noinline void __schedule_bug(struct task_struct *prev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005149{
Satyam Sharma838225b2007-10-24 18:23:50 +02005150 struct pt_regs *regs = get_irq_regs();
5151
5152 printk(KERN_ERR "BUG: scheduling while atomic: %s/%d/0x%08x\n",
5153 prev->comm, prev->pid, preempt_count());
5154
Ingo Molnardd41f592007-07-09 18:51:59 +02005155 debug_show_held_locks(prev);
Arjan van de Vene21f5b12008-05-23 09:05:58 -07005156 print_modules();
Ingo Molnardd41f592007-07-09 18:51:59 +02005157 if (irqs_disabled())
5158 print_irqtrace_events(prev);
Satyam Sharma838225b2007-10-24 18:23:50 +02005159
5160 if (regs)
5161 show_regs(regs);
5162 else
5163 dump_stack();
Ingo Molnardd41f592007-07-09 18:51:59 +02005164}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005165
Ingo Molnardd41f592007-07-09 18:51:59 +02005166/*
5167 * Various schedule()-time debugging checks and statistics:
5168 */
5169static inline void schedule_debug(struct task_struct *prev)
5170{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005171 /*
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01005172 * Test if we are atomic. Since do_exit() needs to call into
Linus Torvalds1da177e2005-04-16 15:20:36 -07005173 * schedule() atomically, we ignore that path for now.
5174 * Otherwise, whine if we are scheduling when we should not be.
5175 */
Roel Kluin3f33a7c2008-05-13 23:44:11 +02005176 if (unlikely(in_atomic_preempt_off() && !prev->exit_state))
Ingo Molnardd41f592007-07-09 18:51:59 +02005177 __schedule_bug(prev);
5178
Linus Torvalds1da177e2005-04-16 15:20:36 -07005179 profile_hit(SCHED_PROFILING, __builtin_return_address(0));
5180
Ingo Molnar2d723762007-10-15 17:00:12 +02005181 schedstat_inc(this_rq(), sched_count);
Ingo Molnarb8efb562007-10-15 17:00:10 +02005182#ifdef CONFIG_SCHEDSTATS
5183 if (unlikely(prev->lock_depth >= 0)) {
Ingo Molnar2d723762007-10-15 17:00:12 +02005184 schedstat_inc(this_rq(), bkl_count);
5185 schedstat_inc(prev, sched_info.bkl_count);
Ingo Molnarb8efb562007-10-15 17:00:10 +02005186 }
5187#endif
Ingo Molnardd41f592007-07-09 18:51:59 +02005188}
5189
Mike Galbraithdf1c99d2009-03-10 19:08:11 +01005190static void put_prev_task(struct rq *rq, struct task_struct *prev)
5191{
5192 if (prev->state == TASK_RUNNING) {
5193 u64 runtime = prev->se.sum_exec_runtime;
5194
5195 runtime -= prev->se.prev_sum_exec_runtime;
5196 runtime = min_t(u64, runtime, 2*sysctl_sched_migration_cost);
5197
5198 /*
5199 * In order to avoid avg_overlap growing stale when we are
5200 * indeed overlapping and hence not getting put to sleep, grow
5201 * the avg_overlap on preemption.
5202 *
5203 * We use the average preemption runtime because that
5204 * correlates to the amount of cache footprint a task can
5205 * build up.
5206 */
5207 update_avg(&prev->se.avg_overlap, runtime);
5208 }
5209 prev->sched_class->put_prev_task(rq, prev);
5210}
5211
Ingo Molnardd41f592007-07-09 18:51:59 +02005212/*
5213 * Pick up the highest-prio task:
5214 */
5215static inline struct task_struct *
Wang Chenb67802e2009-03-02 13:55:26 +08005216pick_next_task(struct rq *rq)
Ingo Molnardd41f592007-07-09 18:51:59 +02005217{
Ingo Molnar5522d5d2007-10-15 17:00:12 +02005218 const struct sched_class *class;
Ingo Molnardd41f592007-07-09 18:51:59 +02005219 struct task_struct *p;
5220
5221 /*
5222 * Optimization: we know that if all tasks are in
5223 * the fair class we can call that function directly:
5224 */
5225 if (likely(rq->nr_running == rq->cfs.nr_running)) {
Ingo Molnarfb8d4722007-08-09 11:16:48 +02005226 p = fair_sched_class.pick_next_task(rq);
Ingo Molnardd41f592007-07-09 18:51:59 +02005227 if (likely(p))
5228 return p;
5229 }
5230
5231 class = sched_class_highest;
5232 for ( ; ; ) {
Ingo Molnarfb8d4722007-08-09 11:16:48 +02005233 p = class->pick_next_task(rq);
Ingo Molnardd41f592007-07-09 18:51:59 +02005234 if (p)
5235 return p;
5236 /*
5237 * Will never be NULL as the idle class always
5238 * returns a non-NULL p:
5239 */
5240 class = class->next;
5241 }
5242}
5243
5244/*
5245 * schedule() is the main scheduler function.
5246 */
Peter Zijlstraff743342009-03-13 12:21:26 +01005247asmlinkage void __sched schedule(void)
Ingo Molnardd41f592007-07-09 18:51:59 +02005248{
5249 struct task_struct *prev, *next;
Harvey Harrison67ca7bd2008-02-15 09:56:36 -08005250 unsigned long *switch_count;
Ingo Molnardd41f592007-07-09 18:51:59 +02005251 struct rq *rq;
Peter Zijlstra31656512008-07-18 18:01:23 +02005252 int cpu;
Ingo Molnardd41f592007-07-09 18:51:59 +02005253
Peter Zijlstraff743342009-03-13 12:21:26 +01005254need_resched:
5255 preempt_disable();
Ingo Molnardd41f592007-07-09 18:51:59 +02005256 cpu = smp_processor_id();
5257 rq = cpu_rq(cpu);
5258 rcu_qsctr_inc(cpu);
5259 prev = rq->curr;
5260 switch_count = &prev->nivcsw;
5261
Linus Torvalds1da177e2005-04-16 15:20:36 -07005262 release_kernel_lock(prev);
5263need_resched_nonpreemptible:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005264
Ingo Molnardd41f592007-07-09 18:51:59 +02005265 schedule_debug(prev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005266
Peter Zijlstra31656512008-07-18 18:01:23 +02005267 if (sched_feat(HRTICK))
Mike Galbraithf333fdc2008-05-12 21:20:55 +02005268 hrtick_clear(rq);
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01005269
Peter Zijlstra8cd162c2008-10-15 20:37:23 +02005270 spin_lock_irq(&rq->lock);
Peter Zijlstra3e51f332008-05-03 18:29:28 +02005271 update_rq_clock(rq);
Ingo Molnar1e819952007-10-15 17:00:13 +02005272 clear_tsk_need_resched(prev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005273
Ingo Molnardd41f592007-07-09 18:51:59 +02005274 if (prev->state && !(preempt_count() & PREEMPT_ACTIVE)) {
Oleg Nesterov16882c12008-06-08 21:20:41 +04005275 if (unlikely(signal_pending_state(prev->state, prev)))
Ingo Molnardd41f592007-07-09 18:51:59 +02005276 prev->state = TASK_RUNNING;
Oleg Nesterov16882c12008-06-08 21:20:41 +04005277 else
Ingo Molnar2e1cb742007-08-09 11:16:49 +02005278 deactivate_task(rq, prev, 1);
Ingo Molnardd41f592007-07-09 18:51:59 +02005279 switch_count = &prev->nvcsw;
5280 }
5281
Steven Rostedt9a897c52008-01-25 21:08:22 +01005282#ifdef CONFIG_SMP
5283 if (prev->sched_class->pre_schedule)
5284 prev->sched_class->pre_schedule(rq, prev);
5285#endif
Steven Rostedtf65eda42008-01-25 21:08:07 +01005286
Ingo Molnardd41f592007-07-09 18:51:59 +02005287 if (unlikely(!rq->nr_running))
5288 idle_balance(cpu, rq);
5289
Mike Galbraithdf1c99d2009-03-10 19:08:11 +01005290 put_prev_task(rq, prev);
Wang Chenb67802e2009-03-02 13:55:26 +08005291 next = pick_next_task(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005292
Linus Torvalds1da177e2005-04-16 15:20:36 -07005293 if (likely(prev != next)) {
David Simner673a90a2008-04-29 10:08:59 +01005294 sched_info_switch(prev, next);
5295
Linus Torvalds1da177e2005-04-16 15:20:36 -07005296 rq->nr_switches++;
5297 rq->curr = next;
5298 ++*switch_count;
5299
Ingo Molnardd41f592007-07-09 18:51:59 +02005300 context_switch(rq, prev, next); /* unlocks the rq */
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01005301 /*
5302 * the context switch might have flipped the stack from under
5303 * us, hence refresh the local variables.
5304 */
5305 cpu = smp_processor_id();
5306 rq = cpu_rq(cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005307 } else
5308 spin_unlock_irq(&rq->lock);
5309
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01005310 if (unlikely(reacquire_kernel_lock(current) < 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005311 goto need_resched_nonpreemptible;
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01005312
Linus Torvalds1da177e2005-04-16 15:20:36 -07005313 preempt_enable_no_resched();
Peter Zijlstraff743342009-03-13 12:21:26 +01005314 if (need_resched())
Linus Torvalds1da177e2005-04-16 15:20:36 -07005315 goto need_resched;
5316}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005317EXPORT_SYMBOL(schedule);
5318
Peter Zijlstra0d66bf62009-01-12 14:01:47 +01005319#ifdef CONFIG_SMP
5320/*
5321 * Look out! "owner" is an entirely speculative pointer
5322 * access and not reliable.
5323 */
5324int mutex_spin_on_owner(struct mutex *lock, struct thread_info *owner)
5325{
5326 unsigned int cpu;
5327 struct rq *rq;
5328
5329 if (!sched_feat(OWNER_SPIN))
5330 return 0;
5331
5332#ifdef CONFIG_DEBUG_PAGEALLOC
5333 /*
5334 * Need to access the cpu field knowing that
5335 * DEBUG_PAGEALLOC could have unmapped it if
5336 * the mutex owner just released it and exited.
5337 */
5338 if (probe_kernel_address(&owner->cpu, cpu))
5339 goto out;
5340#else
5341 cpu = owner->cpu;
5342#endif
5343
5344 /*
5345 * Even if the access succeeded (likely case),
5346 * the cpu field may no longer be valid.
5347 */
5348 if (cpu >= nr_cpumask_bits)
5349 goto out;
5350
5351 /*
5352 * We need to validate that we can do a
5353 * get_cpu() and that we have the percpu area.
5354 */
5355 if (!cpu_online(cpu))
5356 goto out;
5357
5358 rq = cpu_rq(cpu);
5359
5360 for (;;) {
5361 /*
5362 * Owner changed, break to re-assess state.
5363 */
5364 if (lock->owner != owner)
5365 break;
5366
5367 /*
5368 * Is that owner really running on that cpu?
5369 */
5370 if (task_thread_info(rq->curr) != owner || need_resched())
5371 return 0;
5372
5373 cpu_relax();
5374 }
5375out:
5376 return 1;
5377}
5378#endif
5379
Linus Torvalds1da177e2005-04-16 15:20:36 -07005380#ifdef CONFIG_PREEMPT
5381/*
Andreas Mohr2ed6e342006-07-10 04:43:52 -07005382 * this is the entry point to schedule() from in-kernel preemption
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01005383 * off of preempt_enable. Kernel preemptions off return from interrupt
Linus Torvalds1da177e2005-04-16 15:20:36 -07005384 * occur there and call schedule directly.
5385 */
5386asmlinkage void __sched preempt_schedule(void)
5387{
5388 struct thread_info *ti = current_thread_info();
Ingo Molnar6478d882008-01-25 21:08:33 +01005389
Linus Torvalds1da177e2005-04-16 15:20:36 -07005390 /*
5391 * If there is a non-zero preempt_count or interrupts are disabled,
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01005392 * we do not want to preempt the current task. Just return..
Linus Torvalds1da177e2005-04-16 15:20:36 -07005393 */
Nick Pigginbeed33a2006-10-11 01:21:52 -07005394 if (likely(ti->preempt_count || irqs_disabled()))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005395 return;
5396
Andi Kleen3a5c3592007-10-15 17:00:14 +02005397 do {
5398 add_preempt_count(PREEMPT_ACTIVE);
Andi Kleen3a5c3592007-10-15 17:00:14 +02005399 schedule();
Andi Kleen3a5c3592007-10-15 17:00:14 +02005400 sub_preempt_count(PREEMPT_ACTIVE);
5401
5402 /*
5403 * Check again in case we missed a preemption opportunity
5404 * between schedule and now.
5405 */
5406 barrier();
Lai Jiangshan5ed0cec2009-03-06 19:40:20 +08005407 } while (need_resched());
Linus Torvalds1da177e2005-04-16 15:20:36 -07005408}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005409EXPORT_SYMBOL(preempt_schedule);
5410
5411/*
Andreas Mohr2ed6e342006-07-10 04:43:52 -07005412 * this is the entry point to schedule() from kernel preemption
Linus Torvalds1da177e2005-04-16 15:20:36 -07005413 * off of irq context.
5414 * Note, that this is called and return with irqs disabled. This will
5415 * protect us against recursive calling from irq.
5416 */
5417asmlinkage void __sched preempt_schedule_irq(void)
5418{
5419 struct thread_info *ti = current_thread_info();
Ingo Molnar6478d882008-01-25 21:08:33 +01005420
Andreas Mohr2ed6e342006-07-10 04:43:52 -07005421 /* Catch callers which need to be fixed */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005422 BUG_ON(ti->preempt_count || !irqs_disabled());
5423
Andi Kleen3a5c3592007-10-15 17:00:14 +02005424 do {
5425 add_preempt_count(PREEMPT_ACTIVE);
Andi Kleen3a5c3592007-10-15 17:00:14 +02005426 local_irq_enable();
5427 schedule();
5428 local_irq_disable();
Andi Kleen3a5c3592007-10-15 17:00:14 +02005429 sub_preempt_count(PREEMPT_ACTIVE);
5430
5431 /*
5432 * Check again in case we missed a preemption opportunity
5433 * between schedule and now.
5434 */
5435 barrier();
Lai Jiangshan5ed0cec2009-03-06 19:40:20 +08005436 } while (need_resched());
Linus Torvalds1da177e2005-04-16 15:20:36 -07005437}
5438
5439#endif /* CONFIG_PREEMPT */
5440
Ingo Molnar95cdf3b2005-09-10 00:26:11 -07005441int default_wake_function(wait_queue_t *curr, unsigned mode, int sync,
5442 void *key)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005443{
Ingo Molnar48f24c42006-07-03 00:25:40 -07005444 return try_to_wake_up(curr->private, mode, sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005445}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005446EXPORT_SYMBOL(default_wake_function);
5447
5448/*
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01005449 * The core wakeup function. Non-exclusive wakeups (nr_exclusive == 0) just
5450 * wake everything up. If it's an exclusive wakeup (nr_exclusive == small +ve
Linus Torvalds1da177e2005-04-16 15:20:36 -07005451 * number) then we wake all the non-exclusive tasks and one exclusive task.
5452 *
5453 * There are circumstances in which we can try to wake a task which has already
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01005454 * started to run but is not in state TASK_RUNNING. try_to_wake_up() returns
Linus Torvalds1da177e2005-04-16 15:20:36 -07005455 * zero in this (rare) case, and we handle it by continuing to scan the queue.
5456 */
Johannes Weiner78ddb082009-04-14 16:53:05 +02005457static void __wake_up_common(wait_queue_head_t *q, unsigned int mode,
Johannes Weiner777c6c52009-02-04 15:12:14 -08005458 int nr_exclusive, int sync, void *key)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005459{
Matthias Kaehlcke2e458742007-10-15 17:00:02 +02005460 wait_queue_t *curr, *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005461
Matthias Kaehlcke2e458742007-10-15 17:00:02 +02005462 list_for_each_entry_safe(curr, next, &q->task_list, task_list) {
Ingo Molnar48f24c42006-07-03 00:25:40 -07005463 unsigned flags = curr->flags;
5464
Linus Torvalds1da177e2005-04-16 15:20:36 -07005465 if (curr->func(curr, mode, sync, key) &&
Ingo Molnar48f24c42006-07-03 00:25:40 -07005466 (flags & WQ_FLAG_EXCLUSIVE) && !--nr_exclusive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005467 break;
5468 }
5469}
5470
5471/**
5472 * __wake_up - wake up threads blocked on a waitqueue.
5473 * @q: the waitqueue
5474 * @mode: which threads
5475 * @nr_exclusive: how many wake-one or wake-many threads to wake up
Martin Waitz67be2dd2005-05-01 08:59:26 -07005476 * @key: is directly passed to the wakeup function
David Howells50fa6102009-04-28 15:01:38 +01005477 *
5478 * It may be assumed that this function implies a write memory barrier before
5479 * changing the task state if and only if any tasks are woken up.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005480 */
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -08005481void __wake_up(wait_queue_head_t *q, unsigned int mode,
Ingo Molnar95cdf3b2005-09-10 00:26:11 -07005482 int nr_exclusive, void *key)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005483{
5484 unsigned long flags;
5485
5486 spin_lock_irqsave(&q->lock, flags);
5487 __wake_up_common(q, mode, nr_exclusive, 0, key);
5488 spin_unlock_irqrestore(&q->lock, flags);
5489}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005490EXPORT_SYMBOL(__wake_up);
5491
5492/*
5493 * Same as __wake_up but called with the spinlock in wait_queue_head_t held.
5494 */
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -08005495void __wake_up_locked(wait_queue_head_t *q, unsigned int mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005496{
5497 __wake_up_common(q, mode, 1, 0, NULL);
5498}
5499
Davide Libenzi4ede8162009-03-31 15:24:20 -07005500void __wake_up_locked_key(wait_queue_head_t *q, unsigned int mode, void *key)
5501{
5502 __wake_up_common(q, mode, 1, 0, key);
5503}
5504
Linus Torvalds1da177e2005-04-16 15:20:36 -07005505/**
Davide Libenzi4ede8162009-03-31 15:24:20 -07005506 * __wake_up_sync_key - wake up threads blocked on a waitqueue.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005507 * @q: the waitqueue
5508 * @mode: which threads
5509 * @nr_exclusive: how many wake-one or wake-many threads to wake up
Davide Libenzi4ede8162009-03-31 15:24:20 -07005510 * @key: opaque value to be passed to wakeup targets
Linus Torvalds1da177e2005-04-16 15:20:36 -07005511 *
5512 * The sync wakeup differs that the waker knows that it will schedule
5513 * away soon, so while the target thread will be woken up, it will not
5514 * be migrated to another CPU - ie. the two threads are 'synchronized'
5515 * with each other. This can prevent needless bouncing between CPUs.
5516 *
5517 * On UP it can prevent extra preemption.
David Howells50fa6102009-04-28 15:01:38 +01005518 *
5519 * It may be assumed that this function implies a write memory barrier before
5520 * changing the task state if and only if any tasks are woken up.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005521 */
Davide Libenzi4ede8162009-03-31 15:24:20 -07005522void __wake_up_sync_key(wait_queue_head_t *q, unsigned int mode,
5523 int nr_exclusive, void *key)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005524{
5525 unsigned long flags;
5526 int sync = 1;
5527
5528 if (unlikely(!q))
5529 return;
5530
5531 if (unlikely(!nr_exclusive))
5532 sync = 0;
5533
5534 spin_lock_irqsave(&q->lock, flags);
Davide Libenzi4ede8162009-03-31 15:24:20 -07005535 __wake_up_common(q, mode, nr_exclusive, sync, key);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005536 spin_unlock_irqrestore(&q->lock, flags);
5537}
Davide Libenzi4ede8162009-03-31 15:24:20 -07005538EXPORT_SYMBOL_GPL(__wake_up_sync_key);
5539
5540/*
5541 * __wake_up_sync - see __wake_up_sync_key()
5542 */
5543void __wake_up_sync(wait_queue_head_t *q, unsigned int mode, int nr_exclusive)
5544{
5545 __wake_up_sync_key(q, mode, nr_exclusive, NULL);
5546}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005547EXPORT_SYMBOL_GPL(__wake_up_sync); /* For internal use only */
5548
Kevin Diggs65eb3dc2008-08-26 10:26:54 +02005549/**
5550 * complete: - signals a single thread waiting on this completion
5551 * @x: holds the state of this particular completion
5552 *
5553 * This will wake up a single thread waiting on this completion. Threads will be
5554 * awakened in the same order in which they were queued.
5555 *
5556 * See also complete_all(), wait_for_completion() and related routines.
David Howells50fa6102009-04-28 15:01:38 +01005557 *
5558 * It may be assumed that this function implies a write memory barrier before
5559 * changing the task state if and only if any tasks are woken up.
Kevin Diggs65eb3dc2008-08-26 10:26:54 +02005560 */
Ingo Molnarb15136e2007-10-24 18:23:48 +02005561void complete(struct completion *x)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005562{
5563 unsigned long flags;
5564
5565 spin_lock_irqsave(&x->wait.lock, flags);
5566 x->done++;
Matthew Wilcoxd9514f62007-12-06 11:07:07 -05005567 __wake_up_common(&x->wait, TASK_NORMAL, 1, 0, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005568 spin_unlock_irqrestore(&x->wait.lock, flags);
5569}
5570EXPORT_SYMBOL(complete);
5571
Kevin Diggs65eb3dc2008-08-26 10:26:54 +02005572/**
5573 * complete_all: - signals all threads waiting on this completion
5574 * @x: holds the state of this particular completion
5575 *
5576 * This will wake up all threads waiting on this particular completion event.
David Howells50fa6102009-04-28 15:01:38 +01005577 *
5578 * It may be assumed that this function implies a write memory barrier before
5579 * changing the task state if and only if any tasks are woken up.
Kevin Diggs65eb3dc2008-08-26 10:26:54 +02005580 */
Ingo Molnarb15136e2007-10-24 18:23:48 +02005581void complete_all(struct completion *x)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005582{
5583 unsigned long flags;
5584
5585 spin_lock_irqsave(&x->wait.lock, flags);
5586 x->done += UINT_MAX/2;
Matthew Wilcoxd9514f62007-12-06 11:07:07 -05005587 __wake_up_common(&x->wait, TASK_NORMAL, 0, 0, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005588 spin_unlock_irqrestore(&x->wait.lock, flags);
5589}
5590EXPORT_SYMBOL(complete_all);
5591
Andi Kleen8cbbe862007-10-15 17:00:14 +02005592static inline long __sched
5593do_wait_for_common(struct completion *x, long timeout, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005594{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005595 if (!x->done) {
5596 DECLARE_WAITQUEUE(wait, current);
5597
5598 wait.flags |= WQ_FLAG_EXCLUSIVE;
5599 __add_wait_queue_tail(&x->wait, &wait);
5600 do {
Oleg Nesterov94d3d822008-08-20 16:54:41 -07005601 if (signal_pending_state(state, current)) {
Oleg Nesterovea71a542008-06-20 18:32:20 +04005602 timeout = -ERESTARTSYS;
5603 break;
Andi Kleen8cbbe862007-10-15 17:00:14 +02005604 }
5605 __set_current_state(state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005606 spin_unlock_irq(&x->wait.lock);
Andi Kleen8cbbe862007-10-15 17:00:14 +02005607 timeout = schedule_timeout(timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005608 spin_lock_irq(&x->wait.lock);
Oleg Nesterovea71a542008-06-20 18:32:20 +04005609 } while (!x->done && timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005610 __remove_wait_queue(&x->wait, &wait);
Oleg Nesterovea71a542008-06-20 18:32:20 +04005611 if (!x->done)
5612 return timeout;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005613 }
5614 x->done--;
Oleg Nesterovea71a542008-06-20 18:32:20 +04005615 return timeout ?: 1;
Andi Kleen8cbbe862007-10-15 17:00:14 +02005616}
5617
5618static long __sched
5619wait_for_common(struct completion *x, long timeout, int state)
5620{
5621 might_sleep();
5622
5623 spin_lock_irq(&x->wait.lock);
5624 timeout = do_wait_for_common(x, timeout, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005625 spin_unlock_irq(&x->wait.lock);
Andi Kleen8cbbe862007-10-15 17:00:14 +02005626 return timeout;
5627}
5628
Kevin Diggs65eb3dc2008-08-26 10:26:54 +02005629/**
5630 * wait_for_completion: - waits for completion of a task
5631 * @x: holds the state of this particular completion
5632 *
5633 * This waits to be signaled for completion of a specific task. It is NOT
5634 * interruptible and there is no timeout.
5635 *
5636 * See also similar routines (i.e. wait_for_completion_timeout()) with timeout
5637 * and interrupt capability. Also see complete().
5638 */
Ingo Molnarb15136e2007-10-24 18:23:48 +02005639void __sched wait_for_completion(struct completion *x)
Andi Kleen8cbbe862007-10-15 17:00:14 +02005640{
5641 wait_for_common(x, MAX_SCHEDULE_TIMEOUT, TASK_UNINTERRUPTIBLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005642}
5643EXPORT_SYMBOL(wait_for_completion);
5644
Kevin Diggs65eb3dc2008-08-26 10:26:54 +02005645/**
5646 * wait_for_completion_timeout: - waits for completion of a task (w/timeout)
5647 * @x: holds the state of this particular completion
5648 * @timeout: timeout value in jiffies
5649 *
5650 * This waits for either a completion of a specific task to be signaled or for a
5651 * specified timeout to expire. The timeout is in jiffies. It is not
5652 * interruptible.
5653 */
Ingo Molnarb15136e2007-10-24 18:23:48 +02005654unsigned long __sched
Linus Torvalds1da177e2005-04-16 15:20:36 -07005655wait_for_completion_timeout(struct completion *x, unsigned long timeout)
5656{
Andi Kleen8cbbe862007-10-15 17:00:14 +02005657 return wait_for_common(x, timeout, TASK_UNINTERRUPTIBLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005658}
5659EXPORT_SYMBOL(wait_for_completion_timeout);
5660
Kevin Diggs65eb3dc2008-08-26 10:26:54 +02005661/**
5662 * wait_for_completion_interruptible: - waits for completion of a task (w/intr)
5663 * @x: holds the state of this particular completion
5664 *
5665 * This waits for completion of a specific task to be signaled. It is
5666 * interruptible.
5667 */
Andi Kleen8cbbe862007-10-15 17:00:14 +02005668int __sched wait_for_completion_interruptible(struct completion *x)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005669{
Andi Kleen51e97992007-10-18 21:32:55 +02005670 long t = wait_for_common(x, MAX_SCHEDULE_TIMEOUT, TASK_INTERRUPTIBLE);
5671 if (t == -ERESTARTSYS)
5672 return t;
5673 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005674}
5675EXPORT_SYMBOL(wait_for_completion_interruptible);
5676
Kevin Diggs65eb3dc2008-08-26 10:26:54 +02005677/**
5678 * wait_for_completion_interruptible_timeout: - waits for completion (w/(to,intr))
5679 * @x: holds the state of this particular completion
5680 * @timeout: timeout value in jiffies
5681 *
5682 * This waits for either a completion of a specific task to be signaled or for a
5683 * specified timeout to expire. It is interruptible. The timeout is in jiffies.
5684 */
Ingo Molnarb15136e2007-10-24 18:23:48 +02005685unsigned long __sched
Linus Torvalds1da177e2005-04-16 15:20:36 -07005686wait_for_completion_interruptible_timeout(struct completion *x,
5687 unsigned long timeout)
5688{
Andi Kleen8cbbe862007-10-15 17:00:14 +02005689 return wait_for_common(x, timeout, TASK_INTERRUPTIBLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005690}
5691EXPORT_SYMBOL(wait_for_completion_interruptible_timeout);
5692
Kevin Diggs65eb3dc2008-08-26 10:26:54 +02005693/**
5694 * wait_for_completion_killable: - waits for completion of a task (killable)
5695 * @x: holds the state of this particular completion
5696 *
5697 * This waits to be signaled for completion of a specific task. It can be
5698 * interrupted by a kill signal.
5699 */
Matthew Wilcox009e5772007-12-06 12:29:54 -05005700int __sched wait_for_completion_killable(struct completion *x)
5701{
5702 long t = wait_for_common(x, MAX_SCHEDULE_TIMEOUT, TASK_KILLABLE);
5703 if (t == -ERESTARTSYS)
5704 return t;
5705 return 0;
5706}
5707EXPORT_SYMBOL(wait_for_completion_killable);
5708
Dave Chinnerbe4de352008-08-15 00:40:44 -07005709/**
5710 * try_wait_for_completion - try to decrement a completion without blocking
5711 * @x: completion structure
5712 *
5713 * Returns: 0 if a decrement cannot be done without blocking
5714 * 1 if a decrement succeeded.
5715 *
5716 * If a completion is being used as a counting completion,
5717 * attempt to decrement the counter without blocking. This
5718 * enables us to avoid waiting if the resource the completion
5719 * is protecting is not available.
5720 */
5721bool try_wait_for_completion(struct completion *x)
5722{
5723 int ret = 1;
5724
5725 spin_lock_irq(&x->wait.lock);
5726 if (!x->done)
5727 ret = 0;
5728 else
5729 x->done--;
5730 spin_unlock_irq(&x->wait.lock);
5731 return ret;
5732}
5733EXPORT_SYMBOL(try_wait_for_completion);
5734
5735/**
5736 * completion_done - Test to see if a completion has any waiters
5737 * @x: completion structure
5738 *
5739 * Returns: 0 if there are waiters (wait_for_completion() in progress)
5740 * 1 if there are no waiters.
5741 *
5742 */
5743bool completion_done(struct completion *x)
5744{
5745 int ret = 1;
5746
5747 spin_lock_irq(&x->wait.lock);
5748 if (!x->done)
5749 ret = 0;
5750 spin_unlock_irq(&x->wait.lock);
5751 return ret;
5752}
5753EXPORT_SYMBOL(completion_done);
5754
Andi Kleen8cbbe862007-10-15 17:00:14 +02005755static long __sched
5756sleep_on_common(wait_queue_head_t *q, int state, long timeout)
Ingo Molnar0fec1712007-07-09 18:52:01 +02005757{
5758 unsigned long flags;
5759 wait_queue_t wait;
5760
5761 init_waitqueue_entry(&wait, current);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005762
Andi Kleen8cbbe862007-10-15 17:00:14 +02005763 __set_current_state(state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005764
Andi Kleen8cbbe862007-10-15 17:00:14 +02005765 spin_lock_irqsave(&q->lock, flags);
5766 __add_wait_queue(q, &wait);
5767 spin_unlock(&q->lock);
5768 timeout = schedule_timeout(timeout);
5769 spin_lock_irq(&q->lock);
5770 __remove_wait_queue(q, &wait);
5771 spin_unlock_irqrestore(&q->lock, flags);
5772
5773 return timeout;
5774}
5775
5776void __sched interruptible_sleep_on(wait_queue_head_t *q)
5777{
5778 sleep_on_common(q, TASK_INTERRUPTIBLE, MAX_SCHEDULE_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005779}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005780EXPORT_SYMBOL(interruptible_sleep_on);
5781
Ingo Molnar0fec1712007-07-09 18:52:01 +02005782long __sched
Ingo Molnar95cdf3b2005-09-10 00:26:11 -07005783interruptible_sleep_on_timeout(wait_queue_head_t *q, long timeout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005784{
Andi Kleen8cbbe862007-10-15 17:00:14 +02005785 return sleep_on_common(q, TASK_INTERRUPTIBLE, timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005786}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005787EXPORT_SYMBOL(interruptible_sleep_on_timeout);
5788
Ingo Molnar0fec1712007-07-09 18:52:01 +02005789void __sched sleep_on(wait_queue_head_t *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005790{
Andi Kleen8cbbe862007-10-15 17:00:14 +02005791 sleep_on_common(q, TASK_UNINTERRUPTIBLE, MAX_SCHEDULE_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005792}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005793EXPORT_SYMBOL(sleep_on);
5794
Ingo Molnar0fec1712007-07-09 18:52:01 +02005795long __sched sleep_on_timeout(wait_queue_head_t *q, long timeout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005796{
Andi Kleen8cbbe862007-10-15 17:00:14 +02005797 return sleep_on_common(q, TASK_UNINTERRUPTIBLE, timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005798}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005799EXPORT_SYMBOL(sleep_on_timeout);
5800
Ingo Molnarb29739f2006-06-27 02:54:51 -07005801#ifdef CONFIG_RT_MUTEXES
5802
5803/*
5804 * rt_mutex_setprio - set the current priority of a task
5805 * @p: task
5806 * @prio: prio value (kernel-internal form)
5807 *
5808 * This function changes the 'effective' priority of a task. It does
5809 * not touch ->normal_prio like __setscheduler().
5810 *
5811 * Used by the rt_mutex code to implement priority inheritance logic.
5812 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07005813void rt_mutex_setprio(struct task_struct *p, int prio)
Ingo Molnarb29739f2006-06-27 02:54:51 -07005814{
5815 unsigned long flags;
Srivatsa Vaddagiri83b699e2007-10-15 17:00:08 +02005816 int oldprio, on_rq, running;
Ingo Molnar70b97a72006-07-03 00:25:42 -07005817 struct rq *rq;
Steven Rostedtcb469842008-01-25 21:08:22 +01005818 const struct sched_class *prev_class = p->sched_class;
Ingo Molnarb29739f2006-06-27 02:54:51 -07005819
5820 BUG_ON(prio < 0 || prio > MAX_PRIO);
5821
5822 rq = task_rq_lock(p, &flags);
Ingo Molnara8e504d2007-08-09 11:16:47 +02005823 update_rq_clock(rq);
Ingo Molnarb29739f2006-06-27 02:54:51 -07005824
Andrew Mortond5f9f942007-05-08 20:27:06 -07005825 oldprio = p->prio;
Ingo Molnardd41f592007-07-09 18:51:59 +02005826 on_rq = p->se.on_rq;
Dmitry Adamushko051a1d12007-12-18 15:21:13 +01005827 running = task_current(rq, p);
Hiroshi Shimamoto0e1f3482008-03-10 11:01:20 -07005828 if (on_rq)
Ingo Molnar69be72c2007-08-09 11:16:49 +02005829 dequeue_task(rq, p, 0);
Hiroshi Shimamoto0e1f3482008-03-10 11:01:20 -07005830 if (running)
5831 p->sched_class->put_prev_task(rq, p);
Ingo Molnardd41f592007-07-09 18:51:59 +02005832
5833 if (rt_prio(prio))
5834 p->sched_class = &rt_sched_class;
5835 else
5836 p->sched_class = &fair_sched_class;
5837
Ingo Molnarb29739f2006-06-27 02:54:51 -07005838 p->prio = prio;
5839
Hiroshi Shimamoto0e1f3482008-03-10 11:01:20 -07005840 if (running)
5841 p->sched_class->set_curr_task(rq);
Ingo Molnardd41f592007-07-09 18:51:59 +02005842 if (on_rq) {
Ingo Molnar8159f872007-08-09 11:16:49 +02005843 enqueue_task(rq, p, 0);
Steven Rostedtcb469842008-01-25 21:08:22 +01005844
5845 check_class_changed(rq, p, prev_class, oldprio, running);
Ingo Molnarb29739f2006-06-27 02:54:51 -07005846 }
5847 task_rq_unlock(rq, &flags);
5848}
5849
5850#endif
5851
Ingo Molnar36c8b582006-07-03 00:25:41 -07005852void set_user_nice(struct task_struct *p, long nice)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005853{
Ingo Molnardd41f592007-07-09 18:51:59 +02005854 int old_prio, delta, on_rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005855 unsigned long flags;
Ingo Molnar70b97a72006-07-03 00:25:42 -07005856 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005857
5858 if (TASK_NICE(p) == nice || nice < -20 || nice > 19)
5859 return;
5860 /*
5861 * We have to be careful, if called from sys_setpriority(),
5862 * the task might be in the middle of scheduling on another CPU.
5863 */
5864 rq = task_rq_lock(p, &flags);
Ingo Molnara8e504d2007-08-09 11:16:47 +02005865 update_rq_clock(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005866 /*
5867 * The RT priorities are set via sched_setscheduler(), but we still
5868 * allow the 'normal' nice value to be set - but as expected
5869 * it wont have any effect on scheduling until the task is
Ingo Molnardd41f592007-07-09 18:51:59 +02005870 * SCHED_FIFO/SCHED_RR:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005871 */
Ingo Molnare05606d2007-07-09 18:51:59 +02005872 if (task_has_rt_policy(p)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005873 p->static_prio = NICE_TO_PRIO(nice);
5874 goto out_unlock;
5875 }
Ingo Molnardd41f592007-07-09 18:51:59 +02005876 on_rq = p->se.on_rq;
Peter Zijlstrac09595f2008-06-27 13:41:14 +02005877 if (on_rq)
Ingo Molnar69be72c2007-08-09 11:16:49 +02005878 dequeue_task(rq, p, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005879
Linus Torvalds1da177e2005-04-16 15:20:36 -07005880 p->static_prio = NICE_TO_PRIO(nice);
Peter Williams2dd73a42006-06-27 02:54:34 -07005881 set_load_weight(p);
Ingo Molnarb29739f2006-06-27 02:54:51 -07005882 old_prio = p->prio;
5883 p->prio = effective_prio(p);
5884 delta = p->prio - old_prio;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005885
Ingo Molnardd41f592007-07-09 18:51:59 +02005886 if (on_rq) {
Ingo Molnar8159f872007-08-09 11:16:49 +02005887 enqueue_task(rq, p, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005888 /*
Andrew Mortond5f9f942007-05-08 20:27:06 -07005889 * If the task increased its priority or is running and
5890 * lowered its priority, then reschedule its CPU:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005891 */
Andrew Mortond5f9f942007-05-08 20:27:06 -07005892 if (delta < 0 || (delta > 0 && task_running(rq, p)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005893 resched_task(rq->curr);
5894 }
5895out_unlock:
5896 task_rq_unlock(rq, &flags);
5897}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005898EXPORT_SYMBOL(set_user_nice);
5899
Matt Mackalle43379f2005-05-01 08:59:00 -07005900/*
5901 * can_nice - check if a task can reduce its nice value
5902 * @p: task
5903 * @nice: nice value
5904 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07005905int can_nice(const struct task_struct *p, const int nice)
Matt Mackalle43379f2005-05-01 08:59:00 -07005906{
Matt Mackall024f4742005-08-18 11:24:19 -07005907 /* convert nice value [19,-20] to rlimit style value [1,40] */
5908 int nice_rlim = 20 - nice;
Ingo Molnar48f24c42006-07-03 00:25:40 -07005909
Matt Mackalle43379f2005-05-01 08:59:00 -07005910 return (nice_rlim <= p->signal->rlim[RLIMIT_NICE].rlim_cur ||
5911 capable(CAP_SYS_NICE));
5912}
5913
Linus Torvalds1da177e2005-04-16 15:20:36 -07005914#ifdef __ARCH_WANT_SYS_NICE
5915
5916/*
5917 * sys_nice - change the priority of the current process.
5918 * @increment: priority increment
5919 *
5920 * sys_setpriority is a more generic, but much slower function that
5921 * does similar things.
5922 */
Heiko Carstens5add95d2009-01-14 14:14:08 +01005923SYSCALL_DEFINE1(nice, int, increment)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005924{
Ingo Molnar48f24c42006-07-03 00:25:40 -07005925 long nice, retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005926
5927 /*
5928 * Setpriority might change our priority at the same moment.
5929 * We don't have to worry. Conceptually one call occurs first
5930 * and we have a single winner.
5931 */
Matt Mackalle43379f2005-05-01 08:59:00 -07005932 if (increment < -40)
5933 increment = -40;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005934 if (increment > 40)
5935 increment = 40;
5936
Américo Wang2b8f8362009-02-16 18:54:21 +08005937 nice = TASK_NICE(current) + increment;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005938 if (nice < -20)
5939 nice = -20;
5940 if (nice > 19)
5941 nice = 19;
5942
Matt Mackalle43379f2005-05-01 08:59:00 -07005943 if (increment < 0 && !can_nice(current, nice))
5944 return -EPERM;
5945
Linus Torvalds1da177e2005-04-16 15:20:36 -07005946 retval = security_task_setnice(current, nice);
5947 if (retval)
5948 return retval;
5949
5950 set_user_nice(current, nice);
5951 return 0;
5952}
5953
5954#endif
5955
5956/**
5957 * task_prio - return the priority value of a given task.
5958 * @p: the task in question.
5959 *
5960 * This is the priority value as seen by users in /proc.
5961 * RT tasks are offset by -200. Normal tasks are centered
5962 * around 0, value goes from -16 to +15.
5963 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07005964int task_prio(const struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005965{
5966 return p->prio - MAX_RT_PRIO;
5967}
5968
5969/**
5970 * task_nice - return the nice value of a given task.
5971 * @p: the task in question.
5972 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07005973int task_nice(const struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005974{
5975 return TASK_NICE(p);
5976}
Pavel Roskin150d8be2008-03-05 16:56:37 -05005977EXPORT_SYMBOL(task_nice);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005978
5979/**
5980 * idle_cpu - is a given cpu idle currently?
5981 * @cpu: the processor in question.
5982 */
5983int idle_cpu(int cpu)
5984{
5985 return cpu_curr(cpu) == cpu_rq(cpu)->idle;
5986}
5987
Linus Torvalds1da177e2005-04-16 15:20:36 -07005988/**
5989 * idle_task - return the idle task for a given cpu.
5990 * @cpu: the processor in question.
5991 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07005992struct task_struct *idle_task(int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005993{
5994 return cpu_rq(cpu)->idle;
5995}
5996
5997/**
5998 * find_process_by_pid - find a process with a matching PID value.
5999 * @pid: the pid in question.
6000 */
Alexey Dobriyana9957442007-10-15 17:00:13 +02006001static struct task_struct *find_process_by_pid(pid_t pid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006002{
Pavel Emelyanov228ebcb2007-10-18 23:40:16 -07006003 return pid ? find_task_by_vpid(pid) : current;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006004}
6005
6006/* Actually do priority change: must hold rq lock. */
Ingo Molnardd41f592007-07-09 18:51:59 +02006007static void
6008__setscheduler(struct rq *rq, struct task_struct *p, int policy, int prio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006009{
Ingo Molnardd41f592007-07-09 18:51:59 +02006010 BUG_ON(p->se.on_rq);
Ingo Molnar48f24c42006-07-03 00:25:40 -07006011
Linus Torvalds1da177e2005-04-16 15:20:36 -07006012 p->policy = policy;
Ingo Molnardd41f592007-07-09 18:51:59 +02006013 switch (p->policy) {
6014 case SCHED_NORMAL:
6015 case SCHED_BATCH:
6016 case SCHED_IDLE:
6017 p->sched_class = &fair_sched_class;
6018 break;
6019 case SCHED_FIFO:
6020 case SCHED_RR:
6021 p->sched_class = &rt_sched_class;
6022 break;
6023 }
6024
Linus Torvalds1da177e2005-04-16 15:20:36 -07006025 p->rt_priority = prio;
Ingo Molnarb29739f2006-06-27 02:54:51 -07006026 p->normal_prio = normal_prio(p);
6027 /* we are holding p->pi_lock already */
6028 p->prio = rt_mutex_getprio(p);
Peter Williams2dd73a42006-06-27 02:54:34 -07006029 set_load_weight(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006030}
6031
David Howellsc69e8d92008-11-14 10:39:19 +11006032/*
6033 * check the target process has a UID that matches the current process's
6034 */
6035static bool check_same_owner(struct task_struct *p)
6036{
6037 const struct cred *cred = current_cred(), *pcred;
6038 bool match;
6039
6040 rcu_read_lock();
6041 pcred = __task_cred(p);
6042 match = (cred->euid == pcred->euid ||
6043 cred->euid == pcred->uid);
6044 rcu_read_unlock();
6045 return match;
6046}
6047
Rusty Russell961ccdd2008-06-23 13:55:38 +10006048static int __sched_setscheduler(struct task_struct *p, int policy,
6049 struct sched_param *param, bool user)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006050{
Srivatsa Vaddagiri83b699e2007-10-15 17:00:08 +02006051 int retval, oldprio, oldpolicy = -1, on_rq, running;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006052 unsigned long flags;
Steven Rostedtcb469842008-01-25 21:08:22 +01006053 const struct sched_class *prev_class = p->sched_class;
Ingo Molnar70b97a72006-07-03 00:25:42 -07006054 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006055
Steven Rostedt66e53932006-06-27 02:54:44 -07006056 /* may grab non-irq protected spin_locks */
6057 BUG_ON(in_interrupt());
Linus Torvalds1da177e2005-04-16 15:20:36 -07006058recheck:
6059 /* double check policy once rq lock held */
6060 if (policy < 0)
6061 policy = oldpolicy = p->policy;
6062 else if (policy != SCHED_FIFO && policy != SCHED_RR &&
Ingo Molnardd41f592007-07-09 18:51:59 +02006063 policy != SCHED_NORMAL && policy != SCHED_BATCH &&
6064 policy != SCHED_IDLE)
Ingo Molnarb0a94992006-01-14 13:20:41 -08006065 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006066 /*
6067 * Valid priorities for SCHED_FIFO and SCHED_RR are
Ingo Molnardd41f592007-07-09 18:51:59 +02006068 * 1..MAX_USER_RT_PRIO-1, valid priority for SCHED_NORMAL,
6069 * SCHED_BATCH and SCHED_IDLE is 0.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006070 */
6071 if (param->sched_priority < 0 ||
Ingo Molnar95cdf3b2005-09-10 00:26:11 -07006072 (p->mm && param->sched_priority > MAX_USER_RT_PRIO-1) ||
Steven Rostedtd46523e2005-07-25 16:28:39 -04006073 (!p->mm && param->sched_priority > MAX_RT_PRIO-1))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006074 return -EINVAL;
Ingo Molnare05606d2007-07-09 18:51:59 +02006075 if (rt_policy(policy) != (param->sched_priority != 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006076 return -EINVAL;
6077
Olivier Croquette37e4ab32005-06-25 14:57:32 -07006078 /*
6079 * Allow unprivileged RT tasks to decrease priority:
6080 */
Rusty Russell961ccdd2008-06-23 13:55:38 +10006081 if (user && !capable(CAP_SYS_NICE)) {
Ingo Molnare05606d2007-07-09 18:51:59 +02006082 if (rt_policy(policy)) {
Oleg Nesterov8dc3e902006-09-29 02:00:50 -07006083 unsigned long rlim_rtprio;
Oleg Nesterov5fe1d752006-09-29 02:00:48 -07006084
Oleg Nesterov8dc3e902006-09-29 02:00:50 -07006085 if (!lock_task_sighand(p, &flags))
6086 return -ESRCH;
6087 rlim_rtprio = p->signal->rlim[RLIMIT_RTPRIO].rlim_cur;
6088 unlock_task_sighand(p, &flags);
Oleg Nesterov5fe1d752006-09-29 02:00:48 -07006089
Oleg Nesterov8dc3e902006-09-29 02:00:50 -07006090 /* can't set/change the rt policy */
6091 if (policy != p->policy && !rlim_rtprio)
6092 return -EPERM;
6093
6094 /* can't increase priority */
6095 if (param->sched_priority > p->rt_priority &&
6096 param->sched_priority > rlim_rtprio)
6097 return -EPERM;
6098 }
Ingo Molnardd41f592007-07-09 18:51:59 +02006099 /*
6100 * Like positive nice levels, dont allow tasks to
6101 * move out of SCHED_IDLE either:
6102 */
6103 if (p->policy == SCHED_IDLE && policy != SCHED_IDLE)
6104 return -EPERM;
Oleg Nesterov8dc3e902006-09-29 02:00:50 -07006105
Olivier Croquette37e4ab32005-06-25 14:57:32 -07006106 /* can't change other user's priorities */
David Howellsc69e8d92008-11-14 10:39:19 +11006107 if (!check_same_owner(p))
Olivier Croquette37e4ab32005-06-25 14:57:32 -07006108 return -EPERM;
6109 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006110
Jeremy Fitzhardinge725aad22008-08-03 09:33:03 -07006111 if (user) {
Peter Zijlstrab68aa232008-02-13 15:45:40 +01006112#ifdef CONFIG_RT_GROUP_SCHED
Jeremy Fitzhardinge725aad22008-08-03 09:33:03 -07006113 /*
6114 * Do not allow realtime tasks into groups that have no runtime
6115 * assigned.
6116 */
Peter Zijlstra9a7e0b12008-08-19 12:33:06 +02006117 if (rt_bandwidth_enabled() && rt_policy(policy) &&
6118 task_group(p)->rt_bandwidth.rt_runtime == 0)
Jeremy Fitzhardinge725aad22008-08-03 09:33:03 -07006119 return -EPERM;
Peter Zijlstrab68aa232008-02-13 15:45:40 +01006120#endif
6121
Jeremy Fitzhardinge725aad22008-08-03 09:33:03 -07006122 retval = security_task_setscheduler(p, policy, param);
6123 if (retval)
6124 return retval;
6125 }
6126
Linus Torvalds1da177e2005-04-16 15:20:36 -07006127 /*
Ingo Molnarb29739f2006-06-27 02:54:51 -07006128 * make sure no PI-waiters arrive (or leave) while we are
6129 * changing the priority of the task:
6130 */
6131 spin_lock_irqsave(&p->pi_lock, flags);
6132 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07006133 * To be able to change p->policy safely, the apropriate
6134 * runqueue lock must be held.
6135 */
Ingo Molnarb29739f2006-06-27 02:54:51 -07006136 rq = __task_rq_lock(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006137 /* recheck policy now with rq lock held */
6138 if (unlikely(oldpolicy != -1 && oldpolicy != p->policy)) {
6139 policy = oldpolicy = -1;
Ingo Molnarb29739f2006-06-27 02:54:51 -07006140 __task_rq_unlock(rq);
6141 spin_unlock_irqrestore(&p->pi_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006142 goto recheck;
6143 }
Ingo Molnar2daa3572007-08-09 11:16:51 +02006144 update_rq_clock(rq);
Ingo Molnardd41f592007-07-09 18:51:59 +02006145 on_rq = p->se.on_rq;
Dmitry Adamushko051a1d12007-12-18 15:21:13 +01006146 running = task_current(rq, p);
Hiroshi Shimamoto0e1f3482008-03-10 11:01:20 -07006147 if (on_rq)
Ingo Molnar2e1cb742007-08-09 11:16:49 +02006148 deactivate_task(rq, p, 0);
Hiroshi Shimamoto0e1f3482008-03-10 11:01:20 -07006149 if (running)
6150 p->sched_class->put_prev_task(rq, p);
Dmitry Adamushkof6b53202007-10-15 17:00:08 +02006151
Linus Torvalds1da177e2005-04-16 15:20:36 -07006152 oldprio = p->prio;
Ingo Molnardd41f592007-07-09 18:51:59 +02006153 __setscheduler(rq, p, policy, param->sched_priority);
Dmitry Adamushkof6b53202007-10-15 17:00:08 +02006154
Hiroshi Shimamoto0e1f3482008-03-10 11:01:20 -07006155 if (running)
6156 p->sched_class->set_curr_task(rq);
Ingo Molnardd41f592007-07-09 18:51:59 +02006157 if (on_rq) {
6158 activate_task(rq, p, 0);
Steven Rostedtcb469842008-01-25 21:08:22 +01006159
6160 check_class_changed(rq, p, prev_class, oldprio, running);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006161 }
Ingo Molnarb29739f2006-06-27 02:54:51 -07006162 __task_rq_unlock(rq);
6163 spin_unlock_irqrestore(&p->pi_lock, flags);
6164
Thomas Gleixner95e02ca2006-06-27 02:55:02 -07006165 rt_mutex_adjust_pi(p);
6166
Linus Torvalds1da177e2005-04-16 15:20:36 -07006167 return 0;
6168}
Rusty Russell961ccdd2008-06-23 13:55:38 +10006169
6170/**
6171 * sched_setscheduler - change the scheduling policy and/or RT priority of a thread.
6172 * @p: the task in question.
6173 * @policy: new policy.
6174 * @param: structure containing the new RT priority.
6175 *
6176 * NOTE that the task may be already dead.
6177 */
6178int sched_setscheduler(struct task_struct *p, int policy,
6179 struct sched_param *param)
6180{
6181 return __sched_setscheduler(p, policy, param, true);
6182}
Linus Torvalds1da177e2005-04-16 15:20:36 -07006183EXPORT_SYMBOL_GPL(sched_setscheduler);
6184
Rusty Russell961ccdd2008-06-23 13:55:38 +10006185/**
6186 * sched_setscheduler_nocheck - change the scheduling policy and/or RT priority of a thread from kernelspace.
6187 * @p: the task in question.
6188 * @policy: new policy.
6189 * @param: structure containing the new RT priority.
6190 *
6191 * Just like sched_setscheduler, only don't bother checking if the
6192 * current context has permission. For example, this is needed in
6193 * stop_machine(): we create temporary high priority worker threads,
6194 * but our caller might not have that capability.
6195 */
6196int sched_setscheduler_nocheck(struct task_struct *p, int policy,
6197 struct sched_param *param)
6198{
6199 return __sched_setscheduler(p, policy, param, false);
6200}
6201
Ingo Molnar95cdf3b2005-09-10 00:26:11 -07006202static int
6203do_sched_setscheduler(pid_t pid, int policy, struct sched_param __user *param)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006204{
Linus Torvalds1da177e2005-04-16 15:20:36 -07006205 struct sched_param lparam;
6206 struct task_struct *p;
Ingo Molnar36c8b582006-07-03 00:25:41 -07006207 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006208
6209 if (!param || pid < 0)
6210 return -EINVAL;
6211 if (copy_from_user(&lparam, param, sizeof(struct sched_param)))
6212 return -EFAULT;
Oleg Nesterov5fe1d752006-09-29 02:00:48 -07006213
6214 rcu_read_lock();
6215 retval = -ESRCH;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006216 p = find_process_by_pid(pid);
Oleg Nesterov5fe1d752006-09-29 02:00:48 -07006217 if (p != NULL)
6218 retval = sched_setscheduler(p, policy, &lparam);
6219 rcu_read_unlock();
Ingo Molnar36c8b582006-07-03 00:25:41 -07006220
Linus Torvalds1da177e2005-04-16 15:20:36 -07006221 return retval;
6222}
6223
6224/**
6225 * sys_sched_setscheduler - set/change the scheduler policy and RT priority
6226 * @pid: the pid in question.
6227 * @policy: new policy.
6228 * @param: structure containing the new RT priority.
6229 */
Heiko Carstens5add95d2009-01-14 14:14:08 +01006230SYSCALL_DEFINE3(sched_setscheduler, pid_t, pid, int, policy,
6231 struct sched_param __user *, param)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006232{
Jason Baronc21761f2006-01-18 17:43:03 -08006233 /* negative values for policy are not valid */
6234 if (policy < 0)
6235 return -EINVAL;
6236
Linus Torvalds1da177e2005-04-16 15:20:36 -07006237 return do_sched_setscheduler(pid, policy, param);
6238}
6239
6240/**
6241 * sys_sched_setparam - set/change the RT priority of a thread
6242 * @pid: the pid in question.
6243 * @param: structure containing the new RT priority.
6244 */
Heiko Carstens5add95d2009-01-14 14:14:08 +01006245SYSCALL_DEFINE2(sched_setparam, pid_t, pid, struct sched_param __user *, param)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006246{
6247 return do_sched_setscheduler(pid, -1, param);
6248}
6249
6250/**
6251 * sys_sched_getscheduler - get the policy (scheduling class) of a thread
6252 * @pid: the pid in question.
6253 */
Heiko Carstens5add95d2009-01-14 14:14:08 +01006254SYSCALL_DEFINE1(sched_getscheduler, pid_t, pid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006255{
Ingo Molnar36c8b582006-07-03 00:25:41 -07006256 struct task_struct *p;
Andi Kleen3a5c3592007-10-15 17:00:14 +02006257 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006258
6259 if (pid < 0)
Andi Kleen3a5c3592007-10-15 17:00:14 +02006260 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006261
6262 retval = -ESRCH;
6263 read_lock(&tasklist_lock);
6264 p = find_process_by_pid(pid);
6265 if (p) {
6266 retval = security_task_getscheduler(p);
6267 if (!retval)
6268 retval = p->policy;
6269 }
6270 read_unlock(&tasklist_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006271 return retval;
6272}
6273
6274/**
6275 * sys_sched_getscheduler - get the RT priority of a thread
6276 * @pid: the pid in question.
6277 * @param: structure containing the RT priority.
6278 */
Heiko Carstens5add95d2009-01-14 14:14:08 +01006279SYSCALL_DEFINE2(sched_getparam, pid_t, pid, struct sched_param __user *, param)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006280{
6281 struct sched_param lp;
Ingo Molnar36c8b582006-07-03 00:25:41 -07006282 struct task_struct *p;
Andi Kleen3a5c3592007-10-15 17:00:14 +02006283 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006284
6285 if (!param || pid < 0)
Andi Kleen3a5c3592007-10-15 17:00:14 +02006286 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006287
6288 read_lock(&tasklist_lock);
6289 p = find_process_by_pid(pid);
6290 retval = -ESRCH;
6291 if (!p)
6292 goto out_unlock;
6293
6294 retval = security_task_getscheduler(p);
6295 if (retval)
6296 goto out_unlock;
6297
6298 lp.sched_priority = p->rt_priority;
6299 read_unlock(&tasklist_lock);
6300
6301 /*
6302 * This one might sleep, we cannot do it with a spinlock held ...
6303 */
6304 retval = copy_to_user(param, &lp, sizeof(*param)) ? -EFAULT : 0;
6305
Linus Torvalds1da177e2005-04-16 15:20:36 -07006306 return retval;
6307
6308out_unlock:
6309 read_unlock(&tasklist_lock);
6310 return retval;
6311}
6312
Rusty Russell96f874e2008-11-25 02:35:14 +10306313long sched_setaffinity(pid_t pid, const struct cpumask *in_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006314{
Rusty Russell5a16f3d2008-11-25 02:35:11 +10306315 cpumask_var_t cpus_allowed, new_mask;
Ingo Molnar36c8b582006-07-03 00:25:41 -07006316 struct task_struct *p;
6317 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006318
Gautham R Shenoy95402b32008-01-25 21:08:02 +01006319 get_online_cpus();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006320 read_lock(&tasklist_lock);
6321
6322 p = find_process_by_pid(pid);
6323 if (!p) {
6324 read_unlock(&tasklist_lock);
Gautham R Shenoy95402b32008-01-25 21:08:02 +01006325 put_online_cpus();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006326 return -ESRCH;
6327 }
6328
6329 /*
6330 * It is not safe to call set_cpus_allowed with the
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01006331 * tasklist_lock held. We will bump the task_struct's
Linus Torvalds1da177e2005-04-16 15:20:36 -07006332 * usage count and then drop tasklist_lock.
6333 */
6334 get_task_struct(p);
6335 read_unlock(&tasklist_lock);
6336
Rusty Russell5a16f3d2008-11-25 02:35:11 +10306337 if (!alloc_cpumask_var(&cpus_allowed, GFP_KERNEL)) {
6338 retval = -ENOMEM;
6339 goto out_put_task;
6340 }
6341 if (!alloc_cpumask_var(&new_mask, GFP_KERNEL)) {
6342 retval = -ENOMEM;
6343 goto out_free_cpus_allowed;
6344 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006345 retval = -EPERM;
David Howellsc69e8d92008-11-14 10:39:19 +11006346 if (!check_same_owner(p) && !capable(CAP_SYS_NICE))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006347 goto out_unlock;
6348
David Quigleye7834f82006-06-23 02:03:59 -07006349 retval = security_task_setscheduler(p, 0, NULL);
6350 if (retval)
6351 goto out_unlock;
6352
Rusty Russell5a16f3d2008-11-25 02:35:11 +10306353 cpuset_cpus_allowed(p, cpus_allowed);
6354 cpumask_and(new_mask, in_mask, cpus_allowed);
Paul Menage8707d8b2007-10-18 23:40:22 -07006355 again:
Rusty Russell5a16f3d2008-11-25 02:35:11 +10306356 retval = set_cpus_allowed_ptr(p, new_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006357
Paul Menage8707d8b2007-10-18 23:40:22 -07006358 if (!retval) {
Rusty Russell5a16f3d2008-11-25 02:35:11 +10306359 cpuset_cpus_allowed(p, cpus_allowed);
6360 if (!cpumask_subset(new_mask, cpus_allowed)) {
Paul Menage8707d8b2007-10-18 23:40:22 -07006361 /*
6362 * We must have raced with a concurrent cpuset
6363 * update. Just reset the cpus_allowed to the
6364 * cpuset's cpus_allowed
6365 */
Rusty Russell5a16f3d2008-11-25 02:35:11 +10306366 cpumask_copy(new_mask, cpus_allowed);
Paul Menage8707d8b2007-10-18 23:40:22 -07006367 goto again;
6368 }
6369 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006370out_unlock:
Rusty Russell5a16f3d2008-11-25 02:35:11 +10306371 free_cpumask_var(new_mask);
6372out_free_cpus_allowed:
6373 free_cpumask_var(cpus_allowed);
6374out_put_task:
Linus Torvalds1da177e2005-04-16 15:20:36 -07006375 put_task_struct(p);
Gautham R Shenoy95402b32008-01-25 21:08:02 +01006376 put_online_cpus();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006377 return retval;
6378}
6379
6380static int get_user_cpu_mask(unsigned long __user *user_mask_ptr, unsigned len,
Rusty Russell96f874e2008-11-25 02:35:14 +10306381 struct cpumask *new_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006382{
Rusty Russell96f874e2008-11-25 02:35:14 +10306383 if (len < cpumask_size())
6384 cpumask_clear(new_mask);
6385 else if (len > cpumask_size())
6386 len = cpumask_size();
6387
Linus Torvalds1da177e2005-04-16 15:20:36 -07006388 return copy_from_user(new_mask, user_mask_ptr, len) ? -EFAULT : 0;
6389}
6390
6391/**
6392 * sys_sched_setaffinity - set the cpu affinity of a process
6393 * @pid: pid of the process
6394 * @len: length in bytes of the bitmask pointed to by user_mask_ptr
6395 * @user_mask_ptr: user-space pointer to the new cpu mask
6396 */
Heiko Carstens5add95d2009-01-14 14:14:08 +01006397SYSCALL_DEFINE3(sched_setaffinity, pid_t, pid, unsigned int, len,
6398 unsigned long __user *, user_mask_ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006399{
Rusty Russell5a16f3d2008-11-25 02:35:11 +10306400 cpumask_var_t new_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006401 int retval;
6402
Rusty Russell5a16f3d2008-11-25 02:35:11 +10306403 if (!alloc_cpumask_var(&new_mask, GFP_KERNEL))
6404 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006405
Rusty Russell5a16f3d2008-11-25 02:35:11 +10306406 retval = get_user_cpu_mask(user_mask_ptr, len, new_mask);
6407 if (retval == 0)
6408 retval = sched_setaffinity(pid, new_mask);
6409 free_cpumask_var(new_mask);
6410 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006411}
6412
Rusty Russell96f874e2008-11-25 02:35:14 +10306413long sched_getaffinity(pid_t pid, struct cpumask *mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006414{
Ingo Molnar36c8b582006-07-03 00:25:41 -07006415 struct task_struct *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006416 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006417
Gautham R Shenoy95402b32008-01-25 21:08:02 +01006418 get_online_cpus();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006419 read_lock(&tasklist_lock);
6420
6421 retval = -ESRCH;
6422 p = find_process_by_pid(pid);
6423 if (!p)
6424 goto out_unlock;
6425
David Quigleye7834f82006-06-23 02:03:59 -07006426 retval = security_task_getscheduler(p);
6427 if (retval)
6428 goto out_unlock;
6429
Rusty Russell96f874e2008-11-25 02:35:14 +10306430 cpumask_and(mask, &p->cpus_allowed, cpu_online_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006431
6432out_unlock:
6433 read_unlock(&tasklist_lock);
Gautham R Shenoy95402b32008-01-25 21:08:02 +01006434 put_online_cpus();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006435
Ulrich Drepper9531b622007-08-09 11:16:46 +02006436 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006437}
6438
6439/**
6440 * sys_sched_getaffinity - get the cpu affinity of a process
6441 * @pid: pid of the process
6442 * @len: length in bytes of the bitmask pointed to by user_mask_ptr
6443 * @user_mask_ptr: user-space pointer to hold the current cpu mask
6444 */
Heiko Carstens5add95d2009-01-14 14:14:08 +01006445SYSCALL_DEFINE3(sched_getaffinity, pid_t, pid, unsigned int, len,
6446 unsigned long __user *, user_mask_ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006447{
6448 int ret;
Rusty Russellf17c8602008-11-25 02:35:11 +10306449 cpumask_var_t mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006450
Rusty Russellf17c8602008-11-25 02:35:11 +10306451 if (len < cpumask_size())
Linus Torvalds1da177e2005-04-16 15:20:36 -07006452 return -EINVAL;
6453
Rusty Russellf17c8602008-11-25 02:35:11 +10306454 if (!alloc_cpumask_var(&mask, GFP_KERNEL))
6455 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006456
Rusty Russellf17c8602008-11-25 02:35:11 +10306457 ret = sched_getaffinity(pid, mask);
6458 if (ret == 0) {
6459 if (copy_to_user(user_mask_ptr, mask, cpumask_size()))
6460 ret = -EFAULT;
6461 else
6462 ret = cpumask_size();
6463 }
6464 free_cpumask_var(mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006465
Rusty Russellf17c8602008-11-25 02:35:11 +10306466 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006467}
6468
6469/**
6470 * sys_sched_yield - yield the current processor to other threads.
6471 *
Ingo Molnardd41f592007-07-09 18:51:59 +02006472 * This function yields the current CPU to other tasks. If there are no
6473 * other threads running on this CPU then this function will return.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006474 */
Heiko Carstens5add95d2009-01-14 14:14:08 +01006475SYSCALL_DEFINE0(sched_yield)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006476{
Ingo Molnar70b97a72006-07-03 00:25:42 -07006477 struct rq *rq = this_rq_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006478
Ingo Molnar2d723762007-10-15 17:00:12 +02006479 schedstat_inc(rq, yld_count);
Dmitry Adamushko4530d7a2007-10-15 17:00:08 +02006480 current->sched_class->yield_task(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006481
6482 /*
6483 * Since we are going to call schedule() anyway, there's
6484 * no need to preempt or enable interrupts:
6485 */
6486 __release(rq->lock);
Ingo Molnar8a25d5d2006-07-03 00:24:54 -07006487 spin_release(&rq->lock.dep_map, 1, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006488 _raw_spin_unlock(&rq->lock);
6489 preempt_enable_no_resched();
6490
6491 schedule();
6492
6493 return 0;
6494}
6495
Andrew Mortone7b38402006-06-30 01:56:00 -07006496static void __cond_resched(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006497{
Ingo Molnar8e0a43d2006-06-23 02:05:23 -07006498#ifdef CONFIG_DEBUG_SPINLOCK_SLEEP
6499 __might_sleep(__FILE__, __LINE__);
6500#endif
Ingo Molnar5bbcfd92005-07-07 17:57:04 -07006501 /*
6502 * The BKS might be reacquired before we have dropped
6503 * PREEMPT_ACTIVE, which could trigger a second
6504 * cond_resched() call.
6505 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006506 do {
6507 add_preempt_count(PREEMPT_ACTIVE);
6508 schedule();
6509 sub_preempt_count(PREEMPT_ACTIVE);
6510 } while (need_resched());
6511}
6512
Herbert Xu02b67cc2008-01-25 21:08:28 +01006513int __sched _cond_resched(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006514{
Ingo Molnar94142322006-12-29 16:48:13 -08006515 if (need_resched() && !(preempt_count() & PREEMPT_ACTIVE) &&
6516 system_state == SYSTEM_RUNNING) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006517 __cond_resched();
6518 return 1;
6519 }
6520 return 0;
6521}
Herbert Xu02b67cc2008-01-25 21:08:28 +01006522EXPORT_SYMBOL(_cond_resched);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006523
6524/*
6525 * cond_resched_lock() - if a reschedule is pending, drop the given lock,
6526 * call schedule, and on return reacquire the lock.
6527 *
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01006528 * This works OK both with and without CONFIG_PREEMPT. We do strange low-level
Linus Torvalds1da177e2005-04-16 15:20:36 -07006529 * operations here to prevent schedule() from being called twice (once via
6530 * spin_unlock(), once by hand).
6531 */
Ingo Molnar95cdf3b2005-09-10 00:26:11 -07006532int cond_resched_lock(spinlock_t *lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006533{
Nick Piggin95c354f2008-01-30 13:31:20 +01006534 int resched = need_resched() && system_state == SYSTEM_RUNNING;
Jan Kara6df3cec2005-06-13 15:52:32 -07006535 int ret = 0;
6536
Nick Piggin95c354f2008-01-30 13:31:20 +01006537 if (spin_needbreak(lock) || resched) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006538 spin_unlock(lock);
Nick Piggin95c354f2008-01-30 13:31:20 +01006539 if (resched && need_resched())
6540 __cond_resched();
6541 else
6542 cpu_relax();
Jan Kara6df3cec2005-06-13 15:52:32 -07006543 ret = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006544 spin_lock(lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006545 }
Jan Kara6df3cec2005-06-13 15:52:32 -07006546 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006547}
Linus Torvalds1da177e2005-04-16 15:20:36 -07006548EXPORT_SYMBOL(cond_resched_lock);
6549
6550int __sched cond_resched_softirq(void)
6551{
6552 BUG_ON(!in_softirq());
6553
Ingo Molnar94142322006-12-29 16:48:13 -08006554 if (need_resched() && system_state == SYSTEM_RUNNING) {
Thomas Gleixner98d82562007-05-23 13:58:18 -07006555 local_bh_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006556 __cond_resched();
6557 local_bh_disable();
6558 return 1;
6559 }
6560 return 0;
6561}
Linus Torvalds1da177e2005-04-16 15:20:36 -07006562EXPORT_SYMBOL(cond_resched_softirq);
6563
Linus Torvalds1da177e2005-04-16 15:20:36 -07006564/**
6565 * yield - yield the current processor to other threads.
6566 *
Robert P. J. Day72fd4a32007-02-10 01:45:59 -08006567 * This is a shortcut for kernel-space yielding - it marks the
Linus Torvalds1da177e2005-04-16 15:20:36 -07006568 * thread runnable and calls sys_sched_yield().
6569 */
6570void __sched yield(void)
6571{
6572 set_current_state(TASK_RUNNING);
6573 sys_sched_yield();
6574}
Linus Torvalds1da177e2005-04-16 15:20:36 -07006575EXPORT_SYMBOL(yield);
6576
6577/*
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01006578 * This task is about to go to sleep on IO. Increment rq->nr_iowait so
Linus Torvalds1da177e2005-04-16 15:20:36 -07006579 * that process accounting knows that this is a task in IO wait state.
6580 *
6581 * But don't do that if it is a deliberate, throttling IO wait (this task
6582 * has set its backing_dev_info: the queue against which it should throttle)
6583 */
6584void __sched io_schedule(void)
6585{
Ingo Molnar70b97a72006-07-03 00:25:42 -07006586 struct rq *rq = &__raw_get_cpu_var(runqueues);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006587
Shailabh Nagar0ff92242006-07-14 00:24:37 -07006588 delayacct_blkio_start();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006589 atomic_inc(&rq->nr_iowait);
6590 schedule();
6591 atomic_dec(&rq->nr_iowait);
Shailabh Nagar0ff92242006-07-14 00:24:37 -07006592 delayacct_blkio_end();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006593}
Linus Torvalds1da177e2005-04-16 15:20:36 -07006594EXPORT_SYMBOL(io_schedule);
6595
6596long __sched io_schedule_timeout(long timeout)
6597{
Ingo Molnar70b97a72006-07-03 00:25:42 -07006598 struct rq *rq = &__raw_get_cpu_var(runqueues);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006599 long ret;
6600
Shailabh Nagar0ff92242006-07-14 00:24:37 -07006601 delayacct_blkio_start();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006602 atomic_inc(&rq->nr_iowait);
6603 ret = schedule_timeout(timeout);
6604 atomic_dec(&rq->nr_iowait);
Shailabh Nagar0ff92242006-07-14 00:24:37 -07006605 delayacct_blkio_end();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006606 return ret;
6607}
6608
6609/**
6610 * sys_sched_get_priority_max - return maximum RT priority.
6611 * @policy: scheduling class.
6612 *
6613 * this syscall returns the maximum rt_priority that can be used
6614 * by a given scheduling class.
6615 */
Heiko Carstens5add95d2009-01-14 14:14:08 +01006616SYSCALL_DEFINE1(sched_get_priority_max, int, policy)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006617{
6618 int ret = -EINVAL;
6619
6620 switch (policy) {
6621 case SCHED_FIFO:
6622 case SCHED_RR:
6623 ret = MAX_USER_RT_PRIO-1;
6624 break;
6625 case SCHED_NORMAL:
Ingo Molnarb0a94992006-01-14 13:20:41 -08006626 case SCHED_BATCH:
Ingo Molnardd41f592007-07-09 18:51:59 +02006627 case SCHED_IDLE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07006628 ret = 0;
6629 break;
6630 }
6631 return ret;
6632}
6633
6634/**
6635 * sys_sched_get_priority_min - return minimum RT priority.
6636 * @policy: scheduling class.
6637 *
6638 * this syscall returns the minimum rt_priority that can be used
6639 * by a given scheduling class.
6640 */
Heiko Carstens5add95d2009-01-14 14:14:08 +01006641SYSCALL_DEFINE1(sched_get_priority_min, int, policy)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006642{
6643 int ret = -EINVAL;
6644
6645 switch (policy) {
6646 case SCHED_FIFO:
6647 case SCHED_RR:
6648 ret = 1;
6649 break;
6650 case SCHED_NORMAL:
Ingo Molnarb0a94992006-01-14 13:20:41 -08006651 case SCHED_BATCH:
Ingo Molnardd41f592007-07-09 18:51:59 +02006652 case SCHED_IDLE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07006653 ret = 0;
6654 }
6655 return ret;
6656}
6657
6658/**
6659 * sys_sched_rr_get_interval - return the default timeslice of a process.
6660 * @pid: pid of the process.
6661 * @interval: userspace pointer to the timeslice value.
6662 *
6663 * this syscall writes the default timeslice value of a given process
6664 * into the user-space timespec buffer. A value of '0' means infinity.
6665 */
Heiko Carstens17da2bd2009-01-14 14:14:10 +01006666SYSCALL_DEFINE2(sched_rr_get_interval, pid_t, pid,
Heiko Carstens754fe8d2009-01-14 14:14:09 +01006667 struct timespec __user *, interval)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006668{
Ingo Molnar36c8b582006-07-03 00:25:41 -07006669 struct task_struct *p;
Dmitry Adamushkoa4ec24b2007-10-15 17:00:13 +02006670 unsigned int time_slice;
Andi Kleen3a5c3592007-10-15 17:00:14 +02006671 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006672 struct timespec t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006673
6674 if (pid < 0)
Andi Kleen3a5c3592007-10-15 17:00:14 +02006675 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006676
6677 retval = -ESRCH;
6678 read_lock(&tasklist_lock);
6679 p = find_process_by_pid(pid);
6680 if (!p)
6681 goto out_unlock;
6682
6683 retval = security_task_getscheduler(p);
6684 if (retval)
6685 goto out_unlock;
6686
Ingo Molnar77034932007-12-04 17:04:39 +01006687 /*
6688 * Time slice is 0 for SCHED_FIFO tasks and for SCHED_OTHER
6689 * tasks that are on an otherwise idle runqueue:
6690 */
6691 time_slice = 0;
6692 if (p->policy == SCHED_RR) {
Dmitry Adamushkoa4ec24b2007-10-15 17:00:13 +02006693 time_slice = DEF_TIMESLICE;
Miao Xie1868f952008-03-07 09:35:06 +08006694 } else if (p->policy != SCHED_FIFO) {
Dmitry Adamushkoa4ec24b2007-10-15 17:00:13 +02006695 struct sched_entity *se = &p->se;
6696 unsigned long flags;
6697 struct rq *rq;
6698
6699 rq = task_rq_lock(p, &flags);
Ingo Molnar77034932007-12-04 17:04:39 +01006700 if (rq->cfs.load.weight)
6701 time_slice = NS_TO_JIFFIES(sched_slice(&rq->cfs, se));
Dmitry Adamushkoa4ec24b2007-10-15 17:00:13 +02006702 task_rq_unlock(rq, &flags);
6703 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006704 read_unlock(&tasklist_lock);
Dmitry Adamushkoa4ec24b2007-10-15 17:00:13 +02006705 jiffies_to_timespec(time_slice, &t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006706 retval = copy_to_user(interval, &t, sizeof(t)) ? -EFAULT : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006707 return retval;
Andi Kleen3a5c3592007-10-15 17:00:14 +02006708
Linus Torvalds1da177e2005-04-16 15:20:36 -07006709out_unlock:
6710 read_unlock(&tasklist_lock);
6711 return retval;
6712}
6713
Steven Rostedt7c731e02008-05-12 21:20:41 +02006714static const char stat_nam[] = TASK_STATE_TO_CHAR_STR;
Ingo Molnar36c8b582006-07-03 00:25:41 -07006715
Ingo Molnar82a1fcb2008-01-25 21:08:02 +01006716void sched_show_task(struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006717{
Linus Torvalds1da177e2005-04-16 15:20:36 -07006718 unsigned long free = 0;
Ingo Molnar36c8b582006-07-03 00:25:41 -07006719 unsigned state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006720
Linus Torvalds1da177e2005-04-16 15:20:36 -07006721 state = p->state ? __ffs(p->state) + 1 : 0;
Ingo Molnarcc4ea792007-10-18 21:32:56 +02006722 printk(KERN_INFO "%-13.13s %c", p->comm,
Andreas Mohr2ed6e342006-07-10 04:43:52 -07006723 state < sizeof(stat_nam) - 1 ? stat_nam[state] : '?');
Ingo Molnar4bd77322007-07-11 21:21:47 +02006724#if BITS_PER_LONG == 32
Linus Torvalds1da177e2005-04-16 15:20:36 -07006725 if (state == TASK_RUNNING)
Ingo Molnarcc4ea792007-10-18 21:32:56 +02006726 printk(KERN_CONT " running ");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006727 else
Ingo Molnarcc4ea792007-10-18 21:32:56 +02006728 printk(KERN_CONT " %08lx ", thread_saved_pc(p));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006729#else
6730 if (state == TASK_RUNNING)
Ingo Molnarcc4ea792007-10-18 21:32:56 +02006731 printk(KERN_CONT " running task ");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006732 else
Ingo Molnarcc4ea792007-10-18 21:32:56 +02006733 printk(KERN_CONT " %016lx ", thread_saved_pc(p));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006734#endif
6735#ifdef CONFIG_DEBUG_STACK_USAGE
Eric Sandeen7c9f8862008-04-22 16:38:23 -05006736 free = stack_not_used(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006737#endif
David Rientjesaa47b7e2009-05-04 01:38:05 -07006738 printk(KERN_CONT "%5lu %5d %6d 0x%08lx\n", free,
6739 task_pid_nr(p), task_pid_nr(p->real_parent),
6740 (unsigned long)task_thread_info(p)->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006741
Nick Piggin5fb5e6d2008-01-25 21:08:34 +01006742 show_stack(p, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006743}
6744
Ingo Molnare59e2ae2006-12-06 20:35:59 -08006745void show_state_filter(unsigned long state_filter)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006746{
Ingo Molnar36c8b582006-07-03 00:25:41 -07006747 struct task_struct *g, *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006748
Ingo Molnar4bd77322007-07-11 21:21:47 +02006749#if BITS_PER_LONG == 32
6750 printk(KERN_INFO
6751 " task PC stack pid father\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006752#else
Ingo Molnar4bd77322007-07-11 21:21:47 +02006753 printk(KERN_INFO
6754 " task PC stack pid father\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006755#endif
6756 read_lock(&tasklist_lock);
6757 do_each_thread(g, p) {
6758 /*
6759 * reset the NMI-timeout, listing all files on a slow
6760 * console might take alot of time:
6761 */
6762 touch_nmi_watchdog();
Ingo Molnar39bc89f2007-04-25 20:50:03 -07006763 if (!state_filter || (p->state & state_filter))
Ingo Molnar82a1fcb2008-01-25 21:08:02 +01006764 sched_show_task(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006765 } while_each_thread(g, p);
6766
Jeremy Fitzhardinge04c91672007-05-08 00:28:05 -07006767 touch_all_softlockup_watchdogs();
6768
Ingo Molnardd41f592007-07-09 18:51:59 +02006769#ifdef CONFIG_SCHED_DEBUG
6770 sysrq_sched_debug_show();
6771#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07006772 read_unlock(&tasklist_lock);
Ingo Molnare59e2ae2006-12-06 20:35:59 -08006773 /*
6774 * Only show locks if all tasks are dumped:
6775 */
6776 if (state_filter == -1)
6777 debug_show_all_locks();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006778}
6779
Ingo Molnar1df21052007-07-09 18:51:58 +02006780void __cpuinit init_idle_bootup_task(struct task_struct *idle)
6781{
Ingo Molnardd41f592007-07-09 18:51:59 +02006782 idle->sched_class = &idle_sched_class;
Ingo Molnar1df21052007-07-09 18:51:58 +02006783}
6784
Ingo Molnarf340c0d2005-06-28 16:40:42 +02006785/**
6786 * init_idle - set up an idle thread for a given CPU
6787 * @idle: task in question
6788 * @cpu: cpu the idle task belongs to
6789 *
6790 * NOTE: this function does not set the idle thread's NEED_RESCHED
6791 * flag, to make booting more robust.
6792 */
Nick Piggin5c1e1762006-10-03 01:14:04 -07006793void __cpuinit init_idle(struct task_struct *idle, int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006794{
Ingo Molnar70b97a72006-07-03 00:25:42 -07006795 struct rq *rq = cpu_rq(cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006796 unsigned long flags;
6797
Ingo Molnar5cbd54e2008-11-12 20:05:50 +01006798 spin_lock_irqsave(&rq->lock, flags);
6799
Ingo Molnardd41f592007-07-09 18:51:59 +02006800 __sched_fork(idle);
6801 idle->se.exec_start = sched_clock();
6802
Ingo Molnarb29739f2006-06-27 02:54:51 -07006803 idle->prio = idle->normal_prio = MAX_PRIO;
Rusty Russell96f874e2008-11-25 02:35:14 +10306804 cpumask_copy(&idle->cpus_allowed, cpumask_of(cpu));
Ingo Molnardd41f592007-07-09 18:51:59 +02006805 __set_task_cpu(idle, cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006806
Linus Torvalds1da177e2005-04-16 15:20:36 -07006807 rq->curr = rq->idle = idle;
Nick Piggin4866cde2005-06-25 14:57:23 -07006808#if defined(CONFIG_SMP) && defined(__ARCH_WANT_UNLOCKED_CTXSW)
6809 idle->oncpu = 1;
6810#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07006811 spin_unlock_irqrestore(&rq->lock, flags);
6812
6813 /* Set the preempt count _outside_ the spinlocks! */
Linus Torvalds8e3e0762008-05-10 20:58:02 -07006814#if defined(CONFIG_PREEMPT)
6815 task_thread_info(idle)->preempt_count = (idle->lock_depth >= 0);
6816#else
Al Viroa1261f52005-11-13 16:06:55 -08006817 task_thread_info(idle)->preempt_count = 0;
Linus Torvalds8e3e0762008-05-10 20:58:02 -07006818#endif
Ingo Molnardd41f592007-07-09 18:51:59 +02006819 /*
6820 * The idle tasks have their own, simple scheduling class:
6821 */
6822 idle->sched_class = &idle_sched_class;
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01006823 ftrace_graph_init_task(idle);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006824}
6825
6826/*
6827 * In a system that switches off the HZ timer nohz_cpu_mask
6828 * indicates which cpus entered this state. This is used
6829 * in the rcu update to wait only for active cpus. For system
6830 * which do not switch off the HZ timer nohz_cpu_mask should
Rusty Russell6a7b3dc2008-11-25 02:35:04 +10306831 * always be CPU_BITS_NONE.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006832 */
Rusty Russell6a7b3dc2008-11-25 02:35:04 +10306833cpumask_var_t nohz_cpu_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006834
Ingo Molnar19978ca2007-11-09 22:39:38 +01006835/*
6836 * Increase the granularity value when there are more CPUs,
6837 * because with more CPUs the 'effective latency' as visible
6838 * to users decreases. But the relationship is not linear,
6839 * so pick a second-best guess by going with the log2 of the
6840 * number of CPUs.
6841 *
6842 * This idea comes from the SD scheduler of Con Kolivas:
6843 */
6844static inline void sched_init_granularity(void)
6845{
6846 unsigned int factor = 1 + ilog2(num_online_cpus());
6847 const unsigned long limit = 200000000;
6848
6849 sysctl_sched_min_granularity *= factor;
6850 if (sysctl_sched_min_granularity > limit)
6851 sysctl_sched_min_granularity = limit;
6852
6853 sysctl_sched_latency *= factor;
6854 if (sysctl_sched_latency > limit)
6855 sysctl_sched_latency = limit;
6856
6857 sysctl_sched_wakeup_granularity *= factor;
Peter Zijlstra55cd5342008-08-04 08:54:26 +02006858
6859 sysctl_sched_shares_ratelimit *= factor;
Ingo Molnar19978ca2007-11-09 22:39:38 +01006860}
6861
Linus Torvalds1da177e2005-04-16 15:20:36 -07006862#ifdef CONFIG_SMP
6863/*
6864 * This is how migration works:
6865 *
Ingo Molnar70b97a72006-07-03 00:25:42 -07006866 * 1) we queue a struct migration_req structure in the source CPU's
Linus Torvalds1da177e2005-04-16 15:20:36 -07006867 * runqueue and wake up that CPU's migration thread.
6868 * 2) we down() the locked semaphore => thread blocks.
6869 * 3) migration thread wakes up (implicitly it forces the migrated
6870 * thread off the CPU)
6871 * 4) it gets the migration request and checks whether the migrated
6872 * task is still in the wrong runqueue.
6873 * 5) if it's in the wrong runqueue then the migration thread removes
6874 * it and puts it into the right queue.
6875 * 6) migration thread up()s the semaphore.
6876 * 7) we wake up and the migration is done.
6877 */
6878
6879/*
6880 * Change a given task's CPU affinity. Migrate the thread to a
6881 * proper CPU and schedule it away if the CPU it's executing on
6882 * is removed from the allowed bitmask.
6883 *
6884 * NOTE: the caller must have a valid reference to the task, the
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01006885 * task must not exit() & deallocate itself prematurely. The
Linus Torvalds1da177e2005-04-16 15:20:36 -07006886 * call is not atomic; no spinlocks may be held.
6887 */
Rusty Russell96f874e2008-11-25 02:35:14 +10306888int set_cpus_allowed_ptr(struct task_struct *p, const struct cpumask *new_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006889{
Ingo Molnar70b97a72006-07-03 00:25:42 -07006890 struct migration_req req;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006891 unsigned long flags;
Ingo Molnar70b97a72006-07-03 00:25:42 -07006892 struct rq *rq;
Ingo Molnar48f24c42006-07-03 00:25:40 -07006893 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006894
6895 rq = task_rq_lock(p, &flags);
Rusty Russell96f874e2008-11-25 02:35:14 +10306896 if (!cpumask_intersects(new_mask, cpu_online_mask)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006897 ret = -EINVAL;
6898 goto out;
6899 }
6900
David Rientjes9985b0b2008-06-05 12:57:11 -07006901 if (unlikely((p->flags & PF_THREAD_BOUND) && p != current &&
Rusty Russell96f874e2008-11-25 02:35:14 +10306902 !cpumask_equal(&p->cpus_allowed, new_mask))) {
David Rientjes9985b0b2008-06-05 12:57:11 -07006903 ret = -EINVAL;
6904 goto out;
6905 }
6906
Gregory Haskins73fe6aa2008-01-25 21:08:07 +01006907 if (p->sched_class->set_cpus_allowed)
Mike Traviscd8ba7c2008-03-26 14:23:49 -07006908 p->sched_class->set_cpus_allowed(p, new_mask);
Gregory Haskins73fe6aa2008-01-25 21:08:07 +01006909 else {
Rusty Russell96f874e2008-11-25 02:35:14 +10306910 cpumask_copy(&p->cpus_allowed, new_mask);
6911 p->rt.nr_cpus_allowed = cpumask_weight(new_mask);
Gregory Haskins73fe6aa2008-01-25 21:08:07 +01006912 }
6913
Linus Torvalds1da177e2005-04-16 15:20:36 -07006914 /* Can the task run on the task's current CPU? If so, we're done */
Rusty Russell96f874e2008-11-25 02:35:14 +10306915 if (cpumask_test_cpu(task_cpu(p), new_mask))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006916 goto out;
6917
Rusty Russell1e5ce4f2008-11-25 02:35:03 +10306918 if (migrate_task(p, cpumask_any_and(cpu_online_mask, new_mask), &req)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006919 /* Need help from migration thread: drop lock and wait. */
6920 task_rq_unlock(rq, &flags);
6921 wake_up_process(rq->migration_thread);
6922 wait_for_completion(&req.done);
6923 tlb_migrate_finish(p->mm);
6924 return 0;
6925 }
6926out:
6927 task_rq_unlock(rq, &flags);
Ingo Molnar48f24c42006-07-03 00:25:40 -07006928
Linus Torvalds1da177e2005-04-16 15:20:36 -07006929 return ret;
6930}
Mike Traviscd8ba7c2008-03-26 14:23:49 -07006931EXPORT_SYMBOL_GPL(set_cpus_allowed_ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006932
6933/*
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01006934 * Move (not current) task off this cpu, onto dest cpu. We're doing
Linus Torvalds1da177e2005-04-16 15:20:36 -07006935 * this because either it can't run here any more (set_cpus_allowed()
6936 * away from this CPU, or CPU going down), or because we're
6937 * attempting to rebalance this task on exec (sched_exec).
6938 *
6939 * So we race with normal scheduler movements, but that's OK, as long
6940 * as the task is no longer on this CPU.
Kirill Korotaevefc30812006-06-27 02:54:32 -07006941 *
6942 * Returns non-zero if task was successfully migrated.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006943 */
Kirill Korotaevefc30812006-06-27 02:54:32 -07006944static int __migrate_task(struct task_struct *p, int src_cpu, int dest_cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006945{
Ingo Molnar70b97a72006-07-03 00:25:42 -07006946 struct rq *rq_dest, *rq_src;
Ingo Molnardd41f592007-07-09 18:51:59 +02006947 int ret = 0, on_rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006948
Max Krasnyanskye761b772008-07-15 04:43:49 -07006949 if (unlikely(!cpu_active(dest_cpu)))
Kirill Korotaevefc30812006-06-27 02:54:32 -07006950 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006951
6952 rq_src = cpu_rq(src_cpu);
6953 rq_dest = cpu_rq(dest_cpu);
6954
6955 double_rq_lock(rq_src, rq_dest);
6956 /* Already moved. */
6957 if (task_cpu(p) != src_cpu)
Linus Torvaldsb1e38732008-07-10 11:25:03 -07006958 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006959 /* Affinity changed (again). */
Rusty Russell96f874e2008-11-25 02:35:14 +10306960 if (!cpumask_test_cpu(dest_cpu, &p->cpus_allowed))
Linus Torvaldsb1e38732008-07-10 11:25:03 -07006961 goto fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006962
Ingo Molnardd41f592007-07-09 18:51:59 +02006963 on_rq = p->se.on_rq;
Ingo Molnar6e82a3b2007-08-09 11:16:51 +02006964 if (on_rq)
Ingo Molnar2e1cb742007-08-09 11:16:49 +02006965 deactivate_task(rq_src, p, 0);
Ingo Molnar6e82a3b2007-08-09 11:16:51 +02006966
Linus Torvalds1da177e2005-04-16 15:20:36 -07006967 set_task_cpu(p, dest_cpu);
Ingo Molnardd41f592007-07-09 18:51:59 +02006968 if (on_rq) {
6969 activate_task(rq_dest, p, 0);
Peter Zijlstra15afe092008-09-20 23:38:02 +02006970 check_preempt_curr(rq_dest, p, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006971 }
Linus Torvaldsb1e38732008-07-10 11:25:03 -07006972done:
Kirill Korotaevefc30812006-06-27 02:54:32 -07006973 ret = 1;
Linus Torvaldsb1e38732008-07-10 11:25:03 -07006974fail:
Linus Torvalds1da177e2005-04-16 15:20:36 -07006975 double_rq_unlock(rq_src, rq_dest);
Kirill Korotaevefc30812006-06-27 02:54:32 -07006976 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006977}
6978
6979/*
6980 * migration_thread - this is a highprio system thread that performs
6981 * thread migration by bumping thread off CPU then 'pushing' onto
6982 * another runqueue.
6983 */
Ingo Molnar95cdf3b2005-09-10 00:26:11 -07006984static int migration_thread(void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006985{
Linus Torvalds1da177e2005-04-16 15:20:36 -07006986 int cpu = (long)data;
Ingo Molnar70b97a72006-07-03 00:25:42 -07006987 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006988
6989 rq = cpu_rq(cpu);
6990 BUG_ON(rq->migration_thread != current);
6991
6992 set_current_state(TASK_INTERRUPTIBLE);
6993 while (!kthread_should_stop()) {
Ingo Molnar70b97a72006-07-03 00:25:42 -07006994 struct migration_req *req;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006995 struct list_head *head;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006996
Linus Torvalds1da177e2005-04-16 15:20:36 -07006997 spin_lock_irq(&rq->lock);
6998
6999 if (cpu_is_offline(cpu)) {
7000 spin_unlock_irq(&rq->lock);
7001 goto wait_to_die;
7002 }
7003
7004 if (rq->active_balance) {
7005 active_load_balance(rq, cpu);
7006 rq->active_balance = 0;
7007 }
7008
7009 head = &rq->migration_queue;
7010
7011 if (list_empty(head)) {
7012 spin_unlock_irq(&rq->lock);
7013 schedule();
7014 set_current_state(TASK_INTERRUPTIBLE);
7015 continue;
7016 }
Ingo Molnar70b97a72006-07-03 00:25:42 -07007017 req = list_entry(head->next, struct migration_req, list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007018 list_del_init(head->next);
7019
Nick Piggin674311d2005-06-25 14:57:27 -07007020 spin_unlock(&rq->lock);
7021 __migrate_task(req->task, cpu, req->dest_cpu);
7022 local_irq_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07007023
7024 complete(&req->done);
7025 }
7026 __set_current_state(TASK_RUNNING);
7027 return 0;
7028
7029wait_to_die:
7030 /* Wait for kthread_stop */
7031 set_current_state(TASK_INTERRUPTIBLE);
7032 while (!kthread_should_stop()) {
7033 schedule();
7034 set_current_state(TASK_INTERRUPTIBLE);
7035 }
7036 __set_current_state(TASK_RUNNING);
7037 return 0;
7038}
7039
7040#ifdef CONFIG_HOTPLUG_CPU
Oleg Nesterovf7b4cdd2007-10-16 23:30:56 -07007041
7042static int __migrate_task_irq(struct task_struct *p, int src_cpu, int dest_cpu)
7043{
7044 int ret;
7045
7046 local_irq_disable();
7047 ret = __migrate_task(p, src_cpu, dest_cpu);
7048 local_irq_enable();
7049 return ret;
7050}
7051
Kirill Korotaev054b9102006-12-10 02:20:11 -08007052/*
Robert P. J. Day3a4fa0a2007-10-19 23:10:43 +02007053 * Figure out where task on dead CPU should go, use force if necessary.
Kirill Korotaev054b9102006-12-10 02:20:11 -08007054 */
Ingo Molnar48f24c42006-07-03 00:25:40 -07007055static void move_task_off_dead_cpu(int dead_cpu, struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007056{
Ingo Molnar70b97a72006-07-03 00:25:42 -07007057 int dest_cpu;
Mike Travis6ca09df2008-12-31 18:08:45 -08007058 const struct cpumask *nodemask = cpumask_of_node(cpu_to_node(dead_cpu));
Linus Torvalds1da177e2005-04-16 15:20:36 -07007059
Rusty Russelle76bd8d2008-11-25 02:35:11 +10307060again:
7061 /* Look for allowed, online CPU in same node. */
7062 for_each_cpu_and(dest_cpu, nodemask, cpu_online_mask)
7063 if (cpumask_test_cpu(dest_cpu, &p->cpus_allowed))
7064 goto move;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007065
Rusty Russelle76bd8d2008-11-25 02:35:11 +10307066 /* Any allowed, online CPU? */
7067 dest_cpu = cpumask_any_and(&p->cpus_allowed, cpu_online_mask);
7068 if (dest_cpu < nr_cpu_ids)
7069 goto move;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007070
Rusty Russelle76bd8d2008-11-25 02:35:11 +10307071 /* No more Mr. Nice Guy. */
7072 if (dest_cpu >= nr_cpu_ids) {
Rusty Russelle76bd8d2008-11-25 02:35:11 +10307073 cpuset_cpus_allowed_locked(p, &p->cpus_allowed);
7074 dest_cpu = cpumask_any_and(cpu_online_mask, &p->cpus_allowed);
Mike Travisf9a86fc2008-04-04 18:11:07 -07007075
Rusty Russelle76bd8d2008-11-25 02:35:11 +10307076 /*
7077 * Don't tell them about moving exiting tasks or
7078 * kernel threads (both mm NULL), since they never
7079 * leave kernel.
7080 */
7081 if (p->mm && printk_ratelimit()) {
7082 printk(KERN_INFO "process %d (%s) no "
7083 "longer affine to cpu%d\n",
7084 task_pid_nr(p), p->comm, dead_cpu);
Andi Kleen3a5c3592007-10-15 17:00:14 +02007085 }
Rusty Russelle76bd8d2008-11-25 02:35:11 +10307086 }
7087
7088move:
7089 /* It can have affinity changed while we were choosing. */
7090 if (unlikely(!__migrate_task_irq(p, dead_cpu, dest_cpu)))
7091 goto again;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007092}
7093
7094/*
7095 * While a dead CPU has no uninterruptible tasks queued at this point,
7096 * it might still have a nonzero ->nr_uninterruptible counter, because
7097 * for performance reasons the counter is not stricly tracking tasks to
7098 * their home CPUs. So we just add the counter to another CPU's counter,
7099 * to keep the global sum constant after CPU-down:
7100 */
Ingo Molnar70b97a72006-07-03 00:25:42 -07007101static void migrate_nr_uninterruptible(struct rq *rq_src)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007102{
Rusty Russell1e5ce4f2008-11-25 02:35:03 +10307103 struct rq *rq_dest = cpu_rq(cpumask_any(cpu_online_mask));
Linus Torvalds1da177e2005-04-16 15:20:36 -07007104 unsigned long flags;
7105
7106 local_irq_save(flags);
7107 double_rq_lock(rq_src, rq_dest);
7108 rq_dest->nr_uninterruptible += rq_src->nr_uninterruptible;
7109 rq_src->nr_uninterruptible = 0;
7110 double_rq_unlock(rq_src, rq_dest);
7111 local_irq_restore(flags);
7112}
7113
7114/* Run through task list and migrate tasks from the dead cpu. */
7115static void migrate_live_tasks(int src_cpu)
7116{
Ingo Molnar48f24c42006-07-03 00:25:40 -07007117 struct task_struct *p, *t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007118
Oleg Nesterovf7b4cdd2007-10-16 23:30:56 -07007119 read_lock(&tasklist_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007120
Ingo Molnar48f24c42006-07-03 00:25:40 -07007121 do_each_thread(t, p) {
7122 if (p == current)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007123 continue;
7124
Ingo Molnar48f24c42006-07-03 00:25:40 -07007125 if (task_cpu(p) == src_cpu)
7126 move_task_off_dead_cpu(src_cpu, p);
7127 } while_each_thread(t, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007128
Oleg Nesterovf7b4cdd2007-10-16 23:30:56 -07007129 read_unlock(&tasklist_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007130}
7131
Ingo Molnardd41f592007-07-09 18:51:59 +02007132/*
7133 * Schedules idle task to be the next runnable task on current CPU.
Dmitry Adamushko94bc9a72007-11-15 20:57:40 +01007134 * It does so by boosting its priority to highest possible.
7135 * Used by CPU offline code.
Linus Torvalds1da177e2005-04-16 15:20:36 -07007136 */
7137void sched_idle_next(void)
7138{
Ingo Molnar48f24c42006-07-03 00:25:40 -07007139 int this_cpu = smp_processor_id();
Ingo Molnar70b97a72006-07-03 00:25:42 -07007140 struct rq *rq = cpu_rq(this_cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007141 struct task_struct *p = rq->idle;
7142 unsigned long flags;
7143
7144 /* cpu has to be offline */
Ingo Molnar48f24c42006-07-03 00:25:40 -07007145 BUG_ON(cpu_online(this_cpu));
Linus Torvalds1da177e2005-04-16 15:20:36 -07007146
Ingo Molnar48f24c42006-07-03 00:25:40 -07007147 /*
7148 * Strictly not necessary since rest of the CPUs are stopped by now
7149 * and interrupts disabled on the current cpu.
Linus Torvalds1da177e2005-04-16 15:20:36 -07007150 */
7151 spin_lock_irqsave(&rq->lock, flags);
7152
Ingo Molnardd41f592007-07-09 18:51:59 +02007153 __setscheduler(rq, p, SCHED_FIFO, MAX_RT_PRIO-1);
Ingo Molnar48f24c42006-07-03 00:25:40 -07007154
Dmitry Adamushko94bc9a72007-11-15 20:57:40 +01007155 update_rq_clock(rq);
7156 activate_task(rq, p, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007157
7158 spin_unlock_irqrestore(&rq->lock, flags);
7159}
7160
Ingo Molnar48f24c42006-07-03 00:25:40 -07007161/*
7162 * Ensures that the idle task is using init_mm right before its cpu goes
Linus Torvalds1da177e2005-04-16 15:20:36 -07007163 * offline.
7164 */
7165void idle_task_exit(void)
7166{
7167 struct mm_struct *mm = current->active_mm;
7168
7169 BUG_ON(cpu_online(smp_processor_id()));
7170
7171 if (mm != &init_mm)
7172 switch_mm(mm, &init_mm, current);
7173 mmdrop(mm);
7174}
7175
Kirill Korotaev054b9102006-12-10 02:20:11 -08007176/* called under rq->lock with disabled interrupts */
Ingo Molnar36c8b582006-07-03 00:25:41 -07007177static void migrate_dead(unsigned int dead_cpu, struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007178{
Ingo Molnar70b97a72006-07-03 00:25:42 -07007179 struct rq *rq = cpu_rq(dead_cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007180
7181 /* Must be exiting, otherwise would be on tasklist. */
Eugene Teo270f7222007-10-18 23:40:38 -07007182 BUG_ON(!p->exit_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007183
7184 /* Cannot have done final schedule yet: would have vanished. */
Oleg Nesterovc394cc92006-09-29 02:01:11 -07007185 BUG_ON(p->state == TASK_DEAD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007186
Ingo Molnar48f24c42006-07-03 00:25:40 -07007187 get_task_struct(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007188
7189 /*
7190 * Drop lock around migration; if someone else moves it,
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01007191 * that's OK. No task can be added to this CPU, so iteration is
Linus Torvalds1da177e2005-04-16 15:20:36 -07007192 * fine.
7193 */
Oleg Nesterovf7b4cdd2007-10-16 23:30:56 -07007194 spin_unlock_irq(&rq->lock);
Ingo Molnar48f24c42006-07-03 00:25:40 -07007195 move_task_off_dead_cpu(dead_cpu, p);
Oleg Nesterovf7b4cdd2007-10-16 23:30:56 -07007196 spin_lock_irq(&rq->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007197
Ingo Molnar48f24c42006-07-03 00:25:40 -07007198 put_task_struct(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007199}
7200
7201/* release_task() removes task from tasklist, so we won't find dead tasks. */
7202static void migrate_dead_tasks(unsigned int dead_cpu)
7203{
Ingo Molnar70b97a72006-07-03 00:25:42 -07007204 struct rq *rq = cpu_rq(dead_cpu);
Ingo Molnardd41f592007-07-09 18:51:59 +02007205 struct task_struct *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007206
Ingo Molnardd41f592007-07-09 18:51:59 +02007207 for ( ; ; ) {
7208 if (!rq->nr_running)
7209 break;
Ingo Molnara8e504d2007-08-09 11:16:47 +02007210 update_rq_clock(rq);
Wang Chenb67802e2009-03-02 13:55:26 +08007211 next = pick_next_task(rq);
Ingo Molnardd41f592007-07-09 18:51:59 +02007212 if (!next)
7213 break;
Dmitry Adamushko79c53792008-06-29 00:16:56 +02007214 next->sched_class->put_prev_task(rq, next);
Ingo Molnardd41f592007-07-09 18:51:59 +02007215 migrate_dead(dead_cpu, next);
Nick Piggine692ab52007-07-26 13:40:43 +02007216
Linus Torvalds1da177e2005-04-16 15:20:36 -07007217 }
7218}
Thomas Gleixnerdce48a82009-04-11 10:43:41 +02007219
7220/*
7221 * remove the tasks which were accounted by rq from calc_load_tasks.
7222 */
7223static void calc_global_load_remove(struct rq *rq)
7224{
7225 atomic_long_sub(rq->calc_load_active, &calc_load_tasks);
7226}
Linus Torvalds1da177e2005-04-16 15:20:36 -07007227#endif /* CONFIG_HOTPLUG_CPU */
7228
Nick Piggine692ab52007-07-26 13:40:43 +02007229#if defined(CONFIG_SCHED_DEBUG) && defined(CONFIG_SYSCTL)
7230
7231static struct ctl_table sd_ctl_dir[] = {
Alexey Dobriyane0361852007-08-09 11:16:46 +02007232 {
7233 .procname = "sched_domain",
Eric W. Biedermanc57baf12007-08-23 15:18:02 +02007234 .mode = 0555,
Alexey Dobriyane0361852007-08-09 11:16:46 +02007235 },
Ingo Molnar38605ca2007-10-29 21:18:11 +01007236 {0, },
Nick Piggine692ab52007-07-26 13:40:43 +02007237};
7238
7239static struct ctl_table sd_ctl_root[] = {
Alexey Dobriyane0361852007-08-09 11:16:46 +02007240 {
Eric W. Biedermanc57baf12007-08-23 15:18:02 +02007241 .ctl_name = CTL_KERN,
Alexey Dobriyane0361852007-08-09 11:16:46 +02007242 .procname = "kernel",
Eric W. Biedermanc57baf12007-08-23 15:18:02 +02007243 .mode = 0555,
Alexey Dobriyane0361852007-08-09 11:16:46 +02007244 .child = sd_ctl_dir,
7245 },
Ingo Molnar38605ca2007-10-29 21:18:11 +01007246 {0, },
Nick Piggine692ab52007-07-26 13:40:43 +02007247};
7248
7249static struct ctl_table *sd_alloc_ctl_entry(int n)
7250{
7251 struct ctl_table *entry =
Milton Miller5cf9f062007-10-15 17:00:19 +02007252 kcalloc(n, sizeof(struct ctl_table), GFP_KERNEL);
Nick Piggine692ab52007-07-26 13:40:43 +02007253
Nick Piggine692ab52007-07-26 13:40:43 +02007254 return entry;
7255}
7256
Milton Miller6382bc92007-10-15 17:00:19 +02007257static void sd_free_ctl_entry(struct ctl_table **tablep)
7258{
Milton Millercd790072007-10-17 16:55:11 +02007259 struct ctl_table *entry;
Milton Miller6382bc92007-10-15 17:00:19 +02007260
Milton Millercd790072007-10-17 16:55:11 +02007261 /*
7262 * In the intermediate directories, both the child directory and
7263 * procname are dynamically allocated and could fail but the mode
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01007264 * will always be set. In the lowest directory the names are
Milton Millercd790072007-10-17 16:55:11 +02007265 * static strings and all have proc handlers.
7266 */
7267 for (entry = *tablep; entry->mode; entry++) {
Milton Miller6382bc92007-10-15 17:00:19 +02007268 if (entry->child)
7269 sd_free_ctl_entry(&entry->child);
Milton Millercd790072007-10-17 16:55:11 +02007270 if (entry->proc_handler == NULL)
7271 kfree(entry->procname);
7272 }
Milton Miller6382bc92007-10-15 17:00:19 +02007273
7274 kfree(*tablep);
7275 *tablep = NULL;
7276}
7277
Nick Piggine692ab52007-07-26 13:40:43 +02007278static void
Alexey Dobriyane0361852007-08-09 11:16:46 +02007279set_table_entry(struct ctl_table *entry,
Nick Piggine692ab52007-07-26 13:40:43 +02007280 const char *procname, void *data, int maxlen,
7281 mode_t mode, proc_handler *proc_handler)
7282{
Nick Piggine692ab52007-07-26 13:40:43 +02007283 entry->procname = procname;
7284 entry->data = data;
7285 entry->maxlen = maxlen;
7286 entry->mode = mode;
7287 entry->proc_handler = proc_handler;
7288}
7289
7290static struct ctl_table *
7291sd_alloc_ctl_domain_table(struct sched_domain *sd)
7292{
Ingo Molnara5d8c342008-10-09 11:35:51 +02007293 struct ctl_table *table = sd_alloc_ctl_entry(13);
Nick Piggine692ab52007-07-26 13:40:43 +02007294
Milton Millerad1cdc12007-10-15 17:00:19 +02007295 if (table == NULL)
7296 return NULL;
7297
Alexey Dobriyane0361852007-08-09 11:16:46 +02007298 set_table_entry(&table[0], "min_interval", &sd->min_interval,
Nick Piggine692ab52007-07-26 13:40:43 +02007299 sizeof(long), 0644, proc_doulongvec_minmax);
Alexey Dobriyane0361852007-08-09 11:16:46 +02007300 set_table_entry(&table[1], "max_interval", &sd->max_interval,
Nick Piggine692ab52007-07-26 13:40:43 +02007301 sizeof(long), 0644, proc_doulongvec_minmax);
Alexey Dobriyane0361852007-08-09 11:16:46 +02007302 set_table_entry(&table[2], "busy_idx", &sd->busy_idx,
Nick Piggine692ab52007-07-26 13:40:43 +02007303 sizeof(int), 0644, proc_dointvec_minmax);
Alexey Dobriyane0361852007-08-09 11:16:46 +02007304 set_table_entry(&table[3], "idle_idx", &sd->idle_idx,
Nick Piggine692ab52007-07-26 13:40:43 +02007305 sizeof(int), 0644, proc_dointvec_minmax);
Alexey Dobriyane0361852007-08-09 11:16:46 +02007306 set_table_entry(&table[4], "newidle_idx", &sd->newidle_idx,
Nick Piggine692ab52007-07-26 13:40:43 +02007307 sizeof(int), 0644, proc_dointvec_minmax);
Alexey Dobriyane0361852007-08-09 11:16:46 +02007308 set_table_entry(&table[5], "wake_idx", &sd->wake_idx,
Nick Piggine692ab52007-07-26 13:40:43 +02007309 sizeof(int), 0644, proc_dointvec_minmax);
Alexey Dobriyane0361852007-08-09 11:16:46 +02007310 set_table_entry(&table[6], "forkexec_idx", &sd->forkexec_idx,
Nick Piggine692ab52007-07-26 13:40:43 +02007311 sizeof(int), 0644, proc_dointvec_minmax);
Alexey Dobriyane0361852007-08-09 11:16:46 +02007312 set_table_entry(&table[7], "busy_factor", &sd->busy_factor,
Nick Piggine692ab52007-07-26 13:40:43 +02007313 sizeof(int), 0644, proc_dointvec_minmax);
Alexey Dobriyane0361852007-08-09 11:16:46 +02007314 set_table_entry(&table[8], "imbalance_pct", &sd->imbalance_pct,
Nick Piggine692ab52007-07-26 13:40:43 +02007315 sizeof(int), 0644, proc_dointvec_minmax);
Zou Nan haiace8b3d2007-10-15 17:00:14 +02007316 set_table_entry(&table[9], "cache_nice_tries",
Nick Piggine692ab52007-07-26 13:40:43 +02007317 &sd->cache_nice_tries,
7318 sizeof(int), 0644, proc_dointvec_minmax);
Zou Nan haiace8b3d2007-10-15 17:00:14 +02007319 set_table_entry(&table[10], "flags", &sd->flags,
Nick Piggine692ab52007-07-26 13:40:43 +02007320 sizeof(int), 0644, proc_dointvec_minmax);
Ingo Molnara5d8c342008-10-09 11:35:51 +02007321 set_table_entry(&table[11], "name", sd->name,
7322 CORENAME_MAX_SIZE, 0444, proc_dostring);
7323 /* &table[12] is terminator */
Nick Piggine692ab52007-07-26 13:40:43 +02007324
7325 return table;
7326}
7327
Ingo Molnar9a4e7152007-11-28 15:52:56 +01007328static ctl_table *sd_alloc_ctl_cpu_table(int cpu)
Nick Piggine692ab52007-07-26 13:40:43 +02007329{
7330 struct ctl_table *entry, *table;
7331 struct sched_domain *sd;
7332 int domain_num = 0, i;
7333 char buf[32];
7334
7335 for_each_domain(cpu, sd)
7336 domain_num++;
7337 entry = table = sd_alloc_ctl_entry(domain_num + 1);
Milton Millerad1cdc12007-10-15 17:00:19 +02007338 if (table == NULL)
7339 return NULL;
Nick Piggine692ab52007-07-26 13:40:43 +02007340
7341 i = 0;
7342 for_each_domain(cpu, sd) {
7343 snprintf(buf, 32, "domain%d", i);
Nick Piggine692ab52007-07-26 13:40:43 +02007344 entry->procname = kstrdup(buf, GFP_KERNEL);
Eric W. Biedermanc57baf12007-08-23 15:18:02 +02007345 entry->mode = 0555;
Nick Piggine692ab52007-07-26 13:40:43 +02007346 entry->child = sd_alloc_ctl_domain_table(sd);
7347 entry++;
7348 i++;
7349 }
7350 return table;
7351}
7352
7353static struct ctl_table_header *sd_sysctl_header;
Milton Miller6382bc92007-10-15 17:00:19 +02007354static void register_sched_domain_sysctl(void)
Nick Piggine692ab52007-07-26 13:40:43 +02007355{
7356 int i, cpu_num = num_online_cpus();
7357 struct ctl_table *entry = sd_alloc_ctl_entry(cpu_num + 1);
7358 char buf[32];
7359
Milton Miller73785472007-10-24 18:23:48 +02007360 WARN_ON(sd_ctl_dir[0].child);
7361 sd_ctl_dir[0].child = entry;
7362
Milton Millerad1cdc12007-10-15 17:00:19 +02007363 if (entry == NULL)
7364 return;
7365
Milton Miller97b6ea72007-10-15 17:00:19 +02007366 for_each_online_cpu(i) {
Nick Piggine692ab52007-07-26 13:40:43 +02007367 snprintf(buf, 32, "cpu%d", i);
Nick Piggine692ab52007-07-26 13:40:43 +02007368 entry->procname = kstrdup(buf, GFP_KERNEL);
Eric W. Biedermanc57baf12007-08-23 15:18:02 +02007369 entry->mode = 0555;
Nick Piggine692ab52007-07-26 13:40:43 +02007370 entry->child = sd_alloc_ctl_cpu_table(i);
Milton Miller97b6ea72007-10-15 17:00:19 +02007371 entry++;
Nick Piggine692ab52007-07-26 13:40:43 +02007372 }
Milton Miller73785472007-10-24 18:23:48 +02007373
7374 WARN_ON(sd_sysctl_header);
Nick Piggine692ab52007-07-26 13:40:43 +02007375 sd_sysctl_header = register_sysctl_table(sd_ctl_root);
7376}
Milton Miller6382bc92007-10-15 17:00:19 +02007377
Milton Miller73785472007-10-24 18:23:48 +02007378/* may be called multiple times per register */
Milton Miller6382bc92007-10-15 17:00:19 +02007379static void unregister_sched_domain_sysctl(void)
7380{
Milton Miller73785472007-10-24 18:23:48 +02007381 if (sd_sysctl_header)
7382 unregister_sysctl_table(sd_sysctl_header);
Milton Miller6382bc92007-10-15 17:00:19 +02007383 sd_sysctl_header = NULL;
Milton Miller73785472007-10-24 18:23:48 +02007384 if (sd_ctl_dir[0].child)
7385 sd_free_ctl_entry(&sd_ctl_dir[0].child);
Milton Miller6382bc92007-10-15 17:00:19 +02007386}
Nick Piggine692ab52007-07-26 13:40:43 +02007387#else
Milton Miller6382bc92007-10-15 17:00:19 +02007388static void register_sched_domain_sysctl(void)
7389{
7390}
7391static void unregister_sched_domain_sysctl(void)
Nick Piggine692ab52007-07-26 13:40:43 +02007392{
7393}
7394#endif
7395
Gregory Haskins1f11eb62008-06-04 15:04:05 -04007396static void set_rq_online(struct rq *rq)
7397{
7398 if (!rq->online) {
7399 const struct sched_class *class;
7400
Rusty Russellc6c49272008-11-25 02:35:05 +10307401 cpumask_set_cpu(rq->cpu, rq->rd->online);
Gregory Haskins1f11eb62008-06-04 15:04:05 -04007402 rq->online = 1;
7403
7404 for_each_class(class) {
7405 if (class->rq_online)
7406 class->rq_online(rq);
7407 }
7408 }
7409}
7410
7411static void set_rq_offline(struct rq *rq)
7412{
7413 if (rq->online) {
7414 const struct sched_class *class;
7415
7416 for_each_class(class) {
7417 if (class->rq_offline)
7418 class->rq_offline(rq);
7419 }
7420
Rusty Russellc6c49272008-11-25 02:35:05 +10307421 cpumask_clear_cpu(rq->cpu, rq->rd->online);
Gregory Haskins1f11eb62008-06-04 15:04:05 -04007422 rq->online = 0;
7423 }
7424}
7425
Linus Torvalds1da177e2005-04-16 15:20:36 -07007426/*
7427 * migration_call - callback that gets triggered when a CPU is added.
7428 * Here we can start up the necessary migration thread for the new CPU.
7429 */
Ingo Molnar48f24c42006-07-03 00:25:40 -07007430static int __cpuinit
7431migration_call(struct notifier_block *nfb, unsigned long action, void *hcpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007432{
Linus Torvalds1da177e2005-04-16 15:20:36 -07007433 struct task_struct *p;
Ingo Molnar48f24c42006-07-03 00:25:40 -07007434 int cpu = (long)hcpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007435 unsigned long flags;
Ingo Molnar70b97a72006-07-03 00:25:42 -07007436 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007437
7438 switch (action) {
Gautham R Shenoy5be93612007-05-09 02:34:04 -07007439
Linus Torvalds1da177e2005-04-16 15:20:36 -07007440 case CPU_UP_PREPARE:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07007441 case CPU_UP_PREPARE_FROZEN:
Ingo Molnardd41f592007-07-09 18:51:59 +02007442 p = kthread_create(migration_thread, hcpu, "migration/%d", cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007443 if (IS_ERR(p))
7444 return NOTIFY_BAD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007445 kthread_bind(p, cpu);
7446 /* Must be high prio: stop_machine expects to yield to it. */
7447 rq = task_rq_lock(p, &flags);
Ingo Molnardd41f592007-07-09 18:51:59 +02007448 __setscheduler(rq, p, SCHED_FIFO, MAX_RT_PRIO-1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007449 task_rq_unlock(rq, &flags);
7450 cpu_rq(cpu)->migration_thread = p;
7451 break;
Ingo Molnar48f24c42006-07-03 00:25:40 -07007452
Linus Torvalds1da177e2005-04-16 15:20:36 -07007453 case CPU_ONLINE:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07007454 case CPU_ONLINE_FROZEN:
Robert P. J. Day3a4fa0a2007-10-19 23:10:43 +02007455 /* Strictly unnecessary, as first user will wake it. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007456 wake_up_process(cpu_rq(cpu)->migration_thread);
Gregory Haskins1f94ef52008-03-10 16:52:41 -04007457
7458 /* Update our root-domain */
7459 rq = cpu_rq(cpu);
7460 spin_lock_irqsave(&rq->lock, flags);
Thomas Gleixnerdce48a82009-04-11 10:43:41 +02007461 rq->calc_load_update = calc_load_update;
7462 rq->calc_load_active = 0;
Gregory Haskins1f94ef52008-03-10 16:52:41 -04007463 if (rq->rd) {
Rusty Russellc6c49272008-11-25 02:35:05 +10307464 BUG_ON(!cpumask_test_cpu(cpu, rq->rd->span));
Gregory Haskins1f11eb62008-06-04 15:04:05 -04007465
7466 set_rq_online(rq);
Gregory Haskins1f94ef52008-03-10 16:52:41 -04007467 }
7468 spin_unlock_irqrestore(&rq->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007469 break;
Ingo Molnar48f24c42006-07-03 00:25:40 -07007470
Linus Torvalds1da177e2005-04-16 15:20:36 -07007471#ifdef CONFIG_HOTPLUG_CPU
7472 case CPU_UP_CANCELED:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07007473 case CPU_UP_CANCELED_FROZEN:
Heiko Carstensfc75cdf2006-06-25 05:49:10 -07007474 if (!cpu_rq(cpu)->migration_thread)
7475 break;
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01007476 /* Unbind it from offline cpu so it can run. Fall thru. */
Heiko Carstensa4c4af72005-11-07 00:58:38 -08007477 kthread_bind(cpu_rq(cpu)->migration_thread,
Rusty Russell1e5ce4f2008-11-25 02:35:03 +10307478 cpumask_any(cpu_online_mask));
Linus Torvalds1da177e2005-04-16 15:20:36 -07007479 kthread_stop(cpu_rq(cpu)->migration_thread);
7480 cpu_rq(cpu)->migration_thread = NULL;
7481 break;
Ingo Molnar48f24c42006-07-03 00:25:40 -07007482
Linus Torvalds1da177e2005-04-16 15:20:36 -07007483 case CPU_DEAD:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07007484 case CPU_DEAD_FROZEN:
Cliff Wickman470fd642007-10-18 23:40:46 -07007485 cpuset_lock(); /* around calls to cpuset_cpus_allowed_lock() */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007486 migrate_live_tasks(cpu);
7487 rq = cpu_rq(cpu);
7488 kthread_stop(rq->migration_thread);
7489 rq->migration_thread = NULL;
7490 /* Idle task back to normal (off runqueue, low prio) */
Oleg Nesterovd2da2722007-10-16 23:30:56 -07007491 spin_lock_irq(&rq->lock);
Ingo Molnara8e504d2007-08-09 11:16:47 +02007492 update_rq_clock(rq);
Ingo Molnar2e1cb742007-08-09 11:16:49 +02007493 deactivate_task(rq, rq->idle, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007494 rq->idle->static_prio = MAX_PRIO;
Ingo Molnardd41f592007-07-09 18:51:59 +02007495 __setscheduler(rq, rq->idle, SCHED_NORMAL, 0);
7496 rq->idle->sched_class = &idle_sched_class;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007497 migrate_dead_tasks(cpu);
Oleg Nesterovd2da2722007-10-16 23:30:56 -07007498 spin_unlock_irq(&rq->lock);
Cliff Wickman470fd642007-10-18 23:40:46 -07007499 cpuset_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07007500 migrate_nr_uninterruptible(rq);
7501 BUG_ON(rq->nr_running != 0);
Thomas Gleixnerdce48a82009-04-11 10:43:41 +02007502 calc_global_load_remove(rq);
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01007503 /*
7504 * No need to migrate the tasks: it was best-effort if
7505 * they didn't take sched_hotcpu_mutex. Just wake up
7506 * the requestors.
7507 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007508 spin_lock_irq(&rq->lock);
7509 while (!list_empty(&rq->migration_queue)) {
Ingo Molnar70b97a72006-07-03 00:25:42 -07007510 struct migration_req *req;
7511
Linus Torvalds1da177e2005-04-16 15:20:36 -07007512 req = list_entry(rq->migration_queue.next,
Ingo Molnar70b97a72006-07-03 00:25:42 -07007513 struct migration_req, list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007514 list_del_init(&req->list);
Brian King9a2bd242008-12-09 08:47:00 -06007515 spin_unlock_irq(&rq->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007516 complete(&req->done);
Brian King9a2bd242008-12-09 08:47:00 -06007517 spin_lock_irq(&rq->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007518 }
7519 spin_unlock_irq(&rq->lock);
7520 break;
Gregory Haskins57d885f2008-01-25 21:08:18 +01007521
Gregory Haskins08f503b2008-03-10 17:59:11 -04007522 case CPU_DYING:
7523 case CPU_DYING_FROZEN:
Gregory Haskins57d885f2008-01-25 21:08:18 +01007524 /* Update our root-domain */
7525 rq = cpu_rq(cpu);
7526 spin_lock_irqsave(&rq->lock, flags);
7527 if (rq->rd) {
Rusty Russellc6c49272008-11-25 02:35:05 +10307528 BUG_ON(!cpumask_test_cpu(cpu, rq->rd->span));
Gregory Haskins1f11eb62008-06-04 15:04:05 -04007529 set_rq_offline(rq);
Gregory Haskins57d885f2008-01-25 21:08:18 +01007530 }
7531 spin_unlock_irqrestore(&rq->lock, flags);
7532 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007533#endif
7534 }
7535 return NOTIFY_OK;
7536}
7537
7538/* Register at highest priority so that task migration (migrate_all_tasks)
7539 * happens before everything else.
7540 */
Chandra Seetharaman26c21432006-06-27 02:54:10 -07007541static struct notifier_block __cpuinitdata migration_notifier = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007542 .notifier_call = migration_call,
7543 .priority = 10
7544};
7545
Eduard - Gabriel Munteanu7babe8d2008-07-25 19:45:11 -07007546static int __init migration_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007547{
7548 void *cpu = (void *)(long)smp_processor_id();
Akinobu Mita07dccf32006-09-29 02:00:22 -07007549 int err;
Ingo Molnar48f24c42006-07-03 00:25:40 -07007550
7551 /* Start one for the boot CPU: */
Akinobu Mita07dccf32006-09-29 02:00:22 -07007552 err = migration_call(&migration_notifier, CPU_UP_PREPARE, cpu);
7553 BUG_ON(err == NOTIFY_BAD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007554 migration_call(&migration_notifier, CPU_ONLINE, cpu);
7555 register_cpu_notifier(&migration_notifier);
Eduard - Gabriel Munteanu7babe8d2008-07-25 19:45:11 -07007556
7557 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007558}
Eduard - Gabriel Munteanu7babe8d2008-07-25 19:45:11 -07007559early_initcall(migration_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007560#endif
7561
7562#ifdef CONFIG_SMP
Christoph Lameter476f3532007-05-06 14:48:58 -07007563
Ingo Molnar3e9830d2007-10-15 17:00:13 +02007564#ifdef CONFIG_SCHED_DEBUG
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02007565
Mike Travis7c16ec52008-04-04 18:11:11 -07007566static int sched_domain_debug_one(struct sched_domain *sd, int cpu, int level,
Rusty Russell96f874e2008-11-25 02:35:14 +10307567 struct cpumask *groupmask)
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02007568{
7569 struct sched_group *group = sd->groups;
Mike Travis434d53b2008-04-04 18:11:04 -07007570 char str[256];
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02007571
Rusty Russell968ea6d2008-12-13 21:55:51 +10307572 cpulist_scnprintf(str, sizeof(str), sched_domain_span(sd));
Rusty Russell96f874e2008-11-25 02:35:14 +10307573 cpumask_clear(groupmask);
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02007574
7575 printk(KERN_DEBUG "%*s domain %d: ", level, "", level);
7576
7577 if (!(sd->flags & SD_LOAD_BALANCE)) {
7578 printk("does not load-balance\n");
7579 if (sd->parent)
7580 printk(KERN_ERR "ERROR: !SD_LOAD_BALANCE domain"
7581 " has parent");
7582 return -1;
7583 }
7584
Li Zefaneefd7962008-11-04 16:15:37 +08007585 printk(KERN_CONT "span %s level %s\n", str, sd->name);
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02007586
Rusty Russell758b2cd2008-11-25 02:35:04 +10307587 if (!cpumask_test_cpu(cpu, sched_domain_span(sd))) {
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02007588 printk(KERN_ERR "ERROR: domain->span does not contain "
7589 "CPU%d\n", cpu);
7590 }
Rusty Russell758b2cd2008-11-25 02:35:04 +10307591 if (!cpumask_test_cpu(cpu, sched_group_cpus(group))) {
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02007592 printk(KERN_ERR "ERROR: domain->groups does not contain"
7593 " CPU%d\n", cpu);
7594 }
7595
7596 printk(KERN_DEBUG "%*s groups:", level + 1, "");
7597 do {
7598 if (!group) {
7599 printk("\n");
7600 printk(KERN_ERR "ERROR: group is NULL\n");
7601 break;
7602 }
7603
7604 if (!group->__cpu_power) {
7605 printk(KERN_CONT "\n");
7606 printk(KERN_ERR "ERROR: domain->cpu_power not "
7607 "set\n");
7608 break;
7609 }
7610
Rusty Russell758b2cd2008-11-25 02:35:04 +10307611 if (!cpumask_weight(sched_group_cpus(group))) {
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02007612 printk(KERN_CONT "\n");
7613 printk(KERN_ERR "ERROR: empty group\n");
7614 break;
7615 }
7616
Rusty Russell758b2cd2008-11-25 02:35:04 +10307617 if (cpumask_intersects(groupmask, sched_group_cpus(group))) {
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02007618 printk(KERN_CONT "\n");
7619 printk(KERN_ERR "ERROR: repeated CPUs\n");
7620 break;
7621 }
7622
Rusty Russell758b2cd2008-11-25 02:35:04 +10307623 cpumask_or(groupmask, groupmask, sched_group_cpus(group));
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02007624
Rusty Russell968ea6d2008-12-13 21:55:51 +10307625 cpulist_scnprintf(str, sizeof(str), sched_group_cpus(group));
Gautham R Shenoy381512c2009-04-14 09:09:36 +05307626
7627 printk(KERN_CONT " %s", str);
7628 if (group->__cpu_power != SCHED_LOAD_SCALE) {
7629 printk(KERN_CONT " (__cpu_power = %d)",
7630 group->__cpu_power);
7631 }
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02007632
7633 group = group->next;
7634 } while (group != sd->groups);
7635 printk(KERN_CONT "\n");
7636
Rusty Russell758b2cd2008-11-25 02:35:04 +10307637 if (!cpumask_equal(sched_domain_span(sd), groupmask))
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02007638 printk(KERN_ERR "ERROR: groups don't span domain->span\n");
7639
Rusty Russell758b2cd2008-11-25 02:35:04 +10307640 if (sd->parent &&
7641 !cpumask_subset(groupmask, sched_domain_span(sd->parent)))
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02007642 printk(KERN_ERR "ERROR: parent span is not a superset "
7643 "of domain->span\n");
7644 return 0;
7645}
7646
Linus Torvalds1da177e2005-04-16 15:20:36 -07007647static void sched_domain_debug(struct sched_domain *sd, int cpu)
7648{
Rusty Russelld5dd3db2008-11-25 02:35:12 +10307649 cpumask_var_t groupmask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007650 int level = 0;
7651
Nick Piggin41c7ce92005-06-25 14:57:24 -07007652 if (!sd) {
7653 printk(KERN_DEBUG "CPU%d attaching NULL sched-domain.\n", cpu);
7654 return;
7655 }
7656
Linus Torvalds1da177e2005-04-16 15:20:36 -07007657 printk(KERN_DEBUG "CPU%d attaching sched-domain:\n", cpu);
7658
Rusty Russelld5dd3db2008-11-25 02:35:12 +10307659 if (!alloc_cpumask_var(&groupmask, GFP_KERNEL)) {
Mike Travis7c16ec52008-04-04 18:11:11 -07007660 printk(KERN_DEBUG "Cannot load-balance (out of memory)\n");
7661 return;
7662 }
7663
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02007664 for (;;) {
Mike Travis7c16ec52008-04-04 18:11:11 -07007665 if (sched_domain_debug_one(sd, cpu, level, groupmask))
Linus Torvalds1da177e2005-04-16 15:20:36 -07007666 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007667 level++;
7668 sd = sd->parent;
Miguel Ojeda Sandonis33859f72006-12-10 02:20:38 -08007669 if (!sd)
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02007670 break;
7671 }
Rusty Russelld5dd3db2008-11-25 02:35:12 +10307672 free_cpumask_var(groupmask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007673}
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +02007674#else /* !CONFIG_SCHED_DEBUG */
Ingo Molnar48f24c42006-07-03 00:25:40 -07007675# define sched_domain_debug(sd, cpu) do { } while (0)
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +02007676#endif /* CONFIG_SCHED_DEBUG */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007677
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07007678static int sd_degenerate(struct sched_domain *sd)
Suresh Siddha245af2c2005-06-25 14:57:25 -07007679{
Rusty Russell758b2cd2008-11-25 02:35:04 +10307680 if (cpumask_weight(sched_domain_span(sd)) == 1)
Suresh Siddha245af2c2005-06-25 14:57:25 -07007681 return 1;
7682
7683 /* Following flags need at least 2 groups */
7684 if (sd->flags & (SD_LOAD_BALANCE |
7685 SD_BALANCE_NEWIDLE |
7686 SD_BALANCE_FORK |
Siddha, Suresh B89c47102006-10-03 01:14:09 -07007687 SD_BALANCE_EXEC |
7688 SD_SHARE_CPUPOWER |
7689 SD_SHARE_PKG_RESOURCES)) {
Suresh Siddha245af2c2005-06-25 14:57:25 -07007690 if (sd->groups != sd->groups->next)
7691 return 0;
7692 }
7693
7694 /* Following flags don't use groups */
7695 if (sd->flags & (SD_WAKE_IDLE |
7696 SD_WAKE_AFFINE |
7697 SD_WAKE_BALANCE))
7698 return 0;
7699
7700 return 1;
7701}
7702
Ingo Molnar48f24c42006-07-03 00:25:40 -07007703static int
7704sd_parent_degenerate(struct sched_domain *sd, struct sched_domain *parent)
Suresh Siddha245af2c2005-06-25 14:57:25 -07007705{
7706 unsigned long cflags = sd->flags, pflags = parent->flags;
7707
7708 if (sd_degenerate(parent))
7709 return 1;
7710
Rusty Russell758b2cd2008-11-25 02:35:04 +10307711 if (!cpumask_equal(sched_domain_span(sd), sched_domain_span(parent)))
Suresh Siddha245af2c2005-06-25 14:57:25 -07007712 return 0;
7713
7714 /* Does parent contain flags not in child? */
7715 /* WAKE_BALANCE is a subset of WAKE_AFFINE */
7716 if (cflags & SD_WAKE_AFFINE)
7717 pflags &= ~SD_WAKE_BALANCE;
7718 /* Flags needing groups don't count if only 1 group in parent */
7719 if (parent->groups == parent->groups->next) {
7720 pflags &= ~(SD_LOAD_BALANCE |
7721 SD_BALANCE_NEWIDLE |
7722 SD_BALANCE_FORK |
Siddha, Suresh B89c47102006-10-03 01:14:09 -07007723 SD_BALANCE_EXEC |
7724 SD_SHARE_CPUPOWER |
7725 SD_SHARE_PKG_RESOURCES);
Ken Chen54364992008-12-07 18:47:37 -08007726 if (nr_node_ids == 1)
7727 pflags &= ~SD_SERIALIZE;
Suresh Siddha245af2c2005-06-25 14:57:25 -07007728 }
7729 if (~cflags & pflags)
7730 return 0;
7731
7732 return 1;
7733}
7734
Rusty Russellc6c49272008-11-25 02:35:05 +10307735static void free_rootdomain(struct root_domain *rd)
7736{
Rusty Russell68e74562008-11-25 02:35:13 +10307737 cpupri_cleanup(&rd->cpupri);
7738
Rusty Russellc6c49272008-11-25 02:35:05 +10307739 free_cpumask_var(rd->rto_mask);
7740 free_cpumask_var(rd->online);
7741 free_cpumask_var(rd->span);
7742 kfree(rd);
7743}
7744
Gregory Haskins57d885f2008-01-25 21:08:18 +01007745static void rq_attach_root(struct rq *rq, struct root_domain *rd)
7746{
Ingo Molnara0490fa2009-02-12 11:35:40 +01007747 struct root_domain *old_rd = NULL;
Gregory Haskins57d885f2008-01-25 21:08:18 +01007748 unsigned long flags;
Gregory Haskins57d885f2008-01-25 21:08:18 +01007749
7750 spin_lock_irqsave(&rq->lock, flags);
7751
7752 if (rq->rd) {
Ingo Molnara0490fa2009-02-12 11:35:40 +01007753 old_rd = rq->rd;
Gregory Haskins57d885f2008-01-25 21:08:18 +01007754
Rusty Russellc6c49272008-11-25 02:35:05 +10307755 if (cpumask_test_cpu(rq->cpu, old_rd->online))
Gregory Haskins1f11eb62008-06-04 15:04:05 -04007756 set_rq_offline(rq);
Gregory Haskins57d885f2008-01-25 21:08:18 +01007757
Rusty Russellc6c49272008-11-25 02:35:05 +10307758 cpumask_clear_cpu(rq->cpu, old_rd->span);
Gregory Haskinsdc938522008-01-25 21:08:26 +01007759
Ingo Molnara0490fa2009-02-12 11:35:40 +01007760 /*
7761 * If we dont want to free the old_rt yet then
7762 * set old_rd to NULL to skip the freeing later
7763 * in this function:
7764 */
7765 if (!atomic_dec_and_test(&old_rd->refcount))
7766 old_rd = NULL;
Gregory Haskins57d885f2008-01-25 21:08:18 +01007767 }
7768
7769 atomic_inc(&rd->refcount);
7770 rq->rd = rd;
7771
Rusty Russellc6c49272008-11-25 02:35:05 +10307772 cpumask_set_cpu(rq->cpu, rd->span);
7773 if (cpumask_test_cpu(rq->cpu, cpu_online_mask))
Gregory Haskins1f11eb62008-06-04 15:04:05 -04007774 set_rq_online(rq);
Gregory Haskins57d885f2008-01-25 21:08:18 +01007775
7776 spin_unlock_irqrestore(&rq->lock, flags);
Ingo Molnara0490fa2009-02-12 11:35:40 +01007777
7778 if (old_rd)
7779 free_rootdomain(old_rd);
Gregory Haskins57d885f2008-01-25 21:08:18 +01007780}
7781
Li Zefandb2f59c2009-01-06 17:40:36 +08007782static int __init_refok init_rootdomain(struct root_domain *rd, bool bootmem)
Gregory Haskins57d885f2008-01-25 21:08:18 +01007783{
Pekka Enberg36b7b6d2009-06-10 23:42:36 +03007784 gfp_t gfp = GFP_KERNEL;
7785
Gregory Haskins57d885f2008-01-25 21:08:18 +01007786 memset(rd, 0, sizeof(*rd));
7787
Pekka Enberg36b7b6d2009-06-10 23:42:36 +03007788 if (bootmem)
7789 gfp = GFP_NOWAIT;
Gregory Haskins6e0534f2008-05-12 21:21:01 +02007790
Pekka Enberg36b7b6d2009-06-10 23:42:36 +03007791 if (!alloc_cpumask_var(&rd->span, gfp))
Li Zefan0c910d22009-01-06 17:39:06 +08007792 goto out;
Pekka Enberg36b7b6d2009-06-10 23:42:36 +03007793 if (!alloc_cpumask_var(&rd->online, gfp))
Rusty Russellc6c49272008-11-25 02:35:05 +10307794 goto free_span;
Pekka Enberg36b7b6d2009-06-10 23:42:36 +03007795 if (!alloc_cpumask_var(&rd->rto_mask, gfp))
Rusty Russellc6c49272008-11-25 02:35:05 +10307796 goto free_online;
Gregory Haskins6e0534f2008-05-12 21:21:01 +02007797
Rusty Russell68e74562008-11-25 02:35:13 +10307798 if (cpupri_init(&rd->cpupri, false) != 0)
7799 goto free_rto_mask;
Rusty Russellc6c49272008-11-25 02:35:05 +10307800 return 0;
7801
Rusty Russell68e74562008-11-25 02:35:13 +10307802free_rto_mask:
7803 free_cpumask_var(rd->rto_mask);
Rusty Russellc6c49272008-11-25 02:35:05 +10307804free_online:
7805 free_cpumask_var(rd->online);
7806free_span:
7807 free_cpumask_var(rd->span);
Li Zefan0c910d22009-01-06 17:39:06 +08007808out:
Rusty Russellc6c49272008-11-25 02:35:05 +10307809 return -ENOMEM;
Gregory Haskins57d885f2008-01-25 21:08:18 +01007810}
7811
7812static void init_defrootdomain(void)
7813{
Rusty Russellc6c49272008-11-25 02:35:05 +10307814 init_rootdomain(&def_root_domain, true);
7815
Gregory Haskins57d885f2008-01-25 21:08:18 +01007816 atomic_set(&def_root_domain.refcount, 1);
7817}
7818
Gregory Haskinsdc938522008-01-25 21:08:26 +01007819static struct root_domain *alloc_rootdomain(void)
Gregory Haskins57d885f2008-01-25 21:08:18 +01007820{
7821 struct root_domain *rd;
7822
7823 rd = kmalloc(sizeof(*rd), GFP_KERNEL);
7824 if (!rd)
7825 return NULL;
7826
Rusty Russellc6c49272008-11-25 02:35:05 +10307827 if (init_rootdomain(rd, false) != 0) {
7828 kfree(rd);
7829 return NULL;
7830 }
Gregory Haskins57d885f2008-01-25 21:08:18 +01007831
7832 return rd;
7833}
7834
Linus Torvalds1da177e2005-04-16 15:20:36 -07007835/*
Ingo Molnar0eab9142008-01-25 21:08:19 +01007836 * Attach the domain 'sd' to 'cpu' as its base domain. Callers must
Linus Torvalds1da177e2005-04-16 15:20:36 -07007837 * hold the hotplug lock.
7838 */
Ingo Molnar0eab9142008-01-25 21:08:19 +01007839static void
7840cpu_attach_domain(struct sched_domain *sd, struct root_domain *rd, int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007841{
Ingo Molnar70b97a72006-07-03 00:25:42 -07007842 struct rq *rq = cpu_rq(cpu);
Suresh Siddha245af2c2005-06-25 14:57:25 -07007843 struct sched_domain *tmp;
7844
7845 /* Remove the sched domains which do not contribute to scheduling. */
Li Zefanf29c9b12008-11-06 09:45:16 +08007846 for (tmp = sd; tmp; ) {
Suresh Siddha245af2c2005-06-25 14:57:25 -07007847 struct sched_domain *parent = tmp->parent;
7848 if (!parent)
7849 break;
Li Zefanf29c9b12008-11-06 09:45:16 +08007850
Siddha, Suresh B1a848872006-10-03 01:14:08 -07007851 if (sd_parent_degenerate(tmp, parent)) {
Suresh Siddha245af2c2005-06-25 14:57:25 -07007852 tmp->parent = parent->parent;
Siddha, Suresh B1a848872006-10-03 01:14:08 -07007853 if (parent->parent)
7854 parent->parent->child = tmp;
Li Zefanf29c9b12008-11-06 09:45:16 +08007855 } else
7856 tmp = tmp->parent;
Suresh Siddha245af2c2005-06-25 14:57:25 -07007857 }
7858
Siddha, Suresh B1a848872006-10-03 01:14:08 -07007859 if (sd && sd_degenerate(sd)) {
Suresh Siddha245af2c2005-06-25 14:57:25 -07007860 sd = sd->parent;
Siddha, Suresh B1a848872006-10-03 01:14:08 -07007861 if (sd)
7862 sd->child = NULL;
7863 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007864
7865 sched_domain_debug(sd, cpu);
7866
Gregory Haskins57d885f2008-01-25 21:08:18 +01007867 rq_attach_root(rq, rd);
Nick Piggin674311d2005-06-25 14:57:27 -07007868 rcu_assign_pointer(rq->sd, sd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007869}
7870
7871/* cpus with isolated domains */
Rusty Russelldcc30a32008-11-25 02:35:12 +10307872static cpumask_var_t cpu_isolated_map;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007873
7874/* Setup the mask of cpus configured for isolated domains */
7875static int __init isolated_cpu_setup(char *str)
7876{
Rusty Russell968ea6d2008-12-13 21:55:51 +10307877 cpulist_parse(str, cpu_isolated_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007878 return 1;
7879}
7880
Ingo Molnar8927f492007-10-15 17:00:13 +02007881__setup("isolcpus=", isolated_cpu_setup);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007882
7883/*
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08007884 * init_sched_build_groups takes the cpumask we wish to span, and a pointer
7885 * to a function which identifies what group(along with sched group) a CPU
Rusty Russell96f874e2008-11-25 02:35:14 +10307886 * belongs to. The return value of group_fn must be a >= 0 and < nr_cpu_ids
7887 * (due to the fact that we keep track of groups covered with a struct cpumask).
Linus Torvalds1da177e2005-04-16 15:20:36 -07007888 *
7889 * init_sched_build_groups will build a circular linked list of the groups
7890 * covered by the given span, and will set each group's ->cpumask correctly,
7891 * and ->cpu_power to 0.
7892 */
Siddha, Suresh Ba6160582006-10-03 01:14:06 -07007893static void
Rusty Russell96f874e2008-11-25 02:35:14 +10307894init_sched_build_groups(const struct cpumask *span,
7895 const struct cpumask *cpu_map,
7896 int (*group_fn)(int cpu, const struct cpumask *cpu_map,
Mike Travis7c16ec52008-04-04 18:11:11 -07007897 struct sched_group **sg,
Rusty Russell96f874e2008-11-25 02:35:14 +10307898 struct cpumask *tmpmask),
7899 struct cpumask *covered, struct cpumask *tmpmask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007900{
7901 struct sched_group *first = NULL, *last = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007902 int i;
7903
Rusty Russell96f874e2008-11-25 02:35:14 +10307904 cpumask_clear(covered);
Mike Travis7c16ec52008-04-04 18:11:11 -07007905
Rusty Russellabcd0832008-11-25 02:35:02 +10307906 for_each_cpu(i, span) {
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08007907 struct sched_group *sg;
Mike Travis7c16ec52008-04-04 18:11:11 -07007908 int group = group_fn(i, cpu_map, &sg, tmpmask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007909 int j;
7910
Rusty Russell758b2cd2008-11-25 02:35:04 +10307911 if (cpumask_test_cpu(i, covered))
Linus Torvalds1da177e2005-04-16 15:20:36 -07007912 continue;
7913
Rusty Russell758b2cd2008-11-25 02:35:04 +10307914 cpumask_clear(sched_group_cpus(sg));
Eric Dumazet5517d862007-05-08 00:32:57 -07007915 sg->__cpu_power = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007916
Rusty Russellabcd0832008-11-25 02:35:02 +10307917 for_each_cpu(j, span) {
Mike Travis7c16ec52008-04-04 18:11:11 -07007918 if (group_fn(j, cpu_map, NULL, tmpmask) != group)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007919 continue;
7920
Rusty Russell96f874e2008-11-25 02:35:14 +10307921 cpumask_set_cpu(j, covered);
Rusty Russell758b2cd2008-11-25 02:35:04 +10307922 cpumask_set_cpu(j, sched_group_cpus(sg));
Linus Torvalds1da177e2005-04-16 15:20:36 -07007923 }
7924 if (!first)
7925 first = sg;
7926 if (last)
7927 last->next = sg;
7928 last = sg;
7929 }
7930 last->next = first;
7931}
7932
John Hawkes9c1cfda2005-09-06 15:18:14 -07007933#define SD_NODES_PER_DOMAIN 16
Linus Torvalds1da177e2005-04-16 15:20:36 -07007934
John Hawkes9c1cfda2005-09-06 15:18:14 -07007935#ifdef CONFIG_NUMA
akpm@osdl.org198e2f12006-01-12 01:05:30 -08007936
John Hawkes9c1cfda2005-09-06 15:18:14 -07007937/**
7938 * find_next_best_node - find the next node to include in a sched_domain
7939 * @node: node whose sched_domain we're building
7940 * @used_nodes: nodes already in the sched_domain
7941 *
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01007942 * Find the next node to include in a given scheduling domain. Simply
John Hawkes9c1cfda2005-09-06 15:18:14 -07007943 * finds the closest node not already in the @used_nodes map.
7944 *
7945 * Should use nodemask_t.
7946 */
Mike Travisc5f59f02008-04-04 18:11:10 -07007947static int find_next_best_node(int node, nodemask_t *used_nodes)
John Hawkes9c1cfda2005-09-06 15:18:14 -07007948{
7949 int i, n, val, min_val, best_node = 0;
7950
7951 min_val = INT_MAX;
7952
Mike Travis076ac2a2008-05-12 21:21:12 +02007953 for (i = 0; i < nr_node_ids; i++) {
John Hawkes9c1cfda2005-09-06 15:18:14 -07007954 /* Start at @node */
Mike Travis076ac2a2008-05-12 21:21:12 +02007955 n = (node + i) % nr_node_ids;
John Hawkes9c1cfda2005-09-06 15:18:14 -07007956
7957 if (!nr_cpus_node(n))
7958 continue;
7959
7960 /* Skip already used nodes */
Mike Travisc5f59f02008-04-04 18:11:10 -07007961 if (node_isset(n, *used_nodes))
John Hawkes9c1cfda2005-09-06 15:18:14 -07007962 continue;
7963
7964 /* Simple min distance search */
7965 val = node_distance(node, n);
7966
7967 if (val < min_val) {
7968 min_val = val;
7969 best_node = n;
7970 }
7971 }
7972
Mike Travisc5f59f02008-04-04 18:11:10 -07007973 node_set(best_node, *used_nodes);
John Hawkes9c1cfda2005-09-06 15:18:14 -07007974 return best_node;
7975}
7976
7977/**
7978 * sched_domain_node_span - get a cpumask for a node's sched_domain
7979 * @node: node whose cpumask we're constructing
Randy Dunlap73486722008-04-22 10:07:22 -07007980 * @span: resulting cpumask
John Hawkes9c1cfda2005-09-06 15:18:14 -07007981 *
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01007982 * Given a node, construct a good cpumask for its sched_domain to span. It
John Hawkes9c1cfda2005-09-06 15:18:14 -07007983 * should be one that prevents unnecessary balancing, but also spreads tasks
7984 * out optimally.
7985 */
Rusty Russell96f874e2008-11-25 02:35:14 +10307986static void sched_domain_node_span(int node, struct cpumask *span)
John Hawkes9c1cfda2005-09-06 15:18:14 -07007987{
Mike Travisc5f59f02008-04-04 18:11:10 -07007988 nodemask_t used_nodes;
Ingo Molnar48f24c42006-07-03 00:25:40 -07007989 int i;
John Hawkes9c1cfda2005-09-06 15:18:14 -07007990
Mike Travis6ca09df2008-12-31 18:08:45 -08007991 cpumask_clear(span);
Mike Travisc5f59f02008-04-04 18:11:10 -07007992 nodes_clear(used_nodes);
John Hawkes9c1cfda2005-09-06 15:18:14 -07007993
Mike Travis6ca09df2008-12-31 18:08:45 -08007994 cpumask_or(span, span, cpumask_of_node(node));
Mike Travisc5f59f02008-04-04 18:11:10 -07007995 node_set(node, used_nodes);
John Hawkes9c1cfda2005-09-06 15:18:14 -07007996
7997 for (i = 1; i < SD_NODES_PER_DOMAIN; i++) {
Mike Travisc5f59f02008-04-04 18:11:10 -07007998 int next_node = find_next_best_node(node, &used_nodes);
Ingo Molnar48f24c42006-07-03 00:25:40 -07007999
Mike Travis6ca09df2008-12-31 18:08:45 -08008000 cpumask_or(span, span, cpumask_of_node(next_node));
John Hawkes9c1cfda2005-09-06 15:18:14 -07008001 }
John Hawkes9c1cfda2005-09-06 15:18:14 -07008002}
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +02008003#endif /* CONFIG_NUMA */
John Hawkes9c1cfda2005-09-06 15:18:14 -07008004
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07008005int sched_smt_power_savings = 0, sched_mc_power_savings = 0;
Ingo Molnar48f24c42006-07-03 00:25:40 -07008006
John Hawkes9c1cfda2005-09-06 15:18:14 -07008007/*
Rusty Russell6c99e9a2008-11-25 02:35:04 +10308008 * The cpus mask in sched_group and sched_domain hangs off the end.
Ingo Molnar4200efd2009-05-19 09:22:19 +02008009 *
8010 * ( See the the comments in include/linux/sched.h:struct sched_group
8011 * and struct sched_domain. )
Rusty Russell6c99e9a2008-11-25 02:35:04 +10308012 */
8013struct static_sched_group {
8014 struct sched_group sg;
8015 DECLARE_BITMAP(cpus, CONFIG_NR_CPUS);
8016};
8017
8018struct static_sched_domain {
8019 struct sched_domain sd;
8020 DECLARE_BITMAP(span, CONFIG_NR_CPUS);
8021};
8022
8023/*
Ingo Molnar48f24c42006-07-03 00:25:40 -07008024 * SMT sched-domains:
John Hawkes9c1cfda2005-09-06 15:18:14 -07008025 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07008026#ifdef CONFIG_SCHED_SMT
Rusty Russell6c99e9a2008-11-25 02:35:04 +10308027static DEFINE_PER_CPU(struct static_sched_domain, cpu_domains);
8028static DEFINE_PER_CPU(struct static_sched_group, sched_group_cpus);
Ingo Molnar48f24c42006-07-03 00:25:40 -07008029
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01008030static int
Rusty Russell96f874e2008-11-25 02:35:14 +10308031cpu_to_cpu_group(int cpu, const struct cpumask *cpu_map,
8032 struct sched_group **sg, struct cpumask *unused)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008033{
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08008034 if (sg)
Rusty Russell6c99e9a2008-11-25 02:35:04 +10308035 *sg = &per_cpu(sched_group_cpus, cpu).sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008036 return cpu;
8037}
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +02008038#endif /* CONFIG_SCHED_SMT */
Linus Torvalds1da177e2005-04-16 15:20:36 -07008039
Ingo Molnar48f24c42006-07-03 00:25:40 -07008040/*
8041 * multi-core sched-domains:
8042 */
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08008043#ifdef CONFIG_SCHED_MC
Rusty Russell6c99e9a2008-11-25 02:35:04 +10308044static DEFINE_PER_CPU(struct static_sched_domain, core_domains);
8045static DEFINE_PER_CPU(struct static_sched_group, sched_group_core);
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +02008046#endif /* CONFIG_SCHED_MC */
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08008047
8048#if defined(CONFIG_SCHED_MC) && defined(CONFIG_SCHED_SMT)
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01008049static int
Rusty Russell96f874e2008-11-25 02:35:14 +10308050cpu_to_core_group(int cpu, const struct cpumask *cpu_map,
8051 struct sched_group **sg, struct cpumask *mask)
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08008052{
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08008053 int group;
Mike Travis7c16ec52008-04-04 18:11:11 -07008054
Rusty Russellc69fc562009-03-13 14:49:46 +10308055 cpumask_and(mask, topology_thread_cpumask(cpu), cpu_map);
Rusty Russell96f874e2008-11-25 02:35:14 +10308056 group = cpumask_first(mask);
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08008057 if (sg)
Rusty Russell6c99e9a2008-11-25 02:35:04 +10308058 *sg = &per_cpu(sched_group_core, group).sg;
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08008059 return group;
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08008060}
8061#elif defined(CONFIG_SCHED_MC)
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01008062static int
Rusty Russell96f874e2008-11-25 02:35:14 +10308063cpu_to_core_group(int cpu, const struct cpumask *cpu_map,
8064 struct sched_group **sg, struct cpumask *unused)
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08008065{
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08008066 if (sg)
Rusty Russell6c99e9a2008-11-25 02:35:04 +10308067 *sg = &per_cpu(sched_group_core, cpu).sg;
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08008068 return cpu;
8069}
8070#endif
8071
Rusty Russell6c99e9a2008-11-25 02:35:04 +10308072static DEFINE_PER_CPU(struct static_sched_domain, phys_domains);
8073static DEFINE_PER_CPU(struct static_sched_group, sched_group_phys);
Ingo Molnar48f24c42006-07-03 00:25:40 -07008074
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01008075static int
Rusty Russell96f874e2008-11-25 02:35:14 +10308076cpu_to_phys_group(int cpu, const struct cpumask *cpu_map,
8077 struct sched_group **sg, struct cpumask *mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008078{
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08008079 int group;
Ingo Molnar48f24c42006-07-03 00:25:40 -07008080#ifdef CONFIG_SCHED_MC
Mike Travis6ca09df2008-12-31 18:08:45 -08008081 cpumask_and(mask, cpu_coregroup_mask(cpu), cpu_map);
Rusty Russell96f874e2008-11-25 02:35:14 +10308082 group = cpumask_first(mask);
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08008083#elif defined(CONFIG_SCHED_SMT)
Rusty Russellc69fc562009-03-13 14:49:46 +10308084 cpumask_and(mask, topology_thread_cpumask(cpu), cpu_map);
Rusty Russell96f874e2008-11-25 02:35:14 +10308085 group = cpumask_first(mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008086#else
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08008087 group = cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008088#endif
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08008089 if (sg)
Rusty Russell6c99e9a2008-11-25 02:35:04 +10308090 *sg = &per_cpu(sched_group_phys, group).sg;
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08008091 return group;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008092}
8093
8094#ifdef CONFIG_NUMA
John Hawkes9c1cfda2005-09-06 15:18:14 -07008095/*
8096 * The init_sched_build_groups can't handle what we want to do with node
8097 * groups, so roll our own. Now each node has its own list of groups which
8098 * gets dynamically allocated.
8099 */
Rusty Russell62ea9ce2009-01-11 01:04:16 +01008100static DEFINE_PER_CPU(struct static_sched_domain, node_domains);
Mike Travis434d53b2008-04-04 18:11:04 -07008101static struct sched_group ***sched_group_nodes_bycpu;
John Hawkes9c1cfda2005-09-06 15:18:14 -07008102
Rusty Russell62ea9ce2009-01-11 01:04:16 +01008103static DEFINE_PER_CPU(struct static_sched_domain, allnodes_domains);
Rusty Russell6c99e9a2008-11-25 02:35:04 +10308104static DEFINE_PER_CPU(struct static_sched_group, sched_group_allnodes);
John Hawkes9c1cfda2005-09-06 15:18:14 -07008105
Rusty Russell96f874e2008-11-25 02:35:14 +10308106static int cpu_to_allnodes_group(int cpu, const struct cpumask *cpu_map,
8107 struct sched_group **sg,
8108 struct cpumask *nodemask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008109{
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08008110 int group;
8111
Mike Travis6ca09df2008-12-31 18:08:45 -08008112 cpumask_and(nodemask, cpumask_of_node(cpu_to_node(cpu)), cpu_map);
Rusty Russell96f874e2008-11-25 02:35:14 +10308113 group = cpumask_first(nodemask);
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08008114
8115 if (sg)
Rusty Russell6c99e9a2008-11-25 02:35:04 +10308116 *sg = &per_cpu(sched_group_allnodes, group).sg;
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08008117 return group;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008118}
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08008119
Siddha, Suresh B08069032006-03-27 01:15:23 -08008120static void init_numa_sched_groups_power(struct sched_group *group_head)
8121{
8122 struct sched_group *sg = group_head;
8123 int j;
8124
8125 if (!sg)
8126 return;
Andi Kleen3a5c3592007-10-15 17:00:14 +02008127 do {
Rusty Russell758b2cd2008-11-25 02:35:04 +10308128 for_each_cpu(j, sched_group_cpus(sg)) {
Andi Kleen3a5c3592007-10-15 17:00:14 +02008129 struct sched_domain *sd;
Siddha, Suresh B08069032006-03-27 01:15:23 -08008130
Rusty Russell6c99e9a2008-11-25 02:35:04 +10308131 sd = &per_cpu(phys_domains, j).sd;
Miao Xie13318a72009-04-15 09:59:10 +08008132 if (j != group_first_cpu(sd->groups)) {
Andi Kleen3a5c3592007-10-15 17:00:14 +02008133 /*
8134 * Only add "power" once for each
8135 * physical package.
8136 */
8137 continue;
8138 }
8139
8140 sg_inc_cpu_power(sg, sd->groups->__cpu_power);
Siddha, Suresh B08069032006-03-27 01:15:23 -08008141 }
Andi Kleen3a5c3592007-10-15 17:00:14 +02008142 sg = sg->next;
8143 } while (sg != group_head);
Siddha, Suresh B08069032006-03-27 01:15:23 -08008144}
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +02008145#endif /* CONFIG_NUMA */
Linus Torvalds1da177e2005-04-16 15:20:36 -07008146
Siddha, Suresh Ba6160582006-10-03 01:14:06 -07008147#ifdef CONFIG_NUMA
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07008148/* Free memory allocated for various sched_group structures */
Rusty Russell96f874e2008-11-25 02:35:14 +10308149static void free_sched_groups(const struct cpumask *cpu_map,
8150 struct cpumask *nodemask)
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07008151{
Siddha, Suresh Ba6160582006-10-03 01:14:06 -07008152 int cpu, i;
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07008153
Rusty Russellabcd0832008-11-25 02:35:02 +10308154 for_each_cpu(cpu, cpu_map) {
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07008155 struct sched_group **sched_group_nodes
8156 = sched_group_nodes_bycpu[cpu];
8157
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07008158 if (!sched_group_nodes)
8159 continue;
8160
Mike Travis076ac2a2008-05-12 21:21:12 +02008161 for (i = 0; i < nr_node_ids; i++) {
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07008162 struct sched_group *oldsg, *sg = sched_group_nodes[i];
8163
Mike Travis6ca09df2008-12-31 18:08:45 -08008164 cpumask_and(nodemask, cpumask_of_node(i), cpu_map);
Rusty Russell96f874e2008-11-25 02:35:14 +10308165 if (cpumask_empty(nodemask))
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07008166 continue;
8167
8168 if (sg == NULL)
8169 continue;
8170 sg = sg->next;
8171next_sg:
8172 oldsg = sg;
8173 sg = sg->next;
8174 kfree(oldsg);
8175 if (oldsg != sched_group_nodes[i])
8176 goto next_sg;
8177 }
8178 kfree(sched_group_nodes);
8179 sched_group_nodes_bycpu[cpu] = NULL;
8180 }
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07008181}
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +02008182#else /* !CONFIG_NUMA */
Rusty Russell96f874e2008-11-25 02:35:14 +10308183static void free_sched_groups(const struct cpumask *cpu_map,
8184 struct cpumask *nodemask)
Siddha, Suresh Ba6160582006-10-03 01:14:06 -07008185{
8186}
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +02008187#endif /* CONFIG_NUMA */
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07008188
Linus Torvalds1da177e2005-04-16 15:20:36 -07008189/*
Siddha, Suresh B89c47102006-10-03 01:14:09 -07008190 * Initialize sched groups cpu_power.
8191 *
8192 * cpu_power indicates the capacity of sched group, which is used while
8193 * distributing the load between different sched groups in a sched domain.
8194 * Typically cpu_power for all the groups in a sched domain will be same unless
8195 * there are asymmetries in the topology. If there are asymmetries, group
8196 * having more cpu_power will pickup more load compared to the group having
8197 * less cpu_power.
8198 *
8199 * cpu_power will be a multiple of SCHED_LOAD_SCALE. This multiple represents
8200 * the maximum number of tasks a group can handle in the presence of other idle
8201 * or lightly loaded groups in the same sched domain.
8202 */
8203static void init_sched_groups_power(int cpu, struct sched_domain *sd)
8204{
8205 struct sched_domain *child;
8206 struct sched_group *group;
8207
8208 WARN_ON(!sd || !sd->groups);
8209
Miao Xie13318a72009-04-15 09:59:10 +08008210 if (cpu != group_first_cpu(sd->groups))
Siddha, Suresh B89c47102006-10-03 01:14:09 -07008211 return;
8212
8213 child = sd->child;
8214
Eric Dumazet5517d862007-05-08 00:32:57 -07008215 sd->groups->__cpu_power = 0;
8216
Siddha, Suresh B89c47102006-10-03 01:14:09 -07008217 /*
8218 * For perf policy, if the groups in child domain share resources
8219 * (for example cores sharing some portions of the cache hierarchy
8220 * or SMT), then set this domain groups cpu_power such that each group
8221 * can handle only one task, when there are other idle groups in the
8222 * same sched domain.
8223 */
8224 if (!child || (!(sd->flags & SD_POWERSAVINGS_BALANCE) &&
8225 (child->flags &
8226 (SD_SHARE_CPUPOWER | SD_SHARE_PKG_RESOURCES)))) {
Eric Dumazet5517d862007-05-08 00:32:57 -07008227 sg_inc_cpu_power(sd->groups, SCHED_LOAD_SCALE);
Siddha, Suresh B89c47102006-10-03 01:14:09 -07008228 return;
8229 }
8230
Siddha, Suresh B89c47102006-10-03 01:14:09 -07008231 /*
8232 * add cpu_power of each child group to this groups cpu_power
8233 */
8234 group = child->groups;
8235 do {
Eric Dumazet5517d862007-05-08 00:32:57 -07008236 sg_inc_cpu_power(sd->groups, group->__cpu_power);
Siddha, Suresh B89c47102006-10-03 01:14:09 -07008237 group = group->next;
8238 } while (group != child->groups);
8239}
8240
8241/*
Mike Travis7c16ec52008-04-04 18:11:11 -07008242 * Initializers for schedule domains
8243 * Non-inlined to reduce accumulated stack pressure in build_sched_domains()
8244 */
8245
Ingo Molnara5d8c342008-10-09 11:35:51 +02008246#ifdef CONFIG_SCHED_DEBUG
8247# define SD_INIT_NAME(sd, type) sd->name = #type
8248#else
8249# define SD_INIT_NAME(sd, type) do { } while (0)
8250#endif
8251
Mike Travis7c16ec52008-04-04 18:11:11 -07008252#define SD_INIT(sd, type) sd_init_##type(sd)
Ingo Molnara5d8c342008-10-09 11:35:51 +02008253
Mike Travis7c16ec52008-04-04 18:11:11 -07008254#define SD_INIT_FUNC(type) \
8255static noinline void sd_init_##type(struct sched_domain *sd) \
8256{ \
8257 memset(sd, 0, sizeof(*sd)); \
8258 *sd = SD_##type##_INIT; \
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09008259 sd->level = SD_LV_##type; \
Ingo Molnara5d8c342008-10-09 11:35:51 +02008260 SD_INIT_NAME(sd, type); \
Mike Travis7c16ec52008-04-04 18:11:11 -07008261}
8262
8263SD_INIT_FUNC(CPU)
8264#ifdef CONFIG_NUMA
8265 SD_INIT_FUNC(ALLNODES)
8266 SD_INIT_FUNC(NODE)
8267#endif
8268#ifdef CONFIG_SCHED_SMT
8269 SD_INIT_FUNC(SIBLING)
8270#endif
8271#ifdef CONFIG_SCHED_MC
8272 SD_INIT_FUNC(MC)
8273#endif
8274
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09008275static int default_relax_domain_level = -1;
8276
8277static int __init setup_relax_domain_level(char *str)
8278{
Li Zefan30e0e172008-05-13 10:27:17 +08008279 unsigned long val;
8280
8281 val = simple_strtoul(str, NULL, 0);
8282 if (val < SD_LV_MAX)
8283 default_relax_domain_level = val;
8284
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09008285 return 1;
8286}
8287__setup("relax_domain_level=", setup_relax_domain_level);
8288
8289static void set_domain_attribute(struct sched_domain *sd,
8290 struct sched_domain_attr *attr)
8291{
8292 int request;
8293
8294 if (!attr || attr->relax_domain_level < 0) {
8295 if (default_relax_domain_level < 0)
8296 return;
8297 else
8298 request = default_relax_domain_level;
8299 } else
8300 request = attr->relax_domain_level;
8301 if (request < sd->level) {
8302 /* turn off idle balance on this domain */
8303 sd->flags &= ~(SD_WAKE_IDLE|SD_BALANCE_NEWIDLE);
8304 } else {
8305 /* turn on idle balance on this domain */
8306 sd->flags |= (SD_WAKE_IDLE_FAR|SD_BALANCE_NEWIDLE);
8307 }
8308}
8309
Mike Travis7c16ec52008-04-04 18:11:11 -07008310/*
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07008311 * Build sched domains for a given set of cpus and attach the sched domains
8312 * to the individual cpus
Linus Torvalds1da177e2005-04-16 15:20:36 -07008313 */
Rusty Russell96f874e2008-11-25 02:35:14 +10308314static int __build_sched_domains(const struct cpumask *cpu_map,
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09008315 struct sched_domain_attr *attr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008316{
Rusty Russell3404c8d2008-11-25 02:35:03 +10308317 int i, err = -ENOMEM;
Gregory Haskins57d885f2008-01-25 21:08:18 +01008318 struct root_domain *rd;
Rusty Russell3404c8d2008-11-25 02:35:03 +10308319 cpumask_var_t nodemask, this_sibling_map, this_core_map, send_covered,
8320 tmpmask;
John Hawkesd1b55132005-09-06 15:18:14 -07008321#ifdef CONFIG_NUMA
Rusty Russell3404c8d2008-11-25 02:35:03 +10308322 cpumask_var_t domainspan, covered, notcovered;
John Hawkesd1b55132005-09-06 15:18:14 -07008323 struct sched_group **sched_group_nodes = NULL;
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08008324 int sd_allnodes = 0;
John Hawkesd1b55132005-09-06 15:18:14 -07008325
Rusty Russell3404c8d2008-11-25 02:35:03 +10308326 if (!alloc_cpumask_var(&domainspan, GFP_KERNEL))
8327 goto out;
8328 if (!alloc_cpumask_var(&covered, GFP_KERNEL))
8329 goto free_domainspan;
8330 if (!alloc_cpumask_var(&notcovered, GFP_KERNEL))
8331 goto free_covered;
8332#endif
8333
8334 if (!alloc_cpumask_var(&nodemask, GFP_KERNEL))
8335 goto free_notcovered;
8336 if (!alloc_cpumask_var(&this_sibling_map, GFP_KERNEL))
8337 goto free_nodemask;
8338 if (!alloc_cpumask_var(&this_core_map, GFP_KERNEL))
8339 goto free_this_sibling_map;
8340 if (!alloc_cpumask_var(&send_covered, GFP_KERNEL))
8341 goto free_this_core_map;
8342 if (!alloc_cpumask_var(&tmpmask, GFP_KERNEL))
8343 goto free_send_covered;
8344
8345#ifdef CONFIG_NUMA
John Hawkesd1b55132005-09-06 15:18:14 -07008346 /*
8347 * Allocate the per-node list of sched groups
8348 */
Mike Travis076ac2a2008-05-12 21:21:12 +02008349 sched_group_nodes = kcalloc(nr_node_ids, sizeof(struct sched_group *),
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01008350 GFP_KERNEL);
John Hawkesd1b55132005-09-06 15:18:14 -07008351 if (!sched_group_nodes) {
8352 printk(KERN_WARNING "Can not alloc sched group node list\n");
Rusty Russell3404c8d2008-11-25 02:35:03 +10308353 goto free_tmpmask;
John Hawkesd1b55132005-09-06 15:18:14 -07008354 }
John Hawkesd1b55132005-09-06 15:18:14 -07008355#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07008356
Gregory Haskinsdc938522008-01-25 21:08:26 +01008357 rd = alloc_rootdomain();
Gregory Haskins57d885f2008-01-25 21:08:18 +01008358 if (!rd) {
8359 printk(KERN_WARNING "Cannot alloc root domain\n");
Rusty Russell3404c8d2008-11-25 02:35:03 +10308360 goto free_sched_groups;
Gregory Haskins57d885f2008-01-25 21:08:18 +01008361 }
8362
Mike Travis7c16ec52008-04-04 18:11:11 -07008363#ifdef CONFIG_NUMA
Rusty Russell96f874e2008-11-25 02:35:14 +10308364 sched_group_nodes_bycpu[cpumask_first(cpu_map)] = sched_group_nodes;
Mike Travis7c16ec52008-04-04 18:11:11 -07008365#endif
8366
Linus Torvalds1da177e2005-04-16 15:20:36 -07008367 /*
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07008368 * Set up domains for cpus specified by the cpu_map.
Linus Torvalds1da177e2005-04-16 15:20:36 -07008369 */
Rusty Russellabcd0832008-11-25 02:35:02 +10308370 for_each_cpu(i, cpu_map) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008371 struct sched_domain *sd = NULL, *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008372
Mike Travis6ca09df2008-12-31 18:08:45 -08008373 cpumask_and(nodemask, cpumask_of_node(cpu_to_node(i)), cpu_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008374
8375#ifdef CONFIG_NUMA
Rusty Russell96f874e2008-11-25 02:35:14 +10308376 if (cpumask_weight(cpu_map) >
8377 SD_NODES_PER_DOMAIN*cpumask_weight(nodemask)) {
Rusty Russell62ea9ce2009-01-11 01:04:16 +01008378 sd = &per_cpu(allnodes_domains, i).sd;
Mike Travis7c16ec52008-04-04 18:11:11 -07008379 SD_INIT(sd, ALLNODES);
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09008380 set_domain_attribute(sd, attr);
Rusty Russell758b2cd2008-11-25 02:35:04 +10308381 cpumask_copy(sched_domain_span(sd), cpu_map);
Mike Travis7c16ec52008-04-04 18:11:11 -07008382 cpu_to_allnodes_group(i, cpu_map, &sd->groups, tmpmask);
John Hawkes9c1cfda2005-09-06 15:18:14 -07008383 p = sd;
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08008384 sd_allnodes = 1;
John Hawkes9c1cfda2005-09-06 15:18:14 -07008385 } else
8386 p = NULL;
8387
Rusty Russell62ea9ce2009-01-11 01:04:16 +01008388 sd = &per_cpu(node_domains, i).sd;
Mike Travis7c16ec52008-04-04 18:11:11 -07008389 SD_INIT(sd, NODE);
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09008390 set_domain_attribute(sd, attr);
Rusty Russell758b2cd2008-11-25 02:35:04 +10308391 sched_domain_node_span(cpu_to_node(i), sched_domain_span(sd));
John Hawkes9c1cfda2005-09-06 15:18:14 -07008392 sd->parent = p;
Siddha, Suresh B1a848872006-10-03 01:14:08 -07008393 if (p)
8394 p->child = sd;
Rusty Russell758b2cd2008-11-25 02:35:04 +10308395 cpumask_and(sched_domain_span(sd),
8396 sched_domain_span(sd), cpu_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008397#endif
8398
8399 p = sd;
Rusty Russell6c99e9a2008-11-25 02:35:04 +10308400 sd = &per_cpu(phys_domains, i).sd;
Mike Travis7c16ec52008-04-04 18:11:11 -07008401 SD_INIT(sd, CPU);
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09008402 set_domain_attribute(sd, attr);
Rusty Russell758b2cd2008-11-25 02:35:04 +10308403 cpumask_copy(sched_domain_span(sd), nodemask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008404 sd->parent = p;
Siddha, Suresh B1a848872006-10-03 01:14:08 -07008405 if (p)
8406 p->child = sd;
Mike Travis7c16ec52008-04-04 18:11:11 -07008407 cpu_to_phys_group(i, cpu_map, &sd->groups, tmpmask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008408
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08008409#ifdef CONFIG_SCHED_MC
8410 p = sd;
Rusty Russell6c99e9a2008-11-25 02:35:04 +10308411 sd = &per_cpu(core_domains, i).sd;
Mike Travis7c16ec52008-04-04 18:11:11 -07008412 SD_INIT(sd, MC);
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09008413 set_domain_attribute(sd, attr);
Mike Travis6ca09df2008-12-31 18:08:45 -08008414 cpumask_and(sched_domain_span(sd), cpu_map,
8415 cpu_coregroup_mask(i));
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08008416 sd->parent = p;
Siddha, Suresh B1a848872006-10-03 01:14:08 -07008417 p->child = sd;
Mike Travis7c16ec52008-04-04 18:11:11 -07008418 cpu_to_core_group(i, cpu_map, &sd->groups, tmpmask);
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08008419#endif
8420
Linus Torvalds1da177e2005-04-16 15:20:36 -07008421#ifdef CONFIG_SCHED_SMT
8422 p = sd;
Rusty Russell6c99e9a2008-11-25 02:35:04 +10308423 sd = &per_cpu(cpu_domains, i).sd;
Mike Travis7c16ec52008-04-04 18:11:11 -07008424 SD_INIT(sd, SIBLING);
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09008425 set_domain_attribute(sd, attr);
Rusty Russell758b2cd2008-11-25 02:35:04 +10308426 cpumask_and(sched_domain_span(sd),
Rusty Russellc69fc562009-03-13 14:49:46 +10308427 topology_thread_cpumask(i), cpu_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008428 sd->parent = p;
Siddha, Suresh B1a848872006-10-03 01:14:08 -07008429 p->child = sd;
Mike Travis7c16ec52008-04-04 18:11:11 -07008430 cpu_to_cpu_group(i, cpu_map, &sd->groups, tmpmask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008431#endif
8432 }
8433
8434#ifdef CONFIG_SCHED_SMT
8435 /* Set up CPU (sibling) groups */
Rusty Russellabcd0832008-11-25 02:35:02 +10308436 for_each_cpu(i, cpu_map) {
Rusty Russell96f874e2008-11-25 02:35:14 +10308437 cpumask_and(this_sibling_map,
Rusty Russellc69fc562009-03-13 14:49:46 +10308438 topology_thread_cpumask(i), cpu_map);
Rusty Russell96f874e2008-11-25 02:35:14 +10308439 if (i != cpumask_first(this_sibling_map))
Linus Torvalds1da177e2005-04-16 15:20:36 -07008440 continue;
8441
Ingo Molnardd41f592007-07-09 18:51:59 +02008442 init_sched_build_groups(this_sibling_map, cpu_map,
Mike Travis7c16ec52008-04-04 18:11:11 -07008443 &cpu_to_cpu_group,
8444 send_covered, tmpmask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008445 }
8446#endif
8447
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08008448#ifdef CONFIG_SCHED_MC
8449 /* Set up multi-core groups */
Rusty Russellabcd0832008-11-25 02:35:02 +10308450 for_each_cpu(i, cpu_map) {
Mike Travis6ca09df2008-12-31 18:08:45 -08008451 cpumask_and(this_core_map, cpu_coregroup_mask(i), cpu_map);
Rusty Russell96f874e2008-11-25 02:35:14 +10308452 if (i != cpumask_first(this_core_map))
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08008453 continue;
Mike Travis7c16ec52008-04-04 18:11:11 -07008454
Ingo Molnardd41f592007-07-09 18:51:59 +02008455 init_sched_build_groups(this_core_map, cpu_map,
Mike Travis7c16ec52008-04-04 18:11:11 -07008456 &cpu_to_core_group,
8457 send_covered, tmpmask);
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08008458 }
8459#endif
8460
Linus Torvalds1da177e2005-04-16 15:20:36 -07008461 /* Set up physical groups */
Mike Travis076ac2a2008-05-12 21:21:12 +02008462 for (i = 0; i < nr_node_ids; i++) {
Mike Travis6ca09df2008-12-31 18:08:45 -08008463 cpumask_and(nodemask, cpumask_of_node(i), cpu_map);
Rusty Russell96f874e2008-11-25 02:35:14 +10308464 if (cpumask_empty(nodemask))
Linus Torvalds1da177e2005-04-16 15:20:36 -07008465 continue;
8466
Mike Travis7c16ec52008-04-04 18:11:11 -07008467 init_sched_build_groups(nodemask, cpu_map,
8468 &cpu_to_phys_group,
8469 send_covered, tmpmask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008470 }
8471
8472#ifdef CONFIG_NUMA
8473 /* Set up node groups */
Mike Travis7c16ec52008-04-04 18:11:11 -07008474 if (sd_allnodes) {
Mike Travis7c16ec52008-04-04 18:11:11 -07008475 init_sched_build_groups(cpu_map, cpu_map,
8476 &cpu_to_allnodes_group,
8477 send_covered, tmpmask);
8478 }
John Hawkes9c1cfda2005-09-06 15:18:14 -07008479
Mike Travis076ac2a2008-05-12 21:21:12 +02008480 for (i = 0; i < nr_node_ids; i++) {
John Hawkes9c1cfda2005-09-06 15:18:14 -07008481 /* Set up node groups */
8482 struct sched_group *sg, *prev;
John Hawkes9c1cfda2005-09-06 15:18:14 -07008483 int j;
8484
Rusty Russell96f874e2008-11-25 02:35:14 +10308485 cpumask_clear(covered);
Mike Travis6ca09df2008-12-31 18:08:45 -08008486 cpumask_and(nodemask, cpumask_of_node(i), cpu_map);
Rusty Russell96f874e2008-11-25 02:35:14 +10308487 if (cpumask_empty(nodemask)) {
John Hawkesd1b55132005-09-06 15:18:14 -07008488 sched_group_nodes[i] = NULL;
John Hawkes9c1cfda2005-09-06 15:18:14 -07008489 continue;
John Hawkesd1b55132005-09-06 15:18:14 -07008490 }
John Hawkes9c1cfda2005-09-06 15:18:14 -07008491
Mike Travis4bdbaad2008-04-15 16:35:52 -07008492 sched_domain_node_span(i, domainspan);
Rusty Russell96f874e2008-11-25 02:35:14 +10308493 cpumask_and(domainspan, domainspan, cpu_map);
John Hawkes9c1cfda2005-09-06 15:18:14 -07008494
Rusty Russell6c99e9a2008-11-25 02:35:04 +10308495 sg = kmalloc_node(sizeof(struct sched_group) + cpumask_size(),
8496 GFP_KERNEL, i);
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07008497 if (!sg) {
8498 printk(KERN_WARNING "Can not alloc domain group for "
8499 "node %d\n", i);
8500 goto error;
8501 }
John Hawkes9c1cfda2005-09-06 15:18:14 -07008502 sched_group_nodes[i] = sg;
Rusty Russellabcd0832008-11-25 02:35:02 +10308503 for_each_cpu(j, nodemask) {
John Hawkes9c1cfda2005-09-06 15:18:14 -07008504 struct sched_domain *sd;
Ingo Molnar9761eea2007-07-09 18:52:00 +02008505
Rusty Russell62ea9ce2009-01-11 01:04:16 +01008506 sd = &per_cpu(node_domains, j).sd;
John Hawkes9c1cfda2005-09-06 15:18:14 -07008507 sd->groups = sg;
John Hawkes9c1cfda2005-09-06 15:18:14 -07008508 }
Eric Dumazet5517d862007-05-08 00:32:57 -07008509 sg->__cpu_power = 0;
Rusty Russell758b2cd2008-11-25 02:35:04 +10308510 cpumask_copy(sched_group_cpus(sg), nodemask);
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07008511 sg->next = sg;
Rusty Russell96f874e2008-11-25 02:35:14 +10308512 cpumask_or(covered, covered, nodemask);
John Hawkes9c1cfda2005-09-06 15:18:14 -07008513 prev = sg;
8514
Mike Travis076ac2a2008-05-12 21:21:12 +02008515 for (j = 0; j < nr_node_ids; j++) {
Mike Travis076ac2a2008-05-12 21:21:12 +02008516 int n = (i + j) % nr_node_ids;
John Hawkes9c1cfda2005-09-06 15:18:14 -07008517
Rusty Russell96f874e2008-11-25 02:35:14 +10308518 cpumask_complement(notcovered, covered);
8519 cpumask_and(tmpmask, notcovered, cpu_map);
8520 cpumask_and(tmpmask, tmpmask, domainspan);
8521 if (cpumask_empty(tmpmask))
John Hawkes9c1cfda2005-09-06 15:18:14 -07008522 break;
8523
Mike Travis6ca09df2008-12-31 18:08:45 -08008524 cpumask_and(tmpmask, tmpmask, cpumask_of_node(n));
Rusty Russell96f874e2008-11-25 02:35:14 +10308525 if (cpumask_empty(tmpmask))
John Hawkes9c1cfda2005-09-06 15:18:14 -07008526 continue;
8527
Rusty Russell6c99e9a2008-11-25 02:35:04 +10308528 sg = kmalloc_node(sizeof(struct sched_group) +
8529 cpumask_size(),
Srivatsa Vaddagiri15f0b672006-06-27 02:54:40 -07008530 GFP_KERNEL, i);
John Hawkes9c1cfda2005-09-06 15:18:14 -07008531 if (!sg) {
8532 printk(KERN_WARNING
8533 "Can not alloc domain group for node %d\n", j);
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07008534 goto error;
John Hawkes9c1cfda2005-09-06 15:18:14 -07008535 }
Eric Dumazet5517d862007-05-08 00:32:57 -07008536 sg->__cpu_power = 0;
Rusty Russell758b2cd2008-11-25 02:35:04 +10308537 cpumask_copy(sched_group_cpus(sg), tmpmask);
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07008538 sg->next = prev->next;
Rusty Russell96f874e2008-11-25 02:35:14 +10308539 cpumask_or(covered, covered, tmpmask);
John Hawkes9c1cfda2005-09-06 15:18:14 -07008540 prev->next = sg;
8541 prev = sg;
8542 }
John Hawkes9c1cfda2005-09-06 15:18:14 -07008543 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008544#endif
8545
8546 /* Calculate CPU power for physical packages and nodes */
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07008547#ifdef CONFIG_SCHED_SMT
Rusty Russellabcd0832008-11-25 02:35:02 +10308548 for_each_cpu(i, cpu_map) {
Rusty Russell6c99e9a2008-11-25 02:35:04 +10308549 struct sched_domain *sd = &per_cpu(cpu_domains, i).sd;
Ingo Molnardd41f592007-07-09 18:51:59 +02008550
Siddha, Suresh B89c47102006-10-03 01:14:09 -07008551 init_sched_groups_power(i, sd);
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07008552 }
8553#endif
8554#ifdef CONFIG_SCHED_MC
Rusty Russellabcd0832008-11-25 02:35:02 +10308555 for_each_cpu(i, cpu_map) {
Rusty Russell6c99e9a2008-11-25 02:35:04 +10308556 struct sched_domain *sd = &per_cpu(core_domains, i).sd;
Ingo Molnardd41f592007-07-09 18:51:59 +02008557
Siddha, Suresh B89c47102006-10-03 01:14:09 -07008558 init_sched_groups_power(i, sd);
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07008559 }
8560#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07008561
Rusty Russellabcd0832008-11-25 02:35:02 +10308562 for_each_cpu(i, cpu_map) {
Rusty Russell6c99e9a2008-11-25 02:35:04 +10308563 struct sched_domain *sd = &per_cpu(phys_domains, i).sd;
Ingo Molnardd41f592007-07-09 18:51:59 +02008564
Siddha, Suresh B89c47102006-10-03 01:14:09 -07008565 init_sched_groups_power(i, sd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008566 }
8567
John Hawkes9c1cfda2005-09-06 15:18:14 -07008568#ifdef CONFIG_NUMA
Mike Travis076ac2a2008-05-12 21:21:12 +02008569 for (i = 0; i < nr_node_ids; i++)
Siddha, Suresh B08069032006-03-27 01:15:23 -08008570 init_numa_sched_groups_power(sched_group_nodes[i]);
John Hawkes9c1cfda2005-09-06 15:18:14 -07008571
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08008572 if (sd_allnodes) {
8573 struct sched_group *sg;
Siddha, Suresh Bf712c0c2006-07-30 03:02:59 -07008574
Rusty Russell96f874e2008-11-25 02:35:14 +10308575 cpu_to_allnodes_group(cpumask_first(cpu_map), cpu_map, &sg,
Mike Travis7c16ec52008-04-04 18:11:11 -07008576 tmpmask);
Siddha, Suresh Bf712c0c2006-07-30 03:02:59 -07008577 init_numa_sched_groups_power(sg);
8578 }
John Hawkes9c1cfda2005-09-06 15:18:14 -07008579#endif
8580
Linus Torvalds1da177e2005-04-16 15:20:36 -07008581 /* Attach the domains */
Rusty Russellabcd0832008-11-25 02:35:02 +10308582 for_each_cpu(i, cpu_map) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008583 struct sched_domain *sd;
8584#ifdef CONFIG_SCHED_SMT
Rusty Russell6c99e9a2008-11-25 02:35:04 +10308585 sd = &per_cpu(cpu_domains, i).sd;
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08008586#elif defined(CONFIG_SCHED_MC)
Rusty Russell6c99e9a2008-11-25 02:35:04 +10308587 sd = &per_cpu(core_domains, i).sd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008588#else
Rusty Russell6c99e9a2008-11-25 02:35:04 +10308589 sd = &per_cpu(phys_domains, i).sd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008590#endif
Gregory Haskins57d885f2008-01-25 21:08:18 +01008591 cpu_attach_domain(sd, rd, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008592 }
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07008593
Rusty Russell3404c8d2008-11-25 02:35:03 +10308594 err = 0;
8595
8596free_tmpmask:
8597 free_cpumask_var(tmpmask);
8598free_send_covered:
8599 free_cpumask_var(send_covered);
8600free_this_core_map:
8601 free_cpumask_var(this_core_map);
8602free_this_sibling_map:
8603 free_cpumask_var(this_sibling_map);
8604free_nodemask:
8605 free_cpumask_var(nodemask);
8606free_notcovered:
8607#ifdef CONFIG_NUMA
8608 free_cpumask_var(notcovered);
8609free_covered:
8610 free_cpumask_var(covered);
8611free_domainspan:
8612 free_cpumask_var(domainspan);
8613out:
8614#endif
8615 return err;
8616
8617free_sched_groups:
8618#ifdef CONFIG_NUMA
8619 kfree(sched_group_nodes);
8620#endif
8621 goto free_tmpmask;
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07008622
Siddha, Suresh Ba6160582006-10-03 01:14:06 -07008623#ifdef CONFIG_NUMA
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07008624error:
Mike Travis7c16ec52008-04-04 18:11:11 -07008625 free_sched_groups(cpu_map, tmpmask);
Rusty Russellc6c49272008-11-25 02:35:05 +10308626 free_rootdomain(rd);
Rusty Russell3404c8d2008-11-25 02:35:03 +10308627 goto free_tmpmask;
Siddha, Suresh Ba6160582006-10-03 01:14:06 -07008628#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07008629}
Paul Jackson029190c2007-10-18 23:40:20 -07008630
Rusty Russell96f874e2008-11-25 02:35:14 +10308631static int build_sched_domains(const struct cpumask *cpu_map)
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09008632{
8633 return __build_sched_domains(cpu_map, NULL);
8634}
8635
Rusty Russell96f874e2008-11-25 02:35:14 +10308636static struct cpumask *doms_cur; /* current sched domains */
Paul Jackson029190c2007-10-18 23:40:20 -07008637static int ndoms_cur; /* number of sched domains in 'doms_cur' */
Ingo Molnar4285f5942008-05-16 17:47:14 +02008638static struct sched_domain_attr *dattr_cur;
8639 /* attribues of custom domains in 'doms_cur' */
Paul Jackson029190c2007-10-18 23:40:20 -07008640
8641/*
8642 * Special case: If a kmalloc of a doms_cur partition (array of
Rusty Russell42128232008-11-25 02:35:12 +10308643 * cpumask) fails, then fallback to a single sched domain,
8644 * as determined by the single cpumask fallback_doms.
Paul Jackson029190c2007-10-18 23:40:20 -07008645 */
Rusty Russell42128232008-11-25 02:35:12 +10308646static cpumask_var_t fallback_doms;
Paul Jackson029190c2007-10-18 23:40:20 -07008647
Heiko Carstensee79d1b2008-12-09 18:49:50 +01008648/*
8649 * arch_update_cpu_topology lets virtualized architectures update the
8650 * cpu core maps. It is supposed to return 1 if the topology changed
8651 * or 0 if it stayed the same.
8652 */
8653int __attribute__((weak)) arch_update_cpu_topology(void)
Heiko Carstens22e52b02008-03-12 18:31:59 +01008654{
Heiko Carstensee79d1b2008-12-09 18:49:50 +01008655 return 0;
Heiko Carstens22e52b02008-03-12 18:31:59 +01008656}
8657
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07008658/*
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01008659 * Set up scheduler domains and groups. Callers must hold the hotplug lock.
Paul Jackson029190c2007-10-18 23:40:20 -07008660 * For now this just excludes isolated cpus, but could be used to
8661 * exclude other special cases in the future.
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07008662 */
Rusty Russell96f874e2008-11-25 02:35:14 +10308663static int arch_init_sched_domains(const struct cpumask *cpu_map)
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07008664{
Milton Miller73785472007-10-24 18:23:48 +02008665 int err;
8666
Heiko Carstens22e52b02008-03-12 18:31:59 +01008667 arch_update_cpu_topology();
Paul Jackson029190c2007-10-18 23:40:20 -07008668 ndoms_cur = 1;
Rusty Russell96f874e2008-11-25 02:35:14 +10308669 doms_cur = kmalloc(cpumask_size(), GFP_KERNEL);
Paul Jackson029190c2007-10-18 23:40:20 -07008670 if (!doms_cur)
Rusty Russell42128232008-11-25 02:35:12 +10308671 doms_cur = fallback_doms;
Rusty Russelldcc30a32008-11-25 02:35:12 +10308672 cpumask_andnot(doms_cur, cpu_map, cpu_isolated_map);
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09008673 dattr_cur = NULL;
Milton Miller73785472007-10-24 18:23:48 +02008674 err = build_sched_domains(doms_cur);
Milton Miller6382bc92007-10-15 17:00:19 +02008675 register_sched_domain_sysctl();
Milton Miller73785472007-10-24 18:23:48 +02008676
8677 return err;
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07008678}
8679
Rusty Russell96f874e2008-11-25 02:35:14 +10308680static void arch_destroy_sched_domains(const struct cpumask *cpu_map,
8681 struct cpumask *tmpmask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008682{
Mike Travis7c16ec52008-04-04 18:11:11 -07008683 free_sched_groups(cpu_map, tmpmask);
John Hawkes9c1cfda2005-09-06 15:18:14 -07008684}
Linus Torvalds1da177e2005-04-16 15:20:36 -07008685
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07008686/*
8687 * Detach sched domains from a group of cpus specified in cpu_map
8688 * These cpus will now be attached to the NULL domain
8689 */
Rusty Russell96f874e2008-11-25 02:35:14 +10308690static void detach_destroy_domains(const struct cpumask *cpu_map)
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07008691{
Rusty Russell96f874e2008-11-25 02:35:14 +10308692 /* Save because hotplug lock held. */
8693 static DECLARE_BITMAP(tmpmask, CONFIG_NR_CPUS);
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07008694 int i;
8695
Rusty Russellabcd0832008-11-25 02:35:02 +10308696 for_each_cpu(i, cpu_map)
Gregory Haskins57d885f2008-01-25 21:08:18 +01008697 cpu_attach_domain(NULL, &def_root_domain, i);
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07008698 synchronize_sched();
Rusty Russell96f874e2008-11-25 02:35:14 +10308699 arch_destroy_sched_domains(cpu_map, to_cpumask(tmpmask));
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07008700}
8701
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09008702/* handle null as "default" */
8703static int dattrs_equal(struct sched_domain_attr *cur, int idx_cur,
8704 struct sched_domain_attr *new, int idx_new)
8705{
8706 struct sched_domain_attr tmp;
8707
8708 /* fast path */
8709 if (!new && !cur)
8710 return 1;
8711
8712 tmp = SD_ATTR_INIT;
8713 return !memcmp(cur ? (cur + idx_cur) : &tmp,
8714 new ? (new + idx_new) : &tmp,
8715 sizeof(struct sched_domain_attr));
8716}
8717
Paul Jackson029190c2007-10-18 23:40:20 -07008718/*
8719 * Partition sched domains as specified by the 'ndoms_new'
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01008720 * cpumasks in the array doms_new[] of cpumasks. This compares
Paul Jackson029190c2007-10-18 23:40:20 -07008721 * doms_new[] to the current sched domain partitioning, doms_cur[].
8722 * It destroys each deleted domain and builds each new domain.
8723 *
Rusty Russell96f874e2008-11-25 02:35:14 +10308724 * 'doms_new' is an array of cpumask's of length 'ndoms_new'.
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01008725 * The masks don't intersect (don't overlap.) We should setup one
8726 * sched domain for each mask. CPUs not in any of the cpumasks will
8727 * not be load balanced. If the same cpumask appears both in the
Paul Jackson029190c2007-10-18 23:40:20 -07008728 * current 'doms_cur' domains and in the new 'doms_new', we can leave
8729 * it as it is.
8730 *
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01008731 * The passed in 'doms_new' should be kmalloc'd. This routine takes
8732 * ownership of it and will kfree it when done with it. If the caller
Li Zefan700018e2008-11-18 14:02:03 +08008733 * failed the kmalloc call, then it can pass in doms_new == NULL &&
8734 * ndoms_new == 1, and partition_sched_domains() will fallback to
8735 * the single partition 'fallback_doms', it also forces the domains
8736 * to be rebuilt.
Paul Jackson029190c2007-10-18 23:40:20 -07008737 *
Rusty Russell96f874e2008-11-25 02:35:14 +10308738 * If doms_new == NULL it will be replaced with cpu_online_mask.
Li Zefan700018e2008-11-18 14:02:03 +08008739 * ndoms_new == 0 is a special case for destroying existing domains,
8740 * and it will not create the default domain.
Max Krasnyanskydfb512e2008-08-29 13:11:41 -07008741 *
Paul Jackson029190c2007-10-18 23:40:20 -07008742 * Call with hotplug lock held
8743 */
Rusty Russell96f874e2008-11-25 02:35:14 +10308744/* FIXME: Change to struct cpumask *doms_new[] */
8745void partition_sched_domains(int ndoms_new, struct cpumask *doms_new,
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09008746 struct sched_domain_attr *dattr_new)
Paul Jackson029190c2007-10-18 23:40:20 -07008747{
Max Krasnyanskydfb512e2008-08-29 13:11:41 -07008748 int i, j, n;
Heiko Carstensd65bd5e2008-12-09 18:49:51 +01008749 int new_topology;
Paul Jackson029190c2007-10-18 23:40:20 -07008750
Heiko Carstens712555e2008-04-28 11:33:07 +02008751 mutex_lock(&sched_domains_mutex);
Srivatsa Vaddagiria1835612008-01-25 21:08:00 +01008752
Milton Miller73785472007-10-24 18:23:48 +02008753 /* always unregister in case we don't destroy any domains */
8754 unregister_sched_domain_sysctl();
8755
Heiko Carstensd65bd5e2008-12-09 18:49:51 +01008756 /* Let architecture update cpu core mappings. */
8757 new_topology = arch_update_cpu_topology();
8758
Max Krasnyanskydfb512e2008-08-29 13:11:41 -07008759 n = doms_new ? ndoms_new : 0;
Paul Jackson029190c2007-10-18 23:40:20 -07008760
8761 /* Destroy deleted domains */
8762 for (i = 0; i < ndoms_cur; i++) {
Heiko Carstensd65bd5e2008-12-09 18:49:51 +01008763 for (j = 0; j < n && !new_topology; j++) {
Rusty Russell96f874e2008-11-25 02:35:14 +10308764 if (cpumask_equal(&doms_cur[i], &doms_new[j])
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09008765 && dattrs_equal(dattr_cur, i, dattr_new, j))
Paul Jackson029190c2007-10-18 23:40:20 -07008766 goto match1;
8767 }
8768 /* no match - a current sched domain not in new doms_new[] */
8769 detach_destroy_domains(doms_cur + i);
8770match1:
8771 ;
8772 }
8773
Max Krasnyanskye761b772008-07-15 04:43:49 -07008774 if (doms_new == NULL) {
8775 ndoms_cur = 0;
Rusty Russell42128232008-11-25 02:35:12 +10308776 doms_new = fallback_doms;
Rusty Russelldcc30a32008-11-25 02:35:12 +10308777 cpumask_andnot(&doms_new[0], cpu_online_mask, cpu_isolated_map);
Li Zefanfaa2f982008-11-04 16:20:23 +08008778 WARN_ON_ONCE(dattr_new);
Max Krasnyanskye761b772008-07-15 04:43:49 -07008779 }
8780
Paul Jackson029190c2007-10-18 23:40:20 -07008781 /* Build new domains */
8782 for (i = 0; i < ndoms_new; i++) {
Heiko Carstensd65bd5e2008-12-09 18:49:51 +01008783 for (j = 0; j < ndoms_cur && !new_topology; j++) {
Rusty Russell96f874e2008-11-25 02:35:14 +10308784 if (cpumask_equal(&doms_new[i], &doms_cur[j])
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09008785 && dattrs_equal(dattr_new, i, dattr_cur, j))
Paul Jackson029190c2007-10-18 23:40:20 -07008786 goto match2;
8787 }
8788 /* no match - add a new doms_new */
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09008789 __build_sched_domains(doms_new + i,
8790 dattr_new ? dattr_new + i : NULL);
Paul Jackson029190c2007-10-18 23:40:20 -07008791match2:
8792 ;
8793 }
8794
8795 /* Remember the new sched domains */
Rusty Russell42128232008-11-25 02:35:12 +10308796 if (doms_cur != fallback_doms)
Paul Jackson029190c2007-10-18 23:40:20 -07008797 kfree(doms_cur);
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09008798 kfree(dattr_cur); /* kfree(NULL) is safe */
Paul Jackson029190c2007-10-18 23:40:20 -07008799 doms_cur = doms_new;
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09008800 dattr_cur = dattr_new;
Paul Jackson029190c2007-10-18 23:40:20 -07008801 ndoms_cur = ndoms_new;
Milton Miller73785472007-10-24 18:23:48 +02008802
8803 register_sched_domain_sysctl();
Srivatsa Vaddagiria1835612008-01-25 21:08:00 +01008804
Heiko Carstens712555e2008-04-28 11:33:07 +02008805 mutex_unlock(&sched_domains_mutex);
Paul Jackson029190c2007-10-18 23:40:20 -07008806}
8807
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07008808#if defined(CONFIG_SCHED_MC) || defined(CONFIG_SCHED_SMT)
Li Zefanc70f22d2009-01-05 19:07:50 +08008809static void arch_reinit_sched_domains(void)
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07008810{
Gautham R Shenoy95402b32008-01-25 21:08:02 +01008811 get_online_cpus();
Max Krasnyanskydfb512e2008-08-29 13:11:41 -07008812
8813 /* Destroy domains first to force the rebuild */
8814 partition_sched_domains(0, NULL, NULL);
8815
Max Krasnyanskye761b772008-07-15 04:43:49 -07008816 rebuild_sched_domains();
Gautham R Shenoy95402b32008-01-25 21:08:02 +01008817 put_online_cpus();
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07008818}
8819
8820static ssize_t sched_power_savings_store(const char *buf, size_t count, int smt)
8821{
Gautham R Shenoyafb8a9b2008-12-18 23:26:09 +05308822 unsigned int level = 0;
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07008823
Gautham R Shenoyafb8a9b2008-12-18 23:26:09 +05308824 if (sscanf(buf, "%u", &level) != 1)
8825 return -EINVAL;
8826
8827 /*
8828 * level is always be positive so don't check for
8829 * level < POWERSAVINGS_BALANCE_NONE which is 0
8830 * What happens on 0 or 1 byte write,
8831 * need to check for count as well?
8832 */
8833
8834 if (level >= MAX_POWERSAVINGS_BALANCE_LEVELS)
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07008835 return -EINVAL;
8836
8837 if (smt)
Gautham R Shenoyafb8a9b2008-12-18 23:26:09 +05308838 sched_smt_power_savings = level;
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07008839 else
Gautham R Shenoyafb8a9b2008-12-18 23:26:09 +05308840 sched_mc_power_savings = level;
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07008841
Li Zefanc70f22d2009-01-05 19:07:50 +08008842 arch_reinit_sched_domains();
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07008843
Li Zefanc70f22d2009-01-05 19:07:50 +08008844 return count;
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07008845}
8846
Adrian Bunk6707de002007-08-12 18:08:19 +02008847#ifdef CONFIG_SCHED_MC
Andi Kleenf718cd42008-07-29 22:33:52 -07008848static ssize_t sched_mc_power_savings_show(struct sysdev_class *class,
8849 char *page)
Adrian Bunk6707de002007-08-12 18:08:19 +02008850{
8851 return sprintf(page, "%u\n", sched_mc_power_savings);
8852}
Andi Kleenf718cd42008-07-29 22:33:52 -07008853static ssize_t sched_mc_power_savings_store(struct sysdev_class *class,
Adrian Bunk6707de002007-08-12 18:08:19 +02008854 const char *buf, size_t count)
8855{
8856 return sched_power_savings_store(buf, count, 0);
8857}
Andi Kleenf718cd42008-07-29 22:33:52 -07008858static SYSDEV_CLASS_ATTR(sched_mc_power_savings, 0644,
8859 sched_mc_power_savings_show,
8860 sched_mc_power_savings_store);
Adrian Bunk6707de002007-08-12 18:08:19 +02008861#endif
8862
8863#ifdef CONFIG_SCHED_SMT
Andi Kleenf718cd42008-07-29 22:33:52 -07008864static ssize_t sched_smt_power_savings_show(struct sysdev_class *dev,
8865 char *page)
Adrian Bunk6707de002007-08-12 18:08:19 +02008866{
8867 return sprintf(page, "%u\n", sched_smt_power_savings);
8868}
Andi Kleenf718cd42008-07-29 22:33:52 -07008869static ssize_t sched_smt_power_savings_store(struct sysdev_class *dev,
Adrian Bunk6707de002007-08-12 18:08:19 +02008870 const char *buf, size_t count)
8871{
8872 return sched_power_savings_store(buf, count, 1);
8873}
Andi Kleenf718cd42008-07-29 22:33:52 -07008874static SYSDEV_CLASS_ATTR(sched_smt_power_savings, 0644,
8875 sched_smt_power_savings_show,
Adrian Bunk6707de002007-08-12 18:08:19 +02008876 sched_smt_power_savings_store);
8877#endif
8878
Li Zefan39aac642009-01-05 19:18:02 +08008879int __init sched_create_sysfs_power_savings_entries(struct sysdev_class *cls)
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07008880{
8881 int err = 0;
Ingo Molnar48f24c42006-07-03 00:25:40 -07008882
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07008883#ifdef CONFIG_SCHED_SMT
8884 if (smt_capable())
8885 err = sysfs_create_file(&cls->kset.kobj,
8886 &attr_sched_smt_power_savings.attr);
8887#endif
8888#ifdef CONFIG_SCHED_MC
8889 if (!err && mc_capable())
8890 err = sysfs_create_file(&cls->kset.kobj,
8891 &attr_sched_mc_power_savings.attr);
8892#endif
8893 return err;
8894}
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +02008895#endif /* CONFIG_SCHED_MC || CONFIG_SCHED_SMT */
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07008896
Max Krasnyanskye761b772008-07-15 04:43:49 -07008897#ifndef CONFIG_CPUSETS
Linus Torvalds1da177e2005-04-16 15:20:36 -07008898/*
Max Krasnyanskye761b772008-07-15 04:43:49 -07008899 * Add online and remove offline CPUs from the scheduler domains.
8900 * When cpusets are enabled they take over this function.
Linus Torvalds1da177e2005-04-16 15:20:36 -07008901 */
8902static int update_sched_domains(struct notifier_block *nfb,
8903 unsigned long action, void *hcpu)
8904{
Max Krasnyanskye761b772008-07-15 04:43:49 -07008905 switch (action) {
8906 case CPU_ONLINE:
8907 case CPU_ONLINE_FROZEN:
8908 case CPU_DEAD:
8909 case CPU_DEAD_FROZEN:
Max Krasnyanskydfb512e2008-08-29 13:11:41 -07008910 partition_sched_domains(1, NULL, NULL);
Max Krasnyanskye761b772008-07-15 04:43:49 -07008911 return NOTIFY_OK;
8912
8913 default:
8914 return NOTIFY_DONE;
8915 }
8916}
8917#endif
8918
8919static int update_runtime(struct notifier_block *nfb,
8920 unsigned long action, void *hcpu)
8921{
Peter Zijlstra7def2be2008-06-05 14:49:58 +02008922 int cpu = (int)(long)hcpu;
8923
Linus Torvalds1da177e2005-04-16 15:20:36 -07008924 switch (action) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008925 case CPU_DOWN_PREPARE:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07008926 case CPU_DOWN_PREPARE_FROZEN:
Peter Zijlstra7def2be2008-06-05 14:49:58 +02008927 disable_runtime(cpu_rq(cpu));
Linus Torvalds1da177e2005-04-16 15:20:36 -07008928 return NOTIFY_OK;
8929
Linus Torvalds1da177e2005-04-16 15:20:36 -07008930 case CPU_DOWN_FAILED:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07008931 case CPU_DOWN_FAILED_FROZEN:
Linus Torvalds1da177e2005-04-16 15:20:36 -07008932 case CPU_ONLINE:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07008933 case CPU_ONLINE_FROZEN:
Peter Zijlstra7def2be2008-06-05 14:49:58 +02008934 enable_runtime(cpu_rq(cpu));
Max Krasnyanskye761b772008-07-15 04:43:49 -07008935 return NOTIFY_OK;
8936
Linus Torvalds1da177e2005-04-16 15:20:36 -07008937 default:
8938 return NOTIFY_DONE;
8939 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008940}
Linus Torvalds1da177e2005-04-16 15:20:36 -07008941
8942void __init sched_init_smp(void)
8943{
Rusty Russelldcc30a32008-11-25 02:35:12 +10308944 cpumask_var_t non_isolated_cpus;
8945
8946 alloc_cpumask_var(&non_isolated_cpus, GFP_KERNEL);
Nick Piggin5c1e1762006-10-03 01:14:04 -07008947
Mike Travis434d53b2008-04-04 18:11:04 -07008948#if defined(CONFIG_NUMA)
8949 sched_group_nodes_bycpu = kzalloc(nr_cpu_ids * sizeof(void **),
8950 GFP_KERNEL);
8951 BUG_ON(sched_group_nodes_bycpu == NULL);
8952#endif
Gautham R Shenoy95402b32008-01-25 21:08:02 +01008953 get_online_cpus();
Heiko Carstens712555e2008-04-28 11:33:07 +02008954 mutex_lock(&sched_domains_mutex);
Rusty Russelldcc30a32008-11-25 02:35:12 +10308955 arch_init_sched_domains(cpu_online_mask);
8956 cpumask_andnot(non_isolated_cpus, cpu_possible_mask, cpu_isolated_map);
8957 if (cpumask_empty(non_isolated_cpus))
8958 cpumask_set_cpu(smp_processor_id(), non_isolated_cpus);
Heiko Carstens712555e2008-04-28 11:33:07 +02008959 mutex_unlock(&sched_domains_mutex);
Gautham R Shenoy95402b32008-01-25 21:08:02 +01008960 put_online_cpus();
Max Krasnyanskye761b772008-07-15 04:43:49 -07008961
8962#ifndef CONFIG_CPUSETS
Linus Torvalds1da177e2005-04-16 15:20:36 -07008963 /* XXX: Theoretical race here - CPU may be hotplugged now */
8964 hotcpu_notifier(update_sched_domains, 0);
Max Krasnyanskye761b772008-07-15 04:43:49 -07008965#endif
8966
8967 /* RT runtime code needs to handle some hotplug events */
8968 hotcpu_notifier(update_runtime, 0);
8969
Peter Zijlstrab328ca12008-04-29 10:02:46 +02008970 init_hrtick();
Nick Piggin5c1e1762006-10-03 01:14:04 -07008971
8972 /* Move init over to a non-isolated CPU */
Rusty Russelldcc30a32008-11-25 02:35:12 +10308973 if (set_cpus_allowed_ptr(current, non_isolated_cpus) < 0)
Nick Piggin5c1e1762006-10-03 01:14:04 -07008974 BUG();
Ingo Molnar19978ca2007-11-09 22:39:38 +01008975 sched_init_granularity();
Rusty Russelldcc30a32008-11-25 02:35:12 +10308976 free_cpumask_var(non_isolated_cpus);
Rusty Russell42128232008-11-25 02:35:12 +10308977
8978 alloc_cpumask_var(&fallback_doms, GFP_KERNEL);
Rusty Russell0e3900e2008-11-25 02:35:13 +10308979 init_sched_rt_class();
Linus Torvalds1da177e2005-04-16 15:20:36 -07008980}
8981#else
8982void __init sched_init_smp(void)
8983{
Ingo Molnar19978ca2007-11-09 22:39:38 +01008984 sched_init_granularity();
Linus Torvalds1da177e2005-04-16 15:20:36 -07008985}
8986#endif /* CONFIG_SMP */
8987
8988int in_sched_functions(unsigned long addr)
8989{
Linus Torvalds1da177e2005-04-16 15:20:36 -07008990 return in_lock_functions(addr) ||
8991 (addr >= (unsigned long)__sched_text_start
8992 && addr < (unsigned long)__sched_text_end);
8993}
8994
Alexey Dobriyana9957442007-10-15 17:00:13 +02008995static void init_cfs_rq(struct cfs_rq *cfs_rq, struct rq *rq)
Ingo Molnardd41f592007-07-09 18:51:59 +02008996{
8997 cfs_rq->tasks_timeline = RB_ROOT;
Peter Zijlstra4a55bd52008-04-19 19:45:00 +02008998 INIT_LIST_HEAD(&cfs_rq->tasks);
Ingo Molnardd41f592007-07-09 18:51:59 +02008999#ifdef CONFIG_FAIR_GROUP_SCHED
9000 cfs_rq->rq = rq;
9001#endif
Peter Zijlstra67e9fb22007-10-15 17:00:10 +02009002 cfs_rq->min_vruntime = (u64)(-(1LL << 20));
Ingo Molnardd41f592007-07-09 18:51:59 +02009003}
9004
Peter Zijlstrafa85ae22008-01-25 21:08:29 +01009005static void init_rt_rq(struct rt_rq *rt_rq, struct rq *rq)
9006{
9007 struct rt_prio_array *array;
9008 int i;
9009
9010 array = &rt_rq->active;
9011 for (i = 0; i < MAX_RT_PRIO; i++) {
9012 INIT_LIST_HEAD(array->queue + i);
9013 __clear_bit(i, array->bitmap);
9014 }
9015 /* delimiter for bitsearch: */
9016 __set_bit(MAX_RT_PRIO, array->bitmap);
9017
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01009018#if defined CONFIG_SMP || defined CONFIG_RT_GROUP_SCHED
Gregory Haskinse864c492008-12-29 09:39:49 -05009019 rt_rq->highest_prio.curr = MAX_RT_PRIO;
Gregory Haskins398a1532009-01-14 09:10:04 -05009020#ifdef CONFIG_SMP
Gregory Haskinse864c492008-12-29 09:39:49 -05009021 rt_rq->highest_prio.next = MAX_RT_PRIO;
Peter Zijlstra48d5e252008-01-25 21:08:31 +01009022#endif
Peter Zijlstrafa85ae22008-01-25 21:08:29 +01009023#endif
9024#ifdef CONFIG_SMP
9025 rt_rq->rt_nr_migratory = 0;
Peter Zijlstrafa85ae22008-01-25 21:08:29 +01009026 rt_rq->overloaded = 0;
Gregory Haskins917b6272008-12-29 09:39:53 -05009027 plist_head_init(&rq->rt.pushable_tasks, &rq->lock);
Peter Zijlstrafa85ae22008-01-25 21:08:29 +01009028#endif
9029
9030 rt_rq->rt_time = 0;
9031 rt_rq->rt_throttled = 0;
Peter Zijlstraac086bc2008-04-19 19:44:58 +02009032 rt_rq->rt_runtime = 0;
9033 spin_lock_init(&rt_rq->rt_runtime_lock);
Peter Zijlstra6f505b12008-01-25 21:08:30 +01009034
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01009035#ifdef CONFIG_RT_GROUP_SCHED
Peter Zijlstra23b0fdf2008-02-13 15:45:39 +01009036 rt_rq->rt_nr_boosted = 0;
Peter Zijlstra6f505b12008-01-25 21:08:30 +01009037 rt_rq->rq = rq;
9038#endif
Peter Zijlstrafa85ae22008-01-25 21:08:29 +01009039}
9040
Peter Zijlstra6f505b12008-01-25 21:08:30 +01009041#ifdef CONFIG_FAIR_GROUP_SCHED
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02009042static void init_tg_cfs_entry(struct task_group *tg, struct cfs_rq *cfs_rq,
9043 struct sched_entity *se, int cpu, int add,
9044 struct sched_entity *parent)
Peter Zijlstra6f505b12008-01-25 21:08:30 +01009045{
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02009046 struct rq *rq = cpu_rq(cpu);
Peter Zijlstra6f505b12008-01-25 21:08:30 +01009047 tg->cfs_rq[cpu] = cfs_rq;
9048 init_cfs_rq(cfs_rq, rq);
9049 cfs_rq->tg = tg;
9050 if (add)
9051 list_add(&cfs_rq->leaf_cfs_rq_list, &rq->leaf_cfs_rq_list);
9052
9053 tg->se[cpu] = se;
Dhaval Giani354d60c2008-04-19 19:44:59 +02009054 /* se could be NULL for init_task_group */
9055 if (!se)
9056 return;
9057
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02009058 if (!parent)
9059 se->cfs_rq = &rq->cfs;
9060 else
9061 se->cfs_rq = parent->my_q;
9062
Peter Zijlstra6f505b12008-01-25 21:08:30 +01009063 se->my_q = cfs_rq;
9064 se->load.weight = tg->shares;
Peter Zijlstrae05510d2008-05-05 23:56:17 +02009065 se->load.inv_weight = 0;
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02009066 se->parent = parent;
Peter Zijlstra6f505b12008-01-25 21:08:30 +01009067}
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01009068#endif
Peter Zijlstra6f505b12008-01-25 21:08:30 +01009069
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01009070#ifdef CONFIG_RT_GROUP_SCHED
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02009071static void init_tg_rt_entry(struct task_group *tg, struct rt_rq *rt_rq,
9072 struct sched_rt_entity *rt_se, int cpu, int add,
9073 struct sched_rt_entity *parent)
Peter Zijlstra6f505b12008-01-25 21:08:30 +01009074{
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02009075 struct rq *rq = cpu_rq(cpu);
9076
Peter Zijlstra6f505b12008-01-25 21:08:30 +01009077 tg->rt_rq[cpu] = rt_rq;
9078 init_rt_rq(rt_rq, rq);
9079 rt_rq->tg = tg;
9080 rt_rq->rt_se = rt_se;
Peter Zijlstraac086bc2008-04-19 19:44:58 +02009081 rt_rq->rt_runtime = tg->rt_bandwidth.rt_runtime;
Peter Zijlstra6f505b12008-01-25 21:08:30 +01009082 if (add)
9083 list_add(&rt_rq->leaf_rt_rq_list, &rq->leaf_rt_rq_list);
9084
9085 tg->rt_se[cpu] = rt_se;
Dhaval Giani354d60c2008-04-19 19:44:59 +02009086 if (!rt_se)
9087 return;
9088
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02009089 if (!parent)
9090 rt_se->rt_rq = &rq->rt;
9091 else
9092 rt_se->rt_rq = parent->my_q;
9093
Peter Zijlstra6f505b12008-01-25 21:08:30 +01009094 rt_se->my_q = rt_rq;
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02009095 rt_se->parent = parent;
Peter Zijlstra6f505b12008-01-25 21:08:30 +01009096 INIT_LIST_HEAD(&rt_se->run_list);
9097}
9098#endif
9099
Linus Torvalds1da177e2005-04-16 15:20:36 -07009100void __init sched_init(void)
9101{
Ingo Molnardd41f592007-07-09 18:51:59 +02009102 int i, j;
Mike Travis434d53b2008-04-04 18:11:04 -07009103 unsigned long alloc_size = 0, ptr;
9104
9105#ifdef CONFIG_FAIR_GROUP_SCHED
9106 alloc_size += 2 * nr_cpu_ids * sizeof(void **);
9107#endif
9108#ifdef CONFIG_RT_GROUP_SCHED
9109 alloc_size += 2 * nr_cpu_ids * sizeof(void **);
9110#endif
Peter Zijlstraeff766a2008-04-19 19:45:00 +02009111#ifdef CONFIG_USER_SCHED
9112 alloc_size *= 2;
9113#endif
Rusty Russelldf7c8e82009-03-19 15:22:20 +10309114#ifdef CONFIG_CPUMASK_OFFSTACK
Rusty Russell8c083f02009-03-19 15:22:20 +10309115 alloc_size += num_possible_cpus() * cpumask_size();
Rusty Russelldf7c8e82009-03-19 15:22:20 +10309116#endif
Mike Travis434d53b2008-04-04 18:11:04 -07009117 /*
9118 * As sched_init() is called before page_alloc is setup,
9119 * we use alloc_bootmem().
9120 */
9121 if (alloc_size) {
Pekka Enberg36b7b6d2009-06-10 23:42:36 +03009122 ptr = (unsigned long)kzalloc(alloc_size, GFP_NOWAIT);
Mike Travis434d53b2008-04-04 18:11:04 -07009123
9124#ifdef CONFIG_FAIR_GROUP_SCHED
9125 init_task_group.se = (struct sched_entity **)ptr;
9126 ptr += nr_cpu_ids * sizeof(void **);
9127
9128 init_task_group.cfs_rq = (struct cfs_rq **)ptr;
9129 ptr += nr_cpu_ids * sizeof(void **);
Peter Zijlstraeff766a2008-04-19 19:45:00 +02009130
9131#ifdef CONFIG_USER_SCHED
9132 root_task_group.se = (struct sched_entity **)ptr;
9133 ptr += nr_cpu_ids * sizeof(void **);
9134
9135 root_task_group.cfs_rq = (struct cfs_rq **)ptr;
9136 ptr += nr_cpu_ids * sizeof(void **);
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +02009137#endif /* CONFIG_USER_SCHED */
9138#endif /* CONFIG_FAIR_GROUP_SCHED */
Mike Travis434d53b2008-04-04 18:11:04 -07009139#ifdef CONFIG_RT_GROUP_SCHED
9140 init_task_group.rt_se = (struct sched_rt_entity **)ptr;
9141 ptr += nr_cpu_ids * sizeof(void **);
9142
9143 init_task_group.rt_rq = (struct rt_rq **)ptr;
Peter Zijlstraeff766a2008-04-19 19:45:00 +02009144 ptr += nr_cpu_ids * sizeof(void **);
9145
9146#ifdef CONFIG_USER_SCHED
9147 root_task_group.rt_se = (struct sched_rt_entity **)ptr;
9148 ptr += nr_cpu_ids * sizeof(void **);
9149
9150 root_task_group.rt_rq = (struct rt_rq **)ptr;
9151 ptr += nr_cpu_ids * sizeof(void **);
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +02009152#endif /* CONFIG_USER_SCHED */
9153#endif /* CONFIG_RT_GROUP_SCHED */
Rusty Russelldf7c8e82009-03-19 15:22:20 +10309154#ifdef CONFIG_CPUMASK_OFFSTACK
9155 for_each_possible_cpu(i) {
9156 per_cpu(load_balance_tmpmask, i) = (void *)ptr;
9157 ptr += cpumask_size();
9158 }
9159#endif /* CONFIG_CPUMASK_OFFSTACK */
Mike Travis434d53b2008-04-04 18:11:04 -07009160 }
Ingo Molnardd41f592007-07-09 18:51:59 +02009161
Gregory Haskins57d885f2008-01-25 21:08:18 +01009162#ifdef CONFIG_SMP
9163 init_defrootdomain();
9164#endif
9165
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02009166 init_rt_bandwidth(&def_rt_bandwidth,
9167 global_rt_period(), global_rt_runtime());
9168
9169#ifdef CONFIG_RT_GROUP_SCHED
9170 init_rt_bandwidth(&init_task_group.rt_bandwidth,
9171 global_rt_period(), global_rt_runtime());
Peter Zijlstraeff766a2008-04-19 19:45:00 +02009172#ifdef CONFIG_USER_SCHED
9173 init_rt_bandwidth(&root_task_group.rt_bandwidth,
9174 global_rt_period(), RUNTIME_INF);
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +02009175#endif /* CONFIG_USER_SCHED */
9176#endif /* CONFIG_RT_GROUP_SCHED */
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02009177
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01009178#ifdef CONFIG_GROUP_SCHED
Peter Zijlstra6f505b12008-01-25 21:08:30 +01009179 list_add(&init_task_group.list, &task_groups);
Peter Zijlstraf473aa52008-04-19 19:45:00 +02009180 INIT_LIST_HEAD(&init_task_group.children);
9181
9182#ifdef CONFIG_USER_SCHED
9183 INIT_LIST_HEAD(&root_task_group.children);
9184 init_task_group.parent = &root_task_group;
9185 list_add(&init_task_group.siblings, &root_task_group.children);
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +02009186#endif /* CONFIG_USER_SCHED */
9187#endif /* CONFIG_GROUP_SCHED */
Peter Zijlstra6f505b12008-01-25 21:08:30 +01009188
KAMEZAWA Hiroyuki0a945022006-03-28 01:56:37 -08009189 for_each_possible_cpu(i) {
Ingo Molnar70b97a72006-07-03 00:25:42 -07009190 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009191
9192 rq = cpu_rq(i);
9193 spin_lock_init(&rq->lock);
Nick Piggin78979862005-06-25 14:57:13 -07009194 rq->nr_running = 0;
Thomas Gleixnerdce48a82009-04-11 10:43:41 +02009195 rq->calc_load_active = 0;
9196 rq->calc_load_update = jiffies + LOAD_FREQ;
Ingo Molnardd41f592007-07-09 18:51:59 +02009197 init_cfs_rq(&rq->cfs, rq);
Peter Zijlstrafa85ae22008-01-25 21:08:29 +01009198 init_rt_rq(&rq->rt, rq);
Peter Zijlstra6f505b12008-01-25 21:08:30 +01009199#ifdef CONFIG_FAIR_GROUP_SCHED
9200 init_task_group.shares = init_task_group_load;
9201 INIT_LIST_HEAD(&rq->leaf_cfs_rq_list);
Dhaval Giani354d60c2008-04-19 19:44:59 +02009202#ifdef CONFIG_CGROUP_SCHED
9203 /*
9204 * How much cpu bandwidth does init_task_group get?
9205 *
9206 * In case of task-groups formed thr' the cgroup filesystem, it
9207 * gets 100% of the cpu resources in the system. This overall
9208 * system cpu resource is divided among the tasks of
9209 * init_task_group and its child task-groups in a fair manner,
9210 * based on each entity's (task or task-group's) weight
9211 * (se->load.weight).
9212 *
9213 * In other words, if init_task_group has 10 tasks of weight
9214 * 1024) and two child groups A0 and A1 (of weight 1024 each),
9215 * then A0's share of the cpu resource is:
9216 *
9217 * A0's bandwidth = 1024 / (10*1024 + 1024 + 1024) = 8.33%
9218 *
9219 * We achieve this by letting init_task_group's tasks sit
9220 * directly in rq->cfs (i.e init_task_group->se[] = NULL).
9221 */
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02009222 init_tg_cfs_entry(&init_task_group, &rq->cfs, NULL, i, 1, NULL);
Dhaval Giani354d60c2008-04-19 19:44:59 +02009223#elif defined CONFIG_USER_SCHED
Peter Zijlstraeff766a2008-04-19 19:45:00 +02009224 root_task_group.shares = NICE_0_LOAD;
9225 init_tg_cfs_entry(&root_task_group, &rq->cfs, NULL, i, 0, NULL);
Dhaval Giani354d60c2008-04-19 19:44:59 +02009226 /*
9227 * In case of task-groups formed thr' the user id of tasks,
9228 * init_task_group represents tasks belonging to root user.
9229 * Hence it forms a sibling of all subsequent groups formed.
9230 * In this case, init_task_group gets only a fraction of overall
9231 * system cpu resource, based on the weight assigned to root
9232 * user's cpu share (INIT_TASK_GROUP_LOAD). This is accomplished
9233 * by letting tasks of init_task_group sit in a separate cfs_rq
9234 * (init_cfs_rq) and having one entity represent this group of
9235 * tasks in rq->cfs (i.e init_task_group->se[] != NULL).
9236 */
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02009237 init_tg_cfs_entry(&init_task_group,
Peter Zijlstra6f505b12008-01-25 21:08:30 +01009238 &per_cpu(init_cfs_rq, i),
Peter Zijlstraeff766a2008-04-19 19:45:00 +02009239 &per_cpu(init_sched_entity, i), i, 1,
9240 root_task_group.se[i]);
Peter Zijlstra6f505b12008-01-25 21:08:30 +01009241
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01009242#endif
Dhaval Giani354d60c2008-04-19 19:44:59 +02009243#endif /* CONFIG_FAIR_GROUP_SCHED */
9244
9245 rq->rt.rt_runtime = def_rt_bandwidth.rt_runtime;
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01009246#ifdef CONFIG_RT_GROUP_SCHED
Peter Zijlstra6f505b12008-01-25 21:08:30 +01009247 INIT_LIST_HEAD(&rq->leaf_rt_rq_list);
Dhaval Giani354d60c2008-04-19 19:44:59 +02009248#ifdef CONFIG_CGROUP_SCHED
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02009249 init_tg_rt_entry(&init_task_group, &rq->rt, NULL, i, 1, NULL);
Dhaval Giani354d60c2008-04-19 19:44:59 +02009250#elif defined CONFIG_USER_SCHED
Peter Zijlstraeff766a2008-04-19 19:45:00 +02009251 init_tg_rt_entry(&root_task_group, &rq->rt, NULL, i, 0, NULL);
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02009252 init_tg_rt_entry(&init_task_group,
Peter Zijlstra6f505b12008-01-25 21:08:30 +01009253 &per_cpu(init_rt_rq, i),
Peter Zijlstraeff766a2008-04-19 19:45:00 +02009254 &per_cpu(init_sched_rt_entity, i), i, 1,
9255 root_task_group.rt_se[i]);
Dhaval Giani354d60c2008-04-19 19:44:59 +02009256#endif
Peter Zijlstra6f505b12008-01-25 21:08:30 +01009257#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07009258
Ingo Molnardd41f592007-07-09 18:51:59 +02009259 for (j = 0; j < CPU_LOAD_IDX_MAX; j++)
9260 rq->cpu_load[j] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009261#ifdef CONFIG_SMP
Nick Piggin41c7ce92005-06-25 14:57:24 -07009262 rq->sd = NULL;
Gregory Haskins57d885f2008-01-25 21:08:18 +01009263 rq->rd = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009264 rq->active_balance = 0;
Ingo Molnardd41f592007-07-09 18:51:59 +02009265 rq->next_balance = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009266 rq->push_cpu = 0;
Christoph Lameter0a2966b2006-09-25 23:30:51 -07009267 rq->cpu = i;
Gregory Haskins1f11eb62008-06-04 15:04:05 -04009268 rq->online = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009269 rq->migration_thread = NULL;
9270 INIT_LIST_HEAD(&rq->migration_queue);
Gregory Haskinsdc938522008-01-25 21:08:26 +01009271 rq_attach_root(rq, &def_root_domain);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009272#endif
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01009273 init_rq_hrtick(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009274 atomic_set(&rq->nr_iowait, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009275 }
9276
Peter Williams2dd73a42006-06-27 02:54:34 -07009277 set_load_weight(&init_task);
Heiko Carstensb50f60c2006-07-30 03:03:52 -07009278
Avi Kivitye107be32007-07-26 13:40:43 +02009279#ifdef CONFIG_PREEMPT_NOTIFIERS
9280 INIT_HLIST_HEAD(&init_task.preempt_notifiers);
9281#endif
9282
Christoph Lameterc9819f42006-12-10 02:20:25 -08009283#ifdef CONFIG_SMP
Carlos R. Mafra962cf362008-05-15 11:15:37 -03009284 open_softirq(SCHED_SOFTIRQ, run_rebalance_domains);
Christoph Lameterc9819f42006-12-10 02:20:25 -08009285#endif
9286
Heiko Carstensb50f60c2006-07-30 03:03:52 -07009287#ifdef CONFIG_RT_MUTEXES
9288 plist_head_init(&init_task.pi_waiters, &init_task.pi_lock);
9289#endif
9290
Linus Torvalds1da177e2005-04-16 15:20:36 -07009291 /*
9292 * The boot idle thread does lazy MMU switching as well:
9293 */
9294 atomic_inc(&init_mm.mm_count);
9295 enter_lazy_tlb(&init_mm, current);
9296
9297 /*
9298 * Make us the idle thread. Technically, schedule() should not be
9299 * called from this thread, however somewhere below it might be,
9300 * but because we are the idle thread, we just pick up running again
9301 * when this runqueue becomes "idle".
9302 */
9303 init_idle(current, smp_processor_id());
Thomas Gleixnerdce48a82009-04-11 10:43:41 +02009304
9305 calc_load_update = jiffies + LOAD_FREQ;
9306
Ingo Molnardd41f592007-07-09 18:51:59 +02009307 /*
9308 * During early bootup we pretend to be a normal task:
9309 */
9310 current->sched_class = &fair_sched_class;
Ingo Molnar6892b752008-02-13 14:02:36 +01009311
Rusty Russell6a7b3dc2008-11-25 02:35:04 +10309312 /* Allocate the nohz_cpu_mask if CONFIG_CPUMASK_OFFSTACK */
Pekka Enberg4bdddf82009-06-11 08:35:27 +03009313 alloc_cpumask_var(&nohz_cpu_mask, GFP_NOWAIT);
Rusty Russellbf4d83f2008-11-25 09:57:51 +10309314#ifdef CONFIG_SMP
Rusty Russell7d1e6a92008-11-25 02:35:09 +10309315#ifdef CONFIG_NO_HZ
Pekka Enberg4bdddf82009-06-11 08:35:27 +03009316 alloc_cpumask_var(&nohz.cpu_mask, GFP_NOWAIT);
9317 alloc_cpumask_var(&nohz.ilb_grp_nohz_mask, GFP_NOWAIT);
Rusty Russell7d1e6a92008-11-25 02:35:09 +10309318#endif
Pekka Enberg4bdddf82009-06-11 08:35:27 +03009319 alloc_cpumask_var(&cpu_isolated_map, GFP_NOWAIT);
Rusty Russellbf4d83f2008-11-25 09:57:51 +10309320#endif /* SMP */
Rusty Russell6a7b3dc2008-11-25 02:35:04 +10309321
Ingo Molnar6892b752008-02-13 14:02:36 +01009322 scheduler_running = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009323}
9324
9325#ifdef CONFIG_DEBUG_SPINLOCK_SLEEP
9326void __might_sleep(char *file, int line)
9327{
Ingo Molnar48f24c42006-07-03 00:25:40 -07009328#ifdef in_atomic
Linus Torvalds1da177e2005-04-16 15:20:36 -07009329 static unsigned long prev_jiffy; /* ratelimiting */
9330
Ingo Molnaraef745f2008-08-28 11:34:43 +02009331 if ((!in_atomic() && !irqs_disabled()) ||
9332 system_state != SYSTEM_RUNNING || oops_in_progress)
9333 return;
9334 if (time_before(jiffies, prev_jiffy + HZ) && prev_jiffy)
9335 return;
9336 prev_jiffy = jiffies;
9337
9338 printk(KERN_ERR
9339 "BUG: sleeping function called from invalid context at %s:%d\n",
9340 file, line);
9341 printk(KERN_ERR
9342 "in_atomic(): %d, irqs_disabled(): %d, pid: %d, name: %s\n",
9343 in_atomic(), irqs_disabled(),
9344 current->pid, current->comm);
9345
9346 debug_show_held_locks(current);
9347 if (irqs_disabled())
9348 print_irqtrace_events(current);
9349 dump_stack();
Linus Torvalds1da177e2005-04-16 15:20:36 -07009350#endif
9351}
9352EXPORT_SYMBOL(__might_sleep);
9353#endif
9354
9355#ifdef CONFIG_MAGIC_SYSRQ
Andi Kleen3a5e4dc2007-10-15 17:00:15 +02009356static void normalize_task(struct rq *rq, struct task_struct *p)
9357{
9358 int on_rq;
Peter Zijlstra3e51f332008-05-03 18:29:28 +02009359
Andi Kleen3a5e4dc2007-10-15 17:00:15 +02009360 update_rq_clock(rq);
9361 on_rq = p->se.on_rq;
9362 if (on_rq)
9363 deactivate_task(rq, p, 0);
9364 __setscheduler(rq, p, SCHED_NORMAL, 0);
9365 if (on_rq) {
9366 activate_task(rq, p, 0);
9367 resched_task(rq->curr);
9368 }
9369}
9370
Linus Torvalds1da177e2005-04-16 15:20:36 -07009371void normalize_rt_tasks(void)
9372{
Ingo Molnara0f98a12007-06-17 18:37:45 +02009373 struct task_struct *g, *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009374 unsigned long flags;
Ingo Molnar70b97a72006-07-03 00:25:42 -07009375 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009376
Peter Zijlstra4cf5d772008-02-13 15:45:39 +01009377 read_lock_irqsave(&tasklist_lock, flags);
Ingo Molnara0f98a12007-06-17 18:37:45 +02009378 do_each_thread(g, p) {
Ingo Molnar178be792007-10-15 17:00:18 +02009379 /*
9380 * Only normalize user tasks:
9381 */
9382 if (!p->mm)
9383 continue;
9384
Ingo Molnardd41f592007-07-09 18:51:59 +02009385 p->se.exec_start = 0;
Ingo Molnar6cfb0d52007-08-02 17:41:40 +02009386#ifdef CONFIG_SCHEDSTATS
9387 p->se.wait_start = 0;
9388 p->se.sleep_start = 0;
Ingo Molnardd41f592007-07-09 18:51:59 +02009389 p->se.block_start = 0;
Ingo Molnar6cfb0d52007-08-02 17:41:40 +02009390#endif
Ingo Molnardd41f592007-07-09 18:51:59 +02009391
9392 if (!rt_task(p)) {
9393 /*
9394 * Renice negative nice level userspace
9395 * tasks back to 0:
9396 */
9397 if (TASK_NICE(p) < 0 && p->mm)
9398 set_user_nice(p, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009399 continue;
Ingo Molnardd41f592007-07-09 18:51:59 +02009400 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009401
Peter Zijlstra4cf5d772008-02-13 15:45:39 +01009402 spin_lock(&p->pi_lock);
Ingo Molnarb29739f2006-06-27 02:54:51 -07009403 rq = __task_rq_lock(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009404
Ingo Molnar178be792007-10-15 17:00:18 +02009405 normalize_task(rq, p);
Andi Kleen3a5e4dc2007-10-15 17:00:15 +02009406
Ingo Molnarb29739f2006-06-27 02:54:51 -07009407 __task_rq_unlock(rq);
Peter Zijlstra4cf5d772008-02-13 15:45:39 +01009408 spin_unlock(&p->pi_lock);
Ingo Molnara0f98a12007-06-17 18:37:45 +02009409 } while_each_thread(g, p);
9410
Peter Zijlstra4cf5d772008-02-13 15:45:39 +01009411 read_unlock_irqrestore(&tasklist_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009412}
9413
9414#endif /* CONFIG_MAGIC_SYSRQ */
Linus Torvalds1df5c102005-09-12 07:59:21 -07009415
9416#ifdef CONFIG_IA64
9417/*
9418 * These functions are only useful for the IA64 MCA handling.
9419 *
9420 * They can only be called when the whole system has been
9421 * stopped - every CPU needs to be quiescent, and no scheduling
9422 * activity can take place. Using them for anything else would
9423 * be a serious bug, and as a result, they aren't even visible
9424 * under any other configuration.
9425 */
9426
9427/**
9428 * curr_task - return the current task for a given cpu.
9429 * @cpu: the processor in question.
9430 *
9431 * ONLY VALID WHEN THE WHOLE SYSTEM IS STOPPED!
9432 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07009433struct task_struct *curr_task(int cpu)
Linus Torvalds1df5c102005-09-12 07:59:21 -07009434{
9435 return cpu_curr(cpu);
9436}
9437
9438/**
9439 * set_curr_task - set the current task for a given cpu.
9440 * @cpu: the processor in question.
9441 * @p: the task pointer to set.
9442 *
9443 * Description: This function must only be used when non-maskable interrupts
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01009444 * are serviced on a separate stack. It allows the architecture to switch the
9445 * notion of the current task on a cpu in a non-blocking manner. This function
Linus Torvalds1df5c102005-09-12 07:59:21 -07009446 * must be called with all CPU's synchronized, and interrupts disabled, the
9447 * and caller must save the original value of the current task (see
9448 * curr_task() above) and restore that value before reenabling interrupts and
9449 * re-starting the system.
9450 *
9451 * ONLY VALID WHEN THE WHOLE SYSTEM IS STOPPED!
9452 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07009453void set_curr_task(int cpu, struct task_struct *p)
Linus Torvalds1df5c102005-09-12 07:59:21 -07009454{
9455 cpu_curr(cpu) = p;
9456}
9457
9458#endif
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02009459
Peter Zijlstrabccbe082008-02-13 15:45:40 +01009460#ifdef CONFIG_FAIR_GROUP_SCHED
9461static void free_fair_sched_group(struct task_group *tg)
Peter Zijlstra6f505b12008-01-25 21:08:30 +01009462{
9463 int i;
9464
9465 for_each_possible_cpu(i) {
9466 if (tg->cfs_rq)
9467 kfree(tg->cfs_rq[i]);
9468 if (tg->se)
9469 kfree(tg->se[i]);
Peter Zijlstra6f505b12008-01-25 21:08:30 +01009470 }
9471
9472 kfree(tg->cfs_rq);
9473 kfree(tg->se);
Peter Zijlstra6f505b12008-01-25 21:08:30 +01009474}
9475
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02009476static
9477int alloc_fair_sched_group(struct task_group *tg, struct task_group *parent)
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02009478{
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02009479 struct cfs_rq *cfs_rq;
Li Zefaneab17222008-10-29 17:03:22 +08009480 struct sched_entity *se;
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02009481 struct rq *rq;
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02009482 int i;
9483
Mike Travis434d53b2008-04-04 18:11:04 -07009484 tg->cfs_rq = kzalloc(sizeof(cfs_rq) * nr_cpu_ids, GFP_KERNEL);
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02009485 if (!tg->cfs_rq)
9486 goto err;
Mike Travis434d53b2008-04-04 18:11:04 -07009487 tg->se = kzalloc(sizeof(se) * nr_cpu_ids, GFP_KERNEL);
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02009488 if (!tg->se)
9489 goto err;
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01009490
9491 tg->shares = NICE_0_LOAD;
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02009492
9493 for_each_possible_cpu(i) {
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02009494 rq = cpu_rq(i);
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02009495
Li Zefaneab17222008-10-29 17:03:22 +08009496 cfs_rq = kzalloc_node(sizeof(struct cfs_rq),
9497 GFP_KERNEL, cpu_to_node(i));
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02009498 if (!cfs_rq)
9499 goto err;
9500
Li Zefaneab17222008-10-29 17:03:22 +08009501 se = kzalloc_node(sizeof(struct sched_entity),
9502 GFP_KERNEL, cpu_to_node(i));
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02009503 if (!se)
9504 goto err;
9505
Li Zefaneab17222008-10-29 17:03:22 +08009506 init_tg_cfs_entry(tg, cfs_rq, se, i, 0, parent->se[i]);
Peter Zijlstrabccbe082008-02-13 15:45:40 +01009507 }
9508
9509 return 1;
9510
9511 err:
9512 return 0;
9513}
9514
9515static inline void register_fair_sched_group(struct task_group *tg, int cpu)
9516{
9517 list_add_rcu(&tg->cfs_rq[cpu]->leaf_cfs_rq_list,
9518 &cpu_rq(cpu)->leaf_cfs_rq_list);
9519}
9520
9521static inline void unregister_fair_sched_group(struct task_group *tg, int cpu)
9522{
9523 list_del_rcu(&tg->cfs_rq[cpu]->leaf_cfs_rq_list);
9524}
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +02009525#else /* !CONFG_FAIR_GROUP_SCHED */
Peter Zijlstrabccbe082008-02-13 15:45:40 +01009526static inline void free_fair_sched_group(struct task_group *tg)
9527{
9528}
9529
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02009530static inline
9531int alloc_fair_sched_group(struct task_group *tg, struct task_group *parent)
Peter Zijlstrabccbe082008-02-13 15:45:40 +01009532{
9533 return 1;
9534}
9535
9536static inline void register_fair_sched_group(struct task_group *tg, int cpu)
9537{
9538}
9539
9540static inline void unregister_fair_sched_group(struct task_group *tg, int cpu)
9541{
9542}
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +02009543#endif /* CONFIG_FAIR_GROUP_SCHED */
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01009544
9545#ifdef CONFIG_RT_GROUP_SCHED
Peter Zijlstrabccbe082008-02-13 15:45:40 +01009546static void free_rt_sched_group(struct task_group *tg)
9547{
9548 int i;
9549
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02009550 destroy_rt_bandwidth(&tg->rt_bandwidth);
9551
Peter Zijlstrabccbe082008-02-13 15:45:40 +01009552 for_each_possible_cpu(i) {
9553 if (tg->rt_rq)
9554 kfree(tg->rt_rq[i]);
9555 if (tg->rt_se)
9556 kfree(tg->rt_se[i]);
9557 }
9558
9559 kfree(tg->rt_rq);
9560 kfree(tg->rt_se);
9561}
9562
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02009563static
9564int alloc_rt_sched_group(struct task_group *tg, struct task_group *parent)
Peter Zijlstrabccbe082008-02-13 15:45:40 +01009565{
9566 struct rt_rq *rt_rq;
Li Zefaneab17222008-10-29 17:03:22 +08009567 struct sched_rt_entity *rt_se;
Peter Zijlstrabccbe082008-02-13 15:45:40 +01009568 struct rq *rq;
9569 int i;
9570
Mike Travis434d53b2008-04-04 18:11:04 -07009571 tg->rt_rq = kzalloc(sizeof(rt_rq) * nr_cpu_ids, GFP_KERNEL);
Peter Zijlstrabccbe082008-02-13 15:45:40 +01009572 if (!tg->rt_rq)
9573 goto err;
Mike Travis434d53b2008-04-04 18:11:04 -07009574 tg->rt_se = kzalloc(sizeof(rt_se) * nr_cpu_ids, GFP_KERNEL);
Peter Zijlstrabccbe082008-02-13 15:45:40 +01009575 if (!tg->rt_se)
9576 goto err;
9577
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02009578 init_rt_bandwidth(&tg->rt_bandwidth,
9579 ktime_to_ns(def_rt_bandwidth.rt_period), 0);
Peter Zijlstrabccbe082008-02-13 15:45:40 +01009580
9581 for_each_possible_cpu(i) {
9582 rq = cpu_rq(i);
9583
Li Zefaneab17222008-10-29 17:03:22 +08009584 rt_rq = kzalloc_node(sizeof(struct rt_rq),
9585 GFP_KERNEL, cpu_to_node(i));
Peter Zijlstra6f505b12008-01-25 21:08:30 +01009586 if (!rt_rq)
9587 goto err;
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02009588
Li Zefaneab17222008-10-29 17:03:22 +08009589 rt_se = kzalloc_node(sizeof(struct sched_rt_entity),
9590 GFP_KERNEL, cpu_to_node(i));
Peter Zijlstra6f505b12008-01-25 21:08:30 +01009591 if (!rt_se)
9592 goto err;
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02009593
Li Zefaneab17222008-10-29 17:03:22 +08009594 init_tg_rt_entry(tg, rt_rq, rt_se, i, 0, parent->rt_se[i]);
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02009595 }
9596
Peter Zijlstrabccbe082008-02-13 15:45:40 +01009597 return 1;
9598
9599 err:
9600 return 0;
9601}
9602
9603static inline void register_rt_sched_group(struct task_group *tg, int cpu)
9604{
9605 list_add_rcu(&tg->rt_rq[cpu]->leaf_rt_rq_list,
9606 &cpu_rq(cpu)->leaf_rt_rq_list);
9607}
9608
9609static inline void unregister_rt_sched_group(struct task_group *tg, int cpu)
9610{
9611 list_del_rcu(&tg->rt_rq[cpu]->leaf_rt_rq_list);
9612}
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +02009613#else /* !CONFIG_RT_GROUP_SCHED */
Peter Zijlstrabccbe082008-02-13 15:45:40 +01009614static inline void free_rt_sched_group(struct task_group *tg)
9615{
9616}
9617
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02009618static inline
9619int alloc_rt_sched_group(struct task_group *tg, struct task_group *parent)
Peter Zijlstrabccbe082008-02-13 15:45:40 +01009620{
9621 return 1;
9622}
9623
9624static inline void register_rt_sched_group(struct task_group *tg, int cpu)
9625{
9626}
9627
9628static inline void unregister_rt_sched_group(struct task_group *tg, int cpu)
9629{
9630}
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +02009631#endif /* CONFIG_RT_GROUP_SCHED */
Peter Zijlstrabccbe082008-02-13 15:45:40 +01009632
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02009633#ifdef CONFIG_GROUP_SCHED
Peter Zijlstrabccbe082008-02-13 15:45:40 +01009634static void free_sched_group(struct task_group *tg)
9635{
9636 free_fair_sched_group(tg);
9637 free_rt_sched_group(tg);
9638 kfree(tg);
9639}
9640
9641/* allocate runqueue etc for a new task group */
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02009642struct task_group *sched_create_group(struct task_group *parent)
Peter Zijlstrabccbe082008-02-13 15:45:40 +01009643{
9644 struct task_group *tg;
9645 unsigned long flags;
9646 int i;
9647
9648 tg = kzalloc(sizeof(*tg), GFP_KERNEL);
9649 if (!tg)
9650 return ERR_PTR(-ENOMEM);
9651
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02009652 if (!alloc_fair_sched_group(tg, parent))
Peter Zijlstrabccbe082008-02-13 15:45:40 +01009653 goto err;
9654
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02009655 if (!alloc_rt_sched_group(tg, parent))
Peter Zijlstrabccbe082008-02-13 15:45:40 +01009656 goto err;
9657
Peter Zijlstra8ed36992008-02-13 15:45:39 +01009658 spin_lock_irqsave(&task_group_lock, flags);
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02009659 for_each_possible_cpu(i) {
Peter Zijlstrabccbe082008-02-13 15:45:40 +01009660 register_fair_sched_group(tg, i);
9661 register_rt_sched_group(tg, i);
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02009662 }
Peter Zijlstra6f505b12008-01-25 21:08:30 +01009663 list_add_rcu(&tg->list, &task_groups);
Peter Zijlstraf473aa52008-04-19 19:45:00 +02009664
9665 WARN_ON(!parent); /* root should already exist */
9666
9667 tg->parent = parent;
Peter Zijlstraf473aa52008-04-19 19:45:00 +02009668 INIT_LIST_HEAD(&tg->children);
Zhang, Yanmin09f27242030-08-14 15:56:40 +08009669 list_add_rcu(&tg->siblings, &parent->children);
Peter Zijlstra8ed36992008-02-13 15:45:39 +01009670 spin_unlock_irqrestore(&task_group_lock, flags);
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02009671
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02009672 return tg;
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02009673
9674err:
Peter Zijlstra6f505b12008-01-25 21:08:30 +01009675 free_sched_group(tg);
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02009676 return ERR_PTR(-ENOMEM);
9677}
9678
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02009679/* rcu callback to free various structures associated with a task group */
Peter Zijlstra6f505b12008-01-25 21:08:30 +01009680static void free_sched_group_rcu(struct rcu_head *rhp)
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02009681{
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02009682 /* now it should be safe to free those cfs_rqs */
Peter Zijlstra6f505b12008-01-25 21:08:30 +01009683 free_sched_group(container_of(rhp, struct task_group, rcu));
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02009684}
9685
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02009686/* Destroy runqueue etc associated with a task group */
Ingo Molnar4cf86d72007-10-15 17:00:14 +02009687void sched_destroy_group(struct task_group *tg)
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02009688{
Peter Zijlstra8ed36992008-02-13 15:45:39 +01009689 unsigned long flags;
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02009690 int i;
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02009691
Peter Zijlstra8ed36992008-02-13 15:45:39 +01009692 spin_lock_irqsave(&task_group_lock, flags);
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02009693 for_each_possible_cpu(i) {
Peter Zijlstrabccbe082008-02-13 15:45:40 +01009694 unregister_fair_sched_group(tg, i);
9695 unregister_rt_sched_group(tg, i);
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02009696 }
Peter Zijlstra6f505b12008-01-25 21:08:30 +01009697 list_del_rcu(&tg->list);
Peter Zijlstraf473aa52008-04-19 19:45:00 +02009698 list_del_rcu(&tg->siblings);
Peter Zijlstra8ed36992008-02-13 15:45:39 +01009699 spin_unlock_irqrestore(&task_group_lock, flags);
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02009700
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02009701 /* wait for possible concurrent references to cfs_rqs complete */
Peter Zijlstra6f505b12008-01-25 21:08:30 +01009702 call_rcu(&tg->rcu, free_sched_group_rcu);
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02009703}
9704
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02009705/* change task's runqueue when it moves between groups.
Ingo Molnar3a252012007-10-15 17:00:12 +02009706 * The caller of this function should have put the task in its new group
9707 * by now. This function just updates tsk->se.cfs_rq and tsk->se.parent to
9708 * reflect its new group.
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02009709 */
9710void sched_move_task(struct task_struct *tsk)
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02009711{
9712 int on_rq, running;
9713 unsigned long flags;
9714 struct rq *rq;
9715
9716 rq = task_rq_lock(tsk, &flags);
9717
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02009718 update_rq_clock(rq);
9719
Dmitry Adamushko051a1d12007-12-18 15:21:13 +01009720 running = task_current(rq, tsk);
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02009721 on_rq = tsk->se.on_rq;
9722
Hiroshi Shimamoto0e1f3482008-03-10 11:01:20 -07009723 if (on_rq)
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02009724 dequeue_task(rq, tsk, 0);
Hiroshi Shimamoto0e1f3482008-03-10 11:01:20 -07009725 if (unlikely(running))
9726 tsk->sched_class->put_prev_task(rq, tsk);
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02009727
Peter Zijlstra6f505b12008-01-25 21:08:30 +01009728 set_task_rq(tsk, task_cpu(tsk));
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02009729
Peter Zijlstra810b3812008-02-29 15:21:01 -05009730#ifdef CONFIG_FAIR_GROUP_SCHED
9731 if (tsk->sched_class->moved_group)
9732 tsk->sched_class->moved_group(tsk);
9733#endif
9734
Hiroshi Shimamoto0e1f3482008-03-10 11:01:20 -07009735 if (unlikely(running))
9736 tsk->sched_class->set_curr_task(rq);
9737 if (on_rq)
Dmitry Adamushko7074bad2007-10-15 17:00:07 +02009738 enqueue_task(rq, tsk, 0);
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02009739
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02009740 task_rq_unlock(rq, &flags);
9741}
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +02009742#endif /* CONFIG_GROUP_SCHED */
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02009743
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01009744#ifdef CONFIG_FAIR_GROUP_SCHED
Peter Zijlstrac09595f2008-06-27 13:41:14 +02009745static void __set_se_shares(struct sched_entity *se, unsigned long shares)
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02009746{
9747 struct cfs_rq *cfs_rq = se->cfs_rq;
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02009748 int on_rq;
9749
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02009750 on_rq = se->on_rq;
Peter Zijlstra62fb1852008-02-25 17:34:02 +01009751 if (on_rq)
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02009752 dequeue_entity(cfs_rq, se, 0);
9753
9754 se->load.weight = shares;
Peter Zijlstrae05510d2008-05-05 23:56:17 +02009755 se->load.inv_weight = 0;
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02009756
Peter Zijlstra62fb1852008-02-25 17:34:02 +01009757 if (on_rq)
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02009758 enqueue_entity(cfs_rq, se, 0);
Peter Zijlstrac09595f2008-06-27 13:41:14 +02009759}
Peter Zijlstra62fb1852008-02-25 17:34:02 +01009760
Peter Zijlstrac09595f2008-06-27 13:41:14 +02009761static void set_se_shares(struct sched_entity *se, unsigned long shares)
9762{
9763 struct cfs_rq *cfs_rq = se->cfs_rq;
9764 struct rq *rq = cfs_rq->rq;
9765 unsigned long flags;
9766
9767 spin_lock_irqsave(&rq->lock, flags);
9768 __set_se_shares(se, shares);
9769 spin_unlock_irqrestore(&rq->lock, flags);
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02009770}
9771
Peter Zijlstra8ed36992008-02-13 15:45:39 +01009772static DEFINE_MUTEX(shares_mutex);
9773
Ingo Molnar4cf86d72007-10-15 17:00:14 +02009774int sched_group_set_shares(struct task_group *tg, unsigned long shares)
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02009775{
9776 int i;
Peter Zijlstra8ed36992008-02-13 15:45:39 +01009777 unsigned long flags;
Ingo Molnarc61935f2008-01-22 11:24:58 +01009778
Peter Zijlstra62fb1852008-02-25 17:34:02 +01009779 /*
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02009780 * We can't change the weight of the root cgroup.
9781 */
9782 if (!tg->se[0])
9783 return -EINVAL;
9784
Peter Zijlstra18d95a22008-04-19 19:45:00 +02009785 if (shares < MIN_SHARES)
9786 shares = MIN_SHARES;
Miao Xiecb4ad1f2008-04-28 12:54:56 +08009787 else if (shares > MAX_SHARES)
9788 shares = MAX_SHARES;
Peter Zijlstra62fb1852008-02-25 17:34:02 +01009789
Peter Zijlstra8ed36992008-02-13 15:45:39 +01009790 mutex_lock(&shares_mutex);
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02009791 if (tg->shares == shares)
Dhaval Giani5cb350b2007-10-15 17:00:14 +02009792 goto done;
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02009793
Peter Zijlstra8ed36992008-02-13 15:45:39 +01009794 spin_lock_irqsave(&task_group_lock, flags);
Peter Zijlstrabccbe082008-02-13 15:45:40 +01009795 for_each_possible_cpu(i)
9796 unregister_fair_sched_group(tg, i);
Peter Zijlstraf473aa52008-04-19 19:45:00 +02009797 list_del_rcu(&tg->siblings);
Peter Zijlstra8ed36992008-02-13 15:45:39 +01009798 spin_unlock_irqrestore(&task_group_lock, flags);
Srivatsa Vaddagiri6b2d7702008-01-25 21:08:00 +01009799
9800 /* wait for any ongoing reference to this group to finish */
9801 synchronize_sched();
9802
9803 /*
9804 * Now we are free to modify the group's share on each cpu
9805 * w/o tripping rebalance_share or load_balance_fair.
9806 */
9807 tg->shares = shares;
Peter Zijlstrac09595f2008-06-27 13:41:14 +02009808 for_each_possible_cpu(i) {
9809 /*
9810 * force a rebalance
9811 */
9812 cfs_rq_set_shares(tg->cfs_rq[i], 0);
Miao Xiecb4ad1f2008-04-28 12:54:56 +08009813 set_se_shares(tg->se[i], shares);
Peter Zijlstrac09595f2008-06-27 13:41:14 +02009814 }
Srivatsa Vaddagiri6b2d7702008-01-25 21:08:00 +01009815
9816 /*
9817 * Enable load balance activity on this group, by inserting it back on
9818 * each cpu's rq->leaf_cfs_rq_list.
9819 */
Peter Zijlstra8ed36992008-02-13 15:45:39 +01009820 spin_lock_irqsave(&task_group_lock, flags);
Peter Zijlstrabccbe082008-02-13 15:45:40 +01009821 for_each_possible_cpu(i)
9822 register_fair_sched_group(tg, i);
Peter Zijlstraf473aa52008-04-19 19:45:00 +02009823 list_add_rcu(&tg->siblings, &tg->parent->children);
Peter Zijlstra8ed36992008-02-13 15:45:39 +01009824 spin_unlock_irqrestore(&task_group_lock, flags);
Dhaval Giani5cb350b2007-10-15 17:00:14 +02009825done:
Peter Zijlstra8ed36992008-02-13 15:45:39 +01009826 mutex_unlock(&shares_mutex);
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02009827 return 0;
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02009828}
9829
Dhaval Giani5cb350b2007-10-15 17:00:14 +02009830unsigned long sched_group_shares(struct task_group *tg)
9831{
9832 return tg->shares;
9833}
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01009834#endif
Dhaval Giani5cb350b2007-10-15 17:00:14 +02009835
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01009836#ifdef CONFIG_RT_GROUP_SCHED
Peter Zijlstra6f505b12008-01-25 21:08:30 +01009837/*
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01009838 * Ensure that the real time constraints are schedulable.
Peter Zijlstra6f505b12008-01-25 21:08:30 +01009839 */
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01009840static DEFINE_MUTEX(rt_constraints_mutex);
9841
9842static unsigned long to_ratio(u64 period, u64 runtime)
9843{
9844 if (runtime == RUNTIME_INF)
Peter Zijlstra9a7e0b12008-08-19 12:33:06 +02009845 return 1ULL << 20;
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01009846
Peter Zijlstra9a7e0b12008-08-19 12:33:06 +02009847 return div64_u64(runtime << 20, period);
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01009848}
9849
Dhaval Giani521f1a242008-02-28 15:21:56 +05309850/* Must be called with tasklist_lock held */
9851static inline int tg_has_rt_tasks(struct task_group *tg)
9852{
9853 struct task_struct *g, *p;
Peter Zijlstra9a7e0b12008-08-19 12:33:06 +02009854
Dhaval Giani521f1a242008-02-28 15:21:56 +05309855 do_each_thread(g, p) {
9856 if (rt_task(p) && rt_rq_of_se(&p->rt)->tg == tg)
9857 return 1;
9858 } while_each_thread(g, p);
Peter Zijlstra9a7e0b12008-08-19 12:33:06 +02009859
Dhaval Giani521f1a242008-02-28 15:21:56 +05309860 return 0;
9861}
9862
Peter Zijlstra9a7e0b12008-08-19 12:33:06 +02009863struct rt_schedulable_data {
9864 struct task_group *tg;
9865 u64 rt_period;
9866 u64 rt_runtime;
9867};
9868
9869static int tg_schedulable(struct task_group *tg, void *data)
9870{
9871 struct rt_schedulable_data *d = data;
9872 struct task_group *child;
9873 unsigned long total, sum = 0;
9874 u64 period, runtime;
9875
9876 period = ktime_to_ns(tg->rt_bandwidth.rt_period);
9877 runtime = tg->rt_bandwidth.rt_runtime;
9878
9879 if (tg == d->tg) {
9880 period = d->rt_period;
9881 runtime = d->rt_runtime;
9882 }
9883
Peter Zijlstra98a48262009-01-14 10:56:32 +01009884#ifdef CONFIG_USER_SCHED
9885 if (tg == &root_task_group) {
9886 period = global_rt_period();
9887 runtime = global_rt_runtime();
9888 }
9889#endif
9890
Peter Zijlstra4653f802008-09-23 15:33:44 +02009891 /*
9892 * Cannot have more runtime than the period.
9893 */
9894 if (runtime > period && runtime != RUNTIME_INF)
9895 return -EINVAL;
9896
9897 /*
9898 * Ensure we don't starve existing RT tasks.
9899 */
Peter Zijlstra9a7e0b12008-08-19 12:33:06 +02009900 if (rt_bandwidth_enabled() && !runtime && tg_has_rt_tasks(tg))
9901 return -EBUSY;
9902
9903 total = to_ratio(period, runtime);
9904
Peter Zijlstra4653f802008-09-23 15:33:44 +02009905 /*
9906 * Nobody can have more than the global setting allows.
9907 */
9908 if (total > to_ratio(global_rt_period(), global_rt_runtime()))
9909 return -EINVAL;
9910
9911 /*
9912 * The sum of our children's runtime should not exceed our own.
9913 */
Peter Zijlstra9a7e0b12008-08-19 12:33:06 +02009914 list_for_each_entry_rcu(child, &tg->children, siblings) {
9915 period = ktime_to_ns(child->rt_bandwidth.rt_period);
9916 runtime = child->rt_bandwidth.rt_runtime;
9917
9918 if (child == d->tg) {
9919 period = d->rt_period;
9920 runtime = d->rt_runtime;
9921 }
9922
9923 sum += to_ratio(period, runtime);
9924 }
9925
9926 if (sum > total)
9927 return -EINVAL;
9928
9929 return 0;
9930}
9931
9932static int __rt_schedulable(struct task_group *tg, u64 period, u64 runtime)
9933{
9934 struct rt_schedulable_data data = {
9935 .tg = tg,
9936 .rt_period = period,
9937 .rt_runtime = runtime,
9938 };
9939
9940 return walk_tg_tree(tg_schedulable, tg_nop, &data);
9941}
9942
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02009943static int tg_set_bandwidth(struct task_group *tg,
9944 u64 rt_period, u64 rt_runtime)
Peter Zijlstra6f505b12008-01-25 21:08:30 +01009945{
Peter Zijlstraac086bc2008-04-19 19:44:58 +02009946 int i, err = 0;
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01009947
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01009948 mutex_lock(&rt_constraints_mutex);
Dhaval Giani521f1a242008-02-28 15:21:56 +05309949 read_lock(&tasklist_lock);
Peter Zijlstra9a7e0b12008-08-19 12:33:06 +02009950 err = __rt_schedulable(tg, rt_period, rt_runtime);
9951 if (err)
Dhaval Giani521f1a242008-02-28 15:21:56 +05309952 goto unlock;
Peter Zijlstraac086bc2008-04-19 19:44:58 +02009953
9954 spin_lock_irq(&tg->rt_bandwidth.rt_runtime_lock);
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02009955 tg->rt_bandwidth.rt_period = ns_to_ktime(rt_period);
9956 tg->rt_bandwidth.rt_runtime = rt_runtime;
Peter Zijlstraac086bc2008-04-19 19:44:58 +02009957
9958 for_each_possible_cpu(i) {
9959 struct rt_rq *rt_rq = tg->rt_rq[i];
9960
9961 spin_lock(&rt_rq->rt_runtime_lock);
9962 rt_rq->rt_runtime = rt_runtime;
9963 spin_unlock(&rt_rq->rt_runtime_lock);
9964 }
9965 spin_unlock_irq(&tg->rt_bandwidth.rt_runtime_lock);
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01009966 unlock:
Dhaval Giani521f1a242008-02-28 15:21:56 +05309967 read_unlock(&tasklist_lock);
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01009968 mutex_unlock(&rt_constraints_mutex);
9969
9970 return err;
Peter Zijlstra6f505b12008-01-25 21:08:30 +01009971}
9972
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02009973int sched_group_set_rt_runtime(struct task_group *tg, long rt_runtime_us)
9974{
9975 u64 rt_runtime, rt_period;
9976
9977 rt_period = ktime_to_ns(tg->rt_bandwidth.rt_period);
9978 rt_runtime = (u64)rt_runtime_us * NSEC_PER_USEC;
9979 if (rt_runtime_us < 0)
9980 rt_runtime = RUNTIME_INF;
9981
9982 return tg_set_bandwidth(tg, rt_period, rt_runtime);
9983}
9984
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01009985long sched_group_rt_runtime(struct task_group *tg)
9986{
9987 u64 rt_runtime_us;
9988
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02009989 if (tg->rt_bandwidth.rt_runtime == RUNTIME_INF)
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01009990 return -1;
9991
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02009992 rt_runtime_us = tg->rt_bandwidth.rt_runtime;
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01009993 do_div(rt_runtime_us, NSEC_PER_USEC);
9994 return rt_runtime_us;
9995}
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02009996
9997int sched_group_set_rt_period(struct task_group *tg, long rt_period_us)
9998{
9999 u64 rt_runtime, rt_period;
10000
10001 rt_period = (u64)rt_period_us * NSEC_PER_USEC;
10002 rt_runtime = tg->rt_bandwidth.rt_runtime;
10003
Raistlin619b0482008-06-26 18:54:09 +020010004 if (rt_period == 0)
10005 return -EINVAL;
10006
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +020010007 return tg_set_bandwidth(tg, rt_period, rt_runtime);
10008}
10009
10010long sched_group_rt_period(struct task_group *tg)
10011{
10012 u64 rt_period_us;
10013
10014 rt_period_us = ktime_to_ns(tg->rt_bandwidth.rt_period);
10015 do_div(rt_period_us, NSEC_PER_USEC);
10016 return rt_period_us;
10017}
10018
10019static int sched_rt_global_constraints(void)
10020{
Peter Zijlstra4653f802008-09-23 15:33:44 +020010021 u64 runtime, period;
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +020010022 int ret = 0;
10023
Hiroshi Shimamotoec5d4982008-09-10 17:00:19 -070010024 if (sysctl_sched_rt_period <= 0)
10025 return -EINVAL;
10026
Peter Zijlstra4653f802008-09-23 15:33:44 +020010027 runtime = global_rt_runtime();
10028 period = global_rt_period();
10029
10030 /*
10031 * Sanity check on the sysctl variables.
10032 */
10033 if (runtime > period && runtime != RUNTIME_INF)
10034 return -EINVAL;
Peter Zijlstra10b612f2008-06-19 14:22:27 +020010035
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +020010036 mutex_lock(&rt_constraints_mutex);
Peter Zijlstra9a7e0b12008-08-19 12:33:06 +020010037 read_lock(&tasklist_lock);
Peter Zijlstra4653f802008-09-23 15:33:44 +020010038 ret = __rt_schedulable(NULL, 0, 0);
Peter Zijlstra9a7e0b12008-08-19 12:33:06 +020010039 read_unlock(&tasklist_lock);
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +020010040 mutex_unlock(&rt_constraints_mutex);
10041
10042 return ret;
10043}
Dhaval Giani54e99122009-02-27 15:13:54 +053010044
10045int sched_rt_can_attach(struct task_group *tg, struct task_struct *tsk)
10046{
10047 /* Don't accept realtime tasks when there is no way for them to run */
10048 if (rt_task(tsk) && tg->rt_bandwidth.rt_runtime == 0)
10049 return 0;
10050
10051 return 1;
10052}
10053
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +020010054#else /* !CONFIG_RT_GROUP_SCHED */
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +020010055static int sched_rt_global_constraints(void)
10056{
Peter Zijlstraac086bc2008-04-19 19:44:58 +020010057 unsigned long flags;
10058 int i;
10059
Hiroshi Shimamotoec5d4982008-09-10 17:00:19 -070010060 if (sysctl_sched_rt_period <= 0)
10061 return -EINVAL;
10062
Peter Zijlstra60aa6052009-05-05 17:50:21 +020010063 /*
10064 * There's always some RT tasks in the root group
10065 * -- migration, kstopmachine etc..
10066 */
10067 if (sysctl_sched_rt_runtime == 0)
10068 return -EBUSY;
10069
Peter Zijlstraac086bc2008-04-19 19:44:58 +020010070 spin_lock_irqsave(&def_rt_bandwidth.rt_runtime_lock, flags);
10071 for_each_possible_cpu(i) {
10072 struct rt_rq *rt_rq = &cpu_rq(i)->rt;
10073
10074 spin_lock(&rt_rq->rt_runtime_lock);
10075 rt_rq->rt_runtime = global_rt_runtime();
10076 spin_unlock(&rt_rq->rt_runtime_lock);
10077 }
10078 spin_unlock_irqrestore(&def_rt_bandwidth.rt_runtime_lock, flags);
10079
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +020010080 return 0;
10081}
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +020010082#endif /* CONFIG_RT_GROUP_SCHED */
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +020010083
10084int sched_rt_handler(struct ctl_table *table, int write,
10085 struct file *filp, void __user *buffer, size_t *lenp,
10086 loff_t *ppos)
10087{
10088 int ret;
10089 int old_period, old_runtime;
10090 static DEFINE_MUTEX(mutex);
10091
10092 mutex_lock(&mutex);
10093 old_period = sysctl_sched_rt_period;
10094 old_runtime = sysctl_sched_rt_runtime;
10095
10096 ret = proc_dointvec(table, write, filp, buffer, lenp, ppos);
10097
10098 if (!ret && write) {
10099 ret = sched_rt_global_constraints();
10100 if (ret) {
10101 sysctl_sched_rt_period = old_period;
10102 sysctl_sched_rt_runtime = old_runtime;
10103 } else {
10104 def_rt_bandwidth.rt_runtime = global_rt_runtime();
10105 def_rt_bandwidth.rt_period =
10106 ns_to_ktime(global_rt_period());
10107 }
10108 }
10109 mutex_unlock(&mutex);
10110
10111 return ret;
10112}
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -070010113
Peter Zijlstra052f1dc2008-02-13 15:45:40 +010010114#ifdef CONFIG_CGROUP_SCHED
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -070010115
10116/* return corresponding task_group object of a cgroup */
Paul Menage2b01dfe2007-10-24 18:23:50 +020010117static inline struct task_group *cgroup_tg(struct cgroup *cgrp)
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -070010118{
Paul Menage2b01dfe2007-10-24 18:23:50 +020010119 return container_of(cgroup_subsys_state(cgrp, cpu_cgroup_subsys_id),
10120 struct task_group, css);
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -070010121}
10122
10123static struct cgroup_subsys_state *
Paul Menage2b01dfe2007-10-24 18:23:50 +020010124cpu_cgroup_create(struct cgroup_subsys *ss, struct cgroup *cgrp)
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -070010125{
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +020010126 struct task_group *tg, *parent;
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -070010127
Paul Menage2b01dfe2007-10-24 18:23:50 +020010128 if (!cgrp->parent) {
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -070010129 /* This is early initialization for the top cgroup */
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -070010130 return &init_task_group.css;
10131 }
10132
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +020010133 parent = cgroup_tg(cgrp->parent);
10134 tg = sched_create_group(parent);
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -070010135 if (IS_ERR(tg))
10136 return ERR_PTR(-ENOMEM);
10137
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -070010138 return &tg->css;
10139}
10140
Ingo Molnar41a2d6c2007-12-05 15:46:09 +010010141static void
10142cpu_cgroup_destroy(struct cgroup_subsys *ss, struct cgroup *cgrp)
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -070010143{
Paul Menage2b01dfe2007-10-24 18:23:50 +020010144 struct task_group *tg = cgroup_tg(cgrp);
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -070010145
10146 sched_destroy_group(tg);
10147}
10148
Ingo Molnar41a2d6c2007-12-05 15:46:09 +010010149static int
10150cpu_cgroup_can_attach(struct cgroup_subsys *ss, struct cgroup *cgrp,
10151 struct task_struct *tsk)
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -070010152{
Peter Zijlstrab68aa232008-02-13 15:45:40 +010010153#ifdef CONFIG_RT_GROUP_SCHED
Dhaval Giani54e99122009-02-27 15:13:54 +053010154 if (!sched_rt_can_attach(cgroup_tg(cgrp), tsk))
Peter Zijlstrab68aa232008-02-13 15:45:40 +010010155 return -EINVAL;
10156#else
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -070010157 /* We don't support RT-tasks being in separate groups */
10158 if (tsk->sched_class != &fair_sched_class)
10159 return -EINVAL;
Peter Zijlstrab68aa232008-02-13 15:45:40 +010010160#endif
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -070010161
10162 return 0;
10163}
10164
10165static void
Paul Menage2b01dfe2007-10-24 18:23:50 +020010166cpu_cgroup_attach(struct cgroup_subsys *ss, struct cgroup *cgrp,
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -070010167 struct cgroup *old_cont, struct task_struct *tsk)
10168{
10169 sched_move_task(tsk);
10170}
10171
Peter Zijlstra052f1dc2008-02-13 15:45:40 +010010172#ifdef CONFIG_FAIR_GROUP_SCHED
Paul Menagef4c753b2008-04-29 00:59:56 -070010173static int cpu_shares_write_u64(struct cgroup *cgrp, struct cftype *cftype,
Paul Menage2b01dfe2007-10-24 18:23:50 +020010174 u64 shareval)
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -070010175{
Paul Menage2b01dfe2007-10-24 18:23:50 +020010176 return sched_group_set_shares(cgroup_tg(cgrp), shareval);
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -070010177}
10178
Paul Menagef4c753b2008-04-29 00:59:56 -070010179static u64 cpu_shares_read_u64(struct cgroup *cgrp, struct cftype *cft)
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -070010180{
Paul Menage2b01dfe2007-10-24 18:23:50 +020010181 struct task_group *tg = cgroup_tg(cgrp);
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -070010182
10183 return (u64) tg->shares;
10184}
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +020010185#endif /* CONFIG_FAIR_GROUP_SCHED */
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -070010186
Peter Zijlstra052f1dc2008-02-13 15:45:40 +010010187#ifdef CONFIG_RT_GROUP_SCHED
Mirco Tischler0c708142008-05-14 16:05:46 -070010188static int cpu_rt_runtime_write(struct cgroup *cgrp, struct cftype *cft,
Paul Menage06ecb272008-04-29 01:00:06 -070010189 s64 val)
Peter Zijlstra6f505b12008-01-25 21:08:30 +010010190{
Paul Menage06ecb272008-04-29 01:00:06 -070010191 return sched_group_set_rt_runtime(cgroup_tg(cgrp), val);
Peter Zijlstra6f505b12008-01-25 21:08:30 +010010192}
10193
Paul Menage06ecb272008-04-29 01:00:06 -070010194static s64 cpu_rt_runtime_read(struct cgroup *cgrp, struct cftype *cft)
Peter Zijlstra6f505b12008-01-25 21:08:30 +010010195{
Paul Menage06ecb272008-04-29 01:00:06 -070010196 return sched_group_rt_runtime(cgroup_tg(cgrp));
Peter Zijlstra6f505b12008-01-25 21:08:30 +010010197}
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +020010198
10199static int cpu_rt_period_write_uint(struct cgroup *cgrp, struct cftype *cftype,
10200 u64 rt_period_us)
10201{
10202 return sched_group_set_rt_period(cgroup_tg(cgrp), rt_period_us);
10203}
10204
10205static u64 cpu_rt_period_read_uint(struct cgroup *cgrp, struct cftype *cft)
10206{
10207 return sched_group_rt_period(cgroup_tg(cgrp));
10208}
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +020010209#endif /* CONFIG_RT_GROUP_SCHED */
Peter Zijlstra6f505b12008-01-25 21:08:30 +010010210
Paul Menagefe5c7cc2007-10-29 21:18:11 +010010211static struct cftype cpu_files[] = {
Peter Zijlstra052f1dc2008-02-13 15:45:40 +010010212#ifdef CONFIG_FAIR_GROUP_SCHED
Paul Menagefe5c7cc2007-10-29 21:18:11 +010010213 {
10214 .name = "shares",
Paul Menagef4c753b2008-04-29 00:59:56 -070010215 .read_u64 = cpu_shares_read_u64,
10216 .write_u64 = cpu_shares_write_u64,
Paul Menagefe5c7cc2007-10-29 21:18:11 +010010217 },
Peter Zijlstra052f1dc2008-02-13 15:45:40 +010010218#endif
10219#ifdef CONFIG_RT_GROUP_SCHED
Peter Zijlstra6f505b12008-01-25 21:08:30 +010010220 {
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +010010221 .name = "rt_runtime_us",
Paul Menage06ecb272008-04-29 01:00:06 -070010222 .read_s64 = cpu_rt_runtime_read,
10223 .write_s64 = cpu_rt_runtime_write,
Peter Zijlstra6f505b12008-01-25 21:08:30 +010010224 },
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +020010225 {
10226 .name = "rt_period_us",
Paul Menagef4c753b2008-04-29 00:59:56 -070010227 .read_u64 = cpu_rt_period_read_uint,
10228 .write_u64 = cpu_rt_period_write_uint,
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +020010229 },
Peter Zijlstra052f1dc2008-02-13 15:45:40 +010010230#endif
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -070010231};
10232
10233static int cpu_cgroup_populate(struct cgroup_subsys *ss, struct cgroup *cont)
10234{
Paul Menagefe5c7cc2007-10-29 21:18:11 +010010235 return cgroup_add_files(cont, ss, cpu_files, ARRAY_SIZE(cpu_files));
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -070010236}
10237
10238struct cgroup_subsys cpu_cgroup_subsys = {
Ingo Molnar38605ca2007-10-29 21:18:11 +010010239 .name = "cpu",
10240 .create = cpu_cgroup_create,
10241 .destroy = cpu_cgroup_destroy,
10242 .can_attach = cpu_cgroup_can_attach,
10243 .attach = cpu_cgroup_attach,
10244 .populate = cpu_cgroup_populate,
10245 .subsys_id = cpu_cgroup_subsys_id,
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -070010246 .early_init = 1,
10247};
10248
Peter Zijlstra052f1dc2008-02-13 15:45:40 +010010249#endif /* CONFIG_CGROUP_SCHED */
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +010010250
10251#ifdef CONFIG_CGROUP_CPUACCT
10252
10253/*
10254 * CPU accounting code for task groups.
10255 *
10256 * Based on the work by Paul Menage (menage@google.com) and Balbir Singh
10257 * (balbir@in.ibm.com).
10258 */
10259
Bharata B Rao934352f2008-11-10 20:41:13 +053010260/* track cpu usage of a group of tasks and its child groups */
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +010010261struct cpuacct {
10262 struct cgroup_subsys_state css;
10263 /* cpuusage holds pointer to a u64-type object on every cpu */
10264 u64 *cpuusage;
Bharata B Raoef12fef2009-03-31 10:02:22 +053010265 struct percpu_counter cpustat[CPUACCT_STAT_NSTATS];
Bharata B Rao934352f2008-11-10 20:41:13 +053010266 struct cpuacct *parent;
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +010010267};
10268
10269struct cgroup_subsys cpuacct_subsys;
10270
10271/* return cpu accounting group corresponding to this container */
Dhaval Giani32cd7562008-02-29 10:02:43 +053010272static inline struct cpuacct *cgroup_ca(struct cgroup *cgrp)
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +010010273{
Dhaval Giani32cd7562008-02-29 10:02:43 +053010274 return container_of(cgroup_subsys_state(cgrp, cpuacct_subsys_id),
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +010010275 struct cpuacct, css);
10276}
10277
10278/* return cpu accounting group to which this task belongs */
10279static inline struct cpuacct *task_ca(struct task_struct *tsk)
10280{
10281 return container_of(task_subsys_state(tsk, cpuacct_subsys_id),
10282 struct cpuacct, css);
10283}
10284
10285/* create a new cpu accounting group */
10286static struct cgroup_subsys_state *cpuacct_create(
Dhaval Giani32cd7562008-02-29 10:02:43 +053010287 struct cgroup_subsys *ss, struct cgroup *cgrp)
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +010010288{
10289 struct cpuacct *ca = kzalloc(sizeof(*ca), GFP_KERNEL);
Bharata B Raoef12fef2009-03-31 10:02:22 +053010290 int i;
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +010010291
10292 if (!ca)
Bharata B Raoef12fef2009-03-31 10:02:22 +053010293 goto out;
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +010010294
10295 ca->cpuusage = alloc_percpu(u64);
Bharata B Raoef12fef2009-03-31 10:02:22 +053010296 if (!ca->cpuusage)
10297 goto out_free_ca;
10298
10299 for (i = 0; i < CPUACCT_STAT_NSTATS; i++)
10300 if (percpu_counter_init(&ca->cpustat[i], 0))
10301 goto out_free_counters;
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +010010302
Bharata B Rao934352f2008-11-10 20:41:13 +053010303 if (cgrp->parent)
10304 ca->parent = cgroup_ca(cgrp->parent);
10305
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +010010306 return &ca->css;
Bharata B Raoef12fef2009-03-31 10:02:22 +053010307
10308out_free_counters:
10309 while (--i >= 0)
10310 percpu_counter_destroy(&ca->cpustat[i]);
10311 free_percpu(ca->cpuusage);
10312out_free_ca:
10313 kfree(ca);
10314out:
10315 return ERR_PTR(-ENOMEM);
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +010010316}
10317
10318/* destroy an existing cpu accounting group */
Ingo Molnar41a2d6c2007-12-05 15:46:09 +010010319static void
Dhaval Giani32cd7562008-02-29 10:02:43 +053010320cpuacct_destroy(struct cgroup_subsys *ss, struct cgroup *cgrp)
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +010010321{
Dhaval Giani32cd7562008-02-29 10:02:43 +053010322 struct cpuacct *ca = cgroup_ca(cgrp);
Bharata B Raoef12fef2009-03-31 10:02:22 +053010323 int i;
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +010010324
Bharata B Raoef12fef2009-03-31 10:02:22 +053010325 for (i = 0; i < CPUACCT_STAT_NSTATS; i++)
10326 percpu_counter_destroy(&ca->cpustat[i]);
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +010010327 free_percpu(ca->cpuusage);
10328 kfree(ca);
10329}
10330
Ken Chen720f5492008-12-15 22:02:01 -080010331static u64 cpuacct_cpuusage_read(struct cpuacct *ca, int cpu)
10332{
Rusty Russellb36128c2009-02-20 16:29:08 +090010333 u64 *cpuusage = per_cpu_ptr(ca->cpuusage, cpu);
Ken Chen720f5492008-12-15 22:02:01 -080010334 u64 data;
10335
10336#ifndef CONFIG_64BIT
10337 /*
10338 * Take rq->lock to make 64-bit read safe on 32-bit platforms.
10339 */
10340 spin_lock_irq(&cpu_rq(cpu)->lock);
10341 data = *cpuusage;
10342 spin_unlock_irq(&cpu_rq(cpu)->lock);
10343#else
10344 data = *cpuusage;
10345#endif
10346
10347 return data;
10348}
10349
10350static void cpuacct_cpuusage_write(struct cpuacct *ca, int cpu, u64 val)
10351{
Rusty Russellb36128c2009-02-20 16:29:08 +090010352 u64 *cpuusage = per_cpu_ptr(ca->cpuusage, cpu);
Ken Chen720f5492008-12-15 22:02:01 -080010353
10354#ifndef CONFIG_64BIT
10355 /*
10356 * Take rq->lock to make 64-bit write safe on 32-bit platforms.
10357 */
10358 spin_lock_irq(&cpu_rq(cpu)->lock);
10359 *cpuusage = val;
10360 spin_unlock_irq(&cpu_rq(cpu)->lock);
10361#else
10362 *cpuusage = val;
10363#endif
10364}
10365
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +010010366/* return total cpu usage (in nanoseconds) of a group */
Dhaval Giani32cd7562008-02-29 10:02:43 +053010367static u64 cpuusage_read(struct cgroup *cgrp, struct cftype *cft)
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +010010368{
Dhaval Giani32cd7562008-02-29 10:02:43 +053010369 struct cpuacct *ca = cgroup_ca(cgrp);
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +010010370 u64 totalcpuusage = 0;
10371 int i;
10372
Ken Chen720f5492008-12-15 22:02:01 -080010373 for_each_present_cpu(i)
10374 totalcpuusage += cpuacct_cpuusage_read(ca, i);
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +010010375
10376 return totalcpuusage;
10377}
10378
Dhaval Giani0297b802008-02-29 10:02:44 +053010379static int cpuusage_write(struct cgroup *cgrp, struct cftype *cftype,
10380 u64 reset)
10381{
10382 struct cpuacct *ca = cgroup_ca(cgrp);
10383 int err = 0;
10384 int i;
10385
10386 if (reset) {
10387 err = -EINVAL;
10388 goto out;
10389 }
10390
Ken Chen720f5492008-12-15 22:02:01 -080010391 for_each_present_cpu(i)
10392 cpuacct_cpuusage_write(ca, i, 0);
Dhaval Giani0297b802008-02-29 10:02:44 +053010393
Dhaval Giani0297b802008-02-29 10:02:44 +053010394out:
10395 return err;
10396}
10397
Ken Chene9515c32008-12-15 22:04:15 -080010398static int cpuacct_percpu_seq_read(struct cgroup *cgroup, struct cftype *cft,
10399 struct seq_file *m)
10400{
10401 struct cpuacct *ca = cgroup_ca(cgroup);
10402 u64 percpu;
10403 int i;
10404
10405 for_each_present_cpu(i) {
10406 percpu = cpuacct_cpuusage_read(ca, i);
10407 seq_printf(m, "%llu ", (unsigned long long) percpu);
10408 }
10409 seq_printf(m, "\n");
10410 return 0;
10411}
10412
Bharata B Raoef12fef2009-03-31 10:02:22 +053010413static const char *cpuacct_stat_desc[] = {
10414 [CPUACCT_STAT_USER] = "user",
10415 [CPUACCT_STAT_SYSTEM] = "system",
10416};
10417
10418static int cpuacct_stats_show(struct cgroup *cgrp, struct cftype *cft,
10419 struct cgroup_map_cb *cb)
10420{
10421 struct cpuacct *ca = cgroup_ca(cgrp);
10422 int i;
10423
10424 for (i = 0; i < CPUACCT_STAT_NSTATS; i++) {
10425 s64 val = percpu_counter_read(&ca->cpustat[i]);
10426 val = cputime64_to_clock_t(val);
10427 cb->fill(cb, cpuacct_stat_desc[i], val);
10428 }
10429 return 0;
10430}
10431
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +010010432static struct cftype files[] = {
10433 {
10434 .name = "usage",
Paul Menagef4c753b2008-04-29 00:59:56 -070010435 .read_u64 = cpuusage_read,
10436 .write_u64 = cpuusage_write,
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +010010437 },
Ken Chene9515c32008-12-15 22:04:15 -080010438 {
10439 .name = "usage_percpu",
10440 .read_seq_string = cpuacct_percpu_seq_read,
10441 },
Bharata B Raoef12fef2009-03-31 10:02:22 +053010442 {
10443 .name = "stat",
10444 .read_map = cpuacct_stats_show,
10445 },
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +010010446};
10447
Dhaval Giani32cd7562008-02-29 10:02:43 +053010448static int cpuacct_populate(struct cgroup_subsys *ss, struct cgroup *cgrp)
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +010010449{
Dhaval Giani32cd7562008-02-29 10:02:43 +053010450 return cgroup_add_files(cgrp, ss, files, ARRAY_SIZE(files));
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +010010451}
10452
10453/*
10454 * charge this task's execution time to its accounting group.
10455 *
10456 * called with rq->lock held.
10457 */
10458static void cpuacct_charge(struct task_struct *tsk, u64 cputime)
10459{
10460 struct cpuacct *ca;
Bharata B Rao934352f2008-11-10 20:41:13 +053010461 int cpu;
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +010010462
Li Zefanc40c6f82009-02-26 15:40:15 +080010463 if (unlikely(!cpuacct_subsys.active))
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +010010464 return;
10465
Bharata B Rao934352f2008-11-10 20:41:13 +053010466 cpu = task_cpu(tsk);
Bharata B Raoa18b83b2009-03-23 10:02:53 +053010467
10468 rcu_read_lock();
10469
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +010010470 ca = task_ca(tsk);
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +010010471
Bharata B Rao934352f2008-11-10 20:41:13 +053010472 for (; ca; ca = ca->parent) {
Rusty Russellb36128c2009-02-20 16:29:08 +090010473 u64 *cpuusage = per_cpu_ptr(ca->cpuusage, cpu);
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +010010474 *cpuusage += cputime;
10475 }
Bharata B Raoa18b83b2009-03-23 10:02:53 +053010476
10477 rcu_read_unlock();
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +010010478}
10479
Bharata B Raoef12fef2009-03-31 10:02:22 +053010480/*
10481 * Charge the system/user time to the task's accounting group.
10482 */
10483static void cpuacct_update_stats(struct task_struct *tsk,
10484 enum cpuacct_stat_index idx, cputime_t val)
10485{
10486 struct cpuacct *ca;
10487
10488 if (unlikely(!cpuacct_subsys.active))
10489 return;
10490
10491 rcu_read_lock();
10492 ca = task_ca(tsk);
10493
10494 do {
10495 percpu_counter_add(&ca->cpustat[idx], val);
10496 ca = ca->parent;
10497 } while (ca);
10498 rcu_read_unlock();
10499}
10500
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +010010501struct cgroup_subsys cpuacct_subsys = {
10502 .name = "cpuacct",
10503 .create = cpuacct_create,
10504 .destroy = cpuacct_destroy,
10505 .populate = cpuacct_populate,
10506 .subsys_id = cpuacct_subsys_id,
10507};
10508#endif /* CONFIG_CGROUP_CPUACCT */