4 * Kernel scheduler and related syscalls
6 * Copyright (C) 1991-2002 Linus Torvalds
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
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
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
23 * 2007-05-06 Interactivity improvements to CFS by Mike Galbraith
24 * 2007-07-01 Group scheduling enhancements by Srivatsa Vaddagiri
25 * 2007-11-29 RT balancing improvements by Steven Rostedt, Gregory Haskins,
26 * Thomas Gleixner, Mike Kravetz
30 #include <linux/module.h>
31 #include <linux/nmi.h>
32 #include <linux/init.h>
33 #include <linux/uaccess.h>
34 #include <linux/highmem.h>
35 #include <linux/smp_lock.h>
36 #include <asm/mmu_context.h>
37 #include <linux/interrupt.h>
38 #include <linux/capability.h>
39 #include <linux/completion.h>
40 #include <linux/kernel_stat.h>
41 #include <linux/debug_locks.h>
42 #include <linux/security.h>
43 #include <linux/notifier.h>
44 #include <linux/profile.h>
45 #include <linux/freezer.h>
46 #include <linux/vmalloc.h>
47 #include <linux/blkdev.h>
48 #include <linux/delay.h>
49 #include <linux/pid_namespace.h>
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>
57 #include <linux/kthread.h>
58 #include <linux/seq_file.h>
59 #include <linux/sysctl.h>
60 #include <linux/syscalls.h>
61 #include <linux/times.h>
62 #include <linux/tsacct_kern.h>
63 #include <linux/kprobes.h>
64 #include <linux/delayacct.h>
65 #include <linux/reciprocal_div.h>
66 #include <linux/unistd.h>
67 #include <linux/pagemap.h>
68 #include <linux/hrtimer.h>
69 #include <linux/tick.h>
70 #include <linux/bootmem.h>
71 #include <linux/debugfs.h>
72 #include <linux/ctype.h>
73 #include <linux/ftrace.h>
76 #include <asm/irq_regs.h>
79 * Convert user-nice values [ -20 ... 0 ... 19 ]
80 * to static priority [ MAX_RT_PRIO..MAX_PRIO-1 ],
83 #define NICE_TO_PRIO(nice) (MAX_RT_PRIO + (nice) + 20)
84 #define PRIO_TO_NICE(prio) ((prio) - MAX_RT_PRIO - 20)
85 #define TASK_NICE(p) PRIO_TO_NICE((p)->static_prio)
88 * 'User priority' is the nice value converted to something we
89 * can work with better when scaling various scheduler parameters,
90 * it's a [ 0 ... 39 ] range.
92 #define USER_PRIO(p) ((p)-MAX_RT_PRIO)
93 #define TASK_USER_PRIO(p) USER_PRIO((p)->static_prio)
94 #define MAX_USER_PRIO (USER_PRIO(MAX_PRIO))
97 * Helpers for converting nanosecond timing to jiffy resolution
99 #define NS_TO_JIFFIES(TIME) ((unsigned long)(TIME) / (NSEC_PER_SEC / HZ))
101 #define NICE_0_LOAD SCHED_LOAD_SCALE
102 #define NICE_0_SHIFT SCHED_LOAD_SHIFT
105 * These are the 'tuning knobs' of the scheduler:
107 * default timeslice is 100 msecs (used only for SCHED_RR tasks).
108 * Timeslices get refilled after they expire.
110 #define DEF_TIMESLICE (100 * HZ / 1000)
113 * single value that denotes runtime == period, ie unlimited time.
115 #define RUNTIME_INF ((u64)~0ULL)
119 * Divide a load by a sched group cpu_power : (load / sg->__cpu_power)
120 * Since cpu_power is a 'constant', we can use a reciprocal divide.
122 static inline u32
sg_div_cpu_power(const struct sched_group
*sg
, u32 load
)
124 return reciprocal_divide(load
, sg
->reciprocal_cpu_power
);
128 * Each time a sched group cpu_power is changed,
129 * we must compute its reciprocal value
131 static inline void sg_inc_cpu_power(struct sched_group
*sg
, u32 val
)
133 sg
->__cpu_power
+= val
;
134 sg
->reciprocal_cpu_power
= reciprocal_value(sg
->__cpu_power
);
138 static inline int rt_policy(int policy
)
140 if (unlikely(policy
== SCHED_FIFO
) || unlikely(policy
== SCHED_RR
))
145 static inline int task_has_rt_policy(struct task_struct
*p
)
147 return rt_policy(p
->policy
);
151 * This is the priority-queue data structure of the RT scheduling class:
153 struct rt_prio_array
{
154 DECLARE_BITMAP(bitmap
, MAX_RT_PRIO
+1); /* include 1 bit for delimiter */
155 struct list_head queue
[MAX_RT_PRIO
];
158 struct rt_bandwidth
{
159 /* nests inside the rq lock: */
160 spinlock_t rt_runtime_lock
;
163 struct hrtimer rt_period_timer
;
166 static struct rt_bandwidth def_rt_bandwidth
;
168 static int do_sched_rt_period_timer(struct rt_bandwidth
*rt_b
, int overrun
);
170 static enum hrtimer_restart
sched_rt_period_timer(struct hrtimer
*timer
)
172 struct rt_bandwidth
*rt_b
=
173 container_of(timer
, struct rt_bandwidth
, rt_period_timer
);
179 now
= hrtimer_cb_get_time(timer
);
180 overrun
= hrtimer_forward(timer
, now
, rt_b
->rt_period
);
185 idle
= do_sched_rt_period_timer(rt_b
, overrun
);
188 return idle
? HRTIMER_NORESTART
: HRTIMER_RESTART
;
192 void init_rt_bandwidth(struct rt_bandwidth
*rt_b
, u64 period
, u64 runtime
)
194 rt_b
->rt_period
= ns_to_ktime(period
);
195 rt_b
->rt_runtime
= runtime
;
197 spin_lock_init(&rt_b
->rt_runtime_lock
);
199 hrtimer_init(&rt_b
->rt_period_timer
,
200 CLOCK_MONOTONIC
, HRTIMER_MODE_REL
);
201 rt_b
->rt_period_timer
.function
= sched_rt_period_timer
;
202 rt_b
->rt_period_timer
.cb_mode
= HRTIMER_CB_IRQSAFE_NO_SOFTIRQ
;
205 static void start_rt_bandwidth(struct rt_bandwidth
*rt_b
)
209 if (rt_b
->rt_runtime
== RUNTIME_INF
)
212 if (hrtimer_active(&rt_b
->rt_period_timer
))
215 spin_lock(&rt_b
->rt_runtime_lock
);
217 if (hrtimer_active(&rt_b
->rt_period_timer
))
220 now
= hrtimer_cb_get_time(&rt_b
->rt_period_timer
);
221 hrtimer_forward(&rt_b
->rt_period_timer
, now
, rt_b
->rt_period
);
222 hrtimer_start(&rt_b
->rt_period_timer
,
223 rt_b
->rt_period_timer
.expires
,
226 spin_unlock(&rt_b
->rt_runtime_lock
);
229 #ifdef CONFIG_RT_GROUP_SCHED
230 static void destroy_rt_bandwidth(struct rt_bandwidth
*rt_b
)
232 hrtimer_cancel(&rt_b
->rt_period_timer
);
237 * sched_domains_mutex serializes calls to arch_init_sched_domains,
238 * detach_destroy_domains and partition_sched_domains.
240 static DEFINE_MUTEX(sched_domains_mutex
);
242 #ifdef CONFIG_GROUP_SCHED
244 #include <linux/cgroup.h>
248 static LIST_HEAD(task_groups
);
250 /* task group related information */
252 #ifdef CONFIG_CGROUP_SCHED
253 struct cgroup_subsys_state css
;
256 #ifdef CONFIG_FAIR_GROUP_SCHED
257 /* schedulable entities of this group on each cpu */
258 struct sched_entity
**se
;
259 /* runqueue "owned" by this group on each cpu */
260 struct cfs_rq
**cfs_rq
;
261 unsigned long shares
;
264 #ifdef CONFIG_RT_GROUP_SCHED
265 struct sched_rt_entity
**rt_se
;
266 struct rt_rq
**rt_rq
;
268 struct rt_bandwidth rt_bandwidth
;
272 struct list_head list
;
274 struct task_group
*parent
;
275 struct list_head siblings
;
276 struct list_head children
;
279 #ifdef CONFIG_USER_SCHED
283 * Every UID task group (including init_task_group aka UID-0) will
284 * be a child to this group.
286 struct task_group root_task_group
;
288 #ifdef CONFIG_FAIR_GROUP_SCHED
289 /* Default task group's sched entity on each cpu */
290 static DEFINE_PER_CPU(struct sched_entity
, init_sched_entity
);
291 /* Default task group's cfs_rq on each cpu */
292 static DEFINE_PER_CPU(struct cfs_rq
, init_cfs_rq
) ____cacheline_aligned_in_smp
;
295 #ifdef CONFIG_RT_GROUP_SCHED
296 static DEFINE_PER_CPU(struct sched_rt_entity
, init_sched_rt_entity
);
297 static DEFINE_PER_CPU(struct rt_rq
, init_rt_rq
) ____cacheline_aligned_in_smp
;
300 #define root_task_group init_task_group
303 /* task_group_lock serializes add/remove of task groups and also changes to
304 * a task group's cpu shares.
306 static DEFINE_SPINLOCK(task_group_lock
);
308 #ifdef CONFIG_FAIR_GROUP_SCHED
309 #ifdef CONFIG_USER_SCHED
310 # define INIT_TASK_GROUP_LOAD (2*NICE_0_LOAD)
312 # define INIT_TASK_GROUP_LOAD NICE_0_LOAD
316 * A weight of 0, 1 or ULONG_MAX can cause arithmetics problems.
317 * (The default weight is 1024 - so there's no practical
318 * limitation from this.)
321 #define MAX_SHARES (ULONG_MAX - 1)
323 static int init_task_group_load
= INIT_TASK_GROUP_LOAD
;
326 /* Default task group.
327 * Every task in system belong to this group at bootup.
329 struct task_group init_task_group
;
331 /* return group to which a task belongs */
332 static inline struct task_group
*task_group(struct task_struct
*p
)
334 struct task_group
*tg
;
336 #ifdef CONFIG_USER_SCHED
338 #elif defined(CONFIG_CGROUP_SCHED)
339 tg
= container_of(task_subsys_state(p
, cpu_cgroup_subsys_id
),
340 struct task_group
, css
);
342 tg
= &init_task_group
;
347 /* Change a task's cfs_rq and parent entity if it moves across CPUs/groups */
348 static inline void set_task_rq(struct task_struct
*p
, unsigned int cpu
)
350 #ifdef CONFIG_FAIR_GROUP_SCHED
351 p
->se
.cfs_rq
= task_group(p
)->cfs_rq
[cpu
];
352 p
->se
.parent
= task_group(p
)->se
[cpu
];
355 #ifdef CONFIG_RT_GROUP_SCHED
356 p
->rt
.rt_rq
= task_group(p
)->rt_rq
[cpu
];
357 p
->rt
.parent
= task_group(p
)->rt_se
[cpu
];
363 static inline void set_task_rq(struct task_struct
*p
, unsigned int cpu
) { }
365 #endif /* CONFIG_GROUP_SCHED */
367 /* CFS-related fields in a runqueue */
369 struct load_weight load
;
370 unsigned long nr_running
;
375 struct rb_root tasks_timeline
;
376 struct rb_node
*rb_leftmost
;
378 struct list_head tasks
;
379 struct list_head
*balance_iterator
;
382 * 'curr' points to currently running entity on this cfs_rq.
383 * It is set to NULL otherwise (i.e when none are currently running).
385 struct sched_entity
*curr
, *next
;
387 unsigned long nr_spread_over
;
389 #ifdef CONFIG_FAIR_GROUP_SCHED
390 struct rq
*rq
; /* cpu runqueue to which this cfs_rq is attached */
393 * leaf cfs_rqs are those that hold tasks (lowest schedulable entity in
394 * a hierarchy). Non-leaf lrqs hold other higher schedulable entities
395 * (like users, containers etc.)
397 * leaf_cfs_rq_list ties together list of leaf cfs_rq's in a cpu. This
398 * list is used during load balance.
400 struct list_head leaf_cfs_rq_list
;
401 struct task_group
*tg
; /* group that "owns" this runqueue */
404 unsigned long task_weight
;
405 unsigned long shares
;
407 * We need space to build a sched_domain wide view of the full task
408 * group tree, in order to avoid depending on dynamic memory allocation
409 * during the load balancing we place this in the per cpu task group
410 * hierarchy. This limits the load balancing to one instance per cpu,
411 * but more should not be needed anyway.
413 struct aggregate_struct
{
415 * load = weight(cpus) * f(tg)
417 * Where f(tg) is the recursive weight fraction assigned to
423 * part of the group weight distributed to this span.
425 unsigned long shares
;
428 * The sum of all runqueue weights within this span.
430 unsigned long rq_weight
;
433 * Weight contributed by tasks; this is the part we can
434 * influence by moving tasks around.
436 unsigned long task_weight
;
442 /* Real-Time classes' related field in a runqueue: */
444 struct rt_prio_array active
;
445 unsigned long rt_nr_running
;
446 #if defined CONFIG_SMP || defined CONFIG_RT_GROUP_SCHED
447 int highest_prio
; /* highest queued rt task prio */
450 unsigned long rt_nr_migratory
;
456 /* Nests inside the rq lock: */
457 spinlock_t rt_runtime_lock
;
459 #ifdef CONFIG_RT_GROUP_SCHED
460 unsigned long rt_nr_boosted
;
463 struct list_head leaf_rt_rq_list
;
464 struct task_group
*tg
;
465 struct sched_rt_entity
*rt_se
;
472 * We add the notion of a root-domain which will be used to define per-domain
473 * variables. Each exclusive cpuset essentially defines an island domain by
474 * fully partitioning the member cpus from any other cpuset. Whenever a new
475 * exclusive cpuset is created, we also create and attach a new root-domain
485 * The "RT overload" flag: it gets set if a CPU has more than
486 * one runnable RT task.
493 * By default the system creates a single root-domain with all cpus as
494 * members (mimicking the global state we have today).
496 static struct root_domain def_root_domain
;
501 * This is the main, per-CPU runqueue data structure.
503 * Locking rule: those places that want to lock multiple runqueues
504 * (such as the load balancing or the thread migration code), lock
505 * acquire operations must be ordered by ascending &runqueue.
512 * nr_running and cpu_load should be in the same cacheline because
513 * remote CPUs use both these fields when doing load calculation.
515 unsigned long nr_running
;
516 #define CPU_LOAD_IDX_MAX 5
517 unsigned long cpu_load
[CPU_LOAD_IDX_MAX
];
518 unsigned char idle_at_tick
;
520 unsigned long last_tick_seen
;
521 unsigned char in_nohz_recently
;
523 /* capture load from *all* tasks on this cpu: */
524 struct load_weight load
;
525 unsigned long nr_load_updates
;
531 #ifdef CONFIG_FAIR_GROUP_SCHED
532 /* list of leaf cfs_rq on this cpu: */
533 struct list_head leaf_cfs_rq_list
;
535 #ifdef CONFIG_RT_GROUP_SCHED
536 struct list_head leaf_rt_rq_list
;
540 * This is part of a global counter where only the total sum
541 * over all CPUs matters. A task can increase this counter on
542 * one CPU and if it got migrated afterwards it may decrease
543 * it on another CPU. Always updated under the runqueue lock:
545 unsigned long nr_uninterruptible
;
547 struct task_struct
*curr
, *idle
;
548 unsigned long next_balance
;
549 struct mm_struct
*prev_mm
;
556 struct root_domain
*rd
;
557 struct sched_domain
*sd
;
559 /* For active balancing */
562 /* cpu of this runqueue: */
565 struct task_struct
*migration_thread
;
566 struct list_head migration_queue
;
569 #ifdef CONFIG_SCHED_HRTICK
570 unsigned long hrtick_flags
;
571 ktime_t hrtick_expire
;
572 struct hrtimer hrtick_timer
;
575 #ifdef CONFIG_SCHEDSTATS
577 struct sched_info rq_sched_info
;
579 /* sys_sched_yield() stats */
580 unsigned int yld_exp_empty
;
581 unsigned int yld_act_empty
;
582 unsigned int yld_both_empty
;
583 unsigned int yld_count
;
585 /* schedule() stats */
586 unsigned int sched_switch
;
587 unsigned int sched_count
;
588 unsigned int sched_goidle
;
590 /* try_to_wake_up() stats */
591 unsigned int ttwu_count
;
592 unsigned int ttwu_local
;
595 unsigned int bkl_count
;
597 struct lock_class_key rq_lock_key
;
600 static DEFINE_PER_CPU_SHARED_ALIGNED(struct rq
, runqueues
);
602 static inline void check_preempt_curr(struct rq
*rq
, struct task_struct
*p
)
604 rq
->curr
->sched_class
->check_preempt_curr(rq
, p
);
607 static inline int cpu_of(struct rq
*rq
)
617 * The domain tree (rq->sd) is protected by RCU's quiescent state transition.
618 * See detach_destroy_domains: synchronize_sched for details.
620 * The domain tree of any CPU may only be accessed from within
621 * preempt-disabled sections.
623 #define for_each_domain(cpu, __sd) \
624 for (__sd = rcu_dereference(cpu_rq(cpu)->sd); __sd; __sd = __sd->parent)
626 #define cpu_rq(cpu) (&per_cpu(runqueues, (cpu)))
627 #define this_rq() (&__get_cpu_var(runqueues))
628 #define task_rq(p) cpu_rq(task_cpu(p))
629 #define cpu_curr(cpu) (cpu_rq(cpu)->curr)
631 static inline void update_rq_clock(struct rq
*rq
)
633 rq
->clock
= sched_clock_cpu(cpu_of(rq
));
637 * Tunables that become constants when CONFIG_SCHED_DEBUG is off:
639 #ifdef CONFIG_SCHED_DEBUG
640 # define const_debug __read_mostly
642 # define const_debug static const
648 * Returns true if the current cpu runqueue is locked.
649 * This interface allows printk to be called with the runqueue lock
650 * held and know whether or not it is OK to wake up the klogd.
652 int runqueue_is_locked(void)
655 struct rq
*rq
= cpu_rq(cpu
);
658 ret
= spin_is_locked(&rq
->lock
);
664 * Debugging: various feature bits
667 #define SCHED_FEAT(name, enabled) \
668 __SCHED_FEAT_##name ,
671 #include "sched_features.h"
676 #define SCHED_FEAT(name, enabled) \
677 (1UL << __SCHED_FEAT_##name) * enabled |
679 const_debug
unsigned int sysctl_sched_features
=
680 #include "sched_features.h"
685 #ifdef CONFIG_SCHED_DEBUG
686 #define SCHED_FEAT(name, enabled) \
689 static __read_mostly
char *sched_feat_names
[] = {
690 #include "sched_features.h"
696 static int sched_feat_open(struct inode
*inode
, struct file
*filp
)
698 filp
->private_data
= inode
->i_private
;
703 sched_feat_read(struct file
*filp
, char __user
*ubuf
,
704 size_t cnt
, loff_t
*ppos
)
711 for (i
= 0; sched_feat_names
[i
]; i
++) {
712 len
+= strlen(sched_feat_names
[i
]);
716 buf
= kmalloc(len
+ 2, GFP_KERNEL
);
720 for (i
= 0; sched_feat_names
[i
]; i
++) {
721 if (sysctl_sched_features
& (1UL << i
))
722 r
+= sprintf(buf
+ r
, "%s ", sched_feat_names
[i
]);
724 r
+= sprintf(buf
+ r
, "NO_%s ", sched_feat_names
[i
]);
727 r
+= sprintf(buf
+ r
, "\n");
728 WARN_ON(r
>= len
+ 2);
730 r
= simple_read_from_buffer(ubuf
, cnt
, ppos
, buf
, r
);
738 sched_feat_write(struct file
*filp
, const char __user
*ubuf
,
739 size_t cnt
, loff_t
*ppos
)
749 if (copy_from_user(&buf
, ubuf
, cnt
))
754 if (strncmp(buf
, "NO_", 3) == 0) {
759 for (i
= 0; sched_feat_names
[i
]; i
++) {
760 int len
= strlen(sched_feat_names
[i
]);
762 if (strncmp(cmp
, sched_feat_names
[i
], len
) == 0) {
764 sysctl_sched_features
&= ~(1UL << i
);
766 sysctl_sched_features
|= (1UL << i
);
771 if (!sched_feat_names
[i
])
779 static struct file_operations sched_feat_fops
= {
780 .open
= sched_feat_open
,
781 .read
= sched_feat_read
,
782 .write
= sched_feat_write
,
785 static __init
int sched_init_debug(void)
787 debugfs_create_file("sched_features", 0644, NULL
, NULL
,
792 late_initcall(sched_init_debug
);
796 #define sched_feat(x) (sysctl_sched_features & (1UL << __SCHED_FEAT_##x))
799 * Number of tasks to iterate in a single balance run.
800 * Limited because this is done with IRQs disabled.
802 const_debug
unsigned int sysctl_sched_nr_migrate
= 32;
805 * period over which we measure -rt task cpu usage in us.
808 unsigned int sysctl_sched_rt_period
= 1000000;
810 static __read_mostly
int scheduler_running
;
813 * part of the period that we allow rt tasks to run in us.
816 int sysctl_sched_rt_runtime
= 950000;
818 static inline u64
global_rt_period(void)
820 return (u64
)sysctl_sched_rt_period
* NSEC_PER_USEC
;
823 static inline u64
global_rt_runtime(void)
825 if (sysctl_sched_rt_period
< 0)
828 return (u64
)sysctl_sched_rt_runtime
* NSEC_PER_USEC
;
831 unsigned long long time_sync_thresh
= 100000;
833 static DEFINE_PER_CPU(unsigned long long, time_offset
);
834 static DEFINE_PER_CPU(unsigned long long, prev_cpu_time
);
837 * Global lock which we take every now and then to synchronize
838 * the CPUs time. This method is not warp-safe, but it's good
839 * enough to synchronize slowly diverging time sources and thus
840 * it's good enough for tracing:
842 static DEFINE_SPINLOCK(time_sync_lock
);
843 static unsigned long long prev_global_time
;
845 static unsigned long long __sync_cpu_clock(unsigned long long time
, int cpu
)
848 * We want this inlined, to not get tracer function calls
849 * in this critical section:
851 spin_acquire(&time_sync_lock
.dep_map
, 0, 0, _THIS_IP_
);
852 __raw_spin_lock(&time_sync_lock
.raw_lock
);
854 if (time
< prev_global_time
) {
855 per_cpu(time_offset
, cpu
) += prev_global_time
- time
;
856 time
= prev_global_time
;
858 prev_global_time
= time
;
861 __raw_spin_unlock(&time_sync_lock
.raw_lock
);
862 spin_release(&time_sync_lock
.dep_map
, 1, _THIS_IP_
);
867 static unsigned long long __cpu_clock(int cpu
)
869 unsigned long long now
;
872 * Only call sched_clock() if the scheduler has already been
873 * initialized (some code might call cpu_clock() very early):
875 if (unlikely(!scheduler_running
))
878 now
= sched_clock_cpu(cpu
);
884 * For kernel-internal use: high-speed (but slightly incorrect) per-cpu
885 * clock constructed from sched_clock():
887 unsigned long long cpu_clock(int cpu
)
889 unsigned long long prev_cpu_time
, time
, delta_time
;
892 local_irq_save(flags
);
893 prev_cpu_time
= per_cpu(prev_cpu_time
, cpu
);
894 time
= __cpu_clock(cpu
) + per_cpu(time_offset
, cpu
);
895 delta_time
= time
-prev_cpu_time
;
897 if (unlikely(delta_time
> time_sync_thresh
)) {
898 time
= __sync_cpu_clock(time
, cpu
);
899 per_cpu(prev_cpu_time
, cpu
) = time
;
901 local_irq_restore(flags
);
905 EXPORT_SYMBOL_GPL(cpu_clock
);
907 #ifndef prepare_arch_switch
908 # define prepare_arch_switch(next) do { } while (0)
910 #ifndef finish_arch_switch
911 # define finish_arch_switch(prev) do { } while (0)
914 static inline int task_current(struct rq
*rq
, struct task_struct
*p
)
916 return rq
->curr
== p
;
919 #ifndef __ARCH_WANT_UNLOCKED_CTXSW
920 static inline int task_running(struct rq
*rq
, struct task_struct
*p
)
922 return task_current(rq
, p
);
925 static inline void prepare_lock_switch(struct rq
*rq
, struct task_struct
*next
)
929 static inline void finish_lock_switch(struct rq
*rq
, struct task_struct
*prev
)
931 #ifdef CONFIG_DEBUG_SPINLOCK
932 /* this is a valid case when another task releases the spinlock */
933 rq
->lock
.owner
= current
;
936 * If we are tracking spinlock dependencies then we have to
937 * fix up the runqueue lock - which gets 'carried over' from
940 spin_acquire(&rq
->lock
.dep_map
, 0, 0, _THIS_IP_
);
942 spin_unlock_irq(&rq
->lock
);
945 #else /* __ARCH_WANT_UNLOCKED_CTXSW */
946 static inline int task_running(struct rq
*rq
, struct task_struct
*p
)
951 return task_current(rq
, p
);
955 static inline void prepare_lock_switch(struct rq
*rq
, struct task_struct
*next
)
959 * We can optimise this out completely for !SMP, because the
960 * SMP rebalancing from interrupt is the only thing that cares
965 #ifdef __ARCH_WANT_INTERRUPTS_ON_CTXSW
966 spin_unlock_irq(&rq
->lock
);
968 spin_unlock(&rq
->lock
);
972 static inline void finish_lock_switch(struct rq
*rq
, struct task_struct
*prev
)
976 * After ->oncpu is cleared, the task can be moved to a different CPU.
977 * We must ensure this doesn't happen until the switch is completely
983 #ifndef __ARCH_WANT_INTERRUPTS_ON_CTXSW
987 #endif /* __ARCH_WANT_UNLOCKED_CTXSW */
990 * __task_rq_lock - lock the runqueue a given task resides on.
991 * Must be called interrupts disabled.
993 static inline struct rq
*__task_rq_lock(struct task_struct
*p
)
997 struct rq
*rq
= task_rq(p
);
998 spin_lock(&rq
->lock
);
999 if (likely(rq
== task_rq(p
)))
1001 spin_unlock(&rq
->lock
);
1006 * task_rq_lock - lock the runqueue a given task resides on and disable
1007 * interrupts. Note the ordering: we can safely lookup the task_rq without
1008 * explicitly disabling preemption.
1010 static struct rq
*task_rq_lock(struct task_struct
*p
, unsigned long *flags
)
1011 __acquires(rq
->lock
)
1016 local_irq_save(*flags
);
1018 spin_lock(&rq
->lock
);
1019 if (likely(rq
== task_rq(p
)))
1021 spin_unlock_irqrestore(&rq
->lock
, *flags
);
1025 static void __task_rq_unlock(struct rq
*rq
)
1026 __releases(rq
->lock
)
1028 spin_unlock(&rq
->lock
);
1031 static inline void task_rq_unlock(struct rq
*rq
, unsigned long *flags
)
1032 __releases(rq
->lock
)
1034 spin_unlock_irqrestore(&rq
->lock
, *flags
);
1038 * this_rq_lock - lock this runqueue and disable interrupts.
1040 static struct rq
*this_rq_lock(void)
1041 __acquires(rq
->lock
)
1045 local_irq_disable();
1047 spin_lock(&rq
->lock
);
1052 static void __resched_task(struct task_struct
*p
, int tif_bit
);
1054 static inline void resched_task(struct task_struct
*p
)
1056 __resched_task(p
, TIF_NEED_RESCHED
);
1059 #ifdef CONFIG_SCHED_HRTICK
1061 * Use HR-timers to deliver accurate preemption points.
1063 * Its all a bit involved since we cannot program an hrt while holding the
1064 * rq->lock. So what we do is store a state in in rq->hrtick_* and ask for a
1067 * When we get rescheduled we reprogram the hrtick_timer outside of the
1070 static inline void resched_hrt(struct task_struct
*p
)
1072 __resched_task(p
, TIF_HRTICK_RESCHED
);
1075 static inline void resched_rq(struct rq
*rq
)
1077 unsigned long flags
;
1079 spin_lock_irqsave(&rq
->lock
, flags
);
1080 resched_task(rq
->curr
);
1081 spin_unlock_irqrestore(&rq
->lock
, flags
);
1085 HRTICK_SET
, /* re-programm hrtick_timer */
1086 HRTICK_RESET
, /* not a new slice */
1087 HRTICK_BLOCK
, /* stop hrtick operations */
1092 * - enabled by features
1093 * - hrtimer is actually high res
1095 static inline int hrtick_enabled(struct rq
*rq
)
1097 if (!sched_feat(HRTICK
))
1099 if (unlikely(test_bit(HRTICK_BLOCK
, &rq
->hrtick_flags
)))
1101 return hrtimer_is_hres_active(&rq
->hrtick_timer
);
1105 * Called to set the hrtick timer state.
1107 * called with rq->lock held and irqs disabled
1109 static void hrtick_start(struct rq
*rq
, u64 delay
, int reset
)
1111 assert_spin_locked(&rq
->lock
);
1114 * preempt at: now + delay
1117 ktime_add_ns(rq
->hrtick_timer
.base
->get_time(), delay
);
1119 * indicate we need to program the timer
1121 __set_bit(HRTICK_SET
, &rq
->hrtick_flags
);
1123 __set_bit(HRTICK_RESET
, &rq
->hrtick_flags
);
1126 * New slices are called from the schedule path and don't need a
1127 * forced reschedule.
1130 resched_hrt(rq
->curr
);
1133 static void hrtick_clear(struct rq
*rq
)
1135 if (hrtimer_active(&rq
->hrtick_timer
))
1136 hrtimer_cancel(&rq
->hrtick_timer
);
1140 * Update the timer from the possible pending state.
1142 static void hrtick_set(struct rq
*rq
)
1146 unsigned long flags
;
1148 WARN_ON_ONCE(cpu_of(rq
) != smp_processor_id());
1150 spin_lock_irqsave(&rq
->lock
, flags
);
1151 set
= __test_and_clear_bit(HRTICK_SET
, &rq
->hrtick_flags
);
1152 reset
= __test_and_clear_bit(HRTICK_RESET
, &rq
->hrtick_flags
);
1153 time
= rq
->hrtick_expire
;
1154 clear_thread_flag(TIF_HRTICK_RESCHED
);
1155 spin_unlock_irqrestore(&rq
->lock
, flags
);
1158 hrtimer_start(&rq
->hrtick_timer
, time
, HRTIMER_MODE_ABS
);
1159 if (reset
&& !hrtimer_active(&rq
->hrtick_timer
))
1166 * High-resolution timer tick.
1167 * Runs from hardirq context with interrupts disabled.
1169 static enum hrtimer_restart
hrtick(struct hrtimer
*timer
)
1171 struct rq
*rq
= container_of(timer
, struct rq
, hrtick_timer
);
1173 WARN_ON_ONCE(cpu_of(rq
) != smp_processor_id());
1175 spin_lock(&rq
->lock
);
1176 update_rq_clock(rq
);
1177 rq
->curr
->sched_class
->task_tick(rq
, rq
->curr
, 1);
1178 spin_unlock(&rq
->lock
);
1180 return HRTIMER_NORESTART
;
1183 static void hotplug_hrtick_disable(int cpu
)
1185 struct rq
*rq
= cpu_rq(cpu
);
1186 unsigned long flags
;
1188 spin_lock_irqsave(&rq
->lock
, flags
);
1189 rq
->hrtick_flags
= 0;
1190 __set_bit(HRTICK_BLOCK
, &rq
->hrtick_flags
);
1191 spin_unlock_irqrestore(&rq
->lock
, flags
);
1196 static void hotplug_hrtick_enable(int cpu
)
1198 struct rq
*rq
= cpu_rq(cpu
);
1199 unsigned long flags
;
1201 spin_lock_irqsave(&rq
->lock
, flags
);
1202 __clear_bit(HRTICK_BLOCK
, &rq
->hrtick_flags
);
1203 spin_unlock_irqrestore(&rq
->lock
, flags
);
1207 hotplug_hrtick(struct notifier_block
*nfb
, unsigned long action
, void *hcpu
)
1209 int cpu
= (int)(long)hcpu
;
1212 case CPU_UP_CANCELED
:
1213 case CPU_UP_CANCELED_FROZEN
:
1214 case CPU_DOWN_PREPARE
:
1215 case CPU_DOWN_PREPARE_FROZEN
:
1217 case CPU_DEAD_FROZEN
:
1218 hotplug_hrtick_disable(cpu
);
1221 case CPU_UP_PREPARE
:
1222 case CPU_UP_PREPARE_FROZEN
:
1223 case CPU_DOWN_FAILED
:
1224 case CPU_DOWN_FAILED_FROZEN
:
1226 case CPU_ONLINE_FROZEN
:
1227 hotplug_hrtick_enable(cpu
);
1234 static void init_hrtick(void)
1236 hotcpu_notifier(hotplug_hrtick
, 0);
1239 static void init_rq_hrtick(struct rq
*rq
)
1241 rq
->hrtick_flags
= 0;
1242 hrtimer_init(&rq
->hrtick_timer
, CLOCK_MONOTONIC
, HRTIMER_MODE_REL
);
1243 rq
->hrtick_timer
.function
= hrtick
;
1244 rq
->hrtick_timer
.cb_mode
= HRTIMER_CB_IRQSAFE_NO_SOFTIRQ
;
1247 void hrtick_resched(void)
1250 unsigned long flags
;
1252 if (!test_thread_flag(TIF_HRTICK_RESCHED
))
1255 local_irq_save(flags
);
1256 rq
= cpu_rq(smp_processor_id());
1258 local_irq_restore(flags
);
1261 static inline void hrtick_clear(struct rq
*rq
)
1265 static inline void hrtick_set(struct rq
*rq
)
1269 static inline void init_rq_hrtick(struct rq
*rq
)
1273 void hrtick_resched(void)
1277 static inline void init_hrtick(void)
1283 * resched_task - mark a task 'to be rescheduled now'.
1285 * On UP this means the setting of the need_resched flag, on SMP it
1286 * might also involve a cross-CPU call to trigger the scheduler on
1291 #ifndef tsk_is_polling
1292 #define tsk_is_polling(t) test_tsk_thread_flag(t, TIF_POLLING_NRFLAG)
1295 static void __resched_task(struct task_struct
*p
, int tif_bit
)
1299 assert_spin_locked(&task_rq(p
)->lock
);
1301 if (unlikely(test_tsk_thread_flag(p
, tif_bit
)))
1304 set_tsk_thread_flag(p
, tif_bit
);
1307 if (cpu
== smp_processor_id())
1310 /* NEED_RESCHED must be visible before we test polling */
1312 if (!tsk_is_polling(p
))
1313 smp_send_reschedule(cpu
);
1316 static void resched_cpu(int cpu
)
1318 struct rq
*rq
= cpu_rq(cpu
);
1319 unsigned long flags
;
1321 if (!spin_trylock_irqsave(&rq
->lock
, flags
))
1323 resched_task(cpu_curr(cpu
));
1324 spin_unlock_irqrestore(&rq
->lock
, flags
);
1329 * When add_timer_on() enqueues a timer into the timer wheel of an
1330 * idle CPU then this timer might expire before the next timer event
1331 * which is scheduled to wake up that CPU. In case of a completely
1332 * idle system the next event might even be infinite time into the
1333 * future. wake_up_idle_cpu() ensures that the CPU is woken up and
1334 * leaves the inner idle loop so the newly added timer is taken into
1335 * account when the CPU goes back to idle and evaluates the timer
1336 * wheel for the next timer event.
1338 void wake_up_idle_cpu(int cpu
)
1340 struct rq
*rq
= cpu_rq(cpu
);
1342 if (cpu
== smp_processor_id())
1346 * This is safe, as this function is called with the timer
1347 * wheel base lock of (cpu) held. When the CPU is on the way
1348 * to idle and has not yet set rq->curr to idle then it will
1349 * be serialized on the timer wheel base lock and take the new
1350 * timer into account automatically.
1352 if (rq
->curr
!= rq
->idle
)
1356 * We can set TIF_RESCHED on the idle task of the other CPU
1357 * lockless. The worst case is that the other CPU runs the
1358 * idle task through an additional NOOP schedule()
1360 set_tsk_thread_flag(rq
->idle
, TIF_NEED_RESCHED
);
1362 /* NEED_RESCHED must be visible before we test polling */
1364 if (!tsk_is_polling(rq
->idle
))
1365 smp_send_reschedule(cpu
);
1370 static void __resched_task(struct task_struct
*p
, int tif_bit
)
1372 assert_spin_locked(&task_rq(p
)->lock
);
1373 set_tsk_thread_flag(p
, tif_bit
);
1377 #if BITS_PER_LONG == 32
1378 # define WMULT_CONST (~0UL)
1380 # define WMULT_CONST (1UL << 32)
1383 #define WMULT_SHIFT 32
1386 * Shift right and round:
1388 #define SRR(x, y) (((x) + (1UL << ((y) - 1))) >> (y))
1391 * delta *= weight / lw
1393 static unsigned long
1394 calc_delta_mine(unsigned long delta_exec
, unsigned long weight
,
1395 struct load_weight
*lw
)
1399 if (!lw
->inv_weight
)
1400 lw
->inv_weight
= 1 + (WMULT_CONST
-lw
->weight
/2)/(lw
->weight
+1);
1402 tmp
= (u64
)delta_exec
* weight
;
1404 * Check whether we'd overflow the 64-bit multiplication:
1406 if (unlikely(tmp
> WMULT_CONST
))
1407 tmp
= SRR(SRR(tmp
, WMULT_SHIFT
/2) * lw
->inv_weight
,
1410 tmp
= SRR(tmp
* lw
->inv_weight
, WMULT_SHIFT
);
1412 return (unsigned long)min(tmp
, (u64
)(unsigned long)LONG_MAX
);
1415 static inline void update_load_add(struct load_weight
*lw
, unsigned long inc
)
1421 static inline void update_load_sub(struct load_weight
*lw
, unsigned long dec
)
1428 * To aid in avoiding the subversion of "niceness" due to uneven distribution
1429 * of tasks with abnormal "nice" values across CPUs the contribution that
1430 * each task makes to its run queue's load is weighted according to its
1431 * scheduling class and "nice" value. For SCHED_NORMAL tasks this is just a
1432 * scaled version of the new time slice allocation that they receive on time
1436 #define WEIGHT_IDLEPRIO 2
1437 #define WMULT_IDLEPRIO (1 << 31)
1440 * Nice levels are multiplicative, with a gentle 10% change for every
1441 * nice level changed. I.e. when a CPU-bound task goes from nice 0 to
1442 * nice 1, it will get ~10% less CPU time than another CPU-bound task
1443 * that remained on nice 0.
1445 * The "10% effect" is relative and cumulative: from _any_ nice level,
1446 * if you go up 1 level, it's -10% CPU usage, if you go down 1 level
1447 * it's +10% CPU usage. (to achieve that we use a multiplier of 1.25.
1448 * If a task goes up by ~10% and another task goes down by ~10% then
1449 * the relative distance between them is ~25%.)
1451 static const int prio_to_weight
[40] = {
1452 /* -20 */ 88761, 71755, 56483, 46273, 36291,
1453 /* -15 */ 29154, 23254, 18705, 14949, 11916,
1454 /* -10 */ 9548, 7620, 6100, 4904, 3906,
1455 /* -5 */ 3121, 2501, 1991, 1586, 1277,
1456 /* 0 */ 1024, 820, 655, 526, 423,
1457 /* 5 */ 335, 272, 215, 172, 137,
1458 /* 10 */ 110, 87, 70, 56, 45,
1459 /* 15 */ 36, 29, 23, 18, 15,
1463 * Inverse (2^32/x) values of the prio_to_weight[] array, precalculated.
1465 * In cases where the weight does not change often, we can use the
1466 * precalculated inverse to speed up arithmetics by turning divisions
1467 * into multiplications:
1469 static const u32 prio_to_wmult
[40] = {
1470 /* -20 */ 48388, 59856, 76040, 92818, 118348,
1471 /* -15 */ 147320, 184698, 229616, 287308, 360437,
1472 /* -10 */ 449829, 563644, 704093, 875809, 1099582,
1473 /* -5 */ 1376151, 1717300, 2157191, 2708050, 3363326,
1474 /* 0 */ 4194304, 5237765, 6557202, 8165337, 10153587,
1475 /* 5 */ 12820798, 15790321, 19976592, 24970740, 31350126,
1476 /* 10 */ 39045157, 49367440, 61356676, 76695844, 95443717,
1477 /* 15 */ 119304647, 148102320, 186737708, 238609294, 286331153,
1480 static void activate_task(struct rq
*rq
, struct task_struct
*p
, int wakeup
);
1483 * runqueue iterator, to support SMP load-balancing between different
1484 * scheduling classes, without having to expose their internal data
1485 * structures to the load-balancing proper:
1487 struct rq_iterator
{
1489 struct task_struct
*(*start
)(void *);
1490 struct task_struct
*(*next
)(void *);
1494 static unsigned long
1495 balance_tasks(struct rq
*this_rq
, int this_cpu
, struct rq
*busiest
,
1496 unsigned long max_load_move
, struct sched_domain
*sd
,
1497 enum cpu_idle_type idle
, int *all_pinned
,
1498 int *this_best_prio
, struct rq_iterator
*iterator
);
1501 iter_move_one_task(struct rq
*this_rq
, int this_cpu
, struct rq
*busiest
,
1502 struct sched_domain
*sd
, enum cpu_idle_type idle
,
1503 struct rq_iterator
*iterator
);
1506 #ifdef CONFIG_CGROUP_CPUACCT
1507 static void cpuacct_charge(struct task_struct
*tsk
, u64 cputime
);
1509 static inline void cpuacct_charge(struct task_struct
*tsk
, u64 cputime
) {}
1512 static inline void inc_cpu_load(struct rq
*rq
, unsigned long load
)
1514 update_load_add(&rq
->load
, load
);
1517 static inline void dec_cpu_load(struct rq
*rq
, unsigned long load
)
1519 update_load_sub(&rq
->load
, load
);
1523 static unsigned long source_load(int cpu
, int type
);
1524 static unsigned long target_load(int cpu
, int type
);
1525 static unsigned long cpu_avg_load_per_task(int cpu
);
1526 static int task_hot(struct task_struct
*p
, u64 now
, struct sched_domain
*sd
);
1528 #ifdef CONFIG_FAIR_GROUP_SCHED
1531 * Group load balancing.
1533 * We calculate a few balance domain wide aggregate numbers; load and weight.
1534 * Given the pictures below, and assuming each item has equal weight:
1545 * A and B get 1/3-rd of the total load. C and D get 1/3-rd of A's 1/3-rd,
1546 * which equals 1/9-th of the total load.
1549 * The weight of this group on the selected cpus.
1552 * Direct sum of all the cpu's their rq weight, e.g. A would get 3 while
1556 * Part of the rq_weight contributed by tasks; all groups except B would
1560 static inline struct aggregate_struct
*
1561 aggregate(struct task_group
*tg
, struct sched_domain
*sd
)
1563 return &tg
->cfs_rq
[sd
->first_cpu
]->aggregate
;
1566 typedef void (*aggregate_func
)(struct task_group
*, struct sched_domain
*);
1569 * Iterate the full tree, calling @down when first entering a node and @up when
1570 * leaving it for the final time.
1573 void aggregate_walk_tree(aggregate_func down
, aggregate_func up
,
1574 struct sched_domain
*sd
)
1576 struct task_group
*parent
, *child
;
1579 parent
= &root_task_group
;
1581 (*down
)(parent
, sd
);
1582 list_for_each_entry_rcu(child
, &parent
->children
, siblings
) {
1592 parent
= parent
->parent
;
1599 * Calculate the aggregate runqueue weight.
1602 void aggregate_group_weight(struct task_group
*tg
, struct sched_domain
*sd
)
1604 unsigned long rq_weight
= 0;
1605 unsigned long task_weight
= 0;
1608 for_each_cpu_mask(i
, sd
->span
) {
1609 rq_weight
+= tg
->cfs_rq
[i
]->load
.weight
;
1610 task_weight
+= tg
->cfs_rq
[i
]->task_weight
;
1613 aggregate(tg
, sd
)->rq_weight
= rq_weight
;
1614 aggregate(tg
, sd
)->task_weight
= task_weight
;
1618 * Compute the weight of this group on the given cpus.
1621 void aggregate_group_shares(struct task_group
*tg
, struct sched_domain
*sd
)
1623 unsigned long shares
= 0;
1626 for_each_cpu_mask(i
, sd
->span
)
1627 shares
+= tg
->cfs_rq
[i
]->shares
;
1629 if ((!shares
&& aggregate(tg
, sd
)->rq_weight
) || shares
> tg
->shares
)
1630 shares
= tg
->shares
;
1632 aggregate(tg
, sd
)->shares
= shares
;
1636 * Compute the load fraction assigned to this group, relies on the aggregate
1637 * weight and this group's parent's load, i.e. top-down.
1640 void aggregate_group_load(struct task_group
*tg
, struct sched_domain
*sd
)
1648 for_each_cpu_mask(i
, sd
->span
)
1649 load
+= cpu_rq(i
)->load
.weight
;
1652 load
= aggregate(tg
->parent
, sd
)->load
;
1655 * shares is our weight in the parent's rq so
1656 * shares/parent->rq_weight gives our fraction of the load
1658 load
*= aggregate(tg
, sd
)->shares
;
1659 load
/= aggregate(tg
->parent
, sd
)->rq_weight
+ 1;
1662 aggregate(tg
, sd
)->load
= load
;
1665 static void __set_se_shares(struct sched_entity
*se
, unsigned long shares
);
1668 * Calculate and set the cpu's group shares.
1671 __update_group_shares_cpu(struct task_group
*tg
, struct sched_domain
*sd
,
1675 unsigned long shares
;
1676 unsigned long rq_weight
;
1681 rq_weight
= tg
->cfs_rq
[tcpu
]->load
.weight
;
1684 * If there are currently no tasks on the cpu pretend there is one of
1685 * average load so that when a new task gets to run here it will not
1686 * get delayed by group starvation.
1690 rq_weight
= NICE_0_LOAD
;
1694 * \Sum shares * rq_weight
1695 * shares = -----------------------
1699 shares
= aggregate(tg
, sd
)->shares
* rq_weight
;
1700 shares
/= aggregate(tg
, sd
)->rq_weight
+ 1;
1703 * record the actual number of shares, not the boosted amount.
1705 tg
->cfs_rq
[tcpu
]->shares
= boost
? 0 : shares
;
1707 if (shares
< MIN_SHARES
)
1708 shares
= MIN_SHARES
;
1709 else if (shares
> MAX_SHARES
)
1710 shares
= MAX_SHARES
;
1712 __set_se_shares(tg
->se
[tcpu
], shares
);
1716 * Re-adjust the weights on the cpu the task came from and on the cpu the
1720 __move_group_shares(struct task_group
*tg
, struct sched_domain
*sd
,
1723 unsigned long shares
;
1725 shares
= tg
->cfs_rq
[scpu
]->shares
+ tg
->cfs_rq
[dcpu
]->shares
;
1727 __update_group_shares_cpu(tg
, sd
, scpu
);
1728 __update_group_shares_cpu(tg
, sd
, dcpu
);
1731 * ensure we never loose shares due to rounding errors in the
1732 * above redistribution.
1734 shares
-= tg
->cfs_rq
[scpu
]->shares
+ tg
->cfs_rq
[dcpu
]->shares
;
1736 tg
->cfs_rq
[dcpu
]->shares
+= shares
;
1740 * Because changing a group's shares changes the weight of the super-group
1741 * we need to walk up the tree and change all shares until we hit the root.
1744 move_group_shares(struct task_group
*tg
, struct sched_domain
*sd
,
1748 __move_group_shares(tg
, sd
, scpu
, dcpu
);
1754 void aggregate_group_set_shares(struct task_group
*tg
, struct sched_domain
*sd
)
1756 unsigned long shares
= aggregate(tg
, sd
)->shares
;
1759 for_each_cpu_mask(i
, sd
->span
) {
1760 struct rq
*rq
= cpu_rq(i
);
1761 unsigned long flags
;
1763 spin_lock_irqsave(&rq
->lock
, flags
);
1764 __update_group_shares_cpu(tg
, sd
, i
);
1765 spin_unlock_irqrestore(&rq
->lock
, flags
);
1768 aggregate_group_shares(tg
, sd
);
1771 * ensure we never loose shares due to rounding errors in the
1772 * above redistribution.
1774 shares
-= aggregate(tg
, sd
)->shares
;
1776 tg
->cfs_rq
[sd
->first_cpu
]->shares
+= shares
;
1777 aggregate(tg
, sd
)->shares
+= shares
;
1782 * Calculate the accumulative weight and recursive load of each task group
1783 * while walking down the tree.
1786 void aggregate_get_down(struct task_group
*tg
, struct sched_domain
*sd
)
1788 aggregate_group_weight(tg
, sd
);
1789 aggregate_group_shares(tg
, sd
);
1790 aggregate_group_load(tg
, sd
);
1794 * Rebalance the cpu shares while walking back up the tree.
1797 void aggregate_get_up(struct task_group
*tg
, struct sched_domain
*sd
)
1799 aggregate_group_set_shares(tg
, sd
);
1802 static DEFINE_PER_CPU(spinlock_t
, aggregate_lock
);
1804 static void __init
init_aggregate(void)
1808 for_each_possible_cpu(i
)
1809 spin_lock_init(&per_cpu(aggregate_lock
, i
));
1812 static int get_aggregate(struct sched_domain
*sd
)
1814 if (!spin_trylock(&per_cpu(aggregate_lock
, sd
->first_cpu
)))
1817 aggregate_walk_tree(aggregate_get_down
, aggregate_get_up
, sd
);
1821 static void put_aggregate(struct sched_domain
*sd
)
1823 spin_unlock(&per_cpu(aggregate_lock
, sd
->first_cpu
));
1826 static void cfs_rq_set_shares(struct cfs_rq
*cfs_rq
, unsigned long shares
)
1828 cfs_rq
->shares
= shares
;
1833 static inline void init_aggregate(void)
1837 static inline int get_aggregate(struct sched_domain
*sd
)
1842 static inline void put_aggregate(struct sched_domain
*sd
)
1847 #else /* CONFIG_SMP */
1849 #ifdef CONFIG_FAIR_GROUP_SCHED
1850 static void cfs_rq_set_shares(struct cfs_rq
*cfs_rq
, unsigned long shares
)
1855 #endif /* CONFIG_SMP */
1857 #include "sched_stats.h"
1858 #include "sched_idletask.c"
1859 #include "sched_fair.c"
1860 #include "sched_rt.c"
1861 #ifdef CONFIG_SCHED_DEBUG
1862 # include "sched_debug.c"
1865 #define sched_class_highest (&rt_sched_class)
1867 static void inc_nr_running(struct rq
*rq
)
1872 static void dec_nr_running(struct rq
*rq
)
1877 static void set_load_weight(struct task_struct
*p
)
1879 if (task_has_rt_policy(p
)) {
1880 p
->se
.load
.weight
= prio_to_weight
[0] * 2;
1881 p
->se
.load
.inv_weight
= prio_to_wmult
[0] >> 1;
1886 * SCHED_IDLE tasks get minimal weight:
1888 if (p
->policy
== SCHED_IDLE
) {
1889 p
->se
.load
.weight
= WEIGHT_IDLEPRIO
;
1890 p
->se
.load
.inv_weight
= WMULT_IDLEPRIO
;
1894 p
->se
.load
.weight
= prio_to_weight
[p
->static_prio
- MAX_RT_PRIO
];
1895 p
->se
.load
.inv_weight
= prio_to_wmult
[p
->static_prio
- MAX_RT_PRIO
];
1898 static void enqueue_task(struct rq
*rq
, struct task_struct
*p
, int wakeup
)
1900 sched_info_queued(p
);
1901 p
->sched_class
->enqueue_task(rq
, p
, wakeup
);
1905 static void dequeue_task(struct rq
*rq
, struct task_struct
*p
, int sleep
)
1907 p
->sched_class
->dequeue_task(rq
, p
, sleep
);
1912 * __normal_prio - return the priority that is based on the static prio
1914 static inline int __normal_prio(struct task_struct
*p
)
1916 return p
->static_prio
;
1920 * Calculate the expected normal priority: i.e. priority
1921 * without taking RT-inheritance into account. Might be
1922 * boosted by interactivity modifiers. Changes upon fork,
1923 * setprio syscalls, and whenever the interactivity
1924 * estimator recalculates.
1926 static inline int normal_prio(struct task_struct
*p
)
1930 if (task_has_rt_policy(p
))
1931 prio
= MAX_RT_PRIO
-1 - p
->rt_priority
;
1933 prio
= __normal_prio(p
);
1938 * Calculate the current priority, i.e. the priority
1939 * taken into account by the scheduler. This value might
1940 * be boosted by RT tasks, or might be boosted by
1941 * interactivity modifiers. Will be RT if the task got
1942 * RT-boosted. If not then it returns p->normal_prio.
1944 static int effective_prio(struct task_struct
*p
)
1946 p
->normal_prio
= normal_prio(p
);
1948 * If we are RT tasks or we were boosted to RT priority,
1949 * keep the priority unchanged. Otherwise, update priority
1950 * to the normal priority:
1952 if (!rt_prio(p
->prio
))
1953 return p
->normal_prio
;
1958 * activate_task - move a task to the runqueue.
1960 static void activate_task(struct rq
*rq
, struct task_struct
*p
, int wakeup
)
1962 if (task_contributes_to_load(p
))
1963 rq
->nr_uninterruptible
--;
1965 enqueue_task(rq
, p
, wakeup
);
1970 * deactivate_task - remove a task from the runqueue.
1972 static void deactivate_task(struct rq
*rq
, struct task_struct
*p
, int sleep
)
1974 if (task_contributes_to_load(p
))
1975 rq
->nr_uninterruptible
++;
1977 dequeue_task(rq
, p
, sleep
);
1982 * task_curr - is this task currently executing on a CPU?
1983 * @p: the task in question.
1985 inline int task_curr(const struct task_struct
*p
)
1987 return cpu_curr(task_cpu(p
)) == p
;
1990 /* Used instead of source_load when we know the type == 0 */
1991 unsigned long weighted_cpuload(const int cpu
)
1993 return cpu_rq(cpu
)->load
.weight
;
1996 static inline void __set_task_cpu(struct task_struct
*p
, unsigned int cpu
)
1998 set_task_rq(p
, cpu
);
2001 * After ->cpu is set up to a new value, task_rq_lock(p, ...) can be
2002 * successfuly executed on another CPU. We must ensure that updates of
2003 * per-task data have been completed by this moment.
2006 task_thread_info(p
)->cpu
= cpu
;
2010 static inline void check_class_changed(struct rq
*rq
, struct task_struct
*p
,
2011 const struct sched_class
*prev_class
,
2012 int oldprio
, int running
)
2014 if (prev_class
!= p
->sched_class
) {
2015 if (prev_class
->switched_from
)
2016 prev_class
->switched_from(rq
, p
, running
);
2017 p
->sched_class
->switched_to(rq
, p
, running
);
2019 p
->sched_class
->prio_changed(rq
, p
, oldprio
, running
);
2025 * Is this task likely cache-hot:
2028 task_hot(struct task_struct
*p
, u64 now
, struct sched_domain
*sd
)
2033 * Buddy candidates are cache hot:
2035 if (sched_feat(CACHE_HOT_BUDDY
) && (&p
->se
== cfs_rq_of(&p
->se
)->next
))
2038 if (p
->sched_class
!= &fair_sched_class
)
2041 if (sysctl_sched_migration_cost
== -1)
2043 if (sysctl_sched_migration_cost
== 0)
2046 delta
= now
- p
->se
.exec_start
;
2048 return delta
< (s64
)sysctl_sched_migration_cost
;
2052 void set_task_cpu(struct task_struct
*p
, unsigned int new_cpu
)
2054 int old_cpu
= task_cpu(p
);
2055 struct rq
*old_rq
= cpu_rq(old_cpu
), *new_rq
= cpu_rq(new_cpu
);
2056 struct cfs_rq
*old_cfsrq
= task_cfs_rq(p
),
2057 *new_cfsrq
= cpu_cfs_rq(old_cfsrq
, new_cpu
);
2060 clock_offset
= old_rq
->clock
- new_rq
->clock
;
2062 #ifdef CONFIG_SCHEDSTATS
2063 if (p
->se
.wait_start
)
2064 p
->se
.wait_start
-= clock_offset
;
2065 if (p
->se
.sleep_start
)
2066 p
->se
.sleep_start
-= clock_offset
;
2067 if (p
->se
.block_start
)
2068 p
->se
.block_start
-= clock_offset
;
2069 if (old_cpu
!= new_cpu
) {
2070 schedstat_inc(p
, se
.nr_migrations
);
2071 if (task_hot(p
, old_rq
->clock
, NULL
))
2072 schedstat_inc(p
, se
.nr_forced2_migrations
);
2075 p
->se
.vruntime
-= old_cfsrq
->min_vruntime
-
2076 new_cfsrq
->min_vruntime
;
2078 __set_task_cpu(p
, new_cpu
);
2081 struct migration_req
{
2082 struct list_head list
;
2084 struct task_struct
*task
;
2087 struct completion done
;
2091 * The task's runqueue lock must be held.
2092 * Returns true if you have to wait for migration thread.
2095 migrate_task(struct task_struct
*p
, int dest_cpu
, struct migration_req
*req
)
2097 struct rq
*rq
= task_rq(p
);
2100 * If the task is not on a runqueue (and not running), then
2101 * it is sufficient to simply update the task's cpu field.
2103 if (!p
->se
.on_rq
&& !task_running(rq
, p
)) {
2104 set_task_cpu(p
, dest_cpu
);
2108 init_completion(&req
->done
);
2110 req
->dest_cpu
= dest_cpu
;
2111 list_add(&req
->list
, &rq
->migration_queue
);
2117 * wait_task_inactive - wait for a thread to unschedule.
2119 * The caller must ensure that the task *will* unschedule sometime soon,
2120 * else this function might spin for a *long* time. This function can't
2121 * be called with interrupts off, or it may introduce deadlock with
2122 * smp_call_function() if an IPI is sent by the same process we are
2123 * waiting to become inactive.
2125 void wait_task_inactive(struct task_struct
*p
)
2127 unsigned long flags
;
2133 * We do the initial early heuristics without holding
2134 * any task-queue locks at all. We'll only try to get
2135 * the runqueue lock when things look like they will
2141 * If the task is actively running on another CPU
2142 * still, just relax and busy-wait without holding
2145 * NOTE! Since we don't hold any locks, it's not
2146 * even sure that "rq" stays as the right runqueue!
2147 * But we don't care, since "task_running()" will
2148 * return false if the runqueue has changed and p
2149 * is actually now running somewhere else!
2151 while (task_running(rq
, p
))
2155 * Ok, time to look more closely! We need the rq
2156 * lock now, to be *sure*. If we're wrong, we'll
2157 * just go back and repeat.
2159 rq
= task_rq_lock(p
, &flags
);
2160 running
= task_running(rq
, p
);
2161 on_rq
= p
->se
.on_rq
;
2162 task_rq_unlock(rq
, &flags
);
2165 * Was it really running after all now that we
2166 * checked with the proper locks actually held?
2168 * Oops. Go back and try again..
2170 if (unlikely(running
)) {
2176 * It's not enough that it's not actively running,
2177 * it must be off the runqueue _entirely_, and not
2180 * So if it wa still runnable (but just not actively
2181 * running right now), it's preempted, and we should
2182 * yield - it could be a while.
2184 if (unlikely(on_rq
)) {
2185 schedule_timeout_uninterruptible(1);
2190 * Ahh, all good. It wasn't running, and it wasn't
2191 * runnable, which means that it will never become
2192 * running in the future either. We're all done!
2199 * kick_process - kick a running thread to enter/exit the kernel
2200 * @p: the to-be-kicked thread
2202 * Cause a process which is running on another CPU to enter
2203 * kernel-mode, without any delay. (to get signals handled.)
2205 * NOTE: this function doesnt have to take the runqueue lock,
2206 * because all it wants to ensure is that the remote task enters
2207 * the kernel. If the IPI races and the task has been migrated
2208 * to another CPU then no harm is done and the purpose has been
2211 void kick_process(struct task_struct
*p
)
2217 if ((cpu
!= smp_processor_id()) && task_curr(p
))
2218 smp_send_reschedule(cpu
);
2223 * Return a low guess at the load of a migration-source cpu weighted
2224 * according to the scheduling class and "nice" value.
2226 * We want to under-estimate the load of migration sources, to
2227 * balance conservatively.
2229 static unsigned long source_load(int cpu
, int type
)
2231 struct rq
*rq
= cpu_rq(cpu
);
2232 unsigned long total
= weighted_cpuload(cpu
);
2237 return min(rq
->cpu_load
[type
-1], total
);
2241 * Return a high guess at the load of a migration-target cpu weighted
2242 * according to the scheduling class and "nice" value.
2244 static unsigned long target_load(int cpu
, int type
)
2246 struct rq
*rq
= cpu_rq(cpu
);
2247 unsigned long total
= weighted_cpuload(cpu
);
2252 return max(rq
->cpu_load
[type
-1], total
);
2256 * Return the average load per task on the cpu's run queue
2258 static unsigned long cpu_avg_load_per_task(int cpu
)
2260 struct rq
*rq
= cpu_rq(cpu
);
2261 unsigned long total
= weighted_cpuload(cpu
);
2262 unsigned long n
= rq
->nr_running
;
2264 return n
? total
/ n
: SCHED_LOAD_SCALE
;
2268 * find_idlest_group finds and returns the least busy CPU group within the
2271 static struct sched_group
*
2272 find_idlest_group(struct sched_domain
*sd
, struct task_struct
*p
, int this_cpu
)
2274 struct sched_group
*idlest
= NULL
, *this = NULL
, *group
= sd
->groups
;
2275 unsigned long min_load
= ULONG_MAX
, this_load
= 0;
2276 int load_idx
= sd
->forkexec_idx
;
2277 int imbalance
= 100 + (sd
->imbalance_pct
-100)/2;
2280 unsigned long load
, avg_load
;
2284 /* Skip over this group if it has no CPUs allowed */
2285 if (!cpus_intersects(group
->cpumask
, p
->cpus_allowed
))
2288 local_group
= cpu_isset(this_cpu
, group
->cpumask
);
2290 /* Tally up the load of all CPUs in the group */
2293 for_each_cpu_mask(i
, group
->cpumask
) {
2294 /* Bias balancing toward cpus of our domain */
2296 load
= source_load(i
, load_idx
);
2298 load
= target_load(i
, load_idx
);
2303 /* Adjust by relative CPU power of the group */
2304 avg_load
= sg_div_cpu_power(group
,
2305 avg_load
* SCHED_LOAD_SCALE
);
2308 this_load
= avg_load
;
2310 } else if (avg_load
< min_load
) {
2311 min_load
= avg_load
;
2314 } while (group
= group
->next
, group
!= sd
->groups
);
2316 if (!idlest
|| 100*this_load
< imbalance
*min_load
)
2322 * find_idlest_cpu - find the idlest cpu among the cpus in group.
2325 find_idlest_cpu(struct sched_group
*group
, struct task_struct
*p
, int this_cpu
,
2328 unsigned long load
, min_load
= ULONG_MAX
;
2332 /* Traverse only the allowed CPUs */
2333 cpus_and(*tmp
, group
->cpumask
, p
->cpus_allowed
);
2335 for_each_cpu_mask(i
, *tmp
) {
2336 load
= weighted_cpuload(i
);
2338 if (load
< min_load
|| (load
== min_load
&& i
== this_cpu
)) {
2348 * sched_balance_self: balance the current task (running on cpu) in domains
2349 * that have the 'flag' flag set. In practice, this is SD_BALANCE_FORK and
2352 * Balance, ie. select the least loaded group.
2354 * Returns the target CPU number, or the same CPU if no balancing is needed.
2356 * preempt must be disabled.
2358 static int sched_balance_self(int cpu
, int flag
)
2360 struct task_struct
*t
= current
;
2361 struct sched_domain
*tmp
, *sd
= NULL
;
2363 for_each_domain(cpu
, tmp
) {
2365 * If power savings logic is enabled for a domain, stop there.
2367 if (tmp
->flags
& SD_POWERSAVINGS_BALANCE
)
2369 if (tmp
->flags
& flag
)
2374 cpumask_t span
, tmpmask
;
2375 struct sched_group
*group
;
2376 int new_cpu
, weight
;
2378 if (!(sd
->flags
& flag
)) {
2384 group
= find_idlest_group(sd
, t
, cpu
);
2390 new_cpu
= find_idlest_cpu(group
, t
, cpu
, &tmpmask
);
2391 if (new_cpu
== -1 || new_cpu
== cpu
) {
2392 /* Now try balancing at a lower domain level of cpu */
2397 /* Now try balancing at a lower domain level of new_cpu */
2400 weight
= cpus_weight(span
);
2401 for_each_domain(cpu
, tmp
) {
2402 if (weight
<= cpus_weight(tmp
->span
))
2404 if (tmp
->flags
& flag
)
2407 /* while loop will break here if sd == NULL */
2413 #endif /* CONFIG_SMP */
2416 * try_to_wake_up - wake up a thread
2417 * @p: the to-be-woken-up thread
2418 * @state: the mask of task states that can be woken
2419 * @sync: do a synchronous wakeup?
2421 * Put it on the run-queue if it's not already there. The "current"
2422 * thread is always on the run-queue (except when the actual
2423 * re-schedule is in progress), and as such you're allowed to do
2424 * the simpler "current->state = TASK_RUNNING" to mark yourself
2425 * runnable without the overhead of this.
2427 * returns failure only if the task is already active.
2429 static int try_to_wake_up(struct task_struct
*p
, unsigned int state
, int sync
)
2431 int cpu
, orig_cpu
, this_cpu
, success
= 0;
2432 unsigned long flags
;
2436 if (!sched_feat(SYNC_WAKEUPS
))
2440 rq
= task_rq_lock(p
, &flags
);
2441 old_state
= p
->state
;
2442 if (!(old_state
& state
))
2450 this_cpu
= smp_processor_id();
2453 if (unlikely(task_running(rq
, p
)))
2456 cpu
= p
->sched_class
->select_task_rq(p
, sync
);
2457 if (cpu
!= orig_cpu
) {
2458 set_task_cpu(p
, cpu
);
2459 task_rq_unlock(rq
, &flags
);
2460 /* might preempt at this point */
2461 rq
= task_rq_lock(p
, &flags
);
2462 old_state
= p
->state
;
2463 if (!(old_state
& state
))
2468 this_cpu
= smp_processor_id();
2472 #ifdef CONFIG_SCHEDSTATS
2473 schedstat_inc(rq
, ttwu_count
);
2474 if (cpu
== this_cpu
)
2475 schedstat_inc(rq
, ttwu_local
);
2477 struct sched_domain
*sd
;
2478 for_each_domain(this_cpu
, sd
) {
2479 if (cpu_isset(cpu
, sd
->span
)) {
2480 schedstat_inc(sd
, ttwu_wake_remote
);
2488 #endif /* CONFIG_SMP */
2489 schedstat_inc(p
, se
.nr_wakeups
);
2491 schedstat_inc(p
, se
.nr_wakeups_sync
);
2492 if (orig_cpu
!= cpu
)
2493 schedstat_inc(p
, se
.nr_wakeups_migrate
);
2494 if (cpu
== this_cpu
)
2495 schedstat_inc(p
, se
.nr_wakeups_local
);
2497 schedstat_inc(p
, se
.nr_wakeups_remote
);
2498 update_rq_clock(rq
);
2499 activate_task(rq
, p
, 1);
2503 ftrace_wake_up_task(rq
, p
, rq
->curr
);
2504 check_preempt_curr(rq
, p
);
2506 p
->state
= TASK_RUNNING
;
2508 if (p
->sched_class
->task_wake_up
)
2509 p
->sched_class
->task_wake_up(rq
, p
);
2512 task_rq_unlock(rq
, &flags
);
2517 int wake_up_process(struct task_struct
*p
)
2519 return try_to_wake_up(p
, TASK_ALL
, 0);
2521 EXPORT_SYMBOL(wake_up_process
);
2523 int wake_up_state(struct task_struct
*p
, unsigned int state
)
2525 return try_to_wake_up(p
, state
, 0);
2529 * Perform scheduler related setup for a newly forked process p.
2530 * p is forked by current.
2532 * __sched_fork() is basic setup used by init_idle() too:
2534 static void __sched_fork(struct task_struct
*p
)
2536 p
->se
.exec_start
= 0;
2537 p
->se
.sum_exec_runtime
= 0;
2538 p
->se
.prev_sum_exec_runtime
= 0;
2539 p
->se
.last_wakeup
= 0;
2540 p
->se
.avg_overlap
= 0;
2542 #ifdef CONFIG_SCHEDSTATS
2543 p
->se
.wait_start
= 0;
2544 p
->se
.sum_sleep_runtime
= 0;
2545 p
->se
.sleep_start
= 0;
2546 p
->se
.block_start
= 0;
2547 p
->se
.sleep_max
= 0;
2548 p
->se
.block_max
= 0;
2550 p
->se
.slice_max
= 0;
2554 INIT_LIST_HEAD(&p
->rt
.run_list
);
2556 INIT_LIST_HEAD(&p
->se
.group_node
);
2558 #ifdef CONFIG_PREEMPT_NOTIFIERS
2559 INIT_HLIST_HEAD(&p
->preempt_notifiers
);
2563 * We mark the process as running here, but have not actually
2564 * inserted it onto the runqueue yet. This guarantees that
2565 * nobody will actually run it, and a signal or other external
2566 * event cannot wake it up and insert it on the runqueue either.
2568 p
->state
= TASK_RUNNING
;
2572 * fork()/clone()-time setup:
2574 void sched_fork(struct task_struct
*p
, int clone_flags
)
2576 int cpu
= get_cpu();
2581 cpu
= sched_balance_self(cpu
, SD_BALANCE_FORK
);
2583 set_task_cpu(p
, cpu
);
2586 * Make sure we do not leak PI boosting priority to the child:
2588 p
->prio
= current
->normal_prio
;
2589 if (!rt_prio(p
->prio
))
2590 p
->sched_class
= &fair_sched_class
;
2592 #if defined(CONFIG_SCHEDSTATS) || defined(CONFIG_TASK_DELAY_ACCT)
2593 if (likely(sched_info_on()))
2594 memset(&p
->sched_info
, 0, sizeof(p
->sched_info
));
2596 #if defined(CONFIG_SMP) && defined(__ARCH_WANT_UNLOCKED_CTXSW)
2599 #ifdef CONFIG_PREEMPT
2600 /* Want to start with kernel preemption disabled. */
2601 task_thread_info(p
)->preempt_count
= 1;
2607 * wake_up_new_task - wake up a newly created task for the first time.
2609 * This function will do some initial scheduler statistics housekeeping
2610 * that must be done for every newly created context, then puts the task
2611 * on the runqueue and wakes it.
2613 void wake_up_new_task(struct task_struct
*p
, unsigned long clone_flags
)
2615 unsigned long flags
;
2618 rq
= task_rq_lock(p
, &flags
);
2619 BUG_ON(p
->state
!= TASK_RUNNING
);
2620 update_rq_clock(rq
);
2622 p
->prio
= effective_prio(p
);
2624 if (!p
->sched_class
->task_new
|| !current
->se
.on_rq
) {
2625 activate_task(rq
, p
, 0);
2628 * Let the scheduling class do new task startup
2629 * management (if any):
2631 p
->sched_class
->task_new(rq
, p
);
2634 ftrace_wake_up_task(rq
, p
, rq
->curr
);
2635 check_preempt_curr(rq
, p
);
2637 if (p
->sched_class
->task_wake_up
)
2638 p
->sched_class
->task_wake_up(rq
, p
);
2640 task_rq_unlock(rq
, &flags
);
2643 #ifdef CONFIG_PREEMPT_NOTIFIERS
2646 * preempt_notifier_register - tell me when current is being being preempted & rescheduled
2647 * @notifier: notifier struct to register
2649 void preempt_notifier_register(struct preempt_notifier
*notifier
)
2651 hlist_add_head(¬ifier
->link
, ¤t
->preempt_notifiers
);
2653 EXPORT_SYMBOL_GPL(preempt_notifier_register
);
2656 * preempt_notifier_unregister - no longer interested in preemption notifications
2657 * @notifier: notifier struct to unregister
2659 * This is safe to call from within a preemption notifier.
2661 void preempt_notifier_unregister(struct preempt_notifier
*notifier
)
2663 hlist_del(¬ifier
->link
);
2665 EXPORT_SYMBOL_GPL(preempt_notifier_unregister
);
2667 static void fire_sched_in_preempt_notifiers(struct task_struct
*curr
)
2669 struct preempt_notifier
*notifier
;
2670 struct hlist_node
*node
;
2672 hlist_for_each_entry(notifier
, node
, &curr
->preempt_notifiers
, link
)
2673 notifier
->ops
->sched_in(notifier
, raw_smp_processor_id());
2677 fire_sched_out_preempt_notifiers(struct task_struct
*curr
,
2678 struct task_struct
*next
)
2680 struct preempt_notifier
*notifier
;
2681 struct hlist_node
*node
;
2683 hlist_for_each_entry(notifier
, node
, &curr
->preempt_notifiers
, link
)
2684 notifier
->ops
->sched_out(notifier
, next
);
2689 static void fire_sched_in_preempt_notifiers(struct task_struct
*curr
)
2694 fire_sched_out_preempt_notifiers(struct task_struct
*curr
,
2695 struct task_struct
*next
)
2702 * prepare_task_switch - prepare to switch tasks
2703 * @rq: the runqueue preparing to switch
2704 * @prev: the current task that is being switched out
2705 * @next: the task we are going to switch to.
2707 * This is called with the rq lock held and interrupts off. It must
2708 * be paired with a subsequent finish_task_switch after the context
2711 * prepare_task_switch sets up locking and calls architecture specific
2715 prepare_task_switch(struct rq
*rq
, struct task_struct
*prev
,
2716 struct task_struct
*next
)
2718 fire_sched_out_preempt_notifiers(prev
, next
);
2719 prepare_lock_switch(rq
, next
);
2720 prepare_arch_switch(next
);
2724 * finish_task_switch - clean up after a task-switch
2725 * @rq: runqueue associated with task-switch
2726 * @prev: the thread we just switched away from.
2728 * finish_task_switch must be called after the context switch, paired
2729 * with a prepare_task_switch call before the context switch.
2730 * finish_task_switch will reconcile locking set up by prepare_task_switch,
2731 * and do any other architecture-specific cleanup actions.
2733 * Note that we may have delayed dropping an mm in context_switch(). If
2734 * so, we finish that here outside of the runqueue lock. (Doing it
2735 * with the lock held can cause deadlocks; see schedule() for
2738 static void finish_task_switch(struct rq
*rq
, struct task_struct
*prev
)
2739 __releases(rq
->lock
)
2741 struct mm_struct
*mm
= rq
->prev_mm
;
2747 * A task struct has one reference for the use as "current".
2748 * If a task dies, then it sets TASK_DEAD in tsk->state and calls
2749 * schedule one last time. The schedule call will never return, and
2750 * the scheduled task must drop that reference.
2751 * The test for TASK_DEAD must occur while the runqueue locks are
2752 * still held, otherwise prev could be scheduled on another cpu, die
2753 * there before we look at prev->state, and then the reference would
2755 * Manfred Spraul <manfred@colorfullife.com>
2757 prev_state
= prev
->state
;
2758 finish_arch_switch(prev
);
2759 finish_lock_switch(rq
, prev
);
2761 if (current
->sched_class
->post_schedule
)
2762 current
->sched_class
->post_schedule(rq
);
2765 fire_sched_in_preempt_notifiers(current
);
2768 if (unlikely(prev_state
== TASK_DEAD
)) {
2770 * Remove function-return probe instances associated with this
2771 * task and put them back on the free list.
2773 kprobe_flush_task(prev
);
2774 put_task_struct(prev
);
2779 * schedule_tail - first thing a freshly forked thread must call.
2780 * @prev: the thread we just switched away from.
2782 asmlinkage
void schedule_tail(struct task_struct
*prev
)
2783 __releases(rq
->lock
)
2785 struct rq
*rq
= this_rq();
2787 finish_task_switch(rq
, prev
);
2788 #ifdef __ARCH_WANT_UNLOCKED_CTXSW
2789 /* In this case, finish_task_switch does not reenable preemption */
2792 if (current
->set_child_tid
)
2793 put_user(task_pid_vnr(current
), current
->set_child_tid
);
2797 * context_switch - switch to the new MM and the new
2798 * thread's register state.
2801 context_switch(struct rq
*rq
, struct task_struct
*prev
,
2802 struct task_struct
*next
)
2804 struct mm_struct
*mm
, *oldmm
;
2806 prepare_task_switch(rq
, prev
, next
);
2807 ftrace_ctx_switch(rq
, prev
, next
);
2809 oldmm
= prev
->active_mm
;
2811 * For paravirt, this is coupled with an exit in switch_to to
2812 * combine the page table reload and the switch backend into
2815 arch_enter_lazy_cpu_mode();
2817 if (unlikely(!mm
)) {
2818 next
->active_mm
= oldmm
;
2819 atomic_inc(&oldmm
->mm_count
);
2820 enter_lazy_tlb(oldmm
, next
);
2822 switch_mm(oldmm
, mm
, next
);
2824 if (unlikely(!prev
->mm
)) {
2825 prev
->active_mm
= NULL
;
2826 rq
->prev_mm
= oldmm
;
2829 * Since the runqueue lock will be released by the next
2830 * task (which is an invalid locking op but in the case
2831 * of the scheduler it's an obvious special-case), so we
2832 * do an early lockdep release here:
2834 #ifndef __ARCH_WANT_UNLOCKED_CTXSW
2835 spin_release(&rq
->lock
.dep_map
, 1, _THIS_IP_
);
2838 /* Here we just switch the register state and the stack. */
2839 switch_to(prev
, next
, prev
);
2843 * this_rq must be evaluated again because prev may have moved
2844 * CPUs since it called schedule(), thus the 'rq' on its stack
2845 * frame will be invalid.
2847 finish_task_switch(this_rq(), prev
);
2851 * nr_running, nr_uninterruptible and nr_context_switches:
2853 * externally visible scheduler statistics: current number of runnable
2854 * threads, current number of uninterruptible-sleeping threads, total
2855 * number of context switches performed since bootup.
2857 unsigned long nr_running(void)
2859 unsigned long i
, sum
= 0;
2861 for_each_online_cpu(i
)
2862 sum
+= cpu_rq(i
)->nr_running
;
2867 unsigned long nr_uninterruptible(void)
2869 unsigned long i
, sum
= 0;
2871 for_each_possible_cpu(i
)
2872 sum
+= cpu_rq(i
)->nr_uninterruptible
;
2875 * Since we read the counters lockless, it might be slightly
2876 * inaccurate. Do not allow it to go below zero though:
2878 if (unlikely((long)sum
< 0))
2884 unsigned long long nr_context_switches(void)
2887 unsigned long long sum
= 0;
2889 for_each_possible_cpu(i
)
2890 sum
+= cpu_rq(i
)->nr_switches
;
2895 unsigned long nr_iowait(void)
2897 unsigned long i
, sum
= 0;
2899 for_each_possible_cpu(i
)
2900 sum
+= atomic_read(&cpu_rq(i
)->nr_iowait
);
2905 unsigned long nr_active(void)
2907 unsigned long i
, running
= 0, uninterruptible
= 0;
2909 for_each_online_cpu(i
) {
2910 running
+= cpu_rq(i
)->nr_running
;
2911 uninterruptible
+= cpu_rq(i
)->nr_uninterruptible
;
2914 if (unlikely((long)uninterruptible
< 0))
2915 uninterruptible
= 0;
2917 return running
+ uninterruptible
;
2921 * Update rq->cpu_load[] statistics. This function is usually called every
2922 * scheduler tick (TICK_NSEC).
2924 static void update_cpu_load(struct rq
*this_rq
)
2926 unsigned long this_load
= this_rq
->load
.weight
;
2929 this_rq
->nr_load_updates
++;
2931 /* Update our load: */
2932 for (i
= 0, scale
= 1; i
< CPU_LOAD_IDX_MAX
; i
++, scale
+= scale
) {
2933 unsigned long old_load
, new_load
;
2935 /* scale is effectively 1 << i now, and >> i divides by scale */
2937 old_load
= this_rq
->cpu_load
[i
];
2938 new_load
= this_load
;
2940 * Round up the averaging division if load is increasing. This
2941 * prevents us from getting stuck on 9 if the load is 10, for
2944 if (new_load
> old_load
)
2945 new_load
+= scale
-1;
2946 this_rq
->cpu_load
[i
] = (old_load
*(scale
-1) + new_load
) >> i
;
2953 * double_rq_lock - safely lock two runqueues
2955 * Note this does not disable interrupts like task_rq_lock,
2956 * you need to do so manually before calling.
2958 static void double_rq_lock(struct rq
*rq1
, struct rq
*rq2
)
2959 __acquires(rq1
->lock
)
2960 __acquires(rq2
->lock
)
2962 BUG_ON(!irqs_disabled());
2964 spin_lock(&rq1
->lock
);
2965 __acquire(rq2
->lock
); /* Fake it out ;) */
2968 spin_lock(&rq1
->lock
);
2969 spin_lock(&rq2
->lock
);
2971 spin_lock(&rq2
->lock
);
2972 spin_lock(&rq1
->lock
);
2975 update_rq_clock(rq1
);
2976 update_rq_clock(rq2
);
2980 * double_rq_unlock - safely unlock two runqueues
2982 * Note this does not restore interrupts like task_rq_unlock,
2983 * you need to do so manually after calling.
2985 static void double_rq_unlock(struct rq
*rq1
, struct rq
*rq2
)
2986 __releases(rq1
->lock
)
2987 __releases(rq2
->lock
)
2989 spin_unlock(&rq1
->lock
);
2991 spin_unlock(&rq2
->lock
);
2993 __release(rq2
->lock
);
2997 * double_lock_balance - lock the busiest runqueue, this_rq is locked already.
2999 static int double_lock_balance(struct rq
*this_rq
, struct rq
*busiest
)
3000 __releases(this_rq
->lock
)
3001 __acquires(busiest
->lock
)
3002 __acquires(this_rq
->lock
)
3006 if (unlikely(!irqs_disabled())) {
3007 /* printk() doesn't work good under rq->lock */
3008 spin_unlock(&this_rq
->lock
);
3011 if (unlikely(!spin_trylock(&busiest
->lock
))) {
3012 if (busiest
< this_rq
) {
3013 spin_unlock(&this_rq
->lock
);
3014 spin_lock(&busiest
->lock
);
3015 spin_lock(&this_rq
->lock
);
3018 spin_lock(&busiest
->lock
);
3024 * If dest_cpu is allowed for this process, migrate the task to it.
3025 * This is accomplished by forcing the cpu_allowed mask to only
3026 * allow dest_cpu, which will force the cpu onto dest_cpu. Then
3027 * the cpu_allowed mask is restored.
3029 static void sched_migrate_task(struct task_struct
*p
, int dest_cpu
)
3031 struct migration_req req
;
3032 unsigned long flags
;
3035 rq
= task_rq_lock(p
, &flags
);
3036 if (!cpu_isset(dest_cpu
, p
->cpus_allowed
)
3037 || unlikely(cpu_is_offline(dest_cpu
)))
3040 /* force the process onto the specified CPU */
3041 if (migrate_task(p
, dest_cpu
, &req
)) {
3042 /* Need to wait for migration thread (might exit: take ref). */
3043 struct task_struct
*mt
= rq
->migration_thread
;
3045 get_task_struct(mt
);
3046 task_rq_unlock(rq
, &flags
);
3047 wake_up_process(mt
);
3048 put_task_struct(mt
);
3049 wait_for_completion(&req
.done
);
3054 task_rq_unlock(rq
, &flags
);
3058 * sched_exec - execve() is a valuable balancing opportunity, because at
3059 * this point the task has the smallest effective memory and cache footprint.
3061 void sched_exec(void)
3063 int new_cpu
, this_cpu
= get_cpu();
3064 new_cpu
= sched_balance_self(this_cpu
, SD_BALANCE_EXEC
);
3066 if (new_cpu
!= this_cpu
)
3067 sched_migrate_task(current
, new_cpu
);
3071 * pull_task - move a task from a remote runqueue to the local runqueue.
3072 * Both runqueues must be locked.
3074 static void pull_task(struct rq
*src_rq
, struct task_struct
*p
,
3075 struct rq
*this_rq
, int this_cpu
)
3077 deactivate_task(src_rq
, p
, 0);
3078 set_task_cpu(p
, this_cpu
);
3079 activate_task(this_rq
, p
, 0);
3081 * Note that idle threads have a prio of MAX_PRIO, for this test
3082 * to be always true for them.
3084 check_preempt_curr(this_rq
, p
);
3088 * can_migrate_task - may task p from runqueue rq be migrated to this_cpu?
3091 int can_migrate_task(struct task_struct
*p
, struct rq
*rq
, int this_cpu
,
3092 struct sched_domain
*sd
, enum cpu_idle_type idle
,
3096 * We do not migrate tasks that are:
3097 * 1) running (obviously), or
3098 * 2) cannot be migrated to this CPU due to cpus_allowed, or
3099 * 3) are cache-hot on their current CPU.
3101 if (!cpu_isset(this_cpu
, p
->cpus_allowed
)) {
3102 schedstat_inc(p
, se
.nr_failed_migrations_affine
);
3107 if (task_running(rq
, p
)) {
3108 schedstat_inc(p
, se
.nr_failed_migrations_running
);
3113 * Aggressive migration if:
3114 * 1) task is cache cold, or
3115 * 2) too many balance attempts have failed.
3118 if (!task_hot(p
, rq
->clock
, sd
) ||
3119 sd
->nr_balance_failed
> sd
->cache_nice_tries
) {
3120 #ifdef CONFIG_SCHEDSTATS
3121 if (task_hot(p
, rq
->clock
, sd
)) {
3122 schedstat_inc(sd
, lb_hot_gained
[idle
]);
3123 schedstat_inc(p
, se
.nr_forced_migrations
);
3129 if (task_hot(p
, rq
->clock
, sd
)) {
3130 schedstat_inc(p
, se
.nr_failed_migrations_hot
);
3136 static unsigned long
3137 balance_tasks(struct rq
*this_rq
, int this_cpu
, struct rq
*busiest
,
3138 unsigned long max_load_move
, struct sched_domain
*sd
,
3139 enum cpu_idle_type idle
, int *all_pinned
,
3140 int *this_best_prio
, struct rq_iterator
*iterator
)
3142 int loops
= 0, pulled
= 0, pinned
= 0, skip_for_load
;
3143 struct task_struct
*p
;
3144 long rem_load_move
= max_load_move
;
3146 if (max_load_move
== 0)
3152 * Start the load-balancing iterator:
3154 p
= iterator
->start(iterator
->arg
);
3156 if (!p
|| loops
++ > sysctl_sched_nr_migrate
)
3159 * To help distribute high priority tasks across CPUs we don't
3160 * skip a task if it will be the highest priority task (i.e. smallest
3161 * prio value) on its new queue regardless of its load weight
3163 skip_for_load
= (p
->se
.load
.weight
>> 1) > rem_load_move
+
3164 SCHED_LOAD_SCALE_FUZZ
;
3165 if ((skip_for_load
&& p
->prio
>= *this_best_prio
) ||
3166 !can_migrate_task(p
, busiest
, this_cpu
, sd
, idle
, &pinned
)) {
3167 p
= iterator
->next(iterator
->arg
);
3171 pull_task(busiest
, p
, this_rq
, this_cpu
);
3173 rem_load_move
-= p
->se
.load
.weight
;
3176 * We only want to steal up to the prescribed amount of weighted load.
3178 if (rem_load_move
> 0) {
3179 if (p
->prio
< *this_best_prio
)
3180 *this_best_prio
= p
->prio
;
3181 p
= iterator
->next(iterator
->arg
);
3186 * Right now, this is one of only two places pull_task() is called,
3187 * so we can safely collect pull_task() stats here rather than
3188 * inside pull_task().
3190 schedstat_add(sd
, lb_gained
[idle
], pulled
);
3193 *all_pinned
= pinned
;
3195 return max_load_move
- rem_load_move
;
3199 * move_tasks tries to move up to max_load_move weighted load from busiest to
3200 * this_rq, as part of a balancing operation within domain "sd".
3201 * Returns 1 if successful and 0 otherwise.
3203 * Called with both runqueues locked.
3205 static int move_tasks(struct rq
*this_rq
, int this_cpu
, struct rq
*busiest
,
3206 unsigned long max_load_move
,
3207 struct sched_domain
*sd
, enum cpu_idle_type idle
,
3210 const struct sched_class
*class = sched_class_highest
;
3211 unsigned long total_load_moved
= 0;
3212 int this_best_prio
= this_rq
->curr
->prio
;
3216 class->load_balance(this_rq
, this_cpu
, busiest
,
3217 max_load_move
- total_load_moved
,
3218 sd
, idle
, all_pinned
, &this_best_prio
);
3219 class = class->next
;
3220 } while (class && max_load_move
> total_load_moved
);
3222 return total_load_moved
> 0;
3226 iter_move_one_task(struct rq
*this_rq
, int this_cpu
, struct rq
*busiest
,
3227 struct sched_domain
*sd
, enum cpu_idle_type idle
,
3228 struct rq_iterator
*iterator
)
3230 struct task_struct
*p
= iterator
->start(iterator
->arg
);
3234 if (can_migrate_task(p
, busiest
, this_cpu
, sd
, idle
, &pinned
)) {
3235 pull_task(busiest
, p
, this_rq
, this_cpu
);
3237 * Right now, this is only the second place pull_task()
3238 * is called, so we can safely collect pull_task()
3239 * stats here rather than inside pull_task().
3241 schedstat_inc(sd
, lb_gained
[idle
]);
3245 p
= iterator
->next(iterator
->arg
);
3252 * move_one_task tries to move exactly one task from busiest to this_rq, as
3253 * part of active balancing operations within "domain".
3254 * Returns 1 if successful and 0 otherwise.
3256 * Called with both runqueues locked.
3258 static int move_one_task(struct rq
*this_rq
, int this_cpu
, struct rq
*busiest
,
3259 struct sched_domain
*sd
, enum cpu_idle_type idle
)
3261 const struct sched_class
*class;
3263 for (class = sched_class_highest
; class; class = class->next
)
3264 if (class->move_one_task(this_rq
, this_cpu
, busiest
, sd
, idle
))
3271 * find_busiest_group finds and returns the busiest CPU group within the
3272 * domain. It calculates and returns the amount of weighted load which
3273 * should be moved to restore balance via the imbalance parameter.
3275 static struct sched_group
*
3276 find_busiest_group(struct sched_domain
*sd
, int this_cpu
,
3277 unsigned long *imbalance
, enum cpu_idle_type idle
,
3278 int *sd_idle
, const cpumask_t
*cpus
, int *balance
)
3280 struct sched_group
*busiest
= NULL
, *this = NULL
, *group
= sd
->groups
;
3281 unsigned long max_load
, avg_load
, total_load
, this_load
, total_pwr
;
3282 unsigned long max_pull
;
3283 unsigned long busiest_load_per_task
, busiest_nr_running
;
3284 unsigned long this_load_per_task
, this_nr_running
;
3285 int load_idx
, group_imb
= 0;
3286 #if defined(CONFIG_SCHED_MC) || defined(CONFIG_SCHED_SMT)
3287 int power_savings_balance
= 1;
3288 unsigned long leader_nr_running
= 0, min_load_per_task
= 0;
3289 unsigned long min_nr_running
= ULONG_MAX
;
3290 struct sched_group
*group_min
= NULL
, *group_leader
= NULL
;
3293 max_load
= this_load
= total_load
= total_pwr
= 0;
3294 busiest_load_per_task
= busiest_nr_running
= 0;
3295 this_load_per_task
= this_nr_running
= 0;
3296 if (idle
== CPU_NOT_IDLE
)
3297 load_idx
= sd
->busy_idx
;
3298 else if (idle
== CPU_NEWLY_IDLE
)
3299 load_idx
= sd
->newidle_idx
;
3301 load_idx
= sd
->idle_idx
;
3304 unsigned long load
, group_capacity
, max_cpu_load
, min_cpu_load
;
3307 int __group_imb
= 0;
3308 unsigned int balance_cpu
= -1, first_idle_cpu
= 0;
3309 unsigned long sum_nr_running
, sum_weighted_load
;
3311 local_group
= cpu_isset(this_cpu
, group
->cpumask
);
3314 balance_cpu
= first_cpu(group
->cpumask
);
3316 /* Tally up the load of all CPUs in the group */
3317 sum_weighted_load
= sum_nr_running
= avg_load
= 0;
3319 min_cpu_load
= ~0UL;
3321 for_each_cpu_mask(i
, group
->cpumask
) {
3324 if (!cpu_isset(i
, *cpus
))
3329 if (*sd_idle
&& rq
->nr_running
)
3332 /* Bias balancing toward cpus of our domain */
3334 if (idle_cpu(i
) && !first_idle_cpu
) {
3339 load
= target_load(i
, load_idx
);
3341 load
= source_load(i
, load_idx
);
3342 if (load
> max_cpu_load
)
3343 max_cpu_load
= load
;
3344 if (min_cpu_load
> load
)
3345 min_cpu_load
= load
;
3349 sum_nr_running
+= rq
->nr_running
;
3350 sum_weighted_load
+= weighted_cpuload(i
);
3354 * First idle cpu or the first cpu(busiest) in this sched group
3355 * is eligible for doing load balancing at this and above
3356 * domains. In the newly idle case, we will allow all the cpu's
3357 * to do the newly idle load balance.
3359 if (idle
!= CPU_NEWLY_IDLE
&& local_group
&&
3360 balance_cpu
!= this_cpu
&& balance
) {
3365 total_load
+= avg_load
;
3366 total_pwr
+= group
->__cpu_power
;
3368 /* Adjust by relative CPU power of the group */
3369 avg_load
= sg_div_cpu_power(group
,
3370 avg_load
* SCHED_LOAD_SCALE
);
3372 if ((max_cpu_load
- min_cpu_load
) > SCHED_LOAD_SCALE
)
3375 group_capacity
= group
->__cpu_power
/ SCHED_LOAD_SCALE
;
3378 this_load
= avg_load
;
3380 this_nr_running
= sum_nr_running
;
3381 this_load_per_task
= sum_weighted_load
;
3382 } else if (avg_load
> max_load
&&
3383 (sum_nr_running
> group_capacity
|| __group_imb
)) {
3384 max_load
= avg_load
;
3386 busiest_nr_running
= sum_nr_running
;
3387 busiest_load_per_task
= sum_weighted_load
;
3388 group_imb
= __group_imb
;
3391 #if defined(CONFIG_SCHED_MC) || defined(CONFIG_SCHED_SMT)
3393 * Busy processors will not participate in power savings
3396 if (idle
== CPU_NOT_IDLE
||
3397 !(sd
->flags
& SD_POWERSAVINGS_BALANCE
))
3401 * If the local group is idle or completely loaded
3402 * no need to do power savings balance at this domain
3404 if (local_group
&& (this_nr_running
>= group_capacity
||
3406 power_savings_balance
= 0;
3409 * If a group is already running at full capacity or idle,
3410 * don't include that group in power savings calculations
3412 if (!power_savings_balance
|| sum_nr_running
>= group_capacity
3417 * Calculate the group which has the least non-idle load.
3418 * This is the group from where we need to pick up the load
3421 if ((sum_nr_running
< min_nr_running
) ||
3422 (sum_nr_running
== min_nr_running
&&
3423 first_cpu(group
->cpumask
) <
3424 first_cpu(group_min
->cpumask
))) {
3426 min_nr_running
= sum_nr_running
;
3427 min_load_per_task
= sum_weighted_load
/
3432 * Calculate the group which is almost near its
3433 * capacity but still has some space to pick up some load
3434 * from other group and save more power
3436 if (sum_nr_running
<= group_capacity
- 1) {
3437 if (sum_nr_running
> leader_nr_running
||
3438 (sum_nr_running
== leader_nr_running
&&
3439 first_cpu(group
->cpumask
) >
3440 first_cpu(group_leader
->cpumask
))) {
3441 group_leader
= group
;
3442 leader_nr_running
= sum_nr_running
;
3447 group
= group
->next
;
3448 } while (group
!= sd
->groups
);
3450 if (!busiest
|| this_load
>= max_load
|| busiest_nr_running
== 0)
3453 avg_load
= (SCHED_LOAD_SCALE
* total_load
) / total_pwr
;
3455 if (this_load
>= avg_load
||
3456 100*max_load
<= sd
->imbalance_pct
*this_load
)
3459 busiest_load_per_task
/= busiest_nr_running
;
3461 busiest_load_per_task
= min(busiest_load_per_task
, avg_load
);
3464 * We're trying to get all the cpus to the average_load, so we don't
3465 * want to push ourselves above the average load, nor do we wish to
3466 * reduce the max loaded cpu below the average load, as either of these
3467 * actions would just result in more rebalancing later, and ping-pong
3468 * tasks around. Thus we look for the minimum possible imbalance.
3469 * Negative imbalances (*we* are more loaded than anyone else) will
3470 * be counted as no imbalance for these purposes -- we can't fix that
3471 * by pulling tasks to us. Be careful of negative numbers as they'll
3472 * appear as very large values with unsigned longs.
3474 if (max_load
<= busiest_load_per_task
)
3478 * In the presence of smp nice balancing, certain scenarios can have
3479 * max load less than avg load(as we skip the groups at or below
3480 * its cpu_power, while calculating max_load..)
3482 if (max_load
< avg_load
) {
3484 goto small_imbalance
;
3487 /* Don't want to pull so many tasks that a group would go idle */
3488 max_pull
= min(max_load
- avg_load
, max_load
- busiest_load_per_task
);
3490 /* How much load to actually move to equalise the imbalance */
3491 *imbalance
= min(max_pull
* busiest
->__cpu_power
,
3492 (avg_load
- this_load
) * this->__cpu_power
)
3496 * if *imbalance is less than the average load per runnable task
3497 * there is no gaurantee that any tasks will be moved so we'll have
3498 * a think about bumping its value to force at least one task to be
3501 if (*imbalance
< busiest_load_per_task
) {
3502 unsigned long tmp
, pwr_now
, pwr_move
;
3506 pwr_move
= pwr_now
= 0;
3508 if (this_nr_running
) {
3509 this_load_per_task
/= this_nr_running
;
3510 if (busiest_load_per_task
> this_load_per_task
)
3513 this_load_per_task
= SCHED_LOAD_SCALE
;
3515 if (max_load
- this_load
+ SCHED_LOAD_SCALE_FUZZ
>=
3516 busiest_load_per_task
* imbn
) {
3517 *imbalance
= busiest_load_per_task
;
3522 * OK, we don't have enough imbalance to justify moving tasks,
3523 * however we may be able to increase total CPU power used by
3527 pwr_now
+= busiest
->__cpu_power
*
3528 min(busiest_load_per_task
, max_load
);
3529 pwr_now
+= this->__cpu_power
*
3530 min(this_load_per_task
, this_load
);
3531 pwr_now
/= SCHED_LOAD_SCALE
;
3533 /* Amount of load we'd subtract */
3534 tmp
= sg_div_cpu_power(busiest
,
3535 busiest_load_per_task
* SCHED_LOAD_SCALE
);
3537 pwr_move
+= busiest
->__cpu_power
*
3538 min(busiest_load_per_task
, max_load
- tmp
);
3540 /* Amount of load we'd add */
3541 if (max_load
* busiest
->__cpu_power
<
3542 busiest_load_per_task
* SCHED_LOAD_SCALE
)
3543 tmp
= sg_div_cpu_power(this,
3544 max_load
* busiest
->__cpu_power
);
3546 tmp
= sg_div_cpu_power(this,
3547 busiest_load_per_task
* SCHED_LOAD_SCALE
);
3548 pwr_move
+= this->__cpu_power
*
3549 min(this_load_per_task
, this_load
+ tmp
);
3550 pwr_move
/= SCHED_LOAD_SCALE
;
3552 /* Move if we gain throughput */
3553 if (pwr_move
> pwr_now
)
3554 *imbalance
= busiest_load_per_task
;
3560 #if defined(CONFIG_SCHED_MC) || defined(CONFIG_SCHED_SMT)
3561 if (idle
== CPU_NOT_IDLE
|| !(sd
->flags
& SD_POWERSAVINGS_BALANCE
))
3564 if (this == group_leader
&& group_leader
!= group_min
) {
3565 *imbalance
= min_load_per_task
;
3575 * find_busiest_queue - find the busiest runqueue among the cpus in group.
3578 find_busiest_queue(struct sched_group
*group
, enum cpu_idle_type idle
,
3579 unsigned long imbalance
, const cpumask_t
*cpus
)
3581 struct rq
*busiest
= NULL
, *rq
;
3582 unsigned long max_load
= 0;
3585 for_each_cpu_mask(i
, group
->cpumask
) {
3588 if (!cpu_isset(i
, *cpus
))
3592 wl
= weighted_cpuload(i
);
3594 if (rq
->nr_running
== 1 && wl
> imbalance
)
3597 if (wl
> max_load
) {
3607 * Max backoff if we encounter pinned tasks. Pretty arbitrary value, but
3608 * so long as it is large enough.
3610 #define MAX_PINNED_INTERVAL 512
3613 * Check this_cpu to ensure it is balanced within domain. Attempt to move
3614 * tasks if there is an imbalance.
3616 static int load_balance(int this_cpu
, struct rq
*this_rq
,
3617 struct sched_domain
*sd
, enum cpu_idle_type idle
,
3618 int *balance
, cpumask_t
*cpus
)
3620 int ld_moved
, all_pinned
= 0, active_balance
= 0, sd_idle
= 0;
3621 struct sched_group
*group
;
3622 unsigned long imbalance
;
3624 unsigned long flags
;
3625 int unlock_aggregate
;
3629 unlock_aggregate
= get_aggregate(sd
);
3632 * When power savings policy is enabled for the parent domain, idle
3633 * sibling can pick up load irrespective of busy siblings. In this case,
3634 * let the state of idle sibling percolate up as CPU_IDLE, instead of
3635 * portraying it as CPU_NOT_IDLE.
3637 if (idle
!= CPU_NOT_IDLE
&& sd
->flags
& SD_SHARE_CPUPOWER
&&
3638 !test_sd_parent(sd
, SD_POWERSAVINGS_BALANCE
))
3641 schedstat_inc(sd
, lb_count
[idle
]);
3644 group
= find_busiest_group(sd
, this_cpu
, &imbalance
, idle
, &sd_idle
,
3651 schedstat_inc(sd
, lb_nobusyg
[idle
]);
3655 busiest
= find_busiest_queue(group
, idle
, imbalance
, cpus
);
3657 schedstat_inc(sd
, lb_nobusyq
[idle
]);
3661 BUG_ON(busiest
== this_rq
);
3663 schedstat_add(sd
, lb_imbalance
[idle
], imbalance
);
3666 if (busiest
->nr_running
> 1) {
3668 * Attempt to move tasks. If find_busiest_group has found
3669 * an imbalance but busiest->nr_running <= 1, the group is
3670 * still unbalanced. ld_moved simply stays zero, so it is
3671 * correctly treated as an imbalance.
3673 local_irq_save(flags
);
3674 double_rq_lock(this_rq
, busiest
);
3675 ld_moved
= move_tasks(this_rq
, this_cpu
, busiest
,
3676 imbalance
, sd
, idle
, &all_pinned
);
3677 double_rq_unlock(this_rq
, busiest
);
3678 local_irq_restore(flags
);
3681 * some other cpu did the load balance for us.
3683 if (ld_moved
&& this_cpu
!= smp_processor_id())
3684 resched_cpu(this_cpu
);
3686 /* All tasks on this runqueue were pinned by CPU affinity */
3687 if (unlikely(all_pinned
)) {
3688 cpu_clear(cpu_of(busiest
), *cpus
);
3689 if (!cpus_empty(*cpus
))
3696 schedstat_inc(sd
, lb_failed
[idle
]);
3697 sd
->nr_balance_failed
++;
3699 if (unlikely(sd
->nr_balance_failed
> sd
->cache_nice_tries
+2)) {
3701 spin_lock_irqsave(&busiest
->lock
, flags
);
3703 /* don't kick the migration_thread, if the curr
3704 * task on busiest cpu can't be moved to this_cpu
3706 if (!cpu_isset(this_cpu
, busiest
->curr
->cpus_allowed
)) {
3707 spin_unlock_irqrestore(&busiest
->lock
, flags
);
3709 goto out_one_pinned
;
3712 if (!busiest
->active_balance
) {
3713 busiest
->active_balance
= 1;
3714 busiest
->push_cpu
= this_cpu
;
3717 spin_unlock_irqrestore(&busiest
->lock
, flags
);
3719 wake_up_process(busiest
->migration_thread
);
3722 * We've kicked active balancing, reset the failure
3725 sd
->nr_balance_failed
= sd
->cache_nice_tries
+1;
3728 sd
->nr_balance_failed
= 0;
3730 if (likely(!active_balance
)) {
3731 /* We were unbalanced, so reset the balancing interval */
3732 sd
->balance_interval
= sd
->min_interval
;
3735 * If we've begun active balancing, start to back off. This
3736 * case may not be covered by the all_pinned logic if there
3737 * is only 1 task on the busy runqueue (because we don't call
3740 if (sd
->balance_interval
< sd
->max_interval
)
3741 sd
->balance_interval
*= 2;
3744 if (!ld_moved
&& !sd_idle
&& sd
->flags
& SD_SHARE_CPUPOWER
&&
3745 !test_sd_parent(sd
, SD_POWERSAVINGS_BALANCE
))
3751 schedstat_inc(sd
, lb_balanced
[idle
]);
3753 sd
->nr_balance_failed
= 0;
3756 /* tune up the balancing interval */
3757 if ((all_pinned
&& sd
->balance_interval
< MAX_PINNED_INTERVAL
) ||
3758 (sd
->balance_interval
< sd
->max_interval
))
3759 sd
->balance_interval
*= 2;
3761 if (!sd_idle
&& sd
->flags
& SD_SHARE_CPUPOWER
&&
3762 !test_sd_parent(sd
, SD_POWERSAVINGS_BALANCE
))
3767 if (unlock_aggregate
)
3773 * Check this_cpu to ensure it is balanced within domain. Attempt to move
3774 * tasks if there is an imbalance.
3776 * Called from schedule when this_rq is about to become idle (CPU_NEWLY_IDLE).
3777 * this_rq is locked.
3780 load_balance_newidle(int this_cpu
, struct rq
*this_rq
, struct sched_domain
*sd
,
3783 struct sched_group
*group
;
3784 struct rq
*busiest
= NULL
;
3785 unsigned long imbalance
;
3793 * When power savings policy is enabled for the parent domain, idle
3794 * sibling can pick up load irrespective of busy siblings. In this case,
3795 * let the state of idle sibling percolate up as IDLE, instead of
3796 * portraying it as CPU_NOT_IDLE.
3798 if (sd
->flags
& SD_SHARE_CPUPOWER
&&
3799 !test_sd_parent(sd
, SD_POWERSAVINGS_BALANCE
))
3802 schedstat_inc(sd
, lb_count
[CPU_NEWLY_IDLE
]);
3804 group
= find_busiest_group(sd
, this_cpu
, &imbalance
, CPU_NEWLY_IDLE
,
3805 &sd_idle
, cpus
, NULL
);
3807 schedstat_inc(sd
, lb_nobusyg
[CPU_NEWLY_IDLE
]);
3811 busiest
= find_busiest_queue(group
, CPU_NEWLY_IDLE
, imbalance
, cpus
);
3813 schedstat_inc(sd
, lb_nobusyq
[CPU_NEWLY_IDLE
]);
3817 BUG_ON(busiest
== this_rq
);
3819 schedstat_add(sd
, lb_imbalance
[CPU_NEWLY_IDLE
], imbalance
);
3822 if (busiest
->nr_running
> 1) {
3823 /* Attempt to move tasks */
3824 double_lock_balance(this_rq
, busiest
);
3825 /* this_rq->clock is already updated */
3826 update_rq_clock(busiest
);
3827 ld_moved
= move_tasks(this_rq
, this_cpu
, busiest
,
3828 imbalance
, sd
, CPU_NEWLY_IDLE
,
3830 spin_unlock(&busiest
->lock
);
3832 if (unlikely(all_pinned
)) {
3833 cpu_clear(cpu_of(busiest
), *cpus
);
3834 if (!cpus_empty(*cpus
))
3840 schedstat_inc(sd
, lb_failed
[CPU_NEWLY_IDLE
]);
3841 if (!sd_idle
&& sd
->flags
& SD_SHARE_CPUPOWER
&&
3842 !test_sd_parent(sd
, SD_POWERSAVINGS_BALANCE
))
3845 sd
->nr_balance_failed
= 0;
3850 schedstat_inc(sd
, lb_balanced
[CPU_NEWLY_IDLE
]);
3851 if (!sd_idle
&& sd
->flags
& SD_SHARE_CPUPOWER
&&
3852 !test_sd_parent(sd
, SD_POWERSAVINGS_BALANCE
))
3854 sd
->nr_balance_failed
= 0;
3860 * idle_balance is called by schedule() if this_cpu is about to become
3861 * idle. Attempts to pull tasks from other CPUs.
3863 static void idle_balance(int this_cpu
, struct rq
*this_rq
)
3865 struct sched_domain
*sd
;
3866 int pulled_task
= -1;
3867 unsigned long next_balance
= jiffies
+ HZ
;
3870 for_each_domain(this_cpu
, sd
) {
3871 unsigned long interval
;
3873 if (!(sd
->flags
& SD_LOAD_BALANCE
))
3876 if (sd
->flags
& SD_BALANCE_NEWIDLE
)
3877 /* If we've pulled tasks over stop searching: */
3878 pulled_task
= load_balance_newidle(this_cpu
, this_rq
,
3881 interval
= msecs_to_jiffies(sd
->balance_interval
);
3882 if (time_after(next_balance
, sd
->last_balance
+ interval
))
3883 next_balance
= sd
->last_balance
+ interval
;
3887 if (pulled_task
|| time_after(jiffies
, this_rq
->next_balance
)) {
3889 * We are going idle. next_balance may be set based on
3890 * a busy processor. So reset next_balance.
3892 this_rq
->next_balance
= next_balance
;
3897 * active_load_balance is run by migration threads. It pushes running tasks
3898 * off the busiest CPU onto idle CPUs. It requires at least 1 task to be
3899 * running on each physical CPU where possible, and avoids physical /
3900 * logical imbalances.
3902 * Called with busiest_rq locked.
3904 static void active_load_balance(struct rq
*busiest_rq
, int busiest_cpu
)
3906 int target_cpu
= busiest_rq
->push_cpu
;
3907 struct sched_domain
*sd
;
3908 struct rq
*target_rq
;
3910 /* Is there any task to move? */
3911 if (busiest_rq
->nr_running
<= 1)
3914 target_rq
= cpu_rq(target_cpu
);
3917 * This condition is "impossible", if it occurs
3918 * we need to fix it. Originally reported by
3919 * Bjorn Helgaas on a 128-cpu setup.
3921 BUG_ON(busiest_rq
== target_rq
);
3923 /* move a task from busiest_rq to target_rq */
3924 double_lock_balance(busiest_rq
, target_rq
);
3925 update_rq_clock(busiest_rq
);
3926 update_rq_clock(target_rq
);
3928 /* Search for an sd spanning us and the target CPU. */
3929 for_each_domain(target_cpu
, sd
) {
3930 if ((sd
->flags
& SD_LOAD_BALANCE
) &&
3931 cpu_isset(busiest_cpu
, sd
->span
))
3936 schedstat_inc(sd
, alb_count
);
3938 if (move_one_task(target_rq
, target_cpu
, busiest_rq
,
3940 schedstat_inc(sd
, alb_pushed
);
3942 schedstat_inc(sd
, alb_failed
);
3944 spin_unlock(&target_rq
->lock
);
3949 atomic_t load_balancer
;
3951 } nohz ____cacheline_aligned
= {
3952 .load_balancer
= ATOMIC_INIT(-1),
3953 .cpu_mask
= CPU_MASK_NONE
,
3957 * This routine will try to nominate the ilb (idle load balancing)
3958 * owner among the cpus whose ticks are stopped. ilb owner will do the idle
3959 * load balancing on behalf of all those cpus. If all the cpus in the system
3960 * go into this tickless mode, then there will be no ilb owner (as there is
3961 * no need for one) and all the cpus will sleep till the next wakeup event
3964 * For the ilb owner, tick is not stopped. And this tick will be used
3965 * for idle load balancing. ilb owner will still be part of
3968 * While stopping the tick, this cpu will become the ilb owner if there
3969 * is no other owner. And will be the owner till that cpu becomes busy
3970 * or if all cpus in the system stop their ticks at which point
3971 * there is no need for ilb owner.
3973 * When the ilb owner becomes busy, it nominates another owner, during the
3974 * next busy scheduler_tick()
3976 int select_nohz_load_balancer(int stop_tick
)
3978 int cpu
= smp_processor_id();
3981 cpu_set(cpu
, nohz
.cpu_mask
);
3982 cpu_rq(cpu
)->in_nohz_recently
= 1;
3985 * If we are going offline and still the leader, give up!
3987 if (cpu_is_offline(cpu
) &&
3988 atomic_read(&nohz
.load_balancer
) == cpu
) {
3989 if (atomic_cmpxchg(&nohz
.load_balancer
, cpu
, -1) != cpu
)
3994 /* time for ilb owner also to sleep */
3995 if (cpus_weight(nohz
.cpu_mask
) == num_online_cpus()) {
3996 if (atomic_read(&nohz
.load_balancer
) == cpu
)
3997 atomic_set(&nohz
.load_balancer
, -1);
4001 if (atomic_read(&nohz
.load_balancer
) == -1) {
4002 /* make me the ilb owner */
4003 if (atomic_cmpxchg(&nohz
.load_balancer
, -1, cpu
) == -1)
4005 } else if (atomic_read(&nohz
.load_balancer
) == cpu
)
4008 if (!cpu_isset(cpu
, nohz
.cpu_mask
))
4011 cpu_clear(cpu
, nohz
.cpu_mask
);
4013 if (atomic_read(&nohz
.load_balancer
) == cpu
)
4014 if (atomic_cmpxchg(&nohz
.load_balancer
, cpu
, -1) != cpu
)
4021 static DEFINE_SPINLOCK(balancing
);
4024 * It checks each scheduling domain to see if it is due to be balanced,
4025 * and initiates a balancing operation if so.
4027 * Balancing parameters are set up in arch_init_sched_domains.
4029 static void rebalance_domains(int cpu
, enum cpu_idle_type idle
)
4032 struct rq
*rq
= cpu_rq(cpu
);
4033 unsigned long interval
;
4034 struct sched_domain
*sd
;
4035 /* Earliest time when we have to do rebalance again */
4036 unsigned long next_balance
= jiffies
+ 60*HZ
;
4037 int update_next_balance
= 0;
4040 for_each_domain(cpu
, sd
) {
4041 if (!(sd
->flags
& SD_LOAD_BALANCE
))
4044 interval
= sd
->balance_interval
;
4045 if (idle
!= CPU_IDLE
)
4046 interval
*= sd
->busy_factor
;
4048 /* scale ms to jiffies */
4049 interval
= msecs_to_jiffies(interval
);
4050 if (unlikely(!interval
))
4052 if (interval
> HZ
*NR_CPUS
/10)
4053 interval
= HZ
*NR_CPUS
/10;
4056 if (sd
->flags
& SD_SERIALIZE
) {
4057 if (!spin_trylock(&balancing
))
4061 if (time_after_eq(jiffies
, sd
->last_balance
+ interval
)) {
4062 if (load_balance(cpu
, rq
, sd
, idle
, &balance
, &tmp
)) {
4064 * We've pulled tasks over so either we're no
4065 * longer idle, or one of our SMT siblings is
4068 idle
= CPU_NOT_IDLE
;
4070 sd
->last_balance
= jiffies
;
4072 if (sd
->flags
& SD_SERIALIZE
)
4073 spin_unlock(&balancing
);
4075 if (time_after(next_balance
, sd
->last_balance
+ interval
)) {
4076 next_balance
= sd
->last_balance
+ interval
;
4077 update_next_balance
= 1;
4081 * Stop the load balance at this level. There is another
4082 * CPU in our sched group which is doing load balancing more
4090 * next_balance will be updated only when there is a need.
4091 * When the cpu is attached to null domain for ex, it will not be
4094 if (likely(update_next_balance
))
4095 rq
->next_balance
= next_balance
;
4099 * run_rebalance_domains is triggered when needed from the scheduler tick.
4100 * In CONFIG_NO_HZ case, the idle load balance owner will do the
4101 * rebalancing for all the cpus for whom scheduler ticks are stopped.
4103 static void run_rebalance_domains(struct softirq_action
*h
)
4105 int this_cpu
= smp_processor_id();
4106 struct rq
*this_rq
= cpu_rq(this_cpu
);
4107 enum cpu_idle_type idle
= this_rq
->idle_at_tick
?
4108 CPU_IDLE
: CPU_NOT_IDLE
;
4110 rebalance_domains(this_cpu
, idle
);
4114 * If this cpu is the owner for idle load balancing, then do the
4115 * balancing on behalf of the other idle cpus whose ticks are
4118 if (this_rq
->idle_at_tick
&&
4119 atomic_read(&nohz
.load_balancer
) == this_cpu
) {
4120 cpumask_t cpus
= nohz
.cpu_mask
;
4124 cpu_clear(this_cpu
, cpus
);
4125 for_each_cpu_mask(balance_cpu
, cpus
) {
4127 * If this cpu gets work to do, stop the load balancing
4128 * work being done for other cpus. Next load
4129 * balancing owner will pick it up.
4134 rebalance_domains(balance_cpu
, CPU_IDLE
);
4136 rq
= cpu_rq(balance_cpu
);
4137 if (time_after(this_rq
->next_balance
, rq
->next_balance
))
4138 this_rq
->next_balance
= rq
->next_balance
;
4145 * Trigger the SCHED_SOFTIRQ if it is time to do periodic load balancing.
4147 * In case of CONFIG_NO_HZ, this is the place where we nominate a new
4148 * idle load balancing owner or decide to stop the periodic load balancing,
4149 * if the whole system is idle.
4151 static inline void trigger_load_balance(struct rq
*rq
, int cpu
)
4155 * If we were in the nohz mode recently and busy at the current
4156 * scheduler tick, then check if we need to nominate new idle
4159 if (rq
->in_nohz_recently
&& !rq
->idle_at_tick
) {
4160 rq
->in_nohz_recently
= 0;
4162 if (atomic_read(&nohz
.load_balancer
) == cpu
) {
4163 cpu_clear(cpu
, nohz
.cpu_mask
);
4164 atomic_set(&nohz
.load_balancer
, -1);
4167 if (atomic_read(&nohz
.load_balancer
) == -1) {
4169 * simple selection for now: Nominate the
4170 * first cpu in the nohz list to be the next
4173 * TBD: Traverse the sched domains and nominate
4174 * the nearest cpu in the nohz.cpu_mask.
4176 int ilb
= first_cpu(nohz
.cpu_mask
);
4178 if (ilb
< nr_cpu_ids
)
4184 * If this cpu is idle and doing idle load balancing for all the
4185 * cpus with ticks stopped, is it time for that to stop?
4187 if (rq
->idle_at_tick
&& atomic_read(&nohz
.load_balancer
) == cpu
&&
4188 cpus_weight(nohz
.cpu_mask
) == num_online_cpus()) {
4194 * If this cpu is idle and the idle load balancing is done by
4195 * someone else, then no need raise the SCHED_SOFTIRQ
4197 if (rq
->idle_at_tick
&& atomic_read(&nohz
.load_balancer
) != cpu
&&
4198 cpu_isset(cpu
, nohz
.cpu_mask
))
4201 if (time_after_eq(jiffies
, rq
->next_balance
))
4202 raise_softirq(SCHED_SOFTIRQ
);
4205 #else /* CONFIG_SMP */
4208 * on UP we do not need to balance between CPUs:
4210 static inline void idle_balance(int cpu
, struct rq
*rq
)
4216 DEFINE_PER_CPU(struct kernel_stat
, kstat
);
4218 EXPORT_PER_CPU_SYMBOL(kstat
);
4221 * Return p->sum_exec_runtime plus any more ns on the sched_clock
4222 * that have not yet been banked in case the task is currently running.
4224 unsigned long long task_sched_runtime(struct task_struct
*p
)
4226 unsigned long flags
;
4230 rq
= task_rq_lock(p
, &flags
);
4231 ns
= p
->se
.sum_exec_runtime
;
4232 if (task_current(rq
, p
)) {
4233 update_rq_clock(rq
);
4234 delta_exec
= rq
->clock
- p
->se
.exec_start
;
4235 if ((s64
)delta_exec
> 0)
4238 task_rq_unlock(rq
, &flags
);
4244 * Account user cpu time to a process.
4245 * @p: the process that the cpu time gets accounted to
4246 * @cputime: the cpu time spent in user space since the last update
4248 void account_user_time(struct task_struct
*p
, cputime_t cputime
)
4250 struct cpu_usage_stat
*cpustat
= &kstat_this_cpu
.cpustat
;
4253 p
->utime
= cputime_add(p
->utime
, cputime
);
4255 /* Add user time to cpustat. */
4256 tmp
= cputime_to_cputime64(cputime
);
4257 if (TASK_NICE(p
) > 0)
4258 cpustat
->nice
= cputime64_add(cpustat
->nice
, tmp
);
4260 cpustat
->user
= cputime64_add(cpustat
->user
, tmp
);
4264 * Account guest cpu time to a process.
4265 * @p: the process that the cpu time gets accounted to
4266 * @cputime: the cpu time spent in virtual machine since the last update
4268 static void account_guest_time(struct task_struct
*p
, cputime_t cputime
)
4271 struct cpu_usage_stat
*cpustat
= &kstat_this_cpu
.cpustat
;
4273 tmp
= cputime_to_cputime64(cputime
);
4275 p
->utime
= cputime_add(p
->utime
, cputime
);
4276 p
->gtime
= cputime_add(p
->gtime
, cputime
);
4278 cpustat
->user
= cputime64_add(cpustat
->user
, tmp
);
4279 cpustat
->guest
= cputime64_add(cpustat
->guest
, tmp
);
4283 * Account scaled user cpu time to a process.
4284 * @p: the process that the cpu time gets accounted to
4285 * @cputime: the cpu time spent in user space since the last update
4287 void account_user_time_scaled(struct task_struct
*p
, cputime_t cputime
)
4289 p
->utimescaled
= cputime_add(p
->utimescaled
, cputime
);
4293 * Account system cpu time to a process.
4294 * @p: the process that the cpu time gets accounted to
4295 * @hardirq_offset: the offset to subtract from hardirq_count()
4296 * @cputime: the cpu time spent in kernel space since the last update
4298 void account_system_time(struct task_struct
*p
, int hardirq_offset
,
4301 struct cpu_usage_stat
*cpustat
= &kstat_this_cpu
.cpustat
;
4302 struct rq
*rq
= this_rq();
4305 if ((p
->flags
& PF_VCPU
) && (irq_count() - hardirq_offset
== 0)) {
4306 account_guest_time(p
, cputime
);
4310 p
->stime
= cputime_add(p
->stime
, cputime
);
4312 /* Add system time to cpustat. */
4313 tmp
= cputime_to_cputime64(cputime
);
4314 if (hardirq_count() - hardirq_offset
)
4315 cpustat
->irq
= cputime64_add(cpustat
->irq
, tmp
);
4316 else if (softirq_count())
4317 cpustat
->softirq
= cputime64_add(cpustat
->softirq
, tmp
);
4318 else if (p
!= rq
->idle
)
4319 cpustat
->system
= cputime64_add(cpustat
->system
, tmp
);
4320 else if (atomic_read(&rq
->nr_iowait
) > 0)
4321 cpustat
->iowait
= cputime64_add(cpustat
->iowait
, tmp
);
4323 cpustat
->idle
= cputime64_add(cpustat
->idle
, tmp
);
4324 /* Account for system time used */
4325 acct_update_integrals(p
);
4329 * Account scaled system cpu time to a process.
4330 * @p: the process that the cpu time gets accounted to
4331 * @hardirq_offset: the offset to subtract from hardirq_count()
4332 * @cputime: the cpu time spent in kernel space since the last update
4334 void account_system_time_scaled(struct task_struct
*p
, cputime_t cputime
)
4336 p
->stimescaled
= cputime_add(p
->stimescaled
, cputime
);
4340 * Account for involuntary wait time.
4341 * @p: the process from which the cpu time has been stolen
4342 * @steal: the cpu time spent in involuntary wait
4344 void account_steal_time(struct task_struct
*p
, cputime_t steal
)
4346 struct cpu_usage_stat
*cpustat
= &kstat_this_cpu
.cpustat
;
4347 cputime64_t tmp
= cputime_to_cputime64(steal
);
4348 struct rq
*rq
= this_rq();
4350 if (p
== rq
->idle
) {
4351 p
->stime
= cputime_add(p
->stime
, steal
);
4352 if (atomic_read(&rq
->nr_iowait
) > 0)
4353 cpustat
->iowait
= cputime64_add(cpustat
->iowait
, tmp
);
4355 cpustat
->idle
= cputime64_add(cpustat
->idle
, tmp
);
4357 cpustat
->steal
= cputime64_add(cpustat
->steal
, tmp
);
4361 * This function gets called by the timer code, with HZ frequency.
4362 * We call it with interrupts disabled.
4364 * It also gets called by the fork code, when changing the parent's
4367 void scheduler_tick(void)
4369 int cpu
= smp_processor_id();
4370 struct rq
*rq
= cpu_rq(cpu
);
4371 struct task_struct
*curr
= rq
->curr
;
4375 spin_lock(&rq
->lock
);
4376 update_rq_clock(rq
);
4377 update_cpu_load(rq
);
4378 curr
->sched_class
->task_tick(rq
, curr
, 0);
4379 spin_unlock(&rq
->lock
);
4382 rq
->idle_at_tick
= idle_cpu(cpu
);
4383 trigger_load_balance(rq
, cpu
);
4387 #if defined(CONFIG_PREEMPT) && (defined(CONFIG_DEBUG_PREEMPT) || \
4388 defined(CONFIG_PREEMPT_TRACER))
4390 static inline unsigned long get_parent_ip(unsigned long addr
)
4392 if (in_lock_functions(addr
)) {
4393 addr
= CALLER_ADDR2
;
4394 if (in_lock_functions(addr
))
4395 addr
= CALLER_ADDR3
;
4400 void __kprobes
add_preempt_count(int val
)
4402 #ifdef CONFIG_DEBUG_PREEMPT
4406 if (DEBUG_LOCKS_WARN_ON((preempt_count() < 0)))
4409 preempt_count() += val
;
4410 #ifdef CONFIG_DEBUG_PREEMPT
4412 * Spinlock count overflowing soon?
4414 DEBUG_LOCKS_WARN_ON((preempt_count() & PREEMPT_MASK
) >=
4417 if (preempt_count() == val
)
4418 trace_preempt_off(CALLER_ADDR0
, get_parent_ip(CALLER_ADDR1
));
4420 EXPORT_SYMBOL(add_preempt_count
);
4422 void __kprobes
sub_preempt_count(int val
)
4424 #ifdef CONFIG_DEBUG_PREEMPT
4428 if (DEBUG_LOCKS_WARN_ON(val
> preempt_count()))
4431 * Is the spinlock portion underflowing?
4433 if (DEBUG_LOCKS_WARN_ON((val
< PREEMPT_MASK
) &&
4434 !(preempt_count() & PREEMPT_MASK
)))
4438 if (preempt_count() == val
)
4439 trace_preempt_on(CALLER_ADDR0
, get_parent_ip(CALLER_ADDR1
));
4440 preempt_count() -= val
;
4442 EXPORT_SYMBOL(sub_preempt_count
);
4447 * Print scheduling while atomic bug:
4449 static noinline
void __schedule_bug(struct task_struct
*prev
)
4451 struct pt_regs
*regs
= get_irq_regs();
4453 printk(KERN_ERR
"BUG: scheduling while atomic: %s/%d/0x%08x\n",
4454 prev
->comm
, prev
->pid
, preempt_count());
4456 debug_show_held_locks(prev
);
4457 if (irqs_disabled())
4458 print_irqtrace_events(prev
);
4467 * Various schedule()-time debugging checks and statistics:
4469 static inline void schedule_debug(struct task_struct
*prev
)
4472 * Test if we are atomic. Since do_exit() needs to call into
4473 * schedule() atomically, we ignore that path for now.
4474 * Otherwise, whine if we are scheduling when we should not be.
4476 if (unlikely(in_atomic_preempt_off()) && unlikely(!prev
->exit_state
))
4477 __schedule_bug(prev
);
4479 profile_hit(SCHED_PROFILING
, __builtin_return_address(0));
4481 schedstat_inc(this_rq(), sched_count
);
4482 #ifdef CONFIG_SCHEDSTATS
4483 if (unlikely(prev
->lock_depth
>= 0)) {
4484 schedstat_inc(this_rq(), bkl_count
);
4485 schedstat_inc(prev
, sched_info
.bkl_count
);
4491 * Pick up the highest-prio task:
4493 static inline struct task_struct
*
4494 pick_next_task(struct rq
*rq
, struct task_struct
*prev
)
4496 const struct sched_class
*class;
4497 struct task_struct
*p
;
4500 * Optimization: we know that if all tasks are in
4501 * the fair class we can call that function directly:
4503 if (likely(rq
->nr_running
== rq
->cfs
.nr_running
)) {
4504 p
= fair_sched_class
.pick_next_task(rq
);
4509 class = sched_class_highest
;
4511 p
= class->pick_next_task(rq
);
4515 * Will never be NULL as the idle class always
4516 * returns a non-NULL p:
4518 class = class->next
;
4523 * schedule() is the main scheduler function.
4525 asmlinkage
void __sched
schedule(void)
4527 struct task_struct
*prev
, *next
;
4528 unsigned long *switch_count
;
4534 cpu
= smp_processor_id();
4538 switch_count
= &prev
->nivcsw
;
4540 release_kernel_lock(prev
);
4541 need_resched_nonpreemptible
:
4543 schedule_debug(prev
);
4548 * Do the rq-clock update outside the rq lock:
4550 local_irq_disable();
4551 update_rq_clock(rq
);
4552 spin_lock(&rq
->lock
);
4553 clear_tsk_need_resched(prev
);
4555 if (prev
->state
&& !(preempt_count() & PREEMPT_ACTIVE
)) {
4556 if (unlikely((prev
->state
& TASK_INTERRUPTIBLE
) &&
4557 signal_pending(prev
))) {
4558 prev
->state
= TASK_RUNNING
;
4560 deactivate_task(rq
, prev
, 1);
4562 switch_count
= &prev
->nvcsw
;
4566 if (prev
->sched_class
->pre_schedule
)
4567 prev
->sched_class
->pre_schedule(rq
, prev
);
4570 if (unlikely(!rq
->nr_running
))
4571 idle_balance(cpu
, rq
);
4573 prev
->sched_class
->put_prev_task(rq
, prev
);
4574 next
= pick_next_task(rq
, prev
);
4576 if (likely(prev
!= next
)) {
4577 sched_info_switch(prev
, next
);
4583 context_switch(rq
, prev
, next
); /* unlocks the rq */
4585 * the context switch might have flipped the stack from under
4586 * us, hence refresh the local variables.
4588 cpu
= smp_processor_id();
4591 spin_unlock_irq(&rq
->lock
);
4595 if (unlikely(reacquire_kernel_lock(current
) < 0))
4596 goto need_resched_nonpreemptible
;
4598 preempt_enable_no_resched();
4599 if (unlikely(test_thread_flag(TIF_NEED_RESCHED
)))
4602 EXPORT_SYMBOL(schedule
);
4604 #ifdef CONFIG_PREEMPT
4606 * this is the entry point to schedule() from in-kernel preemption
4607 * off of preempt_enable. Kernel preemptions off return from interrupt
4608 * occur there and call schedule directly.
4610 asmlinkage
void __sched
preempt_schedule(void)
4612 struct thread_info
*ti
= current_thread_info();
4615 * If there is a non-zero preempt_count or interrupts are disabled,
4616 * we do not want to preempt the current task. Just return..
4618 if (likely(ti
->preempt_count
|| irqs_disabled()))
4622 add_preempt_count(PREEMPT_ACTIVE
);
4624 sub_preempt_count(PREEMPT_ACTIVE
);
4627 * Check again in case we missed a preemption opportunity
4628 * between schedule and now.
4631 } while (unlikely(test_thread_flag(TIF_NEED_RESCHED
)));
4633 EXPORT_SYMBOL(preempt_schedule
);
4636 * this is the entry point to schedule() from kernel preemption
4637 * off of irq context.
4638 * Note, that this is called and return with irqs disabled. This will
4639 * protect us against recursive calling from irq.
4641 asmlinkage
void __sched
preempt_schedule_irq(void)
4643 struct thread_info
*ti
= current_thread_info();
4645 /* Catch callers which need to be fixed */
4646 BUG_ON(ti
->preempt_count
|| !irqs_disabled());
4649 add_preempt_count(PREEMPT_ACTIVE
);
4652 local_irq_disable();
4653 sub_preempt_count(PREEMPT_ACTIVE
);
4656 * Check again in case we missed a preemption opportunity
4657 * between schedule and now.
4660 } while (unlikely(test_thread_flag(TIF_NEED_RESCHED
)));
4663 #endif /* CONFIG_PREEMPT */
4665 int default_wake_function(wait_queue_t
*curr
, unsigned mode
, int sync
,
4668 return try_to_wake_up(curr
->private, mode
, sync
);
4670 EXPORT_SYMBOL(default_wake_function
);
4673 * The core wakeup function. Non-exclusive wakeups (nr_exclusive == 0) just
4674 * wake everything up. If it's an exclusive wakeup (nr_exclusive == small +ve
4675 * number) then we wake all the non-exclusive tasks and one exclusive task.
4677 * There are circumstances in which we can try to wake a task which has already
4678 * started to run but is not in state TASK_RUNNING. try_to_wake_up() returns
4679 * zero in this (rare) case, and we handle it by continuing to scan the queue.
4681 static void __wake_up_common(wait_queue_head_t
*q
, unsigned int mode
,
4682 int nr_exclusive
, int sync
, void *key
)
4684 wait_queue_t
*curr
, *next
;
4686 list_for_each_entry_safe(curr
, next
, &q
->task_list
, task_list
) {
4687 unsigned flags
= curr
->flags
;
4689 if (curr
->func(curr
, mode
, sync
, key
) &&
4690 (flags
& WQ_FLAG_EXCLUSIVE
) && !--nr_exclusive
)
4696 * __wake_up - wake up threads blocked on a waitqueue.
4698 * @mode: which threads
4699 * @nr_exclusive: how many wake-one or wake-many threads to wake up
4700 * @key: is directly passed to the wakeup function
4702 void __wake_up(wait_queue_head_t
*q
, unsigned int mode
,
4703 int nr_exclusive
, void *key
)
4705 unsigned long flags
;
4707 spin_lock_irqsave(&q
->lock
, flags
);
4708 __wake_up_common(q
, mode
, nr_exclusive
, 0, key
);
4709 spin_unlock_irqrestore(&q
->lock
, flags
);
4711 EXPORT_SYMBOL(__wake_up
);
4714 * Same as __wake_up but called with the spinlock in wait_queue_head_t held.
4716 void __wake_up_locked(wait_queue_head_t
*q
, unsigned int mode
)
4718 __wake_up_common(q
, mode
, 1, 0, NULL
);
4722 * __wake_up_sync - wake up threads blocked on a waitqueue.
4724 * @mode: which threads
4725 * @nr_exclusive: how many wake-one or wake-many threads to wake up
4727 * The sync wakeup differs that the waker knows that it will schedule
4728 * away soon, so while the target thread will be woken up, it will not
4729 * be migrated to another CPU - ie. the two threads are 'synchronized'
4730 * with each other. This can prevent needless bouncing between CPUs.
4732 * On UP it can prevent extra preemption.
4735 __wake_up_sync(wait_queue_head_t
*q
, unsigned int mode
, int nr_exclusive
)
4737 unsigned long flags
;
4743 if (unlikely(!nr_exclusive
))
4746 spin_lock_irqsave(&q
->lock
, flags
);
4747 __wake_up_common(q
, mode
, nr_exclusive
, sync
, NULL
);
4748 spin_unlock_irqrestore(&q
->lock
, flags
);
4750 EXPORT_SYMBOL_GPL(__wake_up_sync
); /* For internal use only */
4752 void complete(struct completion
*x
)
4754 unsigned long flags
;
4756 spin_lock_irqsave(&x
->wait
.lock
, flags
);
4758 __wake_up_common(&x
->wait
, TASK_NORMAL
, 1, 0, NULL
);
4759 spin_unlock_irqrestore(&x
->wait
.lock
, flags
);
4761 EXPORT_SYMBOL(complete
);
4763 void complete_all(struct completion
*x
)
4765 unsigned long flags
;
4767 spin_lock_irqsave(&x
->wait
.lock
, flags
);
4768 x
->done
+= UINT_MAX
/2;
4769 __wake_up_common(&x
->wait
, TASK_NORMAL
, 0, 0, NULL
);
4770 spin_unlock_irqrestore(&x
->wait
.lock
, flags
);
4772 EXPORT_SYMBOL(complete_all
);
4774 static inline long __sched
4775 do_wait_for_common(struct completion
*x
, long timeout
, int state
)
4778 DECLARE_WAITQUEUE(wait
, current
);
4780 wait
.flags
|= WQ_FLAG_EXCLUSIVE
;
4781 __add_wait_queue_tail(&x
->wait
, &wait
);
4783 if ((state
== TASK_INTERRUPTIBLE
&&
4784 signal_pending(current
)) ||
4785 (state
== TASK_KILLABLE
&&
4786 fatal_signal_pending(current
))) {
4787 __remove_wait_queue(&x
->wait
, &wait
);
4788 return -ERESTARTSYS
;
4790 __set_current_state(state
);
4791 spin_unlock_irq(&x
->wait
.lock
);
4792 timeout
= schedule_timeout(timeout
);
4793 spin_lock_irq(&x
->wait
.lock
);
4795 __remove_wait_queue(&x
->wait
, &wait
);
4799 __remove_wait_queue(&x
->wait
, &wait
);
4806 wait_for_common(struct completion
*x
, long timeout
, int state
)
4810 spin_lock_irq(&x
->wait
.lock
);
4811 timeout
= do_wait_for_common(x
, timeout
, state
);
4812 spin_unlock_irq(&x
->wait
.lock
);
4816 void __sched
wait_for_completion(struct completion
*x
)
4818 wait_for_common(x
, MAX_SCHEDULE_TIMEOUT
, TASK_UNINTERRUPTIBLE
);
4820 EXPORT_SYMBOL(wait_for_completion
);
4822 unsigned long __sched
4823 wait_for_completion_timeout(struct completion
*x
, unsigned long timeout
)
4825 return wait_for_common(x
, timeout
, TASK_UNINTERRUPTIBLE
);
4827 EXPORT_SYMBOL(wait_for_completion_timeout
);
4829 int __sched
wait_for_completion_interruptible(struct completion
*x
)
4831 long t
= wait_for_common(x
, MAX_SCHEDULE_TIMEOUT
, TASK_INTERRUPTIBLE
);
4832 if (t
== -ERESTARTSYS
)
4836 EXPORT_SYMBOL(wait_for_completion_interruptible
);
4838 unsigned long __sched
4839 wait_for_completion_interruptible_timeout(struct completion
*x
,
4840 unsigned long timeout
)
4842 return wait_for_common(x
, timeout
, TASK_INTERRUPTIBLE
);
4844 EXPORT_SYMBOL(wait_for_completion_interruptible_timeout
);
4846 int __sched
wait_for_completion_killable(struct completion
*x
)
4848 long t
= wait_for_common(x
, MAX_SCHEDULE_TIMEOUT
, TASK_KILLABLE
);
4849 if (t
== -ERESTARTSYS
)
4853 EXPORT_SYMBOL(wait_for_completion_killable
);
4856 sleep_on_common(wait_queue_head_t
*q
, int state
, long timeout
)
4858 unsigned long flags
;
4861 init_waitqueue_entry(&wait
, current
);
4863 __set_current_state(state
);
4865 spin_lock_irqsave(&q
->lock
, flags
);
4866 __add_wait_queue(q
, &wait
);
4867 spin_unlock(&q
->lock
);
4868 timeout
= schedule_timeout(timeout
);
4869 spin_lock_irq(&q
->lock
);
4870 __remove_wait_queue(q
, &wait
);
4871 spin_unlock_irqrestore(&q
->lock
, flags
);
4876 void __sched
interruptible_sleep_on(wait_queue_head_t
*q
)
4878 sleep_on_common(q
, TASK_INTERRUPTIBLE
, MAX_SCHEDULE_TIMEOUT
);
4880 EXPORT_SYMBOL(interruptible_sleep_on
);
4883 interruptible_sleep_on_timeout(wait_queue_head_t
*q
, long timeout
)
4885 return sleep_on_common(q
, TASK_INTERRUPTIBLE
, timeout
);
4887 EXPORT_SYMBOL(interruptible_sleep_on_timeout
);
4889 void __sched
sleep_on(wait_queue_head_t
*q
)
4891 sleep_on_common(q
, TASK_UNINTERRUPTIBLE
, MAX_SCHEDULE_TIMEOUT
);
4893 EXPORT_SYMBOL(sleep_on
);
4895 long __sched
sleep_on_timeout(wait_queue_head_t
*q
, long timeout
)
4897 return sleep_on_common(q
, TASK_UNINTERRUPTIBLE
, timeout
);
4899 EXPORT_SYMBOL(sleep_on_timeout
);
4901 #ifdef CONFIG_RT_MUTEXES
4904 * rt_mutex_setprio - set the current priority of a task
4906 * @prio: prio value (kernel-internal form)
4908 * This function changes the 'effective' priority of a task. It does
4909 * not touch ->normal_prio like __setscheduler().
4911 * Used by the rt_mutex code to implement priority inheritance logic.
4913 void rt_mutex_setprio(struct task_struct
*p
, int prio
)
4915 unsigned long flags
;
4916 int oldprio
, on_rq
, running
;
4918 const struct sched_class
*prev_class
= p
->sched_class
;
4920 BUG_ON(prio
< 0 || prio
> MAX_PRIO
);
4922 rq
= task_rq_lock(p
, &flags
);
4923 update_rq_clock(rq
);
4926 on_rq
= p
->se
.on_rq
;
4927 running
= task_current(rq
, p
);
4929 dequeue_task(rq
, p
, 0);
4931 p
->sched_class
->put_prev_task(rq
, p
);
4934 p
->sched_class
= &rt_sched_class
;
4936 p
->sched_class
= &fair_sched_class
;
4941 p
->sched_class
->set_curr_task(rq
);
4943 enqueue_task(rq
, p
, 0);
4945 check_class_changed(rq
, p
, prev_class
, oldprio
, running
);
4947 task_rq_unlock(rq
, &flags
);
4952 void set_user_nice(struct task_struct
*p
, long nice
)
4954 int old_prio
, delta
, on_rq
;
4955 unsigned long flags
;
4958 if (TASK_NICE(p
) == nice
|| nice
< -20 || nice
> 19)
4961 * We have to be careful, if called from sys_setpriority(),
4962 * the task might be in the middle of scheduling on another CPU.
4964 rq
= task_rq_lock(p
, &flags
);
4965 update_rq_clock(rq
);
4967 * The RT priorities are set via sched_setscheduler(), but we still
4968 * allow the 'normal' nice value to be set - but as expected
4969 * it wont have any effect on scheduling until the task is
4970 * SCHED_FIFO/SCHED_RR:
4972 if (task_has_rt_policy(p
)) {
4973 p
->static_prio
= NICE_TO_PRIO(nice
);
4976 on_rq
= p
->se
.on_rq
;
4978 dequeue_task(rq
, p
, 0);
4980 p
->static_prio
= NICE_TO_PRIO(nice
);
4983 p
->prio
= effective_prio(p
);
4984 delta
= p
->prio
- old_prio
;
4987 enqueue_task(rq
, p
, 0);
4989 * If the task increased its priority or is running and
4990 * lowered its priority, then reschedule its CPU:
4992 if (delta
< 0 || (delta
> 0 && task_running(rq
, p
)))
4993 resched_task(rq
->curr
);
4996 task_rq_unlock(rq
, &flags
);
4998 EXPORT_SYMBOL(set_user_nice
);
5001 * can_nice - check if a task can reduce its nice value
5005 int can_nice(const struct task_struct
*p
, const int nice
)
5007 /* convert nice value [19,-20] to rlimit style value [1,40] */
5008 int nice_rlim
= 20 - nice
;
5010 return (nice_rlim
<= p
->signal
->rlim
[RLIMIT_NICE
].rlim_cur
||
5011 capable(CAP_SYS_NICE
));
5014 #ifdef __ARCH_WANT_SYS_NICE
5017 * sys_nice - change the priority of the current process.
5018 * @increment: priority increment
5020 * sys_setpriority is a more generic, but much slower function that
5021 * does similar things.
5023 asmlinkage
long sys_nice(int increment
)
5028 * Setpriority might change our priority at the same moment.
5029 * We don't have to worry. Conceptually one call occurs first
5030 * and we have a single winner.
5032 if (increment
< -40)
5037 nice
= PRIO_TO_NICE(current
->static_prio
) + increment
;
5043 if (increment
< 0 && !can_nice(current
, nice
))
5046 retval
= security_task_setnice(current
, nice
);
5050 set_user_nice(current
, nice
);
5057 * task_prio - return the priority value of a given task.
5058 * @p: the task in question.
5060 * This is the priority value as seen by users in /proc.
5061 * RT tasks are offset by -200. Normal tasks are centered
5062 * around 0, value goes from -16 to +15.
5064 int task_prio(const struct task_struct
*p
)
5066 return p
->prio
- MAX_RT_PRIO
;
5070 * task_nice - return the nice value of a given task.
5071 * @p: the task in question.
5073 int task_nice(const struct task_struct
*p
)
5075 return TASK_NICE(p
);
5077 EXPORT_SYMBOL(task_nice
);
5080 * idle_cpu - is a given cpu idle currently?
5081 * @cpu: the processor in question.
5083 int idle_cpu(int cpu
)
5085 return cpu_curr(cpu
) == cpu_rq(cpu
)->idle
;
5089 * idle_task - return the idle task for a given cpu.
5090 * @cpu: the processor in question.
5092 struct task_struct
*idle_task(int cpu
)
5094 return cpu_rq(cpu
)->idle
;
5098 * find_process_by_pid - find a process with a matching PID value.
5099 * @pid: the pid in question.
5101 static struct task_struct
*find_process_by_pid(pid_t pid
)
5103 return pid
? find_task_by_vpid(pid
) : current
;
5106 /* Actually do priority change: must hold rq lock. */
5108 __setscheduler(struct rq
*rq
, struct task_struct
*p
, int policy
, int prio
)
5110 BUG_ON(p
->se
.on_rq
);
5113 switch (p
->policy
) {
5117 p
->sched_class
= &fair_sched_class
;
5121 p
->sched_class
= &rt_sched_class
;
5125 p
->rt_priority
= prio
;
5126 p
->normal_prio
= normal_prio(p
);
5127 /* we are holding p->pi_lock already */
5128 p
->prio
= rt_mutex_getprio(p
);
5133 * sched_setscheduler - change the scheduling policy and/or RT priority of a thread.
5134 * @p: the task in question.
5135 * @policy: new policy.
5136 * @param: structure containing the new RT priority.
5138 * NOTE that the task may be already dead.
5140 int sched_setscheduler(struct task_struct
*p
, int policy
,
5141 struct sched_param
*param
)
5143 int retval
, oldprio
, oldpolicy
= -1, on_rq
, running
;
5144 unsigned long flags
;
5145 const struct sched_class
*prev_class
= p
->sched_class
;
5148 /* may grab non-irq protected spin_locks */
5149 BUG_ON(in_interrupt());
5151 /* double check policy once rq lock held */
5153 policy
= oldpolicy
= p
->policy
;
5154 else if (policy
!= SCHED_FIFO
&& policy
!= SCHED_RR
&&
5155 policy
!= SCHED_NORMAL
&& policy
!= SCHED_BATCH
&&
5156 policy
!= SCHED_IDLE
)
5159 * Valid priorities for SCHED_FIFO and SCHED_RR are
5160 * 1..MAX_USER_RT_PRIO-1, valid priority for SCHED_NORMAL,
5161 * SCHED_BATCH and SCHED_IDLE is 0.
5163 if (param
->sched_priority
< 0 ||
5164 (p
->mm
&& param
->sched_priority
> MAX_USER_RT_PRIO
-1) ||
5165 (!p
->mm
&& param
->sched_priority
> MAX_RT_PRIO
-1))
5167 if (rt_policy(policy
) != (param
->sched_priority
!= 0))
5171 * Allow unprivileged RT tasks to decrease priority:
5173 if (!capable(CAP_SYS_NICE
)) {
5174 if (rt_policy(policy
)) {
5175 unsigned long rlim_rtprio
;
5177 if (!lock_task_sighand(p
, &flags
))
5179 rlim_rtprio
= p
->signal
->rlim
[RLIMIT_RTPRIO
].rlim_cur
;
5180 unlock_task_sighand(p
, &flags
);
5182 /* can't set/change the rt policy */
5183 if (policy
!= p
->policy
&& !rlim_rtprio
)
5186 /* can't increase priority */
5187 if (param
->sched_priority
> p
->rt_priority
&&
5188 param
->sched_priority
> rlim_rtprio
)
5192 * Like positive nice levels, dont allow tasks to
5193 * move out of SCHED_IDLE either:
5195 if (p
->policy
== SCHED_IDLE
&& policy
!= SCHED_IDLE
)
5198 /* can't change other user's priorities */
5199 if ((current
->euid
!= p
->euid
) &&
5200 (current
->euid
!= p
->uid
))
5204 #ifdef CONFIG_RT_GROUP_SCHED
5206 * Do not allow realtime tasks into groups that have no runtime
5209 if (rt_policy(policy
) && task_group(p
)->rt_bandwidth
.rt_runtime
== 0)
5213 retval
= security_task_setscheduler(p
, policy
, param
);
5217 * make sure no PI-waiters arrive (or leave) while we are
5218 * changing the priority of the task:
5220 spin_lock_irqsave(&p
->pi_lock
, flags
);
5222 * To be able to change p->policy safely, the apropriate
5223 * runqueue lock must be held.
5225 rq
= __task_rq_lock(p
);
5226 /* recheck policy now with rq lock held */
5227 if (unlikely(oldpolicy
!= -1 && oldpolicy
!= p
->policy
)) {
5228 policy
= oldpolicy
= -1;
5229 __task_rq_unlock(rq
);
5230 spin_unlock_irqrestore(&p
->pi_lock
, flags
);
5233 update_rq_clock(rq
);
5234 on_rq
= p
->se
.on_rq
;
5235 running
= task_current(rq
, p
);
5237 deactivate_task(rq
, p
, 0);
5239 p
->sched_class
->put_prev_task(rq
, p
);
5242 __setscheduler(rq
, p
, policy
, param
->sched_priority
);
5245 p
->sched_class
->set_curr_task(rq
);
5247 activate_task(rq
, p
, 0);
5249 check_class_changed(rq
, p
, prev_class
, oldprio
, running
);
5251 __task_rq_unlock(rq
);
5252 spin_unlock_irqrestore(&p
->pi_lock
, flags
);
5254 rt_mutex_adjust_pi(p
);
5258 EXPORT_SYMBOL_GPL(sched_setscheduler
);
5261 do_sched_setscheduler(pid_t pid
, int policy
, struct sched_param __user
*param
)
5263 struct sched_param lparam
;
5264 struct task_struct
*p
;
5267 if (!param
|| pid
< 0)
5269 if (copy_from_user(&lparam
, param
, sizeof(struct sched_param
)))
5274 p
= find_process_by_pid(pid
);
5276 retval
= sched_setscheduler(p
, policy
, &lparam
);
5283 * sys_sched_setscheduler - set/change the scheduler policy and RT priority
5284 * @pid: the pid in question.
5285 * @policy: new policy.
5286 * @param: structure containing the new RT priority.
5289 sys_sched_setscheduler(pid_t pid
, int policy
, struct sched_param __user
*param
)
5291 /* negative values for policy are not valid */
5295 return do_sched_setscheduler(pid
, policy
, param
);
5299 * sys_sched_setparam - set/change the RT priority of a thread
5300 * @pid: the pid in question.
5301 * @param: structure containing the new RT priority.
5303 asmlinkage
long sys_sched_setparam(pid_t pid
, struct sched_param __user
*param
)
5305 return do_sched_setscheduler(pid
, -1, param
);
5309 * sys_sched_getscheduler - get the policy (scheduling class) of a thread
5310 * @pid: the pid in question.
5312 asmlinkage
long sys_sched_getscheduler(pid_t pid
)
5314 struct task_struct
*p
;
5321 read_lock(&tasklist_lock
);
5322 p
= find_process_by_pid(pid
);
5324 retval
= security_task_getscheduler(p
);
5328 read_unlock(&tasklist_lock
);
5333 * sys_sched_getscheduler - get the RT priority of a thread
5334 * @pid: the pid in question.
5335 * @param: structure containing the RT priority.
5337 asmlinkage
long sys_sched_getparam(pid_t pid
, struct sched_param __user
*param
)
5339 struct sched_param lp
;
5340 struct task_struct
*p
;
5343 if (!param
|| pid
< 0)
5346 read_lock(&tasklist_lock
);
5347 p
= find_process_by_pid(pid
);
5352 retval
= security_task_getscheduler(p
);
5356 lp
.sched_priority
= p
->rt_priority
;
5357 read_unlock(&tasklist_lock
);
5360 * This one might sleep, we cannot do it with a spinlock held ...
5362 retval
= copy_to_user(param
, &lp
, sizeof(*param
)) ? -EFAULT
: 0;
5367 read_unlock(&tasklist_lock
);
5371 long sched_setaffinity(pid_t pid
, const cpumask_t
*in_mask
)
5373 cpumask_t cpus_allowed
;
5374 cpumask_t new_mask
= *in_mask
;
5375 struct task_struct
*p
;
5379 read_lock(&tasklist_lock
);
5381 p
= find_process_by_pid(pid
);
5383 read_unlock(&tasklist_lock
);
5389 * It is not safe to call set_cpus_allowed with the
5390 * tasklist_lock held. We will bump the task_struct's
5391 * usage count and then drop tasklist_lock.
5394 read_unlock(&tasklist_lock
);
5397 if ((current
->euid
!= p
->euid
) && (current
->euid
!= p
->uid
) &&
5398 !capable(CAP_SYS_NICE
))
5401 retval
= security_task_setscheduler(p
, 0, NULL
);
5405 cpuset_cpus_allowed(p
, &cpus_allowed
);
5406 cpus_and(new_mask
, new_mask
, cpus_allowed
);
5408 retval
= set_cpus_allowed_ptr(p
, &new_mask
);
5411 cpuset_cpus_allowed(p
, &cpus_allowed
);
5412 if (!cpus_subset(new_mask
, cpus_allowed
)) {
5414 * We must have raced with a concurrent cpuset
5415 * update. Just reset the cpus_allowed to the
5416 * cpuset's cpus_allowed
5418 new_mask
= cpus_allowed
;
5428 static int get_user_cpu_mask(unsigned long __user
*user_mask_ptr
, unsigned len
,
5429 cpumask_t
*new_mask
)
5431 if (len
< sizeof(cpumask_t
)) {
5432 memset(new_mask
, 0, sizeof(cpumask_t
));
5433 } else if (len
> sizeof(cpumask_t
)) {
5434 len
= sizeof(cpumask_t
);
5436 return copy_from_user(new_mask
, user_mask_ptr
, len
) ? -EFAULT
: 0;
5440 * sys_sched_setaffinity - set the cpu affinity of a process
5441 * @pid: pid of the process
5442 * @len: length in bytes of the bitmask pointed to by user_mask_ptr
5443 * @user_mask_ptr: user-space pointer to the new cpu mask
5445 asmlinkage
long sys_sched_setaffinity(pid_t pid
, unsigned int len
,
5446 unsigned long __user
*user_mask_ptr
)
5451 retval
= get_user_cpu_mask(user_mask_ptr
, len
, &new_mask
);
5455 return sched_setaffinity(pid
, &new_mask
);
5459 * Represents all cpu's present in the system
5460 * In systems capable of hotplug, this map could dynamically grow
5461 * as new cpu's are detected in the system via any platform specific
5462 * method, such as ACPI for e.g.
5465 cpumask_t cpu_present_map __read_mostly
;
5466 EXPORT_SYMBOL(cpu_present_map
);
5469 cpumask_t cpu_online_map __read_mostly
= CPU_MASK_ALL
;
5470 EXPORT_SYMBOL(cpu_online_map
);
5472 cpumask_t cpu_possible_map __read_mostly
= CPU_MASK_ALL
;
5473 EXPORT_SYMBOL(cpu_possible_map
);
5476 long sched_getaffinity(pid_t pid
, cpumask_t
*mask
)
5478 struct task_struct
*p
;
5482 read_lock(&tasklist_lock
);
5485 p
= find_process_by_pid(pid
);
5489 retval
= security_task_getscheduler(p
);
5493 cpus_and(*mask
, p
->cpus_allowed
, cpu_online_map
);
5496 read_unlock(&tasklist_lock
);
5503 * sys_sched_getaffinity - get the cpu affinity of a process
5504 * @pid: pid of the process
5505 * @len: length in bytes of the bitmask pointed to by user_mask_ptr
5506 * @user_mask_ptr: user-space pointer to hold the current cpu mask
5508 asmlinkage
long sys_sched_getaffinity(pid_t pid
, unsigned int len
,
5509 unsigned long __user
*user_mask_ptr
)
5514 if (len
< sizeof(cpumask_t
))
5517 ret
= sched_getaffinity(pid
, &mask
);
5521 if (copy_to_user(user_mask_ptr
, &mask
, sizeof(cpumask_t
)))
5524 return sizeof(cpumask_t
);
5528 * sys_sched_yield - yield the current processor to other threads.
5530 * This function yields the current CPU to other tasks. If there are no
5531 * other threads running on this CPU then this function will return.
5533 asmlinkage
long sys_sched_yield(void)
5535 struct rq
*rq
= this_rq_lock();
5537 schedstat_inc(rq
, yld_count
);
5538 current
->sched_class
->yield_task(rq
);
5541 * Since we are going to call schedule() anyway, there's
5542 * no need to preempt or enable interrupts:
5544 __release(rq
->lock
);
5545 spin_release(&rq
->lock
.dep_map
, 1, _THIS_IP_
);
5546 _raw_spin_unlock(&rq
->lock
);
5547 preempt_enable_no_resched();
5554 static void __cond_resched(void)
5556 #ifdef CONFIG_DEBUG_SPINLOCK_SLEEP
5557 __might_sleep(__FILE__
, __LINE__
);
5560 * The BKS might be reacquired before we have dropped
5561 * PREEMPT_ACTIVE, which could trigger a second
5562 * cond_resched() call.
5565 add_preempt_count(PREEMPT_ACTIVE
);
5567 sub_preempt_count(PREEMPT_ACTIVE
);
5568 } while (need_resched());
5571 int __sched
_cond_resched(void)
5573 if (need_resched() && !(preempt_count() & PREEMPT_ACTIVE
) &&
5574 system_state
== SYSTEM_RUNNING
) {
5580 EXPORT_SYMBOL(_cond_resched
);
5583 * cond_resched_lock() - if a reschedule is pending, drop the given lock,
5584 * call schedule, and on return reacquire the lock.
5586 * This works OK both with and without CONFIG_PREEMPT. We do strange low-level
5587 * operations here to prevent schedule() from being called twice (once via
5588 * spin_unlock(), once by hand).
5590 int cond_resched_lock(spinlock_t
*lock
)
5592 int resched
= need_resched() && system_state
== SYSTEM_RUNNING
;
5595 if (spin_needbreak(lock
) || resched
) {
5597 if (resched
&& need_resched())
5606 EXPORT_SYMBOL(cond_resched_lock
);
5608 int __sched
cond_resched_softirq(void)
5610 BUG_ON(!in_softirq());
5612 if (need_resched() && system_state
== SYSTEM_RUNNING
) {
5620 EXPORT_SYMBOL(cond_resched_softirq
);
5623 * yield - yield the current processor to other threads.
5625 * This is a shortcut for kernel-space yielding - it marks the
5626 * thread runnable and calls sys_sched_yield().
5628 void __sched
yield(void)
5630 set_current_state(TASK_RUNNING
);
5633 EXPORT_SYMBOL(yield
);
5636 * This task is about to go to sleep on IO. Increment rq->nr_iowait so
5637 * that process accounting knows that this is a task in IO wait state.
5639 * But don't do that if it is a deliberate, throttling IO wait (this task
5640 * has set its backing_dev_info: the queue against which it should throttle)
5642 void __sched
io_schedule(void)
5644 struct rq
*rq
= &__raw_get_cpu_var(runqueues
);
5646 delayacct_blkio_start();
5647 atomic_inc(&rq
->nr_iowait
);
5649 atomic_dec(&rq
->nr_iowait
);
5650 delayacct_blkio_end();
5652 EXPORT_SYMBOL(io_schedule
);
5654 long __sched
io_schedule_timeout(long timeout
)
5656 struct rq
*rq
= &__raw_get_cpu_var(runqueues
);
5659 delayacct_blkio_start();
5660 atomic_inc(&rq
->nr_iowait
);
5661 ret
= schedule_timeout(timeout
);
5662 atomic_dec(&rq
->nr_iowait
);
5663 delayacct_blkio_end();
5668 * sys_sched_get_priority_max - return maximum RT priority.
5669 * @policy: scheduling class.
5671 * this syscall returns the maximum rt_priority that can be used
5672 * by a given scheduling class.
5674 asmlinkage
long sys_sched_get_priority_max(int policy
)
5681 ret
= MAX_USER_RT_PRIO
-1;
5693 * sys_sched_get_priority_min - return minimum RT priority.
5694 * @policy: scheduling class.
5696 * this syscall returns the minimum rt_priority that can be used
5697 * by a given scheduling class.
5699 asmlinkage
long sys_sched_get_priority_min(int policy
)
5717 * sys_sched_rr_get_interval - return the default timeslice of a process.
5718 * @pid: pid of the process.
5719 * @interval: userspace pointer to the timeslice value.
5721 * this syscall writes the default timeslice value of a given process
5722 * into the user-space timespec buffer. A value of '0' means infinity.
5725 long sys_sched_rr_get_interval(pid_t pid
, struct timespec __user
*interval
)
5727 struct task_struct
*p
;
5728 unsigned int time_slice
;
5736 read_lock(&tasklist_lock
);
5737 p
= find_process_by_pid(pid
);
5741 retval
= security_task_getscheduler(p
);
5746 * Time slice is 0 for SCHED_FIFO tasks and for SCHED_OTHER
5747 * tasks that are on an otherwise idle runqueue:
5750 if (p
->policy
== SCHED_RR
) {
5751 time_slice
= DEF_TIMESLICE
;
5752 } else if (p
->policy
!= SCHED_FIFO
) {
5753 struct sched_entity
*se
= &p
->se
;
5754 unsigned long flags
;
5757 rq
= task_rq_lock(p
, &flags
);
5758 if (rq
->cfs
.load
.weight
)
5759 time_slice
= NS_TO_JIFFIES(sched_slice(&rq
->cfs
, se
));
5760 task_rq_unlock(rq
, &flags
);
5762 read_unlock(&tasklist_lock
);
5763 jiffies_to_timespec(time_slice
, &t
);
5764 retval
= copy_to_user(interval
, &t
, sizeof(t
)) ? -EFAULT
: 0;
5768 read_unlock(&tasklist_lock
);
5772 static const char stat_nam
[] = TASK_STATE_TO_CHAR_STR
;
5774 void sched_show_task(struct task_struct
*p
)
5776 unsigned long free
= 0;
5779 state
= p
->state
? __ffs(p
->state
) + 1 : 0;
5780 printk(KERN_INFO
"%-13.13s %c", p
->comm
,
5781 state
< sizeof(stat_nam
) - 1 ? stat_nam
[state
] : '?');
5782 #if BITS_PER_LONG == 32
5783 if (state
== TASK_RUNNING
)
5784 printk(KERN_CONT
" running ");
5786 printk(KERN_CONT
" %08lx ", thread_saved_pc(p
));
5788 if (state
== TASK_RUNNING
)
5789 printk(KERN_CONT
" running task ");
5791 printk(KERN_CONT
" %016lx ", thread_saved_pc(p
));
5793 #ifdef CONFIG_DEBUG_STACK_USAGE
5795 unsigned long *n
= end_of_stack(p
);
5798 free
= (unsigned long)n
- (unsigned long)end_of_stack(p
);
5801 printk(KERN_CONT
"%5lu %5d %6d\n", free
,
5802 task_pid_nr(p
), task_pid_nr(p
->real_parent
));
5804 show_stack(p
, NULL
);
5807 void show_state_filter(unsigned long state_filter
)
5809 struct task_struct
*g
, *p
;
5811 #if BITS_PER_LONG == 32
5813 " task PC stack pid father\n");
5816 " task PC stack pid father\n");
5818 read_lock(&tasklist_lock
);
5819 do_each_thread(g
, p
) {
5821 * reset the NMI-timeout, listing all files on a slow
5822 * console might take alot of time:
5824 touch_nmi_watchdog();
5825 if (!state_filter
|| (p
->state
& state_filter
))
5827 } while_each_thread(g
, p
);
5829 touch_all_softlockup_watchdogs();
5831 #ifdef CONFIG_SCHED_DEBUG
5832 sysrq_sched_debug_show();
5834 read_unlock(&tasklist_lock
);
5836 * Only show locks if all tasks are dumped:
5838 if (state_filter
== -1)
5839 debug_show_all_locks();
5842 void __cpuinit
init_idle_bootup_task(struct task_struct
*idle
)
5844 idle
->sched_class
= &idle_sched_class
;
5848 * init_idle - set up an idle thread for a given CPU
5849 * @idle: task in question
5850 * @cpu: cpu the idle task belongs to
5852 * NOTE: this function does not set the idle thread's NEED_RESCHED
5853 * flag, to make booting more robust.
5855 void __cpuinit
init_idle(struct task_struct
*idle
, int cpu
)
5857 struct rq
*rq
= cpu_rq(cpu
);
5858 unsigned long flags
;
5861 idle
->se
.exec_start
= sched_clock();
5863 idle
->prio
= idle
->normal_prio
= MAX_PRIO
;
5864 idle
->cpus_allowed
= cpumask_of_cpu(cpu
);
5865 __set_task_cpu(idle
, cpu
);
5867 spin_lock_irqsave(&rq
->lock
, flags
);
5868 rq
->curr
= rq
->idle
= idle
;
5869 #if defined(CONFIG_SMP) && defined(__ARCH_WANT_UNLOCKED_CTXSW)
5872 spin_unlock_irqrestore(&rq
->lock
, flags
);
5874 /* Set the preempt count _outside_ the spinlocks! */
5875 #if defined(CONFIG_PREEMPT)
5876 task_thread_info(idle
)->preempt_count
= (idle
->lock_depth
>= 0);
5878 task_thread_info(idle
)->preempt_count
= 0;
5881 * The idle tasks have their own, simple scheduling class:
5883 idle
->sched_class
= &idle_sched_class
;
5887 * In a system that switches off the HZ timer nohz_cpu_mask
5888 * indicates which cpus entered this state. This is used
5889 * in the rcu update to wait only for active cpus. For system
5890 * which do not switch off the HZ timer nohz_cpu_mask should
5891 * always be CPU_MASK_NONE.
5893 cpumask_t nohz_cpu_mask
= CPU_MASK_NONE
;
5896 * Increase the granularity value when there are more CPUs,
5897 * because with more CPUs the 'effective latency' as visible
5898 * to users decreases. But the relationship is not linear,
5899 * so pick a second-best guess by going with the log2 of the
5902 * This idea comes from the SD scheduler of Con Kolivas:
5904 static inline void sched_init_granularity(void)
5906 unsigned int factor
= 1 + ilog2(num_online_cpus());
5907 const unsigned long limit
= 200000000;
5909 sysctl_sched_min_granularity
*= factor
;
5910 if (sysctl_sched_min_granularity
> limit
)
5911 sysctl_sched_min_granularity
= limit
;
5913 sysctl_sched_latency
*= factor
;
5914 if (sysctl_sched_latency
> limit
)
5915 sysctl_sched_latency
= limit
;
5917 sysctl_sched_wakeup_granularity
*= factor
;
5922 * This is how migration works:
5924 * 1) we queue a struct migration_req structure in the source CPU's
5925 * runqueue and wake up that CPU's migration thread.
5926 * 2) we down() the locked semaphore => thread blocks.
5927 * 3) migration thread wakes up (implicitly it forces the migrated
5928 * thread off the CPU)
5929 * 4) it gets the migration request and checks whether the migrated
5930 * task is still in the wrong runqueue.
5931 * 5) if it's in the wrong runqueue then the migration thread removes
5932 * it and puts it into the right queue.
5933 * 6) migration thread up()s the semaphore.
5934 * 7) we wake up and the migration is done.
5938 * Change a given task's CPU affinity. Migrate the thread to a
5939 * proper CPU and schedule it away if the CPU it's executing on
5940 * is removed from the allowed bitmask.
5942 * NOTE: the caller must have a valid reference to the task, the
5943 * task must not exit() & deallocate itself prematurely. The
5944 * call is not atomic; no spinlocks may be held.
5946 int set_cpus_allowed_ptr(struct task_struct
*p
, const cpumask_t
*new_mask
)
5948 struct migration_req req
;
5949 unsigned long flags
;
5953 rq
= task_rq_lock(p
, &flags
);
5954 if (!cpus_intersects(*new_mask
, cpu_online_map
)) {
5959 if (p
->sched_class
->set_cpus_allowed
)
5960 p
->sched_class
->set_cpus_allowed(p
, new_mask
);
5962 p
->cpus_allowed
= *new_mask
;
5963 p
->rt
.nr_cpus_allowed
= cpus_weight(*new_mask
);
5966 /* Can the task run on the task's current CPU? If so, we're done */
5967 if (cpu_isset(task_cpu(p
), *new_mask
))
5970 if (migrate_task(p
, any_online_cpu(*new_mask
), &req
)) {
5971 /* Need help from migration thread: drop lock and wait. */
5972 task_rq_unlock(rq
, &flags
);
5973 wake_up_process(rq
->migration_thread
);
5974 wait_for_completion(&req
.done
);
5975 tlb_migrate_finish(p
->mm
);
5979 task_rq_unlock(rq
, &flags
);
5983 EXPORT_SYMBOL_GPL(set_cpus_allowed_ptr
);
5986 * Move (not current) task off this cpu, onto dest cpu. We're doing
5987 * this because either it can't run here any more (set_cpus_allowed()
5988 * away from this CPU, or CPU going down), or because we're
5989 * attempting to rebalance this task on exec (sched_exec).
5991 * So we race with normal scheduler movements, but that's OK, as long
5992 * as the task is no longer on this CPU.
5994 * Returns non-zero if task was successfully migrated.
5996 static int __migrate_task(struct task_struct
*p
, int src_cpu
, int dest_cpu
)
5998 struct rq
*rq_dest
, *rq_src
;
6001 if (unlikely(cpu_is_offline(dest_cpu
)))
6004 rq_src
= cpu_rq(src_cpu
);
6005 rq_dest
= cpu_rq(dest_cpu
);
6007 double_rq_lock(rq_src
, rq_dest
);
6008 /* Already moved. */
6009 if (task_cpu(p
) != src_cpu
)
6011 /* Affinity changed (again). */
6012 if (!cpu_isset(dest_cpu
, p
->cpus_allowed
))
6015 on_rq
= p
->se
.on_rq
;
6017 deactivate_task(rq_src
, p
, 0);
6019 set_task_cpu(p
, dest_cpu
);
6021 activate_task(rq_dest
, p
, 0);
6022 check_preempt_curr(rq_dest
, p
);
6026 double_rq_unlock(rq_src
, rq_dest
);
6031 * migration_thread - this is a highprio system thread that performs
6032 * thread migration by bumping thread off CPU then 'pushing' onto
6035 static int migration_thread(void *data
)
6037 int cpu
= (long)data
;
6041 BUG_ON(rq
->migration_thread
!= current
);
6043 set_current_state(TASK_INTERRUPTIBLE
);
6044 while (!kthread_should_stop()) {
6045 struct migration_req
*req
;
6046 struct list_head
*head
;
6048 spin_lock_irq(&rq
->lock
);
6050 if (cpu_is_offline(cpu
)) {
6051 spin_unlock_irq(&rq
->lock
);
6055 if (rq
->active_balance
) {
6056 active_load_balance(rq
, cpu
);
6057 rq
->active_balance
= 0;
6060 head
= &rq
->migration_queue
;
6062 if (list_empty(head
)) {
6063 spin_unlock_irq(&rq
->lock
);
6065 set_current_state(TASK_INTERRUPTIBLE
);
6068 req
= list_entry(head
->next
, struct migration_req
, list
);
6069 list_del_init(head
->next
);
6071 spin_unlock(&rq
->lock
);
6072 __migrate_task(req
->task
, cpu
, req
->dest_cpu
);
6075 complete(&req
->done
);
6077 __set_current_state(TASK_RUNNING
);
6081 /* Wait for kthread_stop */
6082 set_current_state(TASK_INTERRUPTIBLE
);
6083 while (!kthread_should_stop()) {
6085 set_current_state(TASK_INTERRUPTIBLE
);
6087 __set_current_state(TASK_RUNNING
);
6091 #ifdef CONFIG_HOTPLUG_CPU
6093 static int __migrate_task_irq(struct task_struct
*p
, int src_cpu
, int dest_cpu
)
6097 local_irq_disable();
6098 ret
= __migrate_task(p
, src_cpu
, dest_cpu
);
6104 * Figure out where task on dead CPU should go, use force if necessary.
6105 * NOTE: interrupts should be disabled by the caller
6107 static void move_task_off_dead_cpu(int dead_cpu
, struct task_struct
*p
)
6109 unsigned long flags
;
6116 mask
= node_to_cpumask(cpu_to_node(dead_cpu
));
6117 cpus_and(mask
, mask
, p
->cpus_allowed
);
6118 dest_cpu
= any_online_cpu(mask
);
6120 /* On any allowed CPU? */
6121 if (dest_cpu
>= nr_cpu_ids
)
6122 dest_cpu
= any_online_cpu(p
->cpus_allowed
);
6124 /* No more Mr. Nice Guy. */
6125 if (dest_cpu
>= nr_cpu_ids
) {
6126 cpumask_t cpus_allowed
;
6128 cpuset_cpus_allowed_locked(p
, &cpus_allowed
);
6130 * Try to stay on the same cpuset, where the
6131 * current cpuset may be a subset of all cpus.
6132 * The cpuset_cpus_allowed_locked() variant of
6133 * cpuset_cpus_allowed() will not block. It must be
6134 * called within calls to cpuset_lock/cpuset_unlock.
6136 rq
= task_rq_lock(p
, &flags
);
6137 p
->cpus_allowed
= cpus_allowed
;
6138 dest_cpu
= any_online_cpu(p
->cpus_allowed
);
6139 task_rq_unlock(rq
, &flags
);
6142 * Don't tell them about moving exiting tasks or
6143 * kernel threads (both mm NULL), since they never
6146 if (p
->mm
&& printk_ratelimit()) {
6147 printk(KERN_INFO
"process %d (%s) no "
6148 "longer affine to cpu%d\n",
6149 task_pid_nr(p
), p
->comm
, dead_cpu
);
6152 } while (!__migrate_task_irq(p
, dead_cpu
, dest_cpu
));
6156 * While a dead CPU has no uninterruptible tasks queued at this point,
6157 * it might still have a nonzero ->nr_uninterruptible counter, because
6158 * for performance reasons the counter is not stricly tracking tasks to
6159 * their home CPUs. So we just add the counter to another CPU's counter,
6160 * to keep the global sum constant after CPU-down:
6162 static void migrate_nr_uninterruptible(struct rq
*rq_src
)
6164 struct rq
*rq_dest
= cpu_rq(any_online_cpu(*CPU_MASK_ALL_PTR
));
6165 unsigned long flags
;
6167 local_irq_save(flags
);
6168 double_rq_lock(rq_src
, rq_dest
);
6169 rq_dest
->nr_uninterruptible
+= rq_src
->nr_uninterruptible
;
6170 rq_src
->nr_uninterruptible
= 0;
6171 double_rq_unlock(rq_src
, rq_dest
);
6172 local_irq_restore(flags
);
6175 /* Run through task list and migrate tasks from the dead cpu. */
6176 static void migrate_live_tasks(int src_cpu
)
6178 struct task_struct
*p
, *t
;
6180 read_lock(&tasklist_lock
);
6182 do_each_thread(t
, p
) {
6186 if (task_cpu(p
) == src_cpu
)
6187 move_task_off_dead_cpu(src_cpu
, p
);
6188 } while_each_thread(t
, p
);
6190 read_unlock(&tasklist_lock
);
6194 * Schedules idle task to be the next runnable task on current CPU.
6195 * It does so by boosting its priority to highest possible.
6196 * Used by CPU offline code.
6198 void sched_idle_next(void)
6200 int this_cpu
= smp_processor_id();
6201 struct rq
*rq
= cpu_rq(this_cpu
);
6202 struct task_struct
*p
= rq
->idle
;
6203 unsigned long flags
;
6205 /* cpu has to be offline */
6206 BUG_ON(cpu_online(this_cpu
));
6209 * Strictly not necessary since rest of the CPUs are stopped by now
6210 * and interrupts disabled on the current cpu.
6212 spin_lock_irqsave(&rq
->lock
, flags
);
6214 __setscheduler(rq
, p
, SCHED_FIFO
, MAX_RT_PRIO
-1);
6216 update_rq_clock(rq
);
6217 activate_task(rq
, p
, 0);
6219 spin_unlock_irqrestore(&rq
->lock
, flags
);
6223 * Ensures that the idle task is using init_mm right before its cpu goes
6226 void idle_task_exit(void)
6228 struct mm_struct
*mm
= current
->active_mm
;
6230 BUG_ON(cpu_online(smp_processor_id()));
6233 switch_mm(mm
, &init_mm
, current
);
6237 /* called under rq->lock with disabled interrupts */
6238 static void migrate_dead(unsigned int dead_cpu
, struct task_struct
*p
)
6240 struct rq
*rq
= cpu_rq(dead_cpu
);
6242 /* Must be exiting, otherwise would be on tasklist. */
6243 BUG_ON(!p
->exit_state
);
6245 /* Cannot have done final schedule yet: would have vanished. */
6246 BUG_ON(p
->state
== TASK_DEAD
);
6251 * Drop lock around migration; if someone else moves it,
6252 * that's OK. No task can be added to this CPU, so iteration is
6255 spin_unlock_irq(&rq
->lock
);
6256 move_task_off_dead_cpu(dead_cpu
, p
);
6257 spin_lock_irq(&rq
->lock
);
6262 /* release_task() removes task from tasklist, so we won't find dead tasks. */
6263 static void migrate_dead_tasks(unsigned int dead_cpu
)
6265 struct rq
*rq
= cpu_rq(dead_cpu
);
6266 struct task_struct
*next
;
6269 if (!rq
->nr_running
)
6271 update_rq_clock(rq
);
6272 next
= pick_next_task(rq
, rq
->curr
);
6275 migrate_dead(dead_cpu
, next
);
6279 #endif /* CONFIG_HOTPLUG_CPU */
6281 #if defined(CONFIG_SCHED_DEBUG) && defined(CONFIG_SYSCTL)
6283 static struct ctl_table sd_ctl_dir
[] = {
6285 .procname
= "sched_domain",
6291 static struct ctl_table sd_ctl_root
[] = {
6293 .ctl_name
= CTL_KERN
,
6294 .procname
= "kernel",
6296 .child
= sd_ctl_dir
,
6301 static struct ctl_table
*sd_alloc_ctl_entry(int n
)
6303 struct ctl_table
*entry
=
6304 kcalloc(n
, sizeof(struct ctl_table
), GFP_KERNEL
);
6309 static void sd_free_ctl_entry(struct ctl_table
**tablep
)
6311 struct ctl_table
*entry
;
6314 * In the intermediate directories, both the child directory and
6315 * procname are dynamically allocated and could fail but the mode
6316 * will always be set. In the lowest directory the names are
6317 * static strings and all have proc handlers.
6319 for (entry
= *tablep
; entry
->mode
; entry
++) {
6321 sd_free_ctl_entry(&entry
->child
);
6322 if (entry
->proc_handler
== NULL
)
6323 kfree(entry
->procname
);
6331 set_table_entry(struct ctl_table
*entry
,
6332 const char *procname
, void *data
, int maxlen
,
6333 mode_t mode
, proc_handler
*proc_handler
)
6335 entry
->procname
= procname
;
6337 entry
->maxlen
= maxlen
;
6339 entry
->proc_handler
= proc_handler
;
6342 static struct ctl_table
*
6343 sd_alloc_ctl_domain_table(struct sched_domain
*sd
)
6345 struct ctl_table
*table
= sd_alloc_ctl_entry(12);
6350 set_table_entry(&table
[0], "min_interval", &sd
->min_interval
,
6351 sizeof(long), 0644, proc_doulongvec_minmax
);
6352 set_table_entry(&table
[1], "max_interval", &sd
->max_interval
,
6353 sizeof(long), 0644, proc_doulongvec_minmax
);
6354 set_table_entry(&table
[2], "busy_idx", &sd
->busy_idx
,
6355 sizeof(int), 0644, proc_dointvec_minmax
);
6356 set_table_entry(&table
[3], "idle_idx", &sd
->idle_idx
,
6357 sizeof(int), 0644, proc_dointvec_minmax
);
6358 set_table_entry(&table
[4], "newidle_idx", &sd
->newidle_idx
,
6359 sizeof(int), 0644, proc_dointvec_minmax
);
6360 set_table_entry(&table
[5], "wake_idx", &sd
->wake_idx
,
6361 sizeof(int), 0644, proc_dointvec_minmax
);
6362 set_table_entry(&table
[6], "forkexec_idx", &sd
->forkexec_idx
,
6363 sizeof(int), 0644, proc_dointvec_minmax
);
6364 set_table_entry(&table
[7], "busy_factor", &sd
->busy_factor
,
6365 sizeof(int), 0644, proc_dointvec_minmax
);
6366 set_table_entry(&table
[8], "imbalance_pct", &sd
->imbalance_pct
,
6367 sizeof(int), 0644, proc_dointvec_minmax
);
6368 set_table_entry(&table
[9], "cache_nice_tries",
6369 &sd
->cache_nice_tries
,
6370 sizeof(int), 0644, proc_dointvec_minmax
);
6371 set_table_entry(&table
[10], "flags", &sd
->flags
,
6372 sizeof(int), 0644, proc_dointvec_minmax
);
6373 /* &table[11] is terminator */
6378 static ctl_table
*sd_alloc_ctl_cpu_table(int cpu
)
6380 struct ctl_table
*entry
, *table
;
6381 struct sched_domain
*sd
;
6382 int domain_num
= 0, i
;
6385 for_each_domain(cpu
, sd
)
6387 entry
= table
= sd_alloc_ctl_entry(domain_num
+ 1);
6392 for_each_domain(cpu
, sd
) {
6393 snprintf(buf
, 32, "domain%d", i
);
6394 entry
->procname
= kstrdup(buf
, GFP_KERNEL
);
6396 entry
->child
= sd_alloc_ctl_domain_table(sd
);
6403 static struct ctl_table_header
*sd_sysctl_header
;
6404 static void register_sched_domain_sysctl(void)
6406 int i
, cpu_num
= num_online_cpus();
6407 struct ctl_table
*entry
= sd_alloc_ctl_entry(cpu_num
+ 1);
6410 WARN_ON(sd_ctl_dir
[0].child
);
6411 sd_ctl_dir
[0].child
= entry
;
6416 for_each_online_cpu(i
) {
6417 snprintf(buf
, 32, "cpu%d", i
);
6418 entry
->procname
= kstrdup(buf
, GFP_KERNEL
);
6420 entry
->child
= sd_alloc_ctl_cpu_table(i
);
6424 WARN_ON(sd_sysctl_header
);
6425 sd_sysctl_header
= register_sysctl_table(sd_ctl_root
);
6428 /* may be called multiple times per register */
6429 static void unregister_sched_domain_sysctl(void)
6431 if (sd_sysctl_header
)
6432 unregister_sysctl_table(sd_sysctl_header
);
6433 sd_sysctl_header
= NULL
;
6434 if (sd_ctl_dir
[0].child
)
6435 sd_free_ctl_entry(&sd_ctl_dir
[0].child
);
6438 static void register_sched_domain_sysctl(void)
6441 static void unregister_sched_domain_sysctl(void)
6447 * migration_call - callback that gets triggered when a CPU is added.
6448 * Here we can start up the necessary migration thread for the new CPU.
6450 static int __cpuinit
6451 migration_call(struct notifier_block
*nfb
, unsigned long action
, void *hcpu
)
6453 struct task_struct
*p
;
6454 int cpu
= (long)hcpu
;
6455 unsigned long flags
;
6460 case CPU_UP_PREPARE
:
6461 case CPU_UP_PREPARE_FROZEN
:
6462 p
= kthread_create(migration_thread
, hcpu
, "migration/%d", cpu
);
6465 kthread_bind(p
, cpu
);
6466 /* Must be high prio: stop_machine expects to yield to it. */
6467 rq
= task_rq_lock(p
, &flags
);
6468 __setscheduler(rq
, p
, SCHED_FIFO
, MAX_RT_PRIO
-1);
6469 task_rq_unlock(rq
, &flags
);
6470 cpu_rq(cpu
)->migration_thread
= p
;
6474 case CPU_ONLINE_FROZEN
:
6475 /* Strictly unnecessary, as first user will wake it. */
6476 wake_up_process(cpu_rq(cpu
)->migration_thread
);
6478 /* Update our root-domain */
6480 spin_lock_irqsave(&rq
->lock
, flags
);
6482 BUG_ON(!cpu_isset(cpu
, rq
->rd
->span
));
6483 cpu_set(cpu
, rq
->rd
->online
);
6485 spin_unlock_irqrestore(&rq
->lock
, flags
);
6488 #ifdef CONFIG_HOTPLUG_CPU
6489 case CPU_UP_CANCELED
:
6490 case CPU_UP_CANCELED_FROZEN
:
6491 if (!cpu_rq(cpu
)->migration_thread
)
6493 /* Unbind it from offline cpu so it can run. Fall thru. */
6494 kthread_bind(cpu_rq(cpu
)->migration_thread
,
6495 any_online_cpu(cpu_online_map
));
6496 kthread_stop(cpu_rq(cpu
)->migration_thread
);
6497 cpu_rq(cpu
)->migration_thread
= NULL
;
6501 case CPU_DEAD_FROZEN
:
6502 cpuset_lock(); /* around calls to cpuset_cpus_allowed_lock() */
6503 migrate_live_tasks(cpu
);
6505 kthread_stop(rq
->migration_thread
);
6506 rq
->migration_thread
= NULL
;
6507 /* Idle task back to normal (off runqueue, low prio) */
6508 spin_lock_irq(&rq
->lock
);
6509 update_rq_clock(rq
);
6510 deactivate_task(rq
, rq
->idle
, 0);
6511 rq
->idle
->static_prio
= MAX_PRIO
;
6512 __setscheduler(rq
, rq
->idle
, SCHED_NORMAL
, 0);
6513 rq
->idle
->sched_class
= &idle_sched_class
;
6514 migrate_dead_tasks(cpu
);
6515 spin_unlock_irq(&rq
->lock
);
6517 migrate_nr_uninterruptible(rq
);
6518 BUG_ON(rq
->nr_running
!= 0);
6521 * No need to migrate the tasks: it was best-effort if
6522 * they didn't take sched_hotcpu_mutex. Just wake up
6525 spin_lock_irq(&rq
->lock
);
6526 while (!list_empty(&rq
->migration_queue
)) {
6527 struct migration_req
*req
;
6529 req
= list_entry(rq
->migration_queue
.next
,
6530 struct migration_req
, list
);
6531 list_del_init(&req
->list
);
6532 complete(&req
->done
);
6534 spin_unlock_irq(&rq
->lock
);
6538 case CPU_DYING_FROZEN
:
6539 /* Update our root-domain */
6541 spin_lock_irqsave(&rq
->lock
, flags
);
6543 BUG_ON(!cpu_isset(cpu
, rq
->rd
->span
));
6544 cpu_clear(cpu
, rq
->rd
->online
);
6546 spin_unlock_irqrestore(&rq
->lock
, flags
);
6553 /* Register at highest priority so that task migration (migrate_all_tasks)
6554 * happens before everything else.
6556 static struct notifier_block __cpuinitdata migration_notifier
= {
6557 .notifier_call
= migration_call
,
6561 void __init
migration_init(void)
6563 void *cpu
= (void *)(long)smp_processor_id();
6566 /* Start one for the boot CPU: */
6567 err
= migration_call(&migration_notifier
, CPU_UP_PREPARE
, cpu
);
6568 BUG_ON(err
== NOTIFY_BAD
);
6569 migration_call(&migration_notifier
, CPU_ONLINE
, cpu
);
6570 register_cpu_notifier(&migration_notifier
);
6576 #ifdef CONFIG_SCHED_DEBUG
6578 static int sched_domain_debug_one(struct sched_domain
*sd
, int cpu
, int level
,
6579 cpumask_t
*groupmask
)
6581 struct sched_group
*group
= sd
->groups
;
6584 cpulist_scnprintf(str
, sizeof(str
), sd
->span
);
6585 cpus_clear(*groupmask
);
6587 printk(KERN_DEBUG
"%*s domain %d: ", level
, "", level
);
6589 if (!(sd
->flags
& SD_LOAD_BALANCE
)) {
6590 printk("does not load-balance\n");
6592 printk(KERN_ERR
"ERROR: !SD_LOAD_BALANCE domain"
6597 printk(KERN_CONT
"span %s\n", str
);
6599 if (!cpu_isset(cpu
, sd
->span
)) {
6600 printk(KERN_ERR
"ERROR: domain->span does not contain "
6603 if (!cpu_isset(cpu
, group
->cpumask
)) {
6604 printk(KERN_ERR
"ERROR: domain->groups does not contain"
6608 printk(KERN_DEBUG
"%*s groups:", level
+ 1, "");
6612 printk(KERN_ERR
"ERROR: group is NULL\n");
6616 if (!group
->__cpu_power
) {
6617 printk(KERN_CONT
"\n");
6618 printk(KERN_ERR
"ERROR: domain->cpu_power not "
6623 if (!cpus_weight(group
->cpumask
)) {
6624 printk(KERN_CONT
"\n");
6625 printk(KERN_ERR
"ERROR: empty group\n");
6629 if (cpus_intersects(*groupmask
, group
->cpumask
)) {
6630 printk(KERN_CONT
"\n");
6631 printk(KERN_ERR
"ERROR: repeated CPUs\n");
6635 cpus_or(*groupmask
, *groupmask
, group
->cpumask
);
6637 cpulist_scnprintf(str
, sizeof(str
), group
->cpumask
);
6638 printk(KERN_CONT
" %s", str
);
6640 group
= group
->next
;
6641 } while (group
!= sd
->groups
);
6642 printk(KERN_CONT
"\n");
6644 if (!cpus_equal(sd
->span
, *groupmask
))
6645 printk(KERN_ERR
"ERROR: groups don't span domain->span\n");
6647 if (sd
->parent
&& !cpus_subset(*groupmask
, sd
->parent
->span
))
6648 printk(KERN_ERR
"ERROR: parent span is not a superset "
6649 "of domain->span\n");
6653 static void sched_domain_debug(struct sched_domain
*sd
, int cpu
)
6655 cpumask_t
*groupmask
;
6659 printk(KERN_DEBUG
"CPU%d attaching NULL sched-domain.\n", cpu
);
6663 printk(KERN_DEBUG
"CPU%d attaching sched-domain:\n", cpu
);
6665 groupmask
= kmalloc(sizeof(cpumask_t
), GFP_KERNEL
);
6667 printk(KERN_DEBUG
"Cannot load-balance (out of memory)\n");
6672 if (sched_domain_debug_one(sd
, cpu
, level
, groupmask
))
6682 # define sched_domain_debug(sd, cpu) do { } while (0)
6685 static int sd_degenerate(struct sched_domain
*sd
)
6687 if (cpus_weight(sd
->span
) == 1)
6690 /* Following flags need at least 2 groups */
6691 if (sd
->flags
& (SD_LOAD_BALANCE
|
6692 SD_BALANCE_NEWIDLE
|
6696 SD_SHARE_PKG_RESOURCES
)) {
6697 if (sd
->groups
!= sd
->groups
->next
)
6701 /* Following flags don't use groups */
6702 if (sd
->flags
& (SD_WAKE_IDLE
|
6711 sd_parent_degenerate(struct sched_domain
*sd
, struct sched_domain
*parent
)
6713 unsigned long cflags
= sd
->flags
, pflags
= parent
->flags
;
6715 if (sd_degenerate(parent
))
6718 if (!cpus_equal(sd
->span
, parent
->span
))
6721 /* Does parent contain flags not in child? */
6722 /* WAKE_BALANCE is a subset of WAKE_AFFINE */
6723 if (cflags
& SD_WAKE_AFFINE
)
6724 pflags
&= ~SD_WAKE_BALANCE
;
6725 /* Flags needing groups don't count if only 1 group in parent */
6726 if (parent
->groups
== parent
->groups
->next
) {
6727 pflags
&= ~(SD_LOAD_BALANCE
|
6728 SD_BALANCE_NEWIDLE
|
6732 SD_SHARE_PKG_RESOURCES
);
6734 if (~cflags
& pflags
)
6740 static void rq_attach_root(struct rq
*rq
, struct root_domain
*rd
)
6742 unsigned long flags
;
6743 const struct sched_class
*class;
6745 spin_lock_irqsave(&rq
->lock
, flags
);
6748 struct root_domain
*old_rd
= rq
->rd
;
6750 for (class = sched_class_highest
; class; class = class->next
) {
6751 if (class->leave_domain
)
6752 class->leave_domain(rq
);
6755 cpu_clear(rq
->cpu
, old_rd
->span
);
6756 cpu_clear(rq
->cpu
, old_rd
->online
);
6758 if (atomic_dec_and_test(&old_rd
->refcount
))
6762 atomic_inc(&rd
->refcount
);
6765 cpu_set(rq
->cpu
, rd
->span
);
6766 if (cpu_isset(rq
->cpu
, cpu_online_map
))
6767 cpu_set(rq
->cpu
, rd
->online
);
6769 for (class = sched_class_highest
; class; class = class->next
) {
6770 if (class->join_domain
)
6771 class->join_domain(rq
);
6774 spin_unlock_irqrestore(&rq
->lock
, flags
);
6777 static void init_rootdomain(struct root_domain
*rd
)
6779 memset(rd
, 0, sizeof(*rd
));
6781 cpus_clear(rd
->span
);
6782 cpus_clear(rd
->online
);
6785 static void init_defrootdomain(void)
6787 init_rootdomain(&def_root_domain
);
6788 atomic_set(&def_root_domain
.refcount
, 1);
6791 static struct root_domain
*alloc_rootdomain(void)
6793 struct root_domain
*rd
;
6795 rd
= kmalloc(sizeof(*rd
), GFP_KERNEL
);
6799 init_rootdomain(rd
);
6805 * Attach the domain 'sd' to 'cpu' as its base domain. Callers must
6806 * hold the hotplug lock.
6809 cpu_attach_domain(struct sched_domain
*sd
, struct root_domain
*rd
, int cpu
)
6811 struct rq
*rq
= cpu_rq(cpu
);
6812 struct sched_domain
*tmp
;
6814 /* Remove the sched domains which do not contribute to scheduling. */
6815 for (tmp
= sd
; tmp
; tmp
= tmp
->parent
) {
6816 struct sched_domain
*parent
= tmp
->parent
;
6819 if (sd_parent_degenerate(tmp
, parent
)) {
6820 tmp
->parent
= parent
->parent
;
6822 parent
->parent
->child
= tmp
;
6826 if (sd
&& sd_degenerate(sd
)) {
6832 sched_domain_debug(sd
, cpu
);
6834 rq_attach_root(rq
, rd
);
6835 rcu_assign_pointer(rq
->sd
, sd
);
6838 /* cpus with isolated domains */
6839 static cpumask_t cpu_isolated_map
= CPU_MASK_NONE
;
6841 /* Setup the mask of cpus configured for isolated domains */
6842 static int __init
isolated_cpu_setup(char *str
)
6844 int ints
[NR_CPUS
], i
;
6846 str
= get_options(str
, ARRAY_SIZE(ints
), ints
);
6847 cpus_clear(cpu_isolated_map
);
6848 for (i
= 1; i
<= ints
[0]; i
++)
6849 if (ints
[i
] < NR_CPUS
)
6850 cpu_set(ints
[i
], cpu_isolated_map
);
6854 __setup("isolcpus=", isolated_cpu_setup
);
6857 * init_sched_build_groups takes the cpumask we wish to span, and a pointer
6858 * to a function which identifies what group(along with sched group) a CPU
6859 * belongs to. The return value of group_fn must be a >= 0 and < NR_CPUS
6860 * (due to the fact that we keep track of groups covered with a cpumask_t).
6862 * init_sched_build_groups will build a circular linked list of the groups
6863 * covered by the given span, and will set each group's ->cpumask correctly,
6864 * and ->cpu_power to 0.
6867 init_sched_build_groups(const cpumask_t
*span
, const cpumask_t
*cpu_map
,
6868 int (*group_fn
)(int cpu
, const cpumask_t
*cpu_map
,
6869 struct sched_group
**sg
,
6870 cpumask_t
*tmpmask
),
6871 cpumask_t
*covered
, cpumask_t
*tmpmask
)
6873 struct sched_group
*first
= NULL
, *last
= NULL
;
6876 cpus_clear(*covered
);
6878 for_each_cpu_mask(i
, *span
) {
6879 struct sched_group
*sg
;
6880 int group
= group_fn(i
, cpu_map
, &sg
, tmpmask
);
6883 if (cpu_isset(i
, *covered
))
6886 cpus_clear(sg
->cpumask
);
6887 sg
->__cpu_power
= 0;
6889 for_each_cpu_mask(j
, *span
) {
6890 if (group_fn(j
, cpu_map
, NULL
, tmpmask
) != group
)
6893 cpu_set(j
, *covered
);
6894 cpu_set(j
, sg
->cpumask
);
6905 #define SD_NODES_PER_DOMAIN 16
6910 * find_next_best_node - find the next node to include in a sched_domain
6911 * @node: node whose sched_domain we're building
6912 * @used_nodes: nodes already in the sched_domain
6914 * Find the next node to include in a given scheduling domain. Simply
6915 * finds the closest node not already in the @used_nodes map.
6917 * Should use nodemask_t.
6919 static int find_next_best_node(int node
, nodemask_t
*used_nodes
)
6921 int i
, n
, val
, min_val
, best_node
= 0;
6925 for (i
= 0; i
< MAX_NUMNODES
; i
++) {
6926 /* Start at @node */
6927 n
= (node
+ i
) % MAX_NUMNODES
;
6929 if (!nr_cpus_node(n
))
6932 /* Skip already used nodes */
6933 if (node_isset(n
, *used_nodes
))
6936 /* Simple min distance search */
6937 val
= node_distance(node
, n
);
6939 if (val
< min_val
) {
6945 node_set(best_node
, *used_nodes
);
6950 * sched_domain_node_span - get a cpumask for a node's sched_domain
6951 * @node: node whose cpumask we're constructing
6952 * @span: resulting cpumask
6954 * Given a node, construct a good cpumask for its sched_domain to span. It
6955 * should be one that prevents unnecessary balancing, but also spreads tasks
6958 static void sched_domain_node_span(int node
, cpumask_t
*span
)
6960 nodemask_t used_nodes
;
6961 node_to_cpumask_ptr(nodemask
, node
);
6965 nodes_clear(used_nodes
);
6967 cpus_or(*span
, *span
, *nodemask
);
6968 node_set(node
, used_nodes
);
6970 for (i
= 1; i
< SD_NODES_PER_DOMAIN
; i
++) {
6971 int next_node
= find_next_best_node(node
, &used_nodes
);
6973 node_to_cpumask_ptr_next(nodemask
, next_node
);
6974 cpus_or(*span
, *span
, *nodemask
);
6979 int sched_smt_power_savings
= 0, sched_mc_power_savings
= 0;
6982 * SMT sched-domains:
6984 #ifdef CONFIG_SCHED_SMT
6985 static DEFINE_PER_CPU(struct sched_domain
, cpu_domains
);
6986 static DEFINE_PER_CPU(struct sched_group
, sched_group_cpus
);
6989 cpu_to_cpu_group(int cpu
, const cpumask_t
*cpu_map
, struct sched_group
**sg
,
6993 *sg
= &per_cpu(sched_group_cpus
, cpu
);
6999 * multi-core sched-domains:
7001 #ifdef CONFIG_SCHED_MC
7002 static DEFINE_PER_CPU(struct sched_domain
, core_domains
);
7003 static DEFINE_PER_CPU(struct sched_group
, sched_group_core
);
7006 #if defined(CONFIG_SCHED_MC) && defined(CONFIG_SCHED_SMT)
7008 cpu_to_core_group(int cpu
, const cpumask_t
*cpu_map
, struct sched_group
**sg
,
7013 *mask
= per_cpu(cpu_sibling_map
, cpu
);
7014 cpus_and(*mask
, *mask
, *cpu_map
);
7015 group
= first_cpu(*mask
);
7017 *sg
= &per_cpu(sched_group_core
, group
);
7020 #elif defined(CONFIG_SCHED_MC)
7022 cpu_to_core_group(int cpu
, const cpumask_t
*cpu_map
, struct sched_group
**sg
,
7026 *sg
= &per_cpu(sched_group_core
, cpu
);
7031 static DEFINE_PER_CPU(struct sched_domain
, phys_domains
);
7032 static DEFINE_PER_CPU(struct sched_group
, sched_group_phys
);
7035 cpu_to_phys_group(int cpu
, const cpumask_t
*cpu_map
, struct sched_group
**sg
,
7039 #ifdef CONFIG_SCHED_MC
7040 *mask
= cpu_coregroup_map(cpu
);
7041 cpus_and(*mask
, *mask
, *cpu_map
);
7042 group
= first_cpu(*mask
);
7043 #elif defined(CONFIG_SCHED_SMT)
7044 *mask
= per_cpu(cpu_sibling_map
, cpu
);
7045 cpus_and(*mask
, *mask
, *cpu_map
);
7046 group
= first_cpu(*mask
);
7051 *sg
= &per_cpu(sched_group_phys
, group
);
7057 * The init_sched_build_groups can't handle what we want to do with node
7058 * groups, so roll our own. Now each node has its own list of groups which
7059 * gets dynamically allocated.
7061 static DEFINE_PER_CPU(struct sched_domain
, node_domains
);
7062 static struct sched_group
***sched_group_nodes_bycpu
;
7064 static DEFINE_PER_CPU(struct sched_domain
, allnodes_domains
);
7065 static DEFINE_PER_CPU(struct sched_group
, sched_group_allnodes
);
7067 static int cpu_to_allnodes_group(int cpu
, const cpumask_t
*cpu_map
,
7068 struct sched_group
**sg
, cpumask_t
*nodemask
)
7072 *nodemask
= node_to_cpumask(cpu_to_node(cpu
));
7073 cpus_and(*nodemask
, *nodemask
, *cpu_map
);
7074 group
= first_cpu(*nodemask
);
7077 *sg
= &per_cpu(sched_group_allnodes
, group
);
7081 static void init_numa_sched_groups_power(struct sched_group
*group_head
)
7083 struct sched_group
*sg
= group_head
;
7089 for_each_cpu_mask(j
, sg
->cpumask
) {
7090 struct sched_domain
*sd
;
7092 sd
= &per_cpu(phys_domains
, j
);
7093 if (j
!= first_cpu(sd
->groups
->cpumask
)) {
7095 * Only add "power" once for each
7101 sg_inc_cpu_power(sg
, sd
->groups
->__cpu_power
);
7104 } while (sg
!= group_head
);
7109 /* Free memory allocated for various sched_group structures */
7110 static void free_sched_groups(const cpumask_t
*cpu_map
, cpumask_t
*nodemask
)
7114 for_each_cpu_mask(cpu
, *cpu_map
) {
7115 struct sched_group
**sched_group_nodes
7116 = sched_group_nodes_bycpu
[cpu
];
7118 if (!sched_group_nodes
)
7121 for (i
= 0; i
< MAX_NUMNODES
; i
++) {
7122 struct sched_group
*oldsg
, *sg
= sched_group_nodes
[i
];
7124 *nodemask
= node_to_cpumask(i
);
7125 cpus_and(*nodemask
, *nodemask
, *cpu_map
);
7126 if (cpus_empty(*nodemask
))
7136 if (oldsg
!= sched_group_nodes
[i
])
7139 kfree(sched_group_nodes
);
7140 sched_group_nodes_bycpu
[cpu
] = NULL
;
7144 static void free_sched_groups(const cpumask_t
*cpu_map
, cpumask_t
*nodemask
)
7150 * Initialize sched groups cpu_power.
7152 * cpu_power indicates the capacity of sched group, which is used while
7153 * distributing the load between different sched groups in a sched domain.
7154 * Typically cpu_power for all the groups in a sched domain will be same unless
7155 * there are asymmetries in the topology. If there are asymmetries, group
7156 * having more cpu_power will pickup more load compared to the group having
7159 * cpu_power will be a multiple of SCHED_LOAD_SCALE. This multiple represents
7160 * the maximum number of tasks a group can handle in the presence of other idle
7161 * or lightly loaded groups in the same sched domain.
7163 static void init_sched_groups_power(int cpu
, struct sched_domain
*sd
)
7165 struct sched_domain
*child
;
7166 struct sched_group
*group
;
7168 WARN_ON(!sd
|| !sd
->groups
);
7170 if (cpu
!= first_cpu(sd
->groups
->cpumask
))
7175 sd
->groups
->__cpu_power
= 0;
7178 * For perf policy, if the groups in child domain share resources
7179 * (for example cores sharing some portions of the cache hierarchy
7180 * or SMT), then set this domain groups cpu_power such that each group
7181 * can handle only one task, when there are other idle groups in the
7182 * same sched domain.
7184 if (!child
|| (!(sd
->flags
& SD_POWERSAVINGS_BALANCE
) &&
7186 (SD_SHARE_CPUPOWER
| SD_SHARE_PKG_RESOURCES
)))) {
7187 sg_inc_cpu_power(sd
->groups
, SCHED_LOAD_SCALE
);
7192 * add cpu_power of each child group to this groups cpu_power
7194 group
= child
->groups
;
7196 sg_inc_cpu_power(sd
->groups
, group
->__cpu_power
);
7197 group
= group
->next
;
7198 } while (group
!= child
->groups
);
7202 * Initializers for schedule domains
7203 * Non-inlined to reduce accumulated stack pressure in build_sched_domains()
7206 #define SD_INIT(sd, type) sd_init_##type(sd)
7207 #define SD_INIT_FUNC(type) \
7208 static noinline void sd_init_##type(struct sched_domain *sd) \
7210 memset(sd, 0, sizeof(*sd)); \
7211 *sd = SD_##type##_INIT; \
7212 sd->level = SD_LV_##type; \
7217 SD_INIT_FUNC(ALLNODES
)
7220 #ifdef CONFIG_SCHED_SMT
7221 SD_INIT_FUNC(SIBLING
)
7223 #ifdef CONFIG_SCHED_MC
7228 * To minimize stack usage kmalloc room for cpumasks and share the
7229 * space as the usage in build_sched_domains() dictates. Used only
7230 * if the amount of space is significant.
7233 cpumask_t tmpmask
; /* make this one first */
7236 cpumask_t this_sibling_map
;
7237 cpumask_t this_core_map
;
7239 cpumask_t send_covered
;
7242 cpumask_t domainspan
;
7244 cpumask_t notcovered
;
7249 #define SCHED_CPUMASK_ALLOC 1
7250 #define SCHED_CPUMASK_FREE(v) kfree(v)
7251 #define SCHED_CPUMASK_DECLARE(v) struct allmasks *v
7253 #define SCHED_CPUMASK_ALLOC 0
7254 #define SCHED_CPUMASK_FREE(v)
7255 #define SCHED_CPUMASK_DECLARE(v) struct allmasks _v, *v = &_v
7258 #define SCHED_CPUMASK_VAR(v, a) cpumask_t *v = (cpumask_t *) \
7259 ((unsigned long)(a) + offsetof(struct allmasks, v))
7261 static int default_relax_domain_level
= -1;
7263 static int __init
setup_relax_domain_level(char *str
)
7265 default_relax_domain_level
= simple_strtoul(str
, NULL
, 0);
7268 __setup("relax_domain_level=", setup_relax_domain_level
);
7270 static void set_domain_attribute(struct sched_domain
*sd
,
7271 struct sched_domain_attr
*attr
)
7275 if (!attr
|| attr
->relax_domain_level
< 0) {
7276 if (default_relax_domain_level
< 0)
7279 request
= default_relax_domain_level
;
7281 request
= attr
->relax_domain_level
;
7282 if (request
< sd
->level
) {
7283 /* turn off idle balance on this domain */
7284 sd
->flags
&= ~(SD_WAKE_IDLE
|SD_BALANCE_NEWIDLE
);
7286 /* turn on idle balance on this domain */
7287 sd
->flags
|= (SD_WAKE_IDLE_FAR
|SD_BALANCE_NEWIDLE
);
7292 * Build sched domains for a given set of cpus and attach the sched domains
7293 * to the individual cpus
7295 static int __build_sched_domains(const cpumask_t
*cpu_map
,
7296 struct sched_domain_attr
*attr
)
7299 struct root_domain
*rd
;
7300 SCHED_CPUMASK_DECLARE(allmasks
);
7303 struct sched_group
**sched_group_nodes
= NULL
;
7304 int sd_allnodes
= 0;
7307 * Allocate the per-node list of sched groups
7309 sched_group_nodes
= kcalloc(MAX_NUMNODES
, sizeof(struct sched_group
*),
7311 if (!sched_group_nodes
) {
7312 printk(KERN_WARNING
"Can not alloc sched group node list\n");
7317 rd
= alloc_rootdomain();
7319 printk(KERN_WARNING
"Cannot alloc root domain\n");
7321 kfree(sched_group_nodes
);
7326 #if SCHED_CPUMASK_ALLOC
7327 /* get space for all scratch cpumask variables */
7328 allmasks
= kmalloc(sizeof(*allmasks
), GFP_KERNEL
);
7330 printk(KERN_WARNING
"Cannot alloc cpumask array\n");
7333 kfree(sched_group_nodes
);
7338 tmpmask
= (cpumask_t
*)allmasks
;
7342 sched_group_nodes_bycpu
[first_cpu(*cpu_map
)] = sched_group_nodes
;
7346 * Set up domains for cpus specified by the cpu_map.
7348 for_each_cpu_mask(i
, *cpu_map
) {
7349 struct sched_domain
*sd
= NULL
, *p
;
7350 SCHED_CPUMASK_VAR(nodemask
, allmasks
);
7352 *nodemask
= node_to_cpumask(cpu_to_node(i
));
7353 cpus_and(*nodemask
, *nodemask
, *cpu_map
);
7356 if (cpus_weight(*cpu_map
) >
7357 SD_NODES_PER_DOMAIN
*cpus_weight(*nodemask
)) {
7358 sd
= &per_cpu(allnodes_domains
, i
);
7359 SD_INIT(sd
, ALLNODES
);
7360 set_domain_attribute(sd
, attr
);
7361 sd
->span
= *cpu_map
;
7362 sd
->first_cpu
= first_cpu(sd
->span
);
7363 cpu_to_allnodes_group(i
, cpu_map
, &sd
->groups
, tmpmask
);
7369 sd
= &per_cpu(node_domains
, i
);
7371 set_domain_attribute(sd
, attr
);
7372 sched_domain_node_span(cpu_to_node(i
), &sd
->span
);
7373 sd
->first_cpu
= first_cpu(sd
->span
);
7377 cpus_and(sd
->span
, sd
->span
, *cpu_map
);
7381 sd
= &per_cpu(phys_domains
, i
);
7383 set_domain_attribute(sd
, attr
);
7384 sd
->span
= *nodemask
;
7385 sd
->first_cpu
= first_cpu(sd
->span
);
7389 cpu_to_phys_group(i
, cpu_map
, &sd
->groups
, tmpmask
);
7391 #ifdef CONFIG_SCHED_MC
7393 sd
= &per_cpu(core_domains
, i
);
7395 set_domain_attribute(sd
, attr
);
7396 sd
->span
= cpu_coregroup_map(i
);
7397 sd
->first_cpu
= first_cpu(sd
->span
);
7398 cpus_and(sd
->span
, sd
->span
, *cpu_map
);
7401 cpu_to_core_group(i
, cpu_map
, &sd
->groups
, tmpmask
);
7404 #ifdef CONFIG_SCHED_SMT
7406 sd
= &per_cpu(cpu_domains
, i
);
7407 SD_INIT(sd
, SIBLING
);
7408 set_domain_attribute(sd
, attr
);
7409 sd
->span
= per_cpu(cpu_sibling_map
, i
);
7410 sd
->first_cpu
= first_cpu(sd
->span
);
7411 cpus_and(sd
->span
, sd
->span
, *cpu_map
);
7414 cpu_to_cpu_group(i
, cpu_map
, &sd
->groups
, tmpmask
);
7418 #ifdef CONFIG_SCHED_SMT
7419 /* Set up CPU (sibling) groups */
7420 for_each_cpu_mask(i
, *cpu_map
) {
7421 SCHED_CPUMASK_VAR(this_sibling_map
, allmasks
);
7422 SCHED_CPUMASK_VAR(send_covered
, allmasks
);
7424 *this_sibling_map
= per_cpu(cpu_sibling_map
, i
);
7425 cpus_and(*this_sibling_map
, *this_sibling_map
, *cpu_map
);
7426 if (i
!= first_cpu(*this_sibling_map
))
7429 init_sched_build_groups(this_sibling_map
, cpu_map
,
7431 send_covered
, tmpmask
);
7435 #ifdef CONFIG_SCHED_MC
7436 /* Set up multi-core groups */
7437 for_each_cpu_mask(i
, *cpu_map
) {
7438 SCHED_CPUMASK_VAR(this_core_map
, allmasks
);
7439 SCHED_CPUMASK_VAR(send_covered
, allmasks
);
7441 *this_core_map
= cpu_coregroup_map(i
);
7442 cpus_and(*this_core_map
, *this_core_map
, *cpu_map
);
7443 if (i
!= first_cpu(*this_core_map
))
7446 init_sched_build_groups(this_core_map
, cpu_map
,
7448 send_covered
, tmpmask
);
7452 /* Set up physical groups */
7453 for (i
= 0; i
< MAX_NUMNODES
; i
++) {
7454 SCHED_CPUMASK_VAR(nodemask
, allmasks
);
7455 SCHED_CPUMASK_VAR(send_covered
, allmasks
);
7457 *nodemask
= node_to_cpumask(i
);
7458 cpus_and(*nodemask
, *nodemask
, *cpu_map
);
7459 if (cpus_empty(*nodemask
))
7462 init_sched_build_groups(nodemask
, cpu_map
,
7464 send_covered
, tmpmask
);
7468 /* Set up node groups */
7470 SCHED_CPUMASK_VAR(send_covered
, allmasks
);
7472 init_sched_build_groups(cpu_map
, cpu_map
,
7473 &cpu_to_allnodes_group
,
7474 send_covered
, tmpmask
);
7477 for (i
= 0; i
< MAX_NUMNODES
; i
++) {
7478 /* Set up node groups */
7479 struct sched_group
*sg
, *prev
;
7480 SCHED_CPUMASK_VAR(nodemask
, allmasks
);
7481 SCHED_CPUMASK_VAR(domainspan
, allmasks
);
7482 SCHED_CPUMASK_VAR(covered
, allmasks
);
7485 *nodemask
= node_to_cpumask(i
);
7486 cpus_clear(*covered
);
7488 cpus_and(*nodemask
, *nodemask
, *cpu_map
);
7489 if (cpus_empty(*nodemask
)) {
7490 sched_group_nodes
[i
] = NULL
;
7494 sched_domain_node_span(i
, domainspan
);
7495 cpus_and(*domainspan
, *domainspan
, *cpu_map
);
7497 sg
= kmalloc_node(sizeof(struct sched_group
), GFP_KERNEL
, i
);
7499 printk(KERN_WARNING
"Can not alloc domain group for "
7503 sched_group_nodes
[i
] = sg
;
7504 for_each_cpu_mask(j
, *nodemask
) {
7505 struct sched_domain
*sd
;
7507 sd
= &per_cpu(node_domains
, j
);
7510 sg
->__cpu_power
= 0;
7511 sg
->cpumask
= *nodemask
;
7513 cpus_or(*covered
, *covered
, *nodemask
);
7516 for (j
= 0; j
< MAX_NUMNODES
; j
++) {
7517 SCHED_CPUMASK_VAR(notcovered
, allmasks
);
7518 int n
= (i
+ j
) % MAX_NUMNODES
;
7519 node_to_cpumask_ptr(pnodemask
, n
);
7521 cpus_complement(*notcovered
, *covered
);
7522 cpus_and(*tmpmask
, *notcovered
, *cpu_map
);
7523 cpus_and(*tmpmask
, *tmpmask
, *domainspan
);
7524 if (cpus_empty(*tmpmask
))
7527 cpus_and(*tmpmask
, *tmpmask
, *pnodemask
);
7528 if (cpus_empty(*tmpmask
))
7531 sg
= kmalloc_node(sizeof(struct sched_group
),
7535 "Can not alloc domain group for node %d\n", j
);
7538 sg
->__cpu_power
= 0;
7539 sg
->cpumask
= *tmpmask
;
7540 sg
->next
= prev
->next
;
7541 cpus_or(*covered
, *covered
, *tmpmask
);
7548 /* Calculate CPU power for physical packages and nodes */
7549 #ifdef CONFIG_SCHED_SMT
7550 for_each_cpu_mask(i
, *cpu_map
) {
7551 struct sched_domain
*sd
= &per_cpu(cpu_domains
, i
);
7553 init_sched_groups_power(i
, sd
);
7556 #ifdef CONFIG_SCHED_MC
7557 for_each_cpu_mask(i
, *cpu_map
) {
7558 struct sched_domain
*sd
= &per_cpu(core_domains
, i
);
7560 init_sched_groups_power(i
, sd
);
7564 for_each_cpu_mask(i
, *cpu_map
) {
7565 struct sched_domain
*sd
= &per_cpu(phys_domains
, i
);
7567 init_sched_groups_power(i
, sd
);
7571 for (i
= 0; i
< MAX_NUMNODES
; i
++)
7572 init_numa_sched_groups_power(sched_group_nodes
[i
]);
7575 struct sched_group
*sg
;
7577 cpu_to_allnodes_group(first_cpu(*cpu_map
), cpu_map
, &sg
,
7579 init_numa_sched_groups_power(sg
);
7583 /* Attach the domains */
7584 for_each_cpu_mask(i
, *cpu_map
) {
7585 struct sched_domain
*sd
;
7586 #ifdef CONFIG_SCHED_SMT
7587 sd
= &per_cpu(cpu_domains
, i
);
7588 #elif defined(CONFIG_SCHED_MC)
7589 sd
= &per_cpu(core_domains
, i
);
7591 sd
= &per_cpu(phys_domains
, i
);
7593 cpu_attach_domain(sd
, rd
, i
);
7596 SCHED_CPUMASK_FREE((void *)allmasks
);
7601 free_sched_groups(cpu_map
, tmpmask
);
7602 SCHED_CPUMASK_FREE((void *)allmasks
);
7607 static int build_sched_domains(const cpumask_t
*cpu_map
)
7609 return __build_sched_domains(cpu_map
, NULL
);
7612 static cpumask_t
*doms_cur
; /* current sched domains */
7613 static int ndoms_cur
; /* number of sched domains in 'doms_cur' */
7614 static struct sched_domain_attr
*dattr_cur
; /* attribues of custom domains
7618 * Special case: If a kmalloc of a doms_cur partition (array of
7619 * cpumask_t) fails, then fallback to a single sched domain,
7620 * as determined by the single cpumask_t fallback_doms.
7622 static cpumask_t fallback_doms
;
7624 void __attribute__((weak
)) arch_update_cpu_topology(void)
7629 * Set up scheduler domains and groups. Callers must hold the hotplug lock.
7630 * For now this just excludes isolated cpus, but could be used to
7631 * exclude other special cases in the future.
7633 static int arch_init_sched_domains(const cpumask_t
*cpu_map
)
7637 arch_update_cpu_topology();
7639 doms_cur
= kmalloc(sizeof(cpumask_t
), GFP_KERNEL
);
7641 doms_cur
= &fallback_doms
;
7642 cpus_andnot(*doms_cur
, *cpu_map
, cpu_isolated_map
);
7644 err
= build_sched_domains(doms_cur
);
7645 register_sched_domain_sysctl();
7650 static void arch_destroy_sched_domains(const cpumask_t
*cpu_map
,
7653 free_sched_groups(cpu_map
, tmpmask
);
7657 * Detach sched domains from a group of cpus specified in cpu_map
7658 * These cpus will now be attached to the NULL domain
7660 static void detach_destroy_domains(const cpumask_t
*cpu_map
)
7665 unregister_sched_domain_sysctl();
7667 for_each_cpu_mask(i
, *cpu_map
)
7668 cpu_attach_domain(NULL
, &def_root_domain
, i
);
7669 synchronize_sched();
7670 arch_destroy_sched_domains(cpu_map
, &tmpmask
);
7673 /* handle null as "default" */
7674 static int dattrs_equal(struct sched_domain_attr
*cur
, int idx_cur
,
7675 struct sched_domain_attr
*new, int idx_new
)
7677 struct sched_domain_attr tmp
;
7684 return !memcmp(cur
? (cur
+ idx_cur
) : &tmp
,
7685 new ? (new + idx_new
) : &tmp
,
7686 sizeof(struct sched_domain_attr
));
7690 * Partition sched domains as specified by the 'ndoms_new'
7691 * cpumasks in the array doms_new[] of cpumasks. This compares
7692 * doms_new[] to the current sched domain partitioning, doms_cur[].
7693 * It destroys each deleted domain and builds each new domain.
7695 * 'doms_new' is an array of cpumask_t's of length 'ndoms_new'.
7696 * The masks don't intersect (don't overlap.) We should setup one
7697 * sched domain for each mask. CPUs not in any of the cpumasks will
7698 * not be load balanced. If the same cpumask appears both in the
7699 * current 'doms_cur' domains and in the new 'doms_new', we can leave
7702 * The passed in 'doms_new' should be kmalloc'd. This routine takes
7703 * ownership of it and will kfree it when done with it. If the caller
7704 * failed the kmalloc call, then it can pass in doms_new == NULL,
7705 * and partition_sched_domains() will fallback to the single partition
7708 * Call with hotplug lock held
7710 void partition_sched_domains(int ndoms_new
, cpumask_t
*doms_new
,
7711 struct sched_domain_attr
*dattr_new
)
7715 mutex_lock(&sched_domains_mutex
);
7717 /* always unregister in case we don't destroy any domains */
7718 unregister_sched_domain_sysctl();
7720 if (doms_new
== NULL
) {
7722 doms_new
= &fallback_doms
;
7723 cpus_andnot(doms_new
[0], cpu_online_map
, cpu_isolated_map
);
7727 /* Destroy deleted domains */
7728 for (i
= 0; i
< ndoms_cur
; i
++) {
7729 for (j
= 0; j
< ndoms_new
; j
++) {
7730 if (cpus_equal(doms_cur
[i
], doms_new
[j
])
7731 && dattrs_equal(dattr_cur
, i
, dattr_new
, j
))
7734 /* no match - a current sched domain not in new doms_new[] */
7735 detach_destroy_domains(doms_cur
+ i
);
7740 /* Build new domains */
7741 for (i
= 0; i
< ndoms_new
; i
++) {
7742 for (j
= 0; j
< ndoms_cur
; j
++) {
7743 if (cpus_equal(doms_new
[i
], doms_cur
[j
])
7744 && dattrs_equal(dattr_new
, i
, dattr_cur
, j
))
7747 /* no match - add a new doms_new */
7748 __build_sched_domains(doms_new
+ i
,
7749 dattr_new
? dattr_new
+ i
: NULL
);
7754 /* Remember the new sched domains */
7755 if (doms_cur
!= &fallback_doms
)
7757 kfree(dattr_cur
); /* kfree(NULL) is safe */
7758 doms_cur
= doms_new
;
7759 dattr_cur
= dattr_new
;
7760 ndoms_cur
= ndoms_new
;
7762 register_sched_domain_sysctl();
7764 mutex_unlock(&sched_domains_mutex
);
7767 #if defined(CONFIG_SCHED_MC) || defined(CONFIG_SCHED_SMT)
7768 int arch_reinit_sched_domains(void)
7773 mutex_lock(&sched_domains_mutex
);
7774 detach_destroy_domains(&cpu_online_map
);
7775 err
= arch_init_sched_domains(&cpu_online_map
);
7776 mutex_unlock(&sched_domains_mutex
);
7782 static ssize_t
sched_power_savings_store(const char *buf
, size_t count
, int smt
)
7786 if (buf
[0] != '0' && buf
[0] != '1')
7790 sched_smt_power_savings
= (buf
[0] == '1');
7792 sched_mc_power_savings
= (buf
[0] == '1');
7794 ret
= arch_reinit_sched_domains();
7796 return ret
? ret
: count
;
7799 #ifdef CONFIG_SCHED_MC
7800 static ssize_t
sched_mc_power_savings_show(struct sys_device
*dev
, char *page
)
7802 return sprintf(page
, "%u\n", sched_mc_power_savings
);
7804 static ssize_t
sched_mc_power_savings_store(struct sys_device
*dev
,
7805 const char *buf
, size_t count
)
7807 return sched_power_savings_store(buf
, count
, 0);
7809 static SYSDEV_ATTR(sched_mc_power_savings
, 0644, sched_mc_power_savings_show
,
7810 sched_mc_power_savings_store
);
7813 #ifdef CONFIG_SCHED_SMT
7814 static ssize_t
sched_smt_power_savings_show(struct sys_device
*dev
, char *page
)
7816 return sprintf(page
, "%u\n", sched_smt_power_savings
);
7818 static ssize_t
sched_smt_power_savings_store(struct sys_device
*dev
,
7819 const char *buf
, size_t count
)
7821 return sched_power_savings_store(buf
, count
, 1);
7823 static SYSDEV_ATTR(sched_smt_power_savings
, 0644, sched_smt_power_savings_show
,
7824 sched_smt_power_savings_store
);
7827 int sched_create_sysfs_power_savings_entries(struct sysdev_class
*cls
)
7831 #ifdef CONFIG_SCHED_SMT
7833 err
= sysfs_create_file(&cls
->kset
.kobj
,
7834 &attr_sched_smt_power_savings
.attr
);
7836 #ifdef CONFIG_SCHED_MC
7837 if (!err
&& mc_capable())
7838 err
= sysfs_create_file(&cls
->kset
.kobj
,
7839 &attr_sched_mc_power_savings
.attr
);
7846 * Force a reinitialization of the sched domains hierarchy. The domains
7847 * and groups cannot be updated in place without racing with the balancing
7848 * code, so we temporarily attach all running cpus to the NULL domain
7849 * which will prevent rebalancing while the sched domains are recalculated.
7851 static int update_sched_domains(struct notifier_block
*nfb
,
7852 unsigned long action
, void *hcpu
)
7855 case CPU_UP_PREPARE
:
7856 case CPU_UP_PREPARE_FROZEN
:
7857 case CPU_DOWN_PREPARE
:
7858 case CPU_DOWN_PREPARE_FROZEN
:
7859 detach_destroy_domains(&cpu_online_map
);
7862 case CPU_UP_CANCELED
:
7863 case CPU_UP_CANCELED_FROZEN
:
7864 case CPU_DOWN_FAILED
:
7865 case CPU_DOWN_FAILED_FROZEN
:
7867 case CPU_ONLINE_FROZEN
:
7869 case CPU_DEAD_FROZEN
:
7871 * Fall through and re-initialise the domains.
7878 /* The hotplug lock is already held by cpu_up/cpu_down */
7879 arch_init_sched_domains(&cpu_online_map
);
7884 void __init
sched_init_smp(void)
7886 cpumask_t non_isolated_cpus
;
7888 #if defined(CONFIG_NUMA)
7889 sched_group_nodes_bycpu
= kzalloc(nr_cpu_ids
* sizeof(void **),
7891 BUG_ON(sched_group_nodes_bycpu
== NULL
);
7894 mutex_lock(&sched_domains_mutex
);
7895 arch_init_sched_domains(&cpu_online_map
);
7896 cpus_andnot(non_isolated_cpus
, cpu_possible_map
, cpu_isolated_map
);
7897 if (cpus_empty(non_isolated_cpus
))
7898 cpu_set(smp_processor_id(), non_isolated_cpus
);
7899 mutex_unlock(&sched_domains_mutex
);
7901 /* XXX: Theoretical race here - CPU may be hotplugged now */
7902 hotcpu_notifier(update_sched_domains
, 0);
7905 /* Move init over to a non-isolated CPU */
7906 if (set_cpus_allowed_ptr(current
, &non_isolated_cpus
) < 0)
7908 sched_init_granularity();
7911 void __init
sched_init_smp(void)
7913 sched_init_granularity();
7915 #endif /* CONFIG_SMP */
7917 int in_sched_functions(unsigned long addr
)
7919 return in_lock_functions(addr
) ||
7920 (addr
>= (unsigned long)__sched_text_start
7921 && addr
< (unsigned long)__sched_text_end
);
7924 static void init_cfs_rq(struct cfs_rq
*cfs_rq
, struct rq
*rq
)
7926 cfs_rq
->tasks_timeline
= RB_ROOT
;
7927 INIT_LIST_HEAD(&cfs_rq
->tasks
);
7928 #ifdef CONFIG_FAIR_GROUP_SCHED
7931 cfs_rq
->min_vruntime
= (u64
)(-(1LL << 20));
7934 static void init_rt_rq(struct rt_rq
*rt_rq
, struct rq
*rq
)
7936 struct rt_prio_array
*array
;
7939 array
= &rt_rq
->active
;
7940 for (i
= 0; i
< MAX_RT_PRIO
; i
++) {
7941 INIT_LIST_HEAD(array
->queue
+ i
);
7942 __clear_bit(i
, array
->bitmap
);
7944 /* delimiter for bitsearch: */
7945 __set_bit(MAX_RT_PRIO
, array
->bitmap
);
7947 #if defined CONFIG_SMP || defined CONFIG_RT_GROUP_SCHED
7948 rt_rq
->highest_prio
= MAX_RT_PRIO
;
7951 rt_rq
->rt_nr_migratory
= 0;
7952 rt_rq
->overloaded
= 0;
7956 rt_rq
->rt_throttled
= 0;
7957 rt_rq
->rt_runtime
= 0;
7958 spin_lock_init(&rt_rq
->rt_runtime_lock
);
7960 #ifdef CONFIG_RT_GROUP_SCHED
7961 rt_rq
->rt_nr_boosted
= 0;
7966 #ifdef CONFIG_FAIR_GROUP_SCHED
7967 static void init_tg_cfs_entry(struct task_group
*tg
, struct cfs_rq
*cfs_rq
,
7968 struct sched_entity
*se
, int cpu
, int add
,
7969 struct sched_entity
*parent
)
7971 struct rq
*rq
= cpu_rq(cpu
);
7972 tg
->cfs_rq
[cpu
] = cfs_rq
;
7973 init_cfs_rq(cfs_rq
, rq
);
7976 list_add(&cfs_rq
->leaf_cfs_rq_list
, &rq
->leaf_cfs_rq_list
);
7979 /* se could be NULL for init_task_group */
7984 se
->cfs_rq
= &rq
->cfs
;
7986 se
->cfs_rq
= parent
->my_q
;
7989 se
->load
.weight
= tg
->shares
;
7990 se
->load
.inv_weight
= 0;
7991 se
->parent
= parent
;
7995 #ifdef CONFIG_RT_GROUP_SCHED
7996 static void init_tg_rt_entry(struct task_group
*tg
, struct rt_rq
*rt_rq
,
7997 struct sched_rt_entity
*rt_se
, int cpu
, int add
,
7998 struct sched_rt_entity
*parent
)
8000 struct rq
*rq
= cpu_rq(cpu
);
8002 tg
->rt_rq
[cpu
] = rt_rq
;
8003 init_rt_rq(rt_rq
, rq
);
8005 rt_rq
->rt_se
= rt_se
;
8006 rt_rq
->rt_runtime
= tg
->rt_bandwidth
.rt_runtime
;
8008 list_add(&rt_rq
->leaf_rt_rq_list
, &rq
->leaf_rt_rq_list
);
8010 tg
->rt_se
[cpu
] = rt_se
;
8015 rt_se
->rt_rq
= &rq
->rt
;
8017 rt_se
->rt_rq
= parent
->my_q
;
8019 rt_se
->rt_rq
= &rq
->rt
;
8020 rt_se
->my_q
= rt_rq
;
8021 rt_se
->parent
= parent
;
8022 INIT_LIST_HEAD(&rt_se
->run_list
);
8026 void __init
sched_init(void)
8029 unsigned long alloc_size
= 0, ptr
;
8031 #ifdef CONFIG_FAIR_GROUP_SCHED
8032 alloc_size
+= 2 * nr_cpu_ids
* sizeof(void **);
8034 #ifdef CONFIG_RT_GROUP_SCHED
8035 alloc_size
+= 2 * nr_cpu_ids
* sizeof(void **);
8037 #ifdef CONFIG_USER_SCHED
8041 * As sched_init() is called before page_alloc is setup,
8042 * we use alloc_bootmem().
8045 ptr
= (unsigned long)alloc_bootmem(alloc_size
);
8047 #ifdef CONFIG_FAIR_GROUP_SCHED
8048 init_task_group
.se
= (struct sched_entity
**)ptr
;
8049 ptr
+= nr_cpu_ids
* sizeof(void **);
8051 init_task_group
.cfs_rq
= (struct cfs_rq
**)ptr
;
8052 ptr
+= nr_cpu_ids
* sizeof(void **);
8054 #ifdef CONFIG_USER_SCHED
8055 root_task_group
.se
= (struct sched_entity
**)ptr
;
8056 ptr
+= nr_cpu_ids
* sizeof(void **);
8058 root_task_group
.cfs_rq
= (struct cfs_rq
**)ptr
;
8059 ptr
+= nr_cpu_ids
* sizeof(void **);
8062 #ifdef CONFIG_RT_GROUP_SCHED
8063 init_task_group
.rt_se
= (struct sched_rt_entity
**)ptr
;
8064 ptr
+= nr_cpu_ids
* sizeof(void **);
8066 init_task_group
.rt_rq
= (struct rt_rq
**)ptr
;
8067 ptr
+= nr_cpu_ids
* sizeof(void **);
8069 #ifdef CONFIG_USER_SCHED
8070 root_task_group
.rt_se
= (struct sched_rt_entity
**)ptr
;
8071 ptr
+= nr_cpu_ids
* sizeof(void **);
8073 root_task_group
.rt_rq
= (struct rt_rq
**)ptr
;
8074 ptr
+= nr_cpu_ids
* sizeof(void **);
8081 init_defrootdomain();
8084 init_rt_bandwidth(&def_rt_bandwidth
,
8085 global_rt_period(), global_rt_runtime());
8087 #ifdef CONFIG_RT_GROUP_SCHED
8088 init_rt_bandwidth(&init_task_group
.rt_bandwidth
,
8089 global_rt_period(), global_rt_runtime());
8090 #ifdef CONFIG_USER_SCHED
8091 init_rt_bandwidth(&root_task_group
.rt_bandwidth
,
8092 global_rt_period(), RUNTIME_INF
);
8096 #ifdef CONFIG_GROUP_SCHED
8097 list_add(&init_task_group
.list
, &task_groups
);
8098 INIT_LIST_HEAD(&init_task_group
.children
);
8100 #ifdef CONFIG_USER_SCHED
8101 INIT_LIST_HEAD(&root_task_group
.children
);
8102 init_task_group
.parent
= &root_task_group
;
8103 list_add(&init_task_group
.siblings
, &root_task_group
.children
);
8107 for_each_possible_cpu(i
) {
8111 spin_lock_init(&rq
->lock
);
8112 lockdep_set_class(&rq
->lock
, &rq
->rq_lock_key
);
8114 init_cfs_rq(&rq
->cfs
, rq
);
8115 init_rt_rq(&rq
->rt
, rq
);
8116 #ifdef CONFIG_FAIR_GROUP_SCHED
8117 init_task_group
.shares
= init_task_group_load
;
8118 INIT_LIST_HEAD(&rq
->leaf_cfs_rq_list
);
8119 #ifdef CONFIG_CGROUP_SCHED
8121 * How much cpu bandwidth does init_task_group get?
8123 * In case of task-groups formed thr' the cgroup filesystem, it
8124 * gets 100% of the cpu resources in the system. This overall
8125 * system cpu resource is divided among the tasks of
8126 * init_task_group and its child task-groups in a fair manner,
8127 * based on each entity's (task or task-group's) weight
8128 * (se->load.weight).
8130 * In other words, if init_task_group has 10 tasks of weight
8131 * 1024) and two child groups A0 and A1 (of weight 1024 each),
8132 * then A0's share of the cpu resource is:
8134 * A0's bandwidth = 1024 / (10*1024 + 1024 + 1024) = 8.33%
8136 * We achieve this by letting init_task_group's tasks sit
8137 * directly in rq->cfs (i.e init_task_group->se[] = NULL).
8139 init_tg_cfs_entry(&init_task_group
, &rq
->cfs
, NULL
, i
, 1, NULL
);
8140 #elif defined CONFIG_USER_SCHED
8141 root_task_group
.shares
= NICE_0_LOAD
;
8142 init_tg_cfs_entry(&root_task_group
, &rq
->cfs
, NULL
, i
, 0, NULL
);
8144 * In case of task-groups formed thr' the user id of tasks,
8145 * init_task_group represents tasks belonging to root user.
8146 * Hence it forms a sibling of all subsequent groups formed.
8147 * In this case, init_task_group gets only a fraction of overall
8148 * system cpu resource, based on the weight assigned to root
8149 * user's cpu share (INIT_TASK_GROUP_LOAD). This is accomplished
8150 * by letting tasks of init_task_group sit in a separate cfs_rq
8151 * (init_cfs_rq) and having one entity represent this group of
8152 * tasks in rq->cfs (i.e init_task_group->se[] != NULL).
8154 init_tg_cfs_entry(&init_task_group
,
8155 &per_cpu(init_cfs_rq
, i
),
8156 &per_cpu(init_sched_entity
, i
), i
, 1,
8157 root_task_group
.se
[i
]);
8160 #endif /* CONFIG_FAIR_GROUP_SCHED */
8162 rq
->rt
.rt_runtime
= def_rt_bandwidth
.rt_runtime
;
8163 #ifdef CONFIG_RT_GROUP_SCHED
8164 INIT_LIST_HEAD(&rq
->leaf_rt_rq_list
);
8165 #ifdef CONFIG_CGROUP_SCHED
8166 init_tg_rt_entry(&init_task_group
, &rq
->rt
, NULL
, i
, 1, NULL
);
8167 #elif defined CONFIG_USER_SCHED
8168 init_tg_rt_entry(&root_task_group
, &rq
->rt
, NULL
, i
, 0, NULL
);
8169 init_tg_rt_entry(&init_task_group
,
8170 &per_cpu(init_rt_rq
, i
),
8171 &per_cpu(init_sched_rt_entity
, i
), i
, 1,
8172 root_task_group
.rt_se
[i
]);
8176 for (j
= 0; j
< CPU_LOAD_IDX_MAX
; j
++)
8177 rq
->cpu_load
[j
] = 0;
8181 rq
->active_balance
= 0;
8182 rq
->next_balance
= jiffies
;
8185 rq
->migration_thread
= NULL
;
8186 INIT_LIST_HEAD(&rq
->migration_queue
);
8187 rq_attach_root(rq
, &def_root_domain
);
8190 atomic_set(&rq
->nr_iowait
, 0);
8193 set_load_weight(&init_task
);
8195 #ifdef CONFIG_PREEMPT_NOTIFIERS
8196 INIT_HLIST_HEAD(&init_task
.preempt_notifiers
);
8200 open_softirq(SCHED_SOFTIRQ
, run_rebalance_domains
, NULL
);
8203 #ifdef CONFIG_RT_MUTEXES
8204 plist_head_init(&init_task
.pi_waiters
, &init_task
.pi_lock
);
8208 * The boot idle thread does lazy MMU switching as well:
8210 atomic_inc(&init_mm
.mm_count
);
8211 enter_lazy_tlb(&init_mm
, current
);
8214 * Make us the idle thread. Technically, schedule() should not be
8215 * called from this thread, however somewhere below it might be,
8216 * but because we are the idle thread, we just pick up running again
8217 * when this runqueue becomes "idle".
8219 init_idle(current
, smp_processor_id());
8221 * During early bootup we pretend to be a normal task:
8223 current
->sched_class
= &fair_sched_class
;
8225 scheduler_running
= 1;
8228 #ifdef CONFIG_DEBUG_SPINLOCK_SLEEP
8229 void __might_sleep(char *file
, int line
)
8232 static unsigned long prev_jiffy
; /* ratelimiting */
8234 if ((in_atomic() || irqs_disabled()) &&
8235 system_state
== SYSTEM_RUNNING
&& !oops_in_progress
) {
8236 if (time_before(jiffies
, prev_jiffy
+ HZ
) && prev_jiffy
)
8238 prev_jiffy
= jiffies
;
8239 printk(KERN_ERR
"BUG: sleeping function called from invalid"
8240 " context at %s:%d\n", file
, line
);
8241 printk("in_atomic():%d, irqs_disabled():%d\n",
8242 in_atomic(), irqs_disabled());
8243 debug_show_held_locks(current
);
8244 if (irqs_disabled())
8245 print_irqtrace_events(current
);
8250 EXPORT_SYMBOL(__might_sleep
);
8253 #ifdef CONFIG_MAGIC_SYSRQ
8254 static void normalize_task(struct rq
*rq
, struct task_struct
*p
)
8258 update_rq_clock(rq
);
8259 on_rq
= p
->se
.on_rq
;
8261 deactivate_task(rq
, p
, 0);
8262 __setscheduler(rq
, p
, SCHED_NORMAL
, 0);
8264 activate_task(rq
, p
, 0);
8265 resched_task(rq
->curr
);
8269 void normalize_rt_tasks(void)
8271 struct task_struct
*g
, *p
;
8272 unsigned long flags
;
8275 read_lock_irqsave(&tasklist_lock
, flags
);
8276 do_each_thread(g
, p
) {
8278 * Only normalize user tasks:
8283 p
->se
.exec_start
= 0;
8284 #ifdef CONFIG_SCHEDSTATS
8285 p
->se
.wait_start
= 0;
8286 p
->se
.sleep_start
= 0;
8287 p
->se
.block_start
= 0;
8292 * Renice negative nice level userspace
8295 if (TASK_NICE(p
) < 0 && p
->mm
)
8296 set_user_nice(p
, 0);
8300 spin_lock(&p
->pi_lock
);
8301 rq
= __task_rq_lock(p
);
8303 normalize_task(rq
, p
);
8305 __task_rq_unlock(rq
);
8306 spin_unlock(&p
->pi_lock
);
8307 } while_each_thread(g
, p
);
8309 read_unlock_irqrestore(&tasklist_lock
, flags
);
8312 #endif /* CONFIG_MAGIC_SYSRQ */
8316 * These functions are only useful for the IA64 MCA handling.
8318 * They can only be called when the whole system has been
8319 * stopped - every CPU needs to be quiescent, and no scheduling
8320 * activity can take place. Using them for anything else would
8321 * be a serious bug, and as a result, they aren't even visible
8322 * under any other configuration.
8326 * curr_task - return the current task for a given cpu.
8327 * @cpu: the processor in question.
8329 * ONLY VALID WHEN THE WHOLE SYSTEM IS STOPPED!
8331 struct task_struct
*curr_task(int cpu
)
8333 return cpu_curr(cpu
);
8337 * set_curr_task - set the current task for a given cpu.
8338 * @cpu: the processor in question.
8339 * @p: the task pointer to set.
8341 * Description: This function must only be used when non-maskable interrupts
8342 * are serviced on a separate stack. It allows the architecture to switch the
8343 * notion of the current task on a cpu in a non-blocking manner. This function
8344 * must be called with all CPU's synchronized, and interrupts disabled, the
8345 * and caller must save the original value of the current task (see
8346 * curr_task() above) and restore that value before reenabling interrupts and
8347 * re-starting the system.
8349 * ONLY VALID WHEN THE WHOLE SYSTEM IS STOPPED!
8351 void set_curr_task(int cpu
, struct task_struct
*p
)
8358 #ifdef CONFIG_FAIR_GROUP_SCHED
8359 static void free_fair_sched_group(struct task_group
*tg
)
8363 for_each_possible_cpu(i
) {
8365 kfree(tg
->cfs_rq
[i
]);
8375 int alloc_fair_sched_group(struct task_group
*tg
, struct task_group
*parent
)
8377 struct cfs_rq
*cfs_rq
;
8378 struct sched_entity
*se
, *parent_se
;
8382 tg
->cfs_rq
= kzalloc(sizeof(cfs_rq
) * nr_cpu_ids
, GFP_KERNEL
);
8385 tg
->se
= kzalloc(sizeof(se
) * nr_cpu_ids
, GFP_KERNEL
);
8389 tg
->shares
= NICE_0_LOAD
;
8391 for_each_possible_cpu(i
) {
8394 cfs_rq
= kmalloc_node(sizeof(struct cfs_rq
),
8395 GFP_KERNEL
|__GFP_ZERO
, cpu_to_node(i
));
8399 se
= kmalloc_node(sizeof(struct sched_entity
),
8400 GFP_KERNEL
|__GFP_ZERO
, cpu_to_node(i
));
8404 parent_se
= parent
? parent
->se
[i
] : NULL
;
8405 init_tg_cfs_entry(tg
, cfs_rq
, se
, i
, 0, parent_se
);
8414 static inline void register_fair_sched_group(struct task_group
*tg
, int cpu
)
8416 list_add_rcu(&tg
->cfs_rq
[cpu
]->leaf_cfs_rq_list
,
8417 &cpu_rq(cpu
)->leaf_cfs_rq_list
);
8420 static inline void unregister_fair_sched_group(struct task_group
*tg
, int cpu
)
8422 list_del_rcu(&tg
->cfs_rq
[cpu
]->leaf_cfs_rq_list
);
8425 static inline void free_fair_sched_group(struct task_group
*tg
)
8430 int alloc_fair_sched_group(struct task_group
*tg
, struct task_group
*parent
)
8435 static inline void register_fair_sched_group(struct task_group
*tg
, int cpu
)
8439 static inline void unregister_fair_sched_group(struct task_group
*tg
, int cpu
)
8444 #ifdef CONFIG_RT_GROUP_SCHED
8445 static void free_rt_sched_group(struct task_group
*tg
)
8449 destroy_rt_bandwidth(&tg
->rt_bandwidth
);
8451 for_each_possible_cpu(i
) {
8453 kfree(tg
->rt_rq
[i
]);
8455 kfree(tg
->rt_se
[i
]);
8463 int alloc_rt_sched_group(struct task_group
*tg
, struct task_group
*parent
)
8465 struct rt_rq
*rt_rq
;
8466 struct sched_rt_entity
*rt_se
, *parent_se
;
8470 tg
->rt_rq
= kzalloc(sizeof(rt_rq
) * nr_cpu_ids
, GFP_KERNEL
);
8473 tg
->rt_se
= kzalloc(sizeof(rt_se
) * nr_cpu_ids
, GFP_KERNEL
);
8477 init_rt_bandwidth(&tg
->rt_bandwidth
,
8478 ktime_to_ns(def_rt_bandwidth
.rt_period
), 0);
8480 for_each_possible_cpu(i
) {
8483 rt_rq
= kmalloc_node(sizeof(struct rt_rq
),
8484 GFP_KERNEL
|__GFP_ZERO
, cpu_to_node(i
));
8488 rt_se
= kmalloc_node(sizeof(struct sched_rt_entity
),
8489 GFP_KERNEL
|__GFP_ZERO
, cpu_to_node(i
));
8493 parent_se
= parent
? parent
->rt_se
[i
] : NULL
;
8494 init_tg_rt_entry(tg
, rt_rq
, rt_se
, i
, 0, parent_se
);
8503 static inline void register_rt_sched_group(struct task_group
*tg
, int cpu
)
8505 list_add_rcu(&tg
->rt_rq
[cpu
]->leaf_rt_rq_list
,
8506 &cpu_rq(cpu
)->leaf_rt_rq_list
);
8509 static inline void unregister_rt_sched_group(struct task_group
*tg
, int cpu
)
8511 list_del_rcu(&tg
->rt_rq
[cpu
]->leaf_rt_rq_list
);
8514 static inline void free_rt_sched_group(struct task_group
*tg
)
8519 int alloc_rt_sched_group(struct task_group
*tg
, struct task_group
*parent
)
8524 static inline void register_rt_sched_group(struct task_group
*tg
, int cpu
)
8528 static inline void unregister_rt_sched_group(struct task_group
*tg
, int cpu
)
8533 #ifdef CONFIG_GROUP_SCHED
8534 static void free_sched_group(struct task_group
*tg
)
8536 free_fair_sched_group(tg
);
8537 free_rt_sched_group(tg
);
8541 /* allocate runqueue etc for a new task group */
8542 struct task_group
*sched_create_group(struct task_group
*parent
)
8544 struct task_group
*tg
;
8545 unsigned long flags
;
8548 tg
= kzalloc(sizeof(*tg
), GFP_KERNEL
);
8550 return ERR_PTR(-ENOMEM
);
8552 if (!alloc_fair_sched_group(tg
, parent
))
8555 if (!alloc_rt_sched_group(tg
, parent
))
8558 spin_lock_irqsave(&task_group_lock
, flags
);
8559 for_each_possible_cpu(i
) {
8560 register_fair_sched_group(tg
, i
);
8561 register_rt_sched_group(tg
, i
);
8563 list_add_rcu(&tg
->list
, &task_groups
);
8565 WARN_ON(!parent
); /* root should already exist */
8567 tg
->parent
= parent
;
8568 list_add_rcu(&tg
->siblings
, &parent
->children
);
8569 INIT_LIST_HEAD(&tg
->children
);
8570 spin_unlock_irqrestore(&task_group_lock
, flags
);
8575 free_sched_group(tg
);
8576 return ERR_PTR(-ENOMEM
);
8579 /* rcu callback to free various structures associated with a task group */
8580 static void free_sched_group_rcu(struct rcu_head
*rhp
)
8582 /* now it should be safe to free those cfs_rqs */
8583 free_sched_group(container_of(rhp
, struct task_group
, rcu
));
8586 /* Destroy runqueue etc associated with a task group */
8587 void sched_destroy_group(struct task_group
*tg
)
8589 unsigned long flags
;
8592 spin_lock_irqsave(&task_group_lock
, flags
);
8593 for_each_possible_cpu(i
) {
8594 unregister_fair_sched_group(tg
, i
);
8595 unregister_rt_sched_group(tg
, i
);
8597 list_del_rcu(&tg
->list
);
8598 list_del_rcu(&tg
->siblings
);
8599 spin_unlock_irqrestore(&task_group_lock
, flags
);
8601 /* wait for possible concurrent references to cfs_rqs complete */
8602 call_rcu(&tg
->rcu
, free_sched_group_rcu
);
8605 /* change task's runqueue when it moves between groups.
8606 * The caller of this function should have put the task in its new group
8607 * by now. This function just updates tsk->se.cfs_rq and tsk->se.parent to
8608 * reflect its new group.
8610 void sched_move_task(struct task_struct
*tsk
)
8613 unsigned long flags
;
8616 rq
= task_rq_lock(tsk
, &flags
);
8618 update_rq_clock(rq
);
8620 running
= task_current(rq
, tsk
);
8621 on_rq
= tsk
->se
.on_rq
;
8624 dequeue_task(rq
, tsk
, 0);
8625 if (unlikely(running
))
8626 tsk
->sched_class
->put_prev_task(rq
, tsk
);
8628 set_task_rq(tsk
, task_cpu(tsk
));
8630 #ifdef CONFIG_FAIR_GROUP_SCHED
8631 if (tsk
->sched_class
->moved_group
)
8632 tsk
->sched_class
->moved_group(tsk
);
8635 if (unlikely(running
))
8636 tsk
->sched_class
->set_curr_task(rq
);
8638 enqueue_task(rq
, tsk
, 0);
8640 task_rq_unlock(rq
, &flags
);
8644 #ifdef CONFIG_FAIR_GROUP_SCHED
8645 static void __set_se_shares(struct sched_entity
*se
, unsigned long shares
)
8647 struct cfs_rq
*cfs_rq
= se
->cfs_rq
;
8652 dequeue_entity(cfs_rq
, se
, 0);
8654 se
->load
.weight
= shares
;
8655 se
->load
.inv_weight
= 0;
8658 enqueue_entity(cfs_rq
, se
, 0);
8661 static void set_se_shares(struct sched_entity
*se
, unsigned long shares
)
8663 struct cfs_rq
*cfs_rq
= se
->cfs_rq
;
8664 struct rq
*rq
= cfs_rq
->rq
;
8665 unsigned long flags
;
8667 spin_lock_irqsave(&rq
->lock
, flags
);
8668 __set_se_shares(se
, shares
);
8669 spin_unlock_irqrestore(&rq
->lock
, flags
);
8672 static DEFINE_MUTEX(shares_mutex
);
8674 int sched_group_set_shares(struct task_group
*tg
, unsigned long shares
)
8677 unsigned long flags
;
8680 * We can't change the weight of the root cgroup.
8685 if (shares
< MIN_SHARES
)
8686 shares
= MIN_SHARES
;
8687 else if (shares
> MAX_SHARES
)
8688 shares
= MAX_SHARES
;
8690 mutex_lock(&shares_mutex
);
8691 if (tg
->shares
== shares
)
8694 spin_lock_irqsave(&task_group_lock
, flags
);
8695 for_each_possible_cpu(i
)
8696 unregister_fair_sched_group(tg
, i
);
8697 list_del_rcu(&tg
->siblings
);
8698 spin_unlock_irqrestore(&task_group_lock
, flags
);
8700 /* wait for any ongoing reference to this group to finish */
8701 synchronize_sched();
8704 * Now we are free to modify the group's share on each cpu
8705 * w/o tripping rebalance_share or load_balance_fair.
8707 tg
->shares
= shares
;
8708 for_each_possible_cpu(i
) {
8712 cfs_rq_set_shares(tg
->cfs_rq
[i
], 0);
8713 set_se_shares(tg
->se
[i
], shares
);
8717 * Enable load balance activity on this group, by inserting it back on
8718 * each cpu's rq->leaf_cfs_rq_list.
8720 spin_lock_irqsave(&task_group_lock
, flags
);
8721 for_each_possible_cpu(i
)
8722 register_fair_sched_group(tg
, i
);
8723 list_add_rcu(&tg
->siblings
, &tg
->parent
->children
);
8724 spin_unlock_irqrestore(&task_group_lock
, flags
);
8726 mutex_unlock(&shares_mutex
);
8730 unsigned long sched_group_shares(struct task_group
*tg
)
8736 #ifdef CONFIG_RT_GROUP_SCHED
8738 * Ensure that the real time constraints are schedulable.
8740 static DEFINE_MUTEX(rt_constraints_mutex
);
8742 static unsigned long to_ratio(u64 period
, u64 runtime
)
8744 if (runtime
== RUNTIME_INF
)
8747 return div64_u64(runtime
<< 16, period
);
8750 #ifdef CONFIG_CGROUP_SCHED
8751 static int __rt_schedulable(struct task_group
*tg
, u64 period
, u64 runtime
)
8753 struct task_group
*tgi
, *parent
= tg
->parent
;
8754 unsigned long total
= 0;
8757 if (global_rt_period() < period
)
8760 return to_ratio(period
, runtime
) <
8761 to_ratio(global_rt_period(), global_rt_runtime());
8764 if (ktime_to_ns(parent
->rt_bandwidth
.rt_period
) < period
)
8768 list_for_each_entry_rcu(tgi
, &parent
->children
, siblings
) {
8772 total
+= to_ratio(ktime_to_ns(tgi
->rt_bandwidth
.rt_period
),
8773 tgi
->rt_bandwidth
.rt_runtime
);
8777 return total
+ to_ratio(period
, runtime
) <
8778 to_ratio(ktime_to_ns(parent
->rt_bandwidth
.rt_period
),
8779 parent
->rt_bandwidth
.rt_runtime
);
8781 #elif defined CONFIG_USER_SCHED
8782 static int __rt_schedulable(struct task_group
*tg
, u64 period
, u64 runtime
)
8784 struct task_group
*tgi
;
8785 unsigned long total
= 0;
8786 unsigned long global_ratio
=
8787 to_ratio(global_rt_period(), global_rt_runtime());
8790 list_for_each_entry_rcu(tgi
, &task_groups
, list
) {
8794 total
+= to_ratio(ktime_to_ns(tgi
->rt_bandwidth
.rt_period
),
8795 tgi
->rt_bandwidth
.rt_runtime
);
8799 return total
+ to_ratio(period
, runtime
) < global_ratio
;
8803 /* Must be called with tasklist_lock held */
8804 static inline int tg_has_rt_tasks(struct task_group
*tg
)
8806 struct task_struct
*g
, *p
;
8807 do_each_thread(g
, p
) {
8808 if (rt_task(p
) && rt_rq_of_se(&p
->rt
)->tg
== tg
)
8810 } while_each_thread(g
, p
);
8814 static int tg_set_bandwidth(struct task_group
*tg
,
8815 u64 rt_period
, u64 rt_runtime
)
8819 mutex_lock(&rt_constraints_mutex
);
8820 read_lock(&tasklist_lock
);
8821 if (rt_runtime
== 0 && tg_has_rt_tasks(tg
)) {
8825 if (!__rt_schedulable(tg
, rt_period
, rt_runtime
)) {
8830 spin_lock_irq(&tg
->rt_bandwidth
.rt_runtime_lock
);
8831 tg
->rt_bandwidth
.rt_period
= ns_to_ktime(rt_period
);
8832 tg
->rt_bandwidth
.rt_runtime
= rt_runtime
;
8834 for_each_possible_cpu(i
) {
8835 struct rt_rq
*rt_rq
= tg
->rt_rq
[i
];
8837 spin_lock(&rt_rq
->rt_runtime_lock
);
8838 rt_rq
->rt_runtime
= rt_runtime
;
8839 spin_unlock(&rt_rq
->rt_runtime_lock
);
8841 spin_unlock_irq(&tg
->rt_bandwidth
.rt_runtime_lock
);
8843 read_unlock(&tasklist_lock
);
8844 mutex_unlock(&rt_constraints_mutex
);
8849 int sched_group_set_rt_runtime(struct task_group
*tg
, long rt_runtime_us
)
8851 u64 rt_runtime
, rt_period
;
8853 rt_period
= ktime_to_ns(tg
->rt_bandwidth
.rt_period
);
8854 rt_runtime
= (u64
)rt_runtime_us
* NSEC_PER_USEC
;
8855 if (rt_runtime_us
< 0)
8856 rt_runtime
= RUNTIME_INF
;
8858 return tg_set_bandwidth(tg
, rt_period
, rt_runtime
);
8861 long sched_group_rt_runtime(struct task_group
*tg
)
8865 if (tg
->rt_bandwidth
.rt_runtime
== RUNTIME_INF
)
8868 rt_runtime_us
= tg
->rt_bandwidth
.rt_runtime
;
8869 do_div(rt_runtime_us
, NSEC_PER_USEC
);
8870 return rt_runtime_us
;
8873 int sched_group_set_rt_period(struct task_group
*tg
, long rt_period_us
)
8875 u64 rt_runtime
, rt_period
;
8877 rt_period
= (u64
)rt_period_us
* NSEC_PER_USEC
;
8878 rt_runtime
= tg
->rt_bandwidth
.rt_runtime
;
8880 return tg_set_bandwidth(tg
, rt_period
, rt_runtime
);
8883 long sched_group_rt_period(struct task_group
*tg
)
8887 rt_period_us
= ktime_to_ns(tg
->rt_bandwidth
.rt_period
);
8888 do_div(rt_period_us
, NSEC_PER_USEC
);
8889 return rt_period_us
;
8892 static int sched_rt_global_constraints(void)
8896 mutex_lock(&rt_constraints_mutex
);
8897 if (!__rt_schedulable(NULL
, 1, 0))
8899 mutex_unlock(&rt_constraints_mutex
);
8904 static int sched_rt_global_constraints(void)
8906 unsigned long flags
;
8909 spin_lock_irqsave(&def_rt_bandwidth
.rt_runtime_lock
, flags
);
8910 for_each_possible_cpu(i
) {
8911 struct rt_rq
*rt_rq
= &cpu_rq(i
)->rt
;
8913 spin_lock(&rt_rq
->rt_runtime_lock
);
8914 rt_rq
->rt_runtime
= global_rt_runtime();
8915 spin_unlock(&rt_rq
->rt_runtime_lock
);
8917 spin_unlock_irqrestore(&def_rt_bandwidth
.rt_runtime_lock
, flags
);
8923 int sched_rt_handler(struct ctl_table
*table
, int write
,
8924 struct file
*filp
, void __user
*buffer
, size_t *lenp
,
8928 int old_period
, old_runtime
;
8929 static DEFINE_MUTEX(mutex
);
8932 old_period
= sysctl_sched_rt_period
;
8933 old_runtime
= sysctl_sched_rt_runtime
;
8935 ret
= proc_dointvec(table
, write
, filp
, buffer
, lenp
, ppos
);
8937 if (!ret
&& write
) {
8938 ret
= sched_rt_global_constraints();
8940 sysctl_sched_rt_period
= old_period
;
8941 sysctl_sched_rt_runtime
= old_runtime
;
8943 def_rt_bandwidth
.rt_runtime
= global_rt_runtime();
8944 def_rt_bandwidth
.rt_period
=
8945 ns_to_ktime(global_rt_period());
8948 mutex_unlock(&mutex
);
8953 #ifdef CONFIG_CGROUP_SCHED
8955 /* return corresponding task_group object of a cgroup */
8956 static inline struct task_group
*cgroup_tg(struct cgroup
*cgrp
)
8958 return container_of(cgroup_subsys_state(cgrp
, cpu_cgroup_subsys_id
),
8959 struct task_group
, css
);
8962 static struct cgroup_subsys_state
*
8963 cpu_cgroup_create(struct cgroup_subsys
*ss
, struct cgroup
*cgrp
)
8965 struct task_group
*tg
, *parent
;
8967 if (!cgrp
->parent
) {
8968 /* This is early initialization for the top cgroup */
8969 init_task_group
.css
.cgroup
= cgrp
;
8970 return &init_task_group
.css
;
8973 parent
= cgroup_tg(cgrp
->parent
);
8974 tg
= sched_create_group(parent
);
8976 return ERR_PTR(-ENOMEM
);
8978 /* Bind the cgroup to task_group object we just created */
8979 tg
->css
.cgroup
= cgrp
;
8985 cpu_cgroup_destroy(struct cgroup_subsys
*ss
, struct cgroup
*cgrp
)
8987 struct task_group
*tg
= cgroup_tg(cgrp
);
8989 sched_destroy_group(tg
);
8993 cpu_cgroup_can_attach(struct cgroup_subsys
*ss
, struct cgroup
*cgrp
,
8994 struct task_struct
*tsk
)
8996 #ifdef CONFIG_RT_GROUP_SCHED
8997 /* Don't accept realtime tasks when there is no way for them to run */
8998 if (rt_task(tsk
) && cgroup_tg(cgrp
)->rt_bandwidth
.rt_runtime
== 0)
9001 /* We don't support RT-tasks being in separate groups */
9002 if (tsk
->sched_class
!= &fair_sched_class
)
9010 cpu_cgroup_attach(struct cgroup_subsys
*ss
, struct cgroup
*cgrp
,
9011 struct cgroup
*old_cont
, struct task_struct
*tsk
)
9013 sched_move_task(tsk
);
9016 #ifdef CONFIG_FAIR_GROUP_SCHED
9017 static int cpu_shares_write_u64(struct cgroup
*cgrp
, struct cftype
*cftype
,
9020 return sched_group_set_shares(cgroup_tg(cgrp
), shareval
);
9023 static u64
cpu_shares_read_u64(struct cgroup
*cgrp
, struct cftype
*cft
)
9025 struct task_group
*tg
= cgroup_tg(cgrp
);
9027 return (u64
) tg
->shares
;
9031 #ifdef CONFIG_RT_GROUP_SCHED
9032 static int cpu_rt_runtime_write(struct cgroup
*cgrp
, struct cftype
*cft
,
9035 return sched_group_set_rt_runtime(cgroup_tg(cgrp
), val
);
9038 static s64
cpu_rt_runtime_read(struct cgroup
*cgrp
, struct cftype
*cft
)
9040 return sched_group_rt_runtime(cgroup_tg(cgrp
));
9043 static int cpu_rt_period_write_uint(struct cgroup
*cgrp
, struct cftype
*cftype
,
9046 return sched_group_set_rt_period(cgroup_tg(cgrp
), rt_period_us
);
9049 static u64
cpu_rt_period_read_uint(struct cgroup
*cgrp
, struct cftype
*cft
)
9051 return sched_group_rt_period(cgroup_tg(cgrp
));
9055 static struct cftype cpu_files
[] = {
9056 #ifdef CONFIG_FAIR_GROUP_SCHED
9059 .read_u64
= cpu_shares_read_u64
,
9060 .write_u64
= cpu_shares_write_u64
,
9063 #ifdef CONFIG_RT_GROUP_SCHED
9065 .name
= "rt_runtime_us",
9066 .read_s64
= cpu_rt_runtime_read
,
9067 .write_s64
= cpu_rt_runtime_write
,
9070 .name
= "rt_period_us",
9071 .read_u64
= cpu_rt_period_read_uint
,
9072 .write_u64
= cpu_rt_period_write_uint
,
9077 static int cpu_cgroup_populate(struct cgroup_subsys
*ss
, struct cgroup
*cont
)
9079 return cgroup_add_files(cont
, ss
, cpu_files
, ARRAY_SIZE(cpu_files
));
9082 struct cgroup_subsys cpu_cgroup_subsys
= {
9084 .create
= cpu_cgroup_create
,
9085 .destroy
= cpu_cgroup_destroy
,
9086 .can_attach
= cpu_cgroup_can_attach
,
9087 .attach
= cpu_cgroup_attach
,
9088 .populate
= cpu_cgroup_populate
,
9089 .subsys_id
= cpu_cgroup_subsys_id
,
9093 #endif /* CONFIG_CGROUP_SCHED */
9095 #ifdef CONFIG_CGROUP_CPUACCT
9098 * CPU accounting code for task groups.
9100 * Based on the work by Paul Menage (menage@google.com) and Balbir Singh
9101 * (balbir@in.ibm.com).
9104 /* track cpu usage of a group of tasks */
9106 struct cgroup_subsys_state css
;
9107 /* cpuusage holds pointer to a u64-type object on every cpu */
9111 struct cgroup_subsys cpuacct_subsys
;
9113 /* return cpu accounting group corresponding to this container */
9114 static inline struct cpuacct
*cgroup_ca(struct cgroup
*cgrp
)
9116 return container_of(cgroup_subsys_state(cgrp
, cpuacct_subsys_id
),
9117 struct cpuacct
, css
);
9120 /* return cpu accounting group to which this task belongs */
9121 static inline struct cpuacct
*task_ca(struct task_struct
*tsk
)
9123 return container_of(task_subsys_state(tsk
, cpuacct_subsys_id
),
9124 struct cpuacct
, css
);
9127 /* create a new cpu accounting group */
9128 static struct cgroup_subsys_state
*cpuacct_create(
9129 struct cgroup_subsys
*ss
, struct cgroup
*cgrp
)
9131 struct cpuacct
*ca
= kzalloc(sizeof(*ca
), GFP_KERNEL
);
9134 return ERR_PTR(-ENOMEM
);
9136 ca
->cpuusage
= alloc_percpu(u64
);
9137 if (!ca
->cpuusage
) {
9139 return ERR_PTR(-ENOMEM
);
9145 /* destroy an existing cpu accounting group */
9147 cpuacct_destroy(struct cgroup_subsys
*ss
, struct cgroup
*cgrp
)
9149 struct cpuacct
*ca
= cgroup_ca(cgrp
);
9151 free_percpu(ca
->cpuusage
);
9155 /* return total cpu usage (in nanoseconds) of a group */
9156 static u64
cpuusage_read(struct cgroup
*cgrp
, struct cftype
*cft
)
9158 struct cpuacct
*ca
= cgroup_ca(cgrp
);
9159 u64 totalcpuusage
= 0;
9162 for_each_possible_cpu(i
) {
9163 u64
*cpuusage
= percpu_ptr(ca
->cpuusage
, i
);
9166 * Take rq->lock to make 64-bit addition safe on 32-bit
9169 spin_lock_irq(&cpu_rq(i
)->lock
);
9170 totalcpuusage
+= *cpuusage
;
9171 spin_unlock_irq(&cpu_rq(i
)->lock
);
9174 return totalcpuusage
;
9177 static int cpuusage_write(struct cgroup
*cgrp
, struct cftype
*cftype
,
9180 struct cpuacct
*ca
= cgroup_ca(cgrp
);
9189 for_each_possible_cpu(i
) {
9190 u64
*cpuusage
= percpu_ptr(ca
->cpuusage
, i
);
9192 spin_lock_irq(&cpu_rq(i
)->lock
);
9194 spin_unlock_irq(&cpu_rq(i
)->lock
);
9200 static struct cftype files
[] = {
9203 .read_u64
= cpuusage_read
,
9204 .write_u64
= cpuusage_write
,
9208 static int cpuacct_populate(struct cgroup_subsys
*ss
, struct cgroup
*cgrp
)
9210 return cgroup_add_files(cgrp
, ss
, files
, ARRAY_SIZE(files
));
9214 * charge this task's execution time to its accounting group.
9216 * called with rq->lock held.
9218 static void cpuacct_charge(struct task_struct
*tsk
, u64 cputime
)
9222 if (!cpuacct_subsys
.active
)
9227 u64
*cpuusage
= percpu_ptr(ca
->cpuusage
, task_cpu(tsk
));
9229 *cpuusage
+= cputime
;
9233 struct cgroup_subsys cpuacct_subsys
= {
9235 .create
= cpuacct_create
,
9236 .destroy
= cpuacct_destroy
,
9237 .populate
= cpuacct_populate
,
9238 .subsys_id
= cpuacct_subsys_id
,
9240 #endif /* CONFIG_CGROUP_CPUACCT */