]>
Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* |
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 | |
c31f2e8a IM |
19 | * 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 | |
b9131769 IM |
25 | * 2007-11-29 RT balancing improvements by Steven Rostedt, Gregory Haskins, |
26 | * Thomas Gleixner, Mike Kravetz | |
1da177e4 LT |
27 | */ |
28 | ||
29 | #include <linux/mm.h> | |
30 | #include <linux/module.h> | |
31 | #include <linux/nmi.h> | |
32 | #include <linux/init.h> | |
dff06c15 | 33 | #include <linux/uaccess.h> |
1da177e4 | 34 | #include <linux/highmem.h> |
1da177e4 LT |
35 | #include <asm/mmu_context.h> |
36 | #include <linux/interrupt.h> | |
c59ede7b | 37 | #include <linux/capability.h> |
1da177e4 LT |
38 | #include <linux/completion.h> |
39 | #include <linux/kernel_stat.h> | |
9a11b49a | 40 | #include <linux/debug_locks.h> |
cdd6c482 | 41 | #include <linux/perf_event.h> |
1da177e4 LT |
42 | #include <linux/security.h> |
43 | #include <linux/notifier.h> | |
44 | #include <linux/profile.h> | |
7dfb7103 | 45 | #include <linux/freezer.h> |
198e2f18 | 46 | #include <linux/vmalloc.h> |
1da177e4 LT |
47 | #include <linux/blkdev.h> |
48 | #include <linux/delay.h> | |
b488893a | 49 | #include <linux/pid_namespace.h> |
1da177e4 LT |
50 | #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> | |
b5aadf7f | 57 | #include <linux/proc_fs.h> |
1da177e4 | 58 | #include <linux/seq_file.h> |
969c7921 | 59 | #include <linux/stop_machine.h> |
e692ab53 | 60 | #include <linux/sysctl.h> |
1da177e4 LT |
61 | #include <linux/syscalls.h> |
62 | #include <linux/times.h> | |
8f0ab514 | 63 | #include <linux/tsacct_kern.h> |
c6fd91f0 | 64 | #include <linux/kprobes.h> |
0ff92245 | 65 | #include <linux/delayacct.h> |
dff06c15 | 66 | #include <linux/unistd.h> |
f5ff8422 | 67 | #include <linux/pagemap.h> |
8f4d37ec | 68 | #include <linux/hrtimer.h> |
30914a58 | 69 | #include <linux/tick.h> |
f00b45c1 PZ |
70 | #include <linux/debugfs.h> |
71 | #include <linux/ctype.h> | |
6cd8a4bb | 72 | #include <linux/ftrace.h> |
5a0e3ad6 | 73 | #include <linux/slab.h> |
1da177e4 | 74 | |
5517d86b | 75 | #include <asm/tlb.h> |
838225b4 | 76 | #include <asm/irq_regs.h> |
335d7afb | 77 | #include <asm/mutex.h> |
e6e6685a GC |
78 | #ifdef CONFIG_PARAVIRT |
79 | #include <asm/paravirt.h> | |
80 | #endif | |
1da177e4 | 81 | |
6e0534f2 | 82 | #include "sched_cpupri.h" |
21aa9af0 | 83 | #include "workqueue_sched.h" |
5091faa4 | 84 | #include "sched_autogroup.h" |
6e0534f2 | 85 | |
a8d154b0 | 86 | #define CREATE_TRACE_POINTS |
ad8d75ff | 87 | #include <trace/events/sched.h> |
a8d154b0 | 88 | |
1da177e4 LT |
89 | /* |
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 | /* | |
d7876a08 | 108 | * Helpers for converting nanosecond timing to jiffy resolution |
1da177e4 | 109 | */ |
d6322faf | 110 | #define NS_TO_JIFFIES(TIME) ((unsigned long)(TIME) / (NSEC_PER_SEC / HZ)) |
1da177e4 | 111 | |
6aa645ea IM |
112 | #define NICE_0_LOAD SCHED_LOAD_SCALE |
113 | #define NICE_0_SHIFT SCHED_LOAD_SHIFT | |
114 | ||
1da177e4 LT |
115 | /* |
116 | * These are the 'tuning knobs' of the scheduler: | |
117 | * | |
a4ec24b4 | 118 | * default timeslice is 100 msecs (used only for SCHED_RR tasks). |
1da177e4 LT |
119 | * Timeslices get refilled after they expire. |
120 | */ | |
1da177e4 | 121 | #define DEF_TIMESLICE (100 * HZ / 1000) |
2dd73a4f | 122 | |
d0b27fa7 PZ |
123 | /* |
124 | * single value that denotes runtime == period, ie unlimited time. | |
125 | */ | |
126 | #define RUNTIME_INF ((u64)~0ULL) | |
127 | ||
e05606d3 IM |
128 | static inline int rt_policy(int policy) |
129 | { | |
63f01241 | 130 | if (policy == SCHED_FIFO || policy == SCHED_RR) |
e05606d3 IM |
131 | return 1; |
132 | return 0; | |
133 | } | |
134 | ||
135 | static inline int task_has_rt_policy(struct task_struct *p) | |
136 | { | |
137 | return rt_policy(p->policy); | |
138 | } | |
139 | ||
1da177e4 | 140 | /* |
6aa645ea | 141 | * This is the priority-queue data structure of the RT scheduling class: |
1da177e4 | 142 | */ |
6aa645ea IM |
143 | struct 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 | }; | |
147 | ||
d0b27fa7 | 148 | struct rt_bandwidth { |
ea736ed5 | 149 | /* nests inside the rq lock: */ |
0986b11b | 150 | raw_spinlock_t rt_runtime_lock; |
ea736ed5 IM |
151 | ktime_t rt_period; |
152 | u64 rt_runtime; | |
153 | struct hrtimer rt_period_timer; | |
d0b27fa7 PZ |
154 | }; |
155 | ||
156 | static struct rt_bandwidth def_rt_bandwidth; | |
157 | ||
158 | static int do_sched_rt_period_timer(struct rt_bandwidth *rt_b, int overrun); | |
159 | ||
160 | static 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 | ||
181 | static | |
182 | void 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 | ||
0986b11b | 187 | raw_spin_lock_init(&rt_b->rt_runtime_lock); |
ac086bc2 | 188 | |
d0b27fa7 PZ |
189 | hrtimer_init(&rt_b->rt_period_timer, |
190 | CLOCK_MONOTONIC, HRTIMER_MODE_REL); | |
191 | rt_b->rt_period_timer.function = sched_rt_period_timer; | |
d0b27fa7 PZ |
192 | } |
193 | ||
c8bfff6d KH |
194 | static inline int rt_bandwidth_enabled(void) |
195 | { | |
196 | return sysctl_sched_rt_runtime >= 0; | |
d0b27fa7 PZ |
197 | } |
198 | ||
58088ad0 | 199 | static void start_bandwidth_timer(struct hrtimer *period_timer, ktime_t period) |
d0b27fa7 | 200 | { |
58088ad0 PT |
201 | unsigned long delta; |
202 | ktime_t soft, hard, now; | |
d0b27fa7 | 203 | |
58088ad0 PT |
204 | for (;;) { |
205 | if (hrtimer_active(period_timer)) | |
206 | break; | |
207 | ||
208 | now = hrtimer_cb_get_time(period_timer); | |
209 | hrtimer_forward(period_timer, now, period); | |
210 | ||
211 | soft = hrtimer_get_softexpires(period_timer); | |
212 | hard = hrtimer_get_expires(period_timer); | |
213 | delta = ktime_to_ns(ktime_sub(hard, soft)); | |
214 | __hrtimer_start_range_ns(period_timer, soft, delta, | |
215 | HRTIMER_MODE_ABS_PINNED, 0); | |
216 | } | |
217 | } | |
218 | ||
219 | static void start_rt_bandwidth(struct rt_bandwidth *rt_b) | |
220 | { | |
cac64d00 | 221 | if (!rt_bandwidth_enabled() || rt_b->rt_runtime == RUNTIME_INF) |
d0b27fa7 PZ |
222 | return; |
223 | ||
224 | if (hrtimer_active(&rt_b->rt_period_timer)) | |
225 | return; | |
226 | ||
0986b11b | 227 | raw_spin_lock(&rt_b->rt_runtime_lock); |
58088ad0 | 228 | start_bandwidth_timer(&rt_b->rt_period_timer, rt_b->rt_period); |
0986b11b | 229 | raw_spin_unlock(&rt_b->rt_runtime_lock); |
d0b27fa7 PZ |
230 | } |
231 | ||
232 | #ifdef CONFIG_RT_GROUP_SCHED | |
233 | static void destroy_rt_bandwidth(struct rt_bandwidth *rt_b) | |
234 | { | |
235 | hrtimer_cancel(&rt_b->rt_period_timer); | |
236 | } | |
237 | #endif | |
238 | ||
712555ee | 239 | /* |
c4a8849a | 240 | * sched_domains_mutex serializes calls to init_sched_domains, |
712555ee HC |
241 | * detach_destroy_domains and partition_sched_domains. |
242 | */ | |
243 | static DEFINE_MUTEX(sched_domains_mutex); | |
244 | ||
7c941438 | 245 | #ifdef CONFIG_CGROUP_SCHED |
29f59db3 | 246 | |
68318b8e SV |
247 | #include <linux/cgroup.h> |
248 | ||
29f59db3 SV |
249 | struct cfs_rq; |
250 | ||
6f505b16 PZ |
251 | static LIST_HEAD(task_groups); |
252 | ||
ab84d31e PT |
253 | struct cfs_bandwidth { |
254 | #ifdef CONFIG_CFS_BANDWIDTH | |
255 | raw_spinlock_t lock; | |
256 | ktime_t period; | |
ec12cb7f | 257 | u64 quota, runtime; |
a790de99 | 258 | s64 hierarchal_quota; |
58088ad0 PT |
259 | |
260 | int idle, timer_active; | |
261 | struct hrtimer period_timer; | |
ab84d31e PT |
262 | #endif |
263 | }; | |
264 | ||
29f59db3 | 265 | /* task group related information */ |
4cf86d77 | 266 | struct task_group { |
68318b8e | 267 | struct cgroup_subsys_state css; |
6c415b92 | 268 | |
052f1dc7 | 269 | #ifdef CONFIG_FAIR_GROUP_SCHED |
29f59db3 SV |
270 | /* schedulable entities of this group on each cpu */ |
271 | struct sched_entity **se; | |
272 | /* runqueue "owned" by this group on each cpu */ | |
273 | struct cfs_rq **cfs_rq; | |
274 | unsigned long shares; | |
2069dd75 PZ |
275 | |
276 | atomic_t load_weight; | |
052f1dc7 PZ |
277 | #endif |
278 | ||
279 | #ifdef CONFIG_RT_GROUP_SCHED | |
280 | struct sched_rt_entity **rt_se; | |
281 | struct rt_rq **rt_rq; | |
282 | ||
d0b27fa7 | 283 | struct rt_bandwidth rt_bandwidth; |
052f1dc7 | 284 | #endif |
6b2d7700 | 285 | |
ae8393e5 | 286 | struct rcu_head rcu; |
6f505b16 | 287 | struct list_head list; |
f473aa5e PZ |
288 | |
289 | struct task_group *parent; | |
290 | struct list_head siblings; | |
291 | struct list_head children; | |
5091faa4 MG |
292 | |
293 | #ifdef CONFIG_SCHED_AUTOGROUP | |
294 | struct autogroup *autogroup; | |
295 | #endif | |
ab84d31e PT |
296 | |
297 | struct cfs_bandwidth cfs_bandwidth; | |
29f59db3 SV |
298 | }; |
299 | ||
3d4b47b4 | 300 | /* task_group_lock serializes the addition/removal of task groups */ |
8ed36996 | 301 | static DEFINE_SPINLOCK(task_group_lock); |
ec2c507f | 302 | |
e9036b36 CG |
303 | #ifdef CONFIG_FAIR_GROUP_SCHED |
304 | ||
07e06b01 | 305 | # define ROOT_TASK_GROUP_LOAD NICE_0_LOAD |
052f1dc7 | 306 | |
cb4ad1ff | 307 | /* |
2e084786 LJ |
308 | * A weight of 0 or 1 can cause arithmetics problems. |
309 | * A weight of a cfs_rq is the sum of weights of which entities | |
310 | * are queued on this cfs_rq, so a weight of a entity should not be | |
311 | * too large, so as the shares value of a task group. | |
cb4ad1ff MX |
312 | * (The default weight is 1024 - so there's no practical |
313 | * limitation from this.) | |
314 | */ | |
cd62287e MG |
315 | #define MIN_SHARES (1UL << 1) |
316 | #define MAX_SHARES (1UL << 18) | |
18d95a28 | 317 | |
07e06b01 | 318 | static int root_task_group_load = ROOT_TASK_GROUP_LOAD; |
052f1dc7 PZ |
319 | #endif |
320 | ||
29f59db3 | 321 | /* Default task group. |
3a252015 | 322 | * Every task in system belong to this group at bootup. |
29f59db3 | 323 | */ |
07e06b01 | 324 | struct task_group root_task_group; |
29f59db3 | 325 | |
7c941438 | 326 | #endif /* CONFIG_CGROUP_SCHED */ |
29f59db3 | 327 | |
6aa645ea IM |
328 | /* CFS-related fields in a runqueue */ |
329 | struct cfs_rq { | |
330 | struct load_weight load; | |
953bfcd1 | 331 | unsigned long nr_running, h_nr_running; |
6aa645ea | 332 | |
6aa645ea | 333 | u64 exec_clock; |
e9acbff6 | 334 | u64 min_vruntime; |
3fe1698b PZ |
335 | #ifndef CONFIG_64BIT |
336 | u64 min_vruntime_copy; | |
337 | #endif | |
6aa645ea IM |
338 | |
339 | struct rb_root tasks_timeline; | |
340 | struct rb_node *rb_leftmost; | |
4a55bd5e PZ |
341 | |
342 | struct list_head tasks; | |
343 | struct list_head *balance_iterator; | |
344 | ||
345 | /* | |
346 | * 'curr' points to currently running entity on this cfs_rq. | |
6aa645ea IM |
347 | * It is set to NULL otherwise (i.e when none are currently running). |
348 | */ | |
ac53db59 | 349 | struct sched_entity *curr, *next, *last, *skip; |
ddc97297 | 350 | |
4934a4d3 | 351 | #ifdef CONFIG_SCHED_DEBUG |
5ac5c4d6 | 352 | unsigned int nr_spread_over; |
4934a4d3 | 353 | #endif |
ddc97297 | 354 | |
62160e3f | 355 | #ifdef CONFIG_FAIR_GROUP_SCHED |
6aa645ea IM |
356 | struct rq *rq; /* cpu runqueue to which this cfs_rq is attached */ |
357 | ||
41a2d6cf IM |
358 | /* |
359 | * leaf cfs_rqs are those that hold tasks (lowest schedulable entity in | |
6aa645ea IM |
360 | * a hierarchy). Non-leaf lrqs hold other higher schedulable entities |
361 | * (like users, containers etc.) | |
362 | * | |
363 | * leaf_cfs_rq_list ties together list of leaf cfs_rq's in a cpu. This | |
364 | * list is used during load balance. | |
365 | */ | |
3d4b47b4 | 366 | int on_list; |
41a2d6cf IM |
367 | struct list_head leaf_cfs_rq_list; |
368 | struct task_group *tg; /* group that "owns" this runqueue */ | |
c09595f6 PZ |
369 | |
370 | #ifdef CONFIG_SMP | |
c09595f6 | 371 | /* |
c8cba857 | 372 | * the part of load.weight contributed by tasks |
c09595f6 | 373 | */ |
c8cba857 | 374 | unsigned long task_weight; |
c09595f6 | 375 | |
c8cba857 PZ |
376 | /* |
377 | * h_load = weight * f(tg) | |
378 | * | |
379 | * Where f(tg) is the recursive weight fraction assigned to | |
380 | * this group. | |
381 | */ | |
382 | unsigned long h_load; | |
c09595f6 | 383 | |
c8cba857 | 384 | /* |
3b3d190e PT |
385 | * Maintaining per-cpu shares distribution for group scheduling |
386 | * | |
387 | * load_stamp is the last time we updated the load average | |
388 | * load_last is the last time we updated the load average and saw load | |
389 | * load_unacc_exec_time is currently unaccounted execution time | |
c8cba857 | 390 | */ |
2069dd75 PZ |
391 | u64 load_avg; |
392 | u64 load_period; | |
3b3d190e | 393 | u64 load_stamp, load_last, load_unacc_exec_time; |
f1d239f7 | 394 | |
2069dd75 | 395 | unsigned long load_contribution; |
c09595f6 | 396 | #endif |
ab84d31e PT |
397 | #ifdef CONFIG_CFS_BANDWIDTH |
398 | int runtime_enabled; | |
399 | s64 runtime_remaining; | |
400 | #endif | |
6aa645ea IM |
401 | #endif |
402 | }; | |
1da177e4 | 403 | |
ab84d31e PT |
404 | #ifdef CONFIG_FAIR_GROUP_SCHED |
405 | #ifdef CONFIG_CFS_BANDWIDTH | |
406 | static inline struct cfs_bandwidth *tg_cfs_bandwidth(struct task_group *tg) | |
407 | { | |
408 | return &tg->cfs_bandwidth; | |
409 | } | |
410 | ||
411 | static inline u64 default_cfs_period(void); | |
58088ad0 PT |
412 | static int do_sched_cfs_period_timer(struct cfs_bandwidth *cfs_b, int overrun); |
413 | ||
414 | static enum hrtimer_restart sched_cfs_period_timer(struct hrtimer *timer) | |
415 | { | |
416 | struct cfs_bandwidth *cfs_b = | |
417 | container_of(timer, struct cfs_bandwidth, period_timer); | |
418 | ktime_t now; | |
419 | int overrun; | |
420 | int idle = 0; | |
421 | ||
422 | for (;;) { | |
423 | now = hrtimer_cb_get_time(timer); | |
424 | overrun = hrtimer_forward(timer, now, cfs_b->period); | |
425 | ||
426 | if (!overrun) | |
427 | break; | |
428 | ||
429 | idle = do_sched_cfs_period_timer(cfs_b, overrun); | |
430 | } | |
431 | ||
432 | return idle ? HRTIMER_NORESTART : HRTIMER_RESTART; | |
433 | } | |
ab84d31e PT |
434 | |
435 | static void init_cfs_bandwidth(struct cfs_bandwidth *cfs_b) | |
436 | { | |
437 | raw_spin_lock_init(&cfs_b->lock); | |
ec12cb7f | 438 | cfs_b->runtime = 0; |
ab84d31e PT |
439 | cfs_b->quota = RUNTIME_INF; |
440 | cfs_b->period = ns_to_ktime(default_cfs_period()); | |
58088ad0 PT |
441 | |
442 | hrtimer_init(&cfs_b->period_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL); | |
443 | cfs_b->period_timer.function = sched_cfs_period_timer; | |
ab84d31e PT |
444 | } |
445 | ||
446 | static void init_cfs_rq_runtime(struct cfs_rq *cfs_rq) | |
447 | { | |
448 | cfs_rq->runtime_enabled = 0; | |
449 | } | |
450 | ||
58088ad0 PT |
451 | /* requires cfs_b->lock, may release to reprogram timer */ |
452 | static void __start_cfs_bandwidth(struct cfs_bandwidth *cfs_b) | |
453 | { | |
454 | /* | |
455 | * The timer may be active because we're trying to set a new bandwidth | |
456 | * period or because we're racing with the tear-down path | |
457 | * (timer_active==0 becomes visible before the hrtimer call-back | |
458 | * terminates). In either case we ensure that it's re-programmed | |
459 | */ | |
460 | while (unlikely(hrtimer_active(&cfs_b->period_timer))) { | |
461 | raw_spin_unlock(&cfs_b->lock); | |
462 | /* ensure cfs_b->lock is available while we wait */ | |
463 | hrtimer_cancel(&cfs_b->period_timer); | |
464 | ||
465 | raw_spin_lock(&cfs_b->lock); | |
466 | /* if someone else restarted the timer then we're done */ | |
467 | if (cfs_b->timer_active) | |
468 | return; | |
469 | } | |
470 | ||
471 | cfs_b->timer_active = 1; | |
472 | start_bandwidth_timer(&cfs_b->period_timer, cfs_b->period); | |
473 | } | |
474 | ||
ab84d31e | 475 | static void destroy_cfs_bandwidth(struct cfs_bandwidth *cfs_b) |
58088ad0 PT |
476 | { |
477 | hrtimer_cancel(&cfs_b->period_timer); | |
478 | } | |
ab84d31e PT |
479 | #else |
480 | static void init_cfs_rq_runtime(struct cfs_rq *cfs_rq) {} | |
481 | static void init_cfs_bandwidth(struct cfs_bandwidth *cfs_b) {} | |
482 | static void destroy_cfs_bandwidth(struct cfs_bandwidth *cfs_b) {} | |
483 | ||
484 | static inline struct cfs_bandwidth *tg_cfs_bandwidth(struct task_group *tg) | |
485 | { | |
486 | return NULL; | |
487 | } | |
488 | #endif /* CONFIG_CFS_BANDWIDTH */ | |
489 | #endif /* CONFIG_FAIR_GROUP_SCHED */ | |
490 | ||
6aa645ea IM |
491 | /* Real-Time classes' related field in a runqueue: */ |
492 | struct rt_rq { | |
493 | struct rt_prio_array active; | |
63489e45 | 494 | unsigned long rt_nr_running; |
052f1dc7 | 495 | #if defined CONFIG_SMP || defined CONFIG_RT_GROUP_SCHED |
e864c499 GH |
496 | struct { |
497 | int curr; /* highest queued rt task prio */ | |
398a153b | 498 | #ifdef CONFIG_SMP |
e864c499 | 499 | int next; /* next highest */ |
398a153b | 500 | #endif |
e864c499 | 501 | } highest_prio; |
6f505b16 | 502 | #endif |
fa85ae24 | 503 | #ifdef CONFIG_SMP |
73fe6aae | 504 | unsigned long rt_nr_migratory; |
a1ba4d8b | 505 | unsigned long rt_nr_total; |
a22d7fc1 | 506 | int overloaded; |
917b627d | 507 | struct plist_head pushable_tasks; |
fa85ae24 | 508 | #endif |
6f505b16 | 509 | int rt_throttled; |
fa85ae24 | 510 | u64 rt_time; |
ac086bc2 | 511 | u64 rt_runtime; |
ea736ed5 | 512 | /* Nests inside the rq lock: */ |
0986b11b | 513 | raw_spinlock_t rt_runtime_lock; |
6f505b16 | 514 | |
052f1dc7 | 515 | #ifdef CONFIG_RT_GROUP_SCHED |
23b0fdfc PZ |
516 | unsigned long rt_nr_boosted; |
517 | ||
6f505b16 PZ |
518 | struct rq *rq; |
519 | struct list_head leaf_rt_rq_list; | |
520 | struct task_group *tg; | |
6f505b16 | 521 | #endif |
6aa645ea IM |
522 | }; |
523 | ||
57d885fe GH |
524 | #ifdef CONFIG_SMP |
525 | ||
526 | /* | |
527 | * We add the notion of a root-domain which will be used to define per-domain | |
0eab9146 IM |
528 | * variables. Each exclusive cpuset essentially defines an island domain by |
529 | * fully partitioning the member cpus from any other cpuset. Whenever a new | |
57d885fe GH |
530 | * exclusive cpuset is created, we also create and attach a new root-domain |
531 | * object. | |
532 | * | |
57d885fe GH |
533 | */ |
534 | struct root_domain { | |
535 | atomic_t refcount; | |
26a148eb | 536 | atomic_t rto_count; |
dce840a0 | 537 | struct rcu_head rcu; |
c6c4927b RR |
538 | cpumask_var_t span; |
539 | cpumask_var_t online; | |
637f5085 | 540 | |
0eab9146 | 541 | /* |
637f5085 GH |
542 | * The "RT overload" flag: it gets set if a CPU has more than |
543 | * one runnable RT task. | |
544 | */ | |
c6c4927b | 545 | cpumask_var_t rto_mask; |
6e0534f2 | 546 | struct cpupri cpupri; |
57d885fe GH |
547 | }; |
548 | ||
dc938520 GH |
549 | /* |
550 | * By default the system creates a single root-domain with all cpus as | |
551 | * members (mimicking the global state we have today). | |
552 | */ | |
57d885fe GH |
553 | static struct root_domain def_root_domain; |
554 | ||
ed2d372c | 555 | #endif /* CONFIG_SMP */ |
57d885fe | 556 | |
1da177e4 LT |
557 | /* |
558 | * This is the main, per-CPU runqueue data structure. | |
559 | * | |
560 | * Locking rule: those places that want to lock multiple runqueues | |
561 | * (such as the load balancing or the thread migration code), lock | |
562 | * acquire operations must be ordered by ascending &runqueue. | |
563 | */ | |
70b97a7f | 564 | struct rq { |
d8016491 | 565 | /* runqueue lock: */ |
05fa785c | 566 | raw_spinlock_t lock; |
1da177e4 LT |
567 | |
568 | /* | |
569 | * nr_running and cpu_load should be in the same cacheline because | |
570 | * remote CPUs use both these fields when doing load calculation. | |
571 | */ | |
572 | unsigned long nr_running; | |
6aa645ea IM |
573 | #define CPU_LOAD_IDX_MAX 5 |
574 | unsigned long cpu_load[CPU_LOAD_IDX_MAX]; | |
fdf3e95d | 575 | unsigned long last_load_update_tick; |
46cb4b7c | 576 | #ifdef CONFIG_NO_HZ |
39c0cbe2 | 577 | u64 nohz_stamp; |
83cd4fe2 | 578 | unsigned char nohz_balance_kick; |
46cb4b7c | 579 | #endif |
61eadef6 | 580 | int skip_clock_update; |
a64692a3 | 581 | |
d8016491 IM |
582 | /* capture load from *all* tasks on this cpu: */ |
583 | struct load_weight load; | |
6aa645ea IM |
584 | unsigned long nr_load_updates; |
585 | u64 nr_switches; | |
586 | ||
587 | struct cfs_rq cfs; | |
6f505b16 | 588 | struct rt_rq rt; |
6f505b16 | 589 | |
6aa645ea | 590 | #ifdef CONFIG_FAIR_GROUP_SCHED |
d8016491 IM |
591 | /* list of leaf cfs_rq on this cpu: */ |
592 | struct list_head leaf_cfs_rq_list; | |
052f1dc7 PZ |
593 | #endif |
594 | #ifdef CONFIG_RT_GROUP_SCHED | |
6f505b16 | 595 | struct list_head leaf_rt_rq_list; |
1da177e4 | 596 | #endif |
1da177e4 LT |
597 | |
598 | /* | |
599 | * This is part of a global counter where only the total sum | |
600 | * over all CPUs matters. A task can increase this counter on | |
601 | * one CPU and if it got migrated afterwards it may decrease | |
602 | * it on another CPU. Always updated under the runqueue lock: | |
603 | */ | |
604 | unsigned long nr_uninterruptible; | |
605 | ||
34f971f6 | 606 | struct task_struct *curr, *idle, *stop; |
c9819f45 | 607 | unsigned long next_balance; |
1da177e4 | 608 | struct mm_struct *prev_mm; |
6aa645ea | 609 | |
3e51f33f | 610 | u64 clock; |
305e6835 | 611 | u64 clock_task; |
6aa645ea | 612 | |
1da177e4 LT |
613 | atomic_t nr_iowait; |
614 | ||
615 | #ifdef CONFIG_SMP | |
0eab9146 | 616 | struct root_domain *rd; |
1da177e4 LT |
617 | struct sched_domain *sd; |
618 | ||
e51fd5e2 PZ |
619 | unsigned long cpu_power; |
620 | ||
a0a522ce | 621 | unsigned char idle_at_tick; |
1da177e4 | 622 | /* For active balancing */ |
3f029d3c | 623 | int post_schedule; |
1da177e4 LT |
624 | int active_balance; |
625 | int push_cpu; | |
969c7921 | 626 | struct cpu_stop_work active_balance_work; |
d8016491 IM |
627 | /* cpu of this runqueue: */ |
628 | int cpu; | |
1f11eb6a | 629 | int online; |
1da177e4 | 630 | |
e9e9250b PZ |
631 | u64 rt_avg; |
632 | u64 age_stamp; | |
1b9508f6 MG |
633 | u64 idle_stamp; |
634 | u64 avg_idle; | |
1da177e4 LT |
635 | #endif |
636 | ||
aa483808 VP |
637 | #ifdef CONFIG_IRQ_TIME_ACCOUNTING |
638 | u64 prev_irq_time; | |
639 | #endif | |
e6e6685a GC |
640 | #ifdef CONFIG_PARAVIRT |
641 | u64 prev_steal_time; | |
642 | #endif | |
095c0aa8 GC |
643 | #ifdef CONFIG_PARAVIRT_TIME_ACCOUNTING |
644 | u64 prev_steal_time_rq; | |
645 | #endif | |
aa483808 | 646 | |
dce48a84 TG |
647 | /* calc_load related fields */ |
648 | unsigned long calc_load_update; | |
649 | long calc_load_active; | |
650 | ||
8f4d37ec | 651 | #ifdef CONFIG_SCHED_HRTICK |
31656519 PZ |
652 | #ifdef CONFIG_SMP |
653 | int hrtick_csd_pending; | |
654 | struct call_single_data hrtick_csd; | |
655 | #endif | |
8f4d37ec PZ |
656 | struct hrtimer hrtick_timer; |
657 | #endif | |
658 | ||
1da177e4 LT |
659 | #ifdef CONFIG_SCHEDSTATS |
660 | /* latency stats */ | |
661 | struct sched_info rq_sched_info; | |
9c2c4802 KC |
662 | unsigned long long rq_cpu_time; |
663 | /* could above be rq->cfs_rq.exec_clock + rq->rt_rq.rt_runtime ? */ | |
1da177e4 LT |
664 | |
665 | /* sys_sched_yield() stats */ | |
480b9434 | 666 | unsigned int yld_count; |
1da177e4 LT |
667 | |
668 | /* schedule() stats */ | |
480b9434 KC |
669 | unsigned int sched_switch; |
670 | unsigned int sched_count; | |
671 | unsigned int sched_goidle; | |
1da177e4 LT |
672 | |
673 | /* try_to_wake_up() stats */ | |
480b9434 KC |
674 | unsigned int ttwu_count; |
675 | unsigned int ttwu_local; | |
1da177e4 | 676 | #endif |
317f3941 PZ |
677 | |
678 | #ifdef CONFIG_SMP | |
679 | struct task_struct *wake_list; | |
680 | #endif | |
1da177e4 LT |
681 | }; |
682 | ||
f34e3b61 | 683 | static DEFINE_PER_CPU_SHARED_ALIGNED(struct rq, runqueues); |
1da177e4 | 684 | |
a64692a3 | 685 | |
1e5a7405 | 686 | static void check_preempt_curr(struct rq *rq, struct task_struct *p, int flags); |
dd41f596 | 687 | |
0a2966b4 CL |
688 | static inline int cpu_of(struct rq *rq) |
689 | { | |
690 | #ifdef CONFIG_SMP | |
691 | return rq->cpu; | |
692 | #else | |
693 | return 0; | |
694 | #endif | |
695 | } | |
696 | ||
497f0ab3 | 697 | #define rcu_dereference_check_sched_domain(p) \ |
d11c563d | 698 | rcu_dereference_check((p), \ |
d11c563d PM |
699 | lockdep_is_held(&sched_domains_mutex)) |
700 | ||
674311d5 NP |
701 | /* |
702 | * The domain tree (rq->sd) is protected by RCU's quiescent state transition. | |
1a20ff27 | 703 | * See detach_destroy_domains: synchronize_sched for details. |
674311d5 NP |
704 | * |
705 | * The domain tree of any CPU may only be accessed from within | |
706 | * preempt-disabled sections. | |
707 | */ | |
48f24c4d | 708 | #define for_each_domain(cpu, __sd) \ |
497f0ab3 | 709 | for (__sd = rcu_dereference_check_sched_domain(cpu_rq(cpu)->sd); __sd; __sd = __sd->parent) |
1da177e4 LT |
710 | |
711 | #define cpu_rq(cpu) (&per_cpu(runqueues, (cpu))) | |
712 | #define this_rq() (&__get_cpu_var(runqueues)) | |
713 | #define task_rq(p) cpu_rq(task_cpu(p)) | |
714 | #define cpu_curr(cpu) (cpu_rq(cpu)->curr) | |
54d35f29 | 715 | #define raw_rq() (&__raw_get_cpu_var(runqueues)) |
1da177e4 | 716 | |
dc61b1d6 PZ |
717 | #ifdef CONFIG_CGROUP_SCHED |
718 | ||
719 | /* | |
720 | * Return the group to which this tasks belongs. | |
721 | * | |
6c6c54e1 PZ |
722 | * We use task_subsys_state_check() and extend the RCU verification with |
723 | * pi->lock and rq->lock because cpu_cgroup_attach() holds those locks for each | |
724 | * task it moves into the cgroup. Therefore by holding either of those locks, | |
725 | * we pin the task to the current cgroup. | |
dc61b1d6 PZ |
726 | */ |
727 | static inline struct task_group *task_group(struct task_struct *p) | |
728 | { | |
5091faa4 | 729 | struct task_group *tg; |
dc61b1d6 PZ |
730 | struct cgroup_subsys_state *css; |
731 | ||
732 | css = task_subsys_state_check(p, cpu_cgroup_subsys_id, | |
6c6c54e1 PZ |
733 | lockdep_is_held(&p->pi_lock) || |
734 | lockdep_is_held(&task_rq(p)->lock)); | |
5091faa4 MG |
735 | tg = container_of(css, struct task_group, css); |
736 | ||
737 | return autogroup_task_group(p, tg); | |
dc61b1d6 PZ |
738 | } |
739 | ||
740 | /* Change a task's cfs_rq and parent entity if it moves across CPUs/groups */ | |
741 | static inline void set_task_rq(struct task_struct *p, unsigned int cpu) | |
742 | { | |
743 | #ifdef CONFIG_FAIR_GROUP_SCHED | |
744 | p->se.cfs_rq = task_group(p)->cfs_rq[cpu]; | |
745 | p->se.parent = task_group(p)->se[cpu]; | |
746 | #endif | |
747 | ||
748 | #ifdef CONFIG_RT_GROUP_SCHED | |
749 | p->rt.rt_rq = task_group(p)->rt_rq[cpu]; | |
750 | p->rt.parent = task_group(p)->rt_se[cpu]; | |
751 | #endif | |
752 | } | |
753 | ||
754 | #else /* CONFIG_CGROUP_SCHED */ | |
755 | ||
756 | static inline void set_task_rq(struct task_struct *p, unsigned int cpu) { } | |
757 | static inline struct task_group *task_group(struct task_struct *p) | |
758 | { | |
759 | return NULL; | |
760 | } | |
761 | ||
762 | #endif /* CONFIG_CGROUP_SCHED */ | |
763 | ||
fe44d621 | 764 | static void update_rq_clock_task(struct rq *rq, s64 delta); |
305e6835 | 765 | |
fe44d621 | 766 | static void update_rq_clock(struct rq *rq) |
3e51f33f | 767 | { |
fe44d621 | 768 | s64 delta; |
305e6835 | 769 | |
61eadef6 | 770 | if (rq->skip_clock_update > 0) |
f26f9aff | 771 | return; |
aa483808 | 772 | |
fe44d621 PZ |
773 | delta = sched_clock_cpu(cpu_of(rq)) - rq->clock; |
774 | rq->clock += delta; | |
775 | update_rq_clock_task(rq, delta); | |
3e51f33f PZ |
776 | } |
777 | ||
bf5c91ba IM |
778 | /* |
779 | * Tunables that become constants when CONFIG_SCHED_DEBUG is off: | |
780 | */ | |
781 | #ifdef CONFIG_SCHED_DEBUG | |
782 | # define const_debug __read_mostly | |
783 | #else | |
784 | # define const_debug static const | |
785 | #endif | |
786 | ||
017730c1 | 787 | /** |
1fd06bb1 | 788 | * runqueue_is_locked - Returns true if the current cpu runqueue is locked |
e17b38bf | 789 | * @cpu: the processor in question. |
017730c1 | 790 | * |
017730c1 IM |
791 | * This interface allows printk to be called with the runqueue lock |
792 | * held and know whether or not it is OK to wake up the klogd. | |
793 | */ | |
89f19f04 | 794 | int runqueue_is_locked(int cpu) |
017730c1 | 795 | { |
05fa785c | 796 | return raw_spin_is_locked(&cpu_rq(cpu)->lock); |
017730c1 IM |
797 | } |
798 | ||
bf5c91ba IM |
799 | /* |
800 | * Debugging: various feature bits | |
801 | */ | |
f00b45c1 PZ |
802 | |
803 | #define SCHED_FEAT(name, enabled) \ | |
804 | __SCHED_FEAT_##name , | |
805 | ||
bf5c91ba | 806 | enum { |
f00b45c1 | 807 | #include "sched_features.h" |
bf5c91ba IM |
808 | }; |
809 | ||
f00b45c1 PZ |
810 | #undef SCHED_FEAT |
811 | ||
812 | #define SCHED_FEAT(name, enabled) \ | |
813 | (1UL << __SCHED_FEAT_##name) * enabled | | |
814 | ||
bf5c91ba | 815 | const_debug unsigned int sysctl_sched_features = |
f00b45c1 PZ |
816 | #include "sched_features.h" |
817 | 0; | |
818 | ||
819 | #undef SCHED_FEAT | |
820 | ||
821 | #ifdef CONFIG_SCHED_DEBUG | |
822 | #define SCHED_FEAT(name, enabled) \ | |
823 | #name , | |
824 | ||
983ed7a6 | 825 | static __read_mostly char *sched_feat_names[] = { |
f00b45c1 PZ |
826 | #include "sched_features.h" |
827 | NULL | |
828 | }; | |
829 | ||
830 | #undef SCHED_FEAT | |
831 | ||
34f3a814 | 832 | static int sched_feat_show(struct seq_file *m, void *v) |
f00b45c1 | 833 | { |
f00b45c1 PZ |
834 | int i; |
835 | ||
836 | for (i = 0; sched_feat_names[i]; i++) { | |
34f3a814 LZ |
837 | if (!(sysctl_sched_features & (1UL << i))) |
838 | seq_puts(m, "NO_"); | |
839 | seq_printf(m, "%s ", sched_feat_names[i]); | |
f00b45c1 | 840 | } |
34f3a814 | 841 | seq_puts(m, "\n"); |
f00b45c1 | 842 | |
34f3a814 | 843 | return 0; |
f00b45c1 PZ |
844 | } |
845 | ||
846 | static ssize_t | |
847 | sched_feat_write(struct file *filp, const char __user *ubuf, | |
848 | size_t cnt, loff_t *ppos) | |
849 | { | |
850 | char buf[64]; | |
7740191c | 851 | char *cmp; |
f00b45c1 PZ |
852 | int neg = 0; |
853 | int i; | |
854 | ||
855 | if (cnt > 63) | |
856 | cnt = 63; | |
857 | ||
858 | if (copy_from_user(&buf, ubuf, cnt)) | |
859 | return -EFAULT; | |
860 | ||
861 | buf[cnt] = 0; | |
7740191c | 862 | cmp = strstrip(buf); |
f00b45c1 | 863 | |
524429c3 | 864 | if (strncmp(cmp, "NO_", 3) == 0) { |
f00b45c1 PZ |
865 | neg = 1; |
866 | cmp += 3; | |
867 | } | |
868 | ||
869 | for (i = 0; sched_feat_names[i]; i++) { | |
7740191c | 870 | if (strcmp(cmp, sched_feat_names[i]) == 0) { |
f00b45c1 PZ |
871 | if (neg) |
872 | sysctl_sched_features &= ~(1UL << i); | |
873 | else | |
874 | sysctl_sched_features |= (1UL << i); | |
875 | break; | |
876 | } | |
877 | } | |
878 | ||
879 | if (!sched_feat_names[i]) | |
880 | return -EINVAL; | |
881 | ||
42994724 | 882 | *ppos += cnt; |
f00b45c1 PZ |
883 | |
884 | return cnt; | |
885 | } | |
886 | ||
34f3a814 LZ |
887 | static int sched_feat_open(struct inode *inode, struct file *filp) |
888 | { | |
889 | return single_open(filp, sched_feat_show, NULL); | |
890 | } | |
891 | ||
828c0950 | 892 | static const struct file_operations sched_feat_fops = { |
34f3a814 LZ |
893 | .open = sched_feat_open, |
894 | .write = sched_feat_write, | |
895 | .read = seq_read, | |
896 | .llseek = seq_lseek, | |
897 | .release = single_release, | |
f00b45c1 PZ |
898 | }; |
899 | ||
900 | static __init int sched_init_debug(void) | |
901 | { | |
f00b45c1 PZ |
902 | debugfs_create_file("sched_features", 0644, NULL, NULL, |
903 | &sched_feat_fops); | |
904 | ||
905 | return 0; | |
906 | } | |
907 | late_initcall(sched_init_debug); | |
908 | ||
909 | #endif | |
910 | ||
911 | #define sched_feat(x) (sysctl_sched_features & (1UL << __SCHED_FEAT_##x)) | |
bf5c91ba | 912 | |
b82d9fdd PZ |
913 | /* |
914 | * Number of tasks to iterate in a single balance run. | |
915 | * Limited because this is done with IRQs disabled. | |
916 | */ | |
917 | const_debug unsigned int sysctl_sched_nr_migrate = 32; | |
918 | ||
e9e9250b PZ |
919 | /* |
920 | * period over which we average the RT time consumption, measured | |
921 | * in ms. | |
922 | * | |
923 | * default: 1s | |
924 | */ | |
925 | const_debug unsigned int sysctl_sched_time_avg = MSEC_PER_SEC; | |
926 | ||
fa85ae24 | 927 | /* |
9f0c1e56 | 928 | * period over which we measure -rt task cpu usage in us. |
fa85ae24 PZ |
929 | * default: 1s |
930 | */ | |
9f0c1e56 | 931 | unsigned int sysctl_sched_rt_period = 1000000; |
fa85ae24 | 932 | |
6892b75e IM |
933 | static __read_mostly int scheduler_running; |
934 | ||
9f0c1e56 PZ |
935 | /* |
936 | * part of the period that we allow rt tasks to run in us. | |
937 | * default: 0.95s | |
938 | */ | |
939 | int sysctl_sched_rt_runtime = 950000; | |
fa85ae24 | 940 | |
d0b27fa7 PZ |
941 | static inline u64 global_rt_period(void) |
942 | { | |
943 | return (u64)sysctl_sched_rt_period * NSEC_PER_USEC; | |
944 | } | |
945 | ||
946 | static inline u64 global_rt_runtime(void) | |
947 | { | |
e26873bb | 948 | if (sysctl_sched_rt_runtime < 0) |
d0b27fa7 PZ |
949 | return RUNTIME_INF; |
950 | ||
951 | return (u64)sysctl_sched_rt_runtime * NSEC_PER_USEC; | |
952 | } | |
fa85ae24 | 953 | |
1da177e4 | 954 | #ifndef prepare_arch_switch |
4866cde0 NP |
955 | # define prepare_arch_switch(next) do { } while (0) |
956 | #endif | |
957 | #ifndef finish_arch_switch | |
958 | # define finish_arch_switch(prev) do { } while (0) | |
959 | #endif | |
960 | ||
051a1d1a DA |
961 | static inline int task_current(struct rq *rq, struct task_struct *p) |
962 | { | |
963 | return rq->curr == p; | |
964 | } | |
965 | ||
70b97a7f | 966 | static inline int task_running(struct rq *rq, struct task_struct *p) |
4866cde0 | 967 | { |
3ca7a440 PZ |
968 | #ifdef CONFIG_SMP |
969 | return p->on_cpu; | |
970 | #else | |
051a1d1a | 971 | return task_current(rq, p); |
3ca7a440 | 972 | #endif |
4866cde0 NP |
973 | } |
974 | ||
3ca7a440 | 975 | #ifndef __ARCH_WANT_UNLOCKED_CTXSW |
70b97a7f | 976 | static inline void prepare_lock_switch(struct rq *rq, struct task_struct *next) |
4866cde0 | 977 | { |
3ca7a440 PZ |
978 | #ifdef CONFIG_SMP |
979 | /* | |
980 | * We can optimise this out completely for !SMP, because the | |
981 | * SMP rebalancing from interrupt is the only thing that cares | |
982 | * here. | |
983 | */ | |
984 | next->on_cpu = 1; | |
985 | #endif | |
4866cde0 NP |
986 | } |
987 | ||
70b97a7f | 988 | static inline void finish_lock_switch(struct rq *rq, struct task_struct *prev) |
4866cde0 | 989 | { |
3ca7a440 PZ |
990 | #ifdef CONFIG_SMP |
991 | /* | |
992 | * After ->on_cpu is cleared, the task can be moved to a different CPU. | |
993 | * We must ensure this doesn't happen until the switch is completely | |
994 | * finished. | |
995 | */ | |
996 | smp_wmb(); | |
997 | prev->on_cpu = 0; | |
998 | #endif | |
da04c035 IM |
999 | #ifdef CONFIG_DEBUG_SPINLOCK |
1000 | /* this is a valid case when another task releases the spinlock */ | |
1001 | rq->lock.owner = current; | |
1002 | #endif | |
8a25d5de IM |
1003 | /* |
1004 | * If we are tracking spinlock dependencies then we have to | |
1005 | * fix up the runqueue lock - which gets 'carried over' from | |
1006 | * prev into current: | |
1007 | */ | |
1008 | spin_acquire(&rq->lock.dep_map, 0, 0, _THIS_IP_); | |
1009 | ||
05fa785c | 1010 | raw_spin_unlock_irq(&rq->lock); |
4866cde0 NP |
1011 | } |
1012 | ||
1013 | #else /* __ARCH_WANT_UNLOCKED_CTXSW */ | |
70b97a7f | 1014 | static inline void prepare_lock_switch(struct rq *rq, struct task_struct *next) |
4866cde0 NP |
1015 | { |
1016 | #ifdef CONFIG_SMP | |
1017 | /* | |
1018 | * We can optimise this out completely for !SMP, because the | |
1019 | * SMP rebalancing from interrupt is the only thing that cares | |
1020 | * here. | |
1021 | */ | |
3ca7a440 | 1022 | next->on_cpu = 1; |
4866cde0 NP |
1023 | #endif |
1024 | #ifdef __ARCH_WANT_INTERRUPTS_ON_CTXSW | |
05fa785c | 1025 | raw_spin_unlock_irq(&rq->lock); |
4866cde0 | 1026 | #else |
05fa785c | 1027 | raw_spin_unlock(&rq->lock); |
4866cde0 NP |
1028 | #endif |
1029 | } | |
1030 | ||
70b97a7f | 1031 | static inline void finish_lock_switch(struct rq *rq, struct task_struct *prev) |
4866cde0 NP |
1032 | { |
1033 | #ifdef CONFIG_SMP | |
1034 | /* | |
3ca7a440 | 1035 | * After ->on_cpu is cleared, the task can be moved to a different CPU. |
4866cde0 NP |
1036 | * We must ensure this doesn't happen until the switch is completely |
1037 | * finished. | |
1038 | */ | |
1039 | smp_wmb(); | |
3ca7a440 | 1040 | prev->on_cpu = 0; |
4866cde0 NP |
1041 | #endif |
1042 | #ifndef __ARCH_WANT_INTERRUPTS_ON_CTXSW | |
1043 | local_irq_enable(); | |
1da177e4 | 1044 | #endif |
4866cde0 NP |
1045 | } |
1046 | #endif /* __ARCH_WANT_UNLOCKED_CTXSW */ | |
1da177e4 | 1047 | |
0970d299 | 1048 | /* |
0122ec5b | 1049 | * __task_rq_lock - lock the rq @p resides on. |
b29739f9 | 1050 | */ |
70b97a7f | 1051 | static inline struct rq *__task_rq_lock(struct task_struct *p) |
b29739f9 IM |
1052 | __acquires(rq->lock) |
1053 | { | |
0970d299 PZ |
1054 | struct rq *rq; |
1055 | ||
0122ec5b PZ |
1056 | lockdep_assert_held(&p->pi_lock); |
1057 | ||
3a5c359a | 1058 | for (;;) { |
0970d299 | 1059 | rq = task_rq(p); |
05fa785c | 1060 | raw_spin_lock(&rq->lock); |
65cc8e48 | 1061 | if (likely(rq == task_rq(p))) |
3a5c359a | 1062 | return rq; |
05fa785c | 1063 | raw_spin_unlock(&rq->lock); |
b29739f9 | 1064 | } |
b29739f9 IM |
1065 | } |
1066 | ||
1da177e4 | 1067 | /* |
0122ec5b | 1068 | * task_rq_lock - lock p->pi_lock and lock the rq @p resides on. |
1da177e4 | 1069 | */ |
70b97a7f | 1070 | static struct rq *task_rq_lock(struct task_struct *p, unsigned long *flags) |
0122ec5b | 1071 | __acquires(p->pi_lock) |
1da177e4 LT |
1072 | __acquires(rq->lock) |
1073 | { | |
70b97a7f | 1074 | struct rq *rq; |
1da177e4 | 1075 | |
3a5c359a | 1076 | for (;;) { |
0122ec5b | 1077 | raw_spin_lock_irqsave(&p->pi_lock, *flags); |
3a5c359a | 1078 | rq = task_rq(p); |
05fa785c | 1079 | raw_spin_lock(&rq->lock); |
65cc8e48 | 1080 | if (likely(rq == task_rq(p))) |
3a5c359a | 1081 | return rq; |
0122ec5b PZ |
1082 | raw_spin_unlock(&rq->lock); |
1083 | raw_spin_unlock_irqrestore(&p->pi_lock, *flags); | |
1da177e4 | 1084 | } |
1da177e4 LT |
1085 | } |
1086 | ||
a9957449 | 1087 | static void __task_rq_unlock(struct rq *rq) |
b29739f9 IM |
1088 | __releases(rq->lock) |
1089 | { | |
05fa785c | 1090 | raw_spin_unlock(&rq->lock); |
b29739f9 IM |
1091 | } |
1092 | ||
0122ec5b PZ |
1093 | static inline void |
1094 | task_rq_unlock(struct rq *rq, struct task_struct *p, unsigned long *flags) | |
1da177e4 | 1095 | __releases(rq->lock) |
0122ec5b | 1096 | __releases(p->pi_lock) |
1da177e4 | 1097 | { |
0122ec5b PZ |
1098 | raw_spin_unlock(&rq->lock); |
1099 | raw_spin_unlock_irqrestore(&p->pi_lock, *flags); | |
1da177e4 LT |
1100 | } |
1101 | ||
1da177e4 | 1102 | /* |
cc2a73b5 | 1103 | * this_rq_lock - lock this runqueue and disable interrupts. |
1da177e4 | 1104 | */ |
a9957449 | 1105 | static struct rq *this_rq_lock(void) |
1da177e4 LT |
1106 | __acquires(rq->lock) |
1107 | { | |
70b97a7f | 1108 | struct rq *rq; |
1da177e4 LT |
1109 | |
1110 | local_irq_disable(); | |
1111 | rq = this_rq(); | |
05fa785c | 1112 | raw_spin_lock(&rq->lock); |
1da177e4 LT |
1113 | |
1114 | return rq; | |
1115 | } | |
1116 | ||
8f4d37ec PZ |
1117 | #ifdef CONFIG_SCHED_HRTICK |
1118 | /* | |
1119 | * Use HR-timers to deliver accurate preemption points. | |
1120 | * | |
1121 | * Its all a bit involved since we cannot program an hrt while holding the | |
1122 | * rq->lock. So what we do is store a state in in rq->hrtick_* and ask for a | |
1123 | * reschedule event. | |
1124 | * | |
1125 | * When we get rescheduled we reprogram the hrtick_timer outside of the | |
1126 | * rq->lock. | |
1127 | */ | |
8f4d37ec PZ |
1128 | |
1129 | /* | |
1130 | * Use hrtick when: | |
1131 | * - enabled by features | |
1132 | * - hrtimer is actually high res | |
1133 | */ | |
1134 | static inline int hrtick_enabled(struct rq *rq) | |
1135 | { | |
1136 | if (!sched_feat(HRTICK)) | |
1137 | return 0; | |
ba42059f | 1138 | if (!cpu_active(cpu_of(rq))) |
b328ca18 | 1139 | return 0; |
8f4d37ec PZ |
1140 | return hrtimer_is_hres_active(&rq->hrtick_timer); |
1141 | } | |
1142 | ||
8f4d37ec PZ |
1143 | static void hrtick_clear(struct rq *rq) |
1144 | { | |
1145 | if (hrtimer_active(&rq->hrtick_timer)) | |
1146 | hrtimer_cancel(&rq->hrtick_timer); | |
1147 | } | |
1148 | ||
8f4d37ec PZ |
1149 | /* |
1150 | * High-resolution timer tick. | |
1151 | * Runs from hardirq context with interrupts disabled. | |
1152 | */ | |
1153 | static enum hrtimer_restart hrtick(struct hrtimer *timer) | |
1154 | { | |
1155 | struct rq *rq = container_of(timer, struct rq, hrtick_timer); | |
1156 | ||
1157 | WARN_ON_ONCE(cpu_of(rq) != smp_processor_id()); | |
1158 | ||
05fa785c | 1159 | raw_spin_lock(&rq->lock); |
3e51f33f | 1160 | update_rq_clock(rq); |
8f4d37ec | 1161 | rq->curr->sched_class->task_tick(rq, rq->curr, 1); |
05fa785c | 1162 | raw_spin_unlock(&rq->lock); |
8f4d37ec PZ |
1163 | |
1164 | return HRTIMER_NORESTART; | |
1165 | } | |
1166 | ||
95e904c7 | 1167 | #ifdef CONFIG_SMP |
31656519 PZ |
1168 | /* |
1169 | * called from hardirq (IPI) context | |
1170 | */ | |
1171 | static void __hrtick_start(void *arg) | |
b328ca18 | 1172 | { |
31656519 | 1173 | struct rq *rq = arg; |
b328ca18 | 1174 | |
05fa785c | 1175 | raw_spin_lock(&rq->lock); |
31656519 PZ |
1176 | hrtimer_restart(&rq->hrtick_timer); |
1177 | rq->hrtick_csd_pending = 0; | |
05fa785c | 1178 | raw_spin_unlock(&rq->lock); |
b328ca18 PZ |
1179 | } |
1180 | ||
31656519 PZ |
1181 | /* |
1182 | * Called to set the hrtick timer state. | |
1183 | * | |
1184 | * called with rq->lock held and irqs disabled | |
1185 | */ | |
1186 | static void hrtick_start(struct rq *rq, u64 delay) | |
b328ca18 | 1187 | { |
31656519 PZ |
1188 | struct hrtimer *timer = &rq->hrtick_timer; |
1189 | ktime_t time = ktime_add_ns(timer->base->get_time(), delay); | |
b328ca18 | 1190 | |
cc584b21 | 1191 | hrtimer_set_expires(timer, time); |
31656519 PZ |
1192 | |
1193 | if (rq == this_rq()) { | |
1194 | hrtimer_restart(timer); | |
1195 | } else if (!rq->hrtick_csd_pending) { | |
6e275637 | 1196 | __smp_call_function_single(cpu_of(rq), &rq->hrtick_csd, 0); |
31656519 PZ |
1197 | rq->hrtick_csd_pending = 1; |
1198 | } | |
b328ca18 PZ |
1199 | } |
1200 | ||
1201 | static int | |
1202 | hotplug_hrtick(struct notifier_block *nfb, unsigned long action, void *hcpu) | |
1203 | { | |
1204 | int cpu = (int)(long)hcpu; | |
1205 | ||
1206 | switch (action) { | |
1207 | case CPU_UP_CANCELED: | |
1208 | case CPU_UP_CANCELED_FROZEN: | |
1209 | case CPU_DOWN_PREPARE: | |
1210 | case CPU_DOWN_PREPARE_FROZEN: | |
1211 | case CPU_DEAD: | |
1212 | case CPU_DEAD_FROZEN: | |
31656519 | 1213 | hrtick_clear(cpu_rq(cpu)); |
b328ca18 PZ |
1214 | return NOTIFY_OK; |
1215 | } | |
1216 | ||
1217 | return NOTIFY_DONE; | |
1218 | } | |
1219 | ||
fa748203 | 1220 | static __init void init_hrtick(void) |
b328ca18 PZ |
1221 | { |
1222 | hotcpu_notifier(hotplug_hrtick, 0); | |
1223 | } | |
31656519 PZ |
1224 | #else |
1225 | /* | |
1226 | * Called to set the hrtick timer state. | |
1227 | * | |
1228 | * called with rq->lock held and irqs disabled | |
1229 | */ | |
1230 | static void hrtick_start(struct rq *rq, u64 delay) | |
1231 | { | |
7f1e2ca9 | 1232 | __hrtimer_start_range_ns(&rq->hrtick_timer, ns_to_ktime(delay), 0, |
5c333864 | 1233 | HRTIMER_MODE_REL_PINNED, 0); |
31656519 | 1234 | } |
b328ca18 | 1235 | |
006c75f1 | 1236 | static inline void init_hrtick(void) |
8f4d37ec | 1237 | { |
8f4d37ec | 1238 | } |
31656519 | 1239 | #endif /* CONFIG_SMP */ |
8f4d37ec | 1240 | |
31656519 | 1241 | static void init_rq_hrtick(struct rq *rq) |
8f4d37ec | 1242 | { |
31656519 PZ |
1243 | #ifdef CONFIG_SMP |
1244 | rq->hrtick_csd_pending = 0; | |
8f4d37ec | 1245 | |
31656519 PZ |
1246 | rq->hrtick_csd.flags = 0; |
1247 | rq->hrtick_csd.func = __hrtick_start; | |
1248 | rq->hrtick_csd.info = rq; | |
1249 | #endif | |
8f4d37ec | 1250 | |
31656519 PZ |
1251 | hrtimer_init(&rq->hrtick_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL); |
1252 | rq->hrtick_timer.function = hrtick; | |
8f4d37ec | 1253 | } |
006c75f1 | 1254 | #else /* CONFIG_SCHED_HRTICK */ |
8f4d37ec PZ |
1255 | static inline void hrtick_clear(struct rq *rq) |
1256 | { | |
1257 | } | |
1258 | ||
8f4d37ec PZ |
1259 | static inline void init_rq_hrtick(struct rq *rq) |
1260 | { | |
1261 | } | |
1262 | ||
b328ca18 PZ |
1263 | static inline void init_hrtick(void) |
1264 | { | |
1265 | } | |
006c75f1 | 1266 | #endif /* CONFIG_SCHED_HRTICK */ |
8f4d37ec | 1267 | |
c24d20db IM |
1268 | /* |
1269 | * resched_task - mark a task 'to be rescheduled now'. | |
1270 | * | |
1271 | * On UP this means the setting of the need_resched flag, on SMP it | |
1272 | * might also involve a cross-CPU call to trigger the scheduler on | |
1273 | * the target CPU. | |
1274 | */ | |
1275 | #ifdef CONFIG_SMP | |
1276 | ||
1277 | #ifndef tsk_is_polling | |
1278 | #define tsk_is_polling(t) test_tsk_thread_flag(t, TIF_POLLING_NRFLAG) | |
1279 | #endif | |
1280 | ||
31656519 | 1281 | static void resched_task(struct task_struct *p) |
c24d20db IM |
1282 | { |
1283 | int cpu; | |
1284 | ||
05fa785c | 1285 | assert_raw_spin_locked(&task_rq(p)->lock); |
c24d20db | 1286 | |
5ed0cec0 | 1287 | if (test_tsk_need_resched(p)) |
c24d20db IM |
1288 | return; |
1289 | ||
5ed0cec0 | 1290 | set_tsk_need_resched(p); |
c24d20db IM |
1291 | |
1292 | cpu = task_cpu(p); | |
1293 | if (cpu == smp_processor_id()) | |
1294 | return; | |
1295 | ||
1296 | /* NEED_RESCHED must be visible before we test polling */ | |
1297 | smp_mb(); | |
1298 | if (!tsk_is_polling(p)) | |
1299 | smp_send_reschedule(cpu); | |
1300 | } | |
1301 | ||
1302 | static void resched_cpu(int cpu) | |
1303 | { | |
1304 | struct rq *rq = cpu_rq(cpu); | |
1305 | unsigned long flags; | |
1306 | ||
05fa785c | 1307 | if (!raw_spin_trylock_irqsave(&rq->lock, flags)) |
c24d20db IM |
1308 | return; |
1309 | resched_task(cpu_curr(cpu)); | |
05fa785c | 1310 | raw_spin_unlock_irqrestore(&rq->lock, flags); |
c24d20db | 1311 | } |
06d8308c TG |
1312 | |
1313 | #ifdef CONFIG_NO_HZ | |
83cd4fe2 VP |
1314 | /* |
1315 | * In the semi idle case, use the nearest busy cpu for migrating timers | |
1316 | * from an idle cpu. This is good for power-savings. | |
1317 | * | |
1318 | * We don't do similar optimization for completely idle system, as | |
1319 | * selecting an idle cpu will add more delays to the timers than intended | |
1320 | * (as that cpu's timer base may not be uptodate wrt jiffies etc). | |
1321 | */ | |
1322 | int get_nohz_timer_target(void) | |
1323 | { | |
1324 | int cpu = smp_processor_id(); | |
1325 | int i; | |
1326 | struct sched_domain *sd; | |
1327 | ||
057f3fad | 1328 | rcu_read_lock(); |
83cd4fe2 | 1329 | for_each_domain(cpu, sd) { |
057f3fad PZ |
1330 | for_each_cpu(i, sched_domain_span(sd)) { |
1331 | if (!idle_cpu(i)) { | |
1332 | cpu = i; | |
1333 | goto unlock; | |
1334 | } | |
1335 | } | |
83cd4fe2 | 1336 | } |
057f3fad PZ |
1337 | unlock: |
1338 | rcu_read_unlock(); | |
83cd4fe2 VP |
1339 | return cpu; |
1340 | } | |
06d8308c TG |
1341 | /* |
1342 | * When add_timer_on() enqueues a timer into the timer wheel of an | |
1343 | * idle CPU then this timer might expire before the next timer event | |
1344 | * which is scheduled to wake up that CPU. In case of a completely | |
1345 | * idle system the next event might even be infinite time into the | |
1346 | * future. wake_up_idle_cpu() ensures that the CPU is woken up and | |
1347 | * leaves the inner idle loop so the newly added timer is taken into | |
1348 | * account when the CPU goes back to idle and evaluates the timer | |
1349 | * wheel for the next timer event. | |
1350 | */ | |
1351 | void wake_up_idle_cpu(int cpu) | |
1352 | { | |
1353 | struct rq *rq = cpu_rq(cpu); | |
1354 | ||
1355 | if (cpu == smp_processor_id()) | |
1356 | return; | |
1357 | ||
1358 | /* | |
1359 | * This is safe, as this function is called with the timer | |
1360 | * wheel base lock of (cpu) held. When the CPU is on the way | |
1361 | * to idle and has not yet set rq->curr to idle then it will | |
1362 | * be serialized on the timer wheel base lock and take the new | |
1363 | * timer into account automatically. | |
1364 | */ | |
1365 | if (rq->curr != rq->idle) | |
1366 | return; | |
1367 | ||
1368 | /* | |
1369 | * We can set TIF_RESCHED on the idle task of the other CPU | |
1370 | * lockless. The worst case is that the other CPU runs the | |
1371 | * idle task through an additional NOOP schedule() | |
1372 | */ | |
5ed0cec0 | 1373 | set_tsk_need_resched(rq->idle); |
06d8308c TG |
1374 | |
1375 | /* NEED_RESCHED must be visible before we test polling */ | |
1376 | smp_mb(); | |
1377 | if (!tsk_is_polling(rq->idle)) | |
1378 | smp_send_reschedule(cpu); | |
1379 | } | |
39c0cbe2 | 1380 | |
6d6bc0ad | 1381 | #endif /* CONFIG_NO_HZ */ |
06d8308c | 1382 | |
e9e9250b PZ |
1383 | static u64 sched_avg_period(void) |
1384 | { | |
1385 | return (u64)sysctl_sched_time_avg * NSEC_PER_MSEC / 2; | |
1386 | } | |
1387 | ||
1388 | static void sched_avg_update(struct rq *rq) | |
1389 | { | |
1390 | s64 period = sched_avg_period(); | |
1391 | ||
1392 | while ((s64)(rq->clock - rq->age_stamp) > period) { | |
0d98bb26 WD |
1393 | /* |
1394 | * Inline assembly required to prevent the compiler | |
1395 | * optimising this loop into a divmod call. | |
1396 | * See __iter_div_u64_rem() for another example of this. | |
1397 | */ | |
1398 | asm("" : "+rm" (rq->age_stamp)); | |
e9e9250b PZ |
1399 | rq->age_stamp += period; |
1400 | rq->rt_avg /= 2; | |
1401 | } | |
1402 | } | |
1403 | ||
1404 | static void sched_rt_avg_update(struct rq *rq, u64 rt_delta) | |
1405 | { | |
1406 | rq->rt_avg += rt_delta; | |
1407 | sched_avg_update(rq); | |
1408 | } | |
1409 | ||
6d6bc0ad | 1410 | #else /* !CONFIG_SMP */ |
31656519 | 1411 | static void resched_task(struct task_struct *p) |
c24d20db | 1412 | { |
05fa785c | 1413 | assert_raw_spin_locked(&task_rq(p)->lock); |
31656519 | 1414 | set_tsk_need_resched(p); |
c24d20db | 1415 | } |
e9e9250b PZ |
1416 | |
1417 | static void sched_rt_avg_update(struct rq *rq, u64 rt_delta) | |
1418 | { | |
1419 | } | |
da2b71ed SS |
1420 | |
1421 | static void sched_avg_update(struct rq *rq) | |
1422 | { | |
1423 | } | |
6d6bc0ad | 1424 | #endif /* CONFIG_SMP */ |
c24d20db | 1425 | |
45bf76df IM |
1426 | #if BITS_PER_LONG == 32 |
1427 | # define WMULT_CONST (~0UL) | |
1428 | #else | |
1429 | # define WMULT_CONST (1UL << 32) | |
1430 | #endif | |
1431 | ||
1432 | #define WMULT_SHIFT 32 | |
1433 | ||
194081eb IM |
1434 | /* |
1435 | * Shift right and round: | |
1436 | */ | |
cf2ab469 | 1437 | #define SRR(x, y) (((x) + (1UL << ((y) - 1))) >> (y)) |
194081eb | 1438 | |
a7be37ac PZ |
1439 | /* |
1440 | * delta *= weight / lw | |
1441 | */ | |
cb1c4fc9 | 1442 | static unsigned long |
45bf76df IM |
1443 | calc_delta_mine(unsigned long delta_exec, unsigned long weight, |
1444 | struct load_weight *lw) | |
1445 | { | |
1446 | u64 tmp; | |
1447 | ||
c8b28116 NR |
1448 | /* |
1449 | * weight can be less than 2^SCHED_LOAD_RESOLUTION for task group sched | |
1450 | * entities since MIN_SHARES = 2. Treat weight as 1 if less than | |
1451 | * 2^SCHED_LOAD_RESOLUTION. | |
1452 | */ | |
1453 | if (likely(weight > (1UL << SCHED_LOAD_RESOLUTION))) | |
1454 | tmp = (u64)delta_exec * scale_load_down(weight); | |
1455 | else | |
1456 | tmp = (u64)delta_exec; | |
db670dac | 1457 | |
7a232e03 | 1458 | if (!lw->inv_weight) { |
c8b28116 NR |
1459 | unsigned long w = scale_load_down(lw->weight); |
1460 | ||
1461 | if (BITS_PER_LONG > 32 && unlikely(w >= WMULT_CONST)) | |
7a232e03 | 1462 | lw->inv_weight = 1; |
c8b28116 NR |
1463 | else if (unlikely(!w)) |
1464 | lw->inv_weight = WMULT_CONST; | |
7a232e03 | 1465 | else |
c8b28116 | 1466 | lw->inv_weight = WMULT_CONST / w; |
7a232e03 | 1467 | } |
45bf76df | 1468 | |
45bf76df IM |
1469 | /* |
1470 | * Check whether we'd overflow the 64-bit multiplication: | |
1471 | */ | |
194081eb | 1472 | if (unlikely(tmp > WMULT_CONST)) |
cf2ab469 | 1473 | tmp = SRR(SRR(tmp, WMULT_SHIFT/2) * lw->inv_weight, |
194081eb IM |
1474 | WMULT_SHIFT/2); |
1475 | else | |
cf2ab469 | 1476 | tmp = SRR(tmp * lw->inv_weight, WMULT_SHIFT); |
45bf76df | 1477 | |
ecf691da | 1478 | return (unsigned long)min(tmp, (u64)(unsigned long)LONG_MAX); |
45bf76df IM |
1479 | } |
1480 | ||
1091985b | 1481 | static inline void update_load_add(struct load_weight *lw, unsigned long inc) |
45bf76df IM |
1482 | { |
1483 | lw->weight += inc; | |
e89996ae | 1484 | lw->inv_weight = 0; |
45bf76df IM |
1485 | } |
1486 | ||
1091985b | 1487 | static inline void update_load_sub(struct load_weight *lw, unsigned long dec) |
45bf76df IM |
1488 | { |
1489 | lw->weight -= dec; | |
e89996ae | 1490 | lw->inv_weight = 0; |
45bf76df IM |
1491 | } |
1492 | ||
2069dd75 PZ |
1493 | static inline void update_load_set(struct load_weight *lw, unsigned long w) |
1494 | { | |
1495 | lw->weight = w; | |
1496 | lw->inv_weight = 0; | |
1497 | } | |
1498 | ||
2dd73a4f PW |
1499 | /* |
1500 | * To aid in avoiding the subversion of "niceness" due to uneven distribution | |
1501 | * of tasks with abnormal "nice" values across CPUs the contribution that | |
1502 | * each task makes to its run queue's load is weighted according to its | |
41a2d6cf | 1503 | * scheduling class and "nice" value. For SCHED_NORMAL tasks this is just a |
2dd73a4f PW |
1504 | * scaled version of the new time slice allocation that they receive on time |
1505 | * slice expiry etc. | |
1506 | */ | |
1507 | ||
cce7ade8 PZ |
1508 | #define WEIGHT_IDLEPRIO 3 |
1509 | #define WMULT_IDLEPRIO 1431655765 | |
dd41f596 IM |
1510 | |
1511 | /* | |
1512 | * Nice levels are multiplicative, with a gentle 10% change for every | |
1513 | * nice level changed. I.e. when a CPU-bound task goes from nice 0 to | |
1514 | * nice 1, it will get ~10% less CPU time than another CPU-bound task | |
1515 | * that remained on nice 0. | |
1516 | * | |
1517 | * The "10% effect" is relative and cumulative: from _any_ nice level, | |
1518 | * if you go up 1 level, it's -10% CPU usage, if you go down 1 level | |
f9153ee6 IM |
1519 | * it's +10% CPU usage. (to achieve that we use a multiplier of 1.25. |
1520 | * If a task goes up by ~10% and another task goes down by ~10% then | |
1521 | * the relative distance between them is ~25%.) | |
dd41f596 IM |
1522 | */ |
1523 | static const int prio_to_weight[40] = { | |
254753dc IM |
1524 | /* -20 */ 88761, 71755, 56483, 46273, 36291, |
1525 | /* -15 */ 29154, 23254, 18705, 14949, 11916, | |
1526 | /* -10 */ 9548, 7620, 6100, 4904, 3906, | |
1527 | /* -5 */ 3121, 2501, 1991, 1586, 1277, | |
1528 | /* 0 */ 1024, 820, 655, 526, 423, | |
1529 | /* 5 */ 335, 272, 215, 172, 137, | |
1530 | /* 10 */ 110, 87, 70, 56, 45, | |
1531 | /* 15 */ 36, 29, 23, 18, 15, | |
dd41f596 IM |
1532 | }; |
1533 | ||
5714d2de IM |
1534 | /* |
1535 | * Inverse (2^32/x) values of the prio_to_weight[] array, precalculated. | |
1536 | * | |
1537 | * In cases where the weight does not change often, we can use the | |
1538 | * precalculated inverse to speed up arithmetics by turning divisions | |
1539 | * into multiplications: | |
1540 | */ | |
dd41f596 | 1541 | static const u32 prio_to_wmult[40] = { |
254753dc IM |
1542 | /* -20 */ 48388, 59856, 76040, 92818, 118348, |
1543 | /* -15 */ 147320, 184698, 229616, 287308, 360437, | |
1544 | /* -10 */ 449829, 563644, 704093, 875809, 1099582, | |
1545 | /* -5 */ 1376151, 1717300, 2157191, 2708050, 3363326, | |
1546 | /* 0 */ 4194304, 5237765, 6557202, 8165337, 10153587, | |
1547 | /* 5 */ 12820798, 15790321, 19976592, 24970740, 31350126, | |
1548 | /* 10 */ 39045157, 49367440, 61356676, 76695844, 95443717, | |
1549 | /* 15 */ 119304647, 148102320, 186737708, 238609294, 286331153, | |
dd41f596 | 1550 | }; |
2dd73a4f | 1551 | |
ef12fefa BR |
1552 | /* Time spent by the tasks of the cpu accounting group executing in ... */ |
1553 | enum cpuacct_stat_index { | |
1554 | CPUACCT_STAT_USER, /* ... user mode */ | |
1555 | CPUACCT_STAT_SYSTEM, /* ... kernel mode */ | |
1556 | ||
1557 | CPUACCT_STAT_NSTATS, | |
1558 | }; | |
1559 | ||
d842de87 SV |
1560 | #ifdef CONFIG_CGROUP_CPUACCT |
1561 | static void cpuacct_charge(struct task_struct *tsk, u64 cputime); | |
ef12fefa BR |
1562 | static void cpuacct_update_stats(struct task_struct *tsk, |
1563 | enum cpuacct_stat_index idx, cputime_t val); | |
d842de87 SV |
1564 | #else |
1565 | static inline void cpuacct_charge(struct task_struct *tsk, u64 cputime) {} | |
ef12fefa BR |
1566 | static inline void cpuacct_update_stats(struct task_struct *tsk, |
1567 | enum cpuacct_stat_index idx, cputime_t val) {} | |
d842de87 SV |
1568 | #endif |
1569 | ||
18d95a28 PZ |
1570 | static inline void inc_cpu_load(struct rq *rq, unsigned long load) |
1571 | { | |
1572 | update_load_add(&rq->load, load); | |
1573 | } | |
1574 | ||
1575 | static inline void dec_cpu_load(struct rq *rq, unsigned long load) | |
1576 | { | |
1577 | update_load_sub(&rq->load, load); | |
1578 | } | |
1579 | ||
a790de99 PT |
1580 | #if defined(CONFIG_RT_GROUP_SCHED) || (defined(CONFIG_FAIR_GROUP_SCHED) && \ |
1581 | (defined(CONFIG_SMP) || defined(CONFIG_CFS_BANDWIDTH))) | |
eb755805 | 1582 | typedef int (*tg_visitor)(struct task_group *, void *); |
c09595f6 PZ |
1583 | |
1584 | /* | |
1585 | * Iterate the full tree, calling @down when first entering a node and @up when | |
1586 | * leaving it for the final time. | |
1587 | */ | |
eb755805 | 1588 | static int walk_tg_tree(tg_visitor down, tg_visitor up, void *data) |
c09595f6 PZ |
1589 | { |
1590 | struct task_group *parent, *child; | |
eb755805 | 1591 | int ret; |
c09595f6 PZ |
1592 | |
1593 | rcu_read_lock(); | |
1594 | parent = &root_task_group; | |
1595 | down: | |
eb755805 PZ |
1596 | ret = (*down)(parent, data); |
1597 | if (ret) | |
1598 | goto out_unlock; | |
c09595f6 PZ |
1599 | list_for_each_entry_rcu(child, &parent->children, siblings) { |
1600 | parent = child; | |
1601 | goto down; | |
1602 | ||
1603 | up: | |
1604 | continue; | |
1605 | } | |
eb755805 PZ |
1606 | ret = (*up)(parent, data); |
1607 | if (ret) | |
1608 | goto out_unlock; | |
c09595f6 PZ |
1609 | |
1610 | child = parent; | |
1611 | parent = parent->parent; | |
1612 | if (parent) | |
1613 | goto up; | |
eb755805 | 1614 | out_unlock: |
c09595f6 | 1615 | rcu_read_unlock(); |
eb755805 PZ |
1616 | |
1617 | return ret; | |
c09595f6 PZ |
1618 | } |
1619 | ||
eb755805 PZ |
1620 | static int tg_nop(struct task_group *tg, void *data) |
1621 | { | |
1622 | return 0; | |
c09595f6 | 1623 | } |
eb755805 PZ |
1624 | #endif |
1625 | ||
1626 | #ifdef CONFIG_SMP | |
f5f08f39 PZ |
1627 | /* Used instead of source_load when we know the type == 0 */ |
1628 | static unsigned long weighted_cpuload(const int cpu) | |
1629 | { | |
1630 | return cpu_rq(cpu)->load.weight; | |
1631 | } | |
1632 | ||
1633 | /* | |
1634 | * Return a low guess at the load of a migration-source cpu weighted | |
1635 | * according to the scheduling class and "nice" value. | |
1636 | * | |
1637 | * We want to under-estimate the load of migration sources, to | |
1638 | * balance conservatively. | |
1639 | */ | |
1640 | static unsigned long source_load(int cpu, int type) | |
1641 | { | |
1642 | struct rq *rq = cpu_rq(cpu); | |
1643 | unsigned long total = weighted_cpuload(cpu); | |
1644 | ||
1645 | if (type == 0 || !sched_feat(LB_BIAS)) | |
1646 | return total; | |
1647 | ||
1648 | return min(rq->cpu_load[type-1], total); | |
1649 | } | |
1650 | ||
1651 | /* | |
1652 | * Return a high guess at the load of a migration-target cpu weighted | |
1653 | * according to the scheduling class and "nice" value. | |
1654 | */ | |
1655 | static unsigned long target_load(int cpu, int type) | |
1656 | { | |
1657 | struct rq *rq = cpu_rq(cpu); | |
1658 | unsigned long total = weighted_cpuload(cpu); | |
1659 | ||
1660 | if (type == 0 || !sched_feat(LB_BIAS)) | |
1661 | return total; | |
1662 | ||
1663 | return max(rq->cpu_load[type-1], total); | |
1664 | } | |
1665 | ||
ae154be1 PZ |
1666 | static unsigned long power_of(int cpu) |
1667 | { | |
e51fd5e2 | 1668 | return cpu_rq(cpu)->cpu_power; |
ae154be1 PZ |
1669 | } |
1670 | ||
eb755805 PZ |
1671 | static int task_hot(struct task_struct *p, u64 now, struct sched_domain *sd); |
1672 | ||
1673 | static unsigned long cpu_avg_load_per_task(int cpu) | |
1674 | { | |
1675 | struct rq *rq = cpu_rq(cpu); | |
af6d596f | 1676 | unsigned long nr_running = ACCESS_ONCE(rq->nr_running); |
eb755805 | 1677 | |
4cd42620 | 1678 | if (nr_running) |
e2b245f8 | 1679 | return rq->load.weight / nr_running; |
eb755805 | 1680 | |
e2b245f8 | 1681 | return 0; |
eb755805 PZ |
1682 | } |
1683 | ||
8f45e2b5 GH |
1684 | #ifdef CONFIG_PREEMPT |
1685 | ||
b78bb868 PZ |
1686 | static void double_rq_lock(struct rq *rq1, struct rq *rq2); |
1687 | ||
70574a99 | 1688 | /* |
8f45e2b5 GH |
1689 | * fair double_lock_balance: Safely acquires both rq->locks in a fair |
1690 | * way at the expense of forcing extra atomic operations in all | |
1691 | * invocations. This assures that the double_lock is acquired using the | |
1692 | * same underlying policy as the spinlock_t on this architecture, which | |
1693 | * reduces latency compared to the unfair variant below. However, it | |
1694 | * also adds more overhead and therefore may reduce throughput. | |
70574a99 | 1695 | */ |
8f45e2b5 GH |
1696 | static inline int _double_lock_balance(struct rq *this_rq, struct rq *busiest) |
1697 | __releases(this_rq->lock) | |
1698 | __acquires(busiest->lock) | |
1699 | __acquires(this_rq->lock) | |
1700 | { | |
05fa785c | 1701 | raw_spin_unlock(&this_rq->lock); |
8f45e2b5 GH |
1702 | double_rq_lock(this_rq, busiest); |
1703 | ||
1704 | return 1; | |
1705 | } | |
1706 | ||
1707 | #else | |
1708 | /* | |
1709 | * Unfair double_lock_balance: Optimizes throughput at the expense of | |
1710 | * latency by eliminating extra atomic operations when the locks are | |
1711 | * already in proper order on entry. This favors lower cpu-ids and will | |
1712 | * grant the double lock to lower cpus over higher ids under contention, | |
1713 | * regardless of entry order into the function. | |
1714 | */ | |
1715 | static int _double_lock_balance(struct rq *this_rq, struct rq *busiest) | |
70574a99 AD |
1716 | __releases(this_rq->lock) |
1717 | __acquires(busiest->lock) | |
1718 | __acquires(this_rq->lock) | |
1719 | { | |
1720 | int ret = 0; | |
1721 | ||
05fa785c | 1722 | if (unlikely(!raw_spin_trylock(&busiest->lock))) { |
70574a99 | 1723 | if (busiest < this_rq) { |
05fa785c TG |
1724 | raw_spin_unlock(&this_rq->lock); |
1725 | raw_spin_lock(&busiest->lock); | |
1726 | raw_spin_lock_nested(&this_rq->lock, | |
1727 | SINGLE_DEPTH_NESTING); | |
70574a99 AD |
1728 | ret = 1; |
1729 | } else | |
05fa785c TG |
1730 | raw_spin_lock_nested(&busiest->lock, |
1731 | SINGLE_DEPTH_NESTING); | |
70574a99 AD |
1732 | } |
1733 | return ret; | |
1734 | } | |
1735 | ||
8f45e2b5 GH |
1736 | #endif /* CONFIG_PREEMPT */ |
1737 | ||
1738 | /* | |
1739 | * double_lock_balance - lock the busiest runqueue, this_rq is locked already. | |
1740 | */ | |
1741 | static int double_lock_balance(struct rq *this_rq, struct rq *busiest) | |
1742 | { | |
1743 | if (unlikely(!irqs_disabled())) { | |
1744 | /* printk() doesn't work good under rq->lock */ | |
05fa785c | 1745 | raw_spin_unlock(&this_rq->lock); |
8f45e2b5 GH |
1746 | BUG_ON(1); |
1747 | } | |
1748 | ||
1749 | return _double_lock_balance(this_rq, busiest); | |
1750 | } | |
1751 | ||
70574a99 AD |
1752 | static inline void double_unlock_balance(struct rq *this_rq, struct rq *busiest) |
1753 | __releases(busiest->lock) | |
1754 | { | |
05fa785c | 1755 | raw_spin_unlock(&busiest->lock); |
70574a99 AD |
1756 | lock_set_subclass(&this_rq->lock.dep_map, 0, _RET_IP_); |
1757 | } | |
1e3c88bd PZ |
1758 | |
1759 | /* | |
1760 | * double_rq_lock - safely lock two runqueues | |
1761 | * | |
1762 | * Note this does not disable interrupts like task_rq_lock, | |
1763 | * you need to do so manually before calling. | |
1764 | */ | |
1765 | static void double_rq_lock(struct rq *rq1, struct rq *rq2) | |
1766 | __acquires(rq1->lock) | |
1767 | __acquires(rq2->lock) | |
1768 | { | |
1769 | BUG_ON(!irqs_disabled()); | |
1770 | if (rq1 == rq2) { | |
1771 | raw_spin_lock(&rq1->lock); | |
1772 | __acquire(rq2->lock); /* Fake it out ;) */ | |
1773 | } else { | |
1774 | if (rq1 < rq2) { | |
1775 | raw_spin_lock(&rq1->lock); | |
1776 | raw_spin_lock_nested(&rq2->lock, SINGLE_DEPTH_NESTING); | |
1777 | } else { | |
1778 | raw_spin_lock(&rq2->lock); | |
1779 | raw_spin_lock_nested(&rq1->lock, SINGLE_DEPTH_NESTING); | |
1780 | } | |
1781 | } | |
1e3c88bd PZ |
1782 | } |
1783 | ||
1784 | /* | |
1785 | * double_rq_unlock - safely unlock two runqueues | |
1786 | * | |
1787 | * Note this does not restore interrupts like task_rq_unlock, | |
1788 | * you need to do so manually after calling. | |
1789 | */ | |
1790 | static void double_rq_unlock(struct rq *rq1, struct rq *rq2) | |
1791 | __releases(rq1->lock) | |
1792 | __releases(rq2->lock) | |
1793 | { | |
1794 | raw_spin_unlock(&rq1->lock); | |
1795 | if (rq1 != rq2) | |
1796 | raw_spin_unlock(&rq2->lock); | |
1797 | else | |
1798 | __release(rq2->lock); | |
1799 | } | |
1800 | ||
d95f4122 MG |
1801 | #else /* CONFIG_SMP */ |
1802 | ||
1803 | /* | |
1804 | * double_rq_lock - safely lock two runqueues | |
1805 | * | |
1806 | * Note this does not disable interrupts like task_rq_lock, | |
1807 | * you need to do so manually before calling. | |
1808 | */ | |
1809 | static void double_rq_lock(struct rq *rq1, struct rq *rq2) | |
1810 | __acquires(rq1->lock) | |
1811 | __acquires(rq2->lock) | |
1812 | { | |
1813 | BUG_ON(!irqs_disabled()); | |
1814 | BUG_ON(rq1 != rq2); | |
1815 | raw_spin_lock(&rq1->lock); | |
1816 | __acquire(rq2->lock); /* Fake it out ;) */ | |
1817 | } | |
1818 | ||
1819 | /* | |
1820 | * double_rq_unlock - safely unlock two runqueues | |
1821 | * | |
1822 | * Note this does not restore interrupts like task_rq_unlock, | |
1823 | * you need to do so manually after calling. | |
1824 | */ | |
1825 | static void double_rq_unlock(struct rq *rq1, struct rq *rq2) | |
1826 | __releases(rq1->lock) | |
1827 | __releases(rq2->lock) | |
1828 | { | |
1829 | BUG_ON(rq1 != rq2); | |
1830 | raw_spin_unlock(&rq1->lock); | |
1831 | __release(rq2->lock); | |
1832 | } | |
1833 | ||
18d95a28 PZ |
1834 | #endif |
1835 | ||
74f5187a | 1836 | static void calc_load_account_idle(struct rq *this_rq); |
0bcdcf28 | 1837 | static void update_sysctl(void); |
acb4a848 | 1838 | static int get_update_sysctl_factor(void); |
fdf3e95d | 1839 | static void update_cpu_load(struct rq *this_rq); |
dce48a84 | 1840 | |
cd29fe6f PZ |
1841 | static inline void __set_task_cpu(struct task_struct *p, unsigned int cpu) |
1842 | { | |
1843 | set_task_rq(p, cpu); | |
1844 | #ifdef CONFIG_SMP | |
1845 | /* | |
1846 | * After ->cpu is set up to a new value, task_rq_lock(p, ...) can be | |
1847 | * successfuly executed on another CPU. We must ensure that updates of | |
1848 | * per-task data have been completed by this moment. | |
1849 | */ | |
1850 | smp_wmb(); | |
1851 | task_thread_info(p)->cpu = cpu; | |
1852 | #endif | |
1853 | } | |
dce48a84 | 1854 | |
1e3c88bd | 1855 | static const struct sched_class rt_sched_class; |
dd41f596 | 1856 | |
34f971f6 | 1857 | #define sched_class_highest (&stop_sched_class) |
1f11eb6a GH |
1858 | #define for_each_class(class) \ |
1859 | for (class = sched_class_highest; class; class = class->next) | |
dd41f596 | 1860 | |
1e3c88bd PZ |
1861 | #include "sched_stats.h" |
1862 | ||
c09595f6 | 1863 | static void inc_nr_running(struct rq *rq) |
9c217245 IM |
1864 | { |
1865 | rq->nr_running++; | |
9c217245 IM |
1866 | } |
1867 | ||
c09595f6 | 1868 | static void dec_nr_running(struct rq *rq) |
9c217245 IM |
1869 | { |
1870 | rq->nr_running--; | |
9c217245 IM |
1871 | } |
1872 | ||
45bf76df IM |
1873 | static void set_load_weight(struct task_struct *p) |
1874 | { | |
f05998d4 NR |
1875 | int prio = p->static_prio - MAX_RT_PRIO; |
1876 | struct load_weight *load = &p->se.load; | |
1877 | ||
dd41f596 IM |
1878 | /* |
1879 | * SCHED_IDLE tasks get minimal weight: | |
1880 | */ | |
1881 | if (p->policy == SCHED_IDLE) { | |
c8b28116 | 1882 | load->weight = scale_load(WEIGHT_IDLEPRIO); |
f05998d4 | 1883 | load->inv_weight = WMULT_IDLEPRIO; |
dd41f596 IM |
1884 | return; |
1885 | } | |
71f8bd46 | 1886 | |
c8b28116 | 1887 | load->weight = scale_load(prio_to_weight[prio]); |
f05998d4 | 1888 | load->inv_weight = prio_to_wmult[prio]; |
71f8bd46 IM |
1889 | } |
1890 | ||
371fd7e7 | 1891 | static void enqueue_task(struct rq *rq, struct task_struct *p, int flags) |
2087a1ad | 1892 | { |
a64692a3 | 1893 | update_rq_clock(rq); |
dd41f596 | 1894 | sched_info_queued(p); |
371fd7e7 | 1895 | p->sched_class->enqueue_task(rq, p, flags); |
71f8bd46 IM |
1896 | } |
1897 | ||
371fd7e7 | 1898 | static void dequeue_task(struct rq *rq, struct task_struct *p, int flags) |
71f8bd46 | 1899 | { |
a64692a3 | 1900 | update_rq_clock(rq); |
46ac22ba | 1901 | sched_info_dequeued(p); |
371fd7e7 | 1902 | p->sched_class->dequeue_task(rq, p, flags); |
71f8bd46 IM |
1903 | } |
1904 | ||
1e3c88bd PZ |
1905 | /* |
1906 | * activate_task - move a task to the runqueue. | |
1907 | */ | |
371fd7e7 | 1908 | static void activate_task(struct rq *rq, struct task_struct *p, int flags) |
1e3c88bd PZ |
1909 | { |
1910 | if (task_contributes_to_load(p)) | |
1911 | rq->nr_uninterruptible--; | |
1912 | ||
371fd7e7 | 1913 | enqueue_task(rq, p, flags); |
1e3c88bd PZ |
1914 | } |
1915 | ||
1916 | /* | |
1917 | * deactivate_task - remove a task from the runqueue. | |
1918 | */ | |
371fd7e7 | 1919 | static void deactivate_task(struct rq *rq, struct task_struct *p, int flags) |
1e3c88bd PZ |
1920 | { |
1921 | if (task_contributes_to_load(p)) | |
1922 | rq->nr_uninterruptible++; | |
1923 | ||
371fd7e7 | 1924 | dequeue_task(rq, p, flags); |
1e3c88bd PZ |
1925 | } |
1926 | ||
b52bfee4 VP |
1927 | #ifdef CONFIG_IRQ_TIME_ACCOUNTING |
1928 | ||
305e6835 VP |
1929 | /* |
1930 | * There are no locks covering percpu hardirq/softirq time. | |
1931 | * They are only modified in account_system_vtime, on corresponding CPU | |
1932 | * with interrupts disabled. So, writes are safe. | |
1933 | * They are read and saved off onto struct rq in update_rq_clock(). | |
1934 | * This may result in other CPU reading this CPU's irq time and can | |
1935 | * race with irq/account_system_vtime on this CPU. We would either get old | |
8e92c201 PZ |
1936 | * or new value with a side effect of accounting a slice of irq time to wrong |
1937 | * task when irq is in progress while we read rq->clock. That is a worthy | |
1938 | * compromise in place of having locks on each irq in account_system_time. | |
305e6835 | 1939 | */ |
b52bfee4 VP |
1940 | static DEFINE_PER_CPU(u64, cpu_hardirq_time); |
1941 | static DEFINE_PER_CPU(u64, cpu_softirq_time); | |
1942 | ||
1943 | static DEFINE_PER_CPU(u64, irq_start_time); | |
1944 | static int sched_clock_irqtime; | |
1945 | ||
1946 | void enable_sched_clock_irqtime(void) | |
1947 | { | |
1948 | sched_clock_irqtime = 1; | |
1949 | } | |
1950 | ||
1951 | void disable_sched_clock_irqtime(void) | |
1952 | { | |
1953 | sched_clock_irqtime = 0; | |
1954 | } | |
1955 | ||
8e92c201 PZ |
1956 | #ifndef CONFIG_64BIT |
1957 | static DEFINE_PER_CPU(seqcount_t, irq_time_seq); | |
1958 | ||
1959 | static inline void irq_time_write_begin(void) | |
1960 | { | |
1961 | __this_cpu_inc(irq_time_seq.sequence); | |
1962 | smp_wmb(); | |
1963 | } | |
1964 | ||
1965 | static inline void irq_time_write_end(void) | |
1966 | { | |
1967 | smp_wmb(); | |
1968 | __this_cpu_inc(irq_time_seq.sequence); | |
1969 | } | |
1970 | ||
1971 | static inline u64 irq_time_read(int cpu) | |
1972 | { | |
1973 | u64 irq_time; | |
1974 | unsigned seq; | |
1975 | ||
1976 | do { | |
1977 | seq = read_seqcount_begin(&per_cpu(irq_time_seq, cpu)); | |
1978 | irq_time = per_cpu(cpu_softirq_time, cpu) + | |
1979 | per_cpu(cpu_hardirq_time, cpu); | |
1980 | } while (read_seqcount_retry(&per_cpu(irq_time_seq, cpu), seq)); | |
1981 | ||
1982 | return irq_time; | |
1983 | } | |
1984 | #else /* CONFIG_64BIT */ | |
1985 | static inline void irq_time_write_begin(void) | |
1986 | { | |
1987 | } | |
1988 | ||
1989 | static inline void irq_time_write_end(void) | |
1990 | { | |
1991 | } | |
1992 | ||
1993 | static inline u64 irq_time_read(int cpu) | |
305e6835 | 1994 | { |
305e6835 VP |
1995 | return per_cpu(cpu_softirq_time, cpu) + per_cpu(cpu_hardirq_time, cpu); |
1996 | } | |
8e92c201 | 1997 | #endif /* CONFIG_64BIT */ |
305e6835 | 1998 | |
fe44d621 PZ |
1999 | /* |
2000 | * Called before incrementing preempt_count on {soft,}irq_enter | |
2001 | * and before decrementing preempt_count on {soft,}irq_exit. | |
2002 | */ | |
b52bfee4 VP |
2003 | void account_system_vtime(struct task_struct *curr) |
2004 | { | |
2005 | unsigned long flags; | |
fe44d621 | 2006 | s64 delta; |
b52bfee4 | 2007 | int cpu; |
b52bfee4 VP |
2008 | |
2009 | if (!sched_clock_irqtime) | |
2010 | return; | |
2011 | ||
2012 | local_irq_save(flags); | |
2013 | ||
b52bfee4 | 2014 | cpu = smp_processor_id(); |
fe44d621 PZ |
2015 | delta = sched_clock_cpu(cpu) - __this_cpu_read(irq_start_time); |
2016 | __this_cpu_add(irq_start_time, delta); | |
2017 | ||
8e92c201 | 2018 | irq_time_write_begin(); |
b52bfee4 VP |
2019 | /* |
2020 | * We do not account for softirq time from ksoftirqd here. | |
2021 | * We want to continue accounting softirq time to ksoftirqd thread | |
2022 | * in that case, so as not to confuse scheduler with a special task | |
2023 | * that do not consume any time, but still wants to run. | |
2024 | */ | |
2025 | if (hardirq_count()) | |
fe44d621 | 2026 | __this_cpu_add(cpu_hardirq_time, delta); |
4dd53d89 | 2027 | else if (in_serving_softirq() && curr != this_cpu_ksoftirqd()) |
fe44d621 | 2028 | __this_cpu_add(cpu_softirq_time, delta); |
b52bfee4 | 2029 | |
8e92c201 | 2030 | irq_time_write_end(); |
b52bfee4 VP |
2031 | local_irq_restore(flags); |
2032 | } | |
b7dadc38 | 2033 | EXPORT_SYMBOL_GPL(account_system_vtime); |
b52bfee4 | 2034 | |
e6e6685a GC |
2035 | #endif /* CONFIG_IRQ_TIME_ACCOUNTING */ |
2036 | ||
2037 | #ifdef CONFIG_PARAVIRT | |
2038 | static inline u64 steal_ticks(u64 steal) | |
aa483808 | 2039 | { |
e6e6685a GC |
2040 | if (unlikely(steal > NSEC_PER_SEC)) |
2041 | return div_u64(steal, TICK_NSEC); | |
fe44d621 | 2042 | |
e6e6685a GC |
2043 | return __iter_div_u64_rem(steal, TICK_NSEC, &steal); |
2044 | } | |
2045 | #endif | |
2046 | ||
fe44d621 | 2047 | static void update_rq_clock_task(struct rq *rq, s64 delta) |
aa483808 | 2048 | { |
095c0aa8 GC |
2049 | /* |
2050 | * In theory, the compile should just see 0 here, and optimize out the call | |
2051 | * to sched_rt_avg_update. But I don't trust it... | |
2052 | */ | |
2053 | #if defined(CONFIG_IRQ_TIME_ACCOUNTING) || defined(CONFIG_PARAVIRT_TIME_ACCOUNTING) | |
2054 | s64 steal = 0, irq_delta = 0; | |
2055 | #endif | |
2056 | #ifdef CONFIG_IRQ_TIME_ACCOUNTING | |
8e92c201 | 2057 | irq_delta = irq_time_read(cpu_of(rq)) - rq->prev_irq_time; |
fe44d621 PZ |
2058 | |
2059 | /* | |
2060 | * Since irq_time is only updated on {soft,}irq_exit, we might run into | |
2061 | * this case when a previous update_rq_clock() happened inside a | |
2062 | * {soft,}irq region. | |
2063 | * | |
2064 | * When this happens, we stop ->clock_task and only update the | |
2065 | * prev_irq_time stamp to account for the part that fit, so that a next | |
2066 | * update will consume the rest. This ensures ->clock_task is | |
2067 | * monotonic. | |
2068 | * | |
2069 | * It does however cause some slight miss-attribution of {soft,}irq | |
2070 | * time, a more accurate solution would be to update the irq_time using | |
2071 | * the current rq->clock timestamp, except that would require using | |
2072 | * atomic ops. | |
2073 | */ | |
2074 | if (irq_delta > delta) | |
2075 | irq_delta = delta; | |
2076 | ||
2077 | rq->prev_irq_time += irq_delta; | |
2078 | delta -= irq_delta; | |
095c0aa8 GC |
2079 | #endif |
2080 | #ifdef CONFIG_PARAVIRT_TIME_ACCOUNTING | |
2081 | if (static_branch((¶virt_steal_rq_enabled))) { | |
2082 | u64 st; | |
2083 | ||
2084 | steal = paravirt_steal_clock(cpu_of(rq)); | |
2085 | steal -= rq->prev_steal_time_rq; | |
2086 | ||
2087 | if (unlikely(steal > delta)) | |
2088 | steal = delta; | |
2089 | ||
2090 | st = steal_ticks(steal); | |
2091 | steal = st * TICK_NSEC; | |
2092 | ||
2093 | rq->prev_steal_time_rq += steal; | |
2094 | ||
2095 | delta -= steal; | |
2096 | } | |
2097 | #endif | |
2098 | ||
fe44d621 PZ |
2099 | rq->clock_task += delta; |
2100 | ||
095c0aa8 GC |
2101 | #if defined(CONFIG_IRQ_TIME_ACCOUNTING) || defined(CONFIG_PARAVIRT_TIME_ACCOUNTING) |
2102 | if ((irq_delta + steal) && sched_feat(NONTASK_POWER)) | |
2103 | sched_rt_avg_update(rq, irq_delta + steal); | |
2104 | #endif | |
aa483808 VP |
2105 | } |
2106 | ||
095c0aa8 | 2107 | #ifdef CONFIG_IRQ_TIME_ACCOUNTING |
abb74cef VP |
2108 | static int irqtime_account_hi_update(void) |
2109 | { | |
2110 | struct cpu_usage_stat *cpustat = &kstat_this_cpu.cpustat; | |
2111 | unsigned long flags; | |
2112 | u64 latest_ns; | |
2113 | int ret = 0; | |
2114 | ||
2115 | local_irq_save(flags); | |
2116 | latest_ns = this_cpu_read(cpu_hardirq_time); | |
2117 | if (cputime64_gt(nsecs_to_cputime64(latest_ns), cpustat->irq)) | |
2118 | ret = 1; | |
2119 | local_irq_restore(flags); | |
2120 | return ret; | |
2121 | } | |
2122 | ||
2123 | static int irqtime_account_si_update(void) | |
2124 | { | |
2125 | struct cpu_usage_stat *cpustat = &kstat_this_cpu.cpustat; | |
2126 | unsigned long flags; | |
2127 | u64 latest_ns; | |
2128 | int ret = 0; | |
2129 | ||
2130 | local_irq_save(flags); | |
2131 | latest_ns = this_cpu_read(cpu_softirq_time); | |
2132 | if (cputime64_gt(nsecs_to_cputime64(latest_ns), cpustat->softirq)) | |
2133 | ret = 1; | |
2134 | local_irq_restore(flags); | |
2135 | return ret; | |
2136 | } | |
2137 | ||
fe44d621 | 2138 | #else /* CONFIG_IRQ_TIME_ACCOUNTING */ |
305e6835 | 2139 | |
abb74cef VP |
2140 | #define sched_clock_irqtime (0) |
2141 | ||
095c0aa8 | 2142 | #endif |
b52bfee4 | 2143 | |
1e3c88bd PZ |
2144 | #include "sched_idletask.c" |
2145 | #include "sched_fair.c" | |
2146 | #include "sched_rt.c" | |
5091faa4 | 2147 | #include "sched_autogroup.c" |
34f971f6 | 2148 | #include "sched_stoptask.c" |
1e3c88bd PZ |
2149 | #ifdef CONFIG_SCHED_DEBUG |
2150 | # include "sched_debug.c" | |
2151 | #endif | |
2152 | ||
34f971f6 PZ |
2153 | void sched_set_stop_task(int cpu, struct task_struct *stop) |
2154 | { | |
2155 | struct sched_param param = { .sched_priority = MAX_RT_PRIO - 1 }; | |
2156 | struct task_struct *old_stop = cpu_rq(cpu)->stop; | |
2157 | ||
2158 | if (stop) { | |
2159 | /* | |
2160 | * Make it appear like a SCHED_FIFO task, its something | |
2161 | * userspace knows about and won't get confused about. | |
2162 | * | |
2163 | * Also, it will make PI more or less work without too | |
2164 | * much confusion -- but then, stop work should not | |
2165 | * rely on PI working anyway. | |
2166 | */ | |
2167 | sched_setscheduler_nocheck(stop, SCHED_FIFO, ¶m); | |
2168 | ||
2169 | stop->sched_class = &stop_sched_class; | |
2170 | } | |
2171 | ||
2172 | cpu_rq(cpu)->stop = stop; | |
2173 | ||
2174 | if (old_stop) { | |
2175 | /* | |
2176 | * Reset it back to a normal scheduling class so that | |
2177 | * it can die in pieces. | |
2178 | */ | |
2179 | old_stop->sched_class = &rt_sched_class; | |
2180 | } | |
2181 | } | |
2182 | ||
14531189 | 2183 | /* |
dd41f596 | 2184 | * __normal_prio - return the priority that is based on the static prio |
14531189 | 2185 | */ |
14531189 IM |
2186 | static inline int __normal_prio(struct task_struct *p) |
2187 | { | |
dd41f596 | 2188 | return p->static_prio; |
14531189 IM |
2189 | } |
2190 | ||
b29739f9 IM |
2191 | /* |
2192 | * Calculate the expected normal priority: i.e. priority | |
2193 | * without taking RT-inheritance into account. Might be | |
2194 | * boosted by interactivity modifiers. Changes upon fork, | |
2195 | * setprio syscalls, and whenever the interactivity | |
2196 | * estimator recalculates. | |
2197 | */ | |
36c8b586 | 2198 | static inline int normal_prio(struct task_struct *p) |
b29739f9 IM |
2199 | { |
2200 | int prio; | |
2201 | ||
e05606d3 | 2202 | if (task_has_rt_policy(p)) |
b29739f9 IM |
2203 | prio = MAX_RT_PRIO-1 - p->rt_priority; |
2204 | else | |
2205 | prio = __normal_prio(p); | |
2206 | return prio; | |
2207 | } | |
2208 | ||
2209 | /* | |
2210 | * Calculate the current priority, i.e. the priority | |
2211 | * taken into account by the scheduler. This value might | |
2212 | * be boosted by RT tasks, or might be boosted by | |
2213 | * interactivity modifiers. Will be RT if the task got | |
2214 | * RT-boosted. If not then it returns p->normal_prio. | |
2215 | */ | |
36c8b586 | 2216 | static int effective_prio(struct task_struct *p) |
b29739f9 IM |
2217 | { |
2218 | p->normal_prio = normal_prio(p); | |
2219 | /* | |
2220 | * If we are RT tasks or we were boosted to RT priority, | |
2221 | * keep the priority unchanged. Otherwise, update priority | |
2222 | * to the normal priority: | |
2223 | */ | |
2224 | if (!rt_prio(p->prio)) | |
2225 | return p->normal_prio; | |
2226 | return p->prio; | |
2227 | } | |
2228 | ||
1da177e4 LT |
2229 | /** |
2230 | * task_curr - is this task currently executing on a CPU? | |
2231 | * @p: the task in question. | |
2232 | */ | |
36c8b586 | 2233 | inline int task_curr(const struct task_struct *p) |
1da177e4 LT |
2234 | { |
2235 | return cpu_curr(task_cpu(p)) == p; | |
2236 | } | |
2237 | ||
cb469845 SR |
2238 | static inline void check_class_changed(struct rq *rq, struct task_struct *p, |
2239 | const struct sched_class *prev_class, | |
da7a735e | 2240 | int oldprio) |
cb469845 SR |
2241 | { |
2242 | if (prev_class != p->sched_class) { | |
2243 | if (prev_class->switched_from) | |
da7a735e PZ |
2244 | prev_class->switched_from(rq, p); |
2245 | p->sched_class->switched_to(rq, p); | |
2246 | } else if (oldprio != p->prio) | |
2247 | p->sched_class->prio_changed(rq, p, oldprio); | |
cb469845 SR |
2248 | } |
2249 | ||
1e5a7405 PZ |
2250 | static void check_preempt_curr(struct rq *rq, struct task_struct *p, int flags) |
2251 | { | |
2252 | const struct sched_class *class; | |
2253 | ||
2254 | if (p->sched_class == rq->curr->sched_class) { | |
2255 | rq->curr->sched_class->check_preempt_curr(rq, p, flags); | |
2256 | } else { | |
2257 | for_each_class(class) { | |
2258 | if (class == rq->curr->sched_class) | |
2259 | break; | |
2260 | if (class == p->sched_class) { | |
2261 | resched_task(rq->curr); | |
2262 | break; | |
2263 | } | |
2264 | } | |
2265 | } | |
2266 | ||
2267 | /* | |
2268 | * A queue event has occurred, and we're going to schedule. In | |
2269 | * this case, we can save a useless back to back clock update. | |
2270 | */ | |
fd2f4419 | 2271 | if (rq->curr->on_rq && test_tsk_need_resched(rq->curr)) |
1e5a7405 PZ |
2272 | rq->skip_clock_update = 1; |
2273 | } | |
2274 | ||
1da177e4 | 2275 | #ifdef CONFIG_SMP |
cc367732 IM |
2276 | /* |
2277 | * Is this task likely cache-hot: | |
2278 | */ | |
e7693a36 | 2279 | static int |
cc367732 IM |
2280 | task_hot(struct task_struct *p, u64 now, struct sched_domain *sd) |
2281 | { | |
2282 | s64 delta; | |
2283 | ||
e6c8fba7 PZ |
2284 | if (p->sched_class != &fair_sched_class) |
2285 | return 0; | |
2286 | ||
ef8002f6 NR |
2287 | if (unlikely(p->policy == SCHED_IDLE)) |
2288 | return 0; | |
2289 | ||
f540a608 IM |
2290 | /* |
2291 | * Buddy candidates are cache hot: | |
2292 | */ | |
f685ceac | 2293 | if (sched_feat(CACHE_HOT_BUDDY) && this_rq()->nr_running && |
4793241b PZ |
2294 | (&p->se == cfs_rq_of(&p->se)->next || |
2295 | &p->se == cfs_rq_of(&p->se)->last)) | |
f540a608 IM |
2296 | return 1; |
2297 | ||
6bc1665b IM |
2298 | if (sysctl_sched_migration_cost == -1) |
2299 | return 1; | |
2300 | if (sysctl_sched_migration_cost == 0) | |
2301 | return 0; | |
2302 | ||
cc367732 IM |
2303 | delta = now - p->se.exec_start; |
2304 | ||
2305 | return delta < (s64)sysctl_sched_migration_cost; | |
2306 | } | |
2307 | ||
dd41f596 | 2308 | void set_task_cpu(struct task_struct *p, unsigned int new_cpu) |
c65cc870 | 2309 | { |
e2912009 PZ |
2310 | #ifdef CONFIG_SCHED_DEBUG |
2311 | /* | |
2312 | * We should never call set_task_cpu() on a blocked task, | |
2313 | * ttwu() will sort out the placement. | |
2314 | */ | |
077614ee PZ |
2315 | WARN_ON_ONCE(p->state != TASK_RUNNING && p->state != TASK_WAKING && |
2316 | !(task_thread_info(p)->preempt_count & PREEMPT_ACTIVE)); | |
0122ec5b PZ |
2317 | |
2318 | #ifdef CONFIG_LOCKDEP | |
6c6c54e1 PZ |
2319 | /* |
2320 | * The caller should hold either p->pi_lock or rq->lock, when changing | |
2321 | * a task's CPU. ->pi_lock for waking tasks, rq->lock for runnable tasks. | |
2322 | * | |
2323 | * sched_move_task() holds both and thus holding either pins the cgroup, | |
2324 | * see set_task_rq(). | |
2325 | * | |
2326 | * Furthermore, all task_rq users should acquire both locks, see | |
2327 | * task_rq_lock(). | |
2328 | */ | |
0122ec5b PZ |
2329 | WARN_ON_ONCE(debug_locks && !(lockdep_is_held(&p->pi_lock) || |
2330 | lockdep_is_held(&task_rq(p)->lock))); | |
2331 | #endif | |
e2912009 PZ |
2332 | #endif |
2333 | ||
de1d7286 | 2334 | trace_sched_migrate_task(p, new_cpu); |
cbc34ed1 | 2335 | |
0c69774e PZ |
2336 | if (task_cpu(p) != new_cpu) { |
2337 | p->se.nr_migrations++; | |
a8b0ca17 | 2338 | perf_sw_event(PERF_COUNT_SW_CPU_MIGRATIONS, 1, NULL, 0); |
0c69774e | 2339 | } |
dd41f596 IM |
2340 | |
2341 | __set_task_cpu(p, new_cpu); | |
c65cc870 IM |
2342 | } |
2343 | ||
969c7921 | 2344 | struct migration_arg { |
36c8b586 | 2345 | struct task_struct *task; |
1da177e4 | 2346 | int dest_cpu; |
70b97a7f | 2347 | }; |
1da177e4 | 2348 | |
969c7921 TH |
2349 | static int migration_cpu_stop(void *data); |
2350 | ||
1da177e4 LT |
2351 | /* |
2352 | * wait_task_inactive - wait for a thread to unschedule. | |
2353 | * | |
85ba2d86 RM |
2354 | * If @match_state is nonzero, it's the @p->state value just checked and |
2355 | * not expected to change. If it changes, i.e. @p might have woken up, | |
2356 | * then return zero. When we succeed in waiting for @p to be off its CPU, | |
2357 | * we return a positive number (its total switch count). If a second call | |
2358 | * a short while later returns the same number, the caller can be sure that | |
2359 | * @p has remained unscheduled the whole time. | |
2360 | * | |
1da177e4 LT |
2361 | * The caller must ensure that the task *will* unschedule sometime soon, |
2362 | * else this function might spin for a *long* time. This function can't | |
2363 | * be called with interrupts off, or it may introduce deadlock with | |
2364 | * smp_call_function() if an IPI is sent by the same process we are | |
2365 | * waiting to become inactive. | |
2366 | */ | |
85ba2d86 | 2367 | unsigned long wait_task_inactive(struct task_struct *p, long match_state) |
1da177e4 LT |
2368 | { |
2369 | unsigned long flags; | |
dd41f596 | 2370 | int running, on_rq; |
85ba2d86 | 2371 | unsigned long ncsw; |
70b97a7f | 2372 | struct rq *rq; |
1da177e4 | 2373 | |
3a5c359a AK |
2374 | for (;;) { |
2375 | /* | |
2376 | * We do the initial early heuristics without holding | |
2377 | * any task-queue locks at all. We'll only try to get | |
2378 | * the runqueue lock when things look like they will | |
2379 | * work out! | |
2380 | */ | |
2381 | rq = task_rq(p); | |
fa490cfd | 2382 | |
3a5c359a AK |
2383 | /* |
2384 | * If the task is actively running on another CPU | |
2385 | * still, just relax and busy-wait without holding | |
2386 | * any locks. | |
2387 | * | |
2388 | * NOTE! Since we don't hold any locks, it's not | |
2389 | * even sure that "rq" stays as the right runqueue! | |
2390 | * But we don't care, since "task_running()" will | |
2391 | * return false if the runqueue has changed and p | |
2392 | * is actually now running somewhere else! | |
2393 | */ | |
85ba2d86 RM |
2394 | while (task_running(rq, p)) { |
2395 | if (match_state && unlikely(p->state != match_state)) | |
2396 | return 0; | |
3a5c359a | 2397 | cpu_relax(); |
85ba2d86 | 2398 | } |
fa490cfd | 2399 | |
3a5c359a AK |
2400 | /* |
2401 | * Ok, time to look more closely! We need the rq | |
2402 | * lock now, to be *sure*. If we're wrong, we'll | |
2403 | * just go back and repeat. | |
2404 | */ | |
2405 | rq = task_rq_lock(p, &flags); | |
27a9da65 | 2406 | trace_sched_wait_task(p); |
3a5c359a | 2407 | running = task_running(rq, p); |
fd2f4419 | 2408 | on_rq = p->on_rq; |
85ba2d86 | 2409 | ncsw = 0; |
f31e11d8 | 2410 | if (!match_state || p->state == match_state) |
93dcf55f | 2411 | ncsw = p->nvcsw | LONG_MIN; /* sets MSB */ |
0122ec5b | 2412 | task_rq_unlock(rq, p, &flags); |
fa490cfd | 2413 | |
85ba2d86 RM |
2414 | /* |
2415 | * If it changed from the expected state, bail out now. | |
2416 | */ | |
2417 | if (unlikely(!ncsw)) | |
2418 | break; | |
2419 | ||
3a5c359a AK |
2420 | /* |
2421 | * Was it really running after all now that we | |
2422 | * checked with the proper locks actually held? | |
2423 | * | |
2424 | * Oops. Go back and try again.. | |
2425 | */ | |
2426 | if (unlikely(running)) { | |
2427 | cpu_relax(); | |
2428 | continue; | |
2429 | } | |
fa490cfd | 2430 | |
3a5c359a AK |
2431 | /* |
2432 | * It's not enough that it's not actively running, | |
2433 | * it must be off the runqueue _entirely_, and not | |
2434 | * preempted! | |
2435 | * | |
80dd99b3 | 2436 | * So if it was still runnable (but just not actively |
3a5c359a AK |
2437 | * running right now), it's preempted, and we should |
2438 | * yield - it could be a while. | |
2439 | */ | |
2440 | if (unlikely(on_rq)) { | |
8eb90c30 TG |
2441 | ktime_t to = ktime_set(0, NSEC_PER_SEC/HZ); |
2442 | ||
2443 | set_current_state(TASK_UNINTERRUPTIBLE); | |
2444 | schedule_hrtimeout(&to, HRTIMER_MODE_REL); | |
3a5c359a AK |
2445 | continue; |
2446 | } | |
fa490cfd | 2447 | |
3a5c359a AK |
2448 | /* |
2449 | * Ahh, all good. It wasn't running, and it wasn't | |
2450 | * runnable, which means that it will never become | |
2451 | * running in the future either. We're all done! | |
2452 | */ | |
2453 | break; | |
2454 | } | |
85ba2d86 RM |
2455 | |
2456 | return ncsw; | |
1da177e4 LT |
2457 | } |
2458 | ||
2459 | /*** | |
2460 | * kick_process - kick a running thread to enter/exit the kernel | |
2461 | * @p: the to-be-kicked thread | |
2462 | * | |
2463 | * Cause a process which is running on another CPU to enter | |
2464 | * kernel-mode, without any delay. (to get signals handled.) | |
2465 | * | |
25985edc | 2466 | * NOTE: this function doesn't have to take the runqueue lock, |
1da177e4 LT |
2467 | * because all it wants to ensure is that the remote task enters |
2468 | * the kernel. If the IPI races and the task has been migrated | |
2469 | * to another CPU then no harm is done and the purpose has been | |
2470 | * achieved as well. | |
2471 | */ | |
36c8b586 | 2472 | void kick_process(struct task_struct *p) |
1da177e4 LT |
2473 | { |
2474 | int cpu; | |
2475 | ||
2476 | preempt_disable(); | |
2477 | cpu = task_cpu(p); | |
2478 | if ((cpu != smp_processor_id()) && task_curr(p)) | |
2479 | smp_send_reschedule(cpu); | |
2480 | preempt_enable(); | |
2481 | } | |
b43e3521 | 2482 | EXPORT_SYMBOL_GPL(kick_process); |
476d139c | 2483 | #endif /* CONFIG_SMP */ |
1da177e4 | 2484 | |
970b13ba | 2485 | #ifdef CONFIG_SMP |
30da688e | 2486 | /* |
013fdb80 | 2487 | * ->cpus_allowed is protected by both rq->lock and p->pi_lock |
30da688e | 2488 | */ |
5da9a0fb PZ |
2489 | static int select_fallback_rq(int cpu, struct task_struct *p) |
2490 | { | |
2491 | int dest_cpu; | |
2492 | const struct cpumask *nodemask = cpumask_of_node(cpu_to_node(cpu)); | |
2493 | ||
2494 | /* Look for allowed, online CPU in same node. */ | |
2495 | for_each_cpu_and(dest_cpu, nodemask, cpu_active_mask) | |
2496 | if (cpumask_test_cpu(dest_cpu, &p->cpus_allowed)) | |
2497 | return dest_cpu; | |
2498 | ||
2499 | /* Any allowed, online CPU? */ | |
2500 | dest_cpu = cpumask_any_and(&p->cpus_allowed, cpu_active_mask); | |
2501 | if (dest_cpu < nr_cpu_ids) | |
2502 | return dest_cpu; | |
2503 | ||
2504 | /* No more Mr. Nice Guy. */ | |
48c5ccae PZ |
2505 | dest_cpu = cpuset_cpus_allowed_fallback(p); |
2506 | /* | |
2507 | * Don't tell them about moving exiting tasks or | |
2508 | * kernel threads (both mm NULL), since they never | |
2509 | * leave kernel. | |
2510 | */ | |
2511 | if (p->mm && printk_ratelimit()) { | |
2512 | printk(KERN_INFO "process %d (%s) no longer affine to cpu%d\n", | |
2513 | task_pid_nr(p), p->comm, cpu); | |
5da9a0fb PZ |
2514 | } |
2515 | ||
2516 | return dest_cpu; | |
2517 | } | |
2518 | ||
e2912009 | 2519 | /* |
013fdb80 | 2520 | * The caller (fork, wakeup) owns p->pi_lock, ->cpus_allowed is stable. |
e2912009 | 2521 | */ |
970b13ba | 2522 | static inline |
7608dec2 | 2523 | int select_task_rq(struct task_struct *p, int sd_flags, int wake_flags) |
970b13ba | 2524 | { |
7608dec2 | 2525 | int cpu = p->sched_class->select_task_rq(p, sd_flags, wake_flags); |
e2912009 PZ |
2526 | |
2527 | /* | |
2528 | * In order not to call set_task_cpu() on a blocking task we need | |
2529 | * to rely on ttwu() to place the task on a valid ->cpus_allowed | |
2530 | * cpu. | |
2531 | * | |
2532 | * Since this is common to all placement strategies, this lives here. | |
2533 | * | |
2534 | * [ this allows ->select_task() to simply return task_cpu(p) and | |
2535 | * not worry about this generic constraint ] | |
2536 | */ | |
2537 | if (unlikely(!cpumask_test_cpu(cpu, &p->cpus_allowed) || | |
70f11205 | 2538 | !cpu_online(cpu))) |
5da9a0fb | 2539 | cpu = select_fallback_rq(task_cpu(p), p); |
e2912009 PZ |
2540 | |
2541 | return cpu; | |
970b13ba | 2542 | } |
09a40af5 MG |
2543 | |
2544 | static void update_avg(u64 *avg, u64 sample) | |
2545 | { | |
2546 | s64 diff = sample - *avg; | |
2547 | *avg += diff >> 3; | |
2548 | } | |
970b13ba PZ |
2549 | #endif |
2550 | ||
d7c01d27 | 2551 | static void |
b84cb5df | 2552 | ttwu_stat(struct task_struct *p, int cpu, int wake_flags) |
9ed3811a | 2553 | { |
d7c01d27 | 2554 | #ifdef CONFIG_SCHEDSTATS |
b84cb5df PZ |
2555 | struct rq *rq = this_rq(); |
2556 | ||
d7c01d27 PZ |
2557 | #ifdef CONFIG_SMP |
2558 | int this_cpu = smp_processor_id(); | |
2559 | ||
2560 | if (cpu == this_cpu) { | |
2561 | schedstat_inc(rq, ttwu_local); | |
2562 | schedstat_inc(p, se.statistics.nr_wakeups_local); | |
2563 | } else { | |
2564 | struct sched_domain *sd; | |
2565 | ||
2566 | schedstat_inc(p, se.statistics.nr_wakeups_remote); | |
057f3fad | 2567 | rcu_read_lock(); |
d7c01d27 PZ |
2568 | for_each_domain(this_cpu, sd) { |
2569 | if (cpumask_test_cpu(cpu, sched_domain_span(sd))) { | |
2570 | schedstat_inc(sd, ttwu_wake_remote); | |
2571 | break; | |
2572 | } | |
2573 | } | |
057f3fad | 2574 | rcu_read_unlock(); |
d7c01d27 | 2575 | } |
f339b9dc PZ |
2576 | |
2577 | if (wake_flags & WF_MIGRATED) | |
2578 | schedstat_inc(p, se.statistics.nr_wakeups_migrate); | |
2579 | ||
d7c01d27 PZ |
2580 | #endif /* CONFIG_SMP */ |
2581 | ||
2582 | schedstat_inc(rq, ttwu_count); | |
9ed3811a | 2583 | schedstat_inc(p, se.statistics.nr_wakeups); |
d7c01d27 PZ |
2584 | |
2585 | if (wake_flags & WF_SYNC) | |
9ed3811a | 2586 | schedstat_inc(p, se.statistics.nr_wakeups_sync); |
d7c01d27 | 2587 | |
d7c01d27 PZ |
2588 | #endif /* CONFIG_SCHEDSTATS */ |
2589 | } | |
2590 | ||
2591 | static void ttwu_activate(struct rq *rq, struct task_struct *p, int en_flags) | |
2592 | { | |
9ed3811a | 2593 | activate_task(rq, p, en_flags); |
fd2f4419 | 2594 | p->on_rq = 1; |
c2f7115e PZ |
2595 | |
2596 | /* if a worker is waking up, notify workqueue */ | |
2597 | if (p->flags & PF_WQ_WORKER) | |
2598 | wq_worker_waking_up(p, cpu_of(rq)); | |
9ed3811a TH |
2599 | } |
2600 | ||
23f41eeb PZ |
2601 | /* |
2602 | * Mark the task runnable and perform wakeup-preemption. | |
2603 | */ | |
89363381 | 2604 | static void |
23f41eeb | 2605 | ttwu_do_wakeup(struct rq *rq, struct task_struct *p, int wake_flags) |
9ed3811a | 2606 | { |
89363381 | 2607 | trace_sched_wakeup(p, true); |
9ed3811a TH |
2608 | check_preempt_curr(rq, p, wake_flags); |
2609 | ||
2610 | p->state = TASK_RUNNING; | |
2611 | #ifdef CONFIG_SMP | |
2612 | if (p->sched_class->task_woken) | |
2613 | p->sched_class->task_woken(rq, p); | |
2614 | ||
e69c6341 | 2615 | if (rq->idle_stamp) { |
9ed3811a TH |
2616 | u64 delta = rq->clock - rq->idle_stamp; |
2617 | u64 max = 2*sysctl_sched_migration_cost; | |
2618 | ||
2619 | if (delta > max) | |
2620 | rq->avg_idle = max; | |
2621 | else | |
2622 | update_avg(&rq->avg_idle, delta); | |
2623 | rq->idle_stamp = 0; | |
2624 | } | |
2625 | #endif | |
2626 | } | |
2627 | ||
c05fbafb PZ |
2628 | static void |
2629 | ttwu_do_activate(struct rq *rq, struct task_struct *p, int wake_flags) | |
2630 | { | |
2631 | #ifdef CONFIG_SMP | |
2632 | if (p->sched_contributes_to_load) | |
2633 | rq->nr_uninterruptible--; | |
2634 | #endif | |
2635 | ||
2636 | ttwu_activate(rq, p, ENQUEUE_WAKEUP | ENQUEUE_WAKING); | |
2637 | ttwu_do_wakeup(rq, p, wake_flags); | |
2638 | } | |
2639 | ||
2640 | /* | |
2641 | * Called in case the task @p isn't fully descheduled from its runqueue, | |
2642 | * in this case we must do a remote wakeup. Its a 'light' wakeup though, | |
2643 | * since all we need to do is flip p->state to TASK_RUNNING, since | |
2644 | * the task is still ->on_rq. | |
2645 | */ | |
2646 | static int ttwu_remote(struct task_struct *p, int wake_flags) | |
2647 | { | |
2648 | struct rq *rq; | |
2649 | int ret = 0; | |
2650 | ||
2651 | rq = __task_rq_lock(p); | |
2652 | if (p->on_rq) { | |
2653 | ttwu_do_wakeup(rq, p, wake_flags); | |
2654 | ret = 1; | |
2655 | } | |
2656 | __task_rq_unlock(rq); | |
2657 | ||
2658 | return ret; | |
2659 | } | |
2660 | ||
317f3941 | 2661 | #ifdef CONFIG_SMP |
c5d753a5 | 2662 | static void sched_ttwu_do_pending(struct task_struct *list) |
317f3941 PZ |
2663 | { |
2664 | struct rq *rq = this_rq(); | |
317f3941 PZ |
2665 | |
2666 | raw_spin_lock(&rq->lock); | |
2667 | ||
2668 | while (list) { | |
2669 | struct task_struct *p = list; | |
2670 | list = list->wake_entry; | |
2671 | ttwu_do_activate(rq, p, 0); | |
2672 | } | |
2673 | ||
2674 | raw_spin_unlock(&rq->lock); | |
2675 | } | |
2676 | ||
c5d753a5 PZ |
2677 | #ifdef CONFIG_HOTPLUG_CPU |
2678 | ||
2679 | static void sched_ttwu_pending(void) | |
2680 | { | |
2681 | struct rq *rq = this_rq(); | |
2682 | struct task_struct *list = xchg(&rq->wake_list, NULL); | |
2683 | ||
2684 | if (!list) | |
2685 | return; | |
2686 | ||
2687 | sched_ttwu_do_pending(list); | |
2688 | } | |
2689 | ||
2690 | #endif /* CONFIG_HOTPLUG_CPU */ | |
2691 | ||
317f3941 PZ |
2692 | void scheduler_ipi(void) |
2693 | { | |
c5d753a5 PZ |
2694 | struct rq *rq = this_rq(); |
2695 | struct task_struct *list = xchg(&rq->wake_list, NULL); | |
2696 | ||
2697 | if (!list) | |
2698 | return; | |
2699 | ||
2700 | /* | |
2701 | * Not all reschedule IPI handlers call irq_enter/irq_exit, since | |
2702 | * traditionally all their work was done from the interrupt return | |
2703 | * path. Now that we actually do some work, we need to make sure | |
2704 | * we do call them. | |
2705 | * | |
2706 | * Some archs already do call them, luckily irq_enter/exit nest | |
2707 | * properly. | |
2708 | * | |
2709 | * Arguably we should visit all archs and update all handlers, | |
2710 | * however a fair share of IPIs are still resched only so this would | |
2711 | * somewhat pessimize the simple resched case. | |
2712 | */ | |
2713 | irq_enter(); | |
2714 | sched_ttwu_do_pending(list); | |
2715 | irq_exit(); | |
317f3941 PZ |
2716 | } |
2717 | ||
2718 | static void ttwu_queue_remote(struct task_struct *p, int cpu) | |
2719 | { | |
2720 | struct rq *rq = cpu_rq(cpu); | |
2721 | struct task_struct *next = rq->wake_list; | |
2722 | ||
2723 | for (;;) { | |
2724 | struct task_struct *old = next; | |
2725 | ||
2726 | p->wake_entry = next; | |
2727 | next = cmpxchg(&rq->wake_list, old, p); | |
2728 | if (next == old) | |
2729 | break; | |
2730 | } | |
2731 | ||
2732 | if (!next) | |
2733 | smp_send_reschedule(cpu); | |
2734 | } | |
d6aa8f85 PZ |
2735 | |
2736 | #ifdef __ARCH_WANT_INTERRUPTS_ON_CTXSW | |
2737 | static int ttwu_activate_remote(struct task_struct *p, int wake_flags) | |
2738 | { | |
2739 | struct rq *rq; | |
2740 | int ret = 0; | |
2741 | ||
2742 | rq = __task_rq_lock(p); | |
2743 | if (p->on_cpu) { | |
2744 | ttwu_activate(rq, p, ENQUEUE_WAKEUP); | |
2745 | ttwu_do_wakeup(rq, p, wake_flags); | |
2746 | ret = 1; | |
2747 | } | |
2748 | __task_rq_unlock(rq); | |
2749 | ||
2750 | return ret; | |
2751 | ||
2752 | } | |
2753 | #endif /* __ARCH_WANT_INTERRUPTS_ON_CTXSW */ | |
2754 | #endif /* CONFIG_SMP */ | |
317f3941 | 2755 | |
c05fbafb PZ |
2756 | static void ttwu_queue(struct task_struct *p, int cpu) |
2757 | { | |
2758 | struct rq *rq = cpu_rq(cpu); | |
2759 | ||
17d9f311 | 2760 | #if defined(CONFIG_SMP) |
317f3941 | 2761 | if (sched_feat(TTWU_QUEUE) && cpu != smp_processor_id()) { |
f01114cb | 2762 | sched_clock_cpu(cpu); /* sync clocks x-cpu */ |
317f3941 PZ |
2763 | ttwu_queue_remote(p, cpu); |
2764 | return; | |
2765 | } | |
2766 | #endif | |
2767 | ||
c05fbafb PZ |
2768 | raw_spin_lock(&rq->lock); |
2769 | ttwu_do_activate(rq, p, 0); | |
2770 | raw_spin_unlock(&rq->lock); | |
9ed3811a TH |
2771 | } |
2772 | ||
2773 | /** | |
1da177e4 | 2774 | * try_to_wake_up - wake up a thread |
9ed3811a | 2775 | * @p: the thread to be awakened |
1da177e4 | 2776 | * @state: the mask of task states that can be woken |
9ed3811a | 2777 | * @wake_flags: wake modifier flags (WF_*) |
1da177e4 LT |
2778 | * |
2779 | * Put it on the run-queue if it's not already there. The "current" | |
2780 | * thread is always on the run-queue (except when the actual | |
2781 | * re-schedule is in progress), and as such you're allowed to do | |
2782 | * the simpler "current->state = TASK_RUNNING" to mark yourself | |
2783 | * runnable without the overhead of this. | |
2784 | * | |
9ed3811a TH |
2785 | * Returns %true if @p was woken up, %false if it was already running |
2786 | * or @state didn't match @p's state. | |
1da177e4 | 2787 | */ |
e4a52bcb PZ |
2788 | static int |
2789 | try_to_wake_up(struct task_struct *p, unsigned int state, int wake_flags) | |
1da177e4 | 2790 | { |
1da177e4 | 2791 | unsigned long flags; |
c05fbafb | 2792 | int cpu, success = 0; |
2398f2c6 | 2793 | |
04e2f174 | 2794 | smp_wmb(); |
013fdb80 | 2795 | raw_spin_lock_irqsave(&p->pi_lock, flags); |
e9c84311 | 2796 | if (!(p->state & state)) |
1da177e4 LT |
2797 | goto out; |
2798 | ||
c05fbafb | 2799 | success = 1; /* we're going to change ->state */ |
1da177e4 | 2800 | cpu = task_cpu(p); |
1da177e4 | 2801 | |
c05fbafb PZ |
2802 | if (p->on_rq && ttwu_remote(p, wake_flags)) |
2803 | goto stat; | |
1da177e4 | 2804 | |
1da177e4 | 2805 | #ifdef CONFIG_SMP |
e9c84311 | 2806 | /* |
c05fbafb PZ |
2807 | * If the owning (remote) cpu is still in the middle of schedule() with |
2808 | * this task as prev, wait until its done referencing the task. | |
e9c84311 | 2809 | */ |
e4a52bcb PZ |
2810 | while (p->on_cpu) { |
2811 | #ifdef __ARCH_WANT_INTERRUPTS_ON_CTXSW | |
2812 | /* | |
d6aa8f85 PZ |
2813 | * In case the architecture enables interrupts in |
2814 | * context_switch(), we cannot busy wait, since that | |
2815 | * would lead to deadlocks when an interrupt hits and | |
2816 | * tries to wake up @prev. So bail and do a complete | |
2817 | * remote wakeup. | |
e4a52bcb | 2818 | */ |
d6aa8f85 | 2819 | if (ttwu_activate_remote(p, wake_flags)) |
c05fbafb | 2820 | goto stat; |
d6aa8f85 | 2821 | #else |
e4a52bcb | 2822 | cpu_relax(); |
d6aa8f85 | 2823 | #endif |
371fd7e7 | 2824 | } |
0970d299 | 2825 | /* |
e4a52bcb | 2826 | * Pairs with the smp_wmb() in finish_lock_switch(). |
0970d299 | 2827 | */ |
e4a52bcb | 2828 | smp_rmb(); |
1da177e4 | 2829 | |
a8e4f2ea | 2830 | p->sched_contributes_to_load = !!task_contributes_to_load(p); |
e9c84311 | 2831 | p->state = TASK_WAKING; |
e7693a36 | 2832 | |
e4a52bcb | 2833 | if (p->sched_class->task_waking) |
74f8e4b2 | 2834 | p->sched_class->task_waking(p); |
efbbd05a | 2835 | |
7608dec2 | 2836 | cpu = select_task_rq(p, SD_BALANCE_WAKE, wake_flags); |
f339b9dc PZ |
2837 | if (task_cpu(p) != cpu) { |
2838 | wake_flags |= WF_MIGRATED; | |
e4a52bcb | 2839 | set_task_cpu(p, cpu); |
f339b9dc | 2840 | } |
1da177e4 | 2841 | #endif /* CONFIG_SMP */ |
1da177e4 | 2842 | |
c05fbafb PZ |
2843 | ttwu_queue(p, cpu); |
2844 | stat: | |
b84cb5df | 2845 | ttwu_stat(p, cpu, wake_flags); |
1da177e4 | 2846 | out: |
013fdb80 | 2847 | raw_spin_unlock_irqrestore(&p->pi_lock, flags); |
1da177e4 LT |
2848 | |
2849 | return success; | |
2850 | } | |
2851 | ||
21aa9af0 TH |
2852 | /** |
2853 | * try_to_wake_up_local - try to wake up a local task with rq lock held | |
2854 | * @p: the thread to be awakened | |
2855 | * | |
2acca55e | 2856 | * Put @p on the run-queue if it's not already there. The caller must |
21aa9af0 | 2857 | * ensure that this_rq() is locked, @p is bound to this_rq() and not |
2acca55e | 2858 | * the current task. |
21aa9af0 TH |
2859 | */ |
2860 | static void try_to_wake_up_local(struct task_struct *p) | |
2861 | { | |
2862 | struct rq *rq = task_rq(p); | |
21aa9af0 TH |
2863 | |
2864 | BUG_ON(rq != this_rq()); | |
2865 | BUG_ON(p == current); | |
2866 | lockdep_assert_held(&rq->lock); | |
2867 | ||
2acca55e PZ |
2868 | if (!raw_spin_trylock(&p->pi_lock)) { |
2869 | raw_spin_unlock(&rq->lock); | |
2870 | raw_spin_lock(&p->pi_lock); | |
2871 | raw_spin_lock(&rq->lock); | |
2872 | } | |
2873 | ||
21aa9af0 | 2874 | if (!(p->state & TASK_NORMAL)) |
2acca55e | 2875 | goto out; |
21aa9af0 | 2876 | |
fd2f4419 | 2877 | if (!p->on_rq) |
d7c01d27 PZ |
2878 | ttwu_activate(rq, p, ENQUEUE_WAKEUP); |
2879 | ||
23f41eeb | 2880 | ttwu_do_wakeup(rq, p, 0); |
b84cb5df | 2881 | ttwu_stat(p, smp_processor_id(), 0); |
2acca55e PZ |
2882 | out: |
2883 | raw_spin_unlock(&p->pi_lock); | |
21aa9af0 TH |
2884 | } |
2885 | ||
50fa610a DH |
2886 | /** |
2887 | * wake_up_process - Wake up a specific process | |
2888 | * @p: The process to be woken up. | |
2889 | * | |
2890 | * Attempt to wake up the nominated process and move it to the set of runnable | |
2891 | * processes. Returns 1 if the process was woken up, 0 if it was already | |
2892 | * running. | |
2893 | * | |
2894 | * It may be assumed that this function implies a write memory barrier before | |
2895 | * changing the task state if and only if any tasks are woken up. | |
2896 | */ | |
7ad5b3a5 | 2897 | int wake_up_process(struct task_struct *p) |
1da177e4 | 2898 | { |
d9514f6c | 2899 | return try_to_wake_up(p, TASK_ALL, 0); |
1da177e4 | 2900 | } |
1da177e4 LT |
2901 | EXPORT_SYMBOL(wake_up_process); |
2902 | ||
7ad5b3a5 | 2903 | int wake_up_state(struct task_struct *p, unsigned int state) |
1da177e4 LT |
2904 | { |
2905 | return try_to_wake_up(p, state, 0); | |
2906 | } | |
2907 | ||
1da177e4 LT |
2908 | /* |
2909 | * Perform scheduler related setup for a newly forked process p. | |
2910 | * p is forked by current. | |
dd41f596 IM |
2911 | * |
2912 | * __sched_fork() is basic setup used by init_idle() too: | |
2913 | */ | |
2914 | static void __sched_fork(struct task_struct *p) | |
2915 | { | |
fd2f4419 PZ |
2916 | p->on_rq = 0; |
2917 | ||
2918 | p->se.on_rq = 0; | |
dd41f596 IM |
2919 | p->se.exec_start = 0; |
2920 | p->se.sum_exec_runtime = 0; | |
f6cf891c | 2921 | p->se.prev_sum_exec_runtime = 0; |
6c594c21 | 2922 | p->se.nr_migrations = 0; |
da7a735e | 2923 | p->se.vruntime = 0; |
fd2f4419 | 2924 | INIT_LIST_HEAD(&p->se.group_node); |
6cfb0d5d IM |
2925 | |
2926 | #ifdef CONFIG_SCHEDSTATS | |
41acab88 | 2927 | memset(&p->se.statistics, 0, sizeof(p->se.statistics)); |
6cfb0d5d | 2928 | #endif |
476d139c | 2929 | |
fa717060 | 2930 | INIT_LIST_HEAD(&p->rt.run_list); |
476d139c | 2931 | |
e107be36 AK |
2932 | #ifdef CONFIG_PREEMPT_NOTIFIERS |
2933 | INIT_HLIST_HEAD(&p->preempt_notifiers); | |
2934 | #endif | |
dd41f596 IM |
2935 | } |
2936 | ||
2937 | /* | |
2938 | * fork()/clone()-time setup: | |
2939 | */ | |
3e51e3ed | 2940 | void sched_fork(struct task_struct *p) |
dd41f596 | 2941 | { |
0122ec5b | 2942 | unsigned long flags; |
dd41f596 IM |
2943 | int cpu = get_cpu(); |
2944 | ||
2945 | __sched_fork(p); | |
06b83b5f | 2946 | /* |
0017d735 | 2947 | * We mark the process as running here. This guarantees that |
06b83b5f PZ |
2948 | * nobody will actually run it, and a signal or other external |
2949 | * event cannot wake it up and insert it on the runqueue either. | |
2950 | */ | |
0017d735 | 2951 | p->state = TASK_RUNNING; |
dd41f596 | 2952 | |
c350a04e MG |
2953 | /* |
2954 | * Make sure we do not leak PI boosting priority to the child. | |
2955 | */ | |
2956 | p->prio = current->normal_prio; | |
2957 | ||
b9dc29e7 MG |
2958 | /* |
2959 | * Revert to default priority/policy on fork if requested. | |
2960 | */ | |
2961 | if (unlikely(p->sched_reset_on_fork)) { | |
c350a04e | 2962 | if (task_has_rt_policy(p)) { |
b9dc29e7 | 2963 | p->policy = SCHED_NORMAL; |
6c697bdf | 2964 | p->static_prio = NICE_TO_PRIO(0); |
c350a04e MG |
2965 | p->rt_priority = 0; |
2966 | } else if (PRIO_TO_NICE(p->static_prio) < 0) | |
2967 | p->static_prio = NICE_TO_PRIO(0); | |
2968 | ||
2969 | p->prio = p->normal_prio = __normal_prio(p); | |
2970 | set_load_weight(p); | |
6c697bdf | 2971 | |
b9dc29e7 MG |
2972 | /* |
2973 | * We don't need the reset flag anymore after the fork. It has | |
2974 | * fulfilled its duty: | |
2975 | */ | |
2976 | p->sched_reset_on_fork = 0; | |
2977 | } | |
ca94c442 | 2978 | |
2ddbf952 HS |
2979 | if (!rt_prio(p->prio)) |
2980 | p->sched_class = &fair_sched_class; | |
b29739f9 | 2981 | |
cd29fe6f PZ |
2982 | if (p->sched_class->task_fork) |
2983 | p->sched_class->task_fork(p); | |
2984 | ||
86951599 PZ |
2985 | /* |
2986 | * The child is not yet in the pid-hash so no cgroup attach races, | |
2987 | * and the cgroup is pinned to this child due to cgroup_fork() | |
2988 | * is ran before sched_fork(). | |
2989 | * | |
2990 | * Silence PROVE_RCU. | |
2991 | */ | |
0122ec5b | 2992 | raw_spin_lock_irqsave(&p->pi_lock, flags); |
5f3edc1b | 2993 | set_task_cpu(p, cpu); |
0122ec5b | 2994 | raw_spin_unlock_irqrestore(&p->pi_lock, flags); |
5f3edc1b | 2995 | |
52f17b6c | 2996 | #if defined(CONFIG_SCHEDSTATS) || defined(CONFIG_TASK_DELAY_ACCT) |
dd41f596 | 2997 | if (likely(sched_info_on())) |
52f17b6c | 2998 | memset(&p->sched_info, 0, sizeof(p->sched_info)); |
1da177e4 | 2999 | #endif |
3ca7a440 PZ |
3000 | #if defined(CONFIG_SMP) |
3001 | p->on_cpu = 0; | |
4866cde0 | 3002 | #endif |
bdd4e85d | 3003 | #ifdef CONFIG_PREEMPT_COUNT |
4866cde0 | 3004 | /* Want to start with kernel preemption disabled. */ |
a1261f54 | 3005 | task_thread_info(p)->preempt_count = 1; |
1da177e4 | 3006 | #endif |
806c09a7 | 3007 | #ifdef CONFIG_SMP |
917b627d | 3008 | plist_node_init(&p->pushable_tasks, MAX_PRIO); |
806c09a7 | 3009 | #endif |
917b627d | 3010 | |
476d139c | 3011 | put_cpu(); |
1da177e4 LT |
3012 | } |
3013 | ||
3014 | /* | |
3015 | * wake_up_new_task - wake up a newly created task for the first time. | |
3016 | * | |
3017 | * This function will do some initial scheduler statistics housekeeping | |
3018 | * that must be done for every newly created context, then puts the task | |
3019 | * on the runqueue and wakes it. | |
3020 | */ | |
3e51e3ed | 3021 | void wake_up_new_task(struct task_struct *p) |
1da177e4 LT |
3022 | { |
3023 | unsigned long flags; | |
dd41f596 | 3024 | struct rq *rq; |
fabf318e | 3025 | |
ab2515c4 | 3026 | raw_spin_lock_irqsave(&p->pi_lock, flags); |
fabf318e PZ |
3027 | #ifdef CONFIG_SMP |
3028 | /* | |
3029 | * Fork balancing, do it here and not earlier because: | |
3030 | * - cpus_allowed can change in the fork path | |
3031 | * - any previously selected cpu might disappear through hotplug | |
fabf318e | 3032 | */ |
ab2515c4 | 3033 | set_task_cpu(p, select_task_rq(p, SD_BALANCE_FORK, 0)); |
0017d735 PZ |
3034 | #endif |
3035 | ||
ab2515c4 | 3036 | rq = __task_rq_lock(p); |
cd29fe6f | 3037 | activate_task(rq, p, 0); |
fd2f4419 | 3038 | p->on_rq = 1; |
89363381 | 3039 | trace_sched_wakeup_new(p, true); |
a7558e01 | 3040 | check_preempt_curr(rq, p, WF_FORK); |
9a897c5a | 3041 | #ifdef CONFIG_SMP |
efbbd05a PZ |
3042 | if (p->sched_class->task_woken) |
3043 | p->sched_class->task_woken(rq, p); | |
9a897c5a | 3044 | #endif |
0122ec5b | 3045 | task_rq_unlock(rq, p, &flags); |
1da177e4 LT |
3046 | } |
3047 | ||
e107be36 AK |
3048 | #ifdef CONFIG_PREEMPT_NOTIFIERS |
3049 | ||
3050 | /** | |
80dd99b3 | 3051 | * preempt_notifier_register - tell me when current is being preempted & rescheduled |
421cee29 | 3052 | * @notifier: notifier struct to register |
e107be36 AK |
3053 | */ |
3054 | void preempt_notifier_register(struct preempt_notifier *notifier) | |
3055 | { | |
3056 | hlist_add_head(¬ifier->link, ¤t->preempt_notifiers); | |
3057 | } | |
3058 | EXPORT_SYMBOL_GPL(preempt_notifier_register); | |
3059 | ||
3060 | /** | |
3061 | * preempt_notifier_unregister - no longer interested in preemption notifications | |
421cee29 | 3062 | * @notifier: notifier struct to unregister |
e107be36 AK |
3063 | * |
3064 | * This is safe to call from within a preemption notifier. | |
3065 | */ | |
3066 | void preempt_notifier_unregister(struct preempt_notifier *notifier) | |
3067 | { | |
3068 | hlist_del(¬ifier->link); | |
3069 | } | |
3070 | EXPORT_SYMBOL_GPL(preempt_notifier_unregister); | |
3071 | ||
3072 | static void fire_sched_in_preempt_notifiers(struct task_struct *curr) | |
3073 | { | |
3074 | struct preempt_notifier *notifier; | |
3075 | struct hlist_node *node; | |
3076 | ||
3077 | hlist_for_each_entry(notifier, node, &curr->preempt_notifiers, link) | |
3078 | notifier->ops->sched_in(notifier, raw_smp_processor_id()); | |
3079 | } | |
3080 | ||
3081 | static void | |
3082 | fire_sched_out_preempt_notifiers(struct task_struct *curr, | |
3083 | struct task_struct *next) | |
3084 | { | |
3085 | struct preempt_notifier *notifier; | |
3086 | struct hlist_node *node; | |
3087 | ||
3088 | hlist_for_each_entry(notifier, node, &curr->preempt_notifiers, link) | |
3089 | notifier->ops->sched_out(notifier, next); | |
3090 | } | |
3091 | ||
6d6bc0ad | 3092 | #else /* !CONFIG_PREEMPT_NOTIFIERS */ |
e107be36 AK |
3093 | |
3094 | static void fire_sched_in_preempt_notifiers(struct task_struct *curr) | |
3095 | { | |
3096 | } | |
3097 | ||
3098 | static void | |
3099 | fire_sched_out_preempt_notifiers(struct task_struct *curr, | |
3100 | struct task_struct *next) | |
3101 | { | |
3102 | } | |
3103 | ||
6d6bc0ad | 3104 | #endif /* CONFIG_PREEMPT_NOTIFIERS */ |
e107be36 | 3105 | |
4866cde0 NP |
3106 | /** |
3107 | * prepare_task_switch - prepare to switch tasks | |
3108 | * @rq: the runqueue preparing to switch | |
421cee29 | 3109 | * @prev: the current task that is being switched out |
4866cde0 NP |
3110 | * @next: the task we are going to switch to. |
3111 | * | |
3112 | * This is called with the rq lock held and interrupts off. It must | |
3113 | * be paired with a subsequent finish_task_switch after the context | |
3114 | * switch. | |
3115 | * | |
3116 | * prepare_task_switch sets up locking and calls architecture specific | |
3117 | * hooks. | |
3118 | */ | |
e107be36 AK |
3119 | static inline void |
3120 | prepare_task_switch(struct rq *rq, struct task_struct *prev, | |
3121 | struct task_struct *next) | |
4866cde0 | 3122 | { |
fe4b04fa PZ |
3123 | sched_info_switch(prev, next); |
3124 | perf_event_task_sched_out(prev, next); | |
e107be36 | 3125 | fire_sched_out_preempt_notifiers(prev, next); |
4866cde0 NP |
3126 | prepare_lock_switch(rq, next); |
3127 | prepare_arch_switch(next); | |
fe4b04fa | 3128 | trace_sched_switch(prev, next); |
4866cde0 NP |
3129 | } |
3130 | ||
1da177e4 LT |
3131 | /** |
3132 | * finish_task_switch - clean up after a task-switch | |
344babaa | 3133 | * @rq: runqueue associated with task-switch |
1da177e4 LT |
3134 | * @prev: the thread we just switched away from. |
3135 | * | |
4866cde0 NP |
3136 | * finish_task_switch must be called after the context switch, paired |
3137 | * with a prepare_task_switch call before the context switch. | |
3138 | * finish_task_switch will reconcile locking set up by prepare_task_switch, | |
3139 | * and do any other architecture-specific cleanup actions. | |
1da177e4 LT |
3140 | * |
3141 | * Note that we may have delayed dropping an mm in context_switch(). If | |
41a2d6cf | 3142 | * so, we finish that here outside of the runqueue lock. (Doing it |
1da177e4 LT |
3143 | * with the lock held can cause deadlocks; see schedule() for |
3144 | * details.) | |
3145 | */ | |
a9957449 | 3146 | static void finish_task_switch(struct rq *rq, struct task_struct *prev) |
1da177e4 LT |
3147 | __releases(rq->lock) |
3148 | { | |
1da177e4 | 3149 | struct mm_struct *mm = rq->prev_mm; |
55a101f8 | 3150 | long prev_state; |
1da177e4 LT |
3151 | |
3152 | rq->prev_mm = NULL; | |
3153 | ||
3154 | /* | |
3155 | * A task struct has one reference for the use as "current". | |
c394cc9f | 3156 | * If a task dies, then it sets TASK_DEAD in tsk->state and calls |
55a101f8 ON |
3157 | * schedule one last time. The schedule call will never return, and |
3158 | * the scheduled task must drop that reference. | |
c394cc9f | 3159 | * The test for TASK_DEAD must occur while the runqueue locks are |
1da177e4 LT |
3160 | * still held, otherwise prev could be scheduled on another cpu, die |
3161 | * there before we look at prev->state, and then the reference would | |
3162 | * be dropped twice. | |
3163 | * Manfred Spraul <manfred@colorfullife.com> | |
3164 | */ | |
55a101f8 | 3165 | prev_state = prev->state; |
4866cde0 | 3166 | finish_arch_switch(prev); |
8381f65d JI |
3167 | #ifdef __ARCH_WANT_INTERRUPTS_ON_CTXSW |
3168 | local_irq_disable(); | |
3169 | #endif /* __ARCH_WANT_INTERRUPTS_ON_CTXSW */ | |
49f47433 | 3170 | perf_event_task_sched_in(current); |
8381f65d JI |
3171 | #ifdef __ARCH_WANT_INTERRUPTS_ON_CTXSW |
3172 | local_irq_enable(); | |
3173 | #endif /* __ARCH_WANT_INTERRUPTS_ON_CTXSW */ | |
4866cde0 | 3174 | finish_lock_switch(rq, prev); |
e8fa1362 | 3175 | |
e107be36 | 3176 | fire_sched_in_preempt_notifiers(current); |
1da177e4 LT |
3177 | if (mm) |
3178 | mmdrop(mm); | |
c394cc9f | 3179 | if (unlikely(prev_state == TASK_DEAD)) { |
c6fd91f0 | 3180 | /* |
3181 | * Remove function-return probe instances associated with this | |
3182 | * task and put them back on the free list. | |
9761eea8 | 3183 | */ |
c6fd91f0 | 3184 | kprobe_flush_task(prev); |
1da177e4 | 3185 | put_task_struct(prev); |
c6fd91f0 | 3186 | } |
1da177e4 LT |
3187 | } |
3188 | ||
3f029d3c GH |
3189 | #ifdef CONFIG_SMP |
3190 | ||
3191 | /* assumes rq->lock is held */ | |
3192 | static inline void pre_schedule(struct rq *rq, struct task_struct *prev) | |
3193 | { | |
3194 | if (prev->sched_class->pre_schedule) | |
3195 | prev->sched_class->pre_schedule(rq, prev); | |
3196 | } | |
3197 | ||
3198 | /* rq->lock is NOT held, but preemption is disabled */ | |
3199 | static inline void post_schedule(struct rq *rq) | |
3200 | { | |
3201 | if (rq->post_schedule) { | |
3202 | unsigned long flags; | |
3203 | ||
05fa785c | 3204 | raw_spin_lock_irqsave(&rq->lock, flags); |
3f029d3c GH |
3205 | if (rq->curr->sched_class->post_schedule) |
3206 | rq->curr->sched_class->post_schedule(rq); | |
05fa785c | 3207 | raw_spin_unlock_irqrestore(&rq->lock, flags); |
3f029d3c GH |
3208 | |
3209 | rq->post_schedule = 0; | |
3210 | } | |
3211 | } | |
3212 | ||
3213 | #else | |
da19ab51 | 3214 | |
3f029d3c GH |
3215 | static inline void pre_schedule(struct rq *rq, struct task_struct *p) |
3216 | { | |
3217 | } | |
3218 | ||
3219 | static inline void post_schedule(struct rq *rq) | |
3220 | { | |
1da177e4 LT |
3221 | } |
3222 | ||
3f029d3c GH |
3223 | #endif |
3224 | ||
1da177e4 LT |
3225 | /** |
3226 | * schedule_tail - first thing a freshly forked thread must call. | |
3227 | * @prev: the thread we just switched away from. | |
3228 | */ | |
36c8b586 | 3229 | asmlinkage void schedule_tail(struct task_struct *prev) |
1da177e4 LT |
3230 | __releases(rq->lock) |
3231 | { | |
70b97a7f IM |
3232 | struct rq *rq = this_rq(); |
3233 | ||
4866cde0 | 3234 | finish_task_switch(rq, prev); |
da19ab51 | 3235 | |
3f029d3c GH |
3236 | /* |
3237 | * FIXME: do we need to worry about rq being invalidated by the | |
3238 | * task_switch? | |
3239 | */ | |
3240 | post_schedule(rq); | |
70b97a7f | 3241 | |
4866cde0 NP |
3242 | #ifdef __ARCH_WANT_UNLOCKED_CTXSW |
3243 | /* In this case, finish_task_switch does not reenable preemption */ | |
3244 | preempt_enable(); | |
3245 | #endif | |
1da177e4 | 3246 | if (current->set_child_tid) |
b488893a | 3247 | put_user(task_pid_vnr(current), current->set_child_tid); |
1da177e4 LT |
3248 | } |
3249 | ||
3250 | /* | |
3251 | * context_switch - switch to the new MM and the new | |
3252 | * thread's register state. | |
3253 | */ | |
dd41f596 | 3254 | static inline void |
70b97a7f | 3255 | context_switch(struct rq *rq, struct task_struct *prev, |
36c8b586 | 3256 | struct task_struct *next) |
1da177e4 | 3257 | { |
dd41f596 | 3258 | struct mm_struct *mm, *oldmm; |
1da177e4 | 3259 | |
e107be36 | 3260 | prepare_task_switch(rq, prev, next); |
fe4b04fa | 3261 | |
dd41f596 IM |
3262 | mm = next->mm; |
3263 | oldmm = prev->active_mm; | |
9226d125 ZA |
3264 | /* |
3265 | * For paravirt, this is coupled with an exit in switch_to to | |
3266 | * combine the page table reload and the switch backend into | |
3267 | * one hypercall. | |
3268 | */ | |
224101ed | 3269 | arch_start_context_switch(prev); |
9226d125 | 3270 | |
31915ab4 | 3271 | if (!mm) { |
1da177e4 LT |
3272 | next->active_mm = oldmm; |
3273 | atomic_inc(&oldmm->mm_count); | |
3274 | enter_lazy_tlb(oldmm, next); | |
3275 | } else | |
3276 | switch_mm(oldmm, mm, next); | |
3277 | ||
31915ab4 | 3278 | if (!prev->mm) { |
1da177e4 | 3279 | prev->active_mm = NULL; |
1da177e4 LT |
3280 | rq->prev_mm = oldmm; |
3281 | } | |
3a5f5e48 IM |
3282 | /* |
3283 | * Since the runqueue lock will be released by the next | |
3284 | * task (which is an invalid locking op but in the case | |
3285 | * of the scheduler it's an obvious special-case), so we | |
3286 | * do an early lockdep release here: | |
3287 | */ | |
3288 | #ifndef __ARCH_WANT_UNLOCKED_CTXSW | |
8a25d5de | 3289 | spin_release(&rq->lock.dep_map, 1, _THIS_IP_); |
3a5f5e48 | 3290 | #endif |
1da177e4 LT |
3291 | |
3292 | /* Here we just switch the register state and the stack. */ | |
3293 | switch_to(prev, next, prev); | |
3294 | ||
dd41f596 IM |
3295 | barrier(); |
3296 | /* | |
3297 | * this_rq must be evaluated again because prev may have moved | |
3298 | * CPUs since it called schedule(), thus the 'rq' on its stack | |
3299 | * frame will be invalid. | |
3300 | */ | |
3301 | finish_task_switch(this_rq(), prev); | |
1da177e4 LT |
3302 | } |
3303 | ||
3304 | /* | |
3305 | * nr_running, nr_uninterruptible and nr_context_switches: | |
3306 | * | |
3307 | * externally visible scheduler statistics: current number of runnable | |
3308 | * threads, current number of uninterruptible-sleeping threads, total | |
3309 | * number of context switches performed since bootup. | |
3310 | */ | |
3311 | unsigned long nr_running(void) | |
3312 | { | |
3313 | unsigned long i, sum = 0; | |
3314 | ||
3315 | for_each_online_cpu(i) | |
3316 | sum += cpu_rq(i)->nr_running; | |
3317 | ||
3318 | return sum; | |
f711f609 | 3319 | } |
1da177e4 LT |
3320 | |
3321 | unsigned long nr_uninterruptible(void) | |
f711f609 | 3322 | { |
1da177e4 | 3323 | unsigned long i, sum = 0; |
f711f609 | 3324 | |
0a945022 | 3325 | for_each_possible_cpu(i) |
1da177e4 | 3326 | sum += cpu_rq(i)->nr_uninterruptible; |
f711f609 GS |
3327 | |
3328 | /* | |
1da177e4 LT |
3329 | * Since we read the counters lockless, it might be slightly |
3330 | * inaccurate. Do not allow it to go below zero though: | |
f711f609 | 3331 | */ |
1da177e4 LT |
3332 | if (unlikely((long)sum < 0)) |
3333 | sum = 0; | |
f711f609 | 3334 | |
1da177e4 | 3335 | return sum; |
f711f609 | 3336 | } |
f711f609 | 3337 | |
1da177e4 | 3338 | unsigned long long nr_context_switches(void) |
46cb4b7c | 3339 | { |
cc94abfc SR |
3340 | int i; |
3341 | unsigned long long sum = 0; | |
46cb4b7c | 3342 | |
0a945022 | 3343 | for_each_possible_cpu(i) |
1da177e4 | 3344 | sum += cpu_rq(i)->nr_switches; |
46cb4b7c | 3345 | |
1da177e4 LT |
3346 | return sum; |
3347 | } | |
483b4ee6 | 3348 | |
1da177e4 LT |
3349 | unsigned long nr_iowait(void) |
3350 | { | |
3351 | unsigned long i, sum = 0; | |
483b4ee6 | 3352 | |
0a945022 | 3353 | for_each_possible_cpu(i) |
1da177e4 | 3354 | sum += atomic_read(&cpu_rq(i)->nr_iowait); |
46cb4b7c | 3355 | |
1da177e4 LT |
3356 | return sum; |
3357 | } | |
483b4ee6 | 3358 | |
8c215bd3 | 3359 | unsigned long nr_iowait_cpu(int cpu) |
69d25870 | 3360 | { |
8c215bd3 | 3361 | struct rq *this = cpu_rq(cpu); |
69d25870 AV |
3362 | return atomic_read(&this->nr_iowait); |
3363 | } | |
46cb4b7c | 3364 | |
69d25870 AV |
3365 | unsigned long this_cpu_load(void) |
3366 | { | |
3367 | struct rq *this = this_rq(); | |
3368 | return this->cpu_load[0]; | |
3369 | } | |
e790fb0b | 3370 | |
46cb4b7c | 3371 | |
dce48a84 TG |
3372 | /* Variables and functions for calc_load */ |
3373 | static atomic_long_t calc_load_tasks; | |
3374 | static unsigned long calc_load_update; | |
3375 | unsigned long avenrun[3]; | |
3376 | EXPORT_SYMBOL(avenrun); | |
46cb4b7c | 3377 | |
74f5187a PZ |
3378 | static long calc_load_fold_active(struct rq *this_rq) |
3379 | { | |
3380 | long nr_active, delta = 0; | |
3381 | ||
3382 | nr_active = this_rq->nr_running; | |
3383 | nr_active += (long) this_rq->nr_uninterruptible; | |
3384 | ||
3385 | if (nr_active != this_rq->calc_load_active) { | |
3386 | delta = nr_active - this_rq->calc_load_active; | |
3387 | this_rq->calc_load_active = nr_active; | |
3388 | } | |
3389 | ||
3390 | return delta; | |
3391 | } | |
3392 | ||
0f004f5a PZ |
3393 | static unsigned long |
3394 | calc_load(unsigned long load, unsigned long exp, unsigned long active) | |
3395 | { | |
3396 | load *= exp; | |
3397 | load += active * (FIXED_1 - exp); | |
3398 | load += 1UL << (FSHIFT - 1); | |
3399 | return load >> FSHIFT; | |
3400 | } | |
3401 | ||
74f5187a PZ |
3402 | #ifdef CONFIG_NO_HZ |
3403 | /* | |
3404 | * For NO_HZ we delay the active fold to the next LOAD_FREQ update. | |
3405 | * | |
3406 | * When making the ILB scale, we should try to pull this in as well. | |
3407 | */ | |
3408 | static atomic_long_t calc_load_tasks_idle; | |
3409 | ||
3410 | static void calc_load_account_idle(struct rq *this_rq) | |
3411 | { | |
3412 | long delta; | |
3413 | ||
3414 | delta = calc_load_fold_active(this_rq); | |
3415 | if (delta) | |
3416 | atomic_long_add(delta, &calc_load_tasks_idle); | |
3417 | } | |
3418 | ||
3419 | static long calc_load_fold_idle(void) | |
3420 | { | |
3421 | long delta = 0; | |
3422 | ||
3423 | /* | |
3424 | * Its got a race, we don't care... | |
3425 | */ | |
3426 | if (atomic_long_read(&calc_load_tasks_idle)) | |
3427 | delta = atomic_long_xchg(&calc_load_tasks_idle, 0); | |
3428 | ||
3429 | return delta; | |
3430 | } | |
0f004f5a PZ |
3431 | |
3432 | /** | |
3433 | * fixed_power_int - compute: x^n, in O(log n) time | |
3434 | * | |
3435 | * @x: base of the power | |
3436 | * @frac_bits: fractional bits of @x | |
3437 | * @n: power to raise @x to. | |
3438 | * | |
3439 | * By exploiting the relation between the definition of the natural power | |
3440 | * function: x^n := x*x*...*x (x multiplied by itself for n times), and | |
3441 | * the binary encoding of numbers used by computers: n := \Sum n_i * 2^i, | |
3442 | * (where: n_i \elem {0, 1}, the binary vector representing n), | |
3443 | * we find: x^n := x^(\Sum n_i * 2^i) := \Prod x^(n_i * 2^i), which is | |
3444 | * of course trivially computable in O(log_2 n), the length of our binary | |
3445 | * vector. | |
3446 | */ | |
3447 | static unsigned long | |
3448 | fixed_power_int(unsigned long x, unsigned int frac_bits, unsigned int n) | |
3449 | { | |
3450 | unsigned long result = 1UL << frac_bits; | |
3451 | ||
3452 | if (n) for (;;) { | |
3453 | if (n & 1) { | |
3454 | result *= x; | |
3455 | result += 1UL << (frac_bits - 1); | |
3456 | result >>= frac_bits; | |
3457 | } | |
3458 | n >>= 1; | |
3459 | if (!n) | |
3460 | break; | |
3461 | x *= x; | |
3462 | x += 1UL << (frac_bits - 1); | |
3463 | x >>= frac_bits; | |
3464 | } | |
3465 | ||
3466 | return result; | |
3467 | } | |
3468 | ||
3469 | /* | |
3470 | * a1 = a0 * e + a * (1 - e) | |
3471 | * | |
3472 | * a2 = a1 * e + a * (1 - e) | |
3473 | * = (a0 * e + a * (1 - e)) * e + a * (1 - e) | |
3474 | * = a0 * e^2 + a * (1 - e) * (1 + e) | |
3475 | * | |
3476 | * a3 = a2 * e + a * (1 - e) | |
3477 | * = (a0 * e^2 + a * (1 - e) * (1 + e)) * e + a * (1 - e) | |
3478 | * = a0 * e^3 + a * (1 - e) * (1 + e + e^2) | |
3479 | * | |
3480 | * ... | |
3481 | * | |
3482 | * an = a0 * e^n + a * (1 - e) * (1 + e + ... + e^n-1) [1] | |
3483 | * = a0 * e^n + a * (1 - e) * (1 - e^n)/(1 - e) | |
3484 | * = a0 * e^n + a * (1 - e^n) | |
3485 | * | |
3486 | * [1] application of the geometric series: | |
3487 | * | |
3488 | * n 1 - x^(n+1) | |
3489 | * S_n := \Sum x^i = ------------- | |
3490 | * i=0 1 - x | |
3491 | */ | |
3492 | static unsigned long | |
3493 | calc_load_n(unsigned long load, unsigned long exp, | |
3494 | unsigned long active, unsigned int n) | |
3495 | { | |
3496 | ||
3497 | return calc_load(load, fixed_power_int(exp, FSHIFT, n), active); | |
3498 | } | |
3499 | ||
3500 | /* | |
3501 | * NO_HZ can leave us missing all per-cpu ticks calling | |
3502 | * calc_load_account_active(), but since an idle CPU folds its delta into | |
3503 | * calc_load_tasks_idle per calc_load_account_idle(), all we need to do is fold | |
3504 | * in the pending idle delta if our idle period crossed a load cycle boundary. | |
3505 | * | |
3506 | * Once we've updated the global active value, we need to apply the exponential | |
3507 | * weights adjusted to the number of cycles missed. | |
3508 | */ | |
3509 | static void calc_global_nohz(unsigned long ticks) | |
3510 | { | |
3511 | long delta, active, n; | |
3512 | ||
3513 | if (time_before(jiffies, calc_load_update)) | |
3514 | return; | |
3515 | ||
3516 | /* | |
3517 | * If we crossed a calc_load_update boundary, make sure to fold | |
3518 | * any pending idle changes, the respective CPUs might have | |
3519 | * missed the tick driven calc_load_account_active() update | |
3520 | * due to NO_HZ. | |
3521 | */ | |
3522 | delta = calc_load_fold_idle(); | |
3523 | if (delta) | |
3524 | atomic_long_add(delta, &calc_load_tasks); | |
3525 | ||
3526 | /* | |
3527 | * If we were idle for multiple load cycles, apply them. | |
3528 | */ | |
3529 | if (ticks >= LOAD_FREQ) { | |
3530 | n = ticks / LOAD_FREQ; | |
3531 | ||
3532 | active = atomic_long_read(&calc_load_tasks); | |
3533 | active = active > 0 ? active * FIXED_1 : 0; | |
3534 | ||
3535 | avenrun[0] = calc_load_n(avenrun[0], EXP_1, active, n); | |
3536 | avenrun[1] = calc_load_n(avenrun[1], EXP_5, active, n); | |
3537 | avenrun[2] = calc_load_n(avenrun[2], EXP_15, active, n); | |
3538 | ||
3539 | calc_load_update += n * LOAD_FREQ; | |
3540 | } | |
3541 | ||
3542 | /* | |
3543 | * Its possible the remainder of the above division also crosses | |
3544 | * a LOAD_FREQ period, the regular check in calc_global_load() | |
3545 | * which comes after this will take care of that. | |
3546 | * | |
3547 | * Consider us being 11 ticks before a cycle completion, and us | |
3548 | * sleeping for 4*LOAD_FREQ + 22 ticks, then the above code will | |
3549 | * age us 4 cycles, and the test in calc_global_load() will | |
3550 | * pick up the final one. | |
3551 | */ | |
3552 | } | |
74f5187a PZ |
3553 | #else |
3554 | static void calc_load_account_idle(struct rq *this_rq) | |
3555 | { | |
3556 | } | |
3557 | ||
3558 | static inline long calc_load_fold_idle(void) | |
3559 | { | |
3560 | return 0; | |
3561 | } | |
0f004f5a PZ |
3562 | |
3563 | static void calc_global_nohz(unsigned long ticks) | |
3564 | { | |
3565 | } | |
74f5187a PZ |
3566 | #endif |
3567 | ||
2d02494f TG |
3568 | /** |
3569 | * get_avenrun - get the load average array | |
3570 | * @loads: pointer to dest load array | |
3571 | * @offset: offset to add | |
3572 | * @shift: shift count to shift the result left | |
3573 | * | |
3574 | * These values are estimates at best, so no need for locking. | |
3575 | */ | |
3576 | void get_avenrun(unsigned long *loads, unsigned long offset, int shift) | |
3577 | { | |
3578 | loads[0] = (avenrun[0] + offset) << shift; | |
3579 | loads[1] = (avenrun[1] + offset) << shift; | |
3580 | loads[2] = (avenrun[2] + offset) << shift; | |
46cb4b7c | 3581 | } |
46cb4b7c | 3582 | |
46cb4b7c | 3583 | /* |
dce48a84 TG |
3584 | * calc_load - update the avenrun load estimates 10 ticks after the |
3585 | * CPUs have updated calc_load_tasks. | |
7835b98b | 3586 | */ |
0f004f5a | 3587 | void calc_global_load(unsigned long ticks) |
7835b98b | 3588 | { |
dce48a84 | 3589 | long active; |
1da177e4 | 3590 | |
0f004f5a PZ |
3591 | calc_global_nohz(ticks); |
3592 | ||
3593 | if (time_before(jiffies, calc_load_update + 10)) | |
dce48a84 | 3594 | return; |
1da177e4 | 3595 | |
dce48a84 TG |
3596 | active = atomic_long_read(&calc_load_tasks); |
3597 | active = active > 0 ? active * FIXED_1 : 0; | |
1da177e4 | 3598 | |
dce48a84 TG |
3599 | avenrun[0] = calc_load(avenrun[0], EXP_1, active); |
3600 | avenrun[1] = calc_load(avenrun[1], EXP_5, active); | |
3601 | avenrun[2] = calc_load(avenrun[2], EXP_15, active); | |
dd41f596 | 3602 | |
dce48a84 TG |
3603 | calc_load_update += LOAD_FREQ; |
3604 | } | |
1da177e4 | 3605 | |
dce48a84 | 3606 | /* |
74f5187a PZ |
3607 | * Called from update_cpu_load() to periodically update this CPU's |
3608 | * active count. | |
dce48a84 TG |
3609 | */ |
3610 | static void calc_load_account_active(struct rq *this_rq) | |
3611 | { | |
74f5187a | 3612 | long delta; |
08c183f3 | 3613 | |
74f5187a PZ |
3614 | if (time_before(jiffies, this_rq->calc_load_update)) |
3615 | return; | |
783609c6 | 3616 | |
74f5187a PZ |
3617 | delta = calc_load_fold_active(this_rq); |
3618 | delta += calc_load_fold_idle(); | |
3619 | if (delta) | |
dce48a84 | 3620 | atomic_long_add(delta, &calc_load_tasks); |
74f5187a PZ |
3621 | |
3622 | this_rq->calc_load_update += LOAD_FREQ; | |
46cb4b7c SS |
3623 | } |
3624 | ||
fdf3e95d VP |
3625 | /* |
3626 | * The exact cpuload at various idx values, calculated at every tick would be | |
3627 | * load = (2^idx - 1) / 2^idx * load + 1 / 2^idx * cur_load | |
3628 | * | |
3629 | * If a cpu misses updates for n-1 ticks (as it was idle) and update gets called | |
3630 | * on nth tick when cpu may be busy, then we have: | |
3631 | * load = ((2^idx - 1) / 2^idx)^(n-1) * load | |
3632 | * load = (2^idx - 1) / 2^idx) * load + 1 / 2^idx * cur_load | |
3633 | * | |
3634 | * decay_load_missed() below does efficient calculation of | |
3635 | * load = ((2^idx - 1) / 2^idx)^(n-1) * load | |
3636 | * avoiding 0..n-1 loop doing load = ((2^idx - 1) / 2^idx) * load | |
3637 | * | |
3638 | * The calculation is approximated on a 128 point scale. | |
3639 | * degrade_zero_ticks is the number of ticks after which load at any | |
3640 | * particular idx is approximated to be zero. | |
3641 | * degrade_factor is a precomputed table, a row for each load idx. | |
3642 | * Each column corresponds to degradation factor for a power of two ticks, | |
3643 | * based on 128 point scale. | |
3644 | * Example: | |
3645 | * row 2, col 3 (=12) says that the degradation at load idx 2 after | |
3646 | * 8 ticks is 12/128 (which is an approximation of exact factor 3^8/4^8). | |
3647 | * | |
3648 | * With this power of 2 load factors, we can degrade the load n times | |
3649 | * by looking at 1 bits in n and doing as many mult/shift instead of | |
3650 | * n mult/shifts needed by the exact degradation. | |
3651 | */ | |
3652 | #define DEGRADE_SHIFT 7 | |
3653 | static const unsigned char | |
3654 | degrade_zero_ticks[CPU_LOAD_IDX_MAX] = {0, 8, 32, 64, 128}; | |
3655 | static const unsigned char | |
3656 | degrade_factor[CPU_LOAD_IDX_MAX][DEGRADE_SHIFT + 1] = { | |
3657 | {0, 0, 0, 0, 0, 0, 0, 0}, | |
3658 | {64, 32, 8, 0, 0, 0, 0, 0}, | |
3659 | {96, 72, 40, 12, 1, 0, 0}, | |
3660 | {112, 98, 75, 43, 15, 1, 0}, | |
3661 | {120, 112, 98, 76, 45, 16, 2} }; | |
3662 | ||
3663 | /* | |
3664 | * Update cpu_load for any missed ticks, due to tickless idle. The backlog | |
3665 | * would be when CPU is idle and so we just decay the old load without | |
3666 | * adding any new load. | |
3667 | */ | |
3668 | static unsigned long | |
3669 | decay_load_missed(unsigned long load, unsigned long missed_updates, int idx) | |
3670 | { | |
3671 | int j = 0; | |
3672 | ||
3673 | if (!missed_updates) | |
3674 | return load; | |
3675 | ||
3676 | if (missed_updates >= degrade_zero_ticks[idx]) | |
3677 | return 0; | |
3678 | ||
3679 | if (idx == 1) | |
3680 | return load >> missed_updates; | |
3681 | ||
3682 | while (missed_updates) { | |
3683 | if (missed_updates % 2) | |
3684 | load = (load * degrade_factor[idx][j]) >> DEGRADE_SHIFT; | |
3685 | ||
3686 | missed_updates >>= 1; | |
3687 | j++; | |
3688 | } | |
3689 | return load; | |
3690 | } | |
3691 | ||
46cb4b7c | 3692 | /* |
dd41f596 | 3693 | * Update rq->cpu_load[] statistics. This function is usually called every |
fdf3e95d VP |
3694 | * scheduler tick (TICK_NSEC). With tickless idle this will not be called |
3695 | * every tick. We fix it up based on jiffies. | |
46cb4b7c | 3696 | */ |
dd41f596 | 3697 | static void update_cpu_load(struct rq *this_rq) |
46cb4b7c | 3698 | { |
495eca49 | 3699 | unsigned long this_load = this_rq->load.weight; |
fdf3e95d VP |
3700 | unsigned long curr_jiffies = jiffies; |
3701 | unsigned long pending_updates; | |
dd41f596 | 3702 | int i, scale; |
46cb4b7c | 3703 | |
dd41f596 | 3704 | this_rq->nr_load_updates++; |
46cb4b7c | 3705 | |
fdf3e95d VP |
3706 | /* Avoid repeated calls on same jiffy, when moving in and out of idle */ |
3707 | if (curr_jiffies == this_rq->last_load_update_tick) | |
3708 | return; | |
3709 | ||
3710 | pending_updates = curr_jiffies - this_rq->last_load_update_tick; | |
3711 | this_rq->last_load_update_tick = curr_jiffies; | |
3712 | ||
dd41f596 | 3713 | /* Update our load: */ |
fdf3e95d VP |
3714 | this_rq->cpu_load[0] = this_load; /* Fasttrack for idx 0 */ |
3715 | for (i = 1, scale = 2; i < CPU_LOAD_IDX_MAX; i++, scale += scale) { | |
dd41f596 | 3716 | unsigned long old_load, new_load; |
7d1e6a9b | 3717 | |
dd41f596 | 3718 | /* scale is effectively 1 << i now, and >> i divides by scale */ |
46cb4b7c | 3719 | |
dd41f596 | 3720 | old_load = this_rq->cpu_load[i]; |
fdf3e95d | 3721 | old_load = decay_load_missed(old_load, pending_updates - 1, i); |
dd41f596 | 3722 | new_load = this_load; |
a25707f3 IM |
3723 | /* |
3724 | * Round up the averaging division if load is increasing. This | |
3725 | * prevents us from getting stuck on 9 if the load is 10, for | |
3726 | * example. | |
3727 | */ | |
3728 | if (new_load > old_load) | |
fdf3e95d VP |
3729 | new_load += scale - 1; |
3730 | ||
3731 | this_rq->cpu_load[i] = (old_load * (scale - 1) + new_load) >> i; | |
dd41f596 | 3732 | } |
da2b71ed SS |
3733 | |
3734 | sched_avg_update(this_rq); | |
fdf3e95d VP |
3735 | } |
3736 | ||
3737 | static void update_cpu_load_active(struct rq *this_rq) | |
3738 | { | |
3739 | update_cpu_load(this_rq); | |
46cb4b7c | 3740 | |
74f5187a | 3741 | calc_load_account_active(this_rq); |
46cb4b7c SS |
3742 | } |
3743 | ||
dd41f596 | 3744 | #ifdef CONFIG_SMP |
8a0be9ef | 3745 | |
46cb4b7c | 3746 | /* |
38022906 PZ |
3747 | * sched_exec - execve() is a valuable balancing opportunity, because at |
3748 | * this point the task has the smallest effective memory and cache footprint. | |
46cb4b7c | 3749 | */ |
38022906 | 3750 | void sched_exec(void) |
46cb4b7c | 3751 | { |
38022906 | 3752 | struct task_struct *p = current; |
1da177e4 | 3753 | unsigned long flags; |
0017d735 | 3754 | int dest_cpu; |
46cb4b7c | 3755 | |
8f42ced9 | 3756 | raw_spin_lock_irqsave(&p->pi_lock, flags); |
7608dec2 | 3757 | dest_cpu = p->sched_class->select_task_rq(p, SD_BALANCE_EXEC, 0); |
0017d735 PZ |
3758 | if (dest_cpu == smp_processor_id()) |
3759 | goto unlock; | |
38022906 | 3760 | |
8f42ced9 | 3761 | if (likely(cpu_active(dest_cpu))) { |
969c7921 | 3762 | struct migration_arg arg = { p, dest_cpu }; |
46cb4b7c | 3763 | |
8f42ced9 PZ |
3764 | raw_spin_unlock_irqrestore(&p->pi_lock, flags); |
3765 | stop_one_cpu(task_cpu(p), migration_cpu_stop, &arg); | |
1da177e4 LT |
3766 | return; |
3767 | } | |
0017d735 | 3768 | unlock: |
8f42ced9 | 3769 | raw_spin_unlock_irqrestore(&p->pi_lock, flags); |
1da177e4 | 3770 | } |
dd41f596 | 3771 | |
1da177e4 LT |
3772 | #endif |
3773 | ||
1da177e4 LT |
3774 | DEFINE_PER_CPU(struct kernel_stat, kstat); |
3775 | ||
3776 | EXPORT_PER_CPU_SYMBOL(kstat); | |
3777 | ||
3778 | /* | |
c5f8d995 | 3779 | * Return any ns on the sched_clock that have not yet been accounted in |
f06febc9 | 3780 | * @p in case that task is currently running. |
c5f8d995 HS |
3781 | * |
3782 | * Called with task_rq_lock() held on @rq. | |
1da177e4 | 3783 | */ |
c5f8d995 HS |
3784 | static u64 do_task_delta_exec(struct task_struct *p, struct rq *rq) |
3785 | { | |
3786 | u64 ns = 0; | |
3787 | ||
3788 | if (task_current(rq, p)) { | |
3789 | update_rq_clock(rq); | |
305e6835 | 3790 | ns = rq->clock_task - p->se.exec_start; |
c5f8d995 HS |
3791 | if ((s64)ns < 0) |
3792 | ns = 0; | |
3793 | } | |
3794 | ||
3795 | return ns; | |
3796 | } | |
3797 | ||
bb34d92f | 3798 | unsigned long long task_delta_exec(struct task_struct *p) |
1da177e4 | 3799 | { |
1da177e4 | 3800 | unsigned long flags; |
41b86e9c | 3801 | struct rq *rq; |
bb34d92f | 3802 | u64 ns = 0; |
48f24c4d | 3803 | |
41b86e9c | 3804 | rq = task_rq_lock(p, &flags); |
c5f8d995 | 3805 | ns = do_task_delta_exec(p, rq); |
0122ec5b | 3806 | task_rq_unlock(rq, p, &flags); |
1508487e | 3807 | |
c5f8d995 HS |
3808 | return ns; |
3809 | } | |
f06febc9 | 3810 | |
c5f8d995 HS |
3811 | /* |
3812 | * Return accounted runtime for the task. | |
3813 | * In case the task is currently running, return the runtime plus current's | |
3814 | * pending runtime that have not been accounted yet. | |
3815 | */ | |
3816 | unsigned long long task_sched_runtime(struct task_struct *p) | |
3817 | { | |
3818 | unsigned long flags; | |
3819 | struct rq *rq; | |
3820 | u64 ns = 0; | |
3821 | ||
3822 | rq = task_rq_lock(p, &flags); | |
3823 | ns = p->se.sum_exec_runtime + do_task_delta_exec(p, rq); | |
0122ec5b | 3824 | task_rq_unlock(rq, p, &flags); |
c5f8d995 HS |
3825 | |
3826 | return ns; | |
3827 | } | |
48f24c4d | 3828 | |
c5f8d995 HS |
3829 | /* |
3830 | * Return sum_exec_runtime for the thread group. | |
3831 | * In case the task is currently running, return the sum plus current's | |
3832 | * pending runtime that have not been accounted yet. | |
3833 | * | |
3834 | * Note that the thread group might have other running tasks as well, | |
3835 | * so the return value not includes other pending runtime that other | |
3836 | * running tasks might have. | |
3837 | */ | |
3838 | unsigned long long thread_group_sched_runtime(struct task_struct *p) | |
3839 | { | |
3840 | struct task_cputime totals; | |
3841 | unsigned long flags; | |
3842 | struct rq *rq; | |
3843 | u64 ns; | |
3844 | ||
3845 | rq = task_rq_lock(p, &flags); | |
3846 | thread_group_cputime(p, &totals); | |
3847 | ns = totals.sum_exec_runtime + do_task_delta_exec(p, rq); | |
0122ec5b | 3848 | task_rq_unlock(rq, p, &flags); |
48f24c4d | 3849 | |
1da177e4 LT |
3850 | return ns; |
3851 | } | |
3852 | ||
1da177e4 LT |
3853 | /* |
3854 | * Account user cpu time to a process. | |
3855 | * @p: the process that the cpu time gets accounted to | |
1da177e4 | 3856 | * @cputime: the cpu time spent in user space since the last update |
457533a7 | 3857 | * @cputime_scaled: cputime scaled by cpu frequency |
1da177e4 | 3858 | */ |
457533a7 MS |
3859 | void account_user_time(struct task_struct *p, cputime_t cputime, |
3860 | cputime_t cputime_scaled) | |
1da177e4 LT |
3861 | { |
3862 | struct cpu_usage_stat *cpustat = &kstat_this_cpu.cpustat; | |
3863 | cputime64_t tmp; | |
3864 | ||
457533a7 | 3865 | /* Add user time to process. */ |
1da177e4 | 3866 | p->utime = cputime_add(p->utime, cputime); |
457533a7 | 3867 | p->utimescaled = cputime_add(p->utimescaled, cputime_scaled); |
f06febc9 | 3868 | account_group_user_time(p, cputime); |
1da177e4 LT |
3869 | |
3870 | /* Add user time to cpustat. */ | |
3871 | tmp = cputime_to_cputime64(cputime); | |
3872 | if (TASK_NICE(p) > 0) | |
3873 | cpustat->nice = cputime64_add(cpustat->nice, tmp); | |
3874 | else | |
3875 | cpustat->user = cputime64_add(cpustat->user, tmp); | |
ef12fefa BR |
3876 | |
3877 | cpuacct_update_stats(p, CPUACCT_STAT_USER, cputime); | |
49b5cf34 JL |
3878 | /* Account for user time used */ |
3879 | acct_update_integrals(p); | |
1da177e4 LT |
3880 | } |
3881 | ||
94886b84 LV |
3882 | /* |
3883 | * Account guest cpu time to a process. | |
3884 | * @p: the process that the cpu time gets accounted to | |
3885 | * @cputime: the cpu time spent in virtual machine since the last update | |
457533a7 | 3886 | * @cputime_scaled: cputime scaled by cpu frequency |
94886b84 | 3887 | */ |
457533a7 MS |
3888 | static void account_guest_time(struct task_struct *p, cputime_t cputime, |
3889 | cputime_t cputime_scaled) | |
94886b84 LV |
3890 | { |
3891 | cputime64_t tmp; | |
3892 | struct cpu_usage_stat *cpustat = &kstat_this_cpu.cpustat; | |
3893 | ||
3894 | tmp = cputime_to_cputime64(cputime); | |
3895 | ||
457533a7 | 3896 | /* Add guest time to process. */ |
94886b84 | 3897 | p->utime = cputime_add(p->utime, cputime); |
457533a7 | 3898 | p->utimescaled = cputime_add(p->utimescaled, cputime_scaled); |
f06febc9 | 3899 | account_group_user_time(p, cputime); |
94886b84 LV |
3900 | p->gtime = cputime_add(p->gtime, cputime); |
3901 | ||
457533a7 | 3902 | /* Add guest time to cpustat. */ |
ce0e7b28 RO |
3903 | if (TASK_NICE(p) > 0) { |
3904 | cpustat->nice = cputime64_add(cpustat->nice, tmp); | |
3905 | cpustat->guest_nice = cputime64_add(cpustat->guest_nice, tmp); | |
3906 | } else { | |
3907 | cpustat->user = cputime64_add(cpustat->user, tmp); | |
3908 | cpustat->guest = cputime64_add(cpustat->guest, tmp); | |
3909 | } | |
94886b84 LV |
3910 | } |
3911 | ||
70a89a66 VP |
3912 | /* |
3913 | * Account system cpu time to a process and desired cpustat field | |
3914 | * @p: the process that the cpu time gets accounted to | |
3915 | * @cputime: the cpu time spent in kernel space since the last update | |
3916 | * @cputime_scaled: cputime scaled by cpu frequency | |
3917 | * @target_cputime64: pointer to cpustat field that has to be updated | |
3918 | */ | |
3919 | static inline | |
3920 | void __account_system_time(struct task_struct *p, cputime_t cputime, | |
3921 | cputime_t cputime_scaled, cputime64_t *target_cputime64) | |
3922 | { | |
3923 | cputime64_t tmp = cputime_to_cputime64(cputime); | |
3924 | ||
3925 | /* Add system time to process. */ | |
3926 | p->stime = cputime_add(p->stime, cputime); | |
3927 | p->stimescaled = cputime_add(p->stimescaled, cputime_scaled); | |
3928 | account_group_system_time(p, cputime); | |
3929 | ||
3930 | /* Add system time to cpustat. */ | |
3931 | *target_cputime64 = cputime64_add(*target_cputime64, tmp); | |
3932 | cpuacct_update_stats(p, CPUACCT_STAT_SYSTEM, cputime); | |
3933 | ||
3934 | /* Account for system time used */ | |
3935 | acct_update_integrals(p); | |
3936 | } | |
3937 | ||
1da177e4 LT |
3938 | /* |
3939 | * Account system cpu time to a process. | |
3940 | * @p: the process that the cpu time gets accounted to | |
3941 | * @hardirq_offset: the offset to subtract from hardirq_count() | |
3942 | * @cputime: the cpu time spent in kernel space since the last update | |
457533a7 | 3943 | * @cputime_scaled: cputime scaled by cpu frequency |
1da177e4 LT |
3944 | */ |
3945 | void account_system_time(struct task_struct *p, int hardirq_offset, | |
457533a7 | 3946 | cputime_t cputime, cputime_t cputime_scaled) |
1da177e4 LT |
3947 | { |
3948 | struct cpu_usage_stat *cpustat = &kstat_this_cpu.cpustat; | |
70a89a66 | 3949 | cputime64_t *target_cputime64; |
1da177e4 | 3950 | |
983ed7a6 | 3951 | if ((p->flags & PF_VCPU) && (irq_count() - hardirq_offset == 0)) { |
457533a7 | 3952 | account_guest_time(p, cputime, cputime_scaled); |
983ed7a6 HH |
3953 | return; |
3954 | } | |
94886b84 | 3955 | |
1da177e4 | 3956 | if (hardirq_count() - hardirq_offset) |
70a89a66 | 3957 | target_cputime64 = &cpustat->irq; |
75e1056f | 3958 | else if (in_serving_softirq()) |
70a89a66 | 3959 | target_cputime64 = &cpustat->softirq; |
1da177e4 | 3960 | else |
70a89a66 | 3961 | target_cputime64 = &cpustat->system; |
ef12fefa | 3962 | |
70a89a66 | 3963 | __account_system_time(p, cputime, cputime_scaled, target_cputime64); |
1da177e4 LT |
3964 | } |
3965 | ||
c66f08be | 3966 | /* |
1da177e4 | 3967 | * Account for involuntary wait time. |
544b4a1f | 3968 | * @cputime: the cpu time spent in involuntary wait |
c66f08be | 3969 | */ |
79741dd3 | 3970 | void account_steal_time(cputime_t cputime) |
c66f08be | 3971 | { |
79741dd3 MS |
3972 | struct cpu_usage_stat *cpustat = &kstat_this_cpu.cpustat; |
3973 | cputime64_t cputime64 = cputime_to_cputime64(cputime); | |
3974 | ||
3975 | cpustat->steal = cputime64_add(cpustat->steal, cputime64); | |
c66f08be MN |
3976 | } |
3977 | ||
1da177e4 | 3978 | /* |
79741dd3 MS |
3979 | * Account for idle time. |
3980 | * @cputime: the cpu time spent in idle wait | |
1da177e4 | 3981 | */ |
79741dd3 | 3982 | void account_idle_time(cputime_t cputime) |
1da177e4 LT |
3983 | { |
3984 | struct cpu_usage_stat *cpustat = &kstat_this_cpu.cpustat; | |
79741dd3 | 3985 | cputime64_t cputime64 = cputime_to_cputime64(cputime); |
70b97a7f | 3986 | struct rq *rq = this_rq(); |
1da177e4 | 3987 | |
79741dd3 MS |
3988 | if (atomic_read(&rq->nr_iowait) > 0) |
3989 | cpustat->iowait = cputime64_add(cpustat->iowait, cputime64); | |
3990 | else | |
3991 | cpustat->idle = cputime64_add(cpustat->idle, cputime64); | |
1da177e4 LT |
3992 | } |
3993 | ||
e6e6685a GC |
3994 | static __always_inline bool steal_account_process_tick(void) |
3995 | { | |
3996 | #ifdef CONFIG_PARAVIRT | |
3997 | if (static_branch(¶virt_steal_enabled)) { | |
3998 | u64 steal, st = 0; | |
3999 | ||
4000 | steal = paravirt_steal_clock(smp_processor_id()); | |
4001 | steal -= this_rq()->prev_steal_time; | |
4002 | ||
4003 | st = steal_ticks(steal); | |
4004 | this_rq()->prev_steal_time += st * TICK_NSEC; | |
4005 | ||
4006 | account_steal_time(st); | |
4007 | return st; | |
4008 | } | |
4009 | #endif | |
4010 | return false; | |
4011 | } | |
4012 | ||
79741dd3 MS |
4013 | #ifndef CONFIG_VIRT_CPU_ACCOUNTING |
4014 | ||
abb74cef VP |
4015 | #ifdef CONFIG_IRQ_TIME_ACCOUNTING |
4016 | /* | |
4017 | * Account a tick to a process and cpustat | |
4018 | * @p: the process that the cpu time gets accounted to | |
4019 | * @user_tick: is the tick from userspace | |
4020 | * @rq: the pointer to rq | |
4021 | * | |
4022 | * Tick demultiplexing follows the order | |
4023 | * - pending hardirq update | |
4024 | * - pending softirq update | |
4025 | * - user_time | |
4026 | * - idle_time | |
4027 | * - system time | |
4028 | * - check for guest_time | |
4029 | * - else account as system_time | |
4030 | * | |
4031 | * Check for hardirq is done both for system and user time as there is | |
4032 | * no timer going off while we are on hardirq and hence we may never get an | |
4033 | * opportunity to update it solely in system time. | |
4034 | * p->stime and friends are only updated on system time and not on irq | |
4035 | * softirq as those do not count in task exec_runtime any more. | |
4036 | */ | |
4037 | static void irqtime_account_process_tick(struct task_struct *p, int user_tick, | |
4038 | struct rq *rq) | |
4039 | { | |
4040 | cputime_t one_jiffy_scaled = cputime_to_scaled(cputime_one_jiffy); | |
4041 | cputime64_t tmp = cputime_to_cputime64(cputime_one_jiffy); | |
4042 | struct cpu_usage_stat *cpustat = &kstat_this_cpu.cpustat; | |
4043 | ||
e6e6685a GC |
4044 | if (steal_account_process_tick()) |
4045 | return; | |
4046 | ||
abb74cef VP |
4047 | if (irqtime_account_hi_update()) { |
4048 | cpustat->irq = cputime64_add(cpustat->irq, tmp); | |
4049 | } else if (irqtime_account_si_update()) { | |
4050 | cpustat->softirq = cputime64_add(cpustat->softirq, tmp); | |
414bee9b VP |
4051 | } else if (this_cpu_ksoftirqd() == p) { |
4052 | /* | |
4053 | * ksoftirqd time do not get accounted in cpu_softirq_time. | |
4054 | * So, we have to handle it separately here. | |
4055 | * Also, p->stime needs to be updated for ksoftirqd. | |
4056 | */ | |
4057 | __account_system_time(p, cputime_one_jiffy, one_jiffy_scaled, | |
4058 | &cpustat->softirq); | |
abb74cef VP |
4059 | } else if (user_tick) { |
4060 | account_user_time(p, cputime_one_jiffy, one_jiffy_scaled); | |
4061 | } else if (p == rq->idle) { | |
4062 | account_idle_time(cputime_one_jiffy); | |
4063 | } else if (p->flags & PF_VCPU) { /* System time or guest time */ | |
4064 | account_guest_time(p, cputime_one_jiffy, one_jiffy_scaled); | |
4065 | } else { | |
4066 | __account_system_time(p, cputime_one_jiffy, one_jiffy_scaled, | |
4067 | &cpustat->system); | |
4068 | } | |
4069 | } | |
4070 | ||
4071 | static void irqtime_account_idle_ticks(int ticks) | |
4072 | { | |
4073 | int i; | |
4074 | struct rq *rq = this_rq(); | |
4075 | ||
4076 | for (i = 0; i < ticks; i++) | |
4077 | irqtime_account_process_tick(current, 0, rq); | |
4078 | } | |
544b4a1f | 4079 | #else /* CONFIG_IRQ_TIME_ACCOUNTING */ |
abb74cef VP |
4080 | static void irqtime_account_idle_ticks(int ticks) {} |
4081 | static void irqtime_account_process_tick(struct task_struct *p, int user_tick, | |
4082 | struct rq *rq) {} | |
544b4a1f | 4083 | #endif /* CONFIG_IRQ_TIME_ACCOUNTING */ |
79741dd3 MS |
4084 | |
4085 | /* | |
4086 | * Account a single tick of cpu time. | |
4087 | * @p: the process that the cpu time gets accounted to | |
4088 | * @user_tick: indicates if the tick is a user or a system tick | |
4089 | */ | |
4090 | void account_process_tick(struct task_struct *p, int user_tick) | |
4091 | { | |
a42548a1 | 4092 | cputime_t one_jiffy_scaled = cputime_to_scaled(cputime_one_jiffy); |
79741dd3 MS |
4093 | struct rq *rq = this_rq(); |
4094 | ||
abb74cef VP |
4095 | if (sched_clock_irqtime) { |
4096 | irqtime_account_process_tick(p, user_tick, rq); | |
4097 | return; | |
4098 | } | |
4099 | ||
e6e6685a GC |
4100 | if (steal_account_process_tick()) |
4101 | return; | |
4102 | ||
79741dd3 | 4103 | if (user_tick) |
a42548a1 | 4104 | account_user_time(p, cputime_one_jiffy, one_jiffy_scaled); |
f5f293a4 | 4105 | else if ((p != rq->idle) || (irq_count() != HARDIRQ_OFFSET)) |
a42548a1 | 4106 | account_system_time(p, HARDIRQ_OFFSET, cputime_one_jiffy, |
79741dd3 MS |
4107 | one_jiffy_scaled); |
4108 | else | |
a42548a1 | 4109 | account_idle_time(cputime_one_jiffy); |
79741dd3 MS |
4110 | } |
4111 | ||
4112 | /* | |
4113 | * Account multiple ticks of steal time. | |
4114 | * @p: the process from which the cpu time has been stolen | |
4115 | * @ticks: number of stolen ticks | |
4116 | */ | |
4117 | void account_steal_ticks(unsigned long ticks) | |
4118 | { | |
4119 | account_steal_time(jiffies_to_cputime(ticks)); | |
4120 | } | |
4121 | ||
4122 | /* | |
4123 | * Account multiple ticks of idle time. | |
4124 | * @ticks: number of stolen ticks | |
4125 | */ | |
4126 | void account_idle_ticks(unsigned long ticks) | |
4127 | { | |
abb74cef VP |
4128 | |
4129 | if (sched_clock_irqtime) { | |
4130 | irqtime_account_idle_ticks(ticks); | |
4131 | return; | |
4132 | } | |
4133 | ||
79741dd3 | 4134 | account_idle_time(jiffies_to_cputime(ticks)); |
1da177e4 LT |
4135 | } |
4136 | ||
79741dd3 MS |
4137 | #endif |
4138 | ||
49048622 BS |
4139 | /* |
4140 | * Use precise platform statistics if available: | |
4141 | */ | |
4142 | #ifdef CONFIG_VIRT_CPU_ACCOUNTING | |
d180c5bc | 4143 | void task_times(struct task_struct *p, cputime_t *ut, cputime_t *st) |
49048622 | 4144 | { |
d99ca3b9 HS |
4145 | *ut = p->utime; |
4146 | *st = p->stime; | |
49048622 BS |
4147 | } |
4148 | ||
0cf55e1e | 4149 | void thread_group_times(struct task_struct *p, cputime_t *ut, cputime_t *st) |
49048622 | 4150 | { |
0cf55e1e HS |
4151 | struct task_cputime cputime; |
4152 | ||
4153 | thread_group_cputime(p, &cputime); | |
4154 | ||
4155 | *ut = cputime.utime; | |
4156 | *st = cputime.stime; | |
49048622 BS |
4157 | } |
4158 | #else | |
761b1d26 HS |
4159 | |
4160 | #ifndef nsecs_to_cputime | |
b7b20df9 | 4161 | # define nsecs_to_cputime(__nsecs) nsecs_to_jiffies(__nsecs) |
761b1d26 HS |
4162 | #endif |
4163 | ||
d180c5bc | 4164 | void task_times(struct task_struct *p, cputime_t *ut, cputime_t *st) |
49048622 | 4165 | { |
d99ca3b9 | 4166 | cputime_t rtime, utime = p->utime, total = cputime_add(utime, p->stime); |
49048622 BS |
4167 | |
4168 | /* | |
4169 | * Use CFS's precise accounting: | |
4170 | */ | |
d180c5bc | 4171 | rtime = nsecs_to_cputime(p->se.sum_exec_runtime); |
49048622 BS |
4172 | |
4173 | if (total) { | |
e75e863d | 4174 | u64 temp = rtime; |
d180c5bc | 4175 | |
e75e863d | 4176 | temp *= utime; |
49048622 | 4177 | do_div(temp, total); |
d180c5bc HS |
4178 | utime = (cputime_t)temp; |
4179 | } else | |
4180 | utime = rtime; | |
49048622 | 4181 | |
d180c5bc HS |
4182 | /* |
4183 | * Compare with previous values, to keep monotonicity: | |
4184 | */ | |
761b1d26 | 4185 | p->prev_utime = max(p->prev_utime, utime); |
d99ca3b9 | 4186 | p->prev_stime = max(p->prev_stime, cputime_sub(rtime, p->prev_utime)); |
49048622 | 4187 | |
d99ca3b9 HS |
4188 | *ut = p->prev_utime; |
4189 | *st = p->prev_stime; | |
49048622 BS |
4190 | } |
4191 | ||
0cf55e1e HS |
4192 | /* |
4193 | * Must be called with siglock held. | |
4194 | */ | |
4195 | void thread_group_times(struct task_struct *p, cputime_t *ut, cputime_t *st) | |
49048622 | 4196 | { |
0cf55e1e HS |
4197 | struct signal_struct *sig = p->signal; |
4198 | struct task_cputime cputime; | |
4199 | cputime_t rtime, utime, total; | |
49048622 | 4200 | |
0cf55e1e | 4201 | thread_group_cputime(p, &cputime); |
49048622 | 4202 | |
0cf55e1e HS |
4203 | total = cputime_add(cputime.utime, cputime.stime); |
4204 | rtime = nsecs_to_cputime(cputime.sum_exec_runtime); | |
49048622 | 4205 | |
0cf55e1e | 4206 | if (total) { |
e75e863d | 4207 | u64 temp = rtime; |
49048622 | 4208 | |
e75e863d | 4209 | temp *= cputime.utime; |
0cf55e1e HS |
4210 | do_div(temp, total); |
4211 | utime = (cputime_t)temp; | |
4212 | } else | |
4213 | utime = rtime; | |
4214 | ||
4215 | sig->prev_utime = max(sig->prev_utime, utime); | |
4216 | sig->prev_stime = max(sig->prev_stime, | |
4217 | cputime_sub(rtime, sig->prev_utime)); | |
4218 | ||
4219 | *ut = sig->prev_utime; | |
4220 | *st = sig->prev_stime; | |
49048622 | 4221 | } |
49048622 | 4222 | #endif |
49048622 | 4223 | |
7835b98b CL |
4224 | /* |
4225 | * This function gets called by the timer code, with HZ frequency. | |
4226 | * We call it with interrupts disabled. | |
7835b98b CL |
4227 | */ |
4228 | void scheduler_tick(void) | |
4229 | { | |
7835b98b CL |
4230 | int cpu = smp_processor_id(); |
4231 | struct rq *rq = cpu_rq(cpu); | |
dd41f596 | 4232 | struct task_struct *curr = rq->curr; |
3e51f33f PZ |
4233 | |
4234 | sched_clock_tick(); | |
dd41f596 | 4235 | |
05fa785c | 4236 | raw_spin_lock(&rq->lock); |
3e51f33f | 4237 | update_rq_clock(rq); |
fdf3e95d | 4238 | update_cpu_load_active(rq); |
fa85ae24 | 4239 | curr->sched_class->task_tick(rq, curr, 0); |
05fa785c | 4240 | raw_spin_unlock(&rq->lock); |
7835b98b | 4241 | |
e9d2b064 | 4242 | perf_event_task_tick(); |
e220d2dc | 4243 | |
e418e1c2 | 4244 | #ifdef CONFIG_SMP |
dd41f596 IM |
4245 | rq->idle_at_tick = idle_cpu(cpu); |
4246 | trigger_load_balance(rq, cpu); | |
e418e1c2 | 4247 | #endif |
1da177e4 LT |
4248 | } |
4249 | ||
132380a0 | 4250 | notrace unsigned long get_parent_ip(unsigned long addr) |
6cd8a4bb SR |
4251 | { |
4252 | if (in_lock_functions(addr)) { | |
4253 | addr = CALLER_ADDR2; | |
4254 | if (in_lock_functions(addr)) | |
4255 | addr = CALLER_ADDR3; | |
4256 | } | |
4257 | return addr; | |
4258 | } | |
1da177e4 | 4259 | |
7e49fcce SR |
4260 | #if defined(CONFIG_PREEMPT) && (defined(CONFIG_DEBUG_PREEMPT) || \ |
4261 | defined(CONFIG_PREEMPT_TRACER)) | |
4262 | ||
43627582 | 4263 | void __kprobes add_preempt_count(int val) |
1da177e4 | 4264 | { |
6cd8a4bb | 4265 | #ifdef CONFIG_DEBUG_PREEMPT |
1da177e4 LT |
4266 | /* |
4267 | * Underflow? | |
4268 | */ | |
9a11b49a IM |
4269 | if (DEBUG_LOCKS_WARN_ON((preempt_count() < 0))) |
4270 | return; | |
6cd8a4bb | 4271 | #endif |
1da177e4 | 4272 | preempt_count() += val; |
6cd8a4bb | 4273 | #ifdef CONFIG_DEBUG_PREEMPT |
1da177e4 LT |
4274 | /* |
4275 | * Spinlock count overflowing soon? | |
4276 | */ | |
33859f7f MOS |
4277 | DEBUG_LOCKS_WARN_ON((preempt_count() & PREEMPT_MASK) >= |
4278 | PREEMPT_MASK - 10); | |
6cd8a4bb SR |
4279 | #endif |
4280 | if (preempt_count() == val) | |
4281 | trace_preempt_off(CALLER_ADDR0, get_parent_ip(CALLER_ADDR1)); | |
1da177e4 LT |
4282 | } |
4283 | EXPORT_SYMBOL(add_preempt_count); | |
4284 | ||
43627582 | 4285 | void __kprobes sub_preempt_count(int val) |
1da177e4 | 4286 | { |
6cd8a4bb | 4287 | #ifdef CONFIG_DEBUG_PREEMPT |
1da177e4 LT |
4288 | /* |
4289 | * Underflow? | |
4290 | */ | |
01e3eb82 | 4291 | if (DEBUG_LOCKS_WARN_ON(val > preempt_count())) |
9a11b49a | 4292 | return; |
1da177e4 LT |
4293 | /* |
4294 | * Is the spinlock portion underflowing? | |
4295 | */ | |
9a11b49a IM |
4296 | if (DEBUG_LOCKS_WARN_ON((val < PREEMPT_MASK) && |
4297 | !(preempt_count() & PREEMPT_MASK))) | |
4298 | return; | |
6cd8a4bb | 4299 | #endif |
9a11b49a | 4300 | |
6cd8a4bb SR |
4301 | if (preempt_count() == val) |
4302 | trace_preempt_on(CALLER_ADDR0, get_parent_ip(CALLER_ADDR1)); | |
1da177e4 LT |
4303 | preempt_count() -= val; |
4304 | } | |
4305 | EXPORT_SYMBOL(sub_preempt_count); | |
4306 | ||
4307 | #endif | |
4308 | ||
4309 | /* | |
dd41f596 | 4310 | * Print scheduling while atomic bug: |
1da177e4 | 4311 | */ |
dd41f596 | 4312 | static noinline void __schedule_bug(struct task_struct *prev) |
1da177e4 | 4313 | { |
838225b4 SS |
4314 | struct pt_regs *regs = get_irq_regs(); |
4315 | ||
3df0fc5b PZ |
4316 | printk(KERN_ERR "BUG: scheduling while atomic: %s/%d/0x%08x\n", |
4317 | prev->comm, prev->pid, preempt_count()); | |
838225b4 | 4318 | |
dd41f596 | 4319 | debug_show_held_locks(prev); |
e21f5b15 | 4320 | print_modules(); |
dd41f596 IM |
4321 | if (irqs_disabled()) |
4322 | print_irqtrace_events(prev); | |
838225b4 SS |
4323 | |
4324 | if (regs) | |
4325 | show_regs(regs); | |
4326 | else | |
4327 | dump_stack(); | |
dd41f596 | 4328 | } |
1da177e4 | 4329 | |
dd41f596 IM |
4330 | /* |
4331 | * Various schedule()-time debugging checks and statistics: | |
4332 | */ | |
4333 | static inline void schedule_debug(struct task_struct *prev) | |
4334 | { | |
1da177e4 | 4335 | /* |
41a2d6cf | 4336 | * Test if we are atomic. Since do_exit() needs to call into |
1da177e4 LT |
4337 | * schedule() atomically, we ignore that path for now. |
4338 | * Otherwise, whine if we are scheduling when we should not be. | |
4339 | */ | |
3f33a7ce | 4340 | if (unlikely(in_atomic_preempt_off() && !prev->exit_state)) |
dd41f596 IM |
4341 | __schedule_bug(prev); |
4342 | ||
1da177e4 LT |
4343 | profile_hit(SCHED_PROFILING, __builtin_return_address(0)); |
4344 | ||
2d72376b | 4345 | schedstat_inc(this_rq(), sched_count); |
dd41f596 IM |
4346 | } |
4347 | ||
6cecd084 | 4348 | static void put_prev_task(struct rq *rq, struct task_struct *prev) |
df1c99d4 | 4349 | { |
61eadef6 | 4350 | if (prev->on_rq || rq->skip_clock_update < 0) |
a64692a3 | 4351 | update_rq_clock(rq); |
6cecd084 | 4352 | prev->sched_class->put_prev_task(rq, prev); |
df1c99d4 MG |
4353 | } |
4354 | ||
dd41f596 IM |
4355 | /* |
4356 | * Pick up the highest-prio task: | |
4357 | */ | |
4358 | static inline struct task_struct * | |
b67802ea | 4359 | pick_next_task(struct rq *rq) |
dd41f596 | 4360 | { |
5522d5d5 | 4361 | const struct sched_class *class; |
dd41f596 | 4362 | struct task_struct *p; |
1da177e4 LT |
4363 | |
4364 | /* | |
dd41f596 IM |
4365 | * Optimization: we know that if all tasks are in |
4366 | * the fair class we can call that function directly: | |
1da177e4 | 4367 | */ |
953bfcd1 | 4368 | if (likely(rq->nr_running == rq->cfs.h_nr_running)) { |
fb8d4724 | 4369 | p = fair_sched_class.pick_next_task(rq); |
dd41f596 IM |
4370 | if (likely(p)) |
4371 | return p; | |
1da177e4 LT |
4372 | } |
4373 | ||
34f971f6 | 4374 | for_each_class(class) { |
fb8d4724 | 4375 | p = class->pick_next_task(rq); |
dd41f596 IM |
4376 | if (p) |
4377 | return p; | |
dd41f596 | 4378 | } |
34f971f6 PZ |
4379 | |
4380 | BUG(); /* the idle class will always have a runnable task */ | |
dd41f596 | 4381 | } |
1da177e4 | 4382 | |
dd41f596 IM |
4383 | /* |
4384 | * schedule() is the main scheduler function. | |
4385 | */ | |
ff743345 | 4386 | asmlinkage void __sched schedule(void) |
dd41f596 IM |
4387 | { |
4388 | struct task_struct *prev, *next; | |
67ca7bde | 4389 | unsigned long *switch_count; |
dd41f596 | 4390 | struct rq *rq; |
31656519 | 4391 | int cpu; |
dd41f596 | 4392 | |
ff743345 PZ |
4393 | need_resched: |
4394 | preempt_disable(); | |
dd41f596 IM |
4395 | cpu = smp_processor_id(); |
4396 | rq = cpu_rq(cpu); | |
25502a6c | 4397 | rcu_note_context_switch(cpu); |
dd41f596 | 4398 | prev = rq->curr; |
dd41f596 | 4399 | |
dd41f596 | 4400 | schedule_debug(prev); |
1da177e4 | 4401 | |
31656519 | 4402 | if (sched_feat(HRTICK)) |
f333fdc9 | 4403 | hrtick_clear(rq); |
8f4d37ec | 4404 | |
05fa785c | 4405 | raw_spin_lock_irq(&rq->lock); |
1da177e4 | 4406 | |
246d86b5 | 4407 | switch_count = &prev->nivcsw; |
1da177e4 | 4408 | if (prev->state && !(preempt_count() & PREEMPT_ACTIVE)) { |
21aa9af0 | 4409 | if (unlikely(signal_pending_state(prev->state, prev))) { |
1da177e4 | 4410 | prev->state = TASK_RUNNING; |
21aa9af0 | 4411 | } else { |
2acca55e PZ |
4412 | deactivate_task(rq, prev, DEQUEUE_SLEEP); |
4413 | prev->on_rq = 0; | |
4414 | ||
21aa9af0 | 4415 | /* |
2acca55e PZ |
4416 | * If a worker went to sleep, notify and ask workqueue |
4417 | * whether it wants to wake up a task to maintain | |
4418 | * concurrency. | |
21aa9af0 TH |
4419 | */ |
4420 | if (prev->flags & PF_WQ_WORKER) { | |
4421 | struct task_struct *to_wakeup; | |
4422 | ||
4423 | to_wakeup = wq_worker_sleeping(prev, cpu); | |
4424 | if (to_wakeup) | |
4425 | try_to_wake_up_local(to_wakeup); | |
4426 | } | |
fd2f4419 | 4427 | |
6631e635 | 4428 | /* |
2acca55e PZ |
4429 | * If we are going to sleep and we have plugged IO |
4430 | * queued, make sure to submit it to avoid deadlocks. | |
6631e635 LT |
4431 | */ |
4432 | if (blk_needs_flush_plug(prev)) { | |
4433 | raw_spin_unlock(&rq->lock); | |
a237c1c5 | 4434 | blk_schedule_flush_plug(prev); |
6631e635 LT |
4435 | raw_spin_lock(&rq->lock); |
4436 | } | |
21aa9af0 | 4437 | } |
dd41f596 | 4438 | switch_count = &prev->nvcsw; |
1da177e4 LT |
4439 | } |
4440 | ||
3f029d3c | 4441 | pre_schedule(rq, prev); |
f65eda4f | 4442 | |
dd41f596 | 4443 | if (unlikely(!rq->nr_running)) |
1da177e4 | 4444 | idle_balance(cpu, rq); |
1da177e4 | 4445 | |
df1c99d4 | 4446 | put_prev_task(rq, prev); |
b67802ea | 4447 | next = pick_next_task(rq); |
f26f9aff MG |
4448 | clear_tsk_need_resched(prev); |
4449 | rq->skip_clock_update = 0; | |
1da177e4 | 4450 | |
1da177e4 | 4451 | if (likely(prev != next)) { |
1da177e4 LT |
4452 | rq->nr_switches++; |
4453 | rq->curr = next; | |
4454 | ++*switch_count; | |
4455 | ||
dd41f596 | 4456 | context_switch(rq, prev, next); /* unlocks the rq */ |
8f4d37ec | 4457 | /* |
246d86b5 ON |
4458 | * The context switch have flipped the stack from under us |
4459 | * and restored the local variables which were saved when | |
4460 | * this task called schedule() in the past. prev == current | |
4461 | * is still correct, but it can be moved to another cpu/rq. | |
8f4d37ec PZ |
4462 | */ |
4463 | cpu = smp_processor_id(); | |
4464 | rq = cpu_rq(cpu); | |
1da177e4 | 4465 | } else |
05fa785c | 4466 | raw_spin_unlock_irq(&rq->lock); |
1da177e4 | 4467 | |
3f029d3c | 4468 | post_schedule(rq); |
1da177e4 | 4469 | |
1da177e4 | 4470 | preempt_enable_no_resched(); |
ff743345 | 4471 | if (need_resched()) |
1da177e4 LT |
4472 | goto need_resched; |
4473 | } | |
1da177e4 LT |
4474 | EXPORT_SYMBOL(schedule); |
4475 | ||
c08f7829 | 4476 | #ifdef CONFIG_MUTEX_SPIN_ON_OWNER |
0d66bf6d | 4477 | |
c6eb3dda PZ |
4478 | static inline bool owner_running(struct mutex *lock, struct task_struct *owner) |
4479 | { | |
c6eb3dda | 4480 | if (lock->owner != owner) |
307bf980 | 4481 | return false; |
0d66bf6d PZ |
4482 | |
4483 | /* | |
c6eb3dda PZ |
4484 | * Ensure we emit the owner->on_cpu, dereference _after_ checking |
4485 | * lock->owner still matches owner, if that fails, owner might | |
4486 | * point to free()d memory, if it still matches, the rcu_read_lock() | |
4487 | * ensures the memory stays valid. | |
0d66bf6d | 4488 | */ |
c6eb3dda | 4489 | barrier(); |
0d66bf6d | 4490 | |
307bf980 | 4491 | return owner->on_cpu; |
c6eb3dda | 4492 | } |
0d66bf6d | 4493 | |
c6eb3dda PZ |
4494 | /* |
4495 | * Look out! "owner" is an entirely speculative pointer | |
4496 | * access and not reliable. | |
4497 | */ | |
4498 | int mutex_spin_on_owner(struct mutex *lock, struct task_struct *owner) | |
4499 | { | |
4500 | if (!sched_feat(OWNER_SPIN)) | |
4501 | return 0; | |
0d66bf6d | 4502 | |
307bf980 | 4503 | rcu_read_lock(); |
c6eb3dda PZ |
4504 | while (owner_running(lock, owner)) { |
4505 | if (need_resched()) | |
307bf980 | 4506 | break; |
0d66bf6d | 4507 | |
335d7afb | 4508 | arch_mutex_cpu_relax(); |
0d66bf6d | 4509 | } |
307bf980 | 4510 | rcu_read_unlock(); |
4b402210 | 4511 | |
c6eb3dda | 4512 | /* |
307bf980 TG |
4513 | * We break out the loop above on need_resched() and when the |
4514 | * owner changed, which is a sign for heavy contention. Return | |
4515 | * success only when lock->owner is NULL. | |
c6eb3dda | 4516 | */ |
307bf980 | 4517 | return lock->owner == NULL; |
0d66bf6d PZ |
4518 | } |
4519 | #endif | |
4520 | ||
1da177e4 LT |
4521 | #ifdef CONFIG_PREEMPT |
4522 | /* | |
2ed6e34f | 4523 | * this is the entry point to schedule() from in-kernel preemption |
41a2d6cf | 4524 | * off of preempt_enable. Kernel preemptions off return from interrupt |
1da177e4 LT |
4525 | * occur there and call schedule directly. |
4526 | */ | |
d1f74e20 | 4527 | asmlinkage void __sched notrace preempt_schedule(void) |
1da177e4 LT |
4528 | { |
4529 | struct thread_info *ti = current_thread_info(); | |
6478d880 | 4530 | |
1da177e4 LT |
4531 | /* |
4532 | * If there is a non-zero preempt_count or interrupts are disabled, | |
41a2d6cf | 4533 | * we do not want to preempt the current task. Just return.. |
1da177e4 | 4534 | */ |
beed33a8 | 4535 | if (likely(ti->preempt_count || irqs_disabled())) |
1da177e4 LT |
4536 | return; |
4537 | ||
3a5c359a | 4538 | do { |
d1f74e20 | 4539 | add_preempt_count_notrace(PREEMPT_ACTIVE); |
3a5c359a | 4540 | schedule(); |
d1f74e20 | 4541 | sub_preempt_count_notrace(PREEMPT_ACTIVE); |
1da177e4 | 4542 | |
3a5c359a AK |
4543 | /* |
4544 | * Check again in case we missed a preemption opportunity | |
4545 | * between schedule and now. | |
4546 | */ | |
4547 | barrier(); | |
5ed0cec0 | 4548 | } while (need_resched()); |
1da177e4 | 4549 | } |
1da177e4 LT |
4550 | EXPORT_SYMBOL(preempt_schedule); |
4551 | ||
4552 | /* | |
2ed6e34f | 4553 | * this is the entry point to schedule() from kernel preemption |
1da177e4 LT |
4554 | * off of irq context. |
4555 | * Note, that this is called and return with irqs disabled. This will | |
4556 | * protect us against recursive calling from irq. | |
4557 | */ | |
4558 | asmlinkage void __sched preempt_schedule_irq(void) | |
4559 | { | |
4560 | struct thread_info *ti = current_thread_info(); | |
6478d880 | 4561 | |
2ed6e34f | 4562 | /* Catch callers which need to be fixed */ |
1da177e4 LT |
4563 | BUG_ON(ti->preempt_count || !irqs_disabled()); |
4564 | ||
3a5c359a AK |
4565 | do { |
4566 | add_preempt_count(PREEMPT_ACTIVE); | |
3a5c359a AK |
4567 | local_irq_enable(); |
4568 | schedule(); | |
4569 | local_irq_disable(); | |
3a5c359a | 4570 | sub_preempt_count(PREEMPT_ACTIVE); |
1da177e4 | 4571 | |
3a5c359a AK |
4572 | /* |
4573 | * Check again in case we missed a preemption opportunity | |
4574 | * between schedule and now. | |
4575 | */ | |
4576 | barrier(); | |
5ed0cec0 | 4577 | } while (need_resched()); |
1da177e4 LT |
4578 | } |
4579 | ||
4580 | #endif /* CONFIG_PREEMPT */ | |
4581 | ||
63859d4f | 4582 | int default_wake_function(wait_queue_t *curr, unsigned mode, int wake_flags, |
95cdf3b7 | 4583 | void *key) |
1da177e4 | 4584 | { |
63859d4f | 4585 | return try_to_wake_up(curr->private, mode, wake_flags); |
1da177e4 | 4586 | } |
1da177e4 LT |
4587 | EXPORT_SYMBOL(default_wake_function); |
4588 | ||
4589 | /* | |
41a2d6cf IM |
4590 | * The core wakeup function. Non-exclusive wakeups (nr_exclusive == 0) just |
4591 | * wake everything up. If it's an exclusive wakeup (nr_exclusive == small +ve | |
1da177e4 LT |
4592 | * number) then we wake all the non-exclusive tasks and one exclusive task. |
4593 | * | |
4594 | * There are circumstances in which we can try to wake a task which has already | |
41a2d6cf | 4595 | * started to run but is not in state TASK_RUNNING. try_to_wake_up() returns |
1da177e4 LT |
4596 | * zero in this (rare) case, and we handle it by continuing to scan the queue. |
4597 | */ | |
78ddb08f | 4598 | static void __wake_up_common(wait_queue_head_t *q, unsigned int mode, |
63859d4f | 4599 | int nr_exclusive, int wake_flags, void *key) |
1da177e4 | 4600 | { |
2e45874c | 4601 | wait_queue_t *curr, *next; |
1da177e4 | 4602 | |
2e45874c | 4603 | list_for_each_entry_safe(curr, next, &q->task_list, task_list) { |
48f24c4d IM |
4604 | unsigned flags = curr->flags; |
4605 | ||
63859d4f | 4606 | if (curr->func(curr, mode, wake_flags, key) && |
48f24c4d | 4607 | (flags & WQ_FLAG_EXCLUSIVE) && !--nr_exclusive) |
1da177e4 LT |
4608 | break; |
4609 | } | |
4610 | } | |
4611 | ||
4612 | /** | |
4613 | * __wake_up - wake up threads blocked on a waitqueue. | |
4614 | * @q: the waitqueue | |
4615 | * @mode: which threads | |
4616 | * @nr_exclusive: how many wake-one or wake-many threads to wake up | |
67be2dd1 | 4617 | * @key: is directly passed to the wakeup function |
50fa610a DH |
4618 | * |
4619 | * It may be assumed that this function implies a write memory barrier before | |
4620 | * changing the task state if and only if any tasks are woken up. | |
1da177e4 | 4621 | */ |
7ad5b3a5 | 4622 | void __wake_up(wait_queue_head_t *q, unsigned int mode, |
95cdf3b7 | 4623 | int nr_exclusive, void *key) |
1da177e4 LT |
4624 | { |
4625 | unsigned long flags; | |
4626 | ||
4627 | spin_lock_irqsave(&q->lock, flags); | |
4628 | __wake_up_common(q, mode, nr_exclusive, 0, key); | |
4629 | spin_unlock_irqrestore(&q->lock, flags); | |
4630 | } | |
1da177e4 LT |
4631 | EXPORT_SYMBOL(__wake_up); |
4632 | ||
4633 | /* | |
4634 | * Same as __wake_up but called with the spinlock in wait_queue_head_t held. | |
4635 | */ | |
7ad5b3a5 | 4636 | void __wake_up_locked(wait_queue_head_t *q, unsigned int mode) |
1da177e4 LT |
4637 | { |
4638 | __wake_up_common(q, mode, 1, 0, NULL); | |
4639 | } | |
22c43c81 | 4640 | EXPORT_SYMBOL_GPL(__wake_up_locked); |
1da177e4 | 4641 | |
4ede816a DL |
4642 | void __wake_up_locked_key(wait_queue_head_t *q, unsigned int mode, void *key) |
4643 | { | |
4644 | __wake_up_common(q, mode, 1, 0, key); | |
4645 | } | |
bf294b41 | 4646 | EXPORT_SYMBOL_GPL(__wake_up_locked_key); |
4ede816a | 4647 | |
1da177e4 | 4648 | /** |
4ede816a | 4649 | * __wake_up_sync_key - wake up threads blocked on a waitqueue. |
1da177e4 LT |
4650 | * @q: the waitqueue |
4651 | * @mode: which threads | |
4652 | * @nr_exclusive: how many wake-one or wake-many threads to wake up | |
4ede816a | 4653 | * @key: opaque value to be passed to wakeup targets |
1da177e4 LT |
4654 | * |
4655 | * The sync wakeup differs that the waker knows that it will schedule | |
4656 | * away soon, so while the target thread will be woken up, it will not | |
4657 | * be migrated to another CPU - ie. the two threads are 'synchronized' | |
4658 | * with each other. This can prevent needless bouncing between CPUs. | |
4659 | * | |
4660 | * On UP it can prevent extra preemption. | |
50fa610a DH |
4661 | * |
4662 | * It may be assumed that this function implies a write memory barrier before | |
4663 | * changing the task state if and only if any tasks are woken up. | |
1da177e4 | 4664 | */ |
4ede816a DL |
4665 | void __wake_up_sync_key(wait_queue_head_t *q, unsigned int mode, |
4666 | int nr_exclusive, void *key) | |
1da177e4 LT |
4667 | { |
4668 | unsigned long flags; | |
7d478721 | 4669 | int wake_flags = WF_SYNC; |
1da177e4 LT |
4670 | |
4671 | if (unlikely(!q)) | |
4672 | return; | |
4673 | ||
4674 | if (unlikely(!nr_exclusive)) | |
7d478721 | 4675 | wake_flags = 0; |
1da177e4 LT |
4676 | |
4677 | spin_lock_irqsave(&q->lock, flags); | |
7d478721 | 4678 | __wake_up_common(q, mode, nr_exclusive, wake_flags, key); |
1da177e4 LT |
4679 | spin_unlock_irqrestore(&q->lock, flags); |
4680 | } | |
4ede816a DL |
4681 | EXPORT_SYMBOL_GPL(__wake_up_sync_key); |
4682 | ||
4683 | /* | |
4684 | * __wake_up_sync - see __wake_up_sync_key() | |
4685 | */ | |
4686 | void __wake_up_sync(wait_queue_head_t *q, unsigned int mode, int nr_exclusive) | |
4687 | { | |
4688 | __wake_up_sync_key(q, mode, nr_exclusive, NULL); | |
4689 | } | |
1da177e4 LT |
4690 | EXPORT_SYMBOL_GPL(__wake_up_sync); /* For internal use only */ |
4691 | ||
65eb3dc6 KD |
4692 | /** |
4693 | * complete: - signals a single thread waiting on this completion | |
4694 | * @x: holds the state of this particular completion | |
4695 | * | |
4696 | * This will wake up a single thread waiting on this completion. Threads will be | |
4697 | * awakened in the same order in which they were queued. | |
4698 | * | |
4699 | * See also complete_all(), wait_for_completion() and related routines. | |
50fa610a DH |
4700 | * |
4701 | * It may be assumed that this function implies a write memory barrier before | |
4702 | * changing the task state if and only if any tasks are woken up. | |
65eb3dc6 | 4703 | */ |
b15136e9 | 4704 | void complete(struct completion *x) |
1da177e4 LT |
4705 | { |
4706 | unsigned long flags; | |
4707 | ||
4708 | spin_lock_irqsave(&x->wait.lock, flags); | |
4709 | x->done++; | |
d9514f6c | 4710 | __wake_up_common(&x->wait, TASK_NORMAL, 1, 0, NULL); |
1da177e4 LT |
4711 | spin_unlock_irqrestore(&x->wait.lock, flags); |
4712 | } | |
4713 | EXPORT_SYMBOL(complete); | |
4714 | ||
65eb3dc6 KD |
4715 | /** |
4716 | * complete_all: - signals all threads waiting on this completion | |
4717 | * @x: holds the state of this particular completion | |
4718 | * | |
4719 | * This will wake up all threads waiting on this particular completion event. | |
50fa610a DH |
4720 | * |
4721 | * It may be assumed that this function implies a write memory barrier before | |
4722 | * changing the task state if and only if any tasks are woken up. | |
65eb3dc6 | 4723 | */ |
b15136e9 | 4724 | void complete_all(struct completion *x) |
1da177e4 LT |
4725 | { |
4726 | unsigned long flags; | |
4727 | ||
4728 | spin_lock_irqsave(&x->wait.lock, flags); | |
4729 | x->done += UINT_MAX/2; | |
d9514f6c | 4730 | __wake_up_common(&x->wait, TASK_NORMAL, 0, 0, NULL); |
1da177e4 LT |
4731 | spin_unlock_irqrestore(&x->wait.lock, flags); |
4732 | } | |
4733 | EXPORT_SYMBOL(complete_all); | |
4734 | ||
8cbbe86d AK |
4735 | static inline long __sched |
4736 | do_wait_for_common(struct completion *x, long timeout, int state) | |
1da177e4 | 4737 | { |
1da177e4 LT |
4738 | if (!x->done) { |
4739 | DECLARE_WAITQUEUE(wait, current); | |
4740 | ||
a93d2f17 | 4741 | __add_wait_queue_tail_exclusive(&x->wait, &wait); |
1da177e4 | 4742 | do { |
94d3d824 | 4743 | if (signal_pending_state(state, current)) { |
ea71a546 ON |
4744 | timeout = -ERESTARTSYS; |
4745 | break; | |
8cbbe86d AK |
4746 | } |
4747 | __set_current_state(state); | |
1da177e4 LT |
4748 | spin_unlock_irq(&x->wait.lock); |
4749 | timeout = schedule_timeout(timeout); | |
4750 | spin_lock_irq(&x->wait.lock); | |
ea71a546 | 4751 | } while (!x->done && timeout); |
1da177e4 | 4752 | __remove_wait_queue(&x->wait, &wait); |
ea71a546 ON |
4753 | if (!x->done) |
4754 | return timeout; | |
1da177e4 LT |
4755 | } |
4756 | x->done--; | |
ea71a546 | 4757 | return timeout ?: 1; |
1da177e4 | 4758 | } |
1da177e4 | 4759 | |
8cbbe86d AK |
4760 | static long __sched |
4761 | wait_for_common(struct completion *x, long timeout, int state) | |
1da177e4 | 4762 | { |
1da177e4 LT |
4763 | might_sleep(); |
4764 | ||
4765 | spin_lock_irq(&x->wait.lock); | |
8cbbe86d | 4766 | timeout = do_wait_for_common(x, timeout, state); |
1da177e4 | 4767 | spin_unlock_irq(&x->wait.lock); |
8cbbe86d AK |
4768 | return timeout; |
4769 | } | |
1da177e4 | 4770 | |
65eb3dc6 KD |
4771 | /** |
4772 | * wait_for_completion: - waits for completion of a task | |
4773 | * @x: holds the state of this particular completion | |
4774 | * | |
4775 | * This waits to be signaled for completion of a specific task. It is NOT | |
4776 | * interruptible and there is no timeout. | |
4777 | * | |
4778 | * See also similar routines (i.e. wait_for_completion_timeout()) with timeout | |
4779 | * and interrupt capability. Also see complete(). | |
4780 | */ | |
b15136e9 | 4781 | void __sched wait_for_completion(struct completion *x) |
8cbbe86d AK |
4782 | { |
4783 | wait_for_common(x, MAX_SCHEDULE_TIMEOUT, TASK_UNINTERRUPTIBLE); | |
1da177e4 | 4784 | } |
8cbbe86d | 4785 | EXPORT_SYMBOL(wait_for_completion); |
1da177e4 | 4786 | |
65eb3dc6 KD |
4787 | /** |
4788 | * wait_for_completion_timeout: - waits for completion of a task (w/timeout) | |
4789 | * @x: holds the state of this particular completion | |
4790 | * @timeout: timeout value in jiffies | |
4791 | * | |
4792 | * This waits for either a completion of a specific task to be signaled or for a | |
4793 | * specified timeout to expire. The timeout is in jiffies. It is not | |
4794 | * interruptible. | |
4795 | */ | |
b15136e9 | 4796 | unsigned long __sched |
8cbbe86d | 4797 | wait_for_completion_timeout(struct completion *x, unsigned long timeout) |
1da177e4 | 4798 | { |
8cbbe86d | 4799 | return wait_for_common(x, timeout, TASK_UNINTERRUPTIBLE); |
1da177e4 | 4800 | } |
8cbbe86d | 4801 | EXPORT_SYMBOL(wait_for_completion_timeout); |
1da177e4 | 4802 | |
65eb3dc6 KD |
4803 | /** |
4804 | * wait_for_completion_interruptible: - waits for completion of a task (w/intr) | |
4805 | * @x: holds the state of this particular completion | |
4806 | * | |
4807 | * This waits for completion of a specific task to be signaled. It is | |
4808 | * interruptible. | |
4809 | */ | |
8cbbe86d | 4810 | int __sched wait_for_completion_interruptible(struct completion *x) |
0fec171c | 4811 | { |
51e97990 AK |
4812 | long t = wait_for_common(x, MAX_SCHEDULE_TIMEOUT, TASK_INTERRUPTIBLE); |
4813 | if (t == -ERESTARTSYS) | |
4814 | return t; | |
4815 | return 0; | |
0fec171c | 4816 | } |
8cbbe86d | 4817 | EXPORT_SYMBOL(wait_for_completion_interruptible); |
1da177e4 | 4818 | |
65eb3dc6 KD |
4819 | /** |
4820 | * wait_for_completion_interruptible_timeout: - waits for completion (w/(to,intr)) | |
4821 | * @x: holds the state of this particular completion | |
4822 | * @timeout: timeout value in jiffies | |
4823 | * | |
4824 | * This waits for either a completion of a specific task to be signaled or for a | |
4825 | * specified timeout to expire. It is interruptible. The timeout is in jiffies. | |
4826 | */ | |
6bf41237 | 4827 | long __sched |
8cbbe86d AK |
4828 | wait_for_completion_interruptible_timeout(struct completion *x, |
4829 | unsigned long timeout) | |
0fec171c | 4830 | { |
8cbbe86d | 4831 | return wait_for_common(x, timeout, TASK_INTERRUPTIBLE); |
0fec171c | 4832 | } |
8cbbe86d | 4833 | EXPORT_SYMBOL(wait_for_completion_interruptible_timeout); |
1da177e4 | 4834 | |
65eb3dc6 KD |
4835 | /** |
4836 | * wait_for_completion_killable: - waits for completion of a task (killable) | |
4837 | * @x: holds the state of this particular completion | |
4838 | * | |
4839 | * This waits to be signaled for completion of a specific task. It can be | |
4840 | * interrupted by a kill signal. | |
4841 | */ | |
009e577e MW |
4842 | int __sched wait_for_completion_killable(struct completion *x) |
4843 | { | |
4844 | long t = wait_for_common(x, MAX_SCHEDULE_TIMEOUT, TASK_KILLABLE); | |
4845 | if (t == -ERESTARTSYS) | |
4846 | return t; | |
4847 | return 0; | |
4848 | } | |
4849 | EXPORT_SYMBOL(wait_for_completion_killable); | |
4850 | ||
0aa12fb4 SW |
4851 | /** |
4852 | * wait_for_completion_killable_timeout: - waits for completion of a task (w/(to,killable)) | |
4853 | * @x: holds the state of this particular completion | |
4854 | * @timeout: timeout value in jiffies | |
4855 | * | |
4856 | * This waits for either a completion of a specific task to be | |
4857 | * signaled or for a specified timeout to expire. It can be | |
4858 | * interrupted by a kill signal. The timeout is in jiffies. | |
4859 | */ | |
6bf41237 | 4860 | long __sched |
0aa12fb4 SW |
4861 | wait_for_completion_killable_timeout(struct completion *x, |
4862 | unsigned long timeout) | |
4863 | { | |
4864 | return wait_for_common(x, timeout, TASK_KILLABLE); | |
4865 | } | |
4866 | EXPORT_SYMBOL(wait_for_completion_killable_timeout); | |
4867 | ||
be4de352 DC |
4868 | /** |
4869 | * try_wait_for_completion - try to decrement a completion without blocking | |
4870 | * @x: completion structure | |
4871 | * | |
4872 | * Returns: 0 if a decrement cannot be done without blocking | |
4873 | * 1 if a decrement succeeded. | |
4874 | * | |
4875 | * If a completion is being used as a counting completion, | |
4876 | * attempt to decrement the counter without blocking. This | |
4877 | * enables us to avoid waiting if the resource the completion | |
4878 | * is protecting is not available. | |
4879 | */ | |
4880 | bool try_wait_for_completion(struct completion *x) | |
4881 | { | |
7539a3b3 | 4882 | unsigned long flags; |
be4de352 DC |
4883 | int ret = 1; |
4884 | ||
7539a3b3 | 4885 | spin_lock_irqsave(&x->wait.lock, flags); |
be4de352 DC |
4886 | if (!x->done) |
4887 | ret = 0; | |
4888 | else | |
4889 | x->done--; | |
7539a3b3 | 4890 | spin_unlock_irqrestore(&x->wait.lock, flags); |
be4de352 DC |
4891 | return ret; |
4892 | } | |
4893 | EXPORT_SYMBOL(try_wait_for_completion); | |
4894 | ||
4895 | /** | |
4896 | * completion_done - Test to see if a completion has any waiters | |
4897 | * @x: completion structure | |
4898 | * | |
4899 | * Returns: 0 if there are waiters (wait_for_completion() in progress) | |
4900 | * 1 if there are no waiters. | |
4901 | * | |
4902 | */ | |
4903 | bool completion_done(struct completion *x) | |
4904 | { | |
7539a3b3 | 4905 | unsigned long flags; |
be4de352 DC |
4906 | int ret = 1; |
4907 | ||
7539a3b3 | 4908 | spin_lock_irqsave(&x->wait.lock, flags); |
be4de352 DC |
4909 | if (!x->done) |
4910 | ret = 0; | |
7539a3b3 | 4911 | spin_unlock_irqrestore(&x->wait.lock, flags); |
be4de352 DC |
4912 | return ret; |
4913 | } | |
4914 | EXPORT_SYMBOL(completion_done); | |
4915 | ||
8cbbe86d AK |
4916 | static long __sched |
4917 | sleep_on_common(wait_queue_head_t *q, int state, long timeout) | |
1da177e4 | 4918 | { |
0fec171c IM |
4919 | unsigned long flags; |
4920 | wait_queue_t wait; | |
4921 | ||
4922 | init_waitqueue_entry(&wait, current); | |
1da177e4 | 4923 | |
8cbbe86d | 4924 | __set_current_state(state); |
1da177e4 | 4925 | |
8cbbe86d AK |
4926 | spin_lock_irqsave(&q->lock, flags); |
4927 | __add_wait_queue(q, &wait); | |
4928 | spin_unlock(&q->lock); | |
4929 | timeout = schedule_timeout(timeout); | |
4930 | spin_lock_irq(&q->lock); | |
4931 | __remove_wait_queue(q, &wait); | |
4932 | spin_unlock_irqrestore(&q->lock, flags); | |
4933 | ||
4934 | return timeout; | |
4935 | } | |
4936 | ||
4937 | void __sched interruptible_sleep_on(wait_queue_head_t *q) | |
4938 | { | |
4939 | sleep_on_common(q, TASK_INTERRUPTIBLE, MAX_SCHEDULE_TIMEOUT); | |
1da177e4 | 4940 | } |
1da177e4 LT |
4941 | EXPORT_SYMBOL(interruptible_sleep_on); |
4942 | ||
0fec171c | 4943 | long __sched |
95cdf3b7 | 4944 | interruptible_sleep_on_timeout(wait_queue_head_t *q, long timeout) |
1da177e4 | 4945 | { |
8cbbe86d | 4946 | return sleep_on_common(q, TASK_INTERRUPTIBLE, timeout); |
1da177e4 | 4947 | } |
1da177e4 LT |
4948 | EXPORT_SYMBOL(interruptible_sleep_on_timeout); |
4949 | ||
0fec171c | 4950 | void __sched sleep_on(wait_queue_head_t *q) |
1da177e4 | 4951 | { |
8cbbe86d | 4952 | sleep_on_common(q, TASK_UNINTERRUPTIBLE, MAX_SCHEDULE_TIMEOUT); |
1da177e4 | 4953 | } |
1da177e4 LT |
4954 | EXPORT_SYMBOL(sleep_on); |
4955 | ||
0fec171c | 4956 | long __sched sleep_on_timeout(wait_queue_head_t *q, long timeout) |
1da177e4 | 4957 | { |
8cbbe86d | 4958 | return sleep_on_common(q, TASK_UNINTERRUPTIBLE, timeout); |
1da177e4 | 4959 | } |
1da177e4 LT |
4960 | EXPORT_SYMBOL(sleep_on_timeout); |
4961 | ||
b29739f9 IM |
4962 | #ifdef CONFIG_RT_MUTEXES |
4963 | ||
4964 | /* | |
4965 | * rt_mutex_setprio - set the current priority of a task | |
4966 | * @p: task | |
4967 | * @prio: prio value (kernel-internal form) | |
4968 | * | |
4969 | * This function changes the 'effective' priority of a task. It does | |
4970 | * not touch ->normal_prio like __setscheduler(). | |
4971 | * | |
4972 | * Used by the rt_mutex code to implement priority inheritance logic. | |
4973 | */ | |
36c8b586 | 4974 | void rt_mutex_setprio(struct task_struct *p, int prio) |
b29739f9 | 4975 | { |
83b699ed | 4976 | int oldprio, on_rq, running; |
70b97a7f | 4977 | struct rq *rq; |
83ab0aa0 | 4978 | const struct sched_class *prev_class; |
b29739f9 IM |
4979 | |
4980 | BUG_ON(prio < 0 || prio > MAX_PRIO); | |
4981 | ||
0122ec5b | 4982 | rq = __task_rq_lock(p); |
b29739f9 | 4983 | |
a8027073 | 4984 | trace_sched_pi_setprio(p, prio); |
d5f9f942 | 4985 | oldprio = p->prio; |
83ab0aa0 | 4986 | prev_class = p->sched_class; |
fd2f4419 | 4987 | on_rq = p->on_rq; |
051a1d1a | 4988 | running = task_current(rq, p); |
0e1f3483 | 4989 | if (on_rq) |
69be72c1 | 4990 | dequeue_task(rq, p, 0); |
0e1f3483 HS |
4991 | if (running) |
4992 | p->sched_class->put_prev_task(rq, p); | |
dd41f596 IM |
4993 | |
4994 | if (rt_prio(prio)) | |
4995 | p->sched_class = &rt_sched_class; | |
4996 | else | |
4997 | p->sched_class = &fair_sched_class; | |
4998 | ||
b29739f9 IM |
4999 | p->prio = prio; |
5000 | ||
0e1f3483 HS |
5001 | if (running) |
5002 | p->sched_class->set_curr_task(rq); | |
da7a735e | 5003 | if (on_rq) |
371fd7e7 | 5004 | enqueue_task(rq, p, oldprio < prio ? ENQUEUE_HEAD : 0); |
cb469845 | 5005 | |
da7a735e | 5006 | check_class_changed(rq, p, prev_class, oldprio); |
0122ec5b | 5007 | __task_rq_unlock(rq); |
b29739f9 IM |
5008 | } |
5009 | ||
5010 | #endif | |
5011 | ||
36c8b586 | 5012 | void set_user_nice(struct task_struct *p, long nice) |
1da177e4 | 5013 | { |
dd41f596 | 5014 | int old_prio, delta, on_rq; |
1da177e4 | 5015 | unsigned long flags; |
70b97a7f | 5016 | struct rq *rq; |
1da177e4 LT |
5017 | |
5018 | if (TASK_NICE(p) == nice || nice < -20 || nice > 19) | |
5019 | return; | |
5020 | /* | |
5021 | * We have to be careful, if called from sys_setpriority(), | |
5022 | * the task might be in the middle of scheduling on another CPU. | |
5023 | */ | |
5024 | rq = task_rq_lock(p, &flags); | |
5025 | /* | |
5026 | * The RT priorities are set via sched_setscheduler(), but we still | |
5027 | * allow the 'normal' nice value to be set - but as expected | |
5028 | * it wont have any effect on scheduling until the task is | |
dd41f596 | 5029 | * SCHED_FIFO/SCHED_RR: |
1da177e4 | 5030 | */ |
e05606d3 | 5031 | if (task_has_rt_policy(p)) { |
1da177e4 LT |
5032 | p->static_prio = NICE_TO_PRIO(nice); |
5033 | goto out_unlock; | |
5034 | } | |
fd2f4419 | 5035 | on_rq = p->on_rq; |
c09595f6 | 5036 | if (on_rq) |
69be72c1 | 5037 | dequeue_task(rq, p, 0); |
1da177e4 | 5038 | |
1da177e4 | 5039 | p->static_prio = NICE_TO_PRIO(nice); |
2dd73a4f | 5040 | set_load_weight(p); |
b29739f9 IM |
5041 | old_prio = p->prio; |
5042 | p->prio = effective_prio(p); | |
5043 | delta = p->prio - old_prio; | |
1da177e4 | 5044 | |
dd41f596 | 5045 | if (on_rq) { |
371fd7e7 | 5046 | enqueue_task(rq, p, 0); |
1da177e4 | 5047 | /* |
d5f9f942 AM |
5048 | * If the task increased its priority or is running and |
5049 | * lowered its priority, then reschedule its CPU: | |
1da177e4 | 5050 | */ |
d5f9f942 | 5051 | if (delta < 0 || (delta > 0 && task_running(rq, p))) |
1da177e4 LT |
5052 | resched_task(rq->curr); |
5053 | } | |
5054 | out_unlock: | |
0122ec5b | 5055 | task_rq_unlock(rq, p, &flags); |
1da177e4 | 5056 | } |
1da177e4 LT |
5057 | EXPORT_SYMBOL(set_user_nice); |
5058 | ||
e43379f1 MM |
5059 | /* |
5060 | * can_nice - check if a task can reduce its nice value | |
5061 | * @p: task | |
5062 | * @nice: nice value | |
5063 | */ | |
36c8b586 | 5064 | int can_nice(const struct task_struct *p, const int nice) |
e43379f1 | 5065 | { |
024f4747 MM |
5066 | /* convert nice value [19,-20] to rlimit style value [1,40] */ |
5067 | int nice_rlim = 20 - nice; | |
48f24c4d | 5068 | |
78d7d407 | 5069 | return (nice_rlim <= task_rlimit(p, RLIMIT_NICE) || |
e43379f1 MM |
5070 | capable(CAP_SYS_NICE)); |
5071 | } | |
5072 | ||
1da177e4 LT |
5073 | #ifdef __ARCH_WANT_SYS_NICE |
5074 | ||
5075 | /* | |
5076 | * sys_nice - change the priority of the current process. | |
5077 | * @increment: priority increment | |
5078 | * | |
5079 | * sys_setpriority is a more generic, but much slower function that | |
5080 | * does similar things. | |
5081 | */ | |
5add95d4 | 5082 | SYSCALL_DEFINE1(nice, int, increment) |
1da177e4 | 5083 | { |
48f24c4d | 5084 | long nice, retval; |
1da177e4 LT |
5085 | |
5086 | /* | |
5087 | * Setpriority might change our priority at the same moment. | |
5088 | * We don't have to worry. Conceptually one call occurs first | |
5089 | * and we have a single winner. | |
5090 | */ | |
e43379f1 MM |
5091 | if (increment < -40) |
5092 | increment = -40; | |
1da177e4 LT |
5093 | if (increment > 40) |
5094 | increment = 40; | |
5095 | ||
2b8f836f | 5096 | nice = TASK_NICE(current) + increment; |
1da177e4 LT |
5097 | if (nice < -20) |
5098 | nice = -20; | |
5099 | if (nice > 19) | |
5100 | nice = 19; | |
5101 | ||
e43379f1 MM |
5102 | if (increment < 0 && !can_nice(current, nice)) |
5103 | return -EPERM; | |
5104 | ||
1da177e4 LT |
5105 | retval = security_task_setnice(current, nice); |
5106 | if (retval) | |
5107 | return retval; | |
5108 | ||
5109 | set_user_nice(current, nice); | |
5110 | return 0; | |
5111 | } | |
5112 | ||
5113 | #endif | |
5114 | ||
5115 | /** | |
5116 | * task_prio - return the priority value of a given task. | |
5117 | * @p: the task in question. | |
5118 | * | |
5119 | * This is the priority value as seen by users in /proc. | |
5120 | * RT tasks are offset by -200. Normal tasks are centered | |
5121 | * around 0, value goes from -16 to +15. | |
5122 | */ | |
36c8b586 | 5123 | int task_prio(const struct task_struct *p) |
1da177e4 LT |
5124 | { |
5125 | return p->prio - MAX_RT_PRIO; | |
5126 | } | |
5127 | ||
5128 | /** | |
5129 | * task_nice - return the nice value of a given task. | |
5130 | * @p: the task in question. | |
5131 | */ | |
36c8b586 | 5132 | int task_nice(const struct task_struct *p) |
1da177e4 LT |
5133 | { |
5134 | return TASK_NICE(p); | |
5135 | } | |
150d8bed | 5136 | EXPORT_SYMBOL(task_nice); |
1da177e4 LT |
5137 | |
5138 | /** | |
5139 | * idle_cpu - is a given cpu idle currently? | |
5140 | * @cpu: the processor in question. | |
5141 | */ | |
5142 | int idle_cpu(int cpu) | |
5143 | { | |
5144 | return cpu_curr(cpu) == cpu_rq(cpu)->idle; | |
5145 | } | |
5146 | ||
1da177e4 LT |
5147 | /** |
5148 | * idle_task - return the idle task for a given cpu. | |
5149 | * @cpu: the processor in question. | |
5150 | */ | |
36c8b586 | 5151 | struct task_struct *idle_task(int cpu) |
1da177e4 LT |
5152 | { |
5153 | return cpu_rq(cpu)->idle; | |
5154 | } | |
5155 | ||
5156 | /** | |
5157 | * find_process_by_pid - find a process with a matching PID value. | |
5158 | * @pid: the pid in question. | |
5159 | */ | |
a9957449 | 5160 | static struct task_struct *find_process_by_pid(pid_t pid) |
1da177e4 | 5161 | { |
228ebcbe | 5162 | return pid ? find_task_by_vpid(pid) : current; |
1da177e4 LT |
5163 | } |
5164 | ||
5165 | /* Actually do priority change: must hold rq lock. */ | |
dd41f596 IM |
5166 | static void |
5167 | __setscheduler(struct rq *rq, struct task_struct *p, int policy, int prio) | |
1da177e4 | 5168 | { |
1da177e4 LT |
5169 | p->policy = policy; |
5170 | p->rt_priority = prio; | |
b29739f9 IM |
5171 | p->normal_prio = normal_prio(p); |
5172 | /* we are holding p->pi_lock already */ | |
5173 | p->prio = rt_mutex_getprio(p); | |
ffd44db5 PZ |
5174 | if (rt_prio(p->prio)) |
5175 | p->sched_class = &rt_sched_class; | |
5176 | else | |
5177 | p->sched_class = &fair_sched_class; | |
2dd73a4f | 5178 | set_load_weight(p); |
1da177e4 LT |
5179 | } |
5180 | ||
c69e8d9c DH |
5181 | /* |
5182 | * check the target process has a UID that matches the current process's | |
5183 | */ | |
5184 | static bool check_same_owner(struct task_struct *p) | |
5185 | { | |
5186 | const struct cred *cred = current_cred(), *pcred; | |
5187 | bool match; | |
5188 | ||
5189 | rcu_read_lock(); | |
5190 | pcred = __task_cred(p); | |
b0e77598 SH |
5191 | if (cred->user->user_ns == pcred->user->user_ns) |
5192 | match = (cred->euid == pcred->euid || | |
5193 | cred->euid == pcred->uid); | |
5194 | else | |
5195 | match = false; | |
c69e8d9c DH |
5196 | rcu_read_unlock(); |
5197 | return match; | |
5198 | } | |
5199 | ||
961ccddd | 5200 | static int __sched_setscheduler(struct task_struct *p, int policy, |
fe7de49f | 5201 | const struct sched_param *param, bool user) |
1da177e4 | 5202 | { |
83b699ed | 5203 | int retval, oldprio, oldpolicy = -1, on_rq, running; |
1da177e4 | 5204 | unsigned long flags; |
83ab0aa0 | 5205 | const struct sched_class *prev_class; |
70b97a7f | 5206 | struct rq *rq; |
ca94c442 | 5207 | int reset_on_fork; |
1da177e4 | 5208 | |
66e5393a SR |
5209 | /* may grab non-irq protected spin_locks */ |
5210 | BUG_ON(in_interrupt()); | |
1da177e4 LT |
5211 | recheck: |
5212 | /* double check policy once rq lock held */ | |
ca94c442 LP |
5213 | if (policy < 0) { |
5214 | reset_on_fork = p->sched_reset_on_fork; | |
1da177e4 | 5215 | policy = oldpolicy = p->policy; |
ca94c442 LP |
5216 | } else { |
5217 | reset_on_fork = !!(policy & SCHED_RESET_ON_FORK); | |
5218 | policy &= ~SCHED_RESET_ON_FORK; | |
5219 | ||
5220 | if (policy != SCHED_FIFO && policy != SCHED_RR && | |
5221 | policy != SCHED_NORMAL && policy != SCHED_BATCH && | |
5222 | policy != SCHED_IDLE) | |
5223 | return -EINVAL; | |
5224 | } | |
5225 | ||
1da177e4 LT |
5226 | /* |
5227 | * Valid priorities for SCHED_FIFO and SCHED_RR are | |
dd41f596 IM |
5228 | * 1..MAX_USER_RT_PRIO-1, valid priority for SCHED_NORMAL, |
5229 | * SCHED_BATCH and SCHED_IDLE is 0. | |
1da177e4 LT |
5230 | */ |
5231 | if (param->sched_priority < 0 || | |
95cdf3b7 | 5232 | (p->mm && param->sched_priority > MAX_USER_RT_PRIO-1) || |
d46523ea | 5233 | (!p->mm && param->sched_priority > MAX_RT_PRIO-1)) |
1da177e4 | 5234 | return -EINVAL; |
e05606d3 | 5235 | if (rt_policy(policy) != (param->sched_priority != 0)) |
1da177e4 LT |
5236 | return -EINVAL; |
5237 | ||
37e4ab3f OC |
5238 | /* |
5239 | * Allow unprivileged RT tasks to decrease priority: | |
5240 | */ | |
961ccddd | 5241 | if (user && !capable(CAP_SYS_NICE)) { |
e05606d3 | 5242 | if (rt_policy(policy)) { |
a44702e8 ON |
5243 | unsigned long rlim_rtprio = |
5244 | task_rlimit(p, RLIMIT_RTPRIO); | |
8dc3e909 ON |
5245 | |
5246 | /* can't set/change the rt policy */ | |
5247 | if (policy != p->policy && !rlim_rtprio) | |
5248 | return -EPERM; | |
5249 | ||
5250 | /* can't increase priority */ | |
5251 | if (param->sched_priority > p->rt_priority && | |
5252 | param->sched_priority > rlim_rtprio) | |
5253 | return -EPERM; | |
5254 | } | |
c02aa73b | 5255 | |
dd41f596 | 5256 | /* |
c02aa73b DH |
5257 | * Treat SCHED_IDLE as nice 20. Only allow a switch to |
5258 | * SCHED_NORMAL if the RLIMIT_NICE would normally permit it. | |
dd41f596 | 5259 | */ |
c02aa73b DH |
5260 | if (p->policy == SCHED_IDLE && policy != SCHED_IDLE) { |
5261 | if (!can_nice(p, TASK_NICE(p))) | |
5262 | return -EPERM; | |
5263 | } | |
5fe1d75f | 5264 | |
37e4ab3f | 5265 | /* can't change other user's priorities */ |
c69e8d9c | 5266 | if (!check_same_owner(p)) |
37e4ab3f | 5267 | return -EPERM; |
ca94c442 LP |
5268 | |
5269 | /* Normal users shall not reset the sched_reset_on_fork flag */ | |
5270 | if (p->sched_reset_on_fork && !reset_on_fork) | |
5271 | return -EPERM; | |
37e4ab3f | 5272 | } |
1da177e4 | 5273 | |
725aad24 | 5274 | if (user) { |
b0ae1981 | 5275 | retval = security_task_setscheduler(p); |
725aad24 JF |
5276 | if (retval) |
5277 | return retval; | |
5278 | } | |
5279 | ||
b29739f9 IM |
5280 | /* |
5281 | * make sure no PI-waiters arrive (or leave) while we are | |
5282 | * changing the priority of the task: | |
0122ec5b | 5283 | * |
25985edc | 5284 | * To be able to change p->policy safely, the appropriate |
1da177e4 LT |
5285 | * runqueue lock must be held. |
5286 | */ | |
0122ec5b | 5287 | rq = task_rq_lock(p, &flags); |
dc61b1d6 | 5288 | |
34f971f6 PZ |
5289 | /* |
5290 | * Changing the policy of the stop threads its a very bad idea | |
5291 | */ | |
5292 | if (p == rq->stop) { | |
0122ec5b | 5293 | task_rq_unlock(rq, p, &flags); |
34f971f6 PZ |
5294 | return -EINVAL; |
5295 | } | |
5296 | ||
a51e9198 DF |
5297 | /* |
5298 | * If not changing anything there's no need to proceed further: | |
5299 | */ | |
5300 | if (unlikely(policy == p->policy && (!rt_policy(policy) || | |
5301 | param->sched_priority == p->rt_priority))) { | |
5302 | ||
5303 | __task_rq_unlock(rq); | |
5304 | raw_spin_unlock_irqrestore(&p->pi_lock, flags); | |
5305 | return 0; | |
5306 | } | |
5307 | ||
dc61b1d6 PZ |
5308 | #ifdef CONFIG_RT_GROUP_SCHED |
5309 | if (user) { | |
5310 | /* | |
5311 | * Do not allow realtime tasks into groups that have no runtime | |
5312 | * assigned. | |
5313 | */ | |
5314 | if (rt_bandwidth_enabled() && rt_policy(policy) && | |
f4493771 MG |
5315 | task_group(p)->rt_bandwidth.rt_runtime == 0 && |
5316 | !task_group_is_autogroup(task_group(p))) { | |
0122ec5b | 5317 | task_rq_unlock(rq, p, &flags); |
dc61b1d6 PZ |
5318 | return -EPERM; |
5319 | } | |
5320 | } | |
5321 | #endif | |
5322 | ||
1da177e4 LT |
5323 | /* recheck policy now with rq lock held */ |
5324 | if (unlikely(oldpolicy != -1 && oldpolicy != p->policy)) { | |
5325 | policy = oldpolicy = -1; | |
0122ec5b | 5326 | task_rq_unlock(rq, p, &flags); |
1da177e4 LT |
5327 | goto recheck; |
5328 | } | |
fd2f4419 | 5329 | on_rq = p->on_rq; |
051a1d1a | 5330 | running = task_current(rq, p); |
0e1f3483 | 5331 | if (on_rq) |
2e1cb74a | 5332 | deactivate_task(rq, p, 0); |
0e1f3483 HS |
5333 | if (running) |
5334 | p->sched_class->put_prev_task(rq, p); | |
f6b53205 | 5335 | |
ca94c442 LP |
5336 | p->sched_reset_on_fork = reset_on_fork; |
5337 | ||
1da177e4 | 5338 | oldprio = p->prio; |
83ab0aa0 | 5339 | prev_class = p->sched_class; |
dd41f596 | 5340 | __setscheduler(rq, p, policy, param->sched_priority); |
f6b53205 | 5341 | |
0e1f3483 HS |
5342 | if (running) |
5343 | p->sched_class->set_curr_task(rq); | |
da7a735e | 5344 | if (on_rq) |
dd41f596 | 5345 | activate_task(rq, p, 0); |
cb469845 | 5346 | |
da7a735e | 5347 | check_class_changed(rq, p, prev_class, oldprio); |
0122ec5b | 5348 | task_rq_unlock(rq, p, &flags); |
b29739f9 | 5349 | |
95e02ca9 TG |
5350 | rt_mutex_adjust_pi(p); |
5351 | ||
1da177e4 LT |
5352 | return 0; |
5353 | } | |
961ccddd RR |
5354 | |
5355 | /** | |
5356 | * sched_setscheduler - change the scheduling policy and/or RT priority of a thread. | |
5357 | * @p: the task in question. | |
5358 | * @policy: new policy. | |
5359 | * @param: structure containing the new RT priority. | |
5360 | * | |
5361 | * NOTE that the task may be already dead. | |
5362 | */ | |
5363 | int sched_setscheduler(struct task_struct *p, int policy, | |
fe7de49f | 5364 | const struct sched_param *param) |
961ccddd RR |
5365 | { |
5366 | return __sched_setscheduler(p, policy, param, true); | |
5367 | } | |
1da177e4 LT |
5368 | EXPORT_SYMBOL_GPL(sched_setscheduler); |
5369 | ||
961ccddd RR |
5370 | /** |
5371 | * sched_setscheduler_nocheck - change the scheduling policy and/or RT priority of a thread from kernelspace. | |
5372 | * @p: the task in question. | |
5373 | * @policy: new policy. | |
5374 | * @param: structure containing the new RT priority. | |
5375 | * | |
5376 | * Just like sched_setscheduler, only don't bother checking if the | |
5377 | * current context has permission. For example, this is needed in | |
5378 | * stop_machine(): we create temporary high priority worker threads, | |
5379 | * but our caller might not have that capability. | |
5380 | */ | |
5381 | int sched_setscheduler_nocheck(struct task_struct *p, int policy, | |
fe7de49f | 5382 | const struct sched_param *param) |
961ccddd RR |
5383 | { |
5384 | return __sched_setscheduler(p, policy, param, false); | |
5385 | } | |
5386 | ||
95cdf3b7 IM |
5387 | static int |
5388 | do_sched_setscheduler(pid_t pid, int policy, struct sched_param __user *param) | |
1da177e4 | 5389 | { |
1da177e4 LT |
5390 | struct sched_param lparam; |
5391 | struct task_struct *p; | |
36c8b586 | 5392 | int retval; |
1da177e4 LT |
5393 | |
5394 | if (!param || pid < 0) | |
5395 | return -EINVAL; | |
5396 | if (copy_from_user(&lparam, param, sizeof(struct sched_param))) | |
5397 | return -EFAULT; | |
5fe1d75f ON |
5398 | |
5399 | rcu_read_lock(); | |
5400 | retval = -ESRCH; | |
1da177e4 | 5401 | p = find_process_by_pid(pid); |
5fe1d75f ON |
5402 | if (p != NULL) |
5403 | retval = sched_setscheduler(p, policy, &lparam); | |
5404 | rcu_read_unlock(); | |
36c8b586 | 5405 | |
1da177e4 LT |
5406 | return retval; |
5407 | } | |
5408 | ||
5409 | /** | |
5410 | * sys_sched_setscheduler - set/change the scheduler policy and RT priority | |
5411 | * @pid: the pid in question. | |
5412 | * @policy: new policy. | |
5413 | * @param: structure containing the new RT priority. | |
5414 | */ | |
5add95d4 HC |
5415 | SYSCALL_DEFINE3(sched_setscheduler, pid_t, pid, int, policy, |
5416 | struct sched_param __user *, param) | |
1da177e4 | 5417 | { |
c21761f1 JB |
5418 | /* negative values for policy are not valid */ |
5419 | if (policy < 0) | |
5420 | return -EINVAL; | |
5421 | ||
1da177e4 LT |
5422 | return do_sched_setscheduler(pid, policy, param); |
5423 | } | |
5424 | ||
5425 | /** | |
5426 | * sys_sched_setparam - set/change the RT priority of a thread | |
5427 | * @pid: the pid in question. | |
5428 | * @param: structure containing the new RT priority. | |
5429 | */ | |
5add95d4 | 5430 | SYSCALL_DEFINE2(sched_setparam, pid_t, pid, struct sched_param __user *, param) |
1da177e4 LT |
5431 | { |
5432 | return do_sched_setscheduler(pid, -1, param); | |
5433 | } | |
5434 | ||
5435 | /** | |
5436 | * sys_sched_getscheduler - get the policy (scheduling class) of a thread | |
5437 | * @pid: the pid in question. | |
5438 | */ | |
5add95d4 | 5439 | SYSCALL_DEFINE1(sched_getscheduler, pid_t, pid) |
1da177e4 | 5440 | { |
36c8b586 | 5441 | struct task_struct *p; |
3a5c359a | 5442 | int retval; |
1da177e4 LT |
5443 | |
5444 | if (pid < 0) | |
3a5c359a | 5445 | return -EINVAL; |
1da177e4 LT |
5446 | |
5447 | retval = -ESRCH; | |
5fe85be0 | 5448 | rcu_read_lock(); |
1da177e4 LT |
5449 | p = find_process_by_pid(pid); |
5450 | if (p) { | |
5451 | retval = security_task_getscheduler(p); | |
5452 | if (!retval) | |
ca94c442 LP |
5453 | retval = p->policy |
5454 | | (p->sched_reset_on_fork ? SCHED_RESET_ON_FORK : 0); | |
1da177e4 | 5455 | } |
5fe85be0 | 5456 | rcu_read_unlock(); |
1da177e4 LT |
5457 | return retval; |
5458 | } | |
5459 | ||
5460 | /** | |
ca94c442 | 5461 | * sys_sched_getparam - get the RT priority of a thread |
1da177e4 LT |
5462 | * @pid: the pid in question. |
5463 | * @param: structure containing the RT priority. | |
5464 | */ | |
5add95d4 | 5465 | SYSCALL_DEFINE2(sched_getparam, pid_t, pid, struct sched_param __user *, param) |
1da177e4 LT |
5466 | { |
5467 | struct sched_param lp; | |
36c8b586 | 5468 | struct task_struct *p; |
3a5c359a | 5469 | int retval; |
1da177e4 LT |
5470 | |
5471 | if (!param || pid < 0) | |
3a5c359a | 5472 | return -EINVAL; |
1da177e4 | 5473 | |
5fe85be0 | 5474 | rcu_read_lock(); |
1da177e4 LT |
5475 | p = find_process_by_pid(pid); |
5476 | retval = -ESRCH; | |
5477 | if (!p) | |
5478 | goto out_unlock; | |
5479 | ||
5480 | retval = security_task_getscheduler(p); | |
5481 | if (retval) | |
5482 | goto out_unlock; | |
5483 | ||
5484 | lp.sched_priority = p->rt_priority; | |
5fe85be0 | 5485 | rcu_read_unlock(); |
1da177e4 LT |
5486 | |
5487 | /* | |
5488 | * This one might sleep, we cannot do it with a spinlock held ... | |
5489 | */ | |
5490 | retval = copy_to_user(param, &lp, sizeof(*param)) ? -EFAULT : 0; | |
5491 | ||
1da177e4 LT |
5492 | return retval; |
5493 | ||
5494 | out_unlock: | |
5fe85be0 | 5495 | rcu_read_unlock(); |
1da177e4 LT |
5496 | return retval; |
5497 | } | |
5498 | ||
96f874e2 | 5499 | long sched_setaffinity(pid_t pid, const struct cpumask *in_mask) |
1da177e4 | 5500 | { |
5a16f3d3 | 5501 | cpumask_var_t cpus_allowed, new_mask; |
36c8b586 IM |
5502 | struct task_struct *p; |
5503 | int retval; | |
1da177e4 | 5504 | |
95402b38 | 5505 | get_online_cpus(); |
23f5d142 | 5506 | rcu_read_lock(); |
1da177e4 LT |
5507 | |
5508 | p = find_process_by_pid(pid); | |
5509 | if (!p) { | |
23f5d142 | 5510 | rcu_read_unlock(); |
95402b38 | 5511 | put_online_cpus(); |
1da177e4 LT |
5512 | return -ESRCH; |
5513 | } | |
5514 | ||
23f5d142 | 5515 | /* Prevent p going away */ |
1da177e4 | 5516 | get_task_struct(p); |
23f5d142 | 5517 | rcu_read_unlock(); |
1da177e4 | 5518 | |
5a16f3d3 RR |
5519 | if (!alloc_cpumask_var(&cpus_allowed, GFP_KERNEL)) { |
5520 | retval = -ENOMEM; | |
5521 | goto out_put_task; | |
5522 | } | |
5523 | if (!alloc_cpumask_var(&new_mask, GFP_KERNEL)) { | |
5524 | retval = -ENOMEM; | |
5525 | goto out_free_cpus_allowed; | |
5526 | } | |
1da177e4 | 5527 | retval = -EPERM; |
b0e77598 | 5528 | if (!check_same_owner(p) && !task_ns_capable(p, CAP_SYS_NICE)) |
1da177e4 LT |
5529 | goto out_unlock; |
5530 | ||
b0ae1981 | 5531 | retval = security_task_setscheduler(p); |
e7834f8f DQ |
5532 | if (retval) |
5533 | goto out_unlock; | |
5534 | ||
5a16f3d3 RR |
5535 | cpuset_cpus_allowed(p, cpus_allowed); |
5536 | cpumask_and(new_mask, in_mask, cpus_allowed); | |
49246274 | 5537 | again: |
5a16f3d3 | 5538 | retval = set_cpus_allowed_ptr(p, new_mask); |
1da177e4 | 5539 | |
8707d8b8 | 5540 | if (!retval) { |
5a16f3d3 RR |
5541 | cpuset_cpus_allowed(p, cpus_allowed); |
5542 | if (!cpumask_subset(new_mask, cpus_allowed)) { | |
8707d8b8 PM |
5543 | /* |
5544 | * We must have raced with a concurrent cpuset | |
5545 | * update. Just reset the cpus_allowed to the | |
5546 | * cpuset's cpus_allowed | |
5547 | */ | |
5a16f3d3 | 5548 | cpumask_copy(new_mask, cpus_allowed); |
8707d8b8 PM |
5549 | goto again; |
5550 | } | |
5551 | } | |
1da177e4 | 5552 | out_unlock: |
5a16f3d3 RR |
5553 | free_cpumask_var(new_mask); |
5554 | out_free_cpus_allowed: | |
5555 | free_cpumask_var(cpus_allowed); | |
5556 | out_put_task: | |
1da177e4 | 5557 | put_task_struct(p); |
95402b38 | 5558 | put_online_cpus(); |
1da177e4 LT |
5559 | return retval; |
5560 | } | |
5561 | ||
5562 | static int get_user_cpu_mask(unsigned long __user *user_mask_ptr, unsigned len, | |
96f874e2 | 5563 | struct cpumask *new_mask) |
1da177e4 | 5564 | { |
96f874e2 RR |
5565 | if (len < cpumask_size()) |
5566 | cpumask_clear(new_mask); | |
5567 | else if (len > cpumask_size()) | |
5568 | len = cpumask_size(); | |
5569 | ||
1da177e4 LT |
5570 | return copy_from_user(new_mask, user_mask_ptr, len) ? -EFAULT : 0; |
5571 | } | |
5572 | ||
5573 | /** | |
5574 | * sys_sched_setaffinity - set the cpu affinity of a process | |
5575 | * @pid: pid of the process | |
5576 | * @len: length in bytes of the bitmask pointed to by user_mask_ptr | |
5577 | * @user_mask_ptr: user-space pointer to the new cpu mask | |
5578 | */ | |
5add95d4 HC |
5579 | SYSCALL_DEFINE3(sched_setaffinity, pid_t, pid, unsigned int, len, |
5580 | unsigned long __user *, user_mask_ptr) | |
1da177e4 | 5581 | { |
5a16f3d3 | 5582 | cpumask_var_t new_mask; |
1da177e4 LT |
5583 | int retval; |
5584 | ||
5a16f3d3 RR |
5585 | if (!alloc_cpumask_var(&new_mask, GFP_KERNEL)) |
5586 | return -ENOMEM; | |
1da177e4 | 5587 | |
5a16f3d3 RR |
5588 | retval = get_user_cpu_mask(user_mask_ptr, len, new_mask); |
5589 | if (retval == 0) | |
5590 | retval = sched_setaffinity(pid, new_mask); | |
5591 | free_cpumask_var(new_mask); | |
5592 | return retval; | |
1da177e4 LT |
5593 | } |
5594 | ||
96f874e2 | 5595 | long sched_getaffinity(pid_t pid, struct cpumask *mask) |
1da177e4 | 5596 | { |
36c8b586 | 5597 | struct task_struct *p; |
31605683 | 5598 | unsigned long flags; |
1da177e4 | 5599 | int retval; |
1da177e4 | 5600 | |
95402b38 | 5601 | get_online_cpus(); |
23f5d142 | 5602 | rcu_read_lock(); |
1da177e4 LT |
5603 | |
5604 | retval = -ESRCH; | |
5605 | p = find_process_by_pid(pid); | |
5606 | if (!p) | |
5607 | goto out_unlock; | |
5608 | ||
e7834f8f DQ |
5609 | retval = security_task_getscheduler(p); |
5610 | if (retval) | |
5611 | goto out_unlock; | |
5612 | ||
013fdb80 | 5613 | raw_spin_lock_irqsave(&p->pi_lock, flags); |
96f874e2 | 5614 | cpumask_and(mask, &p->cpus_allowed, cpu_online_mask); |
013fdb80 | 5615 | raw_spin_unlock_irqrestore(&p->pi_lock, flags); |
1da177e4 LT |
5616 | |
5617 | out_unlock: | |
23f5d142 | 5618 | rcu_read_unlock(); |
95402b38 | 5619 | put_online_cpus(); |
1da177e4 | 5620 | |
9531b62f | 5621 | return retval; |
1da177e4 LT |
5622 | } |
5623 | ||
5624 | /** | |
5625 | * sys_sched_getaffinity - get the cpu affinity of a process | |
5626 | * @pid: pid of the process | |
5627 | * @len: length in bytes of the bitmask pointed to by user_mask_ptr | |
5628 | * @user_mask_ptr: user-space pointer to hold the current cpu mask | |
5629 | */ | |
5add95d4 HC |
5630 | SYSCALL_DEFINE3(sched_getaffinity, pid_t, pid, unsigned int, len, |
5631 | unsigned long __user *, user_mask_ptr) | |
1da177e4 LT |
5632 | { |
5633 | int ret; | |
f17c8607 | 5634 | cpumask_var_t mask; |
1da177e4 | 5635 | |
84fba5ec | 5636 | if ((len * BITS_PER_BYTE) < nr_cpu_ids) |
cd3d8031 KM |
5637 | return -EINVAL; |
5638 | if (len & (sizeof(unsigned long)-1)) | |
1da177e4 LT |
5639 | return -EINVAL; |
5640 | ||
f17c8607 RR |
5641 | if (!alloc_cpumask_var(&mask, GFP_KERNEL)) |
5642 | return -ENOMEM; | |
1da177e4 | 5643 | |
f17c8607 RR |
5644 | ret = sched_getaffinity(pid, mask); |
5645 | if (ret == 0) { | |
8bc037fb | 5646 | size_t retlen = min_t(size_t, len, cpumask_size()); |
cd3d8031 KM |
5647 | |
5648 | if (copy_to_user(user_mask_ptr, mask, retlen)) | |
f17c8607 RR |
5649 | ret = -EFAULT; |
5650 | else | |
cd3d8031 | 5651 | ret = retlen; |
f17c8607 RR |
5652 | } |
5653 | free_cpumask_var(mask); | |
1da177e4 | 5654 | |
f17c8607 | 5655 | return ret; |
1da177e4 LT |
5656 | } |
5657 | ||
5658 | /** | |
5659 | * sys_sched_yield - yield the current processor to other threads. | |
5660 | * | |
dd41f596 IM |
5661 | * This function yields the current CPU to other tasks. If there are no |
5662 | * other threads running on this CPU then this function will return. | |
1da177e4 | 5663 | */ |
5add95d4 | 5664 | SYSCALL_DEFINE0(sched_yield) |
1da177e4 | 5665 | { |
70b97a7f | 5666 | struct rq *rq = this_rq_lock(); |
1da177e4 | 5667 | |
2d72376b | 5668 | schedstat_inc(rq, yld_count); |
4530d7ab | 5669 | current->sched_class->yield_task(rq); |
1da177e4 LT |
5670 | |
5671 | /* | |
5672 | * Since we are going to call schedule() anyway, there's | |
5673 | * no need to preempt or enable interrupts: | |
5674 | */ | |
5675 | __release(rq->lock); | |
8a25d5de | 5676 | spin_release(&rq->lock.dep_map, 1, _THIS_IP_); |
9828ea9d | 5677 | do_raw_spin_unlock(&rq->lock); |
1da177e4 LT |
5678 | preempt_enable_no_resched(); |
5679 | ||
5680 | schedule(); | |
5681 | ||
5682 | return 0; | |
5683 | } | |
5684 | ||
d86ee480 PZ |
5685 | static inline int should_resched(void) |
5686 | { | |
5687 | return need_resched() && !(preempt_count() & PREEMPT_ACTIVE); | |
5688 | } | |
5689 | ||
e7b38404 | 5690 | static void __cond_resched(void) |
1da177e4 | 5691 | { |
e7aaaa69 FW |
5692 | add_preempt_count(PREEMPT_ACTIVE); |
5693 | schedule(); | |
5694 | sub_preempt_count(PREEMPT_ACTIVE); | |
1da177e4 LT |
5695 | } |
5696 | ||
02b67cc3 | 5697 | int __sched _cond_resched(void) |
1da177e4 | 5698 | { |
d86ee480 | 5699 | if (should_resched()) { |
1da177e4 LT |
5700 | __cond_resched(); |
5701 | return 1; | |
5702 | } | |
5703 | return 0; | |
5704 | } | |
02b67cc3 | 5705 | EXPORT_SYMBOL(_cond_resched); |
1da177e4 LT |
5706 | |
5707 | /* | |
613afbf8 | 5708 | * __cond_resched_lock() - if a reschedule is pending, drop the given lock, |
1da177e4 LT |
5709 | * call schedule, and on return reacquire the lock. |
5710 | * | |
41a2d6cf | 5711 | * This works OK both with and without CONFIG_PREEMPT. We do strange low-level |
1da177e4 LT |
5712 | * operations here to prevent schedule() from being called twice (once via |
5713 | * spin_unlock(), once by hand). | |
5714 | */ | |
613afbf8 | 5715 | int __cond_resched_lock(spinlock_t *lock) |
1da177e4 | 5716 | { |
d86ee480 | 5717 | int resched = should_resched(); |
6df3cecb JK |
5718 | int ret = 0; |
5719 | ||
f607c668 PZ |
5720 | lockdep_assert_held(lock); |
5721 | ||
95c354fe | 5722 | if (spin_needbreak(lock) || resched) { |
1da177e4 | 5723 | spin_unlock(lock); |
d86ee480 | 5724 | if (resched) |
95c354fe NP |
5725 | __cond_resched(); |
5726 | else | |
5727 | cpu_relax(); | |
6df3cecb | 5728 | ret = 1; |
1da177e4 | 5729 | spin_lock(lock); |
1da177e4 | 5730 | } |
6df3cecb | 5731 | return ret; |
1da177e4 | 5732 | } |
613afbf8 | 5733 | EXPORT_SYMBOL(__cond_resched_lock); |
1da177e4 | 5734 | |
613afbf8 | 5735 | int __sched __cond_resched_softirq(void) |
1da177e4 LT |
5736 | { |
5737 | BUG_ON(!in_softirq()); | |
5738 | ||
d86ee480 | 5739 | if (should_resched()) { |
98d82567 | 5740 | local_bh_enable(); |
1da177e4 LT |
5741 | __cond_resched(); |
5742 | local_bh_disable(); | |
5743 | return 1; | |
5744 | } | |
5745 | return 0; | |
5746 | } | |
613afbf8 | 5747 | EXPORT_SYMBOL(__cond_resched_softirq); |
1da177e4 | 5748 | |
1da177e4 LT |
5749 | /** |
5750 | * yield - yield the current processor to other threads. | |
5751 | * | |
72fd4a35 | 5752 | * This is a shortcut for kernel-space yielding - it marks the |
1da177e4 LT |
5753 | * thread runnable and calls sys_sched_yield(). |
5754 | */ | |
5755 | void __sched yield(void) | |
5756 | { | |
5757 | set_current_state(TASK_RUNNING); | |
5758 | sys_sched_yield(); | |
5759 | } | |
1da177e4 LT |
5760 | EXPORT_SYMBOL(yield); |
5761 | ||
d95f4122 MG |
5762 | /** |
5763 | * yield_to - yield the current processor to another thread in | |
5764 | * your thread group, or accelerate that thread toward the | |
5765 | * processor it's on. | |
16addf95 RD |
5766 | * @p: target task |
5767 | * @preempt: whether task preemption is allowed or not | |
d95f4122 MG |
5768 | * |
5769 | * It's the caller's job to ensure that the target task struct | |
5770 | * can't go away on us before we can do any checks. | |
5771 | * | |
5772 | * Returns true if we indeed boosted the target task. | |
5773 | */ | |
5774 | bool __sched yield_to(struct task_struct *p, bool preempt) | |
5775 | { | |
5776 | struct task_struct *curr = current; | |
5777 | struct rq *rq, *p_rq; | |
5778 | unsigned long flags; | |
5779 | bool yielded = 0; | |
5780 | ||
5781 | local_irq_save(flags); | |
5782 | rq = this_rq(); | |
5783 | ||
5784 | again: | |
5785 | p_rq = task_rq(p); | |
5786 | double_rq_lock(rq, p_rq); | |
5787 | while (task_rq(p) != p_rq) { | |
5788 | double_rq_unlock(rq, p_rq); | |
5789 | goto again; | |
5790 | } | |
5791 | ||
5792 | if (!curr->sched_class->yield_to_task) | |
5793 | goto out; | |
5794 | ||
5795 | if (curr->sched_class != p->sched_class) | |
5796 | goto out; | |
5797 | ||
5798 | if (task_running(p_rq, p) || p->state) | |
5799 | goto out; | |
5800 | ||
5801 | yielded = curr->sched_class->yield_to_task(rq, p, preempt); | |
6d1cafd8 | 5802 | if (yielded) { |
d95f4122 | 5803 | schedstat_inc(rq, yld_count); |
6d1cafd8 VP |
5804 | /* |
5805 | * Make p's CPU reschedule; pick_next_entity takes care of | |
5806 | * fairness. | |
5807 | */ | |
5808 | if (preempt && rq != p_rq) | |
5809 | resched_task(p_rq->curr); | |
5810 | } | |
d95f4122 MG |
5811 | |
5812 | out: | |
5813 | double_rq_unlock(rq, p_rq); | |
5814 | local_irq_restore(flags); | |
5815 | ||
5816 | if (yielded) | |
5817 | schedule(); | |
5818 | ||
5819 | return yielded; | |
5820 | } | |
5821 | EXPORT_SYMBOL_GPL(yield_to); | |
5822 | ||
1da177e4 | 5823 | /* |
41a2d6cf | 5824 | * This task is about to go to sleep on IO. Increment rq->nr_iowait so |
1da177e4 | 5825 | * that process accounting knows that this is a task in IO wait state. |
1da177e4 LT |
5826 | */ |
5827 | void __sched io_schedule(void) | |
5828 | { | |
54d35f29 | 5829 | struct rq *rq = raw_rq(); |
1da177e4 | 5830 | |
0ff92245 | 5831 | delayacct_blkio_start(); |
1da177e4 | 5832 | atomic_inc(&rq->nr_iowait); |
73c10101 | 5833 | blk_flush_plug(current); |
8f0dfc34 | 5834 | current->in_iowait = 1; |
1da177e4 | 5835 | schedule(); |
8f0dfc34 | 5836 | current->in_iowait = 0; |
1da177e4 | 5837 | atomic_dec(&rq->nr_iowait); |
0ff92245 | 5838 | delayacct_blkio_end(); |
1da177e4 | 5839 | } |
1da177e4 LT |
5840 | EXPORT_SYMBOL(io_schedule); |
5841 | ||
5842 | long __sched io_schedule_timeout(long timeout) | |
5843 | { | |
54d35f29 | 5844 | struct rq *rq = raw_rq(); |
1da177e4 LT |
5845 | long ret; |
5846 | ||
0ff92245 | 5847 | delayacct_blkio_start(); |
1da177e4 | 5848 | atomic_inc(&rq->nr_iowait); |
73c10101 | 5849 | blk_flush_plug(current); |
8f0dfc34 | 5850 | current->in_iowait = 1; |
1da177e4 | 5851 | ret = schedule_timeout(timeout); |
8f0dfc34 | 5852 | current->in_iowait = 0; |
1da177e4 | 5853 | atomic_dec(&rq->nr_iowait); |
0ff92245 | 5854 | delayacct_blkio_end(); |
1da177e4 LT |
5855 | return ret; |
5856 | } | |
5857 | ||
5858 | /** | |
5859 | * sys_sched_get_priority_max - return maximum RT priority. | |
5860 | * @policy: scheduling class. | |
5861 | * | |
5862 | * this syscall returns the maximum rt_priority that can be used | |
5863 | * by a given scheduling class. | |
5864 | */ | |
5add95d4 | 5865 | SYSCALL_DEFINE1(sched_get_priority_max, int, policy) |
1da177e4 LT |
5866 | { |
5867 | int ret = -EINVAL; | |
5868 | ||
5869 | switch (policy) { | |
5870 | case SCHED_FIFO: | |
5871 | case SCHED_RR: | |
5872 | ret = MAX_USER_RT_PRIO-1; | |
5873 | break; | |
5874 | case SCHED_NORMAL: | |
b0a9499c | 5875 | case SCHED_BATCH: |
dd41f596 | 5876 | case SCHED_IDLE: |
1da177e4 LT |
5877 | ret = 0; |
5878 | break; | |
5879 | } | |
5880 | return ret; | |
5881 | } | |
5882 | ||
5883 | /** | |
5884 | * sys_sched_get_priority_min - return minimum RT priority. | |
5885 | * @policy: scheduling class. | |
5886 | * | |
5887 | * this syscall returns the minimum rt_priority that can be used | |
5888 | * by a given scheduling class. | |
5889 | */ | |
5add95d4 | 5890 | SYSCALL_DEFINE1(sched_get_priority_min, int, policy) |
1da177e4 LT |
5891 | { |
5892 | int ret = -EINVAL; | |
5893 | ||
5894 | switch (policy) { | |
5895 | case SCHED_FIFO: | |
5896 | case SCHED_RR: | |
5897 | ret = 1; | |
5898 | break; | |
5899 | case SCHED_NORMAL: | |
b0a9499c | 5900 | case SCHED_BATCH: |
dd41f596 | 5901 | case SCHED_IDLE: |
1da177e4 LT |
5902 | ret = 0; |
5903 | } | |
5904 | return ret; | |
5905 | } | |
5906 | ||
5907 | /** | |
5908 | * sys_sched_rr_get_interval - return the default timeslice of a process. | |
5909 | * @pid: pid of the process. | |
5910 | * @interval: userspace pointer to the timeslice value. | |
5911 | * | |
5912 | * this syscall writes the default timeslice value of a given process | |
5913 | * into the user-space timespec buffer. A value of '0' means infinity. | |
5914 | */ | |
17da2bd9 | 5915 | SYSCALL_DEFINE2(sched_rr_get_interval, pid_t, pid, |
754fe8d2 | 5916 | struct timespec __user *, interval) |
1da177e4 | 5917 | { |
36c8b586 | 5918 | struct task_struct *p; |
a4ec24b4 | 5919 | unsigned int time_slice; |
dba091b9 TG |
5920 | unsigned long flags; |
5921 | struct rq *rq; | |
3a5c359a | 5922 | int retval; |
1da177e4 | 5923 | struct timespec t; |
1da177e4 LT |
5924 | |
5925 | if (pid < 0) | |
3a5c359a | 5926 | return -EINVAL; |
1da177e4 LT |
5927 | |
5928 | retval = -ESRCH; | |
1a551ae7 | 5929 | rcu_read_lock(); |
1da177e4 LT |
5930 | p = find_process_by_pid(pid); |
5931 | if (!p) | |
5932 | goto out_unlock; | |
5933 | ||
5934 | retval = security_task_getscheduler(p); | |
5935 | if (retval) | |
5936 | goto out_unlock; | |
5937 | ||
dba091b9 TG |
5938 | rq = task_rq_lock(p, &flags); |
5939 | time_slice = p->sched_class->get_rr_interval(rq, p); | |
0122ec5b | 5940 | task_rq_unlock(rq, p, &flags); |
a4ec24b4 | 5941 | |
1a551ae7 | 5942 | rcu_read_unlock(); |
a4ec24b4 | 5943 | jiffies_to_timespec(time_slice, &t); |
1da177e4 | 5944 | retval = copy_to_user(interval, &t, sizeof(t)) ? -EFAULT : 0; |
1da177e4 | 5945 | return retval; |
3a5c359a | 5946 | |
1da177e4 | 5947 | out_unlock: |
1a551ae7 | 5948 | rcu_read_unlock(); |
1da177e4 LT |
5949 | return retval; |
5950 | } | |
5951 | ||
7c731e0a | 5952 | static const char stat_nam[] = TASK_STATE_TO_CHAR_STR; |
36c8b586 | 5953 | |
82a1fcb9 | 5954 | void sched_show_task(struct task_struct *p) |
1da177e4 | 5955 | { |
1da177e4 | 5956 | unsigned long free = 0; |
36c8b586 | 5957 | unsigned state; |
1da177e4 | 5958 | |
1da177e4 | 5959 | state = p->state ? __ffs(p->state) + 1 : 0; |
28d0686c | 5960 | printk(KERN_INFO "%-15.15s %c", p->comm, |
2ed6e34f | 5961 | state < sizeof(stat_nam) - 1 ? stat_nam[state] : '?'); |
4bd77321 | 5962 | #if BITS_PER_LONG == 32 |
1da177e4 | 5963 | if (state == TASK_RUNNING) |
3df0fc5b | 5964 | printk(KERN_CONT " running "); |
1da177e4 | 5965 | else |
3df0fc5b | 5966 | printk(KERN_CONT " %08lx ", thread_saved_pc(p)); |
1da177e4 LT |
5967 | #else |
5968 | if (state == TASK_RUNNING) | |
3df0fc5b | 5969 | printk(KERN_CONT " running task "); |
1da177e4 | 5970 | else |
3df0fc5b | 5971 | printk(KERN_CONT " %016lx ", thread_saved_pc(p)); |
1da177e4 LT |
5972 | #endif |
5973 | #ifdef CONFIG_DEBUG_STACK_USAGE | |
7c9f8861 | 5974 | free = stack_not_used(p); |
1da177e4 | 5975 | #endif |
3df0fc5b | 5976 | printk(KERN_CONT "%5lu %5d %6d 0x%08lx\n", free, |
aa47b7e0 DR |
5977 | task_pid_nr(p), task_pid_nr(p->real_parent), |
5978 | (unsigned long)task_thread_info(p)->flags); | |
1da177e4 | 5979 | |
5fb5e6de | 5980 | show_stack(p, NULL); |
1da177e4 LT |
5981 | } |
5982 | ||
e59e2ae2 | 5983 | void show_state_filter(unsigned long state_filter) |
1da177e4 | 5984 | { |
36c8b586 | 5985 | struct task_struct *g, *p; |
1da177e4 | 5986 | |
4bd77321 | 5987 | #if BITS_PER_LONG == 32 |
3df0fc5b PZ |
5988 | printk(KERN_INFO |
5989 | " task PC stack pid father\n"); | |
1da177e4 | 5990 | #else |
3df0fc5b PZ |
5991 | printk(KERN_INFO |
5992 | " task PC stack pid father\n"); | |
1da177e4 LT |
5993 | #endif |
5994 | read_lock(&tasklist_lock); | |
5995 | do_each_thread(g, p) { | |
5996 | /* | |
5997 | * reset the NMI-timeout, listing all files on a slow | |
25985edc | 5998 | * console might take a lot of time: |
1da177e4 LT |
5999 | */ |
6000 | touch_nmi_watchdog(); | |
39bc89fd | 6001 | if (!state_filter || (p->state & state_filter)) |
82a1fcb9 | 6002 | sched_show_task(p); |
1da177e4 LT |
6003 | } while_each_thread(g, p); |
6004 | ||
04c9167f JF |
6005 | touch_all_softlockup_watchdogs(); |
6006 | ||
dd41f596 IM |
6007 | #ifdef CONFIG_SCHED_DEBUG |
6008 | sysrq_sched_debug_show(); | |
6009 | #endif | |
1da177e4 | 6010 | read_unlock(&tasklist_lock); |
e59e2ae2 IM |
6011 | /* |
6012 | * Only show locks if all tasks are dumped: | |
6013 | */ | |
93335a21 | 6014 | if (!state_filter) |
e59e2ae2 | 6015 | debug_show_all_locks(); |
1da177e4 LT |
6016 | } |
6017 | ||
1df21055 IM |
6018 | void __cpuinit init_idle_bootup_task(struct task_struct *idle) |
6019 | { | |
dd41f596 | 6020 | idle->sched_class = &idle_sched_class; |
1df21055 IM |
6021 | } |
6022 | ||
f340c0d1 IM |
6023 | /** |
6024 | * init_idle - set up an idle thread for a given CPU | |
6025 | * @idle: task in question | |
6026 | * @cpu: cpu the idle task belongs to | |
6027 | * | |
6028 | * NOTE: this function does not set the idle thread's NEED_RESCHED | |
6029 | * flag, to make booting more robust. | |
6030 | */ | |
5c1e1767 | 6031 | void __cpuinit init_idle(struct task_struct *idle, int cpu) |
1da177e4 | 6032 | { |
70b97a7f | 6033 | struct rq *rq = cpu_rq(cpu); |
1da177e4 LT |
6034 | unsigned long flags; |
6035 | ||
05fa785c | 6036 | raw_spin_lock_irqsave(&rq->lock, flags); |
5cbd54ef | 6037 | |
dd41f596 | 6038 | __sched_fork(idle); |
06b83b5f | 6039 | idle->state = TASK_RUNNING; |
dd41f596 IM |
6040 | idle->se.exec_start = sched_clock(); |
6041 | ||
1e1b6c51 | 6042 | do_set_cpus_allowed(idle, cpumask_of(cpu)); |
6506cf6c PZ |
6043 | /* |
6044 | * We're having a chicken and egg problem, even though we are | |
6045 | * holding rq->lock, the cpu isn't yet set to this cpu so the | |
6046 | * lockdep check in task_group() will fail. | |
6047 | * | |
6048 | * Similar case to sched_fork(). / Alternatively we could | |
6049 | * use task_rq_lock() here and obtain the other rq->lock. | |
6050 | * | |
6051 | * Silence PROVE_RCU | |
6052 | */ | |
6053 | rcu_read_lock(); | |
dd41f596 | 6054 | __set_task_cpu(idle, cpu); |
6506cf6c | 6055 | rcu_read_unlock(); |
1da177e4 | 6056 | |
1da177e4 | 6057 | rq->curr = rq->idle = idle; |
3ca7a440 PZ |
6058 | #if defined(CONFIG_SMP) |
6059 | idle->on_cpu = 1; | |
4866cde0 | 6060 | #endif |
05fa785c | 6061 | raw_spin_unlock_irqrestore(&rq->lock, flags); |
1da177e4 LT |
6062 | |
6063 | /* Set the preempt count _outside_ the spinlocks! */ | |
a1261f54 | 6064 | task_thread_info(idle)->preempt_count = 0; |
625f2a37 | 6065 | |
dd41f596 IM |
6066 | /* |
6067 | * The idle tasks have their own, simple scheduling class: | |
6068 | */ | |
6069 | idle->sched_class = &idle_sched_class; | |
868baf07 | 6070 | ftrace_graph_init_idle_task(idle, cpu); |
1da177e4 LT |
6071 | } |
6072 | ||
6073 | /* | |
6074 | * In a system that switches off the HZ timer nohz_cpu_mask | |
6075 | * indicates which cpus entered this state. This is used | |
6076 | * in the rcu update to wait only for active cpus. For system | |
6077 | * which do not switch off the HZ timer nohz_cpu_mask should | |
6a7b3dc3 | 6078 | * always be CPU_BITS_NONE. |
1da177e4 | 6079 | */ |
6a7b3dc3 | 6080 | cpumask_var_t nohz_cpu_mask; |
1da177e4 | 6081 | |
19978ca6 IM |
6082 | /* |
6083 | * Increase the granularity value when there are more CPUs, | |
6084 | * because with more CPUs the 'effective latency' as visible | |
6085 | * to users decreases. But the relationship is not linear, | |
6086 | * so pick a second-best guess by going with the log2 of the | |
6087 | * number of CPUs. | |
6088 | * | |
6089 | * This idea comes from the SD scheduler of Con Kolivas: | |
6090 | */ | |
acb4a848 | 6091 | static int get_update_sysctl_factor(void) |
19978ca6 | 6092 | { |
4ca3ef71 | 6093 | unsigned int cpus = min_t(int, num_online_cpus(), 8); |
1983a922 CE |
6094 | unsigned int factor; |
6095 | ||
6096 | switch (sysctl_sched_tunable_scaling) { | |
6097 | case SCHED_TUNABLESCALING_NONE: | |
6098 | factor = 1; | |
6099 | break; | |
6100 | case SCHED_TUNABLESCALING_LINEAR: | |
6101 | factor = cpus; | |
6102 | break; | |
6103 | case SCHED_TUNABLESCALING_LOG: | |
6104 | default: | |
6105 | factor = 1 + ilog2(cpus); | |
6106 | break; | |
6107 | } | |
19978ca6 | 6108 | |
acb4a848 CE |
6109 | return factor; |
6110 | } | |
19978ca6 | 6111 | |
acb4a848 CE |
6112 | static void update_sysctl(void) |
6113 | { | |
6114 | unsigned int factor = get_update_sysctl_factor(); | |
19978ca6 | 6115 | |
0bcdcf28 CE |
6116 | #define SET_SYSCTL(name) \ |
6117 | (sysctl_##name = (factor) * normalized_sysctl_##name) | |
6118 | SET_SYSCTL(sched_min_granularity); | |
6119 | SET_SYSCTL(sched_latency); | |
6120 | SET_SYSCTL(sched_wakeup_granularity); | |
0bcdcf28 CE |
6121 | #undef SET_SYSCTL |
6122 | } | |
55cd5340 | 6123 | |
0bcdcf28 CE |
6124 | static inline void sched_init_granularity(void) |
6125 | { | |
6126 | update_sysctl(); | |
19978ca6 IM |
6127 | } |
6128 | ||
1da177e4 | 6129 | #ifdef CONFIG_SMP |
1e1b6c51 KM |
6130 | void do_set_cpus_allowed(struct task_struct *p, const struct cpumask *new_mask) |
6131 | { | |
6132 | if (p->sched_class && p->sched_class->set_cpus_allowed) | |
6133 | p->sched_class->set_cpus_allowed(p, new_mask); | |
6134 | else { | |
6135 | cpumask_copy(&p->cpus_allowed, new_mask); | |
6136 | p->rt.nr_cpus_allowed = cpumask_weight(new_mask); | |
6137 | } | |
6138 | } | |
6139 | ||
1da177e4 LT |
6140 | /* |
6141 | * This is how migration works: | |
6142 | * | |
969c7921 TH |
6143 | * 1) we invoke migration_cpu_stop() on the target CPU using |
6144 | * stop_one_cpu(). | |
6145 | * 2) stopper starts to run (implicitly forcing the migrated thread | |
6146 | * off the CPU) | |
6147 | * 3) it checks whether the migrated task is still in the wrong runqueue. | |
6148 | * 4) if it's in the wrong runqueue then the migration thread removes | |
1da177e4 | 6149 | * it and puts it into the right queue. |
969c7921 TH |
6150 | * 5) stopper completes and stop_one_cpu() returns and the migration |
6151 | * is done. | |
1da177e4 LT |
6152 | */ |
6153 | ||
6154 | /* | |
6155 | * Change a given task's CPU affinity. Migrate the thread to a | |
6156 | * proper CPU and schedule it away if the CPU it's executing on | |
6157 | * is removed from the allowed bitmask. | |
6158 | * | |
6159 | * NOTE: the caller must have a valid reference to the task, the | |
41a2d6cf | 6160 | * task must not exit() & deallocate itself prematurely. The |
1da177e4 LT |
6161 | * call is not atomic; no spinlocks may be held. |
6162 | */ | |
96f874e2 | 6163 | int set_cpus_allowed_ptr(struct task_struct *p, const struct cpumask *new_mask) |
1da177e4 LT |
6164 | { |
6165 | unsigned long flags; | |
70b97a7f | 6166 | struct rq *rq; |
969c7921 | 6167 | unsigned int dest_cpu; |
48f24c4d | 6168 | int ret = 0; |
1da177e4 LT |
6169 | |
6170 | rq = task_rq_lock(p, &flags); | |
e2912009 | 6171 | |
db44fc01 YZ |
6172 | if (cpumask_equal(&p->cpus_allowed, new_mask)) |
6173 | goto out; | |
6174 | ||
6ad4c188 | 6175 | if (!cpumask_intersects(new_mask, cpu_active_mask)) { |
1da177e4 LT |
6176 | ret = -EINVAL; |
6177 | goto out; | |
6178 | } | |
6179 | ||
db44fc01 | 6180 | if (unlikely((p->flags & PF_THREAD_BOUND) && p != current)) { |
9985b0ba DR |
6181 | ret = -EINVAL; |
6182 | goto out; | |
6183 | } | |
6184 | ||
1e1b6c51 | 6185 | do_set_cpus_allowed(p, new_mask); |
73fe6aae | 6186 | |
1da177e4 | 6187 | /* Can the task run on the task's current CPU? If so, we're done */ |
96f874e2 | 6188 | if (cpumask_test_cpu(task_cpu(p), new_mask)) |
1da177e4 LT |
6189 | goto out; |
6190 | ||
969c7921 | 6191 | dest_cpu = cpumask_any_and(cpu_active_mask, new_mask); |
bd8e7dde | 6192 | if (p->on_rq) { |
969c7921 | 6193 | struct migration_arg arg = { p, dest_cpu }; |
1da177e4 | 6194 | /* Need help from migration thread: drop lock and wait. */ |
0122ec5b | 6195 | task_rq_unlock(rq, p, &flags); |
969c7921 | 6196 | stop_one_cpu(cpu_of(rq), migration_cpu_stop, &arg); |
1da177e4 LT |
6197 | tlb_migrate_finish(p->mm); |
6198 | return 0; | |
6199 | } | |
6200 | out: | |
0122ec5b | 6201 | task_rq_unlock(rq, p, &flags); |
48f24c4d | 6202 | |
1da177e4 LT |
6203 | return ret; |
6204 | } | |
cd8ba7cd | 6205 | EXPORT_SYMBOL_GPL(set_cpus_allowed_ptr); |
1da177e4 LT |
6206 | |
6207 | /* | |
41a2d6cf | 6208 | * Move (not current) task off this cpu, onto dest cpu. We're doing |
1da177e4 LT |
6209 | * this because either it can't run here any more (set_cpus_allowed() |
6210 | * away from this CPU, or CPU going down), or because we're | |
6211 | * attempting to rebalance this task on exec (sched_exec). | |
6212 | * | |
6213 | * So we race with normal scheduler movements, but that's OK, as long | |
6214 | * as the task is no longer on this CPU. | |
efc30814 KK |
6215 | * |
6216 | * Returns non-zero if task was successfully migrated. | |
1da177e4 | 6217 | */ |
efc30814 | 6218 | static int __migrate_task(struct task_struct *p, int src_cpu, int dest_cpu) |
1da177e4 | 6219 | { |
70b97a7f | 6220 | struct rq *rq_dest, *rq_src; |
e2912009 | 6221 | int ret = 0; |
1da177e4 | 6222 | |
e761b772 | 6223 | if (unlikely(!cpu_active(dest_cpu))) |
efc30814 | 6224 | return ret; |
1da177e4 LT |
6225 | |
6226 | rq_src = cpu_rq(src_cpu); | |
6227 | rq_dest = cpu_rq(dest_cpu); | |
6228 | ||
0122ec5b | 6229 | raw_spin_lock(&p->pi_lock); |
1da177e4 LT |
6230 | double_rq_lock(rq_src, rq_dest); |
6231 | /* Already moved. */ | |
6232 | if (task_cpu(p) != src_cpu) | |
b1e38734 | 6233 | goto done; |
1da177e4 | 6234 | /* Affinity changed (again). */ |
96f874e2 | 6235 | if (!cpumask_test_cpu(dest_cpu, &p->cpus_allowed)) |
b1e38734 | 6236 | goto fail; |
1da177e4 | 6237 | |
e2912009 PZ |
6238 | /* |
6239 | * If we're not on a rq, the next wake-up will ensure we're | |
6240 | * placed properly. | |
6241 | */ | |
fd2f4419 | 6242 | if (p->on_rq) { |
2e1cb74a | 6243 | deactivate_task(rq_src, p, 0); |
e2912009 | 6244 | set_task_cpu(p, dest_cpu); |
dd41f596 | 6245 | activate_task(rq_dest, p, 0); |
15afe09b | 6246 | check_preempt_curr(rq_dest, p, 0); |
1da177e4 | 6247 | } |
b1e38734 | 6248 | done: |
efc30814 | 6249 | ret = 1; |
b1e38734 | 6250 | fail: |
1da177e4 | 6251 | double_rq_unlock(rq_src, rq_dest); |
0122ec5b | 6252 | raw_spin_unlock(&p->pi_lock); |
efc30814 | 6253 | return ret; |
1da177e4 LT |
6254 | } |
6255 | ||
6256 | /* | |
969c7921 TH |
6257 | * migration_cpu_stop - this will be executed by a highprio stopper thread |
6258 | * and performs thread migration by bumping thread off CPU then | |
6259 | * 'pushing' onto another runqueue. | |
1da177e4 | 6260 | */ |
969c7921 | 6261 | static int migration_cpu_stop(void *data) |
1da177e4 | 6262 | { |
969c7921 | 6263 | struct migration_arg *arg = data; |
f7b4cddc | 6264 | |
969c7921 TH |
6265 | /* |
6266 | * The original target cpu might have gone down and we might | |
6267 | * be on another cpu but it doesn't matter. | |
6268 | */ | |
f7b4cddc | 6269 | local_irq_disable(); |
969c7921 | 6270 | __migrate_task(arg->task, raw_smp_processor_id(), arg->dest_cpu); |
f7b4cddc | 6271 | local_irq_enable(); |
1da177e4 | 6272 | return 0; |
f7b4cddc ON |
6273 | } |
6274 | ||
1da177e4 | 6275 | #ifdef CONFIG_HOTPLUG_CPU |
48c5ccae | 6276 | |
054b9108 | 6277 | /* |
48c5ccae PZ |
6278 | * Ensures that the idle task is using init_mm right before its cpu goes |
6279 | * offline. | |
054b9108 | 6280 | */ |
48c5ccae | 6281 | void idle_task_exit(void) |
1da177e4 | 6282 | { |
48c5ccae | 6283 | struct mm_struct *mm = current->active_mm; |
e76bd8d9 | 6284 | |
48c5ccae | 6285 | BUG_ON(cpu_online(smp_processor_id())); |
e76bd8d9 | 6286 | |
48c5ccae PZ |
6287 | if (mm != &init_mm) |
6288 | switch_mm(mm, &init_mm, current); | |
6289 | mmdrop(mm); | |
1da177e4 LT |
6290 | } |
6291 | ||
6292 | /* | |
6293 | * While a dead CPU has no uninterruptible tasks queued at this point, | |
6294 | * it might still have a nonzero ->nr_uninterruptible counter, because | |
6295 | * for performance reasons the counter is not stricly tracking tasks to | |
6296 | * their home CPUs. So we just add the counter to another CPU's counter, | |
6297 | * to keep the global sum constant after CPU-down: | |
6298 | */ | |
70b97a7f | 6299 | static void migrate_nr_uninterruptible(struct rq *rq_src) |
1da177e4 | 6300 | { |
6ad4c188 | 6301 | struct rq *rq_dest = cpu_rq(cpumask_any(cpu_active_mask)); |
1da177e4 | 6302 | |
1da177e4 LT |
6303 | rq_dest->nr_uninterruptible += rq_src->nr_uninterruptible; |
6304 | rq_src->nr_uninterruptible = 0; | |
1da177e4 LT |
6305 | } |
6306 | ||
dd41f596 | 6307 | /* |
48c5ccae | 6308 | * remove the tasks which were accounted by rq from calc_load_tasks. |
1da177e4 | 6309 | */ |
48c5ccae | 6310 | static void calc_global_load_remove(struct rq *rq) |
1da177e4 | 6311 | { |
48c5ccae PZ |
6312 | atomic_long_sub(rq->calc_load_active, &calc_load_tasks); |
6313 | rq->calc_load_active = 0; | |
1da177e4 LT |
6314 | } |
6315 | ||
48f24c4d | 6316 | /* |
48c5ccae PZ |
6317 | * Migrate all tasks from the rq, sleeping tasks will be migrated by |
6318 | * try_to_wake_up()->select_task_rq(). | |
6319 | * | |
6320 | * Called with rq->lock held even though we'er in stop_machine() and | |
6321 | * there's no concurrency possible, we hold the required locks anyway | |
6322 | * because of lock validation efforts. | |
1da177e4 | 6323 | */ |
48c5ccae | 6324 | static void migrate_tasks(unsigned int dead_cpu) |
1da177e4 | 6325 | { |
70b97a7f | 6326 | struct rq *rq = cpu_rq(dead_cpu); |
48c5ccae PZ |
6327 | struct task_struct *next, *stop = rq->stop; |
6328 | int dest_cpu; | |
1da177e4 LT |
6329 | |
6330 | /* | |
48c5ccae PZ |
6331 | * Fudge the rq selection such that the below task selection loop |
6332 | * doesn't get stuck on the currently eligible stop task. | |
6333 | * | |
6334 | * We're currently inside stop_machine() and the rq is either stuck | |
6335 | * in the stop_machine_cpu_stop() loop, or we're executing this code, | |
6336 | * either way we should never end up calling schedule() until we're | |
6337 | * done here. | |
1da177e4 | 6338 | */ |
48c5ccae | 6339 | rq->stop = NULL; |
48f24c4d | 6340 | |
dd41f596 | 6341 | for ( ; ; ) { |
48c5ccae PZ |
6342 | /* |
6343 | * There's this thread running, bail when that's the only | |
6344 | * remaining thread. | |
6345 | */ | |
6346 | if (rq->nr_running == 1) | |
dd41f596 | 6347 | break; |
48c5ccae | 6348 | |
b67802ea | 6349 | next = pick_next_task(rq); |
48c5ccae | 6350 | BUG_ON(!next); |
79c53799 | 6351 | next->sched_class->put_prev_task(rq, next); |
e692ab53 | 6352 | |
48c5ccae PZ |
6353 | /* Find suitable destination for @next, with force if needed. */ |
6354 | dest_cpu = select_fallback_rq(dead_cpu, next); | |
6355 | raw_spin_unlock(&rq->lock); | |
6356 | ||
6357 | __migrate_task(next, dead_cpu, dest_cpu); | |
6358 | ||
6359 | raw_spin_lock(&rq->lock); | |
1da177e4 | 6360 | } |
dce48a84 | 6361 | |
48c5ccae | 6362 | rq->stop = stop; |
dce48a84 | 6363 | } |
48c5ccae | 6364 | |
1da177e4 LT |
6365 | #endif /* CONFIG_HOTPLUG_CPU */ |
6366 | ||
e692ab53 NP |
6367 | #if defined(CONFIG_SCHED_DEBUG) && defined(CONFIG_SYSCTL) |
6368 | ||
6369 | static struct ctl_table sd_ctl_dir[] = { | |
e0361851 AD |
6370 | { |
6371 | .procname = "sched_domain", | |
c57baf1e | 6372 | .mode = 0555, |
e0361851 | 6373 | }, |
56992309 | 6374 | {} |
e692ab53 NP |
6375 | }; |
6376 | ||
6377 | static struct ctl_table sd_ctl_root[] = { | |
e0361851 AD |
6378 | { |
6379 | .procname = "kernel", | |
c57baf1e | 6380 | .mode = 0555, |
e0361851 AD |
6381 | .child = sd_ctl_dir, |
6382 | }, | |
56992309 | 6383 | {} |
e692ab53 NP |
6384 | }; |
6385 | ||
6386 | static struct ctl_table *sd_alloc_ctl_entry(int n) | |
6387 | { | |
6388 | struct ctl_table *entry = | |
5cf9f062 | 6389 | kcalloc(n, sizeof(struct ctl_table), GFP_KERNEL); |
e692ab53 | 6390 | |
e692ab53 NP |
6391 | return entry; |
6392 | } | |
6393 | ||
6382bc90 MM |
6394 | static void sd_free_ctl_entry(struct ctl_table **tablep) |
6395 | { | |
cd790076 | 6396 | struct ctl_table *entry; |
6382bc90 | 6397 | |
cd790076 MM |
6398 | /* |
6399 | * In the intermediate directories, both the child directory and | |
6400 | * procname are dynamically allocated and could fail but the mode | |
41a2d6cf | 6401 | * will always be set. In the lowest directory the names are |
cd790076 MM |
6402 | * static strings and all have proc handlers. |
6403 | */ | |
6404 | for (entry = *tablep; entry->mode; entry++) { | |
6382bc90 MM |
6405 | if (entry->child) |
6406 | sd_free_ctl_entry(&entry->child); | |
cd790076 MM |
6407 | if (entry->proc_handler == NULL) |
6408 | kfree(entry->procname); | |
6409 | } | |
6382bc90 MM |
6410 | |
6411 | kfree(*tablep); | |
6412 | *tablep = NULL; | |
6413 | } | |
6414 | ||
e692ab53 | 6415 | static void |
e0361851 | 6416 | set_table_entry(struct ctl_table *entry, |
e692ab53 NP |
6417 | const char *procname, void *data, int maxlen, |
6418 | mode_t mode, proc_handler *proc_handler) | |
6419 | { | |
e692ab53 NP |
6420 | entry->procname = procname; |
6421 | entry->data = data; | |
6422 | entry->maxlen = maxlen; | |
6423 | entry->mode = mode; | |
6424 | entry->proc_handler = proc_handler; | |
6425 | } | |
6426 | ||
6427 | static struct ctl_table * | |
6428 | sd_alloc_ctl_domain_table(struct sched_domain *sd) | |
6429 | { | |
a5d8c348 | 6430 | struct ctl_table *table = sd_alloc_ctl_entry(13); |
e692ab53 | 6431 | |
ad1cdc1d MM |
6432 | if (table == NULL) |
6433 | return NULL; | |
6434 | ||
e0361851 | 6435 | set_table_entry(&table[0], "min_interval", &sd->min_interval, |
e692ab53 | 6436 | sizeof(long), 0644, proc_doulongvec_minmax); |
e0361851 | 6437 | set_table_entry(&table[1], "max_interval", &sd->max_interval, |
e692ab53 | 6438 | sizeof(long), 0644, proc_doulongvec_minmax); |
e0361851 | 6439 | set_table_entry(&table[2], "busy_idx", &sd->busy_idx, |
e692ab53 | 6440 | sizeof(int), 0644, proc_dointvec_minmax); |
e0361851 | 6441 | set_table_entry(&table[3], "idle_idx", &sd->idle_idx, |
e692ab53 | 6442 | sizeof(int), 0644, proc_dointvec_minmax); |
e0361851 | 6443 | set_table_entry(&table[4], "newidle_idx", &sd->newidle_idx, |
e692ab53 | 6444 | sizeof(int), 0644, proc_dointvec_minmax); |
e0361851 | 6445 | set_table_entry(&table[5], "wake_idx", &sd->wake_idx, |
e692ab53 | 6446 | sizeof(int), 0644, proc_dointvec_minmax); |
e0361851 | 6447 | set_table_entry(&table[6], "forkexec_idx", &sd->forkexec_idx, |
e692ab53 | 6448 | sizeof(int), 0644, proc_dointvec_minmax); |
e0361851 | 6449 | set_table_entry(&table[7], "busy_factor", &sd->busy_factor, |
e692ab53 | 6450 | sizeof(int), 0644, proc_dointvec_minmax); |
e0361851 | 6451 | set_table_entry(&table[8], "imbalance_pct", &sd->imbalance_pct, |
e692ab53 | 6452 | sizeof(int), 0644, proc_dointvec_minmax); |
ace8b3d6 | 6453 | set_table_entry(&table[9], "cache_nice_tries", |
e692ab53 NP |
6454 | &sd->cache_nice_tries, |
6455 | sizeof(int), 0644, proc_dointvec_minmax); | |
ace8b3d6 | 6456 | set_table_entry(&table[10], "flags", &sd->flags, |
e692ab53 | 6457 | sizeof(int), 0644, proc_dointvec_minmax); |
a5d8c348 IM |
6458 | set_table_entry(&table[11], "name", sd->name, |
6459 | CORENAME_MAX_SIZE, 0444, proc_dostring); | |
6460 | /* &table[12] is terminator */ | |
e692ab53 NP |
6461 | |
6462 | return table; | |
6463 | } | |
6464 | ||
9a4e7159 | 6465 | static ctl_table *sd_alloc_ctl_cpu_table(int cpu) |
e692ab53 NP |
6466 | { |
6467 | struct ctl_table *entry, *table; | |
6468 | struct sched_domain *sd; | |
6469 | int domain_num = 0, i; | |
6470 | char buf[32]; | |
6471 | ||
6472 | for_each_domain(cpu, sd) | |
6473 | domain_num++; | |
6474 | entry = table = sd_alloc_ctl_entry(domain_num + 1); | |
ad1cdc1d MM |
6475 | if (table == NULL) |
6476 | return NULL; | |
e692ab53 NP |
6477 | |
6478 | i = 0; | |
6479 | for_each_domain(cpu, sd) { | |
6480 | snprintf(buf, 32, "domain%d", i); | |
e692ab53 | 6481 | entry->procname = kstrdup(buf, GFP_KERNEL); |
c57baf1e | 6482 | entry->mode = 0555; |
e692ab53 NP |
6483 | entry->child = sd_alloc_ctl_domain_table(sd); |
6484 | entry++; | |
6485 | i++; | |
6486 | } | |
6487 | return table; | |
6488 | } | |
6489 | ||
6490 | static struct ctl_table_header *sd_sysctl_header; | |
6382bc90 | 6491 | static void register_sched_domain_sysctl(void) |
e692ab53 | 6492 | { |
6ad4c188 | 6493 | int i, cpu_num = num_possible_cpus(); |
e692ab53 NP |
6494 | struct ctl_table *entry = sd_alloc_ctl_entry(cpu_num + 1); |
6495 | char buf[32]; | |
6496 | ||
7378547f MM |
6497 | WARN_ON(sd_ctl_dir[0].child); |
6498 | sd_ctl_dir[0].child = entry; | |
6499 | ||
ad1cdc1d MM |
6500 | if (entry == NULL) |
6501 | return; | |
6502 | ||
6ad4c188 | 6503 | for_each_possible_cpu(i) { |
e692ab53 | 6504 | snprintf(buf, 32, "cpu%d", i); |
e692ab53 | 6505 | entry->procname = kstrdup(buf, GFP_KERNEL); |
c57baf1e | 6506 | entry->mode = 0555; |
e692ab53 | 6507 | entry->child = sd_alloc_ctl_cpu_table(i); |
97b6ea7b | 6508 | entry++; |
e692ab53 | 6509 | } |
7378547f MM |
6510 | |
6511 | WARN_ON(sd_sysctl_header); | |
e692ab53 NP |
6512 | sd_sysctl_header = register_sysctl_table(sd_ctl_root); |
6513 | } | |
6382bc90 | 6514 | |
7378547f | 6515 | /* may be called multiple times per register */ |
6382bc90 MM |
6516 | static void unregister_sched_domain_sysctl(void) |
6517 | { | |
7378547f MM |
6518 | if (sd_sysctl_header) |
6519 | unregister_sysctl_table(sd_sysctl_header); | |
6382bc90 | 6520 | sd_sysctl_header = NULL; |
7378547f MM |
6521 | if (sd_ctl_dir[0].child) |
6522 | sd_free_ctl_entry(&sd_ctl_dir[0].child); | |
6382bc90 | 6523 | } |
e692ab53 | 6524 | #else |
6382bc90 MM |
6525 | static void register_sched_domain_sysctl(void) |
6526 | { | |
6527 | } | |
6528 | static void unregister_sched_domain_sysctl(void) | |
e692ab53 NP |
6529 | { |
6530 | } | |
6531 | #endif | |
6532 | ||
1f11eb6a GH |
6533 | static void set_rq_online(struct rq *rq) |
6534 | { | |
6535 | if (!rq->online) { | |
6536 | const struct sched_class *class; | |
6537 | ||
c6c4927b | 6538 | cpumask_set_cpu(rq->cpu, rq->rd->online); |
1f11eb6a GH |
6539 | rq->online = 1; |
6540 | ||
6541 | for_each_class(class) { | |
6542 | if (class->rq_online) | |
6543 | class->rq_online(rq); | |
6544 | } | |
6545 | } | |
6546 | } | |
6547 | ||
6548 | static void set_rq_offline(struct rq *rq) | |
6549 | { | |
6550 | if (rq->online) { | |
6551 | const struct sched_class *class; | |
6552 | ||
6553 | for_each_class(class) { | |
6554 | if (class->rq_offline) | |
6555 | class->rq_offline(rq); | |
6556 | } | |
6557 | ||
c6c4927b | 6558 | cpumask_clear_cpu(rq->cpu, rq->rd->online); |
1f11eb6a GH |
6559 | rq->online = 0; |
6560 | } | |
6561 | } | |
6562 | ||
1da177e4 LT |
6563 | /* |
6564 | * migration_call - callback that gets triggered when a CPU is added. | |
6565 | * Here we can start up the necessary migration thread for the new CPU. | |
6566 | */ | |
48f24c4d IM |
6567 | static int __cpuinit |
6568 | migration_call(struct notifier_block *nfb, unsigned long action, void *hcpu) | |
1da177e4 | 6569 | { |
48f24c4d | 6570 | int cpu = (long)hcpu; |
1da177e4 | 6571 | unsigned long flags; |
969c7921 | 6572 | struct rq *rq = cpu_rq(cpu); |
1da177e4 | 6573 | |
48c5ccae | 6574 | switch (action & ~CPU_TASKS_FROZEN) { |
5be9361c | 6575 | |
1da177e4 | 6576 | case CPU_UP_PREPARE: |
a468d389 | 6577 | rq->calc_load_update = calc_load_update; |
1da177e4 | 6578 | break; |
48f24c4d | 6579 | |
1da177e4 | 6580 | case CPU_ONLINE: |
1f94ef59 | 6581 | /* Update our root-domain */ |
05fa785c | 6582 | raw_spin_lock_irqsave(&rq->lock, flags); |
1f94ef59 | 6583 | if (rq->rd) { |
c6c4927b | 6584 | BUG_ON(!cpumask_test_cpu(cpu, rq->rd->span)); |
1f11eb6a GH |
6585 | |
6586 | set_rq_online(rq); | |
1f94ef59 | 6587 | } |
05fa785c | 6588 | raw_spin_unlock_irqrestore(&rq->lock, flags); |
1da177e4 | 6589 | break; |
48f24c4d | 6590 | |
1da177e4 | 6591 | #ifdef CONFIG_HOTPLUG_CPU |
08f503b0 | 6592 | case CPU_DYING: |
317f3941 | 6593 | sched_ttwu_pending(); |
57d885fe | 6594 | /* Update our root-domain */ |
05fa785c | 6595 | raw_spin_lock_irqsave(&rq->lock, flags); |
57d885fe | 6596 | if (rq->rd) { |
c6c4927b | 6597 | BUG_ON(!cpumask_test_cpu(cpu, rq->rd->span)); |
1f11eb6a | 6598 | set_rq_offline(rq); |
57d885fe | 6599 | } |
48c5ccae PZ |
6600 | migrate_tasks(cpu); |
6601 | BUG_ON(rq->nr_running != 1); /* the migration thread */ | |
05fa785c | 6602 | raw_spin_unlock_irqrestore(&rq->lock, flags); |
48c5ccae PZ |
6603 | |
6604 | migrate_nr_uninterruptible(rq); | |
6605 | calc_global_load_remove(rq); | |
57d885fe | 6606 | break; |
1da177e4 LT |
6607 | #endif |
6608 | } | |
49c022e6 PZ |
6609 | |
6610 | update_max_interval(); | |
6611 | ||
1da177e4 LT |
6612 | return NOTIFY_OK; |
6613 | } | |
6614 | ||
f38b0820 PM |
6615 | /* |
6616 | * Register at high priority so that task migration (migrate_all_tasks) | |
6617 | * happens before everything else. This has to be lower priority than | |
cdd6c482 | 6618 | * the notifier in the perf_event subsystem, though. |
1da177e4 | 6619 | */ |
26c2143b | 6620 | static struct notifier_block __cpuinitdata migration_notifier = { |
1da177e4 | 6621 | .notifier_call = migration_call, |
50a323b7 | 6622 | .priority = CPU_PRI_MIGRATION, |
1da177e4 LT |
6623 | }; |
6624 | ||
3a101d05 TH |
6625 | static int __cpuinit sched_cpu_active(struct notifier_block *nfb, |
6626 | unsigned long action, void *hcpu) | |
6627 | { | |
6628 | switch (action & ~CPU_TASKS_FROZEN) { | |
6629 | case CPU_ONLINE: | |
6630 | case CPU_DOWN_FAILED: | |
6631 | set_cpu_active((long)hcpu, true); | |
6632 | return NOTIFY_OK; | |
6633 | default: | |
6634 | return NOTIFY_DONE; | |
6635 | } | |
6636 | } | |
6637 | ||
6638 | static int __cpuinit sched_cpu_inactive(struct notifier_block *nfb, | |
6639 | unsigned long action, void *hcpu) | |
6640 | { | |
6641 | switch (action & ~CPU_TASKS_FROZEN) { | |
6642 | case CPU_DOWN_PREPARE: | |
6643 | set_cpu_active((long)hcpu, false); | |
6644 | return NOTIFY_OK; | |
6645 | default: | |
6646 | return NOTIFY_DONE; | |
6647 | } | |
6648 | } | |
6649 | ||
7babe8db | 6650 | static int __init migration_init(void) |
1da177e4 LT |
6651 | { |
6652 | void *cpu = (void *)(long)smp_processor_id(); | |
07dccf33 | 6653 | int err; |
48f24c4d | 6654 | |
3a101d05 | 6655 | /* Initialize migration for the boot CPU */ |
07dccf33 AM |
6656 | err = migration_call(&migration_notifier, CPU_UP_PREPARE, cpu); |
6657 | BUG_ON(err == NOTIFY_BAD); | |
1da177e4 LT |
6658 | migration_call(&migration_notifier, CPU_ONLINE, cpu); |
6659 | register_cpu_notifier(&migration_notifier); | |
7babe8db | 6660 | |
3a101d05 TH |
6661 | /* Register cpu active notifiers */ |
6662 | cpu_notifier(sched_cpu_active, CPU_PRI_SCHED_ACTIVE); | |
6663 | cpu_notifier(sched_cpu_inactive, CPU_PRI_SCHED_INACTIVE); | |
6664 | ||
a004cd42 | 6665 | return 0; |
1da177e4 | 6666 | } |
7babe8db | 6667 | early_initcall(migration_init); |
1da177e4 LT |
6668 | #endif |
6669 | ||
6670 | #ifdef CONFIG_SMP | |
476f3534 | 6671 | |
4cb98839 PZ |
6672 | static cpumask_var_t sched_domains_tmpmask; /* sched_domains_mutex */ |
6673 | ||
3e9830dc | 6674 | #ifdef CONFIG_SCHED_DEBUG |
4dcf6aff | 6675 | |
f6630114 MT |
6676 | static __read_mostly int sched_domain_debug_enabled; |
6677 | ||
6678 | static int __init sched_domain_debug_setup(char *str) | |
6679 | { | |
6680 | sched_domain_debug_enabled = 1; | |
6681 | ||
6682 | return 0; | |
6683 | } | |
6684 | early_param("sched_debug", sched_domain_debug_setup); | |
6685 | ||
7c16ec58 | 6686 | static int sched_domain_debug_one(struct sched_domain *sd, int cpu, int level, |
96f874e2 | 6687 | struct cpumask *groupmask) |
1da177e4 | 6688 | { |
4dcf6aff | 6689 | struct sched_group *group = sd->groups; |
434d53b0 | 6690 | char str[256]; |
1da177e4 | 6691 | |
968ea6d8 | 6692 | cpulist_scnprintf(str, sizeof(str), sched_domain_span(sd)); |
96f874e2 | 6693 | cpumask_clear(groupmask); |
4dcf6aff IM |
6694 | |
6695 | printk(KERN_DEBUG "%*s domain %d: ", level, "", level); | |
6696 | ||
6697 | if (!(sd->flags & SD_LOAD_BALANCE)) { | |
3df0fc5b | 6698 | printk("does not load-balance\n"); |
4dcf6aff | 6699 | if (sd->parent) |
3df0fc5b PZ |
6700 | printk(KERN_ERR "ERROR: !SD_LOAD_BALANCE domain" |
6701 | " has parent"); | |
4dcf6aff | 6702 | return -1; |
41c7ce9a NP |
6703 | } |
6704 | ||
3df0fc5b | 6705 | printk(KERN_CONT "span %s level %s\n", str, sd->name); |
4dcf6aff | 6706 | |
758b2cdc | 6707 | if (!cpumask_test_cpu(cpu, sched_domain_span(sd))) { |
3df0fc5b PZ |
6708 | printk(KERN_ERR "ERROR: domain->span does not contain " |
6709 | "CPU%d\n", cpu); | |
4dcf6aff | 6710 | } |
758b2cdc | 6711 | if (!cpumask_test_cpu(cpu, sched_group_cpus(group))) { |
3df0fc5b PZ |
6712 | printk(KERN_ERR "ERROR: domain->groups does not contain" |
6713 | " CPU%d\n", cpu); | |
4dcf6aff | 6714 | } |
1da177e4 | 6715 | |
4dcf6aff | 6716 | printk(KERN_DEBUG "%*s groups:", level + 1, ""); |
1da177e4 | 6717 | do { |
4dcf6aff | 6718 | if (!group) { |
3df0fc5b PZ |
6719 | printk("\n"); |
6720 | printk(KERN_ERR "ERROR: group is NULL\n"); | |
1da177e4 LT |
6721 | break; |
6722 | } | |
6723 | ||
9c3f75cb | 6724 | if (!group->sgp->power) { |
3df0fc5b PZ |
6725 | printk(KERN_CONT "\n"); |
6726 | printk(KERN_ERR "ERROR: domain->cpu_power not " | |
6727 | "set\n"); | |
4dcf6aff IM |
6728 | break; |
6729 | } | |
1da177e4 | 6730 | |
758b2cdc | 6731 | if (!cpumask_weight(sched_group_cpus(group))) { |
3df0fc5b PZ |
6732 | printk(KERN_CONT "\n"); |
6733 | printk(KERN_ERR "ERROR: empty group\n"); | |
4dcf6aff IM |
6734 | break; |
6735 | } | |
1da177e4 | 6736 | |
758b2cdc | 6737 | if (cpumask_intersects(groupmask, sched_group_cpus(group))) { |
3df0fc5b PZ |
6738 | printk(KERN_CONT "\n"); |
6739 | printk(KERN_ERR "ERROR: repeated CPUs\n"); | |
4dcf6aff IM |
6740 | break; |
6741 | } | |
1da177e4 | 6742 | |
758b2cdc | 6743 | cpumask_or(groupmask, groupmask, sched_group_cpus(group)); |
1da177e4 | 6744 | |
968ea6d8 | 6745 | cpulist_scnprintf(str, sizeof(str), sched_group_cpus(group)); |
381512cf | 6746 | |
3df0fc5b | 6747 | printk(KERN_CONT " %s", str); |
9c3f75cb | 6748 | if (group->sgp->power != SCHED_POWER_SCALE) { |
3df0fc5b | 6749 | printk(KERN_CONT " (cpu_power = %d)", |
9c3f75cb | 6750 | group->sgp->power); |
381512cf | 6751 | } |
1da177e4 | 6752 | |
4dcf6aff IM |
6753 | group = group->next; |
6754 | } while (group != sd->groups); | |
3df0fc5b | 6755 | printk(KERN_CONT "\n"); |
1da177e4 | 6756 | |
758b2cdc | 6757 | if (!cpumask_equal(sched_domain_span(sd), groupmask)) |
3df0fc5b | 6758 | printk(KERN_ERR "ERROR: groups don't span domain->span\n"); |
1da177e4 | 6759 | |
758b2cdc RR |
6760 | if (sd->parent && |
6761 | !cpumask_subset(groupmask, sched_domain_span(sd->parent))) | |
3df0fc5b PZ |
6762 | printk(KERN_ERR "ERROR: parent span is not a superset " |
6763 | "of domain->span\n"); | |
4dcf6aff IM |
6764 | return 0; |
6765 | } | |
1da177e4 | 6766 | |
4dcf6aff IM |
6767 | static void sched_domain_debug(struct sched_domain *sd, int cpu) |
6768 | { | |
6769 | int level = 0; | |
1da177e4 | 6770 | |
f6630114 MT |
6771 | if (!sched_domain_debug_enabled) |
6772 | return; | |
6773 | ||
4dcf6aff IM |
6774 | if (!sd) { |
6775 | printk(KERN_DEBUG "CPU%d attaching NULL sched-domain.\n", cpu); | |
6776 | return; | |
6777 | } | |
1da177e4 | 6778 | |
4dcf6aff IM |
6779 | printk(KERN_DEBUG "CPU%d attaching sched-domain:\n", cpu); |
6780 | ||
6781 | for (;;) { | |
4cb98839 | 6782 | if (sched_domain_debug_one(sd, cpu, level, sched_domains_tmpmask)) |
4dcf6aff | 6783 | break; |
1da177e4 LT |
6784 | level++; |
6785 | sd = sd->parent; | |
33859f7f | 6786 | if (!sd) |
4dcf6aff IM |
6787 | break; |
6788 | } | |
1da177e4 | 6789 | } |
6d6bc0ad | 6790 | #else /* !CONFIG_SCHED_DEBUG */ |
48f24c4d | 6791 | # define sched_domain_debug(sd, cpu) do { } while (0) |
6d6bc0ad | 6792 | #endif /* CONFIG_SCHED_DEBUG */ |
1da177e4 | 6793 | |
1a20ff27 | 6794 | static int sd_degenerate(struct sched_domain *sd) |
245af2c7 | 6795 | { |
758b2cdc | 6796 | if (cpumask_weight(sched_domain_span(sd)) == 1) |
245af2c7 SS |
6797 | return 1; |
6798 | ||
6799 | /* Following flags need at least 2 groups */ | |
6800 | if (sd->flags & (SD_LOAD_BALANCE | | |
6801 | SD_BALANCE_NEWIDLE | | |
6802 | SD_BALANCE_FORK | | |
89c4710e SS |
6803 | SD_BALANCE_EXEC | |
6804 | SD_SHARE_CPUPOWER | | |
6805 | SD_SHARE_PKG_RESOURCES)) { | |
245af2c7 SS |
6806 | if (sd->groups != sd->groups->next) |
6807 | return 0; | |
6808 | } | |
6809 | ||
6810 | /* Following flags don't use groups */ | |
c88d5910 | 6811 | if (sd->flags & (SD_WAKE_AFFINE)) |
245af2c7 SS |
6812 | return 0; |
6813 | ||
6814 | return 1; | |
6815 | } | |
6816 | ||
48f24c4d IM |
6817 | static int |
6818 | sd_parent_degenerate(struct sched_domain *sd, struct sched_domain *parent) | |
245af2c7 SS |
6819 | { |
6820 | unsigned long cflags = sd->flags, pflags = parent->flags; | |
6821 | ||
6822 | if (sd_degenerate(parent)) | |
6823 | return 1; | |
6824 | ||
758b2cdc | 6825 | if (!cpumask_equal(sched_domain_span(sd), sched_domain_span(parent))) |
245af2c7 SS |
6826 | return 0; |
6827 | ||
245af2c7 SS |
6828 | /* Flags needing groups don't count if only 1 group in parent */ |
6829 | if (parent->groups == parent->groups->next) { | |
6830 | pflags &= ~(SD_LOAD_BALANCE | | |
6831 | SD_BALANCE_NEWIDLE | | |
6832 | SD_BALANCE_FORK | | |
89c4710e SS |
6833 | SD_BALANCE_EXEC | |
6834 | SD_SHARE_CPUPOWER | | |
6835 | SD_SHARE_PKG_RESOURCES); | |
5436499e KC |
6836 | if (nr_node_ids == 1) |
6837 | pflags &= ~SD_SERIALIZE; | |
245af2c7 SS |
6838 | } |
6839 | if (~cflags & pflags) | |
6840 | return 0; | |
6841 | ||
6842 | return 1; | |
6843 | } | |
6844 | ||
dce840a0 | 6845 | static void free_rootdomain(struct rcu_head *rcu) |
c6c4927b | 6846 | { |
dce840a0 | 6847 | struct root_domain *rd = container_of(rcu, struct root_domain, rcu); |
047106ad | 6848 | |
68e74568 | 6849 | cpupri_cleanup(&rd->cpupri); |
c6c4927b RR |
6850 | free_cpumask_var(rd->rto_mask); |
6851 | free_cpumask_var(rd->online); | |
6852 | free_cpumask_var(rd->span); | |
6853 | kfree(rd); | |
6854 | } | |
6855 | ||
57d885fe GH |
6856 | static void rq_attach_root(struct rq *rq, struct root_domain *rd) |
6857 | { | |
a0490fa3 | 6858 | struct root_domain *old_rd = NULL; |
57d885fe | 6859 | unsigned long flags; |
57d885fe | 6860 | |
05fa785c | 6861 | raw_spin_lock_irqsave(&rq->lock, flags); |
57d885fe GH |
6862 | |
6863 | if (rq->rd) { | |
a0490fa3 | 6864 | old_rd = rq->rd; |
57d885fe | 6865 | |
c6c4927b | 6866 | if (cpumask_test_cpu(rq->cpu, old_rd->online)) |
1f11eb6a | 6867 | set_rq_offline(rq); |
57d885fe | 6868 | |
c6c4927b | 6869 | cpumask_clear_cpu(rq->cpu, old_rd->span); |
dc938520 | 6870 | |
a0490fa3 IM |
6871 | /* |
6872 | * If we dont want to free the old_rt yet then | |
6873 | * set old_rd to NULL to skip the freeing later | |
6874 | * in this function: | |
6875 | */ | |
6876 | if (!atomic_dec_and_test(&old_rd->refcount)) | |
6877 | old_rd = NULL; | |
57d885fe GH |
6878 | } |
6879 | ||
6880 | atomic_inc(&rd->refcount); | |
6881 | rq->rd = rd; | |
6882 | ||
c6c4927b | 6883 | cpumask_set_cpu(rq->cpu, rd->span); |
00aec93d | 6884 | if (cpumask_test_cpu(rq->cpu, cpu_active_mask)) |
1f11eb6a | 6885 | set_rq_online(rq); |
57d885fe | 6886 | |
05fa785c | 6887 | raw_spin_unlock_irqrestore(&rq->lock, flags); |
a0490fa3 IM |
6888 | |
6889 | if (old_rd) | |
dce840a0 | 6890 | call_rcu_sched(&old_rd->rcu, free_rootdomain); |
57d885fe GH |
6891 | } |
6892 | ||
68c38fc3 | 6893 | static int init_rootdomain(struct root_domain *rd) |
57d885fe GH |
6894 | { |
6895 | memset(rd, 0, sizeof(*rd)); | |
6896 | ||
68c38fc3 | 6897 | if (!alloc_cpumask_var(&rd->span, GFP_KERNEL)) |
0c910d28 | 6898 | goto out; |
68c38fc3 | 6899 | if (!alloc_cpumask_var(&rd->online, GFP_KERNEL)) |
c6c4927b | 6900 | goto free_span; |
68c38fc3 | 6901 | if (!alloc_cpumask_var(&rd->rto_mask, GFP_KERNEL)) |
c6c4927b | 6902 | goto free_online; |
6e0534f2 | 6903 | |
68c38fc3 | 6904 | if (cpupri_init(&rd->cpupri) != 0) |
68e74568 | 6905 | goto free_rto_mask; |
c6c4927b | 6906 | return 0; |
6e0534f2 | 6907 | |
68e74568 RR |
6908 | free_rto_mask: |
6909 | free_cpumask_var(rd->rto_mask); | |
c6c4927b RR |
6910 | free_online: |
6911 | free_cpumask_var(rd->online); | |
6912 | free_span: | |
6913 | free_cpumask_var(rd->span); | |
0c910d28 | 6914 | out: |
c6c4927b | 6915 | return -ENOMEM; |
57d885fe GH |
6916 | } |
6917 | ||
6918 | static void init_defrootdomain(void) | |
6919 | { | |
68c38fc3 | 6920 | init_rootdomain(&def_root_domain); |
c6c4927b | 6921 | |
57d885fe GH |
6922 | atomic_set(&def_root_domain.refcount, 1); |
6923 | } | |
6924 | ||
dc938520 | 6925 | static struct root_domain *alloc_rootdomain(void) |
57d885fe GH |
6926 | { |
6927 | struct root_domain *rd; | |
6928 | ||
6929 | rd = kmalloc(sizeof(*rd), GFP_KERNEL); | |
6930 | if (!rd) | |
6931 | return NULL; | |
6932 | ||
68c38fc3 | 6933 | if (init_rootdomain(rd) != 0) { |
c6c4927b RR |
6934 | kfree(rd); |
6935 | return NULL; | |
6936 | } | |
57d885fe GH |
6937 | |
6938 | return rd; | |
6939 | } | |
6940 | ||
e3589f6c PZ |
6941 | static void free_sched_groups(struct sched_group *sg, int free_sgp) |
6942 | { | |
6943 | struct sched_group *tmp, *first; | |
6944 | ||
6945 | if (!sg) | |
6946 | return; | |
6947 | ||
6948 | first = sg; | |
6949 | do { | |
6950 | tmp = sg->next; | |
6951 | ||
6952 | if (free_sgp && atomic_dec_and_test(&sg->sgp->ref)) | |
6953 | kfree(sg->sgp); | |
6954 | ||
6955 | kfree(sg); | |
6956 | sg = tmp; | |
6957 | } while (sg != first); | |
6958 | } | |
6959 | ||
dce840a0 PZ |
6960 | static void free_sched_domain(struct rcu_head *rcu) |
6961 | { | |
6962 | struct sched_domain *sd = container_of(rcu, struct sched_domain, rcu); | |
e3589f6c PZ |
6963 | |
6964 | /* | |
6965 | * If its an overlapping domain it has private groups, iterate and | |
6966 | * nuke them all. | |
6967 | */ | |
6968 | if (sd->flags & SD_OVERLAP) { | |
6969 | free_sched_groups(sd->groups, 1); | |
6970 | } else if (atomic_dec_and_test(&sd->groups->ref)) { | |
9c3f75cb | 6971 | kfree(sd->groups->sgp); |
dce840a0 | 6972 | kfree(sd->groups); |
9c3f75cb | 6973 | } |
dce840a0 PZ |
6974 | kfree(sd); |
6975 | } | |
6976 | ||
6977 | static void destroy_sched_domain(struct sched_domain *sd, int cpu) | |
6978 | { | |
6979 | call_rcu(&sd->rcu, free_sched_domain); | |
6980 | } | |
6981 | ||
6982 | static void destroy_sched_domains(struct sched_domain *sd, int cpu) | |
6983 | { | |
6984 | for (; sd; sd = sd->parent) | |
6985 | destroy_sched_domain(sd, cpu); | |
6986 | } | |
6987 | ||
1da177e4 | 6988 | /* |
0eab9146 | 6989 | * Attach the domain 'sd' to 'cpu' as its base domain. Callers must |
1da177e4 LT |
6990 | * hold the hotplug lock. |
6991 | */ | |
0eab9146 IM |
6992 | static void |
6993 | cpu_attach_domain(struct sched_domain *sd, struct root_domain *rd, int cpu) | |
1da177e4 | 6994 | { |
70b97a7f | 6995 | struct rq *rq = cpu_rq(cpu); |
245af2c7 SS |
6996 | struct sched_domain *tmp; |
6997 | ||
6998 | /* Remove the sched domains which do not contribute to scheduling. */ | |
f29c9b1c | 6999 | for (tmp = sd; tmp; ) { |
245af2c7 SS |
7000 | struct sched_domain *parent = tmp->parent; |
7001 | if (!parent) | |
7002 | break; | |
f29c9b1c | 7003 | |
1a848870 | 7004 | if (sd_parent_degenerate(tmp, parent)) { |
245af2c7 | 7005 | tmp->parent = parent->parent; |
1a848870 SS |
7006 | if (parent->parent) |
7007 | parent->parent->child = tmp; | |
dce840a0 | 7008 | destroy_sched_domain(parent, cpu); |
f29c9b1c LZ |
7009 | } else |
7010 | tmp = tmp->parent; | |
245af2c7 SS |
7011 | } |
7012 | ||
1a848870 | 7013 | if (sd && sd_degenerate(sd)) { |
dce840a0 | 7014 | tmp = sd; |
245af2c7 | 7015 | sd = sd->parent; |
dce840a0 | 7016 | destroy_sched_domain(tmp, cpu); |
1a848870 SS |
7017 | if (sd) |
7018 | sd->child = NULL; | |
7019 | } | |
1da177e4 | 7020 | |
4cb98839 | 7021 | sched_domain_debug(sd, cpu); |
1da177e4 | 7022 | |
57d885fe | 7023 | rq_attach_root(rq, rd); |
dce840a0 | 7024 | tmp = rq->sd; |
674311d5 | 7025 | rcu_assign_pointer(rq->sd, sd); |
dce840a0 | 7026 | destroy_sched_domains(tmp, cpu); |
1da177e4 LT |
7027 | } |
7028 | ||
7029 | /* cpus with isolated domains */ | |
dcc30a35 | 7030 | static cpumask_var_t cpu_isolated_map; |
1da177e4 LT |
7031 | |
7032 | /* Setup the mask of cpus configured for isolated domains */ | |
7033 | static int __init isolated_cpu_setup(char *str) | |
7034 | { | |
bdddd296 | 7035 | alloc_bootmem_cpumask_var(&cpu_isolated_map); |
968ea6d8 | 7036 | cpulist_parse(str, cpu_isolated_map); |
1da177e4 LT |
7037 | return 1; |
7038 | } | |
7039 | ||
8927f494 | 7040 | __setup("isolcpus=", isolated_cpu_setup); |
1da177e4 | 7041 | |
9c1cfda2 | 7042 | #define SD_NODES_PER_DOMAIN 16 |
1da177e4 | 7043 | |
9c1cfda2 | 7044 | #ifdef CONFIG_NUMA |
198e2f18 | 7045 | |
9c1cfda2 JH |
7046 | /** |
7047 | * find_next_best_node - find the next node to include in a sched_domain | |
7048 | * @node: node whose sched_domain we're building | |
7049 | * @used_nodes: nodes already in the sched_domain | |
7050 | * | |
41a2d6cf | 7051 | * Find the next node to include in a given scheduling domain. Simply |
9c1cfda2 JH |
7052 | * finds the closest node not already in the @used_nodes map. |
7053 | * | |
7054 | * Should use nodemask_t. | |
7055 | */ | |
c5f59f08 | 7056 | static int find_next_best_node(int node, nodemask_t *used_nodes) |
9c1cfda2 | 7057 | { |
7142d17e | 7058 | int i, n, val, min_val, best_node = -1; |
9c1cfda2 JH |
7059 | |
7060 | min_val = INT_MAX; | |
7061 | ||
076ac2af | 7062 | for (i = 0; i < nr_node_ids; i++) { |
9c1cfda2 | 7063 | /* Start at @node */ |
076ac2af | 7064 | n = (node + i) % nr_node_ids; |
9c1cfda2 JH |
7065 | |
7066 | if (!nr_cpus_node(n)) | |
7067 | continue; | |
7068 | ||
7069 | /* Skip already used nodes */ | |
c5f59f08 | 7070 | if (node_isset(n, *used_nodes)) |
9c1cfda2 JH |
7071 | continue; |
7072 | ||
7073 | /* Simple min distance search */ | |
7074 | val = node_distance(node, n); | |
7075 | ||
7076 | if (val < min_val) { | |
7077 | min_val = val; | |
7078 | best_node = n; | |
7079 | } | |
7080 | } | |
7081 | ||
7142d17e HD |
7082 | if (best_node != -1) |
7083 | node_set(best_node, *used_nodes); | |
9c1cfda2 JH |
7084 | return best_node; |
7085 | } | |
7086 | ||
7087 | /** | |
7088 | * sched_domain_node_span - get a cpumask for a node's sched_domain | |
7089 | * @node: node whose cpumask we're constructing | |
73486722 | 7090 | * @span: resulting cpumask |
9c1cfda2 | 7091 | * |
41a2d6cf | 7092 | * Given a node, construct a good cpumask for its sched_domain to span. It |
9c1cfda2 JH |
7093 | * should be one that prevents unnecessary balancing, but also spreads tasks |
7094 | * out optimally. | |
7095 | */ | |
96f874e2 | 7096 | static void sched_domain_node_span(int node, struct cpumask *span) |
9c1cfda2 | 7097 | { |
c5f59f08 | 7098 | nodemask_t used_nodes; |
48f24c4d | 7099 | int i; |
9c1cfda2 | 7100 | |
6ca09dfc | 7101 | cpumask_clear(span); |
c5f59f08 | 7102 | nodes_clear(used_nodes); |
9c1cfda2 | 7103 | |
6ca09dfc | 7104 | cpumask_or(span, span, cpumask_of_node(node)); |
c5f59f08 | 7105 | node_set(node, used_nodes); |
9c1cfda2 JH |
7106 | |
7107 | for (i = 1; i < SD_NODES_PER_DOMAIN; i++) { | |
c5f59f08 | 7108 | int next_node = find_next_best_node(node, &used_nodes); |
7142d17e HD |
7109 | if (next_node < 0) |
7110 | break; | |
6ca09dfc | 7111 | cpumask_or(span, span, cpumask_of_node(next_node)); |
9c1cfda2 | 7112 | } |
9c1cfda2 | 7113 | } |
d3081f52 PZ |
7114 | |
7115 | static const struct cpumask *cpu_node_mask(int cpu) | |
7116 | { | |
7117 | lockdep_assert_held(&sched_domains_mutex); | |
7118 | ||
7119 | sched_domain_node_span(cpu_to_node(cpu), sched_domains_tmpmask); | |
7120 | ||
7121 | return sched_domains_tmpmask; | |
7122 | } | |
2c402dc3 PZ |
7123 | |
7124 | static const struct cpumask *cpu_allnodes_mask(int cpu) | |
7125 | { | |
7126 | return cpu_possible_mask; | |
7127 | } | |
6d6bc0ad | 7128 | #endif /* CONFIG_NUMA */ |
9c1cfda2 | 7129 | |
d3081f52 PZ |
7130 | static const struct cpumask *cpu_cpu_mask(int cpu) |
7131 | { | |
7132 | return cpumask_of_node(cpu_to_node(cpu)); | |
7133 | } | |
7134 | ||
5c45bf27 | 7135 | int sched_smt_power_savings = 0, sched_mc_power_savings = 0; |
48f24c4d | 7136 | |
dce840a0 PZ |
7137 | struct sd_data { |
7138 | struct sched_domain **__percpu sd; | |
7139 | struct sched_group **__percpu sg; | |
9c3f75cb | 7140 | struct sched_group_power **__percpu sgp; |
dce840a0 PZ |
7141 | }; |
7142 | ||
49a02c51 | 7143 | struct s_data { |
21d42ccf | 7144 | struct sched_domain ** __percpu sd; |
49a02c51 AH |
7145 | struct root_domain *rd; |
7146 | }; | |
7147 | ||
2109b99e | 7148 | enum s_alloc { |
2109b99e | 7149 | sa_rootdomain, |
21d42ccf | 7150 | sa_sd, |
dce840a0 | 7151 | sa_sd_storage, |
2109b99e AH |
7152 | sa_none, |
7153 | }; | |
7154 | ||
54ab4ff4 PZ |
7155 | struct sched_domain_topology_level; |
7156 | ||
7157 | typedef struct sched_domain *(*sched_domain_init_f)(struct sched_domain_topology_level *tl, int cpu); | |
eb7a74e6 PZ |
7158 | typedef const struct cpumask *(*sched_domain_mask_f)(int cpu); |
7159 | ||
e3589f6c PZ |
7160 | #define SDTL_OVERLAP 0x01 |
7161 | ||
eb7a74e6 | 7162 | struct sched_domain_topology_level { |
2c402dc3 PZ |
7163 | sched_domain_init_f init; |
7164 | sched_domain_mask_f mask; | |
e3589f6c | 7165 | int flags; |
54ab4ff4 | 7166 | struct sd_data data; |
eb7a74e6 PZ |
7167 | }; |
7168 | ||
e3589f6c PZ |
7169 | static int |
7170 | build_overlap_sched_groups(struct sched_domain *sd, int cpu) | |
7171 | { | |
7172 | struct sched_group *first = NULL, *last = NULL, *groups = NULL, *sg; | |
7173 | const struct cpumask *span = sched_domain_span(sd); | |
7174 | struct cpumask *covered = sched_domains_tmpmask; | |
7175 | struct sd_data *sdd = sd->private; | |
7176 | struct sched_domain *child; | |
7177 | int i; | |
7178 | ||
7179 | cpumask_clear(covered); | |
7180 | ||
7181 | for_each_cpu(i, span) { | |
7182 | struct cpumask *sg_span; | |
7183 | ||
7184 | if (cpumask_test_cpu(i, covered)) | |
7185 | continue; | |
7186 | ||
7187 | sg = kzalloc_node(sizeof(struct sched_group) + cpumask_size(), | |
7188 | GFP_KERNEL, cpu_to_node(i)); | |
7189 | ||
7190 | if (!sg) | |
7191 | goto fail; | |
7192 | ||
7193 | sg_span = sched_group_cpus(sg); | |
7194 | ||
7195 | child = *per_cpu_ptr(sdd->sd, i); | |
7196 | if (child->child) { | |
7197 | child = child->child; | |
7198 | cpumask_copy(sg_span, sched_domain_span(child)); | |
7199 | } else | |
7200 | cpumask_set_cpu(i, sg_span); | |
7201 | ||
7202 | cpumask_or(covered, covered, sg_span); | |
7203 | ||
7204 | sg->sgp = *per_cpu_ptr(sdd->sgp, cpumask_first(sg_span)); | |
7205 | atomic_inc(&sg->sgp->ref); | |
7206 | ||
7207 | if (cpumask_test_cpu(cpu, sg_span)) | |
7208 | groups = sg; | |
7209 | ||
7210 | if (!first) | |
7211 | first = sg; | |
7212 | if (last) | |
7213 | last->next = sg; | |
7214 | last = sg; | |
7215 | last->next = first; | |
7216 | } | |
7217 | sd->groups = groups; | |
7218 | ||
7219 | return 0; | |
7220 | ||
7221 | fail: | |
7222 | free_sched_groups(first, 0); | |
7223 | ||
7224 | return -ENOMEM; | |
7225 | } | |
7226 | ||
dce840a0 | 7227 | static int get_group(int cpu, struct sd_data *sdd, struct sched_group **sg) |
1da177e4 | 7228 | { |
dce840a0 PZ |
7229 | struct sched_domain *sd = *per_cpu_ptr(sdd->sd, cpu); |
7230 | struct sched_domain *child = sd->child; | |
1da177e4 | 7231 | |
dce840a0 PZ |
7232 | if (child) |
7233 | cpu = cpumask_first(sched_domain_span(child)); | |
1e9f28fa | 7234 | |
9c3f75cb | 7235 | if (sg) { |
dce840a0 | 7236 | *sg = *per_cpu_ptr(sdd->sg, cpu); |
9c3f75cb | 7237 | (*sg)->sgp = *per_cpu_ptr(sdd->sgp, cpu); |
e3589f6c | 7238 | atomic_set(&(*sg)->sgp->ref, 1); /* for claim_allocations */ |
9c3f75cb | 7239 | } |
dce840a0 PZ |
7240 | |
7241 | return cpu; | |
1e9f28fa | 7242 | } |
1e9f28fa | 7243 | |
01a08546 | 7244 | /* |
dce840a0 PZ |
7245 | * build_sched_groups will build a circular linked list of the groups |
7246 | * covered by the given span, and will set each group's ->cpumask correctly, | |
7247 | * and ->cpu_power to 0. | |
e3589f6c PZ |
7248 | * |
7249 | * Assumes the sched_domain tree is fully constructed | |
01a08546 | 7250 | */ |
e3589f6c PZ |
7251 | static int |
7252 | build_sched_groups(struct sched_domain *sd, int cpu) | |
1da177e4 | 7253 | { |
dce840a0 PZ |
7254 | struct sched_group *first = NULL, *last = NULL; |
7255 | struct sd_data *sdd = sd->private; | |
7256 | const struct cpumask *span = sched_domain_span(sd); | |
f96225fd | 7257 | struct cpumask *covered; |
dce840a0 | 7258 | int i; |
9c1cfda2 | 7259 | |
e3589f6c PZ |
7260 | get_group(cpu, sdd, &sd->groups); |
7261 | atomic_inc(&sd->groups->ref); | |
7262 | ||
7263 | if (cpu != cpumask_first(sched_domain_span(sd))) | |
7264 | return 0; | |
7265 | ||
f96225fd PZ |
7266 | lockdep_assert_held(&sched_domains_mutex); |
7267 | covered = sched_domains_tmpmask; | |
7268 | ||
dce840a0 | 7269 | cpumask_clear(covered); |
6711cab4 | 7270 | |
dce840a0 PZ |
7271 | for_each_cpu(i, span) { |
7272 | struct sched_group *sg; | |
7273 | int group = get_group(i, sdd, &sg); | |
7274 | int j; | |
6711cab4 | 7275 | |
dce840a0 PZ |
7276 | if (cpumask_test_cpu(i, covered)) |
7277 | continue; | |
6711cab4 | 7278 | |
dce840a0 | 7279 | cpumask_clear(sched_group_cpus(sg)); |
9c3f75cb | 7280 | sg->sgp->power = 0; |
0601a88d | 7281 | |
dce840a0 PZ |
7282 | for_each_cpu(j, span) { |
7283 | if (get_group(j, sdd, NULL) != group) | |
7284 | continue; | |
0601a88d | 7285 | |
dce840a0 PZ |
7286 | cpumask_set_cpu(j, covered); |
7287 | cpumask_set_cpu(j, sched_group_cpus(sg)); | |
7288 | } | |
0601a88d | 7289 | |
dce840a0 PZ |
7290 | if (!first) |
7291 | first = sg; | |
7292 | if (last) | |
7293 | last->next = sg; | |
7294 | last = sg; | |
7295 | } | |
7296 | last->next = first; | |
e3589f6c PZ |
7297 | |
7298 | return 0; | |
0601a88d | 7299 | } |
51888ca2 | 7300 | |
89c4710e SS |
7301 | /* |
7302 | * Initialize sched groups cpu_power. | |
7303 | * | |
7304 | * cpu_power indicates the capacity of sched group, which is used while | |
7305 | * distributing the load between different sched groups in a sched domain. | |
7306 | * Typically cpu_power for all the groups in a sched domain will be same unless | |
7307 | * there are asymmetries in the topology. If there are asymmetries, group | |
7308 | * having more cpu_power will pickup more load compared to the group having | |
7309 | * less cpu_power. | |
89c4710e SS |
7310 | */ |
7311 | static void init_sched_groups_power(int cpu, struct sched_domain *sd) | |
7312 | { | |
e3589f6c | 7313 | struct sched_group *sg = sd->groups; |
89c4710e | 7314 | |
e3589f6c PZ |
7315 | WARN_ON(!sd || !sg); |
7316 | ||
7317 | do { | |
7318 | sg->group_weight = cpumask_weight(sched_group_cpus(sg)); | |
7319 | sg = sg->next; | |
7320 | } while (sg != sd->groups); | |
89c4710e | 7321 | |
e3589f6c PZ |
7322 | if (cpu != group_first_cpu(sg)) |
7323 | return; | |
aae6d3dd | 7324 | |
d274cb30 | 7325 | update_group_power(sd, cpu); |
89c4710e SS |
7326 | } |
7327 | ||
7c16ec58 MT |
7328 | /* |
7329 | * Initializers for schedule domains | |
7330 | * Non-inlined to reduce accumulated stack pressure in build_sched_domains() | |
7331 | */ | |
7332 | ||
a5d8c348 IM |
7333 | #ifdef CONFIG_SCHED_DEBUG |
7334 | # define SD_INIT_NAME(sd, type) sd->name = #type | |
7335 | #else | |
7336 | # define SD_INIT_NAME(sd, type) do { } while (0) | |
7337 | #endif | |
7338 | ||
54ab4ff4 PZ |
7339 | #define SD_INIT_FUNC(type) \ |
7340 | static noinline struct sched_domain * \ | |
7341 | sd_init_##type(struct sched_domain_topology_level *tl, int cpu) \ | |
7342 | { \ | |
7343 | struct sched_domain *sd = *per_cpu_ptr(tl->data.sd, cpu); \ | |
7344 | *sd = SD_##type##_INIT; \ | |
54ab4ff4 PZ |
7345 | SD_INIT_NAME(sd, type); \ |
7346 | sd->private = &tl->data; \ | |
7347 | return sd; \ | |
7c16ec58 MT |
7348 | } |
7349 | ||
7350 | SD_INIT_FUNC(CPU) | |
7351 | #ifdef CONFIG_NUMA | |
7352 | SD_INIT_FUNC(ALLNODES) | |
7353 | SD_INIT_FUNC(NODE) | |
7354 | #endif | |
7355 | #ifdef CONFIG_SCHED_SMT | |
7356 | SD_INIT_FUNC(SIBLING) | |
7357 | #endif | |
7358 | #ifdef CONFIG_SCHED_MC | |
7359 | SD_INIT_FUNC(MC) | |
7360 | #endif | |
01a08546 HC |
7361 | #ifdef CONFIG_SCHED_BOOK |
7362 | SD_INIT_FUNC(BOOK) | |
7363 | #endif | |
7c16ec58 | 7364 | |
1d3504fc | 7365 | static int default_relax_domain_level = -1; |
60495e77 | 7366 | int sched_domain_level_max; |
1d3504fc HS |
7367 | |
7368 | static int __init setup_relax_domain_level(char *str) | |
7369 | { | |
30e0e178 LZ |
7370 | unsigned long val; |
7371 | ||
7372 | val = simple_strtoul(str, NULL, 0); | |
60495e77 | 7373 | if (val < sched_domain_level_max) |
30e0e178 LZ |
7374 | default_relax_domain_level = val; |
7375 | ||
1d3504fc HS |
7376 | return 1; |
7377 | } | |
7378 | __setup("relax_domain_level=", setup_relax_domain_level); | |
7379 | ||
7380 | static void set_domain_attribute(struct sched_domain *sd, | |
7381 | struct sched_domain_attr *attr) | |
7382 | { | |
7383 | int request; | |
7384 | ||
7385 | if (!attr || attr->relax_domain_level < 0) { | |
7386 | if (default_relax_domain_level < 0) | |
7387 | return; | |
7388 | else | |
7389 | request = default_relax_domain_level; | |
7390 | } else | |
7391 | request = attr->relax_domain_level; | |
7392 | if (request < sd->level) { | |
7393 | /* turn off idle balance on this domain */ | |
c88d5910 | 7394 | sd->flags &= ~(SD_BALANCE_WAKE|SD_BALANCE_NEWIDLE); |
1d3504fc HS |
7395 | } else { |
7396 | /* turn on idle balance on this domain */ | |
c88d5910 | 7397 | sd->flags |= (SD_BALANCE_WAKE|SD_BALANCE_NEWIDLE); |
1d3504fc HS |
7398 | } |
7399 | } | |
7400 | ||
54ab4ff4 PZ |
7401 | static void __sdt_free(const struct cpumask *cpu_map); |
7402 | static int __sdt_alloc(const struct cpumask *cpu_map); | |
7403 | ||
2109b99e AH |
7404 | static void __free_domain_allocs(struct s_data *d, enum s_alloc what, |
7405 | const struct cpumask *cpu_map) | |
7406 | { | |
7407 | switch (what) { | |
2109b99e | 7408 | case sa_rootdomain: |
822ff793 PZ |
7409 | if (!atomic_read(&d->rd->refcount)) |
7410 | free_rootdomain(&d->rd->rcu); /* fall through */ | |
21d42ccf PZ |
7411 | case sa_sd: |
7412 | free_percpu(d->sd); /* fall through */ | |
dce840a0 | 7413 | case sa_sd_storage: |
54ab4ff4 | 7414 | __sdt_free(cpu_map); /* fall through */ |
2109b99e AH |
7415 | case sa_none: |
7416 | break; | |
7417 | } | |
7418 | } | |
3404c8d9 | 7419 | |
2109b99e AH |
7420 | static enum s_alloc __visit_domain_allocation_hell(struct s_data *d, |
7421 | const struct cpumask *cpu_map) | |
7422 | { | |
dce840a0 PZ |
7423 | memset(d, 0, sizeof(*d)); |
7424 | ||
54ab4ff4 PZ |
7425 | if (__sdt_alloc(cpu_map)) |
7426 | return sa_sd_storage; | |
dce840a0 PZ |
7427 | d->sd = alloc_percpu(struct sched_domain *); |
7428 | if (!d->sd) | |
7429 | return sa_sd_storage; | |
2109b99e | 7430 | d->rd = alloc_rootdomain(); |
dce840a0 | 7431 | if (!d->rd) |
21d42ccf | 7432 | return sa_sd; |
2109b99e AH |
7433 | return sa_rootdomain; |
7434 | } | |
57d885fe | 7435 | |
dce840a0 PZ |
7436 | /* |
7437 | * NULL the sd_data elements we've used to build the sched_domain and | |
7438 | * sched_group structure so that the subsequent __free_domain_allocs() | |
7439 | * will not free the data we're using. | |
7440 | */ | |
7441 | static void claim_allocations(int cpu, struct sched_domain *sd) | |
7442 | { | |
7443 | struct sd_data *sdd = sd->private; | |
dce840a0 PZ |
7444 | |
7445 | WARN_ON_ONCE(*per_cpu_ptr(sdd->sd, cpu) != sd); | |
7446 | *per_cpu_ptr(sdd->sd, cpu) = NULL; | |
7447 | ||
e3589f6c | 7448 | if (atomic_read(&(*per_cpu_ptr(sdd->sg, cpu))->ref)) |
dce840a0 | 7449 | *per_cpu_ptr(sdd->sg, cpu) = NULL; |
e3589f6c PZ |
7450 | |
7451 | if (atomic_read(&(*per_cpu_ptr(sdd->sgp, cpu))->ref)) | |
9c3f75cb | 7452 | *per_cpu_ptr(sdd->sgp, cpu) = NULL; |
dce840a0 PZ |
7453 | } |
7454 | ||
2c402dc3 PZ |
7455 | #ifdef CONFIG_SCHED_SMT |
7456 | static const struct cpumask *cpu_smt_mask(int cpu) | |
7f4588f3 | 7457 | { |
2c402dc3 | 7458 | return topology_thread_cpumask(cpu); |
3bd65a80 | 7459 | } |
2c402dc3 | 7460 | #endif |
7f4588f3 | 7461 | |
d069b916 PZ |
7462 | /* |
7463 | * Topology list, bottom-up. | |
7464 | */ | |
2c402dc3 | 7465 | static struct sched_domain_topology_level default_topology[] = { |
d069b916 PZ |
7466 | #ifdef CONFIG_SCHED_SMT |
7467 | { sd_init_SIBLING, cpu_smt_mask, }, | |
01a08546 | 7468 | #endif |
1e9f28fa | 7469 | #ifdef CONFIG_SCHED_MC |
2c402dc3 | 7470 | { sd_init_MC, cpu_coregroup_mask, }, |
1e9f28fa | 7471 | #endif |
d069b916 PZ |
7472 | #ifdef CONFIG_SCHED_BOOK |
7473 | { sd_init_BOOK, cpu_book_mask, }, | |
7474 | #endif | |
7475 | { sd_init_CPU, cpu_cpu_mask, }, | |
7476 | #ifdef CONFIG_NUMA | |
e3589f6c | 7477 | { sd_init_NODE, cpu_node_mask, SDTL_OVERLAP, }, |
d069b916 | 7478 | { sd_init_ALLNODES, cpu_allnodes_mask, }, |
1da177e4 | 7479 | #endif |
eb7a74e6 PZ |
7480 | { NULL, }, |
7481 | }; | |
7482 | ||
7483 | static struct sched_domain_topology_level *sched_domain_topology = default_topology; | |
7484 | ||
54ab4ff4 PZ |
7485 | static int __sdt_alloc(const struct cpumask *cpu_map) |
7486 | { | |
7487 | struct sched_domain_topology_level *tl; | |
7488 | int j; | |
7489 | ||
7490 | for (tl = sched_domain_topology; tl->init; tl++) { | |
7491 | struct sd_data *sdd = &tl->data; | |
7492 | ||
7493 | sdd->sd = alloc_percpu(struct sched_domain *); | |
7494 | if (!sdd->sd) | |
7495 | return -ENOMEM; | |
7496 | ||
7497 | sdd->sg = alloc_percpu(struct sched_group *); | |
7498 | if (!sdd->sg) | |
7499 | return -ENOMEM; | |
7500 | ||
9c3f75cb PZ |
7501 | sdd->sgp = alloc_percpu(struct sched_group_power *); |
7502 | if (!sdd->sgp) | |
7503 | return -ENOMEM; | |
7504 | ||
54ab4ff4 PZ |
7505 | for_each_cpu(j, cpu_map) { |
7506 | struct sched_domain *sd; | |
7507 | struct sched_group *sg; | |
9c3f75cb | 7508 | struct sched_group_power *sgp; |
54ab4ff4 PZ |
7509 | |
7510 | sd = kzalloc_node(sizeof(struct sched_domain) + cpumask_size(), | |
7511 | GFP_KERNEL, cpu_to_node(j)); | |
7512 | if (!sd) | |
7513 | return -ENOMEM; | |
7514 | ||
7515 | *per_cpu_ptr(sdd->sd, j) = sd; | |
7516 | ||
7517 | sg = kzalloc_node(sizeof(struct sched_group) + cpumask_size(), | |
7518 | GFP_KERNEL, cpu_to_node(j)); | |
7519 | if (!sg) | |
7520 | return -ENOMEM; | |
7521 | ||
7522 | *per_cpu_ptr(sdd->sg, j) = sg; | |
9c3f75cb PZ |
7523 | |
7524 | sgp = kzalloc_node(sizeof(struct sched_group_power), | |
7525 | GFP_KERNEL, cpu_to_node(j)); | |
7526 | if (!sgp) | |
7527 | return -ENOMEM; | |
7528 | ||
7529 | *per_cpu_ptr(sdd->sgp, j) = sgp; | |
54ab4ff4 PZ |
7530 | } |
7531 | } | |
7532 | ||
7533 | return 0; | |
7534 | } | |
7535 | ||
7536 | static void __sdt_free(const struct cpumask *cpu_map) | |
7537 | { | |
7538 | struct sched_domain_topology_level *tl; | |
7539 | int j; | |
7540 | ||
7541 | for (tl = sched_domain_topology; tl->init; tl++) { | |
7542 | struct sd_data *sdd = &tl->data; | |
7543 | ||
7544 | for_each_cpu(j, cpu_map) { | |
e3589f6c PZ |
7545 | struct sched_domain *sd = *per_cpu_ptr(sdd->sd, j); |
7546 | if (sd && (sd->flags & SD_OVERLAP)) | |
7547 | free_sched_groups(sd->groups, 0); | |
54ab4ff4 | 7548 | kfree(*per_cpu_ptr(sdd->sg, j)); |
9c3f75cb | 7549 | kfree(*per_cpu_ptr(sdd->sgp, j)); |
54ab4ff4 PZ |
7550 | } |
7551 | free_percpu(sdd->sd); | |
7552 | free_percpu(sdd->sg); | |
9c3f75cb | 7553 | free_percpu(sdd->sgp); |
54ab4ff4 PZ |
7554 | } |
7555 | } | |
7556 | ||
2c402dc3 PZ |
7557 | struct sched_domain *build_sched_domain(struct sched_domain_topology_level *tl, |
7558 | struct s_data *d, const struct cpumask *cpu_map, | |
d069b916 | 7559 | struct sched_domain_attr *attr, struct sched_domain *child, |
2c402dc3 PZ |
7560 | int cpu) |
7561 | { | |
54ab4ff4 | 7562 | struct sched_domain *sd = tl->init(tl, cpu); |
2c402dc3 | 7563 | if (!sd) |
d069b916 | 7564 | return child; |
2c402dc3 PZ |
7565 | |
7566 | set_domain_attribute(sd, attr); | |
7567 | cpumask_and(sched_domain_span(sd), cpu_map, tl->mask(cpu)); | |
60495e77 PZ |
7568 | if (child) { |
7569 | sd->level = child->level + 1; | |
7570 | sched_domain_level_max = max(sched_domain_level_max, sd->level); | |
d069b916 | 7571 | child->parent = sd; |
60495e77 | 7572 | } |
d069b916 | 7573 | sd->child = child; |
2c402dc3 PZ |
7574 | |
7575 | return sd; | |
7576 | } | |
7577 | ||
2109b99e AH |
7578 | /* |
7579 | * Build sched domains for a given set of cpus and attach the sched domains | |
7580 | * to the individual cpus | |
7581 | */ | |
dce840a0 PZ |
7582 | static int build_sched_domains(const struct cpumask *cpu_map, |
7583 | struct sched_domain_attr *attr) | |
2109b99e AH |
7584 | { |
7585 | enum s_alloc alloc_state = sa_none; | |
dce840a0 | 7586 | struct sched_domain *sd; |
2109b99e | 7587 | struct s_data d; |
822ff793 | 7588 | int i, ret = -ENOMEM; |
9c1cfda2 | 7589 | |
2109b99e AH |
7590 | alloc_state = __visit_domain_allocation_hell(&d, cpu_map); |
7591 | if (alloc_state != sa_rootdomain) | |
7592 | goto error; | |
9c1cfda2 | 7593 | |
dce840a0 | 7594 | /* Set up domains for cpus specified by the cpu_map. */ |
abcd083a | 7595 | for_each_cpu(i, cpu_map) { |
eb7a74e6 PZ |
7596 | struct sched_domain_topology_level *tl; |
7597 | ||
3bd65a80 | 7598 | sd = NULL; |
e3589f6c | 7599 | for (tl = sched_domain_topology; tl->init; tl++) { |
2c402dc3 | 7600 | sd = build_sched_domain(tl, &d, cpu_map, attr, sd, i); |
e3589f6c PZ |
7601 | if (tl->flags & SDTL_OVERLAP || sched_feat(FORCE_SD_OVERLAP)) |
7602 | sd->flags |= SD_OVERLAP; | |
d110235d PZ |
7603 | if (cpumask_equal(cpu_map, sched_domain_span(sd))) |
7604 | break; | |
e3589f6c | 7605 | } |
d274cb30 | 7606 | |
d069b916 PZ |
7607 | while (sd->child) |
7608 | sd = sd->child; | |
7609 | ||
21d42ccf | 7610 | *per_cpu_ptr(d.sd, i) = sd; |
dce840a0 PZ |
7611 | } |
7612 | ||
7613 | /* Build the groups for the domains */ | |
7614 | for_each_cpu(i, cpu_map) { | |
7615 | for (sd = *per_cpu_ptr(d.sd, i); sd; sd = sd->parent) { | |
7616 | sd->span_weight = cpumask_weight(sched_domain_span(sd)); | |
e3589f6c PZ |
7617 | if (sd->flags & SD_OVERLAP) { |
7618 | if (build_overlap_sched_groups(sd, i)) | |
7619 | goto error; | |
7620 | } else { | |
7621 | if (build_sched_groups(sd, i)) | |
7622 | goto error; | |
7623 | } | |
1cf51902 | 7624 | } |
a06dadbe | 7625 | } |
9c1cfda2 | 7626 | |
1da177e4 | 7627 | /* Calculate CPU power for physical packages and nodes */ |
a9c9a9b6 PZ |
7628 | for (i = nr_cpumask_bits-1; i >= 0; i--) { |
7629 | if (!cpumask_test_cpu(i, cpu_map)) | |
7630 | continue; | |
9c1cfda2 | 7631 | |
dce840a0 PZ |
7632 | for (sd = *per_cpu_ptr(d.sd, i); sd; sd = sd->parent) { |
7633 | claim_allocations(i, sd); | |
cd4ea6ae | 7634 | init_sched_groups_power(i, sd); |
dce840a0 | 7635 | } |
f712c0c7 | 7636 | } |
9c1cfda2 | 7637 | |
1da177e4 | 7638 | /* Attach the domains */ |
dce840a0 | 7639 | rcu_read_lock(); |
abcd083a | 7640 | for_each_cpu(i, cpu_map) { |
21d42ccf | 7641 | sd = *per_cpu_ptr(d.sd, i); |
49a02c51 | 7642 | cpu_attach_domain(sd, d.rd, i); |
1da177e4 | 7643 | } |
dce840a0 | 7644 | rcu_read_unlock(); |
51888ca2 | 7645 | |
822ff793 | 7646 | ret = 0; |
51888ca2 | 7647 | error: |
2109b99e | 7648 | __free_domain_allocs(&d, alloc_state, cpu_map); |
822ff793 | 7649 | return ret; |
1da177e4 | 7650 | } |
029190c5 | 7651 | |
acc3f5d7 | 7652 | static cpumask_var_t *doms_cur; /* current sched domains */ |
029190c5 | 7653 | static int ndoms_cur; /* number of sched domains in 'doms_cur' */ |
4285f594 IM |
7654 | static struct sched_domain_attr *dattr_cur; |
7655 | /* attribues of custom domains in 'doms_cur' */ | |
029190c5 PJ |
7656 | |
7657 | /* | |
7658 | * Special case: If a kmalloc of a doms_cur partition (array of | |
4212823f RR |
7659 | * cpumask) fails, then fallback to a single sched domain, |
7660 | * as determined by the single cpumask fallback_doms. | |
029190c5 | 7661 | */ |
4212823f | 7662 | static cpumask_var_t fallback_doms; |
029190c5 | 7663 | |
ee79d1bd HC |
7664 | /* |
7665 | * arch_update_cpu_topology lets virtualized architectures update the | |
7666 | * cpu core maps. It is supposed to return 1 if the topology changed | |
7667 | * or 0 if it stayed the same. | |
7668 | */ | |
7669 | int __attribute__((weak)) arch_update_cpu_topology(void) | |
22e52b07 | 7670 | { |
ee79d1bd | 7671 | return 0; |
22e52b07 HC |
7672 | } |
7673 | ||
acc3f5d7 RR |
7674 | cpumask_var_t *alloc_sched_domains(unsigned int ndoms) |
7675 | { | |
7676 | int i; | |
7677 | cpumask_var_t *doms; | |
7678 | ||
7679 | doms = kmalloc(sizeof(*doms) * ndoms, GFP_KERNEL); | |
7680 | if (!doms) | |
7681 | return NULL; | |
7682 | for (i = 0; i < ndoms; i++) { | |
7683 | if (!alloc_cpumask_var(&doms[i], GFP_KERNEL)) { | |
7684 | free_sched_domains(doms, i); | |
7685 | return NULL; | |
7686 | } | |
7687 | } | |
7688 | return doms; | |
7689 | } | |
7690 | ||
7691 | void free_sched_domains(cpumask_var_t doms[], unsigned int ndoms) | |
7692 | { | |
7693 | unsigned int i; | |
7694 | for (i = 0; i < ndoms; i++) | |
7695 | free_cpumask_var(doms[i]); | |
7696 | kfree(doms); | |
7697 | } | |
7698 | ||
1a20ff27 | 7699 | /* |
41a2d6cf | 7700 | * Set up scheduler domains and groups. Callers must hold the hotplug lock. |
029190c5 PJ |
7701 | * For now this just excludes isolated cpus, but could be used to |
7702 | * exclude other special cases in the future. | |
1a20ff27 | 7703 | */ |
c4a8849a | 7704 | static int init_sched_domains(const struct cpumask *cpu_map) |
1a20ff27 | 7705 | { |
7378547f MM |
7706 | int err; |
7707 | ||
22e52b07 | 7708 | arch_update_cpu_topology(); |
029190c5 | 7709 | ndoms_cur = 1; |
acc3f5d7 | 7710 | doms_cur = alloc_sched_domains(ndoms_cur); |
029190c5 | 7711 | if (!doms_cur) |
acc3f5d7 RR |
7712 | doms_cur = &fallback_doms; |
7713 | cpumask_andnot(doms_cur[0], cpu_map, cpu_isolated_map); | |
1d3504fc | 7714 | dattr_cur = NULL; |
dce840a0 | 7715 | err = build_sched_domains(doms_cur[0], NULL); |
6382bc90 | 7716 | register_sched_domain_sysctl(); |
7378547f MM |
7717 | |
7718 | return err; | |
1a20ff27 DG |
7719 | } |
7720 | ||
1a20ff27 DG |
7721 | /* |
7722 | * Detach sched domains from a group of cpus specified in cpu_map | |
7723 | * These cpus will now be attached to the NULL domain | |
7724 | */ | |
96f874e2 | 7725 | static void detach_destroy_domains(const struct cpumask *cpu_map) |
1a20ff27 DG |
7726 | { |
7727 | int i; | |
7728 | ||
dce840a0 | 7729 | rcu_read_lock(); |
abcd083a | 7730 | for_each_cpu(i, cpu_map) |
57d885fe | 7731 | cpu_attach_domain(NULL, &def_root_domain, i); |
dce840a0 | 7732 | rcu_read_unlock(); |
1a20ff27 DG |
7733 | } |
7734 | ||
1d3504fc HS |
7735 | /* handle null as "default" */ |
7736 | static int dattrs_equal(struct sched_domain_attr *cur, int idx_cur, | |
7737 | struct sched_domain_attr *new, int idx_new) | |
7738 | { | |
7739 | struct sched_domain_attr tmp; | |
7740 | ||
7741 | /* fast path */ | |
7742 | if (!new && !cur) | |
7743 | return 1; | |
7744 | ||
7745 | tmp = SD_ATTR_INIT; | |
7746 | return !memcmp(cur ? (cur + idx_cur) : &tmp, | |
7747 | new ? (new + idx_new) : &tmp, | |
7748 | sizeof(struct sched_domain_attr)); | |
7749 | } | |
7750 | ||
029190c5 PJ |
7751 | /* |
7752 | * Partition sched domains as specified by the 'ndoms_new' | |
41a2d6cf | 7753 | * cpumasks in the array doms_new[] of cpumasks. This compares |
029190c5 PJ |
7754 | * doms_new[] to the current sched domain partitioning, doms_cur[]. |
7755 | * It destroys each deleted domain and builds each new domain. | |
7756 | * | |
acc3f5d7 | 7757 | * 'doms_new' is an array of cpumask_var_t's of length 'ndoms_new'. |
41a2d6cf IM |
7758 | * The masks don't intersect (don't overlap.) We should setup one |
7759 | * sched domain for each mask. CPUs not in any of the cpumasks will | |
7760 | * not be load balanced. If the same cpumask appears both in the | |
029190c5 PJ |
7761 | * current 'doms_cur' domains and in the new 'doms_new', we can leave |
7762 | * it as it is. | |
7763 | * | |
acc3f5d7 RR |
7764 | * The passed in 'doms_new' should be allocated using |
7765 | * alloc_sched_domains. This routine takes ownership of it and will | |
7766 | * free_sched_domains it when done with it. If the caller failed the | |
7767 | * alloc call, then it can pass in doms_new == NULL && ndoms_new == 1, | |
7768 | * and partition_sched_domains() will fallback to the single partition | |
7769 | * 'fallback_doms', it also forces the domains to be rebuilt. | |
029190c5 | 7770 | * |
96f874e2 | 7771 | * If doms_new == NULL it will be replaced with cpu_online_mask. |
700018e0 LZ |
7772 | * ndoms_new == 0 is a special case for destroying existing domains, |
7773 | * and it will not create the default domain. | |
dfb512ec | 7774 | * |
029190c5 PJ |
7775 | * Call with hotplug lock held |
7776 | */ | |
acc3f5d7 | 7777 | void partition_sched_domains(int ndoms_new, cpumask_var_t doms_new[], |
1d3504fc | 7778 | struct sched_domain_attr *dattr_new) |
029190c5 | 7779 | { |
dfb512ec | 7780 | int i, j, n; |
d65bd5ec | 7781 | int new_topology; |
029190c5 | 7782 | |
712555ee | 7783 | mutex_lock(&sched_domains_mutex); |
a1835615 | 7784 | |
7378547f MM |
7785 | /* always unregister in case we don't destroy any domains */ |
7786 | unregister_sched_domain_sysctl(); | |
7787 | ||
d65bd5ec HC |
7788 | /* Let architecture update cpu core mappings. */ |
7789 | new_topology = arch_update_cpu_topology(); | |
7790 | ||
dfb512ec | 7791 | n = doms_new ? ndoms_new : 0; |
029190c5 PJ |
7792 | |
7793 | /* Destroy deleted domains */ | |
7794 | for (i = 0; i < ndoms_cur; i++) { | |
d65bd5ec | 7795 | for (j = 0; j < n && !new_topology; j++) { |
acc3f5d7 | 7796 | if (cpumask_equal(doms_cur[i], doms_new[j]) |
1d3504fc | 7797 | && dattrs_equal(dattr_cur, i, dattr_new, j)) |
029190c5 PJ |
7798 | goto match1; |
7799 | } | |
7800 | /* no match - a current sched domain not in new doms_new[] */ | |
acc3f5d7 | 7801 | detach_destroy_domains(doms_cur[i]); |
029190c5 PJ |
7802 | match1: |
7803 | ; | |
7804 | } | |
7805 | ||
e761b772 MK |
7806 | if (doms_new == NULL) { |
7807 | ndoms_cur = 0; | |
acc3f5d7 | 7808 | doms_new = &fallback_doms; |
6ad4c188 | 7809 | cpumask_andnot(doms_new[0], cpu_active_mask, cpu_isolated_map); |
faa2f98f | 7810 | WARN_ON_ONCE(dattr_new); |
e761b772 MK |
7811 | } |
7812 | ||
029190c5 PJ |
7813 | /* Build new domains */ |
7814 | for (i = 0; i < ndoms_new; i++) { | |
d65bd5ec | 7815 | for (j = 0; j < ndoms_cur && !new_topology; j++) { |
acc3f5d7 | 7816 | if (cpumask_equal(doms_new[i], doms_cur[j]) |
1d3504fc | 7817 | && dattrs_equal(dattr_new, i, dattr_cur, j)) |
029190c5 PJ |
7818 | goto match2; |
7819 | } | |
7820 | /* no match - add a new doms_new */ | |
dce840a0 | 7821 | build_sched_domains(doms_new[i], dattr_new ? dattr_new + i : NULL); |
029190c5 PJ |
7822 | match2: |
7823 | ; | |
7824 | } | |
7825 | ||
7826 | /* Remember the new sched domains */ | |
acc3f5d7 RR |
7827 | if (doms_cur != &fallback_doms) |
7828 | free_sched_domains(doms_cur, ndoms_cur); | |
1d3504fc | 7829 | kfree(dattr_cur); /* kfree(NULL) is safe */ |
029190c5 | 7830 | doms_cur = doms_new; |
1d3504fc | 7831 | dattr_cur = dattr_new; |
029190c5 | 7832 | ndoms_cur = ndoms_new; |
7378547f MM |
7833 | |
7834 | register_sched_domain_sysctl(); | |
a1835615 | 7835 | |
712555ee | 7836 | mutex_unlock(&sched_domains_mutex); |
029190c5 PJ |
7837 | } |
7838 | ||
5c45bf27 | 7839 | #if defined(CONFIG_SCHED_MC) || defined(CONFIG_SCHED_SMT) |
c4a8849a | 7840 | static void reinit_sched_domains(void) |
5c45bf27 | 7841 | { |
95402b38 | 7842 | get_online_cpus(); |
dfb512ec MK |
7843 | |
7844 | /* Destroy domains first to force the rebuild */ | |
7845 | partition_sched_domains(0, NULL, NULL); | |
7846 | ||
e761b772 | 7847 | rebuild_sched_domains(); |
95402b38 | 7848 | put_online_cpus(); |
5c45bf27 SS |
7849 | } |
7850 | ||
7851 | static ssize_t sched_power_savings_store(const char *buf, size_t count, int smt) | |
7852 | { | |
afb8a9b7 | 7853 | unsigned int level = 0; |
5c45bf27 | 7854 | |
afb8a9b7 GS |
7855 | if (sscanf(buf, "%u", &level) != 1) |
7856 | return -EINVAL; | |
7857 | ||
7858 | /* | |
7859 | * level is always be positive so don't check for | |
7860 | * level < POWERSAVINGS_BALANCE_NONE which is 0 | |
7861 | * What happens on 0 or 1 byte write, | |
7862 | * need to check for count as well? | |
7863 | */ | |
7864 | ||
7865 | if (level >= MAX_POWERSAVINGS_BALANCE_LEVELS) | |
5c45bf27 SS |
7866 | return -EINVAL; |
7867 | ||
7868 | if (smt) | |
afb8a9b7 | 7869 | sched_smt_power_savings = level; |
5c45bf27 | 7870 | else |
afb8a9b7 | 7871 | sched_mc_power_savings = level; |
5c45bf27 | 7872 | |
c4a8849a | 7873 | reinit_sched_domains(); |
5c45bf27 | 7874 | |
c70f22d2 | 7875 | return count; |
5c45bf27 SS |
7876 | } |
7877 | ||
5c45bf27 | 7878 | #ifdef CONFIG_SCHED_MC |
f718cd4a | 7879 | static ssize_t sched_mc_power_savings_show(struct sysdev_class *class, |
c9be0a36 | 7880 | struct sysdev_class_attribute *attr, |
f718cd4a | 7881 | char *page) |
5c45bf27 SS |
7882 | { |
7883 | return sprintf(page, "%u\n", sched_mc_power_savings); | |
7884 | } | |
f718cd4a | 7885 | static ssize_t sched_mc_power_savings_store(struct sysdev_class *class, |
c9be0a36 | 7886 | struct sysdev_class_attribute *attr, |
48f24c4d | 7887 | const char *buf, size_t count) |
5c45bf27 SS |
7888 | { |
7889 | return sched_power_savings_store(buf, count, 0); | |
7890 | } | |
f718cd4a AK |
7891 | static SYSDEV_CLASS_ATTR(sched_mc_power_savings, 0644, |
7892 | sched_mc_power_savings_show, | |
7893 | sched_mc_power_savings_store); | |
5c45bf27 SS |
7894 | #endif |
7895 | ||
7896 | #ifdef CONFIG_SCHED_SMT | |
f718cd4a | 7897 | static ssize_t sched_smt_power_savings_show(struct sysdev_class *dev, |
c9be0a36 | 7898 | struct sysdev_class_attribute *attr, |
f718cd4a | 7899 | char *page) |
5c45bf27 SS |
7900 | { |
7901 | return sprintf(page, "%u\n", sched_smt_power_savings); | |
7902 | } | |
f718cd4a | 7903 | static ssize_t sched_smt_power_savings_store(struct sysdev_class *dev, |
c9be0a36 | 7904 | struct sysdev_class_attribute *attr, |
48f24c4d | 7905 | const char *buf, size_t count) |
5c45bf27 SS |
7906 | { |
7907 | return sched_power_savings_store(buf, count, 1); | |
7908 | } | |
f718cd4a AK |
7909 | static SYSDEV_CLASS_ATTR(sched_smt_power_savings, 0644, |
7910 | sched_smt_power_savings_show, | |
6707de00 AB |
7911 | sched_smt_power_savings_store); |
7912 | #endif | |
7913 | ||
39aac648 | 7914 | int __init sched_create_sysfs_power_savings_entries(struct sysdev_class *cls) |
6707de00 AB |
7915 | { |
7916 | int err = 0; | |
7917 | ||
7918 | #ifdef CONFIG_SCHED_SMT | |
7919 | if (smt_capable()) | |
7920 | err = sysfs_create_file(&cls->kset.kobj, | |
7921 | &attr_sched_smt_power_savings.attr); | |
7922 | #endif | |
7923 | #ifdef CONFIG_SCHED_MC | |
7924 | if (!err && mc_capable()) | |
7925 | err = sysfs_create_file(&cls->kset.kobj, | |
7926 | &attr_sched_mc_power_savings.attr); | |
7927 | #endif | |
7928 | return err; | |
7929 | } | |
6d6bc0ad | 7930 | #endif /* CONFIG_SCHED_MC || CONFIG_SCHED_SMT */ |
5c45bf27 | 7931 | |
1da177e4 | 7932 | /* |
3a101d05 TH |
7933 | * Update cpusets according to cpu_active mask. If cpusets are |
7934 | * disabled, cpuset_update_active_cpus() becomes a simple wrapper | |
7935 | * around partition_sched_domains(). | |
1da177e4 | 7936 | */ |
0b2e918a TH |
7937 | static int cpuset_cpu_active(struct notifier_block *nfb, unsigned long action, |
7938 | void *hcpu) | |
e761b772 | 7939 | { |
3a101d05 | 7940 | switch (action & ~CPU_TASKS_FROZEN) { |
e761b772 | 7941 | case CPU_ONLINE: |
6ad4c188 | 7942 | case CPU_DOWN_FAILED: |
3a101d05 | 7943 | cpuset_update_active_cpus(); |
e761b772 | 7944 | return NOTIFY_OK; |
3a101d05 TH |
7945 | default: |
7946 | return NOTIFY_DONE; | |
7947 | } | |
7948 | } | |
e761b772 | 7949 | |
0b2e918a TH |
7950 | static int cpuset_cpu_inactive(struct notifier_block *nfb, unsigned long action, |
7951 | void *hcpu) | |
3a101d05 TH |
7952 | { |
7953 | switch (action & ~CPU_TASKS_FROZEN) { | |
7954 | case CPU_DOWN_PREPARE: | |
7955 | cpuset_update_active_cpus(); | |
7956 | return NOTIFY_OK; | |
e761b772 MK |
7957 | default: |
7958 | return NOTIFY_DONE; | |
7959 | } | |
7960 | } | |
e761b772 MK |
7961 | |
7962 | static int update_runtime(struct notifier_block *nfb, | |
7963 | unsigned long action, void *hcpu) | |
1da177e4 | 7964 | { |
7def2be1 PZ |
7965 | int cpu = (int)(long)hcpu; |
7966 | ||
1da177e4 | 7967 | switch (action) { |
1da177e4 | 7968 | case CPU_DOWN_PREPARE: |
8bb78442 | 7969 | case CPU_DOWN_PREPARE_FROZEN: |
7def2be1 | 7970 | disable_runtime(cpu_rq(cpu)); |
1da177e4 LT |
7971 | return NOTIFY_OK; |
7972 | ||
1da177e4 | 7973 | case CPU_DOWN_FAILED: |
8bb78442 | 7974 | case CPU_DOWN_FAILED_FROZEN: |
1da177e4 | 7975 | case CPU_ONLINE: |
8bb78442 | 7976 | case CPU_ONLINE_FROZEN: |
7def2be1 | 7977 | enable_runtime(cpu_rq(cpu)); |
e761b772 MK |
7978 | return NOTIFY_OK; |
7979 | ||
1da177e4 LT |
7980 | default: |
7981 | return NOTIFY_DONE; | |
7982 | } | |
1da177e4 | 7983 | } |
1da177e4 LT |
7984 | |
7985 | void __init sched_init_smp(void) | |
7986 | { | |
dcc30a35 RR |
7987 | cpumask_var_t non_isolated_cpus; |
7988 | ||
7989 | alloc_cpumask_var(&non_isolated_cpus, GFP_KERNEL); | |
cb5fd13f | 7990 | alloc_cpumask_var(&fallback_doms, GFP_KERNEL); |
5c1e1767 | 7991 | |
95402b38 | 7992 | get_online_cpus(); |
712555ee | 7993 | mutex_lock(&sched_domains_mutex); |
c4a8849a | 7994 | init_sched_domains(cpu_active_mask); |
dcc30a35 RR |
7995 | cpumask_andnot(non_isolated_cpus, cpu_possible_mask, cpu_isolated_map); |
7996 | if (cpumask_empty(non_isolated_cpus)) | |
7997 | cpumask_set_cpu(smp_processor_id(), non_isolated_cpus); | |
712555ee | 7998 | mutex_unlock(&sched_domains_mutex); |
95402b38 | 7999 | put_online_cpus(); |
e761b772 | 8000 | |
3a101d05 TH |
8001 | hotcpu_notifier(cpuset_cpu_active, CPU_PRI_CPUSET_ACTIVE); |
8002 | hotcpu_notifier(cpuset_cpu_inactive, CPU_PRI_CPUSET_INACTIVE); | |
e761b772 MK |
8003 | |
8004 | /* RT runtime code needs to handle some hotplug events */ | |
8005 | hotcpu_notifier(update_runtime, 0); | |
8006 | ||
b328ca18 | 8007 | init_hrtick(); |
5c1e1767 NP |
8008 | |
8009 | /* Move init over to a non-isolated CPU */ | |
dcc30a35 | 8010 | if (set_cpus_allowed_ptr(current, non_isolated_cpus) < 0) |
5c1e1767 | 8011 | BUG(); |
19978ca6 | 8012 | sched_init_granularity(); |
dcc30a35 | 8013 | free_cpumask_var(non_isolated_cpus); |
4212823f | 8014 | |
0e3900e6 | 8015 | init_sched_rt_class(); |
1da177e4 LT |
8016 | } |
8017 | #else | |
8018 | void __init sched_init_smp(void) | |
8019 | { | |
19978ca6 | 8020 | sched_init_granularity(); |
1da177e4 LT |
8021 | } |
8022 | #endif /* CONFIG_SMP */ | |
8023 | ||
cd1bb94b AB |
8024 | const_debug unsigned int sysctl_timer_migration = 1; |
8025 | ||
1da177e4 LT |
8026 | int in_sched_functions(unsigned long addr) |
8027 | { | |
1da177e4 LT |
8028 | return in_lock_functions(addr) || |
8029 | (addr >= (unsigned long)__sched_text_start | |
8030 | && addr < (unsigned long)__sched_text_end); | |
8031 | } | |
8032 | ||
acb5a9ba | 8033 | static void init_cfs_rq(struct cfs_rq *cfs_rq) |
dd41f596 IM |
8034 | { |
8035 | cfs_rq->tasks_timeline = RB_ROOT; | |
4a55bd5e | 8036 | INIT_LIST_HEAD(&cfs_rq->tasks); |
67e9fb2a | 8037 | cfs_rq->min_vruntime = (u64)(-(1LL << 20)); |
c64be78f PZ |
8038 | #ifndef CONFIG_64BIT |
8039 | cfs_rq->min_vruntime_copy = cfs_rq->min_vruntime; | |
8040 | #endif | |
dd41f596 IM |
8041 | } |
8042 | ||
fa85ae24 PZ |
8043 | static void init_rt_rq(struct rt_rq *rt_rq, struct rq *rq) |
8044 | { | |
8045 | struct rt_prio_array *array; | |
8046 | int i; | |
8047 | ||
8048 | array = &rt_rq->active; | |
8049 | for (i = 0; i < MAX_RT_PRIO; i++) { | |
8050 | INIT_LIST_HEAD(array->queue + i); | |
8051 | __clear_bit(i, array->bitmap); | |
8052 | } | |
8053 | /* delimiter for bitsearch: */ | |
8054 | __set_bit(MAX_RT_PRIO, array->bitmap); | |
8055 | ||
acb5a9ba | 8056 | #if defined CONFIG_SMP |
e864c499 GH |
8057 | rt_rq->highest_prio.curr = MAX_RT_PRIO; |
8058 | rt_rq->highest_prio.next = MAX_RT_PRIO; | |
fa85ae24 | 8059 | rt_rq->rt_nr_migratory = 0; |
fa85ae24 | 8060 | rt_rq->overloaded = 0; |
732375c6 | 8061 | plist_head_init(&rt_rq->pushable_tasks); |
fa85ae24 PZ |
8062 | #endif |
8063 | ||
8064 | rt_rq->rt_time = 0; | |
8065 | rt_rq->rt_throttled = 0; | |
ac086bc2 | 8066 | rt_rq->rt_runtime = 0; |
0986b11b | 8067 | raw_spin_lock_init(&rt_rq->rt_runtime_lock); |
fa85ae24 PZ |
8068 | } |
8069 | ||
6f505b16 | 8070 | #ifdef CONFIG_FAIR_GROUP_SCHED |
ec7dc8ac | 8071 | static void init_tg_cfs_entry(struct task_group *tg, struct cfs_rq *cfs_rq, |
3d4b47b4 | 8072 | struct sched_entity *se, int cpu, |
ec7dc8ac | 8073 | struct sched_entity *parent) |
6f505b16 | 8074 | { |
ec7dc8ac | 8075 | struct rq *rq = cpu_rq(cpu); |
acb5a9ba | 8076 | |
6f505b16 | 8077 | cfs_rq->tg = tg; |
acb5a9ba JS |
8078 | cfs_rq->rq = rq; |
8079 | #ifdef CONFIG_SMP | |
8080 | /* allow initial update_cfs_load() to truncate */ | |
8081 | cfs_rq->load_stamp = 1; | |
8082 | #endif | |
ab84d31e | 8083 | init_cfs_rq_runtime(cfs_rq); |
6f505b16 | 8084 | |
acb5a9ba | 8085 | tg->cfs_rq[cpu] = cfs_rq; |
6f505b16 | 8086 | tg->se[cpu] = se; |
acb5a9ba | 8087 | |
07e06b01 | 8088 | /* se could be NULL for root_task_group */ |
354d60c2 DG |
8089 | if (!se) |
8090 | return; | |
8091 | ||
ec7dc8ac DG |
8092 | if (!parent) |
8093 | se->cfs_rq = &rq->cfs; | |
8094 | else | |
8095 | se->cfs_rq = parent->my_q; | |
8096 | ||
6f505b16 | 8097 | se->my_q = cfs_rq; |
9437178f | 8098 | update_load_set(&se->load, 0); |
ec7dc8ac | 8099 | se->parent = parent; |
6f505b16 | 8100 | } |
052f1dc7 | 8101 | #endif |
6f505b16 | 8102 | |
052f1dc7 | 8103 | #ifdef CONFIG_RT_GROUP_SCHED |
ec7dc8ac | 8104 | static void init_tg_rt_entry(struct task_group *tg, struct rt_rq *rt_rq, |
3d4b47b4 | 8105 | struct sched_rt_entity *rt_se, int cpu, |
ec7dc8ac | 8106 | struct sched_rt_entity *parent) |
6f505b16 | 8107 | { |
ec7dc8ac DG |
8108 | struct rq *rq = cpu_rq(cpu); |
8109 | ||
acb5a9ba JS |
8110 | rt_rq->highest_prio.curr = MAX_RT_PRIO; |
8111 | rt_rq->rt_nr_boosted = 0; | |
8112 | rt_rq->rq = rq; | |
6f505b16 | 8113 | rt_rq->tg = tg; |
6f505b16 | 8114 | |
acb5a9ba | 8115 | tg->rt_rq[cpu] = rt_rq; |
6f505b16 | 8116 | tg->rt_se[cpu] = rt_se; |
acb5a9ba | 8117 | |
354d60c2 DG |
8118 | if (!rt_se) |
8119 | return; | |
8120 | ||
ec7dc8ac DG |
8121 | if (!parent) |
8122 | rt_se->rt_rq = &rq->rt; | |
8123 | else | |
8124 | rt_se->rt_rq = parent->my_q; | |
8125 | ||
6f505b16 | 8126 | rt_se->my_q = rt_rq; |
ec7dc8ac | 8127 | rt_se->parent = parent; |
6f505b16 PZ |
8128 | INIT_LIST_HEAD(&rt_se->run_list); |
8129 | } | |
8130 | #endif | |
8131 | ||
1da177e4 LT |
8132 | void __init sched_init(void) |
8133 | { | |
dd41f596 | 8134 | int i, j; |
434d53b0 MT |
8135 | unsigned long alloc_size = 0, ptr; |
8136 | ||
8137 | #ifdef CONFIG_FAIR_GROUP_SCHED | |
8138 | alloc_size += 2 * nr_cpu_ids * sizeof(void **); | |
8139 | #endif | |
8140 | #ifdef CONFIG_RT_GROUP_SCHED | |
8141 | alloc_size += 2 * nr_cpu_ids * sizeof(void **); | |
eff766a6 | 8142 | #endif |
df7c8e84 | 8143 | #ifdef CONFIG_CPUMASK_OFFSTACK |
8c083f08 | 8144 | alloc_size += num_possible_cpus() * cpumask_size(); |
434d53b0 | 8145 | #endif |
434d53b0 | 8146 | if (alloc_size) { |
36b7b6d4 | 8147 | ptr = (unsigned long)kzalloc(alloc_size, GFP_NOWAIT); |
434d53b0 MT |
8148 | |
8149 | #ifdef CONFIG_FAIR_GROUP_SCHED | |
07e06b01 | 8150 | root_task_group.se = (struct sched_entity **)ptr; |
434d53b0 MT |
8151 | ptr += nr_cpu_ids * sizeof(void **); |
8152 | ||
07e06b01 | 8153 | root_task_group.cfs_rq = (struct cfs_rq **)ptr; |
434d53b0 | 8154 | ptr += nr_cpu_ids * sizeof(void **); |
eff766a6 | 8155 | |
6d6bc0ad | 8156 | #endif /* CONFIG_FAIR_GROUP_SCHED */ |
434d53b0 | 8157 | #ifdef CONFIG_RT_GROUP_SCHED |
07e06b01 | 8158 | root_task_group.rt_se = (struct sched_rt_entity **)ptr; |
434d53b0 MT |
8159 | ptr += nr_cpu_ids * sizeof(void **); |
8160 | ||
07e06b01 | 8161 | root_task_group.rt_rq = (struct rt_rq **)ptr; |
eff766a6 PZ |
8162 | ptr += nr_cpu_ids * sizeof(void **); |
8163 | ||
6d6bc0ad | 8164 | #endif /* CONFIG_RT_GROUP_SCHED */ |
df7c8e84 RR |
8165 | #ifdef CONFIG_CPUMASK_OFFSTACK |
8166 | for_each_possible_cpu(i) { | |
8167 | per_cpu(load_balance_tmpmask, i) = (void *)ptr; | |
8168 | ptr += cpumask_size(); | |
8169 | } | |
8170 | #endif /* CONFIG_CPUMASK_OFFSTACK */ | |
434d53b0 | 8171 | } |
dd41f596 | 8172 | |
57d885fe GH |
8173 | #ifdef CONFIG_SMP |
8174 | init_defrootdomain(); | |
8175 | #endif | |
8176 | ||
d0b27fa7 PZ |
8177 | init_rt_bandwidth(&def_rt_bandwidth, |
8178 | global_rt_period(), global_rt_runtime()); | |
8179 | ||
8180 | #ifdef CONFIG_RT_GROUP_SCHED | |
07e06b01 | 8181 | init_rt_bandwidth(&root_task_group.rt_bandwidth, |
d0b27fa7 | 8182 | global_rt_period(), global_rt_runtime()); |
6d6bc0ad | 8183 | #endif /* CONFIG_RT_GROUP_SCHED */ |
d0b27fa7 | 8184 | |
7c941438 | 8185 | #ifdef CONFIG_CGROUP_SCHED |
07e06b01 YZ |
8186 | list_add(&root_task_group.list, &task_groups); |
8187 | INIT_LIST_HEAD(&root_task_group.children); | |
5091faa4 | 8188 | autogroup_init(&init_task); |
7c941438 | 8189 | #endif /* CONFIG_CGROUP_SCHED */ |
6f505b16 | 8190 | |
0a945022 | 8191 | for_each_possible_cpu(i) { |
70b97a7f | 8192 | struct rq *rq; |
1da177e4 LT |
8193 | |
8194 | rq = cpu_rq(i); | |
05fa785c | 8195 | raw_spin_lock_init(&rq->lock); |
7897986b | 8196 | rq->nr_running = 0; |
dce48a84 TG |
8197 | rq->calc_load_active = 0; |
8198 | rq->calc_load_update = jiffies + LOAD_FREQ; | |
acb5a9ba | 8199 | init_cfs_rq(&rq->cfs); |
6f505b16 | 8200 | init_rt_rq(&rq->rt, rq); |
dd41f596 | 8201 | #ifdef CONFIG_FAIR_GROUP_SCHED |
07e06b01 | 8202 | root_task_group.shares = root_task_group_load; |
6f505b16 | 8203 | INIT_LIST_HEAD(&rq->leaf_cfs_rq_list); |
354d60c2 | 8204 | /* |
07e06b01 | 8205 | * How much cpu bandwidth does root_task_group get? |
354d60c2 DG |
8206 | * |
8207 | * In case of task-groups formed thr' the cgroup filesystem, it | |
8208 | * gets 100% of the cpu resources in the system. This overall | |
8209 | * system cpu resource is divided among the tasks of | |
07e06b01 | 8210 | * root_task_group and its child task-groups in a fair manner, |
354d60c2 DG |
8211 | * based on each entity's (task or task-group's) weight |
8212 | * (se->load.weight). | |
8213 | * | |
07e06b01 | 8214 | * In other words, if root_task_group has 10 tasks of weight |
354d60c2 DG |
8215 | * 1024) and two child groups A0 and A1 (of weight 1024 each), |
8216 | * then A0's share of the cpu resource is: | |
8217 | * | |
0d905bca | 8218 | * A0's bandwidth = 1024 / (10*1024 + 1024 + 1024) = 8.33% |
354d60c2 | 8219 | * |
07e06b01 YZ |
8220 | * We achieve this by letting root_task_group's tasks sit |
8221 | * directly in rq->cfs (i.e root_task_group->se[] = NULL). | |
354d60c2 | 8222 | */ |
ab84d31e | 8223 | init_cfs_bandwidth(&root_task_group.cfs_bandwidth); |
07e06b01 | 8224 | init_tg_cfs_entry(&root_task_group, &rq->cfs, NULL, i, NULL); |
354d60c2 DG |
8225 | #endif /* CONFIG_FAIR_GROUP_SCHED */ |
8226 | ||
8227 | rq->rt.rt_runtime = def_rt_bandwidth.rt_runtime; | |
052f1dc7 | 8228 | #ifdef CONFIG_RT_GROUP_SCHED |
6f505b16 | 8229 | INIT_LIST_HEAD(&rq->leaf_rt_rq_list); |
07e06b01 | 8230 | init_tg_rt_entry(&root_task_group, &rq->rt, NULL, i, NULL); |
dd41f596 | 8231 | #endif |
1da177e4 | 8232 | |
dd41f596 IM |
8233 | for (j = 0; j < CPU_LOAD_IDX_MAX; j++) |
8234 | rq->cpu_load[j] = 0; | |
fdf3e95d VP |
8235 | |
8236 | rq->last_load_update_tick = jiffies; | |
8237 | ||
1da177e4 | 8238 | #ifdef CONFIG_SMP |
41c7ce9a | 8239 | rq->sd = NULL; |
57d885fe | 8240 | rq->rd = NULL; |
1399fa78 | 8241 | rq->cpu_power = SCHED_POWER_SCALE; |
3f029d3c | 8242 | rq->post_schedule = 0; |
1da177e4 | 8243 | rq->active_balance = 0; |
dd41f596 | 8244 | rq->next_balance = jiffies; |
1da177e4 | 8245 | rq->push_cpu = 0; |
0a2966b4 | 8246 | rq->cpu = i; |
1f11eb6a | 8247 | rq->online = 0; |
eae0c9df MG |
8248 | rq->idle_stamp = 0; |
8249 | rq->avg_idle = 2*sysctl_sched_migration_cost; | |
dc938520 | 8250 | rq_attach_root(rq, &def_root_domain); |
83cd4fe2 VP |
8251 | #ifdef CONFIG_NO_HZ |
8252 | rq->nohz_balance_kick = 0; | |
8253 | init_sched_softirq_csd(&per_cpu(remote_sched_softirq_cb, i)); | |
8254 | #endif | |
1da177e4 | 8255 | #endif |
8f4d37ec | 8256 | init_rq_hrtick(rq); |
1da177e4 | 8257 | atomic_set(&rq->nr_iowait, 0); |
1da177e4 LT |
8258 | } |
8259 | ||
2dd73a4f | 8260 | set_load_weight(&init_task); |
b50f60ce | 8261 | |
e107be36 AK |
8262 | #ifdef CONFIG_PREEMPT_NOTIFIERS |
8263 | INIT_HLIST_HEAD(&init_task.preempt_notifiers); | |
8264 | #endif | |
8265 | ||
c9819f45 | 8266 | #ifdef CONFIG_SMP |
962cf36c | 8267 | open_softirq(SCHED_SOFTIRQ, run_rebalance_domains); |
c9819f45 CL |
8268 | #endif |
8269 | ||
b50f60ce | 8270 | #ifdef CONFIG_RT_MUTEXES |
732375c6 | 8271 | plist_head_init(&init_task.pi_waiters); |
b50f60ce HC |
8272 | #endif |
8273 | ||
1da177e4 LT |
8274 | /* |
8275 | * The boot idle thread does lazy MMU switching as well: | |
8276 | */ | |
8277 | atomic_inc(&init_mm.mm_count); | |
8278 | enter_lazy_tlb(&init_mm, current); | |
8279 | ||
8280 | /* | |
8281 | * Make us the idle thread. Technically, schedule() should not be | |
8282 | * called from this thread, however somewhere below it might be, | |
8283 | * but because we are the idle thread, we just pick up running again | |
8284 | * when this runqueue becomes "idle". | |
8285 | */ | |
8286 | init_idle(current, smp_processor_id()); | |
dce48a84 TG |
8287 | |
8288 | calc_load_update = jiffies + LOAD_FREQ; | |
8289 | ||
dd41f596 IM |
8290 | /* |
8291 | * During early bootup we pretend to be a normal task: | |
8292 | */ | |
8293 | current->sched_class = &fair_sched_class; | |
6892b75e | 8294 | |
6a7b3dc3 | 8295 | /* Allocate the nohz_cpu_mask if CONFIG_CPUMASK_OFFSTACK */ |
49557e62 | 8296 | zalloc_cpumask_var(&nohz_cpu_mask, GFP_NOWAIT); |
bf4d83f6 | 8297 | #ifdef CONFIG_SMP |
4cb98839 | 8298 | zalloc_cpumask_var(&sched_domains_tmpmask, GFP_NOWAIT); |
7d1e6a9b | 8299 | #ifdef CONFIG_NO_HZ |
83cd4fe2 VP |
8300 | zalloc_cpumask_var(&nohz.idle_cpus_mask, GFP_NOWAIT); |
8301 | alloc_cpumask_var(&nohz.grp_idle_mask, GFP_NOWAIT); | |
8302 | atomic_set(&nohz.load_balancer, nr_cpu_ids); | |
8303 | atomic_set(&nohz.first_pick_cpu, nr_cpu_ids); | |
8304 | atomic_set(&nohz.second_pick_cpu, nr_cpu_ids); | |
7d1e6a9b | 8305 | #endif |
bdddd296 RR |
8306 | /* May be allocated at isolcpus cmdline parse time */ |
8307 | if (cpu_isolated_map == NULL) | |
8308 | zalloc_cpumask_var(&cpu_isolated_map, GFP_NOWAIT); | |
bf4d83f6 | 8309 | #endif /* SMP */ |
6a7b3dc3 | 8310 | |
6892b75e | 8311 | scheduler_running = 1; |
1da177e4 LT |
8312 | } |
8313 | ||
d902db1e | 8314 | #ifdef CONFIG_DEBUG_ATOMIC_SLEEP |
e4aafea2 FW |
8315 | static inline int preempt_count_equals(int preempt_offset) |
8316 | { | |
234da7bc | 8317 | int nested = (preempt_count() & ~PREEMPT_ACTIVE) + rcu_preempt_depth(); |
e4aafea2 | 8318 | |
4ba8216c | 8319 | return (nested == preempt_offset); |
e4aafea2 FW |
8320 | } |
8321 | ||
d894837f | 8322 | void __might_sleep(const char *file, int line, int preempt_offset) |
1da177e4 | 8323 | { |
1da177e4 LT |
8324 | static unsigned long prev_jiffy; /* ratelimiting */ |
8325 | ||
e4aafea2 FW |
8326 | if ((preempt_count_equals(preempt_offset) && !irqs_disabled()) || |
8327 | system_state != SYSTEM_RUNNING || oops_in_progress) | |
aef745fc IM |
8328 | return; |
8329 | if (time_before(jiffies, prev_jiffy + HZ) && prev_jiffy) | |
8330 | return; | |
8331 | prev_jiffy = jiffies; | |
8332 | ||
3df0fc5b PZ |
8333 | printk(KERN_ERR |
8334 | "BUG: sleeping function called from invalid context at %s:%d\n", | |
8335 | file, line); | |
8336 | printk(KERN_ERR | |
8337 | "in_atomic(): %d, irqs_disabled(): %d, pid: %d, name: %s\n", | |
8338 | in_atomic(), irqs_disabled(), | |
8339 | current->pid, current->comm); | |
aef745fc IM |
8340 | |
8341 | debug_show_held_locks(current); | |
8342 | if (irqs_disabled()) | |
8343 | print_irqtrace_events(current); | |
8344 | dump_stack(); | |
1da177e4 LT |
8345 | } |
8346 | EXPORT_SYMBOL(__might_sleep); | |
8347 | #endif | |
8348 | ||
8349 | #ifdef CONFIG_MAGIC_SYSRQ | |
3a5e4dc1 AK |
8350 | static void normalize_task(struct rq *rq, struct task_struct *p) |
8351 | { | |
da7a735e PZ |
8352 | const struct sched_class *prev_class = p->sched_class; |
8353 | int old_prio = p->prio; | |
3a5e4dc1 | 8354 | int on_rq; |
3e51f33f | 8355 | |
fd2f4419 | 8356 | on_rq = p->on_rq; |
3a5e4dc1 AK |
8357 | if (on_rq) |
8358 | deactivate_task(rq, p, 0); | |
8359 | __setscheduler(rq, p, SCHED_NORMAL, 0); | |
8360 | if (on_rq) { | |
8361 | activate_task(rq, p, 0); | |
8362 | resched_task(rq->curr); | |
8363 | } | |
da7a735e PZ |
8364 | |
8365 | check_class_changed(rq, p, prev_class, old_prio); | |
3a5e4dc1 AK |
8366 | } |
8367 | ||
1da177e4 LT |
8368 | void normalize_rt_tasks(void) |
8369 | { | |
a0f98a1c | 8370 | struct task_struct *g, *p; |
1da177e4 | 8371 | unsigned long flags; |
70b97a7f | 8372 | struct rq *rq; |
1da177e4 | 8373 | |
4cf5d77a | 8374 | read_lock_irqsave(&tasklist_lock, flags); |
a0f98a1c | 8375 | do_each_thread(g, p) { |
178be793 IM |
8376 | /* |
8377 | * Only normalize user tasks: | |
8378 | */ | |
8379 | if (!p->mm) | |
8380 | continue; | |
8381 | ||
6cfb0d5d | 8382 | p->se.exec_start = 0; |
6cfb0d5d | 8383 | #ifdef CONFIG_SCHEDSTATS |
41acab88 LDM |
8384 | p->se.statistics.wait_start = 0; |
8385 | p->se.statistics.sleep_start = 0; | |
8386 | p->se.statistics.block_start = 0; | |
6cfb0d5d | 8387 | #endif |
dd41f596 IM |
8388 | |
8389 | if (!rt_task(p)) { | |
8390 | /* | |
8391 | * Renice negative nice level userspace | |
8392 | * tasks back to 0: | |
8393 | */ | |
8394 | if (TASK_NICE(p) < 0 && p->mm) | |
8395 | set_user_nice(p, 0); | |
1da177e4 | 8396 | continue; |
dd41f596 | 8397 | } |
1da177e4 | 8398 | |
1d615482 | 8399 | raw_spin_lock(&p->pi_lock); |
b29739f9 | 8400 | rq = __task_rq_lock(p); |
1da177e4 | 8401 | |
178be793 | 8402 | normalize_task(rq, p); |
3a5e4dc1 | 8403 | |
b29739f9 | 8404 | __task_rq_unlock(rq); |
1d615482 | 8405 | raw_spin_unlock(&p->pi_lock); |
a0f98a1c IM |
8406 | } while_each_thread(g, p); |
8407 | ||
4cf5d77a | 8408 | read_unlock_irqrestore(&tasklist_lock, flags); |
1da177e4 LT |
8409 | } |
8410 | ||
8411 | #endif /* CONFIG_MAGIC_SYSRQ */ | |
1df5c10a | 8412 | |
67fc4e0c | 8413 | #if defined(CONFIG_IA64) || defined(CONFIG_KGDB_KDB) |
1df5c10a | 8414 | /* |
67fc4e0c | 8415 | * These functions are only useful for the IA64 MCA handling, or kdb. |
1df5c10a LT |
8416 | * |
8417 | * They can only be called when the whole system has been | |
8418 | * stopped - every CPU needs to be quiescent, and no scheduling | |
8419 | * activity can take place. Using them for anything else would | |
8420 | * be a serious bug, and as a result, they aren't even visible | |
8421 | * under any other configuration. | |
8422 | */ | |
8423 | ||
8424 | /** | |
8425 | * curr_task - return the current task for a given cpu. | |
8426 | * @cpu: the processor in question. | |
8427 | * | |
8428 | * ONLY VALID WHEN THE WHOLE SYSTEM IS STOPPED! | |
8429 | */ | |
36c8b586 | 8430 | struct task_struct *curr_task(int cpu) |
1df5c10a LT |
8431 | { |
8432 | return cpu_curr(cpu); | |
8433 | } | |
8434 | ||
67fc4e0c JW |
8435 | #endif /* defined(CONFIG_IA64) || defined(CONFIG_KGDB_KDB) */ |
8436 | ||
8437 | #ifdef CONFIG_IA64 | |
1df5c10a LT |
8438 | /** |
8439 | * set_curr_task - set the current task for a given cpu. | |
8440 | * @cpu: the processor in question. | |
8441 | * @p: the task pointer to set. | |
8442 | * | |
8443 | * Description: This function must only be used when non-maskable interrupts | |
41a2d6cf IM |
8444 | * are serviced on a separate stack. It allows the architecture to switch the |
8445 | * notion of the current task on a cpu in a non-blocking manner. This function | |
1df5c10a LT |
8446 | * must be called with all CPU's synchronized, and interrupts disabled, the |
8447 | * and caller must save the original value of the current task (see | |
8448 | * curr_task() above) and restore that value before reenabling interrupts and | |
8449 | * re-starting the system. | |
8450 | * | |
8451 | * ONLY VALID WHEN THE WHOLE SYSTEM IS STOPPED! | |
8452 | */ | |
36c8b586 | 8453 | void set_curr_task(int cpu, struct task_struct *p) |
1df5c10a LT |
8454 | { |
8455 | cpu_curr(cpu) = p; | |
8456 | } | |
8457 | ||
8458 | #endif | |
29f59db3 | 8459 | |
bccbe08a PZ |
8460 | #ifdef CONFIG_FAIR_GROUP_SCHED |
8461 | static void free_fair_sched_group(struct task_group *tg) | |
6f505b16 PZ |
8462 | { |
8463 | int i; | |
8464 | ||
ab84d31e PT |
8465 | destroy_cfs_bandwidth(tg_cfs_bandwidth(tg)); |
8466 | ||
6f505b16 PZ |
8467 | for_each_possible_cpu(i) { |
8468 | if (tg->cfs_rq) | |
8469 | kfree(tg->cfs_rq[i]); | |
8470 | if (tg->se) | |
8471 | kfree(tg->se[i]); | |
6f505b16 PZ |
8472 | } |
8473 | ||
8474 | kfree(tg->cfs_rq); | |
8475 | kfree(tg->se); | |
6f505b16 PZ |
8476 | } |
8477 | ||
ec7dc8ac DG |
8478 | static |
8479 | int alloc_fair_sched_group(struct task_group *tg, struct task_group *parent) | |
29f59db3 | 8480 | { |
29f59db3 | 8481 | struct cfs_rq *cfs_rq; |
eab17229 | 8482 | struct sched_entity *se; |
29f59db3 SV |
8483 | int i; |
8484 | ||
434d53b0 | 8485 | tg->cfs_rq = kzalloc(sizeof(cfs_rq) * nr_cpu_ids, GFP_KERNEL); |
29f59db3 SV |
8486 | if (!tg->cfs_rq) |
8487 | goto err; | |
434d53b0 | 8488 | tg->se = kzalloc(sizeof(se) * nr_cpu_ids, GFP_KERNEL); |
29f59db3 SV |
8489 | if (!tg->se) |
8490 | goto err; | |
052f1dc7 PZ |
8491 | |
8492 | tg->shares = NICE_0_LOAD; | |
29f59db3 | 8493 | |
ab84d31e PT |
8494 | init_cfs_bandwidth(tg_cfs_bandwidth(tg)); |
8495 | ||
29f59db3 | 8496 | for_each_possible_cpu(i) { |
eab17229 LZ |
8497 | cfs_rq = kzalloc_node(sizeof(struct cfs_rq), |
8498 | GFP_KERNEL, cpu_to_node(i)); | |
29f59db3 SV |
8499 | if (!cfs_rq) |
8500 | goto err; | |
8501 | ||
eab17229 LZ |
8502 | se = kzalloc_node(sizeof(struct sched_entity), |
8503 | GFP_KERNEL, cpu_to_node(i)); | |
29f59db3 | 8504 | if (!se) |
dfc12eb2 | 8505 | goto err_free_rq; |
29f59db3 | 8506 | |
acb5a9ba | 8507 | init_cfs_rq(cfs_rq); |
3d4b47b4 | 8508 | init_tg_cfs_entry(tg, cfs_rq, se, i, parent->se[i]); |
bccbe08a PZ |
8509 | } |
8510 | ||
8511 | return 1; | |
8512 | ||
49246274 | 8513 | err_free_rq: |
dfc12eb2 | 8514 | kfree(cfs_rq); |
49246274 | 8515 | err: |
bccbe08a PZ |
8516 | return 0; |
8517 | } | |
8518 | ||
bccbe08a PZ |
8519 | static inline void unregister_fair_sched_group(struct task_group *tg, int cpu) |
8520 | { | |
3d4b47b4 PZ |
8521 | struct rq *rq = cpu_rq(cpu); |
8522 | unsigned long flags; | |
3d4b47b4 PZ |
8523 | |
8524 | /* | |
8525 | * Only empty task groups can be destroyed; so we can speculatively | |
8526 | * check on_list without danger of it being re-added. | |
8527 | */ | |
8528 | if (!tg->cfs_rq[cpu]->on_list) | |
8529 | return; | |
8530 | ||
8531 | raw_spin_lock_irqsave(&rq->lock, flags); | |
822bc180 | 8532 | list_del_leaf_cfs_rq(tg->cfs_rq[cpu]); |
3d4b47b4 | 8533 | raw_spin_unlock_irqrestore(&rq->lock, flags); |
bccbe08a | 8534 | } |
5f817d67 | 8535 | #else /* !CONFIG_FAIR_GROUP_SCHED */ |
bccbe08a PZ |
8536 | static inline void free_fair_sched_group(struct task_group *tg) |
8537 | { | |
8538 | } | |
8539 | ||
ec7dc8ac DG |
8540 | static inline |
8541 | int alloc_fair_sched_group(struct task_group *tg, struct task_group *parent) | |
bccbe08a PZ |
8542 | { |
8543 | return 1; | |
8544 | } | |
8545 | ||
bccbe08a PZ |
8546 | static inline void unregister_fair_sched_group(struct task_group *tg, int cpu) |
8547 | { | |
8548 | } | |
6d6bc0ad | 8549 | #endif /* CONFIG_FAIR_GROUP_SCHED */ |
052f1dc7 PZ |
8550 | |
8551 | #ifdef CONFIG_RT_GROUP_SCHED | |
bccbe08a PZ |
8552 | static void free_rt_sched_group(struct task_group *tg) |
8553 | { | |
8554 | int i; | |
8555 | ||
99bc5242 BL |
8556 | if (tg->rt_se) |
8557 | destroy_rt_bandwidth(&tg->rt_bandwidth); | |
d0b27fa7 | 8558 | |
bccbe08a PZ |
8559 | for_each_possible_cpu(i) { |
8560 | if (tg->rt_rq) | |
8561 | kfree(tg->rt_rq[i]); | |
8562 | if (tg->rt_se) | |
8563 | kfree(tg->rt_se[i]); | |
8564 | } | |
8565 | ||
8566 | kfree(tg->rt_rq); | |
8567 | kfree(tg->rt_se); | |
8568 | } | |
8569 | ||
ec7dc8ac DG |
8570 | static |
8571 | int alloc_rt_sched_group(struct task_group *tg, struct task_group *parent) | |
bccbe08a PZ |
8572 | { |
8573 | struct rt_rq *rt_rq; | |
eab17229 | 8574 | struct sched_rt_entity *rt_se; |
bccbe08a PZ |
8575 | int i; |
8576 | ||
434d53b0 | 8577 | tg->rt_rq = kzalloc(sizeof(rt_rq) * nr_cpu_ids, GFP_KERNEL); |
bccbe08a PZ |
8578 | if (!tg->rt_rq) |
8579 | goto err; | |
434d53b0 | 8580 | tg->rt_se = kzalloc(sizeof(rt_se) * nr_cpu_ids, GFP_KERNEL); |
bccbe08a PZ |
8581 | if (!tg->rt_se) |
8582 | goto err; | |
8583 | ||
d0b27fa7 PZ |
8584 | init_rt_bandwidth(&tg->rt_bandwidth, |
8585 | ktime_to_ns(def_rt_bandwidth.rt_period), 0); | |
bccbe08a PZ |
8586 | |
8587 | for_each_possible_cpu(i) { | |
eab17229 LZ |
8588 | rt_rq = kzalloc_node(sizeof(struct rt_rq), |
8589 | GFP_KERNEL, cpu_to_node(i)); | |
6f505b16 PZ |
8590 | if (!rt_rq) |
8591 | goto err; | |
29f59db3 | 8592 | |
eab17229 LZ |
8593 | rt_se = kzalloc_node(sizeof(struct sched_rt_entity), |
8594 | GFP_KERNEL, cpu_to_node(i)); | |
6f505b16 | 8595 | if (!rt_se) |
dfc12eb2 | 8596 | goto err_free_rq; |
29f59db3 | 8597 | |
acb5a9ba JS |
8598 | init_rt_rq(rt_rq, cpu_rq(i)); |
8599 | rt_rq->rt_runtime = tg->rt_bandwidth.rt_runtime; | |
3d4b47b4 | 8600 | init_tg_rt_entry(tg, rt_rq, rt_se, i, parent->rt_se[i]); |
29f59db3 SV |
8601 | } |
8602 | ||
bccbe08a PZ |
8603 | return 1; |
8604 | ||
49246274 | 8605 | err_free_rq: |
dfc12eb2 | 8606 | kfree(rt_rq); |
49246274 | 8607 | err: |
bccbe08a PZ |
8608 | return 0; |
8609 | } | |
6d6bc0ad | 8610 | #else /* !CONFIG_RT_GROUP_SCHED */ |
bccbe08a PZ |
8611 | static inline void free_rt_sched_group(struct task_group *tg) |
8612 | { | |
8613 | } | |
8614 | ||
ec7dc8ac DG |
8615 | static inline |
8616 | int alloc_rt_sched_group(struct task_group *tg, struct task_group *parent) | |
bccbe08a PZ |
8617 | { |
8618 | return 1; | |
8619 | } | |
6d6bc0ad | 8620 | #endif /* CONFIG_RT_GROUP_SCHED */ |
bccbe08a | 8621 | |
7c941438 | 8622 | #ifdef CONFIG_CGROUP_SCHED |
bccbe08a PZ |
8623 | static void free_sched_group(struct task_group *tg) |
8624 | { | |
8625 | free_fair_sched_group(tg); | |
8626 | free_rt_sched_group(tg); | |
e9aa1dd1 | 8627 | autogroup_free(tg); |
bccbe08a PZ |
8628 | kfree(tg); |
8629 | } | |
8630 | ||
8631 | /* allocate runqueue etc for a new task group */ | |
ec7dc8ac | 8632 | struct task_group *sched_create_group(struct task_group *parent) |
bccbe08a PZ |
8633 | { |
8634 | struct task_group *tg; | |
8635 | unsigned long flags; | |
bccbe08a PZ |
8636 | |
8637 | tg = kzalloc(sizeof(*tg), GFP_KERNEL); | |
8638 | if (!tg) | |
8639 | return ERR_PTR(-ENOMEM); | |
8640 | ||
ec7dc8ac | 8641 | if (!alloc_fair_sched_group(tg, parent)) |
bccbe08a PZ |
8642 | goto err; |
8643 | ||
ec7dc8ac | 8644 | if (!alloc_rt_sched_group(tg, parent)) |
bccbe08a PZ |
8645 | goto err; |
8646 | ||
8ed36996 | 8647 | spin_lock_irqsave(&task_group_lock, flags); |
6f505b16 | 8648 | list_add_rcu(&tg->list, &task_groups); |
f473aa5e PZ |
8649 | |
8650 | WARN_ON(!parent); /* root should already exist */ | |
8651 | ||
8652 | tg->parent = parent; | |
f473aa5e | 8653 | INIT_LIST_HEAD(&tg->children); |
09f2724a | 8654 | list_add_rcu(&tg->siblings, &parent->children); |
8ed36996 | 8655 | spin_unlock_irqrestore(&task_group_lock, flags); |
29f59db3 | 8656 | |
9b5b7751 | 8657 | return tg; |
29f59db3 SV |
8658 | |
8659 | err: | |
6f505b16 | 8660 | free_sched_group(tg); |
29f59db3 SV |
8661 | return ERR_PTR(-ENOMEM); |
8662 | } | |
8663 | ||
9b5b7751 | 8664 | /* rcu callback to free various structures associated with a task group */ |
6f505b16 | 8665 | static void free_sched_group_rcu(struct rcu_head *rhp) |
29f59db3 | 8666 | { |
29f59db3 | 8667 | /* now it should be safe to free those cfs_rqs */ |
6f505b16 | 8668 | free_sched_group(container_of(rhp, struct task_group, rcu)); |
29f59db3 SV |
8669 | } |
8670 | ||
9b5b7751 | 8671 | /* Destroy runqueue etc associated with a task group */ |
4cf86d77 | 8672 | void sched_destroy_group(struct task_group *tg) |
29f59db3 | 8673 | { |
8ed36996 | 8674 | unsigned long flags; |
9b5b7751 | 8675 | int i; |
29f59db3 | 8676 | |
3d4b47b4 PZ |
8677 | /* end participation in shares distribution */ |
8678 | for_each_possible_cpu(i) | |
bccbe08a | 8679 | unregister_fair_sched_group(tg, i); |
3d4b47b4 PZ |
8680 | |
8681 | spin_lock_irqsave(&task_group_lock, flags); | |
6f505b16 | 8682 | list_del_rcu(&tg->list); |
f473aa5e | 8683 | list_del_rcu(&tg->siblings); |
8ed36996 | 8684 | spin_unlock_irqrestore(&task_group_lock, flags); |
9b5b7751 | 8685 | |
9b5b7751 | 8686 | /* wait for possible concurrent references to cfs_rqs complete */ |
6f505b16 | 8687 | call_rcu(&tg->rcu, free_sched_group_rcu); |
29f59db3 SV |
8688 | } |
8689 | ||
9b5b7751 | 8690 | /* change task's runqueue when it moves between groups. |
3a252015 IM |
8691 | * The caller of this function should have put the task in its new group |
8692 | * by now. This function just updates tsk->se.cfs_rq and tsk->se.parent to | |
8693 | * reflect its new group. | |
9b5b7751 SV |
8694 | */ |
8695 | void sched_move_task(struct task_struct *tsk) | |
29f59db3 SV |
8696 | { |
8697 | int on_rq, running; | |
8698 | unsigned long flags; | |
8699 | struct rq *rq; | |
8700 | ||
8701 | rq = task_rq_lock(tsk, &flags); | |
8702 | ||
051a1d1a | 8703 | running = task_current(rq, tsk); |
fd2f4419 | 8704 | on_rq = tsk->on_rq; |
29f59db3 | 8705 | |
0e1f3483 | 8706 | if (on_rq) |
29f59db3 | 8707 | dequeue_task(rq, tsk, 0); |
0e1f3483 HS |
8708 | if (unlikely(running)) |
8709 | tsk->sched_class->put_prev_task(rq, tsk); | |
29f59db3 | 8710 | |
810b3817 | 8711 | #ifdef CONFIG_FAIR_GROUP_SCHED |
b2b5ce02 PZ |
8712 | if (tsk->sched_class->task_move_group) |
8713 | tsk->sched_class->task_move_group(tsk, on_rq); | |
8714 | else | |
810b3817 | 8715 | #endif |
b2b5ce02 | 8716 | set_task_rq(tsk, task_cpu(tsk)); |
810b3817 | 8717 | |
0e1f3483 HS |
8718 | if (unlikely(running)) |
8719 | tsk->sched_class->set_curr_task(rq); | |
8720 | if (on_rq) | |
371fd7e7 | 8721 | enqueue_task(rq, tsk, 0); |
29f59db3 | 8722 | |
0122ec5b | 8723 | task_rq_unlock(rq, tsk, &flags); |
29f59db3 | 8724 | } |
7c941438 | 8725 | #endif /* CONFIG_CGROUP_SCHED */ |
29f59db3 | 8726 | |
052f1dc7 | 8727 | #ifdef CONFIG_FAIR_GROUP_SCHED |
8ed36996 PZ |
8728 | static DEFINE_MUTEX(shares_mutex); |
8729 | ||
4cf86d77 | 8730 | int sched_group_set_shares(struct task_group *tg, unsigned long shares) |
29f59db3 SV |
8731 | { |
8732 | int i; | |
8ed36996 | 8733 | unsigned long flags; |
c61935fd | 8734 | |
ec7dc8ac DG |
8735 | /* |
8736 | * We can't change the weight of the root cgroup. | |
8737 | */ | |
8738 | if (!tg->se[0]) | |
8739 | return -EINVAL; | |
8740 | ||
cd62287e | 8741 | shares = clamp(shares, scale_load(MIN_SHARES), scale_load(MAX_SHARES)); |
62fb1851 | 8742 | |
8ed36996 | 8743 | mutex_lock(&shares_mutex); |
9b5b7751 | 8744 | if (tg->shares == shares) |
5cb350ba | 8745 | goto done; |
29f59db3 | 8746 | |
9b5b7751 | 8747 | tg->shares = shares; |
c09595f6 | 8748 | for_each_possible_cpu(i) { |
9437178f PT |
8749 | struct rq *rq = cpu_rq(i); |
8750 | struct sched_entity *se; | |
8751 | ||
8752 | se = tg->se[i]; | |
8753 | /* Propagate contribution to hierarchy */ | |
8754 | raw_spin_lock_irqsave(&rq->lock, flags); | |
8755 | for_each_sched_entity(se) | |
6d5ab293 | 8756 | update_cfs_shares(group_cfs_rq(se)); |
9437178f | 8757 | raw_spin_unlock_irqrestore(&rq->lock, flags); |
c09595f6 | 8758 | } |
29f59db3 | 8759 | |
5cb350ba | 8760 | done: |
8ed36996 | 8761 | mutex_unlock(&shares_mutex); |
9b5b7751 | 8762 | return 0; |
29f59db3 SV |
8763 | } |
8764 | ||
5cb350ba DG |
8765 | unsigned long sched_group_shares(struct task_group *tg) |
8766 | { | |
8767 | return tg->shares; | |
8768 | } | |
052f1dc7 | 8769 | #endif |
5cb350ba | 8770 | |
a790de99 | 8771 | #if defined(CONFIG_RT_GROUP_SCHED) || defined(CONFIG_CFS_BANDWIDTH) |
9f0c1e56 PZ |
8772 | static unsigned long to_ratio(u64 period, u64 runtime) |
8773 | { | |
8774 | if (runtime == RUNTIME_INF) | |
9a7e0b18 | 8775 | return 1ULL << 20; |
9f0c1e56 | 8776 | |
9a7e0b18 | 8777 | return div64_u64(runtime << 20, period); |
9f0c1e56 | 8778 | } |
a790de99 PT |
8779 | #endif |
8780 | ||
8781 | #ifdef CONFIG_RT_GROUP_SCHED | |
8782 | /* | |
8783 | * Ensure that the real time constraints are schedulable. | |
8784 | */ | |
8785 | static DEFINE_MUTEX(rt_constraints_mutex); | |
9f0c1e56 | 8786 | |
9a7e0b18 PZ |
8787 | /* Must be called with tasklist_lock held */ |
8788 | static inline int tg_has_rt_tasks(struct task_group *tg) | |
b40b2e8e | 8789 | { |
9a7e0b18 | 8790 | struct task_struct *g, *p; |
b40b2e8e | 8791 | |
9a7e0b18 PZ |
8792 | do_each_thread(g, p) { |
8793 | if (rt_task(p) && rt_rq_of_se(&p->rt)->tg == tg) | |
8794 | return 1; | |
8795 | } while_each_thread(g, p); | |
b40b2e8e | 8796 | |
9a7e0b18 PZ |
8797 | return 0; |
8798 | } | |
b40b2e8e | 8799 | |
9a7e0b18 PZ |
8800 | struct rt_schedulable_data { |
8801 | struct task_group *tg; | |
8802 | u64 rt_period; | |
8803 | u64 rt_runtime; | |
8804 | }; | |
b40b2e8e | 8805 | |
a790de99 | 8806 | static int tg_rt_schedulable(struct task_group *tg, void *data) |
9a7e0b18 PZ |
8807 | { |
8808 | struct rt_schedulable_data *d = data; | |
8809 | struct task_group *child; | |
8810 | unsigned long total, sum = 0; | |
8811 | u64 period, runtime; | |
b40b2e8e | 8812 | |
9a7e0b18 PZ |
8813 | period = ktime_to_ns(tg->rt_bandwidth.rt_period); |
8814 | runtime = tg->rt_bandwidth.rt_runtime; | |
b40b2e8e | 8815 | |
9a7e0b18 PZ |
8816 | if (tg == d->tg) { |
8817 | period = d->rt_period; | |
8818 | runtime = d->rt_runtime; | |
b40b2e8e | 8819 | } |
b40b2e8e | 8820 | |
4653f803 PZ |
8821 | /* |
8822 | * Cannot have more runtime than the period. | |
8823 | */ | |
8824 | if (runtime > period && runtime != RUNTIME_INF) | |
8825 | return -EINVAL; | |
6f505b16 | 8826 | |
4653f803 PZ |
8827 | /* |
8828 | * Ensure we don't starve existing RT tasks. | |
8829 | */ | |
9a7e0b18 PZ |
8830 | if (rt_bandwidth_enabled() && !runtime && tg_has_rt_tasks(tg)) |
8831 | return -EBUSY; | |
6f505b16 | 8832 | |
9a7e0b18 | 8833 | total = to_ratio(period, runtime); |
6f505b16 | 8834 | |
4653f803 PZ |
8835 | /* |
8836 | * Nobody can have more than the global setting allows. | |
8837 | */ | |
8838 | if (total > to_ratio(global_rt_period(), global_rt_runtime())) | |
8839 | return -EINVAL; | |
6f505b16 | 8840 | |
4653f803 PZ |
8841 | /* |
8842 | * The sum of our children's runtime should not exceed our own. | |
8843 | */ | |
9a7e0b18 PZ |
8844 | list_for_each_entry_rcu(child, &tg->children, siblings) { |
8845 | period = ktime_to_ns(child->rt_bandwidth.rt_period); | |
8846 | runtime = child->rt_bandwidth.rt_runtime; | |
6f505b16 | 8847 | |
9a7e0b18 PZ |
8848 | if (child == d->tg) { |
8849 | period = d->rt_period; | |
8850 | runtime = d->rt_runtime; | |
8851 | } | |
6f505b16 | 8852 | |
9a7e0b18 | 8853 | sum += to_ratio(period, runtime); |
9f0c1e56 | 8854 | } |
6f505b16 | 8855 | |
9a7e0b18 PZ |
8856 | if (sum > total) |
8857 | return -EINVAL; | |
8858 | ||
8859 | return 0; | |
6f505b16 PZ |
8860 | } |
8861 | ||
9a7e0b18 | 8862 | static int __rt_schedulable(struct task_group *tg, u64 period, u64 runtime) |
521f1a24 | 8863 | { |
9a7e0b18 PZ |
8864 | struct rt_schedulable_data data = { |
8865 | .tg = tg, | |
8866 | .rt_period = period, | |
8867 | .rt_runtime = runtime, | |
8868 | }; | |
8869 | ||
a790de99 | 8870 | return walk_tg_tree(tg_rt_schedulable, tg_nop, &data); |
521f1a24 DG |
8871 | } |
8872 | ||
ab84d31e | 8873 | static int tg_set_rt_bandwidth(struct task_group *tg, |
d0b27fa7 | 8874 | u64 rt_period, u64 rt_runtime) |
6f505b16 | 8875 | { |
ac086bc2 | 8876 | int i, err = 0; |
9f0c1e56 | 8877 | |
9f0c1e56 | 8878 | mutex_lock(&rt_constraints_mutex); |
521f1a24 | 8879 | read_lock(&tasklist_lock); |
9a7e0b18 PZ |
8880 | err = __rt_schedulable(tg, rt_period, rt_runtime); |
8881 | if (err) | |
9f0c1e56 | 8882 | goto unlock; |
ac086bc2 | 8883 | |
0986b11b | 8884 | raw_spin_lock_irq(&tg->rt_bandwidth.rt_runtime_lock); |
d0b27fa7 PZ |
8885 | tg->rt_bandwidth.rt_period = ns_to_ktime(rt_period); |
8886 | tg->rt_bandwidth.rt_runtime = rt_runtime; | |
ac086bc2 PZ |
8887 | |
8888 | for_each_possible_cpu(i) { | |
8889 | struct rt_rq *rt_rq = tg->rt_rq[i]; | |
8890 | ||
0986b11b | 8891 | raw_spin_lock(&rt_rq->rt_runtime_lock); |
ac086bc2 | 8892 | rt_rq->rt_runtime = rt_runtime; |
0986b11b | 8893 | raw_spin_unlock(&rt_rq->rt_runtime_lock); |
ac086bc2 | 8894 | } |
0986b11b | 8895 | raw_spin_unlock_irq(&tg->rt_bandwidth.rt_runtime_lock); |
49246274 | 8896 | unlock: |
521f1a24 | 8897 | read_unlock(&tasklist_lock); |
9f0c1e56 PZ |
8898 | mutex_unlock(&rt_constraints_mutex); |
8899 | ||
8900 | return err; | |
6f505b16 PZ |
8901 | } |
8902 | ||
d0b27fa7 PZ |
8903 | int sched_group_set_rt_runtime(struct task_group *tg, long rt_runtime_us) |
8904 | { | |
8905 | u64 rt_runtime, rt_period; | |
8906 | ||
8907 | rt_period = ktime_to_ns(tg->rt_bandwidth.rt_period); | |
8908 | rt_runtime = (u64)rt_runtime_us * NSEC_PER_USEC; | |
8909 | if (rt_runtime_us < 0) | |
8910 | rt_runtime = RUNTIME_INF; | |
8911 | ||
ab84d31e | 8912 | return tg_set_rt_bandwidth(tg, rt_period, rt_runtime); |
d0b27fa7 PZ |
8913 | } |
8914 | ||
9f0c1e56 PZ |
8915 | long sched_group_rt_runtime(struct task_group *tg) |
8916 | { | |
8917 | u64 rt_runtime_us; | |
8918 | ||
d0b27fa7 | 8919 | if (tg->rt_bandwidth.rt_runtime == RUNTIME_INF) |
9f0c1e56 PZ |
8920 | return -1; |
8921 | ||
d0b27fa7 | 8922 | rt_runtime_us = tg->rt_bandwidth.rt_runtime; |
9f0c1e56 PZ |
8923 | do_div(rt_runtime_us, NSEC_PER_USEC); |
8924 | return rt_runtime_us; | |
8925 | } | |
d0b27fa7 PZ |
8926 | |
8927 | int sched_group_set_rt_period(struct task_group *tg, long rt_period_us) | |
8928 | { | |
8929 | u64 rt_runtime, rt_period; | |
8930 | ||
8931 | rt_period = (u64)rt_period_us * NSEC_PER_USEC; | |
8932 | rt_runtime = tg->rt_bandwidth.rt_runtime; | |
8933 | ||
619b0488 R |
8934 | if (rt_period == 0) |
8935 | return -EINVAL; | |
8936 | ||
ab84d31e | 8937 | return tg_set_rt_bandwidth(tg, rt_period, rt_runtime); |
d0b27fa7 PZ |
8938 | } |
8939 | ||
8940 | long sched_group_rt_period(struct task_group *tg) | |
8941 | { | |
8942 | u64 rt_period_us; | |
8943 | ||
8944 | rt_period_us = ktime_to_ns(tg->rt_bandwidth.rt_period); | |
8945 | do_div(rt_period_us, NSEC_PER_USEC); | |
8946 | return rt_period_us; | |
8947 | } | |
8948 | ||
8949 | static int sched_rt_global_constraints(void) | |
8950 | { | |
4653f803 | 8951 | u64 runtime, period; |
d0b27fa7 PZ |
8952 | int ret = 0; |
8953 | ||
ec5d4989 HS |
8954 | if (sysctl_sched_rt_period <= 0) |
8955 | return -EINVAL; | |
8956 | ||
4653f803 PZ |
8957 | runtime = global_rt_runtime(); |
8958 | period = global_rt_period(); | |
8959 | ||
8960 | /* | |
8961 | * Sanity check on the sysctl variables. | |
8962 | */ | |
8963 | if (runtime > period && runtime != RUNTIME_INF) | |
8964 | return -EINVAL; | |
10b612f4 | 8965 | |
d0b27fa7 | 8966 | mutex_lock(&rt_constraints_mutex); |
9a7e0b18 | 8967 | read_lock(&tasklist_lock); |
4653f803 | 8968 | ret = __rt_schedulable(NULL, 0, 0); |
9a7e0b18 | 8969 | read_unlock(&tasklist_lock); |
d0b27fa7 PZ |
8970 | mutex_unlock(&rt_constraints_mutex); |
8971 | ||
8972 | return ret; | |
8973 | } | |
54e99124 DG |
8974 | |
8975 | int sched_rt_can_attach(struct task_group *tg, struct task_struct *tsk) | |
8976 | { | |
8977 | /* Don't accept realtime tasks when there is no way for them to run */ | |
8978 | if (rt_task(tsk) && tg->rt_bandwidth.rt_runtime == 0) | |
8979 | return 0; | |
8980 | ||
8981 | return 1; | |
8982 | } | |
8983 | ||
6d6bc0ad | 8984 | #else /* !CONFIG_RT_GROUP_SCHED */ |
d0b27fa7 PZ |
8985 | static int sched_rt_global_constraints(void) |
8986 | { | |
ac086bc2 PZ |
8987 | unsigned long flags; |
8988 | int i; | |
8989 | ||
ec5d4989 HS |
8990 | if (sysctl_sched_rt_period <= 0) |
8991 | return -EINVAL; | |
8992 | ||
60aa605d PZ |
8993 | /* |
8994 | * There's always some RT tasks in the root group | |
8995 | * -- migration, kstopmachine etc.. | |
8996 | */ | |
8997 | if (sysctl_sched_rt_runtime == 0) | |
8998 | return -EBUSY; | |
8999 | ||
0986b11b | 9000 | raw_spin_lock_irqsave(&def_rt_bandwidth.rt_runtime_lock, flags); |
ac086bc2 PZ |
9001 | for_each_possible_cpu(i) { |
9002 | struct rt_rq *rt_rq = &cpu_rq(i)->rt; | |
9003 | ||
0986b11b | 9004 | raw_spin_lock(&rt_rq->rt_runtime_lock); |
ac086bc2 | 9005 | rt_rq->rt_runtime = global_rt_runtime(); |
0986b11b | 9006 | raw_spin_unlock(&rt_rq->rt_runtime_lock); |
ac086bc2 | 9007 | } |
0986b11b | 9008 | raw_spin_unlock_irqrestore(&def_rt_bandwidth.rt_runtime_lock, flags); |
ac086bc2 | 9009 | |
d0b27fa7 PZ |
9010 | return 0; |
9011 | } | |
6d6bc0ad | 9012 | #endif /* CONFIG_RT_GROUP_SCHED */ |
d0b27fa7 PZ |
9013 | |
9014 | int sched_rt_handler(struct ctl_table *table, int write, | |
8d65af78 | 9015 | void __user *buffer, size_t *lenp, |
d0b27fa7 PZ |
9016 | loff_t *ppos) |
9017 | { | |
9018 | int ret; | |
9019 | int old_period, old_runtime; | |
9020 | static DEFINE_MUTEX(mutex); | |
9021 | ||
9022 | mutex_lock(&mutex); | |
9023 | old_period = sysctl_sched_rt_period; | |
9024 | old_runtime = sysctl_sched_rt_runtime; | |
9025 | ||
8d65af78 | 9026 | ret = proc_dointvec(table, write, buffer, lenp, ppos); |
d0b27fa7 PZ |
9027 | |
9028 | if (!ret && write) { | |
9029 | ret = sched_rt_global_constraints(); | |
9030 | if (ret) { | |
9031 | sysctl_sched_rt_period = old_period; | |
9032 | sysctl_sched_rt_runtime = old_runtime; | |
9033 | } else { | |
9034 | def_rt_bandwidth.rt_runtime = global_rt_runtime(); | |
9035 | def_rt_bandwidth.rt_period = | |
9036 | ns_to_ktime(global_rt_period()); | |
9037 | } | |
9038 | } | |
9039 | mutex_unlock(&mutex); | |
9040 | ||
9041 | return ret; | |
9042 | } | |
68318b8e | 9043 | |
052f1dc7 | 9044 | #ifdef CONFIG_CGROUP_SCHED |
68318b8e SV |
9045 | |
9046 | /* return corresponding task_group object of a cgroup */ | |
2b01dfe3 | 9047 | static inline struct task_group *cgroup_tg(struct cgroup *cgrp) |
68318b8e | 9048 | { |
2b01dfe3 PM |
9049 | return container_of(cgroup_subsys_state(cgrp, cpu_cgroup_subsys_id), |
9050 | struct task_group, css); | |
68318b8e SV |
9051 | } |
9052 | ||
9053 | static struct cgroup_subsys_state * | |
2b01dfe3 | 9054 | cpu_cgroup_create(struct cgroup_subsys *ss, struct cgroup *cgrp) |
68318b8e | 9055 | { |
ec7dc8ac | 9056 | struct task_group *tg, *parent; |
68318b8e | 9057 | |
2b01dfe3 | 9058 | if (!cgrp->parent) { |
68318b8e | 9059 | /* This is early initialization for the top cgroup */ |
07e06b01 | 9060 | return &root_task_group.css; |
68318b8e SV |
9061 | } |
9062 | ||
ec7dc8ac DG |
9063 | parent = cgroup_tg(cgrp->parent); |
9064 | tg = sched_create_group(parent); | |
68318b8e SV |
9065 | if (IS_ERR(tg)) |
9066 | return ERR_PTR(-ENOMEM); | |
9067 | ||
68318b8e SV |
9068 | return &tg->css; |
9069 | } | |
9070 | ||
41a2d6cf IM |
9071 | static void |
9072 | cpu_cgroup_destroy(struct cgroup_subsys *ss, struct cgroup *cgrp) | |
68318b8e | 9073 | { |
2b01dfe3 | 9074 | struct task_group *tg = cgroup_tg(cgrp); |
68318b8e SV |
9075 | |
9076 | sched_destroy_group(tg); | |
9077 | } | |
9078 | ||
41a2d6cf | 9079 | static int |
be367d09 | 9080 | cpu_cgroup_can_attach_task(struct cgroup *cgrp, struct task_struct *tsk) |
68318b8e | 9081 | { |
b68aa230 | 9082 | #ifdef CONFIG_RT_GROUP_SCHED |
54e99124 | 9083 | if (!sched_rt_can_attach(cgroup_tg(cgrp), tsk)) |
b68aa230 PZ |
9084 | return -EINVAL; |
9085 | #else | |
68318b8e SV |
9086 | /* We don't support RT-tasks being in separate groups */ |
9087 | if (tsk->sched_class != &fair_sched_class) | |
9088 | return -EINVAL; | |
b68aa230 | 9089 | #endif |
be367d09 BB |
9090 | return 0; |
9091 | } | |
68318b8e | 9092 | |
68318b8e | 9093 | static void |
f780bdb7 | 9094 | cpu_cgroup_attach_task(struct cgroup *cgrp, struct task_struct *tsk) |
68318b8e SV |
9095 | { |
9096 | sched_move_task(tsk); | |
9097 | } | |
9098 | ||
068c5cc5 | 9099 | static void |
d41d5a01 PZ |
9100 | cpu_cgroup_exit(struct cgroup_subsys *ss, struct cgroup *cgrp, |
9101 | struct cgroup *old_cgrp, struct task_struct *task) | |
068c5cc5 PZ |
9102 | { |
9103 | /* | |
9104 | * cgroup_exit() is called in the copy_process() failure path. | |
9105 | * Ignore this case since the task hasn't ran yet, this avoids | |
9106 | * trying to poke a half freed task state from generic code. | |
9107 | */ | |
9108 | if (!(task->flags & PF_EXITING)) | |
9109 | return; | |
9110 | ||
9111 | sched_move_task(task); | |
9112 | } | |
9113 | ||
052f1dc7 | 9114 | #ifdef CONFIG_FAIR_GROUP_SCHED |
f4c753b7 | 9115 | static int cpu_shares_write_u64(struct cgroup *cgrp, struct cftype *cftype, |
2b01dfe3 | 9116 | u64 shareval) |
68318b8e | 9117 | { |
c8b28116 | 9118 | return sched_group_set_shares(cgroup_tg(cgrp), scale_load(shareval)); |
68318b8e SV |
9119 | } |
9120 | ||
f4c753b7 | 9121 | static u64 cpu_shares_read_u64(struct cgroup *cgrp, struct cftype *cft) |
68318b8e | 9122 | { |
2b01dfe3 | 9123 | struct task_group *tg = cgroup_tg(cgrp); |
68318b8e | 9124 | |
c8b28116 | 9125 | return (u64) scale_load_down(tg->shares); |
68318b8e | 9126 | } |
ab84d31e PT |
9127 | |
9128 | #ifdef CONFIG_CFS_BANDWIDTH | |
a790de99 PT |
9129 | static DEFINE_MUTEX(cfs_constraints_mutex); |
9130 | ||
ab84d31e PT |
9131 | const u64 max_cfs_quota_period = 1 * NSEC_PER_SEC; /* 1s */ |
9132 | const u64 min_cfs_quota_period = 1 * NSEC_PER_MSEC; /* 1ms */ | |
9133 | ||
a790de99 PT |
9134 | static int __cfs_schedulable(struct task_group *tg, u64 period, u64 runtime); |
9135 | ||
ab84d31e PT |
9136 | static int tg_set_cfs_bandwidth(struct task_group *tg, u64 period, u64 quota) |
9137 | { | |
58088ad0 | 9138 | int i, ret = 0, runtime_enabled; |
ab84d31e | 9139 | struct cfs_bandwidth *cfs_b = tg_cfs_bandwidth(tg); |
ab84d31e PT |
9140 | |
9141 | if (tg == &root_task_group) | |
9142 | return -EINVAL; | |
9143 | ||
9144 | /* | |
9145 | * Ensure we have at some amount of bandwidth every period. This is | |
9146 | * to prevent reaching a state of large arrears when throttled via | |
9147 | * entity_tick() resulting in prolonged exit starvation. | |
9148 | */ | |
9149 | if (quota < min_cfs_quota_period || period < min_cfs_quota_period) | |
9150 | return -EINVAL; | |
9151 | ||
9152 | /* | |
9153 | * Likewise, bound things on the otherside by preventing insane quota | |
9154 | * periods. This also allows us to normalize in computing quota | |
9155 | * feasibility. | |
9156 | */ | |
9157 | if (period > max_cfs_quota_period) | |
9158 | return -EINVAL; | |
9159 | ||
a790de99 PT |
9160 | mutex_lock(&cfs_constraints_mutex); |
9161 | ret = __cfs_schedulable(tg, period, quota); | |
9162 | if (ret) | |
9163 | goto out_unlock; | |
9164 | ||
58088ad0 | 9165 | runtime_enabled = quota != RUNTIME_INF; |
ab84d31e PT |
9166 | raw_spin_lock_irq(&cfs_b->lock); |
9167 | cfs_b->period = ns_to_ktime(period); | |
9168 | cfs_b->quota = quota; | |
ec12cb7f | 9169 | cfs_b->runtime = quota; |
58088ad0 PT |
9170 | |
9171 | /* restart the period timer (if active) to handle new period expiry */ | |
9172 | if (runtime_enabled && cfs_b->timer_active) { | |
9173 | /* force a reprogram */ | |
9174 | cfs_b->timer_active = 0; | |
9175 | __start_cfs_bandwidth(cfs_b); | |
9176 | } | |
ab84d31e PT |
9177 | raw_spin_unlock_irq(&cfs_b->lock); |
9178 | ||
9179 | for_each_possible_cpu(i) { | |
9180 | struct cfs_rq *cfs_rq = tg->cfs_rq[i]; | |
9181 | struct rq *rq = rq_of(cfs_rq); | |
9182 | ||
9183 | raw_spin_lock_irq(&rq->lock); | |
58088ad0 | 9184 | cfs_rq->runtime_enabled = runtime_enabled; |
ab84d31e PT |
9185 | cfs_rq->runtime_remaining = 0; |
9186 | raw_spin_unlock_irq(&rq->lock); | |
9187 | } | |
a790de99 PT |
9188 | out_unlock: |
9189 | mutex_unlock(&cfs_constraints_mutex); | |
ab84d31e | 9190 | |
a790de99 | 9191 | return ret; |
ab84d31e PT |
9192 | } |
9193 | ||
9194 | int tg_set_cfs_quota(struct task_group *tg, long cfs_quota_us) | |
9195 | { | |
9196 | u64 quota, period; | |
9197 | ||
9198 | period = ktime_to_ns(tg_cfs_bandwidth(tg)->period); | |
9199 | if (cfs_quota_us < 0) | |
9200 | quota = RUNTIME_INF; | |
9201 | else | |
9202 | quota = (u64)cfs_quota_us * NSEC_PER_USEC; | |
9203 | ||
9204 | return tg_set_cfs_bandwidth(tg, period, quota); | |
9205 | } | |
9206 | ||
9207 | long tg_get_cfs_quota(struct task_group *tg) | |
9208 | { | |
9209 | u64 quota_us; | |
9210 | ||
9211 | if (tg_cfs_bandwidth(tg)->quota == RUNTIME_INF) | |
9212 | return -1; | |
9213 | ||
9214 | quota_us = tg_cfs_bandwidth(tg)->quota; | |
9215 | do_div(quota_us, NSEC_PER_USEC); | |
9216 | ||
9217 | return quota_us; | |
9218 | } | |
9219 | ||
9220 | int tg_set_cfs_period(struct task_group *tg, long cfs_period_us) | |
9221 | { | |
9222 | u64 quota, period; | |
9223 | ||
9224 | period = (u64)cfs_period_us * NSEC_PER_USEC; | |
9225 | quota = tg_cfs_bandwidth(tg)->quota; | |
9226 | ||
9227 | if (period <= 0) | |
9228 | return -EINVAL; | |
9229 | ||
9230 | return tg_set_cfs_bandwidth(tg, period, quota); | |
9231 | } | |
9232 | ||
9233 | long tg_get_cfs_period(struct task_group *tg) | |
9234 | { | |
9235 | u64 cfs_period_us; | |
9236 | ||
9237 | cfs_period_us = ktime_to_ns(tg_cfs_bandwidth(tg)->period); | |
9238 | do_div(cfs_period_us, NSEC_PER_USEC); | |
9239 | ||
9240 | return cfs_period_us; | |
9241 | } | |
9242 | ||
9243 | static s64 cpu_cfs_quota_read_s64(struct cgroup *cgrp, struct cftype *cft) | |
9244 | { | |
9245 | return tg_get_cfs_quota(cgroup_tg(cgrp)); | |
9246 | } | |
9247 | ||
9248 | static int cpu_cfs_quota_write_s64(struct cgroup *cgrp, struct cftype *cftype, | |
9249 | s64 cfs_quota_us) | |
9250 | { | |
9251 | return tg_set_cfs_quota(cgroup_tg(cgrp), cfs_quota_us); | |
9252 | } | |
9253 | ||
9254 | static u64 cpu_cfs_period_read_u64(struct cgroup *cgrp, struct cftype *cft) | |
9255 | { | |
9256 | return tg_get_cfs_period(cgroup_tg(cgrp)); | |
9257 | } | |
9258 | ||
9259 | static int cpu_cfs_period_write_u64(struct cgroup *cgrp, struct cftype *cftype, | |
9260 | u64 cfs_period_us) | |
9261 | { | |
9262 | return tg_set_cfs_period(cgroup_tg(cgrp), cfs_period_us); | |
9263 | } | |
9264 | ||
a790de99 PT |
9265 | struct cfs_schedulable_data { |
9266 | struct task_group *tg; | |
9267 | u64 period, quota; | |
9268 | }; | |
9269 | ||
9270 | /* | |
9271 | * normalize group quota/period to be quota/max_period | |
9272 | * note: units are usecs | |
9273 | */ | |
9274 | static u64 normalize_cfs_quota(struct task_group *tg, | |
9275 | struct cfs_schedulable_data *d) | |
9276 | { | |
9277 | u64 quota, period; | |
9278 | ||
9279 | if (tg == d->tg) { | |
9280 | period = d->period; | |
9281 | quota = d->quota; | |
9282 | } else { | |
9283 | period = tg_get_cfs_period(tg); | |
9284 | quota = tg_get_cfs_quota(tg); | |
9285 | } | |
9286 | ||
9287 | /* note: these should typically be equivalent */ | |
9288 | if (quota == RUNTIME_INF || quota == -1) | |
9289 | return RUNTIME_INF; | |
9290 | ||
9291 | return to_ratio(period, quota); | |
9292 | } | |
9293 | ||
9294 | static int tg_cfs_schedulable_down(struct task_group *tg, void *data) | |
9295 | { | |
9296 | struct cfs_schedulable_data *d = data; | |
9297 | struct cfs_bandwidth *cfs_b = tg_cfs_bandwidth(tg); | |
9298 | s64 quota = 0, parent_quota = -1; | |
9299 | ||
9300 | if (!tg->parent) { | |
9301 | quota = RUNTIME_INF; | |
9302 | } else { | |
9303 | struct cfs_bandwidth *parent_b = tg_cfs_bandwidth(tg->parent); | |
9304 | ||
9305 | quota = normalize_cfs_quota(tg, d); | |
9306 | parent_quota = parent_b->hierarchal_quota; | |
9307 | ||
9308 | /* | |
9309 | * ensure max(child_quota) <= parent_quota, inherit when no | |
9310 | * limit is set | |
9311 | */ | |
9312 | if (quota == RUNTIME_INF) | |
9313 | quota = parent_quota; | |
9314 | else if (parent_quota != RUNTIME_INF && quota > parent_quota) | |
9315 | return -EINVAL; | |
9316 | } | |
9317 | cfs_b->hierarchal_quota = quota; | |
9318 | ||
9319 | return 0; | |
9320 | } | |
9321 | ||
9322 | static int __cfs_schedulable(struct task_group *tg, u64 period, u64 quota) | |
9323 | { | |
9324 | struct cfs_schedulable_data data = { | |
9325 | .tg = tg, | |
9326 | .period = period, | |
9327 | .quota = quota, | |
9328 | }; | |
9329 | ||
9330 | if (quota != RUNTIME_INF) { | |
9331 | do_div(data.period, NSEC_PER_USEC); | |
9332 | do_div(data.quota, NSEC_PER_USEC); | |
9333 | } | |
9334 | ||
9335 | return walk_tg_tree(tg_cfs_schedulable_down, tg_nop, &data); | |
9336 | } | |
ab84d31e | 9337 | #endif /* CONFIG_CFS_BANDWIDTH */ |
6d6bc0ad | 9338 | #endif /* CONFIG_FAIR_GROUP_SCHED */ |
68318b8e | 9339 | |
052f1dc7 | 9340 | #ifdef CONFIG_RT_GROUP_SCHED |
0c70814c | 9341 | static int cpu_rt_runtime_write(struct cgroup *cgrp, struct cftype *cft, |
06ecb27c | 9342 | s64 val) |
6f505b16 | 9343 | { |
06ecb27c | 9344 | return sched_group_set_rt_runtime(cgroup_tg(cgrp), val); |
6f505b16 PZ |
9345 | } |
9346 | ||
06ecb27c | 9347 | static s64 cpu_rt_runtime_read(struct cgroup *cgrp, struct cftype *cft) |
6f505b16 | 9348 | { |
06ecb27c | 9349 | return sched_group_rt_runtime(cgroup_tg(cgrp)); |
6f505b16 | 9350 | } |
d0b27fa7 PZ |
9351 | |
9352 | static int cpu_rt_period_write_uint(struct cgroup *cgrp, struct cftype *cftype, | |
9353 | u64 rt_period_us) | |
9354 | { | |
9355 | return sched_group_set_rt_period(cgroup_tg(cgrp), rt_period_us); | |
9356 | } | |
9357 | ||
9358 | static u64 cpu_rt_period_read_uint(struct cgroup *cgrp, struct cftype *cft) | |
9359 | { | |
9360 | return sched_group_rt_period(cgroup_tg(cgrp)); | |
9361 | } | |
6d6bc0ad | 9362 | #endif /* CONFIG_RT_GROUP_SCHED */ |
6f505b16 | 9363 | |
fe5c7cc2 | 9364 | static struct cftype cpu_files[] = { |
052f1dc7 | 9365 | #ifdef CONFIG_FAIR_GROUP_SCHED |
fe5c7cc2 PM |
9366 | { |
9367 | .name = "shares", | |
f4c753b7 PM |
9368 | .read_u64 = cpu_shares_read_u64, |
9369 | .write_u64 = cpu_shares_write_u64, | |
fe5c7cc2 | 9370 | }, |
052f1dc7 | 9371 | #endif |
ab84d31e PT |
9372 | #ifdef CONFIG_CFS_BANDWIDTH |
9373 | { | |
9374 | .name = "cfs_quota_us", | |
9375 | .read_s64 = cpu_cfs_quota_read_s64, | |
9376 | .write_s64 = cpu_cfs_quota_write_s64, | |
9377 | }, | |
9378 | { | |
9379 | .name = "cfs_period_us", | |
9380 | .read_u64 = cpu_cfs_period_read_u64, | |
9381 | .write_u64 = cpu_cfs_period_write_u64, | |
9382 | }, | |
9383 | #endif | |
052f1dc7 | 9384 | #ifdef CONFIG_RT_GROUP_SCHED |
6f505b16 | 9385 | { |
9f0c1e56 | 9386 | .name = "rt_runtime_us", |
06ecb27c PM |
9387 | .read_s64 = cpu_rt_runtime_read, |
9388 | .write_s64 = cpu_rt_runtime_write, | |
6f505b16 | 9389 | }, |
d0b27fa7 PZ |
9390 | { |
9391 | .name = "rt_period_us", | |
f4c753b7 PM |
9392 | .read_u64 = cpu_rt_period_read_uint, |
9393 | .write_u64 = cpu_rt_period_write_uint, | |
d0b27fa7 | 9394 | }, |
052f1dc7 | 9395 | #endif |
68318b8e SV |
9396 | }; |
9397 | ||
9398 | static int cpu_cgroup_populate(struct cgroup_subsys *ss, struct cgroup *cont) | |
9399 | { | |
fe5c7cc2 | 9400 | return cgroup_add_files(cont, ss, cpu_files, ARRAY_SIZE(cpu_files)); |
68318b8e SV |
9401 | } |
9402 | ||
9403 | struct cgroup_subsys cpu_cgroup_subsys = { | |
38605cae IM |
9404 | .name = "cpu", |
9405 | .create = cpu_cgroup_create, | |
9406 | .destroy = cpu_cgroup_destroy, | |
f780bdb7 BB |
9407 | .can_attach_task = cpu_cgroup_can_attach_task, |
9408 | .attach_task = cpu_cgroup_attach_task, | |
068c5cc5 | 9409 | .exit = cpu_cgroup_exit, |
38605cae IM |
9410 | .populate = cpu_cgroup_populate, |
9411 | .subsys_id = cpu_cgroup_subsys_id, | |
68318b8e SV |
9412 | .early_init = 1, |
9413 | }; | |
9414 | ||
052f1dc7 | 9415 | #endif /* CONFIG_CGROUP_SCHED */ |
d842de87 SV |
9416 | |
9417 | #ifdef CONFIG_CGROUP_CPUACCT | |
9418 | ||
9419 | /* | |
9420 | * CPU accounting code for task groups. | |
9421 | * | |
9422 | * Based on the work by Paul Menage (menage@google.com) and Balbir Singh | |
9423 | * (balbir@in.ibm.com). | |
9424 | */ | |
9425 | ||
934352f2 | 9426 | /* track cpu usage of a group of tasks and its child groups */ |
d842de87 SV |
9427 | struct cpuacct { |
9428 | struct cgroup_subsys_state css; | |
9429 | /* cpuusage holds pointer to a u64-type object on every cpu */ | |
43cf38eb | 9430 | u64 __percpu *cpuusage; |
ef12fefa | 9431 | struct percpu_counter cpustat[CPUACCT_STAT_NSTATS]; |
934352f2 | 9432 | struct cpuacct *parent; |
d842de87 SV |
9433 | }; |
9434 | ||
9435 | struct cgroup_subsys cpuacct_subsys; | |
9436 | ||
9437 | /* return cpu accounting group corresponding to this container */ | |
32cd756a | 9438 | static inline struct cpuacct *cgroup_ca(struct cgroup *cgrp) |
d842de87 | 9439 | { |
32cd756a | 9440 | return container_of(cgroup_subsys_state(cgrp, cpuacct_subsys_id), |
d842de87 SV |
9441 | struct cpuacct, css); |
9442 | } | |
9443 | ||
9444 | /* return cpu accounting group to which this task belongs */ | |
9445 | static inline struct cpuacct *task_ca(struct task_struct *tsk) | |
9446 | { | |
9447 | return container_of(task_subsys_state(tsk, cpuacct_subsys_id), | |
9448 | struct cpuacct, css); | |
9449 | } | |
9450 | ||
9451 | /* create a new cpu accounting group */ | |
9452 | static struct cgroup_subsys_state *cpuacct_create( | |
32cd756a | 9453 | struct cgroup_subsys *ss, struct cgroup *cgrp) |
d842de87 SV |
9454 | { |
9455 | struct cpuacct *ca = kzalloc(sizeof(*ca), GFP_KERNEL); | |
ef12fefa | 9456 | int i; |
d842de87 SV |
9457 | |
9458 | if (!ca) | |
ef12fefa | 9459 | goto out; |
d842de87 SV |
9460 | |
9461 | ca->cpuusage = alloc_percpu(u64); | |
ef12fefa BR |
9462 | if (!ca->cpuusage) |
9463 | goto out_free_ca; | |
9464 | ||
9465 | for (i = 0; i < CPUACCT_STAT_NSTATS; i++) | |
9466 | if (percpu_counter_init(&ca->cpustat[i], 0)) | |
9467 | goto out_free_counters; | |
d842de87 | 9468 | |
934352f2 BR |
9469 | if (cgrp->parent) |
9470 | ca->parent = cgroup_ca(cgrp->parent); | |
9471 | ||
d842de87 | 9472 | return &ca->css; |
ef12fefa BR |
9473 | |
9474 | out_free_counters: | |
9475 | while (--i >= 0) | |
9476 | percpu_counter_destroy(&ca->cpustat[i]); | |
9477 | free_percpu(ca->cpuusage); | |
9478 | out_free_ca: | |
9479 | kfree(ca); | |
9480 | out: | |
9481 | return ERR_PTR(-ENOMEM); | |
d842de87 SV |
9482 | } |
9483 | ||
9484 | /* destroy an existing cpu accounting group */ | |
41a2d6cf | 9485 | static void |
32cd756a | 9486 | cpuacct_destroy(struct cgroup_subsys *ss, struct cgroup *cgrp) |
d842de87 | 9487 | { |
32cd756a | 9488 | struct cpuacct *ca = cgroup_ca(cgrp); |
ef12fefa | 9489 | int i; |
d842de87 | 9490 | |
ef12fefa BR |
9491 | for (i = 0; i < CPUACCT_STAT_NSTATS; i++) |
9492 | percpu_counter_destroy(&ca->cpustat[i]); | |
d842de87 SV |
9493 | free_percpu(ca->cpuusage); |
9494 | kfree(ca); | |
9495 | } | |
9496 | ||
720f5498 KC |
9497 | static u64 cpuacct_cpuusage_read(struct cpuacct *ca, int cpu) |
9498 | { | |
b36128c8 | 9499 | u64 *cpuusage = per_cpu_ptr(ca->cpuusage, cpu); |
720f5498 KC |
9500 | u64 data; |
9501 | ||
9502 | #ifndef CONFIG_64BIT | |
9503 | /* | |
9504 | * Take rq->lock to make 64-bit read safe on 32-bit platforms. | |
9505 | */ | |
05fa785c | 9506 | raw_spin_lock_irq(&cpu_rq(cpu)->lock); |
720f5498 | 9507 | data = *cpuusage; |
05fa785c | 9508 | raw_spin_unlock_irq(&cpu_rq(cpu)->lock); |
720f5498 KC |
9509 | #else |
9510 | data = *cpuusage; | |
9511 | #endif | |
9512 | ||
9513 | return data; | |
9514 | } | |
9515 | ||
9516 | static void cpuacct_cpuusage_write(struct cpuacct *ca, int cpu, u64 val) | |
9517 | { | |
b36128c8 | 9518 | u64 *cpuusage = per_cpu_ptr(ca->cpuusage, cpu); |
720f5498 KC |
9519 | |
9520 | #ifndef CONFIG_64BIT | |
9521 | /* | |
9522 | * Take rq->lock to make 64-bit write safe on 32-bit platforms. | |
9523 | */ | |
05fa785c | 9524 | raw_spin_lock_irq(&cpu_rq(cpu)->lock); |
720f5498 | 9525 | *cpuusage = val; |
05fa785c | 9526 | raw_spin_unlock_irq(&cpu_rq(cpu)->lock); |
720f5498 KC |
9527 | #else |
9528 | *cpuusage = val; | |
9529 | #endif | |
9530 | } | |
9531 | ||
d842de87 | 9532 | /* return total cpu usage (in nanoseconds) of a group */ |
32cd756a | 9533 | static u64 cpuusage_read(struct cgroup *cgrp, struct cftype *cft) |
d842de87 | 9534 | { |
32cd756a | 9535 | struct cpuacct *ca = cgroup_ca(cgrp); |
d842de87 SV |
9536 | u64 totalcpuusage = 0; |
9537 | int i; | |
9538 | ||
720f5498 KC |
9539 | for_each_present_cpu(i) |
9540 | totalcpuusage += cpuacct_cpuusage_read(ca, i); | |
d842de87 SV |
9541 | |
9542 | return totalcpuusage; | |
9543 | } | |
9544 | ||
0297b803 DG |
9545 | static int cpuusage_write(struct cgroup *cgrp, struct cftype *cftype, |
9546 | u64 reset) | |
9547 | { | |
9548 | struct cpuacct *ca = cgroup_ca(cgrp); | |
9549 | int err = 0; | |
9550 | int i; | |
9551 | ||
9552 | if (reset) { | |
9553 | err = -EINVAL; | |
9554 | goto out; | |
9555 | } | |
9556 | ||
720f5498 KC |
9557 | for_each_present_cpu(i) |
9558 | cpuacct_cpuusage_write(ca, i, 0); | |
0297b803 | 9559 | |
0297b803 DG |
9560 | out: |
9561 | return err; | |
9562 | } | |
9563 | ||
e9515c3c KC |
9564 | static int cpuacct_percpu_seq_read(struct cgroup *cgroup, struct cftype *cft, |
9565 | struct seq_file *m) | |
9566 | { | |
9567 | struct cpuacct *ca = cgroup_ca(cgroup); | |
9568 | u64 percpu; | |
9569 | int i; | |
9570 | ||
9571 | for_each_present_cpu(i) { | |
9572 | percpu = cpuacct_cpuusage_read(ca, i); | |
9573 | seq_printf(m, "%llu ", (unsigned long long) percpu); | |
9574 | } | |
9575 | seq_printf(m, "\n"); | |
9576 | return 0; | |
9577 | } | |
9578 | ||
ef12fefa BR |
9579 | static const char *cpuacct_stat_desc[] = { |
9580 | [CPUACCT_STAT_USER] = "user", | |
9581 | [CPUACCT_STAT_SYSTEM] = "system", | |
9582 | }; | |
9583 | ||
9584 | static int cpuacct_stats_show(struct cgroup *cgrp, struct cftype *cft, | |
9585 | struct cgroup_map_cb *cb) | |
9586 | { | |
9587 | struct cpuacct *ca = cgroup_ca(cgrp); | |
9588 | int i; | |
9589 | ||
9590 | for (i = 0; i < CPUACCT_STAT_NSTATS; i++) { | |
9591 | s64 val = percpu_counter_read(&ca->cpustat[i]); | |
9592 | val = cputime64_to_clock_t(val); | |
9593 | cb->fill(cb, cpuacct_stat_desc[i], val); | |
9594 | } | |
9595 | return 0; | |
9596 | } | |
9597 | ||
d842de87 SV |
9598 | static struct cftype files[] = { |
9599 | { | |
9600 | .name = "usage", | |
f4c753b7 PM |
9601 | .read_u64 = cpuusage_read, |
9602 | .write_u64 = cpuusage_write, | |
d842de87 | 9603 | }, |
e9515c3c KC |
9604 | { |
9605 | .name = "usage_percpu", | |
9606 | .read_seq_string = cpuacct_percpu_seq_read, | |
9607 | }, | |
ef12fefa BR |
9608 | { |
9609 | .name = "stat", | |
9610 | .read_map = cpuacct_stats_show, | |
9611 | }, | |
d842de87 SV |
9612 | }; |
9613 | ||
32cd756a | 9614 | static int cpuacct_populate(struct cgroup_subsys *ss, struct cgroup *cgrp) |
d842de87 | 9615 | { |
32cd756a | 9616 | return cgroup_add_files(cgrp, ss, files, ARRAY_SIZE(files)); |
d842de87 SV |
9617 | } |
9618 | ||
9619 | /* | |
9620 | * charge this task's execution time to its accounting group. | |
9621 | * | |
9622 | * called with rq->lock held. | |
9623 | */ | |
9624 | static void cpuacct_charge(struct task_struct *tsk, u64 cputime) | |
9625 | { | |
9626 | struct cpuacct *ca; | |
934352f2 | 9627 | int cpu; |
d842de87 | 9628 | |
c40c6f85 | 9629 | if (unlikely(!cpuacct_subsys.active)) |
d842de87 SV |
9630 | return; |
9631 | ||
934352f2 | 9632 | cpu = task_cpu(tsk); |
a18b83b7 BR |
9633 | |
9634 | rcu_read_lock(); | |
9635 | ||
d842de87 | 9636 | ca = task_ca(tsk); |
d842de87 | 9637 | |
934352f2 | 9638 | for (; ca; ca = ca->parent) { |
b36128c8 | 9639 | u64 *cpuusage = per_cpu_ptr(ca->cpuusage, cpu); |
d842de87 SV |
9640 | *cpuusage += cputime; |
9641 | } | |
a18b83b7 BR |
9642 | |
9643 | rcu_read_unlock(); | |
d842de87 SV |
9644 | } |
9645 | ||
fa535a77 AB |
9646 | /* |
9647 | * When CONFIG_VIRT_CPU_ACCOUNTING is enabled one jiffy can be very large | |
9648 | * in cputime_t units. As a result, cpuacct_update_stats calls | |
9649 | * percpu_counter_add with values large enough to always overflow the | |
9650 | * per cpu batch limit causing bad SMP scalability. | |
9651 | * | |
9652 | * To fix this we scale percpu_counter_batch by cputime_one_jiffy so we | |
9653 | * batch the same amount of time with CONFIG_VIRT_CPU_ACCOUNTING disabled | |
9654 | * and enabled. We cap it at INT_MAX which is the largest allowed batch value. | |
9655 | */ | |
9656 | #ifdef CONFIG_SMP | |
9657 | #define CPUACCT_BATCH \ | |
9658 | min_t(long, percpu_counter_batch * cputime_one_jiffy, INT_MAX) | |
9659 | #else | |
9660 | #define CPUACCT_BATCH 0 | |
9661 | #endif | |
9662 | ||
ef12fefa BR |
9663 | /* |
9664 | * Charge the system/user time to the task's accounting group. | |
9665 | */ | |
9666 | static void cpuacct_update_stats(struct task_struct *tsk, | |
9667 | enum cpuacct_stat_index idx, cputime_t val) | |
9668 | { | |
9669 | struct cpuacct *ca; | |
fa535a77 | 9670 | int batch = CPUACCT_BATCH; |
ef12fefa BR |
9671 | |
9672 | if (unlikely(!cpuacct_subsys.active)) | |
9673 | return; | |
9674 | ||
9675 | rcu_read_lock(); | |
9676 | ca = task_ca(tsk); | |
9677 | ||
9678 | do { | |
fa535a77 | 9679 | __percpu_counter_add(&ca->cpustat[idx], val, batch); |
ef12fefa BR |
9680 | ca = ca->parent; |
9681 | } while (ca); | |
9682 | rcu_read_unlock(); | |
9683 | } | |
9684 | ||
d842de87 SV |
9685 | struct cgroup_subsys cpuacct_subsys = { |
9686 | .name = "cpuacct", | |
9687 | .create = cpuacct_create, | |
9688 | .destroy = cpuacct_destroy, | |
9689 | .populate = cpuacct_populate, | |
9690 | .subsys_id = cpuacct_subsys_id, | |
9691 | }; | |
9692 | #endif /* CONFIG_CGROUP_CPUACCT */ |