blob: 39e7086021697ddfbea15b4cf5cf6013953c3b08 [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>
Mike Travis434d53b2008-04-04 18:11:04 -070071#include <linux/bootmem.h>
Peter Zijlstraf00b45c2008-04-19 19:45:00 +020072#include <linux/debugfs.h>
73#include <linux/ctype.h>
Steven Rostedt6cd8a4b2008-05-12 21:20:42 +020074#include <linux/ftrace.h>
Mathieu Desnoyers0a16b602008-07-18 12:16:17 -040075#include <trace/sched.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070076
Eric Dumazet5517d862007-05-08 00:32:57 -070077#include <asm/tlb.h>
Satyam Sharma838225b2007-10-24 18:23:50 +020078#include <asm/irq_regs.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070079
Gregory Haskins6e0534f2008-05-12 21:21:01 +020080#include "sched_cpupri.h"
81
Linus Torvalds1da177e2005-04-16 15:20:36 -070082/*
83 * Convert user-nice values [ -20 ... 0 ... 19 ]
84 * to static priority [ MAX_RT_PRIO..MAX_PRIO-1 ],
85 * and back.
86 */
87#define NICE_TO_PRIO(nice) (MAX_RT_PRIO + (nice) + 20)
88#define PRIO_TO_NICE(prio) ((prio) - MAX_RT_PRIO - 20)
89#define TASK_NICE(p) PRIO_TO_NICE((p)->static_prio)
90
91/*
92 * 'User priority' is the nice value converted to something we
93 * can work with better when scaling various scheduler parameters,
94 * it's a [ 0 ... 39 ] range.
95 */
96#define USER_PRIO(p) ((p)-MAX_RT_PRIO)
97#define TASK_USER_PRIO(p) USER_PRIO((p)->static_prio)
98#define MAX_USER_PRIO (USER_PRIO(MAX_PRIO))
99
100/*
Ingo Molnard7876a02008-01-25 21:08:19 +0100101 * Helpers for converting nanosecond timing to jiffy resolution
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102 */
Eric Dumazetd6322fa2007-11-09 22:39:38 +0100103#define NS_TO_JIFFIES(TIME) ((unsigned long)(TIME) / (NSEC_PER_SEC / HZ))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200105#define NICE_0_LOAD SCHED_LOAD_SCALE
106#define NICE_0_SHIFT SCHED_LOAD_SHIFT
107
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108/*
109 * These are the 'tuning knobs' of the scheduler:
110 *
Dmitry Adamushkoa4ec24b2007-10-15 17:00:13 +0200111 * default timeslice is 100 msecs (used only for SCHED_RR tasks).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112 * Timeslices get refilled after they expire.
113 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114#define DEF_TIMESLICE (100 * HZ / 1000)
Peter Williams2dd73a42006-06-27 02:54:34 -0700115
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +0200116/*
117 * single value that denotes runtime == period, ie unlimited time.
118 */
119#define RUNTIME_INF ((u64)~0ULL)
120
Mathieu Desnoyers7e066fb2008-11-14 17:47:47 -0500121DEFINE_TRACE(sched_wait_task);
122DEFINE_TRACE(sched_wakeup);
123DEFINE_TRACE(sched_wakeup_new);
124DEFINE_TRACE(sched_switch);
125DEFINE_TRACE(sched_migrate_task);
126
Eric Dumazet5517d862007-05-08 00:32:57 -0700127#ifdef CONFIG_SMP
Steven Noonanfd2ab302009-01-11 01:04:22 -0800128
129static void double_rq_lock(struct rq *rq1, struct rq *rq2);
130
Eric Dumazet5517d862007-05-08 00:32:57 -0700131/*
132 * Divide a load by a sched group cpu_power : (load / sg->__cpu_power)
133 * Since cpu_power is a 'constant', we can use a reciprocal divide.
134 */
135static inline u32 sg_div_cpu_power(const struct sched_group *sg, u32 load)
136{
137 return reciprocal_divide(load, sg->reciprocal_cpu_power);
138}
139
140/*
141 * Each time a sched group cpu_power is changed,
142 * we must compute its reciprocal value
143 */
144static inline void sg_inc_cpu_power(struct sched_group *sg, u32 val)
145{
146 sg->__cpu_power += val;
147 sg->reciprocal_cpu_power = reciprocal_value(sg->__cpu_power);
148}
149#endif
150
Ingo Molnare05606d2007-07-09 18:51:59 +0200151static inline int rt_policy(int policy)
152{
Roel Kluin3f33a7c2008-05-13 23:44:11 +0200153 if (unlikely(policy == SCHED_FIFO || policy == SCHED_RR))
Ingo Molnare05606d2007-07-09 18:51:59 +0200154 return 1;
155 return 0;
156}
157
158static inline int task_has_rt_policy(struct task_struct *p)
159{
160 return rt_policy(p->policy);
161}
162
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163/*
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200164 * This is the priority-queue data structure of the RT scheduling class:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165 */
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200166struct rt_prio_array {
167 DECLARE_BITMAP(bitmap, MAX_RT_PRIO+1); /* include 1 bit for delimiter */
168 struct list_head queue[MAX_RT_PRIO];
169};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +0200171struct rt_bandwidth {
Ingo Molnarea736ed2008-03-25 13:51:45 +0100172 /* nests inside the rq lock: */
173 spinlock_t rt_runtime_lock;
174 ktime_t rt_period;
175 u64 rt_runtime;
176 struct hrtimer rt_period_timer;
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +0200177};
178
179static struct rt_bandwidth def_rt_bandwidth;
180
181static int do_sched_rt_period_timer(struct rt_bandwidth *rt_b, int overrun);
182
183static enum hrtimer_restart sched_rt_period_timer(struct hrtimer *timer)
184{
185 struct rt_bandwidth *rt_b =
186 container_of(timer, struct rt_bandwidth, rt_period_timer);
187 ktime_t now;
188 int overrun;
189 int idle = 0;
190
191 for (;;) {
192 now = hrtimer_cb_get_time(timer);
193 overrun = hrtimer_forward(timer, now, rt_b->rt_period);
194
195 if (!overrun)
196 break;
197
198 idle = do_sched_rt_period_timer(rt_b, overrun);
199 }
200
201 return idle ? HRTIMER_NORESTART : HRTIMER_RESTART;
202}
203
204static
205void init_rt_bandwidth(struct rt_bandwidth *rt_b, u64 period, u64 runtime)
206{
207 rt_b->rt_period = ns_to_ktime(period);
208 rt_b->rt_runtime = runtime;
209
Peter Zijlstraac086bc2008-04-19 19:44:58 +0200210 spin_lock_init(&rt_b->rt_runtime_lock);
211
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +0200212 hrtimer_init(&rt_b->rt_period_timer,
213 CLOCK_MONOTONIC, HRTIMER_MODE_REL);
214 rt_b->rt_period_timer.function = sched_rt_period_timer;
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +0200215}
216
Krzysztof Heltc8bfff62008-09-05 23:46:19 +0200217static inline int rt_bandwidth_enabled(void)
218{
219 return sysctl_sched_rt_runtime >= 0;
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +0200220}
221
222static void start_rt_bandwidth(struct rt_bandwidth *rt_b)
223{
224 ktime_t now;
225
Hiroshi Shimamotocac64d02009-02-25 09:59:26 -0800226 if (!rt_bandwidth_enabled() || rt_b->rt_runtime == RUNTIME_INF)
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +0200227 return;
228
229 if (hrtimer_active(&rt_b->rt_period_timer))
230 return;
231
232 spin_lock(&rt_b->rt_runtime_lock);
233 for (;;) {
234 if (hrtimer_active(&rt_b->rt_period_timer))
235 break;
236
237 now = hrtimer_cb_get_time(&rt_b->rt_period_timer);
238 hrtimer_forward(&rt_b->rt_period_timer, now, rt_b->rt_period);
Arjan van de Vencc584b22008-09-01 15:02:30 -0700239 hrtimer_start_expires(&rt_b->rt_period_timer,
240 HRTIMER_MODE_ABS);
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +0200241 }
242 spin_unlock(&rt_b->rt_runtime_lock);
243}
244
245#ifdef CONFIG_RT_GROUP_SCHED
246static void destroy_rt_bandwidth(struct rt_bandwidth *rt_b)
247{
248 hrtimer_cancel(&rt_b->rt_period_timer);
249}
250#endif
251
Heiko Carstens712555e2008-04-28 11:33:07 +0200252/*
253 * sched_domains_mutex serializes calls to arch_init_sched_domains,
254 * detach_destroy_domains and partition_sched_domains.
255 */
256static DEFINE_MUTEX(sched_domains_mutex);
257
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100258#ifdef CONFIG_GROUP_SCHED
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +0200259
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -0700260#include <linux/cgroup.h>
261
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +0200262struct cfs_rq;
263
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100264static LIST_HEAD(task_groups);
265
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +0200266/* task group related information */
Ingo Molnar4cf86d72007-10-15 17:00:14 +0200267struct task_group {
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100268#ifdef CONFIG_CGROUP_SCHED
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -0700269 struct cgroup_subsys_state css;
270#endif
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100271
Arun R Bharadwaj6c415b92008-12-01 20:49:05 +0530272#ifdef CONFIG_USER_SCHED
273 uid_t uid;
274#endif
275
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100276#ifdef CONFIG_FAIR_GROUP_SCHED
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +0200277 /* schedulable entities of this group on each cpu */
278 struct sched_entity **se;
279 /* runqueue "owned" by this group on each cpu */
280 struct cfs_rq **cfs_rq;
281 unsigned long shares;
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100282#endif
283
284#ifdef CONFIG_RT_GROUP_SCHED
285 struct sched_rt_entity **rt_se;
286 struct rt_rq **rt_rq;
287
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +0200288 struct rt_bandwidth rt_bandwidth;
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100289#endif
Srivatsa Vaddagiri6b2d7702008-01-25 21:08:00 +0100290
Srivatsa Vaddagiriae8393e2007-10-29 21:18:11 +0100291 struct rcu_head rcu;
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100292 struct list_head list;
Peter Zijlstraf473aa52008-04-19 19:45:00 +0200293
294 struct task_group *parent;
295 struct list_head siblings;
296 struct list_head children;
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +0200297};
298
Dhaval Giani354d60c2008-04-19 19:44:59 +0200299#ifdef CONFIG_USER_SCHED
Peter Zijlstraeff766a2008-04-19 19:45:00 +0200300
Arun R Bharadwaj6c415b92008-12-01 20:49:05 +0530301/* Helper function to pass uid information to create_sched_user() */
302void set_tg_uid(struct user_struct *user)
303{
304 user->tg->uid = user->uid;
305}
306
Peter Zijlstraeff766a2008-04-19 19:45:00 +0200307/*
308 * Root task group.
309 * Every UID task group (including init_task_group aka UID-0) will
310 * be a child to this group.
311 */
312struct task_group root_task_group;
313
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100314#ifdef CONFIG_FAIR_GROUP_SCHED
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +0200315/* Default task group's sched entity on each cpu */
316static DEFINE_PER_CPU(struct sched_entity, init_sched_entity);
317/* Default task group's cfs_rq on each cpu */
318static DEFINE_PER_CPU(struct cfs_rq, init_cfs_rq) ____cacheline_aligned_in_smp;
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +0200319#endif /* CONFIG_FAIR_GROUP_SCHED */
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100320
321#ifdef CONFIG_RT_GROUP_SCHED
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100322static DEFINE_PER_CPU(struct sched_rt_entity, init_sched_rt_entity);
323static DEFINE_PER_CPU(struct rt_rq, init_rt_rq) ____cacheline_aligned_in_smp;
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +0200324#endif /* CONFIG_RT_GROUP_SCHED */
Peter Zijlstra9a7e0b12008-08-19 12:33:06 +0200325#else /* !CONFIG_USER_SCHED */
Peter Zijlstraeff766a2008-04-19 19:45:00 +0200326#define root_task_group init_task_group
Peter Zijlstra9a7e0b12008-08-19 12:33:06 +0200327#endif /* CONFIG_USER_SCHED */
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100328
Peter Zijlstra8ed36992008-02-13 15:45:39 +0100329/* task_group_lock serializes add/remove of task groups and also changes to
Srivatsa Vaddagiriec2c5072008-01-25 21:07:59 +0100330 * a task group's cpu shares.
331 */
Peter Zijlstra8ed36992008-02-13 15:45:39 +0100332static DEFINE_SPINLOCK(task_group_lock);
Srivatsa Vaddagiriec2c5072008-01-25 21:07:59 +0100333
Peter Zijlstra57310a92009-03-09 13:56:21 +0100334#ifdef CONFIG_SMP
335static int root_task_group_empty(void)
336{
337 return list_empty(&root_task_group.children);
338}
339#endif
340
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100341#ifdef CONFIG_FAIR_GROUP_SCHED
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100342#ifdef CONFIG_USER_SCHED
Ingo Molnar0eab9142008-01-25 21:08:19 +0100343# define INIT_TASK_GROUP_LOAD (2*NICE_0_LOAD)
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +0200344#else /* !CONFIG_USER_SCHED */
Srivatsa Vaddagiri93f992c2008-01-25 21:07:59 +0100345# define INIT_TASK_GROUP_LOAD NICE_0_LOAD
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +0200346#endif /* CONFIG_USER_SCHED */
Srivatsa Vaddagiri24e377a2007-10-15 17:00:09 +0200347
Miao Xiecb4ad1f2008-04-28 12:54:56 +0800348/*
Lai Jiangshan2e084782008-06-12 16:42:58 +0800349 * A weight of 0 or 1 can cause arithmetics problems.
350 * A weight of a cfs_rq is the sum of weights of which entities
351 * are queued on this cfs_rq, so a weight of a entity should not be
352 * too large, so as the shares value of a task group.
Miao Xiecb4ad1f2008-04-28 12:54:56 +0800353 * (The default weight is 1024 - so there's no practical
354 * limitation from this.)
355 */
Peter Zijlstra18d95a22008-04-19 19:45:00 +0200356#define MIN_SHARES 2
Lai Jiangshan2e084782008-06-12 16:42:58 +0800357#define MAX_SHARES (1UL << 18)
Peter Zijlstra18d95a22008-04-19 19:45:00 +0200358
Srivatsa Vaddagiri93f992c2008-01-25 21:07:59 +0100359static int init_task_group_load = INIT_TASK_GROUP_LOAD;
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100360#endif
361
362/* Default task group.
363 * Every task in system belong to this group at bootup.
364 */
Mike Travis434d53b2008-04-04 18:11:04 -0700365struct task_group init_task_group;
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +0200366
367/* return group to which a task belongs */
Ingo Molnar4cf86d72007-10-15 17:00:14 +0200368static inline struct task_group *task_group(struct task_struct *p)
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +0200369{
Ingo Molnar4cf86d72007-10-15 17:00:14 +0200370 struct task_group *tg;
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +0200371
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100372#ifdef CONFIG_USER_SCHED
David Howellsc69e8d92008-11-14 10:39:19 +1100373 rcu_read_lock();
374 tg = __task_cred(p)->user->tg;
375 rcu_read_unlock();
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100376#elif defined(CONFIG_CGROUP_SCHED)
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -0700377 tg = container_of(task_subsys_state(p, cpu_cgroup_subsys_id),
378 struct task_group, css);
Srivatsa Vaddagiri24e377a2007-10-15 17:00:09 +0200379#else
Ingo Molnar41a2d6c2007-12-05 15:46:09 +0100380 tg = &init_task_group;
Srivatsa Vaddagiri24e377a2007-10-15 17:00:09 +0200381#endif
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +0200382 return tg;
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +0200383}
384
385/* Change a task's cfs_rq and parent entity if it moves across CPUs/groups */
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100386static inline void set_task_rq(struct task_struct *p, unsigned int cpu)
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +0200387{
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100388#ifdef CONFIG_FAIR_GROUP_SCHED
Dmitry Adamushkoce96b5a2007-11-15 20:57:40 +0100389 p->se.cfs_rq = task_group(p)->cfs_rq[cpu];
390 p->se.parent = task_group(p)->se[cpu];
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100391#endif
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100392
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100393#ifdef CONFIG_RT_GROUP_SCHED
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100394 p->rt.rt_rq = task_group(p)->rt_rq[cpu];
395 p->rt.parent = task_group(p)->rt_se[cpu];
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100396#endif
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +0200397}
398
399#else
400
Peter Zijlstra57310a92009-03-09 13:56:21 +0100401#ifdef CONFIG_SMP
402static int root_task_group_empty(void)
403{
404 return 1;
405}
406#endif
407
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100408static inline void set_task_rq(struct task_struct *p, unsigned int cpu) { }
Peter Zijlstra83378262008-06-27 13:41:37 +0200409static inline struct task_group *task_group(struct task_struct *p)
410{
411 return NULL;
412}
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +0200413
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100414#endif /* CONFIG_GROUP_SCHED */
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +0200415
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200416/* CFS-related fields in a runqueue */
417struct cfs_rq {
418 struct load_weight load;
419 unsigned long nr_running;
420
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200421 u64 exec_clock;
Ingo Molnare9acbff2007-10-15 17:00:04 +0200422 u64 min_vruntime;
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200423
424 struct rb_root tasks_timeline;
425 struct rb_node *rb_leftmost;
Peter Zijlstra4a55bd52008-04-19 19:45:00 +0200426
427 struct list_head tasks;
428 struct list_head *balance_iterator;
429
430 /*
431 * 'curr' points to currently running entity on this cfs_rq.
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200432 * It is set to NULL otherwise (i.e when none are currently running).
433 */
Peter Zijlstra47932412008-11-04 21:25:09 +0100434 struct sched_entity *curr, *next, *last;
Peter Zijlstraddc97292007-10-15 17:00:10 +0200435
Peter Zijlstra5ac5c4d2008-11-10 10:46:32 +0100436 unsigned int nr_spread_over;
Peter Zijlstraddc97292007-10-15 17:00:10 +0200437
Ingo Molnar62160e3f2007-10-15 17:00:03 +0200438#ifdef CONFIG_FAIR_GROUP_SCHED
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200439 struct rq *rq; /* cpu runqueue to which this cfs_rq is attached */
440
Ingo Molnar41a2d6c2007-12-05 15:46:09 +0100441 /*
442 * leaf cfs_rqs are those that hold tasks (lowest schedulable entity in
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200443 * a hierarchy). Non-leaf lrqs hold other higher schedulable entities
444 * (like users, containers etc.)
445 *
446 * leaf_cfs_rq_list ties together list of leaf cfs_rq's in a cpu. This
447 * list is used during load balance.
448 */
Ingo Molnar41a2d6c2007-12-05 15:46:09 +0100449 struct list_head leaf_cfs_rq_list;
450 struct task_group *tg; /* group that "owns" this runqueue */
Peter Zijlstrac09595f2008-06-27 13:41:14 +0200451
452#ifdef CONFIG_SMP
Peter Zijlstrac09595f2008-06-27 13:41:14 +0200453 /*
Peter Zijlstrac8cba852008-06-27 13:41:23 +0200454 * the part of load.weight contributed by tasks
Peter Zijlstrac09595f2008-06-27 13:41:14 +0200455 */
Peter Zijlstrac8cba852008-06-27 13:41:23 +0200456 unsigned long task_weight;
Peter Zijlstrac09595f2008-06-27 13:41:14 +0200457
Peter Zijlstrac8cba852008-06-27 13:41:23 +0200458 /*
459 * h_load = weight * f(tg)
460 *
461 * Where f(tg) is the recursive weight fraction assigned to
462 * this group.
463 */
464 unsigned long h_load;
Peter Zijlstrac09595f2008-06-27 13:41:14 +0200465
Peter Zijlstrac8cba852008-06-27 13:41:23 +0200466 /*
467 * this cpu's part of tg->shares
468 */
469 unsigned long shares;
Peter Zijlstraf1d239f2008-06-27 13:41:38 +0200470
471 /*
472 * load.weight at the time we set shares
473 */
474 unsigned long rq_weight;
Peter Zijlstrac09595f2008-06-27 13:41:14 +0200475#endif
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200476#endif
477};
478
479/* Real-Time classes' related field in a runqueue: */
480struct rt_rq {
481 struct rt_prio_array active;
Steven Rostedt63489e42008-01-25 21:08:03 +0100482 unsigned long rt_nr_running;
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100483#if defined CONFIG_SMP || defined CONFIG_RT_GROUP_SCHED
Gregory Haskinse864c492008-12-29 09:39:49 -0500484 struct {
485 int curr; /* highest queued rt task prio */
Gregory Haskins398a1532009-01-14 09:10:04 -0500486#ifdef CONFIG_SMP
Gregory Haskinse864c492008-12-29 09:39:49 -0500487 int next; /* next highest */
Gregory Haskins398a1532009-01-14 09:10:04 -0500488#endif
Gregory Haskinse864c492008-12-29 09:39:49 -0500489 } highest_prio;
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100490#endif
Peter Zijlstrafa85ae22008-01-25 21:08:29 +0100491#ifdef CONFIG_SMP
Gregory Haskins73fe6aa2008-01-25 21:08:07 +0100492 unsigned long rt_nr_migratory;
Gregory Haskinsa22d7fc2008-01-25 21:08:12 +0100493 int overloaded;
Gregory Haskins917b6272008-12-29 09:39:53 -0500494 struct plist_head pushable_tasks;
Peter Zijlstrafa85ae22008-01-25 21:08:29 +0100495#endif
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100496 int rt_throttled;
Peter Zijlstrafa85ae22008-01-25 21:08:29 +0100497 u64 rt_time;
Peter Zijlstraac086bc2008-04-19 19:44:58 +0200498 u64 rt_runtime;
Ingo Molnarea736ed2008-03-25 13:51:45 +0100499 /* Nests inside the rq lock: */
Peter Zijlstraac086bc2008-04-19 19:44:58 +0200500 spinlock_t rt_runtime_lock;
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100501
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100502#ifdef CONFIG_RT_GROUP_SCHED
Peter Zijlstra23b0fdf2008-02-13 15:45:39 +0100503 unsigned long rt_nr_boosted;
504
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100505 struct rq *rq;
506 struct list_head leaf_rt_rq_list;
507 struct task_group *tg;
508 struct sched_rt_entity *rt_se;
509#endif
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200510};
511
Gregory Haskins57d885f2008-01-25 21:08:18 +0100512#ifdef CONFIG_SMP
513
514/*
515 * We add the notion of a root-domain which will be used to define per-domain
Ingo Molnar0eab9142008-01-25 21:08:19 +0100516 * variables. Each exclusive cpuset essentially defines an island domain by
517 * fully partitioning the member cpus from any other cpuset. Whenever a new
Gregory Haskins57d885f2008-01-25 21:08:18 +0100518 * exclusive cpuset is created, we also create and attach a new root-domain
519 * object.
520 *
Gregory Haskins57d885f2008-01-25 21:08:18 +0100521 */
522struct root_domain {
523 atomic_t refcount;
Rusty Russellc6c49272008-11-25 02:35:05 +1030524 cpumask_var_t span;
525 cpumask_var_t online;
Gregory Haskins637f5082008-01-25 21:08:18 +0100526
Ingo Molnar0eab9142008-01-25 21:08:19 +0100527 /*
Gregory Haskins637f5082008-01-25 21:08:18 +0100528 * The "RT overload" flag: it gets set if a CPU has more than
529 * one runnable RT task.
530 */
Rusty Russellc6c49272008-11-25 02:35:05 +1030531 cpumask_var_t rto_mask;
Ingo Molnar0eab9142008-01-25 21:08:19 +0100532 atomic_t rto_count;
Gregory Haskins6e0534f2008-05-12 21:21:01 +0200533#ifdef CONFIG_SMP
534 struct cpupri cpupri;
535#endif
Vaidyanathan Srinivasan7a09b1a2008-12-18 23:26:22 +0530536#if defined(CONFIG_SCHED_MC) || defined(CONFIG_SCHED_SMT)
537 /*
538 * Preferred wake up cpu nominated by sched_mc balance that will be
539 * used when most cpus are idle in the system indicating overall very
540 * low system utilisation. Triggered at POWERSAVINGS_BALANCE_WAKEUP(2)
541 */
542 unsigned int sched_mc_preferred_wakeup_cpu;
543#endif
Gregory Haskins57d885f2008-01-25 21:08:18 +0100544};
545
Gregory Haskinsdc938522008-01-25 21:08:26 +0100546/*
547 * By default the system creates a single root-domain with all cpus as
548 * members (mimicking the global state we have today).
549 */
Gregory Haskins57d885f2008-01-25 21:08:18 +0100550static struct root_domain def_root_domain;
551
552#endif
553
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200554/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 * This is the main, per-CPU runqueue data structure.
556 *
557 * Locking rule: those places that want to lock multiple runqueues
558 * (such as the load balancing or the thread migration code), lock
559 * acquire operations must be ordered by ascending &runqueue.
560 */
Ingo Molnar70b97a72006-07-03 00:25:42 -0700561struct rq {
Ingo Molnard8016492007-10-18 21:32:55 +0200562 /* runqueue lock: */
563 spinlock_t lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564
565 /*
566 * nr_running and cpu_load should be in the same cacheline because
567 * remote CPUs use both these fields when doing load calculation.
568 */
569 unsigned long nr_running;
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200570 #define CPU_LOAD_IDX_MAX 5
571 unsigned long cpu_load[CPU_LOAD_IDX_MAX];
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -0700572#ifdef CONFIG_NO_HZ
Guillaume Chazarain15934a32008-04-19 19:44:57 +0200573 unsigned long last_tick_seen;
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -0700574 unsigned char in_nohz_recently;
575#endif
Ingo Molnard8016492007-10-18 21:32:55 +0200576 /* capture load from *all* tasks on this cpu: */
577 struct load_weight load;
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200578 unsigned long nr_load_updates;
579 u64 nr_switches;
Paul Mackerras23a185c2009-02-09 22:42:47 +1100580 u64 nr_migrations_in;
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200581
582 struct cfs_rq cfs;
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100583 struct rt_rq rt;
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100584
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200585#ifdef CONFIG_FAIR_GROUP_SCHED
Ingo Molnard8016492007-10-18 21:32:55 +0200586 /* list of leaf cfs_rq on this cpu: */
587 struct list_head leaf_cfs_rq_list;
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100588#endif
589#ifdef CONFIG_RT_GROUP_SCHED
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100590 struct list_head leaf_rt_rq_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592
593 /*
594 * This is part of a global counter where only the total sum
595 * over all CPUs matters. A task can increase this counter on
596 * one CPU and if it got migrated afterwards it may decrease
597 * it on another CPU. Always updated under the runqueue lock:
598 */
599 unsigned long nr_uninterruptible;
600
Ingo Molnar36c8b582006-07-03 00:25:41 -0700601 struct task_struct *curr, *idle;
Christoph Lameterc9819f42006-12-10 02:20:25 -0800602 unsigned long next_balance;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603 struct mm_struct *prev_mm;
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200604
Peter Zijlstra3e51f332008-05-03 18:29:28 +0200605 u64 clock;
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200606
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607 atomic_t nr_iowait;
608
609#ifdef CONFIG_SMP
Ingo Molnar0eab9142008-01-25 21:08:19 +0100610 struct root_domain *rd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611 struct sched_domain *sd;
612
Henrik Austada0a522c2009-02-13 20:35:45 +0100613 unsigned char idle_at_tick;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614 /* For active balancing */
615 int active_balance;
616 int push_cpu;
Ingo Molnard8016492007-10-18 21:32:55 +0200617 /* cpu of this runqueue: */
618 int cpu;
Gregory Haskins1f11eb62008-06-04 15:04:05 -0400619 int online;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620
Peter Zijlstraa8a51d52008-06-27 13:41:26 +0200621 unsigned long avg_load_per_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622
Ingo Molnar36c8b582006-07-03 00:25:41 -0700623 struct task_struct *migration_thread;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624 struct list_head migration_queue;
625#endif
626
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +0100627#ifdef CONFIG_SCHED_HRTICK
Peter Zijlstra31656512008-07-18 18:01:23 +0200628#ifdef CONFIG_SMP
629 int hrtick_csd_pending;
630 struct call_single_data hrtick_csd;
631#endif
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +0100632 struct hrtimer hrtick_timer;
633#endif
634
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635#ifdef CONFIG_SCHEDSTATS
636 /* latency stats */
637 struct sched_info rq_sched_info;
Ken Chen9c2c4802008-12-16 23:41:22 -0800638 unsigned long long rq_cpu_time;
639 /* could above be rq->cfs_rq.exec_clock + rq->rt_rq.rt_runtime ? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640
641 /* sys_sched_yield() stats */
Ken Chen480b9432007-10-18 21:32:56 +0200642 unsigned int yld_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643
644 /* schedule() stats */
Ken Chen480b9432007-10-18 21:32:56 +0200645 unsigned int sched_switch;
646 unsigned int sched_count;
647 unsigned int sched_goidle;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648
649 /* try_to_wake_up() stats */
Ken Chen480b9432007-10-18 21:32:56 +0200650 unsigned int ttwu_count;
651 unsigned int ttwu_local;
Ingo Molnarb8efb562007-10-15 17:00:10 +0200652
653 /* BKL stats */
Ken Chen480b9432007-10-18 21:32:56 +0200654 unsigned int bkl_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655#endif
656};
657
Fenghua Yuf34e3b62007-07-19 01:48:13 -0700658static DEFINE_PER_CPU_SHARED_ALIGNED(struct rq, runqueues);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659
Peter Zijlstra15afe092008-09-20 23:38:02 +0200660static inline void check_preempt_curr(struct rq *rq, struct task_struct *p, int sync)
Ingo Molnardd41f592007-07-09 18:51:59 +0200661{
Peter Zijlstra15afe092008-09-20 23:38:02 +0200662 rq->curr->sched_class->check_preempt_curr(rq, p, sync);
Ingo Molnardd41f592007-07-09 18:51:59 +0200663}
664
Christoph Lameter0a2966b2006-09-25 23:30:51 -0700665static inline int cpu_of(struct rq *rq)
666{
667#ifdef CONFIG_SMP
668 return rq->cpu;
669#else
670 return 0;
671#endif
672}
673
Ingo Molnar20d315d2007-07-09 18:51:58 +0200674/*
Nick Piggin674311d2005-06-25 14:57:27 -0700675 * The domain tree (rq->sd) is protected by RCU's quiescent state transition.
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -0700676 * See detach_destroy_domains: synchronize_sched for details.
Nick Piggin674311d2005-06-25 14:57:27 -0700677 *
678 * The domain tree of any CPU may only be accessed from within
679 * preempt-disabled sections.
680 */
Ingo Molnar48f24c42006-07-03 00:25:40 -0700681#define for_each_domain(cpu, __sd) \
682 for (__sd = rcu_dereference(cpu_rq(cpu)->sd); __sd; __sd = __sd->parent)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683
684#define cpu_rq(cpu) (&per_cpu(runqueues, (cpu)))
685#define this_rq() (&__get_cpu_var(runqueues))
686#define task_rq(p) cpu_rq(task_cpu(p))
687#define cpu_curr(cpu) (cpu_rq(cpu)->curr)
688
Ingo Molnaraa9c4c02008-12-17 14:10:57 +0100689inline void update_rq_clock(struct rq *rq)
Peter Zijlstra3e51f332008-05-03 18:29:28 +0200690{
691 rq->clock = sched_clock_cpu(cpu_of(rq));
692}
693
Ingo Molnare436d802007-07-19 21:28:35 +0200694/*
Ingo Molnarbf5c91b2007-10-15 17:00:04 +0200695 * Tunables that become constants when CONFIG_SCHED_DEBUG is off:
696 */
697#ifdef CONFIG_SCHED_DEBUG
698# define const_debug __read_mostly
699#else
700# define const_debug static const
701#endif
702
Ingo Molnar017730c2008-05-12 21:20:52 +0200703/**
704 * runqueue_is_locked
705 *
706 * Returns true if the current cpu runqueue is locked.
707 * This interface allows printk to be called with the runqueue lock
708 * held and know whether or not it is OK to wake up the klogd.
709 */
710int runqueue_is_locked(void)
711{
712 int cpu = get_cpu();
713 struct rq *rq = cpu_rq(cpu);
714 int ret;
715
716 ret = spin_is_locked(&rq->lock);
717 put_cpu();
718 return ret;
719}
720
Ingo Molnarbf5c91b2007-10-15 17:00:04 +0200721/*
722 * Debugging: various feature bits
723 */
Peter Zijlstraf00b45c2008-04-19 19:45:00 +0200724
725#define SCHED_FEAT(name, enabled) \
726 __SCHED_FEAT_##name ,
727
Ingo Molnarbf5c91b2007-10-15 17:00:04 +0200728enum {
Peter Zijlstraf00b45c2008-04-19 19:45:00 +0200729#include "sched_features.h"
Ingo Molnarbf5c91b2007-10-15 17:00:04 +0200730};
731
Peter Zijlstraf00b45c2008-04-19 19:45:00 +0200732#undef SCHED_FEAT
Ingo Molnarbf5c91b2007-10-15 17:00:04 +0200733
Peter Zijlstraf00b45c2008-04-19 19:45:00 +0200734#define SCHED_FEAT(name, enabled) \
735 (1UL << __SCHED_FEAT_##name) * enabled |
736
737const_debug unsigned int sysctl_sched_features =
738#include "sched_features.h"
739 0;
740
741#undef SCHED_FEAT
742
743#ifdef CONFIG_SCHED_DEBUG
744#define SCHED_FEAT(name, enabled) \
745 #name ,
746
Harvey Harrison983ed7a2008-04-24 18:17:55 -0700747static __read_mostly char *sched_feat_names[] = {
Peter Zijlstraf00b45c2008-04-19 19:45:00 +0200748#include "sched_features.h"
749 NULL
750};
751
752#undef SCHED_FEAT
753
Li Zefan34f3a812008-10-30 15:23:32 +0800754static int sched_feat_show(struct seq_file *m, void *v)
Peter Zijlstraf00b45c2008-04-19 19:45:00 +0200755{
Peter Zijlstraf00b45c2008-04-19 19:45:00 +0200756 int i;
757
758 for (i = 0; sched_feat_names[i]; i++) {
Li Zefan34f3a812008-10-30 15:23:32 +0800759 if (!(sysctl_sched_features & (1UL << i)))
760 seq_puts(m, "NO_");
761 seq_printf(m, "%s ", sched_feat_names[i]);
Peter Zijlstraf00b45c2008-04-19 19:45:00 +0200762 }
Li Zefan34f3a812008-10-30 15:23:32 +0800763 seq_puts(m, "\n");
Peter Zijlstraf00b45c2008-04-19 19:45:00 +0200764
Li Zefan34f3a812008-10-30 15:23:32 +0800765 return 0;
Peter Zijlstraf00b45c2008-04-19 19:45:00 +0200766}
767
768static ssize_t
769sched_feat_write(struct file *filp, const char __user *ubuf,
770 size_t cnt, loff_t *ppos)
771{
772 char buf[64];
773 char *cmp = buf;
774 int neg = 0;
775 int i;
776
777 if (cnt > 63)
778 cnt = 63;
779
780 if (copy_from_user(&buf, ubuf, cnt))
781 return -EFAULT;
782
783 buf[cnt] = 0;
784
Ingo Molnarc24b7c52008-04-18 10:55:34 +0200785 if (strncmp(buf, "NO_", 3) == 0) {
Peter Zijlstraf00b45c2008-04-19 19:45:00 +0200786 neg = 1;
787 cmp += 3;
788 }
789
790 for (i = 0; sched_feat_names[i]; i++) {
791 int len = strlen(sched_feat_names[i]);
792
793 if (strncmp(cmp, sched_feat_names[i], len) == 0) {
794 if (neg)
795 sysctl_sched_features &= ~(1UL << i);
796 else
797 sysctl_sched_features |= (1UL << i);
798 break;
799 }
800 }
801
802 if (!sched_feat_names[i])
803 return -EINVAL;
804
805 filp->f_pos += cnt;
806
807 return cnt;
808}
809
Li Zefan34f3a812008-10-30 15:23:32 +0800810static int sched_feat_open(struct inode *inode, struct file *filp)
811{
812 return single_open(filp, sched_feat_show, NULL);
813}
814
Peter Zijlstraf00b45c2008-04-19 19:45:00 +0200815static struct file_operations sched_feat_fops = {
Li Zefan34f3a812008-10-30 15:23:32 +0800816 .open = sched_feat_open,
817 .write = sched_feat_write,
818 .read = seq_read,
819 .llseek = seq_lseek,
820 .release = single_release,
Peter Zijlstraf00b45c2008-04-19 19:45:00 +0200821};
822
823static __init int sched_init_debug(void)
824{
Peter Zijlstraf00b45c2008-04-19 19:45:00 +0200825 debugfs_create_file("sched_features", 0644, NULL, NULL,
826 &sched_feat_fops);
827
828 return 0;
829}
830late_initcall(sched_init_debug);
831
832#endif
833
834#define sched_feat(x) (sysctl_sched_features & (1UL << __SCHED_FEAT_##x))
Ingo Molnarbf5c91b2007-10-15 17:00:04 +0200835
836/*
Peter Zijlstrab82d9fd2007-11-09 22:39:39 +0100837 * Number of tasks to iterate in a single balance run.
838 * Limited because this is done with IRQs disabled.
839 */
840const_debug unsigned int sysctl_sched_nr_migrate = 32;
841
842/*
Peter Zijlstra2398f2c2008-06-27 13:41:35 +0200843 * ratelimit for updating the group shares.
Peter Zijlstra55cd5342008-08-04 08:54:26 +0200844 * default: 0.25ms
Peter Zijlstra2398f2c2008-06-27 13:41:35 +0200845 */
Peter Zijlstra55cd5342008-08-04 08:54:26 +0200846unsigned int sysctl_sched_shares_ratelimit = 250000;
Peter Zijlstra2398f2c2008-06-27 13:41:35 +0200847
848/*
Peter Zijlstraffda12a2008-10-17 19:27:02 +0200849 * Inject some fuzzyness into changing the per-cpu group shares
850 * this avoids remote rq-locks at the expense of fairness.
851 * default: 4
852 */
853unsigned int sysctl_sched_shares_thresh = 4;
854
855/*
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +0100856 * period over which we measure -rt task cpu usage in us.
Peter Zijlstrafa85ae22008-01-25 21:08:29 +0100857 * default: 1s
858 */
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +0100859unsigned int sysctl_sched_rt_period = 1000000;
Peter Zijlstrafa85ae22008-01-25 21:08:29 +0100860
Ingo Molnar6892b752008-02-13 14:02:36 +0100861static __read_mostly int scheduler_running;
862
Peter Zijlstrafa85ae22008-01-25 21:08:29 +0100863/*
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +0100864 * part of the period that we allow rt tasks to run in us.
865 * default: 0.95s
Peter Zijlstrafa85ae22008-01-25 21:08:29 +0100866 */
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +0100867int sysctl_sched_rt_runtime = 950000;
868
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +0200869static inline u64 global_rt_period(void)
870{
871 return (u64)sysctl_sched_rt_period * NSEC_PER_USEC;
872}
873
874static inline u64 global_rt_runtime(void)
875{
roel kluine26873b2008-07-22 16:51:15 -0400876 if (sysctl_sched_rt_runtime < 0)
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +0200877 return RUNTIME_INF;
878
879 return (u64)sysctl_sched_rt_runtime * NSEC_PER_USEC;
880}
Peter Zijlstrafa85ae22008-01-25 21:08:29 +0100881
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882#ifndef prepare_arch_switch
Nick Piggin4866cde2005-06-25 14:57:23 -0700883# define prepare_arch_switch(next) do { } while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884#endif
Nick Piggin4866cde2005-06-25 14:57:23 -0700885#ifndef finish_arch_switch
886# define finish_arch_switch(prev) do { } while (0)
887#endif
888
Dmitry Adamushko051a1d12007-12-18 15:21:13 +0100889static inline int task_current(struct rq *rq, struct task_struct *p)
890{
891 return rq->curr == p;
892}
893
Nick Piggin4866cde2005-06-25 14:57:23 -0700894#ifndef __ARCH_WANT_UNLOCKED_CTXSW
Ingo Molnar70b97a72006-07-03 00:25:42 -0700895static inline int task_running(struct rq *rq, struct task_struct *p)
Nick Piggin4866cde2005-06-25 14:57:23 -0700896{
Dmitry Adamushko051a1d12007-12-18 15:21:13 +0100897 return task_current(rq, p);
Nick Piggin4866cde2005-06-25 14:57:23 -0700898}
899
Ingo Molnar70b97a72006-07-03 00:25:42 -0700900static inline void prepare_lock_switch(struct rq *rq, struct task_struct *next)
Nick Piggin4866cde2005-06-25 14:57:23 -0700901{
902}
903
Ingo Molnar70b97a72006-07-03 00:25:42 -0700904static inline void finish_lock_switch(struct rq *rq, struct task_struct *prev)
Nick Piggin4866cde2005-06-25 14:57:23 -0700905{
Ingo Molnarda04c032005-09-13 11:17:59 +0200906#ifdef CONFIG_DEBUG_SPINLOCK
907 /* this is a valid case when another task releases the spinlock */
908 rq->lock.owner = current;
909#endif
Ingo Molnar8a25d5d2006-07-03 00:24:54 -0700910 /*
911 * If we are tracking spinlock dependencies then we have to
912 * fix up the runqueue lock - which gets 'carried over' from
913 * prev into current:
914 */
915 spin_acquire(&rq->lock.dep_map, 0, 0, _THIS_IP_);
916
Nick Piggin4866cde2005-06-25 14:57:23 -0700917 spin_unlock_irq(&rq->lock);
918}
919
920#else /* __ARCH_WANT_UNLOCKED_CTXSW */
Ingo Molnar70b97a72006-07-03 00:25:42 -0700921static inline int task_running(struct rq *rq, struct task_struct *p)
Nick Piggin4866cde2005-06-25 14:57:23 -0700922{
923#ifdef CONFIG_SMP
924 return p->oncpu;
925#else
Dmitry Adamushko051a1d12007-12-18 15:21:13 +0100926 return task_current(rq, p);
Nick Piggin4866cde2005-06-25 14:57:23 -0700927#endif
928}
929
Ingo Molnar70b97a72006-07-03 00:25:42 -0700930static inline void prepare_lock_switch(struct rq *rq, struct task_struct *next)
Nick Piggin4866cde2005-06-25 14:57:23 -0700931{
932#ifdef CONFIG_SMP
933 /*
934 * We can optimise this out completely for !SMP, because the
935 * SMP rebalancing from interrupt is the only thing that cares
936 * here.
937 */
938 next->oncpu = 1;
939#endif
940#ifdef __ARCH_WANT_INTERRUPTS_ON_CTXSW
941 spin_unlock_irq(&rq->lock);
942#else
943 spin_unlock(&rq->lock);
944#endif
945}
946
Ingo Molnar70b97a72006-07-03 00:25:42 -0700947static inline void finish_lock_switch(struct rq *rq, struct task_struct *prev)
Nick Piggin4866cde2005-06-25 14:57:23 -0700948{
949#ifdef CONFIG_SMP
950 /*
951 * After ->oncpu is cleared, the task can be moved to a different CPU.
952 * We must ensure this doesn't happen until the switch is completely
953 * finished.
954 */
955 smp_wmb();
956 prev->oncpu = 0;
957#endif
958#ifndef __ARCH_WANT_INTERRUPTS_ON_CTXSW
959 local_irq_enable();
960#endif
961}
962#endif /* __ARCH_WANT_UNLOCKED_CTXSW */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963
964/*
Ingo Molnarb29739f2006-06-27 02:54:51 -0700965 * __task_rq_lock - lock the runqueue a given task resides on.
966 * Must be called interrupts disabled.
967 */
Ingo Molnar70b97a72006-07-03 00:25:42 -0700968static inline struct rq *__task_rq_lock(struct task_struct *p)
Ingo Molnarb29739f2006-06-27 02:54:51 -0700969 __acquires(rq->lock)
970{
Andi Kleen3a5c3592007-10-15 17:00:14 +0200971 for (;;) {
972 struct rq *rq = task_rq(p);
973 spin_lock(&rq->lock);
974 if (likely(rq == task_rq(p)))
975 return rq;
Ingo Molnarb29739f2006-06-27 02:54:51 -0700976 spin_unlock(&rq->lock);
Ingo Molnarb29739f2006-06-27 02:54:51 -0700977 }
Ingo Molnarb29739f2006-06-27 02:54:51 -0700978}
979
980/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981 * task_rq_lock - lock the runqueue a given task resides on and disable
Ingo Molnar41a2d6c2007-12-05 15:46:09 +0100982 * interrupts. Note the ordering: we can safely lookup the task_rq without
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983 * explicitly disabling preemption.
984 */
Ingo Molnar70b97a72006-07-03 00:25:42 -0700985static struct rq *task_rq_lock(struct task_struct *p, unsigned long *flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986 __acquires(rq->lock)
987{
Ingo Molnar70b97a72006-07-03 00:25:42 -0700988 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989
Andi Kleen3a5c3592007-10-15 17:00:14 +0200990 for (;;) {
991 local_irq_save(*flags);
992 rq = task_rq(p);
993 spin_lock(&rq->lock);
994 if (likely(rq == task_rq(p)))
995 return rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996 spin_unlock_irqrestore(&rq->lock, *flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998}
999
Ingo Molnaraa9c4c02008-12-17 14:10:57 +01001000void curr_rq_lock_irq_save(unsigned long *flags)
1001 __acquires(rq->lock)
1002{
1003 struct rq *rq;
1004
1005 local_irq_save(*flags);
1006 rq = cpu_rq(smp_processor_id());
1007 spin_lock(&rq->lock);
1008}
1009
1010void curr_rq_unlock_irq_restore(unsigned long *flags)
1011 __releases(rq->lock)
1012{
1013 struct rq *rq;
1014
1015 rq = cpu_rq(smp_processor_id());
1016 spin_unlock(&rq->lock);
1017 local_irq_restore(*flags);
1018}
1019
Oleg Nesterovad474ca2008-11-10 15:39:30 +01001020void task_rq_unlock_wait(struct task_struct *p)
1021{
1022 struct rq *rq = task_rq(p);
1023
1024 smp_mb(); /* spin-unlock-wait is not a full memory barrier */
1025 spin_unlock_wait(&rq->lock);
1026}
1027
Alexey Dobriyana9957442007-10-15 17:00:13 +02001028static void __task_rq_unlock(struct rq *rq)
Ingo Molnarb29739f2006-06-27 02:54:51 -07001029 __releases(rq->lock)
1030{
1031 spin_unlock(&rq->lock);
1032}
1033
Ingo Molnar70b97a72006-07-03 00:25:42 -07001034static inline void task_rq_unlock(struct rq *rq, unsigned long *flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035 __releases(rq->lock)
1036{
1037 spin_unlock_irqrestore(&rq->lock, *flags);
1038}
1039
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040/*
Robert P. J. Daycc2a73b2006-12-10 02:20:00 -08001041 * this_rq_lock - lock this runqueue and disable interrupts.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042 */
Alexey Dobriyana9957442007-10-15 17:00:13 +02001043static struct rq *this_rq_lock(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044 __acquires(rq->lock)
1045{
Ingo Molnar70b97a72006-07-03 00:25:42 -07001046 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047
1048 local_irq_disable();
1049 rq = this_rq();
1050 spin_lock(&rq->lock);
1051
1052 return rq;
1053}
1054
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01001055#ifdef CONFIG_SCHED_HRTICK
1056/*
1057 * Use HR-timers to deliver accurate preemption points.
1058 *
1059 * Its all a bit involved since we cannot program an hrt while holding the
1060 * rq->lock. So what we do is store a state in in rq->hrtick_* and ask for a
1061 * reschedule event.
1062 *
1063 * When we get rescheduled we reprogram the hrtick_timer outside of the
1064 * rq->lock.
1065 */
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01001066
1067/*
1068 * Use hrtick when:
1069 * - enabled by features
1070 * - hrtimer is actually high res
1071 */
1072static inline int hrtick_enabled(struct rq *rq)
1073{
1074 if (!sched_feat(HRTICK))
1075 return 0;
Ingo Molnarba420592008-07-20 11:02:06 +02001076 if (!cpu_active(cpu_of(rq)))
Peter Zijlstrab328ca12008-04-29 10:02:46 +02001077 return 0;
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01001078 return hrtimer_is_hres_active(&rq->hrtick_timer);
1079}
1080
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01001081static void hrtick_clear(struct rq *rq)
1082{
1083 if (hrtimer_active(&rq->hrtick_timer))
1084 hrtimer_cancel(&rq->hrtick_timer);
1085}
1086
1087/*
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01001088 * High-resolution timer tick.
1089 * Runs from hardirq context with interrupts disabled.
1090 */
1091static enum hrtimer_restart hrtick(struct hrtimer *timer)
1092{
1093 struct rq *rq = container_of(timer, struct rq, hrtick_timer);
1094
1095 WARN_ON_ONCE(cpu_of(rq) != smp_processor_id());
1096
1097 spin_lock(&rq->lock);
Peter Zijlstra3e51f332008-05-03 18:29:28 +02001098 update_rq_clock(rq);
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01001099 rq->curr->sched_class->task_tick(rq, rq->curr, 1);
1100 spin_unlock(&rq->lock);
1101
1102 return HRTIMER_NORESTART;
1103}
1104
Rabin Vincent95e904c2008-05-11 05:55:33 +05301105#ifdef CONFIG_SMP
Peter Zijlstra31656512008-07-18 18:01:23 +02001106/*
1107 * called from hardirq (IPI) context
1108 */
1109static void __hrtick_start(void *arg)
Peter Zijlstrab328ca12008-04-29 10:02:46 +02001110{
Peter Zijlstra31656512008-07-18 18:01:23 +02001111 struct rq *rq = arg;
Peter Zijlstrab328ca12008-04-29 10:02:46 +02001112
Peter Zijlstra31656512008-07-18 18:01:23 +02001113 spin_lock(&rq->lock);
1114 hrtimer_restart(&rq->hrtick_timer);
1115 rq->hrtick_csd_pending = 0;
1116 spin_unlock(&rq->lock);
Peter Zijlstrab328ca12008-04-29 10:02:46 +02001117}
1118
Peter Zijlstra31656512008-07-18 18:01:23 +02001119/*
1120 * Called to set the hrtick timer state.
1121 *
1122 * called with rq->lock held and irqs disabled
1123 */
1124static void hrtick_start(struct rq *rq, u64 delay)
Peter Zijlstrab328ca12008-04-29 10:02:46 +02001125{
Peter Zijlstra31656512008-07-18 18:01:23 +02001126 struct hrtimer *timer = &rq->hrtick_timer;
1127 ktime_t time = ktime_add_ns(timer->base->get_time(), delay);
Peter Zijlstrab328ca12008-04-29 10:02:46 +02001128
Arjan van de Vencc584b22008-09-01 15:02:30 -07001129 hrtimer_set_expires(timer, time);
Peter Zijlstra31656512008-07-18 18:01:23 +02001130
1131 if (rq == this_rq()) {
1132 hrtimer_restart(timer);
1133 } else if (!rq->hrtick_csd_pending) {
Peter Zijlstra6e275632009-02-25 13:59:48 +01001134 __smp_call_function_single(cpu_of(rq), &rq->hrtick_csd, 0);
Peter Zijlstra31656512008-07-18 18:01:23 +02001135 rq->hrtick_csd_pending = 1;
1136 }
Peter Zijlstrab328ca12008-04-29 10:02:46 +02001137}
1138
1139static int
1140hotplug_hrtick(struct notifier_block *nfb, unsigned long action, void *hcpu)
1141{
1142 int cpu = (int)(long)hcpu;
1143
1144 switch (action) {
1145 case CPU_UP_CANCELED:
1146 case CPU_UP_CANCELED_FROZEN:
1147 case CPU_DOWN_PREPARE:
1148 case CPU_DOWN_PREPARE_FROZEN:
1149 case CPU_DEAD:
1150 case CPU_DEAD_FROZEN:
Peter Zijlstra31656512008-07-18 18:01:23 +02001151 hrtick_clear(cpu_rq(cpu));
Peter Zijlstrab328ca12008-04-29 10:02:46 +02001152 return NOTIFY_OK;
1153 }
1154
1155 return NOTIFY_DONE;
1156}
1157
Rakib Mullickfa748202008-09-22 14:55:45 -07001158static __init void init_hrtick(void)
Peter Zijlstrab328ca12008-04-29 10:02:46 +02001159{
1160 hotcpu_notifier(hotplug_hrtick, 0);
1161}
Peter Zijlstra31656512008-07-18 18:01:23 +02001162#else
1163/*
1164 * Called to set the hrtick timer state.
1165 *
1166 * called with rq->lock held and irqs disabled
1167 */
1168static void hrtick_start(struct rq *rq, u64 delay)
1169{
1170 hrtimer_start(&rq->hrtick_timer, ns_to_ktime(delay), HRTIMER_MODE_REL);
1171}
1172
Andrew Morton006c75f2008-09-22 14:55:46 -07001173static inline void init_hrtick(void)
Peter Zijlstra31656512008-07-18 18:01:23 +02001174{
1175}
Rabin Vincent95e904c2008-05-11 05:55:33 +05301176#endif /* CONFIG_SMP */
Peter Zijlstrab328ca12008-04-29 10:02:46 +02001177
1178static void init_rq_hrtick(struct rq *rq)
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01001179{
Peter Zijlstra31656512008-07-18 18:01:23 +02001180#ifdef CONFIG_SMP
1181 rq->hrtick_csd_pending = 0;
1182
1183 rq->hrtick_csd.flags = 0;
1184 rq->hrtick_csd.func = __hrtick_start;
1185 rq->hrtick_csd.info = rq;
1186#endif
1187
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01001188 hrtimer_init(&rq->hrtick_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
1189 rq->hrtick_timer.function = hrtick;
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01001190}
Andrew Morton006c75f2008-09-22 14:55:46 -07001191#else /* CONFIG_SCHED_HRTICK */
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01001192static inline void hrtick_clear(struct rq *rq)
1193{
1194}
1195
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01001196static inline void init_rq_hrtick(struct rq *rq)
1197{
1198}
1199
Peter Zijlstrab328ca12008-04-29 10:02:46 +02001200static inline void init_hrtick(void)
1201{
1202}
Andrew Morton006c75f2008-09-22 14:55:46 -07001203#endif /* CONFIG_SCHED_HRTICK */
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01001204
Ingo Molnar1b9f19c2007-07-09 18:51:59 +02001205/*
Ingo Molnarc24d20d2007-07-09 18:51:59 +02001206 * resched_task - mark a task 'to be rescheduled now'.
1207 *
1208 * On UP this means the setting of the need_resched flag, on SMP it
1209 * might also involve a cross-CPU call to trigger the scheduler on
1210 * the target CPU.
1211 */
1212#ifdef CONFIG_SMP
1213
1214#ifndef tsk_is_polling
1215#define tsk_is_polling(t) test_tsk_thread_flag(t, TIF_POLLING_NRFLAG)
1216#endif
1217
Peter Zijlstra31656512008-07-18 18:01:23 +02001218static void resched_task(struct task_struct *p)
Ingo Molnarc24d20d2007-07-09 18:51:59 +02001219{
1220 int cpu;
1221
1222 assert_spin_locked(&task_rq(p)->lock);
1223
Lai Jiangshan5ed0cec2009-03-06 19:40:20 +08001224 if (test_tsk_need_resched(p))
Ingo Molnarc24d20d2007-07-09 18:51:59 +02001225 return;
1226
Lai Jiangshan5ed0cec2009-03-06 19:40:20 +08001227 set_tsk_need_resched(p);
Ingo Molnarc24d20d2007-07-09 18:51:59 +02001228
1229 cpu = task_cpu(p);
1230 if (cpu == smp_processor_id())
1231 return;
1232
1233 /* NEED_RESCHED must be visible before we test polling */
1234 smp_mb();
1235 if (!tsk_is_polling(p))
1236 smp_send_reschedule(cpu);
1237}
1238
1239static void resched_cpu(int cpu)
1240{
1241 struct rq *rq = cpu_rq(cpu);
1242 unsigned long flags;
1243
1244 if (!spin_trylock_irqsave(&rq->lock, flags))
1245 return;
1246 resched_task(cpu_curr(cpu));
1247 spin_unlock_irqrestore(&rq->lock, flags);
1248}
Thomas Gleixner06d83082008-03-22 09:20:24 +01001249
1250#ifdef CONFIG_NO_HZ
1251/*
1252 * When add_timer_on() enqueues a timer into the timer wheel of an
1253 * idle CPU then this timer might expire before the next timer event
1254 * which is scheduled to wake up that CPU. In case of a completely
1255 * idle system the next event might even be infinite time into the
1256 * future. wake_up_idle_cpu() ensures that the CPU is woken up and
1257 * leaves the inner idle loop so the newly added timer is taken into
1258 * account when the CPU goes back to idle and evaluates the timer
1259 * wheel for the next timer event.
1260 */
1261void wake_up_idle_cpu(int cpu)
1262{
1263 struct rq *rq = cpu_rq(cpu);
1264
1265 if (cpu == smp_processor_id())
1266 return;
1267
1268 /*
1269 * This is safe, as this function is called with the timer
1270 * wheel base lock of (cpu) held. When the CPU is on the way
1271 * to idle and has not yet set rq->curr to idle then it will
1272 * be serialized on the timer wheel base lock and take the new
1273 * timer into account automatically.
1274 */
1275 if (rq->curr != rq->idle)
1276 return;
1277
1278 /*
1279 * We can set TIF_RESCHED on the idle task of the other CPU
1280 * lockless. The worst case is that the other CPU runs the
1281 * idle task through an additional NOOP schedule()
1282 */
Lai Jiangshan5ed0cec2009-03-06 19:40:20 +08001283 set_tsk_need_resched(rq->idle);
Thomas Gleixner06d83082008-03-22 09:20:24 +01001284
1285 /* NEED_RESCHED must be visible before we test polling */
1286 smp_mb();
1287 if (!tsk_is_polling(rq->idle))
1288 smp_send_reschedule(cpu);
1289}
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +02001290#endif /* CONFIG_NO_HZ */
Thomas Gleixner06d83082008-03-22 09:20:24 +01001291
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +02001292#else /* !CONFIG_SMP */
Peter Zijlstra31656512008-07-18 18:01:23 +02001293static void resched_task(struct task_struct *p)
Ingo Molnarc24d20d2007-07-09 18:51:59 +02001294{
1295 assert_spin_locked(&task_rq(p)->lock);
Peter Zijlstra31656512008-07-18 18:01:23 +02001296 set_tsk_need_resched(p);
Ingo Molnarc24d20d2007-07-09 18:51:59 +02001297}
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +02001298#endif /* CONFIG_SMP */
Ingo Molnarc24d20d2007-07-09 18:51:59 +02001299
Ingo Molnar45bf76d2007-07-09 18:51:59 +02001300#if BITS_PER_LONG == 32
1301# define WMULT_CONST (~0UL)
1302#else
1303# define WMULT_CONST (1UL << 32)
1304#endif
1305
1306#define WMULT_SHIFT 32
1307
Ingo Molnar194081e2007-08-09 11:16:51 +02001308/*
1309 * Shift right and round:
1310 */
Ingo Molnarcf2ab462007-09-05 14:32:49 +02001311#define SRR(x, y) (((x) + (1UL << ((y) - 1))) >> (y))
Ingo Molnar194081e2007-08-09 11:16:51 +02001312
Peter Zijlstraa7be37a2008-06-27 13:41:11 +02001313/*
1314 * delta *= weight / lw
1315 */
Ingo Molnarcb1c4fc2007-08-02 17:41:40 +02001316static unsigned long
Ingo Molnar45bf76d2007-07-09 18:51:59 +02001317calc_delta_mine(unsigned long delta_exec, unsigned long weight,
1318 struct load_weight *lw)
1319{
1320 u64 tmp;
1321
Lai Jiangshan7a232e02008-06-12 16:43:07 +08001322 if (!lw->inv_weight) {
1323 if (BITS_PER_LONG > 32 && unlikely(lw->weight >= WMULT_CONST))
1324 lw->inv_weight = 1;
1325 else
1326 lw->inv_weight = 1 + (WMULT_CONST-lw->weight/2)
1327 / (lw->weight+1);
1328 }
Ingo Molnar45bf76d2007-07-09 18:51:59 +02001329
1330 tmp = (u64)delta_exec * weight;
1331 /*
1332 * Check whether we'd overflow the 64-bit multiplication:
1333 */
Ingo Molnar194081e2007-08-09 11:16:51 +02001334 if (unlikely(tmp > WMULT_CONST))
Ingo Molnarcf2ab462007-09-05 14:32:49 +02001335 tmp = SRR(SRR(tmp, WMULT_SHIFT/2) * lw->inv_weight,
Ingo Molnar194081e2007-08-09 11:16:51 +02001336 WMULT_SHIFT/2);
1337 else
Ingo Molnarcf2ab462007-09-05 14:32:49 +02001338 tmp = SRR(tmp * lw->inv_weight, WMULT_SHIFT);
Ingo Molnar45bf76d2007-07-09 18:51:59 +02001339
Ingo Molnarecf691d2007-08-02 17:41:40 +02001340 return (unsigned long)min(tmp, (u64)(unsigned long)LONG_MAX);
Ingo Molnar45bf76d2007-07-09 18:51:59 +02001341}
1342
Ingo Molnar10919852007-10-15 17:00:04 +02001343static inline void update_load_add(struct load_weight *lw, unsigned long inc)
Ingo Molnar45bf76d2007-07-09 18:51:59 +02001344{
1345 lw->weight += inc;
Ingo Molnare89996a2008-03-14 23:48:28 +01001346 lw->inv_weight = 0;
Ingo Molnar45bf76d2007-07-09 18:51:59 +02001347}
1348
Ingo Molnar10919852007-10-15 17:00:04 +02001349static inline void update_load_sub(struct load_weight *lw, unsigned long dec)
Ingo Molnar45bf76d2007-07-09 18:51:59 +02001350{
1351 lw->weight -= dec;
Ingo Molnare89996a2008-03-14 23:48:28 +01001352 lw->inv_weight = 0;
Ingo Molnar45bf76d2007-07-09 18:51:59 +02001353}
1354
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355/*
Peter Williams2dd73a42006-06-27 02:54:34 -07001356 * To aid in avoiding the subversion of "niceness" due to uneven distribution
1357 * of tasks with abnormal "nice" values across CPUs the contribution that
1358 * each task makes to its run queue's load is weighted according to its
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01001359 * scheduling class and "nice" value. For SCHED_NORMAL tasks this is just a
Peter Williams2dd73a42006-06-27 02:54:34 -07001360 * scaled version of the new time slice allocation that they receive on time
1361 * slice expiry etc.
1362 */
1363
Peter Zijlstracce7ade2009-01-15 14:53:37 +01001364#define WEIGHT_IDLEPRIO 3
1365#define WMULT_IDLEPRIO 1431655765
Ingo Molnardd41f592007-07-09 18:51:59 +02001366
1367/*
1368 * Nice levels are multiplicative, with a gentle 10% change for every
1369 * nice level changed. I.e. when a CPU-bound task goes from nice 0 to
1370 * nice 1, it will get ~10% less CPU time than another CPU-bound task
1371 * that remained on nice 0.
1372 *
1373 * The "10% effect" is relative and cumulative: from _any_ nice level,
1374 * if you go up 1 level, it's -10% CPU usage, if you go down 1 level
Ingo Molnarf9153ee2007-07-16 09:46:30 +02001375 * it's +10% CPU usage. (to achieve that we use a multiplier of 1.25.
1376 * If a task goes up by ~10% and another task goes down by ~10% then
1377 * the relative distance between them is ~25%.)
Ingo Molnardd41f592007-07-09 18:51:59 +02001378 */
1379static const int prio_to_weight[40] = {
Ingo Molnar254753d2007-08-09 11:16:51 +02001380 /* -20 */ 88761, 71755, 56483, 46273, 36291,
1381 /* -15 */ 29154, 23254, 18705, 14949, 11916,
1382 /* -10 */ 9548, 7620, 6100, 4904, 3906,
1383 /* -5 */ 3121, 2501, 1991, 1586, 1277,
1384 /* 0 */ 1024, 820, 655, 526, 423,
1385 /* 5 */ 335, 272, 215, 172, 137,
1386 /* 10 */ 110, 87, 70, 56, 45,
1387 /* 15 */ 36, 29, 23, 18, 15,
Ingo Molnardd41f592007-07-09 18:51:59 +02001388};
1389
Ingo Molnar5714d2d2007-07-16 09:46:31 +02001390/*
1391 * Inverse (2^32/x) values of the prio_to_weight[] array, precalculated.
1392 *
1393 * In cases where the weight does not change often, we can use the
1394 * precalculated inverse to speed up arithmetics by turning divisions
1395 * into multiplications:
1396 */
Ingo Molnardd41f592007-07-09 18:51:59 +02001397static const u32 prio_to_wmult[40] = {
Ingo Molnar254753d2007-08-09 11:16:51 +02001398 /* -20 */ 48388, 59856, 76040, 92818, 118348,
1399 /* -15 */ 147320, 184698, 229616, 287308, 360437,
1400 /* -10 */ 449829, 563644, 704093, 875809, 1099582,
1401 /* -5 */ 1376151, 1717300, 2157191, 2708050, 3363326,
1402 /* 0 */ 4194304, 5237765, 6557202, 8165337, 10153587,
1403 /* 5 */ 12820798, 15790321, 19976592, 24970740, 31350126,
1404 /* 10 */ 39045157, 49367440, 61356676, 76695844, 95443717,
1405 /* 15 */ 119304647, 148102320, 186737708, 238609294, 286331153,
Ingo Molnardd41f592007-07-09 18:51:59 +02001406};
Peter Williams2dd73a42006-06-27 02:54:34 -07001407
Ingo Molnardd41f592007-07-09 18:51:59 +02001408static void activate_task(struct rq *rq, struct task_struct *p, int wakeup);
1409
1410/*
1411 * runqueue iterator, to support SMP load-balancing between different
1412 * scheduling classes, without having to expose their internal data
1413 * structures to the load-balancing proper:
1414 */
1415struct rq_iterator {
1416 void *arg;
1417 struct task_struct *(*start)(void *);
1418 struct task_struct *(*next)(void *);
1419};
1420
Peter Williamse1d14842007-10-24 18:23:51 +02001421#ifdef CONFIG_SMP
1422static unsigned long
1423balance_tasks(struct rq *this_rq, int this_cpu, struct rq *busiest,
1424 unsigned long max_load_move, struct sched_domain *sd,
1425 enum cpu_idle_type idle, int *all_pinned,
1426 int *this_best_prio, struct rq_iterator *iterator);
1427
1428static int
1429iter_move_one_task(struct rq *this_rq, int this_cpu, struct rq *busiest,
1430 struct sched_domain *sd, enum cpu_idle_type idle,
1431 struct rq_iterator *iterator);
Peter Williamse1d14842007-10-24 18:23:51 +02001432#endif
Ingo Molnardd41f592007-07-09 18:51:59 +02001433
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01001434#ifdef CONFIG_CGROUP_CPUACCT
1435static void cpuacct_charge(struct task_struct *tsk, u64 cputime);
1436#else
1437static inline void cpuacct_charge(struct task_struct *tsk, u64 cputime) {}
1438#endif
1439
Peter Zijlstra18d95a22008-04-19 19:45:00 +02001440static inline void inc_cpu_load(struct rq *rq, unsigned long load)
1441{
1442 update_load_add(&rq->load, load);
1443}
1444
1445static inline void dec_cpu_load(struct rq *rq, unsigned long load)
1446{
1447 update_load_sub(&rq->load, load);
1448}
1449
Ingo Molnar7940ca32008-08-19 13:40:47 +02001450#if (defined(CONFIG_SMP) && defined(CONFIG_FAIR_GROUP_SCHED)) || defined(CONFIG_RT_GROUP_SCHED)
Peter Zijlstraeb755802008-08-19 12:33:05 +02001451typedef int (*tg_visitor)(struct task_group *, void *);
1452
1453/*
1454 * Iterate the full tree, calling @down when first entering a node and @up when
1455 * leaving it for the final time.
1456 */
1457static int walk_tg_tree(tg_visitor down, tg_visitor up, void *data)
1458{
1459 struct task_group *parent, *child;
1460 int ret;
1461
1462 rcu_read_lock();
1463 parent = &root_task_group;
1464down:
1465 ret = (*down)(parent, data);
1466 if (ret)
1467 goto out_unlock;
1468 list_for_each_entry_rcu(child, &parent->children, siblings) {
1469 parent = child;
1470 goto down;
1471
1472up:
1473 continue;
1474 }
1475 ret = (*up)(parent, data);
1476 if (ret)
1477 goto out_unlock;
1478
1479 child = parent;
1480 parent = parent->parent;
1481 if (parent)
1482 goto up;
1483out_unlock:
1484 rcu_read_unlock();
1485
1486 return ret;
1487}
1488
1489static int tg_nop(struct task_group *tg, void *data)
1490{
1491 return 0;
1492}
1493#endif
1494
Gregory Haskinse7693a32008-01-25 21:08:09 +01001495#ifdef CONFIG_SMP
1496static unsigned long source_load(int cpu, int type);
1497static unsigned long target_load(int cpu, int type);
Gregory Haskinse7693a32008-01-25 21:08:09 +01001498static int task_hot(struct task_struct *p, u64 now, struct sched_domain *sd);
Peter Zijlstrac09595f2008-06-27 13:41:14 +02001499
Peter Zijlstraa8a51d52008-06-27 13:41:26 +02001500static unsigned long cpu_avg_load_per_task(int cpu)
1501{
1502 struct rq *rq = cpu_rq(cpu);
Ingo Molnaraf6d5962008-11-29 20:45:15 +01001503 unsigned long nr_running = ACCESS_ONCE(rq->nr_running);
Peter Zijlstraa8a51d52008-06-27 13:41:26 +02001504
Steven Rostedt4cd42622008-11-26 21:04:24 -05001505 if (nr_running)
1506 rq->avg_load_per_task = rq->load.weight / nr_running;
Balbir Singha2d47772008-11-12 16:19:00 +05301507 else
1508 rq->avg_load_per_task = 0;
Peter Zijlstraa8a51d52008-06-27 13:41:26 +02001509
1510 return rq->avg_load_per_task;
1511}
1512
Peter Zijlstrac09595f2008-06-27 13:41:14 +02001513#ifdef CONFIG_FAIR_GROUP_SCHED
1514
Peter Zijlstrac09595f2008-06-27 13:41:14 +02001515static void __set_se_shares(struct sched_entity *se, unsigned long shares);
1516
1517/*
1518 * Calculate and set the cpu's group shares.
1519 */
1520static void
Peter Zijlstraffda12a2008-10-17 19:27:02 +02001521update_group_shares_cpu(struct task_group *tg, int cpu,
1522 unsigned long sd_shares, unsigned long sd_rq_weight)
Peter Zijlstrac09595f2008-06-27 13:41:14 +02001523{
Peter Zijlstrac09595f2008-06-27 13:41:14 +02001524 unsigned long shares;
1525 unsigned long rq_weight;
1526
Peter Zijlstrac8cba852008-06-27 13:41:23 +02001527 if (!tg->se[cpu])
Peter Zijlstrac09595f2008-06-27 13:41:14 +02001528 return;
1529
Ken Chenec4e0e22008-11-18 22:41:57 -08001530 rq_weight = tg->cfs_rq[cpu]->rq_weight;
Peter Zijlstrac8cba852008-06-27 13:41:23 +02001531
Peter Zijlstrac09595f2008-06-27 13:41:14 +02001532 /*
1533 * \Sum shares * rq_weight
1534 * shares = -----------------------
1535 * \Sum rq_weight
1536 *
1537 */
Ken Chenec4e0e22008-11-18 22:41:57 -08001538 shares = (sd_shares * rq_weight) / sd_rq_weight;
Peter Zijlstraffda12a2008-10-17 19:27:02 +02001539 shares = clamp_t(unsigned long, shares, MIN_SHARES, MAX_SHARES);
Peter Zijlstrac09595f2008-06-27 13:41:14 +02001540
Peter Zijlstraffda12a2008-10-17 19:27:02 +02001541 if (abs(shares - tg->se[cpu]->load.weight) >
1542 sysctl_sched_shares_thresh) {
1543 struct rq *rq = cpu_rq(cpu);
1544 unsigned long flags;
Peter Zijlstrac09595f2008-06-27 13:41:14 +02001545
Peter Zijlstraffda12a2008-10-17 19:27:02 +02001546 spin_lock_irqsave(&rq->lock, flags);
Ken Chenec4e0e22008-11-18 22:41:57 -08001547 tg->cfs_rq[cpu]->shares = shares;
Peter Zijlstrac09595f2008-06-27 13:41:14 +02001548
Peter Zijlstraffda12a2008-10-17 19:27:02 +02001549 __set_se_shares(tg->se[cpu], shares);
1550 spin_unlock_irqrestore(&rq->lock, flags);
1551 }
Peter Zijlstrac09595f2008-06-27 13:41:14 +02001552}
1553
1554/*
Peter Zijlstrac8cba852008-06-27 13:41:23 +02001555 * Re-compute the task group their per cpu shares over the given domain.
1556 * This needs to be done in a bottom-up fashion because the rq weight of a
1557 * parent group depends on the shares of its child groups.
Peter Zijlstrac09595f2008-06-27 13:41:14 +02001558 */
Peter Zijlstraeb755802008-08-19 12:33:05 +02001559static int tg_shares_up(struct task_group *tg, void *data)
Peter Zijlstrac09595f2008-06-27 13:41:14 +02001560{
Ken Chenec4e0e22008-11-18 22:41:57 -08001561 unsigned long weight, rq_weight = 0;
Peter Zijlstrac8cba852008-06-27 13:41:23 +02001562 unsigned long shares = 0;
Peter Zijlstraeb755802008-08-19 12:33:05 +02001563 struct sched_domain *sd = data;
Peter Zijlstrac09595f2008-06-27 13:41:14 +02001564 int i;
1565
Rusty Russell758b2cd2008-11-25 02:35:04 +10301566 for_each_cpu(i, sched_domain_span(sd)) {
Ken Chenec4e0e22008-11-18 22:41:57 -08001567 /*
1568 * If there are currently no tasks on the cpu pretend there
1569 * is one of average load so that when a new task gets to
1570 * run here it will not get delayed by group starvation.
1571 */
1572 weight = tg->cfs_rq[i]->load.weight;
1573 if (!weight)
1574 weight = NICE_0_LOAD;
1575
1576 tg->cfs_rq[i]->rq_weight = weight;
1577 rq_weight += weight;
Peter Zijlstrac8cba852008-06-27 13:41:23 +02001578 shares += tg->cfs_rq[i]->shares;
1579 }
1580
1581 if ((!shares && rq_weight) || shares > tg->shares)
1582 shares = tg->shares;
1583
1584 if (!sd->parent || !(sd->parent->flags & SD_LOAD_BALANCE))
1585 shares = tg->shares;
1586
Rusty Russell758b2cd2008-11-25 02:35:04 +10301587 for_each_cpu(i, sched_domain_span(sd))
Peter Zijlstraffda12a2008-10-17 19:27:02 +02001588 update_group_shares_cpu(tg, i, shares, rq_weight);
Peter Zijlstraeb755802008-08-19 12:33:05 +02001589
1590 return 0;
Peter Zijlstrac09595f2008-06-27 13:41:14 +02001591}
1592
1593/*
Peter Zijlstrac8cba852008-06-27 13:41:23 +02001594 * Compute the cpu's hierarchical load factor for each task group.
1595 * This needs to be done in a top-down fashion because the load of a child
1596 * group is a fraction of its parents load.
Peter Zijlstrac09595f2008-06-27 13:41:14 +02001597 */
Peter Zijlstraeb755802008-08-19 12:33:05 +02001598static int tg_load_down(struct task_group *tg, void *data)
Peter Zijlstrac09595f2008-06-27 13:41:14 +02001599{
Peter Zijlstrac8cba852008-06-27 13:41:23 +02001600 unsigned long load;
Peter Zijlstraeb755802008-08-19 12:33:05 +02001601 long cpu = (long)data;
Peter Zijlstrac09595f2008-06-27 13:41:14 +02001602
Peter Zijlstrac8cba852008-06-27 13:41:23 +02001603 if (!tg->parent) {
1604 load = cpu_rq(cpu)->load.weight;
1605 } else {
1606 load = tg->parent->cfs_rq[cpu]->h_load;
1607 load *= tg->cfs_rq[cpu]->shares;
1608 load /= tg->parent->cfs_rq[cpu]->load.weight + 1;
1609 }
1610
1611 tg->cfs_rq[cpu]->h_load = load;
Peter Zijlstrac09595f2008-06-27 13:41:14 +02001612
Peter Zijlstraeb755802008-08-19 12:33:05 +02001613 return 0;
Peter Zijlstra4d8d5952008-06-27 13:41:19 +02001614}
1615
Peter Zijlstrac8cba852008-06-27 13:41:23 +02001616static void update_shares(struct sched_domain *sd)
Peter Zijlstrac09595f2008-06-27 13:41:14 +02001617{
Peter Zijlstra2398f2c2008-06-27 13:41:35 +02001618 u64 now = cpu_clock(raw_smp_processor_id());
1619 s64 elapsed = now - sd->last_update;
1620
1621 if (elapsed >= (s64)(u64)sysctl_sched_shares_ratelimit) {
1622 sd->last_update = now;
Peter Zijlstraeb755802008-08-19 12:33:05 +02001623 walk_tg_tree(tg_nop, tg_shares_up, sd);
Peter Zijlstra2398f2c2008-06-27 13:41:35 +02001624 }
Peter Zijlstrac09595f2008-06-27 13:41:14 +02001625}
1626
Peter Zijlstra3e5459b2008-06-27 13:41:24 +02001627static void update_shares_locked(struct rq *rq, struct sched_domain *sd)
1628{
1629 spin_unlock(&rq->lock);
1630 update_shares(sd);
1631 spin_lock(&rq->lock);
1632}
1633
Peter Zijlstraeb755802008-08-19 12:33:05 +02001634static void update_h_load(long cpu)
Peter Zijlstrac09595f2008-06-27 13:41:14 +02001635{
Peter Zijlstraeb755802008-08-19 12:33:05 +02001636 walk_tg_tree(tg_load_down, tg_nop, (void *)cpu);
Peter Zijlstrac09595f2008-06-27 13:41:14 +02001637}
1638
Peter Zijlstrac09595f2008-06-27 13:41:14 +02001639#else
1640
Peter Zijlstrac8cba852008-06-27 13:41:23 +02001641static inline void update_shares(struct sched_domain *sd)
Peter Zijlstrac09595f2008-06-27 13:41:14 +02001642{
1643}
1644
Peter Zijlstra3e5459b2008-06-27 13:41:24 +02001645static inline void update_shares_locked(struct rq *rq, struct sched_domain *sd)
1646{
1647}
1648
Peter Zijlstrac09595f2008-06-27 13:41:14 +02001649#endif
1650
Gregory Haskins8f45e2b2008-12-29 09:39:51 -05001651#ifdef CONFIG_PREEMPT
1652
Alexey Dobriyan70574a92008-11-28 22:08:00 +03001653/*
Gregory Haskins8f45e2b2008-12-29 09:39:51 -05001654 * fair double_lock_balance: Safely acquires both rq->locks in a fair
1655 * way at the expense of forcing extra atomic operations in all
1656 * invocations. This assures that the double_lock is acquired using the
1657 * same underlying policy as the spinlock_t on this architecture, which
1658 * reduces latency compared to the unfair variant below. However, it
1659 * also adds more overhead and therefore may reduce throughput.
Alexey Dobriyan70574a92008-11-28 22:08:00 +03001660 */
Gregory Haskins8f45e2b2008-12-29 09:39:51 -05001661static inline int _double_lock_balance(struct rq *this_rq, struct rq *busiest)
1662 __releases(this_rq->lock)
1663 __acquires(busiest->lock)
1664 __acquires(this_rq->lock)
1665{
1666 spin_unlock(&this_rq->lock);
1667 double_rq_lock(this_rq, busiest);
1668
1669 return 1;
1670}
1671
1672#else
1673/*
1674 * Unfair double_lock_balance: Optimizes throughput at the expense of
1675 * latency by eliminating extra atomic operations when the locks are
1676 * already in proper order on entry. This favors lower cpu-ids and will
1677 * grant the double lock to lower cpus over higher ids under contention,
1678 * regardless of entry order into the function.
1679 */
1680static int _double_lock_balance(struct rq *this_rq, struct rq *busiest)
Alexey Dobriyan70574a92008-11-28 22:08:00 +03001681 __releases(this_rq->lock)
1682 __acquires(busiest->lock)
1683 __acquires(this_rq->lock)
1684{
1685 int ret = 0;
1686
Alexey Dobriyan70574a92008-11-28 22:08:00 +03001687 if (unlikely(!spin_trylock(&busiest->lock))) {
1688 if (busiest < this_rq) {
1689 spin_unlock(&this_rq->lock);
1690 spin_lock(&busiest->lock);
1691 spin_lock_nested(&this_rq->lock, SINGLE_DEPTH_NESTING);
1692 ret = 1;
1693 } else
1694 spin_lock_nested(&busiest->lock, SINGLE_DEPTH_NESTING);
1695 }
1696 return ret;
1697}
1698
Gregory Haskins8f45e2b2008-12-29 09:39:51 -05001699#endif /* CONFIG_PREEMPT */
1700
1701/*
1702 * double_lock_balance - lock the busiest runqueue, this_rq is locked already.
1703 */
1704static int double_lock_balance(struct rq *this_rq, struct rq *busiest)
1705{
1706 if (unlikely(!irqs_disabled())) {
1707 /* printk() doesn't work good under rq->lock */
1708 spin_unlock(&this_rq->lock);
1709 BUG_ON(1);
1710 }
1711
1712 return _double_lock_balance(this_rq, busiest);
1713}
1714
Alexey Dobriyan70574a92008-11-28 22:08:00 +03001715static inline void double_unlock_balance(struct rq *this_rq, struct rq *busiest)
1716 __releases(busiest->lock)
1717{
1718 spin_unlock(&busiest->lock);
1719 lock_set_subclass(&this_rq->lock.dep_map, 0, _RET_IP_);
1720}
Peter Zijlstra18d95a22008-04-19 19:45:00 +02001721#endif
Peter Zijlstra18d95a22008-04-19 19:45:00 +02001722
1723#ifdef CONFIG_FAIR_GROUP_SCHED
1724static void cfs_rq_set_shares(struct cfs_rq *cfs_rq, unsigned long shares)
1725{
Vegard Nossum30432092008-06-27 21:35:50 +02001726#ifdef CONFIG_SMP
Ingo Molnar34e83e82008-06-27 15:42:36 +02001727 cfs_rq->shares = shares;
1728#endif
Peter Zijlstra18d95a22008-04-19 19:45:00 +02001729}
1730#endif
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
Peter Zijlstracbc34ed2008-12-10 08:08:22 +01001962 trace_sched_migrate_task(p, task_cpu(p), new_cpu);
1963
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 Molnar6c594c22008-12-14 12:34:15 +01001971#endif
Ingo Molnarcc367732007-10-15 17:00:18 +02001972 if (old_cpu != new_cpu) {
Ingo Molnar6c594c22008-12-14 12:34:15 +01001973 p->se.nr_migrations++;
Paul Mackerras23a185c2009-02-09 22:42:47 +11001974 new_rq->nr_migrations_in++;
Ingo Molnar6c594c22008-12-14 12:34:15 +01001975#ifdef CONFIG_SCHEDSTATS
Ingo Molnarcc367732007-10-15 17:00:18 +02001976 if (task_hot(p, old_rq->clock, NULL))
1977 schedstat_inc(p, se.nr_forced2_migrations);
Ingo Molnar6cfb0d52007-08-02 17:41:40 +02001978#endif
Ingo Molnar6c594c22008-12-14 12:34:15 +01001979 }
Srivatsa Vaddagiri2830cf82007-10-15 17:00:12 +02001980 p->se.vruntime -= old_cfsrq->min_vruntime -
1981 new_cfsrq->min_vruntime;
Ingo Molnardd41f592007-07-09 18:51:59 +02001982
1983 __set_task_cpu(p, new_cpu);
Ingo Molnarc65cc872007-07-09 18:51:58 +02001984}
1985
Ingo Molnar70b97a72006-07-03 00:25:42 -07001986struct migration_req {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001987 struct list_head list;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001988
Ingo Molnar36c8b582006-07-03 00:25:41 -07001989 struct task_struct *task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001990 int dest_cpu;
1991
Linus Torvalds1da177e2005-04-16 15:20:36 -07001992 struct completion done;
Ingo Molnar70b97a72006-07-03 00:25:42 -07001993};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001994
1995/*
1996 * The task's runqueue lock must be held.
1997 * Returns true if you have to wait for migration thread.
1998 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07001999static int
Ingo Molnar70b97a72006-07-03 00:25:42 -07002000migrate_task(struct task_struct *p, int dest_cpu, struct migration_req *req)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002001{
Ingo Molnar70b97a72006-07-03 00:25:42 -07002002 struct rq *rq = task_rq(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002003
2004 /*
2005 * If the task is not on a runqueue (and not running), then
2006 * it is sufficient to simply update the task's cpu field.
2007 */
Ingo Molnardd41f592007-07-09 18:51:59 +02002008 if (!p->se.on_rq && !task_running(rq, p)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002009 set_task_cpu(p, dest_cpu);
2010 return 0;
2011 }
2012
2013 init_completion(&req->done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002014 req->task = p;
2015 req->dest_cpu = dest_cpu;
2016 list_add(&req->list, &rq->migration_queue);
Ingo Molnar48f24c42006-07-03 00:25:40 -07002017
Linus Torvalds1da177e2005-04-16 15:20:36 -07002018 return 1;
2019}
2020
2021/*
2022 * wait_task_inactive - wait for a thread to unschedule.
2023 *
Roland McGrath85ba2d82008-07-25 19:45:58 -07002024 * If @match_state is nonzero, it's the @p->state value just checked and
2025 * not expected to change. If it changes, i.e. @p might have woken up,
2026 * then return zero. When we succeed in waiting for @p to be off its CPU,
2027 * we return a positive number (its total switch count). If a second call
2028 * a short while later returns the same number, the caller can be sure that
2029 * @p has remained unscheduled the whole time.
2030 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002031 * The caller must ensure that the task *will* unschedule sometime soon,
2032 * else this function might spin for a *long* time. This function can't
2033 * be called with interrupts off, or it may introduce deadlock with
2034 * smp_call_function() if an IPI is sent by the same process we are
2035 * waiting to become inactive.
2036 */
Roland McGrath85ba2d82008-07-25 19:45:58 -07002037unsigned long wait_task_inactive(struct task_struct *p, long match_state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002038{
2039 unsigned long flags;
Ingo Molnardd41f592007-07-09 18:51:59 +02002040 int running, on_rq;
Roland McGrath85ba2d82008-07-25 19:45:58 -07002041 unsigned long ncsw;
Ingo Molnar70b97a72006-07-03 00:25:42 -07002042 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002043
Andi Kleen3a5c3592007-10-15 17:00:14 +02002044 for (;;) {
2045 /*
2046 * We do the initial early heuristics without holding
2047 * any task-queue locks at all. We'll only try to get
2048 * the runqueue lock when things look like they will
2049 * work out!
2050 */
2051 rq = task_rq(p);
Linus Torvaldsfa490cf2007-06-18 09:34:40 -07002052
Andi Kleen3a5c3592007-10-15 17:00:14 +02002053 /*
2054 * If the task is actively running on another CPU
2055 * still, just relax and busy-wait without holding
2056 * any locks.
2057 *
2058 * NOTE! Since we don't hold any locks, it's not
2059 * even sure that "rq" stays as the right runqueue!
2060 * But we don't care, since "task_running()" will
2061 * return false if the runqueue has changed and p
2062 * is actually now running somewhere else!
2063 */
Roland McGrath85ba2d82008-07-25 19:45:58 -07002064 while (task_running(rq, p)) {
2065 if (match_state && unlikely(p->state != match_state))
2066 return 0;
Andi Kleen3a5c3592007-10-15 17:00:14 +02002067 cpu_relax();
Roland McGrath85ba2d82008-07-25 19:45:58 -07002068 }
Linus Torvaldsfa490cf2007-06-18 09:34:40 -07002069
Andi Kleen3a5c3592007-10-15 17:00:14 +02002070 /*
2071 * Ok, time to look more closely! We need the rq
2072 * lock now, to be *sure*. If we're wrong, we'll
2073 * just go back and repeat.
2074 */
2075 rq = task_rq_lock(p, &flags);
Mathieu Desnoyers0a16b602008-07-18 12:16:17 -04002076 trace_sched_wait_task(rq, p);
Andi Kleen3a5c3592007-10-15 17:00:14 +02002077 running = task_running(rq, p);
2078 on_rq = p->se.on_rq;
Roland McGrath85ba2d82008-07-25 19:45:58 -07002079 ncsw = 0;
Oleg Nesterovf31e11d2008-08-20 16:54:44 -07002080 if (!match_state || p->state == match_state)
Oleg Nesterov93dcf552008-08-20 16:54:44 -07002081 ncsw = p->nvcsw | LONG_MIN; /* sets MSB */
Andi Kleen3a5c3592007-10-15 17:00:14 +02002082 task_rq_unlock(rq, &flags);
Linus Torvaldsfa490cf2007-06-18 09:34:40 -07002083
Andi Kleen3a5c3592007-10-15 17:00:14 +02002084 /*
Roland McGrath85ba2d82008-07-25 19:45:58 -07002085 * If it changed from the expected state, bail out now.
2086 */
2087 if (unlikely(!ncsw))
2088 break;
2089
2090 /*
Andi Kleen3a5c3592007-10-15 17:00:14 +02002091 * Was it really running after all now that we
2092 * checked with the proper locks actually held?
2093 *
2094 * Oops. Go back and try again..
2095 */
2096 if (unlikely(running)) {
2097 cpu_relax();
2098 continue;
2099 }
2100
2101 /*
2102 * It's not enough that it's not actively running,
2103 * it must be off the runqueue _entirely_, and not
2104 * preempted!
2105 *
Luis Henriques80dd99b2009-03-16 19:58:09 +00002106 * So if it was still runnable (but just not actively
Andi Kleen3a5c3592007-10-15 17:00:14 +02002107 * running right now), it's preempted, and we should
2108 * yield - it could be a while.
2109 */
2110 if (unlikely(on_rq)) {
2111 schedule_timeout_uninterruptible(1);
2112 continue;
2113 }
2114
2115 /*
2116 * Ahh, all good. It wasn't running, and it wasn't
2117 * runnable, which means that it will never become
2118 * running in the future either. We're all done!
2119 */
2120 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002121 }
Roland McGrath85ba2d82008-07-25 19:45:58 -07002122
2123 return ncsw;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002124}
2125
2126/***
2127 * kick_process - kick a running thread to enter/exit the kernel
2128 * @p: the to-be-kicked thread
2129 *
2130 * Cause a process which is running on another CPU to enter
2131 * kernel-mode, without any delay. (to get signals handled.)
2132 *
2133 * NOTE: this function doesnt have to take the runqueue lock,
2134 * because all it wants to ensure is that the remote task enters
2135 * the kernel. If the IPI races and the task has been migrated
2136 * to another CPU then no harm is done and the purpose has been
2137 * achieved as well.
2138 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07002139void kick_process(struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002140{
2141 int cpu;
2142
2143 preempt_disable();
2144 cpu = task_cpu(p);
2145 if ((cpu != smp_processor_id()) && task_curr(p))
2146 smp_send_reschedule(cpu);
2147 preempt_enable();
2148}
2149
2150/*
Peter Williams2dd73a42006-06-27 02:54:34 -07002151 * Return a low guess at the load of a migration-source cpu weighted
2152 * according to the scheduling class and "nice" value.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002153 *
2154 * We want to under-estimate the load of migration sources, to
2155 * balance conservatively.
2156 */
Alexey Dobriyana9957442007-10-15 17:00:13 +02002157static unsigned long source_load(int cpu, int type)
Con Kolivasb9104722005-11-08 21:38:55 -08002158{
Ingo Molnar70b97a72006-07-03 00:25:42 -07002159 struct rq *rq = cpu_rq(cpu);
Ingo Molnardd41f592007-07-09 18:51:59 +02002160 unsigned long total = weighted_cpuload(cpu);
Nick Piggina2000572006-02-10 01:51:02 -08002161
Peter Zijlstra93b75212008-06-27 13:41:33 +02002162 if (type == 0 || !sched_feat(LB_BIAS))
Ingo Molnardd41f592007-07-09 18:51:59 +02002163 return total;
Peter Williams2dd73a42006-06-27 02:54:34 -07002164
Ingo Molnardd41f592007-07-09 18:51:59 +02002165 return min(rq->cpu_load[type-1], total);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002166}
2167
2168/*
Peter Williams2dd73a42006-06-27 02:54:34 -07002169 * Return a high guess at the load of a migration-target cpu weighted
2170 * according to the scheduling class and "nice" value.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002171 */
Alexey Dobriyana9957442007-10-15 17:00:13 +02002172static unsigned long target_load(int cpu, int type)
Con Kolivasb9104722005-11-08 21:38:55 -08002173{
Ingo Molnar70b97a72006-07-03 00:25:42 -07002174 struct rq *rq = cpu_rq(cpu);
Ingo Molnardd41f592007-07-09 18:51:59 +02002175 unsigned long total = weighted_cpuload(cpu);
Nick Piggina2000572006-02-10 01:51:02 -08002176
Peter Zijlstra93b75212008-06-27 13:41:33 +02002177 if (type == 0 || !sched_feat(LB_BIAS))
Ingo Molnardd41f592007-07-09 18:51:59 +02002178 return total;
Peter Williams2dd73a42006-06-27 02:54:34 -07002179
Ingo Molnardd41f592007-07-09 18:51:59 +02002180 return max(rq->cpu_load[type-1], total);
Peter Williams2dd73a42006-06-27 02:54:34 -07002181}
2182
2183/*
Nick Piggin147cbb42005-06-25 14:57:19 -07002184 * find_idlest_group finds and returns the least busy CPU group within the
2185 * domain.
2186 */
2187static struct sched_group *
2188find_idlest_group(struct sched_domain *sd, struct task_struct *p, int this_cpu)
2189{
2190 struct sched_group *idlest = NULL, *this = NULL, *group = sd->groups;
2191 unsigned long min_load = ULONG_MAX, this_load = 0;
2192 int load_idx = sd->forkexec_idx;
2193 int imbalance = 100 + (sd->imbalance_pct-100)/2;
2194
2195 do {
2196 unsigned long load, avg_load;
2197 int local_group;
2198 int i;
2199
M.Baris Demirayda5a5522005-09-10 00:26:09 -07002200 /* Skip over this group if it has no CPUs allowed */
Rusty Russell758b2cd2008-11-25 02:35:04 +10302201 if (!cpumask_intersects(sched_group_cpus(group),
2202 &p->cpus_allowed))
Andi Kleen3a5c3592007-10-15 17:00:14 +02002203 continue;
M.Baris Demirayda5a5522005-09-10 00:26:09 -07002204
Rusty Russell758b2cd2008-11-25 02:35:04 +10302205 local_group = cpumask_test_cpu(this_cpu,
2206 sched_group_cpus(group));
Nick Piggin147cbb42005-06-25 14:57:19 -07002207
2208 /* Tally up the load of all CPUs in the group */
2209 avg_load = 0;
2210
Rusty Russell758b2cd2008-11-25 02:35:04 +10302211 for_each_cpu(i, sched_group_cpus(group)) {
Nick Piggin147cbb42005-06-25 14:57:19 -07002212 /* Bias balancing toward cpus of our domain */
2213 if (local_group)
2214 load = source_load(i, load_idx);
2215 else
2216 load = target_load(i, load_idx);
2217
2218 avg_load += load;
2219 }
2220
2221 /* Adjust by relative CPU power of the group */
Eric Dumazet5517d862007-05-08 00:32:57 -07002222 avg_load = sg_div_cpu_power(group,
2223 avg_load * SCHED_LOAD_SCALE);
Nick Piggin147cbb42005-06-25 14:57:19 -07002224
2225 if (local_group) {
2226 this_load = avg_load;
2227 this = group;
2228 } else if (avg_load < min_load) {
2229 min_load = avg_load;
2230 idlest = group;
2231 }
Andi Kleen3a5c3592007-10-15 17:00:14 +02002232 } while (group = group->next, group != sd->groups);
Nick Piggin147cbb42005-06-25 14:57:19 -07002233
2234 if (!idlest || 100*this_load < imbalance*min_load)
2235 return NULL;
2236 return idlest;
2237}
2238
2239/*
Satoru Takeuchi0feaece2006-10-03 01:14:10 -07002240 * find_idlest_cpu - find the idlest cpu among the cpus in group.
Nick Piggin147cbb42005-06-25 14:57:19 -07002241 */
Ingo Molnar95cdf3b2005-09-10 00:26:11 -07002242static int
Rusty Russell758b2cd2008-11-25 02:35:04 +10302243find_idlest_cpu(struct sched_group *group, struct task_struct *p, int this_cpu)
Nick Piggin147cbb42005-06-25 14:57:19 -07002244{
2245 unsigned long load, min_load = ULONG_MAX;
2246 int idlest = -1;
2247 int i;
2248
M.Baris Demirayda5a5522005-09-10 00:26:09 -07002249 /* Traverse only the allowed CPUs */
Rusty Russell758b2cd2008-11-25 02:35:04 +10302250 for_each_cpu_and(i, sched_group_cpus(group), &p->cpus_allowed) {
Peter Williams2dd73a42006-06-27 02:54:34 -07002251 load = weighted_cpuload(i);
Nick Piggin147cbb42005-06-25 14:57:19 -07002252
2253 if (load < min_load || (load == min_load && i == this_cpu)) {
2254 min_load = load;
2255 idlest = i;
2256 }
2257 }
2258
2259 return idlest;
2260}
2261
Nick Piggin476d1392005-06-25 14:57:29 -07002262/*
2263 * sched_balance_self: balance the current task (running on cpu) in domains
2264 * that have the 'flag' flag set. In practice, this is SD_BALANCE_FORK and
2265 * SD_BALANCE_EXEC.
2266 *
2267 * Balance, ie. select the least loaded group.
2268 *
2269 * Returns the target CPU number, or the same CPU if no balancing is needed.
2270 *
2271 * preempt must be disabled.
2272 */
2273static int sched_balance_self(int cpu, int flag)
2274{
2275 struct task_struct *t = current;
2276 struct sched_domain *tmp, *sd = NULL;
Nick Piggin147cbb42005-06-25 14:57:19 -07002277
Chen, Kenneth Wc96d1452006-06-27 02:54:28 -07002278 for_each_domain(cpu, tmp) {
Ingo Molnar9761eea2007-07-09 18:52:00 +02002279 /*
2280 * If power savings logic is enabled for a domain, stop there.
2281 */
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07002282 if (tmp->flags & SD_POWERSAVINGS_BALANCE)
2283 break;
Nick Piggin476d1392005-06-25 14:57:29 -07002284 if (tmp->flags & flag)
2285 sd = tmp;
Chen, Kenneth Wc96d1452006-06-27 02:54:28 -07002286 }
Nick Piggin476d1392005-06-25 14:57:29 -07002287
Peter Zijlstra039a1c42008-06-27 13:41:25 +02002288 if (sd)
2289 update_shares(sd);
2290
Nick Piggin476d1392005-06-25 14:57:29 -07002291 while (sd) {
Nick Piggin476d1392005-06-25 14:57:29 -07002292 struct sched_group *group;
Siddha, Suresh B1a848872006-10-03 01:14:08 -07002293 int new_cpu, weight;
2294
2295 if (!(sd->flags & flag)) {
2296 sd = sd->child;
2297 continue;
2298 }
Nick Piggin476d1392005-06-25 14:57:29 -07002299
Nick Piggin476d1392005-06-25 14:57:29 -07002300 group = find_idlest_group(sd, t, cpu);
Siddha, Suresh B1a848872006-10-03 01:14:08 -07002301 if (!group) {
2302 sd = sd->child;
2303 continue;
2304 }
Nick Piggin476d1392005-06-25 14:57:29 -07002305
Rusty Russell758b2cd2008-11-25 02:35:04 +10302306 new_cpu = find_idlest_cpu(group, t, cpu);
Siddha, Suresh B1a848872006-10-03 01:14:08 -07002307 if (new_cpu == -1 || new_cpu == cpu) {
2308 /* Now try balancing at a lower domain level of cpu */
2309 sd = sd->child;
2310 continue;
2311 }
Nick Piggin476d1392005-06-25 14:57:29 -07002312
Siddha, Suresh B1a848872006-10-03 01:14:08 -07002313 /* Now try balancing at a lower domain level of new_cpu */
Nick Piggin476d1392005-06-25 14:57:29 -07002314 cpu = new_cpu;
Rusty Russell758b2cd2008-11-25 02:35:04 +10302315 weight = cpumask_weight(sched_domain_span(sd));
Nick Piggin476d1392005-06-25 14:57:29 -07002316 sd = NULL;
Nick Piggin476d1392005-06-25 14:57:29 -07002317 for_each_domain(cpu, tmp) {
Rusty Russell758b2cd2008-11-25 02:35:04 +10302318 if (weight <= cpumask_weight(sched_domain_span(tmp)))
Nick Piggin476d1392005-06-25 14:57:29 -07002319 break;
2320 if (tmp->flags & flag)
2321 sd = tmp;
2322 }
2323 /* while loop will break here if sd == NULL */
2324 }
2325
2326 return cpu;
2327}
2328
2329#endif /* CONFIG_SMP */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002330
Thomas Gleixner0793a612008-12-04 20:12:29 +01002331/**
2332 * task_oncpu_function_call - call a function on the cpu on which a task runs
2333 * @p: the task to evaluate
2334 * @func: the function to be called
2335 * @info: the function call argument
2336 *
2337 * Calls the function @func when the task is currently running. This might
2338 * be on the current CPU, which just calls the function directly
2339 */
2340void task_oncpu_function_call(struct task_struct *p,
2341 void (*func) (void *info), void *info)
2342{
2343 int cpu;
2344
2345 preempt_disable();
2346 cpu = task_cpu(p);
2347 if (task_curr(p))
2348 smp_call_function_single(cpu, func, info, 1);
2349 preempt_enable();
2350}
2351
Linus Torvalds1da177e2005-04-16 15:20:36 -07002352/***
2353 * try_to_wake_up - wake up a thread
2354 * @p: the to-be-woken-up thread
2355 * @state: the mask of task states that can be woken
2356 * @sync: do a synchronous wakeup?
2357 *
2358 * Put it on the run-queue if it's not already there. The "current"
2359 * thread is always on the run-queue (except when the actual
2360 * re-schedule is in progress), and as such you're allowed to do
2361 * the simpler "current->state = TASK_RUNNING" to mark yourself
2362 * runnable without the overhead of this.
2363 *
2364 * returns failure only if the task is already active.
2365 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07002366static int try_to_wake_up(struct task_struct *p, unsigned int state, int sync)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002367{
Ingo Molnarcc367732007-10-15 17:00:18 +02002368 int cpu, orig_cpu, this_cpu, success = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002369 unsigned long flags;
2370 long old_state;
Ingo Molnar70b97a72006-07-03 00:25:42 -07002371 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002372
Ingo Molnarb85d0662008-03-16 20:03:22 +01002373 if (!sched_feat(SYNC_WAKEUPS))
2374 sync = 0;
2375
Peter Zijlstra2398f2c2008-06-27 13:41:35 +02002376#ifdef CONFIG_SMP
Peter Zijlstra57310a92009-03-09 13:56:21 +01002377 if (sched_feat(LB_WAKEUP_UPDATE) && !root_task_group_empty()) {
Peter Zijlstra2398f2c2008-06-27 13:41:35 +02002378 struct sched_domain *sd;
2379
2380 this_cpu = raw_smp_processor_id();
2381 cpu = task_cpu(p);
2382
2383 for_each_domain(this_cpu, sd) {
Rusty Russell758b2cd2008-11-25 02:35:04 +10302384 if (cpumask_test_cpu(cpu, sched_domain_span(sd))) {
Peter Zijlstra2398f2c2008-06-27 13:41:35 +02002385 update_shares(sd);
2386 break;
2387 }
2388 }
2389 }
2390#endif
2391
Linus Torvalds04e2f172008-02-23 18:05:03 -08002392 smp_wmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002393 rq = task_rq_lock(p, &flags);
Mike Galbraith03e89e42008-12-16 08:45:30 +01002394 update_rq_clock(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002395 old_state = p->state;
2396 if (!(old_state & state))
2397 goto out;
2398
Ingo Molnardd41f592007-07-09 18:51:59 +02002399 if (p->se.on_rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002400 goto out_running;
2401
2402 cpu = task_cpu(p);
Ingo Molnarcc367732007-10-15 17:00:18 +02002403 orig_cpu = cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002404 this_cpu = smp_processor_id();
2405
2406#ifdef CONFIG_SMP
2407 if (unlikely(task_running(rq, p)))
2408 goto out_activate;
2409
Dmitry Adamushko5d2f5a62008-01-25 21:08:21 +01002410 cpu = p->sched_class->select_task_rq(p, sync);
2411 if (cpu != orig_cpu) {
2412 set_task_cpu(p, cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002413 task_rq_unlock(rq, &flags);
2414 /* might preempt at this point */
2415 rq = task_rq_lock(p, &flags);
2416 old_state = p->state;
2417 if (!(old_state & state))
2418 goto out;
Ingo Molnardd41f592007-07-09 18:51:59 +02002419 if (p->se.on_rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002420 goto out_running;
2421
2422 this_cpu = smp_processor_id();
2423 cpu = task_cpu(p);
2424 }
2425
Gregory Haskinse7693a32008-01-25 21:08:09 +01002426#ifdef CONFIG_SCHEDSTATS
2427 schedstat_inc(rq, ttwu_count);
2428 if (cpu == this_cpu)
2429 schedstat_inc(rq, ttwu_local);
2430 else {
2431 struct sched_domain *sd;
2432 for_each_domain(this_cpu, sd) {
Rusty Russell758b2cd2008-11-25 02:35:04 +10302433 if (cpumask_test_cpu(cpu, sched_domain_span(sd))) {
Gregory Haskinse7693a32008-01-25 21:08:09 +01002434 schedstat_inc(sd, ttwu_wake_remote);
2435 break;
2436 }
2437 }
2438 }
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +02002439#endif /* CONFIG_SCHEDSTATS */
Gregory Haskinse7693a32008-01-25 21:08:09 +01002440
Linus Torvalds1da177e2005-04-16 15:20:36 -07002441out_activate:
2442#endif /* CONFIG_SMP */
Ingo Molnarcc367732007-10-15 17:00:18 +02002443 schedstat_inc(p, se.nr_wakeups);
2444 if (sync)
2445 schedstat_inc(p, se.nr_wakeups_sync);
2446 if (orig_cpu != cpu)
2447 schedstat_inc(p, se.nr_wakeups_migrate);
2448 if (cpu == this_cpu)
2449 schedstat_inc(p, se.nr_wakeups_local);
2450 else
2451 schedstat_inc(p, se.nr_wakeups_remote);
Ingo Molnardd41f592007-07-09 18:51:59 +02002452 activate_task(rq, p, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002453 success = 1;
2454
Peter Zijlstra831451a2009-01-14 12:39:18 +01002455 /*
2456 * Only attribute actual wakeups done by this task.
2457 */
2458 if (!in_interrupt()) {
2459 struct sched_entity *se = &current->se;
2460 u64 sample = se->sum_exec_runtime;
2461
2462 if (se->last_wakeup)
2463 sample -= se->last_wakeup;
2464 else
2465 sample -= se->start_runtime;
2466 update_avg(&se->avg_wakeup, sample);
2467
2468 se->last_wakeup = se->sum_exec_runtime;
2469 }
2470
Linus Torvalds1da177e2005-04-16 15:20:36 -07002471out_running:
Peter Zijlstra468a15b2008-12-16 08:07:03 +01002472 trace_sched_wakeup(rq, p, success);
Peter Zijlstra15afe092008-09-20 23:38:02 +02002473 check_preempt_curr(rq, p, sync);
Ingo Molnar4ae7d5c2008-03-19 01:42:00 +01002474
Linus Torvalds1da177e2005-04-16 15:20:36 -07002475 p->state = TASK_RUNNING;
Steven Rostedt9a897c52008-01-25 21:08:22 +01002476#ifdef CONFIG_SMP
2477 if (p->sched_class->task_wake_up)
2478 p->sched_class->task_wake_up(rq, p);
2479#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002480out:
2481 task_rq_unlock(rq, &flags);
2482
2483 return success;
2484}
2485
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -08002486int wake_up_process(struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002487{
Matthew Wilcoxd9514f62007-12-06 11:07:07 -05002488 return try_to_wake_up(p, TASK_ALL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002489}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002490EXPORT_SYMBOL(wake_up_process);
2491
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -08002492int wake_up_state(struct task_struct *p, unsigned int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002493{
2494 return try_to_wake_up(p, state, 0);
2495}
2496
Linus Torvalds1da177e2005-04-16 15:20:36 -07002497/*
2498 * Perform scheduler related setup for a newly forked process p.
2499 * p is forked by current.
Ingo Molnardd41f592007-07-09 18:51:59 +02002500 *
2501 * __sched_fork() is basic setup used by init_idle() too:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002502 */
Ingo Molnardd41f592007-07-09 18:51:59 +02002503static void __sched_fork(struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002504{
Ingo Molnardd41f592007-07-09 18:51:59 +02002505 p->se.exec_start = 0;
2506 p->se.sum_exec_runtime = 0;
Ingo Molnarf6cf8912007-08-28 12:53:24 +02002507 p->se.prev_sum_exec_runtime = 0;
Ingo Molnar6c594c22008-12-14 12:34:15 +01002508 p->se.nr_migrations = 0;
Ingo Molnar4ae7d5c2008-03-19 01:42:00 +01002509 p->se.last_wakeup = 0;
2510 p->se.avg_overlap = 0;
Peter Zijlstra831451a2009-01-14 12:39:18 +01002511 p->se.start_runtime = 0;
2512 p->se.avg_wakeup = sysctl_sched_wakeup_granularity;
Ingo Molnar6cfb0d52007-08-02 17:41:40 +02002513
2514#ifdef CONFIG_SCHEDSTATS
2515 p->se.wait_start = 0;
Ingo Molnardd41f592007-07-09 18:51:59 +02002516 p->se.sum_sleep_runtime = 0;
2517 p->se.sleep_start = 0;
Ingo Molnardd41f592007-07-09 18:51:59 +02002518 p->se.block_start = 0;
2519 p->se.sleep_max = 0;
2520 p->se.block_max = 0;
2521 p->se.exec_max = 0;
Ingo Molnareba1ed42007-10-15 17:00:02 +02002522 p->se.slice_max = 0;
Ingo Molnardd41f592007-07-09 18:51:59 +02002523 p->se.wait_max = 0;
Ingo Molnar6cfb0d52007-08-02 17:41:40 +02002524#endif
Nick Piggin476d1392005-06-25 14:57:29 -07002525
Peter Zijlstrafa717062008-01-25 21:08:27 +01002526 INIT_LIST_HEAD(&p->rt.run_list);
Ingo Molnardd41f592007-07-09 18:51:59 +02002527 p->se.on_rq = 0;
Peter Zijlstra4a55bd52008-04-19 19:45:00 +02002528 INIT_LIST_HEAD(&p->se.group_node);
Nick Piggin476d1392005-06-25 14:57:29 -07002529
Avi Kivitye107be32007-07-26 13:40:43 +02002530#ifdef CONFIG_PREEMPT_NOTIFIERS
2531 INIT_HLIST_HEAD(&p->preempt_notifiers);
2532#endif
2533
Linus Torvalds1da177e2005-04-16 15:20:36 -07002534 /*
2535 * We mark the process as running here, but have not actually
2536 * inserted it onto the runqueue yet. This guarantees that
2537 * nobody will actually run it, and a signal or other external
2538 * event cannot wake it up and insert it on the runqueue either.
2539 */
2540 p->state = TASK_RUNNING;
Ingo Molnardd41f592007-07-09 18:51:59 +02002541}
2542
2543/*
2544 * fork()/clone()-time setup:
2545 */
2546void sched_fork(struct task_struct *p, int clone_flags)
2547{
2548 int cpu = get_cpu();
2549
2550 __sched_fork(p);
2551
2552#ifdef CONFIG_SMP
2553 cpu = sched_balance_self(cpu, SD_BALANCE_FORK);
2554#endif
Ingo Molnar02e4bac2007-10-15 17:00:11 +02002555 set_task_cpu(p, cpu);
Ingo Molnarb29739f2006-06-27 02:54:51 -07002556
2557 /*
2558 * Make sure we do not leak PI boosting priority to the child:
2559 */
2560 p->prio = current->normal_prio;
Hiroshi Shimamoto2ddbf952007-10-15 17:00:11 +02002561 if (!rt_prio(p->prio))
2562 p->sched_class = &fair_sched_class;
Ingo Molnarb29739f2006-06-27 02:54:51 -07002563
Chandra Seetharaman52f17b62006-07-14 00:24:38 -07002564#if defined(CONFIG_SCHEDSTATS) || defined(CONFIG_TASK_DELAY_ACCT)
Ingo Molnardd41f592007-07-09 18:51:59 +02002565 if (likely(sched_info_on()))
Chandra Seetharaman52f17b62006-07-14 00:24:38 -07002566 memset(&p->sched_info, 0, sizeof(p->sched_info));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002567#endif
Chen, Kenneth Wd6077cb2006-02-14 13:53:10 -08002568#if defined(CONFIG_SMP) && defined(__ARCH_WANT_UNLOCKED_CTXSW)
Nick Piggin4866cde2005-06-25 14:57:23 -07002569 p->oncpu = 0;
2570#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002571#ifdef CONFIG_PREEMPT
Nick Piggin4866cde2005-06-25 14:57:23 -07002572 /* Want to start with kernel preemption disabled. */
Al Viroa1261f52005-11-13 16:06:55 -08002573 task_thread_info(p)->preempt_count = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002574#endif
Gregory Haskins917b6272008-12-29 09:39:53 -05002575 plist_node_init(&p->pushable_tasks, MAX_PRIO);
2576
Nick Piggin476d1392005-06-25 14:57:29 -07002577 put_cpu();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002578}
2579
2580/*
2581 * wake_up_new_task - wake up a newly created task for the first time.
2582 *
2583 * This function will do some initial scheduler statistics housekeeping
2584 * that must be done for every newly created context, then puts the task
2585 * on the runqueue and wakes it.
2586 */
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -08002587void wake_up_new_task(struct task_struct *p, unsigned long clone_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002588{
2589 unsigned long flags;
Ingo Molnardd41f592007-07-09 18:51:59 +02002590 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002591
2592 rq = task_rq_lock(p, &flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002593 BUG_ON(p->state != TASK_RUNNING);
Ingo Molnara8e504d2007-08-09 11:16:47 +02002594 update_rq_clock(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002595
2596 p->prio = effective_prio(p);
2597
Srivatsa Vaddagirib9dca1e2007-10-17 16:55:11 +02002598 if (!p->sched_class->task_new || !current->se.on_rq) {
Ingo Molnardd41f592007-07-09 18:51:59 +02002599 activate_task(rq, p, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002600 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002601 /*
Ingo Molnardd41f592007-07-09 18:51:59 +02002602 * Let the scheduling class do new task startup
2603 * management (if any):
Linus Torvalds1da177e2005-04-16 15:20:36 -07002604 */
Ingo Molnaree0827d2007-08-09 11:16:49 +02002605 p->sched_class->task_new(rq, p);
Peter Zijlstrac09595f2008-06-27 13:41:14 +02002606 inc_nr_running(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002607 }
Ingo Molnarc71dd422008-12-19 01:09:51 +01002608 trace_sched_wakeup_new(rq, p, 1);
Peter Zijlstra15afe092008-09-20 23:38:02 +02002609 check_preempt_curr(rq, p, 0);
Steven Rostedt9a897c52008-01-25 21:08:22 +01002610#ifdef CONFIG_SMP
2611 if (p->sched_class->task_wake_up)
2612 p->sched_class->task_wake_up(rq, p);
2613#endif
Ingo Molnardd41f592007-07-09 18:51:59 +02002614 task_rq_unlock(rq, &flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002615}
2616
Avi Kivitye107be32007-07-26 13:40:43 +02002617#ifdef CONFIG_PREEMPT_NOTIFIERS
2618
2619/**
Luis Henriques80dd99b2009-03-16 19:58:09 +00002620 * preempt_notifier_register - tell me when current is being preempted & rescheduled
Randy Dunlap421cee22007-07-31 00:37:50 -07002621 * @notifier: notifier struct to register
Avi Kivitye107be32007-07-26 13:40:43 +02002622 */
2623void preempt_notifier_register(struct preempt_notifier *notifier)
2624{
2625 hlist_add_head(&notifier->link, &current->preempt_notifiers);
2626}
2627EXPORT_SYMBOL_GPL(preempt_notifier_register);
2628
2629/**
2630 * preempt_notifier_unregister - no longer interested in preemption notifications
Randy Dunlap421cee22007-07-31 00:37:50 -07002631 * @notifier: notifier struct to unregister
Avi Kivitye107be32007-07-26 13:40:43 +02002632 *
2633 * This is safe to call from within a preemption notifier.
2634 */
2635void preempt_notifier_unregister(struct preempt_notifier *notifier)
2636{
2637 hlist_del(&notifier->link);
2638}
2639EXPORT_SYMBOL_GPL(preempt_notifier_unregister);
2640
2641static void fire_sched_in_preempt_notifiers(struct task_struct *curr)
2642{
2643 struct preempt_notifier *notifier;
2644 struct hlist_node *node;
2645
2646 hlist_for_each_entry(notifier, node, &curr->preempt_notifiers, link)
2647 notifier->ops->sched_in(notifier, raw_smp_processor_id());
2648}
2649
2650static void
2651fire_sched_out_preempt_notifiers(struct task_struct *curr,
2652 struct task_struct *next)
2653{
2654 struct preempt_notifier *notifier;
2655 struct hlist_node *node;
2656
2657 hlist_for_each_entry(notifier, node, &curr->preempt_notifiers, link)
2658 notifier->ops->sched_out(notifier, next);
2659}
2660
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +02002661#else /* !CONFIG_PREEMPT_NOTIFIERS */
Avi Kivitye107be32007-07-26 13:40:43 +02002662
2663static void fire_sched_in_preempt_notifiers(struct task_struct *curr)
2664{
2665}
2666
2667static void
2668fire_sched_out_preempt_notifiers(struct task_struct *curr,
2669 struct task_struct *next)
2670{
2671}
2672
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +02002673#endif /* CONFIG_PREEMPT_NOTIFIERS */
Avi Kivitye107be32007-07-26 13:40:43 +02002674
Linus Torvalds1da177e2005-04-16 15:20:36 -07002675/**
Nick Piggin4866cde2005-06-25 14:57:23 -07002676 * prepare_task_switch - prepare to switch tasks
2677 * @rq: the runqueue preparing to switch
Randy Dunlap421cee22007-07-31 00:37:50 -07002678 * @prev: the current task that is being switched out
Nick Piggin4866cde2005-06-25 14:57:23 -07002679 * @next: the task we are going to switch to.
2680 *
2681 * This is called with the rq lock held and interrupts off. It must
2682 * be paired with a subsequent finish_task_switch after the context
2683 * switch.
2684 *
2685 * prepare_task_switch sets up locking and calls architecture specific
2686 * hooks.
2687 */
Avi Kivitye107be32007-07-26 13:40:43 +02002688static inline void
2689prepare_task_switch(struct rq *rq, struct task_struct *prev,
2690 struct task_struct *next)
Nick Piggin4866cde2005-06-25 14:57:23 -07002691{
Avi Kivitye107be32007-07-26 13:40:43 +02002692 fire_sched_out_preempt_notifiers(prev, next);
Nick Piggin4866cde2005-06-25 14:57:23 -07002693 prepare_lock_switch(rq, next);
2694 prepare_arch_switch(next);
2695}
2696
2697/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07002698 * finish_task_switch - clean up after a task-switch
Jeff Garzik344baba2005-09-07 01:15:17 -04002699 * @rq: runqueue associated with task-switch
Linus Torvalds1da177e2005-04-16 15:20:36 -07002700 * @prev: the thread we just switched away from.
2701 *
Nick Piggin4866cde2005-06-25 14:57:23 -07002702 * finish_task_switch must be called after the context switch, paired
2703 * with a prepare_task_switch call before the context switch.
2704 * finish_task_switch will reconcile locking set up by prepare_task_switch,
2705 * and do any other architecture-specific cleanup actions.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002706 *
2707 * Note that we may have delayed dropping an mm in context_switch(). If
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01002708 * so, we finish that here outside of the runqueue lock. (Doing it
Linus Torvalds1da177e2005-04-16 15:20:36 -07002709 * with the lock held can cause deadlocks; see schedule() for
2710 * details.)
2711 */
Alexey Dobriyana9957442007-10-15 17:00:13 +02002712static void finish_task_switch(struct rq *rq, struct task_struct *prev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002713 __releases(rq->lock)
2714{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002715 struct mm_struct *mm = rq->prev_mm;
Oleg Nesterov55a101f2006-09-29 02:01:10 -07002716 long prev_state;
Gregory Haskins967fc042008-12-29 09:39:52 -05002717#ifdef CONFIG_SMP
2718 int post_schedule = 0;
2719
2720 if (current->sched_class->needs_post_schedule)
2721 post_schedule = current->sched_class->needs_post_schedule(rq);
2722#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002723
2724 rq->prev_mm = NULL;
2725
2726 /*
2727 * A task struct has one reference for the use as "current".
Oleg Nesterovc394cc92006-09-29 02:01:11 -07002728 * If a task dies, then it sets TASK_DEAD in tsk->state and calls
Oleg Nesterov55a101f2006-09-29 02:01:10 -07002729 * schedule one last time. The schedule call will never return, and
2730 * the scheduled task must drop that reference.
Oleg Nesterovc394cc92006-09-29 02:01:11 -07002731 * The test for TASK_DEAD must occur while the runqueue locks are
Linus Torvalds1da177e2005-04-16 15:20:36 -07002732 * still held, otherwise prev could be scheduled on another cpu, die
2733 * there before we look at prev->state, and then the reference would
2734 * be dropped twice.
2735 * Manfred Spraul <manfred@colorfullife.com>
2736 */
Oleg Nesterov55a101f2006-09-29 02:01:10 -07002737 prev_state = prev->state;
Nick Piggin4866cde2005-06-25 14:57:23 -07002738 finish_arch_switch(prev);
Thomas Gleixner0793a612008-12-04 20:12:29 +01002739 perf_counter_task_sched_in(current, cpu_of(rq));
Nick Piggin4866cde2005-06-25 14:57:23 -07002740 finish_lock_switch(rq, prev);
Steven Rostedt9a897c52008-01-25 21:08:22 +01002741#ifdef CONFIG_SMP
Gregory Haskins967fc042008-12-29 09:39:52 -05002742 if (post_schedule)
Steven Rostedt9a897c52008-01-25 21:08:22 +01002743 current->sched_class->post_schedule(rq);
2744#endif
Steven Rostedte8fa1362008-01-25 21:08:05 +01002745
Avi Kivitye107be32007-07-26 13:40:43 +02002746 fire_sched_in_preempt_notifiers(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002747 if (mm)
2748 mmdrop(mm);
Oleg Nesterovc394cc92006-09-29 02:01:11 -07002749 if (unlikely(prev_state == TASK_DEAD)) {
bibo maoc6fd91f2006-03-26 01:38:20 -08002750 /*
2751 * Remove function-return probe instances associated with this
2752 * task and put them back on the free list.
Ingo Molnar9761eea2007-07-09 18:52:00 +02002753 */
bibo maoc6fd91f2006-03-26 01:38:20 -08002754 kprobe_flush_task(prev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002755 put_task_struct(prev);
bibo maoc6fd91f2006-03-26 01:38:20 -08002756 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002757}
2758
2759/**
2760 * schedule_tail - first thing a freshly forked thread must call.
2761 * @prev: the thread we just switched away from.
2762 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07002763asmlinkage void schedule_tail(struct task_struct *prev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002764 __releases(rq->lock)
2765{
Ingo Molnar70b97a72006-07-03 00:25:42 -07002766 struct rq *rq = this_rq();
2767
Nick Piggin4866cde2005-06-25 14:57:23 -07002768 finish_task_switch(rq, prev);
2769#ifdef __ARCH_WANT_UNLOCKED_CTXSW
2770 /* In this case, finish_task_switch does not reenable preemption */
2771 preempt_enable();
2772#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002773 if (current->set_child_tid)
Pavel Emelyanovb4888932007-10-18 23:40:14 -07002774 put_user(task_pid_vnr(current), current->set_child_tid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002775}
2776
2777/*
2778 * context_switch - switch to the new MM and the new
2779 * thread's register state.
2780 */
Ingo Molnardd41f592007-07-09 18:51:59 +02002781static inline void
Ingo Molnar70b97a72006-07-03 00:25:42 -07002782context_switch(struct rq *rq, struct task_struct *prev,
Ingo Molnar36c8b582006-07-03 00:25:41 -07002783 struct task_struct *next)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002784{
Ingo Molnardd41f592007-07-09 18:51:59 +02002785 struct mm_struct *mm, *oldmm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002786
Avi Kivitye107be32007-07-26 13:40:43 +02002787 prepare_task_switch(rq, prev, next);
Mathieu Desnoyers0a16b602008-07-18 12:16:17 -04002788 trace_sched_switch(rq, prev, next);
Ingo Molnardd41f592007-07-09 18:51:59 +02002789 mm = next->mm;
2790 oldmm = prev->active_mm;
Zachary Amsden9226d122007-02-13 13:26:21 +01002791 /*
2792 * For paravirt, this is coupled with an exit in switch_to to
2793 * combine the page table reload and the switch backend into
2794 * one hypercall.
2795 */
2796 arch_enter_lazy_cpu_mode();
2797
Ingo Molnardd41f592007-07-09 18:51:59 +02002798 if (unlikely(!mm)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002799 next->active_mm = oldmm;
2800 atomic_inc(&oldmm->mm_count);
2801 enter_lazy_tlb(oldmm, next);
2802 } else
2803 switch_mm(oldmm, mm, next);
2804
Ingo Molnardd41f592007-07-09 18:51:59 +02002805 if (unlikely(!prev->mm)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002806 prev->active_mm = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002807 rq->prev_mm = oldmm;
2808 }
Ingo Molnar3a5f5e42006-07-14 00:24:27 -07002809 /*
2810 * Since the runqueue lock will be released by the next
2811 * task (which is an invalid locking op but in the case
2812 * of the scheduler it's an obvious special-case), so we
2813 * do an early lockdep release here:
2814 */
2815#ifndef __ARCH_WANT_UNLOCKED_CTXSW
Ingo Molnar8a25d5d2006-07-03 00:24:54 -07002816 spin_release(&rq->lock.dep_map, 1, _THIS_IP_);
Ingo Molnar3a5f5e42006-07-14 00:24:27 -07002817#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002818
2819 /* Here we just switch the register state and the stack. */
2820 switch_to(prev, next, prev);
2821
Ingo Molnardd41f592007-07-09 18:51:59 +02002822 barrier();
2823 /*
2824 * this_rq must be evaluated again because prev may have moved
2825 * CPUs since it called schedule(), thus the 'rq' on its stack
2826 * frame will be invalid.
2827 */
2828 finish_task_switch(this_rq(), prev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002829}
2830
2831/*
2832 * nr_running, nr_uninterruptible and nr_context_switches:
2833 *
2834 * externally visible scheduler statistics: current number of runnable
2835 * threads, current number of uninterruptible-sleeping threads, total
2836 * number of context switches performed since bootup.
2837 */
2838unsigned long nr_running(void)
2839{
2840 unsigned long i, sum = 0;
2841
2842 for_each_online_cpu(i)
2843 sum += cpu_rq(i)->nr_running;
2844
2845 return sum;
2846}
2847
2848unsigned long nr_uninterruptible(void)
2849{
2850 unsigned long i, sum = 0;
2851
KAMEZAWA Hiroyuki0a945022006-03-28 01:56:37 -08002852 for_each_possible_cpu(i)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002853 sum += cpu_rq(i)->nr_uninterruptible;
2854
2855 /*
2856 * Since we read the counters lockless, it might be slightly
2857 * inaccurate. Do not allow it to go below zero though:
2858 */
2859 if (unlikely((long)sum < 0))
2860 sum = 0;
2861
2862 return sum;
2863}
2864
2865unsigned long long nr_context_switches(void)
2866{
Steven Rostedtcc94abf2006-06-27 02:54:31 -07002867 int i;
2868 unsigned long long sum = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002869
KAMEZAWA Hiroyuki0a945022006-03-28 01:56:37 -08002870 for_each_possible_cpu(i)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002871 sum += cpu_rq(i)->nr_switches;
2872
2873 return sum;
2874}
2875
2876unsigned long nr_iowait(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 += atomic_read(&cpu_rq(i)->nr_iowait);
2882
2883 return sum;
2884}
2885
Jack Steinerdb1b1fe2006-03-31 02:31:21 -08002886unsigned long nr_active(void)
2887{
2888 unsigned long i, running = 0, uninterruptible = 0;
2889
2890 for_each_online_cpu(i) {
2891 running += cpu_rq(i)->nr_running;
2892 uninterruptible += cpu_rq(i)->nr_uninterruptible;
2893 }
2894
2895 if (unlikely((long)uninterruptible < 0))
2896 uninterruptible = 0;
2897
2898 return running + uninterruptible;
2899}
2900
Linus Torvalds1da177e2005-04-16 15:20:36 -07002901/*
Paul Mackerras23a185c2009-02-09 22:42:47 +11002902 * Externally visible per-cpu scheduler statistics:
2903 * cpu_nr_switches(cpu) - number of context switches on that cpu
2904 * cpu_nr_migrations(cpu) - number of migrations into that cpu
2905 */
2906u64 cpu_nr_switches(int cpu)
2907{
2908 return cpu_rq(cpu)->nr_switches;
2909}
2910
2911u64 cpu_nr_migrations(int cpu)
2912{
2913 return cpu_rq(cpu)->nr_migrations_in;
2914}
2915
2916/*
Ingo Molnardd41f592007-07-09 18:51:59 +02002917 * Update rq->cpu_load[] statistics. This function is usually called every
2918 * scheduler tick (TICK_NSEC).
Ingo Molnar48f24c42006-07-03 00:25:40 -07002919 */
Ingo Molnardd41f592007-07-09 18:51:59 +02002920static void update_cpu_load(struct rq *this_rq)
Ingo Molnar48f24c42006-07-03 00:25:40 -07002921{
Dmitry Adamushko495eca42007-10-15 17:00:06 +02002922 unsigned long this_load = this_rq->load.weight;
Ingo Molnardd41f592007-07-09 18:51:59 +02002923 int i, scale;
2924
2925 this_rq->nr_load_updates++;
Ingo Molnardd41f592007-07-09 18:51:59 +02002926
2927 /* Update our load: */
2928 for (i = 0, scale = 1; i < CPU_LOAD_IDX_MAX; i++, scale += scale) {
2929 unsigned long old_load, new_load;
2930
2931 /* scale is effectively 1 << i now, and >> i divides by scale */
2932
2933 old_load = this_rq->cpu_load[i];
2934 new_load = this_load;
Ingo Molnara25707f2007-10-15 17:00:03 +02002935 /*
2936 * Round up the averaging division if load is increasing. This
2937 * prevents us from getting stuck on 9 if the load is 10, for
2938 * example.
2939 */
2940 if (new_load > old_load)
2941 new_load += scale-1;
Ingo Molnardd41f592007-07-09 18:51:59 +02002942 this_rq->cpu_load[i] = (old_load*(scale-1) + new_load) >> i;
2943 }
Ingo Molnar48f24c42006-07-03 00:25:40 -07002944}
2945
Ingo Molnardd41f592007-07-09 18:51:59 +02002946#ifdef CONFIG_SMP
2947
Ingo Molnar48f24c42006-07-03 00:25:40 -07002948/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002949 * double_rq_lock - safely lock two runqueues
2950 *
2951 * Note this does not disable interrupts like task_rq_lock,
2952 * you need to do so manually before calling.
2953 */
Ingo Molnar70b97a72006-07-03 00:25:42 -07002954static void double_rq_lock(struct rq *rq1, struct rq *rq2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002955 __acquires(rq1->lock)
2956 __acquires(rq2->lock)
2957{
Kirill Korotaev054b9102006-12-10 02:20:11 -08002958 BUG_ON(!irqs_disabled());
Linus Torvalds1da177e2005-04-16 15:20:36 -07002959 if (rq1 == rq2) {
2960 spin_lock(&rq1->lock);
2961 __acquire(rq2->lock); /* Fake it out ;) */
2962 } else {
Chen, Kenneth Wc96d1452006-06-27 02:54:28 -07002963 if (rq1 < rq2) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002964 spin_lock(&rq1->lock);
Peter Zijlstra5e710e32008-07-30 13:26:57 +02002965 spin_lock_nested(&rq2->lock, SINGLE_DEPTH_NESTING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002966 } else {
2967 spin_lock(&rq2->lock);
Peter Zijlstra5e710e32008-07-30 13:26:57 +02002968 spin_lock_nested(&rq1->lock, SINGLE_DEPTH_NESTING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002969 }
2970 }
Ingo Molnar6e82a3b2007-08-09 11:16:51 +02002971 update_rq_clock(rq1);
2972 update_rq_clock(rq2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002973}
2974
2975/*
2976 * double_rq_unlock - safely unlock two runqueues
2977 *
2978 * Note this does not restore interrupts like task_rq_unlock,
2979 * you need to do so manually after calling.
2980 */
Ingo Molnar70b97a72006-07-03 00:25:42 -07002981static void double_rq_unlock(struct rq *rq1, struct rq *rq2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002982 __releases(rq1->lock)
2983 __releases(rq2->lock)
2984{
2985 spin_unlock(&rq1->lock);
2986 if (rq1 != rq2)
2987 spin_unlock(&rq2->lock);
2988 else
2989 __release(rq2->lock);
2990}
2991
2992/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002993 * If dest_cpu is allowed for this process, migrate the task to it.
2994 * This is accomplished by forcing the cpu_allowed mask to only
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01002995 * allow dest_cpu, which will force the cpu onto dest_cpu. Then
Linus Torvalds1da177e2005-04-16 15:20:36 -07002996 * the cpu_allowed mask is restored.
2997 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07002998static void sched_migrate_task(struct task_struct *p, int dest_cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002999{
Ingo Molnar70b97a72006-07-03 00:25:42 -07003000 struct migration_req req;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003001 unsigned long flags;
Ingo Molnar70b97a72006-07-03 00:25:42 -07003002 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003003
3004 rq = task_rq_lock(p, &flags);
Rusty Russell96f874e2008-11-25 02:35:14 +10303005 if (!cpumask_test_cpu(dest_cpu, &p->cpus_allowed)
Max Krasnyanskye761b772008-07-15 04:43:49 -07003006 || unlikely(!cpu_active(dest_cpu)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003007 goto out;
3008
3009 /* force the process onto the specified CPU */
3010 if (migrate_task(p, dest_cpu, &req)) {
3011 /* Need to wait for migration thread (might exit: take ref). */
3012 struct task_struct *mt = rq->migration_thread;
Ingo Molnar36c8b582006-07-03 00:25:41 -07003013
Linus Torvalds1da177e2005-04-16 15:20:36 -07003014 get_task_struct(mt);
3015 task_rq_unlock(rq, &flags);
3016 wake_up_process(mt);
3017 put_task_struct(mt);
3018 wait_for_completion(&req.done);
Ingo Molnar36c8b582006-07-03 00:25:41 -07003019
Linus Torvalds1da177e2005-04-16 15:20:36 -07003020 return;
3021 }
3022out:
3023 task_rq_unlock(rq, &flags);
3024}
3025
3026/*
Nick Piggin476d1392005-06-25 14:57:29 -07003027 * sched_exec - execve() is a valuable balancing opportunity, because at
3028 * this point the task has the smallest effective memory and cache footprint.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003029 */
3030void sched_exec(void)
3031{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003032 int new_cpu, this_cpu = get_cpu();
Nick Piggin476d1392005-06-25 14:57:29 -07003033 new_cpu = sched_balance_self(this_cpu, SD_BALANCE_EXEC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003034 put_cpu();
Nick Piggin476d1392005-06-25 14:57:29 -07003035 if (new_cpu != this_cpu)
3036 sched_migrate_task(current, new_cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003037}
3038
3039/*
3040 * pull_task - move a task from a remote runqueue to the local runqueue.
3041 * Both runqueues must be locked.
3042 */
Ingo Molnardd41f592007-07-09 18:51:59 +02003043static void pull_task(struct rq *src_rq, struct task_struct *p,
3044 struct rq *this_rq, int this_cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003045{
Ingo Molnar2e1cb742007-08-09 11:16:49 +02003046 deactivate_task(src_rq, p, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003047 set_task_cpu(p, this_cpu);
Ingo Molnardd41f592007-07-09 18:51:59 +02003048 activate_task(this_rq, p, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003049 /*
3050 * Note that idle threads have a prio of MAX_PRIO, for this test
3051 * to be always true for them.
3052 */
Peter Zijlstra15afe092008-09-20 23:38:02 +02003053 check_preempt_curr(this_rq, p, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003054}
3055
3056/*
3057 * can_migrate_task - may task p from runqueue rq be migrated to this_cpu?
3058 */
Arjan van de Ven858119e2006-01-14 13:20:43 -08003059static
Ingo Molnar70b97a72006-07-03 00:25:42 -07003060int can_migrate_task(struct task_struct *p, struct rq *rq, int this_cpu,
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003061 struct sched_domain *sd, enum cpu_idle_type idle,
Ingo Molnar95cdf3b2005-09-10 00:26:11 -07003062 int *all_pinned)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003063{
Luis Henriques708dc512009-03-16 19:59:02 +00003064 int tsk_cache_hot = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003065 /*
3066 * We do not migrate tasks that are:
3067 * 1) running (obviously), or
3068 * 2) cannot be migrated to this CPU due to cpus_allowed, or
3069 * 3) are cache-hot on their current CPU.
3070 */
Rusty Russell96f874e2008-11-25 02:35:14 +10303071 if (!cpumask_test_cpu(this_cpu, &p->cpus_allowed)) {
Ingo Molnarcc367732007-10-15 17:00:18 +02003072 schedstat_inc(p, se.nr_failed_migrations_affine);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003073 return 0;
Ingo Molnarcc367732007-10-15 17:00:18 +02003074 }
Nick Piggin81026792005-06-25 14:57:07 -07003075 *all_pinned = 0;
3076
Ingo Molnarcc367732007-10-15 17:00:18 +02003077 if (task_running(rq, p)) {
3078 schedstat_inc(p, se.nr_failed_migrations_running);
Nick Piggin81026792005-06-25 14:57:07 -07003079 return 0;
Ingo Molnarcc367732007-10-15 17:00:18 +02003080 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003081
Ingo Molnarda84d962007-10-15 17:00:18 +02003082 /*
3083 * Aggressive migration if:
3084 * 1) task is cache cold, or
3085 * 2) too many balance attempts have failed.
3086 */
3087
Luis Henriques708dc512009-03-16 19:59:02 +00003088 tsk_cache_hot = task_hot(p, rq->clock, sd);
3089 if (!tsk_cache_hot ||
3090 sd->nr_balance_failed > sd->cache_nice_tries) {
Ingo Molnarda84d962007-10-15 17:00:18 +02003091#ifdef CONFIG_SCHEDSTATS
Luis Henriques708dc512009-03-16 19:59:02 +00003092 if (tsk_cache_hot) {
Ingo Molnarda84d962007-10-15 17:00:18 +02003093 schedstat_inc(sd, lb_hot_gained[idle]);
Ingo Molnarcc367732007-10-15 17:00:18 +02003094 schedstat_inc(p, se.nr_forced_migrations);
3095 }
Ingo Molnarda84d962007-10-15 17:00:18 +02003096#endif
3097 return 1;
3098 }
3099
Luis Henriques708dc512009-03-16 19:59:02 +00003100 if (tsk_cache_hot) {
Ingo Molnarcc367732007-10-15 17:00:18 +02003101 schedstat_inc(p, se.nr_failed_migrations_hot);
Ingo Molnarda84d962007-10-15 17:00:18 +02003102 return 0;
Ingo Molnarcc367732007-10-15 17:00:18 +02003103 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003104 return 1;
3105}
3106
Peter Williamse1d14842007-10-24 18:23:51 +02003107static unsigned long
3108balance_tasks(struct rq *this_rq, int this_cpu, struct rq *busiest,
3109 unsigned long max_load_move, struct sched_domain *sd,
3110 enum cpu_idle_type idle, int *all_pinned,
3111 int *this_best_prio, struct rq_iterator *iterator)
Ingo Molnardd41f592007-07-09 18:51:59 +02003112{
Peter Zijlstra051c6762008-06-27 13:41:31 +02003113 int loops = 0, pulled = 0, pinned = 0;
Ingo Molnardd41f592007-07-09 18:51:59 +02003114 struct task_struct *p;
3115 long rem_load_move = max_load_move;
3116
Peter Williamse1d14842007-10-24 18:23:51 +02003117 if (max_load_move == 0)
Ingo Molnardd41f592007-07-09 18:51:59 +02003118 goto out;
3119
3120 pinned = 1;
3121
3122 /*
3123 * Start the load-balancing iterator:
3124 */
3125 p = iterator->start(iterator->arg);
3126next:
Peter Zijlstrab82d9fd2007-11-09 22:39:39 +01003127 if (!p || loops++ > sysctl_sched_nr_migrate)
Ingo Molnardd41f592007-07-09 18:51:59 +02003128 goto out;
Peter Zijlstra051c6762008-06-27 13:41:31 +02003129
3130 if ((p->se.load.weight >> 1) > rem_load_move ||
Ingo Molnardd41f592007-07-09 18:51:59 +02003131 !can_migrate_task(p, busiest, this_cpu, sd, idle, &pinned)) {
Ingo Molnardd41f592007-07-09 18:51:59 +02003132 p = iterator->next(iterator->arg);
3133 goto next;
3134 }
3135
3136 pull_task(busiest, p, this_rq, this_cpu);
3137 pulled++;
3138 rem_load_move -= p->se.load.weight;
3139
Gregory Haskins7e96fa52008-12-29 09:39:50 -05003140#ifdef CONFIG_PREEMPT
3141 /*
3142 * NEWIDLE balancing is a source of latency, so preemptible kernels
3143 * will stop after the first task is pulled to minimize the critical
3144 * section.
3145 */
3146 if (idle == CPU_NEWLY_IDLE)
3147 goto out;
3148#endif
3149
Ingo Molnardd41f592007-07-09 18:51:59 +02003150 /*
Peter Zijlstrab82d9fd2007-11-09 22:39:39 +01003151 * We only want to steal up to the prescribed amount of weighted load.
Ingo Molnardd41f592007-07-09 18:51:59 +02003152 */
Peter Williamse1d14842007-10-24 18:23:51 +02003153 if (rem_load_move > 0) {
Peter Williamsa4ac01c2007-08-09 11:16:46 +02003154 if (p->prio < *this_best_prio)
3155 *this_best_prio = p->prio;
Ingo Molnardd41f592007-07-09 18:51:59 +02003156 p = iterator->next(iterator->arg);
3157 goto next;
3158 }
3159out:
3160 /*
Peter Williamse1d14842007-10-24 18:23:51 +02003161 * Right now, this is one of only two places pull_task() is called,
Ingo Molnardd41f592007-07-09 18:51:59 +02003162 * so we can safely collect pull_task() stats here rather than
3163 * inside pull_task().
3164 */
3165 schedstat_add(sd, lb_gained[idle], pulled);
3166
3167 if (all_pinned)
3168 *all_pinned = pinned;
Peter Williamse1d14842007-10-24 18:23:51 +02003169
3170 return max_load_move - rem_load_move;
Ingo Molnardd41f592007-07-09 18:51:59 +02003171}
Ingo Molnar48f24c42006-07-03 00:25:40 -07003172
Linus Torvalds1da177e2005-04-16 15:20:36 -07003173/*
Peter Williams43010652007-08-09 11:16:46 +02003174 * move_tasks tries to move up to max_load_move weighted load from busiest to
3175 * this_rq, as part of a balancing operation within domain "sd".
3176 * Returns 1 if successful and 0 otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003177 *
3178 * Called with both runqueues locked.
3179 */
Ingo Molnar70b97a72006-07-03 00:25:42 -07003180static int move_tasks(struct rq *this_rq, int this_cpu, struct rq *busiest,
Peter Williams43010652007-08-09 11:16:46 +02003181 unsigned long max_load_move,
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003182 struct sched_domain *sd, enum cpu_idle_type idle,
Peter Williams2dd73a42006-06-27 02:54:34 -07003183 int *all_pinned)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003184{
Ingo Molnar5522d5d2007-10-15 17:00:12 +02003185 const struct sched_class *class = sched_class_highest;
Peter Williams43010652007-08-09 11:16:46 +02003186 unsigned long total_load_moved = 0;
Peter Williamsa4ac01c2007-08-09 11:16:46 +02003187 int this_best_prio = this_rq->curr->prio;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003188
Ingo Molnardd41f592007-07-09 18:51:59 +02003189 do {
Peter Williams43010652007-08-09 11:16:46 +02003190 total_load_moved +=
3191 class->load_balance(this_rq, this_cpu, busiest,
Peter Williamse1d14842007-10-24 18:23:51 +02003192 max_load_move - total_load_moved,
Peter Williamsa4ac01c2007-08-09 11:16:46 +02003193 sd, idle, all_pinned, &this_best_prio);
Ingo Molnardd41f592007-07-09 18:51:59 +02003194 class = class->next;
Gregory Haskinsc4acb2c2008-06-27 14:29:55 -06003195
Gregory Haskins7e96fa52008-12-29 09:39:50 -05003196#ifdef CONFIG_PREEMPT
3197 /*
3198 * NEWIDLE balancing is a source of latency, so preemptible
3199 * kernels will stop after the first task is pulled to minimize
3200 * the critical section.
3201 */
Gregory Haskinsc4acb2c2008-06-27 14:29:55 -06003202 if (idle == CPU_NEWLY_IDLE && this_rq->nr_running)
3203 break;
Gregory Haskins7e96fa52008-12-29 09:39:50 -05003204#endif
Peter Williams43010652007-08-09 11:16:46 +02003205 } while (class && max_load_move > total_load_moved);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003206
Peter Williams43010652007-08-09 11:16:46 +02003207 return total_load_moved > 0;
3208}
3209
Peter Williamse1d14842007-10-24 18:23:51 +02003210static int
3211iter_move_one_task(struct rq *this_rq, int this_cpu, struct rq *busiest,
3212 struct sched_domain *sd, enum cpu_idle_type idle,
3213 struct rq_iterator *iterator)
3214{
3215 struct task_struct *p = iterator->start(iterator->arg);
3216 int pinned = 0;
3217
3218 while (p) {
3219 if (can_migrate_task(p, busiest, this_cpu, sd, idle, &pinned)) {
3220 pull_task(busiest, p, this_rq, this_cpu);
3221 /*
3222 * Right now, this is only the second place pull_task()
3223 * is called, so we can safely collect pull_task()
3224 * stats here rather than inside pull_task().
3225 */
3226 schedstat_inc(sd, lb_gained[idle]);
3227
3228 return 1;
3229 }
3230 p = iterator->next(iterator->arg);
3231 }
3232
3233 return 0;
3234}
3235
Peter Williams43010652007-08-09 11:16:46 +02003236/*
3237 * move_one_task tries to move exactly one task from busiest to this_rq, as
3238 * part of active balancing operations within "domain".
3239 * Returns 1 if successful and 0 otherwise.
3240 *
3241 * Called with both runqueues locked.
3242 */
3243static int move_one_task(struct rq *this_rq, int this_cpu, struct rq *busiest,
3244 struct sched_domain *sd, enum cpu_idle_type idle)
3245{
Ingo Molnar5522d5d2007-10-15 17:00:12 +02003246 const struct sched_class *class;
Peter Williams43010652007-08-09 11:16:46 +02003247
3248 for (class = sched_class_highest; class; class = class->next)
Peter Williamse1d14842007-10-24 18:23:51 +02003249 if (class->move_one_task(this_rq, this_cpu, busiest, sd, idle))
Peter Williams43010652007-08-09 11:16:46 +02003250 return 1;
3251
3252 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003253}
Gautham R Shenoy67bb6c02009-03-25 14:43:35 +05303254/********** Helpers for find_busiest_group ************************/
Randy Dunlapd5ac5372009-03-28 21:52:47 -07003255/*
Gautham R Shenoy222d6562009-03-25 14:43:56 +05303256 * sd_lb_stats - Structure to store the statistics of a sched_domain
3257 * during load balancing.
3258 */
3259struct sd_lb_stats {
3260 struct sched_group *busiest; /* Busiest group in this sd */
3261 struct sched_group *this; /* Local group in this sd */
3262 unsigned long total_load; /* Total load of all groups in sd */
3263 unsigned long total_pwr; /* Total power of all groups in sd */
3264 unsigned long avg_load; /* Average load across all groups in sd */
3265
3266 /** Statistics of this group */
3267 unsigned long this_load;
3268 unsigned long this_load_per_task;
3269 unsigned long this_nr_running;
3270
3271 /* Statistics of the busiest group */
3272 unsigned long max_load;
3273 unsigned long busiest_load_per_task;
3274 unsigned long busiest_nr_running;
3275
3276 int group_imb; /* Is there imbalance in this sd */
3277#if defined(CONFIG_SCHED_MC) || defined(CONFIG_SCHED_SMT)
3278 int power_savings_balance; /* Is powersave balance needed for this sd */
3279 struct sched_group *group_min; /* Least loaded group in sd */
3280 struct sched_group *group_leader; /* Group which relieves group_min */
3281 unsigned long min_load_per_task; /* load_per_task in group_min */
3282 unsigned long leader_nr_running; /* Nr running of group_leader */
3283 unsigned long min_nr_running; /* Nr running of group_min */
3284#endif
3285};
Linus Torvalds1da177e2005-04-16 15:20:36 -07003286
3287/*
Gautham R Shenoy381be782009-03-25 14:43:46 +05303288 * sg_lb_stats - stats of a sched_group required for load_balancing
3289 */
3290struct sg_lb_stats {
3291 unsigned long avg_load; /*Avg load across the CPUs of the group */
3292 unsigned long group_load; /* Total load over the CPUs of the group */
3293 unsigned long sum_nr_running; /* Nr tasks running in the group */
3294 unsigned long sum_weighted_load; /* Weighted load of group's tasks */
3295 unsigned long group_capacity;
3296 int group_imb; /* Is there an imbalance in the group ? */
3297};
3298
3299/**
Gautham R Shenoy67bb6c02009-03-25 14:43:35 +05303300 * group_first_cpu - Returns the first cpu in the cpumask of a sched_group.
3301 * @group: The group whose first cpu is to be returned.
3302 */
3303static inline unsigned int group_first_cpu(struct sched_group *group)
3304{
3305 return cpumask_first(sched_group_cpus(group));
3306}
3307
3308/**
3309 * get_sd_load_idx - Obtain the load index for a given sched domain.
3310 * @sd: The sched_domain whose load_idx is to be obtained.
3311 * @idle: The Idle status of the CPU for whose sd load_icx is obtained.
3312 */
3313static inline int get_sd_load_idx(struct sched_domain *sd,
3314 enum cpu_idle_type idle)
3315{
3316 int load_idx;
3317
3318 switch (idle) {
3319 case CPU_NOT_IDLE:
3320 load_idx = sd->busy_idx;
3321 break;
3322
3323 case CPU_NEWLY_IDLE:
3324 load_idx = sd->newidle_idx;
3325 break;
3326 default:
3327 load_idx = sd->idle_idx;
3328 break;
3329 }
3330
3331 return load_idx;
3332}
Gautham R Shenoy1f8c5532009-03-25 14:43:51 +05303333
3334
Gautham R Shenoyc071df12009-03-25 14:44:22 +05303335#if defined(CONFIG_SCHED_MC) || defined(CONFIG_SCHED_SMT)
3336/**
3337 * init_sd_power_savings_stats - Initialize power savings statistics for
3338 * the given sched_domain, during load balancing.
3339 *
3340 * @sd: Sched domain whose power-savings statistics are to be initialized.
3341 * @sds: Variable containing the statistics for sd.
3342 * @idle: Idle status of the CPU at which we're performing load-balancing.
3343 */
3344static inline void init_sd_power_savings_stats(struct sched_domain *sd,
3345 struct sd_lb_stats *sds, enum cpu_idle_type idle)
3346{
3347 /*
3348 * Busy processors will not participate in power savings
3349 * balance.
3350 */
3351 if (idle == CPU_NOT_IDLE || !(sd->flags & SD_POWERSAVINGS_BALANCE))
3352 sds->power_savings_balance = 0;
3353 else {
3354 sds->power_savings_balance = 1;
3355 sds->min_nr_running = ULONG_MAX;
3356 sds->leader_nr_running = 0;
3357 }
3358}
3359
3360/**
3361 * update_sd_power_savings_stats - Update the power saving stats for a
3362 * sched_domain while performing load balancing.
3363 *
3364 * @group: sched_group belonging to the sched_domain under consideration.
3365 * @sds: Variable containing the statistics of the sched_domain
3366 * @local_group: Does group contain the CPU for which we're performing
3367 * load balancing ?
3368 * @sgs: Variable containing the statistics of the group.
3369 */
3370static inline void update_sd_power_savings_stats(struct sched_group *group,
3371 struct sd_lb_stats *sds, int local_group, struct sg_lb_stats *sgs)
3372{
3373
3374 if (!sds->power_savings_balance)
3375 return;
3376
3377 /*
3378 * If the local group is idle or completely loaded
3379 * no need to do power savings balance at this domain
3380 */
3381 if (local_group && (sds->this_nr_running >= sgs->group_capacity ||
3382 !sds->this_nr_running))
3383 sds->power_savings_balance = 0;
3384
3385 /*
3386 * If a group is already running at full capacity or idle,
3387 * don't include that group in power savings calculations
3388 */
3389 if (!sds->power_savings_balance ||
3390 sgs->sum_nr_running >= sgs->group_capacity ||
3391 !sgs->sum_nr_running)
3392 return;
3393
3394 /*
3395 * Calculate the group which has the least non-idle load.
3396 * This is the group from where we need to pick up the load
3397 * for saving power
3398 */
3399 if ((sgs->sum_nr_running < sds->min_nr_running) ||
3400 (sgs->sum_nr_running == sds->min_nr_running &&
3401 group_first_cpu(group) > group_first_cpu(sds->group_min))) {
3402 sds->group_min = group;
3403 sds->min_nr_running = sgs->sum_nr_running;
3404 sds->min_load_per_task = sgs->sum_weighted_load /
3405 sgs->sum_nr_running;
3406 }
3407
3408 /*
3409 * Calculate the group which is almost near its
3410 * capacity but still has some space to pick up some load
3411 * from other group and save more power
3412 */
3413 if (sgs->sum_nr_running > sgs->group_capacity - 1)
3414 return;
3415
3416 if (sgs->sum_nr_running > sds->leader_nr_running ||
3417 (sgs->sum_nr_running == sds->leader_nr_running &&
3418 group_first_cpu(group) < group_first_cpu(sds->group_leader))) {
3419 sds->group_leader = group;
3420 sds->leader_nr_running = sgs->sum_nr_running;
3421 }
3422}
3423
3424/**
Randy Dunlapd5ac5372009-03-28 21:52:47 -07003425 * check_power_save_busiest_group - see if there is potential for some power-savings balance
Gautham R Shenoyc071df12009-03-25 14:44:22 +05303426 * @sds: Variable containing the statistics of the sched_domain
3427 * under consideration.
3428 * @this_cpu: Cpu at which we're currently performing load-balancing.
3429 * @imbalance: Variable to store the imbalance.
3430 *
Randy Dunlapd5ac5372009-03-28 21:52:47 -07003431 * Description:
3432 * Check if we have potential to perform some power-savings balance.
3433 * If yes, set the busiest group to be the least loaded group in the
3434 * sched_domain, so that it's CPUs can be put to idle.
3435 *
Gautham R Shenoyc071df12009-03-25 14:44:22 +05303436 * Returns 1 if there is potential to perform power-savings balance.
3437 * Else returns 0.
3438 */
3439static inline int check_power_save_busiest_group(struct sd_lb_stats *sds,
3440 int this_cpu, unsigned long *imbalance)
3441{
3442 if (!sds->power_savings_balance)
3443 return 0;
3444
3445 if (sds->this != sds->group_leader ||
3446 sds->group_leader == sds->group_min)
3447 return 0;
3448
3449 *imbalance = sds->min_load_per_task;
3450 sds->busiest = sds->group_min;
3451
3452 if (sched_mc_power_savings >= POWERSAVINGS_BALANCE_WAKEUP) {
3453 cpu_rq(this_cpu)->rd->sched_mc_preferred_wakeup_cpu =
3454 group_first_cpu(sds->group_leader);
3455 }
3456
3457 return 1;
3458
3459}
3460#else /* CONFIG_SCHED_MC || CONFIG_SCHED_SMT */
3461static inline void init_sd_power_savings_stats(struct sched_domain *sd,
3462 struct sd_lb_stats *sds, enum cpu_idle_type idle)
3463{
3464 return;
3465}
3466
3467static inline void update_sd_power_savings_stats(struct sched_group *group,
3468 struct sd_lb_stats *sds, int local_group, struct sg_lb_stats *sgs)
3469{
3470 return;
3471}
3472
3473static inline int check_power_save_busiest_group(struct sd_lb_stats *sds,
3474 int this_cpu, unsigned long *imbalance)
3475{
3476 return 0;
3477}
3478#endif /* CONFIG_SCHED_MC || CONFIG_SCHED_SMT */
3479
3480
Gautham R Shenoy1f8c5532009-03-25 14:43:51 +05303481/**
3482 * update_sg_lb_stats - Update sched_group's statistics for load balancing.
3483 * @group: sched_group whose statistics are to be updated.
3484 * @this_cpu: Cpu for which load balance is currently performed.
3485 * @idle: Idle status of this_cpu
3486 * @load_idx: Load index of sched_domain of this_cpu for load calc.
3487 * @sd_idle: Idle status of the sched_domain containing group.
3488 * @local_group: Does group contain this_cpu.
3489 * @cpus: Set of cpus considered for load balancing.
3490 * @balance: Should we balance.
3491 * @sgs: variable to hold the statistics for this group.
3492 */
3493static inline void update_sg_lb_stats(struct sched_group *group, int this_cpu,
3494 enum cpu_idle_type idle, int load_idx, int *sd_idle,
3495 int local_group, const struct cpumask *cpus,
3496 int *balance, struct sg_lb_stats *sgs)
3497{
3498 unsigned long load, max_cpu_load, min_cpu_load;
3499 int i;
3500 unsigned int balance_cpu = -1, first_idle_cpu = 0;
3501 unsigned long sum_avg_load_per_task;
3502 unsigned long avg_load_per_task;
3503
3504 if (local_group)
3505 balance_cpu = group_first_cpu(group);
3506
3507 /* Tally up the load of all CPUs in the group */
3508 sum_avg_load_per_task = avg_load_per_task = 0;
3509 max_cpu_load = 0;
3510 min_cpu_load = ~0UL;
3511
3512 for_each_cpu_and(i, sched_group_cpus(group), cpus) {
3513 struct rq *rq = cpu_rq(i);
3514
3515 if (*sd_idle && rq->nr_running)
3516 *sd_idle = 0;
3517
3518 /* Bias balancing toward cpus of our domain */
3519 if (local_group) {
3520 if (idle_cpu(i) && !first_idle_cpu) {
3521 first_idle_cpu = 1;
3522 balance_cpu = i;
3523 }
3524
3525 load = target_load(i, load_idx);
3526 } else {
3527 load = source_load(i, load_idx);
3528 if (load > max_cpu_load)
3529 max_cpu_load = load;
3530 if (min_cpu_load > load)
3531 min_cpu_load = load;
3532 }
3533
3534 sgs->group_load += load;
3535 sgs->sum_nr_running += rq->nr_running;
3536 sgs->sum_weighted_load += weighted_cpuload(i);
3537
3538 sum_avg_load_per_task += cpu_avg_load_per_task(i);
3539 }
3540
3541 /*
3542 * First idle cpu or the first cpu(busiest) in this sched group
3543 * is eligible for doing load balancing at this and above
3544 * domains. In the newly idle case, we will allow all the cpu's
3545 * to do the newly idle load balance.
3546 */
3547 if (idle != CPU_NEWLY_IDLE && local_group &&
3548 balance_cpu != this_cpu && balance) {
3549 *balance = 0;
3550 return;
3551 }
3552
3553 /* Adjust by relative CPU power of the group */
3554 sgs->avg_load = sg_div_cpu_power(group,
3555 sgs->group_load * SCHED_LOAD_SCALE);
3556
3557
3558 /*
3559 * Consider the group unbalanced when the imbalance is larger
3560 * than the average weight of two tasks.
3561 *
3562 * APZ: with cgroup the avg task weight can vary wildly and
3563 * might not be a suitable number - should we keep a
3564 * normalized nr_running number somewhere that negates
3565 * the hierarchy?
3566 */
3567 avg_load_per_task = sg_div_cpu_power(group,
3568 sum_avg_load_per_task * SCHED_LOAD_SCALE);
3569
3570 if ((max_cpu_load - min_cpu_load) > 2*avg_load_per_task)
3571 sgs->group_imb = 1;
3572
3573 sgs->group_capacity = group->__cpu_power / SCHED_LOAD_SCALE;
3574
3575}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003576
Gautham R Shenoy37abe192009-03-25 14:44:01 +05303577/**
3578 * update_sd_lb_stats - Update sched_group's statistics for load balancing.
3579 * @sd: sched_domain whose statistics are to be updated.
3580 * @this_cpu: Cpu for which load balance is currently performed.
3581 * @idle: Idle status of this_cpu
3582 * @sd_idle: Idle status of the sched_domain containing group.
3583 * @cpus: Set of cpus considered for load balancing.
3584 * @balance: Should we balance.
3585 * @sds: variable to hold the statistics for this sched_domain.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003586 */
Gautham R Shenoy37abe192009-03-25 14:44:01 +05303587static inline void update_sd_lb_stats(struct sched_domain *sd, int this_cpu,
3588 enum cpu_idle_type idle, int *sd_idle,
3589 const struct cpumask *cpus, int *balance,
3590 struct sd_lb_stats *sds)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003591{
Gautham R Shenoy222d6562009-03-25 14:43:56 +05303592 struct sched_group *group = sd->groups;
Gautham R Shenoy37abe192009-03-25 14:44:01 +05303593 struct sg_lb_stats sgs;
Gautham R Shenoy222d6562009-03-25 14:43:56 +05303594 int load_idx;
3595
Gautham R Shenoyc071df12009-03-25 14:44:22 +05303596 init_sd_power_savings_stats(sd, sds, idle);
Gautham R Shenoy67bb6c02009-03-25 14:43:35 +05303597 load_idx = get_sd_load_idx(sd, idle);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003598
3599 do {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003600 int local_group;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003601
Rusty Russell758b2cd2008-11-25 02:35:04 +10303602 local_group = cpumask_test_cpu(this_cpu,
3603 sched_group_cpus(group));
Gautham R Shenoy381be782009-03-25 14:43:46 +05303604 memset(&sgs, 0, sizeof(sgs));
Gautham R Shenoy1f8c5532009-03-25 14:43:51 +05303605 update_sg_lb_stats(group, this_cpu, idle, load_idx, sd_idle,
3606 local_group, cpus, balance, &sgs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003607
Gautham R Shenoy37abe192009-03-25 14:44:01 +05303608 if (local_group && balance && !(*balance))
3609 return;
Siddha, Suresh B783609c2006-12-10 02:20:33 -08003610
Gautham R Shenoy37abe192009-03-25 14:44:01 +05303611 sds->total_load += sgs.group_load;
3612 sds->total_pwr += group->__cpu_power;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003613
Linus Torvalds1da177e2005-04-16 15:20:36 -07003614 if (local_group) {
Gautham R Shenoy37abe192009-03-25 14:44:01 +05303615 sds->this_load = sgs.avg_load;
3616 sds->this = group;
3617 sds->this_nr_running = sgs.sum_nr_running;
3618 sds->this_load_per_task = sgs.sum_weighted_load;
3619 } else if (sgs.avg_load > sds->max_load &&
Gautham R Shenoy381be782009-03-25 14:43:46 +05303620 (sgs.sum_nr_running > sgs.group_capacity ||
3621 sgs.group_imb)) {
Gautham R Shenoy37abe192009-03-25 14:44:01 +05303622 sds->max_load = sgs.avg_load;
3623 sds->busiest = group;
3624 sds->busiest_nr_running = sgs.sum_nr_running;
3625 sds->busiest_load_per_task = sgs.sum_weighted_load;
3626 sds->group_imb = sgs.group_imb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003627 }
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07003628
Gautham R Shenoyc071df12009-03-25 14:44:22 +05303629 update_sd_power_savings_stats(group, sds, local_group, &sgs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003630 group = group->next;
3631 } while (group != sd->groups);
3632
Gautham R Shenoy37abe192009-03-25 14:44:01 +05303633}
Gautham R Shenoy2e6f44a2009-03-25 14:44:06 +05303634
3635/**
3636 * fix_small_imbalance - Calculate the minor imbalance that exists
Gautham R Shenoydbc523a2009-03-25 14:44:12 +05303637 * amongst the groups of a sched_domain, during
3638 * load balancing.
Gautham R Shenoy2e6f44a2009-03-25 14:44:06 +05303639 * @sds: Statistics of the sched_domain whose imbalance is to be calculated.
3640 * @this_cpu: The cpu at whose sched_domain we're performing load-balance.
3641 * @imbalance: Variable to store the imbalance.
3642 */
3643static inline void fix_small_imbalance(struct sd_lb_stats *sds,
3644 int this_cpu, unsigned long *imbalance)
3645{
3646 unsigned long tmp, pwr_now = 0, pwr_move = 0;
3647 unsigned int imbn = 2;
3648
3649 if (sds->this_nr_running) {
3650 sds->this_load_per_task /= sds->this_nr_running;
3651 if (sds->busiest_load_per_task >
3652 sds->this_load_per_task)
3653 imbn = 1;
3654 } else
3655 sds->this_load_per_task =
3656 cpu_avg_load_per_task(this_cpu);
3657
3658 if (sds->max_load - sds->this_load + sds->busiest_load_per_task >=
3659 sds->busiest_load_per_task * imbn) {
3660 *imbalance = sds->busiest_load_per_task;
3661 return;
3662 }
3663
3664 /*
3665 * OK, we don't have enough imbalance to justify moving tasks,
3666 * however we may be able to increase total CPU power used by
3667 * moving them.
3668 */
3669
3670 pwr_now += sds->busiest->__cpu_power *
3671 min(sds->busiest_load_per_task, sds->max_load);
3672 pwr_now += sds->this->__cpu_power *
3673 min(sds->this_load_per_task, sds->this_load);
3674 pwr_now /= SCHED_LOAD_SCALE;
3675
3676 /* Amount of load we'd subtract */
3677 tmp = sg_div_cpu_power(sds->busiest,
3678 sds->busiest_load_per_task * SCHED_LOAD_SCALE);
3679 if (sds->max_load > tmp)
3680 pwr_move += sds->busiest->__cpu_power *
3681 min(sds->busiest_load_per_task, sds->max_load - tmp);
3682
3683 /* Amount of load we'd add */
3684 if (sds->max_load * sds->busiest->__cpu_power <
3685 sds->busiest_load_per_task * SCHED_LOAD_SCALE)
3686 tmp = sg_div_cpu_power(sds->this,
3687 sds->max_load * sds->busiest->__cpu_power);
3688 else
3689 tmp = sg_div_cpu_power(sds->this,
3690 sds->busiest_load_per_task * SCHED_LOAD_SCALE);
3691 pwr_move += sds->this->__cpu_power *
3692 min(sds->this_load_per_task, sds->this_load + tmp);
3693 pwr_move /= SCHED_LOAD_SCALE;
3694
3695 /* Move if we gain throughput */
3696 if (pwr_move > pwr_now)
3697 *imbalance = sds->busiest_load_per_task;
3698}
Gautham R Shenoydbc523a2009-03-25 14:44:12 +05303699
3700/**
3701 * calculate_imbalance - Calculate the amount of imbalance present within the
3702 * groups of a given sched_domain during load balance.
3703 * @sds: statistics of the sched_domain whose imbalance is to be calculated.
3704 * @this_cpu: Cpu for which currently load balance is being performed.
3705 * @imbalance: The variable to store the imbalance.
3706 */
3707static inline void calculate_imbalance(struct sd_lb_stats *sds, int this_cpu,
3708 unsigned long *imbalance)
3709{
3710 unsigned long max_pull;
3711 /*
3712 * In the presence of smp nice balancing, certain scenarios can have
3713 * max load less than avg load(as we skip the groups at or below
3714 * its cpu_power, while calculating max_load..)
3715 */
3716 if (sds->max_load < sds->avg_load) {
3717 *imbalance = 0;
3718 return fix_small_imbalance(sds, this_cpu, imbalance);
3719 }
3720
3721 /* Don't want to pull so many tasks that a group would go idle */
3722 max_pull = min(sds->max_load - sds->avg_load,
3723 sds->max_load - sds->busiest_load_per_task);
3724
3725 /* How much load to actually move to equalise the imbalance */
3726 *imbalance = min(max_pull * sds->busiest->__cpu_power,
3727 (sds->avg_load - sds->this_load) * sds->this->__cpu_power)
3728 / SCHED_LOAD_SCALE;
3729
3730 /*
3731 * if *imbalance is less than the average load per runnable task
3732 * there is no gaurantee that any tasks will be moved so we'll have
3733 * a think about bumping its value to force at least one task to be
3734 * moved
3735 */
3736 if (*imbalance < sds->busiest_load_per_task)
3737 return fix_small_imbalance(sds, this_cpu, imbalance);
3738
3739}
Gautham R Shenoy37abe192009-03-25 14:44:01 +05303740/******* find_busiest_group() helpers end here *********************/
3741
Gautham R Shenoyb7bb4c92009-03-25 14:44:27 +05303742/**
3743 * find_busiest_group - Returns the busiest group within the sched_domain
3744 * if there is an imbalance. If there isn't an imbalance, and
3745 * the user has opted for power-savings, it returns a group whose
3746 * CPUs can be put to idle by rebalancing those tasks elsewhere, if
3747 * such a group exists.
3748 *
3749 * Also calculates the amount of weighted load which should be moved
3750 * to restore balance.
3751 *
3752 * @sd: The sched_domain whose busiest group is to be returned.
3753 * @this_cpu: The cpu for which load balancing is currently being performed.
3754 * @imbalance: Variable which stores amount of weighted load which should
3755 * be moved to restore balance/put a group to idle.
3756 * @idle: The idle status of this_cpu.
3757 * @sd_idle: The idleness of sd
3758 * @cpus: The set of CPUs under consideration for load-balancing.
3759 * @balance: Pointer to a variable indicating if this_cpu
3760 * is the appropriate cpu to perform load balancing at this_level.
3761 *
3762 * Returns: - the busiest group if imbalance exists.
3763 * - If no imbalance and user has opted for power-savings balance,
3764 * return the least loaded group whose CPUs can be
3765 * put to idle by rebalancing its tasks onto our group.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003766 */
3767static struct sched_group *
3768find_busiest_group(struct sched_domain *sd, int this_cpu,
3769 unsigned long *imbalance, enum cpu_idle_type idle,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003770 int *sd_idle, const struct cpumask *cpus, int *balance)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003771{
Gautham R Shenoy37abe192009-03-25 14:44:01 +05303772 struct sd_lb_stats sds;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003773
Gautham R Shenoy37abe192009-03-25 14:44:01 +05303774 memset(&sds, 0, sizeof(sds));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003775
Gautham R Shenoy37abe192009-03-25 14:44:01 +05303776 /*
3777 * Compute the various statistics relavent for load balancing at
3778 * this level.
3779 */
3780 update_sd_lb_stats(sd, this_cpu, idle, sd_idle, cpus,
3781 balance, &sds);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003782
Gautham R Shenoyb7bb4c92009-03-25 14:44:27 +05303783 /* Cases where imbalance does not exist from POV of this_cpu */
3784 /* 1) this_cpu is not the appropriate cpu to perform load balancing
3785 * at this level.
3786 * 2) There is no busy sibling group to pull from.
3787 * 3) This group is the busiest group.
3788 * 4) This group is more busy than the avg busieness at this
3789 * sched_domain.
3790 * 5) The imbalance is within the specified limit.
3791 * 6) Any rebalance would lead to ping-pong
3792 */
Gautham R Shenoy37abe192009-03-25 14:44:01 +05303793 if (balance && !(*balance))
3794 goto ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003795
Gautham R Shenoyb7bb4c92009-03-25 14:44:27 +05303796 if (!sds.busiest || sds.busiest_nr_running == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003797 goto out_balanced;
3798
Gautham R Shenoyb7bb4c92009-03-25 14:44:27 +05303799 if (sds.this_load >= sds.max_load)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003800 goto out_balanced;
3801
Gautham R Shenoy222d6562009-03-25 14:43:56 +05303802 sds.avg_load = (SCHED_LOAD_SCALE * sds.total_load) / sds.total_pwr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003803
Gautham R Shenoyb7bb4c92009-03-25 14:44:27 +05303804 if (sds.this_load >= sds.avg_load)
3805 goto out_balanced;
3806
3807 if (100 * sds.max_load <= sd->imbalance_pct * sds.this_load)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003808 goto out_balanced;
3809
Gautham R Shenoy222d6562009-03-25 14:43:56 +05303810 sds.busiest_load_per_task /= sds.busiest_nr_running;
3811 if (sds.group_imb)
3812 sds.busiest_load_per_task =
3813 min(sds.busiest_load_per_task, sds.avg_load);
Ken Chen908a7c12007-10-17 16:55:11 +02003814
Linus Torvalds1da177e2005-04-16 15:20:36 -07003815 /*
3816 * We're trying to get all the cpus to the average_load, so we don't
3817 * want to push ourselves above the average load, nor do we wish to
3818 * reduce the max loaded cpu below the average load, as either of these
3819 * actions would just result in more rebalancing later, and ping-pong
3820 * tasks around. Thus we look for the minimum possible imbalance.
3821 * Negative imbalances (*we* are more loaded than anyone else) will
3822 * be counted as no imbalance for these purposes -- we can't fix that
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01003823 * by pulling tasks to us. Be careful of negative numbers as they'll
Linus Torvalds1da177e2005-04-16 15:20:36 -07003824 * appear as very large values with unsigned longs.
3825 */
Gautham R Shenoy222d6562009-03-25 14:43:56 +05303826 if (sds.max_load <= sds.busiest_load_per_task)
Peter Williams2dd73a42006-06-27 02:54:34 -07003827 goto out_balanced;
3828
Gautham R Shenoydbc523a2009-03-25 14:44:12 +05303829 /* Looks like there is an imbalance. Compute it */
3830 calculate_imbalance(&sds, this_cpu, imbalance);
Gautham R Shenoy222d6562009-03-25 14:43:56 +05303831 return sds.busiest;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003832
3833out_balanced:
Gautham R Shenoyc071df12009-03-25 14:44:22 +05303834 /*
3835 * There is no obvious imbalance. But check if we can do some balancing
3836 * to save power.
3837 */
3838 if (check_power_save_busiest_group(&sds, this_cpu, imbalance))
3839 return sds.busiest;
Siddha, Suresh B783609c2006-12-10 02:20:33 -08003840ret:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003841 *imbalance = 0;
3842 return NULL;
3843}
3844
3845/*
3846 * find_busiest_queue - find the busiest runqueue among the cpus in group.
3847 */
Ingo Molnar70b97a72006-07-03 00:25:42 -07003848static struct rq *
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003849find_busiest_queue(struct sched_group *group, enum cpu_idle_type idle,
Rusty Russell96f874e2008-11-25 02:35:14 +10303850 unsigned long imbalance, const struct cpumask *cpus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003851{
Ingo Molnar70b97a72006-07-03 00:25:42 -07003852 struct rq *busiest = NULL, *rq;
Peter Williams2dd73a42006-06-27 02:54:34 -07003853 unsigned long max_load = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003854 int i;
3855
Rusty Russell758b2cd2008-11-25 02:35:04 +10303856 for_each_cpu(i, sched_group_cpus(group)) {
Ingo Molnardd41f592007-07-09 18:51:59 +02003857 unsigned long wl;
Christoph Lameter0a2966b2006-09-25 23:30:51 -07003858
Rusty Russell96f874e2008-11-25 02:35:14 +10303859 if (!cpumask_test_cpu(i, cpus))
Christoph Lameter0a2966b2006-09-25 23:30:51 -07003860 continue;
3861
Ingo Molnar48f24c42006-07-03 00:25:40 -07003862 rq = cpu_rq(i);
Ingo Molnardd41f592007-07-09 18:51:59 +02003863 wl = weighted_cpuload(i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003864
Ingo Molnardd41f592007-07-09 18:51:59 +02003865 if (rq->nr_running == 1 && wl > imbalance)
Peter Williams2dd73a42006-06-27 02:54:34 -07003866 continue;
3867
Ingo Molnardd41f592007-07-09 18:51:59 +02003868 if (wl > max_load) {
3869 max_load = wl;
Ingo Molnar48f24c42006-07-03 00:25:40 -07003870 busiest = rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003871 }
3872 }
3873
3874 return busiest;
3875}
3876
3877/*
Nick Piggin77391d72005-06-25 14:57:30 -07003878 * Max backoff if we encounter pinned tasks. Pretty arbitrary value, but
3879 * so long as it is large enough.
3880 */
3881#define MAX_PINNED_INTERVAL 512
3882
Rusty Russelldf7c8e82009-03-19 15:22:20 +10303883/* Working cpumask for load_balance and load_balance_newidle. */
3884static DEFINE_PER_CPU(cpumask_var_t, load_balance_tmpmask);
3885
Nick Piggin77391d72005-06-25 14:57:30 -07003886/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003887 * Check this_cpu to ensure it is balanced within domain. Attempt to move
3888 * tasks if there is an imbalance.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003889 */
Ingo Molnar70b97a72006-07-03 00:25:42 -07003890static int load_balance(int this_cpu, struct rq *this_rq,
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003891 struct sched_domain *sd, enum cpu_idle_type idle,
Rusty Russelldf7c8e82009-03-19 15:22:20 +10303892 int *balance)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003893{
Peter Williams43010652007-08-09 11:16:46 +02003894 int ld_moved, all_pinned = 0, active_balance = 0, sd_idle = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003895 struct sched_group *group;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003896 unsigned long imbalance;
Ingo Molnar70b97a72006-07-03 00:25:42 -07003897 struct rq *busiest;
Christoph Lameterfe2eea32006-12-10 02:20:21 -08003898 unsigned long flags;
Rusty Russelldf7c8e82009-03-19 15:22:20 +10303899 struct cpumask *cpus = __get_cpu_var(load_balance_tmpmask);
Nick Piggin5969fe02005-09-10 00:26:19 -07003900
Rusty Russell96f874e2008-11-25 02:35:14 +10303901 cpumask_setall(cpus);
Mike Travis7c16ec52008-04-04 18:11:11 -07003902
Siddha, Suresh B89c47102006-10-03 01:14:09 -07003903 /*
3904 * When power savings policy is enabled for the parent domain, idle
3905 * sibling can pick up load irrespective of busy siblings. In this case,
Ingo Molnardd41f592007-07-09 18:51:59 +02003906 * let the state of idle sibling percolate up as CPU_IDLE, instead of
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003907 * portraying it as CPU_NOT_IDLE.
Siddha, Suresh B89c47102006-10-03 01:14:09 -07003908 */
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003909 if (idle != CPU_NOT_IDLE && sd->flags & SD_SHARE_CPUPOWER &&
Siddha, Suresh B89c47102006-10-03 01:14:09 -07003910 !test_sd_parent(sd, SD_POWERSAVINGS_BALANCE))
Nick Piggin5969fe02005-09-10 00:26:19 -07003911 sd_idle = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003912
Ingo Molnar2d723762007-10-15 17:00:12 +02003913 schedstat_inc(sd, lb_count[idle]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003914
Christoph Lameter0a2966b2006-09-25 23:30:51 -07003915redo:
Peter Zijlstrac8cba852008-06-27 13:41:23 +02003916 update_shares(sd);
Christoph Lameter0a2966b2006-09-25 23:30:51 -07003917 group = find_busiest_group(sd, this_cpu, &imbalance, idle, &sd_idle,
Mike Travis7c16ec52008-04-04 18:11:11 -07003918 cpus, balance);
Siddha, Suresh B783609c2006-12-10 02:20:33 -08003919
Chen, Kenneth W06066712006-12-10 02:20:35 -08003920 if (*balance == 0)
Siddha, Suresh B783609c2006-12-10 02:20:33 -08003921 goto out_balanced;
Siddha, Suresh B783609c2006-12-10 02:20:33 -08003922
Linus Torvalds1da177e2005-04-16 15:20:36 -07003923 if (!group) {
3924 schedstat_inc(sd, lb_nobusyg[idle]);
3925 goto out_balanced;
3926 }
3927
Mike Travis7c16ec52008-04-04 18:11:11 -07003928 busiest = find_busiest_queue(group, idle, imbalance, cpus);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003929 if (!busiest) {
3930 schedstat_inc(sd, lb_nobusyq[idle]);
3931 goto out_balanced;
3932 }
3933
Nick Piggindb935db2005-06-25 14:57:11 -07003934 BUG_ON(busiest == this_rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003935
3936 schedstat_add(sd, lb_imbalance[idle], imbalance);
3937
Peter Williams43010652007-08-09 11:16:46 +02003938 ld_moved = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003939 if (busiest->nr_running > 1) {
3940 /*
3941 * Attempt to move tasks. If find_busiest_group has found
3942 * an imbalance but busiest->nr_running <= 1, the group is
Peter Williams43010652007-08-09 11:16:46 +02003943 * still unbalanced. ld_moved simply stays zero, so it is
Linus Torvalds1da177e2005-04-16 15:20:36 -07003944 * correctly treated as an imbalance.
3945 */
Christoph Lameterfe2eea32006-12-10 02:20:21 -08003946 local_irq_save(flags);
Nick Piggine17224b2005-09-10 00:26:18 -07003947 double_rq_lock(this_rq, busiest);
Peter Williams43010652007-08-09 11:16:46 +02003948 ld_moved = move_tasks(this_rq, this_cpu, busiest,
Ingo Molnar48f24c42006-07-03 00:25:40 -07003949 imbalance, sd, idle, &all_pinned);
Nick Piggine17224b2005-09-10 00:26:18 -07003950 double_rq_unlock(this_rq, busiest);
Christoph Lameterfe2eea32006-12-10 02:20:21 -08003951 local_irq_restore(flags);
Nick Piggin81026792005-06-25 14:57:07 -07003952
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07003953 /*
3954 * some other cpu did the load balance for us.
3955 */
Peter Williams43010652007-08-09 11:16:46 +02003956 if (ld_moved && this_cpu != smp_processor_id())
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07003957 resched_cpu(this_cpu);
3958
Nick Piggin81026792005-06-25 14:57:07 -07003959 /* All tasks on this runqueue were pinned by CPU affinity */
Christoph Lameter0a2966b2006-09-25 23:30:51 -07003960 if (unlikely(all_pinned)) {
Rusty Russell96f874e2008-11-25 02:35:14 +10303961 cpumask_clear_cpu(cpu_of(busiest), cpus);
3962 if (!cpumask_empty(cpus))
Christoph Lameter0a2966b2006-09-25 23:30:51 -07003963 goto redo;
Nick Piggin81026792005-06-25 14:57:07 -07003964 goto out_balanced;
Christoph Lameter0a2966b2006-09-25 23:30:51 -07003965 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003966 }
Nick Piggin81026792005-06-25 14:57:07 -07003967
Peter Williams43010652007-08-09 11:16:46 +02003968 if (!ld_moved) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003969 schedstat_inc(sd, lb_failed[idle]);
3970 sd->nr_balance_failed++;
3971
3972 if (unlikely(sd->nr_balance_failed > sd->cache_nice_tries+2)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003973
Christoph Lameterfe2eea32006-12-10 02:20:21 -08003974 spin_lock_irqsave(&busiest->lock, flags);
Siddha, Suresh Bfa3b6dd2005-09-10 00:26:21 -07003975
3976 /* don't kick the migration_thread, if the curr
3977 * task on busiest cpu can't be moved to this_cpu
3978 */
Rusty Russell96f874e2008-11-25 02:35:14 +10303979 if (!cpumask_test_cpu(this_cpu,
3980 &busiest->curr->cpus_allowed)) {
Christoph Lameterfe2eea32006-12-10 02:20:21 -08003981 spin_unlock_irqrestore(&busiest->lock, flags);
Siddha, Suresh Bfa3b6dd2005-09-10 00:26:21 -07003982 all_pinned = 1;
3983 goto out_one_pinned;
3984 }
3985
Linus Torvalds1da177e2005-04-16 15:20:36 -07003986 if (!busiest->active_balance) {
3987 busiest->active_balance = 1;
3988 busiest->push_cpu = this_cpu;
Nick Piggin81026792005-06-25 14:57:07 -07003989 active_balance = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003990 }
Christoph Lameterfe2eea32006-12-10 02:20:21 -08003991 spin_unlock_irqrestore(&busiest->lock, flags);
Nick Piggin81026792005-06-25 14:57:07 -07003992 if (active_balance)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003993 wake_up_process(busiest->migration_thread);
3994
3995 /*
3996 * We've kicked active balancing, reset the failure
3997 * counter.
3998 */
Nick Piggin39507452005-06-25 14:57:09 -07003999 sd->nr_balance_failed = sd->cache_nice_tries+1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004000 }
Nick Piggin81026792005-06-25 14:57:07 -07004001 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07004002 sd->nr_balance_failed = 0;
4003
Nick Piggin81026792005-06-25 14:57:07 -07004004 if (likely(!active_balance)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004005 /* We were unbalanced, so reset the balancing interval */
4006 sd->balance_interval = sd->min_interval;
Nick Piggin81026792005-06-25 14:57:07 -07004007 } else {
4008 /*
4009 * If we've begun active balancing, start to back off. This
4010 * case may not be covered by the all_pinned logic if there
4011 * is only 1 task on the busy runqueue (because we don't call
4012 * move_tasks).
4013 */
4014 if (sd->balance_interval < sd->max_interval)
4015 sd->balance_interval *= 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004016 }
4017
Peter Williams43010652007-08-09 11:16:46 +02004018 if (!ld_moved && !sd_idle && sd->flags & SD_SHARE_CPUPOWER &&
Siddha, Suresh B89c47102006-10-03 01:14:09 -07004019 !test_sd_parent(sd, SD_POWERSAVINGS_BALANCE))
Peter Zijlstrac09595f2008-06-27 13:41:14 +02004020 ld_moved = -1;
4021
4022 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004023
4024out_balanced:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004025 schedstat_inc(sd, lb_balanced[idle]);
4026
Nick Piggin16cfb1c2005-06-25 14:57:08 -07004027 sd->nr_balance_failed = 0;
Siddha, Suresh Bfa3b6dd2005-09-10 00:26:21 -07004028
4029out_one_pinned:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004030 /* tune up the balancing interval */
Nick Piggin77391d72005-06-25 14:57:30 -07004031 if ((all_pinned && sd->balance_interval < MAX_PINNED_INTERVAL) ||
4032 (sd->balance_interval < sd->max_interval))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004033 sd->balance_interval *= 2;
4034
Ingo Molnar48f24c42006-07-03 00:25:40 -07004035 if (!sd_idle && sd->flags & SD_SHARE_CPUPOWER &&
Siddha, Suresh B89c47102006-10-03 01:14:09 -07004036 !test_sd_parent(sd, SD_POWERSAVINGS_BALANCE))
Peter Zijlstrac09595f2008-06-27 13:41:14 +02004037 ld_moved = -1;
4038 else
4039 ld_moved = 0;
4040out:
Peter Zijlstrac8cba852008-06-27 13:41:23 +02004041 if (ld_moved)
4042 update_shares(sd);
Peter Zijlstrac09595f2008-06-27 13:41:14 +02004043 return ld_moved;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004044}
4045
4046/*
4047 * Check this_cpu to ensure it is balanced within domain. Attempt to move
4048 * tasks if there is an imbalance.
4049 *
Ingo Molnard15bcfd2007-07-09 18:51:57 +02004050 * Called from schedule when this_rq is about to become idle (CPU_NEWLY_IDLE).
Linus Torvalds1da177e2005-04-16 15:20:36 -07004051 * this_rq is locked.
4052 */
Ingo Molnar48f24c42006-07-03 00:25:40 -07004053static int
Rusty Russelldf7c8e82009-03-19 15:22:20 +10304054load_balance_newidle(int this_cpu, struct rq *this_rq, struct sched_domain *sd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004055{
4056 struct sched_group *group;
Ingo Molnar70b97a72006-07-03 00:25:42 -07004057 struct rq *busiest = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004058 unsigned long imbalance;
Peter Williams43010652007-08-09 11:16:46 +02004059 int ld_moved = 0;
Nick Piggin5969fe02005-09-10 00:26:19 -07004060 int sd_idle = 0;
Suresh Siddha969bb4e2007-07-19 21:28:35 +02004061 int all_pinned = 0;
Rusty Russelldf7c8e82009-03-19 15:22:20 +10304062 struct cpumask *cpus = __get_cpu_var(load_balance_tmpmask);
Mike Travis7c16ec52008-04-04 18:11:11 -07004063
Rusty Russell96f874e2008-11-25 02:35:14 +10304064 cpumask_setall(cpus);
Nick Piggin5969fe02005-09-10 00:26:19 -07004065
Siddha, Suresh B89c47102006-10-03 01:14:09 -07004066 /*
4067 * When power savings policy is enabled for the parent domain, idle
4068 * sibling can pick up load irrespective of busy siblings. In this case,
4069 * let the state of idle sibling percolate up as IDLE, instead of
Ingo Molnard15bcfd2007-07-09 18:51:57 +02004070 * portraying it as CPU_NOT_IDLE.
Siddha, Suresh B89c47102006-10-03 01:14:09 -07004071 */
4072 if (sd->flags & SD_SHARE_CPUPOWER &&
4073 !test_sd_parent(sd, SD_POWERSAVINGS_BALANCE))
Nick Piggin5969fe02005-09-10 00:26:19 -07004074 sd_idle = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004075
Ingo Molnar2d723762007-10-15 17:00:12 +02004076 schedstat_inc(sd, lb_count[CPU_NEWLY_IDLE]);
Christoph Lameter0a2966b2006-09-25 23:30:51 -07004077redo:
Peter Zijlstra3e5459b2008-06-27 13:41:24 +02004078 update_shares_locked(this_rq, sd);
Ingo Molnard15bcfd2007-07-09 18:51:57 +02004079 group = find_busiest_group(sd, this_cpu, &imbalance, CPU_NEWLY_IDLE,
Mike Travis7c16ec52008-04-04 18:11:11 -07004080 &sd_idle, cpus, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004081 if (!group) {
Ingo Molnard15bcfd2007-07-09 18:51:57 +02004082 schedstat_inc(sd, lb_nobusyg[CPU_NEWLY_IDLE]);
Nick Piggin16cfb1c2005-06-25 14:57:08 -07004083 goto out_balanced;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004084 }
4085
Mike Travis7c16ec52008-04-04 18:11:11 -07004086 busiest = find_busiest_queue(group, CPU_NEWLY_IDLE, imbalance, cpus);
Nick Piggindb935db2005-06-25 14:57:11 -07004087 if (!busiest) {
Ingo Molnard15bcfd2007-07-09 18:51:57 +02004088 schedstat_inc(sd, lb_nobusyq[CPU_NEWLY_IDLE]);
Nick Piggin16cfb1c2005-06-25 14:57:08 -07004089 goto out_balanced;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004090 }
4091
Nick Piggindb935db2005-06-25 14:57:11 -07004092 BUG_ON(busiest == this_rq);
4093
Ingo Molnard15bcfd2007-07-09 18:51:57 +02004094 schedstat_add(sd, lb_imbalance[CPU_NEWLY_IDLE], imbalance);
Nick Piggind6d5cfa2005-09-10 00:26:16 -07004095
Peter Williams43010652007-08-09 11:16:46 +02004096 ld_moved = 0;
Nick Piggind6d5cfa2005-09-10 00:26:16 -07004097 if (busiest->nr_running > 1) {
4098 /* Attempt to move tasks */
4099 double_lock_balance(this_rq, busiest);
Ingo Molnar6e82a3b2007-08-09 11:16:51 +02004100 /* this_rq->clock is already updated */
4101 update_rq_clock(busiest);
Peter Williams43010652007-08-09 11:16:46 +02004102 ld_moved = move_tasks(this_rq, this_cpu, busiest,
Suresh Siddha969bb4e2007-07-19 21:28:35 +02004103 imbalance, sd, CPU_NEWLY_IDLE,
4104 &all_pinned);
Peter Zijlstra1b12bbc2008-08-11 09:30:22 +02004105 double_unlock_balance(this_rq, busiest);
Christoph Lameter0a2966b2006-09-25 23:30:51 -07004106
Suresh Siddha969bb4e2007-07-19 21:28:35 +02004107 if (unlikely(all_pinned)) {
Rusty Russell96f874e2008-11-25 02:35:14 +10304108 cpumask_clear_cpu(cpu_of(busiest), cpus);
4109 if (!cpumask_empty(cpus))
Christoph Lameter0a2966b2006-09-25 23:30:51 -07004110 goto redo;
4111 }
Nick Piggind6d5cfa2005-09-10 00:26:16 -07004112 }
4113
Peter Williams43010652007-08-09 11:16:46 +02004114 if (!ld_moved) {
Vaidyanathan Srinivasan36dffab2008-12-20 10:06:38 +05304115 int active_balance = 0;
Vaidyanathan Srinivasanad273b32008-12-18 23:26:36 +05304116
Ingo Molnard15bcfd2007-07-09 18:51:57 +02004117 schedstat_inc(sd, lb_failed[CPU_NEWLY_IDLE]);
Siddha, Suresh B89c47102006-10-03 01:14:09 -07004118 if (!sd_idle && sd->flags & SD_SHARE_CPUPOWER &&
4119 !test_sd_parent(sd, SD_POWERSAVINGS_BALANCE))
Nick Piggin5969fe02005-09-10 00:26:19 -07004120 return -1;
Vaidyanathan Srinivasanad273b32008-12-18 23:26:36 +05304121
4122 if (sched_mc_power_savings < POWERSAVINGS_BALANCE_WAKEUP)
4123 return -1;
4124
4125 if (sd->nr_balance_failed++ < 2)
4126 return -1;
4127
4128 /*
4129 * The only task running in a non-idle cpu can be moved to this
4130 * cpu in an attempt to completely freeup the other CPU
4131 * package. The same method used to move task in load_balance()
4132 * have been extended for load_balance_newidle() to speedup
4133 * consolidation at sched_mc=POWERSAVINGS_BALANCE_WAKEUP (2)
4134 *
4135 * The package power saving logic comes from
4136 * find_busiest_group(). If there are no imbalance, then
4137 * f_b_g() will return NULL. However when sched_mc={1,2} then
4138 * f_b_g() will select a group from which a running task may be
4139 * pulled to this cpu in order to make the other package idle.
4140 * If there is no opportunity to make a package idle and if
4141 * there are no imbalance, then f_b_g() will return NULL and no
4142 * action will be taken in load_balance_newidle().
4143 *
4144 * Under normal task pull operation due to imbalance, there
4145 * will be more than one task in the source run queue and
4146 * move_tasks() will succeed. ld_moved will be true and this
4147 * active balance code will not be triggered.
4148 */
4149
4150 /* Lock busiest in correct order while this_rq is held */
4151 double_lock_balance(this_rq, busiest);
4152
4153 /*
4154 * don't kick the migration_thread, if the curr
4155 * task on busiest cpu can't be moved to this_cpu
4156 */
Mike Travis6ca09df2008-12-31 18:08:45 -08004157 if (!cpumask_test_cpu(this_cpu, &busiest->curr->cpus_allowed)) {
Vaidyanathan Srinivasanad273b32008-12-18 23:26:36 +05304158 double_unlock_balance(this_rq, busiest);
4159 all_pinned = 1;
4160 return ld_moved;
4161 }
4162
4163 if (!busiest->active_balance) {
4164 busiest->active_balance = 1;
4165 busiest->push_cpu = this_cpu;
4166 active_balance = 1;
4167 }
4168
4169 double_unlock_balance(this_rq, busiest);
Peter Zijlstrada8d5082009-01-07 15:28:57 +01004170 /*
4171 * Should not call ttwu while holding a rq->lock
4172 */
4173 spin_unlock(&this_rq->lock);
Vaidyanathan Srinivasanad273b32008-12-18 23:26:36 +05304174 if (active_balance)
4175 wake_up_process(busiest->migration_thread);
Peter Zijlstrada8d5082009-01-07 15:28:57 +01004176 spin_lock(&this_rq->lock);
Vaidyanathan Srinivasanad273b32008-12-18 23:26:36 +05304177
Nick Piggin5969fe02005-09-10 00:26:19 -07004178 } else
Nick Piggin16cfb1c2005-06-25 14:57:08 -07004179 sd->nr_balance_failed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004180
Peter Zijlstra3e5459b2008-06-27 13:41:24 +02004181 update_shares_locked(this_rq, sd);
Peter Williams43010652007-08-09 11:16:46 +02004182 return ld_moved;
Nick Piggin16cfb1c2005-06-25 14:57:08 -07004183
4184out_balanced:
Ingo Molnard15bcfd2007-07-09 18:51:57 +02004185 schedstat_inc(sd, lb_balanced[CPU_NEWLY_IDLE]);
Ingo Molnar48f24c42006-07-03 00:25:40 -07004186 if (!sd_idle && sd->flags & SD_SHARE_CPUPOWER &&
Siddha, Suresh B89c47102006-10-03 01:14:09 -07004187 !test_sd_parent(sd, SD_POWERSAVINGS_BALANCE))
Nick Piggin5969fe02005-09-10 00:26:19 -07004188 return -1;
Nick Piggin16cfb1c2005-06-25 14:57:08 -07004189 sd->nr_balance_failed = 0;
Ingo Molnar48f24c42006-07-03 00:25:40 -07004190
Nick Piggin16cfb1c2005-06-25 14:57:08 -07004191 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004192}
4193
4194/*
4195 * idle_balance is called by schedule() if this_cpu is about to become
4196 * idle. Attempts to pull tasks from other CPUs.
4197 */
Ingo Molnar70b97a72006-07-03 00:25:42 -07004198static void idle_balance(int this_cpu, struct rq *this_rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004199{
4200 struct sched_domain *sd;
Vaidyanathan Srinivasanefbe0272008-12-08 20:52:49 +05304201 int pulled_task = 0;
Ingo Molnardd41f592007-07-09 18:51:59 +02004202 unsigned long next_balance = jiffies + HZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004203
4204 for_each_domain(this_cpu, sd) {
Christoph Lameter92c4ca52007-06-23 17:16:33 -07004205 unsigned long interval;
4206
4207 if (!(sd->flags & SD_LOAD_BALANCE))
4208 continue;
4209
4210 if (sd->flags & SD_BALANCE_NEWIDLE)
Ingo Molnar48f24c42006-07-03 00:25:40 -07004211 /* If we've pulled tasks over stop searching: */
Mike Travis7c16ec52008-04-04 18:11:11 -07004212 pulled_task = load_balance_newidle(this_cpu, this_rq,
Rusty Russelldf7c8e82009-03-19 15:22:20 +10304213 sd);
Christoph Lameter92c4ca52007-06-23 17:16:33 -07004214
4215 interval = msecs_to_jiffies(sd->balance_interval);
4216 if (time_after(next_balance, sd->last_balance + interval))
4217 next_balance = sd->last_balance + interval;
4218 if (pulled_task)
4219 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004220 }
Ingo Molnardd41f592007-07-09 18:51:59 +02004221 if (pulled_task || time_after(jiffies, this_rq->next_balance)) {
Christoph Lameter1bd77f22006-12-10 02:20:27 -08004222 /*
4223 * We are going idle. next_balance may be set based on
4224 * a busy processor. So reset next_balance.
4225 */
4226 this_rq->next_balance = next_balance;
Ingo Molnardd41f592007-07-09 18:51:59 +02004227 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004228}
4229
4230/*
4231 * active_load_balance is run by migration threads. It pushes running tasks
4232 * off the busiest CPU onto idle CPUs. It requires at least 1 task to be
4233 * running on each physical CPU where possible, and avoids physical /
4234 * logical imbalances.
4235 *
4236 * Called with busiest_rq locked.
4237 */
Ingo Molnar70b97a72006-07-03 00:25:42 -07004238static void active_load_balance(struct rq *busiest_rq, int busiest_cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004239{
Nick Piggin39507452005-06-25 14:57:09 -07004240 int target_cpu = busiest_rq->push_cpu;
Ingo Molnar70b97a72006-07-03 00:25:42 -07004241 struct sched_domain *sd;
4242 struct rq *target_rq;
Nick Piggin39507452005-06-25 14:57:09 -07004243
Ingo Molnar48f24c42006-07-03 00:25:40 -07004244 /* Is there any task to move? */
Nick Piggin39507452005-06-25 14:57:09 -07004245 if (busiest_rq->nr_running <= 1)
Nick Piggin39507452005-06-25 14:57:09 -07004246 return;
4247
4248 target_rq = cpu_rq(target_cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004249
4250 /*
Nick Piggin39507452005-06-25 14:57:09 -07004251 * This condition is "impossible", if it occurs
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01004252 * we need to fix it. Originally reported by
Nick Piggin39507452005-06-25 14:57:09 -07004253 * Bjorn Helgaas on a 128-cpu setup.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004254 */
Nick Piggin39507452005-06-25 14:57:09 -07004255 BUG_ON(busiest_rq == target_rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004256
Nick Piggin39507452005-06-25 14:57:09 -07004257 /* move a task from busiest_rq to target_rq */
4258 double_lock_balance(busiest_rq, target_rq);
Ingo Molnar6e82a3b2007-08-09 11:16:51 +02004259 update_rq_clock(busiest_rq);
4260 update_rq_clock(target_rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004261
Nick Piggin39507452005-06-25 14:57:09 -07004262 /* Search for an sd spanning us and the target CPU. */
Chen, Kenneth Wc96d1452006-06-27 02:54:28 -07004263 for_each_domain(target_cpu, sd) {
Nick Piggin39507452005-06-25 14:57:09 -07004264 if ((sd->flags & SD_LOAD_BALANCE) &&
Rusty Russell758b2cd2008-11-25 02:35:04 +10304265 cpumask_test_cpu(busiest_cpu, sched_domain_span(sd)))
Nick Piggin39507452005-06-25 14:57:09 -07004266 break;
Chen, Kenneth Wc96d1452006-06-27 02:54:28 -07004267 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004268
Ingo Molnar48f24c42006-07-03 00:25:40 -07004269 if (likely(sd)) {
Ingo Molnar2d723762007-10-15 17:00:12 +02004270 schedstat_inc(sd, alb_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004271
Peter Williams43010652007-08-09 11:16:46 +02004272 if (move_one_task(target_rq, target_cpu, busiest_rq,
4273 sd, CPU_IDLE))
Ingo Molnar48f24c42006-07-03 00:25:40 -07004274 schedstat_inc(sd, alb_pushed);
4275 else
4276 schedstat_inc(sd, alb_failed);
4277 }
Peter Zijlstra1b12bbc2008-08-11 09:30:22 +02004278 double_unlock_balance(busiest_rq, target_rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004279}
4280
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07004281#ifdef CONFIG_NO_HZ
4282static struct {
4283 atomic_t load_balancer;
Rusty Russell7d1e6a92008-11-25 02:35:09 +10304284 cpumask_var_t cpu_mask;
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07004285} nohz ____cacheline_aligned = {
4286 .load_balancer = ATOMIC_INIT(-1),
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07004287};
4288
Christoph Lameter7835b982006-12-10 02:20:22 -08004289/*
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07004290 * This routine will try to nominate the ilb (idle load balancing)
4291 * owner among the cpus whose ticks are stopped. ilb owner will do the idle
4292 * load balancing on behalf of all those cpus. If all the cpus in the system
4293 * go into this tickless mode, then there will be no ilb owner (as there is
4294 * no need for one) and all the cpus will sleep till the next wakeup event
4295 * arrives...
Christoph Lameter7835b982006-12-10 02:20:22 -08004296 *
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07004297 * For the ilb owner, tick is not stopped. And this tick will be used
4298 * for idle load balancing. ilb owner will still be part of
4299 * nohz.cpu_mask..
4300 *
4301 * While stopping the tick, this cpu will become the ilb owner if there
4302 * is no other owner. And will be the owner till that cpu becomes busy
4303 * or if all cpus in the system stop their ticks at which point
4304 * there is no need for ilb owner.
4305 *
4306 * When the ilb owner becomes busy, it nominates another owner, during the
4307 * next busy scheduler_tick()
4308 */
4309int select_nohz_load_balancer(int stop_tick)
4310{
4311 int cpu = smp_processor_id();
4312
4313 if (stop_tick) {
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07004314 cpu_rq(cpu)->in_nohz_recently = 1;
4315
Suresh Siddha483b4ee2009-02-04 11:59:44 -08004316 if (!cpu_active(cpu)) {
4317 if (atomic_read(&nohz.load_balancer) != cpu)
4318 return 0;
4319
4320 /*
4321 * If we are going offline and still the leader,
4322 * give up!
4323 */
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07004324 if (atomic_cmpxchg(&nohz.load_balancer, cpu, -1) != cpu)
4325 BUG();
Suresh Siddha483b4ee2009-02-04 11:59:44 -08004326
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07004327 return 0;
4328 }
4329
Suresh Siddha483b4ee2009-02-04 11:59:44 -08004330 cpumask_set_cpu(cpu, nohz.cpu_mask);
4331
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07004332 /* time for ilb owner also to sleep */
Rusty Russell7d1e6a92008-11-25 02:35:09 +10304333 if (cpumask_weight(nohz.cpu_mask) == num_online_cpus()) {
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07004334 if (atomic_read(&nohz.load_balancer) == cpu)
4335 atomic_set(&nohz.load_balancer, -1);
4336 return 0;
4337 }
4338
4339 if (atomic_read(&nohz.load_balancer) == -1) {
4340 /* make me the ilb owner */
4341 if (atomic_cmpxchg(&nohz.load_balancer, -1, cpu) == -1)
4342 return 1;
4343 } else if (atomic_read(&nohz.load_balancer) == cpu)
4344 return 1;
4345 } else {
Rusty Russell7d1e6a92008-11-25 02:35:09 +10304346 if (!cpumask_test_cpu(cpu, nohz.cpu_mask))
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07004347 return 0;
4348
Rusty Russell7d1e6a92008-11-25 02:35:09 +10304349 cpumask_clear_cpu(cpu, nohz.cpu_mask);
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07004350
4351 if (atomic_read(&nohz.load_balancer) == cpu)
4352 if (atomic_cmpxchg(&nohz.load_balancer, cpu, -1) != cpu)
4353 BUG();
4354 }
4355 return 0;
4356}
4357#endif
4358
4359static DEFINE_SPINLOCK(balancing);
4360
4361/*
Christoph Lameter7835b982006-12-10 02:20:22 -08004362 * It checks each scheduling domain to see if it is due to be balanced,
4363 * and initiates a balancing operation if so.
4364 *
4365 * Balancing parameters are set up in arch_init_sched_domains.
4366 */
Alexey Dobriyana9957442007-10-15 17:00:13 +02004367static void rebalance_domains(int cpu, enum cpu_idle_type idle)
Christoph Lameter7835b982006-12-10 02:20:22 -08004368{
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07004369 int balance = 1;
4370 struct rq *rq = cpu_rq(cpu);
Christoph Lameter7835b982006-12-10 02:20:22 -08004371 unsigned long interval;
4372 struct sched_domain *sd;
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07004373 /* Earliest time when we have to do rebalance again */
Christoph Lameterc9819f42006-12-10 02:20:25 -08004374 unsigned long next_balance = jiffies + 60*HZ;
Suresh Siddhaf549da82007-08-23 15:18:02 +02004375 int update_next_balance = 0;
Dmitry Adamushkod07355f2008-05-12 21:21:15 +02004376 int need_serialize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004377
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07004378 for_each_domain(cpu, sd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004379 if (!(sd->flags & SD_LOAD_BALANCE))
4380 continue;
4381
4382 interval = sd->balance_interval;
Ingo Molnard15bcfd2007-07-09 18:51:57 +02004383 if (idle != CPU_IDLE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004384 interval *= sd->busy_factor;
4385
4386 /* scale ms to jiffies */
4387 interval = msecs_to_jiffies(interval);
4388 if (unlikely(!interval))
4389 interval = 1;
Ingo Molnardd41f592007-07-09 18:51:59 +02004390 if (interval > HZ*NR_CPUS/10)
4391 interval = HZ*NR_CPUS/10;
4392
Dmitry Adamushkod07355f2008-05-12 21:21:15 +02004393 need_serialize = sd->flags & SD_SERIALIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004394
Dmitry Adamushkod07355f2008-05-12 21:21:15 +02004395 if (need_serialize) {
Christoph Lameter08c183f2006-12-10 02:20:29 -08004396 if (!spin_trylock(&balancing))
4397 goto out;
4398 }
4399
Christoph Lameterc9819f42006-12-10 02:20:25 -08004400 if (time_after_eq(jiffies, sd->last_balance + interval)) {
Rusty Russelldf7c8e82009-03-19 15:22:20 +10304401 if (load_balance(cpu, rq, sd, idle, &balance)) {
Siddha, Suresh Bfa3b6dd2005-09-10 00:26:21 -07004402 /*
4403 * We've pulled tasks over so either we're no
Nick Piggin5969fe02005-09-10 00:26:19 -07004404 * longer idle, or one of our SMT siblings is
4405 * not idle.
4406 */
Ingo Molnard15bcfd2007-07-09 18:51:57 +02004407 idle = CPU_NOT_IDLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004408 }
Christoph Lameter1bd77f22006-12-10 02:20:27 -08004409 sd->last_balance = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004410 }
Dmitry Adamushkod07355f2008-05-12 21:21:15 +02004411 if (need_serialize)
Christoph Lameter08c183f2006-12-10 02:20:29 -08004412 spin_unlock(&balancing);
4413out:
Suresh Siddhaf549da82007-08-23 15:18:02 +02004414 if (time_after(next_balance, sd->last_balance + interval)) {
Christoph Lameterc9819f42006-12-10 02:20:25 -08004415 next_balance = sd->last_balance + interval;
Suresh Siddhaf549da82007-08-23 15:18:02 +02004416 update_next_balance = 1;
4417 }
Siddha, Suresh B783609c2006-12-10 02:20:33 -08004418
4419 /*
4420 * Stop the load balance at this level. There is another
4421 * CPU in our sched group which is doing load balancing more
4422 * actively.
4423 */
4424 if (!balance)
4425 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004426 }
Suresh Siddhaf549da82007-08-23 15:18:02 +02004427
4428 /*
4429 * next_balance will be updated only when there is a need.
4430 * When the cpu is attached to null domain for ex, it will not be
4431 * updated.
4432 */
4433 if (likely(update_next_balance))
4434 rq->next_balance = next_balance;
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07004435}
4436
4437/*
4438 * run_rebalance_domains is triggered when needed from the scheduler tick.
4439 * In CONFIG_NO_HZ case, the idle load balance owner will do the
4440 * rebalancing for all the cpus for whom scheduler ticks are stopped.
4441 */
4442static void run_rebalance_domains(struct softirq_action *h)
4443{
Ingo Molnardd41f592007-07-09 18:51:59 +02004444 int this_cpu = smp_processor_id();
4445 struct rq *this_rq = cpu_rq(this_cpu);
4446 enum cpu_idle_type idle = this_rq->idle_at_tick ?
4447 CPU_IDLE : CPU_NOT_IDLE;
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07004448
Ingo Molnardd41f592007-07-09 18:51:59 +02004449 rebalance_domains(this_cpu, idle);
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07004450
4451#ifdef CONFIG_NO_HZ
4452 /*
4453 * If this cpu is the owner for idle load balancing, then do the
4454 * balancing on behalf of the other idle cpus whose ticks are
4455 * stopped.
4456 */
Ingo Molnardd41f592007-07-09 18:51:59 +02004457 if (this_rq->idle_at_tick &&
4458 atomic_read(&nohz.load_balancer) == this_cpu) {
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07004459 struct rq *rq;
4460 int balance_cpu;
4461
Rusty Russell7d1e6a92008-11-25 02:35:09 +10304462 for_each_cpu(balance_cpu, nohz.cpu_mask) {
4463 if (balance_cpu == this_cpu)
4464 continue;
4465
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07004466 /*
4467 * If this cpu gets work to do, stop the load balancing
4468 * work being done for other cpus. Next load
4469 * balancing owner will pick it up.
4470 */
4471 if (need_resched())
4472 break;
4473
Oleg Nesterovde0cf892007-08-12 18:08:19 +02004474 rebalance_domains(balance_cpu, CPU_IDLE);
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07004475
4476 rq = cpu_rq(balance_cpu);
Ingo Molnardd41f592007-07-09 18:51:59 +02004477 if (time_after(this_rq->next_balance, rq->next_balance))
4478 this_rq->next_balance = rq->next_balance;
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07004479 }
4480 }
4481#endif
4482}
4483
Frederic Weisbecker8a0be9e2009-03-05 01:27:02 +01004484static inline int on_null_domain(int cpu)
4485{
4486 return !rcu_dereference(cpu_rq(cpu)->sd);
4487}
4488
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07004489/*
4490 * Trigger the SCHED_SOFTIRQ if it is time to do periodic load balancing.
4491 *
4492 * In case of CONFIG_NO_HZ, this is the place where we nominate a new
4493 * idle load balancing owner or decide to stop the periodic load balancing,
4494 * if the whole system is idle.
4495 */
Ingo Molnardd41f592007-07-09 18:51:59 +02004496static inline void trigger_load_balance(struct rq *rq, int cpu)
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07004497{
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07004498#ifdef CONFIG_NO_HZ
4499 /*
4500 * If we were in the nohz mode recently and busy at the current
4501 * scheduler tick, then check if we need to nominate new idle
4502 * load balancer.
4503 */
4504 if (rq->in_nohz_recently && !rq->idle_at_tick) {
4505 rq->in_nohz_recently = 0;
4506
4507 if (atomic_read(&nohz.load_balancer) == cpu) {
Rusty Russell7d1e6a92008-11-25 02:35:09 +10304508 cpumask_clear_cpu(cpu, nohz.cpu_mask);
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07004509 atomic_set(&nohz.load_balancer, -1);
4510 }
4511
4512 if (atomic_read(&nohz.load_balancer) == -1) {
4513 /*
4514 * simple selection for now: Nominate the
4515 * first cpu in the nohz list to be the next
4516 * ilb owner.
4517 *
4518 * TBD: Traverse the sched domains and nominate
4519 * the nearest cpu in the nohz.cpu_mask.
4520 */
Rusty Russell7d1e6a92008-11-25 02:35:09 +10304521 int ilb = cpumask_first(nohz.cpu_mask);
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07004522
Mike Travis434d53b2008-04-04 18:11:04 -07004523 if (ilb < nr_cpu_ids)
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07004524 resched_cpu(ilb);
4525 }
4526 }
4527
4528 /*
4529 * If this cpu is idle and doing idle load balancing for all the
4530 * cpus with ticks stopped, is it time for that to stop?
4531 */
4532 if (rq->idle_at_tick && atomic_read(&nohz.load_balancer) == cpu &&
Rusty Russell7d1e6a92008-11-25 02:35:09 +10304533 cpumask_weight(nohz.cpu_mask) == num_online_cpus()) {
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07004534 resched_cpu(cpu);
4535 return;
4536 }
4537
4538 /*
4539 * If this cpu is idle and the idle load balancing is done by
4540 * someone else, then no need raise the SCHED_SOFTIRQ
4541 */
4542 if (rq->idle_at_tick && atomic_read(&nohz.load_balancer) != cpu &&
Rusty Russell7d1e6a92008-11-25 02:35:09 +10304543 cpumask_test_cpu(cpu, nohz.cpu_mask))
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07004544 return;
4545#endif
Frederic Weisbecker8a0be9e2009-03-05 01:27:02 +01004546 /* Don't need to rebalance while attached to NULL domain */
4547 if (time_after_eq(jiffies, rq->next_balance) &&
4548 likely(!on_null_domain(cpu)))
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07004549 raise_softirq(SCHED_SOFTIRQ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004550}
Ingo Molnardd41f592007-07-09 18:51:59 +02004551
4552#else /* CONFIG_SMP */
4553
Linus Torvalds1da177e2005-04-16 15:20:36 -07004554/*
4555 * on UP we do not need to balance between CPUs:
4556 */
Ingo Molnar70b97a72006-07-03 00:25:42 -07004557static inline void idle_balance(int cpu, struct rq *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004558{
4559}
Ingo Molnardd41f592007-07-09 18:51:59 +02004560
Linus Torvalds1da177e2005-04-16 15:20:36 -07004561#endif
4562
Linus Torvalds1da177e2005-04-16 15:20:36 -07004563DEFINE_PER_CPU(struct kernel_stat, kstat);
4564
4565EXPORT_PER_CPU_SYMBOL(kstat);
4566
4567/*
Frank Mayharf06febc2008-09-12 09:54:39 -07004568 * Return any ns on the sched_clock that have not yet been banked in
4569 * @p in case that task is currently running.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004570 */
Ingo Molnaraa9c4c02008-12-17 14:10:57 +01004571unsigned long long __task_delta_exec(struct task_struct *p, int update)
4572{
4573 s64 delta_exec;
4574 struct rq *rq;
4575
4576 rq = task_rq(p);
4577 WARN_ON_ONCE(!runqueue_is_locked());
4578 WARN_ON_ONCE(!task_current(rq, p));
4579
4580 if (update)
4581 update_rq_clock(rq);
4582
4583 delta_exec = rq->clock - p->se.exec_start;
4584
4585 WARN_ON_ONCE(delta_exec < 0);
4586
4587 return delta_exec;
4588}
4589
4590/*
4591 * Return any ns on the sched_clock that have not yet been banked in
4592 * @p in case that task is currently running.
4593 */
Frank Mayharbb34d922008-09-12 09:54:39 -07004594unsigned long long task_delta_exec(struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004595{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004596 unsigned long flags;
Ingo Molnar41b86e92007-07-09 18:51:58 +02004597 struct rq *rq;
Frank Mayharbb34d922008-09-12 09:54:39 -07004598 u64 ns = 0;
Ingo Molnar48f24c42006-07-03 00:25:40 -07004599
Ingo Molnar41b86e92007-07-09 18:51:58 +02004600 rq = task_rq_lock(p, &flags);
Ingo Molnar15084872008-09-30 08:28:17 +02004601
Dmitry Adamushko051a1d12007-12-18 15:21:13 +01004602 if (task_current(rq, p)) {
Frank Mayharf06febc2008-09-12 09:54:39 -07004603 u64 delta_exec;
4604
Ingo Molnara8e504d2007-08-09 11:16:47 +02004605 update_rq_clock(rq);
4606 delta_exec = rq->clock - p->se.exec_start;
Ingo Molnar41b86e92007-07-09 18:51:58 +02004607 if ((s64)delta_exec > 0)
Frank Mayharbb34d922008-09-12 09:54:39 -07004608 ns = delta_exec;
Ingo Molnar41b86e92007-07-09 18:51:58 +02004609 }
Ingo Molnar48f24c42006-07-03 00:25:40 -07004610
Linus Torvalds1da177e2005-04-16 15:20:36 -07004611 task_rq_unlock(rq, &flags);
4612
4613 return ns;
4614}
4615
4616/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07004617 * Account user cpu time to a process.
4618 * @p: the process that the cpu time gets accounted to
Linus Torvalds1da177e2005-04-16 15:20:36 -07004619 * @cputime: the cpu time spent in user space since the last update
Martin Schwidefsky457533a2008-12-31 15:11:37 +01004620 * @cputime_scaled: cputime scaled by cpu frequency
Linus Torvalds1da177e2005-04-16 15:20:36 -07004621 */
Martin Schwidefsky457533a2008-12-31 15:11:37 +01004622void account_user_time(struct task_struct *p, cputime_t cputime,
4623 cputime_t cputime_scaled)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004624{
4625 struct cpu_usage_stat *cpustat = &kstat_this_cpu.cpustat;
4626 cputime64_t tmp;
4627
Martin Schwidefsky457533a2008-12-31 15:11:37 +01004628 /* Add user time to process. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004629 p->utime = cputime_add(p->utime, cputime);
Martin Schwidefsky457533a2008-12-31 15:11:37 +01004630 p->utimescaled = cputime_add(p->utimescaled, cputime_scaled);
Frank Mayharf06febc2008-09-12 09:54:39 -07004631 account_group_user_time(p, cputime);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004632
4633 /* Add user time to cpustat. */
4634 tmp = cputime_to_cputime64(cputime);
4635 if (TASK_NICE(p) > 0)
4636 cpustat->nice = cputime64_add(cpustat->nice, tmp);
4637 else
4638 cpustat->user = cputime64_add(cpustat->user, tmp);
Jonathan Lim49b5cf32008-07-25 01:48:40 -07004639 /* Account for user time used */
4640 acct_update_integrals(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004641}
4642
4643/*
Laurent Vivier94886b82007-10-15 17:00:19 +02004644 * Account guest cpu time to a process.
4645 * @p: the process that the cpu time gets accounted to
4646 * @cputime: the cpu time spent in virtual machine since the last update
Martin Schwidefsky457533a2008-12-31 15:11:37 +01004647 * @cputime_scaled: cputime scaled by cpu frequency
Laurent Vivier94886b82007-10-15 17:00:19 +02004648 */
Martin Schwidefsky457533a2008-12-31 15:11:37 +01004649static void account_guest_time(struct task_struct *p, cputime_t cputime,
4650 cputime_t cputime_scaled)
Laurent Vivier94886b82007-10-15 17:00:19 +02004651{
4652 cputime64_t tmp;
4653 struct cpu_usage_stat *cpustat = &kstat_this_cpu.cpustat;
4654
4655 tmp = cputime_to_cputime64(cputime);
4656
Martin Schwidefsky457533a2008-12-31 15:11:37 +01004657 /* Add guest time to process. */
Laurent Vivier94886b82007-10-15 17:00:19 +02004658 p->utime = cputime_add(p->utime, cputime);
Martin Schwidefsky457533a2008-12-31 15:11:37 +01004659 p->utimescaled = cputime_add(p->utimescaled, cputime_scaled);
Frank Mayharf06febc2008-09-12 09:54:39 -07004660 account_group_user_time(p, cputime);
Laurent Vivier94886b82007-10-15 17:00:19 +02004661 p->gtime = cputime_add(p->gtime, cputime);
4662
Martin Schwidefsky457533a2008-12-31 15:11:37 +01004663 /* Add guest time to cpustat. */
Laurent Vivier94886b82007-10-15 17:00:19 +02004664 cpustat->user = cputime64_add(cpustat->user, tmp);
4665 cpustat->guest = cputime64_add(cpustat->guest, tmp);
4666}
4667
4668/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07004669 * Account system cpu time to a process.
4670 * @p: the process that the cpu time gets accounted to
4671 * @hardirq_offset: the offset to subtract from hardirq_count()
4672 * @cputime: the cpu time spent in kernel space since the last update
Martin Schwidefsky457533a2008-12-31 15:11:37 +01004673 * @cputime_scaled: cputime scaled by cpu frequency
Linus Torvalds1da177e2005-04-16 15:20:36 -07004674 */
4675void account_system_time(struct task_struct *p, int hardirq_offset,
Martin Schwidefsky457533a2008-12-31 15:11:37 +01004676 cputime_t cputime, cputime_t cputime_scaled)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004677{
4678 struct cpu_usage_stat *cpustat = &kstat_this_cpu.cpustat;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004679 cputime64_t tmp;
4680
Harvey Harrison983ed7a2008-04-24 18:17:55 -07004681 if ((p->flags & PF_VCPU) && (irq_count() - hardirq_offset == 0)) {
Martin Schwidefsky457533a2008-12-31 15:11:37 +01004682 account_guest_time(p, cputime, cputime_scaled);
Harvey Harrison983ed7a2008-04-24 18:17:55 -07004683 return;
4684 }
Laurent Vivier94886b82007-10-15 17:00:19 +02004685
Martin Schwidefsky457533a2008-12-31 15:11:37 +01004686 /* Add system time to process. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004687 p->stime = cputime_add(p->stime, cputime);
Martin Schwidefsky457533a2008-12-31 15:11:37 +01004688 p->stimescaled = cputime_add(p->stimescaled, cputime_scaled);
Frank Mayharf06febc2008-09-12 09:54:39 -07004689 account_group_system_time(p, cputime);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004690
4691 /* Add system time to cpustat. */
4692 tmp = cputime_to_cputime64(cputime);
4693 if (hardirq_count() - hardirq_offset)
4694 cpustat->irq = cputime64_add(cpustat->irq, tmp);
4695 else if (softirq_count())
4696 cpustat->softirq = cputime64_add(cpustat->softirq, tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004697 else
Martin Schwidefsky79741dd2008-12-31 15:11:38 +01004698 cpustat->system = cputime64_add(cpustat->system, tmp);
4699
Linus Torvalds1da177e2005-04-16 15:20:36 -07004700 /* Account for system time used */
4701 acct_update_integrals(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004702}
4703
4704/*
4705 * Account for involuntary wait time.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004706 * @steal: the cpu time spent in involuntary wait
Linus Torvalds1da177e2005-04-16 15:20:36 -07004707 */
Martin Schwidefsky79741dd2008-12-31 15:11:38 +01004708void account_steal_time(cputime_t cputime)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004709{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004710 struct cpu_usage_stat *cpustat = &kstat_this_cpu.cpustat;
Martin Schwidefsky79741dd2008-12-31 15:11:38 +01004711 cputime64_t cputime64 = cputime_to_cputime64(cputime);
4712
4713 cpustat->steal = cputime64_add(cpustat->steal, cputime64);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004714}
4715
Christoph Lameter7835b982006-12-10 02:20:22 -08004716/*
Martin Schwidefsky79741dd2008-12-31 15:11:38 +01004717 * Account for idle time.
4718 * @cputime: the cpu time spent in idle wait
Linus Torvalds1da177e2005-04-16 15:20:36 -07004719 */
Martin Schwidefsky79741dd2008-12-31 15:11:38 +01004720void account_idle_time(cputime_t cputime)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004721{
4722 struct cpu_usage_stat *cpustat = &kstat_this_cpu.cpustat;
Martin Schwidefsky79741dd2008-12-31 15:11:38 +01004723 cputime64_t cputime64 = cputime_to_cputime64(cputime);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004724 struct rq *rq = this_rq();
4725
Martin Schwidefsky79741dd2008-12-31 15:11:38 +01004726 if (atomic_read(&rq->nr_iowait) > 0)
4727 cpustat->iowait = cputime64_add(cpustat->iowait, cputime64);
4728 else
4729 cpustat->idle = cputime64_add(cpustat->idle, cputime64);
Christoph Lameter7835b982006-12-10 02:20:22 -08004730}
4731
Martin Schwidefsky79741dd2008-12-31 15:11:38 +01004732#ifndef CONFIG_VIRT_CPU_ACCOUNTING
4733
4734/*
4735 * Account a single tick of cpu time.
4736 * @p: the process that the cpu time gets accounted to
4737 * @user_tick: indicates if the tick is a user or a system tick
4738 */
4739void account_process_tick(struct task_struct *p, int user_tick)
4740{
4741 cputime_t one_jiffy = jiffies_to_cputime(1);
4742 cputime_t one_jiffy_scaled = cputime_to_scaled(one_jiffy);
4743 struct rq *rq = this_rq();
4744
4745 if (user_tick)
4746 account_user_time(p, one_jiffy, one_jiffy_scaled);
4747 else if (p != rq->idle)
4748 account_system_time(p, HARDIRQ_OFFSET, one_jiffy,
4749 one_jiffy_scaled);
4750 else
4751 account_idle_time(one_jiffy);
4752}
4753
4754/*
4755 * Account multiple ticks of steal time.
4756 * @p: the process from which the cpu time has been stolen
4757 * @ticks: number of stolen ticks
4758 */
4759void account_steal_ticks(unsigned long ticks)
4760{
4761 account_steal_time(jiffies_to_cputime(ticks));
4762}
4763
4764/*
4765 * Account multiple ticks of idle time.
4766 * @ticks: number of stolen ticks
4767 */
4768void account_idle_ticks(unsigned long ticks)
4769{
4770 account_idle_time(jiffies_to_cputime(ticks));
4771}
4772
4773#endif
4774
Christoph Lameter7835b982006-12-10 02:20:22 -08004775/*
Balbir Singh49048622008-09-05 18:12:23 +02004776 * Use precise platform statistics if available:
4777 */
4778#ifdef CONFIG_VIRT_CPU_ACCOUNTING
4779cputime_t task_utime(struct task_struct *p)
4780{
4781 return p->utime;
4782}
4783
4784cputime_t task_stime(struct task_struct *p)
4785{
4786 return p->stime;
4787}
4788#else
4789cputime_t task_utime(struct task_struct *p)
4790{
4791 clock_t utime = cputime_to_clock_t(p->utime),
4792 total = utime + cputime_to_clock_t(p->stime);
4793 u64 temp;
4794
4795 /*
4796 * Use CFS's precise accounting:
4797 */
4798 temp = (u64)nsec_to_clock_t(p->se.sum_exec_runtime);
4799
4800 if (total) {
4801 temp *= utime;
4802 do_div(temp, total);
4803 }
4804 utime = (clock_t)temp;
4805
4806 p->prev_utime = max(p->prev_utime, clock_t_to_cputime(utime));
4807 return p->prev_utime;
4808}
4809
4810cputime_t task_stime(struct task_struct *p)
4811{
4812 clock_t stime;
4813
4814 /*
4815 * Use CFS's precise accounting. (we subtract utime from
4816 * the total, to make sure the total observed by userspace
4817 * grows monotonically - apps rely on that):
4818 */
4819 stime = nsec_to_clock_t(p->se.sum_exec_runtime) -
4820 cputime_to_clock_t(task_utime(p));
4821
4822 if (stime >= 0)
4823 p->prev_stime = max(p->prev_stime, clock_t_to_cputime(stime));
4824
4825 return p->prev_stime;
4826}
4827#endif
4828
4829inline cputime_t task_gtime(struct task_struct *p)
4830{
4831 return p->gtime;
4832}
4833
4834/*
Christoph Lameter7835b982006-12-10 02:20:22 -08004835 * This function gets called by the timer code, with HZ frequency.
4836 * We call it with interrupts disabled.
4837 *
4838 * It also gets called by the fork code, when changing the parent's
4839 * timeslices.
4840 */
4841void scheduler_tick(void)
4842{
Christoph Lameter7835b982006-12-10 02:20:22 -08004843 int cpu = smp_processor_id();
4844 struct rq *rq = cpu_rq(cpu);
Ingo Molnardd41f592007-07-09 18:51:59 +02004845 struct task_struct *curr = rq->curr;
Peter Zijlstra3e51f332008-05-03 18:29:28 +02004846
4847 sched_clock_tick();
Christoph Lameter7835b982006-12-10 02:20:22 -08004848
Ingo Molnardd41f592007-07-09 18:51:59 +02004849 spin_lock(&rq->lock);
Peter Zijlstra3e51f332008-05-03 18:29:28 +02004850 update_rq_clock(rq);
Ingo Molnarf1a438d2007-08-09 11:16:45 +02004851 update_cpu_load(rq);
Peter Zijlstrafa85ae22008-01-25 21:08:29 +01004852 curr->sched_class->task_tick(rq, curr, 0);
Ingo Molnaraa9c4c02008-12-17 14:10:57 +01004853 perf_counter_task_tick(curr, cpu);
Ingo Molnardd41f592007-07-09 18:51:59 +02004854 spin_unlock(&rq->lock);
4855
Christoph Lametere418e1c2006-12-10 02:20:23 -08004856#ifdef CONFIG_SMP
Ingo Molnardd41f592007-07-09 18:51:59 +02004857 rq->idle_at_tick = idle_cpu(cpu);
4858 trigger_load_balance(rq, cpu);
Christoph Lametere418e1c2006-12-10 02:20:23 -08004859#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07004860}
4861
Steven Rostedt7e49fcc2009-01-22 19:01:40 -05004862unsigned long get_parent_ip(unsigned long addr)
Steven Rostedt6cd8a4b2008-05-12 21:20:42 +02004863{
4864 if (in_lock_functions(addr)) {
4865 addr = CALLER_ADDR2;
4866 if (in_lock_functions(addr))
4867 addr = CALLER_ADDR3;
4868 }
4869 return addr;
4870}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004871
Steven Rostedt7e49fcc2009-01-22 19:01:40 -05004872#if defined(CONFIG_PREEMPT) && (defined(CONFIG_DEBUG_PREEMPT) || \
4873 defined(CONFIG_PREEMPT_TRACER))
4874
Srinivasa Ds43627582008-02-23 15:24:04 -08004875void __kprobes add_preempt_count(int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004876{
Steven Rostedt6cd8a4b2008-05-12 21:20:42 +02004877#ifdef CONFIG_DEBUG_PREEMPT
Linus Torvalds1da177e2005-04-16 15:20:36 -07004878 /*
4879 * Underflow?
4880 */
Ingo Molnar9a11b49a2006-07-03 00:24:33 -07004881 if (DEBUG_LOCKS_WARN_ON((preempt_count() < 0)))
4882 return;
Steven Rostedt6cd8a4b2008-05-12 21:20:42 +02004883#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07004884 preempt_count() += val;
Steven Rostedt6cd8a4b2008-05-12 21:20:42 +02004885#ifdef CONFIG_DEBUG_PREEMPT
Linus Torvalds1da177e2005-04-16 15:20:36 -07004886 /*
4887 * Spinlock count overflowing soon?
4888 */
Miguel Ojeda Sandonis33859f72006-12-10 02:20:38 -08004889 DEBUG_LOCKS_WARN_ON((preempt_count() & PREEMPT_MASK) >=
4890 PREEMPT_MASK - 10);
Steven Rostedt6cd8a4b2008-05-12 21:20:42 +02004891#endif
4892 if (preempt_count() == val)
4893 trace_preempt_off(CALLER_ADDR0, get_parent_ip(CALLER_ADDR1));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004894}
4895EXPORT_SYMBOL(add_preempt_count);
4896
Srinivasa Ds43627582008-02-23 15:24:04 -08004897void __kprobes sub_preempt_count(int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004898{
Steven Rostedt6cd8a4b2008-05-12 21:20:42 +02004899#ifdef CONFIG_DEBUG_PREEMPT
Linus Torvalds1da177e2005-04-16 15:20:36 -07004900 /*
4901 * Underflow?
4902 */
Ingo Molnar01e3eb82009-01-12 13:00:50 +01004903 if (DEBUG_LOCKS_WARN_ON(val > preempt_count()))
Ingo Molnar9a11b49a2006-07-03 00:24:33 -07004904 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004905 /*
4906 * Is the spinlock portion underflowing?
4907 */
Ingo Molnar9a11b49a2006-07-03 00:24:33 -07004908 if (DEBUG_LOCKS_WARN_ON((val < PREEMPT_MASK) &&
4909 !(preempt_count() & PREEMPT_MASK)))
4910 return;
Steven Rostedt6cd8a4b2008-05-12 21:20:42 +02004911#endif
Ingo Molnar9a11b49a2006-07-03 00:24:33 -07004912
Steven Rostedt6cd8a4b2008-05-12 21:20:42 +02004913 if (preempt_count() == val)
4914 trace_preempt_on(CALLER_ADDR0, get_parent_ip(CALLER_ADDR1));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004915 preempt_count() -= val;
4916}
4917EXPORT_SYMBOL(sub_preempt_count);
4918
4919#endif
4920
4921/*
Ingo Molnardd41f592007-07-09 18:51:59 +02004922 * Print scheduling while atomic bug:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004923 */
Ingo Molnardd41f592007-07-09 18:51:59 +02004924static noinline void __schedule_bug(struct task_struct *prev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004925{
Satyam Sharma838225b2007-10-24 18:23:50 +02004926 struct pt_regs *regs = get_irq_regs();
4927
4928 printk(KERN_ERR "BUG: scheduling while atomic: %s/%d/0x%08x\n",
4929 prev->comm, prev->pid, preempt_count());
4930
Ingo Molnardd41f592007-07-09 18:51:59 +02004931 debug_show_held_locks(prev);
Arjan van de Vene21f5b12008-05-23 09:05:58 -07004932 print_modules();
Ingo Molnardd41f592007-07-09 18:51:59 +02004933 if (irqs_disabled())
4934 print_irqtrace_events(prev);
Satyam Sharma838225b2007-10-24 18:23:50 +02004935
4936 if (regs)
4937 show_regs(regs);
4938 else
4939 dump_stack();
Ingo Molnardd41f592007-07-09 18:51:59 +02004940}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004941
Ingo Molnardd41f592007-07-09 18:51:59 +02004942/*
4943 * Various schedule()-time debugging checks and statistics:
4944 */
4945static inline void schedule_debug(struct task_struct *prev)
4946{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004947 /*
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01004948 * Test if we are atomic. Since do_exit() needs to call into
Linus Torvalds1da177e2005-04-16 15:20:36 -07004949 * schedule() atomically, we ignore that path for now.
4950 * Otherwise, whine if we are scheduling when we should not be.
4951 */
Roel Kluin3f33a7c2008-05-13 23:44:11 +02004952 if (unlikely(in_atomic_preempt_off() && !prev->exit_state))
Ingo Molnardd41f592007-07-09 18:51:59 +02004953 __schedule_bug(prev);
4954
Linus Torvalds1da177e2005-04-16 15:20:36 -07004955 profile_hit(SCHED_PROFILING, __builtin_return_address(0));
4956
Ingo Molnar2d723762007-10-15 17:00:12 +02004957 schedstat_inc(this_rq(), sched_count);
Ingo Molnarb8efb562007-10-15 17:00:10 +02004958#ifdef CONFIG_SCHEDSTATS
4959 if (unlikely(prev->lock_depth >= 0)) {
Ingo Molnar2d723762007-10-15 17:00:12 +02004960 schedstat_inc(this_rq(), bkl_count);
4961 schedstat_inc(prev, sched_info.bkl_count);
Ingo Molnarb8efb562007-10-15 17:00:10 +02004962 }
4963#endif
Ingo Molnardd41f592007-07-09 18:51:59 +02004964}
4965
Mike Galbraithdf1c99d2009-03-10 19:08:11 +01004966static void put_prev_task(struct rq *rq, struct task_struct *prev)
4967{
4968 if (prev->state == TASK_RUNNING) {
4969 u64 runtime = prev->se.sum_exec_runtime;
4970
4971 runtime -= prev->se.prev_sum_exec_runtime;
4972 runtime = min_t(u64, runtime, 2*sysctl_sched_migration_cost);
4973
4974 /*
4975 * In order to avoid avg_overlap growing stale when we are
4976 * indeed overlapping and hence not getting put to sleep, grow
4977 * the avg_overlap on preemption.
4978 *
4979 * We use the average preemption runtime because that
4980 * correlates to the amount of cache footprint a task can
4981 * build up.
4982 */
4983 update_avg(&prev->se.avg_overlap, runtime);
4984 }
4985 prev->sched_class->put_prev_task(rq, prev);
4986}
4987
Ingo Molnardd41f592007-07-09 18:51:59 +02004988/*
4989 * Pick up the highest-prio task:
4990 */
4991static inline struct task_struct *
Wang Chenb67802e2009-03-02 13:55:26 +08004992pick_next_task(struct rq *rq)
Ingo Molnardd41f592007-07-09 18:51:59 +02004993{
Ingo Molnar5522d5d2007-10-15 17:00:12 +02004994 const struct sched_class *class;
Ingo Molnardd41f592007-07-09 18:51:59 +02004995 struct task_struct *p;
4996
4997 /*
4998 * Optimization: we know that if all tasks are in
4999 * the fair class we can call that function directly:
5000 */
5001 if (likely(rq->nr_running == rq->cfs.nr_running)) {
Ingo Molnarfb8d4722007-08-09 11:16:48 +02005002 p = fair_sched_class.pick_next_task(rq);
Ingo Molnardd41f592007-07-09 18:51:59 +02005003 if (likely(p))
5004 return p;
5005 }
5006
5007 class = sched_class_highest;
5008 for ( ; ; ) {
Ingo Molnarfb8d4722007-08-09 11:16:48 +02005009 p = class->pick_next_task(rq);
Ingo Molnardd41f592007-07-09 18:51:59 +02005010 if (p)
5011 return p;
5012 /*
5013 * Will never be NULL as the idle class always
5014 * returns a non-NULL p:
5015 */
5016 class = class->next;
5017 }
5018}
5019
5020/*
5021 * schedule() is the main scheduler function.
5022 */
Peter Zijlstra41719b02009-01-14 15:36:26 +01005023asmlinkage void __sched __schedule(void)
Ingo Molnardd41f592007-07-09 18:51:59 +02005024{
5025 struct task_struct *prev, *next;
Harvey Harrison67ca7bd2008-02-15 09:56:36 -08005026 unsigned long *switch_count;
Ingo Molnardd41f592007-07-09 18:51:59 +02005027 struct rq *rq;
Peter Zijlstra31656512008-07-18 18:01:23 +02005028 int cpu;
Ingo Molnardd41f592007-07-09 18:51:59 +02005029
Ingo Molnardd41f592007-07-09 18:51:59 +02005030 cpu = smp_processor_id();
5031 rq = cpu_rq(cpu);
5032 rcu_qsctr_inc(cpu);
5033 prev = rq->curr;
5034 switch_count = &prev->nivcsw;
5035
Linus Torvalds1da177e2005-04-16 15:20:36 -07005036 release_kernel_lock(prev);
5037need_resched_nonpreemptible:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005038
Ingo Molnardd41f592007-07-09 18:51:59 +02005039 schedule_debug(prev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005040
Peter Zijlstra31656512008-07-18 18:01:23 +02005041 if (sched_feat(HRTICK))
Mike Galbraithf333fdc2008-05-12 21:20:55 +02005042 hrtick_clear(rq);
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01005043
Peter Zijlstra8cd162c2008-10-15 20:37:23 +02005044 spin_lock_irq(&rq->lock);
Peter Zijlstra3e51f332008-05-03 18:29:28 +02005045 update_rq_clock(rq);
Ingo Molnar1e819952007-10-15 17:00:13 +02005046 clear_tsk_need_resched(prev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005047
Ingo Molnardd41f592007-07-09 18:51:59 +02005048 if (prev->state && !(preempt_count() & PREEMPT_ACTIVE)) {
Oleg Nesterov16882c12008-06-08 21:20:41 +04005049 if (unlikely(signal_pending_state(prev->state, prev)))
Ingo Molnardd41f592007-07-09 18:51:59 +02005050 prev->state = TASK_RUNNING;
Oleg Nesterov16882c12008-06-08 21:20:41 +04005051 else
Ingo Molnar2e1cb742007-08-09 11:16:49 +02005052 deactivate_task(rq, prev, 1);
Ingo Molnardd41f592007-07-09 18:51:59 +02005053 switch_count = &prev->nvcsw;
5054 }
5055
Steven Rostedt9a897c52008-01-25 21:08:22 +01005056#ifdef CONFIG_SMP
5057 if (prev->sched_class->pre_schedule)
5058 prev->sched_class->pre_schedule(rq, prev);
5059#endif
Steven Rostedtf65eda42008-01-25 21:08:07 +01005060
Ingo Molnardd41f592007-07-09 18:51:59 +02005061 if (unlikely(!rq->nr_running))
5062 idle_balance(cpu, rq);
5063
Mike Galbraithdf1c99d2009-03-10 19:08:11 +01005064 put_prev_task(rq, prev);
Wang Chenb67802e2009-03-02 13:55:26 +08005065 next = pick_next_task(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005066
Linus Torvalds1da177e2005-04-16 15:20:36 -07005067 if (likely(prev != next)) {
David Simner673a90a2008-04-29 10:08:59 +01005068 sched_info_switch(prev, next);
Ingo Molnaraa9c4c02008-12-17 14:10:57 +01005069 perf_counter_task_sched_out(prev, cpu);
David Simner673a90a2008-04-29 10:08:59 +01005070
Linus Torvalds1da177e2005-04-16 15:20:36 -07005071 rq->nr_switches++;
5072 rq->curr = next;
5073 ++*switch_count;
5074
Ingo Molnardd41f592007-07-09 18:51:59 +02005075 context_switch(rq, prev, next); /* unlocks the rq */
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01005076 /*
5077 * the context switch might have flipped the stack from under
5078 * us, hence refresh the local variables.
5079 */
5080 cpu = smp_processor_id();
5081 rq = cpu_rq(cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005082 } else
5083 spin_unlock_irq(&rq->lock);
5084
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01005085 if (unlikely(reacquire_kernel_lock(current) < 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005086 goto need_resched_nonpreemptible;
Peter Zijlstra41719b02009-01-14 15:36:26 +01005087}
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01005088
Peter Zijlstra41719b02009-01-14 15:36:26 +01005089asmlinkage void __sched schedule(void)
5090{
5091need_resched:
5092 preempt_disable();
5093 __schedule();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005094 preempt_enable_no_resched();
5095 if (unlikely(test_thread_flag(TIF_NEED_RESCHED)))
5096 goto need_resched;
5097}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005098EXPORT_SYMBOL(schedule);
5099
Peter Zijlstra0d66bf62009-01-12 14:01:47 +01005100#ifdef CONFIG_SMP
5101/*
5102 * Look out! "owner" is an entirely speculative pointer
5103 * access and not reliable.
5104 */
5105int mutex_spin_on_owner(struct mutex *lock, struct thread_info *owner)
5106{
5107 unsigned int cpu;
5108 struct rq *rq;
5109
5110 if (!sched_feat(OWNER_SPIN))
5111 return 0;
5112
5113#ifdef CONFIG_DEBUG_PAGEALLOC
5114 /*
5115 * Need to access the cpu field knowing that
5116 * DEBUG_PAGEALLOC could have unmapped it if
5117 * the mutex owner just released it and exited.
5118 */
5119 if (probe_kernel_address(&owner->cpu, cpu))
5120 goto out;
5121#else
5122 cpu = owner->cpu;
5123#endif
5124
5125 /*
5126 * Even if the access succeeded (likely case),
5127 * the cpu field may no longer be valid.
5128 */
5129 if (cpu >= nr_cpumask_bits)
5130 goto out;
5131
5132 /*
5133 * We need to validate that we can do a
5134 * get_cpu() and that we have the percpu area.
5135 */
5136 if (!cpu_online(cpu))
5137 goto out;
5138
5139 rq = cpu_rq(cpu);
5140
5141 for (;;) {
5142 /*
5143 * Owner changed, break to re-assess state.
5144 */
5145 if (lock->owner != owner)
5146 break;
5147
5148 /*
5149 * Is that owner really running on that cpu?
5150 */
5151 if (task_thread_info(rq->curr) != owner || need_resched())
5152 return 0;
5153
5154 cpu_relax();
5155 }
5156out:
5157 return 1;
5158}
5159#endif
5160
Linus Torvalds1da177e2005-04-16 15:20:36 -07005161#ifdef CONFIG_PREEMPT
5162/*
Andreas Mohr2ed6e342006-07-10 04:43:52 -07005163 * this is the entry point to schedule() from in-kernel preemption
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01005164 * off of preempt_enable. Kernel preemptions off return from interrupt
Linus Torvalds1da177e2005-04-16 15:20:36 -07005165 * occur there and call schedule directly.
5166 */
5167asmlinkage void __sched preempt_schedule(void)
5168{
5169 struct thread_info *ti = current_thread_info();
Ingo Molnar6478d882008-01-25 21:08:33 +01005170
Linus Torvalds1da177e2005-04-16 15:20:36 -07005171 /*
5172 * If there is a non-zero preempt_count or interrupts are disabled,
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01005173 * we do not want to preempt the current task. Just return..
Linus Torvalds1da177e2005-04-16 15:20:36 -07005174 */
Nick Pigginbeed33a2006-10-11 01:21:52 -07005175 if (likely(ti->preempt_count || irqs_disabled()))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005176 return;
5177
Andi Kleen3a5c3592007-10-15 17:00:14 +02005178 do {
5179 add_preempt_count(PREEMPT_ACTIVE);
Andi Kleen3a5c3592007-10-15 17:00:14 +02005180 schedule();
Andi Kleen3a5c3592007-10-15 17:00:14 +02005181 sub_preempt_count(PREEMPT_ACTIVE);
5182
5183 /*
5184 * Check again in case we missed a preemption opportunity
5185 * between schedule and now.
5186 */
5187 barrier();
Lai Jiangshan5ed0cec2009-03-06 19:40:20 +08005188 } while (need_resched());
Linus Torvalds1da177e2005-04-16 15:20:36 -07005189}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005190EXPORT_SYMBOL(preempt_schedule);
5191
5192/*
Andreas Mohr2ed6e342006-07-10 04:43:52 -07005193 * this is the entry point to schedule() from kernel preemption
Linus Torvalds1da177e2005-04-16 15:20:36 -07005194 * off of irq context.
5195 * Note, that this is called and return with irqs disabled. This will
5196 * protect us against recursive calling from irq.
5197 */
5198asmlinkage void __sched preempt_schedule_irq(void)
5199{
5200 struct thread_info *ti = current_thread_info();
Ingo Molnar6478d882008-01-25 21:08:33 +01005201
Andreas Mohr2ed6e342006-07-10 04:43:52 -07005202 /* Catch callers which need to be fixed */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005203 BUG_ON(ti->preempt_count || !irqs_disabled());
5204
Andi Kleen3a5c3592007-10-15 17:00:14 +02005205 do {
5206 add_preempt_count(PREEMPT_ACTIVE);
Andi Kleen3a5c3592007-10-15 17:00:14 +02005207 local_irq_enable();
5208 schedule();
5209 local_irq_disable();
Andi Kleen3a5c3592007-10-15 17:00:14 +02005210 sub_preempt_count(PREEMPT_ACTIVE);
5211
5212 /*
5213 * Check again in case we missed a preemption opportunity
5214 * between schedule and now.
5215 */
5216 barrier();
Lai Jiangshan5ed0cec2009-03-06 19:40:20 +08005217 } while (need_resched());
Linus Torvalds1da177e2005-04-16 15:20:36 -07005218}
5219
5220#endif /* CONFIG_PREEMPT */
5221
Ingo Molnar95cdf3b2005-09-10 00:26:11 -07005222int default_wake_function(wait_queue_t *curr, unsigned mode, int sync,
5223 void *key)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005224{
Ingo Molnar48f24c42006-07-03 00:25:40 -07005225 return try_to_wake_up(curr->private, mode, sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005226}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005227EXPORT_SYMBOL(default_wake_function);
5228
5229/*
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01005230 * The core wakeup function. Non-exclusive wakeups (nr_exclusive == 0) just
5231 * wake everything up. If it's an exclusive wakeup (nr_exclusive == small +ve
Linus Torvalds1da177e2005-04-16 15:20:36 -07005232 * number) then we wake all the non-exclusive tasks and one exclusive task.
5233 *
5234 * There are circumstances in which we can try to wake a task which has already
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01005235 * started to run but is not in state TASK_RUNNING. try_to_wake_up() returns
Linus Torvalds1da177e2005-04-16 15:20:36 -07005236 * zero in this (rare) case, and we handle it by continuing to scan the queue.
5237 */
Johannes Weiner777c6c52009-02-04 15:12:14 -08005238void __wake_up_common(wait_queue_head_t *q, unsigned int mode,
5239 int nr_exclusive, int sync, void *key)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005240{
Matthias Kaehlcke2e458742007-10-15 17:00:02 +02005241 wait_queue_t *curr, *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005242
Matthias Kaehlcke2e458742007-10-15 17:00:02 +02005243 list_for_each_entry_safe(curr, next, &q->task_list, task_list) {
Ingo Molnar48f24c42006-07-03 00:25:40 -07005244 unsigned flags = curr->flags;
5245
Linus Torvalds1da177e2005-04-16 15:20:36 -07005246 if (curr->func(curr, mode, sync, key) &&
Ingo Molnar48f24c42006-07-03 00:25:40 -07005247 (flags & WQ_FLAG_EXCLUSIVE) && !--nr_exclusive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005248 break;
5249 }
5250}
5251
5252/**
5253 * __wake_up - wake up threads blocked on a waitqueue.
5254 * @q: the waitqueue
5255 * @mode: which threads
5256 * @nr_exclusive: how many wake-one or wake-many threads to wake up
Martin Waitz67be2dd2005-05-01 08:59:26 -07005257 * @key: is directly passed to the wakeup function
Linus Torvalds1da177e2005-04-16 15:20:36 -07005258 */
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -08005259void __wake_up(wait_queue_head_t *q, unsigned int mode,
Ingo Molnar95cdf3b2005-09-10 00:26:11 -07005260 int nr_exclusive, void *key)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005261{
5262 unsigned long flags;
5263
5264 spin_lock_irqsave(&q->lock, flags);
5265 __wake_up_common(q, mode, nr_exclusive, 0, key);
5266 spin_unlock_irqrestore(&q->lock, flags);
5267}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005268EXPORT_SYMBOL(__wake_up);
5269
5270/*
5271 * Same as __wake_up but called with the spinlock in wait_queue_head_t held.
5272 */
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -08005273void __wake_up_locked(wait_queue_head_t *q, unsigned int mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005274{
5275 __wake_up_common(q, mode, 1, 0, NULL);
5276}
5277
Davide Libenzi4ede8162009-03-31 15:24:20 -07005278void __wake_up_locked_key(wait_queue_head_t *q, unsigned int mode, void *key)
5279{
5280 __wake_up_common(q, mode, 1, 0, key);
5281}
5282
Linus Torvalds1da177e2005-04-16 15:20:36 -07005283/**
Davide Libenzi4ede8162009-03-31 15:24:20 -07005284 * __wake_up_sync_key - wake up threads blocked on a waitqueue.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005285 * @q: the waitqueue
5286 * @mode: which threads
5287 * @nr_exclusive: how many wake-one or wake-many threads to wake up
Davide Libenzi4ede8162009-03-31 15:24:20 -07005288 * @key: opaque value to be passed to wakeup targets
Linus Torvalds1da177e2005-04-16 15:20:36 -07005289 *
5290 * The sync wakeup differs that the waker knows that it will schedule
5291 * away soon, so while the target thread will be woken up, it will not
5292 * be migrated to another CPU - ie. the two threads are 'synchronized'
5293 * with each other. This can prevent needless bouncing between CPUs.
5294 *
5295 * On UP it can prevent extra preemption.
5296 */
Davide Libenzi4ede8162009-03-31 15:24:20 -07005297void __wake_up_sync_key(wait_queue_head_t *q, unsigned int mode,
5298 int nr_exclusive, void *key)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005299{
5300 unsigned long flags;
5301 int sync = 1;
5302
5303 if (unlikely(!q))
5304 return;
5305
5306 if (unlikely(!nr_exclusive))
5307 sync = 0;
5308
5309 spin_lock_irqsave(&q->lock, flags);
Davide Libenzi4ede8162009-03-31 15:24:20 -07005310 __wake_up_common(q, mode, nr_exclusive, sync, key);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005311 spin_unlock_irqrestore(&q->lock, flags);
5312}
Davide Libenzi4ede8162009-03-31 15:24:20 -07005313EXPORT_SYMBOL_GPL(__wake_up_sync_key);
5314
5315/*
5316 * __wake_up_sync - see __wake_up_sync_key()
5317 */
5318void __wake_up_sync(wait_queue_head_t *q, unsigned int mode, int nr_exclusive)
5319{
5320 __wake_up_sync_key(q, mode, nr_exclusive, NULL);
5321}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005322EXPORT_SYMBOL_GPL(__wake_up_sync); /* For internal use only */
5323
Kevin Diggs65eb3dc2008-08-26 10:26:54 +02005324/**
5325 * complete: - signals a single thread waiting on this completion
5326 * @x: holds the state of this particular completion
5327 *
5328 * This will wake up a single thread waiting on this completion. Threads will be
5329 * awakened in the same order in which they were queued.
5330 *
5331 * See also complete_all(), wait_for_completion() and related routines.
5332 */
Ingo Molnarb15136e2007-10-24 18:23:48 +02005333void complete(struct completion *x)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005334{
5335 unsigned long flags;
5336
5337 spin_lock_irqsave(&x->wait.lock, flags);
5338 x->done++;
Matthew Wilcoxd9514f62007-12-06 11:07:07 -05005339 __wake_up_common(&x->wait, TASK_NORMAL, 1, 0, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005340 spin_unlock_irqrestore(&x->wait.lock, flags);
5341}
5342EXPORT_SYMBOL(complete);
5343
Kevin Diggs65eb3dc2008-08-26 10:26:54 +02005344/**
5345 * complete_all: - signals all threads waiting on this completion
5346 * @x: holds the state of this particular completion
5347 *
5348 * This will wake up all threads waiting on this particular completion event.
5349 */
Ingo Molnarb15136e2007-10-24 18:23:48 +02005350void complete_all(struct completion *x)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005351{
5352 unsigned long flags;
5353
5354 spin_lock_irqsave(&x->wait.lock, flags);
5355 x->done += UINT_MAX/2;
Matthew Wilcoxd9514f62007-12-06 11:07:07 -05005356 __wake_up_common(&x->wait, TASK_NORMAL, 0, 0, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005357 spin_unlock_irqrestore(&x->wait.lock, flags);
5358}
5359EXPORT_SYMBOL(complete_all);
5360
Andi Kleen8cbbe862007-10-15 17:00:14 +02005361static inline long __sched
5362do_wait_for_common(struct completion *x, long timeout, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005363{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005364 if (!x->done) {
5365 DECLARE_WAITQUEUE(wait, current);
5366
5367 wait.flags |= WQ_FLAG_EXCLUSIVE;
5368 __add_wait_queue_tail(&x->wait, &wait);
5369 do {
Oleg Nesterov94d3d822008-08-20 16:54:41 -07005370 if (signal_pending_state(state, current)) {
Oleg Nesterovea71a542008-06-20 18:32:20 +04005371 timeout = -ERESTARTSYS;
5372 break;
Andi Kleen8cbbe862007-10-15 17:00:14 +02005373 }
5374 __set_current_state(state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005375 spin_unlock_irq(&x->wait.lock);
Andi Kleen8cbbe862007-10-15 17:00:14 +02005376 timeout = schedule_timeout(timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005377 spin_lock_irq(&x->wait.lock);
Oleg Nesterovea71a542008-06-20 18:32:20 +04005378 } while (!x->done && timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005379 __remove_wait_queue(&x->wait, &wait);
Oleg Nesterovea71a542008-06-20 18:32:20 +04005380 if (!x->done)
5381 return timeout;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005382 }
5383 x->done--;
Oleg Nesterovea71a542008-06-20 18:32:20 +04005384 return timeout ?: 1;
Andi Kleen8cbbe862007-10-15 17:00:14 +02005385}
5386
5387static long __sched
5388wait_for_common(struct completion *x, long timeout, int state)
5389{
5390 might_sleep();
5391
5392 spin_lock_irq(&x->wait.lock);
5393 timeout = do_wait_for_common(x, timeout, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005394 spin_unlock_irq(&x->wait.lock);
Andi Kleen8cbbe862007-10-15 17:00:14 +02005395 return timeout;
5396}
5397
Kevin Diggs65eb3dc2008-08-26 10:26:54 +02005398/**
5399 * wait_for_completion: - waits for completion of a task
5400 * @x: holds the state of this particular completion
5401 *
5402 * This waits to be signaled for completion of a specific task. It is NOT
5403 * interruptible and there is no timeout.
5404 *
5405 * See also similar routines (i.e. wait_for_completion_timeout()) with timeout
5406 * and interrupt capability. Also see complete().
5407 */
Ingo Molnarb15136e2007-10-24 18:23:48 +02005408void __sched wait_for_completion(struct completion *x)
Andi Kleen8cbbe862007-10-15 17:00:14 +02005409{
5410 wait_for_common(x, MAX_SCHEDULE_TIMEOUT, TASK_UNINTERRUPTIBLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005411}
5412EXPORT_SYMBOL(wait_for_completion);
5413
Kevin Diggs65eb3dc2008-08-26 10:26:54 +02005414/**
5415 * wait_for_completion_timeout: - waits for completion of a task (w/timeout)
5416 * @x: holds the state of this particular completion
5417 * @timeout: timeout value in jiffies
5418 *
5419 * This waits for either a completion of a specific task to be signaled or for a
5420 * specified timeout to expire. The timeout is in jiffies. It is not
5421 * interruptible.
5422 */
Ingo Molnarb15136e2007-10-24 18:23:48 +02005423unsigned long __sched
Linus Torvalds1da177e2005-04-16 15:20:36 -07005424wait_for_completion_timeout(struct completion *x, unsigned long timeout)
5425{
Andi Kleen8cbbe862007-10-15 17:00:14 +02005426 return wait_for_common(x, timeout, TASK_UNINTERRUPTIBLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005427}
5428EXPORT_SYMBOL(wait_for_completion_timeout);
5429
Kevin Diggs65eb3dc2008-08-26 10:26:54 +02005430/**
5431 * wait_for_completion_interruptible: - waits for completion of a task (w/intr)
5432 * @x: holds the state of this particular completion
5433 *
5434 * This waits for completion of a specific task to be signaled. It is
5435 * interruptible.
5436 */
Andi Kleen8cbbe862007-10-15 17:00:14 +02005437int __sched wait_for_completion_interruptible(struct completion *x)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005438{
Andi Kleen51e97992007-10-18 21:32:55 +02005439 long t = wait_for_common(x, MAX_SCHEDULE_TIMEOUT, TASK_INTERRUPTIBLE);
5440 if (t == -ERESTARTSYS)
5441 return t;
5442 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005443}
5444EXPORT_SYMBOL(wait_for_completion_interruptible);
5445
Kevin Diggs65eb3dc2008-08-26 10:26:54 +02005446/**
5447 * wait_for_completion_interruptible_timeout: - waits for completion (w/(to,intr))
5448 * @x: holds the state of this particular completion
5449 * @timeout: timeout value in jiffies
5450 *
5451 * This waits for either a completion of a specific task to be signaled or for a
5452 * specified timeout to expire. It is interruptible. The timeout is in jiffies.
5453 */
Ingo Molnarb15136e2007-10-24 18:23:48 +02005454unsigned long __sched
Linus Torvalds1da177e2005-04-16 15:20:36 -07005455wait_for_completion_interruptible_timeout(struct completion *x,
5456 unsigned long timeout)
5457{
Andi Kleen8cbbe862007-10-15 17:00:14 +02005458 return wait_for_common(x, timeout, TASK_INTERRUPTIBLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005459}
5460EXPORT_SYMBOL(wait_for_completion_interruptible_timeout);
5461
Kevin Diggs65eb3dc2008-08-26 10:26:54 +02005462/**
5463 * wait_for_completion_killable: - waits for completion of a task (killable)
5464 * @x: holds the state of this particular completion
5465 *
5466 * This waits to be signaled for completion of a specific task. It can be
5467 * interrupted by a kill signal.
5468 */
Matthew Wilcox009e5772007-12-06 12:29:54 -05005469int __sched wait_for_completion_killable(struct completion *x)
5470{
5471 long t = wait_for_common(x, MAX_SCHEDULE_TIMEOUT, TASK_KILLABLE);
5472 if (t == -ERESTARTSYS)
5473 return t;
5474 return 0;
5475}
5476EXPORT_SYMBOL(wait_for_completion_killable);
5477
Dave Chinnerbe4de352008-08-15 00:40:44 -07005478/**
5479 * try_wait_for_completion - try to decrement a completion without blocking
5480 * @x: completion structure
5481 *
5482 * Returns: 0 if a decrement cannot be done without blocking
5483 * 1 if a decrement succeeded.
5484 *
5485 * If a completion is being used as a counting completion,
5486 * attempt to decrement the counter without blocking. This
5487 * enables us to avoid waiting if the resource the completion
5488 * is protecting is not available.
5489 */
5490bool try_wait_for_completion(struct completion *x)
5491{
5492 int ret = 1;
5493
5494 spin_lock_irq(&x->wait.lock);
5495 if (!x->done)
5496 ret = 0;
5497 else
5498 x->done--;
5499 spin_unlock_irq(&x->wait.lock);
5500 return ret;
5501}
5502EXPORT_SYMBOL(try_wait_for_completion);
5503
5504/**
5505 * completion_done - Test to see if a completion has any waiters
5506 * @x: completion structure
5507 *
5508 * Returns: 0 if there are waiters (wait_for_completion() in progress)
5509 * 1 if there are no waiters.
5510 *
5511 */
5512bool completion_done(struct completion *x)
5513{
5514 int ret = 1;
5515
5516 spin_lock_irq(&x->wait.lock);
5517 if (!x->done)
5518 ret = 0;
5519 spin_unlock_irq(&x->wait.lock);
5520 return ret;
5521}
5522EXPORT_SYMBOL(completion_done);
5523
Andi Kleen8cbbe862007-10-15 17:00:14 +02005524static long __sched
5525sleep_on_common(wait_queue_head_t *q, int state, long timeout)
Ingo Molnar0fec1712007-07-09 18:52:01 +02005526{
5527 unsigned long flags;
5528 wait_queue_t wait;
5529
5530 init_waitqueue_entry(&wait, current);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005531
Andi Kleen8cbbe862007-10-15 17:00:14 +02005532 __set_current_state(state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005533
Andi Kleen8cbbe862007-10-15 17:00:14 +02005534 spin_lock_irqsave(&q->lock, flags);
5535 __add_wait_queue(q, &wait);
5536 spin_unlock(&q->lock);
5537 timeout = schedule_timeout(timeout);
5538 spin_lock_irq(&q->lock);
5539 __remove_wait_queue(q, &wait);
5540 spin_unlock_irqrestore(&q->lock, flags);
5541
5542 return timeout;
5543}
5544
5545void __sched interruptible_sleep_on(wait_queue_head_t *q)
5546{
5547 sleep_on_common(q, TASK_INTERRUPTIBLE, MAX_SCHEDULE_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005548}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005549EXPORT_SYMBOL(interruptible_sleep_on);
5550
Ingo Molnar0fec1712007-07-09 18:52:01 +02005551long __sched
Ingo Molnar95cdf3b2005-09-10 00:26:11 -07005552interruptible_sleep_on_timeout(wait_queue_head_t *q, long timeout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005553{
Andi Kleen8cbbe862007-10-15 17:00:14 +02005554 return sleep_on_common(q, TASK_INTERRUPTIBLE, timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005555}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005556EXPORT_SYMBOL(interruptible_sleep_on_timeout);
5557
Ingo Molnar0fec1712007-07-09 18:52:01 +02005558void __sched sleep_on(wait_queue_head_t *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005559{
Andi Kleen8cbbe862007-10-15 17:00:14 +02005560 sleep_on_common(q, TASK_UNINTERRUPTIBLE, MAX_SCHEDULE_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005561}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005562EXPORT_SYMBOL(sleep_on);
5563
Ingo Molnar0fec1712007-07-09 18:52:01 +02005564long __sched sleep_on_timeout(wait_queue_head_t *q, long timeout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005565{
Andi Kleen8cbbe862007-10-15 17:00:14 +02005566 return sleep_on_common(q, TASK_UNINTERRUPTIBLE, timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005567}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005568EXPORT_SYMBOL(sleep_on_timeout);
5569
Ingo Molnarb29739f2006-06-27 02:54:51 -07005570#ifdef CONFIG_RT_MUTEXES
5571
5572/*
5573 * rt_mutex_setprio - set the current priority of a task
5574 * @p: task
5575 * @prio: prio value (kernel-internal form)
5576 *
5577 * This function changes the 'effective' priority of a task. It does
5578 * not touch ->normal_prio like __setscheduler().
5579 *
5580 * Used by the rt_mutex code to implement priority inheritance logic.
5581 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07005582void rt_mutex_setprio(struct task_struct *p, int prio)
Ingo Molnarb29739f2006-06-27 02:54:51 -07005583{
5584 unsigned long flags;
Srivatsa Vaddagiri83b699e2007-10-15 17:00:08 +02005585 int oldprio, on_rq, running;
Ingo Molnar70b97a72006-07-03 00:25:42 -07005586 struct rq *rq;
Steven Rostedtcb469842008-01-25 21:08:22 +01005587 const struct sched_class *prev_class = p->sched_class;
Ingo Molnarb29739f2006-06-27 02:54:51 -07005588
5589 BUG_ON(prio < 0 || prio > MAX_PRIO);
5590
5591 rq = task_rq_lock(p, &flags);
Ingo Molnara8e504d2007-08-09 11:16:47 +02005592 update_rq_clock(rq);
Ingo Molnarb29739f2006-06-27 02:54:51 -07005593
Andrew Mortond5f9f942007-05-08 20:27:06 -07005594 oldprio = p->prio;
Ingo Molnardd41f592007-07-09 18:51:59 +02005595 on_rq = p->se.on_rq;
Dmitry Adamushko051a1d12007-12-18 15:21:13 +01005596 running = task_current(rq, p);
Hiroshi Shimamoto0e1f3482008-03-10 11:01:20 -07005597 if (on_rq)
Ingo Molnar69be72c2007-08-09 11:16:49 +02005598 dequeue_task(rq, p, 0);
Hiroshi Shimamoto0e1f3482008-03-10 11:01:20 -07005599 if (running)
5600 p->sched_class->put_prev_task(rq, p);
Ingo Molnardd41f592007-07-09 18:51:59 +02005601
5602 if (rt_prio(prio))
5603 p->sched_class = &rt_sched_class;
5604 else
5605 p->sched_class = &fair_sched_class;
5606
Ingo Molnarb29739f2006-06-27 02:54:51 -07005607 p->prio = prio;
5608
Hiroshi Shimamoto0e1f3482008-03-10 11:01:20 -07005609 if (running)
5610 p->sched_class->set_curr_task(rq);
Ingo Molnardd41f592007-07-09 18:51:59 +02005611 if (on_rq) {
Ingo Molnar8159f872007-08-09 11:16:49 +02005612 enqueue_task(rq, p, 0);
Steven Rostedtcb469842008-01-25 21:08:22 +01005613
5614 check_class_changed(rq, p, prev_class, oldprio, running);
Ingo Molnarb29739f2006-06-27 02:54:51 -07005615 }
5616 task_rq_unlock(rq, &flags);
5617}
5618
5619#endif
5620
Ingo Molnar36c8b582006-07-03 00:25:41 -07005621void set_user_nice(struct task_struct *p, long nice)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005622{
Ingo Molnardd41f592007-07-09 18:51:59 +02005623 int old_prio, delta, on_rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005624 unsigned long flags;
Ingo Molnar70b97a72006-07-03 00:25:42 -07005625 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005626
5627 if (TASK_NICE(p) == nice || nice < -20 || nice > 19)
5628 return;
5629 /*
5630 * We have to be careful, if called from sys_setpriority(),
5631 * the task might be in the middle of scheduling on another CPU.
5632 */
5633 rq = task_rq_lock(p, &flags);
Ingo Molnara8e504d2007-08-09 11:16:47 +02005634 update_rq_clock(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005635 /*
5636 * The RT priorities are set via sched_setscheduler(), but we still
5637 * allow the 'normal' nice value to be set - but as expected
5638 * it wont have any effect on scheduling until the task is
Ingo Molnardd41f592007-07-09 18:51:59 +02005639 * SCHED_FIFO/SCHED_RR:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005640 */
Ingo Molnare05606d2007-07-09 18:51:59 +02005641 if (task_has_rt_policy(p)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005642 p->static_prio = NICE_TO_PRIO(nice);
5643 goto out_unlock;
5644 }
Ingo Molnardd41f592007-07-09 18:51:59 +02005645 on_rq = p->se.on_rq;
Peter Zijlstrac09595f2008-06-27 13:41:14 +02005646 if (on_rq)
Ingo Molnar69be72c2007-08-09 11:16:49 +02005647 dequeue_task(rq, p, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005648
Linus Torvalds1da177e2005-04-16 15:20:36 -07005649 p->static_prio = NICE_TO_PRIO(nice);
Peter Williams2dd73a42006-06-27 02:54:34 -07005650 set_load_weight(p);
Ingo Molnarb29739f2006-06-27 02:54:51 -07005651 old_prio = p->prio;
5652 p->prio = effective_prio(p);
5653 delta = p->prio - old_prio;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005654
Ingo Molnardd41f592007-07-09 18:51:59 +02005655 if (on_rq) {
Ingo Molnar8159f872007-08-09 11:16:49 +02005656 enqueue_task(rq, p, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005657 /*
Andrew Mortond5f9f942007-05-08 20:27:06 -07005658 * If the task increased its priority or is running and
5659 * lowered its priority, then reschedule its CPU:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005660 */
Andrew Mortond5f9f942007-05-08 20:27:06 -07005661 if (delta < 0 || (delta > 0 && task_running(rq, p)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005662 resched_task(rq->curr);
5663 }
5664out_unlock:
5665 task_rq_unlock(rq, &flags);
5666}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005667EXPORT_SYMBOL(set_user_nice);
5668
Matt Mackalle43379f2005-05-01 08:59:00 -07005669/*
5670 * can_nice - check if a task can reduce its nice value
5671 * @p: task
5672 * @nice: nice value
5673 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07005674int can_nice(const struct task_struct *p, const int nice)
Matt Mackalle43379f2005-05-01 08:59:00 -07005675{
Matt Mackall024f4742005-08-18 11:24:19 -07005676 /* convert nice value [19,-20] to rlimit style value [1,40] */
5677 int nice_rlim = 20 - nice;
Ingo Molnar48f24c42006-07-03 00:25:40 -07005678
Matt Mackalle43379f2005-05-01 08:59:00 -07005679 return (nice_rlim <= p->signal->rlim[RLIMIT_NICE].rlim_cur ||
5680 capable(CAP_SYS_NICE));
5681}
5682
Linus Torvalds1da177e2005-04-16 15:20:36 -07005683#ifdef __ARCH_WANT_SYS_NICE
5684
5685/*
5686 * sys_nice - change the priority of the current process.
5687 * @increment: priority increment
5688 *
5689 * sys_setpriority is a more generic, but much slower function that
5690 * does similar things.
5691 */
Heiko Carstens5add95d2009-01-14 14:14:08 +01005692SYSCALL_DEFINE1(nice, int, increment)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005693{
Ingo Molnar48f24c42006-07-03 00:25:40 -07005694 long nice, retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005695
5696 /*
5697 * Setpriority might change our priority at the same moment.
5698 * We don't have to worry. Conceptually one call occurs first
5699 * and we have a single winner.
5700 */
Matt Mackalle43379f2005-05-01 08:59:00 -07005701 if (increment < -40)
5702 increment = -40;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005703 if (increment > 40)
5704 increment = 40;
5705
Américo Wang2b8f8362009-02-16 18:54:21 +08005706 nice = TASK_NICE(current) + increment;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005707 if (nice < -20)
5708 nice = -20;
5709 if (nice > 19)
5710 nice = 19;
5711
Matt Mackalle43379f2005-05-01 08:59:00 -07005712 if (increment < 0 && !can_nice(current, nice))
5713 return -EPERM;
5714
Linus Torvalds1da177e2005-04-16 15:20:36 -07005715 retval = security_task_setnice(current, nice);
5716 if (retval)
5717 return retval;
5718
5719 set_user_nice(current, nice);
5720 return 0;
5721}
5722
5723#endif
5724
5725/**
5726 * task_prio - return the priority value of a given task.
5727 * @p: the task in question.
5728 *
5729 * This is the priority value as seen by users in /proc.
5730 * RT tasks are offset by -200. Normal tasks are centered
5731 * around 0, value goes from -16 to +15.
5732 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07005733int task_prio(const struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005734{
5735 return p->prio - MAX_RT_PRIO;
5736}
5737
5738/**
5739 * task_nice - return the nice value of a given task.
5740 * @p: the task in question.
5741 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07005742int task_nice(const struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005743{
5744 return TASK_NICE(p);
5745}
Pavel Roskin150d8be2008-03-05 16:56:37 -05005746EXPORT_SYMBOL(task_nice);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005747
5748/**
5749 * idle_cpu - is a given cpu idle currently?
5750 * @cpu: the processor in question.
5751 */
5752int idle_cpu(int cpu)
5753{
5754 return cpu_curr(cpu) == cpu_rq(cpu)->idle;
5755}
5756
Linus Torvalds1da177e2005-04-16 15:20:36 -07005757/**
5758 * idle_task - return the idle task for a given cpu.
5759 * @cpu: the processor in question.
5760 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07005761struct task_struct *idle_task(int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005762{
5763 return cpu_rq(cpu)->idle;
5764}
5765
5766/**
5767 * find_process_by_pid - find a process with a matching PID value.
5768 * @pid: the pid in question.
5769 */
Alexey Dobriyana9957442007-10-15 17:00:13 +02005770static struct task_struct *find_process_by_pid(pid_t pid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005771{
Pavel Emelyanov228ebcb2007-10-18 23:40:16 -07005772 return pid ? find_task_by_vpid(pid) : current;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005773}
5774
5775/* Actually do priority change: must hold rq lock. */
Ingo Molnardd41f592007-07-09 18:51:59 +02005776static void
5777__setscheduler(struct rq *rq, struct task_struct *p, int policy, int prio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005778{
Ingo Molnardd41f592007-07-09 18:51:59 +02005779 BUG_ON(p->se.on_rq);
Ingo Molnar48f24c42006-07-03 00:25:40 -07005780
Linus Torvalds1da177e2005-04-16 15:20:36 -07005781 p->policy = policy;
Ingo Molnardd41f592007-07-09 18:51:59 +02005782 switch (p->policy) {
5783 case SCHED_NORMAL:
5784 case SCHED_BATCH:
5785 case SCHED_IDLE:
5786 p->sched_class = &fair_sched_class;
5787 break;
5788 case SCHED_FIFO:
5789 case SCHED_RR:
5790 p->sched_class = &rt_sched_class;
5791 break;
5792 }
5793
Linus Torvalds1da177e2005-04-16 15:20:36 -07005794 p->rt_priority = prio;
Ingo Molnarb29739f2006-06-27 02:54:51 -07005795 p->normal_prio = normal_prio(p);
5796 /* we are holding p->pi_lock already */
5797 p->prio = rt_mutex_getprio(p);
Peter Williams2dd73a42006-06-27 02:54:34 -07005798 set_load_weight(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005799}
5800
David Howellsc69e8d92008-11-14 10:39:19 +11005801/*
5802 * check the target process has a UID that matches the current process's
5803 */
5804static bool check_same_owner(struct task_struct *p)
5805{
5806 const struct cred *cred = current_cred(), *pcred;
5807 bool match;
5808
5809 rcu_read_lock();
5810 pcred = __task_cred(p);
5811 match = (cred->euid == pcred->euid ||
5812 cred->euid == pcred->uid);
5813 rcu_read_unlock();
5814 return match;
5815}
5816
Rusty Russell961ccdd2008-06-23 13:55:38 +10005817static int __sched_setscheduler(struct task_struct *p, int policy,
5818 struct sched_param *param, bool user)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005819{
Srivatsa Vaddagiri83b699e2007-10-15 17:00:08 +02005820 int retval, oldprio, oldpolicy = -1, on_rq, running;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005821 unsigned long flags;
Steven Rostedtcb469842008-01-25 21:08:22 +01005822 const struct sched_class *prev_class = p->sched_class;
Ingo Molnar70b97a72006-07-03 00:25:42 -07005823 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005824
Steven Rostedt66e53932006-06-27 02:54:44 -07005825 /* may grab non-irq protected spin_locks */
5826 BUG_ON(in_interrupt());
Linus Torvalds1da177e2005-04-16 15:20:36 -07005827recheck:
5828 /* double check policy once rq lock held */
5829 if (policy < 0)
5830 policy = oldpolicy = p->policy;
5831 else if (policy != SCHED_FIFO && policy != SCHED_RR &&
Ingo Molnardd41f592007-07-09 18:51:59 +02005832 policy != SCHED_NORMAL && policy != SCHED_BATCH &&
5833 policy != SCHED_IDLE)
Ingo Molnarb0a94992006-01-14 13:20:41 -08005834 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005835 /*
5836 * Valid priorities for SCHED_FIFO and SCHED_RR are
Ingo Molnardd41f592007-07-09 18:51:59 +02005837 * 1..MAX_USER_RT_PRIO-1, valid priority for SCHED_NORMAL,
5838 * SCHED_BATCH and SCHED_IDLE is 0.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005839 */
5840 if (param->sched_priority < 0 ||
Ingo Molnar95cdf3b2005-09-10 00:26:11 -07005841 (p->mm && param->sched_priority > MAX_USER_RT_PRIO-1) ||
Steven Rostedtd46523e2005-07-25 16:28:39 -04005842 (!p->mm && param->sched_priority > MAX_RT_PRIO-1))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005843 return -EINVAL;
Ingo Molnare05606d2007-07-09 18:51:59 +02005844 if (rt_policy(policy) != (param->sched_priority != 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005845 return -EINVAL;
5846
Olivier Croquette37e4ab32005-06-25 14:57:32 -07005847 /*
5848 * Allow unprivileged RT tasks to decrease priority:
5849 */
Rusty Russell961ccdd2008-06-23 13:55:38 +10005850 if (user && !capable(CAP_SYS_NICE)) {
Ingo Molnare05606d2007-07-09 18:51:59 +02005851 if (rt_policy(policy)) {
Oleg Nesterov8dc3e902006-09-29 02:00:50 -07005852 unsigned long rlim_rtprio;
Oleg Nesterov5fe1d752006-09-29 02:00:48 -07005853
Oleg Nesterov8dc3e902006-09-29 02:00:50 -07005854 if (!lock_task_sighand(p, &flags))
5855 return -ESRCH;
5856 rlim_rtprio = p->signal->rlim[RLIMIT_RTPRIO].rlim_cur;
5857 unlock_task_sighand(p, &flags);
Oleg Nesterov5fe1d752006-09-29 02:00:48 -07005858
Oleg Nesterov8dc3e902006-09-29 02:00:50 -07005859 /* can't set/change the rt policy */
5860 if (policy != p->policy && !rlim_rtprio)
5861 return -EPERM;
5862
5863 /* can't increase priority */
5864 if (param->sched_priority > p->rt_priority &&
5865 param->sched_priority > rlim_rtprio)
5866 return -EPERM;
5867 }
Ingo Molnardd41f592007-07-09 18:51:59 +02005868 /*
5869 * Like positive nice levels, dont allow tasks to
5870 * move out of SCHED_IDLE either:
5871 */
5872 if (p->policy == SCHED_IDLE && policy != SCHED_IDLE)
5873 return -EPERM;
Oleg Nesterov8dc3e902006-09-29 02:00:50 -07005874
Olivier Croquette37e4ab32005-06-25 14:57:32 -07005875 /* can't change other user's priorities */
David Howellsc69e8d92008-11-14 10:39:19 +11005876 if (!check_same_owner(p))
Olivier Croquette37e4ab32005-06-25 14:57:32 -07005877 return -EPERM;
5878 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005879
Jeremy Fitzhardinge725aad22008-08-03 09:33:03 -07005880 if (user) {
Peter Zijlstrab68aa232008-02-13 15:45:40 +01005881#ifdef CONFIG_RT_GROUP_SCHED
Jeremy Fitzhardinge725aad22008-08-03 09:33:03 -07005882 /*
5883 * Do not allow realtime tasks into groups that have no runtime
5884 * assigned.
5885 */
Peter Zijlstra9a7e0b12008-08-19 12:33:06 +02005886 if (rt_bandwidth_enabled() && rt_policy(policy) &&
5887 task_group(p)->rt_bandwidth.rt_runtime == 0)
Jeremy Fitzhardinge725aad22008-08-03 09:33:03 -07005888 return -EPERM;
Peter Zijlstrab68aa232008-02-13 15:45:40 +01005889#endif
5890
Jeremy Fitzhardinge725aad22008-08-03 09:33:03 -07005891 retval = security_task_setscheduler(p, policy, param);
5892 if (retval)
5893 return retval;
5894 }
5895
Linus Torvalds1da177e2005-04-16 15:20:36 -07005896 /*
Ingo Molnarb29739f2006-06-27 02:54:51 -07005897 * make sure no PI-waiters arrive (or leave) while we are
5898 * changing the priority of the task:
5899 */
5900 spin_lock_irqsave(&p->pi_lock, flags);
5901 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07005902 * To be able to change p->policy safely, the apropriate
5903 * runqueue lock must be held.
5904 */
Ingo Molnarb29739f2006-06-27 02:54:51 -07005905 rq = __task_rq_lock(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005906 /* recheck policy now with rq lock held */
5907 if (unlikely(oldpolicy != -1 && oldpolicy != p->policy)) {
5908 policy = oldpolicy = -1;
Ingo Molnarb29739f2006-06-27 02:54:51 -07005909 __task_rq_unlock(rq);
5910 spin_unlock_irqrestore(&p->pi_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005911 goto recheck;
5912 }
Ingo Molnar2daa3572007-08-09 11:16:51 +02005913 update_rq_clock(rq);
Ingo Molnardd41f592007-07-09 18:51:59 +02005914 on_rq = p->se.on_rq;
Dmitry Adamushko051a1d12007-12-18 15:21:13 +01005915 running = task_current(rq, p);
Hiroshi Shimamoto0e1f3482008-03-10 11:01:20 -07005916 if (on_rq)
Ingo Molnar2e1cb742007-08-09 11:16:49 +02005917 deactivate_task(rq, p, 0);
Hiroshi Shimamoto0e1f3482008-03-10 11:01:20 -07005918 if (running)
5919 p->sched_class->put_prev_task(rq, p);
Dmitry Adamushkof6b53202007-10-15 17:00:08 +02005920
Linus Torvalds1da177e2005-04-16 15:20:36 -07005921 oldprio = p->prio;
Ingo Molnardd41f592007-07-09 18:51:59 +02005922 __setscheduler(rq, p, policy, param->sched_priority);
Dmitry Adamushkof6b53202007-10-15 17:00:08 +02005923
Hiroshi Shimamoto0e1f3482008-03-10 11:01:20 -07005924 if (running)
5925 p->sched_class->set_curr_task(rq);
Ingo Molnardd41f592007-07-09 18:51:59 +02005926 if (on_rq) {
5927 activate_task(rq, p, 0);
Steven Rostedtcb469842008-01-25 21:08:22 +01005928
5929 check_class_changed(rq, p, prev_class, oldprio, running);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005930 }
Ingo Molnarb29739f2006-06-27 02:54:51 -07005931 __task_rq_unlock(rq);
5932 spin_unlock_irqrestore(&p->pi_lock, flags);
5933
Thomas Gleixner95e02ca2006-06-27 02:55:02 -07005934 rt_mutex_adjust_pi(p);
5935
Linus Torvalds1da177e2005-04-16 15:20:36 -07005936 return 0;
5937}
Rusty Russell961ccdd2008-06-23 13:55:38 +10005938
5939/**
5940 * sched_setscheduler - change the scheduling policy and/or RT priority of a thread.
5941 * @p: the task in question.
5942 * @policy: new policy.
5943 * @param: structure containing the new RT priority.
5944 *
5945 * NOTE that the task may be already dead.
5946 */
5947int sched_setscheduler(struct task_struct *p, int policy,
5948 struct sched_param *param)
5949{
5950 return __sched_setscheduler(p, policy, param, true);
5951}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005952EXPORT_SYMBOL_GPL(sched_setscheduler);
5953
Rusty Russell961ccdd2008-06-23 13:55:38 +10005954/**
5955 * sched_setscheduler_nocheck - change the scheduling policy and/or RT priority of a thread from kernelspace.
5956 * @p: the task in question.
5957 * @policy: new policy.
5958 * @param: structure containing the new RT priority.
5959 *
5960 * Just like sched_setscheduler, only don't bother checking if the
5961 * current context has permission. For example, this is needed in
5962 * stop_machine(): we create temporary high priority worker threads,
5963 * but our caller might not have that capability.
5964 */
5965int sched_setscheduler_nocheck(struct task_struct *p, int policy,
5966 struct sched_param *param)
5967{
5968 return __sched_setscheduler(p, policy, param, false);
5969}
5970
Ingo Molnar95cdf3b2005-09-10 00:26:11 -07005971static int
5972do_sched_setscheduler(pid_t pid, int policy, struct sched_param __user *param)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005973{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005974 struct sched_param lparam;
5975 struct task_struct *p;
Ingo Molnar36c8b582006-07-03 00:25:41 -07005976 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005977
5978 if (!param || pid < 0)
5979 return -EINVAL;
5980 if (copy_from_user(&lparam, param, sizeof(struct sched_param)))
5981 return -EFAULT;
Oleg Nesterov5fe1d752006-09-29 02:00:48 -07005982
5983 rcu_read_lock();
5984 retval = -ESRCH;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005985 p = find_process_by_pid(pid);
Oleg Nesterov5fe1d752006-09-29 02:00:48 -07005986 if (p != NULL)
5987 retval = sched_setscheduler(p, policy, &lparam);
5988 rcu_read_unlock();
Ingo Molnar36c8b582006-07-03 00:25:41 -07005989
Linus Torvalds1da177e2005-04-16 15:20:36 -07005990 return retval;
5991}
5992
5993/**
5994 * sys_sched_setscheduler - set/change the scheduler policy and RT priority
5995 * @pid: the pid in question.
5996 * @policy: new policy.
5997 * @param: structure containing the new RT priority.
5998 */
Heiko Carstens5add95d2009-01-14 14:14:08 +01005999SYSCALL_DEFINE3(sched_setscheduler, pid_t, pid, int, policy,
6000 struct sched_param __user *, param)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006001{
Jason Baronc21761f2006-01-18 17:43:03 -08006002 /* negative values for policy are not valid */
6003 if (policy < 0)
6004 return -EINVAL;
6005
Linus Torvalds1da177e2005-04-16 15:20:36 -07006006 return do_sched_setscheduler(pid, policy, param);
6007}
6008
6009/**
6010 * sys_sched_setparam - set/change the RT priority of a thread
6011 * @pid: the pid in question.
6012 * @param: structure containing the new RT priority.
6013 */
Heiko Carstens5add95d2009-01-14 14:14:08 +01006014SYSCALL_DEFINE2(sched_setparam, pid_t, pid, struct sched_param __user *, param)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006015{
6016 return do_sched_setscheduler(pid, -1, param);
6017}
6018
6019/**
6020 * sys_sched_getscheduler - get the policy (scheduling class) of a thread
6021 * @pid: the pid in question.
6022 */
Heiko Carstens5add95d2009-01-14 14:14:08 +01006023SYSCALL_DEFINE1(sched_getscheduler, pid_t, pid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006024{
Ingo Molnar36c8b582006-07-03 00:25:41 -07006025 struct task_struct *p;
Andi Kleen3a5c3592007-10-15 17:00:14 +02006026 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006027
6028 if (pid < 0)
Andi Kleen3a5c3592007-10-15 17:00:14 +02006029 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006030
6031 retval = -ESRCH;
6032 read_lock(&tasklist_lock);
6033 p = find_process_by_pid(pid);
6034 if (p) {
6035 retval = security_task_getscheduler(p);
6036 if (!retval)
6037 retval = p->policy;
6038 }
6039 read_unlock(&tasklist_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006040 return retval;
6041}
6042
6043/**
6044 * sys_sched_getscheduler - get the RT priority of a thread
6045 * @pid: the pid in question.
6046 * @param: structure containing the RT priority.
6047 */
Heiko Carstens5add95d2009-01-14 14:14:08 +01006048SYSCALL_DEFINE2(sched_getparam, pid_t, pid, struct sched_param __user *, param)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006049{
6050 struct sched_param lp;
Ingo Molnar36c8b582006-07-03 00:25:41 -07006051 struct task_struct *p;
Andi Kleen3a5c3592007-10-15 17:00:14 +02006052 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006053
6054 if (!param || pid < 0)
Andi Kleen3a5c3592007-10-15 17:00:14 +02006055 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006056
6057 read_lock(&tasklist_lock);
6058 p = find_process_by_pid(pid);
6059 retval = -ESRCH;
6060 if (!p)
6061 goto out_unlock;
6062
6063 retval = security_task_getscheduler(p);
6064 if (retval)
6065 goto out_unlock;
6066
6067 lp.sched_priority = p->rt_priority;
6068 read_unlock(&tasklist_lock);
6069
6070 /*
6071 * This one might sleep, we cannot do it with a spinlock held ...
6072 */
6073 retval = copy_to_user(param, &lp, sizeof(*param)) ? -EFAULT : 0;
6074
Linus Torvalds1da177e2005-04-16 15:20:36 -07006075 return retval;
6076
6077out_unlock:
6078 read_unlock(&tasklist_lock);
6079 return retval;
6080}
6081
Rusty Russell96f874e2008-11-25 02:35:14 +10306082long sched_setaffinity(pid_t pid, const struct cpumask *in_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006083{
Rusty Russell5a16f3d2008-11-25 02:35:11 +10306084 cpumask_var_t cpus_allowed, new_mask;
Ingo Molnar36c8b582006-07-03 00:25:41 -07006085 struct task_struct *p;
6086 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006087
Gautham R Shenoy95402b32008-01-25 21:08:02 +01006088 get_online_cpus();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006089 read_lock(&tasklist_lock);
6090
6091 p = find_process_by_pid(pid);
6092 if (!p) {
6093 read_unlock(&tasklist_lock);
Gautham R Shenoy95402b32008-01-25 21:08:02 +01006094 put_online_cpus();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006095 return -ESRCH;
6096 }
6097
6098 /*
6099 * It is not safe to call set_cpus_allowed with the
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01006100 * tasklist_lock held. We will bump the task_struct's
Linus Torvalds1da177e2005-04-16 15:20:36 -07006101 * usage count and then drop tasklist_lock.
6102 */
6103 get_task_struct(p);
6104 read_unlock(&tasklist_lock);
6105
Rusty Russell5a16f3d2008-11-25 02:35:11 +10306106 if (!alloc_cpumask_var(&cpus_allowed, GFP_KERNEL)) {
6107 retval = -ENOMEM;
6108 goto out_put_task;
6109 }
6110 if (!alloc_cpumask_var(&new_mask, GFP_KERNEL)) {
6111 retval = -ENOMEM;
6112 goto out_free_cpus_allowed;
6113 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006114 retval = -EPERM;
David Howellsc69e8d92008-11-14 10:39:19 +11006115 if (!check_same_owner(p) && !capable(CAP_SYS_NICE))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006116 goto out_unlock;
6117
David Quigleye7834f82006-06-23 02:03:59 -07006118 retval = security_task_setscheduler(p, 0, NULL);
6119 if (retval)
6120 goto out_unlock;
6121
Rusty Russell5a16f3d2008-11-25 02:35:11 +10306122 cpuset_cpus_allowed(p, cpus_allowed);
6123 cpumask_and(new_mask, in_mask, cpus_allowed);
Paul Menage8707d8b2007-10-18 23:40:22 -07006124 again:
Rusty Russell5a16f3d2008-11-25 02:35:11 +10306125 retval = set_cpus_allowed_ptr(p, new_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006126
Paul Menage8707d8b2007-10-18 23:40:22 -07006127 if (!retval) {
Rusty Russell5a16f3d2008-11-25 02:35:11 +10306128 cpuset_cpus_allowed(p, cpus_allowed);
6129 if (!cpumask_subset(new_mask, cpus_allowed)) {
Paul Menage8707d8b2007-10-18 23:40:22 -07006130 /*
6131 * We must have raced with a concurrent cpuset
6132 * update. Just reset the cpus_allowed to the
6133 * cpuset's cpus_allowed
6134 */
Rusty Russell5a16f3d2008-11-25 02:35:11 +10306135 cpumask_copy(new_mask, cpus_allowed);
Paul Menage8707d8b2007-10-18 23:40:22 -07006136 goto again;
6137 }
6138 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006139out_unlock:
Rusty Russell5a16f3d2008-11-25 02:35:11 +10306140 free_cpumask_var(new_mask);
6141out_free_cpus_allowed:
6142 free_cpumask_var(cpus_allowed);
6143out_put_task:
Linus Torvalds1da177e2005-04-16 15:20:36 -07006144 put_task_struct(p);
Gautham R Shenoy95402b32008-01-25 21:08:02 +01006145 put_online_cpus();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006146 return retval;
6147}
6148
6149static int get_user_cpu_mask(unsigned long __user *user_mask_ptr, unsigned len,
Rusty Russell96f874e2008-11-25 02:35:14 +10306150 struct cpumask *new_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006151{
Rusty Russell96f874e2008-11-25 02:35:14 +10306152 if (len < cpumask_size())
6153 cpumask_clear(new_mask);
6154 else if (len > cpumask_size())
6155 len = cpumask_size();
6156
Linus Torvalds1da177e2005-04-16 15:20:36 -07006157 return copy_from_user(new_mask, user_mask_ptr, len) ? -EFAULT : 0;
6158}
6159
6160/**
6161 * sys_sched_setaffinity - set the cpu affinity of a process
6162 * @pid: pid of the process
6163 * @len: length in bytes of the bitmask pointed to by user_mask_ptr
6164 * @user_mask_ptr: user-space pointer to the new cpu mask
6165 */
Heiko Carstens5add95d2009-01-14 14:14:08 +01006166SYSCALL_DEFINE3(sched_setaffinity, pid_t, pid, unsigned int, len,
6167 unsigned long __user *, user_mask_ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006168{
Rusty Russell5a16f3d2008-11-25 02:35:11 +10306169 cpumask_var_t new_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006170 int retval;
6171
Rusty Russell5a16f3d2008-11-25 02:35:11 +10306172 if (!alloc_cpumask_var(&new_mask, GFP_KERNEL))
6173 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006174
Rusty Russell5a16f3d2008-11-25 02:35:11 +10306175 retval = get_user_cpu_mask(user_mask_ptr, len, new_mask);
6176 if (retval == 0)
6177 retval = sched_setaffinity(pid, new_mask);
6178 free_cpumask_var(new_mask);
6179 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006180}
6181
Rusty Russell96f874e2008-11-25 02:35:14 +10306182long sched_getaffinity(pid_t pid, struct cpumask *mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006183{
Ingo Molnar36c8b582006-07-03 00:25:41 -07006184 struct task_struct *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006185 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006186
Gautham R Shenoy95402b32008-01-25 21:08:02 +01006187 get_online_cpus();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006188 read_lock(&tasklist_lock);
6189
6190 retval = -ESRCH;
6191 p = find_process_by_pid(pid);
6192 if (!p)
6193 goto out_unlock;
6194
David Quigleye7834f82006-06-23 02:03:59 -07006195 retval = security_task_getscheduler(p);
6196 if (retval)
6197 goto out_unlock;
6198
Rusty Russell96f874e2008-11-25 02:35:14 +10306199 cpumask_and(mask, &p->cpus_allowed, cpu_online_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006200
6201out_unlock:
6202 read_unlock(&tasklist_lock);
Gautham R Shenoy95402b32008-01-25 21:08:02 +01006203 put_online_cpus();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006204
Ulrich Drepper9531b622007-08-09 11:16:46 +02006205 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006206}
6207
6208/**
6209 * sys_sched_getaffinity - get the cpu affinity of a process
6210 * @pid: pid of the process
6211 * @len: length in bytes of the bitmask pointed to by user_mask_ptr
6212 * @user_mask_ptr: user-space pointer to hold the current cpu mask
6213 */
Heiko Carstens5add95d2009-01-14 14:14:08 +01006214SYSCALL_DEFINE3(sched_getaffinity, pid_t, pid, unsigned int, len,
6215 unsigned long __user *, user_mask_ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006216{
6217 int ret;
Rusty Russellf17c8602008-11-25 02:35:11 +10306218 cpumask_var_t mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006219
Rusty Russellf17c8602008-11-25 02:35:11 +10306220 if (len < cpumask_size())
Linus Torvalds1da177e2005-04-16 15:20:36 -07006221 return -EINVAL;
6222
Rusty Russellf17c8602008-11-25 02:35:11 +10306223 if (!alloc_cpumask_var(&mask, GFP_KERNEL))
6224 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006225
Rusty Russellf17c8602008-11-25 02:35:11 +10306226 ret = sched_getaffinity(pid, mask);
6227 if (ret == 0) {
6228 if (copy_to_user(user_mask_ptr, mask, cpumask_size()))
6229 ret = -EFAULT;
6230 else
6231 ret = cpumask_size();
6232 }
6233 free_cpumask_var(mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006234
Rusty Russellf17c8602008-11-25 02:35:11 +10306235 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006236}
6237
6238/**
6239 * sys_sched_yield - yield the current processor to other threads.
6240 *
Ingo Molnardd41f592007-07-09 18:51:59 +02006241 * This function yields the current CPU to other tasks. If there are no
6242 * other threads running on this CPU then this function will return.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006243 */
Heiko Carstens5add95d2009-01-14 14:14:08 +01006244SYSCALL_DEFINE0(sched_yield)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006245{
Ingo Molnar70b97a72006-07-03 00:25:42 -07006246 struct rq *rq = this_rq_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006247
Ingo Molnar2d723762007-10-15 17:00:12 +02006248 schedstat_inc(rq, yld_count);
Dmitry Adamushko4530d7a2007-10-15 17:00:08 +02006249 current->sched_class->yield_task(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006250
6251 /*
6252 * Since we are going to call schedule() anyway, there's
6253 * no need to preempt or enable interrupts:
6254 */
6255 __release(rq->lock);
Ingo Molnar8a25d5d2006-07-03 00:24:54 -07006256 spin_release(&rq->lock.dep_map, 1, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006257 _raw_spin_unlock(&rq->lock);
6258 preempt_enable_no_resched();
6259
6260 schedule();
6261
6262 return 0;
6263}
6264
Andrew Mortone7b38402006-06-30 01:56:00 -07006265static void __cond_resched(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006266{
Ingo Molnar8e0a43d2006-06-23 02:05:23 -07006267#ifdef CONFIG_DEBUG_SPINLOCK_SLEEP
6268 __might_sleep(__FILE__, __LINE__);
6269#endif
Ingo Molnar5bbcfd92005-07-07 17:57:04 -07006270 /*
6271 * The BKS might be reacquired before we have dropped
6272 * PREEMPT_ACTIVE, which could trigger a second
6273 * cond_resched() call.
6274 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006275 do {
6276 add_preempt_count(PREEMPT_ACTIVE);
6277 schedule();
6278 sub_preempt_count(PREEMPT_ACTIVE);
6279 } while (need_resched());
6280}
6281
Herbert Xu02b67cc32008-01-25 21:08:28 +01006282int __sched _cond_resched(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006283{
Ingo Molnar94142322006-12-29 16:48:13 -08006284 if (need_resched() && !(preempt_count() & PREEMPT_ACTIVE) &&
6285 system_state == SYSTEM_RUNNING) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006286 __cond_resched();
6287 return 1;
6288 }
6289 return 0;
6290}
Herbert Xu02b67cc32008-01-25 21:08:28 +01006291EXPORT_SYMBOL(_cond_resched);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006292
6293/*
6294 * cond_resched_lock() - if a reschedule is pending, drop the given lock,
6295 * call schedule, and on return reacquire the lock.
6296 *
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01006297 * This works OK both with and without CONFIG_PREEMPT. We do strange low-level
Linus Torvalds1da177e2005-04-16 15:20:36 -07006298 * operations here to prevent schedule() from being called twice (once via
6299 * spin_unlock(), once by hand).
6300 */
Ingo Molnar95cdf3b2005-09-10 00:26:11 -07006301int cond_resched_lock(spinlock_t *lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006302{
Nick Piggin95c354f2008-01-30 13:31:20 +01006303 int resched = need_resched() && system_state == SYSTEM_RUNNING;
Jan Kara6df3cec2005-06-13 15:52:32 -07006304 int ret = 0;
6305
Nick Piggin95c354f2008-01-30 13:31:20 +01006306 if (spin_needbreak(lock) || resched) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006307 spin_unlock(lock);
Nick Piggin95c354f2008-01-30 13:31:20 +01006308 if (resched && need_resched())
6309 __cond_resched();
6310 else
6311 cpu_relax();
Jan Kara6df3cec2005-06-13 15:52:32 -07006312 ret = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006313 spin_lock(lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006314 }
Jan Kara6df3cec2005-06-13 15:52:32 -07006315 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006316}
Linus Torvalds1da177e2005-04-16 15:20:36 -07006317EXPORT_SYMBOL(cond_resched_lock);
6318
6319int __sched cond_resched_softirq(void)
6320{
6321 BUG_ON(!in_softirq());
6322
Ingo Molnar94142322006-12-29 16:48:13 -08006323 if (need_resched() && system_state == SYSTEM_RUNNING) {
Thomas Gleixner98d825672007-05-23 13:58:18 -07006324 local_bh_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006325 __cond_resched();
6326 local_bh_disable();
6327 return 1;
6328 }
6329 return 0;
6330}
Linus Torvalds1da177e2005-04-16 15:20:36 -07006331EXPORT_SYMBOL(cond_resched_softirq);
6332
Linus Torvalds1da177e2005-04-16 15:20:36 -07006333/**
6334 * yield - yield the current processor to other threads.
6335 *
Robert P. J. Day72fd4a32007-02-10 01:45:59 -08006336 * This is a shortcut for kernel-space yielding - it marks the
Linus Torvalds1da177e2005-04-16 15:20:36 -07006337 * thread runnable and calls sys_sched_yield().
6338 */
6339void __sched yield(void)
6340{
6341 set_current_state(TASK_RUNNING);
6342 sys_sched_yield();
6343}
Linus Torvalds1da177e2005-04-16 15:20:36 -07006344EXPORT_SYMBOL(yield);
6345
6346/*
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01006347 * This task is about to go to sleep on IO. Increment rq->nr_iowait so
Linus Torvalds1da177e2005-04-16 15:20:36 -07006348 * that process accounting knows that this is a task in IO wait state.
6349 *
6350 * But don't do that if it is a deliberate, throttling IO wait (this task
6351 * has set its backing_dev_info: the queue against which it should throttle)
6352 */
6353void __sched io_schedule(void)
6354{
Ingo Molnar70b97a72006-07-03 00:25:42 -07006355 struct rq *rq = &__raw_get_cpu_var(runqueues);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006356
Shailabh Nagar0ff92242006-07-14 00:24:37 -07006357 delayacct_blkio_start();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006358 atomic_inc(&rq->nr_iowait);
6359 schedule();
6360 atomic_dec(&rq->nr_iowait);
Shailabh Nagar0ff92242006-07-14 00:24:37 -07006361 delayacct_blkio_end();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006362}
Linus Torvalds1da177e2005-04-16 15:20:36 -07006363EXPORT_SYMBOL(io_schedule);
6364
6365long __sched io_schedule_timeout(long timeout)
6366{
Ingo Molnar70b97a72006-07-03 00:25:42 -07006367 struct rq *rq = &__raw_get_cpu_var(runqueues);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006368 long ret;
6369
Shailabh Nagar0ff92242006-07-14 00:24:37 -07006370 delayacct_blkio_start();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006371 atomic_inc(&rq->nr_iowait);
6372 ret = schedule_timeout(timeout);
6373 atomic_dec(&rq->nr_iowait);
Shailabh Nagar0ff92242006-07-14 00:24:37 -07006374 delayacct_blkio_end();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006375 return ret;
6376}
6377
6378/**
6379 * sys_sched_get_priority_max - return maximum RT priority.
6380 * @policy: scheduling class.
6381 *
6382 * this syscall returns the maximum rt_priority that can be used
6383 * by a given scheduling class.
6384 */
Heiko Carstens5add95d2009-01-14 14:14:08 +01006385SYSCALL_DEFINE1(sched_get_priority_max, int, policy)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006386{
6387 int ret = -EINVAL;
6388
6389 switch (policy) {
6390 case SCHED_FIFO:
6391 case SCHED_RR:
6392 ret = MAX_USER_RT_PRIO-1;
6393 break;
6394 case SCHED_NORMAL:
Ingo Molnarb0a94992006-01-14 13:20:41 -08006395 case SCHED_BATCH:
Ingo Molnardd41f592007-07-09 18:51:59 +02006396 case SCHED_IDLE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07006397 ret = 0;
6398 break;
6399 }
6400 return ret;
6401}
6402
6403/**
6404 * sys_sched_get_priority_min - return minimum RT priority.
6405 * @policy: scheduling class.
6406 *
6407 * this syscall returns the minimum rt_priority that can be used
6408 * by a given scheduling class.
6409 */
Heiko Carstens5add95d2009-01-14 14:14:08 +01006410SYSCALL_DEFINE1(sched_get_priority_min, int, policy)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006411{
6412 int ret = -EINVAL;
6413
6414 switch (policy) {
6415 case SCHED_FIFO:
6416 case SCHED_RR:
6417 ret = 1;
6418 break;
6419 case SCHED_NORMAL:
Ingo Molnarb0a94992006-01-14 13:20:41 -08006420 case SCHED_BATCH:
Ingo Molnardd41f592007-07-09 18:51:59 +02006421 case SCHED_IDLE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07006422 ret = 0;
6423 }
6424 return ret;
6425}
6426
6427/**
6428 * sys_sched_rr_get_interval - return the default timeslice of a process.
6429 * @pid: pid of the process.
6430 * @interval: userspace pointer to the timeslice value.
6431 *
6432 * this syscall writes the default timeslice value of a given process
6433 * into the user-space timespec buffer. A value of '0' means infinity.
6434 */
Heiko Carstens17da2bd2009-01-14 14:14:10 +01006435SYSCALL_DEFINE2(sched_rr_get_interval, pid_t, pid,
Heiko Carstens754fe8d2009-01-14 14:14:09 +01006436 struct timespec __user *, interval)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006437{
Ingo Molnar36c8b582006-07-03 00:25:41 -07006438 struct task_struct *p;
Dmitry Adamushkoa4ec24b2007-10-15 17:00:13 +02006439 unsigned int time_slice;
Andi Kleen3a5c3592007-10-15 17:00:14 +02006440 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006441 struct timespec t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006442
6443 if (pid < 0)
Andi Kleen3a5c3592007-10-15 17:00:14 +02006444 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006445
6446 retval = -ESRCH;
6447 read_lock(&tasklist_lock);
6448 p = find_process_by_pid(pid);
6449 if (!p)
6450 goto out_unlock;
6451
6452 retval = security_task_getscheduler(p);
6453 if (retval)
6454 goto out_unlock;
6455
Ingo Molnar77034932007-12-04 17:04:39 +01006456 /*
6457 * Time slice is 0 for SCHED_FIFO tasks and for SCHED_OTHER
6458 * tasks that are on an otherwise idle runqueue:
6459 */
6460 time_slice = 0;
6461 if (p->policy == SCHED_RR) {
Dmitry Adamushkoa4ec24b2007-10-15 17:00:13 +02006462 time_slice = DEF_TIMESLICE;
Miao Xie1868f952008-03-07 09:35:06 +08006463 } else if (p->policy != SCHED_FIFO) {
Dmitry Adamushkoa4ec24b2007-10-15 17:00:13 +02006464 struct sched_entity *se = &p->se;
6465 unsigned long flags;
6466 struct rq *rq;
6467
6468 rq = task_rq_lock(p, &flags);
Ingo Molnar77034932007-12-04 17:04:39 +01006469 if (rq->cfs.load.weight)
6470 time_slice = NS_TO_JIFFIES(sched_slice(&rq->cfs, se));
Dmitry Adamushkoa4ec24b2007-10-15 17:00:13 +02006471 task_rq_unlock(rq, &flags);
6472 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006473 read_unlock(&tasklist_lock);
Dmitry Adamushkoa4ec24b2007-10-15 17:00:13 +02006474 jiffies_to_timespec(time_slice, &t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006475 retval = copy_to_user(interval, &t, sizeof(t)) ? -EFAULT : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006476 return retval;
Andi Kleen3a5c3592007-10-15 17:00:14 +02006477
Linus Torvalds1da177e2005-04-16 15:20:36 -07006478out_unlock:
6479 read_unlock(&tasklist_lock);
6480 return retval;
6481}
6482
Steven Rostedt7c731e02008-05-12 21:20:41 +02006483static const char stat_nam[] = TASK_STATE_TO_CHAR_STR;
Ingo Molnar36c8b582006-07-03 00:25:41 -07006484
Ingo Molnar82a1fcb2008-01-25 21:08:02 +01006485void sched_show_task(struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006486{
Linus Torvalds1da177e2005-04-16 15:20:36 -07006487 unsigned long free = 0;
Ingo Molnar36c8b582006-07-03 00:25:41 -07006488 unsigned state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006489
Linus Torvalds1da177e2005-04-16 15:20:36 -07006490 state = p->state ? __ffs(p->state) + 1 : 0;
Ingo Molnarcc4ea792007-10-18 21:32:56 +02006491 printk(KERN_INFO "%-13.13s %c", p->comm,
Andreas Mohr2ed6e342006-07-10 04:43:52 -07006492 state < sizeof(stat_nam) - 1 ? stat_nam[state] : '?');
Ingo Molnar4bd77322007-07-11 21:21:47 +02006493#if BITS_PER_LONG == 32
Linus Torvalds1da177e2005-04-16 15:20:36 -07006494 if (state == TASK_RUNNING)
Ingo Molnarcc4ea792007-10-18 21:32:56 +02006495 printk(KERN_CONT " running ");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006496 else
Ingo Molnarcc4ea792007-10-18 21:32:56 +02006497 printk(KERN_CONT " %08lx ", thread_saved_pc(p));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006498#else
6499 if (state == TASK_RUNNING)
Ingo Molnarcc4ea792007-10-18 21:32:56 +02006500 printk(KERN_CONT " running task ");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006501 else
Ingo Molnarcc4ea792007-10-18 21:32:56 +02006502 printk(KERN_CONT " %016lx ", thread_saved_pc(p));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006503#endif
6504#ifdef CONFIG_DEBUG_STACK_USAGE
Eric Sandeen7c9f8862008-04-22 16:38:23 -05006505 free = stack_not_used(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006506#endif
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -07006507 printk(KERN_CONT "%5lu %5d %6d\n", free,
Roland McGrathfcfd50a2008-01-09 00:03:23 -08006508 task_pid_nr(p), task_pid_nr(p->real_parent));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006509
Nick Piggin5fb5e6d2008-01-25 21:08:34 +01006510 show_stack(p, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006511}
6512
Ingo Molnare59e2ae2006-12-06 20:35:59 -08006513void show_state_filter(unsigned long state_filter)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006514{
Ingo Molnar36c8b582006-07-03 00:25:41 -07006515 struct task_struct *g, *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006516
Ingo Molnar4bd77322007-07-11 21:21:47 +02006517#if BITS_PER_LONG == 32
6518 printk(KERN_INFO
6519 " task PC stack pid father\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006520#else
Ingo Molnar4bd77322007-07-11 21:21:47 +02006521 printk(KERN_INFO
6522 " task PC stack pid father\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006523#endif
6524 read_lock(&tasklist_lock);
6525 do_each_thread(g, p) {
6526 /*
6527 * reset the NMI-timeout, listing all files on a slow
6528 * console might take alot of time:
6529 */
6530 touch_nmi_watchdog();
Ingo Molnar39bc89f2007-04-25 20:50:03 -07006531 if (!state_filter || (p->state & state_filter))
Ingo Molnar82a1fcb2008-01-25 21:08:02 +01006532 sched_show_task(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006533 } while_each_thread(g, p);
6534
Jeremy Fitzhardinge04c91672007-05-08 00:28:05 -07006535 touch_all_softlockup_watchdogs();
6536
Ingo Molnardd41f592007-07-09 18:51:59 +02006537#ifdef CONFIG_SCHED_DEBUG
6538 sysrq_sched_debug_show();
6539#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07006540 read_unlock(&tasklist_lock);
Ingo Molnare59e2ae2006-12-06 20:35:59 -08006541 /*
6542 * Only show locks if all tasks are dumped:
6543 */
6544 if (state_filter == -1)
6545 debug_show_all_locks();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006546}
6547
Ingo Molnar1df21052007-07-09 18:51:58 +02006548void __cpuinit init_idle_bootup_task(struct task_struct *idle)
6549{
Ingo Molnardd41f592007-07-09 18:51:59 +02006550 idle->sched_class = &idle_sched_class;
Ingo Molnar1df21052007-07-09 18:51:58 +02006551}
6552
Ingo Molnarf340c0d2005-06-28 16:40:42 +02006553/**
6554 * init_idle - set up an idle thread for a given CPU
6555 * @idle: task in question
6556 * @cpu: cpu the idle task belongs to
6557 *
6558 * NOTE: this function does not set the idle thread's NEED_RESCHED
6559 * flag, to make booting more robust.
6560 */
Nick Piggin5c1e1762006-10-03 01:14:04 -07006561void __cpuinit init_idle(struct task_struct *idle, int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006562{
Ingo Molnar70b97a72006-07-03 00:25:42 -07006563 struct rq *rq = cpu_rq(cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006564 unsigned long flags;
6565
Ingo Molnar5cbd54e2008-11-12 20:05:50 +01006566 spin_lock_irqsave(&rq->lock, flags);
6567
Ingo Molnardd41f592007-07-09 18:51:59 +02006568 __sched_fork(idle);
6569 idle->se.exec_start = sched_clock();
6570
Ingo Molnarb29739f2006-06-27 02:54:51 -07006571 idle->prio = idle->normal_prio = MAX_PRIO;
Rusty Russell96f874e2008-11-25 02:35:14 +10306572 cpumask_copy(&idle->cpus_allowed, cpumask_of(cpu));
Ingo Molnardd41f592007-07-09 18:51:59 +02006573 __set_task_cpu(idle, cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006574
Linus Torvalds1da177e2005-04-16 15:20:36 -07006575 rq->curr = rq->idle = idle;
Nick Piggin4866cde2005-06-25 14:57:23 -07006576#if defined(CONFIG_SMP) && defined(__ARCH_WANT_UNLOCKED_CTXSW)
6577 idle->oncpu = 1;
6578#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07006579 spin_unlock_irqrestore(&rq->lock, flags);
6580
6581 /* Set the preempt count _outside_ the spinlocks! */
Linus Torvalds8e3e0762008-05-10 20:58:02 -07006582#if defined(CONFIG_PREEMPT)
6583 task_thread_info(idle)->preempt_count = (idle->lock_depth >= 0);
6584#else
Al Viroa1261f52005-11-13 16:06:55 -08006585 task_thread_info(idle)->preempt_count = 0;
Linus Torvalds8e3e0762008-05-10 20:58:02 -07006586#endif
Ingo Molnardd41f592007-07-09 18:51:59 +02006587 /*
6588 * The idle tasks have their own, simple scheduling class:
6589 */
6590 idle->sched_class = &idle_sched_class;
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01006591 ftrace_graph_init_task(idle);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006592}
6593
6594/*
6595 * In a system that switches off the HZ timer nohz_cpu_mask
6596 * indicates which cpus entered this state. This is used
6597 * in the rcu update to wait only for active cpus. For system
6598 * which do not switch off the HZ timer nohz_cpu_mask should
Rusty Russell6a7b3dc2008-11-25 02:35:04 +10306599 * always be CPU_BITS_NONE.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006600 */
Rusty Russell6a7b3dc2008-11-25 02:35:04 +10306601cpumask_var_t nohz_cpu_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006602
Ingo Molnar19978ca2007-11-09 22:39:38 +01006603/*
6604 * Increase the granularity value when there are more CPUs,
6605 * because with more CPUs the 'effective latency' as visible
6606 * to users decreases. But the relationship is not linear,
6607 * so pick a second-best guess by going with the log2 of the
6608 * number of CPUs.
6609 *
6610 * This idea comes from the SD scheduler of Con Kolivas:
6611 */
6612static inline void sched_init_granularity(void)
6613{
6614 unsigned int factor = 1 + ilog2(num_online_cpus());
6615 const unsigned long limit = 200000000;
6616
6617 sysctl_sched_min_granularity *= factor;
6618 if (sysctl_sched_min_granularity > limit)
6619 sysctl_sched_min_granularity = limit;
6620
6621 sysctl_sched_latency *= factor;
6622 if (sysctl_sched_latency > limit)
6623 sysctl_sched_latency = limit;
6624
6625 sysctl_sched_wakeup_granularity *= factor;
Peter Zijlstra55cd5342008-08-04 08:54:26 +02006626
6627 sysctl_sched_shares_ratelimit *= factor;
Ingo Molnar19978ca2007-11-09 22:39:38 +01006628}
6629
Linus Torvalds1da177e2005-04-16 15:20:36 -07006630#ifdef CONFIG_SMP
6631/*
6632 * This is how migration works:
6633 *
Ingo Molnar70b97a72006-07-03 00:25:42 -07006634 * 1) we queue a struct migration_req structure in the source CPU's
Linus Torvalds1da177e2005-04-16 15:20:36 -07006635 * runqueue and wake up that CPU's migration thread.
6636 * 2) we down() the locked semaphore => thread blocks.
6637 * 3) migration thread wakes up (implicitly it forces the migrated
6638 * thread off the CPU)
6639 * 4) it gets the migration request and checks whether the migrated
6640 * task is still in the wrong runqueue.
6641 * 5) if it's in the wrong runqueue then the migration thread removes
6642 * it and puts it into the right queue.
6643 * 6) migration thread up()s the semaphore.
6644 * 7) we wake up and the migration is done.
6645 */
6646
6647/*
6648 * Change a given task's CPU affinity. Migrate the thread to a
6649 * proper CPU and schedule it away if the CPU it's executing on
6650 * is removed from the allowed bitmask.
6651 *
6652 * NOTE: the caller must have a valid reference to the task, the
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01006653 * task must not exit() & deallocate itself prematurely. The
Linus Torvalds1da177e2005-04-16 15:20:36 -07006654 * call is not atomic; no spinlocks may be held.
6655 */
Rusty Russell96f874e2008-11-25 02:35:14 +10306656int set_cpus_allowed_ptr(struct task_struct *p, const struct cpumask *new_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006657{
Ingo Molnar70b97a72006-07-03 00:25:42 -07006658 struct migration_req req;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006659 unsigned long flags;
Ingo Molnar70b97a72006-07-03 00:25:42 -07006660 struct rq *rq;
Ingo Molnar48f24c42006-07-03 00:25:40 -07006661 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006662
6663 rq = task_rq_lock(p, &flags);
Rusty Russell96f874e2008-11-25 02:35:14 +10306664 if (!cpumask_intersects(new_mask, cpu_online_mask)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006665 ret = -EINVAL;
6666 goto out;
6667 }
6668
David Rientjes9985b0b2008-06-05 12:57:11 -07006669 if (unlikely((p->flags & PF_THREAD_BOUND) && p != current &&
Rusty Russell96f874e2008-11-25 02:35:14 +10306670 !cpumask_equal(&p->cpus_allowed, new_mask))) {
David Rientjes9985b0b2008-06-05 12:57:11 -07006671 ret = -EINVAL;
6672 goto out;
6673 }
6674
Gregory Haskins73fe6aa2008-01-25 21:08:07 +01006675 if (p->sched_class->set_cpus_allowed)
Mike Traviscd8ba7c2008-03-26 14:23:49 -07006676 p->sched_class->set_cpus_allowed(p, new_mask);
Gregory Haskins73fe6aa2008-01-25 21:08:07 +01006677 else {
Rusty Russell96f874e2008-11-25 02:35:14 +10306678 cpumask_copy(&p->cpus_allowed, new_mask);
6679 p->rt.nr_cpus_allowed = cpumask_weight(new_mask);
Gregory Haskins73fe6aa2008-01-25 21:08:07 +01006680 }
6681
Linus Torvalds1da177e2005-04-16 15:20:36 -07006682 /* Can the task run on the task's current CPU? If so, we're done */
Rusty Russell96f874e2008-11-25 02:35:14 +10306683 if (cpumask_test_cpu(task_cpu(p), new_mask))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006684 goto out;
6685
Rusty Russell1e5ce4f2008-11-25 02:35:03 +10306686 if (migrate_task(p, cpumask_any_and(cpu_online_mask, new_mask), &req)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006687 /* Need help from migration thread: drop lock and wait. */
6688 task_rq_unlock(rq, &flags);
6689 wake_up_process(rq->migration_thread);
6690 wait_for_completion(&req.done);
6691 tlb_migrate_finish(p->mm);
6692 return 0;
6693 }
6694out:
6695 task_rq_unlock(rq, &flags);
Ingo Molnar48f24c42006-07-03 00:25:40 -07006696
Linus Torvalds1da177e2005-04-16 15:20:36 -07006697 return ret;
6698}
Mike Traviscd8ba7c2008-03-26 14:23:49 -07006699EXPORT_SYMBOL_GPL(set_cpus_allowed_ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006700
6701/*
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01006702 * Move (not current) task off this cpu, onto dest cpu. We're doing
Linus Torvalds1da177e2005-04-16 15:20:36 -07006703 * this because either it can't run here any more (set_cpus_allowed()
6704 * away from this CPU, or CPU going down), or because we're
6705 * attempting to rebalance this task on exec (sched_exec).
6706 *
6707 * So we race with normal scheduler movements, but that's OK, as long
6708 * as the task is no longer on this CPU.
Kirill Korotaevefc30812006-06-27 02:54:32 -07006709 *
6710 * Returns non-zero if task was successfully migrated.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006711 */
Kirill Korotaevefc30812006-06-27 02:54:32 -07006712static int __migrate_task(struct task_struct *p, int src_cpu, int dest_cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006713{
Ingo Molnar70b97a72006-07-03 00:25:42 -07006714 struct rq *rq_dest, *rq_src;
Ingo Molnardd41f592007-07-09 18:51:59 +02006715 int ret = 0, on_rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006716
Max Krasnyanskye761b772008-07-15 04:43:49 -07006717 if (unlikely(!cpu_active(dest_cpu)))
Kirill Korotaevefc30812006-06-27 02:54:32 -07006718 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006719
6720 rq_src = cpu_rq(src_cpu);
6721 rq_dest = cpu_rq(dest_cpu);
6722
6723 double_rq_lock(rq_src, rq_dest);
6724 /* Already moved. */
6725 if (task_cpu(p) != src_cpu)
Linus Torvaldsb1e38732008-07-10 11:25:03 -07006726 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006727 /* Affinity changed (again). */
Rusty Russell96f874e2008-11-25 02:35:14 +10306728 if (!cpumask_test_cpu(dest_cpu, &p->cpus_allowed))
Linus Torvaldsb1e38732008-07-10 11:25:03 -07006729 goto fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006730
Ingo Molnardd41f592007-07-09 18:51:59 +02006731 on_rq = p->se.on_rq;
Ingo Molnar6e82a3b2007-08-09 11:16:51 +02006732 if (on_rq)
Ingo Molnar2e1cb742007-08-09 11:16:49 +02006733 deactivate_task(rq_src, p, 0);
Ingo Molnar6e82a3b2007-08-09 11:16:51 +02006734
Linus Torvalds1da177e2005-04-16 15:20:36 -07006735 set_task_cpu(p, dest_cpu);
Ingo Molnardd41f592007-07-09 18:51:59 +02006736 if (on_rq) {
6737 activate_task(rq_dest, p, 0);
Peter Zijlstra15afe092008-09-20 23:38:02 +02006738 check_preempt_curr(rq_dest, p, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006739 }
Linus Torvaldsb1e38732008-07-10 11:25:03 -07006740done:
Kirill Korotaevefc30812006-06-27 02:54:32 -07006741 ret = 1;
Linus Torvaldsb1e38732008-07-10 11:25:03 -07006742fail:
Linus Torvalds1da177e2005-04-16 15:20:36 -07006743 double_rq_unlock(rq_src, rq_dest);
Kirill Korotaevefc30812006-06-27 02:54:32 -07006744 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006745}
6746
6747/*
6748 * migration_thread - this is a highprio system thread that performs
6749 * thread migration by bumping thread off CPU then 'pushing' onto
6750 * another runqueue.
6751 */
Ingo Molnar95cdf3b2005-09-10 00:26:11 -07006752static int migration_thread(void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006753{
Linus Torvalds1da177e2005-04-16 15:20:36 -07006754 int cpu = (long)data;
Ingo Molnar70b97a72006-07-03 00:25:42 -07006755 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006756
6757 rq = cpu_rq(cpu);
6758 BUG_ON(rq->migration_thread != current);
6759
6760 set_current_state(TASK_INTERRUPTIBLE);
6761 while (!kthread_should_stop()) {
Ingo Molnar70b97a72006-07-03 00:25:42 -07006762 struct migration_req *req;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006763 struct list_head *head;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006764
Linus Torvalds1da177e2005-04-16 15:20:36 -07006765 spin_lock_irq(&rq->lock);
6766
6767 if (cpu_is_offline(cpu)) {
6768 spin_unlock_irq(&rq->lock);
6769 goto wait_to_die;
6770 }
6771
6772 if (rq->active_balance) {
6773 active_load_balance(rq, cpu);
6774 rq->active_balance = 0;
6775 }
6776
6777 head = &rq->migration_queue;
6778
6779 if (list_empty(head)) {
6780 spin_unlock_irq(&rq->lock);
6781 schedule();
6782 set_current_state(TASK_INTERRUPTIBLE);
6783 continue;
6784 }
Ingo Molnar70b97a72006-07-03 00:25:42 -07006785 req = list_entry(head->next, struct migration_req, list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006786 list_del_init(head->next);
6787
Nick Piggin674311d2005-06-25 14:57:27 -07006788 spin_unlock(&rq->lock);
6789 __migrate_task(req->task, cpu, req->dest_cpu);
6790 local_irq_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006791
6792 complete(&req->done);
6793 }
6794 __set_current_state(TASK_RUNNING);
6795 return 0;
6796
6797wait_to_die:
6798 /* Wait for kthread_stop */
6799 set_current_state(TASK_INTERRUPTIBLE);
6800 while (!kthread_should_stop()) {
6801 schedule();
6802 set_current_state(TASK_INTERRUPTIBLE);
6803 }
6804 __set_current_state(TASK_RUNNING);
6805 return 0;
6806}
6807
6808#ifdef CONFIG_HOTPLUG_CPU
Oleg Nesterovf7b4cdd2007-10-16 23:30:56 -07006809
6810static int __migrate_task_irq(struct task_struct *p, int src_cpu, int dest_cpu)
6811{
6812 int ret;
6813
6814 local_irq_disable();
6815 ret = __migrate_task(p, src_cpu, dest_cpu);
6816 local_irq_enable();
6817 return ret;
6818}
6819
Kirill Korotaev054b9102006-12-10 02:20:11 -08006820/*
Robert P. J. Day3a4fa0a2007-10-19 23:10:43 +02006821 * Figure out where task on dead CPU should go, use force if necessary.
Kirill Korotaev054b9102006-12-10 02:20:11 -08006822 */
Ingo Molnar48f24c42006-07-03 00:25:40 -07006823static void move_task_off_dead_cpu(int dead_cpu, struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006824{
Ingo Molnar70b97a72006-07-03 00:25:42 -07006825 int dest_cpu;
Mike Travis6ca09df2008-12-31 18:08:45 -08006826 const struct cpumask *nodemask = cpumask_of_node(cpu_to_node(dead_cpu));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006827
Rusty Russelle76bd8d2008-11-25 02:35:11 +10306828again:
6829 /* Look for allowed, online CPU in same node. */
6830 for_each_cpu_and(dest_cpu, nodemask, cpu_online_mask)
6831 if (cpumask_test_cpu(dest_cpu, &p->cpus_allowed))
6832 goto move;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006833
Rusty Russelle76bd8d2008-11-25 02:35:11 +10306834 /* Any allowed, online CPU? */
6835 dest_cpu = cpumask_any_and(&p->cpus_allowed, cpu_online_mask);
6836 if (dest_cpu < nr_cpu_ids)
6837 goto move;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006838
Rusty Russelle76bd8d2008-11-25 02:35:11 +10306839 /* No more Mr. Nice Guy. */
6840 if (dest_cpu >= nr_cpu_ids) {
Rusty Russelle76bd8d2008-11-25 02:35:11 +10306841 cpuset_cpus_allowed_locked(p, &p->cpus_allowed);
6842 dest_cpu = cpumask_any_and(cpu_online_mask, &p->cpus_allowed);
Mike Travisf9a86fc2008-04-04 18:11:07 -07006843
Rusty Russelle76bd8d2008-11-25 02:35:11 +10306844 /*
6845 * Don't tell them about moving exiting tasks or
6846 * kernel threads (both mm NULL), since they never
6847 * leave kernel.
6848 */
6849 if (p->mm && printk_ratelimit()) {
6850 printk(KERN_INFO "process %d (%s) no "
6851 "longer affine to cpu%d\n",
6852 task_pid_nr(p), p->comm, dead_cpu);
Andi Kleen3a5c3592007-10-15 17:00:14 +02006853 }
Rusty Russelle76bd8d2008-11-25 02:35:11 +10306854 }
6855
6856move:
6857 /* It can have affinity changed while we were choosing. */
6858 if (unlikely(!__migrate_task_irq(p, dead_cpu, dest_cpu)))
6859 goto again;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006860}
6861
6862/*
6863 * While a dead CPU has no uninterruptible tasks queued at this point,
6864 * it might still have a nonzero ->nr_uninterruptible counter, because
6865 * for performance reasons the counter is not stricly tracking tasks to
6866 * their home CPUs. So we just add the counter to another CPU's counter,
6867 * to keep the global sum constant after CPU-down:
6868 */
Ingo Molnar70b97a72006-07-03 00:25:42 -07006869static void migrate_nr_uninterruptible(struct rq *rq_src)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006870{
Rusty Russell1e5ce4f2008-11-25 02:35:03 +10306871 struct rq *rq_dest = cpu_rq(cpumask_any(cpu_online_mask));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006872 unsigned long flags;
6873
6874 local_irq_save(flags);
6875 double_rq_lock(rq_src, rq_dest);
6876 rq_dest->nr_uninterruptible += rq_src->nr_uninterruptible;
6877 rq_src->nr_uninterruptible = 0;
6878 double_rq_unlock(rq_src, rq_dest);
6879 local_irq_restore(flags);
6880}
6881
6882/* Run through task list and migrate tasks from the dead cpu. */
6883static void migrate_live_tasks(int src_cpu)
6884{
Ingo Molnar48f24c42006-07-03 00:25:40 -07006885 struct task_struct *p, *t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006886
Oleg Nesterovf7b4cdd2007-10-16 23:30:56 -07006887 read_lock(&tasklist_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006888
Ingo Molnar48f24c42006-07-03 00:25:40 -07006889 do_each_thread(t, p) {
6890 if (p == current)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006891 continue;
6892
Ingo Molnar48f24c42006-07-03 00:25:40 -07006893 if (task_cpu(p) == src_cpu)
6894 move_task_off_dead_cpu(src_cpu, p);
6895 } while_each_thread(t, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006896
Oleg Nesterovf7b4cdd2007-10-16 23:30:56 -07006897 read_unlock(&tasklist_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006898}
6899
Ingo Molnardd41f592007-07-09 18:51:59 +02006900/*
6901 * Schedules idle task to be the next runnable task on current CPU.
Dmitry Adamushko94bc9a72007-11-15 20:57:40 +01006902 * It does so by boosting its priority to highest possible.
6903 * Used by CPU offline code.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006904 */
6905void sched_idle_next(void)
6906{
Ingo Molnar48f24c42006-07-03 00:25:40 -07006907 int this_cpu = smp_processor_id();
Ingo Molnar70b97a72006-07-03 00:25:42 -07006908 struct rq *rq = cpu_rq(this_cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006909 struct task_struct *p = rq->idle;
6910 unsigned long flags;
6911
6912 /* cpu has to be offline */
Ingo Molnar48f24c42006-07-03 00:25:40 -07006913 BUG_ON(cpu_online(this_cpu));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006914
Ingo Molnar48f24c42006-07-03 00:25:40 -07006915 /*
6916 * Strictly not necessary since rest of the CPUs are stopped by now
6917 * and interrupts disabled on the current cpu.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006918 */
6919 spin_lock_irqsave(&rq->lock, flags);
6920
Ingo Molnardd41f592007-07-09 18:51:59 +02006921 __setscheduler(rq, p, SCHED_FIFO, MAX_RT_PRIO-1);
Ingo Molnar48f24c42006-07-03 00:25:40 -07006922
Dmitry Adamushko94bc9a72007-11-15 20:57:40 +01006923 update_rq_clock(rq);
6924 activate_task(rq, p, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006925
6926 spin_unlock_irqrestore(&rq->lock, flags);
6927}
6928
Ingo Molnar48f24c42006-07-03 00:25:40 -07006929/*
6930 * Ensures that the idle task is using init_mm right before its cpu goes
Linus Torvalds1da177e2005-04-16 15:20:36 -07006931 * offline.
6932 */
6933void idle_task_exit(void)
6934{
6935 struct mm_struct *mm = current->active_mm;
6936
6937 BUG_ON(cpu_online(smp_processor_id()));
6938
6939 if (mm != &init_mm)
6940 switch_mm(mm, &init_mm, current);
6941 mmdrop(mm);
6942}
6943
Kirill Korotaev054b9102006-12-10 02:20:11 -08006944/* called under rq->lock with disabled interrupts */
Ingo Molnar36c8b582006-07-03 00:25:41 -07006945static void migrate_dead(unsigned int dead_cpu, struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006946{
Ingo Molnar70b97a72006-07-03 00:25:42 -07006947 struct rq *rq = cpu_rq(dead_cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006948
6949 /* Must be exiting, otherwise would be on tasklist. */
Eugene Teo270f7222007-10-18 23:40:38 -07006950 BUG_ON(!p->exit_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006951
6952 /* Cannot have done final schedule yet: would have vanished. */
Oleg Nesterovc394cc92006-09-29 02:01:11 -07006953 BUG_ON(p->state == TASK_DEAD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006954
Ingo Molnar48f24c42006-07-03 00:25:40 -07006955 get_task_struct(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006956
6957 /*
6958 * Drop lock around migration; if someone else moves it,
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01006959 * that's OK. No task can be added to this CPU, so iteration is
Linus Torvalds1da177e2005-04-16 15:20:36 -07006960 * fine.
6961 */
Oleg Nesterovf7b4cdd2007-10-16 23:30:56 -07006962 spin_unlock_irq(&rq->lock);
Ingo Molnar48f24c42006-07-03 00:25:40 -07006963 move_task_off_dead_cpu(dead_cpu, p);
Oleg Nesterovf7b4cdd2007-10-16 23:30:56 -07006964 spin_lock_irq(&rq->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006965
Ingo Molnar48f24c42006-07-03 00:25:40 -07006966 put_task_struct(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006967}
6968
6969/* release_task() removes task from tasklist, so we won't find dead tasks. */
6970static void migrate_dead_tasks(unsigned int dead_cpu)
6971{
Ingo Molnar70b97a72006-07-03 00:25:42 -07006972 struct rq *rq = cpu_rq(dead_cpu);
Ingo Molnardd41f592007-07-09 18:51:59 +02006973 struct task_struct *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006974
Ingo Molnardd41f592007-07-09 18:51:59 +02006975 for ( ; ; ) {
6976 if (!rq->nr_running)
6977 break;
Ingo Molnara8e504d2007-08-09 11:16:47 +02006978 update_rq_clock(rq);
Wang Chenb67802e2009-03-02 13:55:26 +08006979 next = pick_next_task(rq);
Ingo Molnardd41f592007-07-09 18:51:59 +02006980 if (!next)
6981 break;
Dmitry Adamushko79c53792008-06-29 00:16:56 +02006982 next->sched_class->put_prev_task(rq, next);
Ingo Molnardd41f592007-07-09 18:51:59 +02006983 migrate_dead(dead_cpu, next);
Nick Piggine692ab52007-07-26 13:40:43 +02006984
Linus Torvalds1da177e2005-04-16 15:20:36 -07006985 }
6986}
6987#endif /* CONFIG_HOTPLUG_CPU */
6988
Nick Piggine692ab52007-07-26 13:40:43 +02006989#if defined(CONFIG_SCHED_DEBUG) && defined(CONFIG_SYSCTL)
6990
6991static struct ctl_table sd_ctl_dir[] = {
Alexey Dobriyane0361852007-08-09 11:16:46 +02006992 {
6993 .procname = "sched_domain",
Eric W. Biedermanc57baf12007-08-23 15:18:02 +02006994 .mode = 0555,
Alexey Dobriyane0361852007-08-09 11:16:46 +02006995 },
Ingo Molnar38605ca2007-10-29 21:18:11 +01006996 {0, },
Nick Piggine692ab52007-07-26 13:40:43 +02006997};
6998
6999static struct ctl_table sd_ctl_root[] = {
Alexey Dobriyane0361852007-08-09 11:16:46 +02007000 {
Eric W. Biedermanc57baf12007-08-23 15:18:02 +02007001 .ctl_name = CTL_KERN,
Alexey Dobriyane0361852007-08-09 11:16:46 +02007002 .procname = "kernel",
Eric W. Biedermanc57baf12007-08-23 15:18:02 +02007003 .mode = 0555,
Alexey Dobriyane0361852007-08-09 11:16:46 +02007004 .child = sd_ctl_dir,
7005 },
Ingo Molnar38605ca2007-10-29 21:18:11 +01007006 {0, },
Nick Piggine692ab52007-07-26 13:40:43 +02007007};
7008
7009static struct ctl_table *sd_alloc_ctl_entry(int n)
7010{
7011 struct ctl_table *entry =
Milton Miller5cf9f062007-10-15 17:00:19 +02007012 kcalloc(n, sizeof(struct ctl_table), GFP_KERNEL);
Nick Piggine692ab52007-07-26 13:40:43 +02007013
Nick Piggine692ab52007-07-26 13:40:43 +02007014 return entry;
7015}
7016
Milton Miller6382bc92007-10-15 17:00:19 +02007017static void sd_free_ctl_entry(struct ctl_table **tablep)
7018{
Milton Millercd7900762007-10-17 16:55:11 +02007019 struct ctl_table *entry;
Milton Miller6382bc92007-10-15 17:00:19 +02007020
Milton Millercd7900762007-10-17 16:55:11 +02007021 /*
7022 * In the intermediate directories, both the child directory and
7023 * procname are dynamically allocated and could fail but the mode
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01007024 * will always be set. In the lowest directory the names are
Milton Millercd7900762007-10-17 16:55:11 +02007025 * static strings and all have proc handlers.
7026 */
7027 for (entry = *tablep; entry->mode; entry++) {
Milton Miller6382bc92007-10-15 17:00:19 +02007028 if (entry->child)
7029 sd_free_ctl_entry(&entry->child);
Milton Millercd7900762007-10-17 16:55:11 +02007030 if (entry->proc_handler == NULL)
7031 kfree(entry->procname);
7032 }
Milton Miller6382bc92007-10-15 17:00:19 +02007033
7034 kfree(*tablep);
7035 *tablep = NULL;
7036}
7037
Nick Piggine692ab52007-07-26 13:40:43 +02007038static void
Alexey Dobriyane0361852007-08-09 11:16:46 +02007039set_table_entry(struct ctl_table *entry,
Nick Piggine692ab52007-07-26 13:40:43 +02007040 const char *procname, void *data, int maxlen,
7041 mode_t mode, proc_handler *proc_handler)
7042{
Nick Piggine692ab52007-07-26 13:40:43 +02007043 entry->procname = procname;
7044 entry->data = data;
7045 entry->maxlen = maxlen;
7046 entry->mode = mode;
7047 entry->proc_handler = proc_handler;
7048}
7049
7050static struct ctl_table *
7051sd_alloc_ctl_domain_table(struct sched_domain *sd)
7052{
Ingo Molnara5d8c342008-10-09 11:35:51 +02007053 struct ctl_table *table = sd_alloc_ctl_entry(13);
Nick Piggine692ab52007-07-26 13:40:43 +02007054
Milton Millerad1cdc12007-10-15 17:00:19 +02007055 if (table == NULL)
7056 return NULL;
7057
Alexey Dobriyane0361852007-08-09 11:16:46 +02007058 set_table_entry(&table[0], "min_interval", &sd->min_interval,
Nick Piggine692ab52007-07-26 13:40:43 +02007059 sizeof(long), 0644, proc_doulongvec_minmax);
Alexey Dobriyane0361852007-08-09 11:16:46 +02007060 set_table_entry(&table[1], "max_interval", &sd->max_interval,
Nick Piggine692ab52007-07-26 13:40:43 +02007061 sizeof(long), 0644, proc_doulongvec_minmax);
Alexey Dobriyane0361852007-08-09 11:16:46 +02007062 set_table_entry(&table[2], "busy_idx", &sd->busy_idx,
Nick Piggine692ab52007-07-26 13:40:43 +02007063 sizeof(int), 0644, proc_dointvec_minmax);
Alexey Dobriyane0361852007-08-09 11:16:46 +02007064 set_table_entry(&table[3], "idle_idx", &sd->idle_idx,
Nick Piggine692ab52007-07-26 13:40:43 +02007065 sizeof(int), 0644, proc_dointvec_minmax);
Alexey Dobriyane0361852007-08-09 11:16:46 +02007066 set_table_entry(&table[4], "newidle_idx", &sd->newidle_idx,
Nick Piggine692ab52007-07-26 13:40:43 +02007067 sizeof(int), 0644, proc_dointvec_minmax);
Alexey Dobriyane0361852007-08-09 11:16:46 +02007068 set_table_entry(&table[5], "wake_idx", &sd->wake_idx,
Nick Piggine692ab52007-07-26 13:40:43 +02007069 sizeof(int), 0644, proc_dointvec_minmax);
Alexey Dobriyane0361852007-08-09 11:16:46 +02007070 set_table_entry(&table[6], "forkexec_idx", &sd->forkexec_idx,
Nick Piggine692ab52007-07-26 13:40:43 +02007071 sizeof(int), 0644, proc_dointvec_minmax);
Alexey Dobriyane0361852007-08-09 11:16:46 +02007072 set_table_entry(&table[7], "busy_factor", &sd->busy_factor,
Nick Piggine692ab52007-07-26 13:40:43 +02007073 sizeof(int), 0644, proc_dointvec_minmax);
Alexey Dobriyane0361852007-08-09 11:16:46 +02007074 set_table_entry(&table[8], "imbalance_pct", &sd->imbalance_pct,
Nick Piggine692ab52007-07-26 13:40:43 +02007075 sizeof(int), 0644, proc_dointvec_minmax);
Zou Nan haiace8b3d2007-10-15 17:00:14 +02007076 set_table_entry(&table[9], "cache_nice_tries",
Nick Piggine692ab52007-07-26 13:40:43 +02007077 &sd->cache_nice_tries,
7078 sizeof(int), 0644, proc_dointvec_minmax);
Zou Nan haiace8b3d2007-10-15 17:00:14 +02007079 set_table_entry(&table[10], "flags", &sd->flags,
Nick Piggine692ab52007-07-26 13:40:43 +02007080 sizeof(int), 0644, proc_dointvec_minmax);
Ingo Molnara5d8c342008-10-09 11:35:51 +02007081 set_table_entry(&table[11], "name", sd->name,
7082 CORENAME_MAX_SIZE, 0444, proc_dostring);
7083 /* &table[12] is terminator */
Nick Piggine692ab52007-07-26 13:40:43 +02007084
7085 return table;
7086}
7087
Ingo Molnar9a4e7152007-11-28 15:52:56 +01007088static ctl_table *sd_alloc_ctl_cpu_table(int cpu)
Nick Piggine692ab52007-07-26 13:40:43 +02007089{
7090 struct ctl_table *entry, *table;
7091 struct sched_domain *sd;
7092 int domain_num = 0, i;
7093 char buf[32];
7094
7095 for_each_domain(cpu, sd)
7096 domain_num++;
7097 entry = table = sd_alloc_ctl_entry(domain_num + 1);
Milton Millerad1cdc12007-10-15 17:00:19 +02007098 if (table == NULL)
7099 return NULL;
Nick Piggine692ab52007-07-26 13:40:43 +02007100
7101 i = 0;
7102 for_each_domain(cpu, sd) {
7103 snprintf(buf, 32, "domain%d", i);
Nick Piggine692ab52007-07-26 13:40:43 +02007104 entry->procname = kstrdup(buf, GFP_KERNEL);
Eric W. Biedermanc57baf12007-08-23 15:18:02 +02007105 entry->mode = 0555;
Nick Piggine692ab52007-07-26 13:40:43 +02007106 entry->child = sd_alloc_ctl_domain_table(sd);
7107 entry++;
7108 i++;
7109 }
7110 return table;
7111}
7112
7113static struct ctl_table_header *sd_sysctl_header;
Milton Miller6382bc92007-10-15 17:00:19 +02007114static void register_sched_domain_sysctl(void)
Nick Piggine692ab52007-07-26 13:40:43 +02007115{
7116 int i, cpu_num = num_online_cpus();
7117 struct ctl_table *entry = sd_alloc_ctl_entry(cpu_num + 1);
7118 char buf[32];
7119
Milton Miller73785472007-10-24 18:23:48 +02007120 WARN_ON(sd_ctl_dir[0].child);
7121 sd_ctl_dir[0].child = entry;
7122
Milton Millerad1cdc12007-10-15 17:00:19 +02007123 if (entry == NULL)
7124 return;
7125
Milton Miller97b6ea72007-10-15 17:00:19 +02007126 for_each_online_cpu(i) {
Nick Piggine692ab52007-07-26 13:40:43 +02007127 snprintf(buf, 32, "cpu%d", i);
Nick Piggine692ab52007-07-26 13:40:43 +02007128 entry->procname = kstrdup(buf, GFP_KERNEL);
Eric W. Biedermanc57baf12007-08-23 15:18:02 +02007129 entry->mode = 0555;
Nick Piggine692ab52007-07-26 13:40:43 +02007130 entry->child = sd_alloc_ctl_cpu_table(i);
Milton Miller97b6ea72007-10-15 17:00:19 +02007131 entry++;
Nick Piggine692ab52007-07-26 13:40:43 +02007132 }
Milton Miller73785472007-10-24 18:23:48 +02007133
7134 WARN_ON(sd_sysctl_header);
Nick Piggine692ab52007-07-26 13:40:43 +02007135 sd_sysctl_header = register_sysctl_table(sd_ctl_root);
7136}
Milton Miller6382bc92007-10-15 17:00:19 +02007137
Milton Miller73785472007-10-24 18:23:48 +02007138/* may be called multiple times per register */
Milton Miller6382bc92007-10-15 17:00:19 +02007139static void unregister_sched_domain_sysctl(void)
7140{
Milton Miller73785472007-10-24 18:23:48 +02007141 if (sd_sysctl_header)
7142 unregister_sysctl_table(sd_sysctl_header);
Milton Miller6382bc92007-10-15 17:00:19 +02007143 sd_sysctl_header = NULL;
Milton Miller73785472007-10-24 18:23:48 +02007144 if (sd_ctl_dir[0].child)
7145 sd_free_ctl_entry(&sd_ctl_dir[0].child);
Milton Miller6382bc92007-10-15 17:00:19 +02007146}
Nick Piggine692ab52007-07-26 13:40:43 +02007147#else
Milton Miller6382bc92007-10-15 17:00:19 +02007148static void register_sched_domain_sysctl(void)
7149{
7150}
7151static void unregister_sched_domain_sysctl(void)
Nick Piggine692ab52007-07-26 13:40:43 +02007152{
7153}
7154#endif
7155
Gregory Haskins1f11eb62008-06-04 15:04:05 -04007156static void set_rq_online(struct rq *rq)
7157{
7158 if (!rq->online) {
7159 const struct sched_class *class;
7160
Rusty Russellc6c49272008-11-25 02:35:05 +10307161 cpumask_set_cpu(rq->cpu, rq->rd->online);
Gregory Haskins1f11eb62008-06-04 15:04:05 -04007162 rq->online = 1;
7163
7164 for_each_class(class) {
7165 if (class->rq_online)
7166 class->rq_online(rq);
7167 }
7168 }
7169}
7170
7171static void set_rq_offline(struct rq *rq)
7172{
7173 if (rq->online) {
7174 const struct sched_class *class;
7175
7176 for_each_class(class) {
7177 if (class->rq_offline)
7178 class->rq_offline(rq);
7179 }
7180
Rusty Russellc6c49272008-11-25 02:35:05 +10307181 cpumask_clear_cpu(rq->cpu, rq->rd->online);
Gregory Haskins1f11eb62008-06-04 15:04:05 -04007182 rq->online = 0;
7183 }
7184}
7185
Linus Torvalds1da177e2005-04-16 15:20:36 -07007186/*
7187 * migration_call - callback that gets triggered when a CPU is added.
7188 * Here we can start up the necessary migration thread for the new CPU.
7189 */
Ingo Molnar48f24c42006-07-03 00:25:40 -07007190static int __cpuinit
7191migration_call(struct notifier_block *nfb, unsigned long action, void *hcpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007192{
Linus Torvalds1da177e2005-04-16 15:20:36 -07007193 struct task_struct *p;
Ingo Molnar48f24c42006-07-03 00:25:40 -07007194 int cpu = (long)hcpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007195 unsigned long flags;
Ingo Molnar70b97a72006-07-03 00:25:42 -07007196 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007197
7198 switch (action) {
Gautham R Shenoy5be93612007-05-09 02:34:04 -07007199
Linus Torvalds1da177e2005-04-16 15:20:36 -07007200 case CPU_UP_PREPARE:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07007201 case CPU_UP_PREPARE_FROZEN:
Ingo Molnardd41f592007-07-09 18:51:59 +02007202 p = kthread_create(migration_thread, hcpu, "migration/%d", cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007203 if (IS_ERR(p))
7204 return NOTIFY_BAD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007205 kthread_bind(p, cpu);
7206 /* Must be high prio: stop_machine expects to yield to it. */
7207 rq = task_rq_lock(p, &flags);
Ingo Molnardd41f592007-07-09 18:51:59 +02007208 __setscheduler(rq, p, SCHED_FIFO, MAX_RT_PRIO-1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007209 task_rq_unlock(rq, &flags);
7210 cpu_rq(cpu)->migration_thread = p;
7211 break;
Ingo Molnar48f24c42006-07-03 00:25:40 -07007212
Linus Torvalds1da177e2005-04-16 15:20:36 -07007213 case CPU_ONLINE:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07007214 case CPU_ONLINE_FROZEN:
Robert P. J. Day3a4fa0a2007-10-19 23:10:43 +02007215 /* Strictly unnecessary, as first user will wake it. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007216 wake_up_process(cpu_rq(cpu)->migration_thread);
Gregory Haskins1f94ef52008-03-10 16:52:41 -04007217
7218 /* Update our root-domain */
7219 rq = cpu_rq(cpu);
7220 spin_lock_irqsave(&rq->lock, flags);
7221 if (rq->rd) {
Rusty Russellc6c49272008-11-25 02:35:05 +10307222 BUG_ON(!cpumask_test_cpu(cpu, rq->rd->span));
Gregory Haskins1f11eb62008-06-04 15:04:05 -04007223
7224 set_rq_online(rq);
Gregory Haskins1f94ef52008-03-10 16:52:41 -04007225 }
7226 spin_unlock_irqrestore(&rq->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007227 break;
Ingo Molnar48f24c42006-07-03 00:25:40 -07007228
Linus Torvalds1da177e2005-04-16 15:20:36 -07007229#ifdef CONFIG_HOTPLUG_CPU
7230 case CPU_UP_CANCELED:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07007231 case CPU_UP_CANCELED_FROZEN:
Heiko Carstensfc75cdf2006-06-25 05:49:10 -07007232 if (!cpu_rq(cpu)->migration_thread)
7233 break;
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01007234 /* Unbind it from offline cpu so it can run. Fall thru. */
Heiko Carstensa4c4af72005-11-07 00:58:38 -08007235 kthread_bind(cpu_rq(cpu)->migration_thread,
Rusty Russell1e5ce4f2008-11-25 02:35:03 +10307236 cpumask_any(cpu_online_mask));
Linus Torvalds1da177e2005-04-16 15:20:36 -07007237 kthread_stop(cpu_rq(cpu)->migration_thread);
7238 cpu_rq(cpu)->migration_thread = NULL;
7239 break;
Ingo Molnar48f24c42006-07-03 00:25:40 -07007240
Linus Torvalds1da177e2005-04-16 15:20:36 -07007241 case CPU_DEAD:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07007242 case CPU_DEAD_FROZEN:
Cliff Wickman470fd6462007-10-18 23:40:46 -07007243 cpuset_lock(); /* around calls to cpuset_cpus_allowed_lock() */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007244 migrate_live_tasks(cpu);
7245 rq = cpu_rq(cpu);
7246 kthread_stop(rq->migration_thread);
7247 rq->migration_thread = NULL;
7248 /* Idle task back to normal (off runqueue, low prio) */
Oleg Nesterovd2da2722007-10-16 23:30:56 -07007249 spin_lock_irq(&rq->lock);
Ingo Molnara8e504d2007-08-09 11:16:47 +02007250 update_rq_clock(rq);
Ingo Molnar2e1cb742007-08-09 11:16:49 +02007251 deactivate_task(rq, rq->idle, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007252 rq->idle->static_prio = MAX_PRIO;
Ingo Molnardd41f592007-07-09 18:51:59 +02007253 __setscheduler(rq, rq->idle, SCHED_NORMAL, 0);
7254 rq->idle->sched_class = &idle_sched_class;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007255 migrate_dead_tasks(cpu);
Oleg Nesterovd2da2722007-10-16 23:30:56 -07007256 spin_unlock_irq(&rq->lock);
Cliff Wickman470fd6462007-10-18 23:40:46 -07007257 cpuset_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07007258 migrate_nr_uninterruptible(rq);
7259 BUG_ON(rq->nr_running != 0);
7260
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01007261 /*
7262 * No need to migrate the tasks: it was best-effort if
7263 * they didn't take sched_hotcpu_mutex. Just wake up
7264 * the requestors.
7265 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007266 spin_lock_irq(&rq->lock);
7267 while (!list_empty(&rq->migration_queue)) {
Ingo Molnar70b97a72006-07-03 00:25:42 -07007268 struct migration_req *req;
7269
Linus Torvalds1da177e2005-04-16 15:20:36 -07007270 req = list_entry(rq->migration_queue.next,
Ingo Molnar70b97a72006-07-03 00:25:42 -07007271 struct migration_req, list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007272 list_del_init(&req->list);
Brian King9a2bd242008-12-09 08:47:00 -06007273 spin_unlock_irq(&rq->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007274 complete(&req->done);
Brian King9a2bd242008-12-09 08:47:00 -06007275 spin_lock_irq(&rq->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007276 }
7277 spin_unlock_irq(&rq->lock);
7278 break;
Gregory Haskins57d885f2008-01-25 21:08:18 +01007279
Gregory Haskins08f503b2008-03-10 17:59:11 -04007280 case CPU_DYING:
7281 case CPU_DYING_FROZEN:
Gregory Haskins57d885f2008-01-25 21:08:18 +01007282 /* Update our root-domain */
7283 rq = cpu_rq(cpu);
7284 spin_lock_irqsave(&rq->lock, flags);
7285 if (rq->rd) {
Rusty Russellc6c49272008-11-25 02:35:05 +10307286 BUG_ON(!cpumask_test_cpu(cpu, rq->rd->span));
Gregory Haskins1f11eb62008-06-04 15:04:05 -04007287 set_rq_offline(rq);
Gregory Haskins57d885f2008-01-25 21:08:18 +01007288 }
7289 spin_unlock_irqrestore(&rq->lock, flags);
7290 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007291#endif
7292 }
7293 return NOTIFY_OK;
7294}
7295
7296/* Register at highest priority so that task migration (migrate_all_tasks)
7297 * happens before everything else.
7298 */
Chandra Seetharaman26c21432006-06-27 02:54:10 -07007299static struct notifier_block __cpuinitdata migration_notifier = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007300 .notifier_call = migration_call,
7301 .priority = 10
7302};
7303
Eduard - Gabriel Munteanu7babe8d2008-07-25 19:45:11 -07007304static int __init migration_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007305{
7306 void *cpu = (void *)(long)smp_processor_id();
Akinobu Mita07dccf32006-09-29 02:00:22 -07007307 int err;
Ingo Molnar48f24c42006-07-03 00:25:40 -07007308
7309 /* Start one for the boot CPU: */
Akinobu Mita07dccf32006-09-29 02:00:22 -07007310 err = migration_call(&migration_notifier, CPU_UP_PREPARE, cpu);
7311 BUG_ON(err == NOTIFY_BAD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007312 migration_call(&migration_notifier, CPU_ONLINE, cpu);
7313 register_cpu_notifier(&migration_notifier);
Eduard - Gabriel Munteanu7babe8d2008-07-25 19:45:11 -07007314
7315 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007316}
Eduard - Gabriel Munteanu7babe8d2008-07-25 19:45:11 -07007317early_initcall(migration_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007318#endif
7319
7320#ifdef CONFIG_SMP
Christoph Lameter476f3532007-05-06 14:48:58 -07007321
Ingo Molnar3e9830d2007-10-15 17:00:13 +02007322#ifdef CONFIG_SCHED_DEBUG
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02007323
Mike Travis7c16ec52008-04-04 18:11:11 -07007324static int sched_domain_debug_one(struct sched_domain *sd, int cpu, int level,
Rusty Russell96f874e2008-11-25 02:35:14 +10307325 struct cpumask *groupmask)
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02007326{
7327 struct sched_group *group = sd->groups;
Mike Travis434d53b2008-04-04 18:11:04 -07007328 char str[256];
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02007329
Rusty Russell968ea6d2008-12-13 21:55:51 +10307330 cpulist_scnprintf(str, sizeof(str), sched_domain_span(sd));
Rusty Russell96f874e2008-11-25 02:35:14 +10307331 cpumask_clear(groupmask);
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02007332
7333 printk(KERN_DEBUG "%*s domain %d: ", level, "", level);
7334
7335 if (!(sd->flags & SD_LOAD_BALANCE)) {
7336 printk("does not load-balance\n");
7337 if (sd->parent)
7338 printk(KERN_ERR "ERROR: !SD_LOAD_BALANCE domain"
7339 " has parent");
7340 return -1;
7341 }
7342
Li Zefaneefd7962008-11-04 16:15:37 +08007343 printk(KERN_CONT "span %s level %s\n", str, sd->name);
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02007344
Rusty Russell758b2cd2008-11-25 02:35:04 +10307345 if (!cpumask_test_cpu(cpu, sched_domain_span(sd))) {
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02007346 printk(KERN_ERR "ERROR: domain->span does not contain "
7347 "CPU%d\n", cpu);
7348 }
Rusty Russell758b2cd2008-11-25 02:35:04 +10307349 if (!cpumask_test_cpu(cpu, sched_group_cpus(group))) {
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02007350 printk(KERN_ERR "ERROR: domain->groups does not contain"
7351 " CPU%d\n", cpu);
7352 }
7353
7354 printk(KERN_DEBUG "%*s groups:", level + 1, "");
7355 do {
7356 if (!group) {
7357 printk("\n");
7358 printk(KERN_ERR "ERROR: group is NULL\n");
7359 break;
7360 }
7361
7362 if (!group->__cpu_power) {
7363 printk(KERN_CONT "\n");
7364 printk(KERN_ERR "ERROR: domain->cpu_power not "
7365 "set\n");
7366 break;
7367 }
7368
Rusty Russell758b2cd2008-11-25 02:35:04 +10307369 if (!cpumask_weight(sched_group_cpus(group))) {
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02007370 printk(KERN_CONT "\n");
7371 printk(KERN_ERR "ERROR: empty group\n");
7372 break;
7373 }
7374
Rusty Russell758b2cd2008-11-25 02:35:04 +10307375 if (cpumask_intersects(groupmask, sched_group_cpus(group))) {
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02007376 printk(KERN_CONT "\n");
7377 printk(KERN_ERR "ERROR: repeated CPUs\n");
7378 break;
7379 }
7380
Rusty Russell758b2cd2008-11-25 02:35:04 +10307381 cpumask_or(groupmask, groupmask, sched_group_cpus(group));
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02007382
Rusty Russell968ea6d2008-12-13 21:55:51 +10307383 cpulist_scnprintf(str, sizeof(str), sched_group_cpus(group));
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02007384 printk(KERN_CONT " %s", str);
7385
7386 group = group->next;
7387 } while (group != sd->groups);
7388 printk(KERN_CONT "\n");
7389
Rusty Russell758b2cd2008-11-25 02:35:04 +10307390 if (!cpumask_equal(sched_domain_span(sd), groupmask))
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02007391 printk(KERN_ERR "ERROR: groups don't span domain->span\n");
7392
Rusty Russell758b2cd2008-11-25 02:35:04 +10307393 if (sd->parent &&
7394 !cpumask_subset(groupmask, sched_domain_span(sd->parent)))
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02007395 printk(KERN_ERR "ERROR: parent span is not a superset "
7396 "of domain->span\n");
7397 return 0;
7398}
7399
Linus Torvalds1da177e2005-04-16 15:20:36 -07007400static void sched_domain_debug(struct sched_domain *sd, int cpu)
7401{
Rusty Russelld5dd3db2008-11-25 02:35:12 +10307402 cpumask_var_t groupmask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007403 int level = 0;
7404
Nick Piggin41c7ce92005-06-25 14:57:24 -07007405 if (!sd) {
7406 printk(KERN_DEBUG "CPU%d attaching NULL sched-domain.\n", cpu);
7407 return;
7408 }
7409
Linus Torvalds1da177e2005-04-16 15:20:36 -07007410 printk(KERN_DEBUG "CPU%d attaching sched-domain:\n", cpu);
7411
Rusty Russelld5dd3db2008-11-25 02:35:12 +10307412 if (!alloc_cpumask_var(&groupmask, GFP_KERNEL)) {
Mike Travis7c16ec52008-04-04 18:11:11 -07007413 printk(KERN_DEBUG "Cannot load-balance (out of memory)\n");
7414 return;
7415 }
7416
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02007417 for (;;) {
Mike Travis7c16ec52008-04-04 18:11:11 -07007418 if (sched_domain_debug_one(sd, cpu, level, groupmask))
Linus Torvalds1da177e2005-04-16 15:20:36 -07007419 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007420 level++;
7421 sd = sd->parent;
Miguel Ojeda Sandonis33859f72006-12-10 02:20:38 -08007422 if (!sd)
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02007423 break;
7424 }
Rusty Russelld5dd3db2008-11-25 02:35:12 +10307425 free_cpumask_var(groupmask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007426}
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +02007427#else /* !CONFIG_SCHED_DEBUG */
Ingo Molnar48f24c42006-07-03 00:25:40 -07007428# define sched_domain_debug(sd, cpu) do { } while (0)
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +02007429#endif /* CONFIG_SCHED_DEBUG */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007430
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07007431static int sd_degenerate(struct sched_domain *sd)
Suresh Siddha245af2c2005-06-25 14:57:25 -07007432{
Rusty Russell758b2cd2008-11-25 02:35:04 +10307433 if (cpumask_weight(sched_domain_span(sd)) == 1)
Suresh Siddha245af2c2005-06-25 14:57:25 -07007434 return 1;
7435
7436 /* Following flags need at least 2 groups */
7437 if (sd->flags & (SD_LOAD_BALANCE |
7438 SD_BALANCE_NEWIDLE |
7439 SD_BALANCE_FORK |
Siddha, Suresh B89c47102006-10-03 01:14:09 -07007440 SD_BALANCE_EXEC |
7441 SD_SHARE_CPUPOWER |
7442 SD_SHARE_PKG_RESOURCES)) {
Suresh Siddha245af2c2005-06-25 14:57:25 -07007443 if (sd->groups != sd->groups->next)
7444 return 0;
7445 }
7446
7447 /* Following flags don't use groups */
7448 if (sd->flags & (SD_WAKE_IDLE |
7449 SD_WAKE_AFFINE |
7450 SD_WAKE_BALANCE))
7451 return 0;
7452
7453 return 1;
7454}
7455
Ingo Molnar48f24c42006-07-03 00:25:40 -07007456static int
7457sd_parent_degenerate(struct sched_domain *sd, struct sched_domain *parent)
Suresh Siddha245af2c2005-06-25 14:57:25 -07007458{
7459 unsigned long cflags = sd->flags, pflags = parent->flags;
7460
7461 if (sd_degenerate(parent))
7462 return 1;
7463
Rusty Russell758b2cd2008-11-25 02:35:04 +10307464 if (!cpumask_equal(sched_domain_span(sd), sched_domain_span(parent)))
Suresh Siddha245af2c2005-06-25 14:57:25 -07007465 return 0;
7466
7467 /* Does parent contain flags not in child? */
7468 /* WAKE_BALANCE is a subset of WAKE_AFFINE */
7469 if (cflags & SD_WAKE_AFFINE)
7470 pflags &= ~SD_WAKE_BALANCE;
7471 /* Flags needing groups don't count if only 1 group in parent */
7472 if (parent->groups == parent->groups->next) {
7473 pflags &= ~(SD_LOAD_BALANCE |
7474 SD_BALANCE_NEWIDLE |
7475 SD_BALANCE_FORK |
Siddha, Suresh B89c47102006-10-03 01:14:09 -07007476 SD_BALANCE_EXEC |
7477 SD_SHARE_CPUPOWER |
7478 SD_SHARE_PKG_RESOURCES);
Ken Chen54364992008-12-07 18:47:37 -08007479 if (nr_node_ids == 1)
7480 pflags &= ~SD_SERIALIZE;
Suresh Siddha245af2c2005-06-25 14:57:25 -07007481 }
7482 if (~cflags & pflags)
7483 return 0;
7484
7485 return 1;
7486}
7487
Rusty Russellc6c49272008-11-25 02:35:05 +10307488static void free_rootdomain(struct root_domain *rd)
7489{
Rusty Russell68e74562008-11-25 02:35:13 +10307490 cpupri_cleanup(&rd->cpupri);
7491
Rusty Russellc6c49272008-11-25 02:35:05 +10307492 free_cpumask_var(rd->rto_mask);
7493 free_cpumask_var(rd->online);
7494 free_cpumask_var(rd->span);
7495 kfree(rd);
7496}
7497
Gregory Haskins57d885f2008-01-25 21:08:18 +01007498static void rq_attach_root(struct rq *rq, struct root_domain *rd)
7499{
Ingo Molnara0490fa2009-02-12 11:35:40 +01007500 struct root_domain *old_rd = NULL;
Gregory Haskins57d885f2008-01-25 21:08:18 +01007501 unsigned long flags;
Gregory Haskins57d885f2008-01-25 21:08:18 +01007502
7503 spin_lock_irqsave(&rq->lock, flags);
7504
7505 if (rq->rd) {
Ingo Molnara0490fa2009-02-12 11:35:40 +01007506 old_rd = rq->rd;
Gregory Haskins57d885f2008-01-25 21:08:18 +01007507
Rusty Russellc6c49272008-11-25 02:35:05 +10307508 if (cpumask_test_cpu(rq->cpu, old_rd->online))
Gregory Haskins1f11eb62008-06-04 15:04:05 -04007509 set_rq_offline(rq);
Gregory Haskins57d885f2008-01-25 21:08:18 +01007510
Rusty Russellc6c49272008-11-25 02:35:05 +10307511 cpumask_clear_cpu(rq->cpu, old_rd->span);
Gregory Haskinsdc938522008-01-25 21:08:26 +01007512
Ingo Molnara0490fa2009-02-12 11:35:40 +01007513 /*
7514 * If we dont want to free the old_rt yet then
7515 * set old_rd to NULL to skip the freeing later
7516 * in this function:
7517 */
7518 if (!atomic_dec_and_test(&old_rd->refcount))
7519 old_rd = NULL;
Gregory Haskins57d885f2008-01-25 21:08:18 +01007520 }
7521
7522 atomic_inc(&rd->refcount);
7523 rq->rd = rd;
7524
Rusty Russellc6c49272008-11-25 02:35:05 +10307525 cpumask_set_cpu(rq->cpu, rd->span);
7526 if (cpumask_test_cpu(rq->cpu, cpu_online_mask))
Gregory Haskins1f11eb62008-06-04 15:04:05 -04007527 set_rq_online(rq);
Gregory Haskins57d885f2008-01-25 21:08:18 +01007528
7529 spin_unlock_irqrestore(&rq->lock, flags);
Ingo Molnara0490fa2009-02-12 11:35:40 +01007530
7531 if (old_rd)
7532 free_rootdomain(old_rd);
Gregory Haskins57d885f2008-01-25 21:08:18 +01007533}
7534
Li Zefandb2f59c2009-01-06 17:40:36 +08007535static int __init_refok init_rootdomain(struct root_domain *rd, bool bootmem)
Gregory Haskins57d885f2008-01-25 21:08:18 +01007536{
7537 memset(rd, 0, sizeof(*rd));
7538
Rusty Russellc6c49272008-11-25 02:35:05 +10307539 if (bootmem) {
7540 alloc_bootmem_cpumask_var(&def_root_domain.span);
7541 alloc_bootmem_cpumask_var(&def_root_domain.online);
7542 alloc_bootmem_cpumask_var(&def_root_domain.rto_mask);
Rusty Russell68e74562008-11-25 02:35:13 +10307543 cpupri_init(&rd->cpupri, true);
Rusty Russellc6c49272008-11-25 02:35:05 +10307544 return 0;
7545 }
Gregory Haskins6e0534f2008-05-12 21:21:01 +02007546
Rusty Russellc6c49272008-11-25 02:35:05 +10307547 if (!alloc_cpumask_var(&rd->span, GFP_KERNEL))
Li Zefan0c910d22009-01-06 17:39:06 +08007548 goto out;
Rusty Russellc6c49272008-11-25 02:35:05 +10307549 if (!alloc_cpumask_var(&rd->online, GFP_KERNEL))
7550 goto free_span;
7551 if (!alloc_cpumask_var(&rd->rto_mask, GFP_KERNEL))
7552 goto free_online;
Gregory Haskins6e0534f2008-05-12 21:21:01 +02007553
Rusty Russell68e74562008-11-25 02:35:13 +10307554 if (cpupri_init(&rd->cpupri, false) != 0)
7555 goto free_rto_mask;
Rusty Russellc6c49272008-11-25 02:35:05 +10307556 return 0;
7557
Rusty Russell68e74562008-11-25 02:35:13 +10307558free_rto_mask:
7559 free_cpumask_var(rd->rto_mask);
Rusty Russellc6c49272008-11-25 02:35:05 +10307560free_online:
7561 free_cpumask_var(rd->online);
7562free_span:
7563 free_cpumask_var(rd->span);
Li Zefan0c910d22009-01-06 17:39:06 +08007564out:
Rusty Russellc6c49272008-11-25 02:35:05 +10307565 return -ENOMEM;
Gregory Haskins57d885f2008-01-25 21:08:18 +01007566}
7567
7568static void init_defrootdomain(void)
7569{
Rusty Russellc6c49272008-11-25 02:35:05 +10307570 init_rootdomain(&def_root_domain, true);
7571
Gregory Haskins57d885f2008-01-25 21:08:18 +01007572 atomic_set(&def_root_domain.refcount, 1);
7573}
7574
Gregory Haskinsdc938522008-01-25 21:08:26 +01007575static struct root_domain *alloc_rootdomain(void)
Gregory Haskins57d885f2008-01-25 21:08:18 +01007576{
7577 struct root_domain *rd;
7578
7579 rd = kmalloc(sizeof(*rd), GFP_KERNEL);
7580 if (!rd)
7581 return NULL;
7582
Rusty Russellc6c49272008-11-25 02:35:05 +10307583 if (init_rootdomain(rd, false) != 0) {
7584 kfree(rd);
7585 return NULL;
7586 }
Gregory Haskins57d885f2008-01-25 21:08:18 +01007587
7588 return rd;
7589}
7590
Linus Torvalds1da177e2005-04-16 15:20:36 -07007591/*
Ingo Molnar0eab9142008-01-25 21:08:19 +01007592 * Attach the domain 'sd' to 'cpu' as its base domain. Callers must
Linus Torvalds1da177e2005-04-16 15:20:36 -07007593 * hold the hotplug lock.
7594 */
Ingo Molnar0eab9142008-01-25 21:08:19 +01007595static void
7596cpu_attach_domain(struct sched_domain *sd, struct root_domain *rd, int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007597{
Ingo Molnar70b97a72006-07-03 00:25:42 -07007598 struct rq *rq = cpu_rq(cpu);
Suresh Siddha245af2c2005-06-25 14:57:25 -07007599 struct sched_domain *tmp;
7600
7601 /* Remove the sched domains which do not contribute to scheduling. */
Li Zefanf29c9b12008-11-06 09:45:16 +08007602 for (tmp = sd; tmp; ) {
Suresh Siddha245af2c2005-06-25 14:57:25 -07007603 struct sched_domain *parent = tmp->parent;
7604 if (!parent)
7605 break;
Li Zefanf29c9b12008-11-06 09:45:16 +08007606
Siddha, Suresh B1a848872006-10-03 01:14:08 -07007607 if (sd_parent_degenerate(tmp, parent)) {
Suresh Siddha245af2c2005-06-25 14:57:25 -07007608 tmp->parent = parent->parent;
Siddha, Suresh B1a848872006-10-03 01:14:08 -07007609 if (parent->parent)
7610 parent->parent->child = tmp;
Li Zefanf29c9b12008-11-06 09:45:16 +08007611 } else
7612 tmp = tmp->parent;
Suresh Siddha245af2c2005-06-25 14:57:25 -07007613 }
7614
Siddha, Suresh B1a848872006-10-03 01:14:08 -07007615 if (sd && sd_degenerate(sd)) {
Suresh Siddha245af2c2005-06-25 14:57:25 -07007616 sd = sd->parent;
Siddha, Suresh B1a848872006-10-03 01:14:08 -07007617 if (sd)
7618 sd->child = NULL;
7619 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007620
7621 sched_domain_debug(sd, cpu);
7622
Gregory Haskins57d885f2008-01-25 21:08:18 +01007623 rq_attach_root(rq, rd);
Nick Piggin674311d2005-06-25 14:57:27 -07007624 rcu_assign_pointer(rq->sd, sd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007625}
7626
7627/* cpus with isolated domains */
Rusty Russelldcc30a32008-11-25 02:35:12 +10307628static cpumask_var_t cpu_isolated_map;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007629
7630/* Setup the mask of cpus configured for isolated domains */
7631static int __init isolated_cpu_setup(char *str)
7632{
Rusty Russell968ea6d2008-12-13 21:55:51 +10307633 cpulist_parse(str, cpu_isolated_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007634 return 1;
7635}
7636
Ingo Molnar8927f492007-10-15 17:00:13 +02007637__setup("isolcpus=", isolated_cpu_setup);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007638
7639/*
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08007640 * init_sched_build_groups takes the cpumask we wish to span, and a pointer
7641 * to a function which identifies what group(along with sched group) a CPU
Rusty Russell96f874e2008-11-25 02:35:14 +10307642 * belongs to. The return value of group_fn must be a >= 0 and < nr_cpu_ids
7643 * (due to the fact that we keep track of groups covered with a struct cpumask).
Linus Torvalds1da177e2005-04-16 15:20:36 -07007644 *
7645 * init_sched_build_groups will build a circular linked list of the groups
7646 * covered by the given span, and will set each group's ->cpumask correctly,
7647 * and ->cpu_power to 0.
7648 */
Siddha, Suresh Ba6160582006-10-03 01:14:06 -07007649static void
Rusty Russell96f874e2008-11-25 02:35:14 +10307650init_sched_build_groups(const struct cpumask *span,
7651 const struct cpumask *cpu_map,
7652 int (*group_fn)(int cpu, const struct cpumask *cpu_map,
Mike Travis7c16ec52008-04-04 18:11:11 -07007653 struct sched_group **sg,
Rusty Russell96f874e2008-11-25 02:35:14 +10307654 struct cpumask *tmpmask),
7655 struct cpumask *covered, struct cpumask *tmpmask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007656{
7657 struct sched_group *first = NULL, *last = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007658 int i;
7659
Rusty Russell96f874e2008-11-25 02:35:14 +10307660 cpumask_clear(covered);
Mike Travis7c16ec52008-04-04 18:11:11 -07007661
Rusty Russellabcd0832008-11-25 02:35:02 +10307662 for_each_cpu(i, span) {
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08007663 struct sched_group *sg;
Mike Travis7c16ec52008-04-04 18:11:11 -07007664 int group = group_fn(i, cpu_map, &sg, tmpmask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007665 int j;
7666
Rusty Russell758b2cd2008-11-25 02:35:04 +10307667 if (cpumask_test_cpu(i, covered))
Linus Torvalds1da177e2005-04-16 15:20:36 -07007668 continue;
7669
Rusty Russell758b2cd2008-11-25 02:35:04 +10307670 cpumask_clear(sched_group_cpus(sg));
Eric Dumazet5517d862007-05-08 00:32:57 -07007671 sg->__cpu_power = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007672
Rusty Russellabcd0832008-11-25 02:35:02 +10307673 for_each_cpu(j, span) {
Mike Travis7c16ec52008-04-04 18:11:11 -07007674 if (group_fn(j, cpu_map, NULL, tmpmask) != group)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007675 continue;
7676
Rusty Russell96f874e2008-11-25 02:35:14 +10307677 cpumask_set_cpu(j, covered);
Rusty Russell758b2cd2008-11-25 02:35:04 +10307678 cpumask_set_cpu(j, sched_group_cpus(sg));
Linus Torvalds1da177e2005-04-16 15:20:36 -07007679 }
7680 if (!first)
7681 first = sg;
7682 if (last)
7683 last->next = sg;
7684 last = sg;
7685 }
7686 last->next = first;
7687}
7688
John Hawkes9c1cfda2005-09-06 15:18:14 -07007689#define SD_NODES_PER_DOMAIN 16
Linus Torvalds1da177e2005-04-16 15:20:36 -07007690
John Hawkes9c1cfda2005-09-06 15:18:14 -07007691#ifdef CONFIG_NUMA
akpm@osdl.org198e2f12006-01-12 01:05:30 -08007692
John Hawkes9c1cfda2005-09-06 15:18:14 -07007693/**
7694 * find_next_best_node - find the next node to include in a sched_domain
7695 * @node: node whose sched_domain we're building
7696 * @used_nodes: nodes already in the sched_domain
7697 *
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01007698 * Find the next node to include in a given scheduling domain. Simply
John Hawkes9c1cfda2005-09-06 15:18:14 -07007699 * finds the closest node not already in the @used_nodes map.
7700 *
7701 * Should use nodemask_t.
7702 */
Mike Travisc5f59f02008-04-04 18:11:10 -07007703static int find_next_best_node(int node, nodemask_t *used_nodes)
John Hawkes9c1cfda2005-09-06 15:18:14 -07007704{
7705 int i, n, val, min_val, best_node = 0;
7706
7707 min_val = INT_MAX;
7708
Mike Travis076ac2a2008-05-12 21:21:12 +02007709 for (i = 0; i < nr_node_ids; i++) {
John Hawkes9c1cfda2005-09-06 15:18:14 -07007710 /* Start at @node */
Mike Travis076ac2a2008-05-12 21:21:12 +02007711 n = (node + i) % nr_node_ids;
John Hawkes9c1cfda2005-09-06 15:18:14 -07007712
7713 if (!nr_cpus_node(n))
7714 continue;
7715
7716 /* Skip already used nodes */
Mike Travisc5f59f02008-04-04 18:11:10 -07007717 if (node_isset(n, *used_nodes))
John Hawkes9c1cfda2005-09-06 15:18:14 -07007718 continue;
7719
7720 /* Simple min distance search */
7721 val = node_distance(node, n);
7722
7723 if (val < min_val) {
7724 min_val = val;
7725 best_node = n;
7726 }
7727 }
7728
Mike Travisc5f59f02008-04-04 18:11:10 -07007729 node_set(best_node, *used_nodes);
John Hawkes9c1cfda2005-09-06 15:18:14 -07007730 return best_node;
7731}
7732
7733/**
7734 * sched_domain_node_span - get a cpumask for a node's sched_domain
7735 * @node: node whose cpumask we're constructing
Randy Dunlap73486722008-04-22 10:07:22 -07007736 * @span: resulting cpumask
John Hawkes9c1cfda2005-09-06 15:18:14 -07007737 *
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01007738 * Given a node, construct a good cpumask for its sched_domain to span. It
John Hawkes9c1cfda2005-09-06 15:18:14 -07007739 * should be one that prevents unnecessary balancing, but also spreads tasks
7740 * out optimally.
7741 */
Rusty Russell96f874e2008-11-25 02:35:14 +10307742static void sched_domain_node_span(int node, struct cpumask *span)
John Hawkes9c1cfda2005-09-06 15:18:14 -07007743{
Mike Travisc5f59f02008-04-04 18:11:10 -07007744 nodemask_t used_nodes;
Ingo Molnar48f24c42006-07-03 00:25:40 -07007745 int i;
John Hawkes9c1cfda2005-09-06 15:18:14 -07007746
Mike Travis6ca09df2008-12-31 18:08:45 -08007747 cpumask_clear(span);
Mike Travisc5f59f02008-04-04 18:11:10 -07007748 nodes_clear(used_nodes);
John Hawkes9c1cfda2005-09-06 15:18:14 -07007749
Mike Travis6ca09df2008-12-31 18:08:45 -08007750 cpumask_or(span, span, cpumask_of_node(node));
Mike Travisc5f59f02008-04-04 18:11:10 -07007751 node_set(node, used_nodes);
John Hawkes9c1cfda2005-09-06 15:18:14 -07007752
7753 for (i = 1; i < SD_NODES_PER_DOMAIN; i++) {
Mike Travisc5f59f02008-04-04 18:11:10 -07007754 int next_node = find_next_best_node(node, &used_nodes);
Ingo Molnar48f24c42006-07-03 00:25:40 -07007755
Mike Travis6ca09df2008-12-31 18:08:45 -08007756 cpumask_or(span, span, cpumask_of_node(next_node));
John Hawkes9c1cfda2005-09-06 15:18:14 -07007757 }
John Hawkes9c1cfda2005-09-06 15:18:14 -07007758}
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +02007759#endif /* CONFIG_NUMA */
John Hawkes9c1cfda2005-09-06 15:18:14 -07007760
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07007761int sched_smt_power_savings = 0, sched_mc_power_savings = 0;
Ingo Molnar48f24c42006-07-03 00:25:40 -07007762
John Hawkes9c1cfda2005-09-06 15:18:14 -07007763/*
Rusty Russell6c99e9a2008-11-25 02:35:04 +10307764 * The cpus mask in sched_group and sched_domain hangs off the end.
7765 * FIXME: use cpumask_var_t or dynamic percpu alloc to avoid wasting space
7766 * for nr_cpu_ids < CONFIG_NR_CPUS.
7767 */
7768struct static_sched_group {
7769 struct sched_group sg;
7770 DECLARE_BITMAP(cpus, CONFIG_NR_CPUS);
7771};
7772
7773struct static_sched_domain {
7774 struct sched_domain sd;
7775 DECLARE_BITMAP(span, CONFIG_NR_CPUS);
7776};
7777
7778/*
Ingo Molnar48f24c42006-07-03 00:25:40 -07007779 * SMT sched-domains:
John Hawkes9c1cfda2005-09-06 15:18:14 -07007780 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007781#ifdef CONFIG_SCHED_SMT
Rusty Russell6c99e9a2008-11-25 02:35:04 +10307782static DEFINE_PER_CPU(struct static_sched_domain, cpu_domains);
7783static DEFINE_PER_CPU(struct static_sched_group, sched_group_cpus);
Ingo Molnar48f24c42006-07-03 00:25:40 -07007784
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01007785static int
Rusty Russell96f874e2008-11-25 02:35:14 +10307786cpu_to_cpu_group(int cpu, const struct cpumask *cpu_map,
7787 struct sched_group **sg, struct cpumask *unused)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007788{
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08007789 if (sg)
Rusty Russell6c99e9a2008-11-25 02:35:04 +10307790 *sg = &per_cpu(sched_group_cpus, cpu).sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007791 return cpu;
7792}
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +02007793#endif /* CONFIG_SCHED_SMT */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007794
Ingo Molnar48f24c42006-07-03 00:25:40 -07007795/*
7796 * multi-core sched-domains:
7797 */
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08007798#ifdef CONFIG_SCHED_MC
Rusty Russell6c99e9a2008-11-25 02:35:04 +10307799static DEFINE_PER_CPU(struct static_sched_domain, core_domains);
7800static DEFINE_PER_CPU(struct static_sched_group, sched_group_core);
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +02007801#endif /* CONFIG_SCHED_MC */
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08007802
7803#if defined(CONFIG_SCHED_MC) && defined(CONFIG_SCHED_SMT)
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01007804static int
Rusty Russell96f874e2008-11-25 02:35:14 +10307805cpu_to_core_group(int cpu, const struct cpumask *cpu_map,
7806 struct sched_group **sg, struct cpumask *mask)
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08007807{
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08007808 int group;
Mike Travis7c16ec52008-04-04 18:11:11 -07007809
Rusty Russellc69fc562009-03-13 14:49:46 +10307810 cpumask_and(mask, topology_thread_cpumask(cpu), cpu_map);
Rusty Russell96f874e2008-11-25 02:35:14 +10307811 group = cpumask_first(mask);
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08007812 if (sg)
Rusty Russell6c99e9a2008-11-25 02:35:04 +10307813 *sg = &per_cpu(sched_group_core, group).sg;
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08007814 return group;
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08007815}
7816#elif defined(CONFIG_SCHED_MC)
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01007817static int
Rusty Russell96f874e2008-11-25 02:35:14 +10307818cpu_to_core_group(int cpu, const struct cpumask *cpu_map,
7819 struct sched_group **sg, struct cpumask *unused)
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08007820{
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08007821 if (sg)
Rusty Russell6c99e9a2008-11-25 02:35:04 +10307822 *sg = &per_cpu(sched_group_core, cpu).sg;
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08007823 return cpu;
7824}
7825#endif
7826
Rusty Russell6c99e9a2008-11-25 02:35:04 +10307827static DEFINE_PER_CPU(struct static_sched_domain, phys_domains);
7828static DEFINE_PER_CPU(struct static_sched_group, sched_group_phys);
Ingo Molnar48f24c42006-07-03 00:25:40 -07007829
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01007830static int
Rusty Russell96f874e2008-11-25 02:35:14 +10307831cpu_to_phys_group(int cpu, const struct cpumask *cpu_map,
7832 struct sched_group **sg, struct cpumask *mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007833{
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08007834 int group;
Ingo Molnar48f24c42006-07-03 00:25:40 -07007835#ifdef CONFIG_SCHED_MC
Mike Travis6ca09df2008-12-31 18:08:45 -08007836 cpumask_and(mask, cpu_coregroup_mask(cpu), cpu_map);
Rusty Russell96f874e2008-11-25 02:35:14 +10307837 group = cpumask_first(mask);
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08007838#elif defined(CONFIG_SCHED_SMT)
Rusty Russellc69fc562009-03-13 14:49:46 +10307839 cpumask_and(mask, topology_thread_cpumask(cpu), cpu_map);
Rusty Russell96f874e2008-11-25 02:35:14 +10307840 group = cpumask_first(mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007841#else
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08007842 group = cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007843#endif
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08007844 if (sg)
Rusty Russell6c99e9a2008-11-25 02:35:04 +10307845 *sg = &per_cpu(sched_group_phys, group).sg;
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08007846 return group;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007847}
7848
7849#ifdef CONFIG_NUMA
John Hawkes9c1cfda2005-09-06 15:18:14 -07007850/*
7851 * The init_sched_build_groups can't handle what we want to do with node
7852 * groups, so roll our own. Now each node has its own list of groups which
7853 * gets dynamically allocated.
7854 */
Rusty Russell62ea9ce2009-01-11 01:04:16 +01007855static DEFINE_PER_CPU(struct static_sched_domain, node_domains);
Mike Travis434d53b2008-04-04 18:11:04 -07007856static struct sched_group ***sched_group_nodes_bycpu;
John Hawkes9c1cfda2005-09-06 15:18:14 -07007857
Rusty Russell62ea9ce2009-01-11 01:04:16 +01007858static DEFINE_PER_CPU(struct static_sched_domain, allnodes_domains);
Rusty Russell6c99e9a2008-11-25 02:35:04 +10307859static DEFINE_PER_CPU(struct static_sched_group, sched_group_allnodes);
John Hawkes9c1cfda2005-09-06 15:18:14 -07007860
Rusty Russell96f874e2008-11-25 02:35:14 +10307861static int cpu_to_allnodes_group(int cpu, const struct cpumask *cpu_map,
7862 struct sched_group **sg,
7863 struct cpumask *nodemask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007864{
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08007865 int group;
7866
Mike Travis6ca09df2008-12-31 18:08:45 -08007867 cpumask_and(nodemask, cpumask_of_node(cpu_to_node(cpu)), cpu_map);
Rusty Russell96f874e2008-11-25 02:35:14 +10307868 group = cpumask_first(nodemask);
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08007869
7870 if (sg)
Rusty Russell6c99e9a2008-11-25 02:35:04 +10307871 *sg = &per_cpu(sched_group_allnodes, group).sg;
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08007872 return group;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007873}
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08007874
Siddha, Suresh B08069032006-03-27 01:15:23 -08007875static void init_numa_sched_groups_power(struct sched_group *group_head)
7876{
7877 struct sched_group *sg = group_head;
7878 int j;
7879
7880 if (!sg)
7881 return;
Andi Kleen3a5c3592007-10-15 17:00:14 +02007882 do {
Rusty Russell758b2cd2008-11-25 02:35:04 +10307883 for_each_cpu(j, sched_group_cpus(sg)) {
Andi Kleen3a5c3592007-10-15 17:00:14 +02007884 struct sched_domain *sd;
Siddha, Suresh B08069032006-03-27 01:15:23 -08007885
Rusty Russell6c99e9a2008-11-25 02:35:04 +10307886 sd = &per_cpu(phys_domains, j).sd;
Rusty Russell758b2cd2008-11-25 02:35:04 +10307887 if (j != cpumask_first(sched_group_cpus(sd->groups))) {
Andi Kleen3a5c3592007-10-15 17:00:14 +02007888 /*
7889 * Only add "power" once for each
7890 * physical package.
7891 */
7892 continue;
7893 }
7894
7895 sg_inc_cpu_power(sg, sd->groups->__cpu_power);
Siddha, Suresh B08069032006-03-27 01:15:23 -08007896 }
Andi Kleen3a5c3592007-10-15 17:00:14 +02007897 sg = sg->next;
7898 } while (sg != group_head);
Siddha, Suresh B08069032006-03-27 01:15:23 -08007899}
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +02007900#endif /* CONFIG_NUMA */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007901
Siddha, Suresh Ba6160582006-10-03 01:14:06 -07007902#ifdef CONFIG_NUMA
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07007903/* Free memory allocated for various sched_group structures */
Rusty Russell96f874e2008-11-25 02:35:14 +10307904static void free_sched_groups(const struct cpumask *cpu_map,
7905 struct cpumask *nodemask)
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07007906{
Siddha, Suresh Ba6160582006-10-03 01:14:06 -07007907 int cpu, i;
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07007908
Rusty Russellabcd0832008-11-25 02:35:02 +10307909 for_each_cpu(cpu, cpu_map) {
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07007910 struct sched_group **sched_group_nodes
7911 = sched_group_nodes_bycpu[cpu];
7912
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07007913 if (!sched_group_nodes)
7914 continue;
7915
Mike Travis076ac2a2008-05-12 21:21:12 +02007916 for (i = 0; i < nr_node_ids; i++) {
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07007917 struct sched_group *oldsg, *sg = sched_group_nodes[i];
7918
Mike Travis6ca09df2008-12-31 18:08:45 -08007919 cpumask_and(nodemask, cpumask_of_node(i), cpu_map);
Rusty Russell96f874e2008-11-25 02:35:14 +10307920 if (cpumask_empty(nodemask))
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07007921 continue;
7922
7923 if (sg == NULL)
7924 continue;
7925 sg = sg->next;
7926next_sg:
7927 oldsg = sg;
7928 sg = sg->next;
7929 kfree(oldsg);
7930 if (oldsg != sched_group_nodes[i])
7931 goto next_sg;
7932 }
7933 kfree(sched_group_nodes);
7934 sched_group_nodes_bycpu[cpu] = NULL;
7935 }
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07007936}
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +02007937#else /* !CONFIG_NUMA */
Rusty Russell96f874e2008-11-25 02:35:14 +10307938static void free_sched_groups(const struct cpumask *cpu_map,
7939 struct cpumask *nodemask)
Siddha, Suresh Ba6160582006-10-03 01:14:06 -07007940{
7941}
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +02007942#endif /* CONFIG_NUMA */
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07007943
Linus Torvalds1da177e2005-04-16 15:20:36 -07007944/*
Siddha, Suresh B89c47102006-10-03 01:14:09 -07007945 * Initialize sched groups cpu_power.
7946 *
7947 * cpu_power indicates the capacity of sched group, which is used while
7948 * distributing the load between different sched groups in a sched domain.
7949 * Typically cpu_power for all the groups in a sched domain will be same unless
7950 * there are asymmetries in the topology. If there are asymmetries, group
7951 * having more cpu_power will pickup more load compared to the group having
7952 * less cpu_power.
7953 *
7954 * cpu_power will be a multiple of SCHED_LOAD_SCALE. This multiple represents
7955 * the maximum number of tasks a group can handle in the presence of other idle
7956 * or lightly loaded groups in the same sched domain.
7957 */
7958static void init_sched_groups_power(int cpu, struct sched_domain *sd)
7959{
7960 struct sched_domain *child;
7961 struct sched_group *group;
7962
7963 WARN_ON(!sd || !sd->groups);
7964
Rusty Russell758b2cd2008-11-25 02:35:04 +10307965 if (cpu != cpumask_first(sched_group_cpus(sd->groups)))
Siddha, Suresh B89c47102006-10-03 01:14:09 -07007966 return;
7967
7968 child = sd->child;
7969
Eric Dumazet5517d862007-05-08 00:32:57 -07007970 sd->groups->__cpu_power = 0;
7971
Siddha, Suresh B89c47102006-10-03 01:14:09 -07007972 /*
7973 * For perf policy, if the groups in child domain share resources
7974 * (for example cores sharing some portions of the cache hierarchy
7975 * or SMT), then set this domain groups cpu_power such that each group
7976 * can handle only one task, when there are other idle groups in the
7977 * same sched domain.
7978 */
7979 if (!child || (!(sd->flags & SD_POWERSAVINGS_BALANCE) &&
7980 (child->flags &
7981 (SD_SHARE_CPUPOWER | SD_SHARE_PKG_RESOURCES)))) {
Eric Dumazet5517d862007-05-08 00:32:57 -07007982 sg_inc_cpu_power(sd->groups, SCHED_LOAD_SCALE);
Siddha, Suresh B89c47102006-10-03 01:14:09 -07007983 return;
7984 }
7985
Siddha, Suresh B89c47102006-10-03 01:14:09 -07007986 /*
7987 * add cpu_power of each child group to this groups cpu_power
7988 */
7989 group = child->groups;
7990 do {
Eric Dumazet5517d862007-05-08 00:32:57 -07007991 sg_inc_cpu_power(sd->groups, group->__cpu_power);
Siddha, Suresh B89c47102006-10-03 01:14:09 -07007992 group = group->next;
7993 } while (group != child->groups);
7994}
7995
7996/*
Mike Travis7c16ec52008-04-04 18:11:11 -07007997 * Initializers for schedule domains
7998 * Non-inlined to reduce accumulated stack pressure in build_sched_domains()
7999 */
8000
Ingo Molnara5d8c342008-10-09 11:35:51 +02008001#ifdef CONFIG_SCHED_DEBUG
8002# define SD_INIT_NAME(sd, type) sd->name = #type
8003#else
8004# define SD_INIT_NAME(sd, type) do { } while (0)
8005#endif
8006
Mike Travis7c16ec52008-04-04 18:11:11 -07008007#define SD_INIT(sd, type) sd_init_##type(sd)
Ingo Molnara5d8c342008-10-09 11:35:51 +02008008
Mike Travis7c16ec52008-04-04 18:11:11 -07008009#define SD_INIT_FUNC(type) \
8010static noinline void sd_init_##type(struct sched_domain *sd) \
8011{ \
8012 memset(sd, 0, sizeof(*sd)); \
8013 *sd = SD_##type##_INIT; \
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09008014 sd->level = SD_LV_##type; \
Ingo Molnara5d8c342008-10-09 11:35:51 +02008015 SD_INIT_NAME(sd, type); \
Mike Travis7c16ec52008-04-04 18:11:11 -07008016}
8017
8018SD_INIT_FUNC(CPU)
8019#ifdef CONFIG_NUMA
8020 SD_INIT_FUNC(ALLNODES)
8021 SD_INIT_FUNC(NODE)
8022#endif
8023#ifdef CONFIG_SCHED_SMT
8024 SD_INIT_FUNC(SIBLING)
8025#endif
8026#ifdef CONFIG_SCHED_MC
8027 SD_INIT_FUNC(MC)
8028#endif
8029
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09008030static int default_relax_domain_level = -1;
8031
8032static int __init setup_relax_domain_level(char *str)
8033{
Li Zefan30e0e172008-05-13 10:27:17 +08008034 unsigned long val;
8035
8036 val = simple_strtoul(str, NULL, 0);
8037 if (val < SD_LV_MAX)
8038 default_relax_domain_level = val;
8039
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09008040 return 1;
8041}
8042__setup("relax_domain_level=", setup_relax_domain_level);
8043
8044static void set_domain_attribute(struct sched_domain *sd,
8045 struct sched_domain_attr *attr)
8046{
8047 int request;
8048
8049 if (!attr || attr->relax_domain_level < 0) {
8050 if (default_relax_domain_level < 0)
8051 return;
8052 else
8053 request = default_relax_domain_level;
8054 } else
8055 request = attr->relax_domain_level;
8056 if (request < sd->level) {
8057 /* turn off idle balance on this domain */
8058 sd->flags &= ~(SD_WAKE_IDLE|SD_BALANCE_NEWIDLE);
8059 } else {
8060 /* turn on idle balance on this domain */
8061 sd->flags |= (SD_WAKE_IDLE_FAR|SD_BALANCE_NEWIDLE);
8062 }
8063}
8064
Mike Travis7c16ec52008-04-04 18:11:11 -07008065/*
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07008066 * Build sched domains for a given set of cpus and attach the sched domains
8067 * to the individual cpus
Linus Torvalds1da177e2005-04-16 15:20:36 -07008068 */
Rusty Russell96f874e2008-11-25 02:35:14 +10308069static int __build_sched_domains(const struct cpumask *cpu_map,
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09008070 struct sched_domain_attr *attr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008071{
Rusty Russell3404c8d2008-11-25 02:35:03 +10308072 int i, err = -ENOMEM;
Gregory Haskins57d885f2008-01-25 21:08:18 +01008073 struct root_domain *rd;
Rusty Russell3404c8d2008-11-25 02:35:03 +10308074 cpumask_var_t nodemask, this_sibling_map, this_core_map, send_covered,
8075 tmpmask;
John Hawkesd1b55132005-09-06 15:18:14 -07008076#ifdef CONFIG_NUMA
Rusty Russell3404c8d2008-11-25 02:35:03 +10308077 cpumask_var_t domainspan, covered, notcovered;
John Hawkesd1b55132005-09-06 15:18:14 -07008078 struct sched_group **sched_group_nodes = NULL;
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08008079 int sd_allnodes = 0;
John Hawkesd1b55132005-09-06 15:18:14 -07008080
Rusty Russell3404c8d2008-11-25 02:35:03 +10308081 if (!alloc_cpumask_var(&domainspan, GFP_KERNEL))
8082 goto out;
8083 if (!alloc_cpumask_var(&covered, GFP_KERNEL))
8084 goto free_domainspan;
8085 if (!alloc_cpumask_var(&notcovered, GFP_KERNEL))
8086 goto free_covered;
8087#endif
8088
8089 if (!alloc_cpumask_var(&nodemask, GFP_KERNEL))
8090 goto free_notcovered;
8091 if (!alloc_cpumask_var(&this_sibling_map, GFP_KERNEL))
8092 goto free_nodemask;
8093 if (!alloc_cpumask_var(&this_core_map, GFP_KERNEL))
8094 goto free_this_sibling_map;
8095 if (!alloc_cpumask_var(&send_covered, GFP_KERNEL))
8096 goto free_this_core_map;
8097 if (!alloc_cpumask_var(&tmpmask, GFP_KERNEL))
8098 goto free_send_covered;
8099
8100#ifdef CONFIG_NUMA
John Hawkesd1b55132005-09-06 15:18:14 -07008101 /*
8102 * Allocate the per-node list of sched groups
8103 */
Mike Travis076ac2a2008-05-12 21:21:12 +02008104 sched_group_nodes = kcalloc(nr_node_ids, sizeof(struct sched_group *),
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01008105 GFP_KERNEL);
John Hawkesd1b55132005-09-06 15:18:14 -07008106 if (!sched_group_nodes) {
8107 printk(KERN_WARNING "Can not alloc sched group node list\n");
Rusty Russell3404c8d2008-11-25 02:35:03 +10308108 goto free_tmpmask;
John Hawkesd1b55132005-09-06 15:18:14 -07008109 }
John Hawkesd1b55132005-09-06 15:18:14 -07008110#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07008111
Gregory Haskinsdc938522008-01-25 21:08:26 +01008112 rd = alloc_rootdomain();
Gregory Haskins57d885f2008-01-25 21:08:18 +01008113 if (!rd) {
8114 printk(KERN_WARNING "Cannot alloc root domain\n");
Rusty Russell3404c8d2008-11-25 02:35:03 +10308115 goto free_sched_groups;
Gregory Haskins57d885f2008-01-25 21:08:18 +01008116 }
8117
Mike Travis7c16ec52008-04-04 18:11:11 -07008118#ifdef CONFIG_NUMA
Rusty Russell96f874e2008-11-25 02:35:14 +10308119 sched_group_nodes_bycpu[cpumask_first(cpu_map)] = sched_group_nodes;
Mike Travis7c16ec52008-04-04 18:11:11 -07008120#endif
8121
Linus Torvalds1da177e2005-04-16 15:20:36 -07008122 /*
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07008123 * Set up domains for cpus specified by the cpu_map.
Linus Torvalds1da177e2005-04-16 15:20:36 -07008124 */
Rusty Russellabcd0832008-11-25 02:35:02 +10308125 for_each_cpu(i, cpu_map) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008126 struct sched_domain *sd = NULL, *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008127
Mike Travis6ca09df2008-12-31 18:08:45 -08008128 cpumask_and(nodemask, cpumask_of_node(cpu_to_node(i)), cpu_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008129
8130#ifdef CONFIG_NUMA
Rusty Russell96f874e2008-11-25 02:35:14 +10308131 if (cpumask_weight(cpu_map) >
8132 SD_NODES_PER_DOMAIN*cpumask_weight(nodemask)) {
Rusty Russell62ea9ce2009-01-11 01:04:16 +01008133 sd = &per_cpu(allnodes_domains, i).sd;
Mike Travis7c16ec52008-04-04 18:11:11 -07008134 SD_INIT(sd, ALLNODES);
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09008135 set_domain_attribute(sd, attr);
Rusty Russell758b2cd2008-11-25 02:35:04 +10308136 cpumask_copy(sched_domain_span(sd), cpu_map);
Mike Travis7c16ec52008-04-04 18:11:11 -07008137 cpu_to_allnodes_group(i, cpu_map, &sd->groups, tmpmask);
John Hawkes9c1cfda2005-09-06 15:18:14 -07008138 p = sd;
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08008139 sd_allnodes = 1;
John Hawkes9c1cfda2005-09-06 15:18:14 -07008140 } else
8141 p = NULL;
8142
Rusty Russell62ea9ce2009-01-11 01:04:16 +01008143 sd = &per_cpu(node_domains, i).sd;
Mike Travis7c16ec52008-04-04 18:11:11 -07008144 SD_INIT(sd, NODE);
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09008145 set_domain_attribute(sd, attr);
Rusty Russell758b2cd2008-11-25 02:35:04 +10308146 sched_domain_node_span(cpu_to_node(i), sched_domain_span(sd));
John Hawkes9c1cfda2005-09-06 15:18:14 -07008147 sd->parent = p;
Siddha, Suresh B1a848872006-10-03 01:14:08 -07008148 if (p)
8149 p->child = sd;
Rusty Russell758b2cd2008-11-25 02:35:04 +10308150 cpumask_and(sched_domain_span(sd),
8151 sched_domain_span(sd), cpu_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008152#endif
8153
8154 p = sd;
Rusty Russell6c99e9a2008-11-25 02:35:04 +10308155 sd = &per_cpu(phys_domains, i).sd;
Mike Travis7c16ec52008-04-04 18:11:11 -07008156 SD_INIT(sd, CPU);
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09008157 set_domain_attribute(sd, attr);
Rusty Russell758b2cd2008-11-25 02:35:04 +10308158 cpumask_copy(sched_domain_span(sd), nodemask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008159 sd->parent = p;
Siddha, Suresh B1a848872006-10-03 01:14:08 -07008160 if (p)
8161 p->child = sd;
Mike Travis7c16ec52008-04-04 18:11:11 -07008162 cpu_to_phys_group(i, cpu_map, &sd->groups, tmpmask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008163
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08008164#ifdef CONFIG_SCHED_MC
8165 p = sd;
Rusty Russell6c99e9a2008-11-25 02:35:04 +10308166 sd = &per_cpu(core_domains, i).sd;
Mike Travis7c16ec52008-04-04 18:11:11 -07008167 SD_INIT(sd, MC);
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09008168 set_domain_attribute(sd, attr);
Mike Travis6ca09df2008-12-31 18:08:45 -08008169 cpumask_and(sched_domain_span(sd), cpu_map,
8170 cpu_coregroup_mask(i));
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08008171 sd->parent = p;
Siddha, Suresh B1a848872006-10-03 01:14:08 -07008172 p->child = sd;
Mike Travis7c16ec52008-04-04 18:11:11 -07008173 cpu_to_core_group(i, cpu_map, &sd->groups, tmpmask);
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08008174#endif
8175
Linus Torvalds1da177e2005-04-16 15:20:36 -07008176#ifdef CONFIG_SCHED_SMT
8177 p = sd;
Rusty Russell6c99e9a2008-11-25 02:35:04 +10308178 sd = &per_cpu(cpu_domains, i).sd;
Mike Travis7c16ec52008-04-04 18:11:11 -07008179 SD_INIT(sd, SIBLING);
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09008180 set_domain_attribute(sd, attr);
Rusty Russell758b2cd2008-11-25 02:35:04 +10308181 cpumask_and(sched_domain_span(sd),
Rusty Russellc69fc562009-03-13 14:49:46 +10308182 topology_thread_cpumask(i), cpu_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008183 sd->parent = p;
Siddha, Suresh B1a848872006-10-03 01:14:08 -07008184 p->child = sd;
Mike Travis7c16ec52008-04-04 18:11:11 -07008185 cpu_to_cpu_group(i, cpu_map, &sd->groups, tmpmask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008186#endif
8187 }
8188
8189#ifdef CONFIG_SCHED_SMT
8190 /* Set up CPU (sibling) groups */
Rusty Russellabcd0832008-11-25 02:35:02 +10308191 for_each_cpu(i, cpu_map) {
Rusty Russell96f874e2008-11-25 02:35:14 +10308192 cpumask_and(this_sibling_map,
Rusty Russellc69fc562009-03-13 14:49:46 +10308193 topology_thread_cpumask(i), cpu_map);
Rusty Russell96f874e2008-11-25 02:35:14 +10308194 if (i != cpumask_first(this_sibling_map))
Linus Torvalds1da177e2005-04-16 15:20:36 -07008195 continue;
8196
Ingo Molnardd41f592007-07-09 18:51:59 +02008197 init_sched_build_groups(this_sibling_map, cpu_map,
Mike Travis7c16ec52008-04-04 18:11:11 -07008198 &cpu_to_cpu_group,
8199 send_covered, tmpmask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008200 }
8201#endif
8202
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08008203#ifdef CONFIG_SCHED_MC
8204 /* Set up multi-core groups */
Rusty Russellabcd0832008-11-25 02:35:02 +10308205 for_each_cpu(i, cpu_map) {
Mike Travis6ca09df2008-12-31 18:08:45 -08008206 cpumask_and(this_core_map, cpu_coregroup_mask(i), cpu_map);
Rusty Russell96f874e2008-11-25 02:35:14 +10308207 if (i != cpumask_first(this_core_map))
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08008208 continue;
Mike Travis7c16ec52008-04-04 18:11:11 -07008209
Ingo Molnardd41f592007-07-09 18:51:59 +02008210 init_sched_build_groups(this_core_map, cpu_map,
Mike Travis7c16ec52008-04-04 18:11:11 -07008211 &cpu_to_core_group,
8212 send_covered, tmpmask);
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08008213 }
8214#endif
8215
Linus Torvalds1da177e2005-04-16 15:20:36 -07008216 /* Set up physical groups */
Mike Travis076ac2a2008-05-12 21:21:12 +02008217 for (i = 0; i < nr_node_ids; i++) {
Mike Travis6ca09df2008-12-31 18:08:45 -08008218 cpumask_and(nodemask, cpumask_of_node(i), cpu_map);
Rusty Russell96f874e2008-11-25 02:35:14 +10308219 if (cpumask_empty(nodemask))
Linus Torvalds1da177e2005-04-16 15:20:36 -07008220 continue;
8221
Mike Travis7c16ec52008-04-04 18:11:11 -07008222 init_sched_build_groups(nodemask, cpu_map,
8223 &cpu_to_phys_group,
8224 send_covered, tmpmask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008225 }
8226
8227#ifdef CONFIG_NUMA
8228 /* Set up node groups */
Mike Travis7c16ec52008-04-04 18:11:11 -07008229 if (sd_allnodes) {
Mike Travis7c16ec52008-04-04 18:11:11 -07008230 init_sched_build_groups(cpu_map, cpu_map,
8231 &cpu_to_allnodes_group,
8232 send_covered, tmpmask);
8233 }
John Hawkes9c1cfda2005-09-06 15:18:14 -07008234
Mike Travis076ac2a2008-05-12 21:21:12 +02008235 for (i = 0; i < nr_node_ids; i++) {
John Hawkes9c1cfda2005-09-06 15:18:14 -07008236 /* Set up node groups */
8237 struct sched_group *sg, *prev;
John Hawkes9c1cfda2005-09-06 15:18:14 -07008238 int j;
8239
Rusty Russell96f874e2008-11-25 02:35:14 +10308240 cpumask_clear(covered);
Mike Travis6ca09df2008-12-31 18:08:45 -08008241 cpumask_and(nodemask, cpumask_of_node(i), cpu_map);
Rusty Russell96f874e2008-11-25 02:35:14 +10308242 if (cpumask_empty(nodemask)) {
John Hawkesd1b55132005-09-06 15:18:14 -07008243 sched_group_nodes[i] = NULL;
John Hawkes9c1cfda2005-09-06 15:18:14 -07008244 continue;
John Hawkesd1b55132005-09-06 15:18:14 -07008245 }
John Hawkes9c1cfda2005-09-06 15:18:14 -07008246
Mike Travis4bdbaad32008-04-15 16:35:52 -07008247 sched_domain_node_span(i, domainspan);
Rusty Russell96f874e2008-11-25 02:35:14 +10308248 cpumask_and(domainspan, domainspan, cpu_map);
John Hawkes9c1cfda2005-09-06 15:18:14 -07008249
Rusty Russell6c99e9a2008-11-25 02:35:04 +10308250 sg = kmalloc_node(sizeof(struct sched_group) + cpumask_size(),
8251 GFP_KERNEL, i);
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07008252 if (!sg) {
8253 printk(KERN_WARNING "Can not alloc domain group for "
8254 "node %d\n", i);
8255 goto error;
8256 }
John Hawkes9c1cfda2005-09-06 15:18:14 -07008257 sched_group_nodes[i] = sg;
Rusty Russellabcd0832008-11-25 02:35:02 +10308258 for_each_cpu(j, nodemask) {
John Hawkes9c1cfda2005-09-06 15:18:14 -07008259 struct sched_domain *sd;
Ingo Molnar9761eea2007-07-09 18:52:00 +02008260
Rusty Russell62ea9ce2009-01-11 01:04:16 +01008261 sd = &per_cpu(node_domains, j).sd;
John Hawkes9c1cfda2005-09-06 15:18:14 -07008262 sd->groups = sg;
John Hawkes9c1cfda2005-09-06 15:18:14 -07008263 }
Eric Dumazet5517d862007-05-08 00:32:57 -07008264 sg->__cpu_power = 0;
Rusty Russell758b2cd2008-11-25 02:35:04 +10308265 cpumask_copy(sched_group_cpus(sg), nodemask);
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07008266 sg->next = sg;
Rusty Russell96f874e2008-11-25 02:35:14 +10308267 cpumask_or(covered, covered, nodemask);
John Hawkes9c1cfda2005-09-06 15:18:14 -07008268 prev = sg;
8269
Mike Travis076ac2a2008-05-12 21:21:12 +02008270 for (j = 0; j < nr_node_ids; j++) {
Mike Travis076ac2a2008-05-12 21:21:12 +02008271 int n = (i + j) % nr_node_ids;
John Hawkes9c1cfda2005-09-06 15:18:14 -07008272
Rusty Russell96f874e2008-11-25 02:35:14 +10308273 cpumask_complement(notcovered, covered);
8274 cpumask_and(tmpmask, notcovered, cpu_map);
8275 cpumask_and(tmpmask, tmpmask, domainspan);
8276 if (cpumask_empty(tmpmask))
John Hawkes9c1cfda2005-09-06 15:18:14 -07008277 break;
8278
Mike Travis6ca09df2008-12-31 18:08:45 -08008279 cpumask_and(tmpmask, tmpmask, cpumask_of_node(n));
Rusty Russell96f874e2008-11-25 02:35:14 +10308280 if (cpumask_empty(tmpmask))
John Hawkes9c1cfda2005-09-06 15:18:14 -07008281 continue;
8282
Rusty Russell6c99e9a2008-11-25 02:35:04 +10308283 sg = kmalloc_node(sizeof(struct sched_group) +
8284 cpumask_size(),
Srivatsa Vaddagiri15f0b672006-06-27 02:54:40 -07008285 GFP_KERNEL, i);
John Hawkes9c1cfda2005-09-06 15:18:14 -07008286 if (!sg) {
8287 printk(KERN_WARNING
8288 "Can not alloc domain group for node %d\n", j);
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07008289 goto error;
John Hawkes9c1cfda2005-09-06 15:18:14 -07008290 }
Eric Dumazet5517d862007-05-08 00:32:57 -07008291 sg->__cpu_power = 0;
Rusty Russell758b2cd2008-11-25 02:35:04 +10308292 cpumask_copy(sched_group_cpus(sg), tmpmask);
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07008293 sg->next = prev->next;
Rusty Russell96f874e2008-11-25 02:35:14 +10308294 cpumask_or(covered, covered, tmpmask);
John Hawkes9c1cfda2005-09-06 15:18:14 -07008295 prev->next = sg;
8296 prev = sg;
8297 }
John Hawkes9c1cfda2005-09-06 15:18:14 -07008298 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008299#endif
8300
8301 /* Calculate CPU power for physical packages and nodes */
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07008302#ifdef CONFIG_SCHED_SMT
Rusty Russellabcd0832008-11-25 02:35:02 +10308303 for_each_cpu(i, cpu_map) {
Rusty Russell6c99e9a2008-11-25 02:35:04 +10308304 struct sched_domain *sd = &per_cpu(cpu_domains, i).sd;
Ingo Molnardd41f592007-07-09 18:51:59 +02008305
Siddha, Suresh B89c47102006-10-03 01:14:09 -07008306 init_sched_groups_power(i, sd);
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07008307 }
8308#endif
8309#ifdef CONFIG_SCHED_MC
Rusty Russellabcd0832008-11-25 02:35:02 +10308310 for_each_cpu(i, cpu_map) {
Rusty Russell6c99e9a2008-11-25 02:35:04 +10308311 struct sched_domain *sd = &per_cpu(core_domains, i).sd;
Ingo Molnardd41f592007-07-09 18:51:59 +02008312
Siddha, Suresh B89c47102006-10-03 01:14:09 -07008313 init_sched_groups_power(i, sd);
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07008314 }
8315#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07008316
Rusty Russellabcd0832008-11-25 02:35:02 +10308317 for_each_cpu(i, cpu_map) {
Rusty Russell6c99e9a2008-11-25 02:35:04 +10308318 struct sched_domain *sd = &per_cpu(phys_domains, i).sd;
Ingo Molnardd41f592007-07-09 18:51:59 +02008319
Siddha, Suresh B89c47102006-10-03 01:14:09 -07008320 init_sched_groups_power(i, sd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008321 }
8322
John Hawkes9c1cfda2005-09-06 15:18:14 -07008323#ifdef CONFIG_NUMA
Mike Travis076ac2a2008-05-12 21:21:12 +02008324 for (i = 0; i < nr_node_ids; i++)
Siddha, Suresh B08069032006-03-27 01:15:23 -08008325 init_numa_sched_groups_power(sched_group_nodes[i]);
John Hawkes9c1cfda2005-09-06 15:18:14 -07008326
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08008327 if (sd_allnodes) {
8328 struct sched_group *sg;
Siddha, Suresh Bf712c0c2006-07-30 03:02:59 -07008329
Rusty Russell96f874e2008-11-25 02:35:14 +10308330 cpu_to_allnodes_group(cpumask_first(cpu_map), cpu_map, &sg,
Mike Travis7c16ec52008-04-04 18:11:11 -07008331 tmpmask);
Siddha, Suresh Bf712c0c2006-07-30 03:02:59 -07008332 init_numa_sched_groups_power(sg);
8333 }
John Hawkes9c1cfda2005-09-06 15:18:14 -07008334#endif
8335
Linus Torvalds1da177e2005-04-16 15:20:36 -07008336 /* Attach the domains */
Rusty Russellabcd0832008-11-25 02:35:02 +10308337 for_each_cpu(i, cpu_map) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008338 struct sched_domain *sd;
8339#ifdef CONFIG_SCHED_SMT
Rusty Russell6c99e9a2008-11-25 02:35:04 +10308340 sd = &per_cpu(cpu_domains, i).sd;
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08008341#elif defined(CONFIG_SCHED_MC)
Rusty Russell6c99e9a2008-11-25 02:35:04 +10308342 sd = &per_cpu(core_domains, i).sd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008343#else
Rusty Russell6c99e9a2008-11-25 02:35:04 +10308344 sd = &per_cpu(phys_domains, i).sd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008345#endif
Gregory Haskins57d885f2008-01-25 21:08:18 +01008346 cpu_attach_domain(sd, rd, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008347 }
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07008348
Rusty Russell3404c8d2008-11-25 02:35:03 +10308349 err = 0;
8350
8351free_tmpmask:
8352 free_cpumask_var(tmpmask);
8353free_send_covered:
8354 free_cpumask_var(send_covered);
8355free_this_core_map:
8356 free_cpumask_var(this_core_map);
8357free_this_sibling_map:
8358 free_cpumask_var(this_sibling_map);
8359free_nodemask:
8360 free_cpumask_var(nodemask);
8361free_notcovered:
8362#ifdef CONFIG_NUMA
8363 free_cpumask_var(notcovered);
8364free_covered:
8365 free_cpumask_var(covered);
8366free_domainspan:
8367 free_cpumask_var(domainspan);
8368out:
8369#endif
8370 return err;
8371
8372free_sched_groups:
8373#ifdef CONFIG_NUMA
8374 kfree(sched_group_nodes);
8375#endif
8376 goto free_tmpmask;
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07008377
Siddha, Suresh Ba6160582006-10-03 01:14:06 -07008378#ifdef CONFIG_NUMA
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07008379error:
Mike Travis7c16ec52008-04-04 18:11:11 -07008380 free_sched_groups(cpu_map, tmpmask);
Rusty Russellc6c49272008-11-25 02:35:05 +10308381 free_rootdomain(rd);
Rusty Russell3404c8d2008-11-25 02:35:03 +10308382 goto free_tmpmask;
Siddha, Suresh Ba6160582006-10-03 01:14:06 -07008383#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07008384}
Paul Jackson029190c2007-10-18 23:40:20 -07008385
Rusty Russell96f874e2008-11-25 02:35:14 +10308386static int build_sched_domains(const struct cpumask *cpu_map)
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09008387{
8388 return __build_sched_domains(cpu_map, NULL);
8389}
8390
Rusty Russell96f874e2008-11-25 02:35:14 +10308391static struct cpumask *doms_cur; /* current sched domains */
Paul Jackson029190c2007-10-18 23:40:20 -07008392static int ndoms_cur; /* number of sched domains in 'doms_cur' */
Ingo Molnar4285f5942008-05-16 17:47:14 +02008393static struct sched_domain_attr *dattr_cur;
8394 /* attribues of custom domains in 'doms_cur' */
Paul Jackson029190c2007-10-18 23:40:20 -07008395
8396/*
8397 * Special case: If a kmalloc of a doms_cur partition (array of
Rusty Russell42128232008-11-25 02:35:12 +10308398 * cpumask) fails, then fallback to a single sched domain,
8399 * as determined by the single cpumask fallback_doms.
Paul Jackson029190c2007-10-18 23:40:20 -07008400 */
Rusty Russell42128232008-11-25 02:35:12 +10308401static cpumask_var_t fallback_doms;
Paul Jackson029190c2007-10-18 23:40:20 -07008402
Heiko Carstensee79d1b2008-12-09 18:49:50 +01008403/*
8404 * arch_update_cpu_topology lets virtualized architectures update the
8405 * cpu core maps. It is supposed to return 1 if the topology changed
8406 * or 0 if it stayed the same.
8407 */
8408int __attribute__((weak)) arch_update_cpu_topology(void)
Heiko Carstens22e52b02008-03-12 18:31:59 +01008409{
Heiko Carstensee79d1b2008-12-09 18:49:50 +01008410 return 0;
Heiko Carstens22e52b02008-03-12 18:31:59 +01008411}
8412
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07008413/*
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01008414 * Set up scheduler domains and groups. Callers must hold the hotplug lock.
Paul Jackson029190c2007-10-18 23:40:20 -07008415 * For now this just excludes isolated cpus, but could be used to
8416 * exclude other special cases in the future.
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07008417 */
Rusty Russell96f874e2008-11-25 02:35:14 +10308418static int arch_init_sched_domains(const struct cpumask *cpu_map)
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07008419{
Milton Miller73785472007-10-24 18:23:48 +02008420 int err;
8421
Heiko Carstens22e52b02008-03-12 18:31:59 +01008422 arch_update_cpu_topology();
Paul Jackson029190c2007-10-18 23:40:20 -07008423 ndoms_cur = 1;
Rusty Russell96f874e2008-11-25 02:35:14 +10308424 doms_cur = kmalloc(cpumask_size(), GFP_KERNEL);
Paul Jackson029190c2007-10-18 23:40:20 -07008425 if (!doms_cur)
Rusty Russell42128232008-11-25 02:35:12 +10308426 doms_cur = fallback_doms;
Rusty Russelldcc30a32008-11-25 02:35:12 +10308427 cpumask_andnot(doms_cur, cpu_map, cpu_isolated_map);
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09008428 dattr_cur = NULL;
Milton Miller73785472007-10-24 18:23:48 +02008429 err = build_sched_domains(doms_cur);
Milton Miller6382bc92007-10-15 17:00:19 +02008430 register_sched_domain_sysctl();
Milton Miller73785472007-10-24 18:23:48 +02008431
8432 return err;
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07008433}
8434
Rusty Russell96f874e2008-11-25 02:35:14 +10308435static void arch_destroy_sched_domains(const struct cpumask *cpu_map,
8436 struct cpumask *tmpmask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008437{
Mike Travis7c16ec52008-04-04 18:11:11 -07008438 free_sched_groups(cpu_map, tmpmask);
John Hawkes9c1cfda2005-09-06 15:18:14 -07008439}
Linus Torvalds1da177e2005-04-16 15:20:36 -07008440
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07008441/*
8442 * Detach sched domains from a group of cpus specified in cpu_map
8443 * These cpus will now be attached to the NULL domain
8444 */
Rusty Russell96f874e2008-11-25 02:35:14 +10308445static void detach_destroy_domains(const struct cpumask *cpu_map)
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07008446{
Rusty Russell96f874e2008-11-25 02:35:14 +10308447 /* Save because hotplug lock held. */
8448 static DECLARE_BITMAP(tmpmask, CONFIG_NR_CPUS);
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07008449 int i;
8450
Rusty Russellabcd0832008-11-25 02:35:02 +10308451 for_each_cpu(i, cpu_map)
Gregory Haskins57d885f2008-01-25 21:08:18 +01008452 cpu_attach_domain(NULL, &def_root_domain, i);
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07008453 synchronize_sched();
Rusty Russell96f874e2008-11-25 02:35:14 +10308454 arch_destroy_sched_domains(cpu_map, to_cpumask(tmpmask));
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07008455}
8456
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09008457/* handle null as "default" */
8458static int dattrs_equal(struct sched_domain_attr *cur, int idx_cur,
8459 struct sched_domain_attr *new, int idx_new)
8460{
8461 struct sched_domain_attr tmp;
8462
8463 /* fast path */
8464 if (!new && !cur)
8465 return 1;
8466
8467 tmp = SD_ATTR_INIT;
8468 return !memcmp(cur ? (cur + idx_cur) : &tmp,
8469 new ? (new + idx_new) : &tmp,
8470 sizeof(struct sched_domain_attr));
8471}
8472
Paul Jackson029190c2007-10-18 23:40:20 -07008473/*
8474 * Partition sched domains as specified by the 'ndoms_new'
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01008475 * cpumasks in the array doms_new[] of cpumasks. This compares
Paul Jackson029190c2007-10-18 23:40:20 -07008476 * doms_new[] to the current sched domain partitioning, doms_cur[].
8477 * It destroys each deleted domain and builds each new domain.
8478 *
Rusty Russell96f874e2008-11-25 02:35:14 +10308479 * 'doms_new' is an array of cpumask's of length 'ndoms_new'.
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01008480 * The masks don't intersect (don't overlap.) We should setup one
8481 * sched domain for each mask. CPUs not in any of the cpumasks will
8482 * not be load balanced. If the same cpumask appears both in the
Paul Jackson029190c2007-10-18 23:40:20 -07008483 * current 'doms_cur' domains and in the new 'doms_new', we can leave
8484 * it as it is.
8485 *
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01008486 * The passed in 'doms_new' should be kmalloc'd. This routine takes
8487 * ownership of it and will kfree it when done with it. If the caller
Li Zefan700018e2008-11-18 14:02:03 +08008488 * failed the kmalloc call, then it can pass in doms_new == NULL &&
8489 * ndoms_new == 1, and partition_sched_domains() will fallback to
8490 * the single partition 'fallback_doms', it also forces the domains
8491 * to be rebuilt.
Paul Jackson029190c2007-10-18 23:40:20 -07008492 *
Rusty Russell96f874e2008-11-25 02:35:14 +10308493 * If doms_new == NULL it will be replaced with cpu_online_mask.
Li Zefan700018e2008-11-18 14:02:03 +08008494 * ndoms_new == 0 is a special case for destroying existing domains,
8495 * and it will not create the default domain.
Max Krasnyanskydfb512e2008-08-29 13:11:41 -07008496 *
Paul Jackson029190c2007-10-18 23:40:20 -07008497 * Call with hotplug lock held
8498 */
Rusty Russell96f874e2008-11-25 02:35:14 +10308499/* FIXME: Change to struct cpumask *doms_new[] */
8500void partition_sched_domains(int ndoms_new, struct cpumask *doms_new,
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09008501 struct sched_domain_attr *dattr_new)
Paul Jackson029190c2007-10-18 23:40:20 -07008502{
Max Krasnyanskydfb512e2008-08-29 13:11:41 -07008503 int i, j, n;
Heiko Carstensd65bd5e2008-12-09 18:49:51 +01008504 int new_topology;
Paul Jackson029190c2007-10-18 23:40:20 -07008505
Heiko Carstens712555e2008-04-28 11:33:07 +02008506 mutex_lock(&sched_domains_mutex);
Srivatsa Vaddagiria1835612008-01-25 21:08:00 +01008507
Milton Miller73785472007-10-24 18:23:48 +02008508 /* always unregister in case we don't destroy any domains */
8509 unregister_sched_domain_sysctl();
8510
Heiko Carstensd65bd5e2008-12-09 18:49:51 +01008511 /* Let architecture update cpu core mappings. */
8512 new_topology = arch_update_cpu_topology();
8513
Max Krasnyanskydfb512e2008-08-29 13:11:41 -07008514 n = doms_new ? ndoms_new : 0;
Paul Jackson029190c2007-10-18 23:40:20 -07008515
8516 /* Destroy deleted domains */
8517 for (i = 0; i < ndoms_cur; i++) {
Heiko Carstensd65bd5e2008-12-09 18:49:51 +01008518 for (j = 0; j < n && !new_topology; j++) {
Rusty Russell96f874e2008-11-25 02:35:14 +10308519 if (cpumask_equal(&doms_cur[i], &doms_new[j])
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09008520 && dattrs_equal(dattr_cur, i, dattr_new, j))
Paul Jackson029190c2007-10-18 23:40:20 -07008521 goto match1;
8522 }
8523 /* no match - a current sched domain not in new doms_new[] */
8524 detach_destroy_domains(doms_cur + i);
8525match1:
8526 ;
8527 }
8528
Max Krasnyanskye761b772008-07-15 04:43:49 -07008529 if (doms_new == NULL) {
8530 ndoms_cur = 0;
Rusty Russell42128232008-11-25 02:35:12 +10308531 doms_new = fallback_doms;
Rusty Russelldcc30a32008-11-25 02:35:12 +10308532 cpumask_andnot(&doms_new[0], cpu_online_mask, cpu_isolated_map);
Li Zefanfaa2f982008-11-04 16:20:23 +08008533 WARN_ON_ONCE(dattr_new);
Max Krasnyanskye761b772008-07-15 04:43:49 -07008534 }
8535
Paul Jackson029190c2007-10-18 23:40:20 -07008536 /* Build new domains */
8537 for (i = 0; i < ndoms_new; i++) {
Heiko Carstensd65bd5e2008-12-09 18:49:51 +01008538 for (j = 0; j < ndoms_cur && !new_topology; j++) {
Rusty Russell96f874e2008-11-25 02:35:14 +10308539 if (cpumask_equal(&doms_new[i], &doms_cur[j])
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09008540 && dattrs_equal(dattr_new, i, dattr_cur, j))
Paul Jackson029190c2007-10-18 23:40:20 -07008541 goto match2;
8542 }
8543 /* no match - add a new doms_new */
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09008544 __build_sched_domains(doms_new + i,
8545 dattr_new ? dattr_new + i : NULL);
Paul Jackson029190c2007-10-18 23:40:20 -07008546match2:
8547 ;
8548 }
8549
8550 /* Remember the new sched domains */
Rusty Russell42128232008-11-25 02:35:12 +10308551 if (doms_cur != fallback_doms)
Paul Jackson029190c2007-10-18 23:40:20 -07008552 kfree(doms_cur);
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09008553 kfree(dattr_cur); /* kfree(NULL) is safe */
Paul Jackson029190c2007-10-18 23:40:20 -07008554 doms_cur = doms_new;
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09008555 dattr_cur = dattr_new;
Paul Jackson029190c2007-10-18 23:40:20 -07008556 ndoms_cur = ndoms_new;
Milton Miller73785472007-10-24 18:23:48 +02008557
8558 register_sched_domain_sysctl();
Srivatsa Vaddagiria1835612008-01-25 21:08:00 +01008559
Heiko Carstens712555e2008-04-28 11:33:07 +02008560 mutex_unlock(&sched_domains_mutex);
Paul Jackson029190c2007-10-18 23:40:20 -07008561}
8562
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07008563#if defined(CONFIG_SCHED_MC) || defined(CONFIG_SCHED_SMT)
Li Zefanc70f22d2009-01-05 19:07:50 +08008564static void arch_reinit_sched_domains(void)
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07008565{
Gautham R Shenoy95402b32008-01-25 21:08:02 +01008566 get_online_cpus();
Max Krasnyanskydfb512e2008-08-29 13:11:41 -07008567
8568 /* Destroy domains first to force the rebuild */
8569 partition_sched_domains(0, NULL, NULL);
8570
Max Krasnyanskye761b772008-07-15 04:43:49 -07008571 rebuild_sched_domains();
Gautham R Shenoy95402b32008-01-25 21:08:02 +01008572 put_online_cpus();
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07008573}
8574
8575static ssize_t sched_power_savings_store(const char *buf, size_t count, int smt)
8576{
Gautham R Shenoyafb8a9b2008-12-18 23:26:09 +05308577 unsigned int level = 0;
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07008578
Gautham R Shenoyafb8a9b2008-12-18 23:26:09 +05308579 if (sscanf(buf, "%u", &level) != 1)
8580 return -EINVAL;
8581
8582 /*
8583 * level is always be positive so don't check for
8584 * level < POWERSAVINGS_BALANCE_NONE which is 0
8585 * What happens on 0 or 1 byte write,
8586 * need to check for count as well?
8587 */
8588
8589 if (level >= MAX_POWERSAVINGS_BALANCE_LEVELS)
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07008590 return -EINVAL;
8591
8592 if (smt)
Gautham R Shenoyafb8a9b2008-12-18 23:26:09 +05308593 sched_smt_power_savings = level;
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07008594 else
Gautham R Shenoyafb8a9b2008-12-18 23:26:09 +05308595 sched_mc_power_savings = level;
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07008596
Li Zefanc70f22d2009-01-05 19:07:50 +08008597 arch_reinit_sched_domains();
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07008598
Li Zefanc70f22d2009-01-05 19:07:50 +08008599 return count;
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07008600}
8601
Adrian Bunk6707de002007-08-12 18:08:19 +02008602#ifdef CONFIG_SCHED_MC
Andi Kleenf718cd42008-07-29 22:33:52 -07008603static ssize_t sched_mc_power_savings_show(struct sysdev_class *class,
8604 char *page)
Adrian Bunk6707de002007-08-12 18:08:19 +02008605{
8606 return sprintf(page, "%u\n", sched_mc_power_savings);
8607}
Andi Kleenf718cd42008-07-29 22:33:52 -07008608static ssize_t sched_mc_power_savings_store(struct sysdev_class *class,
Adrian Bunk6707de002007-08-12 18:08:19 +02008609 const char *buf, size_t count)
8610{
8611 return sched_power_savings_store(buf, count, 0);
8612}
Andi Kleenf718cd42008-07-29 22:33:52 -07008613static SYSDEV_CLASS_ATTR(sched_mc_power_savings, 0644,
8614 sched_mc_power_savings_show,
8615 sched_mc_power_savings_store);
Adrian Bunk6707de002007-08-12 18:08:19 +02008616#endif
8617
8618#ifdef CONFIG_SCHED_SMT
Andi Kleenf718cd42008-07-29 22:33:52 -07008619static ssize_t sched_smt_power_savings_show(struct sysdev_class *dev,
8620 char *page)
Adrian Bunk6707de002007-08-12 18:08:19 +02008621{
8622 return sprintf(page, "%u\n", sched_smt_power_savings);
8623}
Andi Kleenf718cd42008-07-29 22:33:52 -07008624static ssize_t sched_smt_power_savings_store(struct sysdev_class *dev,
Adrian Bunk6707de002007-08-12 18:08:19 +02008625 const char *buf, size_t count)
8626{
8627 return sched_power_savings_store(buf, count, 1);
8628}
Andi Kleenf718cd42008-07-29 22:33:52 -07008629static SYSDEV_CLASS_ATTR(sched_smt_power_savings, 0644,
8630 sched_smt_power_savings_show,
Adrian Bunk6707de002007-08-12 18:08:19 +02008631 sched_smt_power_savings_store);
8632#endif
8633
Li Zefan39aac642009-01-05 19:18:02 +08008634int __init sched_create_sysfs_power_savings_entries(struct sysdev_class *cls)
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07008635{
8636 int err = 0;
Ingo Molnar48f24c42006-07-03 00:25:40 -07008637
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07008638#ifdef CONFIG_SCHED_SMT
8639 if (smt_capable())
8640 err = sysfs_create_file(&cls->kset.kobj,
8641 &attr_sched_smt_power_savings.attr);
8642#endif
8643#ifdef CONFIG_SCHED_MC
8644 if (!err && mc_capable())
8645 err = sysfs_create_file(&cls->kset.kobj,
8646 &attr_sched_mc_power_savings.attr);
8647#endif
8648 return err;
8649}
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +02008650#endif /* CONFIG_SCHED_MC || CONFIG_SCHED_SMT */
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07008651
Max Krasnyanskye761b772008-07-15 04:43:49 -07008652#ifndef CONFIG_CPUSETS
Linus Torvalds1da177e2005-04-16 15:20:36 -07008653/*
Max Krasnyanskye761b772008-07-15 04:43:49 -07008654 * Add online and remove offline CPUs from the scheduler domains.
8655 * When cpusets are enabled they take over this function.
Linus Torvalds1da177e2005-04-16 15:20:36 -07008656 */
8657static int update_sched_domains(struct notifier_block *nfb,
8658 unsigned long action, void *hcpu)
8659{
Max Krasnyanskye761b772008-07-15 04:43:49 -07008660 switch (action) {
8661 case CPU_ONLINE:
8662 case CPU_ONLINE_FROZEN:
8663 case CPU_DEAD:
8664 case CPU_DEAD_FROZEN:
Max Krasnyanskydfb512e2008-08-29 13:11:41 -07008665 partition_sched_domains(1, NULL, NULL);
Max Krasnyanskye761b772008-07-15 04:43:49 -07008666 return NOTIFY_OK;
8667
8668 default:
8669 return NOTIFY_DONE;
8670 }
8671}
8672#endif
8673
8674static int update_runtime(struct notifier_block *nfb,
8675 unsigned long action, void *hcpu)
8676{
Peter Zijlstra7def2be2008-06-05 14:49:58 +02008677 int cpu = (int)(long)hcpu;
8678
Linus Torvalds1da177e2005-04-16 15:20:36 -07008679 switch (action) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008680 case CPU_DOWN_PREPARE:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07008681 case CPU_DOWN_PREPARE_FROZEN:
Peter Zijlstra7def2be2008-06-05 14:49:58 +02008682 disable_runtime(cpu_rq(cpu));
Linus Torvalds1da177e2005-04-16 15:20:36 -07008683 return NOTIFY_OK;
8684
Linus Torvalds1da177e2005-04-16 15:20:36 -07008685 case CPU_DOWN_FAILED:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07008686 case CPU_DOWN_FAILED_FROZEN:
Linus Torvalds1da177e2005-04-16 15:20:36 -07008687 case CPU_ONLINE:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07008688 case CPU_ONLINE_FROZEN:
Peter Zijlstra7def2be2008-06-05 14:49:58 +02008689 enable_runtime(cpu_rq(cpu));
Max Krasnyanskye761b772008-07-15 04:43:49 -07008690 return NOTIFY_OK;
8691
Linus Torvalds1da177e2005-04-16 15:20:36 -07008692 default:
8693 return NOTIFY_DONE;
8694 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008695}
Linus Torvalds1da177e2005-04-16 15:20:36 -07008696
8697void __init sched_init_smp(void)
8698{
Rusty Russelldcc30a32008-11-25 02:35:12 +10308699 cpumask_var_t non_isolated_cpus;
8700
8701 alloc_cpumask_var(&non_isolated_cpus, GFP_KERNEL);
Nick Piggin5c1e1762006-10-03 01:14:04 -07008702
Mike Travis434d53b2008-04-04 18:11:04 -07008703#if defined(CONFIG_NUMA)
8704 sched_group_nodes_bycpu = kzalloc(nr_cpu_ids * sizeof(void **),
8705 GFP_KERNEL);
8706 BUG_ON(sched_group_nodes_bycpu == NULL);
8707#endif
Gautham R Shenoy95402b32008-01-25 21:08:02 +01008708 get_online_cpus();
Heiko Carstens712555e2008-04-28 11:33:07 +02008709 mutex_lock(&sched_domains_mutex);
Rusty Russelldcc30a32008-11-25 02:35:12 +10308710 arch_init_sched_domains(cpu_online_mask);
8711 cpumask_andnot(non_isolated_cpus, cpu_possible_mask, cpu_isolated_map);
8712 if (cpumask_empty(non_isolated_cpus))
8713 cpumask_set_cpu(smp_processor_id(), non_isolated_cpus);
Heiko Carstens712555e2008-04-28 11:33:07 +02008714 mutex_unlock(&sched_domains_mutex);
Gautham R Shenoy95402b32008-01-25 21:08:02 +01008715 put_online_cpus();
Max Krasnyanskye761b772008-07-15 04:43:49 -07008716
8717#ifndef CONFIG_CPUSETS
Linus Torvalds1da177e2005-04-16 15:20:36 -07008718 /* XXX: Theoretical race here - CPU may be hotplugged now */
8719 hotcpu_notifier(update_sched_domains, 0);
Max Krasnyanskye761b772008-07-15 04:43:49 -07008720#endif
8721
8722 /* RT runtime code needs to handle some hotplug events */
8723 hotcpu_notifier(update_runtime, 0);
8724
Peter Zijlstrab328ca12008-04-29 10:02:46 +02008725 init_hrtick();
Nick Piggin5c1e1762006-10-03 01:14:04 -07008726
8727 /* Move init over to a non-isolated CPU */
Rusty Russelldcc30a32008-11-25 02:35:12 +10308728 if (set_cpus_allowed_ptr(current, non_isolated_cpus) < 0)
Nick Piggin5c1e1762006-10-03 01:14:04 -07008729 BUG();
Ingo Molnar19978ca2007-11-09 22:39:38 +01008730 sched_init_granularity();
Rusty Russelldcc30a32008-11-25 02:35:12 +10308731 free_cpumask_var(non_isolated_cpus);
Rusty Russell42128232008-11-25 02:35:12 +10308732
8733 alloc_cpumask_var(&fallback_doms, GFP_KERNEL);
Rusty Russell0e3900e2008-11-25 02:35:13 +10308734 init_sched_rt_class();
Linus Torvalds1da177e2005-04-16 15:20:36 -07008735}
8736#else
8737void __init sched_init_smp(void)
8738{
Ingo Molnar19978ca2007-11-09 22:39:38 +01008739 sched_init_granularity();
Linus Torvalds1da177e2005-04-16 15:20:36 -07008740}
8741#endif /* CONFIG_SMP */
8742
8743int in_sched_functions(unsigned long addr)
8744{
Linus Torvalds1da177e2005-04-16 15:20:36 -07008745 return in_lock_functions(addr) ||
8746 (addr >= (unsigned long)__sched_text_start
8747 && addr < (unsigned long)__sched_text_end);
8748}
8749
Alexey Dobriyana9957442007-10-15 17:00:13 +02008750static void init_cfs_rq(struct cfs_rq *cfs_rq, struct rq *rq)
Ingo Molnardd41f592007-07-09 18:51:59 +02008751{
8752 cfs_rq->tasks_timeline = RB_ROOT;
Peter Zijlstra4a55bd52008-04-19 19:45:00 +02008753 INIT_LIST_HEAD(&cfs_rq->tasks);
Ingo Molnardd41f592007-07-09 18:51:59 +02008754#ifdef CONFIG_FAIR_GROUP_SCHED
8755 cfs_rq->rq = rq;
8756#endif
Peter Zijlstra67e9fb22007-10-15 17:00:10 +02008757 cfs_rq->min_vruntime = (u64)(-(1LL << 20));
Ingo Molnardd41f592007-07-09 18:51:59 +02008758}
8759
Peter Zijlstrafa85ae22008-01-25 21:08:29 +01008760static void init_rt_rq(struct rt_rq *rt_rq, struct rq *rq)
8761{
8762 struct rt_prio_array *array;
8763 int i;
8764
8765 array = &rt_rq->active;
8766 for (i = 0; i < MAX_RT_PRIO; i++) {
8767 INIT_LIST_HEAD(array->queue + i);
8768 __clear_bit(i, array->bitmap);
8769 }
8770 /* delimiter for bitsearch: */
8771 __set_bit(MAX_RT_PRIO, array->bitmap);
8772
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01008773#if defined CONFIG_SMP || defined CONFIG_RT_GROUP_SCHED
Gregory Haskinse864c492008-12-29 09:39:49 -05008774 rt_rq->highest_prio.curr = MAX_RT_PRIO;
Gregory Haskins398a1532009-01-14 09:10:04 -05008775#ifdef CONFIG_SMP
Gregory Haskinse864c492008-12-29 09:39:49 -05008776 rt_rq->highest_prio.next = MAX_RT_PRIO;
Peter Zijlstra48d5e252008-01-25 21:08:31 +01008777#endif
Peter Zijlstrafa85ae22008-01-25 21:08:29 +01008778#endif
8779#ifdef CONFIG_SMP
8780 rt_rq->rt_nr_migratory = 0;
Peter Zijlstrafa85ae22008-01-25 21:08:29 +01008781 rt_rq->overloaded = 0;
Gregory Haskins917b6272008-12-29 09:39:53 -05008782 plist_head_init(&rq->rt.pushable_tasks, &rq->lock);
Peter Zijlstrafa85ae22008-01-25 21:08:29 +01008783#endif
8784
8785 rt_rq->rt_time = 0;
8786 rt_rq->rt_throttled = 0;
Peter Zijlstraac086bc2008-04-19 19:44:58 +02008787 rt_rq->rt_runtime = 0;
8788 spin_lock_init(&rt_rq->rt_runtime_lock);
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008789
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01008790#ifdef CONFIG_RT_GROUP_SCHED
Peter Zijlstra23b0fdf2008-02-13 15:45:39 +01008791 rt_rq->rt_nr_boosted = 0;
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008792 rt_rq->rq = rq;
8793#endif
Peter Zijlstrafa85ae22008-01-25 21:08:29 +01008794}
8795
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008796#ifdef CONFIG_FAIR_GROUP_SCHED
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008797static void init_tg_cfs_entry(struct task_group *tg, struct cfs_rq *cfs_rq,
8798 struct sched_entity *se, int cpu, int add,
8799 struct sched_entity *parent)
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008800{
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008801 struct rq *rq = cpu_rq(cpu);
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008802 tg->cfs_rq[cpu] = cfs_rq;
8803 init_cfs_rq(cfs_rq, rq);
8804 cfs_rq->tg = tg;
8805 if (add)
8806 list_add(&cfs_rq->leaf_cfs_rq_list, &rq->leaf_cfs_rq_list);
8807
8808 tg->se[cpu] = se;
Dhaval Giani354d60c2008-04-19 19:44:59 +02008809 /* se could be NULL for init_task_group */
8810 if (!se)
8811 return;
8812
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008813 if (!parent)
8814 se->cfs_rq = &rq->cfs;
8815 else
8816 se->cfs_rq = parent->my_q;
8817
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008818 se->my_q = cfs_rq;
8819 se->load.weight = tg->shares;
Peter Zijlstrae05510d2008-05-05 23:56:17 +02008820 se->load.inv_weight = 0;
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008821 se->parent = parent;
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008822}
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01008823#endif
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008824
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01008825#ifdef CONFIG_RT_GROUP_SCHED
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008826static void init_tg_rt_entry(struct task_group *tg, struct rt_rq *rt_rq,
8827 struct sched_rt_entity *rt_se, int cpu, int add,
8828 struct sched_rt_entity *parent)
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008829{
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008830 struct rq *rq = cpu_rq(cpu);
8831
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008832 tg->rt_rq[cpu] = rt_rq;
8833 init_rt_rq(rt_rq, rq);
8834 rt_rq->tg = tg;
8835 rt_rq->rt_se = rt_se;
Peter Zijlstraac086bc2008-04-19 19:44:58 +02008836 rt_rq->rt_runtime = tg->rt_bandwidth.rt_runtime;
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008837 if (add)
8838 list_add(&rt_rq->leaf_rt_rq_list, &rq->leaf_rt_rq_list);
8839
8840 tg->rt_se[cpu] = rt_se;
Dhaval Giani354d60c2008-04-19 19:44:59 +02008841 if (!rt_se)
8842 return;
8843
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008844 if (!parent)
8845 rt_se->rt_rq = &rq->rt;
8846 else
8847 rt_se->rt_rq = parent->my_q;
8848
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008849 rt_se->my_q = rt_rq;
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008850 rt_se->parent = parent;
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008851 INIT_LIST_HEAD(&rt_se->run_list);
8852}
8853#endif
8854
Linus Torvalds1da177e2005-04-16 15:20:36 -07008855void __init sched_init(void)
8856{
Ingo Molnardd41f592007-07-09 18:51:59 +02008857 int i, j;
Mike Travis434d53b2008-04-04 18:11:04 -07008858 unsigned long alloc_size = 0, ptr;
8859
8860#ifdef CONFIG_FAIR_GROUP_SCHED
8861 alloc_size += 2 * nr_cpu_ids * sizeof(void **);
8862#endif
8863#ifdef CONFIG_RT_GROUP_SCHED
8864 alloc_size += 2 * nr_cpu_ids * sizeof(void **);
8865#endif
Peter Zijlstraeff766a2008-04-19 19:45:00 +02008866#ifdef CONFIG_USER_SCHED
8867 alloc_size *= 2;
8868#endif
Rusty Russelldf7c8e82009-03-19 15:22:20 +10308869#ifdef CONFIG_CPUMASK_OFFSTACK
Rusty Russell8c083f02009-03-19 15:22:20 +10308870 alloc_size += num_possible_cpus() * cpumask_size();
Rusty Russelldf7c8e82009-03-19 15:22:20 +10308871#endif
Mike Travis434d53b2008-04-04 18:11:04 -07008872 /*
8873 * As sched_init() is called before page_alloc is setup,
8874 * we use alloc_bootmem().
8875 */
8876 if (alloc_size) {
David Miller5a9d3222008-04-24 20:46:20 -07008877 ptr = (unsigned long)alloc_bootmem(alloc_size);
Mike Travis434d53b2008-04-04 18:11:04 -07008878
8879#ifdef CONFIG_FAIR_GROUP_SCHED
8880 init_task_group.se = (struct sched_entity **)ptr;
8881 ptr += nr_cpu_ids * sizeof(void **);
8882
8883 init_task_group.cfs_rq = (struct cfs_rq **)ptr;
8884 ptr += nr_cpu_ids * sizeof(void **);
Peter Zijlstraeff766a2008-04-19 19:45:00 +02008885
8886#ifdef CONFIG_USER_SCHED
8887 root_task_group.se = (struct sched_entity **)ptr;
8888 ptr += nr_cpu_ids * sizeof(void **);
8889
8890 root_task_group.cfs_rq = (struct cfs_rq **)ptr;
8891 ptr += nr_cpu_ids * sizeof(void **);
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +02008892#endif /* CONFIG_USER_SCHED */
8893#endif /* CONFIG_FAIR_GROUP_SCHED */
Mike Travis434d53b2008-04-04 18:11:04 -07008894#ifdef CONFIG_RT_GROUP_SCHED
8895 init_task_group.rt_se = (struct sched_rt_entity **)ptr;
8896 ptr += nr_cpu_ids * sizeof(void **);
8897
8898 init_task_group.rt_rq = (struct rt_rq **)ptr;
Peter Zijlstraeff766a2008-04-19 19:45:00 +02008899 ptr += nr_cpu_ids * sizeof(void **);
8900
8901#ifdef CONFIG_USER_SCHED
8902 root_task_group.rt_se = (struct sched_rt_entity **)ptr;
8903 ptr += nr_cpu_ids * sizeof(void **);
8904
8905 root_task_group.rt_rq = (struct rt_rq **)ptr;
8906 ptr += nr_cpu_ids * sizeof(void **);
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +02008907#endif /* CONFIG_USER_SCHED */
8908#endif /* CONFIG_RT_GROUP_SCHED */
Rusty Russelldf7c8e82009-03-19 15:22:20 +10308909#ifdef CONFIG_CPUMASK_OFFSTACK
8910 for_each_possible_cpu(i) {
8911 per_cpu(load_balance_tmpmask, i) = (void *)ptr;
8912 ptr += cpumask_size();
8913 }
8914#endif /* CONFIG_CPUMASK_OFFSTACK */
Mike Travis434d53b2008-04-04 18:11:04 -07008915 }
Ingo Molnardd41f592007-07-09 18:51:59 +02008916
Gregory Haskins57d885f2008-01-25 21:08:18 +01008917#ifdef CONFIG_SMP
8918 init_defrootdomain();
8919#endif
8920
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008921 init_rt_bandwidth(&def_rt_bandwidth,
8922 global_rt_period(), global_rt_runtime());
8923
8924#ifdef CONFIG_RT_GROUP_SCHED
8925 init_rt_bandwidth(&init_task_group.rt_bandwidth,
8926 global_rt_period(), global_rt_runtime());
Peter Zijlstraeff766a2008-04-19 19:45:00 +02008927#ifdef CONFIG_USER_SCHED
8928 init_rt_bandwidth(&root_task_group.rt_bandwidth,
8929 global_rt_period(), RUNTIME_INF);
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +02008930#endif /* CONFIG_USER_SCHED */
8931#endif /* CONFIG_RT_GROUP_SCHED */
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008932
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01008933#ifdef CONFIG_GROUP_SCHED
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008934 list_add(&init_task_group.list, &task_groups);
Peter Zijlstraf473aa52008-04-19 19:45:00 +02008935 INIT_LIST_HEAD(&init_task_group.children);
8936
8937#ifdef CONFIG_USER_SCHED
8938 INIT_LIST_HEAD(&root_task_group.children);
8939 init_task_group.parent = &root_task_group;
8940 list_add(&init_task_group.siblings, &root_task_group.children);
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +02008941#endif /* CONFIG_USER_SCHED */
8942#endif /* CONFIG_GROUP_SCHED */
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008943
KAMEZAWA Hiroyuki0a945022006-03-28 01:56:37 -08008944 for_each_possible_cpu(i) {
Ingo Molnar70b97a72006-07-03 00:25:42 -07008945 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008946
8947 rq = cpu_rq(i);
8948 spin_lock_init(&rq->lock);
Nick Piggin78979862005-06-25 14:57:13 -07008949 rq->nr_running = 0;
Ingo Molnardd41f592007-07-09 18:51:59 +02008950 init_cfs_rq(&rq->cfs, rq);
Peter Zijlstrafa85ae22008-01-25 21:08:29 +01008951 init_rt_rq(&rq->rt, rq);
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008952#ifdef CONFIG_FAIR_GROUP_SCHED
8953 init_task_group.shares = init_task_group_load;
8954 INIT_LIST_HEAD(&rq->leaf_cfs_rq_list);
Dhaval Giani354d60c2008-04-19 19:44:59 +02008955#ifdef CONFIG_CGROUP_SCHED
8956 /*
8957 * How much cpu bandwidth does init_task_group get?
8958 *
8959 * In case of task-groups formed thr' the cgroup filesystem, it
8960 * gets 100% of the cpu resources in the system. This overall
8961 * system cpu resource is divided among the tasks of
8962 * init_task_group and its child task-groups in a fair manner,
8963 * based on each entity's (task or task-group's) weight
8964 * (se->load.weight).
8965 *
8966 * In other words, if init_task_group has 10 tasks of weight
8967 * 1024) and two child groups A0 and A1 (of weight 1024 each),
8968 * then A0's share of the cpu resource is:
8969 *
8970 * A0's bandwidth = 1024 / (10*1024 + 1024 + 1024) = 8.33%
8971 *
8972 * We achieve this by letting init_task_group's tasks sit
8973 * directly in rq->cfs (i.e init_task_group->se[] = NULL).
8974 */
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008975 init_tg_cfs_entry(&init_task_group, &rq->cfs, NULL, i, 1, NULL);
Dhaval Giani354d60c2008-04-19 19:44:59 +02008976#elif defined CONFIG_USER_SCHED
Peter Zijlstraeff766a2008-04-19 19:45:00 +02008977 root_task_group.shares = NICE_0_LOAD;
8978 init_tg_cfs_entry(&root_task_group, &rq->cfs, NULL, i, 0, NULL);
Dhaval Giani354d60c2008-04-19 19:44:59 +02008979 /*
8980 * In case of task-groups formed thr' the user id of tasks,
8981 * init_task_group represents tasks belonging to root user.
8982 * Hence it forms a sibling of all subsequent groups formed.
8983 * In this case, init_task_group gets only a fraction of overall
8984 * system cpu resource, based on the weight assigned to root
8985 * user's cpu share (INIT_TASK_GROUP_LOAD). This is accomplished
8986 * by letting tasks of init_task_group sit in a separate cfs_rq
8987 * (init_cfs_rq) and having one entity represent this group of
8988 * tasks in rq->cfs (i.e init_task_group->se[] != NULL).
8989 */
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008990 init_tg_cfs_entry(&init_task_group,
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008991 &per_cpu(init_cfs_rq, i),
Peter Zijlstraeff766a2008-04-19 19:45:00 +02008992 &per_cpu(init_sched_entity, i), i, 1,
8993 root_task_group.se[i]);
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008994
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01008995#endif
Dhaval Giani354d60c2008-04-19 19:44:59 +02008996#endif /* CONFIG_FAIR_GROUP_SCHED */
8997
8998 rq->rt.rt_runtime = def_rt_bandwidth.rt_runtime;
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01008999#ifdef CONFIG_RT_GROUP_SCHED
Peter Zijlstra6f505b12008-01-25 21:08:30 +01009000 INIT_LIST_HEAD(&rq->leaf_rt_rq_list);
Dhaval Giani354d60c2008-04-19 19:44:59 +02009001#ifdef CONFIG_CGROUP_SCHED
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02009002 init_tg_rt_entry(&init_task_group, &rq->rt, NULL, i, 1, NULL);
Dhaval Giani354d60c2008-04-19 19:44:59 +02009003#elif defined CONFIG_USER_SCHED
Peter Zijlstraeff766a2008-04-19 19:45:00 +02009004 init_tg_rt_entry(&root_task_group, &rq->rt, NULL, i, 0, NULL);
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02009005 init_tg_rt_entry(&init_task_group,
Peter Zijlstra6f505b12008-01-25 21:08:30 +01009006 &per_cpu(init_rt_rq, i),
Peter Zijlstraeff766a2008-04-19 19:45:00 +02009007 &per_cpu(init_sched_rt_entity, i), i, 1,
9008 root_task_group.rt_se[i]);
Dhaval Giani354d60c2008-04-19 19:44:59 +02009009#endif
Peter Zijlstra6f505b12008-01-25 21:08:30 +01009010#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07009011
Ingo Molnardd41f592007-07-09 18:51:59 +02009012 for (j = 0; j < CPU_LOAD_IDX_MAX; j++)
9013 rq->cpu_load[j] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009014#ifdef CONFIG_SMP
Nick Piggin41c7ce92005-06-25 14:57:24 -07009015 rq->sd = NULL;
Gregory Haskins57d885f2008-01-25 21:08:18 +01009016 rq->rd = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009017 rq->active_balance = 0;
Ingo Molnardd41f592007-07-09 18:51:59 +02009018 rq->next_balance = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009019 rq->push_cpu = 0;
Christoph Lameter0a2966b2006-09-25 23:30:51 -07009020 rq->cpu = i;
Gregory Haskins1f11eb62008-06-04 15:04:05 -04009021 rq->online = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009022 rq->migration_thread = NULL;
9023 INIT_LIST_HEAD(&rq->migration_queue);
Gregory Haskinsdc938522008-01-25 21:08:26 +01009024 rq_attach_root(rq, &def_root_domain);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009025#endif
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01009026 init_rq_hrtick(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009027 atomic_set(&rq->nr_iowait, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009028 }
9029
Peter Williams2dd73a42006-06-27 02:54:34 -07009030 set_load_weight(&init_task);
Heiko Carstensb50f60c2006-07-30 03:03:52 -07009031
Avi Kivitye107be32007-07-26 13:40:43 +02009032#ifdef CONFIG_PREEMPT_NOTIFIERS
9033 INIT_HLIST_HEAD(&init_task.preempt_notifiers);
9034#endif
9035
Christoph Lameterc9819f42006-12-10 02:20:25 -08009036#ifdef CONFIG_SMP
Carlos R. Mafra962cf362008-05-15 11:15:37 -03009037 open_softirq(SCHED_SOFTIRQ, run_rebalance_domains);
Christoph Lameterc9819f42006-12-10 02:20:25 -08009038#endif
9039
Heiko Carstensb50f60c2006-07-30 03:03:52 -07009040#ifdef CONFIG_RT_MUTEXES
9041 plist_head_init(&init_task.pi_waiters, &init_task.pi_lock);
9042#endif
9043
Linus Torvalds1da177e2005-04-16 15:20:36 -07009044 /*
9045 * The boot idle thread does lazy MMU switching as well:
9046 */
9047 atomic_inc(&init_mm.mm_count);
9048 enter_lazy_tlb(&init_mm, current);
9049
9050 /*
9051 * Make us the idle thread. Technically, schedule() should not be
9052 * called from this thread, however somewhere below it might be,
9053 * but because we are the idle thread, we just pick up running again
9054 * when this runqueue becomes "idle".
9055 */
9056 init_idle(current, smp_processor_id());
Ingo Molnardd41f592007-07-09 18:51:59 +02009057 /*
9058 * During early bootup we pretend to be a normal task:
9059 */
9060 current->sched_class = &fair_sched_class;
Ingo Molnar6892b752008-02-13 14:02:36 +01009061
Rusty Russell6a7b3dc2008-11-25 02:35:04 +10309062 /* Allocate the nohz_cpu_mask if CONFIG_CPUMASK_OFFSTACK */
9063 alloc_bootmem_cpumask_var(&nohz_cpu_mask);
Rusty Russellbf4d83f2008-11-25 09:57:51 +10309064#ifdef CONFIG_SMP
Rusty Russell7d1e6a92008-11-25 02:35:09 +10309065#ifdef CONFIG_NO_HZ
9066 alloc_bootmem_cpumask_var(&nohz.cpu_mask);
9067#endif
Rusty Russelldcc30a32008-11-25 02:35:12 +10309068 alloc_bootmem_cpumask_var(&cpu_isolated_map);
Rusty Russellbf4d83f2008-11-25 09:57:51 +10309069#endif /* SMP */
Rusty Russell6a7b3dc2008-11-25 02:35:04 +10309070
Ingo Molnar6892b752008-02-13 14:02:36 +01009071 scheduler_running = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009072}
9073
9074#ifdef CONFIG_DEBUG_SPINLOCK_SLEEP
9075void __might_sleep(char *file, int line)
9076{
Ingo Molnar48f24c42006-07-03 00:25:40 -07009077#ifdef in_atomic
Linus Torvalds1da177e2005-04-16 15:20:36 -07009078 static unsigned long prev_jiffy; /* ratelimiting */
9079
Ingo Molnaraef745f2008-08-28 11:34:43 +02009080 if ((!in_atomic() && !irqs_disabled()) ||
9081 system_state != SYSTEM_RUNNING || oops_in_progress)
9082 return;
9083 if (time_before(jiffies, prev_jiffy + HZ) && prev_jiffy)
9084 return;
9085 prev_jiffy = jiffies;
9086
9087 printk(KERN_ERR
9088 "BUG: sleeping function called from invalid context at %s:%d\n",
9089 file, line);
9090 printk(KERN_ERR
9091 "in_atomic(): %d, irqs_disabled(): %d, pid: %d, name: %s\n",
9092 in_atomic(), irqs_disabled(),
9093 current->pid, current->comm);
9094
9095 debug_show_held_locks(current);
9096 if (irqs_disabled())
9097 print_irqtrace_events(current);
9098 dump_stack();
Linus Torvalds1da177e2005-04-16 15:20:36 -07009099#endif
9100}
9101EXPORT_SYMBOL(__might_sleep);
9102#endif
9103
9104#ifdef CONFIG_MAGIC_SYSRQ
Andi Kleen3a5e4dc2007-10-15 17:00:15 +02009105static void normalize_task(struct rq *rq, struct task_struct *p)
9106{
9107 int on_rq;
Peter Zijlstra3e51f332008-05-03 18:29:28 +02009108
Andi Kleen3a5e4dc2007-10-15 17:00:15 +02009109 update_rq_clock(rq);
9110 on_rq = p->se.on_rq;
9111 if (on_rq)
9112 deactivate_task(rq, p, 0);
9113 __setscheduler(rq, p, SCHED_NORMAL, 0);
9114 if (on_rq) {
9115 activate_task(rq, p, 0);
9116 resched_task(rq->curr);
9117 }
9118}
9119
Linus Torvalds1da177e2005-04-16 15:20:36 -07009120void normalize_rt_tasks(void)
9121{
Ingo Molnara0f98a12007-06-17 18:37:45 +02009122 struct task_struct *g, *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009123 unsigned long flags;
Ingo Molnar70b97a72006-07-03 00:25:42 -07009124 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009125
Peter Zijlstra4cf5d772008-02-13 15:45:39 +01009126 read_lock_irqsave(&tasklist_lock, flags);
Ingo Molnara0f98a12007-06-17 18:37:45 +02009127 do_each_thread(g, p) {
Ingo Molnar178be792007-10-15 17:00:18 +02009128 /*
9129 * Only normalize user tasks:
9130 */
9131 if (!p->mm)
9132 continue;
9133
Ingo Molnardd41f592007-07-09 18:51:59 +02009134 p->se.exec_start = 0;
Ingo Molnar6cfb0d52007-08-02 17:41:40 +02009135#ifdef CONFIG_SCHEDSTATS
9136 p->se.wait_start = 0;
9137 p->se.sleep_start = 0;
Ingo Molnardd41f592007-07-09 18:51:59 +02009138 p->se.block_start = 0;
Ingo Molnar6cfb0d52007-08-02 17:41:40 +02009139#endif
Ingo Molnardd41f592007-07-09 18:51:59 +02009140
9141 if (!rt_task(p)) {
9142 /*
9143 * Renice negative nice level userspace
9144 * tasks back to 0:
9145 */
9146 if (TASK_NICE(p) < 0 && p->mm)
9147 set_user_nice(p, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009148 continue;
Ingo Molnardd41f592007-07-09 18:51:59 +02009149 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009150
Peter Zijlstra4cf5d772008-02-13 15:45:39 +01009151 spin_lock(&p->pi_lock);
Ingo Molnarb29739f2006-06-27 02:54:51 -07009152 rq = __task_rq_lock(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009153
Ingo Molnar178be792007-10-15 17:00:18 +02009154 normalize_task(rq, p);
Andi Kleen3a5e4dc2007-10-15 17:00:15 +02009155
Ingo Molnarb29739f2006-06-27 02:54:51 -07009156 __task_rq_unlock(rq);
Peter Zijlstra4cf5d772008-02-13 15:45:39 +01009157 spin_unlock(&p->pi_lock);
Ingo Molnara0f98a12007-06-17 18:37:45 +02009158 } while_each_thread(g, p);
9159
Peter Zijlstra4cf5d772008-02-13 15:45:39 +01009160 read_unlock_irqrestore(&tasklist_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009161}
9162
9163#endif /* CONFIG_MAGIC_SYSRQ */
Linus Torvalds1df5c102005-09-12 07:59:21 -07009164
9165#ifdef CONFIG_IA64
9166/*
9167 * These functions are only useful for the IA64 MCA handling.
9168 *
9169 * They can only be called when the whole system has been
9170 * stopped - every CPU needs to be quiescent, and no scheduling
9171 * activity can take place. Using them for anything else would
9172 * be a serious bug, and as a result, they aren't even visible
9173 * under any other configuration.
9174 */
9175
9176/**
9177 * curr_task - return the current task for a given cpu.
9178 * @cpu: the processor in question.
9179 *
9180 * ONLY VALID WHEN THE WHOLE SYSTEM IS STOPPED!
9181 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07009182struct task_struct *curr_task(int cpu)
Linus Torvalds1df5c102005-09-12 07:59:21 -07009183{
9184 return cpu_curr(cpu);
9185}
9186
9187/**
9188 * set_curr_task - set the current task for a given cpu.
9189 * @cpu: the processor in question.
9190 * @p: the task pointer to set.
9191 *
9192 * Description: This function must only be used when non-maskable interrupts
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01009193 * are serviced on a separate stack. It allows the architecture to switch the
9194 * notion of the current task on a cpu in a non-blocking manner. This function
Linus Torvalds1df5c102005-09-12 07:59:21 -07009195 * must be called with all CPU's synchronized, and interrupts disabled, the
9196 * and caller must save the original value of the current task (see
9197 * curr_task() above) and restore that value before reenabling interrupts and
9198 * re-starting the system.
9199 *
9200 * ONLY VALID WHEN THE WHOLE SYSTEM IS STOPPED!
9201 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07009202void set_curr_task(int cpu, struct task_struct *p)
Linus Torvalds1df5c102005-09-12 07:59:21 -07009203{
9204 cpu_curr(cpu) = p;
9205}
9206
9207#endif
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02009208
Peter Zijlstrabccbe082008-02-13 15:45:40 +01009209#ifdef CONFIG_FAIR_GROUP_SCHED
9210static void free_fair_sched_group(struct task_group *tg)
Peter Zijlstra6f505b12008-01-25 21:08:30 +01009211{
9212 int i;
9213
9214 for_each_possible_cpu(i) {
9215 if (tg->cfs_rq)
9216 kfree(tg->cfs_rq[i]);
9217 if (tg->se)
9218 kfree(tg->se[i]);
Peter Zijlstra6f505b12008-01-25 21:08:30 +01009219 }
9220
9221 kfree(tg->cfs_rq);
9222 kfree(tg->se);
Peter Zijlstra6f505b12008-01-25 21:08:30 +01009223}
9224
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02009225static
9226int alloc_fair_sched_group(struct task_group *tg, struct task_group *parent)
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02009227{
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02009228 struct cfs_rq *cfs_rq;
Li Zefaneab17222008-10-29 17:03:22 +08009229 struct sched_entity *se;
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02009230 struct rq *rq;
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02009231 int i;
9232
Mike Travis434d53b2008-04-04 18:11:04 -07009233 tg->cfs_rq = kzalloc(sizeof(cfs_rq) * nr_cpu_ids, GFP_KERNEL);
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02009234 if (!tg->cfs_rq)
9235 goto err;
Mike Travis434d53b2008-04-04 18:11:04 -07009236 tg->se = kzalloc(sizeof(se) * nr_cpu_ids, GFP_KERNEL);
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02009237 if (!tg->se)
9238 goto err;
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01009239
9240 tg->shares = NICE_0_LOAD;
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02009241
9242 for_each_possible_cpu(i) {
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02009243 rq = cpu_rq(i);
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02009244
Li Zefaneab17222008-10-29 17:03:22 +08009245 cfs_rq = kzalloc_node(sizeof(struct cfs_rq),
9246 GFP_KERNEL, cpu_to_node(i));
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02009247 if (!cfs_rq)
9248 goto err;
9249
Li Zefaneab17222008-10-29 17:03:22 +08009250 se = kzalloc_node(sizeof(struct sched_entity),
9251 GFP_KERNEL, cpu_to_node(i));
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02009252 if (!se)
9253 goto err;
9254
Li Zefaneab17222008-10-29 17:03:22 +08009255 init_tg_cfs_entry(tg, cfs_rq, se, i, 0, parent->se[i]);
Peter Zijlstrabccbe082008-02-13 15:45:40 +01009256 }
9257
9258 return 1;
9259
9260 err:
9261 return 0;
9262}
9263
9264static inline void register_fair_sched_group(struct task_group *tg, int cpu)
9265{
9266 list_add_rcu(&tg->cfs_rq[cpu]->leaf_cfs_rq_list,
9267 &cpu_rq(cpu)->leaf_cfs_rq_list);
9268}
9269
9270static inline void unregister_fair_sched_group(struct task_group *tg, int cpu)
9271{
9272 list_del_rcu(&tg->cfs_rq[cpu]->leaf_cfs_rq_list);
9273}
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +02009274#else /* !CONFG_FAIR_GROUP_SCHED */
Peter Zijlstrabccbe082008-02-13 15:45:40 +01009275static inline void free_fair_sched_group(struct task_group *tg)
9276{
9277}
9278
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02009279static inline
9280int alloc_fair_sched_group(struct task_group *tg, struct task_group *parent)
Peter Zijlstrabccbe082008-02-13 15:45:40 +01009281{
9282 return 1;
9283}
9284
9285static inline void register_fair_sched_group(struct task_group *tg, int cpu)
9286{
9287}
9288
9289static inline void unregister_fair_sched_group(struct task_group *tg, int cpu)
9290{
9291}
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +02009292#endif /* CONFIG_FAIR_GROUP_SCHED */
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01009293
9294#ifdef CONFIG_RT_GROUP_SCHED
Peter Zijlstrabccbe082008-02-13 15:45:40 +01009295static void free_rt_sched_group(struct task_group *tg)
9296{
9297 int i;
9298
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02009299 destroy_rt_bandwidth(&tg->rt_bandwidth);
9300
Peter Zijlstrabccbe082008-02-13 15:45:40 +01009301 for_each_possible_cpu(i) {
9302 if (tg->rt_rq)
9303 kfree(tg->rt_rq[i]);
9304 if (tg->rt_se)
9305 kfree(tg->rt_se[i]);
9306 }
9307
9308 kfree(tg->rt_rq);
9309 kfree(tg->rt_se);
9310}
9311
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02009312static
9313int alloc_rt_sched_group(struct task_group *tg, struct task_group *parent)
Peter Zijlstrabccbe082008-02-13 15:45:40 +01009314{
9315 struct rt_rq *rt_rq;
Li Zefaneab17222008-10-29 17:03:22 +08009316 struct sched_rt_entity *rt_se;
Peter Zijlstrabccbe082008-02-13 15:45:40 +01009317 struct rq *rq;
9318 int i;
9319
Mike Travis434d53b2008-04-04 18:11:04 -07009320 tg->rt_rq = kzalloc(sizeof(rt_rq) * nr_cpu_ids, GFP_KERNEL);
Peter Zijlstrabccbe082008-02-13 15:45:40 +01009321 if (!tg->rt_rq)
9322 goto err;
Mike Travis434d53b2008-04-04 18:11:04 -07009323 tg->rt_se = kzalloc(sizeof(rt_se) * nr_cpu_ids, GFP_KERNEL);
Peter Zijlstrabccbe082008-02-13 15:45:40 +01009324 if (!tg->rt_se)
9325 goto err;
9326
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02009327 init_rt_bandwidth(&tg->rt_bandwidth,
9328 ktime_to_ns(def_rt_bandwidth.rt_period), 0);
Peter Zijlstrabccbe082008-02-13 15:45:40 +01009329
9330 for_each_possible_cpu(i) {
9331 rq = cpu_rq(i);
9332
Li Zefaneab17222008-10-29 17:03:22 +08009333 rt_rq = kzalloc_node(sizeof(struct rt_rq),
9334 GFP_KERNEL, cpu_to_node(i));
Peter Zijlstra6f505b12008-01-25 21:08:30 +01009335 if (!rt_rq)
9336 goto err;
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02009337
Li Zefaneab17222008-10-29 17:03:22 +08009338 rt_se = kzalloc_node(sizeof(struct sched_rt_entity),
9339 GFP_KERNEL, cpu_to_node(i));
Peter Zijlstra6f505b12008-01-25 21:08:30 +01009340 if (!rt_se)
9341 goto err;
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02009342
Li Zefaneab17222008-10-29 17:03:22 +08009343 init_tg_rt_entry(tg, rt_rq, rt_se, i, 0, parent->rt_se[i]);
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02009344 }
9345
Peter Zijlstrabccbe082008-02-13 15:45:40 +01009346 return 1;
9347
9348 err:
9349 return 0;
9350}
9351
9352static inline void register_rt_sched_group(struct task_group *tg, int cpu)
9353{
9354 list_add_rcu(&tg->rt_rq[cpu]->leaf_rt_rq_list,
9355 &cpu_rq(cpu)->leaf_rt_rq_list);
9356}
9357
9358static inline void unregister_rt_sched_group(struct task_group *tg, int cpu)
9359{
9360 list_del_rcu(&tg->rt_rq[cpu]->leaf_rt_rq_list);
9361}
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +02009362#else /* !CONFIG_RT_GROUP_SCHED */
Peter Zijlstrabccbe082008-02-13 15:45:40 +01009363static inline void free_rt_sched_group(struct task_group *tg)
9364{
9365}
9366
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02009367static inline
9368int alloc_rt_sched_group(struct task_group *tg, struct task_group *parent)
Peter Zijlstrabccbe082008-02-13 15:45:40 +01009369{
9370 return 1;
9371}
9372
9373static inline void register_rt_sched_group(struct task_group *tg, int cpu)
9374{
9375}
9376
9377static inline void unregister_rt_sched_group(struct task_group *tg, int cpu)
9378{
9379}
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +02009380#endif /* CONFIG_RT_GROUP_SCHED */
Peter Zijlstrabccbe082008-02-13 15:45:40 +01009381
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02009382#ifdef CONFIG_GROUP_SCHED
Peter Zijlstrabccbe082008-02-13 15:45:40 +01009383static void free_sched_group(struct task_group *tg)
9384{
9385 free_fair_sched_group(tg);
9386 free_rt_sched_group(tg);
9387 kfree(tg);
9388}
9389
9390/* allocate runqueue etc for a new task group */
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02009391struct task_group *sched_create_group(struct task_group *parent)
Peter Zijlstrabccbe082008-02-13 15:45:40 +01009392{
9393 struct task_group *tg;
9394 unsigned long flags;
9395 int i;
9396
9397 tg = kzalloc(sizeof(*tg), GFP_KERNEL);
9398 if (!tg)
9399 return ERR_PTR(-ENOMEM);
9400
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02009401 if (!alloc_fair_sched_group(tg, parent))
Peter Zijlstrabccbe082008-02-13 15:45:40 +01009402 goto err;
9403
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02009404 if (!alloc_rt_sched_group(tg, parent))
Peter Zijlstrabccbe082008-02-13 15:45:40 +01009405 goto err;
9406
Peter Zijlstra8ed36992008-02-13 15:45:39 +01009407 spin_lock_irqsave(&task_group_lock, flags);
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02009408 for_each_possible_cpu(i) {
Peter Zijlstrabccbe082008-02-13 15:45:40 +01009409 register_fair_sched_group(tg, i);
9410 register_rt_sched_group(tg, i);
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02009411 }
Peter Zijlstra6f505b12008-01-25 21:08:30 +01009412 list_add_rcu(&tg->list, &task_groups);
Peter Zijlstraf473aa52008-04-19 19:45:00 +02009413
9414 WARN_ON(!parent); /* root should already exist */
9415
9416 tg->parent = parent;
Peter Zijlstraf473aa52008-04-19 19:45:00 +02009417 INIT_LIST_HEAD(&tg->children);
Zhang, Yanmin09f27242030-08-14 15:56:40 +08009418 list_add_rcu(&tg->siblings, &parent->children);
Peter Zijlstra8ed36992008-02-13 15:45:39 +01009419 spin_unlock_irqrestore(&task_group_lock, flags);
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02009420
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02009421 return tg;
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02009422
9423err:
Peter Zijlstra6f505b12008-01-25 21:08:30 +01009424 free_sched_group(tg);
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02009425 return ERR_PTR(-ENOMEM);
9426}
9427
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02009428/* rcu callback to free various structures associated with a task group */
Peter Zijlstra6f505b12008-01-25 21:08:30 +01009429static void free_sched_group_rcu(struct rcu_head *rhp)
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02009430{
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02009431 /* now it should be safe to free those cfs_rqs */
Peter Zijlstra6f505b12008-01-25 21:08:30 +01009432 free_sched_group(container_of(rhp, struct task_group, rcu));
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02009433}
9434
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02009435/* Destroy runqueue etc associated with a task group */
Ingo Molnar4cf86d72007-10-15 17:00:14 +02009436void sched_destroy_group(struct task_group *tg)
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02009437{
Peter Zijlstra8ed36992008-02-13 15:45:39 +01009438 unsigned long flags;
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02009439 int i;
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02009440
Peter Zijlstra8ed36992008-02-13 15:45:39 +01009441 spin_lock_irqsave(&task_group_lock, flags);
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02009442 for_each_possible_cpu(i) {
Peter Zijlstrabccbe082008-02-13 15:45:40 +01009443 unregister_fair_sched_group(tg, i);
9444 unregister_rt_sched_group(tg, i);
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02009445 }
Peter Zijlstra6f505b12008-01-25 21:08:30 +01009446 list_del_rcu(&tg->list);
Peter Zijlstraf473aa52008-04-19 19:45:00 +02009447 list_del_rcu(&tg->siblings);
Peter Zijlstra8ed36992008-02-13 15:45:39 +01009448 spin_unlock_irqrestore(&task_group_lock, flags);
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02009449
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02009450 /* wait for possible concurrent references to cfs_rqs complete */
Peter Zijlstra6f505b12008-01-25 21:08:30 +01009451 call_rcu(&tg->rcu, free_sched_group_rcu);
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02009452}
9453
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02009454/* change task's runqueue when it moves between groups.
Ingo Molnar3a252012007-10-15 17:00:12 +02009455 * The caller of this function should have put the task in its new group
9456 * by now. This function just updates tsk->se.cfs_rq and tsk->se.parent to
9457 * reflect its new group.
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02009458 */
9459void sched_move_task(struct task_struct *tsk)
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02009460{
9461 int on_rq, running;
9462 unsigned long flags;
9463 struct rq *rq;
9464
9465 rq = task_rq_lock(tsk, &flags);
9466
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02009467 update_rq_clock(rq);
9468
Dmitry Adamushko051a1d12007-12-18 15:21:13 +01009469 running = task_current(rq, tsk);
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02009470 on_rq = tsk->se.on_rq;
9471
Hiroshi Shimamoto0e1f3482008-03-10 11:01:20 -07009472 if (on_rq)
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02009473 dequeue_task(rq, tsk, 0);
Hiroshi Shimamoto0e1f3482008-03-10 11:01:20 -07009474 if (unlikely(running))
9475 tsk->sched_class->put_prev_task(rq, tsk);
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02009476
Peter Zijlstra6f505b12008-01-25 21:08:30 +01009477 set_task_rq(tsk, task_cpu(tsk));
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02009478
Peter Zijlstra810b3812008-02-29 15:21:01 -05009479#ifdef CONFIG_FAIR_GROUP_SCHED
9480 if (tsk->sched_class->moved_group)
9481 tsk->sched_class->moved_group(tsk);
9482#endif
9483
Hiroshi Shimamoto0e1f3482008-03-10 11:01:20 -07009484 if (unlikely(running))
9485 tsk->sched_class->set_curr_task(rq);
9486 if (on_rq)
Dmitry Adamushko7074bad2007-10-15 17:00:07 +02009487 enqueue_task(rq, tsk, 0);
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02009488
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02009489 task_rq_unlock(rq, &flags);
9490}
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +02009491#endif /* CONFIG_GROUP_SCHED */
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02009492
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01009493#ifdef CONFIG_FAIR_GROUP_SCHED
Peter Zijlstrac09595f2008-06-27 13:41:14 +02009494static void __set_se_shares(struct sched_entity *se, unsigned long shares)
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02009495{
9496 struct cfs_rq *cfs_rq = se->cfs_rq;
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02009497 int on_rq;
9498
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02009499 on_rq = se->on_rq;
Peter Zijlstra62fb1852008-02-25 17:34:02 +01009500 if (on_rq)
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02009501 dequeue_entity(cfs_rq, se, 0);
9502
9503 se->load.weight = shares;
Peter Zijlstrae05510d2008-05-05 23:56:17 +02009504 se->load.inv_weight = 0;
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02009505
Peter Zijlstra62fb1852008-02-25 17:34:02 +01009506 if (on_rq)
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02009507 enqueue_entity(cfs_rq, se, 0);
Peter Zijlstrac09595f2008-06-27 13:41:14 +02009508}
Peter Zijlstra62fb1852008-02-25 17:34:02 +01009509
Peter Zijlstrac09595f2008-06-27 13:41:14 +02009510static void set_se_shares(struct sched_entity *se, unsigned long shares)
9511{
9512 struct cfs_rq *cfs_rq = se->cfs_rq;
9513 struct rq *rq = cfs_rq->rq;
9514 unsigned long flags;
9515
9516 spin_lock_irqsave(&rq->lock, flags);
9517 __set_se_shares(se, shares);
9518 spin_unlock_irqrestore(&rq->lock, flags);
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02009519}
9520
Peter Zijlstra8ed36992008-02-13 15:45:39 +01009521static DEFINE_MUTEX(shares_mutex);
9522
Ingo Molnar4cf86d72007-10-15 17:00:14 +02009523int sched_group_set_shares(struct task_group *tg, unsigned long shares)
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02009524{
9525 int i;
Peter Zijlstra8ed36992008-02-13 15:45:39 +01009526 unsigned long flags;
Ingo Molnarc61935f2008-01-22 11:24:58 +01009527
Peter Zijlstra62fb1852008-02-25 17:34:02 +01009528 /*
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02009529 * We can't change the weight of the root cgroup.
9530 */
9531 if (!tg->se[0])
9532 return -EINVAL;
9533
Peter Zijlstra18d95a22008-04-19 19:45:00 +02009534 if (shares < MIN_SHARES)
9535 shares = MIN_SHARES;
Miao Xiecb4ad1f2008-04-28 12:54:56 +08009536 else if (shares > MAX_SHARES)
9537 shares = MAX_SHARES;
Peter Zijlstra62fb1852008-02-25 17:34:02 +01009538
Peter Zijlstra8ed36992008-02-13 15:45:39 +01009539 mutex_lock(&shares_mutex);
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02009540 if (tg->shares == shares)
Dhaval Giani5cb350b2007-10-15 17:00:14 +02009541 goto done;
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02009542
Peter Zijlstra8ed36992008-02-13 15:45:39 +01009543 spin_lock_irqsave(&task_group_lock, flags);
Peter Zijlstrabccbe082008-02-13 15:45:40 +01009544 for_each_possible_cpu(i)
9545 unregister_fair_sched_group(tg, i);
Peter Zijlstraf473aa52008-04-19 19:45:00 +02009546 list_del_rcu(&tg->siblings);
Peter Zijlstra8ed36992008-02-13 15:45:39 +01009547 spin_unlock_irqrestore(&task_group_lock, flags);
Srivatsa Vaddagiri6b2d7702008-01-25 21:08:00 +01009548
9549 /* wait for any ongoing reference to this group to finish */
9550 synchronize_sched();
9551
9552 /*
9553 * Now we are free to modify the group's share on each cpu
9554 * w/o tripping rebalance_share or load_balance_fair.
9555 */
9556 tg->shares = shares;
Peter Zijlstrac09595f2008-06-27 13:41:14 +02009557 for_each_possible_cpu(i) {
9558 /*
9559 * force a rebalance
9560 */
9561 cfs_rq_set_shares(tg->cfs_rq[i], 0);
Miao Xiecb4ad1f2008-04-28 12:54:56 +08009562 set_se_shares(tg->se[i], shares);
Peter Zijlstrac09595f2008-06-27 13:41:14 +02009563 }
Srivatsa Vaddagiri6b2d7702008-01-25 21:08:00 +01009564
9565 /*
9566 * Enable load balance activity on this group, by inserting it back on
9567 * each cpu's rq->leaf_cfs_rq_list.
9568 */
Peter Zijlstra8ed36992008-02-13 15:45:39 +01009569 spin_lock_irqsave(&task_group_lock, flags);
Peter Zijlstrabccbe082008-02-13 15:45:40 +01009570 for_each_possible_cpu(i)
9571 register_fair_sched_group(tg, i);
Peter Zijlstraf473aa52008-04-19 19:45:00 +02009572 list_add_rcu(&tg->siblings, &tg->parent->children);
Peter Zijlstra8ed36992008-02-13 15:45:39 +01009573 spin_unlock_irqrestore(&task_group_lock, flags);
Dhaval Giani5cb350b2007-10-15 17:00:14 +02009574done:
Peter Zijlstra8ed36992008-02-13 15:45:39 +01009575 mutex_unlock(&shares_mutex);
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02009576 return 0;
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02009577}
9578
Dhaval Giani5cb350b2007-10-15 17:00:14 +02009579unsigned long sched_group_shares(struct task_group *tg)
9580{
9581 return tg->shares;
9582}
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01009583#endif
Dhaval Giani5cb350b2007-10-15 17:00:14 +02009584
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01009585#ifdef CONFIG_RT_GROUP_SCHED
Peter Zijlstra6f505b12008-01-25 21:08:30 +01009586/*
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01009587 * Ensure that the real time constraints are schedulable.
Peter Zijlstra6f505b12008-01-25 21:08:30 +01009588 */
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01009589static DEFINE_MUTEX(rt_constraints_mutex);
9590
9591static unsigned long to_ratio(u64 period, u64 runtime)
9592{
9593 if (runtime == RUNTIME_INF)
Peter Zijlstra9a7e0b12008-08-19 12:33:06 +02009594 return 1ULL << 20;
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01009595
Peter Zijlstra9a7e0b12008-08-19 12:33:06 +02009596 return div64_u64(runtime << 20, period);
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01009597}
9598
Dhaval Giani521f1a242008-02-28 15:21:56 +05309599/* Must be called with tasklist_lock held */
9600static inline int tg_has_rt_tasks(struct task_group *tg)
9601{
9602 struct task_struct *g, *p;
Peter Zijlstra9a7e0b12008-08-19 12:33:06 +02009603
Dhaval Giani521f1a242008-02-28 15:21:56 +05309604 do_each_thread(g, p) {
9605 if (rt_task(p) && rt_rq_of_se(&p->rt)->tg == tg)
9606 return 1;
9607 } while_each_thread(g, p);
Peter Zijlstra9a7e0b12008-08-19 12:33:06 +02009608
Dhaval Giani521f1a242008-02-28 15:21:56 +05309609 return 0;
9610}
9611
Peter Zijlstra9a7e0b12008-08-19 12:33:06 +02009612struct rt_schedulable_data {
9613 struct task_group *tg;
9614 u64 rt_period;
9615 u64 rt_runtime;
9616};
9617
9618static int tg_schedulable(struct task_group *tg, void *data)
9619{
9620 struct rt_schedulable_data *d = data;
9621 struct task_group *child;
9622 unsigned long total, sum = 0;
9623 u64 period, runtime;
9624
9625 period = ktime_to_ns(tg->rt_bandwidth.rt_period);
9626 runtime = tg->rt_bandwidth.rt_runtime;
9627
9628 if (tg == d->tg) {
9629 period = d->rt_period;
9630 runtime = d->rt_runtime;
9631 }
9632
Peter Zijlstra98a48262009-01-14 10:56:32 +01009633#ifdef CONFIG_USER_SCHED
9634 if (tg == &root_task_group) {
9635 period = global_rt_period();
9636 runtime = global_rt_runtime();
9637 }
9638#endif
9639
Peter Zijlstra4653f802008-09-23 15:33:44 +02009640 /*
9641 * Cannot have more runtime than the period.
9642 */
9643 if (runtime > period && runtime != RUNTIME_INF)
9644 return -EINVAL;
9645
9646 /*
9647 * Ensure we don't starve existing RT tasks.
9648 */
Peter Zijlstra9a7e0b12008-08-19 12:33:06 +02009649 if (rt_bandwidth_enabled() && !runtime && tg_has_rt_tasks(tg))
9650 return -EBUSY;
9651
9652 total = to_ratio(period, runtime);
9653
Peter Zijlstra4653f802008-09-23 15:33:44 +02009654 /*
9655 * Nobody can have more than the global setting allows.
9656 */
9657 if (total > to_ratio(global_rt_period(), global_rt_runtime()))
9658 return -EINVAL;
9659
9660 /*
9661 * The sum of our children's runtime should not exceed our own.
9662 */
Peter Zijlstra9a7e0b12008-08-19 12:33:06 +02009663 list_for_each_entry_rcu(child, &tg->children, siblings) {
9664 period = ktime_to_ns(child->rt_bandwidth.rt_period);
9665 runtime = child->rt_bandwidth.rt_runtime;
9666
9667 if (child == d->tg) {
9668 period = d->rt_period;
9669 runtime = d->rt_runtime;
9670 }
9671
9672 sum += to_ratio(period, runtime);
9673 }
9674
9675 if (sum > total)
9676 return -EINVAL;
9677
9678 return 0;
9679}
9680
9681static int __rt_schedulable(struct task_group *tg, u64 period, u64 runtime)
9682{
9683 struct rt_schedulable_data data = {
9684 .tg = tg,
9685 .rt_period = period,
9686 .rt_runtime = runtime,
9687 };
9688
9689 return walk_tg_tree(tg_schedulable, tg_nop, &data);
9690}
9691
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02009692static int tg_set_bandwidth(struct task_group *tg,
9693 u64 rt_period, u64 rt_runtime)
Peter Zijlstra6f505b12008-01-25 21:08:30 +01009694{
Peter Zijlstraac086bc2008-04-19 19:44:58 +02009695 int i, err = 0;
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01009696
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01009697 mutex_lock(&rt_constraints_mutex);
Dhaval Giani521f1a242008-02-28 15:21:56 +05309698 read_lock(&tasklist_lock);
Peter Zijlstra9a7e0b12008-08-19 12:33:06 +02009699 err = __rt_schedulable(tg, rt_period, rt_runtime);
9700 if (err)
Dhaval Giani521f1a242008-02-28 15:21:56 +05309701 goto unlock;
Peter Zijlstraac086bc2008-04-19 19:44:58 +02009702
9703 spin_lock_irq(&tg->rt_bandwidth.rt_runtime_lock);
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02009704 tg->rt_bandwidth.rt_period = ns_to_ktime(rt_period);
9705 tg->rt_bandwidth.rt_runtime = rt_runtime;
Peter Zijlstraac086bc2008-04-19 19:44:58 +02009706
9707 for_each_possible_cpu(i) {
9708 struct rt_rq *rt_rq = tg->rt_rq[i];
9709
9710 spin_lock(&rt_rq->rt_runtime_lock);
9711 rt_rq->rt_runtime = rt_runtime;
9712 spin_unlock(&rt_rq->rt_runtime_lock);
9713 }
9714 spin_unlock_irq(&tg->rt_bandwidth.rt_runtime_lock);
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01009715 unlock:
Dhaval Giani521f1a242008-02-28 15:21:56 +05309716 read_unlock(&tasklist_lock);
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01009717 mutex_unlock(&rt_constraints_mutex);
9718
9719 return err;
Peter Zijlstra6f505b12008-01-25 21:08:30 +01009720}
9721
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02009722int sched_group_set_rt_runtime(struct task_group *tg, long rt_runtime_us)
9723{
9724 u64 rt_runtime, rt_period;
9725
9726 rt_period = ktime_to_ns(tg->rt_bandwidth.rt_period);
9727 rt_runtime = (u64)rt_runtime_us * NSEC_PER_USEC;
9728 if (rt_runtime_us < 0)
9729 rt_runtime = RUNTIME_INF;
9730
9731 return tg_set_bandwidth(tg, rt_period, rt_runtime);
9732}
9733
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01009734long sched_group_rt_runtime(struct task_group *tg)
9735{
9736 u64 rt_runtime_us;
9737
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02009738 if (tg->rt_bandwidth.rt_runtime == RUNTIME_INF)
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01009739 return -1;
9740
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02009741 rt_runtime_us = tg->rt_bandwidth.rt_runtime;
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01009742 do_div(rt_runtime_us, NSEC_PER_USEC);
9743 return rt_runtime_us;
9744}
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02009745
9746int sched_group_set_rt_period(struct task_group *tg, long rt_period_us)
9747{
9748 u64 rt_runtime, rt_period;
9749
9750 rt_period = (u64)rt_period_us * NSEC_PER_USEC;
9751 rt_runtime = tg->rt_bandwidth.rt_runtime;
9752
Raistlin619b0482008-06-26 18:54:09 +02009753 if (rt_period == 0)
9754 return -EINVAL;
9755
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02009756 return tg_set_bandwidth(tg, rt_period, rt_runtime);
9757}
9758
9759long sched_group_rt_period(struct task_group *tg)
9760{
9761 u64 rt_period_us;
9762
9763 rt_period_us = ktime_to_ns(tg->rt_bandwidth.rt_period);
9764 do_div(rt_period_us, NSEC_PER_USEC);
9765 return rt_period_us;
9766}
9767
9768static int sched_rt_global_constraints(void)
9769{
Peter Zijlstra4653f802008-09-23 15:33:44 +02009770 u64 runtime, period;
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02009771 int ret = 0;
9772
Hiroshi Shimamotoec5d4982008-09-10 17:00:19 -07009773 if (sysctl_sched_rt_period <= 0)
9774 return -EINVAL;
9775
Peter Zijlstra4653f802008-09-23 15:33:44 +02009776 runtime = global_rt_runtime();
9777 period = global_rt_period();
9778
9779 /*
9780 * Sanity check on the sysctl variables.
9781 */
9782 if (runtime > period && runtime != RUNTIME_INF)
9783 return -EINVAL;
Peter Zijlstra10b612f2008-06-19 14:22:27 +02009784
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02009785 mutex_lock(&rt_constraints_mutex);
Peter Zijlstra9a7e0b12008-08-19 12:33:06 +02009786 read_lock(&tasklist_lock);
Peter Zijlstra4653f802008-09-23 15:33:44 +02009787 ret = __rt_schedulable(NULL, 0, 0);
Peter Zijlstra9a7e0b12008-08-19 12:33:06 +02009788 read_unlock(&tasklist_lock);
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02009789 mutex_unlock(&rt_constraints_mutex);
9790
9791 return ret;
9792}
Dhaval Giani54e99122009-02-27 15:13:54 +05309793
9794int sched_rt_can_attach(struct task_group *tg, struct task_struct *tsk)
9795{
9796 /* Don't accept realtime tasks when there is no way for them to run */
9797 if (rt_task(tsk) && tg->rt_bandwidth.rt_runtime == 0)
9798 return 0;
9799
9800 return 1;
9801}
9802
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +02009803#else /* !CONFIG_RT_GROUP_SCHED */
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02009804static int sched_rt_global_constraints(void)
9805{
Peter Zijlstraac086bc2008-04-19 19:44:58 +02009806 unsigned long flags;
9807 int i;
9808
Hiroshi Shimamotoec5d4982008-09-10 17:00:19 -07009809 if (sysctl_sched_rt_period <= 0)
9810 return -EINVAL;
9811
Peter Zijlstraac086bc2008-04-19 19:44:58 +02009812 spin_lock_irqsave(&def_rt_bandwidth.rt_runtime_lock, flags);
9813 for_each_possible_cpu(i) {
9814 struct rt_rq *rt_rq = &cpu_rq(i)->rt;
9815
9816 spin_lock(&rt_rq->rt_runtime_lock);
9817 rt_rq->rt_runtime = global_rt_runtime();
9818 spin_unlock(&rt_rq->rt_runtime_lock);
9819 }
9820 spin_unlock_irqrestore(&def_rt_bandwidth.rt_runtime_lock, flags);
9821
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02009822 return 0;
9823}
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +02009824#endif /* CONFIG_RT_GROUP_SCHED */
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02009825
9826int sched_rt_handler(struct ctl_table *table, int write,
9827 struct file *filp, void __user *buffer, size_t *lenp,
9828 loff_t *ppos)
9829{
9830 int ret;
9831 int old_period, old_runtime;
9832 static DEFINE_MUTEX(mutex);
9833
9834 mutex_lock(&mutex);
9835 old_period = sysctl_sched_rt_period;
9836 old_runtime = sysctl_sched_rt_runtime;
9837
9838 ret = proc_dointvec(table, write, filp, buffer, lenp, ppos);
9839
9840 if (!ret && write) {
9841 ret = sched_rt_global_constraints();
9842 if (ret) {
9843 sysctl_sched_rt_period = old_period;
9844 sysctl_sched_rt_runtime = old_runtime;
9845 } else {
9846 def_rt_bandwidth.rt_runtime = global_rt_runtime();
9847 def_rt_bandwidth.rt_period =
9848 ns_to_ktime(global_rt_period());
9849 }
9850 }
9851 mutex_unlock(&mutex);
9852
9853 return ret;
9854}
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07009855
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01009856#ifdef CONFIG_CGROUP_SCHED
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07009857
9858/* return corresponding task_group object of a cgroup */
Paul Menage2b01dfe2007-10-24 18:23:50 +02009859static inline struct task_group *cgroup_tg(struct cgroup *cgrp)
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07009860{
Paul Menage2b01dfe2007-10-24 18:23:50 +02009861 return container_of(cgroup_subsys_state(cgrp, cpu_cgroup_subsys_id),
9862 struct task_group, css);
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07009863}
9864
9865static struct cgroup_subsys_state *
Paul Menage2b01dfe2007-10-24 18:23:50 +02009866cpu_cgroup_create(struct cgroup_subsys *ss, struct cgroup *cgrp)
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07009867{
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02009868 struct task_group *tg, *parent;
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07009869
Paul Menage2b01dfe2007-10-24 18:23:50 +02009870 if (!cgrp->parent) {
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07009871 /* This is early initialization for the top cgroup */
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07009872 return &init_task_group.css;
9873 }
9874
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02009875 parent = cgroup_tg(cgrp->parent);
9876 tg = sched_create_group(parent);
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07009877 if (IS_ERR(tg))
9878 return ERR_PTR(-ENOMEM);
9879
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07009880 return &tg->css;
9881}
9882
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01009883static void
9884cpu_cgroup_destroy(struct cgroup_subsys *ss, struct cgroup *cgrp)
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07009885{
Paul Menage2b01dfe2007-10-24 18:23:50 +02009886 struct task_group *tg = cgroup_tg(cgrp);
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07009887
9888 sched_destroy_group(tg);
9889}
9890
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01009891static int
9892cpu_cgroup_can_attach(struct cgroup_subsys *ss, struct cgroup *cgrp,
9893 struct task_struct *tsk)
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07009894{
Peter Zijlstrab68aa232008-02-13 15:45:40 +01009895#ifdef CONFIG_RT_GROUP_SCHED
Dhaval Giani54e99122009-02-27 15:13:54 +05309896 if (!sched_rt_can_attach(cgroup_tg(cgrp), tsk))
Peter Zijlstrab68aa232008-02-13 15:45:40 +01009897 return -EINVAL;
9898#else
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07009899 /* We don't support RT-tasks being in separate groups */
9900 if (tsk->sched_class != &fair_sched_class)
9901 return -EINVAL;
Peter Zijlstrab68aa232008-02-13 15:45:40 +01009902#endif
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07009903
9904 return 0;
9905}
9906
9907static void
Paul Menage2b01dfe2007-10-24 18:23:50 +02009908cpu_cgroup_attach(struct cgroup_subsys *ss, struct cgroup *cgrp,
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07009909 struct cgroup *old_cont, struct task_struct *tsk)
9910{
9911 sched_move_task(tsk);
9912}
9913
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01009914#ifdef CONFIG_FAIR_GROUP_SCHED
Paul Menagef4c753b2008-04-29 00:59:56 -07009915static int cpu_shares_write_u64(struct cgroup *cgrp, struct cftype *cftype,
Paul Menage2b01dfe2007-10-24 18:23:50 +02009916 u64 shareval)
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07009917{
Paul Menage2b01dfe2007-10-24 18:23:50 +02009918 return sched_group_set_shares(cgroup_tg(cgrp), shareval);
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07009919}
9920
Paul Menagef4c753b2008-04-29 00:59:56 -07009921static u64 cpu_shares_read_u64(struct cgroup *cgrp, struct cftype *cft)
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07009922{
Paul Menage2b01dfe2007-10-24 18:23:50 +02009923 struct task_group *tg = cgroup_tg(cgrp);
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07009924
9925 return (u64) tg->shares;
9926}
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +02009927#endif /* CONFIG_FAIR_GROUP_SCHED */
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07009928
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01009929#ifdef CONFIG_RT_GROUP_SCHED
Mirco Tischler0c708142008-05-14 16:05:46 -07009930static int cpu_rt_runtime_write(struct cgroup *cgrp, struct cftype *cft,
Paul Menage06ecb272008-04-29 01:00:06 -07009931 s64 val)
Peter Zijlstra6f505b12008-01-25 21:08:30 +01009932{
Paul Menage06ecb272008-04-29 01:00:06 -07009933 return sched_group_set_rt_runtime(cgroup_tg(cgrp), val);
Peter Zijlstra6f505b12008-01-25 21:08:30 +01009934}
9935
Paul Menage06ecb272008-04-29 01:00:06 -07009936static s64 cpu_rt_runtime_read(struct cgroup *cgrp, struct cftype *cft)
Peter Zijlstra6f505b12008-01-25 21:08:30 +01009937{
Paul Menage06ecb272008-04-29 01:00:06 -07009938 return sched_group_rt_runtime(cgroup_tg(cgrp));
Peter Zijlstra6f505b12008-01-25 21:08:30 +01009939}
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02009940
9941static int cpu_rt_period_write_uint(struct cgroup *cgrp, struct cftype *cftype,
9942 u64 rt_period_us)
9943{
9944 return sched_group_set_rt_period(cgroup_tg(cgrp), rt_period_us);
9945}
9946
9947static u64 cpu_rt_period_read_uint(struct cgroup *cgrp, struct cftype *cft)
9948{
9949 return sched_group_rt_period(cgroup_tg(cgrp));
9950}
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +02009951#endif /* CONFIG_RT_GROUP_SCHED */
Peter Zijlstra6f505b12008-01-25 21:08:30 +01009952
Paul Menagefe5c7cc2007-10-29 21:18:11 +01009953static struct cftype cpu_files[] = {
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01009954#ifdef CONFIG_FAIR_GROUP_SCHED
Paul Menagefe5c7cc2007-10-29 21:18:11 +01009955 {
9956 .name = "shares",
Paul Menagef4c753b2008-04-29 00:59:56 -07009957 .read_u64 = cpu_shares_read_u64,
9958 .write_u64 = cpu_shares_write_u64,
Paul Menagefe5c7cc2007-10-29 21:18:11 +01009959 },
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01009960#endif
9961#ifdef CONFIG_RT_GROUP_SCHED
Peter Zijlstra6f505b12008-01-25 21:08:30 +01009962 {
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01009963 .name = "rt_runtime_us",
Paul Menage06ecb272008-04-29 01:00:06 -07009964 .read_s64 = cpu_rt_runtime_read,
9965 .write_s64 = cpu_rt_runtime_write,
Peter Zijlstra6f505b12008-01-25 21:08:30 +01009966 },
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02009967 {
9968 .name = "rt_period_us",
Paul Menagef4c753b2008-04-29 00:59:56 -07009969 .read_u64 = cpu_rt_period_read_uint,
9970 .write_u64 = cpu_rt_period_write_uint,
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02009971 },
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01009972#endif
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07009973};
9974
9975static int cpu_cgroup_populate(struct cgroup_subsys *ss, struct cgroup *cont)
9976{
Paul Menagefe5c7cc2007-10-29 21:18:11 +01009977 return cgroup_add_files(cont, ss, cpu_files, ARRAY_SIZE(cpu_files));
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07009978}
9979
9980struct cgroup_subsys cpu_cgroup_subsys = {
Ingo Molnar38605ca2007-10-29 21:18:11 +01009981 .name = "cpu",
9982 .create = cpu_cgroup_create,
9983 .destroy = cpu_cgroup_destroy,
9984 .can_attach = cpu_cgroup_can_attach,
9985 .attach = cpu_cgroup_attach,
9986 .populate = cpu_cgroup_populate,
9987 .subsys_id = cpu_cgroup_subsys_id,
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07009988 .early_init = 1,
9989};
9990
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01009991#endif /* CONFIG_CGROUP_SCHED */
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01009992
9993#ifdef CONFIG_CGROUP_CPUACCT
9994
9995/*
9996 * CPU accounting code for task groups.
9997 *
9998 * Based on the work by Paul Menage (menage@google.com) and Balbir Singh
9999 * (balbir@in.ibm.com).
10000 */
10001
Bharata B Rao934352f2008-11-10 20:41:13 +053010002/* track cpu usage of a group of tasks and its child groups */
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +010010003struct cpuacct {
10004 struct cgroup_subsys_state css;
10005 /* cpuusage holds pointer to a u64-type object on every cpu */
10006 u64 *cpuusage;
Bharata B Rao934352f2008-11-10 20:41:13 +053010007 struct cpuacct *parent;
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +010010008};
10009
10010struct cgroup_subsys cpuacct_subsys;
10011
10012/* return cpu accounting group corresponding to this container */
Dhaval Giani32cd7562008-02-29 10:02:43 +053010013static inline struct cpuacct *cgroup_ca(struct cgroup *cgrp)
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +010010014{
Dhaval Giani32cd7562008-02-29 10:02:43 +053010015 return container_of(cgroup_subsys_state(cgrp, cpuacct_subsys_id),
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +010010016 struct cpuacct, css);
10017}
10018
10019/* return cpu accounting group to which this task belongs */
10020static inline struct cpuacct *task_ca(struct task_struct *tsk)
10021{
10022 return container_of(task_subsys_state(tsk, cpuacct_subsys_id),
10023 struct cpuacct, css);
10024}
10025
10026/* create a new cpu accounting group */
10027static struct cgroup_subsys_state *cpuacct_create(
Dhaval Giani32cd7562008-02-29 10:02:43 +053010028 struct cgroup_subsys *ss, struct cgroup *cgrp)
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +010010029{
10030 struct cpuacct *ca = kzalloc(sizeof(*ca), GFP_KERNEL);
10031
10032 if (!ca)
10033 return ERR_PTR(-ENOMEM);
10034
10035 ca->cpuusage = alloc_percpu(u64);
10036 if (!ca->cpuusage) {
10037 kfree(ca);
10038 return ERR_PTR(-ENOMEM);
10039 }
10040
Bharata B Rao934352f2008-11-10 20:41:13 +053010041 if (cgrp->parent)
10042 ca->parent = cgroup_ca(cgrp->parent);
10043
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +010010044 return &ca->css;
10045}
10046
10047/* destroy an existing cpu accounting group */
Ingo Molnar41a2d6c2007-12-05 15:46:09 +010010048static void
Dhaval Giani32cd7562008-02-29 10:02:43 +053010049cpuacct_destroy(struct cgroup_subsys *ss, struct cgroup *cgrp)
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +010010050{
Dhaval Giani32cd7562008-02-29 10:02:43 +053010051 struct cpuacct *ca = cgroup_ca(cgrp);
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +010010052
10053 free_percpu(ca->cpuusage);
10054 kfree(ca);
10055}
10056
Ken Chen720f5492008-12-15 22:02:01 -080010057static u64 cpuacct_cpuusage_read(struct cpuacct *ca, int cpu)
10058{
Rusty Russellb36128c2009-02-20 16:29:08 +090010059 u64 *cpuusage = per_cpu_ptr(ca->cpuusage, cpu);
Ken Chen720f5492008-12-15 22:02:01 -080010060 u64 data;
10061
10062#ifndef CONFIG_64BIT
10063 /*
10064 * Take rq->lock to make 64-bit read safe on 32-bit platforms.
10065 */
10066 spin_lock_irq(&cpu_rq(cpu)->lock);
10067 data = *cpuusage;
10068 spin_unlock_irq(&cpu_rq(cpu)->lock);
10069#else
10070 data = *cpuusage;
10071#endif
10072
10073 return data;
10074}
10075
10076static void cpuacct_cpuusage_write(struct cpuacct *ca, int cpu, u64 val)
10077{
Rusty Russellb36128c2009-02-20 16:29:08 +090010078 u64 *cpuusage = per_cpu_ptr(ca->cpuusage, cpu);
Ken Chen720f5492008-12-15 22:02:01 -080010079
10080#ifndef CONFIG_64BIT
10081 /*
10082 * Take rq->lock to make 64-bit write safe on 32-bit platforms.
10083 */
10084 spin_lock_irq(&cpu_rq(cpu)->lock);
10085 *cpuusage = val;
10086 spin_unlock_irq(&cpu_rq(cpu)->lock);
10087#else
10088 *cpuusage = val;
10089#endif
10090}
10091
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +010010092/* return total cpu usage (in nanoseconds) of a group */
Dhaval Giani32cd7562008-02-29 10:02:43 +053010093static u64 cpuusage_read(struct cgroup *cgrp, struct cftype *cft)
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +010010094{
Dhaval Giani32cd7562008-02-29 10:02:43 +053010095 struct cpuacct *ca = cgroup_ca(cgrp);
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +010010096 u64 totalcpuusage = 0;
10097 int i;
10098
Ken Chen720f5492008-12-15 22:02:01 -080010099 for_each_present_cpu(i)
10100 totalcpuusage += cpuacct_cpuusage_read(ca, i);
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +010010101
10102 return totalcpuusage;
10103}
10104
Dhaval Giani0297b802008-02-29 10:02:44 +053010105static int cpuusage_write(struct cgroup *cgrp, struct cftype *cftype,
10106 u64 reset)
10107{
10108 struct cpuacct *ca = cgroup_ca(cgrp);
10109 int err = 0;
10110 int i;
10111
10112 if (reset) {
10113 err = -EINVAL;
10114 goto out;
10115 }
10116
Ken Chen720f5492008-12-15 22:02:01 -080010117 for_each_present_cpu(i)
10118 cpuacct_cpuusage_write(ca, i, 0);
Dhaval Giani0297b802008-02-29 10:02:44 +053010119
Dhaval Giani0297b802008-02-29 10:02:44 +053010120out:
10121 return err;
10122}
10123
Ken Chene9515c32008-12-15 22:04:15 -080010124static int cpuacct_percpu_seq_read(struct cgroup *cgroup, struct cftype *cft,
10125 struct seq_file *m)
10126{
10127 struct cpuacct *ca = cgroup_ca(cgroup);
10128 u64 percpu;
10129 int i;
10130
10131 for_each_present_cpu(i) {
10132 percpu = cpuacct_cpuusage_read(ca, i);
10133 seq_printf(m, "%llu ", (unsigned long long) percpu);
10134 }
10135 seq_printf(m, "\n");
10136 return 0;
10137}
10138
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +010010139static struct cftype files[] = {
10140 {
10141 .name = "usage",
Paul Menagef4c753b2008-04-29 00:59:56 -070010142 .read_u64 = cpuusage_read,
10143 .write_u64 = cpuusage_write,
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +010010144 },
Ken Chene9515c32008-12-15 22:04:15 -080010145 {
10146 .name = "usage_percpu",
10147 .read_seq_string = cpuacct_percpu_seq_read,
10148 },
10149
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +010010150};
10151
Dhaval Giani32cd7562008-02-29 10:02:43 +053010152static int cpuacct_populate(struct cgroup_subsys *ss, struct cgroup *cgrp)
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +010010153{
Dhaval Giani32cd7562008-02-29 10:02:43 +053010154 return cgroup_add_files(cgrp, ss, files, ARRAY_SIZE(files));
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +010010155}
10156
10157/*
10158 * charge this task's execution time to its accounting group.
10159 *
10160 * called with rq->lock held.
10161 */
10162static void cpuacct_charge(struct task_struct *tsk, u64 cputime)
10163{
10164 struct cpuacct *ca;
Bharata B Rao934352f2008-11-10 20:41:13 +053010165 int cpu;
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +010010166
Li Zefanc40c6f82009-02-26 15:40:15 +080010167 if (unlikely(!cpuacct_subsys.active))
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +010010168 return;
10169
Bharata B Rao934352f2008-11-10 20:41:13 +053010170 cpu = task_cpu(tsk);
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +010010171 ca = task_ca(tsk);
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +010010172
Bharata B Rao934352f2008-11-10 20:41:13 +053010173 for (; ca; ca = ca->parent) {
Rusty Russellb36128c2009-02-20 16:29:08 +090010174 u64 *cpuusage = per_cpu_ptr(ca->cpuusage, cpu);
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +010010175 *cpuusage += cputime;
10176 }
10177}
10178
10179struct cgroup_subsys cpuacct_subsys = {
10180 .name = "cpuacct",
10181 .create = cpuacct_create,
10182 .destroy = cpuacct_destroy,
10183 .populate = cpuacct_populate,
10184 .subsys_id = cpuacct_subsys_id,
10185};
10186#endif /* CONFIG_CGROUP_CPUACCT */