blob: 35561c63a49079af3c7abe4e3450131e1edcb8aa [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 Molnar9a11b49a2006-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;
Paul Turnerec12cb72011-07-21 09:43:30 -0700254 u64 quota, runtime;
Paul Turnera790de92011-07-21 09:43:29 -0700255 s64 hierarchal_quota;
Paul Turnerab84d312011-07-21 09:43:28 -0700256#endif
257};
258
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +0200259/* task group related information */
Ingo Molnar4cf86d72007-10-15 17:00:14 +0200260struct task_group {
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -0700261 struct cgroup_subsys_state css;
Arun R Bharadwaj6c415b92008-12-01 20:49:05 +0530262
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100263#ifdef CONFIG_FAIR_GROUP_SCHED
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +0200264 /* schedulable entities of this group on each cpu */
265 struct sched_entity **se;
266 /* runqueue "owned" by this group on each cpu */
267 struct cfs_rq **cfs_rq;
268 unsigned long shares;
Peter Zijlstra2069dd72010-11-15 15:47:00 -0800269
270 atomic_t load_weight;
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100271#endif
272
273#ifdef CONFIG_RT_GROUP_SCHED
274 struct sched_rt_entity **rt_se;
275 struct rt_rq **rt_rq;
276
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +0200277 struct rt_bandwidth rt_bandwidth;
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100278#endif
Srivatsa Vaddagiri6b2d7702008-01-25 21:08:00 +0100279
Srivatsa Vaddagiriae8393e2007-10-29 21:18:11 +0100280 struct rcu_head rcu;
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100281 struct list_head list;
Peter Zijlstraf473aa52008-04-19 19:45:00 +0200282
283 struct task_group *parent;
284 struct list_head siblings;
285 struct list_head children;
Mike Galbraith5091faa2010-11-30 14:18:03 +0100286
287#ifdef CONFIG_SCHED_AUTOGROUP
288 struct autogroup *autogroup;
289#endif
Paul Turnerab84d312011-07-21 09:43:28 -0700290
291 struct cfs_bandwidth cfs_bandwidth;
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +0200292};
293
Peter Zijlstra3d4b47b2010-11-15 15:47:01 -0800294/* task_group_lock serializes the addition/removal of task groups */
Peter Zijlstra8ed36992008-02-13 15:45:39 +0100295static DEFINE_SPINLOCK(task_group_lock);
Srivatsa Vaddagiriec2c5072008-01-25 21:07:59 +0100296
Cyrill Gorcunove9036b32009-10-26 22:24:14 +0300297#ifdef CONFIG_FAIR_GROUP_SCHED
298
Yong Zhang07e06b02011-01-07 15:17:36 +0800299# define ROOT_TASK_GROUP_LOAD NICE_0_LOAD
Srivatsa Vaddagiri24e377a2007-10-15 17:00:09 +0200300
Miao Xiecb4ad1f2008-04-28 12:54:56 +0800301/*
Lai Jiangshan2e084782008-06-12 16:42:58 +0800302 * A weight of 0 or 1 can cause arithmetics problems.
303 * A weight of a cfs_rq is the sum of weights of which entities
304 * are queued on this cfs_rq, so a weight of a entity should not be
305 * too large, so as the shares value of a task group.
Miao Xiecb4ad1f2008-04-28 12:54:56 +0800306 * (The default weight is 1024 - so there's no practical
307 * limitation from this.)
308 */
Mike Galbraithcd622872011-06-04 15:03:20 +0200309#define MIN_SHARES (1UL << 1)
310#define MAX_SHARES (1UL << 18)
Peter Zijlstra18d95a22008-04-19 19:45:00 +0200311
Yong Zhang07e06b02011-01-07 15:17:36 +0800312static int root_task_group_load = ROOT_TASK_GROUP_LOAD;
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100313#endif
314
315/* Default task group.
316 * Every task in system belong to this group at bootup.
317 */
Yong Zhang07e06b02011-01-07 15:17:36 +0800318struct task_group root_task_group;
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +0200319
Dhaval Giani7c941432010-01-20 13:26:18 +0100320#endif /* CONFIG_CGROUP_SCHED */
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +0200321
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200322/* CFS-related fields in a runqueue */
323struct cfs_rq {
324 struct load_weight load;
Paul Turner953bfcd2011-07-21 09:43:27 -0700325 unsigned long nr_running, h_nr_running;
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200326
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200327 u64 exec_clock;
Ingo Molnare9acbff2007-10-15 17:00:04 +0200328 u64 min_vruntime;
Peter Zijlstra3fe16982011-04-05 17:23:48 +0200329#ifndef CONFIG_64BIT
330 u64 min_vruntime_copy;
331#endif
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200332
333 struct rb_root tasks_timeline;
334 struct rb_node *rb_leftmost;
Peter Zijlstra4a55bd52008-04-19 19:45:00 +0200335
336 struct list_head tasks;
337 struct list_head *balance_iterator;
338
339 /*
340 * 'curr' points to currently running entity on this cfs_rq.
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200341 * It is set to NULL otherwise (i.e when none are currently running).
342 */
Rik van Rielac53db52011-02-01 09:51:03 -0500343 struct sched_entity *curr, *next, *last, *skip;
Peter Zijlstraddc97292007-10-15 17:00:10 +0200344
Rakib Mullick4934a4d2011-05-04 22:53:46 +0600345#ifdef CONFIG_SCHED_DEBUG
Peter Zijlstra5ac5c4d2008-11-10 10:46:32 +0100346 unsigned int nr_spread_over;
Rakib Mullick4934a4d2011-05-04 22:53:46 +0600347#endif
Peter Zijlstraddc97292007-10-15 17:00:10 +0200348
Ingo Molnar62160e32007-10-15 17:00:03 +0200349#ifdef CONFIG_FAIR_GROUP_SCHED
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200350 struct rq *rq; /* cpu runqueue to which this cfs_rq is attached */
351
Ingo Molnar41a2d6c2007-12-05 15:46:09 +0100352 /*
353 * leaf cfs_rqs are those that hold tasks (lowest schedulable entity in
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200354 * a hierarchy). Non-leaf lrqs hold other higher schedulable entities
355 * (like users, containers etc.)
356 *
357 * leaf_cfs_rq_list ties together list of leaf cfs_rq's in a cpu. This
358 * list is used during load balance.
359 */
Peter Zijlstra3d4b47b2010-11-15 15:47:01 -0800360 int on_list;
Ingo Molnar41a2d6c2007-12-05 15:46:09 +0100361 struct list_head leaf_cfs_rq_list;
362 struct task_group *tg; /* group that "owns" this runqueue */
Peter Zijlstrac09595f2008-06-27 13:41:14 +0200363
364#ifdef CONFIG_SMP
Peter Zijlstrac09595f2008-06-27 13:41:14 +0200365 /*
Peter Zijlstrac8cba852008-06-27 13:41:23 +0200366 * the part of load.weight contributed by tasks
Peter Zijlstrac09595f2008-06-27 13:41:14 +0200367 */
Peter Zijlstrac8cba852008-06-27 13:41:23 +0200368 unsigned long task_weight;
Peter Zijlstrac09595f2008-06-27 13:41:14 +0200369
Peter Zijlstrac8cba852008-06-27 13:41:23 +0200370 /*
371 * h_load = weight * f(tg)
372 *
373 * Where f(tg) is the recursive weight fraction assigned to
374 * this group.
375 */
376 unsigned long h_load;
Peter Zijlstrac09595f2008-06-27 13:41:14 +0200377
Peter Zijlstrac8cba852008-06-27 13:41:23 +0200378 /*
Paul Turner3b3d1902010-11-15 15:47:08 -0800379 * Maintaining per-cpu shares distribution for group scheduling
380 *
381 * load_stamp is the last time we updated the load average
382 * load_last is the last time we updated the load average and saw load
383 * load_unacc_exec_time is currently unaccounted execution time
Peter Zijlstrac8cba852008-06-27 13:41:23 +0200384 */
Peter Zijlstra2069dd72010-11-15 15:47:00 -0800385 u64 load_avg;
386 u64 load_period;
Paul Turner3b3d1902010-11-15 15:47:08 -0800387 u64 load_stamp, load_last, load_unacc_exec_time;
Peter Zijlstraf1d239f2008-06-27 13:41:38 +0200388
Peter Zijlstra2069dd72010-11-15 15:47:00 -0800389 unsigned long load_contribution;
Peter Zijlstrac09595f2008-06-27 13:41:14 +0200390#endif
Paul Turnerab84d312011-07-21 09:43:28 -0700391#ifdef CONFIG_CFS_BANDWIDTH
392 int runtime_enabled;
393 s64 runtime_remaining;
394#endif
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200395#endif
396};
397
Paul Turnerab84d312011-07-21 09:43:28 -0700398#ifdef CONFIG_FAIR_GROUP_SCHED
399#ifdef CONFIG_CFS_BANDWIDTH
400static inline struct cfs_bandwidth *tg_cfs_bandwidth(struct task_group *tg)
401{
402 return &tg->cfs_bandwidth;
403}
404
405static inline u64 default_cfs_period(void);
406
407static void init_cfs_bandwidth(struct cfs_bandwidth *cfs_b)
408{
409 raw_spin_lock_init(&cfs_b->lock);
Paul Turnerec12cb72011-07-21 09:43:30 -0700410 cfs_b->runtime = 0;
Paul Turnerab84d312011-07-21 09:43:28 -0700411 cfs_b->quota = RUNTIME_INF;
412 cfs_b->period = ns_to_ktime(default_cfs_period());
413}
414
415static void init_cfs_rq_runtime(struct cfs_rq *cfs_rq)
416{
417 cfs_rq->runtime_enabled = 0;
418}
419
420static void destroy_cfs_bandwidth(struct cfs_bandwidth *cfs_b)
421{}
422#else
423static void init_cfs_rq_runtime(struct cfs_rq *cfs_rq) {}
424static void init_cfs_bandwidth(struct cfs_bandwidth *cfs_b) {}
425static void destroy_cfs_bandwidth(struct cfs_bandwidth *cfs_b) {}
426
427static inline struct cfs_bandwidth *tg_cfs_bandwidth(struct task_group *tg)
428{
429 return NULL;
430}
431#endif /* CONFIG_CFS_BANDWIDTH */
432#endif /* CONFIG_FAIR_GROUP_SCHED */
433
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200434/* Real-Time classes' related field in a runqueue: */
435struct rt_rq {
436 struct rt_prio_array active;
Steven Rostedt63489e42008-01-25 21:08:03 +0100437 unsigned long rt_nr_running;
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100438#if defined CONFIG_SMP || defined CONFIG_RT_GROUP_SCHED
Gregory Haskinse864c492008-12-29 09:39:49 -0500439 struct {
440 int curr; /* highest queued rt task prio */
Gregory Haskins398a1532009-01-14 09:10:04 -0500441#ifdef CONFIG_SMP
Gregory Haskinse864c492008-12-29 09:39:49 -0500442 int next; /* next highest */
Gregory Haskins398a1532009-01-14 09:10:04 -0500443#endif
Gregory Haskinse864c492008-12-29 09:39:49 -0500444 } highest_prio;
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100445#endif
Peter Zijlstrafa85ae22008-01-25 21:08:29 +0100446#ifdef CONFIG_SMP
Gregory Haskins73fe6aa2008-01-25 21:08:07 +0100447 unsigned long rt_nr_migratory;
Peter Zijlstraa1ba4d82009-04-01 18:40:15 +0200448 unsigned long rt_nr_total;
Gregory Haskinsa22d7fc2008-01-25 21:08:12 +0100449 int overloaded;
Gregory Haskins917b6272008-12-29 09:39:53 -0500450 struct plist_head pushable_tasks;
Peter Zijlstrafa85ae22008-01-25 21:08:29 +0100451#endif
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100452 int rt_throttled;
Peter Zijlstrafa85ae22008-01-25 21:08:29 +0100453 u64 rt_time;
Peter Zijlstraac086bc2008-04-19 19:44:58 +0200454 u64 rt_runtime;
Ingo Molnarea736ed2008-03-25 13:51:45 +0100455 /* Nests inside the rq lock: */
Thomas Gleixner0986b112009-11-17 15:32:06 +0100456 raw_spinlock_t rt_runtime_lock;
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100457
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100458#ifdef CONFIG_RT_GROUP_SCHED
Peter Zijlstra23b0fdf2008-02-13 15:45:39 +0100459 unsigned long rt_nr_boosted;
460
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100461 struct rq *rq;
462 struct list_head leaf_rt_rq_list;
463 struct task_group *tg;
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100464#endif
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200465};
466
Gregory Haskins57d885f2008-01-25 21:08:18 +0100467#ifdef CONFIG_SMP
468
469/*
470 * We add the notion of a root-domain which will be used to define per-domain
Ingo Molnar0eab9142008-01-25 21:08:19 +0100471 * variables. Each exclusive cpuset essentially defines an island domain by
472 * fully partitioning the member cpus from any other cpuset. Whenever a new
Gregory Haskins57d885f2008-01-25 21:08:18 +0100473 * exclusive cpuset is created, we also create and attach a new root-domain
474 * object.
475 *
Gregory Haskins57d885f2008-01-25 21:08:18 +0100476 */
477struct root_domain {
478 atomic_t refcount;
Richard Kennedy26a148e2011-07-15 11:41:31 +0100479 atomic_t rto_count;
Peter Zijlstradce840a2011-04-07 14:09:50 +0200480 struct rcu_head rcu;
Rusty Russellc6c49272008-11-25 02:35:05 +1030481 cpumask_var_t span;
482 cpumask_var_t online;
Gregory Haskins637f5082008-01-25 21:08:18 +0100483
Ingo Molnar0eab9142008-01-25 21:08:19 +0100484 /*
Gregory Haskins637f5082008-01-25 21:08:18 +0100485 * The "RT overload" flag: it gets set if a CPU has more than
486 * one runnable RT task.
487 */
Rusty Russellc6c49272008-11-25 02:35:05 +1030488 cpumask_var_t rto_mask;
Gregory Haskins6e0534f2008-05-12 21:21:01 +0200489 struct cpupri cpupri;
Gregory Haskins57d885f2008-01-25 21:08:18 +0100490};
491
Gregory Haskinsdc938522008-01-25 21:08:26 +0100492/*
493 * By default the system creates a single root-domain with all cpus as
494 * members (mimicking the global state we have today).
495 */
Gregory Haskins57d885f2008-01-25 21:08:18 +0100496static struct root_domain def_root_domain;
497
Christian Dietriched2d3722010-09-06 16:37:05 +0200498#endif /* CONFIG_SMP */
Gregory Haskins57d885f2008-01-25 21:08:18 +0100499
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200500/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 * This is the main, per-CPU runqueue data structure.
502 *
503 * Locking rule: those places that want to lock multiple runqueues
504 * (such as the load balancing or the thread migration code), lock
505 * acquire operations must be ordered by ascending &runqueue.
506 */
Ingo Molnar70b97a72006-07-03 00:25:42 -0700507struct rq {
Ingo Molnard8016492007-10-18 21:32:55 +0200508 /* runqueue lock: */
Thomas Gleixner05fa7852009-11-17 14:28:38 +0100509 raw_spinlock_t lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510
511 /*
512 * nr_running and cpu_load should be in the same cacheline because
513 * remote CPUs use both these fields when doing load calculation.
514 */
515 unsigned long nr_running;
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200516 #define CPU_LOAD_IDX_MAX 5
517 unsigned long cpu_load[CPU_LOAD_IDX_MAX];
Venkatesh Pallipadifdf3e952010-05-17 18:14:43 -0700518 unsigned long last_load_update_tick;
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -0700519#ifdef CONFIG_NO_HZ
Mike Galbraith39c0cbe2010-03-11 17:17:13 +0100520 u64 nohz_stamp;
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -0700521 unsigned char nohz_balance_kick;
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -0700522#endif
Mike Galbraith61eadef2011-04-29 08:36:50 +0200523 int skip_clock_update;
Mike Galbraitha64692a2010-03-11 17:16:20 +0100524
Ingo Molnard8016492007-10-18 21:32:55 +0200525 /* capture load from *all* tasks on this cpu: */
526 struct load_weight load;
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200527 unsigned long nr_load_updates;
528 u64 nr_switches;
529
530 struct cfs_rq cfs;
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100531 struct rt_rq rt;
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100532
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200533#ifdef CONFIG_FAIR_GROUP_SCHED
Ingo Molnard8016492007-10-18 21:32:55 +0200534 /* list of leaf cfs_rq on this cpu: */
535 struct list_head leaf_cfs_rq_list;
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100536#endif
537#ifdef CONFIG_RT_GROUP_SCHED
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100538 struct list_head leaf_rt_rq_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540
541 /*
542 * This is part of a global counter where only the total sum
543 * over all CPUs matters. A task can increase this counter on
544 * one CPU and if it got migrated afterwards it may decrease
545 * it on another CPU. Always updated under the runqueue lock:
546 */
547 unsigned long nr_uninterruptible;
548
Peter Zijlstra34f971f2010-09-22 13:53:15 +0200549 struct task_struct *curr, *idle, *stop;
Christoph Lameterc9819f42006-12-10 02:20:25 -0800550 unsigned long next_balance;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 struct mm_struct *prev_mm;
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200552
Peter Zijlstra3e51f332008-05-03 18:29:28 +0200553 u64 clock;
Venkatesh Pallipadi305e6832010-10-04 17:03:21 -0700554 u64 clock_task;
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200555
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 atomic_t nr_iowait;
557
558#ifdef CONFIG_SMP
Ingo Molnar0eab9142008-01-25 21:08:19 +0100559 struct root_domain *rd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 struct sched_domain *sd;
561
Peter Zijlstrae51fd5e2010-05-31 12:37:30 +0200562 unsigned long cpu_power;
563
Henrik Austada0a522c2009-02-13 20:35:45 +0100564 unsigned char idle_at_tick;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 /* For active balancing */
Gregory Haskins3f029d32009-07-29 11:08:47 -0400566 int post_schedule;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 int active_balance;
568 int push_cpu;
Tejun Heo969c7922010-05-06 18:49:21 +0200569 struct cpu_stop_work active_balance_work;
Ingo Molnard8016492007-10-18 21:32:55 +0200570 /* cpu of this runqueue: */
571 int cpu;
Gregory Haskins1f11eb62008-06-04 15:04:05 -0400572 int online;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573
Peter Zijlstrae9e92502009-09-01 10:34:37 +0200574 u64 rt_avg;
575 u64 age_stamp;
Mike Galbraith1b9508f2009-11-04 17:53:50 +0100576 u64 idle_stamp;
577 u64 avg_idle;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578#endif
579
Venkatesh Pallipadiaa483802010-10-04 17:03:22 -0700580#ifdef CONFIG_IRQ_TIME_ACCOUNTING
581 u64 prev_irq_time;
582#endif
Glauber Costae6e66852011-07-11 15:28:17 -0400583#ifdef CONFIG_PARAVIRT
584 u64 prev_steal_time;
585#endif
Glauber Costa095c0aa2011-07-11 15:28:18 -0400586#ifdef CONFIG_PARAVIRT_TIME_ACCOUNTING
587 u64 prev_steal_time_rq;
588#endif
Venkatesh Pallipadiaa483802010-10-04 17:03:22 -0700589
Thomas Gleixnerdce48a82009-04-11 10:43:41 +0200590 /* calc_load related fields */
591 unsigned long calc_load_update;
592 long calc_load_active;
593
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +0100594#ifdef CONFIG_SCHED_HRTICK
Peter Zijlstra31656512008-07-18 18:01:23 +0200595#ifdef CONFIG_SMP
596 int hrtick_csd_pending;
597 struct call_single_data hrtick_csd;
598#endif
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +0100599 struct hrtimer hrtick_timer;
600#endif
601
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602#ifdef CONFIG_SCHEDSTATS
603 /* latency stats */
604 struct sched_info rq_sched_info;
Ken Chen9c2c4802008-12-16 23:41:22 -0800605 unsigned long long rq_cpu_time;
606 /* could above be rq->cfs_rq.exec_clock + rq->rt_rq.rt_runtime ? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607
608 /* sys_sched_yield() stats */
Ken Chen480b9432007-10-18 21:32:56 +0200609 unsigned int yld_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610
611 /* schedule() stats */
Ken Chen480b9432007-10-18 21:32:56 +0200612 unsigned int sched_switch;
613 unsigned int sched_count;
614 unsigned int sched_goidle;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615
616 /* try_to_wake_up() stats */
Ken Chen480b9432007-10-18 21:32:56 +0200617 unsigned int ttwu_count;
618 unsigned int ttwu_local;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619#endif
Peter Zijlstra317f3942011-04-05 17:23:58 +0200620
621#ifdef CONFIG_SMP
622 struct task_struct *wake_list;
623#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624};
625
Fenghua Yuf34e3b62007-07-19 01:48:13 -0700626static DEFINE_PER_CPU_SHARED_ALIGNED(struct rq, runqueues);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627
Mike Galbraitha64692a2010-03-11 17:16:20 +0100628
Peter Zijlstra1e5a7402010-10-31 12:37:04 +0100629static void check_preempt_curr(struct rq *rq, struct task_struct *p, int flags);
Ingo Molnardd41f592007-07-09 18:51:59 +0200630
Christoph Lameter0a2966b2006-09-25 23:30:51 -0700631static inline int cpu_of(struct rq *rq)
632{
633#ifdef CONFIG_SMP
634 return rq->cpu;
635#else
636 return 0;
637#endif
638}
639
Paul E. McKenney497f0ab2010-02-22 17:04:51 -0800640#define rcu_dereference_check_sched_domain(p) \
Paul E. McKenneyd11c5632010-02-22 17:04:50 -0800641 rcu_dereference_check((p), \
Paul E. McKenneyd11c5632010-02-22 17:04:50 -0800642 lockdep_is_held(&sched_domains_mutex))
643
Ingo Molnar20d315d2007-07-09 18:51:58 +0200644/*
Nick Piggin674311d2005-06-25 14:57:27 -0700645 * The domain tree (rq->sd) is protected by RCU's quiescent state transition.
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -0700646 * See detach_destroy_domains: synchronize_sched for details.
Nick Piggin674311d2005-06-25 14:57:27 -0700647 *
648 * The domain tree of any CPU may only be accessed from within
649 * preempt-disabled sections.
650 */
Ingo Molnar48f24c42006-07-03 00:25:40 -0700651#define for_each_domain(cpu, __sd) \
Paul E. McKenney497f0ab2010-02-22 17:04:51 -0800652 for (__sd = rcu_dereference_check_sched_domain(cpu_rq(cpu)->sd); __sd; __sd = __sd->parent)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653
654#define cpu_rq(cpu) (&per_cpu(runqueues, (cpu)))
655#define this_rq() (&__get_cpu_var(runqueues))
656#define task_rq(p) cpu_rq(task_cpu(p))
657#define cpu_curr(cpu) (cpu_rq(cpu)->curr)
Hitoshi Mitake54d35f22009-06-29 14:44:57 +0900658#define raw_rq() (&__raw_get_cpu_var(runqueues))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659
Peter Zijlstradc61b1d2010-06-08 11:40:42 +0200660#ifdef CONFIG_CGROUP_SCHED
661
662/*
663 * Return the group to which this tasks belongs.
664 *
Peter Zijlstra6c6c54e2011-06-03 17:37:07 +0200665 * We use task_subsys_state_check() and extend the RCU verification with
666 * pi->lock and rq->lock because cpu_cgroup_attach() holds those locks for each
667 * task it moves into the cgroup. Therefore by holding either of those locks,
668 * we pin the task to the current cgroup.
Peter Zijlstradc61b1d2010-06-08 11:40:42 +0200669 */
670static inline struct task_group *task_group(struct task_struct *p)
671{
Mike Galbraith5091faa2010-11-30 14:18:03 +0100672 struct task_group *tg;
Peter Zijlstradc61b1d2010-06-08 11:40:42 +0200673 struct cgroup_subsys_state *css;
674
675 css = task_subsys_state_check(p, cpu_cgroup_subsys_id,
Peter Zijlstra6c6c54e2011-06-03 17:37:07 +0200676 lockdep_is_held(&p->pi_lock) ||
677 lockdep_is_held(&task_rq(p)->lock));
Mike Galbraith5091faa2010-11-30 14:18:03 +0100678 tg = container_of(css, struct task_group, css);
679
680 return autogroup_task_group(p, tg);
Peter Zijlstradc61b1d2010-06-08 11:40:42 +0200681}
682
683/* Change a task's cfs_rq and parent entity if it moves across CPUs/groups */
684static inline void set_task_rq(struct task_struct *p, unsigned int cpu)
685{
686#ifdef CONFIG_FAIR_GROUP_SCHED
687 p->se.cfs_rq = task_group(p)->cfs_rq[cpu];
688 p->se.parent = task_group(p)->se[cpu];
689#endif
690
691#ifdef CONFIG_RT_GROUP_SCHED
692 p->rt.rt_rq = task_group(p)->rt_rq[cpu];
693 p->rt.parent = task_group(p)->rt_se[cpu];
694#endif
695}
696
697#else /* CONFIG_CGROUP_SCHED */
698
699static inline void set_task_rq(struct task_struct *p, unsigned int cpu) { }
700static inline struct task_group *task_group(struct task_struct *p)
701{
702 return NULL;
703}
704
705#endif /* CONFIG_CGROUP_SCHED */
706
Peter Zijlstrafe44d622010-12-09 14:15:34 +0100707static void update_rq_clock_task(struct rq *rq, s64 delta);
Venkatesh Pallipadi305e6832010-10-04 17:03:21 -0700708
Peter Zijlstrafe44d622010-12-09 14:15:34 +0100709static void update_rq_clock(struct rq *rq)
Peter Zijlstra3e51f332008-05-03 18:29:28 +0200710{
Peter Zijlstrafe44d622010-12-09 14:15:34 +0100711 s64 delta;
Venkatesh Pallipadi305e6832010-10-04 17:03:21 -0700712
Mike Galbraith61eadef2011-04-29 08:36:50 +0200713 if (rq->skip_clock_update > 0)
Mike Galbraithf26f9af2010-12-08 11:05:42 +0100714 return;
Venkatesh Pallipadiaa483802010-10-04 17:03:22 -0700715
Peter Zijlstrafe44d622010-12-09 14:15:34 +0100716 delta = sched_clock_cpu(cpu_of(rq)) - rq->clock;
717 rq->clock += delta;
718 update_rq_clock_task(rq, delta);
Peter Zijlstra3e51f332008-05-03 18:29:28 +0200719}
720
Ingo Molnare436d802007-07-19 21:28:35 +0200721/*
Ingo Molnarbf5c91b2007-10-15 17:00:04 +0200722 * Tunables that become constants when CONFIG_SCHED_DEBUG is off:
723 */
724#ifdef CONFIG_SCHED_DEBUG
725# define const_debug __read_mostly
726#else
727# define const_debug static const
728#endif
729
Ingo Molnar017730c2008-05-12 21:20:52 +0200730/**
Randy Dunlap1fd06bb2011-03-15 16:12:30 -0700731 * runqueue_is_locked - Returns true if the current cpu runqueue is locked
Randy Dunlape17b38b2009-10-11 19:12:00 -0700732 * @cpu: the processor in question.
Ingo Molnar017730c2008-05-12 21:20:52 +0200733 *
Ingo Molnar017730c2008-05-12 21:20:52 +0200734 * This interface allows printk to be called with the runqueue lock
735 * held and know whether or not it is OK to wake up the klogd.
736 */
Andrew Morton89f19f02009-09-19 11:55:44 -0700737int runqueue_is_locked(int cpu)
Ingo Molnar017730c2008-05-12 21:20:52 +0200738{
Thomas Gleixner05fa7852009-11-17 14:28:38 +0100739 return raw_spin_is_locked(&cpu_rq(cpu)->lock);
Ingo Molnar017730c2008-05-12 21:20:52 +0200740}
741
Ingo Molnarbf5c91b2007-10-15 17:00:04 +0200742/*
743 * Debugging: various feature bits
744 */
Peter Zijlstraf00b45c2008-04-19 19:45:00 +0200745
746#define SCHED_FEAT(name, enabled) \
747 __SCHED_FEAT_##name ,
748
Ingo Molnarbf5c91b2007-10-15 17:00:04 +0200749enum {
Peter Zijlstraf00b45c2008-04-19 19:45:00 +0200750#include "sched_features.h"
Ingo Molnarbf5c91b2007-10-15 17:00:04 +0200751};
752
Peter Zijlstraf00b45c2008-04-19 19:45:00 +0200753#undef SCHED_FEAT
Ingo Molnarbf5c91b2007-10-15 17:00:04 +0200754
Peter Zijlstraf00b45c2008-04-19 19:45:00 +0200755#define SCHED_FEAT(name, enabled) \
756 (1UL << __SCHED_FEAT_##name) * enabled |
757
758const_debug unsigned int sysctl_sched_features =
759#include "sched_features.h"
760 0;
761
762#undef SCHED_FEAT
763
764#ifdef CONFIG_SCHED_DEBUG
765#define SCHED_FEAT(name, enabled) \
766 #name ,
767
Harvey Harrison983ed7a2008-04-24 18:17:55 -0700768static __read_mostly char *sched_feat_names[] = {
Peter Zijlstraf00b45c2008-04-19 19:45:00 +0200769#include "sched_features.h"
770 NULL
771};
772
773#undef SCHED_FEAT
774
Li Zefan34f3a812008-10-30 15:23:32 +0800775static int sched_feat_show(struct seq_file *m, void *v)
Peter Zijlstraf00b45c2008-04-19 19:45:00 +0200776{
Peter Zijlstraf00b45c2008-04-19 19:45:00 +0200777 int i;
778
779 for (i = 0; sched_feat_names[i]; i++) {
Li Zefan34f3a812008-10-30 15:23:32 +0800780 if (!(sysctl_sched_features & (1UL << i)))
781 seq_puts(m, "NO_");
782 seq_printf(m, "%s ", sched_feat_names[i]);
Peter Zijlstraf00b45c2008-04-19 19:45:00 +0200783 }
Li Zefan34f3a812008-10-30 15:23:32 +0800784 seq_puts(m, "\n");
Peter Zijlstraf00b45c2008-04-19 19:45:00 +0200785
Li Zefan34f3a812008-10-30 15:23:32 +0800786 return 0;
Peter Zijlstraf00b45c2008-04-19 19:45:00 +0200787}
788
789static ssize_t
790sched_feat_write(struct file *filp, const char __user *ubuf,
791 size_t cnt, loff_t *ppos)
792{
793 char buf[64];
Mathieu Desnoyers77401912010-09-13 17:47:00 -0400794 char *cmp;
Peter Zijlstraf00b45c2008-04-19 19:45:00 +0200795 int neg = 0;
796 int i;
797
798 if (cnt > 63)
799 cnt = 63;
800
801 if (copy_from_user(&buf, ubuf, cnt))
802 return -EFAULT;
803
804 buf[cnt] = 0;
Mathieu Desnoyers77401912010-09-13 17:47:00 -0400805 cmp = strstrip(buf);
Peter Zijlstraf00b45c2008-04-19 19:45:00 +0200806
Hillf Danton524429c2011-01-06 20:58:12 +0800807 if (strncmp(cmp, "NO_", 3) == 0) {
Peter Zijlstraf00b45c2008-04-19 19:45:00 +0200808 neg = 1;
809 cmp += 3;
810 }
811
812 for (i = 0; sched_feat_names[i]; i++) {
Mathieu Desnoyers77401912010-09-13 17:47:00 -0400813 if (strcmp(cmp, sched_feat_names[i]) == 0) {
Peter Zijlstraf00b45c2008-04-19 19:45:00 +0200814 if (neg)
815 sysctl_sched_features &= ~(1UL << i);
816 else
817 sysctl_sched_features |= (1UL << i);
818 break;
819 }
820 }
821
822 if (!sched_feat_names[i])
823 return -EINVAL;
824
Jan Blunck42994722009-11-20 17:40:37 +0100825 *ppos += cnt;
Peter Zijlstraf00b45c2008-04-19 19:45:00 +0200826
827 return cnt;
828}
829
Li Zefan34f3a812008-10-30 15:23:32 +0800830static int sched_feat_open(struct inode *inode, struct file *filp)
831{
832 return single_open(filp, sched_feat_show, NULL);
833}
834
Alexey Dobriyan828c0952009-10-01 15:43:56 -0700835static const struct file_operations sched_feat_fops = {
Li Zefan34f3a812008-10-30 15:23:32 +0800836 .open = sched_feat_open,
837 .write = sched_feat_write,
838 .read = seq_read,
839 .llseek = seq_lseek,
840 .release = single_release,
Peter Zijlstraf00b45c2008-04-19 19:45:00 +0200841};
842
843static __init int sched_init_debug(void)
844{
Peter Zijlstraf00b45c2008-04-19 19:45:00 +0200845 debugfs_create_file("sched_features", 0644, NULL, NULL,
846 &sched_feat_fops);
847
848 return 0;
849}
850late_initcall(sched_init_debug);
851
852#endif
853
854#define sched_feat(x) (sysctl_sched_features & (1UL << __SCHED_FEAT_##x))
Ingo Molnarbf5c91b2007-10-15 17:00:04 +0200855
856/*
Peter Zijlstrab82d9fd2007-11-09 22:39:39 +0100857 * Number of tasks to iterate in a single balance run.
858 * Limited because this is done with IRQs disabled.
859 */
860const_debug unsigned int sysctl_sched_nr_migrate = 32;
861
862/*
Peter Zijlstrae9e92502009-09-01 10:34:37 +0200863 * period over which we average the RT time consumption, measured
864 * in ms.
865 *
866 * default: 1s
867 */
868const_debug unsigned int sysctl_sched_time_avg = MSEC_PER_SEC;
869
870/*
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +0100871 * period over which we measure -rt task cpu usage in us.
Peter Zijlstrafa85ae22008-01-25 21:08:29 +0100872 * default: 1s
873 */
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +0100874unsigned int sysctl_sched_rt_period = 1000000;
Peter Zijlstrafa85ae22008-01-25 21:08:29 +0100875
Ingo Molnar6892b752008-02-13 14:02:36 +0100876static __read_mostly int scheduler_running;
877
Peter Zijlstrafa85ae22008-01-25 21:08:29 +0100878/*
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +0100879 * part of the period that we allow rt tasks to run in us.
880 * default: 0.95s
Peter Zijlstrafa85ae22008-01-25 21:08:29 +0100881 */
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +0100882int sysctl_sched_rt_runtime = 950000;
883
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +0200884static inline u64 global_rt_period(void)
885{
886 return (u64)sysctl_sched_rt_period * NSEC_PER_USEC;
887}
888
889static inline u64 global_rt_runtime(void)
890{
roel kluine26873b2008-07-22 16:51:15 -0400891 if (sysctl_sched_rt_runtime < 0)
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +0200892 return RUNTIME_INF;
893
894 return (u64)sysctl_sched_rt_runtime * NSEC_PER_USEC;
895}
Peter Zijlstrafa85ae22008-01-25 21:08:29 +0100896
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897#ifndef prepare_arch_switch
Nick Piggin4866cde2005-06-25 14:57:23 -0700898# define prepare_arch_switch(next) do { } while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899#endif
Nick Piggin4866cde2005-06-25 14:57:23 -0700900#ifndef finish_arch_switch
901# define finish_arch_switch(prev) do { } while (0)
902#endif
903
Dmitry Adamushko051a1d12007-12-18 15:21:13 +0100904static inline int task_current(struct rq *rq, struct task_struct *p)
905{
906 return rq->curr == p;
907}
908
Ingo Molnar70b97a72006-07-03 00:25:42 -0700909static inline int task_running(struct rq *rq, struct task_struct *p)
Nick Piggin4866cde2005-06-25 14:57:23 -0700910{
Peter Zijlstra3ca7a442011-04-05 17:23:40 +0200911#ifdef CONFIG_SMP
912 return p->on_cpu;
913#else
Dmitry Adamushko051a1d12007-12-18 15:21:13 +0100914 return task_current(rq, p);
Peter Zijlstra3ca7a442011-04-05 17:23:40 +0200915#endif
Nick Piggin4866cde2005-06-25 14:57:23 -0700916}
917
Peter Zijlstra3ca7a442011-04-05 17:23:40 +0200918#ifndef __ARCH_WANT_UNLOCKED_CTXSW
Ingo Molnar70b97a72006-07-03 00:25:42 -0700919static inline void prepare_lock_switch(struct rq *rq, struct task_struct *next)
Nick Piggin4866cde2005-06-25 14:57:23 -0700920{
Peter Zijlstra3ca7a442011-04-05 17:23:40 +0200921#ifdef CONFIG_SMP
922 /*
923 * We can optimise this out completely for !SMP, because the
924 * SMP rebalancing from interrupt is the only thing that cares
925 * here.
926 */
927 next->on_cpu = 1;
928#endif
Nick Piggin4866cde2005-06-25 14:57:23 -0700929}
930
Ingo Molnar70b97a72006-07-03 00:25:42 -0700931static inline void finish_lock_switch(struct rq *rq, struct task_struct *prev)
Nick Piggin4866cde2005-06-25 14:57:23 -0700932{
Peter Zijlstra3ca7a442011-04-05 17:23:40 +0200933#ifdef CONFIG_SMP
934 /*
935 * After ->on_cpu is cleared, the task can be moved to a different CPU.
936 * We must ensure this doesn't happen until the switch is completely
937 * finished.
938 */
939 smp_wmb();
940 prev->on_cpu = 0;
941#endif
Ingo Molnarda04c032005-09-13 11:17:59 +0200942#ifdef CONFIG_DEBUG_SPINLOCK
943 /* this is a valid case when another task releases the spinlock */
944 rq->lock.owner = current;
945#endif
Ingo Molnar8a25d5d2006-07-03 00:24:54 -0700946 /*
947 * If we are tracking spinlock dependencies then we have to
948 * fix up the runqueue lock - which gets 'carried over' from
949 * prev into current:
950 */
951 spin_acquire(&rq->lock.dep_map, 0, 0, _THIS_IP_);
952
Thomas Gleixner05fa7852009-11-17 14:28:38 +0100953 raw_spin_unlock_irq(&rq->lock);
Nick Piggin4866cde2005-06-25 14:57:23 -0700954}
955
956#else /* __ARCH_WANT_UNLOCKED_CTXSW */
Ingo Molnar70b97a72006-07-03 00:25:42 -0700957static inline void prepare_lock_switch(struct rq *rq, struct task_struct *next)
Nick Piggin4866cde2005-06-25 14:57:23 -0700958{
959#ifdef CONFIG_SMP
960 /*
961 * We can optimise this out completely for !SMP, because the
962 * SMP rebalancing from interrupt is the only thing that cares
963 * here.
964 */
Peter Zijlstra3ca7a442011-04-05 17:23:40 +0200965 next->on_cpu = 1;
Nick Piggin4866cde2005-06-25 14:57:23 -0700966#endif
967#ifdef __ARCH_WANT_INTERRUPTS_ON_CTXSW
Thomas Gleixner05fa7852009-11-17 14:28:38 +0100968 raw_spin_unlock_irq(&rq->lock);
Nick Piggin4866cde2005-06-25 14:57:23 -0700969#else
Thomas Gleixner05fa7852009-11-17 14:28:38 +0100970 raw_spin_unlock(&rq->lock);
Nick Piggin4866cde2005-06-25 14:57:23 -0700971#endif
972}
973
Ingo Molnar70b97a72006-07-03 00:25:42 -0700974static inline void finish_lock_switch(struct rq *rq, struct task_struct *prev)
Nick Piggin4866cde2005-06-25 14:57:23 -0700975{
976#ifdef CONFIG_SMP
977 /*
Peter Zijlstra3ca7a442011-04-05 17:23:40 +0200978 * After ->on_cpu is cleared, the task can be moved to a different CPU.
Nick Piggin4866cde2005-06-25 14:57:23 -0700979 * We must ensure this doesn't happen until the switch is completely
980 * finished.
981 */
982 smp_wmb();
Peter Zijlstra3ca7a442011-04-05 17:23:40 +0200983 prev->on_cpu = 0;
Nick Piggin4866cde2005-06-25 14:57:23 -0700984#endif
985#ifndef __ARCH_WANT_INTERRUPTS_ON_CTXSW
986 local_irq_enable();
987#endif
988}
989#endif /* __ARCH_WANT_UNLOCKED_CTXSW */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990
991/*
Peter Zijlstra0122ec52011-04-05 17:23:51 +0200992 * __task_rq_lock - lock the rq @p resides on.
Ingo Molnarb29739f2006-06-27 02:54:51 -0700993 */
Ingo Molnar70b97a72006-07-03 00:25:42 -0700994static inline struct rq *__task_rq_lock(struct task_struct *p)
Ingo Molnarb29739f2006-06-27 02:54:51 -0700995 __acquires(rq->lock)
996{
Peter Zijlstra0970d292010-02-15 14:45:54 +0100997 struct rq *rq;
998
Peter Zijlstra0122ec52011-04-05 17:23:51 +0200999 lockdep_assert_held(&p->pi_lock);
1000
Andi Kleen3a5c3592007-10-15 17:00:14 +02001001 for (;;) {
Peter Zijlstra0970d292010-02-15 14:45:54 +01001002 rq = task_rq(p);
Thomas Gleixner05fa7852009-11-17 14:28:38 +01001003 raw_spin_lock(&rq->lock);
Peter Zijlstra65cc8e42010-03-25 21:05:16 +01001004 if (likely(rq == task_rq(p)))
Andi Kleen3a5c3592007-10-15 17:00:14 +02001005 return rq;
Thomas Gleixner05fa7852009-11-17 14:28:38 +01001006 raw_spin_unlock(&rq->lock);
Ingo Molnarb29739f2006-06-27 02:54:51 -07001007 }
Ingo Molnarb29739f2006-06-27 02:54:51 -07001008}
1009
1010/*
Peter Zijlstra0122ec52011-04-05 17:23:51 +02001011 * task_rq_lock - lock p->pi_lock and lock the rq @p resides on.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012 */
Ingo Molnar70b97a72006-07-03 00:25:42 -07001013static struct rq *task_rq_lock(struct task_struct *p, unsigned long *flags)
Peter Zijlstra0122ec52011-04-05 17:23:51 +02001014 __acquires(p->pi_lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015 __acquires(rq->lock)
1016{
Ingo Molnar70b97a72006-07-03 00:25:42 -07001017 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018
Andi Kleen3a5c3592007-10-15 17:00:14 +02001019 for (;;) {
Peter Zijlstra0122ec52011-04-05 17:23:51 +02001020 raw_spin_lock_irqsave(&p->pi_lock, *flags);
Andi Kleen3a5c3592007-10-15 17:00:14 +02001021 rq = task_rq(p);
Thomas Gleixner05fa7852009-11-17 14:28:38 +01001022 raw_spin_lock(&rq->lock);
Peter Zijlstra65cc8e42010-03-25 21:05:16 +01001023 if (likely(rq == task_rq(p)))
Andi Kleen3a5c3592007-10-15 17:00:14 +02001024 return rq;
Peter Zijlstra0122ec52011-04-05 17:23:51 +02001025 raw_spin_unlock(&rq->lock);
1026 raw_spin_unlock_irqrestore(&p->pi_lock, *flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028}
1029
Alexey Dobriyana9957442007-10-15 17:00:13 +02001030static void __task_rq_unlock(struct rq *rq)
Ingo Molnarb29739f2006-06-27 02:54:51 -07001031 __releases(rq->lock)
1032{
Thomas Gleixner05fa7852009-11-17 14:28:38 +01001033 raw_spin_unlock(&rq->lock);
Ingo Molnarb29739f2006-06-27 02:54:51 -07001034}
1035
Peter Zijlstra0122ec52011-04-05 17:23:51 +02001036static inline void
1037task_rq_unlock(struct rq *rq, struct task_struct *p, unsigned long *flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038 __releases(rq->lock)
Peter Zijlstra0122ec52011-04-05 17:23:51 +02001039 __releases(p->pi_lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040{
Peter Zijlstra0122ec52011-04-05 17:23:51 +02001041 raw_spin_unlock(&rq->lock);
1042 raw_spin_unlock_irqrestore(&p->pi_lock, *flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043}
1044
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045/*
Robert P. J. Daycc2a73b2006-12-10 02:20:00 -08001046 * this_rq_lock - lock this runqueue and disable interrupts.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047 */
Alexey Dobriyana9957442007-10-15 17:00:13 +02001048static struct rq *this_rq_lock(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049 __acquires(rq->lock)
1050{
Ingo Molnar70b97a72006-07-03 00:25:42 -07001051 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052
1053 local_irq_disable();
1054 rq = this_rq();
Thomas Gleixner05fa7852009-11-17 14:28:38 +01001055 raw_spin_lock(&rq->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056
1057 return rq;
1058}
1059
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01001060#ifdef CONFIG_SCHED_HRTICK
1061/*
1062 * Use HR-timers to deliver accurate preemption points.
1063 *
1064 * Its all a bit involved since we cannot program an hrt while holding the
1065 * rq->lock. So what we do is store a state in in rq->hrtick_* and ask for a
1066 * reschedule event.
1067 *
1068 * When we get rescheduled we reprogram the hrtick_timer outside of the
1069 * rq->lock.
1070 */
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01001071
1072/*
1073 * Use hrtick when:
1074 * - enabled by features
1075 * - hrtimer is actually high res
1076 */
1077static inline int hrtick_enabled(struct rq *rq)
1078{
1079 if (!sched_feat(HRTICK))
1080 return 0;
Ingo Molnarba420592008-07-20 11:02:06 +02001081 if (!cpu_active(cpu_of(rq)))
Peter Zijlstrab328ca12008-04-29 10:02:46 +02001082 return 0;
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01001083 return hrtimer_is_hres_active(&rq->hrtick_timer);
1084}
1085
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01001086static void hrtick_clear(struct rq *rq)
1087{
1088 if (hrtimer_active(&rq->hrtick_timer))
1089 hrtimer_cancel(&rq->hrtick_timer);
1090}
1091
1092/*
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01001093 * High-resolution timer tick.
1094 * Runs from hardirq context with interrupts disabled.
1095 */
1096static enum hrtimer_restart hrtick(struct hrtimer *timer)
1097{
1098 struct rq *rq = container_of(timer, struct rq, hrtick_timer);
1099
1100 WARN_ON_ONCE(cpu_of(rq) != smp_processor_id());
1101
Thomas Gleixner05fa7852009-11-17 14:28:38 +01001102 raw_spin_lock(&rq->lock);
Peter Zijlstra3e51f332008-05-03 18:29:28 +02001103 update_rq_clock(rq);
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01001104 rq->curr->sched_class->task_tick(rq, rq->curr, 1);
Thomas Gleixner05fa7852009-11-17 14:28:38 +01001105 raw_spin_unlock(&rq->lock);
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01001106
1107 return HRTIMER_NORESTART;
1108}
1109
Rabin Vincent95e904c2008-05-11 05:55:33 +05301110#ifdef CONFIG_SMP
Peter Zijlstra31656512008-07-18 18:01:23 +02001111/*
1112 * called from hardirq (IPI) context
1113 */
1114static void __hrtick_start(void *arg)
Peter Zijlstrab328ca12008-04-29 10:02:46 +02001115{
Peter Zijlstra31656512008-07-18 18:01:23 +02001116 struct rq *rq = arg;
Peter Zijlstrab328ca12008-04-29 10:02:46 +02001117
Thomas Gleixner05fa7852009-11-17 14:28:38 +01001118 raw_spin_lock(&rq->lock);
Peter Zijlstra31656512008-07-18 18:01:23 +02001119 hrtimer_restart(&rq->hrtick_timer);
1120 rq->hrtick_csd_pending = 0;
Thomas Gleixner05fa7852009-11-17 14:28:38 +01001121 raw_spin_unlock(&rq->lock);
Peter Zijlstrab328ca12008-04-29 10:02:46 +02001122}
1123
Peter Zijlstra31656512008-07-18 18:01:23 +02001124/*
1125 * Called to set the hrtick timer state.
1126 *
1127 * called with rq->lock held and irqs disabled
1128 */
1129static void hrtick_start(struct rq *rq, u64 delay)
Peter Zijlstrab328ca12008-04-29 10:02:46 +02001130{
Peter Zijlstra31656512008-07-18 18:01:23 +02001131 struct hrtimer *timer = &rq->hrtick_timer;
1132 ktime_t time = ktime_add_ns(timer->base->get_time(), delay);
Peter Zijlstrab328ca12008-04-29 10:02:46 +02001133
Arjan van de Vencc584b22008-09-01 15:02:30 -07001134 hrtimer_set_expires(timer, time);
Peter Zijlstra31656512008-07-18 18:01:23 +02001135
1136 if (rq == this_rq()) {
1137 hrtimer_restart(timer);
1138 } else if (!rq->hrtick_csd_pending) {
Peter Zijlstra6e275632009-02-25 13:59:48 +01001139 __smp_call_function_single(cpu_of(rq), &rq->hrtick_csd, 0);
Peter Zijlstra31656512008-07-18 18:01:23 +02001140 rq->hrtick_csd_pending = 1;
1141 }
Peter Zijlstrab328ca12008-04-29 10:02:46 +02001142}
1143
1144static int
1145hotplug_hrtick(struct notifier_block *nfb, unsigned long action, void *hcpu)
1146{
1147 int cpu = (int)(long)hcpu;
1148
1149 switch (action) {
1150 case CPU_UP_CANCELED:
1151 case CPU_UP_CANCELED_FROZEN:
1152 case CPU_DOWN_PREPARE:
1153 case CPU_DOWN_PREPARE_FROZEN:
1154 case CPU_DEAD:
1155 case CPU_DEAD_FROZEN:
Peter Zijlstra31656512008-07-18 18:01:23 +02001156 hrtick_clear(cpu_rq(cpu));
Peter Zijlstrab328ca12008-04-29 10:02:46 +02001157 return NOTIFY_OK;
1158 }
1159
1160 return NOTIFY_DONE;
1161}
1162
Rakib Mullickfa748202008-09-22 14:55:45 -07001163static __init void init_hrtick(void)
Peter Zijlstrab328ca12008-04-29 10:02:46 +02001164{
1165 hotcpu_notifier(hotplug_hrtick, 0);
1166}
Peter Zijlstra31656512008-07-18 18:01:23 +02001167#else
1168/*
1169 * Called to set the hrtick timer state.
1170 *
1171 * called with rq->lock held and irqs disabled
1172 */
1173static void hrtick_start(struct rq *rq, u64 delay)
1174{
Peter Zijlstra7f1e2ca2009-03-13 12:21:27 +01001175 __hrtimer_start_range_ns(&rq->hrtick_timer, ns_to_ktime(delay), 0,
Arun R Bharadwaj5c333862009-04-16 12:14:37 +05301176 HRTIMER_MODE_REL_PINNED, 0);
Peter Zijlstra31656512008-07-18 18:01:23 +02001177}
1178
Andrew Morton006c75f2008-09-22 14:55:46 -07001179static inline void init_hrtick(void)
Peter Zijlstra31656512008-07-18 18:01:23 +02001180{
1181}
Rabin Vincent95e904c2008-05-11 05:55:33 +05301182#endif /* CONFIG_SMP */
Peter Zijlstrab328ca12008-04-29 10:02:46 +02001183
1184static void init_rq_hrtick(struct rq *rq)
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01001185{
Peter Zijlstra31656512008-07-18 18:01:23 +02001186#ifdef CONFIG_SMP
1187 rq->hrtick_csd_pending = 0;
1188
1189 rq->hrtick_csd.flags = 0;
1190 rq->hrtick_csd.func = __hrtick_start;
1191 rq->hrtick_csd.info = rq;
1192#endif
1193
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01001194 hrtimer_init(&rq->hrtick_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
1195 rq->hrtick_timer.function = hrtick;
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01001196}
Andrew Morton006c75f2008-09-22 14:55:46 -07001197#else /* CONFIG_SCHED_HRTICK */
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01001198static inline void hrtick_clear(struct rq *rq)
1199{
1200}
1201
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01001202static inline void init_rq_hrtick(struct rq *rq)
1203{
1204}
1205
Peter Zijlstrab328ca12008-04-29 10:02:46 +02001206static inline void init_hrtick(void)
1207{
1208}
Andrew Morton006c75f2008-09-22 14:55:46 -07001209#endif /* CONFIG_SCHED_HRTICK */
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01001210
Ingo Molnar1b9f19c2007-07-09 18:51:59 +02001211/*
Ingo Molnarc24d20d2007-07-09 18:51:59 +02001212 * resched_task - mark a task 'to be rescheduled now'.
1213 *
1214 * On UP this means the setting of the need_resched flag, on SMP it
1215 * might also involve a cross-CPU call to trigger the scheduler on
1216 * the target CPU.
1217 */
1218#ifdef CONFIG_SMP
1219
1220#ifndef tsk_is_polling
1221#define tsk_is_polling(t) test_tsk_thread_flag(t, TIF_POLLING_NRFLAG)
1222#endif
1223
Peter Zijlstra31656512008-07-18 18:01:23 +02001224static void resched_task(struct task_struct *p)
Ingo Molnarc24d20d2007-07-09 18:51:59 +02001225{
1226 int cpu;
1227
Thomas Gleixner05fa7852009-11-17 14:28:38 +01001228 assert_raw_spin_locked(&task_rq(p)->lock);
Ingo Molnarc24d20d2007-07-09 18:51:59 +02001229
Lai Jiangshan5ed0cec2009-03-06 19:40:20 +08001230 if (test_tsk_need_resched(p))
Ingo Molnarc24d20d2007-07-09 18:51:59 +02001231 return;
1232
Lai Jiangshan5ed0cec2009-03-06 19:40:20 +08001233 set_tsk_need_resched(p);
Ingo Molnarc24d20d2007-07-09 18:51:59 +02001234
1235 cpu = task_cpu(p);
1236 if (cpu == smp_processor_id())
1237 return;
1238
1239 /* NEED_RESCHED must be visible before we test polling */
1240 smp_mb();
1241 if (!tsk_is_polling(p))
1242 smp_send_reschedule(cpu);
1243}
1244
1245static void resched_cpu(int cpu)
1246{
1247 struct rq *rq = cpu_rq(cpu);
1248 unsigned long flags;
1249
Thomas Gleixner05fa7852009-11-17 14:28:38 +01001250 if (!raw_spin_trylock_irqsave(&rq->lock, flags))
Ingo Molnarc24d20d2007-07-09 18:51:59 +02001251 return;
1252 resched_task(cpu_curr(cpu));
Thomas Gleixner05fa7852009-11-17 14:28:38 +01001253 raw_spin_unlock_irqrestore(&rq->lock, flags);
Ingo Molnarc24d20d2007-07-09 18:51:59 +02001254}
Thomas Gleixner06d83082008-03-22 09:20:24 +01001255
1256#ifdef CONFIG_NO_HZ
1257/*
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07001258 * In the semi idle case, use the nearest busy cpu for migrating timers
1259 * from an idle cpu. This is good for power-savings.
1260 *
1261 * We don't do similar optimization for completely idle system, as
1262 * selecting an idle cpu will add more delays to the timers than intended
1263 * (as that cpu's timer base may not be uptodate wrt jiffies etc).
1264 */
1265int get_nohz_timer_target(void)
1266{
1267 int cpu = smp_processor_id();
1268 int i;
1269 struct sched_domain *sd;
1270
Peter Zijlstra057f3fa2011-04-18 11:24:34 +02001271 rcu_read_lock();
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07001272 for_each_domain(cpu, sd) {
Peter Zijlstra057f3fa2011-04-18 11:24:34 +02001273 for_each_cpu(i, sched_domain_span(sd)) {
1274 if (!idle_cpu(i)) {
1275 cpu = i;
1276 goto unlock;
1277 }
1278 }
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07001279 }
Peter Zijlstra057f3fa2011-04-18 11:24:34 +02001280unlock:
1281 rcu_read_unlock();
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07001282 return cpu;
1283}
1284/*
Thomas Gleixner06d83082008-03-22 09:20:24 +01001285 * When add_timer_on() enqueues a timer into the timer wheel of an
1286 * idle CPU then this timer might expire before the next timer event
1287 * which is scheduled to wake up that CPU. In case of a completely
1288 * idle system the next event might even be infinite time into the
1289 * future. wake_up_idle_cpu() ensures that the CPU is woken up and
1290 * leaves the inner idle loop so the newly added timer is taken into
1291 * account when the CPU goes back to idle and evaluates the timer
1292 * wheel for the next timer event.
1293 */
1294void wake_up_idle_cpu(int cpu)
1295{
1296 struct rq *rq = cpu_rq(cpu);
1297
1298 if (cpu == smp_processor_id())
1299 return;
1300
1301 /*
1302 * This is safe, as this function is called with the timer
1303 * wheel base lock of (cpu) held. When the CPU is on the way
1304 * to idle and has not yet set rq->curr to idle then it will
1305 * be serialized on the timer wheel base lock and take the new
1306 * timer into account automatically.
1307 */
1308 if (rq->curr != rq->idle)
1309 return;
1310
1311 /*
1312 * We can set TIF_RESCHED on the idle task of the other CPU
1313 * lockless. The worst case is that the other CPU runs the
1314 * idle task through an additional NOOP schedule()
1315 */
Lai Jiangshan5ed0cec2009-03-06 19:40:20 +08001316 set_tsk_need_resched(rq->idle);
Thomas Gleixner06d83082008-03-22 09:20:24 +01001317
1318 /* NEED_RESCHED must be visible before we test polling */
1319 smp_mb();
1320 if (!tsk_is_polling(rq->idle))
1321 smp_send_reschedule(cpu);
1322}
Mike Galbraith39c0cbe2010-03-11 17:17:13 +01001323
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +02001324#endif /* CONFIG_NO_HZ */
Thomas Gleixner06d83082008-03-22 09:20:24 +01001325
Peter Zijlstrae9e92502009-09-01 10:34:37 +02001326static u64 sched_avg_period(void)
1327{
1328 return (u64)sysctl_sched_time_avg * NSEC_PER_MSEC / 2;
1329}
1330
1331static void sched_avg_update(struct rq *rq)
1332{
1333 s64 period = sched_avg_period();
1334
1335 while ((s64)(rq->clock - rq->age_stamp) > period) {
Will Deacon0d98bb22010-05-24 12:11:43 -07001336 /*
1337 * Inline assembly required to prevent the compiler
1338 * optimising this loop into a divmod call.
1339 * See __iter_div_u64_rem() for another example of this.
1340 */
1341 asm("" : "+rm" (rq->age_stamp));
Peter Zijlstrae9e92502009-09-01 10:34:37 +02001342 rq->age_stamp += period;
1343 rq->rt_avg /= 2;
1344 }
1345}
1346
1347static void sched_rt_avg_update(struct rq *rq, u64 rt_delta)
1348{
1349 rq->rt_avg += rt_delta;
1350 sched_avg_update(rq);
1351}
1352
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +02001353#else /* !CONFIG_SMP */
Peter Zijlstra31656512008-07-18 18:01:23 +02001354static void resched_task(struct task_struct *p)
Ingo Molnarc24d20d2007-07-09 18:51:59 +02001355{
Thomas Gleixner05fa7852009-11-17 14:28:38 +01001356 assert_raw_spin_locked(&task_rq(p)->lock);
Peter Zijlstra31656512008-07-18 18:01:23 +02001357 set_tsk_need_resched(p);
Ingo Molnarc24d20d2007-07-09 18:51:59 +02001358}
Peter Zijlstrae9e92502009-09-01 10:34:37 +02001359
1360static void sched_rt_avg_update(struct rq *rq, u64 rt_delta)
1361{
1362}
Suresh Siddhada2b71e2010-08-23 13:42:51 -07001363
1364static void sched_avg_update(struct rq *rq)
1365{
1366}
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +02001367#endif /* CONFIG_SMP */
Ingo Molnarc24d20d2007-07-09 18:51:59 +02001368
Ingo Molnar45bf76d2007-07-09 18:51:59 +02001369#if BITS_PER_LONG == 32
1370# define WMULT_CONST (~0UL)
1371#else
1372# define WMULT_CONST (1UL << 32)
1373#endif
1374
1375#define WMULT_SHIFT 32
1376
Ingo Molnar194081e2007-08-09 11:16:51 +02001377/*
1378 * Shift right and round:
1379 */
Ingo Molnarcf2ab462007-09-05 14:32:49 +02001380#define SRR(x, y) (((x) + (1UL << ((y) - 1))) >> (y))
Ingo Molnar194081e2007-08-09 11:16:51 +02001381
Peter Zijlstraa7be37a2008-06-27 13:41:11 +02001382/*
1383 * delta *= weight / lw
1384 */
Ingo Molnarcb1c4fc2007-08-02 17:41:40 +02001385static unsigned long
Ingo Molnar45bf76d2007-07-09 18:51:59 +02001386calc_delta_mine(unsigned long delta_exec, unsigned long weight,
1387 struct load_weight *lw)
1388{
1389 u64 tmp;
1390
Nikhil Raoc8b28112011-05-18 14:37:48 -07001391 /*
1392 * weight can be less than 2^SCHED_LOAD_RESOLUTION for task group sched
1393 * entities since MIN_SHARES = 2. Treat weight as 1 if less than
1394 * 2^SCHED_LOAD_RESOLUTION.
1395 */
1396 if (likely(weight > (1UL << SCHED_LOAD_RESOLUTION)))
1397 tmp = (u64)delta_exec * scale_load_down(weight);
1398 else
1399 tmp = (u64)delta_exec;
Stephan Baerwolfdb670da2011-05-11 18:03:29 +02001400
Lai Jiangshan7a232e02008-06-12 16:43:07 +08001401 if (!lw->inv_weight) {
Nikhil Raoc8b28112011-05-18 14:37:48 -07001402 unsigned long w = scale_load_down(lw->weight);
1403
1404 if (BITS_PER_LONG > 32 && unlikely(w >= WMULT_CONST))
Lai Jiangshan7a232e02008-06-12 16:43:07 +08001405 lw->inv_weight = 1;
Nikhil Raoc8b28112011-05-18 14:37:48 -07001406 else if (unlikely(!w))
1407 lw->inv_weight = WMULT_CONST;
Lai Jiangshan7a232e02008-06-12 16:43:07 +08001408 else
Nikhil Raoc8b28112011-05-18 14:37:48 -07001409 lw->inv_weight = WMULT_CONST / w;
Lai Jiangshan7a232e02008-06-12 16:43:07 +08001410 }
Ingo Molnar45bf76d2007-07-09 18:51:59 +02001411
Ingo Molnar45bf76d2007-07-09 18:51:59 +02001412 /*
1413 * Check whether we'd overflow the 64-bit multiplication:
1414 */
Ingo Molnar194081e2007-08-09 11:16:51 +02001415 if (unlikely(tmp > WMULT_CONST))
Ingo Molnarcf2ab462007-09-05 14:32:49 +02001416 tmp = SRR(SRR(tmp, WMULT_SHIFT/2) * lw->inv_weight,
Ingo Molnar194081e2007-08-09 11:16:51 +02001417 WMULT_SHIFT/2);
1418 else
Ingo Molnarcf2ab462007-09-05 14:32:49 +02001419 tmp = SRR(tmp * lw->inv_weight, WMULT_SHIFT);
Ingo Molnar45bf76d2007-07-09 18:51:59 +02001420
Ingo Molnarecf691d2007-08-02 17:41:40 +02001421 return (unsigned long)min(tmp, (u64)(unsigned long)LONG_MAX);
Ingo Molnar45bf76d2007-07-09 18:51:59 +02001422}
1423
Ingo Molnar10919852007-10-15 17:00:04 +02001424static inline void update_load_add(struct load_weight *lw, unsigned long inc)
Ingo Molnar45bf76d2007-07-09 18:51:59 +02001425{
1426 lw->weight += inc;
Ingo Molnare89996a2008-03-14 23:48:28 +01001427 lw->inv_weight = 0;
Ingo Molnar45bf76d2007-07-09 18:51:59 +02001428}
1429
Ingo Molnar10919852007-10-15 17:00:04 +02001430static inline void update_load_sub(struct load_weight *lw, unsigned long dec)
Ingo Molnar45bf76d2007-07-09 18:51:59 +02001431{
1432 lw->weight -= dec;
Ingo Molnare89996a2008-03-14 23:48:28 +01001433 lw->inv_weight = 0;
Ingo Molnar45bf76d2007-07-09 18:51:59 +02001434}
1435
Peter Zijlstra2069dd72010-11-15 15:47:00 -08001436static inline void update_load_set(struct load_weight *lw, unsigned long w)
1437{
1438 lw->weight = w;
1439 lw->inv_weight = 0;
1440}
1441
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442/*
Peter Williams2dd73a42006-06-27 02:54:34 -07001443 * To aid in avoiding the subversion of "niceness" due to uneven distribution
1444 * of tasks with abnormal "nice" values across CPUs the contribution that
1445 * each task makes to its run queue's load is weighted according to its
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01001446 * scheduling class and "nice" value. For SCHED_NORMAL tasks this is just a
Peter Williams2dd73a42006-06-27 02:54:34 -07001447 * scaled version of the new time slice allocation that they receive on time
1448 * slice expiry etc.
1449 */
1450
Peter Zijlstracce7ade2009-01-15 14:53:37 +01001451#define WEIGHT_IDLEPRIO 3
1452#define WMULT_IDLEPRIO 1431655765
Ingo Molnardd41f592007-07-09 18:51:59 +02001453
1454/*
1455 * Nice levels are multiplicative, with a gentle 10% change for every
1456 * nice level changed. I.e. when a CPU-bound task goes from nice 0 to
1457 * nice 1, it will get ~10% less CPU time than another CPU-bound task
1458 * that remained on nice 0.
1459 *
1460 * The "10% effect" is relative and cumulative: from _any_ nice level,
1461 * if you go up 1 level, it's -10% CPU usage, if you go down 1 level
Ingo Molnarf9153ee2007-07-16 09:46:30 +02001462 * it's +10% CPU usage. (to achieve that we use a multiplier of 1.25.
1463 * If a task goes up by ~10% and another task goes down by ~10% then
1464 * the relative distance between them is ~25%.)
Ingo Molnardd41f592007-07-09 18:51:59 +02001465 */
1466static const int prio_to_weight[40] = {
Ingo Molnar254753d2007-08-09 11:16:51 +02001467 /* -20 */ 88761, 71755, 56483, 46273, 36291,
1468 /* -15 */ 29154, 23254, 18705, 14949, 11916,
1469 /* -10 */ 9548, 7620, 6100, 4904, 3906,
1470 /* -5 */ 3121, 2501, 1991, 1586, 1277,
1471 /* 0 */ 1024, 820, 655, 526, 423,
1472 /* 5 */ 335, 272, 215, 172, 137,
1473 /* 10 */ 110, 87, 70, 56, 45,
1474 /* 15 */ 36, 29, 23, 18, 15,
Ingo Molnardd41f592007-07-09 18:51:59 +02001475};
1476
Ingo Molnar5714d2d2007-07-16 09:46:31 +02001477/*
1478 * Inverse (2^32/x) values of the prio_to_weight[] array, precalculated.
1479 *
1480 * In cases where the weight does not change often, we can use the
1481 * precalculated inverse to speed up arithmetics by turning divisions
1482 * into multiplications:
1483 */
Ingo Molnardd41f592007-07-09 18:51:59 +02001484static const u32 prio_to_wmult[40] = {
Ingo Molnar254753d2007-08-09 11:16:51 +02001485 /* -20 */ 48388, 59856, 76040, 92818, 118348,
1486 /* -15 */ 147320, 184698, 229616, 287308, 360437,
1487 /* -10 */ 449829, 563644, 704093, 875809, 1099582,
1488 /* -5 */ 1376151, 1717300, 2157191, 2708050, 3363326,
1489 /* 0 */ 4194304, 5237765, 6557202, 8165337, 10153587,
1490 /* 5 */ 12820798, 15790321, 19976592, 24970740, 31350126,
1491 /* 10 */ 39045157, 49367440, 61356676, 76695844, 95443717,
1492 /* 15 */ 119304647, 148102320, 186737708, 238609294, 286331153,
Ingo Molnardd41f592007-07-09 18:51:59 +02001493};
Peter Williams2dd73a42006-06-27 02:54:34 -07001494
Bharata B Raoef12fef2009-03-31 10:02:22 +05301495/* Time spent by the tasks of the cpu accounting group executing in ... */
1496enum cpuacct_stat_index {
1497 CPUACCT_STAT_USER, /* ... user mode */
1498 CPUACCT_STAT_SYSTEM, /* ... kernel mode */
1499
1500 CPUACCT_STAT_NSTATS,
1501};
1502
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01001503#ifdef CONFIG_CGROUP_CPUACCT
1504static void cpuacct_charge(struct task_struct *tsk, u64 cputime);
Bharata B Raoef12fef2009-03-31 10:02:22 +05301505static void cpuacct_update_stats(struct task_struct *tsk,
1506 enum cpuacct_stat_index idx, cputime_t val);
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01001507#else
1508static inline void cpuacct_charge(struct task_struct *tsk, u64 cputime) {}
Bharata B Raoef12fef2009-03-31 10:02:22 +05301509static inline void cpuacct_update_stats(struct task_struct *tsk,
1510 enum cpuacct_stat_index idx, cputime_t val) {}
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01001511#endif
1512
Peter Zijlstra18d95a22008-04-19 19:45:00 +02001513static inline void inc_cpu_load(struct rq *rq, unsigned long load)
1514{
1515 update_load_add(&rq->load, load);
1516}
1517
1518static inline void dec_cpu_load(struct rq *rq, unsigned long load)
1519{
1520 update_load_sub(&rq->load, load);
1521}
1522
Paul Turnera790de92011-07-21 09:43:29 -07001523#if defined(CONFIG_RT_GROUP_SCHED) || (defined(CONFIG_FAIR_GROUP_SCHED) && \
1524 (defined(CONFIG_SMP) || defined(CONFIG_CFS_BANDWIDTH)))
Peter Zijlstraeb755802008-08-19 12:33:05 +02001525typedef int (*tg_visitor)(struct task_group *, void *);
1526
1527/*
1528 * Iterate the full tree, calling @down when first entering a node and @up when
1529 * leaving it for the final time.
1530 */
1531static int walk_tg_tree(tg_visitor down, tg_visitor up, void *data)
1532{
1533 struct task_group *parent, *child;
1534 int ret;
1535
1536 rcu_read_lock();
1537 parent = &root_task_group;
1538down:
1539 ret = (*down)(parent, data);
1540 if (ret)
1541 goto out_unlock;
1542 list_for_each_entry_rcu(child, &parent->children, siblings) {
1543 parent = child;
1544 goto down;
1545
1546up:
1547 continue;
1548 }
1549 ret = (*up)(parent, data);
1550 if (ret)
1551 goto out_unlock;
1552
1553 child = parent;
1554 parent = parent->parent;
1555 if (parent)
1556 goto up;
1557out_unlock:
1558 rcu_read_unlock();
1559
1560 return ret;
1561}
1562
1563static int tg_nop(struct task_group *tg, void *data)
1564{
1565 return 0;
1566}
1567#endif
1568
Gregory Haskinse7693a32008-01-25 21:08:09 +01001569#ifdef CONFIG_SMP
Peter Zijlstraf5f08f32009-09-10 13:35:28 +02001570/* Used instead of source_load when we know the type == 0 */
1571static unsigned long weighted_cpuload(const int cpu)
1572{
1573 return cpu_rq(cpu)->load.weight;
1574}
1575
1576/*
1577 * Return a low guess at the load of a migration-source cpu weighted
1578 * according to the scheduling class and "nice" value.
1579 *
1580 * We want to under-estimate the load of migration sources, to
1581 * balance conservatively.
1582 */
1583static unsigned long source_load(int cpu, int type)
1584{
1585 struct rq *rq = cpu_rq(cpu);
1586 unsigned long total = weighted_cpuload(cpu);
1587
1588 if (type == 0 || !sched_feat(LB_BIAS))
1589 return total;
1590
1591 return min(rq->cpu_load[type-1], total);
1592}
1593
1594/*
1595 * Return a high guess at the load of a migration-target cpu weighted
1596 * according to the scheduling class and "nice" value.
1597 */
1598static unsigned long target_load(int cpu, int type)
1599{
1600 struct rq *rq = cpu_rq(cpu);
1601 unsigned long total = weighted_cpuload(cpu);
1602
1603 if (type == 0 || !sched_feat(LB_BIAS))
1604 return total;
1605
1606 return max(rq->cpu_load[type-1], total);
1607}
1608
Peter Zijlstraae154be2009-09-10 14:40:57 +02001609static unsigned long power_of(int cpu)
1610{
Peter Zijlstrae51fd5e2010-05-31 12:37:30 +02001611 return cpu_rq(cpu)->cpu_power;
Peter Zijlstraae154be2009-09-10 14:40:57 +02001612}
1613
Gregory Haskinse7693a32008-01-25 21:08:09 +01001614static int task_hot(struct task_struct *p, u64 now, struct sched_domain *sd);
Peter Zijlstrac09595f2008-06-27 13:41:14 +02001615
Peter Zijlstraa8a51d52008-06-27 13:41:26 +02001616static unsigned long cpu_avg_load_per_task(int cpu)
1617{
1618 struct rq *rq = cpu_rq(cpu);
Ingo Molnaraf6d5962008-11-29 20:45:15 +01001619 unsigned long nr_running = ACCESS_ONCE(rq->nr_running);
Peter Zijlstraa8a51d52008-06-27 13:41:26 +02001620
Steven Rostedt4cd42622008-11-26 21:04:24 -05001621 if (nr_running)
Jan H. Schönherre2b245f2011-08-01 11:03:28 +02001622 return rq->load.weight / nr_running;
Peter Zijlstraa8a51d52008-06-27 13:41:26 +02001623
Jan H. Schönherre2b245f2011-08-01 11:03:28 +02001624 return 0;
Peter Zijlstraa8a51d52008-06-27 13:41:26 +02001625}
1626
Gregory Haskins8f45e2b2008-12-29 09:39:51 -05001627#ifdef CONFIG_PREEMPT
1628
Peter Zijlstrab78bb862009-09-15 14:23:18 +02001629static void double_rq_lock(struct rq *rq1, struct rq *rq2);
1630
Alexey Dobriyan70574a92008-11-28 22:08:00 +03001631/*
Gregory Haskins8f45e2b2008-12-29 09:39:51 -05001632 * fair double_lock_balance: Safely acquires both rq->locks in a fair
1633 * way at the expense of forcing extra atomic operations in all
1634 * invocations. This assures that the double_lock is acquired using the
1635 * same underlying policy as the spinlock_t on this architecture, which
1636 * reduces latency compared to the unfair variant below. However, it
1637 * also adds more overhead and therefore may reduce throughput.
Alexey Dobriyan70574a92008-11-28 22:08:00 +03001638 */
Gregory Haskins8f45e2b2008-12-29 09:39:51 -05001639static inline int _double_lock_balance(struct rq *this_rq, struct rq *busiest)
1640 __releases(this_rq->lock)
1641 __acquires(busiest->lock)
1642 __acquires(this_rq->lock)
1643{
Thomas Gleixner05fa7852009-11-17 14:28:38 +01001644 raw_spin_unlock(&this_rq->lock);
Gregory Haskins8f45e2b2008-12-29 09:39:51 -05001645 double_rq_lock(this_rq, busiest);
1646
1647 return 1;
1648}
1649
1650#else
1651/*
1652 * Unfair double_lock_balance: Optimizes throughput at the expense of
1653 * latency by eliminating extra atomic operations when the locks are
1654 * already in proper order on entry. This favors lower cpu-ids and will
1655 * grant the double lock to lower cpus over higher ids under contention,
1656 * regardless of entry order into the function.
1657 */
1658static int _double_lock_balance(struct rq *this_rq, struct rq *busiest)
Alexey Dobriyan70574a92008-11-28 22:08:00 +03001659 __releases(this_rq->lock)
1660 __acquires(busiest->lock)
1661 __acquires(this_rq->lock)
1662{
1663 int ret = 0;
1664
Thomas Gleixner05fa7852009-11-17 14:28:38 +01001665 if (unlikely(!raw_spin_trylock(&busiest->lock))) {
Alexey Dobriyan70574a92008-11-28 22:08:00 +03001666 if (busiest < this_rq) {
Thomas Gleixner05fa7852009-11-17 14:28:38 +01001667 raw_spin_unlock(&this_rq->lock);
1668 raw_spin_lock(&busiest->lock);
1669 raw_spin_lock_nested(&this_rq->lock,
1670 SINGLE_DEPTH_NESTING);
Alexey Dobriyan70574a92008-11-28 22:08:00 +03001671 ret = 1;
1672 } else
Thomas Gleixner05fa7852009-11-17 14:28:38 +01001673 raw_spin_lock_nested(&busiest->lock,
1674 SINGLE_DEPTH_NESTING);
Alexey Dobriyan70574a92008-11-28 22:08:00 +03001675 }
1676 return ret;
1677}
1678
Gregory Haskins8f45e2b2008-12-29 09:39:51 -05001679#endif /* CONFIG_PREEMPT */
1680
1681/*
1682 * double_lock_balance - lock the busiest runqueue, this_rq is locked already.
1683 */
1684static int double_lock_balance(struct rq *this_rq, struct rq *busiest)
1685{
1686 if (unlikely(!irqs_disabled())) {
1687 /* printk() doesn't work good under rq->lock */
Thomas Gleixner05fa7852009-11-17 14:28:38 +01001688 raw_spin_unlock(&this_rq->lock);
Gregory Haskins8f45e2b2008-12-29 09:39:51 -05001689 BUG_ON(1);
1690 }
1691
1692 return _double_lock_balance(this_rq, busiest);
1693}
1694
Alexey Dobriyan70574a92008-11-28 22:08:00 +03001695static inline void double_unlock_balance(struct rq *this_rq, struct rq *busiest)
1696 __releases(busiest->lock)
1697{
Thomas Gleixner05fa7852009-11-17 14:28:38 +01001698 raw_spin_unlock(&busiest->lock);
Alexey Dobriyan70574a92008-11-28 22:08:00 +03001699 lock_set_subclass(&this_rq->lock.dep_map, 0, _RET_IP_);
1700}
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01001701
1702/*
1703 * double_rq_lock - safely lock two runqueues
1704 *
1705 * Note this does not disable interrupts like task_rq_lock,
1706 * you need to do so manually before calling.
1707 */
1708static void double_rq_lock(struct rq *rq1, struct rq *rq2)
1709 __acquires(rq1->lock)
1710 __acquires(rq2->lock)
1711{
1712 BUG_ON(!irqs_disabled());
1713 if (rq1 == rq2) {
1714 raw_spin_lock(&rq1->lock);
1715 __acquire(rq2->lock); /* Fake it out ;) */
1716 } else {
1717 if (rq1 < rq2) {
1718 raw_spin_lock(&rq1->lock);
1719 raw_spin_lock_nested(&rq2->lock, SINGLE_DEPTH_NESTING);
1720 } else {
1721 raw_spin_lock(&rq2->lock);
1722 raw_spin_lock_nested(&rq1->lock, SINGLE_DEPTH_NESTING);
1723 }
1724 }
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01001725}
1726
1727/*
1728 * double_rq_unlock - safely unlock two runqueues
1729 *
1730 * Note this does not restore interrupts like task_rq_unlock,
1731 * you need to do so manually after calling.
1732 */
1733static void double_rq_unlock(struct rq *rq1, struct rq *rq2)
1734 __releases(rq1->lock)
1735 __releases(rq2->lock)
1736{
1737 raw_spin_unlock(&rq1->lock);
1738 if (rq1 != rq2)
1739 raw_spin_unlock(&rq2->lock);
1740 else
1741 __release(rq2->lock);
1742}
1743
Mike Galbraithd95f4122011-02-01 09:50:51 -05001744#else /* CONFIG_SMP */
1745
1746/*
1747 * double_rq_lock - safely lock two runqueues
1748 *
1749 * Note this does not disable interrupts like task_rq_lock,
1750 * you need to do so manually before calling.
1751 */
1752static void double_rq_lock(struct rq *rq1, struct rq *rq2)
1753 __acquires(rq1->lock)
1754 __acquires(rq2->lock)
1755{
1756 BUG_ON(!irqs_disabled());
1757 BUG_ON(rq1 != rq2);
1758 raw_spin_lock(&rq1->lock);
1759 __acquire(rq2->lock); /* Fake it out ;) */
1760}
1761
1762/*
1763 * double_rq_unlock - safely unlock two runqueues
1764 *
1765 * Note this does not restore interrupts like task_rq_unlock,
1766 * you need to do so manually after calling.
1767 */
1768static void double_rq_unlock(struct rq *rq1, struct rq *rq2)
1769 __releases(rq1->lock)
1770 __releases(rq2->lock)
1771{
1772 BUG_ON(rq1 != rq2);
1773 raw_spin_unlock(&rq1->lock);
1774 __release(rq2->lock);
1775}
1776
Peter Zijlstra18d95a22008-04-19 19:45:00 +02001777#endif
Peter Zijlstra18d95a22008-04-19 19:45:00 +02001778
Peter Zijlstra74f51872010-04-22 21:50:19 +02001779static void calc_load_account_idle(struct rq *this_rq);
Christian Ehrhardt0bcdcf22009-11-30 12:16:46 +01001780static void update_sysctl(void);
Christian Ehrhardtacb4a842009-11-30 12:16:48 +01001781static int get_update_sysctl_factor(void);
Venkatesh Pallipadifdf3e952010-05-17 18:14:43 -07001782static void update_cpu_load(struct rq *this_rq);
Thomas Gleixnerdce48a82009-04-11 10:43:41 +02001783
Peter Zijlstracd29fe62009-11-27 17:32:46 +01001784static inline void __set_task_cpu(struct task_struct *p, unsigned int cpu)
1785{
1786 set_task_rq(p, cpu);
1787#ifdef CONFIG_SMP
1788 /*
1789 * After ->cpu is set up to a new value, task_rq_lock(p, ...) can be
1790 * successfuly executed on another CPU. We must ensure that updates of
1791 * per-task data have been completed by this moment.
1792 */
1793 smp_wmb();
1794 task_thread_info(p)->cpu = cpu;
1795#endif
1796}
Peter Zijlstra18d95a22008-04-19 19:45:00 +02001797
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01001798static const struct sched_class rt_sched_class;
Ingo Molnardd41f592007-07-09 18:51:59 +02001799
Peter Zijlstra34f971f2010-09-22 13:53:15 +02001800#define sched_class_highest (&stop_sched_class)
Gregory Haskins1f11eb62008-06-04 15:04:05 -04001801#define for_each_class(class) \
1802 for (class = sched_class_highest; class; class = class->next)
Ingo Molnardd41f592007-07-09 18:51:59 +02001803
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01001804#include "sched_stats.h"
1805
Peter Zijlstrac09595f2008-06-27 13:41:14 +02001806static void inc_nr_running(struct rq *rq)
Ingo Molnar6363ca52008-05-29 11:28:57 +02001807{
1808 rq->nr_running++;
Ingo Molnar6363ca52008-05-29 11:28:57 +02001809}
1810
Peter Zijlstrac09595f2008-06-27 13:41:14 +02001811static void dec_nr_running(struct rq *rq)
Ingo Molnar9c217242007-08-02 17:41:40 +02001812{
1813 rq->nr_running--;
Ingo Molnar9c217242007-08-02 17:41:40 +02001814}
1815
Ingo Molnar45bf76d2007-07-09 18:51:59 +02001816static void set_load_weight(struct task_struct *p)
1817{
Nikhil Raof05998d2011-05-18 10:09:38 -07001818 int prio = p->static_prio - MAX_RT_PRIO;
1819 struct load_weight *load = &p->se.load;
1820
Ingo Molnardd41f592007-07-09 18:51:59 +02001821 /*
1822 * SCHED_IDLE tasks get minimal weight:
1823 */
1824 if (p->policy == SCHED_IDLE) {
Nikhil Raoc8b28112011-05-18 14:37:48 -07001825 load->weight = scale_load(WEIGHT_IDLEPRIO);
Nikhil Raof05998d2011-05-18 10:09:38 -07001826 load->inv_weight = WMULT_IDLEPRIO;
Ingo Molnardd41f592007-07-09 18:51:59 +02001827 return;
1828 }
1829
Nikhil Raoc8b28112011-05-18 14:37:48 -07001830 load->weight = scale_load(prio_to_weight[prio]);
Nikhil Raof05998d2011-05-18 10:09:38 -07001831 load->inv_weight = prio_to_wmult[prio];
Ingo Molnar45bf76d2007-07-09 18:51:59 +02001832}
1833
Peter Zijlstra371fd7e2010-03-24 16:38:48 +01001834static void enqueue_task(struct rq *rq, struct task_struct *p, int flags)
Gregory Haskins2087a1a2008-06-27 14:30:00 -06001835{
Mike Galbraitha64692a2010-03-11 17:16:20 +01001836 update_rq_clock(rq);
Ingo Molnar71f8bd42007-07-09 18:51:59 +02001837 sched_info_queued(p);
Peter Zijlstra371fd7e2010-03-24 16:38:48 +01001838 p->sched_class->enqueue_task(rq, p, flags);
Ingo Molnardd41f592007-07-09 18:51:59 +02001839}
1840
Peter Zijlstra371fd7e2010-03-24 16:38:48 +01001841static void dequeue_task(struct rq *rq, struct task_struct *p, int flags)
Ingo Molnardd41f592007-07-09 18:51:59 +02001842{
Mike Galbraitha64692a2010-03-11 17:16:20 +01001843 update_rq_clock(rq);
Ankita Garg46ac22b2008-07-01 14:30:06 +05301844 sched_info_dequeued(p);
Peter Zijlstra371fd7e2010-03-24 16:38:48 +01001845 p->sched_class->dequeue_task(rq, p, flags);
Ingo Molnar71f8bd42007-07-09 18:51:59 +02001846}
1847
1848/*
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01001849 * activate_task - move a task to the runqueue.
1850 */
Peter Zijlstra371fd7e2010-03-24 16:38:48 +01001851static void activate_task(struct rq *rq, struct task_struct *p, int flags)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01001852{
1853 if (task_contributes_to_load(p))
1854 rq->nr_uninterruptible--;
1855
Peter Zijlstra371fd7e2010-03-24 16:38:48 +01001856 enqueue_task(rq, p, flags);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01001857}
1858
1859/*
1860 * deactivate_task - remove a task from the runqueue.
1861 */
Peter Zijlstra371fd7e2010-03-24 16:38:48 +01001862static void deactivate_task(struct rq *rq, struct task_struct *p, int flags)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01001863{
1864 if (task_contributes_to_load(p))
1865 rq->nr_uninterruptible++;
1866
Peter Zijlstra371fd7e2010-03-24 16:38:48 +01001867 dequeue_task(rq, p, flags);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01001868}
1869
Venkatesh Pallipadib52bfee2010-10-04 17:03:19 -07001870#ifdef CONFIG_IRQ_TIME_ACCOUNTING
1871
Venkatesh Pallipadi305e6832010-10-04 17:03:21 -07001872/*
1873 * There are no locks covering percpu hardirq/softirq time.
1874 * They are only modified in account_system_vtime, on corresponding CPU
1875 * with interrupts disabled. So, writes are safe.
1876 * They are read and saved off onto struct rq in update_rq_clock().
1877 * This may result in other CPU reading this CPU's irq time and can
1878 * race with irq/account_system_vtime on this CPU. We would either get old
Peter Zijlstra8e92c202010-12-09 14:15:34 +01001879 * or new value with a side effect of accounting a slice of irq time to wrong
1880 * task when irq is in progress while we read rq->clock. That is a worthy
1881 * compromise in place of having locks on each irq in account_system_time.
Venkatesh Pallipadi305e6832010-10-04 17:03:21 -07001882 */
Venkatesh Pallipadib52bfee2010-10-04 17:03:19 -07001883static DEFINE_PER_CPU(u64, cpu_hardirq_time);
1884static DEFINE_PER_CPU(u64, cpu_softirq_time);
1885
1886static DEFINE_PER_CPU(u64, irq_start_time);
1887static int sched_clock_irqtime;
1888
1889void enable_sched_clock_irqtime(void)
1890{
1891 sched_clock_irqtime = 1;
1892}
1893
1894void disable_sched_clock_irqtime(void)
1895{
1896 sched_clock_irqtime = 0;
1897}
1898
Peter Zijlstra8e92c202010-12-09 14:15:34 +01001899#ifndef CONFIG_64BIT
1900static DEFINE_PER_CPU(seqcount_t, irq_time_seq);
1901
1902static inline void irq_time_write_begin(void)
1903{
1904 __this_cpu_inc(irq_time_seq.sequence);
1905 smp_wmb();
1906}
1907
1908static inline void irq_time_write_end(void)
1909{
1910 smp_wmb();
1911 __this_cpu_inc(irq_time_seq.sequence);
1912}
1913
1914static inline u64 irq_time_read(int cpu)
1915{
1916 u64 irq_time;
1917 unsigned seq;
1918
1919 do {
1920 seq = read_seqcount_begin(&per_cpu(irq_time_seq, cpu));
1921 irq_time = per_cpu(cpu_softirq_time, cpu) +
1922 per_cpu(cpu_hardirq_time, cpu);
1923 } while (read_seqcount_retry(&per_cpu(irq_time_seq, cpu), seq));
1924
1925 return irq_time;
1926}
1927#else /* CONFIG_64BIT */
1928static inline void irq_time_write_begin(void)
1929{
1930}
1931
1932static inline void irq_time_write_end(void)
1933{
1934}
1935
1936static inline u64 irq_time_read(int cpu)
Venkatesh Pallipadi305e6832010-10-04 17:03:21 -07001937{
Venkatesh Pallipadi305e6832010-10-04 17:03:21 -07001938 return per_cpu(cpu_softirq_time, cpu) + per_cpu(cpu_hardirq_time, cpu);
1939}
Peter Zijlstra8e92c202010-12-09 14:15:34 +01001940#endif /* CONFIG_64BIT */
Venkatesh Pallipadi305e6832010-10-04 17:03:21 -07001941
Peter Zijlstrafe44d622010-12-09 14:15:34 +01001942/*
1943 * Called before incrementing preempt_count on {soft,}irq_enter
1944 * and before decrementing preempt_count on {soft,}irq_exit.
1945 */
Venkatesh Pallipadib52bfee2010-10-04 17:03:19 -07001946void account_system_vtime(struct task_struct *curr)
1947{
1948 unsigned long flags;
Peter Zijlstrafe44d622010-12-09 14:15:34 +01001949 s64 delta;
Venkatesh Pallipadib52bfee2010-10-04 17:03:19 -07001950 int cpu;
Venkatesh Pallipadib52bfee2010-10-04 17:03:19 -07001951
1952 if (!sched_clock_irqtime)
1953 return;
1954
1955 local_irq_save(flags);
1956
Venkatesh Pallipadib52bfee2010-10-04 17:03:19 -07001957 cpu = smp_processor_id();
Peter Zijlstrafe44d622010-12-09 14:15:34 +01001958 delta = sched_clock_cpu(cpu) - __this_cpu_read(irq_start_time);
1959 __this_cpu_add(irq_start_time, delta);
1960
Peter Zijlstra8e92c202010-12-09 14:15:34 +01001961 irq_time_write_begin();
Venkatesh Pallipadib52bfee2010-10-04 17:03:19 -07001962 /*
1963 * We do not account for softirq time from ksoftirqd here.
1964 * We want to continue accounting softirq time to ksoftirqd thread
1965 * in that case, so as not to confuse scheduler with a special task
1966 * that do not consume any time, but still wants to run.
1967 */
1968 if (hardirq_count())
Peter Zijlstrafe44d622010-12-09 14:15:34 +01001969 __this_cpu_add(cpu_hardirq_time, delta);
Venkatesh Pallipadi4dd53d82010-12-21 17:09:00 -08001970 else if (in_serving_softirq() && curr != this_cpu_ksoftirqd())
Peter Zijlstrafe44d622010-12-09 14:15:34 +01001971 __this_cpu_add(cpu_softirq_time, delta);
Venkatesh Pallipadib52bfee2010-10-04 17:03:19 -07001972
Peter Zijlstra8e92c202010-12-09 14:15:34 +01001973 irq_time_write_end();
Venkatesh Pallipadib52bfee2010-10-04 17:03:19 -07001974 local_irq_restore(flags);
1975}
Ingo Molnarb7dadc32010-10-18 20:00:37 +02001976EXPORT_SYMBOL_GPL(account_system_vtime);
Venkatesh Pallipadib52bfee2010-10-04 17:03:19 -07001977
Glauber Costae6e66852011-07-11 15:28:17 -04001978#endif /* CONFIG_IRQ_TIME_ACCOUNTING */
1979
1980#ifdef CONFIG_PARAVIRT
1981static inline u64 steal_ticks(u64 steal)
1982{
1983 if (unlikely(steal > NSEC_PER_SEC))
1984 return div_u64(steal, TICK_NSEC);
1985
1986 return __iter_div_u64_rem(steal, TICK_NSEC, &steal);
1987}
1988#endif
1989
Peter Zijlstrafe44d622010-12-09 14:15:34 +01001990static void update_rq_clock_task(struct rq *rq, s64 delta)
Venkatesh Pallipadiaa483802010-10-04 17:03:22 -07001991{
Glauber Costa095c0aa2011-07-11 15:28:18 -04001992/*
1993 * In theory, the compile should just see 0 here, and optimize out the call
1994 * to sched_rt_avg_update. But I don't trust it...
1995 */
1996#if defined(CONFIG_IRQ_TIME_ACCOUNTING) || defined(CONFIG_PARAVIRT_TIME_ACCOUNTING)
1997 s64 steal = 0, irq_delta = 0;
1998#endif
1999#ifdef CONFIG_IRQ_TIME_ACCOUNTING
Peter Zijlstra8e92c202010-12-09 14:15:34 +01002000 irq_delta = irq_time_read(cpu_of(rq)) - rq->prev_irq_time;
Peter Zijlstrafe44d622010-12-09 14:15:34 +01002001
2002 /*
2003 * Since irq_time is only updated on {soft,}irq_exit, we might run into
2004 * this case when a previous update_rq_clock() happened inside a
2005 * {soft,}irq region.
2006 *
2007 * When this happens, we stop ->clock_task and only update the
2008 * prev_irq_time stamp to account for the part that fit, so that a next
2009 * update will consume the rest. This ensures ->clock_task is
2010 * monotonic.
2011 *
2012 * It does however cause some slight miss-attribution of {soft,}irq
2013 * time, a more accurate solution would be to update the irq_time using
2014 * the current rq->clock timestamp, except that would require using
2015 * atomic ops.
2016 */
2017 if (irq_delta > delta)
2018 irq_delta = delta;
2019
2020 rq->prev_irq_time += irq_delta;
2021 delta -= irq_delta;
Glauber Costa095c0aa2011-07-11 15:28:18 -04002022#endif
2023#ifdef CONFIG_PARAVIRT_TIME_ACCOUNTING
2024 if (static_branch((&paravirt_steal_rq_enabled))) {
2025 u64 st;
2026
2027 steal = paravirt_steal_clock(cpu_of(rq));
2028 steal -= rq->prev_steal_time_rq;
2029
2030 if (unlikely(steal > delta))
2031 steal = delta;
2032
2033 st = steal_ticks(steal);
2034 steal = st * TICK_NSEC;
2035
2036 rq->prev_steal_time_rq += steal;
2037
2038 delta -= steal;
2039 }
2040#endif
2041
Peter Zijlstrafe44d622010-12-09 14:15:34 +01002042 rq->clock_task += delta;
2043
Glauber Costa095c0aa2011-07-11 15:28:18 -04002044#if defined(CONFIG_IRQ_TIME_ACCOUNTING) || defined(CONFIG_PARAVIRT_TIME_ACCOUNTING)
2045 if ((irq_delta + steal) && sched_feat(NONTASK_POWER))
2046 sched_rt_avg_update(rq, irq_delta + steal);
2047#endif
Venkatesh Pallipadiaa483802010-10-04 17:03:22 -07002048}
2049
Glauber Costa095c0aa2011-07-11 15:28:18 -04002050#ifdef CONFIG_IRQ_TIME_ACCOUNTING
Venkatesh Pallipadiabb74ce2010-12-21 17:09:03 -08002051static int irqtime_account_hi_update(void)
2052{
2053 struct cpu_usage_stat *cpustat = &kstat_this_cpu.cpustat;
2054 unsigned long flags;
2055 u64 latest_ns;
2056 int ret = 0;
2057
2058 local_irq_save(flags);
2059 latest_ns = this_cpu_read(cpu_hardirq_time);
2060 if (cputime64_gt(nsecs_to_cputime64(latest_ns), cpustat->irq))
2061 ret = 1;
2062 local_irq_restore(flags);
2063 return ret;
2064}
2065
2066static int irqtime_account_si_update(void)
2067{
2068 struct cpu_usage_stat *cpustat = &kstat_this_cpu.cpustat;
2069 unsigned long flags;
2070 u64 latest_ns;
2071 int ret = 0;
2072
2073 local_irq_save(flags);
2074 latest_ns = this_cpu_read(cpu_softirq_time);
2075 if (cputime64_gt(nsecs_to_cputime64(latest_ns), cpustat->softirq))
2076 ret = 1;
2077 local_irq_restore(flags);
2078 return ret;
2079}
2080
Peter Zijlstrafe44d622010-12-09 14:15:34 +01002081#else /* CONFIG_IRQ_TIME_ACCOUNTING */
Venkatesh Pallipadi305e6832010-10-04 17:03:21 -07002082
Venkatesh Pallipadiabb74ce2010-12-21 17:09:03 -08002083#define sched_clock_irqtime (0)
2084
Glauber Costa095c0aa2011-07-11 15:28:18 -04002085#endif
Venkatesh Pallipadib52bfee2010-10-04 17:03:19 -07002086
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01002087#include "sched_idletask.c"
2088#include "sched_fair.c"
2089#include "sched_rt.c"
Mike Galbraith5091faa2010-11-30 14:18:03 +01002090#include "sched_autogroup.c"
Peter Zijlstra34f971f2010-09-22 13:53:15 +02002091#include "sched_stoptask.c"
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01002092#ifdef CONFIG_SCHED_DEBUG
2093# include "sched_debug.c"
2094#endif
2095
Peter Zijlstra34f971f2010-09-22 13:53:15 +02002096void sched_set_stop_task(int cpu, struct task_struct *stop)
2097{
2098 struct sched_param param = { .sched_priority = MAX_RT_PRIO - 1 };
2099 struct task_struct *old_stop = cpu_rq(cpu)->stop;
2100
2101 if (stop) {
2102 /*
2103 * Make it appear like a SCHED_FIFO task, its something
2104 * userspace knows about and won't get confused about.
2105 *
2106 * Also, it will make PI more or less work without too
2107 * much confusion -- but then, stop work should not
2108 * rely on PI working anyway.
2109 */
2110 sched_setscheduler_nocheck(stop, SCHED_FIFO, &param);
2111
2112 stop->sched_class = &stop_sched_class;
2113 }
2114
2115 cpu_rq(cpu)->stop = stop;
2116
2117 if (old_stop) {
2118 /*
2119 * Reset it back to a normal scheduling class so that
2120 * it can die in pieces.
2121 */
2122 old_stop->sched_class = &rt_sched_class;
2123 }
2124}
2125
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01002126/*
Ingo Molnardd41f592007-07-09 18:51:59 +02002127 * __normal_prio - return the priority that is based on the static prio
Ingo Molnar71f8bd42007-07-09 18:51:59 +02002128 */
Ingo Molnar14531182007-07-09 18:51:59 +02002129static inline int __normal_prio(struct task_struct *p)
2130{
Ingo Molnardd41f592007-07-09 18:51:59 +02002131 return p->static_prio;
Ingo Molnar14531182007-07-09 18:51:59 +02002132}
2133
2134/*
Ingo Molnarb29739f2006-06-27 02:54:51 -07002135 * Calculate the expected normal priority: i.e. priority
2136 * without taking RT-inheritance into account. Might be
2137 * boosted by interactivity modifiers. Changes upon fork,
2138 * setprio syscalls, and whenever the interactivity
2139 * estimator recalculates.
2140 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07002141static inline int normal_prio(struct task_struct *p)
Ingo Molnarb29739f2006-06-27 02:54:51 -07002142{
2143 int prio;
2144
Ingo Molnare05606d2007-07-09 18:51:59 +02002145 if (task_has_rt_policy(p))
Ingo Molnarb29739f2006-06-27 02:54:51 -07002146 prio = MAX_RT_PRIO-1 - p->rt_priority;
2147 else
2148 prio = __normal_prio(p);
2149 return prio;
2150}
2151
2152/*
2153 * Calculate the current priority, i.e. the priority
2154 * taken into account by the scheduler. This value might
2155 * be boosted by RT tasks, or might be boosted by
2156 * interactivity modifiers. Will be RT if the task got
2157 * RT-boosted. If not then it returns p->normal_prio.
2158 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07002159static int effective_prio(struct task_struct *p)
Ingo Molnarb29739f2006-06-27 02:54:51 -07002160{
2161 p->normal_prio = normal_prio(p);
2162 /*
2163 * If we are RT tasks or we were boosted to RT priority,
2164 * keep the priority unchanged. Otherwise, update priority
2165 * to the normal priority:
2166 */
2167 if (!rt_prio(p->prio))
2168 return p->normal_prio;
2169 return p->prio;
2170}
2171
Linus Torvalds1da177e2005-04-16 15:20:36 -07002172/**
2173 * task_curr - is this task currently executing on a CPU?
2174 * @p: the task in question.
2175 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07002176inline int task_curr(const struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002177{
2178 return cpu_curr(task_cpu(p)) == p;
2179}
2180
Steven Rostedtcb469842008-01-25 21:08:22 +01002181static inline void check_class_changed(struct rq *rq, struct task_struct *p,
2182 const struct sched_class *prev_class,
Peter Zijlstrada7a7352011-01-17 17:03:27 +01002183 int oldprio)
Steven Rostedtcb469842008-01-25 21:08:22 +01002184{
2185 if (prev_class != p->sched_class) {
2186 if (prev_class->switched_from)
Peter Zijlstrada7a7352011-01-17 17:03:27 +01002187 prev_class->switched_from(rq, p);
2188 p->sched_class->switched_to(rq, p);
2189 } else if (oldprio != p->prio)
2190 p->sched_class->prio_changed(rq, p, oldprio);
Steven Rostedtcb469842008-01-25 21:08:22 +01002191}
2192
Peter Zijlstra1e5a7402010-10-31 12:37:04 +01002193static void check_preempt_curr(struct rq *rq, struct task_struct *p, int flags)
2194{
2195 const struct sched_class *class;
2196
2197 if (p->sched_class == rq->curr->sched_class) {
2198 rq->curr->sched_class->check_preempt_curr(rq, p, flags);
2199 } else {
2200 for_each_class(class) {
2201 if (class == rq->curr->sched_class)
2202 break;
2203 if (class == p->sched_class) {
2204 resched_task(rq->curr);
2205 break;
2206 }
2207 }
2208 }
2209
2210 /*
2211 * A queue event has occurred, and we're going to schedule. In
2212 * this case, we can save a useless back to back clock update.
2213 */
Peter Zijlstrafd2f4412011-04-05 17:23:44 +02002214 if (rq->curr->on_rq && test_tsk_need_resched(rq->curr))
Peter Zijlstra1e5a7402010-10-31 12:37:04 +01002215 rq->skip_clock_update = 1;
2216}
2217
Linus Torvalds1da177e2005-04-16 15:20:36 -07002218#ifdef CONFIG_SMP
Ingo Molnarcc367732007-10-15 17:00:18 +02002219/*
2220 * Is this task likely cache-hot:
2221 */
Gregory Haskinse7693a32008-01-25 21:08:09 +01002222static int
Ingo Molnarcc367732007-10-15 17:00:18 +02002223task_hot(struct task_struct *p, u64 now, struct sched_domain *sd)
2224{
2225 s64 delta;
2226
Peter Zijlstrae6c8fba2009-12-16 18:04:33 +01002227 if (p->sched_class != &fair_sched_class)
2228 return 0;
2229
Nikhil Raoef8002f2010-10-13 12:09:35 -07002230 if (unlikely(p->policy == SCHED_IDLE))
2231 return 0;
2232
Ingo Molnarf540a602008-03-15 17:10:34 +01002233 /*
2234 * Buddy candidates are cache hot:
2235 */
Mike Galbraithf685cea2009-10-23 23:09:22 +02002236 if (sched_feat(CACHE_HOT_BUDDY) && this_rq()->nr_running &&
Peter Zijlstra47932412008-11-04 21:25:09 +01002237 (&p->se == cfs_rq_of(&p->se)->next ||
2238 &p->se == cfs_rq_of(&p->se)->last))
Ingo Molnarf540a602008-03-15 17:10:34 +01002239 return 1;
2240
Ingo Molnar6bc16652007-10-15 17:00:18 +02002241 if (sysctl_sched_migration_cost == -1)
2242 return 1;
2243 if (sysctl_sched_migration_cost == 0)
2244 return 0;
2245
Ingo Molnarcc367732007-10-15 17:00:18 +02002246 delta = now - p->se.exec_start;
2247
2248 return delta < (s64)sysctl_sched_migration_cost;
2249}
2250
Ingo Molnardd41f592007-07-09 18:51:59 +02002251void set_task_cpu(struct task_struct *p, unsigned int new_cpu)
Ingo Molnarc65cc872007-07-09 18:51:58 +02002252{
Peter Zijlstrae2912002009-12-16 18:04:36 +01002253#ifdef CONFIG_SCHED_DEBUG
2254 /*
2255 * We should never call set_task_cpu() on a blocked task,
2256 * ttwu() will sort out the placement.
2257 */
Peter Zijlstra077614e2009-12-17 13:16:31 +01002258 WARN_ON_ONCE(p->state != TASK_RUNNING && p->state != TASK_WAKING &&
2259 !(task_thread_info(p)->preempt_count & PREEMPT_ACTIVE));
Peter Zijlstra0122ec52011-04-05 17:23:51 +02002260
2261#ifdef CONFIG_LOCKDEP
Peter Zijlstra6c6c54e2011-06-03 17:37:07 +02002262 /*
2263 * The caller should hold either p->pi_lock or rq->lock, when changing
2264 * a task's CPU. ->pi_lock for waking tasks, rq->lock for runnable tasks.
2265 *
2266 * sched_move_task() holds both and thus holding either pins the cgroup,
2267 * see set_task_rq().
2268 *
2269 * Furthermore, all task_rq users should acquire both locks, see
2270 * task_rq_lock().
2271 */
Peter Zijlstra0122ec52011-04-05 17:23:51 +02002272 WARN_ON_ONCE(debug_locks && !(lockdep_is_held(&p->pi_lock) ||
2273 lockdep_is_held(&task_rq(p)->lock)));
2274#endif
Peter Zijlstrae2912002009-12-16 18:04:36 +01002275#endif
2276
Mathieu Desnoyersde1d7282009-05-05 16:49:59 +08002277 trace_sched_migrate_task(p, new_cpu);
Peter Zijlstracbc34ed2008-12-10 08:08:22 +01002278
Peter Zijlstra0c697742009-12-22 15:43:19 +01002279 if (task_cpu(p) != new_cpu) {
2280 p->se.nr_migrations++;
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02002281 perf_sw_event(PERF_COUNT_SW_CPU_MIGRATIONS, 1, NULL, 0);
Peter Zijlstra0c697742009-12-22 15:43:19 +01002282 }
Ingo Molnardd41f592007-07-09 18:51:59 +02002283
2284 __set_task_cpu(p, new_cpu);
Ingo Molnarc65cc872007-07-09 18:51:58 +02002285}
2286
Tejun Heo969c7922010-05-06 18:49:21 +02002287struct migration_arg {
Ingo Molnar36c8b582006-07-03 00:25:41 -07002288 struct task_struct *task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002289 int dest_cpu;
Ingo Molnar70b97a72006-07-03 00:25:42 -07002290};
Linus Torvalds1da177e2005-04-16 15:20:36 -07002291
Tejun Heo969c7922010-05-06 18:49:21 +02002292static int migration_cpu_stop(void *data);
2293
Linus Torvalds1da177e2005-04-16 15:20:36 -07002294/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002295 * wait_task_inactive - wait for a thread to unschedule.
2296 *
Roland McGrath85ba2d82008-07-25 19:45:58 -07002297 * If @match_state is nonzero, it's the @p->state value just checked and
2298 * not expected to change. If it changes, i.e. @p might have woken up,
2299 * then return zero. When we succeed in waiting for @p to be off its CPU,
2300 * we return a positive number (its total switch count). If a second call
2301 * a short while later returns the same number, the caller can be sure that
2302 * @p has remained unscheduled the whole time.
2303 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002304 * The caller must ensure that the task *will* unschedule sometime soon,
2305 * else this function might spin for a *long* time. This function can't
2306 * be called with interrupts off, or it may introduce deadlock with
2307 * smp_call_function() if an IPI is sent by the same process we are
2308 * waiting to become inactive.
2309 */
Roland McGrath85ba2d82008-07-25 19:45:58 -07002310unsigned long wait_task_inactive(struct task_struct *p, long match_state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002311{
2312 unsigned long flags;
Ingo Molnardd41f592007-07-09 18:51:59 +02002313 int running, on_rq;
Roland McGrath85ba2d82008-07-25 19:45:58 -07002314 unsigned long ncsw;
Ingo Molnar70b97a72006-07-03 00:25:42 -07002315 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002316
Andi Kleen3a5c3592007-10-15 17:00:14 +02002317 for (;;) {
2318 /*
2319 * We do the initial early heuristics without holding
2320 * any task-queue locks at all. We'll only try to get
2321 * the runqueue lock when things look like they will
2322 * work out!
2323 */
2324 rq = task_rq(p);
Linus Torvaldsfa490cf2007-06-18 09:34:40 -07002325
Andi Kleen3a5c3592007-10-15 17:00:14 +02002326 /*
2327 * If the task is actively running on another CPU
2328 * still, just relax and busy-wait without holding
2329 * any locks.
2330 *
2331 * NOTE! Since we don't hold any locks, it's not
2332 * even sure that "rq" stays as the right runqueue!
2333 * But we don't care, since "task_running()" will
2334 * return false if the runqueue has changed and p
2335 * is actually now running somewhere else!
2336 */
Roland McGrath85ba2d82008-07-25 19:45:58 -07002337 while (task_running(rq, p)) {
2338 if (match_state && unlikely(p->state != match_state))
2339 return 0;
Andi Kleen3a5c3592007-10-15 17:00:14 +02002340 cpu_relax();
Roland McGrath85ba2d82008-07-25 19:45:58 -07002341 }
Linus Torvaldsfa490cf2007-06-18 09:34:40 -07002342
Andi Kleen3a5c3592007-10-15 17:00:14 +02002343 /*
2344 * Ok, time to look more closely! We need the rq
2345 * lock now, to be *sure*. If we're wrong, we'll
2346 * just go back and repeat.
2347 */
2348 rq = task_rq_lock(p, &flags);
Peter Zijlstra27a9da62010-05-04 20:36:56 +02002349 trace_sched_wait_task(p);
Andi Kleen3a5c3592007-10-15 17:00:14 +02002350 running = task_running(rq, p);
Peter Zijlstrafd2f4412011-04-05 17:23:44 +02002351 on_rq = p->on_rq;
Roland McGrath85ba2d82008-07-25 19:45:58 -07002352 ncsw = 0;
Oleg Nesterovf31e11d2008-08-20 16:54:44 -07002353 if (!match_state || p->state == match_state)
Oleg Nesterov93dcf552008-08-20 16:54:44 -07002354 ncsw = p->nvcsw | LONG_MIN; /* sets MSB */
Peter Zijlstra0122ec52011-04-05 17:23:51 +02002355 task_rq_unlock(rq, p, &flags);
Linus Torvaldsfa490cf2007-06-18 09:34:40 -07002356
Andi Kleen3a5c3592007-10-15 17:00:14 +02002357 /*
Roland McGrath85ba2d82008-07-25 19:45:58 -07002358 * If it changed from the expected state, bail out now.
2359 */
2360 if (unlikely(!ncsw))
2361 break;
2362
2363 /*
Andi Kleen3a5c3592007-10-15 17:00:14 +02002364 * Was it really running after all now that we
2365 * checked with the proper locks actually held?
2366 *
2367 * Oops. Go back and try again..
2368 */
2369 if (unlikely(running)) {
2370 cpu_relax();
2371 continue;
2372 }
2373
2374 /*
2375 * It's not enough that it's not actively running,
2376 * it must be off the runqueue _entirely_, and not
2377 * preempted!
2378 *
Luis Henriques80dd99b2009-03-16 19:58:09 +00002379 * So if it was still runnable (but just not actively
Andi Kleen3a5c3592007-10-15 17:00:14 +02002380 * running right now), it's preempted, and we should
2381 * yield - it could be a while.
2382 */
2383 if (unlikely(on_rq)) {
Thomas Gleixner8eb90c32011-02-23 23:52:21 +00002384 ktime_t to = ktime_set(0, NSEC_PER_SEC/HZ);
2385
2386 set_current_state(TASK_UNINTERRUPTIBLE);
2387 schedule_hrtimeout(&to, HRTIMER_MODE_REL);
Andi Kleen3a5c3592007-10-15 17:00:14 +02002388 continue;
2389 }
2390
2391 /*
2392 * Ahh, all good. It wasn't running, and it wasn't
2393 * runnable, which means that it will never become
2394 * running in the future either. We're all done!
2395 */
2396 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002397 }
Roland McGrath85ba2d82008-07-25 19:45:58 -07002398
2399 return ncsw;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002400}
2401
2402/***
2403 * kick_process - kick a running thread to enter/exit the kernel
2404 * @p: the to-be-kicked thread
2405 *
2406 * Cause a process which is running on another CPU to enter
2407 * kernel-mode, without any delay. (to get signals handled.)
2408 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002409 * NOTE: this function doesn't have to take the runqueue lock,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002410 * because all it wants to ensure is that the remote task enters
2411 * the kernel. If the IPI races and the task has been migrated
2412 * to another CPU then no harm is done and the purpose has been
2413 * achieved as well.
2414 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07002415void kick_process(struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002416{
2417 int cpu;
2418
2419 preempt_disable();
2420 cpu = task_cpu(p);
2421 if ((cpu != smp_processor_id()) && task_curr(p))
2422 smp_send_reschedule(cpu);
2423 preempt_enable();
2424}
Rusty Russellb43e3522009-06-12 22:27:00 -06002425EXPORT_SYMBOL_GPL(kick_process);
Nick Piggin476d1392005-06-25 14:57:29 -07002426#endif /* CONFIG_SMP */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002427
Peter Zijlstra970b13b2009-11-25 13:31:39 +01002428#ifdef CONFIG_SMP
Oleg Nesterov30da6882010-03-15 10:10:19 +01002429/*
Peter Zijlstra013fdb82011-04-05 17:23:45 +02002430 * ->cpus_allowed is protected by both rq->lock and p->pi_lock
Oleg Nesterov30da6882010-03-15 10:10:19 +01002431 */
Peter Zijlstra5da9a0f2009-12-16 18:04:38 +01002432static int select_fallback_rq(int cpu, struct task_struct *p)
2433{
2434 int dest_cpu;
2435 const struct cpumask *nodemask = cpumask_of_node(cpu_to_node(cpu));
2436
2437 /* Look for allowed, online CPU in same node. */
2438 for_each_cpu_and(dest_cpu, nodemask, cpu_active_mask)
2439 if (cpumask_test_cpu(dest_cpu, &p->cpus_allowed))
2440 return dest_cpu;
2441
2442 /* Any allowed, online CPU? */
2443 dest_cpu = cpumask_any_and(&p->cpus_allowed, cpu_active_mask);
2444 if (dest_cpu < nr_cpu_ids)
2445 return dest_cpu;
2446
2447 /* No more Mr. Nice Guy. */
Peter Zijlstra48c5cca2010-11-13 19:32:29 +01002448 dest_cpu = cpuset_cpus_allowed_fallback(p);
2449 /*
2450 * Don't tell them about moving exiting tasks or
2451 * kernel threads (both mm NULL), since they never
2452 * leave kernel.
2453 */
2454 if (p->mm && printk_ratelimit()) {
2455 printk(KERN_INFO "process %d (%s) no longer affine to cpu%d\n",
2456 task_pid_nr(p), p->comm, cpu);
Peter Zijlstra5da9a0f2009-12-16 18:04:38 +01002457 }
2458
2459 return dest_cpu;
2460}
2461
Peter Zijlstrae2912002009-12-16 18:04:36 +01002462/*
Peter Zijlstra013fdb82011-04-05 17:23:45 +02002463 * The caller (fork, wakeup) owns p->pi_lock, ->cpus_allowed is stable.
Peter Zijlstrae2912002009-12-16 18:04:36 +01002464 */
Peter Zijlstra970b13b2009-11-25 13:31:39 +01002465static inline
Peter Zijlstra7608dec2011-04-05 17:23:46 +02002466int select_task_rq(struct task_struct *p, int sd_flags, int wake_flags)
Peter Zijlstra970b13b2009-11-25 13:31:39 +01002467{
Peter Zijlstra7608dec2011-04-05 17:23:46 +02002468 int cpu = p->sched_class->select_task_rq(p, sd_flags, wake_flags);
Peter Zijlstrae2912002009-12-16 18:04:36 +01002469
2470 /*
2471 * In order not to call set_task_cpu() on a blocking task we need
2472 * to rely on ttwu() to place the task on a valid ->cpus_allowed
2473 * cpu.
2474 *
2475 * Since this is common to all placement strategies, this lives here.
2476 *
2477 * [ this allows ->select_task() to simply return task_cpu(p) and
2478 * not worry about this generic constraint ]
2479 */
2480 if (unlikely(!cpumask_test_cpu(cpu, &p->cpus_allowed) ||
Peter Zijlstra70f11202009-12-20 17:36:27 +01002481 !cpu_online(cpu)))
Peter Zijlstra5da9a0f2009-12-16 18:04:38 +01002482 cpu = select_fallback_rq(task_cpu(p), p);
Peter Zijlstrae2912002009-12-16 18:04:36 +01002483
2484 return cpu;
Peter Zijlstra970b13b2009-11-25 13:31:39 +01002485}
Mike Galbraith09a40af2010-04-15 07:29:59 +02002486
2487static void update_avg(u64 *avg, u64 sample)
2488{
2489 s64 diff = sample - *avg;
2490 *avg += diff >> 3;
2491}
Peter Zijlstra970b13b2009-11-25 13:31:39 +01002492#endif
2493
Peter Zijlstrad7c01d22011-04-05 17:23:43 +02002494static void
Peter Zijlstrab84cb5d2011-04-05 17:23:55 +02002495ttwu_stat(struct task_struct *p, int cpu, int wake_flags)
Tejun Heo9ed38112009-12-03 15:08:03 +09002496{
Peter Zijlstrad7c01d22011-04-05 17:23:43 +02002497#ifdef CONFIG_SCHEDSTATS
Peter Zijlstrab84cb5d2011-04-05 17:23:55 +02002498 struct rq *rq = this_rq();
Tejun Heo9ed38112009-12-03 15:08:03 +09002499
Peter Zijlstrad7c01d22011-04-05 17:23:43 +02002500#ifdef CONFIG_SMP
2501 int this_cpu = smp_processor_id();
Tejun Heo9ed38112009-12-03 15:08:03 +09002502
Peter Zijlstrad7c01d22011-04-05 17:23:43 +02002503 if (cpu == this_cpu) {
2504 schedstat_inc(rq, ttwu_local);
2505 schedstat_inc(p, se.statistics.nr_wakeups_local);
2506 } else {
2507 struct sched_domain *sd;
2508
2509 schedstat_inc(p, se.statistics.nr_wakeups_remote);
Peter Zijlstra057f3fa2011-04-18 11:24:34 +02002510 rcu_read_lock();
Peter Zijlstrad7c01d22011-04-05 17:23:43 +02002511 for_each_domain(this_cpu, sd) {
2512 if (cpumask_test_cpu(cpu, sched_domain_span(sd))) {
2513 schedstat_inc(sd, ttwu_wake_remote);
2514 break;
2515 }
2516 }
Peter Zijlstra057f3fa2011-04-18 11:24:34 +02002517 rcu_read_unlock();
Peter Zijlstrad7c01d22011-04-05 17:23:43 +02002518 }
Peter Zijlstraf339b9d2011-05-31 10:49:20 +02002519
2520 if (wake_flags & WF_MIGRATED)
2521 schedstat_inc(p, se.statistics.nr_wakeups_migrate);
2522
Peter Zijlstrad7c01d22011-04-05 17:23:43 +02002523#endif /* CONFIG_SMP */
2524
2525 schedstat_inc(rq, ttwu_count);
2526 schedstat_inc(p, se.statistics.nr_wakeups);
2527
2528 if (wake_flags & WF_SYNC)
2529 schedstat_inc(p, se.statistics.nr_wakeups_sync);
2530
Peter Zijlstrad7c01d22011-04-05 17:23:43 +02002531#endif /* CONFIG_SCHEDSTATS */
Tejun Heo9ed38112009-12-03 15:08:03 +09002532}
2533
Peter Zijlstrad7c01d22011-04-05 17:23:43 +02002534static void ttwu_activate(struct rq *rq, struct task_struct *p, int en_flags)
Tejun Heo9ed38112009-12-03 15:08:03 +09002535{
Tejun Heo9ed38112009-12-03 15:08:03 +09002536 activate_task(rq, p, en_flags);
Peter Zijlstrafd2f4412011-04-05 17:23:44 +02002537 p->on_rq = 1;
Peter Zijlstrac2f71152011-04-13 13:28:56 +02002538
2539 /* if a worker is waking up, notify workqueue */
2540 if (p->flags & PF_WQ_WORKER)
2541 wq_worker_waking_up(p, cpu_of(rq));
Tejun Heo9ed38112009-12-03 15:08:03 +09002542}
2543
Peter Zijlstra23f41ee2011-04-05 17:23:56 +02002544/*
2545 * Mark the task runnable and perform wakeup-preemption.
2546 */
Peter Zijlstra89363382011-04-05 17:23:42 +02002547static void
Peter Zijlstra23f41ee2011-04-05 17:23:56 +02002548ttwu_do_wakeup(struct rq *rq, struct task_struct *p, int wake_flags)
Tejun Heo9ed38112009-12-03 15:08:03 +09002549{
Peter Zijlstra89363382011-04-05 17:23:42 +02002550 trace_sched_wakeup(p, true);
Tejun Heo9ed38112009-12-03 15:08:03 +09002551 check_preempt_curr(rq, p, wake_flags);
2552
2553 p->state = TASK_RUNNING;
2554#ifdef CONFIG_SMP
2555 if (p->sched_class->task_woken)
2556 p->sched_class->task_woken(rq, p);
2557
Steven Rostedte69c6342010-12-06 17:10:31 -05002558 if (rq->idle_stamp) {
Tejun Heo9ed38112009-12-03 15:08:03 +09002559 u64 delta = rq->clock - rq->idle_stamp;
2560 u64 max = 2*sysctl_sched_migration_cost;
2561
2562 if (delta > max)
2563 rq->avg_idle = max;
2564 else
2565 update_avg(&rq->avg_idle, delta);
2566 rq->idle_stamp = 0;
2567 }
2568#endif
2569}
2570
Peter Zijlstrac05fbaf2011-04-05 17:23:57 +02002571static void
2572ttwu_do_activate(struct rq *rq, struct task_struct *p, int wake_flags)
2573{
2574#ifdef CONFIG_SMP
2575 if (p->sched_contributes_to_load)
2576 rq->nr_uninterruptible--;
2577#endif
2578
2579 ttwu_activate(rq, p, ENQUEUE_WAKEUP | ENQUEUE_WAKING);
2580 ttwu_do_wakeup(rq, p, wake_flags);
2581}
2582
2583/*
2584 * Called in case the task @p isn't fully descheduled from its runqueue,
2585 * in this case we must do a remote wakeup. Its a 'light' wakeup though,
2586 * since all we need to do is flip p->state to TASK_RUNNING, since
2587 * the task is still ->on_rq.
2588 */
2589static int ttwu_remote(struct task_struct *p, int wake_flags)
2590{
2591 struct rq *rq;
2592 int ret = 0;
2593
2594 rq = __task_rq_lock(p);
2595 if (p->on_rq) {
2596 ttwu_do_wakeup(rq, p, wake_flags);
2597 ret = 1;
2598 }
2599 __task_rq_unlock(rq);
2600
2601 return ret;
2602}
2603
Peter Zijlstra317f3942011-04-05 17:23:58 +02002604#ifdef CONFIG_SMP
Peter Zijlstrac5d753a2011-07-19 15:07:25 -07002605static void sched_ttwu_do_pending(struct task_struct *list)
Peter Zijlstra317f3942011-04-05 17:23:58 +02002606{
2607 struct rq *rq = this_rq();
Peter Zijlstra317f3942011-04-05 17:23:58 +02002608
2609 raw_spin_lock(&rq->lock);
2610
2611 while (list) {
2612 struct task_struct *p = list;
2613 list = list->wake_entry;
2614 ttwu_do_activate(rq, p, 0);
2615 }
2616
2617 raw_spin_unlock(&rq->lock);
2618}
2619
Peter Zijlstrac5d753a2011-07-19 15:07:25 -07002620#ifdef CONFIG_HOTPLUG_CPU
2621
2622static void sched_ttwu_pending(void)
2623{
2624 struct rq *rq = this_rq();
2625 struct task_struct *list = xchg(&rq->wake_list, NULL);
2626
2627 if (!list)
2628 return;
2629
2630 sched_ttwu_do_pending(list);
2631}
2632
2633#endif /* CONFIG_HOTPLUG_CPU */
2634
Peter Zijlstra317f3942011-04-05 17:23:58 +02002635void scheduler_ipi(void)
2636{
Peter Zijlstrac5d753a2011-07-19 15:07:25 -07002637 struct rq *rq = this_rq();
2638 struct task_struct *list = xchg(&rq->wake_list, NULL);
2639
2640 if (!list)
2641 return;
2642
2643 /*
2644 * Not all reschedule IPI handlers call irq_enter/irq_exit, since
2645 * traditionally all their work was done from the interrupt return
2646 * path. Now that we actually do some work, we need to make sure
2647 * we do call them.
2648 *
2649 * Some archs already do call them, luckily irq_enter/exit nest
2650 * properly.
2651 *
2652 * Arguably we should visit all archs and update all handlers,
2653 * however a fair share of IPIs are still resched only so this would
2654 * somewhat pessimize the simple resched case.
2655 */
2656 irq_enter();
2657 sched_ttwu_do_pending(list);
2658 irq_exit();
Peter Zijlstra317f3942011-04-05 17:23:58 +02002659}
2660
2661static void ttwu_queue_remote(struct task_struct *p, int cpu)
2662{
2663 struct rq *rq = cpu_rq(cpu);
2664 struct task_struct *next = rq->wake_list;
2665
2666 for (;;) {
2667 struct task_struct *old = next;
2668
2669 p->wake_entry = next;
2670 next = cmpxchg(&rq->wake_list, old, p);
2671 if (next == old)
2672 break;
2673 }
2674
2675 if (!next)
2676 smp_send_reschedule(cpu);
2677}
Peter Zijlstrad6aa8f82011-05-26 14:21:33 +02002678
2679#ifdef __ARCH_WANT_INTERRUPTS_ON_CTXSW
2680static int ttwu_activate_remote(struct task_struct *p, int wake_flags)
2681{
2682 struct rq *rq;
2683 int ret = 0;
2684
2685 rq = __task_rq_lock(p);
2686 if (p->on_cpu) {
2687 ttwu_activate(rq, p, ENQUEUE_WAKEUP);
2688 ttwu_do_wakeup(rq, p, wake_flags);
2689 ret = 1;
2690 }
2691 __task_rq_unlock(rq);
2692
2693 return ret;
2694
2695}
2696#endif /* __ARCH_WANT_INTERRUPTS_ON_CTXSW */
2697#endif /* CONFIG_SMP */
Peter Zijlstra317f3942011-04-05 17:23:58 +02002698
Peter Zijlstrac05fbaf2011-04-05 17:23:57 +02002699static void ttwu_queue(struct task_struct *p, int cpu)
2700{
2701 struct rq *rq = cpu_rq(cpu);
2702
Daniel Hellstrom17d9f312011-05-20 04:01:10 +00002703#if defined(CONFIG_SMP)
Peter Zijlstra317f3942011-04-05 17:23:58 +02002704 if (sched_feat(TTWU_QUEUE) && cpu != smp_processor_id()) {
Peter Zijlstraf01114c2011-05-31 12:26:55 +02002705 sched_clock_cpu(cpu); /* sync clocks x-cpu */
Peter Zijlstra317f3942011-04-05 17:23:58 +02002706 ttwu_queue_remote(p, cpu);
2707 return;
2708 }
2709#endif
2710
Peter Zijlstrac05fbaf2011-04-05 17:23:57 +02002711 raw_spin_lock(&rq->lock);
2712 ttwu_do_activate(rq, p, 0);
2713 raw_spin_unlock(&rq->lock);
Tejun Heo9ed38112009-12-03 15:08:03 +09002714}
2715
2716/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07002717 * try_to_wake_up - wake up a thread
Tejun Heo9ed38112009-12-03 15:08:03 +09002718 * @p: the thread to be awakened
Linus Torvalds1da177e2005-04-16 15:20:36 -07002719 * @state: the mask of task states that can be woken
Tejun Heo9ed38112009-12-03 15:08:03 +09002720 * @wake_flags: wake modifier flags (WF_*)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002721 *
2722 * Put it on the run-queue if it's not already there. The "current"
2723 * thread is always on the run-queue (except when the actual
2724 * re-schedule is in progress), and as such you're allowed to do
2725 * the simpler "current->state = TASK_RUNNING" to mark yourself
2726 * runnable without the overhead of this.
2727 *
Tejun Heo9ed38112009-12-03 15:08:03 +09002728 * Returns %true if @p was woken up, %false if it was already running
2729 * or @state didn't match @p's state.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002730 */
Peter Zijlstrae4a52bc2011-04-05 17:23:54 +02002731static int
2732try_to_wake_up(struct task_struct *p, unsigned int state, int wake_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002733{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002734 unsigned long flags;
Peter Zijlstrac05fbaf2011-04-05 17:23:57 +02002735 int cpu, success = 0;
Peter Zijlstra2398f2c2008-06-27 13:41:35 +02002736
Linus Torvalds04e2f172008-02-23 18:05:03 -08002737 smp_wmb();
Peter Zijlstra013fdb82011-04-05 17:23:45 +02002738 raw_spin_lock_irqsave(&p->pi_lock, flags);
Peter Zijlstrae9c84312009-09-15 14:43:03 +02002739 if (!(p->state & state))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002740 goto out;
2741
Peter Zijlstrac05fbaf2011-04-05 17:23:57 +02002742 success = 1; /* we're going to change ->state */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002743 cpu = task_cpu(p);
Peter Zijlstrad7c01d22011-04-05 17:23:43 +02002744
Peter Zijlstrac05fbaf2011-04-05 17:23:57 +02002745 if (p->on_rq && ttwu_remote(p, wake_flags))
2746 goto stat;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002747
2748#ifdef CONFIG_SMP
Peter Zijlstrae9c84312009-09-15 14:43:03 +02002749 /*
Peter Zijlstrac05fbaf2011-04-05 17:23:57 +02002750 * If the owning (remote) cpu is still in the middle of schedule() with
2751 * this task as prev, wait until its done referencing the task.
Peter Zijlstrae9c84312009-09-15 14:43:03 +02002752 */
Peter Zijlstrae4a52bc2011-04-05 17:23:54 +02002753 while (p->on_cpu) {
2754#ifdef __ARCH_WANT_INTERRUPTS_ON_CTXSW
2755 /*
Peter Zijlstrad6aa8f82011-05-26 14:21:33 +02002756 * In case the architecture enables interrupts in
2757 * context_switch(), we cannot busy wait, since that
2758 * would lead to deadlocks when an interrupt hits and
2759 * tries to wake up @prev. So bail and do a complete
2760 * remote wakeup.
Peter Zijlstrae4a52bc2011-04-05 17:23:54 +02002761 */
Peter Zijlstrad6aa8f82011-05-26 14:21:33 +02002762 if (ttwu_activate_remote(p, wake_flags))
Peter Zijlstrac05fbaf2011-04-05 17:23:57 +02002763 goto stat;
Peter Zijlstrad6aa8f82011-05-26 14:21:33 +02002764#else
Peter Zijlstrae4a52bc2011-04-05 17:23:54 +02002765 cpu_relax();
Peter Zijlstrad6aa8f82011-05-26 14:21:33 +02002766#endif
Peter Zijlstracc87f762010-03-26 12:22:14 +01002767 }
Peter Zijlstrae4a52bc2011-04-05 17:23:54 +02002768 /*
2769 * Pairs with the smp_wmb() in finish_lock_switch().
2770 */
2771 smp_rmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002772
Peter Zijlstraa8e4f2e2011-04-05 17:23:49 +02002773 p->sched_contributes_to_load = !!task_contributes_to_load(p);
Peter Zijlstrae9c84312009-09-15 14:43:03 +02002774 p->state = TASK_WAKING;
Peter Zijlstraefbbd052009-12-16 18:04:40 +01002775
Peter Zijlstrae4a52bc2011-04-05 17:23:54 +02002776 if (p->sched_class->task_waking)
Peter Zijlstra74f8e4b2011-04-05 17:23:47 +02002777 p->sched_class->task_waking(p);
Peter Zijlstraab19cb22009-11-27 15:44:43 +01002778
Peter Zijlstra7608dec2011-04-05 17:23:46 +02002779 cpu = select_task_rq(p, SD_BALANCE_WAKE, wake_flags);
Peter Zijlstraf339b9d2011-05-31 10:49:20 +02002780 if (task_cpu(p) != cpu) {
2781 wake_flags |= WF_MIGRATED;
Mike Galbraithf5dc3752009-10-09 08:35:03 +02002782 set_task_cpu(p, cpu);
Peter Zijlstraf339b9d2011-05-31 10:49:20 +02002783 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002784#endif /* CONFIG_SMP */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002785
Peter Zijlstrac05fbaf2011-04-05 17:23:57 +02002786 ttwu_queue(p, cpu);
2787stat:
Peter Zijlstrab84cb5d2011-04-05 17:23:55 +02002788 ttwu_stat(p, cpu, wake_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002789out:
Peter Zijlstra013fdb82011-04-05 17:23:45 +02002790 raw_spin_unlock_irqrestore(&p->pi_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002791
2792 return success;
2793}
2794
David Howells50fa6102009-04-28 15:01:38 +01002795/**
Tejun Heo21aa9af2010-06-08 21:40:37 +02002796 * try_to_wake_up_local - try to wake up a local task with rq lock held
2797 * @p: the thread to be awakened
2798 *
Peter Zijlstra2acca552011-04-05 17:23:50 +02002799 * Put @p on the run-queue if it's not already there. The caller must
Tejun Heo21aa9af2010-06-08 21:40:37 +02002800 * ensure that this_rq() is locked, @p is bound to this_rq() and not
Peter Zijlstra2acca552011-04-05 17:23:50 +02002801 * the current task.
Tejun Heo21aa9af2010-06-08 21:40:37 +02002802 */
2803static void try_to_wake_up_local(struct task_struct *p)
2804{
2805 struct rq *rq = task_rq(p);
Tejun Heo21aa9af2010-06-08 21:40:37 +02002806
2807 BUG_ON(rq != this_rq());
2808 BUG_ON(p == current);
2809 lockdep_assert_held(&rq->lock);
2810
Peter Zijlstra2acca552011-04-05 17:23:50 +02002811 if (!raw_spin_trylock(&p->pi_lock)) {
2812 raw_spin_unlock(&rq->lock);
2813 raw_spin_lock(&p->pi_lock);
2814 raw_spin_lock(&rq->lock);
Tejun Heo21aa9af2010-06-08 21:40:37 +02002815 }
Peter Zijlstra2acca552011-04-05 17:23:50 +02002816
Tejun Heo21aa9af2010-06-08 21:40:37 +02002817 if (!(p->state & TASK_NORMAL))
Peter Zijlstra2acca552011-04-05 17:23:50 +02002818 goto out;
Tejun Heo21aa9af2010-06-08 21:40:37 +02002819
Peter Zijlstrafd2f4412011-04-05 17:23:44 +02002820 if (!p->on_rq)
Peter Zijlstrad7c01d22011-04-05 17:23:43 +02002821 ttwu_activate(rq, p, ENQUEUE_WAKEUP);
2822
Peter Zijlstra23f41ee2011-04-05 17:23:56 +02002823 ttwu_do_wakeup(rq, p, 0);
Peter Zijlstrab84cb5d2011-04-05 17:23:55 +02002824 ttwu_stat(p, smp_processor_id(), 0);
Peter Zijlstra2acca552011-04-05 17:23:50 +02002825out:
2826 raw_spin_unlock(&p->pi_lock);
Tejun Heo21aa9af2010-06-08 21:40:37 +02002827}
2828
2829/**
David Howells50fa6102009-04-28 15:01:38 +01002830 * wake_up_process - Wake up a specific process
2831 * @p: The process to be woken up.
2832 *
2833 * Attempt to wake up the nominated process and move it to the set of runnable
2834 * processes. Returns 1 if the process was woken up, 0 if it was already
2835 * running.
2836 *
2837 * It may be assumed that this function implies a write memory barrier before
2838 * changing the task state if and only if any tasks are woken up.
2839 */
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -08002840int wake_up_process(struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002841{
Matthew Wilcoxd9514f62007-12-06 11:07:07 -05002842 return try_to_wake_up(p, TASK_ALL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002843}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002844EXPORT_SYMBOL(wake_up_process);
2845
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -08002846int wake_up_state(struct task_struct *p, unsigned int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002847{
2848 return try_to_wake_up(p, state, 0);
2849}
2850
Linus Torvalds1da177e2005-04-16 15:20:36 -07002851/*
2852 * Perform scheduler related setup for a newly forked process p.
2853 * p is forked by current.
Ingo Molnardd41f592007-07-09 18:51:59 +02002854 *
2855 * __sched_fork() is basic setup used by init_idle() too:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002856 */
Ingo Molnardd41f592007-07-09 18:51:59 +02002857static void __sched_fork(struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002858{
Peter Zijlstrafd2f4412011-04-05 17:23:44 +02002859 p->on_rq = 0;
2860
2861 p->se.on_rq = 0;
Ingo Molnardd41f592007-07-09 18:51:59 +02002862 p->se.exec_start = 0;
2863 p->se.sum_exec_runtime = 0;
Ingo Molnarf6cf8912007-08-28 12:53:24 +02002864 p->se.prev_sum_exec_runtime = 0;
Ingo Molnar6c594c22008-12-14 12:34:15 +01002865 p->se.nr_migrations = 0;
Peter Zijlstrada7a7352011-01-17 17:03:27 +01002866 p->se.vruntime = 0;
Peter Zijlstrafd2f4412011-04-05 17:23:44 +02002867 INIT_LIST_HEAD(&p->se.group_node);
Ingo Molnar6cfb0d52007-08-02 17:41:40 +02002868
2869#ifdef CONFIG_SCHEDSTATS
Lucas De Marchi41acab82010-03-10 23:37:45 -03002870 memset(&p->se.statistics, 0, sizeof(p->se.statistics));
Ingo Molnar6cfb0d52007-08-02 17:41:40 +02002871#endif
Nick Piggin476d1392005-06-25 14:57:29 -07002872
Peter Zijlstrafa717062008-01-25 21:08:27 +01002873 INIT_LIST_HEAD(&p->rt.run_list);
Nick Piggin476d1392005-06-25 14:57:29 -07002874
Avi Kivitye107be32007-07-26 13:40:43 +02002875#ifdef CONFIG_PREEMPT_NOTIFIERS
2876 INIT_HLIST_HEAD(&p->preempt_notifiers);
2877#endif
Ingo Molnardd41f592007-07-09 18:51:59 +02002878}
2879
2880/*
2881 * fork()/clone()-time setup:
2882 */
Samir Bellabes3e51e3e2011-05-11 18:18:05 +02002883void sched_fork(struct task_struct *p)
Ingo Molnardd41f592007-07-09 18:51:59 +02002884{
Peter Zijlstra0122ec52011-04-05 17:23:51 +02002885 unsigned long flags;
Ingo Molnardd41f592007-07-09 18:51:59 +02002886 int cpu = get_cpu();
2887
2888 __sched_fork(p);
Peter Zijlstra06b83b52009-12-16 18:04:35 +01002889 /*
Peter Zijlstra0017d732010-03-24 18:34:10 +01002890 * We mark the process as running here. This guarantees that
Peter Zijlstra06b83b52009-12-16 18:04:35 +01002891 * nobody will actually run it, and a signal or other external
2892 * event cannot wake it up and insert it on the runqueue either.
2893 */
Peter Zijlstra0017d732010-03-24 18:34:10 +01002894 p->state = TASK_RUNNING;
Ingo Molnardd41f592007-07-09 18:51:59 +02002895
Ingo Molnarb29739f2006-06-27 02:54:51 -07002896 /*
Mike Galbraithc350a042011-07-27 17:14:55 +02002897 * Make sure we do not leak PI boosting priority to the child.
2898 */
2899 p->prio = current->normal_prio;
2900
2901 /*
Mike Galbraithb9dc29e2009-06-17 10:46:01 +02002902 * Revert to default priority/policy on fork if requested.
2903 */
2904 if (unlikely(p->sched_reset_on_fork)) {
Mike Galbraithc350a042011-07-27 17:14:55 +02002905 if (task_has_rt_policy(p)) {
Mike Galbraithb9dc29e2009-06-17 10:46:01 +02002906 p->policy = SCHED_NORMAL;
Mike Galbraith6c697bd2009-06-17 10:48:02 +02002907 p->static_prio = NICE_TO_PRIO(0);
Mike Galbraithc350a042011-07-27 17:14:55 +02002908 p->rt_priority = 0;
2909 } else if (PRIO_TO_NICE(p->static_prio) < 0)
2910 p->static_prio = NICE_TO_PRIO(0);
2911
2912 p->prio = p->normal_prio = __normal_prio(p);
2913 set_load_weight(p);
Mike Galbraith6c697bd2009-06-17 10:48:02 +02002914
Mike Galbraithb9dc29e2009-06-17 10:46:01 +02002915 /*
2916 * We don't need the reset flag anymore after the fork. It has
2917 * fulfilled its duty:
2918 */
2919 p->sched_reset_on_fork = 0;
2920 }
Lennart Poetteringca94c442009-06-15 17:17:47 +02002921
Hiroshi Shimamoto2ddbf952007-10-15 17:00:11 +02002922 if (!rt_prio(p->prio))
2923 p->sched_class = &fair_sched_class;
Ingo Molnarb29739f2006-06-27 02:54:51 -07002924
Peter Zijlstracd29fe62009-11-27 17:32:46 +01002925 if (p->sched_class->task_fork)
2926 p->sched_class->task_fork(p);
2927
Peter Zijlstra86951592010-06-22 11:44:53 +02002928 /*
2929 * The child is not yet in the pid-hash so no cgroup attach races,
2930 * and the cgroup is pinned to this child due to cgroup_fork()
2931 * is ran before sched_fork().
2932 *
2933 * Silence PROVE_RCU.
2934 */
Peter Zijlstra0122ec52011-04-05 17:23:51 +02002935 raw_spin_lock_irqsave(&p->pi_lock, flags);
Peter Zijlstra5f3edc12009-09-10 13:42:00 +02002936 set_task_cpu(p, cpu);
Peter Zijlstra0122ec52011-04-05 17:23:51 +02002937 raw_spin_unlock_irqrestore(&p->pi_lock, flags);
Peter Zijlstra5f3edc12009-09-10 13:42:00 +02002938
Chandra Seetharaman52f17b62006-07-14 00:24:38 -07002939#if defined(CONFIG_SCHEDSTATS) || defined(CONFIG_TASK_DELAY_ACCT)
Ingo Molnardd41f592007-07-09 18:51:59 +02002940 if (likely(sched_info_on()))
Chandra Seetharaman52f17b62006-07-14 00:24:38 -07002941 memset(&p->sched_info, 0, sizeof(p->sched_info));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002942#endif
Peter Zijlstra3ca7a442011-04-05 17:23:40 +02002943#if defined(CONFIG_SMP)
2944 p->on_cpu = 0;
Nick Piggin4866cde2005-06-25 14:57:23 -07002945#endif
Frederic Weisbeckerbdd4e852011-06-08 01:13:27 +02002946#ifdef CONFIG_PREEMPT_COUNT
Nick Piggin4866cde2005-06-25 14:57:23 -07002947 /* Want to start with kernel preemption disabled. */
Al Viroa1261f52005-11-13 16:06:55 -08002948 task_thread_info(p)->preempt_count = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002949#endif
Dario Faggioli806c09a2010-11-30 19:51:33 +01002950#ifdef CONFIG_SMP
Gregory Haskins917b6272008-12-29 09:39:53 -05002951 plist_node_init(&p->pushable_tasks, MAX_PRIO);
Dario Faggioli806c09a2010-11-30 19:51:33 +01002952#endif
Gregory Haskins917b6272008-12-29 09:39:53 -05002953
Nick Piggin476d1392005-06-25 14:57:29 -07002954 put_cpu();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002955}
2956
2957/*
2958 * wake_up_new_task - wake up a newly created task for the first time.
2959 *
2960 * This function will do some initial scheduler statistics housekeeping
2961 * that must be done for every newly created context, then puts the task
2962 * on the runqueue and wakes it.
2963 */
Samir Bellabes3e51e3e2011-05-11 18:18:05 +02002964void wake_up_new_task(struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002965{
2966 unsigned long flags;
Ingo Molnardd41f592007-07-09 18:51:59 +02002967 struct rq *rq;
Peter Zijlstrafabf3182010-01-21 21:04:57 +01002968
Peter Zijlstraab2515c2011-04-05 17:23:52 +02002969 raw_spin_lock_irqsave(&p->pi_lock, flags);
Peter Zijlstrafabf3182010-01-21 21:04:57 +01002970#ifdef CONFIG_SMP
2971 /*
2972 * Fork balancing, do it here and not earlier because:
2973 * - cpus_allowed can change in the fork path
2974 * - any previously selected cpu might disappear through hotplug
Peter Zijlstrafabf3182010-01-21 21:04:57 +01002975 */
Peter Zijlstraab2515c2011-04-05 17:23:52 +02002976 set_task_cpu(p, select_task_rq(p, SD_BALANCE_FORK, 0));
Peter Zijlstrafabf3182010-01-21 21:04:57 +01002977#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002978
Peter Zijlstraab2515c2011-04-05 17:23:52 +02002979 rq = __task_rq_lock(p);
Peter Zijlstracd29fe62009-11-27 17:32:46 +01002980 activate_task(rq, p, 0);
Peter Zijlstrafd2f4412011-04-05 17:23:44 +02002981 p->on_rq = 1;
Peter Zijlstra89363382011-04-05 17:23:42 +02002982 trace_sched_wakeup_new(p, true);
Peter Zijlstraa7558e02009-09-14 20:02:34 +02002983 check_preempt_curr(rq, p, WF_FORK);
Steven Rostedt9a897c52008-01-25 21:08:22 +01002984#ifdef CONFIG_SMP
Peter Zijlstraefbbd052009-12-16 18:04:40 +01002985 if (p->sched_class->task_woken)
2986 p->sched_class->task_woken(rq, p);
Steven Rostedt9a897c52008-01-25 21:08:22 +01002987#endif
Peter Zijlstra0122ec52011-04-05 17:23:51 +02002988 task_rq_unlock(rq, p, &flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002989}
2990
Avi Kivitye107be32007-07-26 13:40:43 +02002991#ifdef CONFIG_PREEMPT_NOTIFIERS
2992
2993/**
Luis Henriques80dd99b2009-03-16 19:58:09 +00002994 * preempt_notifier_register - tell me when current is being preempted & rescheduled
Randy Dunlap421cee22007-07-31 00:37:50 -07002995 * @notifier: notifier struct to register
Avi Kivitye107be32007-07-26 13:40:43 +02002996 */
2997void preempt_notifier_register(struct preempt_notifier *notifier)
2998{
2999 hlist_add_head(&notifier->link, &current->preempt_notifiers);
3000}
3001EXPORT_SYMBOL_GPL(preempt_notifier_register);
3002
3003/**
3004 * preempt_notifier_unregister - no longer interested in preemption notifications
Randy Dunlap421cee22007-07-31 00:37:50 -07003005 * @notifier: notifier struct to unregister
Avi Kivitye107be32007-07-26 13:40:43 +02003006 *
3007 * This is safe to call from within a preemption notifier.
3008 */
3009void preempt_notifier_unregister(struct preempt_notifier *notifier)
3010{
3011 hlist_del(&notifier->link);
3012}
3013EXPORT_SYMBOL_GPL(preempt_notifier_unregister);
3014
3015static void fire_sched_in_preempt_notifiers(struct task_struct *curr)
3016{
3017 struct preempt_notifier *notifier;
3018 struct hlist_node *node;
3019
3020 hlist_for_each_entry(notifier, node, &curr->preempt_notifiers, link)
3021 notifier->ops->sched_in(notifier, raw_smp_processor_id());
3022}
3023
3024static void
3025fire_sched_out_preempt_notifiers(struct task_struct *curr,
3026 struct task_struct *next)
3027{
3028 struct preempt_notifier *notifier;
3029 struct hlist_node *node;
3030
3031 hlist_for_each_entry(notifier, node, &curr->preempt_notifiers, link)
3032 notifier->ops->sched_out(notifier, next);
3033}
3034
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +02003035#else /* !CONFIG_PREEMPT_NOTIFIERS */
Avi Kivitye107be32007-07-26 13:40:43 +02003036
3037static void fire_sched_in_preempt_notifiers(struct task_struct *curr)
3038{
3039}
3040
3041static void
3042fire_sched_out_preempt_notifiers(struct task_struct *curr,
3043 struct task_struct *next)
3044{
3045}
3046
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +02003047#endif /* CONFIG_PREEMPT_NOTIFIERS */
Avi Kivitye107be32007-07-26 13:40:43 +02003048
Linus Torvalds1da177e2005-04-16 15:20:36 -07003049/**
Nick Piggin4866cde2005-06-25 14:57:23 -07003050 * prepare_task_switch - prepare to switch tasks
3051 * @rq: the runqueue preparing to switch
Randy Dunlap421cee22007-07-31 00:37:50 -07003052 * @prev: the current task that is being switched out
Nick Piggin4866cde2005-06-25 14:57:23 -07003053 * @next: the task we are going to switch to.
3054 *
3055 * This is called with the rq lock held and interrupts off. It must
3056 * be paired with a subsequent finish_task_switch after the context
3057 * switch.
3058 *
3059 * prepare_task_switch sets up locking and calls architecture specific
3060 * hooks.
3061 */
Avi Kivitye107be32007-07-26 13:40:43 +02003062static inline void
3063prepare_task_switch(struct rq *rq, struct task_struct *prev,
3064 struct task_struct *next)
Nick Piggin4866cde2005-06-25 14:57:23 -07003065{
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01003066 sched_info_switch(prev, next);
3067 perf_event_task_sched_out(prev, next);
Avi Kivitye107be32007-07-26 13:40:43 +02003068 fire_sched_out_preempt_notifiers(prev, next);
Nick Piggin4866cde2005-06-25 14:57:23 -07003069 prepare_lock_switch(rq, next);
3070 prepare_arch_switch(next);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01003071 trace_sched_switch(prev, next);
Nick Piggin4866cde2005-06-25 14:57:23 -07003072}
3073
3074/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07003075 * finish_task_switch - clean up after a task-switch
Jeff Garzik344baba2005-09-07 01:15:17 -04003076 * @rq: runqueue associated with task-switch
Linus Torvalds1da177e2005-04-16 15:20:36 -07003077 * @prev: the thread we just switched away from.
3078 *
Nick Piggin4866cde2005-06-25 14:57:23 -07003079 * finish_task_switch must be called after the context switch, paired
3080 * with a prepare_task_switch call before the context switch.
3081 * finish_task_switch will reconcile locking set up by prepare_task_switch,
3082 * and do any other architecture-specific cleanup actions.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003083 *
3084 * Note that we may have delayed dropping an mm in context_switch(). If
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01003085 * so, we finish that here outside of the runqueue lock. (Doing it
Linus Torvalds1da177e2005-04-16 15:20:36 -07003086 * with the lock held can cause deadlocks; see schedule() for
3087 * details.)
3088 */
Alexey Dobriyana9957442007-10-15 17:00:13 +02003089static void finish_task_switch(struct rq *rq, struct task_struct *prev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003090 __releases(rq->lock)
3091{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003092 struct mm_struct *mm = rq->prev_mm;
Oleg Nesterov55a101f2006-09-29 02:01:10 -07003093 long prev_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003094
3095 rq->prev_mm = NULL;
3096
3097 /*
3098 * A task struct has one reference for the use as "current".
Oleg Nesterovc394cc92006-09-29 02:01:11 -07003099 * If a task dies, then it sets TASK_DEAD in tsk->state and calls
Oleg Nesterov55a101f2006-09-29 02:01:10 -07003100 * schedule one last time. The schedule call will never return, and
3101 * the scheduled task must drop that reference.
Oleg Nesterovc394cc92006-09-29 02:01:11 -07003102 * The test for TASK_DEAD must occur while the runqueue locks are
Linus Torvalds1da177e2005-04-16 15:20:36 -07003103 * still held, otherwise prev could be scheduled on another cpu, die
3104 * there before we look at prev->state, and then the reference would
3105 * be dropped twice.
3106 * Manfred Spraul <manfred@colorfullife.com>
3107 */
Oleg Nesterov55a101f2006-09-29 02:01:10 -07003108 prev_state = prev->state;
Nick Piggin4866cde2005-06-25 14:57:23 -07003109 finish_arch_switch(prev);
Jamie Iles8381f652010-01-08 15:27:33 +00003110#ifdef __ARCH_WANT_INTERRUPTS_ON_CTXSW
3111 local_irq_disable();
3112#endif /* __ARCH_WANT_INTERRUPTS_ON_CTXSW */
Peter Zijlstra49f47432009-12-27 11:51:52 +01003113 perf_event_task_sched_in(current);
Jamie Iles8381f652010-01-08 15:27:33 +00003114#ifdef __ARCH_WANT_INTERRUPTS_ON_CTXSW
3115 local_irq_enable();
3116#endif /* __ARCH_WANT_INTERRUPTS_ON_CTXSW */
Nick Piggin4866cde2005-06-25 14:57:23 -07003117 finish_lock_switch(rq, prev);
Steven Rostedte8fa1362008-01-25 21:08:05 +01003118
Avi Kivitye107be32007-07-26 13:40:43 +02003119 fire_sched_in_preempt_notifiers(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003120 if (mm)
3121 mmdrop(mm);
Oleg Nesterovc394cc92006-09-29 02:01:11 -07003122 if (unlikely(prev_state == TASK_DEAD)) {
bibo maoc6fd91f2006-03-26 01:38:20 -08003123 /*
3124 * Remove function-return probe instances associated with this
3125 * task and put them back on the free list.
Ingo Molnar9761eea2007-07-09 18:52:00 +02003126 */
bibo maoc6fd91f2006-03-26 01:38:20 -08003127 kprobe_flush_task(prev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003128 put_task_struct(prev);
bibo maoc6fd91f2006-03-26 01:38:20 -08003129 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003130}
3131
Gregory Haskins3f029d32009-07-29 11:08:47 -04003132#ifdef CONFIG_SMP
3133
3134/* assumes rq->lock is held */
3135static inline void pre_schedule(struct rq *rq, struct task_struct *prev)
3136{
3137 if (prev->sched_class->pre_schedule)
3138 prev->sched_class->pre_schedule(rq, prev);
3139}
3140
3141/* rq->lock is NOT held, but preemption is disabled */
3142static inline void post_schedule(struct rq *rq)
3143{
3144 if (rq->post_schedule) {
3145 unsigned long flags;
3146
Thomas Gleixner05fa7852009-11-17 14:28:38 +01003147 raw_spin_lock_irqsave(&rq->lock, flags);
Gregory Haskins3f029d32009-07-29 11:08:47 -04003148 if (rq->curr->sched_class->post_schedule)
3149 rq->curr->sched_class->post_schedule(rq);
Thomas Gleixner05fa7852009-11-17 14:28:38 +01003150 raw_spin_unlock_irqrestore(&rq->lock, flags);
Gregory Haskins3f029d32009-07-29 11:08:47 -04003151
3152 rq->post_schedule = 0;
3153 }
3154}
3155
3156#else
3157
3158static inline void pre_schedule(struct rq *rq, struct task_struct *p)
3159{
3160}
3161
3162static inline void post_schedule(struct rq *rq)
3163{
3164}
3165
3166#endif
3167
Linus Torvalds1da177e2005-04-16 15:20:36 -07003168/**
3169 * schedule_tail - first thing a freshly forked thread must call.
3170 * @prev: the thread we just switched away from.
3171 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07003172asmlinkage void schedule_tail(struct task_struct *prev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003173 __releases(rq->lock)
3174{
Ingo Molnar70b97a72006-07-03 00:25:42 -07003175 struct rq *rq = this_rq();
3176
Nick Piggin4866cde2005-06-25 14:57:23 -07003177 finish_task_switch(rq, prev);
Steven Rostedtda19ab52009-07-29 00:21:22 -04003178
Gregory Haskins3f029d32009-07-29 11:08:47 -04003179 /*
3180 * FIXME: do we need to worry about rq being invalidated by the
3181 * task_switch?
3182 */
3183 post_schedule(rq);
Steven Rostedtda19ab52009-07-29 00:21:22 -04003184
Nick Piggin4866cde2005-06-25 14:57:23 -07003185#ifdef __ARCH_WANT_UNLOCKED_CTXSW
3186 /* In this case, finish_task_switch does not reenable preemption */
3187 preempt_enable();
3188#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003189 if (current->set_child_tid)
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003190 put_user(task_pid_vnr(current), current->set_child_tid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003191}
3192
3193/*
3194 * context_switch - switch to the new MM and the new
3195 * thread's register state.
3196 */
Ingo Molnardd41f592007-07-09 18:51:59 +02003197static inline void
Ingo Molnar70b97a72006-07-03 00:25:42 -07003198context_switch(struct rq *rq, struct task_struct *prev,
Ingo Molnar36c8b582006-07-03 00:25:41 -07003199 struct task_struct *next)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003200{
Ingo Molnardd41f592007-07-09 18:51:59 +02003201 struct mm_struct *mm, *oldmm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003202
Avi Kivitye107be32007-07-26 13:40:43 +02003203 prepare_task_switch(rq, prev, next);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01003204
Ingo Molnardd41f592007-07-09 18:51:59 +02003205 mm = next->mm;
3206 oldmm = prev->active_mm;
Zachary Amsden9226d122007-02-13 13:26:21 +01003207 /*
3208 * For paravirt, this is coupled with an exit in switch_to to
3209 * combine the page table reload and the switch backend into
3210 * one hypercall.
3211 */
Jeremy Fitzhardinge224101e2009-02-18 11:18:57 -08003212 arch_start_context_switch(prev);
Zachary Amsden9226d122007-02-13 13:26:21 +01003213
Heiko Carstens31915ab2010-09-16 14:42:25 +02003214 if (!mm) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003215 next->active_mm = oldmm;
3216 atomic_inc(&oldmm->mm_count);
3217 enter_lazy_tlb(oldmm, next);
3218 } else
3219 switch_mm(oldmm, mm, next);
3220
Heiko Carstens31915ab2010-09-16 14:42:25 +02003221 if (!prev->mm) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003222 prev->active_mm = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003223 rq->prev_mm = oldmm;
3224 }
Ingo Molnar3a5f5e42006-07-14 00:24:27 -07003225 /*
3226 * Since the runqueue lock will be released by the next
3227 * task (which is an invalid locking op but in the case
3228 * of the scheduler it's an obvious special-case), so we
3229 * do an early lockdep release here:
3230 */
3231#ifndef __ARCH_WANT_UNLOCKED_CTXSW
Ingo Molnar8a25d5d2006-07-03 00:24:54 -07003232 spin_release(&rq->lock.dep_map, 1, _THIS_IP_);
Ingo Molnar3a5f5e42006-07-14 00:24:27 -07003233#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003234
3235 /* Here we just switch the register state and the stack. */
3236 switch_to(prev, next, prev);
3237
Ingo Molnardd41f592007-07-09 18:51:59 +02003238 barrier();
3239 /*
3240 * this_rq must be evaluated again because prev may have moved
3241 * CPUs since it called schedule(), thus the 'rq' on its stack
3242 * frame will be invalid.
3243 */
3244 finish_task_switch(this_rq(), prev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003245}
3246
3247/*
3248 * nr_running, nr_uninterruptible and nr_context_switches:
3249 *
3250 * externally visible scheduler statistics: current number of runnable
3251 * threads, current number of uninterruptible-sleeping threads, total
3252 * number of context switches performed since bootup.
3253 */
3254unsigned long nr_running(void)
3255{
3256 unsigned long i, sum = 0;
3257
3258 for_each_online_cpu(i)
3259 sum += cpu_rq(i)->nr_running;
3260
3261 return sum;
3262}
3263
3264unsigned long nr_uninterruptible(void)
3265{
3266 unsigned long i, sum = 0;
3267
KAMEZAWA Hiroyuki0a945022006-03-28 01:56:37 -08003268 for_each_possible_cpu(i)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003269 sum += cpu_rq(i)->nr_uninterruptible;
3270
3271 /*
3272 * Since we read the counters lockless, it might be slightly
3273 * inaccurate. Do not allow it to go below zero though:
3274 */
3275 if (unlikely((long)sum < 0))
3276 sum = 0;
3277
3278 return sum;
3279}
3280
3281unsigned long long nr_context_switches(void)
3282{
Steven Rostedtcc94abf2006-06-27 02:54:31 -07003283 int i;
3284 unsigned long long sum = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003285
KAMEZAWA Hiroyuki0a945022006-03-28 01:56:37 -08003286 for_each_possible_cpu(i)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003287 sum += cpu_rq(i)->nr_switches;
3288
3289 return sum;
3290}
3291
3292unsigned long nr_iowait(void)
3293{
3294 unsigned long i, sum = 0;
3295
KAMEZAWA Hiroyuki0a945022006-03-28 01:56:37 -08003296 for_each_possible_cpu(i)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003297 sum += atomic_read(&cpu_rq(i)->nr_iowait);
3298
3299 return sum;
3300}
3301
Peter Zijlstra8c215bd2010-07-01 09:07:17 +02003302unsigned long nr_iowait_cpu(int cpu)
Arjan van de Ven69d25872009-09-21 17:04:08 -07003303{
Peter Zijlstra8c215bd2010-07-01 09:07:17 +02003304 struct rq *this = cpu_rq(cpu);
Arjan van de Ven69d25872009-09-21 17:04:08 -07003305 return atomic_read(&this->nr_iowait);
3306}
3307
3308unsigned long this_cpu_load(void)
3309{
3310 struct rq *this = this_rq();
3311 return this->cpu_load[0];
3312}
3313
3314
Thomas Gleixnerdce48a82009-04-11 10:43:41 +02003315/* Variables and functions for calc_load */
3316static atomic_long_t calc_load_tasks;
3317static unsigned long calc_load_update;
3318unsigned long avenrun[3];
3319EXPORT_SYMBOL(avenrun);
3320
Peter Zijlstra74f51872010-04-22 21:50:19 +02003321static long calc_load_fold_active(struct rq *this_rq)
3322{
3323 long nr_active, delta = 0;
3324
3325 nr_active = this_rq->nr_running;
3326 nr_active += (long) this_rq->nr_uninterruptible;
3327
3328 if (nr_active != this_rq->calc_load_active) {
3329 delta = nr_active - this_rq->calc_load_active;
3330 this_rq->calc_load_active = nr_active;
3331 }
3332
3333 return delta;
3334}
3335
Peter Zijlstra0f004f52010-11-30 19:48:45 +01003336static unsigned long
3337calc_load(unsigned long load, unsigned long exp, unsigned long active)
3338{
3339 load *= exp;
3340 load += active * (FIXED_1 - exp);
3341 load += 1UL << (FSHIFT - 1);
3342 return load >> FSHIFT;
3343}
3344
Peter Zijlstra74f51872010-04-22 21:50:19 +02003345#ifdef CONFIG_NO_HZ
3346/*
3347 * For NO_HZ we delay the active fold to the next LOAD_FREQ update.
3348 *
3349 * When making the ILB scale, we should try to pull this in as well.
3350 */
3351static atomic_long_t calc_load_tasks_idle;
3352
3353static void calc_load_account_idle(struct rq *this_rq)
3354{
3355 long delta;
3356
3357 delta = calc_load_fold_active(this_rq);
3358 if (delta)
3359 atomic_long_add(delta, &calc_load_tasks_idle);
3360}
3361
3362static long calc_load_fold_idle(void)
3363{
3364 long delta = 0;
3365
3366 /*
3367 * Its got a race, we don't care...
3368 */
3369 if (atomic_long_read(&calc_load_tasks_idle))
3370 delta = atomic_long_xchg(&calc_load_tasks_idle, 0);
3371
3372 return delta;
3373}
Peter Zijlstra0f004f52010-11-30 19:48:45 +01003374
3375/**
3376 * fixed_power_int - compute: x^n, in O(log n) time
3377 *
3378 * @x: base of the power
3379 * @frac_bits: fractional bits of @x
3380 * @n: power to raise @x to.
3381 *
3382 * By exploiting the relation between the definition of the natural power
3383 * function: x^n := x*x*...*x (x multiplied by itself for n times), and
3384 * the binary encoding of numbers used by computers: n := \Sum n_i * 2^i,
3385 * (where: n_i \elem {0, 1}, the binary vector representing n),
3386 * we find: x^n := x^(\Sum n_i * 2^i) := \Prod x^(n_i * 2^i), which is
3387 * of course trivially computable in O(log_2 n), the length of our binary
3388 * vector.
3389 */
3390static unsigned long
3391fixed_power_int(unsigned long x, unsigned int frac_bits, unsigned int n)
3392{
3393 unsigned long result = 1UL << frac_bits;
3394
3395 if (n) for (;;) {
3396 if (n & 1) {
3397 result *= x;
3398 result += 1UL << (frac_bits - 1);
3399 result >>= frac_bits;
3400 }
3401 n >>= 1;
3402 if (!n)
3403 break;
3404 x *= x;
3405 x += 1UL << (frac_bits - 1);
3406 x >>= frac_bits;
3407 }
3408
3409 return result;
3410}
3411
3412/*
3413 * a1 = a0 * e + a * (1 - e)
3414 *
3415 * a2 = a1 * e + a * (1 - e)
3416 * = (a0 * e + a * (1 - e)) * e + a * (1 - e)
3417 * = a0 * e^2 + a * (1 - e) * (1 + e)
3418 *
3419 * a3 = a2 * e + a * (1 - e)
3420 * = (a0 * e^2 + a * (1 - e) * (1 + e)) * e + a * (1 - e)
3421 * = a0 * e^3 + a * (1 - e) * (1 + e + e^2)
3422 *
3423 * ...
3424 *
3425 * an = a0 * e^n + a * (1 - e) * (1 + e + ... + e^n-1) [1]
3426 * = a0 * e^n + a * (1 - e) * (1 - e^n)/(1 - e)
3427 * = a0 * e^n + a * (1 - e^n)
3428 *
3429 * [1] application of the geometric series:
3430 *
3431 * n 1 - x^(n+1)
3432 * S_n := \Sum x^i = -------------
3433 * i=0 1 - x
3434 */
3435static unsigned long
3436calc_load_n(unsigned long load, unsigned long exp,
3437 unsigned long active, unsigned int n)
3438{
3439
3440 return calc_load(load, fixed_power_int(exp, FSHIFT, n), active);
3441}
3442
3443/*
3444 * NO_HZ can leave us missing all per-cpu ticks calling
3445 * calc_load_account_active(), but since an idle CPU folds its delta into
3446 * calc_load_tasks_idle per calc_load_account_idle(), all we need to do is fold
3447 * in the pending idle delta if our idle period crossed a load cycle boundary.
3448 *
3449 * Once we've updated the global active value, we need to apply the exponential
3450 * weights adjusted to the number of cycles missed.
3451 */
3452static void calc_global_nohz(unsigned long ticks)
3453{
3454 long delta, active, n;
3455
3456 if (time_before(jiffies, calc_load_update))
3457 return;
3458
3459 /*
3460 * If we crossed a calc_load_update boundary, make sure to fold
3461 * any pending idle changes, the respective CPUs might have
3462 * missed the tick driven calc_load_account_active() update
3463 * due to NO_HZ.
3464 */
3465 delta = calc_load_fold_idle();
3466 if (delta)
3467 atomic_long_add(delta, &calc_load_tasks);
3468
3469 /*
3470 * If we were idle for multiple load cycles, apply them.
3471 */
3472 if (ticks >= LOAD_FREQ) {
3473 n = ticks / LOAD_FREQ;
3474
3475 active = atomic_long_read(&calc_load_tasks);
3476 active = active > 0 ? active * FIXED_1 : 0;
3477
3478 avenrun[0] = calc_load_n(avenrun[0], EXP_1, active, n);
3479 avenrun[1] = calc_load_n(avenrun[1], EXP_5, active, n);
3480 avenrun[2] = calc_load_n(avenrun[2], EXP_15, active, n);
3481
3482 calc_load_update += n * LOAD_FREQ;
3483 }
3484
3485 /*
3486 * Its possible the remainder of the above division also crosses
3487 * a LOAD_FREQ period, the regular check in calc_global_load()
3488 * which comes after this will take care of that.
3489 *
3490 * Consider us being 11 ticks before a cycle completion, and us
3491 * sleeping for 4*LOAD_FREQ + 22 ticks, then the above code will
3492 * age us 4 cycles, and the test in calc_global_load() will
3493 * pick up the final one.
3494 */
3495}
Peter Zijlstra74f51872010-04-22 21:50:19 +02003496#else
3497static void calc_load_account_idle(struct rq *this_rq)
3498{
3499}
3500
3501static inline long calc_load_fold_idle(void)
3502{
3503 return 0;
3504}
Peter Zijlstra0f004f52010-11-30 19:48:45 +01003505
3506static void calc_global_nohz(unsigned long ticks)
3507{
3508}
Peter Zijlstra74f51872010-04-22 21:50:19 +02003509#endif
3510
Thomas Gleixner2d024942009-05-02 20:08:52 +02003511/**
3512 * get_avenrun - get the load average array
3513 * @loads: pointer to dest load array
3514 * @offset: offset to add
3515 * @shift: shift count to shift the result left
3516 *
3517 * These values are estimates at best, so no need for locking.
3518 */
3519void get_avenrun(unsigned long *loads, unsigned long offset, int shift)
3520{
3521 loads[0] = (avenrun[0] + offset) << shift;
3522 loads[1] = (avenrun[1] + offset) << shift;
3523 loads[2] = (avenrun[2] + offset) << shift;
3524}
3525
Thomas Gleixnerdce48a82009-04-11 10:43:41 +02003526/*
3527 * calc_load - update the avenrun load estimates 10 ticks after the
3528 * CPUs have updated calc_load_tasks.
3529 */
Peter Zijlstra0f004f52010-11-30 19:48:45 +01003530void calc_global_load(unsigned long ticks)
Thomas Gleixnerdce48a82009-04-11 10:43:41 +02003531{
Thomas Gleixnerdce48a82009-04-11 10:43:41 +02003532 long active;
3533
Peter Zijlstra0f004f52010-11-30 19:48:45 +01003534 calc_global_nohz(ticks);
3535
3536 if (time_before(jiffies, calc_load_update + 10))
Thomas Gleixnerdce48a82009-04-11 10:43:41 +02003537 return;
3538
3539 active = atomic_long_read(&calc_load_tasks);
3540 active = active > 0 ? active * FIXED_1 : 0;
3541
3542 avenrun[0] = calc_load(avenrun[0], EXP_1, active);
3543 avenrun[1] = calc_load(avenrun[1], EXP_5, active);
3544 avenrun[2] = calc_load(avenrun[2], EXP_15, active);
3545
3546 calc_load_update += LOAD_FREQ;
3547}
3548
3549/*
Peter Zijlstra74f51872010-04-22 21:50:19 +02003550 * Called from update_cpu_load() to periodically update this CPU's
3551 * active count.
Thomas Gleixnerdce48a82009-04-11 10:43:41 +02003552 */
3553static void calc_load_account_active(struct rq *this_rq)
3554{
Peter Zijlstra74f51872010-04-22 21:50:19 +02003555 long delta;
Thomas Gleixnerdce48a82009-04-11 10:43:41 +02003556
Peter Zijlstra74f51872010-04-22 21:50:19 +02003557 if (time_before(jiffies, this_rq->calc_load_update))
3558 return;
Thomas Gleixnerdce48a82009-04-11 10:43:41 +02003559
Peter Zijlstra74f51872010-04-22 21:50:19 +02003560 delta = calc_load_fold_active(this_rq);
3561 delta += calc_load_fold_idle();
3562 if (delta)
Thomas Gleixnerdce48a82009-04-11 10:43:41 +02003563 atomic_long_add(delta, &calc_load_tasks);
Peter Zijlstra74f51872010-04-22 21:50:19 +02003564
3565 this_rq->calc_load_update += LOAD_FREQ;
Jack Steinerdb1b1fe2006-03-31 02:31:21 -08003566}
3567
Linus Torvalds1da177e2005-04-16 15:20:36 -07003568/*
Venkatesh Pallipadifdf3e952010-05-17 18:14:43 -07003569 * The exact cpuload at various idx values, calculated at every tick would be
3570 * load = (2^idx - 1) / 2^idx * load + 1 / 2^idx * cur_load
3571 *
3572 * If a cpu misses updates for n-1 ticks (as it was idle) and update gets called
3573 * on nth tick when cpu may be busy, then we have:
3574 * load = ((2^idx - 1) / 2^idx)^(n-1) * load
3575 * load = (2^idx - 1) / 2^idx) * load + 1 / 2^idx * cur_load
3576 *
3577 * decay_load_missed() below does efficient calculation of
3578 * load = ((2^idx - 1) / 2^idx)^(n-1) * load
3579 * avoiding 0..n-1 loop doing load = ((2^idx - 1) / 2^idx) * load
3580 *
3581 * The calculation is approximated on a 128 point scale.
3582 * degrade_zero_ticks is the number of ticks after which load at any
3583 * particular idx is approximated to be zero.
3584 * degrade_factor is a precomputed table, a row for each load idx.
3585 * Each column corresponds to degradation factor for a power of two ticks,
3586 * based on 128 point scale.
3587 * Example:
3588 * row 2, col 3 (=12) says that the degradation at load idx 2 after
3589 * 8 ticks is 12/128 (which is an approximation of exact factor 3^8/4^8).
3590 *
3591 * With this power of 2 load factors, we can degrade the load n times
3592 * by looking at 1 bits in n and doing as many mult/shift instead of
3593 * n mult/shifts needed by the exact degradation.
3594 */
3595#define DEGRADE_SHIFT 7
3596static const unsigned char
3597 degrade_zero_ticks[CPU_LOAD_IDX_MAX] = {0, 8, 32, 64, 128};
3598static const unsigned char
3599 degrade_factor[CPU_LOAD_IDX_MAX][DEGRADE_SHIFT + 1] = {
3600 {0, 0, 0, 0, 0, 0, 0, 0},
3601 {64, 32, 8, 0, 0, 0, 0, 0},
3602 {96, 72, 40, 12, 1, 0, 0},
3603 {112, 98, 75, 43, 15, 1, 0},
3604 {120, 112, 98, 76, 45, 16, 2} };
3605
3606/*
3607 * Update cpu_load for any missed ticks, due to tickless idle. The backlog
3608 * would be when CPU is idle and so we just decay the old load without
3609 * adding any new load.
3610 */
3611static unsigned long
3612decay_load_missed(unsigned long load, unsigned long missed_updates, int idx)
3613{
3614 int j = 0;
3615
3616 if (!missed_updates)
3617 return load;
3618
3619 if (missed_updates >= degrade_zero_ticks[idx])
3620 return 0;
3621
3622 if (idx == 1)
3623 return load >> missed_updates;
3624
3625 while (missed_updates) {
3626 if (missed_updates % 2)
3627 load = (load * degrade_factor[idx][j]) >> DEGRADE_SHIFT;
3628
3629 missed_updates >>= 1;
3630 j++;
3631 }
3632 return load;
3633}
3634
3635/*
Ingo Molnardd41f592007-07-09 18:51:59 +02003636 * Update rq->cpu_load[] statistics. This function is usually called every
Venkatesh Pallipadifdf3e952010-05-17 18:14:43 -07003637 * scheduler tick (TICK_NSEC). With tickless idle this will not be called
3638 * every tick. We fix it up based on jiffies.
Ingo Molnar48f24c42006-07-03 00:25:40 -07003639 */
Ingo Molnardd41f592007-07-09 18:51:59 +02003640static void update_cpu_load(struct rq *this_rq)
Ingo Molnar48f24c42006-07-03 00:25:40 -07003641{
Dmitry Adamushko495eca42007-10-15 17:00:06 +02003642 unsigned long this_load = this_rq->load.weight;
Venkatesh Pallipadifdf3e952010-05-17 18:14:43 -07003643 unsigned long curr_jiffies = jiffies;
3644 unsigned long pending_updates;
Ingo Molnardd41f592007-07-09 18:51:59 +02003645 int i, scale;
3646
3647 this_rq->nr_load_updates++;
Ingo Molnardd41f592007-07-09 18:51:59 +02003648
Venkatesh Pallipadifdf3e952010-05-17 18:14:43 -07003649 /* Avoid repeated calls on same jiffy, when moving in and out of idle */
3650 if (curr_jiffies == this_rq->last_load_update_tick)
3651 return;
3652
3653 pending_updates = curr_jiffies - this_rq->last_load_update_tick;
3654 this_rq->last_load_update_tick = curr_jiffies;
3655
Ingo Molnardd41f592007-07-09 18:51:59 +02003656 /* Update our load: */
Venkatesh Pallipadifdf3e952010-05-17 18:14:43 -07003657 this_rq->cpu_load[0] = this_load; /* Fasttrack for idx 0 */
3658 for (i = 1, scale = 2; i < CPU_LOAD_IDX_MAX; i++, scale += scale) {
Ingo Molnardd41f592007-07-09 18:51:59 +02003659 unsigned long old_load, new_load;
3660
3661 /* scale is effectively 1 << i now, and >> i divides by scale */
3662
3663 old_load = this_rq->cpu_load[i];
Venkatesh Pallipadifdf3e952010-05-17 18:14:43 -07003664 old_load = decay_load_missed(old_load, pending_updates - 1, i);
Ingo Molnardd41f592007-07-09 18:51:59 +02003665 new_load = this_load;
Ingo Molnara25707f2007-10-15 17:00:03 +02003666 /*
3667 * Round up the averaging division if load is increasing. This
3668 * prevents us from getting stuck on 9 if the load is 10, for
3669 * example.
3670 */
3671 if (new_load > old_load)
Venkatesh Pallipadifdf3e952010-05-17 18:14:43 -07003672 new_load += scale - 1;
3673
3674 this_rq->cpu_load[i] = (old_load * (scale - 1) + new_load) >> i;
Ingo Molnardd41f592007-07-09 18:51:59 +02003675 }
Suresh Siddhada2b71e2010-08-23 13:42:51 -07003676
3677 sched_avg_update(this_rq);
Venkatesh Pallipadifdf3e952010-05-17 18:14:43 -07003678}
3679
3680static void update_cpu_load_active(struct rq *this_rq)
3681{
3682 update_cpu_load(this_rq);
Thomas Gleixnerdce48a82009-04-11 10:43:41 +02003683
Peter Zijlstra74f51872010-04-22 21:50:19 +02003684 calc_load_account_active(this_rq);
Ingo Molnar48f24c42006-07-03 00:25:40 -07003685}
3686
Ingo Molnardd41f592007-07-09 18:51:59 +02003687#ifdef CONFIG_SMP
3688
Ingo Molnar48f24c42006-07-03 00:25:40 -07003689/*
Peter Zijlstra38022902009-12-16 18:04:37 +01003690 * sched_exec - execve() is a valuable balancing opportunity, because at
3691 * this point the task has the smallest effective memory and cache footprint.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003692 */
Peter Zijlstra38022902009-12-16 18:04:37 +01003693void sched_exec(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003694{
Peter Zijlstra38022902009-12-16 18:04:37 +01003695 struct task_struct *p = current;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003696 unsigned long flags;
Peter Zijlstra0017d732010-03-24 18:34:10 +01003697 int dest_cpu;
Peter Zijlstra38022902009-12-16 18:04:37 +01003698
Peter Zijlstra8f42ced2011-04-05 17:23:53 +02003699 raw_spin_lock_irqsave(&p->pi_lock, flags);
Peter Zijlstra7608dec2011-04-05 17:23:46 +02003700 dest_cpu = p->sched_class->select_task_rq(p, SD_BALANCE_EXEC, 0);
Peter Zijlstra0017d732010-03-24 18:34:10 +01003701 if (dest_cpu == smp_processor_id())
3702 goto unlock;
Peter Zijlstra38022902009-12-16 18:04:37 +01003703
Peter Zijlstra8f42ced2011-04-05 17:23:53 +02003704 if (likely(cpu_active(dest_cpu))) {
Tejun Heo969c7922010-05-06 18:49:21 +02003705 struct migration_arg arg = { p, dest_cpu };
Ingo Molnar36c8b582006-07-03 00:25:41 -07003706
Peter Zijlstra8f42ced2011-04-05 17:23:53 +02003707 raw_spin_unlock_irqrestore(&p->pi_lock, flags);
3708 stop_one_cpu(task_cpu(p), migration_cpu_stop, &arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003709 return;
3710 }
Peter Zijlstra0017d732010-03-24 18:34:10 +01003711unlock:
Peter Zijlstra8f42ced2011-04-05 17:23:53 +02003712 raw_spin_unlock_irqrestore(&p->pi_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003713}
3714
Linus Torvalds1da177e2005-04-16 15:20:36 -07003715#endif
3716
Linus Torvalds1da177e2005-04-16 15:20:36 -07003717DEFINE_PER_CPU(struct kernel_stat, kstat);
3718
3719EXPORT_PER_CPU_SYMBOL(kstat);
3720
3721/*
Hidetoshi Setoc5f8d992009-03-31 16:56:03 +09003722 * Return any ns on the sched_clock that have not yet been accounted in
Frank Mayharf06febc2008-09-12 09:54:39 -07003723 * @p in case that task is currently running.
Hidetoshi Setoc5f8d992009-03-31 16:56:03 +09003724 *
3725 * Called with task_rq_lock() held on @rq.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003726 */
Hidetoshi Setoc5f8d992009-03-31 16:56:03 +09003727static u64 do_task_delta_exec(struct task_struct *p, struct rq *rq)
3728{
3729 u64 ns = 0;
3730
3731 if (task_current(rq, p)) {
3732 update_rq_clock(rq);
Venkatesh Pallipadi305e6832010-10-04 17:03:21 -07003733 ns = rq->clock_task - p->se.exec_start;
Hidetoshi Setoc5f8d992009-03-31 16:56:03 +09003734 if ((s64)ns < 0)
3735 ns = 0;
3736 }
3737
3738 return ns;
3739}
3740
Frank Mayharbb34d922008-09-12 09:54:39 -07003741unsigned long long task_delta_exec(struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003742{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003743 unsigned long flags;
Ingo Molnar41b86e92007-07-09 18:51:58 +02003744 struct rq *rq;
Frank Mayharbb34d922008-09-12 09:54:39 -07003745 u64 ns = 0;
Ingo Molnar48f24c42006-07-03 00:25:40 -07003746
Ingo Molnar41b86e92007-07-09 18:51:58 +02003747 rq = task_rq_lock(p, &flags);
Hidetoshi Setoc5f8d992009-03-31 16:56:03 +09003748 ns = do_task_delta_exec(p, rq);
Peter Zijlstra0122ec52011-04-05 17:23:51 +02003749 task_rq_unlock(rq, p, &flags);
Ingo Molnar15084872008-09-30 08:28:17 +02003750
Hidetoshi Setoc5f8d992009-03-31 16:56:03 +09003751 return ns;
3752}
Frank Mayharf06febc2008-09-12 09:54:39 -07003753
Hidetoshi Setoc5f8d992009-03-31 16:56:03 +09003754/*
3755 * Return accounted runtime for the task.
3756 * In case the task is currently running, return the runtime plus current's
3757 * pending runtime that have not been accounted yet.
3758 */
3759unsigned long long task_sched_runtime(struct task_struct *p)
3760{
3761 unsigned long flags;
3762 struct rq *rq;
3763 u64 ns = 0;
Ingo Molnar48f24c42006-07-03 00:25:40 -07003764
Hidetoshi Setoc5f8d992009-03-31 16:56:03 +09003765 rq = task_rq_lock(p, &flags);
3766 ns = p->se.sum_exec_runtime + do_task_delta_exec(p, rq);
Peter Zijlstra0122ec52011-04-05 17:23:51 +02003767 task_rq_unlock(rq, p, &flags);
Hidetoshi Setoc5f8d992009-03-31 16:56:03 +09003768
3769 return ns;
3770}
3771
3772/*
3773 * Return sum_exec_runtime for the thread group.
3774 * In case the task is currently running, return the sum plus current's
3775 * pending runtime that have not been accounted yet.
3776 *
3777 * Note that the thread group might have other running tasks as well,
3778 * so the return value not includes other pending runtime that other
3779 * running tasks might have.
3780 */
3781unsigned long long thread_group_sched_runtime(struct task_struct *p)
3782{
3783 struct task_cputime totals;
3784 unsigned long flags;
3785 struct rq *rq;
3786 u64 ns;
3787
3788 rq = task_rq_lock(p, &flags);
3789 thread_group_cputime(p, &totals);
3790 ns = totals.sum_exec_runtime + do_task_delta_exec(p, rq);
Peter Zijlstra0122ec52011-04-05 17:23:51 +02003791 task_rq_unlock(rq, p, &flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003792
3793 return ns;
3794}
3795
3796/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003797 * Account user cpu time to a process.
3798 * @p: the process that the cpu time gets accounted to
Linus Torvalds1da177e2005-04-16 15:20:36 -07003799 * @cputime: the cpu time spent in user space since the last update
Martin Schwidefsky457533a2008-12-31 15:11:37 +01003800 * @cputime_scaled: cputime scaled by cpu frequency
Linus Torvalds1da177e2005-04-16 15:20:36 -07003801 */
Martin Schwidefsky457533a2008-12-31 15:11:37 +01003802void account_user_time(struct task_struct *p, cputime_t cputime,
3803 cputime_t cputime_scaled)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003804{
3805 struct cpu_usage_stat *cpustat = &kstat_this_cpu.cpustat;
3806 cputime64_t tmp;
3807
Martin Schwidefsky457533a2008-12-31 15:11:37 +01003808 /* Add user time to process. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003809 p->utime = cputime_add(p->utime, cputime);
Martin Schwidefsky457533a2008-12-31 15:11:37 +01003810 p->utimescaled = cputime_add(p->utimescaled, cputime_scaled);
Frank Mayharf06febc2008-09-12 09:54:39 -07003811 account_group_user_time(p, cputime);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003812
3813 /* Add user time to cpustat. */
3814 tmp = cputime_to_cputime64(cputime);
3815 if (TASK_NICE(p) > 0)
3816 cpustat->nice = cputime64_add(cpustat->nice, tmp);
3817 else
3818 cpustat->user = cputime64_add(cpustat->user, tmp);
Bharata B Raoef12fef2009-03-31 10:02:22 +05303819
3820 cpuacct_update_stats(p, CPUACCT_STAT_USER, cputime);
Jonathan Lim49b5cf32008-07-25 01:48:40 -07003821 /* Account for user time used */
3822 acct_update_integrals(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003823}
3824
3825/*
Laurent Vivier94886b82007-10-15 17:00:19 +02003826 * Account guest cpu time to a process.
3827 * @p: the process that the cpu time gets accounted to
3828 * @cputime: the cpu time spent in virtual machine since the last update
Martin Schwidefsky457533a2008-12-31 15:11:37 +01003829 * @cputime_scaled: cputime scaled by cpu frequency
Laurent Vivier94886b82007-10-15 17:00:19 +02003830 */
Martin Schwidefsky457533a2008-12-31 15:11:37 +01003831static void account_guest_time(struct task_struct *p, cputime_t cputime,
3832 cputime_t cputime_scaled)
Laurent Vivier94886b82007-10-15 17:00:19 +02003833{
3834 cputime64_t tmp;
3835 struct cpu_usage_stat *cpustat = &kstat_this_cpu.cpustat;
3836
3837 tmp = cputime_to_cputime64(cputime);
3838
Martin Schwidefsky457533a2008-12-31 15:11:37 +01003839 /* Add guest time to process. */
Laurent Vivier94886b82007-10-15 17:00:19 +02003840 p->utime = cputime_add(p->utime, cputime);
Martin Schwidefsky457533a2008-12-31 15:11:37 +01003841 p->utimescaled = cputime_add(p->utimescaled, cputime_scaled);
Frank Mayharf06febc2008-09-12 09:54:39 -07003842 account_group_user_time(p, cputime);
Laurent Vivier94886b82007-10-15 17:00:19 +02003843 p->gtime = cputime_add(p->gtime, cputime);
3844
Martin Schwidefsky457533a2008-12-31 15:11:37 +01003845 /* Add guest time to cpustat. */
Ryota Ozakice0e7b22009-10-24 01:20:10 +09003846 if (TASK_NICE(p) > 0) {
3847 cpustat->nice = cputime64_add(cpustat->nice, tmp);
3848 cpustat->guest_nice = cputime64_add(cpustat->guest_nice, tmp);
3849 } else {
3850 cpustat->user = cputime64_add(cpustat->user, tmp);
3851 cpustat->guest = cputime64_add(cpustat->guest, tmp);
3852 }
Laurent Vivier94886b82007-10-15 17:00:19 +02003853}
3854
3855/*
Venkatesh Pallipadi70a89a62010-12-21 17:09:02 -08003856 * Account system cpu time to a process and desired cpustat field
3857 * @p: the process that the cpu time gets accounted to
3858 * @cputime: the cpu time spent in kernel space since the last update
3859 * @cputime_scaled: cputime scaled by cpu frequency
3860 * @target_cputime64: pointer to cpustat field that has to be updated
3861 */
3862static inline
3863void __account_system_time(struct task_struct *p, cputime_t cputime,
3864 cputime_t cputime_scaled, cputime64_t *target_cputime64)
3865{
3866 cputime64_t tmp = cputime_to_cputime64(cputime);
3867
3868 /* Add system time to process. */
3869 p->stime = cputime_add(p->stime, cputime);
3870 p->stimescaled = cputime_add(p->stimescaled, cputime_scaled);
3871 account_group_system_time(p, cputime);
3872
3873 /* Add system time to cpustat. */
3874 *target_cputime64 = cputime64_add(*target_cputime64, tmp);
3875 cpuacct_update_stats(p, CPUACCT_STAT_SYSTEM, cputime);
3876
3877 /* Account for system time used */
3878 acct_update_integrals(p);
3879}
3880
3881/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003882 * Account system cpu time to a process.
3883 * @p: the process that the cpu time gets accounted to
3884 * @hardirq_offset: the offset to subtract from hardirq_count()
3885 * @cputime: the cpu time spent in kernel space since the last update
Martin Schwidefsky457533a2008-12-31 15:11:37 +01003886 * @cputime_scaled: cputime scaled by cpu frequency
Linus Torvalds1da177e2005-04-16 15:20:36 -07003887 */
3888void account_system_time(struct task_struct *p, int hardirq_offset,
Martin Schwidefsky457533a2008-12-31 15:11:37 +01003889 cputime_t cputime, cputime_t cputime_scaled)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003890{
3891 struct cpu_usage_stat *cpustat = &kstat_this_cpu.cpustat;
Venkatesh Pallipadi70a89a62010-12-21 17:09:02 -08003892 cputime64_t *target_cputime64;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003893
Harvey Harrison983ed7a2008-04-24 18:17:55 -07003894 if ((p->flags & PF_VCPU) && (irq_count() - hardirq_offset == 0)) {
Martin Schwidefsky457533a2008-12-31 15:11:37 +01003895 account_guest_time(p, cputime, cputime_scaled);
Harvey Harrison983ed7a2008-04-24 18:17:55 -07003896 return;
3897 }
Laurent Vivier94886b82007-10-15 17:00:19 +02003898
Linus Torvalds1da177e2005-04-16 15:20:36 -07003899 if (hardirq_count() - hardirq_offset)
Venkatesh Pallipadi70a89a62010-12-21 17:09:02 -08003900 target_cputime64 = &cpustat->irq;
Venkatesh Pallipadi75e10562010-10-04 17:03:16 -07003901 else if (in_serving_softirq())
Venkatesh Pallipadi70a89a62010-12-21 17:09:02 -08003902 target_cputime64 = &cpustat->softirq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003903 else
Venkatesh Pallipadi70a89a62010-12-21 17:09:02 -08003904 target_cputime64 = &cpustat->system;
Martin Schwidefsky79741dd2008-12-31 15:11:38 +01003905
Venkatesh Pallipadi70a89a62010-12-21 17:09:02 -08003906 __account_system_time(p, cputime, cputime_scaled, target_cputime64);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003907}
3908
3909/*
3910 * Account for involuntary wait time.
Venkatesh Pallipadi544b4a12011-02-25 15:13:16 -08003911 * @cputime: the cpu time spent in involuntary wait
Linus Torvalds1da177e2005-04-16 15:20:36 -07003912 */
Martin Schwidefsky79741dd2008-12-31 15:11:38 +01003913void account_steal_time(cputime_t cputime)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003914{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003915 struct cpu_usage_stat *cpustat = &kstat_this_cpu.cpustat;
Martin Schwidefsky79741dd2008-12-31 15:11:38 +01003916 cputime64_t cputime64 = cputime_to_cputime64(cputime);
3917
3918 cpustat->steal = cputime64_add(cpustat->steal, cputime64);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003919}
3920
Christoph Lameter7835b982006-12-10 02:20:22 -08003921/*
Martin Schwidefsky79741dd2008-12-31 15:11:38 +01003922 * Account for idle time.
3923 * @cputime: the cpu time spent in idle wait
Linus Torvalds1da177e2005-04-16 15:20:36 -07003924 */
Martin Schwidefsky79741dd2008-12-31 15:11:38 +01003925void account_idle_time(cputime_t cputime)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003926{
3927 struct cpu_usage_stat *cpustat = &kstat_this_cpu.cpustat;
Martin Schwidefsky79741dd2008-12-31 15:11:38 +01003928 cputime64_t cputime64 = cputime_to_cputime64(cputime);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003929 struct rq *rq = this_rq();
3930
Martin Schwidefsky79741dd2008-12-31 15:11:38 +01003931 if (atomic_read(&rq->nr_iowait) > 0)
3932 cpustat->iowait = cputime64_add(cpustat->iowait, cputime64);
3933 else
3934 cpustat->idle = cputime64_add(cpustat->idle, cputime64);
Christoph Lameter7835b982006-12-10 02:20:22 -08003935}
3936
Glauber Costae6e66852011-07-11 15:28:17 -04003937static __always_inline bool steal_account_process_tick(void)
3938{
3939#ifdef CONFIG_PARAVIRT
3940 if (static_branch(&paravirt_steal_enabled)) {
3941 u64 steal, st = 0;
3942
3943 steal = paravirt_steal_clock(smp_processor_id());
3944 steal -= this_rq()->prev_steal_time;
3945
3946 st = steal_ticks(steal);
3947 this_rq()->prev_steal_time += st * TICK_NSEC;
3948
3949 account_steal_time(st);
3950 return st;
3951 }
3952#endif
3953 return false;
3954}
3955
Martin Schwidefsky79741dd2008-12-31 15:11:38 +01003956#ifndef CONFIG_VIRT_CPU_ACCOUNTING
3957
Venkatesh Pallipadiabb74ce2010-12-21 17:09:03 -08003958#ifdef CONFIG_IRQ_TIME_ACCOUNTING
3959/*
3960 * Account a tick to a process and cpustat
3961 * @p: the process that the cpu time gets accounted to
3962 * @user_tick: is the tick from userspace
3963 * @rq: the pointer to rq
3964 *
3965 * Tick demultiplexing follows the order
3966 * - pending hardirq update
3967 * - pending softirq update
3968 * - user_time
3969 * - idle_time
3970 * - system time
3971 * - check for guest_time
3972 * - else account as system_time
3973 *
3974 * Check for hardirq is done both for system and user time as there is
3975 * no timer going off while we are on hardirq and hence we may never get an
3976 * opportunity to update it solely in system time.
3977 * p->stime and friends are only updated on system time and not on irq
3978 * softirq as those do not count in task exec_runtime any more.
3979 */
3980static void irqtime_account_process_tick(struct task_struct *p, int user_tick,
3981 struct rq *rq)
3982{
3983 cputime_t one_jiffy_scaled = cputime_to_scaled(cputime_one_jiffy);
3984 cputime64_t tmp = cputime_to_cputime64(cputime_one_jiffy);
3985 struct cpu_usage_stat *cpustat = &kstat_this_cpu.cpustat;
3986
Glauber Costae6e66852011-07-11 15:28:17 -04003987 if (steal_account_process_tick())
3988 return;
3989
Venkatesh Pallipadiabb74ce2010-12-21 17:09:03 -08003990 if (irqtime_account_hi_update()) {
3991 cpustat->irq = cputime64_add(cpustat->irq, tmp);
3992 } else if (irqtime_account_si_update()) {
3993 cpustat->softirq = cputime64_add(cpustat->softirq, tmp);
Venkatesh Pallipadi414bee92010-12-21 17:09:04 -08003994 } else if (this_cpu_ksoftirqd() == p) {
3995 /*
3996 * ksoftirqd time do not get accounted in cpu_softirq_time.
3997 * So, we have to handle it separately here.
3998 * Also, p->stime needs to be updated for ksoftirqd.
3999 */
4000 __account_system_time(p, cputime_one_jiffy, one_jiffy_scaled,
4001 &cpustat->softirq);
Venkatesh Pallipadiabb74ce2010-12-21 17:09:03 -08004002 } else if (user_tick) {
4003 account_user_time(p, cputime_one_jiffy, one_jiffy_scaled);
4004 } else if (p == rq->idle) {
4005 account_idle_time(cputime_one_jiffy);
4006 } else if (p->flags & PF_VCPU) { /* System time or guest time */
4007 account_guest_time(p, cputime_one_jiffy, one_jiffy_scaled);
4008 } else {
4009 __account_system_time(p, cputime_one_jiffy, one_jiffy_scaled,
4010 &cpustat->system);
4011 }
4012}
4013
4014static void irqtime_account_idle_ticks(int ticks)
4015{
4016 int i;
4017 struct rq *rq = this_rq();
4018
4019 for (i = 0; i < ticks; i++)
4020 irqtime_account_process_tick(current, 0, rq);
4021}
Venkatesh Pallipadi544b4a12011-02-25 15:13:16 -08004022#else /* CONFIG_IRQ_TIME_ACCOUNTING */
Venkatesh Pallipadiabb74ce2010-12-21 17:09:03 -08004023static void irqtime_account_idle_ticks(int ticks) {}
4024static void irqtime_account_process_tick(struct task_struct *p, int user_tick,
4025 struct rq *rq) {}
Venkatesh Pallipadi544b4a12011-02-25 15:13:16 -08004026#endif /* CONFIG_IRQ_TIME_ACCOUNTING */
Martin Schwidefsky79741dd2008-12-31 15:11:38 +01004027
4028/*
4029 * Account a single tick of cpu time.
4030 * @p: the process that the cpu time gets accounted to
4031 * @user_tick: indicates if the tick is a user or a system tick
4032 */
4033void account_process_tick(struct task_struct *p, int user_tick)
4034{
Stanislaw Gruszkaa42548a2009-07-29 12:15:29 +02004035 cputime_t one_jiffy_scaled = cputime_to_scaled(cputime_one_jiffy);
Martin Schwidefsky79741dd2008-12-31 15:11:38 +01004036 struct rq *rq = this_rq();
4037
Venkatesh Pallipadiabb74ce2010-12-21 17:09:03 -08004038 if (sched_clock_irqtime) {
4039 irqtime_account_process_tick(p, user_tick, rq);
4040 return;
4041 }
4042
Glauber Costae6e66852011-07-11 15:28:17 -04004043 if (steal_account_process_tick())
4044 return;
4045
Martin Schwidefsky79741dd2008-12-31 15:11:38 +01004046 if (user_tick)
Stanislaw Gruszkaa42548a2009-07-29 12:15:29 +02004047 account_user_time(p, cputime_one_jiffy, one_jiffy_scaled);
Eric Dumazetf5f293a2009-04-29 14:44:49 +02004048 else if ((p != rq->idle) || (irq_count() != HARDIRQ_OFFSET))
Stanislaw Gruszkaa42548a2009-07-29 12:15:29 +02004049 account_system_time(p, HARDIRQ_OFFSET, cputime_one_jiffy,
Martin Schwidefsky79741dd2008-12-31 15:11:38 +01004050 one_jiffy_scaled);
4051 else
Stanislaw Gruszkaa42548a2009-07-29 12:15:29 +02004052 account_idle_time(cputime_one_jiffy);
Martin Schwidefsky79741dd2008-12-31 15:11:38 +01004053}
4054
4055/*
4056 * Account multiple ticks of steal time.
4057 * @p: the process from which the cpu time has been stolen
4058 * @ticks: number of stolen ticks
4059 */
4060void account_steal_ticks(unsigned long ticks)
4061{
4062 account_steal_time(jiffies_to_cputime(ticks));
4063}
4064
4065/*
4066 * Account multiple ticks of idle time.
4067 * @ticks: number of stolen ticks
4068 */
4069void account_idle_ticks(unsigned long ticks)
4070{
Venkatesh Pallipadiabb74ce2010-12-21 17:09:03 -08004071
4072 if (sched_clock_irqtime) {
4073 irqtime_account_idle_ticks(ticks);
4074 return;
4075 }
4076
Martin Schwidefsky79741dd2008-12-31 15:11:38 +01004077 account_idle_time(jiffies_to_cputime(ticks));
4078}
4079
4080#endif
4081
Christoph Lameter7835b982006-12-10 02:20:22 -08004082/*
Balbir Singh49048622008-09-05 18:12:23 +02004083 * Use precise platform statistics if available:
4084 */
4085#ifdef CONFIG_VIRT_CPU_ACCOUNTING
Hidetoshi Setod180c5b2009-11-26 14:48:30 +09004086void task_times(struct task_struct *p, cputime_t *ut, cputime_t *st)
Balbir Singh49048622008-09-05 18:12:23 +02004087{
Hidetoshi Setod99ca3b2009-12-02 17:26:47 +09004088 *ut = p->utime;
4089 *st = p->stime;
Balbir Singh49048622008-09-05 18:12:23 +02004090}
4091
Hidetoshi Seto0cf55e12009-12-02 17:28:07 +09004092void thread_group_times(struct task_struct *p, cputime_t *ut, cputime_t *st)
Balbir Singh49048622008-09-05 18:12:23 +02004093{
Hidetoshi Seto0cf55e12009-12-02 17:28:07 +09004094 struct task_cputime cputime;
4095
4096 thread_group_cputime(p, &cputime);
4097
4098 *ut = cputime.utime;
4099 *st = cputime.stime;
Balbir Singh49048622008-09-05 18:12:23 +02004100}
4101#else
Hidetoshi Seto761b1d22009-11-12 13:33:45 +09004102
4103#ifndef nsecs_to_cputime
Hidetoshi Setob7b20df2009-11-26 14:49:27 +09004104# define nsecs_to_cputime(__nsecs) nsecs_to_jiffies(__nsecs)
Hidetoshi Seto761b1d22009-11-12 13:33:45 +09004105#endif
4106
Hidetoshi Setod180c5b2009-11-26 14:48:30 +09004107void task_times(struct task_struct *p, cputime_t *ut, cputime_t *st)
Balbir Singh49048622008-09-05 18:12:23 +02004108{
Hidetoshi Setod99ca3b2009-12-02 17:26:47 +09004109 cputime_t rtime, utime = p->utime, total = cputime_add(utime, p->stime);
Balbir Singh49048622008-09-05 18:12:23 +02004110
4111 /*
4112 * Use CFS's precise accounting:
4113 */
Hidetoshi Setod180c5b2009-11-26 14:48:30 +09004114 rtime = nsecs_to_cputime(p->se.sum_exec_runtime);
Balbir Singh49048622008-09-05 18:12:23 +02004115
4116 if (total) {
Stanislaw Gruszkae75e8632010-09-14 16:35:14 +02004117 u64 temp = rtime;
Balbir Singh49048622008-09-05 18:12:23 +02004118
Stanislaw Gruszkae75e8632010-09-14 16:35:14 +02004119 temp *= utime;
Balbir Singh49048622008-09-05 18:12:23 +02004120 do_div(temp, total);
Hidetoshi Setod180c5b2009-11-26 14:48:30 +09004121 utime = (cputime_t)temp;
4122 } else
4123 utime = rtime;
Balbir Singh49048622008-09-05 18:12:23 +02004124
4125 /*
Hidetoshi Setod180c5b2009-11-26 14:48:30 +09004126 * Compare with previous values, to keep monotonicity:
Balbir Singh49048622008-09-05 18:12:23 +02004127 */
Hidetoshi Seto761b1d22009-11-12 13:33:45 +09004128 p->prev_utime = max(p->prev_utime, utime);
Hidetoshi Setod99ca3b2009-12-02 17:26:47 +09004129 p->prev_stime = max(p->prev_stime, cputime_sub(rtime, p->prev_utime));
Balbir Singh49048622008-09-05 18:12:23 +02004130
Hidetoshi Setod99ca3b2009-12-02 17:26:47 +09004131 *ut = p->prev_utime;
4132 *st = p->prev_stime;
Hidetoshi Setod180c5b2009-11-26 14:48:30 +09004133}
Balbir Singh49048622008-09-05 18:12:23 +02004134
Hidetoshi Seto0cf55e12009-12-02 17:28:07 +09004135/*
4136 * Must be called with siglock held.
4137 */
4138void thread_group_times(struct task_struct *p, cputime_t *ut, cputime_t *st)
4139{
4140 struct signal_struct *sig = p->signal;
4141 struct task_cputime cputime;
4142 cputime_t rtime, utime, total;
4143
4144 thread_group_cputime(p, &cputime);
4145
4146 total = cputime_add(cputime.utime, cputime.stime);
4147 rtime = nsecs_to_cputime(cputime.sum_exec_runtime);
4148
4149 if (total) {
Stanislaw Gruszkae75e8632010-09-14 16:35:14 +02004150 u64 temp = rtime;
Hidetoshi Seto0cf55e12009-12-02 17:28:07 +09004151
Stanislaw Gruszkae75e8632010-09-14 16:35:14 +02004152 temp *= cputime.utime;
Hidetoshi Seto0cf55e12009-12-02 17:28:07 +09004153 do_div(temp, total);
4154 utime = (cputime_t)temp;
4155 } else
4156 utime = rtime;
4157
4158 sig->prev_utime = max(sig->prev_utime, utime);
4159 sig->prev_stime = max(sig->prev_stime,
4160 cputime_sub(rtime, sig->prev_utime));
4161
4162 *ut = sig->prev_utime;
4163 *st = sig->prev_stime;
Balbir Singh49048622008-09-05 18:12:23 +02004164}
4165#endif
4166
Balbir Singh49048622008-09-05 18:12:23 +02004167/*
Christoph Lameter7835b982006-12-10 02:20:22 -08004168 * This function gets called by the timer code, with HZ frequency.
4169 * We call it with interrupts disabled.
Christoph Lameter7835b982006-12-10 02:20:22 -08004170 */
4171void scheduler_tick(void)
4172{
Christoph Lameter7835b982006-12-10 02:20:22 -08004173 int cpu = smp_processor_id();
4174 struct rq *rq = cpu_rq(cpu);
Ingo Molnardd41f592007-07-09 18:51:59 +02004175 struct task_struct *curr = rq->curr;
Peter Zijlstra3e51f332008-05-03 18:29:28 +02004176
4177 sched_clock_tick();
Christoph Lameter7835b982006-12-10 02:20:22 -08004178
Thomas Gleixner05fa7852009-11-17 14:28:38 +01004179 raw_spin_lock(&rq->lock);
Peter Zijlstra3e51f332008-05-03 18:29:28 +02004180 update_rq_clock(rq);
Venkatesh Pallipadifdf3e952010-05-17 18:14:43 -07004181 update_cpu_load_active(rq);
Peter Zijlstrafa85ae22008-01-25 21:08:29 +01004182 curr->sched_class->task_tick(rq, curr, 0);
Thomas Gleixner05fa7852009-11-17 14:28:38 +01004183 raw_spin_unlock(&rq->lock);
Ingo Molnardd41f592007-07-09 18:51:59 +02004184
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02004185 perf_event_task_tick();
Peter Zijlstrae220d2d2009-05-23 18:28:55 +02004186
Christoph Lametere418e1c2006-12-10 02:20:23 -08004187#ifdef CONFIG_SMP
Ingo Molnardd41f592007-07-09 18:51:59 +02004188 rq->idle_at_tick = idle_cpu(cpu);
4189 trigger_load_balance(rq, cpu);
Christoph Lametere418e1c2006-12-10 02:20:23 -08004190#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07004191}
4192
Lai Jiangshan132380a2009-04-02 14:18:25 +08004193notrace unsigned long get_parent_ip(unsigned long addr)
Steven Rostedt6cd8a4b2008-05-12 21:20:42 +02004194{
4195 if (in_lock_functions(addr)) {
4196 addr = CALLER_ADDR2;
4197 if (in_lock_functions(addr))
4198 addr = CALLER_ADDR3;
4199 }
4200 return addr;
4201}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004202
Steven Rostedt7e49fcc2009-01-22 19:01:40 -05004203#if defined(CONFIG_PREEMPT) && (defined(CONFIG_DEBUG_PREEMPT) || \
4204 defined(CONFIG_PREEMPT_TRACER))
4205
Srinivasa Ds43627582008-02-23 15:24:04 -08004206void __kprobes add_preempt_count(int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004207{
Steven Rostedt6cd8a4b2008-05-12 21:20:42 +02004208#ifdef CONFIG_DEBUG_PREEMPT
Linus Torvalds1da177e2005-04-16 15:20:36 -07004209 /*
4210 * Underflow?
4211 */
Ingo Molnar9a11b49a2006-07-03 00:24:33 -07004212 if (DEBUG_LOCKS_WARN_ON((preempt_count() < 0)))
4213 return;
Steven Rostedt6cd8a4b2008-05-12 21:20:42 +02004214#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07004215 preempt_count() += val;
Steven Rostedt6cd8a4b2008-05-12 21:20:42 +02004216#ifdef CONFIG_DEBUG_PREEMPT
Linus Torvalds1da177e2005-04-16 15:20:36 -07004217 /*
4218 * Spinlock count overflowing soon?
4219 */
Miguel Ojeda Sandonis33859f72006-12-10 02:20:38 -08004220 DEBUG_LOCKS_WARN_ON((preempt_count() & PREEMPT_MASK) >=
4221 PREEMPT_MASK - 10);
Steven Rostedt6cd8a4b2008-05-12 21:20:42 +02004222#endif
4223 if (preempt_count() == val)
4224 trace_preempt_off(CALLER_ADDR0, get_parent_ip(CALLER_ADDR1));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004225}
4226EXPORT_SYMBOL(add_preempt_count);
4227
Srinivasa Ds43627582008-02-23 15:24:04 -08004228void __kprobes sub_preempt_count(int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004229{
Steven Rostedt6cd8a4b2008-05-12 21:20:42 +02004230#ifdef CONFIG_DEBUG_PREEMPT
Linus Torvalds1da177e2005-04-16 15:20:36 -07004231 /*
4232 * Underflow?
4233 */
Ingo Molnar01e3eb82009-01-12 13:00:50 +01004234 if (DEBUG_LOCKS_WARN_ON(val > preempt_count()))
Ingo Molnar9a11b49a2006-07-03 00:24:33 -07004235 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004236 /*
4237 * Is the spinlock portion underflowing?
4238 */
Ingo Molnar9a11b49a2006-07-03 00:24:33 -07004239 if (DEBUG_LOCKS_WARN_ON((val < PREEMPT_MASK) &&
4240 !(preempt_count() & PREEMPT_MASK)))
4241 return;
Steven Rostedt6cd8a4b2008-05-12 21:20:42 +02004242#endif
Ingo Molnar9a11b49a2006-07-03 00:24:33 -07004243
Steven Rostedt6cd8a4b2008-05-12 21:20:42 +02004244 if (preempt_count() == val)
4245 trace_preempt_on(CALLER_ADDR0, get_parent_ip(CALLER_ADDR1));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004246 preempt_count() -= val;
4247}
4248EXPORT_SYMBOL(sub_preempt_count);
4249
4250#endif
4251
4252/*
Ingo Molnardd41f592007-07-09 18:51:59 +02004253 * Print scheduling while atomic bug:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004254 */
Ingo Molnardd41f592007-07-09 18:51:59 +02004255static noinline void __schedule_bug(struct task_struct *prev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004256{
Satyam Sharma838225b2007-10-24 18:23:50 +02004257 struct pt_regs *regs = get_irq_regs();
4258
Peter Zijlstra3df0fc52009-12-20 14:23:57 +01004259 printk(KERN_ERR "BUG: scheduling while atomic: %s/%d/0x%08x\n",
4260 prev->comm, prev->pid, preempt_count());
Satyam Sharma838225b2007-10-24 18:23:50 +02004261
Ingo Molnardd41f592007-07-09 18:51:59 +02004262 debug_show_held_locks(prev);
Arjan van de Vene21f5b12008-05-23 09:05:58 -07004263 print_modules();
Ingo Molnardd41f592007-07-09 18:51:59 +02004264 if (irqs_disabled())
4265 print_irqtrace_events(prev);
Satyam Sharma838225b2007-10-24 18:23:50 +02004266
4267 if (regs)
4268 show_regs(regs);
4269 else
4270 dump_stack();
Ingo Molnardd41f592007-07-09 18:51:59 +02004271}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004272
Ingo Molnardd41f592007-07-09 18:51:59 +02004273/*
4274 * Various schedule()-time debugging checks and statistics:
4275 */
4276static inline void schedule_debug(struct task_struct *prev)
4277{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004278 /*
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01004279 * Test if we are atomic. Since do_exit() needs to call into
Linus Torvalds1da177e2005-04-16 15:20:36 -07004280 * schedule() atomically, we ignore that path for now.
4281 * Otherwise, whine if we are scheduling when we should not be.
4282 */
Roel Kluin3f33a7c2008-05-13 23:44:11 +02004283 if (unlikely(in_atomic_preempt_off() && !prev->exit_state))
Ingo Molnardd41f592007-07-09 18:51:59 +02004284 __schedule_bug(prev);
4285
Linus Torvalds1da177e2005-04-16 15:20:36 -07004286 profile_hit(SCHED_PROFILING, __builtin_return_address(0));
4287
Ingo Molnar2d723762007-10-15 17:00:12 +02004288 schedstat_inc(this_rq(), sched_count);
Ingo Molnardd41f592007-07-09 18:51:59 +02004289}
4290
Peter Zijlstra6cecd082009-11-30 13:00:37 +01004291static void put_prev_task(struct rq *rq, struct task_struct *prev)
Mike Galbraithdf1c99d2009-03-10 19:08:11 +01004292{
Mike Galbraith61eadef2011-04-29 08:36:50 +02004293 if (prev->on_rq || rq->skip_clock_update < 0)
Mike Galbraitha64692a2010-03-11 17:16:20 +01004294 update_rq_clock(rq);
Peter Zijlstra6cecd082009-11-30 13:00:37 +01004295 prev->sched_class->put_prev_task(rq, prev);
Mike Galbraithdf1c99d2009-03-10 19:08:11 +01004296}
4297
Ingo Molnardd41f592007-07-09 18:51:59 +02004298/*
4299 * Pick up the highest-prio task:
4300 */
4301static inline struct task_struct *
Wang Chenb67802e2009-03-02 13:55:26 +08004302pick_next_task(struct rq *rq)
Ingo Molnardd41f592007-07-09 18:51:59 +02004303{
Ingo Molnar5522d5d2007-10-15 17:00:12 +02004304 const struct sched_class *class;
Ingo Molnardd41f592007-07-09 18:51:59 +02004305 struct task_struct *p;
4306
4307 /*
4308 * Optimization: we know that if all tasks are in
4309 * the fair class we can call that function directly:
4310 */
Paul Turner953bfcd2011-07-21 09:43:27 -07004311 if (likely(rq->nr_running == rq->cfs.h_nr_running)) {
Ingo Molnarfb8d4722007-08-09 11:16:48 +02004312 p = fair_sched_class.pick_next_task(rq);
Ingo Molnardd41f592007-07-09 18:51:59 +02004313 if (likely(p))
4314 return p;
4315 }
4316
Peter Zijlstra34f971f2010-09-22 13:53:15 +02004317 for_each_class(class) {
Ingo Molnarfb8d4722007-08-09 11:16:48 +02004318 p = class->pick_next_task(rq);
Ingo Molnardd41f592007-07-09 18:51:59 +02004319 if (p)
4320 return p;
Ingo Molnardd41f592007-07-09 18:51:59 +02004321 }
Peter Zijlstra34f971f2010-09-22 13:53:15 +02004322
4323 BUG(); /* the idle class will always have a runnable task */
Ingo Molnardd41f592007-07-09 18:51:59 +02004324}
4325
4326/*
4327 * schedule() is the main scheduler function.
4328 */
Peter Zijlstraff743342009-03-13 12:21:26 +01004329asmlinkage void __sched schedule(void)
Ingo Molnardd41f592007-07-09 18:51:59 +02004330{
4331 struct task_struct *prev, *next;
Harvey Harrison67ca7bd2008-02-15 09:56:36 -08004332 unsigned long *switch_count;
Ingo Molnardd41f592007-07-09 18:51:59 +02004333 struct rq *rq;
Peter Zijlstra31656512008-07-18 18:01:23 +02004334 int cpu;
Ingo Molnardd41f592007-07-09 18:51:59 +02004335
Peter Zijlstraff743342009-03-13 12:21:26 +01004336need_resched:
4337 preempt_disable();
Ingo Molnardd41f592007-07-09 18:51:59 +02004338 cpu = smp_processor_id();
4339 rq = cpu_rq(cpu);
Paul E. McKenney25502a62010-04-01 17:37:01 -07004340 rcu_note_context_switch(cpu);
Ingo Molnardd41f592007-07-09 18:51:59 +02004341 prev = rq->curr;
Ingo Molnardd41f592007-07-09 18:51:59 +02004342
Ingo Molnardd41f592007-07-09 18:51:59 +02004343 schedule_debug(prev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004344
Peter Zijlstra31656512008-07-18 18:01:23 +02004345 if (sched_feat(HRTICK))
Mike Galbraithf333fdc2008-05-12 21:20:55 +02004346 hrtick_clear(rq);
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01004347
Thomas Gleixner05fa7852009-11-17 14:28:38 +01004348 raw_spin_lock_irq(&rq->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004349
Oleg Nesterov246d86b2010-05-19 14:57:11 +02004350 switch_count = &prev->nivcsw;
Ingo Molnardd41f592007-07-09 18:51:59 +02004351 if (prev->state && !(preempt_count() & PREEMPT_ACTIVE)) {
Tejun Heo21aa9af2010-06-08 21:40:37 +02004352 if (unlikely(signal_pending_state(prev->state, prev))) {
Ingo Molnardd41f592007-07-09 18:51:59 +02004353 prev->state = TASK_RUNNING;
Tejun Heo21aa9af2010-06-08 21:40:37 +02004354 } else {
Peter Zijlstra2acca552011-04-05 17:23:50 +02004355 deactivate_task(rq, prev, DEQUEUE_SLEEP);
4356 prev->on_rq = 0;
4357
Tejun Heo21aa9af2010-06-08 21:40:37 +02004358 /*
Peter Zijlstra2acca552011-04-05 17:23:50 +02004359 * If a worker went to sleep, notify and ask workqueue
4360 * whether it wants to wake up a task to maintain
4361 * concurrency.
Tejun Heo21aa9af2010-06-08 21:40:37 +02004362 */
4363 if (prev->flags & PF_WQ_WORKER) {
4364 struct task_struct *to_wakeup;
4365
4366 to_wakeup = wq_worker_sleeping(prev, cpu);
4367 if (to_wakeup)
4368 try_to_wake_up_local(to_wakeup);
4369 }
Peter Zijlstrafd2f4412011-04-05 17:23:44 +02004370
Linus Torvalds6631e632011-04-13 08:08:20 -07004371 /*
Peter Zijlstra2acca552011-04-05 17:23:50 +02004372 * If we are going to sleep and we have plugged IO
4373 * queued, make sure to submit it to avoid deadlocks.
Linus Torvalds6631e632011-04-13 08:08:20 -07004374 */
4375 if (blk_needs_flush_plug(prev)) {
4376 raw_spin_unlock(&rq->lock);
Jens Axboea237c1c2011-04-16 13:27:55 +02004377 blk_schedule_flush_plug(prev);
Linus Torvalds6631e632011-04-13 08:08:20 -07004378 raw_spin_lock(&rq->lock);
4379 }
Tejun Heo21aa9af2010-06-08 21:40:37 +02004380 }
Ingo Molnardd41f592007-07-09 18:51:59 +02004381 switch_count = &prev->nvcsw;
4382 }
4383
Gregory Haskins3f029d32009-07-29 11:08:47 -04004384 pre_schedule(rq, prev);
Steven Rostedtf65eda42008-01-25 21:08:07 +01004385
Ingo Molnardd41f592007-07-09 18:51:59 +02004386 if (unlikely(!rq->nr_running))
4387 idle_balance(cpu, rq);
4388
Mike Galbraithdf1c99d2009-03-10 19:08:11 +01004389 put_prev_task(rq, prev);
Wang Chenb67802e2009-03-02 13:55:26 +08004390 next = pick_next_task(rq);
Mike Galbraithf26f9af2010-12-08 11:05:42 +01004391 clear_tsk_need_resched(prev);
4392 rq->skip_clock_update = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004393
Linus Torvalds1da177e2005-04-16 15:20:36 -07004394 if (likely(prev != next)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004395 rq->nr_switches++;
4396 rq->curr = next;
4397 ++*switch_count;
4398
Ingo Molnardd41f592007-07-09 18:51:59 +02004399 context_switch(rq, prev, next); /* unlocks the rq */
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01004400 /*
Oleg Nesterov246d86b2010-05-19 14:57:11 +02004401 * The context switch have flipped the stack from under us
4402 * and restored the local variables which were saved when
4403 * this task called schedule() in the past. prev == current
4404 * is still correct, but it can be moved to another cpu/rq.
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01004405 */
4406 cpu = smp_processor_id();
4407 rq = cpu_rq(cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004408 } else
Thomas Gleixner05fa7852009-11-17 14:28:38 +01004409 raw_spin_unlock_irq(&rq->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004410
Gregory Haskins3f029d32009-07-29 11:08:47 -04004411 post_schedule(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004412
Linus Torvalds1da177e2005-04-16 15:20:36 -07004413 preempt_enable_no_resched();
Peter Zijlstraff743342009-03-13 12:21:26 +01004414 if (need_resched())
Linus Torvalds1da177e2005-04-16 15:20:36 -07004415 goto need_resched;
4416}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004417EXPORT_SYMBOL(schedule);
4418
Frederic Weisbeckerc08f7822009-12-02 20:49:17 +01004419#ifdef CONFIG_MUTEX_SPIN_ON_OWNER
Peter Zijlstrac6eb3dd2011-04-05 17:23:41 +02004420
4421static inline bool owner_running(struct mutex *lock, struct task_struct *owner)
4422{
Peter Zijlstrac6eb3dd2011-04-05 17:23:41 +02004423 if (lock->owner != owner)
Thomas Gleixner307bf982011-06-10 15:08:55 +02004424 return false;
Peter Zijlstrac6eb3dd2011-04-05 17:23:41 +02004425
4426 /*
4427 * Ensure we emit the owner->on_cpu, dereference _after_ checking
4428 * lock->owner still matches owner, if that fails, owner might
4429 * point to free()d memory, if it still matches, the rcu_read_lock()
4430 * ensures the memory stays valid.
4431 */
4432 barrier();
4433
Thomas Gleixner307bf982011-06-10 15:08:55 +02004434 return owner->on_cpu;
Peter Zijlstrac6eb3dd2011-04-05 17:23:41 +02004435}
4436
Peter Zijlstra0d66bf62009-01-12 14:01:47 +01004437/*
4438 * Look out! "owner" is an entirely speculative pointer
4439 * access and not reliable.
4440 */
Peter Zijlstrac6eb3dd2011-04-05 17:23:41 +02004441int mutex_spin_on_owner(struct mutex *lock, struct task_struct *owner)
Peter Zijlstra0d66bf62009-01-12 14:01:47 +01004442{
Peter Zijlstra0d66bf62009-01-12 14:01:47 +01004443 if (!sched_feat(OWNER_SPIN))
4444 return 0;
4445
Thomas Gleixner307bf982011-06-10 15:08:55 +02004446 rcu_read_lock();
Peter Zijlstrac6eb3dd2011-04-05 17:23:41 +02004447 while (owner_running(lock, owner)) {
4448 if (need_resched())
Thomas Gleixner307bf982011-06-10 15:08:55 +02004449 break;
Peter Zijlstra0d66bf62009-01-12 14:01:47 +01004450
Gerald Schaefer335d7af2010-11-22 15:47:36 +01004451 arch_mutex_cpu_relax();
Peter Zijlstra0d66bf62009-01-12 14:01:47 +01004452 }
Thomas Gleixner307bf982011-06-10 15:08:55 +02004453 rcu_read_unlock();
Benjamin Herrenschmidt4b402212010-04-16 23:20:00 +02004454
Peter Zijlstrac6eb3dd2011-04-05 17:23:41 +02004455 /*
Thomas Gleixner307bf982011-06-10 15:08:55 +02004456 * We break out the loop above on need_resched() and when the
4457 * owner changed, which is a sign for heavy contention. Return
4458 * success only when lock->owner is NULL.
Peter Zijlstrac6eb3dd2011-04-05 17:23:41 +02004459 */
Thomas Gleixner307bf982011-06-10 15:08:55 +02004460 return lock->owner == NULL;
Peter Zijlstra0d66bf62009-01-12 14:01:47 +01004461}
4462#endif
4463
Linus Torvalds1da177e2005-04-16 15:20:36 -07004464#ifdef CONFIG_PREEMPT
4465/*
Andreas Mohr2ed6e342006-07-10 04:43:52 -07004466 * this is the entry point to schedule() from in-kernel preemption
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01004467 * off of preempt_enable. Kernel preemptions off return from interrupt
Linus Torvalds1da177e2005-04-16 15:20:36 -07004468 * occur there and call schedule directly.
4469 */
Steven Rostedtd1f74e22010-06-02 21:52:29 -04004470asmlinkage void __sched notrace preempt_schedule(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004471{
4472 struct thread_info *ti = current_thread_info();
Ingo Molnar6478d882008-01-25 21:08:33 +01004473
Linus Torvalds1da177e2005-04-16 15:20:36 -07004474 /*
4475 * If there is a non-zero preempt_count or interrupts are disabled,
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01004476 * we do not want to preempt the current task. Just return..
Linus Torvalds1da177e2005-04-16 15:20:36 -07004477 */
Nick Pigginbeed33a2006-10-11 01:21:52 -07004478 if (likely(ti->preempt_count || irqs_disabled()))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004479 return;
4480
Andi Kleen3a5c3592007-10-15 17:00:14 +02004481 do {
Steven Rostedtd1f74e22010-06-02 21:52:29 -04004482 add_preempt_count_notrace(PREEMPT_ACTIVE);
Andi Kleen3a5c3592007-10-15 17:00:14 +02004483 schedule();
Steven Rostedtd1f74e22010-06-02 21:52:29 -04004484 sub_preempt_count_notrace(PREEMPT_ACTIVE);
Andi Kleen3a5c3592007-10-15 17:00:14 +02004485
4486 /*
4487 * Check again in case we missed a preemption opportunity
4488 * between schedule and now.
4489 */
4490 barrier();
Lai Jiangshan5ed0cec2009-03-06 19:40:20 +08004491 } while (need_resched());
Linus Torvalds1da177e2005-04-16 15:20:36 -07004492}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004493EXPORT_SYMBOL(preempt_schedule);
4494
4495/*
Andreas Mohr2ed6e342006-07-10 04:43:52 -07004496 * this is the entry point to schedule() from kernel preemption
Linus Torvalds1da177e2005-04-16 15:20:36 -07004497 * off of irq context.
4498 * Note, that this is called and return with irqs disabled. This will
4499 * protect us against recursive calling from irq.
4500 */
4501asmlinkage void __sched preempt_schedule_irq(void)
4502{
4503 struct thread_info *ti = current_thread_info();
Ingo Molnar6478d882008-01-25 21:08:33 +01004504
Andreas Mohr2ed6e342006-07-10 04:43:52 -07004505 /* Catch callers which need to be fixed */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004506 BUG_ON(ti->preempt_count || !irqs_disabled());
4507
Andi Kleen3a5c3592007-10-15 17:00:14 +02004508 do {
4509 add_preempt_count(PREEMPT_ACTIVE);
Andi Kleen3a5c3592007-10-15 17:00:14 +02004510 local_irq_enable();
4511 schedule();
4512 local_irq_disable();
Andi Kleen3a5c3592007-10-15 17:00:14 +02004513 sub_preempt_count(PREEMPT_ACTIVE);
4514
4515 /*
4516 * Check again in case we missed a preemption opportunity
4517 * between schedule and now.
4518 */
4519 barrier();
Lai Jiangshan5ed0cec2009-03-06 19:40:20 +08004520 } while (need_resched());
Linus Torvalds1da177e2005-04-16 15:20:36 -07004521}
4522
4523#endif /* CONFIG_PREEMPT */
4524
Peter Zijlstra63859d42009-09-15 19:14:42 +02004525int default_wake_function(wait_queue_t *curr, unsigned mode, int wake_flags,
Ingo Molnar95cdf3b2005-09-10 00:26:11 -07004526 void *key)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004527{
Peter Zijlstra63859d42009-09-15 19:14:42 +02004528 return try_to_wake_up(curr->private, mode, wake_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004529}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004530EXPORT_SYMBOL(default_wake_function);
4531
4532/*
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01004533 * The core wakeup function. Non-exclusive wakeups (nr_exclusive == 0) just
4534 * wake everything up. If it's an exclusive wakeup (nr_exclusive == small +ve
Linus Torvalds1da177e2005-04-16 15:20:36 -07004535 * number) then we wake all the non-exclusive tasks and one exclusive task.
4536 *
4537 * There are circumstances in which we can try to wake a task which has already
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01004538 * started to run but is not in state TASK_RUNNING. try_to_wake_up() returns
Linus Torvalds1da177e2005-04-16 15:20:36 -07004539 * zero in this (rare) case, and we handle it by continuing to scan the queue.
4540 */
Johannes Weiner78ddb082009-04-14 16:53:05 +02004541static void __wake_up_common(wait_queue_head_t *q, unsigned int mode,
Peter Zijlstra63859d42009-09-15 19:14:42 +02004542 int nr_exclusive, int wake_flags, void *key)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004543{
Matthias Kaehlcke2e458742007-10-15 17:00:02 +02004544 wait_queue_t *curr, *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004545
Matthias Kaehlcke2e458742007-10-15 17:00:02 +02004546 list_for_each_entry_safe(curr, next, &q->task_list, task_list) {
Ingo Molnar48f24c42006-07-03 00:25:40 -07004547 unsigned flags = curr->flags;
4548
Peter Zijlstra63859d42009-09-15 19:14:42 +02004549 if (curr->func(curr, mode, wake_flags, key) &&
Ingo Molnar48f24c42006-07-03 00:25:40 -07004550 (flags & WQ_FLAG_EXCLUSIVE) && !--nr_exclusive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004551 break;
4552 }
4553}
4554
4555/**
4556 * __wake_up - wake up threads blocked on a waitqueue.
4557 * @q: the waitqueue
4558 * @mode: which threads
4559 * @nr_exclusive: how many wake-one or wake-many threads to wake up
Martin Waitz67be2dd2005-05-01 08:59:26 -07004560 * @key: is directly passed to the wakeup function
David Howells50fa6102009-04-28 15:01:38 +01004561 *
4562 * It may be assumed that this function implies a write memory barrier before
4563 * changing the task state if and only if any tasks are woken up.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004564 */
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -08004565void __wake_up(wait_queue_head_t *q, unsigned int mode,
Ingo Molnar95cdf3b2005-09-10 00:26:11 -07004566 int nr_exclusive, void *key)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004567{
4568 unsigned long flags;
4569
4570 spin_lock_irqsave(&q->lock, flags);
4571 __wake_up_common(q, mode, nr_exclusive, 0, key);
4572 spin_unlock_irqrestore(&q->lock, flags);
4573}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004574EXPORT_SYMBOL(__wake_up);
4575
4576/*
4577 * Same as __wake_up but called with the spinlock in wait_queue_head_t held.
4578 */
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -08004579void __wake_up_locked(wait_queue_head_t *q, unsigned int mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004580{
4581 __wake_up_common(q, mode, 1, 0, NULL);
4582}
Michal Nazarewicz22c43c82010-05-05 12:53:11 +02004583EXPORT_SYMBOL_GPL(__wake_up_locked);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004584
Davide Libenzi4ede8162009-03-31 15:24:20 -07004585void __wake_up_locked_key(wait_queue_head_t *q, unsigned int mode, void *key)
4586{
4587 __wake_up_common(q, mode, 1, 0, key);
4588}
Trond Myklebustbf294b42011-02-21 11:05:41 -08004589EXPORT_SYMBOL_GPL(__wake_up_locked_key);
Davide Libenzi4ede8162009-03-31 15:24:20 -07004590
Linus Torvalds1da177e2005-04-16 15:20:36 -07004591/**
Davide Libenzi4ede8162009-03-31 15:24:20 -07004592 * __wake_up_sync_key - wake up threads blocked on a waitqueue.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004593 * @q: the waitqueue
4594 * @mode: which threads
4595 * @nr_exclusive: how many wake-one or wake-many threads to wake up
Davide Libenzi4ede8162009-03-31 15:24:20 -07004596 * @key: opaque value to be passed to wakeup targets
Linus Torvalds1da177e2005-04-16 15:20:36 -07004597 *
4598 * The sync wakeup differs that the waker knows that it will schedule
4599 * away soon, so while the target thread will be woken up, it will not
4600 * be migrated to another CPU - ie. the two threads are 'synchronized'
4601 * with each other. This can prevent needless bouncing between CPUs.
4602 *
4603 * On UP it can prevent extra preemption.
David Howells50fa6102009-04-28 15:01:38 +01004604 *
4605 * It may be assumed that this function implies a write memory barrier before
4606 * changing the task state if and only if any tasks are woken up.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004607 */
Davide Libenzi4ede8162009-03-31 15:24:20 -07004608void __wake_up_sync_key(wait_queue_head_t *q, unsigned int mode,
4609 int nr_exclusive, void *key)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004610{
4611 unsigned long flags;
Peter Zijlstra7d478722009-09-14 19:55:44 +02004612 int wake_flags = WF_SYNC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004613
4614 if (unlikely(!q))
4615 return;
4616
4617 if (unlikely(!nr_exclusive))
Peter Zijlstra7d478722009-09-14 19:55:44 +02004618 wake_flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004619
4620 spin_lock_irqsave(&q->lock, flags);
Peter Zijlstra7d478722009-09-14 19:55:44 +02004621 __wake_up_common(q, mode, nr_exclusive, wake_flags, key);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004622 spin_unlock_irqrestore(&q->lock, flags);
4623}
Davide Libenzi4ede8162009-03-31 15:24:20 -07004624EXPORT_SYMBOL_GPL(__wake_up_sync_key);
4625
4626/*
4627 * __wake_up_sync - see __wake_up_sync_key()
4628 */
4629void __wake_up_sync(wait_queue_head_t *q, unsigned int mode, int nr_exclusive)
4630{
4631 __wake_up_sync_key(q, mode, nr_exclusive, NULL);
4632}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004633EXPORT_SYMBOL_GPL(__wake_up_sync); /* For internal use only */
4634
Kevin Diggs65eb3dc2008-08-26 10:26:54 +02004635/**
4636 * complete: - signals a single thread waiting on this completion
4637 * @x: holds the state of this particular completion
4638 *
4639 * This will wake up a single thread waiting on this completion. Threads will be
4640 * awakened in the same order in which they were queued.
4641 *
4642 * See also complete_all(), wait_for_completion() and related routines.
David Howells50fa6102009-04-28 15:01:38 +01004643 *
4644 * It may be assumed that this function implies a write memory barrier before
4645 * changing the task state if and only if any tasks are woken up.
Kevin Diggs65eb3dc2008-08-26 10:26:54 +02004646 */
Ingo Molnarb15136e2007-10-24 18:23:48 +02004647void complete(struct completion *x)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004648{
4649 unsigned long flags;
4650
4651 spin_lock_irqsave(&x->wait.lock, flags);
4652 x->done++;
Matthew Wilcoxd9514f62007-12-06 11:07:07 -05004653 __wake_up_common(&x->wait, TASK_NORMAL, 1, 0, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004654 spin_unlock_irqrestore(&x->wait.lock, flags);
4655}
4656EXPORT_SYMBOL(complete);
4657
Kevin Diggs65eb3dc2008-08-26 10:26:54 +02004658/**
4659 * complete_all: - signals all threads waiting on this completion
4660 * @x: holds the state of this particular completion
4661 *
4662 * This will wake up all threads waiting on this particular completion event.
David Howells50fa6102009-04-28 15:01:38 +01004663 *
4664 * It may be assumed that this function implies a write memory barrier before
4665 * changing the task state if and only if any tasks are woken up.
Kevin Diggs65eb3dc2008-08-26 10:26:54 +02004666 */
Ingo Molnarb15136e2007-10-24 18:23:48 +02004667void complete_all(struct completion *x)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004668{
4669 unsigned long flags;
4670
4671 spin_lock_irqsave(&x->wait.lock, flags);
4672 x->done += UINT_MAX/2;
Matthew Wilcoxd9514f62007-12-06 11:07:07 -05004673 __wake_up_common(&x->wait, TASK_NORMAL, 0, 0, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004674 spin_unlock_irqrestore(&x->wait.lock, flags);
4675}
4676EXPORT_SYMBOL(complete_all);
4677
Andi Kleen8cbbe862007-10-15 17:00:14 +02004678static inline long __sched
4679do_wait_for_common(struct completion *x, long timeout, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004680{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004681 if (!x->done) {
4682 DECLARE_WAITQUEUE(wait, current);
4683
Changli Gaoa93d2f12010-05-07 14:33:26 +08004684 __add_wait_queue_tail_exclusive(&x->wait, &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004685 do {
Oleg Nesterov94d3d822008-08-20 16:54:41 -07004686 if (signal_pending_state(state, current)) {
Oleg Nesterovea71a542008-06-20 18:32:20 +04004687 timeout = -ERESTARTSYS;
4688 break;
Andi Kleen8cbbe862007-10-15 17:00:14 +02004689 }
4690 __set_current_state(state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004691 spin_unlock_irq(&x->wait.lock);
Andi Kleen8cbbe862007-10-15 17:00:14 +02004692 timeout = schedule_timeout(timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004693 spin_lock_irq(&x->wait.lock);
Oleg Nesterovea71a542008-06-20 18:32:20 +04004694 } while (!x->done && timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004695 __remove_wait_queue(&x->wait, &wait);
Oleg Nesterovea71a542008-06-20 18:32:20 +04004696 if (!x->done)
4697 return timeout;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004698 }
4699 x->done--;
Oleg Nesterovea71a542008-06-20 18:32:20 +04004700 return timeout ?: 1;
Andi Kleen8cbbe862007-10-15 17:00:14 +02004701}
4702
4703static long __sched
4704wait_for_common(struct completion *x, long timeout, int state)
4705{
4706 might_sleep();
4707
4708 spin_lock_irq(&x->wait.lock);
4709 timeout = do_wait_for_common(x, timeout, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004710 spin_unlock_irq(&x->wait.lock);
Andi Kleen8cbbe862007-10-15 17:00:14 +02004711 return timeout;
4712}
4713
Kevin Diggs65eb3dc2008-08-26 10:26:54 +02004714/**
4715 * wait_for_completion: - waits for completion of a task
4716 * @x: holds the state of this particular completion
4717 *
4718 * This waits to be signaled for completion of a specific task. It is NOT
4719 * interruptible and there is no timeout.
4720 *
4721 * See also similar routines (i.e. wait_for_completion_timeout()) with timeout
4722 * and interrupt capability. Also see complete().
4723 */
Ingo Molnarb15136e2007-10-24 18:23:48 +02004724void __sched wait_for_completion(struct completion *x)
Andi Kleen8cbbe862007-10-15 17:00:14 +02004725{
4726 wait_for_common(x, MAX_SCHEDULE_TIMEOUT, TASK_UNINTERRUPTIBLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004727}
4728EXPORT_SYMBOL(wait_for_completion);
4729
Kevin Diggs65eb3dc2008-08-26 10:26:54 +02004730/**
4731 * wait_for_completion_timeout: - waits for completion of a task (w/timeout)
4732 * @x: holds the state of this particular completion
4733 * @timeout: timeout value in jiffies
4734 *
4735 * This waits for either a completion of a specific task to be signaled or for a
4736 * specified timeout to expire. The timeout is in jiffies. It is not
4737 * interruptible.
4738 */
Ingo Molnarb15136e2007-10-24 18:23:48 +02004739unsigned long __sched
Linus Torvalds1da177e2005-04-16 15:20:36 -07004740wait_for_completion_timeout(struct completion *x, unsigned long timeout)
4741{
Andi Kleen8cbbe862007-10-15 17:00:14 +02004742 return wait_for_common(x, timeout, TASK_UNINTERRUPTIBLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004743}
4744EXPORT_SYMBOL(wait_for_completion_timeout);
4745
Kevin Diggs65eb3dc2008-08-26 10:26:54 +02004746/**
4747 * wait_for_completion_interruptible: - waits for completion of a task (w/intr)
4748 * @x: holds the state of this particular completion
4749 *
4750 * This waits for completion of a specific task to be signaled. It is
4751 * interruptible.
4752 */
Andi Kleen8cbbe862007-10-15 17:00:14 +02004753int __sched wait_for_completion_interruptible(struct completion *x)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004754{
Andi Kleen51e97992007-10-18 21:32:55 +02004755 long t = wait_for_common(x, MAX_SCHEDULE_TIMEOUT, TASK_INTERRUPTIBLE);
4756 if (t == -ERESTARTSYS)
4757 return t;
4758 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004759}
4760EXPORT_SYMBOL(wait_for_completion_interruptible);
4761
Kevin Diggs65eb3dc2008-08-26 10:26:54 +02004762/**
4763 * wait_for_completion_interruptible_timeout: - waits for completion (w/(to,intr))
4764 * @x: holds the state of this particular completion
4765 * @timeout: timeout value in jiffies
4766 *
4767 * This waits for either a completion of a specific task to be signaled or for a
4768 * specified timeout to expire. It is interruptible. The timeout is in jiffies.
4769 */
NeilBrown6bf41232011-01-05 12:50:16 +11004770long __sched
Linus Torvalds1da177e2005-04-16 15:20:36 -07004771wait_for_completion_interruptible_timeout(struct completion *x,
4772 unsigned long timeout)
4773{
Andi Kleen8cbbe862007-10-15 17:00:14 +02004774 return wait_for_common(x, timeout, TASK_INTERRUPTIBLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004775}
4776EXPORT_SYMBOL(wait_for_completion_interruptible_timeout);
4777
Kevin Diggs65eb3dc2008-08-26 10:26:54 +02004778/**
4779 * wait_for_completion_killable: - waits for completion of a task (killable)
4780 * @x: holds the state of this particular completion
4781 *
4782 * This waits to be signaled for completion of a specific task. It can be
4783 * interrupted by a kill signal.
4784 */
Matthew Wilcox009e5772007-12-06 12:29:54 -05004785int __sched wait_for_completion_killable(struct completion *x)
4786{
4787 long t = wait_for_common(x, MAX_SCHEDULE_TIMEOUT, TASK_KILLABLE);
4788 if (t == -ERESTARTSYS)
4789 return t;
4790 return 0;
4791}
4792EXPORT_SYMBOL(wait_for_completion_killable);
4793
Dave Chinnerbe4de352008-08-15 00:40:44 -07004794/**
Sage Weil0aa12fb2010-05-29 09:12:30 -07004795 * wait_for_completion_killable_timeout: - waits for completion of a task (w/(to,killable))
4796 * @x: holds the state of this particular completion
4797 * @timeout: timeout value in jiffies
4798 *
4799 * This waits for either a completion of a specific task to be
4800 * signaled or for a specified timeout to expire. It can be
4801 * interrupted by a kill signal. The timeout is in jiffies.
4802 */
NeilBrown6bf41232011-01-05 12:50:16 +11004803long __sched
Sage Weil0aa12fb2010-05-29 09:12:30 -07004804wait_for_completion_killable_timeout(struct completion *x,
4805 unsigned long timeout)
4806{
4807 return wait_for_common(x, timeout, TASK_KILLABLE);
4808}
4809EXPORT_SYMBOL(wait_for_completion_killable_timeout);
4810
4811/**
Dave Chinnerbe4de352008-08-15 00:40:44 -07004812 * try_wait_for_completion - try to decrement a completion without blocking
4813 * @x: completion structure
4814 *
4815 * Returns: 0 if a decrement cannot be done without blocking
4816 * 1 if a decrement succeeded.
4817 *
4818 * If a completion is being used as a counting completion,
4819 * attempt to decrement the counter without blocking. This
4820 * enables us to avoid waiting if the resource the completion
4821 * is protecting is not available.
4822 */
4823bool try_wait_for_completion(struct completion *x)
4824{
Rafael J. Wysocki7539a3b2009-12-13 00:07:30 +01004825 unsigned long flags;
Dave Chinnerbe4de352008-08-15 00:40:44 -07004826 int ret = 1;
4827
Rafael J. Wysocki7539a3b2009-12-13 00:07:30 +01004828 spin_lock_irqsave(&x->wait.lock, flags);
Dave Chinnerbe4de352008-08-15 00:40:44 -07004829 if (!x->done)
4830 ret = 0;
4831 else
4832 x->done--;
Rafael J. Wysocki7539a3b2009-12-13 00:07:30 +01004833 spin_unlock_irqrestore(&x->wait.lock, flags);
Dave Chinnerbe4de352008-08-15 00:40:44 -07004834 return ret;
4835}
4836EXPORT_SYMBOL(try_wait_for_completion);
4837
4838/**
4839 * completion_done - Test to see if a completion has any waiters
4840 * @x: completion structure
4841 *
4842 * Returns: 0 if there are waiters (wait_for_completion() in progress)
4843 * 1 if there are no waiters.
4844 *
4845 */
4846bool completion_done(struct completion *x)
4847{
Rafael J. Wysocki7539a3b2009-12-13 00:07:30 +01004848 unsigned long flags;
Dave Chinnerbe4de352008-08-15 00:40:44 -07004849 int ret = 1;
4850
Rafael J. Wysocki7539a3b2009-12-13 00:07:30 +01004851 spin_lock_irqsave(&x->wait.lock, flags);
Dave Chinnerbe4de352008-08-15 00:40:44 -07004852 if (!x->done)
4853 ret = 0;
Rafael J. Wysocki7539a3b2009-12-13 00:07:30 +01004854 spin_unlock_irqrestore(&x->wait.lock, flags);
Dave Chinnerbe4de352008-08-15 00:40:44 -07004855 return ret;
4856}
4857EXPORT_SYMBOL(completion_done);
4858
Andi Kleen8cbbe862007-10-15 17:00:14 +02004859static long __sched
4860sleep_on_common(wait_queue_head_t *q, int state, long timeout)
Ingo Molnar0fec1712007-07-09 18:52:01 +02004861{
4862 unsigned long flags;
4863 wait_queue_t wait;
4864
4865 init_waitqueue_entry(&wait, current);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004866
Andi Kleen8cbbe862007-10-15 17:00:14 +02004867 __set_current_state(state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004868
Andi Kleen8cbbe862007-10-15 17:00:14 +02004869 spin_lock_irqsave(&q->lock, flags);
4870 __add_wait_queue(q, &wait);
4871 spin_unlock(&q->lock);
4872 timeout = schedule_timeout(timeout);
4873 spin_lock_irq(&q->lock);
4874 __remove_wait_queue(q, &wait);
4875 spin_unlock_irqrestore(&q->lock, flags);
4876
4877 return timeout;
4878}
4879
4880void __sched interruptible_sleep_on(wait_queue_head_t *q)
4881{
4882 sleep_on_common(q, TASK_INTERRUPTIBLE, MAX_SCHEDULE_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004883}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004884EXPORT_SYMBOL(interruptible_sleep_on);
4885
Ingo Molnar0fec1712007-07-09 18:52:01 +02004886long __sched
Ingo Molnar95cdf3b2005-09-10 00:26:11 -07004887interruptible_sleep_on_timeout(wait_queue_head_t *q, long timeout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004888{
Andi Kleen8cbbe862007-10-15 17:00:14 +02004889 return sleep_on_common(q, TASK_INTERRUPTIBLE, timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004890}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004891EXPORT_SYMBOL(interruptible_sleep_on_timeout);
4892
Ingo Molnar0fec1712007-07-09 18:52:01 +02004893void __sched sleep_on(wait_queue_head_t *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004894{
Andi Kleen8cbbe862007-10-15 17:00:14 +02004895 sleep_on_common(q, TASK_UNINTERRUPTIBLE, MAX_SCHEDULE_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004896}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004897EXPORT_SYMBOL(sleep_on);
4898
Ingo Molnar0fec1712007-07-09 18:52:01 +02004899long __sched sleep_on_timeout(wait_queue_head_t *q, long timeout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004900{
Andi Kleen8cbbe862007-10-15 17:00:14 +02004901 return sleep_on_common(q, TASK_UNINTERRUPTIBLE, timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004902}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004903EXPORT_SYMBOL(sleep_on_timeout);
4904
Ingo Molnarb29739f2006-06-27 02:54:51 -07004905#ifdef CONFIG_RT_MUTEXES
4906
4907/*
4908 * rt_mutex_setprio - set the current priority of a task
4909 * @p: task
4910 * @prio: prio value (kernel-internal form)
4911 *
4912 * This function changes the 'effective' priority of a task. It does
4913 * not touch ->normal_prio like __setscheduler().
4914 *
4915 * Used by the rt_mutex code to implement priority inheritance logic.
4916 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07004917void rt_mutex_setprio(struct task_struct *p, int prio)
Ingo Molnarb29739f2006-06-27 02:54:51 -07004918{
Srivatsa Vaddagiri83b699e2007-10-15 17:00:08 +02004919 int oldprio, on_rq, running;
Ingo Molnar70b97a72006-07-03 00:25:42 -07004920 struct rq *rq;
Thomas Gleixner83ab0aa2010-02-17 09:05:48 +01004921 const struct sched_class *prev_class;
Ingo Molnarb29739f2006-06-27 02:54:51 -07004922
4923 BUG_ON(prio < 0 || prio > MAX_PRIO);
4924
Peter Zijlstra0122ec52011-04-05 17:23:51 +02004925 rq = __task_rq_lock(p);
Ingo Molnarb29739f2006-06-27 02:54:51 -07004926
Steven Rostedta8027072010-09-20 15:13:34 -04004927 trace_sched_pi_setprio(p, prio);
Andrew Mortond5f9f942007-05-08 20:27:06 -07004928 oldprio = p->prio;
Thomas Gleixner83ab0aa2010-02-17 09:05:48 +01004929 prev_class = p->sched_class;
Peter Zijlstrafd2f4412011-04-05 17:23:44 +02004930 on_rq = p->on_rq;
Dmitry Adamushko051a1d12007-12-18 15:21:13 +01004931 running = task_current(rq, p);
Hiroshi Shimamoto0e1f3482008-03-10 11:01:20 -07004932 if (on_rq)
Ingo Molnar69be72c2007-08-09 11:16:49 +02004933 dequeue_task(rq, p, 0);
Hiroshi Shimamoto0e1f3482008-03-10 11:01:20 -07004934 if (running)
4935 p->sched_class->put_prev_task(rq, p);
Ingo Molnardd41f592007-07-09 18:51:59 +02004936
4937 if (rt_prio(prio))
4938 p->sched_class = &rt_sched_class;
4939 else
4940 p->sched_class = &fair_sched_class;
4941
Ingo Molnarb29739f2006-06-27 02:54:51 -07004942 p->prio = prio;
4943
Hiroshi Shimamoto0e1f3482008-03-10 11:01:20 -07004944 if (running)
4945 p->sched_class->set_curr_task(rq);
Peter Zijlstrada7a7352011-01-17 17:03:27 +01004946 if (on_rq)
Peter Zijlstra371fd7e2010-03-24 16:38:48 +01004947 enqueue_task(rq, p, oldprio < prio ? ENQUEUE_HEAD : 0);
Steven Rostedtcb469842008-01-25 21:08:22 +01004948
Peter Zijlstrada7a7352011-01-17 17:03:27 +01004949 check_class_changed(rq, p, prev_class, oldprio);
Peter Zijlstra0122ec52011-04-05 17:23:51 +02004950 __task_rq_unlock(rq);
Ingo Molnarb29739f2006-06-27 02:54:51 -07004951}
4952
4953#endif
4954
Ingo Molnar36c8b582006-07-03 00:25:41 -07004955void set_user_nice(struct task_struct *p, long nice)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004956{
Ingo Molnardd41f592007-07-09 18:51:59 +02004957 int old_prio, delta, on_rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004958 unsigned long flags;
Ingo Molnar70b97a72006-07-03 00:25:42 -07004959 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004960
4961 if (TASK_NICE(p) == nice || nice < -20 || nice > 19)
4962 return;
4963 /*
4964 * We have to be careful, if called from sys_setpriority(),
4965 * the task might be in the middle of scheduling on another CPU.
4966 */
4967 rq = task_rq_lock(p, &flags);
4968 /*
4969 * The RT priorities are set via sched_setscheduler(), but we still
4970 * allow the 'normal' nice value to be set - but as expected
4971 * it wont have any effect on scheduling until the task is
Ingo Molnardd41f592007-07-09 18:51:59 +02004972 * SCHED_FIFO/SCHED_RR:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004973 */
Ingo Molnare05606d2007-07-09 18:51:59 +02004974 if (task_has_rt_policy(p)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004975 p->static_prio = NICE_TO_PRIO(nice);
4976 goto out_unlock;
4977 }
Peter Zijlstrafd2f4412011-04-05 17:23:44 +02004978 on_rq = p->on_rq;
Peter Zijlstrac09595f2008-06-27 13:41:14 +02004979 if (on_rq)
Ingo Molnar69be72c2007-08-09 11:16:49 +02004980 dequeue_task(rq, p, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004981
Linus Torvalds1da177e2005-04-16 15:20:36 -07004982 p->static_prio = NICE_TO_PRIO(nice);
Peter Williams2dd73a42006-06-27 02:54:34 -07004983 set_load_weight(p);
Ingo Molnarb29739f2006-06-27 02:54:51 -07004984 old_prio = p->prio;
4985 p->prio = effective_prio(p);
4986 delta = p->prio - old_prio;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004987
Ingo Molnardd41f592007-07-09 18:51:59 +02004988 if (on_rq) {
Peter Zijlstra371fd7e2010-03-24 16:38:48 +01004989 enqueue_task(rq, p, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004990 /*
Andrew Mortond5f9f942007-05-08 20:27:06 -07004991 * If the task increased its priority or is running and
4992 * lowered its priority, then reschedule its CPU:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004993 */
Andrew Mortond5f9f942007-05-08 20:27:06 -07004994 if (delta < 0 || (delta > 0 && task_running(rq, p)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004995 resched_task(rq->curr);
4996 }
4997out_unlock:
Peter Zijlstra0122ec52011-04-05 17:23:51 +02004998 task_rq_unlock(rq, p, &flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004999}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005000EXPORT_SYMBOL(set_user_nice);
5001
Matt Mackalle43379f2005-05-01 08:59:00 -07005002/*
5003 * can_nice - check if a task can reduce its nice value
5004 * @p: task
5005 * @nice: nice value
5006 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07005007int can_nice(const struct task_struct *p, const int nice)
Matt Mackalle43379f2005-05-01 08:59:00 -07005008{
Matt Mackall024f4742005-08-18 11:24:19 -07005009 /* convert nice value [19,-20] to rlimit style value [1,40] */
5010 int nice_rlim = 20 - nice;
Ingo Molnar48f24c42006-07-03 00:25:40 -07005011
Jiri Slaby78d7d402010-03-05 13:42:54 -08005012 return (nice_rlim <= task_rlimit(p, RLIMIT_NICE) ||
Matt Mackalle43379f2005-05-01 08:59:00 -07005013 capable(CAP_SYS_NICE));
5014}
5015
Linus Torvalds1da177e2005-04-16 15:20:36 -07005016#ifdef __ARCH_WANT_SYS_NICE
5017
5018/*
5019 * sys_nice - change the priority of the current process.
5020 * @increment: priority increment
5021 *
5022 * sys_setpriority is a more generic, but much slower function that
5023 * does similar things.
5024 */
Heiko Carstens5add95d2009-01-14 14:14:08 +01005025SYSCALL_DEFINE1(nice, int, increment)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005026{
Ingo Molnar48f24c42006-07-03 00:25:40 -07005027 long nice, retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005028
5029 /*
5030 * Setpriority might change our priority at the same moment.
5031 * We don't have to worry. Conceptually one call occurs first
5032 * and we have a single winner.
5033 */
Matt Mackalle43379f2005-05-01 08:59:00 -07005034 if (increment < -40)
5035 increment = -40;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005036 if (increment > 40)
5037 increment = 40;
5038
Américo Wang2b8f8362009-02-16 18:54:21 +08005039 nice = TASK_NICE(current) + increment;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005040 if (nice < -20)
5041 nice = -20;
5042 if (nice > 19)
5043 nice = 19;
5044
Matt Mackalle43379f2005-05-01 08:59:00 -07005045 if (increment < 0 && !can_nice(current, nice))
5046 return -EPERM;
5047
Linus Torvalds1da177e2005-04-16 15:20:36 -07005048 retval = security_task_setnice(current, nice);
5049 if (retval)
5050 return retval;
5051
5052 set_user_nice(current, nice);
5053 return 0;
5054}
5055
5056#endif
5057
5058/**
5059 * task_prio - return the priority value of a given task.
5060 * @p: the task in question.
5061 *
5062 * This is the priority value as seen by users in /proc.
5063 * RT tasks are offset by -200. Normal tasks are centered
5064 * around 0, value goes from -16 to +15.
5065 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07005066int task_prio(const struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005067{
5068 return p->prio - MAX_RT_PRIO;
5069}
5070
5071/**
5072 * task_nice - return the nice value of a given task.
5073 * @p: the task in question.
5074 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07005075int task_nice(const struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005076{
5077 return TASK_NICE(p);
5078}
Pavel Roskin150d8be2008-03-05 16:56:37 -05005079EXPORT_SYMBOL(task_nice);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005080
5081/**
5082 * idle_cpu - is a given cpu idle currently?
5083 * @cpu: the processor in question.
5084 */
5085int idle_cpu(int cpu)
5086{
5087 return cpu_curr(cpu) == cpu_rq(cpu)->idle;
5088}
5089
Linus Torvalds1da177e2005-04-16 15:20:36 -07005090/**
5091 * idle_task - return the idle task for a given cpu.
5092 * @cpu: the processor in question.
5093 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07005094struct task_struct *idle_task(int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005095{
5096 return cpu_rq(cpu)->idle;
5097}
5098
5099/**
5100 * find_process_by_pid - find a process with a matching PID value.
5101 * @pid: the pid in question.
5102 */
Alexey Dobriyana9957442007-10-15 17:00:13 +02005103static struct task_struct *find_process_by_pid(pid_t pid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005104{
Pavel Emelyanov228ebcb2007-10-18 23:40:16 -07005105 return pid ? find_task_by_vpid(pid) : current;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005106}
5107
5108/* Actually do priority change: must hold rq lock. */
Ingo Molnardd41f592007-07-09 18:51:59 +02005109static void
5110__setscheduler(struct rq *rq, struct task_struct *p, int policy, int prio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005111{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005112 p->policy = policy;
5113 p->rt_priority = prio;
Ingo Molnarb29739f2006-06-27 02:54:51 -07005114 p->normal_prio = normal_prio(p);
5115 /* we are holding p->pi_lock already */
5116 p->prio = rt_mutex_getprio(p);
Peter Zijlstraffd44db2009-11-10 20:12:01 +01005117 if (rt_prio(p->prio))
5118 p->sched_class = &rt_sched_class;
5119 else
5120 p->sched_class = &fair_sched_class;
Peter Williams2dd73a42006-06-27 02:54:34 -07005121 set_load_weight(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005122}
5123
David Howellsc69e8d92008-11-14 10:39:19 +11005124/*
5125 * check the target process has a UID that matches the current process's
5126 */
5127static bool check_same_owner(struct task_struct *p)
5128{
5129 const struct cred *cred = current_cred(), *pcred;
5130 bool match;
5131
5132 rcu_read_lock();
5133 pcred = __task_cred(p);
Serge E. Hallynb0e77592011-03-23 16:43:24 -07005134 if (cred->user->user_ns == pcred->user->user_ns)
5135 match = (cred->euid == pcred->euid ||
5136 cred->euid == pcred->uid);
5137 else
5138 match = false;
David Howellsc69e8d92008-11-14 10:39:19 +11005139 rcu_read_unlock();
5140 return match;
5141}
5142
Rusty Russell961ccdd2008-06-23 13:55:38 +10005143static int __sched_setscheduler(struct task_struct *p, int policy,
KOSAKI Motohirofe7de492010-10-20 16:01:12 -07005144 const struct sched_param *param, bool user)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005145{
Srivatsa Vaddagiri83b699e2007-10-15 17:00:08 +02005146 int retval, oldprio, oldpolicy = -1, on_rq, running;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005147 unsigned long flags;
Thomas Gleixner83ab0aa2010-02-17 09:05:48 +01005148 const struct sched_class *prev_class;
Ingo Molnar70b97a72006-07-03 00:25:42 -07005149 struct rq *rq;
Lennart Poetteringca94c442009-06-15 17:17:47 +02005150 int reset_on_fork;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005151
Steven Rostedt66e53932006-06-27 02:54:44 -07005152 /* may grab non-irq protected spin_locks */
5153 BUG_ON(in_interrupt());
Linus Torvalds1da177e2005-04-16 15:20:36 -07005154recheck:
5155 /* double check policy once rq lock held */
Lennart Poetteringca94c442009-06-15 17:17:47 +02005156 if (policy < 0) {
5157 reset_on_fork = p->sched_reset_on_fork;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005158 policy = oldpolicy = p->policy;
Lennart Poetteringca94c442009-06-15 17:17:47 +02005159 } else {
5160 reset_on_fork = !!(policy & SCHED_RESET_ON_FORK);
5161 policy &= ~SCHED_RESET_ON_FORK;
5162
5163 if (policy != SCHED_FIFO && policy != SCHED_RR &&
5164 policy != SCHED_NORMAL && policy != SCHED_BATCH &&
5165 policy != SCHED_IDLE)
5166 return -EINVAL;
5167 }
5168
Linus Torvalds1da177e2005-04-16 15:20:36 -07005169 /*
5170 * Valid priorities for SCHED_FIFO and SCHED_RR are
Ingo Molnardd41f592007-07-09 18:51:59 +02005171 * 1..MAX_USER_RT_PRIO-1, valid priority for SCHED_NORMAL,
5172 * SCHED_BATCH and SCHED_IDLE is 0.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005173 */
5174 if (param->sched_priority < 0 ||
Ingo Molnar95cdf3b2005-09-10 00:26:11 -07005175 (p->mm && param->sched_priority > MAX_USER_RT_PRIO-1) ||
Steven Rostedtd46523e2005-07-25 16:28:39 -04005176 (!p->mm && param->sched_priority > MAX_RT_PRIO-1))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005177 return -EINVAL;
Ingo Molnare05606d2007-07-09 18:51:59 +02005178 if (rt_policy(policy) != (param->sched_priority != 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005179 return -EINVAL;
5180
Olivier Croquette37e4ab32005-06-25 14:57:32 -07005181 /*
5182 * Allow unprivileged RT tasks to decrease priority:
5183 */
Rusty Russell961ccdd2008-06-23 13:55:38 +10005184 if (user && !capable(CAP_SYS_NICE)) {
Ingo Molnare05606d2007-07-09 18:51:59 +02005185 if (rt_policy(policy)) {
Oleg Nesterova44702e2010-06-11 01:09:44 +02005186 unsigned long rlim_rtprio =
5187 task_rlimit(p, RLIMIT_RTPRIO);
Oleg Nesterov5fe1d752006-09-29 02:00:48 -07005188
Oleg Nesterov8dc3e902006-09-29 02:00:50 -07005189 /* can't set/change the rt policy */
5190 if (policy != p->policy && !rlim_rtprio)
5191 return -EPERM;
5192
5193 /* can't increase priority */
5194 if (param->sched_priority > p->rt_priority &&
5195 param->sched_priority > rlim_rtprio)
5196 return -EPERM;
5197 }
Darren Hartc02aa732011-02-17 15:37:07 -08005198
Ingo Molnardd41f592007-07-09 18:51:59 +02005199 /*
Darren Hartc02aa732011-02-17 15:37:07 -08005200 * Treat SCHED_IDLE as nice 20. Only allow a switch to
5201 * SCHED_NORMAL if the RLIMIT_NICE would normally permit it.
Ingo Molnardd41f592007-07-09 18:51:59 +02005202 */
Darren Hartc02aa732011-02-17 15:37:07 -08005203 if (p->policy == SCHED_IDLE && policy != SCHED_IDLE) {
5204 if (!can_nice(p, TASK_NICE(p)))
5205 return -EPERM;
5206 }
Oleg Nesterov8dc3e902006-09-29 02:00:50 -07005207
Olivier Croquette37e4ab32005-06-25 14:57:32 -07005208 /* can't change other user's priorities */
David Howellsc69e8d92008-11-14 10:39:19 +11005209 if (!check_same_owner(p))
Olivier Croquette37e4ab32005-06-25 14:57:32 -07005210 return -EPERM;
Lennart Poetteringca94c442009-06-15 17:17:47 +02005211
5212 /* Normal users shall not reset the sched_reset_on_fork flag */
5213 if (p->sched_reset_on_fork && !reset_on_fork)
5214 return -EPERM;
Olivier Croquette37e4ab32005-06-25 14:57:32 -07005215 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005216
Jeremy Fitzhardinge725aad22008-08-03 09:33:03 -07005217 if (user) {
KOSAKI Motohirob0ae1982010-10-15 04:21:18 +09005218 retval = security_task_setscheduler(p);
Jeremy Fitzhardinge725aad22008-08-03 09:33:03 -07005219 if (retval)
5220 return retval;
5221 }
5222
Linus Torvalds1da177e2005-04-16 15:20:36 -07005223 /*
Ingo Molnarb29739f2006-06-27 02:54:51 -07005224 * make sure no PI-waiters arrive (or leave) while we are
5225 * changing the priority of the task:
Peter Zijlstra0122ec52011-04-05 17:23:51 +02005226 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -03005227 * To be able to change p->policy safely, the appropriate
Linus Torvalds1da177e2005-04-16 15:20:36 -07005228 * runqueue lock must be held.
5229 */
Peter Zijlstra0122ec52011-04-05 17:23:51 +02005230 rq = task_rq_lock(p, &flags);
Peter Zijlstradc61b1d2010-06-08 11:40:42 +02005231
Peter Zijlstra34f971f2010-09-22 13:53:15 +02005232 /*
5233 * Changing the policy of the stop threads its a very bad idea
5234 */
5235 if (p == rq->stop) {
Peter Zijlstra0122ec52011-04-05 17:23:51 +02005236 task_rq_unlock(rq, p, &flags);
Peter Zijlstra34f971f2010-09-22 13:53:15 +02005237 return -EINVAL;
5238 }
5239
Dario Faggiolia51e9192011-03-24 14:00:18 +01005240 /*
5241 * If not changing anything there's no need to proceed further:
5242 */
5243 if (unlikely(policy == p->policy && (!rt_policy(policy) ||
5244 param->sched_priority == p->rt_priority))) {
5245
5246 __task_rq_unlock(rq);
5247 raw_spin_unlock_irqrestore(&p->pi_lock, flags);
5248 return 0;
5249 }
5250
Peter Zijlstradc61b1d2010-06-08 11:40:42 +02005251#ifdef CONFIG_RT_GROUP_SCHED
5252 if (user) {
5253 /*
5254 * Do not allow realtime tasks into groups that have no runtime
5255 * assigned.
5256 */
5257 if (rt_bandwidth_enabled() && rt_policy(policy) &&
Mike Galbraithf4493772011-01-13 04:54:50 +01005258 task_group(p)->rt_bandwidth.rt_runtime == 0 &&
5259 !task_group_is_autogroup(task_group(p))) {
Peter Zijlstra0122ec52011-04-05 17:23:51 +02005260 task_rq_unlock(rq, p, &flags);
Peter Zijlstradc61b1d2010-06-08 11:40:42 +02005261 return -EPERM;
5262 }
5263 }
5264#endif
5265
Linus Torvalds1da177e2005-04-16 15:20:36 -07005266 /* recheck policy now with rq lock held */
5267 if (unlikely(oldpolicy != -1 && oldpolicy != p->policy)) {
5268 policy = oldpolicy = -1;
Peter Zijlstra0122ec52011-04-05 17:23:51 +02005269 task_rq_unlock(rq, p, &flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005270 goto recheck;
5271 }
Peter Zijlstrafd2f4412011-04-05 17:23:44 +02005272 on_rq = p->on_rq;
Dmitry Adamushko051a1d12007-12-18 15:21:13 +01005273 running = task_current(rq, p);
Hiroshi Shimamoto0e1f3482008-03-10 11:01:20 -07005274 if (on_rq)
Ingo Molnar2e1cb742007-08-09 11:16:49 +02005275 deactivate_task(rq, p, 0);
Hiroshi Shimamoto0e1f3482008-03-10 11:01:20 -07005276 if (running)
5277 p->sched_class->put_prev_task(rq, p);
Dmitry Adamushkof6b53202007-10-15 17:00:08 +02005278
Lennart Poetteringca94c442009-06-15 17:17:47 +02005279 p->sched_reset_on_fork = reset_on_fork;
5280
Linus Torvalds1da177e2005-04-16 15:20:36 -07005281 oldprio = p->prio;
Thomas Gleixner83ab0aa2010-02-17 09:05:48 +01005282 prev_class = p->sched_class;
Ingo Molnardd41f592007-07-09 18:51:59 +02005283 __setscheduler(rq, p, policy, param->sched_priority);
Dmitry Adamushkof6b53202007-10-15 17:00:08 +02005284
Hiroshi Shimamoto0e1f3482008-03-10 11:01:20 -07005285 if (running)
5286 p->sched_class->set_curr_task(rq);
Peter Zijlstrada7a7352011-01-17 17:03:27 +01005287 if (on_rq)
Ingo Molnardd41f592007-07-09 18:51:59 +02005288 activate_task(rq, p, 0);
Steven Rostedtcb469842008-01-25 21:08:22 +01005289
Peter Zijlstrada7a7352011-01-17 17:03:27 +01005290 check_class_changed(rq, p, prev_class, oldprio);
Peter Zijlstra0122ec52011-04-05 17:23:51 +02005291 task_rq_unlock(rq, p, &flags);
Ingo Molnarb29739f2006-06-27 02:54:51 -07005292
Thomas Gleixner95e02ca2006-06-27 02:55:02 -07005293 rt_mutex_adjust_pi(p);
5294
Linus Torvalds1da177e2005-04-16 15:20:36 -07005295 return 0;
5296}
Rusty Russell961ccdd2008-06-23 13:55:38 +10005297
5298/**
5299 * sched_setscheduler - change the scheduling policy and/or RT priority of a thread.
5300 * @p: the task in question.
5301 * @policy: new policy.
5302 * @param: structure containing the new RT priority.
5303 *
5304 * NOTE that the task may be already dead.
5305 */
5306int sched_setscheduler(struct task_struct *p, int policy,
KOSAKI Motohirofe7de492010-10-20 16:01:12 -07005307 const struct sched_param *param)
Rusty Russell961ccdd2008-06-23 13:55:38 +10005308{
5309 return __sched_setscheduler(p, policy, param, true);
5310}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005311EXPORT_SYMBOL_GPL(sched_setscheduler);
5312
Rusty Russell961ccdd2008-06-23 13:55:38 +10005313/**
5314 * sched_setscheduler_nocheck - change the scheduling policy and/or RT priority of a thread from kernelspace.
5315 * @p: the task in question.
5316 * @policy: new policy.
5317 * @param: structure containing the new RT priority.
5318 *
5319 * Just like sched_setscheduler, only don't bother checking if the
5320 * current context has permission. For example, this is needed in
5321 * stop_machine(): we create temporary high priority worker threads,
5322 * but our caller might not have that capability.
5323 */
5324int sched_setscheduler_nocheck(struct task_struct *p, int policy,
KOSAKI Motohirofe7de492010-10-20 16:01:12 -07005325 const struct sched_param *param)
Rusty Russell961ccdd2008-06-23 13:55:38 +10005326{
5327 return __sched_setscheduler(p, policy, param, false);
5328}
5329
Ingo Molnar95cdf3b2005-09-10 00:26:11 -07005330static int
5331do_sched_setscheduler(pid_t pid, int policy, struct sched_param __user *param)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005332{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005333 struct sched_param lparam;
5334 struct task_struct *p;
Ingo Molnar36c8b582006-07-03 00:25:41 -07005335 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005336
5337 if (!param || pid < 0)
5338 return -EINVAL;
5339 if (copy_from_user(&lparam, param, sizeof(struct sched_param)))
5340 return -EFAULT;
Oleg Nesterov5fe1d752006-09-29 02:00:48 -07005341
5342 rcu_read_lock();
5343 retval = -ESRCH;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005344 p = find_process_by_pid(pid);
Oleg Nesterov5fe1d752006-09-29 02:00:48 -07005345 if (p != NULL)
5346 retval = sched_setscheduler(p, policy, &lparam);
5347 rcu_read_unlock();
Ingo Molnar36c8b582006-07-03 00:25:41 -07005348
Linus Torvalds1da177e2005-04-16 15:20:36 -07005349 return retval;
5350}
5351
5352/**
5353 * sys_sched_setscheduler - set/change the scheduler policy and RT priority
5354 * @pid: the pid in question.
5355 * @policy: new policy.
5356 * @param: structure containing the new RT priority.
5357 */
Heiko Carstens5add95d2009-01-14 14:14:08 +01005358SYSCALL_DEFINE3(sched_setscheduler, pid_t, pid, int, policy,
5359 struct sched_param __user *, param)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005360{
Jason Baronc21761f2006-01-18 17:43:03 -08005361 /* negative values for policy are not valid */
5362 if (policy < 0)
5363 return -EINVAL;
5364
Linus Torvalds1da177e2005-04-16 15:20:36 -07005365 return do_sched_setscheduler(pid, policy, param);
5366}
5367
5368/**
5369 * sys_sched_setparam - set/change the RT priority of a thread
5370 * @pid: the pid in question.
5371 * @param: structure containing the new RT priority.
5372 */
Heiko Carstens5add95d2009-01-14 14:14:08 +01005373SYSCALL_DEFINE2(sched_setparam, pid_t, pid, struct sched_param __user *, param)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005374{
5375 return do_sched_setscheduler(pid, -1, param);
5376}
5377
5378/**
5379 * sys_sched_getscheduler - get the policy (scheduling class) of a thread
5380 * @pid: the pid in question.
5381 */
Heiko Carstens5add95d2009-01-14 14:14:08 +01005382SYSCALL_DEFINE1(sched_getscheduler, pid_t, pid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005383{
Ingo Molnar36c8b582006-07-03 00:25:41 -07005384 struct task_struct *p;
Andi Kleen3a5c3592007-10-15 17:00:14 +02005385 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005386
5387 if (pid < 0)
Andi Kleen3a5c3592007-10-15 17:00:14 +02005388 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005389
5390 retval = -ESRCH;
Thomas Gleixner5fe85be2009-12-09 10:14:58 +00005391 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005392 p = find_process_by_pid(pid);
5393 if (p) {
5394 retval = security_task_getscheduler(p);
5395 if (!retval)
Lennart Poetteringca94c442009-06-15 17:17:47 +02005396 retval = p->policy
5397 | (p->sched_reset_on_fork ? SCHED_RESET_ON_FORK : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005398 }
Thomas Gleixner5fe85be2009-12-09 10:14:58 +00005399 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005400 return retval;
5401}
5402
5403/**
Lennart Poetteringca94c442009-06-15 17:17:47 +02005404 * sys_sched_getparam - get the RT priority of a thread
Linus Torvalds1da177e2005-04-16 15:20:36 -07005405 * @pid: the pid in question.
5406 * @param: structure containing the RT priority.
5407 */
Heiko Carstens5add95d2009-01-14 14:14:08 +01005408SYSCALL_DEFINE2(sched_getparam, pid_t, pid, struct sched_param __user *, param)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005409{
5410 struct sched_param lp;
Ingo Molnar36c8b582006-07-03 00:25:41 -07005411 struct task_struct *p;
Andi Kleen3a5c3592007-10-15 17:00:14 +02005412 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005413
5414 if (!param || pid < 0)
Andi Kleen3a5c3592007-10-15 17:00:14 +02005415 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005416
Thomas Gleixner5fe85be2009-12-09 10:14:58 +00005417 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005418 p = find_process_by_pid(pid);
5419 retval = -ESRCH;
5420 if (!p)
5421 goto out_unlock;
5422
5423 retval = security_task_getscheduler(p);
5424 if (retval)
5425 goto out_unlock;
5426
5427 lp.sched_priority = p->rt_priority;
Thomas Gleixner5fe85be2009-12-09 10:14:58 +00005428 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005429
5430 /*
5431 * This one might sleep, we cannot do it with a spinlock held ...
5432 */
5433 retval = copy_to_user(param, &lp, sizeof(*param)) ? -EFAULT : 0;
5434
Linus Torvalds1da177e2005-04-16 15:20:36 -07005435 return retval;
5436
5437out_unlock:
Thomas Gleixner5fe85be2009-12-09 10:14:58 +00005438 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005439 return retval;
5440}
5441
Rusty Russell96f874e2008-11-25 02:35:14 +10305442long sched_setaffinity(pid_t pid, const struct cpumask *in_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005443{
Rusty Russell5a16f3d2008-11-25 02:35:11 +10305444 cpumask_var_t cpus_allowed, new_mask;
Ingo Molnar36c8b582006-07-03 00:25:41 -07005445 struct task_struct *p;
5446 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005447
Gautham R Shenoy95402b32008-01-25 21:08:02 +01005448 get_online_cpus();
Thomas Gleixner23f5d142009-12-09 10:15:01 +00005449 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005450
5451 p = find_process_by_pid(pid);
5452 if (!p) {
Thomas Gleixner23f5d142009-12-09 10:15:01 +00005453 rcu_read_unlock();
Gautham R Shenoy95402b32008-01-25 21:08:02 +01005454 put_online_cpus();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005455 return -ESRCH;
5456 }
5457
Thomas Gleixner23f5d142009-12-09 10:15:01 +00005458 /* Prevent p going away */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005459 get_task_struct(p);
Thomas Gleixner23f5d142009-12-09 10:15:01 +00005460 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005461
Rusty Russell5a16f3d2008-11-25 02:35:11 +10305462 if (!alloc_cpumask_var(&cpus_allowed, GFP_KERNEL)) {
5463 retval = -ENOMEM;
5464 goto out_put_task;
5465 }
5466 if (!alloc_cpumask_var(&new_mask, GFP_KERNEL)) {
5467 retval = -ENOMEM;
5468 goto out_free_cpus_allowed;
5469 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005470 retval = -EPERM;
Serge E. Hallynb0e77592011-03-23 16:43:24 -07005471 if (!check_same_owner(p) && !task_ns_capable(p, CAP_SYS_NICE))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005472 goto out_unlock;
5473
KOSAKI Motohirob0ae1982010-10-15 04:21:18 +09005474 retval = security_task_setscheduler(p);
David Quigleye7834f82006-06-23 02:03:59 -07005475 if (retval)
5476 goto out_unlock;
5477
Rusty Russell5a16f3d2008-11-25 02:35:11 +10305478 cpuset_cpus_allowed(p, cpus_allowed);
5479 cpumask_and(new_mask, in_mask, cpus_allowed);
Peter Zijlstra49246272010-10-17 21:46:10 +02005480again:
Rusty Russell5a16f3d2008-11-25 02:35:11 +10305481 retval = set_cpus_allowed_ptr(p, new_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005482
Paul Menage8707d8b2007-10-18 23:40:22 -07005483 if (!retval) {
Rusty Russell5a16f3d2008-11-25 02:35:11 +10305484 cpuset_cpus_allowed(p, cpus_allowed);
5485 if (!cpumask_subset(new_mask, cpus_allowed)) {
Paul Menage8707d8b2007-10-18 23:40:22 -07005486 /*
5487 * We must have raced with a concurrent cpuset
5488 * update. Just reset the cpus_allowed to the
5489 * cpuset's cpus_allowed
5490 */
Rusty Russell5a16f3d2008-11-25 02:35:11 +10305491 cpumask_copy(new_mask, cpus_allowed);
Paul Menage8707d8b2007-10-18 23:40:22 -07005492 goto again;
5493 }
5494 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005495out_unlock:
Rusty Russell5a16f3d2008-11-25 02:35:11 +10305496 free_cpumask_var(new_mask);
5497out_free_cpus_allowed:
5498 free_cpumask_var(cpus_allowed);
5499out_put_task:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005500 put_task_struct(p);
Gautham R Shenoy95402b32008-01-25 21:08:02 +01005501 put_online_cpus();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005502 return retval;
5503}
5504
5505static int get_user_cpu_mask(unsigned long __user *user_mask_ptr, unsigned len,
Rusty Russell96f874e2008-11-25 02:35:14 +10305506 struct cpumask *new_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005507{
Rusty Russell96f874e2008-11-25 02:35:14 +10305508 if (len < cpumask_size())
5509 cpumask_clear(new_mask);
5510 else if (len > cpumask_size())
5511 len = cpumask_size();
5512
Linus Torvalds1da177e2005-04-16 15:20:36 -07005513 return copy_from_user(new_mask, user_mask_ptr, len) ? -EFAULT : 0;
5514}
5515
5516/**
5517 * sys_sched_setaffinity - set the cpu affinity of a process
5518 * @pid: pid of the process
5519 * @len: length in bytes of the bitmask pointed to by user_mask_ptr
5520 * @user_mask_ptr: user-space pointer to the new cpu mask
5521 */
Heiko Carstens5add95d2009-01-14 14:14:08 +01005522SYSCALL_DEFINE3(sched_setaffinity, pid_t, pid, unsigned int, len,
5523 unsigned long __user *, user_mask_ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005524{
Rusty Russell5a16f3d2008-11-25 02:35:11 +10305525 cpumask_var_t new_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005526 int retval;
5527
Rusty Russell5a16f3d2008-11-25 02:35:11 +10305528 if (!alloc_cpumask_var(&new_mask, GFP_KERNEL))
5529 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005530
Rusty Russell5a16f3d2008-11-25 02:35:11 +10305531 retval = get_user_cpu_mask(user_mask_ptr, len, new_mask);
5532 if (retval == 0)
5533 retval = sched_setaffinity(pid, new_mask);
5534 free_cpumask_var(new_mask);
5535 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005536}
5537
Rusty Russell96f874e2008-11-25 02:35:14 +10305538long sched_getaffinity(pid_t pid, struct cpumask *mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005539{
Ingo Molnar36c8b582006-07-03 00:25:41 -07005540 struct task_struct *p;
Thomas Gleixner31605682009-12-08 20:24:16 +00005541 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005542 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005543
Gautham R Shenoy95402b32008-01-25 21:08:02 +01005544 get_online_cpus();
Thomas Gleixner23f5d142009-12-09 10:15:01 +00005545 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005546
5547 retval = -ESRCH;
5548 p = find_process_by_pid(pid);
5549 if (!p)
5550 goto out_unlock;
5551
David Quigleye7834f82006-06-23 02:03:59 -07005552 retval = security_task_getscheduler(p);
5553 if (retval)
5554 goto out_unlock;
5555
Peter Zijlstra013fdb82011-04-05 17:23:45 +02005556 raw_spin_lock_irqsave(&p->pi_lock, flags);
Rusty Russell96f874e2008-11-25 02:35:14 +10305557 cpumask_and(mask, &p->cpus_allowed, cpu_online_mask);
Peter Zijlstra013fdb82011-04-05 17:23:45 +02005558 raw_spin_unlock_irqrestore(&p->pi_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005559
5560out_unlock:
Thomas Gleixner23f5d142009-12-09 10:15:01 +00005561 rcu_read_unlock();
Gautham R Shenoy95402b32008-01-25 21:08:02 +01005562 put_online_cpus();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005563
Ulrich Drepper9531b622007-08-09 11:16:46 +02005564 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005565}
5566
5567/**
5568 * sys_sched_getaffinity - get the cpu affinity of a process
5569 * @pid: pid of the process
5570 * @len: length in bytes of the bitmask pointed to by user_mask_ptr
5571 * @user_mask_ptr: user-space pointer to hold the current cpu mask
5572 */
Heiko Carstens5add95d2009-01-14 14:14:08 +01005573SYSCALL_DEFINE3(sched_getaffinity, pid_t, pid, unsigned int, len,
5574 unsigned long __user *, user_mask_ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005575{
5576 int ret;
Rusty Russellf17c8602008-11-25 02:35:11 +10305577 cpumask_var_t mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005578
Anton Blanchard84fba5e2010-04-06 17:02:19 +10005579 if ((len * BITS_PER_BYTE) < nr_cpu_ids)
KOSAKI Motohirocd3d8032010-03-12 16:15:36 +09005580 return -EINVAL;
5581 if (len & (sizeof(unsigned long)-1))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005582 return -EINVAL;
5583
Rusty Russellf17c8602008-11-25 02:35:11 +10305584 if (!alloc_cpumask_var(&mask, GFP_KERNEL))
5585 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005586
Rusty Russellf17c8602008-11-25 02:35:11 +10305587 ret = sched_getaffinity(pid, mask);
5588 if (ret == 0) {
KOSAKI Motohiro8bc037f2010-03-17 09:36:58 +09005589 size_t retlen = min_t(size_t, len, cpumask_size());
KOSAKI Motohirocd3d8032010-03-12 16:15:36 +09005590
5591 if (copy_to_user(user_mask_ptr, mask, retlen))
Rusty Russellf17c8602008-11-25 02:35:11 +10305592 ret = -EFAULT;
5593 else
KOSAKI Motohirocd3d8032010-03-12 16:15:36 +09005594 ret = retlen;
Rusty Russellf17c8602008-11-25 02:35:11 +10305595 }
5596 free_cpumask_var(mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005597
Rusty Russellf17c8602008-11-25 02:35:11 +10305598 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005599}
5600
5601/**
5602 * sys_sched_yield - yield the current processor to other threads.
5603 *
Ingo Molnardd41f592007-07-09 18:51:59 +02005604 * This function yields the current CPU to other tasks. If there are no
5605 * other threads running on this CPU then this function will return.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005606 */
Heiko Carstens5add95d2009-01-14 14:14:08 +01005607SYSCALL_DEFINE0(sched_yield)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005608{
Ingo Molnar70b97a72006-07-03 00:25:42 -07005609 struct rq *rq = this_rq_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005610
Ingo Molnar2d723762007-10-15 17:00:12 +02005611 schedstat_inc(rq, yld_count);
Dmitry Adamushko4530d7a2007-10-15 17:00:08 +02005612 current->sched_class->yield_task(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005613
5614 /*
5615 * Since we are going to call schedule() anyway, there's
5616 * no need to preempt or enable interrupts:
5617 */
5618 __release(rq->lock);
Ingo Molnar8a25d5d2006-07-03 00:24:54 -07005619 spin_release(&rq->lock.dep_map, 1, _THIS_IP_);
Thomas Gleixner9828ea92009-12-03 20:55:53 +01005620 do_raw_spin_unlock(&rq->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005621 preempt_enable_no_resched();
5622
5623 schedule();
5624
5625 return 0;
5626}
5627
Peter Zijlstrad86ee482009-07-10 14:57:57 +02005628static inline int should_resched(void)
5629{
5630 return need_resched() && !(preempt_count() & PREEMPT_ACTIVE);
5631}
5632
Andrew Mortone7b38402006-06-30 01:56:00 -07005633static void __cond_resched(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005634{
Frederic Weisbeckere7aaaa62009-07-16 15:44:29 +02005635 add_preempt_count(PREEMPT_ACTIVE);
5636 schedule();
5637 sub_preempt_count(PREEMPT_ACTIVE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005638}
5639
Herbert Xu02b67cc2008-01-25 21:08:28 +01005640int __sched _cond_resched(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005641{
Peter Zijlstrad86ee482009-07-10 14:57:57 +02005642 if (should_resched()) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005643 __cond_resched();
5644 return 1;
5645 }
5646 return 0;
5647}
Herbert Xu02b67cc2008-01-25 21:08:28 +01005648EXPORT_SYMBOL(_cond_resched);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005649
5650/*
Frederic Weisbecker613afbf2009-07-16 15:44:29 +02005651 * __cond_resched_lock() - if a reschedule is pending, drop the given lock,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005652 * call schedule, and on return reacquire the lock.
5653 *
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01005654 * This works OK both with and without CONFIG_PREEMPT. We do strange low-level
Linus Torvalds1da177e2005-04-16 15:20:36 -07005655 * operations here to prevent schedule() from being called twice (once via
5656 * spin_unlock(), once by hand).
5657 */
Frederic Weisbecker613afbf2009-07-16 15:44:29 +02005658int __cond_resched_lock(spinlock_t *lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005659{
Peter Zijlstrad86ee482009-07-10 14:57:57 +02005660 int resched = should_resched();
Jan Kara6df3cec2005-06-13 15:52:32 -07005661 int ret = 0;
5662
Peter Zijlstraf607c662009-07-20 19:16:29 +02005663 lockdep_assert_held(lock);
5664
Nick Piggin95c354f2008-01-30 13:31:20 +01005665 if (spin_needbreak(lock) || resched) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005666 spin_unlock(lock);
Peter Zijlstrad86ee482009-07-10 14:57:57 +02005667 if (resched)
Nick Piggin95c354f2008-01-30 13:31:20 +01005668 __cond_resched();
5669 else
5670 cpu_relax();
Jan Kara6df3cec2005-06-13 15:52:32 -07005671 ret = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005672 spin_lock(lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005673 }
Jan Kara6df3cec2005-06-13 15:52:32 -07005674 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005675}
Frederic Weisbecker613afbf2009-07-16 15:44:29 +02005676EXPORT_SYMBOL(__cond_resched_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005677
Frederic Weisbecker613afbf2009-07-16 15:44:29 +02005678int __sched __cond_resched_softirq(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005679{
5680 BUG_ON(!in_softirq());
5681
Peter Zijlstrad86ee482009-07-10 14:57:57 +02005682 if (should_resched()) {
Thomas Gleixner98d82562007-05-23 13:58:18 -07005683 local_bh_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005684 __cond_resched();
5685 local_bh_disable();
5686 return 1;
5687 }
5688 return 0;
5689}
Frederic Weisbecker613afbf2009-07-16 15:44:29 +02005690EXPORT_SYMBOL(__cond_resched_softirq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005691
Linus Torvalds1da177e2005-04-16 15:20:36 -07005692/**
5693 * yield - yield the current processor to other threads.
5694 *
Robert P. J. Day72fd4a32007-02-10 01:45:59 -08005695 * This is a shortcut for kernel-space yielding - it marks the
Linus Torvalds1da177e2005-04-16 15:20:36 -07005696 * thread runnable and calls sys_sched_yield().
5697 */
5698void __sched yield(void)
5699{
5700 set_current_state(TASK_RUNNING);
5701 sys_sched_yield();
5702}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005703EXPORT_SYMBOL(yield);
5704
Mike Galbraithd95f4122011-02-01 09:50:51 -05005705/**
5706 * yield_to - yield the current processor to another thread in
5707 * your thread group, or accelerate that thread toward the
5708 * processor it's on.
Randy Dunlap16addf92011-03-18 09:34:53 -07005709 * @p: target task
5710 * @preempt: whether task preemption is allowed or not
Mike Galbraithd95f4122011-02-01 09:50:51 -05005711 *
5712 * It's the caller's job to ensure that the target task struct
5713 * can't go away on us before we can do any checks.
5714 *
5715 * Returns true if we indeed boosted the target task.
5716 */
5717bool __sched yield_to(struct task_struct *p, bool preempt)
5718{
5719 struct task_struct *curr = current;
5720 struct rq *rq, *p_rq;
5721 unsigned long flags;
5722 bool yielded = 0;
5723
5724 local_irq_save(flags);
5725 rq = this_rq();
5726
5727again:
5728 p_rq = task_rq(p);
5729 double_rq_lock(rq, p_rq);
5730 while (task_rq(p) != p_rq) {
5731 double_rq_unlock(rq, p_rq);
5732 goto again;
5733 }
5734
5735 if (!curr->sched_class->yield_to_task)
5736 goto out;
5737
5738 if (curr->sched_class != p->sched_class)
5739 goto out;
5740
5741 if (task_running(p_rq, p) || p->state)
5742 goto out;
5743
5744 yielded = curr->sched_class->yield_to_task(rq, p, preempt);
Venkatesh Pallipadi6d1cafd2011-03-01 16:28:21 -08005745 if (yielded) {
Mike Galbraithd95f4122011-02-01 09:50:51 -05005746 schedstat_inc(rq, yld_count);
Venkatesh Pallipadi6d1cafd2011-03-01 16:28:21 -08005747 /*
5748 * Make p's CPU reschedule; pick_next_entity takes care of
5749 * fairness.
5750 */
5751 if (preempt && rq != p_rq)
5752 resched_task(p_rq->curr);
5753 }
Mike Galbraithd95f4122011-02-01 09:50:51 -05005754
5755out:
5756 double_rq_unlock(rq, p_rq);
5757 local_irq_restore(flags);
5758
5759 if (yielded)
5760 schedule();
5761
5762 return yielded;
5763}
5764EXPORT_SYMBOL_GPL(yield_to);
5765
Linus Torvalds1da177e2005-04-16 15:20:36 -07005766/*
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01005767 * This task is about to go to sleep on IO. Increment rq->nr_iowait so
Linus Torvalds1da177e2005-04-16 15:20:36 -07005768 * that process accounting knows that this is a task in IO wait state.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005769 */
5770void __sched io_schedule(void)
5771{
Hitoshi Mitake54d35f22009-06-29 14:44:57 +09005772 struct rq *rq = raw_rq();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005773
Shailabh Nagar0ff92242006-07-14 00:24:37 -07005774 delayacct_blkio_start();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005775 atomic_inc(&rq->nr_iowait);
Jens Axboe73c10102011-03-08 13:19:51 +01005776 blk_flush_plug(current);
Arjan van de Ven8f0dfc32009-07-20 11:26:58 -07005777 current->in_iowait = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005778 schedule();
Arjan van de Ven8f0dfc32009-07-20 11:26:58 -07005779 current->in_iowait = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005780 atomic_dec(&rq->nr_iowait);
Shailabh Nagar0ff92242006-07-14 00:24:37 -07005781 delayacct_blkio_end();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005782}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005783EXPORT_SYMBOL(io_schedule);
5784
5785long __sched io_schedule_timeout(long timeout)
5786{
Hitoshi Mitake54d35f22009-06-29 14:44:57 +09005787 struct rq *rq = raw_rq();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005788 long ret;
5789
Shailabh Nagar0ff92242006-07-14 00:24:37 -07005790 delayacct_blkio_start();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005791 atomic_inc(&rq->nr_iowait);
Jens Axboe73c10102011-03-08 13:19:51 +01005792 blk_flush_plug(current);
Arjan van de Ven8f0dfc32009-07-20 11:26:58 -07005793 current->in_iowait = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005794 ret = schedule_timeout(timeout);
Arjan van de Ven8f0dfc32009-07-20 11:26:58 -07005795 current->in_iowait = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005796 atomic_dec(&rq->nr_iowait);
Shailabh Nagar0ff92242006-07-14 00:24:37 -07005797 delayacct_blkio_end();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005798 return ret;
5799}
5800
5801/**
5802 * sys_sched_get_priority_max - return maximum RT priority.
5803 * @policy: scheduling class.
5804 *
5805 * this syscall returns the maximum rt_priority that can be used
5806 * by a given scheduling class.
5807 */
Heiko Carstens5add95d2009-01-14 14:14:08 +01005808SYSCALL_DEFINE1(sched_get_priority_max, int, policy)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005809{
5810 int ret = -EINVAL;
5811
5812 switch (policy) {
5813 case SCHED_FIFO:
5814 case SCHED_RR:
5815 ret = MAX_USER_RT_PRIO-1;
5816 break;
5817 case SCHED_NORMAL:
Ingo Molnarb0a94992006-01-14 13:20:41 -08005818 case SCHED_BATCH:
Ingo Molnardd41f592007-07-09 18:51:59 +02005819 case SCHED_IDLE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005820 ret = 0;
5821 break;
5822 }
5823 return ret;
5824}
5825
5826/**
5827 * sys_sched_get_priority_min - return minimum RT priority.
5828 * @policy: scheduling class.
5829 *
5830 * this syscall returns the minimum rt_priority that can be used
5831 * by a given scheduling class.
5832 */
Heiko Carstens5add95d2009-01-14 14:14:08 +01005833SYSCALL_DEFINE1(sched_get_priority_min, int, policy)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005834{
5835 int ret = -EINVAL;
5836
5837 switch (policy) {
5838 case SCHED_FIFO:
5839 case SCHED_RR:
5840 ret = 1;
5841 break;
5842 case SCHED_NORMAL:
Ingo Molnarb0a94992006-01-14 13:20:41 -08005843 case SCHED_BATCH:
Ingo Molnardd41f592007-07-09 18:51:59 +02005844 case SCHED_IDLE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005845 ret = 0;
5846 }
5847 return ret;
5848}
5849
5850/**
5851 * sys_sched_rr_get_interval - return the default timeslice of a process.
5852 * @pid: pid of the process.
5853 * @interval: userspace pointer to the timeslice value.
5854 *
5855 * this syscall writes the default timeslice value of a given process
5856 * into the user-space timespec buffer. A value of '0' means infinity.
5857 */
Heiko Carstens17da2bd2009-01-14 14:14:10 +01005858SYSCALL_DEFINE2(sched_rr_get_interval, pid_t, pid,
Heiko Carstens754fe8d2009-01-14 14:14:09 +01005859 struct timespec __user *, interval)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005860{
Ingo Molnar36c8b582006-07-03 00:25:41 -07005861 struct task_struct *p;
Dmitry Adamushkoa4ec24b2007-10-15 17:00:13 +02005862 unsigned int time_slice;
Thomas Gleixnerdba091b2009-12-09 09:32:03 +01005863 unsigned long flags;
5864 struct rq *rq;
Andi Kleen3a5c3592007-10-15 17:00:14 +02005865 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005866 struct timespec t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005867
5868 if (pid < 0)
Andi Kleen3a5c3592007-10-15 17:00:14 +02005869 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005870
5871 retval = -ESRCH;
Thomas Gleixner1a551ae2009-12-09 10:15:11 +00005872 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005873 p = find_process_by_pid(pid);
5874 if (!p)
5875 goto out_unlock;
5876
5877 retval = security_task_getscheduler(p);
5878 if (retval)
5879 goto out_unlock;
5880
Thomas Gleixnerdba091b2009-12-09 09:32:03 +01005881 rq = task_rq_lock(p, &flags);
5882 time_slice = p->sched_class->get_rr_interval(rq, p);
Peter Zijlstra0122ec52011-04-05 17:23:51 +02005883 task_rq_unlock(rq, p, &flags);
Dmitry Adamushkoa4ec24b2007-10-15 17:00:13 +02005884
Thomas Gleixner1a551ae2009-12-09 10:15:11 +00005885 rcu_read_unlock();
Dmitry Adamushkoa4ec24b2007-10-15 17:00:13 +02005886 jiffies_to_timespec(time_slice, &t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005887 retval = copy_to_user(interval, &t, sizeof(t)) ? -EFAULT : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005888 return retval;
Andi Kleen3a5c3592007-10-15 17:00:14 +02005889
Linus Torvalds1da177e2005-04-16 15:20:36 -07005890out_unlock:
Thomas Gleixner1a551ae2009-12-09 10:15:11 +00005891 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005892 return retval;
5893}
5894
Steven Rostedt7c731e02008-05-12 21:20:41 +02005895static const char stat_nam[] = TASK_STATE_TO_CHAR_STR;
Ingo Molnar36c8b582006-07-03 00:25:41 -07005896
Ingo Molnar82a1fcb2008-01-25 21:08:02 +01005897void sched_show_task(struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005898{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005899 unsigned long free = 0;
Ingo Molnar36c8b582006-07-03 00:25:41 -07005900 unsigned state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005901
Linus Torvalds1da177e2005-04-16 15:20:36 -07005902 state = p->state ? __ffs(p->state) + 1 : 0;
Erik Gilling28d06862010-11-19 18:08:51 -08005903 printk(KERN_INFO "%-15.15s %c", p->comm,
Andreas Mohr2ed6e342006-07-10 04:43:52 -07005904 state < sizeof(stat_nam) - 1 ? stat_nam[state] : '?');
Ingo Molnar4bd77322007-07-11 21:21:47 +02005905#if BITS_PER_LONG == 32
Linus Torvalds1da177e2005-04-16 15:20:36 -07005906 if (state == TASK_RUNNING)
Peter Zijlstra3df0fc52009-12-20 14:23:57 +01005907 printk(KERN_CONT " running ");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005908 else
Peter Zijlstra3df0fc52009-12-20 14:23:57 +01005909 printk(KERN_CONT " %08lx ", thread_saved_pc(p));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005910#else
5911 if (state == TASK_RUNNING)
Peter Zijlstra3df0fc52009-12-20 14:23:57 +01005912 printk(KERN_CONT " running task ");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005913 else
Peter Zijlstra3df0fc52009-12-20 14:23:57 +01005914 printk(KERN_CONT " %016lx ", thread_saved_pc(p));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005915#endif
5916#ifdef CONFIG_DEBUG_STACK_USAGE
Eric Sandeen7c9f8862008-04-22 16:38:23 -05005917 free = stack_not_used(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005918#endif
Peter Zijlstra3df0fc52009-12-20 14:23:57 +01005919 printk(KERN_CONT "%5lu %5d %6d 0x%08lx\n", free,
David Rientjesaa47b7e2009-05-04 01:38:05 -07005920 task_pid_nr(p), task_pid_nr(p->real_parent),
5921 (unsigned long)task_thread_info(p)->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005922
Nick Piggin5fb5e6d2008-01-25 21:08:34 +01005923 show_stack(p, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005924}
5925
Ingo Molnare59e2ae2006-12-06 20:35:59 -08005926void show_state_filter(unsigned long state_filter)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005927{
Ingo Molnar36c8b582006-07-03 00:25:41 -07005928 struct task_struct *g, *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005929
Ingo Molnar4bd77322007-07-11 21:21:47 +02005930#if BITS_PER_LONG == 32
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#else
Peter Zijlstra3df0fc52009-12-20 14:23:57 +01005934 printk(KERN_INFO
5935 " task PC stack pid father\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005936#endif
5937 read_lock(&tasklist_lock);
5938 do_each_thread(g, p) {
5939 /*
5940 * reset the NMI-timeout, listing all files on a slow
Lucas De Marchi25985ed2011-03-30 22:57:33 -03005941 * console might take a lot of time:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005942 */
5943 touch_nmi_watchdog();
Ingo Molnar39bc89f2007-04-25 20:50:03 -07005944 if (!state_filter || (p->state & state_filter))
Ingo Molnar82a1fcb2008-01-25 21:08:02 +01005945 sched_show_task(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005946 } while_each_thread(g, p);
5947
Jeremy Fitzhardinge04c91672007-05-08 00:28:05 -07005948 touch_all_softlockup_watchdogs();
5949
Ingo Molnardd41f592007-07-09 18:51:59 +02005950#ifdef CONFIG_SCHED_DEBUG
5951 sysrq_sched_debug_show();
5952#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07005953 read_unlock(&tasklist_lock);
Ingo Molnare59e2ae2006-12-06 20:35:59 -08005954 /*
5955 * Only show locks if all tasks are dumped:
5956 */
Shmulik Ladkani93335a22009-11-25 15:23:41 +02005957 if (!state_filter)
Ingo Molnare59e2ae2006-12-06 20:35:59 -08005958 debug_show_all_locks();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005959}
5960
Ingo Molnar1df21052007-07-09 18:51:58 +02005961void __cpuinit init_idle_bootup_task(struct task_struct *idle)
5962{
Ingo Molnardd41f592007-07-09 18:51:59 +02005963 idle->sched_class = &idle_sched_class;
Ingo Molnar1df21052007-07-09 18:51:58 +02005964}
5965
Ingo Molnarf340c0d2005-06-28 16:40:42 +02005966/**
5967 * init_idle - set up an idle thread for a given CPU
5968 * @idle: task in question
5969 * @cpu: cpu the idle task belongs to
5970 *
5971 * NOTE: this function does not set the idle thread's NEED_RESCHED
5972 * flag, to make booting more robust.
5973 */
Nick Piggin5c1e1762006-10-03 01:14:04 -07005974void __cpuinit init_idle(struct task_struct *idle, int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005975{
Ingo Molnar70b97a72006-07-03 00:25:42 -07005976 struct rq *rq = cpu_rq(cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005977 unsigned long flags;
5978
Thomas Gleixner05fa7852009-11-17 14:28:38 +01005979 raw_spin_lock_irqsave(&rq->lock, flags);
Ingo Molnar5cbd54e2008-11-12 20:05:50 +01005980
Ingo Molnardd41f592007-07-09 18:51:59 +02005981 __sched_fork(idle);
Peter Zijlstra06b83b52009-12-16 18:04:35 +01005982 idle->state = TASK_RUNNING;
Ingo Molnardd41f592007-07-09 18:51:59 +02005983 idle->se.exec_start = sched_clock();
5984
KOSAKI Motohiro1e1b6c52011-05-19 15:08:58 +09005985 do_set_cpus_allowed(idle, cpumask_of(cpu));
Peter Zijlstra6506cf6c2010-09-16 17:50:31 +02005986 /*
5987 * We're having a chicken and egg problem, even though we are
5988 * holding rq->lock, the cpu isn't yet set to this cpu so the
5989 * lockdep check in task_group() will fail.
5990 *
5991 * Similar case to sched_fork(). / Alternatively we could
5992 * use task_rq_lock() here and obtain the other rq->lock.
5993 *
5994 * Silence PROVE_RCU
5995 */
5996 rcu_read_lock();
Ingo Molnardd41f592007-07-09 18:51:59 +02005997 __set_task_cpu(idle, cpu);
Peter Zijlstra6506cf6c2010-09-16 17:50:31 +02005998 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005999
Linus Torvalds1da177e2005-04-16 15:20:36 -07006000 rq->curr = rq->idle = idle;
Peter Zijlstra3ca7a442011-04-05 17:23:40 +02006001#if defined(CONFIG_SMP)
6002 idle->on_cpu = 1;
Nick Piggin4866cde2005-06-25 14:57:23 -07006003#endif
Thomas Gleixner05fa7852009-11-17 14:28:38 +01006004 raw_spin_unlock_irqrestore(&rq->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006005
6006 /* Set the preempt count _outside_ the spinlocks! */
Al Viroa1261f52005-11-13 16:06:55 -08006007 task_thread_info(idle)->preempt_count = 0;
Jonathan Corbet625f2a32011-04-22 11:19:10 -06006008
Ingo Molnardd41f592007-07-09 18:51:59 +02006009 /*
6010 * The idle tasks have their own, simple scheduling class:
6011 */
6012 idle->sched_class = &idle_sched_class;
Steven Rostedt868baf02011-02-10 21:26:13 -05006013 ftrace_graph_init_idle_task(idle, cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006014}
6015
6016/*
6017 * In a system that switches off the HZ timer nohz_cpu_mask
6018 * indicates which cpus entered this state. This is used
6019 * in the rcu update to wait only for active cpus. For system
6020 * which do not switch off the HZ timer nohz_cpu_mask should
Rusty Russell6a7b3dc2008-11-25 02:35:04 +10306021 * always be CPU_BITS_NONE.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006022 */
Rusty Russell6a7b3dc2008-11-25 02:35:04 +10306023cpumask_var_t nohz_cpu_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006024
Ingo Molnar19978ca2007-11-09 22:39:38 +01006025/*
6026 * Increase the granularity value when there are more CPUs,
6027 * because with more CPUs the 'effective latency' as visible
6028 * to users decreases. But the relationship is not linear,
6029 * so pick a second-best guess by going with the log2 of the
6030 * number of CPUs.
6031 *
6032 * This idea comes from the SD scheduler of Con Kolivas:
6033 */
Christian Ehrhardtacb4a842009-11-30 12:16:48 +01006034static int get_update_sysctl_factor(void)
Christian Ehrhardt0bcdcf22009-11-30 12:16:46 +01006035{
Mike Galbraith4ca3ef72009-12-10 09:25:53 +01006036 unsigned int cpus = min_t(int, num_online_cpus(), 8);
Christian Ehrhardt1983a922009-11-30 12:16:47 +01006037 unsigned int factor;
6038
6039 switch (sysctl_sched_tunable_scaling) {
6040 case SCHED_TUNABLESCALING_NONE:
6041 factor = 1;
6042 break;
6043 case SCHED_TUNABLESCALING_LINEAR:
6044 factor = cpus;
6045 break;
6046 case SCHED_TUNABLESCALING_LOG:
6047 default:
6048 factor = 1 + ilog2(cpus);
6049 break;
6050 }
Christian Ehrhardt0bcdcf22009-11-30 12:16:46 +01006051
Christian Ehrhardtacb4a842009-11-30 12:16:48 +01006052 return factor;
6053}
6054
6055static void update_sysctl(void)
6056{
6057 unsigned int factor = get_update_sysctl_factor();
6058
Christian Ehrhardt0bcdcf22009-11-30 12:16:46 +01006059#define SET_SYSCTL(name) \
6060 (sysctl_##name = (factor) * normalized_sysctl_##name)
6061 SET_SYSCTL(sched_min_granularity);
6062 SET_SYSCTL(sched_latency);
6063 SET_SYSCTL(sched_wakeup_granularity);
Christian Ehrhardt0bcdcf22009-11-30 12:16:46 +01006064#undef SET_SYSCTL
6065}
6066
Ingo Molnar19978ca2007-11-09 22:39:38 +01006067static inline void sched_init_granularity(void)
6068{
Christian Ehrhardt0bcdcf22009-11-30 12:16:46 +01006069 update_sysctl();
Ingo Molnar19978ca2007-11-09 22:39:38 +01006070}
6071
Linus Torvalds1da177e2005-04-16 15:20:36 -07006072#ifdef CONFIG_SMP
KOSAKI Motohiro1e1b6c52011-05-19 15:08:58 +09006073void do_set_cpus_allowed(struct task_struct *p, const struct cpumask *new_mask)
6074{
6075 if (p->sched_class && p->sched_class->set_cpus_allowed)
6076 p->sched_class->set_cpus_allowed(p, new_mask);
6077 else {
6078 cpumask_copy(&p->cpus_allowed, new_mask);
6079 p->rt.nr_cpus_allowed = cpumask_weight(new_mask);
6080 }
6081}
6082
Linus Torvalds1da177e2005-04-16 15:20:36 -07006083/*
6084 * This is how migration works:
6085 *
Tejun Heo969c7922010-05-06 18:49:21 +02006086 * 1) we invoke migration_cpu_stop() on the target CPU using
6087 * stop_one_cpu().
6088 * 2) stopper starts to run (implicitly forcing the migrated thread
6089 * off the CPU)
6090 * 3) it checks whether the migrated task is still in the wrong runqueue.
6091 * 4) if it's in the wrong runqueue then the migration thread removes
Linus Torvalds1da177e2005-04-16 15:20:36 -07006092 * it and puts it into the right queue.
Tejun Heo969c7922010-05-06 18:49:21 +02006093 * 5) stopper completes and stop_one_cpu() returns and the migration
6094 * is done.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006095 */
6096
6097/*
6098 * Change a given task's CPU affinity. Migrate the thread to a
6099 * proper CPU and schedule it away if the CPU it's executing on
6100 * is removed from the allowed bitmask.
6101 *
6102 * NOTE: the caller must have a valid reference to the task, the
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01006103 * task must not exit() & deallocate itself prematurely. The
Linus Torvalds1da177e2005-04-16 15:20:36 -07006104 * call is not atomic; no spinlocks may be held.
6105 */
Rusty Russell96f874e2008-11-25 02:35:14 +10306106int set_cpus_allowed_ptr(struct task_struct *p, const struct cpumask *new_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006107{
6108 unsigned long flags;
Ingo Molnar70b97a72006-07-03 00:25:42 -07006109 struct rq *rq;
Tejun Heo969c7922010-05-06 18:49:21 +02006110 unsigned int dest_cpu;
Ingo Molnar48f24c42006-07-03 00:25:40 -07006111 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006112
6113 rq = task_rq_lock(p, &flags);
Peter Zijlstrae2912002009-12-16 18:04:36 +01006114
Yong Zhangdb44fc02011-05-09 22:07:05 +08006115 if (cpumask_equal(&p->cpus_allowed, new_mask))
6116 goto out;
6117
Peter Zijlstra6ad4c182009-11-25 13:31:39 +01006118 if (!cpumask_intersects(new_mask, cpu_active_mask)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006119 ret = -EINVAL;
6120 goto out;
6121 }
6122
Yong Zhangdb44fc02011-05-09 22:07:05 +08006123 if (unlikely((p->flags & PF_THREAD_BOUND) && p != current)) {
David Rientjes9985b0b2008-06-05 12:57:11 -07006124 ret = -EINVAL;
6125 goto out;
6126 }
6127
KOSAKI Motohiro1e1b6c52011-05-19 15:08:58 +09006128 do_set_cpus_allowed(p, new_mask);
Gregory Haskins73fe6aa2008-01-25 21:08:07 +01006129
Linus Torvalds1da177e2005-04-16 15:20:36 -07006130 /* Can the task run on the task's current CPU? If so, we're done */
Rusty Russell96f874e2008-11-25 02:35:14 +10306131 if (cpumask_test_cpu(task_cpu(p), new_mask))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006132 goto out;
6133
Tejun Heo969c7922010-05-06 18:49:21 +02006134 dest_cpu = cpumask_any_and(cpu_active_mask, new_mask);
Peter Zijlstrabd8e7dd2011-04-05 17:23:59 +02006135 if (p->on_rq) {
Tejun Heo969c7922010-05-06 18:49:21 +02006136 struct migration_arg arg = { p, dest_cpu };
Linus Torvalds1da177e2005-04-16 15:20:36 -07006137 /* Need help from migration thread: drop lock and wait. */
Peter Zijlstra0122ec52011-04-05 17:23:51 +02006138 task_rq_unlock(rq, p, &flags);
Tejun Heo969c7922010-05-06 18:49:21 +02006139 stop_one_cpu(cpu_of(rq), migration_cpu_stop, &arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006140 tlb_migrate_finish(p->mm);
6141 return 0;
6142 }
6143out:
Peter Zijlstra0122ec52011-04-05 17:23:51 +02006144 task_rq_unlock(rq, p, &flags);
Ingo Molnar48f24c42006-07-03 00:25:40 -07006145
Linus Torvalds1da177e2005-04-16 15:20:36 -07006146 return ret;
6147}
Mike Traviscd8ba7c2008-03-26 14:23:49 -07006148EXPORT_SYMBOL_GPL(set_cpus_allowed_ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006149
6150/*
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01006151 * Move (not current) task off this cpu, onto dest cpu. We're doing
Linus Torvalds1da177e2005-04-16 15:20:36 -07006152 * this because either it can't run here any more (set_cpus_allowed()
6153 * away from this CPU, or CPU going down), or because we're
6154 * attempting to rebalance this task on exec (sched_exec).
6155 *
6156 * So we race with normal scheduler movements, but that's OK, as long
6157 * as the task is no longer on this CPU.
Kirill Korotaevefc30812006-06-27 02:54:32 -07006158 *
6159 * Returns non-zero if task was successfully migrated.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006160 */
Kirill Korotaevefc30812006-06-27 02:54:32 -07006161static int __migrate_task(struct task_struct *p, int src_cpu, int dest_cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006162{
Ingo Molnar70b97a72006-07-03 00:25:42 -07006163 struct rq *rq_dest, *rq_src;
Peter Zijlstrae2912002009-12-16 18:04:36 +01006164 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006165
Max Krasnyanskye761b772008-07-15 04:43:49 -07006166 if (unlikely(!cpu_active(dest_cpu)))
Kirill Korotaevefc30812006-06-27 02:54:32 -07006167 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006168
6169 rq_src = cpu_rq(src_cpu);
6170 rq_dest = cpu_rq(dest_cpu);
6171
Peter Zijlstra0122ec52011-04-05 17:23:51 +02006172 raw_spin_lock(&p->pi_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006173 double_rq_lock(rq_src, rq_dest);
6174 /* Already moved. */
6175 if (task_cpu(p) != src_cpu)
Linus Torvaldsb1e38732008-07-10 11:25:03 -07006176 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006177 /* Affinity changed (again). */
Rusty Russell96f874e2008-11-25 02:35:14 +10306178 if (!cpumask_test_cpu(dest_cpu, &p->cpus_allowed))
Linus Torvaldsb1e38732008-07-10 11:25:03 -07006179 goto fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006180
Peter Zijlstrae2912002009-12-16 18:04:36 +01006181 /*
6182 * If we're not on a rq, the next wake-up will ensure we're
6183 * placed properly.
6184 */
Peter Zijlstrafd2f4412011-04-05 17:23:44 +02006185 if (p->on_rq) {
Ingo Molnar2e1cb742007-08-09 11:16:49 +02006186 deactivate_task(rq_src, p, 0);
Peter Zijlstrae2912002009-12-16 18:04:36 +01006187 set_task_cpu(p, dest_cpu);
Ingo Molnardd41f592007-07-09 18:51:59 +02006188 activate_task(rq_dest, p, 0);
Peter Zijlstra15afe092008-09-20 23:38:02 +02006189 check_preempt_curr(rq_dest, p, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006190 }
Linus Torvaldsb1e38732008-07-10 11:25:03 -07006191done:
Kirill Korotaevefc30812006-06-27 02:54:32 -07006192 ret = 1;
Linus Torvaldsb1e38732008-07-10 11:25:03 -07006193fail:
Linus Torvalds1da177e2005-04-16 15:20:36 -07006194 double_rq_unlock(rq_src, rq_dest);
Peter Zijlstra0122ec52011-04-05 17:23:51 +02006195 raw_spin_unlock(&p->pi_lock);
Kirill Korotaevefc30812006-06-27 02:54:32 -07006196 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006197}
6198
6199/*
Tejun Heo969c7922010-05-06 18:49:21 +02006200 * migration_cpu_stop - this will be executed by a highprio stopper thread
6201 * and performs thread migration by bumping thread off CPU then
6202 * 'pushing' onto another runqueue.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006203 */
Tejun Heo969c7922010-05-06 18:49:21 +02006204static int migration_cpu_stop(void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006205{
Tejun Heo969c7922010-05-06 18:49:21 +02006206 struct migration_arg *arg = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006207
Tejun Heo969c7922010-05-06 18:49:21 +02006208 /*
6209 * The original target cpu might have gone down and we might
6210 * be on another cpu but it doesn't matter.
6211 */
6212 local_irq_disable();
6213 __migrate_task(arg->task, raw_smp_processor_id(), arg->dest_cpu);
6214 local_irq_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006215 return 0;
6216}
6217
6218#ifdef CONFIG_HOTPLUG_CPU
Linus Torvalds1da177e2005-04-16 15:20:36 -07006219
Ingo Molnar48f24c42006-07-03 00:25:40 -07006220/*
6221 * Ensures that the idle task is using init_mm right before its cpu goes
Linus Torvalds1da177e2005-04-16 15:20:36 -07006222 * offline.
6223 */
6224void idle_task_exit(void)
6225{
6226 struct mm_struct *mm = current->active_mm;
6227
6228 BUG_ON(cpu_online(smp_processor_id()));
6229
6230 if (mm != &init_mm)
6231 switch_mm(mm, &init_mm, current);
6232 mmdrop(mm);
6233}
6234
Peter Zijlstra48c5cca2010-11-13 19:32:29 +01006235/*
6236 * While a dead CPU has no uninterruptible tasks queued at this point,
6237 * it might still have a nonzero ->nr_uninterruptible counter, because
6238 * for performance reasons the counter is not stricly tracking tasks to
6239 * their home CPUs. So we just add the counter to another CPU's counter,
6240 * to keep the global sum constant after CPU-down:
6241 */
6242static void migrate_nr_uninterruptible(struct rq *rq_src)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006243{
Peter Zijlstra48c5cca2010-11-13 19:32:29 +01006244 struct rq *rq_dest = cpu_rq(cpumask_any(cpu_active_mask));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006245
Peter Zijlstra48c5cca2010-11-13 19:32:29 +01006246 rq_dest->nr_uninterruptible += rq_src->nr_uninterruptible;
6247 rq_src->nr_uninterruptible = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006248}
Thomas Gleixnerdce48a82009-04-11 10:43:41 +02006249
6250/*
6251 * remove the tasks which were accounted by rq from calc_load_tasks.
6252 */
6253static void calc_global_load_remove(struct rq *rq)
6254{
6255 atomic_long_sub(rq->calc_load_active, &calc_load_tasks);
Thomas Gleixnera468d382009-07-17 14:15:46 +02006256 rq->calc_load_active = 0;
Thomas Gleixnerdce48a82009-04-11 10:43:41 +02006257}
Peter Zijlstra48c5cca2010-11-13 19:32:29 +01006258
6259/*
6260 * Migrate all tasks from the rq, sleeping tasks will be migrated by
6261 * try_to_wake_up()->select_task_rq().
6262 *
6263 * Called with rq->lock held even though we'er in stop_machine() and
6264 * there's no concurrency possible, we hold the required locks anyway
6265 * because of lock validation efforts.
6266 */
6267static void migrate_tasks(unsigned int dead_cpu)
6268{
6269 struct rq *rq = cpu_rq(dead_cpu);
6270 struct task_struct *next, *stop = rq->stop;
6271 int dest_cpu;
6272
6273 /*
6274 * Fudge the rq selection such that the below task selection loop
6275 * doesn't get stuck on the currently eligible stop task.
6276 *
6277 * We're currently inside stop_machine() and the rq is either stuck
6278 * in the stop_machine_cpu_stop() loop, or we're executing this code,
6279 * either way we should never end up calling schedule() until we're
6280 * done here.
6281 */
6282 rq->stop = NULL;
6283
6284 for ( ; ; ) {
6285 /*
6286 * There's this thread running, bail when that's the only
6287 * remaining thread.
6288 */
6289 if (rq->nr_running == 1)
6290 break;
6291
6292 next = pick_next_task(rq);
6293 BUG_ON(!next);
6294 next->sched_class->put_prev_task(rq, next);
6295
6296 /* Find suitable destination for @next, with force if needed. */
6297 dest_cpu = select_fallback_rq(dead_cpu, next);
6298 raw_spin_unlock(&rq->lock);
6299
6300 __migrate_task(next, dead_cpu, dest_cpu);
6301
6302 raw_spin_lock(&rq->lock);
6303 }
6304
6305 rq->stop = stop;
6306}
6307
Linus Torvalds1da177e2005-04-16 15:20:36 -07006308#endif /* CONFIG_HOTPLUG_CPU */
6309
Nick Piggine692ab52007-07-26 13:40:43 +02006310#if defined(CONFIG_SCHED_DEBUG) && defined(CONFIG_SYSCTL)
6311
6312static struct ctl_table sd_ctl_dir[] = {
Alexey Dobriyane0361852007-08-09 11:16:46 +02006313 {
6314 .procname = "sched_domain",
Eric W. Biedermanc57baf12007-08-23 15:18:02 +02006315 .mode = 0555,
Alexey Dobriyane0361852007-08-09 11:16:46 +02006316 },
Eric W. Biederman56992302009-11-05 15:38:40 -08006317 {}
Nick Piggine692ab52007-07-26 13:40:43 +02006318};
6319
6320static struct ctl_table sd_ctl_root[] = {
Alexey Dobriyane0361852007-08-09 11:16:46 +02006321 {
6322 .procname = "kernel",
Eric W. Biedermanc57baf12007-08-23 15:18:02 +02006323 .mode = 0555,
Alexey Dobriyane0361852007-08-09 11:16:46 +02006324 .child = sd_ctl_dir,
6325 },
Eric W. Biederman56992302009-11-05 15:38:40 -08006326 {}
Nick Piggine692ab52007-07-26 13:40:43 +02006327};
6328
6329static struct ctl_table *sd_alloc_ctl_entry(int n)
6330{
6331 struct ctl_table *entry =
Milton Miller5cf9f062007-10-15 17:00:19 +02006332 kcalloc(n, sizeof(struct ctl_table), GFP_KERNEL);
Nick Piggine692ab52007-07-26 13:40:43 +02006333
Nick Piggine692ab52007-07-26 13:40:43 +02006334 return entry;
6335}
6336
Milton Miller6382bc92007-10-15 17:00:19 +02006337static void sd_free_ctl_entry(struct ctl_table **tablep)
6338{
Milton Millercd790072007-10-17 16:55:11 +02006339 struct ctl_table *entry;
Milton Miller6382bc92007-10-15 17:00:19 +02006340
Milton Millercd790072007-10-17 16:55:11 +02006341 /*
6342 * In the intermediate directories, both the child directory and
6343 * procname are dynamically allocated and could fail but the mode
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01006344 * will always be set. In the lowest directory the names are
Milton Millercd790072007-10-17 16:55:11 +02006345 * static strings and all have proc handlers.
6346 */
6347 for (entry = *tablep; entry->mode; entry++) {
Milton Miller6382bc92007-10-15 17:00:19 +02006348 if (entry->child)
6349 sd_free_ctl_entry(&entry->child);
Milton Millercd790072007-10-17 16:55:11 +02006350 if (entry->proc_handler == NULL)
6351 kfree(entry->procname);
6352 }
Milton Miller6382bc92007-10-15 17:00:19 +02006353
6354 kfree(*tablep);
6355 *tablep = NULL;
6356}
6357
Nick Piggine692ab52007-07-26 13:40:43 +02006358static void
Alexey Dobriyane0361852007-08-09 11:16:46 +02006359set_table_entry(struct ctl_table *entry,
Nick Piggine692ab52007-07-26 13:40:43 +02006360 const char *procname, void *data, int maxlen,
6361 mode_t mode, proc_handler *proc_handler)
6362{
Nick Piggine692ab52007-07-26 13:40:43 +02006363 entry->procname = procname;
6364 entry->data = data;
6365 entry->maxlen = maxlen;
6366 entry->mode = mode;
6367 entry->proc_handler = proc_handler;
6368}
6369
6370static struct ctl_table *
6371sd_alloc_ctl_domain_table(struct sched_domain *sd)
6372{
Ingo Molnara5d8c342008-10-09 11:35:51 +02006373 struct ctl_table *table = sd_alloc_ctl_entry(13);
Nick Piggine692ab52007-07-26 13:40:43 +02006374
Milton Millerad1cdc12007-10-15 17:00:19 +02006375 if (table == NULL)
6376 return NULL;
6377
Alexey Dobriyane0361852007-08-09 11:16:46 +02006378 set_table_entry(&table[0], "min_interval", &sd->min_interval,
Nick Piggine692ab52007-07-26 13:40:43 +02006379 sizeof(long), 0644, proc_doulongvec_minmax);
Alexey Dobriyane0361852007-08-09 11:16:46 +02006380 set_table_entry(&table[1], "max_interval", &sd->max_interval,
Nick Piggine692ab52007-07-26 13:40:43 +02006381 sizeof(long), 0644, proc_doulongvec_minmax);
Alexey Dobriyane0361852007-08-09 11:16:46 +02006382 set_table_entry(&table[2], "busy_idx", &sd->busy_idx,
Nick Piggine692ab52007-07-26 13:40:43 +02006383 sizeof(int), 0644, proc_dointvec_minmax);
Alexey Dobriyane0361852007-08-09 11:16:46 +02006384 set_table_entry(&table[3], "idle_idx", &sd->idle_idx,
Nick Piggine692ab52007-07-26 13:40:43 +02006385 sizeof(int), 0644, proc_dointvec_minmax);
Alexey Dobriyane0361852007-08-09 11:16:46 +02006386 set_table_entry(&table[4], "newidle_idx", &sd->newidle_idx,
Nick Piggine692ab52007-07-26 13:40:43 +02006387 sizeof(int), 0644, proc_dointvec_minmax);
Alexey Dobriyane0361852007-08-09 11:16:46 +02006388 set_table_entry(&table[5], "wake_idx", &sd->wake_idx,
Nick Piggine692ab52007-07-26 13:40:43 +02006389 sizeof(int), 0644, proc_dointvec_minmax);
Alexey Dobriyane0361852007-08-09 11:16:46 +02006390 set_table_entry(&table[6], "forkexec_idx", &sd->forkexec_idx,
Nick Piggine692ab52007-07-26 13:40:43 +02006391 sizeof(int), 0644, proc_dointvec_minmax);
Alexey Dobriyane0361852007-08-09 11:16:46 +02006392 set_table_entry(&table[7], "busy_factor", &sd->busy_factor,
Nick Piggine692ab52007-07-26 13:40:43 +02006393 sizeof(int), 0644, proc_dointvec_minmax);
Alexey Dobriyane0361852007-08-09 11:16:46 +02006394 set_table_entry(&table[8], "imbalance_pct", &sd->imbalance_pct,
Nick Piggine692ab52007-07-26 13:40:43 +02006395 sizeof(int), 0644, proc_dointvec_minmax);
Zou Nan haiace8b3d2007-10-15 17:00:14 +02006396 set_table_entry(&table[9], "cache_nice_tries",
Nick Piggine692ab52007-07-26 13:40:43 +02006397 &sd->cache_nice_tries,
6398 sizeof(int), 0644, proc_dointvec_minmax);
Zou Nan haiace8b3d2007-10-15 17:00:14 +02006399 set_table_entry(&table[10], "flags", &sd->flags,
Nick Piggine692ab52007-07-26 13:40:43 +02006400 sizeof(int), 0644, proc_dointvec_minmax);
Ingo Molnara5d8c342008-10-09 11:35:51 +02006401 set_table_entry(&table[11], "name", sd->name,
6402 CORENAME_MAX_SIZE, 0444, proc_dostring);
6403 /* &table[12] is terminator */
Nick Piggine692ab52007-07-26 13:40:43 +02006404
6405 return table;
6406}
6407
Ingo Molnar9a4e7152007-11-28 15:52:56 +01006408static ctl_table *sd_alloc_ctl_cpu_table(int cpu)
Nick Piggine692ab52007-07-26 13:40:43 +02006409{
6410 struct ctl_table *entry, *table;
6411 struct sched_domain *sd;
6412 int domain_num = 0, i;
6413 char buf[32];
6414
6415 for_each_domain(cpu, sd)
6416 domain_num++;
6417 entry = table = sd_alloc_ctl_entry(domain_num + 1);
Milton Millerad1cdc12007-10-15 17:00:19 +02006418 if (table == NULL)
6419 return NULL;
Nick Piggine692ab52007-07-26 13:40:43 +02006420
6421 i = 0;
6422 for_each_domain(cpu, sd) {
6423 snprintf(buf, 32, "domain%d", i);
Nick Piggine692ab52007-07-26 13:40:43 +02006424 entry->procname = kstrdup(buf, GFP_KERNEL);
Eric W. Biedermanc57baf12007-08-23 15:18:02 +02006425 entry->mode = 0555;
Nick Piggine692ab52007-07-26 13:40:43 +02006426 entry->child = sd_alloc_ctl_domain_table(sd);
6427 entry++;
6428 i++;
6429 }
6430 return table;
6431}
6432
6433static struct ctl_table_header *sd_sysctl_header;
Milton Miller6382bc92007-10-15 17:00:19 +02006434static void register_sched_domain_sysctl(void)
Nick Piggine692ab52007-07-26 13:40:43 +02006435{
Peter Zijlstra6ad4c182009-11-25 13:31:39 +01006436 int i, cpu_num = num_possible_cpus();
Nick Piggine692ab52007-07-26 13:40:43 +02006437 struct ctl_table *entry = sd_alloc_ctl_entry(cpu_num + 1);
6438 char buf[32];
6439
Milton Miller73785472007-10-24 18:23:48 +02006440 WARN_ON(sd_ctl_dir[0].child);
6441 sd_ctl_dir[0].child = entry;
6442
Milton Millerad1cdc12007-10-15 17:00:19 +02006443 if (entry == NULL)
6444 return;
6445
Peter Zijlstra6ad4c182009-11-25 13:31:39 +01006446 for_each_possible_cpu(i) {
Nick Piggine692ab52007-07-26 13:40:43 +02006447 snprintf(buf, 32, "cpu%d", i);
Nick Piggine692ab52007-07-26 13:40:43 +02006448 entry->procname = kstrdup(buf, GFP_KERNEL);
Eric W. Biedermanc57baf12007-08-23 15:18:02 +02006449 entry->mode = 0555;
Nick Piggine692ab52007-07-26 13:40:43 +02006450 entry->child = sd_alloc_ctl_cpu_table(i);
Milton Miller97b6ea72007-10-15 17:00:19 +02006451 entry++;
Nick Piggine692ab52007-07-26 13:40:43 +02006452 }
Milton Miller73785472007-10-24 18:23:48 +02006453
6454 WARN_ON(sd_sysctl_header);
Nick Piggine692ab52007-07-26 13:40:43 +02006455 sd_sysctl_header = register_sysctl_table(sd_ctl_root);
6456}
Milton Miller6382bc92007-10-15 17:00:19 +02006457
Milton Miller73785472007-10-24 18:23:48 +02006458/* may be called multiple times per register */
Milton Miller6382bc92007-10-15 17:00:19 +02006459static void unregister_sched_domain_sysctl(void)
6460{
Milton Miller73785472007-10-24 18:23:48 +02006461 if (sd_sysctl_header)
6462 unregister_sysctl_table(sd_sysctl_header);
Milton Miller6382bc92007-10-15 17:00:19 +02006463 sd_sysctl_header = NULL;
Milton Miller73785472007-10-24 18:23:48 +02006464 if (sd_ctl_dir[0].child)
6465 sd_free_ctl_entry(&sd_ctl_dir[0].child);
Milton Miller6382bc92007-10-15 17:00:19 +02006466}
Nick Piggine692ab52007-07-26 13:40:43 +02006467#else
Milton Miller6382bc92007-10-15 17:00:19 +02006468static void register_sched_domain_sysctl(void)
6469{
6470}
6471static void unregister_sched_domain_sysctl(void)
Nick Piggine692ab52007-07-26 13:40:43 +02006472{
6473}
6474#endif
6475
Gregory Haskins1f11eb62008-06-04 15:04:05 -04006476static void set_rq_online(struct rq *rq)
6477{
6478 if (!rq->online) {
6479 const struct sched_class *class;
6480
Rusty Russellc6c49272008-11-25 02:35:05 +10306481 cpumask_set_cpu(rq->cpu, rq->rd->online);
Gregory Haskins1f11eb62008-06-04 15:04:05 -04006482 rq->online = 1;
6483
6484 for_each_class(class) {
6485 if (class->rq_online)
6486 class->rq_online(rq);
6487 }
6488 }
6489}
6490
6491static void set_rq_offline(struct rq *rq)
6492{
6493 if (rq->online) {
6494 const struct sched_class *class;
6495
6496 for_each_class(class) {
6497 if (class->rq_offline)
6498 class->rq_offline(rq);
6499 }
6500
Rusty Russellc6c49272008-11-25 02:35:05 +10306501 cpumask_clear_cpu(rq->cpu, rq->rd->online);
Gregory Haskins1f11eb62008-06-04 15:04:05 -04006502 rq->online = 0;
6503 }
6504}
6505
Linus Torvalds1da177e2005-04-16 15:20:36 -07006506/*
6507 * migration_call - callback that gets triggered when a CPU is added.
6508 * Here we can start up the necessary migration thread for the new CPU.
6509 */
Ingo Molnar48f24c42006-07-03 00:25:40 -07006510static int __cpuinit
6511migration_call(struct notifier_block *nfb, unsigned long action, void *hcpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006512{
Ingo Molnar48f24c42006-07-03 00:25:40 -07006513 int cpu = (long)hcpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006514 unsigned long flags;
Tejun Heo969c7922010-05-06 18:49:21 +02006515 struct rq *rq = cpu_rq(cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006516
Peter Zijlstra48c5cca2010-11-13 19:32:29 +01006517 switch (action & ~CPU_TASKS_FROZEN) {
Gautham R Shenoy5be93612007-05-09 02:34:04 -07006518
Linus Torvalds1da177e2005-04-16 15:20:36 -07006519 case CPU_UP_PREPARE:
Thomas Gleixnera468d382009-07-17 14:15:46 +02006520 rq->calc_load_update = calc_load_update;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006521 break;
Ingo Molnar48f24c42006-07-03 00:25:40 -07006522
Linus Torvalds1da177e2005-04-16 15:20:36 -07006523 case CPU_ONLINE:
Gregory Haskins1f94ef52008-03-10 16:52:41 -04006524 /* Update our root-domain */
Thomas Gleixner05fa7852009-11-17 14:28:38 +01006525 raw_spin_lock_irqsave(&rq->lock, flags);
Gregory Haskins1f94ef52008-03-10 16:52:41 -04006526 if (rq->rd) {
Rusty Russellc6c49272008-11-25 02:35:05 +10306527 BUG_ON(!cpumask_test_cpu(cpu, rq->rd->span));
Gregory Haskins1f11eb62008-06-04 15:04:05 -04006528
6529 set_rq_online(rq);
Gregory Haskins1f94ef52008-03-10 16:52:41 -04006530 }
Thomas Gleixner05fa7852009-11-17 14:28:38 +01006531 raw_spin_unlock_irqrestore(&rq->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006532 break;
Ingo Molnar48f24c42006-07-03 00:25:40 -07006533
Linus Torvalds1da177e2005-04-16 15:20:36 -07006534#ifdef CONFIG_HOTPLUG_CPU
Gregory Haskins08f503b2008-03-10 17:59:11 -04006535 case CPU_DYING:
Peter Zijlstra317f3942011-04-05 17:23:58 +02006536 sched_ttwu_pending();
Gregory Haskins57d885f2008-01-25 21:08:18 +01006537 /* Update our root-domain */
Thomas Gleixner05fa7852009-11-17 14:28:38 +01006538 raw_spin_lock_irqsave(&rq->lock, flags);
Gregory Haskins57d885f2008-01-25 21:08:18 +01006539 if (rq->rd) {
Rusty Russellc6c49272008-11-25 02:35:05 +10306540 BUG_ON(!cpumask_test_cpu(cpu, rq->rd->span));
Gregory Haskins1f11eb62008-06-04 15:04:05 -04006541 set_rq_offline(rq);
Gregory Haskins57d885f2008-01-25 21:08:18 +01006542 }
Peter Zijlstra48c5cca2010-11-13 19:32:29 +01006543 migrate_tasks(cpu);
6544 BUG_ON(rq->nr_running != 1); /* the migration thread */
Thomas Gleixner05fa7852009-11-17 14:28:38 +01006545 raw_spin_unlock_irqrestore(&rq->lock, flags);
Peter Zijlstra48c5cca2010-11-13 19:32:29 +01006546
6547 migrate_nr_uninterruptible(rq);
6548 calc_global_load_remove(rq);
Gregory Haskins57d885f2008-01-25 21:08:18 +01006549 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006550#endif
6551 }
Peter Zijlstra49c022e2011-04-05 10:14:25 +02006552
6553 update_max_interval();
6554
Linus Torvalds1da177e2005-04-16 15:20:36 -07006555 return NOTIFY_OK;
6556}
6557
Paul Mackerrasf38b0822009-06-02 21:05:16 +10006558/*
6559 * Register at high priority so that task migration (migrate_all_tasks)
6560 * happens before everything else. This has to be lower priority than
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006561 * the notifier in the perf_event subsystem, though.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006562 */
Chandra Seetharaman26c21432006-06-27 02:54:10 -07006563static struct notifier_block __cpuinitdata migration_notifier = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006564 .notifier_call = migration_call,
Tejun Heo50a323b2010-06-08 21:40:36 +02006565 .priority = CPU_PRI_MIGRATION,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006566};
6567
Tejun Heo3a101d02010-06-08 21:40:36 +02006568static int __cpuinit sched_cpu_active(struct notifier_block *nfb,
6569 unsigned long action, void *hcpu)
6570{
6571 switch (action & ~CPU_TASKS_FROZEN) {
6572 case CPU_ONLINE:
6573 case CPU_DOWN_FAILED:
6574 set_cpu_active((long)hcpu, true);
6575 return NOTIFY_OK;
6576 default:
6577 return NOTIFY_DONE;
6578 }
6579}
6580
6581static int __cpuinit sched_cpu_inactive(struct notifier_block *nfb,
6582 unsigned long action, void *hcpu)
6583{
6584 switch (action & ~CPU_TASKS_FROZEN) {
6585 case CPU_DOWN_PREPARE:
6586 set_cpu_active((long)hcpu, false);
6587 return NOTIFY_OK;
6588 default:
6589 return NOTIFY_DONE;
6590 }
6591}
6592
Eduard - Gabriel Munteanu7babe8d2008-07-25 19:45:11 -07006593static int __init migration_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006594{
6595 void *cpu = (void *)(long)smp_processor_id();
Akinobu Mita07dccf32006-09-29 02:00:22 -07006596 int err;
Ingo Molnar48f24c42006-07-03 00:25:40 -07006597
Tejun Heo3a101d02010-06-08 21:40:36 +02006598 /* Initialize migration for the boot CPU */
Akinobu Mita07dccf32006-09-29 02:00:22 -07006599 err = migration_call(&migration_notifier, CPU_UP_PREPARE, cpu);
6600 BUG_ON(err == NOTIFY_BAD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006601 migration_call(&migration_notifier, CPU_ONLINE, cpu);
6602 register_cpu_notifier(&migration_notifier);
Eduard - Gabriel Munteanu7babe8d2008-07-25 19:45:11 -07006603
Tejun Heo3a101d02010-06-08 21:40:36 +02006604 /* Register cpu active notifiers */
6605 cpu_notifier(sched_cpu_active, CPU_PRI_SCHED_ACTIVE);
6606 cpu_notifier(sched_cpu_inactive, CPU_PRI_SCHED_INACTIVE);
6607
Thomas Gleixnera004cd42009-07-21 09:54:05 +02006608 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006609}
Eduard - Gabriel Munteanu7babe8d2008-07-25 19:45:11 -07006610early_initcall(migration_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006611#endif
6612
6613#ifdef CONFIG_SMP
Christoph Lameter476f3532007-05-06 14:48:58 -07006614
Peter Zijlstra4cb98832011-04-07 14:09:58 +02006615static cpumask_var_t sched_domains_tmpmask; /* sched_domains_mutex */
6616
Ingo Molnar3e9830d2007-10-15 17:00:13 +02006617#ifdef CONFIG_SCHED_DEBUG
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02006618
Mike Travisf6630112009-11-17 18:22:15 -06006619static __read_mostly int sched_domain_debug_enabled;
6620
6621static int __init sched_domain_debug_setup(char *str)
6622{
6623 sched_domain_debug_enabled = 1;
6624
6625 return 0;
6626}
6627early_param("sched_debug", sched_domain_debug_setup);
6628
Mike Travis7c16ec52008-04-04 18:11:11 -07006629static int sched_domain_debug_one(struct sched_domain *sd, int cpu, int level,
Rusty Russell96f874e2008-11-25 02:35:14 +10306630 struct cpumask *groupmask)
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02006631{
6632 struct sched_group *group = sd->groups;
Mike Travis434d53b2008-04-04 18:11:04 -07006633 char str[256];
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02006634
Rusty Russell968ea6d2008-12-13 21:55:51 +10306635 cpulist_scnprintf(str, sizeof(str), sched_domain_span(sd));
Rusty Russell96f874e2008-11-25 02:35:14 +10306636 cpumask_clear(groupmask);
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02006637
6638 printk(KERN_DEBUG "%*s domain %d: ", level, "", level);
6639
6640 if (!(sd->flags & SD_LOAD_BALANCE)) {
Peter Zijlstra3df0fc52009-12-20 14:23:57 +01006641 printk("does not load-balance\n");
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02006642 if (sd->parent)
Peter Zijlstra3df0fc52009-12-20 14:23:57 +01006643 printk(KERN_ERR "ERROR: !SD_LOAD_BALANCE domain"
6644 " has parent");
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02006645 return -1;
6646 }
6647
Peter Zijlstra3df0fc52009-12-20 14:23:57 +01006648 printk(KERN_CONT "span %s level %s\n", str, sd->name);
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02006649
Rusty Russell758b2cd2008-11-25 02:35:04 +10306650 if (!cpumask_test_cpu(cpu, sched_domain_span(sd))) {
Peter Zijlstra3df0fc52009-12-20 14:23:57 +01006651 printk(KERN_ERR "ERROR: domain->span does not contain "
6652 "CPU%d\n", cpu);
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02006653 }
Rusty Russell758b2cd2008-11-25 02:35:04 +10306654 if (!cpumask_test_cpu(cpu, sched_group_cpus(group))) {
Peter Zijlstra3df0fc52009-12-20 14:23:57 +01006655 printk(KERN_ERR "ERROR: domain->groups does not contain"
6656 " CPU%d\n", cpu);
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02006657 }
6658
6659 printk(KERN_DEBUG "%*s groups:", level + 1, "");
6660 do {
6661 if (!group) {
Peter Zijlstra3df0fc52009-12-20 14:23:57 +01006662 printk("\n");
6663 printk(KERN_ERR "ERROR: group is NULL\n");
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02006664 break;
6665 }
6666
Peter Zijlstra9c3f75c2011-07-14 13:00:06 +02006667 if (!group->sgp->power) {
Peter Zijlstra3df0fc52009-12-20 14:23:57 +01006668 printk(KERN_CONT "\n");
6669 printk(KERN_ERR "ERROR: domain->cpu_power not "
6670 "set\n");
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02006671 break;
6672 }
6673
Rusty Russell758b2cd2008-11-25 02:35:04 +10306674 if (!cpumask_weight(sched_group_cpus(group))) {
Peter Zijlstra3df0fc52009-12-20 14:23:57 +01006675 printk(KERN_CONT "\n");
6676 printk(KERN_ERR "ERROR: empty group\n");
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02006677 break;
6678 }
6679
Rusty Russell758b2cd2008-11-25 02:35:04 +10306680 if (cpumask_intersects(groupmask, sched_group_cpus(group))) {
Peter Zijlstra3df0fc52009-12-20 14:23:57 +01006681 printk(KERN_CONT "\n");
6682 printk(KERN_ERR "ERROR: repeated CPUs\n");
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02006683 break;
6684 }
6685
Rusty Russell758b2cd2008-11-25 02:35:04 +10306686 cpumask_or(groupmask, groupmask, sched_group_cpus(group));
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02006687
Rusty Russell968ea6d2008-12-13 21:55:51 +10306688 cpulist_scnprintf(str, sizeof(str), sched_group_cpus(group));
Gautham R Shenoy381512c2009-04-14 09:09:36 +05306689
Peter Zijlstra3df0fc52009-12-20 14:23:57 +01006690 printk(KERN_CONT " %s", str);
Peter Zijlstra9c3f75c2011-07-14 13:00:06 +02006691 if (group->sgp->power != SCHED_POWER_SCALE) {
Peter Zijlstra3df0fc52009-12-20 14:23:57 +01006692 printk(KERN_CONT " (cpu_power = %d)",
Peter Zijlstra9c3f75c2011-07-14 13:00:06 +02006693 group->sgp->power);
Gautham R Shenoy381512c2009-04-14 09:09:36 +05306694 }
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02006695
6696 group = group->next;
6697 } while (group != sd->groups);
Peter Zijlstra3df0fc52009-12-20 14:23:57 +01006698 printk(KERN_CONT "\n");
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02006699
Rusty Russell758b2cd2008-11-25 02:35:04 +10306700 if (!cpumask_equal(sched_domain_span(sd), groupmask))
Peter Zijlstra3df0fc52009-12-20 14:23:57 +01006701 printk(KERN_ERR "ERROR: groups don't span domain->span\n");
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02006702
Rusty Russell758b2cd2008-11-25 02:35:04 +10306703 if (sd->parent &&
6704 !cpumask_subset(groupmask, sched_domain_span(sd->parent)))
Peter Zijlstra3df0fc52009-12-20 14:23:57 +01006705 printk(KERN_ERR "ERROR: parent span is not a superset "
6706 "of domain->span\n");
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02006707 return 0;
6708}
6709
Linus Torvalds1da177e2005-04-16 15:20:36 -07006710static void sched_domain_debug(struct sched_domain *sd, int cpu)
6711{
6712 int level = 0;
6713
Mike Travisf6630112009-11-17 18:22:15 -06006714 if (!sched_domain_debug_enabled)
6715 return;
6716
Nick Piggin41c7ce92005-06-25 14:57:24 -07006717 if (!sd) {
6718 printk(KERN_DEBUG "CPU%d attaching NULL sched-domain.\n", cpu);
6719 return;
6720 }
6721
Linus Torvalds1da177e2005-04-16 15:20:36 -07006722 printk(KERN_DEBUG "CPU%d attaching sched-domain:\n", cpu);
6723
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02006724 for (;;) {
Peter Zijlstra4cb98832011-04-07 14:09:58 +02006725 if (sched_domain_debug_one(sd, cpu, level, sched_domains_tmpmask))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006726 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006727 level++;
6728 sd = sd->parent;
Miguel Ojeda Sandonis33859f72006-12-10 02:20:38 -08006729 if (!sd)
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02006730 break;
6731 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006732}
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +02006733#else /* !CONFIG_SCHED_DEBUG */
Ingo Molnar48f24c42006-07-03 00:25:40 -07006734# define sched_domain_debug(sd, cpu) do { } while (0)
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +02006735#endif /* CONFIG_SCHED_DEBUG */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006736
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07006737static int sd_degenerate(struct sched_domain *sd)
Suresh Siddha245af2c2005-06-25 14:57:25 -07006738{
Rusty Russell758b2cd2008-11-25 02:35:04 +10306739 if (cpumask_weight(sched_domain_span(sd)) == 1)
Suresh Siddha245af2c2005-06-25 14:57:25 -07006740 return 1;
6741
6742 /* Following flags need at least 2 groups */
6743 if (sd->flags & (SD_LOAD_BALANCE |
6744 SD_BALANCE_NEWIDLE |
6745 SD_BALANCE_FORK |
Siddha, Suresh B89c47102006-10-03 01:14:09 -07006746 SD_BALANCE_EXEC |
6747 SD_SHARE_CPUPOWER |
6748 SD_SHARE_PKG_RESOURCES)) {
Suresh Siddha245af2c2005-06-25 14:57:25 -07006749 if (sd->groups != sd->groups->next)
6750 return 0;
6751 }
6752
6753 /* Following flags don't use groups */
Peter Zijlstrac88d5912009-09-10 13:50:02 +02006754 if (sd->flags & (SD_WAKE_AFFINE))
Suresh Siddha245af2c2005-06-25 14:57:25 -07006755 return 0;
6756
6757 return 1;
6758}
6759
Ingo Molnar48f24c42006-07-03 00:25:40 -07006760static int
6761sd_parent_degenerate(struct sched_domain *sd, struct sched_domain *parent)
Suresh Siddha245af2c2005-06-25 14:57:25 -07006762{
6763 unsigned long cflags = sd->flags, pflags = parent->flags;
6764
6765 if (sd_degenerate(parent))
6766 return 1;
6767
Rusty Russell758b2cd2008-11-25 02:35:04 +10306768 if (!cpumask_equal(sched_domain_span(sd), sched_domain_span(parent)))
Suresh Siddha245af2c2005-06-25 14:57:25 -07006769 return 0;
6770
Suresh Siddha245af2c2005-06-25 14:57:25 -07006771 /* Flags needing groups don't count if only 1 group in parent */
6772 if (parent->groups == parent->groups->next) {
6773 pflags &= ~(SD_LOAD_BALANCE |
6774 SD_BALANCE_NEWIDLE |
6775 SD_BALANCE_FORK |
Siddha, Suresh B89c47102006-10-03 01:14:09 -07006776 SD_BALANCE_EXEC |
6777 SD_SHARE_CPUPOWER |
6778 SD_SHARE_PKG_RESOURCES);
Ken Chen54364992008-12-07 18:47:37 -08006779 if (nr_node_ids == 1)
6780 pflags &= ~SD_SERIALIZE;
Suresh Siddha245af2c2005-06-25 14:57:25 -07006781 }
6782 if (~cflags & pflags)
6783 return 0;
6784
6785 return 1;
6786}
6787
Peter Zijlstradce840a2011-04-07 14:09:50 +02006788static void free_rootdomain(struct rcu_head *rcu)
Rusty Russellc6c49272008-11-25 02:35:05 +10306789{
Peter Zijlstradce840a2011-04-07 14:09:50 +02006790 struct root_domain *rd = container_of(rcu, struct root_domain, rcu);
Peter Zijlstra047106a2009-11-16 10:28:09 +01006791
Rusty Russell68e74562008-11-25 02:35:13 +10306792 cpupri_cleanup(&rd->cpupri);
Rusty Russellc6c49272008-11-25 02:35:05 +10306793 free_cpumask_var(rd->rto_mask);
6794 free_cpumask_var(rd->online);
6795 free_cpumask_var(rd->span);
6796 kfree(rd);
6797}
6798
Gregory Haskins57d885f2008-01-25 21:08:18 +01006799static void rq_attach_root(struct rq *rq, struct root_domain *rd)
6800{
Ingo Molnara0490fa2009-02-12 11:35:40 +01006801 struct root_domain *old_rd = NULL;
Gregory Haskins57d885f2008-01-25 21:08:18 +01006802 unsigned long flags;
Gregory Haskins57d885f2008-01-25 21:08:18 +01006803
Thomas Gleixner05fa7852009-11-17 14:28:38 +01006804 raw_spin_lock_irqsave(&rq->lock, flags);
Gregory Haskins57d885f2008-01-25 21:08:18 +01006805
6806 if (rq->rd) {
Ingo Molnara0490fa2009-02-12 11:35:40 +01006807 old_rd = rq->rd;
Gregory Haskins57d885f2008-01-25 21:08:18 +01006808
Rusty Russellc6c49272008-11-25 02:35:05 +10306809 if (cpumask_test_cpu(rq->cpu, old_rd->online))
Gregory Haskins1f11eb62008-06-04 15:04:05 -04006810 set_rq_offline(rq);
Gregory Haskins57d885f2008-01-25 21:08:18 +01006811
Rusty Russellc6c49272008-11-25 02:35:05 +10306812 cpumask_clear_cpu(rq->cpu, old_rd->span);
Gregory Haskinsdc938522008-01-25 21:08:26 +01006813
Ingo Molnara0490fa2009-02-12 11:35:40 +01006814 /*
6815 * If we dont want to free the old_rt yet then
6816 * set old_rd to NULL to skip the freeing later
6817 * in this function:
6818 */
6819 if (!atomic_dec_and_test(&old_rd->refcount))
6820 old_rd = NULL;
Gregory Haskins57d885f2008-01-25 21:08:18 +01006821 }
6822
6823 atomic_inc(&rd->refcount);
6824 rq->rd = rd;
6825
Rusty Russellc6c49272008-11-25 02:35:05 +10306826 cpumask_set_cpu(rq->cpu, rd->span);
Gregory Haskins00aec932009-07-30 10:57:23 -04006827 if (cpumask_test_cpu(rq->cpu, cpu_active_mask))
Gregory Haskins1f11eb62008-06-04 15:04:05 -04006828 set_rq_online(rq);
Gregory Haskins57d885f2008-01-25 21:08:18 +01006829
Thomas Gleixner05fa7852009-11-17 14:28:38 +01006830 raw_spin_unlock_irqrestore(&rq->lock, flags);
Ingo Molnara0490fa2009-02-12 11:35:40 +01006831
6832 if (old_rd)
Peter Zijlstradce840a2011-04-07 14:09:50 +02006833 call_rcu_sched(&old_rd->rcu, free_rootdomain);
Gregory Haskins57d885f2008-01-25 21:08:18 +01006834}
6835
Pekka Enberg68c38fc2010-07-15 23:18:22 +03006836static int init_rootdomain(struct root_domain *rd)
Gregory Haskins57d885f2008-01-25 21:08:18 +01006837{
6838 memset(rd, 0, sizeof(*rd));
6839
Pekka Enberg68c38fc2010-07-15 23:18:22 +03006840 if (!alloc_cpumask_var(&rd->span, GFP_KERNEL))
Li Zefan0c910d22009-01-06 17:39:06 +08006841 goto out;
Pekka Enberg68c38fc2010-07-15 23:18:22 +03006842 if (!alloc_cpumask_var(&rd->online, GFP_KERNEL))
Rusty Russellc6c49272008-11-25 02:35:05 +10306843 goto free_span;
Pekka Enberg68c38fc2010-07-15 23:18:22 +03006844 if (!alloc_cpumask_var(&rd->rto_mask, GFP_KERNEL))
Rusty Russellc6c49272008-11-25 02:35:05 +10306845 goto free_online;
Gregory Haskins6e0534f2008-05-12 21:21:01 +02006846
Pekka Enberg68c38fc2010-07-15 23:18:22 +03006847 if (cpupri_init(&rd->cpupri) != 0)
Rusty Russell68e74562008-11-25 02:35:13 +10306848 goto free_rto_mask;
Rusty Russellc6c49272008-11-25 02:35:05 +10306849 return 0;
6850
Rusty Russell68e74562008-11-25 02:35:13 +10306851free_rto_mask:
6852 free_cpumask_var(rd->rto_mask);
Rusty Russellc6c49272008-11-25 02:35:05 +10306853free_online:
6854 free_cpumask_var(rd->online);
6855free_span:
6856 free_cpumask_var(rd->span);
Li Zefan0c910d22009-01-06 17:39:06 +08006857out:
Rusty Russellc6c49272008-11-25 02:35:05 +10306858 return -ENOMEM;
Gregory Haskins57d885f2008-01-25 21:08:18 +01006859}
6860
6861static void init_defrootdomain(void)
6862{
Pekka Enberg68c38fc2010-07-15 23:18:22 +03006863 init_rootdomain(&def_root_domain);
Rusty Russellc6c49272008-11-25 02:35:05 +10306864
Gregory Haskins57d885f2008-01-25 21:08:18 +01006865 atomic_set(&def_root_domain.refcount, 1);
6866}
6867
Gregory Haskinsdc938522008-01-25 21:08:26 +01006868static struct root_domain *alloc_rootdomain(void)
Gregory Haskins57d885f2008-01-25 21:08:18 +01006869{
6870 struct root_domain *rd;
6871
6872 rd = kmalloc(sizeof(*rd), GFP_KERNEL);
6873 if (!rd)
6874 return NULL;
6875
Pekka Enberg68c38fc2010-07-15 23:18:22 +03006876 if (init_rootdomain(rd) != 0) {
Rusty Russellc6c49272008-11-25 02:35:05 +10306877 kfree(rd);
6878 return NULL;
6879 }
Gregory Haskins57d885f2008-01-25 21:08:18 +01006880
6881 return rd;
6882}
6883
Peter Zijlstrae3589f62011-07-15 10:35:52 +02006884static void free_sched_groups(struct sched_group *sg, int free_sgp)
6885{
6886 struct sched_group *tmp, *first;
6887
6888 if (!sg)
6889 return;
6890
6891 first = sg;
6892 do {
6893 tmp = sg->next;
6894
6895 if (free_sgp && atomic_dec_and_test(&sg->sgp->ref))
6896 kfree(sg->sgp);
6897
6898 kfree(sg);
6899 sg = tmp;
6900 } while (sg != first);
6901}
6902
Peter Zijlstradce840a2011-04-07 14:09:50 +02006903static void free_sched_domain(struct rcu_head *rcu)
6904{
6905 struct sched_domain *sd = container_of(rcu, struct sched_domain, rcu);
Peter Zijlstrae3589f62011-07-15 10:35:52 +02006906
6907 /*
6908 * If its an overlapping domain it has private groups, iterate and
6909 * nuke them all.
6910 */
6911 if (sd->flags & SD_OVERLAP) {
6912 free_sched_groups(sd->groups, 1);
6913 } else if (atomic_dec_and_test(&sd->groups->ref)) {
Peter Zijlstra9c3f75c2011-07-14 13:00:06 +02006914 kfree(sd->groups->sgp);
Peter Zijlstradce840a2011-04-07 14:09:50 +02006915 kfree(sd->groups);
Peter Zijlstra9c3f75c2011-07-14 13:00:06 +02006916 }
Peter Zijlstradce840a2011-04-07 14:09:50 +02006917 kfree(sd);
6918}
6919
6920static void destroy_sched_domain(struct sched_domain *sd, int cpu)
6921{
6922 call_rcu(&sd->rcu, free_sched_domain);
6923}
6924
6925static void destroy_sched_domains(struct sched_domain *sd, int cpu)
6926{
6927 for (; sd; sd = sd->parent)
6928 destroy_sched_domain(sd, cpu);
6929}
6930
Linus Torvalds1da177e2005-04-16 15:20:36 -07006931/*
Ingo Molnar0eab9142008-01-25 21:08:19 +01006932 * Attach the domain 'sd' to 'cpu' as its base domain. Callers must
Linus Torvalds1da177e2005-04-16 15:20:36 -07006933 * hold the hotplug lock.
6934 */
Ingo Molnar0eab9142008-01-25 21:08:19 +01006935static void
6936cpu_attach_domain(struct sched_domain *sd, struct root_domain *rd, int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006937{
Ingo Molnar70b97a72006-07-03 00:25:42 -07006938 struct rq *rq = cpu_rq(cpu);
Suresh Siddha245af2c2005-06-25 14:57:25 -07006939 struct sched_domain *tmp;
6940
6941 /* Remove the sched domains which do not contribute to scheduling. */
Li Zefanf29c9b12008-11-06 09:45:16 +08006942 for (tmp = sd; tmp; ) {
Suresh Siddha245af2c2005-06-25 14:57:25 -07006943 struct sched_domain *parent = tmp->parent;
6944 if (!parent)
6945 break;
Li Zefanf29c9b12008-11-06 09:45:16 +08006946
Siddha, Suresh B1a848872006-10-03 01:14:08 -07006947 if (sd_parent_degenerate(tmp, parent)) {
Suresh Siddha245af2c2005-06-25 14:57:25 -07006948 tmp->parent = parent->parent;
Siddha, Suresh B1a848872006-10-03 01:14:08 -07006949 if (parent->parent)
6950 parent->parent->child = tmp;
Peter Zijlstradce840a2011-04-07 14:09:50 +02006951 destroy_sched_domain(parent, cpu);
Li Zefanf29c9b12008-11-06 09:45:16 +08006952 } else
6953 tmp = tmp->parent;
Suresh Siddha245af2c2005-06-25 14:57:25 -07006954 }
6955
Siddha, Suresh B1a848872006-10-03 01:14:08 -07006956 if (sd && sd_degenerate(sd)) {
Peter Zijlstradce840a2011-04-07 14:09:50 +02006957 tmp = sd;
Suresh Siddha245af2c2005-06-25 14:57:25 -07006958 sd = sd->parent;
Peter Zijlstradce840a2011-04-07 14:09:50 +02006959 destroy_sched_domain(tmp, cpu);
Siddha, Suresh B1a848872006-10-03 01:14:08 -07006960 if (sd)
6961 sd->child = NULL;
6962 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006963
Peter Zijlstra4cb98832011-04-07 14:09:58 +02006964 sched_domain_debug(sd, cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006965
Gregory Haskins57d885f2008-01-25 21:08:18 +01006966 rq_attach_root(rq, rd);
Peter Zijlstradce840a2011-04-07 14:09:50 +02006967 tmp = rq->sd;
Nick Piggin674311d2005-06-25 14:57:27 -07006968 rcu_assign_pointer(rq->sd, sd);
Peter Zijlstradce840a2011-04-07 14:09:50 +02006969 destroy_sched_domains(tmp, cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006970}
6971
6972/* cpus with isolated domains */
Rusty Russelldcc30a32008-11-25 02:35:12 +10306973static cpumask_var_t cpu_isolated_map;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006974
6975/* Setup the mask of cpus configured for isolated domains */
6976static int __init isolated_cpu_setup(char *str)
6977{
Rusty Russellbdddd292009-12-02 14:09:16 +10306978 alloc_bootmem_cpumask_var(&cpu_isolated_map);
Rusty Russell968ea6d2008-12-13 21:55:51 +10306979 cpulist_parse(str, cpu_isolated_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006980 return 1;
6981}
6982
Ingo Molnar8927f492007-10-15 17:00:13 +02006983__setup("isolcpus=", isolated_cpu_setup);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006984
John Hawkes9c1cfda2005-09-06 15:18:14 -07006985#define SD_NODES_PER_DOMAIN 16
Linus Torvalds1da177e2005-04-16 15:20:36 -07006986
John Hawkes9c1cfda2005-09-06 15:18:14 -07006987#ifdef CONFIG_NUMA
akpm@osdl.org198e2f12006-01-12 01:05:30 -08006988
John Hawkes9c1cfda2005-09-06 15:18:14 -07006989/**
6990 * find_next_best_node - find the next node to include in a sched_domain
6991 * @node: node whose sched_domain we're building
6992 * @used_nodes: nodes already in the sched_domain
6993 *
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01006994 * Find the next node to include in a given scheduling domain. Simply
John Hawkes9c1cfda2005-09-06 15:18:14 -07006995 * finds the closest node not already in the @used_nodes map.
6996 *
6997 * Should use nodemask_t.
6998 */
Mike Travisc5f59f02008-04-04 18:11:10 -07006999static int find_next_best_node(int node, nodemask_t *used_nodes)
John Hawkes9c1cfda2005-09-06 15:18:14 -07007000{
Hillf Danton7142d172011-05-05 20:53:20 +08007001 int i, n, val, min_val, best_node = -1;
John Hawkes9c1cfda2005-09-06 15:18:14 -07007002
7003 min_val = INT_MAX;
7004
Mike Travis076ac2a2008-05-12 21:21:12 +02007005 for (i = 0; i < nr_node_ids; i++) {
John Hawkes9c1cfda2005-09-06 15:18:14 -07007006 /* Start at @node */
Mike Travis076ac2a2008-05-12 21:21:12 +02007007 n = (node + i) % nr_node_ids;
John Hawkes9c1cfda2005-09-06 15:18:14 -07007008
7009 if (!nr_cpus_node(n))
7010 continue;
7011
7012 /* Skip already used nodes */
Mike Travisc5f59f02008-04-04 18:11:10 -07007013 if (node_isset(n, *used_nodes))
John Hawkes9c1cfda2005-09-06 15:18:14 -07007014 continue;
7015
7016 /* Simple min distance search */
7017 val = node_distance(node, n);
7018
7019 if (val < min_val) {
7020 min_val = val;
7021 best_node = n;
7022 }
7023 }
7024
Hillf Danton7142d172011-05-05 20:53:20 +08007025 if (best_node != -1)
7026 node_set(best_node, *used_nodes);
John Hawkes9c1cfda2005-09-06 15:18:14 -07007027 return best_node;
7028}
7029
7030/**
7031 * sched_domain_node_span - get a cpumask for a node's sched_domain
7032 * @node: node whose cpumask we're constructing
Randy Dunlap73486722008-04-22 10:07:22 -07007033 * @span: resulting cpumask
John Hawkes9c1cfda2005-09-06 15:18:14 -07007034 *
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01007035 * Given a node, construct a good cpumask for its sched_domain to span. It
John Hawkes9c1cfda2005-09-06 15:18:14 -07007036 * should be one that prevents unnecessary balancing, but also spreads tasks
7037 * out optimally.
7038 */
Rusty Russell96f874e2008-11-25 02:35:14 +10307039static void sched_domain_node_span(int node, struct cpumask *span)
John Hawkes9c1cfda2005-09-06 15:18:14 -07007040{
Mike Travisc5f59f02008-04-04 18:11:10 -07007041 nodemask_t used_nodes;
Ingo Molnar48f24c42006-07-03 00:25:40 -07007042 int i;
John Hawkes9c1cfda2005-09-06 15:18:14 -07007043
Mike Travis6ca09df2008-12-31 18:08:45 -08007044 cpumask_clear(span);
Mike Travisc5f59f02008-04-04 18:11:10 -07007045 nodes_clear(used_nodes);
John Hawkes9c1cfda2005-09-06 15:18:14 -07007046
Mike Travis6ca09df2008-12-31 18:08:45 -08007047 cpumask_or(span, span, cpumask_of_node(node));
Mike Travisc5f59f02008-04-04 18:11:10 -07007048 node_set(node, used_nodes);
John Hawkes9c1cfda2005-09-06 15:18:14 -07007049
7050 for (i = 1; i < SD_NODES_PER_DOMAIN; i++) {
Mike Travisc5f59f02008-04-04 18:11:10 -07007051 int next_node = find_next_best_node(node, &used_nodes);
Hillf Danton7142d172011-05-05 20:53:20 +08007052 if (next_node < 0)
7053 break;
Mike Travis6ca09df2008-12-31 18:08:45 -08007054 cpumask_or(span, span, cpumask_of_node(next_node));
John Hawkes9c1cfda2005-09-06 15:18:14 -07007055 }
John Hawkes9c1cfda2005-09-06 15:18:14 -07007056}
Peter Zijlstrad3081f52011-04-07 14:09:59 +02007057
7058static const struct cpumask *cpu_node_mask(int cpu)
7059{
7060 lockdep_assert_held(&sched_domains_mutex);
7061
7062 sched_domain_node_span(cpu_to_node(cpu), sched_domains_tmpmask);
7063
7064 return sched_domains_tmpmask;
7065}
Peter Zijlstra2c402dc32011-04-07 14:10:01 +02007066
7067static const struct cpumask *cpu_allnodes_mask(int cpu)
7068{
7069 return cpu_possible_mask;
7070}
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +02007071#endif /* CONFIG_NUMA */
John Hawkes9c1cfda2005-09-06 15:18:14 -07007072
Peter Zijlstrad3081f52011-04-07 14:09:59 +02007073static const struct cpumask *cpu_cpu_mask(int cpu)
7074{
7075 return cpumask_of_node(cpu_to_node(cpu));
7076}
7077
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07007078int sched_smt_power_savings = 0, sched_mc_power_savings = 0;
Ingo Molnar48f24c42006-07-03 00:25:40 -07007079
Peter Zijlstradce840a2011-04-07 14:09:50 +02007080struct sd_data {
7081 struct sched_domain **__percpu sd;
7082 struct sched_group **__percpu sg;
Peter Zijlstra9c3f75c2011-07-14 13:00:06 +02007083 struct sched_group_power **__percpu sgp;
Peter Zijlstradce840a2011-04-07 14:09:50 +02007084};
7085
Andreas Herrmann49a02c52009-08-18 12:51:52 +02007086struct s_data {
Peter Zijlstra21d42cc2011-04-07 14:09:48 +02007087 struct sched_domain ** __percpu sd;
Andreas Herrmann49a02c52009-08-18 12:51:52 +02007088 struct root_domain *rd;
7089};
7090
Andreas Herrmann2109b992009-08-18 12:53:00 +02007091enum s_alloc {
Andreas Herrmann2109b992009-08-18 12:53:00 +02007092 sa_rootdomain,
Peter Zijlstra21d42cc2011-04-07 14:09:48 +02007093 sa_sd,
Peter Zijlstradce840a2011-04-07 14:09:50 +02007094 sa_sd_storage,
Andreas Herrmann2109b992009-08-18 12:53:00 +02007095 sa_none,
7096};
7097
Peter Zijlstra54ab4ff2011-04-07 14:10:03 +02007098struct sched_domain_topology_level;
7099
7100typedef struct sched_domain *(*sched_domain_init_f)(struct sched_domain_topology_level *tl, int cpu);
Peter Zijlstraeb7a74e2011-04-07 14:10:00 +02007101typedef const struct cpumask *(*sched_domain_mask_f)(int cpu);
7102
Peter Zijlstrae3589f62011-07-15 10:35:52 +02007103#define SDTL_OVERLAP 0x01
7104
Peter Zijlstraeb7a74e2011-04-07 14:10:00 +02007105struct sched_domain_topology_level {
Peter Zijlstra2c402dc32011-04-07 14:10:01 +02007106 sched_domain_init_f init;
7107 sched_domain_mask_f mask;
Peter Zijlstrae3589f62011-07-15 10:35:52 +02007108 int flags;
Peter Zijlstra54ab4ff2011-04-07 14:10:03 +02007109 struct sd_data data;
Peter Zijlstraeb7a74e2011-04-07 14:10:00 +02007110};
7111
Peter Zijlstrae3589f62011-07-15 10:35:52 +02007112static int
7113build_overlap_sched_groups(struct sched_domain *sd, int cpu)
7114{
7115 struct sched_group *first = NULL, *last = NULL, *groups = NULL, *sg;
7116 const struct cpumask *span = sched_domain_span(sd);
7117 struct cpumask *covered = sched_domains_tmpmask;
7118 struct sd_data *sdd = sd->private;
7119 struct sched_domain *child;
7120 int i;
7121
7122 cpumask_clear(covered);
7123
7124 for_each_cpu(i, span) {
7125 struct cpumask *sg_span;
7126
7127 if (cpumask_test_cpu(i, covered))
7128 continue;
7129
7130 sg = kzalloc_node(sizeof(struct sched_group) + cpumask_size(),
7131 GFP_KERNEL, cpu_to_node(i));
7132
7133 if (!sg)
7134 goto fail;
7135
7136 sg_span = sched_group_cpus(sg);
7137
7138 child = *per_cpu_ptr(sdd->sd, i);
7139 if (child->child) {
7140 child = child->child;
7141 cpumask_copy(sg_span, sched_domain_span(child));
7142 } else
7143 cpumask_set_cpu(i, sg_span);
7144
7145 cpumask_or(covered, covered, sg_span);
7146
7147 sg->sgp = *per_cpu_ptr(sdd->sgp, cpumask_first(sg_span));
7148 atomic_inc(&sg->sgp->ref);
7149
7150 if (cpumask_test_cpu(cpu, sg_span))
7151 groups = sg;
7152
7153 if (!first)
7154 first = sg;
7155 if (last)
7156 last->next = sg;
7157 last = sg;
7158 last->next = first;
7159 }
7160 sd->groups = groups;
7161
7162 return 0;
7163
7164fail:
7165 free_sched_groups(first, 0);
7166
7167 return -ENOMEM;
7168}
7169
Peter Zijlstradce840a2011-04-07 14:09:50 +02007170static int get_group(int cpu, struct sd_data *sdd, struct sched_group **sg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007171{
Peter Zijlstradce840a2011-04-07 14:09:50 +02007172 struct sched_domain *sd = *per_cpu_ptr(sdd->sd, cpu);
7173 struct sched_domain *child = sd->child;
7174
7175 if (child)
7176 cpu = cpumask_first(sched_domain_span(child));
7177
Peter Zijlstra9c3f75c2011-07-14 13:00:06 +02007178 if (sg) {
Peter Zijlstradce840a2011-04-07 14:09:50 +02007179 *sg = *per_cpu_ptr(sdd->sg, cpu);
Peter Zijlstra9c3f75c2011-07-14 13:00:06 +02007180 (*sg)->sgp = *per_cpu_ptr(sdd->sgp, cpu);
Peter Zijlstrae3589f62011-07-15 10:35:52 +02007181 atomic_set(&(*sg)->sgp->ref, 1); /* for claim_allocations */
Peter Zijlstra9c3f75c2011-07-14 13:00:06 +02007182 }
Peter Zijlstradce840a2011-04-07 14:09:50 +02007183
Linus Torvalds1da177e2005-04-16 15:20:36 -07007184 return cpu;
7185}
Linus Torvalds1da177e2005-04-16 15:20:36 -07007186
Ingo Molnar48f24c42006-07-03 00:25:40 -07007187/*
Peter Zijlstradce840a2011-04-07 14:09:50 +02007188 * build_sched_groups will build a circular linked list of the groups
7189 * covered by the given span, and will set each group's ->cpumask correctly,
7190 * and ->cpu_power to 0.
Peter Zijlstrae3589f62011-07-15 10:35:52 +02007191 *
7192 * Assumes the sched_domain tree is fully constructed
Ingo Molnar48f24c42006-07-03 00:25:40 -07007193 */
Peter Zijlstrae3589f62011-07-15 10:35:52 +02007194static int
7195build_sched_groups(struct sched_domain *sd, int cpu)
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08007196{
Peter Zijlstradce840a2011-04-07 14:09:50 +02007197 struct sched_group *first = NULL, *last = NULL;
7198 struct sd_data *sdd = sd->private;
7199 const struct cpumask *span = sched_domain_span(sd);
Peter Zijlstraf96225f2011-04-07 14:09:57 +02007200 struct cpumask *covered;
Peter Zijlstradce840a2011-04-07 14:09:50 +02007201 int i;
7202
Peter Zijlstrae3589f62011-07-15 10:35:52 +02007203 get_group(cpu, sdd, &sd->groups);
7204 atomic_inc(&sd->groups->ref);
7205
7206 if (cpu != cpumask_first(sched_domain_span(sd)))
7207 return 0;
7208
Peter Zijlstraf96225f2011-04-07 14:09:57 +02007209 lockdep_assert_held(&sched_domains_mutex);
7210 covered = sched_domains_tmpmask;
7211
Peter Zijlstradce840a2011-04-07 14:09:50 +02007212 cpumask_clear(covered);
7213
7214 for_each_cpu(i, span) {
7215 struct sched_group *sg;
7216 int group = get_group(i, sdd, &sg);
7217 int j;
7218
7219 if (cpumask_test_cpu(i, covered))
7220 continue;
7221
7222 cpumask_clear(sched_group_cpus(sg));
Peter Zijlstra9c3f75c2011-07-14 13:00:06 +02007223 sg->sgp->power = 0;
Peter Zijlstradce840a2011-04-07 14:09:50 +02007224
7225 for_each_cpu(j, span) {
7226 if (get_group(j, sdd, NULL) != group)
7227 continue;
7228
7229 cpumask_set_cpu(j, covered);
7230 cpumask_set_cpu(j, sched_group_cpus(sg));
7231 }
7232
7233 if (!first)
7234 first = sg;
7235 if (last)
7236 last->next = sg;
7237 last = sg;
7238 }
7239 last->next = first;
Peter Zijlstrae3589f62011-07-15 10:35:52 +02007240
7241 return 0;
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08007242}
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07007243
Linus Torvalds1da177e2005-04-16 15:20:36 -07007244/*
Siddha, Suresh B89c47102006-10-03 01:14:09 -07007245 * Initialize sched groups cpu_power.
7246 *
7247 * cpu_power indicates the capacity of sched group, which is used while
7248 * distributing the load between different sched groups in a sched domain.
7249 * Typically cpu_power for all the groups in a sched domain will be same unless
7250 * there are asymmetries in the topology. If there are asymmetries, group
7251 * having more cpu_power will pickup more load compared to the group having
7252 * less cpu_power.
Siddha, Suresh B89c47102006-10-03 01:14:09 -07007253 */
7254static void init_sched_groups_power(int cpu, struct sched_domain *sd)
7255{
Peter Zijlstrae3589f62011-07-15 10:35:52 +02007256 struct sched_group *sg = sd->groups;
Siddha, Suresh B89c47102006-10-03 01:14:09 -07007257
Peter Zijlstrae3589f62011-07-15 10:35:52 +02007258 WARN_ON(!sd || !sg);
7259
7260 do {
7261 sg->group_weight = cpumask_weight(sched_group_cpus(sg));
7262 sg = sg->next;
7263 } while (sg != sd->groups);
7264
7265 if (cpu != group_first_cpu(sg))
Siddha, Suresh B89c47102006-10-03 01:14:09 -07007266 return;
7267
Peter Zijlstrad274cb32011-04-07 14:09:43 +02007268 update_group_power(sd, cpu);
Siddha, Suresh B89c47102006-10-03 01:14:09 -07007269}
7270
7271/*
Mike Travis7c16ec52008-04-04 18:11:11 -07007272 * Initializers for schedule domains
7273 * Non-inlined to reduce accumulated stack pressure in build_sched_domains()
7274 */
7275
Ingo Molnara5d8c342008-10-09 11:35:51 +02007276#ifdef CONFIG_SCHED_DEBUG
7277# define SD_INIT_NAME(sd, type) sd->name = #type
7278#else
7279# define SD_INIT_NAME(sd, type) do { } while (0)
7280#endif
7281
Peter Zijlstra54ab4ff2011-04-07 14:10:03 +02007282#define SD_INIT_FUNC(type) \
7283static noinline struct sched_domain * \
7284sd_init_##type(struct sched_domain_topology_level *tl, int cpu) \
7285{ \
7286 struct sched_domain *sd = *per_cpu_ptr(tl->data.sd, cpu); \
7287 *sd = SD_##type##_INIT; \
Peter Zijlstra54ab4ff2011-04-07 14:10:03 +02007288 SD_INIT_NAME(sd, type); \
7289 sd->private = &tl->data; \
7290 return sd; \
Mike Travis7c16ec52008-04-04 18:11:11 -07007291}
7292
7293SD_INIT_FUNC(CPU)
7294#ifdef CONFIG_NUMA
7295 SD_INIT_FUNC(ALLNODES)
7296 SD_INIT_FUNC(NODE)
7297#endif
7298#ifdef CONFIG_SCHED_SMT
7299 SD_INIT_FUNC(SIBLING)
7300#endif
7301#ifdef CONFIG_SCHED_MC
7302 SD_INIT_FUNC(MC)
7303#endif
Heiko Carstens01a08542010-08-31 10:28:16 +02007304#ifdef CONFIG_SCHED_BOOK
7305 SD_INIT_FUNC(BOOK)
7306#endif
Mike Travis7c16ec52008-04-04 18:11:11 -07007307
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09007308static int default_relax_domain_level = -1;
Peter Zijlstra60495e72011-04-07 14:10:04 +02007309int sched_domain_level_max;
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09007310
7311static int __init setup_relax_domain_level(char *str)
7312{
Li Zefan30e0e172008-05-13 10:27:17 +08007313 unsigned long val;
7314
7315 val = simple_strtoul(str, NULL, 0);
Peter Zijlstra60495e72011-04-07 14:10:04 +02007316 if (val < sched_domain_level_max)
Li Zefan30e0e172008-05-13 10:27:17 +08007317 default_relax_domain_level = val;
7318
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09007319 return 1;
7320}
7321__setup("relax_domain_level=", setup_relax_domain_level);
7322
7323static void set_domain_attribute(struct sched_domain *sd,
7324 struct sched_domain_attr *attr)
7325{
7326 int request;
7327
7328 if (!attr || attr->relax_domain_level < 0) {
7329 if (default_relax_domain_level < 0)
7330 return;
7331 else
7332 request = default_relax_domain_level;
7333 } else
7334 request = attr->relax_domain_level;
7335 if (request < sd->level) {
7336 /* turn off 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 } else {
7339 /* turn on idle balance on this domain */
Peter Zijlstrac88d5912009-09-10 13:50:02 +02007340 sd->flags |= (SD_BALANCE_WAKE|SD_BALANCE_NEWIDLE);
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09007341 }
7342}
7343
Peter Zijlstra54ab4ff2011-04-07 14:10:03 +02007344static void __sdt_free(const struct cpumask *cpu_map);
7345static int __sdt_alloc(const struct cpumask *cpu_map);
7346
Andreas Herrmann2109b992009-08-18 12:53:00 +02007347static void __free_domain_allocs(struct s_data *d, enum s_alloc what,
7348 const struct cpumask *cpu_map)
7349{
7350 switch (what) {
Andreas Herrmann2109b992009-08-18 12:53:00 +02007351 case sa_rootdomain:
Peter Zijlstra822ff792011-04-07 14:09:51 +02007352 if (!atomic_read(&d->rd->refcount))
7353 free_rootdomain(&d->rd->rcu); /* fall through */
Peter Zijlstra21d42cc2011-04-07 14:09:48 +02007354 case sa_sd:
7355 free_percpu(d->sd); /* fall through */
Peter Zijlstradce840a2011-04-07 14:09:50 +02007356 case sa_sd_storage:
Peter Zijlstra54ab4ff2011-04-07 14:10:03 +02007357 __sdt_free(cpu_map); /* fall through */
Andreas Herrmann2109b992009-08-18 12:53:00 +02007358 case sa_none:
7359 break;
7360 }
7361}
7362
7363static enum s_alloc __visit_domain_allocation_hell(struct s_data *d,
7364 const struct cpumask *cpu_map)
7365{
Peter Zijlstradce840a2011-04-07 14:09:50 +02007366 memset(d, 0, sizeof(*d));
7367
Peter Zijlstra54ab4ff2011-04-07 14:10:03 +02007368 if (__sdt_alloc(cpu_map))
7369 return sa_sd_storage;
Peter Zijlstra21d42cc2011-04-07 14:09:48 +02007370 d->sd = alloc_percpu(struct sched_domain *);
Peter Zijlstradce840a2011-04-07 14:09:50 +02007371 if (!d->sd)
7372 return sa_sd_storage;
Andreas Herrmann2109b992009-08-18 12:53:00 +02007373 d->rd = alloc_rootdomain();
Peter Zijlstradce840a2011-04-07 14:09:50 +02007374 if (!d->rd)
Peter Zijlstra21d42cc2011-04-07 14:09:48 +02007375 return sa_sd;
Andreas Herrmann2109b992009-08-18 12:53:00 +02007376 return sa_rootdomain;
7377}
7378
Peter Zijlstradce840a2011-04-07 14:09:50 +02007379/*
7380 * NULL the sd_data elements we've used to build the sched_domain and
7381 * sched_group structure so that the subsequent __free_domain_allocs()
7382 * will not free the data we're using.
7383 */
7384static void claim_allocations(int cpu, struct sched_domain *sd)
7385{
7386 struct sd_data *sdd = sd->private;
Peter Zijlstradce840a2011-04-07 14:09:50 +02007387
7388 WARN_ON_ONCE(*per_cpu_ptr(sdd->sd, cpu) != sd);
7389 *per_cpu_ptr(sdd->sd, cpu) = NULL;
7390
Peter Zijlstrae3589f62011-07-15 10:35:52 +02007391 if (atomic_read(&(*per_cpu_ptr(sdd->sg, cpu))->ref))
Peter Zijlstradce840a2011-04-07 14:09:50 +02007392 *per_cpu_ptr(sdd->sg, cpu) = NULL;
Peter Zijlstrae3589f62011-07-15 10:35:52 +02007393
7394 if (atomic_read(&(*per_cpu_ptr(sdd->sgp, cpu))->ref))
Peter Zijlstra9c3f75c2011-07-14 13:00:06 +02007395 *per_cpu_ptr(sdd->sgp, cpu) = NULL;
Peter Zijlstradce840a2011-04-07 14:09:50 +02007396}
7397
Andreas Herrmannd8173532009-08-18 12:57:03 +02007398#ifdef CONFIG_SCHED_SMT
Peter Zijlstra2c402dc32011-04-07 14:10:01 +02007399static const struct cpumask *cpu_smt_mask(int cpu)
7400{
7401 return topology_thread_cpumask(cpu);
Andreas Herrmannd8173532009-08-18 12:57:03 +02007402}
Peter Zijlstra2c402dc32011-04-07 14:10:01 +02007403#endif
Andreas Herrmannd8173532009-08-18 12:57:03 +02007404
Peter Zijlstrad069b912011-04-07 14:10:02 +02007405/*
7406 * Topology list, bottom-up.
7407 */
Peter Zijlstraeb7a74e2011-04-07 14:10:00 +02007408static struct sched_domain_topology_level default_topology[] = {
Peter Zijlstrad069b912011-04-07 14:10:02 +02007409#ifdef CONFIG_SCHED_SMT
7410 { sd_init_SIBLING, cpu_smt_mask, },
Peter Zijlstra2c402dc32011-04-07 14:10:01 +02007411#endif
7412#ifdef CONFIG_SCHED_MC
7413 { sd_init_MC, cpu_coregroup_mask, },
7414#endif
Peter Zijlstrad069b912011-04-07 14:10:02 +02007415#ifdef CONFIG_SCHED_BOOK
7416 { sd_init_BOOK, cpu_book_mask, },
7417#endif
7418 { sd_init_CPU, cpu_cpu_mask, },
7419#ifdef CONFIG_NUMA
Peter Zijlstrae3589f62011-07-15 10:35:52 +02007420 { sd_init_NODE, cpu_node_mask, SDTL_OVERLAP, },
Peter Zijlstrad069b912011-04-07 14:10:02 +02007421 { sd_init_ALLNODES, cpu_allnodes_mask, },
Peter Zijlstra2c402dc32011-04-07 14:10:01 +02007422#endif
Peter Zijlstraeb7a74e2011-04-07 14:10:00 +02007423 { NULL, },
7424};
7425
7426static struct sched_domain_topology_level *sched_domain_topology = default_topology;
7427
Peter Zijlstra54ab4ff2011-04-07 14:10:03 +02007428static int __sdt_alloc(const struct cpumask *cpu_map)
7429{
7430 struct sched_domain_topology_level *tl;
7431 int j;
7432
7433 for (tl = sched_domain_topology; tl->init; tl++) {
7434 struct sd_data *sdd = &tl->data;
7435
7436 sdd->sd = alloc_percpu(struct sched_domain *);
7437 if (!sdd->sd)
7438 return -ENOMEM;
7439
7440 sdd->sg = alloc_percpu(struct sched_group *);
7441 if (!sdd->sg)
7442 return -ENOMEM;
7443
Peter Zijlstra9c3f75c2011-07-14 13:00:06 +02007444 sdd->sgp = alloc_percpu(struct sched_group_power *);
7445 if (!sdd->sgp)
7446 return -ENOMEM;
7447
Peter Zijlstra54ab4ff2011-04-07 14:10:03 +02007448 for_each_cpu(j, cpu_map) {
7449 struct sched_domain *sd;
7450 struct sched_group *sg;
Peter Zijlstra9c3f75c2011-07-14 13:00:06 +02007451 struct sched_group_power *sgp;
Peter Zijlstra54ab4ff2011-04-07 14:10:03 +02007452
7453 sd = kzalloc_node(sizeof(struct sched_domain) + cpumask_size(),
7454 GFP_KERNEL, cpu_to_node(j));
7455 if (!sd)
7456 return -ENOMEM;
7457
7458 *per_cpu_ptr(sdd->sd, j) = sd;
7459
7460 sg = kzalloc_node(sizeof(struct sched_group) + cpumask_size(),
7461 GFP_KERNEL, cpu_to_node(j));
7462 if (!sg)
7463 return -ENOMEM;
7464
7465 *per_cpu_ptr(sdd->sg, j) = sg;
Peter Zijlstra9c3f75c2011-07-14 13:00:06 +02007466
7467 sgp = kzalloc_node(sizeof(struct sched_group_power),
7468 GFP_KERNEL, cpu_to_node(j));
7469 if (!sgp)
7470 return -ENOMEM;
7471
7472 *per_cpu_ptr(sdd->sgp, j) = sgp;
Peter Zijlstra54ab4ff2011-04-07 14:10:03 +02007473 }
7474 }
7475
7476 return 0;
7477}
7478
7479static void __sdt_free(const struct cpumask *cpu_map)
7480{
7481 struct sched_domain_topology_level *tl;
7482 int j;
7483
7484 for (tl = sched_domain_topology; tl->init; tl++) {
7485 struct sd_data *sdd = &tl->data;
7486
7487 for_each_cpu(j, cpu_map) {
Peter Zijlstrae3589f62011-07-15 10:35:52 +02007488 struct sched_domain *sd = *per_cpu_ptr(sdd->sd, j);
7489 if (sd && (sd->flags & SD_OVERLAP))
7490 free_sched_groups(sd->groups, 0);
Peter Zijlstra54ab4ff2011-04-07 14:10:03 +02007491 kfree(*per_cpu_ptr(sdd->sg, j));
Peter Zijlstra9c3f75c2011-07-14 13:00:06 +02007492 kfree(*per_cpu_ptr(sdd->sgp, j));
Peter Zijlstra54ab4ff2011-04-07 14:10:03 +02007493 }
7494 free_percpu(sdd->sd);
7495 free_percpu(sdd->sg);
Peter Zijlstra9c3f75c2011-07-14 13:00:06 +02007496 free_percpu(sdd->sgp);
Peter Zijlstra54ab4ff2011-04-07 14:10:03 +02007497 }
7498}
7499
Peter Zijlstra2c402dc32011-04-07 14:10:01 +02007500struct sched_domain *build_sched_domain(struct sched_domain_topology_level *tl,
7501 struct s_data *d, const struct cpumask *cpu_map,
Peter Zijlstrad069b912011-04-07 14:10:02 +02007502 struct sched_domain_attr *attr, struct sched_domain *child,
Peter Zijlstra2c402dc32011-04-07 14:10:01 +02007503 int cpu)
7504{
Peter Zijlstra54ab4ff2011-04-07 14:10:03 +02007505 struct sched_domain *sd = tl->init(tl, cpu);
Peter Zijlstra2c402dc32011-04-07 14:10:01 +02007506 if (!sd)
Peter Zijlstrad069b912011-04-07 14:10:02 +02007507 return child;
Peter Zijlstra2c402dc32011-04-07 14:10:01 +02007508
7509 set_domain_attribute(sd, attr);
7510 cpumask_and(sched_domain_span(sd), cpu_map, tl->mask(cpu));
Peter Zijlstra60495e72011-04-07 14:10:04 +02007511 if (child) {
7512 sd->level = child->level + 1;
7513 sched_domain_level_max = max(sched_domain_level_max, sd->level);
Peter Zijlstrad069b912011-04-07 14:10:02 +02007514 child->parent = sd;
Peter Zijlstra60495e72011-04-07 14:10:04 +02007515 }
Peter Zijlstrad069b912011-04-07 14:10:02 +02007516 sd->child = child;
Peter Zijlstra2c402dc32011-04-07 14:10:01 +02007517
7518 return sd;
7519}
7520
Mike Travis7c16ec52008-04-04 18:11:11 -07007521/*
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07007522 * Build sched domains for a given set of cpus and attach the sched domains
7523 * to the individual cpus
Linus Torvalds1da177e2005-04-16 15:20:36 -07007524 */
Peter Zijlstradce840a2011-04-07 14:09:50 +02007525static int build_sched_domains(const struct cpumask *cpu_map,
7526 struct sched_domain_attr *attr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007527{
Andreas Herrmann2109b992009-08-18 12:53:00 +02007528 enum s_alloc alloc_state = sa_none;
Peter Zijlstradce840a2011-04-07 14:09:50 +02007529 struct sched_domain *sd;
Andreas Herrmann49a02c52009-08-18 12:51:52 +02007530 struct s_data d;
Peter Zijlstra822ff792011-04-07 14:09:51 +02007531 int i, ret = -ENOMEM;
Rusty Russell3404c8d2008-11-25 02:35:03 +10307532
Andreas Herrmann2109b992009-08-18 12:53:00 +02007533 alloc_state = __visit_domain_allocation_hell(&d, cpu_map);
7534 if (alloc_state != sa_rootdomain)
7535 goto error;
Mike Travis7c16ec52008-04-04 18:11:11 -07007536
Peter Zijlstradce840a2011-04-07 14:09:50 +02007537 /* Set up domains for cpus specified by the cpu_map. */
Rusty Russellabcd0832008-11-25 02:35:02 +10307538 for_each_cpu(i, cpu_map) {
Peter Zijlstraeb7a74e2011-04-07 14:10:00 +02007539 struct sched_domain_topology_level *tl;
7540
Peter Zijlstra3bd65a82011-04-07 14:09:54 +02007541 sd = NULL;
Peter Zijlstrae3589f62011-07-15 10:35:52 +02007542 for (tl = sched_domain_topology; tl->init; tl++) {
Peter Zijlstra2c402dc32011-04-07 14:10:01 +02007543 sd = build_sched_domain(tl, &d, cpu_map, attr, sd, i);
Peter Zijlstrae3589f62011-07-15 10:35:52 +02007544 if (tl->flags & SDTL_OVERLAP || sched_feat(FORCE_SD_OVERLAP))
7545 sd->flags |= SD_OVERLAP;
Peter Zijlstrad1102352011-07-20 18:42:57 +02007546 if (cpumask_equal(cpu_map, sched_domain_span(sd)))
7547 break;
Peter Zijlstrae3589f62011-07-15 10:35:52 +02007548 }
Peter Zijlstrad274cb32011-04-07 14:09:43 +02007549
Peter Zijlstrad069b912011-04-07 14:10:02 +02007550 while (sd->child)
7551 sd = sd->child;
7552
Peter Zijlstra21d42cc2011-04-07 14:09:48 +02007553 *per_cpu_ptr(d.sd, i) = sd;
Peter Zijlstradce840a2011-04-07 14:09:50 +02007554 }
Peter Zijlstra21d42cc2011-04-07 14:09:48 +02007555
Peter Zijlstradce840a2011-04-07 14:09:50 +02007556 /* Build the groups for the domains */
7557 for_each_cpu(i, cpu_map) {
7558 for (sd = *per_cpu_ptr(d.sd, i); sd; sd = sd->parent) {
7559 sd->span_weight = cpumask_weight(sched_domain_span(sd));
Peter Zijlstrae3589f62011-07-15 10:35:52 +02007560 if (sd->flags & SD_OVERLAP) {
7561 if (build_overlap_sched_groups(sd, i))
7562 goto error;
7563 } else {
7564 if (build_sched_groups(sd, i))
7565 goto error;
7566 }
Peter Zijlstra1cf51902011-04-07 14:09:47 +02007567 }
Peter Zijlstraa06dadb2011-04-07 14:09:44 +02007568 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007569
Linus Torvalds1da177e2005-04-16 15:20:36 -07007570 /* Calculate CPU power for physical packages and nodes */
Peter Zijlstraa9c9a9b2011-04-07 14:09:49 +02007571 for (i = nr_cpumask_bits-1; i >= 0; i--) {
7572 if (!cpumask_test_cpu(i, cpu_map))
7573 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007574
Peter Zijlstradce840a2011-04-07 14:09:50 +02007575 for (sd = *per_cpu_ptr(d.sd, i); sd; sd = sd->parent) {
7576 claim_allocations(i, sd);
Peter Zijlstracd4ea6a2011-04-07 14:09:45 +02007577 init_sched_groups_power(i, sd);
Peter Zijlstradce840a2011-04-07 14:09:50 +02007578 }
Siddha, Suresh Bf712c0c2006-07-30 03:02:59 -07007579 }
John Hawkes9c1cfda2005-09-06 15:18:14 -07007580
Linus Torvalds1da177e2005-04-16 15:20:36 -07007581 /* Attach the domains */
Peter Zijlstradce840a2011-04-07 14:09:50 +02007582 rcu_read_lock();
Rusty Russellabcd0832008-11-25 02:35:02 +10307583 for_each_cpu(i, cpu_map) {
Peter Zijlstra21d42cc2011-04-07 14:09:48 +02007584 sd = *per_cpu_ptr(d.sd, i);
Andreas Herrmann49a02c52009-08-18 12:51:52 +02007585 cpu_attach_domain(sd, d.rd, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007586 }
Peter Zijlstradce840a2011-04-07 14:09:50 +02007587 rcu_read_unlock();
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07007588
Peter Zijlstra822ff792011-04-07 14:09:51 +02007589 ret = 0;
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07007590error:
Andreas Herrmann2109b992009-08-18 12:53:00 +02007591 __free_domain_allocs(&d, alloc_state, cpu_map);
Peter Zijlstra822ff792011-04-07 14:09:51 +02007592 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007593}
Paul Jackson029190c2007-10-18 23:40:20 -07007594
Rusty Russellacc3f5d2009-11-03 14:53:40 +10307595static cpumask_var_t *doms_cur; /* current sched domains */
Paul Jackson029190c2007-10-18 23:40:20 -07007596static int ndoms_cur; /* number of sched domains in 'doms_cur' */
Ingo Molnar4285f5942008-05-16 17:47:14 +02007597static struct sched_domain_attr *dattr_cur;
7598 /* attribues of custom domains in 'doms_cur' */
Paul Jackson029190c2007-10-18 23:40:20 -07007599
7600/*
7601 * Special case: If a kmalloc of a doms_cur partition (array of
Rusty Russell42128232008-11-25 02:35:12 +10307602 * cpumask) fails, then fallback to a single sched domain,
7603 * as determined by the single cpumask fallback_doms.
Paul Jackson029190c2007-10-18 23:40:20 -07007604 */
Rusty Russell42128232008-11-25 02:35:12 +10307605static cpumask_var_t fallback_doms;
Paul Jackson029190c2007-10-18 23:40:20 -07007606
Heiko Carstensee79d1b2008-12-09 18:49:50 +01007607/*
7608 * arch_update_cpu_topology lets virtualized architectures update the
7609 * cpu core maps. It is supposed to return 1 if the topology changed
7610 * or 0 if it stayed the same.
7611 */
7612int __attribute__((weak)) arch_update_cpu_topology(void)
Heiko Carstens22e52b02008-03-12 18:31:59 +01007613{
Heiko Carstensee79d1b2008-12-09 18:49:50 +01007614 return 0;
Heiko Carstens22e52b02008-03-12 18:31:59 +01007615}
7616
Rusty Russellacc3f5d2009-11-03 14:53:40 +10307617cpumask_var_t *alloc_sched_domains(unsigned int ndoms)
7618{
7619 int i;
7620 cpumask_var_t *doms;
7621
7622 doms = kmalloc(sizeof(*doms) * ndoms, GFP_KERNEL);
7623 if (!doms)
7624 return NULL;
7625 for (i = 0; i < ndoms; i++) {
7626 if (!alloc_cpumask_var(&doms[i], GFP_KERNEL)) {
7627 free_sched_domains(doms, i);
7628 return NULL;
7629 }
7630 }
7631 return doms;
7632}
7633
7634void free_sched_domains(cpumask_var_t doms[], unsigned int ndoms)
7635{
7636 unsigned int i;
7637 for (i = 0; i < ndoms; i++)
7638 free_cpumask_var(doms[i]);
7639 kfree(doms);
7640}
7641
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07007642/*
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01007643 * Set up scheduler domains and groups. Callers must hold the hotplug lock.
Paul Jackson029190c2007-10-18 23:40:20 -07007644 * For now this just excludes isolated cpus, but could be used to
7645 * exclude other special cases in the future.
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07007646 */
Peter Zijlstrac4a88492011-04-07 14:09:42 +02007647static int init_sched_domains(const struct cpumask *cpu_map)
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07007648{
Milton Miller73785472007-10-24 18:23:48 +02007649 int err;
7650
Heiko Carstens22e52b02008-03-12 18:31:59 +01007651 arch_update_cpu_topology();
Paul Jackson029190c2007-10-18 23:40:20 -07007652 ndoms_cur = 1;
Rusty Russellacc3f5d2009-11-03 14:53:40 +10307653 doms_cur = alloc_sched_domains(ndoms_cur);
Paul Jackson029190c2007-10-18 23:40:20 -07007654 if (!doms_cur)
Rusty Russellacc3f5d2009-11-03 14:53:40 +10307655 doms_cur = &fallback_doms;
7656 cpumask_andnot(doms_cur[0], cpu_map, cpu_isolated_map);
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09007657 dattr_cur = NULL;
Peter Zijlstradce840a2011-04-07 14:09:50 +02007658 err = build_sched_domains(doms_cur[0], NULL);
Milton Miller6382bc92007-10-15 17:00:19 +02007659 register_sched_domain_sysctl();
Milton Miller73785472007-10-24 18:23:48 +02007660
7661 return err;
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07007662}
7663
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07007664/*
7665 * Detach sched domains from a group of cpus specified in cpu_map
7666 * These cpus will now be attached to the NULL domain
7667 */
Rusty Russell96f874e2008-11-25 02:35:14 +10307668static void detach_destroy_domains(const struct cpumask *cpu_map)
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07007669{
7670 int i;
7671
Peter Zijlstradce840a2011-04-07 14:09:50 +02007672 rcu_read_lock();
Rusty Russellabcd0832008-11-25 02:35:02 +10307673 for_each_cpu(i, cpu_map)
Gregory Haskins57d885f2008-01-25 21:08:18 +01007674 cpu_attach_domain(NULL, &def_root_domain, i);
Peter Zijlstradce840a2011-04-07 14:09:50 +02007675 rcu_read_unlock();
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07007676}
7677
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09007678/* handle null as "default" */
7679static int dattrs_equal(struct sched_domain_attr *cur, int idx_cur,
7680 struct sched_domain_attr *new, int idx_new)
7681{
7682 struct sched_domain_attr tmp;
7683
7684 /* fast path */
7685 if (!new && !cur)
7686 return 1;
7687
7688 tmp = SD_ATTR_INIT;
7689 return !memcmp(cur ? (cur + idx_cur) : &tmp,
7690 new ? (new + idx_new) : &tmp,
7691 sizeof(struct sched_domain_attr));
7692}
7693
Paul Jackson029190c2007-10-18 23:40:20 -07007694/*
7695 * Partition sched domains as specified by the 'ndoms_new'
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01007696 * cpumasks in the array doms_new[] of cpumasks. This compares
Paul Jackson029190c2007-10-18 23:40:20 -07007697 * doms_new[] to the current sched domain partitioning, doms_cur[].
7698 * It destroys each deleted domain and builds each new domain.
7699 *
Rusty Russellacc3f5d2009-11-03 14:53:40 +10307700 * 'doms_new' is an array of cpumask_var_t's of length 'ndoms_new'.
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01007701 * The masks don't intersect (don't overlap.) We should setup one
7702 * sched domain for each mask. CPUs not in any of the cpumasks will
7703 * not be load balanced. If the same cpumask appears both in the
Paul Jackson029190c2007-10-18 23:40:20 -07007704 * current 'doms_cur' domains and in the new 'doms_new', we can leave
7705 * it as it is.
7706 *
Rusty Russellacc3f5d2009-11-03 14:53:40 +10307707 * The passed in 'doms_new' should be allocated using
7708 * alloc_sched_domains. This routine takes ownership of it and will
7709 * free_sched_domains it when done with it. If the caller failed the
7710 * alloc call, then it can pass in doms_new == NULL && ndoms_new == 1,
7711 * and partition_sched_domains() will fallback to the single partition
7712 * 'fallback_doms', it also forces the domains to be rebuilt.
Paul Jackson029190c2007-10-18 23:40:20 -07007713 *
Rusty Russell96f874e2008-11-25 02:35:14 +10307714 * If doms_new == NULL it will be replaced with cpu_online_mask.
Li Zefan700018e2008-11-18 14:02:03 +08007715 * ndoms_new == 0 is a special case for destroying existing domains,
7716 * and it will not create the default domain.
Max Krasnyanskydfb512e2008-08-29 13:11:41 -07007717 *
Paul Jackson029190c2007-10-18 23:40:20 -07007718 * Call with hotplug lock held
7719 */
Rusty Russellacc3f5d2009-11-03 14:53:40 +10307720void partition_sched_domains(int ndoms_new, cpumask_var_t doms_new[],
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09007721 struct sched_domain_attr *dattr_new)
Paul Jackson029190c2007-10-18 23:40:20 -07007722{
Max Krasnyanskydfb512e2008-08-29 13:11:41 -07007723 int i, j, n;
Heiko Carstensd65bd5e2008-12-09 18:49:51 +01007724 int new_topology;
Paul Jackson029190c2007-10-18 23:40:20 -07007725
Heiko Carstens712555e2008-04-28 11:33:07 +02007726 mutex_lock(&sched_domains_mutex);
Srivatsa Vaddagiria1835612008-01-25 21:08:00 +01007727
Milton Miller73785472007-10-24 18:23:48 +02007728 /* always unregister in case we don't destroy any domains */
7729 unregister_sched_domain_sysctl();
7730
Heiko Carstensd65bd5e2008-12-09 18:49:51 +01007731 /* Let architecture update cpu core mappings. */
7732 new_topology = arch_update_cpu_topology();
7733
Max Krasnyanskydfb512e2008-08-29 13:11:41 -07007734 n = doms_new ? ndoms_new : 0;
Paul Jackson029190c2007-10-18 23:40:20 -07007735
7736 /* Destroy deleted domains */
7737 for (i = 0; i < ndoms_cur; i++) {
Heiko Carstensd65bd5e2008-12-09 18:49:51 +01007738 for (j = 0; j < n && !new_topology; j++) {
Rusty Russellacc3f5d2009-11-03 14:53:40 +10307739 if (cpumask_equal(doms_cur[i], doms_new[j])
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09007740 && dattrs_equal(dattr_cur, i, dattr_new, j))
Paul Jackson029190c2007-10-18 23:40:20 -07007741 goto match1;
7742 }
7743 /* no match - a current sched domain not in new doms_new[] */
Rusty Russellacc3f5d2009-11-03 14:53:40 +10307744 detach_destroy_domains(doms_cur[i]);
Paul Jackson029190c2007-10-18 23:40:20 -07007745match1:
7746 ;
7747 }
7748
Max Krasnyanskye761b772008-07-15 04:43:49 -07007749 if (doms_new == NULL) {
7750 ndoms_cur = 0;
Rusty Russellacc3f5d2009-11-03 14:53:40 +10307751 doms_new = &fallback_doms;
Peter Zijlstra6ad4c182009-11-25 13:31:39 +01007752 cpumask_andnot(doms_new[0], cpu_active_mask, cpu_isolated_map);
Li Zefanfaa2f982008-11-04 16:20:23 +08007753 WARN_ON_ONCE(dattr_new);
Max Krasnyanskye761b772008-07-15 04:43:49 -07007754 }
7755
Paul Jackson029190c2007-10-18 23:40:20 -07007756 /* Build new domains */
7757 for (i = 0; i < ndoms_new; i++) {
Heiko Carstensd65bd5e2008-12-09 18:49:51 +01007758 for (j = 0; j < ndoms_cur && !new_topology; j++) {
Rusty Russellacc3f5d2009-11-03 14:53:40 +10307759 if (cpumask_equal(doms_new[i], doms_cur[j])
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09007760 && dattrs_equal(dattr_new, i, dattr_cur, j))
Paul Jackson029190c2007-10-18 23:40:20 -07007761 goto match2;
7762 }
7763 /* no match - add a new doms_new */
Peter Zijlstradce840a2011-04-07 14:09:50 +02007764 build_sched_domains(doms_new[i], dattr_new ? dattr_new + i : NULL);
Paul Jackson029190c2007-10-18 23:40:20 -07007765match2:
7766 ;
7767 }
7768
7769 /* Remember the new sched domains */
Rusty Russellacc3f5d2009-11-03 14:53:40 +10307770 if (doms_cur != &fallback_doms)
7771 free_sched_domains(doms_cur, ndoms_cur);
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09007772 kfree(dattr_cur); /* kfree(NULL) is safe */
Paul Jackson029190c2007-10-18 23:40:20 -07007773 doms_cur = doms_new;
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09007774 dattr_cur = dattr_new;
Paul Jackson029190c2007-10-18 23:40:20 -07007775 ndoms_cur = ndoms_new;
Milton Miller73785472007-10-24 18:23:48 +02007776
7777 register_sched_domain_sysctl();
Srivatsa Vaddagiria1835612008-01-25 21:08:00 +01007778
Heiko Carstens712555e2008-04-28 11:33:07 +02007779 mutex_unlock(&sched_domains_mutex);
Paul Jackson029190c2007-10-18 23:40:20 -07007780}
7781
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07007782#if defined(CONFIG_SCHED_MC) || defined(CONFIG_SCHED_SMT)
Peter Zijlstrac4a88492011-04-07 14:09:42 +02007783static void reinit_sched_domains(void)
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07007784{
Gautham R Shenoy95402b32008-01-25 21:08:02 +01007785 get_online_cpus();
Max Krasnyanskydfb512e2008-08-29 13:11:41 -07007786
7787 /* Destroy domains first to force the rebuild */
7788 partition_sched_domains(0, NULL, NULL);
7789
Max Krasnyanskye761b772008-07-15 04:43:49 -07007790 rebuild_sched_domains();
Gautham R Shenoy95402b32008-01-25 21:08:02 +01007791 put_online_cpus();
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07007792}
7793
7794static ssize_t sched_power_savings_store(const char *buf, size_t count, int smt)
7795{
Gautham R Shenoyafb8a9b2008-12-18 23:26:09 +05307796 unsigned int level = 0;
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07007797
Gautham R Shenoyafb8a9b2008-12-18 23:26:09 +05307798 if (sscanf(buf, "%u", &level) != 1)
7799 return -EINVAL;
7800
7801 /*
7802 * level is always be positive so don't check for
7803 * level < POWERSAVINGS_BALANCE_NONE which is 0
7804 * What happens on 0 or 1 byte write,
7805 * need to check for count as well?
7806 */
7807
7808 if (level >= MAX_POWERSAVINGS_BALANCE_LEVELS)
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07007809 return -EINVAL;
7810
7811 if (smt)
Gautham R Shenoyafb8a9b2008-12-18 23:26:09 +05307812 sched_smt_power_savings = level;
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07007813 else
Gautham R Shenoyafb8a9b2008-12-18 23:26:09 +05307814 sched_mc_power_savings = level;
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07007815
Peter Zijlstrac4a88492011-04-07 14:09:42 +02007816 reinit_sched_domains();
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07007817
Li Zefanc70f22d2009-01-05 19:07:50 +08007818 return count;
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07007819}
7820
Adrian Bunk6707de002007-08-12 18:08:19 +02007821#ifdef CONFIG_SCHED_MC
Andi Kleenf718cd42008-07-29 22:33:52 -07007822static ssize_t sched_mc_power_savings_show(struct sysdev_class *class,
Andi Kleenc9be0a32010-01-05 12:47:58 +01007823 struct sysdev_class_attribute *attr,
Andi Kleenf718cd42008-07-29 22:33:52 -07007824 char *page)
Adrian Bunk6707de002007-08-12 18:08:19 +02007825{
7826 return sprintf(page, "%u\n", sched_mc_power_savings);
7827}
Andi Kleenf718cd42008-07-29 22:33:52 -07007828static ssize_t sched_mc_power_savings_store(struct sysdev_class *class,
Andi Kleenc9be0a32010-01-05 12:47:58 +01007829 struct sysdev_class_attribute *attr,
Adrian Bunk6707de002007-08-12 18:08:19 +02007830 const char *buf, size_t count)
7831{
7832 return sched_power_savings_store(buf, count, 0);
7833}
Andi Kleenf718cd42008-07-29 22:33:52 -07007834static SYSDEV_CLASS_ATTR(sched_mc_power_savings, 0644,
7835 sched_mc_power_savings_show,
7836 sched_mc_power_savings_store);
Adrian Bunk6707de002007-08-12 18:08:19 +02007837#endif
7838
7839#ifdef CONFIG_SCHED_SMT
Andi Kleenf718cd42008-07-29 22:33:52 -07007840static ssize_t sched_smt_power_savings_show(struct sysdev_class *dev,
Andi Kleenc9be0a32010-01-05 12:47:58 +01007841 struct sysdev_class_attribute *attr,
Andi Kleenf718cd42008-07-29 22:33:52 -07007842 char *page)
Adrian Bunk6707de002007-08-12 18:08:19 +02007843{
7844 return sprintf(page, "%u\n", sched_smt_power_savings);
7845}
Andi Kleenf718cd42008-07-29 22:33:52 -07007846static ssize_t sched_smt_power_savings_store(struct sysdev_class *dev,
Andi Kleenc9be0a32010-01-05 12:47:58 +01007847 struct sysdev_class_attribute *attr,
Adrian Bunk6707de002007-08-12 18:08:19 +02007848 const char *buf, size_t count)
7849{
7850 return sched_power_savings_store(buf, count, 1);
7851}
Andi Kleenf718cd42008-07-29 22:33:52 -07007852static SYSDEV_CLASS_ATTR(sched_smt_power_savings, 0644,
7853 sched_smt_power_savings_show,
Adrian Bunk6707de002007-08-12 18:08:19 +02007854 sched_smt_power_savings_store);
7855#endif
7856
Li Zefan39aac642009-01-05 19:18:02 +08007857int __init sched_create_sysfs_power_savings_entries(struct sysdev_class *cls)
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07007858{
7859 int err = 0;
Ingo Molnar48f24c42006-07-03 00:25:40 -07007860
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07007861#ifdef CONFIG_SCHED_SMT
7862 if (smt_capable())
7863 err = sysfs_create_file(&cls->kset.kobj,
7864 &attr_sched_smt_power_savings.attr);
7865#endif
7866#ifdef CONFIG_SCHED_MC
7867 if (!err && mc_capable())
7868 err = sysfs_create_file(&cls->kset.kobj,
7869 &attr_sched_mc_power_savings.attr);
7870#endif
7871 return err;
7872}
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +02007873#endif /* CONFIG_SCHED_MC || CONFIG_SCHED_SMT */
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07007874
Linus Torvalds1da177e2005-04-16 15:20:36 -07007875/*
Tejun Heo3a101d02010-06-08 21:40:36 +02007876 * Update cpusets according to cpu_active mask. If cpusets are
7877 * disabled, cpuset_update_active_cpus() becomes a simple wrapper
7878 * around partition_sched_domains().
Linus Torvalds1da177e2005-04-16 15:20:36 -07007879 */
Tejun Heo0b2e9182010-06-21 23:53:31 +02007880static int cpuset_cpu_active(struct notifier_block *nfb, unsigned long action,
7881 void *hcpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007882{
Tejun Heo3a101d02010-06-08 21:40:36 +02007883 switch (action & ~CPU_TASKS_FROZEN) {
Max Krasnyanskye761b772008-07-15 04:43:49 -07007884 case CPU_ONLINE:
Peter Zijlstra6ad4c182009-11-25 13:31:39 +01007885 case CPU_DOWN_FAILED:
Tejun Heo3a101d02010-06-08 21:40:36 +02007886 cpuset_update_active_cpus();
Max Krasnyanskye761b772008-07-15 04:43:49 -07007887 return NOTIFY_OK;
Max Krasnyanskye761b772008-07-15 04:43:49 -07007888 default:
7889 return NOTIFY_DONE;
7890 }
7891}
Tejun Heo3a101d02010-06-08 21:40:36 +02007892
Tejun Heo0b2e9182010-06-21 23:53:31 +02007893static int cpuset_cpu_inactive(struct notifier_block *nfb, unsigned long action,
7894 void *hcpu)
Tejun Heo3a101d02010-06-08 21:40:36 +02007895{
7896 switch (action & ~CPU_TASKS_FROZEN) {
7897 case CPU_DOWN_PREPARE:
7898 cpuset_update_active_cpus();
7899 return NOTIFY_OK;
7900 default:
7901 return NOTIFY_DONE;
7902 }
7903}
Max Krasnyanskye761b772008-07-15 04:43:49 -07007904
7905static int update_runtime(struct notifier_block *nfb,
7906 unsigned long action, void *hcpu)
7907{
Peter Zijlstra7def2be2008-06-05 14:49:58 +02007908 int cpu = (int)(long)hcpu;
7909
Linus Torvalds1da177e2005-04-16 15:20:36 -07007910 switch (action) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007911 case CPU_DOWN_PREPARE:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07007912 case CPU_DOWN_PREPARE_FROZEN:
Peter Zijlstra7def2be2008-06-05 14:49:58 +02007913 disable_runtime(cpu_rq(cpu));
Linus Torvalds1da177e2005-04-16 15:20:36 -07007914 return NOTIFY_OK;
7915
Linus Torvalds1da177e2005-04-16 15:20:36 -07007916 case CPU_DOWN_FAILED:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07007917 case CPU_DOWN_FAILED_FROZEN:
Linus Torvalds1da177e2005-04-16 15:20:36 -07007918 case CPU_ONLINE:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07007919 case CPU_ONLINE_FROZEN:
Peter Zijlstra7def2be2008-06-05 14:49:58 +02007920 enable_runtime(cpu_rq(cpu));
Max Krasnyanskye761b772008-07-15 04:43:49 -07007921 return NOTIFY_OK;
7922
Linus Torvalds1da177e2005-04-16 15:20:36 -07007923 default:
7924 return NOTIFY_DONE;
7925 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007926}
Linus Torvalds1da177e2005-04-16 15:20:36 -07007927
7928void __init sched_init_smp(void)
7929{
Rusty Russelldcc30a32008-11-25 02:35:12 +10307930 cpumask_var_t non_isolated_cpus;
7931
7932 alloc_cpumask_var(&non_isolated_cpus, GFP_KERNEL);
Yong Zhangcb5fd132009-09-14 20:20:16 +08007933 alloc_cpumask_var(&fallback_doms, GFP_KERNEL);
Nick Piggin5c1e1762006-10-03 01:14:04 -07007934
Gautham R Shenoy95402b32008-01-25 21:08:02 +01007935 get_online_cpus();
Heiko Carstens712555e2008-04-28 11:33:07 +02007936 mutex_lock(&sched_domains_mutex);
Peter Zijlstrac4a88492011-04-07 14:09:42 +02007937 init_sched_domains(cpu_active_mask);
Rusty Russelldcc30a32008-11-25 02:35:12 +10307938 cpumask_andnot(non_isolated_cpus, cpu_possible_mask, cpu_isolated_map);
7939 if (cpumask_empty(non_isolated_cpus))
7940 cpumask_set_cpu(smp_processor_id(), non_isolated_cpus);
Heiko Carstens712555e2008-04-28 11:33:07 +02007941 mutex_unlock(&sched_domains_mutex);
Gautham R Shenoy95402b32008-01-25 21:08:02 +01007942 put_online_cpus();
Max Krasnyanskye761b772008-07-15 04:43:49 -07007943
Tejun Heo3a101d02010-06-08 21:40:36 +02007944 hotcpu_notifier(cpuset_cpu_active, CPU_PRI_CPUSET_ACTIVE);
7945 hotcpu_notifier(cpuset_cpu_inactive, CPU_PRI_CPUSET_INACTIVE);
Max Krasnyanskye761b772008-07-15 04:43:49 -07007946
7947 /* RT runtime code needs to handle some hotplug events */
7948 hotcpu_notifier(update_runtime, 0);
7949
Peter Zijlstrab328ca12008-04-29 10:02:46 +02007950 init_hrtick();
Nick Piggin5c1e1762006-10-03 01:14:04 -07007951
7952 /* Move init over to a non-isolated CPU */
Rusty Russelldcc30a32008-11-25 02:35:12 +10307953 if (set_cpus_allowed_ptr(current, non_isolated_cpus) < 0)
Nick Piggin5c1e1762006-10-03 01:14:04 -07007954 BUG();
Ingo Molnar19978ca2007-11-09 22:39:38 +01007955 sched_init_granularity();
Rusty Russelldcc30a32008-11-25 02:35:12 +10307956 free_cpumask_var(non_isolated_cpus);
Rusty Russell42128232008-11-25 02:35:12 +10307957
Rusty Russell0e3900e2008-11-25 02:35:13 +10307958 init_sched_rt_class();
Linus Torvalds1da177e2005-04-16 15:20:36 -07007959}
7960#else
7961void __init sched_init_smp(void)
7962{
Ingo Molnar19978ca2007-11-09 22:39:38 +01007963 sched_init_granularity();
Linus Torvalds1da177e2005-04-16 15:20:36 -07007964}
7965#endif /* CONFIG_SMP */
7966
Arun R Bharadwajcd1bb942009-04-16 12:15:34 +05307967const_debug unsigned int sysctl_timer_migration = 1;
7968
Linus Torvalds1da177e2005-04-16 15:20:36 -07007969int in_sched_functions(unsigned long addr)
7970{
Linus Torvalds1da177e2005-04-16 15:20:36 -07007971 return in_lock_functions(addr) ||
7972 (addr >= (unsigned long)__sched_text_start
7973 && addr < (unsigned long)__sched_text_end);
7974}
7975
Jan H. Schönherracb5a9b2011-07-14 18:32:43 +02007976static void init_cfs_rq(struct cfs_rq *cfs_rq)
Ingo Molnardd41f592007-07-09 18:51:59 +02007977{
7978 cfs_rq->tasks_timeline = RB_ROOT;
Peter Zijlstra4a55bd52008-04-19 19:45:00 +02007979 INIT_LIST_HEAD(&cfs_rq->tasks);
Peter Zijlstra67e9fb22007-10-15 17:00:10 +02007980 cfs_rq->min_vruntime = (u64)(-(1LL << 20));
Peter Zijlstrac64be782011-07-11 16:28:50 +02007981#ifndef CONFIG_64BIT
7982 cfs_rq->min_vruntime_copy = cfs_rq->min_vruntime;
7983#endif
Ingo Molnardd41f592007-07-09 18:51:59 +02007984}
7985
Peter Zijlstrafa85ae22008-01-25 21:08:29 +01007986static void init_rt_rq(struct rt_rq *rt_rq, struct rq *rq)
7987{
7988 struct rt_prio_array *array;
7989 int i;
7990
7991 array = &rt_rq->active;
7992 for (i = 0; i < MAX_RT_PRIO; i++) {
7993 INIT_LIST_HEAD(array->queue + i);
7994 __clear_bit(i, array->bitmap);
7995 }
7996 /* delimiter for bitsearch: */
7997 __set_bit(MAX_RT_PRIO, array->bitmap);
7998
Jan H. Schönherracb5a9b2011-07-14 18:32:43 +02007999#if defined CONFIG_SMP
Gregory Haskinse864c492008-12-29 09:39:49 -05008000 rt_rq->highest_prio.curr = MAX_RT_PRIO;
8001 rt_rq->highest_prio.next = MAX_RT_PRIO;
Peter Zijlstrafa85ae22008-01-25 21:08:29 +01008002 rt_rq->rt_nr_migratory = 0;
Peter Zijlstrafa85ae22008-01-25 21:08:29 +01008003 rt_rq->overloaded = 0;
Dima Zavin732375c2011-07-07 17:27:59 -07008004 plist_head_init(&rt_rq->pushable_tasks);
Peter Zijlstrafa85ae22008-01-25 21:08:29 +01008005#endif
8006
8007 rt_rq->rt_time = 0;
8008 rt_rq->rt_throttled = 0;
Peter Zijlstraac086bc2008-04-19 19:44:58 +02008009 rt_rq->rt_runtime = 0;
Thomas Gleixner0986b112009-11-17 15:32:06 +01008010 raw_spin_lock_init(&rt_rq->rt_runtime_lock);
Peter Zijlstrafa85ae22008-01-25 21:08:29 +01008011}
8012
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008013#ifdef CONFIG_FAIR_GROUP_SCHED
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008014static void init_tg_cfs_entry(struct task_group *tg, struct cfs_rq *cfs_rq,
Peter Zijlstra3d4b47b2010-11-15 15:47:01 -08008015 struct sched_entity *se, int cpu,
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008016 struct sched_entity *parent)
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008017{
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008018 struct rq *rq = cpu_rq(cpu);
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008019
Jan H. Schönherracb5a9b2011-07-14 18:32:43 +02008020 cfs_rq->tg = tg;
8021 cfs_rq->rq = rq;
8022#ifdef CONFIG_SMP
8023 /* allow initial update_cfs_load() to truncate */
8024 cfs_rq->load_stamp = 1;
8025#endif
Paul Turnerab84d312011-07-21 09:43:28 -07008026 init_cfs_rq_runtime(cfs_rq);
Jan H. Schönherracb5a9b2011-07-14 18:32:43 +02008027
8028 tg->cfs_rq[cpu] = cfs_rq;
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008029 tg->se[cpu] = se;
Jan H. Schönherracb5a9b2011-07-14 18:32:43 +02008030
Yong Zhang07e06b02011-01-07 15:17:36 +08008031 /* se could be NULL for root_task_group */
Dhaval Giani354d60c2008-04-19 19:44:59 +02008032 if (!se)
8033 return;
8034
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008035 if (!parent)
8036 se->cfs_rq = &rq->cfs;
8037 else
8038 se->cfs_rq = parent->my_q;
8039
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008040 se->my_q = cfs_rq;
Paul Turner94371782010-11-15 15:47:10 -08008041 update_load_set(&se->load, 0);
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008042 se->parent = parent;
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008043}
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01008044#endif
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008045
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01008046#ifdef CONFIG_RT_GROUP_SCHED
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008047static void init_tg_rt_entry(struct task_group *tg, struct rt_rq *rt_rq,
Peter Zijlstra3d4b47b2010-11-15 15:47:01 -08008048 struct sched_rt_entity *rt_se, int cpu,
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008049 struct sched_rt_entity *parent)
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008050{
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008051 struct rq *rq = cpu_rq(cpu);
8052
Jan H. Schönherracb5a9b2011-07-14 18:32:43 +02008053 rt_rq->highest_prio.curr = MAX_RT_PRIO;
8054 rt_rq->rt_nr_boosted = 0;
8055 rt_rq->rq = rq;
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008056 rt_rq->tg = tg;
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008057
Jan H. Schönherracb5a9b2011-07-14 18:32:43 +02008058 tg->rt_rq[cpu] = rt_rq;
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008059 tg->rt_se[cpu] = rt_se;
Jan H. Schönherracb5a9b2011-07-14 18:32:43 +02008060
Dhaval Giani354d60c2008-04-19 19:44:59 +02008061 if (!rt_se)
8062 return;
8063
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008064 if (!parent)
8065 rt_se->rt_rq = &rq->rt;
8066 else
8067 rt_se->rt_rq = parent->my_q;
8068
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008069 rt_se->my_q = rt_rq;
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008070 rt_se->parent = parent;
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008071 INIT_LIST_HEAD(&rt_se->run_list);
8072}
8073#endif
8074
Linus Torvalds1da177e2005-04-16 15:20:36 -07008075void __init sched_init(void)
8076{
Ingo Molnardd41f592007-07-09 18:51:59 +02008077 int i, j;
Mike Travis434d53b2008-04-04 18:11:04 -07008078 unsigned long alloc_size = 0, ptr;
8079
8080#ifdef CONFIG_FAIR_GROUP_SCHED
8081 alloc_size += 2 * nr_cpu_ids * sizeof(void **);
8082#endif
8083#ifdef CONFIG_RT_GROUP_SCHED
8084 alloc_size += 2 * nr_cpu_ids * sizeof(void **);
8085#endif
Rusty Russelldf7c8e82009-03-19 15:22:20 +10308086#ifdef CONFIG_CPUMASK_OFFSTACK
Rusty Russell8c083f02009-03-19 15:22:20 +10308087 alloc_size += num_possible_cpus() * cpumask_size();
Rusty Russelldf7c8e82009-03-19 15:22:20 +10308088#endif
Mike Travis434d53b2008-04-04 18:11:04 -07008089 if (alloc_size) {
Pekka Enberg36b7b6d2009-06-10 23:42:36 +03008090 ptr = (unsigned long)kzalloc(alloc_size, GFP_NOWAIT);
Mike Travis434d53b2008-04-04 18:11:04 -07008091
8092#ifdef CONFIG_FAIR_GROUP_SCHED
Yong Zhang07e06b02011-01-07 15:17:36 +08008093 root_task_group.se = (struct sched_entity **)ptr;
Mike Travis434d53b2008-04-04 18:11:04 -07008094 ptr += nr_cpu_ids * sizeof(void **);
8095
Yong Zhang07e06b02011-01-07 15:17:36 +08008096 root_task_group.cfs_rq = (struct cfs_rq **)ptr;
Mike Travis434d53b2008-04-04 18:11:04 -07008097 ptr += nr_cpu_ids * sizeof(void **);
Peter Zijlstraeff766a2008-04-19 19:45:00 +02008098
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +02008099#endif /* CONFIG_FAIR_GROUP_SCHED */
Mike Travis434d53b2008-04-04 18:11:04 -07008100#ifdef CONFIG_RT_GROUP_SCHED
Yong Zhang07e06b02011-01-07 15:17:36 +08008101 root_task_group.rt_se = (struct sched_rt_entity **)ptr;
Mike Travis434d53b2008-04-04 18:11:04 -07008102 ptr += nr_cpu_ids * sizeof(void **);
8103
Yong Zhang07e06b02011-01-07 15:17:36 +08008104 root_task_group.rt_rq = (struct rt_rq **)ptr;
Peter Zijlstraeff766a2008-04-19 19:45:00 +02008105 ptr += nr_cpu_ids * sizeof(void **);
8106
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +02008107#endif /* CONFIG_RT_GROUP_SCHED */
Rusty Russelldf7c8e82009-03-19 15:22:20 +10308108#ifdef CONFIG_CPUMASK_OFFSTACK
8109 for_each_possible_cpu(i) {
8110 per_cpu(load_balance_tmpmask, i) = (void *)ptr;
8111 ptr += cpumask_size();
8112 }
8113#endif /* CONFIG_CPUMASK_OFFSTACK */
Mike Travis434d53b2008-04-04 18:11:04 -07008114 }
Ingo Molnardd41f592007-07-09 18:51:59 +02008115
Gregory Haskins57d885f2008-01-25 21:08:18 +01008116#ifdef CONFIG_SMP
8117 init_defrootdomain();
8118#endif
8119
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008120 init_rt_bandwidth(&def_rt_bandwidth,
8121 global_rt_period(), global_rt_runtime());
8122
8123#ifdef CONFIG_RT_GROUP_SCHED
Yong Zhang07e06b02011-01-07 15:17:36 +08008124 init_rt_bandwidth(&root_task_group.rt_bandwidth,
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008125 global_rt_period(), global_rt_runtime());
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +02008126#endif /* CONFIG_RT_GROUP_SCHED */
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008127
Dhaval Giani7c941432010-01-20 13:26:18 +01008128#ifdef CONFIG_CGROUP_SCHED
Yong Zhang07e06b02011-01-07 15:17:36 +08008129 list_add(&root_task_group.list, &task_groups);
8130 INIT_LIST_HEAD(&root_task_group.children);
Mike Galbraith5091faa2010-11-30 14:18:03 +01008131 autogroup_init(&init_task);
Dhaval Giani7c941432010-01-20 13:26:18 +01008132#endif /* CONFIG_CGROUP_SCHED */
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008133
KAMEZAWA Hiroyuki0a945022006-03-28 01:56:37 -08008134 for_each_possible_cpu(i) {
Ingo Molnar70b97a72006-07-03 00:25:42 -07008135 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008136
8137 rq = cpu_rq(i);
Thomas Gleixner05fa7852009-11-17 14:28:38 +01008138 raw_spin_lock_init(&rq->lock);
Nick Piggin78979862005-06-25 14:57:13 -07008139 rq->nr_running = 0;
Thomas Gleixnerdce48a82009-04-11 10:43:41 +02008140 rq->calc_load_active = 0;
8141 rq->calc_load_update = jiffies + LOAD_FREQ;
Jan H. Schönherracb5a9b2011-07-14 18:32:43 +02008142 init_cfs_rq(&rq->cfs);
Peter Zijlstrafa85ae22008-01-25 21:08:29 +01008143 init_rt_rq(&rq->rt, rq);
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008144#ifdef CONFIG_FAIR_GROUP_SCHED
Yong Zhang07e06b02011-01-07 15:17:36 +08008145 root_task_group.shares = root_task_group_load;
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008146 INIT_LIST_HEAD(&rq->leaf_cfs_rq_list);
Dhaval Giani354d60c2008-04-19 19:44:59 +02008147 /*
Yong Zhang07e06b02011-01-07 15:17:36 +08008148 * How much cpu bandwidth does root_task_group get?
Dhaval Giani354d60c2008-04-19 19:44:59 +02008149 *
8150 * In case of task-groups formed thr' the cgroup filesystem, it
8151 * gets 100% of the cpu resources in the system. This overall
8152 * system cpu resource is divided among the tasks of
Yong Zhang07e06b02011-01-07 15:17:36 +08008153 * root_task_group and its child task-groups in a fair manner,
Dhaval Giani354d60c2008-04-19 19:44:59 +02008154 * based on each entity's (task or task-group's) weight
8155 * (se->load.weight).
8156 *
Yong Zhang07e06b02011-01-07 15:17:36 +08008157 * In other words, if root_task_group has 10 tasks of weight
Dhaval Giani354d60c2008-04-19 19:44:59 +02008158 * 1024) and two child groups A0 and A1 (of weight 1024 each),
8159 * then A0's share of the cpu resource is:
8160 *
Ingo Molnar0d905bc2009-05-04 19:13:30 +02008161 * A0's bandwidth = 1024 / (10*1024 + 1024 + 1024) = 8.33%
Dhaval Giani354d60c2008-04-19 19:44:59 +02008162 *
Yong Zhang07e06b02011-01-07 15:17:36 +08008163 * We achieve this by letting root_task_group's tasks sit
8164 * directly in rq->cfs (i.e root_task_group->se[] = NULL).
Dhaval Giani354d60c2008-04-19 19:44:59 +02008165 */
Paul Turnerab84d312011-07-21 09:43:28 -07008166 init_cfs_bandwidth(&root_task_group.cfs_bandwidth);
Yong Zhang07e06b02011-01-07 15:17:36 +08008167 init_tg_cfs_entry(&root_task_group, &rq->cfs, NULL, i, NULL);
Dhaval Giani354d60c2008-04-19 19:44:59 +02008168#endif /* CONFIG_FAIR_GROUP_SCHED */
8169
8170 rq->rt.rt_runtime = def_rt_bandwidth.rt_runtime;
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01008171#ifdef CONFIG_RT_GROUP_SCHED
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008172 INIT_LIST_HEAD(&rq->leaf_rt_rq_list);
Yong Zhang07e06b02011-01-07 15:17:36 +08008173 init_tg_rt_entry(&root_task_group, &rq->rt, NULL, i, NULL);
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008174#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07008175
Ingo Molnardd41f592007-07-09 18:51:59 +02008176 for (j = 0; j < CPU_LOAD_IDX_MAX; j++)
8177 rq->cpu_load[j] = 0;
Venkatesh Pallipadifdf3e952010-05-17 18:14:43 -07008178
8179 rq->last_load_update_tick = jiffies;
8180
Linus Torvalds1da177e2005-04-16 15:20:36 -07008181#ifdef CONFIG_SMP
Nick Piggin41c7ce92005-06-25 14:57:24 -07008182 rq->sd = NULL;
Gregory Haskins57d885f2008-01-25 21:08:18 +01008183 rq->rd = NULL;
Nikhil Rao1399fa72011-05-18 10:09:39 -07008184 rq->cpu_power = SCHED_POWER_SCALE;
Gregory Haskins3f029d32009-07-29 11:08:47 -04008185 rq->post_schedule = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008186 rq->active_balance = 0;
Ingo Molnardd41f592007-07-09 18:51:59 +02008187 rq->next_balance = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008188 rq->push_cpu = 0;
Christoph Lameter0a2966b2006-09-25 23:30:51 -07008189 rq->cpu = i;
Gregory Haskins1f11eb62008-06-04 15:04:05 -04008190 rq->online = 0;
Mike Galbraitheae0c9d2009-11-10 03:50:02 +01008191 rq->idle_stamp = 0;
8192 rq->avg_idle = 2*sysctl_sched_migration_cost;
Gregory Haskinsdc938522008-01-25 21:08:26 +01008193 rq_attach_root(rq, &def_root_domain);
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07008194#ifdef CONFIG_NO_HZ
8195 rq->nohz_balance_kick = 0;
8196 init_sched_softirq_csd(&per_cpu(remote_sched_softirq_cb, i));
8197#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07008198#endif
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01008199 init_rq_hrtick(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008200 atomic_set(&rq->nr_iowait, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008201 }
8202
Peter Williams2dd73a42006-06-27 02:54:34 -07008203 set_load_weight(&init_task);
Heiko Carstensb50f60c2006-07-30 03:03:52 -07008204
Avi Kivitye107be32007-07-26 13:40:43 +02008205#ifdef CONFIG_PREEMPT_NOTIFIERS
8206 INIT_HLIST_HEAD(&init_task.preempt_notifiers);
8207#endif
8208
Christoph Lameterc9819f42006-12-10 02:20:25 -08008209#ifdef CONFIG_SMP
Carlos R. Mafra962cf362008-05-15 11:15:37 -03008210 open_softirq(SCHED_SOFTIRQ, run_rebalance_domains);
Christoph Lameterc9819f42006-12-10 02:20:25 -08008211#endif
8212
Heiko Carstensb50f60c2006-07-30 03:03:52 -07008213#ifdef CONFIG_RT_MUTEXES
Dima Zavin732375c2011-07-07 17:27:59 -07008214 plist_head_init(&init_task.pi_waiters);
Heiko Carstensb50f60c2006-07-30 03:03:52 -07008215#endif
8216
Linus Torvalds1da177e2005-04-16 15:20:36 -07008217 /*
8218 * The boot idle thread does lazy MMU switching as well:
8219 */
8220 atomic_inc(&init_mm.mm_count);
8221 enter_lazy_tlb(&init_mm, current);
8222
8223 /*
8224 * Make us the idle thread. Technically, schedule() should not be
8225 * called from this thread, however somewhere below it might be,
8226 * but because we are the idle thread, we just pick up running again
8227 * when this runqueue becomes "idle".
8228 */
8229 init_idle(current, smp_processor_id());
Thomas Gleixnerdce48a82009-04-11 10:43:41 +02008230
8231 calc_load_update = jiffies + LOAD_FREQ;
8232
Ingo Molnardd41f592007-07-09 18:51:59 +02008233 /*
8234 * During early bootup we pretend to be a normal task:
8235 */
8236 current->sched_class = &fair_sched_class;
Ingo Molnar6892b752008-02-13 14:02:36 +01008237
Rusty Russell6a7b3dc2008-11-25 02:35:04 +10308238 /* Allocate the nohz_cpu_mask if CONFIG_CPUMASK_OFFSTACK */
Rusty Russell49557e62009-11-02 20:37:20 +10308239 zalloc_cpumask_var(&nohz_cpu_mask, GFP_NOWAIT);
Rusty Russellbf4d83f2008-11-25 09:57:51 +10308240#ifdef CONFIG_SMP
Peter Zijlstra4cb98832011-04-07 14:09:58 +02008241 zalloc_cpumask_var(&sched_domains_tmpmask, GFP_NOWAIT);
Rusty Russell7d1e6a92008-11-25 02:35:09 +10308242#ifdef CONFIG_NO_HZ
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07008243 zalloc_cpumask_var(&nohz.idle_cpus_mask, GFP_NOWAIT);
8244 alloc_cpumask_var(&nohz.grp_idle_mask, GFP_NOWAIT);
8245 atomic_set(&nohz.load_balancer, nr_cpu_ids);
8246 atomic_set(&nohz.first_pick_cpu, nr_cpu_ids);
8247 atomic_set(&nohz.second_pick_cpu, nr_cpu_ids);
Rusty Russell7d1e6a92008-11-25 02:35:09 +10308248#endif
Rusty Russellbdddd292009-12-02 14:09:16 +10308249 /* May be allocated at isolcpus cmdline parse time */
8250 if (cpu_isolated_map == NULL)
8251 zalloc_cpumask_var(&cpu_isolated_map, GFP_NOWAIT);
Rusty Russellbf4d83f2008-11-25 09:57:51 +10308252#endif /* SMP */
Rusty Russell6a7b3dc2008-11-25 02:35:04 +10308253
Ingo Molnar6892b752008-02-13 14:02:36 +01008254 scheduler_running = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008255}
8256
Frederic Weisbeckerd902db12011-06-08 19:31:56 +02008257#ifdef CONFIG_DEBUG_ATOMIC_SLEEP
Frederic Weisbeckere4aafea2009-07-16 15:44:29 +02008258static inline int preempt_count_equals(int preempt_offset)
8259{
Frederic Weisbecker234da7b2009-12-16 20:21:05 +01008260 int nested = (preempt_count() & ~PREEMPT_ACTIVE) + rcu_preempt_depth();
Frederic Weisbeckere4aafea2009-07-16 15:44:29 +02008261
Arnd Bergmann4ba82162011-01-25 22:52:22 +01008262 return (nested == preempt_offset);
Frederic Weisbeckere4aafea2009-07-16 15:44:29 +02008263}
8264
Simon Kagstromd8948372009-12-23 11:08:18 +01008265void __might_sleep(const char *file, int line, int preempt_offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008266{
Linus Torvalds1da177e2005-04-16 15:20:36 -07008267 static unsigned long prev_jiffy; /* ratelimiting */
8268
Frederic Weisbeckere4aafea2009-07-16 15:44:29 +02008269 if ((preempt_count_equals(preempt_offset) && !irqs_disabled()) ||
8270 system_state != SYSTEM_RUNNING || oops_in_progress)
Ingo Molnaraef745f2008-08-28 11:34:43 +02008271 return;
8272 if (time_before(jiffies, prev_jiffy + HZ) && prev_jiffy)
8273 return;
8274 prev_jiffy = jiffies;
8275
Peter Zijlstra3df0fc52009-12-20 14:23:57 +01008276 printk(KERN_ERR
8277 "BUG: sleeping function called from invalid context at %s:%d\n",
8278 file, line);
8279 printk(KERN_ERR
8280 "in_atomic(): %d, irqs_disabled(): %d, pid: %d, name: %s\n",
8281 in_atomic(), irqs_disabled(),
8282 current->pid, current->comm);
Ingo Molnaraef745f2008-08-28 11:34:43 +02008283
8284 debug_show_held_locks(current);
8285 if (irqs_disabled())
8286 print_irqtrace_events(current);
8287 dump_stack();
Linus Torvalds1da177e2005-04-16 15:20:36 -07008288}
8289EXPORT_SYMBOL(__might_sleep);
8290#endif
8291
8292#ifdef CONFIG_MAGIC_SYSRQ
Andi Kleen3a5e4dc2007-10-15 17:00:15 +02008293static void normalize_task(struct rq *rq, struct task_struct *p)
8294{
Peter Zijlstrada7a7352011-01-17 17:03:27 +01008295 const struct sched_class *prev_class = p->sched_class;
8296 int old_prio = p->prio;
Andi Kleen3a5e4dc2007-10-15 17:00:15 +02008297 int on_rq;
Peter Zijlstra3e51f332008-05-03 18:29:28 +02008298
Peter Zijlstrafd2f4412011-04-05 17:23:44 +02008299 on_rq = p->on_rq;
Andi Kleen3a5e4dc2007-10-15 17:00:15 +02008300 if (on_rq)
8301 deactivate_task(rq, p, 0);
8302 __setscheduler(rq, p, SCHED_NORMAL, 0);
8303 if (on_rq) {
8304 activate_task(rq, p, 0);
8305 resched_task(rq->curr);
8306 }
Peter Zijlstrada7a7352011-01-17 17:03:27 +01008307
8308 check_class_changed(rq, p, prev_class, old_prio);
Andi Kleen3a5e4dc2007-10-15 17:00:15 +02008309}
8310
Linus Torvalds1da177e2005-04-16 15:20:36 -07008311void normalize_rt_tasks(void)
8312{
Ingo Molnara0f98a12007-06-17 18:37:45 +02008313 struct task_struct *g, *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008314 unsigned long flags;
Ingo Molnar70b97a72006-07-03 00:25:42 -07008315 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008316
Peter Zijlstra4cf5d772008-02-13 15:45:39 +01008317 read_lock_irqsave(&tasklist_lock, flags);
Ingo Molnara0f98a12007-06-17 18:37:45 +02008318 do_each_thread(g, p) {
Ingo Molnar178be792007-10-15 17:00:18 +02008319 /*
8320 * Only normalize user tasks:
8321 */
8322 if (!p->mm)
8323 continue;
8324
Ingo Molnardd41f592007-07-09 18:51:59 +02008325 p->se.exec_start = 0;
Ingo Molnar6cfb0d52007-08-02 17:41:40 +02008326#ifdef CONFIG_SCHEDSTATS
Lucas De Marchi41acab82010-03-10 23:37:45 -03008327 p->se.statistics.wait_start = 0;
8328 p->se.statistics.sleep_start = 0;
8329 p->se.statistics.block_start = 0;
Ingo Molnar6cfb0d52007-08-02 17:41:40 +02008330#endif
Ingo Molnardd41f592007-07-09 18:51:59 +02008331
8332 if (!rt_task(p)) {
8333 /*
8334 * Renice negative nice level userspace
8335 * tasks back to 0:
8336 */
8337 if (TASK_NICE(p) < 0 && p->mm)
8338 set_user_nice(p, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008339 continue;
Ingo Molnardd41f592007-07-09 18:51:59 +02008340 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008341
Thomas Gleixner1d615482009-11-17 14:54:03 +01008342 raw_spin_lock(&p->pi_lock);
Ingo Molnarb29739f2006-06-27 02:54:51 -07008343 rq = __task_rq_lock(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008344
Ingo Molnar178be792007-10-15 17:00:18 +02008345 normalize_task(rq, p);
Andi Kleen3a5e4dc2007-10-15 17:00:15 +02008346
Ingo Molnarb29739f2006-06-27 02:54:51 -07008347 __task_rq_unlock(rq);
Thomas Gleixner1d615482009-11-17 14:54:03 +01008348 raw_spin_unlock(&p->pi_lock);
Ingo Molnara0f98a12007-06-17 18:37:45 +02008349 } while_each_thread(g, p);
8350
Peter Zijlstra4cf5d772008-02-13 15:45:39 +01008351 read_unlock_irqrestore(&tasklist_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008352}
8353
8354#endif /* CONFIG_MAGIC_SYSRQ */
Linus Torvalds1df5c102005-09-12 07:59:21 -07008355
Jason Wessel67fc4e02010-05-20 21:04:21 -05008356#if defined(CONFIG_IA64) || defined(CONFIG_KGDB_KDB)
Linus Torvalds1df5c102005-09-12 07:59:21 -07008357/*
Jason Wessel67fc4e02010-05-20 21:04:21 -05008358 * These functions are only useful for the IA64 MCA handling, or kdb.
Linus Torvalds1df5c102005-09-12 07:59:21 -07008359 *
8360 * They can only be called when the whole system has been
8361 * stopped - every CPU needs to be quiescent, and no scheduling
8362 * activity can take place. Using them for anything else would
8363 * be a serious bug, and as a result, they aren't even visible
8364 * under any other configuration.
8365 */
8366
8367/**
8368 * curr_task - return the current task for a given cpu.
8369 * @cpu: the processor in question.
8370 *
8371 * ONLY VALID WHEN THE WHOLE SYSTEM IS STOPPED!
8372 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07008373struct task_struct *curr_task(int cpu)
Linus Torvalds1df5c102005-09-12 07:59:21 -07008374{
8375 return cpu_curr(cpu);
8376}
8377
Jason Wessel67fc4e02010-05-20 21:04:21 -05008378#endif /* defined(CONFIG_IA64) || defined(CONFIG_KGDB_KDB) */
8379
8380#ifdef CONFIG_IA64
Linus Torvalds1df5c102005-09-12 07:59:21 -07008381/**
8382 * set_curr_task - set the current task for a given cpu.
8383 * @cpu: the processor in question.
8384 * @p: the task pointer to set.
8385 *
8386 * Description: This function must only be used when non-maskable interrupts
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01008387 * are serviced on a separate stack. It allows the architecture to switch the
8388 * notion of the current task on a cpu in a non-blocking manner. This function
Linus Torvalds1df5c102005-09-12 07:59:21 -07008389 * must be called with all CPU's synchronized, and interrupts disabled, the
8390 * and caller must save the original value of the current task (see
8391 * curr_task() above) and restore that value before reenabling interrupts and
8392 * re-starting the system.
8393 *
8394 * ONLY VALID WHEN THE WHOLE SYSTEM IS STOPPED!
8395 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07008396void set_curr_task(int cpu, struct task_struct *p)
Linus Torvalds1df5c102005-09-12 07:59:21 -07008397{
8398 cpu_curr(cpu) = p;
8399}
8400
8401#endif
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008402
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008403#ifdef CONFIG_FAIR_GROUP_SCHED
8404static void free_fair_sched_group(struct task_group *tg)
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008405{
8406 int i;
8407
Paul Turnerab84d312011-07-21 09:43:28 -07008408 destroy_cfs_bandwidth(tg_cfs_bandwidth(tg));
8409
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008410 for_each_possible_cpu(i) {
8411 if (tg->cfs_rq)
8412 kfree(tg->cfs_rq[i]);
8413 if (tg->se)
8414 kfree(tg->se[i]);
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008415 }
8416
8417 kfree(tg->cfs_rq);
8418 kfree(tg->se);
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008419}
8420
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008421static
8422int alloc_fair_sched_group(struct task_group *tg, struct task_group *parent)
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008423{
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008424 struct cfs_rq *cfs_rq;
Li Zefaneab17222008-10-29 17:03:22 +08008425 struct sched_entity *se;
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008426 int i;
8427
Mike Travis434d53b2008-04-04 18:11:04 -07008428 tg->cfs_rq = kzalloc(sizeof(cfs_rq) * nr_cpu_ids, GFP_KERNEL);
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008429 if (!tg->cfs_rq)
8430 goto err;
Mike Travis434d53b2008-04-04 18:11:04 -07008431 tg->se = kzalloc(sizeof(se) * nr_cpu_ids, GFP_KERNEL);
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008432 if (!tg->se)
8433 goto err;
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01008434
8435 tg->shares = NICE_0_LOAD;
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008436
Paul Turnerab84d312011-07-21 09:43:28 -07008437 init_cfs_bandwidth(tg_cfs_bandwidth(tg));
8438
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008439 for_each_possible_cpu(i) {
Li Zefaneab17222008-10-29 17:03:22 +08008440 cfs_rq = kzalloc_node(sizeof(struct cfs_rq),
8441 GFP_KERNEL, cpu_to_node(i));
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008442 if (!cfs_rq)
8443 goto err;
8444
Li Zefaneab17222008-10-29 17:03:22 +08008445 se = kzalloc_node(sizeof(struct sched_entity),
8446 GFP_KERNEL, cpu_to_node(i));
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008447 if (!se)
Phil Carmodydfc12eb2009-12-10 14:29:37 +02008448 goto err_free_rq;
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008449
Jan H. Schönherracb5a9b2011-07-14 18:32:43 +02008450 init_cfs_rq(cfs_rq);
Peter Zijlstra3d4b47b2010-11-15 15:47:01 -08008451 init_tg_cfs_entry(tg, cfs_rq, se, i, parent->se[i]);
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008452 }
8453
8454 return 1;
8455
Peter Zijlstra49246272010-10-17 21:46:10 +02008456err_free_rq:
Phil Carmodydfc12eb2009-12-10 14:29:37 +02008457 kfree(cfs_rq);
Peter Zijlstra49246272010-10-17 21:46:10 +02008458err:
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008459 return 0;
8460}
8461
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008462static inline void unregister_fair_sched_group(struct task_group *tg, int cpu)
8463{
Peter Zijlstra3d4b47b2010-11-15 15:47:01 -08008464 struct rq *rq = cpu_rq(cpu);
8465 unsigned long flags;
Peter Zijlstra3d4b47b2010-11-15 15:47:01 -08008466
8467 /*
8468 * Only empty task groups can be destroyed; so we can speculatively
8469 * check on_list without danger of it being re-added.
8470 */
8471 if (!tg->cfs_rq[cpu]->on_list)
8472 return;
8473
8474 raw_spin_lock_irqsave(&rq->lock, flags);
Paul Turner822bc182010-11-29 16:55:40 -08008475 list_del_leaf_cfs_rq(tg->cfs_rq[cpu]);
Peter Zijlstra3d4b47b2010-11-15 15:47:01 -08008476 raw_spin_unlock_irqrestore(&rq->lock, flags);
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008477}
Jan Schoenherr5f817d62011-07-13 20:13:31 +02008478#else /* !CONFIG_FAIR_GROUP_SCHED */
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008479static inline void free_fair_sched_group(struct task_group *tg)
8480{
8481}
8482
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008483static inline
8484int alloc_fair_sched_group(struct task_group *tg, struct task_group *parent)
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008485{
8486 return 1;
8487}
8488
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008489static inline void unregister_fair_sched_group(struct task_group *tg, int cpu)
8490{
8491}
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +02008492#endif /* CONFIG_FAIR_GROUP_SCHED */
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01008493
8494#ifdef CONFIG_RT_GROUP_SCHED
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008495static void free_rt_sched_group(struct task_group *tg)
8496{
8497 int i;
8498
Bianca Lutz99bc5242011-07-13 20:13:36 +02008499 if (tg->rt_se)
8500 destroy_rt_bandwidth(&tg->rt_bandwidth);
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008501
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008502 for_each_possible_cpu(i) {
8503 if (tg->rt_rq)
8504 kfree(tg->rt_rq[i]);
8505 if (tg->rt_se)
8506 kfree(tg->rt_se[i]);
8507 }
8508
8509 kfree(tg->rt_rq);
8510 kfree(tg->rt_se);
8511}
8512
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008513static
8514int alloc_rt_sched_group(struct task_group *tg, struct task_group *parent)
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008515{
8516 struct rt_rq *rt_rq;
Li Zefaneab17222008-10-29 17:03:22 +08008517 struct sched_rt_entity *rt_se;
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008518 int i;
8519
Mike Travis434d53b2008-04-04 18:11:04 -07008520 tg->rt_rq = kzalloc(sizeof(rt_rq) * nr_cpu_ids, GFP_KERNEL);
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008521 if (!tg->rt_rq)
8522 goto err;
Mike Travis434d53b2008-04-04 18:11:04 -07008523 tg->rt_se = kzalloc(sizeof(rt_se) * nr_cpu_ids, GFP_KERNEL);
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008524 if (!tg->rt_se)
8525 goto err;
8526
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008527 init_rt_bandwidth(&tg->rt_bandwidth,
8528 ktime_to_ns(def_rt_bandwidth.rt_period), 0);
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008529
8530 for_each_possible_cpu(i) {
Li Zefaneab17222008-10-29 17:03:22 +08008531 rt_rq = kzalloc_node(sizeof(struct rt_rq),
8532 GFP_KERNEL, cpu_to_node(i));
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008533 if (!rt_rq)
8534 goto err;
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008535
Li Zefaneab17222008-10-29 17:03:22 +08008536 rt_se = kzalloc_node(sizeof(struct sched_rt_entity),
8537 GFP_KERNEL, cpu_to_node(i));
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008538 if (!rt_se)
Phil Carmodydfc12eb2009-12-10 14:29:37 +02008539 goto err_free_rq;
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008540
Jan H. Schönherracb5a9b2011-07-14 18:32:43 +02008541 init_rt_rq(rt_rq, cpu_rq(i));
8542 rt_rq->rt_runtime = tg->rt_bandwidth.rt_runtime;
Peter Zijlstra3d4b47b2010-11-15 15:47:01 -08008543 init_tg_rt_entry(tg, rt_rq, rt_se, i, parent->rt_se[i]);
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008544 }
8545
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008546 return 1;
8547
Peter Zijlstra49246272010-10-17 21:46:10 +02008548err_free_rq:
Phil Carmodydfc12eb2009-12-10 14:29:37 +02008549 kfree(rt_rq);
Peter Zijlstra49246272010-10-17 21:46:10 +02008550err:
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008551 return 0;
8552}
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +02008553#else /* !CONFIG_RT_GROUP_SCHED */
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008554static inline void free_rt_sched_group(struct task_group *tg)
8555{
8556}
8557
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008558static inline
8559int alloc_rt_sched_group(struct task_group *tg, struct task_group *parent)
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008560{
8561 return 1;
8562}
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +02008563#endif /* CONFIG_RT_GROUP_SCHED */
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008564
Dhaval Giani7c941432010-01-20 13:26:18 +01008565#ifdef CONFIG_CGROUP_SCHED
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008566static void free_sched_group(struct task_group *tg)
8567{
8568 free_fair_sched_group(tg);
8569 free_rt_sched_group(tg);
Mike Galbraithe9aa1dd2011-01-05 11:11:25 +01008570 autogroup_free(tg);
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008571 kfree(tg);
8572}
8573
8574/* allocate runqueue etc for a new task group */
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008575struct task_group *sched_create_group(struct task_group *parent)
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008576{
8577 struct task_group *tg;
8578 unsigned long flags;
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008579
8580 tg = kzalloc(sizeof(*tg), GFP_KERNEL);
8581 if (!tg)
8582 return ERR_PTR(-ENOMEM);
8583
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008584 if (!alloc_fair_sched_group(tg, parent))
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008585 goto err;
8586
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008587 if (!alloc_rt_sched_group(tg, parent))
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008588 goto err;
8589
Peter Zijlstra8ed36992008-02-13 15:45:39 +01008590 spin_lock_irqsave(&task_group_lock, flags);
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008591 list_add_rcu(&tg->list, &task_groups);
Peter Zijlstraf473aa52008-04-19 19:45:00 +02008592
8593 WARN_ON(!parent); /* root should already exist */
8594
8595 tg->parent = parent;
Peter Zijlstraf473aa52008-04-19 19:45:00 +02008596 INIT_LIST_HEAD(&tg->children);
Zhang, Yanmin09f27242030-08-14 15:56:40 +08008597 list_add_rcu(&tg->siblings, &parent->children);
Peter Zijlstra8ed36992008-02-13 15:45:39 +01008598 spin_unlock_irqrestore(&task_group_lock, flags);
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008599
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02008600 return tg;
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008601
8602err:
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008603 free_sched_group(tg);
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008604 return ERR_PTR(-ENOMEM);
8605}
8606
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02008607/* rcu callback to free various structures associated with a task group */
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008608static void free_sched_group_rcu(struct rcu_head *rhp)
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008609{
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008610 /* now it should be safe to free those cfs_rqs */
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008611 free_sched_group(container_of(rhp, struct task_group, rcu));
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008612}
8613
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02008614/* Destroy runqueue etc associated with a task group */
Ingo Molnar4cf86d72007-10-15 17:00:14 +02008615void sched_destroy_group(struct task_group *tg)
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008616{
Peter Zijlstra8ed36992008-02-13 15:45:39 +01008617 unsigned long flags;
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02008618 int i;
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008619
Peter Zijlstra3d4b47b2010-11-15 15:47:01 -08008620 /* end participation in shares distribution */
8621 for_each_possible_cpu(i)
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008622 unregister_fair_sched_group(tg, i);
Peter Zijlstra3d4b47b2010-11-15 15:47:01 -08008623
8624 spin_lock_irqsave(&task_group_lock, flags);
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008625 list_del_rcu(&tg->list);
Peter Zijlstraf473aa52008-04-19 19:45:00 +02008626 list_del_rcu(&tg->siblings);
Peter Zijlstra8ed36992008-02-13 15:45:39 +01008627 spin_unlock_irqrestore(&task_group_lock, flags);
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02008628
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02008629 /* wait for possible concurrent references to cfs_rqs complete */
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008630 call_rcu(&tg->rcu, free_sched_group_rcu);
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008631}
8632
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02008633/* change task's runqueue when it moves between groups.
Ingo Molnar3a252012007-10-15 17:00:12 +02008634 * The caller of this function should have put the task in its new group
8635 * by now. This function just updates tsk->se.cfs_rq and tsk->se.parent to
8636 * reflect its new group.
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02008637 */
8638void sched_move_task(struct task_struct *tsk)
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008639{
8640 int on_rq, running;
8641 unsigned long flags;
8642 struct rq *rq;
8643
8644 rq = task_rq_lock(tsk, &flags);
8645
Dmitry Adamushko051a1d12007-12-18 15:21:13 +01008646 running = task_current(rq, tsk);
Peter Zijlstrafd2f4412011-04-05 17:23:44 +02008647 on_rq = tsk->on_rq;
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008648
Hiroshi Shimamoto0e1f3482008-03-10 11:01:20 -07008649 if (on_rq)
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008650 dequeue_task(rq, tsk, 0);
Hiroshi Shimamoto0e1f3482008-03-10 11:01:20 -07008651 if (unlikely(running))
8652 tsk->sched_class->put_prev_task(rq, tsk);
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008653
Peter Zijlstra810b3812008-02-29 15:21:01 -05008654#ifdef CONFIG_FAIR_GROUP_SCHED
Peter Zijlstrab2b5ce02010-10-15 15:24:15 +02008655 if (tsk->sched_class->task_move_group)
8656 tsk->sched_class->task_move_group(tsk, on_rq);
8657 else
Peter Zijlstra810b3812008-02-29 15:21:01 -05008658#endif
Peter Zijlstrab2b5ce02010-10-15 15:24:15 +02008659 set_task_rq(tsk, task_cpu(tsk));
Peter Zijlstra810b3812008-02-29 15:21:01 -05008660
Hiroshi Shimamoto0e1f3482008-03-10 11:01:20 -07008661 if (unlikely(running))
8662 tsk->sched_class->set_curr_task(rq);
8663 if (on_rq)
Peter Zijlstra371fd7e2010-03-24 16:38:48 +01008664 enqueue_task(rq, tsk, 0);
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008665
Peter Zijlstra0122ec52011-04-05 17:23:51 +02008666 task_rq_unlock(rq, tsk, &flags);
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008667}
Dhaval Giani7c941432010-01-20 13:26:18 +01008668#endif /* CONFIG_CGROUP_SCHED */
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008669
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01008670#ifdef CONFIG_FAIR_GROUP_SCHED
Peter Zijlstra8ed36992008-02-13 15:45:39 +01008671static DEFINE_MUTEX(shares_mutex);
8672
Ingo Molnar4cf86d72007-10-15 17:00:14 +02008673int sched_group_set_shares(struct task_group *tg, unsigned long shares)
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008674{
8675 int i;
Peter Zijlstra8ed36992008-02-13 15:45:39 +01008676 unsigned long flags;
Ingo Molnarc61935f2008-01-22 11:24:58 +01008677
Peter Zijlstra62fb1852008-02-25 17:34:02 +01008678 /*
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008679 * We can't change the weight of the root cgroup.
8680 */
8681 if (!tg->se[0])
8682 return -EINVAL;
8683
Mike Galbraithcd622872011-06-04 15:03:20 +02008684 shares = clamp(shares, scale_load(MIN_SHARES), scale_load(MAX_SHARES));
Peter Zijlstra62fb1852008-02-25 17:34:02 +01008685
Peter Zijlstra8ed36992008-02-13 15:45:39 +01008686 mutex_lock(&shares_mutex);
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02008687 if (tg->shares == shares)
Dhaval Giani5cb350b2007-10-15 17:00:14 +02008688 goto done;
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008689
Srivatsa Vaddagiri6b2d7702008-01-25 21:08:00 +01008690 tg->shares = shares;
Peter Zijlstrac09595f2008-06-27 13:41:14 +02008691 for_each_possible_cpu(i) {
Paul Turner94371782010-11-15 15:47:10 -08008692 struct rq *rq = cpu_rq(i);
8693 struct sched_entity *se;
8694
8695 se = tg->se[i];
8696 /* Propagate contribution to hierarchy */
8697 raw_spin_lock_irqsave(&rq->lock, flags);
8698 for_each_sched_entity(se)
Paul Turner6d5ab292011-01-21 20:45:01 -08008699 update_cfs_shares(group_cfs_rq(se));
Paul Turner94371782010-11-15 15:47:10 -08008700 raw_spin_unlock_irqrestore(&rq->lock, flags);
Peter Zijlstrac09595f2008-06-27 13:41:14 +02008701 }
Srivatsa Vaddagiri6b2d7702008-01-25 21:08:00 +01008702
Dhaval Giani5cb350b2007-10-15 17:00:14 +02008703done:
Peter Zijlstra8ed36992008-02-13 15:45:39 +01008704 mutex_unlock(&shares_mutex);
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02008705 return 0;
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008706}
8707
Dhaval Giani5cb350b2007-10-15 17:00:14 +02008708unsigned long sched_group_shares(struct task_group *tg)
8709{
8710 return tg->shares;
8711}
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01008712#endif
Dhaval Giani5cb350b2007-10-15 17:00:14 +02008713
Paul Turnera790de92011-07-21 09:43:29 -07008714#if defined(CONFIG_RT_GROUP_SCHED) || defined(CONFIG_CFS_BANDWIDTH)
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01008715static unsigned long to_ratio(u64 period, u64 runtime)
8716{
8717 if (runtime == RUNTIME_INF)
Peter Zijlstra9a7e0b12008-08-19 12:33:06 +02008718 return 1ULL << 20;
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01008719
Peter Zijlstra9a7e0b12008-08-19 12:33:06 +02008720 return div64_u64(runtime << 20, period);
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01008721}
Paul Turnera790de92011-07-21 09:43:29 -07008722#endif
8723
8724#ifdef CONFIG_RT_GROUP_SCHED
8725/*
8726 * Ensure that the real time constraints are schedulable.
8727 */
8728static DEFINE_MUTEX(rt_constraints_mutex);
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01008729
Dhaval Giani521f1a242008-02-28 15:21:56 +05308730/* Must be called with tasklist_lock held */
8731static inline int tg_has_rt_tasks(struct task_group *tg)
8732{
8733 struct task_struct *g, *p;
Peter Zijlstra9a7e0b12008-08-19 12:33:06 +02008734
Dhaval Giani521f1a242008-02-28 15:21:56 +05308735 do_each_thread(g, p) {
8736 if (rt_task(p) && rt_rq_of_se(&p->rt)->tg == tg)
8737 return 1;
8738 } while_each_thread(g, p);
Peter Zijlstra9a7e0b12008-08-19 12:33:06 +02008739
Dhaval Giani521f1a242008-02-28 15:21:56 +05308740 return 0;
8741}
8742
Peter Zijlstra9a7e0b12008-08-19 12:33:06 +02008743struct rt_schedulable_data {
8744 struct task_group *tg;
8745 u64 rt_period;
8746 u64 rt_runtime;
8747};
8748
Paul Turnera790de92011-07-21 09:43:29 -07008749static int tg_rt_schedulable(struct task_group *tg, void *data)
Peter Zijlstra9a7e0b12008-08-19 12:33:06 +02008750{
8751 struct rt_schedulable_data *d = data;
8752 struct task_group *child;
8753 unsigned long total, sum = 0;
8754 u64 period, runtime;
8755
8756 period = ktime_to_ns(tg->rt_bandwidth.rt_period);
8757 runtime = tg->rt_bandwidth.rt_runtime;
8758
8759 if (tg == d->tg) {
8760 period = d->rt_period;
8761 runtime = d->rt_runtime;
8762 }
8763
Peter Zijlstra4653f802008-09-23 15:33:44 +02008764 /*
8765 * Cannot have more runtime than the period.
8766 */
8767 if (runtime > period && runtime != RUNTIME_INF)
8768 return -EINVAL;
8769
8770 /*
8771 * Ensure we don't starve existing RT tasks.
8772 */
Peter Zijlstra9a7e0b12008-08-19 12:33:06 +02008773 if (rt_bandwidth_enabled() && !runtime && tg_has_rt_tasks(tg))
8774 return -EBUSY;
8775
8776 total = to_ratio(period, runtime);
8777
Peter Zijlstra4653f802008-09-23 15:33:44 +02008778 /*
8779 * Nobody can have more than the global setting allows.
8780 */
8781 if (total > to_ratio(global_rt_period(), global_rt_runtime()))
8782 return -EINVAL;
8783
8784 /*
8785 * The sum of our children's runtime should not exceed our own.
8786 */
Peter Zijlstra9a7e0b12008-08-19 12:33:06 +02008787 list_for_each_entry_rcu(child, &tg->children, siblings) {
8788 period = ktime_to_ns(child->rt_bandwidth.rt_period);
8789 runtime = child->rt_bandwidth.rt_runtime;
8790
8791 if (child == d->tg) {
8792 period = d->rt_period;
8793 runtime = d->rt_runtime;
8794 }
8795
8796 sum += to_ratio(period, runtime);
8797 }
8798
8799 if (sum > total)
8800 return -EINVAL;
8801
8802 return 0;
8803}
8804
8805static int __rt_schedulable(struct task_group *tg, u64 period, u64 runtime)
8806{
8807 struct rt_schedulable_data data = {
8808 .tg = tg,
8809 .rt_period = period,
8810 .rt_runtime = runtime,
8811 };
8812
Paul Turnera790de92011-07-21 09:43:29 -07008813 return walk_tg_tree(tg_rt_schedulable, tg_nop, &data);
Peter Zijlstra9a7e0b12008-08-19 12:33:06 +02008814}
8815
Paul Turnerab84d312011-07-21 09:43:28 -07008816static int tg_set_rt_bandwidth(struct task_group *tg,
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008817 u64 rt_period, u64 rt_runtime)
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008818{
Peter Zijlstraac086bc2008-04-19 19:44:58 +02008819 int i, err = 0;
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01008820
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01008821 mutex_lock(&rt_constraints_mutex);
Dhaval Giani521f1a242008-02-28 15:21:56 +05308822 read_lock(&tasklist_lock);
Peter Zijlstra9a7e0b12008-08-19 12:33:06 +02008823 err = __rt_schedulable(tg, rt_period, rt_runtime);
8824 if (err)
Dhaval Giani521f1a242008-02-28 15:21:56 +05308825 goto unlock;
Peter Zijlstraac086bc2008-04-19 19:44:58 +02008826
Thomas Gleixner0986b112009-11-17 15:32:06 +01008827 raw_spin_lock_irq(&tg->rt_bandwidth.rt_runtime_lock);
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008828 tg->rt_bandwidth.rt_period = ns_to_ktime(rt_period);
8829 tg->rt_bandwidth.rt_runtime = rt_runtime;
Peter Zijlstraac086bc2008-04-19 19:44:58 +02008830
8831 for_each_possible_cpu(i) {
8832 struct rt_rq *rt_rq = tg->rt_rq[i];
8833
Thomas Gleixner0986b112009-11-17 15:32:06 +01008834 raw_spin_lock(&rt_rq->rt_runtime_lock);
Peter Zijlstraac086bc2008-04-19 19:44:58 +02008835 rt_rq->rt_runtime = rt_runtime;
Thomas Gleixner0986b112009-11-17 15:32:06 +01008836 raw_spin_unlock(&rt_rq->rt_runtime_lock);
Peter Zijlstraac086bc2008-04-19 19:44:58 +02008837 }
Thomas Gleixner0986b112009-11-17 15:32:06 +01008838 raw_spin_unlock_irq(&tg->rt_bandwidth.rt_runtime_lock);
Peter Zijlstra49246272010-10-17 21:46:10 +02008839unlock:
Dhaval Giani521f1a242008-02-28 15:21:56 +05308840 read_unlock(&tasklist_lock);
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01008841 mutex_unlock(&rt_constraints_mutex);
8842
8843 return err;
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008844}
8845
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008846int sched_group_set_rt_runtime(struct task_group *tg, long rt_runtime_us)
8847{
8848 u64 rt_runtime, rt_period;
8849
8850 rt_period = ktime_to_ns(tg->rt_bandwidth.rt_period);
8851 rt_runtime = (u64)rt_runtime_us * NSEC_PER_USEC;
8852 if (rt_runtime_us < 0)
8853 rt_runtime = RUNTIME_INF;
8854
Paul Turnerab84d312011-07-21 09:43:28 -07008855 return tg_set_rt_bandwidth(tg, rt_period, rt_runtime);
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008856}
8857
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01008858long sched_group_rt_runtime(struct task_group *tg)
8859{
8860 u64 rt_runtime_us;
8861
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008862 if (tg->rt_bandwidth.rt_runtime == RUNTIME_INF)
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01008863 return -1;
8864
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008865 rt_runtime_us = tg->rt_bandwidth.rt_runtime;
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01008866 do_div(rt_runtime_us, NSEC_PER_USEC);
8867 return rt_runtime_us;
8868}
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008869
8870int sched_group_set_rt_period(struct task_group *tg, long rt_period_us)
8871{
8872 u64 rt_runtime, rt_period;
8873
8874 rt_period = (u64)rt_period_us * NSEC_PER_USEC;
8875 rt_runtime = tg->rt_bandwidth.rt_runtime;
8876
Raistlin619b0482008-06-26 18:54:09 +02008877 if (rt_period == 0)
8878 return -EINVAL;
8879
Paul Turnerab84d312011-07-21 09:43:28 -07008880 return tg_set_rt_bandwidth(tg, rt_period, rt_runtime);
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008881}
8882
8883long sched_group_rt_period(struct task_group *tg)
8884{
8885 u64 rt_period_us;
8886
8887 rt_period_us = ktime_to_ns(tg->rt_bandwidth.rt_period);
8888 do_div(rt_period_us, NSEC_PER_USEC);
8889 return rt_period_us;
8890}
8891
8892static int sched_rt_global_constraints(void)
8893{
Peter Zijlstra4653f802008-09-23 15:33:44 +02008894 u64 runtime, period;
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008895 int ret = 0;
8896
Hiroshi Shimamotoec5d4982008-09-10 17:00:19 -07008897 if (sysctl_sched_rt_period <= 0)
8898 return -EINVAL;
8899
Peter Zijlstra4653f802008-09-23 15:33:44 +02008900 runtime = global_rt_runtime();
8901 period = global_rt_period();
8902
8903 /*
8904 * Sanity check on the sysctl variables.
8905 */
8906 if (runtime > period && runtime != RUNTIME_INF)
8907 return -EINVAL;
Peter Zijlstra10b612f2008-06-19 14:22:27 +02008908
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008909 mutex_lock(&rt_constraints_mutex);
Peter Zijlstra9a7e0b12008-08-19 12:33:06 +02008910 read_lock(&tasklist_lock);
Peter Zijlstra4653f802008-09-23 15:33:44 +02008911 ret = __rt_schedulable(NULL, 0, 0);
Peter Zijlstra9a7e0b12008-08-19 12:33:06 +02008912 read_unlock(&tasklist_lock);
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008913 mutex_unlock(&rt_constraints_mutex);
8914
8915 return ret;
8916}
Dhaval Giani54e99122009-02-27 15:13:54 +05308917
8918int sched_rt_can_attach(struct task_group *tg, struct task_struct *tsk)
8919{
8920 /* Don't accept realtime tasks when there is no way for them to run */
8921 if (rt_task(tsk) && tg->rt_bandwidth.rt_runtime == 0)
8922 return 0;
8923
8924 return 1;
8925}
8926
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +02008927#else /* !CONFIG_RT_GROUP_SCHED */
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008928static int sched_rt_global_constraints(void)
8929{
Peter Zijlstraac086bc2008-04-19 19:44:58 +02008930 unsigned long flags;
8931 int i;
8932
Hiroshi Shimamotoec5d4982008-09-10 17:00:19 -07008933 if (sysctl_sched_rt_period <= 0)
8934 return -EINVAL;
8935
Peter Zijlstra60aa6052009-05-05 17:50:21 +02008936 /*
8937 * There's always some RT tasks in the root group
8938 * -- migration, kstopmachine etc..
8939 */
8940 if (sysctl_sched_rt_runtime == 0)
8941 return -EBUSY;
8942
Thomas Gleixner0986b112009-11-17 15:32:06 +01008943 raw_spin_lock_irqsave(&def_rt_bandwidth.rt_runtime_lock, flags);
Peter Zijlstraac086bc2008-04-19 19:44:58 +02008944 for_each_possible_cpu(i) {
8945 struct rt_rq *rt_rq = &cpu_rq(i)->rt;
8946
Thomas Gleixner0986b112009-11-17 15:32:06 +01008947 raw_spin_lock(&rt_rq->rt_runtime_lock);
Peter Zijlstraac086bc2008-04-19 19:44:58 +02008948 rt_rq->rt_runtime = global_rt_runtime();
Thomas Gleixner0986b112009-11-17 15:32:06 +01008949 raw_spin_unlock(&rt_rq->rt_runtime_lock);
Peter Zijlstraac086bc2008-04-19 19:44:58 +02008950 }
Thomas Gleixner0986b112009-11-17 15:32:06 +01008951 raw_spin_unlock_irqrestore(&def_rt_bandwidth.rt_runtime_lock, flags);
Peter Zijlstraac086bc2008-04-19 19:44:58 +02008952
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008953 return 0;
8954}
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +02008955#endif /* CONFIG_RT_GROUP_SCHED */
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008956
8957int sched_rt_handler(struct ctl_table *table, int write,
Alexey Dobriyan8d65af72009-09-23 15:57:19 -07008958 void __user *buffer, size_t *lenp,
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008959 loff_t *ppos)
8960{
8961 int ret;
8962 int old_period, old_runtime;
8963 static DEFINE_MUTEX(mutex);
8964
8965 mutex_lock(&mutex);
8966 old_period = sysctl_sched_rt_period;
8967 old_runtime = sysctl_sched_rt_runtime;
8968
Alexey Dobriyan8d65af72009-09-23 15:57:19 -07008969 ret = proc_dointvec(table, write, buffer, lenp, ppos);
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008970
8971 if (!ret && write) {
8972 ret = sched_rt_global_constraints();
8973 if (ret) {
8974 sysctl_sched_rt_period = old_period;
8975 sysctl_sched_rt_runtime = old_runtime;
8976 } else {
8977 def_rt_bandwidth.rt_runtime = global_rt_runtime();
8978 def_rt_bandwidth.rt_period =
8979 ns_to_ktime(global_rt_period());
8980 }
8981 }
8982 mutex_unlock(&mutex);
8983
8984 return ret;
8985}
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008986
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01008987#ifdef CONFIG_CGROUP_SCHED
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008988
8989/* return corresponding task_group object of a cgroup */
Paul Menage2b01dfe2007-10-24 18:23:50 +02008990static inline struct task_group *cgroup_tg(struct cgroup *cgrp)
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008991{
Paul Menage2b01dfe2007-10-24 18:23:50 +02008992 return container_of(cgroup_subsys_state(cgrp, cpu_cgroup_subsys_id),
8993 struct task_group, css);
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008994}
8995
8996static struct cgroup_subsys_state *
Paul Menage2b01dfe2007-10-24 18:23:50 +02008997cpu_cgroup_create(struct cgroup_subsys *ss, struct cgroup *cgrp)
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008998{
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008999 struct task_group *tg, *parent;
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07009000
Paul Menage2b01dfe2007-10-24 18:23:50 +02009001 if (!cgrp->parent) {
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07009002 /* This is early initialization for the top cgroup */
Yong Zhang07e06b02011-01-07 15:17:36 +08009003 return &root_task_group.css;
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07009004 }
9005
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02009006 parent = cgroup_tg(cgrp->parent);
9007 tg = sched_create_group(parent);
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07009008 if (IS_ERR(tg))
9009 return ERR_PTR(-ENOMEM);
9010
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07009011 return &tg->css;
9012}
9013
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01009014static void
9015cpu_cgroup_destroy(struct cgroup_subsys *ss, struct cgroup *cgrp)
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07009016{
Paul Menage2b01dfe2007-10-24 18:23:50 +02009017 struct task_group *tg = cgroup_tg(cgrp);
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07009018
9019 sched_destroy_group(tg);
9020}
9021
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01009022static int
Ben Blumbe367d02009-09-23 15:56:31 -07009023cpu_cgroup_can_attach_task(struct cgroup *cgrp, struct task_struct *tsk)
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07009024{
Peter Zijlstrab68aa232008-02-13 15:45:40 +01009025#ifdef CONFIG_RT_GROUP_SCHED
Dhaval Giani54e99122009-02-27 15:13:54 +05309026 if (!sched_rt_can_attach(cgroup_tg(cgrp), tsk))
Peter Zijlstrab68aa232008-02-13 15:45:40 +01009027 return -EINVAL;
9028#else
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07009029 /* We don't support RT-tasks being in separate groups */
9030 if (tsk->sched_class != &fair_sched_class)
9031 return -EINVAL;
Peter Zijlstrab68aa232008-02-13 15:45:40 +01009032#endif
Ben Blumbe367d02009-09-23 15:56:31 -07009033 return 0;
9034}
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07009035
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07009036static void
Ben Blumf780bdb2011-05-26 16:25:19 -07009037cpu_cgroup_attach_task(struct cgroup *cgrp, struct task_struct *tsk)
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07009038{
9039 sched_move_task(tsk);
9040}
9041
Peter Zijlstra068c5cc2011-01-19 12:26:11 +01009042static void
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01009043cpu_cgroup_exit(struct cgroup_subsys *ss, struct cgroup *cgrp,
9044 struct cgroup *old_cgrp, struct task_struct *task)
Peter Zijlstra068c5cc2011-01-19 12:26:11 +01009045{
9046 /*
9047 * cgroup_exit() is called in the copy_process() failure path.
9048 * Ignore this case since the task hasn't ran yet, this avoids
9049 * trying to poke a half freed task state from generic code.
9050 */
9051 if (!(task->flags & PF_EXITING))
9052 return;
9053
9054 sched_move_task(task);
9055}
9056
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01009057#ifdef CONFIG_FAIR_GROUP_SCHED
Paul Menagef4c753b2008-04-29 00:59:56 -07009058static int cpu_shares_write_u64(struct cgroup *cgrp, struct cftype *cftype,
Paul Menage2b01dfe2007-10-24 18:23:50 +02009059 u64 shareval)
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07009060{
Nikhil Raoc8b28112011-05-18 14:37:48 -07009061 return sched_group_set_shares(cgroup_tg(cgrp), scale_load(shareval));
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07009062}
9063
Paul Menagef4c753b2008-04-29 00:59:56 -07009064static u64 cpu_shares_read_u64(struct cgroup *cgrp, struct cftype *cft)
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07009065{
Paul Menage2b01dfe2007-10-24 18:23:50 +02009066 struct task_group *tg = cgroup_tg(cgrp);
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07009067
Nikhil Raoc8b28112011-05-18 14:37:48 -07009068 return (u64) scale_load_down(tg->shares);
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07009069}
Paul Turnerab84d312011-07-21 09:43:28 -07009070
9071#ifdef CONFIG_CFS_BANDWIDTH
Paul Turnera790de92011-07-21 09:43:29 -07009072static DEFINE_MUTEX(cfs_constraints_mutex);
9073
Paul Turnerab84d312011-07-21 09:43:28 -07009074const u64 max_cfs_quota_period = 1 * NSEC_PER_SEC; /* 1s */
9075const u64 min_cfs_quota_period = 1 * NSEC_PER_MSEC; /* 1ms */
9076
Paul Turnera790de92011-07-21 09:43:29 -07009077static int __cfs_schedulable(struct task_group *tg, u64 period, u64 runtime);
9078
Paul Turnerab84d312011-07-21 09:43:28 -07009079static int tg_set_cfs_bandwidth(struct task_group *tg, u64 period, u64 quota)
9080{
Paul Turnera790de92011-07-21 09:43:29 -07009081 int i, ret = 0;
Paul Turnerab84d312011-07-21 09:43:28 -07009082 struct cfs_bandwidth *cfs_b = tg_cfs_bandwidth(tg);
Paul Turnerab84d312011-07-21 09:43:28 -07009083
9084 if (tg == &root_task_group)
9085 return -EINVAL;
9086
9087 /*
9088 * Ensure we have at some amount of bandwidth every period. This is
9089 * to prevent reaching a state of large arrears when throttled via
9090 * entity_tick() resulting in prolonged exit starvation.
9091 */
9092 if (quota < min_cfs_quota_period || period < min_cfs_quota_period)
9093 return -EINVAL;
9094
9095 /*
9096 * Likewise, bound things on the otherside by preventing insane quota
9097 * periods. This also allows us to normalize in computing quota
9098 * feasibility.
9099 */
9100 if (period > max_cfs_quota_period)
9101 return -EINVAL;
9102
Paul Turnera790de92011-07-21 09:43:29 -07009103 mutex_lock(&cfs_constraints_mutex);
9104 ret = __cfs_schedulable(tg, period, quota);
9105 if (ret)
9106 goto out_unlock;
9107
Paul Turnerab84d312011-07-21 09:43:28 -07009108 raw_spin_lock_irq(&cfs_b->lock);
9109 cfs_b->period = ns_to_ktime(period);
9110 cfs_b->quota = quota;
Paul Turnerec12cb72011-07-21 09:43:30 -07009111 cfs_b->runtime = quota;
Paul Turnerab84d312011-07-21 09:43:28 -07009112 raw_spin_unlock_irq(&cfs_b->lock);
9113
9114 for_each_possible_cpu(i) {
9115 struct cfs_rq *cfs_rq = tg->cfs_rq[i];
9116 struct rq *rq = rq_of(cfs_rq);
9117
9118 raw_spin_lock_irq(&rq->lock);
9119 cfs_rq->runtime_enabled = quota != RUNTIME_INF;
9120 cfs_rq->runtime_remaining = 0;
9121 raw_spin_unlock_irq(&rq->lock);
9122 }
Paul Turnera790de92011-07-21 09:43:29 -07009123out_unlock:
9124 mutex_unlock(&cfs_constraints_mutex);
Paul Turnerab84d312011-07-21 09:43:28 -07009125
Paul Turnera790de92011-07-21 09:43:29 -07009126 return ret;
Paul Turnerab84d312011-07-21 09:43:28 -07009127}
9128
9129int tg_set_cfs_quota(struct task_group *tg, long cfs_quota_us)
9130{
9131 u64 quota, period;
9132
9133 period = ktime_to_ns(tg_cfs_bandwidth(tg)->period);
9134 if (cfs_quota_us < 0)
9135 quota = RUNTIME_INF;
9136 else
9137 quota = (u64)cfs_quota_us * NSEC_PER_USEC;
9138
9139 return tg_set_cfs_bandwidth(tg, period, quota);
9140}
9141
9142long tg_get_cfs_quota(struct task_group *tg)
9143{
9144 u64 quota_us;
9145
9146 if (tg_cfs_bandwidth(tg)->quota == RUNTIME_INF)
9147 return -1;
9148
9149 quota_us = tg_cfs_bandwidth(tg)->quota;
9150 do_div(quota_us, NSEC_PER_USEC);
9151
9152 return quota_us;
9153}
9154
9155int tg_set_cfs_period(struct task_group *tg, long cfs_period_us)
9156{
9157 u64 quota, period;
9158
9159 period = (u64)cfs_period_us * NSEC_PER_USEC;
9160 quota = tg_cfs_bandwidth(tg)->quota;
9161
9162 if (period <= 0)
9163 return -EINVAL;
9164
9165 return tg_set_cfs_bandwidth(tg, period, quota);
9166}
9167
9168long tg_get_cfs_period(struct task_group *tg)
9169{
9170 u64 cfs_period_us;
9171
9172 cfs_period_us = ktime_to_ns(tg_cfs_bandwidth(tg)->period);
9173 do_div(cfs_period_us, NSEC_PER_USEC);
9174
9175 return cfs_period_us;
9176}
9177
9178static s64 cpu_cfs_quota_read_s64(struct cgroup *cgrp, struct cftype *cft)
9179{
9180 return tg_get_cfs_quota(cgroup_tg(cgrp));
9181}
9182
9183static int cpu_cfs_quota_write_s64(struct cgroup *cgrp, struct cftype *cftype,
9184 s64 cfs_quota_us)
9185{
9186 return tg_set_cfs_quota(cgroup_tg(cgrp), cfs_quota_us);
9187}
9188
9189static u64 cpu_cfs_period_read_u64(struct cgroup *cgrp, struct cftype *cft)
9190{
9191 return tg_get_cfs_period(cgroup_tg(cgrp));
9192}
9193
9194static int cpu_cfs_period_write_u64(struct cgroup *cgrp, struct cftype *cftype,
9195 u64 cfs_period_us)
9196{
9197 return tg_set_cfs_period(cgroup_tg(cgrp), cfs_period_us);
9198}
9199
Paul Turnera790de92011-07-21 09:43:29 -07009200struct cfs_schedulable_data {
9201 struct task_group *tg;
9202 u64 period, quota;
9203};
9204
9205/*
9206 * normalize group quota/period to be quota/max_period
9207 * note: units are usecs
9208 */
9209static u64 normalize_cfs_quota(struct task_group *tg,
9210 struct cfs_schedulable_data *d)
9211{
9212 u64 quota, period;
9213
9214 if (tg == d->tg) {
9215 period = d->period;
9216 quota = d->quota;
9217 } else {
9218 period = tg_get_cfs_period(tg);
9219 quota = tg_get_cfs_quota(tg);
9220 }
9221
9222 /* note: these should typically be equivalent */
9223 if (quota == RUNTIME_INF || quota == -1)
9224 return RUNTIME_INF;
9225
9226 return to_ratio(period, quota);
9227}
9228
9229static int tg_cfs_schedulable_down(struct task_group *tg, void *data)
9230{
9231 struct cfs_schedulable_data *d = data;
9232 struct cfs_bandwidth *cfs_b = tg_cfs_bandwidth(tg);
9233 s64 quota = 0, parent_quota = -1;
9234
9235 if (!tg->parent) {
9236 quota = RUNTIME_INF;
9237 } else {
9238 struct cfs_bandwidth *parent_b = tg_cfs_bandwidth(tg->parent);
9239
9240 quota = normalize_cfs_quota(tg, d);
9241 parent_quota = parent_b->hierarchal_quota;
9242
9243 /*
9244 * ensure max(child_quota) <= parent_quota, inherit when no
9245 * limit is set
9246 */
9247 if (quota == RUNTIME_INF)
9248 quota = parent_quota;
9249 else if (parent_quota != RUNTIME_INF && quota > parent_quota)
9250 return -EINVAL;
9251 }
9252 cfs_b->hierarchal_quota = quota;
9253
9254 return 0;
9255}
9256
9257static int __cfs_schedulable(struct task_group *tg, u64 period, u64 quota)
9258{
9259 struct cfs_schedulable_data data = {
9260 .tg = tg,
9261 .period = period,
9262 .quota = quota,
9263 };
9264
9265 if (quota != RUNTIME_INF) {
9266 do_div(data.period, NSEC_PER_USEC);
9267 do_div(data.quota, NSEC_PER_USEC);
9268 }
9269
9270 return walk_tg_tree(tg_cfs_schedulable_down, tg_nop, &data);
9271}
Paul Turnerab84d312011-07-21 09:43:28 -07009272#endif /* CONFIG_CFS_BANDWIDTH */
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +02009273#endif /* CONFIG_FAIR_GROUP_SCHED */
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07009274
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01009275#ifdef CONFIG_RT_GROUP_SCHED
Mirco Tischler0c708142008-05-14 16:05:46 -07009276static int cpu_rt_runtime_write(struct cgroup *cgrp, struct cftype *cft,
Paul Menage06ecb272008-04-29 01:00:06 -07009277 s64 val)
Peter Zijlstra6f505b12008-01-25 21:08:30 +01009278{
Paul Menage06ecb272008-04-29 01:00:06 -07009279 return sched_group_set_rt_runtime(cgroup_tg(cgrp), val);
Peter Zijlstra6f505b12008-01-25 21:08:30 +01009280}
9281
Paul Menage06ecb272008-04-29 01:00:06 -07009282static s64 cpu_rt_runtime_read(struct cgroup *cgrp, struct cftype *cft)
Peter Zijlstra6f505b12008-01-25 21:08:30 +01009283{
Paul Menage06ecb272008-04-29 01:00:06 -07009284 return sched_group_rt_runtime(cgroup_tg(cgrp));
Peter Zijlstra6f505b12008-01-25 21:08:30 +01009285}
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02009286
9287static int cpu_rt_period_write_uint(struct cgroup *cgrp, struct cftype *cftype,
9288 u64 rt_period_us)
9289{
9290 return sched_group_set_rt_period(cgroup_tg(cgrp), rt_period_us);
9291}
9292
9293static u64 cpu_rt_period_read_uint(struct cgroup *cgrp, struct cftype *cft)
9294{
9295 return sched_group_rt_period(cgroup_tg(cgrp));
9296}
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +02009297#endif /* CONFIG_RT_GROUP_SCHED */
Peter Zijlstra6f505b12008-01-25 21:08:30 +01009298
Paul Menagefe5c7cc2007-10-29 21:18:11 +01009299static struct cftype cpu_files[] = {
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01009300#ifdef CONFIG_FAIR_GROUP_SCHED
Paul Menagefe5c7cc2007-10-29 21:18:11 +01009301 {
9302 .name = "shares",
Paul Menagef4c753b2008-04-29 00:59:56 -07009303 .read_u64 = cpu_shares_read_u64,
9304 .write_u64 = cpu_shares_write_u64,
Paul Menagefe5c7cc2007-10-29 21:18:11 +01009305 },
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01009306#endif
Paul Turnerab84d312011-07-21 09:43:28 -07009307#ifdef CONFIG_CFS_BANDWIDTH
9308 {
9309 .name = "cfs_quota_us",
9310 .read_s64 = cpu_cfs_quota_read_s64,
9311 .write_s64 = cpu_cfs_quota_write_s64,
9312 },
9313 {
9314 .name = "cfs_period_us",
9315 .read_u64 = cpu_cfs_period_read_u64,
9316 .write_u64 = cpu_cfs_period_write_u64,
9317 },
9318#endif
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01009319#ifdef CONFIG_RT_GROUP_SCHED
Peter Zijlstra6f505b12008-01-25 21:08:30 +01009320 {
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01009321 .name = "rt_runtime_us",
Paul Menage06ecb272008-04-29 01:00:06 -07009322 .read_s64 = cpu_rt_runtime_read,
9323 .write_s64 = cpu_rt_runtime_write,
Peter Zijlstra6f505b12008-01-25 21:08:30 +01009324 },
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02009325 {
9326 .name = "rt_period_us",
Paul Menagef4c753b2008-04-29 00:59:56 -07009327 .read_u64 = cpu_rt_period_read_uint,
9328 .write_u64 = cpu_rt_period_write_uint,
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02009329 },
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01009330#endif
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07009331};
9332
9333static int cpu_cgroup_populate(struct cgroup_subsys *ss, struct cgroup *cont)
9334{
Paul Menagefe5c7cc2007-10-29 21:18:11 +01009335 return cgroup_add_files(cont, ss, cpu_files, ARRAY_SIZE(cpu_files));
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07009336}
9337
9338struct cgroup_subsys cpu_cgroup_subsys = {
Ingo Molnar38605ca2007-10-29 21:18:11 +01009339 .name = "cpu",
9340 .create = cpu_cgroup_create,
9341 .destroy = cpu_cgroup_destroy,
Ben Blumf780bdb2011-05-26 16:25:19 -07009342 .can_attach_task = cpu_cgroup_can_attach_task,
9343 .attach_task = cpu_cgroup_attach_task,
Peter Zijlstra068c5cc2011-01-19 12:26:11 +01009344 .exit = cpu_cgroup_exit,
Ingo Molnar38605ca2007-10-29 21:18:11 +01009345 .populate = cpu_cgroup_populate,
9346 .subsys_id = cpu_cgroup_subsys_id,
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07009347 .early_init = 1,
9348};
9349
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01009350#endif /* CONFIG_CGROUP_SCHED */
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01009351
9352#ifdef CONFIG_CGROUP_CPUACCT
9353
9354/*
9355 * CPU accounting code for task groups.
9356 *
9357 * Based on the work by Paul Menage (menage@google.com) and Balbir Singh
9358 * (balbir@in.ibm.com).
9359 */
9360
Bharata B Rao934352f2008-11-10 20:41:13 +05309361/* track cpu usage of a group of tasks and its child groups */
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01009362struct cpuacct {
9363 struct cgroup_subsys_state css;
9364 /* cpuusage holds pointer to a u64-type object on every cpu */
Tejun Heo43cf38e2010-02-02 14:38:57 +09009365 u64 __percpu *cpuusage;
Bharata B Raoef12fef2009-03-31 10:02:22 +05309366 struct percpu_counter cpustat[CPUACCT_STAT_NSTATS];
Bharata B Rao934352f2008-11-10 20:41:13 +05309367 struct cpuacct *parent;
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01009368};
9369
9370struct cgroup_subsys cpuacct_subsys;
9371
9372/* return cpu accounting group corresponding to this container */
Dhaval Giani32cd7562008-02-29 10:02:43 +05309373static inline struct cpuacct *cgroup_ca(struct cgroup *cgrp)
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01009374{
Dhaval Giani32cd7562008-02-29 10:02:43 +05309375 return container_of(cgroup_subsys_state(cgrp, cpuacct_subsys_id),
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01009376 struct cpuacct, css);
9377}
9378
9379/* return cpu accounting group to which this task belongs */
9380static inline struct cpuacct *task_ca(struct task_struct *tsk)
9381{
9382 return container_of(task_subsys_state(tsk, cpuacct_subsys_id),
9383 struct cpuacct, css);
9384}
9385
9386/* create a new cpu accounting group */
9387static struct cgroup_subsys_state *cpuacct_create(
Dhaval Giani32cd7562008-02-29 10:02:43 +05309388 struct cgroup_subsys *ss, struct cgroup *cgrp)
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01009389{
9390 struct cpuacct *ca = kzalloc(sizeof(*ca), GFP_KERNEL);
Bharata B Raoef12fef2009-03-31 10:02:22 +05309391 int i;
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01009392
9393 if (!ca)
Bharata B Raoef12fef2009-03-31 10:02:22 +05309394 goto out;
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01009395
9396 ca->cpuusage = alloc_percpu(u64);
Bharata B Raoef12fef2009-03-31 10:02:22 +05309397 if (!ca->cpuusage)
9398 goto out_free_ca;
9399
9400 for (i = 0; i < CPUACCT_STAT_NSTATS; i++)
9401 if (percpu_counter_init(&ca->cpustat[i], 0))
9402 goto out_free_counters;
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01009403
Bharata B Rao934352f2008-11-10 20:41:13 +05309404 if (cgrp->parent)
9405 ca->parent = cgroup_ca(cgrp->parent);
9406
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01009407 return &ca->css;
Bharata B Raoef12fef2009-03-31 10:02:22 +05309408
9409out_free_counters:
9410 while (--i >= 0)
9411 percpu_counter_destroy(&ca->cpustat[i]);
9412 free_percpu(ca->cpuusage);
9413out_free_ca:
9414 kfree(ca);
9415out:
9416 return ERR_PTR(-ENOMEM);
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01009417}
9418
9419/* destroy an existing cpu accounting group */
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01009420static void
Dhaval Giani32cd7562008-02-29 10:02:43 +05309421cpuacct_destroy(struct cgroup_subsys *ss, struct cgroup *cgrp)
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01009422{
Dhaval Giani32cd7562008-02-29 10:02:43 +05309423 struct cpuacct *ca = cgroup_ca(cgrp);
Bharata B Raoef12fef2009-03-31 10:02:22 +05309424 int i;
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01009425
Bharata B Raoef12fef2009-03-31 10:02:22 +05309426 for (i = 0; i < CPUACCT_STAT_NSTATS; i++)
9427 percpu_counter_destroy(&ca->cpustat[i]);
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01009428 free_percpu(ca->cpuusage);
9429 kfree(ca);
9430}
9431
Ken Chen720f5492008-12-15 22:02:01 -08009432static u64 cpuacct_cpuusage_read(struct cpuacct *ca, int cpu)
9433{
Rusty Russellb36128c2009-02-20 16:29:08 +09009434 u64 *cpuusage = per_cpu_ptr(ca->cpuusage, cpu);
Ken Chen720f5492008-12-15 22:02:01 -08009435 u64 data;
9436
9437#ifndef CONFIG_64BIT
9438 /*
9439 * Take rq->lock to make 64-bit read safe on 32-bit platforms.
9440 */
Thomas Gleixner05fa7852009-11-17 14:28:38 +01009441 raw_spin_lock_irq(&cpu_rq(cpu)->lock);
Ken Chen720f5492008-12-15 22:02:01 -08009442 data = *cpuusage;
Thomas Gleixner05fa7852009-11-17 14:28:38 +01009443 raw_spin_unlock_irq(&cpu_rq(cpu)->lock);
Ken Chen720f5492008-12-15 22:02:01 -08009444#else
9445 data = *cpuusage;
9446#endif
9447
9448 return data;
9449}
9450
9451static void cpuacct_cpuusage_write(struct cpuacct *ca, int cpu, u64 val)
9452{
Rusty Russellb36128c2009-02-20 16:29:08 +09009453 u64 *cpuusage = per_cpu_ptr(ca->cpuusage, cpu);
Ken Chen720f5492008-12-15 22:02:01 -08009454
9455#ifndef CONFIG_64BIT
9456 /*
9457 * Take rq->lock to make 64-bit write safe on 32-bit platforms.
9458 */
Thomas Gleixner05fa7852009-11-17 14:28:38 +01009459 raw_spin_lock_irq(&cpu_rq(cpu)->lock);
Ken Chen720f5492008-12-15 22:02:01 -08009460 *cpuusage = val;
Thomas Gleixner05fa7852009-11-17 14:28:38 +01009461 raw_spin_unlock_irq(&cpu_rq(cpu)->lock);
Ken Chen720f5492008-12-15 22:02:01 -08009462#else
9463 *cpuusage = val;
9464#endif
9465}
9466
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01009467/* return total cpu usage (in nanoseconds) of a group */
Dhaval Giani32cd7562008-02-29 10:02:43 +05309468static u64 cpuusage_read(struct cgroup *cgrp, struct cftype *cft)
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01009469{
Dhaval Giani32cd7562008-02-29 10:02:43 +05309470 struct cpuacct *ca = cgroup_ca(cgrp);
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01009471 u64 totalcpuusage = 0;
9472 int i;
9473
Ken Chen720f5492008-12-15 22:02:01 -08009474 for_each_present_cpu(i)
9475 totalcpuusage += cpuacct_cpuusage_read(ca, i);
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01009476
9477 return totalcpuusage;
9478}
9479
Dhaval Giani0297b802008-02-29 10:02:44 +05309480static int cpuusage_write(struct cgroup *cgrp, struct cftype *cftype,
9481 u64 reset)
9482{
9483 struct cpuacct *ca = cgroup_ca(cgrp);
9484 int err = 0;
9485 int i;
9486
9487 if (reset) {
9488 err = -EINVAL;
9489 goto out;
9490 }
9491
Ken Chen720f5492008-12-15 22:02:01 -08009492 for_each_present_cpu(i)
9493 cpuacct_cpuusage_write(ca, i, 0);
Dhaval Giani0297b802008-02-29 10:02:44 +05309494
Dhaval Giani0297b802008-02-29 10:02:44 +05309495out:
9496 return err;
9497}
9498
Ken Chene9515c32008-12-15 22:04:15 -08009499static int cpuacct_percpu_seq_read(struct cgroup *cgroup, struct cftype *cft,
9500 struct seq_file *m)
9501{
9502 struct cpuacct *ca = cgroup_ca(cgroup);
9503 u64 percpu;
9504 int i;
9505
9506 for_each_present_cpu(i) {
9507 percpu = cpuacct_cpuusage_read(ca, i);
9508 seq_printf(m, "%llu ", (unsigned long long) percpu);
9509 }
9510 seq_printf(m, "\n");
9511 return 0;
9512}
9513
Bharata B Raoef12fef2009-03-31 10:02:22 +05309514static const char *cpuacct_stat_desc[] = {
9515 [CPUACCT_STAT_USER] = "user",
9516 [CPUACCT_STAT_SYSTEM] = "system",
9517};
9518
9519static int cpuacct_stats_show(struct cgroup *cgrp, struct cftype *cft,
9520 struct cgroup_map_cb *cb)
9521{
9522 struct cpuacct *ca = cgroup_ca(cgrp);
9523 int i;
9524
9525 for (i = 0; i < CPUACCT_STAT_NSTATS; i++) {
9526 s64 val = percpu_counter_read(&ca->cpustat[i]);
9527 val = cputime64_to_clock_t(val);
9528 cb->fill(cb, cpuacct_stat_desc[i], val);
9529 }
9530 return 0;
9531}
9532
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01009533static struct cftype files[] = {
9534 {
9535 .name = "usage",
Paul Menagef4c753b2008-04-29 00:59:56 -07009536 .read_u64 = cpuusage_read,
9537 .write_u64 = cpuusage_write,
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01009538 },
Ken Chene9515c32008-12-15 22:04:15 -08009539 {
9540 .name = "usage_percpu",
9541 .read_seq_string = cpuacct_percpu_seq_read,
9542 },
Bharata B Raoef12fef2009-03-31 10:02:22 +05309543 {
9544 .name = "stat",
9545 .read_map = cpuacct_stats_show,
9546 },
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01009547};
9548
Dhaval Giani32cd7562008-02-29 10:02:43 +05309549static int cpuacct_populate(struct cgroup_subsys *ss, struct cgroup *cgrp)
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01009550{
Dhaval Giani32cd7562008-02-29 10:02:43 +05309551 return cgroup_add_files(cgrp, ss, files, ARRAY_SIZE(files));
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01009552}
9553
9554/*
9555 * charge this task's execution time to its accounting group.
9556 *
9557 * called with rq->lock held.
9558 */
9559static void cpuacct_charge(struct task_struct *tsk, u64 cputime)
9560{
9561 struct cpuacct *ca;
Bharata B Rao934352f2008-11-10 20:41:13 +05309562 int cpu;
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01009563
Li Zefanc40c6f82009-02-26 15:40:15 +08009564 if (unlikely(!cpuacct_subsys.active))
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01009565 return;
9566
Bharata B Rao934352f2008-11-10 20:41:13 +05309567 cpu = task_cpu(tsk);
Bharata B Raoa18b83b2009-03-23 10:02:53 +05309568
9569 rcu_read_lock();
9570
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01009571 ca = task_ca(tsk);
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01009572
Bharata B Rao934352f2008-11-10 20:41:13 +05309573 for (; ca; ca = ca->parent) {
Rusty Russellb36128c2009-02-20 16:29:08 +09009574 u64 *cpuusage = per_cpu_ptr(ca->cpuusage, cpu);
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01009575 *cpuusage += cputime;
9576 }
Bharata B Raoa18b83b2009-03-23 10:02:53 +05309577
9578 rcu_read_unlock();
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01009579}
9580
Bharata B Raoef12fef2009-03-31 10:02:22 +05309581/*
Anton Blanchardfa535a72010-02-02 14:46:13 -08009582 * When CONFIG_VIRT_CPU_ACCOUNTING is enabled one jiffy can be very large
9583 * in cputime_t units. As a result, cpuacct_update_stats calls
9584 * percpu_counter_add with values large enough to always overflow the
9585 * per cpu batch limit causing bad SMP scalability.
9586 *
9587 * To fix this we scale percpu_counter_batch by cputime_one_jiffy so we
9588 * batch the same amount of time with CONFIG_VIRT_CPU_ACCOUNTING disabled
9589 * and enabled. We cap it at INT_MAX which is the largest allowed batch value.
9590 */
9591#ifdef CONFIG_SMP
9592#define CPUACCT_BATCH \
9593 min_t(long, percpu_counter_batch * cputime_one_jiffy, INT_MAX)
9594#else
9595#define CPUACCT_BATCH 0
9596#endif
9597
9598/*
Bharata B Raoef12fef2009-03-31 10:02:22 +05309599 * Charge the system/user time to the task's accounting group.
9600 */
9601static void cpuacct_update_stats(struct task_struct *tsk,
9602 enum cpuacct_stat_index idx, cputime_t val)
9603{
9604 struct cpuacct *ca;
Anton Blanchardfa535a72010-02-02 14:46:13 -08009605 int batch = CPUACCT_BATCH;
Bharata B Raoef12fef2009-03-31 10:02:22 +05309606
9607 if (unlikely(!cpuacct_subsys.active))
9608 return;
9609
9610 rcu_read_lock();
9611 ca = task_ca(tsk);
9612
9613 do {
Anton Blanchardfa535a72010-02-02 14:46:13 -08009614 __percpu_counter_add(&ca->cpustat[idx], val, batch);
Bharata B Raoef12fef2009-03-31 10:02:22 +05309615 ca = ca->parent;
9616 } while (ca);
9617 rcu_read_unlock();
9618}
9619
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01009620struct cgroup_subsys cpuacct_subsys = {
9621 .name = "cpuacct",
9622 .create = cpuacct_create,
9623 .destroy = cpuacct_destroy,
9624 .populate = cpuacct_populate,
9625 .subsys_id = cpuacct_subsys_id,
9626};
9627#endif /* CONFIG_CGROUP_CPUACCT */