blob: 2b942e49d0fad0919e64d420c2ef31c401c82c88 [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>
Ingo Molnarcdd6c482009-09-21 12:02:48 +020042#include <linux/perf_event.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070043#include <linux/security.h>
44#include <linux/notifier.h>
45#include <linux/profile.h>
Nigel Cunningham7dfb7102006-12-06 20:34:23 -080046#include <linux/freezer.h>
akpm@osdl.org198e2f12006-01-12 01:05:30 -080047#include <linux/vmalloc.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070048#include <linux/blkdev.h>
49#include <linux/delay.h>
Pavel Emelyanovb4888932007-10-18 23:40:14 -070050#include <linux/pid_namespace.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070051#include <linux/smp.h>
52#include <linux/threads.h>
53#include <linux/timer.h>
54#include <linux/rcupdate.h>
55#include <linux/cpu.h>
56#include <linux/cpuset.h>
57#include <linux/percpu.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>
Tejun Heo969c7922010-05-06 18:49:21 +020060#include <linux/stop_machine.h>
Nick Piggine692ab52007-07-26 13:40:43 +020061#include <linux/sysctl.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070062#include <linux/syscalls.h>
63#include <linux/times.h>
Jay Lan8f0ab512006-09-30 23:28:59 -070064#include <linux/tsacct_kern.h>
bibo maoc6fd91f2006-03-26 01:38:20 -080065#include <linux/kprobes.h>
Shailabh Nagar0ff92242006-07-14 00:24:37 -070066#include <linux/delayacct.h>
Ingo Molnardff06c12007-07-09 18:52:00 +020067#include <linux/unistd.h>
Jens Axboef5ff8422007-09-21 09:19:54 +020068#include <linux/pagemap.h>
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +010069#include <linux/hrtimer.h>
Reynes Philippe30914a52008-03-17 16:19:05 -070070#include <linux/tick.h>
Peter Zijlstraf00b45c2008-04-19 19:45:00 +020071#include <linux/debugfs.h>
72#include <linux/ctype.h>
Steven Rostedt6cd8a4b2008-05-12 21:20:42 +020073#include <linux/ftrace.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090074#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070075
Eric Dumazet5517d862007-05-08 00:32:57 -070076#include <asm/tlb.h>
Satyam Sharma838225b2007-10-24 18:23:50 +020077#include <asm/irq_regs.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070078
Gregory Haskins6e0534f2008-05-12 21:21:01 +020079#include "sched_cpupri.h"
80
Steven Rostedta8d154b2009-04-10 09:36:00 -040081#define CREATE_TRACE_POINTS
Steven Rostedtad8d75f2009-04-14 19:39:12 -040082#include <trace/events/sched.h>
Steven Rostedta8d154b2009-04-10 09:36:00 -040083
Linus Torvalds1da177e2005-04-16 15:20:36 -070084/*
85 * Convert user-nice values [ -20 ... 0 ... 19 ]
86 * to static priority [ MAX_RT_PRIO..MAX_PRIO-1 ],
87 * and back.
88 */
89#define NICE_TO_PRIO(nice) (MAX_RT_PRIO + (nice) + 20)
90#define PRIO_TO_NICE(prio) ((prio) - MAX_RT_PRIO - 20)
91#define TASK_NICE(p) PRIO_TO_NICE((p)->static_prio)
92
93/*
94 * 'User priority' is the nice value converted to something we
95 * can work with better when scaling various scheduler parameters,
96 * it's a [ 0 ... 39 ] range.
97 */
98#define USER_PRIO(p) ((p)-MAX_RT_PRIO)
99#define TASK_USER_PRIO(p) USER_PRIO((p)->static_prio)
100#define MAX_USER_PRIO (USER_PRIO(MAX_PRIO))
101
102/*
Ingo Molnard7876a02008-01-25 21:08:19 +0100103 * Helpers for converting nanosecond timing to jiffy resolution
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 */
Eric Dumazetd6322fa2007-11-09 22:39:38 +0100105#define NS_TO_JIFFIES(TIME) ((unsigned long)(TIME) / (NSEC_PER_SEC / HZ))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200107#define NICE_0_LOAD SCHED_LOAD_SCALE
108#define NICE_0_SHIFT SCHED_LOAD_SHIFT
109
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110/*
111 * These are the 'tuning knobs' of the scheduler:
112 *
Dmitry Adamushkoa4ec24b2007-10-15 17:00:13 +0200113 * default timeslice is 100 msecs (used only for SCHED_RR tasks).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 * Timeslices get refilled after they expire.
115 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116#define DEF_TIMESLICE (100 * HZ / 1000)
Peter Williams2dd73a42006-06-27 02:54:34 -0700117
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +0200118/*
119 * single value that denotes runtime == period, ie unlimited time.
120 */
121#define RUNTIME_INF ((u64)~0ULL)
122
Ingo Molnare05606d2007-07-09 18:51:59 +0200123static inline int rt_policy(int policy)
124{
Roel Kluin3f33a7c2008-05-13 23:44:11 +0200125 if (unlikely(policy == SCHED_FIFO || policy == SCHED_RR))
Ingo Molnare05606d2007-07-09 18:51:59 +0200126 return 1;
127 return 0;
128}
129
130static inline int task_has_rt_policy(struct task_struct *p)
131{
132 return rt_policy(p->policy);
133}
134
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135/*
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200136 * This is the priority-queue data structure of the RT scheduling class:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 */
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200138struct rt_prio_array {
139 DECLARE_BITMAP(bitmap, MAX_RT_PRIO+1); /* include 1 bit for delimiter */
140 struct list_head queue[MAX_RT_PRIO];
141};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +0200143struct rt_bandwidth {
Ingo Molnarea736ed2008-03-25 13:51:45 +0100144 /* nests inside the rq lock: */
Thomas Gleixner0986b112009-11-17 15:32:06 +0100145 raw_spinlock_t rt_runtime_lock;
Ingo Molnarea736ed2008-03-25 13:51:45 +0100146 ktime_t rt_period;
147 u64 rt_runtime;
148 struct hrtimer rt_period_timer;
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +0200149};
150
151static struct rt_bandwidth def_rt_bandwidth;
152
153static int do_sched_rt_period_timer(struct rt_bandwidth *rt_b, int overrun);
154
155static enum hrtimer_restart sched_rt_period_timer(struct hrtimer *timer)
156{
157 struct rt_bandwidth *rt_b =
158 container_of(timer, struct rt_bandwidth, rt_period_timer);
159 ktime_t now;
160 int overrun;
161 int idle = 0;
162
163 for (;;) {
164 now = hrtimer_cb_get_time(timer);
165 overrun = hrtimer_forward(timer, now, rt_b->rt_period);
166
167 if (!overrun)
168 break;
169
170 idle = do_sched_rt_period_timer(rt_b, overrun);
171 }
172
173 return idle ? HRTIMER_NORESTART : HRTIMER_RESTART;
174}
175
176static
177void init_rt_bandwidth(struct rt_bandwidth *rt_b, u64 period, u64 runtime)
178{
179 rt_b->rt_period = ns_to_ktime(period);
180 rt_b->rt_runtime = runtime;
181
Thomas Gleixner0986b112009-11-17 15:32:06 +0100182 raw_spin_lock_init(&rt_b->rt_runtime_lock);
Peter Zijlstraac086bc2008-04-19 19:44:58 +0200183
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +0200184 hrtimer_init(&rt_b->rt_period_timer,
185 CLOCK_MONOTONIC, HRTIMER_MODE_REL);
186 rt_b->rt_period_timer.function = sched_rt_period_timer;
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +0200187}
188
Krzysztof Heltc8bfff62008-09-05 23:46:19 +0200189static inline int rt_bandwidth_enabled(void)
190{
191 return sysctl_sched_rt_runtime >= 0;
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +0200192}
193
194static void start_rt_bandwidth(struct rt_bandwidth *rt_b)
195{
196 ktime_t now;
197
Hiroshi Shimamotocac64d02009-02-25 09:59:26 -0800198 if (!rt_bandwidth_enabled() || rt_b->rt_runtime == RUNTIME_INF)
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +0200199 return;
200
201 if (hrtimer_active(&rt_b->rt_period_timer))
202 return;
203
Thomas Gleixner0986b112009-11-17 15:32:06 +0100204 raw_spin_lock(&rt_b->rt_runtime_lock);
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +0200205 for (;;) {
Peter Zijlstra7f1e2ca2009-03-13 12:21:27 +0100206 unsigned long delta;
207 ktime_t soft, hard;
208
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +0200209 if (hrtimer_active(&rt_b->rt_period_timer))
210 break;
211
212 now = hrtimer_cb_get_time(&rt_b->rt_period_timer);
213 hrtimer_forward(&rt_b->rt_period_timer, now, rt_b->rt_period);
Peter Zijlstra7f1e2ca2009-03-13 12:21:27 +0100214
215 soft = hrtimer_get_softexpires(&rt_b->rt_period_timer);
216 hard = hrtimer_get_expires(&rt_b->rt_period_timer);
217 delta = ktime_to_ns(ktime_sub(hard, soft));
218 __hrtimer_start_range_ns(&rt_b->rt_period_timer, soft, delta,
Arun R Bharadwaj5c333862009-04-16 12:14:37 +0530219 HRTIMER_MODE_ABS_PINNED, 0);
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +0200220 }
Thomas Gleixner0986b112009-11-17 15:32:06 +0100221 raw_spin_unlock(&rt_b->rt_runtime_lock);
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +0200222}
223
224#ifdef CONFIG_RT_GROUP_SCHED
225static void destroy_rt_bandwidth(struct rt_bandwidth *rt_b)
226{
227 hrtimer_cancel(&rt_b->rt_period_timer);
228}
229#endif
230
Heiko Carstens712555e2008-04-28 11:33:07 +0200231/*
232 * sched_domains_mutex serializes calls to arch_init_sched_domains,
233 * detach_destroy_domains and partition_sched_domains.
234 */
235static DEFINE_MUTEX(sched_domains_mutex);
236
Dhaval Giani7c941432010-01-20 13:26:18 +0100237#ifdef CONFIG_CGROUP_SCHED
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +0200238
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -0700239#include <linux/cgroup.h>
240
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +0200241struct cfs_rq;
242
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100243static LIST_HEAD(task_groups);
244
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +0200245/* task group related information */
Ingo Molnar4cf86d72007-10-15 17:00:14 +0200246struct task_group {
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -0700247 struct cgroup_subsys_state css;
Arun R Bharadwaj6c415b92008-12-01 20:49:05 +0530248
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100249#ifdef CONFIG_FAIR_GROUP_SCHED
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +0200250 /* schedulable entities of this group on each cpu */
251 struct sched_entity **se;
252 /* runqueue "owned" by this group on each cpu */
253 struct cfs_rq **cfs_rq;
254 unsigned long shares;
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100255#endif
256
257#ifdef CONFIG_RT_GROUP_SCHED
258 struct sched_rt_entity **rt_se;
259 struct rt_rq **rt_rq;
260
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +0200261 struct rt_bandwidth rt_bandwidth;
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100262#endif
Srivatsa Vaddagiri6b2d7702008-01-25 21:08:00 +0100263
Srivatsa Vaddagiriae8393e2007-10-29 21:18:11 +0100264 struct rcu_head rcu;
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100265 struct list_head list;
Peter Zijlstraf473aa52008-04-19 19:45:00 +0200266
267 struct task_group *parent;
268 struct list_head siblings;
269 struct list_head children;
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +0200270};
271
Peter Zijlstraeff766a2008-04-19 19:45:00 +0200272#define root_task_group init_task_group
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100273
Peter Zijlstra8ed36992008-02-13 15:45:39 +0100274/* task_group_lock serializes add/remove of task groups and also changes to
Srivatsa Vaddagiriec2c5072008-01-25 21:07:59 +0100275 * a task group's cpu shares.
276 */
Peter Zijlstra8ed36992008-02-13 15:45:39 +0100277static DEFINE_SPINLOCK(task_group_lock);
Srivatsa Vaddagiriec2c5072008-01-25 21:07:59 +0100278
Cyrill Gorcunove9036b32009-10-26 22:24:14 +0300279#ifdef CONFIG_FAIR_GROUP_SCHED
280
Peter Zijlstra57310a92009-03-09 13:56:21 +0100281#ifdef CONFIG_SMP
282static int root_task_group_empty(void)
283{
284 return list_empty(&root_task_group.children);
285}
286#endif
287
Srivatsa Vaddagiri93f992c2008-01-25 21:07:59 +0100288# define INIT_TASK_GROUP_LOAD NICE_0_LOAD
Srivatsa Vaddagiri24e377a2007-10-15 17:00:09 +0200289
Miao Xiecb4ad1f2008-04-28 12:54:56 +0800290/*
Lai Jiangshan2e084782008-06-12 16:42:58 +0800291 * A weight of 0 or 1 can cause arithmetics problems.
292 * A weight of a cfs_rq is the sum of weights of which entities
293 * are queued on this cfs_rq, so a weight of a entity should not be
294 * too large, so as the shares value of a task group.
Miao Xiecb4ad1f2008-04-28 12:54:56 +0800295 * (The default weight is 1024 - so there's no practical
296 * limitation from this.)
297 */
Peter Zijlstra18d95a22008-04-19 19:45:00 +0200298#define MIN_SHARES 2
Lai Jiangshan2e084782008-06-12 16:42:58 +0800299#define MAX_SHARES (1UL << 18)
Peter Zijlstra18d95a22008-04-19 19:45:00 +0200300
Srivatsa Vaddagiri93f992c2008-01-25 21:07:59 +0100301static int init_task_group_load = INIT_TASK_GROUP_LOAD;
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100302#endif
303
304/* Default task group.
305 * Every task in system belong to this group at bootup.
306 */
Mike Travis434d53b2008-04-04 18:11:04 -0700307struct task_group init_task_group;
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +0200308
309/* return group to which a task belongs */
Ingo Molnar4cf86d72007-10-15 17:00:14 +0200310static inline struct task_group *task_group(struct task_struct *p)
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +0200311{
Ingo Molnar4cf86d72007-10-15 17:00:14 +0200312 struct task_group *tg;
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +0200313
Dhaval Giani7c941432010-01-20 13:26:18 +0100314#ifdef CONFIG_CGROUP_SCHED
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -0700315 tg = container_of(task_subsys_state(p, cpu_cgroup_subsys_id),
316 struct task_group, css);
Srivatsa Vaddagiri24e377a2007-10-15 17:00:09 +0200317#else
Ingo Molnar41a2d6c2007-12-05 15:46:09 +0100318 tg = &init_task_group;
Srivatsa Vaddagiri24e377a2007-10-15 17:00:09 +0200319#endif
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +0200320 return tg;
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +0200321}
322
323/* Change a task's cfs_rq and parent entity if it moves across CPUs/groups */
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100324static inline void set_task_rq(struct task_struct *p, unsigned int cpu)
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +0200325{
Peter Zijlstra8b08ca52010-04-21 13:02:07 -0700326 /*
327 * Strictly speaking this rcu_read_lock() is not needed since the
328 * task_group is tied to the cgroup, which in turn can never go away
329 * as long as there are tasks attached to it.
330 *
331 * However since task_group() uses task_subsys_state() which is an
332 * rcu_dereference() user, this quiets CONFIG_PROVE_RCU.
333 */
334 rcu_read_lock();
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100335#ifdef CONFIG_FAIR_GROUP_SCHED
Dmitry Adamushkoce96b5a2007-11-15 20:57:40 +0100336 p->se.cfs_rq = task_group(p)->cfs_rq[cpu];
337 p->se.parent = task_group(p)->se[cpu];
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100338#endif
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100339
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100340#ifdef CONFIG_RT_GROUP_SCHED
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100341 p->rt.rt_rq = task_group(p)->rt_rq[cpu];
342 p->rt.parent = task_group(p)->rt_se[cpu];
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100343#endif
Peter Zijlstra8b08ca52010-04-21 13:02:07 -0700344 rcu_read_unlock();
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +0200345}
346
347#else
348
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100349static inline void set_task_rq(struct task_struct *p, unsigned int cpu) { }
Peter Zijlstra83378262008-06-27 13:41:37 +0200350static inline struct task_group *task_group(struct task_struct *p)
351{
352 return NULL;
353}
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +0200354
Dhaval Giani7c941432010-01-20 13:26:18 +0100355#endif /* CONFIG_CGROUP_SCHED */
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +0200356
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200357/* CFS-related fields in a runqueue */
358struct cfs_rq {
359 struct load_weight load;
360 unsigned long nr_running;
361
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200362 u64 exec_clock;
Ingo Molnare9acbff2007-10-15 17:00:04 +0200363 u64 min_vruntime;
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200364
365 struct rb_root tasks_timeline;
366 struct rb_node *rb_leftmost;
Peter Zijlstra4a55bd52008-04-19 19:45:00 +0200367
368 struct list_head tasks;
369 struct list_head *balance_iterator;
370
371 /*
372 * 'curr' points to currently running entity on this cfs_rq.
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200373 * It is set to NULL otherwise (i.e when none are currently running).
374 */
Peter Zijlstra47932412008-11-04 21:25:09 +0100375 struct sched_entity *curr, *next, *last;
Peter Zijlstraddc97292007-10-15 17:00:10 +0200376
Peter Zijlstra5ac5c4d2008-11-10 10:46:32 +0100377 unsigned int nr_spread_over;
Peter Zijlstraddc97292007-10-15 17:00:10 +0200378
Ingo Molnar62160e32007-10-15 17:00:03 +0200379#ifdef CONFIG_FAIR_GROUP_SCHED
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200380 struct rq *rq; /* cpu runqueue to which this cfs_rq is attached */
381
Ingo Molnar41a2d6c2007-12-05 15:46:09 +0100382 /*
383 * leaf cfs_rqs are those that hold tasks (lowest schedulable entity in
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200384 * a hierarchy). Non-leaf lrqs hold other higher schedulable entities
385 * (like users, containers etc.)
386 *
387 * leaf_cfs_rq_list ties together list of leaf cfs_rq's in a cpu. This
388 * list is used during load balance.
389 */
Ingo Molnar41a2d6c2007-12-05 15:46:09 +0100390 struct list_head leaf_cfs_rq_list;
391 struct task_group *tg; /* group that "owns" this runqueue */
Peter Zijlstrac09595f2008-06-27 13:41:14 +0200392
393#ifdef CONFIG_SMP
Peter Zijlstrac09595f2008-06-27 13:41:14 +0200394 /*
Peter Zijlstrac8cba852008-06-27 13:41:23 +0200395 * the part of load.weight contributed by tasks
Peter Zijlstrac09595f2008-06-27 13:41:14 +0200396 */
Peter Zijlstrac8cba852008-06-27 13:41:23 +0200397 unsigned long task_weight;
Peter Zijlstrac09595f2008-06-27 13:41:14 +0200398
Peter Zijlstrac8cba852008-06-27 13:41:23 +0200399 /*
400 * h_load = weight * f(tg)
401 *
402 * Where f(tg) is the recursive weight fraction assigned to
403 * this group.
404 */
405 unsigned long h_load;
Peter Zijlstrac09595f2008-06-27 13:41:14 +0200406
Peter Zijlstrac8cba852008-06-27 13:41:23 +0200407 /*
408 * this cpu's part of tg->shares
409 */
410 unsigned long shares;
Peter Zijlstraf1d239f2008-06-27 13:41:38 +0200411
412 /*
413 * load.weight at the time we set shares
414 */
415 unsigned long rq_weight;
Peter Zijlstrac09595f2008-06-27 13:41:14 +0200416#endif
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200417#endif
418};
419
420/* Real-Time classes' related field in a runqueue: */
421struct rt_rq {
422 struct rt_prio_array active;
Steven Rostedt63489e42008-01-25 21:08:03 +0100423 unsigned long rt_nr_running;
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100424#if defined CONFIG_SMP || defined CONFIG_RT_GROUP_SCHED
Gregory Haskinse864c492008-12-29 09:39:49 -0500425 struct {
426 int curr; /* highest queued rt task prio */
Gregory Haskins398a1532009-01-14 09:10:04 -0500427#ifdef CONFIG_SMP
Gregory Haskinse864c492008-12-29 09:39:49 -0500428 int next; /* next highest */
Gregory Haskins398a1532009-01-14 09:10:04 -0500429#endif
Gregory Haskinse864c492008-12-29 09:39:49 -0500430 } highest_prio;
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100431#endif
Peter Zijlstrafa85ae22008-01-25 21:08:29 +0100432#ifdef CONFIG_SMP
Gregory Haskins73fe6aa2008-01-25 21:08:07 +0100433 unsigned long rt_nr_migratory;
Peter Zijlstraa1ba4d82009-04-01 18:40:15 +0200434 unsigned long rt_nr_total;
Gregory Haskinsa22d7fc2008-01-25 21:08:12 +0100435 int overloaded;
Gregory Haskins917b6272008-12-29 09:39:53 -0500436 struct plist_head pushable_tasks;
Peter Zijlstrafa85ae22008-01-25 21:08:29 +0100437#endif
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100438 int rt_throttled;
Peter Zijlstrafa85ae22008-01-25 21:08:29 +0100439 u64 rt_time;
Peter Zijlstraac086bc2008-04-19 19:44:58 +0200440 u64 rt_runtime;
Ingo Molnarea736ed2008-03-25 13:51:45 +0100441 /* Nests inside the rq lock: */
Thomas Gleixner0986b112009-11-17 15:32:06 +0100442 raw_spinlock_t rt_runtime_lock;
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100443
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100444#ifdef CONFIG_RT_GROUP_SCHED
Peter Zijlstra23b0fdf2008-02-13 15:45:39 +0100445 unsigned long rt_nr_boosted;
446
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100447 struct rq *rq;
448 struct list_head leaf_rt_rq_list;
449 struct task_group *tg;
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100450#endif
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200451};
452
Gregory Haskins57d885f2008-01-25 21:08:18 +0100453#ifdef CONFIG_SMP
454
455/*
456 * We add the notion of a root-domain which will be used to define per-domain
Ingo Molnar0eab9142008-01-25 21:08:19 +0100457 * variables. Each exclusive cpuset essentially defines an island domain by
458 * fully partitioning the member cpus from any other cpuset. Whenever a new
Gregory Haskins57d885f2008-01-25 21:08:18 +0100459 * exclusive cpuset is created, we also create and attach a new root-domain
460 * object.
461 *
Gregory Haskins57d885f2008-01-25 21:08:18 +0100462 */
463struct root_domain {
464 atomic_t refcount;
Rusty Russellc6c49272008-11-25 02:35:05 +1030465 cpumask_var_t span;
466 cpumask_var_t online;
Gregory Haskins637f5082008-01-25 21:08:18 +0100467
Ingo Molnar0eab9142008-01-25 21:08:19 +0100468 /*
Gregory Haskins637f5082008-01-25 21:08:18 +0100469 * The "RT overload" flag: it gets set if a CPU has more than
470 * one runnable RT task.
471 */
Rusty Russellc6c49272008-11-25 02:35:05 +1030472 cpumask_var_t rto_mask;
Ingo Molnar0eab9142008-01-25 21:08:19 +0100473 atomic_t rto_count;
Gregory Haskins6e0534f2008-05-12 21:21:01 +0200474#ifdef CONFIG_SMP
475 struct cpupri cpupri;
476#endif
Gregory Haskins57d885f2008-01-25 21:08:18 +0100477};
478
Gregory Haskinsdc938522008-01-25 21:08:26 +0100479/*
480 * By default the system creates a single root-domain with all cpus as
481 * members (mimicking the global state we have today).
482 */
Gregory Haskins57d885f2008-01-25 21:08:18 +0100483static struct root_domain def_root_domain;
484
485#endif
486
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200487/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 * This is the main, per-CPU runqueue data structure.
489 *
490 * Locking rule: those places that want to lock multiple runqueues
491 * (such as the load balancing or the thread migration code), lock
492 * acquire operations must be ordered by ascending &runqueue.
493 */
Ingo Molnar70b97a72006-07-03 00:25:42 -0700494struct rq {
Ingo Molnard8016492007-10-18 21:32:55 +0200495 /* runqueue lock: */
Thomas Gleixner05fa7852009-11-17 14:28:38 +0100496 raw_spinlock_t lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497
498 /*
499 * nr_running and cpu_load should be in the same cacheline because
500 * remote CPUs use both these fields when doing load calculation.
501 */
502 unsigned long nr_running;
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200503 #define CPU_LOAD_IDX_MAX 5
504 unsigned long cpu_load[CPU_LOAD_IDX_MAX];
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -0700505#ifdef CONFIG_NO_HZ
Mike Galbraith39c0cbe2010-03-11 17:17:13 +0100506 u64 nohz_stamp;
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -0700507 unsigned char in_nohz_recently;
508#endif
Mike Galbraitha64692a2010-03-11 17:16:20 +0100509 unsigned int skip_clock_update;
510
Ingo Molnard8016492007-10-18 21:32:55 +0200511 /* capture load from *all* tasks on this cpu: */
512 struct load_weight load;
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200513 unsigned long nr_load_updates;
514 u64 nr_switches;
515
516 struct cfs_rq cfs;
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100517 struct rt_rq rt;
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100518
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200519#ifdef CONFIG_FAIR_GROUP_SCHED
Ingo Molnard8016492007-10-18 21:32:55 +0200520 /* list of leaf cfs_rq on this cpu: */
521 struct list_head leaf_cfs_rq_list;
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100522#endif
523#ifdef CONFIG_RT_GROUP_SCHED
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100524 struct list_head leaf_rt_rq_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526
527 /*
528 * This is part of a global counter where only the total sum
529 * over all CPUs matters. A task can increase this counter on
530 * one CPU and if it got migrated afterwards it may decrease
531 * it on another CPU. Always updated under the runqueue lock:
532 */
533 unsigned long nr_uninterruptible;
534
Ingo Molnar36c8b582006-07-03 00:25:41 -0700535 struct task_struct *curr, *idle;
Christoph Lameterc9819f42006-12-10 02:20:25 -0800536 unsigned long next_balance;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 struct mm_struct *prev_mm;
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200538
Peter Zijlstra3e51f332008-05-03 18:29:28 +0200539 u64 clock;
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200540
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 atomic_t nr_iowait;
542
543#ifdef CONFIG_SMP
Ingo Molnar0eab9142008-01-25 21:08:19 +0100544 struct root_domain *rd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545 struct sched_domain *sd;
546
Peter Zijlstrae51fd5e2010-05-31 12:37:30 +0200547 unsigned long cpu_power;
548
Henrik Austada0a522c2009-02-13 20:35:45 +0100549 unsigned char idle_at_tick;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550 /* For active balancing */
Gregory Haskins3f029d32009-07-29 11:08:47 -0400551 int post_schedule;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552 int active_balance;
553 int push_cpu;
Tejun Heo969c7922010-05-06 18:49:21 +0200554 struct cpu_stop_work active_balance_work;
Ingo Molnard8016492007-10-18 21:32:55 +0200555 /* cpu of this runqueue: */
556 int cpu;
Gregory Haskins1f11eb62008-06-04 15:04:05 -0400557 int online;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558
Peter Zijlstraa8a51d52008-06-27 13:41:26 +0200559 unsigned long avg_load_per_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560
Peter Zijlstrae9e92502009-09-01 10:34:37 +0200561 u64 rt_avg;
562 u64 age_stamp;
Mike Galbraith1b9508f2009-11-04 17:53:50 +0100563 u64 idle_stamp;
564 u64 avg_idle;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565#endif
566
Thomas Gleixnerdce48a82009-04-11 10:43:41 +0200567 /* calc_load related fields */
568 unsigned long calc_load_update;
569 long calc_load_active;
570
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +0100571#ifdef CONFIG_SCHED_HRTICK
Peter Zijlstra31656512008-07-18 18:01:23 +0200572#ifdef CONFIG_SMP
573 int hrtick_csd_pending;
574 struct call_single_data hrtick_csd;
575#endif
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +0100576 struct hrtimer hrtick_timer;
577#endif
578
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579#ifdef CONFIG_SCHEDSTATS
580 /* latency stats */
581 struct sched_info rq_sched_info;
Ken Chen9c2c4802008-12-16 23:41:22 -0800582 unsigned long long rq_cpu_time;
583 /* could above be rq->cfs_rq.exec_clock + rq->rt_rq.rt_runtime ? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584
585 /* sys_sched_yield() stats */
Ken Chen480b9432007-10-18 21:32:56 +0200586 unsigned int yld_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587
588 /* schedule() stats */
Ken Chen480b9432007-10-18 21:32:56 +0200589 unsigned int sched_switch;
590 unsigned int sched_count;
591 unsigned int sched_goidle;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592
593 /* try_to_wake_up() stats */
Ken Chen480b9432007-10-18 21:32:56 +0200594 unsigned int ttwu_count;
595 unsigned int ttwu_local;
Ingo Molnarb8efb562007-10-15 17:00:10 +0200596
597 /* BKL stats */
Ken Chen480b9432007-10-18 21:32:56 +0200598 unsigned int bkl_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599#endif
600};
601
Fenghua Yuf34e3b62007-07-19 01:48:13 -0700602static DEFINE_PER_CPU_SHARED_ALIGNED(struct rq, runqueues);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603
Peter Zijlstra7d478722009-09-14 19:55:44 +0200604static inline
605void check_preempt_curr(struct rq *rq, struct task_struct *p, int flags)
Ingo Molnardd41f592007-07-09 18:51:59 +0200606{
Peter Zijlstra7d478722009-09-14 19:55:44 +0200607 rq->curr->sched_class->check_preempt_curr(rq, p, flags);
Mike Galbraitha64692a2010-03-11 17:16:20 +0100608
609 /*
610 * A queue event has occurred, and we're going to schedule. In
611 * this case, we can save a useless back to back clock update.
612 */
613 if (test_tsk_need_resched(p))
614 rq->skip_clock_update = 1;
Ingo Molnardd41f592007-07-09 18:51:59 +0200615}
616
Christoph Lameter0a2966b2006-09-25 23:30:51 -0700617static inline int cpu_of(struct rq *rq)
618{
619#ifdef CONFIG_SMP
620 return rq->cpu;
621#else
622 return 0;
623#endif
624}
625
Paul E. McKenney497f0ab2010-02-22 17:04:51 -0800626#define rcu_dereference_check_sched_domain(p) \
Paul E. McKenneyd11c5632010-02-22 17:04:50 -0800627 rcu_dereference_check((p), \
628 rcu_read_lock_sched_held() || \
629 lockdep_is_held(&sched_domains_mutex))
630
Ingo Molnar20d315d2007-07-09 18:51:58 +0200631/*
Nick Piggin674311d2005-06-25 14:57:27 -0700632 * The domain tree (rq->sd) is protected by RCU's quiescent state transition.
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -0700633 * See detach_destroy_domains: synchronize_sched for details.
Nick Piggin674311d2005-06-25 14:57:27 -0700634 *
635 * The domain tree of any CPU may only be accessed from within
636 * preempt-disabled sections.
637 */
Ingo Molnar48f24c42006-07-03 00:25:40 -0700638#define for_each_domain(cpu, __sd) \
Paul E. McKenney497f0ab2010-02-22 17:04:51 -0800639 for (__sd = rcu_dereference_check_sched_domain(cpu_rq(cpu)->sd); __sd; __sd = __sd->parent)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640
641#define cpu_rq(cpu) (&per_cpu(runqueues, (cpu)))
642#define this_rq() (&__get_cpu_var(runqueues))
643#define task_rq(p) cpu_rq(task_cpu(p))
644#define cpu_curr(cpu) (cpu_rq(cpu)->curr)
Hitoshi Mitake54d35f22009-06-29 14:44:57 +0900645#define raw_rq() (&__raw_get_cpu_var(runqueues))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646
Ingo Molnaraa9c4c02008-12-17 14:10:57 +0100647inline void update_rq_clock(struct rq *rq)
Peter Zijlstra3e51f332008-05-03 18:29:28 +0200648{
Mike Galbraitha64692a2010-03-11 17:16:20 +0100649 if (!rq->skip_clock_update)
650 rq->clock = sched_clock_cpu(cpu_of(rq));
Peter Zijlstra3e51f332008-05-03 18:29:28 +0200651}
652
Ingo Molnare436d802007-07-19 21:28:35 +0200653/*
Ingo Molnarbf5c91b2007-10-15 17:00:04 +0200654 * Tunables that become constants when CONFIG_SCHED_DEBUG is off:
655 */
656#ifdef CONFIG_SCHED_DEBUG
657# define const_debug __read_mostly
658#else
659# define const_debug static const
660#endif
661
Ingo Molnar017730c2008-05-12 21:20:52 +0200662/**
663 * runqueue_is_locked
Randy Dunlape17b38b2009-10-11 19:12:00 -0700664 * @cpu: the processor in question.
Ingo Molnar017730c2008-05-12 21:20:52 +0200665 *
666 * Returns true if the current cpu runqueue is locked.
667 * This interface allows printk to be called with the runqueue lock
668 * held and know whether or not it is OK to wake up the klogd.
669 */
Andrew Morton89f19f02009-09-19 11:55:44 -0700670int runqueue_is_locked(int cpu)
Ingo Molnar017730c2008-05-12 21:20:52 +0200671{
Thomas Gleixner05fa7852009-11-17 14:28:38 +0100672 return raw_spin_is_locked(&cpu_rq(cpu)->lock);
Ingo Molnar017730c2008-05-12 21:20:52 +0200673}
674
Ingo Molnarbf5c91b2007-10-15 17:00:04 +0200675/*
676 * Debugging: various feature bits
677 */
Peter Zijlstraf00b45c2008-04-19 19:45:00 +0200678
679#define SCHED_FEAT(name, enabled) \
680 __SCHED_FEAT_##name ,
681
Ingo Molnarbf5c91b2007-10-15 17:00:04 +0200682enum {
Peter Zijlstraf00b45c2008-04-19 19:45:00 +0200683#include "sched_features.h"
Ingo Molnarbf5c91b2007-10-15 17:00:04 +0200684};
685
Peter Zijlstraf00b45c2008-04-19 19:45:00 +0200686#undef SCHED_FEAT
Ingo Molnarbf5c91b2007-10-15 17:00:04 +0200687
Peter Zijlstraf00b45c2008-04-19 19:45:00 +0200688#define SCHED_FEAT(name, enabled) \
689 (1UL << __SCHED_FEAT_##name) * enabled |
690
691const_debug unsigned int sysctl_sched_features =
692#include "sched_features.h"
693 0;
694
695#undef SCHED_FEAT
696
697#ifdef CONFIG_SCHED_DEBUG
698#define SCHED_FEAT(name, enabled) \
699 #name ,
700
Harvey Harrison983ed7a2008-04-24 18:17:55 -0700701static __read_mostly char *sched_feat_names[] = {
Peter Zijlstraf00b45c2008-04-19 19:45:00 +0200702#include "sched_features.h"
703 NULL
704};
705
706#undef SCHED_FEAT
707
Li Zefan34f3a812008-10-30 15:23:32 +0800708static int sched_feat_show(struct seq_file *m, void *v)
Peter Zijlstraf00b45c2008-04-19 19:45:00 +0200709{
Peter Zijlstraf00b45c2008-04-19 19:45:00 +0200710 int i;
711
712 for (i = 0; sched_feat_names[i]; i++) {
Li Zefan34f3a812008-10-30 15:23:32 +0800713 if (!(sysctl_sched_features & (1UL << i)))
714 seq_puts(m, "NO_");
715 seq_printf(m, "%s ", sched_feat_names[i]);
Peter Zijlstraf00b45c2008-04-19 19:45:00 +0200716 }
Li Zefan34f3a812008-10-30 15:23:32 +0800717 seq_puts(m, "\n");
Peter Zijlstraf00b45c2008-04-19 19:45:00 +0200718
Li Zefan34f3a812008-10-30 15:23:32 +0800719 return 0;
Peter Zijlstraf00b45c2008-04-19 19:45:00 +0200720}
721
722static ssize_t
723sched_feat_write(struct file *filp, const char __user *ubuf,
724 size_t cnt, loff_t *ppos)
725{
726 char buf[64];
727 char *cmp = buf;
728 int neg = 0;
729 int i;
730
731 if (cnt > 63)
732 cnt = 63;
733
734 if (copy_from_user(&buf, ubuf, cnt))
735 return -EFAULT;
736
737 buf[cnt] = 0;
738
Ingo Molnarc24b7c52008-04-18 10:55:34 +0200739 if (strncmp(buf, "NO_", 3) == 0) {
Peter Zijlstraf00b45c2008-04-19 19:45:00 +0200740 neg = 1;
741 cmp += 3;
742 }
743
744 for (i = 0; sched_feat_names[i]; i++) {
745 int len = strlen(sched_feat_names[i]);
746
747 if (strncmp(cmp, sched_feat_names[i], len) == 0) {
748 if (neg)
749 sysctl_sched_features &= ~(1UL << i);
750 else
751 sysctl_sched_features |= (1UL << i);
752 break;
753 }
754 }
755
756 if (!sched_feat_names[i])
757 return -EINVAL;
758
Jan Blunck42994722009-11-20 17:40:37 +0100759 *ppos += cnt;
Peter Zijlstraf00b45c2008-04-19 19:45:00 +0200760
761 return cnt;
762}
763
Li Zefan34f3a812008-10-30 15:23:32 +0800764static int sched_feat_open(struct inode *inode, struct file *filp)
765{
766 return single_open(filp, sched_feat_show, NULL);
767}
768
Alexey Dobriyan828c0952009-10-01 15:43:56 -0700769static const struct file_operations sched_feat_fops = {
Li Zefan34f3a812008-10-30 15:23:32 +0800770 .open = sched_feat_open,
771 .write = sched_feat_write,
772 .read = seq_read,
773 .llseek = seq_lseek,
774 .release = single_release,
Peter Zijlstraf00b45c2008-04-19 19:45:00 +0200775};
776
777static __init int sched_init_debug(void)
778{
Peter Zijlstraf00b45c2008-04-19 19:45:00 +0200779 debugfs_create_file("sched_features", 0644, NULL, NULL,
780 &sched_feat_fops);
781
782 return 0;
783}
784late_initcall(sched_init_debug);
785
786#endif
787
788#define sched_feat(x) (sysctl_sched_features & (1UL << __SCHED_FEAT_##x))
Ingo Molnarbf5c91b2007-10-15 17:00:04 +0200789
790/*
Peter Zijlstrab82d9fd2007-11-09 22:39:39 +0100791 * Number of tasks to iterate in a single balance run.
792 * Limited because this is done with IRQs disabled.
793 */
794const_debug unsigned int sysctl_sched_nr_migrate = 32;
795
796/*
Peter Zijlstra2398f2c2008-06-27 13:41:35 +0200797 * ratelimit for updating the group shares.
Peter Zijlstra55cd5342008-08-04 08:54:26 +0200798 * default: 0.25ms
Peter Zijlstra2398f2c2008-06-27 13:41:35 +0200799 */
Peter Zijlstra55cd5342008-08-04 08:54:26 +0200800unsigned int sysctl_sched_shares_ratelimit = 250000;
Christian Ehrhardt0bcdcf22009-11-30 12:16:46 +0100801unsigned int normalized_sysctl_sched_shares_ratelimit = 250000;
Peter Zijlstra2398f2c2008-06-27 13:41:35 +0200802
803/*
Peter Zijlstraffda12a2008-10-17 19:27:02 +0200804 * Inject some fuzzyness into changing the per-cpu group shares
805 * this avoids remote rq-locks at the expense of fairness.
806 * default: 4
807 */
808unsigned int sysctl_sched_shares_thresh = 4;
809
810/*
Peter Zijlstrae9e92502009-09-01 10:34:37 +0200811 * period over which we average the RT time consumption, measured
812 * in ms.
813 *
814 * default: 1s
815 */
816const_debug unsigned int sysctl_sched_time_avg = MSEC_PER_SEC;
817
818/*
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +0100819 * period over which we measure -rt task cpu usage in us.
Peter Zijlstrafa85ae22008-01-25 21:08:29 +0100820 * default: 1s
821 */
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +0100822unsigned int sysctl_sched_rt_period = 1000000;
Peter Zijlstrafa85ae22008-01-25 21:08:29 +0100823
Ingo Molnar6892b752008-02-13 14:02:36 +0100824static __read_mostly int scheduler_running;
825
Peter Zijlstrafa85ae22008-01-25 21:08:29 +0100826/*
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +0100827 * part of the period that we allow rt tasks to run in us.
828 * default: 0.95s
Peter Zijlstrafa85ae22008-01-25 21:08:29 +0100829 */
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +0100830int sysctl_sched_rt_runtime = 950000;
831
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +0200832static inline u64 global_rt_period(void)
833{
834 return (u64)sysctl_sched_rt_period * NSEC_PER_USEC;
835}
836
837static inline u64 global_rt_runtime(void)
838{
roel kluine26873b2008-07-22 16:51:15 -0400839 if (sysctl_sched_rt_runtime < 0)
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +0200840 return RUNTIME_INF;
841
842 return (u64)sysctl_sched_rt_runtime * NSEC_PER_USEC;
843}
Peter Zijlstrafa85ae22008-01-25 21:08:29 +0100844
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845#ifndef prepare_arch_switch
Nick Piggin4866cde2005-06-25 14:57:23 -0700846# define prepare_arch_switch(next) do { } while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847#endif
Nick Piggin4866cde2005-06-25 14:57:23 -0700848#ifndef finish_arch_switch
849# define finish_arch_switch(prev) do { } while (0)
850#endif
851
Dmitry Adamushko051a1d12007-12-18 15:21:13 +0100852static inline int task_current(struct rq *rq, struct task_struct *p)
853{
854 return rq->curr == p;
855}
856
Nick Piggin4866cde2005-06-25 14:57:23 -0700857#ifndef __ARCH_WANT_UNLOCKED_CTXSW
Ingo Molnar70b97a72006-07-03 00:25:42 -0700858static inline int task_running(struct rq *rq, struct task_struct *p)
Nick Piggin4866cde2005-06-25 14:57:23 -0700859{
Dmitry Adamushko051a1d12007-12-18 15:21:13 +0100860 return task_current(rq, p);
Nick Piggin4866cde2005-06-25 14:57:23 -0700861}
862
Ingo Molnar70b97a72006-07-03 00:25:42 -0700863static inline void prepare_lock_switch(struct rq *rq, struct task_struct *next)
Nick Piggin4866cde2005-06-25 14:57:23 -0700864{
865}
866
Ingo Molnar70b97a72006-07-03 00:25:42 -0700867static inline void finish_lock_switch(struct rq *rq, struct task_struct *prev)
Nick Piggin4866cde2005-06-25 14:57:23 -0700868{
Ingo Molnarda04c032005-09-13 11:17:59 +0200869#ifdef CONFIG_DEBUG_SPINLOCK
870 /* this is a valid case when another task releases the spinlock */
871 rq->lock.owner = current;
872#endif
Ingo Molnar8a25d5d2006-07-03 00:24:54 -0700873 /*
874 * If we are tracking spinlock dependencies then we have to
875 * fix up the runqueue lock - which gets 'carried over' from
876 * prev into current:
877 */
878 spin_acquire(&rq->lock.dep_map, 0, 0, _THIS_IP_);
879
Thomas Gleixner05fa7852009-11-17 14:28:38 +0100880 raw_spin_unlock_irq(&rq->lock);
Nick Piggin4866cde2005-06-25 14:57:23 -0700881}
882
883#else /* __ARCH_WANT_UNLOCKED_CTXSW */
Ingo Molnar70b97a72006-07-03 00:25:42 -0700884static inline int task_running(struct rq *rq, struct task_struct *p)
Nick Piggin4866cde2005-06-25 14:57:23 -0700885{
886#ifdef CONFIG_SMP
887 return p->oncpu;
888#else
Dmitry Adamushko051a1d12007-12-18 15:21:13 +0100889 return task_current(rq, p);
Nick Piggin4866cde2005-06-25 14:57:23 -0700890#endif
891}
892
Ingo Molnar70b97a72006-07-03 00:25:42 -0700893static inline void prepare_lock_switch(struct rq *rq, struct task_struct *next)
Nick Piggin4866cde2005-06-25 14:57:23 -0700894{
895#ifdef CONFIG_SMP
896 /*
897 * We can optimise this out completely for !SMP, because the
898 * SMP rebalancing from interrupt is the only thing that cares
899 * here.
900 */
901 next->oncpu = 1;
902#endif
903#ifdef __ARCH_WANT_INTERRUPTS_ON_CTXSW
Thomas Gleixner05fa7852009-11-17 14:28:38 +0100904 raw_spin_unlock_irq(&rq->lock);
Nick Piggin4866cde2005-06-25 14:57:23 -0700905#else
Thomas Gleixner05fa7852009-11-17 14:28:38 +0100906 raw_spin_unlock(&rq->lock);
Nick Piggin4866cde2005-06-25 14:57:23 -0700907#endif
908}
909
Ingo Molnar70b97a72006-07-03 00:25:42 -0700910static inline void finish_lock_switch(struct rq *rq, struct task_struct *prev)
Nick Piggin4866cde2005-06-25 14:57:23 -0700911{
912#ifdef CONFIG_SMP
913 /*
914 * After ->oncpu is cleared, the task can be moved to a different CPU.
915 * We must ensure this doesn't happen until the switch is completely
916 * finished.
917 */
918 smp_wmb();
919 prev->oncpu = 0;
920#endif
921#ifndef __ARCH_WANT_INTERRUPTS_ON_CTXSW
922 local_irq_enable();
923#endif
924}
925#endif /* __ARCH_WANT_UNLOCKED_CTXSW */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926
927/*
Peter Zijlstra65cc8e42010-03-25 21:05:16 +0100928 * Check whether the task is waking, we use this to synchronize ->cpus_allowed
929 * against ttwu().
Peter Zijlstra0970d292010-02-15 14:45:54 +0100930 */
931static inline int task_is_waking(struct task_struct *p)
932{
Peter Zijlstra0017d732010-03-24 18:34:10 +0100933 return unlikely(p->state == TASK_WAKING);
Peter Zijlstra0970d292010-02-15 14:45:54 +0100934}
935
936/*
Ingo Molnarb29739f2006-06-27 02:54:51 -0700937 * __task_rq_lock - lock the runqueue a given task resides on.
938 * Must be called interrupts disabled.
939 */
Ingo Molnar70b97a72006-07-03 00:25:42 -0700940static inline struct rq *__task_rq_lock(struct task_struct *p)
Ingo Molnarb29739f2006-06-27 02:54:51 -0700941 __acquires(rq->lock)
942{
Peter Zijlstra0970d292010-02-15 14:45:54 +0100943 struct rq *rq;
944
Andi Kleen3a5c3592007-10-15 17:00:14 +0200945 for (;;) {
Peter Zijlstra0970d292010-02-15 14:45:54 +0100946 rq = task_rq(p);
Thomas Gleixner05fa7852009-11-17 14:28:38 +0100947 raw_spin_lock(&rq->lock);
Peter Zijlstra65cc8e42010-03-25 21:05:16 +0100948 if (likely(rq == task_rq(p)))
Andi Kleen3a5c3592007-10-15 17:00:14 +0200949 return rq;
Thomas Gleixner05fa7852009-11-17 14:28:38 +0100950 raw_spin_unlock(&rq->lock);
Ingo Molnarb29739f2006-06-27 02:54:51 -0700951 }
Ingo Molnarb29739f2006-06-27 02:54:51 -0700952}
953
954/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955 * task_rq_lock - lock the runqueue a given task resides on and disable
Ingo Molnar41a2d6c2007-12-05 15:46:09 +0100956 * interrupts. Note the ordering: we can safely lookup the task_rq without
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957 * explicitly disabling preemption.
958 */
Ingo Molnar70b97a72006-07-03 00:25:42 -0700959static struct rq *task_rq_lock(struct task_struct *p, unsigned long *flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960 __acquires(rq->lock)
961{
Ingo Molnar70b97a72006-07-03 00:25:42 -0700962 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963
Andi Kleen3a5c3592007-10-15 17:00:14 +0200964 for (;;) {
965 local_irq_save(*flags);
966 rq = task_rq(p);
Thomas Gleixner05fa7852009-11-17 14:28:38 +0100967 raw_spin_lock(&rq->lock);
Peter Zijlstra65cc8e42010-03-25 21:05:16 +0100968 if (likely(rq == task_rq(p)))
Andi Kleen3a5c3592007-10-15 17:00:14 +0200969 return rq;
Thomas Gleixner05fa7852009-11-17 14:28:38 +0100970 raw_spin_unlock_irqrestore(&rq->lock, *flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972}
973
Alexey Dobriyana9957442007-10-15 17:00:13 +0200974static void __task_rq_unlock(struct rq *rq)
Ingo Molnarb29739f2006-06-27 02:54:51 -0700975 __releases(rq->lock)
976{
Thomas Gleixner05fa7852009-11-17 14:28:38 +0100977 raw_spin_unlock(&rq->lock);
Ingo Molnarb29739f2006-06-27 02:54:51 -0700978}
979
Ingo Molnar70b97a72006-07-03 00:25:42 -0700980static inline void task_rq_unlock(struct rq *rq, unsigned long *flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981 __releases(rq->lock)
982{
Thomas Gleixner05fa7852009-11-17 14:28:38 +0100983 raw_spin_unlock_irqrestore(&rq->lock, *flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984}
985
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986/*
Robert P. J. Daycc2a73b2006-12-10 02:20:00 -0800987 * this_rq_lock - lock this runqueue and disable interrupts.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988 */
Alexey Dobriyana9957442007-10-15 17:00:13 +0200989static struct rq *this_rq_lock(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990 __acquires(rq->lock)
991{
Ingo Molnar70b97a72006-07-03 00:25:42 -0700992 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993
994 local_irq_disable();
995 rq = this_rq();
Thomas Gleixner05fa7852009-11-17 14:28:38 +0100996 raw_spin_lock(&rq->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997
998 return rq;
999}
1000
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01001001#ifdef CONFIG_SCHED_HRTICK
1002/*
1003 * Use HR-timers to deliver accurate preemption points.
1004 *
1005 * Its all a bit involved since we cannot program an hrt while holding the
1006 * rq->lock. So what we do is store a state in in rq->hrtick_* and ask for a
1007 * reschedule event.
1008 *
1009 * When we get rescheduled we reprogram the hrtick_timer outside of the
1010 * rq->lock.
1011 */
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01001012
1013/*
1014 * Use hrtick when:
1015 * - enabled by features
1016 * - hrtimer is actually high res
1017 */
1018static inline int hrtick_enabled(struct rq *rq)
1019{
1020 if (!sched_feat(HRTICK))
1021 return 0;
Ingo Molnarba420592008-07-20 11:02:06 +02001022 if (!cpu_active(cpu_of(rq)))
Peter Zijlstrab328ca12008-04-29 10:02:46 +02001023 return 0;
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01001024 return hrtimer_is_hres_active(&rq->hrtick_timer);
1025}
1026
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01001027static void hrtick_clear(struct rq *rq)
1028{
1029 if (hrtimer_active(&rq->hrtick_timer))
1030 hrtimer_cancel(&rq->hrtick_timer);
1031}
1032
1033/*
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01001034 * High-resolution timer tick.
1035 * Runs from hardirq context with interrupts disabled.
1036 */
1037static enum hrtimer_restart hrtick(struct hrtimer *timer)
1038{
1039 struct rq *rq = container_of(timer, struct rq, hrtick_timer);
1040
1041 WARN_ON_ONCE(cpu_of(rq) != smp_processor_id());
1042
Thomas Gleixner05fa7852009-11-17 14:28:38 +01001043 raw_spin_lock(&rq->lock);
Peter Zijlstra3e51f332008-05-03 18:29:28 +02001044 update_rq_clock(rq);
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01001045 rq->curr->sched_class->task_tick(rq, rq->curr, 1);
Thomas Gleixner05fa7852009-11-17 14:28:38 +01001046 raw_spin_unlock(&rq->lock);
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01001047
1048 return HRTIMER_NORESTART;
1049}
1050
Rabin Vincent95e904c2008-05-11 05:55:33 +05301051#ifdef CONFIG_SMP
Peter Zijlstra31656512008-07-18 18:01:23 +02001052/*
1053 * called from hardirq (IPI) context
1054 */
1055static void __hrtick_start(void *arg)
Peter Zijlstrab328ca12008-04-29 10:02:46 +02001056{
Peter Zijlstra31656512008-07-18 18:01:23 +02001057 struct rq *rq = arg;
Peter Zijlstrab328ca12008-04-29 10:02:46 +02001058
Thomas Gleixner05fa7852009-11-17 14:28:38 +01001059 raw_spin_lock(&rq->lock);
Peter Zijlstra31656512008-07-18 18:01:23 +02001060 hrtimer_restart(&rq->hrtick_timer);
1061 rq->hrtick_csd_pending = 0;
Thomas Gleixner05fa7852009-11-17 14:28:38 +01001062 raw_spin_unlock(&rq->lock);
Peter Zijlstrab328ca12008-04-29 10:02:46 +02001063}
1064
Peter Zijlstra31656512008-07-18 18:01:23 +02001065/*
1066 * Called to set the hrtick timer state.
1067 *
1068 * called with rq->lock held and irqs disabled
1069 */
1070static void hrtick_start(struct rq *rq, u64 delay)
Peter Zijlstrab328ca12008-04-29 10:02:46 +02001071{
Peter Zijlstra31656512008-07-18 18:01:23 +02001072 struct hrtimer *timer = &rq->hrtick_timer;
1073 ktime_t time = ktime_add_ns(timer->base->get_time(), delay);
Peter Zijlstrab328ca12008-04-29 10:02:46 +02001074
Arjan van de Vencc584b22008-09-01 15:02:30 -07001075 hrtimer_set_expires(timer, time);
Peter Zijlstra31656512008-07-18 18:01:23 +02001076
1077 if (rq == this_rq()) {
1078 hrtimer_restart(timer);
1079 } else if (!rq->hrtick_csd_pending) {
Peter Zijlstra6e275632009-02-25 13:59:48 +01001080 __smp_call_function_single(cpu_of(rq), &rq->hrtick_csd, 0);
Peter Zijlstra31656512008-07-18 18:01:23 +02001081 rq->hrtick_csd_pending = 1;
1082 }
Peter Zijlstrab328ca12008-04-29 10:02:46 +02001083}
1084
1085static int
1086hotplug_hrtick(struct notifier_block *nfb, unsigned long action, void *hcpu)
1087{
1088 int cpu = (int)(long)hcpu;
1089
1090 switch (action) {
1091 case CPU_UP_CANCELED:
1092 case CPU_UP_CANCELED_FROZEN:
1093 case CPU_DOWN_PREPARE:
1094 case CPU_DOWN_PREPARE_FROZEN:
1095 case CPU_DEAD:
1096 case CPU_DEAD_FROZEN:
Peter Zijlstra31656512008-07-18 18:01:23 +02001097 hrtick_clear(cpu_rq(cpu));
Peter Zijlstrab328ca12008-04-29 10:02:46 +02001098 return NOTIFY_OK;
1099 }
1100
1101 return NOTIFY_DONE;
1102}
1103
Rakib Mullickfa748202008-09-22 14:55:45 -07001104static __init void init_hrtick(void)
Peter Zijlstrab328ca12008-04-29 10:02:46 +02001105{
1106 hotcpu_notifier(hotplug_hrtick, 0);
1107}
Peter Zijlstra31656512008-07-18 18:01:23 +02001108#else
1109/*
1110 * Called to set the hrtick timer state.
1111 *
1112 * called with rq->lock held and irqs disabled
1113 */
1114static void hrtick_start(struct rq *rq, u64 delay)
1115{
Peter Zijlstra7f1e2ca2009-03-13 12:21:27 +01001116 __hrtimer_start_range_ns(&rq->hrtick_timer, ns_to_ktime(delay), 0,
Arun R Bharadwaj5c333862009-04-16 12:14:37 +05301117 HRTIMER_MODE_REL_PINNED, 0);
Peter Zijlstra31656512008-07-18 18:01:23 +02001118}
1119
Andrew Morton006c75f2008-09-22 14:55:46 -07001120static inline void init_hrtick(void)
Peter Zijlstra31656512008-07-18 18:01:23 +02001121{
1122}
Rabin Vincent95e904c2008-05-11 05:55:33 +05301123#endif /* CONFIG_SMP */
Peter Zijlstrab328ca12008-04-29 10:02:46 +02001124
1125static void init_rq_hrtick(struct rq *rq)
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01001126{
Peter Zijlstra31656512008-07-18 18:01:23 +02001127#ifdef CONFIG_SMP
1128 rq->hrtick_csd_pending = 0;
1129
1130 rq->hrtick_csd.flags = 0;
1131 rq->hrtick_csd.func = __hrtick_start;
1132 rq->hrtick_csd.info = rq;
1133#endif
1134
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01001135 hrtimer_init(&rq->hrtick_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
1136 rq->hrtick_timer.function = hrtick;
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01001137}
Andrew Morton006c75f2008-09-22 14:55:46 -07001138#else /* CONFIG_SCHED_HRTICK */
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01001139static inline void hrtick_clear(struct rq *rq)
1140{
1141}
1142
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01001143static inline void init_rq_hrtick(struct rq *rq)
1144{
1145}
1146
Peter Zijlstrab328ca12008-04-29 10:02:46 +02001147static inline void init_hrtick(void)
1148{
1149}
Andrew Morton006c75f2008-09-22 14:55:46 -07001150#endif /* CONFIG_SCHED_HRTICK */
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01001151
Ingo Molnar1b9f19c2007-07-09 18:51:59 +02001152/*
Ingo Molnarc24d20d2007-07-09 18:51:59 +02001153 * resched_task - mark a task 'to be rescheduled now'.
1154 *
1155 * On UP this means the setting of the need_resched flag, on SMP it
1156 * might also involve a cross-CPU call to trigger the scheduler on
1157 * the target CPU.
1158 */
1159#ifdef CONFIG_SMP
1160
1161#ifndef tsk_is_polling
1162#define tsk_is_polling(t) test_tsk_thread_flag(t, TIF_POLLING_NRFLAG)
1163#endif
1164
Peter Zijlstra31656512008-07-18 18:01:23 +02001165static void resched_task(struct task_struct *p)
Ingo Molnarc24d20d2007-07-09 18:51:59 +02001166{
1167 int cpu;
1168
Thomas Gleixner05fa7852009-11-17 14:28:38 +01001169 assert_raw_spin_locked(&task_rq(p)->lock);
Ingo Molnarc24d20d2007-07-09 18:51:59 +02001170
Lai Jiangshan5ed0cec2009-03-06 19:40:20 +08001171 if (test_tsk_need_resched(p))
Ingo Molnarc24d20d2007-07-09 18:51:59 +02001172 return;
1173
Lai Jiangshan5ed0cec2009-03-06 19:40:20 +08001174 set_tsk_need_resched(p);
Ingo Molnarc24d20d2007-07-09 18:51:59 +02001175
1176 cpu = task_cpu(p);
1177 if (cpu == smp_processor_id())
1178 return;
1179
1180 /* NEED_RESCHED must be visible before we test polling */
1181 smp_mb();
1182 if (!tsk_is_polling(p))
1183 smp_send_reschedule(cpu);
1184}
1185
1186static void resched_cpu(int cpu)
1187{
1188 struct rq *rq = cpu_rq(cpu);
1189 unsigned long flags;
1190
Thomas Gleixner05fa7852009-11-17 14:28:38 +01001191 if (!raw_spin_trylock_irqsave(&rq->lock, flags))
Ingo Molnarc24d20d2007-07-09 18:51:59 +02001192 return;
1193 resched_task(cpu_curr(cpu));
Thomas Gleixner05fa7852009-11-17 14:28:38 +01001194 raw_spin_unlock_irqrestore(&rq->lock, flags);
Ingo Molnarc24d20d2007-07-09 18:51:59 +02001195}
Thomas Gleixner06d83082008-03-22 09:20:24 +01001196
1197#ifdef CONFIG_NO_HZ
1198/*
1199 * When add_timer_on() enqueues a timer into the timer wheel of an
1200 * idle CPU then this timer might expire before the next timer event
1201 * which is scheduled to wake up that CPU. In case of a completely
1202 * idle system the next event might even be infinite time into the
1203 * future. wake_up_idle_cpu() ensures that the CPU is woken up and
1204 * leaves the inner idle loop so the newly added timer is taken into
1205 * account when the CPU goes back to idle and evaluates the timer
1206 * wheel for the next timer event.
1207 */
1208void wake_up_idle_cpu(int cpu)
1209{
1210 struct rq *rq = cpu_rq(cpu);
1211
1212 if (cpu == smp_processor_id())
1213 return;
1214
1215 /*
1216 * This is safe, as this function is called with the timer
1217 * wheel base lock of (cpu) held. When the CPU is on the way
1218 * to idle and has not yet set rq->curr to idle then it will
1219 * be serialized on the timer wheel base lock and take the new
1220 * timer into account automatically.
1221 */
1222 if (rq->curr != rq->idle)
1223 return;
1224
1225 /*
1226 * We can set TIF_RESCHED on the idle task of the other CPU
1227 * lockless. The worst case is that the other CPU runs the
1228 * idle task through an additional NOOP schedule()
1229 */
Lai Jiangshan5ed0cec2009-03-06 19:40:20 +08001230 set_tsk_need_resched(rq->idle);
Thomas Gleixner06d83082008-03-22 09:20:24 +01001231
1232 /* NEED_RESCHED must be visible before we test polling */
1233 smp_mb();
1234 if (!tsk_is_polling(rq->idle))
1235 smp_send_reschedule(cpu);
1236}
Mike Galbraith39c0cbe2010-03-11 17:17:13 +01001237
1238int nohz_ratelimit(int cpu)
1239{
1240 struct rq *rq = cpu_rq(cpu);
1241 u64 diff = rq->clock - rq->nohz_stamp;
1242
1243 rq->nohz_stamp = rq->clock;
1244
1245 return diff < (NSEC_PER_SEC / HZ) >> 1;
1246}
1247
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +02001248#endif /* CONFIG_NO_HZ */
Thomas Gleixner06d83082008-03-22 09:20:24 +01001249
Peter Zijlstrae9e92502009-09-01 10:34:37 +02001250static u64 sched_avg_period(void)
1251{
1252 return (u64)sysctl_sched_time_avg * NSEC_PER_MSEC / 2;
1253}
1254
1255static void sched_avg_update(struct rq *rq)
1256{
1257 s64 period = sched_avg_period();
1258
1259 while ((s64)(rq->clock - rq->age_stamp) > period) {
1260 rq->age_stamp += period;
1261 rq->rt_avg /= 2;
1262 }
1263}
1264
1265static void sched_rt_avg_update(struct rq *rq, u64 rt_delta)
1266{
1267 rq->rt_avg += rt_delta;
1268 sched_avg_update(rq);
1269}
1270
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +02001271#else /* !CONFIG_SMP */
Peter Zijlstra31656512008-07-18 18:01:23 +02001272static void resched_task(struct task_struct *p)
Ingo Molnarc24d20d2007-07-09 18:51:59 +02001273{
Thomas Gleixner05fa7852009-11-17 14:28:38 +01001274 assert_raw_spin_locked(&task_rq(p)->lock);
Peter Zijlstra31656512008-07-18 18:01:23 +02001275 set_tsk_need_resched(p);
Ingo Molnarc24d20d2007-07-09 18:51:59 +02001276}
Peter Zijlstrae9e92502009-09-01 10:34:37 +02001277
1278static void sched_rt_avg_update(struct rq *rq, u64 rt_delta)
1279{
1280}
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +02001281#endif /* CONFIG_SMP */
Ingo Molnarc24d20d2007-07-09 18:51:59 +02001282
Ingo Molnar45bf76d2007-07-09 18:51:59 +02001283#if BITS_PER_LONG == 32
1284# define WMULT_CONST (~0UL)
1285#else
1286# define WMULT_CONST (1UL << 32)
1287#endif
1288
1289#define WMULT_SHIFT 32
1290
Ingo Molnar194081e2007-08-09 11:16:51 +02001291/*
1292 * Shift right and round:
1293 */
Ingo Molnarcf2ab462007-09-05 14:32:49 +02001294#define SRR(x, y) (((x) + (1UL << ((y) - 1))) >> (y))
Ingo Molnar194081e2007-08-09 11:16:51 +02001295
Peter Zijlstraa7be37a2008-06-27 13:41:11 +02001296/*
1297 * delta *= weight / lw
1298 */
Ingo Molnarcb1c4fc2007-08-02 17:41:40 +02001299static unsigned long
Ingo Molnar45bf76d2007-07-09 18:51:59 +02001300calc_delta_mine(unsigned long delta_exec, unsigned long weight,
1301 struct load_weight *lw)
1302{
1303 u64 tmp;
1304
Lai Jiangshan7a232e02008-06-12 16:43:07 +08001305 if (!lw->inv_weight) {
1306 if (BITS_PER_LONG > 32 && unlikely(lw->weight >= WMULT_CONST))
1307 lw->inv_weight = 1;
1308 else
1309 lw->inv_weight = 1 + (WMULT_CONST-lw->weight/2)
1310 / (lw->weight+1);
1311 }
Ingo Molnar45bf76d2007-07-09 18:51:59 +02001312
1313 tmp = (u64)delta_exec * weight;
1314 /*
1315 * Check whether we'd overflow the 64-bit multiplication:
1316 */
Ingo Molnar194081e2007-08-09 11:16:51 +02001317 if (unlikely(tmp > WMULT_CONST))
Ingo Molnarcf2ab462007-09-05 14:32:49 +02001318 tmp = SRR(SRR(tmp, WMULT_SHIFT/2) * lw->inv_weight,
Ingo Molnar194081e2007-08-09 11:16:51 +02001319 WMULT_SHIFT/2);
1320 else
Ingo Molnarcf2ab462007-09-05 14:32:49 +02001321 tmp = SRR(tmp * lw->inv_weight, WMULT_SHIFT);
Ingo Molnar45bf76d2007-07-09 18:51:59 +02001322
Ingo Molnarecf691d2007-08-02 17:41:40 +02001323 return (unsigned long)min(tmp, (u64)(unsigned long)LONG_MAX);
Ingo Molnar45bf76d2007-07-09 18:51:59 +02001324}
1325
Ingo Molnar10919852007-10-15 17:00:04 +02001326static inline void update_load_add(struct load_weight *lw, unsigned long inc)
Ingo Molnar45bf76d2007-07-09 18:51:59 +02001327{
1328 lw->weight += inc;
Ingo Molnare89996a2008-03-14 23:48:28 +01001329 lw->inv_weight = 0;
Ingo Molnar45bf76d2007-07-09 18:51:59 +02001330}
1331
Ingo Molnar10919852007-10-15 17:00:04 +02001332static inline void update_load_sub(struct load_weight *lw, unsigned long dec)
Ingo Molnar45bf76d2007-07-09 18:51:59 +02001333{
1334 lw->weight -= dec;
Ingo Molnare89996a2008-03-14 23:48:28 +01001335 lw->inv_weight = 0;
Ingo Molnar45bf76d2007-07-09 18:51:59 +02001336}
1337
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338/*
Peter Williams2dd73a42006-06-27 02:54:34 -07001339 * To aid in avoiding the subversion of "niceness" due to uneven distribution
1340 * of tasks with abnormal "nice" values across CPUs the contribution that
1341 * each task makes to its run queue's load is weighted according to its
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01001342 * scheduling class and "nice" value. For SCHED_NORMAL tasks this is just a
Peter Williams2dd73a42006-06-27 02:54:34 -07001343 * scaled version of the new time slice allocation that they receive on time
1344 * slice expiry etc.
1345 */
1346
Peter Zijlstracce7ade2009-01-15 14:53:37 +01001347#define WEIGHT_IDLEPRIO 3
1348#define WMULT_IDLEPRIO 1431655765
Ingo Molnardd41f592007-07-09 18:51:59 +02001349
1350/*
1351 * Nice levels are multiplicative, with a gentle 10% change for every
1352 * nice level changed. I.e. when a CPU-bound task goes from nice 0 to
1353 * nice 1, it will get ~10% less CPU time than another CPU-bound task
1354 * that remained on nice 0.
1355 *
1356 * The "10% effect" is relative and cumulative: from _any_ nice level,
1357 * if you go up 1 level, it's -10% CPU usage, if you go down 1 level
Ingo Molnarf9153ee2007-07-16 09:46:30 +02001358 * it's +10% CPU usage. (to achieve that we use a multiplier of 1.25.
1359 * If a task goes up by ~10% and another task goes down by ~10% then
1360 * the relative distance between them is ~25%.)
Ingo Molnardd41f592007-07-09 18:51:59 +02001361 */
1362static const int prio_to_weight[40] = {
Ingo Molnar254753d2007-08-09 11:16:51 +02001363 /* -20 */ 88761, 71755, 56483, 46273, 36291,
1364 /* -15 */ 29154, 23254, 18705, 14949, 11916,
1365 /* -10 */ 9548, 7620, 6100, 4904, 3906,
1366 /* -5 */ 3121, 2501, 1991, 1586, 1277,
1367 /* 0 */ 1024, 820, 655, 526, 423,
1368 /* 5 */ 335, 272, 215, 172, 137,
1369 /* 10 */ 110, 87, 70, 56, 45,
1370 /* 15 */ 36, 29, 23, 18, 15,
Ingo Molnardd41f592007-07-09 18:51:59 +02001371};
1372
Ingo Molnar5714d2d2007-07-16 09:46:31 +02001373/*
1374 * Inverse (2^32/x) values of the prio_to_weight[] array, precalculated.
1375 *
1376 * In cases where the weight does not change often, we can use the
1377 * precalculated inverse to speed up arithmetics by turning divisions
1378 * into multiplications:
1379 */
Ingo Molnardd41f592007-07-09 18:51:59 +02001380static const u32 prio_to_wmult[40] = {
Ingo Molnar254753d2007-08-09 11:16:51 +02001381 /* -20 */ 48388, 59856, 76040, 92818, 118348,
1382 /* -15 */ 147320, 184698, 229616, 287308, 360437,
1383 /* -10 */ 449829, 563644, 704093, 875809, 1099582,
1384 /* -5 */ 1376151, 1717300, 2157191, 2708050, 3363326,
1385 /* 0 */ 4194304, 5237765, 6557202, 8165337, 10153587,
1386 /* 5 */ 12820798, 15790321, 19976592, 24970740, 31350126,
1387 /* 10 */ 39045157, 49367440, 61356676, 76695844, 95443717,
1388 /* 15 */ 119304647, 148102320, 186737708, 238609294, 286331153,
Ingo Molnardd41f592007-07-09 18:51:59 +02001389};
Peter Williams2dd73a42006-06-27 02:54:34 -07001390
Bharata B Raoef12fef2009-03-31 10:02:22 +05301391/* Time spent by the tasks of the cpu accounting group executing in ... */
1392enum cpuacct_stat_index {
1393 CPUACCT_STAT_USER, /* ... user mode */
1394 CPUACCT_STAT_SYSTEM, /* ... kernel mode */
1395
1396 CPUACCT_STAT_NSTATS,
1397};
1398
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01001399#ifdef CONFIG_CGROUP_CPUACCT
1400static void cpuacct_charge(struct task_struct *tsk, u64 cputime);
Bharata B Raoef12fef2009-03-31 10:02:22 +05301401static void cpuacct_update_stats(struct task_struct *tsk,
1402 enum cpuacct_stat_index idx, cputime_t val);
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01001403#else
1404static inline void cpuacct_charge(struct task_struct *tsk, u64 cputime) {}
Bharata B Raoef12fef2009-03-31 10:02:22 +05301405static inline void cpuacct_update_stats(struct task_struct *tsk,
1406 enum cpuacct_stat_index idx, cputime_t val) {}
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01001407#endif
1408
Peter Zijlstra18d95a22008-04-19 19:45:00 +02001409static inline void inc_cpu_load(struct rq *rq, unsigned long load)
1410{
1411 update_load_add(&rq->load, load);
1412}
1413
1414static inline void dec_cpu_load(struct rq *rq, unsigned long load)
1415{
1416 update_load_sub(&rq->load, load);
1417}
1418
Ingo Molnar7940ca32008-08-19 13:40:47 +02001419#if (defined(CONFIG_SMP) && defined(CONFIG_FAIR_GROUP_SCHED)) || defined(CONFIG_RT_GROUP_SCHED)
Peter Zijlstraeb755802008-08-19 12:33:05 +02001420typedef int (*tg_visitor)(struct task_group *, void *);
1421
1422/*
1423 * Iterate the full tree, calling @down when first entering a node and @up when
1424 * leaving it for the final time.
1425 */
1426static int walk_tg_tree(tg_visitor down, tg_visitor up, void *data)
1427{
1428 struct task_group *parent, *child;
1429 int ret;
1430
1431 rcu_read_lock();
1432 parent = &root_task_group;
1433down:
1434 ret = (*down)(parent, data);
1435 if (ret)
1436 goto out_unlock;
1437 list_for_each_entry_rcu(child, &parent->children, siblings) {
1438 parent = child;
1439 goto down;
1440
1441up:
1442 continue;
1443 }
1444 ret = (*up)(parent, data);
1445 if (ret)
1446 goto out_unlock;
1447
1448 child = parent;
1449 parent = parent->parent;
1450 if (parent)
1451 goto up;
1452out_unlock:
1453 rcu_read_unlock();
1454
1455 return ret;
1456}
1457
1458static int tg_nop(struct task_group *tg, void *data)
1459{
1460 return 0;
1461}
1462#endif
1463
Gregory Haskinse7693a32008-01-25 21:08:09 +01001464#ifdef CONFIG_SMP
Peter Zijlstraf5f08f32009-09-10 13:35:28 +02001465/* Used instead of source_load when we know the type == 0 */
1466static unsigned long weighted_cpuload(const int cpu)
1467{
1468 return cpu_rq(cpu)->load.weight;
1469}
1470
1471/*
1472 * Return a low guess at the load of a migration-source cpu weighted
1473 * according to the scheduling class and "nice" value.
1474 *
1475 * We want to under-estimate the load of migration sources, to
1476 * balance conservatively.
1477 */
1478static unsigned long source_load(int cpu, int type)
1479{
1480 struct rq *rq = cpu_rq(cpu);
1481 unsigned long total = weighted_cpuload(cpu);
1482
1483 if (type == 0 || !sched_feat(LB_BIAS))
1484 return total;
1485
1486 return min(rq->cpu_load[type-1], total);
1487}
1488
1489/*
1490 * Return a high guess at the load of a migration-target cpu weighted
1491 * according to the scheduling class and "nice" value.
1492 */
1493static unsigned long target_load(int cpu, int type)
1494{
1495 struct rq *rq = cpu_rq(cpu);
1496 unsigned long total = weighted_cpuload(cpu);
1497
1498 if (type == 0 || !sched_feat(LB_BIAS))
1499 return total;
1500
1501 return max(rq->cpu_load[type-1], total);
1502}
1503
Peter Zijlstraae154be2009-09-10 14:40:57 +02001504static unsigned long power_of(int cpu)
1505{
Peter Zijlstrae51fd5e2010-05-31 12:37:30 +02001506 return cpu_rq(cpu)->cpu_power;
Peter Zijlstraae154be2009-09-10 14:40:57 +02001507}
1508
Gregory Haskinse7693a32008-01-25 21:08:09 +01001509static int task_hot(struct task_struct *p, u64 now, struct sched_domain *sd);
Peter Zijlstrac09595f2008-06-27 13:41:14 +02001510
Peter Zijlstraa8a51d52008-06-27 13:41:26 +02001511static unsigned long cpu_avg_load_per_task(int cpu)
1512{
1513 struct rq *rq = cpu_rq(cpu);
Ingo Molnaraf6d5962008-11-29 20:45:15 +01001514 unsigned long nr_running = ACCESS_ONCE(rq->nr_running);
Peter Zijlstraa8a51d52008-06-27 13:41:26 +02001515
Steven Rostedt4cd42622008-11-26 21:04:24 -05001516 if (nr_running)
1517 rq->avg_load_per_task = rq->load.weight / nr_running;
Balbir Singha2d47772008-11-12 16:19:00 +05301518 else
1519 rq->avg_load_per_task = 0;
Peter Zijlstraa8a51d52008-06-27 13:41:26 +02001520
1521 return rq->avg_load_per_task;
1522}
1523
Peter Zijlstrac09595f2008-06-27 13:41:14 +02001524#ifdef CONFIG_FAIR_GROUP_SCHED
1525
Tejun Heo43cf38e2010-02-02 14:38:57 +09001526static __read_mostly unsigned long __percpu *update_shares_data;
Peter Zijlstra34d76c42009-08-27 13:08:56 +02001527
Peter Zijlstrac09595f2008-06-27 13:41:14 +02001528static void __set_se_shares(struct sched_entity *se, unsigned long shares);
1529
1530/*
1531 * Calculate and set the cpu's group shares.
1532 */
Peter Zijlstra34d76c42009-08-27 13:08:56 +02001533static void update_group_shares_cpu(struct task_group *tg, int cpu,
1534 unsigned long sd_shares,
1535 unsigned long sd_rq_weight,
Jiri Kosina4a6cc4b2009-10-29 00:26:00 +09001536 unsigned long *usd_rq_weight)
Peter Zijlstrac09595f2008-06-27 13:41:14 +02001537{
Peter Zijlstra34d76c42009-08-27 13:08:56 +02001538 unsigned long shares, rq_weight;
Peter Zijlstraa5004272009-07-27 14:04:49 +02001539 int boost = 0;
Peter Zijlstrac09595f2008-06-27 13:41:14 +02001540
Jiri Kosina4a6cc4b2009-10-29 00:26:00 +09001541 rq_weight = usd_rq_weight[cpu];
Peter Zijlstraa5004272009-07-27 14:04:49 +02001542 if (!rq_weight) {
1543 boost = 1;
1544 rq_weight = NICE_0_LOAD;
1545 }
Peter Zijlstrac8cba852008-06-27 13:41:23 +02001546
Peter Zijlstrac09595f2008-06-27 13:41:14 +02001547 /*
Peter Zijlstraa8af7242009-08-21 13:58:54 +02001548 * \Sum_j shares_j * rq_weight_i
1549 * shares_i = -----------------------------
1550 * \Sum_j rq_weight_j
Peter Zijlstrac09595f2008-06-27 13:41:14 +02001551 */
Ken Chenec4e0e22008-11-18 22:41:57 -08001552 shares = (sd_shares * rq_weight) / sd_rq_weight;
Peter Zijlstraffda12a2008-10-17 19:27:02 +02001553 shares = clamp_t(unsigned long, shares, MIN_SHARES, MAX_SHARES);
Peter Zijlstrac09595f2008-06-27 13:41:14 +02001554
Peter Zijlstraffda12a2008-10-17 19:27:02 +02001555 if (abs(shares - tg->se[cpu]->load.weight) >
1556 sysctl_sched_shares_thresh) {
1557 struct rq *rq = cpu_rq(cpu);
1558 unsigned long flags;
Peter Zijlstrac09595f2008-06-27 13:41:14 +02001559
Thomas Gleixner05fa7852009-11-17 14:28:38 +01001560 raw_spin_lock_irqsave(&rq->lock, flags);
Peter Zijlstra34d76c42009-08-27 13:08:56 +02001561 tg->cfs_rq[cpu]->rq_weight = boost ? 0 : rq_weight;
Peter Zijlstraa5004272009-07-27 14:04:49 +02001562 tg->cfs_rq[cpu]->shares = boost ? 0 : shares;
Peter Zijlstraffda12a2008-10-17 19:27:02 +02001563 __set_se_shares(tg->se[cpu], shares);
Thomas Gleixner05fa7852009-11-17 14:28:38 +01001564 raw_spin_unlock_irqrestore(&rq->lock, flags);
Peter Zijlstraffda12a2008-10-17 19:27:02 +02001565 }
Peter Zijlstrac09595f2008-06-27 13:41:14 +02001566}
1567
1568/*
Peter Zijlstrac8cba852008-06-27 13:41:23 +02001569 * Re-compute the task group their per cpu shares over the given domain.
1570 * This needs to be done in a bottom-up fashion because the rq weight of a
1571 * parent group depends on the shares of its child groups.
Peter Zijlstrac09595f2008-06-27 13:41:14 +02001572 */
Peter Zijlstraeb755802008-08-19 12:33:05 +02001573static int tg_shares_up(struct task_group *tg, void *data)
Peter Zijlstrac09595f2008-06-27 13:41:14 +02001574{
Peter Zijlstracd8ad402009-12-03 18:00:07 +01001575 unsigned long weight, rq_weight = 0, sum_weight = 0, shares = 0;
Jiri Kosina4a6cc4b2009-10-29 00:26:00 +09001576 unsigned long *usd_rq_weight;
Peter Zijlstraeb755802008-08-19 12:33:05 +02001577 struct sched_domain *sd = data;
Peter Zijlstra34d76c42009-08-27 13:08:56 +02001578 unsigned long flags;
Peter Zijlstrac09595f2008-06-27 13:41:14 +02001579 int i;
1580
Peter Zijlstra34d76c42009-08-27 13:08:56 +02001581 if (!tg->se[0])
1582 return 0;
1583
1584 local_irq_save(flags);
Jiri Kosina4a6cc4b2009-10-29 00:26:00 +09001585 usd_rq_weight = per_cpu_ptr(update_shares_data, smp_processor_id());
Peter Zijlstra34d76c42009-08-27 13:08:56 +02001586
Rusty Russell758b2cd2008-11-25 02:35:04 +10301587 for_each_cpu(i, sched_domain_span(sd)) {
Peter Zijlstra34d76c42009-08-27 13:08:56 +02001588 weight = tg->cfs_rq[i]->load.weight;
Jiri Kosina4a6cc4b2009-10-29 00:26:00 +09001589 usd_rq_weight[i] = weight;
Peter Zijlstra34d76c42009-08-27 13:08:56 +02001590
Peter Zijlstracd8ad402009-12-03 18:00:07 +01001591 rq_weight += weight;
Ken Chenec4e0e22008-11-18 22:41:57 -08001592 /*
1593 * If there are currently no tasks on the cpu pretend there
1594 * is one of average load so that when a new task gets to
1595 * run here it will not get delayed by group starvation.
1596 */
Ken Chenec4e0e22008-11-18 22:41:57 -08001597 if (!weight)
1598 weight = NICE_0_LOAD;
1599
Peter Zijlstracd8ad402009-12-03 18:00:07 +01001600 sum_weight += weight;
Peter Zijlstrac8cba852008-06-27 13:41:23 +02001601 shares += tg->cfs_rq[i]->shares;
1602 }
1603
Peter Zijlstracd8ad402009-12-03 18:00:07 +01001604 if (!rq_weight)
1605 rq_weight = sum_weight;
1606
Peter Zijlstrac8cba852008-06-27 13:41:23 +02001607 if ((!shares && rq_weight) || shares > tg->shares)
1608 shares = tg->shares;
1609
1610 if (!sd->parent || !(sd->parent->flags & SD_LOAD_BALANCE))
1611 shares = tg->shares;
1612
Rusty Russell758b2cd2008-11-25 02:35:04 +10301613 for_each_cpu(i, sched_domain_span(sd))
Jiri Kosina4a6cc4b2009-10-29 00:26:00 +09001614 update_group_shares_cpu(tg, i, shares, rq_weight, usd_rq_weight);
Peter Zijlstra34d76c42009-08-27 13:08:56 +02001615
1616 local_irq_restore(flags);
Peter Zijlstraeb755802008-08-19 12:33:05 +02001617
1618 return 0;
Peter Zijlstrac09595f2008-06-27 13:41:14 +02001619}
1620
1621/*
Peter Zijlstrac8cba852008-06-27 13:41:23 +02001622 * Compute the cpu's hierarchical load factor for each task group.
1623 * This needs to be done in a top-down fashion because the load of a child
1624 * group is a fraction of its parents load.
Peter Zijlstrac09595f2008-06-27 13:41:14 +02001625 */
Peter Zijlstraeb755802008-08-19 12:33:05 +02001626static int tg_load_down(struct task_group *tg, void *data)
Peter Zijlstrac09595f2008-06-27 13:41:14 +02001627{
Peter Zijlstrac8cba852008-06-27 13:41:23 +02001628 unsigned long load;
Peter Zijlstraeb755802008-08-19 12:33:05 +02001629 long cpu = (long)data;
Peter Zijlstrac09595f2008-06-27 13:41:14 +02001630
Peter Zijlstrac8cba852008-06-27 13:41:23 +02001631 if (!tg->parent) {
1632 load = cpu_rq(cpu)->load.weight;
1633 } else {
1634 load = tg->parent->cfs_rq[cpu]->h_load;
1635 load *= tg->cfs_rq[cpu]->shares;
1636 load /= tg->parent->cfs_rq[cpu]->load.weight + 1;
1637 }
1638
1639 tg->cfs_rq[cpu]->h_load = load;
Peter Zijlstrac09595f2008-06-27 13:41:14 +02001640
Peter Zijlstraeb755802008-08-19 12:33:05 +02001641 return 0;
Peter Zijlstra4d8d5952008-06-27 13:41:19 +02001642}
1643
Peter Zijlstrac8cba852008-06-27 13:41:23 +02001644static void update_shares(struct sched_domain *sd)
Peter Zijlstrac09595f2008-06-27 13:41:14 +02001645{
Peter Zijlstrae7097152009-06-03 15:41:20 +02001646 s64 elapsed;
1647 u64 now;
1648
1649 if (root_task_group_empty())
1650 return;
1651
1652 now = cpu_clock(raw_smp_processor_id());
1653 elapsed = now - sd->last_update;
Peter Zijlstra2398f2c2008-06-27 13:41:35 +02001654
1655 if (elapsed >= (s64)(u64)sysctl_sched_shares_ratelimit) {
1656 sd->last_update = now;
Peter Zijlstraeb755802008-08-19 12:33:05 +02001657 walk_tg_tree(tg_nop, tg_shares_up, sd);
Peter Zijlstra2398f2c2008-06-27 13:41:35 +02001658 }
Peter Zijlstrac09595f2008-06-27 13:41:14 +02001659}
1660
Peter Zijlstraeb755802008-08-19 12:33:05 +02001661static void update_h_load(long cpu)
Peter Zijlstrac09595f2008-06-27 13:41:14 +02001662{
Peter Zijlstrae7097152009-06-03 15:41:20 +02001663 if (root_task_group_empty())
1664 return;
1665
Peter Zijlstraeb755802008-08-19 12:33:05 +02001666 walk_tg_tree(tg_load_down, tg_nop, (void *)cpu);
Peter Zijlstrac09595f2008-06-27 13:41:14 +02001667}
1668
Peter Zijlstrac09595f2008-06-27 13:41:14 +02001669#else
1670
Peter Zijlstrac8cba852008-06-27 13:41:23 +02001671static inline void update_shares(struct sched_domain *sd)
Peter Zijlstrac09595f2008-06-27 13:41:14 +02001672{
1673}
1674
Peter Zijlstrac09595f2008-06-27 13:41:14 +02001675#endif
1676
Gregory Haskins8f45e2b2008-12-29 09:39:51 -05001677#ifdef CONFIG_PREEMPT
1678
Peter Zijlstrab78bb862009-09-15 14:23:18 +02001679static void double_rq_lock(struct rq *rq1, struct rq *rq2);
1680
Alexey Dobriyan70574a92008-11-28 22:08:00 +03001681/*
Gregory Haskins8f45e2b2008-12-29 09:39:51 -05001682 * fair double_lock_balance: Safely acquires both rq->locks in a fair
1683 * way at the expense of forcing extra atomic operations in all
1684 * invocations. This assures that the double_lock is acquired using the
1685 * same underlying policy as the spinlock_t on this architecture, which
1686 * reduces latency compared to the unfair variant below. However, it
1687 * also adds more overhead and therefore may reduce throughput.
Alexey Dobriyan70574a92008-11-28 22:08:00 +03001688 */
Gregory Haskins8f45e2b2008-12-29 09:39:51 -05001689static inline int _double_lock_balance(struct rq *this_rq, struct rq *busiest)
1690 __releases(this_rq->lock)
1691 __acquires(busiest->lock)
1692 __acquires(this_rq->lock)
1693{
Thomas Gleixner05fa7852009-11-17 14:28:38 +01001694 raw_spin_unlock(&this_rq->lock);
Gregory Haskins8f45e2b2008-12-29 09:39:51 -05001695 double_rq_lock(this_rq, busiest);
1696
1697 return 1;
1698}
1699
1700#else
1701/*
1702 * Unfair double_lock_balance: Optimizes throughput at the expense of
1703 * latency by eliminating extra atomic operations when the locks are
1704 * already in proper order on entry. This favors lower cpu-ids and will
1705 * grant the double lock to lower cpus over higher ids under contention,
1706 * regardless of entry order into the function.
1707 */
1708static int _double_lock_balance(struct rq *this_rq, struct rq *busiest)
Alexey Dobriyan70574a92008-11-28 22:08:00 +03001709 __releases(this_rq->lock)
1710 __acquires(busiest->lock)
1711 __acquires(this_rq->lock)
1712{
1713 int ret = 0;
1714
Thomas Gleixner05fa7852009-11-17 14:28:38 +01001715 if (unlikely(!raw_spin_trylock(&busiest->lock))) {
Alexey Dobriyan70574a92008-11-28 22:08:00 +03001716 if (busiest < this_rq) {
Thomas Gleixner05fa7852009-11-17 14:28:38 +01001717 raw_spin_unlock(&this_rq->lock);
1718 raw_spin_lock(&busiest->lock);
1719 raw_spin_lock_nested(&this_rq->lock,
1720 SINGLE_DEPTH_NESTING);
Alexey Dobriyan70574a92008-11-28 22:08:00 +03001721 ret = 1;
1722 } else
Thomas Gleixner05fa7852009-11-17 14:28:38 +01001723 raw_spin_lock_nested(&busiest->lock,
1724 SINGLE_DEPTH_NESTING);
Alexey Dobriyan70574a92008-11-28 22:08:00 +03001725 }
1726 return ret;
1727}
1728
Gregory Haskins8f45e2b2008-12-29 09:39:51 -05001729#endif /* CONFIG_PREEMPT */
1730
1731/*
1732 * double_lock_balance - lock the busiest runqueue, this_rq is locked already.
1733 */
1734static int double_lock_balance(struct rq *this_rq, struct rq *busiest)
1735{
1736 if (unlikely(!irqs_disabled())) {
1737 /* printk() doesn't work good under rq->lock */
Thomas Gleixner05fa7852009-11-17 14:28:38 +01001738 raw_spin_unlock(&this_rq->lock);
Gregory Haskins8f45e2b2008-12-29 09:39:51 -05001739 BUG_ON(1);
1740 }
1741
1742 return _double_lock_balance(this_rq, busiest);
1743}
1744
Alexey Dobriyan70574a92008-11-28 22:08:00 +03001745static inline void double_unlock_balance(struct rq *this_rq, struct rq *busiest)
1746 __releases(busiest->lock)
1747{
Thomas Gleixner05fa7852009-11-17 14:28:38 +01001748 raw_spin_unlock(&busiest->lock);
Alexey Dobriyan70574a92008-11-28 22:08:00 +03001749 lock_set_subclass(&this_rq->lock.dep_map, 0, _RET_IP_);
1750}
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01001751
1752/*
1753 * double_rq_lock - safely lock two runqueues
1754 *
1755 * Note this does not disable interrupts like task_rq_lock,
1756 * you need to do so manually before calling.
1757 */
1758static void double_rq_lock(struct rq *rq1, struct rq *rq2)
1759 __acquires(rq1->lock)
1760 __acquires(rq2->lock)
1761{
1762 BUG_ON(!irqs_disabled());
1763 if (rq1 == rq2) {
1764 raw_spin_lock(&rq1->lock);
1765 __acquire(rq2->lock); /* Fake it out ;) */
1766 } else {
1767 if (rq1 < rq2) {
1768 raw_spin_lock(&rq1->lock);
1769 raw_spin_lock_nested(&rq2->lock, SINGLE_DEPTH_NESTING);
1770 } else {
1771 raw_spin_lock(&rq2->lock);
1772 raw_spin_lock_nested(&rq1->lock, SINGLE_DEPTH_NESTING);
1773 }
1774 }
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01001775}
1776
1777/*
1778 * double_rq_unlock - safely unlock two runqueues
1779 *
1780 * Note this does not restore interrupts like task_rq_unlock,
1781 * you need to do so manually after calling.
1782 */
1783static void double_rq_unlock(struct rq *rq1, struct rq *rq2)
1784 __releases(rq1->lock)
1785 __releases(rq2->lock)
1786{
1787 raw_spin_unlock(&rq1->lock);
1788 if (rq1 != rq2)
1789 raw_spin_unlock(&rq2->lock);
1790 else
1791 __release(rq2->lock);
1792}
1793
Peter Zijlstra18d95a22008-04-19 19:45:00 +02001794#endif
Peter Zijlstra18d95a22008-04-19 19:45:00 +02001795
1796#ifdef CONFIG_FAIR_GROUP_SCHED
1797static void cfs_rq_set_shares(struct cfs_rq *cfs_rq, unsigned long shares)
1798{
Vegard Nossum30432092008-06-27 21:35:50 +02001799#ifdef CONFIG_SMP
Ingo Molnar34e83e82008-06-27 15:42:36 +02001800 cfs_rq->shares = shares;
1801#endif
Peter Zijlstra18d95a22008-04-19 19:45:00 +02001802}
1803#endif
1804
Peter Zijlstra74f51872010-04-22 21:50:19 +02001805static void calc_load_account_idle(struct rq *this_rq);
Christian Ehrhardt0bcdcf22009-11-30 12:16:46 +01001806static void update_sysctl(void);
Christian Ehrhardtacb4a842009-11-30 12:16:48 +01001807static int get_update_sysctl_factor(void);
Thomas Gleixnerdce48a82009-04-11 10:43:41 +02001808
Peter Zijlstracd29fe62009-11-27 17:32:46 +01001809static inline void __set_task_cpu(struct task_struct *p, unsigned int cpu)
1810{
1811 set_task_rq(p, cpu);
1812#ifdef CONFIG_SMP
1813 /*
1814 * After ->cpu is set up to a new value, task_rq_lock(p, ...) can be
1815 * successfuly executed on another CPU. We must ensure that updates of
1816 * per-task data have been completed by this moment.
1817 */
1818 smp_wmb();
1819 task_thread_info(p)->cpu = cpu;
1820#endif
1821}
Gregory Haskinse7693a32008-01-25 21:08:09 +01001822
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01001823static const struct sched_class rt_sched_class;
Ingo Molnardd41f592007-07-09 18:51:59 +02001824
1825#define sched_class_highest (&rt_sched_class)
Gregory Haskins1f11eb62008-06-04 15:04:05 -04001826#define for_each_class(class) \
1827 for (class = sched_class_highest; class; class = class->next)
Ingo Molnardd41f592007-07-09 18:51:59 +02001828
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01001829#include "sched_stats.h"
1830
Peter Zijlstrac09595f2008-06-27 13:41:14 +02001831static void inc_nr_running(struct rq *rq)
Ingo Molnar6363ca52008-05-29 11:28:57 +02001832{
1833 rq->nr_running++;
Ingo Molnar6363ca52008-05-29 11:28:57 +02001834}
1835
Peter Zijlstrac09595f2008-06-27 13:41:14 +02001836static void dec_nr_running(struct rq *rq)
Ingo Molnar9c217242007-08-02 17:41:40 +02001837{
1838 rq->nr_running--;
Ingo Molnar9c217242007-08-02 17:41:40 +02001839}
1840
Ingo Molnar45bf76d2007-07-09 18:51:59 +02001841static void set_load_weight(struct task_struct *p)
1842{
1843 if (task_has_rt_policy(p)) {
Peter Zijlstrae51fd5e2010-05-31 12:37:30 +02001844 p->se.load.weight = 0;
1845 p->se.load.inv_weight = WMULT_CONST;
Ingo Molnardd41f592007-07-09 18:51:59 +02001846 return;
1847 }
1848
1849 /*
1850 * SCHED_IDLE tasks get minimal weight:
1851 */
1852 if (p->policy == SCHED_IDLE) {
1853 p->se.load.weight = WEIGHT_IDLEPRIO;
1854 p->se.load.inv_weight = WMULT_IDLEPRIO;
1855 return;
1856 }
1857
1858 p->se.load.weight = prio_to_weight[p->static_prio - MAX_RT_PRIO];
1859 p->se.load.inv_weight = prio_to_wmult[p->static_prio - MAX_RT_PRIO];
Ingo Molnar45bf76d2007-07-09 18:51:59 +02001860}
1861
Peter Zijlstra371fd7e2010-03-24 16:38:48 +01001862static void enqueue_task(struct rq *rq, struct task_struct *p, int flags)
Gregory Haskins2087a1a2008-06-27 14:30:00 -06001863{
Mike Galbraitha64692a2010-03-11 17:16:20 +01001864 update_rq_clock(rq);
Ingo Molnar71f8bd42007-07-09 18:51:59 +02001865 sched_info_queued(p);
Peter Zijlstra371fd7e2010-03-24 16:38:48 +01001866 p->sched_class->enqueue_task(rq, p, flags);
Ingo Molnardd41f592007-07-09 18:51:59 +02001867 p->se.on_rq = 1;
1868}
1869
Peter Zijlstra371fd7e2010-03-24 16:38:48 +01001870static void dequeue_task(struct rq *rq, struct task_struct *p, int flags)
Ingo Molnardd41f592007-07-09 18:51:59 +02001871{
Mike Galbraitha64692a2010-03-11 17:16:20 +01001872 update_rq_clock(rq);
Ankita Garg46ac22b2008-07-01 14:30:06 +05301873 sched_info_dequeued(p);
Peter Zijlstra371fd7e2010-03-24 16:38:48 +01001874 p->sched_class->dequeue_task(rq, p, flags);
Ingo Molnardd41f592007-07-09 18:51:59 +02001875 p->se.on_rq = 0;
Ingo Molnar71f8bd42007-07-09 18:51:59 +02001876}
1877
1878/*
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01001879 * activate_task - move a task to the runqueue.
1880 */
Peter Zijlstra371fd7e2010-03-24 16:38:48 +01001881static void activate_task(struct rq *rq, struct task_struct *p, int flags)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01001882{
1883 if (task_contributes_to_load(p))
1884 rq->nr_uninterruptible--;
1885
Peter Zijlstra371fd7e2010-03-24 16:38:48 +01001886 enqueue_task(rq, p, flags);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01001887 inc_nr_running(rq);
1888}
1889
1890/*
1891 * deactivate_task - remove a task from the runqueue.
1892 */
Peter Zijlstra371fd7e2010-03-24 16:38:48 +01001893static void deactivate_task(struct rq *rq, struct task_struct *p, int flags)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01001894{
1895 if (task_contributes_to_load(p))
1896 rq->nr_uninterruptible++;
1897
Peter Zijlstra371fd7e2010-03-24 16:38:48 +01001898 dequeue_task(rq, p, flags);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01001899 dec_nr_running(rq);
1900}
1901
1902#include "sched_idletask.c"
1903#include "sched_fair.c"
1904#include "sched_rt.c"
1905#ifdef CONFIG_SCHED_DEBUG
1906# include "sched_debug.c"
1907#endif
1908
1909/*
Ingo Molnardd41f592007-07-09 18:51:59 +02001910 * __normal_prio - return the priority that is based on the static prio
Ingo Molnar71f8bd42007-07-09 18:51:59 +02001911 */
Ingo Molnar14531182007-07-09 18:51:59 +02001912static inline int __normal_prio(struct task_struct *p)
1913{
Ingo Molnardd41f592007-07-09 18:51:59 +02001914 return p->static_prio;
Ingo Molnar14531182007-07-09 18:51:59 +02001915}
1916
1917/*
Ingo Molnarb29739f2006-06-27 02:54:51 -07001918 * Calculate the expected normal priority: i.e. priority
1919 * without taking RT-inheritance into account. Might be
1920 * boosted by interactivity modifiers. Changes upon fork,
1921 * setprio syscalls, and whenever the interactivity
1922 * estimator recalculates.
1923 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07001924static inline int normal_prio(struct task_struct *p)
Ingo Molnarb29739f2006-06-27 02:54:51 -07001925{
1926 int prio;
1927
Ingo Molnare05606d2007-07-09 18:51:59 +02001928 if (task_has_rt_policy(p))
Ingo Molnarb29739f2006-06-27 02:54:51 -07001929 prio = MAX_RT_PRIO-1 - p->rt_priority;
1930 else
1931 prio = __normal_prio(p);
1932 return prio;
1933}
1934
1935/*
1936 * Calculate the current priority, i.e. the priority
1937 * taken into account by the scheduler. This value might
1938 * be boosted by RT tasks, or might be boosted by
1939 * interactivity modifiers. Will be RT if the task got
1940 * RT-boosted. If not then it returns p->normal_prio.
1941 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07001942static int effective_prio(struct task_struct *p)
Ingo Molnarb29739f2006-06-27 02:54:51 -07001943{
1944 p->normal_prio = normal_prio(p);
1945 /*
1946 * If we are RT tasks or we were boosted to RT priority,
1947 * keep the priority unchanged. Otherwise, update priority
1948 * to the normal priority:
1949 */
1950 if (!rt_prio(p->prio))
1951 return p->normal_prio;
1952 return p->prio;
1953}
1954
Linus Torvalds1da177e2005-04-16 15:20:36 -07001955/**
1956 * task_curr - is this task currently executing on a CPU?
1957 * @p: the task in question.
1958 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07001959inline int task_curr(const struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001960{
1961 return cpu_curr(task_cpu(p)) == p;
1962}
1963
Steven Rostedtcb469842008-01-25 21:08:22 +01001964static inline void check_class_changed(struct rq *rq, struct task_struct *p,
1965 const struct sched_class *prev_class,
1966 int oldprio, int running)
1967{
1968 if (prev_class != p->sched_class) {
1969 if (prev_class->switched_from)
1970 prev_class->switched_from(rq, p, running);
1971 p->sched_class->switched_to(rq, p, running);
1972 } else
1973 p->sched_class->prio_changed(rq, p, oldprio, running);
1974}
1975
Linus Torvalds1da177e2005-04-16 15:20:36 -07001976#ifdef CONFIG_SMP
Ingo Molnarcc367732007-10-15 17:00:18 +02001977/*
1978 * Is this task likely cache-hot:
1979 */
Gregory Haskinse7693a32008-01-25 21:08:09 +01001980static int
Ingo Molnarcc367732007-10-15 17:00:18 +02001981task_hot(struct task_struct *p, u64 now, struct sched_domain *sd)
1982{
1983 s64 delta;
1984
Peter Zijlstrae6c8fba2009-12-16 18:04:33 +01001985 if (p->sched_class != &fair_sched_class)
1986 return 0;
1987
Ingo Molnarf540a602008-03-15 17:10:34 +01001988 /*
1989 * Buddy candidates are cache hot:
1990 */
Mike Galbraithf685cea2009-10-23 23:09:22 +02001991 if (sched_feat(CACHE_HOT_BUDDY) && this_rq()->nr_running &&
Peter Zijlstra47932412008-11-04 21:25:09 +01001992 (&p->se == cfs_rq_of(&p->se)->next ||
1993 &p->se == cfs_rq_of(&p->se)->last))
Ingo Molnarf540a602008-03-15 17:10:34 +01001994 return 1;
1995
Ingo Molnar6bc16652007-10-15 17:00:18 +02001996 if (sysctl_sched_migration_cost == -1)
1997 return 1;
1998 if (sysctl_sched_migration_cost == 0)
1999 return 0;
2000
Ingo Molnarcc367732007-10-15 17:00:18 +02002001 delta = now - p->se.exec_start;
2002
2003 return delta < (s64)sysctl_sched_migration_cost;
2004}
2005
Ingo Molnardd41f592007-07-09 18:51:59 +02002006void set_task_cpu(struct task_struct *p, unsigned int new_cpu)
Ingo Molnarc65cc872007-07-09 18:51:58 +02002007{
Peter Zijlstrae2912002009-12-16 18:04:36 +01002008#ifdef CONFIG_SCHED_DEBUG
2009 /*
2010 * We should never call set_task_cpu() on a blocked task,
2011 * ttwu() will sort out the placement.
2012 */
Peter Zijlstra077614e2009-12-17 13:16:31 +01002013 WARN_ON_ONCE(p->state != TASK_RUNNING && p->state != TASK_WAKING &&
2014 !(task_thread_info(p)->preempt_count & PREEMPT_ACTIVE));
Peter Zijlstrae2912002009-12-16 18:04:36 +01002015#endif
2016
Mathieu Desnoyersde1d7282009-05-05 16:49:59 +08002017 trace_sched_migrate_task(p, new_cpu);
Peter Zijlstracbc34ed2008-12-10 08:08:22 +01002018
Peter Zijlstra0c697742009-12-22 15:43:19 +01002019 if (task_cpu(p) != new_cpu) {
2020 p->se.nr_migrations++;
2021 perf_sw_event(PERF_COUNT_SW_CPU_MIGRATIONS, 1, 1, NULL, 0);
2022 }
Ingo Molnardd41f592007-07-09 18:51:59 +02002023
2024 __set_task_cpu(p, new_cpu);
Ingo Molnarc65cc872007-07-09 18:51:58 +02002025}
2026
Tejun Heo969c7922010-05-06 18:49:21 +02002027struct migration_arg {
Ingo Molnar36c8b582006-07-03 00:25:41 -07002028 struct task_struct *task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002029 int dest_cpu;
Ingo Molnar70b97a72006-07-03 00:25:42 -07002030};
Linus Torvalds1da177e2005-04-16 15:20:36 -07002031
Tejun Heo969c7922010-05-06 18:49:21 +02002032static int migration_cpu_stop(void *data);
2033
Linus Torvalds1da177e2005-04-16 15:20:36 -07002034/*
2035 * The task's runqueue lock must be held.
2036 * Returns true if you have to wait for migration thread.
2037 */
Tejun Heo969c7922010-05-06 18:49:21 +02002038static bool migrate_task(struct task_struct *p, int dest_cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002039{
Ingo Molnar70b97a72006-07-03 00:25:42 -07002040 struct rq *rq = task_rq(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002041
2042 /*
2043 * If the task is not on a runqueue (and not running), then
Peter Zijlstrae2912002009-12-16 18:04:36 +01002044 * the next wake-up will properly place the task.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002045 */
Tejun Heo969c7922010-05-06 18:49:21 +02002046 return p->se.on_rq || task_running(rq, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002047}
2048
2049/*
2050 * wait_task_inactive - wait for a thread to unschedule.
2051 *
Roland McGrath85ba2d82008-07-25 19:45:58 -07002052 * If @match_state is nonzero, it's the @p->state value just checked and
2053 * not expected to change. If it changes, i.e. @p might have woken up,
2054 * then return zero. When we succeed in waiting for @p to be off its CPU,
2055 * we return a positive number (its total switch count). If a second call
2056 * a short while later returns the same number, the caller can be sure that
2057 * @p has remained unscheduled the whole time.
2058 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002059 * The caller must ensure that the task *will* unschedule sometime soon,
2060 * else this function might spin for a *long* time. This function can't
2061 * be called with interrupts off, or it may introduce deadlock with
2062 * smp_call_function() if an IPI is sent by the same process we are
2063 * waiting to become inactive.
2064 */
Roland McGrath85ba2d82008-07-25 19:45:58 -07002065unsigned long wait_task_inactive(struct task_struct *p, long match_state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002066{
2067 unsigned long flags;
Ingo Molnardd41f592007-07-09 18:51:59 +02002068 int running, on_rq;
Roland McGrath85ba2d82008-07-25 19:45:58 -07002069 unsigned long ncsw;
Ingo Molnar70b97a72006-07-03 00:25:42 -07002070 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002071
Andi Kleen3a5c3592007-10-15 17:00:14 +02002072 for (;;) {
2073 /*
2074 * We do the initial early heuristics without holding
2075 * any task-queue locks at all. We'll only try to get
2076 * the runqueue lock when things look like they will
2077 * work out!
2078 */
2079 rq = task_rq(p);
Linus Torvaldsfa490cf2007-06-18 09:34:40 -07002080
Andi Kleen3a5c3592007-10-15 17:00:14 +02002081 /*
2082 * If the task is actively running on another CPU
2083 * still, just relax and busy-wait without holding
2084 * any locks.
2085 *
2086 * NOTE! Since we don't hold any locks, it's not
2087 * even sure that "rq" stays as the right runqueue!
2088 * But we don't care, since "task_running()" will
2089 * return false if the runqueue has changed and p
2090 * is actually now running somewhere else!
2091 */
Roland McGrath85ba2d82008-07-25 19:45:58 -07002092 while (task_running(rq, p)) {
2093 if (match_state && unlikely(p->state != match_state))
2094 return 0;
Andi Kleen3a5c3592007-10-15 17:00:14 +02002095 cpu_relax();
Roland McGrath85ba2d82008-07-25 19:45:58 -07002096 }
Linus Torvaldsfa490cf2007-06-18 09:34:40 -07002097
Andi Kleen3a5c3592007-10-15 17:00:14 +02002098 /*
2099 * Ok, time to look more closely! We need the rq
2100 * lock now, to be *sure*. If we're wrong, we'll
2101 * just go back and repeat.
2102 */
2103 rq = task_rq_lock(p, &flags);
Peter Zijlstra27a9da62010-05-04 20:36:56 +02002104 trace_sched_wait_task(p);
Andi Kleen3a5c3592007-10-15 17:00:14 +02002105 running = task_running(rq, p);
2106 on_rq = p->se.on_rq;
Roland McGrath85ba2d82008-07-25 19:45:58 -07002107 ncsw = 0;
Oleg Nesterovf31e11d2008-08-20 16:54:44 -07002108 if (!match_state || p->state == match_state)
Oleg Nesterov93dcf552008-08-20 16:54:44 -07002109 ncsw = p->nvcsw | LONG_MIN; /* sets MSB */
Andi Kleen3a5c3592007-10-15 17:00:14 +02002110 task_rq_unlock(rq, &flags);
Linus Torvaldsfa490cf2007-06-18 09:34:40 -07002111
Andi Kleen3a5c3592007-10-15 17:00:14 +02002112 /*
Roland McGrath85ba2d82008-07-25 19:45:58 -07002113 * If it changed from the expected state, bail out now.
2114 */
2115 if (unlikely(!ncsw))
2116 break;
2117
2118 /*
Andi Kleen3a5c3592007-10-15 17:00:14 +02002119 * Was it really running after all now that we
2120 * checked with the proper locks actually held?
2121 *
2122 * Oops. Go back and try again..
2123 */
2124 if (unlikely(running)) {
2125 cpu_relax();
2126 continue;
2127 }
2128
2129 /*
2130 * It's not enough that it's not actively running,
2131 * it must be off the runqueue _entirely_, and not
2132 * preempted!
2133 *
Luis Henriques80dd99b2009-03-16 19:58:09 +00002134 * So if it was still runnable (but just not actively
Andi Kleen3a5c3592007-10-15 17:00:14 +02002135 * running right now), it's preempted, and we should
2136 * yield - it could be a while.
2137 */
2138 if (unlikely(on_rq)) {
2139 schedule_timeout_uninterruptible(1);
2140 continue;
2141 }
2142
2143 /*
2144 * Ahh, all good. It wasn't running, and it wasn't
2145 * runnable, which means that it will never become
2146 * running in the future either. We're all done!
2147 */
2148 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002149 }
Roland McGrath85ba2d82008-07-25 19:45:58 -07002150
2151 return ncsw;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002152}
2153
2154/***
2155 * kick_process - kick a running thread to enter/exit the kernel
2156 * @p: the to-be-kicked thread
2157 *
2158 * Cause a process which is running on another CPU to enter
2159 * kernel-mode, without any delay. (to get signals handled.)
2160 *
2161 * NOTE: this function doesnt have to take the runqueue lock,
2162 * because all it wants to ensure is that the remote task enters
2163 * the kernel. If the IPI races and the task has been migrated
2164 * to another CPU then no harm is done and the purpose has been
2165 * achieved as well.
2166 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07002167void kick_process(struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002168{
2169 int cpu;
2170
2171 preempt_disable();
2172 cpu = task_cpu(p);
2173 if ((cpu != smp_processor_id()) && task_curr(p))
2174 smp_send_reschedule(cpu);
2175 preempt_enable();
2176}
Rusty Russellb43e3522009-06-12 22:27:00 -06002177EXPORT_SYMBOL_GPL(kick_process);
Nick Piggin476d1392005-06-25 14:57:29 -07002178#endif /* CONFIG_SMP */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002179
Thomas Gleixner0793a612008-12-04 20:12:29 +01002180/**
2181 * task_oncpu_function_call - call a function on the cpu on which a task runs
2182 * @p: the task to evaluate
2183 * @func: the function to be called
2184 * @info: the function call argument
2185 *
2186 * Calls the function @func when the task is currently running. This might
2187 * be on the current CPU, which just calls the function directly
2188 */
2189void task_oncpu_function_call(struct task_struct *p,
2190 void (*func) (void *info), void *info)
2191{
2192 int cpu;
2193
2194 preempt_disable();
2195 cpu = task_cpu(p);
2196 if (task_curr(p))
2197 smp_call_function_single(cpu, func, info, 1);
2198 preempt_enable();
2199}
2200
Peter Zijlstra970b13b2009-11-25 13:31:39 +01002201#ifdef CONFIG_SMP
Oleg Nesterov30da6882010-03-15 10:10:19 +01002202/*
2203 * ->cpus_allowed is protected by either TASK_WAKING or rq->lock held.
2204 */
Peter Zijlstra5da9a0f2009-12-16 18:04:38 +01002205static int select_fallback_rq(int cpu, struct task_struct *p)
2206{
2207 int dest_cpu;
2208 const struct cpumask *nodemask = cpumask_of_node(cpu_to_node(cpu));
2209
2210 /* Look for allowed, online CPU in same node. */
2211 for_each_cpu_and(dest_cpu, nodemask, cpu_active_mask)
2212 if (cpumask_test_cpu(dest_cpu, &p->cpus_allowed))
2213 return dest_cpu;
2214
2215 /* Any allowed, online CPU? */
2216 dest_cpu = cpumask_any_and(&p->cpus_allowed, cpu_active_mask);
2217 if (dest_cpu < nr_cpu_ids)
2218 return dest_cpu;
2219
2220 /* No more Mr. Nice Guy. */
Oleg Nesterov897f0b32010-03-15 10:10:03 +01002221 if (unlikely(dest_cpu >= nr_cpu_ids)) {
Oleg Nesterov9084bb82010-03-15 10:10:27 +01002222 dest_cpu = cpuset_cpus_allowed_fallback(p);
Peter Zijlstra5da9a0f2009-12-16 18:04:38 +01002223 /*
2224 * Don't tell them about moving exiting tasks or
2225 * kernel threads (both mm NULL), since they never
2226 * leave kernel.
2227 */
2228 if (p->mm && printk_ratelimit()) {
2229 printk(KERN_INFO "process %d (%s) no "
2230 "longer affine to cpu%d\n",
2231 task_pid_nr(p), p->comm, cpu);
2232 }
2233 }
2234
2235 return dest_cpu;
2236}
2237
Peter Zijlstrae2912002009-12-16 18:04:36 +01002238/*
Oleg Nesterov30da6882010-03-15 10:10:19 +01002239 * The caller (fork, wakeup) owns TASK_WAKING, ->cpus_allowed is stable.
Peter Zijlstrae2912002009-12-16 18:04:36 +01002240 */
Peter Zijlstra970b13b2009-11-25 13:31:39 +01002241static inline
Peter Zijlstra0017d732010-03-24 18:34:10 +01002242int select_task_rq(struct rq *rq, struct task_struct *p, int sd_flags, int wake_flags)
Peter Zijlstra970b13b2009-11-25 13:31:39 +01002243{
Peter Zijlstra0017d732010-03-24 18:34:10 +01002244 int cpu = p->sched_class->select_task_rq(rq, p, sd_flags, wake_flags);
Peter Zijlstrae2912002009-12-16 18:04:36 +01002245
2246 /*
2247 * In order not to call set_task_cpu() on a blocking task we need
2248 * to rely on ttwu() to place the task on a valid ->cpus_allowed
2249 * cpu.
2250 *
2251 * Since this is common to all placement strategies, this lives here.
2252 *
2253 * [ this allows ->select_task() to simply return task_cpu(p) and
2254 * not worry about this generic constraint ]
2255 */
2256 if (unlikely(!cpumask_test_cpu(cpu, &p->cpus_allowed) ||
Peter Zijlstra70f11202009-12-20 17:36:27 +01002257 !cpu_online(cpu)))
Peter Zijlstra5da9a0f2009-12-16 18:04:38 +01002258 cpu = select_fallback_rq(task_cpu(p), p);
Peter Zijlstrae2912002009-12-16 18:04:36 +01002259
2260 return cpu;
Peter Zijlstra970b13b2009-11-25 13:31:39 +01002261}
Mike Galbraith09a40af2010-04-15 07:29:59 +02002262
2263static void update_avg(u64 *avg, u64 sample)
2264{
2265 s64 diff = sample - *avg;
2266 *avg += diff >> 3;
2267}
Peter Zijlstra970b13b2009-11-25 13:31:39 +01002268#endif
2269
Linus Torvalds1da177e2005-04-16 15:20:36 -07002270/***
2271 * try_to_wake_up - wake up a thread
2272 * @p: the to-be-woken-up thread
2273 * @state: the mask of task states that can be woken
2274 * @sync: do a synchronous wakeup?
2275 *
2276 * Put it on the run-queue if it's not already there. The "current"
2277 * thread is always on the run-queue (except when the actual
2278 * re-schedule is in progress), and as such you're allowed to do
2279 * the simpler "current->state = TASK_RUNNING" to mark yourself
2280 * runnable without the overhead of this.
2281 *
2282 * returns failure only if the task is already active.
2283 */
Peter Zijlstra7d478722009-09-14 19:55:44 +02002284static int try_to_wake_up(struct task_struct *p, unsigned int state,
2285 int wake_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002286{
Ingo Molnarcc367732007-10-15 17:00:18 +02002287 int cpu, orig_cpu, this_cpu, success = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002288 unsigned long flags;
Peter Zijlstra371fd7e2010-03-24 16:38:48 +01002289 unsigned long en_flags = ENQUEUE_WAKEUP;
Dan Carpenterab3b3aa2010-03-06 14:17:52 +03002290 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002291
Peter Zijlstrae9c84312009-09-15 14:43:03 +02002292 this_cpu = get_cpu();
Peter Zijlstra2398f2c2008-06-27 13:41:35 +02002293
Linus Torvalds04e2f172008-02-23 18:05:03 -08002294 smp_wmb();
Dan Carpenterab3b3aa2010-03-06 14:17:52 +03002295 rq = task_rq_lock(p, &flags);
Peter Zijlstrae9c84312009-09-15 14:43:03 +02002296 if (!(p->state & state))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002297 goto out;
2298
Ingo Molnardd41f592007-07-09 18:51:59 +02002299 if (p->se.on_rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002300 goto out_running;
2301
2302 cpu = task_cpu(p);
Ingo Molnarcc367732007-10-15 17:00:18 +02002303 orig_cpu = cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002304
2305#ifdef CONFIG_SMP
2306 if (unlikely(task_running(rq, p)))
2307 goto out_activate;
2308
Peter Zijlstrae9c84312009-09-15 14:43:03 +02002309 /*
2310 * In order to handle concurrent wakeups and release the rq->lock
2311 * we put the task in TASK_WAKING state.
Ingo Molnareb24073b2009-09-16 21:09:13 +02002312 *
2313 * First fix up the nr_uninterruptible count:
Peter Zijlstrae9c84312009-09-15 14:43:03 +02002314 */
Peter Zijlstracc87f762010-03-26 12:22:14 +01002315 if (task_contributes_to_load(p)) {
2316 if (likely(cpu_online(orig_cpu)))
2317 rq->nr_uninterruptible--;
2318 else
2319 this_rq()->nr_uninterruptible--;
2320 }
Peter Zijlstrae9c84312009-09-15 14:43:03 +02002321 p->state = TASK_WAKING;
Peter Zijlstraefbbd052009-12-16 18:04:40 +01002322
Peter Zijlstra371fd7e2010-03-24 16:38:48 +01002323 if (p->sched_class->task_waking) {
Peter Zijlstraefbbd052009-12-16 18:04:40 +01002324 p->sched_class->task_waking(rq, p);
Peter Zijlstra371fd7e2010-03-24 16:38:48 +01002325 en_flags |= ENQUEUE_WAKING;
Peter Zijlstra0970d292010-02-15 14:45:54 +01002326 }
Peter Zijlstraab19cb22009-11-27 15:44:43 +01002327
Peter Zijlstra0017d732010-03-24 18:34:10 +01002328 cpu = select_task_rq(rq, p, SD_BALANCE_WAKE, wake_flags);
2329 if (cpu != orig_cpu)
Mike Galbraithf5dc3752009-10-09 08:35:03 +02002330 set_task_cpu(p, cpu);
Peter Zijlstra0017d732010-03-24 18:34:10 +01002331 __task_rq_unlock(rq);
Peter Zijlstraab19cb22009-11-27 15:44:43 +01002332
Peter Zijlstra0970d292010-02-15 14:45:54 +01002333 rq = cpu_rq(cpu);
2334 raw_spin_lock(&rq->lock);
Mike Galbraithf5dc3752009-10-09 08:35:03 +02002335
Peter Zijlstra0970d292010-02-15 14:45:54 +01002336 /*
2337 * We migrated the task without holding either rq->lock, however
2338 * since the task is not on the task list itself, nobody else
2339 * will try and migrate the task, hence the rq should match the
2340 * cpu we just moved it to.
2341 */
2342 WARN_ON(task_cpu(p) != cpu);
Peter Zijlstrae9c84312009-09-15 14:43:03 +02002343 WARN_ON(p->state != TASK_WAKING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002344
Gregory Haskinse7693a32008-01-25 21:08:09 +01002345#ifdef CONFIG_SCHEDSTATS
2346 schedstat_inc(rq, ttwu_count);
2347 if (cpu == this_cpu)
2348 schedstat_inc(rq, ttwu_local);
2349 else {
2350 struct sched_domain *sd;
2351 for_each_domain(this_cpu, sd) {
Rusty Russell758b2cd2008-11-25 02:35:04 +10302352 if (cpumask_test_cpu(cpu, sched_domain_span(sd))) {
Gregory Haskinse7693a32008-01-25 21:08:09 +01002353 schedstat_inc(sd, ttwu_wake_remote);
2354 break;
2355 }
2356 }
2357 }
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +02002358#endif /* CONFIG_SCHEDSTATS */
Gregory Haskinse7693a32008-01-25 21:08:09 +01002359
Linus Torvalds1da177e2005-04-16 15:20:36 -07002360out_activate:
2361#endif /* CONFIG_SMP */
Lucas De Marchi41acab82010-03-10 23:37:45 -03002362 schedstat_inc(p, se.statistics.nr_wakeups);
Peter Zijlstra7d478722009-09-14 19:55:44 +02002363 if (wake_flags & WF_SYNC)
Lucas De Marchi41acab82010-03-10 23:37:45 -03002364 schedstat_inc(p, se.statistics.nr_wakeups_sync);
Ingo Molnarcc367732007-10-15 17:00:18 +02002365 if (orig_cpu != cpu)
Lucas De Marchi41acab82010-03-10 23:37:45 -03002366 schedstat_inc(p, se.statistics.nr_wakeups_migrate);
Ingo Molnarcc367732007-10-15 17:00:18 +02002367 if (cpu == this_cpu)
Lucas De Marchi41acab82010-03-10 23:37:45 -03002368 schedstat_inc(p, se.statistics.nr_wakeups_local);
Ingo Molnarcc367732007-10-15 17:00:18 +02002369 else
Lucas De Marchi41acab82010-03-10 23:37:45 -03002370 schedstat_inc(p, se.statistics.nr_wakeups_remote);
Peter Zijlstra371fd7e2010-03-24 16:38:48 +01002371 activate_task(rq, p, en_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002372 success = 1;
2373
2374out_running:
Peter Zijlstra27a9da62010-05-04 20:36:56 +02002375 trace_sched_wakeup(p, success);
Peter Zijlstra7d478722009-09-14 19:55:44 +02002376 check_preempt_curr(rq, p, wake_flags);
Ingo Molnar4ae7d5c2008-03-19 01:42:00 +01002377
Linus Torvalds1da177e2005-04-16 15:20:36 -07002378 p->state = TASK_RUNNING;
Steven Rostedt9a897c52008-01-25 21:08:22 +01002379#ifdef CONFIG_SMP
Peter Zijlstraefbbd052009-12-16 18:04:40 +01002380 if (p->sched_class->task_woken)
2381 p->sched_class->task_woken(rq, p);
Mike Galbraitheae0c9d2009-11-10 03:50:02 +01002382
2383 if (unlikely(rq->idle_stamp)) {
2384 u64 delta = rq->clock - rq->idle_stamp;
2385 u64 max = 2*sysctl_sched_migration_cost;
2386
2387 if (delta > max)
2388 rq->avg_idle = max;
2389 else
2390 update_avg(&rq->avg_idle, delta);
2391 rq->idle_stamp = 0;
2392 }
Steven Rostedt9a897c52008-01-25 21:08:22 +01002393#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002394out:
2395 task_rq_unlock(rq, &flags);
Peter Zijlstrae9c84312009-09-15 14:43:03 +02002396 put_cpu();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002397
2398 return success;
2399}
2400
David Howells50fa6102009-04-28 15:01:38 +01002401/**
2402 * wake_up_process - Wake up a specific process
2403 * @p: The process to be woken up.
2404 *
2405 * Attempt to wake up the nominated process and move it to the set of runnable
2406 * processes. Returns 1 if the process was woken up, 0 if it was already
2407 * running.
2408 *
2409 * It may be assumed that this function implies a write memory barrier before
2410 * changing the task state if and only if any tasks are woken up.
2411 */
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -08002412int wake_up_process(struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002413{
Matthew Wilcoxd9514f62007-12-06 11:07:07 -05002414 return try_to_wake_up(p, TASK_ALL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002415}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002416EXPORT_SYMBOL(wake_up_process);
2417
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -08002418int wake_up_state(struct task_struct *p, unsigned int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002419{
2420 return try_to_wake_up(p, state, 0);
2421}
2422
Linus Torvalds1da177e2005-04-16 15:20:36 -07002423/*
2424 * Perform scheduler related setup for a newly forked process p.
2425 * p is forked by current.
Ingo Molnardd41f592007-07-09 18:51:59 +02002426 *
2427 * __sched_fork() is basic setup used by init_idle() too:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002428 */
Ingo Molnardd41f592007-07-09 18:51:59 +02002429static void __sched_fork(struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002430{
Ingo Molnardd41f592007-07-09 18:51:59 +02002431 p->se.exec_start = 0;
2432 p->se.sum_exec_runtime = 0;
Ingo Molnarf6cf8912007-08-28 12:53:24 +02002433 p->se.prev_sum_exec_runtime = 0;
Ingo Molnar6c594c22008-12-14 12:34:15 +01002434 p->se.nr_migrations = 0;
Ingo Molnar6cfb0d52007-08-02 17:41:40 +02002435
2436#ifdef CONFIG_SCHEDSTATS
Lucas De Marchi41acab82010-03-10 23:37:45 -03002437 memset(&p->se.statistics, 0, sizeof(p->se.statistics));
Ingo Molnar6cfb0d52007-08-02 17:41:40 +02002438#endif
Nick Piggin476d1392005-06-25 14:57:29 -07002439
Peter Zijlstrafa717062008-01-25 21:08:27 +01002440 INIT_LIST_HEAD(&p->rt.run_list);
Ingo Molnardd41f592007-07-09 18:51:59 +02002441 p->se.on_rq = 0;
Peter Zijlstra4a55bd52008-04-19 19:45:00 +02002442 INIT_LIST_HEAD(&p->se.group_node);
Nick Piggin476d1392005-06-25 14:57:29 -07002443
Avi Kivitye107be32007-07-26 13:40:43 +02002444#ifdef CONFIG_PREEMPT_NOTIFIERS
2445 INIT_HLIST_HEAD(&p->preempt_notifiers);
2446#endif
Ingo Molnardd41f592007-07-09 18:51:59 +02002447}
2448
2449/*
2450 * fork()/clone()-time setup:
2451 */
2452void sched_fork(struct task_struct *p, int clone_flags)
2453{
2454 int cpu = get_cpu();
2455
2456 __sched_fork(p);
Peter Zijlstra06b83b52009-12-16 18:04:35 +01002457 /*
Peter Zijlstra0017d732010-03-24 18:34:10 +01002458 * We mark the process as running here. This guarantees that
Peter Zijlstra06b83b52009-12-16 18:04:35 +01002459 * nobody will actually run it, and a signal or other external
2460 * event cannot wake it up and insert it on the runqueue either.
2461 */
Peter Zijlstra0017d732010-03-24 18:34:10 +01002462 p->state = TASK_RUNNING;
Ingo Molnardd41f592007-07-09 18:51:59 +02002463
Ingo Molnarb29739f2006-06-27 02:54:51 -07002464 /*
Mike Galbraithb9dc29e2009-06-17 10:46:01 +02002465 * Revert to default priority/policy on fork if requested.
2466 */
2467 if (unlikely(p->sched_reset_on_fork)) {
Peter Williamsf83f9ac2009-09-24 06:47:10 +00002468 if (p->policy == SCHED_FIFO || p->policy == SCHED_RR) {
Mike Galbraithb9dc29e2009-06-17 10:46:01 +02002469 p->policy = SCHED_NORMAL;
Peter Williamsf83f9ac2009-09-24 06:47:10 +00002470 p->normal_prio = p->static_prio;
2471 }
Mike Galbraithb9dc29e2009-06-17 10:46:01 +02002472
Mike Galbraith6c697bd2009-06-17 10:48:02 +02002473 if (PRIO_TO_NICE(p->static_prio) < 0) {
2474 p->static_prio = NICE_TO_PRIO(0);
Peter Williamsf83f9ac2009-09-24 06:47:10 +00002475 p->normal_prio = p->static_prio;
Mike Galbraith6c697bd2009-06-17 10:48:02 +02002476 set_load_weight(p);
2477 }
2478
Mike Galbraithb9dc29e2009-06-17 10:46:01 +02002479 /*
2480 * We don't need the reset flag anymore after the fork. It has
2481 * fulfilled its duty:
2482 */
2483 p->sched_reset_on_fork = 0;
2484 }
Lennart Poetteringca94c442009-06-15 17:17:47 +02002485
Peter Williamsf83f9ac2009-09-24 06:47:10 +00002486 /*
2487 * Make sure we do not leak PI boosting priority to the child.
2488 */
2489 p->prio = current->normal_prio;
2490
Hiroshi Shimamoto2ddbf952007-10-15 17:00:11 +02002491 if (!rt_prio(p->prio))
2492 p->sched_class = &fair_sched_class;
Ingo Molnarb29739f2006-06-27 02:54:51 -07002493
Peter Zijlstracd29fe62009-11-27 17:32:46 +01002494 if (p->sched_class->task_fork)
2495 p->sched_class->task_fork(p);
2496
Peter Zijlstra5f3edc12009-09-10 13:42:00 +02002497 set_task_cpu(p, cpu);
2498
Chandra Seetharaman52f17b62006-07-14 00:24:38 -07002499#if defined(CONFIG_SCHEDSTATS) || defined(CONFIG_TASK_DELAY_ACCT)
Ingo Molnardd41f592007-07-09 18:51:59 +02002500 if (likely(sched_info_on()))
Chandra Seetharaman52f17b62006-07-14 00:24:38 -07002501 memset(&p->sched_info, 0, sizeof(p->sched_info));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002502#endif
Chen, Kenneth Wd6077cb2006-02-14 13:53:10 -08002503#if defined(CONFIG_SMP) && defined(__ARCH_WANT_UNLOCKED_CTXSW)
Nick Piggin4866cde2005-06-25 14:57:23 -07002504 p->oncpu = 0;
2505#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002506#ifdef CONFIG_PREEMPT
Nick Piggin4866cde2005-06-25 14:57:23 -07002507 /* Want to start with kernel preemption disabled. */
Al Viroa1261f52005-11-13 16:06:55 -08002508 task_thread_info(p)->preempt_count = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002509#endif
Gregory Haskins917b6272008-12-29 09:39:53 -05002510 plist_node_init(&p->pushable_tasks, MAX_PRIO);
2511
Nick Piggin476d1392005-06-25 14:57:29 -07002512 put_cpu();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002513}
2514
2515/*
2516 * wake_up_new_task - wake up a newly created task for the first time.
2517 *
2518 * This function will do some initial scheduler statistics housekeeping
2519 * that must be done for every newly created context, then puts the task
2520 * on the runqueue and wakes it.
2521 */
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -08002522void wake_up_new_task(struct task_struct *p, unsigned long clone_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002523{
2524 unsigned long flags;
Ingo Molnardd41f592007-07-09 18:51:59 +02002525 struct rq *rq;
Andrew Mortonc8906922010-03-11 14:08:43 -08002526 int cpu __maybe_unused = get_cpu();
Peter Zijlstrafabf3182010-01-21 21:04:57 +01002527
2528#ifdef CONFIG_SMP
Peter Zijlstra0017d732010-03-24 18:34:10 +01002529 rq = task_rq_lock(p, &flags);
2530 p->state = TASK_WAKING;
2531
Peter Zijlstrafabf3182010-01-21 21:04:57 +01002532 /*
2533 * Fork balancing, do it here and not earlier because:
2534 * - cpus_allowed can change in the fork path
2535 * - any previously selected cpu might disappear through hotplug
2536 *
Peter Zijlstra0017d732010-03-24 18:34:10 +01002537 * We set TASK_WAKING so that select_task_rq() can drop rq->lock
2538 * without people poking at ->cpus_allowed.
Peter Zijlstrafabf3182010-01-21 21:04:57 +01002539 */
Peter Zijlstra0017d732010-03-24 18:34:10 +01002540 cpu = select_task_rq(rq, p, SD_BALANCE_FORK, 0);
Peter Zijlstrafabf3182010-01-21 21:04:57 +01002541 set_task_cpu(p, cpu);
Peter Zijlstra0017d732010-03-24 18:34:10 +01002542
2543 p->state = TASK_RUNNING;
2544 task_rq_unlock(rq, &flags);
Peter Zijlstrafabf3182010-01-21 21:04:57 +01002545#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002546
Peter Zijlstra0017d732010-03-24 18:34:10 +01002547 rq = task_rq_lock(p, &flags);
Peter Zijlstracd29fe62009-11-27 17:32:46 +01002548 activate_task(rq, p, 0);
Peter Zijlstra27a9da62010-05-04 20:36:56 +02002549 trace_sched_wakeup_new(p, 1);
Peter Zijlstraa7558e02009-09-14 20:02:34 +02002550 check_preempt_curr(rq, p, WF_FORK);
Steven Rostedt9a897c52008-01-25 21:08:22 +01002551#ifdef CONFIG_SMP
Peter Zijlstraefbbd052009-12-16 18:04:40 +01002552 if (p->sched_class->task_woken)
2553 p->sched_class->task_woken(rq, p);
Steven Rostedt9a897c52008-01-25 21:08:22 +01002554#endif
Ingo Molnardd41f592007-07-09 18:51:59 +02002555 task_rq_unlock(rq, &flags);
Peter Zijlstrafabf3182010-01-21 21:04:57 +01002556 put_cpu();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002557}
2558
Avi Kivitye107be32007-07-26 13:40:43 +02002559#ifdef CONFIG_PREEMPT_NOTIFIERS
2560
2561/**
Luis Henriques80dd99b2009-03-16 19:58:09 +00002562 * preempt_notifier_register - tell me when current is being preempted & rescheduled
Randy Dunlap421cee22007-07-31 00:37:50 -07002563 * @notifier: notifier struct to register
Avi Kivitye107be32007-07-26 13:40:43 +02002564 */
2565void preempt_notifier_register(struct preempt_notifier *notifier)
2566{
2567 hlist_add_head(&notifier->link, &current->preempt_notifiers);
2568}
2569EXPORT_SYMBOL_GPL(preempt_notifier_register);
2570
2571/**
2572 * preempt_notifier_unregister - no longer interested in preemption notifications
Randy Dunlap421cee22007-07-31 00:37:50 -07002573 * @notifier: notifier struct to unregister
Avi Kivitye107be32007-07-26 13:40:43 +02002574 *
2575 * This is safe to call from within a preemption notifier.
2576 */
2577void preempt_notifier_unregister(struct preempt_notifier *notifier)
2578{
2579 hlist_del(&notifier->link);
2580}
2581EXPORT_SYMBOL_GPL(preempt_notifier_unregister);
2582
2583static void fire_sched_in_preempt_notifiers(struct task_struct *curr)
2584{
2585 struct preempt_notifier *notifier;
2586 struct hlist_node *node;
2587
2588 hlist_for_each_entry(notifier, node, &curr->preempt_notifiers, link)
2589 notifier->ops->sched_in(notifier, raw_smp_processor_id());
2590}
2591
2592static void
2593fire_sched_out_preempt_notifiers(struct task_struct *curr,
2594 struct task_struct *next)
2595{
2596 struct preempt_notifier *notifier;
2597 struct hlist_node *node;
2598
2599 hlist_for_each_entry(notifier, node, &curr->preempt_notifiers, link)
2600 notifier->ops->sched_out(notifier, next);
2601}
2602
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +02002603#else /* !CONFIG_PREEMPT_NOTIFIERS */
Avi Kivitye107be32007-07-26 13:40:43 +02002604
2605static void fire_sched_in_preempt_notifiers(struct task_struct *curr)
2606{
2607}
2608
2609static void
2610fire_sched_out_preempt_notifiers(struct task_struct *curr,
2611 struct task_struct *next)
2612{
2613}
2614
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +02002615#endif /* CONFIG_PREEMPT_NOTIFIERS */
Avi Kivitye107be32007-07-26 13:40:43 +02002616
Linus Torvalds1da177e2005-04-16 15:20:36 -07002617/**
Nick Piggin4866cde2005-06-25 14:57:23 -07002618 * prepare_task_switch - prepare to switch tasks
2619 * @rq: the runqueue preparing to switch
Randy Dunlap421cee22007-07-31 00:37:50 -07002620 * @prev: the current task that is being switched out
Nick Piggin4866cde2005-06-25 14:57:23 -07002621 * @next: the task we are going to switch to.
2622 *
2623 * This is called with the rq lock held and interrupts off. It must
2624 * be paired with a subsequent finish_task_switch after the context
2625 * switch.
2626 *
2627 * prepare_task_switch sets up locking and calls architecture specific
2628 * hooks.
2629 */
Avi Kivitye107be32007-07-26 13:40:43 +02002630static inline void
2631prepare_task_switch(struct rq *rq, struct task_struct *prev,
2632 struct task_struct *next)
Nick Piggin4866cde2005-06-25 14:57:23 -07002633{
Avi Kivitye107be32007-07-26 13:40:43 +02002634 fire_sched_out_preempt_notifiers(prev, next);
Nick Piggin4866cde2005-06-25 14:57:23 -07002635 prepare_lock_switch(rq, next);
2636 prepare_arch_switch(next);
2637}
2638
2639/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07002640 * finish_task_switch - clean up after a task-switch
Jeff Garzik344baba2005-09-07 01:15:17 -04002641 * @rq: runqueue associated with task-switch
Linus Torvalds1da177e2005-04-16 15:20:36 -07002642 * @prev: the thread we just switched away from.
2643 *
Nick Piggin4866cde2005-06-25 14:57:23 -07002644 * finish_task_switch must be called after the context switch, paired
2645 * with a prepare_task_switch call before the context switch.
2646 * finish_task_switch will reconcile locking set up by prepare_task_switch,
2647 * and do any other architecture-specific cleanup actions.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002648 *
2649 * Note that we may have delayed dropping an mm in context_switch(). If
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01002650 * so, we finish that here outside of the runqueue lock. (Doing it
Linus Torvalds1da177e2005-04-16 15:20:36 -07002651 * with the lock held can cause deadlocks; see schedule() for
2652 * details.)
2653 */
Alexey Dobriyana9957442007-10-15 17:00:13 +02002654static void finish_task_switch(struct rq *rq, struct task_struct *prev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002655 __releases(rq->lock)
2656{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002657 struct mm_struct *mm = rq->prev_mm;
Oleg Nesterov55a101f2006-09-29 02:01:10 -07002658 long prev_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002659
2660 rq->prev_mm = NULL;
2661
2662 /*
2663 * A task struct has one reference for the use as "current".
Oleg Nesterovc394cc92006-09-29 02:01:11 -07002664 * If a task dies, then it sets TASK_DEAD in tsk->state and calls
Oleg Nesterov55a101f2006-09-29 02:01:10 -07002665 * schedule one last time. The schedule call will never return, and
2666 * the scheduled task must drop that reference.
Oleg Nesterovc394cc92006-09-29 02:01:11 -07002667 * The test for TASK_DEAD must occur while the runqueue locks are
Linus Torvalds1da177e2005-04-16 15:20:36 -07002668 * still held, otherwise prev could be scheduled on another cpu, die
2669 * there before we look at prev->state, and then the reference would
2670 * be dropped twice.
2671 * Manfred Spraul <manfred@colorfullife.com>
2672 */
Oleg Nesterov55a101f2006-09-29 02:01:10 -07002673 prev_state = prev->state;
Nick Piggin4866cde2005-06-25 14:57:23 -07002674 finish_arch_switch(prev);
Jamie Iles8381f652010-01-08 15:27:33 +00002675#ifdef __ARCH_WANT_INTERRUPTS_ON_CTXSW
2676 local_irq_disable();
2677#endif /* __ARCH_WANT_INTERRUPTS_ON_CTXSW */
Peter Zijlstra49f47432009-12-27 11:51:52 +01002678 perf_event_task_sched_in(current);
Jamie Iles8381f652010-01-08 15:27:33 +00002679#ifdef __ARCH_WANT_INTERRUPTS_ON_CTXSW
2680 local_irq_enable();
2681#endif /* __ARCH_WANT_INTERRUPTS_ON_CTXSW */
Nick Piggin4866cde2005-06-25 14:57:23 -07002682 finish_lock_switch(rq, prev);
Steven Rostedte8fa1362008-01-25 21:08:05 +01002683
Avi Kivitye107be32007-07-26 13:40:43 +02002684 fire_sched_in_preempt_notifiers(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002685 if (mm)
2686 mmdrop(mm);
Oleg Nesterovc394cc92006-09-29 02:01:11 -07002687 if (unlikely(prev_state == TASK_DEAD)) {
bibo maoc6fd91f2006-03-26 01:38:20 -08002688 /*
2689 * Remove function-return probe instances associated with this
2690 * task and put them back on the free list.
Ingo Molnar9761eea2007-07-09 18:52:00 +02002691 */
bibo maoc6fd91f2006-03-26 01:38:20 -08002692 kprobe_flush_task(prev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002693 put_task_struct(prev);
bibo maoc6fd91f2006-03-26 01:38:20 -08002694 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002695}
2696
Gregory Haskins3f029d32009-07-29 11:08:47 -04002697#ifdef CONFIG_SMP
2698
2699/* assumes rq->lock is held */
2700static inline void pre_schedule(struct rq *rq, struct task_struct *prev)
2701{
2702 if (prev->sched_class->pre_schedule)
2703 prev->sched_class->pre_schedule(rq, prev);
2704}
2705
2706/* rq->lock is NOT held, but preemption is disabled */
2707static inline void post_schedule(struct rq *rq)
2708{
2709 if (rq->post_schedule) {
2710 unsigned long flags;
2711
Thomas Gleixner05fa7852009-11-17 14:28:38 +01002712 raw_spin_lock_irqsave(&rq->lock, flags);
Gregory Haskins3f029d32009-07-29 11:08:47 -04002713 if (rq->curr->sched_class->post_schedule)
2714 rq->curr->sched_class->post_schedule(rq);
Thomas Gleixner05fa7852009-11-17 14:28:38 +01002715 raw_spin_unlock_irqrestore(&rq->lock, flags);
Gregory Haskins3f029d32009-07-29 11:08:47 -04002716
2717 rq->post_schedule = 0;
2718 }
2719}
2720
2721#else
2722
2723static inline void pre_schedule(struct rq *rq, struct task_struct *p)
2724{
2725}
2726
2727static inline void post_schedule(struct rq *rq)
2728{
2729}
2730
2731#endif
2732
Linus Torvalds1da177e2005-04-16 15:20:36 -07002733/**
2734 * schedule_tail - first thing a freshly forked thread must call.
2735 * @prev: the thread we just switched away from.
2736 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07002737asmlinkage void schedule_tail(struct task_struct *prev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002738 __releases(rq->lock)
2739{
Ingo Molnar70b97a72006-07-03 00:25:42 -07002740 struct rq *rq = this_rq();
2741
Nick Piggin4866cde2005-06-25 14:57:23 -07002742 finish_task_switch(rq, prev);
Steven Rostedtda19ab52009-07-29 00:21:22 -04002743
Gregory Haskins3f029d32009-07-29 11:08:47 -04002744 /*
2745 * FIXME: do we need to worry about rq being invalidated by the
2746 * task_switch?
2747 */
2748 post_schedule(rq);
Steven Rostedtda19ab52009-07-29 00:21:22 -04002749
Nick Piggin4866cde2005-06-25 14:57:23 -07002750#ifdef __ARCH_WANT_UNLOCKED_CTXSW
2751 /* In this case, finish_task_switch does not reenable preemption */
2752 preempt_enable();
2753#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002754 if (current->set_child_tid)
Pavel Emelyanovb4888932007-10-18 23:40:14 -07002755 put_user(task_pid_vnr(current), current->set_child_tid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002756}
2757
2758/*
2759 * context_switch - switch to the new MM and the new
2760 * thread's register state.
2761 */
Ingo Molnardd41f592007-07-09 18:51:59 +02002762static inline void
Ingo Molnar70b97a72006-07-03 00:25:42 -07002763context_switch(struct rq *rq, struct task_struct *prev,
Ingo Molnar36c8b582006-07-03 00:25:41 -07002764 struct task_struct *next)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002765{
Ingo Molnardd41f592007-07-09 18:51:59 +02002766 struct mm_struct *mm, *oldmm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002767
Avi Kivitye107be32007-07-26 13:40:43 +02002768 prepare_task_switch(rq, prev, next);
Peter Zijlstra27a9da62010-05-04 20:36:56 +02002769 trace_sched_switch(prev, next);
Ingo Molnardd41f592007-07-09 18:51:59 +02002770 mm = next->mm;
2771 oldmm = prev->active_mm;
Zachary Amsden9226d122007-02-13 13:26:21 +01002772 /*
2773 * For paravirt, this is coupled with an exit in switch_to to
2774 * combine the page table reload and the switch backend into
2775 * one hypercall.
2776 */
Jeremy Fitzhardinge224101e2009-02-18 11:18:57 -08002777 arch_start_context_switch(prev);
Zachary Amsden9226d122007-02-13 13:26:21 +01002778
Tim Blechmann710390d2009-11-24 11:55:27 +01002779 if (likely(!mm)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002780 next->active_mm = oldmm;
2781 atomic_inc(&oldmm->mm_count);
2782 enter_lazy_tlb(oldmm, next);
2783 } else
2784 switch_mm(oldmm, mm, next);
2785
Tim Blechmann710390d2009-11-24 11:55:27 +01002786 if (likely(!prev->mm)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002787 prev->active_mm = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002788 rq->prev_mm = oldmm;
2789 }
Ingo Molnar3a5f5e42006-07-14 00:24:27 -07002790 /*
2791 * Since the runqueue lock will be released by the next
2792 * task (which is an invalid locking op but in the case
2793 * of the scheduler it's an obvious special-case), so we
2794 * do an early lockdep release here:
2795 */
2796#ifndef __ARCH_WANT_UNLOCKED_CTXSW
Ingo Molnar8a25d5d2006-07-03 00:24:54 -07002797 spin_release(&rq->lock.dep_map, 1, _THIS_IP_);
Ingo Molnar3a5f5e42006-07-14 00:24:27 -07002798#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002799
2800 /* Here we just switch the register state and the stack. */
2801 switch_to(prev, next, prev);
2802
Ingo Molnardd41f592007-07-09 18:51:59 +02002803 barrier();
2804 /*
2805 * this_rq must be evaluated again because prev may have moved
2806 * CPUs since it called schedule(), thus the 'rq' on its stack
2807 * frame will be invalid.
2808 */
2809 finish_task_switch(this_rq(), prev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002810}
2811
2812/*
2813 * nr_running, nr_uninterruptible and nr_context_switches:
2814 *
2815 * externally visible scheduler statistics: current number of runnable
2816 * threads, current number of uninterruptible-sleeping threads, total
2817 * number of context switches performed since bootup.
2818 */
2819unsigned long nr_running(void)
2820{
2821 unsigned long i, sum = 0;
2822
2823 for_each_online_cpu(i)
2824 sum += cpu_rq(i)->nr_running;
2825
2826 return sum;
2827}
2828
2829unsigned long nr_uninterruptible(void)
2830{
2831 unsigned long i, sum = 0;
2832
KAMEZAWA Hiroyuki0a945022006-03-28 01:56:37 -08002833 for_each_possible_cpu(i)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002834 sum += cpu_rq(i)->nr_uninterruptible;
2835
2836 /*
2837 * Since we read the counters lockless, it might be slightly
2838 * inaccurate. Do not allow it to go below zero though:
2839 */
2840 if (unlikely((long)sum < 0))
2841 sum = 0;
2842
2843 return sum;
2844}
2845
2846unsigned long long nr_context_switches(void)
2847{
Steven Rostedtcc94abf2006-06-27 02:54:31 -07002848 int i;
2849 unsigned long long sum = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002850
KAMEZAWA Hiroyuki0a945022006-03-28 01:56:37 -08002851 for_each_possible_cpu(i)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002852 sum += cpu_rq(i)->nr_switches;
2853
2854 return sum;
2855}
2856
2857unsigned long nr_iowait(void)
2858{
2859 unsigned long i, sum = 0;
2860
KAMEZAWA Hiroyuki0a945022006-03-28 01:56:37 -08002861 for_each_possible_cpu(i)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002862 sum += atomic_read(&cpu_rq(i)->nr_iowait);
2863
2864 return sum;
2865}
2866
Arjan van de Ven69d25872009-09-21 17:04:08 -07002867unsigned long nr_iowait_cpu(void)
2868{
2869 struct rq *this = this_rq();
2870 return atomic_read(&this->nr_iowait);
2871}
2872
2873unsigned long this_cpu_load(void)
2874{
2875 struct rq *this = this_rq();
2876 return this->cpu_load[0];
2877}
2878
2879
Thomas Gleixnerdce48a82009-04-11 10:43:41 +02002880/* Variables and functions for calc_load */
2881static atomic_long_t calc_load_tasks;
2882static unsigned long calc_load_update;
2883unsigned long avenrun[3];
2884EXPORT_SYMBOL(avenrun);
2885
Peter Zijlstra74f51872010-04-22 21:50:19 +02002886static long calc_load_fold_active(struct rq *this_rq)
2887{
2888 long nr_active, delta = 0;
2889
2890 nr_active = this_rq->nr_running;
2891 nr_active += (long) this_rq->nr_uninterruptible;
2892
2893 if (nr_active != this_rq->calc_load_active) {
2894 delta = nr_active - this_rq->calc_load_active;
2895 this_rq->calc_load_active = nr_active;
2896 }
2897
2898 return delta;
2899}
2900
2901#ifdef CONFIG_NO_HZ
2902/*
2903 * For NO_HZ we delay the active fold to the next LOAD_FREQ update.
2904 *
2905 * When making the ILB scale, we should try to pull this in as well.
2906 */
2907static atomic_long_t calc_load_tasks_idle;
2908
2909static void calc_load_account_idle(struct rq *this_rq)
2910{
2911 long delta;
2912
2913 delta = calc_load_fold_active(this_rq);
2914 if (delta)
2915 atomic_long_add(delta, &calc_load_tasks_idle);
2916}
2917
2918static long calc_load_fold_idle(void)
2919{
2920 long delta = 0;
2921
2922 /*
2923 * Its got a race, we don't care...
2924 */
2925 if (atomic_long_read(&calc_load_tasks_idle))
2926 delta = atomic_long_xchg(&calc_load_tasks_idle, 0);
2927
2928 return delta;
2929}
2930#else
2931static void calc_load_account_idle(struct rq *this_rq)
2932{
2933}
2934
2935static inline long calc_load_fold_idle(void)
2936{
2937 return 0;
2938}
2939#endif
2940
Thomas Gleixner2d024942009-05-02 20:08:52 +02002941/**
2942 * get_avenrun - get the load average array
2943 * @loads: pointer to dest load array
2944 * @offset: offset to add
2945 * @shift: shift count to shift the result left
2946 *
2947 * These values are estimates at best, so no need for locking.
2948 */
2949void get_avenrun(unsigned long *loads, unsigned long offset, int shift)
2950{
2951 loads[0] = (avenrun[0] + offset) << shift;
2952 loads[1] = (avenrun[1] + offset) << shift;
2953 loads[2] = (avenrun[2] + offset) << shift;
2954}
2955
Thomas Gleixnerdce48a82009-04-11 10:43:41 +02002956static unsigned long
2957calc_load(unsigned long load, unsigned long exp, unsigned long active)
Jack Steinerdb1b1fe2006-03-31 02:31:21 -08002958{
Thomas Gleixnerdce48a82009-04-11 10:43:41 +02002959 load *= exp;
2960 load += active * (FIXED_1 - exp);
2961 return load >> FSHIFT;
2962}
Jack Steinerdb1b1fe2006-03-31 02:31:21 -08002963
Thomas Gleixnerdce48a82009-04-11 10:43:41 +02002964/*
2965 * calc_load - update the avenrun load estimates 10 ticks after the
2966 * CPUs have updated calc_load_tasks.
2967 */
2968void calc_global_load(void)
2969{
2970 unsigned long upd = calc_load_update + 10;
2971 long active;
2972
2973 if (time_before(jiffies, upd))
2974 return;
2975
2976 active = atomic_long_read(&calc_load_tasks);
2977 active = active > 0 ? active * FIXED_1 : 0;
2978
2979 avenrun[0] = calc_load(avenrun[0], EXP_1, active);
2980 avenrun[1] = calc_load(avenrun[1], EXP_5, active);
2981 avenrun[2] = calc_load(avenrun[2], EXP_15, active);
2982
2983 calc_load_update += LOAD_FREQ;
2984}
2985
2986/*
Peter Zijlstra74f51872010-04-22 21:50:19 +02002987 * Called from update_cpu_load() to periodically update this CPU's
2988 * active count.
Thomas Gleixnerdce48a82009-04-11 10:43:41 +02002989 */
2990static void calc_load_account_active(struct rq *this_rq)
2991{
Peter Zijlstra74f51872010-04-22 21:50:19 +02002992 long delta;
Thomas Gleixnerdce48a82009-04-11 10:43:41 +02002993
Peter Zijlstra74f51872010-04-22 21:50:19 +02002994 if (time_before(jiffies, this_rq->calc_load_update))
2995 return;
Thomas Gleixnerdce48a82009-04-11 10:43:41 +02002996
Peter Zijlstra74f51872010-04-22 21:50:19 +02002997 delta = calc_load_fold_active(this_rq);
2998 delta += calc_load_fold_idle();
2999 if (delta)
Thomas Gleixnerdce48a82009-04-11 10:43:41 +02003000 atomic_long_add(delta, &calc_load_tasks);
Peter Zijlstra74f51872010-04-22 21:50:19 +02003001
3002 this_rq->calc_load_update += LOAD_FREQ;
Jack Steinerdb1b1fe2006-03-31 02:31:21 -08003003}
3004
Linus Torvalds1da177e2005-04-16 15:20:36 -07003005/*
Ingo Molnardd41f592007-07-09 18:51:59 +02003006 * Update rq->cpu_load[] statistics. This function is usually called every
3007 * scheduler tick (TICK_NSEC).
Ingo Molnar48f24c42006-07-03 00:25:40 -07003008 */
Ingo Molnardd41f592007-07-09 18:51:59 +02003009static void update_cpu_load(struct rq *this_rq)
Ingo Molnar48f24c42006-07-03 00:25:40 -07003010{
Dmitry Adamushko495eca42007-10-15 17:00:06 +02003011 unsigned long this_load = this_rq->load.weight;
Ingo Molnardd41f592007-07-09 18:51:59 +02003012 int i, scale;
3013
3014 this_rq->nr_load_updates++;
Ingo Molnardd41f592007-07-09 18:51:59 +02003015
3016 /* Update our load: */
3017 for (i = 0, scale = 1; i < CPU_LOAD_IDX_MAX; i++, scale += scale) {
3018 unsigned long old_load, new_load;
3019
3020 /* scale is effectively 1 << i now, and >> i divides by scale */
3021
3022 old_load = this_rq->cpu_load[i];
3023 new_load = this_load;
Ingo Molnara25707f2007-10-15 17:00:03 +02003024 /*
3025 * Round up the averaging division if load is increasing. This
3026 * prevents us from getting stuck on 9 if the load is 10, for
3027 * example.
3028 */
3029 if (new_load > old_load)
3030 new_load += scale-1;
Ingo Molnardd41f592007-07-09 18:51:59 +02003031 this_rq->cpu_load[i] = (old_load*(scale-1) + new_load) >> i;
3032 }
Thomas Gleixnerdce48a82009-04-11 10:43:41 +02003033
Peter Zijlstra74f51872010-04-22 21:50:19 +02003034 calc_load_account_active(this_rq);
Ingo Molnar48f24c42006-07-03 00:25:40 -07003035}
3036
Ingo Molnardd41f592007-07-09 18:51:59 +02003037#ifdef CONFIG_SMP
3038
Ingo Molnar48f24c42006-07-03 00:25:40 -07003039/*
Peter Zijlstra38022902009-12-16 18:04:37 +01003040 * sched_exec - execve() is a valuable balancing opportunity, because at
3041 * this point the task has the smallest effective memory and cache footprint.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003042 */
Peter Zijlstra38022902009-12-16 18:04:37 +01003043void sched_exec(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003044{
Peter Zijlstra38022902009-12-16 18:04:37 +01003045 struct task_struct *p = current;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003046 unsigned long flags;
Ingo Molnar70b97a72006-07-03 00:25:42 -07003047 struct rq *rq;
Peter Zijlstra0017d732010-03-24 18:34:10 +01003048 int dest_cpu;
Peter Zijlstra38022902009-12-16 18:04:37 +01003049
Linus Torvalds1da177e2005-04-16 15:20:36 -07003050 rq = task_rq_lock(p, &flags);
Peter Zijlstra0017d732010-03-24 18:34:10 +01003051 dest_cpu = p->sched_class->select_task_rq(rq, p, SD_BALANCE_EXEC, 0);
3052 if (dest_cpu == smp_processor_id())
3053 goto unlock;
Peter Zijlstra38022902009-12-16 18:04:37 +01003054
3055 /*
3056 * select_task_rq() can race against ->cpus_allowed
3057 */
Oleg Nesterov30da6882010-03-15 10:10:19 +01003058 if (cpumask_test_cpu(dest_cpu, &p->cpus_allowed) &&
Tejun Heo969c7922010-05-06 18:49:21 +02003059 likely(cpu_active(dest_cpu)) && migrate_task(p, dest_cpu)) {
3060 struct migration_arg arg = { p, dest_cpu };
Ingo Molnar36c8b582006-07-03 00:25:41 -07003061
Linus Torvalds1da177e2005-04-16 15:20:36 -07003062 task_rq_unlock(rq, &flags);
Tejun Heo969c7922010-05-06 18:49:21 +02003063 stop_one_cpu(cpu_of(rq), migration_cpu_stop, &arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003064 return;
3065 }
Peter Zijlstra0017d732010-03-24 18:34:10 +01003066unlock:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003067 task_rq_unlock(rq, &flags);
3068}
3069
Linus Torvalds1da177e2005-04-16 15:20:36 -07003070#endif
3071
Linus Torvalds1da177e2005-04-16 15:20:36 -07003072DEFINE_PER_CPU(struct kernel_stat, kstat);
3073
3074EXPORT_PER_CPU_SYMBOL(kstat);
3075
3076/*
Hidetoshi Setoc5f8d992009-03-31 16:56:03 +09003077 * Return any ns on the sched_clock that have not yet been accounted in
Frank Mayharf06febc2008-09-12 09:54:39 -07003078 * @p in case that task is currently running.
Hidetoshi Setoc5f8d992009-03-31 16:56:03 +09003079 *
3080 * Called with task_rq_lock() held on @rq.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003081 */
Hidetoshi Setoc5f8d992009-03-31 16:56:03 +09003082static u64 do_task_delta_exec(struct task_struct *p, struct rq *rq)
3083{
3084 u64 ns = 0;
3085
3086 if (task_current(rq, p)) {
3087 update_rq_clock(rq);
3088 ns = rq->clock - p->se.exec_start;
3089 if ((s64)ns < 0)
3090 ns = 0;
3091 }
3092
3093 return ns;
3094}
3095
Frank Mayharbb34d922008-09-12 09:54:39 -07003096unsigned long long task_delta_exec(struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003097{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003098 unsigned long flags;
Ingo Molnar41b86e92007-07-09 18:51:58 +02003099 struct rq *rq;
Frank Mayharbb34d922008-09-12 09:54:39 -07003100 u64 ns = 0;
Ingo Molnar48f24c42006-07-03 00:25:40 -07003101
Ingo Molnar41b86e92007-07-09 18:51:58 +02003102 rq = task_rq_lock(p, &flags);
Hidetoshi Setoc5f8d992009-03-31 16:56:03 +09003103 ns = do_task_delta_exec(p, rq);
3104 task_rq_unlock(rq, &flags);
Ingo Molnar15084872008-09-30 08:28:17 +02003105
Hidetoshi Setoc5f8d992009-03-31 16:56:03 +09003106 return ns;
3107}
Frank Mayharf06febc2008-09-12 09:54:39 -07003108
Hidetoshi Setoc5f8d992009-03-31 16:56:03 +09003109/*
3110 * Return accounted runtime for the task.
3111 * In case the task is currently running, return the runtime plus current's
3112 * pending runtime that have not been accounted yet.
3113 */
3114unsigned long long task_sched_runtime(struct task_struct *p)
3115{
3116 unsigned long flags;
3117 struct rq *rq;
3118 u64 ns = 0;
Ingo Molnar48f24c42006-07-03 00:25:40 -07003119
Hidetoshi Setoc5f8d992009-03-31 16:56:03 +09003120 rq = task_rq_lock(p, &flags);
3121 ns = p->se.sum_exec_runtime + do_task_delta_exec(p, rq);
3122 task_rq_unlock(rq, &flags);
3123
3124 return ns;
3125}
3126
3127/*
3128 * Return sum_exec_runtime for the thread group.
3129 * In case the task is currently running, return the sum plus current's
3130 * pending runtime that have not been accounted yet.
3131 *
3132 * Note that the thread group might have other running tasks as well,
3133 * so the return value not includes other pending runtime that other
3134 * running tasks might have.
3135 */
3136unsigned long long thread_group_sched_runtime(struct task_struct *p)
3137{
3138 struct task_cputime totals;
3139 unsigned long flags;
3140 struct rq *rq;
3141 u64 ns;
3142
3143 rq = task_rq_lock(p, &flags);
3144 thread_group_cputime(p, &totals);
3145 ns = totals.sum_exec_runtime + do_task_delta_exec(p, rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003146 task_rq_unlock(rq, &flags);
3147
3148 return ns;
3149}
3150
3151/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003152 * Account user cpu time to a process.
3153 * @p: the process that the cpu time gets accounted to
Linus Torvalds1da177e2005-04-16 15:20:36 -07003154 * @cputime: the cpu time spent in user space since the last update
Martin Schwidefsky457533a2008-12-31 15:11:37 +01003155 * @cputime_scaled: cputime scaled by cpu frequency
Linus Torvalds1da177e2005-04-16 15:20:36 -07003156 */
Martin Schwidefsky457533a2008-12-31 15:11:37 +01003157void account_user_time(struct task_struct *p, cputime_t cputime,
3158 cputime_t cputime_scaled)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003159{
3160 struct cpu_usage_stat *cpustat = &kstat_this_cpu.cpustat;
3161 cputime64_t tmp;
3162
Martin Schwidefsky457533a2008-12-31 15:11:37 +01003163 /* Add user time to process. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003164 p->utime = cputime_add(p->utime, cputime);
Martin Schwidefsky457533a2008-12-31 15:11:37 +01003165 p->utimescaled = cputime_add(p->utimescaled, cputime_scaled);
Frank Mayharf06febc2008-09-12 09:54:39 -07003166 account_group_user_time(p, cputime);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003167
3168 /* Add user time to cpustat. */
3169 tmp = cputime_to_cputime64(cputime);
3170 if (TASK_NICE(p) > 0)
3171 cpustat->nice = cputime64_add(cpustat->nice, tmp);
3172 else
3173 cpustat->user = cputime64_add(cpustat->user, tmp);
Bharata B Raoef12fef2009-03-31 10:02:22 +05303174
3175 cpuacct_update_stats(p, CPUACCT_STAT_USER, cputime);
Jonathan Lim49b5cf32008-07-25 01:48:40 -07003176 /* Account for user time used */
3177 acct_update_integrals(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003178}
3179
3180/*
Laurent Vivier94886b82007-10-15 17:00:19 +02003181 * Account guest cpu time to a process.
3182 * @p: the process that the cpu time gets accounted to
3183 * @cputime: the cpu time spent in virtual machine since the last update
Martin Schwidefsky457533a2008-12-31 15:11:37 +01003184 * @cputime_scaled: cputime scaled by cpu frequency
Laurent Vivier94886b82007-10-15 17:00:19 +02003185 */
Martin Schwidefsky457533a2008-12-31 15:11:37 +01003186static void account_guest_time(struct task_struct *p, cputime_t cputime,
3187 cputime_t cputime_scaled)
Laurent Vivier94886b82007-10-15 17:00:19 +02003188{
3189 cputime64_t tmp;
3190 struct cpu_usage_stat *cpustat = &kstat_this_cpu.cpustat;
3191
3192 tmp = cputime_to_cputime64(cputime);
3193
Martin Schwidefsky457533a2008-12-31 15:11:37 +01003194 /* Add guest time to process. */
Laurent Vivier94886b82007-10-15 17:00:19 +02003195 p->utime = cputime_add(p->utime, cputime);
Martin Schwidefsky457533a2008-12-31 15:11:37 +01003196 p->utimescaled = cputime_add(p->utimescaled, cputime_scaled);
Frank Mayharf06febc2008-09-12 09:54:39 -07003197 account_group_user_time(p, cputime);
Laurent Vivier94886b82007-10-15 17:00:19 +02003198 p->gtime = cputime_add(p->gtime, cputime);
3199
Martin Schwidefsky457533a2008-12-31 15:11:37 +01003200 /* Add guest time to cpustat. */
Ryota Ozakice0e7b22009-10-24 01:20:10 +09003201 if (TASK_NICE(p) > 0) {
3202 cpustat->nice = cputime64_add(cpustat->nice, tmp);
3203 cpustat->guest_nice = cputime64_add(cpustat->guest_nice, tmp);
3204 } else {
3205 cpustat->user = cputime64_add(cpustat->user, tmp);
3206 cpustat->guest = cputime64_add(cpustat->guest, tmp);
3207 }
Laurent Vivier94886b82007-10-15 17:00:19 +02003208}
3209
3210/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003211 * Account system cpu time to a process.
3212 * @p: the process that the cpu time gets accounted to
3213 * @hardirq_offset: the offset to subtract from hardirq_count()
3214 * @cputime: the cpu time spent in kernel space since the last update
Martin Schwidefsky457533a2008-12-31 15:11:37 +01003215 * @cputime_scaled: cputime scaled by cpu frequency
Linus Torvalds1da177e2005-04-16 15:20:36 -07003216 */
3217void account_system_time(struct task_struct *p, int hardirq_offset,
Martin Schwidefsky457533a2008-12-31 15:11:37 +01003218 cputime_t cputime, cputime_t cputime_scaled)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003219{
3220 struct cpu_usage_stat *cpustat = &kstat_this_cpu.cpustat;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003221 cputime64_t tmp;
3222
Harvey Harrison983ed7a2008-04-24 18:17:55 -07003223 if ((p->flags & PF_VCPU) && (irq_count() - hardirq_offset == 0)) {
Martin Schwidefsky457533a2008-12-31 15:11:37 +01003224 account_guest_time(p, cputime, cputime_scaled);
Harvey Harrison983ed7a2008-04-24 18:17:55 -07003225 return;
3226 }
Laurent Vivier94886b82007-10-15 17:00:19 +02003227
Martin Schwidefsky457533a2008-12-31 15:11:37 +01003228 /* Add system time to process. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003229 p->stime = cputime_add(p->stime, cputime);
Martin Schwidefsky457533a2008-12-31 15:11:37 +01003230 p->stimescaled = cputime_add(p->stimescaled, cputime_scaled);
Frank Mayharf06febc2008-09-12 09:54:39 -07003231 account_group_system_time(p, cputime);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003232
3233 /* Add system time to cpustat. */
3234 tmp = cputime_to_cputime64(cputime);
3235 if (hardirq_count() - hardirq_offset)
3236 cpustat->irq = cputime64_add(cpustat->irq, tmp);
3237 else if (softirq_count())
3238 cpustat->softirq = cputime64_add(cpustat->softirq, tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003239 else
Martin Schwidefsky79741dd2008-12-31 15:11:38 +01003240 cpustat->system = cputime64_add(cpustat->system, tmp);
3241
Bharata B Raoef12fef2009-03-31 10:02:22 +05303242 cpuacct_update_stats(p, CPUACCT_STAT_SYSTEM, cputime);
3243
Linus Torvalds1da177e2005-04-16 15:20:36 -07003244 /* Account for system time used */
3245 acct_update_integrals(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003246}
3247
3248/*
3249 * Account for involuntary wait time.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003250 * @steal: the cpu time spent in involuntary wait
Linus Torvalds1da177e2005-04-16 15:20:36 -07003251 */
Martin Schwidefsky79741dd2008-12-31 15:11:38 +01003252void account_steal_time(cputime_t cputime)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003253{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003254 struct cpu_usage_stat *cpustat = &kstat_this_cpu.cpustat;
Martin Schwidefsky79741dd2008-12-31 15:11:38 +01003255 cputime64_t cputime64 = cputime_to_cputime64(cputime);
3256
3257 cpustat->steal = cputime64_add(cpustat->steal, cputime64);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003258}
3259
Christoph Lameter7835b982006-12-10 02:20:22 -08003260/*
Martin Schwidefsky79741dd2008-12-31 15:11:38 +01003261 * Account for idle time.
3262 * @cputime: the cpu time spent in idle wait
Linus Torvalds1da177e2005-04-16 15:20:36 -07003263 */
Martin Schwidefsky79741dd2008-12-31 15:11:38 +01003264void account_idle_time(cputime_t cputime)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003265{
3266 struct cpu_usage_stat *cpustat = &kstat_this_cpu.cpustat;
Martin Schwidefsky79741dd2008-12-31 15:11:38 +01003267 cputime64_t cputime64 = cputime_to_cputime64(cputime);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003268 struct rq *rq = this_rq();
3269
Martin Schwidefsky79741dd2008-12-31 15:11:38 +01003270 if (atomic_read(&rq->nr_iowait) > 0)
3271 cpustat->iowait = cputime64_add(cpustat->iowait, cputime64);
3272 else
3273 cpustat->idle = cputime64_add(cpustat->idle, cputime64);
Christoph Lameter7835b982006-12-10 02:20:22 -08003274}
3275
Martin Schwidefsky79741dd2008-12-31 15:11:38 +01003276#ifndef CONFIG_VIRT_CPU_ACCOUNTING
3277
3278/*
3279 * Account a single tick of cpu time.
3280 * @p: the process that the cpu time gets accounted to
3281 * @user_tick: indicates if the tick is a user or a system tick
3282 */
3283void account_process_tick(struct task_struct *p, int user_tick)
3284{
Stanislaw Gruszkaa42548a2009-07-29 12:15:29 +02003285 cputime_t one_jiffy_scaled = cputime_to_scaled(cputime_one_jiffy);
Martin Schwidefsky79741dd2008-12-31 15:11:38 +01003286 struct rq *rq = this_rq();
3287
3288 if (user_tick)
Stanislaw Gruszkaa42548a2009-07-29 12:15:29 +02003289 account_user_time(p, cputime_one_jiffy, one_jiffy_scaled);
Eric Dumazetf5f293a2009-04-29 14:44:49 +02003290 else if ((p != rq->idle) || (irq_count() != HARDIRQ_OFFSET))
Stanislaw Gruszkaa42548a2009-07-29 12:15:29 +02003291 account_system_time(p, HARDIRQ_OFFSET, cputime_one_jiffy,
Martin Schwidefsky79741dd2008-12-31 15:11:38 +01003292 one_jiffy_scaled);
3293 else
Stanislaw Gruszkaa42548a2009-07-29 12:15:29 +02003294 account_idle_time(cputime_one_jiffy);
Martin Schwidefsky79741dd2008-12-31 15:11:38 +01003295}
3296
3297/*
3298 * Account multiple ticks of steal time.
3299 * @p: the process from which the cpu time has been stolen
3300 * @ticks: number of stolen ticks
3301 */
3302void account_steal_ticks(unsigned long ticks)
3303{
3304 account_steal_time(jiffies_to_cputime(ticks));
3305}
3306
3307/*
3308 * Account multiple ticks of idle time.
3309 * @ticks: number of stolen ticks
3310 */
3311void account_idle_ticks(unsigned long ticks)
3312{
3313 account_idle_time(jiffies_to_cputime(ticks));
3314}
3315
3316#endif
3317
Christoph Lameter7835b982006-12-10 02:20:22 -08003318/*
Balbir Singh49048622008-09-05 18:12:23 +02003319 * Use precise platform statistics if available:
3320 */
3321#ifdef CONFIG_VIRT_CPU_ACCOUNTING
Hidetoshi Setod180c5b2009-11-26 14:48:30 +09003322void task_times(struct task_struct *p, cputime_t *ut, cputime_t *st)
Balbir Singh49048622008-09-05 18:12:23 +02003323{
Hidetoshi Setod99ca3b2009-12-02 17:26:47 +09003324 *ut = p->utime;
3325 *st = p->stime;
Balbir Singh49048622008-09-05 18:12:23 +02003326}
3327
Hidetoshi Seto0cf55e12009-12-02 17:28:07 +09003328void thread_group_times(struct task_struct *p, cputime_t *ut, cputime_t *st)
Balbir Singh49048622008-09-05 18:12:23 +02003329{
Hidetoshi Seto0cf55e12009-12-02 17:28:07 +09003330 struct task_cputime cputime;
3331
3332 thread_group_cputime(p, &cputime);
3333
3334 *ut = cputime.utime;
3335 *st = cputime.stime;
Balbir Singh49048622008-09-05 18:12:23 +02003336}
3337#else
Hidetoshi Seto761b1d22009-11-12 13:33:45 +09003338
3339#ifndef nsecs_to_cputime
Hidetoshi Setob7b20df92009-11-26 14:49:27 +09003340# define nsecs_to_cputime(__nsecs) nsecs_to_jiffies(__nsecs)
Hidetoshi Seto761b1d22009-11-12 13:33:45 +09003341#endif
3342
Hidetoshi Setod180c5b2009-11-26 14:48:30 +09003343void task_times(struct task_struct *p, cputime_t *ut, cputime_t *st)
Balbir Singh49048622008-09-05 18:12:23 +02003344{
Hidetoshi Setod99ca3b2009-12-02 17:26:47 +09003345 cputime_t rtime, utime = p->utime, total = cputime_add(utime, p->stime);
Balbir Singh49048622008-09-05 18:12:23 +02003346
3347 /*
3348 * Use CFS's precise accounting:
3349 */
Hidetoshi Setod180c5b2009-11-26 14:48:30 +09003350 rtime = nsecs_to_cputime(p->se.sum_exec_runtime);
Balbir Singh49048622008-09-05 18:12:23 +02003351
3352 if (total) {
Hidetoshi Setod180c5b2009-11-26 14:48:30 +09003353 u64 temp;
Balbir Singh49048622008-09-05 18:12:23 +02003354
Hidetoshi Setod180c5b2009-11-26 14:48:30 +09003355 temp = (u64)(rtime * utime);
Balbir Singh49048622008-09-05 18:12:23 +02003356 do_div(temp, total);
Hidetoshi Setod180c5b2009-11-26 14:48:30 +09003357 utime = (cputime_t)temp;
3358 } else
3359 utime = rtime;
Balbir Singh49048622008-09-05 18:12:23 +02003360
3361 /*
Hidetoshi Setod180c5b2009-11-26 14:48:30 +09003362 * Compare with previous values, to keep monotonicity:
Balbir Singh49048622008-09-05 18:12:23 +02003363 */
Hidetoshi Seto761b1d22009-11-12 13:33:45 +09003364 p->prev_utime = max(p->prev_utime, utime);
Hidetoshi Setod99ca3b2009-12-02 17:26:47 +09003365 p->prev_stime = max(p->prev_stime, cputime_sub(rtime, p->prev_utime));
Balbir Singh49048622008-09-05 18:12:23 +02003366
Hidetoshi Setod99ca3b2009-12-02 17:26:47 +09003367 *ut = p->prev_utime;
3368 *st = p->prev_stime;
Hidetoshi Setod180c5b2009-11-26 14:48:30 +09003369}
Balbir Singh49048622008-09-05 18:12:23 +02003370
Hidetoshi Seto0cf55e12009-12-02 17:28:07 +09003371/*
3372 * Must be called with siglock held.
3373 */
3374void thread_group_times(struct task_struct *p, cputime_t *ut, cputime_t *st)
3375{
3376 struct signal_struct *sig = p->signal;
3377 struct task_cputime cputime;
3378 cputime_t rtime, utime, total;
3379
3380 thread_group_cputime(p, &cputime);
3381
3382 total = cputime_add(cputime.utime, cputime.stime);
3383 rtime = nsecs_to_cputime(cputime.sum_exec_runtime);
3384
3385 if (total) {
3386 u64 temp;
3387
3388 temp = (u64)(rtime * cputime.utime);
3389 do_div(temp, total);
3390 utime = (cputime_t)temp;
3391 } else
3392 utime = rtime;
3393
3394 sig->prev_utime = max(sig->prev_utime, utime);
3395 sig->prev_stime = max(sig->prev_stime,
3396 cputime_sub(rtime, sig->prev_utime));
3397
3398 *ut = sig->prev_utime;
3399 *st = sig->prev_stime;
Balbir Singh49048622008-09-05 18:12:23 +02003400}
3401#endif
3402
Balbir Singh49048622008-09-05 18:12:23 +02003403/*
Christoph Lameter7835b982006-12-10 02:20:22 -08003404 * This function gets called by the timer code, with HZ frequency.
3405 * We call it with interrupts disabled.
3406 *
3407 * It also gets called by the fork code, when changing the parent's
3408 * timeslices.
3409 */
3410void scheduler_tick(void)
3411{
Christoph Lameter7835b982006-12-10 02:20:22 -08003412 int cpu = smp_processor_id();
3413 struct rq *rq = cpu_rq(cpu);
Ingo Molnardd41f592007-07-09 18:51:59 +02003414 struct task_struct *curr = rq->curr;
Peter Zijlstra3e51f332008-05-03 18:29:28 +02003415
3416 sched_clock_tick();
Christoph Lameter7835b982006-12-10 02:20:22 -08003417
Thomas Gleixner05fa7852009-11-17 14:28:38 +01003418 raw_spin_lock(&rq->lock);
Peter Zijlstra3e51f332008-05-03 18:29:28 +02003419 update_rq_clock(rq);
Ingo Molnarf1a438d2007-08-09 11:16:45 +02003420 update_cpu_load(rq);
Peter Zijlstrafa85ae22008-01-25 21:08:29 +01003421 curr->sched_class->task_tick(rq, curr, 0);
Thomas Gleixner05fa7852009-11-17 14:28:38 +01003422 raw_spin_unlock(&rq->lock);
Ingo Molnardd41f592007-07-09 18:51:59 +02003423
Peter Zijlstra49f47432009-12-27 11:51:52 +01003424 perf_event_task_tick(curr);
Peter Zijlstrae220d2d2009-05-23 18:28:55 +02003425
Christoph Lametere418e1c2006-12-10 02:20:23 -08003426#ifdef CONFIG_SMP
Ingo Molnardd41f592007-07-09 18:51:59 +02003427 rq->idle_at_tick = idle_cpu(cpu);
3428 trigger_load_balance(rq, cpu);
Christoph Lametere418e1c2006-12-10 02:20:23 -08003429#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003430}
3431
Lai Jiangshan132380a2009-04-02 14:18:25 +08003432notrace unsigned long get_parent_ip(unsigned long addr)
Steven Rostedt6cd8a4b2008-05-12 21:20:42 +02003433{
3434 if (in_lock_functions(addr)) {
3435 addr = CALLER_ADDR2;
3436 if (in_lock_functions(addr))
3437 addr = CALLER_ADDR3;
3438 }
3439 return addr;
3440}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003441
Steven Rostedt7e49fcc2009-01-22 19:01:40 -05003442#if defined(CONFIG_PREEMPT) && (defined(CONFIG_DEBUG_PREEMPT) || \
3443 defined(CONFIG_PREEMPT_TRACER))
3444
Srinivasa Ds43627582008-02-23 15:24:04 -08003445void __kprobes add_preempt_count(int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003446{
Steven Rostedt6cd8a4b2008-05-12 21:20:42 +02003447#ifdef CONFIG_DEBUG_PREEMPT
Linus Torvalds1da177e2005-04-16 15:20:36 -07003448 /*
3449 * Underflow?
3450 */
Ingo Molnar9a11b49a2006-07-03 00:24:33 -07003451 if (DEBUG_LOCKS_WARN_ON((preempt_count() < 0)))
3452 return;
Steven Rostedt6cd8a4b2008-05-12 21:20:42 +02003453#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003454 preempt_count() += val;
Steven Rostedt6cd8a4b2008-05-12 21:20:42 +02003455#ifdef CONFIG_DEBUG_PREEMPT
Linus Torvalds1da177e2005-04-16 15:20:36 -07003456 /*
3457 * Spinlock count overflowing soon?
3458 */
Miguel Ojeda Sandonis33859f72006-12-10 02:20:38 -08003459 DEBUG_LOCKS_WARN_ON((preempt_count() & PREEMPT_MASK) >=
3460 PREEMPT_MASK - 10);
Steven Rostedt6cd8a4b2008-05-12 21:20:42 +02003461#endif
3462 if (preempt_count() == val)
3463 trace_preempt_off(CALLER_ADDR0, get_parent_ip(CALLER_ADDR1));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003464}
3465EXPORT_SYMBOL(add_preempt_count);
3466
Srinivasa Ds43627582008-02-23 15:24:04 -08003467void __kprobes sub_preempt_count(int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003468{
Steven Rostedt6cd8a4b2008-05-12 21:20:42 +02003469#ifdef CONFIG_DEBUG_PREEMPT
Linus Torvalds1da177e2005-04-16 15:20:36 -07003470 /*
3471 * Underflow?
3472 */
Ingo Molnar01e3eb82009-01-12 13:00:50 +01003473 if (DEBUG_LOCKS_WARN_ON(val > preempt_count()))
Ingo Molnar9a11b49a2006-07-03 00:24:33 -07003474 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003475 /*
3476 * Is the spinlock portion underflowing?
3477 */
Ingo Molnar9a11b49a2006-07-03 00:24:33 -07003478 if (DEBUG_LOCKS_WARN_ON((val < PREEMPT_MASK) &&
3479 !(preempt_count() & PREEMPT_MASK)))
3480 return;
Steven Rostedt6cd8a4b2008-05-12 21:20:42 +02003481#endif
Ingo Molnar9a11b49a2006-07-03 00:24:33 -07003482
Steven Rostedt6cd8a4b2008-05-12 21:20:42 +02003483 if (preempt_count() == val)
3484 trace_preempt_on(CALLER_ADDR0, get_parent_ip(CALLER_ADDR1));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003485 preempt_count() -= val;
3486}
3487EXPORT_SYMBOL(sub_preempt_count);
3488
3489#endif
3490
3491/*
Ingo Molnardd41f592007-07-09 18:51:59 +02003492 * Print scheduling while atomic bug:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003493 */
Ingo Molnardd41f592007-07-09 18:51:59 +02003494static noinline void __schedule_bug(struct task_struct *prev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003495{
Satyam Sharma838225b2007-10-24 18:23:50 +02003496 struct pt_regs *regs = get_irq_regs();
3497
Peter Zijlstra3df0fc52009-12-20 14:23:57 +01003498 printk(KERN_ERR "BUG: scheduling while atomic: %s/%d/0x%08x\n",
3499 prev->comm, prev->pid, preempt_count());
Satyam Sharma838225b2007-10-24 18:23:50 +02003500
Ingo Molnardd41f592007-07-09 18:51:59 +02003501 debug_show_held_locks(prev);
Arjan van de Vene21f5b12008-05-23 09:05:58 -07003502 print_modules();
Ingo Molnardd41f592007-07-09 18:51:59 +02003503 if (irqs_disabled())
3504 print_irqtrace_events(prev);
Satyam Sharma838225b2007-10-24 18:23:50 +02003505
3506 if (regs)
3507 show_regs(regs);
3508 else
3509 dump_stack();
Ingo Molnardd41f592007-07-09 18:51:59 +02003510}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003511
Ingo Molnardd41f592007-07-09 18:51:59 +02003512/*
3513 * Various schedule()-time debugging checks and statistics:
3514 */
3515static inline void schedule_debug(struct task_struct *prev)
3516{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003517 /*
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01003518 * Test if we are atomic. Since do_exit() needs to call into
Linus Torvalds1da177e2005-04-16 15:20:36 -07003519 * schedule() atomically, we ignore that path for now.
3520 * Otherwise, whine if we are scheduling when we should not be.
3521 */
Roel Kluin3f33a7c2008-05-13 23:44:11 +02003522 if (unlikely(in_atomic_preempt_off() && !prev->exit_state))
Ingo Molnardd41f592007-07-09 18:51:59 +02003523 __schedule_bug(prev);
3524
Linus Torvalds1da177e2005-04-16 15:20:36 -07003525 profile_hit(SCHED_PROFILING, __builtin_return_address(0));
3526
Ingo Molnar2d723762007-10-15 17:00:12 +02003527 schedstat_inc(this_rq(), sched_count);
Ingo Molnarb8efb562007-10-15 17:00:10 +02003528#ifdef CONFIG_SCHEDSTATS
3529 if (unlikely(prev->lock_depth >= 0)) {
Ingo Molnar2d723762007-10-15 17:00:12 +02003530 schedstat_inc(this_rq(), bkl_count);
3531 schedstat_inc(prev, sched_info.bkl_count);
Ingo Molnarb8efb562007-10-15 17:00:10 +02003532 }
3533#endif
Ingo Molnardd41f592007-07-09 18:51:59 +02003534}
3535
Peter Zijlstra6cecd082009-11-30 13:00:37 +01003536static void put_prev_task(struct rq *rq, struct task_struct *prev)
Mike Galbraithdf1c99d2009-03-10 19:08:11 +01003537{
Mike Galbraitha64692a2010-03-11 17:16:20 +01003538 if (prev->se.on_rq)
3539 update_rq_clock(rq);
3540 rq->skip_clock_update = 0;
Peter Zijlstra6cecd082009-11-30 13:00:37 +01003541 prev->sched_class->put_prev_task(rq, prev);
Mike Galbraithdf1c99d2009-03-10 19:08:11 +01003542}
3543
Ingo Molnardd41f592007-07-09 18:51:59 +02003544/*
3545 * Pick up the highest-prio task:
3546 */
3547static inline struct task_struct *
Wang Chenb67802e2009-03-02 13:55:26 +08003548pick_next_task(struct rq *rq)
Ingo Molnardd41f592007-07-09 18:51:59 +02003549{
Ingo Molnar5522d5d2007-10-15 17:00:12 +02003550 const struct sched_class *class;
Ingo Molnardd41f592007-07-09 18:51:59 +02003551 struct task_struct *p;
3552
3553 /*
3554 * Optimization: we know that if all tasks are in
3555 * the fair class we can call that function directly:
3556 */
3557 if (likely(rq->nr_running == rq->cfs.nr_running)) {
Ingo Molnarfb8d4722007-08-09 11:16:48 +02003558 p = fair_sched_class.pick_next_task(rq);
Ingo Molnardd41f592007-07-09 18:51:59 +02003559 if (likely(p))
3560 return p;
3561 }
3562
3563 class = sched_class_highest;
3564 for ( ; ; ) {
Ingo Molnarfb8d4722007-08-09 11:16:48 +02003565 p = class->pick_next_task(rq);
Ingo Molnardd41f592007-07-09 18:51:59 +02003566 if (p)
3567 return p;
3568 /*
3569 * Will never be NULL as the idle class always
3570 * returns a non-NULL p:
3571 */
3572 class = class->next;
3573 }
3574}
3575
3576/*
3577 * schedule() is the main scheduler function.
3578 */
Peter Zijlstraff743342009-03-13 12:21:26 +01003579asmlinkage void __sched schedule(void)
Ingo Molnardd41f592007-07-09 18:51:59 +02003580{
3581 struct task_struct *prev, *next;
Harvey Harrison67ca7bd2008-02-15 09:56:36 -08003582 unsigned long *switch_count;
Ingo Molnardd41f592007-07-09 18:51:59 +02003583 struct rq *rq;
Peter Zijlstra31656512008-07-18 18:01:23 +02003584 int cpu;
Ingo Molnardd41f592007-07-09 18:51:59 +02003585
Peter Zijlstraff743342009-03-13 12:21:26 +01003586need_resched:
3587 preempt_disable();
Ingo Molnardd41f592007-07-09 18:51:59 +02003588 cpu = smp_processor_id();
3589 rq = cpu_rq(cpu);
Paul E. McKenney25502a62010-04-01 17:37:01 -07003590 rcu_note_context_switch(cpu);
Ingo Molnardd41f592007-07-09 18:51:59 +02003591 prev = rq->curr;
3592 switch_count = &prev->nivcsw;
3593
Linus Torvalds1da177e2005-04-16 15:20:36 -07003594 release_kernel_lock(prev);
3595need_resched_nonpreemptible:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003596
Ingo Molnardd41f592007-07-09 18:51:59 +02003597 schedule_debug(prev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003598
Peter Zijlstra31656512008-07-18 18:01:23 +02003599 if (sched_feat(HRTICK))
Mike Galbraithf333fdc2008-05-12 21:20:55 +02003600 hrtick_clear(rq);
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01003601
Thomas Gleixner05fa7852009-11-17 14:28:38 +01003602 raw_spin_lock_irq(&rq->lock);
Ingo Molnar1e819952007-10-15 17:00:13 +02003603 clear_tsk_need_resched(prev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003604
Ingo Molnardd41f592007-07-09 18:51:59 +02003605 if (prev->state && !(preempt_count() & PREEMPT_ACTIVE)) {
Oleg Nesterov16882c12008-06-08 21:20:41 +04003606 if (unlikely(signal_pending_state(prev->state, prev)))
Ingo Molnardd41f592007-07-09 18:51:59 +02003607 prev->state = TASK_RUNNING;
Oleg Nesterov16882c12008-06-08 21:20:41 +04003608 else
Peter Zijlstra371fd7e2010-03-24 16:38:48 +01003609 deactivate_task(rq, prev, DEQUEUE_SLEEP);
Ingo Molnardd41f592007-07-09 18:51:59 +02003610 switch_count = &prev->nvcsw;
3611 }
3612
Gregory Haskins3f029d32009-07-29 11:08:47 -04003613 pre_schedule(rq, prev);
Steven Rostedtf65eda42008-01-25 21:08:07 +01003614
Ingo Molnardd41f592007-07-09 18:51:59 +02003615 if (unlikely(!rq->nr_running))
3616 idle_balance(cpu, rq);
3617
Mike Galbraithdf1c99d2009-03-10 19:08:11 +01003618 put_prev_task(rq, prev);
Wang Chenb67802e2009-03-02 13:55:26 +08003619 next = pick_next_task(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003620
Linus Torvalds1da177e2005-04-16 15:20:36 -07003621 if (likely(prev != next)) {
David Simner673a90a2008-04-29 10:08:59 +01003622 sched_info_switch(prev, next);
Peter Zijlstra49f47432009-12-27 11:51:52 +01003623 perf_event_task_sched_out(prev, next);
David Simner673a90a2008-04-29 10:08:59 +01003624
Linus Torvalds1da177e2005-04-16 15:20:36 -07003625 rq->nr_switches++;
3626 rq->curr = next;
3627 ++*switch_count;
3628
Ingo Molnardd41f592007-07-09 18:51:59 +02003629 context_switch(rq, prev, next); /* unlocks the rq */
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01003630 /*
3631 * the context switch might have flipped the stack from under
3632 * us, hence refresh the local variables.
3633 */
3634 cpu = smp_processor_id();
3635 rq = cpu_rq(cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003636 } else
Thomas Gleixner05fa7852009-11-17 14:28:38 +01003637 raw_spin_unlock_irq(&rq->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003638
Gregory Haskins3f029d32009-07-29 11:08:47 -04003639 post_schedule(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003640
Yong Zhang6d558c32010-01-11 14:21:25 +08003641 if (unlikely(reacquire_kernel_lock(current) < 0)) {
3642 prev = rq->curr;
3643 switch_count = &prev->nivcsw;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003644 goto need_resched_nonpreemptible;
Yong Zhang6d558c32010-01-11 14:21:25 +08003645 }
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01003646
Linus Torvalds1da177e2005-04-16 15:20:36 -07003647 preempt_enable_no_resched();
Peter Zijlstraff743342009-03-13 12:21:26 +01003648 if (need_resched())
Linus Torvalds1da177e2005-04-16 15:20:36 -07003649 goto need_resched;
3650}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003651EXPORT_SYMBOL(schedule);
3652
Frederic Weisbeckerc08f7822009-12-02 20:49:17 +01003653#ifdef CONFIG_MUTEX_SPIN_ON_OWNER
Peter Zijlstra0d66bf62009-01-12 14:01:47 +01003654/*
3655 * Look out! "owner" is an entirely speculative pointer
3656 * access and not reliable.
3657 */
3658int mutex_spin_on_owner(struct mutex *lock, struct thread_info *owner)
3659{
3660 unsigned int cpu;
3661 struct rq *rq;
3662
3663 if (!sched_feat(OWNER_SPIN))
3664 return 0;
3665
3666#ifdef CONFIG_DEBUG_PAGEALLOC
3667 /*
3668 * Need to access the cpu field knowing that
3669 * DEBUG_PAGEALLOC could have unmapped it if
3670 * the mutex owner just released it and exited.
3671 */
3672 if (probe_kernel_address(&owner->cpu, cpu))
Benjamin Herrenschmidt4b402212010-04-16 23:20:00 +02003673 return 0;
Peter Zijlstra0d66bf62009-01-12 14:01:47 +01003674#else
3675 cpu = owner->cpu;
3676#endif
3677
3678 /*
3679 * Even if the access succeeded (likely case),
3680 * the cpu field may no longer be valid.
3681 */
3682 if (cpu >= nr_cpumask_bits)
Benjamin Herrenschmidt4b402212010-04-16 23:20:00 +02003683 return 0;
Peter Zijlstra0d66bf62009-01-12 14:01:47 +01003684
3685 /*
3686 * We need to validate that we can do a
3687 * get_cpu() and that we have the percpu area.
3688 */
3689 if (!cpu_online(cpu))
Benjamin Herrenschmidt4b402212010-04-16 23:20:00 +02003690 return 0;
Peter Zijlstra0d66bf62009-01-12 14:01:47 +01003691
3692 rq = cpu_rq(cpu);
3693
3694 for (;;) {
3695 /*
3696 * Owner changed, break to re-assess state.
3697 */
3698 if (lock->owner != owner)
3699 break;
3700
3701 /*
3702 * Is that owner really running on that cpu?
3703 */
3704 if (task_thread_info(rq->curr) != owner || need_resched())
3705 return 0;
3706
3707 cpu_relax();
3708 }
Benjamin Herrenschmidt4b402212010-04-16 23:20:00 +02003709
Peter Zijlstra0d66bf62009-01-12 14:01:47 +01003710 return 1;
3711}
3712#endif
3713
Linus Torvalds1da177e2005-04-16 15:20:36 -07003714#ifdef CONFIG_PREEMPT
3715/*
Andreas Mohr2ed6e342006-07-10 04:43:52 -07003716 * this is the entry point to schedule() from in-kernel preemption
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01003717 * off of preempt_enable. Kernel preemptions off return from interrupt
Linus Torvalds1da177e2005-04-16 15:20:36 -07003718 * occur there and call schedule directly.
3719 */
3720asmlinkage void __sched preempt_schedule(void)
3721{
3722 struct thread_info *ti = current_thread_info();
Ingo Molnar6478d882008-01-25 21:08:33 +01003723
Linus Torvalds1da177e2005-04-16 15:20:36 -07003724 /*
3725 * If there is a non-zero preempt_count or interrupts are disabled,
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01003726 * we do not want to preempt the current task. Just return..
Linus Torvalds1da177e2005-04-16 15:20:36 -07003727 */
Nick Pigginbeed33a2006-10-11 01:21:52 -07003728 if (likely(ti->preempt_count || irqs_disabled()))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003729 return;
3730
Andi Kleen3a5c3592007-10-15 17:00:14 +02003731 do {
3732 add_preempt_count(PREEMPT_ACTIVE);
Andi Kleen3a5c3592007-10-15 17:00:14 +02003733 schedule();
Andi Kleen3a5c3592007-10-15 17:00:14 +02003734 sub_preempt_count(PREEMPT_ACTIVE);
3735
3736 /*
3737 * Check again in case we missed a preemption opportunity
3738 * between schedule and now.
3739 */
3740 barrier();
Lai Jiangshan5ed0cec2009-03-06 19:40:20 +08003741 } while (need_resched());
Linus Torvalds1da177e2005-04-16 15:20:36 -07003742}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003743EXPORT_SYMBOL(preempt_schedule);
3744
3745/*
Andreas Mohr2ed6e342006-07-10 04:43:52 -07003746 * this is the entry point to schedule() from kernel preemption
Linus Torvalds1da177e2005-04-16 15:20:36 -07003747 * off of irq context.
3748 * Note, that this is called and return with irqs disabled. This will
3749 * protect us against recursive calling from irq.
3750 */
3751asmlinkage void __sched preempt_schedule_irq(void)
3752{
3753 struct thread_info *ti = current_thread_info();
Ingo Molnar6478d882008-01-25 21:08:33 +01003754
Andreas Mohr2ed6e342006-07-10 04:43:52 -07003755 /* Catch callers which need to be fixed */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003756 BUG_ON(ti->preempt_count || !irqs_disabled());
3757
Andi Kleen3a5c3592007-10-15 17:00:14 +02003758 do {
3759 add_preempt_count(PREEMPT_ACTIVE);
Andi Kleen3a5c3592007-10-15 17:00:14 +02003760 local_irq_enable();
3761 schedule();
3762 local_irq_disable();
Andi Kleen3a5c3592007-10-15 17:00:14 +02003763 sub_preempt_count(PREEMPT_ACTIVE);
3764
3765 /*
3766 * Check again in case we missed a preemption opportunity
3767 * between schedule and now.
3768 */
3769 barrier();
Lai Jiangshan5ed0cec2009-03-06 19:40:20 +08003770 } while (need_resched());
Linus Torvalds1da177e2005-04-16 15:20:36 -07003771}
3772
3773#endif /* CONFIG_PREEMPT */
3774
Peter Zijlstra63859d42009-09-15 19:14:42 +02003775int default_wake_function(wait_queue_t *curr, unsigned mode, int wake_flags,
Ingo Molnar95cdf3b2005-09-10 00:26:11 -07003776 void *key)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003777{
Peter Zijlstra63859d42009-09-15 19:14:42 +02003778 return try_to_wake_up(curr->private, mode, wake_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003779}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003780EXPORT_SYMBOL(default_wake_function);
3781
3782/*
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01003783 * The core wakeup function. Non-exclusive wakeups (nr_exclusive == 0) just
3784 * wake everything up. If it's an exclusive wakeup (nr_exclusive == small +ve
Linus Torvalds1da177e2005-04-16 15:20:36 -07003785 * number) then we wake all the non-exclusive tasks and one exclusive task.
3786 *
3787 * There are circumstances in which we can try to wake a task which has already
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01003788 * started to run but is not in state TASK_RUNNING. try_to_wake_up() returns
Linus Torvalds1da177e2005-04-16 15:20:36 -07003789 * zero in this (rare) case, and we handle it by continuing to scan the queue.
3790 */
Johannes Weiner78ddb082009-04-14 16:53:05 +02003791static void __wake_up_common(wait_queue_head_t *q, unsigned int mode,
Peter Zijlstra63859d42009-09-15 19:14:42 +02003792 int nr_exclusive, int wake_flags, void *key)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003793{
Matthias Kaehlcke2e458742007-10-15 17:00:02 +02003794 wait_queue_t *curr, *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003795
Matthias Kaehlcke2e458742007-10-15 17:00:02 +02003796 list_for_each_entry_safe(curr, next, &q->task_list, task_list) {
Ingo Molnar48f24c42006-07-03 00:25:40 -07003797 unsigned flags = curr->flags;
3798
Peter Zijlstra63859d42009-09-15 19:14:42 +02003799 if (curr->func(curr, mode, wake_flags, key) &&
Ingo Molnar48f24c42006-07-03 00:25:40 -07003800 (flags & WQ_FLAG_EXCLUSIVE) && !--nr_exclusive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003801 break;
3802 }
3803}
3804
3805/**
3806 * __wake_up - wake up threads blocked on a waitqueue.
3807 * @q: the waitqueue
3808 * @mode: which threads
3809 * @nr_exclusive: how many wake-one or wake-many threads to wake up
Martin Waitz67be2dd2005-05-01 08:59:26 -07003810 * @key: is directly passed to the wakeup function
David Howells50fa6102009-04-28 15:01:38 +01003811 *
3812 * It may be assumed that this function implies a write memory barrier before
3813 * changing the task state if and only if any tasks are woken up.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003814 */
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -08003815void __wake_up(wait_queue_head_t *q, unsigned int mode,
Ingo Molnar95cdf3b2005-09-10 00:26:11 -07003816 int nr_exclusive, void *key)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003817{
3818 unsigned long flags;
3819
3820 spin_lock_irqsave(&q->lock, flags);
3821 __wake_up_common(q, mode, nr_exclusive, 0, key);
3822 spin_unlock_irqrestore(&q->lock, flags);
3823}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003824EXPORT_SYMBOL(__wake_up);
3825
3826/*
3827 * Same as __wake_up but called with the spinlock in wait_queue_head_t held.
3828 */
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -08003829void __wake_up_locked(wait_queue_head_t *q, unsigned int mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003830{
3831 __wake_up_common(q, mode, 1, 0, NULL);
3832}
Michal Nazarewicz22c43c82010-05-05 12:53:11 +02003833EXPORT_SYMBOL_GPL(__wake_up_locked);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003834
Davide Libenzi4ede8162009-03-31 15:24:20 -07003835void __wake_up_locked_key(wait_queue_head_t *q, unsigned int mode, void *key)
3836{
3837 __wake_up_common(q, mode, 1, 0, key);
3838}
3839
Linus Torvalds1da177e2005-04-16 15:20:36 -07003840/**
Davide Libenzi4ede8162009-03-31 15:24:20 -07003841 * __wake_up_sync_key - wake up threads blocked on a waitqueue.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003842 * @q: the waitqueue
3843 * @mode: which threads
3844 * @nr_exclusive: how many wake-one or wake-many threads to wake up
Davide Libenzi4ede8162009-03-31 15:24:20 -07003845 * @key: opaque value to be passed to wakeup targets
Linus Torvalds1da177e2005-04-16 15:20:36 -07003846 *
3847 * The sync wakeup differs that the waker knows that it will schedule
3848 * away soon, so while the target thread will be woken up, it will not
3849 * be migrated to another CPU - ie. the two threads are 'synchronized'
3850 * with each other. This can prevent needless bouncing between CPUs.
3851 *
3852 * On UP it can prevent extra preemption.
David Howells50fa6102009-04-28 15:01:38 +01003853 *
3854 * It may be assumed that this function implies a write memory barrier before
3855 * changing the task state if and only if any tasks are woken up.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003856 */
Davide Libenzi4ede8162009-03-31 15:24:20 -07003857void __wake_up_sync_key(wait_queue_head_t *q, unsigned int mode,
3858 int nr_exclusive, void *key)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003859{
3860 unsigned long flags;
Peter Zijlstra7d478722009-09-14 19:55:44 +02003861 int wake_flags = WF_SYNC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003862
3863 if (unlikely(!q))
3864 return;
3865
3866 if (unlikely(!nr_exclusive))
Peter Zijlstra7d478722009-09-14 19:55:44 +02003867 wake_flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003868
3869 spin_lock_irqsave(&q->lock, flags);
Peter Zijlstra7d478722009-09-14 19:55:44 +02003870 __wake_up_common(q, mode, nr_exclusive, wake_flags, key);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003871 spin_unlock_irqrestore(&q->lock, flags);
3872}
Davide Libenzi4ede8162009-03-31 15:24:20 -07003873EXPORT_SYMBOL_GPL(__wake_up_sync_key);
3874
3875/*
3876 * __wake_up_sync - see __wake_up_sync_key()
3877 */
3878void __wake_up_sync(wait_queue_head_t *q, unsigned int mode, int nr_exclusive)
3879{
3880 __wake_up_sync_key(q, mode, nr_exclusive, NULL);
3881}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003882EXPORT_SYMBOL_GPL(__wake_up_sync); /* For internal use only */
3883
Kevin Diggs65eb3dc2008-08-26 10:26:54 +02003884/**
3885 * complete: - signals a single thread waiting on this completion
3886 * @x: holds the state of this particular completion
3887 *
3888 * This will wake up a single thread waiting on this completion. Threads will be
3889 * awakened in the same order in which they were queued.
3890 *
3891 * See also complete_all(), wait_for_completion() and related routines.
David Howells50fa6102009-04-28 15:01:38 +01003892 *
3893 * It may be assumed that this function implies a write memory barrier before
3894 * changing the task state if and only if any tasks are woken up.
Kevin Diggs65eb3dc2008-08-26 10:26:54 +02003895 */
Ingo Molnarb15136e2007-10-24 18:23:48 +02003896void complete(struct completion *x)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003897{
3898 unsigned long flags;
3899
3900 spin_lock_irqsave(&x->wait.lock, flags);
3901 x->done++;
Matthew Wilcoxd9514f62007-12-06 11:07:07 -05003902 __wake_up_common(&x->wait, TASK_NORMAL, 1, 0, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003903 spin_unlock_irqrestore(&x->wait.lock, flags);
3904}
3905EXPORT_SYMBOL(complete);
3906
Kevin Diggs65eb3dc2008-08-26 10:26:54 +02003907/**
3908 * complete_all: - signals all threads waiting on this completion
3909 * @x: holds the state of this particular completion
3910 *
3911 * This will wake up all threads waiting on this particular completion event.
David Howells50fa6102009-04-28 15:01:38 +01003912 *
3913 * It may be assumed that this function implies a write memory barrier before
3914 * changing the task state if and only if any tasks are woken up.
Kevin Diggs65eb3dc2008-08-26 10:26:54 +02003915 */
Ingo Molnarb15136e2007-10-24 18:23:48 +02003916void complete_all(struct completion *x)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003917{
3918 unsigned long flags;
3919
3920 spin_lock_irqsave(&x->wait.lock, flags);
3921 x->done += UINT_MAX/2;
Matthew Wilcoxd9514f62007-12-06 11:07:07 -05003922 __wake_up_common(&x->wait, TASK_NORMAL, 0, 0, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003923 spin_unlock_irqrestore(&x->wait.lock, flags);
3924}
3925EXPORT_SYMBOL(complete_all);
3926
Andi Kleen8cbbe862007-10-15 17:00:14 +02003927static inline long __sched
3928do_wait_for_common(struct completion *x, long timeout, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003929{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003930 if (!x->done) {
3931 DECLARE_WAITQUEUE(wait, current);
3932
Changli Gaoa93d2f12010-05-07 14:33:26 +08003933 __add_wait_queue_tail_exclusive(&x->wait, &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003934 do {
Oleg Nesterov94d3d822008-08-20 16:54:41 -07003935 if (signal_pending_state(state, current)) {
Oleg Nesterovea71a542008-06-20 18:32:20 +04003936 timeout = -ERESTARTSYS;
3937 break;
Andi Kleen8cbbe862007-10-15 17:00:14 +02003938 }
3939 __set_current_state(state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003940 spin_unlock_irq(&x->wait.lock);
Andi Kleen8cbbe862007-10-15 17:00:14 +02003941 timeout = schedule_timeout(timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003942 spin_lock_irq(&x->wait.lock);
Oleg Nesterovea71a542008-06-20 18:32:20 +04003943 } while (!x->done && timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003944 __remove_wait_queue(&x->wait, &wait);
Oleg Nesterovea71a542008-06-20 18:32:20 +04003945 if (!x->done)
3946 return timeout;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003947 }
3948 x->done--;
Oleg Nesterovea71a542008-06-20 18:32:20 +04003949 return timeout ?: 1;
Andi Kleen8cbbe862007-10-15 17:00:14 +02003950}
3951
3952static long __sched
3953wait_for_common(struct completion *x, long timeout, int state)
3954{
3955 might_sleep();
3956
3957 spin_lock_irq(&x->wait.lock);
3958 timeout = do_wait_for_common(x, timeout, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003959 spin_unlock_irq(&x->wait.lock);
Andi Kleen8cbbe862007-10-15 17:00:14 +02003960 return timeout;
3961}
3962
Kevin Diggs65eb3dc2008-08-26 10:26:54 +02003963/**
3964 * wait_for_completion: - waits for completion of a task
3965 * @x: holds the state of this particular completion
3966 *
3967 * This waits to be signaled for completion of a specific task. It is NOT
3968 * interruptible and there is no timeout.
3969 *
3970 * See also similar routines (i.e. wait_for_completion_timeout()) with timeout
3971 * and interrupt capability. Also see complete().
3972 */
Ingo Molnarb15136e2007-10-24 18:23:48 +02003973void __sched wait_for_completion(struct completion *x)
Andi Kleen8cbbe862007-10-15 17:00:14 +02003974{
3975 wait_for_common(x, MAX_SCHEDULE_TIMEOUT, TASK_UNINTERRUPTIBLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003976}
3977EXPORT_SYMBOL(wait_for_completion);
3978
Kevin Diggs65eb3dc2008-08-26 10:26:54 +02003979/**
3980 * wait_for_completion_timeout: - waits for completion of a task (w/timeout)
3981 * @x: holds the state of this particular completion
3982 * @timeout: timeout value in jiffies
3983 *
3984 * This waits for either a completion of a specific task to be signaled or for a
3985 * specified timeout to expire. The timeout is in jiffies. It is not
3986 * interruptible.
3987 */
Ingo Molnarb15136e2007-10-24 18:23:48 +02003988unsigned long __sched
Linus Torvalds1da177e2005-04-16 15:20:36 -07003989wait_for_completion_timeout(struct completion *x, unsigned long timeout)
3990{
Andi Kleen8cbbe862007-10-15 17:00:14 +02003991 return wait_for_common(x, timeout, TASK_UNINTERRUPTIBLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003992}
3993EXPORT_SYMBOL(wait_for_completion_timeout);
3994
Kevin Diggs65eb3dc2008-08-26 10:26:54 +02003995/**
3996 * wait_for_completion_interruptible: - waits for completion of a task (w/intr)
3997 * @x: holds the state of this particular completion
3998 *
3999 * This waits for completion of a specific task to be signaled. It is
4000 * interruptible.
4001 */
Andi Kleen8cbbe862007-10-15 17:00:14 +02004002int __sched wait_for_completion_interruptible(struct completion *x)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004003{
Andi Kleen51e97992007-10-18 21:32:55 +02004004 long t = wait_for_common(x, MAX_SCHEDULE_TIMEOUT, TASK_INTERRUPTIBLE);
4005 if (t == -ERESTARTSYS)
4006 return t;
4007 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004008}
4009EXPORT_SYMBOL(wait_for_completion_interruptible);
4010
Kevin Diggs65eb3dc2008-08-26 10:26:54 +02004011/**
4012 * wait_for_completion_interruptible_timeout: - waits for completion (w/(to,intr))
4013 * @x: holds the state of this particular completion
4014 * @timeout: timeout value in jiffies
4015 *
4016 * This waits for either a completion of a specific task to be signaled or for a
4017 * specified timeout to expire. It is interruptible. The timeout is in jiffies.
4018 */
Ingo Molnarb15136e2007-10-24 18:23:48 +02004019unsigned long __sched
Linus Torvalds1da177e2005-04-16 15:20:36 -07004020wait_for_completion_interruptible_timeout(struct completion *x,
4021 unsigned long timeout)
4022{
Andi Kleen8cbbe862007-10-15 17:00:14 +02004023 return wait_for_common(x, timeout, TASK_INTERRUPTIBLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004024}
4025EXPORT_SYMBOL(wait_for_completion_interruptible_timeout);
4026
Kevin Diggs65eb3dc2008-08-26 10:26:54 +02004027/**
4028 * wait_for_completion_killable: - waits for completion of a task (killable)
4029 * @x: holds the state of this particular completion
4030 *
4031 * This waits to be signaled for completion of a specific task. It can be
4032 * interrupted by a kill signal.
4033 */
Matthew Wilcox009e5772007-12-06 12:29:54 -05004034int __sched wait_for_completion_killable(struct completion *x)
4035{
4036 long t = wait_for_common(x, MAX_SCHEDULE_TIMEOUT, TASK_KILLABLE);
4037 if (t == -ERESTARTSYS)
4038 return t;
4039 return 0;
4040}
4041EXPORT_SYMBOL(wait_for_completion_killable);
4042
Dave Chinnerbe4de352008-08-15 00:40:44 -07004043/**
Sage Weil0aa12fb2010-05-29 09:12:30 -07004044 * wait_for_completion_killable_timeout: - waits for completion of a task (w/(to,killable))
4045 * @x: holds the state of this particular completion
4046 * @timeout: timeout value in jiffies
4047 *
4048 * This waits for either a completion of a specific task to be
4049 * signaled or for a specified timeout to expire. It can be
4050 * interrupted by a kill signal. The timeout is in jiffies.
4051 */
4052unsigned long __sched
4053wait_for_completion_killable_timeout(struct completion *x,
4054 unsigned long timeout)
4055{
4056 return wait_for_common(x, timeout, TASK_KILLABLE);
4057}
4058EXPORT_SYMBOL(wait_for_completion_killable_timeout);
4059
4060/**
Dave Chinnerbe4de352008-08-15 00:40:44 -07004061 * try_wait_for_completion - try to decrement a completion without blocking
4062 * @x: completion structure
4063 *
4064 * Returns: 0 if a decrement cannot be done without blocking
4065 * 1 if a decrement succeeded.
4066 *
4067 * If a completion is being used as a counting completion,
4068 * attempt to decrement the counter without blocking. This
4069 * enables us to avoid waiting if the resource the completion
4070 * is protecting is not available.
4071 */
4072bool try_wait_for_completion(struct completion *x)
4073{
Rafael J. Wysocki7539a3b2009-12-13 00:07:30 +01004074 unsigned long flags;
Dave Chinnerbe4de352008-08-15 00:40:44 -07004075 int ret = 1;
4076
Rafael J. Wysocki7539a3b2009-12-13 00:07:30 +01004077 spin_lock_irqsave(&x->wait.lock, flags);
Dave Chinnerbe4de352008-08-15 00:40:44 -07004078 if (!x->done)
4079 ret = 0;
4080 else
4081 x->done--;
Rafael J. Wysocki7539a3b2009-12-13 00:07:30 +01004082 spin_unlock_irqrestore(&x->wait.lock, flags);
Dave Chinnerbe4de352008-08-15 00:40:44 -07004083 return ret;
4084}
4085EXPORT_SYMBOL(try_wait_for_completion);
4086
4087/**
4088 * completion_done - Test to see if a completion has any waiters
4089 * @x: completion structure
4090 *
4091 * Returns: 0 if there are waiters (wait_for_completion() in progress)
4092 * 1 if there are no waiters.
4093 *
4094 */
4095bool completion_done(struct completion *x)
4096{
Rafael J. Wysocki7539a3b2009-12-13 00:07:30 +01004097 unsigned long flags;
Dave Chinnerbe4de352008-08-15 00:40:44 -07004098 int ret = 1;
4099
Rafael J. Wysocki7539a3b2009-12-13 00:07:30 +01004100 spin_lock_irqsave(&x->wait.lock, flags);
Dave Chinnerbe4de352008-08-15 00:40:44 -07004101 if (!x->done)
4102 ret = 0;
Rafael J. Wysocki7539a3b2009-12-13 00:07:30 +01004103 spin_unlock_irqrestore(&x->wait.lock, flags);
Dave Chinnerbe4de352008-08-15 00:40:44 -07004104 return ret;
4105}
4106EXPORT_SYMBOL(completion_done);
4107
Andi Kleen8cbbe862007-10-15 17:00:14 +02004108static long __sched
4109sleep_on_common(wait_queue_head_t *q, int state, long timeout)
Ingo Molnar0fec1712007-07-09 18:52:01 +02004110{
4111 unsigned long flags;
4112 wait_queue_t wait;
4113
4114 init_waitqueue_entry(&wait, current);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004115
Andi Kleen8cbbe862007-10-15 17:00:14 +02004116 __set_current_state(state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004117
Andi Kleen8cbbe862007-10-15 17:00:14 +02004118 spin_lock_irqsave(&q->lock, flags);
4119 __add_wait_queue(q, &wait);
4120 spin_unlock(&q->lock);
4121 timeout = schedule_timeout(timeout);
4122 spin_lock_irq(&q->lock);
4123 __remove_wait_queue(q, &wait);
4124 spin_unlock_irqrestore(&q->lock, flags);
4125
4126 return timeout;
4127}
4128
4129void __sched interruptible_sleep_on(wait_queue_head_t *q)
4130{
4131 sleep_on_common(q, TASK_INTERRUPTIBLE, MAX_SCHEDULE_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004132}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004133EXPORT_SYMBOL(interruptible_sleep_on);
4134
Ingo Molnar0fec1712007-07-09 18:52:01 +02004135long __sched
Ingo Molnar95cdf3b2005-09-10 00:26:11 -07004136interruptible_sleep_on_timeout(wait_queue_head_t *q, long timeout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004137{
Andi Kleen8cbbe862007-10-15 17:00:14 +02004138 return sleep_on_common(q, TASK_INTERRUPTIBLE, timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004139}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004140EXPORT_SYMBOL(interruptible_sleep_on_timeout);
4141
Ingo Molnar0fec1712007-07-09 18:52:01 +02004142void __sched sleep_on(wait_queue_head_t *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004143{
Andi Kleen8cbbe862007-10-15 17:00:14 +02004144 sleep_on_common(q, TASK_UNINTERRUPTIBLE, MAX_SCHEDULE_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004145}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004146EXPORT_SYMBOL(sleep_on);
4147
Ingo Molnar0fec1712007-07-09 18:52:01 +02004148long __sched sleep_on_timeout(wait_queue_head_t *q, long timeout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004149{
Andi Kleen8cbbe862007-10-15 17:00:14 +02004150 return sleep_on_common(q, TASK_UNINTERRUPTIBLE, timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004151}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004152EXPORT_SYMBOL(sleep_on_timeout);
4153
Ingo Molnarb29739f2006-06-27 02:54:51 -07004154#ifdef CONFIG_RT_MUTEXES
4155
4156/*
4157 * rt_mutex_setprio - set the current priority of a task
4158 * @p: task
4159 * @prio: prio value (kernel-internal form)
4160 *
4161 * This function changes the 'effective' priority of a task. It does
4162 * not touch ->normal_prio like __setscheduler().
4163 *
4164 * Used by the rt_mutex code to implement priority inheritance logic.
4165 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07004166void rt_mutex_setprio(struct task_struct *p, int prio)
Ingo Molnarb29739f2006-06-27 02:54:51 -07004167{
4168 unsigned long flags;
Srivatsa Vaddagiri83b699e2007-10-15 17:00:08 +02004169 int oldprio, on_rq, running;
Ingo Molnar70b97a72006-07-03 00:25:42 -07004170 struct rq *rq;
Thomas Gleixner83ab0aa2010-02-17 09:05:48 +01004171 const struct sched_class *prev_class;
Ingo Molnarb29739f2006-06-27 02:54:51 -07004172
4173 BUG_ON(prio < 0 || prio > MAX_PRIO);
4174
4175 rq = task_rq_lock(p, &flags);
4176
Andrew Mortond5f9f942007-05-08 20:27:06 -07004177 oldprio = p->prio;
Thomas Gleixner83ab0aa2010-02-17 09:05:48 +01004178 prev_class = p->sched_class;
Ingo Molnardd41f592007-07-09 18:51:59 +02004179 on_rq = p->se.on_rq;
Dmitry Adamushko051a1d12007-12-18 15:21:13 +01004180 running = task_current(rq, p);
Hiroshi Shimamoto0e1f3482008-03-10 11:01:20 -07004181 if (on_rq)
Ingo Molnar69be72c2007-08-09 11:16:49 +02004182 dequeue_task(rq, p, 0);
Hiroshi Shimamoto0e1f3482008-03-10 11:01:20 -07004183 if (running)
4184 p->sched_class->put_prev_task(rq, p);
Ingo Molnardd41f592007-07-09 18:51:59 +02004185
4186 if (rt_prio(prio))
4187 p->sched_class = &rt_sched_class;
4188 else
4189 p->sched_class = &fair_sched_class;
4190
Ingo Molnarb29739f2006-06-27 02:54:51 -07004191 p->prio = prio;
4192
Hiroshi Shimamoto0e1f3482008-03-10 11:01:20 -07004193 if (running)
4194 p->sched_class->set_curr_task(rq);
Ingo Molnardd41f592007-07-09 18:51:59 +02004195 if (on_rq) {
Peter Zijlstra371fd7e2010-03-24 16:38:48 +01004196 enqueue_task(rq, p, oldprio < prio ? ENQUEUE_HEAD : 0);
Steven Rostedtcb469842008-01-25 21:08:22 +01004197
4198 check_class_changed(rq, p, prev_class, oldprio, running);
Ingo Molnarb29739f2006-06-27 02:54:51 -07004199 }
4200 task_rq_unlock(rq, &flags);
4201}
4202
4203#endif
4204
Ingo Molnar36c8b582006-07-03 00:25:41 -07004205void set_user_nice(struct task_struct *p, long nice)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004206{
Ingo Molnardd41f592007-07-09 18:51:59 +02004207 int old_prio, delta, on_rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004208 unsigned long flags;
Ingo Molnar70b97a72006-07-03 00:25:42 -07004209 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004210
4211 if (TASK_NICE(p) == nice || nice < -20 || nice > 19)
4212 return;
4213 /*
4214 * We have to be careful, if called from sys_setpriority(),
4215 * the task might be in the middle of scheduling on another CPU.
4216 */
4217 rq = task_rq_lock(p, &flags);
4218 /*
4219 * The RT priorities are set via sched_setscheduler(), but we still
4220 * allow the 'normal' nice value to be set - but as expected
4221 * it wont have any effect on scheduling until the task is
Ingo Molnardd41f592007-07-09 18:51:59 +02004222 * SCHED_FIFO/SCHED_RR:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004223 */
Ingo Molnare05606d2007-07-09 18:51:59 +02004224 if (task_has_rt_policy(p)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004225 p->static_prio = NICE_TO_PRIO(nice);
4226 goto out_unlock;
4227 }
Ingo Molnardd41f592007-07-09 18:51:59 +02004228 on_rq = p->se.on_rq;
Peter Zijlstrac09595f2008-06-27 13:41:14 +02004229 if (on_rq)
Ingo Molnar69be72c2007-08-09 11:16:49 +02004230 dequeue_task(rq, p, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004231
Linus Torvalds1da177e2005-04-16 15:20:36 -07004232 p->static_prio = NICE_TO_PRIO(nice);
Peter Williams2dd73a42006-06-27 02:54:34 -07004233 set_load_weight(p);
Ingo Molnarb29739f2006-06-27 02:54:51 -07004234 old_prio = p->prio;
4235 p->prio = effective_prio(p);
4236 delta = p->prio - old_prio;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004237
Ingo Molnardd41f592007-07-09 18:51:59 +02004238 if (on_rq) {
Peter Zijlstra371fd7e2010-03-24 16:38:48 +01004239 enqueue_task(rq, p, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004240 /*
Andrew Mortond5f9f942007-05-08 20:27:06 -07004241 * If the task increased its priority or is running and
4242 * lowered its priority, then reschedule its CPU:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004243 */
Andrew Mortond5f9f942007-05-08 20:27:06 -07004244 if (delta < 0 || (delta > 0 && task_running(rq, p)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004245 resched_task(rq->curr);
4246 }
4247out_unlock:
4248 task_rq_unlock(rq, &flags);
4249}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004250EXPORT_SYMBOL(set_user_nice);
4251
Matt Mackalle43379f2005-05-01 08:59:00 -07004252/*
4253 * can_nice - check if a task can reduce its nice value
4254 * @p: task
4255 * @nice: nice value
4256 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07004257int can_nice(const struct task_struct *p, const int nice)
Matt Mackalle43379f2005-05-01 08:59:00 -07004258{
Matt Mackall024f4742005-08-18 11:24:19 -07004259 /* convert nice value [19,-20] to rlimit style value [1,40] */
4260 int nice_rlim = 20 - nice;
Ingo Molnar48f24c42006-07-03 00:25:40 -07004261
Jiri Slaby78d7d402010-03-05 13:42:54 -08004262 return (nice_rlim <= task_rlimit(p, RLIMIT_NICE) ||
Matt Mackalle43379f2005-05-01 08:59:00 -07004263 capable(CAP_SYS_NICE));
4264}
4265
Linus Torvalds1da177e2005-04-16 15:20:36 -07004266#ifdef __ARCH_WANT_SYS_NICE
4267
4268/*
4269 * sys_nice - change the priority of the current process.
4270 * @increment: priority increment
4271 *
4272 * sys_setpriority is a more generic, but much slower function that
4273 * does similar things.
4274 */
Heiko Carstens5add95d2009-01-14 14:14:08 +01004275SYSCALL_DEFINE1(nice, int, increment)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004276{
Ingo Molnar48f24c42006-07-03 00:25:40 -07004277 long nice, retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004278
4279 /*
4280 * Setpriority might change our priority at the same moment.
4281 * We don't have to worry. Conceptually one call occurs first
4282 * and we have a single winner.
4283 */
Matt Mackalle43379f2005-05-01 08:59:00 -07004284 if (increment < -40)
4285 increment = -40;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004286 if (increment > 40)
4287 increment = 40;
4288
Américo Wang2b8f8362009-02-16 18:54:21 +08004289 nice = TASK_NICE(current) + increment;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004290 if (nice < -20)
4291 nice = -20;
4292 if (nice > 19)
4293 nice = 19;
4294
Matt Mackalle43379f2005-05-01 08:59:00 -07004295 if (increment < 0 && !can_nice(current, nice))
4296 return -EPERM;
4297
Linus Torvalds1da177e2005-04-16 15:20:36 -07004298 retval = security_task_setnice(current, nice);
4299 if (retval)
4300 return retval;
4301
4302 set_user_nice(current, nice);
4303 return 0;
4304}
4305
4306#endif
4307
4308/**
4309 * task_prio - return the priority value of a given task.
4310 * @p: the task in question.
4311 *
4312 * This is the priority value as seen by users in /proc.
4313 * RT tasks are offset by -200. Normal tasks are centered
4314 * around 0, value goes from -16 to +15.
4315 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07004316int task_prio(const struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004317{
4318 return p->prio - MAX_RT_PRIO;
4319}
4320
4321/**
4322 * task_nice - return the nice value of a given task.
4323 * @p: the task in question.
4324 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07004325int task_nice(const struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004326{
4327 return TASK_NICE(p);
4328}
Pavel Roskin150d8be2008-03-05 16:56:37 -05004329EXPORT_SYMBOL(task_nice);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004330
4331/**
4332 * idle_cpu - is a given cpu idle currently?
4333 * @cpu: the processor in question.
4334 */
4335int idle_cpu(int cpu)
4336{
4337 return cpu_curr(cpu) == cpu_rq(cpu)->idle;
4338}
4339
Linus Torvalds1da177e2005-04-16 15:20:36 -07004340/**
4341 * idle_task - return the idle task for a given cpu.
4342 * @cpu: the processor in question.
4343 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07004344struct task_struct *idle_task(int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004345{
4346 return cpu_rq(cpu)->idle;
4347}
4348
4349/**
4350 * find_process_by_pid - find a process with a matching PID value.
4351 * @pid: the pid in question.
4352 */
Alexey Dobriyana9957442007-10-15 17:00:13 +02004353static struct task_struct *find_process_by_pid(pid_t pid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004354{
Pavel Emelyanov228ebcb2007-10-18 23:40:16 -07004355 return pid ? find_task_by_vpid(pid) : current;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004356}
4357
4358/* Actually do priority change: must hold rq lock. */
Ingo Molnardd41f592007-07-09 18:51:59 +02004359static void
4360__setscheduler(struct rq *rq, struct task_struct *p, int policy, int prio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004361{
Ingo Molnardd41f592007-07-09 18:51:59 +02004362 BUG_ON(p->se.on_rq);
Ingo Molnar48f24c42006-07-03 00:25:40 -07004363
Linus Torvalds1da177e2005-04-16 15:20:36 -07004364 p->policy = policy;
4365 p->rt_priority = prio;
Ingo Molnarb29739f2006-06-27 02:54:51 -07004366 p->normal_prio = normal_prio(p);
4367 /* we are holding p->pi_lock already */
4368 p->prio = rt_mutex_getprio(p);
Peter Zijlstraffd44db2009-11-10 20:12:01 +01004369 if (rt_prio(p->prio))
4370 p->sched_class = &rt_sched_class;
4371 else
4372 p->sched_class = &fair_sched_class;
Peter Williams2dd73a42006-06-27 02:54:34 -07004373 set_load_weight(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004374}
4375
David Howellsc69e8d92008-11-14 10:39:19 +11004376/*
4377 * check the target process has a UID that matches the current process's
4378 */
4379static bool check_same_owner(struct task_struct *p)
4380{
4381 const struct cred *cred = current_cred(), *pcred;
4382 bool match;
4383
4384 rcu_read_lock();
4385 pcred = __task_cred(p);
4386 match = (cred->euid == pcred->euid ||
4387 cred->euid == pcred->uid);
4388 rcu_read_unlock();
4389 return match;
4390}
4391
Rusty Russell961ccdd2008-06-23 13:55:38 +10004392static int __sched_setscheduler(struct task_struct *p, int policy,
4393 struct sched_param *param, bool user)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004394{
Srivatsa Vaddagiri83b699e2007-10-15 17:00:08 +02004395 int retval, oldprio, oldpolicy = -1, on_rq, running;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004396 unsigned long flags;
Thomas Gleixner83ab0aa2010-02-17 09:05:48 +01004397 const struct sched_class *prev_class;
Ingo Molnar70b97a72006-07-03 00:25:42 -07004398 struct rq *rq;
Lennart Poetteringca94c442009-06-15 17:17:47 +02004399 int reset_on_fork;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004400
Steven Rostedt66e53932006-06-27 02:54:44 -07004401 /* may grab non-irq protected spin_locks */
4402 BUG_ON(in_interrupt());
Linus Torvalds1da177e2005-04-16 15:20:36 -07004403recheck:
4404 /* double check policy once rq lock held */
Lennart Poetteringca94c442009-06-15 17:17:47 +02004405 if (policy < 0) {
4406 reset_on_fork = p->sched_reset_on_fork;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004407 policy = oldpolicy = p->policy;
Lennart Poetteringca94c442009-06-15 17:17:47 +02004408 } else {
4409 reset_on_fork = !!(policy & SCHED_RESET_ON_FORK);
4410 policy &= ~SCHED_RESET_ON_FORK;
4411
4412 if (policy != SCHED_FIFO && policy != SCHED_RR &&
4413 policy != SCHED_NORMAL && policy != SCHED_BATCH &&
4414 policy != SCHED_IDLE)
4415 return -EINVAL;
4416 }
4417
Linus Torvalds1da177e2005-04-16 15:20:36 -07004418 /*
4419 * Valid priorities for SCHED_FIFO and SCHED_RR are
Ingo Molnardd41f592007-07-09 18:51:59 +02004420 * 1..MAX_USER_RT_PRIO-1, valid priority for SCHED_NORMAL,
4421 * SCHED_BATCH and SCHED_IDLE is 0.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004422 */
4423 if (param->sched_priority < 0 ||
Ingo Molnar95cdf3b2005-09-10 00:26:11 -07004424 (p->mm && param->sched_priority > MAX_USER_RT_PRIO-1) ||
Steven Rostedtd46523e2005-07-25 16:28:39 -04004425 (!p->mm && param->sched_priority > MAX_RT_PRIO-1))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004426 return -EINVAL;
Ingo Molnare05606d2007-07-09 18:51:59 +02004427 if (rt_policy(policy) != (param->sched_priority != 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004428 return -EINVAL;
4429
Olivier Croquette37e4ab32005-06-25 14:57:32 -07004430 /*
4431 * Allow unprivileged RT tasks to decrease priority:
4432 */
Rusty Russell961ccdd2008-06-23 13:55:38 +10004433 if (user && !capable(CAP_SYS_NICE)) {
Ingo Molnare05606d2007-07-09 18:51:59 +02004434 if (rt_policy(policy)) {
Oleg Nesterov8dc3e902006-09-29 02:00:50 -07004435 unsigned long rlim_rtprio;
Oleg Nesterov5fe1d752006-09-29 02:00:48 -07004436
Oleg Nesterov8dc3e902006-09-29 02:00:50 -07004437 if (!lock_task_sighand(p, &flags))
4438 return -ESRCH;
Jiri Slaby78d7d402010-03-05 13:42:54 -08004439 rlim_rtprio = task_rlimit(p, RLIMIT_RTPRIO);
Oleg Nesterov8dc3e902006-09-29 02:00:50 -07004440 unlock_task_sighand(p, &flags);
Oleg Nesterov5fe1d752006-09-29 02:00:48 -07004441
Oleg Nesterov8dc3e902006-09-29 02:00:50 -07004442 /* can't set/change the rt policy */
4443 if (policy != p->policy && !rlim_rtprio)
4444 return -EPERM;
4445
4446 /* can't increase priority */
4447 if (param->sched_priority > p->rt_priority &&
4448 param->sched_priority > rlim_rtprio)
4449 return -EPERM;
4450 }
Ingo Molnardd41f592007-07-09 18:51:59 +02004451 /*
4452 * Like positive nice levels, dont allow tasks to
4453 * move out of SCHED_IDLE either:
4454 */
4455 if (p->policy == SCHED_IDLE && policy != SCHED_IDLE)
4456 return -EPERM;
Oleg Nesterov8dc3e902006-09-29 02:00:50 -07004457
Olivier Croquette37e4ab32005-06-25 14:57:32 -07004458 /* can't change other user's priorities */
David Howellsc69e8d92008-11-14 10:39:19 +11004459 if (!check_same_owner(p))
Olivier Croquette37e4ab32005-06-25 14:57:32 -07004460 return -EPERM;
Lennart Poetteringca94c442009-06-15 17:17:47 +02004461
4462 /* Normal users shall not reset the sched_reset_on_fork flag */
4463 if (p->sched_reset_on_fork && !reset_on_fork)
4464 return -EPERM;
Olivier Croquette37e4ab32005-06-25 14:57:32 -07004465 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004466
Jeremy Fitzhardinge725aad22008-08-03 09:33:03 -07004467 if (user) {
Peter Zijlstrab68aa232008-02-13 15:45:40 +01004468#ifdef CONFIG_RT_GROUP_SCHED
Jeremy Fitzhardinge725aad22008-08-03 09:33:03 -07004469 /*
4470 * Do not allow realtime tasks into groups that have no runtime
4471 * assigned.
4472 */
Peter Zijlstra9a7e0b12008-08-19 12:33:06 +02004473 if (rt_bandwidth_enabled() && rt_policy(policy) &&
4474 task_group(p)->rt_bandwidth.rt_runtime == 0)
Jeremy Fitzhardinge725aad22008-08-03 09:33:03 -07004475 return -EPERM;
Peter Zijlstrab68aa232008-02-13 15:45:40 +01004476#endif
4477
Jeremy Fitzhardinge725aad22008-08-03 09:33:03 -07004478 retval = security_task_setscheduler(p, policy, param);
4479 if (retval)
4480 return retval;
4481 }
4482
Linus Torvalds1da177e2005-04-16 15:20:36 -07004483 /*
Ingo Molnarb29739f2006-06-27 02:54:51 -07004484 * make sure no PI-waiters arrive (or leave) while we are
4485 * changing the priority of the task:
4486 */
Thomas Gleixner1d615482009-11-17 14:54:03 +01004487 raw_spin_lock_irqsave(&p->pi_lock, flags);
Ingo Molnarb29739f2006-06-27 02:54:51 -07004488 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07004489 * To be able to change p->policy safely, the apropriate
4490 * runqueue lock must be held.
4491 */
Ingo Molnarb29739f2006-06-27 02:54:51 -07004492 rq = __task_rq_lock(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004493 /* recheck policy now with rq lock held */
4494 if (unlikely(oldpolicy != -1 && oldpolicy != p->policy)) {
4495 policy = oldpolicy = -1;
Ingo Molnarb29739f2006-06-27 02:54:51 -07004496 __task_rq_unlock(rq);
Thomas Gleixner1d615482009-11-17 14:54:03 +01004497 raw_spin_unlock_irqrestore(&p->pi_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004498 goto recheck;
4499 }
Ingo Molnardd41f592007-07-09 18:51:59 +02004500 on_rq = p->se.on_rq;
Dmitry Adamushko051a1d12007-12-18 15:21:13 +01004501 running = task_current(rq, p);
Hiroshi Shimamoto0e1f3482008-03-10 11:01:20 -07004502 if (on_rq)
Ingo Molnar2e1cb742007-08-09 11:16:49 +02004503 deactivate_task(rq, p, 0);
Hiroshi Shimamoto0e1f3482008-03-10 11:01:20 -07004504 if (running)
4505 p->sched_class->put_prev_task(rq, p);
Dmitry Adamushkof6b53202007-10-15 17:00:08 +02004506
Lennart Poetteringca94c442009-06-15 17:17:47 +02004507 p->sched_reset_on_fork = reset_on_fork;
4508
Linus Torvalds1da177e2005-04-16 15:20:36 -07004509 oldprio = p->prio;
Thomas Gleixner83ab0aa2010-02-17 09:05:48 +01004510 prev_class = p->sched_class;
Ingo Molnardd41f592007-07-09 18:51:59 +02004511 __setscheduler(rq, p, policy, param->sched_priority);
Dmitry Adamushkof6b53202007-10-15 17:00:08 +02004512
Hiroshi Shimamoto0e1f3482008-03-10 11:01:20 -07004513 if (running)
4514 p->sched_class->set_curr_task(rq);
Ingo Molnardd41f592007-07-09 18:51:59 +02004515 if (on_rq) {
4516 activate_task(rq, p, 0);
Steven Rostedtcb469842008-01-25 21:08:22 +01004517
4518 check_class_changed(rq, p, prev_class, oldprio, running);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004519 }
Ingo Molnarb29739f2006-06-27 02:54:51 -07004520 __task_rq_unlock(rq);
Thomas Gleixner1d615482009-11-17 14:54:03 +01004521 raw_spin_unlock_irqrestore(&p->pi_lock, flags);
Ingo Molnarb29739f2006-06-27 02:54:51 -07004522
Thomas Gleixner95e02ca2006-06-27 02:55:02 -07004523 rt_mutex_adjust_pi(p);
4524
Linus Torvalds1da177e2005-04-16 15:20:36 -07004525 return 0;
4526}
Rusty Russell961ccdd2008-06-23 13:55:38 +10004527
4528/**
4529 * sched_setscheduler - change the scheduling policy and/or RT priority of a thread.
4530 * @p: the task in question.
4531 * @policy: new policy.
4532 * @param: structure containing the new RT priority.
4533 *
4534 * NOTE that the task may be already dead.
4535 */
4536int sched_setscheduler(struct task_struct *p, int policy,
4537 struct sched_param *param)
4538{
4539 return __sched_setscheduler(p, policy, param, true);
4540}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004541EXPORT_SYMBOL_GPL(sched_setscheduler);
4542
Rusty Russell961ccdd2008-06-23 13:55:38 +10004543/**
4544 * sched_setscheduler_nocheck - change the scheduling policy and/or RT priority of a thread from kernelspace.
4545 * @p: the task in question.
4546 * @policy: new policy.
4547 * @param: structure containing the new RT priority.
4548 *
4549 * Just like sched_setscheduler, only don't bother checking if the
4550 * current context has permission. For example, this is needed in
4551 * stop_machine(): we create temporary high priority worker threads,
4552 * but our caller might not have that capability.
4553 */
4554int sched_setscheduler_nocheck(struct task_struct *p, int policy,
4555 struct sched_param *param)
4556{
4557 return __sched_setscheduler(p, policy, param, false);
4558}
4559
Ingo Molnar95cdf3b2005-09-10 00:26:11 -07004560static int
4561do_sched_setscheduler(pid_t pid, int policy, struct sched_param __user *param)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004562{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004563 struct sched_param lparam;
4564 struct task_struct *p;
Ingo Molnar36c8b582006-07-03 00:25:41 -07004565 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004566
4567 if (!param || pid < 0)
4568 return -EINVAL;
4569 if (copy_from_user(&lparam, param, sizeof(struct sched_param)))
4570 return -EFAULT;
Oleg Nesterov5fe1d752006-09-29 02:00:48 -07004571
4572 rcu_read_lock();
4573 retval = -ESRCH;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004574 p = find_process_by_pid(pid);
Oleg Nesterov5fe1d752006-09-29 02:00:48 -07004575 if (p != NULL)
4576 retval = sched_setscheduler(p, policy, &lparam);
4577 rcu_read_unlock();
Ingo Molnar36c8b582006-07-03 00:25:41 -07004578
Linus Torvalds1da177e2005-04-16 15:20:36 -07004579 return retval;
4580}
4581
4582/**
4583 * sys_sched_setscheduler - set/change the scheduler policy and RT priority
4584 * @pid: the pid in question.
4585 * @policy: new policy.
4586 * @param: structure containing the new RT priority.
4587 */
Heiko Carstens5add95d2009-01-14 14:14:08 +01004588SYSCALL_DEFINE3(sched_setscheduler, pid_t, pid, int, policy,
4589 struct sched_param __user *, param)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004590{
Jason Baronc21761f2006-01-18 17:43:03 -08004591 /* negative values for policy are not valid */
4592 if (policy < 0)
4593 return -EINVAL;
4594
Linus Torvalds1da177e2005-04-16 15:20:36 -07004595 return do_sched_setscheduler(pid, policy, param);
4596}
4597
4598/**
4599 * sys_sched_setparam - set/change the RT priority of a thread
4600 * @pid: the pid in question.
4601 * @param: structure containing the new RT priority.
4602 */
Heiko Carstens5add95d2009-01-14 14:14:08 +01004603SYSCALL_DEFINE2(sched_setparam, pid_t, pid, struct sched_param __user *, param)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004604{
4605 return do_sched_setscheduler(pid, -1, param);
4606}
4607
4608/**
4609 * sys_sched_getscheduler - get the policy (scheduling class) of a thread
4610 * @pid: the pid in question.
4611 */
Heiko Carstens5add95d2009-01-14 14:14:08 +01004612SYSCALL_DEFINE1(sched_getscheduler, pid_t, pid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004613{
Ingo Molnar36c8b582006-07-03 00:25:41 -07004614 struct task_struct *p;
Andi Kleen3a5c3592007-10-15 17:00:14 +02004615 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004616
4617 if (pid < 0)
Andi Kleen3a5c3592007-10-15 17:00:14 +02004618 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004619
4620 retval = -ESRCH;
Thomas Gleixner5fe85be2009-12-09 10:14:58 +00004621 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004622 p = find_process_by_pid(pid);
4623 if (p) {
4624 retval = security_task_getscheduler(p);
4625 if (!retval)
Lennart Poetteringca94c442009-06-15 17:17:47 +02004626 retval = p->policy
4627 | (p->sched_reset_on_fork ? SCHED_RESET_ON_FORK : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004628 }
Thomas Gleixner5fe85be2009-12-09 10:14:58 +00004629 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004630 return retval;
4631}
4632
4633/**
Lennart Poetteringca94c442009-06-15 17:17:47 +02004634 * sys_sched_getparam - get the RT priority of a thread
Linus Torvalds1da177e2005-04-16 15:20:36 -07004635 * @pid: the pid in question.
4636 * @param: structure containing the RT priority.
4637 */
Heiko Carstens5add95d2009-01-14 14:14:08 +01004638SYSCALL_DEFINE2(sched_getparam, pid_t, pid, struct sched_param __user *, param)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004639{
4640 struct sched_param lp;
Ingo Molnar36c8b582006-07-03 00:25:41 -07004641 struct task_struct *p;
Andi Kleen3a5c3592007-10-15 17:00:14 +02004642 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004643
4644 if (!param || pid < 0)
Andi Kleen3a5c3592007-10-15 17:00:14 +02004645 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004646
Thomas Gleixner5fe85be2009-12-09 10:14:58 +00004647 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004648 p = find_process_by_pid(pid);
4649 retval = -ESRCH;
4650 if (!p)
4651 goto out_unlock;
4652
4653 retval = security_task_getscheduler(p);
4654 if (retval)
4655 goto out_unlock;
4656
4657 lp.sched_priority = p->rt_priority;
Thomas Gleixner5fe85be2009-12-09 10:14:58 +00004658 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004659
4660 /*
4661 * This one might sleep, we cannot do it with a spinlock held ...
4662 */
4663 retval = copy_to_user(param, &lp, sizeof(*param)) ? -EFAULT : 0;
4664
Linus Torvalds1da177e2005-04-16 15:20:36 -07004665 return retval;
4666
4667out_unlock:
Thomas Gleixner5fe85be2009-12-09 10:14:58 +00004668 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004669 return retval;
4670}
4671
Rusty Russell96f874e2008-11-25 02:35:14 +10304672long sched_setaffinity(pid_t pid, const struct cpumask *in_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004673{
Rusty Russell5a16f3d2008-11-25 02:35:11 +10304674 cpumask_var_t cpus_allowed, new_mask;
Ingo Molnar36c8b582006-07-03 00:25:41 -07004675 struct task_struct *p;
4676 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004677
Gautham R Shenoy95402b32008-01-25 21:08:02 +01004678 get_online_cpus();
Thomas Gleixner23f5d142009-12-09 10:15:01 +00004679 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004680
4681 p = find_process_by_pid(pid);
4682 if (!p) {
Thomas Gleixner23f5d142009-12-09 10:15:01 +00004683 rcu_read_unlock();
Gautham R Shenoy95402b32008-01-25 21:08:02 +01004684 put_online_cpus();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004685 return -ESRCH;
4686 }
4687
Thomas Gleixner23f5d142009-12-09 10:15:01 +00004688 /* Prevent p going away */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004689 get_task_struct(p);
Thomas Gleixner23f5d142009-12-09 10:15:01 +00004690 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004691
Rusty Russell5a16f3d2008-11-25 02:35:11 +10304692 if (!alloc_cpumask_var(&cpus_allowed, GFP_KERNEL)) {
4693 retval = -ENOMEM;
4694 goto out_put_task;
4695 }
4696 if (!alloc_cpumask_var(&new_mask, GFP_KERNEL)) {
4697 retval = -ENOMEM;
4698 goto out_free_cpus_allowed;
4699 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004700 retval = -EPERM;
David Howellsc69e8d92008-11-14 10:39:19 +11004701 if (!check_same_owner(p) && !capable(CAP_SYS_NICE))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004702 goto out_unlock;
4703
David Quigleye7834f82006-06-23 02:03:59 -07004704 retval = security_task_setscheduler(p, 0, NULL);
4705 if (retval)
4706 goto out_unlock;
4707
Rusty Russell5a16f3d2008-11-25 02:35:11 +10304708 cpuset_cpus_allowed(p, cpus_allowed);
4709 cpumask_and(new_mask, in_mask, cpus_allowed);
Paul Menage8707d8b2007-10-18 23:40:22 -07004710 again:
Rusty Russell5a16f3d2008-11-25 02:35:11 +10304711 retval = set_cpus_allowed_ptr(p, new_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004712
Paul Menage8707d8b2007-10-18 23:40:22 -07004713 if (!retval) {
Rusty Russell5a16f3d2008-11-25 02:35:11 +10304714 cpuset_cpus_allowed(p, cpus_allowed);
4715 if (!cpumask_subset(new_mask, cpus_allowed)) {
Paul Menage8707d8b2007-10-18 23:40:22 -07004716 /*
4717 * We must have raced with a concurrent cpuset
4718 * update. Just reset the cpus_allowed to the
4719 * cpuset's cpus_allowed
4720 */
Rusty Russell5a16f3d2008-11-25 02:35:11 +10304721 cpumask_copy(new_mask, cpus_allowed);
Paul Menage8707d8b2007-10-18 23:40:22 -07004722 goto again;
4723 }
4724 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004725out_unlock:
Rusty Russell5a16f3d2008-11-25 02:35:11 +10304726 free_cpumask_var(new_mask);
4727out_free_cpus_allowed:
4728 free_cpumask_var(cpus_allowed);
4729out_put_task:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004730 put_task_struct(p);
Gautham R Shenoy95402b32008-01-25 21:08:02 +01004731 put_online_cpus();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004732 return retval;
4733}
4734
4735static int get_user_cpu_mask(unsigned long __user *user_mask_ptr, unsigned len,
Rusty Russell96f874e2008-11-25 02:35:14 +10304736 struct cpumask *new_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004737{
Rusty Russell96f874e2008-11-25 02:35:14 +10304738 if (len < cpumask_size())
4739 cpumask_clear(new_mask);
4740 else if (len > cpumask_size())
4741 len = cpumask_size();
4742
Linus Torvalds1da177e2005-04-16 15:20:36 -07004743 return copy_from_user(new_mask, user_mask_ptr, len) ? -EFAULT : 0;
4744}
4745
4746/**
4747 * sys_sched_setaffinity - set the cpu affinity of a process
4748 * @pid: pid of the process
4749 * @len: length in bytes of the bitmask pointed to by user_mask_ptr
4750 * @user_mask_ptr: user-space pointer to the new cpu mask
4751 */
Heiko Carstens5add95d2009-01-14 14:14:08 +01004752SYSCALL_DEFINE3(sched_setaffinity, pid_t, pid, unsigned int, len,
4753 unsigned long __user *, user_mask_ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004754{
Rusty Russell5a16f3d2008-11-25 02:35:11 +10304755 cpumask_var_t new_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004756 int retval;
4757
Rusty Russell5a16f3d2008-11-25 02:35:11 +10304758 if (!alloc_cpumask_var(&new_mask, GFP_KERNEL))
4759 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004760
Rusty Russell5a16f3d2008-11-25 02:35:11 +10304761 retval = get_user_cpu_mask(user_mask_ptr, len, new_mask);
4762 if (retval == 0)
4763 retval = sched_setaffinity(pid, new_mask);
4764 free_cpumask_var(new_mask);
4765 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004766}
4767
Rusty Russell96f874e2008-11-25 02:35:14 +10304768long sched_getaffinity(pid_t pid, struct cpumask *mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004769{
Ingo Molnar36c8b582006-07-03 00:25:41 -07004770 struct task_struct *p;
Thomas Gleixner31605682009-12-08 20:24:16 +00004771 unsigned long flags;
4772 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004773 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004774
Gautham R Shenoy95402b32008-01-25 21:08:02 +01004775 get_online_cpus();
Thomas Gleixner23f5d142009-12-09 10:15:01 +00004776 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004777
4778 retval = -ESRCH;
4779 p = find_process_by_pid(pid);
4780 if (!p)
4781 goto out_unlock;
4782
David Quigleye7834f82006-06-23 02:03:59 -07004783 retval = security_task_getscheduler(p);
4784 if (retval)
4785 goto out_unlock;
4786
Thomas Gleixner31605682009-12-08 20:24:16 +00004787 rq = task_rq_lock(p, &flags);
Rusty Russell96f874e2008-11-25 02:35:14 +10304788 cpumask_and(mask, &p->cpus_allowed, cpu_online_mask);
Thomas Gleixner31605682009-12-08 20:24:16 +00004789 task_rq_unlock(rq, &flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004790
4791out_unlock:
Thomas Gleixner23f5d142009-12-09 10:15:01 +00004792 rcu_read_unlock();
Gautham R Shenoy95402b32008-01-25 21:08:02 +01004793 put_online_cpus();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004794
Ulrich Drepper9531b622007-08-09 11:16:46 +02004795 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004796}
4797
4798/**
4799 * sys_sched_getaffinity - get the cpu affinity of a process
4800 * @pid: pid of the process
4801 * @len: length in bytes of the bitmask pointed to by user_mask_ptr
4802 * @user_mask_ptr: user-space pointer to hold the current cpu mask
4803 */
Heiko Carstens5add95d2009-01-14 14:14:08 +01004804SYSCALL_DEFINE3(sched_getaffinity, pid_t, pid, unsigned int, len,
4805 unsigned long __user *, user_mask_ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004806{
4807 int ret;
Rusty Russellf17c8602008-11-25 02:35:11 +10304808 cpumask_var_t mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004809
Anton Blanchard84fba5e2010-04-06 17:02:19 +10004810 if ((len * BITS_PER_BYTE) < nr_cpu_ids)
KOSAKI Motohirocd3d8032010-03-12 16:15:36 +09004811 return -EINVAL;
4812 if (len & (sizeof(unsigned long)-1))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004813 return -EINVAL;
4814
Rusty Russellf17c8602008-11-25 02:35:11 +10304815 if (!alloc_cpumask_var(&mask, GFP_KERNEL))
4816 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004817
Rusty Russellf17c8602008-11-25 02:35:11 +10304818 ret = sched_getaffinity(pid, mask);
4819 if (ret == 0) {
KOSAKI Motohiro8bc037f2010-03-17 09:36:58 +09004820 size_t retlen = min_t(size_t, len, cpumask_size());
KOSAKI Motohirocd3d8032010-03-12 16:15:36 +09004821
4822 if (copy_to_user(user_mask_ptr, mask, retlen))
Rusty Russellf17c8602008-11-25 02:35:11 +10304823 ret = -EFAULT;
4824 else
KOSAKI Motohirocd3d8032010-03-12 16:15:36 +09004825 ret = retlen;
Rusty Russellf17c8602008-11-25 02:35:11 +10304826 }
4827 free_cpumask_var(mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004828
Rusty Russellf17c8602008-11-25 02:35:11 +10304829 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004830}
4831
4832/**
4833 * sys_sched_yield - yield the current processor to other threads.
4834 *
Ingo Molnardd41f592007-07-09 18:51:59 +02004835 * This function yields the current CPU to other tasks. If there are no
4836 * other threads running on this CPU then this function will return.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004837 */
Heiko Carstens5add95d2009-01-14 14:14:08 +01004838SYSCALL_DEFINE0(sched_yield)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004839{
Ingo Molnar70b97a72006-07-03 00:25:42 -07004840 struct rq *rq = this_rq_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004841
Ingo Molnar2d723762007-10-15 17:00:12 +02004842 schedstat_inc(rq, yld_count);
Dmitry Adamushko4530d7a2007-10-15 17:00:08 +02004843 current->sched_class->yield_task(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004844
4845 /*
4846 * Since we are going to call schedule() anyway, there's
4847 * no need to preempt or enable interrupts:
4848 */
4849 __release(rq->lock);
Ingo Molnar8a25d5d2006-07-03 00:24:54 -07004850 spin_release(&rq->lock.dep_map, 1, _THIS_IP_);
Thomas Gleixner9828ea92009-12-03 20:55:53 +01004851 do_raw_spin_unlock(&rq->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004852 preempt_enable_no_resched();
4853
4854 schedule();
4855
4856 return 0;
4857}
4858
Peter Zijlstrad86ee482009-07-10 14:57:57 +02004859static inline int should_resched(void)
4860{
4861 return need_resched() && !(preempt_count() & PREEMPT_ACTIVE);
4862}
4863
Andrew Mortone7b38402006-06-30 01:56:00 -07004864static void __cond_resched(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004865{
Frederic Weisbeckere7aaaa62009-07-16 15:44:29 +02004866 add_preempt_count(PREEMPT_ACTIVE);
4867 schedule();
4868 sub_preempt_count(PREEMPT_ACTIVE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004869}
4870
Herbert Xu02b67cc32008-01-25 21:08:28 +01004871int __sched _cond_resched(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004872{
Peter Zijlstrad86ee482009-07-10 14:57:57 +02004873 if (should_resched()) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004874 __cond_resched();
4875 return 1;
4876 }
4877 return 0;
4878}
Herbert Xu02b67cc32008-01-25 21:08:28 +01004879EXPORT_SYMBOL(_cond_resched);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004880
4881/*
Frederic Weisbecker613afbf2009-07-16 15:44:29 +02004882 * __cond_resched_lock() - if a reschedule is pending, drop the given lock,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004883 * call schedule, and on return reacquire the lock.
4884 *
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01004885 * This works OK both with and without CONFIG_PREEMPT. We do strange low-level
Linus Torvalds1da177e2005-04-16 15:20:36 -07004886 * operations here to prevent schedule() from being called twice (once via
4887 * spin_unlock(), once by hand).
4888 */
Frederic Weisbecker613afbf2009-07-16 15:44:29 +02004889int __cond_resched_lock(spinlock_t *lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004890{
Peter Zijlstrad86ee482009-07-10 14:57:57 +02004891 int resched = should_resched();
Jan Kara6df3cec2005-06-13 15:52:32 -07004892 int ret = 0;
4893
Peter Zijlstraf607c662009-07-20 19:16:29 +02004894 lockdep_assert_held(lock);
4895
Nick Piggin95c354f2008-01-30 13:31:20 +01004896 if (spin_needbreak(lock) || resched) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004897 spin_unlock(lock);
Peter Zijlstrad86ee482009-07-10 14:57:57 +02004898 if (resched)
Nick Piggin95c354f2008-01-30 13:31:20 +01004899 __cond_resched();
4900 else
4901 cpu_relax();
Jan Kara6df3cec2005-06-13 15:52:32 -07004902 ret = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004903 spin_lock(lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004904 }
Jan Kara6df3cec2005-06-13 15:52:32 -07004905 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004906}
Frederic Weisbecker613afbf2009-07-16 15:44:29 +02004907EXPORT_SYMBOL(__cond_resched_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004908
Frederic Weisbecker613afbf2009-07-16 15:44:29 +02004909int __sched __cond_resched_softirq(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004910{
4911 BUG_ON(!in_softirq());
4912
Peter Zijlstrad86ee482009-07-10 14:57:57 +02004913 if (should_resched()) {
Thomas Gleixner98d825672007-05-23 13:58:18 -07004914 local_bh_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004915 __cond_resched();
4916 local_bh_disable();
4917 return 1;
4918 }
4919 return 0;
4920}
Frederic Weisbecker613afbf2009-07-16 15:44:29 +02004921EXPORT_SYMBOL(__cond_resched_softirq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004922
Linus Torvalds1da177e2005-04-16 15:20:36 -07004923/**
4924 * yield - yield the current processor to other threads.
4925 *
Robert P. J. Day72fd4a32007-02-10 01:45:59 -08004926 * This is a shortcut for kernel-space yielding - it marks the
Linus Torvalds1da177e2005-04-16 15:20:36 -07004927 * thread runnable and calls sys_sched_yield().
4928 */
4929void __sched yield(void)
4930{
4931 set_current_state(TASK_RUNNING);
4932 sys_sched_yield();
4933}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004934EXPORT_SYMBOL(yield);
4935
4936/*
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01004937 * This task is about to go to sleep on IO. Increment rq->nr_iowait so
Linus Torvalds1da177e2005-04-16 15:20:36 -07004938 * that process accounting knows that this is a task in IO wait state.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004939 */
4940void __sched io_schedule(void)
4941{
Hitoshi Mitake54d35f22009-06-29 14:44:57 +09004942 struct rq *rq = raw_rq();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004943
Shailabh Nagar0ff92242006-07-14 00:24:37 -07004944 delayacct_blkio_start();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004945 atomic_inc(&rq->nr_iowait);
Arjan van de Ven8f0dfc32009-07-20 11:26:58 -07004946 current->in_iowait = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004947 schedule();
Arjan van de Ven8f0dfc32009-07-20 11:26:58 -07004948 current->in_iowait = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004949 atomic_dec(&rq->nr_iowait);
Shailabh Nagar0ff92242006-07-14 00:24:37 -07004950 delayacct_blkio_end();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004951}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004952EXPORT_SYMBOL(io_schedule);
4953
4954long __sched io_schedule_timeout(long timeout)
4955{
Hitoshi Mitake54d35f22009-06-29 14:44:57 +09004956 struct rq *rq = raw_rq();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004957 long ret;
4958
Shailabh Nagar0ff92242006-07-14 00:24:37 -07004959 delayacct_blkio_start();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004960 atomic_inc(&rq->nr_iowait);
Arjan van de Ven8f0dfc32009-07-20 11:26:58 -07004961 current->in_iowait = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004962 ret = schedule_timeout(timeout);
Arjan van de Ven8f0dfc32009-07-20 11:26:58 -07004963 current->in_iowait = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004964 atomic_dec(&rq->nr_iowait);
Shailabh Nagar0ff92242006-07-14 00:24:37 -07004965 delayacct_blkio_end();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004966 return ret;
4967}
4968
4969/**
4970 * sys_sched_get_priority_max - return maximum RT priority.
4971 * @policy: scheduling class.
4972 *
4973 * this syscall returns the maximum rt_priority that can be used
4974 * by a given scheduling class.
4975 */
Heiko Carstens5add95d2009-01-14 14:14:08 +01004976SYSCALL_DEFINE1(sched_get_priority_max, int, policy)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004977{
4978 int ret = -EINVAL;
4979
4980 switch (policy) {
4981 case SCHED_FIFO:
4982 case SCHED_RR:
4983 ret = MAX_USER_RT_PRIO-1;
4984 break;
4985 case SCHED_NORMAL:
Ingo Molnarb0a94992006-01-14 13:20:41 -08004986 case SCHED_BATCH:
Ingo Molnardd41f592007-07-09 18:51:59 +02004987 case SCHED_IDLE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004988 ret = 0;
4989 break;
4990 }
4991 return ret;
4992}
4993
4994/**
4995 * sys_sched_get_priority_min - return minimum RT priority.
4996 * @policy: scheduling class.
4997 *
4998 * this syscall returns the minimum rt_priority that can be used
4999 * by a given scheduling class.
5000 */
Heiko Carstens5add95d2009-01-14 14:14:08 +01005001SYSCALL_DEFINE1(sched_get_priority_min, int, policy)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005002{
5003 int ret = -EINVAL;
5004
5005 switch (policy) {
5006 case SCHED_FIFO:
5007 case SCHED_RR:
5008 ret = 1;
5009 break;
5010 case SCHED_NORMAL:
Ingo Molnarb0a94992006-01-14 13:20:41 -08005011 case SCHED_BATCH:
Ingo Molnardd41f592007-07-09 18:51:59 +02005012 case SCHED_IDLE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005013 ret = 0;
5014 }
5015 return ret;
5016}
5017
5018/**
5019 * sys_sched_rr_get_interval - return the default timeslice of a process.
5020 * @pid: pid of the process.
5021 * @interval: userspace pointer to the timeslice value.
5022 *
5023 * this syscall writes the default timeslice value of a given process
5024 * into the user-space timespec buffer. A value of '0' means infinity.
5025 */
Heiko Carstens17da2bd2009-01-14 14:14:10 +01005026SYSCALL_DEFINE2(sched_rr_get_interval, pid_t, pid,
Heiko Carstens754fe8d2009-01-14 14:14:09 +01005027 struct timespec __user *, interval)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005028{
Ingo Molnar36c8b582006-07-03 00:25:41 -07005029 struct task_struct *p;
Dmitry Adamushkoa4ec24b2007-10-15 17:00:13 +02005030 unsigned int time_slice;
Thomas Gleixnerdba091b2009-12-09 09:32:03 +01005031 unsigned long flags;
5032 struct rq *rq;
Andi Kleen3a5c3592007-10-15 17:00:14 +02005033 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005034 struct timespec t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005035
5036 if (pid < 0)
Andi Kleen3a5c3592007-10-15 17:00:14 +02005037 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005038
5039 retval = -ESRCH;
Thomas Gleixner1a551ae2009-12-09 10:15:11 +00005040 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005041 p = find_process_by_pid(pid);
5042 if (!p)
5043 goto out_unlock;
5044
5045 retval = security_task_getscheduler(p);
5046 if (retval)
5047 goto out_unlock;
5048
Thomas Gleixnerdba091b2009-12-09 09:32:03 +01005049 rq = task_rq_lock(p, &flags);
5050 time_slice = p->sched_class->get_rr_interval(rq, p);
5051 task_rq_unlock(rq, &flags);
Dmitry Adamushkoa4ec24b2007-10-15 17:00:13 +02005052
Thomas Gleixner1a551ae2009-12-09 10:15:11 +00005053 rcu_read_unlock();
Dmitry Adamushkoa4ec24b2007-10-15 17:00:13 +02005054 jiffies_to_timespec(time_slice, &t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005055 retval = copy_to_user(interval, &t, sizeof(t)) ? -EFAULT : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005056 return retval;
Andi Kleen3a5c3592007-10-15 17:00:14 +02005057
Linus Torvalds1da177e2005-04-16 15:20:36 -07005058out_unlock:
Thomas Gleixner1a551ae2009-12-09 10:15:11 +00005059 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005060 return retval;
5061}
5062
Steven Rostedt7c731e02008-05-12 21:20:41 +02005063static const char stat_nam[] = TASK_STATE_TO_CHAR_STR;
Ingo Molnar36c8b582006-07-03 00:25:41 -07005064
Ingo Molnar82a1fcb2008-01-25 21:08:02 +01005065void sched_show_task(struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005066{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005067 unsigned long free = 0;
Ingo Molnar36c8b582006-07-03 00:25:41 -07005068 unsigned state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005069
Linus Torvalds1da177e2005-04-16 15:20:36 -07005070 state = p->state ? __ffs(p->state) + 1 : 0;
Peter Zijlstra3df0fc52009-12-20 14:23:57 +01005071 printk(KERN_INFO "%-13.13s %c", p->comm,
Andreas Mohr2ed6e342006-07-10 04:43:52 -07005072 state < sizeof(stat_nam) - 1 ? stat_nam[state] : '?');
Ingo Molnar4bd77322007-07-11 21:21:47 +02005073#if BITS_PER_LONG == 32
Linus Torvalds1da177e2005-04-16 15:20:36 -07005074 if (state == TASK_RUNNING)
Peter Zijlstra3df0fc52009-12-20 14:23:57 +01005075 printk(KERN_CONT " running ");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005076 else
Peter Zijlstra3df0fc52009-12-20 14:23:57 +01005077 printk(KERN_CONT " %08lx ", thread_saved_pc(p));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005078#else
5079 if (state == TASK_RUNNING)
Peter Zijlstra3df0fc52009-12-20 14:23:57 +01005080 printk(KERN_CONT " running task ");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005081 else
Peter Zijlstra3df0fc52009-12-20 14:23:57 +01005082 printk(KERN_CONT " %016lx ", thread_saved_pc(p));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005083#endif
5084#ifdef CONFIG_DEBUG_STACK_USAGE
Eric Sandeen7c9f8862008-04-22 16:38:23 -05005085 free = stack_not_used(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005086#endif
Peter Zijlstra3df0fc52009-12-20 14:23:57 +01005087 printk(KERN_CONT "%5lu %5d %6d 0x%08lx\n", free,
David Rientjesaa47b7e2009-05-04 01:38:05 -07005088 task_pid_nr(p), task_pid_nr(p->real_parent),
5089 (unsigned long)task_thread_info(p)->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005090
Nick Piggin5fb5e6d2008-01-25 21:08:34 +01005091 show_stack(p, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005092}
5093
Ingo Molnare59e2ae2006-12-06 20:35:59 -08005094void show_state_filter(unsigned long state_filter)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005095{
Ingo Molnar36c8b582006-07-03 00:25:41 -07005096 struct task_struct *g, *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005097
Ingo Molnar4bd77322007-07-11 21:21:47 +02005098#if BITS_PER_LONG == 32
Peter Zijlstra3df0fc52009-12-20 14:23:57 +01005099 printk(KERN_INFO
5100 " task PC stack pid father\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005101#else
Peter Zijlstra3df0fc52009-12-20 14:23:57 +01005102 printk(KERN_INFO
5103 " task PC stack pid father\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005104#endif
5105 read_lock(&tasklist_lock);
5106 do_each_thread(g, p) {
5107 /*
5108 * reset the NMI-timeout, listing all files on a slow
5109 * console might take alot of time:
5110 */
5111 touch_nmi_watchdog();
Ingo Molnar39bc89f2007-04-25 20:50:03 -07005112 if (!state_filter || (p->state & state_filter))
Ingo Molnar82a1fcb2008-01-25 21:08:02 +01005113 sched_show_task(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005114 } while_each_thread(g, p);
5115
Jeremy Fitzhardinge04c91672007-05-08 00:28:05 -07005116 touch_all_softlockup_watchdogs();
5117
Ingo Molnardd41f592007-07-09 18:51:59 +02005118#ifdef CONFIG_SCHED_DEBUG
5119 sysrq_sched_debug_show();
5120#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07005121 read_unlock(&tasklist_lock);
Ingo Molnare59e2ae2006-12-06 20:35:59 -08005122 /*
5123 * Only show locks if all tasks are dumped:
5124 */
Shmulik Ladkani93335a22009-11-25 15:23:41 +02005125 if (!state_filter)
Ingo Molnare59e2ae2006-12-06 20:35:59 -08005126 debug_show_all_locks();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005127}
5128
Ingo Molnar1df21052007-07-09 18:51:58 +02005129void __cpuinit init_idle_bootup_task(struct task_struct *idle)
5130{
Ingo Molnardd41f592007-07-09 18:51:59 +02005131 idle->sched_class = &idle_sched_class;
Ingo Molnar1df21052007-07-09 18:51:58 +02005132}
5133
Ingo Molnarf340c0d2005-06-28 16:40:42 +02005134/**
5135 * init_idle - set up an idle thread for a given CPU
5136 * @idle: task in question
5137 * @cpu: cpu the idle task belongs to
5138 *
5139 * NOTE: this function does not set the idle thread's NEED_RESCHED
5140 * flag, to make booting more robust.
5141 */
Nick Piggin5c1e1762006-10-03 01:14:04 -07005142void __cpuinit init_idle(struct task_struct *idle, int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005143{
Ingo Molnar70b97a72006-07-03 00:25:42 -07005144 struct rq *rq = cpu_rq(cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005145 unsigned long flags;
5146
Thomas Gleixner05fa7852009-11-17 14:28:38 +01005147 raw_spin_lock_irqsave(&rq->lock, flags);
Ingo Molnar5cbd54e2008-11-12 20:05:50 +01005148
Ingo Molnardd41f592007-07-09 18:51:59 +02005149 __sched_fork(idle);
Peter Zijlstra06b83b52009-12-16 18:04:35 +01005150 idle->state = TASK_RUNNING;
Ingo Molnardd41f592007-07-09 18:51:59 +02005151 idle->se.exec_start = sched_clock();
5152
Rusty Russell96f874e2008-11-25 02:35:14 +10305153 cpumask_copy(&idle->cpus_allowed, cpumask_of(cpu));
Ingo Molnardd41f592007-07-09 18:51:59 +02005154 __set_task_cpu(idle, cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005155
Linus Torvalds1da177e2005-04-16 15:20:36 -07005156 rq->curr = rq->idle = idle;
Nick Piggin4866cde2005-06-25 14:57:23 -07005157#if defined(CONFIG_SMP) && defined(__ARCH_WANT_UNLOCKED_CTXSW)
5158 idle->oncpu = 1;
5159#endif
Thomas Gleixner05fa7852009-11-17 14:28:38 +01005160 raw_spin_unlock_irqrestore(&rq->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005161
5162 /* Set the preempt count _outside_ the spinlocks! */
Linus Torvalds8e3e0762008-05-10 20:58:02 -07005163#if defined(CONFIG_PREEMPT)
5164 task_thread_info(idle)->preempt_count = (idle->lock_depth >= 0);
5165#else
Al Viroa1261f52005-11-13 16:06:55 -08005166 task_thread_info(idle)->preempt_count = 0;
Linus Torvalds8e3e0762008-05-10 20:58:02 -07005167#endif
Ingo Molnardd41f592007-07-09 18:51:59 +02005168 /*
5169 * The idle tasks have their own, simple scheduling class:
5170 */
5171 idle->sched_class = &idle_sched_class;
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01005172 ftrace_graph_init_task(idle);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005173}
5174
5175/*
5176 * In a system that switches off the HZ timer nohz_cpu_mask
5177 * indicates which cpus entered this state. This is used
5178 * in the rcu update to wait only for active cpus. For system
5179 * which do not switch off the HZ timer nohz_cpu_mask should
Rusty Russell6a7b3dc2008-11-25 02:35:04 +10305180 * always be CPU_BITS_NONE.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005181 */
Rusty Russell6a7b3dc2008-11-25 02:35:04 +10305182cpumask_var_t nohz_cpu_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005183
Ingo Molnar19978ca2007-11-09 22:39:38 +01005184/*
5185 * Increase the granularity value when there are more CPUs,
5186 * because with more CPUs the 'effective latency' as visible
5187 * to users decreases. But the relationship is not linear,
5188 * so pick a second-best guess by going with the log2 of the
5189 * number of CPUs.
5190 *
5191 * This idea comes from the SD scheduler of Con Kolivas:
5192 */
Christian Ehrhardtacb4a842009-11-30 12:16:48 +01005193static int get_update_sysctl_factor(void)
Christian Ehrhardt0bcdcf22009-11-30 12:16:46 +01005194{
Mike Galbraith4ca3ef72009-12-10 09:25:53 +01005195 unsigned int cpus = min_t(int, num_online_cpus(), 8);
Christian Ehrhardt1983a922009-11-30 12:16:47 +01005196 unsigned int factor;
5197
5198 switch (sysctl_sched_tunable_scaling) {
5199 case SCHED_TUNABLESCALING_NONE:
5200 factor = 1;
5201 break;
5202 case SCHED_TUNABLESCALING_LINEAR:
5203 factor = cpus;
5204 break;
5205 case SCHED_TUNABLESCALING_LOG:
5206 default:
5207 factor = 1 + ilog2(cpus);
5208 break;
5209 }
Christian Ehrhardt0bcdcf22009-11-30 12:16:46 +01005210
Christian Ehrhardtacb4a842009-11-30 12:16:48 +01005211 return factor;
5212}
5213
5214static void update_sysctl(void)
5215{
5216 unsigned int factor = get_update_sysctl_factor();
5217
Christian Ehrhardt0bcdcf22009-11-30 12:16:46 +01005218#define SET_SYSCTL(name) \
5219 (sysctl_##name = (factor) * normalized_sysctl_##name)
5220 SET_SYSCTL(sched_min_granularity);
5221 SET_SYSCTL(sched_latency);
5222 SET_SYSCTL(sched_wakeup_granularity);
5223 SET_SYSCTL(sched_shares_ratelimit);
5224#undef SET_SYSCTL
5225}
5226
Ingo Molnar19978ca2007-11-09 22:39:38 +01005227static inline void sched_init_granularity(void)
5228{
Christian Ehrhardt0bcdcf22009-11-30 12:16:46 +01005229 update_sysctl();
Ingo Molnar19978ca2007-11-09 22:39:38 +01005230}
5231
Linus Torvalds1da177e2005-04-16 15:20:36 -07005232#ifdef CONFIG_SMP
5233/*
5234 * This is how migration works:
5235 *
Tejun Heo969c7922010-05-06 18:49:21 +02005236 * 1) we invoke migration_cpu_stop() on the target CPU using
5237 * stop_one_cpu().
5238 * 2) stopper starts to run (implicitly forcing the migrated thread
5239 * off the CPU)
5240 * 3) it checks whether the migrated task is still in the wrong runqueue.
5241 * 4) if it's in the wrong runqueue then the migration thread removes
Linus Torvalds1da177e2005-04-16 15:20:36 -07005242 * it and puts it into the right queue.
Tejun Heo969c7922010-05-06 18:49:21 +02005243 * 5) stopper completes and stop_one_cpu() returns and the migration
5244 * is done.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005245 */
5246
5247/*
5248 * Change a given task's CPU affinity. Migrate the thread to a
5249 * proper CPU and schedule it away if the CPU it's executing on
5250 * is removed from the allowed bitmask.
5251 *
5252 * NOTE: the caller must have a valid reference to the task, the
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01005253 * task must not exit() & deallocate itself prematurely. The
Linus Torvalds1da177e2005-04-16 15:20:36 -07005254 * call is not atomic; no spinlocks may be held.
5255 */
Rusty Russell96f874e2008-11-25 02:35:14 +10305256int set_cpus_allowed_ptr(struct task_struct *p, const struct cpumask *new_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005257{
5258 unsigned long flags;
Ingo Molnar70b97a72006-07-03 00:25:42 -07005259 struct rq *rq;
Tejun Heo969c7922010-05-06 18:49:21 +02005260 unsigned int dest_cpu;
Ingo Molnar48f24c42006-07-03 00:25:40 -07005261 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005262
Peter Zijlstra65cc8e42010-03-25 21:05:16 +01005263 /*
5264 * Serialize against TASK_WAKING so that ttwu() and wunt() can
5265 * drop the rq->lock and still rely on ->cpus_allowed.
5266 */
5267again:
5268 while (task_is_waking(p))
5269 cpu_relax();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005270 rq = task_rq_lock(p, &flags);
Peter Zijlstra65cc8e42010-03-25 21:05:16 +01005271 if (task_is_waking(p)) {
5272 task_rq_unlock(rq, &flags);
5273 goto again;
5274 }
Peter Zijlstrae2912002009-12-16 18:04:36 +01005275
Peter Zijlstra6ad4c182009-11-25 13:31:39 +01005276 if (!cpumask_intersects(new_mask, cpu_active_mask)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005277 ret = -EINVAL;
5278 goto out;
5279 }
5280
David Rientjes9985b0b2008-06-05 12:57:11 -07005281 if (unlikely((p->flags & PF_THREAD_BOUND) && p != current &&
Rusty Russell96f874e2008-11-25 02:35:14 +10305282 !cpumask_equal(&p->cpus_allowed, new_mask))) {
David Rientjes9985b0b2008-06-05 12:57:11 -07005283 ret = -EINVAL;
5284 goto out;
5285 }
5286
Gregory Haskins73fe6aa2008-01-25 21:08:07 +01005287 if (p->sched_class->set_cpus_allowed)
Mike Traviscd8ba7c2008-03-26 14:23:49 -07005288 p->sched_class->set_cpus_allowed(p, new_mask);
Gregory Haskins73fe6aa2008-01-25 21:08:07 +01005289 else {
Rusty Russell96f874e2008-11-25 02:35:14 +10305290 cpumask_copy(&p->cpus_allowed, new_mask);
5291 p->rt.nr_cpus_allowed = cpumask_weight(new_mask);
Gregory Haskins73fe6aa2008-01-25 21:08:07 +01005292 }
5293
Linus Torvalds1da177e2005-04-16 15:20:36 -07005294 /* Can the task run on the task's current CPU? If so, we're done */
Rusty Russell96f874e2008-11-25 02:35:14 +10305295 if (cpumask_test_cpu(task_cpu(p), new_mask))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005296 goto out;
5297
Tejun Heo969c7922010-05-06 18:49:21 +02005298 dest_cpu = cpumask_any_and(cpu_active_mask, new_mask);
5299 if (migrate_task(p, dest_cpu)) {
5300 struct migration_arg arg = { p, dest_cpu };
Linus Torvalds1da177e2005-04-16 15:20:36 -07005301 /* Need help from migration thread: drop lock and wait. */
5302 task_rq_unlock(rq, &flags);
Tejun Heo969c7922010-05-06 18:49:21 +02005303 stop_one_cpu(cpu_of(rq), migration_cpu_stop, &arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005304 tlb_migrate_finish(p->mm);
5305 return 0;
5306 }
5307out:
5308 task_rq_unlock(rq, &flags);
Ingo Molnar48f24c42006-07-03 00:25:40 -07005309
Linus Torvalds1da177e2005-04-16 15:20:36 -07005310 return ret;
5311}
Mike Traviscd8ba7c2008-03-26 14:23:49 -07005312EXPORT_SYMBOL_GPL(set_cpus_allowed_ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005313
5314/*
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01005315 * Move (not current) task off this cpu, onto dest cpu. We're doing
Linus Torvalds1da177e2005-04-16 15:20:36 -07005316 * this because either it can't run here any more (set_cpus_allowed()
5317 * away from this CPU, or CPU going down), or because we're
5318 * attempting to rebalance this task on exec (sched_exec).
5319 *
5320 * So we race with normal scheduler movements, but that's OK, as long
5321 * as the task is no longer on this CPU.
Kirill Korotaevefc30812006-06-27 02:54:32 -07005322 *
5323 * Returns non-zero if task was successfully migrated.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005324 */
Kirill Korotaevefc30812006-06-27 02:54:32 -07005325static int __migrate_task(struct task_struct *p, int src_cpu, int dest_cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005326{
Ingo Molnar70b97a72006-07-03 00:25:42 -07005327 struct rq *rq_dest, *rq_src;
Peter Zijlstrae2912002009-12-16 18:04:36 +01005328 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005329
Max Krasnyanskye761b772008-07-15 04:43:49 -07005330 if (unlikely(!cpu_active(dest_cpu)))
Kirill Korotaevefc30812006-06-27 02:54:32 -07005331 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005332
5333 rq_src = cpu_rq(src_cpu);
5334 rq_dest = cpu_rq(dest_cpu);
5335
5336 double_rq_lock(rq_src, rq_dest);
5337 /* Already moved. */
5338 if (task_cpu(p) != src_cpu)
Linus Torvaldsb1e38732008-07-10 11:25:03 -07005339 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005340 /* Affinity changed (again). */
Rusty Russell96f874e2008-11-25 02:35:14 +10305341 if (!cpumask_test_cpu(dest_cpu, &p->cpus_allowed))
Linus Torvaldsb1e38732008-07-10 11:25:03 -07005342 goto fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005343
Peter Zijlstrae2912002009-12-16 18:04:36 +01005344 /*
5345 * If we're not on a rq, the next wake-up will ensure we're
5346 * placed properly.
5347 */
5348 if (p->se.on_rq) {
Ingo Molnar2e1cb742007-08-09 11:16:49 +02005349 deactivate_task(rq_src, p, 0);
Peter Zijlstrae2912002009-12-16 18:04:36 +01005350 set_task_cpu(p, dest_cpu);
Ingo Molnardd41f592007-07-09 18:51:59 +02005351 activate_task(rq_dest, p, 0);
Peter Zijlstra15afe092008-09-20 23:38:02 +02005352 check_preempt_curr(rq_dest, p, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005353 }
Linus Torvaldsb1e38732008-07-10 11:25:03 -07005354done:
Kirill Korotaevefc30812006-06-27 02:54:32 -07005355 ret = 1;
Linus Torvaldsb1e38732008-07-10 11:25:03 -07005356fail:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005357 double_rq_unlock(rq_src, rq_dest);
Kirill Korotaevefc30812006-06-27 02:54:32 -07005358 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005359}
5360
5361/*
Tejun Heo969c7922010-05-06 18:49:21 +02005362 * migration_cpu_stop - this will be executed by a highprio stopper thread
5363 * and performs thread migration by bumping thread off CPU then
5364 * 'pushing' onto another runqueue.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005365 */
Tejun Heo969c7922010-05-06 18:49:21 +02005366static int migration_cpu_stop(void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005367{
Tejun Heo969c7922010-05-06 18:49:21 +02005368 struct migration_arg *arg = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005369
Tejun Heo969c7922010-05-06 18:49:21 +02005370 /*
5371 * The original target cpu might have gone down and we might
5372 * be on another cpu but it doesn't matter.
5373 */
5374 local_irq_disable();
5375 __migrate_task(arg->task, raw_smp_processor_id(), arg->dest_cpu);
5376 local_irq_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005377 return 0;
5378}
5379
5380#ifdef CONFIG_HOTPLUG_CPU
Kirill Korotaev054b9102006-12-10 02:20:11 -08005381/*
Robert P. J. Day3a4fa0a2007-10-19 23:10:43 +02005382 * Figure out where task on dead CPU should go, use force if necessary.
Kirill Korotaev054b9102006-12-10 02:20:11 -08005383 */
Oleg Nesterov6a1bdc12010-03-15 10:10:23 +01005384void move_task_off_dead_cpu(int dead_cpu, struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005385{
Oleg Nesterov1445c082010-03-15 10:10:10 +01005386 struct rq *rq = cpu_rq(dead_cpu);
5387 int needs_cpu, uninitialized_var(dest_cpu);
5388 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005389
Oleg Nesterov1445c082010-03-15 10:10:10 +01005390 local_irq_save(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005391
Oleg Nesterov1445c082010-03-15 10:10:10 +01005392 raw_spin_lock(&rq->lock);
5393 needs_cpu = (task_cpu(p) == dead_cpu) && (p->state != TASK_WAKING);
5394 if (needs_cpu)
5395 dest_cpu = select_fallback_rq(dead_cpu, p);
5396 raw_spin_unlock(&rq->lock);
Oleg Nesterovc1804d52010-03-15 10:10:14 +01005397 /*
5398 * It can only fail if we race with set_cpus_allowed(),
5399 * in the racer should migrate the task anyway.
5400 */
Oleg Nesterov1445c082010-03-15 10:10:10 +01005401 if (needs_cpu)
Oleg Nesterovc1804d52010-03-15 10:10:14 +01005402 __migrate_task(p, dead_cpu, dest_cpu);
Oleg Nesterov1445c082010-03-15 10:10:10 +01005403 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005404}
5405
5406/*
5407 * While a dead CPU has no uninterruptible tasks queued at this point,
5408 * it might still have a nonzero ->nr_uninterruptible counter, because
5409 * for performance reasons the counter is not stricly tracking tasks to
5410 * their home CPUs. So we just add the counter to another CPU's counter,
5411 * to keep the global sum constant after CPU-down:
5412 */
Ingo Molnar70b97a72006-07-03 00:25:42 -07005413static void migrate_nr_uninterruptible(struct rq *rq_src)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005414{
Peter Zijlstra6ad4c182009-11-25 13:31:39 +01005415 struct rq *rq_dest = cpu_rq(cpumask_any(cpu_active_mask));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005416 unsigned long flags;
5417
5418 local_irq_save(flags);
5419 double_rq_lock(rq_src, rq_dest);
5420 rq_dest->nr_uninterruptible += rq_src->nr_uninterruptible;
5421 rq_src->nr_uninterruptible = 0;
5422 double_rq_unlock(rq_src, rq_dest);
5423 local_irq_restore(flags);
5424}
5425
5426/* Run through task list and migrate tasks from the dead cpu. */
5427static void migrate_live_tasks(int src_cpu)
5428{
Ingo Molnar48f24c42006-07-03 00:25:40 -07005429 struct task_struct *p, *t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005430
Oleg Nesterovf7b4cdd2007-10-16 23:30:56 -07005431 read_lock(&tasklist_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005432
Ingo Molnar48f24c42006-07-03 00:25:40 -07005433 do_each_thread(t, p) {
5434 if (p == current)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005435 continue;
5436
Ingo Molnar48f24c42006-07-03 00:25:40 -07005437 if (task_cpu(p) == src_cpu)
5438 move_task_off_dead_cpu(src_cpu, p);
5439 } while_each_thread(t, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005440
Oleg Nesterovf7b4cdd2007-10-16 23:30:56 -07005441 read_unlock(&tasklist_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005442}
5443
Ingo Molnardd41f592007-07-09 18:51:59 +02005444/*
5445 * Schedules idle task to be the next runnable task on current CPU.
Dmitry Adamushko94bc9a72007-11-15 20:57:40 +01005446 * It does so by boosting its priority to highest possible.
5447 * Used by CPU offline code.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005448 */
5449void sched_idle_next(void)
5450{
Ingo Molnar48f24c42006-07-03 00:25:40 -07005451 int this_cpu = smp_processor_id();
Ingo Molnar70b97a72006-07-03 00:25:42 -07005452 struct rq *rq = cpu_rq(this_cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005453 struct task_struct *p = rq->idle;
5454 unsigned long flags;
5455
5456 /* cpu has to be offline */
Ingo Molnar48f24c42006-07-03 00:25:40 -07005457 BUG_ON(cpu_online(this_cpu));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005458
Ingo Molnar48f24c42006-07-03 00:25:40 -07005459 /*
5460 * Strictly not necessary since rest of the CPUs are stopped by now
5461 * and interrupts disabled on the current cpu.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005462 */
Thomas Gleixner05fa7852009-11-17 14:28:38 +01005463 raw_spin_lock_irqsave(&rq->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005464
Ingo Molnardd41f592007-07-09 18:51:59 +02005465 __setscheduler(rq, p, SCHED_FIFO, MAX_RT_PRIO-1);
Ingo Molnar48f24c42006-07-03 00:25:40 -07005466
Dmitry Adamushko94bc9a72007-11-15 20:57:40 +01005467 activate_task(rq, p, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005468
Thomas Gleixner05fa7852009-11-17 14:28:38 +01005469 raw_spin_unlock_irqrestore(&rq->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005470}
5471
Ingo Molnar48f24c42006-07-03 00:25:40 -07005472/*
5473 * Ensures that the idle task is using init_mm right before its cpu goes
Linus Torvalds1da177e2005-04-16 15:20:36 -07005474 * offline.
5475 */
5476void idle_task_exit(void)
5477{
5478 struct mm_struct *mm = current->active_mm;
5479
5480 BUG_ON(cpu_online(smp_processor_id()));
5481
5482 if (mm != &init_mm)
5483 switch_mm(mm, &init_mm, current);
5484 mmdrop(mm);
5485}
5486
Kirill Korotaev054b9102006-12-10 02:20:11 -08005487/* called under rq->lock with disabled interrupts */
Ingo Molnar36c8b582006-07-03 00:25:41 -07005488static void migrate_dead(unsigned int dead_cpu, struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005489{
Ingo Molnar70b97a72006-07-03 00:25:42 -07005490 struct rq *rq = cpu_rq(dead_cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005491
5492 /* Must be exiting, otherwise would be on tasklist. */
Eugene Teo270f7222007-10-18 23:40:38 -07005493 BUG_ON(!p->exit_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005494
5495 /* Cannot have done final schedule yet: would have vanished. */
Oleg Nesterovc394cc92006-09-29 02:01:11 -07005496 BUG_ON(p->state == TASK_DEAD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005497
Ingo Molnar48f24c42006-07-03 00:25:40 -07005498 get_task_struct(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005499
5500 /*
5501 * Drop lock around migration; if someone else moves it,
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01005502 * that's OK. No task can be added to this CPU, so iteration is
Linus Torvalds1da177e2005-04-16 15:20:36 -07005503 * fine.
5504 */
Thomas Gleixner05fa7852009-11-17 14:28:38 +01005505 raw_spin_unlock_irq(&rq->lock);
Ingo Molnar48f24c42006-07-03 00:25:40 -07005506 move_task_off_dead_cpu(dead_cpu, p);
Thomas Gleixner05fa7852009-11-17 14:28:38 +01005507 raw_spin_lock_irq(&rq->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005508
Ingo Molnar48f24c42006-07-03 00:25:40 -07005509 put_task_struct(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005510}
5511
5512/* release_task() removes task from tasklist, so we won't find dead tasks. */
5513static void migrate_dead_tasks(unsigned int dead_cpu)
5514{
Ingo Molnar70b97a72006-07-03 00:25:42 -07005515 struct rq *rq = cpu_rq(dead_cpu);
Ingo Molnardd41f592007-07-09 18:51:59 +02005516 struct task_struct *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005517
Ingo Molnardd41f592007-07-09 18:51:59 +02005518 for ( ; ; ) {
5519 if (!rq->nr_running)
5520 break;
Wang Chenb67802e2009-03-02 13:55:26 +08005521 next = pick_next_task(rq);
Ingo Molnardd41f592007-07-09 18:51:59 +02005522 if (!next)
5523 break;
Dmitry Adamushko79c53792008-06-29 00:16:56 +02005524 next->sched_class->put_prev_task(rq, next);
Ingo Molnardd41f592007-07-09 18:51:59 +02005525 migrate_dead(dead_cpu, next);
Nick Piggine692ab52007-07-26 13:40:43 +02005526
Linus Torvalds1da177e2005-04-16 15:20:36 -07005527 }
5528}
Thomas Gleixnerdce48a82009-04-11 10:43:41 +02005529
5530/*
5531 * remove the tasks which were accounted by rq from calc_load_tasks.
5532 */
5533static void calc_global_load_remove(struct rq *rq)
5534{
5535 atomic_long_sub(rq->calc_load_active, &calc_load_tasks);
Thomas Gleixnera468d382009-07-17 14:15:46 +02005536 rq->calc_load_active = 0;
Thomas Gleixnerdce48a82009-04-11 10:43:41 +02005537}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005538#endif /* CONFIG_HOTPLUG_CPU */
5539
Nick Piggine692ab52007-07-26 13:40:43 +02005540#if defined(CONFIG_SCHED_DEBUG) && defined(CONFIG_SYSCTL)
5541
5542static struct ctl_table sd_ctl_dir[] = {
Alexey Dobriyane0361852007-08-09 11:16:46 +02005543 {
5544 .procname = "sched_domain",
Eric W. Biedermanc57baf12007-08-23 15:18:02 +02005545 .mode = 0555,
Alexey Dobriyane0361852007-08-09 11:16:46 +02005546 },
Eric W. Biederman56992302009-11-05 15:38:40 -08005547 {}
Nick Piggine692ab52007-07-26 13:40:43 +02005548};
5549
5550static struct ctl_table sd_ctl_root[] = {
Alexey Dobriyane0361852007-08-09 11:16:46 +02005551 {
5552 .procname = "kernel",
Eric W. Biedermanc57baf12007-08-23 15:18:02 +02005553 .mode = 0555,
Alexey Dobriyane0361852007-08-09 11:16:46 +02005554 .child = sd_ctl_dir,
5555 },
Eric W. Biederman56992302009-11-05 15:38:40 -08005556 {}
Nick Piggine692ab52007-07-26 13:40:43 +02005557};
5558
5559static struct ctl_table *sd_alloc_ctl_entry(int n)
5560{
5561 struct ctl_table *entry =
Milton Miller5cf9f062007-10-15 17:00:19 +02005562 kcalloc(n, sizeof(struct ctl_table), GFP_KERNEL);
Nick Piggine692ab52007-07-26 13:40:43 +02005563
Nick Piggine692ab52007-07-26 13:40:43 +02005564 return entry;
5565}
5566
Milton Miller6382bc92007-10-15 17:00:19 +02005567static void sd_free_ctl_entry(struct ctl_table **tablep)
5568{
Milton Millercd7900762007-10-17 16:55:11 +02005569 struct ctl_table *entry;
Milton Miller6382bc92007-10-15 17:00:19 +02005570
Milton Millercd7900762007-10-17 16:55:11 +02005571 /*
5572 * In the intermediate directories, both the child directory and
5573 * procname are dynamically allocated and could fail but the mode
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01005574 * will always be set. In the lowest directory the names are
Milton Millercd7900762007-10-17 16:55:11 +02005575 * static strings and all have proc handlers.
5576 */
5577 for (entry = *tablep; entry->mode; entry++) {
Milton Miller6382bc92007-10-15 17:00:19 +02005578 if (entry->child)
5579 sd_free_ctl_entry(&entry->child);
Milton Millercd7900762007-10-17 16:55:11 +02005580 if (entry->proc_handler == NULL)
5581 kfree(entry->procname);
5582 }
Milton Miller6382bc92007-10-15 17:00:19 +02005583
5584 kfree(*tablep);
5585 *tablep = NULL;
5586}
5587
Nick Piggine692ab52007-07-26 13:40:43 +02005588static void
Alexey Dobriyane0361852007-08-09 11:16:46 +02005589set_table_entry(struct ctl_table *entry,
Nick Piggine692ab52007-07-26 13:40:43 +02005590 const char *procname, void *data, int maxlen,
5591 mode_t mode, proc_handler *proc_handler)
5592{
Nick Piggine692ab52007-07-26 13:40:43 +02005593 entry->procname = procname;
5594 entry->data = data;
5595 entry->maxlen = maxlen;
5596 entry->mode = mode;
5597 entry->proc_handler = proc_handler;
5598}
5599
5600static struct ctl_table *
5601sd_alloc_ctl_domain_table(struct sched_domain *sd)
5602{
Ingo Molnara5d8c342008-10-09 11:35:51 +02005603 struct ctl_table *table = sd_alloc_ctl_entry(13);
Nick Piggine692ab52007-07-26 13:40:43 +02005604
Milton Millerad1cdc12007-10-15 17:00:19 +02005605 if (table == NULL)
5606 return NULL;
5607
Alexey Dobriyane0361852007-08-09 11:16:46 +02005608 set_table_entry(&table[0], "min_interval", &sd->min_interval,
Nick Piggine692ab52007-07-26 13:40:43 +02005609 sizeof(long), 0644, proc_doulongvec_minmax);
Alexey Dobriyane0361852007-08-09 11:16:46 +02005610 set_table_entry(&table[1], "max_interval", &sd->max_interval,
Nick Piggine692ab52007-07-26 13:40:43 +02005611 sizeof(long), 0644, proc_doulongvec_minmax);
Alexey Dobriyane0361852007-08-09 11:16:46 +02005612 set_table_entry(&table[2], "busy_idx", &sd->busy_idx,
Nick Piggine692ab52007-07-26 13:40:43 +02005613 sizeof(int), 0644, proc_dointvec_minmax);
Alexey Dobriyane0361852007-08-09 11:16:46 +02005614 set_table_entry(&table[3], "idle_idx", &sd->idle_idx,
Nick Piggine692ab52007-07-26 13:40:43 +02005615 sizeof(int), 0644, proc_dointvec_minmax);
Alexey Dobriyane0361852007-08-09 11:16:46 +02005616 set_table_entry(&table[4], "newidle_idx", &sd->newidle_idx,
Nick Piggine692ab52007-07-26 13:40:43 +02005617 sizeof(int), 0644, proc_dointvec_minmax);
Alexey Dobriyane0361852007-08-09 11:16:46 +02005618 set_table_entry(&table[5], "wake_idx", &sd->wake_idx,
Nick Piggine692ab52007-07-26 13:40:43 +02005619 sizeof(int), 0644, proc_dointvec_minmax);
Alexey Dobriyane0361852007-08-09 11:16:46 +02005620 set_table_entry(&table[6], "forkexec_idx", &sd->forkexec_idx,
Nick Piggine692ab52007-07-26 13:40:43 +02005621 sizeof(int), 0644, proc_dointvec_minmax);
Alexey Dobriyane0361852007-08-09 11:16:46 +02005622 set_table_entry(&table[7], "busy_factor", &sd->busy_factor,
Nick Piggine692ab52007-07-26 13:40:43 +02005623 sizeof(int), 0644, proc_dointvec_minmax);
Alexey Dobriyane0361852007-08-09 11:16:46 +02005624 set_table_entry(&table[8], "imbalance_pct", &sd->imbalance_pct,
Nick Piggine692ab52007-07-26 13:40:43 +02005625 sizeof(int), 0644, proc_dointvec_minmax);
Zou Nan haiace8b3d2007-10-15 17:00:14 +02005626 set_table_entry(&table[9], "cache_nice_tries",
Nick Piggine692ab52007-07-26 13:40:43 +02005627 &sd->cache_nice_tries,
5628 sizeof(int), 0644, proc_dointvec_minmax);
Zou Nan haiace8b3d2007-10-15 17:00:14 +02005629 set_table_entry(&table[10], "flags", &sd->flags,
Nick Piggine692ab52007-07-26 13:40:43 +02005630 sizeof(int), 0644, proc_dointvec_minmax);
Ingo Molnara5d8c342008-10-09 11:35:51 +02005631 set_table_entry(&table[11], "name", sd->name,
5632 CORENAME_MAX_SIZE, 0444, proc_dostring);
5633 /* &table[12] is terminator */
Nick Piggine692ab52007-07-26 13:40:43 +02005634
5635 return table;
5636}
5637
Ingo Molnar9a4e7152007-11-28 15:52:56 +01005638static ctl_table *sd_alloc_ctl_cpu_table(int cpu)
Nick Piggine692ab52007-07-26 13:40:43 +02005639{
5640 struct ctl_table *entry, *table;
5641 struct sched_domain *sd;
5642 int domain_num = 0, i;
5643 char buf[32];
5644
5645 for_each_domain(cpu, sd)
5646 domain_num++;
5647 entry = table = sd_alloc_ctl_entry(domain_num + 1);
Milton Millerad1cdc12007-10-15 17:00:19 +02005648 if (table == NULL)
5649 return NULL;
Nick Piggine692ab52007-07-26 13:40:43 +02005650
5651 i = 0;
5652 for_each_domain(cpu, sd) {
5653 snprintf(buf, 32, "domain%d", i);
Nick Piggine692ab52007-07-26 13:40:43 +02005654 entry->procname = kstrdup(buf, GFP_KERNEL);
Eric W. Biedermanc57baf12007-08-23 15:18:02 +02005655 entry->mode = 0555;
Nick Piggine692ab52007-07-26 13:40:43 +02005656 entry->child = sd_alloc_ctl_domain_table(sd);
5657 entry++;
5658 i++;
5659 }
5660 return table;
5661}
5662
5663static struct ctl_table_header *sd_sysctl_header;
Milton Miller6382bc92007-10-15 17:00:19 +02005664static void register_sched_domain_sysctl(void)
Nick Piggine692ab52007-07-26 13:40:43 +02005665{
Peter Zijlstra6ad4c182009-11-25 13:31:39 +01005666 int i, cpu_num = num_possible_cpus();
Nick Piggine692ab52007-07-26 13:40:43 +02005667 struct ctl_table *entry = sd_alloc_ctl_entry(cpu_num + 1);
5668 char buf[32];
5669
Milton Miller73785472007-10-24 18:23:48 +02005670 WARN_ON(sd_ctl_dir[0].child);
5671 sd_ctl_dir[0].child = entry;
5672
Milton Millerad1cdc12007-10-15 17:00:19 +02005673 if (entry == NULL)
5674 return;
5675
Peter Zijlstra6ad4c182009-11-25 13:31:39 +01005676 for_each_possible_cpu(i) {
Nick Piggine692ab52007-07-26 13:40:43 +02005677 snprintf(buf, 32, "cpu%d", i);
Nick Piggine692ab52007-07-26 13:40:43 +02005678 entry->procname = kstrdup(buf, GFP_KERNEL);
Eric W. Biedermanc57baf12007-08-23 15:18:02 +02005679 entry->mode = 0555;
Nick Piggine692ab52007-07-26 13:40:43 +02005680 entry->child = sd_alloc_ctl_cpu_table(i);
Milton Miller97b6ea72007-10-15 17:00:19 +02005681 entry++;
Nick Piggine692ab52007-07-26 13:40:43 +02005682 }
Milton Miller73785472007-10-24 18:23:48 +02005683
5684 WARN_ON(sd_sysctl_header);
Nick Piggine692ab52007-07-26 13:40:43 +02005685 sd_sysctl_header = register_sysctl_table(sd_ctl_root);
5686}
Milton Miller6382bc92007-10-15 17:00:19 +02005687
Milton Miller73785472007-10-24 18:23:48 +02005688/* may be called multiple times per register */
Milton Miller6382bc92007-10-15 17:00:19 +02005689static void unregister_sched_domain_sysctl(void)
5690{
Milton Miller73785472007-10-24 18:23:48 +02005691 if (sd_sysctl_header)
5692 unregister_sysctl_table(sd_sysctl_header);
Milton Miller6382bc92007-10-15 17:00:19 +02005693 sd_sysctl_header = NULL;
Milton Miller73785472007-10-24 18:23:48 +02005694 if (sd_ctl_dir[0].child)
5695 sd_free_ctl_entry(&sd_ctl_dir[0].child);
Milton Miller6382bc92007-10-15 17:00:19 +02005696}
Nick Piggine692ab52007-07-26 13:40:43 +02005697#else
Milton Miller6382bc92007-10-15 17:00:19 +02005698static void register_sched_domain_sysctl(void)
5699{
5700}
5701static void unregister_sched_domain_sysctl(void)
Nick Piggine692ab52007-07-26 13:40:43 +02005702{
5703}
5704#endif
5705
Gregory Haskins1f11eb62008-06-04 15:04:05 -04005706static void set_rq_online(struct rq *rq)
5707{
5708 if (!rq->online) {
5709 const struct sched_class *class;
5710
Rusty Russellc6c49272008-11-25 02:35:05 +10305711 cpumask_set_cpu(rq->cpu, rq->rd->online);
Gregory Haskins1f11eb62008-06-04 15:04:05 -04005712 rq->online = 1;
5713
5714 for_each_class(class) {
5715 if (class->rq_online)
5716 class->rq_online(rq);
5717 }
5718 }
5719}
5720
5721static void set_rq_offline(struct rq *rq)
5722{
5723 if (rq->online) {
5724 const struct sched_class *class;
5725
5726 for_each_class(class) {
5727 if (class->rq_offline)
5728 class->rq_offline(rq);
5729 }
5730
Rusty Russellc6c49272008-11-25 02:35:05 +10305731 cpumask_clear_cpu(rq->cpu, rq->rd->online);
Gregory Haskins1f11eb62008-06-04 15:04:05 -04005732 rq->online = 0;
5733 }
5734}
5735
Linus Torvalds1da177e2005-04-16 15:20:36 -07005736/*
5737 * migration_call - callback that gets triggered when a CPU is added.
5738 * Here we can start up the necessary migration thread for the new CPU.
5739 */
Ingo Molnar48f24c42006-07-03 00:25:40 -07005740static int __cpuinit
5741migration_call(struct notifier_block *nfb, unsigned long action, void *hcpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005742{
Ingo Molnar48f24c42006-07-03 00:25:40 -07005743 int cpu = (long)hcpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005744 unsigned long flags;
Tejun Heo969c7922010-05-06 18:49:21 +02005745 struct rq *rq = cpu_rq(cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005746
5747 switch (action) {
Gautham R Shenoy5be93612007-05-09 02:34:04 -07005748
Linus Torvalds1da177e2005-04-16 15:20:36 -07005749 case CPU_UP_PREPARE:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07005750 case CPU_UP_PREPARE_FROZEN:
Thomas Gleixnera468d382009-07-17 14:15:46 +02005751 rq->calc_load_update = calc_load_update;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005752 break;
Ingo Molnar48f24c42006-07-03 00:25:40 -07005753
Linus Torvalds1da177e2005-04-16 15:20:36 -07005754 case CPU_ONLINE:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07005755 case CPU_ONLINE_FROZEN:
Gregory Haskins1f94ef52008-03-10 16:52:41 -04005756 /* Update our root-domain */
Thomas Gleixner05fa7852009-11-17 14:28:38 +01005757 raw_spin_lock_irqsave(&rq->lock, flags);
Gregory Haskins1f94ef52008-03-10 16:52:41 -04005758 if (rq->rd) {
Rusty Russellc6c49272008-11-25 02:35:05 +10305759 BUG_ON(!cpumask_test_cpu(cpu, rq->rd->span));
Gregory Haskins1f11eb62008-06-04 15:04:05 -04005760
5761 set_rq_online(rq);
Gregory Haskins1f94ef52008-03-10 16:52:41 -04005762 }
Thomas Gleixner05fa7852009-11-17 14:28:38 +01005763 raw_spin_unlock_irqrestore(&rq->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005764 break;
Ingo Molnar48f24c42006-07-03 00:25:40 -07005765
Linus Torvalds1da177e2005-04-16 15:20:36 -07005766#ifdef CONFIG_HOTPLUG_CPU
Linus Torvalds1da177e2005-04-16 15:20:36 -07005767 case CPU_DEAD:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07005768 case CPU_DEAD_FROZEN:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005769 migrate_live_tasks(cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005770 /* Idle task back to normal (off runqueue, low prio) */
Thomas Gleixner05fa7852009-11-17 14:28:38 +01005771 raw_spin_lock_irq(&rq->lock);
Ingo Molnar2e1cb742007-08-09 11:16:49 +02005772 deactivate_task(rq, rq->idle, 0);
Ingo Molnardd41f592007-07-09 18:51:59 +02005773 __setscheduler(rq, rq->idle, SCHED_NORMAL, 0);
5774 rq->idle->sched_class = &idle_sched_class;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005775 migrate_dead_tasks(cpu);
Thomas Gleixner05fa7852009-11-17 14:28:38 +01005776 raw_spin_unlock_irq(&rq->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005777 migrate_nr_uninterruptible(rq);
5778 BUG_ON(rq->nr_running != 0);
Thomas Gleixnerdce48a82009-04-11 10:43:41 +02005779 calc_global_load_remove(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005780 break;
Gregory Haskins57d885f2008-01-25 21:08:18 +01005781
Gregory Haskins08f503b2008-03-10 17:59:11 -04005782 case CPU_DYING:
5783 case CPU_DYING_FROZEN:
Gregory Haskins57d885f2008-01-25 21:08:18 +01005784 /* Update our root-domain */
Thomas Gleixner05fa7852009-11-17 14:28:38 +01005785 raw_spin_lock_irqsave(&rq->lock, flags);
Gregory Haskins57d885f2008-01-25 21:08:18 +01005786 if (rq->rd) {
Rusty Russellc6c49272008-11-25 02:35:05 +10305787 BUG_ON(!cpumask_test_cpu(cpu, rq->rd->span));
Gregory Haskins1f11eb62008-06-04 15:04:05 -04005788 set_rq_offline(rq);
Gregory Haskins57d885f2008-01-25 21:08:18 +01005789 }
Thomas Gleixner05fa7852009-11-17 14:28:38 +01005790 raw_spin_unlock_irqrestore(&rq->lock, flags);
Gregory Haskins57d885f2008-01-25 21:08:18 +01005791 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005792#endif
5793 }
5794 return NOTIFY_OK;
5795}
5796
Paul Mackerrasf38b0822009-06-02 21:05:16 +10005797/*
5798 * Register at high priority so that task migration (migrate_all_tasks)
5799 * happens before everything else. This has to be lower priority than
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005800 * the notifier in the perf_event subsystem, though.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005801 */
Chandra Seetharaman26c21432006-06-27 02:54:10 -07005802static struct notifier_block __cpuinitdata migration_notifier = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005803 .notifier_call = migration_call,
Tejun Heo50a323b2010-06-08 21:40:36 +02005804 .priority = CPU_PRI_MIGRATION,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005805};
5806
Tejun Heo3a101d02010-06-08 21:40:36 +02005807static int __cpuinit sched_cpu_active(struct notifier_block *nfb,
5808 unsigned long action, void *hcpu)
5809{
5810 switch (action & ~CPU_TASKS_FROZEN) {
5811 case CPU_ONLINE:
5812 case CPU_DOWN_FAILED:
5813 set_cpu_active((long)hcpu, true);
5814 return NOTIFY_OK;
5815 default:
5816 return NOTIFY_DONE;
5817 }
5818}
5819
5820static int __cpuinit sched_cpu_inactive(struct notifier_block *nfb,
5821 unsigned long action, void *hcpu)
5822{
5823 switch (action & ~CPU_TASKS_FROZEN) {
5824 case CPU_DOWN_PREPARE:
5825 set_cpu_active((long)hcpu, false);
5826 return NOTIFY_OK;
5827 default:
5828 return NOTIFY_DONE;
5829 }
5830}
5831
Eduard - Gabriel Munteanu7babe8d2008-07-25 19:45:11 -07005832static int __init migration_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005833{
5834 void *cpu = (void *)(long)smp_processor_id();
Akinobu Mita07dccf32006-09-29 02:00:22 -07005835 int err;
Ingo Molnar48f24c42006-07-03 00:25:40 -07005836
Tejun Heo3a101d02010-06-08 21:40:36 +02005837 /* Initialize migration for the boot CPU */
Akinobu Mita07dccf32006-09-29 02:00:22 -07005838 err = migration_call(&migration_notifier, CPU_UP_PREPARE, cpu);
5839 BUG_ON(err == NOTIFY_BAD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005840 migration_call(&migration_notifier, CPU_ONLINE, cpu);
5841 register_cpu_notifier(&migration_notifier);
Eduard - Gabriel Munteanu7babe8d2008-07-25 19:45:11 -07005842
Tejun Heo3a101d02010-06-08 21:40:36 +02005843 /* Register cpu active notifiers */
5844 cpu_notifier(sched_cpu_active, CPU_PRI_SCHED_ACTIVE);
5845 cpu_notifier(sched_cpu_inactive, CPU_PRI_SCHED_INACTIVE);
5846
Thomas Gleixnera004cd42009-07-21 09:54:05 +02005847 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005848}
Eduard - Gabriel Munteanu7babe8d2008-07-25 19:45:11 -07005849early_initcall(migration_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005850#endif
5851
5852#ifdef CONFIG_SMP
Christoph Lameter476f3532007-05-06 14:48:58 -07005853
Ingo Molnar3e9830d2007-10-15 17:00:13 +02005854#ifdef CONFIG_SCHED_DEBUG
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02005855
Mike Travisf6630112009-11-17 18:22:15 -06005856static __read_mostly int sched_domain_debug_enabled;
5857
5858static int __init sched_domain_debug_setup(char *str)
5859{
5860 sched_domain_debug_enabled = 1;
5861
5862 return 0;
5863}
5864early_param("sched_debug", sched_domain_debug_setup);
5865
Mike Travis7c16ec52008-04-04 18:11:11 -07005866static int sched_domain_debug_one(struct sched_domain *sd, int cpu, int level,
Rusty Russell96f874e2008-11-25 02:35:14 +10305867 struct cpumask *groupmask)
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02005868{
5869 struct sched_group *group = sd->groups;
Mike Travis434d53b2008-04-04 18:11:04 -07005870 char str[256];
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02005871
Rusty Russell968ea6d2008-12-13 21:55:51 +10305872 cpulist_scnprintf(str, sizeof(str), sched_domain_span(sd));
Rusty Russell96f874e2008-11-25 02:35:14 +10305873 cpumask_clear(groupmask);
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02005874
5875 printk(KERN_DEBUG "%*s domain %d: ", level, "", level);
5876
5877 if (!(sd->flags & SD_LOAD_BALANCE)) {
Peter Zijlstra3df0fc52009-12-20 14:23:57 +01005878 printk("does not load-balance\n");
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02005879 if (sd->parent)
Peter Zijlstra3df0fc52009-12-20 14:23:57 +01005880 printk(KERN_ERR "ERROR: !SD_LOAD_BALANCE domain"
5881 " has parent");
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02005882 return -1;
5883 }
5884
Peter Zijlstra3df0fc52009-12-20 14:23:57 +01005885 printk(KERN_CONT "span %s level %s\n", str, sd->name);
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02005886
Rusty Russell758b2cd2008-11-25 02:35:04 +10305887 if (!cpumask_test_cpu(cpu, sched_domain_span(sd))) {
Peter Zijlstra3df0fc52009-12-20 14:23:57 +01005888 printk(KERN_ERR "ERROR: domain->span does not contain "
5889 "CPU%d\n", cpu);
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02005890 }
Rusty Russell758b2cd2008-11-25 02:35:04 +10305891 if (!cpumask_test_cpu(cpu, sched_group_cpus(group))) {
Peter Zijlstra3df0fc52009-12-20 14:23:57 +01005892 printk(KERN_ERR "ERROR: domain->groups does not contain"
5893 " CPU%d\n", cpu);
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02005894 }
5895
5896 printk(KERN_DEBUG "%*s groups:", level + 1, "");
5897 do {
5898 if (!group) {
Peter Zijlstra3df0fc52009-12-20 14:23:57 +01005899 printk("\n");
5900 printk(KERN_ERR "ERROR: group is NULL\n");
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02005901 break;
5902 }
5903
Peter Zijlstra18a38852009-09-01 10:34:39 +02005904 if (!group->cpu_power) {
Peter Zijlstra3df0fc52009-12-20 14:23:57 +01005905 printk(KERN_CONT "\n");
5906 printk(KERN_ERR "ERROR: domain->cpu_power not "
5907 "set\n");
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02005908 break;
5909 }
5910
Rusty Russell758b2cd2008-11-25 02:35:04 +10305911 if (!cpumask_weight(sched_group_cpus(group))) {
Peter Zijlstra3df0fc52009-12-20 14:23:57 +01005912 printk(KERN_CONT "\n");
5913 printk(KERN_ERR "ERROR: empty group\n");
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02005914 break;
5915 }
5916
Rusty Russell758b2cd2008-11-25 02:35:04 +10305917 if (cpumask_intersects(groupmask, sched_group_cpus(group))) {
Peter Zijlstra3df0fc52009-12-20 14:23:57 +01005918 printk(KERN_CONT "\n");
5919 printk(KERN_ERR "ERROR: repeated CPUs\n");
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02005920 break;
5921 }
5922
Rusty Russell758b2cd2008-11-25 02:35:04 +10305923 cpumask_or(groupmask, groupmask, sched_group_cpus(group));
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02005924
Rusty Russell968ea6d2008-12-13 21:55:51 +10305925 cpulist_scnprintf(str, sizeof(str), sched_group_cpus(group));
Gautham R Shenoy381512c2009-04-14 09:09:36 +05305926
Peter Zijlstra3df0fc52009-12-20 14:23:57 +01005927 printk(KERN_CONT " %s", str);
Peter Zijlstra18a38852009-09-01 10:34:39 +02005928 if (group->cpu_power != SCHED_LOAD_SCALE) {
Peter Zijlstra3df0fc52009-12-20 14:23:57 +01005929 printk(KERN_CONT " (cpu_power = %d)",
5930 group->cpu_power);
Gautham R Shenoy381512c2009-04-14 09:09:36 +05305931 }
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02005932
5933 group = group->next;
5934 } while (group != sd->groups);
Peter Zijlstra3df0fc52009-12-20 14:23:57 +01005935 printk(KERN_CONT "\n");
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02005936
Rusty Russell758b2cd2008-11-25 02:35:04 +10305937 if (!cpumask_equal(sched_domain_span(sd), groupmask))
Peter Zijlstra3df0fc52009-12-20 14:23:57 +01005938 printk(KERN_ERR "ERROR: groups don't span domain->span\n");
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02005939
Rusty Russell758b2cd2008-11-25 02:35:04 +10305940 if (sd->parent &&
5941 !cpumask_subset(groupmask, sched_domain_span(sd->parent)))
Peter Zijlstra3df0fc52009-12-20 14:23:57 +01005942 printk(KERN_ERR "ERROR: parent span is not a superset "
5943 "of domain->span\n");
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02005944 return 0;
5945}
5946
Linus Torvalds1da177e2005-04-16 15:20:36 -07005947static void sched_domain_debug(struct sched_domain *sd, int cpu)
5948{
Rusty Russelld5dd3db2008-11-25 02:35:12 +10305949 cpumask_var_t groupmask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005950 int level = 0;
5951
Mike Travisf6630112009-11-17 18:22:15 -06005952 if (!sched_domain_debug_enabled)
5953 return;
5954
Nick Piggin41c7ce92005-06-25 14:57:24 -07005955 if (!sd) {
5956 printk(KERN_DEBUG "CPU%d attaching NULL sched-domain.\n", cpu);
5957 return;
5958 }
5959
Linus Torvalds1da177e2005-04-16 15:20:36 -07005960 printk(KERN_DEBUG "CPU%d attaching sched-domain:\n", cpu);
5961
Rusty Russelld5dd3db2008-11-25 02:35:12 +10305962 if (!alloc_cpumask_var(&groupmask, GFP_KERNEL)) {
Mike Travis7c16ec52008-04-04 18:11:11 -07005963 printk(KERN_DEBUG "Cannot load-balance (out of memory)\n");
5964 return;
5965 }
5966
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02005967 for (;;) {
Mike Travis7c16ec52008-04-04 18:11:11 -07005968 if (sched_domain_debug_one(sd, cpu, level, groupmask))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005969 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005970 level++;
5971 sd = sd->parent;
Miguel Ojeda Sandonis33859f72006-12-10 02:20:38 -08005972 if (!sd)
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02005973 break;
5974 }
Rusty Russelld5dd3db2008-11-25 02:35:12 +10305975 free_cpumask_var(groupmask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005976}
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +02005977#else /* !CONFIG_SCHED_DEBUG */
Ingo Molnar48f24c42006-07-03 00:25:40 -07005978# define sched_domain_debug(sd, cpu) do { } while (0)
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +02005979#endif /* CONFIG_SCHED_DEBUG */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005980
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07005981static int sd_degenerate(struct sched_domain *sd)
Suresh Siddha245af2c2005-06-25 14:57:25 -07005982{
Rusty Russell758b2cd2008-11-25 02:35:04 +10305983 if (cpumask_weight(sched_domain_span(sd)) == 1)
Suresh Siddha245af2c2005-06-25 14:57:25 -07005984 return 1;
5985
5986 /* Following flags need at least 2 groups */
5987 if (sd->flags & (SD_LOAD_BALANCE |
5988 SD_BALANCE_NEWIDLE |
5989 SD_BALANCE_FORK |
Siddha, Suresh B89c47102006-10-03 01:14:09 -07005990 SD_BALANCE_EXEC |
5991 SD_SHARE_CPUPOWER |
5992 SD_SHARE_PKG_RESOURCES)) {
Suresh Siddha245af2c2005-06-25 14:57:25 -07005993 if (sd->groups != sd->groups->next)
5994 return 0;
5995 }
5996
5997 /* Following flags don't use groups */
Peter Zijlstrac88d5912009-09-10 13:50:02 +02005998 if (sd->flags & (SD_WAKE_AFFINE))
Suresh Siddha245af2c2005-06-25 14:57:25 -07005999 return 0;
6000
6001 return 1;
6002}
6003
Ingo Molnar48f24c42006-07-03 00:25:40 -07006004static int
6005sd_parent_degenerate(struct sched_domain *sd, struct sched_domain *parent)
Suresh Siddha245af2c2005-06-25 14:57:25 -07006006{
6007 unsigned long cflags = sd->flags, pflags = parent->flags;
6008
6009 if (sd_degenerate(parent))
6010 return 1;
6011
Rusty Russell758b2cd2008-11-25 02:35:04 +10306012 if (!cpumask_equal(sched_domain_span(sd), sched_domain_span(parent)))
Suresh Siddha245af2c2005-06-25 14:57:25 -07006013 return 0;
6014
Suresh Siddha245af2c2005-06-25 14:57:25 -07006015 /* Flags needing groups don't count if only 1 group in parent */
6016 if (parent->groups == parent->groups->next) {
6017 pflags &= ~(SD_LOAD_BALANCE |
6018 SD_BALANCE_NEWIDLE |
6019 SD_BALANCE_FORK |
Siddha, Suresh B89c47102006-10-03 01:14:09 -07006020 SD_BALANCE_EXEC |
6021 SD_SHARE_CPUPOWER |
6022 SD_SHARE_PKG_RESOURCES);
Ken Chen54364992008-12-07 18:47:37 -08006023 if (nr_node_ids == 1)
6024 pflags &= ~SD_SERIALIZE;
Suresh Siddha245af2c2005-06-25 14:57:25 -07006025 }
6026 if (~cflags & pflags)
6027 return 0;
6028
6029 return 1;
6030}
6031
Rusty Russellc6c49272008-11-25 02:35:05 +10306032static void free_rootdomain(struct root_domain *rd)
6033{
Peter Zijlstra047106a2009-11-16 10:28:09 +01006034 synchronize_sched();
6035
Rusty Russell68e74562008-11-25 02:35:13 +10306036 cpupri_cleanup(&rd->cpupri);
6037
Rusty Russellc6c49272008-11-25 02:35:05 +10306038 free_cpumask_var(rd->rto_mask);
6039 free_cpumask_var(rd->online);
6040 free_cpumask_var(rd->span);
6041 kfree(rd);
6042}
6043
Gregory Haskins57d885f2008-01-25 21:08:18 +01006044static void rq_attach_root(struct rq *rq, struct root_domain *rd)
6045{
Ingo Molnara0490fa2009-02-12 11:35:40 +01006046 struct root_domain *old_rd = NULL;
Gregory Haskins57d885f2008-01-25 21:08:18 +01006047 unsigned long flags;
Gregory Haskins57d885f2008-01-25 21:08:18 +01006048
Thomas Gleixner05fa7852009-11-17 14:28:38 +01006049 raw_spin_lock_irqsave(&rq->lock, flags);
Gregory Haskins57d885f2008-01-25 21:08:18 +01006050
6051 if (rq->rd) {
Ingo Molnara0490fa2009-02-12 11:35:40 +01006052 old_rd = rq->rd;
Gregory Haskins57d885f2008-01-25 21:08:18 +01006053
Rusty Russellc6c49272008-11-25 02:35:05 +10306054 if (cpumask_test_cpu(rq->cpu, old_rd->online))
Gregory Haskins1f11eb62008-06-04 15:04:05 -04006055 set_rq_offline(rq);
Gregory Haskins57d885f2008-01-25 21:08:18 +01006056
Rusty Russellc6c49272008-11-25 02:35:05 +10306057 cpumask_clear_cpu(rq->cpu, old_rd->span);
Gregory Haskinsdc938522008-01-25 21:08:26 +01006058
Ingo Molnara0490fa2009-02-12 11:35:40 +01006059 /*
6060 * If we dont want to free the old_rt yet then
6061 * set old_rd to NULL to skip the freeing later
6062 * in this function:
6063 */
6064 if (!atomic_dec_and_test(&old_rd->refcount))
6065 old_rd = NULL;
Gregory Haskins57d885f2008-01-25 21:08:18 +01006066 }
6067
6068 atomic_inc(&rd->refcount);
6069 rq->rd = rd;
6070
Rusty Russellc6c49272008-11-25 02:35:05 +10306071 cpumask_set_cpu(rq->cpu, rd->span);
Gregory Haskins00aec932009-07-30 10:57:23 -04006072 if (cpumask_test_cpu(rq->cpu, cpu_active_mask))
Gregory Haskins1f11eb62008-06-04 15:04:05 -04006073 set_rq_online(rq);
Gregory Haskins57d885f2008-01-25 21:08:18 +01006074
Thomas Gleixner05fa7852009-11-17 14:28:38 +01006075 raw_spin_unlock_irqrestore(&rq->lock, flags);
Ingo Molnara0490fa2009-02-12 11:35:40 +01006076
6077 if (old_rd)
6078 free_rootdomain(old_rd);
Gregory Haskins57d885f2008-01-25 21:08:18 +01006079}
6080
Li Zefanfd5e1b52009-06-15 13:34:19 +08006081static int init_rootdomain(struct root_domain *rd, bool bootmem)
Gregory Haskins57d885f2008-01-25 21:08:18 +01006082{
Pekka Enberg36b7b6d2009-06-10 23:42:36 +03006083 gfp_t gfp = GFP_KERNEL;
6084
Gregory Haskins57d885f2008-01-25 21:08:18 +01006085 memset(rd, 0, sizeof(*rd));
6086
Pekka Enberg36b7b6d2009-06-10 23:42:36 +03006087 if (bootmem)
6088 gfp = GFP_NOWAIT;
Gregory Haskins6e0534f2008-05-12 21:21:01 +02006089
Pekka Enberg36b7b6d2009-06-10 23:42:36 +03006090 if (!alloc_cpumask_var(&rd->span, gfp))
Li Zefan0c910d22009-01-06 17:39:06 +08006091 goto out;
Pekka Enberg36b7b6d2009-06-10 23:42:36 +03006092 if (!alloc_cpumask_var(&rd->online, gfp))
Rusty Russellc6c49272008-11-25 02:35:05 +10306093 goto free_span;
Pekka Enberg36b7b6d2009-06-10 23:42:36 +03006094 if (!alloc_cpumask_var(&rd->rto_mask, gfp))
Rusty Russellc6c49272008-11-25 02:35:05 +10306095 goto free_online;
Gregory Haskins6e0534f2008-05-12 21:21:01 +02006096
Pekka Enberg0fb53022009-06-11 08:41:22 +03006097 if (cpupri_init(&rd->cpupri, bootmem) != 0)
Rusty Russell68e74562008-11-25 02:35:13 +10306098 goto free_rto_mask;
Rusty Russellc6c49272008-11-25 02:35:05 +10306099 return 0;
6100
Rusty Russell68e74562008-11-25 02:35:13 +10306101free_rto_mask:
6102 free_cpumask_var(rd->rto_mask);
Rusty Russellc6c49272008-11-25 02:35:05 +10306103free_online:
6104 free_cpumask_var(rd->online);
6105free_span:
6106 free_cpumask_var(rd->span);
Li Zefan0c910d22009-01-06 17:39:06 +08006107out:
Rusty Russellc6c49272008-11-25 02:35:05 +10306108 return -ENOMEM;
Gregory Haskins57d885f2008-01-25 21:08:18 +01006109}
6110
6111static void init_defrootdomain(void)
6112{
Rusty Russellc6c49272008-11-25 02:35:05 +10306113 init_rootdomain(&def_root_domain, true);
6114
Gregory Haskins57d885f2008-01-25 21:08:18 +01006115 atomic_set(&def_root_domain.refcount, 1);
6116}
6117
Gregory Haskinsdc938522008-01-25 21:08:26 +01006118static struct root_domain *alloc_rootdomain(void)
Gregory Haskins57d885f2008-01-25 21:08:18 +01006119{
6120 struct root_domain *rd;
6121
6122 rd = kmalloc(sizeof(*rd), GFP_KERNEL);
6123 if (!rd)
6124 return NULL;
6125
Rusty Russellc6c49272008-11-25 02:35:05 +10306126 if (init_rootdomain(rd, false) != 0) {
6127 kfree(rd);
6128 return NULL;
6129 }
Gregory Haskins57d885f2008-01-25 21:08:18 +01006130
6131 return rd;
6132}
6133
Linus Torvalds1da177e2005-04-16 15:20:36 -07006134/*
Ingo Molnar0eab9142008-01-25 21:08:19 +01006135 * Attach the domain 'sd' to 'cpu' as its base domain. Callers must
Linus Torvalds1da177e2005-04-16 15:20:36 -07006136 * hold the hotplug lock.
6137 */
Ingo Molnar0eab9142008-01-25 21:08:19 +01006138static void
6139cpu_attach_domain(struct sched_domain *sd, struct root_domain *rd, int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006140{
Ingo Molnar70b97a72006-07-03 00:25:42 -07006141 struct rq *rq = cpu_rq(cpu);
Suresh Siddha245af2c2005-06-25 14:57:25 -07006142 struct sched_domain *tmp;
6143
Peter Zijlstra669c55e2010-04-16 14:59:29 +02006144 for (tmp = sd; tmp; tmp = tmp->parent)
6145 tmp->span_weight = cpumask_weight(sched_domain_span(tmp));
6146
Suresh Siddha245af2c2005-06-25 14:57:25 -07006147 /* Remove the sched domains which do not contribute to scheduling. */
Li Zefanf29c9b12008-11-06 09:45:16 +08006148 for (tmp = sd; tmp; ) {
Suresh Siddha245af2c2005-06-25 14:57:25 -07006149 struct sched_domain *parent = tmp->parent;
6150 if (!parent)
6151 break;
Li Zefanf29c9b12008-11-06 09:45:16 +08006152
Siddha, Suresh B1a848872006-10-03 01:14:08 -07006153 if (sd_parent_degenerate(tmp, parent)) {
Suresh Siddha245af2c2005-06-25 14:57:25 -07006154 tmp->parent = parent->parent;
Siddha, Suresh B1a848872006-10-03 01:14:08 -07006155 if (parent->parent)
6156 parent->parent->child = tmp;
Li Zefanf29c9b12008-11-06 09:45:16 +08006157 } else
6158 tmp = tmp->parent;
Suresh Siddha245af2c2005-06-25 14:57:25 -07006159 }
6160
Siddha, Suresh B1a848872006-10-03 01:14:08 -07006161 if (sd && sd_degenerate(sd)) {
Suresh Siddha245af2c2005-06-25 14:57:25 -07006162 sd = sd->parent;
Siddha, Suresh B1a848872006-10-03 01:14:08 -07006163 if (sd)
6164 sd->child = NULL;
6165 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006166
6167 sched_domain_debug(sd, cpu);
6168
Gregory Haskins57d885f2008-01-25 21:08:18 +01006169 rq_attach_root(rq, rd);
Nick Piggin674311d2005-06-25 14:57:27 -07006170 rcu_assign_pointer(rq->sd, sd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006171}
6172
6173/* cpus with isolated domains */
Rusty Russelldcc30a32008-11-25 02:35:12 +10306174static cpumask_var_t cpu_isolated_map;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006175
6176/* Setup the mask of cpus configured for isolated domains */
6177static int __init isolated_cpu_setup(char *str)
6178{
Rusty Russellbdddd292009-12-02 14:09:16 +10306179 alloc_bootmem_cpumask_var(&cpu_isolated_map);
Rusty Russell968ea6d2008-12-13 21:55:51 +10306180 cpulist_parse(str, cpu_isolated_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006181 return 1;
6182}
6183
Ingo Molnar8927f492007-10-15 17:00:13 +02006184__setup("isolcpus=", isolated_cpu_setup);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006185
6186/*
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08006187 * init_sched_build_groups takes the cpumask we wish to span, and a pointer
6188 * to a function which identifies what group(along with sched group) a CPU
Rusty Russell96f874e2008-11-25 02:35:14 +10306189 * belongs to. The return value of group_fn must be a >= 0 and < nr_cpu_ids
6190 * (due to the fact that we keep track of groups covered with a struct cpumask).
Linus Torvalds1da177e2005-04-16 15:20:36 -07006191 *
6192 * init_sched_build_groups will build a circular linked list of the groups
6193 * covered by the given span, and will set each group's ->cpumask correctly,
6194 * and ->cpu_power to 0.
6195 */
Siddha, Suresh Ba6160582006-10-03 01:14:06 -07006196static void
Rusty Russell96f874e2008-11-25 02:35:14 +10306197init_sched_build_groups(const struct cpumask *span,
6198 const struct cpumask *cpu_map,
6199 int (*group_fn)(int cpu, const struct cpumask *cpu_map,
Mike Travis7c16ec52008-04-04 18:11:11 -07006200 struct sched_group **sg,
Rusty Russell96f874e2008-11-25 02:35:14 +10306201 struct cpumask *tmpmask),
6202 struct cpumask *covered, struct cpumask *tmpmask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006203{
6204 struct sched_group *first = NULL, *last = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006205 int i;
6206
Rusty Russell96f874e2008-11-25 02:35:14 +10306207 cpumask_clear(covered);
Mike Travis7c16ec52008-04-04 18:11:11 -07006208
Rusty Russellabcd0832008-11-25 02:35:02 +10306209 for_each_cpu(i, span) {
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08006210 struct sched_group *sg;
Mike Travis7c16ec52008-04-04 18:11:11 -07006211 int group = group_fn(i, cpu_map, &sg, tmpmask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006212 int j;
6213
Rusty Russell758b2cd2008-11-25 02:35:04 +10306214 if (cpumask_test_cpu(i, covered))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006215 continue;
6216
Rusty Russell758b2cd2008-11-25 02:35:04 +10306217 cpumask_clear(sched_group_cpus(sg));
Peter Zijlstra18a38852009-09-01 10:34:39 +02006218 sg->cpu_power = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006219
Rusty Russellabcd0832008-11-25 02:35:02 +10306220 for_each_cpu(j, span) {
Mike Travis7c16ec52008-04-04 18:11:11 -07006221 if (group_fn(j, cpu_map, NULL, tmpmask) != group)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006222 continue;
6223
Rusty Russell96f874e2008-11-25 02:35:14 +10306224 cpumask_set_cpu(j, covered);
Rusty Russell758b2cd2008-11-25 02:35:04 +10306225 cpumask_set_cpu(j, sched_group_cpus(sg));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006226 }
6227 if (!first)
6228 first = sg;
6229 if (last)
6230 last->next = sg;
6231 last = sg;
6232 }
6233 last->next = first;
6234}
6235
John Hawkes9c1cfda2005-09-06 15:18:14 -07006236#define SD_NODES_PER_DOMAIN 16
Linus Torvalds1da177e2005-04-16 15:20:36 -07006237
John Hawkes9c1cfda2005-09-06 15:18:14 -07006238#ifdef CONFIG_NUMA
akpm@osdl.org198e2f12006-01-12 01:05:30 -08006239
John Hawkes9c1cfda2005-09-06 15:18:14 -07006240/**
6241 * find_next_best_node - find the next node to include in a sched_domain
6242 * @node: node whose sched_domain we're building
6243 * @used_nodes: nodes already in the sched_domain
6244 *
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01006245 * Find the next node to include in a given scheduling domain. Simply
John Hawkes9c1cfda2005-09-06 15:18:14 -07006246 * finds the closest node not already in the @used_nodes map.
6247 *
6248 * Should use nodemask_t.
6249 */
Mike Travisc5f59f02008-04-04 18:11:10 -07006250static int find_next_best_node(int node, nodemask_t *used_nodes)
John Hawkes9c1cfda2005-09-06 15:18:14 -07006251{
6252 int i, n, val, min_val, best_node = 0;
6253
6254 min_val = INT_MAX;
6255
Mike Travis076ac2a2008-05-12 21:21:12 +02006256 for (i = 0; i < nr_node_ids; i++) {
John Hawkes9c1cfda2005-09-06 15:18:14 -07006257 /* Start at @node */
Mike Travis076ac2a2008-05-12 21:21:12 +02006258 n = (node + i) % nr_node_ids;
John Hawkes9c1cfda2005-09-06 15:18:14 -07006259
6260 if (!nr_cpus_node(n))
6261 continue;
6262
6263 /* Skip already used nodes */
Mike Travisc5f59f02008-04-04 18:11:10 -07006264 if (node_isset(n, *used_nodes))
John Hawkes9c1cfda2005-09-06 15:18:14 -07006265 continue;
6266
6267 /* Simple min distance search */
6268 val = node_distance(node, n);
6269
6270 if (val < min_val) {
6271 min_val = val;
6272 best_node = n;
6273 }
6274 }
6275
Mike Travisc5f59f02008-04-04 18:11:10 -07006276 node_set(best_node, *used_nodes);
John Hawkes9c1cfda2005-09-06 15:18:14 -07006277 return best_node;
6278}
6279
6280/**
6281 * sched_domain_node_span - get a cpumask for a node's sched_domain
6282 * @node: node whose cpumask we're constructing
Randy Dunlap73486722008-04-22 10:07:22 -07006283 * @span: resulting cpumask
John Hawkes9c1cfda2005-09-06 15:18:14 -07006284 *
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01006285 * Given a node, construct a good cpumask for its sched_domain to span. It
John Hawkes9c1cfda2005-09-06 15:18:14 -07006286 * should be one that prevents unnecessary balancing, but also spreads tasks
6287 * out optimally.
6288 */
Rusty Russell96f874e2008-11-25 02:35:14 +10306289static void sched_domain_node_span(int node, struct cpumask *span)
John Hawkes9c1cfda2005-09-06 15:18:14 -07006290{
Mike Travisc5f59f02008-04-04 18:11:10 -07006291 nodemask_t used_nodes;
Ingo Molnar48f24c42006-07-03 00:25:40 -07006292 int i;
John Hawkes9c1cfda2005-09-06 15:18:14 -07006293
Mike Travis6ca09df2008-12-31 18:08:45 -08006294 cpumask_clear(span);
Mike Travisc5f59f02008-04-04 18:11:10 -07006295 nodes_clear(used_nodes);
John Hawkes9c1cfda2005-09-06 15:18:14 -07006296
Mike Travis6ca09df2008-12-31 18:08:45 -08006297 cpumask_or(span, span, cpumask_of_node(node));
Mike Travisc5f59f02008-04-04 18:11:10 -07006298 node_set(node, used_nodes);
John Hawkes9c1cfda2005-09-06 15:18:14 -07006299
6300 for (i = 1; i < SD_NODES_PER_DOMAIN; i++) {
Mike Travisc5f59f02008-04-04 18:11:10 -07006301 int next_node = find_next_best_node(node, &used_nodes);
Ingo Molnar48f24c42006-07-03 00:25:40 -07006302
Mike Travis6ca09df2008-12-31 18:08:45 -08006303 cpumask_or(span, span, cpumask_of_node(next_node));
John Hawkes9c1cfda2005-09-06 15:18:14 -07006304 }
John Hawkes9c1cfda2005-09-06 15:18:14 -07006305}
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +02006306#endif /* CONFIG_NUMA */
John Hawkes9c1cfda2005-09-06 15:18:14 -07006307
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07006308int sched_smt_power_savings = 0, sched_mc_power_savings = 0;
Ingo Molnar48f24c42006-07-03 00:25:40 -07006309
John Hawkes9c1cfda2005-09-06 15:18:14 -07006310/*
Rusty Russell6c99e9a2008-11-25 02:35:04 +10306311 * The cpus mask in sched_group and sched_domain hangs off the end.
Ingo Molnar4200efd2009-05-19 09:22:19 +02006312 *
6313 * ( See the the comments in include/linux/sched.h:struct sched_group
6314 * and struct sched_domain. )
Rusty Russell6c99e9a2008-11-25 02:35:04 +10306315 */
6316struct static_sched_group {
6317 struct sched_group sg;
6318 DECLARE_BITMAP(cpus, CONFIG_NR_CPUS);
6319};
6320
6321struct static_sched_domain {
6322 struct sched_domain sd;
6323 DECLARE_BITMAP(span, CONFIG_NR_CPUS);
6324};
6325
Andreas Herrmann49a02c52009-08-18 12:51:52 +02006326struct s_data {
6327#ifdef CONFIG_NUMA
6328 int sd_allnodes;
6329 cpumask_var_t domainspan;
6330 cpumask_var_t covered;
6331 cpumask_var_t notcovered;
6332#endif
6333 cpumask_var_t nodemask;
6334 cpumask_var_t this_sibling_map;
6335 cpumask_var_t this_core_map;
6336 cpumask_var_t send_covered;
6337 cpumask_var_t tmpmask;
6338 struct sched_group **sched_group_nodes;
6339 struct root_domain *rd;
6340};
6341
Andreas Herrmann2109b992009-08-18 12:53:00 +02006342enum s_alloc {
6343 sa_sched_groups = 0,
6344 sa_rootdomain,
6345 sa_tmpmask,
6346 sa_send_covered,
6347 sa_this_core_map,
6348 sa_this_sibling_map,
6349 sa_nodemask,
6350 sa_sched_group_nodes,
6351#ifdef CONFIG_NUMA
6352 sa_notcovered,
6353 sa_covered,
6354 sa_domainspan,
6355#endif
6356 sa_none,
6357};
6358
Rusty Russell6c99e9a2008-11-25 02:35:04 +10306359/*
Ingo Molnar48f24c42006-07-03 00:25:40 -07006360 * SMT sched-domains:
John Hawkes9c1cfda2005-09-06 15:18:14 -07006361 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006362#ifdef CONFIG_SCHED_SMT
Rusty Russell6c99e9a2008-11-25 02:35:04 +10306363static DEFINE_PER_CPU(struct static_sched_domain, cpu_domains);
Tejun Heo1871e522009-10-29 22:34:13 +09006364static DEFINE_PER_CPU(struct static_sched_group, sched_groups);
Ingo Molnar48f24c42006-07-03 00:25:40 -07006365
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01006366static int
Rusty Russell96f874e2008-11-25 02:35:14 +10306367cpu_to_cpu_group(int cpu, const struct cpumask *cpu_map,
6368 struct sched_group **sg, struct cpumask *unused)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006369{
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08006370 if (sg)
Tejun Heo1871e522009-10-29 22:34:13 +09006371 *sg = &per_cpu(sched_groups, cpu).sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006372 return cpu;
6373}
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +02006374#endif /* CONFIG_SCHED_SMT */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006375
Ingo Molnar48f24c42006-07-03 00:25:40 -07006376/*
6377 * multi-core sched-domains:
6378 */
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08006379#ifdef CONFIG_SCHED_MC
Rusty Russell6c99e9a2008-11-25 02:35:04 +10306380static DEFINE_PER_CPU(struct static_sched_domain, core_domains);
6381static DEFINE_PER_CPU(struct static_sched_group, sched_group_core);
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +02006382#endif /* CONFIG_SCHED_MC */
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08006383
6384#if defined(CONFIG_SCHED_MC) && defined(CONFIG_SCHED_SMT)
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01006385static int
Rusty Russell96f874e2008-11-25 02:35:14 +10306386cpu_to_core_group(int cpu, const struct cpumask *cpu_map,
6387 struct sched_group **sg, struct cpumask *mask)
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08006388{
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08006389 int group;
Mike Travis7c16ec52008-04-04 18:11:11 -07006390
Rusty Russellc69fc562009-03-13 14:49:46 +10306391 cpumask_and(mask, topology_thread_cpumask(cpu), cpu_map);
Rusty Russell96f874e2008-11-25 02:35:14 +10306392 group = cpumask_first(mask);
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08006393 if (sg)
Rusty Russell6c99e9a2008-11-25 02:35:04 +10306394 *sg = &per_cpu(sched_group_core, group).sg;
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08006395 return group;
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08006396}
6397#elif defined(CONFIG_SCHED_MC)
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01006398static int
Rusty Russell96f874e2008-11-25 02:35:14 +10306399cpu_to_core_group(int cpu, const struct cpumask *cpu_map,
6400 struct sched_group **sg, struct cpumask *unused)
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08006401{
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08006402 if (sg)
Rusty Russell6c99e9a2008-11-25 02:35:04 +10306403 *sg = &per_cpu(sched_group_core, cpu).sg;
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08006404 return cpu;
6405}
6406#endif
6407
Rusty Russell6c99e9a2008-11-25 02:35:04 +10306408static DEFINE_PER_CPU(struct static_sched_domain, phys_domains);
6409static DEFINE_PER_CPU(struct static_sched_group, sched_group_phys);
Ingo Molnar48f24c42006-07-03 00:25:40 -07006410
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01006411static int
Rusty Russell96f874e2008-11-25 02:35:14 +10306412cpu_to_phys_group(int cpu, const struct cpumask *cpu_map,
6413 struct sched_group **sg, struct cpumask *mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006414{
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08006415 int group;
Ingo Molnar48f24c42006-07-03 00:25:40 -07006416#ifdef CONFIG_SCHED_MC
Mike Travis6ca09df2008-12-31 18:08:45 -08006417 cpumask_and(mask, cpu_coregroup_mask(cpu), cpu_map);
Rusty Russell96f874e2008-11-25 02:35:14 +10306418 group = cpumask_first(mask);
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08006419#elif defined(CONFIG_SCHED_SMT)
Rusty Russellc69fc562009-03-13 14:49:46 +10306420 cpumask_and(mask, topology_thread_cpumask(cpu), cpu_map);
Rusty Russell96f874e2008-11-25 02:35:14 +10306421 group = cpumask_first(mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006422#else
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08006423 group = cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006424#endif
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08006425 if (sg)
Rusty Russell6c99e9a2008-11-25 02:35:04 +10306426 *sg = &per_cpu(sched_group_phys, group).sg;
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08006427 return group;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006428}
6429
6430#ifdef CONFIG_NUMA
John Hawkes9c1cfda2005-09-06 15:18:14 -07006431/*
6432 * The init_sched_build_groups can't handle what we want to do with node
6433 * groups, so roll our own. Now each node has its own list of groups which
6434 * gets dynamically allocated.
6435 */
Rusty Russell62ea9ce2009-01-11 01:04:16 +01006436static DEFINE_PER_CPU(struct static_sched_domain, node_domains);
Mike Travis434d53b2008-04-04 18:11:04 -07006437static struct sched_group ***sched_group_nodes_bycpu;
John Hawkes9c1cfda2005-09-06 15:18:14 -07006438
Rusty Russell62ea9ce2009-01-11 01:04:16 +01006439static DEFINE_PER_CPU(struct static_sched_domain, allnodes_domains);
Rusty Russell6c99e9a2008-11-25 02:35:04 +10306440static DEFINE_PER_CPU(struct static_sched_group, sched_group_allnodes);
John Hawkes9c1cfda2005-09-06 15:18:14 -07006441
Rusty Russell96f874e2008-11-25 02:35:14 +10306442static int cpu_to_allnodes_group(int cpu, const struct cpumask *cpu_map,
6443 struct sched_group **sg,
6444 struct cpumask *nodemask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006445{
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08006446 int group;
6447
Mike Travis6ca09df2008-12-31 18:08:45 -08006448 cpumask_and(nodemask, cpumask_of_node(cpu_to_node(cpu)), cpu_map);
Rusty Russell96f874e2008-11-25 02:35:14 +10306449 group = cpumask_first(nodemask);
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08006450
6451 if (sg)
Rusty Russell6c99e9a2008-11-25 02:35:04 +10306452 *sg = &per_cpu(sched_group_allnodes, group).sg;
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08006453 return group;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006454}
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08006455
Siddha, Suresh B08069032006-03-27 01:15:23 -08006456static void init_numa_sched_groups_power(struct sched_group *group_head)
6457{
6458 struct sched_group *sg = group_head;
6459 int j;
6460
6461 if (!sg)
6462 return;
Andi Kleen3a5c3592007-10-15 17:00:14 +02006463 do {
Rusty Russell758b2cd2008-11-25 02:35:04 +10306464 for_each_cpu(j, sched_group_cpus(sg)) {
Andi Kleen3a5c3592007-10-15 17:00:14 +02006465 struct sched_domain *sd;
Siddha, Suresh B08069032006-03-27 01:15:23 -08006466
Rusty Russell6c99e9a2008-11-25 02:35:04 +10306467 sd = &per_cpu(phys_domains, j).sd;
Miao Xie13318a72009-04-15 09:59:10 +08006468 if (j != group_first_cpu(sd->groups)) {
Andi Kleen3a5c3592007-10-15 17:00:14 +02006469 /*
6470 * Only add "power" once for each
6471 * physical package.
6472 */
6473 continue;
6474 }
6475
Peter Zijlstra18a38852009-09-01 10:34:39 +02006476 sg->cpu_power += sd->groups->cpu_power;
Siddha, Suresh B08069032006-03-27 01:15:23 -08006477 }
Andi Kleen3a5c3592007-10-15 17:00:14 +02006478 sg = sg->next;
6479 } while (sg != group_head);
Siddha, Suresh B08069032006-03-27 01:15:23 -08006480}
Andreas Herrmann0601a882009-08-18 13:01:11 +02006481
6482static int build_numa_sched_groups(struct s_data *d,
6483 const struct cpumask *cpu_map, int num)
6484{
6485 struct sched_domain *sd;
6486 struct sched_group *sg, *prev;
6487 int n, j;
6488
6489 cpumask_clear(d->covered);
6490 cpumask_and(d->nodemask, cpumask_of_node(num), cpu_map);
6491 if (cpumask_empty(d->nodemask)) {
6492 d->sched_group_nodes[num] = NULL;
6493 goto out;
6494 }
6495
6496 sched_domain_node_span(num, d->domainspan);
6497 cpumask_and(d->domainspan, d->domainspan, cpu_map);
6498
6499 sg = kmalloc_node(sizeof(struct sched_group) + cpumask_size(),
6500 GFP_KERNEL, num);
6501 if (!sg) {
Peter Zijlstra3df0fc52009-12-20 14:23:57 +01006502 printk(KERN_WARNING "Can not alloc domain group for node %d\n",
6503 num);
Andreas Herrmann0601a882009-08-18 13:01:11 +02006504 return -ENOMEM;
6505 }
6506 d->sched_group_nodes[num] = sg;
6507
6508 for_each_cpu(j, d->nodemask) {
6509 sd = &per_cpu(node_domains, j).sd;
6510 sd->groups = sg;
6511 }
6512
Peter Zijlstra18a38852009-09-01 10:34:39 +02006513 sg->cpu_power = 0;
Andreas Herrmann0601a882009-08-18 13:01:11 +02006514 cpumask_copy(sched_group_cpus(sg), d->nodemask);
6515 sg->next = sg;
6516 cpumask_or(d->covered, d->covered, d->nodemask);
6517
6518 prev = sg;
6519 for (j = 0; j < nr_node_ids; j++) {
6520 n = (num + j) % nr_node_ids;
6521 cpumask_complement(d->notcovered, d->covered);
6522 cpumask_and(d->tmpmask, d->notcovered, cpu_map);
6523 cpumask_and(d->tmpmask, d->tmpmask, d->domainspan);
6524 if (cpumask_empty(d->tmpmask))
6525 break;
6526 cpumask_and(d->tmpmask, d->tmpmask, cpumask_of_node(n));
6527 if (cpumask_empty(d->tmpmask))
6528 continue;
6529 sg = kmalloc_node(sizeof(struct sched_group) + cpumask_size(),
6530 GFP_KERNEL, num);
6531 if (!sg) {
Peter Zijlstra3df0fc52009-12-20 14:23:57 +01006532 printk(KERN_WARNING
6533 "Can not alloc domain group for node %d\n", j);
Andreas Herrmann0601a882009-08-18 13:01:11 +02006534 return -ENOMEM;
6535 }
Peter Zijlstra18a38852009-09-01 10:34:39 +02006536 sg->cpu_power = 0;
Andreas Herrmann0601a882009-08-18 13:01:11 +02006537 cpumask_copy(sched_group_cpus(sg), d->tmpmask);
6538 sg->next = prev->next;
6539 cpumask_or(d->covered, d->covered, d->tmpmask);
6540 prev->next = sg;
6541 prev = sg;
6542 }
6543out:
6544 return 0;
6545}
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +02006546#endif /* CONFIG_NUMA */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006547
Siddha, Suresh Ba6160582006-10-03 01:14:06 -07006548#ifdef CONFIG_NUMA
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07006549/* Free memory allocated for various sched_group structures */
Rusty Russell96f874e2008-11-25 02:35:14 +10306550static void free_sched_groups(const struct cpumask *cpu_map,
6551 struct cpumask *nodemask)
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07006552{
Siddha, Suresh Ba6160582006-10-03 01:14:06 -07006553 int cpu, i;
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07006554
Rusty Russellabcd0832008-11-25 02:35:02 +10306555 for_each_cpu(cpu, cpu_map) {
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07006556 struct sched_group **sched_group_nodes
6557 = sched_group_nodes_bycpu[cpu];
6558
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07006559 if (!sched_group_nodes)
6560 continue;
6561
Mike Travis076ac2a2008-05-12 21:21:12 +02006562 for (i = 0; i < nr_node_ids; i++) {
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07006563 struct sched_group *oldsg, *sg = sched_group_nodes[i];
6564
Mike Travis6ca09df2008-12-31 18:08:45 -08006565 cpumask_and(nodemask, cpumask_of_node(i), cpu_map);
Rusty Russell96f874e2008-11-25 02:35:14 +10306566 if (cpumask_empty(nodemask))
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07006567 continue;
6568
6569 if (sg == NULL)
6570 continue;
6571 sg = sg->next;
6572next_sg:
6573 oldsg = sg;
6574 sg = sg->next;
6575 kfree(oldsg);
6576 if (oldsg != sched_group_nodes[i])
6577 goto next_sg;
6578 }
6579 kfree(sched_group_nodes);
6580 sched_group_nodes_bycpu[cpu] = NULL;
6581 }
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07006582}
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +02006583#else /* !CONFIG_NUMA */
Rusty Russell96f874e2008-11-25 02:35:14 +10306584static void free_sched_groups(const struct cpumask *cpu_map,
6585 struct cpumask *nodemask)
Siddha, Suresh Ba6160582006-10-03 01:14:06 -07006586{
6587}
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +02006588#endif /* CONFIG_NUMA */
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07006589
Linus Torvalds1da177e2005-04-16 15:20:36 -07006590/*
Siddha, Suresh B89c47102006-10-03 01:14:09 -07006591 * Initialize sched groups cpu_power.
6592 *
6593 * cpu_power indicates the capacity of sched group, which is used while
6594 * distributing the load between different sched groups in a sched domain.
6595 * Typically cpu_power for all the groups in a sched domain will be same unless
6596 * there are asymmetries in the topology. If there are asymmetries, group
6597 * having more cpu_power will pickup more load compared to the group having
6598 * less cpu_power.
Siddha, Suresh B89c47102006-10-03 01:14:09 -07006599 */
6600static void init_sched_groups_power(int cpu, struct sched_domain *sd)
6601{
6602 struct sched_domain *child;
6603 struct sched_group *group;
Peter Zijlstraf93e65c2009-09-01 10:34:32 +02006604 long power;
6605 int weight;
Siddha, Suresh B89c47102006-10-03 01:14:09 -07006606
6607 WARN_ON(!sd || !sd->groups);
6608
Miao Xie13318a72009-04-15 09:59:10 +08006609 if (cpu != group_first_cpu(sd->groups))
Siddha, Suresh B89c47102006-10-03 01:14:09 -07006610 return;
6611
6612 child = sd->child;
6613
Peter Zijlstra18a38852009-09-01 10:34:39 +02006614 sd->groups->cpu_power = 0;
Eric Dumazet5517d862007-05-08 00:32:57 -07006615
Peter Zijlstraf93e65c2009-09-01 10:34:32 +02006616 if (!child) {
6617 power = SCHED_LOAD_SCALE;
6618 weight = cpumask_weight(sched_domain_span(sd));
6619 /*
6620 * SMT siblings share the power of a single core.
Peter Zijlstraa52bfd72009-09-01 10:34:35 +02006621 * Usually multiple threads get a better yield out of
6622 * that one core than a single thread would have,
6623 * reflect that in sd->smt_gain.
Peter Zijlstraf93e65c2009-09-01 10:34:32 +02006624 */
Peter Zijlstraa52bfd72009-09-01 10:34:35 +02006625 if ((sd->flags & SD_SHARE_CPUPOWER) && weight > 1) {
6626 power *= sd->smt_gain;
Peter Zijlstraf93e65c2009-09-01 10:34:32 +02006627 power /= weight;
Peter Zijlstraa52bfd72009-09-01 10:34:35 +02006628 power >>= SCHED_LOAD_SHIFT;
6629 }
Peter Zijlstra18a38852009-09-01 10:34:39 +02006630 sd->groups->cpu_power += power;
Siddha, Suresh B89c47102006-10-03 01:14:09 -07006631 return;
6632 }
6633
Siddha, Suresh B89c47102006-10-03 01:14:09 -07006634 /*
Peter Zijlstraf93e65c2009-09-01 10:34:32 +02006635 * Add cpu_power of each child group to this groups cpu_power.
Siddha, Suresh B89c47102006-10-03 01:14:09 -07006636 */
6637 group = child->groups;
6638 do {
Peter Zijlstra18a38852009-09-01 10:34:39 +02006639 sd->groups->cpu_power += group->cpu_power;
Siddha, Suresh B89c47102006-10-03 01:14:09 -07006640 group = group->next;
6641 } while (group != child->groups);
6642}
6643
6644/*
Mike Travis7c16ec52008-04-04 18:11:11 -07006645 * Initializers for schedule domains
6646 * Non-inlined to reduce accumulated stack pressure in build_sched_domains()
6647 */
6648
Ingo Molnara5d8c342008-10-09 11:35:51 +02006649#ifdef CONFIG_SCHED_DEBUG
6650# define SD_INIT_NAME(sd, type) sd->name = #type
6651#else
6652# define SD_INIT_NAME(sd, type) do { } while (0)
6653#endif
6654
Mike Travis7c16ec52008-04-04 18:11:11 -07006655#define SD_INIT(sd, type) sd_init_##type(sd)
Ingo Molnara5d8c342008-10-09 11:35:51 +02006656
Mike Travis7c16ec52008-04-04 18:11:11 -07006657#define SD_INIT_FUNC(type) \
6658static noinline void sd_init_##type(struct sched_domain *sd) \
6659{ \
6660 memset(sd, 0, sizeof(*sd)); \
6661 *sd = SD_##type##_INIT; \
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09006662 sd->level = SD_LV_##type; \
Ingo Molnara5d8c342008-10-09 11:35:51 +02006663 SD_INIT_NAME(sd, type); \
Mike Travis7c16ec52008-04-04 18:11:11 -07006664}
6665
6666SD_INIT_FUNC(CPU)
6667#ifdef CONFIG_NUMA
6668 SD_INIT_FUNC(ALLNODES)
6669 SD_INIT_FUNC(NODE)
6670#endif
6671#ifdef CONFIG_SCHED_SMT
6672 SD_INIT_FUNC(SIBLING)
6673#endif
6674#ifdef CONFIG_SCHED_MC
6675 SD_INIT_FUNC(MC)
6676#endif
6677
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09006678static int default_relax_domain_level = -1;
6679
6680static int __init setup_relax_domain_level(char *str)
6681{
Li Zefan30e0e172008-05-13 10:27:17 +08006682 unsigned long val;
6683
6684 val = simple_strtoul(str, NULL, 0);
6685 if (val < SD_LV_MAX)
6686 default_relax_domain_level = val;
6687
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09006688 return 1;
6689}
6690__setup("relax_domain_level=", setup_relax_domain_level);
6691
6692static void set_domain_attribute(struct sched_domain *sd,
6693 struct sched_domain_attr *attr)
6694{
6695 int request;
6696
6697 if (!attr || attr->relax_domain_level < 0) {
6698 if (default_relax_domain_level < 0)
6699 return;
6700 else
6701 request = default_relax_domain_level;
6702 } else
6703 request = attr->relax_domain_level;
6704 if (request < sd->level) {
6705 /* turn off idle balance on this domain */
Peter Zijlstrac88d5912009-09-10 13:50:02 +02006706 sd->flags &= ~(SD_BALANCE_WAKE|SD_BALANCE_NEWIDLE);
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09006707 } else {
6708 /* turn on idle balance on this domain */
Peter Zijlstrac88d5912009-09-10 13:50:02 +02006709 sd->flags |= (SD_BALANCE_WAKE|SD_BALANCE_NEWIDLE);
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09006710 }
6711}
6712
Andreas Herrmann2109b992009-08-18 12:53:00 +02006713static void __free_domain_allocs(struct s_data *d, enum s_alloc what,
6714 const struct cpumask *cpu_map)
6715{
6716 switch (what) {
6717 case sa_sched_groups:
6718 free_sched_groups(cpu_map, d->tmpmask); /* fall through */
6719 d->sched_group_nodes = NULL;
6720 case sa_rootdomain:
6721 free_rootdomain(d->rd); /* fall through */
6722 case sa_tmpmask:
6723 free_cpumask_var(d->tmpmask); /* fall through */
6724 case sa_send_covered:
6725 free_cpumask_var(d->send_covered); /* fall through */
6726 case sa_this_core_map:
6727 free_cpumask_var(d->this_core_map); /* fall through */
6728 case sa_this_sibling_map:
6729 free_cpumask_var(d->this_sibling_map); /* fall through */
6730 case sa_nodemask:
6731 free_cpumask_var(d->nodemask); /* fall through */
6732 case sa_sched_group_nodes:
6733#ifdef CONFIG_NUMA
6734 kfree(d->sched_group_nodes); /* fall through */
6735 case sa_notcovered:
6736 free_cpumask_var(d->notcovered); /* fall through */
6737 case sa_covered:
6738 free_cpumask_var(d->covered); /* fall through */
6739 case sa_domainspan:
6740 free_cpumask_var(d->domainspan); /* fall through */
6741#endif
6742 case sa_none:
6743 break;
6744 }
6745}
6746
6747static enum s_alloc __visit_domain_allocation_hell(struct s_data *d,
6748 const struct cpumask *cpu_map)
6749{
6750#ifdef CONFIG_NUMA
6751 if (!alloc_cpumask_var(&d->domainspan, GFP_KERNEL))
6752 return sa_none;
6753 if (!alloc_cpumask_var(&d->covered, GFP_KERNEL))
6754 return sa_domainspan;
6755 if (!alloc_cpumask_var(&d->notcovered, GFP_KERNEL))
6756 return sa_covered;
6757 /* Allocate the per-node list of sched groups */
6758 d->sched_group_nodes = kcalloc(nr_node_ids,
6759 sizeof(struct sched_group *), GFP_KERNEL);
6760 if (!d->sched_group_nodes) {
Peter Zijlstra3df0fc52009-12-20 14:23:57 +01006761 printk(KERN_WARNING "Can not alloc sched group node list\n");
Andreas Herrmann2109b992009-08-18 12:53:00 +02006762 return sa_notcovered;
6763 }
6764 sched_group_nodes_bycpu[cpumask_first(cpu_map)] = d->sched_group_nodes;
6765#endif
6766 if (!alloc_cpumask_var(&d->nodemask, GFP_KERNEL))
6767 return sa_sched_group_nodes;
6768 if (!alloc_cpumask_var(&d->this_sibling_map, GFP_KERNEL))
6769 return sa_nodemask;
6770 if (!alloc_cpumask_var(&d->this_core_map, GFP_KERNEL))
6771 return sa_this_sibling_map;
6772 if (!alloc_cpumask_var(&d->send_covered, GFP_KERNEL))
6773 return sa_this_core_map;
6774 if (!alloc_cpumask_var(&d->tmpmask, GFP_KERNEL))
6775 return sa_send_covered;
6776 d->rd = alloc_rootdomain();
6777 if (!d->rd) {
Peter Zijlstra3df0fc52009-12-20 14:23:57 +01006778 printk(KERN_WARNING "Cannot alloc root domain\n");
Andreas Herrmann2109b992009-08-18 12:53:00 +02006779 return sa_tmpmask;
6780 }
6781 return sa_rootdomain;
6782}
6783
Andreas Herrmann7f4588f2009-08-18 12:54:06 +02006784static struct sched_domain *__build_numa_sched_domains(struct s_data *d,
6785 const struct cpumask *cpu_map, struct sched_domain_attr *attr, int i)
6786{
6787 struct sched_domain *sd = NULL;
6788#ifdef CONFIG_NUMA
6789 struct sched_domain *parent;
6790
6791 d->sd_allnodes = 0;
6792 if (cpumask_weight(cpu_map) >
6793 SD_NODES_PER_DOMAIN * cpumask_weight(d->nodemask)) {
6794 sd = &per_cpu(allnodes_domains, i).sd;
6795 SD_INIT(sd, ALLNODES);
6796 set_domain_attribute(sd, attr);
6797 cpumask_copy(sched_domain_span(sd), cpu_map);
6798 cpu_to_allnodes_group(i, cpu_map, &sd->groups, d->tmpmask);
6799 d->sd_allnodes = 1;
6800 }
6801 parent = sd;
6802
6803 sd = &per_cpu(node_domains, i).sd;
6804 SD_INIT(sd, NODE);
6805 set_domain_attribute(sd, attr);
6806 sched_domain_node_span(cpu_to_node(i), sched_domain_span(sd));
6807 sd->parent = parent;
6808 if (parent)
6809 parent->child = sd;
6810 cpumask_and(sched_domain_span(sd), sched_domain_span(sd), cpu_map);
6811#endif
6812 return sd;
6813}
6814
Andreas Herrmann87cce662009-08-18 12:54:55 +02006815static struct sched_domain *__build_cpu_sched_domain(struct s_data *d,
6816 const struct cpumask *cpu_map, struct sched_domain_attr *attr,
6817 struct sched_domain *parent, int i)
6818{
6819 struct sched_domain *sd;
6820 sd = &per_cpu(phys_domains, i).sd;
6821 SD_INIT(sd, CPU);
6822 set_domain_attribute(sd, attr);
6823 cpumask_copy(sched_domain_span(sd), d->nodemask);
6824 sd->parent = parent;
6825 if (parent)
6826 parent->child = sd;
6827 cpu_to_phys_group(i, cpu_map, &sd->groups, d->tmpmask);
6828 return sd;
6829}
6830
Andreas Herrmann410c4082009-08-18 12:56:14 +02006831static struct sched_domain *__build_mc_sched_domain(struct s_data *d,
6832 const struct cpumask *cpu_map, struct sched_domain_attr *attr,
6833 struct sched_domain *parent, int i)
6834{
6835 struct sched_domain *sd = parent;
6836#ifdef CONFIG_SCHED_MC
6837 sd = &per_cpu(core_domains, i).sd;
6838 SD_INIT(sd, MC);
6839 set_domain_attribute(sd, attr);
6840 cpumask_and(sched_domain_span(sd), cpu_map, cpu_coregroup_mask(i));
6841 sd->parent = parent;
6842 parent->child = sd;
6843 cpu_to_core_group(i, cpu_map, &sd->groups, d->tmpmask);
6844#endif
6845 return sd;
6846}
6847
Andreas Herrmannd8173532009-08-18 12:57:03 +02006848static struct sched_domain *__build_smt_sched_domain(struct s_data *d,
6849 const struct cpumask *cpu_map, struct sched_domain_attr *attr,
6850 struct sched_domain *parent, int i)
6851{
6852 struct sched_domain *sd = parent;
6853#ifdef CONFIG_SCHED_SMT
6854 sd = &per_cpu(cpu_domains, i).sd;
6855 SD_INIT(sd, SIBLING);
6856 set_domain_attribute(sd, attr);
6857 cpumask_and(sched_domain_span(sd), cpu_map, topology_thread_cpumask(i));
6858 sd->parent = parent;
6859 parent->child = sd;
6860 cpu_to_cpu_group(i, cpu_map, &sd->groups, d->tmpmask);
6861#endif
6862 return sd;
6863}
6864
Andreas Herrmann0e8e85c2009-08-18 12:57:51 +02006865static void build_sched_groups(struct s_data *d, enum sched_domain_level l,
6866 const struct cpumask *cpu_map, int cpu)
6867{
6868 switch (l) {
6869#ifdef CONFIG_SCHED_SMT
6870 case SD_LV_SIBLING: /* set up CPU (sibling) groups */
6871 cpumask_and(d->this_sibling_map, cpu_map,
6872 topology_thread_cpumask(cpu));
6873 if (cpu == cpumask_first(d->this_sibling_map))
6874 init_sched_build_groups(d->this_sibling_map, cpu_map,
6875 &cpu_to_cpu_group,
6876 d->send_covered, d->tmpmask);
6877 break;
6878#endif
Andreas Herrmanna2af04c2009-08-18 12:58:38 +02006879#ifdef CONFIG_SCHED_MC
6880 case SD_LV_MC: /* set up multi-core groups */
6881 cpumask_and(d->this_core_map, cpu_map, cpu_coregroup_mask(cpu));
6882 if (cpu == cpumask_first(d->this_core_map))
6883 init_sched_build_groups(d->this_core_map, cpu_map,
6884 &cpu_to_core_group,
6885 d->send_covered, d->tmpmask);
6886 break;
6887#endif
Andreas Herrmann86548092009-08-18 12:59:28 +02006888 case SD_LV_CPU: /* set up physical groups */
6889 cpumask_and(d->nodemask, cpumask_of_node(cpu), cpu_map);
6890 if (!cpumask_empty(d->nodemask))
6891 init_sched_build_groups(d->nodemask, cpu_map,
6892 &cpu_to_phys_group,
6893 d->send_covered, d->tmpmask);
6894 break;
Andreas Herrmannde616e32009-08-18 13:00:13 +02006895#ifdef CONFIG_NUMA
6896 case SD_LV_ALLNODES:
6897 init_sched_build_groups(cpu_map, cpu_map, &cpu_to_allnodes_group,
6898 d->send_covered, d->tmpmask);
6899 break;
6900#endif
Andreas Herrmann0e8e85c2009-08-18 12:57:51 +02006901 default:
6902 break;
6903 }
6904}
6905
Mike Travis7c16ec52008-04-04 18:11:11 -07006906/*
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07006907 * Build sched domains for a given set of cpus and attach the sched domains
6908 * to the individual cpus
Linus Torvalds1da177e2005-04-16 15:20:36 -07006909 */
Rusty Russell96f874e2008-11-25 02:35:14 +10306910static int __build_sched_domains(const struct cpumask *cpu_map,
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09006911 struct sched_domain_attr *attr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006912{
Andreas Herrmann2109b992009-08-18 12:53:00 +02006913 enum s_alloc alloc_state = sa_none;
Andreas Herrmann49a02c52009-08-18 12:51:52 +02006914 struct s_data d;
Andreas Herrmann294b0c92009-08-18 13:02:29 +02006915 struct sched_domain *sd;
Andreas Herrmann2109b992009-08-18 12:53:00 +02006916 int i;
John Hawkesd1b55132005-09-06 15:18:14 -07006917#ifdef CONFIG_NUMA
Andreas Herrmann49a02c52009-08-18 12:51:52 +02006918 d.sd_allnodes = 0;
Rusty Russell3404c8d2008-11-25 02:35:03 +10306919#endif
6920
Andreas Herrmann2109b992009-08-18 12:53:00 +02006921 alloc_state = __visit_domain_allocation_hell(&d, cpu_map);
6922 if (alloc_state != sa_rootdomain)
6923 goto error;
6924 alloc_state = sa_sched_groups;
Mike Travis7c16ec52008-04-04 18:11:11 -07006925
Linus Torvalds1da177e2005-04-16 15:20:36 -07006926 /*
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07006927 * Set up domains for cpus specified by the cpu_map.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006928 */
Rusty Russellabcd0832008-11-25 02:35:02 +10306929 for_each_cpu(i, cpu_map) {
Andreas Herrmann49a02c52009-08-18 12:51:52 +02006930 cpumask_and(d.nodemask, cpumask_of_node(cpu_to_node(i)),
6931 cpu_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006932
Andreas Herrmann7f4588f2009-08-18 12:54:06 +02006933 sd = __build_numa_sched_domains(&d, cpu_map, attr, i);
Andreas Herrmann87cce662009-08-18 12:54:55 +02006934 sd = __build_cpu_sched_domain(&d, cpu_map, attr, sd, i);
Andreas Herrmann410c4082009-08-18 12:56:14 +02006935 sd = __build_mc_sched_domain(&d, cpu_map, attr, sd, i);
Andreas Herrmannd8173532009-08-18 12:57:03 +02006936 sd = __build_smt_sched_domain(&d, cpu_map, attr, sd, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006937 }
6938
Rusty Russellabcd0832008-11-25 02:35:02 +10306939 for_each_cpu(i, cpu_map) {
Andreas Herrmann0e8e85c2009-08-18 12:57:51 +02006940 build_sched_groups(&d, SD_LV_SIBLING, cpu_map, i);
Andreas Herrmanna2af04c2009-08-18 12:58:38 +02006941 build_sched_groups(&d, SD_LV_MC, cpu_map, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006942 }
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08006943
Linus Torvalds1da177e2005-04-16 15:20:36 -07006944 /* Set up physical groups */
Andreas Herrmann86548092009-08-18 12:59:28 +02006945 for (i = 0; i < nr_node_ids; i++)
6946 build_sched_groups(&d, SD_LV_CPU, cpu_map, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006947
6948#ifdef CONFIG_NUMA
6949 /* Set up node groups */
Andreas Herrmannde616e32009-08-18 13:00:13 +02006950 if (d.sd_allnodes)
6951 build_sched_groups(&d, SD_LV_ALLNODES, cpu_map, 0);
John Hawkes9c1cfda2005-09-06 15:18:14 -07006952
Andreas Herrmann0601a882009-08-18 13:01:11 +02006953 for (i = 0; i < nr_node_ids; i++)
6954 if (build_numa_sched_groups(&d, cpu_map, i))
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07006955 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006956#endif
6957
6958 /* Calculate CPU power for physical packages and nodes */
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07006959#ifdef CONFIG_SCHED_SMT
Rusty Russellabcd0832008-11-25 02:35:02 +10306960 for_each_cpu(i, cpu_map) {
Andreas Herrmann294b0c92009-08-18 13:02:29 +02006961 sd = &per_cpu(cpu_domains, i).sd;
Siddha, Suresh B89c47102006-10-03 01:14:09 -07006962 init_sched_groups_power(i, sd);
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07006963 }
6964#endif
6965#ifdef CONFIG_SCHED_MC
Rusty Russellabcd0832008-11-25 02:35:02 +10306966 for_each_cpu(i, cpu_map) {
Andreas Herrmann294b0c92009-08-18 13:02:29 +02006967 sd = &per_cpu(core_domains, i).sd;
Siddha, Suresh B89c47102006-10-03 01:14:09 -07006968 init_sched_groups_power(i, sd);
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07006969 }
6970#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07006971
Rusty Russellabcd0832008-11-25 02:35:02 +10306972 for_each_cpu(i, cpu_map) {
Andreas Herrmann294b0c92009-08-18 13:02:29 +02006973 sd = &per_cpu(phys_domains, i).sd;
Siddha, Suresh B89c47102006-10-03 01:14:09 -07006974 init_sched_groups_power(i, sd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006975 }
6976
John Hawkes9c1cfda2005-09-06 15:18:14 -07006977#ifdef CONFIG_NUMA
Mike Travis076ac2a2008-05-12 21:21:12 +02006978 for (i = 0; i < nr_node_ids; i++)
Andreas Herrmann49a02c52009-08-18 12:51:52 +02006979 init_numa_sched_groups_power(d.sched_group_nodes[i]);
John Hawkes9c1cfda2005-09-06 15:18:14 -07006980
Andreas Herrmann49a02c52009-08-18 12:51:52 +02006981 if (d.sd_allnodes) {
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08006982 struct sched_group *sg;
Siddha, Suresh Bf712c0c2006-07-30 03:02:59 -07006983
Rusty Russell96f874e2008-11-25 02:35:14 +10306984 cpu_to_allnodes_group(cpumask_first(cpu_map), cpu_map, &sg,
Andreas Herrmann49a02c52009-08-18 12:51:52 +02006985 d.tmpmask);
Siddha, Suresh Bf712c0c2006-07-30 03:02:59 -07006986 init_numa_sched_groups_power(sg);
6987 }
John Hawkes9c1cfda2005-09-06 15:18:14 -07006988#endif
6989
Linus Torvalds1da177e2005-04-16 15:20:36 -07006990 /* Attach the domains */
Rusty Russellabcd0832008-11-25 02:35:02 +10306991 for_each_cpu(i, cpu_map) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006992#ifdef CONFIG_SCHED_SMT
Rusty Russell6c99e9a2008-11-25 02:35:04 +10306993 sd = &per_cpu(cpu_domains, i).sd;
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08006994#elif defined(CONFIG_SCHED_MC)
Rusty Russell6c99e9a2008-11-25 02:35:04 +10306995 sd = &per_cpu(core_domains, i).sd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006996#else
Rusty Russell6c99e9a2008-11-25 02:35:04 +10306997 sd = &per_cpu(phys_domains, i).sd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006998#endif
Andreas Herrmann49a02c52009-08-18 12:51:52 +02006999 cpu_attach_domain(sd, d.rd, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007000 }
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07007001
Andreas Herrmann2109b992009-08-18 12:53:00 +02007002 d.sched_group_nodes = NULL; /* don't free this we still need it */
7003 __free_domain_allocs(&d, sa_tmpmask, cpu_map);
7004 return 0;
Rusty Russell3404c8d2008-11-25 02:35:03 +10307005
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07007006error:
Andreas Herrmann2109b992009-08-18 12:53:00 +02007007 __free_domain_allocs(&d, alloc_state, cpu_map);
7008 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007009}
Paul Jackson029190c2007-10-18 23:40:20 -07007010
Rusty Russell96f874e2008-11-25 02:35:14 +10307011static int build_sched_domains(const struct cpumask *cpu_map)
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09007012{
7013 return __build_sched_domains(cpu_map, NULL);
7014}
7015
Rusty Russellacc3f5d2009-11-03 14:53:40 +10307016static cpumask_var_t *doms_cur; /* current sched domains */
Paul Jackson029190c2007-10-18 23:40:20 -07007017static int ndoms_cur; /* number of sched domains in 'doms_cur' */
Ingo Molnar4285f5942008-05-16 17:47:14 +02007018static struct sched_domain_attr *dattr_cur;
7019 /* attribues of custom domains in 'doms_cur' */
Paul Jackson029190c2007-10-18 23:40:20 -07007020
7021/*
7022 * Special case: If a kmalloc of a doms_cur partition (array of
Rusty Russell42128232008-11-25 02:35:12 +10307023 * cpumask) fails, then fallback to a single sched domain,
7024 * as determined by the single cpumask fallback_doms.
Paul Jackson029190c2007-10-18 23:40:20 -07007025 */
Rusty Russell42128232008-11-25 02:35:12 +10307026static cpumask_var_t fallback_doms;
Paul Jackson029190c2007-10-18 23:40:20 -07007027
Heiko Carstensee79d1b2008-12-09 18:49:50 +01007028/*
7029 * arch_update_cpu_topology lets virtualized architectures update the
7030 * cpu core maps. It is supposed to return 1 if the topology changed
7031 * or 0 if it stayed the same.
7032 */
7033int __attribute__((weak)) arch_update_cpu_topology(void)
Heiko Carstens22e52b02008-03-12 18:31:59 +01007034{
Heiko Carstensee79d1b2008-12-09 18:49:50 +01007035 return 0;
Heiko Carstens22e52b02008-03-12 18:31:59 +01007036}
7037
Rusty Russellacc3f5d2009-11-03 14:53:40 +10307038cpumask_var_t *alloc_sched_domains(unsigned int ndoms)
7039{
7040 int i;
7041 cpumask_var_t *doms;
7042
7043 doms = kmalloc(sizeof(*doms) * ndoms, GFP_KERNEL);
7044 if (!doms)
7045 return NULL;
7046 for (i = 0; i < ndoms; i++) {
7047 if (!alloc_cpumask_var(&doms[i], GFP_KERNEL)) {
7048 free_sched_domains(doms, i);
7049 return NULL;
7050 }
7051 }
7052 return doms;
7053}
7054
7055void free_sched_domains(cpumask_var_t doms[], unsigned int ndoms)
7056{
7057 unsigned int i;
7058 for (i = 0; i < ndoms; i++)
7059 free_cpumask_var(doms[i]);
7060 kfree(doms);
7061}
7062
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07007063/*
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01007064 * Set up scheduler domains and groups. Callers must hold the hotplug lock.
Paul Jackson029190c2007-10-18 23:40:20 -07007065 * For now this just excludes isolated cpus, but could be used to
7066 * exclude other special cases in the future.
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07007067 */
Rusty Russell96f874e2008-11-25 02:35:14 +10307068static int arch_init_sched_domains(const struct cpumask *cpu_map)
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07007069{
Milton Miller73785472007-10-24 18:23:48 +02007070 int err;
7071
Heiko Carstens22e52b02008-03-12 18:31:59 +01007072 arch_update_cpu_topology();
Paul Jackson029190c2007-10-18 23:40:20 -07007073 ndoms_cur = 1;
Rusty Russellacc3f5d2009-11-03 14:53:40 +10307074 doms_cur = alloc_sched_domains(ndoms_cur);
Paul Jackson029190c2007-10-18 23:40:20 -07007075 if (!doms_cur)
Rusty Russellacc3f5d2009-11-03 14:53:40 +10307076 doms_cur = &fallback_doms;
7077 cpumask_andnot(doms_cur[0], cpu_map, cpu_isolated_map);
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09007078 dattr_cur = NULL;
Rusty Russellacc3f5d2009-11-03 14:53:40 +10307079 err = build_sched_domains(doms_cur[0]);
Milton Miller6382bc92007-10-15 17:00:19 +02007080 register_sched_domain_sysctl();
Milton Miller73785472007-10-24 18:23:48 +02007081
7082 return err;
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07007083}
7084
Rusty Russell96f874e2008-11-25 02:35:14 +10307085static void arch_destroy_sched_domains(const struct cpumask *cpu_map,
7086 struct cpumask *tmpmask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007087{
Mike Travis7c16ec52008-04-04 18:11:11 -07007088 free_sched_groups(cpu_map, tmpmask);
John Hawkes9c1cfda2005-09-06 15:18:14 -07007089}
Linus Torvalds1da177e2005-04-16 15:20:36 -07007090
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07007091/*
7092 * Detach sched domains from a group of cpus specified in cpu_map
7093 * These cpus will now be attached to the NULL domain
7094 */
Rusty Russell96f874e2008-11-25 02:35:14 +10307095static void detach_destroy_domains(const struct cpumask *cpu_map)
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07007096{
Rusty Russell96f874e2008-11-25 02:35:14 +10307097 /* Save because hotplug lock held. */
7098 static DECLARE_BITMAP(tmpmask, CONFIG_NR_CPUS);
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07007099 int i;
7100
Rusty Russellabcd0832008-11-25 02:35:02 +10307101 for_each_cpu(i, cpu_map)
Gregory Haskins57d885f2008-01-25 21:08:18 +01007102 cpu_attach_domain(NULL, &def_root_domain, i);
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07007103 synchronize_sched();
Rusty Russell96f874e2008-11-25 02:35:14 +10307104 arch_destroy_sched_domains(cpu_map, to_cpumask(tmpmask));
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07007105}
7106
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09007107/* handle null as "default" */
7108static int dattrs_equal(struct sched_domain_attr *cur, int idx_cur,
7109 struct sched_domain_attr *new, int idx_new)
7110{
7111 struct sched_domain_attr tmp;
7112
7113 /* fast path */
7114 if (!new && !cur)
7115 return 1;
7116
7117 tmp = SD_ATTR_INIT;
7118 return !memcmp(cur ? (cur + idx_cur) : &tmp,
7119 new ? (new + idx_new) : &tmp,
7120 sizeof(struct sched_domain_attr));
7121}
7122
Paul Jackson029190c2007-10-18 23:40:20 -07007123/*
7124 * Partition sched domains as specified by the 'ndoms_new'
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01007125 * cpumasks in the array doms_new[] of cpumasks. This compares
Paul Jackson029190c2007-10-18 23:40:20 -07007126 * doms_new[] to the current sched domain partitioning, doms_cur[].
7127 * It destroys each deleted domain and builds each new domain.
7128 *
Rusty Russellacc3f5d2009-11-03 14:53:40 +10307129 * 'doms_new' is an array of cpumask_var_t's of length 'ndoms_new'.
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01007130 * The masks don't intersect (don't overlap.) We should setup one
7131 * sched domain for each mask. CPUs not in any of the cpumasks will
7132 * not be load balanced. If the same cpumask appears both in the
Paul Jackson029190c2007-10-18 23:40:20 -07007133 * current 'doms_cur' domains and in the new 'doms_new', we can leave
7134 * it as it is.
7135 *
Rusty Russellacc3f5d2009-11-03 14:53:40 +10307136 * The passed in 'doms_new' should be allocated using
7137 * alloc_sched_domains. This routine takes ownership of it and will
7138 * free_sched_domains it when done with it. If the caller failed the
7139 * alloc call, then it can pass in doms_new == NULL && ndoms_new == 1,
7140 * and partition_sched_domains() will fallback to the single partition
7141 * 'fallback_doms', it also forces the domains to be rebuilt.
Paul Jackson029190c2007-10-18 23:40:20 -07007142 *
Rusty Russell96f874e2008-11-25 02:35:14 +10307143 * If doms_new == NULL it will be replaced with cpu_online_mask.
Li Zefan700018e2008-11-18 14:02:03 +08007144 * ndoms_new == 0 is a special case for destroying existing domains,
7145 * and it will not create the default domain.
Max Krasnyanskydfb512e2008-08-29 13:11:41 -07007146 *
Paul Jackson029190c2007-10-18 23:40:20 -07007147 * Call with hotplug lock held
7148 */
Rusty Russellacc3f5d2009-11-03 14:53:40 +10307149void partition_sched_domains(int ndoms_new, cpumask_var_t doms_new[],
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09007150 struct sched_domain_attr *dattr_new)
Paul Jackson029190c2007-10-18 23:40:20 -07007151{
Max Krasnyanskydfb512e2008-08-29 13:11:41 -07007152 int i, j, n;
Heiko Carstensd65bd5e2008-12-09 18:49:51 +01007153 int new_topology;
Paul Jackson029190c2007-10-18 23:40:20 -07007154
Heiko Carstens712555e2008-04-28 11:33:07 +02007155 mutex_lock(&sched_domains_mutex);
Srivatsa Vaddagiria1835612008-01-25 21:08:00 +01007156
Milton Miller73785472007-10-24 18:23:48 +02007157 /* always unregister in case we don't destroy any domains */
7158 unregister_sched_domain_sysctl();
7159
Heiko Carstensd65bd5e2008-12-09 18:49:51 +01007160 /* Let architecture update cpu core mappings. */
7161 new_topology = arch_update_cpu_topology();
7162
Max Krasnyanskydfb512e2008-08-29 13:11:41 -07007163 n = doms_new ? ndoms_new : 0;
Paul Jackson029190c2007-10-18 23:40:20 -07007164
7165 /* Destroy deleted domains */
7166 for (i = 0; i < ndoms_cur; i++) {
Heiko Carstensd65bd5e2008-12-09 18:49:51 +01007167 for (j = 0; j < n && !new_topology; j++) {
Rusty Russellacc3f5d2009-11-03 14:53:40 +10307168 if (cpumask_equal(doms_cur[i], doms_new[j])
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09007169 && dattrs_equal(dattr_cur, i, dattr_new, j))
Paul Jackson029190c2007-10-18 23:40:20 -07007170 goto match1;
7171 }
7172 /* no match - a current sched domain not in new doms_new[] */
Rusty Russellacc3f5d2009-11-03 14:53:40 +10307173 detach_destroy_domains(doms_cur[i]);
Paul Jackson029190c2007-10-18 23:40:20 -07007174match1:
7175 ;
7176 }
7177
Max Krasnyanskye761b772008-07-15 04:43:49 -07007178 if (doms_new == NULL) {
7179 ndoms_cur = 0;
Rusty Russellacc3f5d2009-11-03 14:53:40 +10307180 doms_new = &fallback_doms;
Peter Zijlstra6ad4c182009-11-25 13:31:39 +01007181 cpumask_andnot(doms_new[0], cpu_active_mask, cpu_isolated_map);
Li Zefanfaa2f982008-11-04 16:20:23 +08007182 WARN_ON_ONCE(dattr_new);
Max Krasnyanskye761b772008-07-15 04:43:49 -07007183 }
7184
Paul Jackson029190c2007-10-18 23:40:20 -07007185 /* Build new domains */
7186 for (i = 0; i < ndoms_new; i++) {
Heiko Carstensd65bd5e2008-12-09 18:49:51 +01007187 for (j = 0; j < ndoms_cur && !new_topology; j++) {
Rusty Russellacc3f5d2009-11-03 14:53:40 +10307188 if (cpumask_equal(doms_new[i], doms_cur[j])
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09007189 && dattrs_equal(dattr_new, i, dattr_cur, j))
Paul Jackson029190c2007-10-18 23:40:20 -07007190 goto match2;
7191 }
7192 /* no match - add a new doms_new */
Rusty Russellacc3f5d2009-11-03 14:53:40 +10307193 __build_sched_domains(doms_new[i],
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09007194 dattr_new ? dattr_new + i : NULL);
Paul Jackson029190c2007-10-18 23:40:20 -07007195match2:
7196 ;
7197 }
7198
7199 /* Remember the new sched domains */
Rusty Russellacc3f5d2009-11-03 14:53:40 +10307200 if (doms_cur != &fallback_doms)
7201 free_sched_domains(doms_cur, ndoms_cur);
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09007202 kfree(dattr_cur); /* kfree(NULL) is safe */
Paul Jackson029190c2007-10-18 23:40:20 -07007203 doms_cur = doms_new;
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09007204 dattr_cur = dattr_new;
Paul Jackson029190c2007-10-18 23:40:20 -07007205 ndoms_cur = ndoms_new;
Milton Miller73785472007-10-24 18:23:48 +02007206
7207 register_sched_domain_sysctl();
Srivatsa Vaddagiria1835612008-01-25 21:08:00 +01007208
Heiko Carstens712555e2008-04-28 11:33:07 +02007209 mutex_unlock(&sched_domains_mutex);
Paul Jackson029190c2007-10-18 23:40:20 -07007210}
7211
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07007212#if defined(CONFIG_SCHED_MC) || defined(CONFIG_SCHED_SMT)
Li Zefanc70f22d2009-01-05 19:07:50 +08007213static void arch_reinit_sched_domains(void)
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07007214{
Gautham R Shenoy95402b32008-01-25 21:08:02 +01007215 get_online_cpus();
Max Krasnyanskydfb512e2008-08-29 13:11:41 -07007216
7217 /* Destroy domains first to force the rebuild */
7218 partition_sched_domains(0, NULL, NULL);
7219
Max Krasnyanskye761b772008-07-15 04:43:49 -07007220 rebuild_sched_domains();
Gautham R Shenoy95402b32008-01-25 21:08:02 +01007221 put_online_cpus();
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07007222}
7223
7224static ssize_t sched_power_savings_store(const char *buf, size_t count, int smt)
7225{
Gautham R Shenoyafb8a9b2008-12-18 23:26:09 +05307226 unsigned int level = 0;
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07007227
Gautham R Shenoyafb8a9b2008-12-18 23:26:09 +05307228 if (sscanf(buf, "%u", &level) != 1)
7229 return -EINVAL;
7230
7231 /*
7232 * level is always be positive so don't check for
7233 * level < POWERSAVINGS_BALANCE_NONE which is 0
7234 * What happens on 0 or 1 byte write,
7235 * need to check for count as well?
7236 */
7237
7238 if (level >= MAX_POWERSAVINGS_BALANCE_LEVELS)
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07007239 return -EINVAL;
7240
7241 if (smt)
Gautham R Shenoyafb8a9b2008-12-18 23:26:09 +05307242 sched_smt_power_savings = level;
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07007243 else
Gautham R Shenoyafb8a9b2008-12-18 23:26:09 +05307244 sched_mc_power_savings = level;
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07007245
Li Zefanc70f22d2009-01-05 19:07:50 +08007246 arch_reinit_sched_domains();
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07007247
Li Zefanc70f22d2009-01-05 19:07:50 +08007248 return count;
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07007249}
7250
Adrian Bunk6707de002007-08-12 18:08:19 +02007251#ifdef CONFIG_SCHED_MC
Andi Kleenf718cd42008-07-29 22:33:52 -07007252static ssize_t sched_mc_power_savings_show(struct sysdev_class *class,
Andi Kleenc9be0a32010-01-05 12:47:58 +01007253 struct sysdev_class_attribute *attr,
Andi Kleenf718cd42008-07-29 22:33:52 -07007254 char *page)
Adrian Bunk6707de002007-08-12 18:08:19 +02007255{
7256 return sprintf(page, "%u\n", sched_mc_power_savings);
7257}
Andi Kleenf718cd42008-07-29 22:33:52 -07007258static ssize_t sched_mc_power_savings_store(struct sysdev_class *class,
Andi Kleenc9be0a32010-01-05 12:47:58 +01007259 struct sysdev_class_attribute *attr,
Adrian Bunk6707de002007-08-12 18:08:19 +02007260 const char *buf, size_t count)
7261{
7262 return sched_power_savings_store(buf, count, 0);
7263}
Andi Kleenf718cd42008-07-29 22:33:52 -07007264static SYSDEV_CLASS_ATTR(sched_mc_power_savings, 0644,
7265 sched_mc_power_savings_show,
7266 sched_mc_power_savings_store);
Adrian Bunk6707de002007-08-12 18:08:19 +02007267#endif
7268
7269#ifdef CONFIG_SCHED_SMT
Andi Kleenf718cd42008-07-29 22:33:52 -07007270static ssize_t sched_smt_power_savings_show(struct sysdev_class *dev,
Andi Kleenc9be0a32010-01-05 12:47:58 +01007271 struct sysdev_class_attribute *attr,
Andi Kleenf718cd42008-07-29 22:33:52 -07007272 char *page)
Adrian Bunk6707de002007-08-12 18:08:19 +02007273{
7274 return sprintf(page, "%u\n", sched_smt_power_savings);
7275}
Andi Kleenf718cd42008-07-29 22:33:52 -07007276static ssize_t sched_smt_power_savings_store(struct sysdev_class *dev,
Andi Kleenc9be0a32010-01-05 12:47:58 +01007277 struct sysdev_class_attribute *attr,
Adrian Bunk6707de002007-08-12 18:08:19 +02007278 const char *buf, size_t count)
7279{
7280 return sched_power_savings_store(buf, count, 1);
7281}
Andi Kleenf718cd42008-07-29 22:33:52 -07007282static SYSDEV_CLASS_ATTR(sched_smt_power_savings, 0644,
7283 sched_smt_power_savings_show,
Adrian Bunk6707de002007-08-12 18:08:19 +02007284 sched_smt_power_savings_store);
7285#endif
7286
Li Zefan39aac642009-01-05 19:18:02 +08007287int __init sched_create_sysfs_power_savings_entries(struct sysdev_class *cls)
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07007288{
7289 int err = 0;
Ingo Molnar48f24c42006-07-03 00:25:40 -07007290
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07007291#ifdef CONFIG_SCHED_SMT
7292 if (smt_capable())
7293 err = sysfs_create_file(&cls->kset.kobj,
7294 &attr_sched_smt_power_savings.attr);
7295#endif
7296#ifdef CONFIG_SCHED_MC
7297 if (!err && mc_capable())
7298 err = sysfs_create_file(&cls->kset.kobj,
7299 &attr_sched_mc_power_savings.attr);
7300#endif
7301 return err;
7302}
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +02007303#endif /* CONFIG_SCHED_MC || CONFIG_SCHED_SMT */
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07007304
Linus Torvalds1da177e2005-04-16 15:20:36 -07007305/*
Tejun Heo3a101d02010-06-08 21:40:36 +02007306 * Update cpusets according to cpu_active mask. If cpusets are
7307 * disabled, cpuset_update_active_cpus() becomes a simple wrapper
7308 * around partition_sched_domains().
Linus Torvalds1da177e2005-04-16 15:20:36 -07007309 */
Tejun Heo3a101d02010-06-08 21:40:36 +02007310static int __cpuexit cpuset_cpu_active(struct notifier_block *nfb,
7311 unsigned long action, void *hcpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007312{
Tejun Heo3a101d02010-06-08 21:40:36 +02007313 switch (action & ~CPU_TASKS_FROZEN) {
Max Krasnyanskye761b772008-07-15 04:43:49 -07007314 case CPU_ONLINE:
Peter Zijlstra6ad4c182009-11-25 13:31:39 +01007315 case CPU_DOWN_FAILED:
Tejun Heo3a101d02010-06-08 21:40:36 +02007316 cpuset_update_active_cpus();
Max Krasnyanskye761b772008-07-15 04:43:49 -07007317 return NOTIFY_OK;
Max Krasnyanskye761b772008-07-15 04:43:49 -07007318 default:
7319 return NOTIFY_DONE;
7320 }
7321}
Tejun Heo3a101d02010-06-08 21:40:36 +02007322
7323static int __cpuexit cpuset_cpu_inactive(struct notifier_block *nfb,
7324 unsigned long action, void *hcpu)
7325{
7326 switch (action & ~CPU_TASKS_FROZEN) {
7327 case CPU_DOWN_PREPARE:
7328 cpuset_update_active_cpus();
7329 return NOTIFY_OK;
7330 default:
7331 return NOTIFY_DONE;
7332 }
7333}
Max Krasnyanskye761b772008-07-15 04:43:49 -07007334
7335static int update_runtime(struct notifier_block *nfb,
7336 unsigned long action, void *hcpu)
7337{
Peter Zijlstra7def2be2008-06-05 14:49:58 +02007338 int cpu = (int)(long)hcpu;
7339
Linus Torvalds1da177e2005-04-16 15:20:36 -07007340 switch (action) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007341 case CPU_DOWN_PREPARE:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07007342 case CPU_DOWN_PREPARE_FROZEN:
Peter Zijlstra7def2be2008-06-05 14:49:58 +02007343 disable_runtime(cpu_rq(cpu));
Linus Torvalds1da177e2005-04-16 15:20:36 -07007344 return NOTIFY_OK;
7345
Linus Torvalds1da177e2005-04-16 15:20:36 -07007346 case CPU_DOWN_FAILED:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07007347 case CPU_DOWN_FAILED_FROZEN:
Linus Torvalds1da177e2005-04-16 15:20:36 -07007348 case CPU_ONLINE:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07007349 case CPU_ONLINE_FROZEN:
Peter Zijlstra7def2be2008-06-05 14:49:58 +02007350 enable_runtime(cpu_rq(cpu));
Max Krasnyanskye761b772008-07-15 04:43:49 -07007351 return NOTIFY_OK;
7352
Linus Torvalds1da177e2005-04-16 15:20:36 -07007353 default:
7354 return NOTIFY_DONE;
7355 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007356}
Linus Torvalds1da177e2005-04-16 15:20:36 -07007357
7358void __init sched_init_smp(void)
7359{
Rusty Russelldcc30a32008-11-25 02:35:12 +10307360 cpumask_var_t non_isolated_cpus;
7361
7362 alloc_cpumask_var(&non_isolated_cpus, GFP_KERNEL);
Yong Zhangcb5fd132009-09-14 20:20:16 +08007363 alloc_cpumask_var(&fallback_doms, GFP_KERNEL);
Nick Piggin5c1e1762006-10-03 01:14:04 -07007364
Mike Travis434d53b2008-04-04 18:11:04 -07007365#if defined(CONFIG_NUMA)
7366 sched_group_nodes_bycpu = kzalloc(nr_cpu_ids * sizeof(void **),
7367 GFP_KERNEL);
7368 BUG_ON(sched_group_nodes_bycpu == NULL);
7369#endif
Gautham R Shenoy95402b32008-01-25 21:08:02 +01007370 get_online_cpus();
Heiko Carstens712555e2008-04-28 11:33:07 +02007371 mutex_lock(&sched_domains_mutex);
Peter Zijlstra6ad4c182009-11-25 13:31:39 +01007372 arch_init_sched_domains(cpu_active_mask);
Rusty Russelldcc30a32008-11-25 02:35:12 +10307373 cpumask_andnot(non_isolated_cpus, cpu_possible_mask, cpu_isolated_map);
7374 if (cpumask_empty(non_isolated_cpus))
7375 cpumask_set_cpu(smp_processor_id(), non_isolated_cpus);
Heiko Carstens712555e2008-04-28 11:33:07 +02007376 mutex_unlock(&sched_domains_mutex);
Gautham R Shenoy95402b32008-01-25 21:08:02 +01007377 put_online_cpus();
Max Krasnyanskye761b772008-07-15 04:43:49 -07007378
Tejun Heo3a101d02010-06-08 21:40:36 +02007379 hotcpu_notifier(cpuset_cpu_active, CPU_PRI_CPUSET_ACTIVE);
7380 hotcpu_notifier(cpuset_cpu_inactive, CPU_PRI_CPUSET_INACTIVE);
Max Krasnyanskye761b772008-07-15 04:43:49 -07007381
7382 /* RT runtime code needs to handle some hotplug events */
7383 hotcpu_notifier(update_runtime, 0);
7384
Peter Zijlstrab328ca12008-04-29 10:02:46 +02007385 init_hrtick();
Nick Piggin5c1e1762006-10-03 01:14:04 -07007386
7387 /* Move init over to a non-isolated CPU */
Rusty Russelldcc30a32008-11-25 02:35:12 +10307388 if (set_cpus_allowed_ptr(current, non_isolated_cpus) < 0)
Nick Piggin5c1e1762006-10-03 01:14:04 -07007389 BUG();
Ingo Molnar19978ca2007-11-09 22:39:38 +01007390 sched_init_granularity();
Rusty Russelldcc30a32008-11-25 02:35:12 +10307391 free_cpumask_var(non_isolated_cpus);
Rusty Russell42128232008-11-25 02:35:12 +10307392
Rusty Russell0e3900e2008-11-25 02:35:13 +10307393 init_sched_rt_class();
Linus Torvalds1da177e2005-04-16 15:20:36 -07007394}
7395#else
7396void __init sched_init_smp(void)
7397{
Ingo Molnar19978ca2007-11-09 22:39:38 +01007398 sched_init_granularity();
Linus Torvalds1da177e2005-04-16 15:20:36 -07007399}
7400#endif /* CONFIG_SMP */
7401
Arun R Bharadwajcd1bb942009-04-16 12:15:34 +05307402const_debug unsigned int sysctl_timer_migration = 1;
7403
Linus Torvalds1da177e2005-04-16 15:20:36 -07007404int in_sched_functions(unsigned long addr)
7405{
Linus Torvalds1da177e2005-04-16 15:20:36 -07007406 return in_lock_functions(addr) ||
7407 (addr >= (unsigned long)__sched_text_start
7408 && addr < (unsigned long)__sched_text_end);
7409}
7410
Alexey Dobriyana9957442007-10-15 17:00:13 +02007411static void init_cfs_rq(struct cfs_rq *cfs_rq, struct rq *rq)
Ingo Molnardd41f592007-07-09 18:51:59 +02007412{
7413 cfs_rq->tasks_timeline = RB_ROOT;
Peter Zijlstra4a55bd52008-04-19 19:45:00 +02007414 INIT_LIST_HEAD(&cfs_rq->tasks);
Ingo Molnardd41f592007-07-09 18:51:59 +02007415#ifdef CONFIG_FAIR_GROUP_SCHED
7416 cfs_rq->rq = rq;
7417#endif
Peter Zijlstra67e9fb22007-10-15 17:00:10 +02007418 cfs_rq->min_vruntime = (u64)(-(1LL << 20));
Ingo Molnardd41f592007-07-09 18:51:59 +02007419}
7420
Peter Zijlstrafa85ae22008-01-25 21:08:29 +01007421static void init_rt_rq(struct rt_rq *rt_rq, struct rq *rq)
7422{
7423 struct rt_prio_array *array;
7424 int i;
7425
7426 array = &rt_rq->active;
7427 for (i = 0; i < MAX_RT_PRIO; i++) {
7428 INIT_LIST_HEAD(array->queue + i);
7429 __clear_bit(i, array->bitmap);
7430 }
7431 /* delimiter for bitsearch: */
7432 __set_bit(MAX_RT_PRIO, array->bitmap);
7433
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01007434#if defined CONFIG_SMP || defined CONFIG_RT_GROUP_SCHED
Gregory Haskinse864c492008-12-29 09:39:49 -05007435 rt_rq->highest_prio.curr = MAX_RT_PRIO;
Gregory Haskins398a1532009-01-14 09:10:04 -05007436#ifdef CONFIG_SMP
Gregory Haskinse864c492008-12-29 09:39:49 -05007437 rt_rq->highest_prio.next = MAX_RT_PRIO;
Peter Zijlstra48d5e252008-01-25 21:08:31 +01007438#endif
Peter Zijlstrafa85ae22008-01-25 21:08:29 +01007439#endif
7440#ifdef CONFIG_SMP
7441 rt_rq->rt_nr_migratory = 0;
Peter Zijlstrafa85ae22008-01-25 21:08:29 +01007442 rt_rq->overloaded = 0;
Thomas Gleixner05fa7852009-11-17 14:28:38 +01007443 plist_head_init_raw(&rt_rq->pushable_tasks, &rq->lock);
Peter Zijlstrafa85ae22008-01-25 21:08:29 +01007444#endif
7445
7446 rt_rq->rt_time = 0;
7447 rt_rq->rt_throttled = 0;
Peter Zijlstraac086bc2008-04-19 19:44:58 +02007448 rt_rq->rt_runtime = 0;
Thomas Gleixner0986b112009-11-17 15:32:06 +01007449 raw_spin_lock_init(&rt_rq->rt_runtime_lock);
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007450
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01007451#ifdef CONFIG_RT_GROUP_SCHED
Peter Zijlstra23b0fdf2008-02-13 15:45:39 +01007452 rt_rq->rt_nr_boosted = 0;
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007453 rt_rq->rq = rq;
7454#endif
Peter Zijlstrafa85ae22008-01-25 21:08:29 +01007455}
7456
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007457#ifdef CONFIG_FAIR_GROUP_SCHED
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02007458static void init_tg_cfs_entry(struct task_group *tg, struct cfs_rq *cfs_rq,
7459 struct sched_entity *se, int cpu, int add,
7460 struct sched_entity *parent)
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007461{
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02007462 struct rq *rq = cpu_rq(cpu);
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007463 tg->cfs_rq[cpu] = cfs_rq;
7464 init_cfs_rq(cfs_rq, rq);
7465 cfs_rq->tg = tg;
7466 if (add)
7467 list_add(&cfs_rq->leaf_cfs_rq_list, &rq->leaf_cfs_rq_list);
7468
7469 tg->se[cpu] = se;
Dhaval Giani354d60c2008-04-19 19:44:59 +02007470 /* se could be NULL for init_task_group */
7471 if (!se)
7472 return;
7473
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02007474 if (!parent)
7475 se->cfs_rq = &rq->cfs;
7476 else
7477 se->cfs_rq = parent->my_q;
7478
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007479 se->my_q = cfs_rq;
7480 se->load.weight = tg->shares;
Peter Zijlstrae05510d2008-05-05 23:56:17 +02007481 se->load.inv_weight = 0;
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02007482 se->parent = parent;
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007483}
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01007484#endif
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007485
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01007486#ifdef CONFIG_RT_GROUP_SCHED
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02007487static void init_tg_rt_entry(struct task_group *tg, struct rt_rq *rt_rq,
7488 struct sched_rt_entity *rt_se, int cpu, int add,
7489 struct sched_rt_entity *parent)
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007490{
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02007491 struct rq *rq = cpu_rq(cpu);
7492
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007493 tg->rt_rq[cpu] = rt_rq;
7494 init_rt_rq(rt_rq, rq);
7495 rt_rq->tg = tg;
Peter Zijlstraac086bc2008-04-19 19:44:58 +02007496 rt_rq->rt_runtime = tg->rt_bandwidth.rt_runtime;
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007497 if (add)
7498 list_add(&rt_rq->leaf_rt_rq_list, &rq->leaf_rt_rq_list);
7499
7500 tg->rt_se[cpu] = rt_se;
Dhaval Giani354d60c2008-04-19 19:44:59 +02007501 if (!rt_se)
7502 return;
7503
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02007504 if (!parent)
7505 rt_se->rt_rq = &rq->rt;
7506 else
7507 rt_se->rt_rq = parent->my_q;
7508
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007509 rt_se->my_q = rt_rq;
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02007510 rt_se->parent = parent;
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007511 INIT_LIST_HEAD(&rt_se->run_list);
7512}
7513#endif
7514
Linus Torvalds1da177e2005-04-16 15:20:36 -07007515void __init sched_init(void)
7516{
Ingo Molnardd41f592007-07-09 18:51:59 +02007517 int i, j;
Mike Travis434d53b2008-04-04 18:11:04 -07007518 unsigned long alloc_size = 0, ptr;
7519
7520#ifdef CONFIG_FAIR_GROUP_SCHED
7521 alloc_size += 2 * nr_cpu_ids * sizeof(void **);
7522#endif
7523#ifdef CONFIG_RT_GROUP_SCHED
7524 alloc_size += 2 * nr_cpu_ids * sizeof(void **);
7525#endif
Rusty Russelldf7c8e82009-03-19 15:22:20 +10307526#ifdef CONFIG_CPUMASK_OFFSTACK
Rusty Russell8c083f02009-03-19 15:22:20 +10307527 alloc_size += num_possible_cpus() * cpumask_size();
Rusty Russelldf7c8e82009-03-19 15:22:20 +10307528#endif
Mike Travis434d53b2008-04-04 18:11:04 -07007529 if (alloc_size) {
Pekka Enberg36b7b6d2009-06-10 23:42:36 +03007530 ptr = (unsigned long)kzalloc(alloc_size, GFP_NOWAIT);
Mike Travis434d53b2008-04-04 18:11:04 -07007531
7532#ifdef CONFIG_FAIR_GROUP_SCHED
7533 init_task_group.se = (struct sched_entity **)ptr;
7534 ptr += nr_cpu_ids * sizeof(void **);
7535
7536 init_task_group.cfs_rq = (struct cfs_rq **)ptr;
7537 ptr += nr_cpu_ids * sizeof(void **);
Peter Zijlstraeff766a2008-04-19 19:45:00 +02007538
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +02007539#endif /* CONFIG_FAIR_GROUP_SCHED */
Mike Travis434d53b2008-04-04 18:11:04 -07007540#ifdef CONFIG_RT_GROUP_SCHED
7541 init_task_group.rt_se = (struct sched_rt_entity **)ptr;
7542 ptr += nr_cpu_ids * sizeof(void **);
7543
7544 init_task_group.rt_rq = (struct rt_rq **)ptr;
Peter Zijlstraeff766a2008-04-19 19:45:00 +02007545 ptr += nr_cpu_ids * sizeof(void **);
7546
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +02007547#endif /* CONFIG_RT_GROUP_SCHED */
Rusty Russelldf7c8e82009-03-19 15:22:20 +10307548#ifdef CONFIG_CPUMASK_OFFSTACK
7549 for_each_possible_cpu(i) {
7550 per_cpu(load_balance_tmpmask, i) = (void *)ptr;
7551 ptr += cpumask_size();
7552 }
7553#endif /* CONFIG_CPUMASK_OFFSTACK */
Mike Travis434d53b2008-04-04 18:11:04 -07007554 }
Ingo Molnardd41f592007-07-09 18:51:59 +02007555
Gregory Haskins57d885f2008-01-25 21:08:18 +01007556#ifdef CONFIG_SMP
7557 init_defrootdomain();
7558#endif
7559
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02007560 init_rt_bandwidth(&def_rt_bandwidth,
7561 global_rt_period(), global_rt_runtime());
7562
7563#ifdef CONFIG_RT_GROUP_SCHED
7564 init_rt_bandwidth(&init_task_group.rt_bandwidth,
7565 global_rt_period(), global_rt_runtime());
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +02007566#endif /* CONFIG_RT_GROUP_SCHED */
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02007567
Dhaval Giani7c941432010-01-20 13:26:18 +01007568#ifdef CONFIG_CGROUP_SCHED
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007569 list_add(&init_task_group.list, &task_groups);
Peter Zijlstraf473aa52008-04-19 19:45:00 +02007570 INIT_LIST_HEAD(&init_task_group.children);
7571
Dhaval Giani7c941432010-01-20 13:26:18 +01007572#endif /* CONFIG_CGROUP_SCHED */
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007573
Jiri Kosina4a6cc4b2009-10-29 00:26:00 +09007574#if defined CONFIG_FAIR_GROUP_SCHED && defined CONFIG_SMP
7575 update_shares_data = __alloc_percpu(nr_cpu_ids * sizeof(unsigned long),
7576 __alignof__(unsigned long));
7577#endif
KAMEZAWA Hiroyuki0a945022006-03-28 01:56:37 -08007578 for_each_possible_cpu(i) {
Ingo Molnar70b97a72006-07-03 00:25:42 -07007579 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007580
7581 rq = cpu_rq(i);
Thomas Gleixner05fa7852009-11-17 14:28:38 +01007582 raw_spin_lock_init(&rq->lock);
Nick Piggin78979862005-06-25 14:57:13 -07007583 rq->nr_running = 0;
Thomas Gleixnerdce48a82009-04-11 10:43:41 +02007584 rq->calc_load_active = 0;
7585 rq->calc_load_update = jiffies + LOAD_FREQ;
Ingo Molnardd41f592007-07-09 18:51:59 +02007586 init_cfs_rq(&rq->cfs, rq);
Peter Zijlstrafa85ae22008-01-25 21:08:29 +01007587 init_rt_rq(&rq->rt, rq);
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007588#ifdef CONFIG_FAIR_GROUP_SCHED
7589 init_task_group.shares = init_task_group_load;
7590 INIT_LIST_HEAD(&rq->leaf_cfs_rq_list);
Dhaval Giani354d60c2008-04-19 19:44:59 +02007591#ifdef CONFIG_CGROUP_SCHED
7592 /*
7593 * How much cpu bandwidth does init_task_group get?
7594 *
7595 * In case of task-groups formed thr' the cgroup filesystem, it
7596 * gets 100% of the cpu resources in the system. This overall
7597 * system cpu resource is divided among the tasks of
7598 * init_task_group and its child task-groups in a fair manner,
7599 * based on each entity's (task or task-group's) weight
7600 * (se->load.weight).
7601 *
7602 * In other words, if init_task_group has 10 tasks of weight
7603 * 1024) and two child groups A0 and A1 (of weight 1024 each),
7604 * then A0's share of the cpu resource is:
7605 *
Ingo Molnar0d905bc2009-05-04 19:13:30 +02007606 * A0's bandwidth = 1024 / (10*1024 + 1024 + 1024) = 8.33%
Dhaval Giani354d60c2008-04-19 19:44:59 +02007607 *
7608 * We achieve this by letting init_task_group's tasks sit
7609 * directly in rq->cfs (i.e init_task_group->se[] = NULL).
7610 */
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02007611 init_tg_cfs_entry(&init_task_group, &rq->cfs, NULL, i, 1, NULL);
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01007612#endif
Dhaval Giani354d60c2008-04-19 19:44:59 +02007613#endif /* CONFIG_FAIR_GROUP_SCHED */
7614
7615 rq->rt.rt_runtime = def_rt_bandwidth.rt_runtime;
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01007616#ifdef CONFIG_RT_GROUP_SCHED
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007617 INIT_LIST_HEAD(&rq->leaf_rt_rq_list);
Dhaval Giani354d60c2008-04-19 19:44:59 +02007618#ifdef CONFIG_CGROUP_SCHED
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02007619 init_tg_rt_entry(&init_task_group, &rq->rt, NULL, i, 1, NULL);
Dhaval Giani354d60c2008-04-19 19:44:59 +02007620#endif
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007621#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07007622
Ingo Molnardd41f592007-07-09 18:51:59 +02007623 for (j = 0; j < CPU_LOAD_IDX_MAX; j++)
7624 rq->cpu_load[j] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007625#ifdef CONFIG_SMP
Nick Piggin41c7ce92005-06-25 14:57:24 -07007626 rq->sd = NULL;
Gregory Haskins57d885f2008-01-25 21:08:18 +01007627 rq->rd = NULL;
Peter Zijlstrae51fd5e2010-05-31 12:37:30 +02007628 rq->cpu_power = SCHED_LOAD_SCALE;
Gregory Haskins3f029d32009-07-29 11:08:47 -04007629 rq->post_schedule = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007630 rq->active_balance = 0;
Ingo Molnardd41f592007-07-09 18:51:59 +02007631 rq->next_balance = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007632 rq->push_cpu = 0;
Christoph Lameter0a2966b2006-09-25 23:30:51 -07007633 rq->cpu = i;
Gregory Haskins1f11eb62008-06-04 15:04:05 -04007634 rq->online = 0;
Mike Galbraitheae0c9d2009-11-10 03:50:02 +01007635 rq->idle_stamp = 0;
7636 rq->avg_idle = 2*sysctl_sched_migration_cost;
Gregory Haskinsdc938522008-01-25 21:08:26 +01007637 rq_attach_root(rq, &def_root_domain);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007638#endif
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01007639 init_rq_hrtick(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007640 atomic_set(&rq->nr_iowait, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007641 }
7642
Peter Williams2dd73a42006-06-27 02:54:34 -07007643 set_load_weight(&init_task);
Heiko Carstensb50f60c2006-07-30 03:03:52 -07007644
Avi Kivitye107be32007-07-26 13:40:43 +02007645#ifdef CONFIG_PREEMPT_NOTIFIERS
7646 INIT_HLIST_HEAD(&init_task.preempt_notifiers);
7647#endif
7648
Christoph Lameterc9819f42006-12-10 02:20:25 -08007649#ifdef CONFIG_SMP
Carlos R. Mafra962cf362008-05-15 11:15:37 -03007650 open_softirq(SCHED_SOFTIRQ, run_rebalance_domains);
Christoph Lameterc9819f42006-12-10 02:20:25 -08007651#endif
7652
Heiko Carstensb50f60c2006-07-30 03:03:52 -07007653#ifdef CONFIG_RT_MUTEXES
Thomas Gleixner1d615482009-11-17 14:54:03 +01007654 plist_head_init_raw(&init_task.pi_waiters, &init_task.pi_lock);
Heiko Carstensb50f60c2006-07-30 03:03:52 -07007655#endif
7656
Linus Torvalds1da177e2005-04-16 15:20:36 -07007657 /*
7658 * The boot idle thread does lazy MMU switching as well:
7659 */
7660 atomic_inc(&init_mm.mm_count);
7661 enter_lazy_tlb(&init_mm, current);
7662
7663 /*
7664 * Make us the idle thread. Technically, schedule() should not be
7665 * called from this thread, however somewhere below it might be,
7666 * but because we are the idle thread, we just pick up running again
7667 * when this runqueue becomes "idle".
7668 */
7669 init_idle(current, smp_processor_id());
Thomas Gleixnerdce48a82009-04-11 10:43:41 +02007670
7671 calc_load_update = jiffies + LOAD_FREQ;
7672
Ingo Molnardd41f592007-07-09 18:51:59 +02007673 /*
7674 * During early bootup we pretend to be a normal task:
7675 */
7676 current->sched_class = &fair_sched_class;
Ingo Molnar6892b752008-02-13 14:02:36 +01007677
Rusty Russell6a7b3dc2008-11-25 02:35:04 +10307678 /* Allocate the nohz_cpu_mask if CONFIG_CPUMASK_OFFSTACK */
Rusty Russell49557e62009-11-02 20:37:20 +10307679 zalloc_cpumask_var(&nohz_cpu_mask, GFP_NOWAIT);
Rusty Russellbf4d83f2008-11-25 09:57:51 +10307680#ifdef CONFIG_SMP
Rusty Russell7d1e6a92008-11-25 02:35:09 +10307681#ifdef CONFIG_NO_HZ
Rusty Russell49557e62009-11-02 20:37:20 +10307682 zalloc_cpumask_var(&nohz.cpu_mask, GFP_NOWAIT);
Pekka Enberg4bdddf82009-06-11 08:35:27 +03007683 alloc_cpumask_var(&nohz.ilb_grp_nohz_mask, GFP_NOWAIT);
Rusty Russell7d1e6a92008-11-25 02:35:09 +10307684#endif
Rusty Russellbdddd292009-12-02 14:09:16 +10307685 /* May be allocated at isolcpus cmdline parse time */
7686 if (cpu_isolated_map == NULL)
7687 zalloc_cpumask_var(&cpu_isolated_map, GFP_NOWAIT);
Rusty Russellbf4d83f2008-11-25 09:57:51 +10307688#endif /* SMP */
Rusty Russell6a7b3dc2008-11-25 02:35:04 +10307689
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007690 perf_event_init();
Ingo Molnar0d905bc2009-05-04 19:13:30 +02007691
Ingo Molnar6892b752008-02-13 14:02:36 +01007692 scheduler_running = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007693}
7694
7695#ifdef CONFIG_DEBUG_SPINLOCK_SLEEP
Frederic Weisbeckere4aafea2009-07-16 15:44:29 +02007696static inline int preempt_count_equals(int preempt_offset)
7697{
Frederic Weisbecker234da7b2009-12-16 20:21:05 +01007698 int nested = (preempt_count() & ~PREEMPT_ACTIVE) + rcu_preempt_depth();
Frederic Weisbeckere4aafea2009-07-16 15:44:29 +02007699
7700 return (nested == PREEMPT_INATOMIC_BASE + preempt_offset);
7701}
7702
Simon Kagstromd8948372009-12-23 11:08:18 +01007703void __might_sleep(const char *file, int line, int preempt_offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007704{
Ingo Molnar48f24c42006-07-03 00:25:40 -07007705#ifdef in_atomic
Linus Torvalds1da177e2005-04-16 15:20:36 -07007706 static unsigned long prev_jiffy; /* ratelimiting */
7707
Frederic Weisbeckere4aafea2009-07-16 15:44:29 +02007708 if ((preempt_count_equals(preempt_offset) && !irqs_disabled()) ||
7709 system_state != SYSTEM_RUNNING || oops_in_progress)
Ingo Molnaraef745f2008-08-28 11:34:43 +02007710 return;
7711 if (time_before(jiffies, prev_jiffy + HZ) && prev_jiffy)
7712 return;
7713 prev_jiffy = jiffies;
7714
Peter Zijlstra3df0fc52009-12-20 14:23:57 +01007715 printk(KERN_ERR
7716 "BUG: sleeping function called from invalid context at %s:%d\n",
7717 file, line);
7718 printk(KERN_ERR
7719 "in_atomic(): %d, irqs_disabled(): %d, pid: %d, name: %s\n",
7720 in_atomic(), irqs_disabled(),
7721 current->pid, current->comm);
Ingo Molnaraef745f2008-08-28 11:34:43 +02007722
7723 debug_show_held_locks(current);
7724 if (irqs_disabled())
7725 print_irqtrace_events(current);
7726 dump_stack();
Linus Torvalds1da177e2005-04-16 15:20:36 -07007727#endif
7728}
7729EXPORT_SYMBOL(__might_sleep);
7730#endif
7731
7732#ifdef CONFIG_MAGIC_SYSRQ
Andi Kleen3a5e4dc2007-10-15 17:00:15 +02007733static void normalize_task(struct rq *rq, struct task_struct *p)
7734{
7735 int on_rq;
Peter Zijlstra3e51f332008-05-03 18:29:28 +02007736
Andi Kleen3a5e4dc2007-10-15 17:00:15 +02007737 on_rq = p->se.on_rq;
7738 if (on_rq)
7739 deactivate_task(rq, p, 0);
7740 __setscheduler(rq, p, SCHED_NORMAL, 0);
7741 if (on_rq) {
7742 activate_task(rq, p, 0);
7743 resched_task(rq->curr);
7744 }
7745}
7746
Linus Torvalds1da177e2005-04-16 15:20:36 -07007747void normalize_rt_tasks(void)
7748{
Ingo Molnara0f98a12007-06-17 18:37:45 +02007749 struct task_struct *g, *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007750 unsigned long flags;
Ingo Molnar70b97a72006-07-03 00:25:42 -07007751 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007752
Peter Zijlstra4cf5d772008-02-13 15:45:39 +01007753 read_lock_irqsave(&tasklist_lock, flags);
Ingo Molnara0f98a12007-06-17 18:37:45 +02007754 do_each_thread(g, p) {
Ingo Molnar178be792007-10-15 17:00:18 +02007755 /*
7756 * Only normalize user tasks:
7757 */
7758 if (!p->mm)
7759 continue;
7760
Ingo Molnardd41f592007-07-09 18:51:59 +02007761 p->se.exec_start = 0;
Ingo Molnar6cfb0d52007-08-02 17:41:40 +02007762#ifdef CONFIG_SCHEDSTATS
Lucas De Marchi41acab82010-03-10 23:37:45 -03007763 p->se.statistics.wait_start = 0;
7764 p->se.statistics.sleep_start = 0;
7765 p->se.statistics.block_start = 0;
Ingo Molnar6cfb0d52007-08-02 17:41:40 +02007766#endif
Ingo Molnardd41f592007-07-09 18:51:59 +02007767
7768 if (!rt_task(p)) {
7769 /*
7770 * Renice negative nice level userspace
7771 * tasks back to 0:
7772 */
7773 if (TASK_NICE(p) < 0 && p->mm)
7774 set_user_nice(p, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007775 continue;
Ingo Molnardd41f592007-07-09 18:51:59 +02007776 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007777
Thomas Gleixner1d615482009-11-17 14:54:03 +01007778 raw_spin_lock(&p->pi_lock);
Ingo Molnarb29739f2006-06-27 02:54:51 -07007779 rq = __task_rq_lock(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007780
Ingo Molnar178be792007-10-15 17:00:18 +02007781 normalize_task(rq, p);
Andi Kleen3a5e4dc2007-10-15 17:00:15 +02007782
Ingo Molnarb29739f2006-06-27 02:54:51 -07007783 __task_rq_unlock(rq);
Thomas Gleixner1d615482009-11-17 14:54:03 +01007784 raw_spin_unlock(&p->pi_lock);
Ingo Molnara0f98a12007-06-17 18:37:45 +02007785 } while_each_thread(g, p);
7786
Peter Zijlstra4cf5d772008-02-13 15:45:39 +01007787 read_unlock_irqrestore(&tasklist_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007788}
7789
7790#endif /* CONFIG_MAGIC_SYSRQ */
Linus Torvalds1df5c102005-09-12 07:59:21 -07007791
Jason Wessel67fc4e02010-05-20 21:04:21 -05007792#if defined(CONFIG_IA64) || defined(CONFIG_KGDB_KDB)
Linus Torvalds1df5c102005-09-12 07:59:21 -07007793/*
Jason Wessel67fc4e02010-05-20 21:04:21 -05007794 * These functions are only useful for the IA64 MCA handling, or kdb.
Linus Torvalds1df5c102005-09-12 07:59:21 -07007795 *
7796 * They can only be called when the whole system has been
7797 * stopped - every CPU needs to be quiescent, and no scheduling
7798 * activity can take place. Using them for anything else would
7799 * be a serious bug, and as a result, they aren't even visible
7800 * under any other configuration.
7801 */
7802
7803/**
7804 * curr_task - return the current task for a given cpu.
7805 * @cpu: the processor in question.
7806 *
7807 * ONLY VALID WHEN THE WHOLE SYSTEM IS STOPPED!
7808 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07007809struct task_struct *curr_task(int cpu)
Linus Torvalds1df5c102005-09-12 07:59:21 -07007810{
7811 return cpu_curr(cpu);
7812}
7813
Jason Wessel67fc4e02010-05-20 21:04:21 -05007814#endif /* defined(CONFIG_IA64) || defined(CONFIG_KGDB_KDB) */
7815
7816#ifdef CONFIG_IA64
Linus Torvalds1df5c102005-09-12 07:59:21 -07007817/**
7818 * set_curr_task - set the current task for a given cpu.
7819 * @cpu: the processor in question.
7820 * @p: the task pointer to set.
7821 *
7822 * Description: This function must only be used when non-maskable interrupts
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01007823 * are serviced on a separate stack. It allows the architecture to switch the
7824 * notion of the current task on a cpu in a non-blocking manner. This function
Linus Torvalds1df5c102005-09-12 07:59:21 -07007825 * must be called with all CPU's synchronized, and interrupts disabled, the
7826 * and caller must save the original value of the current task (see
7827 * curr_task() above) and restore that value before reenabling interrupts and
7828 * re-starting the system.
7829 *
7830 * ONLY VALID WHEN THE WHOLE SYSTEM IS STOPPED!
7831 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07007832void set_curr_task(int cpu, struct task_struct *p)
Linus Torvalds1df5c102005-09-12 07:59:21 -07007833{
7834 cpu_curr(cpu) = p;
7835}
7836
7837#endif
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007838
Peter Zijlstrabccbe082008-02-13 15:45:40 +01007839#ifdef CONFIG_FAIR_GROUP_SCHED
7840static void free_fair_sched_group(struct task_group *tg)
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007841{
7842 int i;
7843
7844 for_each_possible_cpu(i) {
7845 if (tg->cfs_rq)
7846 kfree(tg->cfs_rq[i]);
7847 if (tg->se)
7848 kfree(tg->se[i]);
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007849 }
7850
7851 kfree(tg->cfs_rq);
7852 kfree(tg->se);
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007853}
7854
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02007855static
7856int alloc_fair_sched_group(struct task_group *tg, struct task_group *parent)
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007857{
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007858 struct cfs_rq *cfs_rq;
Li Zefaneab17222008-10-29 17:03:22 +08007859 struct sched_entity *se;
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02007860 struct rq *rq;
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007861 int i;
7862
Mike Travis434d53b2008-04-04 18:11:04 -07007863 tg->cfs_rq = kzalloc(sizeof(cfs_rq) * nr_cpu_ids, GFP_KERNEL);
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007864 if (!tg->cfs_rq)
7865 goto err;
Mike Travis434d53b2008-04-04 18:11:04 -07007866 tg->se = kzalloc(sizeof(se) * nr_cpu_ids, GFP_KERNEL);
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007867 if (!tg->se)
7868 goto err;
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01007869
7870 tg->shares = NICE_0_LOAD;
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007871
7872 for_each_possible_cpu(i) {
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02007873 rq = cpu_rq(i);
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007874
Li Zefaneab17222008-10-29 17:03:22 +08007875 cfs_rq = kzalloc_node(sizeof(struct cfs_rq),
7876 GFP_KERNEL, cpu_to_node(i));
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007877 if (!cfs_rq)
7878 goto err;
7879
Li Zefaneab17222008-10-29 17:03:22 +08007880 se = kzalloc_node(sizeof(struct sched_entity),
7881 GFP_KERNEL, cpu_to_node(i));
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007882 if (!se)
Phil Carmodydfc12eb2009-12-10 14:29:37 +02007883 goto err_free_rq;
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007884
Li Zefaneab17222008-10-29 17:03:22 +08007885 init_tg_cfs_entry(tg, cfs_rq, se, i, 0, parent->se[i]);
Peter Zijlstrabccbe082008-02-13 15:45:40 +01007886 }
7887
7888 return 1;
7889
Phil Carmodydfc12eb2009-12-10 14:29:37 +02007890 err_free_rq:
7891 kfree(cfs_rq);
Peter Zijlstrabccbe082008-02-13 15:45:40 +01007892 err:
7893 return 0;
7894}
7895
7896static inline void register_fair_sched_group(struct task_group *tg, int cpu)
7897{
7898 list_add_rcu(&tg->cfs_rq[cpu]->leaf_cfs_rq_list,
7899 &cpu_rq(cpu)->leaf_cfs_rq_list);
7900}
7901
7902static inline void unregister_fair_sched_group(struct task_group *tg, int cpu)
7903{
7904 list_del_rcu(&tg->cfs_rq[cpu]->leaf_cfs_rq_list);
7905}
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +02007906#else /* !CONFG_FAIR_GROUP_SCHED */
Peter Zijlstrabccbe082008-02-13 15:45:40 +01007907static inline void free_fair_sched_group(struct task_group *tg)
7908{
7909}
7910
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02007911static inline
7912int alloc_fair_sched_group(struct task_group *tg, struct task_group *parent)
Peter Zijlstrabccbe082008-02-13 15:45:40 +01007913{
7914 return 1;
7915}
7916
7917static inline void register_fair_sched_group(struct task_group *tg, int cpu)
7918{
7919}
7920
7921static inline void unregister_fair_sched_group(struct task_group *tg, int cpu)
7922{
7923}
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +02007924#endif /* CONFIG_FAIR_GROUP_SCHED */
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01007925
7926#ifdef CONFIG_RT_GROUP_SCHED
Peter Zijlstrabccbe082008-02-13 15:45:40 +01007927static void free_rt_sched_group(struct task_group *tg)
7928{
7929 int i;
7930
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02007931 destroy_rt_bandwidth(&tg->rt_bandwidth);
7932
Peter Zijlstrabccbe082008-02-13 15:45:40 +01007933 for_each_possible_cpu(i) {
7934 if (tg->rt_rq)
7935 kfree(tg->rt_rq[i]);
7936 if (tg->rt_se)
7937 kfree(tg->rt_se[i]);
7938 }
7939
7940 kfree(tg->rt_rq);
7941 kfree(tg->rt_se);
7942}
7943
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02007944static
7945int alloc_rt_sched_group(struct task_group *tg, struct task_group *parent)
Peter Zijlstrabccbe082008-02-13 15:45:40 +01007946{
7947 struct rt_rq *rt_rq;
Li Zefaneab17222008-10-29 17:03:22 +08007948 struct sched_rt_entity *rt_se;
Peter Zijlstrabccbe082008-02-13 15:45:40 +01007949 struct rq *rq;
7950 int i;
7951
Mike Travis434d53b2008-04-04 18:11:04 -07007952 tg->rt_rq = kzalloc(sizeof(rt_rq) * nr_cpu_ids, GFP_KERNEL);
Peter Zijlstrabccbe082008-02-13 15:45:40 +01007953 if (!tg->rt_rq)
7954 goto err;
Mike Travis434d53b2008-04-04 18:11:04 -07007955 tg->rt_se = kzalloc(sizeof(rt_se) * nr_cpu_ids, GFP_KERNEL);
Peter Zijlstrabccbe082008-02-13 15:45:40 +01007956 if (!tg->rt_se)
7957 goto err;
7958
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02007959 init_rt_bandwidth(&tg->rt_bandwidth,
7960 ktime_to_ns(def_rt_bandwidth.rt_period), 0);
Peter Zijlstrabccbe082008-02-13 15:45:40 +01007961
7962 for_each_possible_cpu(i) {
7963 rq = cpu_rq(i);
7964
Li Zefaneab17222008-10-29 17:03:22 +08007965 rt_rq = kzalloc_node(sizeof(struct rt_rq),
7966 GFP_KERNEL, cpu_to_node(i));
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007967 if (!rt_rq)
7968 goto err;
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007969
Li Zefaneab17222008-10-29 17:03:22 +08007970 rt_se = kzalloc_node(sizeof(struct sched_rt_entity),
7971 GFP_KERNEL, cpu_to_node(i));
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007972 if (!rt_se)
Phil Carmodydfc12eb2009-12-10 14:29:37 +02007973 goto err_free_rq;
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007974
Li Zefaneab17222008-10-29 17:03:22 +08007975 init_tg_rt_entry(tg, rt_rq, rt_se, i, 0, parent->rt_se[i]);
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007976 }
7977
Peter Zijlstrabccbe082008-02-13 15:45:40 +01007978 return 1;
7979
Phil Carmodydfc12eb2009-12-10 14:29:37 +02007980 err_free_rq:
7981 kfree(rt_rq);
Peter Zijlstrabccbe082008-02-13 15:45:40 +01007982 err:
7983 return 0;
7984}
7985
7986static inline void register_rt_sched_group(struct task_group *tg, int cpu)
7987{
7988 list_add_rcu(&tg->rt_rq[cpu]->leaf_rt_rq_list,
7989 &cpu_rq(cpu)->leaf_rt_rq_list);
7990}
7991
7992static inline void unregister_rt_sched_group(struct task_group *tg, int cpu)
7993{
7994 list_del_rcu(&tg->rt_rq[cpu]->leaf_rt_rq_list);
7995}
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +02007996#else /* !CONFIG_RT_GROUP_SCHED */
Peter Zijlstrabccbe082008-02-13 15:45:40 +01007997static inline void free_rt_sched_group(struct task_group *tg)
7998{
7999}
8000
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008001static inline
8002int alloc_rt_sched_group(struct task_group *tg, struct task_group *parent)
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008003{
8004 return 1;
8005}
8006
8007static inline void register_rt_sched_group(struct task_group *tg, int cpu)
8008{
8009}
8010
8011static inline void unregister_rt_sched_group(struct task_group *tg, int cpu)
8012{
8013}
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +02008014#endif /* CONFIG_RT_GROUP_SCHED */
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008015
Dhaval Giani7c941432010-01-20 13:26:18 +01008016#ifdef CONFIG_CGROUP_SCHED
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008017static void free_sched_group(struct task_group *tg)
8018{
8019 free_fair_sched_group(tg);
8020 free_rt_sched_group(tg);
8021 kfree(tg);
8022}
8023
8024/* allocate runqueue etc for a new task group */
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008025struct task_group *sched_create_group(struct task_group *parent)
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008026{
8027 struct task_group *tg;
8028 unsigned long flags;
8029 int i;
8030
8031 tg = kzalloc(sizeof(*tg), GFP_KERNEL);
8032 if (!tg)
8033 return ERR_PTR(-ENOMEM);
8034
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008035 if (!alloc_fair_sched_group(tg, parent))
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008036 goto err;
8037
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008038 if (!alloc_rt_sched_group(tg, parent))
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008039 goto err;
8040
Peter Zijlstra8ed36992008-02-13 15:45:39 +01008041 spin_lock_irqsave(&task_group_lock, flags);
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02008042 for_each_possible_cpu(i) {
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008043 register_fair_sched_group(tg, i);
8044 register_rt_sched_group(tg, i);
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02008045 }
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008046 list_add_rcu(&tg->list, &task_groups);
Peter Zijlstraf473aa52008-04-19 19:45:00 +02008047
8048 WARN_ON(!parent); /* root should already exist */
8049
8050 tg->parent = parent;
Peter Zijlstraf473aa52008-04-19 19:45:00 +02008051 INIT_LIST_HEAD(&tg->children);
Zhang, Yanmin09f27242030-08-14 15:56:40 +08008052 list_add_rcu(&tg->siblings, &parent->children);
Peter Zijlstra8ed36992008-02-13 15:45:39 +01008053 spin_unlock_irqrestore(&task_group_lock, flags);
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008054
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02008055 return tg;
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008056
8057err:
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008058 free_sched_group(tg);
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008059 return ERR_PTR(-ENOMEM);
8060}
8061
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02008062/* rcu callback to free various structures associated with a task group */
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008063static void free_sched_group_rcu(struct rcu_head *rhp)
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008064{
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008065 /* now it should be safe to free those cfs_rqs */
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008066 free_sched_group(container_of(rhp, struct task_group, rcu));
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008067}
8068
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02008069/* Destroy runqueue etc associated with a task group */
Ingo Molnar4cf86d72007-10-15 17:00:14 +02008070void sched_destroy_group(struct task_group *tg)
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008071{
Peter Zijlstra8ed36992008-02-13 15:45:39 +01008072 unsigned long flags;
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02008073 int i;
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008074
Peter Zijlstra8ed36992008-02-13 15:45:39 +01008075 spin_lock_irqsave(&task_group_lock, flags);
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02008076 for_each_possible_cpu(i) {
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008077 unregister_fair_sched_group(tg, i);
8078 unregister_rt_sched_group(tg, i);
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02008079 }
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008080 list_del_rcu(&tg->list);
Peter Zijlstraf473aa52008-04-19 19:45:00 +02008081 list_del_rcu(&tg->siblings);
Peter Zijlstra8ed36992008-02-13 15:45:39 +01008082 spin_unlock_irqrestore(&task_group_lock, flags);
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02008083
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02008084 /* wait for possible concurrent references to cfs_rqs complete */
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008085 call_rcu(&tg->rcu, free_sched_group_rcu);
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008086}
8087
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02008088/* change task's runqueue when it moves between groups.
Ingo Molnar3a252012007-10-15 17:00:12 +02008089 * The caller of this function should have put the task in its new group
8090 * by now. This function just updates tsk->se.cfs_rq and tsk->se.parent to
8091 * reflect its new group.
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02008092 */
8093void sched_move_task(struct task_struct *tsk)
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008094{
8095 int on_rq, running;
8096 unsigned long flags;
8097 struct rq *rq;
8098
8099 rq = task_rq_lock(tsk, &flags);
8100
Dmitry Adamushko051a1d12007-12-18 15:21:13 +01008101 running = task_current(rq, tsk);
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008102 on_rq = tsk->se.on_rq;
8103
Hiroshi Shimamoto0e1f3482008-03-10 11:01:20 -07008104 if (on_rq)
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008105 dequeue_task(rq, tsk, 0);
Hiroshi Shimamoto0e1f3482008-03-10 11:01:20 -07008106 if (unlikely(running))
8107 tsk->sched_class->put_prev_task(rq, tsk);
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008108
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008109 set_task_rq(tsk, task_cpu(tsk));
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008110
Peter Zijlstra810b3812008-02-29 15:21:01 -05008111#ifdef CONFIG_FAIR_GROUP_SCHED
8112 if (tsk->sched_class->moved_group)
Peter Zijlstra88ec22d2009-12-16 18:04:41 +01008113 tsk->sched_class->moved_group(tsk, on_rq);
Peter Zijlstra810b3812008-02-29 15:21:01 -05008114#endif
8115
Hiroshi Shimamoto0e1f3482008-03-10 11:01:20 -07008116 if (unlikely(running))
8117 tsk->sched_class->set_curr_task(rq);
8118 if (on_rq)
Peter Zijlstra371fd7e2010-03-24 16:38:48 +01008119 enqueue_task(rq, tsk, 0);
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008120
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008121 task_rq_unlock(rq, &flags);
8122}
Dhaval Giani7c941432010-01-20 13:26:18 +01008123#endif /* CONFIG_CGROUP_SCHED */
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008124
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01008125#ifdef CONFIG_FAIR_GROUP_SCHED
Peter Zijlstrac09595f2008-06-27 13:41:14 +02008126static void __set_se_shares(struct sched_entity *se, unsigned long shares)
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008127{
8128 struct cfs_rq *cfs_rq = se->cfs_rq;
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008129 int on_rq;
8130
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008131 on_rq = se->on_rq;
Peter Zijlstra62fb1852008-02-25 17:34:02 +01008132 if (on_rq)
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008133 dequeue_entity(cfs_rq, se, 0);
8134
8135 se->load.weight = shares;
Peter Zijlstrae05510d2008-05-05 23:56:17 +02008136 se->load.inv_weight = 0;
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008137
Peter Zijlstra62fb1852008-02-25 17:34:02 +01008138 if (on_rq)
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008139 enqueue_entity(cfs_rq, se, 0);
Peter Zijlstrac09595f2008-06-27 13:41:14 +02008140}
Peter Zijlstra62fb1852008-02-25 17:34:02 +01008141
Peter Zijlstrac09595f2008-06-27 13:41:14 +02008142static void set_se_shares(struct sched_entity *se, unsigned long shares)
8143{
8144 struct cfs_rq *cfs_rq = se->cfs_rq;
8145 struct rq *rq = cfs_rq->rq;
8146 unsigned long flags;
8147
Thomas Gleixner05fa7852009-11-17 14:28:38 +01008148 raw_spin_lock_irqsave(&rq->lock, flags);
Peter Zijlstrac09595f2008-06-27 13:41:14 +02008149 __set_se_shares(se, shares);
Thomas Gleixner05fa7852009-11-17 14:28:38 +01008150 raw_spin_unlock_irqrestore(&rq->lock, flags);
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008151}
8152
Peter Zijlstra8ed36992008-02-13 15:45:39 +01008153static DEFINE_MUTEX(shares_mutex);
8154
Ingo Molnar4cf86d72007-10-15 17:00:14 +02008155int sched_group_set_shares(struct task_group *tg, unsigned long shares)
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008156{
8157 int i;
Peter Zijlstra8ed36992008-02-13 15:45:39 +01008158 unsigned long flags;
Ingo Molnarc61935f2008-01-22 11:24:58 +01008159
Peter Zijlstra62fb1852008-02-25 17:34:02 +01008160 /*
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008161 * We can't change the weight of the root cgroup.
8162 */
8163 if (!tg->se[0])
8164 return -EINVAL;
8165
Peter Zijlstra18d95a22008-04-19 19:45:00 +02008166 if (shares < MIN_SHARES)
8167 shares = MIN_SHARES;
Miao Xiecb4ad1f2008-04-28 12:54:56 +08008168 else if (shares > MAX_SHARES)
8169 shares = MAX_SHARES;
Peter Zijlstra62fb1852008-02-25 17:34:02 +01008170
Peter Zijlstra8ed36992008-02-13 15:45:39 +01008171 mutex_lock(&shares_mutex);
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02008172 if (tg->shares == shares)
Dhaval Giani5cb350b2007-10-15 17:00:14 +02008173 goto done;
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008174
Peter Zijlstra8ed36992008-02-13 15:45:39 +01008175 spin_lock_irqsave(&task_group_lock, flags);
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008176 for_each_possible_cpu(i)
8177 unregister_fair_sched_group(tg, i);
Peter Zijlstraf473aa52008-04-19 19:45:00 +02008178 list_del_rcu(&tg->siblings);
Peter Zijlstra8ed36992008-02-13 15:45:39 +01008179 spin_unlock_irqrestore(&task_group_lock, flags);
Srivatsa Vaddagiri6b2d7702008-01-25 21:08:00 +01008180
8181 /* wait for any ongoing reference to this group to finish */
8182 synchronize_sched();
8183
8184 /*
8185 * Now we are free to modify the group's share on each cpu
8186 * w/o tripping rebalance_share or load_balance_fair.
8187 */
8188 tg->shares = shares;
Peter Zijlstrac09595f2008-06-27 13:41:14 +02008189 for_each_possible_cpu(i) {
8190 /*
8191 * force a rebalance
8192 */
8193 cfs_rq_set_shares(tg->cfs_rq[i], 0);
Miao Xiecb4ad1f2008-04-28 12:54:56 +08008194 set_se_shares(tg->se[i], shares);
Peter Zijlstrac09595f2008-06-27 13:41:14 +02008195 }
Srivatsa Vaddagiri6b2d7702008-01-25 21:08:00 +01008196
8197 /*
8198 * Enable load balance activity on this group, by inserting it back on
8199 * each cpu's rq->leaf_cfs_rq_list.
8200 */
Peter Zijlstra8ed36992008-02-13 15:45:39 +01008201 spin_lock_irqsave(&task_group_lock, flags);
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008202 for_each_possible_cpu(i)
8203 register_fair_sched_group(tg, i);
Peter Zijlstraf473aa52008-04-19 19:45:00 +02008204 list_add_rcu(&tg->siblings, &tg->parent->children);
Peter Zijlstra8ed36992008-02-13 15:45:39 +01008205 spin_unlock_irqrestore(&task_group_lock, flags);
Dhaval Giani5cb350b2007-10-15 17:00:14 +02008206done:
Peter Zijlstra8ed36992008-02-13 15:45:39 +01008207 mutex_unlock(&shares_mutex);
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02008208 return 0;
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008209}
8210
Dhaval Giani5cb350b2007-10-15 17:00:14 +02008211unsigned long sched_group_shares(struct task_group *tg)
8212{
8213 return tg->shares;
8214}
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01008215#endif
Dhaval Giani5cb350b2007-10-15 17:00:14 +02008216
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01008217#ifdef CONFIG_RT_GROUP_SCHED
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008218/*
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01008219 * Ensure that the real time constraints are schedulable.
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008220 */
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01008221static DEFINE_MUTEX(rt_constraints_mutex);
8222
8223static unsigned long to_ratio(u64 period, u64 runtime)
8224{
8225 if (runtime == RUNTIME_INF)
Peter Zijlstra9a7e0b12008-08-19 12:33:06 +02008226 return 1ULL << 20;
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01008227
Peter Zijlstra9a7e0b12008-08-19 12:33:06 +02008228 return div64_u64(runtime << 20, period);
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01008229}
8230
Dhaval Giani521f1a242008-02-28 15:21:56 +05308231/* Must be called with tasklist_lock held */
8232static inline int tg_has_rt_tasks(struct task_group *tg)
8233{
8234 struct task_struct *g, *p;
Peter Zijlstra9a7e0b12008-08-19 12:33:06 +02008235
Dhaval Giani521f1a242008-02-28 15:21:56 +05308236 do_each_thread(g, p) {
8237 if (rt_task(p) && rt_rq_of_se(&p->rt)->tg == tg)
8238 return 1;
8239 } while_each_thread(g, p);
Peter Zijlstra9a7e0b12008-08-19 12:33:06 +02008240
Dhaval Giani521f1a242008-02-28 15:21:56 +05308241 return 0;
8242}
8243
Peter Zijlstra9a7e0b12008-08-19 12:33:06 +02008244struct rt_schedulable_data {
8245 struct task_group *tg;
8246 u64 rt_period;
8247 u64 rt_runtime;
8248};
8249
8250static int tg_schedulable(struct task_group *tg, void *data)
8251{
8252 struct rt_schedulable_data *d = data;
8253 struct task_group *child;
8254 unsigned long total, sum = 0;
8255 u64 period, runtime;
8256
8257 period = ktime_to_ns(tg->rt_bandwidth.rt_period);
8258 runtime = tg->rt_bandwidth.rt_runtime;
8259
8260 if (tg == d->tg) {
8261 period = d->rt_period;
8262 runtime = d->rt_runtime;
8263 }
8264
Peter Zijlstra4653f802008-09-23 15:33:44 +02008265 /*
8266 * Cannot have more runtime than the period.
8267 */
8268 if (runtime > period && runtime != RUNTIME_INF)
8269 return -EINVAL;
8270
8271 /*
8272 * Ensure we don't starve existing RT tasks.
8273 */
Peter Zijlstra9a7e0b12008-08-19 12:33:06 +02008274 if (rt_bandwidth_enabled() && !runtime && tg_has_rt_tasks(tg))
8275 return -EBUSY;
8276
8277 total = to_ratio(period, runtime);
8278
Peter Zijlstra4653f802008-09-23 15:33:44 +02008279 /*
8280 * Nobody can have more than the global setting allows.
8281 */
8282 if (total > to_ratio(global_rt_period(), global_rt_runtime()))
8283 return -EINVAL;
8284
8285 /*
8286 * The sum of our children's runtime should not exceed our own.
8287 */
Peter Zijlstra9a7e0b12008-08-19 12:33:06 +02008288 list_for_each_entry_rcu(child, &tg->children, siblings) {
8289 period = ktime_to_ns(child->rt_bandwidth.rt_period);
8290 runtime = child->rt_bandwidth.rt_runtime;
8291
8292 if (child == d->tg) {
8293 period = d->rt_period;
8294 runtime = d->rt_runtime;
8295 }
8296
8297 sum += to_ratio(period, runtime);
8298 }
8299
8300 if (sum > total)
8301 return -EINVAL;
8302
8303 return 0;
8304}
8305
8306static int __rt_schedulable(struct task_group *tg, u64 period, u64 runtime)
8307{
8308 struct rt_schedulable_data data = {
8309 .tg = tg,
8310 .rt_period = period,
8311 .rt_runtime = runtime,
8312 };
8313
8314 return walk_tg_tree(tg_schedulable, tg_nop, &data);
8315}
8316
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008317static int tg_set_bandwidth(struct task_group *tg,
8318 u64 rt_period, u64 rt_runtime)
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008319{
Peter Zijlstraac086bc2008-04-19 19:44:58 +02008320 int i, err = 0;
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01008321
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01008322 mutex_lock(&rt_constraints_mutex);
Dhaval Giani521f1a242008-02-28 15:21:56 +05308323 read_lock(&tasklist_lock);
Peter Zijlstra9a7e0b12008-08-19 12:33:06 +02008324 err = __rt_schedulable(tg, rt_period, rt_runtime);
8325 if (err)
Dhaval Giani521f1a242008-02-28 15:21:56 +05308326 goto unlock;
Peter Zijlstraac086bc2008-04-19 19:44:58 +02008327
Thomas Gleixner0986b112009-11-17 15:32:06 +01008328 raw_spin_lock_irq(&tg->rt_bandwidth.rt_runtime_lock);
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008329 tg->rt_bandwidth.rt_period = ns_to_ktime(rt_period);
8330 tg->rt_bandwidth.rt_runtime = rt_runtime;
Peter Zijlstraac086bc2008-04-19 19:44:58 +02008331
8332 for_each_possible_cpu(i) {
8333 struct rt_rq *rt_rq = tg->rt_rq[i];
8334
Thomas Gleixner0986b112009-11-17 15:32:06 +01008335 raw_spin_lock(&rt_rq->rt_runtime_lock);
Peter Zijlstraac086bc2008-04-19 19:44:58 +02008336 rt_rq->rt_runtime = rt_runtime;
Thomas Gleixner0986b112009-11-17 15:32:06 +01008337 raw_spin_unlock(&rt_rq->rt_runtime_lock);
Peter Zijlstraac086bc2008-04-19 19:44:58 +02008338 }
Thomas Gleixner0986b112009-11-17 15:32:06 +01008339 raw_spin_unlock_irq(&tg->rt_bandwidth.rt_runtime_lock);
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01008340 unlock:
Dhaval Giani521f1a242008-02-28 15:21:56 +05308341 read_unlock(&tasklist_lock);
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01008342 mutex_unlock(&rt_constraints_mutex);
8343
8344 return err;
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008345}
8346
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008347int sched_group_set_rt_runtime(struct task_group *tg, long rt_runtime_us)
8348{
8349 u64 rt_runtime, rt_period;
8350
8351 rt_period = ktime_to_ns(tg->rt_bandwidth.rt_period);
8352 rt_runtime = (u64)rt_runtime_us * NSEC_PER_USEC;
8353 if (rt_runtime_us < 0)
8354 rt_runtime = RUNTIME_INF;
8355
8356 return tg_set_bandwidth(tg, rt_period, rt_runtime);
8357}
8358
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01008359long sched_group_rt_runtime(struct task_group *tg)
8360{
8361 u64 rt_runtime_us;
8362
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008363 if (tg->rt_bandwidth.rt_runtime == RUNTIME_INF)
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01008364 return -1;
8365
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008366 rt_runtime_us = tg->rt_bandwidth.rt_runtime;
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01008367 do_div(rt_runtime_us, NSEC_PER_USEC);
8368 return rt_runtime_us;
8369}
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008370
8371int sched_group_set_rt_period(struct task_group *tg, long rt_period_us)
8372{
8373 u64 rt_runtime, rt_period;
8374
8375 rt_period = (u64)rt_period_us * NSEC_PER_USEC;
8376 rt_runtime = tg->rt_bandwidth.rt_runtime;
8377
Raistlin619b0482008-06-26 18:54:09 +02008378 if (rt_period == 0)
8379 return -EINVAL;
8380
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008381 return tg_set_bandwidth(tg, rt_period, rt_runtime);
8382}
8383
8384long sched_group_rt_period(struct task_group *tg)
8385{
8386 u64 rt_period_us;
8387
8388 rt_period_us = ktime_to_ns(tg->rt_bandwidth.rt_period);
8389 do_div(rt_period_us, NSEC_PER_USEC);
8390 return rt_period_us;
8391}
8392
8393static int sched_rt_global_constraints(void)
8394{
Peter Zijlstra4653f802008-09-23 15:33:44 +02008395 u64 runtime, period;
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008396 int ret = 0;
8397
Hiroshi Shimamotoec5d4982008-09-10 17:00:19 -07008398 if (sysctl_sched_rt_period <= 0)
8399 return -EINVAL;
8400
Peter Zijlstra4653f802008-09-23 15:33:44 +02008401 runtime = global_rt_runtime();
8402 period = global_rt_period();
8403
8404 /*
8405 * Sanity check on the sysctl variables.
8406 */
8407 if (runtime > period && runtime != RUNTIME_INF)
8408 return -EINVAL;
Peter Zijlstra10b612f2008-06-19 14:22:27 +02008409
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008410 mutex_lock(&rt_constraints_mutex);
Peter Zijlstra9a7e0b12008-08-19 12:33:06 +02008411 read_lock(&tasklist_lock);
Peter Zijlstra4653f802008-09-23 15:33:44 +02008412 ret = __rt_schedulable(NULL, 0, 0);
Peter Zijlstra9a7e0b12008-08-19 12:33:06 +02008413 read_unlock(&tasklist_lock);
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008414 mutex_unlock(&rt_constraints_mutex);
8415
8416 return ret;
8417}
Dhaval Giani54e99122009-02-27 15:13:54 +05308418
8419int sched_rt_can_attach(struct task_group *tg, struct task_struct *tsk)
8420{
8421 /* Don't accept realtime tasks when there is no way for them to run */
8422 if (rt_task(tsk) && tg->rt_bandwidth.rt_runtime == 0)
8423 return 0;
8424
8425 return 1;
8426}
8427
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +02008428#else /* !CONFIG_RT_GROUP_SCHED */
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008429static int sched_rt_global_constraints(void)
8430{
Peter Zijlstraac086bc2008-04-19 19:44:58 +02008431 unsigned long flags;
8432 int i;
8433
Hiroshi Shimamotoec5d4982008-09-10 17:00:19 -07008434 if (sysctl_sched_rt_period <= 0)
8435 return -EINVAL;
8436
Peter Zijlstra60aa6052009-05-05 17:50:21 +02008437 /*
8438 * There's always some RT tasks in the root group
8439 * -- migration, kstopmachine etc..
8440 */
8441 if (sysctl_sched_rt_runtime == 0)
8442 return -EBUSY;
8443
Thomas Gleixner0986b112009-11-17 15:32:06 +01008444 raw_spin_lock_irqsave(&def_rt_bandwidth.rt_runtime_lock, flags);
Peter Zijlstraac086bc2008-04-19 19:44:58 +02008445 for_each_possible_cpu(i) {
8446 struct rt_rq *rt_rq = &cpu_rq(i)->rt;
8447
Thomas Gleixner0986b112009-11-17 15:32:06 +01008448 raw_spin_lock(&rt_rq->rt_runtime_lock);
Peter Zijlstraac086bc2008-04-19 19:44:58 +02008449 rt_rq->rt_runtime = global_rt_runtime();
Thomas Gleixner0986b112009-11-17 15:32:06 +01008450 raw_spin_unlock(&rt_rq->rt_runtime_lock);
Peter Zijlstraac086bc2008-04-19 19:44:58 +02008451 }
Thomas Gleixner0986b112009-11-17 15:32:06 +01008452 raw_spin_unlock_irqrestore(&def_rt_bandwidth.rt_runtime_lock, flags);
Peter Zijlstraac086bc2008-04-19 19:44:58 +02008453
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008454 return 0;
8455}
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +02008456#endif /* CONFIG_RT_GROUP_SCHED */
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008457
8458int sched_rt_handler(struct ctl_table *table, int write,
Alexey Dobriyan8d65af72009-09-23 15:57:19 -07008459 void __user *buffer, size_t *lenp,
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008460 loff_t *ppos)
8461{
8462 int ret;
8463 int old_period, old_runtime;
8464 static DEFINE_MUTEX(mutex);
8465
8466 mutex_lock(&mutex);
8467 old_period = sysctl_sched_rt_period;
8468 old_runtime = sysctl_sched_rt_runtime;
8469
Alexey Dobriyan8d65af72009-09-23 15:57:19 -07008470 ret = proc_dointvec(table, write, buffer, lenp, ppos);
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008471
8472 if (!ret && write) {
8473 ret = sched_rt_global_constraints();
8474 if (ret) {
8475 sysctl_sched_rt_period = old_period;
8476 sysctl_sched_rt_runtime = old_runtime;
8477 } else {
8478 def_rt_bandwidth.rt_runtime = global_rt_runtime();
8479 def_rt_bandwidth.rt_period =
8480 ns_to_ktime(global_rt_period());
8481 }
8482 }
8483 mutex_unlock(&mutex);
8484
8485 return ret;
8486}
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008487
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01008488#ifdef CONFIG_CGROUP_SCHED
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008489
8490/* return corresponding task_group object of a cgroup */
Paul Menage2b01dfe2007-10-24 18:23:50 +02008491static inline struct task_group *cgroup_tg(struct cgroup *cgrp)
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008492{
Paul Menage2b01dfe2007-10-24 18:23:50 +02008493 return container_of(cgroup_subsys_state(cgrp, cpu_cgroup_subsys_id),
8494 struct task_group, css);
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008495}
8496
8497static struct cgroup_subsys_state *
Paul Menage2b01dfe2007-10-24 18:23:50 +02008498cpu_cgroup_create(struct cgroup_subsys *ss, struct cgroup *cgrp)
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008499{
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008500 struct task_group *tg, *parent;
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008501
Paul Menage2b01dfe2007-10-24 18:23:50 +02008502 if (!cgrp->parent) {
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008503 /* This is early initialization for the top cgroup */
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008504 return &init_task_group.css;
8505 }
8506
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008507 parent = cgroup_tg(cgrp->parent);
8508 tg = sched_create_group(parent);
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008509 if (IS_ERR(tg))
8510 return ERR_PTR(-ENOMEM);
8511
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008512 return &tg->css;
8513}
8514
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01008515static void
8516cpu_cgroup_destroy(struct cgroup_subsys *ss, struct cgroup *cgrp)
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008517{
Paul Menage2b01dfe2007-10-24 18:23:50 +02008518 struct task_group *tg = cgroup_tg(cgrp);
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008519
8520 sched_destroy_group(tg);
8521}
8522
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01008523static int
Ben Blumbe367d02009-09-23 15:56:31 -07008524cpu_cgroup_can_attach_task(struct cgroup *cgrp, struct task_struct *tsk)
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008525{
Peter Zijlstrab68aa232008-02-13 15:45:40 +01008526#ifdef CONFIG_RT_GROUP_SCHED
Dhaval Giani54e99122009-02-27 15:13:54 +05308527 if (!sched_rt_can_attach(cgroup_tg(cgrp), tsk))
Peter Zijlstrab68aa232008-02-13 15:45:40 +01008528 return -EINVAL;
8529#else
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008530 /* We don't support RT-tasks being in separate groups */
8531 if (tsk->sched_class != &fair_sched_class)
8532 return -EINVAL;
Peter Zijlstrab68aa232008-02-13 15:45:40 +01008533#endif
Ben Blumbe367d02009-09-23 15:56:31 -07008534 return 0;
8535}
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008536
Ben Blumbe367d02009-09-23 15:56:31 -07008537static int
8538cpu_cgroup_can_attach(struct cgroup_subsys *ss, struct cgroup *cgrp,
8539 struct task_struct *tsk, bool threadgroup)
8540{
8541 int retval = cpu_cgroup_can_attach_task(cgrp, tsk);
8542 if (retval)
8543 return retval;
8544 if (threadgroup) {
8545 struct task_struct *c;
8546 rcu_read_lock();
8547 list_for_each_entry_rcu(c, &tsk->thread_group, thread_group) {
8548 retval = cpu_cgroup_can_attach_task(cgrp, c);
8549 if (retval) {
8550 rcu_read_unlock();
8551 return retval;
8552 }
8553 }
8554 rcu_read_unlock();
8555 }
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008556 return 0;
8557}
8558
8559static void
Paul Menage2b01dfe2007-10-24 18:23:50 +02008560cpu_cgroup_attach(struct cgroup_subsys *ss, struct cgroup *cgrp,
Ben Blumbe367d02009-09-23 15:56:31 -07008561 struct cgroup *old_cont, struct task_struct *tsk,
8562 bool threadgroup)
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008563{
8564 sched_move_task(tsk);
Ben Blumbe367d02009-09-23 15:56:31 -07008565 if (threadgroup) {
8566 struct task_struct *c;
8567 rcu_read_lock();
8568 list_for_each_entry_rcu(c, &tsk->thread_group, thread_group) {
8569 sched_move_task(c);
8570 }
8571 rcu_read_unlock();
8572 }
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008573}
8574
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01008575#ifdef CONFIG_FAIR_GROUP_SCHED
Paul Menagef4c753b2008-04-29 00:59:56 -07008576static int cpu_shares_write_u64(struct cgroup *cgrp, struct cftype *cftype,
Paul Menage2b01dfe2007-10-24 18:23:50 +02008577 u64 shareval)
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008578{
Paul Menage2b01dfe2007-10-24 18:23:50 +02008579 return sched_group_set_shares(cgroup_tg(cgrp), shareval);
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008580}
8581
Paul Menagef4c753b2008-04-29 00:59:56 -07008582static u64 cpu_shares_read_u64(struct cgroup *cgrp, struct cftype *cft)
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008583{
Paul Menage2b01dfe2007-10-24 18:23:50 +02008584 struct task_group *tg = cgroup_tg(cgrp);
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008585
8586 return (u64) tg->shares;
8587}
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +02008588#endif /* CONFIG_FAIR_GROUP_SCHED */
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008589
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01008590#ifdef CONFIG_RT_GROUP_SCHED
Mirco Tischler0c708142008-05-14 16:05:46 -07008591static int cpu_rt_runtime_write(struct cgroup *cgrp, struct cftype *cft,
Paul Menage06ecb272008-04-29 01:00:06 -07008592 s64 val)
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008593{
Paul Menage06ecb272008-04-29 01:00:06 -07008594 return sched_group_set_rt_runtime(cgroup_tg(cgrp), val);
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008595}
8596
Paul Menage06ecb272008-04-29 01:00:06 -07008597static s64 cpu_rt_runtime_read(struct cgroup *cgrp, struct cftype *cft)
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008598{
Paul Menage06ecb272008-04-29 01:00:06 -07008599 return sched_group_rt_runtime(cgroup_tg(cgrp));
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008600}
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008601
8602static int cpu_rt_period_write_uint(struct cgroup *cgrp, struct cftype *cftype,
8603 u64 rt_period_us)
8604{
8605 return sched_group_set_rt_period(cgroup_tg(cgrp), rt_period_us);
8606}
8607
8608static u64 cpu_rt_period_read_uint(struct cgroup *cgrp, struct cftype *cft)
8609{
8610 return sched_group_rt_period(cgroup_tg(cgrp));
8611}
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +02008612#endif /* CONFIG_RT_GROUP_SCHED */
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008613
Paul Menagefe5c7cc2007-10-29 21:18:11 +01008614static struct cftype cpu_files[] = {
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01008615#ifdef CONFIG_FAIR_GROUP_SCHED
Paul Menagefe5c7cc2007-10-29 21:18:11 +01008616 {
8617 .name = "shares",
Paul Menagef4c753b2008-04-29 00:59:56 -07008618 .read_u64 = cpu_shares_read_u64,
8619 .write_u64 = cpu_shares_write_u64,
Paul Menagefe5c7cc2007-10-29 21:18:11 +01008620 },
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01008621#endif
8622#ifdef CONFIG_RT_GROUP_SCHED
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008623 {
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01008624 .name = "rt_runtime_us",
Paul Menage06ecb272008-04-29 01:00:06 -07008625 .read_s64 = cpu_rt_runtime_read,
8626 .write_s64 = cpu_rt_runtime_write,
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008627 },
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008628 {
8629 .name = "rt_period_us",
Paul Menagef4c753b2008-04-29 00:59:56 -07008630 .read_u64 = cpu_rt_period_read_uint,
8631 .write_u64 = cpu_rt_period_write_uint,
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008632 },
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01008633#endif
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008634};
8635
8636static int cpu_cgroup_populate(struct cgroup_subsys *ss, struct cgroup *cont)
8637{
Paul Menagefe5c7cc2007-10-29 21:18:11 +01008638 return cgroup_add_files(cont, ss, cpu_files, ARRAY_SIZE(cpu_files));
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008639}
8640
8641struct cgroup_subsys cpu_cgroup_subsys = {
Ingo Molnar38605ca2007-10-29 21:18:11 +01008642 .name = "cpu",
8643 .create = cpu_cgroup_create,
8644 .destroy = cpu_cgroup_destroy,
8645 .can_attach = cpu_cgroup_can_attach,
8646 .attach = cpu_cgroup_attach,
8647 .populate = cpu_cgroup_populate,
8648 .subsys_id = cpu_cgroup_subsys_id,
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008649 .early_init = 1,
8650};
8651
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01008652#endif /* CONFIG_CGROUP_SCHED */
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01008653
8654#ifdef CONFIG_CGROUP_CPUACCT
8655
8656/*
8657 * CPU accounting code for task groups.
8658 *
8659 * Based on the work by Paul Menage (menage@google.com) and Balbir Singh
8660 * (balbir@in.ibm.com).
8661 */
8662
Bharata B Rao934352f2008-11-10 20:41:13 +05308663/* track cpu usage of a group of tasks and its child groups */
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01008664struct cpuacct {
8665 struct cgroup_subsys_state css;
8666 /* cpuusage holds pointer to a u64-type object on every cpu */
Tejun Heo43cf38e2010-02-02 14:38:57 +09008667 u64 __percpu *cpuusage;
Bharata B Raoef12fef2009-03-31 10:02:22 +05308668 struct percpu_counter cpustat[CPUACCT_STAT_NSTATS];
Bharata B Rao934352f2008-11-10 20:41:13 +05308669 struct cpuacct *parent;
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01008670};
8671
8672struct cgroup_subsys cpuacct_subsys;
8673
8674/* return cpu accounting group corresponding to this container */
Dhaval Giani32cd7562008-02-29 10:02:43 +05308675static inline struct cpuacct *cgroup_ca(struct cgroup *cgrp)
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01008676{
Dhaval Giani32cd7562008-02-29 10:02:43 +05308677 return container_of(cgroup_subsys_state(cgrp, cpuacct_subsys_id),
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01008678 struct cpuacct, css);
8679}
8680
8681/* return cpu accounting group to which this task belongs */
8682static inline struct cpuacct *task_ca(struct task_struct *tsk)
8683{
8684 return container_of(task_subsys_state(tsk, cpuacct_subsys_id),
8685 struct cpuacct, css);
8686}
8687
8688/* create a new cpu accounting group */
8689static struct cgroup_subsys_state *cpuacct_create(
Dhaval Giani32cd7562008-02-29 10:02:43 +05308690 struct cgroup_subsys *ss, struct cgroup *cgrp)
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01008691{
8692 struct cpuacct *ca = kzalloc(sizeof(*ca), GFP_KERNEL);
Bharata B Raoef12fef2009-03-31 10:02:22 +05308693 int i;
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01008694
8695 if (!ca)
Bharata B Raoef12fef2009-03-31 10:02:22 +05308696 goto out;
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01008697
8698 ca->cpuusage = alloc_percpu(u64);
Bharata B Raoef12fef2009-03-31 10:02:22 +05308699 if (!ca->cpuusage)
8700 goto out_free_ca;
8701
8702 for (i = 0; i < CPUACCT_STAT_NSTATS; i++)
8703 if (percpu_counter_init(&ca->cpustat[i], 0))
8704 goto out_free_counters;
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01008705
Bharata B Rao934352f2008-11-10 20:41:13 +05308706 if (cgrp->parent)
8707 ca->parent = cgroup_ca(cgrp->parent);
8708
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01008709 return &ca->css;
Bharata B Raoef12fef2009-03-31 10:02:22 +05308710
8711out_free_counters:
8712 while (--i >= 0)
8713 percpu_counter_destroy(&ca->cpustat[i]);
8714 free_percpu(ca->cpuusage);
8715out_free_ca:
8716 kfree(ca);
8717out:
8718 return ERR_PTR(-ENOMEM);
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01008719}
8720
8721/* destroy an existing cpu accounting group */
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01008722static void
Dhaval Giani32cd7562008-02-29 10:02:43 +05308723cpuacct_destroy(struct cgroup_subsys *ss, struct cgroup *cgrp)
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01008724{
Dhaval Giani32cd7562008-02-29 10:02:43 +05308725 struct cpuacct *ca = cgroup_ca(cgrp);
Bharata B Raoef12fef2009-03-31 10:02:22 +05308726 int i;
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01008727
Bharata B Raoef12fef2009-03-31 10:02:22 +05308728 for (i = 0; i < CPUACCT_STAT_NSTATS; i++)
8729 percpu_counter_destroy(&ca->cpustat[i]);
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01008730 free_percpu(ca->cpuusage);
8731 kfree(ca);
8732}
8733
Ken Chen720f5492008-12-15 22:02:01 -08008734static u64 cpuacct_cpuusage_read(struct cpuacct *ca, int cpu)
8735{
Rusty Russellb36128c2009-02-20 16:29:08 +09008736 u64 *cpuusage = per_cpu_ptr(ca->cpuusage, cpu);
Ken Chen720f5492008-12-15 22:02:01 -08008737 u64 data;
8738
8739#ifndef CONFIG_64BIT
8740 /*
8741 * Take rq->lock to make 64-bit read safe on 32-bit platforms.
8742 */
Thomas Gleixner05fa7852009-11-17 14:28:38 +01008743 raw_spin_lock_irq(&cpu_rq(cpu)->lock);
Ken Chen720f5492008-12-15 22:02:01 -08008744 data = *cpuusage;
Thomas Gleixner05fa7852009-11-17 14:28:38 +01008745 raw_spin_unlock_irq(&cpu_rq(cpu)->lock);
Ken Chen720f5492008-12-15 22:02:01 -08008746#else
8747 data = *cpuusage;
8748#endif
8749
8750 return data;
8751}
8752
8753static void cpuacct_cpuusage_write(struct cpuacct *ca, int cpu, u64 val)
8754{
Rusty Russellb36128c2009-02-20 16:29:08 +09008755 u64 *cpuusage = per_cpu_ptr(ca->cpuusage, cpu);
Ken Chen720f5492008-12-15 22:02:01 -08008756
8757#ifndef CONFIG_64BIT
8758 /*
8759 * Take rq->lock to make 64-bit write safe on 32-bit platforms.
8760 */
Thomas Gleixner05fa7852009-11-17 14:28:38 +01008761 raw_spin_lock_irq(&cpu_rq(cpu)->lock);
Ken Chen720f5492008-12-15 22:02:01 -08008762 *cpuusage = val;
Thomas Gleixner05fa7852009-11-17 14:28:38 +01008763 raw_spin_unlock_irq(&cpu_rq(cpu)->lock);
Ken Chen720f5492008-12-15 22:02:01 -08008764#else
8765 *cpuusage = val;
8766#endif
8767}
8768
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01008769/* return total cpu usage (in nanoseconds) of a group */
Dhaval Giani32cd7562008-02-29 10:02:43 +05308770static u64 cpuusage_read(struct cgroup *cgrp, struct cftype *cft)
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01008771{
Dhaval Giani32cd7562008-02-29 10:02:43 +05308772 struct cpuacct *ca = cgroup_ca(cgrp);
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01008773 u64 totalcpuusage = 0;
8774 int i;
8775
Ken Chen720f5492008-12-15 22:02:01 -08008776 for_each_present_cpu(i)
8777 totalcpuusage += cpuacct_cpuusage_read(ca, i);
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01008778
8779 return totalcpuusage;
8780}
8781
Dhaval Giani0297b802008-02-29 10:02:44 +05308782static int cpuusage_write(struct cgroup *cgrp, struct cftype *cftype,
8783 u64 reset)
8784{
8785 struct cpuacct *ca = cgroup_ca(cgrp);
8786 int err = 0;
8787 int i;
8788
8789 if (reset) {
8790 err = -EINVAL;
8791 goto out;
8792 }
8793
Ken Chen720f5492008-12-15 22:02:01 -08008794 for_each_present_cpu(i)
8795 cpuacct_cpuusage_write(ca, i, 0);
Dhaval Giani0297b802008-02-29 10:02:44 +05308796
Dhaval Giani0297b802008-02-29 10:02:44 +05308797out:
8798 return err;
8799}
8800
Ken Chene9515c32008-12-15 22:04:15 -08008801static int cpuacct_percpu_seq_read(struct cgroup *cgroup, struct cftype *cft,
8802 struct seq_file *m)
8803{
8804 struct cpuacct *ca = cgroup_ca(cgroup);
8805 u64 percpu;
8806 int i;
8807
8808 for_each_present_cpu(i) {
8809 percpu = cpuacct_cpuusage_read(ca, i);
8810 seq_printf(m, "%llu ", (unsigned long long) percpu);
8811 }
8812 seq_printf(m, "\n");
8813 return 0;
8814}
8815
Bharata B Raoef12fef2009-03-31 10:02:22 +05308816static const char *cpuacct_stat_desc[] = {
8817 [CPUACCT_STAT_USER] = "user",
8818 [CPUACCT_STAT_SYSTEM] = "system",
8819};
8820
8821static int cpuacct_stats_show(struct cgroup *cgrp, struct cftype *cft,
8822 struct cgroup_map_cb *cb)
8823{
8824 struct cpuacct *ca = cgroup_ca(cgrp);
8825 int i;
8826
8827 for (i = 0; i < CPUACCT_STAT_NSTATS; i++) {
8828 s64 val = percpu_counter_read(&ca->cpustat[i]);
8829 val = cputime64_to_clock_t(val);
8830 cb->fill(cb, cpuacct_stat_desc[i], val);
8831 }
8832 return 0;
8833}
8834
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01008835static struct cftype files[] = {
8836 {
8837 .name = "usage",
Paul Menagef4c753b2008-04-29 00:59:56 -07008838 .read_u64 = cpuusage_read,
8839 .write_u64 = cpuusage_write,
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01008840 },
Ken Chene9515c32008-12-15 22:04:15 -08008841 {
8842 .name = "usage_percpu",
8843 .read_seq_string = cpuacct_percpu_seq_read,
8844 },
Bharata B Raoef12fef2009-03-31 10:02:22 +05308845 {
8846 .name = "stat",
8847 .read_map = cpuacct_stats_show,
8848 },
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01008849};
8850
Dhaval Giani32cd7562008-02-29 10:02:43 +05308851static int cpuacct_populate(struct cgroup_subsys *ss, struct cgroup *cgrp)
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01008852{
Dhaval Giani32cd7562008-02-29 10:02:43 +05308853 return cgroup_add_files(cgrp, ss, files, ARRAY_SIZE(files));
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01008854}
8855
8856/*
8857 * charge this task's execution time to its accounting group.
8858 *
8859 * called with rq->lock held.
8860 */
8861static void cpuacct_charge(struct task_struct *tsk, u64 cputime)
8862{
8863 struct cpuacct *ca;
Bharata B Rao934352f2008-11-10 20:41:13 +05308864 int cpu;
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01008865
Li Zefanc40c6f82009-02-26 15:40:15 +08008866 if (unlikely(!cpuacct_subsys.active))
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01008867 return;
8868
Bharata B Rao934352f2008-11-10 20:41:13 +05308869 cpu = task_cpu(tsk);
Bharata B Raoa18b83b2009-03-23 10:02:53 +05308870
8871 rcu_read_lock();
8872
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01008873 ca = task_ca(tsk);
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01008874
Bharata B Rao934352f2008-11-10 20:41:13 +05308875 for (; ca; ca = ca->parent) {
Rusty Russellb36128c2009-02-20 16:29:08 +09008876 u64 *cpuusage = per_cpu_ptr(ca->cpuusage, cpu);
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01008877 *cpuusage += cputime;
8878 }
Bharata B Raoa18b83b2009-03-23 10:02:53 +05308879
8880 rcu_read_unlock();
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01008881}
8882
Bharata B Raoef12fef2009-03-31 10:02:22 +05308883/*
Anton Blanchardfa535a72010-02-02 14:46:13 -08008884 * When CONFIG_VIRT_CPU_ACCOUNTING is enabled one jiffy can be very large
8885 * in cputime_t units. As a result, cpuacct_update_stats calls
8886 * percpu_counter_add with values large enough to always overflow the
8887 * per cpu batch limit causing bad SMP scalability.
8888 *
8889 * To fix this we scale percpu_counter_batch by cputime_one_jiffy so we
8890 * batch the same amount of time with CONFIG_VIRT_CPU_ACCOUNTING disabled
8891 * and enabled. We cap it at INT_MAX which is the largest allowed batch value.
8892 */
8893#ifdef CONFIG_SMP
8894#define CPUACCT_BATCH \
8895 min_t(long, percpu_counter_batch * cputime_one_jiffy, INT_MAX)
8896#else
8897#define CPUACCT_BATCH 0
8898#endif
8899
8900/*
Bharata B Raoef12fef2009-03-31 10:02:22 +05308901 * Charge the system/user time to the task's accounting group.
8902 */
8903static void cpuacct_update_stats(struct task_struct *tsk,
8904 enum cpuacct_stat_index idx, cputime_t val)
8905{
8906 struct cpuacct *ca;
Anton Blanchardfa535a72010-02-02 14:46:13 -08008907 int batch = CPUACCT_BATCH;
Bharata B Raoef12fef2009-03-31 10:02:22 +05308908
8909 if (unlikely(!cpuacct_subsys.active))
8910 return;
8911
8912 rcu_read_lock();
8913 ca = task_ca(tsk);
8914
8915 do {
Anton Blanchardfa535a72010-02-02 14:46:13 -08008916 __percpu_counter_add(&ca->cpustat[idx], val, batch);
Bharata B Raoef12fef2009-03-31 10:02:22 +05308917 ca = ca->parent;
8918 } while (ca);
8919 rcu_read_unlock();
8920}
8921
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01008922struct cgroup_subsys cpuacct_subsys = {
8923 .name = "cpuacct",
8924 .create = cpuacct_create,
8925 .destroy = cpuacct_destroy,
8926 .populate = cpuacct_populate,
8927 .subsys_id = cpuacct_subsys_id,
8928};
8929#endif /* CONFIG_CGROUP_CPUACCT */
Paul E. McKenney03b042b2009-06-25 09:08:16 -07008930
8931#ifndef CONFIG_SMP
8932
Paul E. McKenney03b042b2009-06-25 09:08:16 -07008933void synchronize_sched_expedited(void)
8934{
Paul E. McKenneyfc390cd2010-05-06 11:42:52 -07008935 barrier();
Paul E. McKenney03b042b2009-06-25 09:08:16 -07008936}
8937EXPORT_SYMBOL_GPL(synchronize_sched_expedited);
8938
8939#else /* #ifndef CONFIG_SMP */
8940
Paul E. McKenneycc631fb2010-05-06 18:49:21 +02008941static atomic_t synchronize_sched_expedited_count = ATOMIC_INIT(0);
Paul E. McKenney03b042b2009-06-25 09:08:16 -07008942
Paul E. McKenneycc631fb2010-05-06 18:49:21 +02008943static int synchronize_sched_expedited_cpu_stop(void *data)
Paul E. McKenney03b042b2009-06-25 09:08:16 -07008944{
Tejun Heo969c7922010-05-06 18:49:21 +02008945 /*
8946 * There must be a full memory barrier on each affected CPU
8947 * between the time that try_stop_cpus() is called and the
8948 * time that it returns.
8949 *
8950 * In the current initial implementation of cpu_stop, the
8951 * above condition is already met when the control reaches
8952 * this point and the following smp_mb() is not strictly
8953 * necessary. Do smp_mb() anyway for documentation and
8954 * robustness against future implementation changes.
8955 */
Paul E. McKenneycc631fb2010-05-06 18:49:21 +02008956 smp_mb(); /* See above comment block. */
Tejun Heo969c7922010-05-06 18:49:21 +02008957 return 0;
Paul E. McKenney03b042b2009-06-25 09:08:16 -07008958}
Paul E. McKenney03b042b2009-06-25 09:08:16 -07008959
8960/*
8961 * Wait for an rcu-sched grace period to elapse, but use "big hammer"
8962 * approach to force grace period to end quickly. This consumes
8963 * significant time on all CPUs, and is thus not recommended for
8964 * any sort of common-case code.
8965 *
8966 * Note that it is illegal to call this function while holding any
8967 * lock that is acquired by a CPU-hotplug notifier. Failing to
8968 * observe this restriction will result in deadlock.
8969 */
8970void synchronize_sched_expedited(void)
8971{
Tejun Heo969c7922010-05-06 18:49:21 +02008972 int snap, trycount = 0;
Paul E. McKenney03b042b2009-06-25 09:08:16 -07008973
8974 smp_mb(); /* ensure prior mod happens before capturing snap. */
Tejun Heo969c7922010-05-06 18:49:21 +02008975 snap = atomic_read(&synchronize_sched_expedited_count) + 1;
Paul E. McKenney03b042b2009-06-25 09:08:16 -07008976 get_online_cpus();
Tejun Heo969c7922010-05-06 18:49:21 +02008977 while (try_stop_cpus(cpu_online_mask,
8978 synchronize_sched_expedited_cpu_stop,
Tejun Heo94458d52010-05-06 18:49:21 +02008979 NULL) == -EAGAIN) {
Paul E. McKenney03b042b2009-06-25 09:08:16 -07008980 put_online_cpus();
8981 if (trycount++ < 10)
8982 udelay(trycount * num_online_cpus());
8983 else {
8984 synchronize_sched();
8985 return;
8986 }
Tejun Heo969c7922010-05-06 18:49:21 +02008987 if (atomic_read(&synchronize_sched_expedited_count) - snap > 0) {
Paul E. McKenney03b042b2009-06-25 09:08:16 -07008988 smp_mb(); /* ensure test happens before caller kfree */
8989 return;
8990 }
8991 get_online_cpus();
8992 }
Tejun Heo969c7922010-05-06 18:49:21 +02008993 atomic_inc(&synchronize_sched_expedited_count);
Paul E. McKenneycc631fb2010-05-06 18:49:21 +02008994 smp_mb__after_atomic_inc(); /* ensure post-GP actions seen after GP. */
Paul E. McKenney03b042b2009-06-25 09:08:16 -07008995 put_online_cpus();
Paul E. McKenney03b042b2009-06-25 09:08:16 -07008996}
8997EXPORT_SYMBOL_GPL(synchronize_sched_expedited);
8998
8999#endif /* #else #ifndef CONFIG_SMP */