blob: 0b914fc90a55d6c2cda4dfe676dbe4e9c904f747 [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>
58#include <linux/kthread.h>
Alexey Dobriyanb5aadf72008-10-06 13:23:43 +040059#include <linux/proc_fs.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070060#include <linux/seq_file.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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070074
Eric Dumazet5517d862007-05-08 00:32:57 -070075#include <asm/tlb.h>
Satyam Sharma838225b2007-10-24 18:23:50 +020076#include <asm/irq_regs.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070077
Gregory Haskins6e0534f2008-05-12 21:21:01 +020078#include "sched_cpupri.h"
79
Steven Rostedta8d154b2009-04-10 09:36:00 -040080#define CREATE_TRACE_POINTS
Steven Rostedtad8d75f2009-04-14 19:39:12 -040081#include <trace/events/sched.h>
Steven Rostedta8d154b2009-04-10 09:36:00 -040082
Linus Torvalds1da177e2005-04-16 15:20:36 -070083/*
84 * Convert user-nice values [ -20 ... 0 ... 19 ]
85 * to static priority [ MAX_RT_PRIO..MAX_PRIO-1 ],
86 * and back.
87 */
88#define NICE_TO_PRIO(nice) (MAX_RT_PRIO + (nice) + 20)
89#define PRIO_TO_NICE(prio) ((prio) - MAX_RT_PRIO - 20)
90#define TASK_NICE(p) PRIO_TO_NICE((p)->static_prio)
91
92/*
93 * 'User priority' is the nice value converted to something we
94 * can work with better when scaling various scheduler parameters,
95 * it's a [ 0 ... 39 ] range.
96 */
97#define USER_PRIO(p) ((p)-MAX_RT_PRIO)
98#define TASK_USER_PRIO(p) USER_PRIO((p)->static_prio)
99#define MAX_USER_PRIO (USER_PRIO(MAX_PRIO))
100
101/*
Ingo Molnard7876a02008-01-25 21:08:19 +0100102 * Helpers for converting nanosecond timing to jiffy resolution
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 */
Eric Dumazetd6322fa2007-11-09 22:39:38 +0100104#define NS_TO_JIFFIES(TIME) ((unsigned long)(TIME) / (NSEC_PER_SEC / HZ))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200106#define NICE_0_LOAD SCHED_LOAD_SCALE
107#define NICE_0_SHIFT SCHED_LOAD_SHIFT
108
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109/*
110 * These are the 'tuning knobs' of the scheduler:
111 *
Dmitry Adamushkoa4ec24b2007-10-15 17:00:13 +0200112 * default timeslice is 100 msecs (used only for SCHED_RR tasks).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113 * Timeslices get refilled after they expire.
114 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115#define DEF_TIMESLICE (100 * HZ / 1000)
Peter Williams2dd73a42006-06-27 02:54:34 -0700116
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +0200117/*
118 * single value that denotes runtime == period, ie unlimited time.
119 */
120#define RUNTIME_INF ((u64)~0ULL)
121
Ingo Molnare05606d2007-07-09 18:51:59 +0200122static inline int rt_policy(int policy)
123{
Roel Kluin3f33a7c2008-05-13 23:44:11 +0200124 if (unlikely(policy == SCHED_FIFO || policy == SCHED_RR))
Ingo Molnare05606d2007-07-09 18:51:59 +0200125 return 1;
126 return 0;
127}
128
129static inline int task_has_rt_policy(struct task_struct *p)
130{
131 return rt_policy(p->policy);
132}
133
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134/*
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200135 * This is the priority-queue data structure of the RT scheduling class:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 */
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200137struct rt_prio_array {
138 DECLARE_BITMAP(bitmap, MAX_RT_PRIO+1); /* include 1 bit for delimiter */
139 struct list_head queue[MAX_RT_PRIO];
140};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +0200142struct rt_bandwidth {
Ingo Molnarea736ed2008-03-25 13:51:45 +0100143 /* nests inside the rq lock: */
Thomas Gleixner0986b112009-11-17 15:32:06 +0100144 raw_spinlock_t rt_runtime_lock;
Ingo Molnarea736ed2008-03-25 13:51:45 +0100145 ktime_t rt_period;
146 u64 rt_runtime;
147 struct hrtimer rt_period_timer;
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +0200148};
149
150static struct rt_bandwidth def_rt_bandwidth;
151
152static int do_sched_rt_period_timer(struct rt_bandwidth *rt_b, int overrun);
153
154static enum hrtimer_restart sched_rt_period_timer(struct hrtimer *timer)
155{
156 struct rt_bandwidth *rt_b =
157 container_of(timer, struct rt_bandwidth, rt_period_timer);
158 ktime_t now;
159 int overrun;
160 int idle = 0;
161
162 for (;;) {
163 now = hrtimer_cb_get_time(timer);
164 overrun = hrtimer_forward(timer, now, rt_b->rt_period);
165
166 if (!overrun)
167 break;
168
169 idle = do_sched_rt_period_timer(rt_b, overrun);
170 }
171
172 return idle ? HRTIMER_NORESTART : HRTIMER_RESTART;
173}
174
175static
176void init_rt_bandwidth(struct rt_bandwidth *rt_b, u64 period, u64 runtime)
177{
178 rt_b->rt_period = ns_to_ktime(period);
179 rt_b->rt_runtime = runtime;
180
Thomas Gleixner0986b112009-11-17 15:32:06 +0100181 raw_spin_lock_init(&rt_b->rt_runtime_lock);
Peter Zijlstraac086bc2008-04-19 19:44:58 +0200182
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +0200183 hrtimer_init(&rt_b->rt_period_timer,
184 CLOCK_MONOTONIC, HRTIMER_MODE_REL);
185 rt_b->rt_period_timer.function = sched_rt_period_timer;
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +0200186}
187
Krzysztof Heltc8bfff62008-09-05 23:46:19 +0200188static inline int rt_bandwidth_enabled(void)
189{
190 return sysctl_sched_rt_runtime >= 0;
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +0200191}
192
193static void start_rt_bandwidth(struct rt_bandwidth *rt_b)
194{
195 ktime_t now;
196
Hiroshi Shimamotocac64d02009-02-25 09:59:26 -0800197 if (!rt_bandwidth_enabled() || rt_b->rt_runtime == RUNTIME_INF)
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +0200198 return;
199
200 if (hrtimer_active(&rt_b->rt_period_timer))
201 return;
202
Thomas Gleixner0986b112009-11-17 15:32:06 +0100203 raw_spin_lock(&rt_b->rt_runtime_lock);
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +0200204 for (;;) {
Peter Zijlstra7f1e2ca2009-03-13 12:21:27 +0100205 unsigned long delta;
206 ktime_t soft, hard;
207
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +0200208 if (hrtimer_active(&rt_b->rt_period_timer))
209 break;
210
211 now = hrtimer_cb_get_time(&rt_b->rt_period_timer);
212 hrtimer_forward(&rt_b->rt_period_timer, now, rt_b->rt_period);
Peter Zijlstra7f1e2ca2009-03-13 12:21:27 +0100213
214 soft = hrtimer_get_softexpires(&rt_b->rt_period_timer);
215 hard = hrtimer_get_expires(&rt_b->rt_period_timer);
216 delta = ktime_to_ns(ktime_sub(hard, soft));
217 __hrtimer_start_range_ns(&rt_b->rt_period_timer, soft, delta,
Arun R Bharadwaj5c333862009-04-16 12:14:37 +0530218 HRTIMER_MODE_ABS_PINNED, 0);
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +0200219 }
Thomas Gleixner0986b112009-11-17 15:32:06 +0100220 raw_spin_unlock(&rt_b->rt_runtime_lock);
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +0200221}
222
223#ifdef CONFIG_RT_GROUP_SCHED
224static void destroy_rt_bandwidth(struct rt_bandwidth *rt_b)
225{
226 hrtimer_cancel(&rt_b->rt_period_timer);
227}
228#endif
229
Heiko Carstens712555e2008-04-28 11:33:07 +0200230/*
231 * sched_domains_mutex serializes calls to arch_init_sched_domains,
232 * detach_destroy_domains and partition_sched_domains.
233 */
234static DEFINE_MUTEX(sched_domains_mutex);
235
Dhaval Giani7c941432010-01-20 13:26:18 +0100236#ifdef CONFIG_CGROUP_SCHED
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +0200237
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -0700238#include <linux/cgroup.h>
239
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +0200240struct cfs_rq;
241
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100242static LIST_HEAD(task_groups);
243
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +0200244/* task group related information */
Ingo Molnar4cf86d72007-10-15 17:00:14 +0200245struct task_group {
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -0700246 struct cgroup_subsys_state css;
Arun R Bharadwaj6c415b92008-12-01 20:49:05 +0530247
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100248#ifdef CONFIG_FAIR_GROUP_SCHED
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +0200249 /* schedulable entities of this group on each cpu */
250 struct sched_entity **se;
251 /* runqueue "owned" by this group on each cpu */
252 struct cfs_rq **cfs_rq;
253 unsigned long shares;
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100254#endif
255
256#ifdef CONFIG_RT_GROUP_SCHED
257 struct sched_rt_entity **rt_se;
258 struct rt_rq **rt_rq;
259
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +0200260 struct rt_bandwidth rt_bandwidth;
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100261#endif
Srivatsa Vaddagiri6b2d7702008-01-25 21:08:00 +0100262
Srivatsa Vaddagiriae8393e2007-10-29 21:18:11 +0100263 struct rcu_head rcu;
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100264 struct list_head list;
Peter Zijlstraf473aa52008-04-19 19:45:00 +0200265
266 struct task_group *parent;
267 struct list_head siblings;
268 struct list_head children;
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +0200269};
270
Peter Zijlstraeff766a2008-04-19 19:45:00 +0200271#define root_task_group init_task_group
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100272
Peter Zijlstra8ed36992008-02-13 15:45:39 +0100273/* task_group_lock serializes add/remove of task groups and also changes to
Srivatsa Vaddagiriec2c5072008-01-25 21:07:59 +0100274 * a task group's cpu shares.
275 */
Peter Zijlstra8ed36992008-02-13 15:45:39 +0100276static DEFINE_SPINLOCK(task_group_lock);
Srivatsa Vaddagiriec2c5072008-01-25 21:07:59 +0100277
Cyrill Gorcunove9036b32009-10-26 22:24:14 +0300278#ifdef CONFIG_FAIR_GROUP_SCHED
279
Peter Zijlstra57310a92009-03-09 13:56:21 +0100280#ifdef CONFIG_SMP
281static int root_task_group_empty(void)
282{
283 return list_empty(&root_task_group.children);
284}
285#endif
286
Srivatsa Vaddagiri93f992c2008-01-25 21:07:59 +0100287# define INIT_TASK_GROUP_LOAD NICE_0_LOAD
Srivatsa Vaddagiri24e377a2007-10-15 17:00:09 +0200288
Miao Xiecb4ad1f2008-04-28 12:54:56 +0800289/*
Lai Jiangshan2e084782008-06-12 16:42:58 +0800290 * A weight of 0 or 1 can cause arithmetics problems.
291 * A weight of a cfs_rq is the sum of weights of which entities
292 * are queued on this cfs_rq, so a weight of a entity should not be
293 * too large, so as the shares value of a task group.
Miao Xiecb4ad1f2008-04-28 12:54:56 +0800294 * (The default weight is 1024 - so there's no practical
295 * limitation from this.)
296 */
Peter Zijlstra18d95a22008-04-19 19:45:00 +0200297#define MIN_SHARES 2
Lai Jiangshan2e084782008-06-12 16:42:58 +0800298#define MAX_SHARES (1UL << 18)
Peter Zijlstra18d95a22008-04-19 19:45:00 +0200299
Srivatsa Vaddagiri93f992c2008-01-25 21:07:59 +0100300static int init_task_group_load = INIT_TASK_GROUP_LOAD;
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100301#endif
302
303/* Default task group.
304 * Every task in system belong to this group at bootup.
305 */
Mike Travis434d53b2008-04-04 18:11:04 -0700306struct task_group init_task_group;
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +0200307
308/* return group to which a task belongs */
Ingo Molnar4cf86d72007-10-15 17:00:14 +0200309static inline struct task_group *task_group(struct task_struct *p)
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +0200310{
Ingo Molnar4cf86d72007-10-15 17:00:14 +0200311 struct task_group *tg;
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +0200312
Dhaval Giani7c941432010-01-20 13:26:18 +0100313#ifdef CONFIG_CGROUP_SCHED
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -0700314 tg = container_of(task_subsys_state(p, cpu_cgroup_subsys_id),
315 struct task_group, css);
Srivatsa Vaddagiri24e377a2007-10-15 17:00:09 +0200316#else
Ingo Molnar41a2d6c2007-12-05 15:46:09 +0100317 tg = &init_task_group;
Srivatsa Vaddagiri24e377a2007-10-15 17:00:09 +0200318#endif
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +0200319 return tg;
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +0200320}
321
322/* Change a task's cfs_rq and parent entity if it moves across CPUs/groups */
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100323static inline void set_task_rq(struct task_struct *p, unsigned int cpu)
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +0200324{
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100325#ifdef CONFIG_FAIR_GROUP_SCHED
Dmitry Adamushkoce96b5a2007-11-15 20:57:40 +0100326 p->se.cfs_rq = task_group(p)->cfs_rq[cpu];
327 p->se.parent = task_group(p)->se[cpu];
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100328#endif
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100329
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100330#ifdef CONFIG_RT_GROUP_SCHED
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100331 p->rt.rt_rq = task_group(p)->rt_rq[cpu];
332 p->rt.parent = task_group(p)->rt_se[cpu];
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100333#endif
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +0200334}
335
336#else
337
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100338static inline void set_task_rq(struct task_struct *p, unsigned int cpu) { }
Peter Zijlstra83378262008-06-27 13:41:37 +0200339static inline struct task_group *task_group(struct task_struct *p)
340{
341 return NULL;
342}
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +0200343
Dhaval Giani7c941432010-01-20 13:26:18 +0100344#endif /* CONFIG_CGROUP_SCHED */
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +0200345
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200346/* CFS-related fields in a runqueue */
347struct cfs_rq {
348 struct load_weight load;
349 unsigned long nr_running;
350
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200351 u64 exec_clock;
Ingo Molnare9acbff2007-10-15 17:00:04 +0200352 u64 min_vruntime;
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200353
354 struct rb_root tasks_timeline;
355 struct rb_node *rb_leftmost;
Peter Zijlstra4a55bd52008-04-19 19:45:00 +0200356
357 struct list_head tasks;
358 struct list_head *balance_iterator;
359
360 /*
361 * 'curr' points to currently running entity on this cfs_rq.
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200362 * It is set to NULL otherwise (i.e when none are currently running).
363 */
Peter Zijlstra47932412008-11-04 21:25:09 +0100364 struct sched_entity *curr, *next, *last;
Peter Zijlstraddc97292007-10-15 17:00:10 +0200365
Peter Zijlstra5ac5c4d2008-11-10 10:46:32 +0100366 unsigned int nr_spread_over;
Peter Zijlstraddc97292007-10-15 17:00:10 +0200367
Ingo Molnar62160e32007-10-15 17:00:03 +0200368#ifdef CONFIG_FAIR_GROUP_SCHED
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200369 struct rq *rq; /* cpu runqueue to which this cfs_rq is attached */
370
Ingo Molnar41a2d6c2007-12-05 15:46:09 +0100371 /*
372 * leaf cfs_rqs are those that hold tasks (lowest schedulable entity in
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200373 * a hierarchy). Non-leaf lrqs hold other higher schedulable entities
374 * (like users, containers etc.)
375 *
376 * leaf_cfs_rq_list ties together list of leaf cfs_rq's in a cpu. This
377 * list is used during load balance.
378 */
Ingo Molnar41a2d6c2007-12-05 15:46:09 +0100379 struct list_head leaf_cfs_rq_list;
380 struct task_group *tg; /* group that "owns" this runqueue */
Peter Zijlstrac09595f2008-06-27 13:41:14 +0200381
382#ifdef CONFIG_SMP
Peter Zijlstrac09595f2008-06-27 13:41:14 +0200383 /*
Peter Zijlstrac8cba852008-06-27 13:41:23 +0200384 * the part of load.weight contributed by tasks
Peter Zijlstrac09595f2008-06-27 13:41:14 +0200385 */
Peter Zijlstrac8cba852008-06-27 13:41:23 +0200386 unsigned long task_weight;
Peter Zijlstrac09595f2008-06-27 13:41:14 +0200387
Peter Zijlstrac8cba852008-06-27 13:41:23 +0200388 /*
389 * h_load = weight * f(tg)
390 *
391 * Where f(tg) is the recursive weight fraction assigned to
392 * this group.
393 */
394 unsigned long h_load;
Peter Zijlstrac09595f2008-06-27 13:41:14 +0200395
Peter Zijlstrac8cba852008-06-27 13:41:23 +0200396 /*
397 * this cpu's part of tg->shares
398 */
399 unsigned long shares;
Peter Zijlstraf1d239f2008-06-27 13:41:38 +0200400
401 /*
402 * load.weight at the time we set shares
403 */
404 unsigned long rq_weight;
Peter Zijlstrac09595f2008-06-27 13:41:14 +0200405#endif
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200406#endif
407};
408
409/* Real-Time classes' related field in a runqueue: */
410struct rt_rq {
411 struct rt_prio_array active;
Steven Rostedt63489e42008-01-25 21:08:03 +0100412 unsigned long rt_nr_running;
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100413#if defined CONFIG_SMP || defined CONFIG_RT_GROUP_SCHED
Gregory Haskinse864c492008-12-29 09:39:49 -0500414 struct {
415 int curr; /* highest queued rt task prio */
Gregory Haskins398a1532009-01-14 09:10:04 -0500416#ifdef CONFIG_SMP
Gregory Haskinse864c492008-12-29 09:39:49 -0500417 int next; /* next highest */
Gregory Haskins398a1532009-01-14 09:10:04 -0500418#endif
Gregory Haskinse864c492008-12-29 09:39:49 -0500419 } highest_prio;
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100420#endif
Peter Zijlstrafa85ae22008-01-25 21:08:29 +0100421#ifdef CONFIG_SMP
Gregory Haskins73fe6aa2008-01-25 21:08:07 +0100422 unsigned long rt_nr_migratory;
Peter Zijlstraa1ba4d82009-04-01 18:40:15 +0200423 unsigned long rt_nr_total;
Gregory Haskinsa22d7fc2008-01-25 21:08:12 +0100424 int overloaded;
Gregory Haskins917b6272008-12-29 09:39:53 -0500425 struct plist_head pushable_tasks;
Peter Zijlstrafa85ae22008-01-25 21:08:29 +0100426#endif
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100427 int rt_throttled;
Peter Zijlstrafa85ae22008-01-25 21:08:29 +0100428 u64 rt_time;
Peter Zijlstraac086bc2008-04-19 19:44:58 +0200429 u64 rt_runtime;
Ingo Molnarea736ed2008-03-25 13:51:45 +0100430 /* Nests inside the rq lock: */
Thomas Gleixner0986b112009-11-17 15:32:06 +0100431 raw_spinlock_t rt_runtime_lock;
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100432
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100433#ifdef CONFIG_RT_GROUP_SCHED
Peter Zijlstra23b0fdf2008-02-13 15:45:39 +0100434 unsigned long rt_nr_boosted;
435
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100436 struct rq *rq;
437 struct list_head leaf_rt_rq_list;
438 struct task_group *tg;
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100439#endif
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200440};
441
Gregory Haskins57d885f2008-01-25 21:08:18 +0100442#ifdef CONFIG_SMP
443
444/*
445 * We add the notion of a root-domain which will be used to define per-domain
Ingo Molnar0eab9142008-01-25 21:08:19 +0100446 * variables. Each exclusive cpuset essentially defines an island domain by
447 * fully partitioning the member cpus from any other cpuset. Whenever a new
Gregory Haskins57d885f2008-01-25 21:08:18 +0100448 * exclusive cpuset is created, we also create and attach a new root-domain
449 * object.
450 *
Gregory Haskins57d885f2008-01-25 21:08:18 +0100451 */
452struct root_domain {
453 atomic_t refcount;
Rusty Russellc6c49272008-11-25 02:35:05 +1030454 cpumask_var_t span;
455 cpumask_var_t online;
Gregory Haskins637f5082008-01-25 21:08:18 +0100456
Ingo Molnar0eab9142008-01-25 21:08:19 +0100457 /*
Gregory Haskins637f5082008-01-25 21:08:18 +0100458 * The "RT overload" flag: it gets set if a CPU has more than
459 * one runnable RT task.
460 */
Rusty Russellc6c49272008-11-25 02:35:05 +1030461 cpumask_var_t rto_mask;
Ingo Molnar0eab9142008-01-25 21:08:19 +0100462 atomic_t rto_count;
Gregory Haskins6e0534f2008-05-12 21:21:01 +0200463#ifdef CONFIG_SMP
464 struct cpupri cpupri;
465#endif
Gregory Haskins57d885f2008-01-25 21:08:18 +0100466};
467
Gregory Haskinsdc938522008-01-25 21:08:26 +0100468/*
469 * By default the system creates a single root-domain with all cpus as
470 * members (mimicking the global state we have today).
471 */
Gregory Haskins57d885f2008-01-25 21:08:18 +0100472static struct root_domain def_root_domain;
473
474#endif
475
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200476/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477 * This is the main, per-CPU runqueue data structure.
478 *
479 * Locking rule: those places that want to lock multiple runqueues
480 * (such as the load balancing or the thread migration code), lock
481 * acquire operations must be ordered by ascending &runqueue.
482 */
Ingo Molnar70b97a72006-07-03 00:25:42 -0700483struct rq {
Ingo Molnard8016492007-10-18 21:32:55 +0200484 /* runqueue lock: */
Thomas Gleixner05fa7852009-11-17 14:28:38 +0100485 raw_spinlock_t lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486
487 /*
488 * nr_running and cpu_load should be in the same cacheline because
489 * remote CPUs use both these fields when doing load calculation.
490 */
491 unsigned long nr_running;
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200492 #define CPU_LOAD_IDX_MAX 5
493 unsigned long cpu_load[CPU_LOAD_IDX_MAX];
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -0700494#ifdef CONFIG_NO_HZ
495 unsigned char in_nohz_recently;
496#endif
Ingo Molnard8016492007-10-18 21:32:55 +0200497 /* capture load from *all* tasks on this cpu: */
498 struct load_weight load;
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200499 unsigned long nr_load_updates;
500 u64 nr_switches;
501
502 struct cfs_rq cfs;
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100503 struct rt_rq rt;
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100504
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200505#ifdef CONFIG_FAIR_GROUP_SCHED
Ingo Molnard8016492007-10-18 21:32:55 +0200506 /* list of leaf cfs_rq on this cpu: */
507 struct list_head leaf_cfs_rq_list;
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100508#endif
509#ifdef CONFIG_RT_GROUP_SCHED
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100510 struct list_head leaf_rt_rq_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512
513 /*
514 * This is part of a global counter where only the total sum
515 * over all CPUs matters. A task can increase this counter on
516 * one CPU and if it got migrated afterwards it may decrease
517 * it on another CPU. Always updated under the runqueue lock:
518 */
519 unsigned long nr_uninterruptible;
520
Ingo Molnar36c8b582006-07-03 00:25:41 -0700521 struct task_struct *curr, *idle;
Christoph Lameterc9819f42006-12-10 02:20:25 -0800522 unsigned long next_balance;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 struct mm_struct *prev_mm;
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200524
Peter Zijlstra3e51f332008-05-03 18:29:28 +0200525 u64 clock;
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200526
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527 atomic_t nr_iowait;
528
529#ifdef CONFIG_SMP
Ingo Molnar0eab9142008-01-25 21:08:19 +0100530 struct root_domain *rd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 struct sched_domain *sd;
532
Henrik Austada0a522c2009-02-13 20:35:45 +0100533 unsigned char idle_at_tick;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 /* For active balancing */
Gregory Haskins3f029d32009-07-29 11:08:47 -0400535 int post_schedule;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536 int active_balance;
537 int push_cpu;
Ingo Molnard8016492007-10-18 21:32:55 +0200538 /* cpu of this runqueue: */
539 int cpu;
Gregory Haskins1f11eb62008-06-04 15:04:05 -0400540 int online;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541
Peter Zijlstraa8a51d52008-06-27 13:41:26 +0200542 unsigned long avg_load_per_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543
Ingo Molnar36c8b582006-07-03 00:25:41 -0700544 struct task_struct *migration_thread;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545 struct list_head migration_queue;
Peter Zijlstrae9e92502009-09-01 10:34:37 +0200546
547 u64 rt_avg;
548 u64 age_stamp;
Mike Galbraith1b9508f2009-11-04 17:53:50 +0100549 u64 idle_stamp;
550 u64 avg_idle;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551#endif
552
Thomas Gleixnerdce48a82009-04-11 10:43:41 +0200553 /* calc_load related fields */
554 unsigned long calc_load_update;
555 long calc_load_active;
556
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +0100557#ifdef CONFIG_SCHED_HRTICK
Peter Zijlstra31656512008-07-18 18:01:23 +0200558#ifdef CONFIG_SMP
559 int hrtick_csd_pending;
560 struct call_single_data hrtick_csd;
561#endif
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +0100562 struct hrtimer hrtick_timer;
563#endif
564
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565#ifdef CONFIG_SCHEDSTATS
566 /* latency stats */
567 struct sched_info rq_sched_info;
Ken Chen9c2c4802008-12-16 23:41:22 -0800568 unsigned long long rq_cpu_time;
569 /* could above be rq->cfs_rq.exec_clock + rq->rt_rq.rt_runtime ? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570
571 /* sys_sched_yield() stats */
Ken Chen480b9432007-10-18 21:32:56 +0200572 unsigned int yld_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573
574 /* schedule() stats */
Ken Chen480b9432007-10-18 21:32:56 +0200575 unsigned int sched_switch;
576 unsigned int sched_count;
577 unsigned int sched_goidle;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578
579 /* try_to_wake_up() stats */
Ken Chen480b9432007-10-18 21:32:56 +0200580 unsigned int ttwu_count;
581 unsigned int ttwu_local;
Ingo Molnarb8efb562007-10-15 17:00:10 +0200582
583 /* BKL stats */
Ken Chen480b9432007-10-18 21:32:56 +0200584 unsigned int bkl_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585#endif
586};
587
Fenghua Yuf34e3b62007-07-19 01:48:13 -0700588static DEFINE_PER_CPU_SHARED_ALIGNED(struct rq, runqueues);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589
Peter Zijlstra7d478722009-09-14 19:55:44 +0200590static inline
591void check_preempt_curr(struct rq *rq, struct task_struct *p, int flags)
Ingo Molnardd41f592007-07-09 18:51:59 +0200592{
Peter Zijlstra7d478722009-09-14 19:55:44 +0200593 rq->curr->sched_class->check_preempt_curr(rq, p, flags);
Ingo Molnardd41f592007-07-09 18:51:59 +0200594}
595
Christoph Lameter0a2966b2006-09-25 23:30:51 -0700596static inline int cpu_of(struct rq *rq)
597{
598#ifdef CONFIG_SMP
599 return rq->cpu;
600#else
601 return 0;
602#endif
603}
604
Ingo Molnar20d315d2007-07-09 18:51:58 +0200605/*
Nick Piggin674311d2005-06-25 14:57:27 -0700606 * The domain tree (rq->sd) is protected by RCU's quiescent state transition.
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -0700607 * See detach_destroy_domains: synchronize_sched for details.
Nick Piggin674311d2005-06-25 14:57:27 -0700608 *
609 * The domain tree of any CPU may only be accessed from within
610 * preempt-disabled sections.
611 */
Ingo Molnar48f24c42006-07-03 00:25:40 -0700612#define for_each_domain(cpu, __sd) \
613 for (__sd = rcu_dereference(cpu_rq(cpu)->sd); __sd; __sd = __sd->parent)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614
615#define cpu_rq(cpu) (&per_cpu(runqueues, (cpu)))
616#define this_rq() (&__get_cpu_var(runqueues))
617#define task_rq(p) cpu_rq(task_cpu(p))
618#define cpu_curr(cpu) (cpu_rq(cpu)->curr)
Hitoshi Mitake54d35f22009-06-29 14:44:57 +0900619#define raw_rq() (&__raw_get_cpu_var(runqueues))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620
Ingo Molnaraa9c4c02008-12-17 14:10:57 +0100621inline void update_rq_clock(struct rq *rq)
Peter Zijlstra3e51f332008-05-03 18:29:28 +0200622{
623 rq->clock = sched_clock_cpu(cpu_of(rq));
624}
625
Ingo Molnare436d802007-07-19 21:28:35 +0200626/*
Ingo Molnarbf5c91b2007-10-15 17:00:04 +0200627 * Tunables that become constants when CONFIG_SCHED_DEBUG is off:
628 */
629#ifdef CONFIG_SCHED_DEBUG
630# define const_debug __read_mostly
631#else
632# define const_debug static const
633#endif
634
Ingo Molnar017730c2008-05-12 21:20:52 +0200635/**
636 * runqueue_is_locked
Randy Dunlape17b38b2009-10-11 19:12:00 -0700637 * @cpu: the processor in question.
Ingo Molnar017730c2008-05-12 21:20:52 +0200638 *
639 * Returns true if the current cpu runqueue is locked.
640 * This interface allows printk to be called with the runqueue lock
641 * held and know whether or not it is OK to wake up the klogd.
642 */
Andrew Morton89f19f02009-09-19 11:55:44 -0700643int runqueue_is_locked(int cpu)
Ingo Molnar017730c2008-05-12 21:20:52 +0200644{
Thomas Gleixner05fa7852009-11-17 14:28:38 +0100645 return raw_spin_is_locked(&cpu_rq(cpu)->lock);
Ingo Molnar017730c2008-05-12 21:20:52 +0200646}
647
Ingo Molnarbf5c91b2007-10-15 17:00:04 +0200648/*
649 * Debugging: various feature bits
650 */
Peter Zijlstraf00b45c2008-04-19 19:45:00 +0200651
652#define SCHED_FEAT(name, enabled) \
653 __SCHED_FEAT_##name ,
654
Ingo Molnarbf5c91b2007-10-15 17:00:04 +0200655enum {
Peter Zijlstraf00b45c2008-04-19 19:45:00 +0200656#include "sched_features.h"
Ingo Molnarbf5c91b2007-10-15 17:00:04 +0200657};
658
Peter Zijlstraf00b45c2008-04-19 19:45:00 +0200659#undef SCHED_FEAT
Ingo Molnarbf5c91b2007-10-15 17:00:04 +0200660
Peter Zijlstraf00b45c2008-04-19 19:45:00 +0200661#define SCHED_FEAT(name, enabled) \
662 (1UL << __SCHED_FEAT_##name) * enabled |
663
664const_debug unsigned int sysctl_sched_features =
665#include "sched_features.h"
666 0;
667
668#undef SCHED_FEAT
669
670#ifdef CONFIG_SCHED_DEBUG
671#define SCHED_FEAT(name, enabled) \
672 #name ,
673
Harvey Harrison983ed7a2008-04-24 18:17:55 -0700674static __read_mostly char *sched_feat_names[] = {
Peter Zijlstraf00b45c2008-04-19 19:45:00 +0200675#include "sched_features.h"
676 NULL
677};
678
679#undef SCHED_FEAT
680
Li Zefan34f3a812008-10-30 15:23:32 +0800681static int sched_feat_show(struct seq_file *m, void *v)
Peter Zijlstraf00b45c2008-04-19 19:45:00 +0200682{
Peter Zijlstraf00b45c2008-04-19 19:45:00 +0200683 int i;
684
685 for (i = 0; sched_feat_names[i]; i++) {
Li Zefan34f3a812008-10-30 15:23:32 +0800686 if (!(sysctl_sched_features & (1UL << i)))
687 seq_puts(m, "NO_");
688 seq_printf(m, "%s ", sched_feat_names[i]);
Peter Zijlstraf00b45c2008-04-19 19:45:00 +0200689 }
Li Zefan34f3a812008-10-30 15:23:32 +0800690 seq_puts(m, "\n");
Peter Zijlstraf00b45c2008-04-19 19:45:00 +0200691
Li Zefan34f3a812008-10-30 15:23:32 +0800692 return 0;
Peter Zijlstraf00b45c2008-04-19 19:45:00 +0200693}
694
695static ssize_t
696sched_feat_write(struct file *filp, const char __user *ubuf,
697 size_t cnt, loff_t *ppos)
698{
699 char buf[64];
700 char *cmp = buf;
701 int neg = 0;
702 int i;
703
704 if (cnt > 63)
705 cnt = 63;
706
707 if (copy_from_user(&buf, ubuf, cnt))
708 return -EFAULT;
709
710 buf[cnt] = 0;
711
Ingo Molnarc24b7c52008-04-18 10:55:34 +0200712 if (strncmp(buf, "NO_", 3) == 0) {
Peter Zijlstraf00b45c2008-04-19 19:45:00 +0200713 neg = 1;
714 cmp += 3;
715 }
716
717 for (i = 0; sched_feat_names[i]; i++) {
718 int len = strlen(sched_feat_names[i]);
719
720 if (strncmp(cmp, sched_feat_names[i], len) == 0) {
721 if (neg)
722 sysctl_sched_features &= ~(1UL << i);
723 else
724 sysctl_sched_features |= (1UL << i);
725 break;
726 }
727 }
728
729 if (!sched_feat_names[i])
730 return -EINVAL;
731
Jan Blunck42994722009-11-20 17:40:37 +0100732 *ppos += cnt;
Peter Zijlstraf00b45c2008-04-19 19:45:00 +0200733
734 return cnt;
735}
736
Li Zefan34f3a812008-10-30 15:23:32 +0800737static int sched_feat_open(struct inode *inode, struct file *filp)
738{
739 return single_open(filp, sched_feat_show, NULL);
740}
741
Alexey Dobriyan828c0952009-10-01 15:43:56 -0700742static const struct file_operations sched_feat_fops = {
Li Zefan34f3a812008-10-30 15:23:32 +0800743 .open = sched_feat_open,
744 .write = sched_feat_write,
745 .read = seq_read,
746 .llseek = seq_lseek,
747 .release = single_release,
Peter Zijlstraf00b45c2008-04-19 19:45:00 +0200748};
749
750static __init int sched_init_debug(void)
751{
Peter Zijlstraf00b45c2008-04-19 19:45:00 +0200752 debugfs_create_file("sched_features", 0644, NULL, NULL,
753 &sched_feat_fops);
754
755 return 0;
756}
757late_initcall(sched_init_debug);
758
759#endif
760
761#define sched_feat(x) (sysctl_sched_features & (1UL << __SCHED_FEAT_##x))
Ingo Molnarbf5c91b2007-10-15 17:00:04 +0200762
763/*
Peter Zijlstrab82d9fd2007-11-09 22:39:39 +0100764 * Number of tasks to iterate in a single balance run.
765 * Limited because this is done with IRQs disabled.
766 */
767const_debug unsigned int sysctl_sched_nr_migrate = 32;
768
769/*
Peter Zijlstra2398f2c2008-06-27 13:41:35 +0200770 * ratelimit for updating the group shares.
Peter Zijlstra55cd5342008-08-04 08:54:26 +0200771 * default: 0.25ms
Peter Zijlstra2398f2c2008-06-27 13:41:35 +0200772 */
Peter Zijlstra55cd5342008-08-04 08:54:26 +0200773unsigned int sysctl_sched_shares_ratelimit = 250000;
Christian Ehrhardt0bcdcf22009-11-30 12:16:46 +0100774unsigned int normalized_sysctl_sched_shares_ratelimit = 250000;
Peter Zijlstra2398f2c2008-06-27 13:41:35 +0200775
776/*
Peter Zijlstraffda12a2008-10-17 19:27:02 +0200777 * Inject some fuzzyness into changing the per-cpu group shares
778 * this avoids remote rq-locks at the expense of fairness.
779 * default: 4
780 */
781unsigned int sysctl_sched_shares_thresh = 4;
782
783/*
Peter Zijlstrae9e92502009-09-01 10:34:37 +0200784 * period over which we average the RT time consumption, measured
785 * in ms.
786 *
787 * default: 1s
788 */
789const_debug unsigned int sysctl_sched_time_avg = MSEC_PER_SEC;
790
791/*
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +0100792 * period over which we measure -rt task cpu usage in us.
Peter Zijlstrafa85ae22008-01-25 21:08:29 +0100793 * default: 1s
794 */
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +0100795unsigned int sysctl_sched_rt_period = 1000000;
Peter Zijlstrafa85ae22008-01-25 21:08:29 +0100796
Ingo Molnar6892b752008-02-13 14:02:36 +0100797static __read_mostly int scheduler_running;
798
Peter Zijlstrafa85ae22008-01-25 21:08:29 +0100799/*
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +0100800 * part of the period that we allow rt tasks to run in us.
801 * default: 0.95s
Peter Zijlstrafa85ae22008-01-25 21:08:29 +0100802 */
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +0100803int sysctl_sched_rt_runtime = 950000;
804
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +0200805static inline u64 global_rt_period(void)
806{
807 return (u64)sysctl_sched_rt_period * NSEC_PER_USEC;
808}
809
810static inline u64 global_rt_runtime(void)
811{
roel kluine26873b2008-07-22 16:51:15 -0400812 if (sysctl_sched_rt_runtime < 0)
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +0200813 return RUNTIME_INF;
814
815 return (u64)sysctl_sched_rt_runtime * NSEC_PER_USEC;
816}
Peter Zijlstrafa85ae22008-01-25 21:08:29 +0100817
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818#ifndef prepare_arch_switch
Nick Piggin4866cde2005-06-25 14:57:23 -0700819# define prepare_arch_switch(next) do { } while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820#endif
Nick Piggin4866cde2005-06-25 14:57:23 -0700821#ifndef finish_arch_switch
822# define finish_arch_switch(prev) do { } while (0)
823#endif
824
Dmitry Adamushko051a1d12007-12-18 15:21:13 +0100825static inline int task_current(struct rq *rq, struct task_struct *p)
826{
827 return rq->curr == p;
828}
829
Nick Piggin4866cde2005-06-25 14:57:23 -0700830#ifndef __ARCH_WANT_UNLOCKED_CTXSW
Ingo Molnar70b97a72006-07-03 00:25:42 -0700831static inline int task_running(struct rq *rq, struct task_struct *p)
Nick Piggin4866cde2005-06-25 14:57:23 -0700832{
Dmitry Adamushko051a1d12007-12-18 15:21:13 +0100833 return task_current(rq, p);
Nick Piggin4866cde2005-06-25 14:57:23 -0700834}
835
Ingo Molnar70b97a72006-07-03 00:25:42 -0700836static inline void prepare_lock_switch(struct rq *rq, struct task_struct *next)
Nick Piggin4866cde2005-06-25 14:57:23 -0700837{
838}
839
Ingo Molnar70b97a72006-07-03 00:25:42 -0700840static inline void finish_lock_switch(struct rq *rq, struct task_struct *prev)
Nick Piggin4866cde2005-06-25 14:57:23 -0700841{
Ingo Molnarda04c032005-09-13 11:17:59 +0200842#ifdef CONFIG_DEBUG_SPINLOCK
843 /* this is a valid case when another task releases the spinlock */
844 rq->lock.owner = current;
845#endif
Ingo Molnar8a25d5d2006-07-03 00:24:54 -0700846 /*
847 * If we are tracking spinlock dependencies then we have to
848 * fix up the runqueue lock - which gets 'carried over' from
849 * prev into current:
850 */
851 spin_acquire(&rq->lock.dep_map, 0, 0, _THIS_IP_);
852
Thomas Gleixner05fa7852009-11-17 14:28:38 +0100853 raw_spin_unlock_irq(&rq->lock);
Nick Piggin4866cde2005-06-25 14:57:23 -0700854}
855
856#else /* __ARCH_WANT_UNLOCKED_CTXSW */
Ingo Molnar70b97a72006-07-03 00:25:42 -0700857static inline int task_running(struct rq *rq, struct task_struct *p)
Nick Piggin4866cde2005-06-25 14:57:23 -0700858{
859#ifdef CONFIG_SMP
860 return p->oncpu;
861#else
Dmitry Adamushko051a1d12007-12-18 15:21:13 +0100862 return task_current(rq, p);
Nick Piggin4866cde2005-06-25 14:57:23 -0700863#endif
864}
865
Ingo Molnar70b97a72006-07-03 00:25:42 -0700866static inline void prepare_lock_switch(struct rq *rq, struct task_struct *next)
Nick Piggin4866cde2005-06-25 14:57:23 -0700867{
868#ifdef CONFIG_SMP
869 /*
870 * We can optimise this out completely for !SMP, because the
871 * SMP rebalancing from interrupt is the only thing that cares
872 * here.
873 */
874 next->oncpu = 1;
875#endif
876#ifdef __ARCH_WANT_INTERRUPTS_ON_CTXSW
Thomas Gleixner05fa7852009-11-17 14:28:38 +0100877 raw_spin_unlock_irq(&rq->lock);
Nick Piggin4866cde2005-06-25 14:57:23 -0700878#else
Thomas Gleixner05fa7852009-11-17 14:28:38 +0100879 raw_spin_unlock(&rq->lock);
Nick Piggin4866cde2005-06-25 14:57:23 -0700880#endif
881}
882
Ingo Molnar70b97a72006-07-03 00:25:42 -0700883static inline void finish_lock_switch(struct rq *rq, struct task_struct *prev)
Nick Piggin4866cde2005-06-25 14:57:23 -0700884{
885#ifdef CONFIG_SMP
886 /*
887 * After ->oncpu is cleared, the task can be moved to a different CPU.
888 * We must ensure this doesn't happen until the switch is completely
889 * finished.
890 */
891 smp_wmb();
892 prev->oncpu = 0;
893#endif
894#ifndef __ARCH_WANT_INTERRUPTS_ON_CTXSW
895 local_irq_enable();
896#endif
897}
898#endif /* __ARCH_WANT_UNLOCKED_CTXSW */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899
900/*
Peter Zijlstra0970d292010-02-15 14:45:54 +0100901 * Check whether the task is waking, we use this to synchronize against
902 * ttwu() so that task_cpu() reports a stable number.
903 *
904 * We need to make an exception for PF_STARTING tasks because the fork
905 * path might require task_rq_lock() to work, eg. it can call
906 * set_cpus_allowed_ptr() from the cpuset clone_ns code.
907 */
908static inline int task_is_waking(struct task_struct *p)
909{
910 return unlikely((p->state == TASK_WAKING) && !(p->flags & PF_STARTING));
911}
912
913/*
Ingo Molnarb29739f2006-06-27 02:54:51 -0700914 * __task_rq_lock - lock the runqueue a given task resides on.
915 * Must be called interrupts disabled.
916 */
Ingo Molnar70b97a72006-07-03 00:25:42 -0700917static inline struct rq *__task_rq_lock(struct task_struct *p)
Ingo Molnarb29739f2006-06-27 02:54:51 -0700918 __acquires(rq->lock)
919{
Peter Zijlstra0970d292010-02-15 14:45:54 +0100920 struct rq *rq;
921
Andi Kleen3a5c3592007-10-15 17:00:14 +0200922 for (;;) {
Peter Zijlstra0970d292010-02-15 14:45:54 +0100923 while (task_is_waking(p))
924 cpu_relax();
925 rq = task_rq(p);
Thomas Gleixner05fa7852009-11-17 14:28:38 +0100926 raw_spin_lock(&rq->lock);
Peter Zijlstra0970d292010-02-15 14:45:54 +0100927 if (likely(rq == task_rq(p) && !task_is_waking(p)))
Andi Kleen3a5c3592007-10-15 17:00:14 +0200928 return rq;
Thomas Gleixner05fa7852009-11-17 14:28:38 +0100929 raw_spin_unlock(&rq->lock);
Ingo Molnarb29739f2006-06-27 02:54:51 -0700930 }
Ingo Molnarb29739f2006-06-27 02:54:51 -0700931}
932
933/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934 * task_rq_lock - lock the runqueue a given task resides on and disable
Ingo Molnar41a2d6c2007-12-05 15:46:09 +0100935 * interrupts. Note the ordering: we can safely lookup the task_rq without
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936 * explicitly disabling preemption.
937 */
Ingo Molnar70b97a72006-07-03 00:25:42 -0700938static struct rq *task_rq_lock(struct task_struct *p, unsigned long *flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939 __acquires(rq->lock)
940{
Ingo Molnar70b97a72006-07-03 00:25:42 -0700941 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942
Andi Kleen3a5c3592007-10-15 17:00:14 +0200943 for (;;) {
Peter Zijlstra0970d292010-02-15 14:45:54 +0100944 while (task_is_waking(p))
945 cpu_relax();
Andi Kleen3a5c3592007-10-15 17:00:14 +0200946 local_irq_save(*flags);
947 rq = task_rq(p);
Thomas Gleixner05fa7852009-11-17 14:28:38 +0100948 raw_spin_lock(&rq->lock);
Peter Zijlstra0970d292010-02-15 14:45:54 +0100949 if (likely(rq == task_rq(p) && !task_is_waking(p)))
Andi Kleen3a5c3592007-10-15 17:00:14 +0200950 return rq;
Thomas Gleixner05fa7852009-11-17 14:28:38 +0100951 raw_spin_unlock_irqrestore(&rq->lock, *flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953}
954
Oleg Nesterovad474ca2008-11-10 15:39:30 +0100955void task_rq_unlock_wait(struct task_struct *p)
956{
957 struct rq *rq = task_rq(p);
958
959 smp_mb(); /* spin-unlock-wait is not a full memory barrier */
Thomas Gleixner05fa7852009-11-17 14:28:38 +0100960 raw_spin_unlock_wait(&rq->lock);
Oleg Nesterovad474ca2008-11-10 15:39:30 +0100961}
962
Alexey Dobriyana9957442007-10-15 17:00:13 +0200963static void __task_rq_unlock(struct rq *rq)
Ingo Molnarb29739f2006-06-27 02:54:51 -0700964 __releases(rq->lock)
965{
Thomas Gleixner05fa7852009-11-17 14:28:38 +0100966 raw_spin_unlock(&rq->lock);
Ingo Molnarb29739f2006-06-27 02:54:51 -0700967}
968
Ingo Molnar70b97a72006-07-03 00:25:42 -0700969static inline void task_rq_unlock(struct rq *rq, unsigned long *flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970 __releases(rq->lock)
971{
Thomas Gleixner05fa7852009-11-17 14:28:38 +0100972 raw_spin_unlock_irqrestore(&rq->lock, *flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973}
974
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975/*
Robert P. J. Daycc2a73b2006-12-10 02:20:00 -0800976 * this_rq_lock - lock this runqueue and disable interrupts.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977 */
Alexey Dobriyana9957442007-10-15 17:00:13 +0200978static struct rq *this_rq_lock(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979 __acquires(rq->lock)
980{
Ingo Molnar70b97a72006-07-03 00:25:42 -0700981 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982
983 local_irq_disable();
984 rq = this_rq();
Thomas Gleixner05fa7852009-11-17 14:28:38 +0100985 raw_spin_lock(&rq->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986
987 return rq;
988}
989
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +0100990#ifdef CONFIG_SCHED_HRTICK
991/*
992 * Use HR-timers to deliver accurate preemption points.
993 *
994 * Its all a bit involved since we cannot program an hrt while holding the
995 * rq->lock. So what we do is store a state in in rq->hrtick_* and ask for a
996 * reschedule event.
997 *
998 * When we get rescheduled we reprogram the hrtick_timer outside of the
999 * rq->lock.
1000 */
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01001001
1002/*
1003 * Use hrtick when:
1004 * - enabled by features
1005 * - hrtimer is actually high res
1006 */
1007static inline int hrtick_enabled(struct rq *rq)
1008{
1009 if (!sched_feat(HRTICK))
1010 return 0;
Ingo Molnarba420592008-07-20 11:02:06 +02001011 if (!cpu_active(cpu_of(rq)))
Peter Zijlstrab328ca12008-04-29 10:02:46 +02001012 return 0;
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01001013 return hrtimer_is_hres_active(&rq->hrtick_timer);
1014}
1015
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01001016static void hrtick_clear(struct rq *rq)
1017{
1018 if (hrtimer_active(&rq->hrtick_timer))
1019 hrtimer_cancel(&rq->hrtick_timer);
1020}
1021
1022/*
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01001023 * High-resolution timer tick.
1024 * Runs from hardirq context with interrupts disabled.
1025 */
1026static enum hrtimer_restart hrtick(struct hrtimer *timer)
1027{
1028 struct rq *rq = container_of(timer, struct rq, hrtick_timer);
1029
1030 WARN_ON_ONCE(cpu_of(rq) != smp_processor_id());
1031
Thomas Gleixner05fa7852009-11-17 14:28:38 +01001032 raw_spin_lock(&rq->lock);
Peter Zijlstra3e51f332008-05-03 18:29:28 +02001033 update_rq_clock(rq);
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01001034 rq->curr->sched_class->task_tick(rq, rq->curr, 1);
Thomas Gleixner05fa7852009-11-17 14:28:38 +01001035 raw_spin_unlock(&rq->lock);
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01001036
1037 return HRTIMER_NORESTART;
1038}
1039
Rabin Vincent95e904c2008-05-11 05:55:33 +05301040#ifdef CONFIG_SMP
Peter Zijlstra31656512008-07-18 18:01:23 +02001041/*
1042 * called from hardirq (IPI) context
1043 */
1044static void __hrtick_start(void *arg)
Peter Zijlstrab328ca12008-04-29 10:02:46 +02001045{
Peter Zijlstra31656512008-07-18 18:01:23 +02001046 struct rq *rq = arg;
Peter Zijlstrab328ca12008-04-29 10:02:46 +02001047
Thomas Gleixner05fa7852009-11-17 14:28:38 +01001048 raw_spin_lock(&rq->lock);
Peter Zijlstra31656512008-07-18 18:01:23 +02001049 hrtimer_restart(&rq->hrtick_timer);
1050 rq->hrtick_csd_pending = 0;
Thomas Gleixner05fa7852009-11-17 14:28:38 +01001051 raw_spin_unlock(&rq->lock);
Peter Zijlstrab328ca12008-04-29 10:02:46 +02001052}
1053
Peter Zijlstra31656512008-07-18 18:01:23 +02001054/*
1055 * Called to set the hrtick timer state.
1056 *
1057 * called with rq->lock held and irqs disabled
1058 */
1059static void hrtick_start(struct rq *rq, u64 delay)
Peter Zijlstrab328ca12008-04-29 10:02:46 +02001060{
Peter Zijlstra31656512008-07-18 18:01:23 +02001061 struct hrtimer *timer = &rq->hrtick_timer;
1062 ktime_t time = ktime_add_ns(timer->base->get_time(), delay);
Peter Zijlstrab328ca12008-04-29 10:02:46 +02001063
Arjan van de Vencc584b22008-09-01 15:02:30 -07001064 hrtimer_set_expires(timer, time);
Peter Zijlstra31656512008-07-18 18:01:23 +02001065
1066 if (rq == this_rq()) {
1067 hrtimer_restart(timer);
1068 } else if (!rq->hrtick_csd_pending) {
Peter Zijlstra6e275632009-02-25 13:59:48 +01001069 __smp_call_function_single(cpu_of(rq), &rq->hrtick_csd, 0);
Peter Zijlstra31656512008-07-18 18:01:23 +02001070 rq->hrtick_csd_pending = 1;
1071 }
Peter Zijlstrab328ca12008-04-29 10:02:46 +02001072}
1073
1074static int
1075hotplug_hrtick(struct notifier_block *nfb, unsigned long action, void *hcpu)
1076{
1077 int cpu = (int)(long)hcpu;
1078
1079 switch (action) {
1080 case CPU_UP_CANCELED:
1081 case CPU_UP_CANCELED_FROZEN:
1082 case CPU_DOWN_PREPARE:
1083 case CPU_DOWN_PREPARE_FROZEN:
1084 case CPU_DEAD:
1085 case CPU_DEAD_FROZEN:
Peter Zijlstra31656512008-07-18 18:01:23 +02001086 hrtick_clear(cpu_rq(cpu));
Peter Zijlstrab328ca12008-04-29 10:02:46 +02001087 return NOTIFY_OK;
1088 }
1089
1090 return NOTIFY_DONE;
1091}
1092
Rakib Mullickfa748202008-09-22 14:55:45 -07001093static __init void init_hrtick(void)
Peter Zijlstrab328ca12008-04-29 10:02:46 +02001094{
1095 hotcpu_notifier(hotplug_hrtick, 0);
1096}
Peter Zijlstra31656512008-07-18 18:01:23 +02001097#else
1098/*
1099 * Called to set the hrtick timer state.
1100 *
1101 * called with rq->lock held and irqs disabled
1102 */
1103static void hrtick_start(struct rq *rq, u64 delay)
1104{
Peter Zijlstra7f1e2ca2009-03-13 12:21:27 +01001105 __hrtimer_start_range_ns(&rq->hrtick_timer, ns_to_ktime(delay), 0,
Arun R Bharadwaj5c333862009-04-16 12:14:37 +05301106 HRTIMER_MODE_REL_PINNED, 0);
Peter Zijlstra31656512008-07-18 18:01:23 +02001107}
1108
Andrew Morton006c75f2008-09-22 14:55:46 -07001109static inline void init_hrtick(void)
Peter Zijlstra31656512008-07-18 18:01:23 +02001110{
1111}
Rabin Vincent95e904c2008-05-11 05:55:33 +05301112#endif /* CONFIG_SMP */
Peter Zijlstrab328ca12008-04-29 10:02:46 +02001113
1114static void init_rq_hrtick(struct rq *rq)
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01001115{
Peter Zijlstra31656512008-07-18 18:01:23 +02001116#ifdef CONFIG_SMP
1117 rq->hrtick_csd_pending = 0;
1118
1119 rq->hrtick_csd.flags = 0;
1120 rq->hrtick_csd.func = __hrtick_start;
1121 rq->hrtick_csd.info = rq;
1122#endif
1123
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01001124 hrtimer_init(&rq->hrtick_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
1125 rq->hrtick_timer.function = hrtick;
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01001126}
Andrew Morton006c75f2008-09-22 14:55:46 -07001127#else /* CONFIG_SCHED_HRTICK */
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01001128static inline void hrtick_clear(struct rq *rq)
1129{
1130}
1131
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01001132static inline void init_rq_hrtick(struct rq *rq)
1133{
1134}
1135
Peter Zijlstrab328ca12008-04-29 10:02:46 +02001136static inline void init_hrtick(void)
1137{
1138}
Andrew Morton006c75f2008-09-22 14:55:46 -07001139#endif /* CONFIG_SCHED_HRTICK */
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01001140
Ingo Molnar1b9f19c2007-07-09 18:51:59 +02001141/*
Ingo Molnarc24d20d2007-07-09 18:51:59 +02001142 * resched_task - mark a task 'to be rescheduled now'.
1143 *
1144 * On UP this means the setting of the need_resched flag, on SMP it
1145 * might also involve a cross-CPU call to trigger the scheduler on
1146 * the target CPU.
1147 */
1148#ifdef CONFIG_SMP
1149
1150#ifndef tsk_is_polling
1151#define tsk_is_polling(t) test_tsk_thread_flag(t, TIF_POLLING_NRFLAG)
1152#endif
1153
Peter Zijlstra31656512008-07-18 18:01:23 +02001154static void resched_task(struct task_struct *p)
Ingo Molnarc24d20d2007-07-09 18:51:59 +02001155{
1156 int cpu;
1157
Thomas Gleixner05fa7852009-11-17 14:28:38 +01001158 assert_raw_spin_locked(&task_rq(p)->lock);
Ingo Molnarc24d20d2007-07-09 18:51:59 +02001159
Lai Jiangshan5ed0cec2009-03-06 19:40:20 +08001160 if (test_tsk_need_resched(p))
Ingo Molnarc24d20d2007-07-09 18:51:59 +02001161 return;
1162
Lai Jiangshan5ed0cec2009-03-06 19:40:20 +08001163 set_tsk_need_resched(p);
Ingo Molnarc24d20d2007-07-09 18:51:59 +02001164
1165 cpu = task_cpu(p);
1166 if (cpu == smp_processor_id())
1167 return;
1168
1169 /* NEED_RESCHED must be visible before we test polling */
1170 smp_mb();
1171 if (!tsk_is_polling(p))
1172 smp_send_reschedule(cpu);
1173}
1174
1175static void resched_cpu(int cpu)
1176{
1177 struct rq *rq = cpu_rq(cpu);
1178 unsigned long flags;
1179
Thomas Gleixner05fa7852009-11-17 14:28:38 +01001180 if (!raw_spin_trylock_irqsave(&rq->lock, flags))
Ingo Molnarc24d20d2007-07-09 18:51:59 +02001181 return;
1182 resched_task(cpu_curr(cpu));
Thomas Gleixner05fa7852009-11-17 14:28:38 +01001183 raw_spin_unlock_irqrestore(&rq->lock, flags);
Ingo Molnarc24d20d2007-07-09 18:51:59 +02001184}
Thomas Gleixner06d83082008-03-22 09:20:24 +01001185
1186#ifdef CONFIG_NO_HZ
1187/*
1188 * When add_timer_on() enqueues a timer into the timer wheel of an
1189 * idle CPU then this timer might expire before the next timer event
1190 * which is scheduled to wake up that CPU. In case of a completely
1191 * idle system the next event might even be infinite time into the
1192 * future. wake_up_idle_cpu() ensures that the CPU is woken up and
1193 * leaves the inner idle loop so the newly added timer is taken into
1194 * account when the CPU goes back to idle and evaluates the timer
1195 * wheel for the next timer event.
1196 */
1197void wake_up_idle_cpu(int cpu)
1198{
1199 struct rq *rq = cpu_rq(cpu);
1200
1201 if (cpu == smp_processor_id())
1202 return;
1203
1204 /*
1205 * This is safe, as this function is called with the timer
1206 * wheel base lock of (cpu) held. When the CPU is on the way
1207 * to idle and has not yet set rq->curr to idle then it will
1208 * be serialized on the timer wheel base lock and take the new
1209 * timer into account automatically.
1210 */
1211 if (rq->curr != rq->idle)
1212 return;
1213
1214 /*
1215 * We can set TIF_RESCHED on the idle task of the other CPU
1216 * lockless. The worst case is that the other CPU runs the
1217 * idle task through an additional NOOP schedule()
1218 */
Lai Jiangshan5ed0cec2009-03-06 19:40:20 +08001219 set_tsk_need_resched(rq->idle);
Thomas Gleixner06d83082008-03-22 09:20:24 +01001220
1221 /* NEED_RESCHED must be visible before we test polling */
1222 smp_mb();
1223 if (!tsk_is_polling(rq->idle))
1224 smp_send_reschedule(cpu);
1225}
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +02001226#endif /* CONFIG_NO_HZ */
Thomas Gleixner06d83082008-03-22 09:20:24 +01001227
Peter Zijlstrae9e92502009-09-01 10:34:37 +02001228static u64 sched_avg_period(void)
1229{
1230 return (u64)sysctl_sched_time_avg * NSEC_PER_MSEC / 2;
1231}
1232
1233static void sched_avg_update(struct rq *rq)
1234{
1235 s64 period = sched_avg_period();
1236
1237 while ((s64)(rq->clock - rq->age_stamp) > period) {
1238 rq->age_stamp += period;
1239 rq->rt_avg /= 2;
1240 }
1241}
1242
1243static void sched_rt_avg_update(struct rq *rq, u64 rt_delta)
1244{
1245 rq->rt_avg += rt_delta;
1246 sched_avg_update(rq);
1247}
1248
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +02001249#else /* !CONFIG_SMP */
Peter Zijlstra31656512008-07-18 18:01:23 +02001250static void resched_task(struct task_struct *p)
Ingo Molnarc24d20d2007-07-09 18:51:59 +02001251{
Thomas Gleixner05fa7852009-11-17 14:28:38 +01001252 assert_raw_spin_locked(&task_rq(p)->lock);
Peter Zijlstra31656512008-07-18 18:01:23 +02001253 set_tsk_need_resched(p);
Ingo Molnarc24d20d2007-07-09 18:51:59 +02001254}
Peter Zijlstrae9e92502009-09-01 10:34:37 +02001255
1256static void sched_rt_avg_update(struct rq *rq, u64 rt_delta)
1257{
1258}
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +02001259#endif /* CONFIG_SMP */
Ingo Molnarc24d20d2007-07-09 18:51:59 +02001260
Ingo Molnar45bf76d2007-07-09 18:51:59 +02001261#if BITS_PER_LONG == 32
1262# define WMULT_CONST (~0UL)
1263#else
1264# define WMULT_CONST (1UL << 32)
1265#endif
1266
1267#define WMULT_SHIFT 32
1268
Ingo Molnar194081e2007-08-09 11:16:51 +02001269/*
1270 * Shift right and round:
1271 */
Ingo Molnarcf2ab462007-09-05 14:32:49 +02001272#define SRR(x, y) (((x) + (1UL << ((y) - 1))) >> (y))
Ingo Molnar194081e2007-08-09 11:16:51 +02001273
Peter Zijlstraa7be37a2008-06-27 13:41:11 +02001274/*
1275 * delta *= weight / lw
1276 */
Ingo Molnarcb1c4fc2007-08-02 17:41:40 +02001277static unsigned long
Ingo Molnar45bf76d2007-07-09 18:51:59 +02001278calc_delta_mine(unsigned long delta_exec, unsigned long weight,
1279 struct load_weight *lw)
1280{
1281 u64 tmp;
1282
Lai Jiangshan7a232e02008-06-12 16:43:07 +08001283 if (!lw->inv_weight) {
1284 if (BITS_PER_LONG > 32 && unlikely(lw->weight >= WMULT_CONST))
1285 lw->inv_weight = 1;
1286 else
1287 lw->inv_weight = 1 + (WMULT_CONST-lw->weight/2)
1288 / (lw->weight+1);
1289 }
Ingo Molnar45bf76d2007-07-09 18:51:59 +02001290
1291 tmp = (u64)delta_exec * weight;
1292 /*
1293 * Check whether we'd overflow the 64-bit multiplication:
1294 */
Ingo Molnar194081e2007-08-09 11:16:51 +02001295 if (unlikely(tmp > WMULT_CONST))
Ingo Molnarcf2ab462007-09-05 14:32:49 +02001296 tmp = SRR(SRR(tmp, WMULT_SHIFT/2) * lw->inv_weight,
Ingo Molnar194081e2007-08-09 11:16:51 +02001297 WMULT_SHIFT/2);
1298 else
Ingo Molnarcf2ab462007-09-05 14:32:49 +02001299 tmp = SRR(tmp * lw->inv_weight, WMULT_SHIFT);
Ingo Molnar45bf76d2007-07-09 18:51:59 +02001300
Ingo Molnarecf691d2007-08-02 17:41:40 +02001301 return (unsigned long)min(tmp, (u64)(unsigned long)LONG_MAX);
Ingo Molnar45bf76d2007-07-09 18:51:59 +02001302}
1303
Ingo Molnar10919852007-10-15 17:00:04 +02001304static inline void update_load_add(struct load_weight *lw, unsigned long inc)
Ingo Molnar45bf76d2007-07-09 18:51:59 +02001305{
1306 lw->weight += inc;
Ingo Molnare89996a2008-03-14 23:48:28 +01001307 lw->inv_weight = 0;
Ingo Molnar45bf76d2007-07-09 18:51:59 +02001308}
1309
Ingo Molnar10919852007-10-15 17:00:04 +02001310static inline void update_load_sub(struct load_weight *lw, unsigned long dec)
Ingo Molnar45bf76d2007-07-09 18:51:59 +02001311{
1312 lw->weight -= dec;
Ingo Molnare89996a2008-03-14 23:48:28 +01001313 lw->inv_weight = 0;
Ingo Molnar45bf76d2007-07-09 18:51:59 +02001314}
1315
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316/*
Peter Williams2dd73a42006-06-27 02:54:34 -07001317 * To aid in avoiding the subversion of "niceness" due to uneven distribution
1318 * of tasks with abnormal "nice" values across CPUs the contribution that
1319 * each task makes to its run queue's load is weighted according to its
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01001320 * scheduling class and "nice" value. For SCHED_NORMAL tasks this is just a
Peter Williams2dd73a42006-06-27 02:54:34 -07001321 * scaled version of the new time slice allocation that they receive on time
1322 * slice expiry etc.
1323 */
1324
Peter Zijlstracce7ade2009-01-15 14:53:37 +01001325#define WEIGHT_IDLEPRIO 3
1326#define WMULT_IDLEPRIO 1431655765
Ingo Molnardd41f592007-07-09 18:51:59 +02001327
1328/*
1329 * Nice levels are multiplicative, with a gentle 10% change for every
1330 * nice level changed. I.e. when a CPU-bound task goes from nice 0 to
1331 * nice 1, it will get ~10% less CPU time than another CPU-bound task
1332 * that remained on nice 0.
1333 *
1334 * The "10% effect" is relative and cumulative: from _any_ nice level,
1335 * if you go up 1 level, it's -10% CPU usage, if you go down 1 level
Ingo Molnarf9153ee2007-07-16 09:46:30 +02001336 * it's +10% CPU usage. (to achieve that we use a multiplier of 1.25.
1337 * If a task goes up by ~10% and another task goes down by ~10% then
1338 * the relative distance between them is ~25%.)
Ingo Molnardd41f592007-07-09 18:51:59 +02001339 */
1340static const int prio_to_weight[40] = {
Ingo Molnar254753d2007-08-09 11:16:51 +02001341 /* -20 */ 88761, 71755, 56483, 46273, 36291,
1342 /* -15 */ 29154, 23254, 18705, 14949, 11916,
1343 /* -10 */ 9548, 7620, 6100, 4904, 3906,
1344 /* -5 */ 3121, 2501, 1991, 1586, 1277,
1345 /* 0 */ 1024, 820, 655, 526, 423,
1346 /* 5 */ 335, 272, 215, 172, 137,
1347 /* 10 */ 110, 87, 70, 56, 45,
1348 /* 15 */ 36, 29, 23, 18, 15,
Ingo Molnardd41f592007-07-09 18:51:59 +02001349};
1350
Ingo Molnar5714d2d2007-07-16 09:46:31 +02001351/*
1352 * Inverse (2^32/x) values of the prio_to_weight[] array, precalculated.
1353 *
1354 * In cases where the weight does not change often, we can use the
1355 * precalculated inverse to speed up arithmetics by turning divisions
1356 * into multiplications:
1357 */
Ingo Molnardd41f592007-07-09 18:51:59 +02001358static const u32 prio_to_wmult[40] = {
Ingo Molnar254753d2007-08-09 11:16:51 +02001359 /* -20 */ 48388, 59856, 76040, 92818, 118348,
1360 /* -15 */ 147320, 184698, 229616, 287308, 360437,
1361 /* -10 */ 449829, 563644, 704093, 875809, 1099582,
1362 /* -5 */ 1376151, 1717300, 2157191, 2708050, 3363326,
1363 /* 0 */ 4194304, 5237765, 6557202, 8165337, 10153587,
1364 /* 5 */ 12820798, 15790321, 19976592, 24970740, 31350126,
1365 /* 10 */ 39045157, 49367440, 61356676, 76695844, 95443717,
1366 /* 15 */ 119304647, 148102320, 186737708, 238609294, 286331153,
Ingo Molnardd41f592007-07-09 18:51:59 +02001367};
Peter Williams2dd73a42006-06-27 02:54:34 -07001368
Bharata B Raoef12fef2009-03-31 10:02:22 +05301369/* Time spent by the tasks of the cpu accounting group executing in ... */
1370enum cpuacct_stat_index {
1371 CPUACCT_STAT_USER, /* ... user mode */
1372 CPUACCT_STAT_SYSTEM, /* ... kernel mode */
1373
1374 CPUACCT_STAT_NSTATS,
1375};
1376
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01001377#ifdef CONFIG_CGROUP_CPUACCT
1378static void cpuacct_charge(struct task_struct *tsk, u64 cputime);
Bharata B Raoef12fef2009-03-31 10:02:22 +05301379static void cpuacct_update_stats(struct task_struct *tsk,
1380 enum cpuacct_stat_index idx, cputime_t val);
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01001381#else
1382static inline void cpuacct_charge(struct task_struct *tsk, u64 cputime) {}
Bharata B Raoef12fef2009-03-31 10:02:22 +05301383static inline void cpuacct_update_stats(struct task_struct *tsk,
1384 enum cpuacct_stat_index idx, cputime_t val) {}
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01001385#endif
1386
Peter Zijlstra18d95a22008-04-19 19:45:00 +02001387static inline void inc_cpu_load(struct rq *rq, unsigned long load)
1388{
1389 update_load_add(&rq->load, load);
1390}
1391
1392static inline void dec_cpu_load(struct rq *rq, unsigned long load)
1393{
1394 update_load_sub(&rq->load, load);
1395}
1396
Ingo Molnar7940ca32008-08-19 13:40:47 +02001397#if (defined(CONFIG_SMP) && defined(CONFIG_FAIR_GROUP_SCHED)) || defined(CONFIG_RT_GROUP_SCHED)
Peter Zijlstraeb755802008-08-19 12:33:05 +02001398typedef int (*tg_visitor)(struct task_group *, void *);
1399
1400/*
1401 * Iterate the full tree, calling @down when first entering a node and @up when
1402 * leaving it for the final time.
1403 */
1404static int walk_tg_tree(tg_visitor down, tg_visitor up, void *data)
1405{
1406 struct task_group *parent, *child;
1407 int ret;
1408
1409 rcu_read_lock();
1410 parent = &root_task_group;
1411down:
1412 ret = (*down)(parent, data);
1413 if (ret)
1414 goto out_unlock;
1415 list_for_each_entry_rcu(child, &parent->children, siblings) {
1416 parent = child;
1417 goto down;
1418
1419up:
1420 continue;
1421 }
1422 ret = (*up)(parent, data);
1423 if (ret)
1424 goto out_unlock;
1425
1426 child = parent;
1427 parent = parent->parent;
1428 if (parent)
1429 goto up;
1430out_unlock:
1431 rcu_read_unlock();
1432
1433 return ret;
1434}
1435
1436static int tg_nop(struct task_group *tg, void *data)
1437{
1438 return 0;
1439}
1440#endif
1441
Gregory Haskinse7693a32008-01-25 21:08:09 +01001442#ifdef CONFIG_SMP
Peter Zijlstraf5f08f32009-09-10 13:35:28 +02001443/* Used instead of source_load when we know the type == 0 */
1444static unsigned long weighted_cpuload(const int cpu)
1445{
1446 return cpu_rq(cpu)->load.weight;
1447}
1448
1449/*
1450 * Return a low guess at the load of a migration-source cpu weighted
1451 * according to the scheduling class and "nice" value.
1452 *
1453 * We want to under-estimate the load of migration sources, to
1454 * balance conservatively.
1455 */
1456static unsigned long source_load(int cpu, int type)
1457{
1458 struct rq *rq = cpu_rq(cpu);
1459 unsigned long total = weighted_cpuload(cpu);
1460
1461 if (type == 0 || !sched_feat(LB_BIAS))
1462 return total;
1463
1464 return min(rq->cpu_load[type-1], total);
1465}
1466
1467/*
1468 * Return a high guess at the load of a migration-target cpu weighted
1469 * according to the scheduling class and "nice" value.
1470 */
1471static unsigned long target_load(int cpu, int type)
1472{
1473 struct rq *rq = cpu_rq(cpu);
1474 unsigned long total = weighted_cpuload(cpu);
1475
1476 if (type == 0 || !sched_feat(LB_BIAS))
1477 return total;
1478
1479 return max(rq->cpu_load[type-1], total);
1480}
1481
Peter Zijlstraae154be2009-09-10 14:40:57 +02001482static struct sched_group *group_of(int cpu)
1483{
1484 struct sched_domain *sd = rcu_dereference(cpu_rq(cpu)->sd);
1485
1486 if (!sd)
1487 return NULL;
1488
1489 return sd->groups;
1490}
1491
1492static unsigned long power_of(int cpu)
1493{
1494 struct sched_group *group = group_of(cpu);
1495
1496 if (!group)
1497 return SCHED_LOAD_SCALE;
1498
1499 return group->cpu_power;
1500}
1501
Gregory Haskinse7693a32008-01-25 21:08:09 +01001502static int task_hot(struct task_struct *p, u64 now, struct sched_domain *sd);
Peter Zijlstrac09595f2008-06-27 13:41:14 +02001503
Peter Zijlstraa8a51d52008-06-27 13:41:26 +02001504static unsigned long cpu_avg_load_per_task(int cpu)
1505{
1506 struct rq *rq = cpu_rq(cpu);
Ingo Molnaraf6d5962008-11-29 20:45:15 +01001507 unsigned long nr_running = ACCESS_ONCE(rq->nr_running);
Peter Zijlstraa8a51d52008-06-27 13:41:26 +02001508
Steven Rostedt4cd42622008-11-26 21:04:24 -05001509 if (nr_running)
1510 rq->avg_load_per_task = rq->load.weight / nr_running;
Balbir Singha2d47772008-11-12 16:19:00 +05301511 else
1512 rq->avg_load_per_task = 0;
Peter Zijlstraa8a51d52008-06-27 13:41:26 +02001513
1514 return rq->avg_load_per_task;
1515}
1516
Peter Zijlstrac09595f2008-06-27 13:41:14 +02001517#ifdef CONFIG_FAIR_GROUP_SCHED
1518
Jiri Kosina4a6cc4b2009-10-29 00:26:00 +09001519static __read_mostly unsigned long *update_shares_data;
Peter Zijlstra34d76c42009-08-27 13:08:56 +02001520
Peter Zijlstrac09595f2008-06-27 13:41:14 +02001521static void __set_se_shares(struct sched_entity *se, unsigned long shares);
1522
1523/*
1524 * Calculate and set the cpu's group shares.
1525 */
Peter Zijlstra34d76c42009-08-27 13:08:56 +02001526static void update_group_shares_cpu(struct task_group *tg, int cpu,
1527 unsigned long sd_shares,
1528 unsigned long sd_rq_weight,
Jiri Kosina4a6cc4b2009-10-29 00:26:00 +09001529 unsigned long *usd_rq_weight)
Peter Zijlstrac09595f2008-06-27 13:41:14 +02001530{
Peter Zijlstra34d76c42009-08-27 13:08:56 +02001531 unsigned long shares, rq_weight;
Peter Zijlstraa5004272009-07-27 14:04:49 +02001532 int boost = 0;
Peter Zijlstrac09595f2008-06-27 13:41:14 +02001533
Jiri Kosina4a6cc4b2009-10-29 00:26:00 +09001534 rq_weight = usd_rq_weight[cpu];
Peter Zijlstraa5004272009-07-27 14:04:49 +02001535 if (!rq_weight) {
1536 boost = 1;
1537 rq_weight = NICE_0_LOAD;
1538 }
Peter Zijlstrac8cba852008-06-27 13:41:23 +02001539
Peter Zijlstrac09595f2008-06-27 13:41:14 +02001540 /*
Peter Zijlstraa8af7242009-08-21 13:58:54 +02001541 * \Sum_j shares_j * rq_weight_i
1542 * shares_i = -----------------------------
1543 * \Sum_j rq_weight_j
Peter Zijlstrac09595f2008-06-27 13:41:14 +02001544 */
Ken Chenec4e0e22008-11-18 22:41:57 -08001545 shares = (sd_shares * rq_weight) / sd_rq_weight;
Peter Zijlstraffda12a2008-10-17 19:27:02 +02001546 shares = clamp_t(unsigned long, shares, MIN_SHARES, MAX_SHARES);
Peter Zijlstrac09595f2008-06-27 13:41:14 +02001547
Peter Zijlstraffda12a2008-10-17 19:27:02 +02001548 if (abs(shares - tg->se[cpu]->load.weight) >
1549 sysctl_sched_shares_thresh) {
1550 struct rq *rq = cpu_rq(cpu);
1551 unsigned long flags;
Peter Zijlstrac09595f2008-06-27 13:41:14 +02001552
Thomas Gleixner05fa7852009-11-17 14:28:38 +01001553 raw_spin_lock_irqsave(&rq->lock, flags);
Peter Zijlstra34d76c42009-08-27 13:08:56 +02001554 tg->cfs_rq[cpu]->rq_weight = boost ? 0 : rq_weight;
Peter Zijlstraa5004272009-07-27 14:04:49 +02001555 tg->cfs_rq[cpu]->shares = boost ? 0 : shares;
Peter Zijlstraffda12a2008-10-17 19:27:02 +02001556 __set_se_shares(tg->se[cpu], shares);
Thomas Gleixner05fa7852009-11-17 14:28:38 +01001557 raw_spin_unlock_irqrestore(&rq->lock, flags);
Peter Zijlstraffda12a2008-10-17 19:27:02 +02001558 }
Peter Zijlstrac09595f2008-06-27 13:41:14 +02001559}
1560
1561/*
Peter Zijlstrac8cba852008-06-27 13:41:23 +02001562 * Re-compute the task group their per cpu shares over the given domain.
1563 * This needs to be done in a bottom-up fashion because the rq weight of a
1564 * parent group depends on the shares of its child groups.
Peter Zijlstrac09595f2008-06-27 13:41:14 +02001565 */
Peter Zijlstraeb755802008-08-19 12:33:05 +02001566static int tg_shares_up(struct task_group *tg, void *data)
Peter Zijlstrac09595f2008-06-27 13:41:14 +02001567{
Peter Zijlstracd8ad402009-12-03 18:00:07 +01001568 unsigned long weight, rq_weight = 0, sum_weight = 0, shares = 0;
Jiri Kosina4a6cc4b2009-10-29 00:26:00 +09001569 unsigned long *usd_rq_weight;
Peter Zijlstraeb755802008-08-19 12:33:05 +02001570 struct sched_domain *sd = data;
Peter Zijlstra34d76c42009-08-27 13:08:56 +02001571 unsigned long flags;
Peter Zijlstrac09595f2008-06-27 13:41:14 +02001572 int i;
1573
Peter Zijlstra34d76c42009-08-27 13:08:56 +02001574 if (!tg->se[0])
1575 return 0;
1576
1577 local_irq_save(flags);
Jiri Kosina4a6cc4b2009-10-29 00:26:00 +09001578 usd_rq_weight = per_cpu_ptr(update_shares_data, smp_processor_id());
Peter Zijlstra34d76c42009-08-27 13:08:56 +02001579
Rusty Russell758b2cd2008-11-25 02:35:04 +10301580 for_each_cpu(i, sched_domain_span(sd)) {
Peter Zijlstra34d76c42009-08-27 13:08:56 +02001581 weight = tg->cfs_rq[i]->load.weight;
Jiri Kosina4a6cc4b2009-10-29 00:26:00 +09001582 usd_rq_weight[i] = weight;
Peter Zijlstra34d76c42009-08-27 13:08:56 +02001583
Peter Zijlstracd8ad402009-12-03 18:00:07 +01001584 rq_weight += weight;
Ken Chenec4e0e22008-11-18 22:41:57 -08001585 /*
1586 * If there are currently no tasks on the cpu pretend there
1587 * is one of average load so that when a new task gets to
1588 * run here it will not get delayed by group starvation.
1589 */
Ken Chenec4e0e22008-11-18 22:41:57 -08001590 if (!weight)
1591 weight = NICE_0_LOAD;
1592
Peter Zijlstracd8ad402009-12-03 18:00:07 +01001593 sum_weight += weight;
Peter Zijlstrac8cba852008-06-27 13:41:23 +02001594 shares += tg->cfs_rq[i]->shares;
1595 }
1596
Peter Zijlstracd8ad402009-12-03 18:00:07 +01001597 if (!rq_weight)
1598 rq_weight = sum_weight;
1599
Peter Zijlstrac8cba852008-06-27 13:41:23 +02001600 if ((!shares && rq_weight) || shares > tg->shares)
1601 shares = tg->shares;
1602
1603 if (!sd->parent || !(sd->parent->flags & SD_LOAD_BALANCE))
1604 shares = tg->shares;
1605
Rusty Russell758b2cd2008-11-25 02:35:04 +10301606 for_each_cpu(i, sched_domain_span(sd))
Jiri Kosina4a6cc4b2009-10-29 00:26:00 +09001607 update_group_shares_cpu(tg, i, shares, rq_weight, usd_rq_weight);
Peter Zijlstra34d76c42009-08-27 13:08:56 +02001608
1609 local_irq_restore(flags);
Peter Zijlstraeb755802008-08-19 12:33:05 +02001610
1611 return 0;
Peter Zijlstrac09595f2008-06-27 13:41:14 +02001612}
1613
1614/*
Peter Zijlstrac8cba852008-06-27 13:41:23 +02001615 * Compute the cpu's hierarchical load factor for each task group.
1616 * This needs to be done in a top-down fashion because the load of a child
1617 * group is a fraction of its parents load.
Peter Zijlstrac09595f2008-06-27 13:41:14 +02001618 */
Peter Zijlstraeb755802008-08-19 12:33:05 +02001619static int tg_load_down(struct task_group *tg, void *data)
Peter Zijlstrac09595f2008-06-27 13:41:14 +02001620{
Peter Zijlstrac8cba852008-06-27 13:41:23 +02001621 unsigned long load;
Peter Zijlstraeb755802008-08-19 12:33:05 +02001622 long cpu = (long)data;
Peter Zijlstrac09595f2008-06-27 13:41:14 +02001623
Peter Zijlstrac8cba852008-06-27 13:41:23 +02001624 if (!tg->parent) {
1625 load = cpu_rq(cpu)->load.weight;
1626 } else {
1627 load = tg->parent->cfs_rq[cpu]->h_load;
1628 load *= tg->cfs_rq[cpu]->shares;
1629 load /= tg->parent->cfs_rq[cpu]->load.weight + 1;
1630 }
1631
1632 tg->cfs_rq[cpu]->h_load = load;
Peter Zijlstrac09595f2008-06-27 13:41:14 +02001633
Peter Zijlstraeb755802008-08-19 12:33:05 +02001634 return 0;
Peter Zijlstra4d8d5952008-06-27 13:41:19 +02001635}
1636
Peter Zijlstrac8cba852008-06-27 13:41:23 +02001637static void update_shares(struct sched_domain *sd)
Peter Zijlstrac09595f2008-06-27 13:41:14 +02001638{
Peter Zijlstrae7097152009-06-03 15:41:20 +02001639 s64 elapsed;
1640 u64 now;
1641
1642 if (root_task_group_empty())
1643 return;
1644
1645 now = cpu_clock(raw_smp_processor_id());
1646 elapsed = now - sd->last_update;
Peter Zijlstra2398f2c2008-06-27 13:41:35 +02001647
1648 if (elapsed >= (s64)(u64)sysctl_sched_shares_ratelimit) {
1649 sd->last_update = now;
Peter Zijlstraeb755802008-08-19 12:33:05 +02001650 walk_tg_tree(tg_nop, tg_shares_up, sd);
Peter Zijlstra2398f2c2008-06-27 13:41:35 +02001651 }
Peter Zijlstrac09595f2008-06-27 13:41:14 +02001652}
1653
Peter Zijlstraeb755802008-08-19 12:33:05 +02001654static void update_h_load(long cpu)
Peter Zijlstrac09595f2008-06-27 13:41:14 +02001655{
Peter Zijlstrae7097152009-06-03 15:41:20 +02001656 if (root_task_group_empty())
1657 return;
1658
Peter Zijlstraeb755802008-08-19 12:33:05 +02001659 walk_tg_tree(tg_load_down, tg_nop, (void *)cpu);
Peter Zijlstrac09595f2008-06-27 13:41:14 +02001660}
1661
Peter Zijlstrac09595f2008-06-27 13:41:14 +02001662#else
1663
Peter Zijlstrac8cba852008-06-27 13:41:23 +02001664static inline void update_shares(struct sched_domain *sd)
Peter Zijlstrac09595f2008-06-27 13:41:14 +02001665{
1666}
1667
Peter Zijlstrac09595f2008-06-27 13:41:14 +02001668#endif
1669
Gregory Haskins8f45e2b2008-12-29 09:39:51 -05001670#ifdef CONFIG_PREEMPT
1671
Peter Zijlstrab78bb862009-09-15 14:23:18 +02001672static void double_rq_lock(struct rq *rq1, struct rq *rq2);
1673
Alexey Dobriyan70574a92008-11-28 22:08:00 +03001674/*
Gregory Haskins8f45e2b2008-12-29 09:39:51 -05001675 * fair double_lock_balance: Safely acquires both rq->locks in a fair
1676 * way at the expense of forcing extra atomic operations in all
1677 * invocations. This assures that the double_lock is acquired using the
1678 * same underlying policy as the spinlock_t on this architecture, which
1679 * reduces latency compared to the unfair variant below. However, it
1680 * also adds more overhead and therefore may reduce throughput.
Alexey Dobriyan70574a92008-11-28 22:08:00 +03001681 */
Gregory Haskins8f45e2b2008-12-29 09:39:51 -05001682static inline int _double_lock_balance(struct rq *this_rq, struct rq *busiest)
1683 __releases(this_rq->lock)
1684 __acquires(busiest->lock)
1685 __acquires(this_rq->lock)
1686{
Thomas Gleixner05fa7852009-11-17 14:28:38 +01001687 raw_spin_unlock(&this_rq->lock);
Gregory Haskins8f45e2b2008-12-29 09:39:51 -05001688 double_rq_lock(this_rq, busiest);
1689
1690 return 1;
1691}
1692
1693#else
1694/*
1695 * Unfair double_lock_balance: Optimizes throughput at the expense of
1696 * latency by eliminating extra atomic operations when the locks are
1697 * already in proper order on entry. This favors lower cpu-ids and will
1698 * grant the double lock to lower cpus over higher ids under contention,
1699 * regardless of entry order into the function.
1700 */
1701static int _double_lock_balance(struct rq *this_rq, struct rq *busiest)
Alexey Dobriyan70574a92008-11-28 22:08:00 +03001702 __releases(this_rq->lock)
1703 __acquires(busiest->lock)
1704 __acquires(this_rq->lock)
1705{
1706 int ret = 0;
1707
Thomas Gleixner05fa7852009-11-17 14:28:38 +01001708 if (unlikely(!raw_spin_trylock(&busiest->lock))) {
Alexey Dobriyan70574a92008-11-28 22:08:00 +03001709 if (busiest < this_rq) {
Thomas Gleixner05fa7852009-11-17 14:28:38 +01001710 raw_spin_unlock(&this_rq->lock);
1711 raw_spin_lock(&busiest->lock);
1712 raw_spin_lock_nested(&this_rq->lock,
1713 SINGLE_DEPTH_NESTING);
Alexey Dobriyan70574a92008-11-28 22:08:00 +03001714 ret = 1;
1715 } else
Thomas Gleixner05fa7852009-11-17 14:28:38 +01001716 raw_spin_lock_nested(&busiest->lock,
1717 SINGLE_DEPTH_NESTING);
Alexey Dobriyan70574a92008-11-28 22:08:00 +03001718 }
1719 return ret;
1720}
1721
Gregory Haskins8f45e2b2008-12-29 09:39:51 -05001722#endif /* CONFIG_PREEMPT */
1723
1724/*
1725 * double_lock_balance - lock the busiest runqueue, this_rq is locked already.
1726 */
1727static int double_lock_balance(struct rq *this_rq, struct rq *busiest)
1728{
1729 if (unlikely(!irqs_disabled())) {
1730 /* printk() doesn't work good under rq->lock */
Thomas Gleixner05fa7852009-11-17 14:28:38 +01001731 raw_spin_unlock(&this_rq->lock);
Gregory Haskins8f45e2b2008-12-29 09:39:51 -05001732 BUG_ON(1);
1733 }
1734
1735 return _double_lock_balance(this_rq, busiest);
1736}
1737
Alexey Dobriyan70574a92008-11-28 22:08:00 +03001738static inline void double_unlock_balance(struct rq *this_rq, struct rq *busiest)
1739 __releases(busiest->lock)
1740{
Thomas Gleixner05fa7852009-11-17 14:28:38 +01001741 raw_spin_unlock(&busiest->lock);
Alexey Dobriyan70574a92008-11-28 22:08:00 +03001742 lock_set_subclass(&this_rq->lock.dep_map, 0, _RET_IP_);
1743}
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01001744
1745/*
1746 * double_rq_lock - safely lock two runqueues
1747 *
1748 * Note this does not disable interrupts like task_rq_lock,
1749 * you need to do so manually before calling.
1750 */
1751static void double_rq_lock(struct rq *rq1, struct rq *rq2)
1752 __acquires(rq1->lock)
1753 __acquires(rq2->lock)
1754{
1755 BUG_ON(!irqs_disabled());
1756 if (rq1 == rq2) {
1757 raw_spin_lock(&rq1->lock);
1758 __acquire(rq2->lock); /* Fake it out ;) */
1759 } else {
1760 if (rq1 < rq2) {
1761 raw_spin_lock(&rq1->lock);
1762 raw_spin_lock_nested(&rq2->lock, SINGLE_DEPTH_NESTING);
1763 } else {
1764 raw_spin_lock(&rq2->lock);
1765 raw_spin_lock_nested(&rq1->lock, SINGLE_DEPTH_NESTING);
1766 }
1767 }
1768 update_rq_clock(rq1);
1769 update_rq_clock(rq2);
1770}
1771
1772/*
1773 * double_rq_unlock - safely unlock two runqueues
1774 *
1775 * Note this does not restore interrupts like task_rq_unlock,
1776 * you need to do so manually after calling.
1777 */
1778static void double_rq_unlock(struct rq *rq1, struct rq *rq2)
1779 __releases(rq1->lock)
1780 __releases(rq2->lock)
1781{
1782 raw_spin_unlock(&rq1->lock);
1783 if (rq1 != rq2)
1784 raw_spin_unlock(&rq2->lock);
1785 else
1786 __release(rq2->lock);
1787}
1788
Peter Zijlstra18d95a22008-04-19 19:45:00 +02001789#endif
Peter Zijlstra18d95a22008-04-19 19:45:00 +02001790
1791#ifdef CONFIG_FAIR_GROUP_SCHED
1792static void cfs_rq_set_shares(struct cfs_rq *cfs_rq, unsigned long shares)
1793{
Vegard Nossum30432092008-06-27 21:35:50 +02001794#ifdef CONFIG_SMP
Ingo Molnar34e83e82008-06-27 15:42:36 +02001795 cfs_rq->shares = shares;
1796#endif
Peter Zijlstra18d95a22008-04-19 19:45:00 +02001797}
1798#endif
1799
Thomas Gleixnerdce48a82009-04-11 10:43:41 +02001800static void calc_load_account_active(struct rq *this_rq);
Christian Ehrhardt0bcdcf22009-11-30 12:16:46 +01001801static void update_sysctl(void);
Christian Ehrhardtacb4a842009-11-30 12:16:48 +01001802static int get_update_sysctl_factor(void);
Thomas Gleixnerdce48a82009-04-11 10:43:41 +02001803
Peter Zijlstracd29fe62009-11-27 17:32:46 +01001804static inline void __set_task_cpu(struct task_struct *p, unsigned int cpu)
1805{
1806 set_task_rq(p, cpu);
1807#ifdef CONFIG_SMP
1808 /*
1809 * After ->cpu is set up to a new value, task_rq_lock(p, ...) can be
1810 * successfuly executed on another CPU. We must ensure that updates of
1811 * per-task data have been completed by this moment.
1812 */
1813 smp_wmb();
1814 task_thread_info(p)->cpu = cpu;
1815#endif
1816}
Gregory Haskinse7693a32008-01-25 21:08:09 +01001817
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01001818static const struct sched_class rt_sched_class;
Ingo Molnardd41f592007-07-09 18:51:59 +02001819
1820#define sched_class_highest (&rt_sched_class)
Gregory Haskins1f11eb62008-06-04 15:04:05 -04001821#define for_each_class(class) \
1822 for (class = sched_class_highest; class; class = class->next)
Ingo Molnardd41f592007-07-09 18:51:59 +02001823
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01001824#include "sched_stats.h"
1825
Peter Zijlstrac09595f2008-06-27 13:41:14 +02001826static void inc_nr_running(struct rq *rq)
Ingo Molnar6363ca52008-05-29 11:28:57 +02001827{
1828 rq->nr_running++;
Ingo Molnar6363ca52008-05-29 11:28:57 +02001829}
1830
Peter Zijlstrac09595f2008-06-27 13:41:14 +02001831static void dec_nr_running(struct rq *rq)
Ingo Molnar9c217242007-08-02 17:41:40 +02001832{
1833 rq->nr_running--;
Ingo Molnar9c217242007-08-02 17:41:40 +02001834}
1835
Ingo Molnar45bf76d2007-07-09 18:51:59 +02001836static void set_load_weight(struct task_struct *p)
1837{
1838 if (task_has_rt_policy(p)) {
Ingo Molnardd41f592007-07-09 18:51:59 +02001839 p->se.load.weight = prio_to_weight[0] * 2;
1840 p->se.load.inv_weight = prio_to_wmult[0] >> 1;
1841 return;
1842 }
1843
1844 /*
1845 * SCHED_IDLE tasks get minimal weight:
1846 */
1847 if (p->policy == SCHED_IDLE) {
1848 p->se.load.weight = WEIGHT_IDLEPRIO;
1849 p->se.load.inv_weight = WMULT_IDLEPRIO;
1850 return;
1851 }
1852
1853 p->se.load.weight = prio_to_weight[p->static_prio - MAX_RT_PRIO];
1854 p->se.load.inv_weight = prio_to_wmult[p->static_prio - MAX_RT_PRIO];
Ingo Molnar45bf76d2007-07-09 18:51:59 +02001855}
1856
Gregory Haskins2087a1a2008-06-27 14:30:00 -06001857static void update_avg(u64 *avg, u64 sample)
1858{
1859 s64 diff = sample - *avg;
1860 *avg += diff >> 3;
1861}
1862
Thomas Gleixnerea87bb72010-01-20 20:58:57 +00001863static void
1864enqueue_task(struct rq *rq, struct task_struct *p, int wakeup, bool head)
Ingo Molnar71f8bd42007-07-09 18:51:59 +02001865{
Peter Zijlstra831451a2009-01-14 12:39:18 +01001866 if (wakeup)
1867 p->se.start_runtime = p->se.sum_exec_runtime;
1868
Ingo Molnar71f8bd42007-07-09 18:51:59 +02001869 sched_info_queued(p);
Thomas Gleixnerea87bb72010-01-20 20:58:57 +00001870 p->sched_class->enqueue_task(rq, p, wakeup, head);
Ingo Molnardd41f592007-07-09 18:51:59 +02001871 p->se.on_rq = 1;
1872}
1873
Ingo Molnar69be72c2007-08-09 11:16:49 +02001874static void dequeue_task(struct rq *rq, struct task_struct *p, int sleep)
Ingo Molnardd41f592007-07-09 18:51:59 +02001875{
Peter Zijlstra831451a2009-01-14 12:39:18 +01001876 if (sleep) {
1877 if (p->se.last_wakeup) {
1878 update_avg(&p->se.avg_overlap,
1879 p->se.sum_exec_runtime - p->se.last_wakeup);
1880 p->se.last_wakeup = 0;
1881 } else {
1882 update_avg(&p->se.avg_wakeup,
1883 sysctl_sched_wakeup_granularity);
1884 }
Gregory Haskins2087a1a2008-06-27 14:30:00 -06001885 }
1886
Ankita Garg46ac22b2008-07-01 14:30:06 +05301887 sched_info_dequeued(p);
Ingo Molnarf02231e2007-08-09 11:16:48 +02001888 p->sched_class->dequeue_task(rq, p, sleep);
Ingo Molnardd41f592007-07-09 18:51:59 +02001889 p->se.on_rq = 0;
Ingo Molnar71f8bd42007-07-09 18:51:59 +02001890}
1891
1892/*
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01001893 * activate_task - move a task to the runqueue.
1894 */
1895static void activate_task(struct rq *rq, struct task_struct *p, int wakeup)
1896{
1897 if (task_contributes_to_load(p))
1898 rq->nr_uninterruptible--;
1899
Thomas Gleixnerea87bb72010-01-20 20:58:57 +00001900 enqueue_task(rq, p, wakeup, false);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01001901 inc_nr_running(rq);
1902}
1903
1904/*
1905 * deactivate_task - remove a task from the runqueue.
1906 */
1907static void deactivate_task(struct rq *rq, struct task_struct *p, int sleep)
1908{
1909 if (task_contributes_to_load(p))
1910 rq->nr_uninterruptible++;
1911
1912 dequeue_task(rq, p, sleep);
1913 dec_nr_running(rq);
1914}
1915
1916#include "sched_idletask.c"
1917#include "sched_fair.c"
1918#include "sched_rt.c"
1919#ifdef CONFIG_SCHED_DEBUG
1920# include "sched_debug.c"
1921#endif
1922
1923/*
Ingo Molnardd41f592007-07-09 18:51:59 +02001924 * __normal_prio - return the priority that is based on the static prio
Ingo Molnar71f8bd42007-07-09 18:51:59 +02001925 */
Ingo Molnar14531182007-07-09 18:51:59 +02001926static inline int __normal_prio(struct task_struct *p)
1927{
Ingo Molnardd41f592007-07-09 18:51:59 +02001928 return p->static_prio;
Ingo Molnar14531182007-07-09 18:51:59 +02001929}
1930
1931/*
Ingo Molnarb29739f2006-06-27 02:54:51 -07001932 * Calculate the expected normal priority: i.e. priority
1933 * without taking RT-inheritance into account. Might be
1934 * boosted by interactivity modifiers. Changes upon fork,
1935 * setprio syscalls, and whenever the interactivity
1936 * estimator recalculates.
1937 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07001938static inline int normal_prio(struct task_struct *p)
Ingo Molnarb29739f2006-06-27 02:54:51 -07001939{
1940 int prio;
1941
Ingo Molnare05606d2007-07-09 18:51:59 +02001942 if (task_has_rt_policy(p))
Ingo Molnarb29739f2006-06-27 02:54:51 -07001943 prio = MAX_RT_PRIO-1 - p->rt_priority;
1944 else
1945 prio = __normal_prio(p);
1946 return prio;
1947}
1948
1949/*
1950 * Calculate the current priority, i.e. the priority
1951 * taken into account by the scheduler. This value might
1952 * be boosted by RT tasks, or might be boosted by
1953 * interactivity modifiers. Will be RT if the task got
1954 * RT-boosted. If not then it returns p->normal_prio.
1955 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07001956static int effective_prio(struct task_struct *p)
Ingo Molnarb29739f2006-06-27 02:54:51 -07001957{
1958 p->normal_prio = normal_prio(p);
1959 /*
1960 * If we are RT tasks or we were boosted to RT priority,
1961 * keep the priority unchanged. Otherwise, update priority
1962 * to the normal priority:
1963 */
1964 if (!rt_prio(p->prio))
1965 return p->normal_prio;
1966 return p->prio;
1967}
1968
Linus Torvalds1da177e2005-04-16 15:20:36 -07001969/**
1970 * task_curr - is this task currently executing on a CPU?
1971 * @p: the task in question.
1972 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07001973inline int task_curr(const struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001974{
1975 return cpu_curr(task_cpu(p)) == p;
1976}
1977
Steven Rostedtcb469842008-01-25 21:08:22 +01001978static inline void check_class_changed(struct rq *rq, struct task_struct *p,
1979 const struct sched_class *prev_class,
1980 int oldprio, int running)
1981{
1982 if (prev_class != p->sched_class) {
1983 if (prev_class->switched_from)
1984 prev_class->switched_from(rq, p, running);
1985 p->sched_class->switched_to(rq, p, running);
1986 } else
1987 p->sched_class->prio_changed(rq, p, oldprio, running);
1988}
1989
Linus Torvalds1da177e2005-04-16 15:20:36 -07001990#ifdef CONFIG_SMP
Ingo Molnarcc367732007-10-15 17:00:18 +02001991/*
1992 * Is this task likely cache-hot:
1993 */
Gregory Haskinse7693a32008-01-25 21:08:09 +01001994static int
Ingo Molnarcc367732007-10-15 17:00:18 +02001995task_hot(struct task_struct *p, u64 now, struct sched_domain *sd)
1996{
1997 s64 delta;
1998
Peter Zijlstrae6c8fba2009-12-16 18:04:33 +01001999 if (p->sched_class != &fair_sched_class)
2000 return 0;
2001
Ingo Molnarf540a602008-03-15 17:10:34 +01002002 /*
2003 * Buddy candidates are cache hot:
2004 */
Mike Galbraithf685cea2009-10-23 23:09:22 +02002005 if (sched_feat(CACHE_HOT_BUDDY) && this_rq()->nr_running &&
Peter Zijlstra47932412008-11-04 21:25:09 +01002006 (&p->se == cfs_rq_of(&p->se)->next ||
2007 &p->se == cfs_rq_of(&p->se)->last))
Ingo Molnarf540a602008-03-15 17:10:34 +01002008 return 1;
2009
Ingo Molnar6bc16652007-10-15 17:00:18 +02002010 if (sysctl_sched_migration_cost == -1)
2011 return 1;
2012 if (sysctl_sched_migration_cost == 0)
2013 return 0;
2014
Ingo Molnarcc367732007-10-15 17:00:18 +02002015 delta = now - p->se.exec_start;
2016
2017 return delta < (s64)sysctl_sched_migration_cost;
2018}
2019
Ingo Molnardd41f592007-07-09 18:51:59 +02002020void set_task_cpu(struct task_struct *p, unsigned int new_cpu)
Ingo Molnarc65cc872007-07-09 18:51:58 +02002021{
Peter Zijlstrae2912002009-12-16 18:04:36 +01002022#ifdef CONFIG_SCHED_DEBUG
2023 /*
2024 * We should never call set_task_cpu() on a blocked task,
2025 * ttwu() will sort out the placement.
2026 */
Peter Zijlstra077614e2009-12-17 13:16:31 +01002027 WARN_ON_ONCE(p->state != TASK_RUNNING && p->state != TASK_WAKING &&
2028 !(task_thread_info(p)->preempt_count & PREEMPT_ACTIVE));
Peter Zijlstrae2912002009-12-16 18:04:36 +01002029#endif
2030
Mathieu Desnoyersde1d7282009-05-05 16:49:59 +08002031 trace_sched_migrate_task(p, new_cpu);
Peter Zijlstracbc34ed2008-12-10 08:08:22 +01002032
Peter Zijlstra0c697742009-12-22 15:43:19 +01002033 if (task_cpu(p) != new_cpu) {
2034 p->se.nr_migrations++;
2035 perf_sw_event(PERF_COUNT_SW_CPU_MIGRATIONS, 1, 1, NULL, 0);
2036 }
Ingo Molnardd41f592007-07-09 18:51:59 +02002037
2038 __set_task_cpu(p, new_cpu);
Ingo Molnarc65cc872007-07-09 18:51:58 +02002039}
2040
Ingo Molnar70b97a72006-07-03 00:25:42 -07002041struct migration_req {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002042 struct list_head list;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002043
Ingo Molnar36c8b582006-07-03 00:25:41 -07002044 struct task_struct *task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002045 int dest_cpu;
2046
Linus Torvalds1da177e2005-04-16 15:20:36 -07002047 struct completion done;
Ingo Molnar70b97a72006-07-03 00:25:42 -07002048};
Linus Torvalds1da177e2005-04-16 15:20:36 -07002049
2050/*
2051 * The task's runqueue lock must be held.
2052 * Returns true if you have to wait for migration thread.
2053 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07002054static int
Ingo Molnar70b97a72006-07-03 00:25:42 -07002055migrate_task(struct task_struct *p, int dest_cpu, struct migration_req *req)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002056{
Ingo Molnar70b97a72006-07-03 00:25:42 -07002057 struct rq *rq = task_rq(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002058
2059 /*
2060 * If the task is not on a runqueue (and not running), then
Peter Zijlstrae2912002009-12-16 18:04:36 +01002061 * the next wake-up will properly place the task.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002062 */
Peter Zijlstrae2912002009-12-16 18:04:36 +01002063 if (!p->se.on_rq && !task_running(rq, p))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002064 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002065
2066 init_completion(&req->done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002067 req->task = p;
2068 req->dest_cpu = dest_cpu;
2069 list_add(&req->list, &rq->migration_queue);
Ingo Molnar48f24c42006-07-03 00:25:40 -07002070
Linus Torvalds1da177e2005-04-16 15:20:36 -07002071 return 1;
2072}
2073
2074/*
Markus Metzgera26b89f2009-04-03 16:43:34 +02002075 * wait_task_context_switch - wait for a thread to complete at least one
2076 * context switch.
2077 *
2078 * @p must not be current.
2079 */
2080void wait_task_context_switch(struct task_struct *p)
2081{
2082 unsigned long nvcsw, nivcsw, flags;
2083 int running;
2084 struct rq *rq;
2085
2086 nvcsw = p->nvcsw;
2087 nivcsw = p->nivcsw;
2088 for (;;) {
2089 /*
2090 * The runqueue is assigned before the actual context
2091 * switch. We need to take the runqueue lock.
2092 *
2093 * We could check initially without the lock but it is
2094 * very likely that we need to take the lock in every
2095 * iteration.
2096 */
2097 rq = task_rq_lock(p, &flags);
2098 running = task_running(rq, p);
2099 task_rq_unlock(rq, &flags);
2100
2101 if (likely(!running))
2102 break;
2103 /*
2104 * The switch count is incremented before the actual
2105 * context switch. We thus wait for two switches to be
2106 * sure at least one completed.
2107 */
2108 if ((p->nvcsw - nvcsw) > 1)
2109 break;
2110 if ((p->nivcsw - nivcsw) > 1)
2111 break;
2112
2113 cpu_relax();
2114 }
2115}
2116
2117/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002118 * wait_task_inactive - wait for a thread to unschedule.
2119 *
Roland McGrath85ba2d82008-07-25 19:45:58 -07002120 * If @match_state is nonzero, it's the @p->state value just checked and
2121 * not expected to change. If it changes, i.e. @p might have woken up,
2122 * then return zero. When we succeed in waiting for @p to be off its CPU,
2123 * we return a positive number (its total switch count). If a second call
2124 * a short while later returns the same number, the caller can be sure that
2125 * @p has remained unscheduled the whole time.
2126 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002127 * The caller must ensure that the task *will* unschedule sometime soon,
2128 * else this function might spin for a *long* time. This function can't
2129 * be called with interrupts off, or it may introduce deadlock with
2130 * smp_call_function() if an IPI is sent by the same process we are
2131 * waiting to become inactive.
2132 */
Roland McGrath85ba2d82008-07-25 19:45:58 -07002133unsigned long wait_task_inactive(struct task_struct *p, long match_state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002134{
2135 unsigned long flags;
Ingo Molnardd41f592007-07-09 18:51:59 +02002136 int running, on_rq;
Roland McGrath85ba2d82008-07-25 19:45:58 -07002137 unsigned long ncsw;
Ingo Molnar70b97a72006-07-03 00:25:42 -07002138 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002139
Andi Kleen3a5c3592007-10-15 17:00:14 +02002140 for (;;) {
2141 /*
2142 * We do the initial early heuristics without holding
2143 * any task-queue locks at all. We'll only try to get
2144 * the runqueue lock when things look like they will
2145 * work out!
2146 */
2147 rq = task_rq(p);
Linus Torvaldsfa490cf2007-06-18 09:34:40 -07002148
Andi Kleen3a5c3592007-10-15 17:00:14 +02002149 /*
2150 * If the task is actively running on another CPU
2151 * still, just relax and busy-wait without holding
2152 * any locks.
2153 *
2154 * NOTE! Since we don't hold any locks, it's not
2155 * even sure that "rq" stays as the right runqueue!
2156 * But we don't care, since "task_running()" will
2157 * return false if the runqueue has changed and p
2158 * is actually now running somewhere else!
2159 */
Roland McGrath85ba2d82008-07-25 19:45:58 -07002160 while (task_running(rq, p)) {
2161 if (match_state && unlikely(p->state != match_state))
2162 return 0;
Andi Kleen3a5c3592007-10-15 17:00:14 +02002163 cpu_relax();
Roland McGrath85ba2d82008-07-25 19:45:58 -07002164 }
Linus Torvaldsfa490cf2007-06-18 09:34:40 -07002165
Andi Kleen3a5c3592007-10-15 17:00:14 +02002166 /*
2167 * Ok, time to look more closely! We need the rq
2168 * lock now, to be *sure*. If we're wrong, we'll
2169 * just go back and repeat.
2170 */
2171 rq = task_rq_lock(p, &flags);
Mathieu Desnoyers0a16b602008-07-18 12:16:17 -04002172 trace_sched_wait_task(rq, p);
Andi Kleen3a5c3592007-10-15 17:00:14 +02002173 running = task_running(rq, p);
2174 on_rq = p->se.on_rq;
Roland McGrath85ba2d82008-07-25 19:45:58 -07002175 ncsw = 0;
Oleg Nesterovf31e11d2008-08-20 16:54:44 -07002176 if (!match_state || p->state == match_state)
Oleg Nesterov93dcf552008-08-20 16:54:44 -07002177 ncsw = p->nvcsw | LONG_MIN; /* sets MSB */
Andi Kleen3a5c3592007-10-15 17:00:14 +02002178 task_rq_unlock(rq, &flags);
Linus Torvaldsfa490cf2007-06-18 09:34:40 -07002179
Andi Kleen3a5c3592007-10-15 17:00:14 +02002180 /*
Roland McGrath85ba2d82008-07-25 19:45:58 -07002181 * If it changed from the expected state, bail out now.
2182 */
2183 if (unlikely(!ncsw))
2184 break;
2185
2186 /*
Andi Kleen3a5c3592007-10-15 17:00:14 +02002187 * Was it really running after all now that we
2188 * checked with the proper locks actually held?
2189 *
2190 * Oops. Go back and try again..
2191 */
2192 if (unlikely(running)) {
2193 cpu_relax();
2194 continue;
2195 }
2196
2197 /*
2198 * It's not enough that it's not actively running,
2199 * it must be off the runqueue _entirely_, and not
2200 * preempted!
2201 *
Luis Henriques80dd99b2009-03-16 19:58:09 +00002202 * So if it was still runnable (but just not actively
Andi Kleen3a5c3592007-10-15 17:00:14 +02002203 * running right now), it's preempted, and we should
2204 * yield - it could be a while.
2205 */
2206 if (unlikely(on_rq)) {
2207 schedule_timeout_uninterruptible(1);
2208 continue;
2209 }
2210
2211 /*
2212 * Ahh, all good. It wasn't running, and it wasn't
2213 * runnable, which means that it will never become
2214 * running in the future either. We're all done!
2215 */
2216 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002217 }
Roland McGrath85ba2d82008-07-25 19:45:58 -07002218
2219 return ncsw;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002220}
2221
2222/***
2223 * kick_process - kick a running thread to enter/exit the kernel
2224 * @p: the to-be-kicked thread
2225 *
2226 * Cause a process which is running on another CPU to enter
2227 * kernel-mode, without any delay. (to get signals handled.)
2228 *
2229 * NOTE: this function doesnt have to take the runqueue lock,
2230 * because all it wants to ensure is that the remote task enters
2231 * the kernel. If the IPI races and the task has been migrated
2232 * to another CPU then no harm is done and the purpose has been
2233 * achieved as well.
2234 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07002235void kick_process(struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002236{
2237 int cpu;
2238
2239 preempt_disable();
2240 cpu = task_cpu(p);
2241 if ((cpu != smp_processor_id()) && task_curr(p))
2242 smp_send_reschedule(cpu);
2243 preempt_enable();
2244}
Rusty Russellb43e3522009-06-12 22:27:00 -06002245EXPORT_SYMBOL_GPL(kick_process);
Nick Piggin476d1392005-06-25 14:57:29 -07002246#endif /* CONFIG_SMP */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002247
Thomas Gleixner0793a612008-12-04 20:12:29 +01002248/**
2249 * task_oncpu_function_call - call a function on the cpu on which a task runs
2250 * @p: the task to evaluate
2251 * @func: the function to be called
2252 * @info: the function call argument
2253 *
2254 * Calls the function @func when the task is currently running. This might
2255 * be on the current CPU, which just calls the function directly
2256 */
2257void task_oncpu_function_call(struct task_struct *p,
2258 void (*func) (void *info), void *info)
2259{
2260 int cpu;
2261
2262 preempt_disable();
2263 cpu = task_cpu(p);
2264 if (task_curr(p))
2265 smp_call_function_single(cpu, func, info, 1);
2266 preempt_enable();
2267}
2268
Peter Zijlstra970b13b2009-11-25 13:31:39 +01002269#ifdef CONFIG_SMP
Peter Zijlstra5da9a0f2009-12-16 18:04:38 +01002270static int select_fallback_rq(int cpu, struct task_struct *p)
2271{
2272 int dest_cpu;
2273 const struct cpumask *nodemask = cpumask_of_node(cpu_to_node(cpu));
2274
2275 /* Look for allowed, online CPU in same node. */
2276 for_each_cpu_and(dest_cpu, nodemask, cpu_active_mask)
2277 if (cpumask_test_cpu(dest_cpu, &p->cpus_allowed))
2278 return dest_cpu;
2279
2280 /* Any allowed, online CPU? */
2281 dest_cpu = cpumask_any_and(&p->cpus_allowed, cpu_active_mask);
2282 if (dest_cpu < nr_cpu_ids)
2283 return dest_cpu;
2284
2285 /* No more Mr. Nice Guy. */
2286 if (dest_cpu >= nr_cpu_ids) {
2287 rcu_read_lock();
2288 cpuset_cpus_allowed_locked(p, &p->cpus_allowed);
2289 rcu_read_unlock();
2290 dest_cpu = cpumask_any_and(cpu_active_mask, &p->cpus_allowed);
2291
2292 /*
2293 * Don't tell them about moving exiting tasks or
2294 * kernel threads (both mm NULL), since they never
2295 * leave kernel.
2296 */
2297 if (p->mm && printk_ratelimit()) {
2298 printk(KERN_INFO "process %d (%s) no "
2299 "longer affine to cpu%d\n",
2300 task_pid_nr(p), p->comm, cpu);
2301 }
2302 }
2303
2304 return dest_cpu;
2305}
2306
Peter Zijlstrae2912002009-12-16 18:04:36 +01002307/*
Peter Zijlstrafabf3182010-01-21 21:04:57 +01002308 * Gets called from 3 sites (exec, fork, wakeup), since it is called without
2309 * holding rq->lock we need to ensure ->cpus_allowed is stable, this is done
2310 * by:
Peter Zijlstrae2912002009-12-16 18:04:36 +01002311 *
Peter Zijlstrafabf3182010-01-21 21:04:57 +01002312 * exec: is unstable, retry loop
2313 * fork & wake-up: serialize ->cpus_allowed against TASK_WAKING
Peter Zijlstrae2912002009-12-16 18:04:36 +01002314 */
Peter Zijlstra970b13b2009-11-25 13:31:39 +01002315static inline
2316int select_task_rq(struct task_struct *p, int sd_flags, int wake_flags)
2317{
Peter Zijlstrae2912002009-12-16 18:04:36 +01002318 int cpu = p->sched_class->select_task_rq(p, sd_flags, wake_flags);
2319
2320 /*
2321 * In order not to call set_task_cpu() on a blocking task we need
2322 * to rely on ttwu() to place the task on a valid ->cpus_allowed
2323 * cpu.
2324 *
2325 * Since this is common to all placement strategies, this lives here.
2326 *
2327 * [ this allows ->select_task() to simply return task_cpu(p) and
2328 * not worry about this generic constraint ]
2329 */
2330 if (unlikely(!cpumask_test_cpu(cpu, &p->cpus_allowed) ||
Peter Zijlstra70f11202009-12-20 17:36:27 +01002331 !cpu_online(cpu)))
Peter Zijlstra5da9a0f2009-12-16 18:04:38 +01002332 cpu = select_fallback_rq(task_cpu(p), p);
Peter Zijlstrae2912002009-12-16 18:04:36 +01002333
2334 return cpu;
Peter Zijlstra970b13b2009-11-25 13:31:39 +01002335}
2336#endif
2337
Linus Torvalds1da177e2005-04-16 15:20:36 -07002338/***
2339 * try_to_wake_up - wake up a thread
2340 * @p: the to-be-woken-up thread
2341 * @state: the mask of task states that can be woken
2342 * @sync: do a synchronous wakeup?
2343 *
2344 * Put it on the run-queue if it's not already there. The "current"
2345 * thread is always on the run-queue (except when the actual
2346 * re-schedule is in progress), and as such you're allowed to do
2347 * the simpler "current->state = TASK_RUNNING" to mark yourself
2348 * runnable without the overhead of this.
2349 *
2350 * returns failure only if the task is already active.
2351 */
Peter Zijlstra7d478722009-09-14 19:55:44 +02002352static int try_to_wake_up(struct task_struct *p, unsigned int state,
2353 int wake_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002354{
Ingo Molnarcc367732007-10-15 17:00:18 +02002355 int cpu, orig_cpu, this_cpu, success = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002356 unsigned long flags;
Mike Galbraithf5dc3752009-10-09 08:35:03 +02002357 struct rq *rq, *orig_rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002358
Ingo Molnarb85d0662008-03-16 20:03:22 +01002359 if (!sched_feat(SYNC_WAKEUPS))
Peter Zijlstra7d478722009-09-14 19:55:44 +02002360 wake_flags &= ~WF_SYNC;
Ingo Molnarb85d0662008-03-16 20:03:22 +01002361
Peter Zijlstrae9c84312009-09-15 14:43:03 +02002362 this_cpu = get_cpu();
Peter Zijlstra2398f2c2008-06-27 13:41:35 +02002363
Linus Torvalds04e2f172008-02-23 18:05:03 -08002364 smp_wmb();
Mike Galbraithf5dc3752009-10-09 08:35:03 +02002365 rq = orig_rq = task_rq_lock(p, &flags);
Mike Galbraith03e89e42008-12-16 08:45:30 +01002366 update_rq_clock(rq);
Peter Zijlstrae9c84312009-09-15 14:43:03 +02002367 if (!(p->state & state))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002368 goto out;
2369
Ingo Molnardd41f592007-07-09 18:51:59 +02002370 if (p->se.on_rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002371 goto out_running;
2372
2373 cpu = task_cpu(p);
Ingo Molnarcc367732007-10-15 17:00:18 +02002374 orig_cpu = cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002375
2376#ifdef CONFIG_SMP
2377 if (unlikely(task_running(rq, p)))
2378 goto out_activate;
2379
Peter Zijlstrae9c84312009-09-15 14:43:03 +02002380 /*
2381 * In order to handle concurrent wakeups and release the rq->lock
2382 * we put the task in TASK_WAKING state.
Ingo Molnareb240732009-09-16 21:09:13 +02002383 *
2384 * First fix up the nr_uninterruptible count:
Peter Zijlstrae9c84312009-09-15 14:43:03 +02002385 */
Ingo Molnareb240732009-09-16 21:09:13 +02002386 if (task_contributes_to_load(p))
2387 rq->nr_uninterruptible--;
Peter Zijlstrae9c84312009-09-15 14:43:03 +02002388 p->state = TASK_WAKING;
Peter Zijlstraefbbd052009-12-16 18:04:40 +01002389
2390 if (p->sched_class->task_waking)
2391 p->sched_class->task_waking(rq, p);
2392
Peter Zijlstraab19cb22009-11-27 15:44:43 +01002393 __task_rq_unlock(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002394
Peter Zijlstra970b13b2009-11-25 13:31:39 +01002395 cpu = select_task_rq(p, SD_BALANCE_WAKE, wake_flags);
Peter Zijlstra0970d292010-02-15 14:45:54 +01002396 if (cpu != orig_cpu) {
2397 /*
2398 * Since we migrate the task without holding any rq->lock,
2399 * we need to be careful with task_rq_lock(), since that
2400 * might end up locking an invalid rq.
2401 */
Mike Galbraith055a0082009-11-12 11:07:44 +01002402 set_task_cpu(p, cpu);
Peter Zijlstra0970d292010-02-15 14:45:54 +01002403 }
Peter Zijlstraab19cb22009-11-27 15:44:43 +01002404
Peter Zijlstra0970d292010-02-15 14:45:54 +01002405 rq = cpu_rq(cpu);
2406 raw_spin_lock(&rq->lock);
Peter Zijlstraab19cb22009-11-27 15:44:43 +01002407 update_rq_clock(rq);
Mike Galbraithf5dc3752009-10-09 08:35:03 +02002408
Peter Zijlstra0970d292010-02-15 14:45:54 +01002409 /*
2410 * We migrated the task without holding either rq->lock, however
2411 * since the task is not on the task list itself, nobody else
2412 * will try and migrate the task, hence the rq should match the
2413 * cpu we just moved it to.
2414 */
2415 WARN_ON(task_cpu(p) != cpu);
Peter Zijlstrae9c84312009-09-15 14:43:03 +02002416 WARN_ON(p->state != TASK_WAKING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002417
Gregory Haskinse7693a32008-01-25 21:08:09 +01002418#ifdef CONFIG_SCHEDSTATS
2419 schedstat_inc(rq, ttwu_count);
2420 if (cpu == this_cpu)
2421 schedstat_inc(rq, ttwu_local);
2422 else {
2423 struct sched_domain *sd;
2424 for_each_domain(this_cpu, sd) {
Rusty Russell758b2cd2008-11-25 02:35:04 +10302425 if (cpumask_test_cpu(cpu, sched_domain_span(sd))) {
Gregory Haskinse7693a32008-01-25 21:08:09 +01002426 schedstat_inc(sd, ttwu_wake_remote);
2427 break;
2428 }
2429 }
2430 }
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +02002431#endif /* CONFIG_SCHEDSTATS */
Gregory Haskinse7693a32008-01-25 21:08:09 +01002432
Linus Torvalds1da177e2005-04-16 15:20:36 -07002433out_activate:
2434#endif /* CONFIG_SMP */
Ingo Molnarcc367732007-10-15 17:00:18 +02002435 schedstat_inc(p, se.nr_wakeups);
Peter Zijlstra7d478722009-09-14 19:55:44 +02002436 if (wake_flags & WF_SYNC)
Ingo Molnarcc367732007-10-15 17:00:18 +02002437 schedstat_inc(p, se.nr_wakeups_sync);
2438 if (orig_cpu != cpu)
2439 schedstat_inc(p, se.nr_wakeups_migrate);
2440 if (cpu == this_cpu)
2441 schedstat_inc(p, se.nr_wakeups_local);
2442 else
2443 schedstat_inc(p, se.nr_wakeups_remote);
Ingo Molnardd41f592007-07-09 18:51:59 +02002444 activate_task(rq, p, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002445 success = 1;
2446
Peter Zijlstra831451a2009-01-14 12:39:18 +01002447 /*
2448 * Only attribute actual wakeups done by this task.
2449 */
2450 if (!in_interrupt()) {
2451 struct sched_entity *se = &current->se;
2452 u64 sample = se->sum_exec_runtime;
2453
2454 if (se->last_wakeup)
2455 sample -= se->last_wakeup;
2456 else
2457 sample -= se->start_runtime;
2458 update_avg(&se->avg_wakeup, sample);
2459
2460 se->last_wakeup = se->sum_exec_runtime;
2461 }
2462
Linus Torvalds1da177e2005-04-16 15:20:36 -07002463out_running:
Peter Zijlstra468a15b2008-12-16 08:07:03 +01002464 trace_sched_wakeup(rq, p, success);
Peter Zijlstra7d478722009-09-14 19:55:44 +02002465 check_preempt_curr(rq, p, wake_flags);
Ingo Molnar4ae7d5c2008-03-19 01:42:00 +01002466
Linus Torvalds1da177e2005-04-16 15:20:36 -07002467 p->state = TASK_RUNNING;
Steven Rostedt9a897c52008-01-25 21:08:22 +01002468#ifdef CONFIG_SMP
Peter Zijlstraefbbd052009-12-16 18:04:40 +01002469 if (p->sched_class->task_woken)
2470 p->sched_class->task_woken(rq, p);
Mike Galbraitheae0c9d2009-11-10 03:50:02 +01002471
2472 if (unlikely(rq->idle_stamp)) {
2473 u64 delta = rq->clock - rq->idle_stamp;
2474 u64 max = 2*sysctl_sched_migration_cost;
2475
2476 if (delta > max)
2477 rq->avg_idle = max;
2478 else
2479 update_avg(&rq->avg_idle, delta);
2480 rq->idle_stamp = 0;
2481 }
Steven Rostedt9a897c52008-01-25 21:08:22 +01002482#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002483out:
2484 task_rq_unlock(rq, &flags);
Peter Zijlstrae9c84312009-09-15 14:43:03 +02002485 put_cpu();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002486
2487 return success;
2488}
2489
David Howells50fa6102009-04-28 15:01:38 +01002490/**
2491 * wake_up_process - Wake up a specific process
2492 * @p: The process to be woken up.
2493 *
2494 * Attempt to wake up the nominated process and move it to the set of runnable
2495 * processes. Returns 1 if the process was woken up, 0 if it was already
2496 * running.
2497 *
2498 * It may be assumed that this function implies a write memory barrier before
2499 * changing the task state if and only if any tasks are woken up.
2500 */
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -08002501int wake_up_process(struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002502{
Matthew Wilcoxd9514f62007-12-06 11:07:07 -05002503 return try_to_wake_up(p, TASK_ALL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002504}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002505EXPORT_SYMBOL(wake_up_process);
2506
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -08002507int wake_up_state(struct task_struct *p, unsigned int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002508{
2509 return try_to_wake_up(p, state, 0);
2510}
2511
Linus Torvalds1da177e2005-04-16 15:20:36 -07002512/*
2513 * Perform scheduler related setup for a newly forked process p.
2514 * p is forked by current.
Ingo Molnardd41f592007-07-09 18:51:59 +02002515 *
2516 * __sched_fork() is basic setup used by init_idle() too:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002517 */
Ingo Molnardd41f592007-07-09 18:51:59 +02002518static void __sched_fork(struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002519{
Ingo Molnardd41f592007-07-09 18:51:59 +02002520 p->se.exec_start = 0;
2521 p->se.sum_exec_runtime = 0;
Ingo Molnarf6cf8912007-08-28 12:53:24 +02002522 p->se.prev_sum_exec_runtime = 0;
Ingo Molnar6c594c22008-12-14 12:34:15 +01002523 p->se.nr_migrations = 0;
Ingo Molnar4ae7d5c2008-03-19 01:42:00 +01002524 p->se.last_wakeup = 0;
2525 p->se.avg_overlap = 0;
Peter Zijlstra831451a2009-01-14 12:39:18 +01002526 p->se.start_runtime = 0;
2527 p->se.avg_wakeup = sysctl_sched_wakeup_granularity;
Ingo Molnar6cfb0d52007-08-02 17:41:40 +02002528
2529#ifdef CONFIG_SCHEDSTATS
Lucas De Marchi77935272009-07-09 13:57:20 +02002530 p->se.wait_start = 0;
2531 p->se.wait_max = 0;
2532 p->se.wait_count = 0;
2533 p->se.wait_sum = 0;
2534
2535 p->se.sleep_start = 0;
2536 p->se.sleep_max = 0;
2537 p->se.sum_sleep_runtime = 0;
2538
2539 p->se.block_start = 0;
2540 p->se.block_max = 0;
2541 p->se.exec_max = 0;
2542 p->se.slice_max = 0;
2543
2544 p->se.nr_migrations_cold = 0;
2545 p->se.nr_failed_migrations_affine = 0;
2546 p->se.nr_failed_migrations_running = 0;
2547 p->se.nr_failed_migrations_hot = 0;
2548 p->se.nr_forced_migrations = 0;
Lucas De Marchi77935272009-07-09 13:57:20 +02002549
2550 p->se.nr_wakeups = 0;
2551 p->se.nr_wakeups_sync = 0;
2552 p->se.nr_wakeups_migrate = 0;
2553 p->se.nr_wakeups_local = 0;
2554 p->se.nr_wakeups_remote = 0;
2555 p->se.nr_wakeups_affine = 0;
2556 p->se.nr_wakeups_affine_attempts = 0;
2557 p->se.nr_wakeups_passive = 0;
2558 p->se.nr_wakeups_idle = 0;
2559
Ingo Molnar6cfb0d52007-08-02 17:41:40 +02002560#endif
Nick Piggin476d1392005-06-25 14:57:29 -07002561
Peter Zijlstrafa717062008-01-25 21:08:27 +01002562 INIT_LIST_HEAD(&p->rt.run_list);
Ingo Molnardd41f592007-07-09 18:51:59 +02002563 p->se.on_rq = 0;
Peter Zijlstra4a55bd52008-04-19 19:45:00 +02002564 INIT_LIST_HEAD(&p->se.group_node);
Nick Piggin476d1392005-06-25 14:57:29 -07002565
Avi Kivitye107be32007-07-26 13:40:43 +02002566#ifdef CONFIG_PREEMPT_NOTIFIERS
2567 INIT_HLIST_HEAD(&p->preempt_notifiers);
2568#endif
Ingo Molnardd41f592007-07-09 18:51:59 +02002569}
2570
2571/*
2572 * fork()/clone()-time setup:
2573 */
2574void sched_fork(struct task_struct *p, int clone_flags)
2575{
2576 int cpu = get_cpu();
2577
2578 __sched_fork(p);
Peter Zijlstra06b83b52009-12-16 18:04:35 +01002579 /*
2580 * We mark the process as waking here. This guarantees that
2581 * nobody will actually run it, and a signal or other external
2582 * event cannot wake it up and insert it on the runqueue either.
2583 */
2584 p->state = TASK_WAKING;
Ingo Molnardd41f592007-07-09 18:51:59 +02002585
Ingo Molnarb29739f2006-06-27 02:54:51 -07002586 /*
Mike Galbraithb9dc29e2009-06-17 10:46:01 +02002587 * Revert to default priority/policy on fork if requested.
2588 */
2589 if (unlikely(p->sched_reset_on_fork)) {
Peter Williamsf83f9ac2009-09-24 06:47:10 +00002590 if (p->policy == SCHED_FIFO || p->policy == SCHED_RR) {
Mike Galbraithb9dc29e2009-06-17 10:46:01 +02002591 p->policy = SCHED_NORMAL;
Peter Williamsf83f9ac2009-09-24 06:47:10 +00002592 p->normal_prio = p->static_prio;
2593 }
Mike Galbraithb9dc29e2009-06-17 10:46:01 +02002594
Mike Galbraith6c697bd2009-06-17 10:48:02 +02002595 if (PRIO_TO_NICE(p->static_prio) < 0) {
2596 p->static_prio = NICE_TO_PRIO(0);
Peter Williamsf83f9ac2009-09-24 06:47:10 +00002597 p->normal_prio = p->static_prio;
Mike Galbraith6c697bd2009-06-17 10:48:02 +02002598 set_load_weight(p);
2599 }
2600
Mike Galbraithb9dc29e2009-06-17 10:46:01 +02002601 /*
2602 * We don't need the reset flag anymore after the fork. It has
2603 * fulfilled its duty:
2604 */
2605 p->sched_reset_on_fork = 0;
2606 }
Lennart Poetteringca94c442009-06-15 17:17:47 +02002607
Peter Williamsf83f9ac2009-09-24 06:47:10 +00002608 /*
2609 * Make sure we do not leak PI boosting priority to the child.
2610 */
2611 p->prio = current->normal_prio;
2612
Hiroshi Shimamoto2ddbf952007-10-15 17:00:11 +02002613 if (!rt_prio(p->prio))
2614 p->sched_class = &fair_sched_class;
Ingo Molnarb29739f2006-06-27 02:54:51 -07002615
Peter Zijlstracd29fe62009-11-27 17:32:46 +01002616 if (p->sched_class->task_fork)
2617 p->sched_class->task_fork(p);
2618
Peter Zijlstra5f3edc12009-09-10 13:42:00 +02002619 set_task_cpu(p, cpu);
2620
Chandra Seetharaman52f17b62006-07-14 00:24:38 -07002621#if defined(CONFIG_SCHEDSTATS) || defined(CONFIG_TASK_DELAY_ACCT)
Ingo Molnardd41f592007-07-09 18:51:59 +02002622 if (likely(sched_info_on()))
Chandra Seetharaman52f17b62006-07-14 00:24:38 -07002623 memset(&p->sched_info, 0, sizeof(p->sched_info));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002624#endif
Chen, Kenneth Wd6077cb2006-02-14 13:53:10 -08002625#if defined(CONFIG_SMP) && defined(__ARCH_WANT_UNLOCKED_CTXSW)
Nick Piggin4866cde2005-06-25 14:57:23 -07002626 p->oncpu = 0;
2627#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002628#ifdef CONFIG_PREEMPT
Nick Piggin4866cde2005-06-25 14:57:23 -07002629 /* Want to start with kernel preemption disabled. */
Al Viroa1261f52005-11-13 16:06:55 -08002630 task_thread_info(p)->preempt_count = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002631#endif
Gregory Haskins917b6272008-12-29 09:39:53 -05002632 plist_node_init(&p->pushable_tasks, MAX_PRIO);
2633
Nick Piggin476d1392005-06-25 14:57:29 -07002634 put_cpu();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002635}
2636
2637/*
2638 * wake_up_new_task - wake up a newly created task for the first time.
2639 *
2640 * This function will do some initial scheduler statistics housekeeping
2641 * that must be done for every newly created context, then puts the task
2642 * on the runqueue and wakes it.
2643 */
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -08002644void wake_up_new_task(struct task_struct *p, unsigned long clone_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002645{
2646 unsigned long flags;
Ingo Molnardd41f592007-07-09 18:51:59 +02002647 struct rq *rq;
Peter Zijlstra0970d292010-02-15 14:45:54 +01002648 int cpu = get_cpu();
Peter Zijlstrafabf3182010-01-21 21:04:57 +01002649
2650#ifdef CONFIG_SMP
2651 /*
2652 * Fork balancing, do it here and not earlier because:
2653 * - cpus_allowed can change in the fork path
2654 * - any previously selected cpu might disappear through hotplug
2655 *
2656 * We still have TASK_WAKING but PF_STARTING is gone now, meaning
2657 * ->cpus_allowed is stable, we have preemption disabled, meaning
2658 * cpu_online_mask is stable.
2659 */
2660 cpu = select_task_rq(p, SD_BALANCE_FORK, 0);
2661 set_task_cpu(p, cpu);
2662#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002663
Peter Zijlstra0970d292010-02-15 14:45:54 +01002664 /*
2665 * Since the task is not on the rq and we still have TASK_WAKING set
2666 * nobody else will migrate this task.
2667 */
2668 rq = cpu_rq(cpu);
2669 raw_spin_lock_irqsave(&rq->lock, flags);
2670
Peter Zijlstra06b83b52009-12-16 18:04:35 +01002671 BUG_ON(p->state != TASK_WAKING);
2672 p->state = TASK_RUNNING;
Ingo Molnara8e504d2007-08-09 11:16:47 +02002673 update_rq_clock(rq);
Peter Zijlstracd29fe62009-11-27 17:32:46 +01002674 activate_task(rq, p, 0);
Ingo Molnarc71dd422008-12-19 01:09:51 +01002675 trace_sched_wakeup_new(rq, p, 1);
Peter Zijlstraa7558e02009-09-14 20:02:34 +02002676 check_preempt_curr(rq, p, WF_FORK);
Steven Rostedt9a897c52008-01-25 21:08:22 +01002677#ifdef CONFIG_SMP
Peter Zijlstraefbbd052009-12-16 18:04:40 +01002678 if (p->sched_class->task_woken)
2679 p->sched_class->task_woken(rq, p);
Steven Rostedt9a897c52008-01-25 21:08:22 +01002680#endif
Ingo Molnardd41f592007-07-09 18:51:59 +02002681 task_rq_unlock(rq, &flags);
Peter Zijlstrafabf3182010-01-21 21:04:57 +01002682 put_cpu();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002683}
2684
Avi Kivitye107be32007-07-26 13:40:43 +02002685#ifdef CONFIG_PREEMPT_NOTIFIERS
2686
2687/**
Luis Henriques80dd99b2009-03-16 19:58:09 +00002688 * preempt_notifier_register - tell me when current is being preempted & rescheduled
Randy Dunlap421cee22007-07-31 00:37:50 -07002689 * @notifier: notifier struct to register
Avi Kivitye107be32007-07-26 13:40:43 +02002690 */
2691void preempt_notifier_register(struct preempt_notifier *notifier)
2692{
2693 hlist_add_head(&notifier->link, &current->preempt_notifiers);
2694}
2695EXPORT_SYMBOL_GPL(preempt_notifier_register);
2696
2697/**
2698 * preempt_notifier_unregister - no longer interested in preemption notifications
Randy Dunlap421cee22007-07-31 00:37:50 -07002699 * @notifier: notifier struct to unregister
Avi Kivitye107be32007-07-26 13:40:43 +02002700 *
2701 * This is safe to call from within a preemption notifier.
2702 */
2703void preempt_notifier_unregister(struct preempt_notifier *notifier)
2704{
2705 hlist_del(&notifier->link);
2706}
2707EXPORT_SYMBOL_GPL(preempt_notifier_unregister);
2708
2709static void fire_sched_in_preempt_notifiers(struct task_struct *curr)
2710{
2711 struct preempt_notifier *notifier;
2712 struct hlist_node *node;
2713
2714 hlist_for_each_entry(notifier, node, &curr->preempt_notifiers, link)
2715 notifier->ops->sched_in(notifier, raw_smp_processor_id());
2716}
2717
2718static void
2719fire_sched_out_preempt_notifiers(struct task_struct *curr,
2720 struct task_struct *next)
2721{
2722 struct preempt_notifier *notifier;
2723 struct hlist_node *node;
2724
2725 hlist_for_each_entry(notifier, node, &curr->preempt_notifiers, link)
2726 notifier->ops->sched_out(notifier, next);
2727}
2728
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +02002729#else /* !CONFIG_PREEMPT_NOTIFIERS */
Avi Kivitye107be32007-07-26 13:40:43 +02002730
2731static void fire_sched_in_preempt_notifiers(struct task_struct *curr)
2732{
2733}
2734
2735static void
2736fire_sched_out_preempt_notifiers(struct task_struct *curr,
2737 struct task_struct *next)
2738{
2739}
2740
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +02002741#endif /* CONFIG_PREEMPT_NOTIFIERS */
Avi Kivitye107be32007-07-26 13:40:43 +02002742
Linus Torvalds1da177e2005-04-16 15:20:36 -07002743/**
Nick Piggin4866cde2005-06-25 14:57:23 -07002744 * prepare_task_switch - prepare to switch tasks
2745 * @rq: the runqueue preparing to switch
Randy Dunlap421cee22007-07-31 00:37:50 -07002746 * @prev: the current task that is being switched out
Nick Piggin4866cde2005-06-25 14:57:23 -07002747 * @next: the task we are going to switch to.
2748 *
2749 * This is called with the rq lock held and interrupts off. It must
2750 * be paired with a subsequent finish_task_switch after the context
2751 * switch.
2752 *
2753 * prepare_task_switch sets up locking and calls architecture specific
2754 * hooks.
2755 */
Avi Kivitye107be32007-07-26 13:40:43 +02002756static inline void
2757prepare_task_switch(struct rq *rq, struct task_struct *prev,
2758 struct task_struct *next)
Nick Piggin4866cde2005-06-25 14:57:23 -07002759{
Avi Kivitye107be32007-07-26 13:40:43 +02002760 fire_sched_out_preempt_notifiers(prev, next);
Nick Piggin4866cde2005-06-25 14:57:23 -07002761 prepare_lock_switch(rq, next);
2762 prepare_arch_switch(next);
2763}
2764
2765/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07002766 * finish_task_switch - clean up after a task-switch
Jeff Garzik344baba2005-09-07 01:15:17 -04002767 * @rq: runqueue associated with task-switch
Linus Torvalds1da177e2005-04-16 15:20:36 -07002768 * @prev: the thread we just switched away from.
2769 *
Nick Piggin4866cde2005-06-25 14:57:23 -07002770 * finish_task_switch must be called after the context switch, paired
2771 * with a prepare_task_switch call before the context switch.
2772 * finish_task_switch will reconcile locking set up by prepare_task_switch,
2773 * and do any other architecture-specific cleanup actions.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002774 *
2775 * Note that we may have delayed dropping an mm in context_switch(). If
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01002776 * so, we finish that here outside of the runqueue lock. (Doing it
Linus Torvalds1da177e2005-04-16 15:20:36 -07002777 * with the lock held can cause deadlocks; see schedule() for
2778 * details.)
2779 */
Alexey Dobriyana9957442007-10-15 17:00:13 +02002780static void finish_task_switch(struct rq *rq, struct task_struct *prev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002781 __releases(rq->lock)
2782{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002783 struct mm_struct *mm = rq->prev_mm;
Oleg Nesterov55a101f2006-09-29 02:01:10 -07002784 long prev_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002785
2786 rq->prev_mm = NULL;
2787
2788 /*
2789 * A task struct has one reference for the use as "current".
Oleg Nesterovc394cc92006-09-29 02:01:11 -07002790 * If a task dies, then it sets TASK_DEAD in tsk->state and calls
Oleg Nesterov55a101f2006-09-29 02:01:10 -07002791 * schedule one last time. The schedule call will never return, and
2792 * the scheduled task must drop that reference.
Oleg Nesterovc394cc92006-09-29 02:01:11 -07002793 * The test for TASK_DEAD must occur while the runqueue locks are
Linus Torvalds1da177e2005-04-16 15:20:36 -07002794 * still held, otherwise prev could be scheduled on another cpu, die
2795 * there before we look at prev->state, and then the reference would
2796 * be dropped twice.
2797 * Manfred Spraul <manfred@colorfullife.com>
2798 */
Oleg Nesterov55a101f2006-09-29 02:01:10 -07002799 prev_state = prev->state;
Nick Piggin4866cde2005-06-25 14:57:23 -07002800 finish_arch_switch(prev);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002801 perf_event_task_sched_in(current, cpu_of(rq));
Nick Piggin4866cde2005-06-25 14:57:23 -07002802 finish_lock_switch(rq, prev);
Steven Rostedte8fa1362008-01-25 21:08:05 +01002803
Avi Kivitye107be32007-07-26 13:40:43 +02002804 fire_sched_in_preempt_notifiers(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002805 if (mm)
2806 mmdrop(mm);
Oleg Nesterovc394cc92006-09-29 02:01:11 -07002807 if (unlikely(prev_state == TASK_DEAD)) {
bibo maoc6fd91f2006-03-26 01:38:20 -08002808 /*
2809 * Remove function-return probe instances associated with this
2810 * task and put them back on the free list.
Ingo Molnar9761eea2007-07-09 18:52:00 +02002811 */
bibo maoc6fd91f2006-03-26 01:38:20 -08002812 kprobe_flush_task(prev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002813 put_task_struct(prev);
bibo maoc6fd91f2006-03-26 01:38:20 -08002814 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002815}
2816
Gregory Haskins3f029d32009-07-29 11:08:47 -04002817#ifdef CONFIG_SMP
2818
2819/* assumes rq->lock is held */
2820static inline void pre_schedule(struct rq *rq, struct task_struct *prev)
2821{
2822 if (prev->sched_class->pre_schedule)
2823 prev->sched_class->pre_schedule(rq, prev);
2824}
2825
2826/* rq->lock is NOT held, but preemption is disabled */
2827static inline void post_schedule(struct rq *rq)
2828{
2829 if (rq->post_schedule) {
2830 unsigned long flags;
2831
Thomas Gleixner05fa7852009-11-17 14:28:38 +01002832 raw_spin_lock_irqsave(&rq->lock, flags);
Gregory Haskins3f029d32009-07-29 11:08:47 -04002833 if (rq->curr->sched_class->post_schedule)
2834 rq->curr->sched_class->post_schedule(rq);
Thomas Gleixner05fa7852009-11-17 14:28:38 +01002835 raw_spin_unlock_irqrestore(&rq->lock, flags);
Gregory Haskins3f029d32009-07-29 11:08:47 -04002836
2837 rq->post_schedule = 0;
2838 }
2839}
2840
2841#else
2842
2843static inline void pre_schedule(struct rq *rq, struct task_struct *p)
2844{
2845}
2846
2847static inline void post_schedule(struct rq *rq)
2848{
2849}
2850
2851#endif
2852
Linus Torvalds1da177e2005-04-16 15:20:36 -07002853/**
2854 * schedule_tail - first thing a freshly forked thread must call.
2855 * @prev: the thread we just switched away from.
2856 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07002857asmlinkage void schedule_tail(struct task_struct *prev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002858 __releases(rq->lock)
2859{
Ingo Molnar70b97a72006-07-03 00:25:42 -07002860 struct rq *rq = this_rq();
2861
Nick Piggin4866cde2005-06-25 14:57:23 -07002862 finish_task_switch(rq, prev);
Steven Rostedtda19ab52009-07-29 00:21:22 -04002863
Gregory Haskins3f029d32009-07-29 11:08:47 -04002864 /*
2865 * FIXME: do we need to worry about rq being invalidated by the
2866 * task_switch?
2867 */
2868 post_schedule(rq);
Steven Rostedtda19ab52009-07-29 00:21:22 -04002869
Nick Piggin4866cde2005-06-25 14:57:23 -07002870#ifdef __ARCH_WANT_UNLOCKED_CTXSW
2871 /* In this case, finish_task_switch does not reenable preemption */
2872 preempt_enable();
2873#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002874 if (current->set_child_tid)
Pavel Emelyanovb4888932007-10-18 23:40:14 -07002875 put_user(task_pid_vnr(current), current->set_child_tid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002876}
2877
2878/*
2879 * context_switch - switch to the new MM and the new
2880 * thread's register state.
2881 */
Ingo Molnardd41f592007-07-09 18:51:59 +02002882static inline void
Ingo Molnar70b97a72006-07-03 00:25:42 -07002883context_switch(struct rq *rq, struct task_struct *prev,
Ingo Molnar36c8b582006-07-03 00:25:41 -07002884 struct task_struct *next)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002885{
Ingo Molnardd41f592007-07-09 18:51:59 +02002886 struct mm_struct *mm, *oldmm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002887
Avi Kivitye107be32007-07-26 13:40:43 +02002888 prepare_task_switch(rq, prev, next);
Mathieu Desnoyers0a16b602008-07-18 12:16:17 -04002889 trace_sched_switch(rq, prev, next);
Ingo Molnardd41f592007-07-09 18:51:59 +02002890 mm = next->mm;
2891 oldmm = prev->active_mm;
Zachary Amsden9226d122007-02-13 13:26:21 +01002892 /*
2893 * For paravirt, this is coupled with an exit in switch_to to
2894 * combine the page table reload and the switch backend into
2895 * one hypercall.
2896 */
Jeremy Fitzhardinge224101e2009-02-18 11:18:57 -08002897 arch_start_context_switch(prev);
Zachary Amsden9226d122007-02-13 13:26:21 +01002898
Tim Blechmann710390d2009-11-24 11:55:27 +01002899 if (likely(!mm)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002900 next->active_mm = oldmm;
2901 atomic_inc(&oldmm->mm_count);
2902 enter_lazy_tlb(oldmm, next);
2903 } else
2904 switch_mm(oldmm, mm, next);
2905
Tim Blechmann710390d2009-11-24 11:55:27 +01002906 if (likely(!prev->mm)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002907 prev->active_mm = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002908 rq->prev_mm = oldmm;
2909 }
Ingo Molnar3a5f5e42006-07-14 00:24:27 -07002910 /*
2911 * Since the runqueue lock will be released by the next
2912 * task (which is an invalid locking op but in the case
2913 * of the scheduler it's an obvious special-case), so we
2914 * do an early lockdep release here:
2915 */
2916#ifndef __ARCH_WANT_UNLOCKED_CTXSW
Ingo Molnar8a25d5d2006-07-03 00:24:54 -07002917 spin_release(&rq->lock.dep_map, 1, _THIS_IP_);
Ingo Molnar3a5f5e42006-07-14 00:24:27 -07002918#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002919
2920 /* Here we just switch the register state and the stack. */
2921 switch_to(prev, next, prev);
2922
Ingo Molnardd41f592007-07-09 18:51:59 +02002923 barrier();
2924 /*
2925 * this_rq must be evaluated again because prev may have moved
2926 * CPUs since it called schedule(), thus the 'rq' on its stack
2927 * frame will be invalid.
2928 */
2929 finish_task_switch(this_rq(), prev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002930}
2931
2932/*
2933 * nr_running, nr_uninterruptible and nr_context_switches:
2934 *
2935 * externally visible scheduler statistics: current number of runnable
2936 * threads, current number of uninterruptible-sleeping threads, total
2937 * number of context switches performed since bootup.
2938 */
2939unsigned long nr_running(void)
2940{
2941 unsigned long i, sum = 0;
2942
2943 for_each_online_cpu(i)
2944 sum += cpu_rq(i)->nr_running;
2945
2946 return sum;
2947}
2948
2949unsigned long nr_uninterruptible(void)
2950{
2951 unsigned long i, sum = 0;
2952
KAMEZAWA Hiroyuki0a945022006-03-28 01:56:37 -08002953 for_each_possible_cpu(i)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002954 sum += cpu_rq(i)->nr_uninterruptible;
2955
2956 /*
2957 * Since we read the counters lockless, it might be slightly
2958 * inaccurate. Do not allow it to go below zero though:
2959 */
2960 if (unlikely((long)sum < 0))
2961 sum = 0;
2962
2963 return sum;
2964}
2965
2966unsigned long long nr_context_switches(void)
2967{
Steven Rostedtcc94abf2006-06-27 02:54:31 -07002968 int i;
2969 unsigned long long sum = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002970
KAMEZAWA Hiroyuki0a945022006-03-28 01:56:37 -08002971 for_each_possible_cpu(i)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002972 sum += cpu_rq(i)->nr_switches;
2973
2974 return sum;
2975}
2976
2977unsigned long nr_iowait(void)
2978{
2979 unsigned long i, sum = 0;
2980
KAMEZAWA Hiroyuki0a945022006-03-28 01:56:37 -08002981 for_each_possible_cpu(i)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002982 sum += atomic_read(&cpu_rq(i)->nr_iowait);
2983
2984 return sum;
2985}
2986
Arjan van de Ven69d25872009-09-21 17:04:08 -07002987unsigned long nr_iowait_cpu(void)
2988{
2989 struct rq *this = this_rq();
2990 return atomic_read(&this->nr_iowait);
2991}
2992
2993unsigned long this_cpu_load(void)
2994{
2995 struct rq *this = this_rq();
2996 return this->cpu_load[0];
2997}
2998
2999
Thomas Gleixnerdce48a82009-04-11 10:43:41 +02003000/* Variables and functions for calc_load */
3001static atomic_long_t calc_load_tasks;
3002static unsigned long calc_load_update;
3003unsigned long avenrun[3];
3004EXPORT_SYMBOL(avenrun);
3005
Thomas Gleixner2d024942009-05-02 20:08:52 +02003006/**
3007 * get_avenrun - get the load average array
3008 * @loads: pointer to dest load array
3009 * @offset: offset to add
3010 * @shift: shift count to shift the result left
3011 *
3012 * These values are estimates at best, so no need for locking.
3013 */
3014void get_avenrun(unsigned long *loads, unsigned long offset, int shift)
3015{
3016 loads[0] = (avenrun[0] + offset) << shift;
3017 loads[1] = (avenrun[1] + offset) << shift;
3018 loads[2] = (avenrun[2] + offset) << shift;
3019}
3020
Thomas Gleixnerdce48a82009-04-11 10:43:41 +02003021static unsigned long
3022calc_load(unsigned long load, unsigned long exp, unsigned long active)
Jack Steinerdb1b1fe2006-03-31 02:31:21 -08003023{
Thomas Gleixnerdce48a82009-04-11 10:43:41 +02003024 load *= exp;
3025 load += active * (FIXED_1 - exp);
3026 return load >> FSHIFT;
3027}
Jack Steinerdb1b1fe2006-03-31 02:31:21 -08003028
Thomas Gleixnerdce48a82009-04-11 10:43:41 +02003029/*
3030 * calc_load - update the avenrun load estimates 10 ticks after the
3031 * CPUs have updated calc_load_tasks.
3032 */
3033void calc_global_load(void)
3034{
3035 unsigned long upd = calc_load_update + 10;
3036 long active;
3037
3038 if (time_before(jiffies, upd))
3039 return;
3040
3041 active = atomic_long_read(&calc_load_tasks);
3042 active = active > 0 ? active * FIXED_1 : 0;
3043
3044 avenrun[0] = calc_load(avenrun[0], EXP_1, active);
3045 avenrun[1] = calc_load(avenrun[1], EXP_5, active);
3046 avenrun[2] = calc_load(avenrun[2], EXP_15, active);
3047
3048 calc_load_update += LOAD_FREQ;
3049}
3050
3051/*
3052 * Either called from update_cpu_load() or from a cpu going idle
3053 */
3054static void calc_load_account_active(struct rq *this_rq)
3055{
3056 long nr_active, delta;
3057
3058 nr_active = this_rq->nr_running;
3059 nr_active += (long) this_rq->nr_uninterruptible;
3060
3061 if (nr_active != this_rq->calc_load_active) {
3062 delta = nr_active - this_rq->calc_load_active;
3063 this_rq->calc_load_active = nr_active;
3064 atomic_long_add(delta, &calc_load_tasks);
Jack Steinerdb1b1fe2006-03-31 02:31:21 -08003065 }
Jack Steinerdb1b1fe2006-03-31 02:31:21 -08003066}
3067
Linus Torvalds1da177e2005-04-16 15:20:36 -07003068/*
Ingo Molnardd41f592007-07-09 18:51:59 +02003069 * Update rq->cpu_load[] statistics. This function is usually called every
3070 * scheduler tick (TICK_NSEC).
Ingo Molnar48f24c42006-07-03 00:25:40 -07003071 */
Ingo Molnardd41f592007-07-09 18:51:59 +02003072static void update_cpu_load(struct rq *this_rq)
Ingo Molnar48f24c42006-07-03 00:25:40 -07003073{
Dmitry Adamushko495eca42007-10-15 17:00:06 +02003074 unsigned long this_load = this_rq->load.weight;
Ingo Molnardd41f592007-07-09 18:51:59 +02003075 int i, scale;
3076
3077 this_rq->nr_load_updates++;
Ingo Molnardd41f592007-07-09 18:51:59 +02003078
3079 /* Update our load: */
3080 for (i = 0, scale = 1; i < CPU_LOAD_IDX_MAX; i++, scale += scale) {
3081 unsigned long old_load, new_load;
3082
3083 /* scale is effectively 1 << i now, and >> i divides by scale */
3084
3085 old_load = this_rq->cpu_load[i];
3086 new_load = this_load;
Ingo Molnara25707f2007-10-15 17:00:03 +02003087 /*
3088 * Round up the averaging division if load is increasing. This
3089 * prevents us from getting stuck on 9 if the load is 10, for
3090 * example.
3091 */
3092 if (new_load > old_load)
3093 new_load += scale-1;
Ingo Molnardd41f592007-07-09 18:51:59 +02003094 this_rq->cpu_load[i] = (old_load*(scale-1) + new_load) >> i;
3095 }
Thomas Gleixnerdce48a82009-04-11 10:43:41 +02003096
3097 if (time_after_eq(jiffies, this_rq->calc_load_update)) {
3098 this_rq->calc_load_update += LOAD_FREQ;
3099 calc_load_account_active(this_rq);
3100 }
Ingo Molnar48f24c42006-07-03 00:25:40 -07003101}
3102
Ingo Molnardd41f592007-07-09 18:51:59 +02003103#ifdef CONFIG_SMP
3104
Ingo Molnar48f24c42006-07-03 00:25:40 -07003105/*
Peter Zijlstra38022902009-12-16 18:04:37 +01003106 * sched_exec - execve() is a valuable balancing opportunity, because at
3107 * this point the task has the smallest effective memory and cache footprint.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003108 */
Peter Zijlstra38022902009-12-16 18:04:37 +01003109void sched_exec(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003110{
Peter Zijlstra38022902009-12-16 18:04:37 +01003111 struct task_struct *p = current;
Ingo Molnar70b97a72006-07-03 00:25:42 -07003112 struct migration_req req;
Peter Zijlstra38022902009-12-16 18:04:37 +01003113 int dest_cpu, this_cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003114 unsigned long flags;
Ingo Molnar70b97a72006-07-03 00:25:42 -07003115 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003116
Peter Zijlstra38022902009-12-16 18:04:37 +01003117again:
3118 this_cpu = get_cpu();
3119 dest_cpu = select_task_rq(p, SD_BALANCE_EXEC, 0);
3120 if (dest_cpu == this_cpu) {
3121 put_cpu();
3122 return;
3123 }
3124
Linus Torvalds1da177e2005-04-16 15:20:36 -07003125 rq = task_rq_lock(p, &flags);
Peter Zijlstra38022902009-12-16 18:04:37 +01003126 put_cpu();
3127
3128 /*
3129 * select_task_rq() can race against ->cpus_allowed
3130 */
Rusty Russell96f874e2008-11-25 02:35:14 +10303131 if (!cpumask_test_cpu(dest_cpu, &p->cpus_allowed)
Peter Zijlstra38022902009-12-16 18:04:37 +01003132 || unlikely(!cpu_active(dest_cpu))) {
3133 task_rq_unlock(rq, &flags);
3134 goto again;
3135 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003136
3137 /* force the process onto the specified CPU */
3138 if (migrate_task(p, dest_cpu, &req)) {
3139 /* Need to wait for migration thread (might exit: take ref). */
3140 struct task_struct *mt = rq->migration_thread;
Ingo Molnar36c8b582006-07-03 00:25:41 -07003141
Linus Torvalds1da177e2005-04-16 15:20:36 -07003142 get_task_struct(mt);
3143 task_rq_unlock(rq, &flags);
3144 wake_up_process(mt);
3145 put_task_struct(mt);
3146 wait_for_completion(&req.done);
Ingo Molnar36c8b582006-07-03 00:25:41 -07003147
Linus Torvalds1da177e2005-04-16 15:20:36 -07003148 return;
3149 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003150 task_rq_unlock(rq, &flags);
3151}
3152
Linus Torvalds1da177e2005-04-16 15:20:36 -07003153#endif
3154
Linus Torvalds1da177e2005-04-16 15:20:36 -07003155DEFINE_PER_CPU(struct kernel_stat, kstat);
3156
3157EXPORT_PER_CPU_SYMBOL(kstat);
3158
3159/*
Hidetoshi Setoc5f8d992009-03-31 16:56:03 +09003160 * Return any ns on the sched_clock that have not yet been accounted in
Frank Mayharf06febc2008-09-12 09:54:39 -07003161 * @p in case that task is currently running.
Hidetoshi Setoc5f8d992009-03-31 16:56:03 +09003162 *
3163 * Called with task_rq_lock() held on @rq.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003164 */
Hidetoshi Setoc5f8d992009-03-31 16:56:03 +09003165static u64 do_task_delta_exec(struct task_struct *p, struct rq *rq)
3166{
3167 u64 ns = 0;
3168
3169 if (task_current(rq, p)) {
3170 update_rq_clock(rq);
3171 ns = rq->clock - p->se.exec_start;
3172 if ((s64)ns < 0)
3173 ns = 0;
3174 }
3175
3176 return ns;
3177}
3178
Frank Mayharbb34d922008-09-12 09:54:39 -07003179unsigned long long task_delta_exec(struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003180{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003181 unsigned long flags;
Ingo Molnar41b86e92007-07-09 18:51:58 +02003182 struct rq *rq;
Frank Mayharbb34d922008-09-12 09:54:39 -07003183 u64 ns = 0;
Ingo Molnar48f24c42006-07-03 00:25:40 -07003184
Ingo Molnar41b86e92007-07-09 18:51:58 +02003185 rq = task_rq_lock(p, &flags);
Hidetoshi Setoc5f8d992009-03-31 16:56:03 +09003186 ns = do_task_delta_exec(p, rq);
3187 task_rq_unlock(rq, &flags);
Ingo Molnar15084872008-09-30 08:28:17 +02003188
Hidetoshi Setoc5f8d992009-03-31 16:56:03 +09003189 return ns;
3190}
Frank Mayharf06febc2008-09-12 09:54:39 -07003191
Hidetoshi Setoc5f8d992009-03-31 16:56:03 +09003192/*
3193 * Return accounted runtime for the task.
3194 * In case the task is currently running, return the runtime plus current's
3195 * pending runtime that have not been accounted yet.
3196 */
3197unsigned long long task_sched_runtime(struct task_struct *p)
3198{
3199 unsigned long flags;
3200 struct rq *rq;
3201 u64 ns = 0;
Ingo Molnar48f24c42006-07-03 00:25:40 -07003202
Hidetoshi Setoc5f8d992009-03-31 16:56:03 +09003203 rq = task_rq_lock(p, &flags);
3204 ns = p->se.sum_exec_runtime + do_task_delta_exec(p, rq);
3205 task_rq_unlock(rq, &flags);
3206
3207 return ns;
3208}
3209
3210/*
3211 * Return sum_exec_runtime for the thread group.
3212 * In case the task is currently running, return the sum plus current's
3213 * pending runtime that have not been accounted yet.
3214 *
3215 * Note that the thread group might have other running tasks as well,
3216 * so the return value not includes other pending runtime that other
3217 * running tasks might have.
3218 */
3219unsigned long long thread_group_sched_runtime(struct task_struct *p)
3220{
3221 struct task_cputime totals;
3222 unsigned long flags;
3223 struct rq *rq;
3224 u64 ns;
3225
3226 rq = task_rq_lock(p, &flags);
3227 thread_group_cputime(p, &totals);
3228 ns = totals.sum_exec_runtime + do_task_delta_exec(p, rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003229 task_rq_unlock(rq, &flags);
3230
3231 return ns;
3232}
3233
3234/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003235 * Account user cpu time to a process.
3236 * @p: the process that the cpu time gets accounted to
Linus Torvalds1da177e2005-04-16 15:20:36 -07003237 * @cputime: the cpu time spent in user space since the last update
Martin Schwidefsky457533a2008-12-31 15:11:37 +01003238 * @cputime_scaled: cputime scaled by cpu frequency
Linus Torvalds1da177e2005-04-16 15:20:36 -07003239 */
Martin Schwidefsky457533a2008-12-31 15:11:37 +01003240void account_user_time(struct task_struct *p, cputime_t cputime,
3241 cputime_t cputime_scaled)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003242{
3243 struct cpu_usage_stat *cpustat = &kstat_this_cpu.cpustat;
3244 cputime64_t tmp;
3245
Martin Schwidefsky457533a2008-12-31 15:11:37 +01003246 /* Add user time to process. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003247 p->utime = cputime_add(p->utime, cputime);
Martin Schwidefsky457533a2008-12-31 15:11:37 +01003248 p->utimescaled = cputime_add(p->utimescaled, cputime_scaled);
Frank Mayharf06febc2008-09-12 09:54:39 -07003249 account_group_user_time(p, cputime);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003250
3251 /* Add user time to cpustat. */
3252 tmp = cputime_to_cputime64(cputime);
3253 if (TASK_NICE(p) > 0)
3254 cpustat->nice = cputime64_add(cpustat->nice, tmp);
3255 else
3256 cpustat->user = cputime64_add(cpustat->user, tmp);
Bharata B Raoef12fef2009-03-31 10:02:22 +05303257
3258 cpuacct_update_stats(p, CPUACCT_STAT_USER, cputime);
Jonathan Lim49b5cf32008-07-25 01:48:40 -07003259 /* Account for user time used */
3260 acct_update_integrals(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003261}
3262
3263/*
Laurent Vivier94886b82007-10-15 17:00:19 +02003264 * Account guest cpu time to a process.
3265 * @p: the process that the cpu time gets accounted to
3266 * @cputime: the cpu time spent in virtual machine since the last update
Martin Schwidefsky457533a2008-12-31 15:11:37 +01003267 * @cputime_scaled: cputime scaled by cpu frequency
Laurent Vivier94886b82007-10-15 17:00:19 +02003268 */
Martin Schwidefsky457533a2008-12-31 15:11:37 +01003269static void account_guest_time(struct task_struct *p, cputime_t cputime,
3270 cputime_t cputime_scaled)
Laurent Vivier94886b82007-10-15 17:00:19 +02003271{
3272 cputime64_t tmp;
3273 struct cpu_usage_stat *cpustat = &kstat_this_cpu.cpustat;
3274
3275 tmp = cputime_to_cputime64(cputime);
3276
Martin Schwidefsky457533a2008-12-31 15:11:37 +01003277 /* Add guest time to process. */
Laurent Vivier94886b82007-10-15 17:00:19 +02003278 p->utime = cputime_add(p->utime, cputime);
Martin Schwidefsky457533a2008-12-31 15:11:37 +01003279 p->utimescaled = cputime_add(p->utimescaled, cputime_scaled);
Frank Mayharf06febc2008-09-12 09:54:39 -07003280 account_group_user_time(p, cputime);
Laurent Vivier94886b82007-10-15 17:00:19 +02003281 p->gtime = cputime_add(p->gtime, cputime);
3282
Martin Schwidefsky457533a2008-12-31 15:11:37 +01003283 /* Add guest time to cpustat. */
Ryota Ozakice0e7b22009-10-24 01:20:10 +09003284 if (TASK_NICE(p) > 0) {
3285 cpustat->nice = cputime64_add(cpustat->nice, tmp);
3286 cpustat->guest_nice = cputime64_add(cpustat->guest_nice, tmp);
3287 } else {
3288 cpustat->user = cputime64_add(cpustat->user, tmp);
3289 cpustat->guest = cputime64_add(cpustat->guest, tmp);
3290 }
Laurent Vivier94886b82007-10-15 17:00:19 +02003291}
3292
3293/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003294 * Account system cpu time to a process.
3295 * @p: the process that the cpu time gets accounted to
3296 * @hardirq_offset: the offset to subtract from hardirq_count()
3297 * @cputime: the cpu time spent in kernel space since the last update
Martin Schwidefsky457533a2008-12-31 15:11:37 +01003298 * @cputime_scaled: cputime scaled by cpu frequency
Linus Torvalds1da177e2005-04-16 15:20:36 -07003299 */
3300void account_system_time(struct task_struct *p, int hardirq_offset,
Martin Schwidefsky457533a2008-12-31 15:11:37 +01003301 cputime_t cputime, cputime_t cputime_scaled)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003302{
3303 struct cpu_usage_stat *cpustat = &kstat_this_cpu.cpustat;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003304 cputime64_t tmp;
3305
Harvey Harrison983ed7a2008-04-24 18:17:55 -07003306 if ((p->flags & PF_VCPU) && (irq_count() - hardirq_offset == 0)) {
Martin Schwidefsky457533a2008-12-31 15:11:37 +01003307 account_guest_time(p, cputime, cputime_scaled);
Harvey Harrison983ed7a2008-04-24 18:17:55 -07003308 return;
3309 }
Laurent Vivier94886b82007-10-15 17:00:19 +02003310
Martin Schwidefsky457533a2008-12-31 15:11:37 +01003311 /* Add system time to process. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003312 p->stime = cputime_add(p->stime, cputime);
Martin Schwidefsky457533a2008-12-31 15:11:37 +01003313 p->stimescaled = cputime_add(p->stimescaled, cputime_scaled);
Frank Mayharf06febc2008-09-12 09:54:39 -07003314 account_group_system_time(p, cputime);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003315
3316 /* Add system time to cpustat. */
3317 tmp = cputime_to_cputime64(cputime);
3318 if (hardirq_count() - hardirq_offset)
3319 cpustat->irq = cputime64_add(cpustat->irq, tmp);
3320 else if (softirq_count())
3321 cpustat->softirq = cputime64_add(cpustat->softirq, tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003322 else
Martin Schwidefsky79741dd2008-12-31 15:11:38 +01003323 cpustat->system = cputime64_add(cpustat->system, tmp);
3324
Bharata B Raoef12fef2009-03-31 10:02:22 +05303325 cpuacct_update_stats(p, CPUACCT_STAT_SYSTEM, cputime);
3326
Linus Torvalds1da177e2005-04-16 15:20:36 -07003327 /* Account for system time used */
3328 acct_update_integrals(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003329}
3330
3331/*
3332 * Account for involuntary wait time.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003333 * @steal: the cpu time spent in involuntary wait
Linus Torvalds1da177e2005-04-16 15:20:36 -07003334 */
Martin Schwidefsky79741dd2008-12-31 15:11:38 +01003335void account_steal_time(cputime_t cputime)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003336{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003337 struct cpu_usage_stat *cpustat = &kstat_this_cpu.cpustat;
Martin Schwidefsky79741dd2008-12-31 15:11:38 +01003338 cputime64_t cputime64 = cputime_to_cputime64(cputime);
3339
3340 cpustat->steal = cputime64_add(cpustat->steal, cputime64);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003341}
3342
Christoph Lameter7835b982006-12-10 02:20:22 -08003343/*
Martin Schwidefsky79741dd2008-12-31 15:11:38 +01003344 * Account for idle time.
3345 * @cputime: the cpu time spent in idle wait
Linus Torvalds1da177e2005-04-16 15:20:36 -07003346 */
Martin Schwidefsky79741dd2008-12-31 15:11:38 +01003347void account_idle_time(cputime_t cputime)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003348{
3349 struct cpu_usage_stat *cpustat = &kstat_this_cpu.cpustat;
Martin Schwidefsky79741dd2008-12-31 15:11:38 +01003350 cputime64_t cputime64 = cputime_to_cputime64(cputime);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003351 struct rq *rq = this_rq();
3352
Martin Schwidefsky79741dd2008-12-31 15:11:38 +01003353 if (atomic_read(&rq->nr_iowait) > 0)
3354 cpustat->iowait = cputime64_add(cpustat->iowait, cputime64);
3355 else
3356 cpustat->idle = cputime64_add(cpustat->idle, cputime64);
Christoph Lameter7835b982006-12-10 02:20:22 -08003357}
3358
Martin Schwidefsky79741dd2008-12-31 15:11:38 +01003359#ifndef CONFIG_VIRT_CPU_ACCOUNTING
3360
3361/*
3362 * Account a single tick of cpu time.
3363 * @p: the process that the cpu time gets accounted to
3364 * @user_tick: indicates if the tick is a user or a system tick
3365 */
3366void account_process_tick(struct task_struct *p, int user_tick)
3367{
Stanislaw Gruszkaa42548a2009-07-29 12:15:29 +02003368 cputime_t one_jiffy_scaled = cputime_to_scaled(cputime_one_jiffy);
Martin Schwidefsky79741dd2008-12-31 15:11:38 +01003369 struct rq *rq = this_rq();
3370
3371 if (user_tick)
Stanislaw Gruszkaa42548a2009-07-29 12:15:29 +02003372 account_user_time(p, cputime_one_jiffy, one_jiffy_scaled);
Eric Dumazetf5f293a2009-04-29 14:44:49 +02003373 else if ((p != rq->idle) || (irq_count() != HARDIRQ_OFFSET))
Stanislaw Gruszkaa42548a2009-07-29 12:15:29 +02003374 account_system_time(p, HARDIRQ_OFFSET, cputime_one_jiffy,
Martin Schwidefsky79741dd2008-12-31 15:11:38 +01003375 one_jiffy_scaled);
3376 else
Stanislaw Gruszkaa42548a2009-07-29 12:15:29 +02003377 account_idle_time(cputime_one_jiffy);
Martin Schwidefsky79741dd2008-12-31 15:11:38 +01003378}
3379
3380/*
3381 * Account multiple ticks of steal time.
3382 * @p: the process from which the cpu time has been stolen
3383 * @ticks: number of stolen ticks
3384 */
3385void account_steal_ticks(unsigned long ticks)
3386{
3387 account_steal_time(jiffies_to_cputime(ticks));
3388}
3389
3390/*
3391 * Account multiple ticks of idle time.
3392 * @ticks: number of stolen ticks
3393 */
3394void account_idle_ticks(unsigned long ticks)
3395{
3396 account_idle_time(jiffies_to_cputime(ticks));
3397}
3398
3399#endif
3400
Christoph Lameter7835b982006-12-10 02:20:22 -08003401/*
Balbir Singh49048622008-09-05 18:12:23 +02003402 * Use precise platform statistics if available:
3403 */
3404#ifdef CONFIG_VIRT_CPU_ACCOUNTING
Hidetoshi Setod180c5b2009-11-26 14:48:30 +09003405void task_times(struct task_struct *p, cputime_t *ut, cputime_t *st)
Balbir Singh49048622008-09-05 18:12:23 +02003406{
Hidetoshi Setod99ca3b2009-12-02 17:26:47 +09003407 *ut = p->utime;
3408 *st = p->stime;
Balbir Singh49048622008-09-05 18:12:23 +02003409}
3410
Hidetoshi Seto0cf55e12009-12-02 17:28:07 +09003411void thread_group_times(struct task_struct *p, cputime_t *ut, cputime_t *st)
Balbir Singh49048622008-09-05 18:12:23 +02003412{
Hidetoshi Seto0cf55e12009-12-02 17:28:07 +09003413 struct task_cputime cputime;
3414
3415 thread_group_cputime(p, &cputime);
3416
3417 *ut = cputime.utime;
3418 *st = cputime.stime;
Balbir Singh49048622008-09-05 18:12:23 +02003419}
3420#else
Hidetoshi Seto761b1d22009-11-12 13:33:45 +09003421
3422#ifndef nsecs_to_cputime
Hidetoshi Setob7b20df2009-11-26 14:49:27 +09003423# define nsecs_to_cputime(__nsecs) nsecs_to_jiffies(__nsecs)
Hidetoshi Seto761b1d22009-11-12 13:33:45 +09003424#endif
3425
Hidetoshi Setod180c5b2009-11-26 14:48:30 +09003426void task_times(struct task_struct *p, cputime_t *ut, cputime_t *st)
Balbir Singh49048622008-09-05 18:12:23 +02003427{
Hidetoshi Setod99ca3b2009-12-02 17:26:47 +09003428 cputime_t rtime, utime = p->utime, total = cputime_add(utime, p->stime);
Balbir Singh49048622008-09-05 18:12:23 +02003429
3430 /*
3431 * Use CFS's precise accounting:
3432 */
Hidetoshi Setod180c5b2009-11-26 14:48:30 +09003433 rtime = nsecs_to_cputime(p->se.sum_exec_runtime);
Balbir Singh49048622008-09-05 18:12:23 +02003434
3435 if (total) {
Hidetoshi Setod180c5b2009-11-26 14:48:30 +09003436 u64 temp;
Balbir Singh49048622008-09-05 18:12:23 +02003437
Hidetoshi Setod180c5b2009-11-26 14:48:30 +09003438 temp = (u64)(rtime * utime);
Balbir Singh49048622008-09-05 18:12:23 +02003439 do_div(temp, total);
Hidetoshi Setod180c5b2009-11-26 14:48:30 +09003440 utime = (cputime_t)temp;
3441 } else
3442 utime = rtime;
Balbir Singh49048622008-09-05 18:12:23 +02003443
3444 /*
Hidetoshi Setod180c5b2009-11-26 14:48:30 +09003445 * Compare with previous values, to keep monotonicity:
Balbir Singh49048622008-09-05 18:12:23 +02003446 */
Hidetoshi Seto761b1d22009-11-12 13:33:45 +09003447 p->prev_utime = max(p->prev_utime, utime);
Hidetoshi Setod99ca3b2009-12-02 17:26:47 +09003448 p->prev_stime = max(p->prev_stime, cputime_sub(rtime, p->prev_utime));
Balbir Singh49048622008-09-05 18:12:23 +02003449
Hidetoshi Setod99ca3b2009-12-02 17:26:47 +09003450 *ut = p->prev_utime;
3451 *st = p->prev_stime;
Hidetoshi Setod180c5b2009-11-26 14:48:30 +09003452}
Balbir Singh49048622008-09-05 18:12:23 +02003453
Hidetoshi Seto0cf55e12009-12-02 17:28:07 +09003454/*
3455 * Must be called with siglock held.
3456 */
3457void thread_group_times(struct task_struct *p, cputime_t *ut, cputime_t *st)
3458{
3459 struct signal_struct *sig = p->signal;
3460 struct task_cputime cputime;
3461 cputime_t rtime, utime, total;
3462
3463 thread_group_cputime(p, &cputime);
3464
3465 total = cputime_add(cputime.utime, cputime.stime);
3466 rtime = nsecs_to_cputime(cputime.sum_exec_runtime);
3467
3468 if (total) {
3469 u64 temp;
3470
3471 temp = (u64)(rtime * cputime.utime);
3472 do_div(temp, total);
3473 utime = (cputime_t)temp;
3474 } else
3475 utime = rtime;
3476
3477 sig->prev_utime = max(sig->prev_utime, utime);
3478 sig->prev_stime = max(sig->prev_stime,
3479 cputime_sub(rtime, sig->prev_utime));
3480
3481 *ut = sig->prev_utime;
3482 *st = sig->prev_stime;
Balbir Singh49048622008-09-05 18:12:23 +02003483}
3484#endif
3485
Balbir Singh49048622008-09-05 18:12:23 +02003486/*
Christoph Lameter7835b982006-12-10 02:20:22 -08003487 * This function gets called by the timer code, with HZ frequency.
3488 * We call it with interrupts disabled.
3489 *
3490 * It also gets called by the fork code, when changing the parent's
3491 * timeslices.
3492 */
3493void scheduler_tick(void)
3494{
Christoph Lameter7835b982006-12-10 02:20:22 -08003495 int cpu = smp_processor_id();
3496 struct rq *rq = cpu_rq(cpu);
Ingo Molnardd41f592007-07-09 18:51:59 +02003497 struct task_struct *curr = rq->curr;
Peter Zijlstra3e51f332008-05-03 18:29:28 +02003498
3499 sched_clock_tick();
Christoph Lameter7835b982006-12-10 02:20:22 -08003500
Thomas Gleixner05fa7852009-11-17 14:28:38 +01003501 raw_spin_lock(&rq->lock);
Peter Zijlstra3e51f332008-05-03 18:29:28 +02003502 update_rq_clock(rq);
Ingo Molnarf1a438d2007-08-09 11:16:45 +02003503 update_cpu_load(rq);
Peter Zijlstrafa85ae22008-01-25 21:08:29 +01003504 curr->sched_class->task_tick(rq, curr, 0);
Thomas Gleixner05fa7852009-11-17 14:28:38 +01003505 raw_spin_unlock(&rq->lock);
Ingo Molnardd41f592007-07-09 18:51:59 +02003506
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003507 perf_event_task_tick(curr, cpu);
Peter Zijlstrae220d2d2009-05-23 18:28:55 +02003508
Christoph Lametere418e1c2006-12-10 02:20:23 -08003509#ifdef CONFIG_SMP
Ingo Molnardd41f592007-07-09 18:51:59 +02003510 rq->idle_at_tick = idle_cpu(cpu);
3511 trigger_load_balance(rq, cpu);
Christoph Lametere418e1c2006-12-10 02:20:23 -08003512#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003513}
3514
Lai Jiangshan132380a2009-04-02 14:18:25 +08003515notrace unsigned long get_parent_ip(unsigned long addr)
Steven Rostedt6cd8a4b2008-05-12 21:20:42 +02003516{
3517 if (in_lock_functions(addr)) {
3518 addr = CALLER_ADDR2;
3519 if (in_lock_functions(addr))
3520 addr = CALLER_ADDR3;
3521 }
3522 return addr;
3523}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003524
Steven Rostedt7e49fcc2009-01-22 19:01:40 -05003525#if defined(CONFIG_PREEMPT) && (defined(CONFIG_DEBUG_PREEMPT) || \
3526 defined(CONFIG_PREEMPT_TRACER))
3527
Srinivasa Ds43627582008-02-23 15:24:04 -08003528void __kprobes add_preempt_count(int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003529{
Steven Rostedt6cd8a4b2008-05-12 21:20:42 +02003530#ifdef CONFIG_DEBUG_PREEMPT
Linus Torvalds1da177e2005-04-16 15:20:36 -07003531 /*
3532 * Underflow?
3533 */
Ingo Molnar9a11b49a2006-07-03 00:24:33 -07003534 if (DEBUG_LOCKS_WARN_ON((preempt_count() < 0)))
3535 return;
Steven Rostedt6cd8a4b2008-05-12 21:20:42 +02003536#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003537 preempt_count() += val;
Steven Rostedt6cd8a4b2008-05-12 21:20:42 +02003538#ifdef CONFIG_DEBUG_PREEMPT
Linus Torvalds1da177e2005-04-16 15:20:36 -07003539 /*
3540 * Spinlock count overflowing soon?
3541 */
Miguel Ojeda Sandonis33859f72006-12-10 02:20:38 -08003542 DEBUG_LOCKS_WARN_ON((preempt_count() & PREEMPT_MASK) >=
3543 PREEMPT_MASK - 10);
Steven Rostedt6cd8a4b2008-05-12 21:20:42 +02003544#endif
3545 if (preempt_count() == val)
3546 trace_preempt_off(CALLER_ADDR0, get_parent_ip(CALLER_ADDR1));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003547}
3548EXPORT_SYMBOL(add_preempt_count);
3549
Srinivasa Ds43627582008-02-23 15:24:04 -08003550void __kprobes sub_preempt_count(int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003551{
Steven Rostedt6cd8a4b2008-05-12 21:20:42 +02003552#ifdef CONFIG_DEBUG_PREEMPT
Linus Torvalds1da177e2005-04-16 15:20:36 -07003553 /*
3554 * Underflow?
3555 */
Ingo Molnar01e3eb82009-01-12 13:00:50 +01003556 if (DEBUG_LOCKS_WARN_ON(val > preempt_count()))
Ingo Molnar9a11b49a2006-07-03 00:24:33 -07003557 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003558 /*
3559 * Is the spinlock portion underflowing?
3560 */
Ingo Molnar9a11b49a2006-07-03 00:24:33 -07003561 if (DEBUG_LOCKS_WARN_ON((val < PREEMPT_MASK) &&
3562 !(preempt_count() & PREEMPT_MASK)))
3563 return;
Steven Rostedt6cd8a4b2008-05-12 21:20:42 +02003564#endif
Ingo Molnar9a11b49a2006-07-03 00:24:33 -07003565
Steven Rostedt6cd8a4b2008-05-12 21:20:42 +02003566 if (preempt_count() == val)
3567 trace_preempt_on(CALLER_ADDR0, get_parent_ip(CALLER_ADDR1));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003568 preempt_count() -= val;
3569}
3570EXPORT_SYMBOL(sub_preempt_count);
3571
3572#endif
3573
3574/*
Ingo Molnardd41f592007-07-09 18:51:59 +02003575 * Print scheduling while atomic bug:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003576 */
Ingo Molnardd41f592007-07-09 18:51:59 +02003577static noinline void __schedule_bug(struct task_struct *prev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003578{
Satyam Sharma838225b2007-10-24 18:23:50 +02003579 struct pt_regs *regs = get_irq_regs();
3580
Peter Zijlstra3df0fc52009-12-20 14:23:57 +01003581 printk(KERN_ERR "BUG: scheduling while atomic: %s/%d/0x%08x\n",
3582 prev->comm, prev->pid, preempt_count());
Satyam Sharma838225b2007-10-24 18:23:50 +02003583
Ingo Molnardd41f592007-07-09 18:51:59 +02003584 debug_show_held_locks(prev);
Arjan van de Vene21f5b12008-05-23 09:05:58 -07003585 print_modules();
Ingo Molnardd41f592007-07-09 18:51:59 +02003586 if (irqs_disabled())
3587 print_irqtrace_events(prev);
Satyam Sharma838225b2007-10-24 18:23:50 +02003588
3589 if (regs)
3590 show_regs(regs);
3591 else
3592 dump_stack();
Ingo Molnardd41f592007-07-09 18:51:59 +02003593}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003594
Ingo Molnardd41f592007-07-09 18:51:59 +02003595/*
3596 * Various schedule()-time debugging checks and statistics:
3597 */
3598static inline void schedule_debug(struct task_struct *prev)
3599{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003600 /*
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01003601 * Test if we are atomic. Since do_exit() needs to call into
Linus Torvalds1da177e2005-04-16 15:20:36 -07003602 * schedule() atomically, we ignore that path for now.
3603 * Otherwise, whine if we are scheduling when we should not be.
3604 */
Roel Kluin3f33a7c2008-05-13 23:44:11 +02003605 if (unlikely(in_atomic_preempt_off() && !prev->exit_state))
Ingo Molnardd41f592007-07-09 18:51:59 +02003606 __schedule_bug(prev);
3607
Linus Torvalds1da177e2005-04-16 15:20:36 -07003608 profile_hit(SCHED_PROFILING, __builtin_return_address(0));
3609
Ingo Molnar2d723762007-10-15 17:00:12 +02003610 schedstat_inc(this_rq(), sched_count);
Ingo Molnarb8efb562007-10-15 17:00:10 +02003611#ifdef CONFIG_SCHEDSTATS
3612 if (unlikely(prev->lock_depth >= 0)) {
Ingo Molnar2d723762007-10-15 17:00:12 +02003613 schedstat_inc(this_rq(), bkl_count);
3614 schedstat_inc(prev, sched_info.bkl_count);
Ingo Molnarb8efb562007-10-15 17:00:10 +02003615 }
3616#endif
Ingo Molnardd41f592007-07-09 18:51:59 +02003617}
3618
Peter Zijlstra6cecd082009-11-30 13:00:37 +01003619static void put_prev_task(struct rq *rq, struct task_struct *prev)
Mike Galbraithdf1c99d2009-03-10 19:08:11 +01003620{
Peter Zijlstra6cecd082009-11-30 13:00:37 +01003621 if (prev->state == TASK_RUNNING) {
3622 u64 runtime = prev->se.sum_exec_runtime;
Mike Galbraithdf1c99d2009-03-10 19:08:11 +01003623
Peter Zijlstra6cecd082009-11-30 13:00:37 +01003624 runtime -= prev->se.prev_sum_exec_runtime;
3625 runtime = min_t(u64, runtime, 2*sysctl_sched_migration_cost);
Mike Galbraithdf1c99d2009-03-10 19:08:11 +01003626
3627 /*
3628 * In order to avoid avg_overlap growing stale when we are
3629 * indeed overlapping and hence not getting put to sleep, grow
3630 * the avg_overlap on preemption.
3631 *
3632 * We use the average preemption runtime because that
3633 * correlates to the amount of cache footprint a task can
3634 * build up.
3635 */
Peter Zijlstra6cecd082009-11-30 13:00:37 +01003636 update_avg(&prev->se.avg_overlap, runtime);
Mike Galbraithdf1c99d2009-03-10 19:08:11 +01003637 }
Peter Zijlstra6cecd082009-11-30 13:00:37 +01003638 prev->sched_class->put_prev_task(rq, prev);
Mike Galbraithdf1c99d2009-03-10 19:08:11 +01003639}
3640
Ingo Molnardd41f592007-07-09 18:51:59 +02003641/*
3642 * Pick up the highest-prio task:
3643 */
3644static inline struct task_struct *
Wang Chenb67802e2009-03-02 13:55:26 +08003645pick_next_task(struct rq *rq)
Ingo Molnardd41f592007-07-09 18:51:59 +02003646{
Ingo Molnar5522d5d2007-10-15 17:00:12 +02003647 const struct sched_class *class;
Ingo Molnardd41f592007-07-09 18:51:59 +02003648 struct task_struct *p;
3649
3650 /*
3651 * Optimization: we know that if all tasks are in
3652 * the fair class we can call that function directly:
3653 */
3654 if (likely(rq->nr_running == rq->cfs.nr_running)) {
Ingo Molnarfb8d4722007-08-09 11:16:48 +02003655 p = fair_sched_class.pick_next_task(rq);
Ingo Molnardd41f592007-07-09 18:51:59 +02003656 if (likely(p))
3657 return p;
3658 }
3659
3660 class = sched_class_highest;
3661 for ( ; ; ) {
Ingo Molnarfb8d4722007-08-09 11:16:48 +02003662 p = class->pick_next_task(rq);
Ingo Molnardd41f592007-07-09 18:51:59 +02003663 if (p)
3664 return p;
3665 /*
3666 * Will never be NULL as the idle class always
3667 * returns a non-NULL p:
3668 */
3669 class = class->next;
3670 }
3671}
3672
3673/*
3674 * schedule() is the main scheduler function.
3675 */
Peter Zijlstraff743342009-03-13 12:21:26 +01003676asmlinkage void __sched schedule(void)
Ingo Molnardd41f592007-07-09 18:51:59 +02003677{
3678 struct task_struct *prev, *next;
Harvey Harrison67ca7bd2008-02-15 09:56:36 -08003679 unsigned long *switch_count;
Ingo Molnardd41f592007-07-09 18:51:59 +02003680 struct rq *rq;
Peter Zijlstra31656512008-07-18 18:01:23 +02003681 int cpu;
Ingo Molnardd41f592007-07-09 18:51:59 +02003682
Peter Zijlstraff743342009-03-13 12:21:26 +01003683need_resched:
3684 preempt_disable();
Ingo Molnardd41f592007-07-09 18:51:59 +02003685 cpu = smp_processor_id();
3686 rq = cpu_rq(cpu);
Paul E. McKenneyd6714c22009-08-22 13:56:46 -07003687 rcu_sched_qs(cpu);
Ingo Molnardd41f592007-07-09 18:51:59 +02003688 prev = rq->curr;
3689 switch_count = &prev->nivcsw;
3690
Linus Torvalds1da177e2005-04-16 15:20:36 -07003691 release_kernel_lock(prev);
3692need_resched_nonpreemptible:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003693
Ingo Molnardd41f592007-07-09 18:51:59 +02003694 schedule_debug(prev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003695
Peter Zijlstra31656512008-07-18 18:01:23 +02003696 if (sched_feat(HRTICK))
Mike Galbraithf333fdc2008-05-12 21:20:55 +02003697 hrtick_clear(rq);
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01003698
Thomas Gleixner05fa7852009-11-17 14:28:38 +01003699 raw_spin_lock_irq(&rq->lock);
Peter Zijlstra3e51f332008-05-03 18:29:28 +02003700 update_rq_clock(rq);
Ingo Molnar1e819952007-10-15 17:00:13 +02003701 clear_tsk_need_resched(prev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003702
Ingo Molnardd41f592007-07-09 18:51:59 +02003703 if (prev->state && !(preempt_count() & PREEMPT_ACTIVE)) {
Oleg Nesterov16882c12008-06-08 21:20:41 +04003704 if (unlikely(signal_pending_state(prev->state, prev)))
Ingo Molnardd41f592007-07-09 18:51:59 +02003705 prev->state = TASK_RUNNING;
Oleg Nesterov16882c12008-06-08 21:20:41 +04003706 else
Ingo Molnar2e1cb742007-08-09 11:16:49 +02003707 deactivate_task(rq, prev, 1);
Ingo Molnardd41f592007-07-09 18:51:59 +02003708 switch_count = &prev->nvcsw;
3709 }
3710
Gregory Haskins3f029d32009-07-29 11:08:47 -04003711 pre_schedule(rq, prev);
Steven Rostedtf65eda42008-01-25 21:08:07 +01003712
Ingo Molnardd41f592007-07-09 18:51:59 +02003713 if (unlikely(!rq->nr_running))
3714 idle_balance(cpu, rq);
3715
Mike Galbraithdf1c99d2009-03-10 19:08:11 +01003716 put_prev_task(rq, prev);
Wang Chenb67802e2009-03-02 13:55:26 +08003717 next = pick_next_task(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003718
Linus Torvalds1da177e2005-04-16 15:20:36 -07003719 if (likely(prev != next)) {
David Simner673a90a2008-04-29 10:08:59 +01003720 sched_info_switch(prev, next);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003721 perf_event_task_sched_out(prev, next, cpu);
David Simner673a90a2008-04-29 10:08:59 +01003722
Linus Torvalds1da177e2005-04-16 15:20:36 -07003723 rq->nr_switches++;
3724 rq->curr = next;
3725 ++*switch_count;
3726
Ingo Molnardd41f592007-07-09 18:51:59 +02003727 context_switch(rq, prev, next); /* unlocks the rq */
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01003728 /*
3729 * the context switch might have flipped the stack from under
3730 * us, hence refresh the local variables.
3731 */
3732 cpu = smp_processor_id();
3733 rq = cpu_rq(cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003734 } else
Thomas Gleixner05fa7852009-11-17 14:28:38 +01003735 raw_spin_unlock_irq(&rq->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003736
Gregory Haskins3f029d32009-07-29 11:08:47 -04003737 post_schedule(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003738
Yong Zhang6d558c32010-01-11 14:21:25 +08003739 if (unlikely(reacquire_kernel_lock(current) < 0)) {
3740 prev = rq->curr;
3741 switch_count = &prev->nivcsw;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003742 goto need_resched_nonpreemptible;
Yong Zhang6d558c32010-01-11 14:21:25 +08003743 }
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01003744
Linus Torvalds1da177e2005-04-16 15:20:36 -07003745 preempt_enable_no_resched();
Peter Zijlstraff743342009-03-13 12:21:26 +01003746 if (need_resched())
Linus Torvalds1da177e2005-04-16 15:20:36 -07003747 goto need_resched;
3748}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003749EXPORT_SYMBOL(schedule);
3750
Frederic Weisbeckerc08f7822009-12-02 20:49:17 +01003751#ifdef CONFIG_MUTEX_SPIN_ON_OWNER
Peter Zijlstra0d66bf62009-01-12 14:01:47 +01003752/*
3753 * Look out! "owner" is an entirely speculative pointer
3754 * access and not reliable.
3755 */
3756int mutex_spin_on_owner(struct mutex *lock, struct thread_info *owner)
3757{
3758 unsigned int cpu;
3759 struct rq *rq;
3760
3761 if (!sched_feat(OWNER_SPIN))
3762 return 0;
3763
3764#ifdef CONFIG_DEBUG_PAGEALLOC
3765 /*
3766 * Need to access the cpu field knowing that
3767 * DEBUG_PAGEALLOC could have unmapped it if
3768 * the mutex owner just released it and exited.
3769 */
3770 if (probe_kernel_address(&owner->cpu, cpu))
3771 goto out;
3772#else
3773 cpu = owner->cpu;
3774#endif
3775
3776 /*
3777 * Even if the access succeeded (likely case),
3778 * the cpu field may no longer be valid.
3779 */
3780 if (cpu >= nr_cpumask_bits)
3781 goto out;
3782
3783 /*
3784 * We need to validate that we can do a
3785 * get_cpu() and that we have the percpu area.
3786 */
3787 if (!cpu_online(cpu))
3788 goto out;
3789
3790 rq = cpu_rq(cpu);
3791
3792 for (;;) {
3793 /*
3794 * Owner changed, break to re-assess state.
3795 */
3796 if (lock->owner != owner)
3797 break;
3798
3799 /*
3800 * Is that owner really running on that cpu?
3801 */
3802 if (task_thread_info(rq->curr) != owner || need_resched())
3803 return 0;
3804
3805 cpu_relax();
3806 }
3807out:
3808 return 1;
3809}
3810#endif
3811
Linus Torvalds1da177e2005-04-16 15:20:36 -07003812#ifdef CONFIG_PREEMPT
3813/*
Andreas Mohr2ed6e342006-07-10 04:43:52 -07003814 * this is the entry point to schedule() from in-kernel preemption
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01003815 * off of preempt_enable. Kernel preemptions off return from interrupt
Linus Torvalds1da177e2005-04-16 15:20:36 -07003816 * occur there and call schedule directly.
3817 */
3818asmlinkage void __sched preempt_schedule(void)
3819{
3820 struct thread_info *ti = current_thread_info();
Ingo Molnar6478d882008-01-25 21:08:33 +01003821
Linus Torvalds1da177e2005-04-16 15:20:36 -07003822 /*
3823 * If there is a non-zero preempt_count or interrupts are disabled,
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01003824 * we do not want to preempt the current task. Just return..
Linus Torvalds1da177e2005-04-16 15:20:36 -07003825 */
Nick Pigginbeed33a2006-10-11 01:21:52 -07003826 if (likely(ti->preempt_count || irqs_disabled()))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003827 return;
3828
Andi Kleen3a5c3592007-10-15 17:00:14 +02003829 do {
3830 add_preempt_count(PREEMPT_ACTIVE);
Andi Kleen3a5c3592007-10-15 17:00:14 +02003831 schedule();
Andi Kleen3a5c3592007-10-15 17:00:14 +02003832 sub_preempt_count(PREEMPT_ACTIVE);
3833
3834 /*
3835 * Check again in case we missed a preemption opportunity
3836 * between schedule and now.
3837 */
3838 barrier();
Lai Jiangshan5ed0cec2009-03-06 19:40:20 +08003839 } while (need_resched());
Linus Torvalds1da177e2005-04-16 15:20:36 -07003840}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003841EXPORT_SYMBOL(preempt_schedule);
3842
3843/*
Andreas Mohr2ed6e342006-07-10 04:43:52 -07003844 * this is the entry point to schedule() from kernel preemption
Linus Torvalds1da177e2005-04-16 15:20:36 -07003845 * off of irq context.
3846 * Note, that this is called and return with irqs disabled. This will
3847 * protect us against recursive calling from irq.
3848 */
3849asmlinkage void __sched preempt_schedule_irq(void)
3850{
3851 struct thread_info *ti = current_thread_info();
Ingo Molnar6478d882008-01-25 21:08:33 +01003852
Andreas Mohr2ed6e342006-07-10 04:43:52 -07003853 /* Catch callers which need to be fixed */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003854 BUG_ON(ti->preempt_count || !irqs_disabled());
3855
Andi Kleen3a5c3592007-10-15 17:00:14 +02003856 do {
3857 add_preempt_count(PREEMPT_ACTIVE);
Andi Kleen3a5c3592007-10-15 17:00:14 +02003858 local_irq_enable();
3859 schedule();
3860 local_irq_disable();
Andi Kleen3a5c3592007-10-15 17:00:14 +02003861 sub_preempt_count(PREEMPT_ACTIVE);
3862
3863 /*
3864 * Check again in case we missed a preemption opportunity
3865 * between schedule and now.
3866 */
3867 barrier();
Lai Jiangshan5ed0cec2009-03-06 19:40:20 +08003868 } while (need_resched());
Linus Torvalds1da177e2005-04-16 15:20:36 -07003869}
3870
3871#endif /* CONFIG_PREEMPT */
3872
Peter Zijlstra63859d42009-09-15 19:14:42 +02003873int default_wake_function(wait_queue_t *curr, unsigned mode, int wake_flags,
Ingo Molnar95cdf3b2005-09-10 00:26:11 -07003874 void *key)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003875{
Peter Zijlstra63859d42009-09-15 19:14:42 +02003876 return try_to_wake_up(curr->private, mode, wake_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003877}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003878EXPORT_SYMBOL(default_wake_function);
3879
3880/*
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01003881 * The core wakeup function. Non-exclusive wakeups (nr_exclusive == 0) just
3882 * wake everything up. If it's an exclusive wakeup (nr_exclusive == small +ve
Linus Torvalds1da177e2005-04-16 15:20:36 -07003883 * number) then we wake all the non-exclusive tasks and one exclusive task.
3884 *
3885 * There are circumstances in which we can try to wake a task which has already
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01003886 * started to run but is not in state TASK_RUNNING. try_to_wake_up() returns
Linus Torvalds1da177e2005-04-16 15:20:36 -07003887 * zero in this (rare) case, and we handle it by continuing to scan the queue.
3888 */
Johannes Weiner78ddb082009-04-14 16:53:05 +02003889static void __wake_up_common(wait_queue_head_t *q, unsigned int mode,
Peter Zijlstra63859d42009-09-15 19:14:42 +02003890 int nr_exclusive, int wake_flags, void *key)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003891{
Matthias Kaehlcke2e458742007-10-15 17:00:02 +02003892 wait_queue_t *curr, *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003893
Matthias Kaehlcke2e458742007-10-15 17:00:02 +02003894 list_for_each_entry_safe(curr, next, &q->task_list, task_list) {
Ingo Molnar48f24c42006-07-03 00:25:40 -07003895 unsigned flags = curr->flags;
3896
Peter Zijlstra63859d42009-09-15 19:14:42 +02003897 if (curr->func(curr, mode, wake_flags, key) &&
Ingo Molnar48f24c42006-07-03 00:25:40 -07003898 (flags & WQ_FLAG_EXCLUSIVE) && !--nr_exclusive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003899 break;
3900 }
3901}
3902
3903/**
3904 * __wake_up - wake up threads blocked on a waitqueue.
3905 * @q: the waitqueue
3906 * @mode: which threads
3907 * @nr_exclusive: how many wake-one or wake-many threads to wake up
Martin Waitz67be2dd2005-05-01 08:59:26 -07003908 * @key: is directly passed to the wakeup function
David Howells50fa6102009-04-28 15:01:38 +01003909 *
3910 * It may be assumed that this function implies a write memory barrier before
3911 * changing the task state if and only if any tasks are woken up.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003912 */
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -08003913void __wake_up(wait_queue_head_t *q, unsigned int mode,
Ingo Molnar95cdf3b2005-09-10 00:26:11 -07003914 int nr_exclusive, void *key)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003915{
3916 unsigned long flags;
3917
3918 spin_lock_irqsave(&q->lock, flags);
3919 __wake_up_common(q, mode, nr_exclusive, 0, key);
3920 spin_unlock_irqrestore(&q->lock, flags);
3921}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003922EXPORT_SYMBOL(__wake_up);
3923
3924/*
3925 * Same as __wake_up but called with the spinlock in wait_queue_head_t held.
3926 */
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -08003927void __wake_up_locked(wait_queue_head_t *q, unsigned int mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003928{
3929 __wake_up_common(q, mode, 1, 0, NULL);
3930}
3931
Davide Libenzi4ede8162009-03-31 15:24:20 -07003932void __wake_up_locked_key(wait_queue_head_t *q, unsigned int mode, void *key)
3933{
3934 __wake_up_common(q, mode, 1, 0, key);
3935}
3936
Linus Torvalds1da177e2005-04-16 15:20:36 -07003937/**
Davide Libenzi4ede8162009-03-31 15:24:20 -07003938 * __wake_up_sync_key - wake up threads blocked on a waitqueue.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003939 * @q: the waitqueue
3940 * @mode: which threads
3941 * @nr_exclusive: how many wake-one or wake-many threads to wake up
Davide Libenzi4ede8162009-03-31 15:24:20 -07003942 * @key: opaque value to be passed to wakeup targets
Linus Torvalds1da177e2005-04-16 15:20:36 -07003943 *
3944 * The sync wakeup differs that the waker knows that it will schedule
3945 * away soon, so while the target thread will be woken up, it will not
3946 * be migrated to another CPU - ie. the two threads are 'synchronized'
3947 * with each other. This can prevent needless bouncing between CPUs.
3948 *
3949 * On UP it can prevent extra preemption.
David Howells50fa6102009-04-28 15:01:38 +01003950 *
3951 * It may be assumed that this function implies a write memory barrier before
3952 * changing the task state if and only if any tasks are woken up.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003953 */
Davide Libenzi4ede8162009-03-31 15:24:20 -07003954void __wake_up_sync_key(wait_queue_head_t *q, unsigned int mode,
3955 int nr_exclusive, void *key)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003956{
3957 unsigned long flags;
Peter Zijlstra7d478722009-09-14 19:55:44 +02003958 int wake_flags = WF_SYNC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003959
3960 if (unlikely(!q))
3961 return;
3962
3963 if (unlikely(!nr_exclusive))
Peter Zijlstra7d478722009-09-14 19:55:44 +02003964 wake_flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003965
3966 spin_lock_irqsave(&q->lock, flags);
Peter Zijlstra7d478722009-09-14 19:55:44 +02003967 __wake_up_common(q, mode, nr_exclusive, wake_flags, key);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003968 spin_unlock_irqrestore(&q->lock, flags);
3969}
Davide Libenzi4ede8162009-03-31 15:24:20 -07003970EXPORT_SYMBOL_GPL(__wake_up_sync_key);
3971
3972/*
3973 * __wake_up_sync - see __wake_up_sync_key()
3974 */
3975void __wake_up_sync(wait_queue_head_t *q, unsigned int mode, int nr_exclusive)
3976{
3977 __wake_up_sync_key(q, mode, nr_exclusive, NULL);
3978}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003979EXPORT_SYMBOL_GPL(__wake_up_sync); /* For internal use only */
3980
Kevin Diggs65eb3dc2008-08-26 10:26:54 +02003981/**
3982 * complete: - signals a single thread waiting on this completion
3983 * @x: holds the state of this particular completion
3984 *
3985 * This will wake up a single thread waiting on this completion. Threads will be
3986 * awakened in the same order in which they were queued.
3987 *
3988 * See also complete_all(), wait_for_completion() and related routines.
David Howells50fa6102009-04-28 15:01:38 +01003989 *
3990 * It may be assumed that this function implies a write memory barrier before
3991 * changing the task state if and only if any tasks are woken up.
Kevin Diggs65eb3dc2008-08-26 10:26:54 +02003992 */
Ingo Molnarb15136e2007-10-24 18:23:48 +02003993void complete(struct completion *x)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003994{
3995 unsigned long flags;
3996
3997 spin_lock_irqsave(&x->wait.lock, flags);
3998 x->done++;
Matthew Wilcoxd9514f62007-12-06 11:07:07 -05003999 __wake_up_common(&x->wait, TASK_NORMAL, 1, 0, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004000 spin_unlock_irqrestore(&x->wait.lock, flags);
4001}
4002EXPORT_SYMBOL(complete);
4003
Kevin Diggs65eb3dc2008-08-26 10:26:54 +02004004/**
4005 * complete_all: - signals all threads waiting on this completion
4006 * @x: holds the state of this particular completion
4007 *
4008 * This will wake up all threads waiting on this particular completion event.
David Howells50fa6102009-04-28 15:01:38 +01004009 *
4010 * It may be assumed that this function implies a write memory barrier before
4011 * changing the task state if and only if any tasks are woken up.
Kevin Diggs65eb3dc2008-08-26 10:26:54 +02004012 */
Ingo Molnarb15136e2007-10-24 18:23:48 +02004013void complete_all(struct completion *x)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004014{
4015 unsigned long flags;
4016
4017 spin_lock_irqsave(&x->wait.lock, flags);
4018 x->done += UINT_MAX/2;
Matthew Wilcoxd9514f62007-12-06 11:07:07 -05004019 __wake_up_common(&x->wait, TASK_NORMAL, 0, 0, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004020 spin_unlock_irqrestore(&x->wait.lock, flags);
4021}
4022EXPORT_SYMBOL(complete_all);
4023
Andi Kleen8cbbe862007-10-15 17:00:14 +02004024static inline long __sched
4025do_wait_for_common(struct completion *x, long timeout, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004026{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004027 if (!x->done) {
4028 DECLARE_WAITQUEUE(wait, current);
4029
4030 wait.flags |= WQ_FLAG_EXCLUSIVE;
4031 __add_wait_queue_tail(&x->wait, &wait);
4032 do {
Oleg Nesterov94d3d822008-08-20 16:54:41 -07004033 if (signal_pending_state(state, current)) {
Oleg Nesterovea71a542008-06-20 18:32:20 +04004034 timeout = -ERESTARTSYS;
4035 break;
Andi Kleen8cbbe862007-10-15 17:00:14 +02004036 }
4037 __set_current_state(state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004038 spin_unlock_irq(&x->wait.lock);
Andi Kleen8cbbe862007-10-15 17:00:14 +02004039 timeout = schedule_timeout(timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004040 spin_lock_irq(&x->wait.lock);
Oleg Nesterovea71a542008-06-20 18:32:20 +04004041 } while (!x->done && timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004042 __remove_wait_queue(&x->wait, &wait);
Oleg Nesterovea71a542008-06-20 18:32:20 +04004043 if (!x->done)
4044 return timeout;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004045 }
4046 x->done--;
Oleg Nesterovea71a542008-06-20 18:32:20 +04004047 return timeout ?: 1;
Andi Kleen8cbbe862007-10-15 17:00:14 +02004048}
4049
4050static long __sched
4051wait_for_common(struct completion *x, long timeout, int state)
4052{
4053 might_sleep();
4054
4055 spin_lock_irq(&x->wait.lock);
4056 timeout = do_wait_for_common(x, timeout, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004057 spin_unlock_irq(&x->wait.lock);
Andi Kleen8cbbe862007-10-15 17:00:14 +02004058 return timeout;
4059}
4060
Kevin Diggs65eb3dc2008-08-26 10:26:54 +02004061/**
4062 * wait_for_completion: - waits for completion of a task
4063 * @x: holds the state of this particular completion
4064 *
4065 * This waits to be signaled for completion of a specific task. It is NOT
4066 * interruptible and there is no timeout.
4067 *
4068 * See also similar routines (i.e. wait_for_completion_timeout()) with timeout
4069 * and interrupt capability. Also see complete().
4070 */
Ingo Molnarb15136e2007-10-24 18:23:48 +02004071void __sched wait_for_completion(struct completion *x)
Andi Kleen8cbbe862007-10-15 17:00:14 +02004072{
4073 wait_for_common(x, MAX_SCHEDULE_TIMEOUT, TASK_UNINTERRUPTIBLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004074}
4075EXPORT_SYMBOL(wait_for_completion);
4076
Kevin Diggs65eb3dc2008-08-26 10:26:54 +02004077/**
4078 * wait_for_completion_timeout: - waits for completion of a task (w/timeout)
4079 * @x: holds the state of this particular completion
4080 * @timeout: timeout value in jiffies
4081 *
4082 * This waits for either a completion of a specific task to be signaled or for a
4083 * specified timeout to expire. The timeout is in jiffies. It is not
4084 * interruptible.
4085 */
Ingo Molnarb15136e2007-10-24 18:23:48 +02004086unsigned long __sched
Linus Torvalds1da177e2005-04-16 15:20:36 -07004087wait_for_completion_timeout(struct completion *x, unsigned long timeout)
4088{
Andi Kleen8cbbe862007-10-15 17:00:14 +02004089 return wait_for_common(x, timeout, TASK_UNINTERRUPTIBLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004090}
4091EXPORT_SYMBOL(wait_for_completion_timeout);
4092
Kevin Diggs65eb3dc2008-08-26 10:26:54 +02004093/**
4094 * wait_for_completion_interruptible: - waits for completion of a task (w/intr)
4095 * @x: holds the state of this particular completion
4096 *
4097 * This waits for completion of a specific task to be signaled. It is
4098 * interruptible.
4099 */
Andi Kleen8cbbe862007-10-15 17:00:14 +02004100int __sched wait_for_completion_interruptible(struct completion *x)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004101{
Andi Kleen51e97992007-10-18 21:32:55 +02004102 long t = wait_for_common(x, MAX_SCHEDULE_TIMEOUT, TASK_INTERRUPTIBLE);
4103 if (t == -ERESTARTSYS)
4104 return t;
4105 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004106}
4107EXPORT_SYMBOL(wait_for_completion_interruptible);
4108
Kevin Diggs65eb3dc2008-08-26 10:26:54 +02004109/**
4110 * wait_for_completion_interruptible_timeout: - waits for completion (w/(to,intr))
4111 * @x: holds the state of this particular completion
4112 * @timeout: timeout value in jiffies
4113 *
4114 * This waits for either a completion of a specific task to be signaled or for a
4115 * specified timeout to expire. It is interruptible. The timeout is in jiffies.
4116 */
Ingo Molnarb15136e2007-10-24 18:23:48 +02004117unsigned long __sched
Linus Torvalds1da177e2005-04-16 15:20:36 -07004118wait_for_completion_interruptible_timeout(struct completion *x,
4119 unsigned long timeout)
4120{
Andi Kleen8cbbe862007-10-15 17:00:14 +02004121 return wait_for_common(x, timeout, TASK_INTERRUPTIBLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004122}
4123EXPORT_SYMBOL(wait_for_completion_interruptible_timeout);
4124
Kevin Diggs65eb3dc2008-08-26 10:26:54 +02004125/**
4126 * wait_for_completion_killable: - waits for completion of a task (killable)
4127 * @x: holds the state of this particular completion
4128 *
4129 * This waits to be signaled for completion of a specific task. It can be
4130 * interrupted by a kill signal.
4131 */
Matthew Wilcox009e5772007-12-06 12:29:54 -05004132int __sched wait_for_completion_killable(struct completion *x)
4133{
4134 long t = wait_for_common(x, MAX_SCHEDULE_TIMEOUT, TASK_KILLABLE);
4135 if (t == -ERESTARTSYS)
4136 return t;
4137 return 0;
4138}
4139EXPORT_SYMBOL(wait_for_completion_killable);
4140
Dave Chinnerbe4de352008-08-15 00:40:44 -07004141/**
4142 * try_wait_for_completion - try to decrement a completion without blocking
4143 * @x: completion structure
4144 *
4145 * Returns: 0 if a decrement cannot be done without blocking
4146 * 1 if a decrement succeeded.
4147 *
4148 * If a completion is being used as a counting completion,
4149 * attempt to decrement the counter without blocking. This
4150 * enables us to avoid waiting if the resource the completion
4151 * is protecting is not available.
4152 */
4153bool try_wait_for_completion(struct completion *x)
4154{
Rafael J. Wysocki7539a3b2009-12-13 00:07:30 +01004155 unsigned long flags;
Dave Chinnerbe4de352008-08-15 00:40:44 -07004156 int ret = 1;
4157
Rafael J. Wysocki7539a3b2009-12-13 00:07:30 +01004158 spin_lock_irqsave(&x->wait.lock, flags);
Dave Chinnerbe4de352008-08-15 00:40:44 -07004159 if (!x->done)
4160 ret = 0;
4161 else
4162 x->done--;
Rafael J. Wysocki7539a3b2009-12-13 00:07:30 +01004163 spin_unlock_irqrestore(&x->wait.lock, flags);
Dave Chinnerbe4de352008-08-15 00:40:44 -07004164 return ret;
4165}
4166EXPORT_SYMBOL(try_wait_for_completion);
4167
4168/**
4169 * completion_done - Test to see if a completion has any waiters
4170 * @x: completion structure
4171 *
4172 * Returns: 0 if there are waiters (wait_for_completion() in progress)
4173 * 1 if there are no waiters.
4174 *
4175 */
4176bool completion_done(struct completion *x)
4177{
Rafael J. Wysocki7539a3b2009-12-13 00:07:30 +01004178 unsigned long flags;
Dave Chinnerbe4de352008-08-15 00:40:44 -07004179 int ret = 1;
4180
Rafael J. Wysocki7539a3b2009-12-13 00:07:30 +01004181 spin_lock_irqsave(&x->wait.lock, flags);
Dave Chinnerbe4de352008-08-15 00:40:44 -07004182 if (!x->done)
4183 ret = 0;
Rafael J. Wysocki7539a3b2009-12-13 00:07:30 +01004184 spin_unlock_irqrestore(&x->wait.lock, flags);
Dave Chinnerbe4de352008-08-15 00:40:44 -07004185 return ret;
4186}
4187EXPORT_SYMBOL(completion_done);
4188
Andi Kleen8cbbe862007-10-15 17:00:14 +02004189static long __sched
4190sleep_on_common(wait_queue_head_t *q, int state, long timeout)
Ingo Molnar0fec1712007-07-09 18:52:01 +02004191{
4192 unsigned long flags;
4193 wait_queue_t wait;
4194
4195 init_waitqueue_entry(&wait, current);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004196
Andi Kleen8cbbe862007-10-15 17:00:14 +02004197 __set_current_state(state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004198
Andi Kleen8cbbe862007-10-15 17:00:14 +02004199 spin_lock_irqsave(&q->lock, flags);
4200 __add_wait_queue(q, &wait);
4201 spin_unlock(&q->lock);
4202 timeout = schedule_timeout(timeout);
4203 spin_lock_irq(&q->lock);
4204 __remove_wait_queue(q, &wait);
4205 spin_unlock_irqrestore(&q->lock, flags);
4206
4207 return timeout;
4208}
4209
4210void __sched interruptible_sleep_on(wait_queue_head_t *q)
4211{
4212 sleep_on_common(q, TASK_INTERRUPTIBLE, MAX_SCHEDULE_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004213}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004214EXPORT_SYMBOL(interruptible_sleep_on);
4215
Ingo Molnar0fec1712007-07-09 18:52:01 +02004216long __sched
Ingo Molnar95cdf3b2005-09-10 00:26:11 -07004217interruptible_sleep_on_timeout(wait_queue_head_t *q, long timeout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004218{
Andi Kleen8cbbe862007-10-15 17:00:14 +02004219 return sleep_on_common(q, TASK_INTERRUPTIBLE, timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004220}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004221EXPORT_SYMBOL(interruptible_sleep_on_timeout);
4222
Ingo Molnar0fec1712007-07-09 18:52:01 +02004223void __sched sleep_on(wait_queue_head_t *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004224{
Andi Kleen8cbbe862007-10-15 17:00:14 +02004225 sleep_on_common(q, TASK_UNINTERRUPTIBLE, MAX_SCHEDULE_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004226}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004227EXPORT_SYMBOL(sleep_on);
4228
Ingo Molnar0fec1712007-07-09 18:52:01 +02004229long __sched sleep_on_timeout(wait_queue_head_t *q, long timeout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004230{
Andi Kleen8cbbe862007-10-15 17:00:14 +02004231 return sleep_on_common(q, TASK_UNINTERRUPTIBLE, timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004232}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004233EXPORT_SYMBOL(sleep_on_timeout);
4234
Ingo Molnarb29739f2006-06-27 02:54:51 -07004235#ifdef CONFIG_RT_MUTEXES
4236
4237/*
4238 * rt_mutex_setprio - set the current priority of a task
4239 * @p: task
4240 * @prio: prio value (kernel-internal form)
4241 *
4242 * This function changes the 'effective' priority of a task. It does
4243 * not touch ->normal_prio like __setscheduler().
4244 *
4245 * Used by the rt_mutex code to implement priority inheritance logic.
4246 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07004247void rt_mutex_setprio(struct task_struct *p, int prio)
Ingo Molnarb29739f2006-06-27 02:54:51 -07004248{
4249 unsigned long flags;
Srivatsa Vaddagiri83b699e2007-10-15 17:00:08 +02004250 int oldprio, on_rq, running;
Ingo Molnar70b97a72006-07-03 00:25:42 -07004251 struct rq *rq;
Thomas Gleixner83ab0aa2010-02-17 09:05:48 +01004252 const struct sched_class *prev_class;
Ingo Molnarb29739f2006-06-27 02:54:51 -07004253
4254 BUG_ON(prio < 0 || prio > MAX_PRIO);
4255
4256 rq = task_rq_lock(p, &flags);
Ingo Molnara8e504d2007-08-09 11:16:47 +02004257 update_rq_clock(rq);
Ingo Molnarb29739f2006-06-27 02:54:51 -07004258
Andrew Mortond5f9f942007-05-08 20:27:06 -07004259 oldprio = p->prio;
Thomas Gleixner83ab0aa2010-02-17 09:05:48 +01004260 prev_class = p->sched_class;
Ingo Molnardd41f592007-07-09 18:51:59 +02004261 on_rq = p->se.on_rq;
Dmitry Adamushko051a1d12007-12-18 15:21:13 +01004262 running = task_current(rq, p);
Hiroshi Shimamoto0e1f3482008-03-10 11:01:20 -07004263 if (on_rq)
Ingo Molnar69be72c2007-08-09 11:16:49 +02004264 dequeue_task(rq, p, 0);
Hiroshi Shimamoto0e1f3482008-03-10 11:01:20 -07004265 if (running)
4266 p->sched_class->put_prev_task(rq, p);
Ingo Molnardd41f592007-07-09 18:51:59 +02004267
4268 if (rt_prio(prio))
4269 p->sched_class = &rt_sched_class;
4270 else
4271 p->sched_class = &fair_sched_class;
4272
Ingo Molnarb29739f2006-06-27 02:54:51 -07004273 p->prio = prio;
4274
Hiroshi Shimamoto0e1f3482008-03-10 11:01:20 -07004275 if (running)
4276 p->sched_class->set_curr_task(rq);
Ingo Molnardd41f592007-07-09 18:51:59 +02004277 if (on_rq) {
Thomas Gleixner60db48c2010-01-20 20:59:06 +00004278 enqueue_task(rq, p, 0, oldprio < prio);
Steven Rostedtcb469842008-01-25 21:08:22 +01004279
4280 check_class_changed(rq, p, prev_class, oldprio, running);
Ingo Molnarb29739f2006-06-27 02:54:51 -07004281 }
4282 task_rq_unlock(rq, &flags);
4283}
4284
4285#endif
4286
Ingo Molnar36c8b582006-07-03 00:25:41 -07004287void set_user_nice(struct task_struct *p, long nice)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004288{
Ingo Molnardd41f592007-07-09 18:51:59 +02004289 int old_prio, delta, on_rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004290 unsigned long flags;
Ingo Molnar70b97a72006-07-03 00:25:42 -07004291 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004292
4293 if (TASK_NICE(p) == nice || nice < -20 || nice > 19)
4294 return;
4295 /*
4296 * We have to be careful, if called from sys_setpriority(),
4297 * the task might be in the middle of scheduling on another CPU.
4298 */
4299 rq = task_rq_lock(p, &flags);
Ingo Molnara8e504d2007-08-09 11:16:47 +02004300 update_rq_clock(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004301 /*
4302 * The RT priorities are set via sched_setscheduler(), but we still
4303 * allow the 'normal' nice value to be set - but as expected
4304 * it wont have any effect on scheduling until the task is
Ingo Molnardd41f592007-07-09 18:51:59 +02004305 * SCHED_FIFO/SCHED_RR:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004306 */
Ingo Molnare05606d2007-07-09 18:51:59 +02004307 if (task_has_rt_policy(p)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004308 p->static_prio = NICE_TO_PRIO(nice);
4309 goto out_unlock;
4310 }
Ingo Molnardd41f592007-07-09 18:51:59 +02004311 on_rq = p->se.on_rq;
Peter Zijlstrac09595f2008-06-27 13:41:14 +02004312 if (on_rq)
Ingo Molnar69be72c2007-08-09 11:16:49 +02004313 dequeue_task(rq, p, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004314
Linus Torvalds1da177e2005-04-16 15:20:36 -07004315 p->static_prio = NICE_TO_PRIO(nice);
Peter Williams2dd73a42006-06-27 02:54:34 -07004316 set_load_weight(p);
Ingo Molnarb29739f2006-06-27 02:54:51 -07004317 old_prio = p->prio;
4318 p->prio = effective_prio(p);
4319 delta = p->prio - old_prio;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004320
Ingo Molnardd41f592007-07-09 18:51:59 +02004321 if (on_rq) {
Thomas Gleixnerea87bb72010-01-20 20:58:57 +00004322 enqueue_task(rq, p, 0, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004323 /*
Andrew Mortond5f9f942007-05-08 20:27:06 -07004324 * If the task increased its priority or is running and
4325 * lowered its priority, then reschedule its CPU:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004326 */
Andrew Mortond5f9f942007-05-08 20:27:06 -07004327 if (delta < 0 || (delta > 0 && task_running(rq, p)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004328 resched_task(rq->curr);
4329 }
4330out_unlock:
4331 task_rq_unlock(rq, &flags);
4332}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004333EXPORT_SYMBOL(set_user_nice);
4334
Matt Mackalle43379f2005-05-01 08:59:00 -07004335/*
4336 * can_nice - check if a task can reduce its nice value
4337 * @p: task
4338 * @nice: nice value
4339 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07004340int can_nice(const struct task_struct *p, const int nice)
Matt Mackalle43379f2005-05-01 08:59:00 -07004341{
Matt Mackall024f4742005-08-18 11:24:19 -07004342 /* convert nice value [19,-20] to rlimit style value [1,40] */
4343 int nice_rlim = 20 - nice;
Ingo Molnar48f24c42006-07-03 00:25:40 -07004344
Matt Mackalle43379f2005-05-01 08:59:00 -07004345 return (nice_rlim <= p->signal->rlim[RLIMIT_NICE].rlim_cur ||
4346 capable(CAP_SYS_NICE));
4347}
4348
Linus Torvalds1da177e2005-04-16 15:20:36 -07004349#ifdef __ARCH_WANT_SYS_NICE
4350
4351/*
4352 * sys_nice - change the priority of the current process.
4353 * @increment: priority increment
4354 *
4355 * sys_setpriority is a more generic, but much slower function that
4356 * does similar things.
4357 */
Heiko Carstens5add95d2009-01-14 14:14:08 +01004358SYSCALL_DEFINE1(nice, int, increment)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004359{
Ingo Molnar48f24c42006-07-03 00:25:40 -07004360 long nice, retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004361
4362 /*
4363 * Setpriority might change our priority at the same moment.
4364 * We don't have to worry. Conceptually one call occurs first
4365 * and we have a single winner.
4366 */
Matt Mackalle43379f2005-05-01 08:59:00 -07004367 if (increment < -40)
4368 increment = -40;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004369 if (increment > 40)
4370 increment = 40;
4371
Américo Wang2b8f8362009-02-16 18:54:21 +08004372 nice = TASK_NICE(current) + increment;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004373 if (nice < -20)
4374 nice = -20;
4375 if (nice > 19)
4376 nice = 19;
4377
Matt Mackalle43379f2005-05-01 08:59:00 -07004378 if (increment < 0 && !can_nice(current, nice))
4379 return -EPERM;
4380
Linus Torvalds1da177e2005-04-16 15:20:36 -07004381 retval = security_task_setnice(current, nice);
4382 if (retval)
4383 return retval;
4384
4385 set_user_nice(current, nice);
4386 return 0;
4387}
4388
4389#endif
4390
4391/**
4392 * task_prio - return the priority value of a given task.
4393 * @p: the task in question.
4394 *
4395 * This is the priority value as seen by users in /proc.
4396 * RT tasks are offset by -200. Normal tasks are centered
4397 * around 0, value goes from -16 to +15.
4398 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07004399int task_prio(const struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004400{
4401 return p->prio - MAX_RT_PRIO;
4402}
4403
4404/**
4405 * task_nice - return the nice value of a given task.
4406 * @p: the task in question.
4407 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07004408int task_nice(const struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004409{
4410 return TASK_NICE(p);
4411}
Pavel Roskin150d8be2008-03-05 16:56:37 -05004412EXPORT_SYMBOL(task_nice);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004413
4414/**
4415 * idle_cpu - is a given cpu idle currently?
4416 * @cpu: the processor in question.
4417 */
4418int idle_cpu(int cpu)
4419{
4420 return cpu_curr(cpu) == cpu_rq(cpu)->idle;
4421}
4422
Linus Torvalds1da177e2005-04-16 15:20:36 -07004423/**
4424 * idle_task - return the idle task for a given cpu.
4425 * @cpu: the processor in question.
4426 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07004427struct task_struct *idle_task(int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004428{
4429 return cpu_rq(cpu)->idle;
4430}
4431
4432/**
4433 * find_process_by_pid - find a process with a matching PID value.
4434 * @pid: the pid in question.
4435 */
Alexey Dobriyana9957442007-10-15 17:00:13 +02004436static struct task_struct *find_process_by_pid(pid_t pid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004437{
Pavel Emelyanov228ebcb2007-10-18 23:40:16 -07004438 return pid ? find_task_by_vpid(pid) : current;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004439}
4440
4441/* Actually do priority change: must hold rq lock. */
Ingo Molnardd41f592007-07-09 18:51:59 +02004442static void
4443__setscheduler(struct rq *rq, struct task_struct *p, int policy, int prio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004444{
Ingo Molnardd41f592007-07-09 18:51:59 +02004445 BUG_ON(p->se.on_rq);
Ingo Molnar48f24c42006-07-03 00:25:40 -07004446
Linus Torvalds1da177e2005-04-16 15:20:36 -07004447 p->policy = policy;
4448 p->rt_priority = prio;
Ingo Molnarb29739f2006-06-27 02:54:51 -07004449 p->normal_prio = normal_prio(p);
4450 /* we are holding p->pi_lock already */
4451 p->prio = rt_mutex_getprio(p);
Peter Zijlstraffd44db2009-11-10 20:12:01 +01004452 if (rt_prio(p->prio))
4453 p->sched_class = &rt_sched_class;
4454 else
4455 p->sched_class = &fair_sched_class;
Peter Williams2dd73a42006-06-27 02:54:34 -07004456 set_load_weight(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004457}
4458
David Howellsc69e8d92008-11-14 10:39:19 +11004459/*
4460 * check the target process has a UID that matches the current process's
4461 */
4462static bool check_same_owner(struct task_struct *p)
4463{
4464 const struct cred *cred = current_cred(), *pcred;
4465 bool match;
4466
4467 rcu_read_lock();
4468 pcred = __task_cred(p);
4469 match = (cred->euid == pcred->euid ||
4470 cred->euid == pcred->uid);
4471 rcu_read_unlock();
4472 return match;
4473}
4474
Rusty Russell961ccdd2008-06-23 13:55:38 +10004475static int __sched_setscheduler(struct task_struct *p, int policy,
4476 struct sched_param *param, bool user)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004477{
Srivatsa Vaddagiri83b699e2007-10-15 17:00:08 +02004478 int retval, oldprio, oldpolicy = -1, on_rq, running;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004479 unsigned long flags;
Thomas Gleixner83ab0aa2010-02-17 09:05:48 +01004480 const struct sched_class *prev_class;
Ingo Molnar70b97a72006-07-03 00:25:42 -07004481 struct rq *rq;
Lennart Poetteringca94c442009-06-15 17:17:47 +02004482 int reset_on_fork;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004483
Steven Rostedt66e53932006-06-27 02:54:44 -07004484 /* may grab non-irq protected spin_locks */
4485 BUG_ON(in_interrupt());
Linus Torvalds1da177e2005-04-16 15:20:36 -07004486recheck:
4487 /* double check policy once rq lock held */
Lennart Poetteringca94c442009-06-15 17:17:47 +02004488 if (policy < 0) {
4489 reset_on_fork = p->sched_reset_on_fork;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004490 policy = oldpolicy = p->policy;
Lennart Poetteringca94c442009-06-15 17:17:47 +02004491 } else {
4492 reset_on_fork = !!(policy & SCHED_RESET_ON_FORK);
4493 policy &= ~SCHED_RESET_ON_FORK;
4494
4495 if (policy != SCHED_FIFO && policy != SCHED_RR &&
4496 policy != SCHED_NORMAL && policy != SCHED_BATCH &&
4497 policy != SCHED_IDLE)
4498 return -EINVAL;
4499 }
4500
Linus Torvalds1da177e2005-04-16 15:20:36 -07004501 /*
4502 * Valid priorities for SCHED_FIFO and SCHED_RR are
Ingo Molnardd41f592007-07-09 18:51:59 +02004503 * 1..MAX_USER_RT_PRIO-1, valid priority for SCHED_NORMAL,
4504 * SCHED_BATCH and SCHED_IDLE is 0.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004505 */
4506 if (param->sched_priority < 0 ||
Ingo Molnar95cdf3b2005-09-10 00:26:11 -07004507 (p->mm && param->sched_priority > MAX_USER_RT_PRIO-1) ||
Steven Rostedtd46523e2005-07-25 16:28:39 -04004508 (!p->mm && param->sched_priority > MAX_RT_PRIO-1))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004509 return -EINVAL;
Ingo Molnare05606d2007-07-09 18:51:59 +02004510 if (rt_policy(policy) != (param->sched_priority != 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004511 return -EINVAL;
4512
Olivier Croquette37e4ab32005-06-25 14:57:32 -07004513 /*
4514 * Allow unprivileged RT tasks to decrease priority:
4515 */
Rusty Russell961ccdd2008-06-23 13:55:38 +10004516 if (user && !capable(CAP_SYS_NICE)) {
Ingo Molnare05606d2007-07-09 18:51:59 +02004517 if (rt_policy(policy)) {
Oleg Nesterov8dc3e902006-09-29 02:00:50 -07004518 unsigned long rlim_rtprio;
Oleg Nesterov5fe1d752006-09-29 02:00:48 -07004519
Oleg Nesterov8dc3e902006-09-29 02:00:50 -07004520 if (!lock_task_sighand(p, &flags))
4521 return -ESRCH;
4522 rlim_rtprio = p->signal->rlim[RLIMIT_RTPRIO].rlim_cur;
4523 unlock_task_sighand(p, &flags);
Oleg Nesterov5fe1d752006-09-29 02:00:48 -07004524
Oleg Nesterov8dc3e902006-09-29 02:00:50 -07004525 /* can't set/change the rt policy */
4526 if (policy != p->policy && !rlim_rtprio)
4527 return -EPERM;
4528
4529 /* can't increase priority */
4530 if (param->sched_priority > p->rt_priority &&
4531 param->sched_priority > rlim_rtprio)
4532 return -EPERM;
4533 }
Ingo Molnardd41f592007-07-09 18:51:59 +02004534 /*
4535 * Like positive nice levels, dont allow tasks to
4536 * move out of SCHED_IDLE either:
4537 */
4538 if (p->policy == SCHED_IDLE && policy != SCHED_IDLE)
4539 return -EPERM;
Oleg Nesterov8dc3e902006-09-29 02:00:50 -07004540
Olivier Croquette37e4ab32005-06-25 14:57:32 -07004541 /* can't change other user's priorities */
David Howellsc69e8d92008-11-14 10:39:19 +11004542 if (!check_same_owner(p))
Olivier Croquette37e4ab32005-06-25 14:57:32 -07004543 return -EPERM;
Lennart Poetteringca94c442009-06-15 17:17:47 +02004544
4545 /* Normal users shall not reset the sched_reset_on_fork flag */
4546 if (p->sched_reset_on_fork && !reset_on_fork)
4547 return -EPERM;
Olivier Croquette37e4ab32005-06-25 14:57:32 -07004548 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004549
Jeremy Fitzhardinge725aad22008-08-03 09:33:03 -07004550 if (user) {
Peter Zijlstrab68aa232008-02-13 15:45:40 +01004551#ifdef CONFIG_RT_GROUP_SCHED
Jeremy Fitzhardinge725aad22008-08-03 09:33:03 -07004552 /*
4553 * Do not allow realtime tasks into groups that have no runtime
4554 * assigned.
4555 */
Peter Zijlstra9a7e0b12008-08-19 12:33:06 +02004556 if (rt_bandwidth_enabled() && rt_policy(policy) &&
4557 task_group(p)->rt_bandwidth.rt_runtime == 0)
Jeremy Fitzhardinge725aad22008-08-03 09:33:03 -07004558 return -EPERM;
Peter Zijlstrab68aa232008-02-13 15:45:40 +01004559#endif
4560
Jeremy Fitzhardinge725aad22008-08-03 09:33:03 -07004561 retval = security_task_setscheduler(p, policy, param);
4562 if (retval)
4563 return retval;
4564 }
4565
Linus Torvalds1da177e2005-04-16 15:20:36 -07004566 /*
Ingo Molnarb29739f2006-06-27 02:54:51 -07004567 * make sure no PI-waiters arrive (or leave) while we are
4568 * changing the priority of the task:
4569 */
Thomas Gleixner1d615482009-11-17 14:54:03 +01004570 raw_spin_lock_irqsave(&p->pi_lock, flags);
Ingo Molnarb29739f2006-06-27 02:54:51 -07004571 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07004572 * To be able to change p->policy safely, the apropriate
4573 * runqueue lock must be held.
4574 */
Ingo Molnarb29739f2006-06-27 02:54:51 -07004575 rq = __task_rq_lock(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004576 /* recheck policy now with rq lock held */
4577 if (unlikely(oldpolicy != -1 && oldpolicy != p->policy)) {
4578 policy = oldpolicy = -1;
Ingo Molnarb29739f2006-06-27 02:54:51 -07004579 __task_rq_unlock(rq);
Thomas Gleixner1d615482009-11-17 14:54:03 +01004580 raw_spin_unlock_irqrestore(&p->pi_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004581 goto recheck;
4582 }
Ingo Molnar2daa3572007-08-09 11:16:51 +02004583 update_rq_clock(rq);
Ingo Molnardd41f592007-07-09 18:51:59 +02004584 on_rq = p->se.on_rq;
Dmitry Adamushko051a1d12007-12-18 15:21:13 +01004585 running = task_current(rq, p);
Hiroshi Shimamoto0e1f3482008-03-10 11:01:20 -07004586 if (on_rq)
Ingo Molnar2e1cb742007-08-09 11:16:49 +02004587 deactivate_task(rq, p, 0);
Hiroshi Shimamoto0e1f3482008-03-10 11:01:20 -07004588 if (running)
4589 p->sched_class->put_prev_task(rq, p);
Dmitry Adamushkof6b53202007-10-15 17:00:08 +02004590
Lennart Poetteringca94c442009-06-15 17:17:47 +02004591 p->sched_reset_on_fork = reset_on_fork;
4592
Linus Torvalds1da177e2005-04-16 15:20:36 -07004593 oldprio = p->prio;
Thomas Gleixner83ab0aa2010-02-17 09:05:48 +01004594 prev_class = p->sched_class;
Ingo Molnardd41f592007-07-09 18:51:59 +02004595 __setscheduler(rq, p, policy, param->sched_priority);
Dmitry Adamushkof6b53202007-10-15 17:00:08 +02004596
Hiroshi Shimamoto0e1f3482008-03-10 11:01:20 -07004597 if (running)
4598 p->sched_class->set_curr_task(rq);
Ingo Molnardd41f592007-07-09 18:51:59 +02004599 if (on_rq) {
4600 activate_task(rq, p, 0);
Steven Rostedtcb469842008-01-25 21:08:22 +01004601
4602 check_class_changed(rq, p, prev_class, oldprio, running);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004603 }
Ingo Molnarb29739f2006-06-27 02:54:51 -07004604 __task_rq_unlock(rq);
Thomas Gleixner1d615482009-11-17 14:54:03 +01004605 raw_spin_unlock_irqrestore(&p->pi_lock, flags);
Ingo Molnarb29739f2006-06-27 02:54:51 -07004606
Thomas Gleixner95e02ca2006-06-27 02:55:02 -07004607 rt_mutex_adjust_pi(p);
4608
Linus Torvalds1da177e2005-04-16 15:20:36 -07004609 return 0;
4610}
Rusty Russell961ccdd2008-06-23 13:55:38 +10004611
4612/**
4613 * sched_setscheduler - change the scheduling policy and/or RT priority of a thread.
4614 * @p: the task in question.
4615 * @policy: new policy.
4616 * @param: structure containing the new RT priority.
4617 *
4618 * NOTE that the task may be already dead.
4619 */
4620int sched_setscheduler(struct task_struct *p, int policy,
4621 struct sched_param *param)
4622{
4623 return __sched_setscheduler(p, policy, param, true);
4624}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004625EXPORT_SYMBOL_GPL(sched_setscheduler);
4626
Rusty Russell961ccdd2008-06-23 13:55:38 +10004627/**
4628 * sched_setscheduler_nocheck - change the scheduling policy and/or RT priority of a thread from kernelspace.
4629 * @p: the task in question.
4630 * @policy: new policy.
4631 * @param: structure containing the new RT priority.
4632 *
4633 * Just like sched_setscheduler, only don't bother checking if the
4634 * current context has permission. For example, this is needed in
4635 * stop_machine(): we create temporary high priority worker threads,
4636 * but our caller might not have that capability.
4637 */
4638int sched_setscheduler_nocheck(struct task_struct *p, int policy,
4639 struct sched_param *param)
4640{
4641 return __sched_setscheduler(p, policy, param, false);
4642}
4643
Ingo Molnar95cdf3b2005-09-10 00:26:11 -07004644static int
4645do_sched_setscheduler(pid_t pid, int policy, struct sched_param __user *param)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004646{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004647 struct sched_param lparam;
4648 struct task_struct *p;
Ingo Molnar36c8b582006-07-03 00:25:41 -07004649 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004650
4651 if (!param || pid < 0)
4652 return -EINVAL;
4653 if (copy_from_user(&lparam, param, sizeof(struct sched_param)))
4654 return -EFAULT;
Oleg Nesterov5fe1d752006-09-29 02:00:48 -07004655
4656 rcu_read_lock();
4657 retval = -ESRCH;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004658 p = find_process_by_pid(pid);
Oleg Nesterov5fe1d752006-09-29 02:00:48 -07004659 if (p != NULL)
4660 retval = sched_setscheduler(p, policy, &lparam);
4661 rcu_read_unlock();
Ingo Molnar36c8b582006-07-03 00:25:41 -07004662
Linus Torvalds1da177e2005-04-16 15:20:36 -07004663 return retval;
4664}
4665
4666/**
4667 * sys_sched_setscheduler - set/change the scheduler policy and RT priority
4668 * @pid: the pid in question.
4669 * @policy: new policy.
4670 * @param: structure containing the new RT priority.
4671 */
Heiko Carstens5add95d2009-01-14 14:14:08 +01004672SYSCALL_DEFINE3(sched_setscheduler, pid_t, pid, int, policy,
4673 struct sched_param __user *, param)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004674{
Jason Baronc21761f2006-01-18 17:43:03 -08004675 /* negative values for policy are not valid */
4676 if (policy < 0)
4677 return -EINVAL;
4678
Linus Torvalds1da177e2005-04-16 15:20:36 -07004679 return do_sched_setscheduler(pid, policy, param);
4680}
4681
4682/**
4683 * sys_sched_setparam - set/change the RT priority of a thread
4684 * @pid: the pid in question.
4685 * @param: structure containing the new RT priority.
4686 */
Heiko Carstens5add95d2009-01-14 14:14:08 +01004687SYSCALL_DEFINE2(sched_setparam, pid_t, pid, struct sched_param __user *, param)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004688{
4689 return do_sched_setscheduler(pid, -1, param);
4690}
4691
4692/**
4693 * sys_sched_getscheduler - get the policy (scheduling class) of a thread
4694 * @pid: the pid in question.
4695 */
Heiko Carstens5add95d2009-01-14 14:14:08 +01004696SYSCALL_DEFINE1(sched_getscheduler, pid_t, pid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004697{
Ingo Molnar36c8b582006-07-03 00:25:41 -07004698 struct task_struct *p;
Andi Kleen3a5c3592007-10-15 17:00:14 +02004699 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004700
4701 if (pid < 0)
Andi Kleen3a5c3592007-10-15 17:00:14 +02004702 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004703
4704 retval = -ESRCH;
Thomas Gleixner5fe85be2009-12-09 10:14:58 +00004705 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004706 p = find_process_by_pid(pid);
4707 if (p) {
4708 retval = security_task_getscheduler(p);
4709 if (!retval)
Lennart Poetteringca94c442009-06-15 17:17:47 +02004710 retval = p->policy
4711 | (p->sched_reset_on_fork ? SCHED_RESET_ON_FORK : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004712 }
Thomas Gleixner5fe85be2009-12-09 10:14:58 +00004713 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004714 return retval;
4715}
4716
4717/**
Lennart Poetteringca94c442009-06-15 17:17:47 +02004718 * sys_sched_getparam - get the RT priority of a thread
Linus Torvalds1da177e2005-04-16 15:20:36 -07004719 * @pid: the pid in question.
4720 * @param: structure containing the RT priority.
4721 */
Heiko Carstens5add95d2009-01-14 14:14:08 +01004722SYSCALL_DEFINE2(sched_getparam, pid_t, pid, struct sched_param __user *, param)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004723{
4724 struct sched_param lp;
Ingo Molnar36c8b582006-07-03 00:25:41 -07004725 struct task_struct *p;
Andi Kleen3a5c3592007-10-15 17:00:14 +02004726 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004727
4728 if (!param || pid < 0)
Andi Kleen3a5c3592007-10-15 17:00:14 +02004729 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004730
Thomas Gleixner5fe85be2009-12-09 10:14:58 +00004731 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004732 p = find_process_by_pid(pid);
4733 retval = -ESRCH;
4734 if (!p)
4735 goto out_unlock;
4736
4737 retval = security_task_getscheduler(p);
4738 if (retval)
4739 goto out_unlock;
4740
4741 lp.sched_priority = p->rt_priority;
Thomas Gleixner5fe85be2009-12-09 10:14:58 +00004742 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004743
4744 /*
4745 * This one might sleep, we cannot do it with a spinlock held ...
4746 */
4747 retval = copy_to_user(param, &lp, sizeof(*param)) ? -EFAULT : 0;
4748
Linus Torvalds1da177e2005-04-16 15:20:36 -07004749 return retval;
4750
4751out_unlock:
Thomas Gleixner5fe85be2009-12-09 10:14:58 +00004752 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004753 return retval;
4754}
4755
Rusty Russell96f874e2008-11-25 02:35:14 +10304756long sched_setaffinity(pid_t pid, const struct cpumask *in_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004757{
Rusty Russell5a16f3d2008-11-25 02:35:11 +10304758 cpumask_var_t cpus_allowed, new_mask;
Ingo Molnar36c8b582006-07-03 00:25:41 -07004759 struct task_struct *p;
4760 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004761
Gautham R Shenoy95402b32008-01-25 21:08:02 +01004762 get_online_cpus();
Thomas Gleixner23f5d142009-12-09 10:15:01 +00004763 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004764
4765 p = find_process_by_pid(pid);
4766 if (!p) {
Thomas Gleixner23f5d142009-12-09 10:15:01 +00004767 rcu_read_unlock();
Gautham R Shenoy95402b32008-01-25 21:08:02 +01004768 put_online_cpus();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004769 return -ESRCH;
4770 }
4771
Thomas Gleixner23f5d142009-12-09 10:15:01 +00004772 /* Prevent p going away */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004773 get_task_struct(p);
Thomas Gleixner23f5d142009-12-09 10:15:01 +00004774 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004775
Rusty Russell5a16f3d2008-11-25 02:35:11 +10304776 if (!alloc_cpumask_var(&cpus_allowed, GFP_KERNEL)) {
4777 retval = -ENOMEM;
4778 goto out_put_task;
4779 }
4780 if (!alloc_cpumask_var(&new_mask, GFP_KERNEL)) {
4781 retval = -ENOMEM;
4782 goto out_free_cpus_allowed;
4783 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004784 retval = -EPERM;
David Howellsc69e8d92008-11-14 10:39:19 +11004785 if (!check_same_owner(p) && !capable(CAP_SYS_NICE))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004786 goto out_unlock;
4787
David Quigleye7834f82006-06-23 02:03:59 -07004788 retval = security_task_setscheduler(p, 0, NULL);
4789 if (retval)
4790 goto out_unlock;
4791
Rusty Russell5a16f3d2008-11-25 02:35:11 +10304792 cpuset_cpus_allowed(p, cpus_allowed);
4793 cpumask_and(new_mask, in_mask, cpus_allowed);
Paul Menage8707d8b2007-10-18 23:40:22 -07004794 again:
Rusty Russell5a16f3d2008-11-25 02:35:11 +10304795 retval = set_cpus_allowed_ptr(p, new_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004796
Paul Menage8707d8b2007-10-18 23:40:22 -07004797 if (!retval) {
Rusty Russell5a16f3d2008-11-25 02:35:11 +10304798 cpuset_cpus_allowed(p, cpus_allowed);
4799 if (!cpumask_subset(new_mask, cpus_allowed)) {
Paul Menage8707d8b2007-10-18 23:40:22 -07004800 /*
4801 * We must have raced with a concurrent cpuset
4802 * update. Just reset the cpus_allowed to the
4803 * cpuset's cpus_allowed
4804 */
Rusty Russell5a16f3d2008-11-25 02:35:11 +10304805 cpumask_copy(new_mask, cpus_allowed);
Paul Menage8707d8b2007-10-18 23:40:22 -07004806 goto again;
4807 }
4808 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004809out_unlock:
Rusty Russell5a16f3d2008-11-25 02:35:11 +10304810 free_cpumask_var(new_mask);
4811out_free_cpus_allowed:
4812 free_cpumask_var(cpus_allowed);
4813out_put_task:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004814 put_task_struct(p);
Gautham R Shenoy95402b32008-01-25 21:08:02 +01004815 put_online_cpus();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004816 return retval;
4817}
4818
4819static int get_user_cpu_mask(unsigned long __user *user_mask_ptr, unsigned len,
Rusty Russell96f874e2008-11-25 02:35:14 +10304820 struct cpumask *new_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004821{
Rusty Russell96f874e2008-11-25 02:35:14 +10304822 if (len < cpumask_size())
4823 cpumask_clear(new_mask);
4824 else if (len > cpumask_size())
4825 len = cpumask_size();
4826
Linus Torvalds1da177e2005-04-16 15:20:36 -07004827 return copy_from_user(new_mask, user_mask_ptr, len) ? -EFAULT : 0;
4828}
4829
4830/**
4831 * sys_sched_setaffinity - set the cpu affinity of a process
4832 * @pid: pid of the process
4833 * @len: length in bytes of the bitmask pointed to by user_mask_ptr
4834 * @user_mask_ptr: user-space pointer to the new cpu mask
4835 */
Heiko Carstens5add95d2009-01-14 14:14:08 +01004836SYSCALL_DEFINE3(sched_setaffinity, pid_t, pid, unsigned int, len,
4837 unsigned long __user *, user_mask_ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004838{
Rusty Russell5a16f3d2008-11-25 02:35:11 +10304839 cpumask_var_t new_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004840 int retval;
4841
Rusty Russell5a16f3d2008-11-25 02:35:11 +10304842 if (!alloc_cpumask_var(&new_mask, GFP_KERNEL))
4843 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004844
Rusty Russell5a16f3d2008-11-25 02:35:11 +10304845 retval = get_user_cpu_mask(user_mask_ptr, len, new_mask);
4846 if (retval == 0)
4847 retval = sched_setaffinity(pid, new_mask);
4848 free_cpumask_var(new_mask);
4849 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004850}
4851
Rusty Russell96f874e2008-11-25 02:35:14 +10304852long sched_getaffinity(pid_t pid, struct cpumask *mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004853{
Ingo Molnar36c8b582006-07-03 00:25:41 -07004854 struct task_struct *p;
Thomas Gleixner31605682009-12-08 20:24:16 +00004855 unsigned long flags;
4856 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004857 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004858
Gautham R Shenoy95402b32008-01-25 21:08:02 +01004859 get_online_cpus();
Thomas Gleixner23f5d142009-12-09 10:15:01 +00004860 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004861
4862 retval = -ESRCH;
4863 p = find_process_by_pid(pid);
4864 if (!p)
4865 goto out_unlock;
4866
David Quigleye7834f82006-06-23 02:03:59 -07004867 retval = security_task_getscheduler(p);
4868 if (retval)
4869 goto out_unlock;
4870
Thomas Gleixner31605682009-12-08 20:24:16 +00004871 rq = task_rq_lock(p, &flags);
Rusty Russell96f874e2008-11-25 02:35:14 +10304872 cpumask_and(mask, &p->cpus_allowed, cpu_online_mask);
Thomas Gleixner31605682009-12-08 20:24:16 +00004873 task_rq_unlock(rq, &flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004874
4875out_unlock:
Thomas Gleixner23f5d142009-12-09 10:15:01 +00004876 rcu_read_unlock();
Gautham R Shenoy95402b32008-01-25 21:08:02 +01004877 put_online_cpus();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004878
Ulrich Drepper9531b622007-08-09 11:16:46 +02004879 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004880}
4881
4882/**
4883 * sys_sched_getaffinity - get the cpu affinity of a process
4884 * @pid: pid of the process
4885 * @len: length in bytes of the bitmask pointed to by user_mask_ptr
4886 * @user_mask_ptr: user-space pointer to hold the current cpu mask
4887 */
Heiko Carstens5add95d2009-01-14 14:14:08 +01004888SYSCALL_DEFINE3(sched_getaffinity, pid_t, pid, unsigned int, len,
4889 unsigned long __user *, user_mask_ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004890{
4891 int ret;
Rusty Russellf17c8602008-11-25 02:35:11 +10304892 cpumask_var_t mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004893
Rusty Russellf17c8602008-11-25 02:35:11 +10304894 if (len < cpumask_size())
Linus Torvalds1da177e2005-04-16 15:20:36 -07004895 return -EINVAL;
4896
Rusty Russellf17c8602008-11-25 02:35:11 +10304897 if (!alloc_cpumask_var(&mask, GFP_KERNEL))
4898 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004899
Rusty Russellf17c8602008-11-25 02:35:11 +10304900 ret = sched_getaffinity(pid, mask);
4901 if (ret == 0) {
4902 if (copy_to_user(user_mask_ptr, mask, cpumask_size()))
4903 ret = -EFAULT;
4904 else
4905 ret = cpumask_size();
4906 }
4907 free_cpumask_var(mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004908
Rusty Russellf17c8602008-11-25 02:35:11 +10304909 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004910}
4911
4912/**
4913 * sys_sched_yield - yield the current processor to other threads.
4914 *
Ingo Molnardd41f592007-07-09 18:51:59 +02004915 * This function yields the current CPU to other tasks. If there are no
4916 * other threads running on this CPU then this function will return.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004917 */
Heiko Carstens5add95d2009-01-14 14:14:08 +01004918SYSCALL_DEFINE0(sched_yield)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004919{
Ingo Molnar70b97a72006-07-03 00:25:42 -07004920 struct rq *rq = this_rq_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004921
Ingo Molnar2d723762007-10-15 17:00:12 +02004922 schedstat_inc(rq, yld_count);
Dmitry Adamushko4530d7a2007-10-15 17:00:08 +02004923 current->sched_class->yield_task(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004924
4925 /*
4926 * Since we are going to call schedule() anyway, there's
4927 * no need to preempt or enable interrupts:
4928 */
4929 __release(rq->lock);
Ingo Molnar8a25d5d2006-07-03 00:24:54 -07004930 spin_release(&rq->lock.dep_map, 1, _THIS_IP_);
Thomas Gleixner9828ea92009-12-03 20:55:53 +01004931 do_raw_spin_unlock(&rq->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004932 preempt_enable_no_resched();
4933
4934 schedule();
4935
4936 return 0;
4937}
4938
Peter Zijlstrad86ee482009-07-10 14:57:57 +02004939static inline int should_resched(void)
4940{
4941 return need_resched() && !(preempt_count() & PREEMPT_ACTIVE);
4942}
4943
Andrew Mortone7b38402006-06-30 01:56:00 -07004944static void __cond_resched(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004945{
Frederic Weisbeckere7aaaa62009-07-16 15:44:29 +02004946 add_preempt_count(PREEMPT_ACTIVE);
4947 schedule();
4948 sub_preempt_count(PREEMPT_ACTIVE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004949}
4950
Herbert Xu02b67cc2008-01-25 21:08:28 +01004951int __sched _cond_resched(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004952{
Peter Zijlstrad86ee482009-07-10 14:57:57 +02004953 if (should_resched()) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004954 __cond_resched();
4955 return 1;
4956 }
4957 return 0;
4958}
Herbert Xu02b67cc2008-01-25 21:08:28 +01004959EXPORT_SYMBOL(_cond_resched);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004960
4961/*
Frederic Weisbecker613afbf2009-07-16 15:44:29 +02004962 * __cond_resched_lock() - if a reschedule is pending, drop the given lock,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004963 * call schedule, and on return reacquire the lock.
4964 *
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01004965 * This works OK both with and without CONFIG_PREEMPT. We do strange low-level
Linus Torvalds1da177e2005-04-16 15:20:36 -07004966 * operations here to prevent schedule() from being called twice (once via
4967 * spin_unlock(), once by hand).
4968 */
Frederic Weisbecker613afbf2009-07-16 15:44:29 +02004969int __cond_resched_lock(spinlock_t *lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004970{
Peter Zijlstrad86ee482009-07-10 14:57:57 +02004971 int resched = should_resched();
Jan Kara6df3cec2005-06-13 15:52:32 -07004972 int ret = 0;
4973
Peter Zijlstraf607c662009-07-20 19:16:29 +02004974 lockdep_assert_held(lock);
4975
Nick Piggin95c354f2008-01-30 13:31:20 +01004976 if (spin_needbreak(lock) || resched) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004977 spin_unlock(lock);
Peter Zijlstrad86ee482009-07-10 14:57:57 +02004978 if (resched)
Nick Piggin95c354f2008-01-30 13:31:20 +01004979 __cond_resched();
4980 else
4981 cpu_relax();
Jan Kara6df3cec2005-06-13 15:52:32 -07004982 ret = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004983 spin_lock(lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004984 }
Jan Kara6df3cec2005-06-13 15:52:32 -07004985 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004986}
Frederic Weisbecker613afbf2009-07-16 15:44:29 +02004987EXPORT_SYMBOL(__cond_resched_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004988
Frederic Weisbecker613afbf2009-07-16 15:44:29 +02004989int __sched __cond_resched_softirq(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004990{
4991 BUG_ON(!in_softirq());
4992
Peter Zijlstrad86ee482009-07-10 14:57:57 +02004993 if (should_resched()) {
Thomas Gleixner98d82562007-05-23 13:58:18 -07004994 local_bh_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004995 __cond_resched();
4996 local_bh_disable();
4997 return 1;
4998 }
4999 return 0;
5000}
Frederic Weisbecker613afbf2009-07-16 15:44:29 +02005001EXPORT_SYMBOL(__cond_resched_softirq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005002
Linus Torvalds1da177e2005-04-16 15:20:36 -07005003/**
5004 * yield - yield the current processor to other threads.
5005 *
Robert P. J. Day72fd4a32007-02-10 01:45:59 -08005006 * This is a shortcut for kernel-space yielding - it marks the
Linus Torvalds1da177e2005-04-16 15:20:36 -07005007 * thread runnable and calls sys_sched_yield().
5008 */
5009void __sched yield(void)
5010{
5011 set_current_state(TASK_RUNNING);
5012 sys_sched_yield();
5013}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005014EXPORT_SYMBOL(yield);
5015
5016/*
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01005017 * This task is about to go to sleep on IO. Increment rq->nr_iowait so
Linus Torvalds1da177e2005-04-16 15:20:36 -07005018 * that process accounting knows that this is a task in IO wait state.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005019 */
5020void __sched io_schedule(void)
5021{
Hitoshi Mitake54d35f22009-06-29 14:44:57 +09005022 struct rq *rq = raw_rq();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005023
Shailabh Nagar0ff92242006-07-14 00:24:37 -07005024 delayacct_blkio_start();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005025 atomic_inc(&rq->nr_iowait);
Arjan van de Ven8f0dfc32009-07-20 11:26:58 -07005026 current->in_iowait = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005027 schedule();
Arjan van de Ven8f0dfc32009-07-20 11:26:58 -07005028 current->in_iowait = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005029 atomic_dec(&rq->nr_iowait);
Shailabh Nagar0ff92242006-07-14 00:24:37 -07005030 delayacct_blkio_end();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005031}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005032EXPORT_SYMBOL(io_schedule);
5033
5034long __sched io_schedule_timeout(long timeout)
5035{
Hitoshi Mitake54d35f22009-06-29 14:44:57 +09005036 struct rq *rq = raw_rq();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005037 long ret;
5038
Shailabh Nagar0ff92242006-07-14 00:24:37 -07005039 delayacct_blkio_start();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005040 atomic_inc(&rq->nr_iowait);
Arjan van de Ven8f0dfc32009-07-20 11:26:58 -07005041 current->in_iowait = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005042 ret = schedule_timeout(timeout);
Arjan van de Ven8f0dfc32009-07-20 11:26:58 -07005043 current->in_iowait = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005044 atomic_dec(&rq->nr_iowait);
Shailabh Nagar0ff92242006-07-14 00:24:37 -07005045 delayacct_blkio_end();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005046 return ret;
5047}
5048
5049/**
5050 * sys_sched_get_priority_max - return maximum RT priority.
5051 * @policy: scheduling class.
5052 *
5053 * this syscall returns the maximum rt_priority that can be used
5054 * by a given scheduling class.
5055 */
Heiko Carstens5add95d2009-01-14 14:14:08 +01005056SYSCALL_DEFINE1(sched_get_priority_max, int, policy)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005057{
5058 int ret = -EINVAL;
5059
5060 switch (policy) {
5061 case SCHED_FIFO:
5062 case SCHED_RR:
5063 ret = MAX_USER_RT_PRIO-1;
5064 break;
5065 case SCHED_NORMAL:
Ingo Molnarb0a94992006-01-14 13:20:41 -08005066 case SCHED_BATCH:
Ingo Molnardd41f592007-07-09 18:51:59 +02005067 case SCHED_IDLE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005068 ret = 0;
5069 break;
5070 }
5071 return ret;
5072}
5073
5074/**
5075 * sys_sched_get_priority_min - return minimum RT priority.
5076 * @policy: scheduling class.
5077 *
5078 * this syscall returns the minimum rt_priority that can be used
5079 * by a given scheduling class.
5080 */
Heiko Carstens5add95d2009-01-14 14:14:08 +01005081SYSCALL_DEFINE1(sched_get_priority_min, int, policy)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005082{
5083 int ret = -EINVAL;
5084
5085 switch (policy) {
5086 case SCHED_FIFO:
5087 case SCHED_RR:
5088 ret = 1;
5089 break;
5090 case SCHED_NORMAL:
Ingo Molnarb0a94992006-01-14 13:20:41 -08005091 case SCHED_BATCH:
Ingo Molnardd41f592007-07-09 18:51:59 +02005092 case SCHED_IDLE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005093 ret = 0;
5094 }
5095 return ret;
5096}
5097
5098/**
5099 * sys_sched_rr_get_interval - return the default timeslice of a process.
5100 * @pid: pid of the process.
5101 * @interval: userspace pointer to the timeslice value.
5102 *
5103 * this syscall writes the default timeslice value of a given process
5104 * into the user-space timespec buffer. A value of '0' means infinity.
5105 */
Heiko Carstens17da2bd2009-01-14 14:14:10 +01005106SYSCALL_DEFINE2(sched_rr_get_interval, pid_t, pid,
Heiko Carstens754fe8d2009-01-14 14:14:09 +01005107 struct timespec __user *, interval)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005108{
Ingo Molnar36c8b582006-07-03 00:25:41 -07005109 struct task_struct *p;
Dmitry Adamushkoa4ec24b2007-10-15 17:00:13 +02005110 unsigned int time_slice;
Thomas Gleixnerdba091b2009-12-09 09:32:03 +01005111 unsigned long flags;
5112 struct rq *rq;
Andi Kleen3a5c3592007-10-15 17:00:14 +02005113 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005114 struct timespec t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005115
5116 if (pid < 0)
Andi Kleen3a5c3592007-10-15 17:00:14 +02005117 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005118
5119 retval = -ESRCH;
Thomas Gleixner1a551ae2009-12-09 10:15:11 +00005120 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005121 p = find_process_by_pid(pid);
5122 if (!p)
5123 goto out_unlock;
5124
5125 retval = security_task_getscheduler(p);
5126 if (retval)
5127 goto out_unlock;
5128
Thomas Gleixnerdba091b2009-12-09 09:32:03 +01005129 rq = task_rq_lock(p, &flags);
5130 time_slice = p->sched_class->get_rr_interval(rq, p);
5131 task_rq_unlock(rq, &flags);
Dmitry Adamushkoa4ec24b2007-10-15 17:00:13 +02005132
Thomas Gleixner1a551ae2009-12-09 10:15:11 +00005133 rcu_read_unlock();
Dmitry Adamushkoa4ec24b2007-10-15 17:00:13 +02005134 jiffies_to_timespec(time_slice, &t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005135 retval = copy_to_user(interval, &t, sizeof(t)) ? -EFAULT : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005136 return retval;
Andi Kleen3a5c3592007-10-15 17:00:14 +02005137
Linus Torvalds1da177e2005-04-16 15:20:36 -07005138out_unlock:
Thomas Gleixner1a551ae2009-12-09 10:15:11 +00005139 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005140 return retval;
5141}
5142
Steven Rostedt7c731e02008-05-12 21:20:41 +02005143static const char stat_nam[] = TASK_STATE_TO_CHAR_STR;
Ingo Molnar36c8b582006-07-03 00:25:41 -07005144
Ingo Molnar82a1fcb2008-01-25 21:08:02 +01005145void sched_show_task(struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005146{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005147 unsigned long free = 0;
Ingo Molnar36c8b582006-07-03 00:25:41 -07005148 unsigned state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005149
Linus Torvalds1da177e2005-04-16 15:20:36 -07005150 state = p->state ? __ffs(p->state) + 1 : 0;
Peter Zijlstra3df0fc52009-12-20 14:23:57 +01005151 printk(KERN_INFO "%-13.13s %c", p->comm,
Andreas Mohr2ed6e342006-07-10 04:43:52 -07005152 state < sizeof(stat_nam) - 1 ? stat_nam[state] : '?');
Ingo Molnar4bd77322007-07-11 21:21:47 +02005153#if BITS_PER_LONG == 32
Linus Torvalds1da177e2005-04-16 15:20:36 -07005154 if (state == TASK_RUNNING)
Peter Zijlstra3df0fc52009-12-20 14:23:57 +01005155 printk(KERN_CONT " running ");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005156 else
Peter Zijlstra3df0fc52009-12-20 14:23:57 +01005157 printk(KERN_CONT " %08lx ", thread_saved_pc(p));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005158#else
5159 if (state == TASK_RUNNING)
Peter Zijlstra3df0fc52009-12-20 14:23:57 +01005160 printk(KERN_CONT " running task ");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005161 else
Peter Zijlstra3df0fc52009-12-20 14:23:57 +01005162 printk(KERN_CONT " %016lx ", thread_saved_pc(p));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005163#endif
5164#ifdef CONFIG_DEBUG_STACK_USAGE
Eric Sandeen7c9f8862008-04-22 16:38:23 -05005165 free = stack_not_used(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005166#endif
Peter Zijlstra3df0fc52009-12-20 14:23:57 +01005167 printk(KERN_CONT "%5lu %5d %6d 0x%08lx\n", free,
David Rientjesaa47b7e2009-05-04 01:38:05 -07005168 task_pid_nr(p), task_pid_nr(p->real_parent),
5169 (unsigned long)task_thread_info(p)->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005170
Nick Piggin5fb5e6d2008-01-25 21:08:34 +01005171 show_stack(p, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005172}
5173
Ingo Molnare59e2ae2006-12-06 20:35:59 -08005174void show_state_filter(unsigned long state_filter)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005175{
Ingo Molnar36c8b582006-07-03 00:25:41 -07005176 struct task_struct *g, *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005177
Ingo Molnar4bd77322007-07-11 21:21:47 +02005178#if BITS_PER_LONG == 32
Peter Zijlstra3df0fc52009-12-20 14:23:57 +01005179 printk(KERN_INFO
5180 " task PC stack pid father\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005181#else
Peter Zijlstra3df0fc52009-12-20 14:23:57 +01005182 printk(KERN_INFO
5183 " task PC stack pid father\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005184#endif
5185 read_lock(&tasklist_lock);
5186 do_each_thread(g, p) {
5187 /*
5188 * reset the NMI-timeout, listing all files on a slow
5189 * console might take alot of time:
5190 */
5191 touch_nmi_watchdog();
Ingo Molnar39bc89f2007-04-25 20:50:03 -07005192 if (!state_filter || (p->state & state_filter))
Ingo Molnar82a1fcb2008-01-25 21:08:02 +01005193 sched_show_task(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005194 } while_each_thread(g, p);
5195
Jeremy Fitzhardinge04c91672007-05-08 00:28:05 -07005196 touch_all_softlockup_watchdogs();
5197
Ingo Molnardd41f592007-07-09 18:51:59 +02005198#ifdef CONFIG_SCHED_DEBUG
5199 sysrq_sched_debug_show();
5200#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07005201 read_unlock(&tasklist_lock);
Ingo Molnare59e2ae2006-12-06 20:35:59 -08005202 /*
5203 * Only show locks if all tasks are dumped:
5204 */
Shmulik Ladkani93335a22009-11-25 15:23:41 +02005205 if (!state_filter)
Ingo Molnare59e2ae2006-12-06 20:35:59 -08005206 debug_show_all_locks();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005207}
5208
Ingo Molnar1df21052007-07-09 18:51:58 +02005209void __cpuinit init_idle_bootup_task(struct task_struct *idle)
5210{
Ingo Molnardd41f592007-07-09 18:51:59 +02005211 idle->sched_class = &idle_sched_class;
Ingo Molnar1df21052007-07-09 18:51:58 +02005212}
5213
Ingo Molnarf340c0d2005-06-28 16:40:42 +02005214/**
5215 * init_idle - set up an idle thread for a given CPU
5216 * @idle: task in question
5217 * @cpu: cpu the idle task belongs to
5218 *
5219 * NOTE: this function does not set the idle thread's NEED_RESCHED
5220 * flag, to make booting more robust.
5221 */
Nick Piggin5c1e1762006-10-03 01:14:04 -07005222void __cpuinit init_idle(struct task_struct *idle, int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005223{
Ingo Molnar70b97a72006-07-03 00:25:42 -07005224 struct rq *rq = cpu_rq(cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005225 unsigned long flags;
5226
Thomas Gleixner05fa7852009-11-17 14:28:38 +01005227 raw_spin_lock_irqsave(&rq->lock, flags);
Ingo Molnar5cbd54e2008-11-12 20:05:50 +01005228
Ingo Molnardd41f592007-07-09 18:51:59 +02005229 __sched_fork(idle);
Peter Zijlstra06b83b52009-12-16 18:04:35 +01005230 idle->state = TASK_RUNNING;
Ingo Molnardd41f592007-07-09 18:51:59 +02005231 idle->se.exec_start = sched_clock();
5232
Rusty Russell96f874e2008-11-25 02:35:14 +10305233 cpumask_copy(&idle->cpus_allowed, cpumask_of(cpu));
Ingo Molnardd41f592007-07-09 18:51:59 +02005234 __set_task_cpu(idle, cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005235
Linus Torvalds1da177e2005-04-16 15:20:36 -07005236 rq->curr = rq->idle = idle;
Nick Piggin4866cde2005-06-25 14:57:23 -07005237#if defined(CONFIG_SMP) && defined(__ARCH_WANT_UNLOCKED_CTXSW)
5238 idle->oncpu = 1;
5239#endif
Thomas Gleixner05fa7852009-11-17 14:28:38 +01005240 raw_spin_unlock_irqrestore(&rq->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005241
5242 /* Set the preempt count _outside_ the spinlocks! */
Linus Torvalds8e3e0762008-05-10 20:58:02 -07005243#if defined(CONFIG_PREEMPT)
5244 task_thread_info(idle)->preempt_count = (idle->lock_depth >= 0);
5245#else
Al Viroa1261f52005-11-13 16:06:55 -08005246 task_thread_info(idle)->preempt_count = 0;
Linus Torvalds8e3e0762008-05-10 20:58:02 -07005247#endif
Ingo Molnardd41f592007-07-09 18:51:59 +02005248 /*
5249 * The idle tasks have their own, simple scheduling class:
5250 */
5251 idle->sched_class = &idle_sched_class;
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01005252 ftrace_graph_init_task(idle);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005253}
5254
5255/*
5256 * In a system that switches off the HZ timer nohz_cpu_mask
5257 * indicates which cpus entered this state. This is used
5258 * in the rcu update to wait only for active cpus. For system
5259 * which do not switch off the HZ timer nohz_cpu_mask should
Rusty Russell6a7b3dc2008-11-25 02:35:04 +10305260 * always be CPU_BITS_NONE.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005261 */
Rusty Russell6a7b3dc2008-11-25 02:35:04 +10305262cpumask_var_t nohz_cpu_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005263
Ingo Molnar19978ca2007-11-09 22:39:38 +01005264/*
5265 * Increase the granularity value when there are more CPUs,
5266 * because with more CPUs the 'effective latency' as visible
5267 * to users decreases. But the relationship is not linear,
5268 * so pick a second-best guess by going with the log2 of the
5269 * number of CPUs.
5270 *
5271 * This idea comes from the SD scheduler of Con Kolivas:
5272 */
Christian Ehrhardtacb4a842009-11-30 12:16:48 +01005273static int get_update_sysctl_factor(void)
Christian Ehrhardt0bcdcf22009-11-30 12:16:46 +01005274{
Mike Galbraith4ca3ef72009-12-10 09:25:53 +01005275 unsigned int cpus = min_t(int, num_online_cpus(), 8);
Christian Ehrhardt1983a922009-11-30 12:16:47 +01005276 unsigned int factor;
5277
5278 switch (sysctl_sched_tunable_scaling) {
5279 case SCHED_TUNABLESCALING_NONE:
5280 factor = 1;
5281 break;
5282 case SCHED_TUNABLESCALING_LINEAR:
5283 factor = cpus;
5284 break;
5285 case SCHED_TUNABLESCALING_LOG:
5286 default:
5287 factor = 1 + ilog2(cpus);
5288 break;
5289 }
Christian Ehrhardt0bcdcf22009-11-30 12:16:46 +01005290
Christian Ehrhardtacb4a842009-11-30 12:16:48 +01005291 return factor;
5292}
5293
5294static void update_sysctl(void)
5295{
5296 unsigned int factor = get_update_sysctl_factor();
5297
Christian Ehrhardt0bcdcf22009-11-30 12:16:46 +01005298#define SET_SYSCTL(name) \
5299 (sysctl_##name = (factor) * normalized_sysctl_##name)
5300 SET_SYSCTL(sched_min_granularity);
5301 SET_SYSCTL(sched_latency);
5302 SET_SYSCTL(sched_wakeup_granularity);
5303 SET_SYSCTL(sched_shares_ratelimit);
5304#undef SET_SYSCTL
5305}
5306
Ingo Molnar19978ca2007-11-09 22:39:38 +01005307static inline void sched_init_granularity(void)
5308{
Christian Ehrhardt0bcdcf22009-11-30 12:16:46 +01005309 update_sysctl();
Ingo Molnar19978ca2007-11-09 22:39:38 +01005310}
5311
Linus Torvalds1da177e2005-04-16 15:20:36 -07005312#ifdef CONFIG_SMP
5313/*
5314 * This is how migration works:
5315 *
Ingo Molnar70b97a72006-07-03 00:25:42 -07005316 * 1) we queue a struct migration_req structure in the source CPU's
Linus Torvalds1da177e2005-04-16 15:20:36 -07005317 * runqueue and wake up that CPU's migration thread.
5318 * 2) we down() the locked semaphore => thread blocks.
5319 * 3) migration thread wakes up (implicitly it forces the migrated
5320 * thread off the CPU)
5321 * 4) it gets the migration request and checks whether the migrated
5322 * task is still in the wrong runqueue.
5323 * 5) if it's in the wrong runqueue then the migration thread removes
5324 * it and puts it into the right queue.
5325 * 6) migration thread up()s the semaphore.
5326 * 7) we wake up and the migration is done.
5327 */
5328
5329/*
5330 * Change a given task's CPU affinity. Migrate the thread to a
5331 * proper CPU and schedule it away if the CPU it's executing on
5332 * is removed from the allowed bitmask.
5333 *
5334 * NOTE: the caller must have a valid reference to the task, the
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01005335 * task must not exit() & deallocate itself prematurely. The
Linus Torvalds1da177e2005-04-16 15:20:36 -07005336 * call is not atomic; no spinlocks may be held.
5337 */
Rusty Russell96f874e2008-11-25 02:35:14 +10305338int set_cpus_allowed_ptr(struct task_struct *p, const struct cpumask *new_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005339{
Ingo Molnar70b97a72006-07-03 00:25:42 -07005340 struct migration_req req;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005341 unsigned long flags;
Ingo Molnar70b97a72006-07-03 00:25:42 -07005342 struct rq *rq;
Ingo Molnar48f24c42006-07-03 00:25:40 -07005343 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005344
5345 rq = task_rq_lock(p, &flags);
Peter Zijlstrae2912002009-12-16 18:04:36 +01005346
Peter Zijlstra6ad4c182009-11-25 13:31:39 +01005347 if (!cpumask_intersects(new_mask, cpu_active_mask)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005348 ret = -EINVAL;
5349 goto out;
5350 }
5351
David Rientjes9985b0b2008-06-05 12:57:11 -07005352 if (unlikely((p->flags & PF_THREAD_BOUND) && p != current &&
Rusty Russell96f874e2008-11-25 02:35:14 +10305353 !cpumask_equal(&p->cpus_allowed, new_mask))) {
David Rientjes9985b0b2008-06-05 12:57:11 -07005354 ret = -EINVAL;
5355 goto out;
5356 }
5357
Gregory Haskins73fe6aa2008-01-25 21:08:07 +01005358 if (p->sched_class->set_cpus_allowed)
Mike Traviscd8ba7c2008-03-26 14:23:49 -07005359 p->sched_class->set_cpus_allowed(p, new_mask);
Gregory Haskins73fe6aa2008-01-25 21:08:07 +01005360 else {
Rusty Russell96f874e2008-11-25 02:35:14 +10305361 cpumask_copy(&p->cpus_allowed, new_mask);
5362 p->rt.nr_cpus_allowed = cpumask_weight(new_mask);
Gregory Haskins73fe6aa2008-01-25 21:08:07 +01005363 }
5364
Linus Torvalds1da177e2005-04-16 15:20:36 -07005365 /* Can the task run on the task's current CPU? If so, we're done */
Rusty Russell96f874e2008-11-25 02:35:14 +10305366 if (cpumask_test_cpu(task_cpu(p), new_mask))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005367 goto out;
5368
Peter Zijlstra6ad4c182009-11-25 13:31:39 +01005369 if (migrate_task(p, cpumask_any_and(cpu_active_mask, new_mask), &req)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005370 /* Need help from migration thread: drop lock and wait. */
Peter Zijlstra693525e2009-07-21 13:56:38 +02005371 struct task_struct *mt = rq->migration_thread;
5372
5373 get_task_struct(mt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005374 task_rq_unlock(rq, &flags);
5375 wake_up_process(rq->migration_thread);
Peter Zijlstra693525e2009-07-21 13:56:38 +02005376 put_task_struct(mt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005377 wait_for_completion(&req.done);
5378 tlb_migrate_finish(p->mm);
5379 return 0;
5380 }
5381out:
5382 task_rq_unlock(rq, &flags);
Ingo Molnar48f24c42006-07-03 00:25:40 -07005383
Linus Torvalds1da177e2005-04-16 15:20:36 -07005384 return ret;
5385}
Mike Traviscd8ba7c2008-03-26 14:23:49 -07005386EXPORT_SYMBOL_GPL(set_cpus_allowed_ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005387
5388/*
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01005389 * Move (not current) task off this cpu, onto dest cpu. We're doing
Linus Torvalds1da177e2005-04-16 15:20:36 -07005390 * this because either it can't run here any more (set_cpus_allowed()
5391 * away from this CPU, or CPU going down), or because we're
5392 * attempting to rebalance this task on exec (sched_exec).
5393 *
5394 * So we race with normal scheduler movements, but that's OK, as long
5395 * as the task is no longer on this CPU.
Kirill Korotaevefc30812006-06-27 02:54:32 -07005396 *
5397 * Returns non-zero if task was successfully migrated.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005398 */
Kirill Korotaevefc30812006-06-27 02:54:32 -07005399static int __migrate_task(struct task_struct *p, int src_cpu, int dest_cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005400{
Ingo Molnar70b97a72006-07-03 00:25:42 -07005401 struct rq *rq_dest, *rq_src;
Peter Zijlstrae2912002009-12-16 18:04:36 +01005402 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005403
Max Krasnyanskye761b772008-07-15 04:43:49 -07005404 if (unlikely(!cpu_active(dest_cpu)))
Kirill Korotaevefc30812006-06-27 02:54:32 -07005405 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005406
5407 rq_src = cpu_rq(src_cpu);
5408 rq_dest = cpu_rq(dest_cpu);
5409
5410 double_rq_lock(rq_src, rq_dest);
5411 /* Already moved. */
5412 if (task_cpu(p) != src_cpu)
Linus Torvaldsb1e38732008-07-10 11:25:03 -07005413 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005414 /* Affinity changed (again). */
Rusty Russell96f874e2008-11-25 02:35:14 +10305415 if (!cpumask_test_cpu(dest_cpu, &p->cpus_allowed))
Linus Torvaldsb1e38732008-07-10 11:25:03 -07005416 goto fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005417
Peter Zijlstrae2912002009-12-16 18:04:36 +01005418 /*
5419 * If we're not on a rq, the next wake-up will ensure we're
5420 * placed properly.
5421 */
5422 if (p->se.on_rq) {
Ingo Molnar2e1cb742007-08-09 11:16:49 +02005423 deactivate_task(rq_src, p, 0);
Peter Zijlstrae2912002009-12-16 18:04:36 +01005424 set_task_cpu(p, dest_cpu);
Ingo Molnardd41f592007-07-09 18:51:59 +02005425 activate_task(rq_dest, p, 0);
Peter Zijlstra15afe092008-09-20 23:38:02 +02005426 check_preempt_curr(rq_dest, p, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005427 }
Linus Torvaldsb1e38732008-07-10 11:25:03 -07005428done:
Kirill Korotaevefc30812006-06-27 02:54:32 -07005429 ret = 1;
Linus Torvaldsb1e38732008-07-10 11:25:03 -07005430fail:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005431 double_rq_unlock(rq_src, rq_dest);
Kirill Korotaevefc30812006-06-27 02:54:32 -07005432 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005433}
5434
Paul E. McKenney03b042b2009-06-25 09:08:16 -07005435#define RCU_MIGRATION_IDLE 0
5436#define RCU_MIGRATION_NEED_QS 1
5437#define RCU_MIGRATION_GOT_QS 2
5438#define RCU_MIGRATION_MUST_SYNC 3
5439
Linus Torvalds1da177e2005-04-16 15:20:36 -07005440/*
5441 * migration_thread - this is a highprio system thread that performs
5442 * thread migration by bumping thread off CPU then 'pushing' onto
5443 * another runqueue.
5444 */
Ingo Molnar95cdf3b2005-09-10 00:26:11 -07005445static int migration_thread(void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005446{
Paul E. McKenney03b042b2009-06-25 09:08:16 -07005447 int badcpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005448 int cpu = (long)data;
Ingo Molnar70b97a72006-07-03 00:25:42 -07005449 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005450
5451 rq = cpu_rq(cpu);
5452 BUG_ON(rq->migration_thread != current);
5453
5454 set_current_state(TASK_INTERRUPTIBLE);
5455 while (!kthread_should_stop()) {
Ingo Molnar70b97a72006-07-03 00:25:42 -07005456 struct migration_req *req;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005457 struct list_head *head;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005458
Thomas Gleixner05fa7852009-11-17 14:28:38 +01005459 raw_spin_lock_irq(&rq->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005460
5461 if (cpu_is_offline(cpu)) {
Thomas Gleixner05fa7852009-11-17 14:28:38 +01005462 raw_spin_unlock_irq(&rq->lock);
Oleg Nesterov371cbb32009-06-17 16:27:45 -07005463 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005464 }
5465
5466 if (rq->active_balance) {
5467 active_load_balance(rq, cpu);
5468 rq->active_balance = 0;
5469 }
5470
5471 head = &rq->migration_queue;
5472
5473 if (list_empty(head)) {
Thomas Gleixner05fa7852009-11-17 14:28:38 +01005474 raw_spin_unlock_irq(&rq->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005475 schedule();
5476 set_current_state(TASK_INTERRUPTIBLE);
5477 continue;
5478 }
Ingo Molnar70b97a72006-07-03 00:25:42 -07005479 req = list_entry(head->next, struct migration_req, list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005480 list_del_init(head->next);
5481
Paul E. McKenney03b042b2009-06-25 09:08:16 -07005482 if (req->task != NULL) {
Thomas Gleixner05fa7852009-11-17 14:28:38 +01005483 raw_spin_unlock(&rq->lock);
Paul E. McKenney03b042b2009-06-25 09:08:16 -07005484 __migrate_task(req->task, cpu, req->dest_cpu);
5485 } else if (likely(cpu == (badcpu = smp_processor_id()))) {
5486 req->dest_cpu = RCU_MIGRATION_GOT_QS;
Thomas Gleixner05fa7852009-11-17 14:28:38 +01005487 raw_spin_unlock(&rq->lock);
Paul E. McKenney03b042b2009-06-25 09:08:16 -07005488 } else {
5489 req->dest_cpu = RCU_MIGRATION_MUST_SYNC;
Thomas Gleixner05fa7852009-11-17 14:28:38 +01005490 raw_spin_unlock(&rq->lock);
Paul E. McKenney03b042b2009-06-25 09:08:16 -07005491 WARN_ONCE(1, "migration_thread() on CPU %d, expected %d\n", badcpu, cpu);
5492 }
Nick Piggin674311d2005-06-25 14:57:27 -07005493 local_irq_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005494
5495 complete(&req->done);
5496 }
5497 __set_current_state(TASK_RUNNING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005498
Linus Torvalds1da177e2005-04-16 15:20:36 -07005499 return 0;
5500}
5501
5502#ifdef CONFIG_HOTPLUG_CPU
Oleg Nesterovf7b4cdd2007-10-16 23:30:56 -07005503
5504static int __migrate_task_irq(struct task_struct *p, int src_cpu, int dest_cpu)
5505{
5506 int ret;
5507
5508 local_irq_disable();
5509 ret = __migrate_task(p, src_cpu, dest_cpu);
5510 local_irq_enable();
5511 return ret;
5512}
5513
Kirill Korotaev054b9102006-12-10 02:20:11 -08005514/*
Robert P. J. Day3a4fa0a2007-10-19 23:10:43 +02005515 * Figure out where task on dead CPU should go, use force if necessary.
Kirill Korotaev054b9102006-12-10 02:20:11 -08005516 */
Ingo Molnar48f24c42006-07-03 00:25:40 -07005517static void move_task_off_dead_cpu(int dead_cpu, struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005518{
Ingo Molnar70b97a72006-07-03 00:25:42 -07005519 int dest_cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005520
Rusty Russelle76bd8d2008-11-25 02:35:11 +10305521again:
Peter Zijlstra5da9a0f2009-12-16 18:04:38 +01005522 dest_cpu = select_fallback_rq(dead_cpu, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005523
Rusty Russelle76bd8d2008-11-25 02:35:11 +10305524 /* It can have affinity changed while we were choosing. */
5525 if (unlikely(!__migrate_task_irq(p, dead_cpu, dest_cpu)))
5526 goto again;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005527}
5528
5529/*
5530 * While a dead CPU has no uninterruptible tasks queued at this point,
5531 * it might still have a nonzero ->nr_uninterruptible counter, because
5532 * for performance reasons the counter is not stricly tracking tasks to
5533 * their home CPUs. So we just add the counter to another CPU's counter,
5534 * to keep the global sum constant after CPU-down:
5535 */
Ingo Molnar70b97a72006-07-03 00:25:42 -07005536static void migrate_nr_uninterruptible(struct rq *rq_src)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005537{
Peter Zijlstra6ad4c182009-11-25 13:31:39 +01005538 struct rq *rq_dest = cpu_rq(cpumask_any(cpu_active_mask));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005539 unsigned long flags;
5540
5541 local_irq_save(flags);
5542 double_rq_lock(rq_src, rq_dest);
5543 rq_dest->nr_uninterruptible += rq_src->nr_uninterruptible;
5544 rq_src->nr_uninterruptible = 0;
5545 double_rq_unlock(rq_src, rq_dest);
5546 local_irq_restore(flags);
5547}
5548
5549/* Run through task list and migrate tasks from the dead cpu. */
5550static void migrate_live_tasks(int src_cpu)
5551{
Ingo Molnar48f24c42006-07-03 00:25:40 -07005552 struct task_struct *p, *t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005553
Oleg Nesterovf7b4cdd2007-10-16 23:30:56 -07005554 read_lock(&tasklist_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005555
Ingo Molnar48f24c42006-07-03 00:25:40 -07005556 do_each_thread(t, p) {
5557 if (p == current)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005558 continue;
5559
Ingo Molnar48f24c42006-07-03 00:25:40 -07005560 if (task_cpu(p) == src_cpu)
5561 move_task_off_dead_cpu(src_cpu, p);
5562 } while_each_thread(t, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005563
Oleg Nesterovf7b4cdd2007-10-16 23:30:56 -07005564 read_unlock(&tasklist_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005565}
5566
Ingo Molnardd41f592007-07-09 18:51:59 +02005567/*
5568 * Schedules idle task to be the next runnable task on current CPU.
Dmitry Adamushko94bc9a72007-11-15 20:57:40 +01005569 * It does so by boosting its priority to highest possible.
5570 * Used by CPU offline code.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005571 */
5572void sched_idle_next(void)
5573{
Ingo Molnar48f24c42006-07-03 00:25:40 -07005574 int this_cpu = smp_processor_id();
Ingo Molnar70b97a72006-07-03 00:25:42 -07005575 struct rq *rq = cpu_rq(this_cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005576 struct task_struct *p = rq->idle;
5577 unsigned long flags;
5578
5579 /* cpu has to be offline */
Ingo Molnar48f24c42006-07-03 00:25:40 -07005580 BUG_ON(cpu_online(this_cpu));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005581
Ingo Molnar48f24c42006-07-03 00:25:40 -07005582 /*
5583 * Strictly not necessary since rest of the CPUs are stopped by now
5584 * and interrupts disabled on the current cpu.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005585 */
Thomas Gleixner05fa7852009-11-17 14:28:38 +01005586 raw_spin_lock_irqsave(&rq->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005587
Ingo Molnardd41f592007-07-09 18:51:59 +02005588 __setscheduler(rq, p, SCHED_FIFO, MAX_RT_PRIO-1);
Ingo Molnar48f24c42006-07-03 00:25:40 -07005589
Dmitry Adamushko94bc9a72007-11-15 20:57:40 +01005590 update_rq_clock(rq);
5591 activate_task(rq, p, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005592
Thomas Gleixner05fa7852009-11-17 14:28:38 +01005593 raw_spin_unlock_irqrestore(&rq->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005594}
5595
Ingo Molnar48f24c42006-07-03 00:25:40 -07005596/*
5597 * Ensures that the idle task is using init_mm right before its cpu goes
Linus Torvalds1da177e2005-04-16 15:20:36 -07005598 * offline.
5599 */
5600void idle_task_exit(void)
5601{
5602 struct mm_struct *mm = current->active_mm;
5603
5604 BUG_ON(cpu_online(smp_processor_id()));
5605
5606 if (mm != &init_mm)
5607 switch_mm(mm, &init_mm, current);
5608 mmdrop(mm);
5609}
5610
Kirill Korotaev054b9102006-12-10 02:20:11 -08005611/* called under rq->lock with disabled interrupts */
Ingo Molnar36c8b582006-07-03 00:25:41 -07005612static void migrate_dead(unsigned int dead_cpu, struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005613{
Ingo Molnar70b97a72006-07-03 00:25:42 -07005614 struct rq *rq = cpu_rq(dead_cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005615
5616 /* Must be exiting, otherwise would be on tasklist. */
Eugene Teo270f7222007-10-18 23:40:38 -07005617 BUG_ON(!p->exit_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005618
5619 /* Cannot have done final schedule yet: would have vanished. */
Oleg Nesterovc394cc92006-09-29 02:01:11 -07005620 BUG_ON(p->state == TASK_DEAD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005621
Ingo Molnar48f24c42006-07-03 00:25:40 -07005622 get_task_struct(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005623
5624 /*
5625 * Drop lock around migration; if someone else moves it,
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01005626 * that's OK. No task can be added to this CPU, so iteration is
Linus Torvalds1da177e2005-04-16 15:20:36 -07005627 * fine.
5628 */
Thomas Gleixner05fa7852009-11-17 14:28:38 +01005629 raw_spin_unlock_irq(&rq->lock);
Ingo Molnar48f24c42006-07-03 00:25:40 -07005630 move_task_off_dead_cpu(dead_cpu, p);
Thomas Gleixner05fa7852009-11-17 14:28:38 +01005631 raw_spin_lock_irq(&rq->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005632
Ingo Molnar48f24c42006-07-03 00:25:40 -07005633 put_task_struct(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005634}
5635
5636/* release_task() removes task from tasklist, so we won't find dead tasks. */
5637static void migrate_dead_tasks(unsigned int dead_cpu)
5638{
Ingo Molnar70b97a72006-07-03 00:25:42 -07005639 struct rq *rq = cpu_rq(dead_cpu);
Ingo Molnardd41f592007-07-09 18:51:59 +02005640 struct task_struct *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005641
Ingo Molnardd41f592007-07-09 18:51:59 +02005642 for ( ; ; ) {
5643 if (!rq->nr_running)
5644 break;
Ingo Molnara8e504d2007-08-09 11:16:47 +02005645 update_rq_clock(rq);
Wang Chenb67802e2009-03-02 13:55:26 +08005646 next = pick_next_task(rq);
Ingo Molnardd41f592007-07-09 18:51:59 +02005647 if (!next)
5648 break;
Dmitry Adamushko79c53792008-06-29 00:16:56 +02005649 next->sched_class->put_prev_task(rq, next);
Ingo Molnardd41f592007-07-09 18:51:59 +02005650 migrate_dead(dead_cpu, next);
Nick Piggine692ab52007-07-26 13:40:43 +02005651
Linus Torvalds1da177e2005-04-16 15:20:36 -07005652 }
5653}
Thomas Gleixnerdce48a82009-04-11 10:43:41 +02005654
5655/*
5656 * remove the tasks which were accounted by rq from calc_load_tasks.
5657 */
5658static void calc_global_load_remove(struct rq *rq)
5659{
5660 atomic_long_sub(rq->calc_load_active, &calc_load_tasks);
Thomas Gleixnera468d382009-07-17 14:15:46 +02005661 rq->calc_load_active = 0;
Thomas Gleixnerdce48a82009-04-11 10:43:41 +02005662}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005663#endif /* CONFIG_HOTPLUG_CPU */
5664
Nick Piggine692ab52007-07-26 13:40:43 +02005665#if defined(CONFIG_SCHED_DEBUG) && defined(CONFIG_SYSCTL)
5666
5667static struct ctl_table sd_ctl_dir[] = {
Alexey Dobriyane0361852007-08-09 11:16:46 +02005668 {
5669 .procname = "sched_domain",
Eric W. Biedermanc57baf12007-08-23 15:18:02 +02005670 .mode = 0555,
Alexey Dobriyane0361852007-08-09 11:16:46 +02005671 },
Eric W. Biederman56992302009-11-05 15:38:40 -08005672 {}
Nick Piggine692ab52007-07-26 13:40:43 +02005673};
5674
5675static struct ctl_table sd_ctl_root[] = {
Alexey Dobriyane0361852007-08-09 11:16:46 +02005676 {
5677 .procname = "kernel",
Eric W. Biedermanc57baf12007-08-23 15:18:02 +02005678 .mode = 0555,
Alexey Dobriyane0361852007-08-09 11:16:46 +02005679 .child = sd_ctl_dir,
5680 },
Eric W. Biederman56992302009-11-05 15:38:40 -08005681 {}
Nick Piggine692ab52007-07-26 13:40:43 +02005682};
5683
5684static struct ctl_table *sd_alloc_ctl_entry(int n)
5685{
5686 struct ctl_table *entry =
Milton Miller5cf9f062007-10-15 17:00:19 +02005687 kcalloc(n, sizeof(struct ctl_table), GFP_KERNEL);
Nick Piggine692ab52007-07-26 13:40:43 +02005688
Nick Piggine692ab52007-07-26 13:40:43 +02005689 return entry;
5690}
5691
Milton Miller6382bc92007-10-15 17:00:19 +02005692static void sd_free_ctl_entry(struct ctl_table **tablep)
5693{
Milton Millercd790072007-10-17 16:55:11 +02005694 struct ctl_table *entry;
Milton Miller6382bc92007-10-15 17:00:19 +02005695
Milton Millercd790072007-10-17 16:55:11 +02005696 /*
5697 * In the intermediate directories, both the child directory and
5698 * procname are dynamically allocated and could fail but the mode
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01005699 * will always be set. In the lowest directory the names are
Milton Millercd790072007-10-17 16:55:11 +02005700 * static strings and all have proc handlers.
5701 */
5702 for (entry = *tablep; entry->mode; entry++) {
Milton Miller6382bc92007-10-15 17:00:19 +02005703 if (entry->child)
5704 sd_free_ctl_entry(&entry->child);
Milton Millercd790072007-10-17 16:55:11 +02005705 if (entry->proc_handler == NULL)
5706 kfree(entry->procname);
5707 }
Milton Miller6382bc92007-10-15 17:00:19 +02005708
5709 kfree(*tablep);
5710 *tablep = NULL;
5711}
5712
Nick Piggine692ab52007-07-26 13:40:43 +02005713static void
Alexey Dobriyane0361852007-08-09 11:16:46 +02005714set_table_entry(struct ctl_table *entry,
Nick Piggine692ab52007-07-26 13:40:43 +02005715 const char *procname, void *data, int maxlen,
5716 mode_t mode, proc_handler *proc_handler)
5717{
Nick Piggine692ab52007-07-26 13:40:43 +02005718 entry->procname = procname;
5719 entry->data = data;
5720 entry->maxlen = maxlen;
5721 entry->mode = mode;
5722 entry->proc_handler = proc_handler;
5723}
5724
5725static struct ctl_table *
5726sd_alloc_ctl_domain_table(struct sched_domain *sd)
5727{
Ingo Molnara5d8c342008-10-09 11:35:51 +02005728 struct ctl_table *table = sd_alloc_ctl_entry(13);
Nick Piggine692ab52007-07-26 13:40:43 +02005729
Milton Millerad1cdc12007-10-15 17:00:19 +02005730 if (table == NULL)
5731 return NULL;
5732
Alexey Dobriyane0361852007-08-09 11:16:46 +02005733 set_table_entry(&table[0], "min_interval", &sd->min_interval,
Nick Piggine692ab52007-07-26 13:40:43 +02005734 sizeof(long), 0644, proc_doulongvec_minmax);
Alexey Dobriyane0361852007-08-09 11:16:46 +02005735 set_table_entry(&table[1], "max_interval", &sd->max_interval,
Nick Piggine692ab52007-07-26 13:40:43 +02005736 sizeof(long), 0644, proc_doulongvec_minmax);
Alexey Dobriyane0361852007-08-09 11:16:46 +02005737 set_table_entry(&table[2], "busy_idx", &sd->busy_idx,
Nick Piggine692ab52007-07-26 13:40:43 +02005738 sizeof(int), 0644, proc_dointvec_minmax);
Alexey Dobriyane0361852007-08-09 11:16:46 +02005739 set_table_entry(&table[3], "idle_idx", &sd->idle_idx,
Nick Piggine692ab52007-07-26 13:40:43 +02005740 sizeof(int), 0644, proc_dointvec_minmax);
Alexey Dobriyane0361852007-08-09 11:16:46 +02005741 set_table_entry(&table[4], "newidle_idx", &sd->newidle_idx,
Nick Piggine692ab52007-07-26 13:40:43 +02005742 sizeof(int), 0644, proc_dointvec_minmax);
Alexey Dobriyane0361852007-08-09 11:16:46 +02005743 set_table_entry(&table[5], "wake_idx", &sd->wake_idx,
Nick Piggine692ab52007-07-26 13:40:43 +02005744 sizeof(int), 0644, proc_dointvec_minmax);
Alexey Dobriyane0361852007-08-09 11:16:46 +02005745 set_table_entry(&table[6], "forkexec_idx", &sd->forkexec_idx,
Nick Piggine692ab52007-07-26 13:40:43 +02005746 sizeof(int), 0644, proc_dointvec_minmax);
Alexey Dobriyane0361852007-08-09 11:16:46 +02005747 set_table_entry(&table[7], "busy_factor", &sd->busy_factor,
Nick Piggine692ab52007-07-26 13:40:43 +02005748 sizeof(int), 0644, proc_dointvec_minmax);
Alexey Dobriyane0361852007-08-09 11:16:46 +02005749 set_table_entry(&table[8], "imbalance_pct", &sd->imbalance_pct,
Nick Piggine692ab52007-07-26 13:40:43 +02005750 sizeof(int), 0644, proc_dointvec_minmax);
Zou Nan haiace8b3d2007-10-15 17:00:14 +02005751 set_table_entry(&table[9], "cache_nice_tries",
Nick Piggine692ab52007-07-26 13:40:43 +02005752 &sd->cache_nice_tries,
5753 sizeof(int), 0644, proc_dointvec_minmax);
Zou Nan haiace8b3d2007-10-15 17:00:14 +02005754 set_table_entry(&table[10], "flags", &sd->flags,
Nick Piggine692ab52007-07-26 13:40:43 +02005755 sizeof(int), 0644, proc_dointvec_minmax);
Ingo Molnara5d8c342008-10-09 11:35:51 +02005756 set_table_entry(&table[11], "name", sd->name,
5757 CORENAME_MAX_SIZE, 0444, proc_dostring);
5758 /* &table[12] is terminator */
Nick Piggine692ab52007-07-26 13:40:43 +02005759
5760 return table;
5761}
5762
Ingo Molnar9a4e7152007-11-28 15:52:56 +01005763static ctl_table *sd_alloc_ctl_cpu_table(int cpu)
Nick Piggine692ab52007-07-26 13:40:43 +02005764{
5765 struct ctl_table *entry, *table;
5766 struct sched_domain *sd;
5767 int domain_num = 0, i;
5768 char buf[32];
5769
5770 for_each_domain(cpu, sd)
5771 domain_num++;
5772 entry = table = sd_alloc_ctl_entry(domain_num + 1);
Milton Millerad1cdc12007-10-15 17:00:19 +02005773 if (table == NULL)
5774 return NULL;
Nick Piggine692ab52007-07-26 13:40:43 +02005775
5776 i = 0;
5777 for_each_domain(cpu, sd) {
5778 snprintf(buf, 32, "domain%d", i);
Nick Piggine692ab52007-07-26 13:40:43 +02005779 entry->procname = kstrdup(buf, GFP_KERNEL);
Eric W. Biedermanc57baf12007-08-23 15:18:02 +02005780 entry->mode = 0555;
Nick Piggine692ab52007-07-26 13:40:43 +02005781 entry->child = sd_alloc_ctl_domain_table(sd);
5782 entry++;
5783 i++;
5784 }
5785 return table;
5786}
5787
5788static struct ctl_table_header *sd_sysctl_header;
Milton Miller6382bc92007-10-15 17:00:19 +02005789static void register_sched_domain_sysctl(void)
Nick Piggine692ab52007-07-26 13:40:43 +02005790{
Peter Zijlstra6ad4c182009-11-25 13:31:39 +01005791 int i, cpu_num = num_possible_cpus();
Nick Piggine692ab52007-07-26 13:40:43 +02005792 struct ctl_table *entry = sd_alloc_ctl_entry(cpu_num + 1);
5793 char buf[32];
5794
Milton Miller73785472007-10-24 18:23:48 +02005795 WARN_ON(sd_ctl_dir[0].child);
5796 sd_ctl_dir[0].child = entry;
5797
Milton Millerad1cdc12007-10-15 17:00:19 +02005798 if (entry == NULL)
5799 return;
5800
Peter Zijlstra6ad4c182009-11-25 13:31:39 +01005801 for_each_possible_cpu(i) {
Nick Piggine692ab52007-07-26 13:40:43 +02005802 snprintf(buf, 32, "cpu%d", i);
Nick Piggine692ab52007-07-26 13:40:43 +02005803 entry->procname = kstrdup(buf, GFP_KERNEL);
Eric W. Biedermanc57baf12007-08-23 15:18:02 +02005804 entry->mode = 0555;
Nick Piggine692ab52007-07-26 13:40:43 +02005805 entry->child = sd_alloc_ctl_cpu_table(i);
Milton Miller97b6ea72007-10-15 17:00:19 +02005806 entry++;
Nick Piggine692ab52007-07-26 13:40:43 +02005807 }
Milton Miller73785472007-10-24 18:23:48 +02005808
5809 WARN_ON(sd_sysctl_header);
Nick Piggine692ab52007-07-26 13:40:43 +02005810 sd_sysctl_header = register_sysctl_table(sd_ctl_root);
5811}
Milton Miller6382bc92007-10-15 17:00:19 +02005812
Milton Miller73785472007-10-24 18:23:48 +02005813/* may be called multiple times per register */
Milton Miller6382bc92007-10-15 17:00:19 +02005814static void unregister_sched_domain_sysctl(void)
5815{
Milton Miller73785472007-10-24 18:23:48 +02005816 if (sd_sysctl_header)
5817 unregister_sysctl_table(sd_sysctl_header);
Milton Miller6382bc92007-10-15 17:00:19 +02005818 sd_sysctl_header = NULL;
Milton Miller73785472007-10-24 18:23:48 +02005819 if (sd_ctl_dir[0].child)
5820 sd_free_ctl_entry(&sd_ctl_dir[0].child);
Milton Miller6382bc92007-10-15 17:00:19 +02005821}
Nick Piggine692ab52007-07-26 13:40:43 +02005822#else
Milton Miller6382bc92007-10-15 17:00:19 +02005823static void register_sched_domain_sysctl(void)
5824{
5825}
5826static void unregister_sched_domain_sysctl(void)
Nick Piggine692ab52007-07-26 13:40:43 +02005827{
5828}
5829#endif
5830
Gregory Haskins1f11eb62008-06-04 15:04:05 -04005831static void set_rq_online(struct rq *rq)
5832{
5833 if (!rq->online) {
5834 const struct sched_class *class;
5835
Rusty Russellc6c49272008-11-25 02:35:05 +10305836 cpumask_set_cpu(rq->cpu, rq->rd->online);
Gregory Haskins1f11eb62008-06-04 15:04:05 -04005837 rq->online = 1;
5838
5839 for_each_class(class) {
5840 if (class->rq_online)
5841 class->rq_online(rq);
5842 }
5843 }
5844}
5845
5846static void set_rq_offline(struct rq *rq)
5847{
5848 if (rq->online) {
5849 const struct sched_class *class;
5850
5851 for_each_class(class) {
5852 if (class->rq_offline)
5853 class->rq_offline(rq);
5854 }
5855
Rusty Russellc6c49272008-11-25 02:35:05 +10305856 cpumask_clear_cpu(rq->cpu, rq->rd->online);
Gregory Haskins1f11eb62008-06-04 15:04:05 -04005857 rq->online = 0;
5858 }
5859}
5860
Linus Torvalds1da177e2005-04-16 15:20:36 -07005861/*
5862 * migration_call - callback that gets triggered when a CPU is added.
5863 * Here we can start up the necessary migration thread for the new CPU.
5864 */
Ingo Molnar48f24c42006-07-03 00:25:40 -07005865static int __cpuinit
5866migration_call(struct notifier_block *nfb, unsigned long action, void *hcpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005867{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005868 struct task_struct *p;
Ingo Molnar48f24c42006-07-03 00:25:40 -07005869 int cpu = (long)hcpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005870 unsigned long flags;
Ingo Molnar70b97a72006-07-03 00:25:42 -07005871 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005872
5873 switch (action) {
Gautham R Shenoy5be93612007-05-09 02:34:04 -07005874
Linus Torvalds1da177e2005-04-16 15:20:36 -07005875 case CPU_UP_PREPARE:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07005876 case CPU_UP_PREPARE_FROZEN:
Ingo Molnardd41f592007-07-09 18:51:59 +02005877 p = kthread_create(migration_thread, hcpu, "migration/%d", cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005878 if (IS_ERR(p))
5879 return NOTIFY_BAD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005880 kthread_bind(p, cpu);
5881 /* Must be high prio: stop_machine expects to yield to it. */
5882 rq = task_rq_lock(p, &flags);
Ingo Molnardd41f592007-07-09 18:51:59 +02005883 __setscheduler(rq, p, SCHED_FIFO, MAX_RT_PRIO-1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005884 task_rq_unlock(rq, &flags);
Oleg Nesterov371cbb32009-06-17 16:27:45 -07005885 get_task_struct(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005886 cpu_rq(cpu)->migration_thread = p;
Thomas Gleixnera468d382009-07-17 14:15:46 +02005887 rq->calc_load_update = calc_load_update;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005888 break;
Ingo Molnar48f24c42006-07-03 00:25:40 -07005889
Linus Torvalds1da177e2005-04-16 15:20:36 -07005890 case CPU_ONLINE:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07005891 case CPU_ONLINE_FROZEN:
Robert P. J. Day3a4fa0a2007-10-19 23:10:43 +02005892 /* Strictly unnecessary, as first user will wake it. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005893 wake_up_process(cpu_rq(cpu)->migration_thread);
Gregory Haskins1f94ef52008-03-10 16:52:41 -04005894
5895 /* Update our root-domain */
5896 rq = cpu_rq(cpu);
Thomas Gleixner05fa7852009-11-17 14:28:38 +01005897 raw_spin_lock_irqsave(&rq->lock, flags);
Gregory Haskins1f94ef52008-03-10 16:52:41 -04005898 if (rq->rd) {
Rusty Russellc6c49272008-11-25 02:35:05 +10305899 BUG_ON(!cpumask_test_cpu(cpu, rq->rd->span));
Gregory Haskins1f11eb62008-06-04 15:04:05 -04005900
5901 set_rq_online(rq);
Gregory Haskins1f94ef52008-03-10 16:52:41 -04005902 }
Thomas Gleixner05fa7852009-11-17 14:28:38 +01005903 raw_spin_unlock_irqrestore(&rq->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005904 break;
Ingo Molnar48f24c42006-07-03 00:25:40 -07005905
Linus Torvalds1da177e2005-04-16 15:20:36 -07005906#ifdef CONFIG_HOTPLUG_CPU
5907 case CPU_UP_CANCELED:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07005908 case CPU_UP_CANCELED_FROZEN:
Heiko Carstensfc75cdf2006-06-25 05:49:10 -07005909 if (!cpu_rq(cpu)->migration_thread)
5910 break;
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01005911 /* Unbind it from offline cpu so it can run. Fall thru. */
Heiko Carstensa4c4af72005-11-07 00:58:38 -08005912 kthread_bind(cpu_rq(cpu)->migration_thread,
Rusty Russell1e5ce4f2008-11-25 02:35:03 +10305913 cpumask_any(cpu_online_mask));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005914 kthread_stop(cpu_rq(cpu)->migration_thread);
Oleg Nesterov371cbb32009-06-17 16:27:45 -07005915 put_task_struct(cpu_rq(cpu)->migration_thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005916 cpu_rq(cpu)->migration_thread = NULL;
5917 break;
Ingo Molnar48f24c42006-07-03 00:25:40 -07005918
Linus Torvalds1da177e2005-04-16 15:20:36 -07005919 case CPU_DEAD:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07005920 case CPU_DEAD_FROZEN:
Cliff Wickman470fd642007-10-18 23:40:46 -07005921 cpuset_lock(); /* around calls to cpuset_cpus_allowed_lock() */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005922 migrate_live_tasks(cpu);
5923 rq = cpu_rq(cpu);
5924 kthread_stop(rq->migration_thread);
Oleg Nesterov371cbb32009-06-17 16:27:45 -07005925 put_task_struct(rq->migration_thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005926 rq->migration_thread = NULL;
5927 /* Idle task back to normal (off runqueue, low prio) */
Thomas Gleixner05fa7852009-11-17 14:28:38 +01005928 raw_spin_lock_irq(&rq->lock);
Ingo Molnara8e504d2007-08-09 11:16:47 +02005929 update_rq_clock(rq);
Ingo Molnar2e1cb742007-08-09 11:16:49 +02005930 deactivate_task(rq, rq->idle, 0);
Ingo Molnardd41f592007-07-09 18:51:59 +02005931 __setscheduler(rq, rq->idle, SCHED_NORMAL, 0);
5932 rq->idle->sched_class = &idle_sched_class;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005933 migrate_dead_tasks(cpu);
Thomas Gleixner05fa7852009-11-17 14:28:38 +01005934 raw_spin_unlock_irq(&rq->lock);
Cliff Wickman470fd642007-10-18 23:40:46 -07005935 cpuset_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005936 migrate_nr_uninterruptible(rq);
5937 BUG_ON(rq->nr_running != 0);
Thomas Gleixnerdce48a82009-04-11 10:43:41 +02005938 calc_global_load_remove(rq);
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01005939 /*
5940 * No need to migrate the tasks: it was best-effort if
5941 * they didn't take sched_hotcpu_mutex. Just wake up
5942 * the requestors.
5943 */
Thomas Gleixner05fa7852009-11-17 14:28:38 +01005944 raw_spin_lock_irq(&rq->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005945 while (!list_empty(&rq->migration_queue)) {
Ingo Molnar70b97a72006-07-03 00:25:42 -07005946 struct migration_req *req;
5947
Linus Torvalds1da177e2005-04-16 15:20:36 -07005948 req = list_entry(rq->migration_queue.next,
Ingo Molnar70b97a72006-07-03 00:25:42 -07005949 struct migration_req, list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005950 list_del_init(&req->list);
Thomas Gleixner05fa7852009-11-17 14:28:38 +01005951 raw_spin_unlock_irq(&rq->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005952 complete(&req->done);
Thomas Gleixner05fa7852009-11-17 14:28:38 +01005953 raw_spin_lock_irq(&rq->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005954 }
Thomas Gleixner05fa7852009-11-17 14:28:38 +01005955 raw_spin_unlock_irq(&rq->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005956 break;
Gregory Haskins57d885f2008-01-25 21:08:18 +01005957
Gregory Haskins08f503b2008-03-10 17:59:11 -04005958 case CPU_DYING:
5959 case CPU_DYING_FROZEN:
Gregory Haskins57d885f2008-01-25 21:08:18 +01005960 /* Update our root-domain */
5961 rq = cpu_rq(cpu);
Thomas Gleixner05fa7852009-11-17 14:28:38 +01005962 raw_spin_lock_irqsave(&rq->lock, flags);
Gregory Haskins57d885f2008-01-25 21:08:18 +01005963 if (rq->rd) {
Rusty Russellc6c49272008-11-25 02:35:05 +10305964 BUG_ON(!cpumask_test_cpu(cpu, rq->rd->span));
Gregory Haskins1f11eb62008-06-04 15:04:05 -04005965 set_rq_offline(rq);
Gregory Haskins57d885f2008-01-25 21:08:18 +01005966 }
Thomas Gleixner05fa7852009-11-17 14:28:38 +01005967 raw_spin_unlock_irqrestore(&rq->lock, flags);
Gregory Haskins57d885f2008-01-25 21:08:18 +01005968 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005969#endif
5970 }
5971 return NOTIFY_OK;
5972}
5973
Paul Mackerrasf38b0822009-06-02 21:05:16 +10005974/*
5975 * Register at high priority so that task migration (migrate_all_tasks)
5976 * happens before everything else. This has to be lower priority than
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005977 * the notifier in the perf_event subsystem, though.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005978 */
Chandra Seetharaman26c21432006-06-27 02:54:10 -07005979static struct notifier_block __cpuinitdata migration_notifier = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005980 .notifier_call = migration_call,
5981 .priority = 10
5982};
5983
Eduard - Gabriel Munteanu7babe8d2008-07-25 19:45:11 -07005984static int __init migration_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005985{
5986 void *cpu = (void *)(long)smp_processor_id();
Akinobu Mita07dccf32006-09-29 02:00:22 -07005987 int err;
Ingo Molnar48f24c42006-07-03 00:25:40 -07005988
5989 /* Start one for the boot CPU: */
Akinobu Mita07dccf32006-09-29 02:00:22 -07005990 err = migration_call(&migration_notifier, CPU_UP_PREPARE, cpu);
5991 BUG_ON(err == NOTIFY_BAD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005992 migration_call(&migration_notifier, CPU_ONLINE, cpu);
5993 register_cpu_notifier(&migration_notifier);
Eduard - Gabriel Munteanu7babe8d2008-07-25 19:45:11 -07005994
Thomas Gleixnera004cd42009-07-21 09:54:05 +02005995 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005996}
Eduard - Gabriel Munteanu7babe8d2008-07-25 19:45:11 -07005997early_initcall(migration_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005998#endif
5999
6000#ifdef CONFIG_SMP
Christoph Lameter476f3532007-05-06 14:48:58 -07006001
Ingo Molnar3e9830d2007-10-15 17:00:13 +02006002#ifdef CONFIG_SCHED_DEBUG
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02006003
Mike Travisf6630112009-11-17 18:22:15 -06006004static __read_mostly int sched_domain_debug_enabled;
6005
6006static int __init sched_domain_debug_setup(char *str)
6007{
6008 sched_domain_debug_enabled = 1;
6009
6010 return 0;
6011}
6012early_param("sched_debug", sched_domain_debug_setup);
6013
Mike Travis7c16ec52008-04-04 18:11:11 -07006014static int sched_domain_debug_one(struct sched_domain *sd, int cpu, int level,
Rusty Russell96f874e2008-11-25 02:35:14 +10306015 struct cpumask *groupmask)
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02006016{
6017 struct sched_group *group = sd->groups;
Mike Travis434d53b2008-04-04 18:11:04 -07006018 char str[256];
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02006019
Rusty Russell968ea6d2008-12-13 21:55:51 +10306020 cpulist_scnprintf(str, sizeof(str), sched_domain_span(sd));
Rusty Russell96f874e2008-11-25 02:35:14 +10306021 cpumask_clear(groupmask);
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02006022
6023 printk(KERN_DEBUG "%*s domain %d: ", level, "", level);
6024
6025 if (!(sd->flags & SD_LOAD_BALANCE)) {
Peter Zijlstra3df0fc52009-12-20 14:23:57 +01006026 printk("does not load-balance\n");
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02006027 if (sd->parent)
Peter Zijlstra3df0fc52009-12-20 14:23:57 +01006028 printk(KERN_ERR "ERROR: !SD_LOAD_BALANCE domain"
6029 " has parent");
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02006030 return -1;
6031 }
6032
Peter Zijlstra3df0fc52009-12-20 14:23:57 +01006033 printk(KERN_CONT "span %s level %s\n", str, sd->name);
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02006034
Rusty Russell758b2cd2008-11-25 02:35:04 +10306035 if (!cpumask_test_cpu(cpu, sched_domain_span(sd))) {
Peter Zijlstra3df0fc52009-12-20 14:23:57 +01006036 printk(KERN_ERR "ERROR: domain->span does not contain "
6037 "CPU%d\n", cpu);
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02006038 }
Rusty Russell758b2cd2008-11-25 02:35:04 +10306039 if (!cpumask_test_cpu(cpu, sched_group_cpus(group))) {
Peter Zijlstra3df0fc52009-12-20 14:23:57 +01006040 printk(KERN_ERR "ERROR: domain->groups does not contain"
6041 " CPU%d\n", cpu);
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02006042 }
6043
6044 printk(KERN_DEBUG "%*s groups:", level + 1, "");
6045 do {
6046 if (!group) {
Peter Zijlstra3df0fc52009-12-20 14:23:57 +01006047 printk("\n");
6048 printk(KERN_ERR "ERROR: group is NULL\n");
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02006049 break;
6050 }
6051
Peter Zijlstra18a38852009-09-01 10:34:39 +02006052 if (!group->cpu_power) {
Peter Zijlstra3df0fc52009-12-20 14:23:57 +01006053 printk(KERN_CONT "\n");
6054 printk(KERN_ERR "ERROR: domain->cpu_power not "
6055 "set\n");
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02006056 break;
6057 }
6058
Rusty Russell758b2cd2008-11-25 02:35:04 +10306059 if (!cpumask_weight(sched_group_cpus(group))) {
Peter Zijlstra3df0fc52009-12-20 14:23:57 +01006060 printk(KERN_CONT "\n");
6061 printk(KERN_ERR "ERROR: empty group\n");
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02006062 break;
6063 }
6064
Rusty Russell758b2cd2008-11-25 02:35:04 +10306065 if (cpumask_intersects(groupmask, sched_group_cpus(group))) {
Peter Zijlstra3df0fc52009-12-20 14:23:57 +01006066 printk(KERN_CONT "\n");
6067 printk(KERN_ERR "ERROR: repeated CPUs\n");
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02006068 break;
6069 }
6070
Rusty Russell758b2cd2008-11-25 02:35:04 +10306071 cpumask_or(groupmask, groupmask, sched_group_cpus(group));
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02006072
Rusty Russell968ea6d2008-12-13 21:55:51 +10306073 cpulist_scnprintf(str, sizeof(str), sched_group_cpus(group));
Gautham R Shenoy381512c2009-04-14 09:09:36 +05306074
Peter Zijlstra3df0fc52009-12-20 14:23:57 +01006075 printk(KERN_CONT " %s", str);
Peter Zijlstra18a38852009-09-01 10:34:39 +02006076 if (group->cpu_power != SCHED_LOAD_SCALE) {
Peter Zijlstra3df0fc52009-12-20 14:23:57 +01006077 printk(KERN_CONT " (cpu_power = %d)",
6078 group->cpu_power);
Gautham R Shenoy381512c2009-04-14 09:09:36 +05306079 }
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02006080
6081 group = group->next;
6082 } while (group != sd->groups);
Peter Zijlstra3df0fc52009-12-20 14:23:57 +01006083 printk(KERN_CONT "\n");
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02006084
Rusty Russell758b2cd2008-11-25 02:35:04 +10306085 if (!cpumask_equal(sched_domain_span(sd), groupmask))
Peter Zijlstra3df0fc52009-12-20 14:23:57 +01006086 printk(KERN_ERR "ERROR: groups don't span domain->span\n");
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02006087
Rusty Russell758b2cd2008-11-25 02:35:04 +10306088 if (sd->parent &&
6089 !cpumask_subset(groupmask, sched_domain_span(sd->parent)))
Peter Zijlstra3df0fc52009-12-20 14:23:57 +01006090 printk(KERN_ERR "ERROR: parent span is not a superset "
6091 "of domain->span\n");
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02006092 return 0;
6093}
6094
Linus Torvalds1da177e2005-04-16 15:20:36 -07006095static void sched_domain_debug(struct sched_domain *sd, int cpu)
6096{
Rusty Russelld5dd3db2008-11-25 02:35:12 +10306097 cpumask_var_t groupmask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006098 int level = 0;
6099
Mike Travisf6630112009-11-17 18:22:15 -06006100 if (!sched_domain_debug_enabled)
6101 return;
6102
Nick Piggin41c7ce92005-06-25 14:57:24 -07006103 if (!sd) {
6104 printk(KERN_DEBUG "CPU%d attaching NULL sched-domain.\n", cpu);
6105 return;
6106 }
6107
Linus Torvalds1da177e2005-04-16 15:20:36 -07006108 printk(KERN_DEBUG "CPU%d attaching sched-domain:\n", cpu);
6109
Rusty Russelld5dd3db2008-11-25 02:35:12 +10306110 if (!alloc_cpumask_var(&groupmask, GFP_KERNEL)) {
Mike Travis7c16ec52008-04-04 18:11:11 -07006111 printk(KERN_DEBUG "Cannot load-balance (out of memory)\n");
6112 return;
6113 }
6114
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02006115 for (;;) {
Mike Travis7c16ec52008-04-04 18:11:11 -07006116 if (sched_domain_debug_one(sd, cpu, level, groupmask))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006117 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006118 level++;
6119 sd = sd->parent;
Miguel Ojeda Sandonis33859f72006-12-10 02:20:38 -08006120 if (!sd)
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02006121 break;
6122 }
Rusty Russelld5dd3db2008-11-25 02:35:12 +10306123 free_cpumask_var(groupmask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006124}
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +02006125#else /* !CONFIG_SCHED_DEBUG */
Ingo Molnar48f24c42006-07-03 00:25:40 -07006126# define sched_domain_debug(sd, cpu) do { } while (0)
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +02006127#endif /* CONFIG_SCHED_DEBUG */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006128
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07006129static int sd_degenerate(struct sched_domain *sd)
Suresh Siddha245af2c2005-06-25 14:57:25 -07006130{
Rusty Russell758b2cd2008-11-25 02:35:04 +10306131 if (cpumask_weight(sched_domain_span(sd)) == 1)
Suresh Siddha245af2c2005-06-25 14:57:25 -07006132 return 1;
6133
6134 /* Following flags need at least 2 groups */
6135 if (sd->flags & (SD_LOAD_BALANCE |
6136 SD_BALANCE_NEWIDLE |
6137 SD_BALANCE_FORK |
Siddha, Suresh B89c47102006-10-03 01:14:09 -07006138 SD_BALANCE_EXEC |
6139 SD_SHARE_CPUPOWER |
6140 SD_SHARE_PKG_RESOURCES)) {
Suresh Siddha245af2c2005-06-25 14:57:25 -07006141 if (sd->groups != sd->groups->next)
6142 return 0;
6143 }
6144
6145 /* Following flags don't use groups */
Peter Zijlstrac88d5912009-09-10 13:50:02 +02006146 if (sd->flags & (SD_WAKE_AFFINE))
Suresh Siddha245af2c2005-06-25 14:57:25 -07006147 return 0;
6148
6149 return 1;
6150}
6151
Ingo Molnar48f24c42006-07-03 00:25:40 -07006152static int
6153sd_parent_degenerate(struct sched_domain *sd, struct sched_domain *parent)
Suresh Siddha245af2c2005-06-25 14:57:25 -07006154{
6155 unsigned long cflags = sd->flags, pflags = parent->flags;
6156
6157 if (sd_degenerate(parent))
6158 return 1;
6159
Rusty Russell758b2cd2008-11-25 02:35:04 +10306160 if (!cpumask_equal(sched_domain_span(sd), sched_domain_span(parent)))
Suresh Siddha245af2c2005-06-25 14:57:25 -07006161 return 0;
6162
Suresh Siddha245af2c2005-06-25 14:57:25 -07006163 /* Flags needing groups don't count if only 1 group in parent */
6164 if (parent->groups == parent->groups->next) {
6165 pflags &= ~(SD_LOAD_BALANCE |
6166 SD_BALANCE_NEWIDLE |
6167 SD_BALANCE_FORK |
Siddha, Suresh B89c47102006-10-03 01:14:09 -07006168 SD_BALANCE_EXEC |
6169 SD_SHARE_CPUPOWER |
6170 SD_SHARE_PKG_RESOURCES);
Ken Chen54364992008-12-07 18:47:37 -08006171 if (nr_node_ids == 1)
6172 pflags &= ~SD_SERIALIZE;
Suresh Siddha245af2c2005-06-25 14:57:25 -07006173 }
6174 if (~cflags & pflags)
6175 return 0;
6176
6177 return 1;
6178}
6179
Rusty Russellc6c49272008-11-25 02:35:05 +10306180static void free_rootdomain(struct root_domain *rd)
6181{
Peter Zijlstra047106a2009-11-16 10:28:09 +01006182 synchronize_sched();
6183
Rusty Russell68e74562008-11-25 02:35:13 +10306184 cpupri_cleanup(&rd->cpupri);
6185
Rusty Russellc6c49272008-11-25 02:35:05 +10306186 free_cpumask_var(rd->rto_mask);
6187 free_cpumask_var(rd->online);
6188 free_cpumask_var(rd->span);
6189 kfree(rd);
6190}
6191
Gregory Haskins57d885f2008-01-25 21:08:18 +01006192static void rq_attach_root(struct rq *rq, struct root_domain *rd)
6193{
Ingo Molnara0490fa2009-02-12 11:35:40 +01006194 struct root_domain *old_rd = NULL;
Gregory Haskins57d885f2008-01-25 21:08:18 +01006195 unsigned long flags;
Gregory Haskins57d885f2008-01-25 21:08:18 +01006196
Thomas Gleixner05fa7852009-11-17 14:28:38 +01006197 raw_spin_lock_irqsave(&rq->lock, flags);
Gregory Haskins57d885f2008-01-25 21:08:18 +01006198
6199 if (rq->rd) {
Ingo Molnara0490fa2009-02-12 11:35:40 +01006200 old_rd = rq->rd;
Gregory Haskins57d885f2008-01-25 21:08:18 +01006201
Rusty Russellc6c49272008-11-25 02:35:05 +10306202 if (cpumask_test_cpu(rq->cpu, old_rd->online))
Gregory Haskins1f11eb62008-06-04 15:04:05 -04006203 set_rq_offline(rq);
Gregory Haskins57d885f2008-01-25 21:08:18 +01006204
Rusty Russellc6c49272008-11-25 02:35:05 +10306205 cpumask_clear_cpu(rq->cpu, old_rd->span);
Gregory Haskinsdc938522008-01-25 21:08:26 +01006206
Ingo Molnara0490fa2009-02-12 11:35:40 +01006207 /*
6208 * If we dont want to free the old_rt yet then
6209 * set old_rd to NULL to skip the freeing later
6210 * in this function:
6211 */
6212 if (!atomic_dec_and_test(&old_rd->refcount))
6213 old_rd = NULL;
Gregory Haskins57d885f2008-01-25 21:08:18 +01006214 }
6215
6216 atomic_inc(&rd->refcount);
6217 rq->rd = rd;
6218
Rusty Russellc6c49272008-11-25 02:35:05 +10306219 cpumask_set_cpu(rq->cpu, rd->span);
Gregory Haskins00aec932009-07-30 10:57:23 -04006220 if (cpumask_test_cpu(rq->cpu, cpu_active_mask))
Gregory Haskins1f11eb62008-06-04 15:04:05 -04006221 set_rq_online(rq);
Gregory Haskins57d885f2008-01-25 21:08:18 +01006222
Thomas Gleixner05fa7852009-11-17 14:28:38 +01006223 raw_spin_unlock_irqrestore(&rq->lock, flags);
Ingo Molnara0490fa2009-02-12 11:35:40 +01006224
6225 if (old_rd)
6226 free_rootdomain(old_rd);
Gregory Haskins57d885f2008-01-25 21:08:18 +01006227}
6228
Li Zefanfd5e1b52009-06-15 13:34:19 +08006229static int init_rootdomain(struct root_domain *rd, bool bootmem)
Gregory Haskins57d885f2008-01-25 21:08:18 +01006230{
Pekka Enberg36b7b6d2009-06-10 23:42:36 +03006231 gfp_t gfp = GFP_KERNEL;
6232
Gregory Haskins57d885f2008-01-25 21:08:18 +01006233 memset(rd, 0, sizeof(*rd));
6234
Pekka Enberg36b7b6d2009-06-10 23:42:36 +03006235 if (bootmem)
6236 gfp = GFP_NOWAIT;
Gregory Haskins6e0534f2008-05-12 21:21:01 +02006237
Pekka Enberg36b7b6d2009-06-10 23:42:36 +03006238 if (!alloc_cpumask_var(&rd->span, gfp))
Li Zefan0c910d22009-01-06 17:39:06 +08006239 goto out;
Pekka Enberg36b7b6d2009-06-10 23:42:36 +03006240 if (!alloc_cpumask_var(&rd->online, gfp))
Rusty Russellc6c49272008-11-25 02:35:05 +10306241 goto free_span;
Pekka Enberg36b7b6d2009-06-10 23:42:36 +03006242 if (!alloc_cpumask_var(&rd->rto_mask, gfp))
Rusty Russellc6c49272008-11-25 02:35:05 +10306243 goto free_online;
Gregory Haskins6e0534f2008-05-12 21:21:01 +02006244
Pekka Enberg0fb53022009-06-11 08:41:22 +03006245 if (cpupri_init(&rd->cpupri, bootmem) != 0)
Rusty Russell68e74562008-11-25 02:35:13 +10306246 goto free_rto_mask;
Rusty Russellc6c49272008-11-25 02:35:05 +10306247 return 0;
6248
Rusty Russell68e74562008-11-25 02:35:13 +10306249free_rto_mask:
6250 free_cpumask_var(rd->rto_mask);
Rusty Russellc6c49272008-11-25 02:35:05 +10306251free_online:
6252 free_cpumask_var(rd->online);
6253free_span:
6254 free_cpumask_var(rd->span);
Li Zefan0c910d22009-01-06 17:39:06 +08006255out:
Rusty Russellc6c49272008-11-25 02:35:05 +10306256 return -ENOMEM;
Gregory Haskins57d885f2008-01-25 21:08:18 +01006257}
6258
6259static void init_defrootdomain(void)
6260{
Rusty Russellc6c49272008-11-25 02:35:05 +10306261 init_rootdomain(&def_root_domain, true);
6262
Gregory Haskins57d885f2008-01-25 21:08:18 +01006263 atomic_set(&def_root_domain.refcount, 1);
6264}
6265
Gregory Haskinsdc938522008-01-25 21:08:26 +01006266static struct root_domain *alloc_rootdomain(void)
Gregory Haskins57d885f2008-01-25 21:08:18 +01006267{
6268 struct root_domain *rd;
6269
6270 rd = kmalloc(sizeof(*rd), GFP_KERNEL);
6271 if (!rd)
6272 return NULL;
6273
Rusty Russellc6c49272008-11-25 02:35:05 +10306274 if (init_rootdomain(rd, false) != 0) {
6275 kfree(rd);
6276 return NULL;
6277 }
Gregory Haskins57d885f2008-01-25 21:08:18 +01006278
6279 return rd;
6280}
6281
Linus Torvalds1da177e2005-04-16 15:20:36 -07006282/*
Ingo Molnar0eab9142008-01-25 21:08:19 +01006283 * Attach the domain 'sd' to 'cpu' as its base domain. Callers must
Linus Torvalds1da177e2005-04-16 15:20:36 -07006284 * hold the hotplug lock.
6285 */
Ingo Molnar0eab9142008-01-25 21:08:19 +01006286static void
6287cpu_attach_domain(struct sched_domain *sd, struct root_domain *rd, int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006288{
Ingo Molnar70b97a72006-07-03 00:25:42 -07006289 struct rq *rq = cpu_rq(cpu);
Suresh Siddha245af2c2005-06-25 14:57:25 -07006290 struct sched_domain *tmp;
6291
6292 /* Remove the sched domains which do not contribute to scheduling. */
Li Zefanf29c9b12008-11-06 09:45:16 +08006293 for (tmp = sd; tmp; ) {
Suresh Siddha245af2c2005-06-25 14:57:25 -07006294 struct sched_domain *parent = tmp->parent;
6295 if (!parent)
6296 break;
Li Zefanf29c9b12008-11-06 09:45:16 +08006297
Siddha, Suresh B1a848872006-10-03 01:14:08 -07006298 if (sd_parent_degenerate(tmp, parent)) {
Suresh Siddha245af2c2005-06-25 14:57:25 -07006299 tmp->parent = parent->parent;
Siddha, Suresh B1a848872006-10-03 01:14:08 -07006300 if (parent->parent)
6301 parent->parent->child = tmp;
Li Zefanf29c9b12008-11-06 09:45:16 +08006302 } else
6303 tmp = tmp->parent;
Suresh Siddha245af2c2005-06-25 14:57:25 -07006304 }
6305
Siddha, Suresh B1a848872006-10-03 01:14:08 -07006306 if (sd && sd_degenerate(sd)) {
Suresh Siddha245af2c2005-06-25 14:57:25 -07006307 sd = sd->parent;
Siddha, Suresh B1a848872006-10-03 01:14:08 -07006308 if (sd)
6309 sd->child = NULL;
6310 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006311
6312 sched_domain_debug(sd, cpu);
6313
Gregory Haskins57d885f2008-01-25 21:08:18 +01006314 rq_attach_root(rq, rd);
Nick Piggin674311d2005-06-25 14:57:27 -07006315 rcu_assign_pointer(rq->sd, sd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006316}
6317
6318/* cpus with isolated domains */
Rusty Russelldcc30a32008-11-25 02:35:12 +10306319static cpumask_var_t cpu_isolated_map;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006320
6321/* Setup the mask of cpus configured for isolated domains */
6322static int __init isolated_cpu_setup(char *str)
6323{
Rusty Russellbdddd292009-12-02 14:09:16 +10306324 alloc_bootmem_cpumask_var(&cpu_isolated_map);
Rusty Russell968ea6d2008-12-13 21:55:51 +10306325 cpulist_parse(str, cpu_isolated_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006326 return 1;
6327}
6328
Ingo Molnar8927f492007-10-15 17:00:13 +02006329__setup("isolcpus=", isolated_cpu_setup);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006330
6331/*
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08006332 * init_sched_build_groups takes the cpumask we wish to span, and a pointer
6333 * to a function which identifies what group(along with sched group) a CPU
Rusty Russell96f874e2008-11-25 02:35:14 +10306334 * belongs to. The return value of group_fn must be a >= 0 and < nr_cpu_ids
6335 * (due to the fact that we keep track of groups covered with a struct cpumask).
Linus Torvalds1da177e2005-04-16 15:20:36 -07006336 *
6337 * init_sched_build_groups will build a circular linked list of the groups
6338 * covered by the given span, and will set each group's ->cpumask correctly,
6339 * and ->cpu_power to 0.
6340 */
Siddha, Suresh Ba6160582006-10-03 01:14:06 -07006341static void
Rusty Russell96f874e2008-11-25 02:35:14 +10306342init_sched_build_groups(const struct cpumask *span,
6343 const struct cpumask *cpu_map,
6344 int (*group_fn)(int cpu, const struct cpumask *cpu_map,
Mike Travis7c16ec52008-04-04 18:11:11 -07006345 struct sched_group **sg,
Rusty Russell96f874e2008-11-25 02:35:14 +10306346 struct cpumask *tmpmask),
6347 struct cpumask *covered, struct cpumask *tmpmask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006348{
6349 struct sched_group *first = NULL, *last = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006350 int i;
6351
Rusty Russell96f874e2008-11-25 02:35:14 +10306352 cpumask_clear(covered);
Mike Travis7c16ec52008-04-04 18:11:11 -07006353
Rusty Russellabcd0832008-11-25 02:35:02 +10306354 for_each_cpu(i, span) {
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08006355 struct sched_group *sg;
Mike Travis7c16ec52008-04-04 18:11:11 -07006356 int group = group_fn(i, cpu_map, &sg, tmpmask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006357 int j;
6358
Rusty Russell758b2cd2008-11-25 02:35:04 +10306359 if (cpumask_test_cpu(i, covered))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006360 continue;
6361
Rusty Russell758b2cd2008-11-25 02:35:04 +10306362 cpumask_clear(sched_group_cpus(sg));
Peter Zijlstra18a38852009-09-01 10:34:39 +02006363 sg->cpu_power = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006364
Rusty Russellabcd0832008-11-25 02:35:02 +10306365 for_each_cpu(j, span) {
Mike Travis7c16ec52008-04-04 18:11:11 -07006366 if (group_fn(j, cpu_map, NULL, tmpmask) != group)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006367 continue;
6368
Rusty Russell96f874e2008-11-25 02:35:14 +10306369 cpumask_set_cpu(j, covered);
Rusty Russell758b2cd2008-11-25 02:35:04 +10306370 cpumask_set_cpu(j, sched_group_cpus(sg));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006371 }
6372 if (!first)
6373 first = sg;
6374 if (last)
6375 last->next = sg;
6376 last = sg;
6377 }
6378 last->next = first;
6379}
6380
John Hawkes9c1cfda2005-09-06 15:18:14 -07006381#define SD_NODES_PER_DOMAIN 16
Linus Torvalds1da177e2005-04-16 15:20:36 -07006382
John Hawkes9c1cfda2005-09-06 15:18:14 -07006383#ifdef CONFIG_NUMA
akpm@osdl.org198e2f12006-01-12 01:05:30 -08006384
John Hawkes9c1cfda2005-09-06 15:18:14 -07006385/**
6386 * find_next_best_node - find the next node to include in a sched_domain
6387 * @node: node whose sched_domain we're building
6388 * @used_nodes: nodes already in the sched_domain
6389 *
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01006390 * Find the next node to include in a given scheduling domain. Simply
John Hawkes9c1cfda2005-09-06 15:18:14 -07006391 * finds the closest node not already in the @used_nodes map.
6392 *
6393 * Should use nodemask_t.
6394 */
Mike Travisc5f59f02008-04-04 18:11:10 -07006395static int find_next_best_node(int node, nodemask_t *used_nodes)
John Hawkes9c1cfda2005-09-06 15:18:14 -07006396{
6397 int i, n, val, min_val, best_node = 0;
6398
6399 min_val = INT_MAX;
6400
Mike Travis076ac2a2008-05-12 21:21:12 +02006401 for (i = 0; i < nr_node_ids; i++) {
John Hawkes9c1cfda2005-09-06 15:18:14 -07006402 /* Start at @node */
Mike Travis076ac2a2008-05-12 21:21:12 +02006403 n = (node + i) % nr_node_ids;
John Hawkes9c1cfda2005-09-06 15:18:14 -07006404
6405 if (!nr_cpus_node(n))
6406 continue;
6407
6408 /* Skip already used nodes */
Mike Travisc5f59f02008-04-04 18:11:10 -07006409 if (node_isset(n, *used_nodes))
John Hawkes9c1cfda2005-09-06 15:18:14 -07006410 continue;
6411
6412 /* Simple min distance search */
6413 val = node_distance(node, n);
6414
6415 if (val < min_val) {
6416 min_val = val;
6417 best_node = n;
6418 }
6419 }
6420
Mike Travisc5f59f02008-04-04 18:11:10 -07006421 node_set(best_node, *used_nodes);
John Hawkes9c1cfda2005-09-06 15:18:14 -07006422 return best_node;
6423}
6424
6425/**
6426 * sched_domain_node_span - get a cpumask for a node's sched_domain
6427 * @node: node whose cpumask we're constructing
Randy Dunlap73486722008-04-22 10:07:22 -07006428 * @span: resulting cpumask
John Hawkes9c1cfda2005-09-06 15:18:14 -07006429 *
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01006430 * Given a node, construct a good cpumask for its sched_domain to span. It
John Hawkes9c1cfda2005-09-06 15:18:14 -07006431 * should be one that prevents unnecessary balancing, but also spreads tasks
6432 * out optimally.
6433 */
Rusty Russell96f874e2008-11-25 02:35:14 +10306434static void sched_domain_node_span(int node, struct cpumask *span)
John Hawkes9c1cfda2005-09-06 15:18:14 -07006435{
Mike Travisc5f59f02008-04-04 18:11:10 -07006436 nodemask_t used_nodes;
Ingo Molnar48f24c42006-07-03 00:25:40 -07006437 int i;
John Hawkes9c1cfda2005-09-06 15:18:14 -07006438
Mike Travis6ca09df2008-12-31 18:08:45 -08006439 cpumask_clear(span);
Mike Travisc5f59f02008-04-04 18:11:10 -07006440 nodes_clear(used_nodes);
John Hawkes9c1cfda2005-09-06 15:18:14 -07006441
Mike Travis6ca09df2008-12-31 18:08:45 -08006442 cpumask_or(span, span, cpumask_of_node(node));
Mike Travisc5f59f02008-04-04 18:11:10 -07006443 node_set(node, used_nodes);
John Hawkes9c1cfda2005-09-06 15:18:14 -07006444
6445 for (i = 1; i < SD_NODES_PER_DOMAIN; i++) {
Mike Travisc5f59f02008-04-04 18:11:10 -07006446 int next_node = find_next_best_node(node, &used_nodes);
Ingo Molnar48f24c42006-07-03 00:25:40 -07006447
Mike Travis6ca09df2008-12-31 18:08:45 -08006448 cpumask_or(span, span, cpumask_of_node(next_node));
John Hawkes9c1cfda2005-09-06 15:18:14 -07006449 }
John Hawkes9c1cfda2005-09-06 15:18:14 -07006450}
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +02006451#endif /* CONFIG_NUMA */
John Hawkes9c1cfda2005-09-06 15:18:14 -07006452
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07006453int sched_smt_power_savings = 0, sched_mc_power_savings = 0;
Ingo Molnar48f24c42006-07-03 00:25:40 -07006454
John Hawkes9c1cfda2005-09-06 15:18:14 -07006455/*
Rusty Russell6c99e9a2008-11-25 02:35:04 +10306456 * The cpus mask in sched_group and sched_domain hangs off the end.
Ingo Molnar4200efd2009-05-19 09:22:19 +02006457 *
6458 * ( See the the comments in include/linux/sched.h:struct sched_group
6459 * and struct sched_domain. )
Rusty Russell6c99e9a2008-11-25 02:35:04 +10306460 */
6461struct static_sched_group {
6462 struct sched_group sg;
6463 DECLARE_BITMAP(cpus, CONFIG_NR_CPUS);
6464};
6465
6466struct static_sched_domain {
6467 struct sched_domain sd;
6468 DECLARE_BITMAP(span, CONFIG_NR_CPUS);
6469};
6470
Andreas Herrmann49a02c52009-08-18 12:51:52 +02006471struct s_data {
6472#ifdef CONFIG_NUMA
6473 int sd_allnodes;
6474 cpumask_var_t domainspan;
6475 cpumask_var_t covered;
6476 cpumask_var_t notcovered;
6477#endif
6478 cpumask_var_t nodemask;
6479 cpumask_var_t this_sibling_map;
6480 cpumask_var_t this_core_map;
6481 cpumask_var_t send_covered;
6482 cpumask_var_t tmpmask;
6483 struct sched_group **sched_group_nodes;
6484 struct root_domain *rd;
6485};
6486
Andreas Herrmann2109b992009-08-18 12:53:00 +02006487enum s_alloc {
6488 sa_sched_groups = 0,
6489 sa_rootdomain,
6490 sa_tmpmask,
6491 sa_send_covered,
6492 sa_this_core_map,
6493 sa_this_sibling_map,
6494 sa_nodemask,
6495 sa_sched_group_nodes,
6496#ifdef CONFIG_NUMA
6497 sa_notcovered,
6498 sa_covered,
6499 sa_domainspan,
6500#endif
6501 sa_none,
6502};
6503
Rusty Russell6c99e9a2008-11-25 02:35:04 +10306504/*
Ingo Molnar48f24c42006-07-03 00:25:40 -07006505 * SMT sched-domains:
John Hawkes9c1cfda2005-09-06 15:18:14 -07006506 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006507#ifdef CONFIG_SCHED_SMT
Rusty Russell6c99e9a2008-11-25 02:35:04 +10306508static DEFINE_PER_CPU(struct static_sched_domain, cpu_domains);
Tejun Heo1871e522009-10-29 22:34:13 +09006509static DEFINE_PER_CPU(struct static_sched_group, sched_groups);
Ingo Molnar48f24c42006-07-03 00:25:40 -07006510
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01006511static int
Rusty Russell96f874e2008-11-25 02:35:14 +10306512cpu_to_cpu_group(int cpu, const struct cpumask *cpu_map,
6513 struct sched_group **sg, struct cpumask *unused)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006514{
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08006515 if (sg)
Tejun Heo1871e522009-10-29 22:34:13 +09006516 *sg = &per_cpu(sched_groups, cpu).sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006517 return cpu;
6518}
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +02006519#endif /* CONFIG_SCHED_SMT */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006520
Ingo Molnar48f24c42006-07-03 00:25:40 -07006521/*
6522 * multi-core sched-domains:
6523 */
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08006524#ifdef CONFIG_SCHED_MC
Rusty Russell6c99e9a2008-11-25 02:35:04 +10306525static DEFINE_PER_CPU(struct static_sched_domain, core_domains);
6526static DEFINE_PER_CPU(struct static_sched_group, sched_group_core);
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +02006527#endif /* CONFIG_SCHED_MC */
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08006528
6529#if defined(CONFIG_SCHED_MC) && defined(CONFIG_SCHED_SMT)
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01006530static int
Rusty Russell96f874e2008-11-25 02:35:14 +10306531cpu_to_core_group(int cpu, const struct cpumask *cpu_map,
6532 struct sched_group **sg, struct cpumask *mask)
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08006533{
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08006534 int group;
Mike Travis7c16ec52008-04-04 18:11:11 -07006535
Rusty Russellc69fc562009-03-13 14:49:46 +10306536 cpumask_and(mask, topology_thread_cpumask(cpu), cpu_map);
Rusty Russell96f874e2008-11-25 02:35:14 +10306537 group = cpumask_first(mask);
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08006538 if (sg)
Rusty Russell6c99e9a2008-11-25 02:35:04 +10306539 *sg = &per_cpu(sched_group_core, group).sg;
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08006540 return group;
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08006541}
6542#elif defined(CONFIG_SCHED_MC)
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01006543static int
Rusty Russell96f874e2008-11-25 02:35:14 +10306544cpu_to_core_group(int cpu, const struct cpumask *cpu_map,
6545 struct sched_group **sg, struct cpumask *unused)
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08006546{
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08006547 if (sg)
Rusty Russell6c99e9a2008-11-25 02:35:04 +10306548 *sg = &per_cpu(sched_group_core, cpu).sg;
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08006549 return cpu;
6550}
6551#endif
6552
Rusty Russell6c99e9a2008-11-25 02:35:04 +10306553static DEFINE_PER_CPU(struct static_sched_domain, phys_domains);
6554static DEFINE_PER_CPU(struct static_sched_group, sched_group_phys);
Ingo Molnar48f24c42006-07-03 00:25:40 -07006555
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01006556static int
Rusty Russell96f874e2008-11-25 02:35:14 +10306557cpu_to_phys_group(int cpu, const struct cpumask *cpu_map,
6558 struct sched_group **sg, struct cpumask *mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006559{
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08006560 int group;
Ingo Molnar48f24c42006-07-03 00:25:40 -07006561#ifdef CONFIG_SCHED_MC
Mike Travis6ca09df2008-12-31 18:08:45 -08006562 cpumask_and(mask, cpu_coregroup_mask(cpu), cpu_map);
Rusty Russell96f874e2008-11-25 02:35:14 +10306563 group = cpumask_first(mask);
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08006564#elif defined(CONFIG_SCHED_SMT)
Rusty Russellc69fc562009-03-13 14:49:46 +10306565 cpumask_and(mask, topology_thread_cpumask(cpu), cpu_map);
Rusty Russell96f874e2008-11-25 02:35:14 +10306566 group = cpumask_first(mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006567#else
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08006568 group = cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006569#endif
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08006570 if (sg)
Rusty Russell6c99e9a2008-11-25 02:35:04 +10306571 *sg = &per_cpu(sched_group_phys, group).sg;
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08006572 return group;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006573}
6574
6575#ifdef CONFIG_NUMA
John Hawkes9c1cfda2005-09-06 15:18:14 -07006576/*
6577 * The init_sched_build_groups can't handle what we want to do with node
6578 * groups, so roll our own. Now each node has its own list of groups which
6579 * gets dynamically allocated.
6580 */
Rusty Russell62ea9ce2009-01-11 01:04:16 +01006581static DEFINE_PER_CPU(struct static_sched_domain, node_domains);
Mike Travis434d53b2008-04-04 18:11:04 -07006582static struct sched_group ***sched_group_nodes_bycpu;
John Hawkes9c1cfda2005-09-06 15:18:14 -07006583
Rusty Russell62ea9ce2009-01-11 01:04:16 +01006584static DEFINE_PER_CPU(struct static_sched_domain, allnodes_domains);
Rusty Russell6c99e9a2008-11-25 02:35:04 +10306585static DEFINE_PER_CPU(struct static_sched_group, sched_group_allnodes);
John Hawkes9c1cfda2005-09-06 15:18:14 -07006586
Rusty Russell96f874e2008-11-25 02:35:14 +10306587static int cpu_to_allnodes_group(int cpu, const struct cpumask *cpu_map,
6588 struct sched_group **sg,
6589 struct cpumask *nodemask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006590{
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08006591 int group;
6592
Mike Travis6ca09df2008-12-31 18:08:45 -08006593 cpumask_and(nodemask, cpumask_of_node(cpu_to_node(cpu)), cpu_map);
Rusty Russell96f874e2008-11-25 02:35:14 +10306594 group = cpumask_first(nodemask);
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08006595
6596 if (sg)
Rusty Russell6c99e9a2008-11-25 02:35:04 +10306597 *sg = &per_cpu(sched_group_allnodes, group).sg;
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08006598 return group;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006599}
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08006600
Siddha, Suresh B08069032006-03-27 01:15:23 -08006601static void init_numa_sched_groups_power(struct sched_group *group_head)
6602{
6603 struct sched_group *sg = group_head;
6604 int j;
6605
6606 if (!sg)
6607 return;
Andi Kleen3a5c3592007-10-15 17:00:14 +02006608 do {
Rusty Russell758b2cd2008-11-25 02:35:04 +10306609 for_each_cpu(j, sched_group_cpus(sg)) {
Andi Kleen3a5c3592007-10-15 17:00:14 +02006610 struct sched_domain *sd;
Siddha, Suresh B08069032006-03-27 01:15:23 -08006611
Rusty Russell6c99e9a2008-11-25 02:35:04 +10306612 sd = &per_cpu(phys_domains, j).sd;
Miao Xie13318a72009-04-15 09:59:10 +08006613 if (j != group_first_cpu(sd->groups)) {
Andi Kleen3a5c3592007-10-15 17:00:14 +02006614 /*
6615 * Only add "power" once for each
6616 * physical package.
6617 */
6618 continue;
6619 }
6620
Peter Zijlstra18a38852009-09-01 10:34:39 +02006621 sg->cpu_power += sd->groups->cpu_power;
Siddha, Suresh B08069032006-03-27 01:15:23 -08006622 }
Andi Kleen3a5c3592007-10-15 17:00:14 +02006623 sg = sg->next;
6624 } while (sg != group_head);
Siddha, Suresh B08069032006-03-27 01:15:23 -08006625}
Andreas Herrmann0601a882009-08-18 13:01:11 +02006626
6627static int build_numa_sched_groups(struct s_data *d,
6628 const struct cpumask *cpu_map, int num)
6629{
6630 struct sched_domain *sd;
6631 struct sched_group *sg, *prev;
6632 int n, j;
6633
6634 cpumask_clear(d->covered);
6635 cpumask_and(d->nodemask, cpumask_of_node(num), cpu_map);
6636 if (cpumask_empty(d->nodemask)) {
6637 d->sched_group_nodes[num] = NULL;
6638 goto out;
6639 }
6640
6641 sched_domain_node_span(num, d->domainspan);
6642 cpumask_and(d->domainspan, d->domainspan, cpu_map);
6643
6644 sg = kmalloc_node(sizeof(struct sched_group) + cpumask_size(),
6645 GFP_KERNEL, num);
6646 if (!sg) {
Peter Zijlstra3df0fc52009-12-20 14:23:57 +01006647 printk(KERN_WARNING "Can not alloc domain group for node %d\n",
6648 num);
Andreas Herrmann0601a882009-08-18 13:01:11 +02006649 return -ENOMEM;
6650 }
6651 d->sched_group_nodes[num] = sg;
6652
6653 for_each_cpu(j, d->nodemask) {
6654 sd = &per_cpu(node_domains, j).sd;
6655 sd->groups = sg;
6656 }
6657
Peter Zijlstra18a38852009-09-01 10:34:39 +02006658 sg->cpu_power = 0;
Andreas Herrmann0601a882009-08-18 13:01:11 +02006659 cpumask_copy(sched_group_cpus(sg), d->nodemask);
6660 sg->next = sg;
6661 cpumask_or(d->covered, d->covered, d->nodemask);
6662
6663 prev = sg;
6664 for (j = 0; j < nr_node_ids; j++) {
6665 n = (num + j) % nr_node_ids;
6666 cpumask_complement(d->notcovered, d->covered);
6667 cpumask_and(d->tmpmask, d->notcovered, cpu_map);
6668 cpumask_and(d->tmpmask, d->tmpmask, d->domainspan);
6669 if (cpumask_empty(d->tmpmask))
6670 break;
6671 cpumask_and(d->tmpmask, d->tmpmask, cpumask_of_node(n));
6672 if (cpumask_empty(d->tmpmask))
6673 continue;
6674 sg = kmalloc_node(sizeof(struct sched_group) + cpumask_size(),
6675 GFP_KERNEL, num);
6676 if (!sg) {
Peter Zijlstra3df0fc52009-12-20 14:23:57 +01006677 printk(KERN_WARNING
6678 "Can not alloc domain group for node %d\n", j);
Andreas Herrmann0601a882009-08-18 13:01:11 +02006679 return -ENOMEM;
6680 }
Peter Zijlstra18a38852009-09-01 10:34:39 +02006681 sg->cpu_power = 0;
Andreas Herrmann0601a882009-08-18 13:01:11 +02006682 cpumask_copy(sched_group_cpus(sg), d->tmpmask);
6683 sg->next = prev->next;
6684 cpumask_or(d->covered, d->covered, d->tmpmask);
6685 prev->next = sg;
6686 prev = sg;
6687 }
6688out:
6689 return 0;
6690}
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +02006691#endif /* CONFIG_NUMA */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006692
Siddha, Suresh Ba6160582006-10-03 01:14:06 -07006693#ifdef CONFIG_NUMA
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07006694/* Free memory allocated for various sched_group structures */
Rusty Russell96f874e2008-11-25 02:35:14 +10306695static void free_sched_groups(const struct cpumask *cpu_map,
6696 struct cpumask *nodemask)
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07006697{
Siddha, Suresh Ba6160582006-10-03 01:14:06 -07006698 int cpu, i;
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07006699
Rusty Russellabcd0832008-11-25 02:35:02 +10306700 for_each_cpu(cpu, cpu_map) {
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07006701 struct sched_group **sched_group_nodes
6702 = sched_group_nodes_bycpu[cpu];
6703
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07006704 if (!sched_group_nodes)
6705 continue;
6706
Mike Travis076ac2a2008-05-12 21:21:12 +02006707 for (i = 0; i < nr_node_ids; i++) {
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07006708 struct sched_group *oldsg, *sg = sched_group_nodes[i];
6709
Mike Travis6ca09df2008-12-31 18:08:45 -08006710 cpumask_and(nodemask, cpumask_of_node(i), cpu_map);
Rusty Russell96f874e2008-11-25 02:35:14 +10306711 if (cpumask_empty(nodemask))
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07006712 continue;
6713
6714 if (sg == NULL)
6715 continue;
6716 sg = sg->next;
6717next_sg:
6718 oldsg = sg;
6719 sg = sg->next;
6720 kfree(oldsg);
6721 if (oldsg != sched_group_nodes[i])
6722 goto next_sg;
6723 }
6724 kfree(sched_group_nodes);
6725 sched_group_nodes_bycpu[cpu] = NULL;
6726 }
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07006727}
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +02006728#else /* !CONFIG_NUMA */
Rusty Russell96f874e2008-11-25 02:35:14 +10306729static void free_sched_groups(const struct cpumask *cpu_map,
6730 struct cpumask *nodemask)
Siddha, Suresh Ba6160582006-10-03 01:14:06 -07006731{
6732}
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +02006733#endif /* CONFIG_NUMA */
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07006734
Linus Torvalds1da177e2005-04-16 15:20:36 -07006735/*
Siddha, Suresh B89c47102006-10-03 01:14:09 -07006736 * Initialize sched groups cpu_power.
6737 *
6738 * cpu_power indicates the capacity of sched group, which is used while
6739 * distributing the load between different sched groups in a sched domain.
6740 * Typically cpu_power for all the groups in a sched domain will be same unless
6741 * there are asymmetries in the topology. If there are asymmetries, group
6742 * having more cpu_power will pickup more load compared to the group having
6743 * less cpu_power.
Siddha, Suresh B89c47102006-10-03 01:14:09 -07006744 */
6745static void init_sched_groups_power(int cpu, struct sched_domain *sd)
6746{
6747 struct sched_domain *child;
6748 struct sched_group *group;
Peter Zijlstraf93e65c2009-09-01 10:34:32 +02006749 long power;
6750 int weight;
Siddha, Suresh B89c47102006-10-03 01:14:09 -07006751
6752 WARN_ON(!sd || !sd->groups);
6753
Miao Xie13318a72009-04-15 09:59:10 +08006754 if (cpu != group_first_cpu(sd->groups))
Siddha, Suresh B89c47102006-10-03 01:14:09 -07006755 return;
6756
6757 child = sd->child;
6758
Peter Zijlstra18a38852009-09-01 10:34:39 +02006759 sd->groups->cpu_power = 0;
Eric Dumazet5517d862007-05-08 00:32:57 -07006760
Peter Zijlstraf93e65c2009-09-01 10:34:32 +02006761 if (!child) {
6762 power = SCHED_LOAD_SCALE;
6763 weight = cpumask_weight(sched_domain_span(sd));
6764 /*
6765 * SMT siblings share the power of a single core.
Peter Zijlstraa52bfd72009-09-01 10:34:35 +02006766 * Usually multiple threads get a better yield out of
6767 * that one core than a single thread would have,
6768 * reflect that in sd->smt_gain.
Peter Zijlstraf93e65c2009-09-01 10:34:32 +02006769 */
Peter Zijlstraa52bfd72009-09-01 10:34:35 +02006770 if ((sd->flags & SD_SHARE_CPUPOWER) && weight > 1) {
6771 power *= sd->smt_gain;
Peter Zijlstraf93e65c2009-09-01 10:34:32 +02006772 power /= weight;
Peter Zijlstraa52bfd72009-09-01 10:34:35 +02006773 power >>= SCHED_LOAD_SHIFT;
6774 }
Peter Zijlstra18a38852009-09-01 10:34:39 +02006775 sd->groups->cpu_power += power;
Siddha, Suresh B89c47102006-10-03 01:14:09 -07006776 return;
6777 }
6778
Siddha, Suresh B89c47102006-10-03 01:14:09 -07006779 /*
Peter Zijlstraf93e65c2009-09-01 10:34:32 +02006780 * Add cpu_power of each child group to this groups cpu_power.
Siddha, Suresh B89c47102006-10-03 01:14:09 -07006781 */
6782 group = child->groups;
6783 do {
Peter Zijlstra18a38852009-09-01 10:34:39 +02006784 sd->groups->cpu_power += group->cpu_power;
Siddha, Suresh B89c47102006-10-03 01:14:09 -07006785 group = group->next;
6786 } while (group != child->groups);
6787}
6788
6789/*
Mike Travis7c16ec52008-04-04 18:11:11 -07006790 * Initializers for schedule domains
6791 * Non-inlined to reduce accumulated stack pressure in build_sched_domains()
6792 */
6793
Ingo Molnara5d8c342008-10-09 11:35:51 +02006794#ifdef CONFIG_SCHED_DEBUG
6795# define SD_INIT_NAME(sd, type) sd->name = #type
6796#else
6797# define SD_INIT_NAME(sd, type) do { } while (0)
6798#endif
6799
Mike Travis7c16ec52008-04-04 18:11:11 -07006800#define SD_INIT(sd, type) sd_init_##type(sd)
Ingo Molnara5d8c342008-10-09 11:35:51 +02006801
Mike Travis7c16ec52008-04-04 18:11:11 -07006802#define SD_INIT_FUNC(type) \
6803static noinline void sd_init_##type(struct sched_domain *sd) \
6804{ \
6805 memset(sd, 0, sizeof(*sd)); \
6806 *sd = SD_##type##_INIT; \
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09006807 sd->level = SD_LV_##type; \
Ingo Molnara5d8c342008-10-09 11:35:51 +02006808 SD_INIT_NAME(sd, type); \
Mike Travis7c16ec52008-04-04 18:11:11 -07006809}
6810
6811SD_INIT_FUNC(CPU)
6812#ifdef CONFIG_NUMA
6813 SD_INIT_FUNC(ALLNODES)
6814 SD_INIT_FUNC(NODE)
6815#endif
6816#ifdef CONFIG_SCHED_SMT
6817 SD_INIT_FUNC(SIBLING)
6818#endif
6819#ifdef CONFIG_SCHED_MC
6820 SD_INIT_FUNC(MC)
6821#endif
6822
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09006823static int default_relax_domain_level = -1;
6824
6825static int __init setup_relax_domain_level(char *str)
6826{
Li Zefan30e0e172008-05-13 10:27:17 +08006827 unsigned long val;
6828
6829 val = simple_strtoul(str, NULL, 0);
6830 if (val < SD_LV_MAX)
6831 default_relax_domain_level = val;
6832
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09006833 return 1;
6834}
6835__setup("relax_domain_level=", setup_relax_domain_level);
6836
6837static void set_domain_attribute(struct sched_domain *sd,
6838 struct sched_domain_attr *attr)
6839{
6840 int request;
6841
6842 if (!attr || attr->relax_domain_level < 0) {
6843 if (default_relax_domain_level < 0)
6844 return;
6845 else
6846 request = default_relax_domain_level;
6847 } else
6848 request = attr->relax_domain_level;
6849 if (request < sd->level) {
6850 /* turn off idle balance on this domain */
Peter Zijlstrac88d5912009-09-10 13:50:02 +02006851 sd->flags &= ~(SD_BALANCE_WAKE|SD_BALANCE_NEWIDLE);
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09006852 } else {
6853 /* turn on idle balance on this domain */
Peter Zijlstrac88d5912009-09-10 13:50:02 +02006854 sd->flags |= (SD_BALANCE_WAKE|SD_BALANCE_NEWIDLE);
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09006855 }
6856}
6857
Andreas Herrmann2109b992009-08-18 12:53:00 +02006858static void __free_domain_allocs(struct s_data *d, enum s_alloc what,
6859 const struct cpumask *cpu_map)
6860{
6861 switch (what) {
6862 case sa_sched_groups:
6863 free_sched_groups(cpu_map, d->tmpmask); /* fall through */
6864 d->sched_group_nodes = NULL;
6865 case sa_rootdomain:
6866 free_rootdomain(d->rd); /* fall through */
6867 case sa_tmpmask:
6868 free_cpumask_var(d->tmpmask); /* fall through */
6869 case sa_send_covered:
6870 free_cpumask_var(d->send_covered); /* fall through */
6871 case sa_this_core_map:
6872 free_cpumask_var(d->this_core_map); /* fall through */
6873 case sa_this_sibling_map:
6874 free_cpumask_var(d->this_sibling_map); /* fall through */
6875 case sa_nodemask:
6876 free_cpumask_var(d->nodemask); /* fall through */
6877 case sa_sched_group_nodes:
6878#ifdef CONFIG_NUMA
6879 kfree(d->sched_group_nodes); /* fall through */
6880 case sa_notcovered:
6881 free_cpumask_var(d->notcovered); /* fall through */
6882 case sa_covered:
6883 free_cpumask_var(d->covered); /* fall through */
6884 case sa_domainspan:
6885 free_cpumask_var(d->domainspan); /* fall through */
6886#endif
6887 case sa_none:
6888 break;
6889 }
6890}
6891
6892static enum s_alloc __visit_domain_allocation_hell(struct s_data *d,
6893 const struct cpumask *cpu_map)
6894{
6895#ifdef CONFIG_NUMA
6896 if (!alloc_cpumask_var(&d->domainspan, GFP_KERNEL))
6897 return sa_none;
6898 if (!alloc_cpumask_var(&d->covered, GFP_KERNEL))
6899 return sa_domainspan;
6900 if (!alloc_cpumask_var(&d->notcovered, GFP_KERNEL))
6901 return sa_covered;
6902 /* Allocate the per-node list of sched groups */
6903 d->sched_group_nodes = kcalloc(nr_node_ids,
6904 sizeof(struct sched_group *), GFP_KERNEL);
6905 if (!d->sched_group_nodes) {
Peter Zijlstra3df0fc52009-12-20 14:23:57 +01006906 printk(KERN_WARNING "Can not alloc sched group node list\n");
Andreas Herrmann2109b992009-08-18 12:53:00 +02006907 return sa_notcovered;
6908 }
6909 sched_group_nodes_bycpu[cpumask_first(cpu_map)] = d->sched_group_nodes;
6910#endif
6911 if (!alloc_cpumask_var(&d->nodemask, GFP_KERNEL))
6912 return sa_sched_group_nodes;
6913 if (!alloc_cpumask_var(&d->this_sibling_map, GFP_KERNEL))
6914 return sa_nodemask;
6915 if (!alloc_cpumask_var(&d->this_core_map, GFP_KERNEL))
6916 return sa_this_sibling_map;
6917 if (!alloc_cpumask_var(&d->send_covered, GFP_KERNEL))
6918 return sa_this_core_map;
6919 if (!alloc_cpumask_var(&d->tmpmask, GFP_KERNEL))
6920 return sa_send_covered;
6921 d->rd = alloc_rootdomain();
6922 if (!d->rd) {
Peter Zijlstra3df0fc52009-12-20 14:23:57 +01006923 printk(KERN_WARNING "Cannot alloc root domain\n");
Andreas Herrmann2109b992009-08-18 12:53:00 +02006924 return sa_tmpmask;
6925 }
6926 return sa_rootdomain;
6927}
6928
Andreas Herrmann7f4588f2009-08-18 12:54:06 +02006929static struct sched_domain *__build_numa_sched_domains(struct s_data *d,
6930 const struct cpumask *cpu_map, struct sched_domain_attr *attr, int i)
6931{
6932 struct sched_domain *sd = NULL;
6933#ifdef CONFIG_NUMA
6934 struct sched_domain *parent;
6935
6936 d->sd_allnodes = 0;
6937 if (cpumask_weight(cpu_map) >
6938 SD_NODES_PER_DOMAIN * cpumask_weight(d->nodemask)) {
6939 sd = &per_cpu(allnodes_domains, i).sd;
6940 SD_INIT(sd, ALLNODES);
6941 set_domain_attribute(sd, attr);
6942 cpumask_copy(sched_domain_span(sd), cpu_map);
6943 cpu_to_allnodes_group(i, cpu_map, &sd->groups, d->tmpmask);
6944 d->sd_allnodes = 1;
6945 }
6946 parent = sd;
6947
6948 sd = &per_cpu(node_domains, i).sd;
6949 SD_INIT(sd, NODE);
6950 set_domain_attribute(sd, attr);
6951 sched_domain_node_span(cpu_to_node(i), sched_domain_span(sd));
6952 sd->parent = parent;
6953 if (parent)
6954 parent->child = sd;
6955 cpumask_and(sched_domain_span(sd), sched_domain_span(sd), cpu_map);
6956#endif
6957 return sd;
6958}
6959
Andreas Herrmann87cce662009-08-18 12:54:55 +02006960static struct sched_domain *__build_cpu_sched_domain(struct s_data *d,
6961 const struct cpumask *cpu_map, struct sched_domain_attr *attr,
6962 struct sched_domain *parent, int i)
6963{
6964 struct sched_domain *sd;
6965 sd = &per_cpu(phys_domains, i).sd;
6966 SD_INIT(sd, CPU);
6967 set_domain_attribute(sd, attr);
6968 cpumask_copy(sched_domain_span(sd), d->nodemask);
6969 sd->parent = parent;
6970 if (parent)
6971 parent->child = sd;
6972 cpu_to_phys_group(i, cpu_map, &sd->groups, d->tmpmask);
6973 return sd;
6974}
6975
Andreas Herrmann410c4082009-08-18 12:56:14 +02006976static struct sched_domain *__build_mc_sched_domain(struct s_data *d,
6977 const struct cpumask *cpu_map, struct sched_domain_attr *attr,
6978 struct sched_domain *parent, int i)
6979{
6980 struct sched_domain *sd = parent;
6981#ifdef CONFIG_SCHED_MC
6982 sd = &per_cpu(core_domains, i).sd;
6983 SD_INIT(sd, MC);
6984 set_domain_attribute(sd, attr);
6985 cpumask_and(sched_domain_span(sd), cpu_map, cpu_coregroup_mask(i));
6986 sd->parent = parent;
6987 parent->child = sd;
6988 cpu_to_core_group(i, cpu_map, &sd->groups, d->tmpmask);
6989#endif
6990 return sd;
6991}
6992
Andreas Herrmannd8173532009-08-18 12:57:03 +02006993static struct sched_domain *__build_smt_sched_domain(struct s_data *d,
6994 const struct cpumask *cpu_map, struct sched_domain_attr *attr,
6995 struct sched_domain *parent, int i)
6996{
6997 struct sched_domain *sd = parent;
6998#ifdef CONFIG_SCHED_SMT
6999 sd = &per_cpu(cpu_domains, i).sd;
7000 SD_INIT(sd, SIBLING);
7001 set_domain_attribute(sd, attr);
7002 cpumask_and(sched_domain_span(sd), cpu_map, topology_thread_cpumask(i));
7003 sd->parent = parent;
7004 parent->child = sd;
7005 cpu_to_cpu_group(i, cpu_map, &sd->groups, d->tmpmask);
7006#endif
7007 return sd;
7008}
7009
Andreas Herrmann0e8e85c2009-08-18 12:57:51 +02007010static void build_sched_groups(struct s_data *d, enum sched_domain_level l,
7011 const struct cpumask *cpu_map, int cpu)
7012{
7013 switch (l) {
7014#ifdef CONFIG_SCHED_SMT
7015 case SD_LV_SIBLING: /* set up CPU (sibling) groups */
7016 cpumask_and(d->this_sibling_map, cpu_map,
7017 topology_thread_cpumask(cpu));
7018 if (cpu == cpumask_first(d->this_sibling_map))
7019 init_sched_build_groups(d->this_sibling_map, cpu_map,
7020 &cpu_to_cpu_group,
7021 d->send_covered, d->tmpmask);
7022 break;
7023#endif
Andreas Herrmanna2af04c2009-08-18 12:58:38 +02007024#ifdef CONFIG_SCHED_MC
7025 case SD_LV_MC: /* set up multi-core groups */
7026 cpumask_and(d->this_core_map, cpu_map, cpu_coregroup_mask(cpu));
7027 if (cpu == cpumask_first(d->this_core_map))
7028 init_sched_build_groups(d->this_core_map, cpu_map,
7029 &cpu_to_core_group,
7030 d->send_covered, d->tmpmask);
7031 break;
7032#endif
Andreas Herrmann86548092009-08-18 12:59:28 +02007033 case SD_LV_CPU: /* set up physical groups */
7034 cpumask_and(d->nodemask, cpumask_of_node(cpu), cpu_map);
7035 if (!cpumask_empty(d->nodemask))
7036 init_sched_build_groups(d->nodemask, cpu_map,
7037 &cpu_to_phys_group,
7038 d->send_covered, d->tmpmask);
7039 break;
Andreas Herrmannde616e32009-08-18 13:00:13 +02007040#ifdef CONFIG_NUMA
7041 case SD_LV_ALLNODES:
7042 init_sched_build_groups(cpu_map, cpu_map, &cpu_to_allnodes_group,
7043 d->send_covered, d->tmpmask);
7044 break;
7045#endif
Andreas Herrmann0e8e85c2009-08-18 12:57:51 +02007046 default:
7047 break;
7048 }
7049}
7050
Mike Travis7c16ec52008-04-04 18:11:11 -07007051/*
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07007052 * Build sched domains for a given set of cpus and attach the sched domains
7053 * to the individual cpus
Linus Torvalds1da177e2005-04-16 15:20:36 -07007054 */
Rusty Russell96f874e2008-11-25 02:35:14 +10307055static int __build_sched_domains(const struct cpumask *cpu_map,
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09007056 struct sched_domain_attr *attr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007057{
Andreas Herrmann2109b992009-08-18 12:53:00 +02007058 enum s_alloc alloc_state = sa_none;
Andreas Herrmann49a02c52009-08-18 12:51:52 +02007059 struct s_data d;
Andreas Herrmann294b0c92009-08-18 13:02:29 +02007060 struct sched_domain *sd;
Andreas Herrmann2109b992009-08-18 12:53:00 +02007061 int i;
John Hawkesd1b55132005-09-06 15:18:14 -07007062#ifdef CONFIG_NUMA
Andreas Herrmann49a02c52009-08-18 12:51:52 +02007063 d.sd_allnodes = 0;
Rusty Russell3404c8d2008-11-25 02:35:03 +10307064#endif
7065
Andreas Herrmann2109b992009-08-18 12:53:00 +02007066 alloc_state = __visit_domain_allocation_hell(&d, cpu_map);
7067 if (alloc_state != sa_rootdomain)
7068 goto error;
7069 alloc_state = sa_sched_groups;
Mike Travis7c16ec52008-04-04 18:11:11 -07007070
Linus Torvalds1da177e2005-04-16 15:20:36 -07007071 /*
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07007072 * Set up domains for cpus specified by the cpu_map.
Linus Torvalds1da177e2005-04-16 15:20:36 -07007073 */
Rusty Russellabcd0832008-11-25 02:35:02 +10307074 for_each_cpu(i, cpu_map) {
Andreas Herrmann49a02c52009-08-18 12:51:52 +02007075 cpumask_and(d.nodemask, cpumask_of_node(cpu_to_node(i)),
7076 cpu_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007077
Andreas Herrmann7f4588f2009-08-18 12:54:06 +02007078 sd = __build_numa_sched_domains(&d, cpu_map, attr, i);
Andreas Herrmann87cce662009-08-18 12:54:55 +02007079 sd = __build_cpu_sched_domain(&d, cpu_map, attr, sd, i);
Andreas Herrmann410c4082009-08-18 12:56:14 +02007080 sd = __build_mc_sched_domain(&d, cpu_map, attr, sd, i);
Andreas Herrmannd8173532009-08-18 12:57:03 +02007081 sd = __build_smt_sched_domain(&d, cpu_map, attr, sd, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007082 }
7083
Rusty Russellabcd0832008-11-25 02:35:02 +10307084 for_each_cpu(i, cpu_map) {
Andreas Herrmann0e8e85c2009-08-18 12:57:51 +02007085 build_sched_groups(&d, SD_LV_SIBLING, cpu_map, i);
Andreas Herrmanna2af04c2009-08-18 12:58:38 +02007086 build_sched_groups(&d, SD_LV_MC, cpu_map, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007087 }
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08007088
Linus Torvalds1da177e2005-04-16 15:20:36 -07007089 /* Set up physical groups */
Andreas Herrmann86548092009-08-18 12:59:28 +02007090 for (i = 0; i < nr_node_ids; i++)
7091 build_sched_groups(&d, SD_LV_CPU, cpu_map, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007092
7093#ifdef CONFIG_NUMA
7094 /* Set up node groups */
Andreas Herrmannde616e32009-08-18 13:00:13 +02007095 if (d.sd_allnodes)
7096 build_sched_groups(&d, SD_LV_ALLNODES, cpu_map, 0);
John Hawkes9c1cfda2005-09-06 15:18:14 -07007097
Andreas Herrmann0601a882009-08-18 13:01:11 +02007098 for (i = 0; i < nr_node_ids; i++)
7099 if (build_numa_sched_groups(&d, cpu_map, i))
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07007100 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007101#endif
7102
7103 /* Calculate CPU power for physical packages and nodes */
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07007104#ifdef CONFIG_SCHED_SMT
Rusty Russellabcd0832008-11-25 02:35:02 +10307105 for_each_cpu(i, cpu_map) {
Andreas Herrmann294b0c92009-08-18 13:02:29 +02007106 sd = &per_cpu(cpu_domains, i).sd;
Siddha, Suresh B89c47102006-10-03 01:14:09 -07007107 init_sched_groups_power(i, sd);
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07007108 }
7109#endif
7110#ifdef CONFIG_SCHED_MC
Rusty Russellabcd0832008-11-25 02:35:02 +10307111 for_each_cpu(i, cpu_map) {
Andreas Herrmann294b0c92009-08-18 13:02:29 +02007112 sd = &per_cpu(core_domains, i).sd;
Siddha, Suresh B89c47102006-10-03 01:14:09 -07007113 init_sched_groups_power(i, sd);
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07007114 }
7115#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07007116
Rusty Russellabcd0832008-11-25 02:35:02 +10307117 for_each_cpu(i, cpu_map) {
Andreas Herrmann294b0c92009-08-18 13:02:29 +02007118 sd = &per_cpu(phys_domains, i).sd;
Siddha, Suresh B89c47102006-10-03 01:14:09 -07007119 init_sched_groups_power(i, sd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007120 }
7121
John Hawkes9c1cfda2005-09-06 15:18:14 -07007122#ifdef CONFIG_NUMA
Mike Travis076ac2a2008-05-12 21:21:12 +02007123 for (i = 0; i < nr_node_ids; i++)
Andreas Herrmann49a02c52009-08-18 12:51:52 +02007124 init_numa_sched_groups_power(d.sched_group_nodes[i]);
John Hawkes9c1cfda2005-09-06 15:18:14 -07007125
Andreas Herrmann49a02c52009-08-18 12:51:52 +02007126 if (d.sd_allnodes) {
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08007127 struct sched_group *sg;
Siddha, Suresh Bf712c0c2006-07-30 03:02:59 -07007128
Rusty Russell96f874e2008-11-25 02:35:14 +10307129 cpu_to_allnodes_group(cpumask_first(cpu_map), cpu_map, &sg,
Andreas Herrmann49a02c52009-08-18 12:51:52 +02007130 d.tmpmask);
Siddha, Suresh Bf712c0c2006-07-30 03:02:59 -07007131 init_numa_sched_groups_power(sg);
7132 }
John Hawkes9c1cfda2005-09-06 15:18:14 -07007133#endif
7134
Linus Torvalds1da177e2005-04-16 15:20:36 -07007135 /* Attach the domains */
Rusty Russellabcd0832008-11-25 02:35:02 +10307136 for_each_cpu(i, cpu_map) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007137#ifdef CONFIG_SCHED_SMT
Rusty Russell6c99e9a2008-11-25 02:35:04 +10307138 sd = &per_cpu(cpu_domains, i).sd;
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08007139#elif defined(CONFIG_SCHED_MC)
Rusty Russell6c99e9a2008-11-25 02:35:04 +10307140 sd = &per_cpu(core_domains, i).sd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007141#else
Rusty Russell6c99e9a2008-11-25 02:35:04 +10307142 sd = &per_cpu(phys_domains, i).sd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007143#endif
Andreas Herrmann49a02c52009-08-18 12:51:52 +02007144 cpu_attach_domain(sd, d.rd, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007145 }
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07007146
Andreas Herrmann2109b992009-08-18 12:53:00 +02007147 d.sched_group_nodes = NULL; /* don't free this we still need it */
7148 __free_domain_allocs(&d, sa_tmpmask, cpu_map);
7149 return 0;
Rusty Russell3404c8d2008-11-25 02:35:03 +10307150
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07007151error:
Andreas Herrmann2109b992009-08-18 12:53:00 +02007152 __free_domain_allocs(&d, alloc_state, cpu_map);
7153 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007154}
Paul Jackson029190c2007-10-18 23:40:20 -07007155
Rusty Russell96f874e2008-11-25 02:35:14 +10307156static int build_sched_domains(const struct cpumask *cpu_map)
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09007157{
7158 return __build_sched_domains(cpu_map, NULL);
7159}
7160
Rusty Russellacc3f5d2009-11-03 14:53:40 +10307161static cpumask_var_t *doms_cur; /* current sched domains */
Paul Jackson029190c2007-10-18 23:40:20 -07007162static int ndoms_cur; /* number of sched domains in 'doms_cur' */
Ingo Molnar4285f5942008-05-16 17:47:14 +02007163static struct sched_domain_attr *dattr_cur;
7164 /* attribues of custom domains in 'doms_cur' */
Paul Jackson029190c2007-10-18 23:40:20 -07007165
7166/*
7167 * Special case: If a kmalloc of a doms_cur partition (array of
Rusty Russell42128232008-11-25 02:35:12 +10307168 * cpumask) fails, then fallback to a single sched domain,
7169 * as determined by the single cpumask fallback_doms.
Paul Jackson029190c2007-10-18 23:40:20 -07007170 */
Rusty Russell42128232008-11-25 02:35:12 +10307171static cpumask_var_t fallback_doms;
Paul Jackson029190c2007-10-18 23:40:20 -07007172
Heiko Carstensee79d1b2008-12-09 18:49:50 +01007173/*
7174 * arch_update_cpu_topology lets virtualized architectures update the
7175 * cpu core maps. It is supposed to return 1 if the topology changed
7176 * or 0 if it stayed the same.
7177 */
7178int __attribute__((weak)) arch_update_cpu_topology(void)
Heiko Carstens22e52b02008-03-12 18:31:59 +01007179{
Heiko Carstensee79d1b2008-12-09 18:49:50 +01007180 return 0;
Heiko Carstens22e52b02008-03-12 18:31:59 +01007181}
7182
Rusty Russellacc3f5d2009-11-03 14:53:40 +10307183cpumask_var_t *alloc_sched_domains(unsigned int ndoms)
7184{
7185 int i;
7186 cpumask_var_t *doms;
7187
7188 doms = kmalloc(sizeof(*doms) * ndoms, GFP_KERNEL);
7189 if (!doms)
7190 return NULL;
7191 for (i = 0; i < ndoms; i++) {
7192 if (!alloc_cpumask_var(&doms[i], GFP_KERNEL)) {
7193 free_sched_domains(doms, i);
7194 return NULL;
7195 }
7196 }
7197 return doms;
7198}
7199
7200void free_sched_domains(cpumask_var_t doms[], unsigned int ndoms)
7201{
7202 unsigned int i;
7203 for (i = 0; i < ndoms; i++)
7204 free_cpumask_var(doms[i]);
7205 kfree(doms);
7206}
7207
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07007208/*
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01007209 * Set up scheduler domains and groups. Callers must hold the hotplug lock.
Paul Jackson029190c2007-10-18 23:40:20 -07007210 * For now this just excludes isolated cpus, but could be used to
7211 * exclude other special cases in the future.
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07007212 */
Rusty Russell96f874e2008-11-25 02:35:14 +10307213static int arch_init_sched_domains(const struct cpumask *cpu_map)
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07007214{
Milton Miller73785472007-10-24 18:23:48 +02007215 int err;
7216
Heiko Carstens22e52b02008-03-12 18:31:59 +01007217 arch_update_cpu_topology();
Paul Jackson029190c2007-10-18 23:40:20 -07007218 ndoms_cur = 1;
Rusty Russellacc3f5d2009-11-03 14:53:40 +10307219 doms_cur = alloc_sched_domains(ndoms_cur);
Paul Jackson029190c2007-10-18 23:40:20 -07007220 if (!doms_cur)
Rusty Russellacc3f5d2009-11-03 14:53:40 +10307221 doms_cur = &fallback_doms;
7222 cpumask_andnot(doms_cur[0], cpu_map, cpu_isolated_map);
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09007223 dattr_cur = NULL;
Rusty Russellacc3f5d2009-11-03 14:53:40 +10307224 err = build_sched_domains(doms_cur[0]);
Milton Miller6382bc92007-10-15 17:00:19 +02007225 register_sched_domain_sysctl();
Milton Miller73785472007-10-24 18:23:48 +02007226
7227 return err;
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07007228}
7229
Rusty Russell96f874e2008-11-25 02:35:14 +10307230static void arch_destroy_sched_domains(const struct cpumask *cpu_map,
7231 struct cpumask *tmpmask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007232{
Mike Travis7c16ec52008-04-04 18:11:11 -07007233 free_sched_groups(cpu_map, tmpmask);
John Hawkes9c1cfda2005-09-06 15:18:14 -07007234}
Linus Torvalds1da177e2005-04-16 15:20:36 -07007235
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07007236/*
7237 * Detach sched domains from a group of cpus specified in cpu_map
7238 * These cpus will now be attached to the NULL domain
7239 */
Rusty Russell96f874e2008-11-25 02:35:14 +10307240static void detach_destroy_domains(const struct cpumask *cpu_map)
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07007241{
Rusty Russell96f874e2008-11-25 02:35:14 +10307242 /* Save because hotplug lock held. */
7243 static DECLARE_BITMAP(tmpmask, CONFIG_NR_CPUS);
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07007244 int i;
7245
Rusty Russellabcd0832008-11-25 02:35:02 +10307246 for_each_cpu(i, cpu_map)
Gregory Haskins57d885f2008-01-25 21:08:18 +01007247 cpu_attach_domain(NULL, &def_root_domain, i);
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07007248 synchronize_sched();
Rusty Russell96f874e2008-11-25 02:35:14 +10307249 arch_destroy_sched_domains(cpu_map, to_cpumask(tmpmask));
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07007250}
7251
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09007252/* handle null as "default" */
7253static int dattrs_equal(struct sched_domain_attr *cur, int idx_cur,
7254 struct sched_domain_attr *new, int idx_new)
7255{
7256 struct sched_domain_attr tmp;
7257
7258 /* fast path */
7259 if (!new && !cur)
7260 return 1;
7261
7262 tmp = SD_ATTR_INIT;
7263 return !memcmp(cur ? (cur + idx_cur) : &tmp,
7264 new ? (new + idx_new) : &tmp,
7265 sizeof(struct sched_domain_attr));
7266}
7267
Paul Jackson029190c2007-10-18 23:40:20 -07007268/*
7269 * Partition sched domains as specified by the 'ndoms_new'
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01007270 * cpumasks in the array doms_new[] of cpumasks. This compares
Paul Jackson029190c2007-10-18 23:40:20 -07007271 * doms_new[] to the current sched domain partitioning, doms_cur[].
7272 * It destroys each deleted domain and builds each new domain.
7273 *
Rusty Russellacc3f5d2009-11-03 14:53:40 +10307274 * 'doms_new' is an array of cpumask_var_t's of length 'ndoms_new'.
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01007275 * The masks don't intersect (don't overlap.) We should setup one
7276 * sched domain for each mask. CPUs not in any of the cpumasks will
7277 * not be load balanced. If the same cpumask appears both in the
Paul Jackson029190c2007-10-18 23:40:20 -07007278 * current 'doms_cur' domains and in the new 'doms_new', we can leave
7279 * it as it is.
7280 *
Rusty Russellacc3f5d2009-11-03 14:53:40 +10307281 * The passed in 'doms_new' should be allocated using
7282 * alloc_sched_domains. This routine takes ownership of it and will
7283 * free_sched_domains it when done with it. If the caller failed the
7284 * alloc call, then it can pass in doms_new == NULL && ndoms_new == 1,
7285 * and partition_sched_domains() will fallback to the single partition
7286 * 'fallback_doms', it also forces the domains to be rebuilt.
Paul Jackson029190c2007-10-18 23:40:20 -07007287 *
Rusty Russell96f874e2008-11-25 02:35:14 +10307288 * If doms_new == NULL it will be replaced with cpu_online_mask.
Li Zefan700018e2008-11-18 14:02:03 +08007289 * ndoms_new == 0 is a special case for destroying existing domains,
7290 * and it will not create the default domain.
Max Krasnyanskydfb512e2008-08-29 13:11:41 -07007291 *
Paul Jackson029190c2007-10-18 23:40:20 -07007292 * Call with hotplug lock held
7293 */
Rusty Russellacc3f5d2009-11-03 14:53:40 +10307294void partition_sched_domains(int ndoms_new, cpumask_var_t doms_new[],
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09007295 struct sched_domain_attr *dattr_new)
Paul Jackson029190c2007-10-18 23:40:20 -07007296{
Max Krasnyanskydfb512e2008-08-29 13:11:41 -07007297 int i, j, n;
Heiko Carstensd65bd5e2008-12-09 18:49:51 +01007298 int new_topology;
Paul Jackson029190c2007-10-18 23:40:20 -07007299
Heiko Carstens712555e2008-04-28 11:33:07 +02007300 mutex_lock(&sched_domains_mutex);
Srivatsa Vaddagiria1835612008-01-25 21:08:00 +01007301
Milton Miller73785472007-10-24 18:23:48 +02007302 /* always unregister in case we don't destroy any domains */
7303 unregister_sched_domain_sysctl();
7304
Heiko Carstensd65bd5e2008-12-09 18:49:51 +01007305 /* Let architecture update cpu core mappings. */
7306 new_topology = arch_update_cpu_topology();
7307
Max Krasnyanskydfb512e2008-08-29 13:11:41 -07007308 n = doms_new ? ndoms_new : 0;
Paul Jackson029190c2007-10-18 23:40:20 -07007309
7310 /* Destroy deleted domains */
7311 for (i = 0; i < ndoms_cur; i++) {
Heiko Carstensd65bd5e2008-12-09 18:49:51 +01007312 for (j = 0; j < n && !new_topology; j++) {
Rusty Russellacc3f5d2009-11-03 14:53:40 +10307313 if (cpumask_equal(doms_cur[i], doms_new[j])
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09007314 && dattrs_equal(dattr_cur, i, dattr_new, j))
Paul Jackson029190c2007-10-18 23:40:20 -07007315 goto match1;
7316 }
7317 /* no match - a current sched domain not in new doms_new[] */
Rusty Russellacc3f5d2009-11-03 14:53:40 +10307318 detach_destroy_domains(doms_cur[i]);
Paul Jackson029190c2007-10-18 23:40:20 -07007319match1:
7320 ;
7321 }
7322
Max Krasnyanskye761b772008-07-15 04:43:49 -07007323 if (doms_new == NULL) {
7324 ndoms_cur = 0;
Rusty Russellacc3f5d2009-11-03 14:53:40 +10307325 doms_new = &fallback_doms;
Peter Zijlstra6ad4c182009-11-25 13:31:39 +01007326 cpumask_andnot(doms_new[0], cpu_active_mask, cpu_isolated_map);
Li Zefanfaa2f982008-11-04 16:20:23 +08007327 WARN_ON_ONCE(dattr_new);
Max Krasnyanskye761b772008-07-15 04:43:49 -07007328 }
7329
Paul Jackson029190c2007-10-18 23:40:20 -07007330 /* Build new domains */
7331 for (i = 0; i < ndoms_new; i++) {
Heiko Carstensd65bd5e2008-12-09 18:49:51 +01007332 for (j = 0; j < ndoms_cur && !new_topology; j++) {
Rusty Russellacc3f5d2009-11-03 14:53:40 +10307333 if (cpumask_equal(doms_new[i], doms_cur[j])
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09007334 && dattrs_equal(dattr_new, i, dattr_cur, j))
Paul Jackson029190c2007-10-18 23:40:20 -07007335 goto match2;
7336 }
7337 /* no match - add a new doms_new */
Rusty Russellacc3f5d2009-11-03 14:53:40 +10307338 __build_sched_domains(doms_new[i],
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09007339 dattr_new ? dattr_new + i : NULL);
Paul Jackson029190c2007-10-18 23:40:20 -07007340match2:
7341 ;
7342 }
7343
7344 /* Remember the new sched domains */
Rusty Russellacc3f5d2009-11-03 14:53:40 +10307345 if (doms_cur != &fallback_doms)
7346 free_sched_domains(doms_cur, ndoms_cur);
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09007347 kfree(dattr_cur); /* kfree(NULL) is safe */
Paul Jackson029190c2007-10-18 23:40:20 -07007348 doms_cur = doms_new;
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09007349 dattr_cur = dattr_new;
Paul Jackson029190c2007-10-18 23:40:20 -07007350 ndoms_cur = ndoms_new;
Milton Miller73785472007-10-24 18:23:48 +02007351
7352 register_sched_domain_sysctl();
Srivatsa Vaddagiria1835612008-01-25 21:08:00 +01007353
Heiko Carstens712555e2008-04-28 11:33:07 +02007354 mutex_unlock(&sched_domains_mutex);
Paul Jackson029190c2007-10-18 23:40:20 -07007355}
7356
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07007357#if defined(CONFIG_SCHED_MC) || defined(CONFIG_SCHED_SMT)
Li Zefanc70f22d2009-01-05 19:07:50 +08007358static void arch_reinit_sched_domains(void)
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07007359{
Gautham R Shenoy95402b32008-01-25 21:08:02 +01007360 get_online_cpus();
Max Krasnyanskydfb512e2008-08-29 13:11:41 -07007361
7362 /* Destroy domains first to force the rebuild */
7363 partition_sched_domains(0, NULL, NULL);
7364
Max Krasnyanskye761b772008-07-15 04:43:49 -07007365 rebuild_sched_domains();
Gautham R Shenoy95402b32008-01-25 21:08:02 +01007366 put_online_cpus();
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07007367}
7368
7369static ssize_t sched_power_savings_store(const char *buf, size_t count, int smt)
7370{
Gautham R Shenoyafb8a9b2008-12-18 23:26:09 +05307371 unsigned int level = 0;
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07007372
Gautham R Shenoyafb8a9b2008-12-18 23:26:09 +05307373 if (sscanf(buf, "%u", &level) != 1)
7374 return -EINVAL;
7375
7376 /*
7377 * level is always be positive so don't check for
7378 * level < POWERSAVINGS_BALANCE_NONE which is 0
7379 * What happens on 0 or 1 byte write,
7380 * need to check for count as well?
7381 */
7382
7383 if (level >= MAX_POWERSAVINGS_BALANCE_LEVELS)
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07007384 return -EINVAL;
7385
7386 if (smt)
Gautham R Shenoyafb8a9b2008-12-18 23:26:09 +05307387 sched_smt_power_savings = level;
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07007388 else
Gautham R Shenoyafb8a9b2008-12-18 23:26:09 +05307389 sched_mc_power_savings = level;
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07007390
Li Zefanc70f22d2009-01-05 19:07:50 +08007391 arch_reinit_sched_domains();
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07007392
Li Zefanc70f22d2009-01-05 19:07:50 +08007393 return count;
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07007394}
7395
Adrian Bunk6707de002007-08-12 18:08:19 +02007396#ifdef CONFIG_SCHED_MC
Andi Kleenf718cd42008-07-29 22:33:52 -07007397static ssize_t sched_mc_power_savings_show(struct sysdev_class *class,
7398 char *page)
Adrian Bunk6707de002007-08-12 18:08:19 +02007399{
7400 return sprintf(page, "%u\n", sched_mc_power_savings);
7401}
Andi Kleenf718cd42008-07-29 22:33:52 -07007402static ssize_t sched_mc_power_savings_store(struct sysdev_class *class,
Adrian Bunk6707de002007-08-12 18:08:19 +02007403 const char *buf, size_t count)
7404{
7405 return sched_power_savings_store(buf, count, 0);
7406}
Andi Kleenf718cd42008-07-29 22:33:52 -07007407static SYSDEV_CLASS_ATTR(sched_mc_power_savings, 0644,
7408 sched_mc_power_savings_show,
7409 sched_mc_power_savings_store);
Adrian Bunk6707de002007-08-12 18:08:19 +02007410#endif
7411
7412#ifdef CONFIG_SCHED_SMT
Andi Kleenf718cd42008-07-29 22:33:52 -07007413static ssize_t sched_smt_power_savings_show(struct sysdev_class *dev,
7414 char *page)
Adrian Bunk6707de002007-08-12 18:08:19 +02007415{
7416 return sprintf(page, "%u\n", sched_smt_power_savings);
7417}
Andi Kleenf718cd42008-07-29 22:33:52 -07007418static ssize_t sched_smt_power_savings_store(struct sysdev_class *dev,
Adrian Bunk6707de002007-08-12 18:08:19 +02007419 const char *buf, size_t count)
7420{
7421 return sched_power_savings_store(buf, count, 1);
7422}
Andi Kleenf718cd42008-07-29 22:33:52 -07007423static SYSDEV_CLASS_ATTR(sched_smt_power_savings, 0644,
7424 sched_smt_power_savings_show,
Adrian Bunk6707de002007-08-12 18:08:19 +02007425 sched_smt_power_savings_store);
7426#endif
7427
Li Zefan39aac642009-01-05 19:18:02 +08007428int __init sched_create_sysfs_power_savings_entries(struct sysdev_class *cls)
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07007429{
7430 int err = 0;
Ingo Molnar48f24c42006-07-03 00:25:40 -07007431
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07007432#ifdef CONFIG_SCHED_SMT
7433 if (smt_capable())
7434 err = sysfs_create_file(&cls->kset.kobj,
7435 &attr_sched_smt_power_savings.attr);
7436#endif
7437#ifdef CONFIG_SCHED_MC
7438 if (!err && mc_capable())
7439 err = sysfs_create_file(&cls->kset.kobj,
7440 &attr_sched_mc_power_savings.attr);
7441#endif
7442 return err;
7443}
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +02007444#endif /* CONFIG_SCHED_MC || CONFIG_SCHED_SMT */
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07007445
Max Krasnyanskye761b772008-07-15 04:43:49 -07007446#ifndef CONFIG_CPUSETS
Linus Torvalds1da177e2005-04-16 15:20:36 -07007447/*
Max Krasnyanskye761b772008-07-15 04:43:49 -07007448 * Add online and remove offline CPUs from the scheduler domains.
7449 * When cpusets are enabled they take over this function.
Linus Torvalds1da177e2005-04-16 15:20:36 -07007450 */
7451static int update_sched_domains(struct notifier_block *nfb,
7452 unsigned long action, void *hcpu)
7453{
Max Krasnyanskye761b772008-07-15 04:43:49 -07007454 switch (action) {
7455 case CPU_ONLINE:
7456 case CPU_ONLINE_FROZEN:
Peter Zijlstra6ad4c182009-11-25 13:31:39 +01007457 case CPU_DOWN_PREPARE:
7458 case CPU_DOWN_PREPARE_FROZEN:
7459 case CPU_DOWN_FAILED:
7460 case CPU_DOWN_FAILED_FROZEN:
Max Krasnyanskydfb512e2008-08-29 13:11:41 -07007461 partition_sched_domains(1, NULL, NULL);
Max Krasnyanskye761b772008-07-15 04:43:49 -07007462 return NOTIFY_OK;
7463
7464 default:
7465 return NOTIFY_DONE;
7466 }
7467}
7468#endif
7469
7470static int update_runtime(struct notifier_block *nfb,
7471 unsigned long action, void *hcpu)
7472{
Peter Zijlstra7def2be2008-06-05 14:49:58 +02007473 int cpu = (int)(long)hcpu;
7474
Linus Torvalds1da177e2005-04-16 15:20:36 -07007475 switch (action) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007476 case CPU_DOWN_PREPARE:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07007477 case CPU_DOWN_PREPARE_FROZEN:
Peter Zijlstra7def2be2008-06-05 14:49:58 +02007478 disable_runtime(cpu_rq(cpu));
Linus Torvalds1da177e2005-04-16 15:20:36 -07007479 return NOTIFY_OK;
7480
Linus Torvalds1da177e2005-04-16 15:20:36 -07007481 case CPU_DOWN_FAILED:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07007482 case CPU_DOWN_FAILED_FROZEN:
Linus Torvalds1da177e2005-04-16 15:20:36 -07007483 case CPU_ONLINE:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07007484 case CPU_ONLINE_FROZEN:
Peter Zijlstra7def2be2008-06-05 14:49:58 +02007485 enable_runtime(cpu_rq(cpu));
Max Krasnyanskye761b772008-07-15 04:43:49 -07007486 return NOTIFY_OK;
7487
Linus Torvalds1da177e2005-04-16 15:20:36 -07007488 default:
7489 return NOTIFY_DONE;
7490 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007491}
Linus Torvalds1da177e2005-04-16 15:20:36 -07007492
7493void __init sched_init_smp(void)
7494{
Rusty Russelldcc30a32008-11-25 02:35:12 +10307495 cpumask_var_t non_isolated_cpus;
7496
7497 alloc_cpumask_var(&non_isolated_cpus, GFP_KERNEL);
Yong Zhangcb5fd132009-09-14 20:20:16 +08007498 alloc_cpumask_var(&fallback_doms, GFP_KERNEL);
Nick Piggin5c1e1762006-10-03 01:14:04 -07007499
Mike Travis434d53b2008-04-04 18:11:04 -07007500#if defined(CONFIG_NUMA)
7501 sched_group_nodes_bycpu = kzalloc(nr_cpu_ids * sizeof(void **),
7502 GFP_KERNEL);
7503 BUG_ON(sched_group_nodes_bycpu == NULL);
7504#endif
Gautham R Shenoy95402b32008-01-25 21:08:02 +01007505 get_online_cpus();
Heiko Carstens712555e2008-04-28 11:33:07 +02007506 mutex_lock(&sched_domains_mutex);
Peter Zijlstra6ad4c182009-11-25 13:31:39 +01007507 arch_init_sched_domains(cpu_active_mask);
Rusty Russelldcc30a32008-11-25 02:35:12 +10307508 cpumask_andnot(non_isolated_cpus, cpu_possible_mask, cpu_isolated_map);
7509 if (cpumask_empty(non_isolated_cpus))
7510 cpumask_set_cpu(smp_processor_id(), non_isolated_cpus);
Heiko Carstens712555e2008-04-28 11:33:07 +02007511 mutex_unlock(&sched_domains_mutex);
Gautham R Shenoy95402b32008-01-25 21:08:02 +01007512 put_online_cpus();
Max Krasnyanskye761b772008-07-15 04:43:49 -07007513
7514#ifndef CONFIG_CPUSETS
Linus Torvalds1da177e2005-04-16 15:20:36 -07007515 /* XXX: Theoretical race here - CPU may be hotplugged now */
7516 hotcpu_notifier(update_sched_domains, 0);
Max Krasnyanskye761b772008-07-15 04:43:49 -07007517#endif
7518
7519 /* RT runtime code needs to handle some hotplug events */
7520 hotcpu_notifier(update_runtime, 0);
7521
Peter Zijlstrab328ca12008-04-29 10:02:46 +02007522 init_hrtick();
Nick Piggin5c1e1762006-10-03 01:14:04 -07007523
7524 /* Move init over to a non-isolated CPU */
Rusty Russelldcc30a32008-11-25 02:35:12 +10307525 if (set_cpus_allowed_ptr(current, non_isolated_cpus) < 0)
Nick Piggin5c1e1762006-10-03 01:14:04 -07007526 BUG();
Ingo Molnar19978ca2007-11-09 22:39:38 +01007527 sched_init_granularity();
Rusty Russelldcc30a32008-11-25 02:35:12 +10307528 free_cpumask_var(non_isolated_cpus);
Rusty Russell42128232008-11-25 02:35:12 +10307529
Rusty Russell0e3900e2008-11-25 02:35:13 +10307530 init_sched_rt_class();
Linus Torvalds1da177e2005-04-16 15:20:36 -07007531}
7532#else
7533void __init sched_init_smp(void)
7534{
Ingo Molnar19978ca2007-11-09 22:39:38 +01007535 sched_init_granularity();
Linus Torvalds1da177e2005-04-16 15:20:36 -07007536}
7537#endif /* CONFIG_SMP */
7538
Arun R Bharadwajcd1bb942009-04-16 12:15:34 +05307539const_debug unsigned int sysctl_timer_migration = 1;
7540
Linus Torvalds1da177e2005-04-16 15:20:36 -07007541int in_sched_functions(unsigned long addr)
7542{
Linus Torvalds1da177e2005-04-16 15:20:36 -07007543 return in_lock_functions(addr) ||
7544 (addr >= (unsigned long)__sched_text_start
7545 && addr < (unsigned long)__sched_text_end);
7546}
7547
Alexey Dobriyana9957442007-10-15 17:00:13 +02007548static void init_cfs_rq(struct cfs_rq *cfs_rq, struct rq *rq)
Ingo Molnardd41f592007-07-09 18:51:59 +02007549{
7550 cfs_rq->tasks_timeline = RB_ROOT;
Peter Zijlstra4a55bd52008-04-19 19:45:00 +02007551 INIT_LIST_HEAD(&cfs_rq->tasks);
Ingo Molnardd41f592007-07-09 18:51:59 +02007552#ifdef CONFIG_FAIR_GROUP_SCHED
7553 cfs_rq->rq = rq;
7554#endif
Peter Zijlstra67e9fb22007-10-15 17:00:10 +02007555 cfs_rq->min_vruntime = (u64)(-(1LL << 20));
Ingo Molnardd41f592007-07-09 18:51:59 +02007556}
7557
Peter Zijlstrafa85ae22008-01-25 21:08:29 +01007558static void init_rt_rq(struct rt_rq *rt_rq, struct rq *rq)
7559{
7560 struct rt_prio_array *array;
7561 int i;
7562
7563 array = &rt_rq->active;
7564 for (i = 0; i < MAX_RT_PRIO; i++) {
7565 INIT_LIST_HEAD(array->queue + i);
7566 __clear_bit(i, array->bitmap);
7567 }
7568 /* delimiter for bitsearch: */
7569 __set_bit(MAX_RT_PRIO, array->bitmap);
7570
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01007571#if defined CONFIG_SMP || defined CONFIG_RT_GROUP_SCHED
Gregory Haskinse864c492008-12-29 09:39:49 -05007572 rt_rq->highest_prio.curr = MAX_RT_PRIO;
Gregory Haskins398a1532009-01-14 09:10:04 -05007573#ifdef CONFIG_SMP
Gregory Haskinse864c492008-12-29 09:39:49 -05007574 rt_rq->highest_prio.next = MAX_RT_PRIO;
Peter Zijlstra48d5e252008-01-25 21:08:31 +01007575#endif
Peter Zijlstrafa85ae22008-01-25 21:08:29 +01007576#endif
7577#ifdef CONFIG_SMP
7578 rt_rq->rt_nr_migratory = 0;
Peter Zijlstrafa85ae22008-01-25 21:08:29 +01007579 rt_rq->overloaded = 0;
Thomas Gleixner05fa7852009-11-17 14:28:38 +01007580 plist_head_init_raw(&rt_rq->pushable_tasks, &rq->lock);
Peter Zijlstrafa85ae22008-01-25 21:08:29 +01007581#endif
7582
7583 rt_rq->rt_time = 0;
7584 rt_rq->rt_throttled = 0;
Peter Zijlstraac086bc2008-04-19 19:44:58 +02007585 rt_rq->rt_runtime = 0;
Thomas Gleixner0986b112009-11-17 15:32:06 +01007586 raw_spin_lock_init(&rt_rq->rt_runtime_lock);
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007587
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01007588#ifdef CONFIG_RT_GROUP_SCHED
Peter Zijlstra23b0fdf2008-02-13 15:45:39 +01007589 rt_rq->rt_nr_boosted = 0;
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007590 rt_rq->rq = rq;
7591#endif
Peter Zijlstrafa85ae22008-01-25 21:08:29 +01007592}
7593
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007594#ifdef CONFIG_FAIR_GROUP_SCHED
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02007595static void init_tg_cfs_entry(struct task_group *tg, struct cfs_rq *cfs_rq,
7596 struct sched_entity *se, int cpu, int add,
7597 struct sched_entity *parent)
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007598{
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02007599 struct rq *rq = cpu_rq(cpu);
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007600 tg->cfs_rq[cpu] = cfs_rq;
7601 init_cfs_rq(cfs_rq, rq);
7602 cfs_rq->tg = tg;
7603 if (add)
7604 list_add(&cfs_rq->leaf_cfs_rq_list, &rq->leaf_cfs_rq_list);
7605
7606 tg->se[cpu] = se;
Dhaval Giani354d60c2008-04-19 19:44:59 +02007607 /* se could be NULL for init_task_group */
7608 if (!se)
7609 return;
7610
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02007611 if (!parent)
7612 se->cfs_rq = &rq->cfs;
7613 else
7614 se->cfs_rq = parent->my_q;
7615
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007616 se->my_q = cfs_rq;
7617 se->load.weight = tg->shares;
Peter Zijlstrae05510d2008-05-05 23:56:17 +02007618 se->load.inv_weight = 0;
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02007619 se->parent = parent;
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007620}
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01007621#endif
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007622
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01007623#ifdef CONFIG_RT_GROUP_SCHED
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02007624static void init_tg_rt_entry(struct task_group *tg, struct rt_rq *rt_rq,
7625 struct sched_rt_entity *rt_se, int cpu, int add,
7626 struct sched_rt_entity *parent)
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007627{
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02007628 struct rq *rq = cpu_rq(cpu);
7629
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007630 tg->rt_rq[cpu] = rt_rq;
7631 init_rt_rq(rt_rq, rq);
7632 rt_rq->tg = tg;
Peter Zijlstraac086bc2008-04-19 19:44:58 +02007633 rt_rq->rt_runtime = tg->rt_bandwidth.rt_runtime;
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007634 if (add)
7635 list_add(&rt_rq->leaf_rt_rq_list, &rq->leaf_rt_rq_list);
7636
7637 tg->rt_se[cpu] = rt_se;
Dhaval Giani354d60c2008-04-19 19:44:59 +02007638 if (!rt_se)
7639 return;
7640
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02007641 if (!parent)
7642 rt_se->rt_rq = &rq->rt;
7643 else
7644 rt_se->rt_rq = parent->my_q;
7645
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007646 rt_se->my_q = rt_rq;
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02007647 rt_se->parent = parent;
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007648 INIT_LIST_HEAD(&rt_se->run_list);
7649}
7650#endif
7651
Linus Torvalds1da177e2005-04-16 15:20:36 -07007652void __init sched_init(void)
7653{
Ingo Molnardd41f592007-07-09 18:51:59 +02007654 int i, j;
Mike Travis434d53b2008-04-04 18:11:04 -07007655 unsigned long alloc_size = 0, ptr;
7656
7657#ifdef CONFIG_FAIR_GROUP_SCHED
7658 alloc_size += 2 * nr_cpu_ids * sizeof(void **);
7659#endif
7660#ifdef CONFIG_RT_GROUP_SCHED
7661 alloc_size += 2 * nr_cpu_ids * sizeof(void **);
7662#endif
Rusty Russelldf7c8e82009-03-19 15:22:20 +10307663#ifdef CONFIG_CPUMASK_OFFSTACK
Rusty Russell8c083f02009-03-19 15:22:20 +10307664 alloc_size += num_possible_cpus() * cpumask_size();
Rusty Russelldf7c8e82009-03-19 15:22:20 +10307665#endif
Mike Travis434d53b2008-04-04 18:11:04 -07007666 if (alloc_size) {
Pekka Enberg36b7b6d2009-06-10 23:42:36 +03007667 ptr = (unsigned long)kzalloc(alloc_size, GFP_NOWAIT);
Mike Travis434d53b2008-04-04 18:11:04 -07007668
7669#ifdef CONFIG_FAIR_GROUP_SCHED
7670 init_task_group.se = (struct sched_entity **)ptr;
7671 ptr += nr_cpu_ids * sizeof(void **);
7672
7673 init_task_group.cfs_rq = (struct cfs_rq **)ptr;
7674 ptr += nr_cpu_ids * sizeof(void **);
Peter Zijlstraeff766a2008-04-19 19:45:00 +02007675
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +02007676#endif /* CONFIG_FAIR_GROUP_SCHED */
Mike Travis434d53b2008-04-04 18:11:04 -07007677#ifdef CONFIG_RT_GROUP_SCHED
7678 init_task_group.rt_se = (struct sched_rt_entity **)ptr;
7679 ptr += nr_cpu_ids * sizeof(void **);
7680
7681 init_task_group.rt_rq = (struct rt_rq **)ptr;
Peter Zijlstraeff766a2008-04-19 19:45:00 +02007682 ptr += nr_cpu_ids * sizeof(void **);
7683
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +02007684#endif /* CONFIG_RT_GROUP_SCHED */
Rusty Russelldf7c8e82009-03-19 15:22:20 +10307685#ifdef CONFIG_CPUMASK_OFFSTACK
7686 for_each_possible_cpu(i) {
7687 per_cpu(load_balance_tmpmask, i) = (void *)ptr;
7688 ptr += cpumask_size();
7689 }
7690#endif /* CONFIG_CPUMASK_OFFSTACK */
Mike Travis434d53b2008-04-04 18:11:04 -07007691 }
Ingo Molnardd41f592007-07-09 18:51:59 +02007692
Gregory Haskins57d885f2008-01-25 21:08:18 +01007693#ifdef CONFIG_SMP
7694 init_defrootdomain();
7695#endif
7696
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02007697 init_rt_bandwidth(&def_rt_bandwidth,
7698 global_rt_period(), global_rt_runtime());
7699
7700#ifdef CONFIG_RT_GROUP_SCHED
7701 init_rt_bandwidth(&init_task_group.rt_bandwidth,
7702 global_rt_period(), global_rt_runtime());
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +02007703#endif /* CONFIG_RT_GROUP_SCHED */
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02007704
Dhaval Giani7c941432010-01-20 13:26:18 +01007705#ifdef CONFIG_CGROUP_SCHED
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007706 list_add(&init_task_group.list, &task_groups);
Peter Zijlstraf473aa52008-04-19 19:45:00 +02007707 INIT_LIST_HEAD(&init_task_group.children);
7708
Dhaval Giani7c941432010-01-20 13:26:18 +01007709#endif /* CONFIG_CGROUP_SCHED */
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007710
Jiri Kosina4a6cc4b2009-10-29 00:26:00 +09007711#if defined CONFIG_FAIR_GROUP_SCHED && defined CONFIG_SMP
7712 update_shares_data = __alloc_percpu(nr_cpu_ids * sizeof(unsigned long),
7713 __alignof__(unsigned long));
7714#endif
KAMEZAWA Hiroyuki0a945022006-03-28 01:56:37 -08007715 for_each_possible_cpu(i) {
Ingo Molnar70b97a72006-07-03 00:25:42 -07007716 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007717
7718 rq = cpu_rq(i);
Thomas Gleixner05fa7852009-11-17 14:28:38 +01007719 raw_spin_lock_init(&rq->lock);
Nick Piggin78979862005-06-25 14:57:13 -07007720 rq->nr_running = 0;
Thomas Gleixnerdce48a82009-04-11 10:43:41 +02007721 rq->calc_load_active = 0;
7722 rq->calc_load_update = jiffies + LOAD_FREQ;
Ingo Molnardd41f592007-07-09 18:51:59 +02007723 init_cfs_rq(&rq->cfs, rq);
Peter Zijlstrafa85ae22008-01-25 21:08:29 +01007724 init_rt_rq(&rq->rt, rq);
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007725#ifdef CONFIG_FAIR_GROUP_SCHED
7726 init_task_group.shares = init_task_group_load;
7727 INIT_LIST_HEAD(&rq->leaf_cfs_rq_list);
Dhaval Giani354d60c2008-04-19 19:44:59 +02007728#ifdef CONFIG_CGROUP_SCHED
7729 /*
7730 * How much cpu bandwidth does init_task_group get?
7731 *
7732 * In case of task-groups formed thr' the cgroup filesystem, it
7733 * gets 100% of the cpu resources in the system. This overall
7734 * system cpu resource is divided among the tasks of
7735 * init_task_group and its child task-groups in a fair manner,
7736 * based on each entity's (task or task-group's) weight
7737 * (se->load.weight).
7738 *
7739 * In other words, if init_task_group has 10 tasks of weight
7740 * 1024) and two child groups A0 and A1 (of weight 1024 each),
7741 * then A0's share of the cpu resource is:
7742 *
Ingo Molnar0d905bc2009-05-04 19:13:30 +02007743 * A0's bandwidth = 1024 / (10*1024 + 1024 + 1024) = 8.33%
Dhaval Giani354d60c2008-04-19 19:44:59 +02007744 *
7745 * We achieve this by letting init_task_group's tasks sit
7746 * directly in rq->cfs (i.e init_task_group->se[] = NULL).
7747 */
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02007748 init_tg_cfs_entry(&init_task_group, &rq->cfs, NULL, i, 1, NULL);
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01007749#endif
Dhaval Giani354d60c2008-04-19 19:44:59 +02007750#endif /* CONFIG_FAIR_GROUP_SCHED */
7751
7752 rq->rt.rt_runtime = def_rt_bandwidth.rt_runtime;
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01007753#ifdef CONFIG_RT_GROUP_SCHED
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007754 INIT_LIST_HEAD(&rq->leaf_rt_rq_list);
Dhaval Giani354d60c2008-04-19 19:44:59 +02007755#ifdef CONFIG_CGROUP_SCHED
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02007756 init_tg_rt_entry(&init_task_group, &rq->rt, NULL, i, 1, NULL);
Dhaval Giani354d60c2008-04-19 19:44:59 +02007757#endif
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007758#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07007759
Ingo Molnardd41f592007-07-09 18:51:59 +02007760 for (j = 0; j < CPU_LOAD_IDX_MAX; j++)
7761 rq->cpu_load[j] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007762#ifdef CONFIG_SMP
Nick Piggin41c7ce92005-06-25 14:57:24 -07007763 rq->sd = NULL;
Gregory Haskins57d885f2008-01-25 21:08:18 +01007764 rq->rd = NULL;
Gregory Haskins3f029d32009-07-29 11:08:47 -04007765 rq->post_schedule = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007766 rq->active_balance = 0;
Ingo Molnardd41f592007-07-09 18:51:59 +02007767 rq->next_balance = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007768 rq->push_cpu = 0;
Christoph Lameter0a2966b2006-09-25 23:30:51 -07007769 rq->cpu = i;
Gregory Haskins1f11eb62008-06-04 15:04:05 -04007770 rq->online = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007771 rq->migration_thread = NULL;
Mike Galbraitheae0c9d2009-11-10 03:50:02 +01007772 rq->idle_stamp = 0;
7773 rq->avg_idle = 2*sysctl_sched_migration_cost;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007774 INIT_LIST_HEAD(&rq->migration_queue);
Gregory Haskinsdc938522008-01-25 21:08:26 +01007775 rq_attach_root(rq, &def_root_domain);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007776#endif
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01007777 init_rq_hrtick(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007778 atomic_set(&rq->nr_iowait, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007779 }
7780
Peter Williams2dd73a42006-06-27 02:54:34 -07007781 set_load_weight(&init_task);
Heiko Carstensb50f60c2006-07-30 03:03:52 -07007782
Avi Kivitye107be32007-07-26 13:40:43 +02007783#ifdef CONFIG_PREEMPT_NOTIFIERS
7784 INIT_HLIST_HEAD(&init_task.preempt_notifiers);
7785#endif
7786
Christoph Lameterc9819f42006-12-10 02:20:25 -08007787#ifdef CONFIG_SMP
Carlos R. Mafra962cf362008-05-15 11:15:37 -03007788 open_softirq(SCHED_SOFTIRQ, run_rebalance_domains);
Christoph Lameterc9819f42006-12-10 02:20:25 -08007789#endif
7790
Heiko Carstensb50f60c2006-07-30 03:03:52 -07007791#ifdef CONFIG_RT_MUTEXES
Thomas Gleixner1d615482009-11-17 14:54:03 +01007792 plist_head_init_raw(&init_task.pi_waiters, &init_task.pi_lock);
Heiko Carstensb50f60c2006-07-30 03:03:52 -07007793#endif
7794
Linus Torvalds1da177e2005-04-16 15:20:36 -07007795 /*
7796 * The boot idle thread does lazy MMU switching as well:
7797 */
7798 atomic_inc(&init_mm.mm_count);
7799 enter_lazy_tlb(&init_mm, current);
7800
7801 /*
7802 * Make us the idle thread. Technically, schedule() should not be
7803 * called from this thread, however somewhere below it might be,
7804 * but because we are the idle thread, we just pick up running again
7805 * when this runqueue becomes "idle".
7806 */
7807 init_idle(current, smp_processor_id());
Thomas Gleixnerdce48a82009-04-11 10:43:41 +02007808
7809 calc_load_update = jiffies + LOAD_FREQ;
7810
Ingo Molnardd41f592007-07-09 18:51:59 +02007811 /*
7812 * During early bootup we pretend to be a normal task:
7813 */
7814 current->sched_class = &fair_sched_class;
Ingo Molnar6892b752008-02-13 14:02:36 +01007815
Rusty Russell6a7b3dc2008-11-25 02:35:04 +10307816 /* Allocate the nohz_cpu_mask if CONFIG_CPUMASK_OFFSTACK */
Rusty Russell49557e62009-11-02 20:37:20 +10307817 zalloc_cpumask_var(&nohz_cpu_mask, GFP_NOWAIT);
Rusty Russellbf4d83f2008-11-25 09:57:51 +10307818#ifdef CONFIG_SMP
Rusty Russell7d1e6a92008-11-25 02:35:09 +10307819#ifdef CONFIG_NO_HZ
Rusty Russell49557e62009-11-02 20:37:20 +10307820 zalloc_cpumask_var(&nohz.cpu_mask, GFP_NOWAIT);
Pekka Enberg4bdddf82009-06-11 08:35:27 +03007821 alloc_cpumask_var(&nohz.ilb_grp_nohz_mask, GFP_NOWAIT);
Rusty Russell7d1e6a92008-11-25 02:35:09 +10307822#endif
Rusty Russellbdddd292009-12-02 14:09:16 +10307823 /* May be allocated at isolcpus cmdline parse time */
7824 if (cpu_isolated_map == NULL)
7825 zalloc_cpumask_var(&cpu_isolated_map, GFP_NOWAIT);
Rusty Russellbf4d83f2008-11-25 09:57:51 +10307826#endif /* SMP */
Rusty Russell6a7b3dc2008-11-25 02:35:04 +10307827
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007828 perf_event_init();
Ingo Molnar0d905bc2009-05-04 19:13:30 +02007829
Ingo Molnar6892b752008-02-13 14:02:36 +01007830 scheduler_running = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007831}
7832
7833#ifdef CONFIG_DEBUG_SPINLOCK_SLEEP
Frederic Weisbeckere4aafea2009-07-16 15:44:29 +02007834static inline int preempt_count_equals(int preempt_offset)
7835{
Frederic Weisbecker234da7b2009-12-16 20:21:05 +01007836 int nested = (preempt_count() & ~PREEMPT_ACTIVE) + rcu_preempt_depth();
Frederic Weisbeckere4aafea2009-07-16 15:44:29 +02007837
7838 return (nested == PREEMPT_INATOMIC_BASE + preempt_offset);
7839}
7840
Simon Kagstromd8948372009-12-23 11:08:18 +01007841void __might_sleep(const char *file, int line, int preempt_offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007842{
Ingo Molnar48f24c42006-07-03 00:25:40 -07007843#ifdef in_atomic
Linus Torvalds1da177e2005-04-16 15:20:36 -07007844 static unsigned long prev_jiffy; /* ratelimiting */
7845
Frederic Weisbeckere4aafea2009-07-16 15:44:29 +02007846 if ((preempt_count_equals(preempt_offset) && !irqs_disabled()) ||
7847 system_state != SYSTEM_RUNNING || oops_in_progress)
Ingo Molnaraef745f2008-08-28 11:34:43 +02007848 return;
7849 if (time_before(jiffies, prev_jiffy + HZ) && prev_jiffy)
7850 return;
7851 prev_jiffy = jiffies;
7852
Peter Zijlstra3df0fc52009-12-20 14:23:57 +01007853 printk(KERN_ERR
7854 "BUG: sleeping function called from invalid context at %s:%d\n",
7855 file, line);
7856 printk(KERN_ERR
7857 "in_atomic(): %d, irqs_disabled(): %d, pid: %d, name: %s\n",
7858 in_atomic(), irqs_disabled(),
7859 current->pid, current->comm);
Ingo Molnaraef745f2008-08-28 11:34:43 +02007860
7861 debug_show_held_locks(current);
7862 if (irqs_disabled())
7863 print_irqtrace_events(current);
7864 dump_stack();
Linus Torvalds1da177e2005-04-16 15:20:36 -07007865#endif
7866}
7867EXPORT_SYMBOL(__might_sleep);
7868#endif
7869
7870#ifdef CONFIG_MAGIC_SYSRQ
Andi Kleen3a5e4dc2007-10-15 17:00:15 +02007871static void normalize_task(struct rq *rq, struct task_struct *p)
7872{
7873 int on_rq;
Peter Zijlstra3e51f332008-05-03 18:29:28 +02007874
Andi Kleen3a5e4dc2007-10-15 17:00:15 +02007875 update_rq_clock(rq);
7876 on_rq = p->se.on_rq;
7877 if (on_rq)
7878 deactivate_task(rq, p, 0);
7879 __setscheduler(rq, p, SCHED_NORMAL, 0);
7880 if (on_rq) {
7881 activate_task(rq, p, 0);
7882 resched_task(rq->curr);
7883 }
7884}
7885
Linus Torvalds1da177e2005-04-16 15:20:36 -07007886void normalize_rt_tasks(void)
7887{
Ingo Molnara0f98a12007-06-17 18:37:45 +02007888 struct task_struct *g, *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007889 unsigned long flags;
Ingo Molnar70b97a72006-07-03 00:25:42 -07007890 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007891
Peter Zijlstra4cf5d772008-02-13 15:45:39 +01007892 read_lock_irqsave(&tasklist_lock, flags);
Ingo Molnara0f98a12007-06-17 18:37:45 +02007893 do_each_thread(g, p) {
Ingo Molnar178be792007-10-15 17:00:18 +02007894 /*
7895 * Only normalize user tasks:
7896 */
7897 if (!p->mm)
7898 continue;
7899
Ingo Molnardd41f592007-07-09 18:51:59 +02007900 p->se.exec_start = 0;
Ingo Molnar6cfb0d52007-08-02 17:41:40 +02007901#ifdef CONFIG_SCHEDSTATS
7902 p->se.wait_start = 0;
7903 p->se.sleep_start = 0;
Ingo Molnardd41f592007-07-09 18:51:59 +02007904 p->se.block_start = 0;
Ingo Molnar6cfb0d52007-08-02 17:41:40 +02007905#endif
Ingo Molnardd41f592007-07-09 18:51:59 +02007906
7907 if (!rt_task(p)) {
7908 /*
7909 * Renice negative nice level userspace
7910 * tasks back to 0:
7911 */
7912 if (TASK_NICE(p) < 0 && p->mm)
7913 set_user_nice(p, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007914 continue;
Ingo Molnardd41f592007-07-09 18:51:59 +02007915 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007916
Thomas Gleixner1d615482009-11-17 14:54:03 +01007917 raw_spin_lock(&p->pi_lock);
Ingo Molnarb29739f2006-06-27 02:54:51 -07007918 rq = __task_rq_lock(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007919
Ingo Molnar178be792007-10-15 17:00:18 +02007920 normalize_task(rq, p);
Andi Kleen3a5e4dc2007-10-15 17:00:15 +02007921
Ingo Molnarb29739f2006-06-27 02:54:51 -07007922 __task_rq_unlock(rq);
Thomas Gleixner1d615482009-11-17 14:54:03 +01007923 raw_spin_unlock(&p->pi_lock);
Ingo Molnara0f98a12007-06-17 18:37:45 +02007924 } while_each_thread(g, p);
7925
Peter Zijlstra4cf5d772008-02-13 15:45:39 +01007926 read_unlock_irqrestore(&tasklist_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007927}
7928
7929#endif /* CONFIG_MAGIC_SYSRQ */
Linus Torvalds1df5c102005-09-12 07:59:21 -07007930
7931#ifdef CONFIG_IA64
7932/*
7933 * These functions are only useful for the IA64 MCA handling.
7934 *
7935 * They can only be called when the whole system has been
7936 * stopped - every CPU needs to be quiescent, and no scheduling
7937 * activity can take place. Using them for anything else would
7938 * be a serious bug, and as a result, they aren't even visible
7939 * under any other configuration.
7940 */
7941
7942/**
7943 * curr_task - return the current task for a given cpu.
7944 * @cpu: the processor in question.
7945 *
7946 * ONLY VALID WHEN THE WHOLE SYSTEM IS STOPPED!
7947 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07007948struct task_struct *curr_task(int cpu)
Linus Torvalds1df5c102005-09-12 07:59:21 -07007949{
7950 return cpu_curr(cpu);
7951}
7952
7953/**
7954 * set_curr_task - set the current task for a given cpu.
7955 * @cpu: the processor in question.
7956 * @p: the task pointer to set.
7957 *
7958 * Description: This function must only be used when non-maskable interrupts
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01007959 * are serviced on a separate stack. It allows the architecture to switch the
7960 * notion of the current task on a cpu in a non-blocking manner. This function
Linus Torvalds1df5c102005-09-12 07:59:21 -07007961 * must be called with all CPU's synchronized, and interrupts disabled, the
7962 * and caller must save the original value of the current task (see
7963 * curr_task() above) and restore that value before reenabling interrupts and
7964 * re-starting the system.
7965 *
7966 * ONLY VALID WHEN THE WHOLE SYSTEM IS STOPPED!
7967 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07007968void set_curr_task(int cpu, struct task_struct *p)
Linus Torvalds1df5c102005-09-12 07:59:21 -07007969{
7970 cpu_curr(cpu) = p;
7971}
7972
7973#endif
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007974
Peter Zijlstrabccbe082008-02-13 15:45:40 +01007975#ifdef CONFIG_FAIR_GROUP_SCHED
7976static void free_fair_sched_group(struct task_group *tg)
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007977{
7978 int i;
7979
7980 for_each_possible_cpu(i) {
7981 if (tg->cfs_rq)
7982 kfree(tg->cfs_rq[i]);
7983 if (tg->se)
7984 kfree(tg->se[i]);
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007985 }
7986
7987 kfree(tg->cfs_rq);
7988 kfree(tg->se);
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007989}
7990
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02007991static
7992int alloc_fair_sched_group(struct task_group *tg, struct task_group *parent)
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007993{
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007994 struct cfs_rq *cfs_rq;
Li Zefaneab17222008-10-29 17:03:22 +08007995 struct sched_entity *se;
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02007996 struct rq *rq;
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007997 int i;
7998
Mike Travis434d53b2008-04-04 18:11:04 -07007999 tg->cfs_rq = kzalloc(sizeof(cfs_rq) * nr_cpu_ids, GFP_KERNEL);
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008000 if (!tg->cfs_rq)
8001 goto err;
Mike Travis434d53b2008-04-04 18:11:04 -07008002 tg->se = kzalloc(sizeof(se) * nr_cpu_ids, GFP_KERNEL);
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008003 if (!tg->se)
8004 goto err;
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01008005
8006 tg->shares = NICE_0_LOAD;
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008007
8008 for_each_possible_cpu(i) {
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02008009 rq = cpu_rq(i);
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008010
Li Zefaneab17222008-10-29 17:03:22 +08008011 cfs_rq = kzalloc_node(sizeof(struct cfs_rq),
8012 GFP_KERNEL, cpu_to_node(i));
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008013 if (!cfs_rq)
8014 goto err;
8015
Li Zefaneab17222008-10-29 17:03:22 +08008016 se = kzalloc_node(sizeof(struct sched_entity),
8017 GFP_KERNEL, cpu_to_node(i));
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008018 if (!se)
Phil Carmodydfc12eb2009-12-10 14:29:37 +02008019 goto err_free_rq;
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008020
Li Zefaneab17222008-10-29 17:03:22 +08008021 init_tg_cfs_entry(tg, cfs_rq, se, i, 0, parent->se[i]);
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008022 }
8023
8024 return 1;
8025
Phil Carmodydfc12eb2009-12-10 14:29:37 +02008026 err_free_rq:
8027 kfree(cfs_rq);
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008028 err:
8029 return 0;
8030}
8031
8032static inline void register_fair_sched_group(struct task_group *tg, int cpu)
8033{
8034 list_add_rcu(&tg->cfs_rq[cpu]->leaf_cfs_rq_list,
8035 &cpu_rq(cpu)->leaf_cfs_rq_list);
8036}
8037
8038static inline void unregister_fair_sched_group(struct task_group *tg, int cpu)
8039{
8040 list_del_rcu(&tg->cfs_rq[cpu]->leaf_cfs_rq_list);
8041}
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +02008042#else /* !CONFG_FAIR_GROUP_SCHED */
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008043static inline void free_fair_sched_group(struct task_group *tg)
8044{
8045}
8046
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008047static inline
8048int alloc_fair_sched_group(struct task_group *tg, struct task_group *parent)
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008049{
8050 return 1;
8051}
8052
8053static inline void register_fair_sched_group(struct task_group *tg, int cpu)
8054{
8055}
8056
8057static inline void unregister_fair_sched_group(struct task_group *tg, int cpu)
8058{
8059}
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +02008060#endif /* CONFIG_FAIR_GROUP_SCHED */
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01008061
8062#ifdef CONFIG_RT_GROUP_SCHED
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008063static void free_rt_sched_group(struct task_group *tg)
8064{
8065 int i;
8066
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008067 destroy_rt_bandwidth(&tg->rt_bandwidth);
8068
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008069 for_each_possible_cpu(i) {
8070 if (tg->rt_rq)
8071 kfree(tg->rt_rq[i]);
8072 if (tg->rt_se)
8073 kfree(tg->rt_se[i]);
8074 }
8075
8076 kfree(tg->rt_rq);
8077 kfree(tg->rt_se);
8078}
8079
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008080static
8081int alloc_rt_sched_group(struct task_group *tg, struct task_group *parent)
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008082{
8083 struct rt_rq *rt_rq;
Li Zefaneab17222008-10-29 17:03:22 +08008084 struct sched_rt_entity *rt_se;
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008085 struct rq *rq;
8086 int i;
8087
Mike Travis434d53b2008-04-04 18:11:04 -07008088 tg->rt_rq = kzalloc(sizeof(rt_rq) * nr_cpu_ids, GFP_KERNEL);
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008089 if (!tg->rt_rq)
8090 goto err;
Mike Travis434d53b2008-04-04 18:11:04 -07008091 tg->rt_se = kzalloc(sizeof(rt_se) * nr_cpu_ids, GFP_KERNEL);
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008092 if (!tg->rt_se)
8093 goto err;
8094
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008095 init_rt_bandwidth(&tg->rt_bandwidth,
8096 ktime_to_ns(def_rt_bandwidth.rt_period), 0);
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008097
8098 for_each_possible_cpu(i) {
8099 rq = cpu_rq(i);
8100
Li Zefaneab17222008-10-29 17:03:22 +08008101 rt_rq = kzalloc_node(sizeof(struct rt_rq),
8102 GFP_KERNEL, cpu_to_node(i));
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008103 if (!rt_rq)
8104 goto err;
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008105
Li Zefaneab17222008-10-29 17:03:22 +08008106 rt_se = kzalloc_node(sizeof(struct sched_rt_entity),
8107 GFP_KERNEL, cpu_to_node(i));
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008108 if (!rt_se)
Phil Carmodydfc12eb2009-12-10 14:29:37 +02008109 goto err_free_rq;
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008110
Li Zefaneab17222008-10-29 17:03:22 +08008111 init_tg_rt_entry(tg, rt_rq, rt_se, i, 0, parent->rt_se[i]);
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008112 }
8113
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008114 return 1;
8115
Phil Carmodydfc12eb2009-12-10 14:29:37 +02008116 err_free_rq:
8117 kfree(rt_rq);
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008118 err:
8119 return 0;
8120}
8121
8122static inline void register_rt_sched_group(struct task_group *tg, int cpu)
8123{
8124 list_add_rcu(&tg->rt_rq[cpu]->leaf_rt_rq_list,
8125 &cpu_rq(cpu)->leaf_rt_rq_list);
8126}
8127
8128static inline void unregister_rt_sched_group(struct task_group *tg, int cpu)
8129{
8130 list_del_rcu(&tg->rt_rq[cpu]->leaf_rt_rq_list);
8131}
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +02008132#else /* !CONFIG_RT_GROUP_SCHED */
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008133static inline void free_rt_sched_group(struct task_group *tg)
8134{
8135}
8136
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008137static inline
8138int alloc_rt_sched_group(struct task_group *tg, struct task_group *parent)
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008139{
8140 return 1;
8141}
8142
8143static inline void register_rt_sched_group(struct task_group *tg, int cpu)
8144{
8145}
8146
8147static inline void unregister_rt_sched_group(struct task_group *tg, int cpu)
8148{
8149}
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +02008150#endif /* CONFIG_RT_GROUP_SCHED */
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008151
Dhaval Giani7c941432010-01-20 13:26:18 +01008152#ifdef CONFIG_CGROUP_SCHED
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008153static void free_sched_group(struct task_group *tg)
8154{
8155 free_fair_sched_group(tg);
8156 free_rt_sched_group(tg);
8157 kfree(tg);
8158}
8159
8160/* allocate runqueue etc for a new task group */
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008161struct task_group *sched_create_group(struct task_group *parent)
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008162{
8163 struct task_group *tg;
8164 unsigned long flags;
8165 int i;
8166
8167 tg = kzalloc(sizeof(*tg), GFP_KERNEL);
8168 if (!tg)
8169 return ERR_PTR(-ENOMEM);
8170
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008171 if (!alloc_fair_sched_group(tg, parent))
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008172 goto err;
8173
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008174 if (!alloc_rt_sched_group(tg, parent))
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008175 goto err;
8176
Peter Zijlstra8ed36992008-02-13 15:45:39 +01008177 spin_lock_irqsave(&task_group_lock, flags);
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02008178 for_each_possible_cpu(i) {
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008179 register_fair_sched_group(tg, i);
8180 register_rt_sched_group(tg, i);
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02008181 }
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008182 list_add_rcu(&tg->list, &task_groups);
Peter Zijlstraf473aa52008-04-19 19:45:00 +02008183
8184 WARN_ON(!parent); /* root should already exist */
8185
8186 tg->parent = parent;
Peter Zijlstraf473aa52008-04-19 19:45:00 +02008187 INIT_LIST_HEAD(&tg->children);
Zhang, Yanmin09f27242030-08-14 15:56:40 +08008188 list_add_rcu(&tg->siblings, &parent->children);
Peter Zijlstra8ed36992008-02-13 15:45:39 +01008189 spin_unlock_irqrestore(&task_group_lock, flags);
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008190
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02008191 return tg;
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008192
8193err:
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008194 free_sched_group(tg);
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008195 return ERR_PTR(-ENOMEM);
8196}
8197
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02008198/* rcu callback to free various structures associated with a task group */
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008199static void free_sched_group_rcu(struct rcu_head *rhp)
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008200{
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008201 /* now it should be safe to free those cfs_rqs */
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008202 free_sched_group(container_of(rhp, struct task_group, rcu));
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008203}
8204
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02008205/* Destroy runqueue etc associated with a task group */
Ingo Molnar4cf86d72007-10-15 17:00:14 +02008206void sched_destroy_group(struct task_group *tg)
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008207{
Peter Zijlstra8ed36992008-02-13 15:45:39 +01008208 unsigned long flags;
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02008209 int i;
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008210
Peter Zijlstra8ed36992008-02-13 15:45:39 +01008211 spin_lock_irqsave(&task_group_lock, flags);
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02008212 for_each_possible_cpu(i) {
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008213 unregister_fair_sched_group(tg, i);
8214 unregister_rt_sched_group(tg, i);
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02008215 }
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008216 list_del_rcu(&tg->list);
Peter Zijlstraf473aa52008-04-19 19:45:00 +02008217 list_del_rcu(&tg->siblings);
Peter Zijlstra8ed36992008-02-13 15:45:39 +01008218 spin_unlock_irqrestore(&task_group_lock, flags);
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02008219
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02008220 /* wait for possible concurrent references to cfs_rqs complete */
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008221 call_rcu(&tg->rcu, free_sched_group_rcu);
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008222}
8223
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02008224/* change task's runqueue when it moves between groups.
Ingo Molnar3a252012007-10-15 17:00:12 +02008225 * The caller of this function should have put the task in its new group
8226 * by now. This function just updates tsk->se.cfs_rq and tsk->se.parent to
8227 * reflect its new group.
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02008228 */
8229void sched_move_task(struct task_struct *tsk)
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008230{
8231 int on_rq, running;
8232 unsigned long flags;
8233 struct rq *rq;
8234
8235 rq = task_rq_lock(tsk, &flags);
8236
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008237 update_rq_clock(rq);
8238
Dmitry Adamushko051a1d12007-12-18 15:21:13 +01008239 running = task_current(rq, tsk);
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008240 on_rq = tsk->se.on_rq;
8241
Hiroshi Shimamoto0e1f3482008-03-10 11:01:20 -07008242 if (on_rq)
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008243 dequeue_task(rq, tsk, 0);
Hiroshi Shimamoto0e1f3482008-03-10 11:01:20 -07008244 if (unlikely(running))
8245 tsk->sched_class->put_prev_task(rq, tsk);
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008246
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008247 set_task_rq(tsk, task_cpu(tsk));
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008248
Peter Zijlstra810b3812008-02-29 15:21:01 -05008249#ifdef CONFIG_FAIR_GROUP_SCHED
8250 if (tsk->sched_class->moved_group)
Peter Zijlstra88ec22d2009-12-16 18:04:41 +01008251 tsk->sched_class->moved_group(tsk, on_rq);
Peter Zijlstra810b3812008-02-29 15:21:01 -05008252#endif
8253
Hiroshi Shimamoto0e1f3482008-03-10 11:01:20 -07008254 if (unlikely(running))
8255 tsk->sched_class->set_curr_task(rq);
8256 if (on_rq)
Thomas Gleixnerea87bb72010-01-20 20:58:57 +00008257 enqueue_task(rq, tsk, 0, false);
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008258
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008259 task_rq_unlock(rq, &flags);
8260}
Dhaval Giani7c941432010-01-20 13:26:18 +01008261#endif /* CONFIG_CGROUP_SCHED */
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008262
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01008263#ifdef CONFIG_FAIR_GROUP_SCHED
Peter Zijlstrac09595f2008-06-27 13:41:14 +02008264static void __set_se_shares(struct sched_entity *se, unsigned long shares)
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008265{
8266 struct cfs_rq *cfs_rq = se->cfs_rq;
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008267 int on_rq;
8268
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008269 on_rq = se->on_rq;
Peter Zijlstra62fb1852008-02-25 17:34:02 +01008270 if (on_rq)
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008271 dequeue_entity(cfs_rq, se, 0);
8272
8273 se->load.weight = shares;
Peter Zijlstrae05510d2008-05-05 23:56:17 +02008274 se->load.inv_weight = 0;
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008275
Peter Zijlstra62fb1852008-02-25 17:34:02 +01008276 if (on_rq)
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008277 enqueue_entity(cfs_rq, se, 0);
Peter Zijlstrac09595f2008-06-27 13:41:14 +02008278}
Peter Zijlstra62fb1852008-02-25 17:34:02 +01008279
Peter Zijlstrac09595f2008-06-27 13:41:14 +02008280static void set_se_shares(struct sched_entity *se, unsigned long shares)
8281{
8282 struct cfs_rq *cfs_rq = se->cfs_rq;
8283 struct rq *rq = cfs_rq->rq;
8284 unsigned long flags;
8285
Thomas Gleixner05fa7852009-11-17 14:28:38 +01008286 raw_spin_lock_irqsave(&rq->lock, flags);
Peter Zijlstrac09595f2008-06-27 13:41:14 +02008287 __set_se_shares(se, shares);
Thomas Gleixner05fa7852009-11-17 14:28:38 +01008288 raw_spin_unlock_irqrestore(&rq->lock, flags);
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008289}
8290
Peter Zijlstra8ed36992008-02-13 15:45:39 +01008291static DEFINE_MUTEX(shares_mutex);
8292
Ingo Molnar4cf86d72007-10-15 17:00:14 +02008293int sched_group_set_shares(struct task_group *tg, unsigned long shares)
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008294{
8295 int i;
Peter Zijlstra8ed36992008-02-13 15:45:39 +01008296 unsigned long flags;
Ingo Molnarc61935f2008-01-22 11:24:58 +01008297
Peter Zijlstra62fb1852008-02-25 17:34:02 +01008298 /*
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008299 * We can't change the weight of the root cgroup.
8300 */
8301 if (!tg->se[0])
8302 return -EINVAL;
8303
Peter Zijlstra18d95a22008-04-19 19:45:00 +02008304 if (shares < MIN_SHARES)
8305 shares = MIN_SHARES;
Miao Xiecb4ad1f2008-04-28 12:54:56 +08008306 else if (shares > MAX_SHARES)
8307 shares = MAX_SHARES;
Peter Zijlstra62fb1852008-02-25 17:34:02 +01008308
Peter Zijlstra8ed36992008-02-13 15:45:39 +01008309 mutex_lock(&shares_mutex);
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02008310 if (tg->shares == shares)
Dhaval Giani5cb350b2007-10-15 17:00:14 +02008311 goto done;
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008312
Peter Zijlstra8ed36992008-02-13 15:45:39 +01008313 spin_lock_irqsave(&task_group_lock, flags);
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008314 for_each_possible_cpu(i)
8315 unregister_fair_sched_group(tg, i);
Peter Zijlstraf473aa52008-04-19 19:45:00 +02008316 list_del_rcu(&tg->siblings);
Peter Zijlstra8ed36992008-02-13 15:45:39 +01008317 spin_unlock_irqrestore(&task_group_lock, flags);
Srivatsa Vaddagiri6b2d7702008-01-25 21:08:00 +01008318
8319 /* wait for any ongoing reference to this group to finish */
8320 synchronize_sched();
8321
8322 /*
8323 * Now we are free to modify the group's share on each cpu
8324 * w/o tripping rebalance_share or load_balance_fair.
8325 */
8326 tg->shares = shares;
Peter Zijlstrac09595f2008-06-27 13:41:14 +02008327 for_each_possible_cpu(i) {
8328 /*
8329 * force a rebalance
8330 */
8331 cfs_rq_set_shares(tg->cfs_rq[i], 0);
Miao Xiecb4ad1f2008-04-28 12:54:56 +08008332 set_se_shares(tg->se[i], shares);
Peter Zijlstrac09595f2008-06-27 13:41:14 +02008333 }
Srivatsa Vaddagiri6b2d7702008-01-25 21:08:00 +01008334
8335 /*
8336 * Enable load balance activity on this group, by inserting it back on
8337 * each cpu's rq->leaf_cfs_rq_list.
8338 */
Peter Zijlstra8ed36992008-02-13 15:45:39 +01008339 spin_lock_irqsave(&task_group_lock, flags);
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008340 for_each_possible_cpu(i)
8341 register_fair_sched_group(tg, i);
Peter Zijlstraf473aa52008-04-19 19:45:00 +02008342 list_add_rcu(&tg->siblings, &tg->parent->children);
Peter Zijlstra8ed36992008-02-13 15:45:39 +01008343 spin_unlock_irqrestore(&task_group_lock, flags);
Dhaval Giani5cb350b2007-10-15 17:00:14 +02008344done:
Peter Zijlstra8ed36992008-02-13 15:45:39 +01008345 mutex_unlock(&shares_mutex);
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02008346 return 0;
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008347}
8348
Dhaval Giani5cb350b2007-10-15 17:00:14 +02008349unsigned long sched_group_shares(struct task_group *tg)
8350{
8351 return tg->shares;
8352}
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01008353#endif
Dhaval Giani5cb350b2007-10-15 17:00:14 +02008354
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01008355#ifdef CONFIG_RT_GROUP_SCHED
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008356/*
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01008357 * Ensure that the real time constraints are schedulable.
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008358 */
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01008359static DEFINE_MUTEX(rt_constraints_mutex);
8360
8361static unsigned long to_ratio(u64 period, u64 runtime)
8362{
8363 if (runtime == RUNTIME_INF)
Peter Zijlstra9a7e0b12008-08-19 12:33:06 +02008364 return 1ULL << 20;
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01008365
Peter Zijlstra9a7e0b12008-08-19 12:33:06 +02008366 return div64_u64(runtime << 20, period);
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01008367}
8368
Dhaval Giani521f1a242008-02-28 15:21:56 +05308369/* Must be called with tasklist_lock held */
8370static inline int tg_has_rt_tasks(struct task_group *tg)
8371{
8372 struct task_struct *g, *p;
Peter Zijlstra9a7e0b12008-08-19 12:33:06 +02008373
Dhaval Giani521f1a242008-02-28 15:21:56 +05308374 do_each_thread(g, p) {
8375 if (rt_task(p) && rt_rq_of_se(&p->rt)->tg == tg)
8376 return 1;
8377 } while_each_thread(g, p);
Peter Zijlstra9a7e0b12008-08-19 12:33:06 +02008378
Dhaval Giani521f1a242008-02-28 15:21:56 +05308379 return 0;
8380}
8381
Peter Zijlstra9a7e0b12008-08-19 12:33:06 +02008382struct rt_schedulable_data {
8383 struct task_group *tg;
8384 u64 rt_period;
8385 u64 rt_runtime;
8386};
8387
8388static int tg_schedulable(struct task_group *tg, void *data)
8389{
8390 struct rt_schedulable_data *d = data;
8391 struct task_group *child;
8392 unsigned long total, sum = 0;
8393 u64 period, runtime;
8394
8395 period = ktime_to_ns(tg->rt_bandwidth.rt_period);
8396 runtime = tg->rt_bandwidth.rt_runtime;
8397
8398 if (tg == d->tg) {
8399 period = d->rt_period;
8400 runtime = d->rt_runtime;
8401 }
8402
Peter Zijlstra4653f802008-09-23 15:33:44 +02008403 /*
8404 * Cannot have more runtime than the period.
8405 */
8406 if (runtime > period && runtime != RUNTIME_INF)
8407 return -EINVAL;
8408
8409 /*
8410 * Ensure we don't starve existing RT tasks.
8411 */
Peter Zijlstra9a7e0b12008-08-19 12:33:06 +02008412 if (rt_bandwidth_enabled() && !runtime && tg_has_rt_tasks(tg))
8413 return -EBUSY;
8414
8415 total = to_ratio(period, runtime);
8416
Peter Zijlstra4653f802008-09-23 15:33:44 +02008417 /*
8418 * Nobody can have more than the global setting allows.
8419 */
8420 if (total > to_ratio(global_rt_period(), global_rt_runtime()))
8421 return -EINVAL;
8422
8423 /*
8424 * The sum of our children's runtime should not exceed our own.
8425 */
Peter Zijlstra9a7e0b12008-08-19 12:33:06 +02008426 list_for_each_entry_rcu(child, &tg->children, siblings) {
8427 period = ktime_to_ns(child->rt_bandwidth.rt_period);
8428 runtime = child->rt_bandwidth.rt_runtime;
8429
8430 if (child == d->tg) {
8431 period = d->rt_period;
8432 runtime = d->rt_runtime;
8433 }
8434
8435 sum += to_ratio(period, runtime);
8436 }
8437
8438 if (sum > total)
8439 return -EINVAL;
8440
8441 return 0;
8442}
8443
8444static int __rt_schedulable(struct task_group *tg, u64 period, u64 runtime)
8445{
8446 struct rt_schedulable_data data = {
8447 .tg = tg,
8448 .rt_period = period,
8449 .rt_runtime = runtime,
8450 };
8451
8452 return walk_tg_tree(tg_schedulable, tg_nop, &data);
8453}
8454
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008455static int tg_set_bandwidth(struct task_group *tg,
8456 u64 rt_period, u64 rt_runtime)
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008457{
Peter Zijlstraac086bc2008-04-19 19:44:58 +02008458 int i, err = 0;
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01008459
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01008460 mutex_lock(&rt_constraints_mutex);
Dhaval Giani521f1a242008-02-28 15:21:56 +05308461 read_lock(&tasklist_lock);
Peter Zijlstra9a7e0b12008-08-19 12:33:06 +02008462 err = __rt_schedulable(tg, rt_period, rt_runtime);
8463 if (err)
Dhaval Giani521f1a242008-02-28 15:21:56 +05308464 goto unlock;
Peter Zijlstraac086bc2008-04-19 19:44:58 +02008465
Thomas Gleixner0986b112009-11-17 15:32:06 +01008466 raw_spin_lock_irq(&tg->rt_bandwidth.rt_runtime_lock);
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008467 tg->rt_bandwidth.rt_period = ns_to_ktime(rt_period);
8468 tg->rt_bandwidth.rt_runtime = rt_runtime;
Peter Zijlstraac086bc2008-04-19 19:44:58 +02008469
8470 for_each_possible_cpu(i) {
8471 struct rt_rq *rt_rq = tg->rt_rq[i];
8472
Thomas Gleixner0986b112009-11-17 15:32:06 +01008473 raw_spin_lock(&rt_rq->rt_runtime_lock);
Peter Zijlstraac086bc2008-04-19 19:44:58 +02008474 rt_rq->rt_runtime = rt_runtime;
Thomas Gleixner0986b112009-11-17 15:32:06 +01008475 raw_spin_unlock(&rt_rq->rt_runtime_lock);
Peter Zijlstraac086bc2008-04-19 19:44:58 +02008476 }
Thomas Gleixner0986b112009-11-17 15:32:06 +01008477 raw_spin_unlock_irq(&tg->rt_bandwidth.rt_runtime_lock);
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01008478 unlock:
Dhaval Giani521f1a242008-02-28 15:21:56 +05308479 read_unlock(&tasklist_lock);
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01008480 mutex_unlock(&rt_constraints_mutex);
8481
8482 return err;
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008483}
8484
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008485int sched_group_set_rt_runtime(struct task_group *tg, long rt_runtime_us)
8486{
8487 u64 rt_runtime, rt_period;
8488
8489 rt_period = ktime_to_ns(tg->rt_bandwidth.rt_period);
8490 rt_runtime = (u64)rt_runtime_us * NSEC_PER_USEC;
8491 if (rt_runtime_us < 0)
8492 rt_runtime = RUNTIME_INF;
8493
8494 return tg_set_bandwidth(tg, rt_period, rt_runtime);
8495}
8496
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01008497long sched_group_rt_runtime(struct task_group *tg)
8498{
8499 u64 rt_runtime_us;
8500
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008501 if (tg->rt_bandwidth.rt_runtime == RUNTIME_INF)
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01008502 return -1;
8503
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008504 rt_runtime_us = tg->rt_bandwidth.rt_runtime;
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01008505 do_div(rt_runtime_us, NSEC_PER_USEC);
8506 return rt_runtime_us;
8507}
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008508
8509int sched_group_set_rt_period(struct task_group *tg, long rt_period_us)
8510{
8511 u64 rt_runtime, rt_period;
8512
8513 rt_period = (u64)rt_period_us * NSEC_PER_USEC;
8514 rt_runtime = tg->rt_bandwidth.rt_runtime;
8515
Raistlin619b0482008-06-26 18:54:09 +02008516 if (rt_period == 0)
8517 return -EINVAL;
8518
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008519 return tg_set_bandwidth(tg, rt_period, rt_runtime);
8520}
8521
8522long sched_group_rt_period(struct task_group *tg)
8523{
8524 u64 rt_period_us;
8525
8526 rt_period_us = ktime_to_ns(tg->rt_bandwidth.rt_period);
8527 do_div(rt_period_us, NSEC_PER_USEC);
8528 return rt_period_us;
8529}
8530
8531static int sched_rt_global_constraints(void)
8532{
Peter Zijlstra4653f802008-09-23 15:33:44 +02008533 u64 runtime, period;
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008534 int ret = 0;
8535
Hiroshi Shimamotoec5d4982008-09-10 17:00:19 -07008536 if (sysctl_sched_rt_period <= 0)
8537 return -EINVAL;
8538
Peter Zijlstra4653f802008-09-23 15:33:44 +02008539 runtime = global_rt_runtime();
8540 period = global_rt_period();
8541
8542 /*
8543 * Sanity check on the sysctl variables.
8544 */
8545 if (runtime > period && runtime != RUNTIME_INF)
8546 return -EINVAL;
Peter Zijlstra10b612f2008-06-19 14:22:27 +02008547
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008548 mutex_lock(&rt_constraints_mutex);
Peter Zijlstra9a7e0b12008-08-19 12:33:06 +02008549 read_lock(&tasklist_lock);
Peter Zijlstra4653f802008-09-23 15:33:44 +02008550 ret = __rt_schedulable(NULL, 0, 0);
Peter Zijlstra9a7e0b12008-08-19 12:33:06 +02008551 read_unlock(&tasklist_lock);
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008552 mutex_unlock(&rt_constraints_mutex);
8553
8554 return ret;
8555}
Dhaval Giani54e99122009-02-27 15:13:54 +05308556
8557int sched_rt_can_attach(struct task_group *tg, struct task_struct *tsk)
8558{
8559 /* Don't accept realtime tasks when there is no way for them to run */
8560 if (rt_task(tsk) && tg->rt_bandwidth.rt_runtime == 0)
8561 return 0;
8562
8563 return 1;
8564}
8565
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +02008566#else /* !CONFIG_RT_GROUP_SCHED */
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008567static int sched_rt_global_constraints(void)
8568{
Peter Zijlstraac086bc2008-04-19 19:44:58 +02008569 unsigned long flags;
8570 int i;
8571
Hiroshi Shimamotoec5d4982008-09-10 17:00:19 -07008572 if (sysctl_sched_rt_period <= 0)
8573 return -EINVAL;
8574
Peter Zijlstra60aa6052009-05-05 17:50:21 +02008575 /*
8576 * There's always some RT tasks in the root group
8577 * -- migration, kstopmachine etc..
8578 */
8579 if (sysctl_sched_rt_runtime == 0)
8580 return -EBUSY;
8581
Thomas Gleixner0986b112009-11-17 15:32:06 +01008582 raw_spin_lock_irqsave(&def_rt_bandwidth.rt_runtime_lock, flags);
Peter Zijlstraac086bc2008-04-19 19:44:58 +02008583 for_each_possible_cpu(i) {
8584 struct rt_rq *rt_rq = &cpu_rq(i)->rt;
8585
Thomas Gleixner0986b112009-11-17 15:32:06 +01008586 raw_spin_lock(&rt_rq->rt_runtime_lock);
Peter Zijlstraac086bc2008-04-19 19:44:58 +02008587 rt_rq->rt_runtime = global_rt_runtime();
Thomas Gleixner0986b112009-11-17 15:32:06 +01008588 raw_spin_unlock(&rt_rq->rt_runtime_lock);
Peter Zijlstraac086bc2008-04-19 19:44:58 +02008589 }
Thomas Gleixner0986b112009-11-17 15:32:06 +01008590 raw_spin_unlock_irqrestore(&def_rt_bandwidth.rt_runtime_lock, flags);
Peter Zijlstraac086bc2008-04-19 19:44:58 +02008591
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008592 return 0;
8593}
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +02008594#endif /* CONFIG_RT_GROUP_SCHED */
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008595
8596int sched_rt_handler(struct ctl_table *table, int write,
Alexey Dobriyan8d65af72009-09-23 15:57:19 -07008597 void __user *buffer, size_t *lenp,
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008598 loff_t *ppos)
8599{
8600 int ret;
8601 int old_period, old_runtime;
8602 static DEFINE_MUTEX(mutex);
8603
8604 mutex_lock(&mutex);
8605 old_period = sysctl_sched_rt_period;
8606 old_runtime = sysctl_sched_rt_runtime;
8607
Alexey Dobriyan8d65af72009-09-23 15:57:19 -07008608 ret = proc_dointvec(table, write, buffer, lenp, ppos);
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008609
8610 if (!ret && write) {
8611 ret = sched_rt_global_constraints();
8612 if (ret) {
8613 sysctl_sched_rt_period = old_period;
8614 sysctl_sched_rt_runtime = old_runtime;
8615 } else {
8616 def_rt_bandwidth.rt_runtime = global_rt_runtime();
8617 def_rt_bandwidth.rt_period =
8618 ns_to_ktime(global_rt_period());
8619 }
8620 }
8621 mutex_unlock(&mutex);
8622
8623 return ret;
8624}
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008625
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01008626#ifdef CONFIG_CGROUP_SCHED
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008627
8628/* return corresponding task_group object of a cgroup */
Paul Menage2b01dfe2007-10-24 18:23:50 +02008629static inline struct task_group *cgroup_tg(struct cgroup *cgrp)
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008630{
Paul Menage2b01dfe2007-10-24 18:23:50 +02008631 return container_of(cgroup_subsys_state(cgrp, cpu_cgroup_subsys_id),
8632 struct task_group, css);
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008633}
8634
8635static struct cgroup_subsys_state *
Paul Menage2b01dfe2007-10-24 18:23:50 +02008636cpu_cgroup_create(struct cgroup_subsys *ss, struct cgroup *cgrp)
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008637{
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008638 struct task_group *tg, *parent;
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008639
Paul Menage2b01dfe2007-10-24 18:23:50 +02008640 if (!cgrp->parent) {
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008641 /* This is early initialization for the top cgroup */
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008642 return &init_task_group.css;
8643 }
8644
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008645 parent = cgroup_tg(cgrp->parent);
8646 tg = sched_create_group(parent);
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008647 if (IS_ERR(tg))
8648 return ERR_PTR(-ENOMEM);
8649
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008650 return &tg->css;
8651}
8652
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01008653static void
8654cpu_cgroup_destroy(struct cgroup_subsys *ss, struct cgroup *cgrp)
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008655{
Paul Menage2b01dfe2007-10-24 18:23:50 +02008656 struct task_group *tg = cgroup_tg(cgrp);
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008657
8658 sched_destroy_group(tg);
8659}
8660
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01008661static int
Ben Blumbe367d02009-09-23 15:56:31 -07008662cpu_cgroup_can_attach_task(struct cgroup *cgrp, struct task_struct *tsk)
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008663{
Peter Zijlstrab68aa232008-02-13 15:45:40 +01008664#ifdef CONFIG_RT_GROUP_SCHED
Dhaval Giani54e99122009-02-27 15:13:54 +05308665 if (!sched_rt_can_attach(cgroup_tg(cgrp), tsk))
Peter Zijlstrab68aa232008-02-13 15:45:40 +01008666 return -EINVAL;
8667#else
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008668 /* We don't support RT-tasks being in separate groups */
8669 if (tsk->sched_class != &fair_sched_class)
8670 return -EINVAL;
Peter Zijlstrab68aa232008-02-13 15:45:40 +01008671#endif
Ben Blumbe367d02009-09-23 15:56:31 -07008672 return 0;
8673}
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008674
Ben Blumbe367d02009-09-23 15:56:31 -07008675static int
8676cpu_cgroup_can_attach(struct cgroup_subsys *ss, struct cgroup *cgrp,
8677 struct task_struct *tsk, bool threadgroup)
8678{
8679 int retval = cpu_cgroup_can_attach_task(cgrp, tsk);
8680 if (retval)
8681 return retval;
8682 if (threadgroup) {
8683 struct task_struct *c;
8684 rcu_read_lock();
8685 list_for_each_entry_rcu(c, &tsk->thread_group, thread_group) {
8686 retval = cpu_cgroup_can_attach_task(cgrp, c);
8687 if (retval) {
8688 rcu_read_unlock();
8689 return retval;
8690 }
8691 }
8692 rcu_read_unlock();
8693 }
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008694 return 0;
8695}
8696
8697static void
Paul Menage2b01dfe2007-10-24 18:23:50 +02008698cpu_cgroup_attach(struct cgroup_subsys *ss, struct cgroup *cgrp,
Ben Blumbe367d02009-09-23 15:56:31 -07008699 struct cgroup *old_cont, struct task_struct *tsk,
8700 bool threadgroup)
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008701{
8702 sched_move_task(tsk);
Ben Blumbe367d02009-09-23 15:56:31 -07008703 if (threadgroup) {
8704 struct task_struct *c;
8705 rcu_read_lock();
8706 list_for_each_entry_rcu(c, &tsk->thread_group, thread_group) {
8707 sched_move_task(c);
8708 }
8709 rcu_read_unlock();
8710 }
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008711}
8712
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01008713#ifdef CONFIG_FAIR_GROUP_SCHED
Paul Menagef4c753b2008-04-29 00:59:56 -07008714static int cpu_shares_write_u64(struct cgroup *cgrp, struct cftype *cftype,
Paul Menage2b01dfe2007-10-24 18:23:50 +02008715 u64 shareval)
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008716{
Paul Menage2b01dfe2007-10-24 18:23:50 +02008717 return sched_group_set_shares(cgroup_tg(cgrp), shareval);
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008718}
8719
Paul Menagef4c753b2008-04-29 00:59:56 -07008720static u64 cpu_shares_read_u64(struct cgroup *cgrp, struct cftype *cft)
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008721{
Paul Menage2b01dfe2007-10-24 18:23:50 +02008722 struct task_group *tg = cgroup_tg(cgrp);
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008723
8724 return (u64) tg->shares;
8725}
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +02008726#endif /* CONFIG_FAIR_GROUP_SCHED */
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008727
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01008728#ifdef CONFIG_RT_GROUP_SCHED
Mirco Tischler0c708142008-05-14 16:05:46 -07008729static int cpu_rt_runtime_write(struct cgroup *cgrp, struct cftype *cft,
Paul Menage06ecb272008-04-29 01:00:06 -07008730 s64 val)
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008731{
Paul Menage06ecb272008-04-29 01:00:06 -07008732 return sched_group_set_rt_runtime(cgroup_tg(cgrp), val);
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008733}
8734
Paul Menage06ecb272008-04-29 01:00:06 -07008735static s64 cpu_rt_runtime_read(struct cgroup *cgrp, struct cftype *cft)
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008736{
Paul Menage06ecb272008-04-29 01:00:06 -07008737 return sched_group_rt_runtime(cgroup_tg(cgrp));
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008738}
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008739
8740static int cpu_rt_period_write_uint(struct cgroup *cgrp, struct cftype *cftype,
8741 u64 rt_period_us)
8742{
8743 return sched_group_set_rt_period(cgroup_tg(cgrp), rt_period_us);
8744}
8745
8746static u64 cpu_rt_period_read_uint(struct cgroup *cgrp, struct cftype *cft)
8747{
8748 return sched_group_rt_period(cgroup_tg(cgrp));
8749}
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +02008750#endif /* CONFIG_RT_GROUP_SCHED */
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008751
Paul Menagefe5c7cc2007-10-29 21:18:11 +01008752static struct cftype cpu_files[] = {
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01008753#ifdef CONFIG_FAIR_GROUP_SCHED
Paul Menagefe5c7cc2007-10-29 21:18:11 +01008754 {
8755 .name = "shares",
Paul Menagef4c753b2008-04-29 00:59:56 -07008756 .read_u64 = cpu_shares_read_u64,
8757 .write_u64 = cpu_shares_write_u64,
Paul Menagefe5c7cc2007-10-29 21:18:11 +01008758 },
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01008759#endif
8760#ifdef CONFIG_RT_GROUP_SCHED
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008761 {
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01008762 .name = "rt_runtime_us",
Paul Menage06ecb272008-04-29 01:00:06 -07008763 .read_s64 = cpu_rt_runtime_read,
8764 .write_s64 = cpu_rt_runtime_write,
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008765 },
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008766 {
8767 .name = "rt_period_us",
Paul Menagef4c753b2008-04-29 00:59:56 -07008768 .read_u64 = cpu_rt_period_read_uint,
8769 .write_u64 = cpu_rt_period_write_uint,
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008770 },
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01008771#endif
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008772};
8773
8774static int cpu_cgroup_populate(struct cgroup_subsys *ss, struct cgroup *cont)
8775{
Paul Menagefe5c7cc2007-10-29 21:18:11 +01008776 return cgroup_add_files(cont, ss, cpu_files, ARRAY_SIZE(cpu_files));
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008777}
8778
8779struct cgroup_subsys cpu_cgroup_subsys = {
Ingo Molnar38605ca2007-10-29 21:18:11 +01008780 .name = "cpu",
8781 .create = cpu_cgroup_create,
8782 .destroy = cpu_cgroup_destroy,
8783 .can_attach = cpu_cgroup_can_attach,
8784 .attach = cpu_cgroup_attach,
8785 .populate = cpu_cgroup_populate,
8786 .subsys_id = cpu_cgroup_subsys_id,
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008787 .early_init = 1,
8788};
8789
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01008790#endif /* CONFIG_CGROUP_SCHED */
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01008791
8792#ifdef CONFIG_CGROUP_CPUACCT
8793
8794/*
8795 * CPU accounting code for task groups.
8796 *
8797 * Based on the work by Paul Menage (menage@google.com) and Balbir Singh
8798 * (balbir@in.ibm.com).
8799 */
8800
Bharata B Rao934352f2008-11-10 20:41:13 +05308801/* track cpu usage of a group of tasks and its child groups */
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01008802struct cpuacct {
8803 struct cgroup_subsys_state css;
8804 /* cpuusage holds pointer to a u64-type object on every cpu */
8805 u64 *cpuusage;
Bharata B Raoef12fef2009-03-31 10:02:22 +05308806 struct percpu_counter cpustat[CPUACCT_STAT_NSTATS];
Bharata B Rao934352f2008-11-10 20:41:13 +05308807 struct cpuacct *parent;
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01008808};
8809
8810struct cgroup_subsys cpuacct_subsys;
8811
8812/* return cpu accounting group corresponding to this container */
Dhaval Giani32cd7562008-02-29 10:02:43 +05308813static inline struct cpuacct *cgroup_ca(struct cgroup *cgrp)
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01008814{
Dhaval Giani32cd7562008-02-29 10:02:43 +05308815 return container_of(cgroup_subsys_state(cgrp, cpuacct_subsys_id),
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01008816 struct cpuacct, css);
8817}
8818
8819/* return cpu accounting group to which this task belongs */
8820static inline struct cpuacct *task_ca(struct task_struct *tsk)
8821{
8822 return container_of(task_subsys_state(tsk, cpuacct_subsys_id),
8823 struct cpuacct, css);
8824}
8825
8826/* create a new cpu accounting group */
8827static struct cgroup_subsys_state *cpuacct_create(
Dhaval Giani32cd7562008-02-29 10:02:43 +05308828 struct cgroup_subsys *ss, struct cgroup *cgrp)
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01008829{
8830 struct cpuacct *ca = kzalloc(sizeof(*ca), GFP_KERNEL);
Bharata B Raoef12fef2009-03-31 10:02:22 +05308831 int i;
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01008832
8833 if (!ca)
Bharata B Raoef12fef2009-03-31 10:02:22 +05308834 goto out;
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01008835
8836 ca->cpuusage = alloc_percpu(u64);
Bharata B Raoef12fef2009-03-31 10:02:22 +05308837 if (!ca->cpuusage)
8838 goto out_free_ca;
8839
8840 for (i = 0; i < CPUACCT_STAT_NSTATS; i++)
8841 if (percpu_counter_init(&ca->cpustat[i], 0))
8842 goto out_free_counters;
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01008843
Bharata B Rao934352f2008-11-10 20:41:13 +05308844 if (cgrp->parent)
8845 ca->parent = cgroup_ca(cgrp->parent);
8846
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01008847 return &ca->css;
Bharata B Raoef12fef2009-03-31 10:02:22 +05308848
8849out_free_counters:
8850 while (--i >= 0)
8851 percpu_counter_destroy(&ca->cpustat[i]);
8852 free_percpu(ca->cpuusage);
8853out_free_ca:
8854 kfree(ca);
8855out:
8856 return ERR_PTR(-ENOMEM);
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01008857}
8858
8859/* destroy an existing cpu accounting group */
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01008860static void
Dhaval Giani32cd7562008-02-29 10:02:43 +05308861cpuacct_destroy(struct cgroup_subsys *ss, struct cgroup *cgrp)
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01008862{
Dhaval Giani32cd7562008-02-29 10:02:43 +05308863 struct cpuacct *ca = cgroup_ca(cgrp);
Bharata B Raoef12fef2009-03-31 10:02:22 +05308864 int i;
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01008865
Bharata B Raoef12fef2009-03-31 10:02:22 +05308866 for (i = 0; i < CPUACCT_STAT_NSTATS; i++)
8867 percpu_counter_destroy(&ca->cpustat[i]);
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01008868 free_percpu(ca->cpuusage);
8869 kfree(ca);
8870}
8871
Ken Chen720f5492008-12-15 22:02:01 -08008872static u64 cpuacct_cpuusage_read(struct cpuacct *ca, int cpu)
8873{
Rusty Russellb36128c2009-02-20 16:29:08 +09008874 u64 *cpuusage = per_cpu_ptr(ca->cpuusage, cpu);
Ken Chen720f5492008-12-15 22:02:01 -08008875 u64 data;
8876
8877#ifndef CONFIG_64BIT
8878 /*
8879 * Take rq->lock to make 64-bit read safe on 32-bit platforms.
8880 */
Thomas Gleixner05fa7852009-11-17 14:28:38 +01008881 raw_spin_lock_irq(&cpu_rq(cpu)->lock);
Ken Chen720f5492008-12-15 22:02:01 -08008882 data = *cpuusage;
Thomas Gleixner05fa7852009-11-17 14:28:38 +01008883 raw_spin_unlock_irq(&cpu_rq(cpu)->lock);
Ken Chen720f5492008-12-15 22:02:01 -08008884#else
8885 data = *cpuusage;
8886#endif
8887
8888 return data;
8889}
8890
8891static void cpuacct_cpuusage_write(struct cpuacct *ca, int cpu, u64 val)
8892{
Rusty Russellb36128c2009-02-20 16:29:08 +09008893 u64 *cpuusage = per_cpu_ptr(ca->cpuusage, cpu);
Ken Chen720f5492008-12-15 22:02:01 -08008894
8895#ifndef CONFIG_64BIT
8896 /*
8897 * Take rq->lock to make 64-bit write safe on 32-bit platforms.
8898 */
Thomas Gleixner05fa7852009-11-17 14:28:38 +01008899 raw_spin_lock_irq(&cpu_rq(cpu)->lock);
Ken Chen720f5492008-12-15 22:02:01 -08008900 *cpuusage = val;
Thomas Gleixner05fa7852009-11-17 14:28:38 +01008901 raw_spin_unlock_irq(&cpu_rq(cpu)->lock);
Ken Chen720f5492008-12-15 22:02:01 -08008902#else
8903 *cpuusage = val;
8904#endif
8905}
8906
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01008907/* return total cpu usage (in nanoseconds) of a group */
Dhaval Giani32cd7562008-02-29 10:02:43 +05308908static u64 cpuusage_read(struct cgroup *cgrp, struct cftype *cft)
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01008909{
Dhaval Giani32cd7562008-02-29 10:02:43 +05308910 struct cpuacct *ca = cgroup_ca(cgrp);
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01008911 u64 totalcpuusage = 0;
8912 int i;
8913
Ken Chen720f5492008-12-15 22:02:01 -08008914 for_each_present_cpu(i)
8915 totalcpuusage += cpuacct_cpuusage_read(ca, i);
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01008916
8917 return totalcpuusage;
8918}
8919
Dhaval Giani0297b802008-02-29 10:02:44 +05308920static int cpuusage_write(struct cgroup *cgrp, struct cftype *cftype,
8921 u64 reset)
8922{
8923 struct cpuacct *ca = cgroup_ca(cgrp);
8924 int err = 0;
8925 int i;
8926
8927 if (reset) {
8928 err = -EINVAL;
8929 goto out;
8930 }
8931
Ken Chen720f5492008-12-15 22:02:01 -08008932 for_each_present_cpu(i)
8933 cpuacct_cpuusage_write(ca, i, 0);
Dhaval Giani0297b802008-02-29 10:02:44 +05308934
Dhaval Giani0297b802008-02-29 10:02:44 +05308935out:
8936 return err;
8937}
8938
Ken Chene9515c32008-12-15 22:04:15 -08008939static int cpuacct_percpu_seq_read(struct cgroup *cgroup, struct cftype *cft,
8940 struct seq_file *m)
8941{
8942 struct cpuacct *ca = cgroup_ca(cgroup);
8943 u64 percpu;
8944 int i;
8945
8946 for_each_present_cpu(i) {
8947 percpu = cpuacct_cpuusage_read(ca, i);
8948 seq_printf(m, "%llu ", (unsigned long long) percpu);
8949 }
8950 seq_printf(m, "\n");
8951 return 0;
8952}
8953
Bharata B Raoef12fef2009-03-31 10:02:22 +05308954static const char *cpuacct_stat_desc[] = {
8955 [CPUACCT_STAT_USER] = "user",
8956 [CPUACCT_STAT_SYSTEM] = "system",
8957};
8958
8959static int cpuacct_stats_show(struct cgroup *cgrp, struct cftype *cft,
8960 struct cgroup_map_cb *cb)
8961{
8962 struct cpuacct *ca = cgroup_ca(cgrp);
8963 int i;
8964
8965 for (i = 0; i < CPUACCT_STAT_NSTATS; i++) {
8966 s64 val = percpu_counter_read(&ca->cpustat[i]);
8967 val = cputime64_to_clock_t(val);
8968 cb->fill(cb, cpuacct_stat_desc[i], val);
8969 }
8970 return 0;
8971}
8972
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01008973static struct cftype files[] = {
8974 {
8975 .name = "usage",
Paul Menagef4c753b2008-04-29 00:59:56 -07008976 .read_u64 = cpuusage_read,
8977 .write_u64 = cpuusage_write,
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01008978 },
Ken Chene9515c32008-12-15 22:04:15 -08008979 {
8980 .name = "usage_percpu",
8981 .read_seq_string = cpuacct_percpu_seq_read,
8982 },
Bharata B Raoef12fef2009-03-31 10:02:22 +05308983 {
8984 .name = "stat",
8985 .read_map = cpuacct_stats_show,
8986 },
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01008987};
8988
Dhaval Giani32cd7562008-02-29 10:02:43 +05308989static int cpuacct_populate(struct cgroup_subsys *ss, struct cgroup *cgrp)
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01008990{
Dhaval Giani32cd7562008-02-29 10:02:43 +05308991 return cgroup_add_files(cgrp, ss, files, ARRAY_SIZE(files));
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01008992}
8993
8994/*
8995 * charge this task's execution time to its accounting group.
8996 *
8997 * called with rq->lock held.
8998 */
8999static void cpuacct_charge(struct task_struct *tsk, u64 cputime)
9000{
9001 struct cpuacct *ca;
Bharata B Rao934352f2008-11-10 20:41:13 +05309002 int cpu;
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01009003
Li Zefanc40c6f82009-02-26 15:40:15 +08009004 if (unlikely(!cpuacct_subsys.active))
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01009005 return;
9006
Bharata B Rao934352f2008-11-10 20:41:13 +05309007 cpu = task_cpu(tsk);
Bharata B Raoa18b83b2009-03-23 10:02:53 +05309008
9009 rcu_read_lock();
9010
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01009011 ca = task_ca(tsk);
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01009012
Bharata B Rao934352f2008-11-10 20:41:13 +05309013 for (; ca; ca = ca->parent) {
Rusty Russellb36128c2009-02-20 16:29:08 +09009014 u64 *cpuusage = per_cpu_ptr(ca->cpuusage, cpu);
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01009015 *cpuusage += cputime;
9016 }
Bharata B Raoa18b83b2009-03-23 10:02:53 +05309017
9018 rcu_read_unlock();
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01009019}
9020
Bharata B Raoef12fef2009-03-31 10:02:22 +05309021/*
Anton Blanchardfa535a72010-02-02 14:46:13 -08009022 * When CONFIG_VIRT_CPU_ACCOUNTING is enabled one jiffy can be very large
9023 * in cputime_t units. As a result, cpuacct_update_stats calls
9024 * percpu_counter_add with values large enough to always overflow the
9025 * per cpu batch limit causing bad SMP scalability.
9026 *
9027 * To fix this we scale percpu_counter_batch by cputime_one_jiffy so we
9028 * batch the same amount of time with CONFIG_VIRT_CPU_ACCOUNTING disabled
9029 * and enabled. We cap it at INT_MAX which is the largest allowed batch value.
9030 */
9031#ifdef CONFIG_SMP
9032#define CPUACCT_BATCH \
9033 min_t(long, percpu_counter_batch * cputime_one_jiffy, INT_MAX)
9034#else
9035#define CPUACCT_BATCH 0
9036#endif
9037
9038/*
Bharata B Raoef12fef2009-03-31 10:02:22 +05309039 * Charge the system/user time to the task's accounting group.
9040 */
9041static void cpuacct_update_stats(struct task_struct *tsk,
9042 enum cpuacct_stat_index idx, cputime_t val)
9043{
9044 struct cpuacct *ca;
Anton Blanchardfa535a72010-02-02 14:46:13 -08009045 int batch = CPUACCT_BATCH;
Bharata B Raoef12fef2009-03-31 10:02:22 +05309046
9047 if (unlikely(!cpuacct_subsys.active))
9048 return;
9049
9050 rcu_read_lock();
9051 ca = task_ca(tsk);
9052
9053 do {
Anton Blanchardfa535a72010-02-02 14:46:13 -08009054 __percpu_counter_add(&ca->cpustat[idx], val, batch);
Bharata B Raoef12fef2009-03-31 10:02:22 +05309055 ca = ca->parent;
9056 } while (ca);
9057 rcu_read_unlock();
9058}
9059
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01009060struct cgroup_subsys cpuacct_subsys = {
9061 .name = "cpuacct",
9062 .create = cpuacct_create,
9063 .destroy = cpuacct_destroy,
9064 .populate = cpuacct_populate,
9065 .subsys_id = cpuacct_subsys_id,
9066};
9067#endif /* CONFIG_CGROUP_CPUACCT */
Paul E. McKenney03b042b2009-06-25 09:08:16 -07009068
9069#ifndef CONFIG_SMP
9070
9071int rcu_expedited_torture_stats(char *page)
9072{
9073 return 0;
9074}
9075EXPORT_SYMBOL_GPL(rcu_expedited_torture_stats);
9076
9077void synchronize_sched_expedited(void)
9078{
9079}
9080EXPORT_SYMBOL_GPL(synchronize_sched_expedited);
9081
9082#else /* #ifndef CONFIG_SMP */
9083
9084static DEFINE_PER_CPU(struct migration_req, rcu_migration_req);
9085static DEFINE_MUTEX(rcu_sched_expedited_mutex);
9086
9087#define RCU_EXPEDITED_STATE_POST -2
9088#define RCU_EXPEDITED_STATE_IDLE -1
9089
9090static int rcu_expedited_state = RCU_EXPEDITED_STATE_IDLE;
9091
9092int rcu_expedited_torture_stats(char *page)
9093{
9094 int cnt = 0;
9095 int cpu;
9096
9097 cnt += sprintf(&page[cnt], "state: %d /", rcu_expedited_state);
9098 for_each_online_cpu(cpu) {
9099 cnt += sprintf(&page[cnt], " %d:%d",
9100 cpu, per_cpu(rcu_migration_req, cpu).dest_cpu);
9101 }
9102 cnt += sprintf(&page[cnt], "\n");
9103 return cnt;
9104}
9105EXPORT_SYMBOL_GPL(rcu_expedited_torture_stats);
9106
9107static long synchronize_sched_expedited_count;
9108
9109/*
9110 * Wait for an rcu-sched grace period to elapse, but use "big hammer"
9111 * approach to force grace period to end quickly. This consumes
9112 * significant time on all CPUs, and is thus not recommended for
9113 * any sort of common-case code.
9114 *
9115 * Note that it is illegal to call this function while holding any
9116 * lock that is acquired by a CPU-hotplug notifier. Failing to
9117 * observe this restriction will result in deadlock.
9118 */
9119void synchronize_sched_expedited(void)
9120{
9121 int cpu;
9122 unsigned long flags;
9123 bool need_full_sync = 0;
9124 struct rq *rq;
9125 struct migration_req *req;
9126 long snap;
9127 int trycount = 0;
9128
9129 smp_mb(); /* ensure prior mod happens before capturing snap. */
9130 snap = ACCESS_ONCE(synchronize_sched_expedited_count) + 1;
9131 get_online_cpus();
9132 while (!mutex_trylock(&rcu_sched_expedited_mutex)) {
9133 put_online_cpus();
9134 if (trycount++ < 10)
9135 udelay(trycount * num_online_cpus());
9136 else {
9137 synchronize_sched();
9138 return;
9139 }
9140 if (ACCESS_ONCE(synchronize_sched_expedited_count) - snap > 0) {
9141 smp_mb(); /* ensure test happens before caller kfree */
9142 return;
9143 }
9144 get_online_cpus();
9145 }
9146 rcu_expedited_state = RCU_EXPEDITED_STATE_POST;
9147 for_each_online_cpu(cpu) {
9148 rq = cpu_rq(cpu);
9149 req = &per_cpu(rcu_migration_req, cpu);
9150 init_completion(&req->done);
9151 req->task = NULL;
9152 req->dest_cpu = RCU_MIGRATION_NEED_QS;
Thomas Gleixner05fa7852009-11-17 14:28:38 +01009153 raw_spin_lock_irqsave(&rq->lock, flags);
Paul E. McKenney03b042b2009-06-25 09:08:16 -07009154 list_add(&req->list, &rq->migration_queue);
Thomas Gleixner05fa7852009-11-17 14:28:38 +01009155 raw_spin_unlock_irqrestore(&rq->lock, flags);
Paul E. McKenney03b042b2009-06-25 09:08:16 -07009156 wake_up_process(rq->migration_thread);
9157 }
9158 for_each_online_cpu(cpu) {
9159 rcu_expedited_state = cpu;
9160 req = &per_cpu(rcu_migration_req, cpu);
9161 rq = cpu_rq(cpu);
9162 wait_for_completion(&req->done);
Thomas Gleixner05fa7852009-11-17 14:28:38 +01009163 raw_spin_lock_irqsave(&rq->lock, flags);
Paul E. McKenney03b042b2009-06-25 09:08:16 -07009164 if (unlikely(req->dest_cpu == RCU_MIGRATION_MUST_SYNC))
9165 need_full_sync = 1;
9166 req->dest_cpu = RCU_MIGRATION_IDLE;
Thomas Gleixner05fa7852009-11-17 14:28:38 +01009167 raw_spin_unlock_irqrestore(&rq->lock, flags);
Paul E. McKenney03b042b2009-06-25 09:08:16 -07009168 }
9169 rcu_expedited_state = RCU_EXPEDITED_STATE_IDLE;
Paul E. McKenney956539b2009-11-10 13:37:20 -08009170 synchronize_sched_expedited_count++;
Paul E. McKenney03b042b2009-06-25 09:08:16 -07009171 mutex_unlock(&rcu_sched_expedited_mutex);
9172 put_online_cpus();
9173 if (need_full_sync)
9174 synchronize_sched();
9175}
9176EXPORT_SYMBOL_GPL(synchronize_sched_expedited);
9177
9178#endif /* #else #ifndef CONFIG_SMP */