blob: f08cb23be96c3904a1910380a4516f2c5a80b034 [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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070035#include <asm/mmu_context.h>
36#include <linux/interrupt.h>
Randy.Dunlapc59ede72006-01-11 12:17:46 -080037#include <linux/capability.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070038#include <linux/completion.h>
39#include <linux/kernel_stat.h>
Ingo Molnar9a11b492006-07-03 00:24:33 -070040#include <linux/debug_locks.h>
Ingo Molnarcdd6c482009-09-21 12:02:48 +020041#include <linux/perf_event.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070042#include <linux/security.h>
43#include <linux/notifier.h>
44#include <linux/profile.h>
Nigel Cunningham7dfb7102006-12-06 20:34:23 -080045#include <linux/freezer.h>
akpm@osdl.org198e2f12006-01-12 01:05:30 -080046#include <linux/vmalloc.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070047#include <linux/blkdev.h>
48#include <linux/delay.h>
Pavel Emelyanovb4888932007-10-18 23:40:14 -070049#include <linux/pid_namespace.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070050#include <linux/smp.h>
51#include <linux/threads.h>
52#include <linux/timer.h>
53#include <linux/rcupdate.h>
54#include <linux/cpu.h>
55#include <linux/cpuset.h>
56#include <linux/percpu.h>
Alexey Dobriyanb5aadf72008-10-06 13:23:43 +040057#include <linux/proc_fs.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070058#include <linux/seq_file.h>
Tejun Heo969c7922010-05-06 18:49:21 +020059#include <linux/stop_machine.h>
Nick Piggine692ab52007-07-26 13:40:43 +020060#include <linux/sysctl.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070061#include <linux/syscalls.h>
62#include <linux/times.h>
Jay Lan8f0ab512006-09-30 23:28:59 -070063#include <linux/tsacct_kern.h>
bibo maoc6fd91f2006-03-26 01:38:20 -080064#include <linux/kprobes.h>
Shailabh Nagar0ff92242006-07-14 00:24:37 -070065#include <linux/delayacct.h>
Ingo Molnardff06c12007-07-09 18:52:00 +020066#include <linux/unistd.h>
Jens Axboef5ff8422007-09-21 09:19:54 +020067#include <linux/pagemap.h>
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +010068#include <linux/hrtimer.h>
Reynes Philippe30914a52008-03-17 16:19:05 -070069#include <linux/tick.h>
Peter Zijlstraf00b45c2008-04-19 19:45:00 +020070#include <linux/debugfs.h>
71#include <linux/ctype.h>
Steven Rostedt6cd8a4b2008-05-12 21:20:42 +020072#include <linux/ftrace.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090073#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070074
Eric Dumazet5517d862007-05-08 00:32:57 -070075#include <asm/tlb.h>
Satyam Sharma838225b2007-10-24 18:23:50 +020076#include <asm/irq_regs.h>
Gerald Schaefer335d7af2010-11-22 15:47:36 +010077#include <asm/mutex.h>
Glauber Costae6e66852011-07-11 15:28:17 -040078#ifdef CONFIG_PARAVIRT
79#include <asm/paravirt.h>
80#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070081
Gregory Haskins6e0534f2008-05-12 21:21:01 +020082#include "sched_cpupri.h"
Tejun Heo21aa9af2010-06-08 21:40:37 +020083#include "workqueue_sched.h"
Mike Galbraith5091faa2010-11-30 14:18:03 +010084#include "sched_autogroup.h"
Gregory Haskins6e0534f2008-05-12 21:21:01 +020085
Steven Rostedta8d154b2009-04-10 09:36:00 -040086#define CREATE_TRACE_POINTS
Steven Rostedtad8d75f2009-04-14 19:39:12 -040087#include <trace/events/sched.h>
Steven Rostedta8d154b2009-04-10 09:36:00 -040088
Linus Torvalds1da177e2005-04-16 15:20:36 -070089/*
90 * Convert user-nice values [ -20 ... 0 ... 19 ]
91 * to static priority [ MAX_RT_PRIO..MAX_PRIO-1 ],
92 * and back.
93 */
94#define NICE_TO_PRIO(nice) (MAX_RT_PRIO + (nice) + 20)
95#define PRIO_TO_NICE(prio) ((prio) - MAX_RT_PRIO - 20)
96#define TASK_NICE(p) PRIO_TO_NICE((p)->static_prio)
97
98/*
99 * 'User priority' is the nice value converted to something we
100 * can work with better when scaling various scheduler parameters,
101 * it's a [ 0 ... 39 ] range.
102 */
103#define USER_PRIO(p) ((p)-MAX_RT_PRIO)
104#define TASK_USER_PRIO(p) USER_PRIO((p)->static_prio)
105#define MAX_USER_PRIO (USER_PRIO(MAX_PRIO))
106
107/*
Ingo Molnard7876a02008-01-25 21:08:19 +0100108 * Helpers for converting nanosecond timing to jiffy resolution
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109 */
Eric Dumazetd6322fa2007-11-09 22:39:38 +0100110#define NS_TO_JIFFIES(TIME) ((unsigned long)(TIME) / (NSEC_PER_SEC / HZ))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200112#define NICE_0_LOAD SCHED_LOAD_SCALE
113#define NICE_0_SHIFT SCHED_LOAD_SHIFT
114
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115/*
116 * These are the 'tuning knobs' of the scheduler:
117 *
Dmitry Adamushkoa4ec24b2007-10-15 17:00:13 +0200118 * default timeslice is 100 msecs (used only for SCHED_RR tasks).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119 * Timeslices get refilled after they expire.
120 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121#define DEF_TIMESLICE (100 * HZ / 1000)
Peter Williams2dd73a42006-06-27 02:54:34 -0700122
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +0200123/*
124 * single value that denotes runtime == period, ie unlimited time.
125 */
126#define RUNTIME_INF ((u64)~0ULL)
127
Ingo Molnare05606d2007-07-09 18:51:59 +0200128static inline int rt_policy(int policy)
129{
Steven Rostedt63f01242010-12-06 14:48:10 -0500130 if (policy == SCHED_FIFO || policy == SCHED_RR)
Ingo Molnare05606d2007-07-09 18:51:59 +0200131 return 1;
132 return 0;
133}
134
135static inline int task_has_rt_policy(struct task_struct *p)
136{
137 return rt_policy(p->policy);
138}
139
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140/*
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200141 * This is the priority-queue data structure of the RT scheduling class:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 */
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200143struct rt_prio_array {
144 DECLARE_BITMAP(bitmap, MAX_RT_PRIO+1); /* include 1 bit for delimiter */
145 struct list_head queue[MAX_RT_PRIO];
146};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +0200148struct rt_bandwidth {
Ingo Molnarea736ed2008-03-25 13:51:45 +0100149 /* nests inside the rq lock: */
Thomas Gleixner0986b112009-11-17 15:32:06 +0100150 raw_spinlock_t rt_runtime_lock;
Ingo Molnarea736ed2008-03-25 13:51:45 +0100151 ktime_t rt_period;
152 u64 rt_runtime;
153 struct hrtimer rt_period_timer;
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +0200154};
155
156static struct rt_bandwidth def_rt_bandwidth;
157
158static int do_sched_rt_period_timer(struct rt_bandwidth *rt_b, int overrun);
159
160static enum hrtimer_restart sched_rt_period_timer(struct hrtimer *timer)
161{
162 struct rt_bandwidth *rt_b =
163 container_of(timer, struct rt_bandwidth, rt_period_timer);
164 ktime_t now;
165 int overrun;
166 int idle = 0;
167
168 for (;;) {
169 now = hrtimer_cb_get_time(timer);
170 overrun = hrtimer_forward(timer, now, rt_b->rt_period);
171
172 if (!overrun)
173 break;
174
175 idle = do_sched_rt_period_timer(rt_b, overrun);
176 }
177
178 return idle ? HRTIMER_NORESTART : HRTIMER_RESTART;
179}
180
181static
182void init_rt_bandwidth(struct rt_bandwidth *rt_b, u64 period, u64 runtime)
183{
184 rt_b->rt_period = ns_to_ktime(period);
185 rt_b->rt_runtime = runtime;
186
Thomas Gleixner0986b112009-11-17 15:32:06 +0100187 raw_spin_lock_init(&rt_b->rt_runtime_lock);
Peter Zijlstraac086bc2008-04-19 19:44:58 +0200188
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +0200189 hrtimer_init(&rt_b->rt_period_timer,
190 CLOCK_MONOTONIC, HRTIMER_MODE_REL);
191 rt_b->rt_period_timer.function = sched_rt_period_timer;
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +0200192}
193
Krzysztof Heltc8bfff62008-09-05 23:46:19 +0200194static inline int rt_bandwidth_enabled(void)
195{
196 return sysctl_sched_rt_runtime >= 0;
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +0200197}
198
199static void start_rt_bandwidth(struct rt_bandwidth *rt_b)
200{
201 ktime_t now;
202
Hiroshi Shimamotocac64d02009-02-25 09:59:26 -0800203 if (!rt_bandwidth_enabled() || rt_b->rt_runtime == RUNTIME_INF)
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +0200204 return;
205
206 if (hrtimer_active(&rt_b->rt_period_timer))
207 return;
208
Thomas Gleixner0986b112009-11-17 15:32:06 +0100209 raw_spin_lock(&rt_b->rt_runtime_lock);
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +0200210 for (;;) {
Peter Zijlstra7f1e2ca2009-03-13 12:21:27 +0100211 unsigned long delta;
212 ktime_t soft, hard;
213
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +0200214 if (hrtimer_active(&rt_b->rt_period_timer))
215 break;
216
217 now = hrtimer_cb_get_time(&rt_b->rt_period_timer);
218 hrtimer_forward(&rt_b->rt_period_timer, now, rt_b->rt_period);
Peter Zijlstra7f1e2ca2009-03-13 12:21:27 +0100219
220 soft = hrtimer_get_softexpires(&rt_b->rt_period_timer);
221 hard = hrtimer_get_expires(&rt_b->rt_period_timer);
222 delta = ktime_to_ns(ktime_sub(hard, soft));
223 __hrtimer_start_range_ns(&rt_b->rt_period_timer, soft, delta,
Arun R Bharadwaj5c333862009-04-16 12:14:37 +0530224 HRTIMER_MODE_ABS_PINNED, 0);
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +0200225 }
Thomas Gleixner0986b112009-11-17 15:32:06 +0100226 raw_spin_unlock(&rt_b->rt_runtime_lock);
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +0200227}
228
229#ifdef CONFIG_RT_GROUP_SCHED
230static void destroy_rt_bandwidth(struct rt_bandwidth *rt_b)
231{
232 hrtimer_cancel(&rt_b->rt_period_timer);
233}
234#endif
235
Heiko Carstens712555e2008-04-28 11:33:07 +0200236/*
Peter Zijlstrac4a88492011-04-07 14:09:42 +0200237 * sched_domains_mutex serializes calls to init_sched_domains,
Heiko Carstens712555e2008-04-28 11:33:07 +0200238 * detach_destroy_domains and partition_sched_domains.
239 */
240static DEFINE_MUTEX(sched_domains_mutex);
241
Dhaval Giani7c941432010-01-20 13:26:18 +0100242#ifdef CONFIG_CGROUP_SCHED
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +0200243
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -0700244#include <linux/cgroup.h>
245
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +0200246struct cfs_rq;
247
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100248static LIST_HEAD(task_groups);
249
Paul Turnerab84d312011-07-21 09:43:28 -0700250struct cfs_bandwidth {
251#ifdef CONFIG_CFS_BANDWIDTH
252 raw_spinlock_t lock;
253 ktime_t period;
254 u64 quota;
255#endif
256};
257
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +0200258/* task group related information */
Ingo Molnar4cf86d72007-10-15 17:00:14 +0200259struct task_group {
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -0700260 struct cgroup_subsys_state css;
Arun R Bharadwaj6c415b92008-12-01 20:49:05 +0530261
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100262#ifdef CONFIG_FAIR_GROUP_SCHED
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +0200263 /* schedulable entities of this group on each cpu */
264 struct sched_entity **se;
265 /* runqueue "owned" by this group on each cpu */
266 struct cfs_rq **cfs_rq;
267 unsigned long shares;
Peter Zijlstra2069dd72010-11-15 15:47:00 -0800268
269 atomic_t load_weight;
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100270#endif
271
272#ifdef CONFIG_RT_GROUP_SCHED
273 struct sched_rt_entity **rt_se;
274 struct rt_rq **rt_rq;
275
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +0200276 struct rt_bandwidth rt_bandwidth;
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100277#endif
Srivatsa Vaddagiri6b2d7702008-01-25 21:08:00 +0100278
Srivatsa Vaddagiriae8393e2007-10-29 21:18:11 +0100279 struct rcu_head rcu;
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100280 struct list_head list;
Peter Zijlstraf473aa52008-04-19 19:45:00 +0200281
282 struct task_group *parent;
283 struct list_head siblings;
284 struct list_head children;
Mike Galbraith5091faa2010-11-30 14:18:03 +0100285
286#ifdef CONFIG_SCHED_AUTOGROUP
287 struct autogroup *autogroup;
288#endif
Paul Turnerab84d312011-07-21 09:43:28 -0700289
290 struct cfs_bandwidth cfs_bandwidth;
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +0200291};
292
Peter Zijlstra3d4b47b2010-11-15 15:47:01 -0800293/* task_group_lock serializes the addition/removal of task groups */
Peter Zijlstra8ed36992008-02-13 15:45:39 +0100294static DEFINE_SPINLOCK(task_group_lock);
Srivatsa Vaddagiriec2c5072008-01-25 21:07:59 +0100295
Cyrill Gorcunove9036b32009-10-26 22:24:14 +0300296#ifdef CONFIG_FAIR_GROUP_SCHED
297
Yong Zhang07e06b02011-01-07 15:17:36 +0800298# define ROOT_TASK_GROUP_LOAD NICE_0_LOAD
Srivatsa Vaddagiri24e377a2007-10-15 17:00:09 +0200299
Miao Xiecb4ad1f2008-04-28 12:54:56 +0800300/*
Lai Jiangshan2e084782008-06-12 16:42:58 +0800301 * A weight of 0 or 1 can cause arithmetics problems.
302 * A weight of a cfs_rq is the sum of weights of which entities
303 * are queued on this cfs_rq, so a weight of a entity should not be
304 * too large, so as the shares value of a task group.
Miao Xiecb4ad1f2008-04-28 12:54:56 +0800305 * (The default weight is 1024 - so there's no practical
306 * limitation from this.)
307 */
Mike Galbraithcd622872011-06-04 15:03:20 +0200308#define MIN_SHARES (1UL << 1)
309#define MAX_SHARES (1UL << 18)
Peter Zijlstra18d95a22008-04-19 19:45:00 +0200310
Yong Zhang07e06b02011-01-07 15:17:36 +0800311static int root_task_group_load = ROOT_TASK_GROUP_LOAD;
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100312#endif
313
314/* Default task group.
315 * Every task in system belong to this group at bootup.
316 */
Yong Zhang07e06b02011-01-07 15:17:36 +0800317struct task_group root_task_group;
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +0200318
Dhaval Giani7c941432010-01-20 13:26:18 +0100319#endif /* CONFIG_CGROUP_SCHED */
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +0200320
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200321/* CFS-related fields in a runqueue */
322struct cfs_rq {
323 struct load_weight load;
Paul Turner953bfcd2011-07-21 09:43:27 -0700324 unsigned long nr_running, h_nr_running;
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200325
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200326 u64 exec_clock;
Ingo Molnare9acbff2007-10-15 17:00:04 +0200327 u64 min_vruntime;
Peter Zijlstra3fe16982011-04-05 17:23:48 +0200328#ifndef CONFIG_64BIT
329 u64 min_vruntime_copy;
330#endif
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200331
332 struct rb_root tasks_timeline;
333 struct rb_node *rb_leftmost;
Peter Zijlstra4a55bd52008-04-19 19:45:00 +0200334
335 struct list_head tasks;
336 struct list_head *balance_iterator;
337
338 /*
339 * 'curr' points to currently running entity on this cfs_rq.
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200340 * It is set to NULL otherwise (i.e when none are currently running).
341 */
Rik van Rielac53db52011-02-01 09:51:03 -0500342 struct sched_entity *curr, *next, *last, *skip;
Peter Zijlstraddc97292007-10-15 17:00:10 +0200343
Rakib Mullick4934a4d2011-05-04 22:53:46 +0600344#ifdef CONFIG_SCHED_DEBUG
Peter Zijlstra5ac5c4d2008-11-10 10:46:32 +0100345 unsigned int nr_spread_over;
Rakib Mullick4934a4d2011-05-04 22:53:46 +0600346#endif
Peter Zijlstraddc97292007-10-15 17:00:10 +0200347
Ingo Molnar62160e32007-10-15 17:00:03 +0200348#ifdef CONFIG_FAIR_GROUP_SCHED
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200349 struct rq *rq; /* cpu runqueue to which this cfs_rq is attached */
350
Ingo Molnar41a2d6c2007-12-05 15:46:09 +0100351 /*
352 * leaf cfs_rqs are those that hold tasks (lowest schedulable entity in
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200353 * a hierarchy). Non-leaf lrqs hold other higher schedulable entities
354 * (like users, containers etc.)
355 *
356 * leaf_cfs_rq_list ties together list of leaf cfs_rq's in a cpu. This
357 * list is used during load balance.
358 */
Peter Zijlstra3d4b47b2010-11-15 15:47:01 -0800359 int on_list;
Ingo Molnar41a2d6c2007-12-05 15:46:09 +0100360 struct list_head leaf_cfs_rq_list;
361 struct task_group *tg; /* group that "owns" this runqueue */
Peter Zijlstrac09595f2008-06-27 13:41:14 +0200362
363#ifdef CONFIG_SMP
Peter Zijlstrac09595f2008-06-27 13:41:14 +0200364 /*
Peter Zijlstrac8cba852008-06-27 13:41:23 +0200365 * the part of load.weight contributed by tasks
Peter Zijlstrac09595f2008-06-27 13:41:14 +0200366 */
Peter Zijlstrac8cba852008-06-27 13:41:23 +0200367 unsigned long task_weight;
Peter Zijlstrac09595f2008-06-27 13:41:14 +0200368
Peter Zijlstrac8cba852008-06-27 13:41:23 +0200369 /*
370 * h_load = weight * f(tg)
371 *
372 * Where f(tg) is the recursive weight fraction assigned to
373 * this group.
374 */
375 unsigned long h_load;
Peter Zijlstrac09595f2008-06-27 13:41:14 +0200376
Peter Zijlstrac8cba852008-06-27 13:41:23 +0200377 /*
Paul Turner3b3d1902010-11-15 15:47:08 -0800378 * Maintaining per-cpu shares distribution for group scheduling
379 *
380 * load_stamp is the last time we updated the load average
381 * load_last is the last time we updated the load average and saw load
382 * load_unacc_exec_time is currently unaccounted execution time
Peter Zijlstrac8cba852008-06-27 13:41:23 +0200383 */
Peter Zijlstra2069dd72010-11-15 15:47:00 -0800384 u64 load_avg;
385 u64 load_period;
Paul Turner3b3d1902010-11-15 15:47:08 -0800386 u64 load_stamp, load_last, load_unacc_exec_time;
Peter Zijlstraf1d239f2008-06-27 13:41:38 +0200387
Peter Zijlstra2069dd72010-11-15 15:47:00 -0800388 unsigned long load_contribution;
Peter Zijlstrac09595f2008-06-27 13:41:14 +0200389#endif
Paul Turnerab84d312011-07-21 09:43:28 -0700390#ifdef CONFIG_CFS_BANDWIDTH
391 int runtime_enabled;
392 s64 runtime_remaining;
393#endif
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200394#endif
395};
396
Paul Turnerab84d312011-07-21 09:43:28 -0700397#ifdef CONFIG_FAIR_GROUP_SCHED
398#ifdef CONFIG_CFS_BANDWIDTH
399static inline struct cfs_bandwidth *tg_cfs_bandwidth(struct task_group *tg)
400{
401 return &tg->cfs_bandwidth;
402}
403
404static inline u64 default_cfs_period(void);
405
406static void init_cfs_bandwidth(struct cfs_bandwidth *cfs_b)
407{
408 raw_spin_lock_init(&cfs_b->lock);
409 cfs_b->quota = RUNTIME_INF;
410 cfs_b->period = ns_to_ktime(default_cfs_period());
411}
412
413static void init_cfs_rq_runtime(struct cfs_rq *cfs_rq)
414{
415 cfs_rq->runtime_enabled = 0;
416}
417
418static void destroy_cfs_bandwidth(struct cfs_bandwidth *cfs_b)
419{}
420#else
421static void init_cfs_rq_runtime(struct cfs_rq *cfs_rq) {}
422static void init_cfs_bandwidth(struct cfs_bandwidth *cfs_b) {}
423static void destroy_cfs_bandwidth(struct cfs_bandwidth *cfs_b) {}
424
425static inline struct cfs_bandwidth *tg_cfs_bandwidth(struct task_group *tg)
426{
427 return NULL;
428}
429#endif /* CONFIG_CFS_BANDWIDTH */
430#endif /* CONFIG_FAIR_GROUP_SCHED */
431
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200432/* Real-Time classes' related field in a runqueue: */
433struct rt_rq {
434 struct rt_prio_array active;
Steven Rostedt63489e42008-01-25 21:08:03 +0100435 unsigned long rt_nr_running;
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100436#if defined CONFIG_SMP || defined CONFIG_RT_GROUP_SCHED
Gregory Haskinse864c492008-12-29 09:39:49 -0500437 struct {
438 int curr; /* highest queued rt task prio */
Gregory Haskins398a1532009-01-14 09:10:04 -0500439#ifdef CONFIG_SMP
Gregory Haskinse864c492008-12-29 09:39:49 -0500440 int next; /* next highest */
Gregory Haskins398a1532009-01-14 09:10:04 -0500441#endif
Gregory Haskinse864c492008-12-29 09:39:49 -0500442 } highest_prio;
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100443#endif
Peter Zijlstrafa85ae22008-01-25 21:08:29 +0100444#ifdef CONFIG_SMP
Gregory Haskins73fe6aa2008-01-25 21:08:07 +0100445 unsigned long rt_nr_migratory;
Peter Zijlstraa1ba4d82009-04-01 18:40:15 +0200446 unsigned long rt_nr_total;
Gregory Haskinsa22d7fc2008-01-25 21:08:12 +0100447 int overloaded;
Gregory Haskins917b6272008-12-29 09:39:53 -0500448 struct plist_head pushable_tasks;
Peter Zijlstrafa85ae22008-01-25 21:08:29 +0100449#endif
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100450 int rt_throttled;
Peter Zijlstrafa85ae22008-01-25 21:08:29 +0100451 u64 rt_time;
Peter Zijlstraac086bc2008-04-19 19:44:58 +0200452 u64 rt_runtime;
Ingo Molnarea736ed2008-03-25 13:51:45 +0100453 /* Nests inside the rq lock: */
Thomas Gleixner0986b112009-11-17 15:32:06 +0100454 raw_spinlock_t rt_runtime_lock;
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100455
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100456#ifdef CONFIG_RT_GROUP_SCHED
Peter Zijlstra23b0fdf2008-02-13 15:45:39 +0100457 unsigned long rt_nr_boosted;
458
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100459 struct rq *rq;
460 struct list_head leaf_rt_rq_list;
461 struct task_group *tg;
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100462#endif
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200463};
464
Gregory Haskins57d885f2008-01-25 21:08:18 +0100465#ifdef CONFIG_SMP
466
467/*
468 * We add the notion of a root-domain which will be used to define per-domain
Ingo Molnar0eab9142008-01-25 21:08:19 +0100469 * variables. Each exclusive cpuset essentially defines an island domain by
470 * fully partitioning the member cpus from any other cpuset. Whenever a new
Gregory Haskins57d885f2008-01-25 21:08:18 +0100471 * exclusive cpuset is created, we also create and attach a new root-domain
472 * object.
473 *
Gregory Haskins57d885f2008-01-25 21:08:18 +0100474 */
475struct root_domain {
476 atomic_t refcount;
Richard Kennedy26a148e2011-07-15 11:41:31 +0100477 atomic_t rto_count;
Peter Zijlstradce840a2011-04-07 14:09:50 +0200478 struct rcu_head rcu;
Rusty Russellc6c49272008-11-25 02:35:05 +1030479 cpumask_var_t span;
480 cpumask_var_t online;
Gregory Haskins637f5082008-01-25 21:08:18 +0100481
Ingo Molnar0eab9142008-01-25 21:08:19 +0100482 /*
Gregory Haskins637f5082008-01-25 21:08:18 +0100483 * The "RT overload" flag: it gets set if a CPU has more than
484 * one runnable RT task.
485 */
Rusty Russellc6c49272008-11-25 02:35:05 +1030486 cpumask_var_t rto_mask;
Gregory Haskins6e0534f2008-05-12 21:21:01 +0200487 struct cpupri cpupri;
Gregory Haskins57d885f2008-01-25 21:08:18 +0100488};
489
Gregory Haskinsdc938522008-01-25 21:08:26 +0100490/*
491 * By default the system creates a single root-domain with all cpus as
492 * members (mimicking the global state we have today).
493 */
Gregory Haskins57d885f2008-01-25 21:08:18 +0100494static struct root_domain def_root_domain;
495
Christian Dietriched2d3722010-09-06 16:37:05 +0200496#endif /* CONFIG_SMP */
Gregory Haskins57d885f2008-01-25 21:08:18 +0100497
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200498/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 * This is the main, per-CPU runqueue data structure.
500 *
501 * Locking rule: those places that want to lock multiple runqueues
502 * (such as the load balancing or the thread migration code), lock
503 * acquire operations must be ordered by ascending &runqueue.
504 */
Ingo Molnar70b97a72006-07-03 00:25:42 -0700505struct rq {
Ingo Molnard8016492007-10-18 21:32:55 +0200506 /* runqueue lock: */
Thomas Gleixner05fa7852009-11-17 14:28:38 +0100507 raw_spinlock_t lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508
509 /*
510 * nr_running and cpu_load should be in the same cacheline because
511 * remote CPUs use both these fields when doing load calculation.
512 */
513 unsigned long nr_running;
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200514 #define CPU_LOAD_IDX_MAX 5
515 unsigned long cpu_load[CPU_LOAD_IDX_MAX];
Venkatesh Pallipadifdf3e952010-05-17 18:14:43 -0700516 unsigned long last_load_update_tick;
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -0700517#ifdef CONFIG_NO_HZ
Mike Galbraith39c0cbe2010-03-11 17:17:13 +0100518 u64 nohz_stamp;
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -0700519 unsigned char nohz_balance_kick;
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -0700520#endif
Mike Galbraith61eadef2011-04-29 08:36:50 +0200521 int skip_clock_update;
Mike Galbraitha64692a2010-03-11 17:16:20 +0100522
Ingo Molnard8016492007-10-18 21:32:55 +0200523 /* capture load from *all* tasks on this cpu: */
524 struct load_weight load;
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200525 unsigned long nr_load_updates;
526 u64 nr_switches;
527
528 struct cfs_rq cfs;
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100529 struct rt_rq rt;
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100530
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200531#ifdef CONFIG_FAIR_GROUP_SCHED
Ingo Molnard8016492007-10-18 21:32:55 +0200532 /* list of leaf cfs_rq on this cpu: */
533 struct list_head leaf_cfs_rq_list;
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100534#endif
535#ifdef CONFIG_RT_GROUP_SCHED
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100536 struct list_head leaf_rt_rq_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538
539 /*
540 * This is part of a global counter where only the total sum
541 * over all CPUs matters. A task can increase this counter on
542 * one CPU and if it got migrated afterwards it may decrease
543 * it on another CPU. Always updated under the runqueue lock:
544 */
545 unsigned long nr_uninterruptible;
546
Peter Zijlstra34f971f2010-09-22 13:53:15 +0200547 struct task_struct *curr, *idle, *stop;
Christoph Lameterc9819f42006-12-10 02:20:25 -0800548 unsigned long next_balance;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 struct mm_struct *prev_mm;
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200550
Peter Zijlstra3e51f332008-05-03 18:29:28 +0200551 u64 clock;
Venkatesh Pallipadi305e6832010-10-04 17:03:21 -0700552 u64 clock_task;
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200553
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 atomic_t nr_iowait;
555
556#ifdef CONFIG_SMP
Ingo Molnar0eab9142008-01-25 21:08:19 +0100557 struct root_domain *rd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 struct sched_domain *sd;
559
Peter Zijlstrae51fd5e2010-05-31 12:37:30 +0200560 unsigned long cpu_power;
561
Henrik Austada0a522c2009-02-13 20:35:45 +0100562 unsigned char idle_at_tick;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 /* For active balancing */
Gregory Haskins3f029d32009-07-29 11:08:47 -0400564 int post_schedule;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 int active_balance;
566 int push_cpu;
Tejun Heo969c7922010-05-06 18:49:21 +0200567 struct cpu_stop_work active_balance_work;
Ingo Molnard8016492007-10-18 21:32:55 +0200568 /* cpu of this runqueue: */
569 int cpu;
Gregory Haskins1f11eb62008-06-04 15:04:05 -0400570 int online;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571
Peter Zijlstrae9e92502009-09-01 10:34:37 +0200572 u64 rt_avg;
573 u64 age_stamp;
Mike Galbraith1b9508f2009-11-04 17:53:50 +0100574 u64 idle_stamp;
575 u64 avg_idle;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576#endif
577
Venkatesh Pallipadiaa483802010-10-04 17:03:22 -0700578#ifdef CONFIG_IRQ_TIME_ACCOUNTING
579 u64 prev_irq_time;
580#endif
Glauber Costae6e66852011-07-11 15:28:17 -0400581#ifdef CONFIG_PARAVIRT
582 u64 prev_steal_time;
583#endif
Glauber Costa095c0aa2011-07-11 15:28:18 -0400584#ifdef CONFIG_PARAVIRT_TIME_ACCOUNTING
585 u64 prev_steal_time_rq;
586#endif
Venkatesh Pallipadiaa483802010-10-04 17:03:22 -0700587
Thomas Gleixnerdce48a82009-04-11 10:43:41 +0200588 /* calc_load related fields */
589 unsigned long calc_load_update;
590 long calc_load_active;
591
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +0100592#ifdef CONFIG_SCHED_HRTICK
Peter Zijlstra31656512008-07-18 18:01:23 +0200593#ifdef CONFIG_SMP
594 int hrtick_csd_pending;
595 struct call_single_data hrtick_csd;
596#endif
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +0100597 struct hrtimer hrtick_timer;
598#endif
599
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600#ifdef CONFIG_SCHEDSTATS
601 /* latency stats */
602 struct sched_info rq_sched_info;
Ken Chen9c2c4802008-12-16 23:41:22 -0800603 unsigned long long rq_cpu_time;
604 /* could above be rq->cfs_rq.exec_clock + rq->rt_rq.rt_runtime ? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605
606 /* sys_sched_yield() stats */
Ken Chen480b9432007-10-18 21:32:56 +0200607 unsigned int yld_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608
609 /* schedule() stats */
Ken Chen480b9432007-10-18 21:32:56 +0200610 unsigned int sched_switch;
611 unsigned int sched_count;
612 unsigned int sched_goidle;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613
614 /* try_to_wake_up() stats */
Ken Chen480b9432007-10-18 21:32:56 +0200615 unsigned int ttwu_count;
616 unsigned int ttwu_local;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617#endif
Peter Zijlstra317f3942011-04-05 17:23:58 +0200618
619#ifdef CONFIG_SMP
620 struct task_struct *wake_list;
621#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622};
623
Fenghua Yuf34e3b62007-07-19 01:48:13 -0700624static DEFINE_PER_CPU_SHARED_ALIGNED(struct rq, runqueues);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625
Mike Galbraitha64692a2010-03-11 17:16:20 +0100626
Peter Zijlstra1e5a7402010-10-31 12:37:04 +0100627static void check_preempt_curr(struct rq *rq, struct task_struct *p, int flags);
Ingo Molnardd41f592007-07-09 18:51:59 +0200628
Christoph Lameter0a2966b2006-09-25 23:30:51 -0700629static inline int cpu_of(struct rq *rq)
630{
631#ifdef CONFIG_SMP
632 return rq->cpu;
633#else
634 return 0;
635#endif
636}
637
Paul E. McKenney497f0ab2010-02-22 17:04:51 -0800638#define rcu_dereference_check_sched_domain(p) \
Paul E. McKenneyd11c5632010-02-22 17:04:50 -0800639 rcu_dereference_check((p), \
Paul E. McKenneyd11c5632010-02-22 17:04:50 -0800640 lockdep_is_held(&sched_domains_mutex))
641
Ingo Molnar20d315d2007-07-09 18:51:58 +0200642/*
Nick Piggin674311d2005-06-25 14:57:27 -0700643 * The domain tree (rq->sd) is protected by RCU's quiescent state transition.
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -0700644 * See detach_destroy_domains: synchronize_sched for details.
Nick Piggin674311d2005-06-25 14:57:27 -0700645 *
646 * The domain tree of any CPU may only be accessed from within
647 * preempt-disabled sections.
648 */
Ingo Molnar48f24c42006-07-03 00:25:40 -0700649#define for_each_domain(cpu, __sd) \
Paul E. McKenney497f0ab2010-02-22 17:04:51 -0800650 for (__sd = rcu_dereference_check_sched_domain(cpu_rq(cpu)->sd); __sd; __sd = __sd->parent)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651
652#define cpu_rq(cpu) (&per_cpu(runqueues, (cpu)))
653#define this_rq() (&__get_cpu_var(runqueues))
654#define task_rq(p) cpu_rq(task_cpu(p))
655#define cpu_curr(cpu) (cpu_rq(cpu)->curr)
Hitoshi Mitake54d35f22009-06-29 14:44:57 +0900656#define raw_rq() (&__raw_get_cpu_var(runqueues))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657
Peter Zijlstradc61b1d2010-06-08 11:40:42 +0200658#ifdef CONFIG_CGROUP_SCHED
659
660/*
661 * Return the group to which this tasks belongs.
662 *
Peter Zijlstra6c6c54e2011-06-03 17:37:07 +0200663 * We use task_subsys_state_check() and extend the RCU verification with
664 * pi->lock and rq->lock because cpu_cgroup_attach() holds those locks for each
665 * task it moves into the cgroup. Therefore by holding either of those locks,
666 * we pin the task to the current cgroup.
Peter Zijlstradc61b1d2010-06-08 11:40:42 +0200667 */
668static inline struct task_group *task_group(struct task_struct *p)
669{
Mike Galbraith5091faa2010-11-30 14:18:03 +0100670 struct task_group *tg;
Peter Zijlstradc61b1d2010-06-08 11:40:42 +0200671 struct cgroup_subsys_state *css;
672
673 css = task_subsys_state_check(p, cpu_cgroup_subsys_id,
Peter Zijlstra6c6c54e2011-06-03 17:37:07 +0200674 lockdep_is_held(&p->pi_lock) ||
675 lockdep_is_held(&task_rq(p)->lock));
Mike Galbraith5091faa2010-11-30 14:18:03 +0100676 tg = container_of(css, struct task_group, css);
677
678 return autogroup_task_group(p, tg);
Peter Zijlstradc61b1d2010-06-08 11:40:42 +0200679}
680
681/* Change a task's cfs_rq and parent entity if it moves across CPUs/groups */
682static inline void set_task_rq(struct task_struct *p, unsigned int cpu)
683{
684#ifdef CONFIG_FAIR_GROUP_SCHED
685 p->se.cfs_rq = task_group(p)->cfs_rq[cpu];
686 p->se.parent = task_group(p)->se[cpu];
687#endif
688
689#ifdef CONFIG_RT_GROUP_SCHED
690 p->rt.rt_rq = task_group(p)->rt_rq[cpu];
691 p->rt.parent = task_group(p)->rt_se[cpu];
692#endif
693}
694
695#else /* CONFIG_CGROUP_SCHED */
696
697static inline void set_task_rq(struct task_struct *p, unsigned int cpu) { }
698static inline struct task_group *task_group(struct task_struct *p)
699{
700 return NULL;
701}
702
703#endif /* CONFIG_CGROUP_SCHED */
704
Peter Zijlstrafe44d622010-12-09 14:15:34 +0100705static void update_rq_clock_task(struct rq *rq, s64 delta);
Venkatesh Pallipadi305e6832010-10-04 17:03:21 -0700706
Peter Zijlstrafe44d622010-12-09 14:15:34 +0100707static void update_rq_clock(struct rq *rq)
Peter Zijlstra3e51f332008-05-03 18:29:28 +0200708{
Peter Zijlstrafe44d622010-12-09 14:15:34 +0100709 s64 delta;
Venkatesh Pallipadi305e6832010-10-04 17:03:21 -0700710
Mike Galbraith61eadef2011-04-29 08:36:50 +0200711 if (rq->skip_clock_update > 0)
Mike Galbraithf26f9af2010-12-08 11:05:42 +0100712 return;
Venkatesh Pallipadiaa483802010-10-04 17:03:22 -0700713
Peter Zijlstrafe44d622010-12-09 14:15:34 +0100714 delta = sched_clock_cpu(cpu_of(rq)) - rq->clock;
715 rq->clock += delta;
716 update_rq_clock_task(rq, delta);
Peter Zijlstra3e51f332008-05-03 18:29:28 +0200717}
718
Ingo Molnare436d802007-07-19 21:28:35 +0200719/*
Ingo Molnarbf5c91b2007-10-15 17:00:04 +0200720 * Tunables that become constants when CONFIG_SCHED_DEBUG is off:
721 */
722#ifdef CONFIG_SCHED_DEBUG
723# define const_debug __read_mostly
724#else
725# define const_debug static const
726#endif
727
Ingo Molnar017730c2008-05-12 21:20:52 +0200728/**
Randy Dunlap1fd06bb2011-03-15 16:12:30 -0700729 * runqueue_is_locked - Returns true if the current cpu runqueue is locked
Randy Dunlape17b38b2009-10-11 19:12:00 -0700730 * @cpu: the processor in question.
Ingo Molnar017730c2008-05-12 21:20:52 +0200731 *
Ingo Molnar017730c2008-05-12 21:20:52 +0200732 * This interface allows printk to be called with the runqueue lock
733 * held and know whether or not it is OK to wake up the klogd.
734 */
Andrew Morton89f19f02009-09-19 11:55:44 -0700735int runqueue_is_locked(int cpu)
Ingo Molnar017730c2008-05-12 21:20:52 +0200736{
Thomas Gleixner05fa7852009-11-17 14:28:38 +0100737 return raw_spin_is_locked(&cpu_rq(cpu)->lock);
Ingo Molnar017730c2008-05-12 21:20:52 +0200738}
739
Ingo Molnarbf5c91b2007-10-15 17:00:04 +0200740/*
741 * Debugging: various feature bits
742 */
Peter Zijlstraf00b45c2008-04-19 19:45:00 +0200743
744#define SCHED_FEAT(name, enabled) \
745 __SCHED_FEAT_##name ,
746
Ingo Molnarbf5c91b2007-10-15 17:00:04 +0200747enum {
Peter Zijlstraf00b45c2008-04-19 19:45:00 +0200748#include "sched_features.h"
Ingo Molnarbf5c91b2007-10-15 17:00:04 +0200749};
750
Peter Zijlstraf00b45c2008-04-19 19:45:00 +0200751#undef SCHED_FEAT
Ingo Molnarbf5c91b2007-10-15 17:00:04 +0200752
Peter Zijlstraf00b45c2008-04-19 19:45:00 +0200753#define SCHED_FEAT(name, enabled) \
754 (1UL << __SCHED_FEAT_##name) * enabled |
755
756const_debug unsigned int sysctl_sched_features =
757#include "sched_features.h"
758 0;
759
760#undef SCHED_FEAT
761
762#ifdef CONFIG_SCHED_DEBUG
763#define SCHED_FEAT(name, enabled) \
764 #name ,
765
Harvey Harrison983ed7a2008-04-24 18:17:55 -0700766static __read_mostly char *sched_feat_names[] = {
Peter Zijlstraf00b45c2008-04-19 19:45:00 +0200767#include "sched_features.h"
768 NULL
769};
770
771#undef SCHED_FEAT
772
Li Zefan34f3a812008-10-30 15:23:32 +0800773static int sched_feat_show(struct seq_file *m, void *v)
Peter Zijlstraf00b45c2008-04-19 19:45:00 +0200774{
Peter Zijlstraf00b45c2008-04-19 19:45:00 +0200775 int i;
776
777 for (i = 0; sched_feat_names[i]; i++) {
Li Zefan34f3a812008-10-30 15:23:32 +0800778 if (!(sysctl_sched_features & (1UL << i)))
779 seq_puts(m, "NO_");
780 seq_printf(m, "%s ", sched_feat_names[i]);
Peter Zijlstraf00b45c2008-04-19 19:45:00 +0200781 }
Li Zefan34f3a812008-10-30 15:23:32 +0800782 seq_puts(m, "\n");
Peter Zijlstraf00b45c2008-04-19 19:45:00 +0200783
Li Zefan34f3a812008-10-30 15:23:32 +0800784 return 0;
Peter Zijlstraf00b45c2008-04-19 19:45:00 +0200785}
786
787static ssize_t
788sched_feat_write(struct file *filp, const char __user *ubuf,
789 size_t cnt, loff_t *ppos)
790{
791 char buf[64];
Mathieu Desnoyers77401912010-09-13 17:47:00 -0400792 char *cmp;
Peter Zijlstraf00b45c2008-04-19 19:45:00 +0200793 int neg = 0;
794 int i;
795
796 if (cnt > 63)
797 cnt = 63;
798
799 if (copy_from_user(&buf, ubuf, cnt))
800 return -EFAULT;
801
802 buf[cnt] = 0;
Mathieu Desnoyers77401912010-09-13 17:47:00 -0400803 cmp = strstrip(buf);
Peter Zijlstraf00b45c2008-04-19 19:45:00 +0200804
Hillf Danton524429c2011-01-06 20:58:12 +0800805 if (strncmp(cmp, "NO_", 3) == 0) {
Peter Zijlstraf00b45c2008-04-19 19:45:00 +0200806 neg = 1;
807 cmp += 3;
808 }
809
810 for (i = 0; sched_feat_names[i]; i++) {
Mathieu Desnoyers77401912010-09-13 17:47:00 -0400811 if (strcmp(cmp, sched_feat_names[i]) == 0) {
Peter Zijlstraf00b45c2008-04-19 19:45:00 +0200812 if (neg)
813 sysctl_sched_features &= ~(1UL << i);
814 else
815 sysctl_sched_features |= (1UL << i);
816 break;
817 }
818 }
819
820 if (!sched_feat_names[i])
821 return -EINVAL;
822
Jan Blunck42994722009-11-20 17:40:37 +0100823 *ppos += cnt;
Peter Zijlstraf00b45c2008-04-19 19:45:00 +0200824
825 return cnt;
826}
827
Li Zefan34f3a812008-10-30 15:23:32 +0800828static int sched_feat_open(struct inode *inode, struct file *filp)
829{
830 return single_open(filp, sched_feat_show, NULL);
831}
832
Alexey Dobriyan828c0952009-10-01 15:43:56 -0700833static const struct file_operations sched_feat_fops = {
Li Zefan34f3a812008-10-30 15:23:32 +0800834 .open = sched_feat_open,
835 .write = sched_feat_write,
836 .read = seq_read,
837 .llseek = seq_lseek,
838 .release = single_release,
Peter Zijlstraf00b45c2008-04-19 19:45:00 +0200839};
840
841static __init int sched_init_debug(void)
842{
Peter Zijlstraf00b45c2008-04-19 19:45:00 +0200843 debugfs_create_file("sched_features", 0644, NULL, NULL,
844 &sched_feat_fops);
845
846 return 0;
847}
848late_initcall(sched_init_debug);
849
850#endif
851
852#define sched_feat(x) (sysctl_sched_features & (1UL << __SCHED_FEAT_##x))
Ingo Molnarbf5c91b2007-10-15 17:00:04 +0200853
854/*
Peter Zijlstrab82d9fd2007-11-09 22:39:39 +0100855 * Number of tasks to iterate in a single balance run.
856 * Limited because this is done with IRQs disabled.
857 */
858const_debug unsigned int sysctl_sched_nr_migrate = 32;
859
860/*
Peter Zijlstrae9e92502009-09-01 10:34:37 +0200861 * period over which we average the RT time consumption, measured
862 * in ms.
863 *
864 * default: 1s
865 */
866const_debug unsigned int sysctl_sched_time_avg = MSEC_PER_SEC;
867
868/*
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +0100869 * period over which we measure -rt task cpu usage in us.
Peter Zijlstrafa85ae22008-01-25 21:08:29 +0100870 * default: 1s
871 */
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +0100872unsigned int sysctl_sched_rt_period = 1000000;
Peter Zijlstrafa85ae22008-01-25 21:08:29 +0100873
Ingo Molnar6892b752008-02-13 14:02:36 +0100874static __read_mostly int scheduler_running;
875
Peter Zijlstrafa85ae22008-01-25 21:08:29 +0100876/*
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +0100877 * part of the period that we allow rt tasks to run in us.
878 * default: 0.95s
Peter Zijlstrafa85ae22008-01-25 21:08:29 +0100879 */
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +0100880int sysctl_sched_rt_runtime = 950000;
881
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +0200882static inline u64 global_rt_period(void)
883{
884 return (u64)sysctl_sched_rt_period * NSEC_PER_USEC;
885}
886
887static inline u64 global_rt_runtime(void)
888{
roel kluine26873b2008-07-22 16:51:15 -0400889 if (sysctl_sched_rt_runtime < 0)
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +0200890 return RUNTIME_INF;
891
892 return (u64)sysctl_sched_rt_runtime * NSEC_PER_USEC;
893}
Peter Zijlstrafa85ae22008-01-25 21:08:29 +0100894
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895#ifndef prepare_arch_switch
Nick Piggin4866cde2005-06-25 14:57:23 -0700896# define prepare_arch_switch(next) do { } while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897#endif
Nick Piggin4866cde2005-06-25 14:57:23 -0700898#ifndef finish_arch_switch
899# define finish_arch_switch(prev) do { } while (0)
900#endif
901
Dmitry Adamushko051a1d12007-12-18 15:21:13 +0100902static inline int task_current(struct rq *rq, struct task_struct *p)
903{
904 return rq->curr == p;
905}
906
Ingo Molnar70b97a72006-07-03 00:25:42 -0700907static inline int task_running(struct rq *rq, struct task_struct *p)
Nick Piggin4866cde2005-06-25 14:57:23 -0700908{
Peter Zijlstra3ca7a442011-04-05 17:23:40 +0200909#ifdef CONFIG_SMP
910 return p->on_cpu;
911#else
Dmitry Adamushko051a1d12007-12-18 15:21:13 +0100912 return task_current(rq, p);
Peter Zijlstra3ca7a442011-04-05 17:23:40 +0200913#endif
Nick Piggin4866cde2005-06-25 14:57:23 -0700914}
915
Peter Zijlstra3ca7a442011-04-05 17:23:40 +0200916#ifndef __ARCH_WANT_UNLOCKED_CTXSW
Ingo Molnar70b97a72006-07-03 00:25:42 -0700917static inline void prepare_lock_switch(struct rq *rq, struct task_struct *next)
Nick Piggin4866cde2005-06-25 14:57:23 -0700918{
Peter Zijlstra3ca7a442011-04-05 17:23:40 +0200919#ifdef CONFIG_SMP
920 /*
921 * We can optimise this out completely for !SMP, because the
922 * SMP rebalancing from interrupt is the only thing that cares
923 * here.
924 */
925 next->on_cpu = 1;
926#endif
Nick Piggin4866cde2005-06-25 14:57:23 -0700927}
928
Ingo Molnar70b97a72006-07-03 00:25:42 -0700929static inline void finish_lock_switch(struct rq *rq, struct task_struct *prev)
Nick Piggin4866cde2005-06-25 14:57:23 -0700930{
Peter Zijlstra3ca7a442011-04-05 17:23:40 +0200931#ifdef CONFIG_SMP
932 /*
933 * After ->on_cpu is cleared, the task can be moved to a different CPU.
934 * We must ensure this doesn't happen until the switch is completely
935 * finished.
936 */
937 smp_wmb();
938 prev->on_cpu = 0;
939#endif
Ingo Molnarda04c032005-09-13 11:17:59 +0200940#ifdef CONFIG_DEBUG_SPINLOCK
941 /* this is a valid case when another task releases the spinlock */
942 rq->lock.owner = current;
943#endif
Ingo Molnar8a25d5d2006-07-03 00:24:54 -0700944 /*
945 * If we are tracking spinlock dependencies then we have to
946 * fix up the runqueue lock - which gets 'carried over' from
947 * prev into current:
948 */
949 spin_acquire(&rq->lock.dep_map, 0, 0, _THIS_IP_);
950
Thomas Gleixner05fa7852009-11-17 14:28:38 +0100951 raw_spin_unlock_irq(&rq->lock);
Nick Piggin4866cde2005-06-25 14:57:23 -0700952}
953
954#else /* __ARCH_WANT_UNLOCKED_CTXSW */
Ingo Molnar70b97a72006-07-03 00:25:42 -0700955static inline void prepare_lock_switch(struct rq *rq, struct task_struct *next)
Nick Piggin4866cde2005-06-25 14:57:23 -0700956{
957#ifdef CONFIG_SMP
958 /*
959 * We can optimise this out completely for !SMP, because the
960 * SMP rebalancing from interrupt is the only thing that cares
961 * here.
962 */
Peter Zijlstra3ca7a442011-04-05 17:23:40 +0200963 next->on_cpu = 1;
Nick Piggin4866cde2005-06-25 14:57:23 -0700964#endif
965#ifdef __ARCH_WANT_INTERRUPTS_ON_CTXSW
Thomas Gleixner05fa7852009-11-17 14:28:38 +0100966 raw_spin_unlock_irq(&rq->lock);
Nick Piggin4866cde2005-06-25 14:57:23 -0700967#else
Thomas Gleixner05fa7852009-11-17 14:28:38 +0100968 raw_spin_unlock(&rq->lock);
Nick Piggin4866cde2005-06-25 14:57:23 -0700969#endif
970}
971
Ingo Molnar70b97a72006-07-03 00:25:42 -0700972static inline void finish_lock_switch(struct rq *rq, struct task_struct *prev)
Nick Piggin4866cde2005-06-25 14:57:23 -0700973{
974#ifdef CONFIG_SMP
975 /*
Peter Zijlstra3ca7a442011-04-05 17:23:40 +0200976 * After ->on_cpu is cleared, the task can be moved to a different CPU.
Nick Piggin4866cde2005-06-25 14:57:23 -0700977 * We must ensure this doesn't happen until the switch is completely
978 * finished.
979 */
980 smp_wmb();
Peter Zijlstra3ca7a442011-04-05 17:23:40 +0200981 prev->on_cpu = 0;
Nick Piggin4866cde2005-06-25 14:57:23 -0700982#endif
983#ifndef __ARCH_WANT_INTERRUPTS_ON_CTXSW
984 local_irq_enable();
985#endif
986}
987#endif /* __ARCH_WANT_UNLOCKED_CTXSW */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988
989/*
Peter Zijlstra0122ec52011-04-05 17:23:51 +0200990 * __task_rq_lock - lock the rq @p resides on.
Ingo Molnarb29739f2006-06-27 02:54:51 -0700991 */
Ingo Molnar70b97a72006-07-03 00:25:42 -0700992static inline struct rq *__task_rq_lock(struct task_struct *p)
Ingo Molnarb29739f2006-06-27 02:54:51 -0700993 __acquires(rq->lock)
994{
Peter Zijlstra0970d292010-02-15 14:45:54 +0100995 struct rq *rq;
996
Peter Zijlstra0122ec52011-04-05 17:23:51 +0200997 lockdep_assert_held(&p->pi_lock);
998
Andi Kleen3a5c3592007-10-15 17:00:14 +0200999 for (;;) {
Peter Zijlstra0970d292010-02-15 14:45:54 +01001000 rq = task_rq(p);
Thomas Gleixner05fa7852009-11-17 14:28:38 +01001001 raw_spin_lock(&rq->lock);
Peter Zijlstra65cc8e42010-03-25 21:05:16 +01001002 if (likely(rq == task_rq(p)))
Andi Kleen3a5c3592007-10-15 17:00:14 +02001003 return rq;
Thomas Gleixner05fa7852009-11-17 14:28:38 +01001004 raw_spin_unlock(&rq->lock);
Ingo Molnarb29739f2006-06-27 02:54:51 -07001005 }
Ingo Molnarb29739f2006-06-27 02:54:51 -07001006}
1007
1008/*
Peter Zijlstra0122ec52011-04-05 17:23:51 +02001009 * task_rq_lock - lock p->pi_lock and lock the rq @p resides on.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010 */
Ingo Molnar70b97a72006-07-03 00:25:42 -07001011static struct rq *task_rq_lock(struct task_struct *p, unsigned long *flags)
Peter Zijlstra0122ec52011-04-05 17:23:51 +02001012 __acquires(p->pi_lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013 __acquires(rq->lock)
1014{
Ingo Molnar70b97a72006-07-03 00:25:42 -07001015 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016
Andi Kleen3a5c3592007-10-15 17:00:14 +02001017 for (;;) {
Peter Zijlstra0122ec52011-04-05 17:23:51 +02001018 raw_spin_lock_irqsave(&p->pi_lock, *flags);
Andi Kleen3a5c3592007-10-15 17:00:14 +02001019 rq = task_rq(p);
Thomas Gleixner05fa7852009-11-17 14:28:38 +01001020 raw_spin_lock(&rq->lock);
Peter Zijlstra65cc8e42010-03-25 21:05:16 +01001021 if (likely(rq == task_rq(p)))
Andi Kleen3a5c3592007-10-15 17:00:14 +02001022 return rq;
Peter Zijlstra0122ec52011-04-05 17:23:51 +02001023 raw_spin_unlock(&rq->lock);
1024 raw_spin_unlock_irqrestore(&p->pi_lock, *flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026}
1027
Alexey Dobriyana9957442007-10-15 17:00:13 +02001028static void __task_rq_unlock(struct rq *rq)
Ingo Molnarb29739f2006-06-27 02:54:51 -07001029 __releases(rq->lock)
1030{
Thomas Gleixner05fa7852009-11-17 14:28:38 +01001031 raw_spin_unlock(&rq->lock);
Ingo Molnarb29739f2006-06-27 02:54:51 -07001032}
1033
Peter Zijlstra0122ec52011-04-05 17:23:51 +02001034static inline void
1035task_rq_unlock(struct rq *rq, struct task_struct *p, unsigned long *flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036 __releases(rq->lock)
Peter Zijlstra0122ec52011-04-05 17:23:51 +02001037 __releases(p->pi_lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038{
Peter Zijlstra0122ec52011-04-05 17:23:51 +02001039 raw_spin_unlock(&rq->lock);
1040 raw_spin_unlock_irqrestore(&p->pi_lock, *flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041}
1042
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043/*
Robert P. J. Daycc2a73b2006-12-10 02:20:00 -08001044 * this_rq_lock - lock this runqueue and disable interrupts.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045 */
Alexey Dobriyana9957442007-10-15 17:00:13 +02001046static struct rq *this_rq_lock(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047 __acquires(rq->lock)
1048{
Ingo Molnar70b97a72006-07-03 00:25:42 -07001049 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050
1051 local_irq_disable();
1052 rq = this_rq();
Thomas Gleixner05fa7852009-11-17 14:28:38 +01001053 raw_spin_lock(&rq->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054
1055 return rq;
1056}
1057
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01001058#ifdef CONFIG_SCHED_HRTICK
1059/*
1060 * Use HR-timers to deliver accurate preemption points.
1061 *
1062 * Its all a bit involved since we cannot program an hrt while holding the
1063 * rq->lock. So what we do is store a state in in rq->hrtick_* and ask for a
1064 * reschedule event.
1065 *
1066 * When we get rescheduled we reprogram the hrtick_timer outside of the
1067 * rq->lock.
1068 */
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01001069
1070/*
1071 * Use hrtick when:
1072 * - enabled by features
1073 * - hrtimer is actually high res
1074 */
1075static inline int hrtick_enabled(struct rq *rq)
1076{
1077 if (!sched_feat(HRTICK))
1078 return 0;
Ingo Molnarba420592008-07-20 11:02:06 +02001079 if (!cpu_active(cpu_of(rq)))
Peter Zijlstrab328ca12008-04-29 10:02:46 +02001080 return 0;
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01001081 return hrtimer_is_hres_active(&rq->hrtick_timer);
1082}
1083
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01001084static void hrtick_clear(struct rq *rq)
1085{
1086 if (hrtimer_active(&rq->hrtick_timer))
1087 hrtimer_cancel(&rq->hrtick_timer);
1088}
1089
1090/*
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01001091 * High-resolution timer tick.
1092 * Runs from hardirq context with interrupts disabled.
1093 */
1094static enum hrtimer_restart hrtick(struct hrtimer *timer)
1095{
1096 struct rq *rq = container_of(timer, struct rq, hrtick_timer);
1097
1098 WARN_ON_ONCE(cpu_of(rq) != smp_processor_id());
1099
Thomas Gleixner05fa7852009-11-17 14:28:38 +01001100 raw_spin_lock(&rq->lock);
Peter Zijlstra3e51f332008-05-03 18:29:28 +02001101 update_rq_clock(rq);
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01001102 rq->curr->sched_class->task_tick(rq, rq->curr, 1);
Thomas Gleixner05fa7852009-11-17 14:28:38 +01001103 raw_spin_unlock(&rq->lock);
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01001104
1105 return HRTIMER_NORESTART;
1106}
1107
Rabin Vincent95e904c2008-05-11 05:55:33 +05301108#ifdef CONFIG_SMP
Peter Zijlstra31656512008-07-18 18:01:23 +02001109/*
1110 * called from hardirq (IPI) context
1111 */
1112static void __hrtick_start(void *arg)
Peter Zijlstrab328ca12008-04-29 10:02:46 +02001113{
Peter Zijlstra31656512008-07-18 18:01:23 +02001114 struct rq *rq = arg;
Peter Zijlstrab328ca12008-04-29 10:02:46 +02001115
Thomas Gleixner05fa7852009-11-17 14:28:38 +01001116 raw_spin_lock(&rq->lock);
Peter Zijlstra31656512008-07-18 18:01:23 +02001117 hrtimer_restart(&rq->hrtick_timer);
1118 rq->hrtick_csd_pending = 0;
Thomas Gleixner05fa7852009-11-17 14:28:38 +01001119 raw_spin_unlock(&rq->lock);
Peter Zijlstrab328ca12008-04-29 10:02:46 +02001120}
1121
Peter Zijlstra31656512008-07-18 18:01:23 +02001122/*
1123 * Called to set the hrtick timer state.
1124 *
1125 * called with rq->lock held and irqs disabled
1126 */
1127static void hrtick_start(struct rq *rq, u64 delay)
Peter Zijlstrab328ca12008-04-29 10:02:46 +02001128{
Peter Zijlstra31656512008-07-18 18:01:23 +02001129 struct hrtimer *timer = &rq->hrtick_timer;
1130 ktime_t time = ktime_add_ns(timer->base->get_time(), delay);
Peter Zijlstrab328ca12008-04-29 10:02:46 +02001131
Arjan van de Vencc584b22008-09-01 15:02:30 -07001132 hrtimer_set_expires(timer, time);
Peter Zijlstra31656512008-07-18 18:01:23 +02001133
1134 if (rq == this_rq()) {
1135 hrtimer_restart(timer);
1136 } else if (!rq->hrtick_csd_pending) {
Peter Zijlstra6e275632009-02-25 13:59:48 +01001137 __smp_call_function_single(cpu_of(rq), &rq->hrtick_csd, 0);
Peter Zijlstra31656512008-07-18 18:01:23 +02001138 rq->hrtick_csd_pending = 1;
1139 }
Peter Zijlstrab328ca12008-04-29 10:02:46 +02001140}
1141
1142static int
1143hotplug_hrtick(struct notifier_block *nfb, unsigned long action, void *hcpu)
1144{
1145 int cpu = (int)(long)hcpu;
1146
1147 switch (action) {
1148 case CPU_UP_CANCELED:
1149 case CPU_UP_CANCELED_FROZEN:
1150 case CPU_DOWN_PREPARE:
1151 case CPU_DOWN_PREPARE_FROZEN:
1152 case CPU_DEAD:
1153 case CPU_DEAD_FROZEN:
Peter Zijlstra31656512008-07-18 18:01:23 +02001154 hrtick_clear(cpu_rq(cpu));
Peter Zijlstrab328ca12008-04-29 10:02:46 +02001155 return NOTIFY_OK;
1156 }
1157
1158 return NOTIFY_DONE;
1159}
1160
Rakib Mullickfa748202008-09-22 14:55:45 -07001161static __init void init_hrtick(void)
Peter Zijlstrab328ca12008-04-29 10:02:46 +02001162{
1163 hotcpu_notifier(hotplug_hrtick, 0);
1164}
Peter Zijlstra31656512008-07-18 18:01:23 +02001165#else
1166/*
1167 * Called to set the hrtick timer state.
1168 *
1169 * called with rq->lock held and irqs disabled
1170 */
1171static void hrtick_start(struct rq *rq, u64 delay)
1172{
Peter Zijlstra7f1e2ca2009-03-13 12:21:27 +01001173 __hrtimer_start_range_ns(&rq->hrtick_timer, ns_to_ktime(delay), 0,
Arun R Bharadwaj5c333862009-04-16 12:14:37 +05301174 HRTIMER_MODE_REL_PINNED, 0);
Peter Zijlstra31656512008-07-18 18:01:23 +02001175}
1176
Andrew Morton006c75f2008-09-22 14:55:46 -07001177static inline void init_hrtick(void)
Peter Zijlstra31656512008-07-18 18:01:23 +02001178{
1179}
Rabin Vincent95e904c2008-05-11 05:55:33 +05301180#endif /* CONFIG_SMP */
Peter Zijlstrab328ca12008-04-29 10:02:46 +02001181
1182static void init_rq_hrtick(struct rq *rq)
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01001183{
Peter Zijlstra31656512008-07-18 18:01:23 +02001184#ifdef CONFIG_SMP
1185 rq->hrtick_csd_pending = 0;
1186
1187 rq->hrtick_csd.flags = 0;
1188 rq->hrtick_csd.func = __hrtick_start;
1189 rq->hrtick_csd.info = rq;
1190#endif
1191
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01001192 hrtimer_init(&rq->hrtick_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
1193 rq->hrtick_timer.function = hrtick;
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01001194}
Andrew Morton006c75f2008-09-22 14:55:46 -07001195#else /* CONFIG_SCHED_HRTICK */
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01001196static inline void hrtick_clear(struct rq *rq)
1197{
1198}
1199
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01001200static inline void init_rq_hrtick(struct rq *rq)
1201{
1202}
1203
Peter Zijlstrab328ca12008-04-29 10:02:46 +02001204static inline void init_hrtick(void)
1205{
1206}
Andrew Morton006c75f2008-09-22 14:55:46 -07001207#endif /* CONFIG_SCHED_HRTICK */
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01001208
Ingo Molnar1b9f19c2007-07-09 18:51:59 +02001209/*
Ingo Molnarc24d20d2007-07-09 18:51:59 +02001210 * resched_task - mark a task 'to be rescheduled now'.
1211 *
1212 * On UP this means the setting of the need_resched flag, on SMP it
1213 * might also involve a cross-CPU call to trigger the scheduler on
1214 * the target CPU.
1215 */
1216#ifdef CONFIG_SMP
1217
1218#ifndef tsk_is_polling
1219#define tsk_is_polling(t) test_tsk_thread_flag(t, TIF_POLLING_NRFLAG)
1220#endif
1221
Peter Zijlstra31656512008-07-18 18:01:23 +02001222static void resched_task(struct task_struct *p)
Ingo Molnarc24d20d2007-07-09 18:51:59 +02001223{
1224 int cpu;
1225
Thomas Gleixner05fa7852009-11-17 14:28:38 +01001226 assert_raw_spin_locked(&task_rq(p)->lock);
Ingo Molnarc24d20d2007-07-09 18:51:59 +02001227
Lai Jiangshan5ed0cec2009-03-06 19:40:20 +08001228 if (test_tsk_need_resched(p))
Ingo Molnarc24d20d2007-07-09 18:51:59 +02001229 return;
1230
Lai Jiangshan5ed0cec2009-03-06 19:40:20 +08001231 set_tsk_need_resched(p);
Ingo Molnarc24d20d2007-07-09 18:51:59 +02001232
1233 cpu = task_cpu(p);
1234 if (cpu == smp_processor_id())
1235 return;
1236
1237 /* NEED_RESCHED must be visible before we test polling */
1238 smp_mb();
1239 if (!tsk_is_polling(p))
1240 smp_send_reschedule(cpu);
1241}
1242
1243static void resched_cpu(int cpu)
1244{
1245 struct rq *rq = cpu_rq(cpu);
1246 unsigned long flags;
1247
Thomas Gleixner05fa7852009-11-17 14:28:38 +01001248 if (!raw_spin_trylock_irqsave(&rq->lock, flags))
Ingo Molnarc24d20d2007-07-09 18:51:59 +02001249 return;
1250 resched_task(cpu_curr(cpu));
Thomas Gleixner05fa7852009-11-17 14:28:38 +01001251 raw_spin_unlock_irqrestore(&rq->lock, flags);
Ingo Molnarc24d20d2007-07-09 18:51:59 +02001252}
Thomas Gleixner06d83082008-03-22 09:20:24 +01001253
1254#ifdef CONFIG_NO_HZ
1255/*
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07001256 * In the semi idle case, use the nearest busy cpu for migrating timers
1257 * from an idle cpu. This is good for power-savings.
1258 *
1259 * We don't do similar optimization for completely idle system, as
1260 * selecting an idle cpu will add more delays to the timers than intended
1261 * (as that cpu's timer base may not be uptodate wrt jiffies etc).
1262 */
1263int get_nohz_timer_target(void)
1264{
1265 int cpu = smp_processor_id();
1266 int i;
1267 struct sched_domain *sd;
1268
Peter Zijlstra057f3fa2011-04-18 11:24:34 +02001269 rcu_read_lock();
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07001270 for_each_domain(cpu, sd) {
Peter Zijlstra057f3fa2011-04-18 11:24:34 +02001271 for_each_cpu(i, sched_domain_span(sd)) {
1272 if (!idle_cpu(i)) {
1273 cpu = i;
1274 goto unlock;
1275 }
1276 }
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07001277 }
Peter Zijlstra057f3fa2011-04-18 11:24:34 +02001278unlock:
1279 rcu_read_unlock();
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07001280 return cpu;
1281}
1282/*
Thomas Gleixner06d83082008-03-22 09:20:24 +01001283 * When add_timer_on() enqueues a timer into the timer wheel of an
1284 * idle CPU then this timer might expire before the next timer event
1285 * which is scheduled to wake up that CPU. In case of a completely
1286 * idle system the next event might even be infinite time into the
1287 * future. wake_up_idle_cpu() ensures that the CPU is woken up and
1288 * leaves the inner idle loop so the newly added timer is taken into
1289 * account when the CPU goes back to idle and evaluates the timer
1290 * wheel for the next timer event.
1291 */
1292void wake_up_idle_cpu(int cpu)
1293{
1294 struct rq *rq = cpu_rq(cpu);
1295
1296 if (cpu == smp_processor_id())
1297 return;
1298
1299 /*
1300 * This is safe, as this function is called with the timer
1301 * wheel base lock of (cpu) held. When the CPU is on the way
1302 * to idle and has not yet set rq->curr to idle then it will
1303 * be serialized on the timer wheel base lock and take the new
1304 * timer into account automatically.
1305 */
1306 if (rq->curr != rq->idle)
1307 return;
1308
1309 /*
1310 * We can set TIF_RESCHED on the idle task of the other CPU
1311 * lockless. The worst case is that the other CPU runs the
1312 * idle task through an additional NOOP schedule()
1313 */
Lai Jiangshan5ed0cec2009-03-06 19:40:20 +08001314 set_tsk_need_resched(rq->idle);
Thomas Gleixner06d83082008-03-22 09:20:24 +01001315
1316 /* NEED_RESCHED must be visible before we test polling */
1317 smp_mb();
1318 if (!tsk_is_polling(rq->idle))
1319 smp_send_reschedule(cpu);
1320}
Mike Galbraith39c0cbe2010-03-11 17:17:13 +01001321
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +02001322#endif /* CONFIG_NO_HZ */
Thomas Gleixner06d83082008-03-22 09:20:24 +01001323
Peter Zijlstrae9e92502009-09-01 10:34:37 +02001324static u64 sched_avg_period(void)
1325{
1326 return (u64)sysctl_sched_time_avg * NSEC_PER_MSEC / 2;
1327}
1328
1329static void sched_avg_update(struct rq *rq)
1330{
1331 s64 period = sched_avg_period();
1332
1333 while ((s64)(rq->clock - rq->age_stamp) > period) {
Will Deacon0d98bb22010-05-24 12:11:43 -07001334 /*
1335 * Inline assembly required to prevent the compiler
1336 * optimising this loop into a divmod call.
1337 * See __iter_div_u64_rem() for another example of this.
1338 */
1339 asm("" : "+rm" (rq->age_stamp));
Peter Zijlstrae9e92502009-09-01 10:34:37 +02001340 rq->age_stamp += period;
1341 rq->rt_avg /= 2;
1342 }
1343}
1344
1345static void sched_rt_avg_update(struct rq *rq, u64 rt_delta)
1346{
1347 rq->rt_avg += rt_delta;
1348 sched_avg_update(rq);
1349}
1350
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +02001351#else /* !CONFIG_SMP */
Peter Zijlstra31656512008-07-18 18:01:23 +02001352static void resched_task(struct task_struct *p)
Ingo Molnarc24d20d2007-07-09 18:51:59 +02001353{
Thomas Gleixner05fa7852009-11-17 14:28:38 +01001354 assert_raw_spin_locked(&task_rq(p)->lock);
Peter Zijlstra31656512008-07-18 18:01:23 +02001355 set_tsk_need_resched(p);
Ingo Molnarc24d20d2007-07-09 18:51:59 +02001356}
Peter Zijlstrae9e92502009-09-01 10:34:37 +02001357
1358static void sched_rt_avg_update(struct rq *rq, u64 rt_delta)
1359{
1360}
Suresh Siddhada2b71e2010-08-23 13:42:51 -07001361
1362static void sched_avg_update(struct rq *rq)
1363{
1364}
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +02001365#endif /* CONFIG_SMP */
Ingo Molnarc24d20d2007-07-09 18:51:59 +02001366
Ingo Molnar45bf76d2007-07-09 18:51:59 +02001367#if BITS_PER_LONG == 32
1368# define WMULT_CONST (~0UL)
1369#else
1370# define WMULT_CONST (1UL << 32)
1371#endif
1372
1373#define WMULT_SHIFT 32
1374
Ingo Molnar194081e2007-08-09 11:16:51 +02001375/*
1376 * Shift right and round:
1377 */
Ingo Molnarcf2ab462007-09-05 14:32:49 +02001378#define SRR(x, y) (((x) + (1UL << ((y) - 1))) >> (y))
Ingo Molnar194081e2007-08-09 11:16:51 +02001379
Peter Zijlstraa7be37a2008-06-27 13:41:11 +02001380/*
1381 * delta *= weight / lw
1382 */
Ingo Molnarcb1c4fc2007-08-02 17:41:40 +02001383static unsigned long
Ingo Molnar45bf76d2007-07-09 18:51:59 +02001384calc_delta_mine(unsigned long delta_exec, unsigned long weight,
1385 struct load_weight *lw)
1386{
1387 u64 tmp;
1388
Nikhil Raoc8b28112011-05-18 14:37:48 -07001389 /*
1390 * weight can be less than 2^SCHED_LOAD_RESOLUTION for task group sched
1391 * entities since MIN_SHARES = 2. Treat weight as 1 if less than
1392 * 2^SCHED_LOAD_RESOLUTION.
1393 */
1394 if (likely(weight > (1UL << SCHED_LOAD_RESOLUTION)))
1395 tmp = (u64)delta_exec * scale_load_down(weight);
1396 else
1397 tmp = (u64)delta_exec;
Stephan Baerwolfdb670da2011-05-11 18:03:29 +02001398
Lai Jiangshan7a232e02008-06-12 16:43:07 +08001399 if (!lw->inv_weight) {
Nikhil Raoc8b28112011-05-18 14:37:48 -07001400 unsigned long w = scale_load_down(lw->weight);
1401
1402 if (BITS_PER_LONG > 32 && unlikely(w >= WMULT_CONST))
Lai Jiangshan7a232e02008-06-12 16:43:07 +08001403 lw->inv_weight = 1;
Nikhil Raoc8b28112011-05-18 14:37:48 -07001404 else if (unlikely(!w))
1405 lw->inv_weight = WMULT_CONST;
Lai Jiangshan7a232e02008-06-12 16:43:07 +08001406 else
Nikhil Raoc8b28112011-05-18 14:37:48 -07001407 lw->inv_weight = WMULT_CONST / w;
Lai Jiangshan7a232e02008-06-12 16:43:07 +08001408 }
Ingo Molnar45bf76d2007-07-09 18:51:59 +02001409
Ingo Molnar45bf76d2007-07-09 18:51:59 +02001410 /*
1411 * Check whether we'd overflow the 64-bit multiplication:
1412 */
Ingo Molnar194081e2007-08-09 11:16:51 +02001413 if (unlikely(tmp > WMULT_CONST))
Ingo Molnarcf2ab462007-09-05 14:32:49 +02001414 tmp = SRR(SRR(tmp, WMULT_SHIFT/2) * lw->inv_weight,
Ingo Molnar194081e2007-08-09 11:16:51 +02001415 WMULT_SHIFT/2);
1416 else
Ingo Molnarcf2ab462007-09-05 14:32:49 +02001417 tmp = SRR(tmp * lw->inv_weight, WMULT_SHIFT);
Ingo Molnar45bf76d2007-07-09 18:51:59 +02001418
Ingo Molnarecf691d2007-08-02 17:41:40 +02001419 return (unsigned long)min(tmp, (u64)(unsigned long)LONG_MAX);
Ingo Molnar45bf76d2007-07-09 18:51:59 +02001420}
1421
Ingo Molnar10919852007-10-15 17:00:04 +02001422static inline void update_load_add(struct load_weight *lw, unsigned long inc)
Ingo Molnar45bf76d2007-07-09 18:51:59 +02001423{
1424 lw->weight += inc;
Ingo Molnare89996a2008-03-14 23:48:28 +01001425 lw->inv_weight = 0;
Ingo Molnar45bf76d2007-07-09 18:51:59 +02001426}
1427
Ingo Molnar10919852007-10-15 17:00:04 +02001428static inline void update_load_sub(struct load_weight *lw, unsigned long dec)
Ingo Molnar45bf76d2007-07-09 18:51:59 +02001429{
1430 lw->weight -= dec;
Ingo Molnare89996a2008-03-14 23:48:28 +01001431 lw->inv_weight = 0;
Ingo Molnar45bf76d2007-07-09 18:51:59 +02001432}
1433
Peter Zijlstra2069dd72010-11-15 15:47:00 -08001434static inline void update_load_set(struct load_weight *lw, unsigned long w)
1435{
1436 lw->weight = w;
1437 lw->inv_weight = 0;
1438}
1439
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440/*
Peter Williams2dd73a42006-06-27 02:54:34 -07001441 * To aid in avoiding the subversion of "niceness" due to uneven distribution
1442 * of tasks with abnormal "nice" values across CPUs the contribution that
1443 * each task makes to its run queue's load is weighted according to its
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01001444 * scheduling class and "nice" value. For SCHED_NORMAL tasks this is just a
Peter Williams2dd73a42006-06-27 02:54:34 -07001445 * scaled version of the new time slice allocation that they receive on time
1446 * slice expiry etc.
1447 */
1448
Peter Zijlstracce7ade2009-01-15 14:53:37 +01001449#define WEIGHT_IDLEPRIO 3
1450#define WMULT_IDLEPRIO 1431655765
Ingo Molnardd41f592007-07-09 18:51:59 +02001451
1452/*
1453 * Nice levels are multiplicative, with a gentle 10% change for every
1454 * nice level changed. I.e. when a CPU-bound task goes from nice 0 to
1455 * nice 1, it will get ~10% less CPU time than another CPU-bound task
1456 * that remained on nice 0.
1457 *
1458 * The "10% effect" is relative and cumulative: from _any_ nice level,
1459 * if you go up 1 level, it's -10% CPU usage, if you go down 1 level
Ingo Molnarf9153ee2007-07-16 09:46:30 +02001460 * it's +10% CPU usage. (to achieve that we use a multiplier of 1.25.
1461 * If a task goes up by ~10% and another task goes down by ~10% then
1462 * the relative distance between them is ~25%.)
Ingo Molnardd41f592007-07-09 18:51:59 +02001463 */
1464static const int prio_to_weight[40] = {
Ingo Molnar254753d2007-08-09 11:16:51 +02001465 /* -20 */ 88761, 71755, 56483, 46273, 36291,
1466 /* -15 */ 29154, 23254, 18705, 14949, 11916,
1467 /* -10 */ 9548, 7620, 6100, 4904, 3906,
1468 /* -5 */ 3121, 2501, 1991, 1586, 1277,
1469 /* 0 */ 1024, 820, 655, 526, 423,
1470 /* 5 */ 335, 272, 215, 172, 137,
1471 /* 10 */ 110, 87, 70, 56, 45,
1472 /* 15 */ 36, 29, 23, 18, 15,
Ingo Molnardd41f592007-07-09 18:51:59 +02001473};
1474
Ingo Molnar5714d2d2007-07-16 09:46:31 +02001475/*
1476 * Inverse (2^32/x) values of the prio_to_weight[] array, precalculated.
1477 *
1478 * In cases where the weight does not change often, we can use the
1479 * precalculated inverse to speed up arithmetics by turning divisions
1480 * into multiplications:
1481 */
Ingo Molnardd41f592007-07-09 18:51:59 +02001482static const u32 prio_to_wmult[40] = {
Ingo Molnar254753d2007-08-09 11:16:51 +02001483 /* -20 */ 48388, 59856, 76040, 92818, 118348,
1484 /* -15 */ 147320, 184698, 229616, 287308, 360437,
1485 /* -10 */ 449829, 563644, 704093, 875809, 1099582,
1486 /* -5 */ 1376151, 1717300, 2157191, 2708050, 3363326,
1487 /* 0 */ 4194304, 5237765, 6557202, 8165337, 10153587,
1488 /* 5 */ 12820798, 15790321, 19976592, 24970740, 31350126,
1489 /* 10 */ 39045157, 49367440, 61356676, 76695844, 95443717,
1490 /* 15 */ 119304647, 148102320, 186737708, 238609294, 286331153,
Ingo Molnardd41f592007-07-09 18:51:59 +02001491};
Peter Williams2dd73a42006-06-27 02:54:34 -07001492
Bharata B Raoef12fef2009-03-31 10:02:22 +05301493/* Time spent by the tasks of the cpu accounting group executing in ... */
1494enum cpuacct_stat_index {
1495 CPUACCT_STAT_USER, /* ... user mode */
1496 CPUACCT_STAT_SYSTEM, /* ... kernel mode */
1497
1498 CPUACCT_STAT_NSTATS,
1499};
1500
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01001501#ifdef CONFIG_CGROUP_CPUACCT
1502static void cpuacct_charge(struct task_struct *tsk, u64 cputime);
Bharata B Raoef12fef2009-03-31 10:02:22 +05301503static void cpuacct_update_stats(struct task_struct *tsk,
1504 enum cpuacct_stat_index idx, cputime_t val);
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01001505#else
1506static inline void cpuacct_charge(struct task_struct *tsk, u64 cputime) {}
Bharata B Raoef12fef2009-03-31 10:02:22 +05301507static inline void cpuacct_update_stats(struct task_struct *tsk,
1508 enum cpuacct_stat_index idx, cputime_t val) {}
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01001509#endif
1510
Peter Zijlstra18d95a22008-04-19 19:45:00 +02001511static inline void inc_cpu_load(struct rq *rq, unsigned long load)
1512{
1513 update_load_add(&rq->load, load);
1514}
1515
1516static inline void dec_cpu_load(struct rq *rq, unsigned long load)
1517{
1518 update_load_sub(&rq->load, load);
1519}
1520
Ingo Molnar7940ca32008-08-19 13:40:47 +02001521#if (defined(CONFIG_SMP) && defined(CONFIG_FAIR_GROUP_SCHED)) || defined(CONFIG_RT_GROUP_SCHED)
Peter Zijlstraeb755802008-08-19 12:33:05 +02001522typedef int (*tg_visitor)(struct task_group *, void *);
1523
1524/*
1525 * Iterate the full tree, calling @down when first entering a node and @up when
1526 * leaving it for the final time.
1527 */
1528static int walk_tg_tree(tg_visitor down, tg_visitor up, void *data)
1529{
1530 struct task_group *parent, *child;
1531 int ret;
1532
1533 rcu_read_lock();
1534 parent = &root_task_group;
1535down:
1536 ret = (*down)(parent, data);
1537 if (ret)
1538 goto out_unlock;
1539 list_for_each_entry_rcu(child, &parent->children, siblings) {
1540 parent = child;
1541 goto down;
1542
1543up:
1544 continue;
1545 }
1546 ret = (*up)(parent, data);
1547 if (ret)
1548 goto out_unlock;
1549
1550 child = parent;
1551 parent = parent->parent;
1552 if (parent)
1553 goto up;
1554out_unlock:
1555 rcu_read_unlock();
1556
1557 return ret;
1558}
1559
1560static int tg_nop(struct task_group *tg, void *data)
1561{
1562 return 0;
1563}
1564#endif
1565
Gregory Haskinse7693a32008-01-25 21:08:09 +01001566#ifdef CONFIG_SMP
Peter Zijlstraf5f08f32009-09-10 13:35:28 +02001567/* Used instead of source_load when we know the type == 0 */
1568static unsigned long weighted_cpuload(const int cpu)
1569{
1570 return cpu_rq(cpu)->load.weight;
1571}
1572
1573/*
1574 * Return a low guess at the load of a migration-source cpu weighted
1575 * according to the scheduling class and "nice" value.
1576 *
1577 * We want to under-estimate the load of migration sources, to
1578 * balance conservatively.
1579 */
1580static unsigned long source_load(int cpu, int type)
1581{
1582 struct rq *rq = cpu_rq(cpu);
1583 unsigned long total = weighted_cpuload(cpu);
1584
1585 if (type == 0 || !sched_feat(LB_BIAS))
1586 return total;
1587
1588 return min(rq->cpu_load[type-1], total);
1589}
1590
1591/*
1592 * Return a high guess at the load of a migration-target cpu weighted
1593 * according to the scheduling class and "nice" value.
1594 */
1595static unsigned long target_load(int cpu, int type)
1596{
1597 struct rq *rq = cpu_rq(cpu);
1598 unsigned long total = weighted_cpuload(cpu);
1599
1600 if (type == 0 || !sched_feat(LB_BIAS))
1601 return total;
1602
1603 return max(rq->cpu_load[type-1], total);
1604}
1605
Peter Zijlstraae154be2009-09-10 14:40:57 +02001606static unsigned long power_of(int cpu)
1607{
Peter Zijlstrae51fd5e2010-05-31 12:37:30 +02001608 return cpu_rq(cpu)->cpu_power;
Peter Zijlstraae154be2009-09-10 14:40:57 +02001609}
1610
Gregory Haskinse7693a32008-01-25 21:08:09 +01001611static int task_hot(struct task_struct *p, u64 now, struct sched_domain *sd);
Peter Zijlstrac09595f2008-06-27 13:41:14 +02001612
Peter Zijlstraa8a51d52008-06-27 13:41:26 +02001613static unsigned long cpu_avg_load_per_task(int cpu)
1614{
1615 struct rq *rq = cpu_rq(cpu);
Ingo Molnaraf6d5962008-11-29 20:45:15 +01001616 unsigned long nr_running = ACCESS_ONCE(rq->nr_running);
Peter Zijlstraa8a51d52008-06-27 13:41:26 +02001617
Steven Rostedt4cd42622008-11-26 21:04:24 -05001618 if (nr_running)
Jan H. Schönherre2b245f2011-08-01 11:03:28 +02001619 return rq->load.weight / nr_running;
Peter Zijlstraa8a51d52008-06-27 13:41:26 +02001620
Jan H. Schönherre2b245f2011-08-01 11:03:28 +02001621 return 0;
Peter Zijlstraa8a51d52008-06-27 13:41:26 +02001622}
1623
Gregory Haskins8f45e2b2008-12-29 09:39:51 -05001624#ifdef CONFIG_PREEMPT
1625
Peter Zijlstrab78bb862009-09-15 14:23:18 +02001626static void double_rq_lock(struct rq *rq1, struct rq *rq2);
1627
Alexey Dobriyan70574a92008-11-28 22:08:00 +03001628/*
Gregory Haskins8f45e2b2008-12-29 09:39:51 -05001629 * fair double_lock_balance: Safely acquires both rq->locks in a fair
1630 * way at the expense of forcing extra atomic operations in all
1631 * invocations. This assures that the double_lock is acquired using the
1632 * same underlying policy as the spinlock_t on this architecture, which
1633 * reduces latency compared to the unfair variant below. However, it
1634 * also adds more overhead and therefore may reduce throughput.
Alexey Dobriyan70574a92008-11-28 22:08:00 +03001635 */
Gregory Haskins8f45e2b2008-12-29 09:39:51 -05001636static inline int _double_lock_balance(struct rq *this_rq, struct rq *busiest)
1637 __releases(this_rq->lock)
1638 __acquires(busiest->lock)
1639 __acquires(this_rq->lock)
1640{
Thomas Gleixner05fa7852009-11-17 14:28:38 +01001641 raw_spin_unlock(&this_rq->lock);
Gregory Haskins8f45e2b2008-12-29 09:39:51 -05001642 double_rq_lock(this_rq, busiest);
1643
1644 return 1;
1645}
1646
1647#else
1648/*
1649 * Unfair double_lock_balance: Optimizes throughput at the expense of
1650 * latency by eliminating extra atomic operations when the locks are
1651 * already in proper order on entry. This favors lower cpu-ids and will
1652 * grant the double lock to lower cpus over higher ids under contention,
1653 * regardless of entry order into the function.
1654 */
1655static int _double_lock_balance(struct rq *this_rq, struct rq *busiest)
Alexey Dobriyan70574a92008-11-28 22:08:00 +03001656 __releases(this_rq->lock)
1657 __acquires(busiest->lock)
1658 __acquires(this_rq->lock)
1659{
1660 int ret = 0;
1661
Thomas Gleixner05fa7852009-11-17 14:28:38 +01001662 if (unlikely(!raw_spin_trylock(&busiest->lock))) {
Alexey Dobriyan70574a92008-11-28 22:08:00 +03001663 if (busiest < this_rq) {
Thomas Gleixner05fa7852009-11-17 14:28:38 +01001664 raw_spin_unlock(&this_rq->lock);
1665 raw_spin_lock(&busiest->lock);
1666 raw_spin_lock_nested(&this_rq->lock,
1667 SINGLE_DEPTH_NESTING);
Alexey Dobriyan70574a92008-11-28 22:08:00 +03001668 ret = 1;
1669 } else
Thomas Gleixner05fa7852009-11-17 14:28:38 +01001670 raw_spin_lock_nested(&busiest->lock,
1671 SINGLE_DEPTH_NESTING);
Alexey Dobriyan70574a92008-11-28 22:08:00 +03001672 }
1673 return ret;
1674}
1675
Gregory Haskins8f45e2b2008-12-29 09:39:51 -05001676#endif /* CONFIG_PREEMPT */
1677
1678/*
1679 * double_lock_balance - lock the busiest runqueue, this_rq is locked already.
1680 */
1681static int double_lock_balance(struct rq *this_rq, struct rq *busiest)
1682{
1683 if (unlikely(!irqs_disabled())) {
1684 /* printk() doesn't work good under rq->lock */
Thomas Gleixner05fa7852009-11-17 14:28:38 +01001685 raw_spin_unlock(&this_rq->lock);
Gregory Haskins8f45e2b2008-12-29 09:39:51 -05001686 BUG_ON(1);
1687 }
1688
1689 return _double_lock_balance(this_rq, busiest);
1690}
1691
Alexey Dobriyan70574a92008-11-28 22:08:00 +03001692static inline void double_unlock_balance(struct rq *this_rq, struct rq *busiest)
1693 __releases(busiest->lock)
1694{
Thomas Gleixner05fa7852009-11-17 14:28:38 +01001695 raw_spin_unlock(&busiest->lock);
Alexey Dobriyan70574a92008-11-28 22:08:00 +03001696 lock_set_subclass(&this_rq->lock.dep_map, 0, _RET_IP_);
1697}
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01001698
1699/*
1700 * double_rq_lock - safely lock two runqueues
1701 *
1702 * Note this does not disable interrupts like task_rq_lock,
1703 * you need to do so manually before calling.
1704 */
1705static void double_rq_lock(struct rq *rq1, struct rq *rq2)
1706 __acquires(rq1->lock)
1707 __acquires(rq2->lock)
1708{
1709 BUG_ON(!irqs_disabled());
1710 if (rq1 == rq2) {
1711 raw_spin_lock(&rq1->lock);
1712 __acquire(rq2->lock); /* Fake it out ;) */
1713 } else {
1714 if (rq1 < rq2) {
1715 raw_spin_lock(&rq1->lock);
1716 raw_spin_lock_nested(&rq2->lock, SINGLE_DEPTH_NESTING);
1717 } else {
1718 raw_spin_lock(&rq2->lock);
1719 raw_spin_lock_nested(&rq1->lock, SINGLE_DEPTH_NESTING);
1720 }
1721 }
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01001722}
1723
1724/*
1725 * double_rq_unlock - safely unlock two runqueues
1726 *
1727 * Note this does not restore interrupts like task_rq_unlock,
1728 * you need to do so manually after calling.
1729 */
1730static void double_rq_unlock(struct rq *rq1, struct rq *rq2)
1731 __releases(rq1->lock)
1732 __releases(rq2->lock)
1733{
1734 raw_spin_unlock(&rq1->lock);
1735 if (rq1 != rq2)
1736 raw_spin_unlock(&rq2->lock);
1737 else
1738 __release(rq2->lock);
1739}
1740
Mike Galbraithd95f4122011-02-01 09:50:51 -05001741#else /* CONFIG_SMP */
1742
1743/*
1744 * double_rq_lock - safely lock two runqueues
1745 *
1746 * Note this does not disable interrupts like task_rq_lock,
1747 * you need to do so manually before calling.
1748 */
1749static void double_rq_lock(struct rq *rq1, struct rq *rq2)
1750 __acquires(rq1->lock)
1751 __acquires(rq2->lock)
1752{
1753 BUG_ON(!irqs_disabled());
1754 BUG_ON(rq1 != rq2);
1755 raw_spin_lock(&rq1->lock);
1756 __acquire(rq2->lock); /* Fake it out ;) */
1757}
1758
1759/*
1760 * double_rq_unlock - safely unlock two runqueues
1761 *
1762 * Note this does not restore interrupts like task_rq_unlock,
1763 * you need to do so manually after calling.
1764 */
1765static void double_rq_unlock(struct rq *rq1, struct rq *rq2)
1766 __releases(rq1->lock)
1767 __releases(rq2->lock)
1768{
1769 BUG_ON(rq1 != rq2);
1770 raw_spin_unlock(&rq1->lock);
1771 __release(rq2->lock);
1772}
1773
Peter Zijlstra18d95a22008-04-19 19:45:00 +02001774#endif
Peter Zijlstra18d95a22008-04-19 19:45:00 +02001775
Peter Zijlstra74f51872010-04-22 21:50:19 +02001776static void calc_load_account_idle(struct rq *this_rq);
Christian Ehrhardt0bcdcf22009-11-30 12:16:46 +01001777static void update_sysctl(void);
Christian Ehrhardtacb4a842009-11-30 12:16:48 +01001778static int get_update_sysctl_factor(void);
Venkatesh Pallipadifdf3e952010-05-17 18:14:43 -07001779static void update_cpu_load(struct rq *this_rq);
Thomas Gleixnerdce48a82009-04-11 10:43:41 +02001780
Peter Zijlstracd29fe62009-11-27 17:32:46 +01001781static inline void __set_task_cpu(struct task_struct *p, unsigned int cpu)
1782{
1783 set_task_rq(p, cpu);
1784#ifdef CONFIG_SMP
1785 /*
1786 * After ->cpu is set up to a new value, task_rq_lock(p, ...) can be
1787 * successfuly executed on another CPU. We must ensure that updates of
1788 * per-task data have been completed by this moment.
1789 */
1790 smp_wmb();
1791 task_thread_info(p)->cpu = cpu;
1792#endif
1793}
Peter Zijlstra18d95a22008-04-19 19:45:00 +02001794
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01001795static const struct sched_class rt_sched_class;
Ingo Molnardd41f592007-07-09 18:51:59 +02001796
Peter Zijlstra34f971f2010-09-22 13:53:15 +02001797#define sched_class_highest (&stop_sched_class)
Gregory Haskins1f11eb62008-06-04 15:04:05 -04001798#define for_each_class(class) \
1799 for (class = sched_class_highest; class; class = class->next)
Ingo Molnardd41f592007-07-09 18:51:59 +02001800
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01001801#include "sched_stats.h"
1802
Peter Zijlstrac09595f2008-06-27 13:41:14 +02001803static void inc_nr_running(struct rq *rq)
Ingo Molnar6363ca52008-05-29 11:28:57 +02001804{
1805 rq->nr_running++;
Ingo Molnar6363ca52008-05-29 11:28:57 +02001806}
1807
Peter Zijlstrac09595f2008-06-27 13:41:14 +02001808static void dec_nr_running(struct rq *rq)
Ingo Molnar9c217242007-08-02 17:41:40 +02001809{
1810 rq->nr_running--;
Ingo Molnar9c217242007-08-02 17:41:40 +02001811}
1812
Ingo Molnar45bf76d2007-07-09 18:51:59 +02001813static void set_load_weight(struct task_struct *p)
1814{
Nikhil Raof05998d2011-05-18 10:09:38 -07001815 int prio = p->static_prio - MAX_RT_PRIO;
1816 struct load_weight *load = &p->se.load;
1817
Ingo Molnardd41f592007-07-09 18:51:59 +02001818 /*
1819 * SCHED_IDLE tasks get minimal weight:
1820 */
1821 if (p->policy == SCHED_IDLE) {
Nikhil Raoc8b28112011-05-18 14:37:48 -07001822 load->weight = scale_load(WEIGHT_IDLEPRIO);
Nikhil Raof05998d2011-05-18 10:09:38 -07001823 load->inv_weight = WMULT_IDLEPRIO;
Ingo Molnardd41f592007-07-09 18:51:59 +02001824 return;
1825 }
1826
Nikhil Raoc8b28112011-05-18 14:37:48 -07001827 load->weight = scale_load(prio_to_weight[prio]);
Nikhil Raof05998d2011-05-18 10:09:38 -07001828 load->inv_weight = prio_to_wmult[prio];
Ingo Molnar45bf76d2007-07-09 18:51:59 +02001829}
1830
Peter Zijlstra371fd7e2010-03-24 16:38:48 +01001831static void enqueue_task(struct rq *rq, struct task_struct *p, int flags)
Gregory Haskins2087a1a2008-06-27 14:30:00 -06001832{
Mike Galbraitha64692a2010-03-11 17:16:20 +01001833 update_rq_clock(rq);
Ingo Molnar71f8bd42007-07-09 18:51:59 +02001834 sched_info_queued(p);
Peter Zijlstra371fd7e2010-03-24 16:38:48 +01001835 p->sched_class->enqueue_task(rq, p, flags);
Ingo Molnardd41f592007-07-09 18:51:59 +02001836}
1837
Peter Zijlstra371fd7e2010-03-24 16:38:48 +01001838static void dequeue_task(struct rq *rq, struct task_struct *p, int flags)
Ingo Molnardd41f592007-07-09 18:51:59 +02001839{
Mike Galbraitha64692a2010-03-11 17:16:20 +01001840 update_rq_clock(rq);
Ankita Garg46ac22b2008-07-01 14:30:06 +05301841 sched_info_dequeued(p);
Peter Zijlstra371fd7e2010-03-24 16:38:48 +01001842 p->sched_class->dequeue_task(rq, p, flags);
Ingo Molnar71f8bd42007-07-09 18:51:59 +02001843}
1844
1845/*
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01001846 * activate_task - move a task to the runqueue.
1847 */
Peter Zijlstra371fd7e2010-03-24 16:38:48 +01001848static void activate_task(struct rq *rq, struct task_struct *p, int flags)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01001849{
1850 if (task_contributes_to_load(p))
1851 rq->nr_uninterruptible--;
1852
Peter Zijlstra371fd7e2010-03-24 16:38:48 +01001853 enqueue_task(rq, p, flags);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01001854}
1855
1856/*
1857 * deactivate_task - remove a task from the runqueue.
1858 */
Peter Zijlstra371fd7e2010-03-24 16:38:48 +01001859static void deactivate_task(struct rq *rq, struct task_struct *p, int flags)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01001860{
1861 if (task_contributes_to_load(p))
1862 rq->nr_uninterruptible++;
1863
Peter Zijlstra371fd7e2010-03-24 16:38:48 +01001864 dequeue_task(rq, p, flags);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01001865}
1866
Venkatesh Pallipadib52bfee2010-10-04 17:03:19 -07001867#ifdef CONFIG_IRQ_TIME_ACCOUNTING
1868
Venkatesh Pallipadi305e6832010-10-04 17:03:21 -07001869/*
1870 * There are no locks covering percpu hardirq/softirq time.
1871 * They are only modified in account_system_vtime, on corresponding CPU
1872 * with interrupts disabled. So, writes are safe.
1873 * They are read and saved off onto struct rq in update_rq_clock().
1874 * This may result in other CPU reading this CPU's irq time and can
1875 * race with irq/account_system_vtime on this CPU. We would either get old
Peter Zijlstra8e92c202010-12-09 14:15:34 +01001876 * or new value with a side effect of accounting a slice of irq time to wrong
1877 * task when irq is in progress while we read rq->clock. That is a worthy
1878 * compromise in place of having locks on each irq in account_system_time.
Venkatesh Pallipadi305e6832010-10-04 17:03:21 -07001879 */
Venkatesh Pallipadib52bfee2010-10-04 17:03:19 -07001880static DEFINE_PER_CPU(u64, cpu_hardirq_time);
1881static DEFINE_PER_CPU(u64, cpu_softirq_time);
1882
1883static DEFINE_PER_CPU(u64, irq_start_time);
1884static int sched_clock_irqtime;
1885
1886void enable_sched_clock_irqtime(void)
1887{
1888 sched_clock_irqtime = 1;
1889}
1890
1891void disable_sched_clock_irqtime(void)
1892{
1893 sched_clock_irqtime = 0;
1894}
1895
Peter Zijlstra8e92c202010-12-09 14:15:34 +01001896#ifndef CONFIG_64BIT
1897static DEFINE_PER_CPU(seqcount_t, irq_time_seq);
1898
1899static inline void irq_time_write_begin(void)
1900{
1901 __this_cpu_inc(irq_time_seq.sequence);
1902 smp_wmb();
1903}
1904
1905static inline void irq_time_write_end(void)
1906{
1907 smp_wmb();
1908 __this_cpu_inc(irq_time_seq.sequence);
1909}
1910
1911static inline u64 irq_time_read(int cpu)
1912{
1913 u64 irq_time;
1914 unsigned seq;
1915
1916 do {
1917 seq = read_seqcount_begin(&per_cpu(irq_time_seq, cpu));
1918 irq_time = per_cpu(cpu_softirq_time, cpu) +
1919 per_cpu(cpu_hardirq_time, cpu);
1920 } while (read_seqcount_retry(&per_cpu(irq_time_seq, cpu), seq));
1921
1922 return irq_time;
1923}
1924#else /* CONFIG_64BIT */
1925static inline void irq_time_write_begin(void)
1926{
1927}
1928
1929static inline void irq_time_write_end(void)
1930{
1931}
1932
1933static inline u64 irq_time_read(int cpu)
Venkatesh Pallipadi305e6832010-10-04 17:03:21 -07001934{
Venkatesh Pallipadi305e6832010-10-04 17:03:21 -07001935 return per_cpu(cpu_softirq_time, cpu) + per_cpu(cpu_hardirq_time, cpu);
1936}
Peter Zijlstra8e92c202010-12-09 14:15:34 +01001937#endif /* CONFIG_64BIT */
Venkatesh Pallipadi305e6832010-10-04 17:03:21 -07001938
Peter Zijlstrafe44d622010-12-09 14:15:34 +01001939/*
1940 * Called before incrementing preempt_count on {soft,}irq_enter
1941 * and before decrementing preempt_count on {soft,}irq_exit.
1942 */
Venkatesh Pallipadib52bfee2010-10-04 17:03:19 -07001943void account_system_vtime(struct task_struct *curr)
1944{
1945 unsigned long flags;
Peter Zijlstrafe44d622010-12-09 14:15:34 +01001946 s64 delta;
Venkatesh Pallipadib52bfee2010-10-04 17:03:19 -07001947 int cpu;
Venkatesh Pallipadib52bfee2010-10-04 17:03:19 -07001948
1949 if (!sched_clock_irqtime)
1950 return;
1951
1952 local_irq_save(flags);
1953
Venkatesh Pallipadib52bfee2010-10-04 17:03:19 -07001954 cpu = smp_processor_id();
Peter Zijlstrafe44d622010-12-09 14:15:34 +01001955 delta = sched_clock_cpu(cpu) - __this_cpu_read(irq_start_time);
1956 __this_cpu_add(irq_start_time, delta);
1957
Peter Zijlstra8e92c202010-12-09 14:15:34 +01001958 irq_time_write_begin();
Venkatesh Pallipadib52bfee2010-10-04 17:03:19 -07001959 /*
1960 * We do not account for softirq time from ksoftirqd here.
1961 * We want to continue accounting softirq time to ksoftirqd thread
1962 * in that case, so as not to confuse scheduler with a special task
1963 * that do not consume any time, but still wants to run.
1964 */
1965 if (hardirq_count())
Peter Zijlstrafe44d622010-12-09 14:15:34 +01001966 __this_cpu_add(cpu_hardirq_time, delta);
Venkatesh Pallipadi4dd53d82010-12-21 17:09:00 -08001967 else if (in_serving_softirq() && curr != this_cpu_ksoftirqd())
Peter Zijlstrafe44d622010-12-09 14:15:34 +01001968 __this_cpu_add(cpu_softirq_time, delta);
Venkatesh Pallipadib52bfee2010-10-04 17:03:19 -07001969
Peter Zijlstra8e92c202010-12-09 14:15:34 +01001970 irq_time_write_end();
Venkatesh Pallipadib52bfee2010-10-04 17:03:19 -07001971 local_irq_restore(flags);
1972}
Ingo Molnarb7dadc32010-10-18 20:00:37 +02001973EXPORT_SYMBOL_GPL(account_system_vtime);
Venkatesh Pallipadib52bfee2010-10-04 17:03:19 -07001974
Glauber Costae6e66852011-07-11 15:28:17 -04001975#endif /* CONFIG_IRQ_TIME_ACCOUNTING */
1976
1977#ifdef CONFIG_PARAVIRT
1978static inline u64 steal_ticks(u64 steal)
1979{
1980 if (unlikely(steal > NSEC_PER_SEC))
1981 return div_u64(steal, TICK_NSEC);
1982
1983 return __iter_div_u64_rem(steal, TICK_NSEC, &steal);
1984}
1985#endif
1986
Peter Zijlstrafe44d622010-12-09 14:15:34 +01001987static void update_rq_clock_task(struct rq *rq, s64 delta)
Venkatesh Pallipadiaa483802010-10-04 17:03:22 -07001988{
Glauber Costa095c0aa2011-07-11 15:28:18 -04001989/*
1990 * In theory, the compile should just see 0 here, and optimize out the call
1991 * to sched_rt_avg_update. But I don't trust it...
1992 */
1993#if defined(CONFIG_IRQ_TIME_ACCOUNTING) || defined(CONFIG_PARAVIRT_TIME_ACCOUNTING)
1994 s64 steal = 0, irq_delta = 0;
1995#endif
1996#ifdef CONFIG_IRQ_TIME_ACCOUNTING
Peter Zijlstra8e92c202010-12-09 14:15:34 +01001997 irq_delta = irq_time_read(cpu_of(rq)) - rq->prev_irq_time;
Peter Zijlstrafe44d622010-12-09 14:15:34 +01001998
1999 /*
2000 * Since irq_time is only updated on {soft,}irq_exit, we might run into
2001 * this case when a previous update_rq_clock() happened inside a
2002 * {soft,}irq region.
2003 *
2004 * When this happens, we stop ->clock_task and only update the
2005 * prev_irq_time stamp to account for the part that fit, so that a next
2006 * update will consume the rest. This ensures ->clock_task is
2007 * monotonic.
2008 *
2009 * It does however cause some slight miss-attribution of {soft,}irq
2010 * time, a more accurate solution would be to update the irq_time using
2011 * the current rq->clock timestamp, except that would require using
2012 * atomic ops.
2013 */
2014 if (irq_delta > delta)
2015 irq_delta = delta;
2016
2017 rq->prev_irq_time += irq_delta;
2018 delta -= irq_delta;
Glauber Costa095c0aa2011-07-11 15:28:18 -04002019#endif
2020#ifdef CONFIG_PARAVIRT_TIME_ACCOUNTING
2021 if (static_branch((&paravirt_steal_rq_enabled))) {
2022 u64 st;
2023
2024 steal = paravirt_steal_clock(cpu_of(rq));
2025 steal -= rq->prev_steal_time_rq;
2026
2027 if (unlikely(steal > delta))
2028 steal = delta;
2029
2030 st = steal_ticks(steal);
2031 steal = st * TICK_NSEC;
2032
2033 rq->prev_steal_time_rq += steal;
2034
2035 delta -= steal;
2036 }
2037#endif
2038
Peter Zijlstrafe44d622010-12-09 14:15:34 +01002039 rq->clock_task += delta;
2040
Glauber Costa095c0aa2011-07-11 15:28:18 -04002041#if defined(CONFIG_IRQ_TIME_ACCOUNTING) || defined(CONFIG_PARAVIRT_TIME_ACCOUNTING)
2042 if ((irq_delta + steal) && sched_feat(NONTASK_POWER))
2043 sched_rt_avg_update(rq, irq_delta + steal);
2044#endif
Venkatesh Pallipadiaa483802010-10-04 17:03:22 -07002045}
2046
Glauber Costa095c0aa2011-07-11 15:28:18 -04002047#ifdef CONFIG_IRQ_TIME_ACCOUNTING
Venkatesh Pallipadiabb74ce2010-12-21 17:09:03 -08002048static int irqtime_account_hi_update(void)
2049{
2050 struct cpu_usage_stat *cpustat = &kstat_this_cpu.cpustat;
2051 unsigned long flags;
2052 u64 latest_ns;
2053 int ret = 0;
2054
2055 local_irq_save(flags);
2056 latest_ns = this_cpu_read(cpu_hardirq_time);
2057 if (cputime64_gt(nsecs_to_cputime64(latest_ns), cpustat->irq))
2058 ret = 1;
2059 local_irq_restore(flags);
2060 return ret;
2061}
2062
2063static int irqtime_account_si_update(void)
2064{
2065 struct cpu_usage_stat *cpustat = &kstat_this_cpu.cpustat;
2066 unsigned long flags;
2067 u64 latest_ns;
2068 int ret = 0;
2069
2070 local_irq_save(flags);
2071 latest_ns = this_cpu_read(cpu_softirq_time);
2072 if (cputime64_gt(nsecs_to_cputime64(latest_ns), cpustat->softirq))
2073 ret = 1;
2074 local_irq_restore(flags);
2075 return ret;
2076}
2077
Peter Zijlstrafe44d622010-12-09 14:15:34 +01002078#else /* CONFIG_IRQ_TIME_ACCOUNTING */
Venkatesh Pallipadi305e6832010-10-04 17:03:21 -07002079
Venkatesh Pallipadiabb74ce2010-12-21 17:09:03 -08002080#define sched_clock_irqtime (0)
2081
Glauber Costa095c0aa2011-07-11 15:28:18 -04002082#endif
Venkatesh Pallipadib52bfee2010-10-04 17:03:19 -07002083
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01002084#include "sched_idletask.c"
2085#include "sched_fair.c"
2086#include "sched_rt.c"
Mike Galbraith5091faa2010-11-30 14:18:03 +01002087#include "sched_autogroup.c"
Peter Zijlstra34f971f2010-09-22 13:53:15 +02002088#include "sched_stoptask.c"
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01002089#ifdef CONFIG_SCHED_DEBUG
2090# include "sched_debug.c"
2091#endif
2092
Peter Zijlstra34f971f2010-09-22 13:53:15 +02002093void sched_set_stop_task(int cpu, struct task_struct *stop)
2094{
2095 struct sched_param param = { .sched_priority = MAX_RT_PRIO - 1 };
2096 struct task_struct *old_stop = cpu_rq(cpu)->stop;
2097
2098 if (stop) {
2099 /*
2100 * Make it appear like a SCHED_FIFO task, its something
2101 * userspace knows about and won't get confused about.
2102 *
2103 * Also, it will make PI more or less work without too
2104 * much confusion -- but then, stop work should not
2105 * rely on PI working anyway.
2106 */
2107 sched_setscheduler_nocheck(stop, SCHED_FIFO, &param);
2108
2109 stop->sched_class = &stop_sched_class;
2110 }
2111
2112 cpu_rq(cpu)->stop = stop;
2113
2114 if (old_stop) {
2115 /*
2116 * Reset it back to a normal scheduling class so that
2117 * it can die in pieces.
2118 */
2119 old_stop->sched_class = &rt_sched_class;
2120 }
2121}
2122
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01002123/*
Ingo Molnardd41f592007-07-09 18:51:59 +02002124 * __normal_prio - return the priority that is based on the static prio
Ingo Molnar71f8bd42007-07-09 18:51:59 +02002125 */
Ingo Molnar14531182007-07-09 18:51:59 +02002126static inline int __normal_prio(struct task_struct *p)
2127{
Ingo Molnardd41f592007-07-09 18:51:59 +02002128 return p->static_prio;
Ingo Molnar14531182007-07-09 18:51:59 +02002129}
2130
2131/*
Ingo Molnarb29739f2006-06-27 02:54:51 -07002132 * Calculate the expected normal priority: i.e. priority
2133 * without taking RT-inheritance into account. Might be
2134 * boosted by interactivity modifiers. Changes upon fork,
2135 * setprio syscalls, and whenever the interactivity
2136 * estimator recalculates.
2137 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07002138static inline int normal_prio(struct task_struct *p)
Ingo Molnarb29739f2006-06-27 02:54:51 -07002139{
2140 int prio;
2141
Ingo Molnare05606d2007-07-09 18:51:59 +02002142 if (task_has_rt_policy(p))
Ingo Molnarb29739f2006-06-27 02:54:51 -07002143 prio = MAX_RT_PRIO-1 - p->rt_priority;
2144 else
2145 prio = __normal_prio(p);
2146 return prio;
2147}
2148
2149/*
2150 * Calculate the current priority, i.e. the priority
2151 * taken into account by the scheduler. This value might
2152 * be boosted by RT tasks, or might be boosted by
2153 * interactivity modifiers. Will be RT if the task got
2154 * RT-boosted. If not then it returns p->normal_prio.
2155 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07002156static int effective_prio(struct task_struct *p)
Ingo Molnarb29739f2006-06-27 02:54:51 -07002157{
2158 p->normal_prio = normal_prio(p);
2159 /*
2160 * If we are RT tasks or we were boosted to RT priority,
2161 * keep the priority unchanged. Otherwise, update priority
2162 * to the normal priority:
2163 */
2164 if (!rt_prio(p->prio))
2165 return p->normal_prio;
2166 return p->prio;
2167}
2168
Linus Torvalds1da177e2005-04-16 15:20:36 -07002169/**
2170 * task_curr - is this task currently executing on a CPU?
2171 * @p: the task in question.
2172 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07002173inline int task_curr(const struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002174{
2175 return cpu_curr(task_cpu(p)) == p;
2176}
2177
Steven Rostedtcb469842008-01-25 21:08:22 +01002178static inline void check_class_changed(struct rq *rq, struct task_struct *p,
2179 const struct sched_class *prev_class,
Peter Zijlstrada7a7352011-01-17 17:03:27 +01002180 int oldprio)
Steven Rostedtcb469842008-01-25 21:08:22 +01002181{
2182 if (prev_class != p->sched_class) {
2183 if (prev_class->switched_from)
Peter Zijlstrada7a7352011-01-17 17:03:27 +01002184 prev_class->switched_from(rq, p);
2185 p->sched_class->switched_to(rq, p);
2186 } else if (oldprio != p->prio)
2187 p->sched_class->prio_changed(rq, p, oldprio);
Steven Rostedtcb469842008-01-25 21:08:22 +01002188}
2189
Peter Zijlstra1e5a7402010-10-31 12:37:04 +01002190static void check_preempt_curr(struct rq *rq, struct task_struct *p, int flags)
2191{
2192 const struct sched_class *class;
2193
2194 if (p->sched_class == rq->curr->sched_class) {
2195 rq->curr->sched_class->check_preempt_curr(rq, p, flags);
2196 } else {
2197 for_each_class(class) {
2198 if (class == rq->curr->sched_class)
2199 break;
2200 if (class == p->sched_class) {
2201 resched_task(rq->curr);
2202 break;
2203 }
2204 }
2205 }
2206
2207 /*
2208 * A queue event has occurred, and we're going to schedule. In
2209 * this case, we can save a useless back to back clock update.
2210 */
Peter Zijlstrafd2f4412011-04-05 17:23:44 +02002211 if (rq->curr->on_rq && test_tsk_need_resched(rq->curr))
Peter Zijlstra1e5a7402010-10-31 12:37:04 +01002212 rq->skip_clock_update = 1;
2213}
2214
Linus Torvalds1da177e2005-04-16 15:20:36 -07002215#ifdef CONFIG_SMP
Ingo Molnarcc367732007-10-15 17:00:18 +02002216/*
2217 * Is this task likely cache-hot:
2218 */
Gregory Haskinse7693a32008-01-25 21:08:09 +01002219static int
Ingo Molnarcc367732007-10-15 17:00:18 +02002220task_hot(struct task_struct *p, u64 now, struct sched_domain *sd)
2221{
2222 s64 delta;
2223
Peter Zijlstrae6c8fba2009-12-16 18:04:33 +01002224 if (p->sched_class != &fair_sched_class)
2225 return 0;
2226
Nikhil Raoef8002f2010-10-13 12:09:35 -07002227 if (unlikely(p->policy == SCHED_IDLE))
2228 return 0;
2229
Ingo Molnarf540a602008-03-15 17:10:34 +01002230 /*
2231 * Buddy candidates are cache hot:
2232 */
Mike Galbraithf685cea2009-10-23 23:09:22 +02002233 if (sched_feat(CACHE_HOT_BUDDY) && this_rq()->nr_running &&
Peter Zijlstra47932412008-11-04 21:25:09 +01002234 (&p->se == cfs_rq_of(&p->se)->next ||
2235 &p->se == cfs_rq_of(&p->se)->last))
Ingo Molnarf540a602008-03-15 17:10:34 +01002236 return 1;
2237
Ingo Molnar6bc16652007-10-15 17:00:18 +02002238 if (sysctl_sched_migration_cost == -1)
2239 return 1;
2240 if (sysctl_sched_migration_cost == 0)
2241 return 0;
2242
Ingo Molnarcc367732007-10-15 17:00:18 +02002243 delta = now - p->se.exec_start;
2244
2245 return delta < (s64)sysctl_sched_migration_cost;
2246}
2247
Ingo Molnardd41f592007-07-09 18:51:59 +02002248void set_task_cpu(struct task_struct *p, unsigned int new_cpu)
Ingo Molnarc65cc872007-07-09 18:51:58 +02002249{
Peter Zijlstrae2912002009-12-16 18:04:36 +01002250#ifdef CONFIG_SCHED_DEBUG
2251 /*
2252 * We should never call set_task_cpu() on a blocked task,
2253 * ttwu() will sort out the placement.
2254 */
Peter Zijlstra077614e2009-12-17 13:16:31 +01002255 WARN_ON_ONCE(p->state != TASK_RUNNING && p->state != TASK_WAKING &&
2256 !(task_thread_info(p)->preempt_count & PREEMPT_ACTIVE));
Peter Zijlstra0122ec52011-04-05 17:23:51 +02002257
2258#ifdef CONFIG_LOCKDEP
Peter Zijlstra6c6c54e2011-06-03 17:37:07 +02002259 /*
2260 * The caller should hold either p->pi_lock or rq->lock, when changing
2261 * a task's CPU. ->pi_lock for waking tasks, rq->lock for runnable tasks.
2262 *
2263 * sched_move_task() holds both and thus holding either pins the cgroup,
2264 * see set_task_rq().
2265 *
2266 * Furthermore, all task_rq users should acquire both locks, see
2267 * task_rq_lock().
2268 */
Peter Zijlstra0122ec52011-04-05 17:23:51 +02002269 WARN_ON_ONCE(debug_locks && !(lockdep_is_held(&p->pi_lock) ||
2270 lockdep_is_held(&task_rq(p)->lock)));
2271#endif
Peter Zijlstrae2912002009-12-16 18:04:36 +01002272#endif
2273
Mathieu Desnoyersde1d7282009-05-05 16:49:59 +08002274 trace_sched_migrate_task(p, new_cpu);
Peter Zijlstracbc34ed2008-12-10 08:08:22 +01002275
Peter Zijlstra0c697742009-12-22 15:43:19 +01002276 if (task_cpu(p) != new_cpu) {
2277 p->se.nr_migrations++;
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02002278 perf_sw_event(PERF_COUNT_SW_CPU_MIGRATIONS, 1, NULL, 0);
Peter Zijlstra0c697742009-12-22 15:43:19 +01002279 }
Ingo Molnardd41f592007-07-09 18:51:59 +02002280
2281 __set_task_cpu(p, new_cpu);
Ingo Molnarc65cc872007-07-09 18:51:58 +02002282}
2283
Tejun Heo969c7922010-05-06 18:49:21 +02002284struct migration_arg {
Ingo Molnar36c8b582006-07-03 00:25:41 -07002285 struct task_struct *task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002286 int dest_cpu;
Ingo Molnar70b97a72006-07-03 00:25:42 -07002287};
Linus Torvalds1da177e2005-04-16 15:20:36 -07002288
Tejun Heo969c7922010-05-06 18:49:21 +02002289static int migration_cpu_stop(void *data);
2290
Linus Torvalds1da177e2005-04-16 15:20:36 -07002291/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002292 * wait_task_inactive - wait for a thread to unschedule.
2293 *
Roland McGrath85ba2d82008-07-25 19:45:58 -07002294 * If @match_state is nonzero, it's the @p->state value just checked and
2295 * not expected to change. If it changes, i.e. @p might have woken up,
2296 * then return zero. When we succeed in waiting for @p to be off its CPU,
2297 * we return a positive number (its total switch count). If a second call
2298 * a short while later returns the same number, the caller can be sure that
2299 * @p has remained unscheduled the whole time.
2300 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002301 * The caller must ensure that the task *will* unschedule sometime soon,
2302 * else this function might spin for a *long* time. This function can't
2303 * be called with interrupts off, or it may introduce deadlock with
2304 * smp_call_function() if an IPI is sent by the same process we are
2305 * waiting to become inactive.
2306 */
Roland McGrath85ba2d82008-07-25 19:45:58 -07002307unsigned long wait_task_inactive(struct task_struct *p, long match_state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002308{
2309 unsigned long flags;
Ingo Molnardd41f592007-07-09 18:51:59 +02002310 int running, on_rq;
Roland McGrath85ba2d82008-07-25 19:45:58 -07002311 unsigned long ncsw;
Ingo Molnar70b97a72006-07-03 00:25:42 -07002312 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002313
Andi Kleen3a5c3592007-10-15 17:00:14 +02002314 for (;;) {
2315 /*
2316 * We do the initial early heuristics without holding
2317 * any task-queue locks at all. We'll only try to get
2318 * the runqueue lock when things look like they will
2319 * work out!
2320 */
2321 rq = task_rq(p);
Linus Torvaldsfa490cf2007-06-18 09:34:40 -07002322
Andi Kleen3a5c3592007-10-15 17:00:14 +02002323 /*
2324 * If the task is actively running on another CPU
2325 * still, just relax and busy-wait without holding
2326 * any locks.
2327 *
2328 * NOTE! Since we don't hold any locks, it's not
2329 * even sure that "rq" stays as the right runqueue!
2330 * But we don't care, since "task_running()" will
2331 * return false if the runqueue has changed and p
2332 * is actually now running somewhere else!
2333 */
Roland McGrath85ba2d82008-07-25 19:45:58 -07002334 while (task_running(rq, p)) {
2335 if (match_state && unlikely(p->state != match_state))
2336 return 0;
Andi Kleen3a5c3592007-10-15 17:00:14 +02002337 cpu_relax();
Roland McGrath85ba2d82008-07-25 19:45:58 -07002338 }
Linus Torvaldsfa490cf2007-06-18 09:34:40 -07002339
Andi Kleen3a5c3592007-10-15 17:00:14 +02002340 /*
2341 * Ok, time to look more closely! We need the rq
2342 * lock now, to be *sure*. If we're wrong, we'll
2343 * just go back and repeat.
2344 */
2345 rq = task_rq_lock(p, &flags);
Peter Zijlstra27a9da62010-05-04 20:36:56 +02002346 trace_sched_wait_task(p);
Andi Kleen3a5c3592007-10-15 17:00:14 +02002347 running = task_running(rq, p);
Peter Zijlstrafd2f4412011-04-05 17:23:44 +02002348 on_rq = p->on_rq;
Roland McGrath85ba2d82008-07-25 19:45:58 -07002349 ncsw = 0;
Oleg Nesterovf31e11d2008-08-20 16:54:44 -07002350 if (!match_state || p->state == match_state)
Oleg Nesterov93dcf552008-08-20 16:54:44 -07002351 ncsw = p->nvcsw | LONG_MIN; /* sets MSB */
Peter Zijlstra0122ec52011-04-05 17:23:51 +02002352 task_rq_unlock(rq, p, &flags);
Linus Torvaldsfa490cf2007-06-18 09:34:40 -07002353
Andi Kleen3a5c3592007-10-15 17:00:14 +02002354 /*
Roland McGrath85ba2d82008-07-25 19:45:58 -07002355 * If it changed from the expected state, bail out now.
2356 */
2357 if (unlikely(!ncsw))
2358 break;
2359
2360 /*
Andi Kleen3a5c3592007-10-15 17:00:14 +02002361 * Was it really running after all now that we
2362 * checked with the proper locks actually held?
2363 *
2364 * Oops. Go back and try again..
2365 */
2366 if (unlikely(running)) {
2367 cpu_relax();
2368 continue;
2369 }
2370
2371 /*
2372 * It's not enough that it's not actively running,
2373 * it must be off the runqueue _entirely_, and not
2374 * preempted!
2375 *
Luis Henriques80dd99b2009-03-16 19:58:09 +00002376 * So if it was still runnable (but just not actively
Andi Kleen3a5c3592007-10-15 17:00:14 +02002377 * running right now), it's preempted, and we should
2378 * yield - it could be a while.
2379 */
2380 if (unlikely(on_rq)) {
Thomas Gleixner8eb90c32011-02-23 23:52:21 +00002381 ktime_t to = ktime_set(0, NSEC_PER_SEC/HZ);
2382
2383 set_current_state(TASK_UNINTERRUPTIBLE);
2384 schedule_hrtimeout(&to, HRTIMER_MODE_REL);
Andi Kleen3a5c3592007-10-15 17:00:14 +02002385 continue;
2386 }
2387
2388 /*
2389 * Ahh, all good. It wasn't running, and it wasn't
2390 * runnable, which means that it will never become
2391 * running in the future either. We're all done!
2392 */
2393 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002394 }
Roland McGrath85ba2d82008-07-25 19:45:58 -07002395
2396 return ncsw;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002397}
2398
2399/***
2400 * kick_process - kick a running thread to enter/exit the kernel
2401 * @p: the to-be-kicked thread
2402 *
2403 * Cause a process which is running on another CPU to enter
2404 * kernel-mode, without any delay. (to get signals handled.)
2405 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002406 * NOTE: this function doesn't have to take the runqueue lock,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002407 * because all it wants to ensure is that the remote task enters
2408 * the kernel. If the IPI races and the task has been migrated
2409 * to another CPU then no harm is done and the purpose has been
2410 * achieved as well.
2411 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07002412void kick_process(struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002413{
2414 int cpu;
2415
2416 preempt_disable();
2417 cpu = task_cpu(p);
2418 if ((cpu != smp_processor_id()) && task_curr(p))
2419 smp_send_reschedule(cpu);
2420 preempt_enable();
2421}
Rusty Russellb43e3522009-06-12 22:27:00 -06002422EXPORT_SYMBOL_GPL(kick_process);
Nick Piggin476d1392005-06-25 14:57:29 -07002423#endif /* CONFIG_SMP */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002424
Peter Zijlstra970b13b2009-11-25 13:31:39 +01002425#ifdef CONFIG_SMP
Oleg Nesterov30da6882010-03-15 10:10:19 +01002426/*
Peter Zijlstra013fdb82011-04-05 17:23:45 +02002427 * ->cpus_allowed is protected by both rq->lock and p->pi_lock
Oleg Nesterov30da6882010-03-15 10:10:19 +01002428 */
Peter Zijlstra5da9a0f2009-12-16 18:04:38 +01002429static int select_fallback_rq(int cpu, struct task_struct *p)
2430{
2431 int dest_cpu;
2432 const struct cpumask *nodemask = cpumask_of_node(cpu_to_node(cpu));
2433
2434 /* Look for allowed, online CPU in same node. */
2435 for_each_cpu_and(dest_cpu, nodemask, cpu_active_mask)
2436 if (cpumask_test_cpu(dest_cpu, &p->cpus_allowed))
2437 return dest_cpu;
2438
2439 /* Any allowed, online CPU? */
2440 dest_cpu = cpumask_any_and(&p->cpus_allowed, cpu_active_mask);
2441 if (dest_cpu < nr_cpu_ids)
2442 return dest_cpu;
2443
2444 /* No more Mr. Nice Guy. */
Peter Zijlstra48c5cca2010-11-13 19:32:29 +01002445 dest_cpu = cpuset_cpus_allowed_fallback(p);
2446 /*
2447 * Don't tell them about moving exiting tasks or
2448 * kernel threads (both mm NULL), since they never
2449 * leave kernel.
2450 */
2451 if (p->mm && printk_ratelimit()) {
2452 printk(KERN_INFO "process %d (%s) no longer affine to cpu%d\n",
2453 task_pid_nr(p), p->comm, cpu);
Peter Zijlstra5da9a0f2009-12-16 18:04:38 +01002454 }
2455
2456 return dest_cpu;
2457}
2458
Peter Zijlstrae2912002009-12-16 18:04:36 +01002459/*
Peter Zijlstra013fdb82011-04-05 17:23:45 +02002460 * The caller (fork, wakeup) owns p->pi_lock, ->cpus_allowed is stable.
Peter Zijlstrae2912002009-12-16 18:04:36 +01002461 */
Peter Zijlstra970b13b2009-11-25 13:31:39 +01002462static inline
Peter Zijlstra7608dec2011-04-05 17:23:46 +02002463int select_task_rq(struct task_struct *p, int sd_flags, int wake_flags)
Peter Zijlstra970b13b2009-11-25 13:31:39 +01002464{
Peter Zijlstra7608dec2011-04-05 17:23:46 +02002465 int cpu = p->sched_class->select_task_rq(p, sd_flags, wake_flags);
Peter Zijlstrae2912002009-12-16 18:04:36 +01002466
2467 /*
2468 * In order not to call set_task_cpu() on a blocking task we need
2469 * to rely on ttwu() to place the task on a valid ->cpus_allowed
2470 * cpu.
2471 *
2472 * Since this is common to all placement strategies, this lives here.
2473 *
2474 * [ this allows ->select_task() to simply return task_cpu(p) and
2475 * not worry about this generic constraint ]
2476 */
2477 if (unlikely(!cpumask_test_cpu(cpu, &p->cpus_allowed) ||
Peter Zijlstra70f11202009-12-20 17:36:27 +01002478 !cpu_online(cpu)))
Peter Zijlstra5da9a0f2009-12-16 18:04:38 +01002479 cpu = select_fallback_rq(task_cpu(p), p);
Peter Zijlstrae2912002009-12-16 18:04:36 +01002480
2481 return cpu;
Peter Zijlstra970b13b2009-11-25 13:31:39 +01002482}
Mike Galbraith09a40af2010-04-15 07:29:59 +02002483
2484static void update_avg(u64 *avg, u64 sample)
2485{
2486 s64 diff = sample - *avg;
2487 *avg += diff >> 3;
2488}
Peter Zijlstra970b13b2009-11-25 13:31:39 +01002489#endif
2490
Peter Zijlstrad7c01d22011-04-05 17:23:43 +02002491static void
Peter Zijlstrab84cb5d2011-04-05 17:23:55 +02002492ttwu_stat(struct task_struct *p, int cpu, int wake_flags)
Tejun Heo9ed38112009-12-03 15:08:03 +09002493{
Peter Zijlstrad7c01d22011-04-05 17:23:43 +02002494#ifdef CONFIG_SCHEDSTATS
Peter Zijlstrab84cb5d2011-04-05 17:23:55 +02002495 struct rq *rq = this_rq();
Tejun Heo9ed38112009-12-03 15:08:03 +09002496
Peter Zijlstrad7c01d22011-04-05 17:23:43 +02002497#ifdef CONFIG_SMP
2498 int this_cpu = smp_processor_id();
Tejun Heo9ed38112009-12-03 15:08:03 +09002499
Peter Zijlstrad7c01d22011-04-05 17:23:43 +02002500 if (cpu == this_cpu) {
2501 schedstat_inc(rq, ttwu_local);
2502 schedstat_inc(p, se.statistics.nr_wakeups_local);
2503 } else {
2504 struct sched_domain *sd;
2505
2506 schedstat_inc(p, se.statistics.nr_wakeups_remote);
Peter Zijlstra057f3fa2011-04-18 11:24:34 +02002507 rcu_read_lock();
Peter Zijlstrad7c01d22011-04-05 17:23:43 +02002508 for_each_domain(this_cpu, sd) {
2509 if (cpumask_test_cpu(cpu, sched_domain_span(sd))) {
2510 schedstat_inc(sd, ttwu_wake_remote);
2511 break;
2512 }
2513 }
Peter Zijlstra057f3fa2011-04-18 11:24:34 +02002514 rcu_read_unlock();
Peter Zijlstrad7c01d22011-04-05 17:23:43 +02002515 }
Peter Zijlstraf339b9d2011-05-31 10:49:20 +02002516
2517 if (wake_flags & WF_MIGRATED)
2518 schedstat_inc(p, se.statistics.nr_wakeups_migrate);
2519
Peter Zijlstrad7c01d22011-04-05 17:23:43 +02002520#endif /* CONFIG_SMP */
2521
2522 schedstat_inc(rq, ttwu_count);
2523 schedstat_inc(p, se.statistics.nr_wakeups);
2524
2525 if (wake_flags & WF_SYNC)
2526 schedstat_inc(p, se.statistics.nr_wakeups_sync);
2527
Peter Zijlstrad7c01d22011-04-05 17:23:43 +02002528#endif /* CONFIG_SCHEDSTATS */
Tejun Heo9ed38112009-12-03 15:08:03 +09002529}
2530
Peter Zijlstrad7c01d22011-04-05 17:23:43 +02002531static void ttwu_activate(struct rq *rq, struct task_struct *p, int en_flags)
Tejun Heo9ed38112009-12-03 15:08:03 +09002532{
Tejun Heo9ed38112009-12-03 15:08:03 +09002533 activate_task(rq, p, en_flags);
Peter Zijlstrafd2f4412011-04-05 17:23:44 +02002534 p->on_rq = 1;
Peter Zijlstrac2f71152011-04-13 13:28:56 +02002535
2536 /* if a worker is waking up, notify workqueue */
2537 if (p->flags & PF_WQ_WORKER)
2538 wq_worker_waking_up(p, cpu_of(rq));
Tejun Heo9ed38112009-12-03 15:08:03 +09002539}
2540
Peter Zijlstra23f41ee2011-04-05 17:23:56 +02002541/*
2542 * Mark the task runnable and perform wakeup-preemption.
2543 */
Peter Zijlstra89363382011-04-05 17:23:42 +02002544static void
Peter Zijlstra23f41ee2011-04-05 17:23:56 +02002545ttwu_do_wakeup(struct rq *rq, struct task_struct *p, int wake_flags)
Tejun Heo9ed38112009-12-03 15:08:03 +09002546{
Peter Zijlstra89363382011-04-05 17:23:42 +02002547 trace_sched_wakeup(p, true);
Tejun Heo9ed38112009-12-03 15:08:03 +09002548 check_preempt_curr(rq, p, wake_flags);
2549
2550 p->state = TASK_RUNNING;
2551#ifdef CONFIG_SMP
2552 if (p->sched_class->task_woken)
2553 p->sched_class->task_woken(rq, p);
2554
Steven Rostedte69c6342010-12-06 17:10:31 -05002555 if (rq->idle_stamp) {
Tejun Heo9ed38112009-12-03 15:08:03 +09002556 u64 delta = rq->clock - rq->idle_stamp;
2557 u64 max = 2*sysctl_sched_migration_cost;
2558
2559 if (delta > max)
2560 rq->avg_idle = max;
2561 else
2562 update_avg(&rq->avg_idle, delta);
2563 rq->idle_stamp = 0;
2564 }
2565#endif
2566}
2567
Peter Zijlstrac05fbaf2011-04-05 17:23:57 +02002568static void
2569ttwu_do_activate(struct rq *rq, struct task_struct *p, int wake_flags)
2570{
2571#ifdef CONFIG_SMP
2572 if (p->sched_contributes_to_load)
2573 rq->nr_uninterruptible--;
2574#endif
2575
2576 ttwu_activate(rq, p, ENQUEUE_WAKEUP | ENQUEUE_WAKING);
2577 ttwu_do_wakeup(rq, p, wake_flags);
2578}
2579
2580/*
2581 * Called in case the task @p isn't fully descheduled from its runqueue,
2582 * in this case we must do a remote wakeup. Its a 'light' wakeup though,
2583 * since all we need to do is flip p->state to TASK_RUNNING, since
2584 * the task is still ->on_rq.
2585 */
2586static int ttwu_remote(struct task_struct *p, int wake_flags)
2587{
2588 struct rq *rq;
2589 int ret = 0;
2590
2591 rq = __task_rq_lock(p);
2592 if (p->on_rq) {
2593 ttwu_do_wakeup(rq, p, wake_flags);
2594 ret = 1;
2595 }
2596 __task_rq_unlock(rq);
2597
2598 return ret;
2599}
2600
Peter Zijlstra317f3942011-04-05 17:23:58 +02002601#ifdef CONFIG_SMP
Peter Zijlstrac5d753a2011-07-19 15:07:25 -07002602static void sched_ttwu_do_pending(struct task_struct *list)
Peter Zijlstra317f3942011-04-05 17:23:58 +02002603{
2604 struct rq *rq = this_rq();
Peter Zijlstra317f3942011-04-05 17:23:58 +02002605
2606 raw_spin_lock(&rq->lock);
2607
2608 while (list) {
2609 struct task_struct *p = list;
2610 list = list->wake_entry;
2611 ttwu_do_activate(rq, p, 0);
2612 }
2613
2614 raw_spin_unlock(&rq->lock);
2615}
2616
Peter Zijlstrac5d753a2011-07-19 15:07:25 -07002617#ifdef CONFIG_HOTPLUG_CPU
2618
2619static void sched_ttwu_pending(void)
2620{
2621 struct rq *rq = this_rq();
2622 struct task_struct *list = xchg(&rq->wake_list, NULL);
2623
2624 if (!list)
2625 return;
2626
2627 sched_ttwu_do_pending(list);
2628}
2629
2630#endif /* CONFIG_HOTPLUG_CPU */
2631
Peter Zijlstra317f3942011-04-05 17:23:58 +02002632void scheduler_ipi(void)
2633{
Peter Zijlstrac5d753a2011-07-19 15:07:25 -07002634 struct rq *rq = this_rq();
2635 struct task_struct *list = xchg(&rq->wake_list, NULL);
2636
2637 if (!list)
2638 return;
2639
2640 /*
2641 * Not all reschedule IPI handlers call irq_enter/irq_exit, since
2642 * traditionally all their work was done from the interrupt return
2643 * path. Now that we actually do some work, we need to make sure
2644 * we do call them.
2645 *
2646 * Some archs already do call them, luckily irq_enter/exit nest
2647 * properly.
2648 *
2649 * Arguably we should visit all archs and update all handlers,
2650 * however a fair share of IPIs are still resched only so this would
2651 * somewhat pessimize the simple resched case.
2652 */
2653 irq_enter();
2654 sched_ttwu_do_pending(list);
2655 irq_exit();
Peter Zijlstra317f3942011-04-05 17:23:58 +02002656}
2657
2658static void ttwu_queue_remote(struct task_struct *p, int cpu)
2659{
2660 struct rq *rq = cpu_rq(cpu);
2661 struct task_struct *next = rq->wake_list;
2662
2663 for (;;) {
2664 struct task_struct *old = next;
2665
2666 p->wake_entry = next;
2667 next = cmpxchg(&rq->wake_list, old, p);
2668 if (next == old)
2669 break;
2670 }
2671
2672 if (!next)
2673 smp_send_reschedule(cpu);
2674}
Peter Zijlstrad6aa8f82011-05-26 14:21:33 +02002675
2676#ifdef __ARCH_WANT_INTERRUPTS_ON_CTXSW
2677static int ttwu_activate_remote(struct task_struct *p, int wake_flags)
2678{
2679 struct rq *rq;
2680 int ret = 0;
2681
2682 rq = __task_rq_lock(p);
2683 if (p->on_cpu) {
2684 ttwu_activate(rq, p, ENQUEUE_WAKEUP);
2685 ttwu_do_wakeup(rq, p, wake_flags);
2686 ret = 1;
2687 }
2688 __task_rq_unlock(rq);
2689
2690 return ret;
2691
2692}
2693#endif /* __ARCH_WANT_INTERRUPTS_ON_CTXSW */
2694#endif /* CONFIG_SMP */
Peter Zijlstra317f3942011-04-05 17:23:58 +02002695
Peter Zijlstrac05fbaf2011-04-05 17:23:57 +02002696static void ttwu_queue(struct task_struct *p, int cpu)
2697{
2698 struct rq *rq = cpu_rq(cpu);
2699
Daniel Hellstrom17d9f312011-05-20 04:01:10 +00002700#if defined(CONFIG_SMP)
Peter Zijlstra317f3942011-04-05 17:23:58 +02002701 if (sched_feat(TTWU_QUEUE) && cpu != smp_processor_id()) {
Peter Zijlstraf01114c2011-05-31 12:26:55 +02002702 sched_clock_cpu(cpu); /* sync clocks x-cpu */
Peter Zijlstra317f3942011-04-05 17:23:58 +02002703 ttwu_queue_remote(p, cpu);
2704 return;
2705 }
2706#endif
2707
Peter Zijlstrac05fbaf2011-04-05 17:23:57 +02002708 raw_spin_lock(&rq->lock);
2709 ttwu_do_activate(rq, p, 0);
2710 raw_spin_unlock(&rq->lock);
Tejun Heo9ed38112009-12-03 15:08:03 +09002711}
2712
2713/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07002714 * try_to_wake_up - wake up a thread
Tejun Heo9ed38112009-12-03 15:08:03 +09002715 * @p: the thread to be awakened
Linus Torvalds1da177e2005-04-16 15:20:36 -07002716 * @state: the mask of task states that can be woken
Tejun Heo9ed38112009-12-03 15:08:03 +09002717 * @wake_flags: wake modifier flags (WF_*)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002718 *
2719 * Put it on the run-queue if it's not already there. The "current"
2720 * thread is always on the run-queue (except when the actual
2721 * re-schedule is in progress), and as such you're allowed to do
2722 * the simpler "current->state = TASK_RUNNING" to mark yourself
2723 * runnable without the overhead of this.
2724 *
Tejun Heo9ed38112009-12-03 15:08:03 +09002725 * Returns %true if @p was woken up, %false if it was already running
2726 * or @state didn't match @p's state.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002727 */
Peter Zijlstrae4a52bc2011-04-05 17:23:54 +02002728static int
2729try_to_wake_up(struct task_struct *p, unsigned int state, int wake_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002730{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002731 unsigned long flags;
Peter Zijlstrac05fbaf2011-04-05 17:23:57 +02002732 int cpu, success = 0;
Peter Zijlstra2398f2c2008-06-27 13:41:35 +02002733
Linus Torvalds04e2f172008-02-23 18:05:03 -08002734 smp_wmb();
Peter Zijlstra013fdb82011-04-05 17:23:45 +02002735 raw_spin_lock_irqsave(&p->pi_lock, flags);
Peter Zijlstrae9c84312009-09-15 14:43:03 +02002736 if (!(p->state & state))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002737 goto out;
2738
Peter Zijlstrac05fbaf2011-04-05 17:23:57 +02002739 success = 1; /* we're going to change ->state */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002740 cpu = task_cpu(p);
Peter Zijlstrad7c01d22011-04-05 17:23:43 +02002741
Peter Zijlstrac05fbaf2011-04-05 17:23:57 +02002742 if (p->on_rq && ttwu_remote(p, wake_flags))
2743 goto stat;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002744
2745#ifdef CONFIG_SMP
Peter Zijlstrae9c84312009-09-15 14:43:03 +02002746 /*
Peter Zijlstrac05fbaf2011-04-05 17:23:57 +02002747 * If the owning (remote) cpu is still in the middle of schedule() with
2748 * this task as prev, wait until its done referencing the task.
Peter Zijlstrae9c84312009-09-15 14:43:03 +02002749 */
Peter Zijlstrae4a52bc2011-04-05 17:23:54 +02002750 while (p->on_cpu) {
2751#ifdef __ARCH_WANT_INTERRUPTS_ON_CTXSW
2752 /*
Peter Zijlstrad6aa8f82011-05-26 14:21:33 +02002753 * In case the architecture enables interrupts in
2754 * context_switch(), we cannot busy wait, since that
2755 * would lead to deadlocks when an interrupt hits and
2756 * tries to wake up @prev. So bail and do a complete
2757 * remote wakeup.
Peter Zijlstrae4a52bc2011-04-05 17:23:54 +02002758 */
Peter Zijlstrad6aa8f82011-05-26 14:21:33 +02002759 if (ttwu_activate_remote(p, wake_flags))
Peter Zijlstrac05fbaf2011-04-05 17:23:57 +02002760 goto stat;
Peter Zijlstrad6aa8f82011-05-26 14:21:33 +02002761#else
Peter Zijlstrae4a52bc2011-04-05 17:23:54 +02002762 cpu_relax();
Peter Zijlstrad6aa8f82011-05-26 14:21:33 +02002763#endif
Peter Zijlstracc87f762010-03-26 12:22:14 +01002764 }
Peter Zijlstrae4a52bc2011-04-05 17:23:54 +02002765 /*
2766 * Pairs with the smp_wmb() in finish_lock_switch().
2767 */
2768 smp_rmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002769
Peter Zijlstraa8e4f2e2011-04-05 17:23:49 +02002770 p->sched_contributes_to_load = !!task_contributes_to_load(p);
Peter Zijlstrae9c84312009-09-15 14:43:03 +02002771 p->state = TASK_WAKING;
Peter Zijlstraefbbd052009-12-16 18:04:40 +01002772
Peter Zijlstrae4a52bc2011-04-05 17:23:54 +02002773 if (p->sched_class->task_waking)
Peter Zijlstra74f8e4b2011-04-05 17:23:47 +02002774 p->sched_class->task_waking(p);
Peter Zijlstraab19cb22009-11-27 15:44:43 +01002775
Peter Zijlstra7608dec2011-04-05 17:23:46 +02002776 cpu = select_task_rq(p, SD_BALANCE_WAKE, wake_flags);
Peter Zijlstraf339b9d2011-05-31 10:49:20 +02002777 if (task_cpu(p) != cpu) {
2778 wake_flags |= WF_MIGRATED;
Mike Galbraithf5dc3752009-10-09 08:35:03 +02002779 set_task_cpu(p, cpu);
Peter Zijlstraf339b9d2011-05-31 10:49:20 +02002780 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002781#endif /* CONFIG_SMP */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002782
Peter Zijlstrac05fbaf2011-04-05 17:23:57 +02002783 ttwu_queue(p, cpu);
2784stat:
Peter Zijlstrab84cb5d2011-04-05 17:23:55 +02002785 ttwu_stat(p, cpu, wake_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002786out:
Peter Zijlstra013fdb82011-04-05 17:23:45 +02002787 raw_spin_unlock_irqrestore(&p->pi_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002788
2789 return success;
2790}
2791
David Howells50fa6102009-04-28 15:01:38 +01002792/**
Tejun Heo21aa9af2010-06-08 21:40:37 +02002793 * try_to_wake_up_local - try to wake up a local task with rq lock held
2794 * @p: the thread to be awakened
2795 *
Peter Zijlstra2acca552011-04-05 17:23:50 +02002796 * Put @p on the run-queue if it's not already there. The caller must
Tejun Heo21aa9af2010-06-08 21:40:37 +02002797 * ensure that this_rq() is locked, @p is bound to this_rq() and not
Peter Zijlstra2acca552011-04-05 17:23:50 +02002798 * the current task.
Tejun Heo21aa9af2010-06-08 21:40:37 +02002799 */
2800static void try_to_wake_up_local(struct task_struct *p)
2801{
2802 struct rq *rq = task_rq(p);
Tejun Heo21aa9af2010-06-08 21:40:37 +02002803
2804 BUG_ON(rq != this_rq());
2805 BUG_ON(p == current);
2806 lockdep_assert_held(&rq->lock);
2807
Peter Zijlstra2acca552011-04-05 17:23:50 +02002808 if (!raw_spin_trylock(&p->pi_lock)) {
2809 raw_spin_unlock(&rq->lock);
2810 raw_spin_lock(&p->pi_lock);
2811 raw_spin_lock(&rq->lock);
Tejun Heo21aa9af2010-06-08 21:40:37 +02002812 }
Peter Zijlstra2acca552011-04-05 17:23:50 +02002813
Tejun Heo21aa9af2010-06-08 21:40:37 +02002814 if (!(p->state & TASK_NORMAL))
Peter Zijlstra2acca552011-04-05 17:23:50 +02002815 goto out;
Tejun Heo21aa9af2010-06-08 21:40:37 +02002816
Peter Zijlstrafd2f4412011-04-05 17:23:44 +02002817 if (!p->on_rq)
Peter Zijlstrad7c01d22011-04-05 17:23:43 +02002818 ttwu_activate(rq, p, ENQUEUE_WAKEUP);
2819
Peter Zijlstra23f41ee2011-04-05 17:23:56 +02002820 ttwu_do_wakeup(rq, p, 0);
Peter Zijlstrab84cb5d2011-04-05 17:23:55 +02002821 ttwu_stat(p, smp_processor_id(), 0);
Peter Zijlstra2acca552011-04-05 17:23:50 +02002822out:
2823 raw_spin_unlock(&p->pi_lock);
Tejun Heo21aa9af2010-06-08 21:40:37 +02002824}
2825
2826/**
David Howells50fa6102009-04-28 15:01:38 +01002827 * wake_up_process - Wake up a specific process
2828 * @p: The process to be woken up.
2829 *
2830 * Attempt to wake up the nominated process and move it to the set of runnable
2831 * processes. Returns 1 if the process was woken up, 0 if it was already
2832 * running.
2833 *
2834 * It may be assumed that this function implies a write memory barrier before
2835 * changing the task state if and only if any tasks are woken up.
2836 */
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -08002837int wake_up_process(struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002838{
Matthew Wilcoxd9514f62007-12-06 11:07:07 -05002839 return try_to_wake_up(p, TASK_ALL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002840}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002841EXPORT_SYMBOL(wake_up_process);
2842
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -08002843int wake_up_state(struct task_struct *p, unsigned int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002844{
2845 return try_to_wake_up(p, state, 0);
2846}
2847
Linus Torvalds1da177e2005-04-16 15:20:36 -07002848/*
2849 * Perform scheduler related setup for a newly forked process p.
2850 * p is forked by current.
Ingo Molnardd41f592007-07-09 18:51:59 +02002851 *
2852 * __sched_fork() is basic setup used by init_idle() too:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002853 */
Ingo Molnardd41f592007-07-09 18:51:59 +02002854static void __sched_fork(struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002855{
Peter Zijlstrafd2f4412011-04-05 17:23:44 +02002856 p->on_rq = 0;
2857
2858 p->se.on_rq = 0;
Ingo Molnardd41f592007-07-09 18:51:59 +02002859 p->se.exec_start = 0;
2860 p->se.sum_exec_runtime = 0;
Ingo Molnarf6cf8912007-08-28 12:53:24 +02002861 p->se.prev_sum_exec_runtime = 0;
Ingo Molnar6c594c22008-12-14 12:34:15 +01002862 p->se.nr_migrations = 0;
Peter Zijlstrada7a7352011-01-17 17:03:27 +01002863 p->se.vruntime = 0;
Peter Zijlstrafd2f4412011-04-05 17:23:44 +02002864 INIT_LIST_HEAD(&p->se.group_node);
Ingo Molnar6cfb0d52007-08-02 17:41:40 +02002865
2866#ifdef CONFIG_SCHEDSTATS
Lucas De Marchi41acab82010-03-10 23:37:45 -03002867 memset(&p->se.statistics, 0, sizeof(p->se.statistics));
Ingo Molnar6cfb0d52007-08-02 17:41:40 +02002868#endif
Nick Piggin476d1392005-06-25 14:57:29 -07002869
Peter Zijlstrafa717062008-01-25 21:08:27 +01002870 INIT_LIST_HEAD(&p->rt.run_list);
Nick Piggin476d1392005-06-25 14:57:29 -07002871
Avi Kivitye107be32007-07-26 13:40:43 +02002872#ifdef CONFIG_PREEMPT_NOTIFIERS
2873 INIT_HLIST_HEAD(&p->preempt_notifiers);
2874#endif
Ingo Molnardd41f592007-07-09 18:51:59 +02002875}
2876
2877/*
2878 * fork()/clone()-time setup:
2879 */
Samir Bellabes3e51e3e2011-05-11 18:18:05 +02002880void sched_fork(struct task_struct *p)
Ingo Molnardd41f592007-07-09 18:51:59 +02002881{
Peter Zijlstra0122ec52011-04-05 17:23:51 +02002882 unsigned long flags;
Ingo Molnardd41f592007-07-09 18:51:59 +02002883 int cpu = get_cpu();
2884
2885 __sched_fork(p);
Peter Zijlstra06b83b52009-12-16 18:04:35 +01002886 /*
Peter Zijlstra0017d732010-03-24 18:34:10 +01002887 * We mark the process as running here. This guarantees that
Peter Zijlstra06b83b52009-12-16 18:04:35 +01002888 * nobody will actually run it, and a signal or other external
2889 * event cannot wake it up and insert it on the runqueue either.
2890 */
Peter Zijlstra0017d732010-03-24 18:34:10 +01002891 p->state = TASK_RUNNING;
Ingo Molnardd41f592007-07-09 18:51:59 +02002892
Ingo Molnarb29739f2006-06-27 02:54:51 -07002893 /*
Mike Galbraithc350a042011-07-27 17:14:55 +02002894 * Make sure we do not leak PI boosting priority to the child.
2895 */
2896 p->prio = current->normal_prio;
2897
2898 /*
Mike Galbraithb9dc29e2009-06-17 10:46:01 +02002899 * Revert to default priority/policy on fork if requested.
2900 */
2901 if (unlikely(p->sched_reset_on_fork)) {
Mike Galbraithc350a042011-07-27 17:14:55 +02002902 if (task_has_rt_policy(p)) {
Mike Galbraithb9dc29e2009-06-17 10:46:01 +02002903 p->policy = SCHED_NORMAL;
Mike Galbraith6c697bd2009-06-17 10:48:02 +02002904 p->static_prio = NICE_TO_PRIO(0);
Mike Galbraithc350a042011-07-27 17:14:55 +02002905 p->rt_priority = 0;
2906 } else if (PRIO_TO_NICE(p->static_prio) < 0)
2907 p->static_prio = NICE_TO_PRIO(0);
2908
2909 p->prio = p->normal_prio = __normal_prio(p);
2910 set_load_weight(p);
Mike Galbraith6c697bd2009-06-17 10:48:02 +02002911
Mike Galbraithb9dc29e2009-06-17 10:46:01 +02002912 /*
2913 * We don't need the reset flag anymore after the fork. It has
2914 * fulfilled its duty:
2915 */
2916 p->sched_reset_on_fork = 0;
2917 }
Lennart Poetteringca94c442009-06-15 17:17:47 +02002918
Hiroshi Shimamoto2ddbf952007-10-15 17:00:11 +02002919 if (!rt_prio(p->prio))
2920 p->sched_class = &fair_sched_class;
Ingo Molnarb29739f2006-06-27 02:54:51 -07002921
Peter Zijlstracd29fe62009-11-27 17:32:46 +01002922 if (p->sched_class->task_fork)
2923 p->sched_class->task_fork(p);
2924
Peter Zijlstra86951592010-06-22 11:44:53 +02002925 /*
2926 * The child is not yet in the pid-hash so no cgroup attach races,
2927 * and the cgroup is pinned to this child due to cgroup_fork()
2928 * is ran before sched_fork().
2929 *
2930 * Silence PROVE_RCU.
2931 */
Peter Zijlstra0122ec52011-04-05 17:23:51 +02002932 raw_spin_lock_irqsave(&p->pi_lock, flags);
Peter Zijlstra5f3edc12009-09-10 13:42:00 +02002933 set_task_cpu(p, cpu);
Peter Zijlstra0122ec52011-04-05 17:23:51 +02002934 raw_spin_unlock_irqrestore(&p->pi_lock, flags);
Peter Zijlstra5f3edc12009-09-10 13:42:00 +02002935
Chandra Seetharaman52f17b62006-07-14 00:24:38 -07002936#if defined(CONFIG_SCHEDSTATS) || defined(CONFIG_TASK_DELAY_ACCT)
Ingo Molnardd41f592007-07-09 18:51:59 +02002937 if (likely(sched_info_on()))
Chandra Seetharaman52f17b62006-07-14 00:24:38 -07002938 memset(&p->sched_info, 0, sizeof(p->sched_info));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002939#endif
Peter Zijlstra3ca7a442011-04-05 17:23:40 +02002940#if defined(CONFIG_SMP)
2941 p->on_cpu = 0;
Nick Piggin4866cde2005-06-25 14:57:23 -07002942#endif
Frederic Weisbeckerbdd4e852011-06-08 01:13:27 +02002943#ifdef CONFIG_PREEMPT_COUNT
Nick Piggin4866cde2005-06-25 14:57:23 -07002944 /* Want to start with kernel preemption disabled. */
Al Viroa1261f52005-11-13 16:06:55 -08002945 task_thread_info(p)->preempt_count = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002946#endif
Dario Faggioli806c09a2010-11-30 19:51:33 +01002947#ifdef CONFIG_SMP
Gregory Haskins917b6272008-12-29 09:39:53 -05002948 plist_node_init(&p->pushable_tasks, MAX_PRIO);
Dario Faggioli806c09a2010-11-30 19:51:33 +01002949#endif
Gregory Haskins917b6272008-12-29 09:39:53 -05002950
Nick Piggin476d1392005-06-25 14:57:29 -07002951 put_cpu();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002952}
2953
2954/*
2955 * wake_up_new_task - wake up a newly created task for the first time.
2956 *
2957 * This function will do some initial scheduler statistics housekeeping
2958 * that must be done for every newly created context, then puts the task
2959 * on the runqueue and wakes it.
2960 */
Samir Bellabes3e51e3e2011-05-11 18:18:05 +02002961void wake_up_new_task(struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002962{
2963 unsigned long flags;
Ingo Molnardd41f592007-07-09 18:51:59 +02002964 struct rq *rq;
Peter Zijlstrafabf3182010-01-21 21:04:57 +01002965
Peter Zijlstraab2515c2011-04-05 17:23:52 +02002966 raw_spin_lock_irqsave(&p->pi_lock, flags);
Peter Zijlstrafabf3182010-01-21 21:04:57 +01002967#ifdef CONFIG_SMP
2968 /*
2969 * Fork balancing, do it here and not earlier because:
2970 * - cpus_allowed can change in the fork path
2971 * - any previously selected cpu might disappear through hotplug
Peter Zijlstrafabf3182010-01-21 21:04:57 +01002972 */
Peter Zijlstraab2515c2011-04-05 17:23:52 +02002973 set_task_cpu(p, select_task_rq(p, SD_BALANCE_FORK, 0));
Peter Zijlstrafabf3182010-01-21 21:04:57 +01002974#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002975
Peter Zijlstraab2515c2011-04-05 17:23:52 +02002976 rq = __task_rq_lock(p);
Peter Zijlstracd29fe62009-11-27 17:32:46 +01002977 activate_task(rq, p, 0);
Peter Zijlstrafd2f4412011-04-05 17:23:44 +02002978 p->on_rq = 1;
Peter Zijlstra89363382011-04-05 17:23:42 +02002979 trace_sched_wakeup_new(p, true);
Peter Zijlstraa7558e02009-09-14 20:02:34 +02002980 check_preempt_curr(rq, p, WF_FORK);
Steven Rostedt9a897c52008-01-25 21:08:22 +01002981#ifdef CONFIG_SMP
Peter Zijlstraefbbd052009-12-16 18:04:40 +01002982 if (p->sched_class->task_woken)
2983 p->sched_class->task_woken(rq, p);
Steven Rostedt9a897c52008-01-25 21:08:22 +01002984#endif
Peter Zijlstra0122ec52011-04-05 17:23:51 +02002985 task_rq_unlock(rq, p, &flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002986}
2987
Avi Kivitye107be32007-07-26 13:40:43 +02002988#ifdef CONFIG_PREEMPT_NOTIFIERS
2989
2990/**
Luis Henriques80dd99b2009-03-16 19:58:09 +00002991 * preempt_notifier_register - tell me when current is being preempted & rescheduled
Randy Dunlap421cee22007-07-31 00:37:50 -07002992 * @notifier: notifier struct to register
Avi Kivitye107be32007-07-26 13:40:43 +02002993 */
2994void preempt_notifier_register(struct preempt_notifier *notifier)
2995{
2996 hlist_add_head(&notifier->link, &current->preempt_notifiers);
2997}
2998EXPORT_SYMBOL_GPL(preempt_notifier_register);
2999
3000/**
3001 * preempt_notifier_unregister - no longer interested in preemption notifications
Randy Dunlap421cee22007-07-31 00:37:50 -07003002 * @notifier: notifier struct to unregister
Avi Kivitye107be32007-07-26 13:40:43 +02003003 *
3004 * This is safe to call from within a preemption notifier.
3005 */
3006void preempt_notifier_unregister(struct preempt_notifier *notifier)
3007{
3008 hlist_del(&notifier->link);
3009}
3010EXPORT_SYMBOL_GPL(preempt_notifier_unregister);
3011
3012static void fire_sched_in_preempt_notifiers(struct task_struct *curr)
3013{
3014 struct preempt_notifier *notifier;
3015 struct hlist_node *node;
3016
3017 hlist_for_each_entry(notifier, node, &curr->preempt_notifiers, link)
3018 notifier->ops->sched_in(notifier, raw_smp_processor_id());
3019}
3020
3021static void
3022fire_sched_out_preempt_notifiers(struct task_struct *curr,
3023 struct task_struct *next)
3024{
3025 struct preempt_notifier *notifier;
3026 struct hlist_node *node;
3027
3028 hlist_for_each_entry(notifier, node, &curr->preempt_notifiers, link)
3029 notifier->ops->sched_out(notifier, next);
3030}
3031
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +02003032#else /* !CONFIG_PREEMPT_NOTIFIERS */
Avi Kivitye107be32007-07-26 13:40:43 +02003033
3034static void fire_sched_in_preempt_notifiers(struct task_struct *curr)
3035{
3036}
3037
3038static void
3039fire_sched_out_preempt_notifiers(struct task_struct *curr,
3040 struct task_struct *next)
3041{
3042}
3043
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +02003044#endif /* CONFIG_PREEMPT_NOTIFIERS */
Avi Kivitye107be32007-07-26 13:40:43 +02003045
Linus Torvalds1da177e2005-04-16 15:20:36 -07003046/**
Nick Piggin4866cde2005-06-25 14:57:23 -07003047 * prepare_task_switch - prepare to switch tasks
3048 * @rq: the runqueue preparing to switch
Randy Dunlap421cee22007-07-31 00:37:50 -07003049 * @prev: the current task that is being switched out
Nick Piggin4866cde2005-06-25 14:57:23 -07003050 * @next: the task we are going to switch to.
3051 *
3052 * This is called with the rq lock held and interrupts off. It must
3053 * be paired with a subsequent finish_task_switch after the context
3054 * switch.
3055 *
3056 * prepare_task_switch sets up locking and calls architecture specific
3057 * hooks.
3058 */
Avi Kivitye107be32007-07-26 13:40:43 +02003059static inline void
3060prepare_task_switch(struct rq *rq, struct task_struct *prev,
3061 struct task_struct *next)
Nick Piggin4866cde2005-06-25 14:57:23 -07003062{
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01003063 sched_info_switch(prev, next);
3064 perf_event_task_sched_out(prev, next);
Avi Kivitye107be32007-07-26 13:40:43 +02003065 fire_sched_out_preempt_notifiers(prev, next);
Nick Piggin4866cde2005-06-25 14:57:23 -07003066 prepare_lock_switch(rq, next);
3067 prepare_arch_switch(next);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01003068 trace_sched_switch(prev, next);
Nick Piggin4866cde2005-06-25 14:57:23 -07003069}
3070
3071/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07003072 * finish_task_switch - clean up after a task-switch
Jeff Garzik344baba2005-09-07 01:15:17 -04003073 * @rq: runqueue associated with task-switch
Linus Torvalds1da177e2005-04-16 15:20:36 -07003074 * @prev: the thread we just switched away from.
3075 *
Nick Piggin4866cde2005-06-25 14:57:23 -07003076 * finish_task_switch must be called after the context switch, paired
3077 * with a prepare_task_switch call before the context switch.
3078 * finish_task_switch will reconcile locking set up by prepare_task_switch,
3079 * and do any other architecture-specific cleanup actions.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003080 *
3081 * Note that we may have delayed dropping an mm in context_switch(). If
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01003082 * so, we finish that here outside of the runqueue lock. (Doing it
Linus Torvalds1da177e2005-04-16 15:20:36 -07003083 * with the lock held can cause deadlocks; see schedule() for
3084 * details.)
3085 */
Alexey Dobriyana9957442007-10-15 17:00:13 +02003086static void finish_task_switch(struct rq *rq, struct task_struct *prev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003087 __releases(rq->lock)
3088{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003089 struct mm_struct *mm = rq->prev_mm;
Oleg Nesterov55a101f2006-09-29 02:01:10 -07003090 long prev_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003091
3092 rq->prev_mm = NULL;
3093
3094 /*
3095 * A task struct has one reference for the use as "current".
Oleg Nesterovc394cc92006-09-29 02:01:11 -07003096 * If a task dies, then it sets TASK_DEAD in tsk->state and calls
Oleg Nesterov55a101f2006-09-29 02:01:10 -07003097 * schedule one last time. The schedule call will never return, and
3098 * the scheduled task must drop that reference.
Oleg Nesterovc394cc92006-09-29 02:01:11 -07003099 * The test for TASK_DEAD must occur while the runqueue locks are
Linus Torvalds1da177e2005-04-16 15:20:36 -07003100 * still held, otherwise prev could be scheduled on another cpu, die
3101 * there before we look at prev->state, and then the reference would
3102 * be dropped twice.
3103 * Manfred Spraul <manfred@colorfullife.com>
3104 */
Oleg Nesterov55a101f2006-09-29 02:01:10 -07003105 prev_state = prev->state;
Nick Piggin4866cde2005-06-25 14:57:23 -07003106 finish_arch_switch(prev);
Jamie Iles8381f652010-01-08 15:27:33 +00003107#ifdef __ARCH_WANT_INTERRUPTS_ON_CTXSW
3108 local_irq_disable();
3109#endif /* __ARCH_WANT_INTERRUPTS_ON_CTXSW */
Peter Zijlstra49f47432009-12-27 11:51:52 +01003110 perf_event_task_sched_in(current);
Jamie Iles8381f652010-01-08 15:27:33 +00003111#ifdef __ARCH_WANT_INTERRUPTS_ON_CTXSW
3112 local_irq_enable();
3113#endif /* __ARCH_WANT_INTERRUPTS_ON_CTXSW */
Nick Piggin4866cde2005-06-25 14:57:23 -07003114 finish_lock_switch(rq, prev);
Steven Rostedte8fa1362008-01-25 21:08:05 +01003115
Avi Kivitye107be32007-07-26 13:40:43 +02003116 fire_sched_in_preempt_notifiers(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003117 if (mm)
3118 mmdrop(mm);
Oleg Nesterovc394cc92006-09-29 02:01:11 -07003119 if (unlikely(prev_state == TASK_DEAD)) {
bibo maoc6fd91f2006-03-26 01:38:20 -08003120 /*
3121 * Remove function-return probe instances associated with this
3122 * task and put them back on the free list.
Ingo Molnar9761eea2007-07-09 18:52:00 +02003123 */
bibo maoc6fd91f2006-03-26 01:38:20 -08003124 kprobe_flush_task(prev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003125 put_task_struct(prev);
bibo maoc6fd91f2006-03-26 01:38:20 -08003126 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003127}
3128
Gregory Haskins3f029d32009-07-29 11:08:47 -04003129#ifdef CONFIG_SMP
3130
3131/* assumes rq->lock is held */
3132static inline void pre_schedule(struct rq *rq, struct task_struct *prev)
3133{
3134 if (prev->sched_class->pre_schedule)
3135 prev->sched_class->pre_schedule(rq, prev);
3136}
3137
3138/* rq->lock is NOT held, but preemption is disabled */
3139static inline void post_schedule(struct rq *rq)
3140{
3141 if (rq->post_schedule) {
3142 unsigned long flags;
3143
Thomas Gleixner05fa7852009-11-17 14:28:38 +01003144 raw_spin_lock_irqsave(&rq->lock, flags);
Gregory Haskins3f029d32009-07-29 11:08:47 -04003145 if (rq->curr->sched_class->post_schedule)
3146 rq->curr->sched_class->post_schedule(rq);
Thomas Gleixner05fa7852009-11-17 14:28:38 +01003147 raw_spin_unlock_irqrestore(&rq->lock, flags);
Gregory Haskins3f029d32009-07-29 11:08:47 -04003148
3149 rq->post_schedule = 0;
3150 }
3151}
3152
3153#else
3154
3155static inline void pre_schedule(struct rq *rq, struct task_struct *p)
3156{
3157}
3158
3159static inline void post_schedule(struct rq *rq)
3160{
3161}
3162
3163#endif
3164
Linus Torvalds1da177e2005-04-16 15:20:36 -07003165/**
3166 * schedule_tail - first thing a freshly forked thread must call.
3167 * @prev: the thread we just switched away from.
3168 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07003169asmlinkage void schedule_tail(struct task_struct *prev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003170 __releases(rq->lock)
3171{
Ingo Molnar70b97a72006-07-03 00:25:42 -07003172 struct rq *rq = this_rq();
3173
Nick Piggin4866cde2005-06-25 14:57:23 -07003174 finish_task_switch(rq, prev);
Steven Rostedtda19ab52009-07-29 00:21:22 -04003175
Gregory Haskins3f029d32009-07-29 11:08:47 -04003176 /*
3177 * FIXME: do we need to worry about rq being invalidated by the
3178 * task_switch?
3179 */
3180 post_schedule(rq);
Steven Rostedtda19ab52009-07-29 00:21:22 -04003181
Nick Piggin4866cde2005-06-25 14:57:23 -07003182#ifdef __ARCH_WANT_UNLOCKED_CTXSW
3183 /* In this case, finish_task_switch does not reenable preemption */
3184 preempt_enable();
3185#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003186 if (current->set_child_tid)
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003187 put_user(task_pid_vnr(current), current->set_child_tid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003188}
3189
3190/*
3191 * context_switch - switch to the new MM and the new
3192 * thread's register state.
3193 */
Ingo Molnardd41f592007-07-09 18:51:59 +02003194static inline void
Ingo Molnar70b97a72006-07-03 00:25:42 -07003195context_switch(struct rq *rq, struct task_struct *prev,
Ingo Molnar36c8b582006-07-03 00:25:41 -07003196 struct task_struct *next)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003197{
Ingo Molnardd41f592007-07-09 18:51:59 +02003198 struct mm_struct *mm, *oldmm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003199
Avi Kivitye107be32007-07-26 13:40:43 +02003200 prepare_task_switch(rq, prev, next);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01003201
Ingo Molnardd41f592007-07-09 18:51:59 +02003202 mm = next->mm;
3203 oldmm = prev->active_mm;
Zachary Amsden9226d122007-02-13 13:26:21 +01003204 /*
3205 * For paravirt, this is coupled with an exit in switch_to to
3206 * combine the page table reload and the switch backend into
3207 * one hypercall.
3208 */
Jeremy Fitzhardinge224101e2009-02-18 11:18:57 -08003209 arch_start_context_switch(prev);
Zachary Amsden9226d122007-02-13 13:26:21 +01003210
Heiko Carstens31915ab2010-09-16 14:42:25 +02003211 if (!mm) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003212 next->active_mm = oldmm;
3213 atomic_inc(&oldmm->mm_count);
3214 enter_lazy_tlb(oldmm, next);
3215 } else
3216 switch_mm(oldmm, mm, next);
3217
Heiko Carstens31915ab2010-09-16 14:42:25 +02003218 if (!prev->mm) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003219 prev->active_mm = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003220 rq->prev_mm = oldmm;
3221 }
Ingo Molnar3a5f5e42006-07-14 00:24:27 -07003222 /*
3223 * Since the runqueue lock will be released by the next
3224 * task (which is an invalid locking op but in the case
3225 * of the scheduler it's an obvious special-case), so we
3226 * do an early lockdep release here:
3227 */
3228#ifndef __ARCH_WANT_UNLOCKED_CTXSW
Ingo Molnar8a25d5d2006-07-03 00:24:54 -07003229 spin_release(&rq->lock.dep_map, 1, _THIS_IP_);
Ingo Molnar3a5f5e42006-07-14 00:24:27 -07003230#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003231
3232 /* Here we just switch the register state and the stack. */
3233 switch_to(prev, next, prev);
3234
Ingo Molnardd41f592007-07-09 18:51:59 +02003235 barrier();
3236 /*
3237 * this_rq must be evaluated again because prev may have moved
3238 * CPUs since it called schedule(), thus the 'rq' on its stack
3239 * frame will be invalid.
3240 */
3241 finish_task_switch(this_rq(), prev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003242}
3243
3244/*
3245 * nr_running, nr_uninterruptible and nr_context_switches:
3246 *
3247 * externally visible scheduler statistics: current number of runnable
3248 * threads, current number of uninterruptible-sleeping threads, total
3249 * number of context switches performed since bootup.
3250 */
3251unsigned long nr_running(void)
3252{
3253 unsigned long i, sum = 0;
3254
3255 for_each_online_cpu(i)
3256 sum += cpu_rq(i)->nr_running;
3257
3258 return sum;
3259}
3260
3261unsigned long nr_uninterruptible(void)
3262{
3263 unsigned long i, sum = 0;
3264
KAMEZAWA Hiroyuki0a945022006-03-28 01:56:37 -08003265 for_each_possible_cpu(i)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003266 sum += cpu_rq(i)->nr_uninterruptible;
3267
3268 /*
3269 * Since we read the counters lockless, it might be slightly
3270 * inaccurate. Do not allow it to go below zero though:
3271 */
3272 if (unlikely((long)sum < 0))
3273 sum = 0;
3274
3275 return sum;
3276}
3277
3278unsigned long long nr_context_switches(void)
3279{
Steven Rostedtcc94abf2006-06-27 02:54:31 -07003280 int i;
3281 unsigned long long sum = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003282
KAMEZAWA Hiroyuki0a945022006-03-28 01:56:37 -08003283 for_each_possible_cpu(i)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003284 sum += cpu_rq(i)->nr_switches;
3285
3286 return sum;
3287}
3288
3289unsigned long nr_iowait(void)
3290{
3291 unsigned long i, sum = 0;
3292
KAMEZAWA Hiroyuki0a945022006-03-28 01:56:37 -08003293 for_each_possible_cpu(i)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003294 sum += atomic_read(&cpu_rq(i)->nr_iowait);
3295
3296 return sum;
3297}
3298
Peter Zijlstra8c215bd2010-07-01 09:07:17 +02003299unsigned long nr_iowait_cpu(int cpu)
Arjan van de Ven69d25872009-09-21 17:04:08 -07003300{
Peter Zijlstra8c215bd2010-07-01 09:07:17 +02003301 struct rq *this = cpu_rq(cpu);
Arjan van de Ven69d25872009-09-21 17:04:08 -07003302 return atomic_read(&this->nr_iowait);
3303}
3304
3305unsigned long this_cpu_load(void)
3306{
3307 struct rq *this = this_rq();
3308 return this->cpu_load[0];
3309}
3310
3311
Thomas Gleixnerdce48a82009-04-11 10:43:41 +02003312/* Variables and functions for calc_load */
3313static atomic_long_t calc_load_tasks;
3314static unsigned long calc_load_update;
3315unsigned long avenrun[3];
3316EXPORT_SYMBOL(avenrun);
3317
Peter Zijlstra74f51872010-04-22 21:50:19 +02003318static long calc_load_fold_active(struct rq *this_rq)
3319{
3320 long nr_active, delta = 0;
3321
3322 nr_active = this_rq->nr_running;
3323 nr_active += (long) this_rq->nr_uninterruptible;
3324
3325 if (nr_active != this_rq->calc_load_active) {
3326 delta = nr_active - this_rq->calc_load_active;
3327 this_rq->calc_load_active = nr_active;
3328 }
3329
3330 return delta;
3331}
3332
Peter Zijlstra0f004f52010-11-30 19:48:45 +01003333static unsigned long
3334calc_load(unsigned long load, unsigned long exp, unsigned long active)
3335{
3336 load *= exp;
3337 load += active * (FIXED_1 - exp);
3338 load += 1UL << (FSHIFT - 1);
3339 return load >> FSHIFT;
3340}
3341
Peter Zijlstra74f51872010-04-22 21:50:19 +02003342#ifdef CONFIG_NO_HZ
3343/*
3344 * For NO_HZ we delay the active fold to the next LOAD_FREQ update.
3345 *
3346 * When making the ILB scale, we should try to pull this in as well.
3347 */
3348static atomic_long_t calc_load_tasks_idle;
3349
3350static void calc_load_account_idle(struct rq *this_rq)
3351{
3352 long delta;
3353
3354 delta = calc_load_fold_active(this_rq);
3355 if (delta)
3356 atomic_long_add(delta, &calc_load_tasks_idle);
3357}
3358
3359static long calc_load_fold_idle(void)
3360{
3361 long delta = 0;
3362
3363 /*
3364 * Its got a race, we don't care...
3365 */
3366 if (atomic_long_read(&calc_load_tasks_idle))
3367 delta = atomic_long_xchg(&calc_load_tasks_idle, 0);
3368
3369 return delta;
3370}
Peter Zijlstra0f004f52010-11-30 19:48:45 +01003371
3372/**
3373 * fixed_power_int - compute: x^n, in O(log n) time
3374 *
3375 * @x: base of the power
3376 * @frac_bits: fractional bits of @x
3377 * @n: power to raise @x to.
3378 *
3379 * By exploiting the relation between the definition of the natural power
3380 * function: x^n := x*x*...*x (x multiplied by itself for n times), and
3381 * the binary encoding of numbers used by computers: n := \Sum n_i * 2^i,
3382 * (where: n_i \elem {0, 1}, the binary vector representing n),
3383 * we find: x^n := x^(\Sum n_i * 2^i) := \Prod x^(n_i * 2^i), which is
3384 * of course trivially computable in O(log_2 n), the length of our binary
3385 * vector.
3386 */
3387static unsigned long
3388fixed_power_int(unsigned long x, unsigned int frac_bits, unsigned int n)
3389{
3390 unsigned long result = 1UL << frac_bits;
3391
3392 if (n) for (;;) {
3393 if (n & 1) {
3394 result *= x;
3395 result += 1UL << (frac_bits - 1);
3396 result >>= frac_bits;
3397 }
3398 n >>= 1;
3399 if (!n)
3400 break;
3401 x *= x;
3402 x += 1UL << (frac_bits - 1);
3403 x >>= frac_bits;
3404 }
3405
3406 return result;
3407}
3408
3409/*
3410 * a1 = a0 * e + a * (1 - e)
3411 *
3412 * a2 = a1 * e + a * (1 - e)
3413 * = (a0 * e + a * (1 - e)) * e + a * (1 - e)
3414 * = a0 * e^2 + a * (1 - e) * (1 + e)
3415 *
3416 * a3 = a2 * e + a * (1 - e)
3417 * = (a0 * e^2 + a * (1 - e) * (1 + e)) * e + a * (1 - e)
3418 * = a0 * e^3 + a * (1 - e) * (1 + e + e^2)
3419 *
3420 * ...
3421 *
3422 * an = a0 * e^n + a * (1 - e) * (1 + e + ... + e^n-1) [1]
3423 * = a0 * e^n + a * (1 - e) * (1 - e^n)/(1 - e)
3424 * = a0 * e^n + a * (1 - e^n)
3425 *
3426 * [1] application of the geometric series:
3427 *
3428 * n 1 - x^(n+1)
3429 * S_n := \Sum x^i = -------------
3430 * i=0 1 - x
3431 */
3432static unsigned long
3433calc_load_n(unsigned long load, unsigned long exp,
3434 unsigned long active, unsigned int n)
3435{
3436
3437 return calc_load(load, fixed_power_int(exp, FSHIFT, n), active);
3438}
3439
3440/*
3441 * NO_HZ can leave us missing all per-cpu ticks calling
3442 * calc_load_account_active(), but since an idle CPU folds its delta into
3443 * calc_load_tasks_idle per calc_load_account_idle(), all we need to do is fold
3444 * in the pending idle delta if our idle period crossed a load cycle boundary.
3445 *
3446 * Once we've updated the global active value, we need to apply the exponential
3447 * weights adjusted to the number of cycles missed.
3448 */
3449static void calc_global_nohz(unsigned long ticks)
3450{
3451 long delta, active, n;
3452
3453 if (time_before(jiffies, calc_load_update))
3454 return;
3455
3456 /*
3457 * If we crossed a calc_load_update boundary, make sure to fold
3458 * any pending idle changes, the respective CPUs might have
3459 * missed the tick driven calc_load_account_active() update
3460 * due to NO_HZ.
3461 */
3462 delta = calc_load_fold_idle();
3463 if (delta)
3464 atomic_long_add(delta, &calc_load_tasks);
3465
3466 /*
3467 * If we were idle for multiple load cycles, apply them.
3468 */
3469 if (ticks >= LOAD_FREQ) {
3470 n = ticks / LOAD_FREQ;
3471
3472 active = atomic_long_read(&calc_load_tasks);
3473 active = active > 0 ? active * FIXED_1 : 0;
3474
3475 avenrun[0] = calc_load_n(avenrun[0], EXP_1, active, n);
3476 avenrun[1] = calc_load_n(avenrun[1], EXP_5, active, n);
3477 avenrun[2] = calc_load_n(avenrun[2], EXP_15, active, n);
3478
3479 calc_load_update += n * LOAD_FREQ;
3480 }
3481
3482 /*
3483 * Its possible the remainder of the above division also crosses
3484 * a LOAD_FREQ period, the regular check in calc_global_load()
3485 * which comes after this will take care of that.
3486 *
3487 * Consider us being 11 ticks before a cycle completion, and us
3488 * sleeping for 4*LOAD_FREQ + 22 ticks, then the above code will
3489 * age us 4 cycles, and the test in calc_global_load() will
3490 * pick up the final one.
3491 */
3492}
Peter Zijlstra74f51872010-04-22 21:50:19 +02003493#else
3494static void calc_load_account_idle(struct rq *this_rq)
3495{
3496}
3497
3498static inline long calc_load_fold_idle(void)
3499{
3500 return 0;
3501}
Peter Zijlstra0f004f52010-11-30 19:48:45 +01003502
3503static void calc_global_nohz(unsigned long ticks)
3504{
3505}
Peter Zijlstra74f51872010-04-22 21:50:19 +02003506#endif
3507
Thomas Gleixner2d024942009-05-02 20:08:52 +02003508/**
3509 * get_avenrun - get the load average array
3510 * @loads: pointer to dest load array
3511 * @offset: offset to add
3512 * @shift: shift count to shift the result left
3513 *
3514 * These values are estimates at best, so no need for locking.
3515 */
3516void get_avenrun(unsigned long *loads, unsigned long offset, int shift)
3517{
3518 loads[0] = (avenrun[0] + offset) << shift;
3519 loads[1] = (avenrun[1] + offset) << shift;
3520 loads[2] = (avenrun[2] + offset) << shift;
3521}
3522
Thomas Gleixnerdce48a82009-04-11 10:43:41 +02003523/*
3524 * calc_load - update the avenrun load estimates 10 ticks after the
3525 * CPUs have updated calc_load_tasks.
3526 */
Peter Zijlstra0f004f52010-11-30 19:48:45 +01003527void calc_global_load(unsigned long ticks)
Thomas Gleixnerdce48a82009-04-11 10:43:41 +02003528{
Thomas Gleixnerdce48a82009-04-11 10:43:41 +02003529 long active;
3530
Peter Zijlstra0f004f52010-11-30 19:48:45 +01003531 calc_global_nohz(ticks);
3532
3533 if (time_before(jiffies, calc_load_update + 10))
Thomas Gleixnerdce48a82009-04-11 10:43:41 +02003534 return;
3535
3536 active = atomic_long_read(&calc_load_tasks);
3537 active = active > 0 ? active * FIXED_1 : 0;
3538
3539 avenrun[0] = calc_load(avenrun[0], EXP_1, active);
3540 avenrun[1] = calc_load(avenrun[1], EXP_5, active);
3541 avenrun[2] = calc_load(avenrun[2], EXP_15, active);
3542
3543 calc_load_update += LOAD_FREQ;
3544}
3545
3546/*
Peter Zijlstra74f51872010-04-22 21:50:19 +02003547 * Called from update_cpu_load() to periodically update this CPU's
3548 * active count.
Thomas Gleixnerdce48a82009-04-11 10:43:41 +02003549 */
3550static void calc_load_account_active(struct rq *this_rq)
3551{
Peter Zijlstra74f51872010-04-22 21:50:19 +02003552 long delta;
Thomas Gleixnerdce48a82009-04-11 10:43:41 +02003553
Peter Zijlstra74f51872010-04-22 21:50:19 +02003554 if (time_before(jiffies, this_rq->calc_load_update))
3555 return;
Thomas Gleixnerdce48a82009-04-11 10:43:41 +02003556
Peter Zijlstra74f51872010-04-22 21:50:19 +02003557 delta = calc_load_fold_active(this_rq);
3558 delta += calc_load_fold_idle();
3559 if (delta)
Thomas Gleixnerdce48a82009-04-11 10:43:41 +02003560 atomic_long_add(delta, &calc_load_tasks);
Peter Zijlstra74f51872010-04-22 21:50:19 +02003561
3562 this_rq->calc_load_update += LOAD_FREQ;
Jack Steinerdb1b1fe2006-03-31 02:31:21 -08003563}
3564
Linus Torvalds1da177e2005-04-16 15:20:36 -07003565/*
Venkatesh Pallipadifdf3e952010-05-17 18:14:43 -07003566 * The exact cpuload at various idx values, calculated at every tick would be
3567 * load = (2^idx - 1) / 2^idx * load + 1 / 2^idx * cur_load
3568 *
3569 * If a cpu misses updates for n-1 ticks (as it was idle) and update gets called
3570 * on nth tick when cpu may be busy, then we have:
3571 * load = ((2^idx - 1) / 2^idx)^(n-1) * load
3572 * load = (2^idx - 1) / 2^idx) * load + 1 / 2^idx * cur_load
3573 *
3574 * decay_load_missed() below does efficient calculation of
3575 * load = ((2^idx - 1) / 2^idx)^(n-1) * load
3576 * avoiding 0..n-1 loop doing load = ((2^idx - 1) / 2^idx) * load
3577 *
3578 * The calculation is approximated on a 128 point scale.
3579 * degrade_zero_ticks is the number of ticks after which load at any
3580 * particular idx is approximated to be zero.
3581 * degrade_factor is a precomputed table, a row for each load idx.
3582 * Each column corresponds to degradation factor for a power of two ticks,
3583 * based on 128 point scale.
3584 * Example:
3585 * row 2, col 3 (=12) says that the degradation at load idx 2 after
3586 * 8 ticks is 12/128 (which is an approximation of exact factor 3^8/4^8).
3587 *
3588 * With this power of 2 load factors, we can degrade the load n times
3589 * by looking at 1 bits in n and doing as many mult/shift instead of
3590 * n mult/shifts needed by the exact degradation.
3591 */
3592#define DEGRADE_SHIFT 7
3593static const unsigned char
3594 degrade_zero_ticks[CPU_LOAD_IDX_MAX] = {0, 8, 32, 64, 128};
3595static const unsigned char
3596 degrade_factor[CPU_LOAD_IDX_MAX][DEGRADE_SHIFT + 1] = {
3597 {0, 0, 0, 0, 0, 0, 0, 0},
3598 {64, 32, 8, 0, 0, 0, 0, 0},
3599 {96, 72, 40, 12, 1, 0, 0},
3600 {112, 98, 75, 43, 15, 1, 0},
3601 {120, 112, 98, 76, 45, 16, 2} };
3602
3603/*
3604 * Update cpu_load for any missed ticks, due to tickless idle. The backlog
3605 * would be when CPU is idle and so we just decay the old load without
3606 * adding any new load.
3607 */
3608static unsigned long
3609decay_load_missed(unsigned long load, unsigned long missed_updates, int idx)
3610{
3611 int j = 0;
3612
3613 if (!missed_updates)
3614 return load;
3615
3616 if (missed_updates >= degrade_zero_ticks[idx])
3617 return 0;
3618
3619 if (idx == 1)
3620 return load >> missed_updates;
3621
3622 while (missed_updates) {
3623 if (missed_updates % 2)
3624 load = (load * degrade_factor[idx][j]) >> DEGRADE_SHIFT;
3625
3626 missed_updates >>= 1;
3627 j++;
3628 }
3629 return load;
3630}
3631
3632/*
Ingo Molnardd41f592007-07-09 18:51:59 +02003633 * Update rq->cpu_load[] statistics. This function is usually called every
Venkatesh Pallipadifdf3e952010-05-17 18:14:43 -07003634 * scheduler tick (TICK_NSEC). With tickless idle this will not be called
3635 * every tick. We fix it up based on jiffies.
Ingo Molnar48f24c42006-07-03 00:25:40 -07003636 */
Ingo Molnardd41f592007-07-09 18:51:59 +02003637static void update_cpu_load(struct rq *this_rq)
Ingo Molnar48f24c42006-07-03 00:25:40 -07003638{
Dmitry Adamushko495eca42007-10-15 17:00:06 +02003639 unsigned long this_load = this_rq->load.weight;
Venkatesh Pallipadifdf3e952010-05-17 18:14:43 -07003640 unsigned long curr_jiffies = jiffies;
3641 unsigned long pending_updates;
Ingo Molnardd41f592007-07-09 18:51:59 +02003642 int i, scale;
3643
3644 this_rq->nr_load_updates++;
Ingo Molnardd41f592007-07-09 18:51:59 +02003645
Venkatesh Pallipadifdf3e952010-05-17 18:14:43 -07003646 /* Avoid repeated calls on same jiffy, when moving in and out of idle */
3647 if (curr_jiffies == this_rq->last_load_update_tick)
3648 return;
3649
3650 pending_updates = curr_jiffies - this_rq->last_load_update_tick;
3651 this_rq->last_load_update_tick = curr_jiffies;
3652
Ingo Molnardd41f592007-07-09 18:51:59 +02003653 /* Update our load: */
Venkatesh Pallipadifdf3e952010-05-17 18:14:43 -07003654 this_rq->cpu_load[0] = this_load; /* Fasttrack for idx 0 */
3655 for (i = 1, scale = 2; i < CPU_LOAD_IDX_MAX; i++, scale += scale) {
Ingo Molnardd41f592007-07-09 18:51:59 +02003656 unsigned long old_load, new_load;
3657
3658 /* scale is effectively 1 << i now, and >> i divides by scale */
3659
3660 old_load = this_rq->cpu_load[i];
Venkatesh Pallipadifdf3e952010-05-17 18:14:43 -07003661 old_load = decay_load_missed(old_load, pending_updates - 1, i);
Ingo Molnardd41f592007-07-09 18:51:59 +02003662 new_load = this_load;
Ingo Molnara25707f2007-10-15 17:00:03 +02003663 /*
3664 * Round up the averaging division if load is increasing. This
3665 * prevents us from getting stuck on 9 if the load is 10, for
3666 * example.
3667 */
3668 if (new_load > old_load)
Venkatesh Pallipadifdf3e952010-05-17 18:14:43 -07003669 new_load += scale - 1;
3670
3671 this_rq->cpu_load[i] = (old_load * (scale - 1) + new_load) >> i;
Ingo Molnardd41f592007-07-09 18:51:59 +02003672 }
Suresh Siddhada2b71e2010-08-23 13:42:51 -07003673
3674 sched_avg_update(this_rq);
Venkatesh Pallipadifdf3e952010-05-17 18:14:43 -07003675}
3676
3677static void update_cpu_load_active(struct rq *this_rq)
3678{
3679 update_cpu_load(this_rq);
Thomas Gleixnerdce48a82009-04-11 10:43:41 +02003680
Peter Zijlstra74f51872010-04-22 21:50:19 +02003681 calc_load_account_active(this_rq);
Ingo Molnar48f24c42006-07-03 00:25:40 -07003682}
3683
Ingo Molnardd41f592007-07-09 18:51:59 +02003684#ifdef CONFIG_SMP
3685
Ingo Molnar48f24c42006-07-03 00:25:40 -07003686/*
Peter Zijlstra38022902009-12-16 18:04:37 +01003687 * sched_exec - execve() is a valuable balancing opportunity, because at
3688 * this point the task has the smallest effective memory and cache footprint.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003689 */
Peter Zijlstra38022902009-12-16 18:04:37 +01003690void sched_exec(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003691{
Peter Zijlstra38022902009-12-16 18:04:37 +01003692 struct task_struct *p = current;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003693 unsigned long flags;
Peter Zijlstra0017d732010-03-24 18:34:10 +01003694 int dest_cpu;
Peter Zijlstra38022902009-12-16 18:04:37 +01003695
Peter Zijlstra8f42ced2011-04-05 17:23:53 +02003696 raw_spin_lock_irqsave(&p->pi_lock, flags);
Peter Zijlstra7608dec2011-04-05 17:23:46 +02003697 dest_cpu = p->sched_class->select_task_rq(p, SD_BALANCE_EXEC, 0);
Peter Zijlstra0017d732010-03-24 18:34:10 +01003698 if (dest_cpu == smp_processor_id())
3699 goto unlock;
Peter Zijlstra38022902009-12-16 18:04:37 +01003700
Peter Zijlstra8f42ced2011-04-05 17:23:53 +02003701 if (likely(cpu_active(dest_cpu))) {
Tejun Heo969c7922010-05-06 18:49:21 +02003702 struct migration_arg arg = { p, dest_cpu };
Ingo Molnar36c8b582006-07-03 00:25:41 -07003703
Peter Zijlstra8f42ced2011-04-05 17:23:53 +02003704 raw_spin_unlock_irqrestore(&p->pi_lock, flags);
3705 stop_one_cpu(task_cpu(p), migration_cpu_stop, &arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003706 return;
3707 }
Peter Zijlstra0017d732010-03-24 18:34:10 +01003708unlock:
Peter Zijlstra8f42ced2011-04-05 17:23:53 +02003709 raw_spin_unlock_irqrestore(&p->pi_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003710}
3711
Linus Torvalds1da177e2005-04-16 15:20:36 -07003712#endif
3713
Linus Torvalds1da177e2005-04-16 15:20:36 -07003714DEFINE_PER_CPU(struct kernel_stat, kstat);
3715
3716EXPORT_PER_CPU_SYMBOL(kstat);
3717
3718/*
Hidetoshi Setoc5f8d992009-03-31 16:56:03 +09003719 * Return any ns on the sched_clock that have not yet been accounted in
Frank Mayharf06febc2008-09-12 09:54:39 -07003720 * @p in case that task is currently running.
Hidetoshi Setoc5f8d992009-03-31 16:56:03 +09003721 *
3722 * Called with task_rq_lock() held on @rq.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003723 */
Hidetoshi Setoc5f8d992009-03-31 16:56:03 +09003724static u64 do_task_delta_exec(struct task_struct *p, struct rq *rq)
3725{
3726 u64 ns = 0;
3727
3728 if (task_current(rq, p)) {
3729 update_rq_clock(rq);
Venkatesh Pallipadi305e6832010-10-04 17:03:21 -07003730 ns = rq->clock_task - p->se.exec_start;
Hidetoshi Setoc5f8d992009-03-31 16:56:03 +09003731 if ((s64)ns < 0)
3732 ns = 0;
3733 }
3734
3735 return ns;
3736}
3737
Frank Mayharbb34d922008-09-12 09:54:39 -07003738unsigned long long task_delta_exec(struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003739{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003740 unsigned long flags;
Ingo Molnar41b86e92007-07-09 18:51:58 +02003741 struct rq *rq;
Frank Mayharbb34d922008-09-12 09:54:39 -07003742 u64 ns = 0;
Ingo Molnar48f24c42006-07-03 00:25:40 -07003743
Ingo Molnar41b86e92007-07-09 18:51:58 +02003744 rq = task_rq_lock(p, &flags);
Hidetoshi Setoc5f8d992009-03-31 16:56:03 +09003745 ns = do_task_delta_exec(p, rq);
Peter Zijlstra0122ec52011-04-05 17:23:51 +02003746 task_rq_unlock(rq, p, &flags);
Ingo Molnar15084872008-09-30 08:28:17 +02003747
Hidetoshi Setoc5f8d992009-03-31 16:56:03 +09003748 return ns;
3749}
Frank Mayharf06febc2008-09-12 09:54:39 -07003750
Hidetoshi Setoc5f8d992009-03-31 16:56:03 +09003751/*
3752 * Return accounted runtime for the task.
3753 * In case the task is currently running, return the runtime plus current's
3754 * pending runtime that have not been accounted yet.
3755 */
3756unsigned long long task_sched_runtime(struct task_struct *p)
3757{
3758 unsigned long flags;
3759 struct rq *rq;
3760 u64 ns = 0;
Ingo Molnar48f24c42006-07-03 00:25:40 -07003761
Hidetoshi Setoc5f8d992009-03-31 16:56:03 +09003762 rq = task_rq_lock(p, &flags);
3763 ns = p->se.sum_exec_runtime + do_task_delta_exec(p, rq);
Peter Zijlstra0122ec52011-04-05 17:23:51 +02003764 task_rq_unlock(rq, p, &flags);
Hidetoshi Setoc5f8d992009-03-31 16:56:03 +09003765
3766 return ns;
3767}
3768
3769/*
3770 * Return sum_exec_runtime for the thread group.
3771 * In case the task is currently running, return the sum plus current's
3772 * pending runtime that have not been accounted yet.
3773 *
3774 * Note that the thread group might have other running tasks as well,
3775 * so the return value not includes other pending runtime that other
3776 * running tasks might have.
3777 */
3778unsigned long long thread_group_sched_runtime(struct task_struct *p)
3779{
3780 struct task_cputime totals;
3781 unsigned long flags;
3782 struct rq *rq;
3783 u64 ns;
3784
3785 rq = task_rq_lock(p, &flags);
3786 thread_group_cputime(p, &totals);
3787 ns = totals.sum_exec_runtime + do_task_delta_exec(p, rq);
Peter Zijlstra0122ec52011-04-05 17:23:51 +02003788 task_rq_unlock(rq, p, &flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003789
3790 return ns;
3791}
3792
3793/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003794 * Account user cpu time to a process.
3795 * @p: the process that the cpu time gets accounted to
Linus Torvalds1da177e2005-04-16 15:20:36 -07003796 * @cputime: the cpu time spent in user space since the last update
Martin Schwidefsky457533a2008-12-31 15:11:37 +01003797 * @cputime_scaled: cputime scaled by cpu frequency
Linus Torvalds1da177e2005-04-16 15:20:36 -07003798 */
Martin Schwidefsky457533a2008-12-31 15:11:37 +01003799void account_user_time(struct task_struct *p, cputime_t cputime,
3800 cputime_t cputime_scaled)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003801{
3802 struct cpu_usage_stat *cpustat = &kstat_this_cpu.cpustat;
3803 cputime64_t tmp;
3804
Martin Schwidefsky457533a2008-12-31 15:11:37 +01003805 /* Add user time to process. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003806 p->utime = cputime_add(p->utime, cputime);
Martin Schwidefsky457533a2008-12-31 15:11:37 +01003807 p->utimescaled = cputime_add(p->utimescaled, cputime_scaled);
Frank Mayharf06febc2008-09-12 09:54:39 -07003808 account_group_user_time(p, cputime);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003809
3810 /* Add user time to cpustat. */
3811 tmp = cputime_to_cputime64(cputime);
3812 if (TASK_NICE(p) > 0)
3813 cpustat->nice = cputime64_add(cpustat->nice, tmp);
3814 else
3815 cpustat->user = cputime64_add(cpustat->user, tmp);
Bharata B Raoef12fef2009-03-31 10:02:22 +05303816
3817 cpuacct_update_stats(p, CPUACCT_STAT_USER, cputime);
Jonathan Lim49b5cf32008-07-25 01:48:40 -07003818 /* Account for user time used */
3819 acct_update_integrals(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003820}
3821
3822/*
Laurent Vivier94886b82007-10-15 17:00:19 +02003823 * Account guest cpu time to a process.
3824 * @p: the process that the cpu time gets accounted to
3825 * @cputime: the cpu time spent in virtual machine since the last update
Martin Schwidefsky457533a2008-12-31 15:11:37 +01003826 * @cputime_scaled: cputime scaled by cpu frequency
Laurent Vivier94886b82007-10-15 17:00:19 +02003827 */
Martin Schwidefsky457533a2008-12-31 15:11:37 +01003828static void account_guest_time(struct task_struct *p, cputime_t cputime,
3829 cputime_t cputime_scaled)
Laurent Vivier94886b82007-10-15 17:00:19 +02003830{
3831 cputime64_t tmp;
3832 struct cpu_usage_stat *cpustat = &kstat_this_cpu.cpustat;
3833
3834 tmp = cputime_to_cputime64(cputime);
3835
Martin Schwidefsky457533a2008-12-31 15:11:37 +01003836 /* Add guest time to process. */
Laurent Vivier94886b82007-10-15 17:00:19 +02003837 p->utime = cputime_add(p->utime, cputime);
Martin Schwidefsky457533a2008-12-31 15:11:37 +01003838 p->utimescaled = cputime_add(p->utimescaled, cputime_scaled);
Frank Mayharf06febc2008-09-12 09:54:39 -07003839 account_group_user_time(p, cputime);
Laurent Vivier94886b82007-10-15 17:00:19 +02003840 p->gtime = cputime_add(p->gtime, cputime);
3841
Martin Schwidefsky457533a2008-12-31 15:11:37 +01003842 /* Add guest time to cpustat. */
Ryota Ozakice0e7b22009-10-24 01:20:10 +09003843 if (TASK_NICE(p) > 0) {
3844 cpustat->nice = cputime64_add(cpustat->nice, tmp);
3845 cpustat->guest_nice = cputime64_add(cpustat->guest_nice, tmp);
3846 } else {
3847 cpustat->user = cputime64_add(cpustat->user, tmp);
3848 cpustat->guest = cputime64_add(cpustat->guest, tmp);
3849 }
Laurent Vivier94886b82007-10-15 17:00:19 +02003850}
3851
3852/*
Venkatesh Pallipadi70a89a62010-12-21 17:09:02 -08003853 * Account system cpu time to a process and desired cpustat field
3854 * @p: the process that the cpu time gets accounted to
3855 * @cputime: the cpu time spent in kernel space since the last update
3856 * @cputime_scaled: cputime scaled by cpu frequency
3857 * @target_cputime64: pointer to cpustat field that has to be updated
3858 */
3859static inline
3860void __account_system_time(struct task_struct *p, cputime_t cputime,
3861 cputime_t cputime_scaled, cputime64_t *target_cputime64)
3862{
3863 cputime64_t tmp = cputime_to_cputime64(cputime);
3864
3865 /* Add system time to process. */
3866 p->stime = cputime_add(p->stime, cputime);
3867 p->stimescaled = cputime_add(p->stimescaled, cputime_scaled);
3868 account_group_system_time(p, cputime);
3869
3870 /* Add system time to cpustat. */
3871 *target_cputime64 = cputime64_add(*target_cputime64, tmp);
3872 cpuacct_update_stats(p, CPUACCT_STAT_SYSTEM, cputime);
3873
3874 /* Account for system time used */
3875 acct_update_integrals(p);
3876}
3877
3878/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003879 * Account system cpu time to a process.
3880 * @p: the process that the cpu time gets accounted to
3881 * @hardirq_offset: the offset to subtract from hardirq_count()
3882 * @cputime: the cpu time spent in kernel space since the last update
Martin Schwidefsky457533a2008-12-31 15:11:37 +01003883 * @cputime_scaled: cputime scaled by cpu frequency
Linus Torvalds1da177e2005-04-16 15:20:36 -07003884 */
3885void account_system_time(struct task_struct *p, int hardirq_offset,
Martin Schwidefsky457533a2008-12-31 15:11:37 +01003886 cputime_t cputime, cputime_t cputime_scaled)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003887{
3888 struct cpu_usage_stat *cpustat = &kstat_this_cpu.cpustat;
Venkatesh Pallipadi70a89a62010-12-21 17:09:02 -08003889 cputime64_t *target_cputime64;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003890
Harvey Harrison983ed7a2008-04-24 18:17:55 -07003891 if ((p->flags & PF_VCPU) && (irq_count() - hardirq_offset == 0)) {
Martin Schwidefsky457533a2008-12-31 15:11:37 +01003892 account_guest_time(p, cputime, cputime_scaled);
Harvey Harrison983ed7a2008-04-24 18:17:55 -07003893 return;
3894 }
Laurent Vivier94886b82007-10-15 17:00:19 +02003895
Linus Torvalds1da177e2005-04-16 15:20:36 -07003896 if (hardirq_count() - hardirq_offset)
Venkatesh Pallipadi70a89a62010-12-21 17:09:02 -08003897 target_cputime64 = &cpustat->irq;
Venkatesh Pallipadi75e10562010-10-04 17:03:16 -07003898 else if (in_serving_softirq())
Venkatesh Pallipadi70a89a62010-12-21 17:09:02 -08003899 target_cputime64 = &cpustat->softirq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003900 else
Venkatesh Pallipadi70a89a62010-12-21 17:09:02 -08003901 target_cputime64 = &cpustat->system;
Martin Schwidefsky79741dd2008-12-31 15:11:38 +01003902
Venkatesh Pallipadi70a89a62010-12-21 17:09:02 -08003903 __account_system_time(p, cputime, cputime_scaled, target_cputime64);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003904}
3905
3906/*
3907 * Account for involuntary wait time.
Venkatesh Pallipadi544b4a12011-02-25 15:13:16 -08003908 * @cputime: the cpu time spent in involuntary wait
Linus Torvalds1da177e2005-04-16 15:20:36 -07003909 */
Martin Schwidefsky79741dd2008-12-31 15:11:38 +01003910void account_steal_time(cputime_t cputime)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003911{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003912 struct cpu_usage_stat *cpustat = &kstat_this_cpu.cpustat;
Martin Schwidefsky79741dd2008-12-31 15:11:38 +01003913 cputime64_t cputime64 = cputime_to_cputime64(cputime);
3914
3915 cpustat->steal = cputime64_add(cpustat->steal, cputime64);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003916}
3917
Christoph Lameter7835b982006-12-10 02:20:22 -08003918/*
Martin Schwidefsky79741dd2008-12-31 15:11:38 +01003919 * Account for idle time.
3920 * @cputime: the cpu time spent in idle wait
Linus Torvalds1da177e2005-04-16 15:20:36 -07003921 */
Martin Schwidefsky79741dd2008-12-31 15:11:38 +01003922void account_idle_time(cputime_t cputime)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003923{
3924 struct cpu_usage_stat *cpustat = &kstat_this_cpu.cpustat;
Martin Schwidefsky79741dd2008-12-31 15:11:38 +01003925 cputime64_t cputime64 = cputime_to_cputime64(cputime);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003926 struct rq *rq = this_rq();
3927
Martin Schwidefsky79741dd2008-12-31 15:11:38 +01003928 if (atomic_read(&rq->nr_iowait) > 0)
3929 cpustat->iowait = cputime64_add(cpustat->iowait, cputime64);
3930 else
3931 cpustat->idle = cputime64_add(cpustat->idle, cputime64);
Christoph Lameter7835b982006-12-10 02:20:22 -08003932}
3933
Glauber Costae6e66852011-07-11 15:28:17 -04003934static __always_inline bool steal_account_process_tick(void)
3935{
3936#ifdef CONFIG_PARAVIRT
3937 if (static_branch(&paravirt_steal_enabled)) {
3938 u64 steal, st = 0;
3939
3940 steal = paravirt_steal_clock(smp_processor_id());
3941 steal -= this_rq()->prev_steal_time;
3942
3943 st = steal_ticks(steal);
3944 this_rq()->prev_steal_time += st * TICK_NSEC;
3945
3946 account_steal_time(st);
3947 return st;
3948 }
3949#endif
3950 return false;
3951}
3952
Martin Schwidefsky79741dd2008-12-31 15:11:38 +01003953#ifndef CONFIG_VIRT_CPU_ACCOUNTING
3954
Venkatesh Pallipadiabb74ce2010-12-21 17:09:03 -08003955#ifdef CONFIG_IRQ_TIME_ACCOUNTING
3956/*
3957 * Account a tick to a process and cpustat
3958 * @p: the process that the cpu time gets accounted to
3959 * @user_tick: is the tick from userspace
3960 * @rq: the pointer to rq
3961 *
3962 * Tick demultiplexing follows the order
3963 * - pending hardirq update
3964 * - pending softirq update
3965 * - user_time
3966 * - idle_time
3967 * - system time
3968 * - check for guest_time
3969 * - else account as system_time
3970 *
3971 * Check for hardirq is done both for system and user time as there is
3972 * no timer going off while we are on hardirq and hence we may never get an
3973 * opportunity to update it solely in system time.
3974 * p->stime and friends are only updated on system time and not on irq
3975 * softirq as those do not count in task exec_runtime any more.
3976 */
3977static void irqtime_account_process_tick(struct task_struct *p, int user_tick,
3978 struct rq *rq)
3979{
3980 cputime_t one_jiffy_scaled = cputime_to_scaled(cputime_one_jiffy);
3981 cputime64_t tmp = cputime_to_cputime64(cputime_one_jiffy);
3982 struct cpu_usage_stat *cpustat = &kstat_this_cpu.cpustat;
3983
Glauber Costae6e66852011-07-11 15:28:17 -04003984 if (steal_account_process_tick())
3985 return;
3986
Venkatesh Pallipadiabb74ce2010-12-21 17:09:03 -08003987 if (irqtime_account_hi_update()) {
3988 cpustat->irq = cputime64_add(cpustat->irq, tmp);
3989 } else if (irqtime_account_si_update()) {
3990 cpustat->softirq = cputime64_add(cpustat->softirq, tmp);
Venkatesh Pallipadi414bee92010-12-21 17:09:04 -08003991 } else if (this_cpu_ksoftirqd() == p) {
3992 /*
3993 * ksoftirqd time do not get accounted in cpu_softirq_time.
3994 * So, we have to handle it separately here.
3995 * Also, p->stime needs to be updated for ksoftirqd.
3996 */
3997 __account_system_time(p, cputime_one_jiffy, one_jiffy_scaled,
3998 &cpustat->softirq);
Venkatesh Pallipadiabb74ce2010-12-21 17:09:03 -08003999 } else if (user_tick) {
4000 account_user_time(p, cputime_one_jiffy, one_jiffy_scaled);
4001 } else if (p == rq->idle) {
4002 account_idle_time(cputime_one_jiffy);
4003 } else if (p->flags & PF_VCPU) { /* System time or guest time */
4004 account_guest_time(p, cputime_one_jiffy, one_jiffy_scaled);
4005 } else {
4006 __account_system_time(p, cputime_one_jiffy, one_jiffy_scaled,
4007 &cpustat->system);
4008 }
4009}
4010
4011static void irqtime_account_idle_ticks(int ticks)
4012{
4013 int i;
4014 struct rq *rq = this_rq();
4015
4016 for (i = 0; i < ticks; i++)
4017 irqtime_account_process_tick(current, 0, rq);
4018}
Venkatesh Pallipadi544b4a12011-02-25 15:13:16 -08004019#else /* CONFIG_IRQ_TIME_ACCOUNTING */
Venkatesh Pallipadiabb74ce2010-12-21 17:09:03 -08004020static void irqtime_account_idle_ticks(int ticks) {}
4021static void irqtime_account_process_tick(struct task_struct *p, int user_tick,
4022 struct rq *rq) {}
Venkatesh Pallipadi544b4a12011-02-25 15:13:16 -08004023#endif /* CONFIG_IRQ_TIME_ACCOUNTING */
Martin Schwidefsky79741dd2008-12-31 15:11:38 +01004024
4025/*
4026 * Account a single tick of cpu time.
4027 * @p: the process that the cpu time gets accounted to
4028 * @user_tick: indicates if the tick is a user or a system tick
4029 */
4030void account_process_tick(struct task_struct *p, int user_tick)
4031{
Stanislaw Gruszkaa42548a2009-07-29 12:15:29 +02004032 cputime_t one_jiffy_scaled = cputime_to_scaled(cputime_one_jiffy);
Martin Schwidefsky79741dd2008-12-31 15:11:38 +01004033 struct rq *rq = this_rq();
4034
Venkatesh Pallipadiabb74ce2010-12-21 17:09:03 -08004035 if (sched_clock_irqtime) {
4036 irqtime_account_process_tick(p, user_tick, rq);
4037 return;
4038 }
4039
Glauber Costae6e66852011-07-11 15:28:17 -04004040 if (steal_account_process_tick())
4041 return;
4042
Martin Schwidefsky79741dd2008-12-31 15:11:38 +01004043 if (user_tick)
Stanislaw Gruszkaa42548a2009-07-29 12:15:29 +02004044 account_user_time(p, cputime_one_jiffy, one_jiffy_scaled);
Eric Dumazetf5f293a2009-04-29 14:44:49 +02004045 else if ((p != rq->idle) || (irq_count() != HARDIRQ_OFFSET))
Stanislaw Gruszkaa42548a2009-07-29 12:15:29 +02004046 account_system_time(p, HARDIRQ_OFFSET, cputime_one_jiffy,
Martin Schwidefsky79741dd2008-12-31 15:11:38 +01004047 one_jiffy_scaled);
4048 else
Stanislaw Gruszkaa42548a2009-07-29 12:15:29 +02004049 account_idle_time(cputime_one_jiffy);
Martin Schwidefsky79741dd2008-12-31 15:11:38 +01004050}
4051
4052/*
4053 * Account multiple ticks of steal time.
4054 * @p: the process from which the cpu time has been stolen
4055 * @ticks: number of stolen ticks
4056 */
4057void account_steal_ticks(unsigned long ticks)
4058{
4059 account_steal_time(jiffies_to_cputime(ticks));
4060}
4061
4062/*
4063 * Account multiple ticks of idle time.
4064 * @ticks: number of stolen ticks
4065 */
4066void account_idle_ticks(unsigned long ticks)
4067{
Venkatesh Pallipadiabb74ce2010-12-21 17:09:03 -08004068
4069 if (sched_clock_irqtime) {
4070 irqtime_account_idle_ticks(ticks);
4071 return;
4072 }
4073
Martin Schwidefsky79741dd2008-12-31 15:11:38 +01004074 account_idle_time(jiffies_to_cputime(ticks));
4075}
4076
4077#endif
4078
Christoph Lameter7835b982006-12-10 02:20:22 -08004079/*
Balbir Singh49048622008-09-05 18:12:23 +02004080 * Use precise platform statistics if available:
4081 */
4082#ifdef CONFIG_VIRT_CPU_ACCOUNTING
Hidetoshi Setod180c5b2009-11-26 14:48:30 +09004083void task_times(struct task_struct *p, cputime_t *ut, cputime_t *st)
Balbir Singh49048622008-09-05 18:12:23 +02004084{
Hidetoshi Setod99ca3b2009-12-02 17:26:47 +09004085 *ut = p->utime;
4086 *st = p->stime;
Balbir Singh49048622008-09-05 18:12:23 +02004087}
4088
Hidetoshi Seto0cf55e12009-12-02 17:28:07 +09004089void thread_group_times(struct task_struct *p, cputime_t *ut, cputime_t *st)
Balbir Singh49048622008-09-05 18:12:23 +02004090{
Hidetoshi Seto0cf55e12009-12-02 17:28:07 +09004091 struct task_cputime cputime;
4092
4093 thread_group_cputime(p, &cputime);
4094
4095 *ut = cputime.utime;
4096 *st = cputime.stime;
Balbir Singh49048622008-09-05 18:12:23 +02004097}
4098#else
Hidetoshi Seto761b1d22009-11-12 13:33:45 +09004099
4100#ifndef nsecs_to_cputime
Hidetoshi Setob7b20df2009-11-26 14:49:27 +09004101# define nsecs_to_cputime(__nsecs) nsecs_to_jiffies(__nsecs)
Hidetoshi Seto761b1d22009-11-12 13:33:45 +09004102#endif
4103
Hidetoshi Setod180c5b2009-11-26 14:48:30 +09004104void task_times(struct task_struct *p, cputime_t *ut, cputime_t *st)
Balbir Singh49048622008-09-05 18:12:23 +02004105{
Hidetoshi Setod99ca3b2009-12-02 17:26:47 +09004106 cputime_t rtime, utime = p->utime, total = cputime_add(utime, p->stime);
Balbir Singh49048622008-09-05 18:12:23 +02004107
4108 /*
4109 * Use CFS's precise accounting:
4110 */
Hidetoshi Setod180c5b2009-11-26 14:48:30 +09004111 rtime = nsecs_to_cputime(p->se.sum_exec_runtime);
Balbir Singh49048622008-09-05 18:12:23 +02004112
4113 if (total) {
Stanislaw Gruszkae75e8632010-09-14 16:35:14 +02004114 u64 temp = rtime;
Balbir Singh49048622008-09-05 18:12:23 +02004115
Stanislaw Gruszkae75e8632010-09-14 16:35:14 +02004116 temp *= utime;
Balbir Singh49048622008-09-05 18:12:23 +02004117 do_div(temp, total);
Hidetoshi Setod180c5b2009-11-26 14:48:30 +09004118 utime = (cputime_t)temp;
4119 } else
4120 utime = rtime;
Balbir Singh49048622008-09-05 18:12:23 +02004121
4122 /*
Hidetoshi Setod180c5b2009-11-26 14:48:30 +09004123 * Compare with previous values, to keep monotonicity:
Balbir Singh49048622008-09-05 18:12:23 +02004124 */
Hidetoshi Seto761b1d22009-11-12 13:33:45 +09004125 p->prev_utime = max(p->prev_utime, utime);
Hidetoshi Setod99ca3b2009-12-02 17:26:47 +09004126 p->prev_stime = max(p->prev_stime, cputime_sub(rtime, p->prev_utime));
Balbir Singh49048622008-09-05 18:12:23 +02004127
Hidetoshi Setod99ca3b2009-12-02 17:26:47 +09004128 *ut = p->prev_utime;
4129 *st = p->prev_stime;
Hidetoshi Setod180c5b2009-11-26 14:48:30 +09004130}
Balbir Singh49048622008-09-05 18:12:23 +02004131
Hidetoshi Seto0cf55e12009-12-02 17:28:07 +09004132/*
4133 * Must be called with siglock held.
4134 */
4135void thread_group_times(struct task_struct *p, cputime_t *ut, cputime_t *st)
4136{
4137 struct signal_struct *sig = p->signal;
4138 struct task_cputime cputime;
4139 cputime_t rtime, utime, total;
4140
4141 thread_group_cputime(p, &cputime);
4142
4143 total = cputime_add(cputime.utime, cputime.stime);
4144 rtime = nsecs_to_cputime(cputime.sum_exec_runtime);
4145
4146 if (total) {
Stanislaw Gruszkae75e8632010-09-14 16:35:14 +02004147 u64 temp = rtime;
Hidetoshi Seto0cf55e12009-12-02 17:28:07 +09004148
Stanislaw Gruszkae75e8632010-09-14 16:35:14 +02004149 temp *= cputime.utime;
Hidetoshi Seto0cf55e12009-12-02 17:28:07 +09004150 do_div(temp, total);
4151 utime = (cputime_t)temp;
4152 } else
4153 utime = rtime;
4154
4155 sig->prev_utime = max(sig->prev_utime, utime);
4156 sig->prev_stime = max(sig->prev_stime,
4157 cputime_sub(rtime, sig->prev_utime));
4158
4159 *ut = sig->prev_utime;
4160 *st = sig->prev_stime;
Balbir Singh49048622008-09-05 18:12:23 +02004161}
4162#endif
4163
Balbir Singh49048622008-09-05 18:12:23 +02004164/*
Christoph Lameter7835b982006-12-10 02:20:22 -08004165 * This function gets called by the timer code, with HZ frequency.
4166 * We call it with interrupts disabled.
Christoph Lameter7835b982006-12-10 02:20:22 -08004167 */
4168void scheduler_tick(void)
4169{
Christoph Lameter7835b982006-12-10 02:20:22 -08004170 int cpu = smp_processor_id();
4171 struct rq *rq = cpu_rq(cpu);
Ingo Molnardd41f592007-07-09 18:51:59 +02004172 struct task_struct *curr = rq->curr;
Peter Zijlstra3e51f332008-05-03 18:29:28 +02004173
4174 sched_clock_tick();
Christoph Lameter7835b982006-12-10 02:20:22 -08004175
Thomas Gleixner05fa7852009-11-17 14:28:38 +01004176 raw_spin_lock(&rq->lock);
Peter Zijlstra3e51f332008-05-03 18:29:28 +02004177 update_rq_clock(rq);
Venkatesh Pallipadifdf3e952010-05-17 18:14:43 -07004178 update_cpu_load_active(rq);
Peter Zijlstrafa85ae22008-01-25 21:08:29 +01004179 curr->sched_class->task_tick(rq, curr, 0);
Thomas Gleixner05fa7852009-11-17 14:28:38 +01004180 raw_spin_unlock(&rq->lock);
Ingo Molnardd41f592007-07-09 18:51:59 +02004181
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02004182 perf_event_task_tick();
Peter Zijlstrae220d2d2009-05-23 18:28:55 +02004183
Christoph Lametere418e1c2006-12-10 02:20:23 -08004184#ifdef CONFIG_SMP
Ingo Molnardd41f592007-07-09 18:51:59 +02004185 rq->idle_at_tick = idle_cpu(cpu);
4186 trigger_load_balance(rq, cpu);
Christoph Lametere418e1c2006-12-10 02:20:23 -08004187#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07004188}
4189
Lai Jiangshan132380a2009-04-02 14:18:25 +08004190notrace unsigned long get_parent_ip(unsigned long addr)
Steven Rostedt6cd8a4b2008-05-12 21:20:42 +02004191{
4192 if (in_lock_functions(addr)) {
4193 addr = CALLER_ADDR2;
4194 if (in_lock_functions(addr))
4195 addr = CALLER_ADDR3;
4196 }
4197 return addr;
4198}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004199
Steven Rostedt7e49fcc2009-01-22 19:01:40 -05004200#if defined(CONFIG_PREEMPT) && (defined(CONFIG_DEBUG_PREEMPT) || \
4201 defined(CONFIG_PREEMPT_TRACER))
4202
Srinivasa Ds43627582008-02-23 15:24:04 -08004203void __kprobes add_preempt_count(int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004204{
Steven Rostedt6cd8a4b2008-05-12 21:20:42 +02004205#ifdef CONFIG_DEBUG_PREEMPT
Linus Torvalds1da177e2005-04-16 15:20:36 -07004206 /*
4207 * Underflow?
4208 */
Ingo Molnar9a11b492006-07-03 00:24:33 -07004209 if (DEBUG_LOCKS_WARN_ON((preempt_count() < 0)))
4210 return;
Steven Rostedt6cd8a4b2008-05-12 21:20:42 +02004211#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07004212 preempt_count() += val;
Steven Rostedt6cd8a4b2008-05-12 21:20:42 +02004213#ifdef CONFIG_DEBUG_PREEMPT
Linus Torvalds1da177e2005-04-16 15:20:36 -07004214 /*
4215 * Spinlock count overflowing soon?
4216 */
Miguel Ojeda Sandonis33859f72006-12-10 02:20:38 -08004217 DEBUG_LOCKS_WARN_ON((preempt_count() & PREEMPT_MASK) >=
4218 PREEMPT_MASK - 10);
Steven Rostedt6cd8a4b2008-05-12 21:20:42 +02004219#endif
4220 if (preempt_count() == val)
4221 trace_preempt_off(CALLER_ADDR0, get_parent_ip(CALLER_ADDR1));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004222}
4223EXPORT_SYMBOL(add_preempt_count);
4224
Srinivasa Ds43627582008-02-23 15:24:04 -08004225void __kprobes sub_preempt_count(int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004226{
Steven Rostedt6cd8a4b2008-05-12 21:20:42 +02004227#ifdef CONFIG_DEBUG_PREEMPT
Linus Torvalds1da177e2005-04-16 15:20:36 -07004228 /*
4229 * Underflow?
4230 */
Ingo Molnar01e3eb82009-01-12 13:00:50 +01004231 if (DEBUG_LOCKS_WARN_ON(val > preempt_count()))
Ingo Molnar9a11b492006-07-03 00:24:33 -07004232 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004233 /*
4234 * Is the spinlock portion underflowing?
4235 */
Ingo Molnar9a11b492006-07-03 00:24:33 -07004236 if (DEBUG_LOCKS_WARN_ON((val < PREEMPT_MASK) &&
4237 !(preempt_count() & PREEMPT_MASK)))
4238 return;
Steven Rostedt6cd8a4b2008-05-12 21:20:42 +02004239#endif
Ingo Molnar9a11b492006-07-03 00:24:33 -07004240
Steven Rostedt6cd8a4b2008-05-12 21:20:42 +02004241 if (preempt_count() == val)
4242 trace_preempt_on(CALLER_ADDR0, get_parent_ip(CALLER_ADDR1));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004243 preempt_count() -= val;
4244}
4245EXPORT_SYMBOL(sub_preempt_count);
4246
4247#endif
4248
4249/*
Ingo Molnardd41f592007-07-09 18:51:59 +02004250 * Print scheduling while atomic bug:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004251 */
Ingo Molnardd41f592007-07-09 18:51:59 +02004252static noinline void __schedule_bug(struct task_struct *prev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004253{
Satyam Sharma838225b2007-10-24 18:23:50 +02004254 struct pt_regs *regs = get_irq_regs();
4255
Peter Zijlstra3df0fc52009-12-20 14:23:57 +01004256 printk(KERN_ERR "BUG: scheduling while atomic: %s/%d/0x%08x\n",
4257 prev->comm, prev->pid, preempt_count());
Satyam Sharma838225b2007-10-24 18:23:50 +02004258
Ingo Molnardd41f592007-07-09 18:51:59 +02004259 debug_show_held_locks(prev);
Arjan van de Vene21f5b12008-05-23 09:05:58 -07004260 print_modules();
Ingo Molnardd41f592007-07-09 18:51:59 +02004261 if (irqs_disabled())
4262 print_irqtrace_events(prev);
Satyam Sharma838225b2007-10-24 18:23:50 +02004263
4264 if (regs)
4265 show_regs(regs);
4266 else
4267 dump_stack();
Ingo Molnardd41f592007-07-09 18:51:59 +02004268}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004269
Ingo Molnardd41f592007-07-09 18:51:59 +02004270/*
4271 * Various schedule()-time debugging checks and statistics:
4272 */
4273static inline void schedule_debug(struct task_struct *prev)
4274{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004275 /*
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01004276 * Test if we are atomic. Since do_exit() needs to call into
Linus Torvalds1da177e2005-04-16 15:20:36 -07004277 * schedule() atomically, we ignore that path for now.
4278 * Otherwise, whine if we are scheduling when we should not be.
4279 */
Roel Kluin3f33a7c2008-05-13 23:44:11 +02004280 if (unlikely(in_atomic_preempt_off() && !prev->exit_state))
Ingo Molnardd41f592007-07-09 18:51:59 +02004281 __schedule_bug(prev);
4282
Linus Torvalds1da177e2005-04-16 15:20:36 -07004283 profile_hit(SCHED_PROFILING, __builtin_return_address(0));
4284
Ingo Molnar2d723762007-10-15 17:00:12 +02004285 schedstat_inc(this_rq(), sched_count);
Ingo Molnardd41f592007-07-09 18:51:59 +02004286}
4287
Peter Zijlstra6cecd082009-11-30 13:00:37 +01004288static void put_prev_task(struct rq *rq, struct task_struct *prev)
Mike Galbraithdf1c99d2009-03-10 19:08:11 +01004289{
Mike Galbraith61eadef2011-04-29 08:36:50 +02004290 if (prev->on_rq || rq->skip_clock_update < 0)
Mike Galbraitha64692a2010-03-11 17:16:20 +01004291 update_rq_clock(rq);
Peter Zijlstra6cecd082009-11-30 13:00:37 +01004292 prev->sched_class->put_prev_task(rq, prev);
Mike Galbraithdf1c99d2009-03-10 19:08:11 +01004293}
4294
Ingo Molnardd41f592007-07-09 18:51:59 +02004295/*
4296 * Pick up the highest-prio task:
4297 */
4298static inline struct task_struct *
Wang Chenb67802e2009-03-02 13:55:26 +08004299pick_next_task(struct rq *rq)
Ingo Molnardd41f592007-07-09 18:51:59 +02004300{
Ingo Molnar5522d5d2007-10-15 17:00:12 +02004301 const struct sched_class *class;
Ingo Molnardd41f592007-07-09 18:51:59 +02004302 struct task_struct *p;
4303
4304 /*
4305 * Optimization: we know that if all tasks are in
4306 * the fair class we can call that function directly:
4307 */
Paul Turner953bfcd2011-07-21 09:43:27 -07004308 if (likely(rq->nr_running == rq->cfs.h_nr_running)) {
Ingo Molnarfb8d4722007-08-09 11:16:48 +02004309 p = fair_sched_class.pick_next_task(rq);
Ingo Molnardd41f592007-07-09 18:51:59 +02004310 if (likely(p))
4311 return p;
4312 }
4313
Peter Zijlstra34f971f2010-09-22 13:53:15 +02004314 for_each_class(class) {
Ingo Molnarfb8d4722007-08-09 11:16:48 +02004315 p = class->pick_next_task(rq);
Ingo Molnardd41f592007-07-09 18:51:59 +02004316 if (p)
4317 return p;
Ingo Molnardd41f592007-07-09 18:51:59 +02004318 }
Peter Zijlstra34f971f2010-09-22 13:53:15 +02004319
4320 BUG(); /* the idle class will always have a runnable task */
Ingo Molnardd41f592007-07-09 18:51:59 +02004321}
4322
4323/*
4324 * schedule() is the main scheduler function.
4325 */
Peter Zijlstraff743342009-03-13 12:21:26 +01004326asmlinkage void __sched schedule(void)
Ingo Molnardd41f592007-07-09 18:51:59 +02004327{
4328 struct task_struct *prev, *next;
Harvey Harrison67ca7bd2008-02-15 09:56:36 -08004329 unsigned long *switch_count;
Ingo Molnardd41f592007-07-09 18:51:59 +02004330 struct rq *rq;
Peter Zijlstra31656512008-07-18 18:01:23 +02004331 int cpu;
Ingo Molnardd41f592007-07-09 18:51:59 +02004332
Peter Zijlstraff743342009-03-13 12:21:26 +01004333need_resched:
4334 preempt_disable();
Ingo Molnardd41f592007-07-09 18:51:59 +02004335 cpu = smp_processor_id();
4336 rq = cpu_rq(cpu);
Paul E. McKenney25502a62010-04-01 17:37:01 -07004337 rcu_note_context_switch(cpu);
Ingo Molnardd41f592007-07-09 18:51:59 +02004338 prev = rq->curr;
Ingo Molnardd41f592007-07-09 18:51:59 +02004339
Ingo Molnardd41f592007-07-09 18:51:59 +02004340 schedule_debug(prev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004341
Peter Zijlstra31656512008-07-18 18:01:23 +02004342 if (sched_feat(HRTICK))
Mike Galbraithf333fdc2008-05-12 21:20:55 +02004343 hrtick_clear(rq);
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01004344
Thomas Gleixner05fa7852009-11-17 14:28:38 +01004345 raw_spin_lock_irq(&rq->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004346
Oleg Nesterov246d86b2010-05-19 14:57:11 +02004347 switch_count = &prev->nivcsw;
Ingo Molnardd41f592007-07-09 18:51:59 +02004348 if (prev->state && !(preempt_count() & PREEMPT_ACTIVE)) {
Tejun Heo21aa9af2010-06-08 21:40:37 +02004349 if (unlikely(signal_pending_state(prev->state, prev))) {
Ingo Molnardd41f592007-07-09 18:51:59 +02004350 prev->state = TASK_RUNNING;
Tejun Heo21aa9af2010-06-08 21:40:37 +02004351 } else {
Peter Zijlstra2acca552011-04-05 17:23:50 +02004352 deactivate_task(rq, prev, DEQUEUE_SLEEP);
4353 prev->on_rq = 0;
4354
Tejun Heo21aa9af2010-06-08 21:40:37 +02004355 /*
Peter Zijlstra2acca552011-04-05 17:23:50 +02004356 * If a worker went to sleep, notify and ask workqueue
4357 * whether it wants to wake up a task to maintain
4358 * concurrency.
Tejun Heo21aa9af2010-06-08 21:40:37 +02004359 */
4360 if (prev->flags & PF_WQ_WORKER) {
4361 struct task_struct *to_wakeup;
4362
4363 to_wakeup = wq_worker_sleeping(prev, cpu);
4364 if (to_wakeup)
4365 try_to_wake_up_local(to_wakeup);
4366 }
Peter Zijlstrafd2f4412011-04-05 17:23:44 +02004367
Linus Torvalds6631e632011-04-13 08:08:20 -07004368 /*
Peter Zijlstra2acca552011-04-05 17:23:50 +02004369 * If we are going to sleep and we have plugged IO
4370 * queued, make sure to submit it to avoid deadlocks.
Linus Torvalds6631e632011-04-13 08:08:20 -07004371 */
4372 if (blk_needs_flush_plug(prev)) {
4373 raw_spin_unlock(&rq->lock);
Jens Axboea237c1c2011-04-16 13:27:55 +02004374 blk_schedule_flush_plug(prev);
Linus Torvalds6631e632011-04-13 08:08:20 -07004375 raw_spin_lock(&rq->lock);
4376 }
Tejun Heo21aa9af2010-06-08 21:40:37 +02004377 }
Ingo Molnardd41f592007-07-09 18:51:59 +02004378 switch_count = &prev->nvcsw;
4379 }
4380
Gregory Haskins3f029d32009-07-29 11:08:47 -04004381 pre_schedule(rq, prev);
Steven Rostedtf65eda42008-01-25 21:08:07 +01004382
Ingo Molnardd41f592007-07-09 18:51:59 +02004383 if (unlikely(!rq->nr_running))
4384 idle_balance(cpu, rq);
4385
Mike Galbraithdf1c99d2009-03-10 19:08:11 +01004386 put_prev_task(rq, prev);
Wang Chenb67802e2009-03-02 13:55:26 +08004387 next = pick_next_task(rq);
Mike Galbraithf26f9af2010-12-08 11:05:42 +01004388 clear_tsk_need_resched(prev);
4389 rq->skip_clock_update = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004390
Linus Torvalds1da177e2005-04-16 15:20:36 -07004391 if (likely(prev != next)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004392 rq->nr_switches++;
4393 rq->curr = next;
4394 ++*switch_count;
4395
Ingo Molnardd41f592007-07-09 18:51:59 +02004396 context_switch(rq, prev, next); /* unlocks the rq */
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01004397 /*
Oleg Nesterov246d86b2010-05-19 14:57:11 +02004398 * The context switch have flipped the stack from under us
4399 * and restored the local variables which were saved when
4400 * this task called schedule() in the past. prev == current
4401 * is still correct, but it can be moved to another cpu/rq.
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01004402 */
4403 cpu = smp_processor_id();
4404 rq = cpu_rq(cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004405 } else
Thomas Gleixner05fa7852009-11-17 14:28:38 +01004406 raw_spin_unlock_irq(&rq->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004407
Gregory Haskins3f029d32009-07-29 11:08:47 -04004408 post_schedule(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004409
Linus Torvalds1da177e2005-04-16 15:20:36 -07004410 preempt_enable_no_resched();
Peter Zijlstraff743342009-03-13 12:21:26 +01004411 if (need_resched())
Linus Torvalds1da177e2005-04-16 15:20:36 -07004412 goto need_resched;
4413}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004414EXPORT_SYMBOL(schedule);
4415
Frederic Weisbeckerc08f7822009-12-02 20:49:17 +01004416#ifdef CONFIG_MUTEX_SPIN_ON_OWNER
Peter Zijlstrac6eb3dd2011-04-05 17:23:41 +02004417
4418static inline bool owner_running(struct mutex *lock, struct task_struct *owner)
4419{
Peter Zijlstrac6eb3dd2011-04-05 17:23:41 +02004420 if (lock->owner != owner)
Thomas Gleixner307bf982011-06-10 15:08:55 +02004421 return false;
Peter Zijlstrac6eb3dd2011-04-05 17:23:41 +02004422
4423 /*
4424 * Ensure we emit the owner->on_cpu, dereference _after_ checking
4425 * lock->owner still matches owner, if that fails, owner might
4426 * point to free()d memory, if it still matches, the rcu_read_lock()
4427 * ensures the memory stays valid.
4428 */
4429 barrier();
4430
Thomas Gleixner307bf982011-06-10 15:08:55 +02004431 return owner->on_cpu;
Peter Zijlstrac6eb3dd2011-04-05 17:23:41 +02004432}
4433
Peter Zijlstra0d66bf62009-01-12 14:01:47 +01004434/*
4435 * Look out! "owner" is an entirely speculative pointer
4436 * access and not reliable.
4437 */
Peter Zijlstrac6eb3dd2011-04-05 17:23:41 +02004438int mutex_spin_on_owner(struct mutex *lock, struct task_struct *owner)
Peter Zijlstra0d66bf62009-01-12 14:01:47 +01004439{
Peter Zijlstra0d66bf62009-01-12 14:01:47 +01004440 if (!sched_feat(OWNER_SPIN))
4441 return 0;
4442
Thomas Gleixner307bf982011-06-10 15:08:55 +02004443 rcu_read_lock();
Peter Zijlstrac6eb3dd2011-04-05 17:23:41 +02004444 while (owner_running(lock, owner)) {
4445 if (need_resched())
Thomas Gleixner307bf982011-06-10 15:08:55 +02004446 break;
Peter Zijlstra0d66bf62009-01-12 14:01:47 +01004447
Gerald Schaefer335d7af2010-11-22 15:47:36 +01004448 arch_mutex_cpu_relax();
Peter Zijlstra0d66bf62009-01-12 14:01:47 +01004449 }
Thomas Gleixner307bf982011-06-10 15:08:55 +02004450 rcu_read_unlock();
Benjamin Herrenschmidt4b402212010-04-16 23:20:00 +02004451
Peter Zijlstrac6eb3dd2011-04-05 17:23:41 +02004452 /*
Thomas Gleixner307bf982011-06-10 15:08:55 +02004453 * We break out the loop above on need_resched() and when the
4454 * owner changed, which is a sign for heavy contention. Return
4455 * success only when lock->owner is NULL.
Peter Zijlstrac6eb3dd2011-04-05 17:23:41 +02004456 */
Thomas Gleixner307bf982011-06-10 15:08:55 +02004457 return lock->owner == NULL;
Peter Zijlstra0d66bf62009-01-12 14:01:47 +01004458}
4459#endif
4460
Linus Torvalds1da177e2005-04-16 15:20:36 -07004461#ifdef CONFIG_PREEMPT
4462/*
Andreas Mohr2ed6e342006-07-10 04:43:52 -07004463 * this is the entry point to schedule() from in-kernel preemption
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01004464 * off of preempt_enable. Kernel preemptions off return from interrupt
Linus Torvalds1da177e2005-04-16 15:20:36 -07004465 * occur there and call schedule directly.
4466 */
Steven Rostedtd1f74e22010-06-02 21:52:29 -04004467asmlinkage void __sched notrace preempt_schedule(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004468{
4469 struct thread_info *ti = current_thread_info();
Ingo Molnar6478d882008-01-25 21:08:33 +01004470
Linus Torvalds1da177e2005-04-16 15:20:36 -07004471 /*
4472 * If there is a non-zero preempt_count or interrupts are disabled,
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01004473 * we do not want to preempt the current task. Just return..
Linus Torvalds1da177e2005-04-16 15:20:36 -07004474 */
Nick Pigginbeed33a2006-10-11 01:21:52 -07004475 if (likely(ti->preempt_count || irqs_disabled()))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004476 return;
4477
Andi Kleen3a5c3592007-10-15 17:00:14 +02004478 do {
Steven Rostedtd1f74e22010-06-02 21:52:29 -04004479 add_preempt_count_notrace(PREEMPT_ACTIVE);
Andi Kleen3a5c3592007-10-15 17:00:14 +02004480 schedule();
Steven Rostedtd1f74e22010-06-02 21:52:29 -04004481 sub_preempt_count_notrace(PREEMPT_ACTIVE);
Andi Kleen3a5c3592007-10-15 17:00:14 +02004482
4483 /*
4484 * Check again in case we missed a preemption opportunity
4485 * between schedule and now.
4486 */
4487 barrier();
Lai Jiangshan5ed0cec2009-03-06 19:40:20 +08004488 } while (need_resched());
Linus Torvalds1da177e2005-04-16 15:20:36 -07004489}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004490EXPORT_SYMBOL(preempt_schedule);
4491
4492/*
Andreas Mohr2ed6e342006-07-10 04:43:52 -07004493 * this is the entry point to schedule() from kernel preemption
Linus Torvalds1da177e2005-04-16 15:20:36 -07004494 * off of irq context.
4495 * Note, that this is called and return with irqs disabled. This will
4496 * protect us against recursive calling from irq.
4497 */
4498asmlinkage void __sched preempt_schedule_irq(void)
4499{
4500 struct thread_info *ti = current_thread_info();
Ingo Molnar6478d882008-01-25 21:08:33 +01004501
Andreas Mohr2ed6e342006-07-10 04:43:52 -07004502 /* Catch callers which need to be fixed */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004503 BUG_ON(ti->preempt_count || !irqs_disabled());
4504
Andi Kleen3a5c3592007-10-15 17:00:14 +02004505 do {
4506 add_preempt_count(PREEMPT_ACTIVE);
Andi Kleen3a5c3592007-10-15 17:00:14 +02004507 local_irq_enable();
4508 schedule();
4509 local_irq_disable();
Andi Kleen3a5c3592007-10-15 17:00:14 +02004510 sub_preempt_count(PREEMPT_ACTIVE);
4511
4512 /*
4513 * Check again in case we missed a preemption opportunity
4514 * between schedule and now.
4515 */
4516 barrier();
Lai Jiangshan5ed0cec2009-03-06 19:40:20 +08004517 } while (need_resched());
Linus Torvalds1da177e2005-04-16 15:20:36 -07004518}
4519
4520#endif /* CONFIG_PREEMPT */
4521
Peter Zijlstra63859d42009-09-15 19:14:42 +02004522int default_wake_function(wait_queue_t *curr, unsigned mode, int wake_flags,
Ingo Molnar95cdf3b2005-09-10 00:26:11 -07004523 void *key)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004524{
Peter Zijlstra63859d42009-09-15 19:14:42 +02004525 return try_to_wake_up(curr->private, mode, wake_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004526}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004527EXPORT_SYMBOL(default_wake_function);
4528
4529/*
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01004530 * The core wakeup function. Non-exclusive wakeups (nr_exclusive == 0) just
4531 * wake everything up. If it's an exclusive wakeup (nr_exclusive == small +ve
Linus Torvalds1da177e2005-04-16 15:20:36 -07004532 * number) then we wake all the non-exclusive tasks and one exclusive task.
4533 *
4534 * There are circumstances in which we can try to wake a task which has already
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01004535 * started to run but is not in state TASK_RUNNING. try_to_wake_up() returns
Linus Torvalds1da177e2005-04-16 15:20:36 -07004536 * zero in this (rare) case, and we handle it by continuing to scan the queue.
4537 */
Johannes Weiner78ddb082009-04-14 16:53:05 +02004538static void __wake_up_common(wait_queue_head_t *q, unsigned int mode,
Peter Zijlstra63859d42009-09-15 19:14:42 +02004539 int nr_exclusive, int wake_flags, void *key)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004540{
Matthias Kaehlcke2e458742007-10-15 17:00:02 +02004541 wait_queue_t *curr, *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004542
Matthias Kaehlcke2e458742007-10-15 17:00:02 +02004543 list_for_each_entry_safe(curr, next, &q->task_list, task_list) {
Ingo Molnar48f24c42006-07-03 00:25:40 -07004544 unsigned flags = curr->flags;
4545
Peter Zijlstra63859d42009-09-15 19:14:42 +02004546 if (curr->func(curr, mode, wake_flags, key) &&
Ingo Molnar48f24c42006-07-03 00:25:40 -07004547 (flags & WQ_FLAG_EXCLUSIVE) && !--nr_exclusive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004548 break;
4549 }
4550}
4551
4552/**
4553 * __wake_up - wake up threads blocked on a waitqueue.
4554 * @q: the waitqueue
4555 * @mode: which threads
4556 * @nr_exclusive: how many wake-one or wake-many threads to wake up
Martin Waitz67be2dd2005-05-01 08:59:26 -07004557 * @key: is directly passed to the wakeup function
David Howells50fa6102009-04-28 15:01:38 +01004558 *
4559 * It may be assumed that this function implies a write memory barrier before
4560 * changing the task state if and only if any tasks are woken up.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004561 */
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -08004562void __wake_up(wait_queue_head_t *q, unsigned int mode,
Ingo Molnar95cdf3b2005-09-10 00:26:11 -07004563 int nr_exclusive, void *key)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004564{
4565 unsigned long flags;
4566
4567 spin_lock_irqsave(&q->lock, flags);
4568 __wake_up_common(q, mode, nr_exclusive, 0, key);
4569 spin_unlock_irqrestore(&q->lock, flags);
4570}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004571EXPORT_SYMBOL(__wake_up);
4572
4573/*
4574 * Same as __wake_up but called with the spinlock in wait_queue_head_t held.
4575 */
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -08004576void __wake_up_locked(wait_queue_head_t *q, unsigned int mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004577{
4578 __wake_up_common(q, mode, 1, 0, NULL);
4579}
Michal Nazarewicz22c43c82010-05-05 12:53:11 +02004580EXPORT_SYMBOL_GPL(__wake_up_locked);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004581
Davide Libenzi4ede8162009-03-31 15:24:20 -07004582void __wake_up_locked_key(wait_queue_head_t *q, unsigned int mode, void *key)
4583{
4584 __wake_up_common(q, mode, 1, 0, key);
4585}
Trond Myklebustbf294b42011-02-21 11:05:41 -08004586EXPORT_SYMBOL_GPL(__wake_up_locked_key);
Davide Libenzi4ede8162009-03-31 15:24:20 -07004587
Linus Torvalds1da177e2005-04-16 15:20:36 -07004588/**
Davide Libenzi4ede8162009-03-31 15:24:20 -07004589 * __wake_up_sync_key - wake up threads blocked on a waitqueue.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004590 * @q: the waitqueue
4591 * @mode: which threads
4592 * @nr_exclusive: how many wake-one or wake-many threads to wake up
Davide Libenzi4ede8162009-03-31 15:24:20 -07004593 * @key: opaque value to be passed to wakeup targets
Linus Torvalds1da177e2005-04-16 15:20:36 -07004594 *
4595 * The sync wakeup differs that the waker knows that it will schedule
4596 * away soon, so while the target thread will be woken up, it will not
4597 * be migrated to another CPU - ie. the two threads are 'synchronized'
4598 * with each other. This can prevent needless bouncing between CPUs.
4599 *
4600 * On UP it can prevent extra preemption.
David Howells50fa6102009-04-28 15:01:38 +01004601 *
4602 * It may be assumed that this function implies a write memory barrier before
4603 * changing the task state if and only if any tasks are woken up.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004604 */
Davide Libenzi4ede8162009-03-31 15:24:20 -07004605void __wake_up_sync_key(wait_queue_head_t *q, unsigned int mode,
4606 int nr_exclusive, void *key)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004607{
4608 unsigned long flags;
Peter Zijlstra7d478722009-09-14 19:55:44 +02004609 int wake_flags = WF_SYNC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004610
4611 if (unlikely(!q))
4612 return;
4613
4614 if (unlikely(!nr_exclusive))
Peter Zijlstra7d478722009-09-14 19:55:44 +02004615 wake_flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004616
4617 spin_lock_irqsave(&q->lock, flags);
Peter Zijlstra7d478722009-09-14 19:55:44 +02004618 __wake_up_common(q, mode, nr_exclusive, wake_flags, key);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004619 spin_unlock_irqrestore(&q->lock, flags);
4620}
Davide Libenzi4ede8162009-03-31 15:24:20 -07004621EXPORT_SYMBOL_GPL(__wake_up_sync_key);
4622
4623/*
4624 * __wake_up_sync - see __wake_up_sync_key()
4625 */
4626void __wake_up_sync(wait_queue_head_t *q, unsigned int mode, int nr_exclusive)
4627{
4628 __wake_up_sync_key(q, mode, nr_exclusive, NULL);
4629}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004630EXPORT_SYMBOL_GPL(__wake_up_sync); /* For internal use only */
4631
Kevin Diggs65eb3dc2008-08-26 10:26:54 +02004632/**
4633 * complete: - signals a single thread waiting on this completion
4634 * @x: holds the state of this particular completion
4635 *
4636 * This will wake up a single thread waiting on this completion. Threads will be
4637 * awakened in the same order in which they were queued.
4638 *
4639 * See also complete_all(), wait_for_completion() and related routines.
David Howells50fa6102009-04-28 15:01:38 +01004640 *
4641 * It may be assumed that this function implies a write memory barrier before
4642 * changing the task state if and only if any tasks are woken up.
Kevin Diggs65eb3dc2008-08-26 10:26:54 +02004643 */
Ingo Molnarb15136e2007-10-24 18:23:48 +02004644void complete(struct completion *x)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004645{
4646 unsigned long flags;
4647
4648 spin_lock_irqsave(&x->wait.lock, flags);
4649 x->done++;
Matthew Wilcoxd9514f62007-12-06 11:07:07 -05004650 __wake_up_common(&x->wait, TASK_NORMAL, 1, 0, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004651 spin_unlock_irqrestore(&x->wait.lock, flags);
4652}
4653EXPORT_SYMBOL(complete);
4654
Kevin Diggs65eb3dc2008-08-26 10:26:54 +02004655/**
4656 * complete_all: - signals all threads waiting on this completion
4657 * @x: holds the state of this particular completion
4658 *
4659 * This will wake up all threads waiting on this particular completion event.
David Howells50fa6102009-04-28 15:01:38 +01004660 *
4661 * It may be assumed that this function implies a write memory barrier before
4662 * changing the task state if and only if any tasks are woken up.
Kevin Diggs65eb3dc2008-08-26 10:26:54 +02004663 */
Ingo Molnarb15136e2007-10-24 18:23:48 +02004664void complete_all(struct completion *x)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004665{
4666 unsigned long flags;
4667
4668 spin_lock_irqsave(&x->wait.lock, flags);
4669 x->done += UINT_MAX/2;
Matthew Wilcoxd9514f62007-12-06 11:07:07 -05004670 __wake_up_common(&x->wait, TASK_NORMAL, 0, 0, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004671 spin_unlock_irqrestore(&x->wait.lock, flags);
4672}
4673EXPORT_SYMBOL(complete_all);
4674
Andi Kleen8cbbe862007-10-15 17:00:14 +02004675static inline long __sched
4676do_wait_for_common(struct completion *x, long timeout, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004677{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004678 if (!x->done) {
4679 DECLARE_WAITQUEUE(wait, current);
4680
Changli Gaoa93d2f12010-05-07 14:33:26 +08004681 __add_wait_queue_tail_exclusive(&x->wait, &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004682 do {
Oleg Nesterov94d3d822008-08-20 16:54:41 -07004683 if (signal_pending_state(state, current)) {
Oleg Nesterovea71a542008-06-20 18:32:20 +04004684 timeout = -ERESTARTSYS;
4685 break;
Andi Kleen8cbbe862007-10-15 17:00:14 +02004686 }
4687 __set_current_state(state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004688 spin_unlock_irq(&x->wait.lock);
Andi Kleen8cbbe862007-10-15 17:00:14 +02004689 timeout = schedule_timeout(timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004690 spin_lock_irq(&x->wait.lock);
Oleg Nesterovea71a542008-06-20 18:32:20 +04004691 } while (!x->done && timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004692 __remove_wait_queue(&x->wait, &wait);
Oleg Nesterovea71a542008-06-20 18:32:20 +04004693 if (!x->done)
4694 return timeout;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004695 }
4696 x->done--;
Oleg Nesterovea71a542008-06-20 18:32:20 +04004697 return timeout ?: 1;
Andi Kleen8cbbe862007-10-15 17:00:14 +02004698}
4699
4700static long __sched
4701wait_for_common(struct completion *x, long timeout, int state)
4702{
4703 might_sleep();
4704
4705 spin_lock_irq(&x->wait.lock);
4706 timeout = do_wait_for_common(x, timeout, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004707 spin_unlock_irq(&x->wait.lock);
Andi Kleen8cbbe862007-10-15 17:00:14 +02004708 return timeout;
4709}
4710
Kevin Diggs65eb3dc2008-08-26 10:26:54 +02004711/**
4712 * wait_for_completion: - waits for completion of a task
4713 * @x: holds the state of this particular completion
4714 *
4715 * This waits to be signaled for completion of a specific task. It is NOT
4716 * interruptible and there is no timeout.
4717 *
4718 * See also similar routines (i.e. wait_for_completion_timeout()) with timeout
4719 * and interrupt capability. Also see complete().
4720 */
Ingo Molnarb15136e2007-10-24 18:23:48 +02004721void __sched wait_for_completion(struct completion *x)
Andi Kleen8cbbe862007-10-15 17:00:14 +02004722{
4723 wait_for_common(x, MAX_SCHEDULE_TIMEOUT, TASK_UNINTERRUPTIBLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004724}
4725EXPORT_SYMBOL(wait_for_completion);
4726
Kevin Diggs65eb3dc2008-08-26 10:26:54 +02004727/**
4728 * wait_for_completion_timeout: - waits for completion of a task (w/timeout)
4729 * @x: holds the state of this particular completion
4730 * @timeout: timeout value in jiffies
4731 *
4732 * This waits for either a completion of a specific task to be signaled or for a
4733 * specified timeout to expire. The timeout is in jiffies. It is not
4734 * interruptible.
4735 */
Ingo Molnarb15136e2007-10-24 18:23:48 +02004736unsigned long __sched
Linus Torvalds1da177e2005-04-16 15:20:36 -07004737wait_for_completion_timeout(struct completion *x, unsigned long timeout)
4738{
Andi Kleen8cbbe862007-10-15 17:00:14 +02004739 return wait_for_common(x, timeout, TASK_UNINTERRUPTIBLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004740}
4741EXPORT_SYMBOL(wait_for_completion_timeout);
4742
Kevin Diggs65eb3dc2008-08-26 10:26:54 +02004743/**
4744 * wait_for_completion_interruptible: - waits for completion of a task (w/intr)
4745 * @x: holds the state of this particular completion
4746 *
4747 * This waits for completion of a specific task to be signaled. It is
4748 * interruptible.
4749 */
Andi Kleen8cbbe862007-10-15 17:00:14 +02004750int __sched wait_for_completion_interruptible(struct completion *x)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004751{
Andi Kleen51e97992007-10-18 21:32:55 +02004752 long t = wait_for_common(x, MAX_SCHEDULE_TIMEOUT, TASK_INTERRUPTIBLE);
4753 if (t == -ERESTARTSYS)
4754 return t;
4755 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004756}
4757EXPORT_SYMBOL(wait_for_completion_interruptible);
4758
Kevin Diggs65eb3dc2008-08-26 10:26:54 +02004759/**
4760 * wait_for_completion_interruptible_timeout: - waits for completion (w/(to,intr))
4761 * @x: holds the state of this particular completion
4762 * @timeout: timeout value in jiffies
4763 *
4764 * This waits for either a completion of a specific task to be signaled or for a
4765 * specified timeout to expire. It is interruptible. The timeout is in jiffies.
4766 */
NeilBrown6bf41232011-01-05 12:50:16 +11004767long __sched
Linus Torvalds1da177e2005-04-16 15:20:36 -07004768wait_for_completion_interruptible_timeout(struct completion *x,
4769 unsigned long timeout)
4770{
Andi Kleen8cbbe862007-10-15 17:00:14 +02004771 return wait_for_common(x, timeout, TASK_INTERRUPTIBLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004772}
4773EXPORT_SYMBOL(wait_for_completion_interruptible_timeout);
4774
Kevin Diggs65eb3dc2008-08-26 10:26:54 +02004775/**
4776 * wait_for_completion_killable: - waits for completion of a task (killable)
4777 * @x: holds the state of this particular completion
4778 *
4779 * This waits to be signaled for completion of a specific task. It can be
4780 * interrupted by a kill signal.
4781 */
Matthew Wilcox009e5772007-12-06 12:29:54 -05004782int __sched wait_for_completion_killable(struct completion *x)
4783{
4784 long t = wait_for_common(x, MAX_SCHEDULE_TIMEOUT, TASK_KILLABLE);
4785 if (t == -ERESTARTSYS)
4786 return t;
4787 return 0;
4788}
4789EXPORT_SYMBOL(wait_for_completion_killable);
4790
Dave Chinnerbe4de352008-08-15 00:40:44 -07004791/**
Sage Weil0aa12fb2010-05-29 09:12:30 -07004792 * wait_for_completion_killable_timeout: - waits for completion of a task (w/(to,killable))
4793 * @x: holds the state of this particular completion
4794 * @timeout: timeout value in jiffies
4795 *
4796 * This waits for either a completion of a specific task to be
4797 * signaled or for a specified timeout to expire. It can be
4798 * interrupted by a kill signal. The timeout is in jiffies.
4799 */
NeilBrown6bf41232011-01-05 12:50:16 +11004800long __sched
Sage Weil0aa12fb2010-05-29 09:12:30 -07004801wait_for_completion_killable_timeout(struct completion *x,
4802 unsigned long timeout)
4803{
4804 return wait_for_common(x, timeout, TASK_KILLABLE);
4805}
4806EXPORT_SYMBOL(wait_for_completion_killable_timeout);
4807
4808/**
Dave Chinnerbe4de352008-08-15 00:40:44 -07004809 * try_wait_for_completion - try to decrement a completion without blocking
4810 * @x: completion structure
4811 *
4812 * Returns: 0 if a decrement cannot be done without blocking
4813 * 1 if a decrement succeeded.
4814 *
4815 * If a completion is being used as a counting completion,
4816 * attempt to decrement the counter without blocking. This
4817 * enables us to avoid waiting if the resource the completion
4818 * is protecting is not available.
4819 */
4820bool try_wait_for_completion(struct completion *x)
4821{
Rafael J. Wysocki7539a3b2009-12-13 00:07:30 +01004822 unsigned long flags;
Dave Chinnerbe4de352008-08-15 00:40:44 -07004823 int ret = 1;
4824
Rafael J. Wysocki7539a3b2009-12-13 00:07:30 +01004825 spin_lock_irqsave(&x->wait.lock, flags);
Dave Chinnerbe4de352008-08-15 00:40:44 -07004826 if (!x->done)
4827 ret = 0;
4828 else
4829 x->done--;
Rafael J. Wysocki7539a3b2009-12-13 00:07:30 +01004830 spin_unlock_irqrestore(&x->wait.lock, flags);
Dave Chinnerbe4de352008-08-15 00:40:44 -07004831 return ret;
4832}
4833EXPORT_SYMBOL(try_wait_for_completion);
4834
4835/**
4836 * completion_done - Test to see if a completion has any waiters
4837 * @x: completion structure
4838 *
4839 * Returns: 0 if there are waiters (wait_for_completion() in progress)
4840 * 1 if there are no waiters.
4841 *
4842 */
4843bool completion_done(struct completion *x)
4844{
Rafael J. Wysocki7539a3b2009-12-13 00:07:30 +01004845 unsigned long flags;
Dave Chinnerbe4de352008-08-15 00:40:44 -07004846 int ret = 1;
4847
Rafael J. Wysocki7539a3b2009-12-13 00:07:30 +01004848 spin_lock_irqsave(&x->wait.lock, flags);
Dave Chinnerbe4de352008-08-15 00:40:44 -07004849 if (!x->done)
4850 ret = 0;
Rafael J. Wysocki7539a3b2009-12-13 00:07:30 +01004851 spin_unlock_irqrestore(&x->wait.lock, flags);
Dave Chinnerbe4de352008-08-15 00:40:44 -07004852 return ret;
4853}
4854EXPORT_SYMBOL(completion_done);
4855
Andi Kleen8cbbe862007-10-15 17:00:14 +02004856static long __sched
4857sleep_on_common(wait_queue_head_t *q, int state, long timeout)
Ingo Molnar0fec1712007-07-09 18:52:01 +02004858{
4859 unsigned long flags;
4860 wait_queue_t wait;
4861
4862 init_waitqueue_entry(&wait, current);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004863
Andi Kleen8cbbe862007-10-15 17:00:14 +02004864 __set_current_state(state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004865
Andi Kleen8cbbe862007-10-15 17:00:14 +02004866 spin_lock_irqsave(&q->lock, flags);
4867 __add_wait_queue(q, &wait);
4868 spin_unlock(&q->lock);
4869 timeout = schedule_timeout(timeout);
4870 spin_lock_irq(&q->lock);
4871 __remove_wait_queue(q, &wait);
4872 spin_unlock_irqrestore(&q->lock, flags);
4873
4874 return timeout;
4875}
4876
4877void __sched interruptible_sleep_on(wait_queue_head_t *q)
4878{
4879 sleep_on_common(q, TASK_INTERRUPTIBLE, MAX_SCHEDULE_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004880}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004881EXPORT_SYMBOL(interruptible_sleep_on);
4882
Ingo Molnar0fec1712007-07-09 18:52:01 +02004883long __sched
Ingo Molnar95cdf3b2005-09-10 00:26:11 -07004884interruptible_sleep_on_timeout(wait_queue_head_t *q, long timeout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004885{
Andi Kleen8cbbe862007-10-15 17:00:14 +02004886 return sleep_on_common(q, TASK_INTERRUPTIBLE, timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004887}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004888EXPORT_SYMBOL(interruptible_sleep_on_timeout);
4889
Ingo Molnar0fec1712007-07-09 18:52:01 +02004890void __sched sleep_on(wait_queue_head_t *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004891{
Andi Kleen8cbbe862007-10-15 17:00:14 +02004892 sleep_on_common(q, TASK_UNINTERRUPTIBLE, MAX_SCHEDULE_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004893}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004894EXPORT_SYMBOL(sleep_on);
4895
Ingo Molnar0fec1712007-07-09 18:52:01 +02004896long __sched sleep_on_timeout(wait_queue_head_t *q, long timeout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004897{
Andi Kleen8cbbe862007-10-15 17:00:14 +02004898 return sleep_on_common(q, TASK_UNINTERRUPTIBLE, timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004899}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004900EXPORT_SYMBOL(sleep_on_timeout);
4901
Ingo Molnarb29739f2006-06-27 02:54:51 -07004902#ifdef CONFIG_RT_MUTEXES
4903
4904/*
4905 * rt_mutex_setprio - set the current priority of a task
4906 * @p: task
4907 * @prio: prio value (kernel-internal form)
4908 *
4909 * This function changes the 'effective' priority of a task. It does
4910 * not touch ->normal_prio like __setscheduler().
4911 *
4912 * Used by the rt_mutex code to implement priority inheritance logic.
4913 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07004914void rt_mutex_setprio(struct task_struct *p, int prio)
Ingo Molnarb29739f2006-06-27 02:54:51 -07004915{
Srivatsa Vaddagiri83b699e2007-10-15 17:00:08 +02004916 int oldprio, on_rq, running;
Ingo Molnar70b97a72006-07-03 00:25:42 -07004917 struct rq *rq;
Thomas Gleixner83ab0aa2010-02-17 09:05:48 +01004918 const struct sched_class *prev_class;
Ingo Molnarb29739f2006-06-27 02:54:51 -07004919
4920 BUG_ON(prio < 0 || prio > MAX_PRIO);
4921
Peter Zijlstra0122ec52011-04-05 17:23:51 +02004922 rq = __task_rq_lock(p);
Ingo Molnarb29739f2006-06-27 02:54:51 -07004923
Steven Rostedta8027072010-09-20 15:13:34 -04004924 trace_sched_pi_setprio(p, prio);
Andrew Mortond5f9f942007-05-08 20:27:06 -07004925 oldprio = p->prio;
Thomas Gleixner83ab0aa2010-02-17 09:05:48 +01004926 prev_class = p->sched_class;
Peter Zijlstrafd2f4412011-04-05 17:23:44 +02004927 on_rq = p->on_rq;
Dmitry Adamushko051a1d12007-12-18 15:21:13 +01004928 running = task_current(rq, p);
Hiroshi Shimamoto0e1f3482008-03-10 11:01:20 -07004929 if (on_rq)
Ingo Molnar69be72c2007-08-09 11:16:49 +02004930 dequeue_task(rq, p, 0);
Hiroshi Shimamoto0e1f3482008-03-10 11:01:20 -07004931 if (running)
4932 p->sched_class->put_prev_task(rq, p);
Ingo Molnardd41f592007-07-09 18:51:59 +02004933
4934 if (rt_prio(prio))
4935 p->sched_class = &rt_sched_class;
4936 else
4937 p->sched_class = &fair_sched_class;
4938
Ingo Molnarb29739f2006-06-27 02:54:51 -07004939 p->prio = prio;
4940
Hiroshi Shimamoto0e1f3482008-03-10 11:01:20 -07004941 if (running)
4942 p->sched_class->set_curr_task(rq);
Peter Zijlstrada7a7352011-01-17 17:03:27 +01004943 if (on_rq)
Peter Zijlstra371fd7e2010-03-24 16:38:48 +01004944 enqueue_task(rq, p, oldprio < prio ? ENQUEUE_HEAD : 0);
Steven Rostedtcb469842008-01-25 21:08:22 +01004945
Peter Zijlstrada7a7352011-01-17 17:03:27 +01004946 check_class_changed(rq, p, prev_class, oldprio);
Peter Zijlstra0122ec52011-04-05 17:23:51 +02004947 __task_rq_unlock(rq);
Ingo Molnarb29739f2006-06-27 02:54:51 -07004948}
4949
4950#endif
4951
Ingo Molnar36c8b582006-07-03 00:25:41 -07004952void set_user_nice(struct task_struct *p, long nice)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004953{
Ingo Molnardd41f592007-07-09 18:51:59 +02004954 int old_prio, delta, on_rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004955 unsigned long flags;
Ingo Molnar70b97a72006-07-03 00:25:42 -07004956 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004957
4958 if (TASK_NICE(p) == nice || nice < -20 || nice > 19)
4959 return;
4960 /*
4961 * We have to be careful, if called from sys_setpriority(),
4962 * the task might be in the middle of scheduling on another CPU.
4963 */
4964 rq = task_rq_lock(p, &flags);
4965 /*
4966 * The RT priorities are set via sched_setscheduler(), but we still
4967 * allow the 'normal' nice value to be set - but as expected
4968 * it wont have any effect on scheduling until the task is
Ingo Molnardd41f592007-07-09 18:51:59 +02004969 * SCHED_FIFO/SCHED_RR:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004970 */
Ingo Molnare05606d2007-07-09 18:51:59 +02004971 if (task_has_rt_policy(p)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004972 p->static_prio = NICE_TO_PRIO(nice);
4973 goto out_unlock;
4974 }
Peter Zijlstrafd2f4412011-04-05 17:23:44 +02004975 on_rq = p->on_rq;
Peter Zijlstrac09595f2008-06-27 13:41:14 +02004976 if (on_rq)
Ingo Molnar69be72c2007-08-09 11:16:49 +02004977 dequeue_task(rq, p, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004978
Linus Torvalds1da177e2005-04-16 15:20:36 -07004979 p->static_prio = NICE_TO_PRIO(nice);
Peter Williams2dd73a42006-06-27 02:54:34 -07004980 set_load_weight(p);
Ingo Molnarb29739f2006-06-27 02:54:51 -07004981 old_prio = p->prio;
4982 p->prio = effective_prio(p);
4983 delta = p->prio - old_prio;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004984
Ingo Molnardd41f592007-07-09 18:51:59 +02004985 if (on_rq) {
Peter Zijlstra371fd7e2010-03-24 16:38:48 +01004986 enqueue_task(rq, p, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004987 /*
Andrew Mortond5f9f942007-05-08 20:27:06 -07004988 * If the task increased its priority or is running and
4989 * lowered its priority, then reschedule its CPU:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004990 */
Andrew Mortond5f9f942007-05-08 20:27:06 -07004991 if (delta < 0 || (delta > 0 && task_running(rq, p)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004992 resched_task(rq->curr);
4993 }
4994out_unlock:
Peter Zijlstra0122ec52011-04-05 17:23:51 +02004995 task_rq_unlock(rq, p, &flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004996}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004997EXPORT_SYMBOL(set_user_nice);
4998
Matt Mackalle43379f2005-05-01 08:59:00 -07004999/*
5000 * can_nice - check if a task can reduce its nice value
5001 * @p: task
5002 * @nice: nice value
5003 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07005004int can_nice(const struct task_struct *p, const int nice)
Matt Mackalle43379f2005-05-01 08:59:00 -07005005{
Matt Mackall024f4742005-08-18 11:24:19 -07005006 /* convert nice value [19,-20] to rlimit style value [1,40] */
5007 int nice_rlim = 20 - nice;
Ingo Molnar48f24c42006-07-03 00:25:40 -07005008
Jiri Slaby78d7d402010-03-05 13:42:54 -08005009 return (nice_rlim <= task_rlimit(p, RLIMIT_NICE) ||
Matt Mackalle43379f2005-05-01 08:59:00 -07005010 capable(CAP_SYS_NICE));
5011}
5012
Linus Torvalds1da177e2005-04-16 15:20:36 -07005013#ifdef __ARCH_WANT_SYS_NICE
5014
5015/*
5016 * sys_nice - change the priority of the current process.
5017 * @increment: priority increment
5018 *
5019 * sys_setpriority is a more generic, but much slower function that
5020 * does similar things.
5021 */
Heiko Carstens5add95d2009-01-14 14:14:08 +01005022SYSCALL_DEFINE1(nice, int, increment)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005023{
Ingo Molnar48f24c42006-07-03 00:25:40 -07005024 long nice, retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005025
5026 /*
5027 * Setpriority might change our priority at the same moment.
5028 * We don't have to worry. Conceptually one call occurs first
5029 * and we have a single winner.
5030 */
Matt Mackalle43379f2005-05-01 08:59:00 -07005031 if (increment < -40)
5032 increment = -40;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005033 if (increment > 40)
5034 increment = 40;
5035
Américo Wang2b8f8362009-02-16 18:54:21 +08005036 nice = TASK_NICE(current) + increment;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005037 if (nice < -20)
5038 nice = -20;
5039 if (nice > 19)
5040 nice = 19;
5041
Matt Mackalle43379f2005-05-01 08:59:00 -07005042 if (increment < 0 && !can_nice(current, nice))
5043 return -EPERM;
5044
Linus Torvalds1da177e2005-04-16 15:20:36 -07005045 retval = security_task_setnice(current, nice);
5046 if (retval)
5047 return retval;
5048
5049 set_user_nice(current, nice);
5050 return 0;
5051}
5052
5053#endif
5054
5055/**
5056 * task_prio - return the priority value of a given task.
5057 * @p: the task in question.
5058 *
5059 * This is the priority value as seen by users in /proc.
5060 * RT tasks are offset by -200. Normal tasks are centered
5061 * around 0, value goes from -16 to +15.
5062 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07005063int task_prio(const struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005064{
5065 return p->prio - MAX_RT_PRIO;
5066}
5067
5068/**
5069 * task_nice - return the nice value of a given task.
5070 * @p: the task in question.
5071 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07005072int task_nice(const struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005073{
5074 return TASK_NICE(p);
5075}
Pavel Roskin150d8be2008-03-05 16:56:37 -05005076EXPORT_SYMBOL(task_nice);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005077
5078/**
5079 * idle_cpu - is a given cpu idle currently?
5080 * @cpu: the processor in question.
5081 */
5082int idle_cpu(int cpu)
5083{
5084 return cpu_curr(cpu) == cpu_rq(cpu)->idle;
5085}
5086
Linus Torvalds1da177e2005-04-16 15:20:36 -07005087/**
5088 * idle_task - return the idle task for a given cpu.
5089 * @cpu: the processor in question.
5090 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07005091struct task_struct *idle_task(int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005092{
5093 return cpu_rq(cpu)->idle;
5094}
5095
5096/**
5097 * find_process_by_pid - find a process with a matching PID value.
5098 * @pid: the pid in question.
5099 */
Alexey Dobriyana9957442007-10-15 17:00:13 +02005100static struct task_struct *find_process_by_pid(pid_t pid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005101{
Pavel Emelyanov228ebcb2007-10-18 23:40:16 -07005102 return pid ? find_task_by_vpid(pid) : current;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005103}
5104
5105/* Actually do priority change: must hold rq lock. */
Ingo Molnardd41f592007-07-09 18:51:59 +02005106static void
5107__setscheduler(struct rq *rq, struct task_struct *p, int policy, int prio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005108{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005109 p->policy = policy;
5110 p->rt_priority = prio;
Ingo Molnarb29739f2006-06-27 02:54:51 -07005111 p->normal_prio = normal_prio(p);
5112 /* we are holding p->pi_lock already */
5113 p->prio = rt_mutex_getprio(p);
Peter Zijlstraffd44db2009-11-10 20:12:01 +01005114 if (rt_prio(p->prio))
5115 p->sched_class = &rt_sched_class;
5116 else
5117 p->sched_class = &fair_sched_class;
Peter Williams2dd73a42006-06-27 02:54:34 -07005118 set_load_weight(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005119}
5120
David Howellsc69e8d92008-11-14 10:39:19 +11005121/*
5122 * check the target process has a UID that matches the current process's
5123 */
5124static bool check_same_owner(struct task_struct *p)
5125{
5126 const struct cred *cred = current_cred(), *pcred;
5127 bool match;
5128
5129 rcu_read_lock();
5130 pcred = __task_cred(p);
Serge E. Hallynb0e77592011-03-23 16:43:24 -07005131 if (cred->user->user_ns == pcred->user->user_ns)
5132 match = (cred->euid == pcred->euid ||
5133 cred->euid == pcred->uid);
5134 else
5135 match = false;
David Howellsc69e8d92008-11-14 10:39:19 +11005136 rcu_read_unlock();
5137 return match;
5138}
5139
Rusty Russell961ccdd2008-06-23 13:55:38 +10005140static int __sched_setscheduler(struct task_struct *p, int policy,
KOSAKI Motohirofe7de492010-10-20 16:01:12 -07005141 const struct sched_param *param, bool user)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005142{
Srivatsa Vaddagiri83b699e2007-10-15 17:00:08 +02005143 int retval, oldprio, oldpolicy = -1, on_rq, running;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005144 unsigned long flags;
Thomas Gleixner83ab0aa2010-02-17 09:05:48 +01005145 const struct sched_class *prev_class;
Ingo Molnar70b97a72006-07-03 00:25:42 -07005146 struct rq *rq;
Lennart Poetteringca94c442009-06-15 17:17:47 +02005147 int reset_on_fork;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005148
Steven Rostedt66e53932006-06-27 02:54:44 -07005149 /* may grab non-irq protected spin_locks */
5150 BUG_ON(in_interrupt());
Linus Torvalds1da177e2005-04-16 15:20:36 -07005151recheck:
5152 /* double check policy once rq lock held */
Lennart Poetteringca94c442009-06-15 17:17:47 +02005153 if (policy < 0) {
5154 reset_on_fork = p->sched_reset_on_fork;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005155 policy = oldpolicy = p->policy;
Lennart Poetteringca94c442009-06-15 17:17:47 +02005156 } else {
5157 reset_on_fork = !!(policy & SCHED_RESET_ON_FORK);
5158 policy &= ~SCHED_RESET_ON_FORK;
5159
5160 if (policy != SCHED_FIFO && policy != SCHED_RR &&
5161 policy != SCHED_NORMAL && policy != SCHED_BATCH &&
5162 policy != SCHED_IDLE)
5163 return -EINVAL;
5164 }
5165
Linus Torvalds1da177e2005-04-16 15:20:36 -07005166 /*
5167 * Valid priorities for SCHED_FIFO and SCHED_RR are
Ingo Molnardd41f592007-07-09 18:51:59 +02005168 * 1..MAX_USER_RT_PRIO-1, valid priority for SCHED_NORMAL,
5169 * SCHED_BATCH and SCHED_IDLE is 0.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005170 */
5171 if (param->sched_priority < 0 ||
Ingo Molnar95cdf3b2005-09-10 00:26:11 -07005172 (p->mm && param->sched_priority > MAX_USER_RT_PRIO-1) ||
Steven Rostedtd46523e2005-07-25 16:28:39 -04005173 (!p->mm && param->sched_priority > MAX_RT_PRIO-1))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005174 return -EINVAL;
Ingo Molnare05606d2007-07-09 18:51:59 +02005175 if (rt_policy(policy) != (param->sched_priority != 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005176 return -EINVAL;
5177
Olivier Croquette37e4ab32005-06-25 14:57:32 -07005178 /*
5179 * Allow unprivileged RT tasks to decrease priority:
5180 */
Rusty Russell961ccdd2008-06-23 13:55:38 +10005181 if (user && !capable(CAP_SYS_NICE)) {
Ingo Molnare05606d2007-07-09 18:51:59 +02005182 if (rt_policy(policy)) {
Oleg Nesterova44702e2010-06-11 01:09:44 +02005183 unsigned long rlim_rtprio =
5184 task_rlimit(p, RLIMIT_RTPRIO);
Oleg Nesterov5fe1d752006-09-29 02:00:48 -07005185
Oleg Nesterov8dc3e902006-09-29 02:00:50 -07005186 /* can't set/change the rt policy */
5187 if (policy != p->policy && !rlim_rtprio)
5188 return -EPERM;
5189
5190 /* can't increase priority */
5191 if (param->sched_priority > p->rt_priority &&
5192 param->sched_priority > rlim_rtprio)
5193 return -EPERM;
5194 }
Darren Hartc02aa732011-02-17 15:37:07 -08005195
Ingo Molnardd41f592007-07-09 18:51:59 +02005196 /*
Darren Hartc02aa732011-02-17 15:37:07 -08005197 * Treat SCHED_IDLE as nice 20. Only allow a switch to
5198 * SCHED_NORMAL if the RLIMIT_NICE would normally permit it.
Ingo Molnardd41f592007-07-09 18:51:59 +02005199 */
Darren Hartc02aa732011-02-17 15:37:07 -08005200 if (p->policy == SCHED_IDLE && policy != SCHED_IDLE) {
5201 if (!can_nice(p, TASK_NICE(p)))
5202 return -EPERM;
5203 }
Oleg Nesterov8dc3e902006-09-29 02:00:50 -07005204
Olivier Croquette37e4ab32005-06-25 14:57:32 -07005205 /* can't change other user's priorities */
David Howellsc69e8d92008-11-14 10:39:19 +11005206 if (!check_same_owner(p))
Olivier Croquette37e4ab32005-06-25 14:57:32 -07005207 return -EPERM;
Lennart Poetteringca94c442009-06-15 17:17:47 +02005208
5209 /* Normal users shall not reset the sched_reset_on_fork flag */
5210 if (p->sched_reset_on_fork && !reset_on_fork)
5211 return -EPERM;
Olivier Croquette37e4ab32005-06-25 14:57:32 -07005212 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005213
Jeremy Fitzhardinge725aad22008-08-03 09:33:03 -07005214 if (user) {
KOSAKI Motohirob0ae1982010-10-15 04:21:18 +09005215 retval = security_task_setscheduler(p);
Jeremy Fitzhardinge725aad22008-08-03 09:33:03 -07005216 if (retval)
5217 return retval;
5218 }
5219
Linus Torvalds1da177e2005-04-16 15:20:36 -07005220 /*
Ingo Molnarb29739f2006-06-27 02:54:51 -07005221 * make sure no PI-waiters arrive (or leave) while we are
5222 * changing the priority of the task:
Peter Zijlstra0122ec52011-04-05 17:23:51 +02005223 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -03005224 * To be able to change p->policy safely, the appropriate
Linus Torvalds1da177e2005-04-16 15:20:36 -07005225 * runqueue lock must be held.
5226 */
Peter Zijlstra0122ec52011-04-05 17:23:51 +02005227 rq = task_rq_lock(p, &flags);
Peter Zijlstradc61b1d2010-06-08 11:40:42 +02005228
Peter Zijlstra34f971f2010-09-22 13:53:15 +02005229 /*
5230 * Changing the policy of the stop threads its a very bad idea
5231 */
5232 if (p == rq->stop) {
Peter Zijlstra0122ec52011-04-05 17:23:51 +02005233 task_rq_unlock(rq, p, &flags);
Peter Zijlstra34f971f2010-09-22 13:53:15 +02005234 return -EINVAL;
5235 }
5236
Dario Faggiolia51e9192011-03-24 14:00:18 +01005237 /*
5238 * If not changing anything there's no need to proceed further:
5239 */
5240 if (unlikely(policy == p->policy && (!rt_policy(policy) ||
5241 param->sched_priority == p->rt_priority))) {
5242
5243 __task_rq_unlock(rq);
5244 raw_spin_unlock_irqrestore(&p->pi_lock, flags);
5245 return 0;
5246 }
5247
Peter Zijlstradc61b1d2010-06-08 11:40:42 +02005248#ifdef CONFIG_RT_GROUP_SCHED
5249 if (user) {
5250 /*
5251 * Do not allow realtime tasks into groups that have no runtime
5252 * assigned.
5253 */
5254 if (rt_bandwidth_enabled() && rt_policy(policy) &&
Mike Galbraithf4493772011-01-13 04:54:50 +01005255 task_group(p)->rt_bandwidth.rt_runtime == 0 &&
5256 !task_group_is_autogroup(task_group(p))) {
Peter Zijlstra0122ec52011-04-05 17:23:51 +02005257 task_rq_unlock(rq, p, &flags);
Peter Zijlstradc61b1d2010-06-08 11:40:42 +02005258 return -EPERM;
5259 }
5260 }
5261#endif
5262
Linus Torvalds1da177e2005-04-16 15:20:36 -07005263 /* recheck policy now with rq lock held */
5264 if (unlikely(oldpolicy != -1 && oldpolicy != p->policy)) {
5265 policy = oldpolicy = -1;
Peter Zijlstra0122ec52011-04-05 17:23:51 +02005266 task_rq_unlock(rq, p, &flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005267 goto recheck;
5268 }
Peter Zijlstrafd2f4412011-04-05 17:23:44 +02005269 on_rq = p->on_rq;
Dmitry Adamushko051a1d12007-12-18 15:21:13 +01005270 running = task_current(rq, p);
Hiroshi Shimamoto0e1f3482008-03-10 11:01:20 -07005271 if (on_rq)
Ingo Molnar2e1cb742007-08-09 11:16:49 +02005272 deactivate_task(rq, p, 0);
Hiroshi Shimamoto0e1f3482008-03-10 11:01:20 -07005273 if (running)
5274 p->sched_class->put_prev_task(rq, p);
Dmitry Adamushkof6b53202007-10-15 17:00:08 +02005275
Lennart Poetteringca94c442009-06-15 17:17:47 +02005276 p->sched_reset_on_fork = reset_on_fork;
5277
Linus Torvalds1da177e2005-04-16 15:20:36 -07005278 oldprio = p->prio;
Thomas Gleixner83ab0aa2010-02-17 09:05:48 +01005279 prev_class = p->sched_class;
Ingo Molnardd41f592007-07-09 18:51:59 +02005280 __setscheduler(rq, p, policy, param->sched_priority);
Dmitry Adamushkof6b53202007-10-15 17:00:08 +02005281
Hiroshi Shimamoto0e1f3482008-03-10 11:01:20 -07005282 if (running)
5283 p->sched_class->set_curr_task(rq);
Peter Zijlstrada7a7352011-01-17 17:03:27 +01005284 if (on_rq)
Ingo Molnardd41f592007-07-09 18:51:59 +02005285 activate_task(rq, p, 0);
Steven Rostedtcb469842008-01-25 21:08:22 +01005286
Peter Zijlstrada7a7352011-01-17 17:03:27 +01005287 check_class_changed(rq, p, prev_class, oldprio);
Peter Zijlstra0122ec52011-04-05 17:23:51 +02005288 task_rq_unlock(rq, p, &flags);
Ingo Molnarb29739f2006-06-27 02:54:51 -07005289
Thomas Gleixner95e02ca2006-06-27 02:55:02 -07005290 rt_mutex_adjust_pi(p);
5291
Linus Torvalds1da177e2005-04-16 15:20:36 -07005292 return 0;
5293}
Rusty Russell961ccdd2008-06-23 13:55:38 +10005294
5295/**
5296 * sched_setscheduler - change the scheduling policy and/or RT priority of a thread.
5297 * @p: the task in question.
5298 * @policy: new policy.
5299 * @param: structure containing the new RT priority.
5300 *
5301 * NOTE that the task may be already dead.
5302 */
5303int sched_setscheduler(struct task_struct *p, int policy,
KOSAKI Motohirofe7de492010-10-20 16:01:12 -07005304 const struct sched_param *param)
Rusty Russell961ccdd2008-06-23 13:55:38 +10005305{
5306 return __sched_setscheduler(p, policy, param, true);
5307}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005308EXPORT_SYMBOL_GPL(sched_setscheduler);
5309
Rusty Russell961ccdd2008-06-23 13:55:38 +10005310/**
5311 * sched_setscheduler_nocheck - change the scheduling policy and/or RT priority of a thread from kernelspace.
5312 * @p: the task in question.
5313 * @policy: new policy.
5314 * @param: structure containing the new RT priority.
5315 *
5316 * Just like sched_setscheduler, only don't bother checking if the
5317 * current context has permission. For example, this is needed in
5318 * stop_machine(): we create temporary high priority worker threads,
5319 * but our caller might not have that capability.
5320 */
5321int sched_setscheduler_nocheck(struct task_struct *p, int policy,
KOSAKI Motohirofe7de492010-10-20 16:01:12 -07005322 const struct sched_param *param)
Rusty Russell961ccdd2008-06-23 13:55:38 +10005323{
5324 return __sched_setscheduler(p, policy, param, false);
5325}
5326
Ingo Molnar95cdf3b2005-09-10 00:26:11 -07005327static int
5328do_sched_setscheduler(pid_t pid, int policy, struct sched_param __user *param)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005329{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005330 struct sched_param lparam;
5331 struct task_struct *p;
Ingo Molnar36c8b582006-07-03 00:25:41 -07005332 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005333
5334 if (!param || pid < 0)
5335 return -EINVAL;
5336 if (copy_from_user(&lparam, param, sizeof(struct sched_param)))
5337 return -EFAULT;
Oleg Nesterov5fe1d752006-09-29 02:00:48 -07005338
5339 rcu_read_lock();
5340 retval = -ESRCH;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005341 p = find_process_by_pid(pid);
Oleg Nesterov5fe1d752006-09-29 02:00:48 -07005342 if (p != NULL)
5343 retval = sched_setscheduler(p, policy, &lparam);
5344 rcu_read_unlock();
Ingo Molnar36c8b582006-07-03 00:25:41 -07005345
Linus Torvalds1da177e2005-04-16 15:20:36 -07005346 return retval;
5347}
5348
5349/**
5350 * sys_sched_setscheduler - set/change the scheduler policy and RT priority
5351 * @pid: the pid in question.
5352 * @policy: new policy.
5353 * @param: structure containing the new RT priority.
5354 */
Heiko Carstens5add95d2009-01-14 14:14:08 +01005355SYSCALL_DEFINE3(sched_setscheduler, pid_t, pid, int, policy,
5356 struct sched_param __user *, param)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005357{
Jason Baronc21761f2006-01-18 17:43:03 -08005358 /* negative values for policy are not valid */
5359 if (policy < 0)
5360 return -EINVAL;
5361
Linus Torvalds1da177e2005-04-16 15:20:36 -07005362 return do_sched_setscheduler(pid, policy, param);
5363}
5364
5365/**
5366 * sys_sched_setparam - set/change the RT priority of a thread
5367 * @pid: the pid in question.
5368 * @param: structure containing the new RT priority.
5369 */
Heiko Carstens5add95d2009-01-14 14:14:08 +01005370SYSCALL_DEFINE2(sched_setparam, pid_t, pid, struct sched_param __user *, param)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005371{
5372 return do_sched_setscheduler(pid, -1, param);
5373}
5374
5375/**
5376 * sys_sched_getscheduler - get the policy (scheduling class) of a thread
5377 * @pid: the pid in question.
5378 */
Heiko Carstens5add95d2009-01-14 14:14:08 +01005379SYSCALL_DEFINE1(sched_getscheduler, pid_t, pid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005380{
Ingo Molnar36c8b582006-07-03 00:25:41 -07005381 struct task_struct *p;
Andi Kleen3a5c3592007-10-15 17:00:14 +02005382 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005383
5384 if (pid < 0)
Andi Kleen3a5c3592007-10-15 17:00:14 +02005385 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005386
5387 retval = -ESRCH;
Thomas Gleixner5fe85be2009-12-09 10:14:58 +00005388 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005389 p = find_process_by_pid(pid);
5390 if (p) {
5391 retval = security_task_getscheduler(p);
5392 if (!retval)
Lennart Poetteringca94c442009-06-15 17:17:47 +02005393 retval = p->policy
5394 | (p->sched_reset_on_fork ? SCHED_RESET_ON_FORK : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005395 }
Thomas Gleixner5fe85be2009-12-09 10:14:58 +00005396 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005397 return retval;
5398}
5399
5400/**
Lennart Poetteringca94c442009-06-15 17:17:47 +02005401 * sys_sched_getparam - get the RT priority of a thread
Linus Torvalds1da177e2005-04-16 15:20:36 -07005402 * @pid: the pid in question.
5403 * @param: structure containing the RT priority.
5404 */
Heiko Carstens5add95d2009-01-14 14:14:08 +01005405SYSCALL_DEFINE2(sched_getparam, pid_t, pid, struct sched_param __user *, param)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005406{
5407 struct sched_param lp;
Ingo Molnar36c8b582006-07-03 00:25:41 -07005408 struct task_struct *p;
Andi Kleen3a5c3592007-10-15 17:00:14 +02005409 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005410
5411 if (!param || pid < 0)
Andi Kleen3a5c3592007-10-15 17:00:14 +02005412 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005413
Thomas Gleixner5fe85be2009-12-09 10:14:58 +00005414 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005415 p = find_process_by_pid(pid);
5416 retval = -ESRCH;
5417 if (!p)
5418 goto out_unlock;
5419
5420 retval = security_task_getscheduler(p);
5421 if (retval)
5422 goto out_unlock;
5423
5424 lp.sched_priority = p->rt_priority;
Thomas Gleixner5fe85be2009-12-09 10:14:58 +00005425 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005426
5427 /*
5428 * This one might sleep, we cannot do it with a spinlock held ...
5429 */
5430 retval = copy_to_user(param, &lp, sizeof(*param)) ? -EFAULT : 0;
5431
Linus Torvalds1da177e2005-04-16 15:20:36 -07005432 return retval;
5433
5434out_unlock:
Thomas Gleixner5fe85be2009-12-09 10:14:58 +00005435 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005436 return retval;
5437}
5438
Rusty Russell96f874e2008-11-25 02:35:14 +10305439long sched_setaffinity(pid_t pid, const struct cpumask *in_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005440{
Rusty Russell5a16f3d2008-11-25 02:35:11 +10305441 cpumask_var_t cpus_allowed, new_mask;
Ingo Molnar36c8b582006-07-03 00:25:41 -07005442 struct task_struct *p;
5443 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005444
Gautham R Shenoy95402b32008-01-25 21:08:02 +01005445 get_online_cpus();
Thomas Gleixner23f5d142009-12-09 10:15:01 +00005446 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005447
5448 p = find_process_by_pid(pid);
5449 if (!p) {
Thomas Gleixner23f5d142009-12-09 10:15:01 +00005450 rcu_read_unlock();
Gautham R Shenoy95402b32008-01-25 21:08:02 +01005451 put_online_cpus();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005452 return -ESRCH;
5453 }
5454
Thomas Gleixner23f5d142009-12-09 10:15:01 +00005455 /* Prevent p going away */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005456 get_task_struct(p);
Thomas Gleixner23f5d142009-12-09 10:15:01 +00005457 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005458
Rusty Russell5a16f3d2008-11-25 02:35:11 +10305459 if (!alloc_cpumask_var(&cpus_allowed, GFP_KERNEL)) {
5460 retval = -ENOMEM;
5461 goto out_put_task;
5462 }
5463 if (!alloc_cpumask_var(&new_mask, GFP_KERNEL)) {
5464 retval = -ENOMEM;
5465 goto out_free_cpus_allowed;
5466 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005467 retval = -EPERM;
Serge E. Hallynb0e77592011-03-23 16:43:24 -07005468 if (!check_same_owner(p) && !task_ns_capable(p, CAP_SYS_NICE))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005469 goto out_unlock;
5470
KOSAKI Motohirob0ae1982010-10-15 04:21:18 +09005471 retval = security_task_setscheduler(p);
David Quigleye7834f82006-06-23 02:03:59 -07005472 if (retval)
5473 goto out_unlock;
5474
Rusty Russell5a16f3d2008-11-25 02:35:11 +10305475 cpuset_cpus_allowed(p, cpus_allowed);
5476 cpumask_and(new_mask, in_mask, cpus_allowed);
Peter Zijlstra49246272010-10-17 21:46:10 +02005477again:
Rusty Russell5a16f3d2008-11-25 02:35:11 +10305478 retval = set_cpus_allowed_ptr(p, new_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005479
Paul Menage8707d8b2007-10-18 23:40:22 -07005480 if (!retval) {
Rusty Russell5a16f3d2008-11-25 02:35:11 +10305481 cpuset_cpus_allowed(p, cpus_allowed);
5482 if (!cpumask_subset(new_mask, cpus_allowed)) {
Paul Menage8707d8b2007-10-18 23:40:22 -07005483 /*
5484 * We must have raced with a concurrent cpuset
5485 * update. Just reset the cpus_allowed to the
5486 * cpuset's cpus_allowed
5487 */
Rusty Russell5a16f3d2008-11-25 02:35:11 +10305488 cpumask_copy(new_mask, cpus_allowed);
Paul Menage8707d8b2007-10-18 23:40:22 -07005489 goto again;
5490 }
5491 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005492out_unlock:
Rusty Russell5a16f3d2008-11-25 02:35:11 +10305493 free_cpumask_var(new_mask);
5494out_free_cpus_allowed:
5495 free_cpumask_var(cpus_allowed);
5496out_put_task:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005497 put_task_struct(p);
Gautham R Shenoy95402b32008-01-25 21:08:02 +01005498 put_online_cpus();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005499 return retval;
5500}
5501
5502static int get_user_cpu_mask(unsigned long __user *user_mask_ptr, unsigned len,
Rusty Russell96f874e2008-11-25 02:35:14 +10305503 struct cpumask *new_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005504{
Rusty Russell96f874e2008-11-25 02:35:14 +10305505 if (len < cpumask_size())
5506 cpumask_clear(new_mask);
5507 else if (len > cpumask_size())
5508 len = cpumask_size();
5509
Linus Torvalds1da177e2005-04-16 15:20:36 -07005510 return copy_from_user(new_mask, user_mask_ptr, len) ? -EFAULT : 0;
5511}
5512
5513/**
5514 * sys_sched_setaffinity - set the cpu affinity of a process
5515 * @pid: pid of the process
5516 * @len: length in bytes of the bitmask pointed to by user_mask_ptr
5517 * @user_mask_ptr: user-space pointer to the new cpu mask
5518 */
Heiko Carstens5add95d2009-01-14 14:14:08 +01005519SYSCALL_DEFINE3(sched_setaffinity, pid_t, pid, unsigned int, len,
5520 unsigned long __user *, user_mask_ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005521{
Rusty Russell5a16f3d2008-11-25 02:35:11 +10305522 cpumask_var_t new_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005523 int retval;
5524
Rusty Russell5a16f3d2008-11-25 02:35:11 +10305525 if (!alloc_cpumask_var(&new_mask, GFP_KERNEL))
5526 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005527
Rusty Russell5a16f3d2008-11-25 02:35:11 +10305528 retval = get_user_cpu_mask(user_mask_ptr, len, new_mask);
5529 if (retval == 0)
5530 retval = sched_setaffinity(pid, new_mask);
5531 free_cpumask_var(new_mask);
5532 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005533}
5534
Rusty Russell96f874e2008-11-25 02:35:14 +10305535long sched_getaffinity(pid_t pid, struct cpumask *mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005536{
Ingo Molnar36c8b582006-07-03 00:25:41 -07005537 struct task_struct *p;
Thomas Gleixner31605682009-12-08 20:24:16 +00005538 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005539 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005540
Gautham R Shenoy95402b32008-01-25 21:08:02 +01005541 get_online_cpus();
Thomas Gleixner23f5d142009-12-09 10:15:01 +00005542 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005543
5544 retval = -ESRCH;
5545 p = find_process_by_pid(pid);
5546 if (!p)
5547 goto out_unlock;
5548
David Quigleye7834f82006-06-23 02:03:59 -07005549 retval = security_task_getscheduler(p);
5550 if (retval)
5551 goto out_unlock;
5552
Peter Zijlstra013fdb82011-04-05 17:23:45 +02005553 raw_spin_lock_irqsave(&p->pi_lock, flags);
Rusty Russell96f874e2008-11-25 02:35:14 +10305554 cpumask_and(mask, &p->cpus_allowed, cpu_online_mask);
Peter Zijlstra013fdb82011-04-05 17:23:45 +02005555 raw_spin_unlock_irqrestore(&p->pi_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005556
5557out_unlock:
Thomas Gleixner23f5d142009-12-09 10:15:01 +00005558 rcu_read_unlock();
Gautham R Shenoy95402b32008-01-25 21:08:02 +01005559 put_online_cpus();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005560
Ulrich Drepper9531b622007-08-09 11:16:46 +02005561 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005562}
5563
5564/**
5565 * sys_sched_getaffinity - get the cpu affinity of a process
5566 * @pid: pid of the process
5567 * @len: length in bytes of the bitmask pointed to by user_mask_ptr
5568 * @user_mask_ptr: user-space pointer to hold the current cpu mask
5569 */
Heiko Carstens5add95d2009-01-14 14:14:08 +01005570SYSCALL_DEFINE3(sched_getaffinity, pid_t, pid, unsigned int, len,
5571 unsigned long __user *, user_mask_ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005572{
5573 int ret;
Rusty Russellf17c8602008-11-25 02:35:11 +10305574 cpumask_var_t mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005575
Anton Blanchard84fba5e2010-04-06 17:02:19 +10005576 if ((len * BITS_PER_BYTE) < nr_cpu_ids)
KOSAKI Motohirocd3d8032010-03-12 16:15:36 +09005577 return -EINVAL;
5578 if (len & (sizeof(unsigned long)-1))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005579 return -EINVAL;
5580
Rusty Russellf17c8602008-11-25 02:35:11 +10305581 if (!alloc_cpumask_var(&mask, GFP_KERNEL))
5582 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005583
Rusty Russellf17c8602008-11-25 02:35:11 +10305584 ret = sched_getaffinity(pid, mask);
5585 if (ret == 0) {
KOSAKI Motohiro8bc037f2010-03-17 09:36:58 +09005586 size_t retlen = min_t(size_t, len, cpumask_size());
KOSAKI Motohirocd3d8032010-03-12 16:15:36 +09005587
5588 if (copy_to_user(user_mask_ptr, mask, retlen))
Rusty Russellf17c8602008-11-25 02:35:11 +10305589 ret = -EFAULT;
5590 else
KOSAKI Motohirocd3d8032010-03-12 16:15:36 +09005591 ret = retlen;
Rusty Russellf17c8602008-11-25 02:35:11 +10305592 }
5593 free_cpumask_var(mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005594
Rusty Russellf17c8602008-11-25 02:35:11 +10305595 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005596}
5597
5598/**
5599 * sys_sched_yield - yield the current processor to other threads.
5600 *
Ingo Molnardd41f592007-07-09 18:51:59 +02005601 * This function yields the current CPU to other tasks. If there are no
5602 * other threads running on this CPU then this function will return.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005603 */
Heiko Carstens5add95d2009-01-14 14:14:08 +01005604SYSCALL_DEFINE0(sched_yield)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005605{
Ingo Molnar70b97a72006-07-03 00:25:42 -07005606 struct rq *rq = this_rq_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005607
Ingo Molnar2d723762007-10-15 17:00:12 +02005608 schedstat_inc(rq, yld_count);
Dmitry Adamushko4530d7a2007-10-15 17:00:08 +02005609 current->sched_class->yield_task(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005610
5611 /*
5612 * Since we are going to call schedule() anyway, there's
5613 * no need to preempt or enable interrupts:
5614 */
5615 __release(rq->lock);
Ingo Molnar8a25d5d2006-07-03 00:24:54 -07005616 spin_release(&rq->lock.dep_map, 1, _THIS_IP_);
Thomas Gleixner9828ea92009-12-03 20:55:53 +01005617 do_raw_spin_unlock(&rq->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005618 preempt_enable_no_resched();
5619
5620 schedule();
5621
5622 return 0;
5623}
5624
Peter Zijlstrad86ee482009-07-10 14:57:57 +02005625static inline int should_resched(void)
5626{
5627 return need_resched() && !(preempt_count() & PREEMPT_ACTIVE);
5628}
5629
Andrew Mortone7b38402006-06-30 01:56:00 -07005630static void __cond_resched(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005631{
Frederic Weisbeckere7aaaa62009-07-16 15:44:29 +02005632 add_preempt_count(PREEMPT_ACTIVE);
5633 schedule();
5634 sub_preempt_count(PREEMPT_ACTIVE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005635}
5636
Herbert Xu02b67cc2008-01-25 21:08:28 +01005637int __sched _cond_resched(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005638{
Peter Zijlstrad86ee482009-07-10 14:57:57 +02005639 if (should_resched()) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005640 __cond_resched();
5641 return 1;
5642 }
5643 return 0;
5644}
Herbert Xu02b67cc2008-01-25 21:08:28 +01005645EXPORT_SYMBOL(_cond_resched);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005646
5647/*
Frederic Weisbecker613afbf2009-07-16 15:44:29 +02005648 * __cond_resched_lock() - if a reschedule is pending, drop the given lock,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005649 * call schedule, and on return reacquire the lock.
5650 *
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01005651 * This works OK both with and without CONFIG_PREEMPT. We do strange low-level
Linus Torvalds1da177e2005-04-16 15:20:36 -07005652 * operations here to prevent schedule() from being called twice (once via
5653 * spin_unlock(), once by hand).
5654 */
Frederic Weisbecker613afbf2009-07-16 15:44:29 +02005655int __cond_resched_lock(spinlock_t *lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005656{
Peter Zijlstrad86ee482009-07-10 14:57:57 +02005657 int resched = should_resched();
Jan Kara6df3cec2005-06-13 15:52:32 -07005658 int ret = 0;
5659
Peter Zijlstraf607c662009-07-20 19:16:29 +02005660 lockdep_assert_held(lock);
5661
Nick Piggin95c354f2008-01-30 13:31:20 +01005662 if (spin_needbreak(lock) || resched) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005663 spin_unlock(lock);
Peter Zijlstrad86ee482009-07-10 14:57:57 +02005664 if (resched)
Nick Piggin95c354f2008-01-30 13:31:20 +01005665 __cond_resched();
5666 else
5667 cpu_relax();
Jan Kara6df3cec2005-06-13 15:52:32 -07005668 ret = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005669 spin_lock(lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005670 }
Jan Kara6df3cec2005-06-13 15:52:32 -07005671 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005672}
Frederic Weisbecker613afbf2009-07-16 15:44:29 +02005673EXPORT_SYMBOL(__cond_resched_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005674
Frederic Weisbecker613afbf2009-07-16 15:44:29 +02005675int __sched __cond_resched_softirq(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005676{
5677 BUG_ON(!in_softirq());
5678
Peter Zijlstrad86ee482009-07-10 14:57:57 +02005679 if (should_resched()) {
Thomas Gleixner98d82562007-05-23 13:58:18 -07005680 local_bh_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005681 __cond_resched();
5682 local_bh_disable();
5683 return 1;
5684 }
5685 return 0;
5686}
Frederic Weisbecker613afbf2009-07-16 15:44:29 +02005687EXPORT_SYMBOL(__cond_resched_softirq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005688
Linus Torvalds1da177e2005-04-16 15:20:36 -07005689/**
5690 * yield - yield the current processor to other threads.
5691 *
Robert P. J. Day72fd4a32007-02-10 01:45:59 -08005692 * This is a shortcut for kernel-space yielding - it marks the
Linus Torvalds1da177e2005-04-16 15:20:36 -07005693 * thread runnable and calls sys_sched_yield().
5694 */
5695void __sched yield(void)
5696{
5697 set_current_state(TASK_RUNNING);
5698 sys_sched_yield();
5699}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005700EXPORT_SYMBOL(yield);
5701
Mike Galbraithd95f4122011-02-01 09:50:51 -05005702/**
5703 * yield_to - yield the current processor to another thread in
5704 * your thread group, or accelerate that thread toward the
5705 * processor it's on.
Randy Dunlap16addf92011-03-18 09:34:53 -07005706 * @p: target task
5707 * @preempt: whether task preemption is allowed or not
Mike Galbraithd95f4122011-02-01 09:50:51 -05005708 *
5709 * It's the caller's job to ensure that the target task struct
5710 * can't go away on us before we can do any checks.
5711 *
5712 * Returns true if we indeed boosted the target task.
5713 */
5714bool __sched yield_to(struct task_struct *p, bool preempt)
5715{
5716 struct task_struct *curr = current;
5717 struct rq *rq, *p_rq;
5718 unsigned long flags;
5719 bool yielded = 0;
5720
5721 local_irq_save(flags);
5722 rq = this_rq();
5723
5724again:
5725 p_rq = task_rq(p);
5726 double_rq_lock(rq, p_rq);
5727 while (task_rq(p) != p_rq) {
5728 double_rq_unlock(rq, p_rq);
5729 goto again;
5730 }
5731
5732 if (!curr->sched_class->yield_to_task)
5733 goto out;
5734
5735 if (curr->sched_class != p->sched_class)
5736 goto out;
5737
5738 if (task_running(p_rq, p) || p->state)
5739 goto out;
5740
5741 yielded = curr->sched_class->yield_to_task(rq, p, preempt);
Venkatesh Pallipadi6d1cafd2011-03-01 16:28:21 -08005742 if (yielded) {
Mike Galbraithd95f4122011-02-01 09:50:51 -05005743 schedstat_inc(rq, yld_count);
Venkatesh Pallipadi6d1cafd2011-03-01 16:28:21 -08005744 /*
5745 * Make p's CPU reschedule; pick_next_entity takes care of
5746 * fairness.
5747 */
5748 if (preempt && rq != p_rq)
5749 resched_task(p_rq->curr);
5750 }
Mike Galbraithd95f4122011-02-01 09:50:51 -05005751
5752out:
5753 double_rq_unlock(rq, p_rq);
5754 local_irq_restore(flags);
5755
5756 if (yielded)
5757 schedule();
5758
5759 return yielded;
5760}
5761EXPORT_SYMBOL_GPL(yield_to);
5762
Linus Torvalds1da177e2005-04-16 15:20:36 -07005763/*
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01005764 * This task is about to go to sleep on IO. Increment rq->nr_iowait so
Linus Torvalds1da177e2005-04-16 15:20:36 -07005765 * that process accounting knows that this is a task in IO wait state.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005766 */
5767void __sched io_schedule(void)
5768{
Hitoshi Mitake54d35f22009-06-29 14:44:57 +09005769 struct rq *rq = raw_rq();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005770
Shailabh Nagar0ff92242006-07-14 00:24:37 -07005771 delayacct_blkio_start();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005772 atomic_inc(&rq->nr_iowait);
Jens Axboe73c10102011-03-08 13:19:51 +01005773 blk_flush_plug(current);
Arjan van de Ven8f0dfc32009-07-20 11:26:58 -07005774 current->in_iowait = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005775 schedule();
Arjan van de Ven8f0dfc32009-07-20 11:26:58 -07005776 current->in_iowait = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005777 atomic_dec(&rq->nr_iowait);
Shailabh Nagar0ff92242006-07-14 00:24:37 -07005778 delayacct_blkio_end();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005779}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005780EXPORT_SYMBOL(io_schedule);
5781
5782long __sched io_schedule_timeout(long timeout)
5783{
Hitoshi Mitake54d35f22009-06-29 14:44:57 +09005784 struct rq *rq = raw_rq();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005785 long ret;
5786
Shailabh Nagar0ff92242006-07-14 00:24:37 -07005787 delayacct_blkio_start();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005788 atomic_inc(&rq->nr_iowait);
Jens Axboe73c10102011-03-08 13:19:51 +01005789 blk_flush_plug(current);
Arjan van de Ven8f0dfc32009-07-20 11:26:58 -07005790 current->in_iowait = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005791 ret = schedule_timeout(timeout);
Arjan van de Ven8f0dfc32009-07-20 11:26:58 -07005792 current->in_iowait = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005793 atomic_dec(&rq->nr_iowait);
Shailabh Nagar0ff92242006-07-14 00:24:37 -07005794 delayacct_blkio_end();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005795 return ret;
5796}
5797
5798/**
5799 * sys_sched_get_priority_max - return maximum RT priority.
5800 * @policy: scheduling class.
5801 *
5802 * this syscall returns the maximum rt_priority that can be used
5803 * by a given scheduling class.
5804 */
Heiko Carstens5add95d2009-01-14 14:14:08 +01005805SYSCALL_DEFINE1(sched_get_priority_max, int, policy)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005806{
5807 int ret = -EINVAL;
5808
5809 switch (policy) {
5810 case SCHED_FIFO:
5811 case SCHED_RR:
5812 ret = MAX_USER_RT_PRIO-1;
5813 break;
5814 case SCHED_NORMAL:
Ingo Molnarb0a94992006-01-14 13:20:41 -08005815 case SCHED_BATCH:
Ingo Molnardd41f592007-07-09 18:51:59 +02005816 case SCHED_IDLE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005817 ret = 0;
5818 break;
5819 }
5820 return ret;
5821}
5822
5823/**
5824 * sys_sched_get_priority_min - return minimum RT priority.
5825 * @policy: scheduling class.
5826 *
5827 * this syscall returns the minimum rt_priority that can be used
5828 * by a given scheduling class.
5829 */
Heiko Carstens5add95d2009-01-14 14:14:08 +01005830SYSCALL_DEFINE1(sched_get_priority_min, int, policy)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005831{
5832 int ret = -EINVAL;
5833
5834 switch (policy) {
5835 case SCHED_FIFO:
5836 case SCHED_RR:
5837 ret = 1;
5838 break;
5839 case SCHED_NORMAL:
Ingo Molnarb0a94992006-01-14 13:20:41 -08005840 case SCHED_BATCH:
Ingo Molnardd41f592007-07-09 18:51:59 +02005841 case SCHED_IDLE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005842 ret = 0;
5843 }
5844 return ret;
5845}
5846
5847/**
5848 * sys_sched_rr_get_interval - return the default timeslice of a process.
5849 * @pid: pid of the process.
5850 * @interval: userspace pointer to the timeslice value.
5851 *
5852 * this syscall writes the default timeslice value of a given process
5853 * into the user-space timespec buffer. A value of '0' means infinity.
5854 */
Heiko Carstens17da2bd2009-01-14 14:14:10 +01005855SYSCALL_DEFINE2(sched_rr_get_interval, pid_t, pid,
Heiko Carstens754fe8d2009-01-14 14:14:09 +01005856 struct timespec __user *, interval)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005857{
Ingo Molnar36c8b582006-07-03 00:25:41 -07005858 struct task_struct *p;
Dmitry Adamushkoa4ec24b2007-10-15 17:00:13 +02005859 unsigned int time_slice;
Thomas Gleixnerdba091b2009-12-09 09:32:03 +01005860 unsigned long flags;
5861 struct rq *rq;
Andi Kleen3a5c3592007-10-15 17:00:14 +02005862 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005863 struct timespec t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005864
5865 if (pid < 0)
Andi Kleen3a5c3592007-10-15 17:00:14 +02005866 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005867
5868 retval = -ESRCH;
Thomas Gleixner1a551ae2009-12-09 10:15:11 +00005869 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005870 p = find_process_by_pid(pid);
5871 if (!p)
5872 goto out_unlock;
5873
5874 retval = security_task_getscheduler(p);
5875 if (retval)
5876 goto out_unlock;
5877
Thomas Gleixnerdba091b2009-12-09 09:32:03 +01005878 rq = task_rq_lock(p, &flags);
5879 time_slice = p->sched_class->get_rr_interval(rq, p);
Peter Zijlstra0122ec52011-04-05 17:23:51 +02005880 task_rq_unlock(rq, p, &flags);
Dmitry Adamushkoa4ec24b2007-10-15 17:00:13 +02005881
Thomas Gleixner1a551ae2009-12-09 10:15:11 +00005882 rcu_read_unlock();
Dmitry Adamushkoa4ec24b2007-10-15 17:00:13 +02005883 jiffies_to_timespec(time_slice, &t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005884 retval = copy_to_user(interval, &t, sizeof(t)) ? -EFAULT : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005885 return retval;
Andi Kleen3a5c3592007-10-15 17:00:14 +02005886
Linus Torvalds1da177e2005-04-16 15:20:36 -07005887out_unlock:
Thomas Gleixner1a551ae2009-12-09 10:15:11 +00005888 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005889 return retval;
5890}
5891
Steven Rostedt7c731e02008-05-12 21:20:41 +02005892static const char stat_nam[] = TASK_STATE_TO_CHAR_STR;
Ingo Molnar36c8b582006-07-03 00:25:41 -07005893
Ingo Molnar82a1fcb2008-01-25 21:08:02 +01005894void sched_show_task(struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005895{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005896 unsigned long free = 0;
Ingo Molnar36c8b582006-07-03 00:25:41 -07005897 unsigned state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005898
Linus Torvalds1da177e2005-04-16 15:20:36 -07005899 state = p->state ? __ffs(p->state) + 1 : 0;
Erik Gilling28d06862010-11-19 18:08:51 -08005900 printk(KERN_INFO "%-15.15s %c", p->comm,
Andreas Mohr2ed6e342006-07-10 04:43:52 -07005901 state < sizeof(stat_nam) - 1 ? stat_nam[state] : '?');
Ingo Molnar4bd77322007-07-11 21:21:47 +02005902#if BITS_PER_LONG == 32
Linus Torvalds1da177e2005-04-16 15:20:36 -07005903 if (state == TASK_RUNNING)
Peter Zijlstra3df0fc52009-12-20 14:23:57 +01005904 printk(KERN_CONT " running ");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005905 else
Peter Zijlstra3df0fc52009-12-20 14:23:57 +01005906 printk(KERN_CONT " %08lx ", thread_saved_pc(p));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005907#else
5908 if (state == TASK_RUNNING)
Peter Zijlstra3df0fc52009-12-20 14:23:57 +01005909 printk(KERN_CONT " running task ");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005910 else
Peter Zijlstra3df0fc52009-12-20 14:23:57 +01005911 printk(KERN_CONT " %016lx ", thread_saved_pc(p));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005912#endif
5913#ifdef CONFIG_DEBUG_STACK_USAGE
Eric Sandeen7c9f8862008-04-22 16:38:23 -05005914 free = stack_not_used(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005915#endif
Peter Zijlstra3df0fc52009-12-20 14:23:57 +01005916 printk(KERN_CONT "%5lu %5d %6d 0x%08lx\n", free,
David Rientjesaa47b7e2009-05-04 01:38:05 -07005917 task_pid_nr(p), task_pid_nr(p->real_parent),
5918 (unsigned long)task_thread_info(p)->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005919
Nick Piggin5fb5e6d2008-01-25 21:08:34 +01005920 show_stack(p, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005921}
5922
Ingo Molnare59e2ae2006-12-06 20:35:59 -08005923void show_state_filter(unsigned long state_filter)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005924{
Ingo Molnar36c8b582006-07-03 00:25:41 -07005925 struct task_struct *g, *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005926
Ingo Molnar4bd77322007-07-11 21:21:47 +02005927#if BITS_PER_LONG == 32
Peter Zijlstra3df0fc52009-12-20 14:23:57 +01005928 printk(KERN_INFO
5929 " task PC stack pid father\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005930#else
Peter Zijlstra3df0fc52009-12-20 14:23:57 +01005931 printk(KERN_INFO
5932 " task PC stack pid father\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005933#endif
5934 read_lock(&tasklist_lock);
5935 do_each_thread(g, p) {
5936 /*
5937 * reset the NMI-timeout, listing all files on a slow
Lucas De Marchi25985ed2011-03-30 22:57:33 -03005938 * console might take a lot of time:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005939 */
5940 touch_nmi_watchdog();
Ingo Molnar39bc89f2007-04-25 20:50:03 -07005941 if (!state_filter || (p->state & state_filter))
Ingo Molnar82a1fcb2008-01-25 21:08:02 +01005942 sched_show_task(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005943 } while_each_thread(g, p);
5944
Jeremy Fitzhardinge04c91672007-05-08 00:28:05 -07005945 touch_all_softlockup_watchdogs();
5946
Ingo Molnardd41f592007-07-09 18:51:59 +02005947#ifdef CONFIG_SCHED_DEBUG
5948 sysrq_sched_debug_show();
5949#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07005950 read_unlock(&tasklist_lock);
Ingo Molnare59e2ae2006-12-06 20:35:59 -08005951 /*
5952 * Only show locks if all tasks are dumped:
5953 */
Shmulik Ladkani93335a22009-11-25 15:23:41 +02005954 if (!state_filter)
Ingo Molnare59e2ae2006-12-06 20:35:59 -08005955 debug_show_all_locks();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005956}
5957
Ingo Molnar1df21052007-07-09 18:51:58 +02005958void __cpuinit init_idle_bootup_task(struct task_struct *idle)
5959{
Ingo Molnardd41f592007-07-09 18:51:59 +02005960 idle->sched_class = &idle_sched_class;
Ingo Molnar1df21052007-07-09 18:51:58 +02005961}
5962
Ingo Molnarf340c0d2005-06-28 16:40:42 +02005963/**
5964 * init_idle - set up an idle thread for a given CPU
5965 * @idle: task in question
5966 * @cpu: cpu the idle task belongs to
5967 *
5968 * NOTE: this function does not set the idle thread's NEED_RESCHED
5969 * flag, to make booting more robust.
5970 */
Nick Piggin5c1e1762006-10-03 01:14:04 -07005971void __cpuinit init_idle(struct task_struct *idle, int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005972{
Ingo Molnar70b97a72006-07-03 00:25:42 -07005973 struct rq *rq = cpu_rq(cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005974 unsigned long flags;
5975
Thomas Gleixner05fa7852009-11-17 14:28:38 +01005976 raw_spin_lock_irqsave(&rq->lock, flags);
Ingo Molnar5cbd54e2008-11-12 20:05:50 +01005977
Ingo Molnardd41f592007-07-09 18:51:59 +02005978 __sched_fork(idle);
Peter Zijlstra06b83b52009-12-16 18:04:35 +01005979 idle->state = TASK_RUNNING;
Ingo Molnardd41f592007-07-09 18:51:59 +02005980 idle->se.exec_start = sched_clock();
5981
KOSAKI Motohiro1e1b6c52011-05-19 15:08:58 +09005982 do_set_cpus_allowed(idle, cpumask_of(cpu));
Peter Zijlstra6506cf6c2010-09-16 17:50:31 +02005983 /*
5984 * We're having a chicken and egg problem, even though we are
5985 * holding rq->lock, the cpu isn't yet set to this cpu so the
5986 * lockdep check in task_group() will fail.
5987 *
5988 * Similar case to sched_fork(). / Alternatively we could
5989 * use task_rq_lock() here and obtain the other rq->lock.
5990 *
5991 * Silence PROVE_RCU
5992 */
5993 rcu_read_lock();
Ingo Molnardd41f592007-07-09 18:51:59 +02005994 __set_task_cpu(idle, cpu);
Peter Zijlstra6506cf6c2010-09-16 17:50:31 +02005995 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005996
Linus Torvalds1da177e2005-04-16 15:20:36 -07005997 rq->curr = rq->idle = idle;
Peter Zijlstra3ca7a442011-04-05 17:23:40 +02005998#if defined(CONFIG_SMP)
5999 idle->on_cpu = 1;
Nick Piggin4866cde2005-06-25 14:57:23 -07006000#endif
Thomas Gleixner05fa7852009-11-17 14:28:38 +01006001 raw_spin_unlock_irqrestore(&rq->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006002
6003 /* Set the preempt count _outside_ the spinlocks! */
Al Viroa1261f52005-11-13 16:06:55 -08006004 task_thread_info(idle)->preempt_count = 0;
Jonathan Corbet625f2a32011-04-22 11:19:10 -06006005
Ingo Molnardd41f592007-07-09 18:51:59 +02006006 /*
6007 * The idle tasks have their own, simple scheduling class:
6008 */
6009 idle->sched_class = &idle_sched_class;
Steven Rostedt868baf02011-02-10 21:26:13 -05006010 ftrace_graph_init_idle_task(idle, cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006011}
6012
6013/*
6014 * In a system that switches off the HZ timer nohz_cpu_mask
6015 * indicates which cpus entered this state. This is used
6016 * in the rcu update to wait only for active cpus. For system
6017 * which do not switch off the HZ timer nohz_cpu_mask should
Rusty Russell6a7b3dc2008-11-25 02:35:04 +10306018 * always be CPU_BITS_NONE.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006019 */
Rusty Russell6a7b3dc2008-11-25 02:35:04 +10306020cpumask_var_t nohz_cpu_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006021
Ingo Molnar19978ca2007-11-09 22:39:38 +01006022/*
6023 * Increase the granularity value when there are more CPUs,
6024 * because with more CPUs the 'effective latency' as visible
6025 * to users decreases. But the relationship is not linear,
6026 * so pick a second-best guess by going with the log2 of the
6027 * number of CPUs.
6028 *
6029 * This idea comes from the SD scheduler of Con Kolivas:
6030 */
Christian Ehrhardtacb4a842009-11-30 12:16:48 +01006031static int get_update_sysctl_factor(void)
Christian Ehrhardt0bcdcf22009-11-30 12:16:46 +01006032{
Mike Galbraith4ca3ef72009-12-10 09:25:53 +01006033 unsigned int cpus = min_t(int, num_online_cpus(), 8);
Christian Ehrhardt1983a922009-11-30 12:16:47 +01006034 unsigned int factor;
6035
6036 switch (sysctl_sched_tunable_scaling) {
6037 case SCHED_TUNABLESCALING_NONE:
6038 factor = 1;
6039 break;
6040 case SCHED_TUNABLESCALING_LINEAR:
6041 factor = cpus;
6042 break;
6043 case SCHED_TUNABLESCALING_LOG:
6044 default:
6045 factor = 1 + ilog2(cpus);
6046 break;
6047 }
Christian Ehrhardt0bcdcf22009-11-30 12:16:46 +01006048
Christian Ehrhardtacb4a842009-11-30 12:16:48 +01006049 return factor;
6050}
6051
6052static void update_sysctl(void)
6053{
6054 unsigned int factor = get_update_sysctl_factor();
6055
Christian Ehrhardt0bcdcf22009-11-30 12:16:46 +01006056#define SET_SYSCTL(name) \
6057 (sysctl_##name = (factor) * normalized_sysctl_##name)
6058 SET_SYSCTL(sched_min_granularity);
6059 SET_SYSCTL(sched_latency);
6060 SET_SYSCTL(sched_wakeup_granularity);
Christian Ehrhardt0bcdcf22009-11-30 12:16:46 +01006061#undef SET_SYSCTL
6062}
6063
Ingo Molnar19978ca2007-11-09 22:39:38 +01006064static inline void sched_init_granularity(void)
6065{
Christian Ehrhardt0bcdcf22009-11-30 12:16:46 +01006066 update_sysctl();
Ingo Molnar19978ca2007-11-09 22:39:38 +01006067}
6068
Linus Torvalds1da177e2005-04-16 15:20:36 -07006069#ifdef CONFIG_SMP
KOSAKI Motohiro1e1b6c52011-05-19 15:08:58 +09006070void do_set_cpus_allowed(struct task_struct *p, const struct cpumask *new_mask)
6071{
6072 if (p->sched_class && p->sched_class->set_cpus_allowed)
6073 p->sched_class->set_cpus_allowed(p, new_mask);
6074 else {
6075 cpumask_copy(&p->cpus_allowed, new_mask);
6076 p->rt.nr_cpus_allowed = cpumask_weight(new_mask);
6077 }
6078}
6079
Linus Torvalds1da177e2005-04-16 15:20:36 -07006080/*
6081 * This is how migration works:
6082 *
Tejun Heo969c7922010-05-06 18:49:21 +02006083 * 1) we invoke migration_cpu_stop() on the target CPU using
6084 * stop_one_cpu().
6085 * 2) stopper starts to run (implicitly forcing the migrated thread
6086 * off the CPU)
6087 * 3) it checks whether the migrated task is still in the wrong runqueue.
6088 * 4) if it's in the wrong runqueue then the migration thread removes
Linus Torvalds1da177e2005-04-16 15:20:36 -07006089 * it and puts it into the right queue.
Tejun Heo969c7922010-05-06 18:49:21 +02006090 * 5) stopper completes and stop_one_cpu() returns and the migration
6091 * is done.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006092 */
6093
6094/*
6095 * Change a given task's CPU affinity. Migrate the thread to a
6096 * proper CPU and schedule it away if the CPU it's executing on
6097 * is removed from the allowed bitmask.
6098 *
6099 * NOTE: the caller must have a valid reference to the task, the
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01006100 * task must not exit() & deallocate itself prematurely. The
Linus Torvalds1da177e2005-04-16 15:20:36 -07006101 * call is not atomic; no spinlocks may be held.
6102 */
Rusty Russell96f874e2008-11-25 02:35:14 +10306103int set_cpus_allowed_ptr(struct task_struct *p, const struct cpumask *new_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006104{
6105 unsigned long flags;
Ingo Molnar70b97a72006-07-03 00:25:42 -07006106 struct rq *rq;
Tejun Heo969c7922010-05-06 18:49:21 +02006107 unsigned int dest_cpu;
Ingo Molnar48f24c42006-07-03 00:25:40 -07006108 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006109
6110 rq = task_rq_lock(p, &flags);
Peter Zijlstrae2912002009-12-16 18:04:36 +01006111
Yong Zhangdb44fc02011-05-09 22:07:05 +08006112 if (cpumask_equal(&p->cpus_allowed, new_mask))
6113 goto out;
6114
Peter Zijlstra6ad4c182009-11-25 13:31:39 +01006115 if (!cpumask_intersects(new_mask, cpu_active_mask)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006116 ret = -EINVAL;
6117 goto out;
6118 }
6119
Yong Zhangdb44fc02011-05-09 22:07:05 +08006120 if (unlikely((p->flags & PF_THREAD_BOUND) && p != current)) {
David Rientjes9985b0b2008-06-05 12:57:11 -07006121 ret = -EINVAL;
6122 goto out;
6123 }
6124
KOSAKI Motohiro1e1b6c52011-05-19 15:08:58 +09006125 do_set_cpus_allowed(p, new_mask);
Gregory Haskins73fe6aa2008-01-25 21:08:07 +01006126
Linus Torvalds1da177e2005-04-16 15:20:36 -07006127 /* Can the task run on the task's current CPU? If so, we're done */
Rusty Russell96f874e2008-11-25 02:35:14 +10306128 if (cpumask_test_cpu(task_cpu(p), new_mask))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006129 goto out;
6130
Tejun Heo969c7922010-05-06 18:49:21 +02006131 dest_cpu = cpumask_any_and(cpu_active_mask, new_mask);
Peter Zijlstrabd8e7dd2011-04-05 17:23:59 +02006132 if (p->on_rq) {
Tejun Heo969c7922010-05-06 18:49:21 +02006133 struct migration_arg arg = { p, dest_cpu };
Linus Torvalds1da177e2005-04-16 15:20:36 -07006134 /* Need help from migration thread: drop lock and wait. */
Peter Zijlstra0122ec52011-04-05 17:23:51 +02006135 task_rq_unlock(rq, p, &flags);
Tejun Heo969c7922010-05-06 18:49:21 +02006136 stop_one_cpu(cpu_of(rq), migration_cpu_stop, &arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006137 tlb_migrate_finish(p->mm);
6138 return 0;
6139 }
6140out:
Peter Zijlstra0122ec52011-04-05 17:23:51 +02006141 task_rq_unlock(rq, p, &flags);
Ingo Molnar48f24c42006-07-03 00:25:40 -07006142
Linus Torvalds1da177e2005-04-16 15:20:36 -07006143 return ret;
6144}
Mike Traviscd8ba7c2008-03-26 14:23:49 -07006145EXPORT_SYMBOL_GPL(set_cpus_allowed_ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006146
6147/*
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01006148 * Move (not current) task off this cpu, onto dest cpu. We're doing
Linus Torvalds1da177e2005-04-16 15:20:36 -07006149 * this because either it can't run here any more (set_cpus_allowed()
6150 * away from this CPU, or CPU going down), or because we're
6151 * attempting to rebalance this task on exec (sched_exec).
6152 *
6153 * So we race with normal scheduler movements, but that's OK, as long
6154 * as the task is no longer on this CPU.
Kirill Korotaevefc30812006-06-27 02:54:32 -07006155 *
6156 * Returns non-zero if task was successfully migrated.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006157 */
Kirill Korotaevefc30812006-06-27 02:54:32 -07006158static int __migrate_task(struct task_struct *p, int src_cpu, int dest_cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006159{
Ingo Molnar70b97a72006-07-03 00:25:42 -07006160 struct rq *rq_dest, *rq_src;
Peter Zijlstrae2912002009-12-16 18:04:36 +01006161 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006162
Max Krasnyanskye761b772008-07-15 04:43:49 -07006163 if (unlikely(!cpu_active(dest_cpu)))
Kirill Korotaevefc30812006-06-27 02:54:32 -07006164 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006165
6166 rq_src = cpu_rq(src_cpu);
6167 rq_dest = cpu_rq(dest_cpu);
6168
Peter Zijlstra0122ec52011-04-05 17:23:51 +02006169 raw_spin_lock(&p->pi_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006170 double_rq_lock(rq_src, rq_dest);
6171 /* Already moved. */
6172 if (task_cpu(p) != src_cpu)
Linus Torvaldsb1e38732008-07-10 11:25:03 -07006173 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006174 /* Affinity changed (again). */
Rusty Russell96f874e2008-11-25 02:35:14 +10306175 if (!cpumask_test_cpu(dest_cpu, &p->cpus_allowed))
Linus Torvaldsb1e38732008-07-10 11:25:03 -07006176 goto fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006177
Peter Zijlstrae2912002009-12-16 18:04:36 +01006178 /*
6179 * If we're not on a rq, the next wake-up will ensure we're
6180 * placed properly.
6181 */
Peter Zijlstrafd2f4412011-04-05 17:23:44 +02006182 if (p->on_rq) {
Ingo Molnar2e1cb742007-08-09 11:16:49 +02006183 deactivate_task(rq_src, p, 0);
Peter Zijlstrae2912002009-12-16 18:04:36 +01006184 set_task_cpu(p, dest_cpu);
Ingo Molnardd41f592007-07-09 18:51:59 +02006185 activate_task(rq_dest, p, 0);
Peter Zijlstra15afe092008-09-20 23:38:02 +02006186 check_preempt_curr(rq_dest, p, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006187 }
Linus Torvaldsb1e38732008-07-10 11:25:03 -07006188done:
Kirill Korotaevefc30812006-06-27 02:54:32 -07006189 ret = 1;
Linus Torvaldsb1e38732008-07-10 11:25:03 -07006190fail:
Linus Torvalds1da177e2005-04-16 15:20:36 -07006191 double_rq_unlock(rq_src, rq_dest);
Peter Zijlstra0122ec52011-04-05 17:23:51 +02006192 raw_spin_unlock(&p->pi_lock);
Kirill Korotaevefc30812006-06-27 02:54:32 -07006193 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006194}
6195
6196/*
Tejun Heo969c7922010-05-06 18:49:21 +02006197 * migration_cpu_stop - this will be executed by a highprio stopper thread
6198 * and performs thread migration by bumping thread off CPU then
6199 * 'pushing' onto another runqueue.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006200 */
Tejun Heo969c7922010-05-06 18:49:21 +02006201static int migration_cpu_stop(void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006202{
Tejun Heo969c7922010-05-06 18:49:21 +02006203 struct migration_arg *arg = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006204
Tejun Heo969c7922010-05-06 18:49:21 +02006205 /*
6206 * The original target cpu might have gone down and we might
6207 * be on another cpu but it doesn't matter.
6208 */
6209 local_irq_disable();
6210 __migrate_task(arg->task, raw_smp_processor_id(), arg->dest_cpu);
6211 local_irq_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006212 return 0;
6213}
6214
6215#ifdef CONFIG_HOTPLUG_CPU
Linus Torvalds1da177e2005-04-16 15:20:36 -07006216
Ingo Molnar48f24c42006-07-03 00:25:40 -07006217/*
6218 * Ensures that the idle task is using init_mm right before its cpu goes
Linus Torvalds1da177e2005-04-16 15:20:36 -07006219 * offline.
6220 */
6221void idle_task_exit(void)
6222{
6223 struct mm_struct *mm = current->active_mm;
6224
6225 BUG_ON(cpu_online(smp_processor_id()));
6226
6227 if (mm != &init_mm)
6228 switch_mm(mm, &init_mm, current);
6229 mmdrop(mm);
6230}
6231
Peter Zijlstra48c5cca2010-11-13 19:32:29 +01006232/*
6233 * While a dead CPU has no uninterruptible tasks queued at this point,
6234 * it might still have a nonzero ->nr_uninterruptible counter, because
6235 * for performance reasons the counter is not stricly tracking tasks to
6236 * their home CPUs. So we just add the counter to another CPU's counter,
6237 * to keep the global sum constant after CPU-down:
6238 */
6239static void migrate_nr_uninterruptible(struct rq *rq_src)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006240{
Peter Zijlstra48c5cca2010-11-13 19:32:29 +01006241 struct rq *rq_dest = cpu_rq(cpumask_any(cpu_active_mask));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006242
Peter Zijlstra48c5cca2010-11-13 19:32:29 +01006243 rq_dest->nr_uninterruptible += rq_src->nr_uninterruptible;
6244 rq_src->nr_uninterruptible = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006245}
Thomas Gleixnerdce48a82009-04-11 10:43:41 +02006246
6247/*
6248 * remove the tasks which were accounted by rq from calc_load_tasks.
6249 */
6250static void calc_global_load_remove(struct rq *rq)
6251{
6252 atomic_long_sub(rq->calc_load_active, &calc_load_tasks);
Thomas Gleixnera468d382009-07-17 14:15:46 +02006253 rq->calc_load_active = 0;
Thomas Gleixnerdce48a82009-04-11 10:43:41 +02006254}
Peter Zijlstra48c5cca2010-11-13 19:32:29 +01006255
6256/*
6257 * Migrate all tasks from the rq, sleeping tasks will be migrated by
6258 * try_to_wake_up()->select_task_rq().
6259 *
6260 * Called with rq->lock held even though we'er in stop_machine() and
6261 * there's no concurrency possible, we hold the required locks anyway
6262 * because of lock validation efforts.
6263 */
6264static void migrate_tasks(unsigned int dead_cpu)
6265{
6266 struct rq *rq = cpu_rq(dead_cpu);
6267 struct task_struct *next, *stop = rq->stop;
6268 int dest_cpu;
6269
6270 /*
6271 * Fudge the rq selection such that the below task selection loop
6272 * doesn't get stuck on the currently eligible stop task.
6273 *
6274 * We're currently inside stop_machine() and the rq is either stuck
6275 * in the stop_machine_cpu_stop() loop, or we're executing this code,
6276 * either way we should never end up calling schedule() until we're
6277 * done here.
6278 */
6279 rq->stop = NULL;
6280
6281 for ( ; ; ) {
6282 /*
6283 * There's this thread running, bail when that's the only
6284 * remaining thread.
6285 */
6286 if (rq->nr_running == 1)
6287 break;
6288
6289 next = pick_next_task(rq);
6290 BUG_ON(!next);
6291 next->sched_class->put_prev_task(rq, next);
6292
6293 /* Find suitable destination for @next, with force if needed. */
6294 dest_cpu = select_fallback_rq(dead_cpu, next);
6295 raw_spin_unlock(&rq->lock);
6296
6297 __migrate_task(next, dead_cpu, dest_cpu);
6298
6299 raw_spin_lock(&rq->lock);
6300 }
6301
6302 rq->stop = stop;
6303}
6304
Linus Torvalds1da177e2005-04-16 15:20:36 -07006305#endif /* CONFIG_HOTPLUG_CPU */
6306
Nick Piggine692ab52007-07-26 13:40:43 +02006307#if defined(CONFIG_SCHED_DEBUG) && defined(CONFIG_SYSCTL)
6308
6309static struct ctl_table sd_ctl_dir[] = {
Alexey Dobriyane0361852007-08-09 11:16:46 +02006310 {
6311 .procname = "sched_domain",
Eric W. Biedermanc57baf12007-08-23 15:18:02 +02006312 .mode = 0555,
Alexey Dobriyane0361852007-08-09 11:16:46 +02006313 },
Eric W. Biederman56992302009-11-05 15:38:40 -08006314 {}
Nick Piggine692ab52007-07-26 13:40:43 +02006315};
6316
6317static struct ctl_table sd_ctl_root[] = {
Alexey Dobriyane0361852007-08-09 11:16:46 +02006318 {
6319 .procname = "kernel",
Eric W. Biedermanc57baf12007-08-23 15:18:02 +02006320 .mode = 0555,
Alexey Dobriyane0361852007-08-09 11:16:46 +02006321 .child = sd_ctl_dir,
6322 },
Eric W. Biederman56992302009-11-05 15:38:40 -08006323 {}
Nick Piggine692ab52007-07-26 13:40:43 +02006324};
6325
6326static struct ctl_table *sd_alloc_ctl_entry(int n)
6327{
6328 struct ctl_table *entry =
Milton Miller5cf9f062007-10-15 17:00:19 +02006329 kcalloc(n, sizeof(struct ctl_table), GFP_KERNEL);
Nick Piggine692ab52007-07-26 13:40:43 +02006330
Nick Piggine692ab52007-07-26 13:40:43 +02006331 return entry;
6332}
6333
Milton Miller6382bc92007-10-15 17:00:19 +02006334static void sd_free_ctl_entry(struct ctl_table **tablep)
6335{
Milton Millercd790072007-10-17 16:55:11 +02006336 struct ctl_table *entry;
Milton Miller6382bc92007-10-15 17:00:19 +02006337
Milton Millercd790072007-10-17 16:55:11 +02006338 /*
6339 * In the intermediate directories, both the child directory and
6340 * procname are dynamically allocated and could fail but the mode
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01006341 * will always be set. In the lowest directory the names are
Milton Millercd790072007-10-17 16:55:11 +02006342 * static strings and all have proc handlers.
6343 */
6344 for (entry = *tablep; entry->mode; entry++) {
Milton Miller6382bc92007-10-15 17:00:19 +02006345 if (entry->child)
6346 sd_free_ctl_entry(&entry->child);
Milton Millercd790072007-10-17 16:55:11 +02006347 if (entry->proc_handler == NULL)
6348 kfree(entry->procname);
6349 }
Milton Miller6382bc92007-10-15 17:00:19 +02006350
6351 kfree(*tablep);
6352 *tablep = NULL;
6353}
6354
Nick Piggine692ab52007-07-26 13:40:43 +02006355static void
Alexey Dobriyane0361852007-08-09 11:16:46 +02006356set_table_entry(struct ctl_table *entry,
Nick Piggine692ab52007-07-26 13:40:43 +02006357 const char *procname, void *data, int maxlen,
6358 mode_t mode, proc_handler *proc_handler)
6359{
Nick Piggine692ab52007-07-26 13:40:43 +02006360 entry->procname = procname;
6361 entry->data = data;
6362 entry->maxlen = maxlen;
6363 entry->mode = mode;
6364 entry->proc_handler = proc_handler;
6365}
6366
6367static struct ctl_table *
6368sd_alloc_ctl_domain_table(struct sched_domain *sd)
6369{
Ingo Molnara5d8c342008-10-09 11:35:51 +02006370 struct ctl_table *table = sd_alloc_ctl_entry(13);
Nick Piggine692ab52007-07-26 13:40:43 +02006371
Milton Millerad1cdc12007-10-15 17:00:19 +02006372 if (table == NULL)
6373 return NULL;
6374
Alexey Dobriyane0361852007-08-09 11:16:46 +02006375 set_table_entry(&table[0], "min_interval", &sd->min_interval,
Nick Piggine692ab52007-07-26 13:40:43 +02006376 sizeof(long), 0644, proc_doulongvec_minmax);
Alexey Dobriyane0361852007-08-09 11:16:46 +02006377 set_table_entry(&table[1], "max_interval", &sd->max_interval,
Nick Piggine692ab52007-07-26 13:40:43 +02006378 sizeof(long), 0644, proc_doulongvec_minmax);
Alexey Dobriyane0361852007-08-09 11:16:46 +02006379 set_table_entry(&table[2], "busy_idx", &sd->busy_idx,
Nick Piggine692ab52007-07-26 13:40:43 +02006380 sizeof(int), 0644, proc_dointvec_minmax);
Alexey Dobriyane0361852007-08-09 11:16:46 +02006381 set_table_entry(&table[3], "idle_idx", &sd->idle_idx,
Nick Piggine692ab52007-07-26 13:40:43 +02006382 sizeof(int), 0644, proc_dointvec_minmax);
Alexey Dobriyane0361852007-08-09 11:16:46 +02006383 set_table_entry(&table[4], "newidle_idx", &sd->newidle_idx,
Nick Piggine692ab52007-07-26 13:40:43 +02006384 sizeof(int), 0644, proc_dointvec_minmax);
Alexey Dobriyane0361852007-08-09 11:16:46 +02006385 set_table_entry(&table[5], "wake_idx", &sd->wake_idx,
Nick Piggine692ab52007-07-26 13:40:43 +02006386 sizeof(int), 0644, proc_dointvec_minmax);
Alexey Dobriyane0361852007-08-09 11:16:46 +02006387 set_table_entry(&table[6], "forkexec_idx", &sd->forkexec_idx,
Nick Piggine692ab52007-07-26 13:40:43 +02006388 sizeof(int), 0644, proc_dointvec_minmax);
Alexey Dobriyane0361852007-08-09 11:16:46 +02006389 set_table_entry(&table[7], "busy_factor", &sd->busy_factor,
Nick Piggine692ab52007-07-26 13:40:43 +02006390 sizeof(int), 0644, proc_dointvec_minmax);
Alexey Dobriyane0361852007-08-09 11:16:46 +02006391 set_table_entry(&table[8], "imbalance_pct", &sd->imbalance_pct,
Nick Piggine692ab52007-07-26 13:40:43 +02006392 sizeof(int), 0644, proc_dointvec_minmax);
Zou Nan haiace8b3d2007-10-15 17:00:14 +02006393 set_table_entry(&table[9], "cache_nice_tries",
Nick Piggine692ab52007-07-26 13:40:43 +02006394 &sd->cache_nice_tries,
6395 sizeof(int), 0644, proc_dointvec_minmax);
Zou Nan haiace8b3d2007-10-15 17:00:14 +02006396 set_table_entry(&table[10], "flags", &sd->flags,
Nick Piggine692ab52007-07-26 13:40:43 +02006397 sizeof(int), 0644, proc_dointvec_minmax);
Ingo Molnara5d8c342008-10-09 11:35:51 +02006398 set_table_entry(&table[11], "name", sd->name,
6399 CORENAME_MAX_SIZE, 0444, proc_dostring);
6400 /* &table[12] is terminator */
Nick Piggine692ab52007-07-26 13:40:43 +02006401
6402 return table;
6403}
6404
Ingo Molnar9a4e7152007-11-28 15:52:56 +01006405static ctl_table *sd_alloc_ctl_cpu_table(int cpu)
Nick Piggine692ab52007-07-26 13:40:43 +02006406{
6407 struct ctl_table *entry, *table;
6408 struct sched_domain *sd;
6409 int domain_num = 0, i;
6410 char buf[32];
6411
6412 for_each_domain(cpu, sd)
6413 domain_num++;
6414 entry = table = sd_alloc_ctl_entry(domain_num + 1);
Milton Millerad1cdc12007-10-15 17:00:19 +02006415 if (table == NULL)
6416 return NULL;
Nick Piggine692ab52007-07-26 13:40:43 +02006417
6418 i = 0;
6419 for_each_domain(cpu, sd) {
6420 snprintf(buf, 32, "domain%d", i);
Nick Piggine692ab52007-07-26 13:40:43 +02006421 entry->procname = kstrdup(buf, GFP_KERNEL);
Eric W. Biedermanc57baf12007-08-23 15:18:02 +02006422 entry->mode = 0555;
Nick Piggine692ab52007-07-26 13:40:43 +02006423 entry->child = sd_alloc_ctl_domain_table(sd);
6424 entry++;
6425 i++;
6426 }
6427 return table;
6428}
6429
6430static struct ctl_table_header *sd_sysctl_header;
Milton Miller6382bc92007-10-15 17:00:19 +02006431static void register_sched_domain_sysctl(void)
Nick Piggine692ab52007-07-26 13:40:43 +02006432{
Peter Zijlstra6ad4c182009-11-25 13:31:39 +01006433 int i, cpu_num = num_possible_cpus();
Nick Piggine692ab52007-07-26 13:40:43 +02006434 struct ctl_table *entry = sd_alloc_ctl_entry(cpu_num + 1);
6435 char buf[32];
6436
Milton Miller73785472007-10-24 18:23:48 +02006437 WARN_ON(sd_ctl_dir[0].child);
6438 sd_ctl_dir[0].child = entry;
6439
Milton Millerad1cdc12007-10-15 17:00:19 +02006440 if (entry == NULL)
6441 return;
6442
Peter Zijlstra6ad4c182009-11-25 13:31:39 +01006443 for_each_possible_cpu(i) {
Nick Piggine692ab52007-07-26 13:40:43 +02006444 snprintf(buf, 32, "cpu%d", i);
Nick Piggine692ab52007-07-26 13:40:43 +02006445 entry->procname = kstrdup(buf, GFP_KERNEL);
Eric W. Biedermanc57baf12007-08-23 15:18:02 +02006446 entry->mode = 0555;
Nick Piggine692ab52007-07-26 13:40:43 +02006447 entry->child = sd_alloc_ctl_cpu_table(i);
Milton Miller97b6ea72007-10-15 17:00:19 +02006448 entry++;
Nick Piggine692ab52007-07-26 13:40:43 +02006449 }
Milton Miller73785472007-10-24 18:23:48 +02006450
6451 WARN_ON(sd_sysctl_header);
Nick Piggine692ab52007-07-26 13:40:43 +02006452 sd_sysctl_header = register_sysctl_table(sd_ctl_root);
6453}
Milton Miller6382bc92007-10-15 17:00:19 +02006454
Milton Miller73785472007-10-24 18:23:48 +02006455/* may be called multiple times per register */
Milton Miller6382bc92007-10-15 17:00:19 +02006456static void unregister_sched_domain_sysctl(void)
6457{
Milton Miller73785472007-10-24 18:23:48 +02006458 if (sd_sysctl_header)
6459 unregister_sysctl_table(sd_sysctl_header);
Milton Miller6382bc92007-10-15 17:00:19 +02006460 sd_sysctl_header = NULL;
Milton Miller73785472007-10-24 18:23:48 +02006461 if (sd_ctl_dir[0].child)
6462 sd_free_ctl_entry(&sd_ctl_dir[0].child);
Milton Miller6382bc92007-10-15 17:00:19 +02006463}
Nick Piggine692ab52007-07-26 13:40:43 +02006464#else
Milton Miller6382bc92007-10-15 17:00:19 +02006465static void register_sched_domain_sysctl(void)
6466{
6467}
6468static void unregister_sched_domain_sysctl(void)
Nick Piggine692ab52007-07-26 13:40:43 +02006469{
6470}
6471#endif
6472
Gregory Haskins1f11eb62008-06-04 15:04:05 -04006473static void set_rq_online(struct rq *rq)
6474{
6475 if (!rq->online) {
6476 const struct sched_class *class;
6477
Rusty Russellc6c49272008-11-25 02:35:05 +10306478 cpumask_set_cpu(rq->cpu, rq->rd->online);
Gregory Haskins1f11eb62008-06-04 15:04:05 -04006479 rq->online = 1;
6480
6481 for_each_class(class) {
6482 if (class->rq_online)
6483 class->rq_online(rq);
6484 }
6485 }
6486}
6487
6488static void set_rq_offline(struct rq *rq)
6489{
6490 if (rq->online) {
6491 const struct sched_class *class;
6492
6493 for_each_class(class) {
6494 if (class->rq_offline)
6495 class->rq_offline(rq);
6496 }
6497
Rusty Russellc6c49272008-11-25 02:35:05 +10306498 cpumask_clear_cpu(rq->cpu, rq->rd->online);
Gregory Haskins1f11eb62008-06-04 15:04:05 -04006499 rq->online = 0;
6500 }
6501}
6502
Linus Torvalds1da177e2005-04-16 15:20:36 -07006503/*
6504 * migration_call - callback that gets triggered when a CPU is added.
6505 * Here we can start up the necessary migration thread for the new CPU.
6506 */
Ingo Molnar48f24c42006-07-03 00:25:40 -07006507static int __cpuinit
6508migration_call(struct notifier_block *nfb, unsigned long action, void *hcpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006509{
Ingo Molnar48f24c42006-07-03 00:25:40 -07006510 int cpu = (long)hcpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006511 unsigned long flags;
Tejun Heo969c7922010-05-06 18:49:21 +02006512 struct rq *rq = cpu_rq(cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006513
Peter Zijlstra48c5cca2010-11-13 19:32:29 +01006514 switch (action & ~CPU_TASKS_FROZEN) {
Gautham R Shenoy5be93612007-05-09 02:34:04 -07006515
Linus Torvalds1da177e2005-04-16 15:20:36 -07006516 case CPU_UP_PREPARE:
Thomas Gleixnera468d382009-07-17 14:15:46 +02006517 rq->calc_load_update = calc_load_update;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006518 break;
Ingo Molnar48f24c42006-07-03 00:25:40 -07006519
Linus Torvalds1da177e2005-04-16 15:20:36 -07006520 case CPU_ONLINE:
Gregory Haskins1f94ef52008-03-10 16:52:41 -04006521 /* Update our root-domain */
Thomas Gleixner05fa7852009-11-17 14:28:38 +01006522 raw_spin_lock_irqsave(&rq->lock, flags);
Gregory Haskins1f94ef52008-03-10 16:52:41 -04006523 if (rq->rd) {
Rusty Russellc6c49272008-11-25 02:35:05 +10306524 BUG_ON(!cpumask_test_cpu(cpu, rq->rd->span));
Gregory Haskins1f11eb62008-06-04 15:04:05 -04006525
6526 set_rq_online(rq);
Gregory Haskins1f94ef52008-03-10 16:52:41 -04006527 }
Thomas Gleixner05fa7852009-11-17 14:28:38 +01006528 raw_spin_unlock_irqrestore(&rq->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006529 break;
Ingo Molnar48f24c42006-07-03 00:25:40 -07006530
Linus Torvalds1da177e2005-04-16 15:20:36 -07006531#ifdef CONFIG_HOTPLUG_CPU
Gregory Haskins08f503b2008-03-10 17:59:11 -04006532 case CPU_DYING:
Peter Zijlstra317f3942011-04-05 17:23:58 +02006533 sched_ttwu_pending();
Gregory Haskins57d885f2008-01-25 21:08:18 +01006534 /* Update our root-domain */
Thomas Gleixner05fa7852009-11-17 14:28:38 +01006535 raw_spin_lock_irqsave(&rq->lock, flags);
Gregory Haskins57d885f2008-01-25 21:08:18 +01006536 if (rq->rd) {
Rusty Russellc6c49272008-11-25 02:35:05 +10306537 BUG_ON(!cpumask_test_cpu(cpu, rq->rd->span));
Gregory Haskins1f11eb62008-06-04 15:04:05 -04006538 set_rq_offline(rq);
Gregory Haskins57d885f2008-01-25 21:08:18 +01006539 }
Peter Zijlstra48c5cca2010-11-13 19:32:29 +01006540 migrate_tasks(cpu);
6541 BUG_ON(rq->nr_running != 1); /* the migration thread */
Thomas Gleixner05fa7852009-11-17 14:28:38 +01006542 raw_spin_unlock_irqrestore(&rq->lock, flags);
Peter Zijlstra48c5cca2010-11-13 19:32:29 +01006543
6544 migrate_nr_uninterruptible(rq);
6545 calc_global_load_remove(rq);
Gregory Haskins57d885f2008-01-25 21:08:18 +01006546 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006547#endif
6548 }
Peter Zijlstra49c022e2011-04-05 10:14:25 +02006549
6550 update_max_interval();
6551
Linus Torvalds1da177e2005-04-16 15:20:36 -07006552 return NOTIFY_OK;
6553}
6554
Paul Mackerrasf38b0822009-06-02 21:05:16 +10006555/*
6556 * Register at high priority so that task migration (migrate_all_tasks)
6557 * happens before everything else. This has to be lower priority than
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006558 * the notifier in the perf_event subsystem, though.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006559 */
Chandra Seetharaman26c21432006-06-27 02:54:10 -07006560static struct notifier_block __cpuinitdata migration_notifier = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006561 .notifier_call = migration_call,
Tejun Heo50a323b2010-06-08 21:40:36 +02006562 .priority = CPU_PRI_MIGRATION,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006563};
6564
Tejun Heo3a101d02010-06-08 21:40:36 +02006565static int __cpuinit sched_cpu_active(struct notifier_block *nfb,
6566 unsigned long action, void *hcpu)
6567{
6568 switch (action & ~CPU_TASKS_FROZEN) {
6569 case CPU_ONLINE:
6570 case CPU_DOWN_FAILED:
6571 set_cpu_active((long)hcpu, true);
6572 return NOTIFY_OK;
6573 default:
6574 return NOTIFY_DONE;
6575 }
6576}
6577
6578static int __cpuinit sched_cpu_inactive(struct notifier_block *nfb,
6579 unsigned long action, void *hcpu)
6580{
6581 switch (action & ~CPU_TASKS_FROZEN) {
6582 case CPU_DOWN_PREPARE:
6583 set_cpu_active((long)hcpu, false);
6584 return NOTIFY_OK;
6585 default:
6586 return NOTIFY_DONE;
6587 }
6588}
6589
Eduard - Gabriel Munteanu7babe8d2008-07-25 19:45:11 -07006590static int __init migration_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006591{
6592 void *cpu = (void *)(long)smp_processor_id();
Akinobu Mita07dccf32006-09-29 02:00:22 -07006593 int err;
Ingo Molnar48f24c42006-07-03 00:25:40 -07006594
Tejun Heo3a101d02010-06-08 21:40:36 +02006595 /* Initialize migration for the boot CPU */
Akinobu Mita07dccf32006-09-29 02:00:22 -07006596 err = migration_call(&migration_notifier, CPU_UP_PREPARE, cpu);
6597 BUG_ON(err == NOTIFY_BAD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006598 migration_call(&migration_notifier, CPU_ONLINE, cpu);
6599 register_cpu_notifier(&migration_notifier);
Eduard - Gabriel Munteanu7babe8d2008-07-25 19:45:11 -07006600
Tejun Heo3a101d02010-06-08 21:40:36 +02006601 /* Register cpu active notifiers */
6602 cpu_notifier(sched_cpu_active, CPU_PRI_SCHED_ACTIVE);
6603 cpu_notifier(sched_cpu_inactive, CPU_PRI_SCHED_INACTIVE);
6604
Thomas Gleixnera004cd42009-07-21 09:54:05 +02006605 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006606}
Eduard - Gabriel Munteanu7babe8d2008-07-25 19:45:11 -07006607early_initcall(migration_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006608#endif
6609
6610#ifdef CONFIG_SMP
Christoph Lameter476f3532007-05-06 14:48:58 -07006611
Peter Zijlstra4cb98832011-04-07 14:09:58 +02006612static cpumask_var_t sched_domains_tmpmask; /* sched_domains_mutex */
6613
Ingo Molnar3e9830d2007-10-15 17:00:13 +02006614#ifdef CONFIG_SCHED_DEBUG
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02006615
Mike Travisf6630112009-11-17 18:22:15 -06006616static __read_mostly int sched_domain_debug_enabled;
6617
6618static int __init sched_domain_debug_setup(char *str)
6619{
6620 sched_domain_debug_enabled = 1;
6621
6622 return 0;
6623}
6624early_param("sched_debug", sched_domain_debug_setup);
6625
Mike Travis7c16ec52008-04-04 18:11:11 -07006626static int sched_domain_debug_one(struct sched_domain *sd, int cpu, int level,
Rusty Russell96f874e2008-11-25 02:35:14 +10306627 struct cpumask *groupmask)
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02006628{
6629 struct sched_group *group = sd->groups;
Mike Travis434d53b2008-04-04 18:11:04 -07006630 char str[256];
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02006631
Rusty Russell968ea6d2008-12-13 21:55:51 +10306632 cpulist_scnprintf(str, sizeof(str), sched_domain_span(sd));
Rusty Russell96f874e2008-11-25 02:35:14 +10306633 cpumask_clear(groupmask);
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02006634
6635 printk(KERN_DEBUG "%*s domain %d: ", level, "", level);
6636
6637 if (!(sd->flags & SD_LOAD_BALANCE)) {
Peter Zijlstra3df0fc52009-12-20 14:23:57 +01006638 printk("does not load-balance\n");
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02006639 if (sd->parent)
Peter Zijlstra3df0fc52009-12-20 14:23:57 +01006640 printk(KERN_ERR "ERROR: !SD_LOAD_BALANCE domain"
6641 " has parent");
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02006642 return -1;
6643 }
6644
Peter Zijlstra3df0fc52009-12-20 14:23:57 +01006645 printk(KERN_CONT "span %s level %s\n", str, sd->name);
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02006646
Rusty Russell758b2cd2008-11-25 02:35:04 +10306647 if (!cpumask_test_cpu(cpu, sched_domain_span(sd))) {
Peter Zijlstra3df0fc52009-12-20 14:23:57 +01006648 printk(KERN_ERR "ERROR: domain->span does not contain "
6649 "CPU%d\n", cpu);
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02006650 }
Rusty Russell758b2cd2008-11-25 02:35:04 +10306651 if (!cpumask_test_cpu(cpu, sched_group_cpus(group))) {
Peter Zijlstra3df0fc52009-12-20 14:23:57 +01006652 printk(KERN_ERR "ERROR: domain->groups does not contain"
6653 " CPU%d\n", cpu);
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02006654 }
6655
6656 printk(KERN_DEBUG "%*s groups:", level + 1, "");
6657 do {
6658 if (!group) {
Peter Zijlstra3df0fc52009-12-20 14:23:57 +01006659 printk("\n");
6660 printk(KERN_ERR "ERROR: group is NULL\n");
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02006661 break;
6662 }
6663
Peter Zijlstra9c3f75c2011-07-14 13:00:06 +02006664 if (!group->sgp->power) {
Peter Zijlstra3df0fc52009-12-20 14:23:57 +01006665 printk(KERN_CONT "\n");
6666 printk(KERN_ERR "ERROR: domain->cpu_power not "
6667 "set\n");
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02006668 break;
6669 }
6670
Rusty Russell758b2cd2008-11-25 02:35:04 +10306671 if (!cpumask_weight(sched_group_cpus(group))) {
Peter Zijlstra3df0fc52009-12-20 14:23:57 +01006672 printk(KERN_CONT "\n");
6673 printk(KERN_ERR "ERROR: empty group\n");
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02006674 break;
6675 }
6676
Rusty Russell758b2cd2008-11-25 02:35:04 +10306677 if (cpumask_intersects(groupmask, sched_group_cpus(group))) {
Peter Zijlstra3df0fc52009-12-20 14:23:57 +01006678 printk(KERN_CONT "\n");
6679 printk(KERN_ERR "ERROR: repeated CPUs\n");
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02006680 break;
6681 }
6682
Rusty Russell758b2cd2008-11-25 02:35:04 +10306683 cpumask_or(groupmask, groupmask, sched_group_cpus(group));
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02006684
Rusty Russell968ea6d2008-12-13 21:55:51 +10306685 cpulist_scnprintf(str, sizeof(str), sched_group_cpus(group));
Gautham R Shenoy381512c2009-04-14 09:09:36 +05306686
Peter Zijlstra3df0fc52009-12-20 14:23:57 +01006687 printk(KERN_CONT " %s", str);
Peter Zijlstra9c3f75c2011-07-14 13:00:06 +02006688 if (group->sgp->power != SCHED_POWER_SCALE) {
Peter Zijlstra3df0fc52009-12-20 14:23:57 +01006689 printk(KERN_CONT " (cpu_power = %d)",
Peter Zijlstra9c3f75c2011-07-14 13:00:06 +02006690 group->sgp->power);
Gautham R Shenoy381512c2009-04-14 09:09:36 +05306691 }
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02006692
6693 group = group->next;
6694 } while (group != sd->groups);
Peter Zijlstra3df0fc52009-12-20 14:23:57 +01006695 printk(KERN_CONT "\n");
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02006696
Rusty Russell758b2cd2008-11-25 02:35:04 +10306697 if (!cpumask_equal(sched_domain_span(sd), groupmask))
Peter Zijlstra3df0fc52009-12-20 14:23:57 +01006698 printk(KERN_ERR "ERROR: groups don't span domain->span\n");
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02006699
Rusty Russell758b2cd2008-11-25 02:35:04 +10306700 if (sd->parent &&
6701 !cpumask_subset(groupmask, sched_domain_span(sd->parent)))
Peter Zijlstra3df0fc52009-12-20 14:23:57 +01006702 printk(KERN_ERR "ERROR: parent span is not a superset "
6703 "of domain->span\n");
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02006704 return 0;
6705}
6706
Linus Torvalds1da177e2005-04-16 15:20:36 -07006707static void sched_domain_debug(struct sched_domain *sd, int cpu)
6708{
6709 int level = 0;
6710
Mike Travisf6630112009-11-17 18:22:15 -06006711 if (!sched_domain_debug_enabled)
6712 return;
6713
Nick Piggin41c7ce92005-06-25 14:57:24 -07006714 if (!sd) {
6715 printk(KERN_DEBUG "CPU%d attaching NULL sched-domain.\n", cpu);
6716 return;
6717 }
6718
Linus Torvalds1da177e2005-04-16 15:20:36 -07006719 printk(KERN_DEBUG "CPU%d attaching sched-domain:\n", cpu);
6720
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02006721 for (;;) {
Peter Zijlstra4cb98832011-04-07 14:09:58 +02006722 if (sched_domain_debug_one(sd, cpu, level, sched_domains_tmpmask))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006723 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006724 level++;
6725 sd = sd->parent;
Miguel Ojeda Sandonis33859f72006-12-10 02:20:38 -08006726 if (!sd)
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02006727 break;
6728 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006729}
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +02006730#else /* !CONFIG_SCHED_DEBUG */
Ingo Molnar48f24c42006-07-03 00:25:40 -07006731# define sched_domain_debug(sd, cpu) do { } while (0)
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +02006732#endif /* CONFIG_SCHED_DEBUG */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006733
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07006734static int sd_degenerate(struct sched_domain *sd)
Suresh Siddha245af2c2005-06-25 14:57:25 -07006735{
Rusty Russell758b2cd2008-11-25 02:35:04 +10306736 if (cpumask_weight(sched_domain_span(sd)) == 1)
Suresh Siddha245af2c2005-06-25 14:57:25 -07006737 return 1;
6738
6739 /* Following flags need at least 2 groups */
6740 if (sd->flags & (SD_LOAD_BALANCE |
6741 SD_BALANCE_NEWIDLE |
6742 SD_BALANCE_FORK |
Siddha, Suresh B89c47102006-10-03 01:14:09 -07006743 SD_BALANCE_EXEC |
6744 SD_SHARE_CPUPOWER |
6745 SD_SHARE_PKG_RESOURCES)) {
Suresh Siddha245af2c2005-06-25 14:57:25 -07006746 if (sd->groups != sd->groups->next)
6747 return 0;
6748 }
6749
6750 /* Following flags don't use groups */
Peter Zijlstrac88d5912009-09-10 13:50:02 +02006751 if (sd->flags & (SD_WAKE_AFFINE))
Suresh Siddha245af2c2005-06-25 14:57:25 -07006752 return 0;
6753
6754 return 1;
6755}
6756
Ingo Molnar48f24c42006-07-03 00:25:40 -07006757static int
6758sd_parent_degenerate(struct sched_domain *sd, struct sched_domain *parent)
Suresh Siddha245af2c2005-06-25 14:57:25 -07006759{
6760 unsigned long cflags = sd->flags, pflags = parent->flags;
6761
6762 if (sd_degenerate(parent))
6763 return 1;
6764
Rusty Russell758b2cd2008-11-25 02:35:04 +10306765 if (!cpumask_equal(sched_domain_span(sd), sched_domain_span(parent)))
Suresh Siddha245af2c2005-06-25 14:57:25 -07006766 return 0;
6767
Suresh Siddha245af2c2005-06-25 14:57:25 -07006768 /* Flags needing groups don't count if only 1 group in parent */
6769 if (parent->groups == parent->groups->next) {
6770 pflags &= ~(SD_LOAD_BALANCE |
6771 SD_BALANCE_NEWIDLE |
6772 SD_BALANCE_FORK |
Siddha, Suresh B89c47102006-10-03 01:14:09 -07006773 SD_BALANCE_EXEC |
6774 SD_SHARE_CPUPOWER |
6775 SD_SHARE_PKG_RESOURCES);
Ken Chen54364992008-12-07 18:47:37 -08006776 if (nr_node_ids == 1)
6777 pflags &= ~SD_SERIALIZE;
Suresh Siddha245af2c2005-06-25 14:57:25 -07006778 }
6779 if (~cflags & pflags)
6780 return 0;
6781
6782 return 1;
6783}
6784
Peter Zijlstradce840a2011-04-07 14:09:50 +02006785static void free_rootdomain(struct rcu_head *rcu)
Rusty Russellc6c49272008-11-25 02:35:05 +10306786{
Peter Zijlstradce840a2011-04-07 14:09:50 +02006787 struct root_domain *rd = container_of(rcu, struct root_domain, rcu);
Peter Zijlstra047106a2009-11-16 10:28:09 +01006788
Rusty Russell68e74562008-11-25 02:35:13 +10306789 cpupri_cleanup(&rd->cpupri);
Rusty Russellc6c49272008-11-25 02:35:05 +10306790 free_cpumask_var(rd->rto_mask);
6791 free_cpumask_var(rd->online);
6792 free_cpumask_var(rd->span);
6793 kfree(rd);
6794}
6795
Gregory Haskins57d885f2008-01-25 21:08:18 +01006796static void rq_attach_root(struct rq *rq, struct root_domain *rd)
6797{
Ingo Molnara0490fa2009-02-12 11:35:40 +01006798 struct root_domain *old_rd = NULL;
Gregory Haskins57d885f2008-01-25 21:08:18 +01006799 unsigned long flags;
Gregory Haskins57d885f2008-01-25 21:08:18 +01006800
Thomas Gleixner05fa7852009-11-17 14:28:38 +01006801 raw_spin_lock_irqsave(&rq->lock, flags);
Gregory Haskins57d885f2008-01-25 21:08:18 +01006802
6803 if (rq->rd) {
Ingo Molnara0490fa2009-02-12 11:35:40 +01006804 old_rd = rq->rd;
Gregory Haskins57d885f2008-01-25 21:08:18 +01006805
Rusty Russellc6c49272008-11-25 02:35:05 +10306806 if (cpumask_test_cpu(rq->cpu, old_rd->online))
Gregory Haskins1f11eb62008-06-04 15:04:05 -04006807 set_rq_offline(rq);
Gregory Haskins57d885f2008-01-25 21:08:18 +01006808
Rusty Russellc6c49272008-11-25 02:35:05 +10306809 cpumask_clear_cpu(rq->cpu, old_rd->span);
Gregory Haskinsdc938522008-01-25 21:08:26 +01006810
Ingo Molnara0490fa2009-02-12 11:35:40 +01006811 /*
6812 * If we dont want to free the old_rt yet then
6813 * set old_rd to NULL to skip the freeing later
6814 * in this function:
6815 */
6816 if (!atomic_dec_and_test(&old_rd->refcount))
6817 old_rd = NULL;
Gregory Haskins57d885f2008-01-25 21:08:18 +01006818 }
6819
6820 atomic_inc(&rd->refcount);
6821 rq->rd = rd;
6822
Rusty Russellc6c49272008-11-25 02:35:05 +10306823 cpumask_set_cpu(rq->cpu, rd->span);
Gregory Haskins00aec932009-07-30 10:57:23 -04006824 if (cpumask_test_cpu(rq->cpu, cpu_active_mask))
Gregory Haskins1f11eb62008-06-04 15:04:05 -04006825 set_rq_online(rq);
Gregory Haskins57d885f2008-01-25 21:08:18 +01006826
Thomas Gleixner05fa7852009-11-17 14:28:38 +01006827 raw_spin_unlock_irqrestore(&rq->lock, flags);
Ingo Molnara0490fa2009-02-12 11:35:40 +01006828
6829 if (old_rd)
Peter Zijlstradce840a2011-04-07 14:09:50 +02006830 call_rcu_sched(&old_rd->rcu, free_rootdomain);
Gregory Haskins57d885f2008-01-25 21:08:18 +01006831}
6832
Pekka Enberg68c38fc2010-07-15 23:18:22 +03006833static int init_rootdomain(struct root_domain *rd)
Gregory Haskins57d885f2008-01-25 21:08:18 +01006834{
6835 memset(rd, 0, sizeof(*rd));
6836
Pekka Enberg68c38fc2010-07-15 23:18:22 +03006837 if (!alloc_cpumask_var(&rd->span, GFP_KERNEL))
Li Zefan0c910d22009-01-06 17:39:06 +08006838 goto out;
Pekka Enberg68c38fc2010-07-15 23:18:22 +03006839 if (!alloc_cpumask_var(&rd->online, GFP_KERNEL))
Rusty Russellc6c49272008-11-25 02:35:05 +10306840 goto free_span;
Pekka Enberg68c38fc2010-07-15 23:18:22 +03006841 if (!alloc_cpumask_var(&rd->rto_mask, GFP_KERNEL))
Rusty Russellc6c49272008-11-25 02:35:05 +10306842 goto free_online;
Gregory Haskins6e0534f2008-05-12 21:21:01 +02006843
Pekka Enberg68c38fc2010-07-15 23:18:22 +03006844 if (cpupri_init(&rd->cpupri) != 0)
Rusty Russell68e74562008-11-25 02:35:13 +10306845 goto free_rto_mask;
Rusty Russellc6c49272008-11-25 02:35:05 +10306846 return 0;
6847
Rusty Russell68e74562008-11-25 02:35:13 +10306848free_rto_mask:
6849 free_cpumask_var(rd->rto_mask);
Rusty Russellc6c49272008-11-25 02:35:05 +10306850free_online:
6851 free_cpumask_var(rd->online);
6852free_span:
6853 free_cpumask_var(rd->span);
Li Zefan0c910d22009-01-06 17:39:06 +08006854out:
Rusty Russellc6c49272008-11-25 02:35:05 +10306855 return -ENOMEM;
Gregory Haskins57d885f2008-01-25 21:08:18 +01006856}
6857
6858static void init_defrootdomain(void)
6859{
Pekka Enberg68c38fc2010-07-15 23:18:22 +03006860 init_rootdomain(&def_root_domain);
Rusty Russellc6c49272008-11-25 02:35:05 +10306861
Gregory Haskins57d885f2008-01-25 21:08:18 +01006862 atomic_set(&def_root_domain.refcount, 1);
6863}
6864
Gregory Haskinsdc938522008-01-25 21:08:26 +01006865static struct root_domain *alloc_rootdomain(void)
Gregory Haskins57d885f2008-01-25 21:08:18 +01006866{
6867 struct root_domain *rd;
6868
6869 rd = kmalloc(sizeof(*rd), GFP_KERNEL);
6870 if (!rd)
6871 return NULL;
6872
Pekka Enberg68c38fc2010-07-15 23:18:22 +03006873 if (init_rootdomain(rd) != 0) {
Rusty Russellc6c49272008-11-25 02:35:05 +10306874 kfree(rd);
6875 return NULL;
6876 }
Gregory Haskins57d885f2008-01-25 21:08:18 +01006877
6878 return rd;
6879}
6880
Peter Zijlstrae3589f62011-07-15 10:35:52 +02006881static void free_sched_groups(struct sched_group *sg, int free_sgp)
6882{
6883 struct sched_group *tmp, *first;
6884
6885 if (!sg)
6886 return;
6887
6888 first = sg;
6889 do {
6890 tmp = sg->next;
6891
6892 if (free_sgp && atomic_dec_and_test(&sg->sgp->ref))
6893 kfree(sg->sgp);
6894
6895 kfree(sg);
6896 sg = tmp;
6897 } while (sg != first);
6898}
6899
Peter Zijlstradce840a2011-04-07 14:09:50 +02006900static void free_sched_domain(struct rcu_head *rcu)
6901{
6902 struct sched_domain *sd = container_of(rcu, struct sched_domain, rcu);
Peter Zijlstrae3589f62011-07-15 10:35:52 +02006903
6904 /*
6905 * If its an overlapping domain it has private groups, iterate and
6906 * nuke them all.
6907 */
6908 if (sd->flags & SD_OVERLAP) {
6909 free_sched_groups(sd->groups, 1);
6910 } else if (atomic_dec_and_test(&sd->groups->ref)) {
Peter Zijlstra9c3f75c2011-07-14 13:00:06 +02006911 kfree(sd->groups->sgp);
Peter Zijlstradce840a2011-04-07 14:09:50 +02006912 kfree(sd->groups);
Peter Zijlstra9c3f75c2011-07-14 13:00:06 +02006913 }
Peter Zijlstradce840a2011-04-07 14:09:50 +02006914 kfree(sd);
6915}
6916
6917static void destroy_sched_domain(struct sched_domain *sd, int cpu)
6918{
6919 call_rcu(&sd->rcu, free_sched_domain);
6920}
6921
6922static void destroy_sched_domains(struct sched_domain *sd, int cpu)
6923{
6924 for (; sd; sd = sd->parent)
6925 destroy_sched_domain(sd, cpu);
6926}
6927
Linus Torvalds1da177e2005-04-16 15:20:36 -07006928/*
Ingo Molnar0eab9142008-01-25 21:08:19 +01006929 * Attach the domain 'sd' to 'cpu' as its base domain. Callers must
Linus Torvalds1da177e2005-04-16 15:20:36 -07006930 * hold the hotplug lock.
6931 */
Ingo Molnar0eab9142008-01-25 21:08:19 +01006932static void
6933cpu_attach_domain(struct sched_domain *sd, struct root_domain *rd, int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006934{
Ingo Molnar70b97a72006-07-03 00:25:42 -07006935 struct rq *rq = cpu_rq(cpu);
Suresh Siddha245af2c2005-06-25 14:57:25 -07006936 struct sched_domain *tmp;
6937
6938 /* Remove the sched domains which do not contribute to scheduling. */
Li Zefanf29c9b12008-11-06 09:45:16 +08006939 for (tmp = sd; tmp; ) {
Suresh Siddha245af2c2005-06-25 14:57:25 -07006940 struct sched_domain *parent = tmp->parent;
6941 if (!parent)
6942 break;
Li Zefanf29c9b12008-11-06 09:45:16 +08006943
Siddha, Suresh B1a848872006-10-03 01:14:08 -07006944 if (sd_parent_degenerate(tmp, parent)) {
Suresh Siddha245af2c2005-06-25 14:57:25 -07006945 tmp->parent = parent->parent;
Siddha, Suresh B1a848872006-10-03 01:14:08 -07006946 if (parent->parent)
6947 parent->parent->child = tmp;
Peter Zijlstradce840a2011-04-07 14:09:50 +02006948 destroy_sched_domain(parent, cpu);
Li Zefanf29c9b12008-11-06 09:45:16 +08006949 } else
6950 tmp = tmp->parent;
Suresh Siddha245af2c2005-06-25 14:57:25 -07006951 }
6952
Siddha, Suresh B1a848872006-10-03 01:14:08 -07006953 if (sd && sd_degenerate(sd)) {
Peter Zijlstradce840a2011-04-07 14:09:50 +02006954 tmp = sd;
Suresh Siddha245af2c2005-06-25 14:57:25 -07006955 sd = sd->parent;
Peter Zijlstradce840a2011-04-07 14:09:50 +02006956 destroy_sched_domain(tmp, cpu);
Siddha, Suresh B1a848872006-10-03 01:14:08 -07006957 if (sd)
6958 sd->child = NULL;
6959 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006960
Peter Zijlstra4cb98832011-04-07 14:09:58 +02006961 sched_domain_debug(sd, cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006962
Gregory Haskins57d885f2008-01-25 21:08:18 +01006963 rq_attach_root(rq, rd);
Peter Zijlstradce840a2011-04-07 14:09:50 +02006964 tmp = rq->sd;
Nick Piggin674311d2005-06-25 14:57:27 -07006965 rcu_assign_pointer(rq->sd, sd);
Peter Zijlstradce840a2011-04-07 14:09:50 +02006966 destroy_sched_domains(tmp, cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006967}
6968
6969/* cpus with isolated domains */
Rusty Russelldcc30a32008-11-25 02:35:12 +10306970static cpumask_var_t cpu_isolated_map;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006971
6972/* Setup the mask of cpus configured for isolated domains */
6973static int __init isolated_cpu_setup(char *str)
6974{
Rusty Russellbdddd292009-12-02 14:09:16 +10306975 alloc_bootmem_cpumask_var(&cpu_isolated_map);
Rusty Russell968ea6d2008-12-13 21:55:51 +10306976 cpulist_parse(str, cpu_isolated_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006977 return 1;
6978}
6979
Ingo Molnar8927f492007-10-15 17:00:13 +02006980__setup("isolcpus=", isolated_cpu_setup);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006981
John Hawkes9c1cfda2005-09-06 15:18:14 -07006982#define SD_NODES_PER_DOMAIN 16
Linus Torvalds1da177e2005-04-16 15:20:36 -07006983
John Hawkes9c1cfda2005-09-06 15:18:14 -07006984#ifdef CONFIG_NUMA
akpm@osdl.org198e2f12006-01-12 01:05:30 -08006985
John Hawkes9c1cfda2005-09-06 15:18:14 -07006986/**
6987 * find_next_best_node - find the next node to include in a sched_domain
6988 * @node: node whose sched_domain we're building
6989 * @used_nodes: nodes already in the sched_domain
6990 *
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01006991 * Find the next node to include in a given scheduling domain. Simply
John Hawkes9c1cfda2005-09-06 15:18:14 -07006992 * finds the closest node not already in the @used_nodes map.
6993 *
6994 * Should use nodemask_t.
6995 */
Mike Travisc5f59f02008-04-04 18:11:10 -07006996static int find_next_best_node(int node, nodemask_t *used_nodes)
John Hawkes9c1cfda2005-09-06 15:18:14 -07006997{
Hillf Danton7142d172011-05-05 20:53:20 +08006998 int i, n, val, min_val, best_node = -1;
John Hawkes9c1cfda2005-09-06 15:18:14 -07006999
7000 min_val = INT_MAX;
7001
Mike Travis076ac2a2008-05-12 21:21:12 +02007002 for (i = 0; i < nr_node_ids; i++) {
John Hawkes9c1cfda2005-09-06 15:18:14 -07007003 /* Start at @node */
Mike Travis076ac2a2008-05-12 21:21:12 +02007004 n = (node + i) % nr_node_ids;
John Hawkes9c1cfda2005-09-06 15:18:14 -07007005
7006 if (!nr_cpus_node(n))
7007 continue;
7008
7009 /* Skip already used nodes */
Mike Travisc5f59f02008-04-04 18:11:10 -07007010 if (node_isset(n, *used_nodes))
John Hawkes9c1cfda2005-09-06 15:18:14 -07007011 continue;
7012
7013 /* Simple min distance search */
7014 val = node_distance(node, n);
7015
7016 if (val < min_val) {
7017 min_val = val;
7018 best_node = n;
7019 }
7020 }
7021
Hillf Danton7142d172011-05-05 20:53:20 +08007022 if (best_node != -1)
7023 node_set(best_node, *used_nodes);
John Hawkes9c1cfda2005-09-06 15:18:14 -07007024 return best_node;
7025}
7026
7027/**
7028 * sched_domain_node_span - get a cpumask for a node's sched_domain
7029 * @node: node whose cpumask we're constructing
Randy Dunlap73486722008-04-22 10:07:22 -07007030 * @span: resulting cpumask
John Hawkes9c1cfda2005-09-06 15:18:14 -07007031 *
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01007032 * Given a node, construct a good cpumask for its sched_domain to span. It
John Hawkes9c1cfda2005-09-06 15:18:14 -07007033 * should be one that prevents unnecessary balancing, but also spreads tasks
7034 * out optimally.
7035 */
Rusty Russell96f874e2008-11-25 02:35:14 +10307036static void sched_domain_node_span(int node, struct cpumask *span)
John Hawkes9c1cfda2005-09-06 15:18:14 -07007037{
Mike Travisc5f59f02008-04-04 18:11:10 -07007038 nodemask_t used_nodes;
Ingo Molnar48f24c42006-07-03 00:25:40 -07007039 int i;
John Hawkes9c1cfda2005-09-06 15:18:14 -07007040
Mike Travis6ca09df2008-12-31 18:08:45 -08007041 cpumask_clear(span);
Mike Travisc5f59f02008-04-04 18:11:10 -07007042 nodes_clear(used_nodes);
John Hawkes9c1cfda2005-09-06 15:18:14 -07007043
Mike Travis6ca09df2008-12-31 18:08:45 -08007044 cpumask_or(span, span, cpumask_of_node(node));
Mike Travisc5f59f02008-04-04 18:11:10 -07007045 node_set(node, used_nodes);
John Hawkes9c1cfda2005-09-06 15:18:14 -07007046
7047 for (i = 1; i < SD_NODES_PER_DOMAIN; i++) {
Mike Travisc5f59f02008-04-04 18:11:10 -07007048 int next_node = find_next_best_node(node, &used_nodes);
Hillf Danton7142d172011-05-05 20:53:20 +08007049 if (next_node < 0)
7050 break;
Mike Travis6ca09df2008-12-31 18:08:45 -08007051 cpumask_or(span, span, cpumask_of_node(next_node));
John Hawkes9c1cfda2005-09-06 15:18:14 -07007052 }
John Hawkes9c1cfda2005-09-06 15:18:14 -07007053}
Peter Zijlstrad3081f52011-04-07 14:09:59 +02007054
7055static const struct cpumask *cpu_node_mask(int cpu)
7056{
7057 lockdep_assert_held(&sched_domains_mutex);
7058
7059 sched_domain_node_span(cpu_to_node(cpu), sched_domains_tmpmask);
7060
7061 return sched_domains_tmpmask;
7062}
Peter Zijlstra2c402dc2011-04-07 14:10:01 +02007063
7064static const struct cpumask *cpu_allnodes_mask(int cpu)
7065{
7066 return cpu_possible_mask;
7067}
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +02007068#endif /* CONFIG_NUMA */
John Hawkes9c1cfda2005-09-06 15:18:14 -07007069
Peter Zijlstrad3081f52011-04-07 14:09:59 +02007070static const struct cpumask *cpu_cpu_mask(int cpu)
7071{
7072 return cpumask_of_node(cpu_to_node(cpu));
7073}
7074
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07007075int sched_smt_power_savings = 0, sched_mc_power_savings = 0;
Ingo Molnar48f24c42006-07-03 00:25:40 -07007076
Peter Zijlstradce840a2011-04-07 14:09:50 +02007077struct sd_data {
7078 struct sched_domain **__percpu sd;
7079 struct sched_group **__percpu sg;
Peter Zijlstra9c3f75c2011-07-14 13:00:06 +02007080 struct sched_group_power **__percpu sgp;
Peter Zijlstradce840a2011-04-07 14:09:50 +02007081};
7082
Andreas Herrmann49a02c52009-08-18 12:51:52 +02007083struct s_data {
Peter Zijlstra21d42cc2011-04-07 14:09:48 +02007084 struct sched_domain ** __percpu sd;
Andreas Herrmann49a02c52009-08-18 12:51:52 +02007085 struct root_domain *rd;
7086};
7087
Andreas Herrmann2109b992009-08-18 12:53:00 +02007088enum s_alloc {
Andreas Herrmann2109b992009-08-18 12:53:00 +02007089 sa_rootdomain,
Peter Zijlstra21d42cc2011-04-07 14:09:48 +02007090 sa_sd,
Peter Zijlstradce840a2011-04-07 14:09:50 +02007091 sa_sd_storage,
Andreas Herrmann2109b992009-08-18 12:53:00 +02007092 sa_none,
7093};
7094
Peter Zijlstra54ab4ff2011-04-07 14:10:03 +02007095struct sched_domain_topology_level;
7096
7097typedef struct sched_domain *(*sched_domain_init_f)(struct sched_domain_topology_level *tl, int cpu);
Peter Zijlstraeb7a74e2011-04-07 14:10:00 +02007098typedef const struct cpumask *(*sched_domain_mask_f)(int cpu);
7099
Peter Zijlstrae3589f62011-07-15 10:35:52 +02007100#define SDTL_OVERLAP 0x01
7101
Peter Zijlstraeb7a74e2011-04-07 14:10:00 +02007102struct sched_domain_topology_level {
Peter Zijlstra2c402dc2011-04-07 14:10:01 +02007103 sched_domain_init_f init;
7104 sched_domain_mask_f mask;
Peter Zijlstrae3589f62011-07-15 10:35:52 +02007105 int flags;
Peter Zijlstra54ab4ff2011-04-07 14:10:03 +02007106 struct sd_data data;
Peter Zijlstraeb7a74e2011-04-07 14:10:00 +02007107};
7108
Peter Zijlstrae3589f62011-07-15 10:35:52 +02007109static int
7110build_overlap_sched_groups(struct sched_domain *sd, int cpu)
7111{
7112 struct sched_group *first = NULL, *last = NULL, *groups = NULL, *sg;
7113 const struct cpumask *span = sched_domain_span(sd);
7114 struct cpumask *covered = sched_domains_tmpmask;
7115 struct sd_data *sdd = sd->private;
7116 struct sched_domain *child;
7117 int i;
7118
7119 cpumask_clear(covered);
7120
7121 for_each_cpu(i, span) {
7122 struct cpumask *sg_span;
7123
7124 if (cpumask_test_cpu(i, covered))
7125 continue;
7126
7127 sg = kzalloc_node(sizeof(struct sched_group) + cpumask_size(),
7128 GFP_KERNEL, cpu_to_node(i));
7129
7130 if (!sg)
7131 goto fail;
7132
7133 sg_span = sched_group_cpus(sg);
7134
7135 child = *per_cpu_ptr(sdd->sd, i);
7136 if (child->child) {
7137 child = child->child;
7138 cpumask_copy(sg_span, sched_domain_span(child));
7139 } else
7140 cpumask_set_cpu(i, sg_span);
7141
7142 cpumask_or(covered, covered, sg_span);
7143
7144 sg->sgp = *per_cpu_ptr(sdd->sgp, cpumask_first(sg_span));
7145 atomic_inc(&sg->sgp->ref);
7146
7147 if (cpumask_test_cpu(cpu, sg_span))
7148 groups = sg;
7149
7150 if (!first)
7151 first = sg;
7152 if (last)
7153 last->next = sg;
7154 last = sg;
7155 last->next = first;
7156 }
7157 sd->groups = groups;
7158
7159 return 0;
7160
7161fail:
7162 free_sched_groups(first, 0);
7163
7164 return -ENOMEM;
7165}
7166
Peter Zijlstradce840a2011-04-07 14:09:50 +02007167static int get_group(int cpu, struct sd_data *sdd, struct sched_group **sg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007168{
Peter Zijlstradce840a2011-04-07 14:09:50 +02007169 struct sched_domain *sd = *per_cpu_ptr(sdd->sd, cpu);
7170 struct sched_domain *child = sd->child;
7171
7172 if (child)
7173 cpu = cpumask_first(sched_domain_span(child));
7174
Peter Zijlstra9c3f75c2011-07-14 13:00:06 +02007175 if (sg) {
Peter Zijlstradce840a2011-04-07 14:09:50 +02007176 *sg = *per_cpu_ptr(sdd->sg, cpu);
Peter Zijlstra9c3f75c2011-07-14 13:00:06 +02007177 (*sg)->sgp = *per_cpu_ptr(sdd->sgp, cpu);
Peter Zijlstrae3589f62011-07-15 10:35:52 +02007178 atomic_set(&(*sg)->sgp->ref, 1); /* for claim_allocations */
Peter Zijlstra9c3f75c2011-07-14 13:00:06 +02007179 }
Peter Zijlstradce840a2011-04-07 14:09:50 +02007180
Linus Torvalds1da177e2005-04-16 15:20:36 -07007181 return cpu;
7182}
Linus Torvalds1da177e2005-04-16 15:20:36 -07007183
Ingo Molnar48f24c42006-07-03 00:25:40 -07007184/*
Peter Zijlstradce840a2011-04-07 14:09:50 +02007185 * build_sched_groups will build a circular linked list of the groups
7186 * covered by the given span, and will set each group's ->cpumask correctly,
7187 * and ->cpu_power to 0.
Peter Zijlstrae3589f62011-07-15 10:35:52 +02007188 *
7189 * Assumes the sched_domain tree is fully constructed
Ingo Molnar48f24c42006-07-03 00:25:40 -07007190 */
Peter Zijlstrae3589f62011-07-15 10:35:52 +02007191static int
7192build_sched_groups(struct sched_domain *sd, int cpu)
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08007193{
Peter Zijlstradce840a2011-04-07 14:09:50 +02007194 struct sched_group *first = NULL, *last = NULL;
7195 struct sd_data *sdd = sd->private;
7196 const struct cpumask *span = sched_domain_span(sd);
Peter Zijlstraf96225f2011-04-07 14:09:57 +02007197 struct cpumask *covered;
Peter Zijlstradce840a2011-04-07 14:09:50 +02007198 int i;
7199
Peter Zijlstrae3589f62011-07-15 10:35:52 +02007200 get_group(cpu, sdd, &sd->groups);
7201 atomic_inc(&sd->groups->ref);
7202
7203 if (cpu != cpumask_first(sched_domain_span(sd)))
7204 return 0;
7205
Peter Zijlstraf96225f2011-04-07 14:09:57 +02007206 lockdep_assert_held(&sched_domains_mutex);
7207 covered = sched_domains_tmpmask;
7208
Peter Zijlstradce840a2011-04-07 14:09:50 +02007209 cpumask_clear(covered);
7210
7211 for_each_cpu(i, span) {
7212 struct sched_group *sg;
7213 int group = get_group(i, sdd, &sg);
7214 int j;
7215
7216 if (cpumask_test_cpu(i, covered))
7217 continue;
7218
7219 cpumask_clear(sched_group_cpus(sg));
Peter Zijlstra9c3f75c2011-07-14 13:00:06 +02007220 sg->sgp->power = 0;
Peter Zijlstradce840a2011-04-07 14:09:50 +02007221
7222 for_each_cpu(j, span) {
7223 if (get_group(j, sdd, NULL) != group)
7224 continue;
7225
7226 cpumask_set_cpu(j, covered);
7227 cpumask_set_cpu(j, sched_group_cpus(sg));
7228 }
7229
7230 if (!first)
7231 first = sg;
7232 if (last)
7233 last->next = sg;
7234 last = sg;
7235 }
7236 last->next = first;
Peter Zijlstrae3589f62011-07-15 10:35:52 +02007237
7238 return 0;
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08007239}
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07007240
Linus Torvalds1da177e2005-04-16 15:20:36 -07007241/*
Siddha, Suresh B89c47102006-10-03 01:14:09 -07007242 * Initialize sched groups cpu_power.
7243 *
7244 * cpu_power indicates the capacity of sched group, which is used while
7245 * distributing the load between different sched groups in a sched domain.
7246 * Typically cpu_power for all the groups in a sched domain will be same unless
7247 * there are asymmetries in the topology. If there are asymmetries, group
7248 * having more cpu_power will pickup more load compared to the group having
7249 * less cpu_power.
Siddha, Suresh B89c47102006-10-03 01:14:09 -07007250 */
7251static void init_sched_groups_power(int cpu, struct sched_domain *sd)
7252{
Peter Zijlstrae3589f62011-07-15 10:35:52 +02007253 struct sched_group *sg = sd->groups;
Siddha, Suresh B89c47102006-10-03 01:14:09 -07007254
Peter Zijlstrae3589f62011-07-15 10:35:52 +02007255 WARN_ON(!sd || !sg);
7256
7257 do {
7258 sg->group_weight = cpumask_weight(sched_group_cpus(sg));
7259 sg = sg->next;
7260 } while (sg != sd->groups);
7261
7262 if (cpu != group_first_cpu(sg))
Siddha, Suresh B89c47102006-10-03 01:14:09 -07007263 return;
7264
Peter Zijlstrad274cb32011-04-07 14:09:43 +02007265 update_group_power(sd, cpu);
Siddha, Suresh B89c47102006-10-03 01:14:09 -07007266}
7267
7268/*
Mike Travis7c16ec52008-04-04 18:11:11 -07007269 * Initializers for schedule domains
7270 * Non-inlined to reduce accumulated stack pressure in build_sched_domains()
7271 */
7272
Ingo Molnara5d8c342008-10-09 11:35:51 +02007273#ifdef CONFIG_SCHED_DEBUG
7274# define SD_INIT_NAME(sd, type) sd->name = #type
7275#else
7276# define SD_INIT_NAME(sd, type) do { } while (0)
7277#endif
7278
Peter Zijlstra54ab4ff2011-04-07 14:10:03 +02007279#define SD_INIT_FUNC(type) \
7280static noinline struct sched_domain * \
7281sd_init_##type(struct sched_domain_topology_level *tl, int cpu) \
7282{ \
7283 struct sched_domain *sd = *per_cpu_ptr(tl->data.sd, cpu); \
7284 *sd = SD_##type##_INIT; \
Peter Zijlstra54ab4ff2011-04-07 14:10:03 +02007285 SD_INIT_NAME(sd, type); \
7286 sd->private = &tl->data; \
7287 return sd; \
Mike Travis7c16ec52008-04-04 18:11:11 -07007288}
7289
7290SD_INIT_FUNC(CPU)
7291#ifdef CONFIG_NUMA
7292 SD_INIT_FUNC(ALLNODES)
7293 SD_INIT_FUNC(NODE)
7294#endif
7295#ifdef CONFIG_SCHED_SMT
7296 SD_INIT_FUNC(SIBLING)
7297#endif
7298#ifdef CONFIG_SCHED_MC
7299 SD_INIT_FUNC(MC)
7300#endif
Heiko Carstens01a08542010-08-31 10:28:16 +02007301#ifdef CONFIG_SCHED_BOOK
7302 SD_INIT_FUNC(BOOK)
7303#endif
Mike Travis7c16ec52008-04-04 18:11:11 -07007304
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09007305static int default_relax_domain_level = -1;
Peter Zijlstra60495e72011-04-07 14:10:04 +02007306int sched_domain_level_max;
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09007307
7308static int __init setup_relax_domain_level(char *str)
7309{
Li Zefan30e0e172008-05-13 10:27:17 +08007310 unsigned long val;
7311
7312 val = simple_strtoul(str, NULL, 0);
Peter Zijlstra60495e72011-04-07 14:10:04 +02007313 if (val < sched_domain_level_max)
Li Zefan30e0e172008-05-13 10:27:17 +08007314 default_relax_domain_level = val;
7315
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09007316 return 1;
7317}
7318__setup("relax_domain_level=", setup_relax_domain_level);
7319
7320static void set_domain_attribute(struct sched_domain *sd,
7321 struct sched_domain_attr *attr)
7322{
7323 int request;
7324
7325 if (!attr || attr->relax_domain_level < 0) {
7326 if (default_relax_domain_level < 0)
7327 return;
7328 else
7329 request = default_relax_domain_level;
7330 } else
7331 request = attr->relax_domain_level;
7332 if (request < sd->level) {
7333 /* turn off idle balance on this domain */
Peter Zijlstrac88d5912009-09-10 13:50:02 +02007334 sd->flags &= ~(SD_BALANCE_WAKE|SD_BALANCE_NEWIDLE);
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09007335 } else {
7336 /* turn on idle balance on this domain */
Peter Zijlstrac88d5912009-09-10 13:50:02 +02007337 sd->flags |= (SD_BALANCE_WAKE|SD_BALANCE_NEWIDLE);
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09007338 }
7339}
7340
Peter Zijlstra54ab4ff2011-04-07 14:10:03 +02007341static void __sdt_free(const struct cpumask *cpu_map);
7342static int __sdt_alloc(const struct cpumask *cpu_map);
7343
Andreas Herrmann2109b992009-08-18 12:53:00 +02007344static void __free_domain_allocs(struct s_data *d, enum s_alloc what,
7345 const struct cpumask *cpu_map)
7346{
7347 switch (what) {
Andreas Herrmann2109b992009-08-18 12:53:00 +02007348 case sa_rootdomain:
Peter Zijlstra822ff792011-04-07 14:09:51 +02007349 if (!atomic_read(&d->rd->refcount))
7350 free_rootdomain(&d->rd->rcu); /* fall through */
Peter Zijlstra21d42cc2011-04-07 14:09:48 +02007351 case sa_sd:
7352 free_percpu(d->sd); /* fall through */
Peter Zijlstradce840a2011-04-07 14:09:50 +02007353 case sa_sd_storage:
Peter Zijlstra54ab4ff2011-04-07 14:10:03 +02007354 __sdt_free(cpu_map); /* fall through */
Andreas Herrmann2109b992009-08-18 12:53:00 +02007355 case sa_none:
7356 break;
7357 }
7358}
7359
7360static enum s_alloc __visit_domain_allocation_hell(struct s_data *d,
7361 const struct cpumask *cpu_map)
7362{
Peter Zijlstradce840a2011-04-07 14:09:50 +02007363 memset(d, 0, sizeof(*d));
7364
Peter Zijlstra54ab4ff2011-04-07 14:10:03 +02007365 if (__sdt_alloc(cpu_map))
7366 return sa_sd_storage;
Peter Zijlstra21d42cc2011-04-07 14:09:48 +02007367 d->sd = alloc_percpu(struct sched_domain *);
Peter Zijlstradce840a2011-04-07 14:09:50 +02007368 if (!d->sd)
7369 return sa_sd_storage;
Andreas Herrmann2109b992009-08-18 12:53:00 +02007370 d->rd = alloc_rootdomain();
Peter Zijlstradce840a2011-04-07 14:09:50 +02007371 if (!d->rd)
Peter Zijlstra21d42cc2011-04-07 14:09:48 +02007372 return sa_sd;
Andreas Herrmann2109b992009-08-18 12:53:00 +02007373 return sa_rootdomain;
7374}
7375
Peter Zijlstradce840a2011-04-07 14:09:50 +02007376/*
7377 * NULL the sd_data elements we've used to build the sched_domain and
7378 * sched_group structure so that the subsequent __free_domain_allocs()
7379 * will not free the data we're using.
7380 */
7381static void claim_allocations(int cpu, struct sched_domain *sd)
7382{
7383 struct sd_data *sdd = sd->private;
Peter Zijlstradce840a2011-04-07 14:09:50 +02007384
7385 WARN_ON_ONCE(*per_cpu_ptr(sdd->sd, cpu) != sd);
7386 *per_cpu_ptr(sdd->sd, cpu) = NULL;
7387
Peter Zijlstrae3589f62011-07-15 10:35:52 +02007388 if (atomic_read(&(*per_cpu_ptr(sdd->sg, cpu))->ref))
Peter Zijlstradce840a2011-04-07 14:09:50 +02007389 *per_cpu_ptr(sdd->sg, cpu) = NULL;
Peter Zijlstrae3589f62011-07-15 10:35:52 +02007390
7391 if (atomic_read(&(*per_cpu_ptr(sdd->sgp, cpu))->ref))
Peter Zijlstra9c3f75c2011-07-14 13:00:06 +02007392 *per_cpu_ptr(sdd->sgp, cpu) = NULL;
Peter Zijlstradce840a2011-04-07 14:09:50 +02007393}
7394
Andreas Herrmannd8173532009-08-18 12:57:03 +02007395#ifdef CONFIG_SCHED_SMT
Peter Zijlstra2c402dc2011-04-07 14:10:01 +02007396static const struct cpumask *cpu_smt_mask(int cpu)
7397{
7398 return topology_thread_cpumask(cpu);
Andreas Herrmannd8173532009-08-18 12:57:03 +02007399}
Peter Zijlstra2c402dc2011-04-07 14:10:01 +02007400#endif
Andreas Herrmannd8173532009-08-18 12:57:03 +02007401
Peter Zijlstrad069b912011-04-07 14:10:02 +02007402/*
7403 * Topology list, bottom-up.
7404 */
Peter Zijlstraeb7a74e2011-04-07 14:10:00 +02007405static struct sched_domain_topology_level default_topology[] = {
Peter Zijlstrad069b912011-04-07 14:10:02 +02007406#ifdef CONFIG_SCHED_SMT
7407 { sd_init_SIBLING, cpu_smt_mask, },
Peter Zijlstra2c402dc2011-04-07 14:10:01 +02007408#endif
7409#ifdef CONFIG_SCHED_MC
7410 { sd_init_MC, cpu_coregroup_mask, },
7411#endif
Peter Zijlstrad069b912011-04-07 14:10:02 +02007412#ifdef CONFIG_SCHED_BOOK
7413 { sd_init_BOOK, cpu_book_mask, },
7414#endif
7415 { sd_init_CPU, cpu_cpu_mask, },
7416#ifdef CONFIG_NUMA
Peter Zijlstrae3589f62011-07-15 10:35:52 +02007417 { sd_init_NODE, cpu_node_mask, SDTL_OVERLAP, },
Peter Zijlstrad069b912011-04-07 14:10:02 +02007418 { sd_init_ALLNODES, cpu_allnodes_mask, },
Peter Zijlstra2c402dc2011-04-07 14:10:01 +02007419#endif
Peter Zijlstraeb7a74e2011-04-07 14:10:00 +02007420 { NULL, },
7421};
7422
7423static struct sched_domain_topology_level *sched_domain_topology = default_topology;
7424
Peter Zijlstra54ab4ff2011-04-07 14:10:03 +02007425static int __sdt_alloc(const struct cpumask *cpu_map)
7426{
7427 struct sched_domain_topology_level *tl;
7428 int j;
7429
7430 for (tl = sched_domain_topology; tl->init; tl++) {
7431 struct sd_data *sdd = &tl->data;
7432
7433 sdd->sd = alloc_percpu(struct sched_domain *);
7434 if (!sdd->sd)
7435 return -ENOMEM;
7436
7437 sdd->sg = alloc_percpu(struct sched_group *);
7438 if (!sdd->sg)
7439 return -ENOMEM;
7440
Peter Zijlstra9c3f75c2011-07-14 13:00:06 +02007441 sdd->sgp = alloc_percpu(struct sched_group_power *);
7442 if (!sdd->sgp)
7443 return -ENOMEM;
7444
Peter Zijlstra54ab4ff2011-04-07 14:10:03 +02007445 for_each_cpu(j, cpu_map) {
7446 struct sched_domain *sd;
7447 struct sched_group *sg;
Peter Zijlstra9c3f75c2011-07-14 13:00:06 +02007448 struct sched_group_power *sgp;
Peter Zijlstra54ab4ff2011-04-07 14:10:03 +02007449
7450 sd = kzalloc_node(sizeof(struct sched_domain) + cpumask_size(),
7451 GFP_KERNEL, cpu_to_node(j));
7452 if (!sd)
7453 return -ENOMEM;
7454
7455 *per_cpu_ptr(sdd->sd, j) = sd;
7456
7457 sg = kzalloc_node(sizeof(struct sched_group) + cpumask_size(),
7458 GFP_KERNEL, cpu_to_node(j));
7459 if (!sg)
7460 return -ENOMEM;
7461
7462 *per_cpu_ptr(sdd->sg, j) = sg;
Peter Zijlstra9c3f75c2011-07-14 13:00:06 +02007463
7464 sgp = kzalloc_node(sizeof(struct sched_group_power),
7465 GFP_KERNEL, cpu_to_node(j));
7466 if (!sgp)
7467 return -ENOMEM;
7468
7469 *per_cpu_ptr(sdd->sgp, j) = sgp;
Peter Zijlstra54ab4ff2011-04-07 14:10:03 +02007470 }
7471 }
7472
7473 return 0;
7474}
7475
7476static void __sdt_free(const struct cpumask *cpu_map)
7477{
7478 struct sched_domain_topology_level *tl;
7479 int j;
7480
7481 for (tl = sched_domain_topology; tl->init; tl++) {
7482 struct sd_data *sdd = &tl->data;
7483
7484 for_each_cpu(j, cpu_map) {
Peter Zijlstrae3589f62011-07-15 10:35:52 +02007485 struct sched_domain *sd = *per_cpu_ptr(sdd->sd, j);
7486 if (sd && (sd->flags & SD_OVERLAP))
7487 free_sched_groups(sd->groups, 0);
Peter Zijlstra54ab4ff2011-04-07 14:10:03 +02007488 kfree(*per_cpu_ptr(sdd->sg, j));
Peter Zijlstra9c3f75c2011-07-14 13:00:06 +02007489 kfree(*per_cpu_ptr(sdd->sgp, j));
Peter Zijlstra54ab4ff2011-04-07 14:10:03 +02007490 }
7491 free_percpu(sdd->sd);
7492 free_percpu(sdd->sg);
Peter Zijlstra9c3f75c2011-07-14 13:00:06 +02007493 free_percpu(sdd->sgp);
Peter Zijlstra54ab4ff2011-04-07 14:10:03 +02007494 }
7495}
7496
Peter Zijlstra2c402dc2011-04-07 14:10:01 +02007497struct sched_domain *build_sched_domain(struct sched_domain_topology_level *tl,
7498 struct s_data *d, const struct cpumask *cpu_map,
Peter Zijlstrad069b912011-04-07 14:10:02 +02007499 struct sched_domain_attr *attr, struct sched_domain *child,
Peter Zijlstra2c402dc2011-04-07 14:10:01 +02007500 int cpu)
7501{
Peter Zijlstra54ab4ff2011-04-07 14:10:03 +02007502 struct sched_domain *sd = tl->init(tl, cpu);
Peter Zijlstra2c402dc2011-04-07 14:10:01 +02007503 if (!sd)
Peter Zijlstrad069b912011-04-07 14:10:02 +02007504 return child;
Peter Zijlstra2c402dc2011-04-07 14:10:01 +02007505
7506 set_domain_attribute(sd, attr);
7507 cpumask_and(sched_domain_span(sd), cpu_map, tl->mask(cpu));
Peter Zijlstra60495e72011-04-07 14:10:04 +02007508 if (child) {
7509 sd->level = child->level + 1;
7510 sched_domain_level_max = max(sched_domain_level_max, sd->level);
Peter Zijlstrad069b912011-04-07 14:10:02 +02007511 child->parent = sd;
Peter Zijlstra60495e72011-04-07 14:10:04 +02007512 }
Peter Zijlstrad069b912011-04-07 14:10:02 +02007513 sd->child = child;
Peter Zijlstra2c402dc2011-04-07 14:10:01 +02007514
7515 return sd;
7516}
7517
Mike Travis7c16ec52008-04-04 18:11:11 -07007518/*
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07007519 * Build sched domains for a given set of cpus and attach the sched domains
7520 * to the individual cpus
Linus Torvalds1da177e2005-04-16 15:20:36 -07007521 */
Peter Zijlstradce840a2011-04-07 14:09:50 +02007522static int build_sched_domains(const struct cpumask *cpu_map,
7523 struct sched_domain_attr *attr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007524{
Andreas Herrmann2109b992009-08-18 12:53:00 +02007525 enum s_alloc alloc_state = sa_none;
Peter Zijlstradce840a2011-04-07 14:09:50 +02007526 struct sched_domain *sd;
Andreas Herrmann49a02c52009-08-18 12:51:52 +02007527 struct s_data d;
Peter Zijlstra822ff792011-04-07 14:09:51 +02007528 int i, ret = -ENOMEM;
Rusty Russell3404c8d2008-11-25 02:35:03 +10307529
Andreas Herrmann2109b992009-08-18 12:53:00 +02007530 alloc_state = __visit_domain_allocation_hell(&d, cpu_map);
7531 if (alloc_state != sa_rootdomain)
7532 goto error;
Mike Travis7c16ec52008-04-04 18:11:11 -07007533
Peter Zijlstradce840a2011-04-07 14:09:50 +02007534 /* Set up domains for cpus specified by the cpu_map. */
Rusty Russellabcd0832008-11-25 02:35:02 +10307535 for_each_cpu(i, cpu_map) {
Peter Zijlstraeb7a74e2011-04-07 14:10:00 +02007536 struct sched_domain_topology_level *tl;
7537
Peter Zijlstra3bd65a82011-04-07 14:09:54 +02007538 sd = NULL;
Peter Zijlstrae3589f62011-07-15 10:35:52 +02007539 for (tl = sched_domain_topology; tl->init; tl++) {
Peter Zijlstra2c402dc2011-04-07 14:10:01 +02007540 sd = build_sched_domain(tl, &d, cpu_map, attr, sd, i);
Peter Zijlstrae3589f62011-07-15 10:35:52 +02007541 if (tl->flags & SDTL_OVERLAP || sched_feat(FORCE_SD_OVERLAP))
7542 sd->flags |= SD_OVERLAP;
Peter Zijlstrad1102352011-07-20 18:42:57 +02007543 if (cpumask_equal(cpu_map, sched_domain_span(sd)))
7544 break;
Peter Zijlstrae3589f62011-07-15 10:35:52 +02007545 }
Peter Zijlstrad274cb32011-04-07 14:09:43 +02007546
Peter Zijlstrad069b912011-04-07 14:10:02 +02007547 while (sd->child)
7548 sd = sd->child;
7549
Peter Zijlstra21d42cc2011-04-07 14:09:48 +02007550 *per_cpu_ptr(d.sd, i) = sd;
Peter Zijlstradce840a2011-04-07 14:09:50 +02007551 }
Peter Zijlstra21d42cc2011-04-07 14:09:48 +02007552
Peter Zijlstradce840a2011-04-07 14:09:50 +02007553 /* Build the groups for the domains */
7554 for_each_cpu(i, cpu_map) {
7555 for (sd = *per_cpu_ptr(d.sd, i); sd; sd = sd->parent) {
7556 sd->span_weight = cpumask_weight(sched_domain_span(sd));
Peter Zijlstrae3589f62011-07-15 10:35:52 +02007557 if (sd->flags & SD_OVERLAP) {
7558 if (build_overlap_sched_groups(sd, i))
7559 goto error;
7560 } else {
7561 if (build_sched_groups(sd, i))
7562 goto error;
7563 }
Peter Zijlstra1cf51902011-04-07 14:09:47 +02007564 }
Peter Zijlstraa06dadb2011-04-07 14:09:44 +02007565 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007566
Linus Torvalds1da177e2005-04-16 15:20:36 -07007567 /* Calculate CPU power for physical packages and nodes */
Peter Zijlstraa9c9a9b2011-04-07 14:09:49 +02007568 for (i = nr_cpumask_bits-1; i >= 0; i--) {
7569 if (!cpumask_test_cpu(i, cpu_map))
7570 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007571
Peter Zijlstradce840a2011-04-07 14:09:50 +02007572 for (sd = *per_cpu_ptr(d.sd, i); sd; sd = sd->parent) {
7573 claim_allocations(i, sd);
Peter Zijlstracd4ea6a2011-04-07 14:09:45 +02007574 init_sched_groups_power(i, sd);
Peter Zijlstradce840a2011-04-07 14:09:50 +02007575 }
Siddha, Suresh Bf712c0c2006-07-30 03:02:59 -07007576 }
John Hawkes9c1cfda2005-09-06 15:18:14 -07007577
Linus Torvalds1da177e2005-04-16 15:20:36 -07007578 /* Attach the domains */
Peter Zijlstradce840a2011-04-07 14:09:50 +02007579 rcu_read_lock();
Rusty Russellabcd0832008-11-25 02:35:02 +10307580 for_each_cpu(i, cpu_map) {
Peter Zijlstra21d42cc2011-04-07 14:09:48 +02007581 sd = *per_cpu_ptr(d.sd, i);
Andreas Herrmann49a02c52009-08-18 12:51:52 +02007582 cpu_attach_domain(sd, d.rd, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007583 }
Peter Zijlstradce840a2011-04-07 14:09:50 +02007584 rcu_read_unlock();
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07007585
Peter Zijlstra822ff792011-04-07 14:09:51 +02007586 ret = 0;
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07007587error:
Andreas Herrmann2109b992009-08-18 12:53:00 +02007588 __free_domain_allocs(&d, alloc_state, cpu_map);
Peter Zijlstra822ff792011-04-07 14:09:51 +02007589 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007590}
Paul Jackson029190c2007-10-18 23:40:20 -07007591
Rusty Russellacc3f5d2009-11-03 14:53:40 +10307592static cpumask_var_t *doms_cur; /* current sched domains */
Paul Jackson029190c2007-10-18 23:40:20 -07007593static int ndoms_cur; /* number of sched domains in 'doms_cur' */
Ingo Molnar4285f5942008-05-16 17:47:14 +02007594static struct sched_domain_attr *dattr_cur;
7595 /* attribues of custom domains in 'doms_cur' */
Paul Jackson029190c2007-10-18 23:40:20 -07007596
7597/*
7598 * Special case: If a kmalloc of a doms_cur partition (array of
Rusty Russell42128232008-11-25 02:35:12 +10307599 * cpumask) fails, then fallback to a single sched domain,
7600 * as determined by the single cpumask fallback_doms.
Paul Jackson029190c2007-10-18 23:40:20 -07007601 */
Rusty Russell42128232008-11-25 02:35:12 +10307602static cpumask_var_t fallback_doms;
Paul Jackson029190c2007-10-18 23:40:20 -07007603
Heiko Carstensee79d1b2008-12-09 18:49:50 +01007604/*
7605 * arch_update_cpu_topology lets virtualized architectures update the
7606 * cpu core maps. It is supposed to return 1 if the topology changed
7607 * or 0 if it stayed the same.
7608 */
7609int __attribute__((weak)) arch_update_cpu_topology(void)
Heiko Carstens22e52b02008-03-12 18:31:59 +01007610{
Heiko Carstensee79d1b2008-12-09 18:49:50 +01007611 return 0;
Heiko Carstens22e52b02008-03-12 18:31:59 +01007612}
7613
Rusty Russellacc3f5d2009-11-03 14:53:40 +10307614cpumask_var_t *alloc_sched_domains(unsigned int ndoms)
7615{
7616 int i;
7617 cpumask_var_t *doms;
7618
7619 doms = kmalloc(sizeof(*doms) * ndoms, GFP_KERNEL);
7620 if (!doms)
7621 return NULL;
7622 for (i = 0; i < ndoms; i++) {
7623 if (!alloc_cpumask_var(&doms[i], GFP_KERNEL)) {
7624 free_sched_domains(doms, i);
7625 return NULL;
7626 }
7627 }
7628 return doms;
7629}
7630
7631void free_sched_domains(cpumask_var_t doms[], unsigned int ndoms)
7632{
7633 unsigned int i;
7634 for (i = 0; i < ndoms; i++)
7635 free_cpumask_var(doms[i]);
7636 kfree(doms);
7637}
7638
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07007639/*
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01007640 * Set up scheduler domains and groups. Callers must hold the hotplug lock.
Paul Jackson029190c2007-10-18 23:40:20 -07007641 * For now this just excludes isolated cpus, but could be used to
7642 * exclude other special cases in the future.
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07007643 */
Peter Zijlstrac4a88492011-04-07 14:09:42 +02007644static int init_sched_domains(const struct cpumask *cpu_map)
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07007645{
Milton Miller73785472007-10-24 18:23:48 +02007646 int err;
7647
Heiko Carstens22e52b02008-03-12 18:31:59 +01007648 arch_update_cpu_topology();
Paul Jackson029190c2007-10-18 23:40:20 -07007649 ndoms_cur = 1;
Rusty Russellacc3f5d2009-11-03 14:53:40 +10307650 doms_cur = alloc_sched_domains(ndoms_cur);
Paul Jackson029190c2007-10-18 23:40:20 -07007651 if (!doms_cur)
Rusty Russellacc3f5d2009-11-03 14:53:40 +10307652 doms_cur = &fallback_doms;
7653 cpumask_andnot(doms_cur[0], cpu_map, cpu_isolated_map);
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09007654 dattr_cur = NULL;
Peter Zijlstradce840a2011-04-07 14:09:50 +02007655 err = build_sched_domains(doms_cur[0], NULL);
Milton Miller6382bc92007-10-15 17:00:19 +02007656 register_sched_domain_sysctl();
Milton Miller73785472007-10-24 18:23:48 +02007657
7658 return err;
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07007659}
7660
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07007661/*
7662 * Detach sched domains from a group of cpus specified in cpu_map
7663 * These cpus will now be attached to the NULL domain
7664 */
Rusty Russell96f874e2008-11-25 02:35:14 +10307665static void detach_destroy_domains(const struct cpumask *cpu_map)
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07007666{
7667 int i;
7668
Peter Zijlstradce840a2011-04-07 14:09:50 +02007669 rcu_read_lock();
Rusty Russellabcd0832008-11-25 02:35:02 +10307670 for_each_cpu(i, cpu_map)
Gregory Haskins57d885f2008-01-25 21:08:18 +01007671 cpu_attach_domain(NULL, &def_root_domain, i);
Peter Zijlstradce840a2011-04-07 14:09:50 +02007672 rcu_read_unlock();
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07007673}
7674
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09007675/* handle null as "default" */
7676static int dattrs_equal(struct sched_domain_attr *cur, int idx_cur,
7677 struct sched_domain_attr *new, int idx_new)
7678{
7679 struct sched_domain_attr tmp;
7680
7681 /* fast path */
7682 if (!new && !cur)
7683 return 1;
7684
7685 tmp = SD_ATTR_INIT;
7686 return !memcmp(cur ? (cur + idx_cur) : &tmp,
7687 new ? (new + idx_new) : &tmp,
7688 sizeof(struct sched_domain_attr));
7689}
7690
Paul Jackson029190c2007-10-18 23:40:20 -07007691/*
7692 * Partition sched domains as specified by the 'ndoms_new'
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01007693 * cpumasks in the array doms_new[] of cpumasks. This compares
Paul Jackson029190c2007-10-18 23:40:20 -07007694 * doms_new[] to the current sched domain partitioning, doms_cur[].
7695 * It destroys each deleted domain and builds each new domain.
7696 *
Rusty Russellacc3f5d2009-11-03 14:53:40 +10307697 * 'doms_new' is an array of cpumask_var_t's of length 'ndoms_new'.
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01007698 * The masks don't intersect (don't overlap.) We should setup one
7699 * sched domain for each mask. CPUs not in any of the cpumasks will
7700 * not be load balanced. If the same cpumask appears both in the
Paul Jackson029190c2007-10-18 23:40:20 -07007701 * current 'doms_cur' domains and in the new 'doms_new', we can leave
7702 * it as it is.
7703 *
Rusty Russellacc3f5d2009-11-03 14:53:40 +10307704 * The passed in 'doms_new' should be allocated using
7705 * alloc_sched_domains. This routine takes ownership of it and will
7706 * free_sched_domains it when done with it. If the caller failed the
7707 * alloc call, then it can pass in doms_new == NULL && ndoms_new == 1,
7708 * and partition_sched_domains() will fallback to the single partition
7709 * 'fallback_doms', it also forces the domains to be rebuilt.
Paul Jackson029190c2007-10-18 23:40:20 -07007710 *
Rusty Russell96f874e2008-11-25 02:35:14 +10307711 * If doms_new == NULL it will be replaced with cpu_online_mask.
Li Zefan700018e2008-11-18 14:02:03 +08007712 * ndoms_new == 0 is a special case for destroying existing domains,
7713 * and it will not create the default domain.
Max Krasnyanskydfb512e2008-08-29 13:11:41 -07007714 *
Paul Jackson029190c2007-10-18 23:40:20 -07007715 * Call with hotplug lock held
7716 */
Rusty Russellacc3f5d2009-11-03 14:53:40 +10307717void partition_sched_domains(int ndoms_new, cpumask_var_t doms_new[],
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09007718 struct sched_domain_attr *dattr_new)
Paul Jackson029190c2007-10-18 23:40:20 -07007719{
Max Krasnyanskydfb512e2008-08-29 13:11:41 -07007720 int i, j, n;
Heiko Carstensd65bd5e2008-12-09 18:49:51 +01007721 int new_topology;
Paul Jackson029190c2007-10-18 23:40:20 -07007722
Heiko Carstens712555e2008-04-28 11:33:07 +02007723 mutex_lock(&sched_domains_mutex);
Srivatsa Vaddagiria1835612008-01-25 21:08:00 +01007724
Milton Miller73785472007-10-24 18:23:48 +02007725 /* always unregister in case we don't destroy any domains */
7726 unregister_sched_domain_sysctl();
7727
Heiko Carstensd65bd5e2008-12-09 18:49:51 +01007728 /* Let architecture update cpu core mappings. */
7729 new_topology = arch_update_cpu_topology();
7730
Max Krasnyanskydfb512e2008-08-29 13:11:41 -07007731 n = doms_new ? ndoms_new : 0;
Paul Jackson029190c2007-10-18 23:40:20 -07007732
7733 /* Destroy deleted domains */
7734 for (i = 0; i < ndoms_cur; i++) {
Heiko Carstensd65bd5e2008-12-09 18:49:51 +01007735 for (j = 0; j < n && !new_topology; j++) {
Rusty Russellacc3f5d2009-11-03 14:53:40 +10307736 if (cpumask_equal(doms_cur[i], doms_new[j])
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09007737 && dattrs_equal(dattr_cur, i, dattr_new, j))
Paul Jackson029190c2007-10-18 23:40:20 -07007738 goto match1;
7739 }
7740 /* no match - a current sched domain not in new doms_new[] */
Rusty Russellacc3f5d2009-11-03 14:53:40 +10307741 detach_destroy_domains(doms_cur[i]);
Paul Jackson029190c2007-10-18 23:40:20 -07007742match1:
7743 ;
7744 }
7745
Max Krasnyanskye761b772008-07-15 04:43:49 -07007746 if (doms_new == NULL) {
7747 ndoms_cur = 0;
Rusty Russellacc3f5d2009-11-03 14:53:40 +10307748 doms_new = &fallback_doms;
Peter Zijlstra6ad4c182009-11-25 13:31:39 +01007749 cpumask_andnot(doms_new[0], cpu_active_mask, cpu_isolated_map);
Li Zefanfaa2f982008-11-04 16:20:23 +08007750 WARN_ON_ONCE(dattr_new);
Max Krasnyanskye761b772008-07-15 04:43:49 -07007751 }
7752
Paul Jackson029190c2007-10-18 23:40:20 -07007753 /* Build new domains */
7754 for (i = 0; i < ndoms_new; i++) {
Heiko Carstensd65bd5e2008-12-09 18:49:51 +01007755 for (j = 0; j < ndoms_cur && !new_topology; j++) {
Rusty Russellacc3f5d2009-11-03 14:53:40 +10307756 if (cpumask_equal(doms_new[i], doms_cur[j])
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09007757 && dattrs_equal(dattr_new, i, dattr_cur, j))
Paul Jackson029190c2007-10-18 23:40:20 -07007758 goto match2;
7759 }
7760 /* no match - add a new doms_new */
Peter Zijlstradce840a2011-04-07 14:09:50 +02007761 build_sched_domains(doms_new[i], dattr_new ? dattr_new + i : NULL);
Paul Jackson029190c2007-10-18 23:40:20 -07007762match2:
7763 ;
7764 }
7765
7766 /* Remember the new sched domains */
Rusty Russellacc3f5d2009-11-03 14:53:40 +10307767 if (doms_cur != &fallback_doms)
7768 free_sched_domains(doms_cur, ndoms_cur);
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09007769 kfree(dattr_cur); /* kfree(NULL) is safe */
Paul Jackson029190c2007-10-18 23:40:20 -07007770 doms_cur = doms_new;
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09007771 dattr_cur = dattr_new;
Paul Jackson029190c2007-10-18 23:40:20 -07007772 ndoms_cur = ndoms_new;
Milton Miller73785472007-10-24 18:23:48 +02007773
7774 register_sched_domain_sysctl();
Srivatsa Vaddagiria1835612008-01-25 21:08:00 +01007775
Heiko Carstens712555e2008-04-28 11:33:07 +02007776 mutex_unlock(&sched_domains_mutex);
Paul Jackson029190c2007-10-18 23:40:20 -07007777}
7778
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07007779#if defined(CONFIG_SCHED_MC) || defined(CONFIG_SCHED_SMT)
Peter Zijlstrac4a88492011-04-07 14:09:42 +02007780static void reinit_sched_domains(void)
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07007781{
Gautham R Shenoy95402b32008-01-25 21:08:02 +01007782 get_online_cpus();
Max Krasnyanskydfb512e2008-08-29 13:11:41 -07007783
7784 /* Destroy domains first to force the rebuild */
7785 partition_sched_domains(0, NULL, NULL);
7786
Max Krasnyanskye761b772008-07-15 04:43:49 -07007787 rebuild_sched_domains();
Gautham R Shenoy95402b32008-01-25 21:08:02 +01007788 put_online_cpus();
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07007789}
7790
7791static ssize_t sched_power_savings_store(const char *buf, size_t count, int smt)
7792{
Gautham R Shenoyafb8a9b2008-12-18 23:26:09 +05307793 unsigned int level = 0;
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07007794
Gautham R Shenoyafb8a9b2008-12-18 23:26:09 +05307795 if (sscanf(buf, "%u", &level) != 1)
7796 return -EINVAL;
7797
7798 /*
7799 * level is always be positive so don't check for
7800 * level < POWERSAVINGS_BALANCE_NONE which is 0
7801 * What happens on 0 or 1 byte write,
7802 * need to check for count as well?
7803 */
7804
7805 if (level >= MAX_POWERSAVINGS_BALANCE_LEVELS)
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07007806 return -EINVAL;
7807
7808 if (smt)
Gautham R Shenoyafb8a9b2008-12-18 23:26:09 +05307809 sched_smt_power_savings = level;
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07007810 else
Gautham R Shenoyafb8a9b2008-12-18 23:26:09 +05307811 sched_mc_power_savings = level;
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07007812
Peter Zijlstrac4a88492011-04-07 14:09:42 +02007813 reinit_sched_domains();
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07007814
Li Zefanc70f22d2009-01-05 19:07:50 +08007815 return count;
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07007816}
7817
Adrian Bunk6707de002007-08-12 18:08:19 +02007818#ifdef CONFIG_SCHED_MC
Andi Kleenf718cd42008-07-29 22:33:52 -07007819static ssize_t sched_mc_power_savings_show(struct sysdev_class *class,
Andi Kleenc9be0a32010-01-05 12:47:58 +01007820 struct sysdev_class_attribute *attr,
Andi Kleenf718cd42008-07-29 22:33:52 -07007821 char *page)
Adrian Bunk6707de002007-08-12 18:08:19 +02007822{
7823 return sprintf(page, "%u\n", sched_mc_power_savings);
7824}
Andi Kleenf718cd42008-07-29 22:33:52 -07007825static ssize_t sched_mc_power_savings_store(struct sysdev_class *class,
Andi Kleenc9be0a32010-01-05 12:47:58 +01007826 struct sysdev_class_attribute *attr,
Adrian Bunk6707de002007-08-12 18:08:19 +02007827 const char *buf, size_t count)
7828{
7829 return sched_power_savings_store(buf, count, 0);
7830}
Andi Kleenf718cd42008-07-29 22:33:52 -07007831static SYSDEV_CLASS_ATTR(sched_mc_power_savings, 0644,
7832 sched_mc_power_savings_show,
7833 sched_mc_power_savings_store);
Adrian Bunk6707de002007-08-12 18:08:19 +02007834#endif
7835
7836#ifdef CONFIG_SCHED_SMT
Andi Kleenf718cd42008-07-29 22:33:52 -07007837static ssize_t sched_smt_power_savings_show(struct sysdev_class *dev,
Andi Kleenc9be0a32010-01-05 12:47:58 +01007838 struct sysdev_class_attribute *attr,
Andi Kleenf718cd42008-07-29 22:33:52 -07007839 char *page)
Adrian Bunk6707de002007-08-12 18:08:19 +02007840{
7841 return sprintf(page, "%u\n", sched_smt_power_savings);
7842}
Andi Kleenf718cd42008-07-29 22:33:52 -07007843static ssize_t sched_smt_power_savings_store(struct sysdev_class *dev,
Andi Kleenc9be0a32010-01-05 12:47:58 +01007844 struct sysdev_class_attribute *attr,
Adrian Bunk6707de002007-08-12 18:08:19 +02007845 const char *buf, size_t count)
7846{
7847 return sched_power_savings_store(buf, count, 1);
7848}
Andi Kleenf718cd42008-07-29 22:33:52 -07007849static SYSDEV_CLASS_ATTR(sched_smt_power_savings, 0644,
7850 sched_smt_power_savings_show,
Adrian Bunk6707de002007-08-12 18:08:19 +02007851 sched_smt_power_savings_store);
7852#endif
7853
Li Zefan39aac642009-01-05 19:18:02 +08007854int __init sched_create_sysfs_power_savings_entries(struct sysdev_class *cls)
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07007855{
7856 int err = 0;
Ingo Molnar48f24c42006-07-03 00:25:40 -07007857
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07007858#ifdef CONFIG_SCHED_SMT
7859 if (smt_capable())
7860 err = sysfs_create_file(&cls->kset.kobj,
7861 &attr_sched_smt_power_savings.attr);
7862#endif
7863#ifdef CONFIG_SCHED_MC
7864 if (!err && mc_capable())
7865 err = sysfs_create_file(&cls->kset.kobj,
7866 &attr_sched_mc_power_savings.attr);
7867#endif
7868 return err;
7869}
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +02007870#endif /* CONFIG_SCHED_MC || CONFIG_SCHED_SMT */
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07007871
Linus Torvalds1da177e2005-04-16 15:20:36 -07007872/*
Tejun Heo3a101d02010-06-08 21:40:36 +02007873 * Update cpusets according to cpu_active mask. If cpusets are
7874 * disabled, cpuset_update_active_cpus() becomes a simple wrapper
7875 * around partition_sched_domains().
Linus Torvalds1da177e2005-04-16 15:20:36 -07007876 */
Tejun Heo0b2e9182010-06-21 23:53:31 +02007877static int cpuset_cpu_active(struct notifier_block *nfb, unsigned long action,
7878 void *hcpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007879{
Tejun Heo3a101d02010-06-08 21:40:36 +02007880 switch (action & ~CPU_TASKS_FROZEN) {
Max Krasnyanskye761b772008-07-15 04:43:49 -07007881 case CPU_ONLINE:
Peter Zijlstra6ad4c182009-11-25 13:31:39 +01007882 case CPU_DOWN_FAILED:
Tejun Heo3a101d02010-06-08 21:40:36 +02007883 cpuset_update_active_cpus();
Max Krasnyanskye761b772008-07-15 04:43:49 -07007884 return NOTIFY_OK;
Max Krasnyanskye761b772008-07-15 04:43:49 -07007885 default:
7886 return NOTIFY_DONE;
7887 }
7888}
Tejun Heo3a101d02010-06-08 21:40:36 +02007889
Tejun Heo0b2e9182010-06-21 23:53:31 +02007890static int cpuset_cpu_inactive(struct notifier_block *nfb, unsigned long action,
7891 void *hcpu)
Tejun Heo3a101d02010-06-08 21:40:36 +02007892{
7893 switch (action & ~CPU_TASKS_FROZEN) {
7894 case CPU_DOWN_PREPARE:
7895 cpuset_update_active_cpus();
7896 return NOTIFY_OK;
7897 default:
7898 return NOTIFY_DONE;
7899 }
7900}
Max Krasnyanskye761b772008-07-15 04:43:49 -07007901
7902static int update_runtime(struct notifier_block *nfb,
7903 unsigned long action, void *hcpu)
7904{
Peter Zijlstra7def2be2008-06-05 14:49:58 +02007905 int cpu = (int)(long)hcpu;
7906
Linus Torvalds1da177e2005-04-16 15:20:36 -07007907 switch (action) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007908 case CPU_DOWN_PREPARE:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07007909 case CPU_DOWN_PREPARE_FROZEN:
Peter Zijlstra7def2be2008-06-05 14:49:58 +02007910 disable_runtime(cpu_rq(cpu));
Linus Torvalds1da177e2005-04-16 15:20:36 -07007911 return NOTIFY_OK;
7912
Linus Torvalds1da177e2005-04-16 15:20:36 -07007913 case CPU_DOWN_FAILED:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07007914 case CPU_DOWN_FAILED_FROZEN:
Linus Torvalds1da177e2005-04-16 15:20:36 -07007915 case CPU_ONLINE:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07007916 case CPU_ONLINE_FROZEN:
Peter Zijlstra7def2be2008-06-05 14:49:58 +02007917 enable_runtime(cpu_rq(cpu));
Max Krasnyanskye761b772008-07-15 04:43:49 -07007918 return NOTIFY_OK;
7919
Linus Torvalds1da177e2005-04-16 15:20:36 -07007920 default:
7921 return NOTIFY_DONE;
7922 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007923}
Linus Torvalds1da177e2005-04-16 15:20:36 -07007924
7925void __init sched_init_smp(void)
7926{
Rusty Russelldcc30a32008-11-25 02:35:12 +10307927 cpumask_var_t non_isolated_cpus;
7928
7929 alloc_cpumask_var(&non_isolated_cpus, GFP_KERNEL);
Yong Zhangcb5fd132009-09-14 20:20:16 +08007930 alloc_cpumask_var(&fallback_doms, GFP_KERNEL);
Nick Piggin5c1e1762006-10-03 01:14:04 -07007931
Gautham R Shenoy95402b32008-01-25 21:08:02 +01007932 get_online_cpus();
Heiko Carstens712555e2008-04-28 11:33:07 +02007933 mutex_lock(&sched_domains_mutex);
Peter Zijlstrac4a88492011-04-07 14:09:42 +02007934 init_sched_domains(cpu_active_mask);
Rusty Russelldcc30a32008-11-25 02:35:12 +10307935 cpumask_andnot(non_isolated_cpus, cpu_possible_mask, cpu_isolated_map);
7936 if (cpumask_empty(non_isolated_cpus))
7937 cpumask_set_cpu(smp_processor_id(), non_isolated_cpus);
Heiko Carstens712555e2008-04-28 11:33:07 +02007938 mutex_unlock(&sched_domains_mutex);
Gautham R Shenoy95402b32008-01-25 21:08:02 +01007939 put_online_cpus();
Max Krasnyanskye761b772008-07-15 04:43:49 -07007940
Tejun Heo3a101d02010-06-08 21:40:36 +02007941 hotcpu_notifier(cpuset_cpu_active, CPU_PRI_CPUSET_ACTIVE);
7942 hotcpu_notifier(cpuset_cpu_inactive, CPU_PRI_CPUSET_INACTIVE);
Max Krasnyanskye761b772008-07-15 04:43:49 -07007943
7944 /* RT runtime code needs to handle some hotplug events */
7945 hotcpu_notifier(update_runtime, 0);
7946
Peter Zijlstrab328ca12008-04-29 10:02:46 +02007947 init_hrtick();
Nick Piggin5c1e1762006-10-03 01:14:04 -07007948
7949 /* Move init over to a non-isolated CPU */
Rusty Russelldcc30a32008-11-25 02:35:12 +10307950 if (set_cpus_allowed_ptr(current, non_isolated_cpus) < 0)
Nick Piggin5c1e1762006-10-03 01:14:04 -07007951 BUG();
Ingo Molnar19978ca2007-11-09 22:39:38 +01007952 sched_init_granularity();
Rusty Russelldcc30a32008-11-25 02:35:12 +10307953 free_cpumask_var(non_isolated_cpus);
Rusty Russell42128232008-11-25 02:35:12 +10307954
Rusty Russell0e3900e2008-11-25 02:35:13 +10307955 init_sched_rt_class();
Linus Torvalds1da177e2005-04-16 15:20:36 -07007956}
7957#else
7958void __init sched_init_smp(void)
7959{
Ingo Molnar19978ca2007-11-09 22:39:38 +01007960 sched_init_granularity();
Linus Torvalds1da177e2005-04-16 15:20:36 -07007961}
7962#endif /* CONFIG_SMP */
7963
Arun R Bharadwajcd1bb942009-04-16 12:15:34 +05307964const_debug unsigned int sysctl_timer_migration = 1;
7965
Linus Torvalds1da177e2005-04-16 15:20:36 -07007966int in_sched_functions(unsigned long addr)
7967{
Linus Torvalds1da177e2005-04-16 15:20:36 -07007968 return in_lock_functions(addr) ||
7969 (addr >= (unsigned long)__sched_text_start
7970 && addr < (unsigned long)__sched_text_end);
7971}
7972
Jan H. Schönherracb5a9b2011-07-14 18:32:43 +02007973static void init_cfs_rq(struct cfs_rq *cfs_rq)
Ingo Molnardd41f592007-07-09 18:51:59 +02007974{
7975 cfs_rq->tasks_timeline = RB_ROOT;
Peter Zijlstra4a55bd52008-04-19 19:45:00 +02007976 INIT_LIST_HEAD(&cfs_rq->tasks);
Peter Zijlstra67e9fb22007-10-15 17:00:10 +02007977 cfs_rq->min_vruntime = (u64)(-(1LL << 20));
Peter Zijlstrac64be782011-07-11 16:28:50 +02007978#ifndef CONFIG_64BIT
7979 cfs_rq->min_vruntime_copy = cfs_rq->min_vruntime;
7980#endif
Ingo Molnardd41f592007-07-09 18:51:59 +02007981}
7982
Peter Zijlstrafa85ae22008-01-25 21:08:29 +01007983static void init_rt_rq(struct rt_rq *rt_rq, struct rq *rq)
7984{
7985 struct rt_prio_array *array;
7986 int i;
7987
7988 array = &rt_rq->active;
7989 for (i = 0; i < MAX_RT_PRIO; i++) {
7990 INIT_LIST_HEAD(array->queue + i);
7991 __clear_bit(i, array->bitmap);
7992 }
7993 /* delimiter for bitsearch: */
7994 __set_bit(MAX_RT_PRIO, array->bitmap);
7995
Jan H. Schönherracb5a9b2011-07-14 18:32:43 +02007996#if defined CONFIG_SMP
Gregory Haskinse864c492008-12-29 09:39:49 -05007997 rt_rq->highest_prio.curr = MAX_RT_PRIO;
7998 rt_rq->highest_prio.next = MAX_RT_PRIO;
Peter Zijlstrafa85ae22008-01-25 21:08:29 +01007999 rt_rq->rt_nr_migratory = 0;
Peter Zijlstrafa85ae22008-01-25 21:08:29 +01008000 rt_rq->overloaded = 0;
Dima Zavin732375c2011-07-07 17:27:59 -07008001 plist_head_init(&rt_rq->pushable_tasks);
Peter Zijlstrafa85ae22008-01-25 21:08:29 +01008002#endif
8003
8004 rt_rq->rt_time = 0;
8005 rt_rq->rt_throttled = 0;
Peter Zijlstraac086bc2008-04-19 19:44:58 +02008006 rt_rq->rt_runtime = 0;
Thomas Gleixner0986b112009-11-17 15:32:06 +01008007 raw_spin_lock_init(&rt_rq->rt_runtime_lock);
Peter Zijlstrafa85ae22008-01-25 21:08:29 +01008008}
8009
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008010#ifdef CONFIG_FAIR_GROUP_SCHED
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008011static void init_tg_cfs_entry(struct task_group *tg, struct cfs_rq *cfs_rq,
Peter Zijlstra3d4b47b2010-11-15 15:47:01 -08008012 struct sched_entity *se, int cpu,
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008013 struct sched_entity *parent)
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008014{
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008015 struct rq *rq = cpu_rq(cpu);
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008016
Jan H. Schönherracb5a9b2011-07-14 18:32:43 +02008017 cfs_rq->tg = tg;
8018 cfs_rq->rq = rq;
8019#ifdef CONFIG_SMP
8020 /* allow initial update_cfs_load() to truncate */
8021 cfs_rq->load_stamp = 1;
8022#endif
Paul Turnerab84d312011-07-21 09:43:28 -07008023 init_cfs_rq_runtime(cfs_rq);
Jan H. Schönherracb5a9b2011-07-14 18:32:43 +02008024
8025 tg->cfs_rq[cpu] = cfs_rq;
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008026 tg->se[cpu] = se;
Jan H. Schönherracb5a9b2011-07-14 18:32:43 +02008027
Yong Zhang07e06b02011-01-07 15:17:36 +08008028 /* se could be NULL for root_task_group */
Dhaval Giani354d60c2008-04-19 19:44:59 +02008029 if (!se)
8030 return;
8031
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008032 if (!parent)
8033 se->cfs_rq = &rq->cfs;
8034 else
8035 se->cfs_rq = parent->my_q;
8036
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008037 se->my_q = cfs_rq;
Paul Turner94371782010-11-15 15:47:10 -08008038 update_load_set(&se->load, 0);
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008039 se->parent = parent;
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008040}
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01008041#endif
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008042
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01008043#ifdef CONFIG_RT_GROUP_SCHED
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008044static void init_tg_rt_entry(struct task_group *tg, struct rt_rq *rt_rq,
Peter Zijlstra3d4b47b2010-11-15 15:47:01 -08008045 struct sched_rt_entity *rt_se, int cpu,
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008046 struct sched_rt_entity *parent)
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008047{
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008048 struct rq *rq = cpu_rq(cpu);
8049
Jan H. Schönherracb5a9b2011-07-14 18:32:43 +02008050 rt_rq->highest_prio.curr = MAX_RT_PRIO;
8051 rt_rq->rt_nr_boosted = 0;
8052 rt_rq->rq = rq;
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008053 rt_rq->tg = tg;
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008054
Jan H. Schönherracb5a9b2011-07-14 18:32:43 +02008055 tg->rt_rq[cpu] = rt_rq;
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008056 tg->rt_se[cpu] = rt_se;
Jan H. Schönherracb5a9b2011-07-14 18:32:43 +02008057
Dhaval Giani354d60c2008-04-19 19:44:59 +02008058 if (!rt_se)
8059 return;
8060
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008061 if (!parent)
8062 rt_se->rt_rq = &rq->rt;
8063 else
8064 rt_se->rt_rq = parent->my_q;
8065
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008066 rt_se->my_q = rt_rq;
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008067 rt_se->parent = parent;
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008068 INIT_LIST_HEAD(&rt_se->run_list);
8069}
8070#endif
8071
Linus Torvalds1da177e2005-04-16 15:20:36 -07008072void __init sched_init(void)
8073{
Ingo Molnardd41f592007-07-09 18:51:59 +02008074 int i, j;
Mike Travis434d53b2008-04-04 18:11:04 -07008075 unsigned long alloc_size = 0, ptr;
8076
8077#ifdef CONFIG_FAIR_GROUP_SCHED
8078 alloc_size += 2 * nr_cpu_ids * sizeof(void **);
8079#endif
8080#ifdef CONFIG_RT_GROUP_SCHED
8081 alloc_size += 2 * nr_cpu_ids * sizeof(void **);
8082#endif
Rusty Russelldf7c8e82009-03-19 15:22:20 +10308083#ifdef CONFIG_CPUMASK_OFFSTACK
Rusty Russell8c083f02009-03-19 15:22:20 +10308084 alloc_size += num_possible_cpus() * cpumask_size();
Rusty Russelldf7c8e82009-03-19 15:22:20 +10308085#endif
Mike Travis434d53b2008-04-04 18:11:04 -07008086 if (alloc_size) {
Pekka Enberg36b7b6d2009-06-10 23:42:36 +03008087 ptr = (unsigned long)kzalloc(alloc_size, GFP_NOWAIT);
Mike Travis434d53b2008-04-04 18:11:04 -07008088
8089#ifdef CONFIG_FAIR_GROUP_SCHED
Yong Zhang07e06b02011-01-07 15:17:36 +08008090 root_task_group.se = (struct sched_entity **)ptr;
Mike Travis434d53b2008-04-04 18:11:04 -07008091 ptr += nr_cpu_ids * sizeof(void **);
8092
Yong Zhang07e06b02011-01-07 15:17:36 +08008093 root_task_group.cfs_rq = (struct cfs_rq **)ptr;
Mike Travis434d53b2008-04-04 18:11:04 -07008094 ptr += nr_cpu_ids * sizeof(void **);
Peter Zijlstraeff766a2008-04-19 19:45:00 +02008095
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +02008096#endif /* CONFIG_FAIR_GROUP_SCHED */
Mike Travis434d53b2008-04-04 18:11:04 -07008097#ifdef CONFIG_RT_GROUP_SCHED
Yong Zhang07e06b02011-01-07 15:17:36 +08008098 root_task_group.rt_se = (struct sched_rt_entity **)ptr;
Mike Travis434d53b2008-04-04 18:11:04 -07008099 ptr += nr_cpu_ids * sizeof(void **);
8100
Yong Zhang07e06b02011-01-07 15:17:36 +08008101 root_task_group.rt_rq = (struct rt_rq **)ptr;
Peter Zijlstraeff766a2008-04-19 19:45:00 +02008102 ptr += nr_cpu_ids * sizeof(void **);
8103
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +02008104#endif /* CONFIG_RT_GROUP_SCHED */
Rusty Russelldf7c8e82009-03-19 15:22:20 +10308105#ifdef CONFIG_CPUMASK_OFFSTACK
8106 for_each_possible_cpu(i) {
8107 per_cpu(load_balance_tmpmask, i) = (void *)ptr;
8108 ptr += cpumask_size();
8109 }
8110#endif /* CONFIG_CPUMASK_OFFSTACK */
Mike Travis434d53b2008-04-04 18:11:04 -07008111 }
Ingo Molnardd41f592007-07-09 18:51:59 +02008112
Gregory Haskins57d885f2008-01-25 21:08:18 +01008113#ifdef CONFIG_SMP
8114 init_defrootdomain();
8115#endif
8116
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008117 init_rt_bandwidth(&def_rt_bandwidth,
8118 global_rt_period(), global_rt_runtime());
8119
8120#ifdef CONFIG_RT_GROUP_SCHED
Yong Zhang07e06b02011-01-07 15:17:36 +08008121 init_rt_bandwidth(&root_task_group.rt_bandwidth,
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008122 global_rt_period(), global_rt_runtime());
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +02008123#endif /* CONFIG_RT_GROUP_SCHED */
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008124
Dhaval Giani7c941432010-01-20 13:26:18 +01008125#ifdef CONFIG_CGROUP_SCHED
Yong Zhang07e06b02011-01-07 15:17:36 +08008126 list_add(&root_task_group.list, &task_groups);
8127 INIT_LIST_HEAD(&root_task_group.children);
Mike Galbraith5091faa2010-11-30 14:18:03 +01008128 autogroup_init(&init_task);
Dhaval Giani7c941432010-01-20 13:26:18 +01008129#endif /* CONFIG_CGROUP_SCHED */
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008130
KAMEZAWA Hiroyuki0a945022006-03-28 01:56:37 -08008131 for_each_possible_cpu(i) {
Ingo Molnar70b97a72006-07-03 00:25:42 -07008132 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008133
8134 rq = cpu_rq(i);
Thomas Gleixner05fa7852009-11-17 14:28:38 +01008135 raw_spin_lock_init(&rq->lock);
Nick Piggin78979862005-06-25 14:57:13 -07008136 rq->nr_running = 0;
Thomas Gleixnerdce48a82009-04-11 10:43:41 +02008137 rq->calc_load_active = 0;
8138 rq->calc_load_update = jiffies + LOAD_FREQ;
Jan H. Schönherracb5a9b2011-07-14 18:32:43 +02008139 init_cfs_rq(&rq->cfs);
Peter Zijlstrafa85ae22008-01-25 21:08:29 +01008140 init_rt_rq(&rq->rt, rq);
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008141#ifdef CONFIG_FAIR_GROUP_SCHED
Yong Zhang07e06b02011-01-07 15:17:36 +08008142 root_task_group.shares = root_task_group_load;
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008143 INIT_LIST_HEAD(&rq->leaf_cfs_rq_list);
Dhaval Giani354d60c2008-04-19 19:44:59 +02008144 /*
Yong Zhang07e06b02011-01-07 15:17:36 +08008145 * How much cpu bandwidth does root_task_group get?
Dhaval Giani354d60c2008-04-19 19:44:59 +02008146 *
8147 * In case of task-groups formed thr' the cgroup filesystem, it
8148 * gets 100% of the cpu resources in the system. This overall
8149 * system cpu resource is divided among the tasks of
Yong Zhang07e06b02011-01-07 15:17:36 +08008150 * root_task_group and its child task-groups in a fair manner,
Dhaval Giani354d60c2008-04-19 19:44:59 +02008151 * based on each entity's (task or task-group's) weight
8152 * (se->load.weight).
8153 *
Yong Zhang07e06b02011-01-07 15:17:36 +08008154 * In other words, if root_task_group has 10 tasks of weight
Dhaval Giani354d60c2008-04-19 19:44:59 +02008155 * 1024) and two child groups A0 and A1 (of weight 1024 each),
8156 * then A0's share of the cpu resource is:
8157 *
Ingo Molnar0d905bc2009-05-04 19:13:30 +02008158 * A0's bandwidth = 1024 / (10*1024 + 1024 + 1024) = 8.33%
Dhaval Giani354d60c2008-04-19 19:44:59 +02008159 *
Yong Zhang07e06b02011-01-07 15:17:36 +08008160 * We achieve this by letting root_task_group's tasks sit
8161 * directly in rq->cfs (i.e root_task_group->se[] = NULL).
Dhaval Giani354d60c2008-04-19 19:44:59 +02008162 */
Paul Turnerab84d312011-07-21 09:43:28 -07008163 init_cfs_bandwidth(&root_task_group.cfs_bandwidth);
Yong Zhang07e06b02011-01-07 15:17:36 +08008164 init_tg_cfs_entry(&root_task_group, &rq->cfs, NULL, i, NULL);
Dhaval Giani354d60c2008-04-19 19:44:59 +02008165#endif /* CONFIG_FAIR_GROUP_SCHED */
8166
8167 rq->rt.rt_runtime = def_rt_bandwidth.rt_runtime;
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01008168#ifdef CONFIG_RT_GROUP_SCHED
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008169 INIT_LIST_HEAD(&rq->leaf_rt_rq_list);
Yong Zhang07e06b02011-01-07 15:17:36 +08008170 init_tg_rt_entry(&root_task_group, &rq->rt, NULL, i, NULL);
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008171#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07008172
Ingo Molnardd41f592007-07-09 18:51:59 +02008173 for (j = 0; j < CPU_LOAD_IDX_MAX; j++)
8174 rq->cpu_load[j] = 0;
Venkatesh Pallipadifdf3e952010-05-17 18:14:43 -07008175
8176 rq->last_load_update_tick = jiffies;
8177
Linus Torvalds1da177e2005-04-16 15:20:36 -07008178#ifdef CONFIG_SMP
Nick Piggin41c7ce92005-06-25 14:57:24 -07008179 rq->sd = NULL;
Gregory Haskins57d885f2008-01-25 21:08:18 +01008180 rq->rd = NULL;
Nikhil Rao1399fa72011-05-18 10:09:39 -07008181 rq->cpu_power = SCHED_POWER_SCALE;
Gregory Haskins3f029d32009-07-29 11:08:47 -04008182 rq->post_schedule = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008183 rq->active_balance = 0;
Ingo Molnardd41f592007-07-09 18:51:59 +02008184 rq->next_balance = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008185 rq->push_cpu = 0;
Christoph Lameter0a2966b2006-09-25 23:30:51 -07008186 rq->cpu = i;
Gregory Haskins1f11eb62008-06-04 15:04:05 -04008187 rq->online = 0;
Mike Galbraitheae0c9d2009-11-10 03:50:02 +01008188 rq->idle_stamp = 0;
8189 rq->avg_idle = 2*sysctl_sched_migration_cost;
Gregory Haskinsdc938522008-01-25 21:08:26 +01008190 rq_attach_root(rq, &def_root_domain);
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07008191#ifdef CONFIG_NO_HZ
8192 rq->nohz_balance_kick = 0;
8193 init_sched_softirq_csd(&per_cpu(remote_sched_softirq_cb, i));
8194#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07008195#endif
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01008196 init_rq_hrtick(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008197 atomic_set(&rq->nr_iowait, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008198 }
8199
Peter Williams2dd73a42006-06-27 02:54:34 -07008200 set_load_weight(&init_task);
Heiko Carstensb50f60c2006-07-30 03:03:52 -07008201
Avi Kivitye107be32007-07-26 13:40:43 +02008202#ifdef CONFIG_PREEMPT_NOTIFIERS
8203 INIT_HLIST_HEAD(&init_task.preempt_notifiers);
8204#endif
8205
Christoph Lameterc9819f42006-12-10 02:20:25 -08008206#ifdef CONFIG_SMP
Carlos R. Mafra962cf362008-05-15 11:15:37 -03008207 open_softirq(SCHED_SOFTIRQ, run_rebalance_domains);
Christoph Lameterc9819f42006-12-10 02:20:25 -08008208#endif
8209
Heiko Carstensb50f60c2006-07-30 03:03:52 -07008210#ifdef CONFIG_RT_MUTEXES
Dima Zavin732375c2011-07-07 17:27:59 -07008211 plist_head_init(&init_task.pi_waiters);
Heiko Carstensb50f60c2006-07-30 03:03:52 -07008212#endif
8213
Linus Torvalds1da177e2005-04-16 15:20:36 -07008214 /*
8215 * The boot idle thread does lazy MMU switching as well:
8216 */
8217 atomic_inc(&init_mm.mm_count);
8218 enter_lazy_tlb(&init_mm, current);
8219
8220 /*
8221 * Make us the idle thread. Technically, schedule() should not be
8222 * called from this thread, however somewhere below it might be,
8223 * but because we are the idle thread, we just pick up running again
8224 * when this runqueue becomes "idle".
8225 */
8226 init_idle(current, smp_processor_id());
Thomas Gleixnerdce48a82009-04-11 10:43:41 +02008227
8228 calc_load_update = jiffies + LOAD_FREQ;
8229
Ingo Molnardd41f592007-07-09 18:51:59 +02008230 /*
8231 * During early bootup we pretend to be a normal task:
8232 */
8233 current->sched_class = &fair_sched_class;
Ingo Molnar6892b752008-02-13 14:02:36 +01008234
Rusty Russell6a7b3dc2008-11-25 02:35:04 +10308235 /* Allocate the nohz_cpu_mask if CONFIG_CPUMASK_OFFSTACK */
Rusty Russell49557e62009-11-02 20:37:20 +10308236 zalloc_cpumask_var(&nohz_cpu_mask, GFP_NOWAIT);
Rusty Russellbf4d83f2008-11-25 09:57:51 +10308237#ifdef CONFIG_SMP
Peter Zijlstra4cb98832011-04-07 14:09:58 +02008238 zalloc_cpumask_var(&sched_domains_tmpmask, GFP_NOWAIT);
Rusty Russell7d1e6a92008-11-25 02:35:09 +10308239#ifdef CONFIG_NO_HZ
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07008240 zalloc_cpumask_var(&nohz.idle_cpus_mask, GFP_NOWAIT);
8241 alloc_cpumask_var(&nohz.grp_idle_mask, GFP_NOWAIT);
8242 atomic_set(&nohz.load_balancer, nr_cpu_ids);
8243 atomic_set(&nohz.first_pick_cpu, nr_cpu_ids);
8244 atomic_set(&nohz.second_pick_cpu, nr_cpu_ids);
Rusty Russell7d1e6a92008-11-25 02:35:09 +10308245#endif
Rusty Russellbdddd292009-12-02 14:09:16 +10308246 /* May be allocated at isolcpus cmdline parse time */
8247 if (cpu_isolated_map == NULL)
8248 zalloc_cpumask_var(&cpu_isolated_map, GFP_NOWAIT);
Rusty Russellbf4d83f2008-11-25 09:57:51 +10308249#endif /* SMP */
Rusty Russell6a7b3dc2008-11-25 02:35:04 +10308250
Ingo Molnar6892b752008-02-13 14:02:36 +01008251 scheduler_running = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008252}
8253
Frederic Weisbeckerd902db12011-06-08 19:31:56 +02008254#ifdef CONFIG_DEBUG_ATOMIC_SLEEP
Frederic Weisbeckere4aafea2009-07-16 15:44:29 +02008255static inline int preempt_count_equals(int preempt_offset)
8256{
Frederic Weisbecker234da7b2009-12-16 20:21:05 +01008257 int nested = (preempt_count() & ~PREEMPT_ACTIVE) + rcu_preempt_depth();
Frederic Weisbeckere4aafea2009-07-16 15:44:29 +02008258
Arnd Bergmann4ba82162011-01-25 22:52:22 +01008259 return (nested == preempt_offset);
Frederic Weisbeckere4aafea2009-07-16 15:44:29 +02008260}
8261
Simon Kagstromd8948372009-12-23 11:08:18 +01008262void __might_sleep(const char *file, int line, int preempt_offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008263{
Linus Torvalds1da177e2005-04-16 15:20:36 -07008264 static unsigned long prev_jiffy; /* ratelimiting */
8265
Frederic Weisbeckere4aafea2009-07-16 15:44:29 +02008266 if ((preempt_count_equals(preempt_offset) && !irqs_disabled()) ||
8267 system_state != SYSTEM_RUNNING || oops_in_progress)
Ingo Molnaraef745f2008-08-28 11:34:43 +02008268 return;
8269 if (time_before(jiffies, prev_jiffy + HZ) && prev_jiffy)
8270 return;
8271 prev_jiffy = jiffies;
8272
Peter Zijlstra3df0fc52009-12-20 14:23:57 +01008273 printk(KERN_ERR
8274 "BUG: sleeping function called from invalid context at %s:%d\n",
8275 file, line);
8276 printk(KERN_ERR
8277 "in_atomic(): %d, irqs_disabled(): %d, pid: %d, name: %s\n",
8278 in_atomic(), irqs_disabled(),
8279 current->pid, current->comm);
Ingo Molnaraef745f2008-08-28 11:34:43 +02008280
8281 debug_show_held_locks(current);
8282 if (irqs_disabled())
8283 print_irqtrace_events(current);
8284 dump_stack();
Linus Torvalds1da177e2005-04-16 15:20:36 -07008285}
8286EXPORT_SYMBOL(__might_sleep);
8287#endif
8288
8289#ifdef CONFIG_MAGIC_SYSRQ
Andi Kleen3a5e4dc2007-10-15 17:00:15 +02008290static void normalize_task(struct rq *rq, struct task_struct *p)
8291{
Peter Zijlstrada7a7352011-01-17 17:03:27 +01008292 const struct sched_class *prev_class = p->sched_class;
8293 int old_prio = p->prio;
Andi Kleen3a5e4dc2007-10-15 17:00:15 +02008294 int on_rq;
Peter Zijlstra3e51f332008-05-03 18:29:28 +02008295
Peter Zijlstrafd2f4412011-04-05 17:23:44 +02008296 on_rq = p->on_rq;
Andi Kleen3a5e4dc2007-10-15 17:00:15 +02008297 if (on_rq)
8298 deactivate_task(rq, p, 0);
8299 __setscheduler(rq, p, SCHED_NORMAL, 0);
8300 if (on_rq) {
8301 activate_task(rq, p, 0);
8302 resched_task(rq->curr);
8303 }
Peter Zijlstrada7a7352011-01-17 17:03:27 +01008304
8305 check_class_changed(rq, p, prev_class, old_prio);
Andi Kleen3a5e4dc2007-10-15 17:00:15 +02008306}
8307
Linus Torvalds1da177e2005-04-16 15:20:36 -07008308void normalize_rt_tasks(void)
8309{
Ingo Molnara0f98a12007-06-17 18:37:45 +02008310 struct task_struct *g, *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008311 unsigned long flags;
Ingo Molnar70b97a72006-07-03 00:25:42 -07008312 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008313
Peter Zijlstra4cf5d772008-02-13 15:45:39 +01008314 read_lock_irqsave(&tasklist_lock, flags);
Ingo Molnara0f98a12007-06-17 18:37:45 +02008315 do_each_thread(g, p) {
Ingo Molnar178be792007-10-15 17:00:18 +02008316 /*
8317 * Only normalize user tasks:
8318 */
8319 if (!p->mm)
8320 continue;
8321
Ingo Molnardd41f592007-07-09 18:51:59 +02008322 p->se.exec_start = 0;
Ingo Molnar6cfb0d52007-08-02 17:41:40 +02008323#ifdef CONFIG_SCHEDSTATS
Lucas De Marchi41acab82010-03-10 23:37:45 -03008324 p->se.statistics.wait_start = 0;
8325 p->se.statistics.sleep_start = 0;
8326 p->se.statistics.block_start = 0;
Ingo Molnar6cfb0d52007-08-02 17:41:40 +02008327#endif
Ingo Molnardd41f592007-07-09 18:51:59 +02008328
8329 if (!rt_task(p)) {
8330 /*
8331 * Renice negative nice level userspace
8332 * tasks back to 0:
8333 */
8334 if (TASK_NICE(p) < 0 && p->mm)
8335 set_user_nice(p, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008336 continue;
Ingo Molnardd41f592007-07-09 18:51:59 +02008337 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008338
Thomas Gleixner1d615482009-11-17 14:54:03 +01008339 raw_spin_lock(&p->pi_lock);
Ingo Molnarb29739f2006-06-27 02:54:51 -07008340 rq = __task_rq_lock(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008341
Ingo Molnar178be792007-10-15 17:00:18 +02008342 normalize_task(rq, p);
Andi Kleen3a5e4dc2007-10-15 17:00:15 +02008343
Ingo Molnarb29739f2006-06-27 02:54:51 -07008344 __task_rq_unlock(rq);
Thomas Gleixner1d615482009-11-17 14:54:03 +01008345 raw_spin_unlock(&p->pi_lock);
Ingo Molnara0f98a12007-06-17 18:37:45 +02008346 } while_each_thread(g, p);
8347
Peter Zijlstra4cf5d772008-02-13 15:45:39 +01008348 read_unlock_irqrestore(&tasklist_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008349}
8350
8351#endif /* CONFIG_MAGIC_SYSRQ */
Linus Torvalds1df5c102005-09-12 07:59:21 -07008352
Jason Wessel67fc4e02010-05-20 21:04:21 -05008353#if defined(CONFIG_IA64) || defined(CONFIG_KGDB_KDB)
Linus Torvalds1df5c102005-09-12 07:59:21 -07008354/*
Jason Wessel67fc4e02010-05-20 21:04:21 -05008355 * These functions are only useful for the IA64 MCA handling, or kdb.
Linus Torvalds1df5c102005-09-12 07:59:21 -07008356 *
8357 * They can only be called when the whole system has been
8358 * stopped - every CPU needs to be quiescent, and no scheduling
8359 * activity can take place. Using them for anything else would
8360 * be a serious bug, and as a result, they aren't even visible
8361 * under any other configuration.
8362 */
8363
8364/**
8365 * curr_task - return the current task for a given cpu.
8366 * @cpu: the processor in question.
8367 *
8368 * ONLY VALID WHEN THE WHOLE SYSTEM IS STOPPED!
8369 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07008370struct task_struct *curr_task(int cpu)
Linus Torvalds1df5c102005-09-12 07:59:21 -07008371{
8372 return cpu_curr(cpu);
8373}
8374
Jason Wessel67fc4e02010-05-20 21:04:21 -05008375#endif /* defined(CONFIG_IA64) || defined(CONFIG_KGDB_KDB) */
8376
8377#ifdef CONFIG_IA64
Linus Torvalds1df5c102005-09-12 07:59:21 -07008378/**
8379 * set_curr_task - set the current task for a given cpu.
8380 * @cpu: the processor in question.
8381 * @p: the task pointer to set.
8382 *
8383 * Description: This function must only be used when non-maskable interrupts
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01008384 * are serviced on a separate stack. It allows the architecture to switch the
8385 * notion of the current task on a cpu in a non-blocking manner. This function
Linus Torvalds1df5c102005-09-12 07:59:21 -07008386 * must be called with all CPU's synchronized, and interrupts disabled, the
8387 * and caller must save the original value of the current task (see
8388 * curr_task() above) and restore that value before reenabling interrupts and
8389 * re-starting the system.
8390 *
8391 * ONLY VALID WHEN THE WHOLE SYSTEM IS STOPPED!
8392 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07008393void set_curr_task(int cpu, struct task_struct *p)
Linus Torvalds1df5c102005-09-12 07:59:21 -07008394{
8395 cpu_curr(cpu) = p;
8396}
8397
8398#endif
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008399
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008400#ifdef CONFIG_FAIR_GROUP_SCHED
8401static void free_fair_sched_group(struct task_group *tg)
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008402{
8403 int i;
8404
Paul Turnerab84d312011-07-21 09:43:28 -07008405 destroy_cfs_bandwidth(tg_cfs_bandwidth(tg));
8406
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008407 for_each_possible_cpu(i) {
8408 if (tg->cfs_rq)
8409 kfree(tg->cfs_rq[i]);
8410 if (tg->se)
8411 kfree(tg->se[i]);
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008412 }
8413
8414 kfree(tg->cfs_rq);
8415 kfree(tg->se);
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008416}
8417
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008418static
8419int alloc_fair_sched_group(struct task_group *tg, struct task_group *parent)
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008420{
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008421 struct cfs_rq *cfs_rq;
Li Zefaneab17222008-10-29 17:03:22 +08008422 struct sched_entity *se;
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008423 int i;
8424
Mike Travis434d53b2008-04-04 18:11:04 -07008425 tg->cfs_rq = kzalloc(sizeof(cfs_rq) * nr_cpu_ids, GFP_KERNEL);
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008426 if (!tg->cfs_rq)
8427 goto err;
Mike Travis434d53b2008-04-04 18:11:04 -07008428 tg->se = kzalloc(sizeof(se) * nr_cpu_ids, GFP_KERNEL);
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008429 if (!tg->se)
8430 goto err;
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01008431
8432 tg->shares = NICE_0_LOAD;
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008433
Paul Turnerab84d312011-07-21 09:43:28 -07008434 init_cfs_bandwidth(tg_cfs_bandwidth(tg));
8435
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008436 for_each_possible_cpu(i) {
Li Zefaneab17222008-10-29 17:03:22 +08008437 cfs_rq = kzalloc_node(sizeof(struct cfs_rq),
8438 GFP_KERNEL, cpu_to_node(i));
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008439 if (!cfs_rq)
8440 goto err;
8441
Li Zefaneab17222008-10-29 17:03:22 +08008442 se = kzalloc_node(sizeof(struct sched_entity),
8443 GFP_KERNEL, cpu_to_node(i));
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008444 if (!se)
Phil Carmodydfc12eb2009-12-10 14:29:37 +02008445 goto err_free_rq;
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008446
Jan H. Schönherracb5a9b2011-07-14 18:32:43 +02008447 init_cfs_rq(cfs_rq);
Peter Zijlstra3d4b47b2010-11-15 15:47:01 -08008448 init_tg_cfs_entry(tg, cfs_rq, se, i, parent->se[i]);
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008449 }
8450
8451 return 1;
8452
Peter Zijlstra49246272010-10-17 21:46:10 +02008453err_free_rq:
Phil Carmodydfc12eb2009-12-10 14:29:37 +02008454 kfree(cfs_rq);
Peter Zijlstra49246272010-10-17 21:46:10 +02008455err:
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008456 return 0;
8457}
8458
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008459static inline void unregister_fair_sched_group(struct task_group *tg, int cpu)
8460{
Peter Zijlstra3d4b47b2010-11-15 15:47:01 -08008461 struct rq *rq = cpu_rq(cpu);
8462 unsigned long flags;
Peter Zijlstra3d4b47b2010-11-15 15:47:01 -08008463
8464 /*
8465 * Only empty task groups can be destroyed; so we can speculatively
8466 * check on_list without danger of it being re-added.
8467 */
8468 if (!tg->cfs_rq[cpu]->on_list)
8469 return;
8470
8471 raw_spin_lock_irqsave(&rq->lock, flags);
Paul Turner822bc182010-11-29 16:55:40 -08008472 list_del_leaf_cfs_rq(tg->cfs_rq[cpu]);
Peter Zijlstra3d4b47b2010-11-15 15:47:01 -08008473 raw_spin_unlock_irqrestore(&rq->lock, flags);
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008474}
Jan Schoenherr5f817d62011-07-13 20:13:31 +02008475#else /* !CONFIG_FAIR_GROUP_SCHED */
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008476static inline void free_fair_sched_group(struct task_group *tg)
8477{
8478}
8479
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008480static inline
8481int alloc_fair_sched_group(struct task_group *tg, struct task_group *parent)
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008482{
8483 return 1;
8484}
8485
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008486static inline void unregister_fair_sched_group(struct task_group *tg, int cpu)
8487{
8488}
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +02008489#endif /* CONFIG_FAIR_GROUP_SCHED */
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01008490
8491#ifdef CONFIG_RT_GROUP_SCHED
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008492static void free_rt_sched_group(struct task_group *tg)
8493{
8494 int i;
8495
Bianca Lutz99bc5242011-07-13 20:13:36 +02008496 if (tg->rt_se)
8497 destroy_rt_bandwidth(&tg->rt_bandwidth);
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008498
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008499 for_each_possible_cpu(i) {
8500 if (tg->rt_rq)
8501 kfree(tg->rt_rq[i]);
8502 if (tg->rt_se)
8503 kfree(tg->rt_se[i]);
8504 }
8505
8506 kfree(tg->rt_rq);
8507 kfree(tg->rt_se);
8508}
8509
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008510static
8511int alloc_rt_sched_group(struct task_group *tg, struct task_group *parent)
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008512{
8513 struct rt_rq *rt_rq;
Li Zefaneab17222008-10-29 17:03:22 +08008514 struct sched_rt_entity *rt_se;
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008515 int i;
8516
Mike Travis434d53b2008-04-04 18:11:04 -07008517 tg->rt_rq = kzalloc(sizeof(rt_rq) * nr_cpu_ids, GFP_KERNEL);
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008518 if (!tg->rt_rq)
8519 goto err;
Mike Travis434d53b2008-04-04 18:11:04 -07008520 tg->rt_se = kzalloc(sizeof(rt_se) * nr_cpu_ids, GFP_KERNEL);
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008521 if (!tg->rt_se)
8522 goto err;
8523
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008524 init_rt_bandwidth(&tg->rt_bandwidth,
8525 ktime_to_ns(def_rt_bandwidth.rt_period), 0);
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008526
8527 for_each_possible_cpu(i) {
Li Zefaneab17222008-10-29 17:03:22 +08008528 rt_rq = kzalloc_node(sizeof(struct rt_rq),
8529 GFP_KERNEL, cpu_to_node(i));
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008530 if (!rt_rq)
8531 goto err;
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008532
Li Zefaneab17222008-10-29 17:03:22 +08008533 rt_se = kzalloc_node(sizeof(struct sched_rt_entity),
8534 GFP_KERNEL, cpu_to_node(i));
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008535 if (!rt_se)
Phil Carmodydfc12eb2009-12-10 14:29:37 +02008536 goto err_free_rq;
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008537
Jan H. Schönherracb5a9b2011-07-14 18:32:43 +02008538 init_rt_rq(rt_rq, cpu_rq(i));
8539 rt_rq->rt_runtime = tg->rt_bandwidth.rt_runtime;
Peter Zijlstra3d4b47b2010-11-15 15:47:01 -08008540 init_tg_rt_entry(tg, rt_rq, rt_se, i, parent->rt_se[i]);
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008541 }
8542
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008543 return 1;
8544
Peter Zijlstra49246272010-10-17 21:46:10 +02008545err_free_rq:
Phil Carmodydfc12eb2009-12-10 14:29:37 +02008546 kfree(rt_rq);
Peter Zijlstra49246272010-10-17 21:46:10 +02008547err:
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008548 return 0;
8549}
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +02008550#else /* !CONFIG_RT_GROUP_SCHED */
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008551static inline void free_rt_sched_group(struct task_group *tg)
8552{
8553}
8554
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008555static inline
8556int alloc_rt_sched_group(struct task_group *tg, struct task_group *parent)
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008557{
8558 return 1;
8559}
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +02008560#endif /* CONFIG_RT_GROUP_SCHED */
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008561
Dhaval Giani7c941432010-01-20 13:26:18 +01008562#ifdef CONFIG_CGROUP_SCHED
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008563static void free_sched_group(struct task_group *tg)
8564{
8565 free_fair_sched_group(tg);
8566 free_rt_sched_group(tg);
Mike Galbraithe9aa1dd2011-01-05 11:11:25 +01008567 autogroup_free(tg);
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008568 kfree(tg);
8569}
8570
8571/* allocate runqueue etc for a new task group */
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008572struct task_group *sched_create_group(struct task_group *parent)
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008573{
8574 struct task_group *tg;
8575 unsigned long flags;
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008576
8577 tg = kzalloc(sizeof(*tg), GFP_KERNEL);
8578 if (!tg)
8579 return ERR_PTR(-ENOMEM);
8580
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008581 if (!alloc_fair_sched_group(tg, parent))
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008582 goto err;
8583
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008584 if (!alloc_rt_sched_group(tg, parent))
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008585 goto err;
8586
Peter Zijlstra8ed36992008-02-13 15:45:39 +01008587 spin_lock_irqsave(&task_group_lock, flags);
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008588 list_add_rcu(&tg->list, &task_groups);
Peter Zijlstraf473aa52008-04-19 19:45:00 +02008589
8590 WARN_ON(!parent); /* root should already exist */
8591
8592 tg->parent = parent;
Peter Zijlstraf473aa52008-04-19 19:45:00 +02008593 INIT_LIST_HEAD(&tg->children);
Zhang, Yanmin09f27242030-08-14 15:56:40 +08008594 list_add_rcu(&tg->siblings, &parent->children);
Peter Zijlstra8ed36992008-02-13 15:45:39 +01008595 spin_unlock_irqrestore(&task_group_lock, flags);
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008596
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02008597 return tg;
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008598
8599err:
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008600 free_sched_group(tg);
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008601 return ERR_PTR(-ENOMEM);
8602}
8603
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02008604/* rcu callback to free various structures associated with a task group */
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008605static void free_sched_group_rcu(struct rcu_head *rhp)
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008606{
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008607 /* now it should be safe to free those cfs_rqs */
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008608 free_sched_group(container_of(rhp, struct task_group, rcu));
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008609}
8610
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02008611/* Destroy runqueue etc associated with a task group */
Ingo Molnar4cf86d72007-10-15 17:00:14 +02008612void sched_destroy_group(struct task_group *tg)
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008613{
Peter Zijlstra8ed36992008-02-13 15:45:39 +01008614 unsigned long flags;
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02008615 int i;
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008616
Peter Zijlstra3d4b47b2010-11-15 15:47:01 -08008617 /* end participation in shares distribution */
8618 for_each_possible_cpu(i)
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008619 unregister_fair_sched_group(tg, i);
Peter Zijlstra3d4b47b2010-11-15 15:47:01 -08008620
8621 spin_lock_irqsave(&task_group_lock, flags);
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008622 list_del_rcu(&tg->list);
Peter Zijlstraf473aa52008-04-19 19:45:00 +02008623 list_del_rcu(&tg->siblings);
Peter Zijlstra8ed36992008-02-13 15:45:39 +01008624 spin_unlock_irqrestore(&task_group_lock, flags);
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02008625
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02008626 /* wait for possible concurrent references to cfs_rqs complete */
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008627 call_rcu(&tg->rcu, free_sched_group_rcu);
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008628}
8629
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02008630/* change task's runqueue when it moves between groups.
Ingo Molnar3a252012007-10-15 17:00:12 +02008631 * The caller of this function should have put the task in its new group
8632 * by now. This function just updates tsk->se.cfs_rq and tsk->se.parent to
8633 * reflect its new group.
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02008634 */
8635void sched_move_task(struct task_struct *tsk)
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008636{
8637 int on_rq, running;
8638 unsigned long flags;
8639 struct rq *rq;
8640
8641 rq = task_rq_lock(tsk, &flags);
8642
Dmitry Adamushko051a1d12007-12-18 15:21:13 +01008643 running = task_current(rq, tsk);
Peter Zijlstrafd2f4412011-04-05 17:23:44 +02008644 on_rq = tsk->on_rq;
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008645
Hiroshi Shimamoto0e1f3482008-03-10 11:01:20 -07008646 if (on_rq)
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008647 dequeue_task(rq, tsk, 0);
Hiroshi Shimamoto0e1f3482008-03-10 11:01:20 -07008648 if (unlikely(running))
8649 tsk->sched_class->put_prev_task(rq, tsk);
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008650
Peter Zijlstra810b3812008-02-29 15:21:01 -05008651#ifdef CONFIG_FAIR_GROUP_SCHED
Peter Zijlstrab2b5ce02010-10-15 15:24:15 +02008652 if (tsk->sched_class->task_move_group)
8653 tsk->sched_class->task_move_group(tsk, on_rq);
8654 else
Peter Zijlstra810b3812008-02-29 15:21:01 -05008655#endif
Peter Zijlstrab2b5ce02010-10-15 15:24:15 +02008656 set_task_rq(tsk, task_cpu(tsk));
Peter Zijlstra810b3812008-02-29 15:21:01 -05008657
Hiroshi Shimamoto0e1f3482008-03-10 11:01:20 -07008658 if (unlikely(running))
8659 tsk->sched_class->set_curr_task(rq);
8660 if (on_rq)
Peter Zijlstra371fd7e2010-03-24 16:38:48 +01008661 enqueue_task(rq, tsk, 0);
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008662
Peter Zijlstra0122ec52011-04-05 17:23:51 +02008663 task_rq_unlock(rq, tsk, &flags);
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008664}
Dhaval Giani7c941432010-01-20 13:26:18 +01008665#endif /* CONFIG_CGROUP_SCHED */
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008666
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01008667#ifdef CONFIG_FAIR_GROUP_SCHED
Peter Zijlstra8ed36992008-02-13 15:45:39 +01008668static DEFINE_MUTEX(shares_mutex);
8669
Ingo Molnar4cf86d72007-10-15 17:00:14 +02008670int sched_group_set_shares(struct task_group *tg, unsigned long shares)
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008671{
8672 int i;
Peter Zijlstra8ed36992008-02-13 15:45:39 +01008673 unsigned long flags;
Ingo Molnarc61935f2008-01-22 11:24:58 +01008674
Peter Zijlstra62fb1852008-02-25 17:34:02 +01008675 /*
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008676 * We can't change the weight of the root cgroup.
8677 */
8678 if (!tg->se[0])
8679 return -EINVAL;
8680
Mike Galbraithcd622872011-06-04 15:03:20 +02008681 shares = clamp(shares, scale_load(MIN_SHARES), scale_load(MAX_SHARES));
Peter Zijlstra62fb1852008-02-25 17:34:02 +01008682
Peter Zijlstra8ed36992008-02-13 15:45:39 +01008683 mutex_lock(&shares_mutex);
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02008684 if (tg->shares == shares)
Dhaval Giani5cb350b2007-10-15 17:00:14 +02008685 goto done;
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008686
Srivatsa Vaddagiri6b2d7702008-01-25 21:08:00 +01008687 tg->shares = shares;
Peter Zijlstrac09595f2008-06-27 13:41:14 +02008688 for_each_possible_cpu(i) {
Paul Turner94371782010-11-15 15:47:10 -08008689 struct rq *rq = cpu_rq(i);
8690 struct sched_entity *se;
8691
8692 se = tg->se[i];
8693 /* Propagate contribution to hierarchy */
8694 raw_spin_lock_irqsave(&rq->lock, flags);
8695 for_each_sched_entity(se)
Paul Turner6d5ab292011-01-21 20:45:01 -08008696 update_cfs_shares(group_cfs_rq(se));
Paul Turner94371782010-11-15 15:47:10 -08008697 raw_spin_unlock_irqrestore(&rq->lock, flags);
Peter Zijlstrac09595f2008-06-27 13:41:14 +02008698 }
Srivatsa Vaddagiri6b2d7702008-01-25 21:08:00 +01008699
Dhaval Giani5cb350b2007-10-15 17:00:14 +02008700done:
Peter Zijlstra8ed36992008-02-13 15:45:39 +01008701 mutex_unlock(&shares_mutex);
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02008702 return 0;
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008703}
8704
Dhaval Giani5cb350b2007-10-15 17:00:14 +02008705unsigned long sched_group_shares(struct task_group *tg)
8706{
8707 return tg->shares;
8708}
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01008709#endif
Dhaval Giani5cb350b2007-10-15 17:00:14 +02008710
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01008711#ifdef CONFIG_RT_GROUP_SCHED
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008712/*
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01008713 * Ensure that the real time constraints are schedulable.
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008714 */
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01008715static DEFINE_MUTEX(rt_constraints_mutex);
8716
8717static unsigned long to_ratio(u64 period, u64 runtime)
8718{
8719 if (runtime == RUNTIME_INF)
Peter Zijlstra9a7e0b12008-08-19 12:33:06 +02008720 return 1ULL << 20;
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01008721
Peter Zijlstra9a7e0b12008-08-19 12:33:06 +02008722 return div64_u64(runtime << 20, period);
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01008723}
8724
Dhaval Giani521f1a242008-02-28 15:21:56 +05308725/* Must be called with tasklist_lock held */
8726static inline int tg_has_rt_tasks(struct task_group *tg)
8727{
8728 struct task_struct *g, *p;
Peter Zijlstra9a7e0b12008-08-19 12:33:06 +02008729
Dhaval Giani521f1a242008-02-28 15:21:56 +05308730 do_each_thread(g, p) {
8731 if (rt_task(p) && rt_rq_of_se(&p->rt)->tg == tg)
8732 return 1;
8733 } while_each_thread(g, p);
Peter Zijlstra9a7e0b12008-08-19 12:33:06 +02008734
Dhaval Giani521f1a242008-02-28 15:21:56 +05308735 return 0;
8736}
8737
Peter Zijlstra9a7e0b12008-08-19 12:33:06 +02008738struct rt_schedulable_data {
8739 struct task_group *tg;
8740 u64 rt_period;
8741 u64 rt_runtime;
8742};
8743
8744static int tg_schedulable(struct task_group *tg, void *data)
8745{
8746 struct rt_schedulable_data *d = data;
8747 struct task_group *child;
8748 unsigned long total, sum = 0;
8749 u64 period, runtime;
8750
8751 period = ktime_to_ns(tg->rt_bandwidth.rt_period);
8752 runtime = tg->rt_bandwidth.rt_runtime;
8753
8754 if (tg == d->tg) {
8755 period = d->rt_period;
8756 runtime = d->rt_runtime;
8757 }
8758
Peter Zijlstra4653f802008-09-23 15:33:44 +02008759 /*
8760 * Cannot have more runtime than the period.
8761 */
8762 if (runtime > period && runtime != RUNTIME_INF)
8763 return -EINVAL;
8764
8765 /*
8766 * Ensure we don't starve existing RT tasks.
8767 */
Peter Zijlstra9a7e0b12008-08-19 12:33:06 +02008768 if (rt_bandwidth_enabled() && !runtime && tg_has_rt_tasks(tg))
8769 return -EBUSY;
8770
8771 total = to_ratio(period, runtime);
8772
Peter Zijlstra4653f802008-09-23 15:33:44 +02008773 /*
8774 * Nobody can have more than the global setting allows.
8775 */
8776 if (total > to_ratio(global_rt_period(), global_rt_runtime()))
8777 return -EINVAL;
8778
8779 /*
8780 * The sum of our children's runtime should not exceed our own.
8781 */
Peter Zijlstra9a7e0b12008-08-19 12:33:06 +02008782 list_for_each_entry_rcu(child, &tg->children, siblings) {
8783 period = ktime_to_ns(child->rt_bandwidth.rt_period);
8784 runtime = child->rt_bandwidth.rt_runtime;
8785
8786 if (child == d->tg) {
8787 period = d->rt_period;
8788 runtime = d->rt_runtime;
8789 }
8790
8791 sum += to_ratio(period, runtime);
8792 }
8793
8794 if (sum > total)
8795 return -EINVAL;
8796
8797 return 0;
8798}
8799
8800static int __rt_schedulable(struct task_group *tg, u64 period, u64 runtime)
8801{
8802 struct rt_schedulable_data data = {
8803 .tg = tg,
8804 .rt_period = period,
8805 .rt_runtime = runtime,
8806 };
8807
8808 return walk_tg_tree(tg_schedulable, tg_nop, &data);
8809}
8810
Paul Turnerab84d312011-07-21 09:43:28 -07008811static int tg_set_rt_bandwidth(struct task_group *tg,
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008812 u64 rt_period, u64 rt_runtime)
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008813{
Peter Zijlstraac086bc2008-04-19 19:44:58 +02008814 int i, err = 0;
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01008815
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01008816 mutex_lock(&rt_constraints_mutex);
Dhaval Giani521f1a242008-02-28 15:21:56 +05308817 read_lock(&tasklist_lock);
Peter Zijlstra9a7e0b12008-08-19 12:33:06 +02008818 err = __rt_schedulable(tg, rt_period, rt_runtime);
8819 if (err)
Dhaval Giani521f1a242008-02-28 15:21:56 +05308820 goto unlock;
Peter Zijlstraac086bc2008-04-19 19:44:58 +02008821
Thomas Gleixner0986b112009-11-17 15:32:06 +01008822 raw_spin_lock_irq(&tg->rt_bandwidth.rt_runtime_lock);
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008823 tg->rt_bandwidth.rt_period = ns_to_ktime(rt_period);
8824 tg->rt_bandwidth.rt_runtime = rt_runtime;
Peter Zijlstraac086bc2008-04-19 19:44:58 +02008825
8826 for_each_possible_cpu(i) {
8827 struct rt_rq *rt_rq = tg->rt_rq[i];
8828
Thomas Gleixner0986b112009-11-17 15:32:06 +01008829 raw_spin_lock(&rt_rq->rt_runtime_lock);
Peter Zijlstraac086bc2008-04-19 19:44:58 +02008830 rt_rq->rt_runtime = rt_runtime;
Thomas Gleixner0986b112009-11-17 15:32:06 +01008831 raw_spin_unlock(&rt_rq->rt_runtime_lock);
Peter Zijlstraac086bc2008-04-19 19:44:58 +02008832 }
Thomas Gleixner0986b112009-11-17 15:32:06 +01008833 raw_spin_unlock_irq(&tg->rt_bandwidth.rt_runtime_lock);
Peter Zijlstra49246272010-10-17 21:46:10 +02008834unlock:
Dhaval Giani521f1a242008-02-28 15:21:56 +05308835 read_unlock(&tasklist_lock);
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01008836 mutex_unlock(&rt_constraints_mutex);
8837
8838 return err;
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008839}
8840
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008841int sched_group_set_rt_runtime(struct task_group *tg, long rt_runtime_us)
8842{
8843 u64 rt_runtime, rt_period;
8844
8845 rt_period = ktime_to_ns(tg->rt_bandwidth.rt_period);
8846 rt_runtime = (u64)rt_runtime_us * NSEC_PER_USEC;
8847 if (rt_runtime_us < 0)
8848 rt_runtime = RUNTIME_INF;
8849
Paul Turnerab84d312011-07-21 09:43:28 -07008850 return tg_set_rt_bandwidth(tg, rt_period, rt_runtime);
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008851}
8852
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01008853long sched_group_rt_runtime(struct task_group *tg)
8854{
8855 u64 rt_runtime_us;
8856
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008857 if (tg->rt_bandwidth.rt_runtime == RUNTIME_INF)
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01008858 return -1;
8859
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008860 rt_runtime_us = tg->rt_bandwidth.rt_runtime;
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01008861 do_div(rt_runtime_us, NSEC_PER_USEC);
8862 return rt_runtime_us;
8863}
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008864
8865int sched_group_set_rt_period(struct task_group *tg, long rt_period_us)
8866{
8867 u64 rt_runtime, rt_period;
8868
8869 rt_period = (u64)rt_period_us * NSEC_PER_USEC;
8870 rt_runtime = tg->rt_bandwidth.rt_runtime;
8871
Raistlin619b0482008-06-26 18:54:09 +02008872 if (rt_period == 0)
8873 return -EINVAL;
8874
Paul Turnerab84d312011-07-21 09:43:28 -07008875 return tg_set_rt_bandwidth(tg, rt_period, rt_runtime);
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008876}
8877
8878long sched_group_rt_period(struct task_group *tg)
8879{
8880 u64 rt_period_us;
8881
8882 rt_period_us = ktime_to_ns(tg->rt_bandwidth.rt_period);
8883 do_div(rt_period_us, NSEC_PER_USEC);
8884 return rt_period_us;
8885}
8886
8887static int sched_rt_global_constraints(void)
8888{
Peter Zijlstra4653f802008-09-23 15:33:44 +02008889 u64 runtime, period;
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008890 int ret = 0;
8891
Hiroshi Shimamotoec5d4982008-09-10 17:00:19 -07008892 if (sysctl_sched_rt_period <= 0)
8893 return -EINVAL;
8894
Peter Zijlstra4653f802008-09-23 15:33:44 +02008895 runtime = global_rt_runtime();
8896 period = global_rt_period();
8897
8898 /*
8899 * Sanity check on the sysctl variables.
8900 */
8901 if (runtime > period && runtime != RUNTIME_INF)
8902 return -EINVAL;
Peter Zijlstra10b612f2008-06-19 14:22:27 +02008903
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008904 mutex_lock(&rt_constraints_mutex);
Peter Zijlstra9a7e0b12008-08-19 12:33:06 +02008905 read_lock(&tasklist_lock);
Peter Zijlstra4653f802008-09-23 15:33:44 +02008906 ret = __rt_schedulable(NULL, 0, 0);
Peter Zijlstra9a7e0b12008-08-19 12:33:06 +02008907 read_unlock(&tasklist_lock);
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008908 mutex_unlock(&rt_constraints_mutex);
8909
8910 return ret;
8911}
Dhaval Giani54e99122009-02-27 15:13:54 +05308912
8913int sched_rt_can_attach(struct task_group *tg, struct task_struct *tsk)
8914{
8915 /* Don't accept realtime tasks when there is no way for them to run */
8916 if (rt_task(tsk) && tg->rt_bandwidth.rt_runtime == 0)
8917 return 0;
8918
8919 return 1;
8920}
8921
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +02008922#else /* !CONFIG_RT_GROUP_SCHED */
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008923static int sched_rt_global_constraints(void)
8924{
Peter Zijlstraac086bc2008-04-19 19:44:58 +02008925 unsigned long flags;
8926 int i;
8927
Hiroshi Shimamotoec5d4982008-09-10 17:00:19 -07008928 if (sysctl_sched_rt_period <= 0)
8929 return -EINVAL;
8930
Peter Zijlstra60aa6052009-05-05 17:50:21 +02008931 /*
8932 * There's always some RT tasks in the root group
8933 * -- migration, kstopmachine etc..
8934 */
8935 if (sysctl_sched_rt_runtime == 0)
8936 return -EBUSY;
8937
Thomas Gleixner0986b112009-11-17 15:32:06 +01008938 raw_spin_lock_irqsave(&def_rt_bandwidth.rt_runtime_lock, flags);
Peter Zijlstraac086bc2008-04-19 19:44:58 +02008939 for_each_possible_cpu(i) {
8940 struct rt_rq *rt_rq = &cpu_rq(i)->rt;
8941
Thomas Gleixner0986b112009-11-17 15:32:06 +01008942 raw_spin_lock(&rt_rq->rt_runtime_lock);
Peter Zijlstraac086bc2008-04-19 19:44:58 +02008943 rt_rq->rt_runtime = global_rt_runtime();
Thomas Gleixner0986b112009-11-17 15:32:06 +01008944 raw_spin_unlock(&rt_rq->rt_runtime_lock);
Peter Zijlstraac086bc2008-04-19 19:44:58 +02008945 }
Thomas Gleixner0986b112009-11-17 15:32:06 +01008946 raw_spin_unlock_irqrestore(&def_rt_bandwidth.rt_runtime_lock, flags);
Peter Zijlstraac086bc2008-04-19 19:44:58 +02008947
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008948 return 0;
8949}
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +02008950#endif /* CONFIG_RT_GROUP_SCHED */
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008951
8952int sched_rt_handler(struct ctl_table *table, int write,
Alexey Dobriyan8d65af72009-09-23 15:57:19 -07008953 void __user *buffer, size_t *lenp,
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008954 loff_t *ppos)
8955{
8956 int ret;
8957 int old_period, old_runtime;
8958 static DEFINE_MUTEX(mutex);
8959
8960 mutex_lock(&mutex);
8961 old_period = sysctl_sched_rt_period;
8962 old_runtime = sysctl_sched_rt_runtime;
8963
Alexey Dobriyan8d65af72009-09-23 15:57:19 -07008964 ret = proc_dointvec(table, write, buffer, lenp, ppos);
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008965
8966 if (!ret && write) {
8967 ret = sched_rt_global_constraints();
8968 if (ret) {
8969 sysctl_sched_rt_period = old_period;
8970 sysctl_sched_rt_runtime = old_runtime;
8971 } else {
8972 def_rt_bandwidth.rt_runtime = global_rt_runtime();
8973 def_rt_bandwidth.rt_period =
8974 ns_to_ktime(global_rt_period());
8975 }
8976 }
8977 mutex_unlock(&mutex);
8978
8979 return ret;
8980}
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008981
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01008982#ifdef CONFIG_CGROUP_SCHED
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008983
8984/* return corresponding task_group object of a cgroup */
Paul Menage2b01dfe2007-10-24 18:23:50 +02008985static inline struct task_group *cgroup_tg(struct cgroup *cgrp)
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008986{
Paul Menage2b01dfe2007-10-24 18:23:50 +02008987 return container_of(cgroup_subsys_state(cgrp, cpu_cgroup_subsys_id),
8988 struct task_group, css);
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008989}
8990
8991static struct cgroup_subsys_state *
Paul Menage2b01dfe2007-10-24 18:23:50 +02008992cpu_cgroup_create(struct cgroup_subsys *ss, struct cgroup *cgrp)
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008993{
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008994 struct task_group *tg, *parent;
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008995
Paul Menage2b01dfe2007-10-24 18:23:50 +02008996 if (!cgrp->parent) {
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008997 /* This is early initialization for the top cgroup */
Yong Zhang07e06b02011-01-07 15:17:36 +08008998 return &root_task_group.css;
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008999 }
9000
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02009001 parent = cgroup_tg(cgrp->parent);
9002 tg = sched_create_group(parent);
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07009003 if (IS_ERR(tg))
9004 return ERR_PTR(-ENOMEM);
9005
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07009006 return &tg->css;
9007}
9008
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01009009static void
9010cpu_cgroup_destroy(struct cgroup_subsys *ss, struct cgroup *cgrp)
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07009011{
Paul Menage2b01dfe2007-10-24 18:23:50 +02009012 struct task_group *tg = cgroup_tg(cgrp);
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07009013
9014 sched_destroy_group(tg);
9015}
9016
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01009017static int
Ben Blumbe367d02009-09-23 15:56:31 -07009018cpu_cgroup_can_attach_task(struct cgroup *cgrp, struct task_struct *tsk)
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07009019{
Peter Zijlstrab68aa232008-02-13 15:45:40 +01009020#ifdef CONFIG_RT_GROUP_SCHED
Dhaval Giani54e99122009-02-27 15:13:54 +05309021 if (!sched_rt_can_attach(cgroup_tg(cgrp), tsk))
Peter Zijlstrab68aa232008-02-13 15:45:40 +01009022 return -EINVAL;
9023#else
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07009024 /* We don't support RT-tasks being in separate groups */
9025 if (tsk->sched_class != &fair_sched_class)
9026 return -EINVAL;
Peter Zijlstrab68aa232008-02-13 15:45:40 +01009027#endif
Ben Blumbe367d02009-09-23 15:56:31 -07009028 return 0;
9029}
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07009030
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07009031static void
Ben Blumf780bdb2011-05-26 16:25:19 -07009032cpu_cgroup_attach_task(struct cgroup *cgrp, struct task_struct *tsk)
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07009033{
9034 sched_move_task(tsk);
9035}
9036
Peter Zijlstra068c5cc2011-01-19 12:26:11 +01009037static void
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01009038cpu_cgroup_exit(struct cgroup_subsys *ss, struct cgroup *cgrp,
9039 struct cgroup *old_cgrp, struct task_struct *task)
Peter Zijlstra068c5cc2011-01-19 12:26:11 +01009040{
9041 /*
9042 * cgroup_exit() is called in the copy_process() failure path.
9043 * Ignore this case since the task hasn't ran yet, this avoids
9044 * trying to poke a half freed task state from generic code.
9045 */
9046 if (!(task->flags & PF_EXITING))
9047 return;
9048
9049 sched_move_task(task);
9050}
9051
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01009052#ifdef CONFIG_FAIR_GROUP_SCHED
Paul Menagef4c753b2008-04-29 00:59:56 -07009053static int cpu_shares_write_u64(struct cgroup *cgrp, struct cftype *cftype,
Paul Menage2b01dfe2007-10-24 18:23:50 +02009054 u64 shareval)
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07009055{
Nikhil Raoc8b28112011-05-18 14:37:48 -07009056 return sched_group_set_shares(cgroup_tg(cgrp), scale_load(shareval));
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07009057}
9058
Paul Menagef4c753b2008-04-29 00:59:56 -07009059static u64 cpu_shares_read_u64(struct cgroup *cgrp, struct cftype *cft)
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07009060{
Paul Menage2b01dfe2007-10-24 18:23:50 +02009061 struct task_group *tg = cgroup_tg(cgrp);
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07009062
Nikhil Raoc8b28112011-05-18 14:37:48 -07009063 return (u64) scale_load_down(tg->shares);
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07009064}
Paul Turnerab84d312011-07-21 09:43:28 -07009065
9066#ifdef CONFIG_CFS_BANDWIDTH
9067const u64 max_cfs_quota_period = 1 * NSEC_PER_SEC; /* 1s */
9068const u64 min_cfs_quota_period = 1 * NSEC_PER_MSEC; /* 1ms */
9069
9070static int tg_set_cfs_bandwidth(struct task_group *tg, u64 period, u64 quota)
9071{
9072 int i;
9073 struct cfs_bandwidth *cfs_b = tg_cfs_bandwidth(tg);
9074 static DEFINE_MUTEX(mutex);
9075
9076 if (tg == &root_task_group)
9077 return -EINVAL;
9078
9079 /*
9080 * Ensure we have at some amount of bandwidth every period. This is
9081 * to prevent reaching a state of large arrears when throttled via
9082 * entity_tick() resulting in prolonged exit starvation.
9083 */
9084 if (quota < min_cfs_quota_period || period < min_cfs_quota_period)
9085 return -EINVAL;
9086
9087 /*
9088 * Likewise, bound things on the otherside by preventing insane quota
9089 * periods. This also allows us to normalize in computing quota
9090 * feasibility.
9091 */
9092 if (period > max_cfs_quota_period)
9093 return -EINVAL;
9094
9095 mutex_lock(&mutex);
9096 raw_spin_lock_irq(&cfs_b->lock);
9097 cfs_b->period = ns_to_ktime(period);
9098 cfs_b->quota = quota;
9099 raw_spin_unlock_irq(&cfs_b->lock);
9100
9101 for_each_possible_cpu(i) {
9102 struct cfs_rq *cfs_rq = tg->cfs_rq[i];
9103 struct rq *rq = rq_of(cfs_rq);
9104
9105 raw_spin_lock_irq(&rq->lock);
9106 cfs_rq->runtime_enabled = quota != RUNTIME_INF;
9107 cfs_rq->runtime_remaining = 0;
9108 raw_spin_unlock_irq(&rq->lock);
9109 }
9110 mutex_unlock(&mutex);
9111
9112 return 0;
9113}
9114
9115int tg_set_cfs_quota(struct task_group *tg, long cfs_quota_us)
9116{
9117 u64 quota, period;
9118
9119 period = ktime_to_ns(tg_cfs_bandwidth(tg)->period);
9120 if (cfs_quota_us < 0)
9121 quota = RUNTIME_INF;
9122 else
9123 quota = (u64)cfs_quota_us * NSEC_PER_USEC;
9124
9125 return tg_set_cfs_bandwidth(tg, period, quota);
9126}
9127
9128long tg_get_cfs_quota(struct task_group *tg)
9129{
9130 u64 quota_us;
9131
9132 if (tg_cfs_bandwidth(tg)->quota == RUNTIME_INF)
9133 return -1;
9134
9135 quota_us = tg_cfs_bandwidth(tg)->quota;
9136 do_div(quota_us, NSEC_PER_USEC);
9137
9138 return quota_us;
9139}
9140
9141int tg_set_cfs_period(struct task_group *tg, long cfs_period_us)
9142{
9143 u64 quota, period;
9144
9145 period = (u64)cfs_period_us * NSEC_PER_USEC;
9146 quota = tg_cfs_bandwidth(tg)->quota;
9147
9148 if (period <= 0)
9149 return -EINVAL;
9150
9151 return tg_set_cfs_bandwidth(tg, period, quota);
9152}
9153
9154long tg_get_cfs_period(struct task_group *tg)
9155{
9156 u64 cfs_period_us;
9157
9158 cfs_period_us = ktime_to_ns(tg_cfs_bandwidth(tg)->period);
9159 do_div(cfs_period_us, NSEC_PER_USEC);
9160
9161 return cfs_period_us;
9162}
9163
9164static s64 cpu_cfs_quota_read_s64(struct cgroup *cgrp, struct cftype *cft)
9165{
9166 return tg_get_cfs_quota(cgroup_tg(cgrp));
9167}
9168
9169static int cpu_cfs_quota_write_s64(struct cgroup *cgrp, struct cftype *cftype,
9170 s64 cfs_quota_us)
9171{
9172 return tg_set_cfs_quota(cgroup_tg(cgrp), cfs_quota_us);
9173}
9174
9175static u64 cpu_cfs_period_read_u64(struct cgroup *cgrp, struct cftype *cft)
9176{
9177 return tg_get_cfs_period(cgroup_tg(cgrp));
9178}
9179
9180static int cpu_cfs_period_write_u64(struct cgroup *cgrp, struct cftype *cftype,
9181 u64 cfs_period_us)
9182{
9183 return tg_set_cfs_period(cgroup_tg(cgrp), cfs_period_us);
9184}
9185
9186#endif /* CONFIG_CFS_BANDWIDTH */
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +02009187#endif /* CONFIG_FAIR_GROUP_SCHED */
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07009188
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01009189#ifdef CONFIG_RT_GROUP_SCHED
Mirco Tischler0c708142008-05-14 16:05:46 -07009190static int cpu_rt_runtime_write(struct cgroup *cgrp, struct cftype *cft,
Paul Menage06ecb272008-04-29 01:00:06 -07009191 s64 val)
Peter Zijlstra6f505b12008-01-25 21:08:30 +01009192{
Paul Menage06ecb272008-04-29 01:00:06 -07009193 return sched_group_set_rt_runtime(cgroup_tg(cgrp), val);
Peter Zijlstra6f505b12008-01-25 21:08:30 +01009194}
9195
Paul Menage06ecb272008-04-29 01:00:06 -07009196static s64 cpu_rt_runtime_read(struct cgroup *cgrp, struct cftype *cft)
Peter Zijlstra6f505b12008-01-25 21:08:30 +01009197{
Paul Menage06ecb272008-04-29 01:00:06 -07009198 return sched_group_rt_runtime(cgroup_tg(cgrp));
Peter Zijlstra6f505b12008-01-25 21:08:30 +01009199}
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02009200
9201static int cpu_rt_period_write_uint(struct cgroup *cgrp, struct cftype *cftype,
9202 u64 rt_period_us)
9203{
9204 return sched_group_set_rt_period(cgroup_tg(cgrp), rt_period_us);
9205}
9206
9207static u64 cpu_rt_period_read_uint(struct cgroup *cgrp, struct cftype *cft)
9208{
9209 return sched_group_rt_period(cgroup_tg(cgrp));
9210}
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +02009211#endif /* CONFIG_RT_GROUP_SCHED */
Peter Zijlstra6f505b12008-01-25 21:08:30 +01009212
Paul Menagefe5c7cc2007-10-29 21:18:11 +01009213static struct cftype cpu_files[] = {
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01009214#ifdef CONFIG_FAIR_GROUP_SCHED
Paul Menagefe5c7cc2007-10-29 21:18:11 +01009215 {
9216 .name = "shares",
Paul Menagef4c753b2008-04-29 00:59:56 -07009217 .read_u64 = cpu_shares_read_u64,
9218 .write_u64 = cpu_shares_write_u64,
Paul Menagefe5c7cc2007-10-29 21:18:11 +01009219 },
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01009220#endif
Paul Turnerab84d312011-07-21 09:43:28 -07009221#ifdef CONFIG_CFS_BANDWIDTH
9222 {
9223 .name = "cfs_quota_us",
9224 .read_s64 = cpu_cfs_quota_read_s64,
9225 .write_s64 = cpu_cfs_quota_write_s64,
9226 },
9227 {
9228 .name = "cfs_period_us",
9229 .read_u64 = cpu_cfs_period_read_u64,
9230 .write_u64 = cpu_cfs_period_write_u64,
9231 },
9232#endif
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01009233#ifdef CONFIG_RT_GROUP_SCHED
Peter Zijlstra6f505b12008-01-25 21:08:30 +01009234 {
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01009235 .name = "rt_runtime_us",
Paul Menage06ecb272008-04-29 01:00:06 -07009236 .read_s64 = cpu_rt_runtime_read,
9237 .write_s64 = cpu_rt_runtime_write,
Peter Zijlstra6f505b12008-01-25 21:08:30 +01009238 },
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02009239 {
9240 .name = "rt_period_us",
Paul Menagef4c753b2008-04-29 00:59:56 -07009241 .read_u64 = cpu_rt_period_read_uint,
9242 .write_u64 = cpu_rt_period_write_uint,
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02009243 },
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01009244#endif
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07009245};
9246
9247static int cpu_cgroup_populate(struct cgroup_subsys *ss, struct cgroup *cont)
9248{
Paul Menagefe5c7cc2007-10-29 21:18:11 +01009249 return cgroup_add_files(cont, ss, cpu_files, ARRAY_SIZE(cpu_files));
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07009250}
9251
9252struct cgroup_subsys cpu_cgroup_subsys = {
Ingo Molnar38605ca2007-10-29 21:18:11 +01009253 .name = "cpu",
9254 .create = cpu_cgroup_create,
9255 .destroy = cpu_cgroup_destroy,
Ben Blumf780bdb2011-05-26 16:25:19 -07009256 .can_attach_task = cpu_cgroup_can_attach_task,
9257 .attach_task = cpu_cgroup_attach_task,
Peter Zijlstra068c5cc2011-01-19 12:26:11 +01009258 .exit = cpu_cgroup_exit,
Ingo Molnar38605ca2007-10-29 21:18:11 +01009259 .populate = cpu_cgroup_populate,
9260 .subsys_id = cpu_cgroup_subsys_id,
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07009261 .early_init = 1,
9262};
9263
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01009264#endif /* CONFIG_CGROUP_SCHED */
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01009265
9266#ifdef CONFIG_CGROUP_CPUACCT
9267
9268/*
9269 * CPU accounting code for task groups.
9270 *
9271 * Based on the work by Paul Menage (menage@google.com) and Balbir Singh
9272 * (balbir@in.ibm.com).
9273 */
9274
Bharata B Rao934352f2008-11-10 20:41:13 +05309275/* track cpu usage of a group of tasks and its child groups */
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01009276struct cpuacct {
9277 struct cgroup_subsys_state css;
9278 /* cpuusage holds pointer to a u64-type object on every cpu */
Tejun Heo43cf38e2010-02-02 14:38:57 +09009279 u64 __percpu *cpuusage;
Bharata B Raoef12fef2009-03-31 10:02:22 +05309280 struct percpu_counter cpustat[CPUACCT_STAT_NSTATS];
Bharata B Rao934352f2008-11-10 20:41:13 +05309281 struct cpuacct *parent;
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01009282};
9283
9284struct cgroup_subsys cpuacct_subsys;
9285
9286/* return cpu accounting group corresponding to this container */
Dhaval Giani32cd7562008-02-29 10:02:43 +05309287static inline struct cpuacct *cgroup_ca(struct cgroup *cgrp)
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01009288{
Dhaval Giani32cd7562008-02-29 10:02:43 +05309289 return container_of(cgroup_subsys_state(cgrp, cpuacct_subsys_id),
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01009290 struct cpuacct, css);
9291}
9292
9293/* return cpu accounting group to which this task belongs */
9294static inline struct cpuacct *task_ca(struct task_struct *tsk)
9295{
9296 return container_of(task_subsys_state(tsk, cpuacct_subsys_id),
9297 struct cpuacct, css);
9298}
9299
9300/* create a new cpu accounting group */
9301static struct cgroup_subsys_state *cpuacct_create(
Dhaval Giani32cd7562008-02-29 10:02:43 +05309302 struct cgroup_subsys *ss, struct cgroup *cgrp)
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01009303{
9304 struct cpuacct *ca = kzalloc(sizeof(*ca), GFP_KERNEL);
Bharata B Raoef12fef2009-03-31 10:02:22 +05309305 int i;
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01009306
9307 if (!ca)
Bharata B Raoef12fef2009-03-31 10:02:22 +05309308 goto out;
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01009309
9310 ca->cpuusage = alloc_percpu(u64);
Bharata B Raoef12fef2009-03-31 10:02:22 +05309311 if (!ca->cpuusage)
9312 goto out_free_ca;
9313
9314 for (i = 0; i < CPUACCT_STAT_NSTATS; i++)
9315 if (percpu_counter_init(&ca->cpustat[i], 0))
9316 goto out_free_counters;
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01009317
Bharata B Rao934352f2008-11-10 20:41:13 +05309318 if (cgrp->parent)
9319 ca->parent = cgroup_ca(cgrp->parent);
9320
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01009321 return &ca->css;
Bharata B Raoef12fef2009-03-31 10:02:22 +05309322
9323out_free_counters:
9324 while (--i >= 0)
9325 percpu_counter_destroy(&ca->cpustat[i]);
9326 free_percpu(ca->cpuusage);
9327out_free_ca:
9328 kfree(ca);
9329out:
9330 return ERR_PTR(-ENOMEM);
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01009331}
9332
9333/* destroy an existing cpu accounting group */
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01009334static void
Dhaval Giani32cd7562008-02-29 10:02:43 +05309335cpuacct_destroy(struct cgroup_subsys *ss, struct cgroup *cgrp)
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01009336{
Dhaval Giani32cd7562008-02-29 10:02:43 +05309337 struct cpuacct *ca = cgroup_ca(cgrp);
Bharata B Raoef12fef2009-03-31 10:02:22 +05309338 int i;
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01009339
Bharata B Raoef12fef2009-03-31 10:02:22 +05309340 for (i = 0; i < CPUACCT_STAT_NSTATS; i++)
9341 percpu_counter_destroy(&ca->cpustat[i]);
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01009342 free_percpu(ca->cpuusage);
9343 kfree(ca);
9344}
9345
Ken Chen720f5492008-12-15 22:02:01 -08009346static u64 cpuacct_cpuusage_read(struct cpuacct *ca, int cpu)
9347{
Rusty Russellb36128c2009-02-20 16:29:08 +09009348 u64 *cpuusage = per_cpu_ptr(ca->cpuusage, cpu);
Ken Chen720f5492008-12-15 22:02:01 -08009349 u64 data;
9350
9351#ifndef CONFIG_64BIT
9352 /*
9353 * Take rq->lock to make 64-bit read safe on 32-bit platforms.
9354 */
Thomas Gleixner05fa7852009-11-17 14:28:38 +01009355 raw_spin_lock_irq(&cpu_rq(cpu)->lock);
Ken Chen720f5492008-12-15 22:02:01 -08009356 data = *cpuusage;
Thomas Gleixner05fa7852009-11-17 14:28:38 +01009357 raw_spin_unlock_irq(&cpu_rq(cpu)->lock);
Ken Chen720f5492008-12-15 22:02:01 -08009358#else
9359 data = *cpuusage;
9360#endif
9361
9362 return data;
9363}
9364
9365static void cpuacct_cpuusage_write(struct cpuacct *ca, int cpu, u64 val)
9366{
Rusty Russellb36128c2009-02-20 16:29:08 +09009367 u64 *cpuusage = per_cpu_ptr(ca->cpuusage, cpu);
Ken Chen720f5492008-12-15 22:02:01 -08009368
9369#ifndef CONFIG_64BIT
9370 /*
9371 * Take rq->lock to make 64-bit write safe on 32-bit platforms.
9372 */
Thomas Gleixner05fa7852009-11-17 14:28:38 +01009373 raw_spin_lock_irq(&cpu_rq(cpu)->lock);
Ken Chen720f5492008-12-15 22:02:01 -08009374 *cpuusage = val;
Thomas Gleixner05fa7852009-11-17 14:28:38 +01009375 raw_spin_unlock_irq(&cpu_rq(cpu)->lock);
Ken Chen720f5492008-12-15 22:02:01 -08009376#else
9377 *cpuusage = val;
9378#endif
9379}
9380
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01009381/* return total cpu usage (in nanoseconds) of a group */
Dhaval Giani32cd7562008-02-29 10:02:43 +05309382static u64 cpuusage_read(struct cgroup *cgrp, struct cftype *cft)
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01009383{
Dhaval Giani32cd7562008-02-29 10:02:43 +05309384 struct cpuacct *ca = cgroup_ca(cgrp);
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01009385 u64 totalcpuusage = 0;
9386 int i;
9387
Ken Chen720f5492008-12-15 22:02:01 -08009388 for_each_present_cpu(i)
9389 totalcpuusage += cpuacct_cpuusage_read(ca, i);
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01009390
9391 return totalcpuusage;
9392}
9393
Dhaval Giani0297b802008-02-29 10:02:44 +05309394static int cpuusage_write(struct cgroup *cgrp, struct cftype *cftype,
9395 u64 reset)
9396{
9397 struct cpuacct *ca = cgroup_ca(cgrp);
9398 int err = 0;
9399 int i;
9400
9401 if (reset) {
9402 err = -EINVAL;
9403 goto out;
9404 }
9405
Ken Chen720f5492008-12-15 22:02:01 -08009406 for_each_present_cpu(i)
9407 cpuacct_cpuusage_write(ca, i, 0);
Dhaval Giani0297b802008-02-29 10:02:44 +05309408
Dhaval Giani0297b802008-02-29 10:02:44 +05309409out:
9410 return err;
9411}
9412
Ken Chene9515c32008-12-15 22:04:15 -08009413static int cpuacct_percpu_seq_read(struct cgroup *cgroup, struct cftype *cft,
9414 struct seq_file *m)
9415{
9416 struct cpuacct *ca = cgroup_ca(cgroup);
9417 u64 percpu;
9418 int i;
9419
9420 for_each_present_cpu(i) {
9421 percpu = cpuacct_cpuusage_read(ca, i);
9422 seq_printf(m, "%llu ", (unsigned long long) percpu);
9423 }
9424 seq_printf(m, "\n");
9425 return 0;
9426}
9427
Bharata B Raoef12fef2009-03-31 10:02:22 +05309428static const char *cpuacct_stat_desc[] = {
9429 [CPUACCT_STAT_USER] = "user",
9430 [CPUACCT_STAT_SYSTEM] = "system",
9431};
9432
9433static int cpuacct_stats_show(struct cgroup *cgrp, struct cftype *cft,
9434 struct cgroup_map_cb *cb)
9435{
9436 struct cpuacct *ca = cgroup_ca(cgrp);
9437 int i;
9438
9439 for (i = 0; i < CPUACCT_STAT_NSTATS; i++) {
9440 s64 val = percpu_counter_read(&ca->cpustat[i]);
9441 val = cputime64_to_clock_t(val);
9442 cb->fill(cb, cpuacct_stat_desc[i], val);
9443 }
9444 return 0;
9445}
9446
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01009447static struct cftype files[] = {
9448 {
9449 .name = "usage",
Paul Menagef4c753b2008-04-29 00:59:56 -07009450 .read_u64 = cpuusage_read,
9451 .write_u64 = cpuusage_write,
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01009452 },
Ken Chene9515c32008-12-15 22:04:15 -08009453 {
9454 .name = "usage_percpu",
9455 .read_seq_string = cpuacct_percpu_seq_read,
9456 },
Bharata B Raoef12fef2009-03-31 10:02:22 +05309457 {
9458 .name = "stat",
9459 .read_map = cpuacct_stats_show,
9460 },
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01009461};
9462
Dhaval Giani32cd7562008-02-29 10:02:43 +05309463static int cpuacct_populate(struct cgroup_subsys *ss, struct cgroup *cgrp)
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01009464{
Dhaval Giani32cd7562008-02-29 10:02:43 +05309465 return cgroup_add_files(cgrp, ss, files, ARRAY_SIZE(files));
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01009466}
9467
9468/*
9469 * charge this task's execution time to its accounting group.
9470 *
9471 * called with rq->lock held.
9472 */
9473static void cpuacct_charge(struct task_struct *tsk, u64 cputime)
9474{
9475 struct cpuacct *ca;
Bharata B Rao934352f2008-11-10 20:41:13 +05309476 int cpu;
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01009477
Li Zefanc40c6f82009-02-26 15:40:15 +08009478 if (unlikely(!cpuacct_subsys.active))
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01009479 return;
9480
Bharata B Rao934352f2008-11-10 20:41:13 +05309481 cpu = task_cpu(tsk);
Bharata B Raoa18b83b2009-03-23 10:02:53 +05309482
9483 rcu_read_lock();
9484
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01009485 ca = task_ca(tsk);
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01009486
Bharata B Rao934352f2008-11-10 20:41:13 +05309487 for (; ca; ca = ca->parent) {
Rusty Russellb36128c2009-02-20 16:29:08 +09009488 u64 *cpuusage = per_cpu_ptr(ca->cpuusage, cpu);
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01009489 *cpuusage += cputime;
9490 }
Bharata B Raoa18b83b2009-03-23 10:02:53 +05309491
9492 rcu_read_unlock();
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01009493}
9494
Bharata B Raoef12fef2009-03-31 10:02:22 +05309495/*
Anton Blanchardfa535a72010-02-02 14:46:13 -08009496 * When CONFIG_VIRT_CPU_ACCOUNTING is enabled one jiffy can be very large
9497 * in cputime_t units. As a result, cpuacct_update_stats calls
9498 * percpu_counter_add with values large enough to always overflow the
9499 * per cpu batch limit causing bad SMP scalability.
9500 *
9501 * To fix this we scale percpu_counter_batch by cputime_one_jiffy so we
9502 * batch the same amount of time with CONFIG_VIRT_CPU_ACCOUNTING disabled
9503 * and enabled. We cap it at INT_MAX which is the largest allowed batch value.
9504 */
9505#ifdef CONFIG_SMP
9506#define CPUACCT_BATCH \
9507 min_t(long, percpu_counter_batch * cputime_one_jiffy, INT_MAX)
9508#else
9509#define CPUACCT_BATCH 0
9510#endif
9511
9512/*
Bharata B Raoef12fef2009-03-31 10:02:22 +05309513 * Charge the system/user time to the task's accounting group.
9514 */
9515static void cpuacct_update_stats(struct task_struct *tsk,
9516 enum cpuacct_stat_index idx, cputime_t val)
9517{
9518 struct cpuacct *ca;
Anton Blanchardfa535a72010-02-02 14:46:13 -08009519 int batch = CPUACCT_BATCH;
Bharata B Raoef12fef2009-03-31 10:02:22 +05309520
9521 if (unlikely(!cpuacct_subsys.active))
9522 return;
9523
9524 rcu_read_lock();
9525 ca = task_ca(tsk);
9526
9527 do {
Anton Blanchardfa535a72010-02-02 14:46:13 -08009528 __percpu_counter_add(&ca->cpustat[idx], val, batch);
Bharata B Raoef12fef2009-03-31 10:02:22 +05309529 ca = ca->parent;
9530 } while (ca);
9531 rcu_read_unlock();
9532}
9533
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01009534struct cgroup_subsys cpuacct_subsys = {
9535 .name = "cpuacct",
9536 .create = cpuacct_create,
9537 .destroy = cpuacct_destroy,
9538 .populate = cpuacct_populate,
9539 .subsys_id = cpuacct_subsys_id,
9540};
9541#endif /* CONFIG_CGROUP_CPUACCT */