blob: 3e8a7db951a6c0db9583dbaacec75398968fc860 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * kernel/sched.c
3 *
4 * Kernel scheduler and related syscalls
5 *
6 * Copyright (C) 1991-2002 Linus Torvalds
7 *
8 * 1996-12-23 Modified by Dave Grothe to fix bugs in semaphores and
9 * make semaphores SMP safe
10 * 1998-11-19 Implemented schedule_timeout() and related stuff
11 * by Andrea Arcangeli
12 * 2002-01-04 New ultra-scalable O(1) scheduler by Ingo Molnar:
13 * hybrid priority-list and round-robin design with
14 * an array-switch method of distributing timeslices
15 * and per-CPU runqueues. Cleanups and useful suggestions
16 * by Davide Libenzi, preemptible kernel bits by Robert Love.
17 * 2003-09-03 Interactivity tuning by Con Kolivas.
18 * 2004-04-02 Scheduler domains code by Nick Piggin
Ingo Molnarc31f2e82007-07-09 18:52:01 +020019 * 2007-04-15 Work begun on replacing all interactivity tuning with a
20 * fair scheduling design by Con Kolivas.
21 * 2007-05-05 Load balancing (smp-nice) and other improvements
22 * by Peter Williams
23 * 2007-05-06 Interactivity improvements to CFS by Mike Galbraith
24 * 2007-07-01 Group scheduling enhancements by Srivatsa Vaddagiri
Ingo Molnarb9131762008-01-25 21:08:19 +010025 * 2007-11-29 RT balancing improvements by Steven Rostedt, Gregory Haskins,
26 * Thomas Gleixner, Mike Kravetz
Linus Torvalds1da177e2005-04-16 15:20:36 -070027 */
28
29#include <linux/mm.h>
30#include <linux/module.h>
31#include <linux/nmi.h>
32#include <linux/init.h>
Ingo Molnardff06c12007-07-09 18:52:00 +020033#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070034#include <linux/highmem.h>
35#include <linux/smp_lock.h>
36#include <asm/mmu_context.h>
37#include <linux/interrupt.h>
Randy.Dunlapc59ede72006-01-11 12:17:46 -080038#include <linux/capability.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070039#include <linux/completion.h>
40#include <linux/kernel_stat.h>
Ingo Molnar9a11b49a2006-07-03 00:24:33 -070041#include <linux/debug_locks.h>
Ingo Molnarcdd6c482009-09-21 12:02:48 +020042#include <linux/perf_event.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070043#include <linux/security.h>
44#include <linux/notifier.h>
45#include <linux/profile.h>
Nigel Cunningham7dfb7102006-12-06 20:34:23 -080046#include <linux/freezer.h>
akpm@osdl.org198e2f12006-01-12 01:05:30 -080047#include <linux/vmalloc.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070048#include <linux/blkdev.h>
49#include <linux/delay.h>
Pavel Emelyanovb4888932007-10-18 23:40:14 -070050#include <linux/pid_namespace.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070051#include <linux/smp.h>
52#include <linux/threads.h>
53#include <linux/timer.h>
54#include <linux/rcupdate.h>
55#include <linux/cpu.h>
56#include <linux/cpuset.h>
57#include <linux/percpu.h>
Alexey Dobriyanb5aadf72008-10-06 13:23:43 +040058#include <linux/proc_fs.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070059#include <linux/seq_file.h>
Tejun Heo969c7922010-05-06 18:49:21 +020060#include <linux/stop_machine.h>
Nick Piggine692ab52007-07-26 13:40:43 +020061#include <linux/sysctl.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070062#include <linux/syscalls.h>
63#include <linux/times.h>
Jay Lan8f0ab512006-09-30 23:28:59 -070064#include <linux/tsacct_kern.h>
bibo maoc6fd91f2006-03-26 01:38:20 -080065#include <linux/kprobes.h>
Shailabh Nagar0ff92242006-07-14 00:24:37 -070066#include <linux/delayacct.h>
Ingo Molnardff06c12007-07-09 18:52:00 +020067#include <linux/unistd.h>
Jens Axboef5ff8422007-09-21 09:19:54 +020068#include <linux/pagemap.h>
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +010069#include <linux/hrtimer.h>
Reynes Philippe30914a52008-03-17 16:19:05 -070070#include <linux/tick.h>
Peter Zijlstraf00b45c2008-04-19 19:45:00 +020071#include <linux/debugfs.h>
72#include <linux/ctype.h>
Steven Rostedt6cd8a4b2008-05-12 21:20:42 +020073#include <linux/ftrace.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090074#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070075
Eric Dumazet5517d862007-05-08 00:32:57 -070076#include <asm/tlb.h>
Satyam Sharma838225b2007-10-24 18:23:50 +020077#include <asm/irq_regs.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070078
Gregory Haskins6e0534f2008-05-12 21:21:01 +020079#include "sched_cpupri.h"
Tejun Heo21aa9af2010-06-08 21:40:37 +020080#include "workqueue_sched.h"
Gregory Haskins6e0534f2008-05-12 21:21:01 +020081
Steven Rostedta8d154b2009-04-10 09:36:00 -040082#define CREATE_TRACE_POINTS
Steven Rostedtad8d75f2009-04-14 19:39:12 -040083#include <trace/events/sched.h>
Steven Rostedta8d154b2009-04-10 09:36:00 -040084
Linus Torvalds1da177e2005-04-16 15:20:36 -070085/*
86 * Convert user-nice values [ -20 ... 0 ... 19 ]
87 * to static priority [ MAX_RT_PRIO..MAX_PRIO-1 ],
88 * and back.
89 */
90#define NICE_TO_PRIO(nice) (MAX_RT_PRIO + (nice) + 20)
91#define PRIO_TO_NICE(prio) ((prio) - MAX_RT_PRIO - 20)
92#define TASK_NICE(p) PRIO_TO_NICE((p)->static_prio)
93
94/*
95 * 'User priority' is the nice value converted to something we
96 * can work with better when scaling various scheduler parameters,
97 * it's a [ 0 ... 39 ] range.
98 */
99#define USER_PRIO(p) ((p)-MAX_RT_PRIO)
100#define TASK_USER_PRIO(p) USER_PRIO((p)->static_prio)
101#define MAX_USER_PRIO (USER_PRIO(MAX_PRIO))
102
103/*
Ingo Molnard7876a02008-01-25 21:08:19 +0100104 * Helpers for converting nanosecond timing to jiffy resolution
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 */
Eric Dumazetd6322fa2007-11-09 22:39:38 +0100106#define NS_TO_JIFFIES(TIME) ((unsigned long)(TIME) / (NSEC_PER_SEC / HZ))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200108#define NICE_0_LOAD SCHED_LOAD_SCALE
109#define NICE_0_SHIFT SCHED_LOAD_SHIFT
110
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111/*
112 * These are the 'tuning knobs' of the scheduler:
113 *
Dmitry Adamushkoa4ec24b2007-10-15 17:00:13 +0200114 * default timeslice is 100 msecs (used only for SCHED_RR tasks).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 * Timeslices get refilled after they expire.
116 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117#define DEF_TIMESLICE (100 * HZ / 1000)
Peter Williams2dd73a42006-06-27 02:54:34 -0700118
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +0200119/*
120 * single value that denotes runtime == period, ie unlimited time.
121 */
122#define RUNTIME_INF ((u64)~0ULL)
123
Ingo Molnare05606d2007-07-09 18:51:59 +0200124static inline int rt_policy(int policy)
125{
Roel Kluin3f33a7c2008-05-13 23:44:11 +0200126 if (unlikely(policy == SCHED_FIFO || policy == SCHED_RR))
Ingo Molnare05606d2007-07-09 18:51:59 +0200127 return 1;
128 return 0;
129}
130
131static inline int task_has_rt_policy(struct task_struct *p)
132{
133 return rt_policy(p->policy);
134}
135
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136/*
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200137 * This is the priority-queue data structure of the RT scheduling class:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 */
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200139struct rt_prio_array {
140 DECLARE_BITMAP(bitmap, MAX_RT_PRIO+1); /* include 1 bit for delimiter */
141 struct list_head queue[MAX_RT_PRIO];
142};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +0200144struct rt_bandwidth {
Ingo Molnarea736ed2008-03-25 13:51:45 +0100145 /* nests inside the rq lock: */
Thomas Gleixner0986b112009-11-17 15:32:06 +0100146 raw_spinlock_t rt_runtime_lock;
Ingo Molnarea736ed2008-03-25 13:51:45 +0100147 ktime_t rt_period;
148 u64 rt_runtime;
149 struct hrtimer rt_period_timer;
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +0200150};
151
152static struct rt_bandwidth def_rt_bandwidth;
153
154static int do_sched_rt_period_timer(struct rt_bandwidth *rt_b, int overrun);
155
156static enum hrtimer_restart sched_rt_period_timer(struct hrtimer *timer)
157{
158 struct rt_bandwidth *rt_b =
159 container_of(timer, struct rt_bandwidth, rt_period_timer);
160 ktime_t now;
161 int overrun;
162 int idle = 0;
163
164 for (;;) {
165 now = hrtimer_cb_get_time(timer);
166 overrun = hrtimer_forward(timer, now, rt_b->rt_period);
167
168 if (!overrun)
169 break;
170
171 idle = do_sched_rt_period_timer(rt_b, overrun);
172 }
173
174 return idle ? HRTIMER_NORESTART : HRTIMER_RESTART;
175}
176
177static
178void init_rt_bandwidth(struct rt_bandwidth *rt_b, u64 period, u64 runtime)
179{
180 rt_b->rt_period = ns_to_ktime(period);
181 rt_b->rt_runtime = runtime;
182
Thomas Gleixner0986b112009-11-17 15:32:06 +0100183 raw_spin_lock_init(&rt_b->rt_runtime_lock);
Peter Zijlstraac086bc2008-04-19 19:44:58 +0200184
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +0200185 hrtimer_init(&rt_b->rt_period_timer,
186 CLOCK_MONOTONIC, HRTIMER_MODE_REL);
187 rt_b->rt_period_timer.function = sched_rt_period_timer;
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +0200188}
189
Krzysztof Heltc8bfff62008-09-05 23:46:19 +0200190static inline int rt_bandwidth_enabled(void)
191{
192 return sysctl_sched_rt_runtime >= 0;
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +0200193}
194
195static void start_rt_bandwidth(struct rt_bandwidth *rt_b)
196{
197 ktime_t now;
198
Hiroshi Shimamotocac64d02009-02-25 09:59:26 -0800199 if (!rt_bandwidth_enabled() || rt_b->rt_runtime == RUNTIME_INF)
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +0200200 return;
201
202 if (hrtimer_active(&rt_b->rt_period_timer))
203 return;
204
Thomas Gleixner0986b112009-11-17 15:32:06 +0100205 raw_spin_lock(&rt_b->rt_runtime_lock);
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +0200206 for (;;) {
Peter Zijlstra7f1e2ca2009-03-13 12:21:27 +0100207 unsigned long delta;
208 ktime_t soft, hard;
209
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +0200210 if (hrtimer_active(&rt_b->rt_period_timer))
211 break;
212
213 now = hrtimer_cb_get_time(&rt_b->rt_period_timer);
214 hrtimer_forward(&rt_b->rt_period_timer, now, rt_b->rt_period);
Peter Zijlstra7f1e2ca2009-03-13 12:21:27 +0100215
216 soft = hrtimer_get_softexpires(&rt_b->rt_period_timer);
217 hard = hrtimer_get_expires(&rt_b->rt_period_timer);
218 delta = ktime_to_ns(ktime_sub(hard, soft));
219 __hrtimer_start_range_ns(&rt_b->rt_period_timer, soft, delta,
Arun R Bharadwaj5c333862009-04-16 12:14:37 +0530220 HRTIMER_MODE_ABS_PINNED, 0);
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +0200221 }
Thomas Gleixner0986b112009-11-17 15:32:06 +0100222 raw_spin_unlock(&rt_b->rt_runtime_lock);
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +0200223}
224
225#ifdef CONFIG_RT_GROUP_SCHED
226static void destroy_rt_bandwidth(struct rt_bandwidth *rt_b)
227{
228 hrtimer_cancel(&rt_b->rt_period_timer);
229}
230#endif
231
Heiko Carstens712555e2008-04-28 11:33:07 +0200232/*
233 * sched_domains_mutex serializes calls to arch_init_sched_domains,
234 * detach_destroy_domains and partition_sched_domains.
235 */
236static DEFINE_MUTEX(sched_domains_mutex);
237
Dhaval Giani7c941432010-01-20 13:26:18 +0100238#ifdef CONFIG_CGROUP_SCHED
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +0200239
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -0700240#include <linux/cgroup.h>
241
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +0200242struct cfs_rq;
243
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100244static LIST_HEAD(task_groups);
245
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +0200246/* task group related information */
Ingo Molnar4cf86d72007-10-15 17:00:14 +0200247struct task_group {
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -0700248 struct cgroup_subsys_state css;
Arun R Bharadwaj6c415b92008-12-01 20:49:05 +0530249
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100250#ifdef CONFIG_FAIR_GROUP_SCHED
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +0200251 /* schedulable entities of this group on each cpu */
252 struct sched_entity **se;
253 /* runqueue "owned" by this group on each cpu */
254 struct cfs_rq **cfs_rq;
255 unsigned long shares;
Peter Zijlstra2069dd72010-11-15 15:47:00 -0800256
257 atomic_t load_weight;
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100258#endif
259
260#ifdef CONFIG_RT_GROUP_SCHED
261 struct sched_rt_entity **rt_se;
262 struct rt_rq **rt_rq;
263
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +0200264 struct rt_bandwidth rt_bandwidth;
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100265#endif
Srivatsa Vaddagiri6b2d7702008-01-25 21:08:00 +0100266
Srivatsa Vaddagiriae8393e2007-10-29 21:18:11 +0100267 struct rcu_head rcu;
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100268 struct list_head list;
Peter Zijlstraf473aa52008-04-19 19:45:00 +0200269
270 struct task_group *parent;
271 struct list_head siblings;
272 struct list_head children;
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +0200273};
274
Peter Zijlstraeff766a2008-04-19 19:45:00 +0200275#define root_task_group init_task_group
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100276
Peter Zijlstra3d4b47b2010-11-15 15:47:01 -0800277/* task_group_lock serializes the addition/removal of task groups */
Peter Zijlstra8ed36992008-02-13 15:45:39 +0100278static DEFINE_SPINLOCK(task_group_lock);
Srivatsa Vaddagiriec2c5072008-01-25 21:07:59 +0100279
Cyrill Gorcunove9036b32009-10-26 22:24:14 +0300280#ifdef CONFIG_FAIR_GROUP_SCHED
281
Srivatsa Vaddagiri93f992c2008-01-25 21:07:59 +0100282# define INIT_TASK_GROUP_LOAD NICE_0_LOAD
Srivatsa Vaddagiri24e377a2007-10-15 17:00:09 +0200283
Miao Xiecb4ad1f2008-04-28 12:54:56 +0800284/*
Lai Jiangshan2e084782008-06-12 16:42:58 +0800285 * A weight of 0 or 1 can cause arithmetics problems.
286 * A weight of a cfs_rq is the sum of weights of which entities
287 * are queued on this cfs_rq, so a weight of a entity should not be
288 * too large, so as the shares value of a task group.
Miao Xiecb4ad1f2008-04-28 12:54:56 +0800289 * (The default weight is 1024 - so there's no practical
290 * limitation from this.)
291 */
Peter Zijlstra18d95a22008-04-19 19:45:00 +0200292#define MIN_SHARES 2
Lai Jiangshan2e084782008-06-12 16:42:58 +0800293#define MAX_SHARES (1UL << 18)
Peter Zijlstra18d95a22008-04-19 19:45:00 +0200294
Srivatsa Vaddagiri93f992c2008-01-25 21:07:59 +0100295static int init_task_group_load = INIT_TASK_GROUP_LOAD;
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100296#endif
297
298/* Default task group.
299 * Every task in system belong to this group at bootup.
300 */
Mike Travis434d53b2008-04-04 18:11:04 -0700301struct task_group init_task_group;
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +0200302
Dhaval Giani7c941432010-01-20 13:26:18 +0100303#endif /* CONFIG_CGROUP_SCHED */
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +0200304
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200305/* CFS-related fields in a runqueue */
306struct cfs_rq {
307 struct load_weight load;
308 unsigned long nr_running;
309
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200310 u64 exec_clock;
Ingo Molnare9acbff2007-10-15 17:00:04 +0200311 u64 min_vruntime;
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200312
313 struct rb_root tasks_timeline;
314 struct rb_node *rb_leftmost;
Peter Zijlstra4a55bd52008-04-19 19:45:00 +0200315
316 struct list_head tasks;
317 struct list_head *balance_iterator;
318
319 /*
320 * 'curr' points to currently running entity on this cfs_rq.
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200321 * It is set to NULL otherwise (i.e when none are currently running).
322 */
Peter Zijlstra47932412008-11-04 21:25:09 +0100323 struct sched_entity *curr, *next, *last;
Peter Zijlstraddc97292007-10-15 17:00:10 +0200324
Peter Zijlstra5ac5c4d2008-11-10 10:46:32 +0100325 unsigned int nr_spread_over;
Peter Zijlstraddc97292007-10-15 17:00:10 +0200326
Ingo Molnar62160e32007-10-15 17:00:03 +0200327#ifdef CONFIG_FAIR_GROUP_SCHED
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200328 struct rq *rq; /* cpu runqueue to which this cfs_rq is attached */
329
Ingo Molnar41a2d6c2007-12-05 15:46:09 +0100330 /*
331 * leaf cfs_rqs are those that hold tasks (lowest schedulable entity in
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200332 * a hierarchy). Non-leaf lrqs hold other higher schedulable entities
333 * (like users, containers etc.)
334 *
335 * leaf_cfs_rq_list ties together list of leaf cfs_rq's in a cpu. This
336 * list is used during load balance.
337 */
Peter Zijlstra3d4b47b2010-11-15 15:47:01 -0800338 int on_list;
Ingo Molnar41a2d6c2007-12-05 15:46:09 +0100339 struct list_head leaf_cfs_rq_list;
340 struct task_group *tg; /* group that "owns" this runqueue */
Peter Zijlstrac09595f2008-06-27 13:41:14 +0200341
342#ifdef CONFIG_SMP
Peter Zijlstrac09595f2008-06-27 13:41:14 +0200343 /*
Peter Zijlstrac8cba852008-06-27 13:41:23 +0200344 * the part of load.weight contributed by tasks
Peter Zijlstrac09595f2008-06-27 13:41:14 +0200345 */
Peter Zijlstrac8cba852008-06-27 13:41:23 +0200346 unsigned long task_weight;
Peter Zijlstrac09595f2008-06-27 13:41:14 +0200347
Peter Zijlstrac8cba852008-06-27 13:41:23 +0200348 /*
349 * h_load = weight * f(tg)
350 *
351 * Where f(tg) is the recursive weight fraction assigned to
352 * this group.
353 */
354 unsigned long h_load;
Peter Zijlstrac09595f2008-06-27 13:41:14 +0200355
Paul Turner3b3d1902010-11-15 15:47:08 -0800356 /*
357 * Maintaining per-cpu shares distribution for group scheduling
358 *
359 * load_stamp is the last time we updated the load average
360 * load_last is the last time we updated the load average and saw load
361 * load_unacc_exec_time is currently unaccounted execution time
362 */
Peter Zijlstra2069dd72010-11-15 15:47:00 -0800363 u64 load_avg;
364 u64 load_period;
Paul Turner3b3d1902010-11-15 15:47:08 -0800365 u64 load_stamp, load_last, load_unacc_exec_time;
Peter Zijlstraf1d239f2008-06-27 13:41:38 +0200366
Peter Zijlstra2069dd72010-11-15 15:47:00 -0800367 unsigned long load_contribution;
Peter Zijlstrac09595f2008-06-27 13:41:14 +0200368#endif
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200369#endif
370};
371
372/* Real-Time classes' related field in a runqueue: */
373struct rt_rq {
374 struct rt_prio_array active;
Steven Rostedt63489e42008-01-25 21:08:03 +0100375 unsigned long rt_nr_running;
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100376#if defined CONFIG_SMP || defined CONFIG_RT_GROUP_SCHED
Gregory Haskinse864c492008-12-29 09:39:49 -0500377 struct {
378 int curr; /* highest queued rt task prio */
Gregory Haskins398a1532009-01-14 09:10:04 -0500379#ifdef CONFIG_SMP
Gregory Haskinse864c492008-12-29 09:39:49 -0500380 int next; /* next highest */
Gregory Haskins398a1532009-01-14 09:10:04 -0500381#endif
Gregory Haskinse864c492008-12-29 09:39:49 -0500382 } highest_prio;
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100383#endif
Peter Zijlstrafa85ae22008-01-25 21:08:29 +0100384#ifdef CONFIG_SMP
Gregory Haskins73fe6aa2008-01-25 21:08:07 +0100385 unsigned long rt_nr_migratory;
Peter Zijlstraa1ba4d82009-04-01 18:40:15 +0200386 unsigned long rt_nr_total;
Gregory Haskinsa22d7fc2008-01-25 21:08:12 +0100387 int overloaded;
Gregory Haskins917b6272008-12-29 09:39:53 -0500388 struct plist_head pushable_tasks;
Peter Zijlstrafa85ae22008-01-25 21:08:29 +0100389#endif
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100390 int rt_throttled;
Peter Zijlstrafa85ae22008-01-25 21:08:29 +0100391 u64 rt_time;
Peter Zijlstraac086bc2008-04-19 19:44:58 +0200392 u64 rt_runtime;
Ingo Molnarea736ed2008-03-25 13:51:45 +0100393 /* Nests inside the rq lock: */
Thomas Gleixner0986b112009-11-17 15:32:06 +0100394 raw_spinlock_t rt_runtime_lock;
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100395
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100396#ifdef CONFIG_RT_GROUP_SCHED
Peter Zijlstra23b0fdf2008-02-13 15:45:39 +0100397 unsigned long rt_nr_boosted;
398
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100399 struct rq *rq;
400 struct list_head leaf_rt_rq_list;
401 struct task_group *tg;
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100402#endif
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200403};
404
Gregory Haskins57d885f2008-01-25 21:08:18 +0100405#ifdef CONFIG_SMP
406
407/*
408 * We add the notion of a root-domain which will be used to define per-domain
Ingo Molnar0eab9142008-01-25 21:08:19 +0100409 * variables. Each exclusive cpuset essentially defines an island domain by
410 * fully partitioning the member cpus from any other cpuset. Whenever a new
Gregory Haskins57d885f2008-01-25 21:08:18 +0100411 * exclusive cpuset is created, we also create and attach a new root-domain
412 * object.
413 *
Gregory Haskins57d885f2008-01-25 21:08:18 +0100414 */
415struct root_domain {
416 atomic_t refcount;
Rusty Russellc6c49272008-11-25 02:35:05 +1030417 cpumask_var_t span;
418 cpumask_var_t online;
Gregory Haskins637f5082008-01-25 21:08:18 +0100419
Ingo Molnar0eab9142008-01-25 21:08:19 +0100420 /*
Gregory Haskins637f5082008-01-25 21:08:18 +0100421 * The "RT overload" flag: it gets set if a CPU has more than
422 * one runnable RT task.
423 */
Rusty Russellc6c49272008-11-25 02:35:05 +1030424 cpumask_var_t rto_mask;
Ingo Molnar0eab9142008-01-25 21:08:19 +0100425 atomic_t rto_count;
Gregory Haskins6e0534f2008-05-12 21:21:01 +0200426 struct cpupri cpupri;
Gregory Haskins57d885f2008-01-25 21:08:18 +0100427};
428
Gregory Haskinsdc938522008-01-25 21:08:26 +0100429/*
430 * By default the system creates a single root-domain with all cpus as
431 * members (mimicking the global state we have today).
432 */
Gregory Haskins57d885f2008-01-25 21:08:18 +0100433static struct root_domain def_root_domain;
434
Christian Dietriched2d3722010-09-06 16:37:05 +0200435#endif /* CONFIG_SMP */
Gregory Haskins57d885f2008-01-25 21:08:18 +0100436
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200437/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 * This is the main, per-CPU runqueue data structure.
439 *
440 * Locking rule: those places that want to lock multiple runqueues
441 * (such as the load balancing or the thread migration code), lock
442 * acquire operations must be ordered by ascending &runqueue.
443 */
Ingo Molnar70b97a72006-07-03 00:25:42 -0700444struct rq {
Ingo Molnard8016492007-10-18 21:32:55 +0200445 /* runqueue lock: */
Thomas Gleixner05fa7852009-11-17 14:28:38 +0100446 raw_spinlock_t lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447
448 /*
449 * nr_running and cpu_load should be in the same cacheline because
450 * remote CPUs use both these fields when doing load calculation.
451 */
452 unsigned long nr_running;
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200453 #define CPU_LOAD_IDX_MAX 5
454 unsigned long cpu_load[CPU_LOAD_IDX_MAX];
Venkatesh Pallipadifdf3e952010-05-17 18:14:43 -0700455 unsigned long last_load_update_tick;
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -0700456#ifdef CONFIG_NO_HZ
Mike Galbraith39c0cbe2010-03-11 17:17:13 +0100457 u64 nohz_stamp;
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -0700458 unsigned char nohz_balance_kick;
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -0700459#endif
Mike Galbraitha64692a2010-03-11 17:16:20 +0100460 unsigned int skip_clock_update;
461
Ingo Molnard8016492007-10-18 21:32:55 +0200462 /* capture load from *all* tasks on this cpu: */
463 struct load_weight load;
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200464 unsigned long nr_load_updates;
465 u64 nr_switches;
466
467 struct cfs_rq cfs;
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100468 struct rt_rq rt;
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100469
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200470#ifdef CONFIG_FAIR_GROUP_SCHED
Ingo Molnard8016492007-10-18 21:32:55 +0200471 /* list of leaf cfs_rq on this cpu: */
472 struct list_head leaf_cfs_rq_list;
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100473#endif
474#ifdef CONFIG_RT_GROUP_SCHED
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100475 struct list_head leaf_rt_rq_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477
478 /*
479 * This is part of a global counter where only the total sum
480 * over all CPUs matters. A task can increase this counter on
481 * one CPU and if it got migrated afterwards it may decrease
482 * it on another CPU. Always updated under the runqueue lock:
483 */
484 unsigned long nr_uninterruptible;
485
Peter Zijlstra34f971f2010-09-22 13:53:15 +0200486 struct task_struct *curr, *idle, *stop;
Christoph Lameterc9819f42006-12-10 02:20:25 -0800487 unsigned long next_balance;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 struct mm_struct *prev_mm;
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200489
Peter Zijlstra3e51f332008-05-03 18:29:28 +0200490 u64 clock;
Venkatesh Pallipadi305e6832010-10-04 17:03:21 -0700491 u64 clock_task;
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200492
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 atomic_t nr_iowait;
494
495#ifdef CONFIG_SMP
Ingo Molnar0eab9142008-01-25 21:08:19 +0100496 struct root_domain *rd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497 struct sched_domain *sd;
498
Peter Zijlstrae51fd5e2010-05-31 12:37:30 +0200499 unsigned long cpu_power;
500
Henrik Austada0a522c2009-02-13 20:35:45 +0100501 unsigned char idle_at_tick;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502 /* For active balancing */
Gregory Haskins3f029d32009-07-29 11:08:47 -0400503 int post_schedule;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 int active_balance;
505 int push_cpu;
Tejun Heo969c7922010-05-06 18:49:21 +0200506 struct cpu_stop_work active_balance_work;
Ingo Molnard8016492007-10-18 21:32:55 +0200507 /* cpu of this runqueue: */
508 int cpu;
Gregory Haskins1f11eb62008-06-04 15:04:05 -0400509 int online;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510
Peter Zijlstraa8a51d52008-06-27 13:41:26 +0200511 unsigned long avg_load_per_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512
Peter Zijlstrae9e92502009-09-01 10:34:37 +0200513 u64 rt_avg;
514 u64 age_stamp;
Mike Galbraith1b9508f2009-11-04 17:53:50 +0100515 u64 idle_stamp;
516 u64 avg_idle;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517#endif
518
Venkatesh Pallipadiaa483802010-10-04 17:03:22 -0700519#ifdef CONFIG_IRQ_TIME_ACCOUNTING
520 u64 prev_irq_time;
521#endif
522
Thomas Gleixnerdce48a82009-04-11 10:43:41 +0200523 /* calc_load related fields */
524 unsigned long calc_load_update;
525 long calc_load_active;
526
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +0100527#ifdef CONFIG_SCHED_HRTICK
Peter Zijlstra31656512008-07-18 18:01:23 +0200528#ifdef CONFIG_SMP
529 int hrtick_csd_pending;
530 struct call_single_data hrtick_csd;
531#endif
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +0100532 struct hrtimer hrtick_timer;
533#endif
534
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535#ifdef CONFIG_SCHEDSTATS
536 /* latency stats */
537 struct sched_info rq_sched_info;
Ken Chen9c2c4802008-12-16 23:41:22 -0800538 unsigned long long rq_cpu_time;
539 /* could above be rq->cfs_rq.exec_clock + rq->rt_rq.rt_runtime ? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540
541 /* sys_sched_yield() stats */
Ken Chen480b9432007-10-18 21:32:56 +0200542 unsigned int yld_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543
544 /* schedule() stats */
Ken Chen480b9432007-10-18 21:32:56 +0200545 unsigned int sched_switch;
546 unsigned int sched_count;
547 unsigned int sched_goidle;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548
549 /* try_to_wake_up() stats */
Ken Chen480b9432007-10-18 21:32:56 +0200550 unsigned int ttwu_count;
551 unsigned int ttwu_local;
Ingo Molnarb8efb562007-10-15 17:00:10 +0200552
553 /* BKL stats */
Ken Chen480b9432007-10-18 21:32:56 +0200554 unsigned int bkl_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555#endif
556};
557
Fenghua Yuf34e3b62007-07-19 01:48:13 -0700558static DEFINE_PER_CPU_SHARED_ALIGNED(struct rq, runqueues);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559
Mike Galbraitha64692a2010-03-11 17:16:20 +0100560
Peter Zijlstra1e5a7402010-10-31 12:37:04 +0100561static void check_preempt_curr(struct rq *rq, struct task_struct *p, int flags);
Ingo Molnardd41f592007-07-09 18:51:59 +0200562
Christoph Lameter0a2966b2006-09-25 23:30:51 -0700563static inline int cpu_of(struct rq *rq)
564{
565#ifdef CONFIG_SMP
566 return rq->cpu;
567#else
568 return 0;
569#endif
570}
571
Paul E. McKenney497f0ab2010-02-22 17:04:51 -0800572#define rcu_dereference_check_sched_domain(p) \
Paul E. McKenneyd11c5632010-02-22 17:04:50 -0800573 rcu_dereference_check((p), \
574 rcu_read_lock_sched_held() || \
575 lockdep_is_held(&sched_domains_mutex))
576
Ingo Molnar20d315d2007-07-09 18:51:58 +0200577/*
Nick Piggin674311d2005-06-25 14:57:27 -0700578 * The domain tree (rq->sd) is protected by RCU's quiescent state transition.
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -0700579 * See detach_destroy_domains: synchronize_sched for details.
Nick Piggin674311d2005-06-25 14:57:27 -0700580 *
581 * The domain tree of any CPU may only be accessed from within
582 * preempt-disabled sections.
583 */
Ingo Molnar48f24c42006-07-03 00:25:40 -0700584#define for_each_domain(cpu, __sd) \
Paul E. McKenney497f0ab2010-02-22 17:04:51 -0800585 for (__sd = rcu_dereference_check_sched_domain(cpu_rq(cpu)->sd); __sd; __sd = __sd->parent)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586
587#define cpu_rq(cpu) (&per_cpu(runqueues, (cpu)))
588#define this_rq() (&__get_cpu_var(runqueues))
589#define task_rq(p) cpu_rq(task_cpu(p))
590#define cpu_curr(cpu) (cpu_rq(cpu)->curr)
Hitoshi Mitake54d35f22009-06-29 14:44:57 +0900591#define raw_rq() (&__raw_get_cpu_var(runqueues))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592
Peter Zijlstradc61b1d2010-06-08 11:40:42 +0200593#ifdef CONFIG_CGROUP_SCHED
594
595/*
596 * Return the group to which this tasks belongs.
597 *
598 * We use task_subsys_state_check() and extend the RCU verification
599 * with lockdep_is_held(&task_rq(p)->lock) because cpu_cgroup_attach()
600 * holds that lock for each task it moves into the cgroup. Therefore
601 * by holding that lock, we pin the task to the current cgroup.
602 */
603static inline struct task_group *task_group(struct task_struct *p)
604{
605 struct cgroup_subsys_state *css;
606
607 css = task_subsys_state_check(p, cpu_cgroup_subsys_id,
608 lockdep_is_held(&task_rq(p)->lock));
609 return container_of(css, struct task_group, css);
610}
611
612/* Change a task's cfs_rq and parent entity if it moves across CPUs/groups */
613static inline void set_task_rq(struct task_struct *p, unsigned int cpu)
614{
615#ifdef CONFIG_FAIR_GROUP_SCHED
616 p->se.cfs_rq = task_group(p)->cfs_rq[cpu];
617 p->se.parent = task_group(p)->se[cpu];
618#endif
619
620#ifdef CONFIG_RT_GROUP_SCHED
621 p->rt.rt_rq = task_group(p)->rt_rq[cpu];
622 p->rt.parent = task_group(p)->rt_se[cpu];
623#endif
624}
625
626#else /* CONFIG_CGROUP_SCHED */
627
628static inline void set_task_rq(struct task_struct *p, unsigned int cpu) { }
629static inline struct task_group *task_group(struct task_struct *p)
630{
631 return NULL;
632}
633
634#endif /* CONFIG_CGROUP_SCHED */
635
Venkatesh Pallipadi305e6832010-10-04 17:03:21 -0700636static u64 irq_time_cpu(int cpu);
Venkatesh Pallipadiaa483802010-10-04 17:03:22 -0700637static void sched_irq_time_avg_update(struct rq *rq, u64 irq_time);
Venkatesh Pallipadi305e6832010-10-04 17:03:21 -0700638
Ingo Molnaraa9c4c02008-12-17 14:10:57 +0100639inline void update_rq_clock(struct rq *rq)
Peter Zijlstra3e51f332008-05-03 18:29:28 +0200640{
Venkatesh Pallipadi305e6832010-10-04 17:03:21 -0700641 if (!rq->skip_clock_update) {
642 int cpu = cpu_of(rq);
643 u64 irq_time;
644
645 rq->clock = sched_clock_cpu(cpu);
646 irq_time = irq_time_cpu(cpu);
647 if (rq->clock - irq_time > rq->clock_task)
648 rq->clock_task = rq->clock - irq_time;
Venkatesh Pallipadiaa483802010-10-04 17:03:22 -0700649
650 sched_irq_time_avg_update(rq, irq_time);
Venkatesh Pallipadi305e6832010-10-04 17:03:21 -0700651 }
Peter Zijlstra3e51f332008-05-03 18:29:28 +0200652}
653
Ingo Molnare436d802007-07-19 21:28:35 +0200654/*
Ingo Molnarbf5c91b2007-10-15 17:00:04 +0200655 * Tunables that become constants when CONFIG_SCHED_DEBUG is off:
656 */
657#ifdef CONFIG_SCHED_DEBUG
658# define const_debug __read_mostly
659#else
660# define const_debug static const
661#endif
662
Ingo Molnar017730c2008-05-12 21:20:52 +0200663/**
664 * runqueue_is_locked
Randy Dunlape17b38b2009-10-11 19:12:00 -0700665 * @cpu: the processor in question.
Ingo Molnar017730c2008-05-12 21:20:52 +0200666 *
667 * Returns true if the current cpu runqueue is locked.
668 * This interface allows printk to be called with the runqueue lock
669 * held and know whether or not it is OK to wake up the klogd.
670 */
Andrew Morton89f19f02009-09-19 11:55:44 -0700671int runqueue_is_locked(int cpu)
Ingo Molnar017730c2008-05-12 21:20:52 +0200672{
Thomas Gleixner05fa7852009-11-17 14:28:38 +0100673 return raw_spin_is_locked(&cpu_rq(cpu)->lock);
Ingo Molnar017730c2008-05-12 21:20:52 +0200674}
675
Ingo Molnarbf5c91b2007-10-15 17:00:04 +0200676/*
677 * Debugging: various feature bits
678 */
Peter Zijlstraf00b45c2008-04-19 19:45:00 +0200679
680#define SCHED_FEAT(name, enabled) \
681 __SCHED_FEAT_##name ,
682
Ingo Molnarbf5c91b2007-10-15 17:00:04 +0200683enum {
Peter Zijlstraf00b45c2008-04-19 19:45:00 +0200684#include "sched_features.h"
Ingo Molnarbf5c91b2007-10-15 17:00:04 +0200685};
686
Peter Zijlstraf00b45c2008-04-19 19:45:00 +0200687#undef SCHED_FEAT
Ingo Molnarbf5c91b2007-10-15 17:00:04 +0200688
Peter Zijlstraf00b45c2008-04-19 19:45:00 +0200689#define SCHED_FEAT(name, enabled) \
690 (1UL << __SCHED_FEAT_##name) * enabled |
691
692const_debug unsigned int sysctl_sched_features =
693#include "sched_features.h"
694 0;
695
696#undef SCHED_FEAT
697
698#ifdef CONFIG_SCHED_DEBUG
699#define SCHED_FEAT(name, enabled) \
700 #name ,
701
Harvey Harrison983ed7a2008-04-24 18:17:55 -0700702static __read_mostly char *sched_feat_names[] = {
Peter Zijlstraf00b45c2008-04-19 19:45:00 +0200703#include "sched_features.h"
704 NULL
705};
706
707#undef SCHED_FEAT
708
Li Zefan34f3a812008-10-30 15:23:32 +0800709static int sched_feat_show(struct seq_file *m, void *v)
Peter Zijlstraf00b45c2008-04-19 19:45:00 +0200710{
Peter Zijlstraf00b45c2008-04-19 19:45:00 +0200711 int i;
712
713 for (i = 0; sched_feat_names[i]; i++) {
Li Zefan34f3a812008-10-30 15:23:32 +0800714 if (!(sysctl_sched_features & (1UL << i)))
715 seq_puts(m, "NO_");
716 seq_printf(m, "%s ", sched_feat_names[i]);
Peter Zijlstraf00b45c2008-04-19 19:45:00 +0200717 }
Li Zefan34f3a812008-10-30 15:23:32 +0800718 seq_puts(m, "\n");
Peter Zijlstraf00b45c2008-04-19 19:45:00 +0200719
Li Zefan34f3a812008-10-30 15:23:32 +0800720 return 0;
Peter Zijlstraf00b45c2008-04-19 19:45:00 +0200721}
722
723static ssize_t
724sched_feat_write(struct file *filp, const char __user *ubuf,
725 size_t cnt, loff_t *ppos)
726{
727 char buf[64];
Mathieu Desnoyers77401912010-09-13 17:47:00 -0400728 char *cmp;
Peter Zijlstraf00b45c2008-04-19 19:45:00 +0200729 int neg = 0;
730 int i;
731
732 if (cnt > 63)
733 cnt = 63;
734
735 if (copy_from_user(&buf, ubuf, cnt))
736 return -EFAULT;
737
738 buf[cnt] = 0;
Mathieu Desnoyers77401912010-09-13 17:47:00 -0400739 cmp = strstrip(buf);
Peter Zijlstraf00b45c2008-04-19 19:45:00 +0200740
Ingo Molnarc24b7c52008-04-18 10:55:34 +0200741 if (strncmp(buf, "NO_", 3) == 0) {
Peter Zijlstraf00b45c2008-04-19 19:45:00 +0200742 neg = 1;
743 cmp += 3;
744 }
745
746 for (i = 0; sched_feat_names[i]; i++) {
Mathieu Desnoyers77401912010-09-13 17:47:00 -0400747 if (strcmp(cmp, sched_feat_names[i]) == 0) {
Peter Zijlstraf00b45c2008-04-19 19:45:00 +0200748 if (neg)
749 sysctl_sched_features &= ~(1UL << i);
750 else
751 sysctl_sched_features |= (1UL << i);
752 break;
753 }
754 }
755
756 if (!sched_feat_names[i])
757 return -EINVAL;
758
Jan Blunck42994722009-11-20 17:40:37 +0100759 *ppos += cnt;
Peter Zijlstraf00b45c2008-04-19 19:45:00 +0200760
761 return cnt;
762}
763
Li Zefan34f3a812008-10-30 15:23:32 +0800764static int sched_feat_open(struct inode *inode, struct file *filp)
765{
766 return single_open(filp, sched_feat_show, NULL);
767}
768
Alexey Dobriyan828c0952009-10-01 15:43:56 -0700769static const struct file_operations sched_feat_fops = {
Li Zefan34f3a812008-10-30 15:23:32 +0800770 .open = sched_feat_open,
771 .write = sched_feat_write,
772 .read = seq_read,
773 .llseek = seq_lseek,
774 .release = single_release,
Peter Zijlstraf00b45c2008-04-19 19:45:00 +0200775};
776
777static __init int sched_init_debug(void)
778{
Peter Zijlstraf00b45c2008-04-19 19:45:00 +0200779 debugfs_create_file("sched_features", 0644, NULL, NULL,
780 &sched_feat_fops);
781
782 return 0;
783}
784late_initcall(sched_init_debug);
785
786#endif
787
788#define sched_feat(x) (sysctl_sched_features & (1UL << __SCHED_FEAT_##x))
Ingo Molnarbf5c91b2007-10-15 17:00:04 +0200789
790/*
Peter Zijlstrab82d9fd2007-11-09 22:39:39 +0100791 * Number of tasks to iterate in a single balance run.
792 * Limited because this is done with IRQs disabled.
793 */
794const_debug unsigned int sysctl_sched_nr_migrate = 32;
795
796/*
Peter Zijlstrae9e92502009-09-01 10:34:37 +0200797 * period over which we average the RT time consumption, measured
798 * in ms.
799 *
800 * default: 1s
801 */
802const_debug unsigned int sysctl_sched_time_avg = MSEC_PER_SEC;
803
804/*
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +0100805 * period over which we measure -rt task cpu usage in us.
Peter Zijlstrafa85ae22008-01-25 21:08:29 +0100806 * default: 1s
807 */
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +0100808unsigned int sysctl_sched_rt_period = 1000000;
Peter Zijlstrafa85ae22008-01-25 21:08:29 +0100809
Ingo Molnar6892b752008-02-13 14:02:36 +0100810static __read_mostly int scheduler_running;
811
Peter Zijlstrafa85ae22008-01-25 21:08:29 +0100812/*
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +0100813 * part of the period that we allow rt tasks to run in us.
814 * default: 0.95s
Peter Zijlstrafa85ae22008-01-25 21:08:29 +0100815 */
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +0100816int sysctl_sched_rt_runtime = 950000;
817
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +0200818static inline u64 global_rt_period(void)
819{
820 return (u64)sysctl_sched_rt_period * NSEC_PER_USEC;
821}
822
823static inline u64 global_rt_runtime(void)
824{
roel kluine26873b2008-07-22 16:51:15 -0400825 if (sysctl_sched_rt_runtime < 0)
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +0200826 return RUNTIME_INF;
827
828 return (u64)sysctl_sched_rt_runtime * NSEC_PER_USEC;
829}
Peter Zijlstrafa85ae22008-01-25 21:08:29 +0100830
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831#ifndef prepare_arch_switch
Nick Piggin4866cde2005-06-25 14:57:23 -0700832# define prepare_arch_switch(next) do { } while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833#endif
Nick Piggin4866cde2005-06-25 14:57:23 -0700834#ifndef finish_arch_switch
835# define finish_arch_switch(prev) do { } while (0)
836#endif
837
Dmitry Adamushko051a1d12007-12-18 15:21:13 +0100838static inline int task_current(struct rq *rq, struct task_struct *p)
839{
840 return rq->curr == p;
841}
842
Nick Piggin4866cde2005-06-25 14:57:23 -0700843#ifndef __ARCH_WANT_UNLOCKED_CTXSW
Ingo Molnar70b97a72006-07-03 00:25:42 -0700844static inline int task_running(struct rq *rq, struct task_struct *p)
Nick Piggin4866cde2005-06-25 14:57:23 -0700845{
Dmitry Adamushko051a1d12007-12-18 15:21:13 +0100846 return task_current(rq, p);
Nick Piggin4866cde2005-06-25 14:57:23 -0700847}
848
Ingo Molnar70b97a72006-07-03 00:25:42 -0700849static inline void prepare_lock_switch(struct rq *rq, struct task_struct *next)
Nick Piggin4866cde2005-06-25 14:57:23 -0700850{
851}
852
Ingo Molnar70b97a72006-07-03 00:25:42 -0700853static inline void finish_lock_switch(struct rq *rq, struct task_struct *prev)
Nick Piggin4866cde2005-06-25 14:57:23 -0700854{
Ingo Molnarda04c032005-09-13 11:17:59 +0200855#ifdef CONFIG_DEBUG_SPINLOCK
856 /* this is a valid case when another task releases the spinlock */
857 rq->lock.owner = current;
858#endif
Ingo Molnar8a25d5d2006-07-03 00:24:54 -0700859 /*
860 * If we are tracking spinlock dependencies then we have to
861 * fix up the runqueue lock - which gets 'carried over' from
862 * prev into current:
863 */
864 spin_acquire(&rq->lock.dep_map, 0, 0, _THIS_IP_);
865
Thomas Gleixner05fa7852009-11-17 14:28:38 +0100866 raw_spin_unlock_irq(&rq->lock);
Nick Piggin4866cde2005-06-25 14:57:23 -0700867}
868
869#else /* __ARCH_WANT_UNLOCKED_CTXSW */
Ingo Molnar70b97a72006-07-03 00:25:42 -0700870static inline int task_running(struct rq *rq, struct task_struct *p)
Nick Piggin4866cde2005-06-25 14:57:23 -0700871{
872#ifdef CONFIG_SMP
873 return p->oncpu;
874#else
Dmitry Adamushko051a1d12007-12-18 15:21:13 +0100875 return task_current(rq, p);
Nick Piggin4866cde2005-06-25 14:57:23 -0700876#endif
877}
878
Ingo Molnar70b97a72006-07-03 00:25:42 -0700879static inline void prepare_lock_switch(struct rq *rq, struct task_struct *next)
Nick Piggin4866cde2005-06-25 14:57:23 -0700880{
881#ifdef CONFIG_SMP
882 /*
883 * We can optimise this out completely for !SMP, because the
884 * SMP rebalancing from interrupt is the only thing that cares
885 * here.
886 */
887 next->oncpu = 1;
888#endif
889#ifdef __ARCH_WANT_INTERRUPTS_ON_CTXSW
Thomas Gleixner05fa7852009-11-17 14:28:38 +0100890 raw_spin_unlock_irq(&rq->lock);
Nick Piggin4866cde2005-06-25 14:57:23 -0700891#else
Thomas Gleixner05fa7852009-11-17 14:28:38 +0100892 raw_spin_unlock(&rq->lock);
Nick Piggin4866cde2005-06-25 14:57:23 -0700893#endif
894}
895
Ingo Molnar70b97a72006-07-03 00:25:42 -0700896static inline void finish_lock_switch(struct rq *rq, struct task_struct *prev)
Nick Piggin4866cde2005-06-25 14:57:23 -0700897{
898#ifdef CONFIG_SMP
899 /*
900 * After ->oncpu is cleared, the task can be moved to a different CPU.
901 * We must ensure this doesn't happen until the switch is completely
902 * finished.
903 */
904 smp_wmb();
905 prev->oncpu = 0;
906#endif
907#ifndef __ARCH_WANT_INTERRUPTS_ON_CTXSW
908 local_irq_enable();
909#endif
910}
911#endif /* __ARCH_WANT_UNLOCKED_CTXSW */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912
913/*
Peter Zijlstra65cc8e42010-03-25 21:05:16 +0100914 * Check whether the task is waking, we use this to synchronize ->cpus_allowed
915 * against ttwu().
Peter Zijlstra0970d292010-02-15 14:45:54 +0100916 */
917static inline int task_is_waking(struct task_struct *p)
918{
Peter Zijlstra0017d732010-03-24 18:34:10 +0100919 return unlikely(p->state == TASK_WAKING);
Peter Zijlstra0970d292010-02-15 14:45:54 +0100920}
921
922/*
Ingo Molnarb29739f2006-06-27 02:54:51 -0700923 * __task_rq_lock - lock the runqueue a given task resides on.
924 * Must be called interrupts disabled.
925 */
Ingo Molnar70b97a72006-07-03 00:25:42 -0700926static inline struct rq *__task_rq_lock(struct task_struct *p)
Ingo Molnarb29739f2006-06-27 02:54:51 -0700927 __acquires(rq->lock)
928{
Peter Zijlstra0970d292010-02-15 14:45:54 +0100929 struct rq *rq;
930
Andi Kleen3a5c3592007-10-15 17:00:14 +0200931 for (;;) {
Peter Zijlstra0970d292010-02-15 14:45:54 +0100932 rq = task_rq(p);
Thomas Gleixner05fa7852009-11-17 14:28:38 +0100933 raw_spin_lock(&rq->lock);
Peter Zijlstra65cc8e42010-03-25 21:05:16 +0100934 if (likely(rq == task_rq(p)))
Andi Kleen3a5c3592007-10-15 17:00:14 +0200935 return rq;
Thomas Gleixner05fa7852009-11-17 14:28:38 +0100936 raw_spin_unlock(&rq->lock);
Ingo Molnarb29739f2006-06-27 02:54:51 -0700937 }
Ingo Molnarb29739f2006-06-27 02:54:51 -0700938}
939
940/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941 * task_rq_lock - lock the runqueue a given task resides on and disable
Ingo Molnar41a2d6c2007-12-05 15:46:09 +0100942 * interrupts. Note the ordering: we can safely lookup the task_rq without
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943 * explicitly disabling preemption.
944 */
Ingo Molnar70b97a72006-07-03 00:25:42 -0700945static struct rq *task_rq_lock(struct task_struct *p, unsigned long *flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946 __acquires(rq->lock)
947{
Ingo Molnar70b97a72006-07-03 00:25:42 -0700948 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949
Andi Kleen3a5c3592007-10-15 17:00:14 +0200950 for (;;) {
951 local_irq_save(*flags);
952 rq = task_rq(p);
Thomas Gleixner05fa7852009-11-17 14:28:38 +0100953 raw_spin_lock(&rq->lock);
Peter Zijlstra65cc8e42010-03-25 21:05:16 +0100954 if (likely(rq == task_rq(p)))
Andi Kleen3a5c3592007-10-15 17:00:14 +0200955 return rq;
Thomas Gleixner05fa7852009-11-17 14:28:38 +0100956 raw_spin_unlock_irqrestore(&rq->lock, *flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958}
959
Alexey Dobriyana9957442007-10-15 17:00:13 +0200960static void __task_rq_unlock(struct rq *rq)
Ingo Molnarb29739f2006-06-27 02:54:51 -0700961 __releases(rq->lock)
962{
Thomas Gleixner05fa7852009-11-17 14:28:38 +0100963 raw_spin_unlock(&rq->lock);
Ingo Molnarb29739f2006-06-27 02:54:51 -0700964}
965
Ingo Molnar70b97a72006-07-03 00:25:42 -0700966static inline void task_rq_unlock(struct rq *rq, unsigned long *flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967 __releases(rq->lock)
968{
Thomas Gleixner05fa7852009-11-17 14:28:38 +0100969 raw_spin_unlock_irqrestore(&rq->lock, *flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970}
971
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972/*
Robert P. J. Daycc2a73b2006-12-10 02:20:00 -0800973 * this_rq_lock - lock this runqueue and disable interrupts.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974 */
Alexey Dobriyana9957442007-10-15 17:00:13 +0200975static struct rq *this_rq_lock(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976 __acquires(rq->lock)
977{
Ingo Molnar70b97a72006-07-03 00:25:42 -0700978 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979
980 local_irq_disable();
981 rq = this_rq();
Thomas Gleixner05fa7852009-11-17 14:28:38 +0100982 raw_spin_lock(&rq->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983
984 return rq;
985}
986
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +0100987#ifdef CONFIG_SCHED_HRTICK
988/*
989 * Use HR-timers to deliver accurate preemption points.
990 *
991 * Its all a bit involved since we cannot program an hrt while holding the
992 * rq->lock. So what we do is store a state in in rq->hrtick_* and ask for a
993 * reschedule event.
994 *
995 * When we get rescheduled we reprogram the hrtick_timer outside of the
996 * rq->lock.
997 */
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +0100998
999/*
1000 * Use hrtick when:
1001 * - enabled by features
1002 * - hrtimer is actually high res
1003 */
1004static inline int hrtick_enabled(struct rq *rq)
1005{
1006 if (!sched_feat(HRTICK))
1007 return 0;
Ingo Molnarba420592008-07-20 11:02:06 +02001008 if (!cpu_active(cpu_of(rq)))
Peter Zijlstrab328ca12008-04-29 10:02:46 +02001009 return 0;
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01001010 return hrtimer_is_hres_active(&rq->hrtick_timer);
1011}
1012
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01001013static void hrtick_clear(struct rq *rq)
1014{
1015 if (hrtimer_active(&rq->hrtick_timer))
1016 hrtimer_cancel(&rq->hrtick_timer);
1017}
1018
1019/*
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01001020 * High-resolution timer tick.
1021 * Runs from hardirq context with interrupts disabled.
1022 */
1023static enum hrtimer_restart hrtick(struct hrtimer *timer)
1024{
1025 struct rq *rq = container_of(timer, struct rq, hrtick_timer);
1026
1027 WARN_ON_ONCE(cpu_of(rq) != smp_processor_id());
1028
Thomas Gleixner05fa7852009-11-17 14:28:38 +01001029 raw_spin_lock(&rq->lock);
Peter Zijlstra3e51f332008-05-03 18:29:28 +02001030 update_rq_clock(rq);
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01001031 rq->curr->sched_class->task_tick(rq, rq->curr, 1);
Thomas Gleixner05fa7852009-11-17 14:28:38 +01001032 raw_spin_unlock(&rq->lock);
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01001033
1034 return HRTIMER_NORESTART;
1035}
1036
Rabin Vincent95e904c2008-05-11 05:55:33 +05301037#ifdef CONFIG_SMP
Peter Zijlstra31656512008-07-18 18:01:23 +02001038/*
1039 * called from hardirq (IPI) context
1040 */
1041static void __hrtick_start(void *arg)
Peter Zijlstrab328ca12008-04-29 10:02:46 +02001042{
Peter Zijlstra31656512008-07-18 18:01:23 +02001043 struct rq *rq = arg;
Peter Zijlstrab328ca12008-04-29 10:02:46 +02001044
Thomas Gleixner05fa7852009-11-17 14:28:38 +01001045 raw_spin_lock(&rq->lock);
Peter Zijlstra31656512008-07-18 18:01:23 +02001046 hrtimer_restart(&rq->hrtick_timer);
1047 rq->hrtick_csd_pending = 0;
Thomas Gleixner05fa7852009-11-17 14:28:38 +01001048 raw_spin_unlock(&rq->lock);
Peter Zijlstrab328ca12008-04-29 10:02:46 +02001049}
1050
Peter Zijlstra31656512008-07-18 18:01:23 +02001051/*
1052 * Called to set the hrtick timer state.
1053 *
1054 * called with rq->lock held and irqs disabled
1055 */
1056static void hrtick_start(struct rq *rq, u64 delay)
Peter Zijlstrab328ca12008-04-29 10:02:46 +02001057{
Peter Zijlstra31656512008-07-18 18:01:23 +02001058 struct hrtimer *timer = &rq->hrtick_timer;
1059 ktime_t time = ktime_add_ns(timer->base->get_time(), delay);
Peter Zijlstrab328ca12008-04-29 10:02:46 +02001060
Arjan van de Vencc584b22008-09-01 15:02:30 -07001061 hrtimer_set_expires(timer, time);
Peter Zijlstra31656512008-07-18 18:01:23 +02001062
1063 if (rq == this_rq()) {
1064 hrtimer_restart(timer);
1065 } else if (!rq->hrtick_csd_pending) {
Peter Zijlstra6e275632009-02-25 13:59:48 +01001066 __smp_call_function_single(cpu_of(rq), &rq->hrtick_csd, 0);
Peter Zijlstra31656512008-07-18 18:01:23 +02001067 rq->hrtick_csd_pending = 1;
1068 }
Peter Zijlstrab328ca12008-04-29 10:02:46 +02001069}
1070
1071static int
1072hotplug_hrtick(struct notifier_block *nfb, unsigned long action, void *hcpu)
1073{
1074 int cpu = (int)(long)hcpu;
1075
1076 switch (action) {
1077 case CPU_UP_CANCELED:
1078 case CPU_UP_CANCELED_FROZEN:
1079 case CPU_DOWN_PREPARE:
1080 case CPU_DOWN_PREPARE_FROZEN:
1081 case CPU_DEAD:
1082 case CPU_DEAD_FROZEN:
Peter Zijlstra31656512008-07-18 18:01:23 +02001083 hrtick_clear(cpu_rq(cpu));
Peter Zijlstrab328ca12008-04-29 10:02:46 +02001084 return NOTIFY_OK;
1085 }
1086
1087 return NOTIFY_DONE;
1088}
1089
Rakib Mullickfa748202008-09-22 14:55:45 -07001090static __init void init_hrtick(void)
Peter Zijlstrab328ca12008-04-29 10:02:46 +02001091{
1092 hotcpu_notifier(hotplug_hrtick, 0);
1093}
Peter Zijlstra31656512008-07-18 18:01:23 +02001094#else
1095/*
1096 * Called to set the hrtick timer state.
1097 *
1098 * called with rq->lock held and irqs disabled
1099 */
1100static void hrtick_start(struct rq *rq, u64 delay)
1101{
Peter Zijlstra7f1e2ca2009-03-13 12:21:27 +01001102 __hrtimer_start_range_ns(&rq->hrtick_timer, ns_to_ktime(delay), 0,
Arun R Bharadwaj5c333862009-04-16 12:14:37 +05301103 HRTIMER_MODE_REL_PINNED, 0);
Peter Zijlstra31656512008-07-18 18:01:23 +02001104}
1105
Andrew Morton006c75f2008-09-22 14:55:46 -07001106static inline void init_hrtick(void)
Peter Zijlstra31656512008-07-18 18:01:23 +02001107{
1108}
Rabin Vincent95e904c2008-05-11 05:55:33 +05301109#endif /* CONFIG_SMP */
Peter Zijlstrab328ca12008-04-29 10:02:46 +02001110
1111static void init_rq_hrtick(struct rq *rq)
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01001112{
Peter Zijlstra31656512008-07-18 18:01:23 +02001113#ifdef CONFIG_SMP
1114 rq->hrtick_csd_pending = 0;
1115
1116 rq->hrtick_csd.flags = 0;
1117 rq->hrtick_csd.func = __hrtick_start;
1118 rq->hrtick_csd.info = rq;
1119#endif
1120
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01001121 hrtimer_init(&rq->hrtick_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
1122 rq->hrtick_timer.function = hrtick;
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01001123}
Andrew Morton006c75f2008-09-22 14:55:46 -07001124#else /* CONFIG_SCHED_HRTICK */
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01001125static inline void hrtick_clear(struct rq *rq)
1126{
1127}
1128
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01001129static inline void init_rq_hrtick(struct rq *rq)
1130{
1131}
1132
Peter Zijlstrab328ca12008-04-29 10:02:46 +02001133static inline void init_hrtick(void)
1134{
1135}
Andrew Morton006c75f2008-09-22 14:55:46 -07001136#endif /* CONFIG_SCHED_HRTICK */
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01001137
Ingo Molnar1b9f19c2007-07-09 18:51:59 +02001138/*
Ingo Molnarc24d20d2007-07-09 18:51:59 +02001139 * resched_task - mark a task 'to be rescheduled now'.
1140 *
1141 * On UP this means the setting of the need_resched flag, on SMP it
1142 * might also involve a cross-CPU call to trigger the scheduler on
1143 * the target CPU.
1144 */
1145#ifdef CONFIG_SMP
1146
1147#ifndef tsk_is_polling
1148#define tsk_is_polling(t) test_tsk_thread_flag(t, TIF_POLLING_NRFLAG)
1149#endif
1150
Peter Zijlstra31656512008-07-18 18:01:23 +02001151static void resched_task(struct task_struct *p)
Ingo Molnarc24d20d2007-07-09 18:51:59 +02001152{
1153 int cpu;
1154
Thomas Gleixner05fa7852009-11-17 14:28:38 +01001155 assert_raw_spin_locked(&task_rq(p)->lock);
Ingo Molnarc24d20d2007-07-09 18:51:59 +02001156
Lai Jiangshan5ed0cec2009-03-06 19:40:20 +08001157 if (test_tsk_need_resched(p))
Ingo Molnarc24d20d2007-07-09 18:51:59 +02001158 return;
1159
Lai Jiangshan5ed0cec2009-03-06 19:40:20 +08001160 set_tsk_need_resched(p);
Ingo Molnarc24d20d2007-07-09 18:51:59 +02001161
1162 cpu = task_cpu(p);
1163 if (cpu == smp_processor_id())
1164 return;
1165
1166 /* NEED_RESCHED must be visible before we test polling */
1167 smp_mb();
1168 if (!tsk_is_polling(p))
1169 smp_send_reschedule(cpu);
1170}
1171
1172static void resched_cpu(int cpu)
1173{
1174 struct rq *rq = cpu_rq(cpu);
1175 unsigned long flags;
1176
Thomas Gleixner05fa7852009-11-17 14:28:38 +01001177 if (!raw_spin_trylock_irqsave(&rq->lock, flags))
Ingo Molnarc24d20d2007-07-09 18:51:59 +02001178 return;
1179 resched_task(cpu_curr(cpu));
Thomas Gleixner05fa7852009-11-17 14:28:38 +01001180 raw_spin_unlock_irqrestore(&rq->lock, flags);
Ingo Molnarc24d20d2007-07-09 18:51:59 +02001181}
Thomas Gleixner06d83082008-03-22 09:20:24 +01001182
1183#ifdef CONFIG_NO_HZ
1184/*
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07001185 * In the semi idle case, use the nearest busy cpu for migrating timers
1186 * from an idle cpu. This is good for power-savings.
1187 *
1188 * We don't do similar optimization for completely idle system, as
1189 * selecting an idle cpu will add more delays to the timers than intended
1190 * (as that cpu's timer base may not be uptodate wrt jiffies etc).
1191 */
1192int get_nohz_timer_target(void)
1193{
1194 int cpu = smp_processor_id();
1195 int i;
1196 struct sched_domain *sd;
1197
1198 for_each_domain(cpu, sd) {
1199 for_each_cpu(i, sched_domain_span(sd))
1200 if (!idle_cpu(i))
1201 return i;
1202 }
1203 return cpu;
1204}
1205/*
Thomas Gleixner06d83082008-03-22 09:20:24 +01001206 * When add_timer_on() enqueues a timer into the timer wheel of an
1207 * idle CPU then this timer might expire before the next timer event
1208 * which is scheduled to wake up that CPU. In case of a completely
1209 * idle system the next event might even be infinite time into the
1210 * future. wake_up_idle_cpu() ensures that the CPU is woken up and
1211 * leaves the inner idle loop so the newly added timer is taken into
1212 * account when the CPU goes back to idle and evaluates the timer
1213 * wheel for the next timer event.
1214 */
1215void wake_up_idle_cpu(int cpu)
1216{
1217 struct rq *rq = cpu_rq(cpu);
1218
1219 if (cpu == smp_processor_id())
1220 return;
1221
1222 /*
1223 * This is safe, as this function is called with the timer
1224 * wheel base lock of (cpu) held. When the CPU is on the way
1225 * to idle and has not yet set rq->curr to idle then it will
1226 * be serialized on the timer wheel base lock and take the new
1227 * timer into account automatically.
1228 */
1229 if (rq->curr != rq->idle)
1230 return;
1231
1232 /*
1233 * We can set TIF_RESCHED on the idle task of the other CPU
1234 * lockless. The worst case is that the other CPU runs the
1235 * idle task through an additional NOOP schedule()
1236 */
Lai Jiangshan5ed0cec2009-03-06 19:40:20 +08001237 set_tsk_need_resched(rq->idle);
Thomas Gleixner06d83082008-03-22 09:20:24 +01001238
1239 /* NEED_RESCHED must be visible before we test polling */
1240 smp_mb();
1241 if (!tsk_is_polling(rq->idle))
1242 smp_send_reschedule(cpu);
1243}
Mike Galbraith39c0cbe2010-03-11 17:17:13 +01001244
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +02001245#endif /* CONFIG_NO_HZ */
Thomas Gleixner06d83082008-03-22 09:20:24 +01001246
Peter Zijlstrae9e92502009-09-01 10:34:37 +02001247static u64 sched_avg_period(void)
1248{
1249 return (u64)sysctl_sched_time_avg * NSEC_PER_MSEC / 2;
1250}
1251
1252static void sched_avg_update(struct rq *rq)
1253{
1254 s64 period = sched_avg_period();
1255
1256 while ((s64)(rq->clock - rq->age_stamp) > period) {
Will Deacon0d98bb22010-05-24 12:11:43 -07001257 /*
1258 * Inline assembly required to prevent the compiler
1259 * optimising this loop into a divmod call.
1260 * See __iter_div_u64_rem() for another example of this.
1261 */
1262 asm("" : "+rm" (rq->age_stamp));
Peter Zijlstrae9e92502009-09-01 10:34:37 +02001263 rq->age_stamp += period;
1264 rq->rt_avg /= 2;
1265 }
1266}
1267
1268static void sched_rt_avg_update(struct rq *rq, u64 rt_delta)
1269{
1270 rq->rt_avg += rt_delta;
1271 sched_avg_update(rq);
1272}
1273
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +02001274#else /* !CONFIG_SMP */
Peter Zijlstra31656512008-07-18 18:01:23 +02001275static void resched_task(struct task_struct *p)
Ingo Molnarc24d20d2007-07-09 18:51:59 +02001276{
Thomas Gleixner05fa7852009-11-17 14:28:38 +01001277 assert_raw_spin_locked(&task_rq(p)->lock);
Peter Zijlstra31656512008-07-18 18:01:23 +02001278 set_tsk_need_resched(p);
Ingo Molnarc24d20d2007-07-09 18:51:59 +02001279}
Peter Zijlstrae9e92502009-09-01 10:34:37 +02001280
1281static void sched_rt_avg_update(struct rq *rq, u64 rt_delta)
1282{
1283}
Suresh Siddhada2b71e2010-08-23 13:42:51 -07001284
1285static void sched_avg_update(struct rq *rq)
1286{
1287}
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +02001288#endif /* CONFIG_SMP */
Ingo Molnarc24d20d2007-07-09 18:51:59 +02001289
Ingo Molnar45bf76d2007-07-09 18:51:59 +02001290#if BITS_PER_LONG == 32
1291# define WMULT_CONST (~0UL)
1292#else
1293# define WMULT_CONST (1UL << 32)
1294#endif
1295
1296#define WMULT_SHIFT 32
1297
Ingo Molnar194081e2007-08-09 11:16:51 +02001298/*
1299 * Shift right and round:
1300 */
Ingo Molnarcf2ab462007-09-05 14:32:49 +02001301#define SRR(x, y) (((x) + (1UL << ((y) - 1))) >> (y))
Ingo Molnar194081e2007-08-09 11:16:51 +02001302
Peter Zijlstraa7be37a2008-06-27 13:41:11 +02001303/*
1304 * delta *= weight / lw
1305 */
Ingo Molnarcb1c4fc2007-08-02 17:41:40 +02001306static unsigned long
Ingo Molnar45bf76d2007-07-09 18:51:59 +02001307calc_delta_mine(unsigned long delta_exec, unsigned long weight,
1308 struct load_weight *lw)
1309{
1310 u64 tmp;
1311
Lai Jiangshan7a232e02008-06-12 16:43:07 +08001312 if (!lw->inv_weight) {
1313 if (BITS_PER_LONG > 32 && unlikely(lw->weight >= WMULT_CONST))
1314 lw->inv_weight = 1;
1315 else
1316 lw->inv_weight = 1 + (WMULT_CONST-lw->weight/2)
1317 / (lw->weight+1);
1318 }
Ingo Molnar45bf76d2007-07-09 18:51:59 +02001319
1320 tmp = (u64)delta_exec * weight;
1321 /*
1322 * Check whether we'd overflow the 64-bit multiplication:
1323 */
Ingo Molnar194081e2007-08-09 11:16:51 +02001324 if (unlikely(tmp > WMULT_CONST))
Ingo Molnarcf2ab462007-09-05 14:32:49 +02001325 tmp = SRR(SRR(tmp, WMULT_SHIFT/2) * lw->inv_weight,
Ingo Molnar194081e2007-08-09 11:16:51 +02001326 WMULT_SHIFT/2);
1327 else
Ingo Molnarcf2ab462007-09-05 14:32:49 +02001328 tmp = SRR(tmp * lw->inv_weight, WMULT_SHIFT);
Ingo Molnar45bf76d2007-07-09 18:51:59 +02001329
Ingo Molnarecf691d2007-08-02 17:41:40 +02001330 return (unsigned long)min(tmp, (u64)(unsigned long)LONG_MAX);
Ingo Molnar45bf76d2007-07-09 18:51:59 +02001331}
1332
Ingo Molnar10919852007-10-15 17:00:04 +02001333static inline void update_load_add(struct load_weight *lw, unsigned long inc)
Ingo Molnar45bf76d2007-07-09 18:51:59 +02001334{
1335 lw->weight += inc;
Ingo Molnare89996a2008-03-14 23:48:28 +01001336 lw->inv_weight = 0;
Ingo Molnar45bf76d2007-07-09 18:51:59 +02001337}
1338
Ingo Molnar10919852007-10-15 17:00:04 +02001339static inline void update_load_sub(struct load_weight *lw, unsigned long dec)
Ingo Molnar45bf76d2007-07-09 18:51:59 +02001340{
1341 lw->weight -= dec;
Ingo Molnare89996a2008-03-14 23:48:28 +01001342 lw->inv_weight = 0;
Ingo Molnar45bf76d2007-07-09 18:51:59 +02001343}
1344
Peter Zijlstra2069dd72010-11-15 15:47:00 -08001345static inline void update_load_set(struct load_weight *lw, unsigned long w)
1346{
1347 lw->weight = w;
1348 lw->inv_weight = 0;
1349}
1350
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351/*
Peter Williams2dd73a42006-06-27 02:54:34 -07001352 * To aid in avoiding the subversion of "niceness" due to uneven distribution
1353 * of tasks with abnormal "nice" values across CPUs the contribution that
1354 * each task makes to its run queue's load is weighted according to its
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01001355 * scheduling class and "nice" value. For SCHED_NORMAL tasks this is just a
Peter Williams2dd73a42006-06-27 02:54:34 -07001356 * scaled version of the new time slice allocation that they receive on time
1357 * slice expiry etc.
1358 */
1359
Peter Zijlstracce7ade2009-01-15 14:53:37 +01001360#define WEIGHT_IDLEPRIO 3
1361#define WMULT_IDLEPRIO 1431655765
Ingo Molnardd41f592007-07-09 18:51:59 +02001362
1363/*
1364 * Nice levels are multiplicative, with a gentle 10% change for every
1365 * nice level changed. I.e. when a CPU-bound task goes from nice 0 to
1366 * nice 1, it will get ~10% less CPU time than another CPU-bound task
1367 * that remained on nice 0.
1368 *
1369 * The "10% effect" is relative and cumulative: from _any_ nice level,
1370 * if you go up 1 level, it's -10% CPU usage, if you go down 1 level
Ingo Molnarf9153ee2007-07-16 09:46:30 +02001371 * it's +10% CPU usage. (to achieve that we use a multiplier of 1.25.
1372 * If a task goes up by ~10% and another task goes down by ~10% then
1373 * the relative distance between them is ~25%.)
Ingo Molnardd41f592007-07-09 18:51:59 +02001374 */
1375static const int prio_to_weight[40] = {
Ingo Molnar254753d2007-08-09 11:16:51 +02001376 /* -20 */ 88761, 71755, 56483, 46273, 36291,
1377 /* -15 */ 29154, 23254, 18705, 14949, 11916,
1378 /* -10 */ 9548, 7620, 6100, 4904, 3906,
1379 /* -5 */ 3121, 2501, 1991, 1586, 1277,
1380 /* 0 */ 1024, 820, 655, 526, 423,
1381 /* 5 */ 335, 272, 215, 172, 137,
1382 /* 10 */ 110, 87, 70, 56, 45,
1383 /* 15 */ 36, 29, 23, 18, 15,
Ingo Molnardd41f592007-07-09 18:51:59 +02001384};
1385
Ingo Molnar5714d2d2007-07-16 09:46:31 +02001386/*
1387 * Inverse (2^32/x) values of the prio_to_weight[] array, precalculated.
1388 *
1389 * In cases where the weight does not change often, we can use the
1390 * precalculated inverse to speed up arithmetics by turning divisions
1391 * into multiplications:
1392 */
Ingo Molnardd41f592007-07-09 18:51:59 +02001393static const u32 prio_to_wmult[40] = {
Ingo Molnar254753d2007-08-09 11:16:51 +02001394 /* -20 */ 48388, 59856, 76040, 92818, 118348,
1395 /* -15 */ 147320, 184698, 229616, 287308, 360437,
1396 /* -10 */ 449829, 563644, 704093, 875809, 1099582,
1397 /* -5 */ 1376151, 1717300, 2157191, 2708050, 3363326,
1398 /* 0 */ 4194304, 5237765, 6557202, 8165337, 10153587,
1399 /* 5 */ 12820798, 15790321, 19976592, 24970740, 31350126,
1400 /* 10 */ 39045157, 49367440, 61356676, 76695844, 95443717,
1401 /* 15 */ 119304647, 148102320, 186737708, 238609294, 286331153,
Ingo Molnardd41f592007-07-09 18:51:59 +02001402};
Peter Williams2dd73a42006-06-27 02:54:34 -07001403
Bharata B Raoef12fef2009-03-31 10:02:22 +05301404/* Time spent by the tasks of the cpu accounting group executing in ... */
1405enum cpuacct_stat_index {
1406 CPUACCT_STAT_USER, /* ... user mode */
1407 CPUACCT_STAT_SYSTEM, /* ... kernel mode */
1408
1409 CPUACCT_STAT_NSTATS,
1410};
1411
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01001412#ifdef CONFIG_CGROUP_CPUACCT
1413static void cpuacct_charge(struct task_struct *tsk, u64 cputime);
Bharata B Raoef12fef2009-03-31 10:02:22 +05301414static void cpuacct_update_stats(struct task_struct *tsk,
1415 enum cpuacct_stat_index idx, cputime_t val);
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01001416#else
1417static inline void cpuacct_charge(struct task_struct *tsk, u64 cputime) {}
Bharata B Raoef12fef2009-03-31 10:02:22 +05301418static inline void cpuacct_update_stats(struct task_struct *tsk,
1419 enum cpuacct_stat_index idx, cputime_t val) {}
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01001420#endif
1421
Peter Zijlstra18d95a22008-04-19 19:45:00 +02001422static inline void inc_cpu_load(struct rq *rq, unsigned long load)
1423{
1424 update_load_add(&rq->load, load);
1425}
1426
1427static inline void dec_cpu_load(struct rq *rq, unsigned long load)
1428{
1429 update_load_sub(&rq->load, load);
1430}
1431
Ingo Molnar7940ca32008-08-19 13:40:47 +02001432#if (defined(CONFIG_SMP) && defined(CONFIG_FAIR_GROUP_SCHED)) || defined(CONFIG_RT_GROUP_SCHED)
Peter Zijlstraeb755802008-08-19 12:33:05 +02001433typedef int (*tg_visitor)(struct task_group *, void *);
1434
1435/*
1436 * Iterate the full tree, calling @down when first entering a node and @up when
1437 * leaving it for the final time.
1438 */
1439static int walk_tg_tree(tg_visitor down, tg_visitor up, void *data)
1440{
1441 struct task_group *parent, *child;
1442 int ret;
1443
1444 rcu_read_lock();
1445 parent = &root_task_group;
1446down:
1447 ret = (*down)(parent, data);
1448 if (ret)
1449 goto out_unlock;
1450 list_for_each_entry_rcu(child, &parent->children, siblings) {
1451 parent = child;
1452 goto down;
1453
1454up:
1455 continue;
1456 }
1457 ret = (*up)(parent, data);
1458 if (ret)
1459 goto out_unlock;
1460
1461 child = parent;
1462 parent = parent->parent;
1463 if (parent)
1464 goto up;
1465out_unlock:
1466 rcu_read_unlock();
1467
1468 return ret;
1469}
1470
1471static int tg_nop(struct task_group *tg, void *data)
1472{
1473 return 0;
1474}
1475#endif
1476
Gregory Haskinse7693a32008-01-25 21:08:09 +01001477#ifdef CONFIG_SMP
Peter Zijlstraf5f08f32009-09-10 13:35:28 +02001478/* Used instead of source_load when we know the type == 0 */
1479static unsigned long weighted_cpuload(const int cpu)
1480{
1481 return cpu_rq(cpu)->load.weight;
1482}
1483
1484/*
1485 * Return a low guess at the load of a migration-source cpu weighted
1486 * according to the scheduling class and "nice" value.
1487 *
1488 * We want to under-estimate the load of migration sources, to
1489 * balance conservatively.
1490 */
1491static unsigned long source_load(int cpu, int type)
1492{
1493 struct rq *rq = cpu_rq(cpu);
1494 unsigned long total = weighted_cpuload(cpu);
1495
1496 if (type == 0 || !sched_feat(LB_BIAS))
1497 return total;
1498
1499 return min(rq->cpu_load[type-1], total);
1500}
1501
1502/*
1503 * Return a high guess at the load of a migration-target cpu weighted
1504 * according to the scheduling class and "nice" value.
1505 */
1506static unsigned long target_load(int cpu, int type)
1507{
1508 struct rq *rq = cpu_rq(cpu);
1509 unsigned long total = weighted_cpuload(cpu);
1510
1511 if (type == 0 || !sched_feat(LB_BIAS))
1512 return total;
1513
1514 return max(rq->cpu_load[type-1], total);
1515}
1516
Peter Zijlstraae154be2009-09-10 14:40:57 +02001517static unsigned long power_of(int cpu)
1518{
Peter Zijlstrae51fd5e2010-05-31 12:37:30 +02001519 return cpu_rq(cpu)->cpu_power;
Peter Zijlstraae154be2009-09-10 14:40:57 +02001520}
1521
Gregory Haskinse7693a32008-01-25 21:08:09 +01001522static int task_hot(struct task_struct *p, u64 now, struct sched_domain *sd);
Peter Zijlstrac09595f2008-06-27 13:41:14 +02001523
Peter Zijlstraa8a51d52008-06-27 13:41:26 +02001524static unsigned long cpu_avg_load_per_task(int cpu)
1525{
1526 struct rq *rq = cpu_rq(cpu);
Ingo Molnaraf6d5962008-11-29 20:45:15 +01001527 unsigned long nr_running = ACCESS_ONCE(rq->nr_running);
Peter Zijlstraa8a51d52008-06-27 13:41:26 +02001528
Steven Rostedt4cd42622008-11-26 21:04:24 -05001529 if (nr_running)
1530 rq->avg_load_per_task = rq->load.weight / nr_running;
Balbir Singha2d47772008-11-12 16:19:00 +05301531 else
1532 rq->avg_load_per_task = 0;
Peter Zijlstraa8a51d52008-06-27 13:41:26 +02001533
1534 return rq->avg_load_per_task;
1535}
1536
Peter Zijlstrac09595f2008-06-27 13:41:14 +02001537#ifdef CONFIG_FAIR_GROUP_SCHED
1538
Peter Zijlstrac09595f2008-06-27 13:41:14 +02001539/*
Peter Zijlstrac8cba852008-06-27 13:41:23 +02001540 * Compute the cpu's hierarchical load factor for each task group.
1541 * This needs to be done in a top-down fashion because the load of a child
1542 * group is a fraction of its parents load.
Peter Zijlstrac09595f2008-06-27 13:41:14 +02001543 */
Peter Zijlstraeb755802008-08-19 12:33:05 +02001544static int tg_load_down(struct task_group *tg, void *data)
Peter Zijlstrac09595f2008-06-27 13:41:14 +02001545{
Peter Zijlstrac8cba852008-06-27 13:41:23 +02001546 unsigned long load;
Peter Zijlstraeb755802008-08-19 12:33:05 +02001547 long cpu = (long)data;
Peter Zijlstrac09595f2008-06-27 13:41:14 +02001548
Peter Zijlstrac8cba852008-06-27 13:41:23 +02001549 if (!tg->parent) {
1550 load = cpu_rq(cpu)->load.weight;
1551 } else {
1552 load = tg->parent->cfs_rq[cpu]->h_load;
Peter Zijlstra2069dd72010-11-15 15:47:00 -08001553 load *= tg->se[cpu]->load.weight;
Peter Zijlstrac8cba852008-06-27 13:41:23 +02001554 load /= tg->parent->cfs_rq[cpu]->load.weight + 1;
1555 }
1556
1557 tg->cfs_rq[cpu]->h_load = load;
Peter Zijlstrac09595f2008-06-27 13:41:14 +02001558
Peter Zijlstraeb755802008-08-19 12:33:05 +02001559 return 0;
Peter Zijlstra4d8d5952008-06-27 13:41:19 +02001560}
1561
Peter Zijlstraeb755802008-08-19 12:33:05 +02001562static void update_h_load(long cpu)
Peter Zijlstrac09595f2008-06-27 13:41:14 +02001563{
Peter Zijlstraeb755802008-08-19 12:33:05 +02001564 walk_tg_tree(tg_load_down, tg_nop, (void *)cpu);
Peter Zijlstrac09595f2008-06-27 13:41:14 +02001565}
1566
Peter Zijlstrac09595f2008-06-27 13:41:14 +02001567#endif
1568
Gregory Haskins8f45e2b2008-12-29 09:39:51 -05001569#ifdef CONFIG_PREEMPT
1570
Peter Zijlstrab78bb862009-09-15 14:23:18 +02001571static void double_rq_lock(struct rq *rq1, struct rq *rq2);
1572
Alexey Dobriyan70574a92008-11-28 22:08:00 +03001573/*
Gregory Haskins8f45e2b2008-12-29 09:39:51 -05001574 * fair double_lock_balance: Safely acquires both rq->locks in a fair
1575 * way at the expense of forcing extra atomic operations in all
1576 * invocations. This assures that the double_lock is acquired using the
1577 * same underlying policy as the spinlock_t on this architecture, which
1578 * reduces latency compared to the unfair variant below. However, it
1579 * also adds more overhead and therefore may reduce throughput.
Alexey Dobriyan70574a92008-11-28 22:08:00 +03001580 */
Gregory Haskins8f45e2b2008-12-29 09:39:51 -05001581static inline int _double_lock_balance(struct rq *this_rq, struct rq *busiest)
1582 __releases(this_rq->lock)
1583 __acquires(busiest->lock)
1584 __acquires(this_rq->lock)
1585{
Thomas Gleixner05fa7852009-11-17 14:28:38 +01001586 raw_spin_unlock(&this_rq->lock);
Gregory Haskins8f45e2b2008-12-29 09:39:51 -05001587 double_rq_lock(this_rq, busiest);
1588
1589 return 1;
1590}
1591
1592#else
1593/*
1594 * Unfair double_lock_balance: Optimizes throughput at the expense of
1595 * latency by eliminating extra atomic operations when the locks are
1596 * already in proper order on entry. This favors lower cpu-ids and will
1597 * grant the double lock to lower cpus over higher ids under contention,
1598 * regardless of entry order into the function.
1599 */
1600static int _double_lock_balance(struct rq *this_rq, struct rq *busiest)
Alexey Dobriyan70574a92008-11-28 22:08:00 +03001601 __releases(this_rq->lock)
1602 __acquires(busiest->lock)
1603 __acquires(this_rq->lock)
1604{
1605 int ret = 0;
1606
Thomas Gleixner05fa7852009-11-17 14:28:38 +01001607 if (unlikely(!raw_spin_trylock(&busiest->lock))) {
Alexey Dobriyan70574a92008-11-28 22:08:00 +03001608 if (busiest < this_rq) {
Thomas Gleixner05fa7852009-11-17 14:28:38 +01001609 raw_spin_unlock(&this_rq->lock);
1610 raw_spin_lock(&busiest->lock);
1611 raw_spin_lock_nested(&this_rq->lock,
1612 SINGLE_DEPTH_NESTING);
Alexey Dobriyan70574a92008-11-28 22:08:00 +03001613 ret = 1;
1614 } else
Thomas Gleixner05fa7852009-11-17 14:28:38 +01001615 raw_spin_lock_nested(&busiest->lock,
1616 SINGLE_DEPTH_NESTING);
Alexey Dobriyan70574a92008-11-28 22:08:00 +03001617 }
1618 return ret;
1619}
1620
Gregory Haskins8f45e2b2008-12-29 09:39:51 -05001621#endif /* CONFIG_PREEMPT */
1622
1623/*
1624 * double_lock_balance - lock the busiest runqueue, this_rq is locked already.
1625 */
1626static int double_lock_balance(struct rq *this_rq, struct rq *busiest)
1627{
1628 if (unlikely(!irqs_disabled())) {
1629 /* printk() doesn't work good under rq->lock */
Thomas Gleixner05fa7852009-11-17 14:28:38 +01001630 raw_spin_unlock(&this_rq->lock);
Gregory Haskins8f45e2b2008-12-29 09:39:51 -05001631 BUG_ON(1);
1632 }
1633
1634 return _double_lock_balance(this_rq, busiest);
1635}
1636
Alexey Dobriyan70574a92008-11-28 22:08:00 +03001637static inline void double_unlock_balance(struct rq *this_rq, struct rq *busiest)
1638 __releases(busiest->lock)
1639{
Thomas Gleixner05fa7852009-11-17 14:28:38 +01001640 raw_spin_unlock(&busiest->lock);
Alexey Dobriyan70574a92008-11-28 22:08:00 +03001641 lock_set_subclass(&this_rq->lock.dep_map, 0, _RET_IP_);
1642}
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01001643
1644/*
1645 * double_rq_lock - safely lock two runqueues
1646 *
1647 * Note this does not disable interrupts like task_rq_lock,
1648 * you need to do so manually before calling.
1649 */
1650static void double_rq_lock(struct rq *rq1, struct rq *rq2)
1651 __acquires(rq1->lock)
1652 __acquires(rq2->lock)
1653{
1654 BUG_ON(!irqs_disabled());
1655 if (rq1 == rq2) {
1656 raw_spin_lock(&rq1->lock);
1657 __acquire(rq2->lock); /* Fake it out ;) */
1658 } else {
1659 if (rq1 < rq2) {
1660 raw_spin_lock(&rq1->lock);
1661 raw_spin_lock_nested(&rq2->lock, SINGLE_DEPTH_NESTING);
1662 } else {
1663 raw_spin_lock(&rq2->lock);
1664 raw_spin_lock_nested(&rq1->lock, SINGLE_DEPTH_NESTING);
1665 }
1666 }
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01001667}
1668
1669/*
1670 * double_rq_unlock - safely unlock two runqueues
1671 *
1672 * Note this does not restore interrupts like task_rq_unlock,
1673 * you need to do so manually after calling.
1674 */
1675static void double_rq_unlock(struct rq *rq1, struct rq *rq2)
1676 __releases(rq1->lock)
1677 __releases(rq2->lock)
1678{
1679 raw_spin_unlock(&rq1->lock);
1680 if (rq1 != rq2)
1681 raw_spin_unlock(&rq2->lock);
1682 else
1683 __release(rq2->lock);
1684}
1685
Peter Zijlstra18d95a22008-04-19 19:45:00 +02001686#endif
Peter Zijlstra18d95a22008-04-19 19:45:00 +02001687
Peter Zijlstra74f51872010-04-22 21:50:19 +02001688static void calc_load_account_idle(struct rq *this_rq);
Christian Ehrhardt0bcdcf22009-11-30 12:16:46 +01001689static void update_sysctl(void);
Christian Ehrhardtacb4a842009-11-30 12:16:48 +01001690static int get_update_sysctl_factor(void);
Venkatesh Pallipadifdf3e952010-05-17 18:14:43 -07001691static void update_cpu_load(struct rq *this_rq);
Thomas Gleixnerdce48a82009-04-11 10:43:41 +02001692
Peter Zijlstracd29fe62009-11-27 17:32:46 +01001693static inline void __set_task_cpu(struct task_struct *p, unsigned int cpu)
1694{
1695 set_task_rq(p, cpu);
1696#ifdef CONFIG_SMP
1697 /*
1698 * After ->cpu is set up to a new value, task_rq_lock(p, ...) can be
1699 * successfuly executed on another CPU. We must ensure that updates of
1700 * per-task data have been completed by this moment.
1701 */
1702 smp_wmb();
1703 task_thread_info(p)->cpu = cpu;
1704#endif
1705}
Peter Zijlstra18d95a22008-04-19 19:45:00 +02001706
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01001707static const struct sched_class rt_sched_class;
Ingo Molnardd41f592007-07-09 18:51:59 +02001708
Peter Zijlstra34f971f2010-09-22 13:53:15 +02001709#define sched_class_highest (&stop_sched_class)
Gregory Haskins1f11eb62008-06-04 15:04:05 -04001710#define for_each_class(class) \
1711 for (class = sched_class_highest; class; class = class->next)
Ingo Molnardd41f592007-07-09 18:51:59 +02001712
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01001713#include "sched_stats.h"
1714
Peter Zijlstrac09595f2008-06-27 13:41:14 +02001715static void inc_nr_running(struct rq *rq)
Ingo Molnar6363ca52008-05-29 11:28:57 +02001716{
1717 rq->nr_running++;
Ingo Molnar6363ca52008-05-29 11:28:57 +02001718}
1719
Peter Zijlstrac09595f2008-06-27 13:41:14 +02001720static void dec_nr_running(struct rq *rq)
Ingo Molnar9c217242007-08-02 17:41:40 +02001721{
1722 rq->nr_running--;
Ingo Molnar9c217242007-08-02 17:41:40 +02001723}
1724
Ingo Molnar45bf76d2007-07-09 18:51:59 +02001725static void set_load_weight(struct task_struct *p)
1726{
Ingo Molnardd41f592007-07-09 18:51:59 +02001727 /*
1728 * SCHED_IDLE tasks get minimal weight:
1729 */
1730 if (p->policy == SCHED_IDLE) {
1731 p->se.load.weight = WEIGHT_IDLEPRIO;
1732 p->se.load.inv_weight = WMULT_IDLEPRIO;
1733 return;
1734 }
1735
1736 p->se.load.weight = prio_to_weight[p->static_prio - MAX_RT_PRIO];
1737 p->se.load.inv_weight = prio_to_wmult[p->static_prio - MAX_RT_PRIO];
Ingo Molnar45bf76d2007-07-09 18:51:59 +02001738}
1739
Peter Zijlstra371fd7e2010-03-24 16:38:48 +01001740static void enqueue_task(struct rq *rq, struct task_struct *p, int flags)
Gregory Haskins2087a1a2008-06-27 14:30:00 -06001741{
Mike Galbraitha64692a2010-03-11 17:16:20 +01001742 update_rq_clock(rq);
Ingo Molnar71f8bd42007-07-09 18:51:59 +02001743 sched_info_queued(p);
Peter Zijlstra371fd7e2010-03-24 16:38:48 +01001744 p->sched_class->enqueue_task(rq, p, flags);
Ingo Molnardd41f592007-07-09 18:51:59 +02001745 p->se.on_rq = 1;
1746}
1747
Peter Zijlstra371fd7e2010-03-24 16:38:48 +01001748static void dequeue_task(struct rq *rq, struct task_struct *p, int flags)
Ingo Molnardd41f592007-07-09 18:51:59 +02001749{
Mike Galbraitha64692a2010-03-11 17:16:20 +01001750 update_rq_clock(rq);
Ankita Garg46ac22b2008-07-01 14:30:06 +05301751 sched_info_dequeued(p);
Peter Zijlstra371fd7e2010-03-24 16:38:48 +01001752 p->sched_class->dequeue_task(rq, p, flags);
Ingo Molnardd41f592007-07-09 18:51:59 +02001753 p->se.on_rq = 0;
Ingo Molnar71f8bd42007-07-09 18:51:59 +02001754}
1755
1756/*
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01001757 * activate_task - move a task to the runqueue.
1758 */
Peter Zijlstra371fd7e2010-03-24 16:38:48 +01001759static void activate_task(struct rq *rq, struct task_struct *p, int flags)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01001760{
1761 if (task_contributes_to_load(p))
1762 rq->nr_uninterruptible--;
1763
Peter Zijlstra371fd7e2010-03-24 16:38:48 +01001764 enqueue_task(rq, p, flags);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01001765 inc_nr_running(rq);
1766}
1767
1768/*
1769 * deactivate_task - remove a task from the runqueue.
1770 */
Peter Zijlstra371fd7e2010-03-24 16:38:48 +01001771static void deactivate_task(struct rq *rq, struct task_struct *p, int flags)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01001772{
1773 if (task_contributes_to_load(p))
1774 rq->nr_uninterruptible++;
1775
Peter Zijlstra371fd7e2010-03-24 16:38:48 +01001776 dequeue_task(rq, p, flags);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01001777 dec_nr_running(rq);
1778}
1779
Venkatesh Pallipadib52bfee2010-10-04 17:03:19 -07001780#ifdef CONFIG_IRQ_TIME_ACCOUNTING
1781
Venkatesh Pallipadi305e6832010-10-04 17:03:21 -07001782/*
1783 * There are no locks covering percpu hardirq/softirq time.
1784 * They are only modified in account_system_vtime, on corresponding CPU
1785 * with interrupts disabled. So, writes are safe.
1786 * They are read and saved off onto struct rq in update_rq_clock().
1787 * This may result in other CPU reading this CPU's irq time and can
1788 * race with irq/account_system_vtime on this CPU. We would either get old
1789 * or new value (or semi updated value on 32 bit) with a side effect of
1790 * accounting a slice of irq time to wrong task when irq is in progress
1791 * while we read rq->clock. That is a worthy compromise in place of having
1792 * locks on each irq in account_system_time.
1793 */
Venkatesh Pallipadib52bfee2010-10-04 17:03:19 -07001794static DEFINE_PER_CPU(u64, cpu_hardirq_time);
1795static DEFINE_PER_CPU(u64, cpu_softirq_time);
1796
1797static DEFINE_PER_CPU(u64, irq_start_time);
1798static int sched_clock_irqtime;
1799
1800void enable_sched_clock_irqtime(void)
1801{
1802 sched_clock_irqtime = 1;
1803}
1804
1805void disable_sched_clock_irqtime(void)
1806{
1807 sched_clock_irqtime = 0;
1808}
1809
Venkatesh Pallipadi305e6832010-10-04 17:03:21 -07001810static u64 irq_time_cpu(int cpu)
1811{
1812 if (!sched_clock_irqtime)
1813 return 0;
1814
1815 return per_cpu(cpu_softirq_time, cpu) + per_cpu(cpu_hardirq_time, cpu);
1816}
1817
Venkatesh Pallipadib52bfee2010-10-04 17:03:19 -07001818void account_system_vtime(struct task_struct *curr)
1819{
1820 unsigned long flags;
1821 int cpu;
1822 u64 now, delta;
1823
1824 if (!sched_clock_irqtime)
1825 return;
1826
1827 local_irq_save(flags);
1828
Venkatesh Pallipadib52bfee2010-10-04 17:03:19 -07001829 cpu = smp_processor_id();
Venkatesh Pallipadid267f872010-10-04 17:03:23 -07001830 now = sched_clock_cpu(cpu);
Venkatesh Pallipadib52bfee2010-10-04 17:03:19 -07001831 delta = now - per_cpu(irq_start_time, cpu);
1832 per_cpu(irq_start_time, cpu) = now;
1833 /*
1834 * We do not account for softirq time from ksoftirqd here.
1835 * We want to continue accounting softirq time to ksoftirqd thread
1836 * in that case, so as not to confuse scheduler with a special task
1837 * that do not consume any time, but still wants to run.
1838 */
1839 if (hardirq_count())
1840 per_cpu(cpu_hardirq_time, cpu) += delta;
1841 else if (in_serving_softirq() && !(curr->flags & PF_KSOFTIRQD))
1842 per_cpu(cpu_softirq_time, cpu) += delta;
1843
1844 local_irq_restore(flags);
1845}
Ingo Molnarb7dadc32010-10-18 20:00:37 +02001846EXPORT_SYMBOL_GPL(account_system_vtime);
Venkatesh Pallipadib52bfee2010-10-04 17:03:19 -07001847
Venkatesh Pallipadiaa483802010-10-04 17:03:22 -07001848static void sched_irq_time_avg_update(struct rq *rq, u64 curr_irq_time)
1849{
1850 if (sched_clock_irqtime && sched_feat(NONIRQ_POWER)) {
1851 u64 delta_irq = curr_irq_time - rq->prev_irq_time;
1852 rq->prev_irq_time = curr_irq_time;
1853 sched_rt_avg_update(rq, delta_irq);
1854 }
1855}
1856
Venkatesh Pallipadi305e6832010-10-04 17:03:21 -07001857#else
1858
1859static u64 irq_time_cpu(int cpu)
1860{
1861 return 0;
1862}
1863
Venkatesh Pallipadiaa483802010-10-04 17:03:22 -07001864static void sched_irq_time_avg_update(struct rq *rq, u64 curr_irq_time) { }
1865
Venkatesh Pallipadib52bfee2010-10-04 17:03:19 -07001866#endif
1867
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01001868#include "sched_idletask.c"
1869#include "sched_fair.c"
1870#include "sched_rt.c"
Peter Zijlstra34f971f2010-09-22 13:53:15 +02001871#include "sched_stoptask.c"
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01001872#ifdef CONFIG_SCHED_DEBUG
1873# include "sched_debug.c"
1874#endif
1875
Peter Zijlstra34f971f2010-09-22 13:53:15 +02001876void sched_set_stop_task(int cpu, struct task_struct *stop)
1877{
1878 struct sched_param param = { .sched_priority = MAX_RT_PRIO - 1 };
1879 struct task_struct *old_stop = cpu_rq(cpu)->stop;
1880
1881 if (stop) {
1882 /*
1883 * Make it appear like a SCHED_FIFO task, its something
1884 * userspace knows about and won't get confused about.
1885 *
1886 * Also, it will make PI more or less work without too
1887 * much confusion -- but then, stop work should not
1888 * rely on PI working anyway.
1889 */
1890 sched_setscheduler_nocheck(stop, SCHED_FIFO, &param);
1891
1892 stop->sched_class = &stop_sched_class;
1893 }
1894
1895 cpu_rq(cpu)->stop = stop;
1896
1897 if (old_stop) {
1898 /*
1899 * Reset it back to a normal scheduling class so that
1900 * it can die in pieces.
1901 */
1902 old_stop->sched_class = &rt_sched_class;
1903 }
1904}
1905
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01001906/*
Ingo Molnardd41f592007-07-09 18:51:59 +02001907 * __normal_prio - return the priority that is based on the static prio
Ingo Molnar71f8bd42007-07-09 18:51:59 +02001908 */
Ingo Molnar14531182007-07-09 18:51:59 +02001909static inline int __normal_prio(struct task_struct *p)
1910{
Ingo Molnardd41f592007-07-09 18:51:59 +02001911 return p->static_prio;
Ingo Molnar14531182007-07-09 18:51:59 +02001912}
1913
1914/*
Ingo Molnarb29739f2006-06-27 02:54:51 -07001915 * Calculate the expected normal priority: i.e. priority
1916 * without taking RT-inheritance into account. Might be
1917 * boosted by interactivity modifiers. Changes upon fork,
1918 * setprio syscalls, and whenever the interactivity
1919 * estimator recalculates.
1920 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07001921static inline int normal_prio(struct task_struct *p)
Ingo Molnarb29739f2006-06-27 02:54:51 -07001922{
1923 int prio;
1924
Ingo Molnare05606d2007-07-09 18:51:59 +02001925 if (task_has_rt_policy(p))
Ingo Molnarb29739f2006-06-27 02:54:51 -07001926 prio = MAX_RT_PRIO-1 - p->rt_priority;
1927 else
1928 prio = __normal_prio(p);
1929 return prio;
1930}
1931
1932/*
1933 * Calculate the current priority, i.e. the priority
1934 * taken into account by the scheduler. This value might
1935 * be boosted by RT tasks, or might be boosted by
1936 * interactivity modifiers. Will be RT if the task got
1937 * RT-boosted. If not then it returns p->normal_prio.
1938 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07001939static int effective_prio(struct task_struct *p)
Ingo Molnarb29739f2006-06-27 02:54:51 -07001940{
1941 p->normal_prio = normal_prio(p);
1942 /*
1943 * If we are RT tasks or we were boosted to RT priority,
1944 * keep the priority unchanged. Otherwise, update priority
1945 * to the normal priority:
1946 */
1947 if (!rt_prio(p->prio))
1948 return p->normal_prio;
1949 return p->prio;
1950}
1951
Linus Torvalds1da177e2005-04-16 15:20:36 -07001952/**
1953 * task_curr - is this task currently executing on a CPU?
1954 * @p: the task in question.
1955 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07001956inline int task_curr(const struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001957{
1958 return cpu_curr(task_cpu(p)) == p;
1959}
1960
Steven Rostedtcb469842008-01-25 21:08:22 +01001961static inline void check_class_changed(struct rq *rq, struct task_struct *p,
1962 const struct sched_class *prev_class,
1963 int oldprio, int running)
1964{
1965 if (prev_class != p->sched_class) {
1966 if (prev_class->switched_from)
1967 prev_class->switched_from(rq, p, running);
1968 p->sched_class->switched_to(rq, p, running);
1969 } else
1970 p->sched_class->prio_changed(rq, p, oldprio, running);
1971}
1972
Peter Zijlstra1e5a7402010-10-31 12:37:04 +01001973static void check_preempt_curr(struct rq *rq, struct task_struct *p, int flags)
1974{
1975 const struct sched_class *class;
1976
1977 if (p->sched_class == rq->curr->sched_class) {
1978 rq->curr->sched_class->check_preempt_curr(rq, p, flags);
1979 } else {
1980 for_each_class(class) {
1981 if (class == rq->curr->sched_class)
1982 break;
1983 if (class == p->sched_class) {
1984 resched_task(rq->curr);
1985 break;
1986 }
1987 }
1988 }
1989
1990 /*
1991 * A queue event has occurred, and we're going to schedule. In
1992 * this case, we can save a useless back to back clock update.
1993 */
1994 if (test_tsk_need_resched(rq->curr))
1995 rq->skip_clock_update = 1;
1996}
1997
Linus Torvalds1da177e2005-04-16 15:20:36 -07001998#ifdef CONFIG_SMP
Ingo Molnarcc367732007-10-15 17:00:18 +02001999/*
2000 * Is this task likely cache-hot:
2001 */
Gregory Haskinse7693a32008-01-25 21:08:09 +01002002static int
Ingo Molnarcc367732007-10-15 17:00:18 +02002003task_hot(struct task_struct *p, u64 now, struct sched_domain *sd)
2004{
2005 s64 delta;
2006
Peter Zijlstrae6c8fba2009-12-16 18:04:33 +01002007 if (p->sched_class != &fair_sched_class)
2008 return 0;
2009
Nikhil Raoef8002f2010-10-13 12:09:35 -07002010 if (unlikely(p->policy == SCHED_IDLE))
2011 return 0;
2012
Ingo Molnarf540a602008-03-15 17:10:34 +01002013 /*
2014 * Buddy candidates are cache hot:
2015 */
Mike Galbraithf685cea2009-10-23 23:09:22 +02002016 if (sched_feat(CACHE_HOT_BUDDY) && this_rq()->nr_running &&
Peter Zijlstra47932412008-11-04 21:25:09 +01002017 (&p->se == cfs_rq_of(&p->se)->next ||
2018 &p->se == cfs_rq_of(&p->se)->last))
Ingo Molnarf540a602008-03-15 17:10:34 +01002019 return 1;
2020
Ingo Molnar6bc16652007-10-15 17:00:18 +02002021 if (sysctl_sched_migration_cost == -1)
2022 return 1;
2023 if (sysctl_sched_migration_cost == 0)
2024 return 0;
2025
Ingo Molnarcc367732007-10-15 17:00:18 +02002026 delta = now - p->se.exec_start;
2027
2028 return delta < (s64)sysctl_sched_migration_cost;
2029}
2030
Ingo Molnardd41f592007-07-09 18:51:59 +02002031void set_task_cpu(struct task_struct *p, unsigned int new_cpu)
Ingo Molnarc65cc872007-07-09 18:51:58 +02002032{
Peter Zijlstrae2912002009-12-16 18:04:36 +01002033#ifdef CONFIG_SCHED_DEBUG
2034 /*
2035 * We should never call set_task_cpu() on a blocked task,
2036 * ttwu() will sort out the placement.
2037 */
Peter Zijlstra077614e2009-12-17 13:16:31 +01002038 WARN_ON_ONCE(p->state != TASK_RUNNING && p->state != TASK_WAKING &&
2039 !(task_thread_info(p)->preempt_count & PREEMPT_ACTIVE));
Peter Zijlstrae2912002009-12-16 18:04:36 +01002040#endif
2041
Mathieu Desnoyersde1d7282009-05-05 16:49:59 +08002042 trace_sched_migrate_task(p, new_cpu);
Peter Zijlstracbc34ed2008-12-10 08:08:22 +01002043
Peter Zijlstra0c697742009-12-22 15:43:19 +01002044 if (task_cpu(p) != new_cpu) {
2045 p->se.nr_migrations++;
2046 perf_sw_event(PERF_COUNT_SW_CPU_MIGRATIONS, 1, 1, NULL, 0);
2047 }
Ingo Molnardd41f592007-07-09 18:51:59 +02002048
2049 __set_task_cpu(p, new_cpu);
Ingo Molnarc65cc872007-07-09 18:51:58 +02002050}
2051
Tejun Heo969c7922010-05-06 18:49:21 +02002052struct migration_arg {
Ingo Molnar36c8b582006-07-03 00:25:41 -07002053 struct task_struct *task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002054 int dest_cpu;
Ingo Molnar70b97a72006-07-03 00:25:42 -07002055};
Linus Torvalds1da177e2005-04-16 15:20:36 -07002056
Tejun Heo969c7922010-05-06 18:49:21 +02002057static int migration_cpu_stop(void *data);
2058
Linus Torvalds1da177e2005-04-16 15:20:36 -07002059/*
2060 * The task's runqueue lock must be held.
2061 * Returns true if you have to wait for migration thread.
2062 */
Tejun Heo969c7922010-05-06 18:49:21 +02002063static bool migrate_task(struct task_struct *p, int dest_cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002064{
Ingo Molnar70b97a72006-07-03 00:25:42 -07002065 struct rq *rq = task_rq(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002066
2067 /*
2068 * If the task is not on a runqueue (and not running), then
Peter Zijlstrae2912002009-12-16 18:04:36 +01002069 * the next wake-up will properly place the task.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002070 */
Tejun Heo969c7922010-05-06 18:49:21 +02002071 return p->se.on_rq || task_running(rq, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002072}
2073
2074/*
2075 * wait_task_inactive - wait for a thread to unschedule.
2076 *
Roland McGrath85ba2d82008-07-25 19:45:58 -07002077 * If @match_state is nonzero, it's the @p->state value just checked and
2078 * not expected to change. If it changes, i.e. @p might have woken up,
2079 * then return zero. When we succeed in waiting for @p to be off its CPU,
2080 * we return a positive number (its total switch count). If a second call
2081 * a short while later returns the same number, the caller can be sure that
2082 * @p has remained unscheduled the whole time.
2083 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002084 * The caller must ensure that the task *will* unschedule sometime soon,
2085 * else this function might spin for a *long* time. This function can't
2086 * be called with interrupts off, or it may introduce deadlock with
2087 * smp_call_function() if an IPI is sent by the same process we are
2088 * waiting to become inactive.
2089 */
Roland McGrath85ba2d82008-07-25 19:45:58 -07002090unsigned long wait_task_inactive(struct task_struct *p, long match_state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002091{
2092 unsigned long flags;
Ingo Molnardd41f592007-07-09 18:51:59 +02002093 int running, on_rq;
Roland McGrath85ba2d82008-07-25 19:45:58 -07002094 unsigned long ncsw;
Ingo Molnar70b97a72006-07-03 00:25:42 -07002095 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002096
Andi Kleen3a5c3592007-10-15 17:00:14 +02002097 for (;;) {
2098 /*
2099 * We do the initial early heuristics without holding
2100 * any task-queue locks at all. We'll only try to get
2101 * the runqueue lock when things look like they will
2102 * work out!
2103 */
2104 rq = task_rq(p);
Linus Torvaldsfa490cf2007-06-18 09:34:40 -07002105
Andi Kleen3a5c3592007-10-15 17:00:14 +02002106 /*
2107 * If the task is actively running on another CPU
2108 * still, just relax and busy-wait without holding
2109 * any locks.
2110 *
2111 * NOTE! Since we don't hold any locks, it's not
2112 * even sure that "rq" stays as the right runqueue!
2113 * But we don't care, since "task_running()" will
2114 * return false if the runqueue has changed and p
2115 * is actually now running somewhere else!
2116 */
Roland McGrath85ba2d82008-07-25 19:45:58 -07002117 while (task_running(rq, p)) {
2118 if (match_state && unlikely(p->state != match_state))
2119 return 0;
Andi Kleen3a5c3592007-10-15 17:00:14 +02002120 cpu_relax();
Roland McGrath85ba2d82008-07-25 19:45:58 -07002121 }
Linus Torvaldsfa490cf2007-06-18 09:34:40 -07002122
Andi Kleen3a5c3592007-10-15 17:00:14 +02002123 /*
2124 * Ok, time to look more closely! We need the rq
2125 * lock now, to be *sure*. If we're wrong, we'll
2126 * just go back and repeat.
2127 */
2128 rq = task_rq_lock(p, &flags);
Peter Zijlstra27a9da62010-05-04 20:36:56 +02002129 trace_sched_wait_task(p);
Andi Kleen3a5c3592007-10-15 17:00:14 +02002130 running = task_running(rq, p);
2131 on_rq = p->se.on_rq;
Roland McGrath85ba2d82008-07-25 19:45:58 -07002132 ncsw = 0;
Oleg Nesterovf31e11d2008-08-20 16:54:44 -07002133 if (!match_state || p->state == match_state)
Oleg Nesterov93dcf552008-08-20 16:54:44 -07002134 ncsw = p->nvcsw | LONG_MIN; /* sets MSB */
Andi Kleen3a5c3592007-10-15 17:00:14 +02002135 task_rq_unlock(rq, &flags);
Linus Torvaldsfa490cf2007-06-18 09:34:40 -07002136
Andi Kleen3a5c3592007-10-15 17:00:14 +02002137 /*
Roland McGrath85ba2d82008-07-25 19:45:58 -07002138 * If it changed from the expected state, bail out now.
2139 */
2140 if (unlikely(!ncsw))
2141 break;
2142
2143 /*
Andi Kleen3a5c3592007-10-15 17:00:14 +02002144 * Was it really running after all now that we
2145 * checked with the proper locks actually held?
2146 *
2147 * Oops. Go back and try again..
2148 */
2149 if (unlikely(running)) {
2150 cpu_relax();
2151 continue;
2152 }
2153
2154 /*
2155 * It's not enough that it's not actively running,
2156 * it must be off the runqueue _entirely_, and not
2157 * preempted!
2158 *
Luis Henriques80dd99b2009-03-16 19:58:09 +00002159 * So if it was still runnable (but just not actively
Andi Kleen3a5c3592007-10-15 17:00:14 +02002160 * running right now), it's preempted, and we should
2161 * yield - it could be a while.
2162 */
2163 if (unlikely(on_rq)) {
2164 schedule_timeout_uninterruptible(1);
2165 continue;
2166 }
2167
2168 /*
2169 * Ahh, all good. It wasn't running, and it wasn't
2170 * runnable, which means that it will never become
2171 * running in the future either. We're all done!
2172 */
2173 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002174 }
Roland McGrath85ba2d82008-07-25 19:45:58 -07002175
2176 return ncsw;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002177}
2178
2179/***
2180 * kick_process - kick a running thread to enter/exit the kernel
2181 * @p: the to-be-kicked thread
2182 *
2183 * Cause a process which is running on another CPU to enter
2184 * kernel-mode, without any delay. (to get signals handled.)
2185 *
2186 * NOTE: this function doesnt have to take the runqueue lock,
2187 * because all it wants to ensure is that the remote task enters
2188 * the kernel. If the IPI races and the task has been migrated
2189 * to another CPU then no harm is done and the purpose has been
2190 * achieved as well.
2191 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07002192void kick_process(struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002193{
2194 int cpu;
2195
2196 preempt_disable();
2197 cpu = task_cpu(p);
2198 if ((cpu != smp_processor_id()) && task_curr(p))
2199 smp_send_reschedule(cpu);
2200 preempt_enable();
2201}
Rusty Russellb43e3522009-06-12 22:27:00 -06002202EXPORT_SYMBOL_GPL(kick_process);
Nick Piggin476d1392005-06-25 14:57:29 -07002203#endif /* CONFIG_SMP */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002204
Thomas Gleixner0793a612008-12-04 20:12:29 +01002205/**
2206 * task_oncpu_function_call - call a function on the cpu on which a task runs
2207 * @p: the task to evaluate
2208 * @func: the function to be called
2209 * @info: the function call argument
2210 *
2211 * Calls the function @func when the task is currently running. This might
2212 * be on the current CPU, which just calls the function directly
2213 */
2214void task_oncpu_function_call(struct task_struct *p,
2215 void (*func) (void *info), void *info)
2216{
2217 int cpu;
2218
2219 preempt_disable();
2220 cpu = task_cpu(p);
2221 if (task_curr(p))
2222 smp_call_function_single(cpu, func, info, 1);
2223 preempt_enable();
2224}
2225
Peter Zijlstra970b13b2009-11-25 13:31:39 +01002226#ifdef CONFIG_SMP
Oleg Nesterov30da6882010-03-15 10:10:19 +01002227/*
2228 * ->cpus_allowed is protected by either TASK_WAKING or rq->lock held.
2229 */
Peter Zijlstra5da9a0f2009-12-16 18:04:38 +01002230static int select_fallback_rq(int cpu, struct task_struct *p)
2231{
2232 int dest_cpu;
2233 const struct cpumask *nodemask = cpumask_of_node(cpu_to_node(cpu));
2234
2235 /* Look for allowed, online CPU in same node. */
2236 for_each_cpu_and(dest_cpu, nodemask, cpu_active_mask)
2237 if (cpumask_test_cpu(dest_cpu, &p->cpus_allowed))
2238 return dest_cpu;
2239
2240 /* Any allowed, online CPU? */
2241 dest_cpu = cpumask_any_and(&p->cpus_allowed, cpu_active_mask);
2242 if (dest_cpu < nr_cpu_ids)
2243 return dest_cpu;
2244
2245 /* No more Mr. Nice Guy. */
Peter Zijlstra48c5cca2010-11-13 19:32:29 +01002246 dest_cpu = cpuset_cpus_allowed_fallback(p);
2247 /*
2248 * Don't tell them about moving exiting tasks or
2249 * kernel threads (both mm NULL), since they never
2250 * leave kernel.
2251 */
2252 if (p->mm && printk_ratelimit()) {
2253 printk(KERN_INFO "process %d (%s) no longer affine to cpu%d\n",
2254 task_pid_nr(p), p->comm, cpu);
Peter Zijlstra5da9a0f2009-12-16 18:04:38 +01002255 }
2256
2257 return dest_cpu;
2258}
2259
Peter Zijlstrae2912002009-12-16 18:04:36 +01002260/*
Oleg Nesterov30da6882010-03-15 10:10:19 +01002261 * The caller (fork, wakeup) owns TASK_WAKING, ->cpus_allowed is stable.
Peter Zijlstrae2912002009-12-16 18:04:36 +01002262 */
Peter Zijlstra970b13b2009-11-25 13:31:39 +01002263static inline
Peter Zijlstra0017d732010-03-24 18:34:10 +01002264int select_task_rq(struct rq *rq, struct task_struct *p, int sd_flags, int wake_flags)
Peter Zijlstra970b13b2009-11-25 13:31:39 +01002265{
Peter Zijlstra0017d732010-03-24 18:34:10 +01002266 int cpu = p->sched_class->select_task_rq(rq, p, sd_flags, wake_flags);
Peter Zijlstrae2912002009-12-16 18:04:36 +01002267
2268 /*
2269 * In order not to call set_task_cpu() on a blocking task we need
2270 * to rely on ttwu() to place the task on a valid ->cpus_allowed
2271 * cpu.
2272 *
2273 * Since this is common to all placement strategies, this lives here.
2274 *
2275 * [ this allows ->select_task() to simply return task_cpu(p) and
2276 * not worry about this generic constraint ]
2277 */
2278 if (unlikely(!cpumask_test_cpu(cpu, &p->cpus_allowed) ||
Peter Zijlstra70f11202009-12-20 17:36:27 +01002279 !cpu_online(cpu)))
Peter Zijlstra5da9a0f2009-12-16 18:04:38 +01002280 cpu = select_fallback_rq(task_cpu(p), p);
Peter Zijlstrae2912002009-12-16 18:04:36 +01002281
2282 return cpu;
Peter Zijlstra970b13b2009-11-25 13:31:39 +01002283}
Mike Galbraith09a40af2010-04-15 07:29:59 +02002284
2285static void update_avg(u64 *avg, u64 sample)
2286{
2287 s64 diff = sample - *avg;
2288 *avg += diff >> 3;
2289}
Peter Zijlstra970b13b2009-11-25 13:31:39 +01002290#endif
2291
Tejun Heo9ed38112009-12-03 15:08:03 +09002292static inline void ttwu_activate(struct task_struct *p, struct rq *rq,
2293 bool is_sync, bool is_migrate, bool is_local,
2294 unsigned long en_flags)
2295{
2296 schedstat_inc(p, se.statistics.nr_wakeups);
2297 if (is_sync)
2298 schedstat_inc(p, se.statistics.nr_wakeups_sync);
2299 if (is_migrate)
2300 schedstat_inc(p, se.statistics.nr_wakeups_migrate);
2301 if (is_local)
2302 schedstat_inc(p, se.statistics.nr_wakeups_local);
2303 else
2304 schedstat_inc(p, se.statistics.nr_wakeups_remote);
2305
2306 activate_task(rq, p, en_flags);
2307}
2308
2309static inline void ttwu_post_activation(struct task_struct *p, struct rq *rq,
2310 int wake_flags, bool success)
2311{
2312 trace_sched_wakeup(p, success);
2313 check_preempt_curr(rq, p, wake_flags);
2314
2315 p->state = TASK_RUNNING;
2316#ifdef CONFIG_SMP
2317 if (p->sched_class->task_woken)
2318 p->sched_class->task_woken(rq, p);
2319
2320 if (unlikely(rq->idle_stamp)) {
2321 u64 delta = rq->clock - rq->idle_stamp;
2322 u64 max = 2*sysctl_sched_migration_cost;
2323
2324 if (delta > max)
2325 rq->avg_idle = max;
2326 else
2327 update_avg(&rq->avg_idle, delta);
2328 rq->idle_stamp = 0;
2329 }
2330#endif
Tejun Heo21aa9af2010-06-08 21:40:37 +02002331 /* if a worker is waking up, notify workqueue */
2332 if ((p->flags & PF_WQ_WORKER) && success)
2333 wq_worker_waking_up(p, cpu_of(rq));
Tejun Heo9ed38112009-12-03 15:08:03 +09002334}
2335
2336/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07002337 * try_to_wake_up - wake up a thread
Tejun Heo9ed38112009-12-03 15:08:03 +09002338 * @p: the thread to be awakened
Linus Torvalds1da177e2005-04-16 15:20:36 -07002339 * @state: the mask of task states that can be woken
Tejun Heo9ed38112009-12-03 15:08:03 +09002340 * @wake_flags: wake modifier flags (WF_*)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002341 *
2342 * Put it on the run-queue if it's not already there. The "current"
2343 * thread is always on the run-queue (except when the actual
2344 * re-schedule is in progress), and as such you're allowed to do
2345 * the simpler "current->state = TASK_RUNNING" to mark yourself
2346 * runnable without the overhead of this.
2347 *
Tejun Heo9ed38112009-12-03 15:08:03 +09002348 * Returns %true if @p was woken up, %false if it was already running
2349 * or @state didn't match @p's state.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002350 */
Peter Zijlstra7d478722009-09-14 19:55:44 +02002351static int try_to_wake_up(struct task_struct *p, unsigned int state,
2352 int wake_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002353{
Ingo Molnarcc367732007-10-15 17:00:18 +02002354 int cpu, orig_cpu, this_cpu, success = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002355 unsigned long flags;
Peter Zijlstra371fd7e2010-03-24 16:38:48 +01002356 unsigned long en_flags = ENQUEUE_WAKEUP;
Dan Carpenterab3b3aa2010-03-06 14:17:52 +03002357 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002358
Peter Zijlstrae9c84312009-09-15 14:43:03 +02002359 this_cpu = get_cpu();
Peter Zijlstra2398f2c2008-06-27 13:41:35 +02002360
Linus Torvalds04e2f172008-02-23 18:05:03 -08002361 smp_wmb();
Dan Carpenterab3b3aa2010-03-06 14:17:52 +03002362 rq = task_rq_lock(p, &flags);
Peter Zijlstrae9c84312009-09-15 14:43:03 +02002363 if (!(p->state & state))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002364 goto out;
2365
Ingo Molnardd41f592007-07-09 18:51:59 +02002366 if (p->se.on_rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002367 goto out_running;
2368
2369 cpu = task_cpu(p);
Ingo Molnarcc367732007-10-15 17:00:18 +02002370 orig_cpu = cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002371
2372#ifdef CONFIG_SMP
2373 if (unlikely(task_running(rq, p)))
2374 goto out_activate;
2375
Peter Zijlstrae9c84312009-09-15 14:43:03 +02002376 /*
2377 * In order to handle concurrent wakeups and release the rq->lock
2378 * we put the task in TASK_WAKING state.
Ingo Molnareb240732009-09-16 21:09:13 +02002379 *
2380 * First fix up the nr_uninterruptible count:
Peter Zijlstrae9c84312009-09-15 14:43:03 +02002381 */
Peter Zijlstracc87f762010-03-26 12:22:14 +01002382 if (task_contributes_to_load(p)) {
2383 if (likely(cpu_online(orig_cpu)))
2384 rq->nr_uninterruptible--;
2385 else
2386 this_rq()->nr_uninterruptible--;
2387 }
Peter Zijlstrae9c84312009-09-15 14:43:03 +02002388 p->state = TASK_WAKING;
Peter Zijlstraefbbd052009-12-16 18:04:40 +01002389
Peter Zijlstra371fd7e2010-03-24 16:38:48 +01002390 if (p->sched_class->task_waking) {
Peter Zijlstraefbbd052009-12-16 18:04:40 +01002391 p->sched_class->task_waking(rq, p);
Peter Zijlstra371fd7e2010-03-24 16:38:48 +01002392 en_flags |= ENQUEUE_WAKING;
Peter Zijlstra0970d292010-02-15 14:45:54 +01002393 }
Peter Zijlstraab19cb22009-11-27 15:44:43 +01002394
Peter Zijlstra0017d732010-03-24 18:34:10 +01002395 cpu = select_task_rq(rq, p, SD_BALANCE_WAKE, wake_flags);
2396 if (cpu != orig_cpu)
Mike Galbraithf5dc3752009-10-09 08:35:03 +02002397 set_task_cpu(p, cpu);
Peter Zijlstra0017d732010-03-24 18:34:10 +01002398 __task_rq_unlock(rq);
Peter Zijlstraab19cb22009-11-27 15:44:43 +01002399
Peter Zijlstra0970d292010-02-15 14:45:54 +01002400 rq = cpu_rq(cpu);
2401 raw_spin_lock(&rq->lock);
Mike Galbraithf5dc3752009-10-09 08:35:03 +02002402
Peter Zijlstra0970d292010-02-15 14:45:54 +01002403 /*
2404 * We migrated the task without holding either rq->lock, however
2405 * since the task is not on the task list itself, nobody else
2406 * will try and migrate the task, hence the rq should match the
2407 * cpu we just moved it to.
2408 */
2409 WARN_ON(task_cpu(p) != cpu);
Peter Zijlstrae9c84312009-09-15 14:43:03 +02002410 WARN_ON(p->state != TASK_WAKING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002411
Gregory Haskinse7693a32008-01-25 21:08:09 +01002412#ifdef CONFIG_SCHEDSTATS
2413 schedstat_inc(rq, ttwu_count);
2414 if (cpu == this_cpu)
2415 schedstat_inc(rq, ttwu_local);
2416 else {
2417 struct sched_domain *sd;
2418 for_each_domain(this_cpu, sd) {
Rusty Russell758b2cd2008-11-25 02:35:04 +10302419 if (cpumask_test_cpu(cpu, sched_domain_span(sd))) {
Gregory Haskinse7693a32008-01-25 21:08:09 +01002420 schedstat_inc(sd, ttwu_wake_remote);
2421 break;
2422 }
2423 }
2424 }
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +02002425#endif /* CONFIG_SCHEDSTATS */
Gregory Haskinse7693a32008-01-25 21:08:09 +01002426
Linus Torvalds1da177e2005-04-16 15:20:36 -07002427out_activate:
2428#endif /* CONFIG_SMP */
Tejun Heo9ed38112009-12-03 15:08:03 +09002429 ttwu_activate(p, rq, wake_flags & WF_SYNC, orig_cpu != cpu,
2430 cpu == this_cpu, en_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002431 success = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002432out_running:
Tejun Heo9ed38112009-12-03 15:08:03 +09002433 ttwu_post_activation(p, rq, wake_flags, success);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002434out:
2435 task_rq_unlock(rq, &flags);
Peter Zijlstrae9c84312009-09-15 14:43:03 +02002436 put_cpu();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002437
2438 return success;
2439}
2440
David Howells50fa6102009-04-28 15:01:38 +01002441/**
Tejun Heo21aa9af2010-06-08 21:40:37 +02002442 * try_to_wake_up_local - try to wake up a local task with rq lock held
2443 * @p: the thread to be awakened
2444 *
2445 * Put @p on the run-queue if it's not alredy there. The caller must
2446 * ensure that this_rq() is locked, @p is bound to this_rq() and not
2447 * the current task. this_rq() stays locked over invocation.
2448 */
2449static void try_to_wake_up_local(struct task_struct *p)
2450{
2451 struct rq *rq = task_rq(p);
2452 bool success = false;
2453
2454 BUG_ON(rq != this_rq());
2455 BUG_ON(p == current);
2456 lockdep_assert_held(&rq->lock);
2457
2458 if (!(p->state & TASK_NORMAL))
2459 return;
2460
2461 if (!p->se.on_rq) {
2462 if (likely(!task_running(rq, p))) {
2463 schedstat_inc(rq, ttwu_count);
2464 schedstat_inc(rq, ttwu_local);
2465 }
2466 ttwu_activate(p, rq, false, false, true, ENQUEUE_WAKEUP);
2467 success = true;
2468 }
2469 ttwu_post_activation(p, rq, 0, success);
2470}
2471
2472/**
David Howells50fa6102009-04-28 15:01:38 +01002473 * wake_up_process - Wake up a specific process
2474 * @p: The process to be woken up.
2475 *
2476 * Attempt to wake up the nominated process and move it to the set of runnable
2477 * processes. Returns 1 if the process was woken up, 0 if it was already
2478 * running.
2479 *
2480 * It may be assumed that this function implies a write memory barrier before
2481 * changing the task state if and only if any tasks are woken up.
2482 */
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -08002483int wake_up_process(struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002484{
Matthew Wilcoxd9514f62007-12-06 11:07:07 -05002485 return try_to_wake_up(p, TASK_ALL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002486}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002487EXPORT_SYMBOL(wake_up_process);
2488
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -08002489int wake_up_state(struct task_struct *p, unsigned int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002490{
2491 return try_to_wake_up(p, state, 0);
2492}
2493
Linus Torvalds1da177e2005-04-16 15:20:36 -07002494/*
2495 * Perform scheduler related setup for a newly forked process p.
2496 * p is forked by current.
Ingo Molnardd41f592007-07-09 18:51:59 +02002497 *
2498 * __sched_fork() is basic setup used by init_idle() too:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002499 */
Ingo Molnardd41f592007-07-09 18:51:59 +02002500static void __sched_fork(struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002501{
Ingo Molnardd41f592007-07-09 18:51:59 +02002502 p->se.exec_start = 0;
2503 p->se.sum_exec_runtime = 0;
Ingo Molnarf6cf8912007-08-28 12:53:24 +02002504 p->se.prev_sum_exec_runtime = 0;
Ingo Molnar6c594c22008-12-14 12:34:15 +01002505 p->se.nr_migrations = 0;
Ingo Molnar6cfb0d52007-08-02 17:41:40 +02002506
2507#ifdef CONFIG_SCHEDSTATS
Lucas De Marchi41acab82010-03-10 23:37:45 -03002508 memset(&p->se.statistics, 0, sizeof(p->se.statistics));
Ingo Molnar6cfb0d52007-08-02 17:41:40 +02002509#endif
Nick Piggin476d1392005-06-25 14:57:29 -07002510
Peter Zijlstrafa717062008-01-25 21:08:27 +01002511 INIT_LIST_HEAD(&p->rt.run_list);
Ingo Molnardd41f592007-07-09 18:51:59 +02002512 p->se.on_rq = 0;
Peter Zijlstra4a55bd52008-04-19 19:45:00 +02002513 INIT_LIST_HEAD(&p->se.group_node);
Nick Piggin476d1392005-06-25 14:57:29 -07002514
Avi Kivitye107be32007-07-26 13:40:43 +02002515#ifdef CONFIG_PREEMPT_NOTIFIERS
2516 INIT_HLIST_HEAD(&p->preempt_notifiers);
2517#endif
Ingo Molnardd41f592007-07-09 18:51:59 +02002518}
2519
2520/*
2521 * fork()/clone()-time setup:
2522 */
2523void sched_fork(struct task_struct *p, int clone_flags)
2524{
2525 int cpu = get_cpu();
2526
2527 __sched_fork(p);
Peter Zijlstra06b83b52009-12-16 18:04:35 +01002528 /*
Peter Zijlstra0017d732010-03-24 18:34:10 +01002529 * We mark the process as running here. This guarantees that
Peter Zijlstra06b83b52009-12-16 18:04:35 +01002530 * nobody will actually run it, and a signal or other external
2531 * event cannot wake it up and insert it on the runqueue either.
2532 */
Peter Zijlstra0017d732010-03-24 18:34:10 +01002533 p->state = TASK_RUNNING;
Ingo Molnardd41f592007-07-09 18:51:59 +02002534
Ingo Molnarb29739f2006-06-27 02:54:51 -07002535 /*
Mike Galbraithb9dc29e2009-06-17 10:46:01 +02002536 * Revert to default priority/policy on fork if requested.
2537 */
2538 if (unlikely(p->sched_reset_on_fork)) {
Peter Williamsf83f9ac2009-09-24 06:47:10 +00002539 if (p->policy == SCHED_FIFO || p->policy == SCHED_RR) {
Mike Galbraithb9dc29e2009-06-17 10:46:01 +02002540 p->policy = SCHED_NORMAL;
Peter Williamsf83f9ac2009-09-24 06:47:10 +00002541 p->normal_prio = p->static_prio;
2542 }
Mike Galbraithb9dc29e2009-06-17 10:46:01 +02002543
Mike Galbraith6c697bd2009-06-17 10:48:02 +02002544 if (PRIO_TO_NICE(p->static_prio) < 0) {
2545 p->static_prio = NICE_TO_PRIO(0);
Peter Williamsf83f9ac2009-09-24 06:47:10 +00002546 p->normal_prio = p->static_prio;
Mike Galbraith6c697bd2009-06-17 10:48:02 +02002547 set_load_weight(p);
2548 }
2549
Mike Galbraithb9dc29e2009-06-17 10:46:01 +02002550 /*
2551 * We don't need the reset flag anymore after the fork. It has
2552 * fulfilled its duty:
2553 */
2554 p->sched_reset_on_fork = 0;
2555 }
Lennart Poetteringca94c442009-06-15 17:17:47 +02002556
Peter Williamsf83f9ac2009-09-24 06:47:10 +00002557 /*
2558 * Make sure we do not leak PI boosting priority to the child.
2559 */
2560 p->prio = current->normal_prio;
2561
Hiroshi Shimamoto2ddbf952007-10-15 17:00:11 +02002562 if (!rt_prio(p->prio))
2563 p->sched_class = &fair_sched_class;
Ingo Molnarb29739f2006-06-27 02:54:51 -07002564
Peter Zijlstracd29fe62009-11-27 17:32:46 +01002565 if (p->sched_class->task_fork)
2566 p->sched_class->task_fork(p);
2567
Peter Zijlstra86951592010-06-22 11:44:53 +02002568 /*
2569 * The child is not yet in the pid-hash so no cgroup attach races,
2570 * and the cgroup is pinned to this child due to cgroup_fork()
2571 * is ran before sched_fork().
2572 *
2573 * Silence PROVE_RCU.
2574 */
2575 rcu_read_lock();
Peter Zijlstra5f3edc12009-09-10 13:42:00 +02002576 set_task_cpu(p, cpu);
Peter Zijlstra86951592010-06-22 11:44:53 +02002577 rcu_read_unlock();
Peter Zijlstra5f3edc12009-09-10 13:42:00 +02002578
Chandra Seetharaman52f17b62006-07-14 00:24:38 -07002579#if defined(CONFIG_SCHEDSTATS) || defined(CONFIG_TASK_DELAY_ACCT)
Ingo Molnardd41f592007-07-09 18:51:59 +02002580 if (likely(sched_info_on()))
Chandra Seetharaman52f17b62006-07-14 00:24:38 -07002581 memset(&p->sched_info, 0, sizeof(p->sched_info));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002582#endif
Chen, Kenneth Wd6077cb2006-02-14 13:53:10 -08002583#if defined(CONFIG_SMP) && defined(__ARCH_WANT_UNLOCKED_CTXSW)
Nick Piggin4866cde2005-06-25 14:57:23 -07002584 p->oncpu = 0;
2585#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002586#ifdef CONFIG_PREEMPT
Nick Piggin4866cde2005-06-25 14:57:23 -07002587 /* Want to start with kernel preemption disabled. */
Al Viroa1261f52005-11-13 16:06:55 -08002588 task_thread_info(p)->preempt_count = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002589#endif
Gregory Haskins917b6272008-12-29 09:39:53 -05002590 plist_node_init(&p->pushable_tasks, MAX_PRIO);
2591
Nick Piggin476d1392005-06-25 14:57:29 -07002592 put_cpu();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002593}
2594
2595/*
2596 * wake_up_new_task - wake up a newly created task for the first time.
2597 *
2598 * This function will do some initial scheduler statistics housekeeping
2599 * that must be done for every newly created context, then puts the task
2600 * on the runqueue and wakes it.
2601 */
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -08002602void wake_up_new_task(struct task_struct *p, unsigned long clone_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002603{
2604 unsigned long flags;
Ingo Molnardd41f592007-07-09 18:51:59 +02002605 struct rq *rq;
Andrew Mortonc8906922010-03-11 14:08:43 -08002606 int cpu __maybe_unused = get_cpu();
Peter Zijlstrafabf3182010-01-21 21:04:57 +01002607
2608#ifdef CONFIG_SMP
Peter Zijlstra0017d732010-03-24 18:34:10 +01002609 rq = task_rq_lock(p, &flags);
2610 p->state = TASK_WAKING;
2611
Peter Zijlstrafabf3182010-01-21 21:04:57 +01002612 /*
2613 * Fork balancing, do it here and not earlier because:
2614 * - cpus_allowed can change in the fork path
2615 * - any previously selected cpu might disappear through hotplug
2616 *
Peter Zijlstra0017d732010-03-24 18:34:10 +01002617 * We set TASK_WAKING so that select_task_rq() can drop rq->lock
2618 * without people poking at ->cpus_allowed.
Peter Zijlstrafabf3182010-01-21 21:04:57 +01002619 */
Peter Zijlstra0017d732010-03-24 18:34:10 +01002620 cpu = select_task_rq(rq, p, SD_BALANCE_FORK, 0);
Peter Zijlstrafabf3182010-01-21 21:04:57 +01002621 set_task_cpu(p, cpu);
Peter Zijlstra0017d732010-03-24 18:34:10 +01002622
2623 p->state = TASK_RUNNING;
2624 task_rq_unlock(rq, &flags);
Peter Zijlstrafabf3182010-01-21 21:04:57 +01002625#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002626
Peter Zijlstra0017d732010-03-24 18:34:10 +01002627 rq = task_rq_lock(p, &flags);
Peter Zijlstracd29fe62009-11-27 17:32:46 +01002628 activate_task(rq, p, 0);
Peter Zijlstra27a9da62010-05-04 20:36:56 +02002629 trace_sched_wakeup_new(p, 1);
Peter Zijlstraa7558e02009-09-14 20:02:34 +02002630 check_preempt_curr(rq, p, WF_FORK);
Steven Rostedt9a897c52008-01-25 21:08:22 +01002631#ifdef CONFIG_SMP
Peter Zijlstraefbbd052009-12-16 18:04:40 +01002632 if (p->sched_class->task_woken)
2633 p->sched_class->task_woken(rq, p);
Steven Rostedt9a897c52008-01-25 21:08:22 +01002634#endif
Ingo Molnardd41f592007-07-09 18:51:59 +02002635 task_rq_unlock(rq, &flags);
Peter Zijlstrafabf3182010-01-21 21:04:57 +01002636 put_cpu();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002637}
2638
Avi Kivitye107be32007-07-26 13:40:43 +02002639#ifdef CONFIG_PREEMPT_NOTIFIERS
2640
2641/**
Luis Henriques80dd99b2009-03-16 19:58:09 +00002642 * preempt_notifier_register - tell me when current is being preempted & rescheduled
Randy Dunlap421cee22007-07-31 00:37:50 -07002643 * @notifier: notifier struct to register
Avi Kivitye107be32007-07-26 13:40:43 +02002644 */
2645void preempt_notifier_register(struct preempt_notifier *notifier)
2646{
2647 hlist_add_head(&notifier->link, &current->preempt_notifiers);
2648}
2649EXPORT_SYMBOL_GPL(preempt_notifier_register);
2650
2651/**
2652 * preempt_notifier_unregister - no longer interested in preemption notifications
Randy Dunlap421cee22007-07-31 00:37:50 -07002653 * @notifier: notifier struct to unregister
Avi Kivitye107be32007-07-26 13:40:43 +02002654 *
2655 * This is safe to call from within a preemption notifier.
2656 */
2657void preempt_notifier_unregister(struct preempt_notifier *notifier)
2658{
2659 hlist_del(&notifier->link);
2660}
2661EXPORT_SYMBOL_GPL(preempt_notifier_unregister);
2662
2663static void fire_sched_in_preempt_notifiers(struct task_struct *curr)
2664{
2665 struct preempt_notifier *notifier;
2666 struct hlist_node *node;
2667
2668 hlist_for_each_entry(notifier, node, &curr->preempt_notifiers, link)
2669 notifier->ops->sched_in(notifier, raw_smp_processor_id());
2670}
2671
2672static void
2673fire_sched_out_preempt_notifiers(struct task_struct *curr,
2674 struct task_struct *next)
2675{
2676 struct preempt_notifier *notifier;
2677 struct hlist_node *node;
2678
2679 hlist_for_each_entry(notifier, node, &curr->preempt_notifiers, link)
2680 notifier->ops->sched_out(notifier, next);
2681}
2682
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +02002683#else /* !CONFIG_PREEMPT_NOTIFIERS */
Avi Kivitye107be32007-07-26 13:40:43 +02002684
2685static void fire_sched_in_preempt_notifiers(struct task_struct *curr)
2686{
2687}
2688
2689static void
2690fire_sched_out_preempt_notifiers(struct task_struct *curr,
2691 struct task_struct *next)
2692{
2693}
2694
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +02002695#endif /* CONFIG_PREEMPT_NOTIFIERS */
Avi Kivitye107be32007-07-26 13:40:43 +02002696
Linus Torvalds1da177e2005-04-16 15:20:36 -07002697/**
Nick Piggin4866cde2005-06-25 14:57:23 -07002698 * prepare_task_switch - prepare to switch tasks
2699 * @rq: the runqueue preparing to switch
Randy Dunlap421cee22007-07-31 00:37:50 -07002700 * @prev: the current task that is being switched out
Nick Piggin4866cde2005-06-25 14:57:23 -07002701 * @next: the task we are going to switch to.
2702 *
2703 * This is called with the rq lock held and interrupts off. It must
2704 * be paired with a subsequent finish_task_switch after the context
2705 * switch.
2706 *
2707 * prepare_task_switch sets up locking and calls architecture specific
2708 * hooks.
2709 */
Avi Kivitye107be32007-07-26 13:40:43 +02002710static inline void
2711prepare_task_switch(struct rq *rq, struct task_struct *prev,
2712 struct task_struct *next)
Nick Piggin4866cde2005-06-25 14:57:23 -07002713{
Avi Kivitye107be32007-07-26 13:40:43 +02002714 fire_sched_out_preempt_notifiers(prev, next);
Nick Piggin4866cde2005-06-25 14:57:23 -07002715 prepare_lock_switch(rq, next);
2716 prepare_arch_switch(next);
2717}
2718
2719/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07002720 * finish_task_switch - clean up after a task-switch
Jeff Garzik344baba2005-09-07 01:15:17 -04002721 * @rq: runqueue associated with task-switch
Linus Torvalds1da177e2005-04-16 15:20:36 -07002722 * @prev: the thread we just switched away from.
2723 *
Nick Piggin4866cde2005-06-25 14:57:23 -07002724 * finish_task_switch must be called after the context switch, paired
2725 * with a prepare_task_switch call before the context switch.
2726 * finish_task_switch will reconcile locking set up by prepare_task_switch,
2727 * and do any other architecture-specific cleanup actions.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002728 *
2729 * Note that we may have delayed dropping an mm in context_switch(). If
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01002730 * so, we finish that here outside of the runqueue lock. (Doing it
Linus Torvalds1da177e2005-04-16 15:20:36 -07002731 * with the lock held can cause deadlocks; see schedule() for
2732 * details.)
2733 */
Alexey Dobriyana9957442007-10-15 17:00:13 +02002734static void finish_task_switch(struct rq *rq, struct task_struct *prev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002735 __releases(rq->lock)
2736{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002737 struct mm_struct *mm = rq->prev_mm;
Oleg Nesterov55a101f2006-09-29 02:01:10 -07002738 long prev_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002739
2740 rq->prev_mm = NULL;
2741
2742 /*
2743 * A task struct has one reference for the use as "current".
Oleg Nesterovc394cc92006-09-29 02:01:11 -07002744 * If a task dies, then it sets TASK_DEAD in tsk->state and calls
Oleg Nesterov55a101f2006-09-29 02:01:10 -07002745 * schedule one last time. The schedule call will never return, and
2746 * the scheduled task must drop that reference.
Oleg Nesterovc394cc92006-09-29 02:01:11 -07002747 * The test for TASK_DEAD must occur while the runqueue locks are
Linus Torvalds1da177e2005-04-16 15:20:36 -07002748 * still held, otherwise prev could be scheduled on another cpu, die
2749 * there before we look at prev->state, and then the reference would
2750 * be dropped twice.
2751 * Manfred Spraul <manfred@colorfullife.com>
2752 */
Oleg Nesterov55a101f2006-09-29 02:01:10 -07002753 prev_state = prev->state;
Nick Piggin4866cde2005-06-25 14:57:23 -07002754 finish_arch_switch(prev);
Jamie Iles8381f652010-01-08 15:27:33 +00002755#ifdef __ARCH_WANT_INTERRUPTS_ON_CTXSW
2756 local_irq_disable();
2757#endif /* __ARCH_WANT_INTERRUPTS_ON_CTXSW */
Peter Zijlstra49f47432009-12-27 11:51:52 +01002758 perf_event_task_sched_in(current);
Jamie Iles8381f652010-01-08 15:27:33 +00002759#ifdef __ARCH_WANT_INTERRUPTS_ON_CTXSW
2760 local_irq_enable();
2761#endif /* __ARCH_WANT_INTERRUPTS_ON_CTXSW */
Nick Piggin4866cde2005-06-25 14:57:23 -07002762 finish_lock_switch(rq, prev);
Steven Rostedte8fa1362008-01-25 21:08:05 +01002763
Avi Kivitye107be32007-07-26 13:40:43 +02002764 fire_sched_in_preempt_notifiers(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002765 if (mm)
2766 mmdrop(mm);
Oleg Nesterovc394cc92006-09-29 02:01:11 -07002767 if (unlikely(prev_state == TASK_DEAD)) {
bibo maoc6fd91f2006-03-26 01:38:20 -08002768 /*
2769 * Remove function-return probe instances associated with this
2770 * task and put them back on the free list.
Ingo Molnar9761eea2007-07-09 18:52:00 +02002771 */
bibo maoc6fd91f2006-03-26 01:38:20 -08002772 kprobe_flush_task(prev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002773 put_task_struct(prev);
bibo maoc6fd91f2006-03-26 01:38:20 -08002774 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002775}
2776
Gregory Haskins3f029d32009-07-29 11:08:47 -04002777#ifdef CONFIG_SMP
2778
2779/* assumes rq->lock is held */
2780static inline void pre_schedule(struct rq *rq, struct task_struct *prev)
2781{
2782 if (prev->sched_class->pre_schedule)
2783 prev->sched_class->pre_schedule(rq, prev);
2784}
2785
2786/* rq->lock is NOT held, but preemption is disabled */
2787static inline void post_schedule(struct rq *rq)
2788{
2789 if (rq->post_schedule) {
2790 unsigned long flags;
2791
Thomas Gleixner05fa7852009-11-17 14:28:38 +01002792 raw_spin_lock_irqsave(&rq->lock, flags);
Gregory Haskins3f029d32009-07-29 11:08:47 -04002793 if (rq->curr->sched_class->post_schedule)
2794 rq->curr->sched_class->post_schedule(rq);
Thomas Gleixner05fa7852009-11-17 14:28:38 +01002795 raw_spin_unlock_irqrestore(&rq->lock, flags);
Gregory Haskins3f029d32009-07-29 11:08:47 -04002796
2797 rq->post_schedule = 0;
2798 }
2799}
2800
2801#else
2802
2803static inline void pre_schedule(struct rq *rq, struct task_struct *p)
2804{
2805}
2806
2807static inline void post_schedule(struct rq *rq)
2808{
2809}
2810
2811#endif
2812
Linus Torvalds1da177e2005-04-16 15:20:36 -07002813/**
2814 * schedule_tail - first thing a freshly forked thread must call.
2815 * @prev: the thread we just switched away from.
2816 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07002817asmlinkage void schedule_tail(struct task_struct *prev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002818 __releases(rq->lock)
2819{
Ingo Molnar70b97a72006-07-03 00:25:42 -07002820 struct rq *rq = this_rq();
2821
Nick Piggin4866cde2005-06-25 14:57:23 -07002822 finish_task_switch(rq, prev);
Steven Rostedtda19ab52009-07-29 00:21:22 -04002823
Gregory Haskins3f029d32009-07-29 11:08:47 -04002824 /*
2825 * FIXME: do we need to worry about rq being invalidated by the
2826 * task_switch?
2827 */
2828 post_schedule(rq);
Steven Rostedtda19ab52009-07-29 00:21:22 -04002829
Nick Piggin4866cde2005-06-25 14:57:23 -07002830#ifdef __ARCH_WANT_UNLOCKED_CTXSW
2831 /* In this case, finish_task_switch does not reenable preemption */
2832 preempt_enable();
2833#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002834 if (current->set_child_tid)
Pavel Emelyanovb4888932007-10-18 23:40:14 -07002835 put_user(task_pid_vnr(current), current->set_child_tid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002836}
2837
2838/*
2839 * context_switch - switch to the new MM and the new
2840 * thread's register state.
2841 */
Ingo Molnardd41f592007-07-09 18:51:59 +02002842static inline void
Ingo Molnar70b97a72006-07-03 00:25:42 -07002843context_switch(struct rq *rq, struct task_struct *prev,
Ingo Molnar36c8b582006-07-03 00:25:41 -07002844 struct task_struct *next)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002845{
Ingo Molnardd41f592007-07-09 18:51:59 +02002846 struct mm_struct *mm, *oldmm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002847
Avi Kivitye107be32007-07-26 13:40:43 +02002848 prepare_task_switch(rq, prev, next);
Peter Zijlstra27a9da62010-05-04 20:36:56 +02002849 trace_sched_switch(prev, next);
Ingo Molnardd41f592007-07-09 18:51:59 +02002850 mm = next->mm;
2851 oldmm = prev->active_mm;
Zachary Amsden9226d122007-02-13 13:26:21 +01002852 /*
2853 * For paravirt, this is coupled with an exit in switch_to to
2854 * combine the page table reload and the switch backend into
2855 * one hypercall.
2856 */
Jeremy Fitzhardinge224101e2009-02-18 11:18:57 -08002857 arch_start_context_switch(prev);
Zachary Amsden9226d122007-02-13 13:26:21 +01002858
Heiko Carstens31915ab2010-09-16 14:42:25 +02002859 if (!mm) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002860 next->active_mm = oldmm;
2861 atomic_inc(&oldmm->mm_count);
2862 enter_lazy_tlb(oldmm, next);
2863 } else
2864 switch_mm(oldmm, mm, next);
2865
Heiko Carstens31915ab2010-09-16 14:42:25 +02002866 if (!prev->mm) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002867 prev->active_mm = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002868 rq->prev_mm = oldmm;
2869 }
Ingo Molnar3a5f5e42006-07-14 00:24:27 -07002870 /*
2871 * Since the runqueue lock will be released by the next
2872 * task (which is an invalid locking op but in the case
2873 * of the scheduler it's an obvious special-case), so we
2874 * do an early lockdep release here:
2875 */
2876#ifndef __ARCH_WANT_UNLOCKED_CTXSW
Ingo Molnar8a25d5d2006-07-03 00:24:54 -07002877 spin_release(&rq->lock.dep_map, 1, _THIS_IP_);
Ingo Molnar3a5f5e42006-07-14 00:24:27 -07002878#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002879
2880 /* Here we just switch the register state and the stack. */
2881 switch_to(prev, next, prev);
2882
Ingo Molnardd41f592007-07-09 18:51:59 +02002883 barrier();
2884 /*
2885 * this_rq must be evaluated again because prev may have moved
2886 * CPUs since it called schedule(), thus the 'rq' on its stack
2887 * frame will be invalid.
2888 */
2889 finish_task_switch(this_rq(), prev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002890}
2891
2892/*
2893 * nr_running, nr_uninterruptible and nr_context_switches:
2894 *
2895 * externally visible scheduler statistics: current number of runnable
2896 * threads, current number of uninterruptible-sleeping threads, total
2897 * number of context switches performed since bootup.
2898 */
2899unsigned long nr_running(void)
2900{
2901 unsigned long i, sum = 0;
2902
2903 for_each_online_cpu(i)
2904 sum += cpu_rq(i)->nr_running;
2905
2906 return sum;
2907}
2908
2909unsigned long nr_uninterruptible(void)
2910{
2911 unsigned long i, sum = 0;
2912
KAMEZAWA Hiroyuki0a945022006-03-28 01:56:37 -08002913 for_each_possible_cpu(i)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002914 sum += cpu_rq(i)->nr_uninterruptible;
2915
2916 /*
2917 * Since we read the counters lockless, it might be slightly
2918 * inaccurate. Do not allow it to go below zero though:
2919 */
2920 if (unlikely((long)sum < 0))
2921 sum = 0;
2922
2923 return sum;
2924}
2925
2926unsigned long long nr_context_switches(void)
2927{
Steven Rostedtcc94abf2006-06-27 02:54:31 -07002928 int i;
2929 unsigned long long sum = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002930
KAMEZAWA Hiroyuki0a945022006-03-28 01:56:37 -08002931 for_each_possible_cpu(i)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002932 sum += cpu_rq(i)->nr_switches;
2933
2934 return sum;
2935}
2936
2937unsigned long nr_iowait(void)
2938{
2939 unsigned long i, sum = 0;
2940
KAMEZAWA Hiroyuki0a945022006-03-28 01:56:37 -08002941 for_each_possible_cpu(i)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002942 sum += atomic_read(&cpu_rq(i)->nr_iowait);
2943
2944 return sum;
2945}
2946
Peter Zijlstra8c215bd2010-07-01 09:07:17 +02002947unsigned long nr_iowait_cpu(int cpu)
Arjan van de Ven69d25872009-09-21 17:04:08 -07002948{
Peter Zijlstra8c215bd2010-07-01 09:07:17 +02002949 struct rq *this = cpu_rq(cpu);
Arjan van de Ven69d25872009-09-21 17:04:08 -07002950 return atomic_read(&this->nr_iowait);
2951}
2952
2953unsigned long this_cpu_load(void)
2954{
2955 struct rq *this = this_rq();
2956 return this->cpu_load[0];
2957}
2958
2959
Thomas Gleixnerdce48a82009-04-11 10:43:41 +02002960/* Variables and functions for calc_load */
2961static atomic_long_t calc_load_tasks;
2962static unsigned long calc_load_update;
2963unsigned long avenrun[3];
2964EXPORT_SYMBOL(avenrun);
2965
Peter Zijlstra74f51872010-04-22 21:50:19 +02002966static long calc_load_fold_active(struct rq *this_rq)
2967{
2968 long nr_active, delta = 0;
2969
2970 nr_active = this_rq->nr_running;
2971 nr_active += (long) this_rq->nr_uninterruptible;
2972
2973 if (nr_active != this_rq->calc_load_active) {
2974 delta = nr_active - this_rq->calc_load_active;
2975 this_rq->calc_load_active = nr_active;
2976 }
2977
2978 return delta;
2979}
2980
2981#ifdef CONFIG_NO_HZ
2982/*
2983 * For NO_HZ we delay the active fold to the next LOAD_FREQ update.
2984 *
2985 * When making the ILB scale, we should try to pull this in as well.
2986 */
2987static atomic_long_t calc_load_tasks_idle;
2988
2989static void calc_load_account_idle(struct rq *this_rq)
2990{
2991 long delta;
2992
2993 delta = calc_load_fold_active(this_rq);
2994 if (delta)
2995 atomic_long_add(delta, &calc_load_tasks_idle);
2996}
2997
2998static long calc_load_fold_idle(void)
2999{
3000 long delta = 0;
3001
3002 /*
3003 * Its got a race, we don't care...
3004 */
3005 if (atomic_long_read(&calc_load_tasks_idle))
3006 delta = atomic_long_xchg(&calc_load_tasks_idle, 0);
3007
3008 return delta;
3009}
3010#else
3011static void calc_load_account_idle(struct rq *this_rq)
3012{
3013}
3014
3015static inline long calc_load_fold_idle(void)
3016{
3017 return 0;
3018}
3019#endif
3020
Thomas Gleixner2d024942009-05-02 20:08:52 +02003021/**
3022 * get_avenrun - get the load average array
3023 * @loads: pointer to dest load array
3024 * @offset: offset to add
3025 * @shift: shift count to shift the result left
3026 *
3027 * These values are estimates at best, so no need for locking.
3028 */
3029void get_avenrun(unsigned long *loads, unsigned long offset, int shift)
3030{
3031 loads[0] = (avenrun[0] + offset) << shift;
3032 loads[1] = (avenrun[1] + offset) << shift;
3033 loads[2] = (avenrun[2] + offset) << shift;
3034}
3035
Thomas Gleixnerdce48a82009-04-11 10:43:41 +02003036static unsigned long
3037calc_load(unsigned long load, unsigned long exp, unsigned long active)
Jack Steinerdb1b1fe2006-03-31 02:31:21 -08003038{
Thomas Gleixnerdce48a82009-04-11 10:43:41 +02003039 load *= exp;
3040 load += active * (FIXED_1 - exp);
3041 return load >> FSHIFT;
3042}
Jack Steinerdb1b1fe2006-03-31 02:31:21 -08003043
Thomas Gleixnerdce48a82009-04-11 10:43:41 +02003044/*
3045 * calc_load - update the avenrun load estimates 10 ticks after the
3046 * CPUs have updated calc_load_tasks.
3047 */
3048void calc_global_load(void)
3049{
3050 unsigned long upd = calc_load_update + 10;
3051 long active;
3052
3053 if (time_before(jiffies, upd))
3054 return;
3055
3056 active = atomic_long_read(&calc_load_tasks);
3057 active = active > 0 ? active * FIXED_1 : 0;
3058
3059 avenrun[0] = calc_load(avenrun[0], EXP_1, active);
3060 avenrun[1] = calc_load(avenrun[1], EXP_5, active);
3061 avenrun[2] = calc_load(avenrun[2], EXP_15, active);
3062
3063 calc_load_update += LOAD_FREQ;
3064}
3065
3066/*
Peter Zijlstra74f51872010-04-22 21:50:19 +02003067 * Called from update_cpu_load() to periodically update this CPU's
3068 * active count.
Thomas Gleixnerdce48a82009-04-11 10:43:41 +02003069 */
3070static void calc_load_account_active(struct rq *this_rq)
3071{
Peter Zijlstra74f51872010-04-22 21:50:19 +02003072 long delta;
Thomas Gleixnerdce48a82009-04-11 10:43:41 +02003073
Peter Zijlstra74f51872010-04-22 21:50:19 +02003074 if (time_before(jiffies, this_rq->calc_load_update))
3075 return;
Thomas Gleixnerdce48a82009-04-11 10:43:41 +02003076
Peter Zijlstra74f51872010-04-22 21:50:19 +02003077 delta = calc_load_fold_active(this_rq);
3078 delta += calc_load_fold_idle();
3079 if (delta)
Thomas Gleixnerdce48a82009-04-11 10:43:41 +02003080 atomic_long_add(delta, &calc_load_tasks);
Peter Zijlstra74f51872010-04-22 21:50:19 +02003081
3082 this_rq->calc_load_update += LOAD_FREQ;
Jack Steinerdb1b1fe2006-03-31 02:31:21 -08003083}
3084
Linus Torvalds1da177e2005-04-16 15:20:36 -07003085/*
Venkatesh Pallipadifdf3e952010-05-17 18:14:43 -07003086 * The exact cpuload at various idx values, calculated at every tick would be
3087 * load = (2^idx - 1) / 2^idx * load + 1 / 2^idx * cur_load
3088 *
3089 * If a cpu misses updates for n-1 ticks (as it was idle) and update gets called
3090 * on nth tick when cpu may be busy, then we have:
3091 * load = ((2^idx - 1) / 2^idx)^(n-1) * load
3092 * load = (2^idx - 1) / 2^idx) * load + 1 / 2^idx * cur_load
3093 *
3094 * decay_load_missed() below does efficient calculation of
3095 * load = ((2^idx - 1) / 2^idx)^(n-1) * load
3096 * avoiding 0..n-1 loop doing load = ((2^idx - 1) / 2^idx) * load
3097 *
3098 * The calculation is approximated on a 128 point scale.
3099 * degrade_zero_ticks is the number of ticks after which load at any
3100 * particular idx is approximated to be zero.
3101 * degrade_factor is a precomputed table, a row for each load idx.
3102 * Each column corresponds to degradation factor for a power of two ticks,
3103 * based on 128 point scale.
3104 * Example:
3105 * row 2, col 3 (=12) says that the degradation at load idx 2 after
3106 * 8 ticks is 12/128 (which is an approximation of exact factor 3^8/4^8).
3107 *
3108 * With this power of 2 load factors, we can degrade the load n times
3109 * by looking at 1 bits in n and doing as many mult/shift instead of
3110 * n mult/shifts needed by the exact degradation.
3111 */
3112#define DEGRADE_SHIFT 7
3113static const unsigned char
3114 degrade_zero_ticks[CPU_LOAD_IDX_MAX] = {0, 8, 32, 64, 128};
3115static const unsigned char
3116 degrade_factor[CPU_LOAD_IDX_MAX][DEGRADE_SHIFT + 1] = {
3117 {0, 0, 0, 0, 0, 0, 0, 0},
3118 {64, 32, 8, 0, 0, 0, 0, 0},
3119 {96, 72, 40, 12, 1, 0, 0},
3120 {112, 98, 75, 43, 15, 1, 0},
3121 {120, 112, 98, 76, 45, 16, 2} };
3122
3123/*
3124 * Update cpu_load for any missed ticks, due to tickless idle. The backlog
3125 * would be when CPU is idle and so we just decay the old load without
3126 * adding any new load.
3127 */
3128static unsigned long
3129decay_load_missed(unsigned long load, unsigned long missed_updates, int idx)
3130{
3131 int j = 0;
3132
3133 if (!missed_updates)
3134 return load;
3135
3136 if (missed_updates >= degrade_zero_ticks[idx])
3137 return 0;
3138
3139 if (idx == 1)
3140 return load >> missed_updates;
3141
3142 while (missed_updates) {
3143 if (missed_updates % 2)
3144 load = (load * degrade_factor[idx][j]) >> DEGRADE_SHIFT;
3145
3146 missed_updates >>= 1;
3147 j++;
3148 }
3149 return load;
3150}
3151
3152/*
Ingo Molnardd41f592007-07-09 18:51:59 +02003153 * Update rq->cpu_load[] statistics. This function is usually called every
Venkatesh Pallipadifdf3e952010-05-17 18:14:43 -07003154 * scheduler tick (TICK_NSEC). With tickless idle this will not be called
3155 * every tick. We fix it up based on jiffies.
Ingo Molnar48f24c42006-07-03 00:25:40 -07003156 */
Ingo Molnardd41f592007-07-09 18:51:59 +02003157static void update_cpu_load(struct rq *this_rq)
Ingo Molnar48f24c42006-07-03 00:25:40 -07003158{
Dmitry Adamushko495eca42007-10-15 17:00:06 +02003159 unsigned long this_load = this_rq->load.weight;
Venkatesh Pallipadifdf3e952010-05-17 18:14:43 -07003160 unsigned long curr_jiffies = jiffies;
3161 unsigned long pending_updates;
Ingo Molnardd41f592007-07-09 18:51:59 +02003162 int i, scale;
3163
3164 this_rq->nr_load_updates++;
Ingo Molnardd41f592007-07-09 18:51:59 +02003165
Venkatesh Pallipadifdf3e952010-05-17 18:14:43 -07003166 /* Avoid repeated calls on same jiffy, when moving in and out of idle */
3167 if (curr_jiffies == this_rq->last_load_update_tick)
3168 return;
3169
3170 pending_updates = curr_jiffies - this_rq->last_load_update_tick;
3171 this_rq->last_load_update_tick = curr_jiffies;
3172
Ingo Molnardd41f592007-07-09 18:51:59 +02003173 /* Update our load: */
Venkatesh Pallipadifdf3e952010-05-17 18:14:43 -07003174 this_rq->cpu_load[0] = this_load; /* Fasttrack for idx 0 */
3175 for (i = 1, scale = 2; i < CPU_LOAD_IDX_MAX; i++, scale += scale) {
Ingo Molnardd41f592007-07-09 18:51:59 +02003176 unsigned long old_load, new_load;
3177
3178 /* scale is effectively 1 << i now, and >> i divides by scale */
3179
3180 old_load = this_rq->cpu_load[i];
Venkatesh Pallipadifdf3e952010-05-17 18:14:43 -07003181 old_load = decay_load_missed(old_load, pending_updates - 1, i);
Ingo Molnardd41f592007-07-09 18:51:59 +02003182 new_load = this_load;
Ingo Molnara25707f2007-10-15 17:00:03 +02003183 /*
3184 * Round up the averaging division if load is increasing. This
3185 * prevents us from getting stuck on 9 if the load is 10, for
3186 * example.
3187 */
3188 if (new_load > old_load)
Venkatesh Pallipadifdf3e952010-05-17 18:14:43 -07003189 new_load += scale - 1;
3190
3191 this_rq->cpu_load[i] = (old_load * (scale - 1) + new_load) >> i;
Ingo Molnardd41f592007-07-09 18:51:59 +02003192 }
Suresh Siddhada2b71e2010-08-23 13:42:51 -07003193
3194 sched_avg_update(this_rq);
Venkatesh Pallipadifdf3e952010-05-17 18:14:43 -07003195}
3196
3197static void update_cpu_load_active(struct rq *this_rq)
3198{
3199 update_cpu_load(this_rq);
Thomas Gleixnerdce48a82009-04-11 10:43:41 +02003200
Peter Zijlstra74f51872010-04-22 21:50:19 +02003201 calc_load_account_active(this_rq);
Ingo Molnar48f24c42006-07-03 00:25:40 -07003202}
3203
Ingo Molnardd41f592007-07-09 18:51:59 +02003204#ifdef CONFIG_SMP
3205
Ingo Molnar48f24c42006-07-03 00:25:40 -07003206/*
Peter Zijlstra38022902009-12-16 18:04:37 +01003207 * sched_exec - execve() is a valuable balancing opportunity, because at
3208 * this point the task has the smallest effective memory and cache footprint.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003209 */
Peter Zijlstra38022902009-12-16 18:04:37 +01003210void sched_exec(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003211{
Peter Zijlstra38022902009-12-16 18:04:37 +01003212 struct task_struct *p = current;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003213 unsigned long flags;
Ingo Molnar70b97a72006-07-03 00:25:42 -07003214 struct rq *rq;
Peter Zijlstra0017d732010-03-24 18:34:10 +01003215 int dest_cpu;
Peter Zijlstra38022902009-12-16 18:04:37 +01003216
Linus Torvalds1da177e2005-04-16 15:20:36 -07003217 rq = task_rq_lock(p, &flags);
Peter Zijlstra0017d732010-03-24 18:34:10 +01003218 dest_cpu = p->sched_class->select_task_rq(rq, p, SD_BALANCE_EXEC, 0);
3219 if (dest_cpu == smp_processor_id())
3220 goto unlock;
Peter Zijlstra38022902009-12-16 18:04:37 +01003221
3222 /*
3223 * select_task_rq() can race against ->cpus_allowed
3224 */
Oleg Nesterov30da6882010-03-15 10:10:19 +01003225 if (cpumask_test_cpu(dest_cpu, &p->cpus_allowed) &&
Tejun Heo969c7922010-05-06 18:49:21 +02003226 likely(cpu_active(dest_cpu)) && migrate_task(p, dest_cpu)) {
3227 struct migration_arg arg = { p, dest_cpu };
Ingo Molnar36c8b582006-07-03 00:25:41 -07003228
Linus Torvalds1da177e2005-04-16 15:20:36 -07003229 task_rq_unlock(rq, &flags);
Tejun Heo969c7922010-05-06 18:49:21 +02003230 stop_one_cpu(cpu_of(rq), migration_cpu_stop, &arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003231 return;
3232 }
Peter Zijlstra0017d732010-03-24 18:34:10 +01003233unlock:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003234 task_rq_unlock(rq, &flags);
3235}
3236
Linus Torvalds1da177e2005-04-16 15:20:36 -07003237#endif
3238
Linus Torvalds1da177e2005-04-16 15:20:36 -07003239DEFINE_PER_CPU(struct kernel_stat, kstat);
3240
3241EXPORT_PER_CPU_SYMBOL(kstat);
3242
3243/*
Hidetoshi Setoc5f8d992009-03-31 16:56:03 +09003244 * Return any ns on the sched_clock that have not yet been accounted in
Frank Mayharf06febc2008-09-12 09:54:39 -07003245 * @p in case that task is currently running.
Hidetoshi Setoc5f8d992009-03-31 16:56:03 +09003246 *
3247 * Called with task_rq_lock() held on @rq.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003248 */
Hidetoshi Setoc5f8d992009-03-31 16:56:03 +09003249static u64 do_task_delta_exec(struct task_struct *p, struct rq *rq)
3250{
3251 u64 ns = 0;
3252
3253 if (task_current(rq, p)) {
3254 update_rq_clock(rq);
Venkatesh Pallipadi305e6832010-10-04 17:03:21 -07003255 ns = rq->clock_task - p->se.exec_start;
Hidetoshi Setoc5f8d992009-03-31 16:56:03 +09003256 if ((s64)ns < 0)
3257 ns = 0;
3258 }
3259
3260 return ns;
3261}
3262
Frank Mayharbb34d922008-09-12 09:54:39 -07003263unsigned long long task_delta_exec(struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003264{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003265 unsigned long flags;
Ingo Molnar41b86e92007-07-09 18:51:58 +02003266 struct rq *rq;
Frank Mayharbb34d922008-09-12 09:54:39 -07003267 u64 ns = 0;
Ingo Molnar48f24c42006-07-03 00:25:40 -07003268
Ingo Molnar41b86e92007-07-09 18:51:58 +02003269 rq = task_rq_lock(p, &flags);
Hidetoshi Setoc5f8d992009-03-31 16:56:03 +09003270 ns = do_task_delta_exec(p, rq);
3271 task_rq_unlock(rq, &flags);
Ingo Molnar15084872008-09-30 08:28:17 +02003272
Hidetoshi Setoc5f8d992009-03-31 16:56:03 +09003273 return ns;
3274}
Frank Mayharf06febc2008-09-12 09:54:39 -07003275
Hidetoshi Setoc5f8d992009-03-31 16:56:03 +09003276/*
3277 * Return accounted runtime for the task.
3278 * In case the task is currently running, return the runtime plus current's
3279 * pending runtime that have not been accounted yet.
3280 */
3281unsigned long long task_sched_runtime(struct task_struct *p)
3282{
3283 unsigned long flags;
3284 struct rq *rq;
3285 u64 ns = 0;
Ingo Molnar48f24c42006-07-03 00:25:40 -07003286
Hidetoshi Setoc5f8d992009-03-31 16:56:03 +09003287 rq = task_rq_lock(p, &flags);
3288 ns = p->se.sum_exec_runtime + do_task_delta_exec(p, rq);
3289 task_rq_unlock(rq, &flags);
3290
3291 return ns;
3292}
3293
3294/*
3295 * Return sum_exec_runtime for the thread group.
3296 * In case the task is currently running, return the sum plus current's
3297 * pending runtime that have not been accounted yet.
3298 *
3299 * Note that the thread group might have other running tasks as well,
3300 * so the return value not includes other pending runtime that other
3301 * running tasks might have.
3302 */
3303unsigned long long thread_group_sched_runtime(struct task_struct *p)
3304{
3305 struct task_cputime totals;
3306 unsigned long flags;
3307 struct rq *rq;
3308 u64 ns;
3309
3310 rq = task_rq_lock(p, &flags);
3311 thread_group_cputime(p, &totals);
3312 ns = totals.sum_exec_runtime + do_task_delta_exec(p, rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003313 task_rq_unlock(rq, &flags);
3314
3315 return ns;
3316}
3317
3318/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003319 * Account user cpu time to a process.
3320 * @p: the process that the cpu time gets accounted to
Linus Torvalds1da177e2005-04-16 15:20:36 -07003321 * @cputime: the cpu time spent in user space since the last update
Martin Schwidefsky457533a2008-12-31 15:11:37 +01003322 * @cputime_scaled: cputime scaled by cpu frequency
Linus Torvalds1da177e2005-04-16 15:20:36 -07003323 */
Martin Schwidefsky457533a2008-12-31 15:11:37 +01003324void account_user_time(struct task_struct *p, cputime_t cputime,
3325 cputime_t cputime_scaled)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003326{
3327 struct cpu_usage_stat *cpustat = &kstat_this_cpu.cpustat;
3328 cputime64_t tmp;
3329
Martin Schwidefsky457533a2008-12-31 15:11:37 +01003330 /* Add user time to process. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003331 p->utime = cputime_add(p->utime, cputime);
Martin Schwidefsky457533a2008-12-31 15:11:37 +01003332 p->utimescaled = cputime_add(p->utimescaled, cputime_scaled);
Frank Mayharf06febc2008-09-12 09:54:39 -07003333 account_group_user_time(p, cputime);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003334
3335 /* Add user time to cpustat. */
3336 tmp = cputime_to_cputime64(cputime);
3337 if (TASK_NICE(p) > 0)
3338 cpustat->nice = cputime64_add(cpustat->nice, tmp);
3339 else
3340 cpustat->user = cputime64_add(cpustat->user, tmp);
Bharata B Raoef12fef2009-03-31 10:02:22 +05303341
3342 cpuacct_update_stats(p, CPUACCT_STAT_USER, cputime);
Jonathan Lim49b5cf32008-07-25 01:48:40 -07003343 /* Account for user time used */
3344 acct_update_integrals(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003345}
3346
3347/*
Laurent Vivier94886b82007-10-15 17:00:19 +02003348 * Account guest cpu time to a process.
3349 * @p: the process that the cpu time gets accounted to
3350 * @cputime: the cpu time spent in virtual machine since the last update
Martin Schwidefsky457533a2008-12-31 15:11:37 +01003351 * @cputime_scaled: cputime scaled by cpu frequency
Laurent Vivier94886b82007-10-15 17:00:19 +02003352 */
Martin Schwidefsky457533a2008-12-31 15:11:37 +01003353static void account_guest_time(struct task_struct *p, cputime_t cputime,
3354 cputime_t cputime_scaled)
Laurent Vivier94886b82007-10-15 17:00:19 +02003355{
3356 cputime64_t tmp;
3357 struct cpu_usage_stat *cpustat = &kstat_this_cpu.cpustat;
3358
3359 tmp = cputime_to_cputime64(cputime);
3360
Martin Schwidefsky457533a2008-12-31 15:11:37 +01003361 /* Add guest time to process. */
Laurent Vivier94886b82007-10-15 17:00:19 +02003362 p->utime = cputime_add(p->utime, cputime);
Martin Schwidefsky457533a2008-12-31 15:11:37 +01003363 p->utimescaled = cputime_add(p->utimescaled, cputime_scaled);
Frank Mayharf06febc2008-09-12 09:54:39 -07003364 account_group_user_time(p, cputime);
Laurent Vivier94886b82007-10-15 17:00:19 +02003365 p->gtime = cputime_add(p->gtime, cputime);
3366
Martin Schwidefsky457533a2008-12-31 15:11:37 +01003367 /* Add guest time to cpustat. */
Ryota Ozakice0e7b22009-10-24 01:20:10 +09003368 if (TASK_NICE(p) > 0) {
3369 cpustat->nice = cputime64_add(cpustat->nice, tmp);
3370 cpustat->guest_nice = cputime64_add(cpustat->guest_nice, tmp);
3371 } else {
3372 cpustat->user = cputime64_add(cpustat->user, tmp);
3373 cpustat->guest = cputime64_add(cpustat->guest, tmp);
3374 }
Laurent Vivier94886b82007-10-15 17:00:19 +02003375}
3376
3377/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003378 * Account system cpu time to a process.
3379 * @p: the process that the cpu time gets accounted to
3380 * @hardirq_offset: the offset to subtract from hardirq_count()
3381 * @cputime: the cpu time spent in kernel space since the last update
Martin Schwidefsky457533a2008-12-31 15:11:37 +01003382 * @cputime_scaled: cputime scaled by cpu frequency
Linus Torvalds1da177e2005-04-16 15:20:36 -07003383 */
3384void account_system_time(struct task_struct *p, int hardirq_offset,
Martin Schwidefsky457533a2008-12-31 15:11:37 +01003385 cputime_t cputime, cputime_t cputime_scaled)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003386{
3387 struct cpu_usage_stat *cpustat = &kstat_this_cpu.cpustat;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003388 cputime64_t tmp;
3389
Harvey Harrison983ed7a2008-04-24 18:17:55 -07003390 if ((p->flags & PF_VCPU) && (irq_count() - hardirq_offset == 0)) {
Martin Schwidefsky457533a2008-12-31 15:11:37 +01003391 account_guest_time(p, cputime, cputime_scaled);
Harvey Harrison983ed7a2008-04-24 18:17:55 -07003392 return;
3393 }
Laurent Vivier94886b82007-10-15 17:00:19 +02003394
Martin Schwidefsky457533a2008-12-31 15:11:37 +01003395 /* Add system time to process. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003396 p->stime = cputime_add(p->stime, cputime);
Martin Schwidefsky457533a2008-12-31 15:11:37 +01003397 p->stimescaled = cputime_add(p->stimescaled, cputime_scaled);
Frank Mayharf06febc2008-09-12 09:54:39 -07003398 account_group_system_time(p, cputime);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003399
3400 /* Add system time to cpustat. */
3401 tmp = cputime_to_cputime64(cputime);
3402 if (hardirq_count() - hardirq_offset)
3403 cpustat->irq = cputime64_add(cpustat->irq, tmp);
Venkatesh Pallipadi75e10562010-10-04 17:03:16 -07003404 else if (in_serving_softirq())
Linus Torvalds1da177e2005-04-16 15:20:36 -07003405 cpustat->softirq = cputime64_add(cpustat->softirq, tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003406 else
Martin Schwidefsky79741dd2008-12-31 15:11:38 +01003407 cpustat->system = cputime64_add(cpustat->system, tmp);
3408
Bharata B Raoef12fef2009-03-31 10:02:22 +05303409 cpuacct_update_stats(p, CPUACCT_STAT_SYSTEM, cputime);
3410
Linus Torvalds1da177e2005-04-16 15:20:36 -07003411 /* Account for system time used */
3412 acct_update_integrals(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003413}
3414
3415/*
3416 * Account for involuntary wait time.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003417 * @steal: the cpu time spent in involuntary wait
Linus Torvalds1da177e2005-04-16 15:20:36 -07003418 */
Martin Schwidefsky79741dd2008-12-31 15:11:38 +01003419void account_steal_time(cputime_t cputime)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003420{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003421 struct cpu_usage_stat *cpustat = &kstat_this_cpu.cpustat;
Martin Schwidefsky79741dd2008-12-31 15:11:38 +01003422 cputime64_t cputime64 = cputime_to_cputime64(cputime);
3423
3424 cpustat->steal = cputime64_add(cpustat->steal, cputime64);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003425}
3426
Christoph Lameter7835b982006-12-10 02:20:22 -08003427/*
Martin Schwidefsky79741dd2008-12-31 15:11:38 +01003428 * Account for idle time.
3429 * @cputime: the cpu time spent in idle wait
Linus Torvalds1da177e2005-04-16 15:20:36 -07003430 */
Martin Schwidefsky79741dd2008-12-31 15:11:38 +01003431void account_idle_time(cputime_t cputime)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003432{
3433 struct cpu_usage_stat *cpustat = &kstat_this_cpu.cpustat;
Martin Schwidefsky79741dd2008-12-31 15:11:38 +01003434 cputime64_t cputime64 = cputime_to_cputime64(cputime);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003435 struct rq *rq = this_rq();
3436
Martin Schwidefsky79741dd2008-12-31 15:11:38 +01003437 if (atomic_read(&rq->nr_iowait) > 0)
3438 cpustat->iowait = cputime64_add(cpustat->iowait, cputime64);
3439 else
3440 cpustat->idle = cputime64_add(cpustat->idle, cputime64);
Christoph Lameter7835b982006-12-10 02:20:22 -08003441}
3442
Martin Schwidefsky79741dd2008-12-31 15:11:38 +01003443#ifndef CONFIG_VIRT_CPU_ACCOUNTING
3444
3445/*
3446 * Account a single tick of cpu time.
3447 * @p: the process that the cpu time gets accounted to
3448 * @user_tick: indicates if the tick is a user or a system tick
3449 */
3450void account_process_tick(struct task_struct *p, int user_tick)
3451{
Stanislaw Gruszkaa42548a2009-07-29 12:15:29 +02003452 cputime_t one_jiffy_scaled = cputime_to_scaled(cputime_one_jiffy);
Martin Schwidefsky79741dd2008-12-31 15:11:38 +01003453 struct rq *rq = this_rq();
3454
3455 if (user_tick)
Stanislaw Gruszkaa42548a2009-07-29 12:15:29 +02003456 account_user_time(p, cputime_one_jiffy, one_jiffy_scaled);
Eric Dumazetf5f293a2009-04-29 14:44:49 +02003457 else if ((p != rq->idle) || (irq_count() != HARDIRQ_OFFSET))
Stanislaw Gruszkaa42548a2009-07-29 12:15:29 +02003458 account_system_time(p, HARDIRQ_OFFSET, cputime_one_jiffy,
Martin Schwidefsky79741dd2008-12-31 15:11:38 +01003459 one_jiffy_scaled);
3460 else
Stanislaw Gruszkaa42548a2009-07-29 12:15:29 +02003461 account_idle_time(cputime_one_jiffy);
Martin Schwidefsky79741dd2008-12-31 15:11:38 +01003462}
3463
3464/*
3465 * Account multiple ticks of steal time.
3466 * @p: the process from which the cpu time has been stolen
3467 * @ticks: number of stolen ticks
3468 */
3469void account_steal_ticks(unsigned long ticks)
3470{
3471 account_steal_time(jiffies_to_cputime(ticks));
3472}
3473
3474/*
3475 * Account multiple ticks of idle time.
3476 * @ticks: number of stolen ticks
3477 */
3478void account_idle_ticks(unsigned long ticks)
3479{
3480 account_idle_time(jiffies_to_cputime(ticks));
3481}
3482
3483#endif
3484
Christoph Lameter7835b982006-12-10 02:20:22 -08003485/*
Balbir Singh49048622008-09-05 18:12:23 +02003486 * Use precise platform statistics if available:
3487 */
3488#ifdef CONFIG_VIRT_CPU_ACCOUNTING
Hidetoshi Setod180c5b2009-11-26 14:48:30 +09003489void task_times(struct task_struct *p, cputime_t *ut, cputime_t *st)
Balbir Singh49048622008-09-05 18:12:23 +02003490{
Hidetoshi Setod99ca3b2009-12-02 17:26:47 +09003491 *ut = p->utime;
3492 *st = p->stime;
Balbir Singh49048622008-09-05 18:12:23 +02003493}
3494
Hidetoshi Seto0cf55e12009-12-02 17:28:07 +09003495void thread_group_times(struct task_struct *p, cputime_t *ut, cputime_t *st)
Balbir Singh49048622008-09-05 18:12:23 +02003496{
Hidetoshi Seto0cf55e12009-12-02 17:28:07 +09003497 struct task_cputime cputime;
3498
3499 thread_group_cputime(p, &cputime);
3500
3501 *ut = cputime.utime;
3502 *st = cputime.stime;
Balbir Singh49048622008-09-05 18:12:23 +02003503}
3504#else
Hidetoshi Seto761b1d22009-11-12 13:33:45 +09003505
3506#ifndef nsecs_to_cputime
Hidetoshi Setob7b20df2009-11-26 14:49:27 +09003507# define nsecs_to_cputime(__nsecs) nsecs_to_jiffies(__nsecs)
Hidetoshi Seto761b1d22009-11-12 13:33:45 +09003508#endif
3509
Hidetoshi Setod180c5b2009-11-26 14:48:30 +09003510void task_times(struct task_struct *p, cputime_t *ut, cputime_t *st)
Balbir Singh49048622008-09-05 18:12:23 +02003511{
Hidetoshi Setod99ca3b2009-12-02 17:26:47 +09003512 cputime_t rtime, utime = p->utime, total = cputime_add(utime, p->stime);
Balbir Singh49048622008-09-05 18:12:23 +02003513
3514 /*
3515 * Use CFS's precise accounting:
3516 */
Hidetoshi Setod180c5b2009-11-26 14:48:30 +09003517 rtime = nsecs_to_cputime(p->se.sum_exec_runtime);
Balbir Singh49048622008-09-05 18:12:23 +02003518
3519 if (total) {
Stanislaw Gruszkae75e8632010-09-14 16:35:14 +02003520 u64 temp = rtime;
Balbir Singh49048622008-09-05 18:12:23 +02003521
Stanislaw Gruszkae75e8632010-09-14 16:35:14 +02003522 temp *= utime;
Balbir Singh49048622008-09-05 18:12:23 +02003523 do_div(temp, total);
Hidetoshi Setod180c5b2009-11-26 14:48:30 +09003524 utime = (cputime_t)temp;
3525 } else
3526 utime = rtime;
Balbir Singh49048622008-09-05 18:12:23 +02003527
3528 /*
Hidetoshi Setod180c5b2009-11-26 14:48:30 +09003529 * Compare with previous values, to keep monotonicity:
Balbir Singh49048622008-09-05 18:12:23 +02003530 */
Hidetoshi Seto761b1d22009-11-12 13:33:45 +09003531 p->prev_utime = max(p->prev_utime, utime);
Hidetoshi Setod99ca3b2009-12-02 17:26:47 +09003532 p->prev_stime = max(p->prev_stime, cputime_sub(rtime, p->prev_utime));
Balbir Singh49048622008-09-05 18:12:23 +02003533
Hidetoshi Setod99ca3b2009-12-02 17:26:47 +09003534 *ut = p->prev_utime;
3535 *st = p->prev_stime;
Hidetoshi Setod180c5b2009-11-26 14:48:30 +09003536}
Balbir Singh49048622008-09-05 18:12:23 +02003537
Hidetoshi Seto0cf55e12009-12-02 17:28:07 +09003538/*
3539 * Must be called with siglock held.
3540 */
3541void thread_group_times(struct task_struct *p, cputime_t *ut, cputime_t *st)
3542{
3543 struct signal_struct *sig = p->signal;
3544 struct task_cputime cputime;
3545 cputime_t rtime, utime, total;
3546
3547 thread_group_cputime(p, &cputime);
3548
3549 total = cputime_add(cputime.utime, cputime.stime);
3550 rtime = nsecs_to_cputime(cputime.sum_exec_runtime);
3551
3552 if (total) {
Stanislaw Gruszkae75e8632010-09-14 16:35:14 +02003553 u64 temp = rtime;
Hidetoshi Seto0cf55e12009-12-02 17:28:07 +09003554
Stanislaw Gruszkae75e8632010-09-14 16:35:14 +02003555 temp *= cputime.utime;
Hidetoshi Seto0cf55e12009-12-02 17:28:07 +09003556 do_div(temp, total);
3557 utime = (cputime_t)temp;
3558 } else
3559 utime = rtime;
3560
3561 sig->prev_utime = max(sig->prev_utime, utime);
3562 sig->prev_stime = max(sig->prev_stime,
3563 cputime_sub(rtime, sig->prev_utime));
3564
3565 *ut = sig->prev_utime;
3566 *st = sig->prev_stime;
Balbir Singh49048622008-09-05 18:12:23 +02003567}
3568#endif
3569
Balbir Singh49048622008-09-05 18:12:23 +02003570/*
Christoph Lameter7835b982006-12-10 02:20:22 -08003571 * This function gets called by the timer code, with HZ frequency.
3572 * We call it with interrupts disabled.
3573 *
3574 * It also gets called by the fork code, when changing the parent's
3575 * timeslices.
3576 */
3577void scheduler_tick(void)
3578{
Christoph Lameter7835b982006-12-10 02:20:22 -08003579 int cpu = smp_processor_id();
3580 struct rq *rq = cpu_rq(cpu);
Ingo Molnardd41f592007-07-09 18:51:59 +02003581 struct task_struct *curr = rq->curr;
Peter Zijlstra3e51f332008-05-03 18:29:28 +02003582
3583 sched_clock_tick();
Christoph Lameter7835b982006-12-10 02:20:22 -08003584
Thomas Gleixner05fa7852009-11-17 14:28:38 +01003585 raw_spin_lock(&rq->lock);
Peter Zijlstra3e51f332008-05-03 18:29:28 +02003586 update_rq_clock(rq);
Venkatesh Pallipadifdf3e952010-05-17 18:14:43 -07003587 update_cpu_load_active(rq);
Peter Zijlstrafa85ae22008-01-25 21:08:29 +01003588 curr->sched_class->task_tick(rq, curr, 0);
Thomas Gleixner05fa7852009-11-17 14:28:38 +01003589 raw_spin_unlock(&rq->lock);
Ingo Molnardd41f592007-07-09 18:51:59 +02003590
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02003591 perf_event_task_tick();
Peter Zijlstrae220d2d2009-05-23 18:28:55 +02003592
Christoph Lametere418e1c2006-12-10 02:20:23 -08003593#ifdef CONFIG_SMP
Ingo Molnardd41f592007-07-09 18:51:59 +02003594 rq->idle_at_tick = idle_cpu(cpu);
3595 trigger_load_balance(rq, cpu);
Christoph Lametere418e1c2006-12-10 02:20:23 -08003596#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003597}
3598
Lai Jiangshan132380a2009-04-02 14:18:25 +08003599notrace unsigned long get_parent_ip(unsigned long addr)
Steven Rostedt6cd8a4b2008-05-12 21:20:42 +02003600{
3601 if (in_lock_functions(addr)) {
3602 addr = CALLER_ADDR2;
3603 if (in_lock_functions(addr))
3604 addr = CALLER_ADDR3;
3605 }
3606 return addr;
3607}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003608
Steven Rostedt7e49fcc2009-01-22 19:01:40 -05003609#if defined(CONFIG_PREEMPT) && (defined(CONFIG_DEBUG_PREEMPT) || \
3610 defined(CONFIG_PREEMPT_TRACER))
3611
Srinivasa Ds43627582008-02-23 15:24:04 -08003612void __kprobes add_preempt_count(int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003613{
Steven Rostedt6cd8a4b2008-05-12 21:20:42 +02003614#ifdef CONFIG_DEBUG_PREEMPT
Linus Torvalds1da177e2005-04-16 15:20:36 -07003615 /*
3616 * Underflow?
3617 */
Ingo Molnar9a11b49a2006-07-03 00:24:33 -07003618 if (DEBUG_LOCKS_WARN_ON((preempt_count() < 0)))
3619 return;
Steven Rostedt6cd8a4b2008-05-12 21:20:42 +02003620#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003621 preempt_count() += val;
Steven Rostedt6cd8a4b2008-05-12 21:20:42 +02003622#ifdef CONFIG_DEBUG_PREEMPT
Linus Torvalds1da177e2005-04-16 15:20:36 -07003623 /*
3624 * Spinlock count overflowing soon?
3625 */
Miguel Ojeda Sandonis33859f72006-12-10 02:20:38 -08003626 DEBUG_LOCKS_WARN_ON((preempt_count() & PREEMPT_MASK) >=
3627 PREEMPT_MASK - 10);
Steven Rostedt6cd8a4b2008-05-12 21:20:42 +02003628#endif
3629 if (preempt_count() == val)
3630 trace_preempt_off(CALLER_ADDR0, get_parent_ip(CALLER_ADDR1));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003631}
3632EXPORT_SYMBOL(add_preempt_count);
3633
Srinivasa Ds43627582008-02-23 15:24:04 -08003634void __kprobes sub_preempt_count(int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003635{
Steven Rostedt6cd8a4b2008-05-12 21:20:42 +02003636#ifdef CONFIG_DEBUG_PREEMPT
Linus Torvalds1da177e2005-04-16 15:20:36 -07003637 /*
3638 * Underflow?
3639 */
Ingo Molnar01e3eb82009-01-12 13:00:50 +01003640 if (DEBUG_LOCKS_WARN_ON(val > preempt_count()))
Ingo Molnar9a11b49a2006-07-03 00:24:33 -07003641 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003642 /*
3643 * Is the spinlock portion underflowing?
3644 */
Ingo Molnar9a11b49a2006-07-03 00:24:33 -07003645 if (DEBUG_LOCKS_WARN_ON((val < PREEMPT_MASK) &&
3646 !(preempt_count() & PREEMPT_MASK)))
3647 return;
Steven Rostedt6cd8a4b2008-05-12 21:20:42 +02003648#endif
Ingo Molnar9a11b49a2006-07-03 00:24:33 -07003649
Steven Rostedt6cd8a4b2008-05-12 21:20:42 +02003650 if (preempt_count() == val)
3651 trace_preempt_on(CALLER_ADDR0, get_parent_ip(CALLER_ADDR1));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003652 preempt_count() -= val;
3653}
3654EXPORT_SYMBOL(sub_preempt_count);
3655
3656#endif
3657
3658/*
Ingo Molnardd41f592007-07-09 18:51:59 +02003659 * Print scheduling while atomic bug:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003660 */
Ingo Molnardd41f592007-07-09 18:51:59 +02003661static noinline void __schedule_bug(struct task_struct *prev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003662{
Satyam Sharma838225b2007-10-24 18:23:50 +02003663 struct pt_regs *regs = get_irq_regs();
3664
Peter Zijlstra3df0fc52009-12-20 14:23:57 +01003665 printk(KERN_ERR "BUG: scheduling while atomic: %s/%d/0x%08x\n",
3666 prev->comm, prev->pid, preempt_count());
Satyam Sharma838225b2007-10-24 18:23:50 +02003667
Ingo Molnardd41f592007-07-09 18:51:59 +02003668 debug_show_held_locks(prev);
Arjan van de Vene21f5b12008-05-23 09:05:58 -07003669 print_modules();
Ingo Molnardd41f592007-07-09 18:51:59 +02003670 if (irqs_disabled())
3671 print_irqtrace_events(prev);
Satyam Sharma838225b2007-10-24 18:23:50 +02003672
3673 if (regs)
3674 show_regs(regs);
3675 else
3676 dump_stack();
Ingo Molnardd41f592007-07-09 18:51:59 +02003677}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003678
Ingo Molnardd41f592007-07-09 18:51:59 +02003679/*
3680 * Various schedule()-time debugging checks and statistics:
3681 */
3682static inline void schedule_debug(struct task_struct *prev)
3683{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003684 /*
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01003685 * Test if we are atomic. Since do_exit() needs to call into
Linus Torvalds1da177e2005-04-16 15:20:36 -07003686 * schedule() atomically, we ignore that path for now.
3687 * Otherwise, whine if we are scheduling when we should not be.
3688 */
Roel Kluin3f33a7c2008-05-13 23:44:11 +02003689 if (unlikely(in_atomic_preempt_off() && !prev->exit_state))
Ingo Molnardd41f592007-07-09 18:51:59 +02003690 __schedule_bug(prev);
3691
Linus Torvalds1da177e2005-04-16 15:20:36 -07003692 profile_hit(SCHED_PROFILING, __builtin_return_address(0));
3693
Ingo Molnar2d723762007-10-15 17:00:12 +02003694 schedstat_inc(this_rq(), sched_count);
Ingo Molnarb8efb562007-10-15 17:00:10 +02003695#ifdef CONFIG_SCHEDSTATS
3696 if (unlikely(prev->lock_depth >= 0)) {
Ingo Molnar2d723762007-10-15 17:00:12 +02003697 schedstat_inc(this_rq(), bkl_count);
3698 schedstat_inc(prev, sched_info.bkl_count);
Ingo Molnarb8efb562007-10-15 17:00:10 +02003699 }
3700#endif
Ingo Molnardd41f592007-07-09 18:51:59 +02003701}
3702
Peter Zijlstra6cecd082009-11-30 13:00:37 +01003703static void put_prev_task(struct rq *rq, struct task_struct *prev)
Mike Galbraithdf1c99d2009-03-10 19:08:11 +01003704{
Mike Galbraitha64692a2010-03-11 17:16:20 +01003705 if (prev->se.on_rq)
3706 update_rq_clock(rq);
3707 rq->skip_clock_update = 0;
Peter Zijlstra6cecd082009-11-30 13:00:37 +01003708 prev->sched_class->put_prev_task(rq, prev);
Mike Galbraithdf1c99d2009-03-10 19:08:11 +01003709}
3710
Ingo Molnardd41f592007-07-09 18:51:59 +02003711/*
3712 * Pick up the highest-prio task:
3713 */
3714static inline struct task_struct *
Wang Chenb67802e2009-03-02 13:55:26 +08003715pick_next_task(struct rq *rq)
Ingo Molnardd41f592007-07-09 18:51:59 +02003716{
Ingo Molnar5522d5d2007-10-15 17:00:12 +02003717 const struct sched_class *class;
Ingo Molnardd41f592007-07-09 18:51:59 +02003718 struct task_struct *p;
3719
3720 /*
3721 * Optimization: we know that if all tasks are in
3722 * the fair class we can call that function directly:
3723 */
3724 if (likely(rq->nr_running == rq->cfs.nr_running)) {
Ingo Molnarfb8d4722007-08-09 11:16:48 +02003725 p = fair_sched_class.pick_next_task(rq);
Ingo Molnardd41f592007-07-09 18:51:59 +02003726 if (likely(p))
3727 return p;
3728 }
3729
Peter Zijlstra34f971f2010-09-22 13:53:15 +02003730 for_each_class(class) {
Ingo Molnarfb8d4722007-08-09 11:16:48 +02003731 p = class->pick_next_task(rq);
Ingo Molnardd41f592007-07-09 18:51:59 +02003732 if (p)
3733 return p;
Ingo Molnardd41f592007-07-09 18:51:59 +02003734 }
Peter Zijlstra34f971f2010-09-22 13:53:15 +02003735
3736 BUG(); /* the idle class will always have a runnable task */
Ingo Molnardd41f592007-07-09 18:51:59 +02003737}
3738
3739/*
3740 * schedule() is the main scheduler function.
3741 */
Peter Zijlstraff743342009-03-13 12:21:26 +01003742asmlinkage void __sched schedule(void)
Ingo Molnardd41f592007-07-09 18:51:59 +02003743{
3744 struct task_struct *prev, *next;
Harvey Harrison67ca7bd2008-02-15 09:56:36 -08003745 unsigned long *switch_count;
Ingo Molnardd41f592007-07-09 18:51:59 +02003746 struct rq *rq;
Peter Zijlstra31656512008-07-18 18:01:23 +02003747 int cpu;
Ingo Molnardd41f592007-07-09 18:51:59 +02003748
Peter Zijlstraff743342009-03-13 12:21:26 +01003749need_resched:
3750 preempt_disable();
Ingo Molnardd41f592007-07-09 18:51:59 +02003751 cpu = smp_processor_id();
3752 rq = cpu_rq(cpu);
Paul E. McKenney25502a62010-04-01 17:37:01 -07003753 rcu_note_context_switch(cpu);
Ingo Molnardd41f592007-07-09 18:51:59 +02003754 prev = rq->curr;
Ingo Molnardd41f592007-07-09 18:51:59 +02003755
Linus Torvalds1da177e2005-04-16 15:20:36 -07003756 release_kernel_lock(prev);
3757need_resched_nonpreemptible:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003758
Ingo Molnardd41f592007-07-09 18:51:59 +02003759 schedule_debug(prev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003760
Peter Zijlstra31656512008-07-18 18:01:23 +02003761 if (sched_feat(HRTICK))
Mike Galbraithf333fdc2008-05-12 21:20:55 +02003762 hrtick_clear(rq);
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01003763
Thomas Gleixner05fa7852009-11-17 14:28:38 +01003764 raw_spin_lock_irq(&rq->lock);
Ingo Molnar1e819952007-10-15 17:00:13 +02003765 clear_tsk_need_resched(prev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003766
Oleg Nesterov246d86b2010-05-19 14:57:11 +02003767 switch_count = &prev->nivcsw;
Ingo Molnardd41f592007-07-09 18:51:59 +02003768 if (prev->state && !(preempt_count() & PREEMPT_ACTIVE)) {
Tejun Heo21aa9af2010-06-08 21:40:37 +02003769 if (unlikely(signal_pending_state(prev->state, prev))) {
Ingo Molnardd41f592007-07-09 18:51:59 +02003770 prev->state = TASK_RUNNING;
Tejun Heo21aa9af2010-06-08 21:40:37 +02003771 } else {
3772 /*
3773 * If a worker is going to sleep, notify and
3774 * ask workqueue whether it wants to wake up a
3775 * task to maintain concurrency. If so, wake
3776 * up the task.
3777 */
3778 if (prev->flags & PF_WQ_WORKER) {
3779 struct task_struct *to_wakeup;
3780
3781 to_wakeup = wq_worker_sleeping(prev, cpu);
3782 if (to_wakeup)
3783 try_to_wake_up_local(to_wakeup);
3784 }
Peter Zijlstra371fd7e2010-03-24 16:38:48 +01003785 deactivate_task(rq, prev, DEQUEUE_SLEEP);
Tejun Heo21aa9af2010-06-08 21:40:37 +02003786 }
Ingo Molnardd41f592007-07-09 18:51:59 +02003787 switch_count = &prev->nvcsw;
3788 }
3789
Gregory Haskins3f029d32009-07-29 11:08:47 -04003790 pre_schedule(rq, prev);
Steven Rostedtf65eda42008-01-25 21:08:07 +01003791
Ingo Molnardd41f592007-07-09 18:51:59 +02003792 if (unlikely(!rq->nr_running))
3793 idle_balance(cpu, rq);
3794
Mike Galbraithdf1c99d2009-03-10 19:08:11 +01003795 put_prev_task(rq, prev);
Wang Chenb67802e2009-03-02 13:55:26 +08003796 next = pick_next_task(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003797
Linus Torvalds1da177e2005-04-16 15:20:36 -07003798 if (likely(prev != next)) {
David Simner673a90a2008-04-29 10:08:59 +01003799 sched_info_switch(prev, next);
Peter Zijlstra49f47432009-12-27 11:51:52 +01003800 perf_event_task_sched_out(prev, next);
David Simner673a90a2008-04-29 10:08:59 +01003801
Linus Torvalds1da177e2005-04-16 15:20:36 -07003802 rq->nr_switches++;
3803 rq->curr = next;
3804 ++*switch_count;
3805
Ingo Molnardd41f592007-07-09 18:51:59 +02003806 context_switch(rq, prev, next); /* unlocks the rq */
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01003807 /*
Oleg Nesterov246d86b2010-05-19 14:57:11 +02003808 * The context switch have flipped the stack from under us
3809 * and restored the local variables which were saved when
3810 * this task called schedule() in the past. prev == current
3811 * is still correct, but it can be moved to another cpu/rq.
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01003812 */
3813 cpu = smp_processor_id();
3814 rq = cpu_rq(cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003815 } else
Thomas Gleixner05fa7852009-11-17 14:28:38 +01003816 raw_spin_unlock_irq(&rq->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003817
Gregory Haskins3f029d32009-07-29 11:08:47 -04003818 post_schedule(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003819
Oleg Nesterov246d86b2010-05-19 14:57:11 +02003820 if (unlikely(reacquire_kernel_lock(prev)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003821 goto need_resched_nonpreemptible;
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01003822
Linus Torvalds1da177e2005-04-16 15:20:36 -07003823 preempt_enable_no_resched();
Peter Zijlstraff743342009-03-13 12:21:26 +01003824 if (need_resched())
Linus Torvalds1da177e2005-04-16 15:20:36 -07003825 goto need_resched;
3826}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003827EXPORT_SYMBOL(schedule);
3828
Frederic Weisbeckerc08f7822009-12-02 20:49:17 +01003829#ifdef CONFIG_MUTEX_SPIN_ON_OWNER
Peter Zijlstra0d66bf62009-01-12 14:01:47 +01003830/*
3831 * Look out! "owner" is an entirely speculative pointer
3832 * access and not reliable.
3833 */
3834int mutex_spin_on_owner(struct mutex *lock, struct thread_info *owner)
3835{
3836 unsigned int cpu;
3837 struct rq *rq;
3838
3839 if (!sched_feat(OWNER_SPIN))
3840 return 0;
3841
3842#ifdef CONFIG_DEBUG_PAGEALLOC
3843 /*
3844 * Need to access the cpu field knowing that
3845 * DEBUG_PAGEALLOC could have unmapped it if
3846 * the mutex owner just released it and exited.
3847 */
3848 if (probe_kernel_address(&owner->cpu, cpu))
Benjamin Herrenschmidt4b402212010-04-16 23:20:00 +02003849 return 0;
Peter Zijlstra0d66bf62009-01-12 14:01:47 +01003850#else
3851 cpu = owner->cpu;
3852#endif
3853
3854 /*
3855 * Even if the access succeeded (likely case),
3856 * the cpu field may no longer be valid.
3857 */
3858 if (cpu >= nr_cpumask_bits)
Benjamin Herrenschmidt4b402212010-04-16 23:20:00 +02003859 return 0;
Peter Zijlstra0d66bf62009-01-12 14:01:47 +01003860
3861 /*
3862 * We need to validate that we can do a
3863 * get_cpu() and that we have the percpu area.
3864 */
3865 if (!cpu_online(cpu))
Benjamin Herrenschmidt4b402212010-04-16 23:20:00 +02003866 return 0;
Peter Zijlstra0d66bf62009-01-12 14:01:47 +01003867
3868 rq = cpu_rq(cpu);
3869
3870 for (;;) {
3871 /*
3872 * Owner changed, break to re-assess state.
3873 */
Tim Chen9d0f4dc2010-08-18 15:00:27 -07003874 if (lock->owner != owner) {
3875 /*
3876 * If the lock has switched to a different owner,
3877 * we likely have heavy contention. Return 0 to quit
3878 * optimistic spinning and not contend further:
3879 */
3880 if (lock->owner)
3881 return 0;
Peter Zijlstra0d66bf62009-01-12 14:01:47 +01003882 break;
Tim Chen9d0f4dc2010-08-18 15:00:27 -07003883 }
Peter Zijlstra0d66bf62009-01-12 14:01:47 +01003884
3885 /*
3886 * Is that owner really running on that cpu?
3887 */
3888 if (task_thread_info(rq->curr) != owner || need_resched())
3889 return 0;
3890
3891 cpu_relax();
3892 }
Benjamin Herrenschmidt4b402212010-04-16 23:20:00 +02003893
Peter Zijlstra0d66bf62009-01-12 14:01:47 +01003894 return 1;
3895}
3896#endif
3897
Linus Torvalds1da177e2005-04-16 15:20:36 -07003898#ifdef CONFIG_PREEMPT
3899/*
Andreas Mohr2ed6e342006-07-10 04:43:52 -07003900 * this is the entry point to schedule() from in-kernel preemption
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01003901 * off of preempt_enable. Kernel preemptions off return from interrupt
Linus Torvalds1da177e2005-04-16 15:20:36 -07003902 * occur there and call schedule directly.
3903 */
Steven Rostedtd1f74e22010-06-02 21:52:29 -04003904asmlinkage void __sched notrace preempt_schedule(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003905{
3906 struct thread_info *ti = current_thread_info();
Ingo Molnar6478d882008-01-25 21:08:33 +01003907
Linus Torvalds1da177e2005-04-16 15:20:36 -07003908 /*
3909 * If there is a non-zero preempt_count or interrupts are disabled,
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01003910 * we do not want to preempt the current task. Just return..
Linus Torvalds1da177e2005-04-16 15:20:36 -07003911 */
Nick Pigginbeed33a2006-10-11 01:21:52 -07003912 if (likely(ti->preempt_count || irqs_disabled()))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003913 return;
3914
Andi Kleen3a5c3592007-10-15 17:00:14 +02003915 do {
Steven Rostedtd1f74e22010-06-02 21:52:29 -04003916 add_preempt_count_notrace(PREEMPT_ACTIVE);
Andi Kleen3a5c3592007-10-15 17:00:14 +02003917 schedule();
Steven Rostedtd1f74e22010-06-02 21:52:29 -04003918 sub_preempt_count_notrace(PREEMPT_ACTIVE);
Andi Kleen3a5c3592007-10-15 17:00:14 +02003919
3920 /*
3921 * Check again in case we missed a preemption opportunity
3922 * between schedule and now.
3923 */
3924 barrier();
Lai Jiangshan5ed0cec2009-03-06 19:40:20 +08003925 } while (need_resched());
Linus Torvalds1da177e2005-04-16 15:20:36 -07003926}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003927EXPORT_SYMBOL(preempt_schedule);
3928
3929/*
Andreas Mohr2ed6e342006-07-10 04:43:52 -07003930 * this is the entry point to schedule() from kernel preemption
Linus Torvalds1da177e2005-04-16 15:20:36 -07003931 * off of irq context.
3932 * Note, that this is called and return with irqs disabled. This will
3933 * protect us against recursive calling from irq.
3934 */
3935asmlinkage void __sched preempt_schedule_irq(void)
3936{
3937 struct thread_info *ti = current_thread_info();
Ingo Molnar6478d882008-01-25 21:08:33 +01003938
Andreas Mohr2ed6e342006-07-10 04:43:52 -07003939 /* Catch callers which need to be fixed */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003940 BUG_ON(ti->preempt_count || !irqs_disabled());
3941
Andi Kleen3a5c3592007-10-15 17:00:14 +02003942 do {
3943 add_preempt_count(PREEMPT_ACTIVE);
Andi Kleen3a5c3592007-10-15 17:00:14 +02003944 local_irq_enable();
3945 schedule();
3946 local_irq_disable();
Andi Kleen3a5c3592007-10-15 17:00:14 +02003947 sub_preempt_count(PREEMPT_ACTIVE);
3948
3949 /*
3950 * Check again in case we missed a preemption opportunity
3951 * between schedule and now.
3952 */
3953 barrier();
Lai Jiangshan5ed0cec2009-03-06 19:40:20 +08003954 } while (need_resched());
Linus Torvalds1da177e2005-04-16 15:20:36 -07003955}
3956
3957#endif /* CONFIG_PREEMPT */
3958
Peter Zijlstra63859d42009-09-15 19:14:42 +02003959int default_wake_function(wait_queue_t *curr, unsigned mode, int wake_flags,
Ingo Molnar95cdf3b2005-09-10 00:26:11 -07003960 void *key)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003961{
Peter Zijlstra63859d42009-09-15 19:14:42 +02003962 return try_to_wake_up(curr->private, mode, wake_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003963}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003964EXPORT_SYMBOL(default_wake_function);
3965
3966/*
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01003967 * The core wakeup function. Non-exclusive wakeups (nr_exclusive == 0) just
3968 * wake everything up. If it's an exclusive wakeup (nr_exclusive == small +ve
Linus Torvalds1da177e2005-04-16 15:20:36 -07003969 * number) then we wake all the non-exclusive tasks and one exclusive task.
3970 *
3971 * There are circumstances in which we can try to wake a task which has already
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01003972 * started to run but is not in state TASK_RUNNING. try_to_wake_up() returns
Linus Torvalds1da177e2005-04-16 15:20:36 -07003973 * zero in this (rare) case, and we handle it by continuing to scan the queue.
3974 */
Johannes Weiner78ddb082009-04-14 16:53:05 +02003975static void __wake_up_common(wait_queue_head_t *q, unsigned int mode,
Peter Zijlstra63859d42009-09-15 19:14:42 +02003976 int nr_exclusive, int wake_flags, void *key)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003977{
Matthias Kaehlcke2e458742007-10-15 17:00:02 +02003978 wait_queue_t *curr, *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003979
Matthias Kaehlcke2e458742007-10-15 17:00:02 +02003980 list_for_each_entry_safe(curr, next, &q->task_list, task_list) {
Ingo Molnar48f24c42006-07-03 00:25:40 -07003981 unsigned flags = curr->flags;
3982
Peter Zijlstra63859d42009-09-15 19:14:42 +02003983 if (curr->func(curr, mode, wake_flags, key) &&
Ingo Molnar48f24c42006-07-03 00:25:40 -07003984 (flags & WQ_FLAG_EXCLUSIVE) && !--nr_exclusive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003985 break;
3986 }
3987}
3988
3989/**
3990 * __wake_up - wake up threads blocked on a waitqueue.
3991 * @q: the waitqueue
3992 * @mode: which threads
3993 * @nr_exclusive: how many wake-one or wake-many threads to wake up
Martin Waitz67be2dd2005-05-01 08:59:26 -07003994 * @key: is directly passed to the wakeup function
David Howells50fa6102009-04-28 15:01:38 +01003995 *
3996 * It may be assumed that this function implies a write memory barrier before
3997 * changing the task state if and only if any tasks are woken up.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003998 */
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -08003999void __wake_up(wait_queue_head_t *q, unsigned int mode,
Ingo Molnar95cdf3b2005-09-10 00:26:11 -07004000 int nr_exclusive, void *key)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004001{
4002 unsigned long flags;
4003
4004 spin_lock_irqsave(&q->lock, flags);
4005 __wake_up_common(q, mode, nr_exclusive, 0, key);
4006 spin_unlock_irqrestore(&q->lock, flags);
4007}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004008EXPORT_SYMBOL(__wake_up);
4009
4010/*
4011 * Same as __wake_up but called with the spinlock in wait_queue_head_t held.
4012 */
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -08004013void __wake_up_locked(wait_queue_head_t *q, unsigned int mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004014{
4015 __wake_up_common(q, mode, 1, 0, NULL);
4016}
Michal Nazarewicz22c43c82010-05-05 12:53:11 +02004017EXPORT_SYMBOL_GPL(__wake_up_locked);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004018
Davide Libenzi4ede8162009-03-31 15:24:20 -07004019void __wake_up_locked_key(wait_queue_head_t *q, unsigned int mode, void *key)
4020{
4021 __wake_up_common(q, mode, 1, 0, key);
4022}
4023
Linus Torvalds1da177e2005-04-16 15:20:36 -07004024/**
Davide Libenzi4ede8162009-03-31 15:24:20 -07004025 * __wake_up_sync_key - wake up threads blocked on a waitqueue.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004026 * @q: the waitqueue
4027 * @mode: which threads
4028 * @nr_exclusive: how many wake-one or wake-many threads to wake up
Davide Libenzi4ede8162009-03-31 15:24:20 -07004029 * @key: opaque value to be passed to wakeup targets
Linus Torvalds1da177e2005-04-16 15:20:36 -07004030 *
4031 * The sync wakeup differs that the waker knows that it will schedule
4032 * away soon, so while the target thread will be woken up, it will not
4033 * be migrated to another CPU - ie. the two threads are 'synchronized'
4034 * with each other. This can prevent needless bouncing between CPUs.
4035 *
4036 * On UP it can prevent extra preemption.
David Howells50fa6102009-04-28 15:01:38 +01004037 *
4038 * It may be assumed that this function implies a write memory barrier before
4039 * changing the task state if and only if any tasks are woken up.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004040 */
Davide Libenzi4ede8162009-03-31 15:24:20 -07004041void __wake_up_sync_key(wait_queue_head_t *q, unsigned int mode,
4042 int nr_exclusive, void *key)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004043{
4044 unsigned long flags;
Peter Zijlstra7d478722009-09-14 19:55:44 +02004045 int wake_flags = WF_SYNC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004046
4047 if (unlikely(!q))
4048 return;
4049
4050 if (unlikely(!nr_exclusive))
Peter Zijlstra7d478722009-09-14 19:55:44 +02004051 wake_flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004052
4053 spin_lock_irqsave(&q->lock, flags);
Peter Zijlstra7d478722009-09-14 19:55:44 +02004054 __wake_up_common(q, mode, nr_exclusive, wake_flags, key);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004055 spin_unlock_irqrestore(&q->lock, flags);
4056}
Davide Libenzi4ede8162009-03-31 15:24:20 -07004057EXPORT_SYMBOL_GPL(__wake_up_sync_key);
4058
4059/*
4060 * __wake_up_sync - see __wake_up_sync_key()
4061 */
4062void __wake_up_sync(wait_queue_head_t *q, unsigned int mode, int nr_exclusive)
4063{
4064 __wake_up_sync_key(q, mode, nr_exclusive, NULL);
4065}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004066EXPORT_SYMBOL_GPL(__wake_up_sync); /* For internal use only */
4067
Kevin Diggs65eb3dc2008-08-26 10:26:54 +02004068/**
4069 * complete: - signals a single thread waiting on this completion
4070 * @x: holds the state of this particular completion
4071 *
4072 * This will wake up a single thread waiting on this completion. Threads will be
4073 * awakened in the same order in which they were queued.
4074 *
4075 * See also complete_all(), wait_for_completion() and related routines.
David Howells50fa6102009-04-28 15:01:38 +01004076 *
4077 * It may be assumed that this function implies a write memory barrier before
4078 * changing the task state if and only if any tasks are woken up.
Kevin Diggs65eb3dc2008-08-26 10:26:54 +02004079 */
Ingo Molnarb15136e2007-10-24 18:23:48 +02004080void complete(struct completion *x)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004081{
4082 unsigned long flags;
4083
4084 spin_lock_irqsave(&x->wait.lock, flags);
4085 x->done++;
Matthew Wilcoxd9514f62007-12-06 11:07:07 -05004086 __wake_up_common(&x->wait, TASK_NORMAL, 1, 0, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004087 spin_unlock_irqrestore(&x->wait.lock, flags);
4088}
4089EXPORT_SYMBOL(complete);
4090
Kevin Diggs65eb3dc2008-08-26 10:26:54 +02004091/**
4092 * complete_all: - signals all threads waiting on this completion
4093 * @x: holds the state of this particular completion
4094 *
4095 * This will wake up all threads waiting on this particular completion event.
David Howells50fa6102009-04-28 15:01:38 +01004096 *
4097 * It may be assumed that this function implies a write memory barrier before
4098 * changing the task state if and only if any tasks are woken up.
Kevin Diggs65eb3dc2008-08-26 10:26:54 +02004099 */
Ingo Molnarb15136e2007-10-24 18:23:48 +02004100void complete_all(struct completion *x)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004101{
4102 unsigned long flags;
4103
4104 spin_lock_irqsave(&x->wait.lock, flags);
4105 x->done += UINT_MAX/2;
Matthew Wilcoxd9514f62007-12-06 11:07:07 -05004106 __wake_up_common(&x->wait, TASK_NORMAL, 0, 0, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004107 spin_unlock_irqrestore(&x->wait.lock, flags);
4108}
4109EXPORT_SYMBOL(complete_all);
4110
Andi Kleen8cbbe862007-10-15 17:00:14 +02004111static inline long __sched
4112do_wait_for_common(struct completion *x, long timeout, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004113{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004114 if (!x->done) {
4115 DECLARE_WAITQUEUE(wait, current);
4116
Changli Gaoa93d2f12010-05-07 14:33:26 +08004117 __add_wait_queue_tail_exclusive(&x->wait, &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004118 do {
Oleg Nesterov94d3d822008-08-20 16:54:41 -07004119 if (signal_pending_state(state, current)) {
Oleg Nesterovea71a542008-06-20 18:32:20 +04004120 timeout = -ERESTARTSYS;
4121 break;
Andi Kleen8cbbe862007-10-15 17:00:14 +02004122 }
4123 __set_current_state(state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004124 spin_unlock_irq(&x->wait.lock);
Andi Kleen8cbbe862007-10-15 17:00:14 +02004125 timeout = schedule_timeout(timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004126 spin_lock_irq(&x->wait.lock);
Oleg Nesterovea71a542008-06-20 18:32:20 +04004127 } while (!x->done && timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004128 __remove_wait_queue(&x->wait, &wait);
Oleg Nesterovea71a542008-06-20 18:32:20 +04004129 if (!x->done)
4130 return timeout;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004131 }
4132 x->done--;
Oleg Nesterovea71a542008-06-20 18:32:20 +04004133 return timeout ?: 1;
Andi Kleen8cbbe862007-10-15 17:00:14 +02004134}
4135
4136static long __sched
4137wait_for_common(struct completion *x, long timeout, int state)
4138{
4139 might_sleep();
4140
4141 spin_lock_irq(&x->wait.lock);
4142 timeout = do_wait_for_common(x, timeout, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004143 spin_unlock_irq(&x->wait.lock);
Andi Kleen8cbbe862007-10-15 17:00:14 +02004144 return timeout;
4145}
4146
Kevin Diggs65eb3dc2008-08-26 10:26:54 +02004147/**
4148 * wait_for_completion: - waits for completion of a task
4149 * @x: holds the state of this particular completion
4150 *
4151 * This waits to be signaled for completion of a specific task. It is NOT
4152 * interruptible and there is no timeout.
4153 *
4154 * See also similar routines (i.e. wait_for_completion_timeout()) with timeout
4155 * and interrupt capability. Also see complete().
4156 */
Ingo Molnarb15136e2007-10-24 18:23:48 +02004157void __sched wait_for_completion(struct completion *x)
Andi Kleen8cbbe862007-10-15 17:00:14 +02004158{
4159 wait_for_common(x, MAX_SCHEDULE_TIMEOUT, TASK_UNINTERRUPTIBLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004160}
4161EXPORT_SYMBOL(wait_for_completion);
4162
Kevin Diggs65eb3dc2008-08-26 10:26:54 +02004163/**
4164 * wait_for_completion_timeout: - waits for completion of a task (w/timeout)
4165 * @x: holds the state of this particular completion
4166 * @timeout: timeout value in jiffies
4167 *
4168 * This waits for either a completion of a specific task to be signaled or for a
4169 * specified timeout to expire. The timeout is in jiffies. It is not
4170 * interruptible.
4171 */
Ingo Molnarb15136e2007-10-24 18:23:48 +02004172unsigned long __sched
Linus Torvalds1da177e2005-04-16 15:20:36 -07004173wait_for_completion_timeout(struct completion *x, unsigned long timeout)
4174{
Andi Kleen8cbbe862007-10-15 17:00:14 +02004175 return wait_for_common(x, timeout, TASK_UNINTERRUPTIBLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004176}
4177EXPORT_SYMBOL(wait_for_completion_timeout);
4178
Kevin Diggs65eb3dc2008-08-26 10:26:54 +02004179/**
4180 * wait_for_completion_interruptible: - waits for completion of a task (w/intr)
4181 * @x: holds the state of this particular completion
4182 *
4183 * This waits for completion of a specific task to be signaled. It is
4184 * interruptible.
4185 */
Andi Kleen8cbbe862007-10-15 17:00:14 +02004186int __sched wait_for_completion_interruptible(struct completion *x)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004187{
Andi Kleen51e97992007-10-18 21:32:55 +02004188 long t = wait_for_common(x, MAX_SCHEDULE_TIMEOUT, TASK_INTERRUPTIBLE);
4189 if (t == -ERESTARTSYS)
4190 return t;
4191 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004192}
4193EXPORT_SYMBOL(wait_for_completion_interruptible);
4194
Kevin Diggs65eb3dc2008-08-26 10:26:54 +02004195/**
4196 * wait_for_completion_interruptible_timeout: - waits for completion (w/(to,intr))
4197 * @x: holds the state of this particular completion
4198 * @timeout: timeout value in jiffies
4199 *
4200 * This waits for either a completion of a specific task to be signaled or for a
4201 * specified timeout to expire. It is interruptible. The timeout is in jiffies.
4202 */
Ingo Molnarb15136e2007-10-24 18:23:48 +02004203unsigned long __sched
Linus Torvalds1da177e2005-04-16 15:20:36 -07004204wait_for_completion_interruptible_timeout(struct completion *x,
4205 unsigned long timeout)
4206{
Andi Kleen8cbbe862007-10-15 17:00:14 +02004207 return wait_for_common(x, timeout, TASK_INTERRUPTIBLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004208}
4209EXPORT_SYMBOL(wait_for_completion_interruptible_timeout);
4210
Kevin Diggs65eb3dc2008-08-26 10:26:54 +02004211/**
4212 * wait_for_completion_killable: - waits for completion of a task (killable)
4213 * @x: holds the state of this particular completion
4214 *
4215 * This waits to be signaled for completion of a specific task. It can be
4216 * interrupted by a kill signal.
4217 */
Matthew Wilcox009e5772007-12-06 12:29:54 -05004218int __sched wait_for_completion_killable(struct completion *x)
4219{
4220 long t = wait_for_common(x, MAX_SCHEDULE_TIMEOUT, TASK_KILLABLE);
4221 if (t == -ERESTARTSYS)
4222 return t;
4223 return 0;
4224}
4225EXPORT_SYMBOL(wait_for_completion_killable);
4226
Dave Chinnerbe4de352008-08-15 00:40:44 -07004227/**
Sage Weil0aa12fb2010-05-29 09:12:30 -07004228 * wait_for_completion_killable_timeout: - waits for completion of a task (w/(to,killable))
4229 * @x: holds the state of this particular completion
4230 * @timeout: timeout value in jiffies
4231 *
4232 * This waits for either a completion of a specific task to be
4233 * signaled or for a specified timeout to expire. It can be
4234 * interrupted by a kill signal. The timeout is in jiffies.
4235 */
4236unsigned long __sched
4237wait_for_completion_killable_timeout(struct completion *x,
4238 unsigned long timeout)
4239{
4240 return wait_for_common(x, timeout, TASK_KILLABLE);
4241}
4242EXPORT_SYMBOL(wait_for_completion_killable_timeout);
4243
4244/**
Dave Chinnerbe4de352008-08-15 00:40:44 -07004245 * try_wait_for_completion - try to decrement a completion without blocking
4246 * @x: completion structure
4247 *
4248 * Returns: 0 if a decrement cannot be done without blocking
4249 * 1 if a decrement succeeded.
4250 *
4251 * If a completion is being used as a counting completion,
4252 * attempt to decrement the counter without blocking. This
4253 * enables us to avoid waiting if the resource the completion
4254 * is protecting is not available.
4255 */
4256bool try_wait_for_completion(struct completion *x)
4257{
Rafael J. Wysocki7539a3b2009-12-13 00:07:30 +01004258 unsigned long flags;
Dave Chinnerbe4de352008-08-15 00:40:44 -07004259 int ret = 1;
4260
Rafael J. Wysocki7539a3b2009-12-13 00:07:30 +01004261 spin_lock_irqsave(&x->wait.lock, flags);
Dave Chinnerbe4de352008-08-15 00:40:44 -07004262 if (!x->done)
4263 ret = 0;
4264 else
4265 x->done--;
Rafael J. Wysocki7539a3b2009-12-13 00:07:30 +01004266 spin_unlock_irqrestore(&x->wait.lock, flags);
Dave Chinnerbe4de352008-08-15 00:40:44 -07004267 return ret;
4268}
4269EXPORT_SYMBOL(try_wait_for_completion);
4270
4271/**
4272 * completion_done - Test to see if a completion has any waiters
4273 * @x: completion structure
4274 *
4275 * Returns: 0 if there are waiters (wait_for_completion() in progress)
4276 * 1 if there are no waiters.
4277 *
4278 */
4279bool completion_done(struct completion *x)
4280{
Rafael J. Wysocki7539a3b2009-12-13 00:07:30 +01004281 unsigned long flags;
Dave Chinnerbe4de352008-08-15 00:40:44 -07004282 int ret = 1;
4283
Rafael J. Wysocki7539a3b2009-12-13 00:07:30 +01004284 spin_lock_irqsave(&x->wait.lock, flags);
Dave Chinnerbe4de352008-08-15 00:40:44 -07004285 if (!x->done)
4286 ret = 0;
Rafael J. Wysocki7539a3b2009-12-13 00:07:30 +01004287 spin_unlock_irqrestore(&x->wait.lock, flags);
Dave Chinnerbe4de352008-08-15 00:40:44 -07004288 return ret;
4289}
4290EXPORT_SYMBOL(completion_done);
4291
Andi Kleen8cbbe862007-10-15 17:00:14 +02004292static long __sched
4293sleep_on_common(wait_queue_head_t *q, int state, long timeout)
Ingo Molnar0fec1712007-07-09 18:52:01 +02004294{
4295 unsigned long flags;
4296 wait_queue_t wait;
4297
4298 init_waitqueue_entry(&wait, current);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004299
Andi Kleen8cbbe862007-10-15 17:00:14 +02004300 __set_current_state(state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004301
Andi Kleen8cbbe862007-10-15 17:00:14 +02004302 spin_lock_irqsave(&q->lock, flags);
4303 __add_wait_queue(q, &wait);
4304 spin_unlock(&q->lock);
4305 timeout = schedule_timeout(timeout);
4306 spin_lock_irq(&q->lock);
4307 __remove_wait_queue(q, &wait);
4308 spin_unlock_irqrestore(&q->lock, flags);
4309
4310 return timeout;
4311}
4312
4313void __sched interruptible_sleep_on(wait_queue_head_t *q)
4314{
4315 sleep_on_common(q, TASK_INTERRUPTIBLE, MAX_SCHEDULE_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004316}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004317EXPORT_SYMBOL(interruptible_sleep_on);
4318
Ingo Molnar0fec1712007-07-09 18:52:01 +02004319long __sched
Ingo Molnar95cdf3b2005-09-10 00:26:11 -07004320interruptible_sleep_on_timeout(wait_queue_head_t *q, long timeout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004321{
Andi Kleen8cbbe862007-10-15 17:00:14 +02004322 return sleep_on_common(q, TASK_INTERRUPTIBLE, timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004323}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004324EXPORT_SYMBOL(interruptible_sleep_on_timeout);
4325
Ingo Molnar0fec1712007-07-09 18:52:01 +02004326void __sched sleep_on(wait_queue_head_t *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004327{
Andi Kleen8cbbe862007-10-15 17:00:14 +02004328 sleep_on_common(q, TASK_UNINTERRUPTIBLE, MAX_SCHEDULE_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004329}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004330EXPORT_SYMBOL(sleep_on);
4331
Ingo Molnar0fec1712007-07-09 18:52:01 +02004332long __sched sleep_on_timeout(wait_queue_head_t *q, long timeout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004333{
Andi Kleen8cbbe862007-10-15 17:00:14 +02004334 return sleep_on_common(q, TASK_UNINTERRUPTIBLE, timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004335}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004336EXPORT_SYMBOL(sleep_on_timeout);
4337
Ingo Molnarb29739f2006-06-27 02:54:51 -07004338#ifdef CONFIG_RT_MUTEXES
4339
4340/*
4341 * rt_mutex_setprio - set the current priority of a task
4342 * @p: task
4343 * @prio: prio value (kernel-internal form)
4344 *
4345 * This function changes the 'effective' priority of a task. It does
4346 * not touch ->normal_prio like __setscheduler().
4347 *
4348 * Used by the rt_mutex code to implement priority inheritance logic.
4349 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07004350void rt_mutex_setprio(struct task_struct *p, int prio)
Ingo Molnarb29739f2006-06-27 02:54:51 -07004351{
4352 unsigned long flags;
Srivatsa Vaddagiri83b699e2007-10-15 17:00:08 +02004353 int oldprio, on_rq, running;
Ingo Molnar70b97a72006-07-03 00:25:42 -07004354 struct rq *rq;
Thomas Gleixner83ab0aa2010-02-17 09:05:48 +01004355 const struct sched_class *prev_class;
Ingo Molnarb29739f2006-06-27 02:54:51 -07004356
4357 BUG_ON(prio < 0 || prio > MAX_PRIO);
4358
4359 rq = task_rq_lock(p, &flags);
4360
Steven Rostedta8027072010-09-20 15:13:34 -04004361 trace_sched_pi_setprio(p, prio);
Andrew Mortond5f9f942007-05-08 20:27:06 -07004362 oldprio = p->prio;
Thomas Gleixner83ab0aa2010-02-17 09:05:48 +01004363 prev_class = p->sched_class;
Ingo Molnardd41f592007-07-09 18:51:59 +02004364 on_rq = p->se.on_rq;
Dmitry Adamushko051a1d12007-12-18 15:21:13 +01004365 running = task_current(rq, p);
Hiroshi Shimamoto0e1f3482008-03-10 11:01:20 -07004366 if (on_rq)
Ingo Molnar69be72c2007-08-09 11:16:49 +02004367 dequeue_task(rq, p, 0);
Hiroshi Shimamoto0e1f3482008-03-10 11:01:20 -07004368 if (running)
4369 p->sched_class->put_prev_task(rq, p);
Ingo Molnardd41f592007-07-09 18:51:59 +02004370
4371 if (rt_prio(prio))
4372 p->sched_class = &rt_sched_class;
4373 else
4374 p->sched_class = &fair_sched_class;
4375
Ingo Molnarb29739f2006-06-27 02:54:51 -07004376 p->prio = prio;
4377
Hiroshi Shimamoto0e1f3482008-03-10 11:01:20 -07004378 if (running)
4379 p->sched_class->set_curr_task(rq);
Ingo Molnardd41f592007-07-09 18:51:59 +02004380 if (on_rq) {
Peter Zijlstra371fd7e2010-03-24 16:38:48 +01004381 enqueue_task(rq, p, oldprio < prio ? ENQUEUE_HEAD : 0);
Steven Rostedtcb469842008-01-25 21:08:22 +01004382
4383 check_class_changed(rq, p, prev_class, oldprio, running);
Ingo Molnarb29739f2006-06-27 02:54:51 -07004384 }
4385 task_rq_unlock(rq, &flags);
4386}
4387
4388#endif
4389
Ingo Molnar36c8b582006-07-03 00:25:41 -07004390void set_user_nice(struct task_struct *p, long nice)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004391{
Ingo Molnardd41f592007-07-09 18:51:59 +02004392 int old_prio, delta, on_rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004393 unsigned long flags;
Ingo Molnar70b97a72006-07-03 00:25:42 -07004394 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004395
4396 if (TASK_NICE(p) == nice || nice < -20 || nice > 19)
4397 return;
4398 /*
4399 * We have to be careful, if called from sys_setpriority(),
4400 * the task might be in the middle of scheduling on another CPU.
4401 */
4402 rq = task_rq_lock(p, &flags);
4403 /*
4404 * The RT priorities are set via sched_setscheduler(), but we still
4405 * allow the 'normal' nice value to be set - but as expected
4406 * it wont have any effect on scheduling until the task is
Ingo Molnardd41f592007-07-09 18:51:59 +02004407 * SCHED_FIFO/SCHED_RR:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004408 */
Ingo Molnare05606d2007-07-09 18:51:59 +02004409 if (task_has_rt_policy(p)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004410 p->static_prio = NICE_TO_PRIO(nice);
4411 goto out_unlock;
4412 }
Ingo Molnardd41f592007-07-09 18:51:59 +02004413 on_rq = p->se.on_rq;
Peter Zijlstrac09595f2008-06-27 13:41:14 +02004414 if (on_rq)
Ingo Molnar69be72c2007-08-09 11:16:49 +02004415 dequeue_task(rq, p, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004416
Linus Torvalds1da177e2005-04-16 15:20:36 -07004417 p->static_prio = NICE_TO_PRIO(nice);
Peter Williams2dd73a42006-06-27 02:54:34 -07004418 set_load_weight(p);
Ingo Molnarb29739f2006-06-27 02:54:51 -07004419 old_prio = p->prio;
4420 p->prio = effective_prio(p);
4421 delta = p->prio - old_prio;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004422
Ingo Molnardd41f592007-07-09 18:51:59 +02004423 if (on_rq) {
Peter Zijlstra371fd7e2010-03-24 16:38:48 +01004424 enqueue_task(rq, p, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004425 /*
Andrew Mortond5f9f942007-05-08 20:27:06 -07004426 * If the task increased its priority or is running and
4427 * lowered its priority, then reschedule its CPU:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004428 */
Andrew Mortond5f9f942007-05-08 20:27:06 -07004429 if (delta < 0 || (delta > 0 && task_running(rq, p)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004430 resched_task(rq->curr);
4431 }
4432out_unlock:
4433 task_rq_unlock(rq, &flags);
4434}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004435EXPORT_SYMBOL(set_user_nice);
4436
Matt Mackalle43379f2005-05-01 08:59:00 -07004437/*
4438 * can_nice - check if a task can reduce its nice value
4439 * @p: task
4440 * @nice: nice value
4441 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07004442int can_nice(const struct task_struct *p, const int nice)
Matt Mackalle43379f2005-05-01 08:59:00 -07004443{
Matt Mackall024f4742005-08-18 11:24:19 -07004444 /* convert nice value [19,-20] to rlimit style value [1,40] */
4445 int nice_rlim = 20 - nice;
Ingo Molnar48f24c42006-07-03 00:25:40 -07004446
Jiri Slaby78d7d402010-03-05 13:42:54 -08004447 return (nice_rlim <= task_rlimit(p, RLIMIT_NICE) ||
Matt Mackalle43379f2005-05-01 08:59:00 -07004448 capable(CAP_SYS_NICE));
4449}
4450
Linus Torvalds1da177e2005-04-16 15:20:36 -07004451#ifdef __ARCH_WANT_SYS_NICE
4452
4453/*
4454 * sys_nice - change the priority of the current process.
4455 * @increment: priority increment
4456 *
4457 * sys_setpriority is a more generic, but much slower function that
4458 * does similar things.
4459 */
Heiko Carstens5add95d2009-01-14 14:14:08 +01004460SYSCALL_DEFINE1(nice, int, increment)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004461{
Ingo Molnar48f24c42006-07-03 00:25:40 -07004462 long nice, retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004463
4464 /*
4465 * Setpriority might change our priority at the same moment.
4466 * We don't have to worry. Conceptually one call occurs first
4467 * and we have a single winner.
4468 */
Matt Mackalle43379f2005-05-01 08:59:00 -07004469 if (increment < -40)
4470 increment = -40;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004471 if (increment > 40)
4472 increment = 40;
4473
Américo Wang2b8f8362009-02-16 18:54:21 +08004474 nice = TASK_NICE(current) + increment;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004475 if (nice < -20)
4476 nice = -20;
4477 if (nice > 19)
4478 nice = 19;
4479
Matt Mackalle43379f2005-05-01 08:59:00 -07004480 if (increment < 0 && !can_nice(current, nice))
4481 return -EPERM;
4482
Linus Torvalds1da177e2005-04-16 15:20:36 -07004483 retval = security_task_setnice(current, nice);
4484 if (retval)
4485 return retval;
4486
4487 set_user_nice(current, nice);
4488 return 0;
4489}
4490
4491#endif
4492
4493/**
4494 * task_prio - return the priority value of a given task.
4495 * @p: the task in question.
4496 *
4497 * This is the priority value as seen by users in /proc.
4498 * RT tasks are offset by -200. Normal tasks are centered
4499 * around 0, value goes from -16 to +15.
4500 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07004501int task_prio(const struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004502{
4503 return p->prio - MAX_RT_PRIO;
4504}
4505
4506/**
4507 * task_nice - return the nice value of a given task.
4508 * @p: the task in question.
4509 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07004510int task_nice(const struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004511{
4512 return TASK_NICE(p);
4513}
Pavel Roskin150d8be2008-03-05 16:56:37 -05004514EXPORT_SYMBOL(task_nice);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004515
4516/**
4517 * idle_cpu - is a given cpu idle currently?
4518 * @cpu: the processor in question.
4519 */
4520int idle_cpu(int cpu)
4521{
4522 return cpu_curr(cpu) == cpu_rq(cpu)->idle;
4523}
4524
Linus Torvalds1da177e2005-04-16 15:20:36 -07004525/**
4526 * idle_task - return the idle task for a given cpu.
4527 * @cpu: the processor in question.
4528 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07004529struct task_struct *idle_task(int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004530{
4531 return cpu_rq(cpu)->idle;
4532}
4533
4534/**
4535 * find_process_by_pid - find a process with a matching PID value.
4536 * @pid: the pid in question.
4537 */
Alexey Dobriyana9957442007-10-15 17:00:13 +02004538static struct task_struct *find_process_by_pid(pid_t pid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004539{
Pavel Emelyanov228ebcb2007-10-18 23:40:16 -07004540 return pid ? find_task_by_vpid(pid) : current;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004541}
4542
4543/* Actually do priority change: must hold rq lock. */
Ingo Molnardd41f592007-07-09 18:51:59 +02004544static void
4545__setscheduler(struct rq *rq, struct task_struct *p, int policy, int prio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004546{
Ingo Molnardd41f592007-07-09 18:51:59 +02004547 BUG_ON(p->se.on_rq);
Ingo Molnar48f24c42006-07-03 00:25:40 -07004548
Linus Torvalds1da177e2005-04-16 15:20:36 -07004549 p->policy = policy;
4550 p->rt_priority = prio;
Ingo Molnarb29739f2006-06-27 02:54:51 -07004551 p->normal_prio = normal_prio(p);
4552 /* we are holding p->pi_lock already */
4553 p->prio = rt_mutex_getprio(p);
Peter Zijlstraffd44db2009-11-10 20:12:01 +01004554 if (rt_prio(p->prio))
4555 p->sched_class = &rt_sched_class;
4556 else
4557 p->sched_class = &fair_sched_class;
Peter Williams2dd73a42006-06-27 02:54:34 -07004558 set_load_weight(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004559}
4560
David Howellsc69e8d92008-11-14 10:39:19 +11004561/*
4562 * check the target process has a UID that matches the current process's
4563 */
4564static bool check_same_owner(struct task_struct *p)
4565{
4566 const struct cred *cred = current_cred(), *pcred;
4567 bool match;
4568
4569 rcu_read_lock();
4570 pcred = __task_cred(p);
4571 match = (cred->euid == pcred->euid ||
4572 cred->euid == pcred->uid);
4573 rcu_read_unlock();
4574 return match;
4575}
4576
Rusty Russell961ccdd2008-06-23 13:55:38 +10004577static int __sched_setscheduler(struct task_struct *p, int policy,
KOSAKI Motohirofe7de492010-10-20 16:01:12 -07004578 const struct sched_param *param, bool user)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004579{
Srivatsa Vaddagiri83b699e2007-10-15 17:00:08 +02004580 int retval, oldprio, oldpolicy = -1, on_rq, running;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004581 unsigned long flags;
Thomas Gleixner83ab0aa2010-02-17 09:05:48 +01004582 const struct sched_class *prev_class;
Ingo Molnar70b97a72006-07-03 00:25:42 -07004583 struct rq *rq;
Lennart Poetteringca94c442009-06-15 17:17:47 +02004584 int reset_on_fork;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004585
Steven Rostedt66e53932006-06-27 02:54:44 -07004586 /* may grab non-irq protected spin_locks */
4587 BUG_ON(in_interrupt());
Linus Torvalds1da177e2005-04-16 15:20:36 -07004588recheck:
4589 /* double check policy once rq lock held */
Lennart Poetteringca94c442009-06-15 17:17:47 +02004590 if (policy < 0) {
4591 reset_on_fork = p->sched_reset_on_fork;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004592 policy = oldpolicy = p->policy;
Lennart Poetteringca94c442009-06-15 17:17:47 +02004593 } else {
4594 reset_on_fork = !!(policy & SCHED_RESET_ON_FORK);
4595 policy &= ~SCHED_RESET_ON_FORK;
4596
4597 if (policy != SCHED_FIFO && policy != SCHED_RR &&
4598 policy != SCHED_NORMAL && policy != SCHED_BATCH &&
4599 policy != SCHED_IDLE)
4600 return -EINVAL;
4601 }
4602
Linus Torvalds1da177e2005-04-16 15:20:36 -07004603 /*
4604 * Valid priorities for SCHED_FIFO and SCHED_RR are
Ingo Molnardd41f592007-07-09 18:51:59 +02004605 * 1..MAX_USER_RT_PRIO-1, valid priority for SCHED_NORMAL,
4606 * SCHED_BATCH and SCHED_IDLE is 0.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004607 */
4608 if (param->sched_priority < 0 ||
Ingo Molnar95cdf3b2005-09-10 00:26:11 -07004609 (p->mm && param->sched_priority > MAX_USER_RT_PRIO-1) ||
Steven Rostedtd46523e2005-07-25 16:28:39 -04004610 (!p->mm && param->sched_priority > MAX_RT_PRIO-1))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004611 return -EINVAL;
Ingo Molnare05606d2007-07-09 18:51:59 +02004612 if (rt_policy(policy) != (param->sched_priority != 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004613 return -EINVAL;
4614
Olivier Croquette37e4ab32005-06-25 14:57:32 -07004615 /*
4616 * Allow unprivileged RT tasks to decrease priority:
4617 */
Rusty Russell961ccdd2008-06-23 13:55:38 +10004618 if (user && !capable(CAP_SYS_NICE)) {
Ingo Molnare05606d2007-07-09 18:51:59 +02004619 if (rt_policy(policy)) {
Oleg Nesterova44702e2010-06-11 01:09:44 +02004620 unsigned long rlim_rtprio =
4621 task_rlimit(p, RLIMIT_RTPRIO);
Oleg Nesterov5fe1d752006-09-29 02:00:48 -07004622
Oleg Nesterov8dc3e902006-09-29 02:00:50 -07004623 /* can't set/change the rt policy */
4624 if (policy != p->policy && !rlim_rtprio)
4625 return -EPERM;
4626
4627 /* can't increase priority */
4628 if (param->sched_priority > p->rt_priority &&
4629 param->sched_priority > rlim_rtprio)
4630 return -EPERM;
4631 }
Ingo Molnardd41f592007-07-09 18:51:59 +02004632 /*
4633 * Like positive nice levels, dont allow tasks to
4634 * move out of SCHED_IDLE either:
4635 */
4636 if (p->policy == SCHED_IDLE && policy != SCHED_IDLE)
4637 return -EPERM;
Oleg Nesterov8dc3e902006-09-29 02:00:50 -07004638
Olivier Croquette37e4ab32005-06-25 14:57:32 -07004639 /* can't change other user's priorities */
David Howellsc69e8d92008-11-14 10:39:19 +11004640 if (!check_same_owner(p))
Olivier Croquette37e4ab32005-06-25 14:57:32 -07004641 return -EPERM;
Lennart Poetteringca94c442009-06-15 17:17:47 +02004642
4643 /* Normal users shall not reset the sched_reset_on_fork flag */
4644 if (p->sched_reset_on_fork && !reset_on_fork)
4645 return -EPERM;
Olivier Croquette37e4ab32005-06-25 14:57:32 -07004646 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004647
Jeremy Fitzhardinge725aad22008-08-03 09:33:03 -07004648 if (user) {
KOSAKI Motohirob0ae1982010-10-15 04:21:18 +09004649 retval = security_task_setscheduler(p);
Jeremy Fitzhardinge725aad22008-08-03 09:33:03 -07004650 if (retval)
4651 return retval;
4652 }
4653
Linus Torvalds1da177e2005-04-16 15:20:36 -07004654 /*
Ingo Molnarb29739f2006-06-27 02:54:51 -07004655 * make sure no PI-waiters arrive (or leave) while we are
4656 * changing the priority of the task:
4657 */
Thomas Gleixner1d615482009-11-17 14:54:03 +01004658 raw_spin_lock_irqsave(&p->pi_lock, flags);
Ingo Molnarb29739f2006-06-27 02:54:51 -07004659 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07004660 * To be able to change p->policy safely, the apropriate
4661 * runqueue lock must be held.
4662 */
Ingo Molnarb29739f2006-06-27 02:54:51 -07004663 rq = __task_rq_lock(p);
Peter Zijlstradc61b1d2010-06-08 11:40:42 +02004664
Peter Zijlstra34f971f2010-09-22 13:53:15 +02004665 /*
4666 * Changing the policy of the stop threads its a very bad idea
4667 */
4668 if (p == rq->stop) {
4669 __task_rq_unlock(rq);
4670 raw_spin_unlock_irqrestore(&p->pi_lock, flags);
4671 return -EINVAL;
4672 }
4673
Peter Zijlstradc61b1d2010-06-08 11:40:42 +02004674#ifdef CONFIG_RT_GROUP_SCHED
4675 if (user) {
4676 /*
4677 * Do not allow realtime tasks into groups that have no runtime
4678 * assigned.
4679 */
4680 if (rt_bandwidth_enabled() && rt_policy(policy) &&
4681 task_group(p)->rt_bandwidth.rt_runtime == 0) {
4682 __task_rq_unlock(rq);
4683 raw_spin_unlock_irqrestore(&p->pi_lock, flags);
4684 return -EPERM;
4685 }
4686 }
4687#endif
4688
Linus Torvalds1da177e2005-04-16 15:20:36 -07004689 /* recheck policy now with rq lock held */
4690 if (unlikely(oldpolicy != -1 && oldpolicy != p->policy)) {
4691 policy = oldpolicy = -1;
Ingo Molnarb29739f2006-06-27 02:54:51 -07004692 __task_rq_unlock(rq);
Thomas Gleixner1d615482009-11-17 14:54:03 +01004693 raw_spin_unlock_irqrestore(&p->pi_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004694 goto recheck;
4695 }
Ingo Molnardd41f592007-07-09 18:51:59 +02004696 on_rq = p->se.on_rq;
Dmitry Adamushko051a1d12007-12-18 15:21:13 +01004697 running = task_current(rq, p);
Hiroshi Shimamoto0e1f3482008-03-10 11:01:20 -07004698 if (on_rq)
Ingo Molnar2e1cb742007-08-09 11:16:49 +02004699 deactivate_task(rq, p, 0);
Hiroshi Shimamoto0e1f3482008-03-10 11:01:20 -07004700 if (running)
4701 p->sched_class->put_prev_task(rq, p);
Dmitry Adamushkof6b53202007-10-15 17:00:08 +02004702
Lennart Poetteringca94c442009-06-15 17:17:47 +02004703 p->sched_reset_on_fork = reset_on_fork;
4704
Linus Torvalds1da177e2005-04-16 15:20:36 -07004705 oldprio = p->prio;
Thomas Gleixner83ab0aa2010-02-17 09:05:48 +01004706 prev_class = p->sched_class;
Ingo Molnardd41f592007-07-09 18:51:59 +02004707 __setscheduler(rq, p, policy, param->sched_priority);
Dmitry Adamushkof6b53202007-10-15 17:00:08 +02004708
Hiroshi Shimamoto0e1f3482008-03-10 11:01:20 -07004709 if (running)
4710 p->sched_class->set_curr_task(rq);
Ingo Molnardd41f592007-07-09 18:51:59 +02004711 if (on_rq) {
4712 activate_task(rq, p, 0);
Steven Rostedtcb469842008-01-25 21:08:22 +01004713
4714 check_class_changed(rq, p, prev_class, oldprio, running);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004715 }
Ingo Molnarb29739f2006-06-27 02:54:51 -07004716 __task_rq_unlock(rq);
Thomas Gleixner1d615482009-11-17 14:54:03 +01004717 raw_spin_unlock_irqrestore(&p->pi_lock, flags);
Ingo Molnarb29739f2006-06-27 02:54:51 -07004718
Thomas Gleixner95e02ca2006-06-27 02:55:02 -07004719 rt_mutex_adjust_pi(p);
4720
Linus Torvalds1da177e2005-04-16 15:20:36 -07004721 return 0;
4722}
Rusty Russell961ccdd2008-06-23 13:55:38 +10004723
4724/**
4725 * sched_setscheduler - change the scheduling policy and/or RT priority of a thread.
4726 * @p: the task in question.
4727 * @policy: new policy.
4728 * @param: structure containing the new RT priority.
4729 *
4730 * NOTE that the task may be already dead.
4731 */
4732int sched_setscheduler(struct task_struct *p, int policy,
KOSAKI Motohirofe7de492010-10-20 16:01:12 -07004733 const struct sched_param *param)
Rusty Russell961ccdd2008-06-23 13:55:38 +10004734{
4735 return __sched_setscheduler(p, policy, param, true);
4736}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004737EXPORT_SYMBOL_GPL(sched_setscheduler);
4738
Rusty Russell961ccdd2008-06-23 13:55:38 +10004739/**
4740 * sched_setscheduler_nocheck - change the scheduling policy and/or RT priority of a thread from kernelspace.
4741 * @p: the task in question.
4742 * @policy: new policy.
4743 * @param: structure containing the new RT priority.
4744 *
4745 * Just like sched_setscheduler, only don't bother checking if the
4746 * current context has permission. For example, this is needed in
4747 * stop_machine(): we create temporary high priority worker threads,
4748 * but our caller might not have that capability.
4749 */
4750int sched_setscheduler_nocheck(struct task_struct *p, int policy,
KOSAKI Motohirofe7de492010-10-20 16:01:12 -07004751 const struct sched_param *param)
Rusty Russell961ccdd2008-06-23 13:55:38 +10004752{
4753 return __sched_setscheduler(p, policy, param, false);
4754}
4755
Ingo Molnar95cdf3b2005-09-10 00:26:11 -07004756static int
4757do_sched_setscheduler(pid_t pid, int policy, struct sched_param __user *param)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004758{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004759 struct sched_param lparam;
4760 struct task_struct *p;
Ingo Molnar36c8b582006-07-03 00:25:41 -07004761 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004762
4763 if (!param || pid < 0)
4764 return -EINVAL;
4765 if (copy_from_user(&lparam, param, sizeof(struct sched_param)))
4766 return -EFAULT;
Oleg Nesterov5fe1d752006-09-29 02:00:48 -07004767
4768 rcu_read_lock();
4769 retval = -ESRCH;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004770 p = find_process_by_pid(pid);
Oleg Nesterov5fe1d752006-09-29 02:00:48 -07004771 if (p != NULL)
4772 retval = sched_setscheduler(p, policy, &lparam);
4773 rcu_read_unlock();
Ingo Molnar36c8b582006-07-03 00:25:41 -07004774
Linus Torvalds1da177e2005-04-16 15:20:36 -07004775 return retval;
4776}
4777
4778/**
4779 * sys_sched_setscheduler - set/change the scheduler policy and RT priority
4780 * @pid: the pid in question.
4781 * @policy: new policy.
4782 * @param: structure containing the new RT priority.
4783 */
Heiko Carstens5add95d2009-01-14 14:14:08 +01004784SYSCALL_DEFINE3(sched_setscheduler, pid_t, pid, int, policy,
4785 struct sched_param __user *, param)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004786{
Jason Baronc21761f2006-01-18 17:43:03 -08004787 /* negative values for policy are not valid */
4788 if (policy < 0)
4789 return -EINVAL;
4790
Linus Torvalds1da177e2005-04-16 15:20:36 -07004791 return do_sched_setscheduler(pid, policy, param);
4792}
4793
4794/**
4795 * sys_sched_setparam - set/change the RT priority of a thread
4796 * @pid: the pid in question.
4797 * @param: structure containing the new RT priority.
4798 */
Heiko Carstens5add95d2009-01-14 14:14:08 +01004799SYSCALL_DEFINE2(sched_setparam, pid_t, pid, struct sched_param __user *, param)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004800{
4801 return do_sched_setscheduler(pid, -1, param);
4802}
4803
4804/**
4805 * sys_sched_getscheduler - get the policy (scheduling class) of a thread
4806 * @pid: the pid in question.
4807 */
Heiko Carstens5add95d2009-01-14 14:14:08 +01004808SYSCALL_DEFINE1(sched_getscheduler, pid_t, pid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004809{
Ingo Molnar36c8b582006-07-03 00:25:41 -07004810 struct task_struct *p;
Andi Kleen3a5c3592007-10-15 17:00:14 +02004811 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004812
4813 if (pid < 0)
Andi Kleen3a5c3592007-10-15 17:00:14 +02004814 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004815
4816 retval = -ESRCH;
Thomas Gleixner5fe85be2009-12-09 10:14:58 +00004817 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004818 p = find_process_by_pid(pid);
4819 if (p) {
4820 retval = security_task_getscheduler(p);
4821 if (!retval)
Lennart Poetteringca94c442009-06-15 17:17:47 +02004822 retval = p->policy
4823 | (p->sched_reset_on_fork ? SCHED_RESET_ON_FORK : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004824 }
Thomas Gleixner5fe85be2009-12-09 10:14:58 +00004825 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004826 return retval;
4827}
4828
4829/**
Lennart Poetteringca94c442009-06-15 17:17:47 +02004830 * sys_sched_getparam - get the RT priority of a thread
Linus Torvalds1da177e2005-04-16 15:20:36 -07004831 * @pid: the pid in question.
4832 * @param: structure containing the RT priority.
4833 */
Heiko Carstens5add95d2009-01-14 14:14:08 +01004834SYSCALL_DEFINE2(sched_getparam, pid_t, pid, struct sched_param __user *, param)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004835{
4836 struct sched_param lp;
Ingo Molnar36c8b582006-07-03 00:25:41 -07004837 struct task_struct *p;
Andi Kleen3a5c3592007-10-15 17:00:14 +02004838 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004839
4840 if (!param || pid < 0)
Andi Kleen3a5c3592007-10-15 17:00:14 +02004841 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004842
Thomas Gleixner5fe85be2009-12-09 10:14:58 +00004843 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004844 p = find_process_by_pid(pid);
4845 retval = -ESRCH;
4846 if (!p)
4847 goto out_unlock;
4848
4849 retval = security_task_getscheduler(p);
4850 if (retval)
4851 goto out_unlock;
4852
4853 lp.sched_priority = p->rt_priority;
Thomas Gleixner5fe85be2009-12-09 10:14:58 +00004854 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004855
4856 /*
4857 * This one might sleep, we cannot do it with a spinlock held ...
4858 */
4859 retval = copy_to_user(param, &lp, sizeof(*param)) ? -EFAULT : 0;
4860
Linus Torvalds1da177e2005-04-16 15:20:36 -07004861 return retval;
4862
4863out_unlock:
Thomas Gleixner5fe85be2009-12-09 10:14:58 +00004864 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004865 return retval;
4866}
4867
Rusty Russell96f874e2008-11-25 02:35:14 +10304868long sched_setaffinity(pid_t pid, const struct cpumask *in_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004869{
Rusty Russell5a16f3d2008-11-25 02:35:11 +10304870 cpumask_var_t cpus_allowed, new_mask;
Ingo Molnar36c8b582006-07-03 00:25:41 -07004871 struct task_struct *p;
4872 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004873
Gautham R Shenoy95402b32008-01-25 21:08:02 +01004874 get_online_cpus();
Thomas Gleixner23f5d142009-12-09 10:15:01 +00004875 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004876
4877 p = find_process_by_pid(pid);
4878 if (!p) {
Thomas Gleixner23f5d142009-12-09 10:15:01 +00004879 rcu_read_unlock();
Gautham R Shenoy95402b32008-01-25 21:08:02 +01004880 put_online_cpus();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004881 return -ESRCH;
4882 }
4883
Thomas Gleixner23f5d142009-12-09 10:15:01 +00004884 /* Prevent p going away */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004885 get_task_struct(p);
Thomas Gleixner23f5d142009-12-09 10:15:01 +00004886 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004887
Rusty Russell5a16f3d2008-11-25 02:35:11 +10304888 if (!alloc_cpumask_var(&cpus_allowed, GFP_KERNEL)) {
4889 retval = -ENOMEM;
4890 goto out_put_task;
4891 }
4892 if (!alloc_cpumask_var(&new_mask, GFP_KERNEL)) {
4893 retval = -ENOMEM;
4894 goto out_free_cpus_allowed;
4895 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004896 retval = -EPERM;
David Howellsc69e8d92008-11-14 10:39:19 +11004897 if (!check_same_owner(p) && !capable(CAP_SYS_NICE))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004898 goto out_unlock;
4899
KOSAKI Motohirob0ae1982010-10-15 04:21:18 +09004900 retval = security_task_setscheduler(p);
David Quigleye7834f82006-06-23 02:03:59 -07004901 if (retval)
4902 goto out_unlock;
4903
Rusty Russell5a16f3d2008-11-25 02:35:11 +10304904 cpuset_cpus_allowed(p, cpus_allowed);
4905 cpumask_and(new_mask, in_mask, cpus_allowed);
Peter Zijlstra49246272010-10-17 21:46:10 +02004906again:
Rusty Russell5a16f3d2008-11-25 02:35:11 +10304907 retval = set_cpus_allowed_ptr(p, new_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004908
Paul Menage8707d8b2007-10-18 23:40:22 -07004909 if (!retval) {
Rusty Russell5a16f3d2008-11-25 02:35:11 +10304910 cpuset_cpus_allowed(p, cpus_allowed);
4911 if (!cpumask_subset(new_mask, cpus_allowed)) {
Paul Menage8707d8b2007-10-18 23:40:22 -07004912 /*
4913 * We must have raced with a concurrent cpuset
4914 * update. Just reset the cpus_allowed to the
4915 * cpuset's cpus_allowed
4916 */
Rusty Russell5a16f3d2008-11-25 02:35:11 +10304917 cpumask_copy(new_mask, cpus_allowed);
Paul Menage8707d8b2007-10-18 23:40:22 -07004918 goto again;
4919 }
4920 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004921out_unlock:
Rusty Russell5a16f3d2008-11-25 02:35:11 +10304922 free_cpumask_var(new_mask);
4923out_free_cpus_allowed:
4924 free_cpumask_var(cpus_allowed);
4925out_put_task:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004926 put_task_struct(p);
Gautham R Shenoy95402b32008-01-25 21:08:02 +01004927 put_online_cpus();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004928 return retval;
4929}
4930
4931static int get_user_cpu_mask(unsigned long __user *user_mask_ptr, unsigned len,
Rusty Russell96f874e2008-11-25 02:35:14 +10304932 struct cpumask *new_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004933{
Rusty Russell96f874e2008-11-25 02:35:14 +10304934 if (len < cpumask_size())
4935 cpumask_clear(new_mask);
4936 else if (len > cpumask_size())
4937 len = cpumask_size();
4938
Linus Torvalds1da177e2005-04-16 15:20:36 -07004939 return copy_from_user(new_mask, user_mask_ptr, len) ? -EFAULT : 0;
4940}
4941
4942/**
4943 * sys_sched_setaffinity - set the cpu affinity of a process
4944 * @pid: pid of the process
4945 * @len: length in bytes of the bitmask pointed to by user_mask_ptr
4946 * @user_mask_ptr: user-space pointer to the new cpu mask
4947 */
Heiko Carstens5add95d2009-01-14 14:14:08 +01004948SYSCALL_DEFINE3(sched_setaffinity, pid_t, pid, unsigned int, len,
4949 unsigned long __user *, user_mask_ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004950{
Rusty Russell5a16f3d2008-11-25 02:35:11 +10304951 cpumask_var_t new_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004952 int retval;
4953
Rusty Russell5a16f3d2008-11-25 02:35:11 +10304954 if (!alloc_cpumask_var(&new_mask, GFP_KERNEL))
4955 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004956
Rusty Russell5a16f3d2008-11-25 02:35:11 +10304957 retval = get_user_cpu_mask(user_mask_ptr, len, new_mask);
4958 if (retval == 0)
4959 retval = sched_setaffinity(pid, new_mask);
4960 free_cpumask_var(new_mask);
4961 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004962}
4963
Rusty Russell96f874e2008-11-25 02:35:14 +10304964long sched_getaffinity(pid_t pid, struct cpumask *mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004965{
Ingo Molnar36c8b582006-07-03 00:25:41 -07004966 struct task_struct *p;
Thomas Gleixner31605682009-12-08 20:24:16 +00004967 unsigned long flags;
4968 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004969 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004970
Gautham R Shenoy95402b32008-01-25 21:08:02 +01004971 get_online_cpus();
Thomas Gleixner23f5d142009-12-09 10:15:01 +00004972 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004973
4974 retval = -ESRCH;
4975 p = find_process_by_pid(pid);
4976 if (!p)
4977 goto out_unlock;
4978
David Quigleye7834f82006-06-23 02:03:59 -07004979 retval = security_task_getscheduler(p);
4980 if (retval)
4981 goto out_unlock;
4982
Thomas Gleixner31605682009-12-08 20:24:16 +00004983 rq = task_rq_lock(p, &flags);
Rusty Russell96f874e2008-11-25 02:35:14 +10304984 cpumask_and(mask, &p->cpus_allowed, cpu_online_mask);
Thomas Gleixner31605682009-12-08 20:24:16 +00004985 task_rq_unlock(rq, &flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004986
4987out_unlock:
Thomas Gleixner23f5d142009-12-09 10:15:01 +00004988 rcu_read_unlock();
Gautham R Shenoy95402b32008-01-25 21:08:02 +01004989 put_online_cpus();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004990
Ulrich Drepper9531b622007-08-09 11:16:46 +02004991 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004992}
4993
4994/**
4995 * sys_sched_getaffinity - get the cpu affinity of a process
4996 * @pid: pid of the process
4997 * @len: length in bytes of the bitmask pointed to by user_mask_ptr
4998 * @user_mask_ptr: user-space pointer to hold the current cpu mask
4999 */
Heiko Carstens5add95d2009-01-14 14:14:08 +01005000SYSCALL_DEFINE3(sched_getaffinity, pid_t, pid, unsigned int, len,
5001 unsigned long __user *, user_mask_ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005002{
5003 int ret;
Rusty Russellf17c8602008-11-25 02:35:11 +10305004 cpumask_var_t mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005005
Anton Blanchard84fba5e2010-04-06 17:02:19 +10005006 if ((len * BITS_PER_BYTE) < nr_cpu_ids)
KOSAKI Motohirocd3d8032010-03-12 16:15:36 +09005007 return -EINVAL;
5008 if (len & (sizeof(unsigned long)-1))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005009 return -EINVAL;
5010
Rusty Russellf17c8602008-11-25 02:35:11 +10305011 if (!alloc_cpumask_var(&mask, GFP_KERNEL))
5012 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005013
Rusty Russellf17c8602008-11-25 02:35:11 +10305014 ret = sched_getaffinity(pid, mask);
5015 if (ret == 0) {
KOSAKI Motohiro8bc037f2010-03-17 09:36:58 +09005016 size_t retlen = min_t(size_t, len, cpumask_size());
KOSAKI Motohirocd3d8032010-03-12 16:15:36 +09005017
5018 if (copy_to_user(user_mask_ptr, mask, retlen))
Rusty Russellf17c8602008-11-25 02:35:11 +10305019 ret = -EFAULT;
5020 else
KOSAKI Motohirocd3d8032010-03-12 16:15:36 +09005021 ret = retlen;
Rusty Russellf17c8602008-11-25 02:35:11 +10305022 }
5023 free_cpumask_var(mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005024
Rusty Russellf17c8602008-11-25 02:35:11 +10305025 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005026}
5027
5028/**
5029 * sys_sched_yield - yield the current processor to other threads.
5030 *
Ingo Molnardd41f592007-07-09 18:51:59 +02005031 * This function yields the current CPU to other tasks. If there are no
5032 * other threads running on this CPU then this function will return.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005033 */
Heiko Carstens5add95d2009-01-14 14:14:08 +01005034SYSCALL_DEFINE0(sched_yield)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005035{
Ingo Molnar70b97a72006-07-03 00:25:42 -07005036 struct rq *rq = this_rq_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005037
Ingo Molnar2d723762007-10-15 17:00:12 +02005038 schedstat_inc(rq, yld_count);
Dmitry Adamushko4530d7a2007-10-15 17:00:08 +02005039 current->sched_class->yield_task(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005040
5041 /*
5042 * Since we are going to call schedule() anyway, there's
5043 * no need to preempt or enable interrupts:
5044 */
5045 __release(rq->lock);
Ingo Molnar8a25d5d2006-07-03 00:24:54 -07005046 spin_release(&rq->lock.dep_map, 1, _THIS_IP_);
Thomas Gleixner9828ea92009-12-03 20:55:53 +01005047 do_raw_spin_unlock(&rq->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005048 preempt_enable_no_resched();
5049
5050 schedule();
5051
5052 return 0;
5053}
5054
Peter Zijlstrad86ee482009-07-10 14:57:57 +02005055static inline int should_resched(void)
5056{
5057 return need_resched() && !(preempt_count() & PREEMPT_ACTIVE);
5058}
5059
Andrew Mortone7b38402006-06-30 01:56:00 -07005060static void __cond_resched(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005061{
Frederic Weisbeckere7aaaa62009-07-16 15:44:29 +02005062 add_preempt_count(PREEMPT_ACTIVE);
5063 schedule();
5064 sub_preempt_count(PREEMPT_ACTIVE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005065}
5066
Herbert Xu02b67cc2008-01-25 21:08:28 +01005067int __sched _cond_resched(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005068{
Peter Zijlstrad86ee482009-07-10 14:57:57 +02005069 if (should_resched()) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005070 __cond_resched();
5071 return 1;
5072 }
5073 return 0;
5074}
Herbert Xu02b67cc2008-01-25 21:08:28 +01005075EXPORT_SYMBOL(_cond_resched);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005076
5077/*
Frederic Weisbecker613afbf2009-07-16 15:44:29 +02005078 * __cond_resched_lock() - if a reschedule is pending, drop the given lock,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005079 * call schedule, and on return reacquire the lock.
5080 *
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01005081 * This works OK both with and without CONFIG_PREEMPT. We do strange low-level
Linus Torvalds1da177e2005-04-16 15:20:36 -07005082 * operations here to prevent schedule() from being called twice (once via
5083 * spin_unlock(), once by hand).
5084 */
Frederic Weisbecker613afbf2009-07-16 15:44:29 +02005085int __cond_resched_lock(spinlock_t *lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005086{
Peter Zijlstrad86ee482009-07-10 14:57:57 +02005087 int resched = should_resched();
Jan Kara6df3cec2005-06-13 15:52:32 -07005088 int ret = 0;
5089
Peter Zijlstraf607c662009-07-20 19:16:29 +02005090 lockdep_assert_held(lock);
5091
Nick Piggin95c354f2008-01-30 13:31:20 +01005092 if (spin_needbreak(lock) || resched) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005093 spin_unlock(lock);
Peter Zijlstrad86ee482009-07-10 14:57:57 +02005094 if (resched)
Nick Piggin95c354f2008-01-30 13:31:20 +01005095 __cond_resched();
5096 else
5097 cpu_relax();
Jan Kara6df3cec2005-06-13 15:52:32 -07005098 ret = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005099 spin_lock(lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005100 }
Jan Kara6df3cec2005-06-13 15:52:32 -07005101 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005102}
Frederic Weisbecker613afbf2009-07-16 15:44:29 +02005103EXPORT_SYMBOL(__cond_resched_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005104
Frederic Weisbecker613afbf2009-07-16 15:44:29 +02005105int __sched __cond_resched_softirq(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005106{
5107 BUG_ON(!in_softirq());
5108
Peter Zijlstrad86ee482009-07-10 14:57:57 +02005109 if (should_resched()) {
Thomas Gleixner98d82562007-05-23 13:58:18 -07005110 local_bh_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005111 __cond_resched();
5112 local_bh_disable();
5113 return 1;
5114 }
5115 return 0;
5116}
Frederic Weisbecker613afbf2009-07-16 15:44:29 +02005117EXPORT_SYMBOL(__cond_resched_softirq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005118
Linus Torvalds1da177e2005-04-16 15:20:36 -07005119/**
5120 * yield - yield the current processor to other threads.
5121 *
Robert P. J. Day72fd4a32007-02-10 01:45:59 -08005122 * This is a shortcut for kernel-space yielding - it marks the
Linus Torvalds1da177e2005-04-16 15:20:36 -07005123 * thread runnable and calls sys_sched_yield().
5124 */
5125void __sched yield(void)
5126{
5127 set_current_state(TASK_RUNNING);
5128 sys_sched_yield();
5129}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005130EXPORT_SYMBOL(yield);
5131
5132/*
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01005133 * This task is about to go to sleep on IO. Increment rq->nr_iowait so
Linus Torvalds1da177e2005-04-16 15:20:36 -07005134 * that process accounting knows that this is a task in IO wait state.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005135 */
5136void __sched io_schedule(void)
5137{
Hitoshi Mitake54d35f22009-06-29 14:44:57 +09005138 struct rq *rq = raw_rq();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005139
Shailabh Nagar0ff92242006-07-14 00:24:37 -07005140 delayacct_blkio_start();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005141 atomic_inc(&rq->nr_iowait);
Arjan van de Ven8f0dfc32009-07-20 11:26:58 -07005142 current->in_iowait = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005143 schedule();
Arjan van de Ven8f0dfc32009-07-20 11:26:58 -07005144 current->in_iowait = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005145 atomic_dec(&rq->nr_iowait);
Shailabh Nagar0ff92242006-07-14 00:24:37 -07005146 delayacct_blkio_end();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005147}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005148EXPORT_SYMBOL(io_schedule);
5149
5150long __sched io_schedule_timeout(long timeout)
5151{
Hitoshi Mitake54d35f22009-06-29 14:44:57 +09005152 struct rq *rq = raw_rq();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005153 long ret;
5154
Shailabh Nagar0ff92242006-07-14 00:24:37 -07005155 delayacct_blkio_start();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005156 atomic_inc(&rq->nr_iowait);
Arjan van de Ven8f0dfc32009-07-20 11:26:58 -07005157 current->in_iowait = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005158 ret = schedule_timeout(timeout);
Arjan van de Ven8f0dfc32009-07-20 11:26:58 -07005159 current->in_iowait = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005160 atomic_dec(&rq->nr_iowait);
Shailabh Nagar0ff92242006-07-14 00:24:37 -07005161 delayacct_blkio_end();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005162 return ret;
5163}
5164
5165/**
5166 * sys_sched_get_priority_max - return maximum RT priority.
5167 * @policy: scheduling class.
5168 *
5169 * this syscall returns the maximum rt_priority that can be used
5170 * by a given scheduling class.
5171 */
Heiko Carstens5add95d2009-01-14 14:14:08 +01005172SYSCALL_DEFINE1(sched_get_priority_max, int, policy)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005173{
5174 int ret = -EINVAL;
5175
5176 switch (policy) {
5177 case SCHED_FIFO:
5178 case SCHED_RR:
5179 ret = MAX_USER_RT_PRIO-1;
5180 break;
5181 case SCHED_NORMAL:
Ingo Molnarb0a94992006-01-14 13:20:41 -08005182 case SCHED_BATCH:
Ingo Molnardd41f592007-07-09 18:51:59 +02005183 case SCHED_IDLE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005184 ret = 0;
5185 break;
5186 }
5187 return ret;
5188}
5189
5190/**
5191 * sys_sched_get_priority_min - return minimum RT priority.
5192 * @policy: scheduling class.
5193 *
5194 * this syscall returns the minimum rt_priority that can be used
5195 * by a given scheduling class.
5196 */
Heiko Carstens5add95d2009-01-14 14:14:08 +01005197SYSCALL_DEFINE1(sched_get_priority_min, int, policy)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005198{
5199 int ret = -EINVAL;
5200
5201 switch (policy) {
5202 case SCHED_FIFO:
5203 case SCHED_RR:
5204 ret = 1;
5205 break;
5206 case SCHED_NORMAL:
Ingo Molnarb0a94992006-01-14 13:20:41 -08005207 case SCHED_BATCH:
Ingo Molnardd41f592007-07-09 18:51:59 +02005208 case SCHED_IDLE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005209 ret = 0;
5210 }
5211 return ret;
5212}
5213
5214/**
5215 * sys_sched_rr_get_interval - return the default timeslice of a process.
5216 * @pid: pid of the process.
5217 * @interval: userspace pointer to the timeslice value.
5218 *
5219 * this syscall writes the default timeslice value of a given process
5220 * into the user-space timespec buffer. A value of '0' means infinity.
5221 */
Heiko Carstens17da2bd2009-01-14 14:14:10 +01005222SYSCALL_DEFINE2(sched_rr_get_interval, pid_t, pid,
Heiko Carstens754fe8d2009-01-14 14:14:09 +01005223 struct timespec __user *, interval)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005224{
Ingo Molnar36c8b582006-07-03 00:25:41 -07005225 struct task_struct *p;
Dmitry Adamushkoa4ec24b2007-10-15 17:00:13 +02005226 unsigned int time_slice;
Thomas Gleixnerdba091b2009-12-09 09:32:03 +01005227 unsigned long flags;
5228 struct rq *rq;
Andi Kleen3a5c3592007-10-15 17:00:14 +02005229 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005230 struct timespec t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005231
5232 if (pid < 0)
Andi Kleen3a5c3592007-10-15 17:00:14 +02005233 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005234
5235 retval = -ESRCH;
Thomas Gleixner1a551ae2009-12-09 10:15:11 +00005236 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005237 p = find_process_by_pid(pid);
5238 if (!p)
5239 goto out_unlock;
5240
5241 retval = security_task_getscheduler(p);
5242 if (retval)
5243 goto out_unlock;
5244
Thomas Gleixnerdba091b2009-12-09 09:32:03 +01005245 rq = task_rq_lock(p, &flags);
5246 time_slice = p->sched_class->get_rr_interval(rq, p);
5247 task_rq_unlock(rq, &flags);
Dmitry Adamushkoa4ec24b2007-10-15 17:00:13 +02005248
Thomas Gleixner1a551ae2009-12-09 10:15:11 +00005249 rcu_read_unlock();
Dmitry Adamushkoa4ec24b2007-10-15 17:00:13 +02005250 jiffies_to_timespec(time_slice, &t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005251 retval = copy_to_user(interval, &t, sizeof(t)) ? -EFAULT : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005252 return retval;
Andi Kleen3a5c3592007-10-15 17:00:14 +02005253
Linus Torvalds1da177e2005-04-16 15:20:36 -07005254out_unlock:
Thomas Gleixner1a551ae2009-12-09 10:15:11 +00005255 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005256 return retval;
5257}
5258
Steven Rostedt7c731e02008-05-12 21:20:41 +02005259static const char stat_nam[] = TASK_STATE_TO_CHAR_STR;
Ingo Molnar36c8b582006-07-03 00:25:41 -07005260
Ingo Molnar82a1fcb2008-01-25 21:08:02 +01005261void sched_show_task(struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005262{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005263 unsigned long free = 0;
Ingo Molnar36c8b582006-07-03 00:25:41 -07005264 unsigned state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005265
Linus Torvalds1da177e2005-04-16 15:20:36 -07005266 state = p->state ? __ffs(p->state) + 1 : 0;
Erik Gilling28d06862010-11-19 18:08:51 -08005267 printk(KERN_INFO "%-15.15s %c", p->comm,
Andreas Mohr2ed6e342006-07-10 04:43:52 -07005268 state < sizeof(stat_nam) - 1 ? stat_nam[state] : '?');
Ingo Molnar4bd77322007-07-11 21:21:47 +02005269#if BITS_PER_LONG == 32
Linus Torvalds1da177e2005-04-16 15:20:36 -07005270 if (state == TASK_RUNNING)
Peter Zijlstra3df0fc52009-12-20 14:23:57 +01005271 printk(KERN_CONT " running ");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005272 else
Peter Zijlstra3df0fc52009-12-20 14:23:57 +01005273 printk(KERN_CONT " %08lx ", thread_saved_pc(p));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005274#else
5275 if (state == TASK_RUNNING)
Peter Zijlstra3df0fc52009-12-20 14:23:57 +01005276 printk(KERN_CONT " running task ");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005277 else
Peter Zijlstra3df0fc52009-12-20 14:23:57 +01005278 printk(KERN_CONT " %016lx ", thread_saved_pc(p));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005279#endif
5280#ifdef CONFIG_DEBUG_STACK_USAGE
Eric Sandeen7c9f8862008-04-22 16:38:23 -05005281 free = stack_not_used(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005282#endif
Peter Zijlstra3df0fc52009-12-20 14:23:57 +01005283 printk(KERN_CONT "%5lu %5d %6d 0x%08lx\n", free,
David Rientjesaa47b7e2009-05-04 01:38:05 -07005284 task_pid_nr(p), task_pid_nr(p->real_parent),
5285 (unsigned long)task_thread_info(p)->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005286
Nick Piggin5fb5e6d2008-01-25 21:08:34 +01005287 show_stack(p, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005288}
5289
Ingo Molnare59e2ae2006-12-06 20:35:59 -08005290void show_state_filter(unsigned long state_filter)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005291{
Ingo Molnar36c8b582006-07-03 00:25:41 -07005292 struct task_struct *g, *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005293
Ingo Molnar4bd77322007-07-11 21:21:47 +02005294#if BITS_PER_LONG == 32
Peter Zijlstra3df0fc52009-12-20 14:23:57 +01005295 printk(KERN_INFO
5296 " task PC stack pid father\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005297#else
Peter Zijlstra3df0fc52009-12-20 14:23:57 +01005298 printk(KERN_INFO
5299 " task PC stack pid father\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005300#endif
5301 read_lock(&tasklist_lock);
5302 do_each_thread(g, p) {
5303 /*
5304 * reset the NMI-timeout, listing all files on a slow
5305 * console might take alot of time:
5306 */
5307 touch_nmi_watchdog();
Ingo Molnar39bc89f2007-04-25 20:50:03 -07005308 if (!state_filter || (p->state & state_filter))
Ingo Molnar82a1fcb2008-01-25 21:08:02 +01005309 sched_show_task(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005310 } while_each_thread(g, p);
5311
Jeremy Fitzhardinge04c91672007-05-08 00:28:05 -07005312 touch_all_softlockup_watchdogs();
5313
Ingo Molnardd41f592007-07-09 18:51:59 +02005314#ifdef CONFIG_SCHED_DEBUG
5315 sysrq_sched_debug_show();
5316#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07005317 read_unlock(&tasklist_lock);
Ingo Molnare59e2ae2006-12-06 20:35:59 -08005318 /*
5319 * Only show locks if all tasks are dumped:
5320 */
Shmulik Ladkani93335a22009-11-25 15:23:41 +02005321 if (!state_filter)
Ingo Molnare59e2ae2006-12-06 20:35:59 -08005322 debug_show_all_locks();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005323}
5324
Ingo Molnar1df21052007-07-09 18:51:58 +02005325void __cpuinit init_idle_bootup_task(struct task_struct *idle)
5326{
Ingo Molnardd41f592007-07-09 18:51:59 +02005327 idle->sched_class = &idle_sched_class;
Ingo Molnar1df21052007-07-09 18:51:58 +02005328}
5329
Ingo Molnarf340c0d2005-06-28 16:40:42 +02005330/**
5331 * init_idle - set up an idle thread for a given CPU
5332 * @idle: task in question
5333 * @cpu: cpu the idle task belongs to
5334 *
5335 * NOTE: this function does not set the idle thread's NEED_RESCHED
5336 * flag, to make booting more robust.
5337 */
Nick Piggin5c1e1762006-10-03 01:14:04 -07005338void __cpuinit init_idle(struct task_struct *idle, int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005339{
Ingo Molnar70b97a72006-07-03 00:25:42 -07005340 struct rq *rq = cpu_rq(cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005341 unsigned long flags;
5342
Thomas Gleixner05fa7852009-11-17 14:28:38 +01005343 raw_spin_lock_irqsave(&rq->lock, flags);
Ingo Molnar5cbd54e2008-11-12 20:05:50 +01005344
Ingo Molnardd41f592007-07-09 18:51:59 +02005345 __sched_fork(idle);
Peter Zijlstra06b83b52009-12-16 18:04:35 +01005346 idle->state = TASK_RUNNING;
Ingo Molnardd41f592007-07-09 18:51:59 +02005347 idle->se.exec_start = sched_clock();
5348
Rusty Russell96f874e2008-11-25 02:35:14 +10305349 cpumask_copy(&idle->cpus_allowed, cpumask_of(cpu));
Peter Zijlstra6506cf6c2010-09-16 17:50:31 +02005350 /*
5351 * We're having a chicken and egg problem, even though we are
5352 * holding rq->lock, the cpu isn't yet set to this cpu so the
5353 * lockdep check in task_group() will fail.
5354 *
5355 * Similar case to sched_fork(). / Alternatively we could
5356 * use task_rq_lock() here and obtain the other rq->lock.
5357 *
5358 * Silence PROVE_RCU
5359 */
5360 rcu_read_lock();
Ingo Molnardd41f592007-07-09 18:51:59 +02005361 __set_task_cpu(idle, cpu);
Peter Zijlstra6506cf6c2010-09-16 17:50:31 +02005362 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005363
Linus Torvalds1da177e2005-04-16 15:20:36 -07005364 rq->curr = rq->idle = idle;
Nick Piggin4866cde2005-06-25 14:57:23 -07005365#if defined(CONFIG_SMP) && defined(__ARCH_WANT_UNLOCKED_CTXSW)
5366 idle->oncpu = 1;
5367#endif
Thomas Gleixner05fa7852009-11-17 14:28:38 +01005368 raw_spin_unlock_irqrestore(&rq->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005369
5370 /* Set the preempt count _outside_ the spinlocks! */
Linus Torvalds8e3e0762008-05-10 20:58:02 -07005371#if defined(CONFIG_PREEMPT)
5372 task_thread_info(idle)->preempt_count = (idle->lock_depth >= 0);
5373#else
Al Viroa1261f52005-11-13 16:06:55 -08005374 task_thread_info(idle)->preempt_count = 0;
Linus Torvalds8e3e0762008-05-10 20:58:02 -07005375#endif
Ingo Molnardd41f592007-07-09 18:51:59 +02005376 /*
5377 * The idle tasks have their own, simple scheduling class:
5378 */
5379 idle->sched_class = &idle_sched_class;
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01005380 ftrace_graph_init_task(idle);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005381}
5382
5383/*
5384 * In a system that switches off the HZ timer nohz_cpu_mask
5385 * indicates which cpus entered this state. This is used
5386 * in the rcu update to wait only for active cpus. For system
5387 * which do not switch off the HZ timer nohz_cpu_mask should
Rusty Russell6a7b3dc2008-11-25 02:35:04 +10305388 * always be CPU_BITS_NONE.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005389 */
Rusty Russell6a7b3dc2008-11-25 02:35:04 +10305390cpumask_var_t nohz_cpu_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005391
Ingo Molnar19978ca2007-11-09 22:39:38 +01005392/*
5393 * Increase the granularity value when there are more CPUs,
5394 * because with more CPUs the 'effective latency' as visible
5395 * to users decreases. But the relationship is not linear,
5396 * so pick a second-best guess by going with the log2 of the
5397 * number of CPUs.
5398 *
5399 * This idea comes from the SD scheduler of Con Kolivas:
5400 */
Christian Ehrhardtacb4a842009-11-30 12:16:48 +01005401static int get_update_sysctl_factor(void)
Christian Ehrhardt0bcdcf22009-11-30 12:16:46 +01005402{
Mike Galbraith4ca3ef72009-12-10 09:25:53 +01005403 unsigned int cpus = min_t(int, num_online_cpus(), 8);
Christian Ehrhardt1983a922009-11-30 12:16:47 +01005404 unsigned int factor;
5405
5406 switch (sysctl_sched_tunable_scaling) {
5407 case SCHED_TUNABLESCALING_NONE:
5408 factor = 1;
5409 break;
5410 case SCHED_TUNABLESCALING_LINEAR:
5411 factor = cpus;
5412 break;
5413 case SCHED_TUNABLESCALING_LOG:
5414 default:
5415 factor = 1 + ilog2(cpus);
5416 break;
5417 }
Christian Ehrhardt0bcdcf22009-11-30 12:16:46 +01005418
Christian Ehrhardtacb4a842009-11-30 12:16:48 +01005419 return factor;
5420}
5421
5422static void update_sysctl(void)
5423{
5424 unsigned int factor = get_update_sysctl_factor();
5425
Christian Ehrhardt0bcdcf22009-11-30 12:16:46 +01005426#define SET_SYSCTL(name) \
5427 (sysctl_##name = (factor) * normalized_sysctl_##name)
5428 SET_SYSCTL(sched_min_granularity);
5429 SET_SYSCTL(sched_latency);
5430 SET_SYSCTL(sched_wakeup_granularity);
Christian Ehrhardt0bcdcf22009-11-30 12:16:46 +01005431#undef SET_SYSCTL
5432}
5433
Ingo Molnar19978ca2007-11-09 22:39:38 +01005434static inline void sched_init_granularity(void)
5435{
Christian Ehrhardt0bcdcf22009-11-30 12:16:46 +01005436 update_sysctl();
Ingo Molnar19978ca2007-11-09 22:39:38 +01005437}
5438
Linus Torvalds1da177e2005-04-16 15:20:36 -07005439#ifdef CONFIG_SMP
5440/*
5441 * This is how migration works:
5442 *
Tejun Heo969c7922010-05-06 18:49:21 +02005443 * 1) we invoke migration_cpu_stop() on the target CPU using
5444 * stop_one_cpu().
5445 * 2) stopper starts to run (implicitly forcing the migrated thread
5446 * off the CPU)
5447 * 3) it checks whether the migrated task is still in the wrong runqueue.
5448 * 4) if it's in the wrong runqueue then the migration thread removes
Linus Torvalds1da177e2005-04-16 15:20:36 -07005449 * it and puts it into the right queue.
Tejun Heo969c7922010-05-06 18:49:21 +02005450 * 5) stopper completes and stop_one_cpu() returns and the migration
5451 * is done.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005452 */
5453
5454/*
5455 * Change a given task's CPU affinity. Migrate the thread to a
5456 * proper CPU and schedule it away if the CPU it's executing on
5457 * is removed from the allowed bitmask.
5458 *
5459 * NOTE: the caller must have a valid reference to the task, the
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01005460 * task must not exit() & deallocate itself prematurely. The
Linus Torvalds1da177e2005-04-16 15:20:36 -07005461 * call is not atomic; no spinlocks may be held.
5462 */
Rusty Russell96f874e2008-11-25 02:35:14 +10305463int set_cpus_allowed_ptr(struct task_struct *p, const struct cpumask *new_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005464{
5465 unsigned long flags;
Ingo Molnar70b97a72006-07-03 00:25:42 -07005466 struct rq *rq;
Tejun Heo969c7922010-05-06 18:49:21 +02005467 unsigned int dest_cpu;
Ingo Molnar48f24c42006-07-03 00:25:40 -07005468 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005469
Peter Zijlstra65cc8e42010-03-25 21:05:16 +01005470 /*
5471 * Serialize against TASK_WAKING so that ttwu() and wunt() can
5472 * drop the rq->lock and still rely on ->cpus_allowed.
5473 */
5474again:
5475 while (task_is_waking(p))
5476 cpu_relax();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005477 rq = task_rq_lock(p, &flags);
Peter Zijlstra65cc8e42010-03-25 21:05:16 +01005478 if (task_is_waking(p)) {
5479 task_rq_unlock(rq, &flags);
5480 goto again;
5481 }
Peter Zijlstrae2912002009-12-16 18:04:36 +01005482
Peter Zijlstra6ad4c182009-11-25 13:31:39 +01005483 if (!cpumask_intersects(new_mask, cpu_active_mask)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005484 ret = -EINVAL;
5485 goto out;
5486 }
5487
David Rientjes9985b0b2008-06-05 12:57:11 -07005488 if (unlikely((p->flags & PF_THREAD_BOUND) && p != current &&
Rusty Russell96f874e2008-11-25 02:35:14 +10305489 !cpumask_equal(&p->cpus_allowed, new_mask))) {
David Rientjes9985b0b2008-06-05 12:57:11 -07005490 ret = -EINVAL;
5491 goto out;
5492 }
5493
Gregory Haskins73fe6aa2008-01-25 21:08:07 +01005494 if (p->sched_class->set_cpus_allowed)
Mike Traviscd8ba7c2008-03-26 14:23:49 -07005495 p->sched_class->set_cpus_allowed(p, new_mask);
Gregory Haskins73fe6aa2008-01-25 21:08:07 +01005496 else {
Rusty Russell96f874e2008-11-25 02:35:14 +10305497 cpumask_copy(&p->cpus_allowed, new_mask);
5498 p->rt.nr_cpus_allowed = cpumask_weight(new_mask);
Gregory Haskins73fe6aa2008-01-25 21:08:07 +01005499 }
5500
Linus Torvalds1da177e2005-04-16 15:20:36 -07005501 /* Can the task run on the task's current CPU? If so, we're done */
Rusty Russell96f874e2008-11-25 02:35:14 +10305502 if (cpumask_test_cpu(task_cpu(p), new_mask))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005503 goto out;
5504
Tejun Heo969c7922010-05-06 18:49:21 +02005505 dest_cpu = cpumask_any_and(cpu_active_mask, new_mask);
5506 if (migrate_task(p, dest_cpu)) {
5507 struct migration_arg arg = { p, dest_cpu };
Linus Torvalds1da177e2005-04-16 15:20:36 -07005508 /* Need help from migration thread: drop lock and wait. */
5509 task_rq_unlock(rq, &flags);
Tejun Heo969c7922010-05-06 18:49:21 +02005510 stop_one_cpu(cpu_of(rq), migration_cpu_stop, &arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005511 tlb_migrate_finish(p->mm);
5512 return 0;
5513 }
5514out:
5515 task_rq_unlock(rq, &flags);
Ingo Molnar48f24c42006-07-03 00:25:40 -07005516
Linus Torvalds1da177e2005-04-16 15:20:36 -07005517 return ret;
5518}
Mike Traviscd8ba7c2008-03-26 14:23:49 -07005519EXPORT_SYMBOL_GPL(set_cpus_allowed_ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005520
5521/*
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01005522 * Move (not current) task off this cpu, onto dest cpu. We're doing
Linus Torvalds1da177e2005-04-16 15:20:36 -07005523 * this because either it can't run here any more (set_cpus_allowed()
5524 * away from this CPU, or CPU going down), or because we're
5525 * attempting to rebalance this task on exec (sched_exec).
5526 *
5527 * So we race with normal scheduler movements, but that's OK, as long
5528 * as the task is no longer on this CPU.
Kirill Korotaevefc30812006-06-27 02:54:32 -07005529 *
5530 * Returns non-zero if task was successfully migrated.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005531 */
Kirill Korotaevefc30812006-06-27 02:54:32 -07005532static int __migrate_task(struct task_struct *p, int src_cpu, int dest_cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005533{
Ingo Molnar70b97a72006-07-03 00:25:42 -07005534 struct rq *rq_dest, *rq_src;
Peter Zijlstrae2912002009-12-16 18:04:36 +01005535 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005536
Max Krasnyanskye761b772008-07-15 04:43:49 -07005537 if (unlikely(!cpu_active(dest_cpu)))
Kirill Korotaevefc30812006-06-27 02:54:32 -07005538 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005539
5540 rq_src = cpu_rq(src_cpu);
5541 rq_dest = cpu_rq(dest_cpu);
5542
5543 double_rq_lock(rq_src, rq_dest);
5544 /* Already moved. */
5545 if (task_cpu(p) != src_cpu)
Linus Torvaldsb1e38732008-07-10 11:25:03 -07005546 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005547 /* Affinity changed (again). */
Rusty Russell96f874e2008-11-25 02:35:14 +10305548 if (!cpumask_test_cpu(dest_cpu, &p->cpus_allowed))
Linus Torvaldsb1e38732008-07-10 11:25:03 -07005549 goto fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005550
Peter Zijlstrae2912002009-12-16 18:04:36 +01005551 /*
5552 * If we're not on a rq, the next wake-up will ensure we're
5553 * placed properly.
5554 */
5555 if (p->se.on_rq) {
Ingo Molnar2e1cb742007-08-09 11:16:49 +02005556 deactivate_task(rq_src, p, 0);
Peter Zijlstrae2912002009-12-16 18:04:36 +01005557 set_task_cpu(p, dest_cpu);
Ingo Molnardd41f592007-07-09 18:51:59 +02005558 activate_task(rq_dest, p, 0);
Peter Zijlstra15afe092008-09-20 23:38:02 +02005559 check_preempt_curr(rq_dest, p, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005560 }
Linus Torvaldsb1e38732008-07-10 11:25:03 -07005561done:
Kirill Korotaevefc30812006-06-27 02:54:32 -07005562 ret = 1;
Linus Torvaldsb1e38732008-07-10 11:25:03 -07005563fail:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005564 double_rq_unlock(rq_src, rq_dest);
Kirill Korotaevefc30812006-06-27 02:54:32 -07005565 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005566}
5567
5568/*
Tejun Heo969c7922010-05-06 18:49:21 +02005569 * migration_cpu_stop - this will be executed by a highprio stopper thread
5570 * and performs thread migration by bumping thread off CPU then
5571 * 'pushing' onto another runqueue.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005572 */
Tejun Heo969c7922010-05-06 18:49:21 +02005573static int migration_cpu_stop(void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005574{
Tejun Heo969c7922010-05-06 18:49:21 +02005575 struct migration_arg *arg = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005576
Tejun Heo969c7922010-05-06 18:49:21 +02005577 /*
5578 * The original target cpu might have gone down and we might
5579 * be on another cpu but it doesn't matter.
5580 */
5581 local_irq_disable();
5582 __migrate_task(arg->task, raw_smp_processor_id(), arg->dest_cpu);
5583 local_irq_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005584 return 0;
5585}
5586
5587#ifdef CONFIG_HOTPLUG_CPU
Linus Torvalds1da177e2005-04-16 15:20:36 -07005588
Ingo Molnar48f24c42006-07-03 00:25:40 -07005589/*
5590 * Ensures that the idle task is using init_mm right before its cpu goes
Linus Torvalds1da177e2005-04-16 15:20:36 -07005591 * offline.
5592 */
5593void idle_task_exit(void)
5594{
5595 struct mm_struct *mm = current->active_mm;
5596
5597 BUG_ON(cpu_online(smp_processor_id()));
5598
5599 if (mm != &init_mm)
5600 switch_mm(mm, &init_mm, current);
5601 mmdrop(mm);
5602}
5603
Peter Zijlstra48c5cca2010-11-13 19:32:29 +01005604/*
5605 * While a dead CPU has no uninterruptible tasks queued at this point,
5606 * it might still have a nonzero ->nr_uninterruptible counter, because
5607 * for performance reasons the counter is not stricly tracking tasks to
5608 * their home CPUs. So we just add the counter to another CPU's counter,
5609 * to keep the global sum constant after CPU-down:
5610 */
5611static void migrate_nr_uninterruptible(struct rq *rq_src)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005612{
Peter Zijlstra48c5cca2010-11-13 19:32:29 +01005613 struct rq *rq_dest = cpu_rq(cpumask_any(cpu_active_mask));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005614
Peter Zijlstra48c5cca2010-11-13 19:32:29 +01005615 rq_dest->nr_uninterruptible += rq_src->nr_uninterruptible;
5616 rq_src->nr_uninterruptible = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005617}
Thomas Gleixnerdce48a82009-04-11 10:43:41 +02005618
5619/*
5620 * remove the tasks which were accounted by rq from calc_load_tasks.
5621 */
5622static void calc_global_load_remove(struct rq *rq)
5623{
5624 atomic_long_sub(rq->calc_load_active, &calc_load_tasks);
Thomas Gleixnera468d382009-07-17 14:15:46 +02005625 rq->calc_load_active = 0;
Thomas Gleixnerdce48a82009-04-11 10:43:41 +02005626}
Peter Zijlstra48c5cca2010-11-13 19:32:29 +01005627
5628/*
5629 * Migrate all tasks from the rq, sleeping tasks will be migrated by
5630 * try_to_wake_up()->select_task_rq().
5631 *
5632 * Called with rq->lock held even though we'er in stop_machine() and
5633 * there's no concurrency possible, we hold the required locks anyway
5634 * because of lock validation efforts.
5635 */
5636static void migrate_tasks(unsigned int dead_cpu)
5637{
5638 struct rq *rq = cpu_rq(dead_cpu);
5639 struct task_struct *next, *stop = rq->stop;
5640 int dest_cpu;
5641
5642 /*
5643 * Fudge the rq selection such that the below task selection loop
5644 * doesn't get stuck on the currently eligible stop task.
5645 *
5646 * We're currently inside stop_machine() and the rq is either stuck
5647 * in the stop_machine_cpu_stop() loop, or we're executing this code,
5648 * either way we should never end up calling schedule() until we're
5649 * done here.
5650 */
5651 rq->stop = NULL;
5652
5653 for ( ; ; ) {
5654 /*
5655 * There's this thread running, bail when that's the only
5656 * remaining thread.
5657 */
5658 if (rq->nr_running == 1)
5659 break;
5660
5661 next = pick_next_task(rq);
5662 BUG_ON(!next);
5663 next->sched_class->put_prev_task(rq, next);
5664
5665 /* Find suitable destination for @next, with force if needed. */
5666 dest_cpu = select_fallback_rq(dead_cpu, next);
5667 raw_spin_unlock(&rq->lock);
5668
5669 __migrate_task(next, dead_cpu, dest_cpu);
5670
5671 raw_spin_lock(&rq->lock);
5672 }
5673
5674 rq->stop = stop;
5675}
5676
Linus Torvalds1da177e2005-04-16 15:20:36 -07005677#endif /* CONFIG_HOTPLUG_CPU */
5678
Nick Piggine692ab52007-07-26 13:40:43 +02005679#if defined(CONFIG_SCHED_DEBUG) && defined(CONFIG_SYSCTL)
5680
5681static struct ctl_table sd_ctl_dir[] = {
Alexey Dobriyane0361852007-08-09 11:16:46 +02005682 {
5683 .procname = "sched_domain",
Eric W. Biedermanc57baf12007-08-23 15:18:02 +02005684 .mode = 0555,
Alexey Dobriyane0361852007-08-09 11:16:46 +02005685 },
Eric W. Biederman56992302009-11-05 15:38:40 -08005686 {}
Nick Piggine692ab52007-07-26 13:40:43 +02005687};
5688
5689static struct ctl_table sd_ctl_root[] = {
Alexey Dobriyane0361852007-08-09 11:16:46 +02005690 {
5691 .procname = "kernel",
Eric W. Biedermanc57baf12007-08-23 15:18:02 +02005692 .mode = 0555,
Alexey Dobriyane0361852007-08-09 11:16:46 +02005693 .child = sd_ctl_dir,
5694 },
Eric W. Biederman56992302009-11-05 15:38:40 -08005695 {}
Nick Piggine692ab52007-07-26 13:40:43 +02005696};
5697
5698static struct ctl_table *sd_alloc_ctl_entry(int n)
5699{
5700 struct ctl_table *entry =
Milton Miller5cf9f062007-10-15 17:00:19 +02005701 kcalloc(n, sizeof(struct ctl_table), GFP_KERNEL);
Nick Piggine692ab52007-07-26 13:40:43 +02005702
Nick Piggine692ab52007-07-26 13:40:43 +02005703 return entry;
5704}
5705
Milton Miller6382bc92007-10-15 17:00:19 +02005706static void sd_free_ctl_entry(struct ctl_table **tablep)
5707{
Milton Millercd790072007-10-17 16:55:11 +02005708 struct ctl_table *entry;
Milton Miller6382bc92007-10-15 17:00:19 +02005709
Milton Millercd790072007-10-17 16:55:11 +02005710 /*
5711 * In the intermediate directories, both the child directory and
5712 * procname are dynamically allocated and could fail but the mode
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01005713 * will always be set. In the lowest directory the names are
Milton Millercd790072007-10-17 16:55:11 +02005714 * static strings and all have proc handlers.
5715 */
5716 for (entry = *tablep; entry->mode; entry++) {
Milton Miller6382bc92007-10-15 17:00:19 +02005717 if (entry->child)
5718 sd_free_ctl_entry(&entry->child);
Milton Millercd790072007-10-17 16:55:11 +02005719 if (entry->proc_handler == NULL)
5720 kfree(entry->procname);
5721 }
Milton Miller6382bc92007-10-15 17:00:19 +02005722
5723 kfree(*tablep);
5724 *tablep = NULL;
5725}
5726
Nick Piggine692ab52007-07-26 13:40:43 +02005727static void
Alexey Dobriyane0361852007-08-09 11:16:46 +02005728set_table_entry(struct ctl_table *entry,
Nick Piggine692ab52007-07-26 13:40:43 +02005729 const char *procname, void *data, int maxlen,
5730 mode_t mode, proc_handler *proc_handler)
5731{
Nick Piggine692ab52007-07-26 13:40:43 +02005732 entry->procname = procname;
5733 entry->data = data;
5734 entry->maxlen = maxlen;
5735 entry->mode = mode;
5736 entry->proc_handler = proc_handler;
5737}
5738
5739static struct ctl_table *
5740sd_alloc_ctl_domain_table(struct sched_domain *sd)
5741{
Ingo Molnara5d8c342008-10-09 11:35:51 +02005742 struct ctl_table *table = sd_alloc_ctl_entry(13);
Nick Piggine692ab52007-07-26 13:40:43 +02005743
Milton Millerad1cdc12007-10-15 17:00:19 +02005744 if (table == NULL)
5745 return NULL;
5746
Alexey Dobriyane0361852007-08-09 11:16:46 +02005747 set_table_entry(&table[0], "min_interval", &sd->min_interval,
Nick Piggine692ab52007-07-26 13:40:43 +02005748 sizeof(long), 0644, proc_doulongvec_minmax);
Alexey Dobriyane0361852007-08-09 11:16:46 +02005749 set_table_entry(&table[1], "max_interval", &sd->max_interval,
Nick Piggine692ab52007-07-26 13:40:43 +02005750 sizeof(long), 0644, proc_doulongvec_minmax);
Alexey Dobriyane0361852007-08-09 11:16:46 +02005751 set_table_entry(&table[2], "busy_idx", &sd->busy_idx,
Nick Piggine692ab52007-07-26 13:40:43 +02005752 sizeof(int), 0644, proc_dointvec_minmax);
Alexey Dobriyane0361852007-08-09 11:16:46 +02005753 set_table_entry(&table[3], "idle_idx", &sd->idle_idx,
Nick Piggine692ab52007-07-26 13:40:43 +02005754 sizeof(int), 0644, proc_dointvec_minmax);
Alexey Dobriyane0361852007-08-09 11:16:46 +02005755 set_table_entry(&table[4], "newidle_idx", &sd->newidle_idx,
Nick Piggine692ab52007-07-26 13:40:43 +02005756 sizeof(int), 0644, proc_dointvec_minmax);
Alexey Dobriyane0361852007-08-09 11:16:46 +02005757 set_table_entry(&table[5], "wake_idx", &sd->wake_idx,
Nick Piggine692ab52007-07-26 13:40:43 +02005758 sizeof(int), 0644, proc_dointvec_minmax);
Alexey Dobriyane0361852007-08-09 11:16:46 +02005759 set_table_entry(&table[6], "forkexec_idx", &sd->forkexec_idx,
Nick Piggine692ab52007-07-26 13:40:43 +02005760 sizeof(int), 0644, proc_dointvec_minmax);
Alexey Dobriyane0361852007-08-09 11:16:46 +02005761 set_table_entry(&table[7], "busy_factor", &sd->busy_factor,
Nick Piggine692ab52007-07-26 13:40:43 +02005762 sizeof(int), 0644, proc_dointvec_minmax);
Alexey Dobriyane0361852007-08-09 11:16:46 +02005763 set_table_entry(&table[8], "imbalance_pct", &sd->imbalance_pct,
Nick Piggine692ab52007-07-26 13:40:43 +02005764 sizeof(int), 0644, proc_dointvec_minmax);
Zou Nan haiace8b3d2007-10-15 17:00:14 +02005765 set_table_entry(&table[9], "cache_nice_tries",
Nick Piggine692ab52007-07-26 13:40:43 +02005766 &sd->cache_nice_tries,
5767 sizeof(int), 0644, proc_dointvec_minmax);
Zou Nan haiace8b3d2007-10-15 17:00:14 +02005768 set_table_entry(&table[10], "flags", &sd->flags,
Nick Piggine692ab52007-07-26 13:40:43 +02005769 sizeof(int), 0644, proc_dointvec_minmax);
Ingo Molnara5d8c342008-10-09 11:35:51 +02005770 set_table_entry(&table[11], "name", sd->name,
5771 CORENAME_MAX_SIZE, 0444, proc_dostring);
5772 /* &table[12] is terminator */
Nick Piggine692ab52007-07-26 13:40:43 +02005773
5774 return table;
5775}
5776
Ingo Molnar9a4e7152007-11-28 15:52:56 +01005777static ctl_table *sd_alloc_ctl_cpu_table(int cpu)
Nick Piggine692ab52007-07-26 13:40:43 +02005778{
5779 struct ctl_table *entry, *table;
5780 struct sched_domain *sd;
5781 int domain_num = 0, i;
5782 char buf[32];
5783
5784 for_each_domain(cpu, sd)
5785 domain_num++;
5786 entry = table = sd_alloc_ctl_entry(domain_num + 1);
Milton Millerad1cdc12007-10-15 17:00:19 +02005787 if (table == NULL)
5788 return NULL;
Nick Piggine692ab52007-07-26 13:40:43 +02005789
5790 i = 0;
5791 for_each_domain(cpu, sd) {
5792 snprintf(buf, 32, "domain%d", i);
Nick Piggine692ab52007-07-26 13:40:43 +02005793 entry->procname = kstrdup(buf, GFP_KERNEL);
Eric W. Biedermanc57baf12007-08-23 15:18:02 +02005794 entry->mode = 0555;
Nick Piggine692ab52007-07-26 13:40:43 +02005795 entry->child = sd_alloc_ctl_domain_table(sd);
5796 entry++;
5797 i++;
5798 }
5799 return table;
5800}
5801
5802static struct ctl_table_header *sd_sysctl_header;
Milton Miller6382bc92007-10-15 17:00:19 +02005803static void register_sched_domain_sysctl(void)
Nick Piggine692ab52007-07-26 13:40:43 +02005804{
Peter Zijlstra6ad4c182009-11-25 13:31:39 +01005805 int i, cpu_num = num_possible_cpus();
Nick Piggine692ab52007-07-26 13:40:43 +02005806 struct ctl_table *entry = sd_alloc_ctl_entry(cpu_num + 1);
5807 char buf[32];
5808
Milton Miller73785472007-10-24 18:23:48 +02005809 WARN_ON(sd_ctl_dir[0].child);
5810 sd_ctl_dir[0].child = entry;
5811
Milton Millerad1cdc12007-10-15 17:00:19 +02005812 if (entry == NULL)
5813 return;
5814
Peter Zijlstra6ad4c182009-11-25 13:31:39 +01005815 for_each_possible_cpu(i) {
Nick Piggine692ab52007-07-26 13:40:43 +02005816 snprintf(buf, 32, "cpu%d", i);
Nick Piggine692ab52007-07-26 13:40:43 +02005817 entry->procname = kstrdup(buf, GFP_KERNEL);
Eric W. Biedermanc57baf12007-08-23 15:18:02 +02005818 entry->mode = 0555;
Nick Piggine692ab52007-07-26 13:40:43 +02005819 entry->child = sd_alloc_ctl_cpu_table(i);
Milton Miller97b6ea72007-10-15 17:00:19 +02005820 entry++;
Nick Piggine692ab52007-07-26 13:40:43 +02005821 }
Milton Miller73785472007-10-24 18:23:48 +02005822
5823 WARN_ON(sd_sysctl_header);
Nick Piggine692ab52007-07-26 13:40:43 +02005824 sd_sysctl_header = register_sysctl_table(sd_ctl_root);
5825}
Milton Miller6382bc92007-10-15 17:00:19 +02005826
Milton Miller73785472007-10-24 18:23:48 +02005827/* may be called multiple times per register */
Milton Miller6382bc92007-10-15 17:00:19 +02005828static void unregister_sched_domain_sysctl(void)
5829{
Milton Miller73785472007-10-24 18:23:48 +02005830 if (sd_sysctl_header)
5831 unregister_sysctl_table(sd_sysctl_header);
Milton Miller6382bc92007-10-15 17:00:19 +02005832 sd_sysctl_header = NULL;
Milton Miller73785472007-10-24 18:23:48 +02005833 if (sd_ctl_dir[0].child)
5834 sd_free_ctl_entry(&sd_ctl_dir[0].child);
Milton Miller6382bc92007-10-15 17:00:19 +02005835}
Nick Piggine692ab52007-07-26 13:40:43 +02005836#else
Milton Miller6382bc92007-10-15 17:00:19 +02005837static void register_sched_domain_sysctl(void)
5838{
5839}
5840static void unregister_sched_domain_sysctl(void)
Nick Piggine692ab52007-07-26 13:40:43 +02005841{
5842}
5843#endif
5844
Gregory Haskins1f11eb62008-06-04 15:04:05 -04005845static void set_rq_online(struct rq *rq)
5846{
5847 if (!rq->online) {
5848 const struct sched_class *class;
5849
Rusty Russellc6c49272008-11-25 02:35:05 +10305850 cpumask_set_cpu(rq->cpu, rq->rd->online);
Gregory Haskins1f11eb62008-06-04 15:04:05 -04005851 rq->online = 1;
5852
5853 for_each_class(class) {
5854 if (class->rq_online)
5855 class->rq_online(rq);
5856 }
5857 }
5858}
5859
5860static void set_rq_offline(struct rq *rq)
5861{
5862 if (rq->online) {
5863 const struct sched_class *class;
5864
5865 for_each_class(class) {
5866 if (class->rq_offline)
5867 class->rq_offline(rq);
5868 }
5869
Rusty Russellc6c49272008-11-25 02:35:05 +10305870 cpumask_clear_cpu(rq->cpu, rq->rd->online);
Gregory Haskins1f11eb62008-06-04 15:04:05 -04005871 rq->online = 0;
5872 }
5873}
5874
Linus Torvalds1da177e2005-04-16 15:20:36 -07005875/*
5876 * migration_call - callback that gets triggered when a CPU is added.
5877 * Here we can start up the necessary migration thread for the new CPU.
5878 */
Ingo Molnar48f24c42006-07-03 00:25:40 -07005879static int __cpuinit
5880migration_call(struct notifier_block *nfb, unsigned long action, void *hcpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005881{
Ingo Molnar48f24c42006-07-03 00:25:40 -07005882 int cpu = (long)hcpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005883 unsigned long flags;
Tejun Heo969c7922010-05-06 18:49:21 +02005884 struct rq *rq = cpu_rq(cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005885
Peter Zijlstra48c5cca2010-11-13 19:32:29 +01005886 switch (action & ~CPU_TASKS_FROZEN) {
Gautham R Shenoy5be93612007-05-09 02:34:04 -07005887
Linus Torvalds1da177e2005-04-16 15:20:36 -07005888 case CPU_UP_PREPARE:
Thomas Gleixnera468d382009-07-17 14:15:46 +02005889 rq->calc_load_update = calc_load_update;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005890 break;
Ingo Molnar48f24c42006-07-03 00:25:40 -07005891
Linus Torvalds1da177e2005-04-16 15:20:36 -07005892 case CPU_ONLINE:
Gregory Haskins1f94ef52008-03-10 16:52:41 -04005893 /* Update our root-domain */
Thomas Gleixner05fa7852009-11-17 14:28:38 +01005894 raw_spin_lock_irqsave(&rq->lock, flags);
Gregory Haskins1f94ef52008-03-10 16:52:41 -04005895 if (rq->rd) {
Rusty Russellc6c49272008-11-25 02:35:05 +10305896 BUG_ON(!cpumask_test_cpu(cpu, rq->rd->span));
Gregory Haskins1f11eb62008-06-04 15:04:05 -04005897
5898 set_rq_online(rq);
Gregory Haskins1f94ef52008-03-10 16:52:41 -04005899 }
Thomas Gleixner05fa7852009-11-17 14:28:38 +01005900 raw_spin_unlock_irqrestore(&rq->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005901 break;
Ingo Molnar48f24c42006-07-03 00:25:40 -07005902
Linus Torvalds1da177e2005-04-16 15:20:36 -07005903#ifdef CONFIG_HOTPLUG_CPU
Gregory Haskins08f503b2008-03-10 17:59:11 -04005904 case CPU_DYING:
Gregory Haskins57d885f2008-01-25 21:08:18 +01005905 /* Update our root-domain */
Thomas Gleixner05fa7852009-11-17 14:28:38 +01005906 raw_spin_lock_irqsave(&rq->lock, flags);
Gregory Haskins57d885f2008-01-25 21:08:18 +01005907 if (rq->rd) {
Rusty Russellc6c49272008-11-25 02:35:05 +10305908 BUG_ON(!cpumask_test_cpu(cpu, rq->rd->span));
Gregory Haskins1f11eb62008-06-04 15:04:05 -04005909 set_rq_offline(rq);
Gregory Haskins57d885f2008-01-25 21:08:18 +01005910 }
Peter Zijlstra48c5cca2010-11-13 19:32:29 +01005911 migrate_tasks(cpu);
5912 BUG_ON(rq->nr_running != 1); /* the migration thread */
Thomas Gleixner05fa7852009-11-17 14:28:38 +01005913 raw_spin_unlock_irqrestore(&rq->lock, flags);
Peter Zijlstra48c5cca2010-11-13 19:32:29 +01005914
5915 migrate_nr_uninterruptible(rq);
5916 calc_global_load_remove(rq);
Gregory Haskins57d885f2008-01-25 21:08:18 +01005917 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005918#endif
5919 }
5920 return NOTIFY_OK;
5921}
5922
Paul Mackerrasf38b0822009-06-02 21:05:16 +10005923/*
5924 * Register at high priority so that task migration (migrate_all_tasks)
5925 * happens before everything else. This has to be lower priority than
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005926 * the notifier in the perf_event subsystem, though.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005927 */
Chandra Seetharaman26c21432006-06-27 02:54:10 -07005928static struct notifier_block __cpuinitdata migration_notifier = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005929 .notifier_call = migration_call,
Tejun Heo50a323b2010-06-08 21:40:36 +02005930 .priority = CPU_PRI_MIGRATION,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005931};
5932
Tejun Heo3a101d02010-06-08 21:40:36 +02005933static int __cpuinit sched_cpu_active(struct notifier_block *nfb,
5934 unsigned long action, void *hcpu)
5935{
5936 switch (action & ~CPU_TASKS_FROZEN) {
5937 case CPU_ONLINE:
5938 case CPU_DOWN_FAILED:
5939 set_cpu_active((long)hcpu, true);
5940 return NOTIFY_OK;
5941 default:
5942 return NOTIFY_DONE;
5943 }
5944}
5945
5946static int __cpuinit sched_cpu_inactive(struct notifier_block *nfb,
5947 unsigned long action, void *hcpu)
5948{
5949 switch (action & ~CPU_TASKS_FROZEN) {
5950 case CPU_DOWN_PREPARE:
5951 set_cpu_active((long)hcpu, false);
5952 return NOTIFY_OK;
5953 default:
5954 return NOTIFY_DONE;
5955 }
5956}
5957
Eduard - Gabriel Munteanu7babe8d2008-07-25 19:45:11 -07005958static int __init migration_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005959{
5960 void *cpu = (void *)(long)smp_processor_id();
Akinobu Mita07dccf32006-09-29 02:00:22 -07005961 int err;
Ingo Molnar48f24c42006-07-03 00:25:40 -07005962
Tejun Heo3a101d02010-06-08 21:40:36 +02005963 /* Initialize migration for the boot CPU */
Akinobu Mita07dccf32006-09-29 02:00:22 -07005964 err = migration_call(&migration_notifier, CPU_UP_PREPARE, cpu);
5965 BUG_ON(err == NOTIFY_BAD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005966 migration_call(&migration_notifier, CPU_ONLINE, cpu);
5967 register_cpu_notifier(&migration_notifier);
Eduard - Gabriel Munteanu7babe8d2008-07-25 19:45:11 -07005968
Tejun Heo3a101d02010-06-08 21:40:36 +02005969 /* Register cpu active notifiers */
5970 cpu_notifier(sched_cpu_active, CPU_PRI_SCHED_ACTIVE);
5971 cpu_notifier(sched_cpu_inactive, CPU_PRI_SCHED_INACTIVE);
5972
Thomas Gleixnera004cd42009-07-21 09:54:05 +02005973 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005974}
Eduard - Gabriel Munteanu7babe8d2008-07-25 19:45:11 -07005975early_initcall(migration_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005976#endif
5977
5978#ifdef CONFIG_SMP
Christoph Lameter476f3532007-05-06 14:48:58 -07005979
Ingo Molnar3e9830d2007-10-15 17:00:13 +02005980#ifdef CONFIG_SCHED_DEBUG
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02005981
Mike Travisf6630112009-11-17 18:22:15 -06005982static __read_mostly int sched_domain_debug_enabled;
5983
5984static int __init sched_domain_debug_setup(char *str)
5985{
5986 sched_domain_debug_enabled = 1;
5987
5988 return 0;
5989}
5990early_param("sched_debug", sched_domain_debug_setup);
5991
Mike Travis7c16ec52008-04-04 18:11:11 -07005992static int sched_domain_debug_one(struct sched_domain *sd, int cpu, int level,
Rusty Russell96f874e2008-11-25 02:35:14 +10305993 struct cpumask *groupmask)
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02005994{
5995 struct sched_group *group = sd->groups;
Mike Travis434d53b2008-04-04 18:11:04 -07005996 char str[256];
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02005997
Rusty Russell968ea6d2008-12-13 21:55:51 +10305998 cpulist_scnprintf(str, sizeof(str), sched_domain_span(sd));
Rusty Russell96f874e2008-11-25 02:35:14 +10305999 cpumask_clear(groupmask);
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02006000
6001 printk(KERN_DEBUG "%*s domain %d: ", level, "", level);
6002
6003 if (!(sd->flags & SD_LOAD_BALANCE)) {
Peter Zijlstra3df0fc52009-12-20 14:23:57 +01006004 printk("does not load-balance\n");
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02006005 if (sd->parent)
Peter Zijlstra3df0fc52009-12-20 14:23:57 +01006006 printk(KERN_ERR "ERROR: !SD_LOAD_BALANCE domain"
6007 " has parent");
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02006008 return -1;
6009 }
6010
Peter Zijlstra3df0fc52009-12-20 14:23:57 +01006011 printk(KERN_CONT "span %s level %s\n", str, sd->name);
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02006012
Rusty Russell758b2cd2008-11-25 02:35:04 +10306013 if (!cpumask_test_cpu(cpu, sched_domain_span(sd))) {
Peter Zijlstra3df0fc52009-12-20 14:23:57 +01006014 printk(KERN_ERR "ERROR: domain->span does not contain "
6015 "CPU%d\n", cpu);
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02006016 }
Rusty Russell758b2cd2008-11-25 02:35:04 +10306017 if (!cpumask_test_cpu(cpu, sched_group_cpus(group))) {
Peter Zijlstra3df0fc52009-12-20 14:23:57 +01006018 printk(KERN_ERR "ERROR: domain->groups does not contain"
6019 " CPU%d\n", cpu);
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02006020 }
6021
6022 printk(KERN_DEBUG "%*s groups:", level + 1, "");
6023 do {
6024 if (!group) {
Peter Zijlstra3df0fc52009-12-20 14:23:57 +01006025 printk("\n");
6026 printk(KERN_ERR "ERROR: group is NULL\n");
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02006027 break;
6028 }
6029
Peter Zijlstra18a38852009-09-01 10:34:39 +02006030 if (!group->cpu_power) {
Peter Zijlstra3df0fc52009-12-20 14:23:57 +01006031 printk(KERN_CONT "\n");
6032 printk(KERN_ERR "ERROR: domain->cpu_power not "
6033 "set\n");
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02006034 break;
6035 }
6036
Rusty Russell758b2cd2008-11-25 02:35:04 +10306037 if (!cpumask_weight(sched_group_cpus(group))) {
Peter Zijlstra3df0fc52009-12-20 14:23:57 +01006038 printk(KERN_CONT "\n");
6039 printk(KERN_ERR "ERROR: empty group\n");
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02006040 break;
6041 }
6042
Rusty Russell758b2cd2008-11-25 02:35:04 +10306043 if (cpumask_intersects(groupmask, sched_group_cpus(group))) {
Peter Zijlstra3df0fc52009-12-20 14:23:57 +01006044 printk(KERN_CONT "\n");
6045 printk(KERN_ERR "ERROR: repeated CPUs\n");
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02006046 break;
6047 }
6048
Rusty Russell758b2cd2008-11-25 02:35:04 +10306049 cpumask_or(groupmask, groupmask, sched_group_cpus(group));
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02006050
Rusty Russell968ea6d2008-12-13 21:55:51 +10306051 cpulist_scnprintf(str, sizeof(str), sched_group_cpus(group));
Gautham R Shenoy381512c2009-04-14 09:09:36 +05306052
Peter Zijlstra3df0fc52009-12-20 14:23:57 +01006053 printk(KERN_CONT " %s", str);
Peter Zijlstra18a38852009-09-01 10:34:39 +02006054 if (group->cpu_power != SCHED_LOAD_SCALE) {
Peter Zijlstra3df0fc52009-12-20 14:23:57 +01006055 printk(KERN_CONT " (cpu_power = %d)",
6056 group->cpu_power);
Gautham R Shenoy381512c2009-04-14 09:09:36 +05306057 }
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02006058
6059 group = group->next;
6060 } while (group != sd->groups);
Peter Zijlstra3df0fc52009-12-20 14:23:57 +01006061 printk(KERN_CONT "\n");
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02006062
Rusty Russell758b2cd2008-11-25 02:35:04 +10306063 if (!cpumask_equal(sched_domain_span(sd), groupmask))
Peter Zijlstra3df0fc52009-12-20 14:23:57 +01006064 printk(KERN_ERR "ERROR: groups don't span domain->span\n");
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02006065
Rusty Russell758b2cd2008-11-25 02:35:04 +10306066 if (sd->parent &&
6067 !cpumask_subset(groupmask, sched_domain_span(sd->parent)))
Peter Zijlstra3df0fc52009-12-20 14:23:57 +01006068 printk(KERN_ERR "ERROR: parent span is not a superset "
6069 "of domain->span\n");
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02006070 return 0;
6071}
6072
Linus Torvalds1da177e2005-04-16 15:20:36 -07006073static void sched_domain_debug(struct sched_domain *sd, int cpu)
6074{
Rusty Russelld5dd3db2008-11-25 02:35:12 +10306075 cpumask_var_t groupmask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006076 int level = 0;
6077
Mike Travisf6630112009-11-17 18:22:15 -06006078 if (!sched_domain_debug_enabled)
6079 return;
6080
Nick Piggin41c7ce92005-06-25 14:57:24 -07006081 if (!sd) {
6082 printk(KERN_DEBUG "CPU%d attaching NULL sched-domain.\n", cpu);
6083 return;
6084 }
6085
Linus Torvalds1da177e2005-04-16 15:20:36 -07006086 printk(KERN_DEBUG "CPU%d attaching sched-domain:\n", cpu);
6087
Rusty Russelld5dd3db2008-11-25 02:35:12 +10306088 if (!alloc_cpumask_var(&groupmask, GFP_KERNEL)) {
Mike Travis7c16ec52008-04-04 18:11:11 -07006089 printk(KERN_DEBUG "Cannot load-balance (out of memory)\n");
6090 return;
6091 }
6092
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02006093 for (;;) {
Mike Travis7c16ec52008-04-04 18:11:11 -07006094 if (sched_domain_debug_one(sd, cpu, level, groupmask))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006095 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006096 level++;
6097 sd = sd->parent;
Miguel Ojeda Sandonis33859f72006-12-10 02:20:38 -08006098 if (!sd)
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02006099 break;
6100 }
Rusty Russelld5dd3db2008-11-25 02:35:12 +10306101 free_cpumask_var(groupmask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006102}
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +02006103#else /* !CONFIG_SCHED_DEBUG */
Ingo Molnar48f24c42006-07-03 00:25:40 -07006104# define sched_domain_debug(sd, cpu) do { } while (0)
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +02006105#endif /* CONFIG_SCHED_DEBUG */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006106
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07006107static int sd_degenerate(struct sched_domain *sd)
Suresh Siddha245af2c2005-06-25 14:57:25 -07006108{
Rusty Russell758b2cd2008-11-25 02:35:04 +10306109 if (cpumask_weight(sched_domain_span(sd)) == 1)
Suresh Siddha245af2c2005-06-25 14:57:25 -07006110 return 1;
6111
6112 /* Following flags need at least 2 groups */
6113 if (sd->flags & (SD_LOAD_BALANCE |
6114 SD_BALANCE_NEWIDLE |
6115 SD_BALANCE_FORK |
Siddha, Suresh B89c47102006-10-03 01:14:09 -07006116 SD_BALANCE_EXEC |
6117 SD_SHARE_CPUPOWER |
6118 SD_SHARE_PKG_RESOURCES)) {
Suresh Siddha245af2c2005-06-25 14:57:25 -07006119 if (sd->groups != sd->groups->next)
6120 return 0;
6121 }
6122
6123 /* Following flags don't use groups */
Peter Zijlstrac88d5912009-09-10 13:50:02 +02006124 if (sd->flags & (SD_WAKE_AFFINE))
Suresh Siddha245af2c2005-06-25 14:57:25 -07006125 return 0;
6126
6127 return 1;
6128}
6129
Ingo Molnar48f24c42006-07-03 00:25:40 -07006130static int
6131sd_parent_degenerate(struct sched_domain *sd, struct sched_domain *parent)
Suresh Siddha245af2c2005-06-25 14:57:25 -07006132{
6133 unsigned long cflags = sd->flags, pflags = parent->flags;
6134
6135 if (sd_degenerate(parent))
6136 return 1;
6137
Rusty Russell758b2cd2008-11-25 02:35:04 +10306138 if (!cpumask_equal(sched_domain_span(sd), sched_domain_span(parent)))
Suresh Siddha245af2c2005-06-25 14:57:25 -07006139 return 0;
6140
Suresh Siddha245af2c2005-06-25 14:57:25 -07006141 /* Flags needing groups don't count if only 1 group in parent */
6142 if (parent->groups == parent->groups->next) {
6143 pflags &= ~(SD_LOAD_BALANCE |
6144 SD_BALANCE_NEWIDLE |
6145 SD_BALANCE_FORK |
Siddha, Suresh B89c47102006-10-03 01:14:09 -07006146 SD_BALANCE_EXEC |
6147 SD_SHARE_CPUPOWER |
6148 SD_SHARE_PKG_RESOURCES);
Ken Chen54364992008-12-07 18:47:37 -08006149 if (nr_node_ids == 1)
6150 pflags &= ~SD_SERIALIZE;
Suresh Siddha245af2c2005-06-25 14:57:25 -07006151 }
6152 if (~cflags & pflags)
6153 return 0;
6154
6155 return 1;
6156}
6157
Rusty Russellc6c49272008-11-25 02:35:05 +10306158static void free_rootdomain(struct root_domain *rd)
6159{
Peter Zijlstra047106a2009-11-16 10:28:09 +01006160 synchronize_sched();
6161
Rusty Russell68e74562008-11-25 02:35:13 +10306162 cpupri_cleanup(&rd->cpupri);
6163
Rusty Russellc6c49272008-11-25 02:35:05 +10306164 free_cpumask_var(rd->rto_mask);
6165 free_cpumask_var(rd->online);
6166 free_cpumask_var(rd->span);
6167 kfree(rd);
6168}
6169
Gregory Haskins57d885f2008-01-25 21:08:18 +01006170static void rq_attach_root(struct rq *rq, struct root_domain *rd)
6171{
Ingo Molnara0490fa2009-02-12 11:35:40 +01006172 struct root_domain *old_rd = NULL;
Gregory Haskins57d885f2008-01-25 21:08:18 +01006173 unsigned long flags;
Gregory Haskins57d885f2008-01-25 21:08:18 +01006174
Thomas Gleixner05fa7852009-11-17 14:28:38 +01006175 raw_spin_lock_irqsave(&rq->lock, flags);
Gregory Haskins57d885f2008-01-25 21:08:18 +01006176
6177 if (rq->rd) {
Ingo Molnara0490fa2009-02-12 11:35:40 +01006178 old_rd = rq->rd;
Gregory Haskins57d885f2008-01-25 21:08:18 +01006179
Rusty Russellc6c49272008-11-25 02:35:05 +10306180 if (cpumask_test_cpu(rq->cpu, old_rd->online))
Gregory Haskins1f11eb62008-06-04 15:04:05 -04006181 set_rq_offline(rq);
Gregory Haskins57d885f2008-01-25 21:08:18 +01006182
Rusty Russellc6c49272008-11-25 02:35:05 +10306183 cpumask_clear_cpu(rq->cpu, old_rd->span);
Gregory Haskinsdc938522008-01-25 21:08:26 +01006184
Ingo Molnara0490fa2009-02-12 11:35:40 +01006185 /*
6186 * If we dont want to free the old_rt yet then
6187 * set old_rd to NULL to skip the freeing later
6188 * in this function:
6189 */
6190 if (!atomic_dec_and_test(&old_rd->refcount))
6191 old_rd = NULL;
Gregory Haskins57d885f2008-01-25 21:08:18 +01006192 }
6193
6194 atomic_inc(&rd->refcount);
6195 rq->rd = rd;
6196
Rusty Russellc6c49272008-11-25 02:35:05 +10306197 cpumask_set_cpu(rq->cpu, rd->span);
Gregory Haskins00aec932009-07-30 10:57:23 -04006198 if (cpumask_test_cpu(rq->cpu, cpu_active_mask))
Gregory Haskins1f11eb62008-06-04 15:04:05 -04006199 set_rq_online(rq);
Gregory Haskins57d885f2008-01-25 21:08:18 +01006200
Thomas Gleixner05fa7852009-11-17 14:28:38 +01006201 raw_spin_unlock_irqrestore(&rq->lock, flags);
Ingo Molnara0490fa2009-02-12 11:35:40 +01006202
6203 if (old_rd)
6204 free_rootdomain(old_rd);
Gregory Haskins57d885f2008-01-25 21:08:18 +01006205}
6206
Pekka Enberg68c38fc2010-07-15 23:18:22 +03006207static int init_rootdomain(struct root_domain *rd)
Gregory Haskins57d885f2008-01-25 21:08:18 +01006208{
6209 memset(rd, 0, sizeof(*rd));
6210
Pekka Enberg68c38fc2010-07-15 23:18:22 +03006211 if (!alloc_cpumask_var(&rd->span, GFP_KERNEL))
Li Zefan0c910d22009-01-06 17:39:06 +08006212 goto out;
Pekka Enberg68c38fc2010-07-15 23:18:22 +03006213 if (!alloc_cpumask_var(&rd->online, GFP_KERNEL))
Rusty Russellc6c49272008-11-25 02:35:05 +10306214 goto free_span;
Pekka Enberg68c38fc2010-07-15 23:18:22 +03006215 if (!alloc_cpumask_var(&rd->rto_mask, GFP_KERNEL))
Rusty Russellc6c49272008-11-25 02:35:05 +10306216 goto free_online;
Gregory Haskins6e0534f2008-05-12 21:21:01 +02006217
Pekka Enberg68c38fc2010-07-15 23:18:22 +03006218 if (cpupri_init(&rd->cpupri) != 0)
Rusty Russell68e74562008-11-25 02:35:13 +10306219 goto free_rto_mask;
Rusty Russellc6c49272008-11-25 02:35:05 +10306220 return 0;
6221
Rusty Russell68e74562008-11-25 02:35:13 +10306222free_rto_mask:
6223 free_cpumask_var(rd->rto_mask);
Rusty Russellc6c49272008-11-25 02:35:05 +10306224free_online:
6225 free_cpumask_var(rd->online);
6226free_span:
6227 free_cpumask_var(rd->span);
Li Zefan0c910d22009-01-06 17:39:06 +08006228out:
Rusty Russellc6c49272008-11-25 02:35:05 +10306229 return -ENOMEM;
Gregory Haskins57d885f2008-01-25 21:08:18 +01006230}
6231
6232static void init_defrootdomain(void)
6233{
Pekka Enberg68c38fc2010-07-15 23:18:22 +03006234 init_rootdomain(&def_root_domain);
Rusty Russellc6c49272008-11-25 02:35:05 +10306235
Gregory Haskins57d885f2008-01-25 21:08:18 +01006236 atomic_set(&def_root_domain.refcount, 1);
6237}
6238
Gregory Haskinsdc938522008-01-25 21:08:26 +01006239static struct root_domain *alloc_rootdomain(void)
Gregory Haskins57d885f2008-01-25 21:08:18 +01006240{
6241 struct root_domain *rd;
6242
6243 rd = kmalloc(sizeof(*rd), GFP_KERNEL);
6244 if (!rd)
6245 return NULL;
6246
Pekka Enberg68c38fc2010-07-15 23:18:22 +03006247 if (init_rootdomain(rd) != 0) {
Rusty Russellc6c49272008-11-25 02:35:05 +10306248 kfree(rd);
6249 return NULL;
6250 }
Gregory Haskins57d885f2008-01-25 21:08:18 +01006251
6252 return rd;
6253}
6254
Linus Torvalds1da177e2005-04-16 15:20:36 -07006255/*
Ingo Molnar0eab9142008-01-25 21:08:19 +01006256 * Attach the domain 'sd' to 'cpu' as its base domain. Callers must
Linus Torvalds1da177e2005-04-16 15:20:36 -07006257 * hold the hotplug lock.
6258 */
Ingo Molnar0eab9142008-01-25 21:08:19 +01006259static void
6260cpu_attach_domain(struct sched_domain *sd, struct root_domain *rd, int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006261{
Ingo Molnar70b97a72006-07-03 00:25:42 -07006262 struct rq *rq = cpu_rq(cpu);
Suresh Siddha245af2c2005-06-25 14:57:25 -07006263 struct sched_domain *tmp;
6264
Peter Zijlstra669c55e2010-04-16 14:59:29 +02006265 for (tmp = sd; tmp; tmp = tmp->parent)
6266 tmp->span_weight = cpumask_weight(sched_domain_span(tmp));
6267
Suresh Siddha245af2c2005-06-25 14:57:25 -07006268 /* Remove the sched domains which do not contribute to scheduling. */
Li Zefanf29c9b12008-11-06 09:45:16 +08006269 for (tmp = sd; tmp; ) {
Suresh Siddha245af2c2005-06-25 14:57:25 -07006270 struct sched_domain *parent = tmp->parent;
6271 if (!parent)
6272 break;
Li Zefanf29c9b12008-11-06 09:45:16 +08006273
Siddha, Suresh B1a848872006-10-03 01:14:08 -07006274 if (sd_parent_degenerate(tmp, parent)) {
Suresh Siddha245af2c2005-06-25 14:57:25 -07006275 tmp->parent = parent->parent;
Siddha, Suresh B1a848872006-10-03 01:14:08 -07006276 if (parent->parent)
6277 parent->parent->child = tmp;
Li Zefanf29c9b12008-11-06 09:45:16 +08006278 } else
6279 tmp = tmp->parent;
Suresh Siddha245af2c2005-06-25 14:57:25 -07006280 }
6281
Siddha, Suresh B1a848872006-10-03 01:14:08 -07006282 if (sd && sd_degenerate(sd)) {
Suresh Siddha245af2c2005-06-25 14:57:25 -07006283 sd = sd->parent;
Siddha, Suresh B1a848872006-10-03 01:14:08 -07006284 if (sd)
6285 sd->child = NULL;
6286 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006287
6288 sched_domain_debug(sd, cpu);
6289
Gregory Haskins57d885f2008-01-25 21:08:18 +01006290 rq_attach_root(rq, rd);
Nick Piggin674311d2005-06-25 14:57:27 -07006291 rcu_assign_pointer(rq->sd, sd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006292}
6293
6294/* cpus with isolated domains */
Rusty Russelldcc30a32008-11-25 02:35:12 +10306295static cpumask_var_t cpu_isolated_map;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006296
6297/* Setup the mask of cpus configured for isolated domains */
6298static int __init isolated_cpu_setup(char *str)
6299{
Rusty Russellbdddd292009-12-02 14:09:16 +10306300 alloc_bootmem_cpumask_var(&cpu_isolated_map);
Rusty Russell968ea6d2008-12-13 21:55:51 +10306301 cpulist_parse(str, cpu_isolated_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006302 return 1;
6303}
6304
Ingo Molnar8927f492007-10-15 17:00:13 +02006305__setup("isolcpus=", isolated_cpu_setup);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006306
6307/*
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08006308 * init_sched_build_groups takes the cpumask we wish to span, and a pointer
6309 * to a function which identifies what group(along with sched group) a CPU
Rusty Russell96f874e2008-11-25 02:35:14 +10306310 * belongs to. The return value of group_fn must be a >= 0 and < nr_cpu_ids
6311 * (due to the fact that we keep track of groups covered with a struct cpumask).
Linus Torvalds1da177e2005-04-16 15:20:36 -07006312 *
6313 * init_sched_build_groups will build a circular linked list of the groups
6314 * covered by the given span, and will set each group's ->cpumask correctly,
6315 * and ->cpu_power to 0.
6316 */
Siddha, Suresh Ba6160582006-10-03 01:14:06 -07006317static void
Rusty Russell96f874e2008-11-25 02:35:14 +10306318init_sched_build_groups(const struct cpumask *span,
6319 const struct cpumask *cpu_map,
6320 int (*group_fn)(int cpu, const struct cpumask *cpu_map,
Mike Travis7c16ec52008-04-04 18:11:11 -07006321 struct sched_group **sg,
Rusty Russell96f874e2008-11-25 02:35:14 +10306322 struct cpumask *tmpmask),
6323 struct cpumask *covered, struct cpumask *tmpmask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006324{
6325 struct sched_group *first = NULL, *last = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006326 int i;
6327
Rusty Russell96f874e2008-11-25 02:35:14 +10306328 cpumask_clear(covered);
Mike Travis7c16ec52008-04-04 18:11:11 -07006329
Rusty Russellabcd0832008-11-25 02:35:02 +10306330 for_each_cpu(i, span) {
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08006331 struct sched_group *sg;
Mike Travis7c16ec52008-04-04 18:11:11 -07006332 int group = group_fn(i, cpu_map, &sg, tmpmask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006333 int j;
6334
Rusty Russell758b2cd2008-11-25 02:35:04 +10306335 if (cpumask_test_cpu(i, covered))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006336 continue;
6337
Rusty Russell758b2cd2008-11-25 02:35:04 +10306338 cpumask_clear(sched_group_cpus(sg));
Peter Zijlstra18a38852009-09-01 10:34:39 +02006339 sg->cpu_power = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006340
Rusty Russellabcd0832008-11-25 02:35:02 +10306341 for_each_cpu(j, span) {
Mike Travis7c16ec52008-04-04 18:11:11 -07006342 if (group_fn(j, cpu_map, NULL, tmpmask) != group)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006343 continue;
6344
Rusty Russell96f874e2008-11-25 02:35:14 +10306345 cpumask_set_cpu(j, covered);
Rusty Russell758b2cd2008-11-25 02:35:04 +10306346 cpumask_set_cpu(j, sched_group_cpus(sg));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006347 }
6348 if (!first)
6349 first = sg;
6350 if (last)
6351 last->next = sg;
6352 last = sg;
6353 }
6354 last->next = first;
6355}
6356
John Hawkes9c1cfda2005-09-06 15:18:14 -07006357#define SD_NODES_PER_DOMAIN 16
Linus Torvalds1da177e2005-04-16 15:20:36 -07006358
John Hawkes9c1cfda2005-09-06 15:18:14 -07006359#ifdef CONFIG_NUMA
akpm@osdl.org198e2f12006-01-12 01:05:30 -08006360
John Hawkes9c1cfda2005-09-06 15:18:14 -07006361/**
6362 * find_next_best_node - find the next node to include in a sched_domain
6363 * @node: node whose sched_domain we're building
6364 * @used_nodes: nodes already in the sched_domain
6365 *
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01006366 * Find the next node to include in a given scheduling domain. Simply
John Hawkes9c1cfda2005-09-06 15:18:14 -07006367 * finds the closest node not already in the @used_nodes map.
6368 *
6369 * Should use nodemask_t.
6370 */
Mike Travisc5f59f02008-04-04 18:11:10 -07006371static int find_next_best_node(int node, nodemask_t *used_nodes)
John Hawkes9c1cfda2005-09-06 15:18:14 -07006372{
6373 int i, n, val, min_val, best_node = 0;
6374
6375 min_val = INT_MAX;
6376
Mike Travis076ac2a2008-05-12 21:21:12 +02006377 for (i = 0; i < nr_node_ids; i++) {
John Hawkes9c1cfda2005-09-06 15:18:14 -07006378 /* Start at @node */
Mike Travis076ac2a2008-05-12 21:21:12 +02006379 n = (node + i) % nr_node_ids;
John Hawkes9c1cfda2005-09-06 15:18:14 -07006380
6381 if (!nr_cpus_node(n))
6382 continue;
6383
6384 /* Skip already used nodes */
Mike Travisc5f59f02008-04-04 18:11:10 -07006385 if (node_isset(n, *used_nodes))
John Hawkes9c1cfda2005-09-06 15:18:14 -07006386 continue;
6387
6388 /* Simple min distance search */
6389 val = node_distance(node, n);
6390
6391 if (val < min_val) {
6392 min_val = val;
6393 best_node = n;
6394 }
6395 }
6396
Mike Travisc5f59f02008-04-04 18:11:10 -07006397 node_set(best_node, *used_nodes);
John Hawkes9c1cfda2005-09-06 15:18:14 -07006398 return best_node;
6399}
6400
6401/**
6402 * sched_domain_node_span - get a cpumask for a node's sched_domain
6403 * @node: node whose cpumask we're constructing
Randy Dunlap73486722008-04-22 10:07:22 -07006404 * @span: resulting cpumask
John Hawkes9c1cfda2005-09-06 15:18:14 -07006405 *
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01006406 * Given a node, construct a good cpumask for its sched_domain to span. It
John Hawkes9c1cfda2005-09-06 15:18:14 -07006407 * should be one that prevents unnecessary balancing, but also spreads tasks
6408 * out optimally.
6409 */
Rusty Russell96f874e2008-11-25 02:35:14 +10306410static void sched_domain_node_span(int node, struct cpumask *span)
John Hawkes9c1cfda2005-09-06 15:18:14 -07006411{
Mike Travisc5f59f02008-04-04 18:11:10 -07006412 nodemask_t used_nodes;
Ingo Molnar48f24c42006-07-03 00:25:40 -07006413 int i;
John Hawkes9c1cfda2005-09-06 15:18:14 -07006414
Mike Travis6ca09df2008-12-31 18:08:45 -08006415 cpumask_clear(span);
Mike Travisc5f59f02008-04-04 18:11:10 -07006416 nodes_clear(used_nodes);
John Hawkes9c1cfda2005-09-06 15:18:14 -07006417
Mike Travis6ca09df2008-12-31 18:08:45 -08006418 cpumask_or(span, span, cpumask_of_node(node));
Mike Travisc5f59f02008-04-04 18:11:10 -07006419 node_set(node, used_nodes);
John Hawkes9c1cfda2005-09-06 15:18:14 -07006420
6421 for (i = 1; i < SD_NODES_PER_DOMAIN; i++) {
Mike Travisc5f59f02008-04-04 18:11:10 -07006422 int next_node = find_next_best_node(node, &used_nodes);
Ingo Molnar48f24c42006-07-03 00:25:40 -07006423
Mike Travis6ca09df2008-12-31 18:08:45 -08006424 cpumask_or(span, span, cpumask_of_node(next_node));
John Hawkes9c1cfda2005-09-06 15:18:14 -07006425 }
John Hawkes9c1cfda2005-09-06 15:18:14 -07006426}
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +02006427#endif /* CONFIG_NUMA */
John Hawkes9c1cfda2005-09-06 15:18:14 -07006428
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07006429int sched_smt_power_savings = 0, sched_mc_power_savings = 0;
Ingo Molnar48f24c42006-07-03 00:25:40 -07006430
John Hawkes9c1cfda2005-09-06 15:18:14 -07006431/*
Rusty Russell6c99e9a2008-11-25 02:35:04 +10306432 * The cpus mask in sched_group and sched_domain hangs off the end.
Ingo Molnar4200efd2009-05-19 09:22:19 +02006433 *
6434 * ( See the the comments in include/linux/sched.h:struct sched_group
6435 * and struct sched_domain. )
Rusty Russell6c99e9a2008-11-25 02:35:04 +10306436 */
6437struct static_sched_group {
6438 struct sched_group sg;
6439 DECLARE_BITMAP(cpus, CONFIG_NR_CPUS);
6440};
6441
6442struct static_sched_domain {
6443 struct sched_domain sd;
6444 DECLARE_BITMAP(span, CONFIG_NR_CPUS);
6445};
6446
Andreas Herrmann49a02c52009-08-18 12:51:52 +02006447struct s_data {
6448#ifdef CONFIG_NUMA
6449 int sd_allnodes;
6450 cpumask_var_t domainspan;
6451 cpumask_var_t covered;
6452 cpumask_var_t notcovered;
6453#endif
6454 cpumask_var_t nodemask;
6455 cpumask_var_t this_sibling_map;
6456 cpumask_var_t this_core_map;
Heiko Carstens01a08542010-08-31 10:28:16 +02006457 cpumask_var_t this_book_map;
Andreas Herrmann49a02c52009-08-18 12:51:52 +02006458 cpumask_var_t send_covered;
6459 cpumask_var_t tmpmask;
6460 struct sched_group **sched_group_nodes;
6461 struct root_domain *rd;
6462};
6463
Andreas Herrmann2109b992009-08-18 12:53:00 +02006464enum s_alloc {
6465 sa_sched_groups = 0,
6466 sa_rootdomain,
6467 sa_tmpmask,
6468 sa_send_covered,
Heiko Carstens01a08542010-08-31 10:28:16 +02006469 sa_this_book_map,
Andreas Herrmann2109b992009-08-18 12:53:00 +02006470 sa_this_core_map,
6471 sa_this_sibling_map,
6472 sa_nodemask,
6473 sa_sched_group_nodes,
6474#ifdef CONFIG_NUMA
6475 sa_notcovered,
6476 sa_covered,
6477 sa_domainspan,
6478#endif
6479 sa_none,
6480};
6481
Rusty Russell6c99e9a2008-11-25 02:35:04 +10306482/*
Ingo Molnar48f24c42006-07-03 00:25:40 -07006483 * SMT sched-domains:
John Hawkes9c1cfda2005-09-06 15:18:14 -07006484 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006485#ifdef CONFIG_SCHED_SMT
Rusty Russell6c99e9a2008-11-25 02:35:04 +10306486static DEFINE_PER_CPU(struct static_sched_domain, cpu_domains);
Tejun Heo1871e522009-10-29 22:34:13 +09006487static DEFINE_PER_CPU(struct static_sched_group, sched_groups);
Ingo Molnar48f24c42006-07-03 00:25:40 -07006488
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01006489static int
Rusty Russell96f874e2008-11-25 02:35:14 +10306490cpu_to_cpu_group(int cpu, const struct cpumask *cpu_map,
6491 struct sched_group **sg, struct cpumask *unused)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006492{
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08006493 if (sg)
Tejun Heo1871e522009-10-29 22:34:13 +09006494 *sg = &per_cpu(sched_groups, cpu).sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006495 return cpu;
6496}
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +02006497#endif /* CONFIG_SCHED_SMT */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006498
Ingo Molnar48f24c42006-07-03 00:25:40 -07006499/*
6500 * multi-core sched-domains:
6501 */
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08006502#ifdef CONFIG_SCHED_MC
Rusty Russell6c99e9a2008-11-25 02:35:04 +10306503static DEFINE_PER_CPU(struct static_sched_domain, core_domains);
6504static DEFINE_PER_CPU(struct static_sched_group, sched_group_core);
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08006505
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01006506static int
Rusty Russell96f874e2008-11-25 02:35:14 +10306507cpu_to_core_group(int cpu, const struct cpumask *cpu_map,
6508 struct sched_group **sg, struct cpumask *mask)
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08006509{
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08006510 int group;
Heiko Carstensf2698932010-08-31 10:28:15 +02006511#ifdef CONFIG_SCHED_SMT
Rusty Russellc69fc562009-03-13 14:49:46 +10306512 cpumask_and(mask, topology_thread_cpumask(cpu), cpu_map);
Rusty Russell96f874e2008-11-25 02:35:14 +10306513 group = cpumask_first(mask);
Heiko Carstensf2698932010-08-31 10:28:15 +02006514#else
6515 group = cpu;
6516#endif
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08006517 if (sg)
Rusty Russell6c99e9a2008-11-25 02:35:04 +10306518 *sg = &per_cpu(sched_group_core, group).sg;
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08006519 return group;
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08006520}
Heiko Carstensf2698932010-08-31 10:28:15 +02006521#endif /* CONFIG_SCHED_MC */
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08006522
Heiko Carstens01a08542010-08-31 10:28:16 +02006523/*
6524 * book sched-domains:
6525 */
6526#ifdef CONFIG_SCHED_BOOK
6527static DEFINE_PER_CPU(struct static_sched_domain, book_domains);
6528static DEFINE_PER_CPU(struct static_sched_group, sched_group_book);
6529
Linus Torvalds1da177e2005-04-16 15:20:36 -07006530static int
Heiko Carstens01a08542010-08-31 10:28:16 +02006531cpu_to_book_group(int cpu, const struct cpumask *cpu_map,
6532 struct sched_group **sg, struct cpumask *mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006533{
Heiko Carstens01a08542010-08-31 10:28:16 +02006534 int group = cpu;
6535#ifdef CONFIG_SCHED_MC
6536 cpumask_and(mask, cpu_coregroup_mask(cpu), cpu_map);
6537 group = cpumask_first(mask);
6538#elif defined(CONFIG_SCHED_SMT)
6539 cpumask_and(mask, topology_thread_cpumask(cpu), cpu_map);
6540 group = cpumask_first(mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006541#endif
Heiko Carstens01a08542010-08-31 10:28:16 +02006542 if (sg)
6543 *sg = &per_cpu(sched_group_book, group).sg;
6544 return group;
6545}
6546#endif /* CONFIG_SCHED_BOOK */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006547
Rusty Russell6c99e9a2008-11-25 02:35:04 +10306548static DEFINE_PER_CPU(struct static_sched_domain, phys_domains);
6549static DEFINE_PER_CPU(struct static_sched_group, sched_group_phys);
Ingo Molnar48f24c42006-07-03 00:25:40 -07006550
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01006551static int
Rusty Russell96f874e2008-11-25 02:35:14 +10306552cpu_to_phys_group(int cpu, const struct cpumask *cpu_map,
6553 struct sched_group **sg, struct cpumask *mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006554{
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08006555 int group;
Heiko Carstens01a08542010-08-31 10:28:16 +02006556#ifdef CONFIG_SCHED_BOOK
6557 cpumask_and(mask, cpu_book_mask(cpu), cpu_map);
6558 group = cpumask_first(mask);
6559#elif defined(CONFIG_SCHED_MC)
Mike Travis6ca09df2008-12-31 18:08:45 -08006560 cpumask_and(mask, cpu_coregroup_mask(cpu), cpu_map);
Rusty Russell96f874e2008-11-25 02:35:14 +10306561 group = cpumask_first(mask);
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08006562#elif defined(CONFIG_SCHED_SMT)
Rusty Russellc69fc562009-03-13 14:49:46 +10306563 cpumask_and(mask, topology_thread_cpumask(cpu), cpu_map);
Rusty Russell96f874e2008-11-25 02:35:14 +10306564 group = cpumask_first(mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006565#else
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08006566 group = cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006567#endif
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08006568 if (sg)
Rusty Russell6c99e9a2008-11-25 02:35:04 +10306569 *sg = &per_cpu(sched_group_phys, group).sg;
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08006570 return group;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006571}
6572
6573#ifdef CONFIG_NUMA
John Hawkes9c1cfda2005-09-06 15:18:14 -07006574/*
6575 * The init_sched_build_groups can't handle what we want to do with node
6576 * groups, so roll our own. Now each node has its own list of groups which
6577 * gets dynamically allocated.
6578 */
Rusty Russell62ea9ce2009-01-11 01:04:16 +01006579static DEFINE_PER_CPU(struct static_sched_domain, node_domains);
Mike Travis434d53b2008-04-04 18:11:04 -07006580static struct sched_group ***sched_group_nodes_bycpu;
John Hawkes9c1cfda2005-09-06 15:18:14 -07006581
Rusty Russell62ea9ce2009-01-11 01:04:16 +01006582static DEFINE_PER_CPU(struct static_sched_domain, allnodes_domains);
Rusty Russell6c99e9a2008-11-25 02:35:04 +10306583static DEFINE_PER_CPU(struct static_sched_group, sched_group_allnodes);
John Hawkes9c1cfda2005-09-06 15:18:14 -07006584
Rusty Russell96f874e2008-11-25 02:35:14 +10306585static int cpu_to_allnodes_group(int cpu, const struct cpumask *cpu_map,
6586 struct sched_group **sg,
6587 struct cpumask *nodemask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006588{
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08006589 int group;
6590
Mike Travis6ca09df2008-12-31 18:08:45 -08006591 cpumask_and(nodemask, cpumask_of_node(cpu_to_node(cpu)), cpu_map);
Rusty Russell96f874e2008-11-25 02:35:14 +10306592 group = cpumask_first(nodemask);
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08006593
6594 if (sg)
Rusty Russell6c99e9a2008-11-25 02:35:04 +10306595 *sg = &per_cpu(sched_group_allnodes, group).sg;
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08006596 return group;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006597}
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08006598
Siddha, Suresh B08069032006-03-27 01:15:23 -08006599static void init_numa_sched_groups_power(struct sched_group *group_head)
6600{
6601 struct sched_group *sg = group_head;
6602 int j;
6603
6604 if (!sg)
6605 return;
Andi Kleen3a5c3592007-10-15 17:00:14 +02006606 do {
Rusty Russell758b2cd2008-11-25 02:35:04 +10306607 for_each_cpu(j, sched_group_cpus(sg)) {
Andi Kleen3a5c3592007-10-15 17:00:14 +02006608 struct sched_domain *sd;
Siddha, Suresh B08069032006-03-27 01:15:23 -08006609
Rusty Russell6c99e9a2008-11-25 02:35:04 +10306610 sd = &per_cpu(phys_domains, j).sd;
Miao Xie13318a72009-04-15 09:59:10 +08006611 if (j != group_first_cpu(sd->groups)) {
Andi Kleen3a5c3592007-10-15 17:00:14 +02006612 /*
6613 * Only add "power" once for each
6614 * physical package.
6615 */
6616 continue;
6617 }
6618
Peter Zijlstra18a38852009-09-01 10:34:39 +02006619 sg->cpu_power += sd->groups->cpu_power;
Siddha, Suresh B08069032006-03-27 01:15:23 -08006620 }
Andi Kleen3a5c3592007-10-15 17:00:14 +02006621 sg = sg->next;
6622 } while (sg != group_head);
Siddha, Suresh B08069032006-03-27 01:15:23 -08006623}
Andreas Herrmann0601a882009-08-18 13:01:11 +02006624
6625static int build_numa_sched_groups(struct s_data *d,
6626 const struct cpumask *cpu_map, int num)
6627{
6628 struct sched_domain *sd;
6629 struct sched_group *sg, *prev;
6630 int n, j;
6631
6632 cpumask_clear(d->covered);
6633 cpumask_and(d->nodemask, cpumask_of_node(num), cpu_map);
6634 if (cpumask_empty(d->nodemask)) {
6635 d->sched_group_nodes[num] = NULL;
6636 goto out;
6637 }
6638
6639 sched_domain_node_span(num, d->domainspan);
6640 cpumask_and(d->domainspan, d->domainspan, cpu_map);
6641
6642 sg = kmalloc_node(sizeof(struct sched_group) + cpumask_size(),
6643 GFP_KERNEL, num);
6644 if (!sg) {
Peter Zijlstra3df0fc52009-12-20 14:23:57 +01006645 printk(KERN_WARNING "Can not alloc domain group for node %d\n",
6646 num);
Andreas Herrmann0601a882009-08-18 13:01:11 +02006647 return -ENOMEM;
6648 }
6649 d->sched_group_nodes[num] = sg;
6650
6651 for_each_cpu(j, d->nodemask) {
6652 sd = &per_cpu(node_domains, j).sd;
6653 sd->groups = sg;
6654 }
6655
Peter Zijlstra18a38852009-09-01 10:34:39 +02006656 sg->cpu_power = 0;
Andreas Herrmann0601a882009-08-18 13:01:11 +02006657 cpumask_copy(sched_group_cpus(sg), d->nodemask);
6658 sg->next = sg;
6659 cpumask_or(d->covered, d->covered, d->nodemask);
6660
6661 prev = sg;
6662 for (j = 0; j < nr_node_ids; j++) {
6663 n = (num + j) % nr_node_ids;
6664 cpumask_complement(d->notcovered, d->covered);
6665 cpumask_and(d->tmpmask, d->notcovered, cpu_map);
6666 cpumask_and(d->tmpmask, d->tmpmask, d->domainspan);
6667 if (cpumask_empty(d->tmpmask))
6668 break;
6669 cpumask_and(d->tmpmask, d->tmpmask, cpumask_of_node(n));
6670 if (cpumask_empty(d->tmpmask))
6671 continue;
6672 sg = kmalloc_node(sizeof(struct sched_group) + cpumask_size(),
6673 GFP_KERNEL, num);
6674 if (!sg) {
Peter Zijlstra3df0fc52009-12-20 14:23:57 +01006675 printk(KERN_WARNING
6676 "Can not alloc domain group for node %d\n", j);
Andreas Herrmann0601a882009-08-18 13:01:11 +02006677 return -ENOMEM;
6678 }
Peter Zijlstra18a38852009-09-01 10:34:39 +02006679 sg->cpu_power = 0;
Andreas Herrmann0601a882009-08-18 13:01:11 +02006680 cpumask_copy(sched_group_cpus(sg), d->tmpmask);
6681 sg->next = prev->next;
6682 cpumask_or(d->covered, d->covered, d->tmpmask);
6683 prev->next = sg;
6684 prev = sg;
6685 }
6686out:
6687 return 0;
6688}
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +02006689#endif /* CONFIG_NUMA */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006690
Siddha, Suresh Ba6160582006-10-03 01:14:06 -07006691#ifdef CONFIG_NUMA
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07006692/* Free memory allocated for various sched_group structures */
Rusty Russell96f874e2008-11-25 02:35:14 +10306693static void free_sched_groups(const struct cpumask *cpu_map,
6694 struct cpumask *nodemask)
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07006695{
Siddha, Suresh Ba6160582006-10-03 01:14:06 -07006696 int cpu, i;
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07006697
Rusty Russellabcd0832008-11-25 02:35:02 +10306698 for_each_cpu(cpu, cpu_map) {
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07006699 struct sched_group **sched_group_nodes
6700 = sched_group_nodes_bycpu[cpu];
6701
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07006702 if (!sched_group_nodes)
6703 continue;
6704
Mike Travis076ac2a2008-05-12 21:21:12 +02006705 for (i = 0; i < nr_node_ids; i++) {
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07006706 struct sched_group *oldsg, *sg = sched_group_nodes[i];
6707
Mike Travis6ca09df2008-12-31 18:08:45 -08006708 cpumask_and(nodemask, cpumask_of_node(i), cpu_map);
Rusty Russell96f874e2008-11-25 02:35:14 +10306709 if (cpumask_empty(nodemask))
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07006710 continue;
6711
6712 if (sg == NULL)
6713 continue;
6714 sg = sg->next;
6715next_sg:
6716 oldsg = sg;
6717 sg = sg->next;
6718 kfree(oldsg);
6719 if (oldsg != sched_group_nodes[i])
6720 goto next_sg;
6721 }
6722 kfree(sched_group_nodes);
6723 sched_group_nodes_bycpu[cpu] = NULL;
6724 }
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07006725}
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +02006726#else /* !CONFIG_NUMA */
Rusty Russell96f874e2008-11-25 02:35:14 +10306727static void free_sched_groups(const struct cpumask *cpu_map,
6728 struct cpumask *nodemask)
Siddha, Suresh Ba6160582006-10-03 01:14:06 -07006729{
6730}
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +02006731#endif /* CONFIG_NUMA */
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07006732
Linus Torvalds1da177e2005-04-16 15:20:36 -07006733/*
Siddha, Suresh B89c47102006-10-03 01:14:09 -07006734 * Initialize sched groups cpu_power.
6735 *
6736 * cpu_power indicates the capacity of sched group, which is used while
6737 * distributing the load between different sched groups in a sched domain.
6738 * Typically cpu_power for all the groups in a sched domain will be same unless
6739 * there are asymmetries in the topology. If there are asymmetries, group
6740 * having more cpu_power will pickup more load compared to the group having
6741 * less cpu_power.
Siddha, Suresh B89c47102006-10-03 01:14:09 -07006742 */
6743static void init_sched_groups_power(int cpu, struct sched_domain *sd)
6744{
6745 struct sched_domain *child;
6746 struct sched_group *group;
Peter Zijlstraf93e65c2009-09-01 10:34:32 +02006747 long power;
6748 int weight;
Siddha, Suresh B89c47102006-10-03 01:14:09 -07006749
6750 WARN_ON(!sd || !sd->groups);
6751
Miao Xie13318a72009-04-15 09:59:10 +08006752 if (cpu != group_first_cpu(sd->groups))
Siddha, Suresh B89c47102006-10-03 01:14:09 -07006753 return;
6754
Suresh Siddhaaae6d3d2010-09-17 15:02:32 -07006755 sd->groups->group_weight = cpumask_weight(sched_group_cpus(sd->groups));
6756
Siddha, Suresh B89c47102006-10-03 01:14:09 -07006757 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
Heiko Carstens01a08542010-08-31 10:28:16 +02006822#ifdef CONFIG_SCHED_BOOK
6823 SD_INIT_FUNC(BOOK)
6824#endif
Mike Travis7c16ec52008-04-04 18:11:11 -07006825
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09006826static int default_relax_domain_level = -1;
6827
6828static int __init setup_relax_domain_level(char *str)
6829{
Li Zefan30e0e172008-05-13 10:27:17 +08006830 unsigned long val;
6831
6832 val = simple_strtoul(str, NULL, 0);
6833 if (val < SD_LV_MAX)
6834 default_relax_domain_level = val;
6835
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09006836 return 1;
6837}
6838__setup("relax_domain_level=", setup_relax_domain_level);
6839
6840static void set_domain_attribute(struct sched_domain *sd,
6841 struct sched_domain_attr *attr)
6842{
6843 int request;
6844
6845 if (!attr || attr->relax_domain_level < 0) {
6846 if (default_relax_domain_level < 0)
6847 return;
6848 else
6849 request = default_relax_domain_level;
6850 } else
6851 request = attr->relax_domain_level;
6852 if (request < sd->level) {
6853 /* turn off 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 } else {
6856 /* turn on idle balance on this domain */
Peter Zijlstrac88d5912009-09-10 13:50:02 +02006857 sd->flags |= (SD_BALANCE_WAKE|SD_BALANCE_NEWIDLE);
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09006858 }
6859}
6860
Andreas Herrmann2109b992009-08-18 12:53:00 +02006861static void __free_domain_allocs(struct s_data *d, enum s_alloc what,
6862 const struct cpumask *cpu_map)
6863{
6864 switch (what) {
6865 case sa_sched_groups:
6866 free_sched_groups(cpu_map, d->tmpmask); /* fall through */
6867 d->sched_group_nodes = NULL;
6868 case sa_rootdomain:
6869 free_rootdomain(d->rd); /* fall through */
6870 case sa_tmpmask:
6871 free_cpumask_var(d->tmpmask); /* fall through */
6872 case sa_send_covered:
6873 free_cpumask_var(d->send_covered); /* fall through */
Heiko Carstens01a08542010-08-31 10:28:16 +02006874 case sa_this_book_map:
6875 free_cpumask_var(d->this_book_map); /* fall through */
Andreas Herrmann2109b992009-08-18 12:53:00 +02006876 case sa_this_core_map:
6877 free_cpumask_var(d->this_core_map); /* fall through */
6878 case sa_this_sibling_map:
6879 free_cpumask_var(d->this_sibling_map); /* fall through */
6880 case sa_nodemask:
6881 free_cpumask_var(d->nodemask); /* fall through */
6882 case sa_sched_group_nodes:
6883#ifdef CONFIG_NUMA
6884 kfree(d->sched_group_nodes); /* fall through */
6885 case sa_notcovered:
6886 free_cpumask_var(d->notcovered); /* fall through */
6887 case sa_covered:
6888 free_cpumask_var(d->covered); /* fall through */
6889 case sa_domainspan:
6890 free_cpumask_var(d->domainspan); /* fall through */
6891#endif
6892 case sa_none:
6893 break;
6894 }
6895}
6896
6897static enum s_alloc __visit_domain_allocation_hell(struct s_data *d,
6898 const struct cpumask *cpu_map)
6899{
6900#ifdef CONFIG_NUMA
6901 if (!alloc_cpumask_var(&d->domainspan, GFP_KERNEL))
6902 return sa_none;
6903 if (!alloc_cpumask_var(&d->covered, GFP_KERNEL))
6904 return sa_domainspan;
6905 if (!alloc_cpumask_var(&d->notcovered, GFP_KERNEL))
6906 return sa_covered;
6907 /* Allocate the per-node list of sched groups */
6908 d->sched_group_nodes = kcalloc(nr_node_ids,
6909 sizeof(struct sched_group *), GFP_KERNEL);
6910 if (!d->sched_group_nodes) {
Peter Zijlstra3df0fc52009-12-20 14:23:57 +01006911 printk(KERN_WARNING "Can not alloc sched group node list\n");
Andreas Herrmann2109b992009-08-18 12:53:00 +02006912 return sa_notcovered;
6913 }
6914 sched_group_nodes_bycpu[cpumask_first(cpu_map)] = d->sched_group_nodes;
6915#endif
6916 if (!alloc_cpumask_var(&d->nodemask, GFP_KERNEL))
6917 return sa_sched_group_nodes;
6918 if (!alloc_cpumask_var(&d->this_sibling_map, GFP_KERNEL))
6919 return sa_nodemask;
6920 if (!alloc_cpumask_var(&d->this_core_map, GFP_KERNEL))
6921 return sa_this_sibling_map;
Heiko Carstens01a08542010-08-31 10:28:16 +02006922 if (!alloc_cpumask_var(&d->this_book_map, GFP_KERNEL))
Andreas Herrmann2109b992009-08-18 12:53:00 +02006923 return sa_this_core_map;
Heiko Carstens01a08542010-08-31 10:28:16 +02006924 if (!alloc_cpumask_var(&d->send_covered, GFP_KERNEL))
6925 return sa_this_book_map;
Andreas Herrmann2109b992009-08-18 12:53:00 +02006926 if (!alloc_cpumask_var(&d->tmpmask, GFP_KERNEL))
6927 return sa_send_covered;
6928 d->rd = alloc_rootdomain();
6929 if (!d->rd) {
Peter Zijlstra3df0fc52009-12-20 14:23:57 +01006930 printk(KERN_WARNING "Cannot alloc root domain\n");
Andreas Herrmann2109b992009-08-18 12:53:00 +02006931 return sa_tmpmask;
6932 }
6933 return sa_rootdomain;
6934}
6935
Andreas Herrmann7f4588f2009-08-18 12:54:06 +02006936static struct sched_domain *__build_numa_sched_domains(struct s_data *d,
6937 const struct cpumask *cpu_map, struct sched_domain_attr *attr, int i)
6938{
6939 struct sched_domain *sd = NULL;
6940#ifdef CONFIG_NUMA
6941 struct sched_domain *parent;
6942
6943 d->sd_allnodes = 0;
6944 if (cpumask_weight(cpu_map) >
6945 SD_NODES_PER_DOMAIN * cpumask_weight(d->nodemask)) {
6946 sd = &per_cpu(allnodes_domains, i).sd;
6947 SD_INIT(sd, ALLNODES);
6948 set_domain_attribute(sd, attr);
6949 cpumask_copy(sched_domain_span(sd), cpu_map);
6950 cpu_to_allnodes_group(i, cpu_map, &sd->groups, d->tmpmask);
6951 d->sd_allnodes = 1;
6952 }
6953 parent = sd;
6954
6955 sd = &per_cpu(node_domains, i).sd;
6956 SD_INIT(sd, NODE);
6957 set_domain_attribute(sd, attr);
6958 sched_domain_node_span(cpu_to_node(i), sched_domain_span(sd));
6959 sd->parent = parent;
6960 if (parent)
6961 parent->child = sd;
6962 cpumask_and(sched_domain_span(sd), sched_domain_span(sd), cpu_map);
6963#endif
6964 return sd;
6965}
6966
Andreas Herrmann87cce662009-08-18 12:54:55 +02006967static struct sched_domain *__build_cpu_sched_domain(struct s_data *d,
6968 const struct cpumask *cpu_map, struct sched_domain_attr *attr,
6969 struct sched_domain *parent, int i)
6970{
6971 struct sched_domain *sd;
6972 sd = &per_cpu(phys_domains, i).sd;
6973 SD_INIT(sd, CPU);
6974 set_domain_attribute(sd, attr);
6975 cpumask_copy(sched_domain_span(sd), d->nodemask);
6976 sd->parent = parent;
6977 if (parent)
6978 parent->child = sd;
6979 cpu_to_phys_group(i, cpu_map, &sd->groups, d->tmpmask);
6980 return sd;
6981}
6982
Heiko Carstens01a08542010-08-31 10:28:16 +02006983static struct sched_domain *__build_book_sched_domain(struct s_data *d,
6984 const struct cpumask *cpu_map, struct sched_domain_attr *attr,
6985 struct sched_domain *parent, int i)
6986{
6987 struct sched_domain *sd = parent;
6988#ifdef CONFIG_SCHED_BOOK
6989 sd = &per_cpu(book_domains, i).sd;
6990 SD_INIT(sd, BOOK);
6991 set_domain_attribute(sd, attr);
6992 cpumask_and(sched_domain_span(sd), cpu_map, cpu_book_mask(i));
6993 sd->parent = parent;
6994 parent->child = sd;
6995 cpu_to_book_group(i, cpu_map, &sd->groups, d->tmpmask);
6996#endif
6997 return sd;
6998}
6999
Andreas Herrmann410c4082009-08-18 12:56:14 +02007000static struct sched_domain *__build_mc_sched_domain(struct s_data *d,
7001 const struct cpumask *cpu_map, struct sched_domain_attr *attr,
7002 struct sched_domain *parent, int i)
7003{
7004 struct sched_domain *sd = parent;
7005#ifdef CONFIG_SCHED_MC
7006 sd = &per_cpu(core_domains, i).sd;
7007 SD_INIT(sd, MC);
7008 set_domain_attribute(sd, attr);
7009 cpumask_and(sched_domain_span(sd), cpu_map, cpu_coregroup_mask(i));
7010 sd->parent = parent;
7011 parent->child = sd;
7012 cpu_to_core_group(i, cpu_map, &sd->groups, d->tmpmask);
7013#endif
7014 return sd;
7015}
7016
Andreas Herrmannd8173532009-08-18 12:57:03 +02007017static struct sched_domain *__build_smt_sched_domain(struct s_data *d,
7018 const struct cpumask *cpu_map, struct sched_domain_attr *attr,
7019 struct sched_domain *parent, int i)
7020{
7021 struct sched_domain *sd = parent;
7022#ifdef CONFIG_SCHED_SMT
7023 sd = &per_cpu(cpu_domains, i).sd;
7024 SD_INIT(sd, SIBLING);
7025 set_domain_attribute(sd, attr);
7026 cpumask_and(sched_domain_span(sd), cpu_map, topology_thread_cpumask(i));
7027 sd->parent = parent;
7028 parent->child = sd;
7029 cpu_to_cpu_group(i, cpu_map, &sd->groups, d->tmpmask);
7030#endif
7031 return sd;
7032}
7033
Andreas Herrmann0e8e85c2009-08-18 12:57:51 +02007034static void build_sched_groups(struct s_data *d, enum sched_domain_level l,
7035 const struct cpumask *cpu_map, int cpu)
7036{
7037 switch (l) {
7038#ifdef CONFIG_SCHED_SMT
7039 case SD_LV_SIBLING: /* set up CPU (sibling) groups */
7040 cpumask_and(d->this_sibling_map, cpu_map,
7041 topology_thread_cpumask(cpu));
7042 if (cpu == cpumask_first(d->this_sibling_map))
7043 init_sched_build_groups(d->this_sibling_map, cpu_map,
7044 &cpu_to_cpu_group,
7045 d->send_covered, d->tmpmask);
7046 break;
7047#endif
Andreas Herrmanna2af04c2009-08-18 12:58:38 +02007048#ifdef CONFIG_SCHED_MC
7049 case SD_LV_MC: /* set up multi-core groups */
7050 cpumask_and(d->this_core_map, cpu_map, cpu_coregroup_mask(cpu));
7051 if (cpu == cpumask_first(d->this_core_map))
7052 init_sched_build_groups(d->this_core_map, cpu_map,
7053 &cpu_to_core_group,
7054 d->send_covered, d->tmpmask);
7055 break;
7056#endif
Heiko Carstens01a08542010-08-31 10:28:16 +02007057#ifdef CONFIG_SCHED_BOOK
7058 case SD_LV_BOOK: /* set up book groups */
7059 cpumask_and(d->this_book_map, cpu_map, cpu_book_mask(cpu));
7060 if (cpu == cpumask_first(d->this_book_map))
7061 init_sched_build_groups(d->this_book_map, cpu_map,
7062 &cpu_to_book_group,
7063 d->send_covered, d->tmpmask);
7064 break;
7065#endif
Andreas Herrmann86548092009-08-18 12:59:28 +02007066 case SD_LV_CPU: /* set up physical groups */
7067 cpumask_and(d->nodemask, cpumask_of_node(cpu), cpu_map);
7068 if (!cpumask_empty(d->nodemask))
7069 init_sched_build_groups(d->nodemask, cpu_map,
7070 &cpu_to_phys_group,
7071 d->send_covered, d->tmpmask);
7072 break;
Andreas Herrmannde616e32009-08-18 13:00:13 +02007073#ifdef CONFIG_NUMA
7074 case SD_LV_ALLNODES:
7075 init_sched_build_groups(cpu_map, cpu_map, &cpu_to_allnodes_group,
7076 d->send_covered, d->tmpmask);
7077 break;
7078#endif
Andreas Herrmann0e8e85c2009-08-18 12:57:51 +02007079 default:
7080 break;
7081 }
7082}
7083
Mike Travis7c16ec52008-04-04 18:11:11 -07007084/*
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07007085 * Build sched domains for a given set of cpus and attach the sched domains
7086 * to the individual cpus
Linus Torvalds1da177e2005-04-16 15:20:36 -07007087 */
Rusty Russell96f874e2008-11-25 02:35:14 +10307088static int __build_sched_domains(const struct cpumask *cpu_map,
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09007089 struct sched_domain_attr *attr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007090{
Andreas Herrmann2109b992009-08-18 12:53:00 +02007091 enum s_alloc alloc_state = sa_none;
Andreas Herrmann49a02c52009-08-18 12:51:52 +02007092 struct s_data d;
Andreas Herrmann294b0c92009-08-18 13:02:29 +02007093 struct sched_domain *sd;
Andreas Herrmann2109b992009-08-18 12:53:00 +02007094 int i;
John Hawkesd1b55132005-09-06 15:18:14 -07007095#ifdef CONFIG_NUMA
Andreas Herrmann49a02c52009-08-18 12:51:52 +02007096 d.sd_allnodes = 0;
Rusty Russell3404c8d2008-11-25 02:35:03 +10307097#endif
7098
Andreas Herrmann2109b992009-08-18 12:53:00 +02007099 alloc_state = __visit_domain_allocation_hell(&d, cpu_map);
7100 if (alloc_state != sa_rootdomain)
7101 goto error;
7102 alloc_state = sa_sched_groups;
Mike Travis7c16ec52008-04-04 18:11:11 -07007103
Linus Torvalds1da177e2005-04-16 15:20:36 -07007104 /*
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07007105 * Set up domains for cpus specified by the cpu_map.
Linus Torvalds1da177e2005-04-16 15:20:36 -07007106 */
Rusty Russellabcd0832008-11-25 02:35:02 +10307107 for_each_cpu(i, cpu_map) {
Andreas Herrmann49a02c52009-08-18 12:51:52 +02007108 cpumask_and(d.nodemask, cpumask_of_node(cpu_to_node(i)),
7109 cpu_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007110
Andreas Herrmann7f4588f2009-08-18 12:54:06 +02007111 sd = __build_numa_sched_domains(&d, cpu_map, attr, i);
Andreas Herrmann87cce662009-08-18 12:54:55 +02007112 sd = __build_cpu_sched_domain(&d, cpu_map, attr, sd, i);
Heiko Carstens01a08542010-08-31 10:28:16 +02007113 sd = __build_book_sched_domain(&d, cpu_map, attr, sd, i);
Andreas Herrmann410c4082009-08-18 12:56:14 +02007114 sd = __build_mc_sched_domain(&d, cpu_map, attr, sd, i);
Andreas Herrmannd8173532009-08-18 12:57:03 +02007115 sd = __build_smt_sched_domain(&d, cpu_map, attr, sd, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007116 }
7117
Rusty Russellabcd0832008-11-25 02:35:02 +10307118 for_each_cpu(i, cpu_map) {
Andreas Herrmann0e8e85c2009-08-18 12:57:51 +02007119 build_sched_groups(&d, SD_LV_SIBLING, cpu_map, i);
Heiko Carstens01a08542010-08-31 10:28:16 +02007120 build_sched_groups(&d, SD_LV_BOOK, cpu_map, i);
Andreas Herrmanna2af04c2009-08-18 12:58:38 +02007121 build_sched_groups(&d, SD_LV_MC, cpu_map, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007122 }
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08007123
Linus Torvalds1da177e2005-04-16 15:20:36 -07007124 /* Set up physical groups */
Andreas Herrmann86548092009-08-18 12:59:28 +02007125 for (i = 0; i < nr_node_ids; i++)
7126 build_sched_groups(&d, SD_LV_CPU, cpu_map, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007127
7128#ifdef CONFIG_NUMA
7129 /* Set up node groups */
Andreas Herrmannde616e32009-08-18 13:00:13 +02007130 if (d.sd_allnodes)
7131 build_sched_groups(&d, SD_LV_ALLNODES, cpu_map, 0);
John Hawkes9c1cfda2005-09-06 15:18:14 -07007132
Andreas Herrmann0601a882009-08-18 13:01:11 +02007133 for (i = 0; i < nr_node_ids; i++)
7134 if (build_numa_sched_groups(&d, cpu_map, i))
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07007135 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007136#endif
7137
7138 /* Calculate CPU power for physical packages and nodes */
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07007139#ifdef CONFIG_SCHED_SMT
Rusty Russellabcd0832008-11-25 02:35:02 +10307140 for_each_cpu(i, cpu_map) {
Andreas Herrmann294b0c92009-08-18 13:02:29 +02007141 sd = &per_cpu(cpu_domains, i).sd;
Siddha, Suresh B89c47102006-10-03 01:14:09 -07007142 init_sched_groups_power(i, sd);
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07007143 }
7144#endif
7145#ifdef CONFIG_SCHED_MC
Rusty Russellabcd0832008-11-25 02:35:02 +10307146 for_each_cpu(i, cpu_map) {
Andreas Herrmann294b0c92009-08-18 13:02:29 +02007147 sd = &per_cpu(core_domains, i).sd;
Siddha, Suresh B89c47102006-10-03 01:14:09 -07007148 init_sched_groups_power(i, sd);
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07007149 }
7150#endif
Heiko Carstens01a08542010-08-31 10:28:16 +02007151#ifdef CONFIG_SCHED_BOOK
7152 for_each_cpu(i, cpu_map) {
7153 sd = &per_cpu(book_domains, i).sd;
7154 init_sched_groups_power(i, sd);
7155 }
7156#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07007157
Rusty Russellabcd0832008-11-25 02:35:02 +10307158 for_each_cpu(i, cpu_map) {
Andreas Herrmann294b0c92009-08-18 13:02:29 +02007159 sd = &per_cpu(phys_domains, i).sd;
Siddha, Suresh B89c47102006-10-03 01:14:09 -07007160 init_sched_groups_power(i, sd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007161 }
7162
John Hawkes9c1cfda2005-09-06 15:18:14 -07007163#ifdef CONFIG_NUMA
Mike Travis076ac2a2008-05-12 21:21:12 +02007164 for (i = 0; i < nr_node_ids; i++)
Andreas Herrmann49a02c52009-08-18 12:51:52 +02007165 init_numa_sched_groups_power(d.sched_group_nodes[i]);
John Hawkes9c1cfda2005-09-06 15:18:14 -07007166
Andreas Herrmann49a02c52009-08-18 12:51:52 +02007167 if (d.sd_allnodes) {
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08007168 struct sched_group *sg;
Siddha, Suresh Bf712c0c2006-07-30 03:02:59 -07007169
Rusty Russell96f874e2008-11-25 02:35:14 +10307170 cpu_to_allnodes_group(cpumask_first(cpu_map), cpu_map, &sg,
Andreas Herrmann49a02c52009-08-18 12:51:52 +02007171 d.tmpmask);
Siddha, Suresh Bf712c0c2006-07-30 03:02:59 -07007172 init_numa_sched_groups_power(sg);
7173 }
John Hawkes9c1cfda2005-09-06 15:18:14 -07007174#endif
7175
Linus Torvalds1da177e2005-04-16 15:20:36 -07007176 /* Attach the domains */
Rusty Russellabcd0832008-11-25 02:35:02 +10307177 for_each_cpu(i, cpu_map) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007178#ifdef CONFIG_SCHED_SMT
Rusty Russell6c99e9a2008-11-25 02:35:04 +10307179 sd = &per_cpu(cpu_domains, i).sd;
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08007180#elif defined(CONFIG_SCHED_MC)
Rusty Russell6c99e9a2008-11-25 02:35:04 +10307181 sd = &per_cpu(core_domains, i).sd;
Heiko Carstens01a08542010-08-31 10:28:16 +02007182#elif defined(CONFIG_SCHED_BOOK)
7183 sd = &per_cpu(book_domains, i).sd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007184#else
Rusty Russell6c99e9a2008-11-25 02:35:04 +10307185 sd = &per_cpu(phys_domains, i).sd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007186#endif
Andreas Herrmann49a02c52009-08-18 12:51:52 +02007187 cpu_attach_domain(sd, d.rd, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007188 }
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07007189
Andreas Herrmann2109b992009-08-18 12:53:00 +02007190 d.sched_group_nodes = NULL; /* don't free this we still need it */
7191 __free_domain_allocs(&d, sa_tmpmask, cpu_map);
7192 return 0;
Rusty Russell3404c8d2008-11-25 02:35:03 +10307193
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07007194error:
Andreas Herrmann2109b992009-08-18 12:53:00 +02007195 __free_domain_allocs(&d, alloc_state, cpu_map);
7196 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007197}
Paul Jackson029190c2007-10-18 23:40:20 -07007198
Rusty Russell96f874e2008-11-25 02:35:14 +10307199static int build_sched_domains(const struct cpumask *cpu_map)
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09007200{
7201 return __build_sched_domains(cpu_map, NULL);
7202}
7203
Rusty Russellacc3f5d2009-11-03 14:53:40 +10307204static cpumask_var_t *doms_cur; /* current sched domains */
Paul Jackson029190c2007-10-18 23:40:20 -07007205static int ndoms_cur; /* number of sched domains in 'doms_cur' */
Ingo Molnar4285f5942008-05-16 17:47:14 +02007206static struct sched_domain_attr *dattr_cur;
7207 /* attribues of custom domains in 'doms_cur' */
Paul Jackson029190c2007-10-18 23:40:20 -07007208
7209/*
7210 * Special case: If a kmalloc of a doms_cur partition (array of
Rusty Russell42128232008-11-25 02:35:12 +10307211 * cpumask) fails, then fallback to a single sched domain,
7212 * as determined by the single cpumask fallback_doms.
Paul Jackson029190c2007-10-18 23:40:20 -07007213 */
Rusty Russell42128232008-11-25 02:35:12 +10307214static cpumask_var_t fallback_doms;
Paul Jackson029190c2007-10-18 23:40:20 -07007215
Heiko Carstensee79d1b2008-12-09 18:49:50 +01007216/*
7217 * arch_update_cpu_topology lets virtualized architectures update the
7218 * cpu core maps. It is supposed to return 1 if the topology changed
7219 * or 0 if it stayed the same.
7220 */
7221int __attribute__((weak)) arch_update_cpu_topology(void)
Heiko Carstens22e52b02008-03-12 18:31:59 +01007222{
Heiko Carstensee79d1b2008-12-09 18:49:50 +01007223 return 0;
Heiko Carstens22e52b02008-03-12 18:31:59 +01007224}
7225
Rusty Russellacc3f5d2009-11-03 14:53:40 +10307226cpumask_var_t *alloc_sched_domains(unsigned int ndoms)
7227{
7228 int i;
7229 cpumask_var_t *doms;
7230
7231 doms = kmalloc(sizeof(*doms) * ndoms, GFP_KERNEL);
7232 if (!doms)
7233 return NULL;
7234 for (i = 0; i < ndoms; i++) {
7235 if (!alloc_cpumask_var(&doms[i], GFP_KERNEL)) {
7236 free_sched_domains(doms, i);
7237 return NULL;
7238 }
7239 }
7240 return doms;
7241}
7242
7243void free_sched_domains(cpumask_var_t doms[], unsigned int ndoms)
7244{
7245 unsigned int i;
7246 for (i = 0; i < ndoms; i++)
7247 free_cpumask_var(doms[i]);
7248 kfree(doms);
7249}
7250
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07007251/*
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01007252 * Set up scheduler domains and groups. Callers must hold the hotplug lock.
Paul Jackson029190c2007-10-18 23:40:20 -07007253 * For now this just excludes isolated cpus, but could be used to
7254 * exclude other special cases in the future.
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07007255 */
Rusty Russell96f874e2008-11-25 02:35:14 +10307256static int arch_init_sched_domains(const struct cpumask *cpu_map)
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07007257{
Milton Miller73785472007-10-24 18:23:48 +02007258 int err;
7259
Heiko Carstens22e52b02008-03-12 18:31:59 +01007260 arch_update_cpu_topology();
Paul Jackson029190c2007-10-18 23:40:20 -07007261 ndoms_cur = 1;
Rusty Russellacc3f5d2009-11-03 14:53:40 +10307262 doms_cur = alloc_sched_domains(ndoms_cur);
Paul Jackson029190c2007-10-18 23:40:20 -07007263 if (!doms_cur)
Rusty Russellacc3f5d2009-11-03 14:53:40 +10307264 doms_cur = &fallback_doms;
7265 cpumask_andnot(doms_cur[0], cpu_map, cpu_isolated_map);
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09007266 dattr_cur = NULL;
Rusty Russellacc3f5d2009-11-03 14:53:40 +10307267 err = build_sched_domains(doms_cur[0]);
Milton Miller6382bc92007-10-15 17:00:19 +02007268 register_sched_domain_sysctl();
Milton Miller73785472007-10-24 18:23:48 +02007269
7270 return err;
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07007271}
7272
Rusty Russell96f874e2008-11-25 02:35:14 +10307273static void arch_destroy_sched_domains(const struct cpumask *cpu_map,
7274 struct cpumask *tmpmask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007275{
Mike Travis7c16ec52008-04-04 18:11:11 -07007276 free_sched_groups(cpu_map, tmpmask);
John Hawkes9c1cfda2005-09-06 15:18:14 -07007277}
Linus Torvalds1da177e2005-04-16 15:20:36 -07007278
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07007279/*
7280 * Detach sched domains from a group of cpus specified in cpu_map
7281 * These cpus will now be attached to the NULL domain
7282 */
Rusty Russell96f874e2008-11-25 02:35:14 +10307283static void detach_destroy_domains(const struct cpumask *cpu_map)
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07007284{
Rusty Russell96f874e2008-11-25 02:35:14 +10307285 /* Save because hotplug lock held. */
7286 static DECLARE_BITMAP(tmpmask, CONFIG_NR_CPUS);
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07007287 int i;
7288
Rusty Russellabcd0832008-11-25 02:35:02 +10307289 for_each_cpu(i, cpu_map)
Gregory Haskins57d885f2008-01-25 21:08:18 +01007290 cpu_attach_domain(NULL, &def_root_domain, i);
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07007291 synchronize_sched();
Rusty Russell96f874e2008-11-25 02:35:14 +10307292 arch_destroy_sched_domains(cpu_map, to_cpumask(tmpmask));
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07007293}
7294
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09007295/* handle null as "default" */
7296static int dattrs_equal(struct sched_domain_attr *cur, int idx_cur,
7297 struct sched_domain_attr *new, int idx_new)
7298{
7299 struct sched_domain_attr tmp;
7300
7301 /* fast path */
7302 if (!new && !cur)
7303 return 1;
7304
7305 tmp = SD_ATTR_INIT;
7306 return !memcmp(cur ? (cur + idx_cur) : &tmp,
7307 new ? (new + idx_new) : &tmp,
7308 sizeof(struct sched_domain_attr));
7309}
7310
Paul Jackson029190c2007-10-18 23:40:20 -07007311/*
7312 * Partition sched domains as specified by the 'ndoms_new'
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01007313 * cpumasks in the array doms_new[] of cpumasks. This compares
Paul Jackson029190c2007-10-18 23:40:20 -07007314 * doms_new[] to the current sched domain partitioning, doms_cur[].
7315 * It destroys each deleted domain and builds each new domain.
7316 *
Rusty Russellacc3f5d2009-11-03 14:53:40 +10307317 * 'doms_new' is an array of cpumask_var_t's of length 'ndoms_new'.
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01007318 * The masks don't intersect (don't overlap.) We should setup one
7319 * sched domain for each mask. CPUs not in any of the cpumasks will
7320 * not be load balanced. If the same cpumask appears both in the
Paul Jackson029190c2007-10-18 23:40:20 -07007321 * current 'doms_cur' domains and in the new 'doms_new', we can leave
7322 * it as it is.
7323 *
Rusty Russellacc3f5d2009-11-03 14:53:40 +10307324 * The passed in 'doms_new' should be allocated using
7325 * alloc_sched_domains. This routine takes ownership of it and will
7326 * free_sched_domains it when done with it. If the caller failed the
7327 * alloc call, then it can pass in doms_new == NULL && ndoms_new == 1,
7328 * and partition_sched_domains() will fallback to the single partition
7329 * 'fallback_doms', it also forces the domains to be rebuilt.
Paul Jackson029190c2007-10-18 23:40:20 -07007330 *
Rusty Russell96f874e2008-11-25 02:35:14 +10307331 * If doms_new == NULL it will be replaced with cpu_online_mask.
Li Zefan700018e2008-11-18 14:02:03 +08007332 * ndoms_new == 0 is a special case for destroying existing domains,
7333 * and it will not create the default domain.
Max Krasnyanskydfb512e2008-08-29 13:11:41 -07007334 *
Paul Jackson029190c2007-10-18 23:40:20 -07007335 * Call with hotplug lock held
7336 */
Rusty Russellacc3f5d2009-11-03 14:53:40 +10307337void partition_sched_domains(int ndoms_new, cpumask_var_t doms_new[],
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09007338 struct sched_domain_attr *dattr_new)
Paul Jackson029190c2007-10-18 23:40:20 -07007339{
Max Krasnyanskydfb512e2008-08-29 13:11:41 -07007340 int i, j, n;
Heiko Carstensd65bd5e2008-12-09 18:49:51 +01007341 int new_topology;
Paul Jackson029190c2007-10-18 23:40:20 -07007342
Heiko Carstens712555e2008-04-28 11:33:07 +02007343 mutex_lock(&sched_domains_mutex);
Srivatsa Vaddagiria1835612008-01-25 21:08:00 +01007344
Milton Miller73785472007-10-24 18:23:48 +02007345 /* always unregister in case we don't destroy any domains */
7346 unregister_sched_domain_sysctl();
7347
Heiko Carstensd65bd5e2008-12-09 18:49:51 +01007348 /* Let architecture update cpu core mappings. */
7349 new_topology = arch_update_cpu_topology();
7350
Max Krasnyanskydfb512e2008-08-29 13:11:41 -07007351 n = doms_new ? ndoms_new : 0;
Paul Jackson029190c2007-10-18 23:40:20 -07007352
7353 /* Destroy deleted domains */
7354 for (i = 0; i < ndoms_cur; i++) {
Heiko Carstensd65bd5e2008-12-09 18:49:51 +01007355 for (j = 0; j < n && !new_topology; j++) {
Rusty Russellacc3f5d2009-11-03 14:53:40 +10307356 if (cpumask_equal(doms_cur[i], doms_new[j])
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09007357 && dattrs_equal(dattr_cur, i, dattr_new, j))
Paul Jackson029190c2007-10-18 23:40:20 -07007358 goto match1;
7359 }
7360 /* no match - a current sched domain not in new doms_new[] */
Rusty Russellacc3f5d2009-11-03 14:53:40 +10307361 detach_destroy_domains(doms_cur[i]);
Paul Jackson029190c2007-10-18 23:40:20 -07007362match1:
7363 ;
7364 }
7365
Max Krasnyanskye761b772008-07-15 04:43:49 -07007366 if (doms_new == NULL) {
7367 ndoms_cur = 0;
Rusty Russellacc3f5d2009-11-03 14:53:40 +10307368 doms_new = &fallback_doms;
Peter Zijlstra6ad4c182009-11-25 13:31:39 +01007369 cpumask_andnot(doms_new[0], cpu_active_mask, cpu_isolated_map);
Li Zefanfaa2f982008-11-04 16:20:23 +08007370 WARN_ON_ONCE(dattr_new);
Max Krasnyanskye761b772008-07-15 04:43:49 -07007371 }
7372
Paul Jackson029190c2007-10-18 23:40:20 -07007373 /* Build new domains */
7374 for (i = 0; i < ndoms_new; i++) {
Heiko Carstensd65bd5e2008-12-09 18:49:51 +01007375 for (j = 0; j < ndoms_cur && !new_topology; j++) {
Rusty Russellacc3f5d2009-11-03 14:53:40 +10307376 if (cpumask_equal(doms_new[i], doms_cur[j])
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09007377 && dattrs_equal(dattr_new, i, dattr_cur, j))
Paul Jackson029190c2007-10-18 23:40:20 -07007378 goto match2;
7379 }
7380 /* no match - add a new doms_new */
Rusty Russellacc3f5d2009-11-03 14:53:40 +10307381 __build_sched_domains(doms_new[i],
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09007382 dattr_new ? dattr_new + i : NULL);
Paul Jackson029190c2007-10-18 23:40:20 -07007383match2:
7384 ;
7385 }
7386
7387 /* Remember the new sched domains */
Rusty Russellacc3f5d2009-11-03 14:53:40 +10307388 if (doms_cur != &fallback_doms)
7389 free_sched_domains(doms_cur, ndoms_cur);
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09007390 kfree(dattr_cur); /* kfree(NULL) is safe */
Paul Jackson029190c2007-10-18 23:40:20 -07007391 doms_cur = doms_new;
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09007392 dattr_cur = dattr_new;
Paul Jackson029190c2007-10-18 23:40:20 -07007393 ndoms_cur = ndoms_new;
Milton Miller73785472007-10-24 18:23:48 +02007394
7395 register_sched_domain_sysctl();
Srivatsa Vaddagiria1835612008-01-25 21:08:00 +01007396
Heiko Carstens712555e2008-04-28 11:33:07 +02007397 mutex_unlock(&sched_domains_mutex);
Paul Jackson029190c2007-10-18 23:40:20 -07007398}
7399
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07007400#if defined(CONFIG_SCHED_MC) || defined(CONFIG_SCHED_SMT)
Li Zefanc70f22d2009-01-05 19:07:50 +08007401static void arch_reinit_sched_domains(void)
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07007402{
Gautham R Shenoy95402b32008-01-25 21:08:02 +01007403 get_online_cpus();
Max Krasnyanskydfb512e2008-08-29 13:11:41 -07007404
7405 /* Destroy domains first to force the rebuild */
7406 partition_sched_domains(0, NULL, NULL);
7407
Max Krasnyanskye761b772008-07-15 04:43:49 -07007408 rebuild_sched_domains();
Gautham R Shenoy95402b32008-01-25 21:08:02 +01007409 put_online_cpus();
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07007410}
7411
7412static ssize_t sched_power_savings_store(const char *buf, size_t count, int smt)
7413{
Gautham R Shenoyafb8a9b2008-12-18 23:26:09 +05307414 unsigned int level = 0;
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07007415
Gautham R Shenoyafb8a9b2008-12-18 23:26:09 +05307416 if (sscanf(buf, "%u", &level) != 1)
7417 return -EINVAL;
7418
7419 /*
7420 * level is always be positive so don't check for
7421 * level < POWERSAVINGS_BALANCE_NONE which is 0
7422 * What happens on 0 or 1 byte write,
7423 * need to check for count as well?
7424 */
7425
7426 if (level >= MAX_POWERSAVINGS_BALANCE_LEVELS)
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07007427 return -EINVAL;
7428
7429 if (smt)
Gautham R Shenoyafb8a9b2008-12-18 23:26:09 +05307430 sched_smt_power_savings = level;
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07007431 else
Gautham R Shenoyafb8a9b2008-12-18 23:26:09 +05307432 sched_mc_power_savings = level;
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07007433
Li Zefanc70f22d2009-01-05 19:07:50 +08007434 arch_reinit_sched_domains();
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07007435
Li Zefanc70f22d2009-01-05 19:07:50 +08007436 return count;
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07007437}
7438
Adrian Bunk6707de002007-08-12 18:08:19 +02007439#ifdef CONFIG_SCHED_MC
Andi Kleenf718cd42008-07-29 22:33:52 -07007440static ssize_t sched_mc_power_savings_show(struct sysdev_class *class,
Andi Kleenc9be0a32010-01-05 12:47:58 +01007441 struct sysdev_class_attribute *attr,
Andi Kleenf718cd42008-07-29 22:33:52 -07007442 char *page)
Adrian Bunk6707de002007-08-12 18:08:19 +02007443{
7444 return sprintf(page, "%u\n", sched_mc_power_savings);
7445}
Andi Kleenf718cd42008-07-29 22:33:52 -07007446static ssize_t sched_mc_power_savings_store(struct sysdev_class *class,
Andi Kleenc9be0a32010-01-05 12:47:58 +01007447 struct sysdev_class_attribute *attr,
Adrian Bunk6707de002007-08-12 18:08:19 +02007448 const char *buf, size_t count)
7449{
7450 return sched_power_savings_store(buf, count, 0);
7451}
Andi Kleenf718cd42008-07-29 22:33:52 -07007452static SYSDEV_CLASS_ATTR(sched_mc_power_savings, 0644,
7453 sched_mc_power_savings_show,
7454 sched_mc_power_savings_store);
Adrian Bunk6707de002007-08-12 18:08:19 +02007455#endif
7456
7457#ifdef CONFIG_SCHED_SMT
Andi Kleenf718cd42008-07-29 22:33:52 -07007458static ssize_t sched_smt_power_savings_show(struct sysdev_class *dev,
Andi Kleenc9be0a32010-01-05 12:47:58 +01007459 struct sysdev_class_attribute *attr,
Andi Kleenf718cd42008-07-29 22:33:52 -07007460 char *page)
Adrian Bunk6707de002007-08-12 18:08:19 +02007461{
7462 return sprintf(page, "%u\n", sched_smt_power_savings);
7463}
Andi Kleenf718cd42008-07-29 22:33:52 -07007464static ssize_t sched_smt_power_savings_store(struct sysdev_class *dev,
Andi Kleenc9be0a32010-01-05 12:47:58 +01007465 struct sysdev_class_attribute *attr,
Adrian Bunk6707de002007-08-12 18:08:19 +02007466 const char *buf, size_t count)
7467{
7468 return sched_power_savings_store(buf, count, 1);
7469}
Andi Kleenf718cd42008-07-29 22:33:52 -07007470static SYSDEV_CLASS_ATTR(sched_smt_power_savings, 0644,
7471 sched_smt_power_savings_show,
Adrian Bunk6707de002007-08-12 18:08:19 +02007472 sched_smt_power_savings_store);
7473#endif
7474
Li Zefan39aac642009-01-05 19:18:02 +08007475int __init sched_create_sysfs_power_savings_entries(struct sysdev_class *cls)
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07007476{
7477 int err = 0;
Ingo Molnar48f24c42006-07-03 00:25:40 -07007478
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07007479#ifdef CONFIG_SCHED_SMT
7480 if (smt_capable())
7481 err = sysfs_create_file(&cls->kset.kobj,
7482 &attr_sched_smt_power_savings.attr);
7483#endif
7484#ifdef CONFIG_SCHED_MC
7485 if (!err && mc_capable())
7486 err = sysfs_create_file(&cls->kset.kobj,
7487 &attr_sched_mc_power_savings.attr);
7488#endif
7489 return err;
7490}
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +02007491#endif /* CONFIG_SCHED_MC || CONFIG_SCHED_SMT */
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07007492
Linus Torvalds1da177e2005-04-16 15:20:36 -07007493/*
Tejun Heo3a101d02010-06-08 21:40:36 +02007494 * Update cpusets according to cpu_active mask. If cpusets are
7495 * disabled, cpuset_update_active_cpus() becomes a simple wrapper
7496 * around partition_sched_domains().
Linus Torvalds1da177e2005-04-16 15:20:36 -07007497 */
Tejun Heo0b2e9182010-06-21 23:53:31 +02007498static int cpuset_cpu_active(struct notifier_block *nfb, unsigned long action,
7499 void *hcpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007500{
Tejun Heo3a101d02010-06-08 21:40:36 +02007501 switch (action & ~CPU_TASKS_FROZEN) {
Max Krasnyanskye761b772008-07-15 04:43:49 -07007502 case CPU_ONLINE:
Peter Zijlstra6ad4c182009-11-25 13:31:39 +01007503 case CPU_DOWN_FAILED:
Tejun Heo3a101d02010-06-08 21:40:36 +02007504 cpuset_update_active_cpus();
Max Krasnyanskye761b772008-07-15 04:43:49 -07007505 return NOTIFY_OK;
Max Krasnyanskye761b772008-07-15 04:43:49 -07007506 default:
7507 return NOTIFY_DONE;
7508 }
7509}
Tejun Heo3a101d02010-06-08 21:40:36 +02007510
Tejun Heo0b2e9182010-06-21 23:53:31 +02007511static int cpuset_cpu_inactive(struct notifier_block *nfb, unsigned long action,
7512 void *hcpu)
Tejun Heo3a101d02010-06-08 21:40:36 +02007513{
7514 switch (action & ~CPU_TASKS_FROZEN) {
7515 case CPU_DOWN_PREPARE:
7516 cpuset_update_active_cpus();
7517 return NOTIFY_OK;
7518 default:
7519 return NOTIFY_DONE;
7520 }
7521}
Max Krasnyanskye761b772008-07-15 04:43:49 -07007522
7523static int update_runtime(struct notifier_block *nfb,
7524 unsigned long action, void *hcpu)
7525{
Peter Zijlstra7def2be2008-06-05 14:49:58 +02007526 int cpu = (int)(long)hcpu;
7527
Linus Torvalds1da177e2005-04-16 15:20:36 -07007528 switch (action) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007529 case CPU_DOWN_PREPARE:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07007530 case CPU_DOWN_PREPARE_FROZEN:
Peter Zijlstra7def2be2008-06-05 14:49:58 +02007531 disable_runtime(cpu_rq(cpu));
Linus Torvalds1da177e2005-04-16 15:20:36 -07007532 return NOTIFY_OK;
7533
Linus Torvalds1da177e2005-04-16 15:20:36 -07007534 case CPU_DOWN_FAILED:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07007535 case CPU_DOWN_FAILED_FROZEN:
Linus Torvalds1da177e2005-04-16 15:20:36 -07007536 case CPU_ONLINE:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07007537 case CPU_ONLINE_FROZEN:
Peter Zijlstra7def2be2008-06-05 14:49:58 +02007538 enable_runtime(cpu_rq(cpu));
Max Krasnyanskye761b772008-07-15 04:43:49 -07007539 return NOTIFY_OK;
7540
Linus Torvalds1da177e2005-04-16 15:20:36 -07007541 default:
7542 return NOTIFY_DONE;
7543 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007544}
Linus Torvalds1da177e2005-04-16 15:20:36 -07007545
7546void __init sched_init_smp(void)
7547{
Rusty Russelldcc30a32008-11-25 02:35:12 +10307548 cpumask_var_t non_isolated_cpus;
7549
7550 alloc_cpumask_var(&non_isolated_cpus, GFP_KERNEL);
Yong Zhangcb5fd132009-09-14 20:20:16 +08007551 alloc_cpumask_var(&fallback_doms, GFP_KERNEL);
Nick Piggin5c1e1762006-10-03 01:14:04 -07007552
Mike Travis434d53b2008-04-04 18:11:04 -07007553#if defined(CONFIG_NUMA)
7554 sched_group_nodes_bycpu = kzalloc(nr_cpu_ids * sizeof(void **),
7555 GFP_KERNEL);
7556 BUG_ON(sched_group_nodes_bycpu == NULL);
7557#endif
Gautham R Shenoy95402b32008-01-25 21:08:02 +01007558 get_online_cpus();
Heiko Carstens712555e2008-04-28 11:33:07 +02007559 mutex_lock(&sched_domains_mutex);
Peter Zijlstra6ad4c182009-11-25 13:31:39 +01007560 arch_init_sched_domains(cpu_active_mask);
Rusty Russelldcc30a32008-11-25 02:35:12 +10307561 cpumask_andnot(non_isolated_cpus, cpu_possible_mask, cpu_isolated_map);
7562 if (cpumask_empty(non_isolated_cpus))
7563 cpumask_set_cpu(smp_processor_id(), non_isolated_cpus);
Heiko Carstens712555e2008-04-28 11:33:07 +02007564 mutex_unlock(&sched_domains_mutex);
Gautham R Shenoy95402b32008-01-25 21:08:02 +01007565 put_online_cpus();
Max Krasnyanskye761b772008-07-15 04:43:49 -07007566
Tejun Heo3a101d02010-06-08 21:40:36 +02007567 hotcpu_notifier(cpuset_cpu_active, CPU_PRI_CPUSET_ACTIVE);
7568 hotcpu_notifier(cpuset_cpu_inactive, CPU_PRI_CPUSET_INACTIVE);
Max Krasnyanskye761b772008-07-15 04:43:49 -07007569
7570 /* RT runtime code needs to handle some hotplug events */
7571 hotcpu_notifier(update_runtime, 0);
7572
Peter Zijlstrab328ca12008-04-29 10:02:46 +02007573 init_hrtick();
Nick Piggin5c1e1762006-10-03 01:14:04 -07007574
7575 /* Move init over to a non-isolated CPU */
Rusty Russelldcc30a32008-11-25 02:35:12 +10307576 if (set_cpus_allowed_ptr(current, non_isolated_cpus) < 0)
Nick Piggin5c1e1762006-10-03 01:14:04 -07007577 BUG();
Ingo Molnar19978ca2007-11-09 22:39:38 +01007578 sched_init_granularity();
Rusty Russelldcc30a32008-11-25 02:35:12 +10307579 free_cpumask_var(non_isolated_cpus);
Rusty Russell42128232008-11-25 02:35:12 +10307580
Rusty Russell0e3900e2008-11-25 02:35:13 +10307581 init_sched_rt_class();
Linus Torvalds1da177e2005-04-16 15:20:36 -07007582}
7583#else
7584void __init sched_init_smp(void)
7585{
Ingo Molnar19978ca2007-11-09 22:39:38 +01007586 sched_init_granularity();
Linus Torvalds1da177e2005-04-16 15:20:36 -07007587}
7588#endif /* CONFIG_SMP */
7589
Arun R Bharadwajcd1bb942009-04-16 12:15:34 +05307590const_debug unsigned int sysctl_timer_migration = 1;
7591
Linus Torvalds1da177e2005-04-16 15:20:36 -07007592int in_sched_functions(unsigned long addr)
7593{
Linus Torvalds1da177e2005-04-16 15:20:36 -07007594 return in_lock_functions(addr) ||
7595 (addr >= (unsigned long)__sched_text_start
7596 && addr < (unsigned long)__sched_text_end);
7597}
7598
Alexey Dobriyana9957442007-10-15 17:00:13 +02007599static void init_cfs_rq(struct cfs_rq *cfs_rq, struct rq *rq)
Ingo Molnardd41f592007-07-09 18:51:59 +02007600{
7601 cfs_rq->tasks_timeline = RB_ROOT;
Peter Zijlstra4a55bd52008-04-19 19:45:00 +02007602 INIT_LIST_HEAD(&cfs_rq->tasks);
Ingo Molnardd41f592007-07-09 18:51:59 +02007603#ifdef CONFIG_FAIR_GROUP_SCHED
7604 cfs_rq->rq = rq;
7605#endif
Peter Zijlstra67e9fb22007-10-15 17:00:10 +02007606 cfs_rq->min_vruntime = (u64)(-(1LL << 20));
Ingo Molnardd41f592007-07-09 18:51:59 +02007607}
7608
Peter Zijlstrafa85ae22008-01-25 21:08:29 +01007609static void init_rt_rq(struct rt_rq *rt_rq, struct rq *rq)
7610{
7611 struct rt_prio_array *array;
7612 int i;
7613
7614 array = &rt_rq->active;
7615 for (i = 0; i < MAX_RT_PRIO; i++) {
7616 INIT_LIST_HEAD(array->queue + i);
7617 __clear_bit(i, array->bitmap);
7618 }
7619 /* delimiter for bitsearch: */
7620 __set_bit(MAX_RT_PRIO, array->bitmap);
7621
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01007622#if defined CONFIG_SMP || defined CONFIG_RT_GROUP_SCHED
Gregory Haskinse864c492008-12-29 09:39:49 -05007623 rt_rq->highest_prio.curr = MAX_RT_PRIO;
Gregory Haskins398a1532009-01-14 09:10:04 -05007624#ifdef CONFIG_SMP
Gregory Haskinse864c492008-12-29 09:39:49 -05007625 rt_rq->highest_prio.next = MAX_RT_PRIO;
Peter Zijlstra48d5e252008-01-25 21:08:31 +01007626#endif
Peter Zijlstrafa85ae22008-01-25 21:08:29 +01007627#endif
7628#ifdef CONFIG_SMP
7629 rt_rq->rt_nr_migratory = 0;
Peter Zijlstrafa85ae22008-01-25 21:08:29 +01007630 rt_rq->overloaded = 0;
Thomas Gleixner05fa7852009-11-17 14:28:38 +01007631 plist_head_init_raw(&rt_rq->pushable_tasks, &rq->lock);
Peter Zijlstrafa85ae22008-01-25 21:08:29 +01007632#endif
7633
7634 rt_rq->rt_time = 0;
7635 rt_rq->rt_throttled = 0;
Peter Zijlstraac086bc2008-04-19 19:44:58 +02007636 rt_rq->rt_runtime = 0;
Thomas Gleixner0986b112009-11-17 15:32:06 +01007637 raw_spin_lock_init(&rt_rq->rt_runtime_lock);
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007638
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01007639#ifdef CONFIG_RT_GROUP_SCHED
Peter Zijlstra23b0fdf2008-02-13 15:45:39 +01007640 rt_rq->rt_nr_boosted = 0;
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007641 rt_rq->rq = rq;
7642#endif
Peter Zijlstrafa85ae22008-01-25 21:08:29 +01007643}
7644
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007645#ifdef CONFIG_FAIR_GROUP_SCHED
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02007646static void init_tg_cfs_entry(struct task_group *tg, struct cfs_rq *cfs_rq,
Peter Zijlstra3d4b47b2010-11-15 15:47:01 -08007647 struct sched_entity *se, int cpu,
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02007648 struct sched_entity *parent)
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007649{
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02007650 struct rq *rq = cpu_rq(cpu);
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007651 tg->cfs_rq[cpu] = cfs_rq;
7652 init_cfs_rq(cfs_rq, rq);
7653 cfs_rq->tg = tg;
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007654
7655 tg->se[cpu] = se;
Dhaval Giani354d60c2008-04-19 19:44:59 +02007656 /* se could be NULL for init_task_group */
7657 if (!se)
7658 return;
7659
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02007660 if (!parent)
7661 se->cfs_rq = &rq->cfs;
7662 else
7663 se->cfs_rq = parent->my_q;
7664
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007665 se->my_q = cfs_rq;
Paul Turner94371782010-11-15 15:47:10 -08007666 update_load_set(&se->load, 0);
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02007667 se->parent = parent;
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007668}
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01007669#endif
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007670
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01007671#ifdef CONFIG_RT_GROUP_SCHED
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02007672static void init_tg_rt_entry(struct task_group *tg, struct rt_rq *rt_rq,
Peter Zijlstra3d4b47b2010-11-15 15:47:01 -08007673 struct sched_rt_entity *rt_se, int cpu,
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02007674 struct sched_rt_entity *parent)
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007675{
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02007676 struct rq *rq = cpu_rq(cpu);
7677
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007678 tg->rt_rq[cpu] = rt_rq;
7679 init_rt_rq(rt_rq, rq);
7680 rt_rq->tg = tg;
Peter Zijlstraac086bc2008-04-19 19:44:58 +02007681 rt_rq->rt_runtime = tg->rt_bandwidth.rt_runtime;
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007682
7683 tg->rt_se[cpu] = rt_se;
Dhaval Giani354d60c2008-04-19 19:44:59 +02007684 if (!rt_se)
7685 return;
7686
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02007687 if (!parent)
7688 rt_se->rt_rq = &rq->rt;
7689 else
7690 rt_se->rt_rq = parent->my_q;
7691
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007692 rt_se->my_q = rt_rq;
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02007693 rt_se->parent = parent;
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007694 INIT_LIST_HEAD(&rt_se->run_list);
7695}
7696#endif
7697
Linus Torvalds1da177e2005-04-16 15:20:36 -07007698void __init sched_init(void)
7699{
Ingo Molnardd41f592007-07-09 18:51:59 +02007700 int i, j;
Mike Travis434d53b2008-04-04 18:11:04 -07007701 unsigned long alloc_size = 0, ptr;
7702
7703#ifdef CONFIG_FAIR_GROUP_SCHED
7704 alloc_size += 2 * nr_cpu_ids * sizeof(void **);
7705#endif
7706#ifdef CONFIG_RT_GROUP_SCHED
7707 alloc_size += 2 * nr_cpu_ids * sizeof(void **);
7708#endif
Rusty Russelldf7c8e82009-03-19 15:22:20 +10307709#ifdef CONFIG_CPUMASK_OFFSTACK
Rusty Russell8c083f02009-03-19 15:22:20 +10307710 alloc_size += num_possible_cpus() * cpumask_size();
Rusty Russelldf7c8e82009-03-19 15:22:20 +10307711#endif
Mike Travis434d53b2008-04-04 18:11:04 -07007712 if (alloc_size) {
Pekka Enberg36b7b6d2009-06-10 23:42:36 +03007713 ptr = (unsigned long)kzalloc(alloc_size, GFP_NOWAIT);
Mike Travis434d53b2008-04-04 18:11:04 -07007714
7715#ifdef CONFIG_FAIR_GROUP_SCHED
7716 init_task_group.se = (struct sched_entity **)ptr;
7717 ptr += nr_cpu_ids * sizeof(void **);
7718
7719 init_task_group.cfs_rq = (struct cfs_rq **)ptr;
7720 ptr += nr_cpu_ids * sizeof(void **);
Peter Zijlstraeff766a2008-04-19 19:45:00 +02007721
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +02007722#endif /* CONFIG_FAIR_GROUP_SCHED */
Mike Travis434d53b2008-04-04 18:11:04 -07007723#ifdef CONFIG_RT_GROUP_SCHED
7724 init_task_group.rt_se = (struct sched_rt_entity **)ptr;
7725 ptr += nr_cpu_ids * sizeof(void **);
7726
7727 init_task_group.rt_rq = (struct rt_rq **)ptr;
Peter Zijlstraeff766a2008-04-19 19:45:00 +02007728 ptr += nr_cpu_ids * sizeof(void **);
7729
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +02007730#endif /* CONFIG_RT_GROUP_SCHED */
Rusty Russelldf7c8e82009-03-19 15:22:20 +10307731#ifdef CONFIG_CPUMASK_OFFSTACK
7732 for_each_possible_cpu(i) {
7733 per_cpu(load_balance_tmpmask, i) = (void *)ptr;
7734 ptr += cpumask_size();
7735 }
7736#endif /* CONFIG_CPUMASK_OFFSTACK */
Mike Travis434d53b2008-04-04 18:11:04 -07007737 }
Ingo Molnardd41f592007-07-09 18:51:59 +02007738
Gregory Haskins57d885f2008-01-25 21:08:18 +01007739#ifdef CONFIG_SMP
7740 init_defrootdomain();
7741#endif
7742
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02007743 init_rt_bandwidth(&def_rt_bandwidth,
7744 global_rt_period(), global_rt_runtime());
7745
7746#ifdef CONFIG_RT_GROUP_SCHED
7747 init_rt_bandwidth(&init_task_group.rt_bandwidth,
7748 global_rt_period(), global_rt_runtime());
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +02007749#endif /* CONFIG_RT_GROUP_SCHED */
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02007750
Dhaval Giani7c941432010-01-20 13:26:18 +01007751#ifdef CONFIG_CGROUP_SCHED
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007752 list_add(&init_task_group.list, &task_groups);
Peter Zijlstraf473aa52008-04-19 19:45:00 +02007753 INIT_LIST_HEAD(&init_task_group.children);
7754
Dhaval Giani7c941432010-01-20 13:26:18 +01007755#endif /* CONFIG_CGROUP_SCHED */
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007756
KAMEZAWA Hiroyuki0a945022006-03-28 01:56:37 -08007757 for_each_possible_cpu(i) {
Ingo Molnar70b97a72006-07-03 00:25:42 -07007758 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007759
7760 rq = cpu_rq(i);
Thomas Gleixner05fa7852009-11-17 14:28:38 +01007761 raw_spin_lock_init(&rq->lock);
Nick Piggin78979862005-06-25 14:57:13 -07007762 rq->nr_running = 0;
Thomas Gleixnerdce48a82009-04-11 10:43:41 +02007763 rq->calc_load_active = 0;
7764 rq->calc_load_update = jiffies + LOAD_FREQ;
Ingo Molnardd41f592007-07-09 18:51:59 +02007765 init_cfs_rq(&rq->cfs, rq);
Peter Zijlstrafa85ae22008-01-25 21:08:29 +01007766 init_rt_rq(&rq->rt, rq);
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007767#ifdef CONFIG_FAIR_GROUP_SCHED
7768 init_task_group.shares = init_task_group_load;
7769 INIT_LIST_HEAD(&rq->leaf_cfs_rq_list);
Dhaval Giani354d60c2008-04-19 19:44:59 +02007770#ifdef CONFIG_CGROUP_SCHED
7771 /*
7772 * How much cpu bandwidth does init_task_group get?
7773 *
7774 * In case of task-groups formed thr' the cgroup filesystem, it
7775 * gets 100% of the cpu resources in the system. This overall
7776 * system cpu resource is divided among the tasks of
7777 * init_task_group and its child task-groups in a fair manner,
7778 * based on each entity's (task or task-group's) weight
7779 * (se->load.weight).
7780 *
7781 * In other words, if init_task_group has 10 tasks of weight
7782 * 1024) and two child groups A0 and A1 (of weight 1024 each),
7783 * then A0's share of the cpu resource is:
7784 *
Ingo Molnar0d905bc2009-05-04 19:13:30 +02007785 * A0's bandwidth = 1024 / (10*1024 + 1024 + 1024) = 8.33%
Dhaval Giani354d60c2008-04-19 19:44:59 +02007786 *
7787 * We achieve this by letting init_task_group's tasks sit
7788 * directly in rq->cfs (i.e init_task_group->se[] = NULL).
7789 */
Peter Zijlstra3d4b47b2010-11-15 15:47:01 -08007790 init_tg_cfs_entry(&init_task_group, &rq->cfs, NULL, i, NULL);
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01007791#endif
Dhaval Giani354d60c2008-04-19 19:44:59 +02007792#endif /* CONFIG_FAIR_GROUP_SCHED */
7793
7794 rq->rt.rt_runtime = def_rt_bandwidth.rt_runtime;
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01007795#ifdef CONFIG_RT_GROUP_SCHED
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007796 INIT_LIST_HEAD(&rq->leaf_rt_rq_list);
Dhaval Giani354d60c2008-04-19 19:44:59 +02007797#ifdef CONFIG_CGROUP_SCHED
Peter Zijlstra3d4b47b2010-11-15 15:47:01 -08007798 init_tg_rt_entry(&init_task_group, &rq->rt, NULL, i, NULL);
Dhaval Giani354d60c2008-04-19 19:44:59 +02007799#endif
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007800#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07007801
Ingo Molnardd41f592007-07-09 18:51:59 +02007802 for (j = 0; j < CPU_LOAD_IDX_MAX; j++)
7803 rq->cpu_load[j] = 0;
Venkatesh Pallipadifdf3e952010-05-17 18:14:43 -07007804
7805 rq->last_load_update_tick = jiffies;
7806
Linus Torvalds1da177e2005-04-16 15:20:36 -07007807#ifdef CONFIG_SMP
Nick Piggin41c7ce92005-06-25 14:57:24 -07007808 rq->sd = NULL;
Gregory Haskins57d885f2008-01-25 21:08:18 +01007809 rq->rd = NULL;
Peter Zijlstrae51fd5e2010-05-31 12:37:30 +02007810 rq->cpu_power = SCHED_LOAD_SCALE;
Gregory Haskins3f029d32009-07-29 11:08:47 -04007811 rq->post_schedule = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007812 rq->active_balance = 0;
Ingo Molnardd41f592007-07-09 18:51:59 +02007813 rq->next_balance = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007814 rq->push_cpu = 0;
Christoph Lameter0a2966b2006-09-25 23:30:51 -07007815 rq->cpu = i;
Gregory Haskins1f11eb62008-06-04 15:04:05 -04007816 rq->online = 0;
Mike Galbraitheae0c9d2009-11-10 03:50:02 +01007817 rq->idle_stamp = 0;
7818 rq->avg_idle = 2*sysctl_sched_migration_cost;
Gregory Haskinsdc938522008-01-25 21:08:26 +01007819 rq_attach_root(rq, &def_root_domain);
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07007820#ifdef CONFIG_NO_HZ
7821 rq->nohz_balance_kick = 0;
7822 init_sched_softirq_csd(&per_cpu(remote_sched_softirq_cb, i));
7823#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07007824#endif
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01007825 init_rq_hrtick(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007826 atomic_set(&rq->nr_iowait, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007827 }
7828
Peter Williams2dd73a42006-06-27 02:54:34 -07007829 set_load_weight(&init_task);
Heiko Carstensb50f60c2006-07-30 03:03:52 -07007830
Avi Kivitye107be32007-07-26 13:40:43 +02007831#ifdef CONFIG_PREEMPT_NOTIFIERS
7832 INIT_HLIST_HEAD(&init_task.preempt_notifiers);
7833#endif
7834
Christoph Lameterc9819f42006-12-10 02:20:25 -08007835#ifdef CONFIG_SMP
Carlos R. Mafra962cf362008-05-15 11:15:37 -03007836 open_softirq(SCHED_SOFTIRQ, run_rebalance_domains);
Christoph Lameterc9819f42006-12-10 02:20:25 -08007837#endif
7838
Heiko Carstensb50f60c2006-07-30 03:03:52 -07007839#ifdef CONFIG_RT_MUTEXES
Thomas Gleixner1d615482009-11-17 14:54:03 +01007840 plist_head_init_raw(&init_task.pi_waiters, &init_task.pi_lock);
Heiko Carstensb50f60c2006-07-30 03:03:52 -07007841#endif
7842
Linus Torvalds1da177e2005-04-16 15:20:36 -07007843 /*
7844 * The boot idle thread does lazy MMU switching as well:
7845 */
7846 atomic_inc(&init_mm.mm_count);
7847 enter_lazy_tlb(&init_mm, current);
7848
7849 /*
7850 * Make us the idle thread. Technically, schedule() should not be
7851 * called from this thread, however somewhere below it might be,
7852 * but because we are the idle thread, we just pick up running again
7853 * when this runqueue becomes "idle".
7854 */
7855 init_idle(current, smp_processor_id());
Thomas Gleixnerdce48a82009-04-11 10:43:41 +02007856
7857 calc_load_update = jiffies + LOAD_FREQ;
7858
Ingo Molnardd41f592007-07-09 18:51:59 +02007859 /*
7860 * During early bootup we pretend to be a normal task:
7861 */
7862 current->sched_class = &fair_sched_class;
Ingo Molnar6892b752008-02-13 14:02:36 +01007863
Rusty Russell6a7b3dc2008-11-25 02:35:04 +10307864 /* Allocate the nohz_cpu_mask if CONFIG_CPUMASK_OFFSTACK */
Rusty Russell49557e62009-11-02 20:37:20 +10307865 zalloc_cpumask_var(&nohz_cpu_mask, GFP_NOWAIT);
Rusty Russellbf4d83f2008-11-25 09:57:51 +10307866#ifdef CONFIG_SMP
Rusty Russell7d1e6a92008-11-25 02:35:09 +10307867#ifdef CONFIG_NO_HZ
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07007868 zalloc_cpumask_var(&nohz.idle_cpus_mask, GFP_NOWAIT);
7869 alloc_cpumask_var(&nohz.grp_idle_mask, GFP_NOWAIT);
7870 atomic_set(&nohz.load_balancer, nr_cpu_ids);
7871 atomic_set(&nohz.first_pick_cpu, nr_cpu_ids);
7872 atomic_set(&nohz.second_pick_cpu, nr_cpu_ids);
Rusty Russell7d1e6a92008-11-25 02:35:09 +10307873#endif
Rusty Russellbdddd292009-12-02 14:09:16 +10307874 /* May be allocated at isolcpus cmdline parse time */
7875 if (cpu_isolated_map == NULL)
7876 zalloc_cpumask_var(&cpu_isolated_map, GFP_NOWAIT);
Rusty Russellbf4d83f2008-11-25 09:57:51 +10307877#endif /* SMP */
Rusty Russell6a7b3dc2008-11-25 02:35:04 +10307878
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007879 perf_event_init();
Ingo Molnar0d905bc2009-05-04 19:13:30 +02007880
Ingo Molnar6892b752008-02-13 14:02:36 +01007881 scheduler_running = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007882}
7883
7884#ifdef CONFIG_DEBUG_SPINLOCK_SLEEP
Frederic Weisbeckere4aafea2009-07-16 15:44:29 +02007885static inline int preempt_count_equals(int preempt_offset)
7886{
Frederic Weisbecker234da7b2009-12-16 20:21:05 +01007887 int nested = (preempt_count() & ~PREEMPT_ACTIVE) + rcu_preempt_depth();
Frederic Weisbeckere4aafea2009-07-16 15:44:29 +02007888
7889 return (nested == PREEMPT_INATOMIC_BASE + preempt_offset);
7890}
7891
Simon Kagstromd8948372009-12-23 11:08:18 +01007892void __might_sleep(const char *file, int line, int preempt_offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007893{
Ingo Molnar48f24c42006-07-03 00:25:40 -07007894#ifdef in_atomic
Linus Torvalds1da177e2005-04-16 15:20:36 -07007895 static unsigned long prev_jiffy; /* ratelimiting */
7896
Frederic Weisbeckere4aafea2009-07-16 15:44:29 +02007897 if ((preempt_count_equals(preempt_offset) && !irqs_disabled()) ||
7898 system_state != SYSTEM_RUNNING || oops_in_progress)
Ingo Molnaraef745f2008-08-28 11:34:43 +02007899 return;
7900 if (time_before(jiffies, prev_jiffy + HZ) && prev_jiffy)
7901 return;
7902 prev_jiffy = jiffies;
7903
Peter Zijlstra3df0fc52009-12-20 14:23:57 +01007904 printk(KERN_ERR
7905 "BUG: sleeping function called from invalid context at %s:%d\n",
7906 file, line);
7907 printk(KERN_ERR
7908 "in_atomic(): %d, irqs_disabled(): %d, pid: %d, name: %s\n",
7909 in_atomic(), irqs_disabled(),
7910 current->pid, current->comm);
Ingo Molnaraef745f2008-08-28 11:34:43 +02007911
7912 debug_show_held_locks(current);
7913 if (irqs_disabled())
7914 print_irqtrace_events(current);
7915 dump_stack();
Linus Torvalds1da177e2005-04-16 15:20:36 -07007916#endif
7917}
7918EXPORT_SYMBOL(__might_sleep);
7919#endif
7920
7921#ifdef CONFIG_MAGIC_SYSRQ
Andi Kleen3a5e4dc2007-10-15 17:00:15 +02007922static void normalize_task(struct rq *rq, struct task_struct *p)
7923{
7924 int on_rq;
Peter Zijlstra3e51f332008-05-03 18:29:28 +02007925
Andi Kleen3a5e4dc2007-10-15 17:00:15 +02007926 on_rq = p->se.on_rq;
7927 if (on_rq)
7928 deactivate_task(rq, p, 0);
7929 __setscheduler(rq, p, SCHED_NORMAL, 0);
7930 if (on_rq) {
7931 activate_task(rq, p, 0);
7932 resched_task(rq->curr);
7933 }
7934}
7935
Linus Torvalds1da177e2005-04-16 15:20:36 -07007936void normalize_rt_tasks(void)
7937{
Ingo Molnara0f98a12007-06-17 18:37:45 +02007938 struct task_struct *g, *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007939 unsigned long flags;
Ingo Molnar70b97a72006-07-03 00:25:42 -07007940 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007941
Peter Zijlstra4cf5d772008-02-13 15:45:39 +01007942 read_lock_irqsave(&tasklist_lock, flags);
Ingo Molnara0f98a12007-06-17 18:37:45 +02007943 do_each_thread(g, p) {
Ingo Molnar178be792007-10-15 17:00:18 +02007944 /*
7945 * Only normalize user tasks:
7946 */
7947 if (!p->mm)
7948 continue;
7949
Ingo Molnardd41f592007-07-09 18:51:59 +02007950 p->se.exec_start = 0;
Ingo Molnar6cfb0d52007-08-02 17:41:40 +02007951#ifdef CONFIG_SCHEDSTATS
Lucas De Marchi41acab82010-03-10 23:37:45 -03007952 p->se.statistics.wait_start = 0;
7953 p->se.statistics.sleep_start = 0;
7954 p->se.statistics.block_start = 0;
Ingo Molnar6cfb0d52007-08-02 17:41:40 +02007955#endif
Ingo Molnardd41f592007-07-09 18:51:59 +02007956
7957 if (!rt_task(p)) {
7958 /*
7959 * Renice negative nice level userspace
7960 * tasks back to 0:
7961 */
7962 if (TASK_NICE(p) < 0 && p->mm)
7963 set_user_nice(p, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007964 continue;
Ingo Molnardd41f592007-07-09 18:51:59 +02007965 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007966
Thomas Gleixner1d615482009-11-17 14:54:03 +01007967 raw_spin_lock(&p->pi_lock);
Ingo Molnarb29739f2006-06-27 02:54:51 -07007968 rq = __task_rq_lock(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007969
Ingo Molnar178be792007-10-15 17:00:18 +02007970 normalize_task(rq, p);
Andi Kleen3a5e4dc2007-10-15 17:00:15 +02007971
Ingo Molnarb29739f2006-06-27 02:54:51 -07007972 __task_rq_unlock(rq);
Thomas Gleixner1d615482009-11-17 14:54:03 +01007973 raw_spin_unlock(&p->pi_lock);
Ingo Molnara0f98a12007-06-17 18:37:45 +02007974 } while_each_thread(g, p);
7975
Peter Zijlstra4cf5d772008-02-13 15:45:39 +01007976 read_unlock_irqrestore(&tasklist_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007977}
7978
7979#endif /* CONFIG_MAGIC_SYSRQ */
Linus Torvalds1df5c102005-09-12 07:59:21 -07007980
Jason Wessel67fc4e02010-05-20 21:04:21 -05007981#if defined(CONFIG_IA64) || defined(CONFIG_KGDB_KDB)
Linus Torvalds1df5c102005-09-12 07:59:21 -07007982/*
Jason Wessel67fc4e02010-05-20 21:04:21 -05007983 * These functions are only useful for the IA64 MCA handling, or kdb.
Linus Torvalds1df5c102005-09-12 07:59:21 -07007984 *
7985 * They can only be called when the whole system has been
7986 * stopped - every CPU needs to be quiescent, and no scheduling
7987 * activity can take place. Using them for anything else would
7988 * be a serious bug, and as a result, they aren't even visible
7989 * under any other configuration.
7990 */
7991
7992/**
7993 * curr_task - return the current task for a given cpu.
7994 * @cpu: the processor in question.
7995 *
7996 * ONLY VALID WHEN THE WHOLE SYSTEM IS STOPPED!
7997 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07007998struct task_struct *curr_task(int cpu)
Linus Torvalds1df5c102005-09-12 07:59:21 -07007999{
8000 return cpu_curr(cpu);
8001}
8002
Jason Wessel67fc4e02010-05-20 21:04:21 -05008003#endif /* defined(CONFIG_IA64) || defined(CONFIG_KGDB_KDB) */
8004
8005#ifdef CONFIG_IA64
Linus Torvalds1df5c102005-09-12 07:59:21 -07008006/**
8007 * set_curr_task - set the current task for a given cpu.
8008 * @cpu: the processor in question.
8009 * @p: the task pointer to set.
8010 *
8011 * Description: This function must only be used when non-maskable interrupts
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01008012 * are serviced on a separate stack. It allows the architecture to switch the
8013 * notion of the current task on a cpu in a non-blocking manner. This function
Linus Torvalds1df5c102005-09-12 07:59:21 -07008014 * must be called with all CPU's synchronized, and interrupts disabled, the
8015 * and caller must save the original value of the current task (see
8016 * curr_task() above) and restore that value before reenabling interrupts and
8017 * re-starting the system.
8018 *
8019 * ONLY VALID WHEN THE WHOLE SYSTEM IS STOPPED!
8020 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07008021void set_curr_task(int cpu, struct task_struct *p)
Linus Torvalds1df5c102005-09-12 07:59:21 -07008022{
8023 cpu_curr(cpu) = p;
8024}
8025
8026#endif
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008027
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008028#ifdef CONFIG_FAIR_GROUP_SCHED
8029static void free_fair_sched_group(struct task_group *tg)
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008030{
8031 int i;
8032
8033 for_each_possible_cpu(i) {
8034 if (tg->cfs_rq)
8035 kfree(tg->cfs_rq[i]);
8036 if (tg->se)
8037 kfree(tg->se[i]);
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008038 }
8039
8040 kfree(tg->cfs_rq);
8041 kfree(tg->se);
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008042}
8043
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008044static
8045int alloc_fair_sched_group(struct task_group *tg, struct task_group *parent)
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008046{
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008047 struct cfs_rq *cfs_rq;
Li Zefaneab17222008-10-29 17:03:22 +08008048 struct sched_entity *se;
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02008049 struct rq *rq;
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008050 int i;
8051
Mike Travis434d53b2008-04-04 18:11:04 -07008052 tg->cfs_rq = kzalloc(sizeof(cfs_rq) * nr_cpu_ids, GFP_KERNEL);
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008053 if (!tg->cfs_rq)
8054 goto err;
Mike Travis434d53b2008-04-04 18:11:04 -07008055 tg->se = kzalloc(sizeof(se) * nr_cpu_ids, GFP_KERNEL);
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008056 if (!tg->se)
8057 goto err;
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01008058
8059 tg->shares = NICE_0_LOAD;
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008060
8061 for_each_possible_cpu(i) {
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02008062 rq = cpu_rq(i);
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008063
Li Zefaneab17222008-10-29 17:03:22 +08008064 cfs_rq = kzalloc_node(sizeof(struct cfs_rq),
8065 GFP_KERNEL, cpu_to_node(i));
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008066 if (!cfs_rq)
8067 goto err;
8068
Li Zefaneab17222008-10-29 17:03:22 +08008069 se = kzalloc_node(sizeof(struct sched_entity),
8070 GFP_KERNEL, cpu_to_node(i));
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008071 if (!se)
Phil Carmodydfc12eb2009-12-10 14:29:37 +02008072 goto err_free_rq;
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008073
Peter Zijlstra3d4b47b2010-11-15 15:47:01 -08008074 init_tg_cfs_entry(tg, cfs_rq, se, i, parent->se[i]);
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008075 }
8076
8077 return 1;
8078
Peter Zijlstra49246272010-10-17 21:46:10 +02008079err_free_rq:
Phil Carmodydfc12eb2009-12-10 14:29:37 +02008080 kfree(cfs_rq);
Peter Zijlstra49246272010-10-17 21:46:10 +02008081err:
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008082 return 0;
8083}
8084
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008085static inline void unregister_fair_sched_group(struct task_group *tg, int cpu)
8086{
Peter Zijlstra3d4b47b2010-11-15 15:47:01 -08008087 struct rq *rq = cpu_rq(cpu);
8088 unsigned long flags;
8089 int i;
8090
8091 /*
8092 * Only empty task groups can be destroyed; so we can speculatively
8093 * check on_list without danger of it being re-added.
8094 */
8095 if (!tg->cfs_rq[cpu]->on_list)
8096 return;
8097
8098 raw_spin_lock_irqsave(&rq->lock, flags);
8099 list_del_leaf_cfs_rq(tg->cfs_rq[i]);
8100 raw_spin_unlock_irqrestore(&rq->lock, flags);
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008101}
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +02008102#else /* !CONFG_FAIR_GROUP_SCHED */
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008103static inline void free_fair_sched_group(struct task_group *tg)
8104{
8105}
8106
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008107static inline
8108int alloc_fair_sched_group(struct task_group *tg, struct task_group *parent)
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008109{
8110 return 1;
8111}
8112
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008113static inline void unregister_fair_sched_group(struct task_group *tg, int cpu)
8114{
8115}
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +02008116#endif /* CONFIG_FAIR_GROUP_SCHED */
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01008117
8118#ifdef CONFIG_RT_GROUP_SCHED
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008119static void free_rt_sched_group(struct task_group *tg)
8120{
8121 int i;
8122
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008123 destroy_rt_bandwidth(&tg->rt_bandwidth);
8124
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008125 for_each_possible_cpu(i) {
8126 if (tg->rt_rq)
8127 kfree(tg->rt_rq[i]);
8128 if (tg->rt_se)
8129 kfree(tg->rt_se[i]);
8130 }
8131
8132 kfree(tg->rt_rq);
8133 kfree(tg->rt_se);
8134}
8135
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008136static
8137int alloc_rt_sched_group(struct task_group *tg, struct task_group *parent)
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008138{
8139 struct rt_rq *rt_rq;
Li Zefaneab17222008-10-29 17:03:22 +08008140 struct sched_rt_entity *rt_se;
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008141 struct rq *rq;
8142 int i;
8143
Mike Travis434d53b2008-04-04 18:11:04 -07008144 tg->rt_rq = kzalloc(sizeof(rt_rq) * nr_cpu_ids, GFP_KERNEL);
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008145 if (!tg->rt_rq)
8146 goto err;
Mike Travis434d53b2008-04-04 18:11:04 -07008147 tg->rt_se = kzalloc(sizeof(rt_se) * nr_cpu_ids, GFP_KERNEL);
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008148 if (!tg->rt_se)
8149 goto err;
8150
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008151 init_rt_bandwidth(&tg->rt_bandwidth,
8152 ktime_to_ns(def_rt_bandwidth.rt_period), 0);
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008153
8154 for_each_possible_cpu(i) {
8155 rq = cpu_rq(i);
8156
Li Zefaneab17222008-10-29 17:03:22 +08008157 rt_rq = kzalloc_node(sizeof(struct rt_rq),
8158 GFP_KERNEL, cpu_to_node(i));
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008159 if (!rt_rq)
8160 goto err;
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008161
Li Zefaneab17222008-10-29 17:03:22 +08008162 rt_se = kzalloc_node(sizeof(struct sched_rt_entity),
8163 GFP_KERNEL, cpu_to_node(i));
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008164 if (!rt_se)
Phil Carmodydfc12eb2009-12-10 14:29:37 +02008165 goto err_free_rq;
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008166
Peter Zijlstra3d4b47b2010-11-15 15:47:01 -08008167 init_tg_rt_entry(tg, rt_rq, rt_se, i, parent->rt_se[i]);
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008168 }
8169
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008170 return 1;
8171
Peter Zijlstra49246272010-10-17 21:46:10 +02008172err_free_rq:
Phil Carmodydfc12eb2009-12-10 14:29:37 +02008173 kfree(rt_rq);
Peter Zijlstra49246272010-10-17 21:46:10 +02008174err:
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008175 return 0;
8176}
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +02008177#else /* !CONFIG_RT_GROUP_SCHED */
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008178static inline void free_rt_sched_group(struct task_group *tg)
8179{
8180}
8181
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008182static inline
8183int alloc_rt_sched_group(struct task_group *tg, struct task_group *parent)
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008184{
8185 return 1;
8186}
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +02008187#endif /* CONFIG_RT_GROUP_SCHED */
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008188
Dhaval Giani7c941432010-01-20 13:26:18 +01008189#ifdef CONFIG_CGROUP_SCHED
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008190static void free_sched_group(struct task_group *tg)
8191{
8192 free_fair_sched_group(tg);
8193 free_rt_sched_group(tg);
8194 kfree(tg);
8195}
8196
8197/* allocate runqueue etc for a new task group */
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008198struct task_group *sched_create_group(struct task_group *parent)
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008199{
8200 struct task_group *tg;
8201 unsigned long flags;
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008202
8203 tg = kzalloc(sizeof(*tg), GFP_KERNEL);
8204 if (!tg)
8205 return ERR_PTR(-ENOMEM);
8206
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008207 if (!alloc_fair_sched_group(tg, parent))
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008208 goto err;
8209
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008210 if (!alloc_rt_sched_group(tg, parent))
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008211 goto err;
8212
Peter Zijlstra8ed36992008-02-13 15:45:39 +01008213 spin_lock_irqsave(&task_group_lock, flags);
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008214 list_add_rcu(&tg->list, &task_groups);
Peter Zijlstraf473aa52008-04-19 19:45:00 +02008215
8216 WARN_ON(!parent); /* root should already exist */
8217
8218 tg->parent = parent;
Peter Zijlstraf473aa52008-04-19 19:45:00 +02008219 INIT_LIST_HEAD(&tg->children);
Zhang, Yanmin09f27242030-08-14 15:56:40 +08008220 list_add_rcu(&tg->siblings, &parent->children);
Peter Zijlstra8ed36992008-02-13 15:45:39 +01008221 spin_unlock_irqrestore(&task_group_lock, flags);
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008222
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02008223 return tg;
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008224
8225err:
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008226 free_sched_group(tg);
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008227 return ERR_PTR(-ENOMEM);
8228}
8229
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02008230/* rcu callback to free various structures associated with a task group */
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008231static void free_sched_group_rcu(struct rcu_head *rhp)
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008232{
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008233 /* now it should be safe to free those cfs_rqs */
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008234 free_sched_group(container_of(rhp, struct task_group, rcu));
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008235}
8236
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02008237/* Destroy runqueue etc associated with a task group */
Ingo Molnar4cf86d72007-10-15 17:00:14 +02008238void sched_destroy_group(struct task_group *tg)
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008239{
Peter Zijlstra8ed36992008-02-13 15:45:39 +01008240 unsigned long flags;
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02008241 int i;
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008242
Peter Zijlstra3d4b47b2010-11-15 15:47:01 -08008243 /* end participation in shares distribution */
8244 for_each_possible_cpu(i)
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008245 unregister_fair_sched_group(tg, i);
Peter Zijlstra3d4b47b2010-11-15 15:47:01 -08008246
8247 spin_lock_irqsave(&task_group_lock, flags);
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008248 list_del_rcu(&tg->list);
Peter Zijlstraf473aa52008-04-19 19:45:00 +02008249 list_del_rcu(&tg->siblings);
Peter Zijlstra8ed36992008-02-13 15:45:39 +01008250 spin_unlock_irqrestore(&task_group_lock, flags);
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02008251
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02008252 /* wait for possible concurrent references to cfs_rqs complete */
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008253 call_rcu(&tg->rcu, free_sched_group_rcu);
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008254}
8255
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02008256/* change task's runqueue when it moves between groups.
Ingo Molnar3a252012007-10-15 17:00:12 +02008257 * The caller of this function should have put the task in its new group
8258 * by now. This function just updates tsk->se.cfs_rq and tsk->se.parent to
8259 * reflect its new group.
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02008260 */
8261void sched_move_task(struct task_struct *tsk)
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008262{
8263 int on_rq, running;
8264 unsigned long flags;
8265 struct rq *rq;
8266
8267 rq = task_rq_lock(tsk, &flags);
8268
Dmitry Adamushko051a1d12007-12-18 15:21:13 +01008269 running = task_current(rq, tsk);
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008270 on_rq = tsk->se.on_rq;
8271
Hiroshi Shimamoto0e1f3482008-03-10 11:01:20 -07008272 if (on_rq)
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008273 dequeue_task(rq, tsk, 0);
Hiroshi Shimamoto0e1f3482008-03-10 11:01:20 -07008274 if (unlikely(running))
8275 tsk->sched_class->put_prev_task(rq, tsk);
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008276
Peter Zijlstra810b3812008-02-29 15:21:01 -05008277#ifdef CONFIG_FAIR_GROUP_SCHED
Peter Zijlstrab2b5ce02010-10-15 15:24:15 +02008278 if (tsk->sched_class->task_move_group)
8279 tsk->sched_class->task_move_group(tsk, on_rq);
8280 else
Peter Zijlstra810b3812008-02-29 15:21:01 -05008281#endif
Peter Zijlstrab2b5ce02010-10-15 15:24:15 +02008282 set_task_rq(tsk, task_cpu(tsk));
Peter Zijlstra810b3812008-02-29 15:21:01 -05008283
Hiroshi Shimamoto0e1f3482008-03-10 11:01:20 -07008284 if (unlikely(running))
8285 tsk->sched_class->set_curr_task(rq);
8286 if (on_rq)
Peter Zijlstra371fd7e2010-03-24 16:38:48 +01008287 enqueue_task(rq, tsk, 0);
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008288
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008289 task_rq_unlock(rq, &flags);
8290}
Dhaval Giani7c941432010-01-20 13:26:18 +01008291#endif /* CONFIG_CGROUP_SCHED */
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008292
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01008293#ifdef CONFIG_FAIR_GROUP_SCHED
Peter Zijlstra8ed36992008-02-13 15:45:39 +01008294static DEFINE_MUTEX(shares_mutex);
8295
Ingo Molnar4cf86d72007-10-15 17:00:14 +02008296int sched_group_set_shares(struct task_group *tg, unsigned long shares)
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008297{
8298 int i;
Paul Turner94371782010-11-15 15:47:10 -08008299 unsigned long flags;
Ingo Molnarc61935f2008-01-22 11:24:58 +01008300
Peter Zijlstra62fb1852008-02-25 17:34:02 +01008301 /*
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008302 * We can't change the weight of the root cgroup.
8303 */
8304 if (!tg->se[0])
8305 return -EINVAL;
8306
Peter Zijlstra18d95a22008-04-19 19:45:00 +02008307 if (shares < MIN_SHARES)
8308 shares = MIN_SHARES;
Miao Xiecb4ad1f2008-04-28 12:54:56 +08008309 else if (shares > MAX_SHARES)
8310 shares = MAX_SHARES;
Peter Zijlstra62fb1852008-02-25 17:34:02 +01008311
Peter Zijlstra8ed36992008-02-13 15:45:39 +01008312 mutex_lock(&shares_mutex);
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02008313 if (tg->shares == shares)
Dhaval Giani5cb350b2007-10-15 17:00:14 +02008314 goto done;
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008315
Srivatsa Vaddagiri6b2d7702008-01-25 21:08:00 +01008316 tg->shares = shares;
Peter Zijlstrac09595f2008-06-27 13:41:14 +02008317 for_each_possible_cpu(i) {
Paul Turner94371782010-11-15 15:47:10 -08008318 struct rq *rq = cpu_rq(i);
8319 struct sched_entity *se;
8320
8321 se = tg->se[i];
8322 /* Propagate contribution to hierarchy */
8323 raw_spin_lock_irqsave(&rq->lock, flags);
8324 for_each_sched_entity(se)
8325 update_cfs_shares(group_cfs_rq(se), 0);
8326 raw_spin_unlock_irqrestore(&rq->lock, flags);
Peter Zijlstrac09595f2008-06-27 13:41:14 +02008327 }
Srivatsa Vaddagiri6b2d7702008-01-25 21:08:00 +01008328
Dhaval Giani5cb350b2007-10-15 17:00:14 +02008329done:
Peter Zijlstra8ed36992008-02-13 15:45:39 +01008330 mutex_unlock(&shares_mutex);
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02008331 return 0;
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008332}
8333
Dhaval Giani5cb350b2007-10-15 17:00:14 +02008334unsigned long sched_group_shares(struct task_group *tg)
8335{
8336 return tg->shares;
8337}
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01008338#endif
Dhaval Giani5cb350b2007-10-15 17:00:14 +02008339
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01008340#ifdef CONFIG_RT_GROUP_SCHED
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008341/*
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01008342 * Ensure that the real time constraints are schedulable.
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008343 */
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01008344static DEFINE_MUTEX(rt_constraints_mutex);
8345
8346static unsigned long to_ratio(u64 period, u64 runtime)
8347{
8348 if (runtime == RUNTIME_INF)
Peter Zijlstra9a7e0b12008-08-19 12:33:06 +02008349 return 1ULL << 20;
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01008350
Peter Zijlstra9a7e0b12008-08-19 12:33:06 +02008351 return div64_u64(runtime << 20, period);
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01008352}
8353
Dhaval Giani521f1a242008-02-28 15:21:56 +05308354/* Must be called with tasklist_lock held */
8355static inline int tg_has_rt_tasks(struct task_group *tg)
8356{
8357 struct task_struct *g, *p;
Peter Zijlstra9a7e0b12008-08-19 12:33:06 +02008358
Dhaval Giani521f1a242008-02-28 15:21:56 +05308359 do_each_thread(g, p) {
8360 if (rt_task(p) && rt_rq_of_se(&p->rt)->tg == tg)
8361 return 1;
8362 } while_each_thread(g, p);
Peter Zijlstra9a7e0b12008-08-19 12:33:06 +02008363
Dhaval Giani521f1a242008-02-28 15:21:56 +05308364 return 0;
8365}
8366
Peter Zijlstra9a7e0b12008-08-19 12:33:06 +02008367struct rt_schedulable_data {
8368 struct task_group *tg;
8369 u64 rt_period;
8370 u64 rt_runtime;
8371};
8372
8373static int tg_schedulable(struct task_group *tg, void *data)
8374{
8375 struct rt_schedulable_data *d = data;
8376 struct task_group *child;
8377 unsigned long total, sum = 0;
8378 u64 period, runtime;
8379
8380 period = ktime_to_ns(tg->rt_bandwidth.rt_period);
8381 runtime = tg->rt_bandwidth.rt_runtime;
8382
8383 if (tg == d->tg) {
8384 period = d->rt_period;
8385 runtime = d->rt_runtime;
8386 }
8387
Peter Zijlstra4653f802008-09-23 15:33:44 +02008388 /*
8389 * Cannot have more runtime than the period.
8390 */
8391 if (runtime > period && runtime != RUNTIME_INF)
8392 return -EINVAL;
8393
8394 /*
8395 * Ensure we don't starve existing RT tasks.
8396 */
Peter Zijlstra9a7e0b12008-08-19 12:33:06 +02008397 if (rt_bandwidth_enabled() && !runtime && tg_has_rt_tasks(tg))
8398 return -EBUSY;
8399
8400 total = to_ratio(period, runtime);
8401
Peter Zijlstra4653f802008-09-23 15:33:44 +02008402 /*
8403 * Nobody can have more than the global setting allows.
8404 */
8405 if (total > to_ratio(global_rt_period(), global_rt_runtime()))
8406 return -EINVAL;
8407
8408 /*
8409 * The sum of our children's runtime should not exceed our own.
8410 */
Peter Zijlstra9a7e0b12008-08-19 12:33:06 +02008411 list_for_each_entry_rcu(child, &tg->children, siblings) {
8412 period = ktime_to_ns(child->rt_bandwidth.rt_period);
8413 runtime = child->rt_bandwidth.rt_runtime;
8414
8415 if (child == d->tg) {
8416 period = d->rt_period;
8417 runtime = d->rt_runtime;
8418 }
8419
8420 sum += to_ratio(period, runtime);
8421 }
8422
8423 if (sum > total)
8424 return -EINVAL;
8425
8426 return 0;
8427}
8428
8429static int __rt_schedulable(struct task_group *tg, u64 period, u64 runtime)
8430{
8431 struct rt_schedulable_data data = {
8432 .tg = tg,
8433 .rt_period = period,
8434 .rt_runtime = runtime,
8435 };
8436
8437 return walk_tg_tree(tg_schedulable, tg_nop, &data);
8438}
8439
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008440static int tg_set_bandwidth(struct task_group *tg,
8441 u64 rt_period, u64 rt_runtime)
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008442{
Peter Zijlstraac086bc2008-04-19 19:44:58 +02008443 int i, err = 0;
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01008444
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01008445 mutex_lock(&rt_constraints_mutex);
Dhaval Giani521f1a242008-02-28 15:21:56 +05308446 read_lock(&tasklist_lock);
Peter Zijlstra9a7e0b12008-08-19 12:33:06 +02008447 err = __rt_schedulable(tg, rt_period, rt_runtime);
8448 if (err)
Dhaval Giani521f1a242008-02-28 15:21:56 +05308449 goto unlock;
Peter Zijlstraac086bc2008-04-19 19:44:58 +02008450
Thomas Gleixner0986b112009-11-17 15:32:06 +01008451 raw_spin_lock_irq(&tg->rt_bandwidth.rt_runtime_lock);
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008452 tg->rt_bandwidth.rt_period = ns_to_ktime(rt_period);
8453 tg->rt_bandwidth.rt_runtime = rt_runtime;
Peter Zijlstraac086bc2008-04-19 19:44:58 +02008454
8455 for_each_possible_cpu(i) {
8456 struct rt_rq *rt_rq = tg->rt_rq[i];
8457
Thomas Gleixner0986b112009-11-17 15:32:06 +01008458 raw_spin_lock(&rt_rq->rt_runtime_lock);
Peter Zijlstraac086bc2008-04-19 19:44:58 +02008459 rt_rq->rt_runtime = rt_runtime;
Thomas Gleixner0986b112009-11-17 15:32:06 +01008460 raw_spin_unlock(&rt_rq->rt_runtime_lock);
Peter Zijlstraac086bc2008-04-19 19:44:58 +02008461 }
Thomas Gleixner0986b112009-11-17 15:32:06 +01008462 raw_spin_unlock_irq(&tg->rt_bandwidth.rt_runtime_lock);
Peter Zijlstra49246272010-10-17 21:46:10 +02008463unlock:
Dhaval Giani521f1a242008-02-28 15:21:56 +05308464 read_unlock(&tasklist_lock);
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01008465 mutex_unlock(&rt_constraints_mutex);
8466
8467 return err;
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008468}
8469
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008470int sched_group_set_rt_runtime(struct task_group *tg, long rt_runtime_us)
8471{
8472 u64 rt_runtime, rt_period;
8473
8474 rt_period = ktime_to_ns(tg->rt_bandwidth.rt_period);
8475 rt_runtime = (u64)rt_runtime_us * NSEC_PER_USEC;
8476 if (rt_runtime_us < 0)
8477 rt_runtime = RUNTIME_INF;
8478
8479 return tg_set_bandwidth(tg, rt_period, rt_runtime);
8480}
8481
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01008482long sched_group_rt_runtime(struct task_group *tg)
8483{
8484 u64 rt_runtime_us;
8485
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008486 if (tg->rt_bandwidth.rt_runtime == RUNTIME_INF)
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01008487 return -1;
8488
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008489 rt_runtime_us = tg->rt_bandwidth.rt_runtime;
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01008490 do_div(rt_runtime_us, NSEC_PER_USEC);
8491 return rt_runtime_us;
8492}
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008493
8494int sched_group_set_rt_period(struct task_group *tg, long rt_period_us)
8495{
8496 u64 rt_runtime, rt_period;
8497
8498 rt_period = (u64)rt_period_us * NSEC_PER_USEC;
8499 rt_runtime = tg->rt_bandwidth.rt_runtime;
8500
Raistlin619b0482008-06-26 18:54:09 +02008501 if (rt_period == 0)
8502 return -EINVAL;
8503
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008504 return tg_set_bandwidth(tg, rt_period, rt_runtime);
8505}
8506
8507long sched_group_rt_period(struct task_group *tg)
8508{
8509 u64 rt_period_us;
8510
8511 rt_period_us = ktime_to_ns(tg->rt_bandwidth.rt_period);
8512 do_div(rt_period_us, NSEC_PER_USEC);
8513 return rt_period_us;
8514}
8515
8516static int sched_rt_global_constraints(void)
8517{
Peter Zijlstra4653f802008-09-23 15:33:44 +02008518 u64 runtime, period;
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008519 int ret = 0;
8520
Hiroshi Shimamotoec5d4982008-09-10 17:00:19 -07008521 if (sysctl_sched_rt_period <= 0)
8522 return -EINVAL;
8523
Peter Zijlstra4653f802008-09-23 15:33:44 +02008524 runtime = global_rt_runtime();
8525 period = global_rt_period();
8526
8527 /*
8528 * Sanity check on the sysctl variables.
8529 */
8530 if (runtime > period && runtime != RUNTIME_INF)
8531 return -EINVAL;
Peter Zijlstra10b612f2008-06-19 14:22:27 +02008532
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008533 mutex_lock(&rt_constraints_mutex);
Peter Zijlstra9a7e0b12008-08-19 12:33:06 +02008534 read_lock(&tasklist_lock);
Peter Zijlstra4653f802008-09-23 15:33:44 +02008535 ret = __rt_schedulable(NULL, 0, 0);
Peter Zijlstra9a7e0b12008-08-19 12:33:06 +02008536 read_unlock(&tasklist_lock);
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008537 mutex_unlock(&rt_constraints_mutex);
8538
8539 return ret;
8540}
Dhaval Giani54e99122009-02-27 15:13:54 +05308541
8542int sched_rt_can_attach(struct task_group *tg, struct task_struct *tsk)
8543{
8544 /* Don't accept realtime tasks when there is no way for them to run */
8545 if (rt_task(tsk) && tg->rt_bandwidth.rt_runtime == 0)
8546 return 0;
8547
8548 return 1;
8549}
8550
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +02008551#else /* !CONFIG_RT_GROUP_SCHED */
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008552static int sched_rt_global_constraints(void)
8553{
Peter Zijlstraac086bc2008-04-19 19:44:58 +02008554 unsigned long flags;
8555 int i;
8556
Hiroshi Shimamotoec5d4982008-09-10 17:00:19 -07008557 if (sysctl_sched_rt_period <= 0)
8558 return -EINVAL;
8559
Peter Zijlstra60aa6052009-05-05 17:50:21 +02008560 /*
8561 * There's always some RT tasks in the root group
8562 * -- migration, kstopmachine etc..
8563 */
8564 if (sysctl_sched_rt_runtime == 0)
8565 return -EBUSY;
8566
Thomas Gleixner0986b112009-11-17 15:32:06 +01008567 raw_spin_lock_irqsave(&def_rt_bandwidth.rt_runtime_lock, flags);
Peter Zijlstraac086bc2008-04-19 19:44:58 +02008568 for_each_possible_cpu(i) {
8569 struct rt_rq *rt_rq = &cpu_rq(i)->rt;
8570
Thomas Gleixner0986b112009-11-17 15:32:06 +01008571 raw_spin_lock(&rt_rq->rt_runtime_lock);
Peter Zijlstraac086bc2008-04-19 19:44:58 +02008572 rt_rq->rt_runtime = global_rt_runtime();
Thomas Gleixner0986b112009-11-17 15:32:06 +01008573 raw_spin_unlock(&rt_rq->rt_runtime_lock);
Peter Zijlstraac086bc2008-04-19 19:44:58 +02008574 }
Thomas Gleixner0986b112009-11-17 15:32:06 +01008575 raw_spin_unlock_irqrestore(&def_rt_bandwidth.rt_runtime_lock, flags);
Peter Zijlstraac086bc2008-04-19 19:44:58 +02008576
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008577 return 0;
8578}
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +02008579#endif /* CONFIG_RT_GROUP_SCHED */
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008580
8581int sched_rt_handler(struct ctl_table *table, int write,
Alexey Dobriyan8d65af72009-09-23 15:57:19 -07008582 void __user *buffer, size_t *lenp,
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008583 loff_t *ppos)
8584{
8585 int ret;
8586 int old_period, old_runtime;
8587 static DEFINE_MUTEX(mutex);
8588
8589 mutex_lock(&mutex);
8590 old_period = sysctl_sched_rt_period;
8591 old_runtime = sysctl_sched_rt_runtime;
8592
Alexey Dobriyan8d65af72009-09-23 15:57:19 -07008593 ret = proc_dointvec(table, write, buffer, lenp, ppos);
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008594
8595 if (!ret && write) {
8596 ret = sched_rt_global_constraints();
8597 if (ret) {
8598 sysctl_sched_rt_period = old_period;
8599 sysctl_sched_rt_runtime = old_runtime;
8600 } else {
8601 def_rt_bandwidth.rt_runtime = global_rt_runtime();
8602 def_rt_bandwidth.rt_period =
8603 ns_to_ktime(global_rt_period());
8604 }
8605 }
8606 mutex_unlock(&mutex);
8607
8608 return ret;
8609}
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008610
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01008611#ifdef CONFIG_CGROUP_SCHED
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008612
8613/* return corresponding task_group object of a cgroup */
Paul Menage2b01dfe2007-10-24 18:23:50 +02008614static inline struct task_group *cgroup_tg(struct cgroup *cgrp)
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008615{
Paul Menage2b01dfe2007-10-24 18:23:50 +02008616 return container_of(cgroup_subsys_state(cgrp, cpu_cgroup_subsys_id),
8617 struct task_group, css);
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008618}
8619
8620static struct cgroup_subsys_state *
Paul Menage2b01dfe2007-10-24 18:23:50 +02008621cpu_cgroup_create(struct cgroup_subsys *ss, struct cgroup *cgrp)
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008622{
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008623 struct task_group *tg, *parent;
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008624
Paul Menage2b01dfe2007-10-24 18:23:50 +02008625 if (!cgrp->parent) {
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008626 /* This is early initialization for the top cgroup */
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008627 return &init_task_group.css;
8628 }
8629
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008630 parent = cgroup_tg(cgrp->parent);
8631 tg = sched_create_group(parent);
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008632 if (IS_ERR(tg))
8633 return ERR_PTR(-ENOMEM);
8634
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008635 return &tg->css;
8636}
8637
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01008638static void
8639cpu_cgroup_destroy(struct cgroup_subsys *ss, struct cgroup *cgrp)
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008640{
Paul Menage2b01dfe2007-10-24 18:23:50 +02008641 struct task_group *tg = cgroup_tg(cgrp);
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008642
8643 sched_destroy_group(tg);
8644}
8645
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01008646static int
Ben Blumbe367d02009-09-23 15:56:31 -07008647cpu_cgroup_can_attach_task(struct cgroup *cgrp, struct task_struct *tsk)
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008648{
Peter Zijlstrab68aa232008-02-13 15:45:40 +01008649#ifdef CONFIG_RT_GROUP_SCHED
Dhaval Giani54e99122009-02-27 15:13:54 +05308650 if (!sched_rt_can_attach(cgroup_tg(cgrp), tsk))
Peter Zijlstrab68aa232008-02-13 15:45:40 +01008651 return -EINVAL;
8652#else
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008653 /* We don't support RT-tasks being in separate groups */
8654 if (tsk->sched_class != &fair_sched_class)
8655 return -EINVAL;
Peter Zijlstrab68aa232008-02-13 15:45:40 +01008656#endif
Ben Blumbe367d02009-09-23 15:56:31 -07008657 return 0;
8658}
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008659
Ben Blumbe367d02009-09-23 15:56:31 -07008660static int
8661cpu_cgroup_can_attach(struct cgroup_subsys *ss, struct cgroup *cgrp,
8662 struct task_struct *tsk, bool threadgroup)
8663{
8664 int retval = cpu_cgroup_can_attach_task(cgrp, tsk);
8665 if (retval)
8666 return retval;
8667 if (threadgroup) {
8668 struct task_struct *c;
8669 rcu_read_lock();
8670 list_for_each_entry_rcu(c, &tsk->thread_group, thread_group) {
8671 retval = cpu_cgroup_can_attach_task(cgrp, c);
8672 if (retval) {
8673 rcu_read_unlock();
8674 return retval;
8675 }
8676 }
8677 rcu_read_unlock();
8678 }
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008679 return 0;
8680}
8681
8682static void
Paul Menage2b01dfe2007-10-24 18:23:50 +02008683cpu_cgroup_attach(struct cgroup_subsys *ss, struct cgroup *cgrp,
Ben Blumbe367d02009-09-23 15:56:31 -07008684 struct cgroup *old_cont, struct task_struct *tsk,
8685 bool threadgroup)
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008686{
8687 sched_move_task(tsk);
Ben Blumbe367d02009-09-23 15:56:31 -07008688 if (threadgroup) {
8689 struct task_struct *c;
8690 rcu_read_lock();
8691 list_for_each_entry_rcu(c, &tsk->thread_group, thread_group) {
8692 sched_move_task(c);
8693 }
8694 rcu_read_unlock();
8695 }
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008696}
8697
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01008698#ifdef CONFIG_FAIR_GROUP_SCHED
Paul Menagef4c753b2008-04-29 00:59:56 -07008699static int cpu_shares_write_u64(struct cgroup *cgrp, struct cftype *cftype,
Paul Menage2b01dfe2007-10-24 18:23:50 +02008700 u64 shareval)
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008701{
Paul Menage2b01dfe2007-10-24 18:23:50 +02008702 return sched_group_set_shares(cgroup_tg(cgrp), shareval);
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008703}
8704
Paul Menagef4c753b2008-04-29 00:59:56 -07008705static u64 cpu_shares_read_u64(struct cgroup *cgrp, struct cftype *cft)
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008706{
Paul Menage2b01dfe2007-10-24 18:23:50 +02008707 struct task_group *tg = cgroup_tg(cgrp);
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008708
8709 return (u64) tg->shares;
8710}
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +02008711#endif /* CONFIG_FAIR_GROUP_SCHED */
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008712
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01008713#ifdef CONFIG_RT_GROUP_SCHED
Mirco Tischler0c708142008-05-14 16:05:46 -07008714static int cpu_rt_runtime_write(struct cgroup *cgrp, struct cftype *cft,
Paul Menage06ecb272008-04-29 01:00:06 -07008715 s64 val)
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008716{
Paul Menage06ecb272008-04-29 01:00:06 -07008717 return sched_group_set_rt_runtime(cgroup_tg(cgrp), val);
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008718}
8719
Paul Menage06ecb272008-04-29 01:00:06 -07008720static s64 cpu_rt_runtime_read(struct cgroup *cgrp, struct cftype *cft)
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008721{
Paul Menage06ecb272008-04-29 01:00:06 -07008722 return sched_group_rt_runtime(cgroup_tg(cgrp));
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008723}
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008724
8725static int cpu_rt_period_write_uint(struct cgroup *cgrp, struct cftype *cftype,
8726 u64 rt_period_us)
8727{
8728 return sched_group_set_rt_period(cgroup_tg(cgrp), rt_period_us);
8729}
8730
8731static u64 cpu_rt_period_read_uint(struct cgroup *cgrp, struct cftype *cft)
8732{
8733 return sched_group_rt_period(cgroup_tg(cgrp));
8734}
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +02008735#endif /* CONFIG_RT_GROUP_SCHED */
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008736
Paul Menagefe5c7cc2007-10-29 21:18:11 +01008737static struct cftype cpu_files[] = {
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01008738#ifdef CONFIG_FAIR_GROUP_SCHED
Paul Menagefe5c7cc2007-10-29 21:18:11 +01008739 {
8740 .name = "shares",
Paul Menagef4c753b2008-04-29 00:59:56 -07008741 .read_u64 = cpu_shares_read_u64,
8742 .write_u64 = cpu_shares_write_u64,
Paul Menagefe5c7cc2007-10-29 21:18:11 +01008743 },
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01008744#endif
8745#ifdef CONFIG_RT_GROUP_SCHED
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008746 {
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01008747 .name = "rt_runtime_us",
Paul Menage06ecb272008-04-29 01:00:06 -07008748 .read_s64 = cpu_rt_runtime_read,
8749 .write_s64 = cpu_rt_runtime_write,
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008750 },
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008751 {
8752 .name = "rt_period_us",
Paul Menagef4c753b2008-04-29 00:59:56 -07008753 .read_u64 = cpu_rt_period_read_uint,
8754 .write_u64 = cpu_rt_period_write_uint,
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008755 },
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01008756#endif
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008757};
8758
8759static int cpu_cgroup_populate(struct cgroup_subsys *ss, struct cgroup *cont)
8760{
Paul Menagefe5c7cc2007-10-29 21:18:11 +01008761 return cgroup_add_files(cont, ss, cpu_files, ARRAY_SIZE(cpu_files));
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008762}
8763
8764struct cgroup_subsys cpu_cgroup_subsys = {
Ingo Molnar38605ca2007-10-29 21:18:11 +01008765 .name = "cpu",
8766 .create = cpu_cgroup_create,
8767 .destroy = cpu_cgroup_destroy,
8768 .can_attach = cpu_cgroup_can_attach,
8769 .attach = cpu_cgroup_attach,
8770 .populate = cpu_cgroup_populate,
8771 .subsys_id = cpu_cgroup_subsys_id,
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008772 .early_init = 1,
8773};
8774
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01008775#endif /* CONFIG_CGROUP_SCHED */
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01008776
8777#ifdef CONFIG_CGROUP_CPUACCT
8778
8779/*
8780 * CPU accounting code for task groups.
8781 *
8782 * Based on the work by Paul Menage (menage@google.com) and Balbir Singh
8783 * (balbir@in.ibm.com).
8784 */
8785
Bharata B Rao934352f2008-11-10 20:41:13 +05308786/* track cpu usage of a group of tasks and its child groups */
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01008787struct cpuacct {
8788 struct cgroup_subsys_state css;
8789 /* cpuusage holds pointer to a u64-type object on every cpu */
Tejun Heo43cf38e2010-02-02 14:38:57 +09008790 u64 __percpu *cpuusage;
Bharata B Raoef12fef2009-03-31 10:02:22 +05308791 struct percpu_counter cpustat[CPUACCT_STAT_NSTATS];
Bharata B Rao934352f2008-11-10 20:41:13 +05308792 struct cpuacct *parent;
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01008793};
8794
8795struct cgroup_subsys cpuacct_subsys;
8796
8797/* return cpu accounting group corresponding to this container */
Dhaval Giani32cd7562008-02-29 10:02:43 +05308798static inline struct cpuacct *cgroup_ca(struct cgroup *cgrp)
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01008799{
Dhaval Giani32cd7562008-02-29 10:02:43 +05308800 return container_of(cgroup_subsys_state(cgrp, cpuacct_subsys_id),
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01008801 struct cpuacct, css);
8802}
8803
8804/* return cpu accounting group to which this task belongs */
8805static inline struct cpuacct *task_ca(struct task_struct *tsk)
8806{
8807 return container_of(task_subsys_state(tsk, cpuacct_subsys_id),
8808 struct cpuacct, css);
8809}
8810
8811/* create a new cpu accounting group */
8812static struct cgroup_subsys_state *cpuacct_create(
Dhaval Giani32cd7562008-02-29 10:02:43 +05308813 struct cgroup_subsys *ss, struct cgroup *cgrp)
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01008814{
8815 struct cpuacct *ca = kzalloc(sizeof(*ca), GFP_KERNEL);
Bharata B Raoef12fef2009-03-31 10:02:22 +05308816 int i;
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01008817
8818 if (!ca)
Bharata B Raoef12fef2009-03-31 10:02:22 +05308819 goto out;
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01008820
8821 ca->cpuusage = alloc_percpu(u64);
Bharata B Raoef12fef2009-03-31 10:02:22 +05308822 if (!ca->cpuusage)
8823 goto out_free_ca;
8824
8825 for (i = 0; i < CPUACCT_STAT_NSTATS; i++)
8826 if (percpu_counter_init(&ca->cpustat[i], 0))
8827 goto out_free_counters;
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01008828
Bharata B Rao934352f2008-11-10 20:41:13 +05308829 if (cgrp->parent)
8830 ca->parent = cgroup_ca(cgrp->parent);
8831
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01008832 return &ca->css;
Bharata B Raoef12fef2009-03-31 10:02:22 +05308833
8834out_free_counters:
8835 while (--i >= 0)
8836 percpu_counter_destroy(&ca->cpustat[i]);
8837 free_percpu(ca->cpuusage);
8838out_free_ca:
8839 kfree(ca);
8840out:
8841 return ERR_PTR(-ENOMEM);
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01008842}
8843
8844/* destroy an existing cpu accounting group */
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01008845static void
Dhaval Giani32cd7562008-02-29 10:02:43 +05308846cpuacct_destroy(struct cgroup_subsys *ss, struct cgroup *cgrp)
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01008847{
Dhaval Giani32cd7562008-02-29 10:02:43 +05308848 struct cpuacct *ca = cgroup_ca(cgrp);
Bharata B Raoef12fef2009-03-31 10:02:22 +05308849 int i;
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01008850
Bharata B Raoef12fef2009-03-31 10:02:22 +05308851 for (i = 0; i < CPUACCT_STAT_NSTATS; i++)
8852 percpu_counter_destroy(&ca->cpustat[i]);
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01008853 free_percpu(ca->cpuusage);
8854 kfree(ca);
8855}
8856
Ken Chen720f5492008-12-15 22:02:01 -08008857static u64 cpuacct_cpuusage_read(struct cpuacct *ca, int cpu)
8858{
Rusty Russellb36128c2009-02-20 16:29:08 +09008859 u64 *cpuusage = per_cpu_ptr(ca->cpuusage, cpu);
Ken Chen720f5492008-12-15 22:02:01 -08008860 u64 data;
8861
8862#ifndef CONFIG_64BIT
8863 /*
8864 * Take rq->lock to make 64-bit read safe on 32-bit platforms.
8865 */
Thomas Gleixner05fa7852009-11-17 14:28:38 +01008866 raw_spin_lock_irq(&cpu_rq(cpu)->lock);
Ken Chen720f5492008-12-15 22:02:01 -08008867 data = *cpuusage;
Thomas Gleixner05fa7852009-11-17 14:28:38 +01008868 raw_spin_unlock_irq(&cpu_rq(cpu)->lock);
Ken Chen720f5492008-12-15 22:02:01 -08008869#else
8870 data = *cpuusage;
8871#endif
8872
8873 return data;
8874}
8875
8876static void cpuacct_cpuusage_write(struct cpuacct *ca, int cpu, u64 val)
8877{
Rusty Russellb36128c2009-02-20 16:29:08 +09008878 u64 *cpuusage = per_cpu_ptr(ca->cpuusage, cpu);
Ken Chen720f5492008-12-15 22:02:01 -08008879
8880#ifndef CONFIG_64BIT
8881 /*
8882 * Take rq->lock to make 64-bit write safe on 32-bit platforms.
8883 */
Thomas Gleixner05fa7852009-11-17 14:28:38 +01008884 raw_spin_lock_irq(&cpu_rq(cpu)->lock);
Ken Chen720f5492008-12-15 22:02:01 -08008885 *cpuusage = val;
Thomas Gleixner05fa7852009-11-17 14:28:38 +01008886 raw_spin_unlock_irq(&cpu_rq(cpu)->lock);
Ken Chen720f5492008-12-15 22:02:01 -08008887#else
8888 *cpuusage = val;
8889#endif
8890}
8891
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01008892/* return total cpu usage (in nanoseconds) of a group */
Dhaval Giani32cd7562008-02-29 10:02:43 +05308893static u64 cpuusage_read(struct cgroup *cgrp, struct cftype *cft)
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01008894{
Dhaval Giani32cd7562008-02-29 10:02:43 +05308895 struct cpuacct *ca = cgroup_ca(cgrp);
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01008896 u64 totalcpuusage = 0;
8897 int i;
8898
Ken Chen720f5492008-12-15 22:02:01 -08008899 for_each_present_cpu(i)
8900 totalcpuusage += cpuacct_cpuusage_read(ca, i);
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01008901
8902 return totalcpuusage;
8903}
8904
Dhaval Giani0297b802008-02-29 10:02:44 +05308905static int cpuusage_write(struct cgroup *cgrp, struct cftype *cftype,
8906 u64 reset)
8907{
8908 struct cpuacct *ca = cgroup_ca(cgrp);
8909 int err = 0;
8910 int i;
8911
8912 if (reset) {
8913 err = -EINVAL;
8914 goto out;
8915 }
8916
Ken Chen720f5492008-12-15 22:02:01 -08008917 for_each_present_cpu(i)
8918 cpuacct_cpuusage_write(ca, i, 0);
Dhaval Giani0297b802008-02-29 10:02:44 +05308919
Dhaval Giani0297b802008-02-29 10:02:44 +05308920out:
8921 return err;
8922}
8923
Ken Chene9515c32008-12-15 22:04:15 -08008924static int cpuacct_percpu_seq_read(struct cgroup *cgroup, struct cftype *cft,
8925 struct seq_file *m)
8926{
8927 struct cpuacct *ca = cgroup_ca(cgroup);
8928 u64 percpu;
8929 int i;
8930
8931 for_each_present_cpu(i) {
8932 percpu = cpuacct_cpuusage_read(ca, i);
8933 seq_printf(m, "%llu ", (unsigned long long) percpu);
8934 }
8935 seq_printf(m, "\n");
8936 return 0;
8937}
8938
Bharata B Raoef12fef2009-03-31 10:02:22 +05308939static const char *cpuacct_stat_desc[] = {
8940 [CPUACCT_STAT_USER] = "user",
8941 [CPUACCT_STAT_SYSTEM] = "system",
8942};
8943
8944static int cpuacct_stats_show(struct cgroup *cgrp, struct cftype *cft,
8945 struct cgroup_map_cb *cb)
8946{
8947 struct cpuacct *ca = cgroup_ca(cgrp);
8948 int i;
8949
8950 for (i = 0; i < CPUACCT_STAT_NSTATS; i++) {
8951 s64 val = percpu_counter_read(&ca->cpustat[i]);
8952 val = cputime64_to_clock_t(val);
8953 cb->fill(cb, cpuacct_stat_desc[i], val);
8954 }
8955 return 0;
8956}
8957
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01008958static struct cftype files[] = {
8959 {
8960 .name = "usage",
Paul Menagef4c753b2008-04-29 00:59:56 -07008961 .read_u64 = cpuusage_read,
8962 .write_u64 = cpuusage_write,
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01008963 },
Ken Chene9515c32008-12-15 22:04:15 -08008964 {
8965 .name = "usage_percpu",
8966 .read_seq_string = cpuacct_percpu_seq_read,
8967 },
Bharata B Raoef12fef2009-03-31 10:02:22 +05308968 {
8969 .name = "stat",
8970 .read_map = cpuacct_stats_show,
8971 },
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01008972};
8973
Dhaval Giani32cd7562008-02-29 10:02:43 +05308974static int cpuacct_populate(struct cgroup_subsys *ss, struct cgroup *cgrp)
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01008975{
Dhaval Giani32cd7562008-02-29 10:02:43 +05308976 return cgroup_add_files(cgrp, ss, files, ARRAY_SIZE(files));
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01008977}
8978
8979/*
8980 * charge this task's execution time to its accounting group.
8981 *
8982 * called with rq->lock held.
8983 */
8984static void cpuacct_charge(struct task_struct *tsk, u64 cputime)
8985{
8986 struct cpuacct *ca;
Bharata B Rao934352f2008-11-10 20:41:13 +05308987 int cpu;
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01008988
Li Zefanc40c6f82009-02-26 15:40:15 +08008989 if (unlikely(!cpuacct_subsys.active))
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01008990 return;
8991
Bharata B Rao934352f2008-11-10 20:41:13 +05308992 cpu = task_cpu(tsk);
Bharata B Raoa18b83b2009-03-23 10:02:53 +05308993
8994 rcu_read_lock();
8995
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01008996 ca = task_ca(tsk);
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01008997
Bharata B Rao934352f2008-11-10 20:41:13 +05308998 for (; ca; ca = ca->parent) {
Rusty Russellb36128c2009-02-20 16:29:08 +09008999 u64 *cpuusage = per_cpu_ptr(ca->cpuusage, cpu);
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01009000 *cpuusage += cputime;
9001 }
Bharata B Raoa18b83b2009-03-23 10:02:53 +05309002
9003 rcu_read_unlock();
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01009004}
9005
Bharata B Raoef12fef2009-03-31 10:02:22 +05309006/*
Anton Blanchardfa535a72010-02-02 14:46:13 -08009007 * When CONFIG_VIRT_CPU_ACCOUNTING is enabled one jiffy can be very large
9008 * in cputime_t units. As a result, cpuacct_update_stats calls
9009 * percpu_counter_add with values large enough to always overflow the
9010 * per cpu batch limit causing bad SMP scalability.
9011 *
9012 * To fix this we scale percpu_counter_batch by cputime_one_jiffy so we
9013 * batch the same amount of time with CONFIG_VIRT_CPU_ACCOUNTING disabled
9014 * and enabled. We cap it at INT_MAX which is the largest allowed batch value.
9015 */
9016#ifdef CONFIG_SMP
9017#define CPUACCT_BATCH \
9018 min_t(long, percpu_counter_batch * cputime_one_jiffy, INT_MAX)
9019#else
9020#define CPUACCT_BATCH 0
9021#endif
9022
9023/*
Bharata B Raoef12fef2009-03-31 10:02:22 +05309024 * Charge the system/user time to the task's accounting group.
9025 */
9026static void cpuacct_update_stats(struct task_struct *tsk,
9027 enum cpuacct_stat_index idx, cputime_t val)
9028{
9029 struct cpuacct *ca;
Anton Blanchardfa535a72010-02-02 14:46:13 -08009030 int batch = CPUACCT_BATCH;
Bharata B Raoef12fef2009-03-31 10:02:22 +05309031
9032 if (unlikely(!cpuacct_subsys.active))
9033 return;
9034
9035 rcu_read_lock();
9036 ca = task_ca(tsk);
9037
9038 do {
Anton Blanchardfa535a72010-02-02 14:46:13 -08009039 __percpu_counter_add(&ca->cpustat[idx], val, batch);
Bharata B Raoef12fef2009-03-31 10:02:22 +05309040 ca = ca->parent;
9041 } while (ca);
9042 rcu_read_unlock();
9043}
9044
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01009045struct cgroup_subsys cpuacct_subsys = {
9046 .name = "cpuacct",
9047 .create = cpuacct_create,
9048 .destroy = cpuacct_destroy,
9049 .populate = cpuacct_populate,
9050 .subsys_id = cpuacct_subsys_id,
9051};
9052#endif /* CONFIG_CGROUP_CPUACCT */
Paul E. McKenney03b042b2009-06-25 09:08:16 -07009053
9054#ifndef CONFIG_SMP
9055
Paul E. McKenney03b042b2009-06-25 09:08:16 -07009056void synchronize_sched_expedited(void)
9057{
Paul E. McKenneyfc390cd2010-05-06 11:42:52 -07009058 barrier();
Paul E. McKenney03b042b2009-06-25 09:08:16 -07009059}
9060EXPORT_SYMBOL_GPL(synchronize_sched_expedited);
9061
9062#else /* #ifndef CONFIG_SMP */
9063
Paul E. McKenneycc631fb2010-05-06 18:49:21 +02009064static atomic_t synchronize_sched_expedited_count = ATOMIC_INIT(0);
Paul E. McKenney03b042b2009-06-25 09:08:16 -07009065
Paul E. McKenneycc631fb2010-05-06 18:49:21 +02009066static int synchronize_sched_expedited_cpu_stop(void *data)
Paul E. McKenney03b042b2009-06-25 09:08:16 -07009067{
Tejun Heo969c7922010-05-06 18:49:21 +02009068 /*
9069 * There must be a full memory barrier on each affected CPU
9070 * between the time that try_stop_cpus() is called and the
9071 * time that it returns.
9072 *
9073 * In the current initial implementation of cpu_stop, the
9074 * above condition is already met when the control reaches
9075 * this point and the following smp_mb() is not strictly
9076 * necessary. Do smp_mb() anyway for documentation and
9077 * robustness against future implementation changes.
9078 */
Paul E. McKenneycc631fb2010-05-06 18:49:21 +02009079 smp_mb(); /* See above comment block. */
Tejun Heo969c7922010-05-06 18:49:21 +02009080 return 0;
Paul E. McKenney03b042b2009-06-25 09:08:16 -07009081}
Paul E. McKenney03b042b2009-06-25 09:08:16 -07009082
9083/*
9084 * Wait for an rcu-sched grace period to elapse, but use "big hammer"
9085 * approach to force grace period to end quickly. This consumes
9086 * significant time on all CPUs, and is thus not recommended for
9087 * any sort of common-case code.
9088 *
9089 * Note that it is illegal to call this function while holding any
9090 * lock that is acquired by a CPU-hotplug notifier. Failing to
9091 * observe this restriction will result in deadlock.
9092 */
9093void synchronize_sched_expedited(void)
9094{
Tejun Heo969c7922010-05-06 18:49:21 +02009095 int snap, trycount = 0;
Paul E. McKenney03b042b2009-06-25 09:08:16 -07009096
9097 smp_mb(); /* ensure prior mod happens before capturing snap. */
Tejun Heo969c7922010-05-06 18:49:21 +02009098 snap = atomic_read(&synchronize_sched_expedited_count) + 1;
Paul E. McKenney03b042b2009-06-25 09:08:16 -07009099 get_online_cpus();
Tejun Heo969c7922010-05-06 18:49:21 +02009100 while (try_stop_cpus(cpu_online_mask,
9101 synchronize_sched_expedited_cpu_stop,
Tejun Heo94458d52010-05-06 18:49:21 +02009102 NULL) == -EAGAIN) {
Paul E. McKenney03b042b2009-06-25 09:08:16 -07009103 put_online_cpus();
9104 if (trycount++ < 10)
9105 udelay(trycount * num_online_cpus());
9106 else {
9107 synchronize_sched();
9108 return;
9109 }
Tejun Heo969c7922010-05-06 18:49:21 +02009110 if (atomic_read(&synchronize_sched_expedited_count) - snap > 0) {
Paul E. McKenney03b042b2009-06-25 09:08:16 -07009111 smp_mb(); /* ensure test happens before caller kfree */
9112 return;
9113 }
9114 get_online_cpus();
9115 }
Tejun Heo969c7922010-05-06 18:49:21 +02009116 atomic_inc(&synchronize_sched_expedited_count);
Paul E. McKenneycc631fb2010-05-06 18:49:21 +02009117 smp_mb__after_atomic_inc(); /* ensure post-GP actions seen after GP. */
Paul E. McKenney03b042b2009-06-25 09:08:16 -07009118 put_online_cpus();
Paul E. McKenney03b042b2009-06-25 09:08:16 -07009119}
9120EXPORT_SYMBOL_GPL(synchronize_sched_expedited);
9121
9122#endif /* #else #ifndef CONFIG_SMP */