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>
75 #include <asm/irq_regs.h>
78 * Scheduler clock - returns current time in nanosec units.
79 * This is default implementation.
80 * Architectures and sub-architectures can override this.
82 unsigned long long __attribute__((weak
)) sched_clock(void)
84 return (unsigned long long)jiffies
* (NSEC_PER_SEC
/ HZ
);
88 * Convert user-nice values [ -20 ... 0 ... 19 ]
89 * to static priority [ MAX_RT_PRIO..MAX_PRIO-1 ],
92 #define NICE_TO_PRIO(nice) (MAX_RT_PRIO + (nice) + 20)
93 #define PRIO_TO_NICE(prio) ((prio) - MAX_RT_PRIO - 20)
94 #define TASK_NICE(p) PRIO_TO_NICE((p)->static_prio)
97 * 'User priority' is the nice value converted to something we
98 * can work with better when scaling various scheduler parameters,
99 * it's a [ 0 ... 39 ] range.
101 #define USER_PRIO(p) ((p)-MAX_RT_PRIO)
102 #define TASK_USER_PRIO(p) USER_PRIO((p)->static_prio)
103 #define MAX_USER_PRIO (USER_PRIO(MAX_PRIO))
106 * Helpers for converting nanosecond timing to jiffy resolution
108 #define NS_TO_JIFFIES(TIME) ((unsigned long)(TIME) / (NSEC_PER_SEC / HZ))
110 #define NICE_0_LOAD SCHED_LOAD_SCALE
111 #define NICE_0_SHIFT SCHED_LOAD_SHIFT
114 * These are the 'tuning knobs' of the scheduler:
116 * default timeslice is 100 msecs (used only for SCHED_RR tasks).
117 * Timeslices get refilled after they expire.
119 #define DEF_TIMESLICE (100 * HZ / 1000)
122 * single value that denotes runtime == period, ie unlimited time.
124 #define RUNTIME_INF ((u64)~0ULL)
128 * Divide a load by a sched group cpu_power : (load / sg->__cpu_power)
129 * Since cpu_power is a 'constant', we can use a reciprocal divide.
131 static inline u32
sg_div_cpu_power(const struct sched_group
*sg
, u32 load
)
133 return reciprocal_divide(load
, sg
->reciprocal_cpu_power
);
137 * Each time a sched group cpu_power is changed,
138 * we must compute its reciprocal value
140 static inline void sg_inc_cpu_power(struct sched_group
*sg
, u32 val
)
142 sg
->__cpu_power
+= val
;
143 sg
->reciprocal_cpu_power
= reciprocal_value(sg
->__cpu_power
);
147 static inline int rt_policy(int policy
)
149 if (unlikely(policy
== SCHED_FIFO
) || unlikely(policy
== SCHED_RR
))
154 static inline int task_has_rt_policy(struct task_struct
*p
)
156 return rt_policy(p
->policy
);
160 * This is the priority-queue data structure of the RT scheduling class:
162 struct rt_prio_array
{
163 DECLARE_BITMAP(bitmap
, MAX_RT_PRIO
+1); /* include 1 bit for delimiter */
164 struct list_head queue
[MAX_RT_PRIO
];
167 struct rt_bandwidth
{
168 /* nests inside the rq lock: */
169 spinlock_t rt_runtime_lock
;
172 struct hrtimer rt_period_timer
;
175 static struct rt_bandwidth def_rt_bandwidth
;
177 static int do_sched_rt_period_timer(struct rt_bandwidth
*rt_b
, int overrun
);
179 static enum hrtimer_restart
sched_rt_period_timer(struct hrtimer
*timer
)
181 struct rt_bandwidth
*rt_b
=
182 container_of(timer
, struct rt_bandwidth
, rt_period_timer
);
188 now
= hrtimer_cb_get_time(timer
);
189 overrun
= hrtimer_forward(timer
, now
, rt_b
->rt_period
);
194 idle
= do_sched_rt_period_timer(rt_b
, overrun
);
197 return idle
? HRTIMER_NORESTART
: HRTIMER_RESTART
;
201 void init_rt_bandwidth(struct rt_bandwidth
*rt_b
, u64 period
, u64 runtime
)
203 rt_b
->rt_period
= ns_to_ktime(period
);
204 rt_b
->rt_runtime
= runtime
;
206 spin_lock_init(&rt_b
->rt_runtime_lock
);
208 hrtimer_init(&rt_b
->rt_period_timer
,
209 CLOCK_MONOTONIC
, HRTIMER_MODE_REL
);
210 rt_b
->rt_period_timer
.function
= sched_rt_period_timer
;
211 rt_b
->rt_period_timer
.cb_mode
= HRTIMER_CB_IRQSAFE_NO_SOFTIRQ
;
214 static void start_rt_bandwidth(struct rt_bandwidth
*rt_b
)
218 if (rt_b
->rt_runtime
== RUNTIME_INF
)
221 if (hrtimer_active(&rt_b
->rt_period_timer
))
224 spin_lock(&rt_b
->rt_runtime_lock
);
226 if (hrtimer_active(&rt_b
->rt_period_timer
))
229 now
= hrtimer_cb_get_time(&rt_b
->rt_period_timer
);
230 hrtimer_forward(&rt_b
->rt_period_timer
, now
, rt_b
->rt_period
);
231 hrtimer_start(&rt_b
->rt_period_timer
,
232 rt_b
->rt_period_timer
.expires
,
235 spin_unlock(&rt_b
->rt_runtime_lock
);
238 #ifdef CONFIG_RT_GROUP_SCHED
239 static void destroy_rt_bandwidth(struct rt_bandwidth
*rt_b
)
241 hrtimer_cancel(&rt_b
->rt_period_timer
);
245 #ifdef CONFIG_GROUP_SCHED
247 #include <linux/cgroup.h>
251 static LIST_HEAD(task_groups
);
253 /* task group related information */
255 #ifdef CONFIG_CGROUP_SCHED
256 struct cgroup_subsys_state css
;
259 #ifdef CONFIG_FAIR_GROUP_SCHED
260 /* schedulable entities of this group on each cpu */
261 struct sched_entity
**se
;
262 /* runqueue "owned" by this group on each cpu */
263 struct cfs_rq
**cfs_rq
;
264 unsigned long shares
;
267 #ifdef CONFIG_RT_GROUP_SCHED
268 struct sched_rt_entity
**rt_se
;
269 struct rt_rq
**rt_rq
;
271 struct rt_bandwidth rt_bandwidth
;
275 struct list_head list
;
277 struct task_group
*parent
;
278 struct list_head siblings
;
279 struct list_head children
;
282 #ifdef CONFIG_USER_SCHED
286 * Every UID task group (including init_task_group aka UID-0) will
287 * be a child to this group.
289 struct task_group root_task_group
;
291 #ifdef CONFIG_FAIR_GROUP_SCHED
292 /* Default task group's sched entity on each cpu */
293 static DEFINE_PER_CPU(struct sched_entity
, init_sched_entity
);
294 /* Default task group's cfs_rq on each cpu */
295 static DEFINE_PER_CPU(struct cfs_rq
, init_cfs_rq
) ____cacheline_aligned_in_smp
;
298 #ifdef CONFIG_RT_GROUP_SCHED
299 static DEFINE_PER_CPU(struct sched_rt_entity
, init_sched_rt_entity
);
300 static DEFINE_PER_CPU(struct rt_rq
, init_rt_rq
) ____cacheline_aligned_in_smp
;
303 #define root_task_group init_task_group
306 /* task_group_lock serializes add/remove of task groups and also changes to
307 * a task group's cpu shares.
309 static DEFINE_SPINLOCK(task_group_lock
);
311 /* doms_cur_mutex serializes access to doms_cur[] array */
312 static DEFINE_MUTEX(doms_cur_mutex
);
314 #ifdef CONFIG_FAIR_GROUP_SCHED
315 #ifdef CONFIG_USER_SCHED
316 # define INIT_TASK_GROUP_LOAD (2*NICE_0_LOAD)
318 # define INIT_TASK_GROUP_LOAD NICE_0_LOAD
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
];
361 static inline void lock_doms_cur(void)
363 mutex_lock(&doms_cur_mutex
);
366 static inline void unlock_doms_cur(void)
368 mutex_unlock(&doms_cur_mutex
);
373 static inline void set_task_rq(struct task_struct
*p
, unsigned int cpu
) { }
374 static inline void lock_doms_cur(void) { }
375 static inline void unlock_doms_cur(void) { }
377 #endif /* CONFIG_GROUP_SCHED */
379 /* CFS-related fields in a runqueue */
381 struct load_weight load
;
382 unsigned long nr_running
;
387 struct rb_root tasks_timeline
;
388 struct rb_node
*rb_leftmost
;
390 struct list_head tasks
;
391 struct list_head
*balance_iterator
;
394 * 'curr' points to currently running entity on this cfs_rq.
395 * It is set to NULL otherwise (i.e when none are currently running).
397 struct sched_entity
*curr
, *next
;
399 unsigned long nr_spread_over
;
401 #ifdef CONFIG_FAIR_GROUP_SCHED
402 struct rq
*rq
; /* cpu runqueue to which this cfs_rq is attached */
405 * leaf cfs_rqs are those that hold tasks (lowest schedulable entity in
406 * a hierarchy). Non-leaf lrqs hold other higher schedulable entities
407 * (like users, containers etc.)
409 * leaf_cfs_rq_list ties together list of leaf cfs_rq's in a cpu. This
410 * list is used during load balance.
412 struct list_head leaf_cfs_rq_list
;
413 struct task_group
*tg
; /* group that "owns" this runqueue */
416 unsigned long task_weight
;
417 unsigned long shares
;
419 * We need space to build a sched_domain wide view of the full task
420 * group tree, in order to avoid depending on dynamic memory allocation
421 * during the load balancing we place this in the per cpu task group
422 * hierarchy. This limits the load balancing to one instance per cpu,
423 * but more should not be needed anyway.
425 struct aggregate_struct
{
427 * load = weight(cpus) * f(tg)
429 * Where f(tg) is the recursive weight fraction assigned to
435 * part of the group weight distributed to this span.
437 unsigned long shares
;
440 * The sum of all runqueue weights within this span.
442 unsigned long rq_weight
;
445 * Weight contributed by tasks; this is the part we can
446 * influence by moving tasks around.
448 unsigned long task_weight
;
454 /* Real-Time classes' related field in a runqueue: */
456 struct rt_prio_array active
;
457 unsigned long rt_nr_running
;
458 #if defined CONFIG_SMP || defined CONFIG_RT_GROUP_SCHED
459 int highest_prio
; /* highest queued rt task prio */
462 unsigned long rt_nr_migratory
;
468 /* Nests inside the rq lock: */
469 spinlock_t rt_runtime_lock
;
471 #ifdef CONFIG_RT_GROUP_SCHED
472 unsigned long rt_nr_boosted
;
475 struct list_head leaf_rt_rq_list
;
476 struct task_group
*tg
;
477 struct sched_rt_entity
*rt_se
;
484 * We add the notion of a root-domain which will be used to define per-domain
485 * variables. Each exclusive cpuset essentially defines an island domain by
486 * fully partitioning the member cpus from any other cpuset. Whenever a new
487 * exclusive cpuset is created, we also create and attach a new root-domain
497 * The "RT overload" flag: it gets set if a CPU has more than
498 * one runnable RT task.
505 * By default the system creates a single root-domain with all cpus as
506 * members (mimicking the global state we have today).
508 static struct root_domain def_root_domain
;
513 * This is the main, per-CPU runqueue data structure.
515 * Locking rule: those places that want to lock multiple runqueues
516 * (such as the load balancing or the thread migration code), lock
517 * acquire operations must be ordered by ascending &runqueue.
524 * nr_running and cpu_load should be in the same cacheline because
525 * remote CPUs use both these fields when doing load calculation.
527 unsigned long nr_running
;
528 #define CPU_LOAD_IDX_MAX 5
529 unsigned long cpu_load
[CPU_LOAD_IDX_MAX
];
530 unsigned char idle_at_tick
;
532 unsigned long last_tick_seen
;
533 unsigned char in_nohz_recently
;
535 /* capture load from *all* tasks on this cpu: */
536 struct load_weight load
;
537 unsigned long nr_load_updates
;
543 #ifdef CONFIG_FAIR_GROUP_SCHED
544 /* list of leaf cfs_rq on this cpu: */
545 struct list_head leaf_cfs_rq_list
;
547 #ifdef CONFIG_RT_GROUP_SCHED
548 struct list_head leaf_rt_rq_list
;
552 * This is part of a global counter where only the total sum
553 * over all CPUs matters. A task can increase this counter on
554 * one CPU and if it got migrated afterwards it may decrease
555 * it on another CPU. Always updated under the runqueue lock:
557 unsigned long nr_uninterruptible
;
559 struct task_struct
*curr
, *idle
;
560 unsigned long next_balance
;
561 struct mm_struct
*prev_mm
;
563 u64 clock
, prev_clock_raw
;
566 unsigned int clock_warps
, clock_overflows
, clock_underflows
;
568 unsigned int clock_deep_idle_events
;
574 struct root_domain
*rd
;
575 struct sched_domain
*sd
;
577 /* For active balancing */
580 /* cpu of this runqueue: */
583 struct task_struct
*migration_thread
;
584 struct list_head migration_queue
;
587 #ifdef CONFIG_SCHED_HRTICK
588 unsigned long hrtick_flags
;
589 ktime_t hrtick_expire
;
590 struct hrtimer hrtick_timer
;
593 #ifdef CONFIG_SCHEDSTATS
595 struct sched_info rq_sched_info
;
597 /* sys_sched_yield() stats */
598 unsigned int yld_exp_empty
;
599 unsigned int yld_act_empty
;
600 unsigned int yld_both_empty
;
601 unsigned int yld_count
;
603 /* schedule() stats */
604 unsigned int sched_switch
;
605 unsigned int sched_count
;
606 unsigned int sched_goidle
;
608 /* try_to_wake_up() stats */
609 unsigned int ttwu_count
;
610 unsigned int ttwu_local
;
613 unsigned int bkl_count
;
615 struct lock_class_key rq_lock_key
;
618 static DEFINE_PER_CPU_SHARED_ALIGNED(struct rq
, runqueues
);
620 static inline void check_preempt_curr(struct rq
*rq
, struct task_struct
*p
)
622 rq
->curr
->sched_class
->check_preempt_curr(rq
, p
);
625 static inline int cpu_of(struct rq
*rq
)
635 static inline bool nohz_on(int cpu
)
637 return tick_get_tick_sched(cpu
)->nohz_mode
!= NOHZ_MODE_INACTIVE
;
640 static inline u64
max_skipped_ticks(struct rq
*rq
)
642 return nohz_on(cpu_of(rq
)) ? jiffies
- rq
->last_tick_seen
+ 2 : 1;
645 static inline void update_last_tick_seen(struct rq
*rq
)
647 rq
->last_tick_seen
= jiffies
;
650 static inline u64
max_skipped_ticks(struct rq
*rq
)
655 static inline void update_last_tick_seen(struct rq
*rq
)
661 * Update the per-runqueue clock, as finegrained as the platform can give
662 * us, but without assuming monotonicity, etc.:
664 static void __update_rq_clock(struct rq
*rq
)
666 u64 prev_raw
= rq
->prev_clock_raw
;
667 u64 now
= sched_clock();
668 s64 delta
= now
- prev_raw
;
669 u64 clock
= rq
->clock
;
671 #ifdef CONFIG_SCHED_DEBUG
672 WARN_ON_ONCE(cpu_of(rq
) != smp_processor_id());
675 * Protect against sched_clock() occasionally going backwards:
677 if (unlikely(delta
< 0)) {
682 * Catch too large forward jumps too:
684 u64 max_jump
= max_skipped_ticks(rq
) * TICK_NSEC
;
685 u64 max_time
= rq
->tick_timestamp
+ max_jump
;
687 if (unlikely(clock
+ delta
> max_time
)) {
688 if (clock
< max_time
)
692 rq
->clock_overflows
++;
694 if (unlikely(delta
> rq
->clock_max_delta
))
695 rq
->clock_max_delta
= delta
;
700 rq
->prev_clock_raw
= now
;
704 static void update_rq_clock(struct rq
*rq
)
706 if (likely(smp_processor_id() == cpu_of(rq
)))
707 __update_rq_clock(rq
);
711 * The domain tree (rq->sd) is protected by RCU's quiescent state transition.
712 * See detach_destroy_domains: synchronize_sched for details.
714 * The domain tree of any CPU may only be accessed from within
715 * preempt-disabled sections.
717 #define for_each_domain(cpu, __sd) \
718 for (__sd = rcu_dereference(cpu_rq(cpu)->sd); __sd; __sd = __sd->parent)
720 #define cpu_rq(cpu) (&per_cpu(runqueues, (cpu)))
721 #define this_rq() (&__get_cpu_var(runqueues))
722 #define task_rq(p) cpu_rq(task_cpu(p))
723 #define cpu_curr(cpu) (cpu_rq(cpu)->curr)
726 * Tunables that become constants when CONFIG_SCHED_DEBUG is off:
728 #ifdef CONFIG_SCHED_DEBUG
729 # define const_debug __read_mostly
731 # define const_debug static const
735 * Debugging: various feature bits
738 #define SCHED_FEAT(name, enabled) \
739 __SCHED_FEAT_##name ,
742 #include "sched_features.h"
747 #define SCHED_FEAT(name, enabled) \
748 (1UL << __SCHED_FEAT_##name) * enabled |
750 const_debug
unsigned int sysctl_sched_features
=
751 #include "sched_features.h"
756 #ifdef CONFIG_SCHED_DEBUG
757 #define SCHED_FEAT(name, enabled) \
760 static __read_mostly
char *sched_feat_names
[] = {
761 #include "sched_features.h"
767 static int sched_feat_open(struct inode
*inode
, struct file
*filp
)
769 filp
->private_data
= inode
->i_private
;
774 sched_feat_read(struct file
*filp
, char __user
*ubuf
,
775 size_t cnt
, loff_t
*ppos
)
782 for (i
= 0; sched_feat_names
[i
]; i
++) {
783 len
+= strlen(sched_feat_names
[i
]);
787 buf
= kmalloc(len
+ 2, GFP_KERNEL
);
791 for (i
= 0; sched_feat_names
[i
]; i
++) {
792 if (sysctl_sched_features
& (1UL << i
))
793 r
+= sprintf(buf
+ r
, "%s ", sched_feat_names
[i
]);
795 r
+= sprintf(buf
+ r
, "NO_%s ", sched_feat_names
[i
]);
798 r
+= sprintf(buf
+ r
, "\n");
799 WARN_ON(r
>= len
+ 2);
801 r
= simple_read_from_buffer(ubuf
, cnt
, ppos
, buf
, r
);
809 sched_feat_write(struct file
*filp
, const char __user
*ubuf
,
810 size_t cnt
, loff_t
*ppos
)
820 if (copy_from_user(&buf
, ubuf
, cnt
))
825 if (strncmp(buf
, "NO_", 3) == 0) {
830 for (i
= 0; sched_feat_names
[i
]; i
++) {
831 int len
= strlen(sched_feat_names
[i
]);
833 if (strncmp(cmp
, sched_feat_names
[i
], len
) == 0) {
835 sysctl_sched_features
&= ~(1UL << i
);
837 sysctl_sched_features
|= (1UL << i
);
842 if (!sched_feat_names
[i
])
850 static struct file_operations sched_feat_fops
= {
851 .open
= sched_feat_open
,
852 .read
= sched_feat_read
,
853 .write
= sched_feat_write
,
856 static __init
int sched_init_debug(void)
858 debugfs_create_file("sched_features", 0644, NULL
, NULL
,
863 late_initcall(sched_init_debug
);
867 #define sched_feat(x) (sysctl_sched_features & (1UL << __SCHED_FEAT_##x))
870 * Number of tasks to iterate in a single balance run.
871 * Limited because this is done with IRQs disabled.
873 const_debug
unsigned int sysctl_sched_nr_migrate
= 32;
876 * period over which we measure -rt task cpu usage in us.
879 unsigned int sysctl_sched_rt_period
= 1000000;
881 static __read_mostly
int scheduler_running
;
884 * part of the period that we allow rt tasks to run in us.
887 int sysctl_sched_rt_runtime
= 950000;
889 static inline u64
global_rt_period(void)
891 return (u64
)sysctl_sched_rt_period
* NSEC_PER_USEC
;
894 static inline u64
global_rt_runtime(void)
896 if (sysctl_sched_rt_period
< 0)
899 return (u64
)sysctl_sched_rt_runtime
* NSEC_PER_USEC
;
902 static const unsigned long long time_sync_thresh
= 100000;
904 static DEFINE_PER_CPU(unsigned long long, time_offset
);
905 static DEFINE_PER_CPU(unsigned long long, prev_cpu_time
);
908 * Global lock which we take every now and then to synchronize
909 * the CPUs time. This method is not warp-safe, but it's good
910 * enough to synchronize slowly diverging time sources and thus
911 * it's good enough for tracing:
913 static DEFINE_SPINLOCK(time_sync_lock
);
914 static unsigned long long prev_global_time
;
916 static unsigned long long __sync_cpu_clock(cycles_t time
, int cpu
)
920 spin_lock_irqsave(&time_sync_lock
, flags
);
922 if (time
< prev_global_time
) {
923 per_cpu(time_offset
, cpu
) += prev_global_time
- time
;
924 time
= prev_global_time
;
926 prev_global_time
= time
;
929 spin_unlock_irqrestore(&time_sync_lock
, flags
);
934 static unsigned long long __cpu_clock(int cpu
)
936 unsigned long long now
;
941 * Only call sched_clock() if the scheduler has already been
942 * initialized (some code might call cpu_clock() very early):
944 if (unlikely(!scheduler_running
))
947 local_irq_save(flags
);
951 local_irq_restore(flags
);
957 * For kernel-internal use: high-speed (but slightly incorrect) per-cpu
958 * clock constructed from sched_clock():
960 unsigned long long cpu_clock(int cpu
)
962 unsigned long long prev_cpu_time
, time
, delta_time
;
964 prev_cpu_time
= per_cpu(prev_cpu_time
, cpu
);
965 time
= __cpu_clock(cpu
) + per_cpu(time_offset
, cpu
);
966 delta_time
= time
-prev_cpu_time
;
968 if (unlikely(delta_time
> time_sync_thresh
))
969 time
= __sync_cpu_clock(time
, cpu
);
973 EXPORT_SYMBOL_GPL(cpu_clock
);
975 #ifndef prepare_arch_switch
976 # define prepare_arch_switch(next) do { } while (0)
978 #ifndef finish_arch_switch
979 # define finish_arch_switch(prev) do { } while (0)
982 static inline int task_current(struct rq
*rq
, struct task_struct
*p
)
984 return rq
->curr
== p
;
987 #ifndef __ARCH_WANT_UNLOCKED_CTXSW
988 static inline int task_running(struct rq
*rq
, struct task_struct
*p
)
990 return task_current(rq
, p
);
993 static inline void prepare_lock_switch(struct rq
*rq
, struct task_struct
*next
)
997 static inline void finish_lock_switch(struct rq
*rq
, struct task_struct
*prev
)
999 #ifdef CONFIG_DEBUG_SPINLOCK
1000 /* this is a valid case when another task releases the spinlock */
1001 rq
->lock
.owner
= current
;
1004 * If we are tracking spinlock dependencies then we have to
1005 * fix up the runqueue lock - which gets 'carried over' from
1006 * prev into current:
1008 spin_acquire(&rq
->lock
.dep_map
, 0, 0, _THIS_IP_
);
1010 spin_unlock_irq(&rq
->lock
);
1013 #else /* __ARCH_WANT_UNLOCKED_CTXSW */
1014 static inline int task_running(struct rq
*rq
, struct task_struct
*p
)
1019 return task_current(rq
, p
);
1023 static inline void prepare_lock_switch(struct rq
*rq
, struct task_struct
*next
)
1027 * We can optimise this out completely for !SMP, because the
1028 * SMP rebalancing from interrupt is the only thing that cares
1033 #ifdef __ARCH_WANT_INTERRUPTS_ON_CTXSW
1034 spin_unlock_irq(&rq
->lock
);
1036 spin_unlock(&rq
->lock
);
1040 static inline void finish_lock_switch(struct rq
*rq
, struct task_struct
*prev
)
1044 * After ->oncpu is cleared, the task can be moved to a different CPU.
1045 * We must ensure this doesn't happen until the switch is completely
1051 #ifndef __ARCH_WANT_INTERRUPTS_ON_CTXSW
1055 #endif /* __ARCH_WANT_UNLOCKED_CTXSW */
1058 * __task_rq_lock - lock the runqueue a given task resides on.
1059 * Must be called interrupts disabled.
1061 static inline struct rq
*__task_rq_lock(struct task_struct
*p
)
1062 __acquires(rq
->lock
)
1065 struct rq
*rq
= task_rq(p
);
1066 spin_lock(&rq
->lock
);
1067 if (likely(rq
== task_rq(p
)))
1069 spin_unlock(&rq
->lock
);
1074 * task_rq_lock - lock the runqueue a given task resides on and disable
1075 * interrupts. Note the ordering: we can safely lookup the task_rq without
1076 * explicitly disabling preemption.
1078 static struct rq
*task_rq_lock(struct task_struct
*p
, unsigned long *flags
)
1079 __acquires(rq
->lock
)
1084 local_irq_save(*flags
);
1086 spin_lock(&rq
->lock
);
1087 if (likely(rq
== task_rq(p
)))
1089 spin_unlock_irqrestore(&rq
->lock
, *flags
);
1093 static void __task_rq_unlock(struct rq
*rq
)
1094 __releases(rq
->lock
)
1096 spin_unlock(&rq
->lock
);
1099 static inline void task_rq_unlock(struct rq
*rq
, unsigned long *flags
)
1100 __releases(rq
->lock
)
1102 spin_unlock_irqrestore(&rq
->lock
, *flags
);
1106 * this_rq_lock - lock this runqueue and disable interrupts.
1108 static struct rq
*this_rq_lock(void)
1109 __acquires(rq
->lock
)
1113 local_irq_disable();
1115 spin_lock(&rq
->lock
);
1121 * We are going deep-idle (irqs are disabled):
1123 void sched_clock_idle_sleep_event(void)
1125 struct rq
*rq
= cpu_rq(smp_processor_id());
1127 WARN_ON(!irqs_disabled());
1128 spin_lock(&rq
->lock
);
1129 __update_rq_clock(rq
);
1130 spin_unlock(&rq
->lock
);
1131 rq
->clock_deep_idle_events
++;
1133 EXPORT_SYMBOL_GPL(sched_clock_idle_sleep_event
);
1136 * We just idled delta nanoseconds (called with irqs disabled):
1138 void sched_clock_idle_wakeup_event(u64 delta_ns
)
1140 struct rq
*rq
= cpu_rq(smp_processor_id());
1141 u64 now
= sched_clock();
1143 WARN_ON(!irqs_disabled());
1144 rq
->idle_clock
+= delta_ns
;
1146 * Override the previous timestamp and ignore all
1147 * sched_clock() deltas that occured while we idled,
1148 * and use the PM-provided delta_ns to advance the
1151 spin_lock(&rq
->lock
);
1152 rq
->prev_clock_raw
= now
;
1153 rq
->clock
+= delta_ns
;
1154 spin_unlock(&rq
->lock
);
1155 touch_softlockup_watchdog();
1157 EXPORT_SYMBOL_GPL(sched_clock_idle_wakeup_event
);
1159 static void __resched_task(struct task_struct
*p
, int tif_bit
);
1161 static inline void resched_task(struct task_struct
*p
)
1163 __resched_task(p
, TIF_NEED_RESCHED
);
1166 #ifdef CONFIG_SCHED_HRTICK
1168 * Use HR-timers to deliver accurate preemption points.
1170 * Its all a bit involved since we cannot program an hrt while holding the
1171 * rq->lock. So what we do is store a state in in rq->hrtick_* and ask for a
1174 * When we get rescheduled we reprogram the hrtick_timer outside of the
1177 static inline void resched_hrt(struct task_struct
*p
)
1179 __resched_task(p
, TIF_HRTICK_RESCHED
);
1182 static inline void resched_rq(struct rq
*rq
)
1184 unsigned long flags
;
1186 spin_lock_irqsave(&rq
->lock
, flags
);
1187 resched_task(rq
->curr
);
1188 spin_unlock_irqrestore(&rq
->lock
, flags
);
1192 HRTICK_SET
, /* re-programm hrtick_timer */
1193 HRTICK_RESET
, /* not a new slice */
1198 * - enabled by features
1199 * - hrtimer is actually high res
1201 static inline int hrtick_enabled(struct rq
*rq
)
1203 if (!sched_feat(HRTICK
))
1205 return hrtimer_is_hres_active(&rq
->hrtick_timer
);
1209 * Called to set the hrtick timer state.
1211 * called with rq->lock held and irqs disabled
1213 static void hrtick_start(struct rq
*rq
, u64 delay
, int reset
)
1215 assert_spin_locked(&rq
->lock
);
1218 * preempt at: now + delay
1221 ktime_add_ns(rq
->hrtick_timer
.base
->get_time(), delay
);
1223 * indicate we need to program the timer
1225 __set_bit(HRTICK_SET
, &rq
->hrtick_flags
);
1227 __set_bit(HRTICK_RESET
, &rq
->hrtick_flags
);
1230 * New slices are called from the schedule path and don't need a
1231 * forced reschedule.
1234 resched_hrt(rq
->curr
);
1237 static void hrtick_clear(struct rq
*rq
)
1239 if (hrtimer_active(&rq
->hrtick_timer
))
1240 hrtimer_cancel(&rq
->hrtick_timer
);
1244 * Update the timer from the possible pending state.
1246 static void hrtick_set(struct rq
*rq
)
1250 unsigned long flags
;
1252 WARN_ON_ONCE(cpu_of(rq
) != smp_processor_id());
1254 spin_lock_irqsave(&rq
->lock
, flags
);
1255 set
= __test_and_clear_bit(HRTICK_SET
, &rq
->hrtick_flags
);
1256 reset
= __test_and_clear_bit(HRTICK_RESET
, &rq
->hrtick_flags
);
1257 time
= rq
->hrtick_expire
;
1258 clear_thread_flag(TIF_HRTICK_RESCHED
);
1259 spin_unlock_irqrestore(&rq
->lock
, flags
);
1262 hrtimer_start(&rq
->hrtick_timer
, time
, HRTIMER_MODE_ABS
);
1263 if (reset
&& !hrtimer_active(&rq
->hrtick_timer
))
1270 * High-resolution timer tick.
1271 * Runs from hardirq context with interrupts disabled.
1273 static enum hrtimer_restart
hrtick(struct hrtimer
*timer
)
1275 struct rq
*rq
= container_of(timer
, struct rq
, hrtick_timer
);
1277 WARN_ON_ONCE(cpu_of(rq
) != smp_processor_id());
1279 spin_lock(&rq
->lock
);
1280 __update_rq_clock(rq
);
1281 rq
->curr
->sched_class
->task_tick(rq
, rq
->curr
, 1);
1282 spin_unlock(&rq
->lock
);
1284 return HRTIMER_NORESTART
;
1287 static inline void init_rq_hrtick(struct rq
*rq
)
1289 rq
->hrtick_flags
= 0;
1290 hrtimer_init(&rq
->hrtick_timer
, CLOCK_MONOTONIC
, HRTIMER_MODE_REL
);
1291 rq
->hrtick_timer
.function
= hrtick
;
1292 rq
->hrtick_timer
.cb_mode
= HRTIMER_CB_IRQSAFE_NO_SOFTIRQ
;
1295 void hrtick_resched(void)
1298 unsigned long flags
;
1300 if (!test_thread_flag(TIF_HRTICK_RESCHED
))
1303 local_irq_save(flags
);
1304 rq
= cpu_rq(smp_processor_id());
1306 local_irq_restore(flags
);
1309 static inline void hrtick_clear(struct rq
*rq
)
1313 static inline void hrtick_set(struct rq
*rq
)
1317 static inline void init_rq_hrtick(struct rq
*rq
)
1321 void hrtick_resched(void)
1327 * resched_task - mark a task 'to be rescheduled now'.
1329 * On UP this means the setting of the need_resched flag, on SMP it
1330 * might also involve a cross-CPU call to trigger the scheduler on
1335 #ifndef tsk_is_polling
1336 #define tsk_is_polling(t) test_tsk_thread_flag(t, TIF_POLLING_NRFLAG)
1339 static void __resched_task(struct task_struct
*p
, int tif_bit
)
1343 assert_spin_locked(&task_rq(p
)->lock
);
1345 if (unlikely(test_tsk_thread_flag(p
, tif_bit
)))
1348 set_tsk_thread_flag(p
, tif_bit
);
1351 if (cpu
== smp_processor_id())
1354 /* NEED_RESCHED must be visible before we test polling */
1356 if (!tsk_is_polling(p
))
1357 smp_send_reschedule(cpu
);
1360 static void resched_cpu(int cpu
)
1362 struct rq
*rq
= cpu_rq(cpu
);
1363 unsigned long flags
;
1365 if (!spin_trylock_irqsave(&rq
->lock
, flags
))
1367 resched_task(cpu_curr(cpu
));
1368 spin_unlock_irqrestore(&rq
->lock
, flags
);
1373 * When add_timer_on() enqueues a timer into the timer wheel of an
1374 * idle CPU then this timer might expire before the next timer event
1375 * which is scheduled to wake up that CPU. In case of a completely
1376 * idle system the next event might even be infinite time into the
1377 * future. wake_up_idle_cpu() ensures that the CPU is woken up and
1378 * leaves the inner idle loop so the newly added timer is taken into
1379 * account when the CPU goes back to idle and evaluates the timer
1380 * wheel for the next timer event.
1382 void wake_up_idle_cpu(int cpu
)
1384 struct rq
*rq
= cpu_rq(cpu
);
1386 if (cpu
== smp_processor_id())
1390 * This is safe, as this function is called with the timer
1391 * wheel base lock of (cpu) held. When the CPU is on the way
1392 * to idle and has not yet set rq->curr to idle then it will
1393 * be serialized on the timer wheel base lock and take the new
1394 * timer into account automatically.
1396 if (rq
->curr
!= rq
->idle
)
1400 * We can set TIF_RESCHED on the idle task of the other CPU
1401 * lockless. The worst case is that the other CPU runs the
1402 * idle task through an additional NOOP schedule()
1404 set_tsk_thread_flag(rq
->idle
, TIF_NEED_RESCHED
);
1406 /* NEED_RESCHED must be visible before we test polling */
1408 if (!tsk_is_polling(rq
->idle
))
1409 smp_send_reschedule(cpu
);
1414 static void __resched_task(struct task_struct
*p
, int tif_bit
)
1416 assert_spin_locked(&task_rq(p
)->lock
);
1417 set_tsk_thread_flag(p
, tif_bit
);
1421 #if BITS_PER_LONG == 32
1422 # define WMULT_CONST (~0UL)
1424 # define WMULT_CONST (1UL << 32)
1427 #define WMULT_SHIFT 32
1430 * Shift right and round:
1432 #define SRR(x, y) (((x) + (1UL << ((y) - 1))) >> (y))
1435 * delta *= weight / lw
1437 static unsigned long
1438 calc_delta_mine(unsigned long delta_exec
, unsigned long weight
,
1439 struct load_weight
*lw
)
1443 if (!lw
->inv_weight
)
1444 lw
->inv_weight
= 1 + (WMULT_CONST
-lw
->weight
/2)/(lw
->weight
+1);
1446 tmp
= (u64
)delta_exec
* weight
;
1448 * Check whether we'd overflow the 64-bit multiplication:
1450 if (unlikely(tmp
> WMULT_CONST
))
1451 tmp
= SRR(SRR(tmp
, WMULT_SHIFT
/2) * lw
->inv_weight
,
1454 tmp
= SRR(tmp
* lw
->inv_weight
, WMULT_SHIFT
);
1456 return (unsigned long)min(tmp
, (u64
)(unsigned long)LONG_MAX
);
1459 static inline void update_load_add(struct load_weight
*lw
, unsigned long inc
)
1465 static inline void update_load_sub(struct load_weight
*lw
, unsigned long dec
)
1472 * To aid in avoiding the subversion of "niceness" due to uneven distribution
1473 * of tasks with abnormal "nice" values across CPUs the contribution that
1474 * each task makes to its run queue's load is weighted according to its
1475 * scheduling class and "nice" value. For SCHED_NORMAL tasks this is just a
1476 * scaled version of the new time slice allocation that they receive on time
1480 #define WEIGHT_IDLEPRIO 2
1481 #define WMULT_IDLEPRIO (1 << 31)
1484 * Nice levels are multiplicative, with a gentle 10% change for every
1485 * nice level changed. I.e. when a CPU-bound task goes from nice 0 to
1486 * nice 1, it will get ~10% less CPU time than another CPU-bound task
1487 * that remained on nice 0.
1489 * The "10% effect" is relative and cumulative: from _any_ nice level,
1490 * if you go up 1 level, it's -10% CPU usage, if you go down 1 level
1491 * it's +10% CPU usage. (to achieve that we use a multiplier of 1.25.
1492 * If a task goes up by ~10% and another task goes down by ~10% then
1493 * the relative distance between them is ~25%.)
1495 static const int prio_to_weight
[40] = {
1496 /* -20 */ 88761, 71755, 56483, 46273, 36291,
1497 /* -15 */ 29154, 23254, 18705, 14949, 11916,
1498 /* -10 */ 9548, 7620, 6100, 4904, 3906,
1499 /* -5 */ 3121, 2501, 1991, 1586, 1277,
1500 /* 0 */ 1024, 820, 655, 526, 423,
1501 /* 5 */ 335, 272, 215, 172, 137,
1502 /* 10 */ 110, 87, 70, 56, 45,
1503 /* 15 */ 36, 29, 23, 18, 15,
1507 * Inverse (2^32/x) values of the prio_to_weight[] array, precalculated.
1509 * In cases where the weight does not change often, we can use the
1510 * precalculated inverse to speed up arithmetics by turning divisions
1511 * into multiplications:
1513 static const u32 prio_to_wmult
[40] = {
1514 /* -20 */ 48388, 59856, 76040, 92818, 118348,
1515 /* -15 */ 147320, 184698, 229616, 287308, 360437,
1516 /* -10 */ 449829, 563644, 704093, 875809, 1099582,
1517 /* -5 */ 1376151, 1717300, 2157191, 2708050, 3363326,
1518 /* 0 */ 4194304, 5237765, 6557202, 8165337, 10153587,
1519 /* 5 */ 12820798, 15790321, 19976592, 24970740, 31350126,
1520 /* 10 */ 39045157, 49367440, 61356676, 76695844, 95443717,
1521 /* 15 */ 119304647, 148102320, 186737708, 238609294, 286331153,
1524 static void activate_task(struct rq
*rq
, struct task_struct
*p
, int wakeup
);
1527 * runqueue iterator, to support SMP load-balancing between different
1528 * scheduling classes, without having to expose their internal data
1529 * structures to the load-balancing proper:
1531 struct rq_iterator
{
1533 struct task_struct
*(*start
)(void *);
1534 struct task_struct
*(*next
)(void *);
1538 static unsigned long
1539 balance_tasks(struct rq
*this_rq
, int this_cpu
, struct rq
*busiest
,
1540 unsigned long max_load_move
, struct sched_domain
*sd
,
1541 enum cpu_idle_type idle
, int *all_pinned
,
1542 int *this_best_prio
, struct rq_iterator
*iterator
);
1545 iter_move_one_task(struct rq
*this_rq
, int this_cpu
, struct rq
*busiest
,
1546 struct sched_domain
*sd
, enum cpu_idle_type idle
,
1547 struct rq_iterator
*iterator
);
1550 #ifdef CONFIG_CGROUP_CPUACCT
1551 static void cpuacct_charge(struct task_struct
*tsk
, u64 cputime
);
1553 static inline void cpuacct_charge(struct task_struct
*tsk
, u64 cputime
) {}
1556 static inline void inc_cpu_load(struct rq
*rq
, unsigned long load
)
1558 update_load_add(&rq
->load
, load
);
1561 static inline void dec_cpu_load(struct rq
*rq
, unsigned long load
)
1563 update_load_sub(&rq
->load
, load
);
1567 static unsigned long source_load(int cpu
, int type
);
1568 static unsigned long target_load(int cpu
, int type
);
1569 static unsigned long cpu_avg_load_per_task(int cpu
);
1570 static int task_hot(struct task_struct
*p
, u64 now
, struct sched_domain
*sd
);
1572 #ifdef CONFIG_FAIR_GROUP_SCHED
1575 * Group load balancing.
1577 * We calculate a few balance domain wide aggregate numbers; load and weight.
1578 * Given the pictures below, and assuming each item has equal weight:
1589 * A and B get 1/3-rd of the total load. C and D get 1/3-rd of A's 1/3-rd,
1590 * which equals 1/9-th of the total load.
1593 * The weight of this group on the selected cpus.
1596 * Direct sum of all the cpu's their rq weight, e.g. A would get 3 while
1600 * Part of the rq_weight contributed by tasks; all groups except B would
1604 static inline struct aggregate_struct
*
1605 aggregate(struct task_group
*tg
, struct sched_domain
*sd
)
1607 return &tg
->cfs_rq
[sd
->first_cpu
]->aggregate
;
1610 typedef void (*aggregate_func
)(struct task_group
*, struct sched_domain
*);
1613 * Iterate the full tree, calling @down when first entering a node and @up when
1614 * leaving it for the final time.
1617 void aggregate_walk_tree(aggregate_func down
, aggregate_func up
,
1618 struct sched_domain
*sd
)
1620 struct task_group
*parent
, *child
;
1623 parent
= &root_task_group
;
1625 (*down
)(parent
, sd
);
1626 list_for_each_entry_rcu(child
, &parent
->children
, siblings
) {
1636 parent
= parent
->parent
;
1643 * Calculate the aggregate runqueue weight.
1646 void aggregate_group_weight(struct task_group
*tg
, struct sched_domain
*sd
)
1648 unsigned long rq_weight
= 0;
1649 unsigned long task_weight
= 0;
1652 for_each_cpu_mask(i
, sd
->span
) {
1653 rq_weight
+= tg
->cfs_rq
[i
]->load
.weight
;
1654 task_weight
+= tg
->cfs_rq
[i
]->task_weight
;
1657 aggregate(tg
, sd
)->rq_weight
= rq_weight
;
1658 aggregate(tg
, sd
)->task_weight
= task_weight
;
1662 * Compute the weight of this group on the given cpus.
1665 void aggregate_group_shares(struct task_group
*tg
, struct sched_domain
*sd
)
1667 unsigned long shares
= 0;
1670 for_each_cpu_mask(i
, sd
->span
)
1671 shares
+= tg
->cfs_rq
[i
]->shares
;
1673 if ((!shares
&& aggregate(tg
, sd
)->rq_weight
) || shares
> tg
->shares
)
1674 shares
= tg
->shares
;
1676 aggregate(tg
, sd
)->shares
= shares
;
1680 * Compute the load fraction assigned to this group, relies on the aggregate
1681 * weight and this group's parent's load, i.e. top-down.
1684 void aggregate_group_load(struct task_group
*tg
, struct sched_domain
*sd
)
1692 for_each_cpu_mask(i
, sd
->span
)
1693 load
+= cpu_rq(i
)->load
.weight
;
1696 load
= aggregate(tg
->parent
, sd
)->load
;
1699 * shares is our weight in the parent's rq so
1700 * shares/parent->rq_weight gives our fraction of the load
1702 load
*= aggregate(tg
, sd
)->shares
;
1703 load
/= aggregate(tg
->parent
, sd
)->rq_weight
+ 1;
1706 aggregate(tg
, sd
)->load
= load
;
1709 static void __set_se_shares(struct sched_entity
*se
, unsigned long shares
);
1712 * Calculate and set the cpu's group shares.
1715 __update_group_shares_cpu(struct task_group
*tg
, struct sched_domain
*sd
,
1719 unsigned long shares
;
1720 unsigned long rq_weight
;
1725 rq_weight
= tg
->cfs_rq
[tcpu
]->load
.weight
;
1728 * If there are currently no tasks on the cpu pretend there is one of
1729 * average load so that when a new task gets to run here it will not
1730 * get delayed by group starvation.
1734 rq_weight
= NICE_0_LOAD
;
1738 * \Sum shares * rq_weight
1739 * shares = -----------------------
1743 shares
= aggregate(tg
, sd
)->shares
* rq_weight
;
1744 shares
/= aggregate(tg
, sd
)->rq_weight
+ 1;
1747 * record the actual number of shares, not the boosted amount.
1749 tg
->cfs_rq
[tcpu
]->shares
= boost
? 0 : shares
;
1751 if (shares
< MIN_SHARES
)
1752 shares
= MIN_SHARES
;
1754 __set_se_shares(tg
->se
[tcpu
], shares
);
1758 * Re-adjust the weights on the cpu the task came from and on the cpu the
1762 __move_group_shares(struct task_group
*tg
, struct sched_domain
*sd
,
1765 unsigned long shares
;
1767 shares
= tg
->cfs_rq
[scpu
]->shares
+ tg
->cfs_rq
[dcpu
]->shares
;
1769 __update_group_shares_cpu(tg
, sd
, scpu
);
1770 __update_group_shares_cpu(tg
, sd
, dcpu
);
1773 * ensure we never loose shares due to rounding errors in the
1774 * above redistribution.
1776 shares
-= tg
->cfs_rq
[scpu
]->shares
+ tg
->cfs_rq
[dcpu
]->shares
;
1778 tg
->cfs_rq
[dcpu
]->shares
+= shares
;
1782 * Because changing a group's shares changes the weight of the super-group
1783 * we need to walk up the tree and change all shares until we hit the root.
1786 move_group_shares(struct task_group
*tg
, struct sched_domain
*sd
,
1790 __move_group_shares(tg
, sd
, scpu
, dcpu
);
1796 void aggregate_group_set_shares(struct task_group
*tg
, struct sched_domain
*sd
)
1798 unsigned long shares
= aggregate(tg
, sd
)->shares
;
1801 for_each_cpu_mask(i
, sd
->span
) {
1802 struct rq
*rq
= cpu_rq(i
);
1803 unsigned long flags
;
1805 spin_lock_irqsave(&rq
->lock
, flags
);
1806 __update_group_shares_cpu(tg
, sd
, i
);
1807 spin_unlock_irqrestore(&rq
->lock
, flags
);
1810 aggregate_group_shares(tg
, sd
);
1813 * ensure we never loose shares due to rounding errors in the
1814 * above redistribution.
1816 shares
-= aggregate(tg
, sd
)->shares
;
1818 tg
->cfs_rq
[sd
->first_cpu
]->shares
+= shares
;
1819 aggregate(tg
, sd
)->shares
+= shares
;
1824 * Calculate the accumulative weight and recursive load of each task group
1825 * while walking down the tree.
1828 void aggregate_get_down(struct task_group
*tg
, struct sched_domain
*sd
)
1830 aggregate_group_weight(tg
, sd
);
1831 aggregate_group_shares(tg
, sd
);
1832 aggregate_group_load(tg
, sd
);
1836 * Rebalance the cpu shares while walking back up the tree.
1839 void aggregate_get_up(struct task_group
*tg
, struct sched_domain
*sd
)
1841 aggregate_group_set_shares(tg
, sd
);
1844 static DEFINE_PER_CPU(spinlock_t
, aggregate_lock
);
1846 static void __init
init_aggregate(void)
1850 for_each_possible_cpu(i
)
1851 spin_lock_init(&per_cpu(aggregate_lock
, i
));
1854 static int get_aggregate(struct sched_domain
*sd
)
1856 if (!spin_trylock(&per_cpu(aggregate_lock
, sd
->first_cpu
)))
1859 aggregate_walk_tree(aggregate_get_down
, aggregate_get_up
, sd
);
1863 static void put_aggregate(struct sched_domain
*sd
)
1865 spin_unlock(&per_cpu(aggregate_lock
, sd
->first_cpu
));
1868 static void cfs_rq_set_shares(struct cfs_rq
*cfs_rq
, unsigned long shares
)
1870 cfs_rq
->shares
= shares
;
1875 static inline void init_aggregate(void)
1879 static inline int get_aggregate(struct sched_domain
*sd
)
1884 static inline void put_aggregate(struct sched_domain
*sd
)
1889 #else /* CONFIG_SMP */
1891 #ifdef CONFIG_FAIR_GROUP_SCHED
1892 static void cfs_rq_set_shares(struct cfs_rq
*cfs_rq
, unsigned long shares
)
1897 #endif /* CONFIG_SMP */
1899 #include "sched_stats.h"
1900 #include "sched_idletask.c"
1901 #include "sched_fair.c"
1902 #include "sched_rt.c"
1903 #ifdef CONFIG_SCHED_DEBUG
1904 # include "sched_debug.c"
1907 #define sched_class_highest (&rt_sched_class)
1909 static void inc_nr_running(struct rq
*rq
)
1914 static void dec_nr_running(struct rq
*rq
)
1919 static void set_load_weight(struct task_struct
*p
)
1921 if (task_has_rt_policy(p
)) {
1922 p
->se
.load
.weight
= prio_to_weight
[0] * 2;
1923 p
->se
.load
.inv_weight
= prio_to_wmult
[0] >> 1;
1928 * SCHED_IDLE tasks get minimal weight:
1930 if (p
->policy
== SCHED_IDLE
) {
1931 p
->se
.load
.weight
= WEIGHT_IDLEPRIO
;
1932 p
->se
.load
.inv_weight
= WMULT_IDLEPRIO
;
1936 p
->se
.load
.weight
= prio_to_weight
[p
->static_prio
- MAX_RT_PRIO
];
1937 p
->se
.load
.inv_weight
= prio_to_wmult
[p
->static_prio
- MAX_RT_PRIO
];
1940 static void enqueue_task(struct rq
*rq
, struct task_struct
*p
, int wakeup
)
1942 sched_info_queued(p
);
1943 p
->sched_class
->enqueue_task(rq
, p
, wakeup
);
1947 static void dequeue_task(struct rq
*rq
, struct task_struct
*p
, int sleep
)
1949 p
->sched_class
->dequeue_task(rq
, p
, sleep
);
1954 * __normal_prio - return the priority that is based on the static prio
1956 static inline int __normal_prio(struct task_struct
*p
)
1958 return p
->static_prio
;
1962 * Calculate the expected normal priority: i.e. priority
1963 * without taking RT-inheritance into account. Might be
1964 * boosted by interactivity modifiers. Changes upon fork,
1965 * setprio syscalls, and whenever the interactivity
1966 * estimator recalculates.
1968 static inline int normal_prio(struct task_struct
*p
)
1972 if (task_has_rt_policy(p
))
1973 prio
= MAX_RT_PRIO
-1 - p
->rt_priority
;
1975 prio
= __normal_prio(p
);
1980 * Calculate the current priority, i.e. the priority
1981 * taken into account by the scheduler. This value might
1982 * be boosted by RT tasks, or might be boosted by
1983 * interactivity modifiers. Will be RT if the task got
1984 * RT-boosted. If not then it returns p->normal_prio.
1986 static int effective_prio(struct task_struct
*p
)
1988 p
->normal_prio
= normal_prio(p
);
1990 * If we are RT tasks or we were boosted to RT priority,
1991 * keep the priority unchanged. Otherwise, update priority
1992 * to the normal priority:
1994 if (!rt_prio(p
->prio
))
1995 return p
->normal_prio
;
2000 * activate_task - move a task to the runqueue.
2002 static void activate_task(struct rq
*rq
, struct task_struct
*p
, int wakeup
)
2004 if (task_contributes_to_load(p
))
2005 rq
->nr_uninterruptible
--;
2007 enqueue_task(rq
, p
, wakeup
);
2012 * deactivate_task - remove a task from the runqueue.
2014 static void deactivate_task(struct rq
*rq
, struct task_struct
*p
, int sleep
)
2016 if (task_contributes_to_load(p
))
2017 rq
->nr_uninterruptible
++;
2019 dequeue_task(rq
, p
, sleep
);
2024 * task_curr - is this task currently executing on a CPU?
2025 * @p: the task in question.
2027 inline int task_curr(const struct task_struct
*p
)
2029 return cpu_curr(task_cpu(p
)) == p
;
2032 /* Used instead of source_load when we know the type == 0 */
2033 unsigned long weighted_cpuload(const int cpu
)
2035 return cpu_rq(cpu
)->load
.weight
;
2038 static inline void __set_task_cpu(struct task_struct
*p
, unsigned int cpu
)
2040 set_task_rq(p
, cpu
);
2043 * After ->cpu is set up to a new value, task_rq_lock(p, ...) can be
2044 * successfuly executed on another CPU. We must ensure that updates of
2045 * per-task data have been completed by this moment.
2048 task_thread_info(p
)->cpu
= cpu
;
2052 static inline void check_class_changed(struct rq
*rq
, struct task_struct
*p
,
2053 const struct sched_class
*prev_class
,
2054 int oldprio
, int running
)
2056 if (prev_class
!= p
->sched_class
) {
2057 if (prev_class
->switched_from
)
2058 prev_class
->switched_from(rq
, p
, running
);
2059 p
->sched_class
->switched_to(rq
, p
, running
);
2061 p
->sched_class
->prio_changed(rq
, p
, oldprio
, running
);
2067 * Is this task likely cache-hot:
2070 task_hot(struct task_struct
*p
, u64 now
, struct sched_domain
*sd
)
2075 * Buddy candidates are cache hot:
2077 if (sched_feat(CACHE_HOT_BUDDY
) && (&p
->se
== cfs_rq_of(&p
->se
)->next
))
2080 if (p
->sched_class
!= &fair_sched_class
)
2083 if (sysctl_sched_migration_cost
== -1)
2085 if (sysctl_sched_migration_cost
== 0)
2088 delta
= now
- p
->se
.exec_start
;
2090 return delta
< (s64
)sysctl_sched_migration_cost
;
2094 void set_task_cpu(struct task_struct
*p
, unsigned int new_cpu
)
2096 int old_cpu
= task_cpu(p
);
2097 struct rq
*old_rq
= cpu_rq(old_cpu
), *new_rq
= cpu_rq(new_cpu
);
2098 struct cfs_rq
*old_cfsrq
= task_cfs_rq(p
),
2099 *new_cfsrq
= cpu_cfs_rq(old_cfsrq
, new_cpu
);
2102 clock_offset
= old_rq
->clock
- new_rq
->clock
;
2104 #ifdef CONFIG_SCHEDSTATS
2105 if (p
->se
.wait_start
)
2106 p
->se
.wait_start
-= clock_offset
;
2107 if (p
->se
.sleep_start
)
2108 p
->se
.sleep_start
-= clock_offset
;
2109 if (p
->se
.block_start
)
2110 p
->se
.block_start
-= clock_offset
;
2111 if (old_cpu
!= new_cpu
) {
2112 schedstat_inc(p
, se
.nr_migrations
);
2113 if (task_hot(p
, old_rq
->clock
, NULL
))
2114 schedstat_inc(p
, se
.nr_forced2_migrations
);
2117 p
->se
.vruntime
-= old_cfsrq
->min_vruntime
-
2118 new_cfsrq
->min_vruntime
;
2120 __set_task_cpu(p
, new_cpu
);
2123 struct migration_req
{
2124 struct list_head list
;
2126 struct task_struct
*task
;
2129 struct completion done
;
2133 * The task's runqueue lock must be held.
2134 * Returns true if you have to wait for migration thread.
2137 migrate_task(struct task_struct
*p
, int dest_cpu
, struct migration_req
*req
)
2139 struct rq
*rq
= task_rq(p
);
2142 * If the task is not on a runqueue (and not running), then
2143 * it is sufficient to simply update the task's cpu field.
2145 if (!p
->se
.on_rq
&& !task_running(rq
, p
)) {
2146 set_task_cpu(p
, dest_cpu
);
2150 init_completion(&req
->done
);
2152 req
->dest_cpu
= dest_cpu
;
2153 list_add(&req
->list
, &rq
->migration_queue
);
2159 * wait_task_inactive - wait for a thread to unschedule.
2161 * The caller must ensure that the task *will* unschedule sometime soon,
2162 * else this function might spin for a *long* time. This function can't
2163 * be called with interrupts off, or it may introduce deadlock with
2164 * smp_call_function() if an IPI is sent by the same process we are
2165 * waiting to become inactive.
2167 void wait_task_inactive(struct task_struct
*p
)
2169 unsigned long flags
;
2175 * We do the initial early heuristics without holding
2176 * any task-queue locks at all. We'll only try to get
2177 * the runqueue lock when things look like they will
2183 * If the task is actively running on another CPU
2184 * still, just relax and busy-wait without holding
2187 * NOTE! Since we don't hold any locks, it's not
2188 * even sure that "rq" stays as the right runqueue!
2189 * But we don't care, since "task_running()" will
2190 * return false if the runqueue has changed and p
2191 * is actually now running somewhere else!
2193 while (task_running(rq
, p
))
2197 * Ok, time to look more closely! We need the rq
2198 * lock now, to be *sure*. If we're wrong, we'll
2199 * just go back and repeat.
2201 rq
= task_rq_lock(p
, &flags
);
2202 running
= task_running(rq
, p
);
2203 on_rq
= p
->se
.on_rq
;
2204 task_rq_unlock(rq
, &flags
);
2207 * Was it really running after all now that we
2208 * checked with the proper locks actually held?
2210 * Oops. Go back and try again..
2212 if (unlikely(running
)) {
2218 * It's not enough that it's not actively running,
2219 * it must be off the runqueue _entirely_, and not
2222 * So if it wa still runnable (but just not actively
2223 * running right now), it's preempted, and we should
2224 * yield - it could be a while.
2226 if (unlikely(on_rq
)) {
2227 schedule_timeout_uninterruptible(1);
2232 * Ahh, all good. It wasn't running, and it wasn't
2233 * runnable, which means that it will never become
2234 * running in the future either. We're all done!
2241 * kick_process - kick a running thread to enter/exit the kernel
2242 * @p: the to-be-kicked thread
2244 * Cause a process which is running on another CPU to enter
2245 * kernel-mode, without any delay. (to get signals handled.)
2247 * NOTE: this function doesnt have to take the runqueue lock,
2248 * because all it wants to ensure is that the remote task enters
2249 * the kernel. If the IPI races and the task has been migrated
2250 * to another CPU then no harm is done and the purpose has been
2253 void kick_process(struct task_struct
*p
)
2259 if ((cpu
!= smp_processor_id()) && task_curr(p
))
2260 smp_send_reschedule(cpu
);
2265 * Return a low guess at the load of a migration-source cpu weighted
2266 * according to the scheduling class and "nice" value.
2268 * We want to under-estimate the load of migration sources, to
2269 * balance conservatively.
2271 static unsigned long source_load(int cpu
, int type
)
2273 struct rq
*rq
= cpu_rq(cpu
);
2274 unsigned long total
= weighted_cpuload(cpu
);
2279 return min(rq
->cpu_load
[type
-1], total
);
2283 * Return a high guess at the load of a migration-target cpu weighted
2284 * according to the scheduling class and "nice" value.
2286 static unsigned long target_load(int cpu
, int type
)
2288 struct rq
*rq
= cpu_rq(cpu
);
2289 unsigned long total
= weighted_cpuload(cpu
);
2294 return max(rq
->cpu_load
[type
-1], total
);
2298 * Return the average load per task on the cpu's run queue
2300 static unsigned long cpu_avg_load_per_task(int cpu
)
2302 struct rq
*rq
= cpu_rq(cpu
);
2303 unsigned long total
= weighted_cpuload(cpu
);
2304 unsigned long n
= rq
->nr_running
;
2306 return n
? total
/ n
: SCHED_LOAD_SCALE
;
2310 * find_idlest_group finds and returns the least busy CPU group within the
2313 static struct sched_group
*
2314 find_idlest_group(struct sched_domain
*sd
, struct task_struct
*p
, int this_cpu
)
2316 struct sched_group
*idlest
= NULL
, *this = NULL
, *group
= sd
->groups
;
2317 unsigned long min_load
= ULONG_MAX
, this_load
= 0;
2318 int load_idx
= sd
->forkexec_idx
;
2319 int imbalance
= 100 + (sd
->imbalance_pct
-100)/2;
2322 unsigned long load
, avg_load
;
2326 /* Skip over this group if it has no CPUs allowed */
2327 if (!cpus_intersects(group
->cpumask
, p
->cpus_allowed
))
2330 local_group
= cpu_isset(this_cpu
, group
->cpumask
);
2332 /* Tally up the load of all CPUs in the group */
2335 for_each_cpu_mask(i
, group
->cpumask
) {
2336 /* Bias balancing toward cpus of our domain */
2338 load
= source_load(i
, load_idx
);
2340 load
= target_load(i
, load_idx
);
2345 /* Adjust by relative CPU power of the group */
2346 avg_load
= sg_div_cpu_power(group
,
2347 avg_load
* SCHED_LOAD_SCALE
);
2350 this_load
= avg_load
;
2352 } else if (avg_load
< min_load
) {
2353 min_load
= avg_load
;
2356 } while (group
= group
->next
, group
!= sd
->groups
);
2358 if (!idlest
|| 100*this_load
< imbalance
*min_load
)
2364 * find_idlest_cpu - find the idlest cpu among the cpus in group.
2367 find_idlest_cpu(struct sched_group
*group
, struct task_struct
*p
, int this_cpu
,
2370 unsigned long load
, min_load
= ULONG_MAX
;
2374 /* Traverse only the allowed CPUs */
2375 cpus_and(*tmp
, group
->cpumask
, p
->cpus_allowed
);
2377 for_each_cpu_mask(i
, *tmp
) {
2378 load
= weighted_cpuload(i
);
2380 if (load
< min_load
|| (load
== min_load
&& i
== this_cpu
)) {
2390 * sched_balance_self: balance the current task (running on cpu) in domains
2391 * that have the 'flag' flag set. In practice, this is SD_BALANCE_FORK and
2394 * Balance, ie. select the least loaded group.
2396 * Returns the target CPU number, or the same CPU if no balancing is needed.
2398 * preempt must be disabled.
2400 static int sched_balance_self(int cpu
, int flag
)
2402 struct task_struct
*t
= current
;
2403 struct sched_domain
*tmp
, *sd
= NULL
;
2405 for_each_domain(cpu
, tmp
) {
2407 * If power savings logic is enabled for a domain, stop there.
2409 if (tmp
->flags
& SD_POWERSAVINGS_BALANCE
)
2411 if (tmp
->flags
& flag
)
2416 cpumask_t span
, tmpmask
;
2417 struct sched_group
*group
;
2418 int new_cpu
, weight
;
2420 if (!(sd
->flags
& flag
)) {
2426 group
= find_idlest_group(sd
, t
, cpu
);
2432 new_cpu
= find_idlest_cpu(group
, t
, cpu
, &tmpmask
);
2433 if (new_cpu
== -1 || new_cpu
== cpu
) {
2434 /* Now try balancing at a lower domain level of cpu */
2439 /* Now try balancing at a lower domain level of new_cpu */
2442 weight
= cpus_weight(span
);
2443 for_each_domain(cpu
, tmp
) {
2444 if (weight
<= cpus_weight(tmp
->span
))
2446 if (tmp
->flags
& flag
)
2449 /* while loop will break here if sd == NULL */
2455 #endif /* CONFIG_SMP */
2458 * try_to_wake_up - wake up a thread
2459 * @p: the to-be-woken-up thread
2460 * @state: the mask of task states that can be woken
2461 * @sync: do a synchronous wakeup?
2463 * Put it on the run-queue if it's not already there. The "current"
2464 * thread is always on the run-queue (except when the actual
2465 * re-schedule is in progress), and as such you're allowed to do
2466 * the simpler "current->state = TASK_RUNNING" to mark yourself
2467 * runnable without the overhead of this.
2469 * returns failure only if the task is already active.
2471 static int try_to_wake_up(struct task_struct
*p
, unsigned int state
, int sync
)
2473 int cpu
, orig_cpu
, this_cpu
, success
= 0;
2474 unsigned long flags
;
2478 if (!sched_feat(SYNC_WAKEUPS
))
2482 rq
= task_rq_lock(p
, &flags
);
2483 old_state
= p
->state
;
2484 if (!(old_state
& state
))
2492 this_cpu
= smp_processor_id();
2495 if (unlikely(task_running(rq
, p
)))
2498 cpu
= p
->sched_class
->select_task_rq(p
, sync
);
2499 if (cpu
!= orig_cpu
) {
2500 set_task_cpu(p
, cpu
);
2501 task_rq_unlock(rq
, &flags
);
2502 /* might preempt at this point */
2503 rq
= task_rq_lock(p
, &flags
);
2504 old_state
= p
->state
;
2505 if (!(old_state
& state
))
2510 this_cpu
= smp_processor_id();
2514 #ifdef CONFIG_SCHEDSTATS
2515 schedstat_inc(rq
, ttwu_count
);
2516 if (cpu
== this_cpu
)
2517 schedstat_inc(rq
, ttwu_local
);
2519 struct sched_domain
*sd
;
2520 for_each_domain(this_cpu
, sd
) {
2521 if (cpu_isset(cpu
, sd
->span
)) {
2522 schedstat_inc(sd
, ttwu_wake_remote
);
2530 #endif /* CONFIG_SMP */
2531 schedstat_inc(p
, se
.nr_wakeups
);
2533 schedstat_inc(p
, se
.nr_wakeups_sync
);
2534 if (orig_cpu
!= cpu
)
2535 schedstat_inc(p
, se
.nr_wakeups_migrate
);
2536 if (cpu
== this_cpu
)
2537 schedstat_inc(p
, se
.nr_wakeups_local
);
2539 schedstat_inc(p
, se
.nr_wakeups_remote
);
2540 update_rq_clock(rq
);
2541 activate_task(rq
, p
, 1);
2545 check_preempt_curr(rq
, p
);
2547 p
->state
= TASK_RUNNING
;
2549 if (p
->sched_class
->task_wake_up
)
2550 p
->sched_class
->task_wake_up(rq
, p
);
2553 task_rq_unlock(rq
, &flags
);
2558 int wake_up_process(struct task_struct
*p
)
2560 return try_to_wake_up(p
, TASK_ALL
, 0);
2562 EXPORT_SYMBOL(wake_up_process
);
2564 int wake_up_state(struct task_struct
*p
, unsigned int state
)
2566 return try_to_wake_up(p
, state
, 0);
2570 * Perform scheduler related setup for a newly forked process p.
2571 * p is forked by current.
2573 * __sched_fork() is basic setup used by init_idle() too:
2575 static void __sched_fork(struct task_struct
*p
)
2577 p
->se
.exec_start
= 0;
2578 p
->se
.sum_exec_runtime
= 0;
2579 p
->se
.prev_sum_exec_runtime
= 0;
2580 p
->se
.last_wakeup
= 0;
2581 p
->se
.avg_overlap
= 0;
2583 #ifdef CONFIG_SCHEDSTATS
2584 p
->se
.wait_start
= 0;
2585 p
->se
.sum_sleep_runtime
= 0;
2586 p
->se
.sleep_start
= 0;
2587 p
->se
.block_start
= 0;
2588 p
->se
.sleep_max
= 0;
2589 p
->se
.block_max
= 0;
2591 p
->se
.slice_max
= 0;
2595 INIT_LIST_HEAD(&p
->rt
.run_list
);
2597 INIT_LIST_HEAD(&p
->se
.group_node
);
2599 #ifdef CONFIG_PREEMPT_NOTIFIERS
2600 INIT_HLIST_HEAD(&p
->preempt_notifiers
);
2604 * We mark the process as running here, but have not actually
2605 * inserted it onto the runqueue yet. This guarantees that
2606 * nobody will actually run it, and a signal or other external
2607 * event cannot wake it up and insert it on the runqueue either.
2609 p
->state
= TASK_RUNNING
;
2613 * fork()/clone()-time setup:
2615 void sched_fork(struct task_struct
*p
, int clone_flags
)
2617 int cpu
= get_cpu();
2622 cpu
= sched_balance_self(cpu
, SD_BALANCE_FORK
);
2624 set_task_cpu(p
, cpu
);
2627 * Make sure we do not leak PI boosting priority to the child:
2629 p
->prio
= current
->normal_prio
;
2630 if (!rt_prio(p
->prio
))
2631 p
->sched_class
= &fair_sched_class
;
2633 #if defined(CONFIG_SCHEDSTATS) || defined(CONFIG_TASK_DELAY_ACCT)
2634 if (likely(sched_info_on()))
2635 memset(&p
->sched_info
, 0, sizeof(p
->sched_info
));
2637 #if defined(CONFIG_SMP) && defined(__ARCH_WANT_UNLOCKED_CTXSW)
2640 #ifdef CONFIG_PREEMPT
2641 /* Want to start with kernel preemption disabled. */
2642 task_thread_info(p
)->preempt_count
= 1;
2648 * wake_up_new_task - wake up a newly created task for the first time.
2650 * This function will do some initial scheduler statistics housekeeping
2651 * that must be done for every newly created context, then puts the task
2652 * on the runqueue and wakes it.
2654 void wake_up_new_task(struct task_struct
*p
, unsigned long clone_flags
)
2656 unsigned long flags
;
2659 rq
= task_rq_lock(p
, &flags
);
2660 BUG_ON(p
->state
!= TASK_RUNNING
);
2661 update_rq_clock(rq
);
2663 p
->prio
= effective_prio(p
);
2665 if (!p
->sched_class
->task_new
|| !current
->se
.on_rq
) {
2666 activate_task(rq
, p
, 0);
2669 * Let the scheduling class do new task startup
2670 * management (if any):
2672 p
->sched_class
->task_new(rq
, p
);
2675 check_preempt_curr(rq
, p
);
2677 if (p
->sched_class
->task_wake_up
)
2678 p
->sched_class
->task_wake_up(rq
, p
);
2680 task_rq_unlock(rq
, &flags
);
2683 #ifdef CONFIG_PREEMPT_NOTIFIERS
2686 * preempt_notifier_register - tell me when current is being being preempted & rescheduled
2687 * @notifier: notifier struct to register
2689 void preempt_notifier_register(struct preempt_notifier
*notifier
)
2691 hlist_add_head(¬ifier
->link
, ¤t
->preempt_notifiers
);
2693 EXPORT_SYMBOL_GPL(preempt_notifier_register
);
2696 * preempt_notifier_unregister - no longer interested in preemption notifications
2697 * @notifier: notifier struct to unregister
2699 * This is safe to call from within a preemption notifier.
2701 void preempt_notifier_unregister(struct preempt_notifier
*notifier
)
2703 hlist_del(¬ifier
->link
);
2705 EXPORT_SYMBOL_GPL(preempt_notifier_unregister
);
2707 static void fire_sched_in_preempt_notifiers(struct task_struct
*curr
)
2709 struct preempt_notifier
*notifier
;
2710 struct hlist_node
*node
;
2712 hlist_for_each_entry(notifier
, node
, &curr
->preempt_notifiers
, link
)
2713 notifier
->ops
->sched_in(notifier
, raw_smp_processor_id());
2717 fire_sched_out_preempt_notifiers(struct task_struct
*curr
,
2718 struct task_struct
*next
)
2720 struct preempt_notifier
*notifier
;
2721 struct hlist_node
*node
;
2723 hlist_for_each_entry(notifier
, node
, &curr
->preempt_notifiers
, link
)
2724 notifier
->ops
->sched_out(notifier
, next
);
2729 static void fire_sched_in_preempt_notifiers(struct task_struct
*curr
)
2734 fire_sched_out_preempt_notifiers(struct task_struct
*curr
,
2735 struct task_struct
*next
)
2742 * prepare_task_switch - prepare to switch tasks
2743 * @rq: the runqueue preparing to switch
2744 * @prev: the current task that is being switched out
2745 * @next: the task we are going to switch to.
2747 * This is called with the rq lock held and interrupts off. It must
2748 * be paired with a subsequent finish_task_switch after the context
2751 * prepare_task_switch sets up locking and calls architecture specific
2755 prepare_task_switch(struct rq
*rq
, struct task_struct
*prev
,
2756 struct task_struct
*next
)
2758 fire_sched_out_preempt_notifiers(prev
, next
);
2759 prepare_lock_switch(rq
, next
);
2760 prepare_arch_switch(next
);
2764 * finish_task_switch - clean up after a task-switch
2765 * @rq: runqueue associated with task-switch
2766 * @prev: the thread we just switched away from.
2768 * finish_task_switch must be called after the context switch, paired
2769 * with a prepare_task_switch call before the context switch.
2770 * finish_task_switch will reconcile locking set up by prepare_task_switch,
2771 * and do any other architecture-specific cleanup actions.
2773 * Note that we may have delayed dropping an mm in context_switch(). If
2774 * so, we finish that here outside of the runqueue lock. (Doing it
2775 * with the lock held can cause deadlocks; see schedule() for
2778 static void finish_task_switch(struct rq
*rq
, struct task_struct
*prev
)
2779 __releases(rq
->lock
)
2781 struct mm_struct
*mm
= rq
->prev_mm
;
2787 * A task struct has one reference for the use as "current".
2788 * If a task dies, then it sets TASK_DEAD in tsk->state and calls
2789 * schedule one last time. The schedule call will never return, and
2790 * the scheduled task must drop that reference.
2791 * The test for TASK_DEAD must occur while the runqueue locks are
2792 * still held, otherwise prev could be scheduled on another cpu, die
2793 * there before we look at prev->state, and then the reference would
2795 * Manfred Spraul <manfred@colorfullife.com>
2797 prev_state
= prev
->state
;
2798 finish_arch_switch(prev
);
2799 finish_lock_switch(rq
, prev
);
2801 if (current
->sched_class
->post_schedule
)
2802 current
->sched_class
->post_schedule(rq
);
2805 fire_sched_in_preempt_notifiers(current
);
2808 if (unlikely(prev_state
== TASK_DEAD
)) {
2810 * Remove function-return probe instances associated with this
2811 * task and put them back on the free list.
2813 kprobe_flush_task(prev
);
2814 put_task_struct(prev
);
2819 * schedule_tail - first thing a freshly forked thread must call.
2820 * @prev: the thread we just switched away from.
2822 asmlinkage
void schedule_tail(struct task_struct
*prev
)
2823 __releases(rq
->lock
)
2825 struct rq
*rq
= this_rq();
2827 finish_task_switch(rq
, prev
);
2828 #ifdef __ARCH_WANT_UNLOCKED_CTXSW
2829 /* In this case, finish_task_switch does not reenable preemption */
2832 if (current
->set_child_tid
)
2833 put_user(task_pid_vnr(current
), current
->set_child_tid
);
2837 * context_switch - switch to the new MM and the new
2838 * thread's register state.
2841 context_switch(struct rq
*rq
, struct task_struct
*prev
,
2842 struct task_struct
*next
)
2844 struct mm_struct
*mm
, *oldmm
;
2846 prepare_task_switch(rq
, prev
, next
);
2848 oldmm
= prev
->active_mm
;
2850 * For paravirt, this is coupled with an exit in switch_to to
2851 * combine the page table reload and the switch backend into
2854 arch_enter_lazy_cpu_mode();
2856 if (unlikely(!mm
)) {
2857 next
->active_mm
= oldmm
;
2858 atomic_inc(&oldmm
->mm_count
);
2859 enter_lazy_tlb(oldmm
, next
);
2861 switch_mm(oldmm
, mm
, next
);
2863 if (unlikely(!prev
->mm
)) {
2864 prev
->active_mm
= NULL
;
2865 rq
->prev_mm
= oldmm
;
2868 * Since the runqueue lock will be released by the next
2869 * task (which is an invalid locking op but in the case
2870 * of the scheduler it's an obvious special-case), so we
2871 * do an early lockdep release here:
2873 #ifndef __ARCH_WANT_UNLOCKED_CTXSW
2874 spin_release(&rq
->lock
.dep_map
, 1, _THIS_IP_
);
2877 /* Here we just switch the register state and the stack. */
2878 switch_to(prev
, next
, prev
);
2882 * this_rq must be evaluated again because prev may have moved
2883 * CPUs since it called schedule(), thus the 'rq' on its stack
2884 * frame will be invalid.
2886 finish_task_switch(this_rq(), prev
);
2890 * nr_running, nr_uninterruptible and nr_context_switches:
2892 * externally visible scheduler statistics: current number of runnable
2893 * threads, current number of uninterruptible-sleeping threads, total
2894 * number of context switches performed since bootup.
2896 unsigned long nr_running(void)
2898 unsigned long i
, sum
= 0;
2900 for_each_online_cpu(i
)
2901 sum
+= cpu_rq(i
)->nr_running
;
2906 unsigned long nr_uninterruptible(void)
2908 unsigned long i
, sum
= 0;
2910 for_each_possible_cpu(i
)
2911 sum
+= cpu_rq(i
)->nr_uninterruptible
;
2914 * Since we read the counters lockless, it might be slightly
2915 * inaccurate. Do not allow it to go below zero though:
2917 if (unlikely((long)sum
< 0))
2923 unsigned long long nr_context_switches(void)
2926 unsigned long long sum
= 0;
2928 for_each_possible_cpu(i
)
2929 sum
+= cpu_rq(i
)->nr_switches
;
2934 unsigned long nr_iowait(void)
2936 unsigned long i
, sum
= 0;
2938 for_each_possible_cpu(i
)
2939 sum
+= atomic_read(&cpu_rq(i
)->nr_iowait
);
2944 unsigned long nr_active(void)
2946 unsigned long i
, running
= 0, uninterruptible
= 0;
2948 for_each_online_cpu(i
) {
2949 running
+= cpu_rq(i
)->nr_running
;
2950 uninterruptible
+= cpu_rq(i
)->nr_uninterruptible
;
2953 if (unlikely((long)uninterruptible
< 0))
2954 uninterruptible
= 0;
2956 return running
+ uninterruptible
;
2960 * Update rq->cpu_load[] statistics. This function is usually called every
2961 * scheduler tick (TICK_NSEC).
2963 static void update_cpu_load(struct rq
*this_rq
)
2965 unsigned long this_load
= this_rq
->load
.weight
;
2968 this_rq
->nr_load_updates
++;
2970 /* Update our load: */
2971 for (i
= 0, scale
= 1; i
< CPU_LOAD_IDX_MAX
; i
++, scale
+= scale
) {
2972 unsigned long old_load
, new_load
;
2974 /* scale is effectively 1 << i now, and >> i divides by scale */
2976 old_load
= this_rq
->cpu_load
[i
];
2977 new_load
= this_load
;
2979 * Round up the averaging division if load is increasing. This
2980 * prevents us from getting stuck on 9 if the load is 10, for
2983 if (new_load
> old_load
)
2984 new_load
+= scale
-1;
2985 this_rq
->cpu_load
[i
] = (old_load
*(scale
-1) + new_load
) >> i
;
2992 * double_rq_lock - safely lock two runqueues
2994 * Note this does not disable interrupts like task_rq_lock,
2995 * you need to do so manually before calling.
2997 static void double_rq_lock(struct rq
*rq1
, struct rq
*rq2
)
2998 __acquires(rq1
->lock
)
2999 __acquires(rq2
->lock
)
3001 BUG_ON(!irqs_disabled());
3003 spin_lock(&rq1
->lock
);
3004 __acquire(rq2
->lock
); /* Fake it out ;) */
3007 spin_lock(&rq1
->lock
);
3008 spin_lock(&rq2
->lock
);
3010 spin_lock(&rq2
->lock
);
3011 spin_lock(&rq1
->lock
);
3014 update_rq_clock(rq1
);
3015 update_rq_clock(rq2
);
3019 * double_rq_unlock - safely unlock two runqueues
3021 * Note this does not restore interrupts like task_rq_unlock,
3022 * you need to do so manually after calling.
3024 static void double_rq_unlock(struct rq
*rq1
, struct rq
*rq2
)
3025 __releases(rq1
->lock
)
3026 __releases(rq2
->lock
)
3028 spin_unlock(&rq1
->lock
);
3030 spin_unlock(&rq2
->lock
);
3032 __release(rq2
->lock
);
3036 * double_lock_balance - lock the busiest runqueue, this_rq is locked already.
3038 static int double_lock_balance(struct rq
*this_rq
, struct rq
*busiest
)
3039 __releases(this_rq
->lock
)
3040 __acquires(busiest
->lock
)
3041 __acquires(this_rq
->lock
)
3045 if (unlikely(!irqs_disabled())) {
3046 /* printk() doesn't work good under rq->lock */
3047 spin_unlock(&this_rq
->lock
);
3050 if (unlikely(!spin_trylock(&busiest
->lock
))) {
3051 if (busiest
< this_rq
) {
3052 spin_unlock(&this_rq
->lock
);
3053 spin_lock(&busiest
->lock
);
3054 spin_lock(&this_rq
->lock
);
3057 spin_lock(&busiest
->lock
);
3063 * If dest_cpu is allowed for this process, migrate the task to it.
3064 * This is accomplished by forcing the cpu_allowed mask to only
3065 * allow dest_cpu, which will force the cpu onto dest_cpu. Then
3066 * the cpu_allowed mask is restored.
3068 static void sched_migrate_task(struct task_struct
*p
, int dest_cpu
)
3070 struct migration_req req
;
3071 unsigned long flags
;
3074 rq
= task_rq_lock(p
, &flags
);
3075 if (!cpu_isset(dest_cpu
, p
->cpus_allowed
)
3076 || unlikely(cpu_is_offline(dest_cpu
)))
3079 /* force the process onto the specified CPU */
3080 if (migrate_task(p
, dest_cpu
, &req
)) {
3081 /* Need to wait for migration thread (might exit: take ref). */
3082 struct task_struct
*mt
= rq
->migration_thread
;
3084 get_task_struct(mt
);
3085 task_rq_unlock(rq
, &flags
);
3086 wake_up_process(mt
);
3087 put_task_struct(mt
);
3088 wait_for_completion(&req
.done
);
3093 task_rq_unlock(rq
, &flags
);
3097 * sched_exec - execve() is a valuable balancing opportunity, because at
3098 * this point the task has the smallest effective memory and cache footprint.
3100 void sched_exec(void)
3102 int new_cpu
, this_cpu
= get_cpu();
3103 new_cpu
= sched_balance_self(this_cpu
, SD_BALANCE_EXEC
);
3105 if (new_cpu
!= this_cpu
)
3106 sched_migrate_task(current
, new_cpu
);
3110 * pull_task - move a task from a remote runqueue to the local runqueue.
3111 * Both runqueues must be locked.
3113 static void pull_task(struct rq
*src_rq
, struct task_struct
*p
,
3114 struct rq
*this_rq
, int this_cpu
)
3116 deactivate_task(src_rq
, p
, 0);
3117 set_task_cpu(p
, this_cpu
);
3118 activate_task(this_rq
, p
, 0);
3120 * Note that idle threads have a prio of MAX_PRIO, for this test
3121 * to be always true for them.
3123 check_preempt_curr(this_rq
, p
);
3127 * can_migrate_task - may task p from runqueue rq be migrated to this_cpu?
3130 int can_migrate_task(struct task_struct
*p
, struct rq
*rq
, int this_cpu
,
3131 struct sched_domain
*sd
, enum cpu_idle_type idle
,
3135 * We do not migrate tasks that are:
3136 * 1) running (obviously), or
3137 * 2) cannot be migrated to this CPU due to cpus_allowed, or
3138 * 3) are cache-hot on their current CPU.
3140 if (!cpu_isset(this_cpu
, p
->cpus_allowed
)) {
3141 schedstat_inc(p
, se
.nr_failed_migrations_affine
);
3146 if (task_running(rq
, p
)) {
3147 schedstat_inc(p
, se
.nr_failed_migrations_running
);
3152 * Aggressive migration if:
3153 * 1) task is cache cold, or
3154 * 2) too many balance attempts have failed.
3157 if (!task_hot(p
, rq
->clock
, sd
) ||
3158 sd
->nr_balance_failed
> sd
->cache_nice_tries
) {
3159 #ifdef CONFIG_SCHEDSTATS
3160 if (task_hot(p
, rq
->clock
, sd
)) {
3161 schedstat_inc(sd
, lb_hot_gained
[idle
]);
3162 schedstat_inc(p
, se
.nr_forced_migrations
);
3168 if (task_hot(p
, rq
->clock
, sd
)) {
3169 schedstat_inc(p
, se
.nr_failed_migrations_hot
);
3175 static unsigned long
3176 balance_tasks(struct rq
*this_rq
, int this_cpu
, struct rq
*busiest
,
3177 unsigned long max_load_move
, struct sched_domain
*sd
,
3178 enum cpu_idle_type idle
, int *all_pinned
,
3179 int *this_best_prio
, struct rq_iterator
*iterator
)
3181 int loops
= 0, pulled
= 0, pinned
= 0, skip_for_load
;
3182 struct task_struct
*p
;
3183 long rem_load_move
= max_load_move
;
3185 if (max_load_move
== 0)
3191 * Start the load-balancing iterator:
3193 p
= iterator
->start(iterator
->arg
);
3195 if (!p
|| loops
++ > sysctl_sched_nr_migrate
)
3198 * To help distribute high priority tasks across CPUs we don't
3199 * skip a task if it will be the highest priority task (i.e. smallest
3200 * prio value) on its new queue regardless of its load weight
3202 skip_for_load
= (p
->se
.load
.weight
>> 1) > rem_load_move
+
3203 SCHED_LOAD_SCALE_FUZZ
;
3204 if ((skip_for_load
&& p
->prio
>= *this_best_prio
) ||
3205 !can_migrate_task(p
, busiest
, this_cpu
, sd
, idle
, &pinned
)) {
3206 p
= iterator
->next(iterator
->arg
);
3210 pull_task(busiest
, p
, this_rq
, this_cpu
);
3212 rem_load_move
-= p
->se
.load
.weight
;
3215 * We only want to steal up to the prescribed amount of weighted load.
3217 if (rem_load_move
> 0) {
3218 if (p
->prio
< *this_best_prio
)
3219 *this_best_prio
= p
->prio
;
3220 p
= iterator
->next(iterator
->arg
);
3225 * Right now, this is one of only two places pull_task() is called,
3226 * so we can safely collect pull_task() stats here rather than
3227 * inside pull_task().
3229 schedstat_add(sd
, lb_gained
[idle
], pulled
);
3232 *all_pinned
= pinned
;
3234 return max_load_move
- rem_load_move
;
3238 * move_tasks tries to move up to max_load_move weighted load from busiest to
3239 * this_rq, as part of a balancing operation within domain "sd".
3240 * Returns 1 if successful and 0 otherwise.
3242 * Called with both runqueues locked.
3244 static int move_tasks(struct rq
*this_rq
, int this_cpu
, struct rq
*busiest
,
3245 unsigned long max_load_move
,
3246 struct sched_domain
*sd
, enum cpu_idle_type idle
,
3249 const struct sched_class
*class = sched_class_highest
;
3250 unsigned long total_load_moved
= 0;
3251 int this_best_prio
= this_rq
->curr
->prio
;
3255 class->load_balance(this_rq
, this_cpu
, busiest
,
3256 max_load_move
- total_load_moved
,
3257 sd
, idle
, all_pinned
, &this_best_prio
);
3258 class = class->next
;
3259 } while (class && max_load_move
> total_load_moved
);
3261 return total_load_moved
> 0;
3265 iter_move_one_task(struct rq
*this_rq
, int this_cpu
, struct rq
*busiest
,
3266 struct sched_domain
*sd
, enum cpu_idle_type idle
,
3267 struct rq_iterator
*iterator
)
3269 struct task_struct
*p
= iterator
->start(iterator
->arg
);
3273 if (can_migrate_task(p
, busiest
, this_cpu
, sd
, idle
, &pinned
)) {
3274 pull_task(busiest
, p
, this_rq
, this_cpu
);
3276 * Right now, this is only the second place pull_task()
3277 * is called, so we can safely collect pull_task()
3278 * stats here rather than inside pull_task().
3280 schedstat_inc(sd
, lb_gained
[idle
]);
3284 p
= iterator
->next(iterator
->arg
);
3291 * move_one_task tries to move exactly one task from busiest to this_rq, as
3292 * part of active balancing operations within "domain".
3293 * Returns 1 if successful and 0 otherwise.
3295 * Called with both runqueues locked.
3297 static int move_one_task(struct rq
*this_rq
, int this_cpu
, struct rq
*busiest
,
3298 struct sched_domain
*sd
, enum cpu_idle_type idle
)
3300 const struct sched_class
*class;
3302 for (class = sched_class_highest
; class; class = class->next
)
3303 if (class->move_one_task(this_rq
, this_cpu
, busiest
, sd
, idle
))
3310 * find_busiest_group finds and returns the busiest CPU group within the
3311 * domain. It calculates and returns the amount of weighted load which
3312 * should be moved to restore balance via the imbalance parameter.
3314 static struct sched_group
*
3315 find_busiest_group(struct sched_domain
*sd
, int this_cpu
,
3316 unsigned long *imbalance
, enum cpu_idle_type idle
,
3317 int *sd_idle
, const cpumask_t
*cpus
, int *balance
)
3319 struct sched_group
*busiest
= NULL
, *this = NULL
, *group
= sd
->groups
;
3320 unsigned long max_load
, avg_load
, total_load
, this_load
, total_pwr
;
3321 unsigned long max_pull
;
3322 unsigned long busiest_load_per_task
, busiest_nr_running
;
3323 unsigned long this_load_per_task
, this_nr_running
;
3324 int load_idx
, group_imb
= 0;
3325 #if defined(CONFIG_SCHED_MC) || defined(CONFIG_SCHED_SMT)
3326 int power_savings_balance
= 1;
3327 unsigned long leader_nr_running
= 0, min_load_per_task
= 0;
3328 unsigned long min_nr_running
= ULONG_MAX
;
3329 struct sched_group
*group_min
= NULL
, *group_leader
= NULL
;
3332 max_load
= this_load
= total_load
= total_pwr
= 0;
3333 busiest_load_per_task
= busiest_nr_running
= 0;
3334 this_load_per_task
= this_nr_running
= 0;
3335 if (idle
== CPU_NOT_IDLE
)
3336 load_idx
= sd
->busy_idx
;
3337 else if (idle
== CPU_NEWLY_IDLE
)
3338 load_idx
= sd
->newidle_idx
;
3340 load_idx
= sd
->idle_idx
;
3343 unsigned long load
, group_capacity
, max_cpu_load
, min_cpu_load
;
3346 int __group_imb
= 0;
3347 unsigned int balance_cpu
= -1, first_idle_cpu
= 0;
3348 unsigned long sum_nr_running
, sum_weighted_load
;
3350 local_group
= cpu_isset(this_cpu
, group
->cpumask
);
3353 balance_cpu
= first_cpu(group
->cpumask
);
3355 /* Tally up the load of all CPUs in the group */
3356 sum_weighted_load
= sum_nr_running
= avg_load
= 0;
3358 min_cpu_load
= ~0UL;
3360 for_each_cpu_mask(i
, group
->cpumask
) {
3363 if (!cpu_isset(i
, *cpus
))
3368 if (*sd_idle
&& rq
->nr_running
)
3371 /* Bias balancing toward cpus of our domain */
3373 if (idle_cpu(i
) && !first_idle_cpu
) {
3378 load
= target_load(i
, load_idx
);
3380 load
= source_load(i
, load_idx
);
3381 if (load
> max_cpu_load
)
3382 max_cpu_load
= load
;
3383 if (min_cpu_load
> load
)
3384 min_cpu_load
= load
;
3388 sum_nr_running
+= rq
->nr_running
;
3389 sum_weighted_load
+= weighted_cpuload(i
);
3393 * First idle cpu or the first cpu(busiest) in this sched group
3394 * is eligible for doing load balancing at this and above
3395 * domains. In the newly idle case, we will allow all the cpu's
3396 * to do the newly idle load balance.
3398 if (idle
!= CPU_NEWLY_IDLE
&& local_group
&&
3399 balance_cpu
!= this_cpu
&& balance
) {
3404 total_load
+= avg_load
;
3405 total_pwr
+= group
->__cpu_power
;
3407 /* Adjust by relative CPU power of the group */
3408 avg_load
= sg_div_cpu_power(group
,
3409 avg_load
* SCHED_LOAD_SCALE
);
3411 if ((max_cpu_load
- min_cpu_load
) > SCHED_LOAD_SCALE
)
3414 group_capacity
= group
->__cpu_power
/ SCHED_LOAD_SCALE
;
3417 this_load
= avg_load
;
3419 this_nr_running
= sum_nr_running
;
3420 this_load_per_task
= sum_weighted_load
;
3421 } else if (avg_load
> max_load
&&
3422 (sum_nr_running
> group_capacity
|| __group_imb
)) {
3423 max_load
= avg_load
;
3425 busiest_nr_running
= sum_nr_running
;
3426 busiest_load_per_task
= sum_weighted_load
;
3427 group_imb
= __group_imb
;
3430 #if defined(CONFIG_SCHED_MC) || defined(CONFIG_SCHED_SMT)
3432 * Busy processors will not participate in power savings
3435 if (idle
== CPU_NOT_IDLE
||
3436 !(sd
->flags
& SD_POWERSAVINGS_BALANCE
))
3440 * If the local group is idle or completely loaded
3441 * no need to do power savings balance at this domain
3443 if (local_group
&& (this_nr_running
>= group_capacity
||
3445 power_savings_balance
= 0;
3448 * If a group is already running at full capacity or idle,
3449 * don't include that group in power savings calculations
3451 if (!power_savings_balance
|| sum_nr_running
>= group_capacity
3456 * Calculate the group which has the least non-idle load.
3457 * This is the group from where we need to pick up the load
3460 if ((sum_nr_running
< min_nr_running
) ||
3461 (sum_nr_running
== min_nr_running
&&
3462 first_cpu(group
->cpumask
) <
3463 first_cpu(group_min
->cpumask
))) {
3465 min_nr_running
= sum_nr_running
;
3466 min_load_per_task
= sum_weighted_load
/
3471 * Calculate the group which is almost near its
3472 * capacity but still has some space to pick up some load
3473 * from other group and save more power
3475 if (sum_nr_running
<= group_capacity
- 1) {
3476 if (sum_nr_running
> leader_nr_running
||
3477 (sum_nr_running
== leader_nr_running
&&
3478 first_cpu(group
->cpumask
) >
3479 first_cpu(group_leader
->cpumask
))) {
3480 group_leader
= group
;
3481 leader_nr_running
= sum_nr_running
;
3486 group
= group
->next
;
3487 } while (group
!= sd
->groups
);
3489 if (!busiest
|| this_load
>= max_load
|| busiest_nr_running
== 0)
3492 avg_load
= (SCHED_LOAD_SCALE
* total_load
) / total_pwr
;
3494 if (this_load
>= avg_load
||
3495 100*max_load
<= sd
->imbalance_pct
*this_load
)
3498 busiest_load_per_task
/= busiest_nr_running
;
3500 busiest_load_per_task
= min(busiest_load_per_task
, avg_load
);
3503 * We're trying to get all the cpus to the average_load, so we don't
3504 * want to push ourselves above the average load, nor do we wish to
3505 * reduce the max loaded cpu below the average load, as either of these
3506 * actions would just result in more rebalancing later, and ping-pong
3507 * tasks around. Thus we look for the minimum possible imbalance.
3508 * Negative imbalances (*we* are more loaded than anyone else) will
3509 * be counted as no imbalance for these purposes -- we can't fix that
3510 * by pulling tasks to us. Be careful of negative numbers as they'll
3511 * appear as very large values with unsigned longs.
3513 if (max_load
<= busiest_load_per_task
)
3517 * In the presence of smp nice balancing, certain scenarios can have
3518 * max load less than avg load(as we skip the groups at or below
3519 * its cpu_power, while calculating max_load..)
3521 if (max_load
< avg_load
) {
3523 goto small_imbalance
;
3526 /* Don't want to pull so many tasks that a group would go idle */
3527 max_pull
= min(max_load
- avg_load
, max_load
- busiest_load_per_task
);
3529 /* How much load to actually move to equalise the imbalance */
3530 *imbalance
= min(max_pull
* busiest
->__cpu_power
,
3531 (avg_load
- this_load
) * this->__cpu_power
)
3535 * if *imbalance is less than the average load per runnable task
3536 * there is no gaurantee that any tasks will be moved so we'll have
3537 * a think about bumping its value to force at least one task to be
3540 if (*imbalance
< busiest_load_per_task
) {
3541 unsigned long tmp
, pwr_now
, pwr_move
;
3545 pwr_move
= pwr_now
= 0;
3547 if (this_nr_running
) {
3548 this_load_per_task
/= this_nr_running
;
3549 if (busiest_load_per_task
> this_load_per_task
)
3552 this_load_per_task
= SCHED_LOAD_SCALE
;
3554 if (max_load
- this_load
+ SCHED_LOAD_SCALE_FUZZ
>=
3555 busiest_load_per_task
* imbn
) {
3556 *imbalance
= busiest_load_per_task
;
3561 * OK, we don't have enough imbalance to justify moving tasks,
3562 * however we may be able to increase total CPU power used by
3566 pwr_now
+= busiest
->__cpu_power
*
3567 min(busiest_load_per_task
, max_load
);
3568 pwr_now
+= this->__cpu_power
*
3569 min(this_load_per_task
, this_load
);
3570 pwr_now
/= SCHED_LOAD_SCALE
;
3572 /* Amount of load we'd subtract */
3573 tmp
= sg_div_cpu_power(busiest
,
3574 busiest_load_per_task
* SCHED_LOAD_SCALE
);
3576 pwr_move
+= busiest
->__cpu_power
*
3577 min(busiest_load_per_task
, max_load
- tmp
);
3579 /* Amount of load we'd add */
3580 if (max_load
* busiest
->__cpu_power
<
3581 busiest_load_per_task
* SCHED_LOAD_SCALE
)
3582 tmp
= sg_div_cpu_power(this,
3583 max_load
* busiest
->__cpu_power
);
3585 tmp
= sg_div_cpu_power(this,
3586 busiest_load_per_task
* SCHED_LOAD_SCALE
);
3587 pwr_move
+= this->__cpu_power
*
3588 min(this_load_per_task
, this_load
+ tmp
);
3589 pwr_move
/= SCHED_LOAD_SCALE
;
3591 /* Move if we gain throughput */
3592 if (pwr_move
> pwr_now
)
3593 *imbalance
= busiest_load_per_task
;
3599 #if defined(CONFIG_SCHED_MC) || defined(CONFIG_SCHED_SMT)
3600 if (idle
== CPU_NOT_IDLE
|| !(sd
->flags
& SD_POWERSAVINGS_BALANCE
))
3603 if (this == group_leader
&& group_leader
!= group_min
) {
3604 *imbalance
= min_load_per_task
;
3614 * find_busiest_queue - find the busiest runqueue among the cpus in group.
3617 find_busiest_queue(struct sched_group
*group
, enum cpu_idle_type idle
,
3618 unsigned long imbalance
, const cpumask_t
*cpus
)
3620 struct rq
*busiest
= NULL
, *rq
;
3621 unsigned long max_load
= 0;
3624 for_each_cpu_mask(i
, group
->cpumask
) {
3627 if (!cpu_isset(i
, *cpus
))
3631 wl
= weighted_cpuload(i
);
3633 if (rq
->nr_running
== 1 && wl
> imbalance
)
3636 if (wl
> max_load
) {
3646 * Max backoff if we encounter pinned tasks. Pretty arbitrary value, but
3647 * so long as it is large enough.
3649 #define MAX_PINNED_INTERVAL 512
3652 * Check this_cpu to ensure it is balanced within domain. Attempt to move
3653 * tasks if there is an imbalance.
3655 static int load_balance(int this_cpu
, struct rq
*this_rq
,
3656 struct sched_domain
*sd
, enum cpu_idle_type idle
,
3657 int *balance
, cpumask_t
*cpus
)
3659 int ld_moved
, all_pinned
= 0, active_balance
= 0, sd_idle
= 0;
3660 struct sched_group
*group
;
3661 unsigned long imbalance
;
3663 unsigned long flags
;
3664 int unlock_aggregate
;
3668 unlock_aggregate
= get_aggregate(sd
);
3671 * When power savings policy is enabled for the parent domain, idle
3672 * sibling can pick up load irrespective of busy siblings. In this case,
3673 * let the state of idle sibling percolate up as CPU_IDLE, instead of
3674 * portraying it as CPU_NOT_IDLE.
3676 if (idle
!= CPU_NOT_IDLE
&& sd
->flags
& SD_SHARE_CPUPOWER
&&
3677 !test_sd_parent(sd
, SD_POWERSAVINGS_BALANCE
))
3680 schedstat_inc(sd
, lb_count
[idle
]);
3683 group
= find_busiest_group(sd
, this_cpu
, &imbalance
, idle
, &sd_idle
,
3690 schedstat_inc(sd
, lb_nobusyg
[idle
]);
3694 busiest
= find_busiest_queue(group
, idle
, imbalance
, cpus
);
3696 schedstat_inc(sd
, lb_nobusyq
[idle
]);
3700 BUG_ON(busiest
== this_rq
);
3702 schedstat_add(sd
, lb_imbalance
[idle
], imbalance
);
3705 if (busiest
->nr_running
> 1) {
3707 * Attempt to move tasks. If find_busiest_group has found
3708 * an imbalance but busiest->nr_running <= 1, the group is
3709 * still unbalanced. ld_moved simply stays zero, so it is
3710 * correctly treated as an imbalance.
3712 local_irq_save(flags
);
3713 double_rq_lock(this_rq
, busiest
);
3714 ld_moved
= move_tasks(this_rq
, this_cpu
, busiest
,
3715 imbalance
, sd
, idle
, &all_pinned
);
3716 double_rq_unlock(this_rq
, busiest
);
3717 local_irq_restore(flags
);
3720 * some other cpu did the load balance for us.
3722 if (ld_moved
&& this_cpu
!= smp_processor_id())
3723 resched_cpu(this_cpu
);
3725 /* All tasks on this runqueue were pinned by CPU affinity */
3726 if (unlikely(all_pinned
)) {
3727 cpu_clear(cpu_of(busiest
), *cpus
);
3728 if (!cpus_empty(*cpus
))
3735 schedstat_inc(sd
, lb_failed
[idle
]);
3736 sd
->nr_balance_failed
++;
3738 if (unlikely(sd
->nr_balance_failed
> sd
->cache_nice_tries
+2)) {
3740 spin_lock_irqsave(&busiest
->lock
, flags
);
3742 /* don't kick the migration_thread, if the curr
3743 * task on busiest cpu can't be moved to this_cpu
3745 if (!cpu_isset(this_cpu
, busiest
->curr
->cpus_allowed
)) {
3746 spin_unlock_irqrestore(&busiest
->lock
, flags
);
3748 goto out_one_pinned
;
3751 if (!busiest
->active_balance
) {
3752 busiest
->active_balance
= 1;
3753 busiest
->push_cpu
= this_cpu
;
3756 spin_unlock_irqrestore(&busiest
->lock
, flags
);
3758 wake_up_process(busiest
->migration_thread
);
3761 * We've kicked active balancing, reset the failure
3764 sd
->nr_balance_failed
= sd
->cache_nice_tries
+1;
3767 sd
->nr_balance_failed
= 0;
3769 if (likely(!active_balance
)) {
3770 /* We were unbalanced, so reset the balancing interval */
3771 sd
->balance_interval
= sd
->min_interval
;
3774 * If we've begun active balancing, start to back off. This
3775 * case may not be covered by the all_pinned logic if there
3776 * is only 1 task on the busy runqueue (because we don't call
3779 if (sd
->balance_interval
< sd
->max_interval
)
3780 sd
->balance_interval
*= 2;
3783 if (!ld_moved
&& !sd_idle
&& sd
->flags
& SD_SHARE_CPUPOWER
&&
3784 !test_sd_parent(sd
, SD_POWERSAVINGS_BALANCE
))
3790 schedstat_inc(sd
, lb_balanced
[idle
]);
3792 sd
->nr_balance_failed
= 0;
3795 /* tune up the balancing interval */
3796 if ((all_pinned
&& sd
->balance_interval
< MAX_PINNED_INTERVAL
) ||
3797 (sd
->balance_interval
< sd
->max_interval
))
3798 sd
->balance_interval
*= 2;
3800 if (!sd_idle
&& sd
->flags
& SD_SHARE_CPUPOWER
&&
3801 !test_sd_parent(sd
, SD_POWERSAVINGS_BALANCE
))
3806 if (unlock_aggregate
)
3812 * Check this_cpu to ensure it is balanced within domain. Attempt to move
3813 * tasks if there is an imbalance.
3815 * Called from schedule when this_rq is about to become idle (CPU_NEWLY_IDLE).
3816 * this_rq is locked.
3819 load_balance_newidle(int this_cpu
, struct rq
*this_rq
, struct sched_domain
*sd
,
3822 struct sched_group
*group
;
3823 struct rq
*busiest
= NULL
;
3824 unsigned long imbalance
;
3832 * When power savings policy is enabled for the parent domain, idle
3833 * sibling can pick up load irrespective of busy siblings. In this case,
3834 * let the state of idle sibling percolate up as IDLE, instead of
3835 * portraying it as CPU_NOT_IDLE.
3837 if (sd
->flags
& SD_SHARE_CPUPOWER
&&
3838 !test_sd_parent(sd
, SD_POWERSAVINGS_BALANCE
))
3841 schedstat_inc(sd
, lb_count
[CPU_NEWLY_IDLE
]);
3843 group
= find_busiest_group(sd
, this_cpu
, &imbalance
, CPU_NEWLY_IDLE
,
3844 &sd_idle
, cpus
, NULL
);
3846 schedstat_inc(sd
, lb_nobusyg
[CPU_NEWLY_IDLE
]);
3850 busiest
= find_busiest_queue(group
, CPU_NEWLY_IDLE
, imbalance
, cpus
);
3852 schedstat_inc(sd
, lb_nobusyq
[CPU_NEWLY_IDLE
]);
3856 BUG_ON(busiest
== this_rq
);
3858 schedstat_add(sd
, lb_imbalance
[CPU_NEWLY_IDLE
], imbalance
);
3861 if (busiest
->nr_running
> 1) {
3862 /* Attempt to move tasks */
3863 double_lock_balance(this_rq
, busiest
);
3864 /* this_rq->clock is already updated */
3865 update_rq_clock(busiest
);
3866 ld_moved
= move_tasks(this_rq
, this_cpu
, busiest
,
3867 imbalance
, sd
, CPU_NEWLY_IDLE
,
3869 spin_unlock(&busiest
->lock
);
3871 if (unlikely(all_pinned
)) {
3872 cpu_clear(cpu_of(busiest
), *cpus
);
3873 if (!cpus_empty(*cpus
))
3879 schedstat_inc(sd
, lb_failed
[CPU_NEWLY_IDLE
]);
3880 if (!sd_idle
&& sd
->flags
& SD_SHARE_CPUPOWER
&&
3881 !test_sd_parent(sd
, SD_POWERSAVINGS_BALANCE
))
3884 sd
->nr_balance_failed
= 0;
3889 schedstat_inc(sd
, lb_balanced
[CPU_NEWLY_IDLE
]);
3890 if (!sd_idle
&& sd
->flags
& SD_SHARE_CPUPOWER
&&
3891 !test_sd_parent(sd
, SD_POWERSAVINGS_BALANCE
))
3893 sd
->nr_balance_failed
= 0;
3899 * idle_balance is called by schedule() if this_cpu is about to become
3900 * idle. Attempts to pull tasks from other CPUs.
3902 static void idle_balance(int this_cpu
, struct rq
*this_rq
)
3904 struct sched_domain
*sd
;
3905 int pulled_task
= -1;
3906 unsigned long next_balance
= jiffies
+ HZ
;
3909 for_each_domain(this_cpu
, sd
) {
3910 unsigned long interval
;
3912 if (!(sd
->flags
& SD_LOAD_BALANCE
))
3915 if (sd
->flags
& SD_BALANCE_NEWIDLE
)
3916 /* If we've pulled tasks over stop searching: */
3917 pulled_task
= load_balance_newidle(this_cpu
, this_rq
,
3920 interval
= msecs_to_jiffies(sd
->balance_interval
);
3921 if (time_after(next_balance
, sd
->last_balance
+ interval
))
3922 next_balance
= sd
->last_balance
+ interval
;
3926 if (pulled_task
|| time_after(jiffies
, this_rq
->next_balance
)) {
3928 * We are going idle. next_balance may be set based on
3929 * a busy processor. So reset next_balance.
3931 this_rq
->next_balance
= next_balance
;
3936 * active_load_balance is run by migration threads. It pushes running tasks
3937 * off the busiest CPU onto idle CPUs. It requires at least 1 task to be
3938 * running on each physical CPU where possible, and avoids physical /
3939 * logical imbalances.
3941 * Called with busiest_rq locked.
3943 static void active_load_balance(struct rq
*busiest_rq
, int busiest_cpu
)
3945 int target_cpu
= busiest_rq
->push_cpu
;
3946 struct sched_domain
*sd
;
3947 struct rq
*target_rq
;
3949 /* Is there any task to move? */
3950 if (busiest_rq
->nr_running
<= 1)
3953 target_rq
= cpu_rq(target_cpu
);
3956 * This condition is "impossible", if it occurs
3957 * we need to fix it. Originally reported by
3958 * Bjorn Helgaas on a 128-cpu setup.
3960 BUG_ON(busiest_rq
== target_rq
);
3962 /* move a task from busiest_rq to target_rq */
3963 double_lock_balance(busiest_rq
, target_rq
);
3964 update_rq_clock(busiest_rq
);
3965 update_rq_clock(target_rq
);
3967 /* Search for an sd spanning us and the target CPU. */
3968 for_each_domain(target_cpu
, sd
) {
3969 if ((sd
->flags
& SD_LOAD_BALANCE
) &&
3970 cpu_isset(busiest_cpu
, sd
->span
))
3975 schedstat_inc(sd
, alb_count
);
3977 if (move_one_task(target_rq
, target_cpu
, busiest_rq
,
3979 schedstat_inc(sd
, alb_pushed
);
3981 schedstat_inc(sd
, alb_failed
);
3983 spin_unlock(&target_rq
->lock
);
3988 atomic_t load_balancer
;
3990 } nohz ____cacheline_aligned
= {
3991 .load_balancer
= ATOMIC_INIT(-1),
3992 .cpu_mask
= CPU_MASK_NONE
,
3996 * This routine will try to nominate the ilb (idle load balancing)
3997 * owner among the cpus whose ticks are stopped. ilb owner will do the idle
3998 * load balancing on behalf of all those cpus. If all the cpus in the system
3999 * go into this tickless mode, then there will be no ilb owner (as there is
4000 * no need for one) and all the cpus will sleep till the next wakeup event
4003 * For the ilb owner, tick is not stopped. And this tick will be used
4004 * for idle load balancing. ilb owner will still be part of
4007 * While stopping the tick, this cpu will become the ilb owner if there
4008 * is no other owner. And will be the owner till that cpu becomes busy
4009 * or if all cpus in the system stop their ticks at which point
4010 * there is no need for ilb owner.
4012 * When the ilb owner becomes busy, it nominates another owner, during the
4013 * next busy scheduler_tick()
4015 int select_nohz_load_balancer(int stop_tick
)
4017 int cpu
= smp_processor_id();
4020 cpu_set(cpu
, nohz
.cpu_mask
);
4021 cpu_rq(cpu
)->in_nohz_recently
= 1;
4024 * If we are going offline and still the leader, give up!
4026 if (cpu_is_offline(cpu
) &&
4027 atomic_read(&nohz
.load_balancer
) == cpu
) {
4028 if (atomic_cmpxchg(&nohz
.load_balancer
, cpu
, -1) != cpu
)
4033 /* time for ilb owner also to sleep */
4034 if (cpus_weight(nohz
.cpu_mask
) == num_online_cpus()) {
4035 if (atomic_read(&nohz
.load_balancer
) == cpu
)
4036 atomic_set(&nohz
.load_balancer
, -1);
4040 if (atomic_read(&nohz
.load_balancer
) == -1) {
4041 /* make me the ilb owner */
4042 if (atomic_cmpxchg(&nohz
.load_balancer
, -1, cpu
) == -1)
4044 } else if (atomic_read(&nohz
.load_balancer
) == cpu
)
4047 if (!cpu_isset(cpu
, nohz
.cpu_mask
))
4050 cpu_clear(cpu
, nohz
.cpu_mask
);
4052 if (atomic_read(&nohz
.load_balancer
) == cpu
)
4053 if (atomic_cmpxchg(&nohz
.load_balancer
, cpu
, -1) != cpu
)
4060 static DEFINE_SPINLOCK(balancing
);
4063 * It checks each scheduling domain to see if it is due to be balanced,
4064 * and initiates a balancing operation if so.
4066 * Balancing parameters are set up in arch_init_sched_domains.
4068 static void rebalance_domains(int cpu
, enum cpu_idle_type idle
)
4071 struct rq
*rq
= cpu_rq(cpu
);
4072 unsigned long interval
;
4073 struct sched_domain
*sd
;
4074 /* Earliest time when we have to do rebalance again */
4075 unsigned long next_balance
= jiffies
+ 60*HZ
;
4076 int update_next_balance
= 0;
4079 for_each_domain(cpu
, sd
) {
4080 if (!(sd
->flags
& SD_LOAD_BALANCE
))
4083 interval
= sd
->balance_interval
;
4084 if (idle
!= CPU_IDLE
)
4085 interval
*= sd
->busy_factor
;
4087 /* scale ms to jiffies */
4088 interval
= msecs_to_jiffies(interval
);
4089 if (unlikely(!interval
))
4091 if (interval
> HZ
*NR_CPUS
/10)
4092 interval
= HZ
*NR_CPUS
/10;
4095 if (sd
->flags
& SD_SERIALIZE
) {
4096 if (!spin_trylock(&balancing
))
4100 if (time_after_eq(jiffies
, sd
->last_balance
+ interval
)) {
4101 if (load_balance(cpu
, rq
, sd
, idle
, &balance
, &tmp
)) {
4103 * We've pulled tasks over so either we're no
4104 * longer idle, or one of our SMT siblings is
4107 idle
= CPU_NOT_IDLE
;
4109 sd
->last_balance
= jiffies
;
4111 if (sd
->flags
& SD_SERIALIZE
)
4112 spin_unlock(&balancing
);
4114 if (time_after(next_balance
, sd
->last_balance
+ interval
)) {
4115 next_balance
= sd
->last_balance
+ interval
;
4116 update_next_balance
= 1;
4120 * Stop the load balance at this level. There is another
4121 * CPU in our sched group which is doing load balancing more
4129 * next_balance will be updated only when there is a need.
4130 * When the cpu is attached to null domain for ex, it will not be
4133 if (likely(update_next_balance
))
4134 rq
->next_balance
= next_balance
;
4138 * run_rebalance_domains is triggered when needed from the scheduler tick.
4139 * In CONFIG_NO_HZ case, the idle load balance owner will do the
4140 * rebalancing for all the cpus for whom scheduler ticks are stopped.
4142 static void run_rebalance_domains(struct softirq_action
*h
)
4144 int this_cpu
= smp_processor_id();
4145 struct rq
*this_rq
= cpu_rq(this_cpu
);
4146 enum cpu_idle_type idle
= this_rq
->idle_at_tick
?
4147 CPU_IDLE
: CPU_NOT_IDLE
;
4149 rebalance_domains(this_cpu
, idle
);
4153 * If this cpu is the owner for idle load balancing, then do the
4154 * balancing on behalf of the other idle cpus whose ticks are
4157 if (this_rq
->idle_at_tick
&&
4158 atomic_read(&nohz
.load_balancer
) == this_cpu
) {
4159 cpumask_t cpus
= nohz
.cpu_mask
;
4163 cpu_clear(this_cpu
, cpus
);
4164 for_each_cpu_mask(balance_cpu
, cpus
) {
4166 * If this cpu gets work to do, stop the load balancing
4167 * work being done for other cpus. Next load
4168 * balancing owner will pick it up.
4173 rebalance_domains(balance_cpu
, CPU_IDLE
);
4175 rq
= cpu_rq(balance_cpu
);
4176 if (time_after(this_rq
->next_balance
, rq
->next_balance
))
4177 this_rq
->next_balance
= rq
->next_balance
;
4184 * Trigger the SCHED_SOFTIRQ if it is time to do periodic load balancing.
4186 * In case of CONFIG_NO_HZ, this is the place where we nominate a new
4187 * idle load balancing owner or decide to stop the periodic load balancing,
4188 * if the whole system is idle.
4190 static inline void trigger_load_balance(struct rq
*rq
, int cpu
)
4194 * If we were in the nohz mode recently and busy at the current
4195 * scheduler tick, then check if we need to nominate new idle
4198 if (rq
->in_nohz_recently
&& !rq
->idle_at_tick
) {
4199 rq
->in_nohz_recently
= 0;
4201 if (atomic_read(&nohz
.load_balancer
) == cpu
) {
4202 cpu_clear(cpu
, nohz
.cpu_mask
);
4203 atomic_set(&nohz
.load_balancer
, -1);
4206 if (atomic_read(&nohz
.load_balancer
) == -1) {
4208 * simple selection for now: Nominate the
4209 * first cpu in the nohz list to be the next
4212 * TBD: Traverse the sched domains and nominate
4213 * the nearest cpu in the nohz.cpu_mask.
4215 int ilb
= first_cpu(nohz
.cpu_mask
);
4217 if (ilb
< nr_cpu_ids
)
4223 * If this cpu is idle and doing idle load balancing for all the
4224 * cpus with ticks stopped, is it time for that to stop?
4226 if (rq
->idle_at_tick
&& atomic_read(&nohz
.load_balancer
) == cpu
&&
4227 cpus_weight(nohz
.cpu_mask
) == num_online_cpus()) {
4233 * If this cpu is idle and the idle load balancing is done by
4234 * someone else, then no need raise the SCHED_SOFTIRQ
4236 if (rq
->idle_at_tick
&& atomic_read(&nohz
.load_balancer
) != cpu
&&
4237 cpu_isset(cpu
, nohz
.cpu_mask
))
4240 if (time_after_eq(jiffies
, rq
->next_balance
))
4241 raise_softirq(SCHED_SOFTIRQ
);
4244 #else /* CONFIG_SMP */
4247 * on UP we do not need to balance between CPUs:
4249 static inline void idle_balance(int cpu
, struct rq
*rq
)
4255 DEFINE_PER_CPU(struct kernel_stat
, kstat
);
4257 EXPORT_PER_CPU_SYMBOL(kstat
);
4260 * Return p->sum_exec_runtime plus any more ns on the sched_clock
4261 * that have not yet been banked in case the task is currently running.
4263 unsigned long long task_sched_runtime(struct task_struct
*p
)
4265 unsigned long flags
;
4269 rq
= task_rq_lock(p
, &flags
);
4270 ns
= p
->se
.sum_exec_runtime
;
4271 if (task_current(rq
, p
)) {
4272 update_rq_clock(rq
);
4273 delta_exec
= rq
->clock
- p
->se
.exec_start
;
4274 if ((s64
)delta_exec
> 0)
4277 task_rq_unlock(rq
, &flags
);
4283 * Account 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(struct task_struct
*p
, cputime_t cputime
)
4289 struct cpu_usage_stat
*cpustat
= &kstat_this_cpu
.cpustat
;
4292 p
->utime
= cputime_add(p
->utime
, cputime
);
4294 /* Add user time to cpustat. */
4295 tmp
= cputime_to_cputime64(cputime
);
4296 if (TASK_NICE(p
) > 0)
4297 cpustat
->nice
= cputime64_add(cpustat
->nice
, tmp
);
4299 cpustat
->user
= cputime64_add(cpustat
->user
, tmp
);
4303 * Account guest cpu time to a process.
4304 * @p: the process that the cpu time gets accounted to
4305 * @cputime: the cpu time spent in virtual machine since the last update
4307 static void account_guest_time(struct task_struct
*p
, cputime_t cputime
)
4310 struct cpu_usage_stat
*cpustat
= &kstat_this_cpu
.cpustat
;
4312 tmp
= cputime_to_cputime64(cputime
);
4314 p
->utime
= cputime_add(p
->utime
, cputime
);
4315 p
->gtime
= cputime_add(p
->gtime
, cputime
);
4317 cpustat
->user
= cputime64_add(cpustat
->user
, tmp
);
4318 cpustat
->guest
= cputime64_add(cpustat
->guest
, tmp
);
4322 * Account scaled user cpu time to a process.
4323 * @p: the process that the cpu time gets accounted to
4324 * @cputime: the cpu time spent in user space since the last update
4326 void account_user_time_scaled(struct task_struct
*p
, cputime_t cputime
)
4328 p
->utimescaled
= cputime_add(p
->utimescaled
, cputime
);
4332 * Account system cpu time to a process.
4333 * @p: the process that the cpu time gets accounted to
4334 * @hardirq_offset: the offset to subtract from hardirq_count()
4335 * @cputime: the cpu time spent in kernel space since the last update
4337 void account_system_time(struct task_struct
*p
, int hardirq_offset
,
4340 struct cpu_usage_stat
*cpustat
= &kstat_this_cpu
.cpustat
;
4341 struct rq
*rq
= this_rq();
4344 if ((p
->flags
& PF_VCPU
) && (irq_count() - hardirq_offset
== 0)) {
4345 account_guest_time(p
, cputime
);
4349 p
->stime
= cputime_add(p
->stime
, cputime
);
4351 /* Add system time to cpustat. */
4352 tmp
= cputime_to_cputime64(cputime
);
4353 if (hardirq_count() - hardirq_offset
)
4354 cpustat
->irq
= cputime64_add(cpustat
->irq
, tmp
);
4355 else if (softirq_count())
4356 cpustat
->softirq
= cputime64_add(cpustat
->softirq
, tmp
);
4357 else if (p
!= rq
->idle
)
4358 cpustat
->system
= cputime64_add(cpustat
->system
, tmp
);
4359 else if (atomic_read(&rq
->nr_iowait
) > 0)
4360 cpustat
->iowait
= cputime64_add(cpustat
->iowait
, tmp
);
4362 cpustat
->idle
= cputime64_add(cpustat
->idle
, tmp
);
4363 /* Account for system time used */
4364 acct_update_integrals(p
);
4368 * Account scaled system cpu time to a process.
4369 * @p: the process that the cpu time gets accounted to
4370 * @hardirq_offset: the offset to subtract from hardirq_count()
4371 * @cputime: the cpu time spent in kernel space since the last update
4373 void account_system_time_scaled(struct task_struct
*p
, cputime_t cputime
)
4375 p
->stimescaled
= cputime_add(p
->stimescaled
, cputime
);
4379 * Account for involuntary wait time.
4380 * @p: the process from which the cpu time has been stolen
4381 * @steal: the cpu time spent in involuntary wait
4383 void account_steal_time(struct task_struct
*p
, cputime_t steal
)
4385 struct cpu_usage_stat
*cpustat
= &kstat_this_cpu
.cpustat
;
4386 cputime64_t tmp
= cputime_to_cputime64(steal
);
4387 struct rq
*rq
= this_rq();
4389 if (p
== rq
->idle
) {
4390 p
->stime
= cputime_add(p
->stime
, steal
);
4391 if (atomic_read(&rq
->nr_iowait
) > 0)
4392 cpustat
->iowait
= cputime64_add(cpustat
->iowait
, tmp
);
4394 cpustat
->idle
= cputime64_add(cpustat
->idle
, tmp
);
4396 cpustat
->steal
= cputime64_add(cpustat
->steal
, tmp
);
4400 * This function gets called by the timer code, with HZ frequency.
4401 * We call it with interrupts disabled.
4403 * It also gets called by the fork code, when changing the parent's
4406 void scheduler_tick(void)
4408 int cpu
= smp_processor_id();
4409 struct rq
*rq
= cpu_rq(cpu
);
4410 struct task_struct
*curr
= rq
->curr
;
4411 u64 next_tick
= rq
->tick_timestamp
+ TICK_NSEC
;
4413 spin_lock(&rq
->lock
);
4414 __update_rq_clock(rq
);
4416 * Let rq->clock advance by at least TICK_NSEC:
4418 if (unlikely(rq
->clock
< next_tick
)) {
4419 rq
->clock
= next_tick
;
4420 rq
->clock_underflows
++;
4422 rq
->tick_timestamp
= rq
->clock
;
4423 update_last_tick_seen(rq
);
4424 update_cpu_load(rq
);
4425 curr
->sched_class
->task_tick(rq
, curr
, 0);
4426 spin_unlock(&rq
->lock
);
4429 rq
->idle_at_tick
= idle_cpu(cpu
);
4430 trigger_load_balance(rq
, cpu
);
4434 #if defined(CONFIG_PREEMPT) && defined(CONFIG_DEBUG_PREEMPT)
4436 void __kprobes
add_preempt_count(int val
)
4441 if (DEBUG_LOCKS_WARN_ON((preempt_count() < 0)))
4443 preempt_count() += val
;
4445 * Spinlock count overflowing soon?
4447 DEBUG_LOCKS_WARN_ON((preempt_count() & PREEMPT_MASK
) >=
4450 EXPORT_SYMBOL(add_preempt_count
);
4452 void __kprobes
sub_preempt_count(int val
)
4457 if (DEBUG_LOCKS_WARN_ON(val
> preempt_count()))
4460 * Is the spinlock portion underflowing?
4462 if (DEBUG_LOCKS_WARN_ON((val
< PREEMPT_MASK
) &&
4463 !(preempt_count() & PREEMPT_MASK
)))
4466 preempt_count() -= val
;
4468 EXPORT_SYMBOL(sub_preempt_count
);
4473 * Print scheduling while atomic bug:
4475 static noinline
void __schedule_bug(struct task_struct
*prev
)
4477 struct pt_regs
*regs
= get_irq_regs();
4479 printk(KERN_ERR
"BUG: scheduling while atomic: %s/%d/0x%08x\n",
4480 prev
->comm
, prev
->pid
, preempt_count());
4482 debug_show_held_locks(prev
);
4483 if (irqs_disabled())
4484 print_irqtrace_events(prev
);
4493 * Various schedule()-time debugging checks and statistics:
4495 static inline void schedule_debug(struct task_struct
*prev
)
4498 * Test if we are atomic. Since do_exit() needs to call into
4499 * schedule() atomically, we ignore that path for now.
4500 * Otherwise, whine if we are scheduling when we should not be.
4502 if (unlikely(in_atomic_preempt_off()) && unlikely(!prev
->exit_state
))
4503 __schedule_bug(prev
);
4505 profile_hit(SCHED_PROFILING
, __builtin_return_address(0));
4507 schedstat_inc(this_rq(), sched_count
);
4508 #ifdef CONFIG_SCHEDSTATS
4509 if (unlikely(prev
->lock_depth
>= 0)) {
4510 schedstat_inc(this_rq(), bkl_count
);
4511 schedstat_inc(prev
, sched_info
.bkl_count
);
4517 * Pick up the highest-prio task:
4519 static inline struct task_struct
*
4520 pick_next_task(struct rq
*rq
, struct task_struct
*prev
)
4522 const struct sched_class
*class;
4523 struct task_struct
*p
;
4526 * Optimization: we know that if all tasks are in
4527 * the fair class we can call that function directly:
4529 if (likely(rq
->nr_running
== rq
->cfs
.nr_running
)) {
4530 p
= fair_sched_class
.pick_next_task(rq
);
4535 class = sched_class_highest
;
4537 p
= class->pick_next_task(rq
);
4541 * Will never be NULL as the idle class always
4542 * returns a non-NULL p:
4544 class = class->next
;
4549 * schedule() is the main scheduler function.
4551 asmlinkage
void __sched
schedule(void)
4553 struct task_struct
*prev
, *next
;
4554 unsigned long *switch_count
;
4560 cpu
= smp_processor_id();
4564 switch_count
= &prev
->nivcsw
;
4566 release_kernel_lock(prev
);
4567 need_resched_nonpreemptible
:
4569 schedule_debug(prev
);
4574 * Do the rq-clock update outside the rq lock:
4576 local_irq_disable();
4577 __update_rq_clock(rq
);
4578 spin_lock(&rq
->lock
);
4579 clear_tsk_need_resched(prev
);
4581 if (prev
->state
&& !(preempt_count() & PREEMPT_ACTIVE
)) {
4582 if (unlikely((prev
->state
& TASK_INTERRUPTIBLE
) &&
4583 signal_pending(prev
))) {
4584 prev
->state
= TASK_RUNNING
;
4586 deactivate_task(rq
, prev
, 1);
4588 switch_count
= &prev
->nvcsw
;
4592 if (prev
->sched_class
->pre_schedule
)
4593 prev
->sched_class
->pre_schedule(rq
, prev
);
4596 if (unlikely(!rq
->nr_running
))
4597 idle_balance(cpu
, rq
);
4599 prev
->sched_class
->put_prev_task(rq
, prev
);
4600 next
= pick_next_task(rq
, prev
);
4602 sched_info_switch(prev
, next
);
4604 if (likely(prev
!= next
)) {
4609 context_switch(rq
, prev
, next
); /* unlocks the rq */
4611 * the context switch might have flipped the stack from under
4612 * us, hence refresh the local variables.
4614 cpu
= smp_processor_id();
4617 spin_unlock_irq(&rq
->lock
);
4621 if (unlikely(reacquire_kernel_lock(current
) < 0))
4622 goto need_resched_nonpreemptible
;
4624 preempt_enable_no_resched();
4625 if (unlikely(test_thread_flag(TIF_NEED_RESCHED
)))
4628 EXPORT_SYMBOL(schedule
);
4630 #ifdef CONFIG_PREEMPT
4632 * this is the entry point to schedule() from in-kernel preemption
4633 * off of preempt_enable. Kernel preemptions off return from interrupt
4634 * occur there and call schedule directly.
4636 asmlinkage
void __sched
preempt_schedule(void)
4638 struct thread_info
*ti
= current_thread_info();
4639 struct task_struct
*task
= current
;
4640 int saved_lock_depth
;
4643 * If there is a non-zero preempt_count or interrupts are disabled,
4644 * we do not want to preempt the current task. Just return..
4646 if (likely(ti
->preempt_count
|| irqs_disabled()))
4650 add_preempt_count(PREEMPT_ACTIVE
);
4653 * We keep the big kernel semaphore locked, but we
4654 * clear ->lock_depth so that schedule() doesnt
4655 * auto-release the semaphore:
4657 saved_lock_depth
= task
->lock_depth
;
4658 task
->lock_depth
= -1;
4660 task
->lock_depth
= saved_lock_depth
;
4661 sub_preempt_count(PREEMPT_ACTIVE
);
4664 * Check again in case we missed a preemption opportunity
4665 * between schedule and now.
4668 } while (unlikely(test_thread_flag(TIF_NEED_RESCHED
)));
4670 EXPORT_SYMBOL(preempt_schedule
);
4673 * this is the entry point to schedule() from kernel preemption
4674 * off of irq context.
4675 * Note, that this is called and return with irqs disabled. This will
4676 * protect us against recursive calling from irq.
4678 asmlinkage
void __sched
preempt_schedule_irq(void)
4680 struct thread_info
*ti
= current_thread_info();
4681 struct task_struct
*task
= current
;
4682 int saved_lock_depth
;
4684 /* Catch callers which need to be fixed */
4685 BUG_ON(ti
->preempt_count
|| !irqs_disabled());
4688 add_preempt_count(PREEMPT_ACTIVE
);
4691 * We keep the big kernel semaphore locked, but we
4692 * clear ->lock_depth so that schedule() doesnt
4693 * auto-release the semaphore:
4695 saved_lock_depth
= task
->lock_depth
;
4696 task
->lock_depth
= -1;
4699 local_irq_disable();
4700 task
->lock_depth
= saved_lock_depth
;
4701 sub_preempt_count(PREEMPT_ACTIVE
);
4704 * Check again in case we missed a preemption opportunity
4705 * between schedule and now.
4708 } while (unlikely(test_thread_flag(TIF_NEED_RESCHED
)));
4711 #endif /* CONFIG_PREEMPT */
4713 int default_wake_function(wait_queue_t
*curr
, unsigned mode
, int sync
,
4716 return try_to_wake_up(curr
->private, mode
, sync
);
4718 EXPORT_SYMBOL(default_wake_function
);
4721 * The core wakeup function. Non-exclusive wakeups (nr_exclusive == 0) just
4722 * wake everything up. If it's an exclusive wakeup (nr_exclusive == small +ve
4723 * number) then we wake all the non-exclusive tasks and one exclusive task.
4725 * There are circumstances in which we can try to wake a task which has already
4726 * started to run but is not in state TASK_RUNNING. try_to_wake_up() returns
4727 * zero in this (rare) case, and we handle it by continuing to scan the queue.
4729 static void __wake_up_common(wait_queue_head_t
*q
, unsigned int mode
,
4730 int nr_exclusive
, int sync
, void *key
)
4732 wait_queue_t
*curr
, *next
;
4734 list_for_each_entry_safe(curr
, next
, &q
->task_list
, task_list
) {
4735 unsigned flags
= curr
->flags
;
4737 if (curr
->func(curr
, mode
, sync
, key
) &&
4738 (flags
& WQ_FLAG_EXCLUSIVE
) && !--nr_exclusive
)
4744 * __wake_up - wake up threads blocked on a waitqueue.
4746 * @mode: which threads
4747 * @nr_exclusive: how many wake-one or wake-many threads to wake up
4748 * @key: is directly passed to the wakeup function
4750 void __wake_up(wait_queue_head_t
*q
, unsigned int mode
,
4751 int nr_exclusive
, void *key
)
4753 unsigned long flags
;
4755 spin_lock_irqsave(&q
->lock
, flags
);
4756 __wake_up_common(q
, mode
, nr_exclusive
, 0, key
);
4757 spin_unlock_irqrestore(&q
->lock
, flags
);
4759 EXPORT_SYMBOL(__wake_up
);
4762 * Same as __wake_up but called with the spinlock in wait_queue_head_t held.
4764 void __wake_up_locked(wait_queue_head_t
*q
, unsigned int mode
)
4766 __wake_up_common(q
, mode
, 1, 0, NULL
);
4770 * __wake_up_sync - wake up threads blocked on a waitqueue.
4772 * @mode: which threads
4773 * @nr_exclusive: how many wake-one or wake-many threads to wake up
4775 * The sync wakeup differs that the waker knows that it will schedule
4776 * away soon, so while the target thread will be woken up, it will not
4777 * be migrated to another CPU - ie. the two threads are 'synchronized'
4778 * with each other. This can prevent needless bouncing between CPUs.
4780 * On UP it can prevent extra preemption.
4783 __wake_up_sync(wait_queue_head_t
*q
, unsigned int mode
, int nr_exclusive
)
4785 unsigned long flags
;
4791 if (unlikely(!nr_exclusive
))
4794 spin_lock_irqsave(&q
->lock
, flags
);
4795 __wake_up_common(q
, mode
, nr_exclusive
, sync
, NULL
);
4796 spin_unlock_irqrestore(&q
->lock
, flags
);
4798 EXPORT_SYMBOL_GPL(__wake_up_sync
); /* For internal use only */
4800 void complete(struct completion
*x
)
4802 unsigned long flags
;
4804 spin_lock_irqsave(&x
->wait
.lock
, flags
);
4806 __wake_up_common(&x
->wait
, TASK_NORMAL
, 1, 0, NULL
);
4807 spin_unlock_irqrestore(&x
->wait
.lock
, flags
);
4809 EXPORT_SYMBOL(complete
);
4811 void complete_all(struct completion
*x
)
4813 unsigned long flags
;
4815 spin_lock_irqsave(&x
->wait
.lock
, flags
);
4816 x
->done
+= UINT_MAX
/2;
4817 __wake_up_common(&x
->wait
, TASK_NORMAL
, 0, 0, NULL
);
4818 spin_unlock_irqrestore(&x
->wait
.lock
, flags
);
4820 EXPORT_SYMBOL(complete_all
);
4822 static inline long __sched
4823 do_wait_for_common(struct completion
*x
, long timeout
, int state
)
4826 DECLARE_WAITQUEUE(wait
, current
);
4828 wait
.flags
|= WQ_FLAG_EXCLUSIVE
;
4829 __add_wait_queue_tail(&x
->wait
, &wait
);
4831 if ((state
== TASK_INTERRUPTIBLE
&&
4832 signal_pending(current
)) ||
4833 (state
== TASK_KILLABLE
&&
4834 fatal_signal_pending(current
))) {
4835 __remove_wait_queue(&x
->wait
, &wait
);
4836 return -ERESTARTSYS
;
4838 __set_current_state(state
);
4839 spin_unlock_irq(&x
->wait
.lock
);
4840 timeout
= schedule_timeout(timeout
);
4841 spin_lock_irq(&x
->wait
.lock
);
4843 __remove_wait_queue(&x
->wait
, &wait
);
4847 __remove_wait_queue(&x
->wait
, &wait
);
4854 wait_for_common(struct completion
*x
, long timeout
, int state
)
4858 spin_lock_irq(&x
->wait
.lock
);
4859 timeout
= do_wait_for_common(x
, timeout
, state
);
4860 spin_unlock_irq(&x
->wait
.lock
);
4864 void __sched
wait_for_completion(struct completion
*x
)
4866 wait_for_common(x
, MAX_SCHEDULE_TIMEOUT
, TASK_UNINTERRUPTIBLE
);
4868 EXPORT_SYMBOL(wait_for_completion
);
4870 unsigned long __sched
4871 wait_for_completion_timeout(struct completion
*x
, unsigned long timeout
)
4873 return wait_for_common(x
, timeout
, TASK_UNINTERRUPTIBLE
);
4875 EXPORT_SYMBOL(wait_for_completion_timeout
);
4877 int __sched
wait_for_completion_interruptible(struct completion
*x
)
4879 long t
= wait_for_common(x
, MAX_SCHEDULE_TIMEOUT
, TASK_INTERRUPTIBLE
);
4880 if (t
== -ERESTARTSYS
)
4884 EXPORT_SYMBOL(wait_for_completion_interruptible
);
4886 unsigned long __sched
4887 wait_for_completion_interruptible_timeout(struct completion
*x
,
4888 unsigned long timeout
)
4890 return wait_for_common(x
, timeout
, TASK_INTERRUPTIBLE
);
4892 EXPORT_SYMBOL(wait_for_completion_interruptible_timeout
);
4894 int __sched
wait_for_completion_killable(struct completion
*x
)
4896 long t
= wait_for_common(x
, MAX_SCHEDULE_TIMEOUT
, TASK_KILLABLE
);
4897 if (t
== -ERESTARTSYS
)
4901 EXPORT_SYMBOL(wait_for_completion_killable
);
4904 sleep_on_common(wait_queue_head_t
*q
, int state
, long timeout
)
4906 unsigned long flags
;
4909 init_waitqueue_entry(&wait
, current
);
4911 __set_current_state(state
);
4913 spin_lock_irqsave(&q
->lock
, flags
);
4914 __add_wait_queue(q
, &wait
);
4915 spin_unlock(&q
->lock
);
4916 timeout
= schedule_timeout(timeout
);
4917 spin_lock_irq(&q
->lock
);
4918 __remove_wait_queue(q
, &wait
);
4919 spin_unlock_irqrestore(&q
->lock
, flags
);
4924 void __sched
interruptible_sleep_on(wait_queue_head_t
*q
)
4926 sleep_on_common(q
, TASK_INTERRUPTIBLE
, MAX_SCHEDULE_TIMEOUT
);
4928 EXPORT_SYMBOL(interruptible_sleep_on
);
4931 interruptible_sleep_on_timeout(wait_queue_head_t
*q
, long timeout
)
4933 return sleep_on_common(q
, TASK_INTERRUPTIBLE
, timeout
);
4935 EXPORT_SYMBOL(interruptible_sleep_on_timeout
);
4937 void __sched
sleep_on(wait_queue_head_t
*q
)
4939 sleep_on_common(q
, TASK_UNINTERRUPTIBLE
, MAX_SCHEDULE_TIMEOUT
);
4941 EXPORT_SYMBOL(sleep_on
);
4943 long __sched
sleep_on_timeout(wait_queue_head_t
*q
, long timeout
)
4945 return sleep_on_common(q
, TASK_UNINTERRUPTIBLE
, timeout
);
4947 EXPORT_SYMBOL(sleep_on_timeout
);
4949 #ifdef CONFIG_RT_MUTEXES
4952 * rt_mutex_setprio - set the current priority of a task
4954 * @prio: prio value (kernel-internal form)
4956 * This function changes the 'effective' priority of a task. It does
4957 * not touch ->normal_prio like __setscheduler().
4959 * Used by the rt_mutex code to implement priority inheritance logic.
4961 void rt_mutex_setprio(struct task_struct
*p
, int prio
)
4963 unsigned long flags
;
4964 int oldprio
, on_rq
, running
;
4966 const struct sched_class
*prev_class
= p
->sched_class
;
4968 BUG_ON(prio
< 0 || prio
> MAX_PRIO
);
4970 rq
= task_rq_lock(p
, &flags
);
4971 update_rq_clock(rq
);
4974 on_rq
= p
->se
.on_rq
;
4975 running
= task_current(rq
, p
);
4977 dequeue_task(rq
, p
, 0);
4979 p
->sched_class
->put_prev_task(rq
, p
);
4982 p
->sched_class
= &rt_sched_class
;
4984 p
->sched_class
= &fair_sched_class
;
4989 p
->sched_class
->set_curr_task(rq
);
4991 enqueue_task(rq
, p
, 0);
4993 check_class_changed(rq
, p
, prev_class
, oldprio
, running
);
4995 task_rq_unlock(rq
, &flags
);
5000 void set_user_nice(struct task_struct
*p
, long nice
)
5002 int old_prio
, delta
, on_rq
;
5003 unsigned long flags
;
5006 if (TASK_NICE(p
) == nice
|| nice
< -20 || nice
> 19)
5009 * We have to be careful, if called from sys_setpriority(),
5010 * the task might be in the middle of scheduling on another CPU.
5012 rq
= task_rq_lock(p
, &flags
);
5013 update_rq_clock(rq
);
5015 * The RT priorities are set via sched_setscheduler(), but we still
5016 * allow the 'normal' nice value to be set - but as expected
5017 * it wont have any effect on scheduling until the task is
5018 * SCHED_FIFO/SCHED_RR:
5020 if (task_has_rt_policy(p
)) {
5021 p
->static_prio
= NICE_TO_PRIO(nice
);
5024 on_rq
= p
->se
.on_rq
;
5026 dequeue_task(rq
, p
, 0);
5028 p
->static_prio
= NICE_TO_PRIO(nice
);
5031 p
->prio
= effective_prio(p
);
5032 delta
= p
->prio
- old_prio
;
5035 enqueue_task(rq
, p
, 0);
5037 * If the task increased its priority or is running and
5038 * lowered its priority, then reschedule its CPU:
5040 if (delta
< 0 || (delta
> 0 && task_running(rq
, p
)))
5041 resched_task(rq
->curr
);
5044 task_rq_unlock(rq
, &flags
);
5046 EXPORT_SYMBOL(set_user_nice
);
5049 * can_nice - check if a task can reduce its nice value
5053 int can_nice(const struct task_struct
*p
, const int nice
)
5055 /* convert nice value [19,-20] to rlimit style value [1,40] */
5056 int nice_rlim
= 20 - nice
;
5058 return (nice_rlim
<= p
->signal
->rlim
[RLIMIT_NICE
].rlim_cur
||
5059 capable(CAP_SYS_NICE
));
5062 #ifdef __ARCH_WANT_SYS_NICE
5065 * sys_nice - change the priority of the current process.
5066 * @increment: priority increment
5068 * sys_setpriority is a more generic, but much slower function that
5069 * does similar things.
5071 asmlinkage
long sys_nice(int increment
)
5076 * Setpriority might change our priority at the same moment.
5077 * We don't have to worry. Conceptually one call occurs first
5078 * and we have a single winner.
5080 if (increment
< -40)
5085 nice
= PRIO_TO_NICE(current
->static_prio
) + increment
;
5091 if (increment
< 0 && !can_nice(current
, nice
))
5094 retval
= security_task_setnice(current
, nice
);
5098 set_user_nice(current
, nice
);
5105 * task_prio - return the priority value of a given task.
5106 * @p: the task in question.
5108 * This is the priority value as seen by users in /proc.
5109 * RT tasks are offset by -200. Normal tasks are centered
5110 * around 0, value goes from -16 to +15.
5112 int task_prio(const struct task_struct
*p
)
5114 return p
->prio
- MAX_RT_PRIO
;
5118 * task_nice - return the nice value of a given task.
5119 * @p: the task in question.
5121 int task_nice(const struct task_struct
*p
)
5123 return TASK_NICE(p
);
5125 EXPORT_SYMBOL(task_nice
);
5128 * idle_cpu - is a given cpu idle currently?
5129 * @cpu: the processor in question.
5131 int idle_cpu(int cpu
)
5133 return cpu_curr(cpu
) == cpu_rq(cpu
)->idle
;
5137 * idle_task - return the idle task for a given cpu.
5138 * @cpu: the processor in question.
5140 struct task_struct
*idle_task(int cpu
)
5142 return cpu_rq(cpu
)->idle
;
5146 * find_process_by_pid - find a process with a matching PID value.
5147 * @pid: the pid in question.
5149 static struct task_struct
*find_process_by_pid(pid_t pid
)
5151 return pid
? find_task_by_vpid(pid
) : current
;
5154 /* Actually do priority change: must hold rq lock. */
5156 __setscheduler(struct rq
*rq
, struct task_struct
*p
, int policy
, int prio
)
5158 BUG_ON(p
->se
.on_rq
);
5161 switch (p
->policy
) {
5165 p
->sched_class
= &fair_sched_class
;
5169 p
->sched_class
= &rt_sched_class
;
5173 p
->rt_priority
= prio
;
5174 p
->normal_prio
= normal_prio(p
);
5175 /* we are holding p->pi_lock already */
5176 p
->prio
= rt_mutex_getprio(p
);
5181 * sched_setscheduler - change the scheduling policy and/or RT priority of a thread.
5182 * @p: the task in question.
5183 * @policy: new policy.
5184 * @param: structure containing the new RT priority.
5186 * NOTE that the task may be already dead.
5188 int sched_setscheduler(struct task_struct
*p
, int policy
,
5189 struct sched_param
*param
)
5191 int retval
, oldprio
, oldpolicy
= -1, on_rq
, running
;
5192 unsigned long flags
;
5193 const struct sched_class
*prev_class
= p
->sched_class
;
5196 /* may grab non-irq protected spin_locks */
5197 BUG_ON(in_interrupt());
5199 /* double check policy once rq lock held */
5201 policy
= oldpolicy
= p
->policy
;
5202 else if (policy
!= SCHED_FIFO
&& policy
!= SCHED_RR
&&
5203 policy
!= SCHED_NORMAL
&& policy
!= SCHED_BATCH
&&
5204 policy
!= SCHED_IDLE
)
5207 * Valid priorities for SCHED_FIFO and SCHED_RR are
5208 * 1..MAX_USER_RT_PRIO-1, valid priority for SCHED_NORMAL,
5209 * SCHED_BATCH and SCHED_IDLE is 0.
5211 if (param
->sched_priority
< 0 ||
5212 (p
->mm
&& param
->sched_priority
> MAX_USER_RT_PRIO
-1) ||
5213 (!p
->mm
&& param
->sched_priority
> MAX_RT_PRIO
-1))
5215 if (rt_policy(policy
) != (param
->sched_priority
!= 0))
5219 * Allow unprivileged RT tasks to decrease priority:
5221 if (!capable(CAP_SYS_NICE
)) {
5222 if (rt_policy(policy
)) {
5223 unsigned long rlim_rtprio
;
5225 if (!lock_task_sighand(p
, &flags
))
5227 rlim_rtprio
= p
->signal
->rlim
[RLIMIT_RTPRIO
].rlim_cur
;
5228 unlock_task_sighand(p
, &flags
);
5230 /* can't set/change the rt policy */
5231 if (policy
!= p
->policy
&& !rlim_rtprio
)
5234 /* can't increase priority */
5235 if (param
->sched_priority
> p
->rt_priority
&&
5236 param
->sched_priority
> rlim_rtprio
)
5240 * Like positive nice levels, dont allow tasks to
5241 * move out of SCHED_IDLE either:
5243 if (p
->policy
== SCHED_IDLE
&& policy
!= SCHED_IDLE
)
5246 /* can't change other user's priorities */
5247 if ((current
->euid
!= p
->euid
) &&
5248 (current
->euid
!= p
->uid
))
5252 #ifdef CONFIG_RT_GROUP_SCHED
5254 * Do not allow realtime tasks into groups that have no runtime
5257 if (rt_policy(policy
) && task_group(p
)->rt_bandwidth
.rt_runtime
== 0)
5261 retval
= security_task_setscheduler(p
, policy
, param
);
5265 * make sure no PI-waiters arrive (or leave) while we are
5266 * changing the priority of the task:
5268 spin_lock_irqsave(&p
->pi_lock
, flags
);
5270 * To be able to change p->policy safely, the apropriate
5271 * runqueue lock must be held.
5273 rq
= __task_rq_lock(p
);
5274 /* recheck policy now with rq lock held */
5275 if (unlikely(oldpolicy
!= -1 && oldpolicy
!= p
->policy
)) {
5276 policy
= oldpolicy
= -1;
5277 __task_rq_unlock(rq
);
5278 spin_unlock_irqrestore(&p
->pi_lock
, flags
);
5281 update_rq_clock(rq
);
5282 on_rq
= p
->se
.on_rq
;
5283 running
= task_current(rq
, p
);
5285 deactivate_task(rq
, p
, 0);
5287 p
->sched_class
->put_prev_task(rq
, p
);
5290 __setscheduler(rq
, p
, policy
, param
->sched_priority
);
5293 p
->sched_class
->set_curr_task(rq
);
5295 activate_task(rq
, p
, 0);
5297 check_class_changed(rq
, p
, prev_class
, oldprio
, running
);
5299 __task_rq_unlock(rq
);
5300 spin_unlock_irqrestore(&p
->pi_lock
, flags
);
5302 rt_mutex_adjust_pi(p
);
5306 EXPORT_SYMBOL_GPL(sched_setscheduler
);
5309 do_sched_setscheduler(pid_t pid
, int policy
, struct sched_param __user
*param
)
5311 struct sched_param lparam
;
5312 struct task_struct
*p
;
5315 if (!param
|| pid
< 0)
5317 if (copy_from_user(&lparam
, param
, sizeof(struct sched_param
)))
5322 p
= find_process_by_pid(pid
);
5324 retval
= sched_setscheduler(p
, policy
, &lparam
);
5331 * sys_sched_setscheduler - set/change the scheduler policy and RT priority
5332 * @pid: the pid in question.
5333 * @policy: new policy.
5334 * @param: structure containing the new RT priority.
5337 sys_sched_setscheduler(pid_t pid
, int policy
, struct sched_param __user
*param
)
5339 /* negative values for policy are not valid */
5343 return do_sched_setscheduler(pid
, policy
, param
);
5347 * sys_sched_setparam - set/change the RT priority of a thread
5348 * @pid: the pid in question.
5349 * @param: structure containing the new RT priority.
5351 asmlinkage
long sys_sched_setparam(pid_t pid
, struct sched_param __user
*param
)
5353 return do_sched_setscheduler(pid
, -1, param
);
5357 * sys_sched_getscheduler - get the policy (scheduling class) of a thread
5358 * @pid: the pid in question.
5360 asmlinkage
long sys_sched_getscheduler(pid_t pid
)
5362 struct task_struct
*p
;
5369 read_lock(&tasklist_lock
);
5370 p
= find_process_by_pid(pid
);
5372 retval
= security_task_getscheduler(p
);
5376 read_unlock(&tasklist_lock
);
5381 * sys_sched_getscheduler - get the RT priority of a thread
5382 * @pid: the pid in question.
5383 * @param: structure containing the RT priority.
5385 asmlinkage
long sys_sched_getparam(pid_t pid
, struct sched_param __user
*param
)
5387 struct sched_param lp
;
5388 struct task_struct
*p
;
5391 if (!param
|| pid
< 0)
5394 read_lock(&tasklist_lock
);
5395 p
= find_process_by_pid(pid
);
5400 retval
= security_task_getscheduler(p
);
5404 lp
.sched_priority
= p
->rt_priority
;
5405 read_unlock(&tasklist_lock
);
5408 * This one might sleep, we cannot do it with a spinlock held ...
5410 retval
= copy_to_user(param
, &lp
, sizeof(*param
)) ? -EFAULT
: 0;
5415 read_unlock(&tasklist_lock
);
5419 long sched_setaffinity(pid_t pid
, const cpumask_t
*in_mask
)
5421 cpumask_t cpus_allowed
;
5422 cpumask_t new_mask
= *in_mask
;
5423 struct task_struct
*p
;
5427 read_lock(&tasklist_lock
);
5429 p
= find_process_by_pid(pid
);
5431 read_unlock(&tasklist_lock
);
5437 * It is not safe to call set_cpus_allowed with the
5438 * tasklist_lock held. We will bump the task_struct's
5439 * usage count and then drop tasklist_lock.
5442 read_unlock(&tasklist_lock
);
5445 if ((current
->euid
!= p
->euid
) && (current
->euid
!= p
->uid
) &&
5446 !capable(CAP_SYS_NICE
))
5449 retval
= security_task_setscheduler(p
, 0, NULL
);
5453 cpuset_cpus_allowed(p
, &cpus_allowed
);
5454 cpus_and(new_mask
, new_mask
, cpus_allowed
);
5456 retval
= set_cpus_allowed_ptr(p
, &new_mask
);
5459 cpuset_cpus_allowed(p
, &cpus_allowed
);
5460 if (!cpus_subset(new_mask
, cpus_allowed
)) {
5462 * We must have raced with a concurrent cpuset
5463 * update. Just reset the cpus_allowed to the
5464 * cpuset's cpus_allowed
5466 new_mask
= cpus_allowed
;
5476 static int get_user_cpu_mask(unsigned long __user
*user_mask_ptr
, unsigned len
,
5477 cpumask_t
*new_mask
)
5479 if (len
< sizeof(cpumask_t
)) {
5480 memset(new_mask
, 0, sizeof(cpumask_t
));
5481 } else if (len
> sizeof(cpumask_t
)) {
5482 len
= sizeof(cpumask_t
);
5484 return copy_from_user(new_mask
, user_mask_ptr
, len
) ? -EFAULT
: 0;
5488 * sys_sched_setaffinity - set the cpu affinity of a process
5489 * @pid: pid of the process
5490 * @len: length in bytes of the bitmask pointed to by user_mask_ptr
5491 * @user_mask_ptr: user-space pointer to the new cpu mask
5493 asmlinkage
long sys_sched_setaffinity(pid_t pid
, unsigned int len
,
5494 unsigned long __user
*user_mask_ptr
)
5499 retval
= get_user_cpu_mask(user_mask_ptr
, len
, &new_mask
);
5503 return sched_setaffinity(pid
, &new_mask
);
5507 * Represents all cpu's present in the system
5508 * In systems capable of hotplug, this map could dynamically grow
5509 * as new cpu's are detected in the system via any platform specific
5510 * method, such as ACPI for e.g.
5513 cpumask_t cpu_present_map __read_mostly
;
5514 EXPORT_SYMBOL(cpu_present_map
);
5517 cpumask_t cpu_online_map __read_mostly
= CPU_MASK_ALL
;
5518 EXPORT_SYMBOL(cpu_online_map
);
5520 cpumask_t cpu_possible_map __read_mostly
= CPU_MASK_ALL
;
5521 EXPORT_SYMBOL(cpu_possible_map
);
5524 long sched_getaffinity(pid_t pid
, cpumask_t
*mask
)
5526 struct task_struct
*p
;
5530 read_lock(&tasklist_lock
);
5533 p
= find_process_by_pid(pid
);
5537 retval
= security_task_getscheduler(p
);
5541 cpus_and(*mask
, p
->cpus_allowed
, cpu_online_map
);
5544 read_unlock(&tasklist_lock
);
5551 * sys_sched_getaffinity - get the cpu affinity of a process
5552 * @pid: pid of the process
5553 * @len: length in bytes of the bitmask pointed to by user_mask_ptr
5554 * @user_mask_ptr: user-space pointer to hold the current cpu mask
5556 asmlinkage
long sys_sched_getaffinity(pid_t pid
, unsigned int len
,
5557 unsigned long __user
*user_mask_ptr
)
5562 if (len
< sizeof(cpumask_t
))
5565 ret
= sched_getaffinity(pid
, &mask
);
5569 if (copy_to_user(user_mask_ptr
, &mask
, sizeof(cpumask_t
)))
5572 return sizeof(cpumask_t
);
5576 * sys_sched_yield - yield the current processor to other threads.
5578 * This function yields the current CPU to other tasks. If there are no
5579 * other threads running on this CPU then this function will return.
5581 asmlinkage
long sys_sched_yield(void)
5583 struct rq
*rq
= this_rq_lock();
5585 schedstat_inc(rq
, yld_count
);
5586 current
->sched_class
->yield_task(rq
);
5589 * Since we are going to call schedule() anyway, there's
5590 * no need to preempt or enable interrupts:
5592 __release(rq
->lock
);
5593 spin_release(&rq
->lock
.dep_map
, 1, _THIS_IP_
);
5594 _raw_spin_unlock(&rq
->lock
);
5595 preempt_enable_no_resched();
5602 static void __cond_resched(void)
5604 #ifdef CONFIG_DEBUG_SPINLOCK_SLEEP
5605 __might_sleep(__FILE__
, __LINE__
);
5608 * The BKS might be reacquired before we have dropped
5609 * PREEMPT_ACTIVE, which could trigger a second
5610 * cond_resched() call.
5613 add_preempt_count(PREEMPT_ACTIVE
);
5615 sub_preempt_count(PREEMPT_ACTIVE
);
5616 } while (need_resched());
5619 #if !defined(CONFIG_PREEMPT) || defined(CONFIG_PREEMPT_VOLUNTARY)
5620 int __sched
_cond_resched(void)
5622 if (need_resched() && !(preempt_count() & PREEMPT_ACTIVE
) &&
5623 system_state
== SYSTEM_RUNNING
) {
5629 EXPORT_SYMBOL(_cond_resched
);
5633 * cond_resched_lock() - if a reschedule is pending, drop the given lock,
5634 * call schedule, and on return reacquire the lock.
5636 * This works OK both with and without CONFIG_PREEMPT. We do strange low-level
5637 * operations here to prevent schedule() from being called twice (once via
5638 * spin_unlock(), once by hand).
5640 int cond_resched_lock(spinlock_t
*lock
)
5642 int resched
= need_resched() && system_state
== SYSTEM_RUNNING
;
5645 if (spin_needbreak(lock
) || resched
) {
5647 if (resched
&& need_resched())
5656 EXPORT_SYMBOL(cond_resched_lock
);
5658 int __sched
cond_resched_softirq(void)
5660 BUG_ON(!in_softirq());
5662 if (need_resched() && system_state
== SYSTEM_RUNNING
) {
5670 EXPORT_SYMBOL(cond_resched_softirq
);
5673 * yield - yield the current processor to other threads.
5675 * This is a shortcut for kernel-space yielding - it marks the
5676 * thread runnable and calls sys_sched_yield().
5678 void __sched
yield(void)
5680 set_current_state(TASK_RUNNING
);
5683 EXPORT_SYMBOL(yield
);
5686 * This task is about to go to sleep on IO. Increment rq->nr_iowait so
5687 * that process accounting knows that this is a task in IO wait state.
5689 * But don't do that if it is a deliberate, throttling IO wait (this task
5690 * has set its backing_dev_info: the queue against which it should throttle)
5692 void __sched
io_schedule(void)
5694 struct rq
*rq
= &__raw_get_cpu_var(runqueues
);
5696 delayacct_blkio_start();
5697 atomic_inc(&rq
->nr_iowait
);
5699 atomic_dec(&rq
->nr_iowait
);
5700 delayacct_blkio_end();
5702 EXPORT_SYMBOL(io_schedule
);
5704 long __sched
io_schedule_timeout(long timeout
)
5706 struct rq
*rq
= &__raw_get_cpu_var(runqueues
);
5709 delayacct_blkio_start();
5710 atomic_inc(&rq
->nr_iowait
);
5711 ret
= schedule_timeout(timeout
);
5712 atomic_dec(&rq
->nr_iowait
);
5713 delayacct_blkio_end();
5718 * sys_sched_get_priority_max - return maximum RT priority.
5719 * @policy: scheduling class.
5721 * this syscall returns the maximum rt_priority that can be used
5722 * by a given scheduling class.
5724 asmlinkage
long sys_sched_get_priority_max(int policy
)
5731 ret
= MAX_USER_RT_PRIO
-1;
5743 * sys_sched_get_priority_min - return minimum RT priority.
5744 * @policy: scheduling class.
5746 * this syscall returns the minimum rt_priority that can be used
5747 * by a given scheduling class.
5749 asmlinkage
long sys_sched_get_priority_min(int policy
)
5767 * sys_sched_rr_get_interval - return the default timeslice of a process.
5768 * @pid: pid of the process.
5769 * @interval: userspace pointer to the timeslice value.
5771 * this syscall writes the default timeslice value of a given process
5772 * into the user-space timespec buffer. A value of '0' means infinity.
5775 long sys_sched_rr_get_interval(pid_t pid
, struct timespec __user
*interval
)
5777 struct task_struct
*p
;
5778 unsigned int time_slice
;
5786 read_lock(&tasklist_lock
);
5787 p
= find_process_by_pid(pid
);
5791 retval
= security_task_getscheduler(p
);
5796 * Time slice is 0 for SCHED_FIFO tasks and for SCHED_OTHER
5797 * tasks that are on an otherwise idle runqueue:
5800 if (p
->policy
== SCHED_RR
) {
5801 time_slice
= DEF_TIMESLICE
;
5802 } else if (p
->policy
!= SCHED_FIFO
) {
5803 struct sched_entity
*se
= &p
->se
;
5804 unsigned long flags
;
5807 rq
= task_rq_lock(p
, &flags
);
5808 if (rq
->cfs
.load
.weight
)
5809 time_slice
= NS_TO_JIFFIES(sched_slice(&rq
->cfs
, se
));
5810 task_rq_unlock(rq
, &flags
);
5812 read_unlock(&tasklist_lock
);
5813 jiffies_to_timespec(time_slice
, &t
);
5814 retval
= copy_to_user(interval
, &t
, sizeof(t
)) ? -EFAULT
: 0;
5818 read_unlock(&tasklist_lock
);
5822 static const char stat_nam
[] = "RSDTtZX";
5824 void sched_show_task(struct task_struct
*p
)
5826 unsigned long free
= 0;
5829 state
= p
->state
? __ffs(p
->state
) + 1 : 0;
5830 printk(KERN_INFO
"%-13.13s %c", p
->comm
,
5831 state
< sizeof(stat_nam
) - 1 ? stat_nam
[state
] : '?');
5832 #if BITS_PER_LONG == 32
5833 if (state
== TASK_RUNNING
)
5834 printk(KERN_CONT
" running ");
5836 printk(KERN_CONT
" %08lx ", thread_saved_pc(p
));
5838 if (state
== TASK_RUNNING
)
5839 printk(KERN_CONT
" running task ");
5841 printk(KERN_CONT
" %016lx ", thread_saved_pc(p
));
5843 #ifdef CONFIG_DEBUG_STACK_USAGE
5845 unsigned long *n
= end_of_stack(p
);
5848 free
= (unsigned long)n
- (unsigned long)end_of_stack(p
);
5851 printk(KERN_CONT
"%5lu %5d %6d\n", free
,
5852 task_pid_nr(p
), task_pid_nr(p
->real_parent
));
5854 show_stack(p
, NULL
);
5857 void show_state_filter(unsigned long state_filter
)
5859 struct task_struct
*g
, *p
;
5861 #if BITS_PER_LONG == 32
5863 " task PC stack pid father\n");
5866 " task PC stack pid father\n");
5868 read_lock(&tasklist_lock
);
5869 do_each_thread(g
, p
) {
5871 * reset the NMI-timeout, listing all files on a slow
5872 * console might take alot of time:
5874 touch_nmi_watchdog();
5875 if (!state_filter
|| (p
->state
& state_filter
))
5877 } while_each_thread(g
, p
);
5879 touch_all_softlockup_watchdogs();
5881 #ifdef CONFIG_SCHED_DEBUG
5882 sysrq_sched_debug_show();
5884 read_unlock(&tasklist_lock
);
5886 * Only show locks if all tasks are dumped:
5888 if (state_filter
== -1)
5889 debug_show_all_locks();
5892 void __cpuinit
init_idle_bootup_task(struct task_struct
*idle
)
5894 idle
->sched_class
= &idle_sched_class
;
5898 * init_idle - set up an idle thread for a given CPU
5899 * @idle: task in question
5900 * @cpu: cpu the idle task belongs to
5902 * NOTE: this function does not set the idle thread's NEED_RESCHED
5903 * flag, to make booting more robust.
5905 void __cpuinit
init_idle(struct task_struct
*idle
, int cpu
)
5907 struct rq
*rq
= cpu_rq(cpu
);
5908 unsigned long flags
;
5911 idle
->se
.exec_start
= sched_clock();
5913 idle
->prio
= idle
->normal_prio
= MAX_PRIO
;
5914 idle
->cpus_allowed
= cpumask_of_cpu(cpu
);
5915 __set_task_cpu(idle
, cpu
);
5917 spin_lock_irqsave(&rq
->lock
, flags
);
5918 rq
->curr
= rq
->idle
= idle
;
5919 #if defined(CONFIG_SMP) && defined(__ARCH_WANT_UNLOCKED_CTXSW)
5922 spin_unlock_irqrestore(&rq
->lock
, flags
);
5924 /* Set the preempt count _outside_ the spinlocks! */
5925 task_thread_info(idle
)->preempt_count
= 0;
5928 * The idle tasks have their own, simple scheduling class:
5930 idle
->sched_class
= &idle_sched_class
;
5934 * In a system that switches off the HZ timer nohz_cpu_mask
5935 * indicates which cpus entered this state. This is used
5936 * in the rcu update to wait only for active cpus. For system
5937 * which do not switch off the HZ timer nohz_cpu_mask should
5938 * always be CPU_MASK_NONE.
5940 cpumask_t nohz_cpu_mask
= CPU_MASK_NONE
;
5943 * Increase the granularity value when there are more CPUs,
5944 * because with more CPUs the 'effective latency' as visible
5945 * to users decreases. But the relationship is not linear,
5946 * so pick a second-best guess by going with the log2 of the
5949 * This idea comes from the SD scheduler of Con Kolivas:
5951 static inline void sched_init_granularity(void)
5953 unsigned int factor
= 1 + ilog2(num_online_cpus());
5954 const unsigned long limit
= 200000000;
5956 sysctl_sched_min_granularity
*= factor
;
5957 if (sysctl_sched_min_granularity
> limit
)
5958 sysctl_sched_min_granularity
= limit
;
5960 sysctl_sched_latency
*= factor
;
5961 if (sysctl_sched_latency
> limit
)
5962 sysctl_sched_latency
= limit
;
5964 sysctl_sched_wakeup_granularity
*= factor
;
5969 * This is how migration works:
5971 * 1) we queue a struct migration_req structure in the source CPU's
5972 * runqueue and wake up that CPU's migration thread.
5973 * 2) we down() the locked semaphore => thread blocks.
5974 * 3) migration thread wakes up (implicitly it forces the migrated
5975 * thread off the CPU)
5976 * 4) it gets the migration request and checks whether the migrated
5977 * task is still in the wrong runqueue.
5978 * 5) if it's in the wrong runqueue then the migration thread removes
5979 * it and puts it into the right queue.
5980 * 6) migration thread up()s the semaphore.
5981 * 7) we wake up and the migration is done.
5985 * Change a given task's CPU affinity. Migrate the thread to a
5986 * proper CPU and schedule it away if the CPU it's executing on
5987 * is removed from the allowed bitmask.
5989 * NOTE: the caller must have a valid reference to the task, the
5990 * task must not exit() & deallocate itself prematurely. The
5991 * call is not atomic; no spinlocks may be held.
5993 int set_cpus_allowed_ptr(struct task_struct
*p
, const cpumask_t
*new_mask
)
5995 struct migration_req req
;
5996 unsigned long flags
;
6000 rq
= task_rq_lock(p
, &flags
);
6001 if (!cpus_intersects(*new_mask
, cpu_online_map
)) {
6006 if (p
->sched_class
->set_cpus_allowed
)
6007 p
->sched_class
->set_cpus_allowed(p
, new_mask
);
6009 p
->cpus_allowed
= *new_mask
;
6010 p
->rt
.nr_cpus_allowed
= cpus_weight(*new_mask
);
6013 /* Can the task run on the task's current CPU? If so, we're done */
6014 if (cpu_isset(task_cpu(p
), *new_mask
))
6017 if (migrate_task(p
, any_online_cpu(*new_mask
), &req
)) {
6018 /* Need help from migration thread: drop lock and wait. */
6019 task_rq_unlock(rq
, &flags
);
6020 wake_up_process(rq
->migration_thread
);
6021 wait_for_completion(&req
.done
);
6022 tlb_migrate_finish(p
->mm
);
6026 task_rq_unlock(rq
, &flags
);
6030 EXPORT_SYMBOL_GPL(set_cpus_allowed_ptr
);
6033 * Move (not current) task off this cpu, onto dest cpu. We're doing
6034 * this because either it can't run here any more (set_cpus_allowed()
6035 * away from this CPU, or CPU going down), or because we're
6036 * attempting to rebalance this task on exec (sched_exec).
6038 * So we race with normal scheduler movements, but that's OK, as long
6039 * as the task is no longer on this CPU.
6041 * Returns non-zero if task was successfully migrated.
6043 static int __migrate_task(struct task_struct
*p
, int src_cpu
, int dest_cpu
)
6045 struct rq
*rq_dest
, *rq_src
;
6048 if (unlikely(cpu_is_offline(dest_cpu
)))
6051 rq_src
= cpu_rq(src_cpu
);
6052 rq_dest
= cpu_rq(dest_cpu
);
6054 double_rq_lock(rq_src
, rq_dest
);
6055 /* Already moved. */
6056 if (task_cpu(p
) != src_cpu
)
6058 /* Affinity changed (again). */
6059 if (!cpu_isset(dest_cpu
, p
->cpus_allowed
))
6062 on_rq
= p
->se
.on_rq
;
6064 deactivate_task(rq_src
, p
, 0);
6066 set_task_cpu(p
, dest_cpu
);
6068 activate_task(rq_dest
, p
, 0);
6069 check_preempt_curr(rq_dest
, p
);
6073 double_rq_unlock(rq_src
, rq_dest
);
6078 * migration_thread - this is a highprio system thread that performs
6079 * thread migration by bumping thread off CPU then 'pushing' onto
6082 static int migration_thread(void *data
)
6084 int cpu
= (long)data
;
6088 BUG_ON(rq
->migration_thread
!= current
);
6090 set_current_state(TASK_INTERRUPTIBLE
);
6091 while (!kthread_should_stop()) {
6092 struct migration_req
*req
;
6093 struct list_head
*head
;
6095 spin_lock_irq(&rq
->lock
);
6097 if (cpu_is_offline(cpu
)) {
6098 spin_unlock_irq(&rq
->lock
);
6102 if (rq
->active_balance
) {
6103 active_load_balance(rq
, cpu
);
6104 rq
->active_balance
= 0;
6107 head
= &rq
->migration_queue
;
6109 if (list_empty(head
)) {
6110 spin_unlock_irq(&rq
->lock
);
6112 set_current_state(TASK_INTERRUPTIBLE
);
6115 req
= list_entry(head
->next
, struct migration_req
, list
);
6116 list_del_init(head
->next
);
6118 spin_unlock(&rq
->lock
);
6119 __migrate_task(req
->task
, cpu
, req
->dest_cpu
);
6122 complete(&req
->done
);
6124 __set_current_state(TASK_RUNNING
);
6128 /* Wait for kthread_stop */
6129 set_current_state(TASK_INTERRUPTIBLE
);
6130 while (!kthread_should_stop()) {
6132 set_current_state(TASK_INTERRUPTIBLE
);
6134 __set_current_state(TASK_RUNNING
);
6138 #ifdef CONFIG_HOTPLUG_CPU
6140 static int __migrate_task_irq(struct task_struct
*p
, int src_cpu
, int dest_cpu
)
6144 local_irq_disable();
6145 ret
= __migrate_task(p
, src_cpu
, dest_cpu
);
6151 * Figure out where task on dead CPU should go, use force if necessary.
6152 * NOTE: interrupts should be disabled by the caller
6154 static void move_task_off_dead_cpu(int dead_cpu
, struct task_struct
*p
)
6156 unsigned long flags
;
6163 mask
= node_to_cpumask(cpu_to_node(dead_cpu
));
6164 cpus_and(mask
, mask
, p
->cpus_allowed
);
6165 dest_cpu
= any_online_cpu(mask
);
6167 /* On any allowed CPU? */
6168 if (dest_cpu
>= nr_cpu_ids
)
6169 dest_cpu
= any_online_cpu(p
->cpus_allowed
);
6171 /* No more Mr. Nice Guy. */
6172 if (dest_cpu
>= nr_cpu_ids
) {
6173 cpumask_t cpus_allowed
;
6175 cpuset_cpus_allowed_locked(p
, &cpus_allowed
);
6177 * Try to stay on the same cpuset, where the
6178 * current cpuset may be a subset of all cpus.
6179 * The cpuset_cpus_allowed_locked() variant of
6180 * cpuset_cpus_allowed() will not block. It must be
6181 * called within calls to cpuset_lock/cpuset_unlock.
6183 rq
= task_rq_lock(p
, &flags
);
6184 p
->cpus_allowed
= cpus_allowed
;
6185 dest_cpu
= any_online_cpu(p
->cpus_allowed
);
6186 task_rq_unlock(rq
, &flags
);
6189 * Don't tell them about moving exiting tasks or
6190 * kernel threads (both mm NULL), since they never
6193 if (p
->mm
&& printk_ratelimit()) {
6194 printk(KERN_INFO
"process %d (%s) no "
6195 "longer affine to cpu%d\n",
6196 task_pid_nr(p
), p
->comm
, dead_cpu
);
6199 } while (!__migrate_task_irq(p
, dead_cpu
, dest_cpu
));
6203 * While a dead CPU has no uninterruptible tasks queued at this point,
6204 * it might still have a nonzero ->nr_uninterruptible counter, because
6205 * for performance reasons the counter is not stricly tracking tasks to
6206 * their home CPUs. So we just add the counter to another CPU's counter,
6207 * to keep the global sum constant after CPU-down:
6209 static void migrate_nr_uninterruptible(struct rq
*rq_src
)
6211 struct rq
*rq_dest
= cpu_rq(any_online_cpu(*CPU_MASK_ALL_PTR
));
6212 unsigned long flags
;
6214 local_irq_save(flags
);
6215 double_rq_lock(rq_src
, rq_dest
);
6216 rq_dest
->nr_uninterruptible
+= rq_src
->nr_uninterruptible
;
6217 rq_src
->nr_uninterruptible
= 0;
6218 double_rq_unlock(rq_src
, rq_dest
);
6219 local_irq_restore(flags
);
6222 /* Run through task list and migrate tasks from the dead cpu. */
6223 static void migrate_live_tasks(int src_cpu
)
6225 struct task_struct
*p
, *t
;
6227 read_lock(&tasklist_lock
);
6229 do_each_thread(t
, p
) {
6233 if (task_cpu(p
) == src_cpu
)
6234 move_task_off_dead_cpu(src_cpu
, p
);
6235 } while_each_thread(t
, p
);
6237 read_unlock(&tasklist_lock
);
6241 * Schedules idle task to be the next runnable task on current CPU.
6242 * It does so by boosting its priority to highest possible.
6243 * Used by CPU offline code.
6245 void sched_idle_next(void)
6247 int this_cpu
= smp_processor_id();
6248 struct rq
*rq
= cpu_rq(this_cpu
);
6249 struct task_struct
*p
= rq
->idle
;
6250 unsigned long flags
;
6252 /* cpu has to be offline */
6253 BUG_ON(cpu_online(this_cpu
));
6256 * Strictly not necessary since rest of the CPUs are stopped by now
6257 * and interrupts disabled on the current cpu.
6259 spin_lock_irqsave(&rq
->lock
, flags
);
6261 __setscheduler(rq
, p
, SCHED_FIFO
, MAX_RT_PRIO
-1);
6263 update_rq_clock(rq
);
6264 activate_task(rq
, p
, 0);
6266 spin_unlock_irqrestore(&rq
->lock
, flags
);
6270 * Ensures that the idle task is using init_mm right before its cpu goes
6273 void idle_task_exit(void)
6275 struct mm_struct
*mm
= current
->active_mm
;
6277 BUG_ON(cpu_online(smp_processor_id()));
6280 switch_mm(mm
, &init_mm
, current
);
6284 /* called under rq->lock with disabled interrupts */
6285 static void migrate_dead(unsigned int dead_cpu
, struct task_struct
*p
)
6287 struct rq
*rq
= cpu_rq(dead_cpu
);
6289 /* Must be exiting, otherwise would be on tasklist. */
6290 BUG_ON(!p
->exit_state
);
6292 /* Cannot have done final schedule yet: would have vanished. */
6293 BUG_ON(p
->state
== TASK_DEAD
);
6298 * Drop lock around migration; if someone else moves it,
6299 * that's OK. No task can be added to this CPU, so iteration is
6302 spin_unlock_irq(&rq
->lock
);
6303 move_task_off_dead_cpu(dead_cpu
, p
);
6304 spin_lock_irq(&rq
->lock
);
6309 /* release_task() removes task from tasklist, so we won't find dead tasks. */
6310 static void migrate_dead_tasks(unsigned int dead_cpu
)
6312 struct rq
*rq
= cpu_rq(dead_cpu
);
6313 struct task_struct
*next
;
6316 if (!rq
->nr_running
)
6318 update_rq_clock(rq
);
6319 next
= pick_next_task(rq
, rq
->curr
);
6322 migrate_dead(dead_cpu
, next
);
6326 #endif /* CONFIG_HOTPLUG_CPU */
6328 #if defined(CONFIG_SCHED_DEBUG) && defined(CONFIG_SYSCTL)
6330 static struct ctl_table sd_ctl_dir
[] = {
6332 .procname
= "sched_domain",
6338 static struct ctl_table sd_ctl_root
[] = {
6340 .ctl_name
= CTL_KERN
,
6341 .procname
= "kernel",
6343 .child
= sd_ctl_dir
,
6348 static struct ctl_table
*sd_alloc_ctl_entry(int n
)
6350 struct ctl_table
*entry
=
6351 kcalloc(n
, sizeof(struct ctl_table
), GFP_KERNEL
);
6356 static void sd_free_ctl_entry(struct ctl_table
**tablep
)
6358 struct ctl_table
*entry
;
6361 * In the intermediate directories, both the child directory and
6362 * procname are dynamically allocated and could fail but the mode
6363 * will always be set. In the lowest directory the names are
6364 * static strings and all have proc handlers.
6366 for (entry
= *tablep
; entry
->mode
; entry
++) {
6368 sd_free_ctl_entry(&entry
->child
);
6369 if (entry
->proc_handler
== NULL
)
6370 kfree(entry
->procname
);
6378 set_table_entry(struct ctl_table
*entry
,
6379 const char *procname
, void *data
, int maxlen
,
6380 mode_t mode
, proc_handler
*proc_handler
)
6382 entry
->procname
= procname
;
6384 entry
->maxlen
= maxlen
;
6386 entry
->proc_handler
= proc_handler
;
6389 static struct ctl_table
*
6390 sd_alloc_ctl_domain_table(struct sched_domain
*sd
)
6392 struct ctl_table
*table
= sd_alloc_ctl_entry(12);
6397 set_table_entry(&table
[0], "min_interval", &sd
->min_interval
,
6398 sizeof(long), 0644, proc_doulongvec_minmax
);
6399 set_table_entry(&table
[1], "max_interval", &sd
->max_interval
,
6400 sizeof(long), 0644, proc_doulongvec_minmax
);
6401 set_table_entry(&table
[2], "busy_idx", &sd
->busy_idx
,
6402 sizeof(int), 0644, proc_dointvec_minmax
);
6403 set_table_entry(&table
[3], "idle_idx", &sd
->idle_idx
,
6404 sizeof(int), 0644, proc_dointvec_minmax
);
6405 set_table_entry(&table
[4], "newidle_idx", &sd
->newidle_idx
,
6406 sizeof(int), 0644, proc_dointvec_minmax
);
6407 set_table_entry(&table
[5], "wake_idx", &sd
->wake_idx
,
6408 sizeof(int), 0644, proc_dointvec_minmax
);
6409 set_table_entry(&table
[6], "forkexec_idx", &sd
->forkexec_idx
,
6410 sizeof(int), 0644, proc_dointvec_minmax
);
6411 set_table_entry(&table
[7], "busy_factor", &sd
->busy_factor
,
6412 sizeof(int), 0644, proc_dointvec_minmax
);
6413 set_table_entry(&table
[8], "imbalance_pct", &sd
->imbalance_pct
,
6414 sizeof(int), 0644, proc_dointvec_minmax
);
6415 set_table_entry(&table
[9], "cache_nice_tries",
6416 &sd
->cache_nice_tries
,
6417 sizeof(int), 0644, proc_dointvec_minmax
);
6418 set_table_entry(&table
[10], "flags", &sd
->flags
,
6419 sizeof(int), 0644, proc_dointvec_minmax
);
6420 /* &table[11] is terminator */
6425 static ctl_table
*sd_alloc_ctl_cpu_table(int cpu
)
6427 struct ctl_table
*entry
, *table
;
6428 struct sched_domain
*sd
;
6429 int domain_num
= 0, i
;
6432 for_each_domain(cpu
, sd
)
6434 entry
= table
= sd_alloc_ctl_entry(domain_num
+ 1);
6439 for_each_domain(cpu
, sd
) {
6440 snprintf(buf
, 32, "domain%d", i
);
6441 entry
->procname
= kstrdup(buf
, GFP_KERNEL
);
6443 entry
->child
= sd_alloc_ctl_domain_table(sd
);
6450 static struct ctl_table_header
*sd_sysctl_header
;
6451 static void register_sched_domain_sysctl(void)
6453 int i
, cpu_num
= num_online_cpus();
6454 struct ctl_table
*entry
= sd_alloc_ctl_entry(cpu_num
+ 1);
6457 WARN_ON(sd_ctl_dir
[0].child
);
6458 sd_ctl_dir
[0].child
= entry
;
6463 for_each_online_cpu(i
) {
6464 snprintf(buf
, 32, "cpu%d", i
);
6465 entry
->procname
= kstrdup(buf
, GFP_KERNEL
);
6467 entry
->child
= sd_alloc_ctl_cpu_table(i
);
6471 WARN_ON(sd_sysctl_header
);
6472 sd_sysctl_header
= register_sysctl_table(sd_ctl_root
);
6475 /* may be called multiple times per register */
6476 static void unregister_sched_domain_sysctl(void)
6478 if (sd_sysctl_header
)
6479 unregister_sysctl_table(sd_sysctl_header
);
6480 sd_sysctl_header
= NULL
;
6481 if (sd_ctl_dir
[0].child
)
6482 sd_free_ctl_entry(&sd_ctl_dir
[0].child
);
6485 static void register_sched_domain_sysctl(void)
6488 static void unregister_sched_domain_sysctl(void)
6494 * migration_call - callback that gets triggered when a CPU is added.
6495 * Here we can start up the necessary migration thread for the new CPU.
6497 static int __cpuinit
6498 migration_call(struct notifier_block
*nfb
, unsigned long action
, void *hcpu
)
6500 struct task_struct
*p
;
6501 int cpu
= (long)hcpu
;
6502 unsigned long flags
;
6507 case CPU_UP_PREPARE
:
6508 case CPU_UP_PREPARE_FROZEN
:
6509 p
= kthread_create(migration_thread
, hcpu
, "migration/%d", cpu
);
6512 kthread_bind(p
, cpu
);
6513 /* Must be high prio: stop_machine expects to yield to it. */
6514 rq
= task_rq_lock(p
, &flags
);
6515 __setscheduler(rq
, p
, SCHED_FIFO
, MAX_RT_PRIO
-1);
6516 task_rq_unlock(rq
, &flags
);
6517 cpu_rq(cpu
)->migration_thread
= p
;
6521 case CPU_ONLINE_FROZEN
:
6522 /* Strictly unnecessary, as first user will wake it. */
6523 wake_up_process(cpu_rq(cpu
)->migration_thread
);
6525 /* Update our root-domain */
6527 spin_lock_irqsave(&rq
->lock
, flags
);
6529 BUG_ON(!cpu_isset(cpu
, rq
->rd
->span
));
6530 cpu_set(cpu
, rq
->rd
->online
);
6532 spin_unlock_irqrestore(&rq
->lock
, flags
);
6535 #ifdef CONFIG_HOTPLUG_CPU
6536 case CPU_UP_CANCELED
:
6537 case CPU_UP_CANCELED_FROZEN
:
6538 if (!cpu_rq(cpu
)->migration_thread
)
6540 /* Unbind it from offline cpu so it can run. Fall thru. */
6541 kthread_bind(cpu_rq(cpu
)->migration_thread
,
6542 any_online_cpu(cpu_online_map
));
6543 kthread_stop(cpu_rq(cpu
)->migration_thread
);
6544 cpu_rq(cpu
)->migration_thread
= NULL
;
6548 case CPU_DEAD_FROZEN
:
6549 cpuset_lock(); /* around calls to cpuset_cpus_allowed_lock() */
6550 migrate_live_tasks(cpu
);
6552 kthread_stop(rq
->migration_thread
);
6553 rq
->migration_thread
= NULL
;
6554 /* Idle task back to normal (off runqueue, low prio) */
6555 spin_lock_irq(&rq
->lock
);
6556 update_rq_clock(rq
);
6557 deactivate_task(rq
, rq
->idle
, 0);
6558 rq
->idle
->static_prio
= MAX_PRIO
;
6559 __setscheduler(rq
, rq
->idle
, SCHED_NORMAL
, 0);
6560 rq
->idle
->sched_class
= &idle_sched_class
;
6561 migrate_dead_tasks(cpu
);
6562 spin_unlock_irq(&rq
->lock
);
6564 migrate_nr_uninterruptible(rq
);
6565 BUG_ON(rq
->nr_running
!= 0);
6568 * No need to migrate the tasks: it was best-effort if
6569 * they didn't take sched_hotcpu_mutex. Just wake up
6572 spin_lock_irq(&rq
->lock
);
6573 while (!list_empty(&rq
->migration_queue
)) {
6574 struct migration_req
*req
;
6576 req
= list_entry(rq
->migration_queue
.next
,
6577 struct migration_req
, list
);
6578 list_del_init(&req
->list
);
6579 complete(&req
->done
);
6581 spin_unlock_irq(&rq
->lock
);
6585 case CPU_DYING_FROZEN
:
6586 /* Update our root-domain */
6588 spin_lock_irqsave(&rq
->lock
, flags
);
6590 BUG_ON(!cpu_isset(cpu
, rq
->rd
->span
));
6591 cpu_clear(cpu
, rq
->rd
->online
);
6593 spin_unlock_irqrestore(&rq
->lock
, flags
);
6600 /* Register at highest priority so that task migration (migrate_all_tasks)
6601 * happens before everything else.
6603 static struct notifier_block __cpuinitdata migration_notifier
= {
6604 .notifier_call
= migration_call
,
6608 void __init
migration_init(void)
6610 void *cpu
= (void *)(long)smp_processor_id();
6613 /* Start one for the boot CPU: */
6614 err
= migration_call(&migration_notifier
, CPU_UP_PREPARE
, cpu
);
6615 BUG_ON(err
== NOTIFY_BAD
);
6616 migration_call(&migration_notifier
, CPU_ONLINE
, cpu
);
6617 register_cpu_notifier(&migration_notifier
);
6623 #ifdef CONFIG_SCHED_DEBUG
6625 static int sched_domain_debug_one(struct sched_domain
*sd
, int cpu
, int level
,
6626 cpumask_t
*groupmask
)
6628 struct sched_group
*group
= sd
->groups
;
6631 cpulist_scnprintf(str
, sizeof(str
), sd
->span
);
6632 cpus_clear(*groupmask
);
6634 printk(KERN_DEBUG
"%*s domain %d: ", level
, "", level
);
6636 if (!(sd
->flags
& SD_LOAD_BALANCE
)) {
6637 printk("does not load-balance\n");
6639 printk(KERN_ERR
"ERROR: !SD_LOAD_BALANCE domain"
6644 printk(KERN_CONT
"span %s\n", str
);
6646 if (!cpu_isset(cpu
, sd
->span
)) {
6647 printk(KERN_ERR
"ERROR: domain->span does not contain "
6650 if (!cpu_isset(cpu
, group
->cpumask
)) {
6651 printk(KERN_ERR
"ERROR: domain->groups does not contain"
6655 printk(KERN_DEBUG
"%*s groups:", level
+ 1, "");
6659 printk(KERN_ERR
"ERROR: group is NULL\n");
6663 if (!group
->__cpu_power
) {
6664 printk(KERN_CONT
"\n");
6665 printk(KERN_ERR
"ERROR: domain->cpu_power not "
6670 if (!cpus_weight(group
->cpumask
)) {
6671 printk(KERN_CONT
"\n");
6672 printk(KERN_ERR
"ERROR: empty group\n");
6676 if (cpus_intersects(*groupmask
, group
->cpumask
)) {
6677 printk(KERN_CONT
"\n");
6678 printk(KERN_ERR
"ERROR: repeated CPUs\n");
6682 cpus_or(*groupmask
, *groupmask
, group
->cpumask
);
6684 cpulist_scnprintf(str
, sizeof(str
), group
->cpumask
);
6685 printk(KERN_CONT
" %s", str
);
6687 group
= group
->next
;
6688 } while (group
!= sd
->groups
);
6689 printk(KERN_CONT
"\n");
6691 if (!cpus_equal(sd
->span
, *groupmask
))
6692 printk(KERN_ERR
"ERROR: groups don't span domain->span\n");
6694 if (sd
->parent
&& !cpus_subset(*groupmask
, sd
->parent
->span
))
6695 printk(KERN_ERR
"ERROR: parent span is not a superset "
6696 "of domain->span\n");
6700 static void sched_domain_debug(struct sched_domain
*sd
, int cpu
)
6702 cpumask_t
*groupmask
;
6706 printk(KERN_DEBUG
"CPU%d attaching NULL sched-domain.\n", cpu
);
6710 printk(KERN_DEBUG
"CPU%d attaching sched-domain:\n", cpu
);
6712 groupmask
= kmalloc(sizeof(cpumask_t
), GFP_KERNEL
);
6714 printk(KERN_DEBUG
"Cannot load-balance (out of memory)\n");
6719 if (sched_domain_debug_one(sd
, cpu
, level
, groupmask
))
6729 # define sched_domain_debug(sd, cpu) do { } while (0)
6732 static int sd_degenerate(struct sched_domain
*sd
)
6734 if (cpus_weight(sd
->span
) == 1)
6737 /* Following flags need at least 2 groups */
6738 if (sd
->flags
& (SD_LOAD_BALANCE
|
6739 SD_BALANCE_NEWIDLE
|
6743 SD_SHARE_PKG_RESOURCES
)) {
6744 if (sd
->groups
!= sd
->groups
->next
)
6748 /* Following flags don't use groups */
6749 if (sd
->flags
& (SD_WAKE_IDLE
|
6758 sd_parent_degenerate(struct sched_domain
*sd
, struct sched_domain
*parent
)
6760 unsigned long cflags
= sd
->flags
, pflags
= parent
->flags
;
6762 if (sd_degenerate(parent
))
6765 if (!cpus_equal(sd
->span
, parent
->span
))
6768 /* Does parent contain flags not in child? */
6769 /* WAKE_BALANCE is a subset of WAKE_AFFINE */
6770 if (cflags
& SD_WAKE_AFFINE
)
6771 pflags
&= ~SD_WAKE_BALANCE
;
6772 /* Flags needing groups don't count if only 1 group in parent */
6773 if (parent
->groups
== parent
->groups
->next
) {
6774 pflags
&= ~(SD_LOAD_BALANCE
|
6775 SD_BALANCE_NEWIDLE
|
6779 SD_SHARE_PKG_RESOURCES
);
6781 if (~cflags
& pflags
)
6787 static void rq_attach_root(struct rq
*rq
, struct root_domain
*rd
)
6789 unsigned long flags
;
6790 const struct sched_class
*class;
6792 spin_lock_irqsave(&rq
->lock
, flags
);
6795 struct root_domain
*old_rd
= rq
->rd
;
6797 for (class = sched_class_highest
; class; class = class->next
) {
6798 if (class->leave_domain
)
6799 class->leave_domain(rq
);
6802 cpu_clear(rq
->cpu
, old_rd
->span
);
6803 cpu_clear(rq
->cpu
, old_rd
->online
);
6805 if (atomic_dec_and_test(&old_rd
->refcount
))
6809 atomic_inc(&rd
->refcount
);
6812 cpu_set(rq
->cpu
, rd
->span
);
6813 if (cpu_isset(rq
->cpu
, cpu_online_map
))
6814 cpu_set(rq
->cpu
, rd
->online
);
6816 for (class = sched_class_highest
; class; class = class->next
) {
6817 if (class->join_domain
)
6818 class->join_domain(rq
);
6821 spin_unlock_irqrestore(&rq
->lock
, flags
);
6824 static void init_rootdomain(struct root_domain
*rd
)
6826 memset(rd
, 0, sizeof(*rd
));
6828 cpus_clear(rd
->span
);
6829 cpus_clear(rd
->online
);
6832 static void init_defrootdomain(void)
6834 init_rootdomain(&def_root_domain
);
6835 atomic_set(&def_root_domain
.refcount
, 1);
6838 static struct root_domain
*alloc_rootdomain(void)
6840 struct root_domain
*rd
;
6842 rd
= kmalloc(sizeof(*rd
), GFP_KERNEL
);
6846 init_rootdomain(rd
);
6852 * Attach the domain 'sd' to 'cpu' as its base domain. Callers must
6853 * hold the hotplug lock.
6856 cpu_attach_domain(struct sched_domain
*sd
, struct root_domain
*rd
, int cpu
)
6858 struct rq
*rq
= cpu_rq(cpu
);
6859 struct sched_domain
*tmp
;
6861 /* Remove the sched domains which do not contribute to scheduling. */
6862 for (tmp
= sd
; tmp
; tmp
= tmp
->parent
) {
6863 struct sched_domain
*parent
= tmp
->parent
;
6866 if (sd_parent_degenerate(tmp
, parent
)) {
6867 tmp
->parent
= parent
->parent
;
6869 parent
->parent
->child
= tmp
;
6873 if (sd
&& sd_degenerate(sd
)) {
6879 sched_domain_debug(sd
, cpu
);
6881 rq_attach_root(rq
, rd
);
6882 rcu_assign_pointer(rq
->sd
, sd
);
6885 /* cpus with isolated domains */
6886 static cpumask_t cpu_isolated_map
= CPU_MASK_NONE
;
6888 /* Setup the mask of cpus configured for isolated domains */
6889 static int __init
isolated_cpu_setup(char *str
)
6891 int ints
[NR_CPUS
], i
;
6893 str
= get_options(str
, ARRAY_SIZE(ints
), ints
);
6894 cpus_clear(cpu_isolated_map
);
6895 for (i
= 1; i
<= ints
[0]; i
++)
6896 if (ints
[i
] < NR_CPUS
)
6897 cpu_set(ints
[i
], cpu_isolated_map
);
6901 __setup("isolcpus=", isolated_cpu_setup
);
6904 * init_sched_build_groups takes the cpumask we wish to span, and a pointer
6905 * to a function which identifies what group(along with sched group) a CPU
6906 * belongs to. The return value of group_fn must be a >= 0 and < NR_CPUS
6907 * (due to the fact that we keep track of groups covered with a cpumask_t).
6909 * init_sched_build_groups will build a circular linked list of the groups
6910 * covered by the given span, and will set each group's ->cpumask correctly,
6911 * and ->cpu_power to 0.
6914 init_sched_build_groups(const cpumask_t
*span
, const cpumask_t
*cpu_map
,
6915 int (*group_fn
)(int cpu
, const cpumask_t
*cpu_map
,
6916 struct sched_group
**sg
,
6917 cpumask_t
*tmpmask
),
6918 cpumask_t
*covered
, cpumask_t
*tmpmask
)
6920 struct sched_group
*first
= NULL
, *last
= NULL
;
6923 cpus_clear(*covered
);
6925 for_each_cpu_mask(i
, *span
) {
6926 struct sched_group
*sg
;
6927 int group
= group_fn(i
, cpu_map
, &sg
, tmpmask
);
6930 if (cpu_isset(i
, *covered
))
6933 cpus_clear(sg
->cpumask
);
6934 sg
->__cpu_power
= 0;
6936 for_each_cpu_mask(j
, *span
) {
6937 if (group_fn(j
, cpu_map
, NULL
, tmpmask
) != group
)
6940 cpu_set(j
, *covered
);
6941 cpu_set(j
, sg
->cpumask
);
6952 #define SD_NODES_PER_DOMAIN 16
6957 * find_next_best_node - find the next node to include in a sched_domain
6958 * @node: node whose sched_domain we're building
6959 * @used_nodes: nodes already in the sched_domain
6961 * Find the next node to include in a given scheduling domain. Simply
6962 * finds the closest node not already in the @used_nodes map.
6964 * Should use nodemask_t.
6966 static int find_next_best_node(int node
, nodemask_t
*used_nodes
)
6968 int i
, n
, val
, min_val
, best_node
= 0;
6972 for (i
= 0; i
< MAX_NUMNODES
; i
++) {
6973 /* Start at @node */
6974 n
= (node
+ i
) % MAX_NUMNODES
;
6976 if (!nr_cpus_node(n
))
6979 /* Skip already used nodes */
6980 if (node_isset(n
, *used_nodes
))
6983 /* Simple min distance search */
6984 val
= node_distance(node
, n
);
6986 if (val
< min_val
) {
6992 node_set(best_node
, *used_nodes
);
6997 * sched_domain_node_span - get a cpumask for a node's sched_domain
6998 * @node: node whose cpumask we're constructing
6999 * @span: resulting cpumask
7001 * Given a node, construct a good cpumask for its sched_domain to span. It
7002 * should be one that prevents unnecessary balancing, but also spreads tasks
7005 static void sched_domain_node_span(int node
, cpumask_t
*span
)
7007 nodemask_t used_nodes
;
7008 node_to_cpumask_ptr(nodemask
, node
);
7012 nodes_clear(used_nodes
);
7014 cpus_or(*span
, *span
, *nodemask
);
7015 node_set(node
, used_nodes
);
7017 for (i
= 1; i
< SD_NODES_PER_DOMAIN
; i
++) {
7018 int next_node
= find_next_best_node(node
, &used_nodes
);
7020 node_to_cpumask_ptr_next(nodemask
, next_node
);
7021 cpus_or(*span
, *span
, *nodemask
);
7026 int sched_smt_power_savings
= 0, sched_mc_power_savings
= 0;
7029 * SMT sched-domains:
7031 #ifdef CONFIG_SCHED_SMT
7032 static DEFINE_PER_CPU(struct sched_domain
, cpu_domains
);
7033 static DEFINE_PER_CPU(struct sched_group
, sched_group_cpus
);
7036 cpu_to_cpu_group(int cpu
, const cpumask_t
*cpu_map
, struct sched_group
**sg
,
7040 *sg
= &per_cpu(sched_group_cpus
, cpu
);
7046 * multi-core sched-domains:
7048 #ifdef CONFIG_SCHED_MC
7049 static DEFINE_PER_CPU(struct sched_domain
, core_domains
);
7050 static DEFINE_PER_CPU(struct sched_group
, sched_group_core
);
7053 #if defined(CONFIG_SCHED_MC) && defined(CONFIG_SCHED_SMT)
7055 cpu_to_core_group(int cpu
, const cpumask_t
*cpu_map
, struct sched_group
**sg
,
7060 *mask
= per_cpu(cpu_sibling_map
, cpu
);
7061 cpus_and(*mask
, *mask
, *cpu_map
);
7062 group
= first_cpu(*mask
);
7064 *sg
= &per_cpu(sched_group_core
, group
);
7067 #elif defined(CONFIG_SCHED_MC)
7069 cpu_to_core_group(int cpu
, const cpumask_t
*cpu_map
, struct sched_group
**sg
,
7073 *sg
= &per_cpu(sched_group_core
, cpu
);
7078 static DEFINE_PER_CPU(struct sched_domain
, phys_domains
);
7079 static DEFINE_PER_CPU(struct sched_group
, sched_group_phys
);
7082 cpu_to_phys_group(int cpu
, const cpumask_t
*cpu_map
, struct sched_group
**sg
,
7086 #ifdef CONFIG_SCHED_MC
7087 *mask
= cpu_coregroup_map(cpu
);
7088 cpus_and(*mask
, *mask
, *cpu_map
);
7089 group
= first_cpu(*mask
);
7090 #elif defined(CONFIG_SCHED_SMT)
7091 *mask
= per_cpu(cpu_sibling_map
, cpu
);
7092 cpus_and(*mask
, *mask
, *cpu_map
);
7093 group
= first_cpu(*mask
);
7098 *sg
= &per_cpu(sched_group_phys
, group
);
7104 * The init_sched_build_groups can't handle what we want to do with node
7105 * groups, so roll our own. Now each node has its own list of groups which
7106 * gets dynamically allocated.
7108 static DEFINE_PER_CPU(struct sched_domain
, node_domains
);
7109 static struct sched_group
***sched_group_nodes_bycpu
;
7111 static DEFINE_PER_CPU(struct sched_domain
, allnodes_domains
);
7112 static DEFINE_PER_CPU(struct sched_group
, sched_group_allnodes
);
7114 static int cpu_to_allnodes_group(int cpu
, const cpumask_t
*cpu_map
,
7115 struct sched_group
**sg
, cpumask_t
*nodemask
)
7119 *nodemask
= node_to_cpumask(cpu_to_node(cpu
));
7120 cpus_and(*nodemask
, *nodemask
, *cpu_map
);
7121 group
= first_cpu(*nodemask
);
7124 *sg
= &per_cpu(sched_group_allnodes
, group
);
7128 static void init_numa_sched_groups_power(struct sched_group
*group_head
)
7130 struct sched_group
*sg
= group_head
;
7136 for_each_cpu_mask(j
, sg
->cpumask
) {
7137 struct sched_domain
*sd
;
7139 sd
= &per_cpu(phys_domains
, j
);
7140 if (j
!= first_cpu(sd
->groups
->cpumask
)) {
7142 * Only add "power" once for each
7148 sg_inc_cpu_power(sg
, sd
->groups
->__cpu_power
);
7151 } while (sg
!= group_head
);
7156 /* Free memory allocated for various sched_group structures */
7157 static void free_sched_groups(const cpumask_t
*cpu_map
, cpumask_t
*nodemask
)
7161 for_each_cpu_mask(cpu
, *cpu_map
) {
7162 struct sched_group
**sched_group_nodes
7163 = sched_group_nodes_bycpu
[cpu
];
7165 if (!sched_group_nodes
)
7168 for (i
= 0; i
< MAX_NUMNODES
; i
++) {
7169 struct sched_group
*oldsg
, *sg
= sched_group_nodes
[i
];
7171 *nodemask
= node_to_cpumask(i
);
7172 cpus_and(*nodemask
, *nodemask
, *cpu_map
);
7173 if (cpus_empty(*nodemask
))
7183 if (oldsg
!= sched_group_nodes
[i
])
7186 kfree(sched_group_nodes
);
7187 sched_group_nodes_bycpu
[cpu
] = NULL
;
7191 static void free_sched_groups(const cpumask_t
*cpu_map
, cpumask_t
*nodemask
)
7197 * Initialize sched groups cpu_power.
7199 * cpu_power indicates the capacity of sched group, which is used while
7200 * distributing the load between different sched groups in a sched domain.
7201 * Typically cpu_power for all the groups in a sched domain will be same unless
7202 * there are asymmetries in the topology. If there are asymmetries, group
7203 * having more cpu_power will pickup more load compared to the group having
7206 * cpu_power will be a multiple of SCHED_LOAD_SCALE. This multiple represents
7207 * the maximum number of tasks a group can handle in the presence of other idle
7208 * or lightly loaded groups in the same sched domain.
7210 static void init_sched_groups_power(int cpu
, struct sched_domain
*sd
)
7212 struct sched_domain
*child
;
7213 struct sched_group
*group
;
7215 WARN_ON(!sd
|| !sd
->groups
);
7217 if (cpu
!= first_cpu(sd
->groups
->cpumask
))
7222 sd
->groups
->__cpu_power
= 0;
7225 * For perf policy, if the groups in child domain share resources
7226 * (for example cores sharing some portions of the cache hierarchy
7227 * or SMT), then set this domain groups cpu_power such that each group
7228 * can handle only one task, when there are other idle groups in the
7229 * same sched domain.
7231 if (!child
|| (!(sd
->flags
& SD_POWERSAVINGS_BALANCE
) &&
7233 (SD_SHARE_CPUPOWER
| SD_SHARE_PKG_RESOURCES
)))) {
7234 sg_inc_cpu_power(sd
->groups
, SCHED_LOAD_SCALE
);
7239 * add cpu_power of each child group to this groups cpu_power
7241 group
= child
->groups
;
7243 sg_inc_cpu_power(sd
->groups
, group
->__cpu_power
);
7244 group
= group
->next
;
7245 } while (group
!= child
->groups
);
7249 * Initializers for schedule domains
7250 * Non-inlined to reduce accumulated stack pressure in build_sched_domains()
7253 #define SD_INIT(sd, type) sd_init_##type(sd)
7254 #define SD_INIT_FUNC(type) \
7255 static noinline void sd_init_##type(struct sched_domain *sd) \
7257 memset(sd, 0, sizeof(*sd)); \
7258 *sd = SD_##type##_INIT; \
7259 sd->level = SD_LV_##type; \
7264 SD_INIT_FUNC(ALLNODES
)
7267 #ifdef CONFIG_SCHED_SMT
7268 SD_INIT_FUNC(SIBLING
)
7270 #ifdef CONFIG_SCHED_MC
7275 * To minimize stack usage kmalloc room for cpumasks and share the
7276 * space as the usage in build_sched_domains() dictates. Used only
7277 * if the amount of space is significant.
7280 cpumask_t tmpmask
; /* make this one first */
7283 cpumask_t this_sibling_map
;
7284 cpumask_t this_core_map
;
7286 cpumask_t send_covered
;
7289 cpumask_t domainspan
;
7291 cpumask_t notcovered
;
7296 #define SCHED_CPUMASK_ALLOC 1
7297 #define SCHED_CPUMASK_FREE(v) kfree(v)
7298 #define SCHED_CPUMASK_DECLARE(v) struct allmasks *v
7300 #define SCHED_CPUMASK_ALLOC 0
7301 #define SCHED_CPUMASK_FREE(v)
7302 #define SCHED_CPUMASK_DECLARE(v) struct allmasks _v, *v = &_v
7305 #define SCHED_CPUMASK_VAR(v, a) cpumask_t *v = (cpumask_t *) \
7306 ((unsigned long)(a) + offsetof(struct allmasks, v))
7308 static int default_relax_domain_level
= -1;
7310 static int __init
setup_relax_domain_level(char *str
)
7312 default_relax_domain_level
= simple_strtoul(str
, NULL
, 0);
7315 __setup("relax_domain_level=", setup_relax_domain_level
);
7317 static void set_domain_attribute(struct sched_domain
*sd
,
7318 struct sched_domain_attr
*attr
)
7322 if (!attr
|| attr
->relax_domain_level
< 0) {
7323 if (default_relax_domain_level
< 0)
7326 request
= default_relax_domain_level
;
7328 request
= attr
->relax_domain_level
;
7329 if (request
< sd
->level
) {
7330 /* turn off idle balance on this domain */
7331 sd
->flags
&= ~(SD_WAKE_IDLE
|SD_BALANCE_NEWIDLE
);
7333 /* turn on idle balance on this domain */
7334 sd
->flags
|= (SD_WAKE_IDLE_FAR
|SD_BALANCE_NEWIDLE
);
7339 * Build sched domains for a given set of cpus and attach the sched domains
7340 * to the individual cpus
7342 static int __build_sched_domains(const cpumask_t
*cpu_map
,
7343 struct sched_domain_attr
*attr
)
7346 struct root_domain
*rd
;
7347 SCHED_CPUMASK_DECLARE(allmasks
);
7350 struct sched_group
**sched_group_nodes
= NULL
;
7351 int sd_allnodes
= 0;
7354 * Allocate the per-node list of sched groups
7356 sched_group_nodes
= kcalloc(MAX_NUMNODES
, sizeof(struct sched_group
*),
7358 if (!sched_group_nodes
) {
7359 printk(KERN_WARNING
"Can not alloc sched group node list\n");
7364 rd
= alloc_rootdomain();
7366 printk(KERN_WARNING
"Cannot alloc root domain\n");
7368 kfree(sched_group_nodes
);
7373 #if SCHED_CPUMASK_ALLOC
7374 /* get space for all scratch cpumask variables */
7375 allmasks
= kmalloc(sizeof(*allmasks
), GFP_KERNEL
);
7377 printk(KERN_WARNING
"Cannot alloc cpumask array\n");
7380 kfree(sched_group_nodes
);
7385 tmpmask
= (cpumask_t
*)allmasks
;
7389 sched_group_nodes_bycpu
[first_cpu(*cpu_map
)] = sched_group_nodes
;
7393 * Set up domains for cpus specified by the cpu_map.
7395 for_each_cpu_mask(i
, *cpu_map
) {
7396 struct sched_domain
*sd
= NULL
, *p
;
7397 SCHED_CPUMASK_VAR(nodemask
, allmasks
);
7399 *nodemask
= node_to_cpumask(cpu_to_node(i
));
7400 cpus_and(*nodemask
, *nodemask
, *cpu_map
);
7403 if (cpus_weight(*cpu_map
) >
7404 SD_NODES_PER_DOMAIN
*cpus_weight(*nodemask
)) {
7405 sd
= &per_cpu(allnodes_domains
, i
);
7406 SD_INIT(sd
, ALLNODES
);
7407 set_domain_attribute(sd
, attr
);
7408 sd
->span
= *cpu_map
;
7409 sd
->first_cpu
= first_cpu(sd
->span
);
7410 cpu_to_allnodes_group(i
, cpu_map
, &sd
->groups
, tmpmask
);
7416 sd
= &per_cpu(node_domains
, i
);
7418 set_domain_attribute(sd
, attr
);
7419 sched_domain_node_span(cpu_to_node(i
), &sd
->span
);
7420 sd
->first_cpu
= first_cpu(sd
->span
);
7424 cpus_and(sd
->span
, sd
->span
, *cpu_map
);
7428 sd
= &per_cpu(phys_domains
, i
);
7430 set_domain_attribute(sd
, attr
);
7431 sd
->span
= *nodemask
;
7432 sd
->first_cpu
= first_cpu(sd
->span
);
7436 cpu_to_phys_group(i
, cpu_map
, &sd
->groups
, tmpmask
);
7438 #ifdef CONFIG_SCHED_MC
7440 sd
= &per_cpu(core_domains
, i
);
7442 set_domain_attribute(sd
, attr
);
7443 sd
->span
= cpu_coregroup_map(i
);
7444 sd
->first_cpu
= first_cpu(sd
->span
);
7445 cpus_and(sd
->span
, sd
->span
, *cpu_map
);
7448 cpu_to_core_group(i
, cpu_map
, &sd
->groups
, tmpmask
);
7451 #ifdef CONFIG_SCHED_SMT
7453 sd
= &per_cpu(cpu_domains
, i
);
7454 SD_INIT(sd
, SIBLING
);
7455 set_domain_attribute(sd
, attr
);
7456 sd
->span
= per_cpu(cpu_sibling_map
, i
);
7457 sd
->first_cpu
= first_cpu(sd
->span
);
7458 cpus_and(sd
->span
, sd
->span
, *cpu_map
);
7461 cpu_to_cpu_group(i
, cpu_map
, &sd
->groups
, tmpmask
);
7465 #ifdef CONFIG_SCHED_SMT
7466 /* Set up CPU (sibling) groups */
7467 for_each_cpu_mask(i
, *cpu_map
) {
7468 SCHED_CPUMASK_VAR(this_sibling_map
, allmasks
);
7469 SCHED_CPUMASK_VAR(send_covered
, allmasks
);
7471 *this_sibling_map
= per_cpu(cpu_sibling_map
, i
);
7472 cpus_and(*this_sibling_map
, *this_sibling_map
, *cpu_map
);
7473 if (i
!= first_cpu(*this_sibling_map
))
7476 init_sched_build_groups(this_sibling_map
, cpu_map
,
7478 send_covered
, tmpmask
);
7482 #ifdef CONFIG_SCHED_MC
7483 /* Set up multi-core groups */
7484 for_each_cpu_mask(i
, *cpu_map
) {
7485 SCHED_CPUMASK_VAR(this_core_map
, allmasks
);
7486 SCHED_CPUMASK_VAR(send_covered
, allmasks
);
7488 *this_core_map
= cpu_coregroup_map(i
);
7489 cpus_and(*this_core_map
, *this_core_map
, *cpu_map
);
7490 if (i
!= first_cpu(*this_core_map
))
7493 init_sched_build_groups(this_core_map
, cpu_map
,
7495 send_covered
, tmpmask
);
7499 /* Set up physical groups */
7500 for (i
= 0; i
< MAX_NUMNODES
; i
++) {
7501 SCHED_CPUMASK_VAR(nodemask
, allmasks
);
7502 SCHED_CPUMASK_VAR(send_covered
, allmasks
);
7504 *nodemask
= node_to_cpumask(i
);
7505 cpus_and(*nodemask
, *nodemask
, *cpu_map
);
7506 if (cpus_empty(*nodemask
))
7509 init_sched_build_groups(nodemask
, cpu_map
,
7511 send_covered
, tmpmask
);
7515 /* Set up node groups */
7517 SCHED_CPUMASK_VAR(send_covered
, allmasks
);
7519 init_sched_build_groups(cpu_map
, cpu_map
,
7520 &cpu_to_allnodes_group
,
7521 send_covered
, tmpmask
);
7524 for (i
= 0; i
< MAX_NUMNODES
; i
++) {
7525 /* Set up node groups */
7526 struct sched_group
*sg
, *prev
;
7527 SCHED_CPUMASK_VAR(nodemask
, allmasks
);
7528 SCHED_CPUMASK_VAR(domainspan
, allmasks
);
7529 SCHED_CPUMASK_VAR(covered
, allmasks
);
7532 *nodemask
= node_to_cpumask(i
);
7533 cpus_clear(*covered
);
7535 cpus_and(*nodemask
, *nodemask
, *cpu_map
);
7536 if (cpus_empty(*nodemask
)) {
7537 sched_group_nodes
[i
] = NULL
;
7541 sched_domain_node_span(i
, domainspan
);
7542 cpus_and(*domainspan
, *domainspan
, *cpu_map
);
7544 sg
= kmalloc_node(sizeof(struct sched_group
), GFP_KERNEL
, i
);
7546 printk(KERN_WARNING
"Can not alloc domain group for "
7550 sched_group_nodes
[i
] = sg
;
7551 for_each_cpu_mask(j
, *nodemask
) {
7552 struct sched_domain
*sd
;
7554 sd
= &per_cpu(node_domains
, j
);
7557 sg
->__cpu_power
= 0;
7558 sg
->cpumask
= *nodemask
;
7560 cpus_or(*covered
, *covered
, *nodemask
);
7563 for (j
= 0; j
< MAX_NUMNODES
; j
++) {
7564 SCHED_CPUMASK_VAR(notcovered
, allmasks
);
7565 int n
= (i
+ j
) % MAX_NUMNODES
;
7566 node_to_cpumask_ptr(pnodemask
, n
);
7568 cpus_complement(*notcovered
, *covered
);
7569 cpus_and(*tmpmask
, *notcovered
, *cpu_map
);
7570 cpus_and(*tmpmask
, *tmpmask
, *domainspan
);
7571 if (cpus_empty(*tmpmask
))
7574 cpus_and(*tmpmask
, *tmpmask
, *pnodemask
);
7575 if (cpus_empty(*tmpmask
))
7578 sg
= kmalloc_node(sizeof(struct sched_group
),
7582 "Can not alloc domain group for node %d\n", j
);
7585 sg
->__cpu_power
= 0;
7586 sg
->cpumask
= *tmpmask
;
7587 sg
->next
= prev
->next
;
7588 cpus_or(*covered
, *covered
, *tmpmask
);
7595 /* Calculate CPU power for physical packages and nodes */
7596 #ifdef CONFIG_SCHED_SMT
7597 for_each_cpu_mask(i
, *cpu_map
) {
7598 struct sched_domain
*sd
= &per_cpu(cpu_domains
, i
);
7600 init_sched_groups_power(i
, sd
);
7603 #ifdef CONFIG_SCHED_MC
7604 for_each_cpu_mask(i
, *cpu_map
) {
7605 struct sched_domain
*sd
= &per_cpu(core_domains
, i
);
7607 init_sched_groups_power(i
, sd
);
7611 for_each_cpu_mask(i
, *cpu_map
) {
7612 struct sched_domain
*sd
= &per_cpu(phys_domains
, i
);
7614 init_sched_groups_power(i
, sd
);
7618 for (i
= 0; i
< MAX_NUMNODES
; i
++)
7619 init_numa_sched_groups_power(sched_group_nodes
[i
]);
7622 struct sched_group
*sg
;
7624 cpu_to_allnodes_group(first_cpu(*cpu_map
), cpu_map
, &sg
,
7626 init_numa_sched_groups_power(sg
);
7630 /* Attach the domains */
7631 for_each_cpu_mask(i
, *cpu_map
) {
7632 struct sched_domain
*sd
;
7633 #ifdef CONFIG_SCHED_SMT
7634 sd
= &per_cpu(cpu_domains
, i
);
7635 #elif defined(CONFIG_SCHED_MC)
7636 sd
= &per_cpu(core_domains
, i
);
7638 sd
= &per_cpu(phys_domains
, i
);
7640 cpu_attach_domain(sd
, rd
, i
);
7643 SCHED_CPUMASK_FREE((void *)allmasks
);
7648 free_sched_groups(cpu_map
, tmpmask
);
7649 SCHED_CPUMASK_FREE((void *)allmasks
);
7654 static int build_sched_domains(const cpumask_t
*cpu_map
)
7656 return __build_sched_domains(cpu_map
, NULL
);
7659 static cpumask_t
*doms_cur
; /* current sched domains */
7660 static int ndoms_cur
; /* number of sched domains in 'doms_cur' */
7661 static struct sched_domain_attr
*dattr_cur
; /* attribues of custom domains
7665 * Special case: If a kmalloc of a doms_cur partition (array of
7666 * cpumask_t) fails, then fallback to a single sched domain,
7667 * as determined by the single cpumask_t fallback_doms.
7669 static cpumask_t fallback_doms
;
7671 void __attribute__((weak
)) arch_update_cpu_topology(void)
7676 * Set up scheduler domains and groups. Callers must hold the hotplug lock.
7677 * For now this just excludes isolated cpus, but could be used to
7678 * exclude other special cases in the future.
7680 static int arch_init_sched_domains(const cpumask_t
*cpu_map
)
7684 arch_update_cpu_topology();
7686 doms_cur
= kmalloc(sizeof(cpumask_t
), GFP_KERNEL
);
7688 doms_cur
= &fallback_doms
;
7689 cpus_andnot(*doms_cur
, *cpu_map
, cpu_isolated_map
);
7691 err
= build_sched_domains(doms_cur
);
7692 register_sched_domain_sysctl();
7697 static void arch_destroy_sched_domains(const cpumask_t
*cpu_map
,
7700 free_sched_groups(cpu_map
, tmpmask
);
7704 * Detach sched domains from a group of cpus specified in cpu_map
7705 * These cpus will now be attached to the NULL domain
7707 static void detach_destroy_domains(const cpumask_t
*cpu_map
)
7712 unregister_sched_domain_sysctl();
7714 for_each_cpu_mask(i
, *cpu_map
)
7715 cpu_attach_domain(NULL
, &def_root_domain
, i
);
7716 synchronize_sched();
7717 arch_destroy_sched_domains(cpu_map
, &tmpmask
);
7720 /* handle null as "default" */
7721 static int dattrs_equal(struct sched_domain_attr
*cur
, int idx_cur
,
7722 struct sched_domain_attr
*new, int idx_new
)
7724 struct sched_domain_attr tmp
;
7731 return !memcmp(cur
? (cur
+ idx_cur
) : &tmp
,
7732 new ? (new + idx_new
) : &tmp
,
7733 sizeof(struct sched_domain_attr
));
7737 * Partition sched domains as specified by the 'ndoms_new'
7738 * cpumasks in the array doms_new[] of cpumasks. This compares
7739 * doms_new[] to the current sched domain partitioning, doms_cur[].
7740 * It destroys each deleted domain and builds each new domain.
7742 * 'doms_new' is an array of cpumask_t's of length 'ndoms_new'.
7743 * The masks don't intersect (don't overlap.) We should setup one
7744 * sched domain for each mask. CPUs not in any of the cpumasks will
7745 * not be load balanced. If the same cpumask appears both in the
7746 * current 'doms_cur' domains and in the new 'doms_new', we can leave
7749 * The passed in 'doms_new' should be kmalloc'd. This routine takes
7750 * ownership of it and will kfree it when done with it. If the caller
7751 * failed the kmalloc call, then it can pass in doms_new == NULL,
7752 * and partition_sched_domains() will fallback to the single partition
7755 * Call with hotplug lock held
7757 void partition_sched_domains(int ndoms_new
, cpumask_t
*doms_new
,
7758 struct sched_domain_attr
*dattr_new
)
7764 /* always unregister in case we don't destroy any domains */
7765 unregister_sched_domain_sysctl();
7767 if (doms_new
== NULL
) {
7769 doms_new
= &fallback_doms
;
7770 cpus_andnot(doms_new
[0], cpu_online_map
, cpu_isolated_map
);
7774 /* Destroy deleted domains */
7775 for (i
= 0; i
< ndoms_cur
; i
++) {
7776 for (j
= 0; j
< ndoms_new
; j
++) {
7777 if (cpus_equal(doms_cur
[i
], doms_new
[j
])
7778 && dattrs_equal(dattr_cur
, i
, dattr_new
, j
))
7781 /* no match - a current sched domain not in new doms_new[] */
7782 detach_destroy_domains(doms_cur
+ i
);
7787 /* Build new domains */
7788 for (i
= 0; i
< ndoms_new
; i
++) {
7789 for (j
= 0; j
< ndoms_cur
; j
++) {
7790 if (cpus_equal(doms_new
[i
], doms_cur
[j
])
7791 && dattrs_equal(dattr_new
, i
, dattr_cur
, j
))
7794 /* no match - add a new doms_new */
7795 __build_sched_domains(doms_new
+ i
,
7796 dattr_new
? dattr_new
+ i
: NULL
);
7801 /* Remember the new sched domains */
7802 if (doms_cur
!= &fallback_doms
)
7804 kfree(dattr_cur
); /* kfree(NULL) is safe */
7805 doms_cur
= doms_new
;
7806 dattr_cur
= dattr_new
;
7807 ndoms_cur
= ndoms_new
;
7809 register_sched_domain_sysctl();
7814 #if defined(CONFIG_SCHED_MC) || defined(CONFIG_SCHED_SMT)
7815 int arch_reinit_sched_domains(void)
7820 detach_destroy_domains(&cpu_online_map
);
7821 err
= arch_init_sched_domains(&cpu_online_map
);
7827 static ssize_t
sched_power_savings_store(const char *buf
, size_t count
, int smt
)
7831 if (buf
[0] != '0' && buf
[0] != '1')
7835 sched_smt_power_savings
= (buf
[0] == '1');
7837 sched_mc_power_savings
= (buf
[0] == '1');
7839 ret
= arch_reinit_sched_domains();
7841 return ret
? ret
: count
;
7844 #ifdef CONFIG_SCHED_MC
7845 static ssize_t
sched_mc_power_savings_show(struct sys_device
*dev
, char *page
)
7847 return sprintf(page
, "%u\n", sched_mc_power_savings
);
7849 static ssize_t
sched_mc_power_savings_store(struct sys_device
*dev
,
7850 const char *buf
, size_t count
)
7852 return sched_power_savings_store(buf
, count
, 0);
7854 static SYSDEV_ATTR(sched_mc_power_savings
, 0644, sched_mc_power_savings_show
,
7855 sched_mc_power_savings_store
);
7858 #ifdef CONFIG_SCHED_SMT
7859 static ssize_t
sched_smt_power_savings_show(struct sys_device
*dev
, char *page
)
7861 return sprintf(page
, "%u\n", sched_smt_power_savings
);
7863 static ssize_t
sched_smt_power_savings_store(struct sys_device
*dev
,
7864 const char *buf
, size_t count
)
7866 return sched_power_savings_store(buf
, count
, 1);
7868 static SYSDEV_ATTR(sched_smt_power_savings
, 0644, sched_smt_power_savings_show
,
7869 sched_smt_power_savings_store
);
7872 int sched_create_sysfs_power_savings_entries(struct sysdev_class
*cls
)
7876 #ifdef CONFIG_SCHED_SMT
7878 err
= sysfs_create_file(&cls
->kset
.kobj
,
7879 &attr_sched_smt_power_savings
.attr
);
7881 #ifdef CONFIG_SCHED_MC
7882 if (!err
&& mc_capable())
7883 err
= sysfs_create_file(&cls
->kset
.kobj
,
7884 &attr_sched_mc_power_savings
.attr
);
7891 * Force a reinitialization of the sched domains hierarchy. The domains
7892 * and groups cannot be updated in place without racing with the balancing
7893 * code, so we temporarily attach all running cpus to the NULL domain
7894 * which will prevent rebalancing while the sched domains are recalculated.
7896 static int update_sched_domains(struct notifier_block
*nfb
,
7897 unsigned long action
, void *hcpu
)
7900 case CPU_UP_PREPARE
:
7901 case CPU_UP_PREPARE_FROZEN
:
7902 case CPU_DOWN_PREPARE
:
7903 case CPU_DOWN_PREPARE_FROZEN
:
7904 detach_destroy_domains(&cpu_online_map
);
7907 case CPU_UP_CANCELED
:
7908 case CPU_UP_CANCELED_FROZEN
:
7909 case CPU_DOWN_FAILED
:
7910 case CPU_DOWN_FAILED_FROZEN
:
7912 case CPU_ONLINE_FROZEN
:
7914 case CPU_DEAD_FROZEN
:
7916 * Fall through and re-initialise the domains.
7923 /* The hotplug lock is already held by cpu_up/cpu_down */
7924 arch_init_sched_domains(&cpu_online_map
);
7929 void __init
sched_init_smp(void)
7931 cpumask_t non_isolated_cpus
;
7933 #if defined(CONFIG_NUMA)
7934 sched_group_nodes_bycpu
= kzalloc(nr_cpu_ids
* sizeof(void **),
7936 BUG_ON(sched_group_nodes_bycpu
== NULL
);
7939 arch_init_sched_domains(&cpu_online_map
);
7940 cpus_andnot(non_isolated_cpus
, cpu_possible_map
, cpu_isolated_map
);
7941 if (cpus_empty(non_isolated_cpus
))
7942 cpu_set(smp_processor_id(), non_isolated_cpus
);
7944 /* XXX: Theoretical race here - CPU may be hotplugged now */
7945 hotcpu_notifier(update_sched_domains
, 0);
7947 /* Move init over to a non-isolated CPU */
7948 if (set_cpus_allowed_ptr(current
, &non_isolated_cpus
) < 0)
7950 sched_init_granularity();
7953 void __init
sched_init_smp(void)
7955 sched_init_granularity();
7957 #endif /* CONFIG_SMP */
7959 int in_sched_functions(unsigned long addr
)
7961 return in_lock_functions(addr
) ||
7962 (addr
>= (unsigned long)__sched_text_start
7963 && addr
< (unsigned long)__sched_text_end
);
7966 static void init_cfs_rq(struct cfs_rq
*cfs_rq
, struct rq
*rq
)
7968 cfs_rq
->tasks_timeline
= RB_ROOT
;
7969 INIT_LIST_HEAD(&cfs_rq
->tasks
);
7970 #ifdef CONFIG_FAIR_GROUP_SCHED
7973 cfs_rq
->min_vruntime
= (u64
)(-(1LL << 20));
7976 static void init_rt_rq(struct rt_rq
*rt_rq
, struct rq
*rq
)
7978 struct rt_prio_array
*array
;
7981 array
= &rt_rq
->active
;
7982 for (i
= 0; i
< MAX_RT_PRIO
; i
++) {
7983 INIT_LIST_HEAD(array
->queue
+ i
);
7984 __clear_bit(i
, array
->bitmap
);
7986 /* delimiter for bitsearch: */
7987 __set_bit(MAX_RT_PRIO
, array
->bitmap
);
7989 #if defined CONFIG_SMP || defined CONFIG_RT_GROUP_SCHED
7990 rt_rq
->highest_prio
= MAX_RT_PRIO
;
7993 rt_rq
->rt_nr_migratory
= 0;
7994 rt_rq
->overloaded
= 0;
7998 rt_rq
->rt_throttled
= 0;
7999 rt_rq
->rt_runtime
= 0;
8000 spin_lock_init(&rt_rq
->rt_runtime_lock
);
8002 #ifdef CONFIG_RT_GROUP_SCHED
8003 rt_rq
->rt_nr_boosted
= 0;
8008 #ifdef CONFIG_FAIR_GROUP_SCHED
8009 static void init_tg_cfs_entry(struct task_group
*tg
, struct cfs_rq
*cfs_rq
,
8010 struct sched_entity
*se
, int cpu
, int add
,
8011 struct sched_entity
*parent
)
8013 struct rq
*rq
= cpu_rq(cpu
);
8014 tg
->cfs_rq
[cpu
] = cfs_rq
;
8015 init_cfs_rq(cfs_rq
, rq
);
8018 list_add(&cfs_rq
->leaf_cfs_rq_list
, &rq
->leaf_cfs_rq_list
);
8021 /* se could be NULL for init_task_group */
8026 se
->cfs_rq
= &rq
->cfs
;
8028 se
->cfs_rq
= parent
->my_q
;
8031 se
->load
.weight
= tg
->shares
;
8032 se
->load
.inv_weight
= 0;
8033 se
->parent
= parent
;
8037 #ifdef CONFIG_RT_GROUP_SCHED
8038 static void init_tg_rt_entry(struct task_group
*tg
, struct rt_rq
*rt_rq
,
8039 struct sched_rt_entity
*rt_se
, int cpu
, int add
,
8040 struct sched_rt_entity
*parent
)
8042 struct rq
*rq
= cpu_rq(cpu
);
8044 tg
->rt_rq
[cpu
] = rt_rq
;
8045 init_rt_rq(rt_rq
, rq
);
8047 rt_rq
->rt_se
= rt_se
;
8048 rt_rq
->rt_runtime
= tg
->rt_bandwidth
.rt_runtime
;
8050 list_add(&rt_rq
->leaf_rt_rq_list
, &rq
->leaf_rt_rq_list
);
8052 tg
->rt_se
[cpu
] = rt_se
;
8057 rt_se
->rt_rq
= &rq
->rt
;
8059 rt_se
->rt_rq
= parent
->my_q
;
8061 rt_se
->rt_rq
= &rq
->rt
;
8062 rt_se
->my_q
= rt_rq
;
8063 rt_se
->parent
= parent
;
8064 INIT_LIST_HEAD(&rt_se
->run_list
);
8068 void __init
sched_init(void)
8071 unsigned long alloc_size
= 0, ptr
;
8073 #ifdef CONFIG_FAIR_GROUP_SCHED
8074 alloc_size
+= 2 * nr_cpu_ids
* sizeof(void **);
8076 #ifdef CONFIG_RT_GROUP_SCHED
8077 alloc_size
+= 2 * nr_cpu_ids
* sizeof(void **);
8079 #ifdef CONFIG_USER_SCHED
8083 * As sched_init() is called before page_alloc is setup,
8084 * we use alloc_bootmem().
8087 ptr
= (unsigned long)alloc_bootmem(alloc_size
);
8089 #ifdef CONFIG_FAIR_GROUP_SCHED
8090 init_task_group
.se
= (struct sched_entity
**)ptr
;
8091 ptr
+= nr_cpu_ids
* sizeof(void **);
8093 init_task_group
.cfs_rq
= (struct cfs_rq
**)ptr
;
8094 ptr
+= nr_cpu_ids
* sizeof(void **);
8096 #ifdef CONFIG_USER_SCHED
8097 root_task_group
.se
= (struct sched_entity
**)ptr
;
8098 ptr
+= nr_cpu_ids
* sizeof(void **);
8100 root_task_group
.cfs_rq
= (struct cfs_rq
**)ptr
;
8101 ptr
+= nr_cpu_ids
* sizeof(void **);
8104 #ifdef CONFIG_RT_GROUP_SCHED
8105 init_task_group
.rt_se
= (struct sched_rt_entity
**)ptr
;
8106 ptr
+= nr_cpu_ids
* sizeof(void **);
8108 init_task_group
.rt_rq
= (struct rt_rq
**)ptr
;
8109 ptr
+= nr_cpu_ids
* sizeof(void **);
8111 #ifdef CONFIG_USER_SCHED
8112 root_task_group
.rt_se
= (struct sched_rt_entity
**)ptr
;
8113 ptr
+= nr_cpu_ids
* sizeof(void **);
8115 root_task_group
.rt_rq
= (struct rt_rq
**)ptr
;
8116 ptr
+= nr_cpu_ids
* sizeof(void **);
8123 init_defrootdomain();
8126 init_rt_bandwidth(&def_rt_bandwidth
,
8127 global_rt_period(), global_rt_runtime());
8129 #ifdef CONFIG_RT_GROUP_SCHED
8130 init_rt_bandwidth(&init_task_group
.rt_bandwidth
,
8131 global_rt_period(), global_rt_runtime());
8132 #ifdef CONFIG_USER_SCHED
8133 init_rt_bandwidth(&root_task_group
.rt_bandwidth
,
8134 global_rt_period(), RUNTIME_INF
);
8138 #ifdef CONFIG_GROUP_SCHED
8139 list_add(&init_task_group
.list
, &task_groups
);
8140 INIT_LIST_HEAD(&init_task_group
.children
);
8142 #ifdef CONFIG_USER_SCHED
8143 INIT_LIST_HEAD(&root_task_group
.children
);
8144 init_task_group
.parent
= &root_task_group
;
8145 list_add(&init_task_group
.siblings
, &root_task_group
.children
);
8149 for_each_possible_cpu(i
) {
8153 spin_lock_init(&rq
->lock
);
8154 lockdep_set_class(&rq
->lock
, &rq
->rq_lock_key
);
8157 update_last_tick_seen(rq
);
8158 init_cfs_rq(&rq
->cfs
, rq
);
8159 init_rt_rq(&rq
->rt
, rq
);
8160 #ifdef CONFIG_FAIR_GROUP_SCHED
8161 init_task_group
.shares
= init_task_group_load
;
8162 INIT_LIST_HEAD(&rq
->leaf_cfs_rq_list
);
8163 #ifdef CONFIG_CGROUP_SCHED
8165 * How much cpu bandwidth does init_task_group get?
8167 * In case of task-groups formed thr' the cgroup filesystem, it
8168 * gets 100% of the cpu resources in the system. This overall
8169 * system cpu resource is divided among the tasks of
8170 * init_task_group and its child task-groups in a fair manner,
8171 * based on each entity's (task or task-group's) weight
8172 * (se->load.weight).
8174 * In other words, if init_task_group has 10 tasks of weight
8175 * 1024) and two child groups A0 and A1 (of weight 1024 each),
8176 * then A0's share of the cpu resource is:
8178 * A0's bandwidth = 1024 / (10*1024 + 1024 + 1024) = 8.33%
8180 * We achieve this by letting init_task_group's tasks sit
8181 * directly in rq->cfs (i.e init_task_group->se[] = NULL).
8183 init_tg_cfs_entry(&init_task_group
, &rq
->cfs
, NULL
, i
, 1, NULL
);
8184 #elif defined CONFIG_USER_SCHED
8185 root_task_group
.shares
= NICE_0_LOAD
;
8186 init_tg_cfs_entry(&root_task_group
, &rq
->cfs
, NULL
, i
, 0, NULL
);
8188 * In case of task-groups formed thr' the user id of tasks,
8189 * init_task_group represents tasks belonging to root user.
8190 * Hence it forms a sibling of all subsequent groups formed.
8191 * In this case, init_task_group gets only a fraction of overall
8192 * system cpu resource, based on the weight assigned to root
8193 * user's cpu share (INIT_TASK_GROUP_LOAD). This is accomplished
8194 * by letting tasks of init_task_group sit in a separate cfs_rq
8195 * (init_cfs_rq) and having one entity represent this group of
8196 * tasks in rq->cfs (i.e init_task_group->se[] != NULL).
8198 init_tg_cfs_entry(&init_task_group
,
8199 &per_cpu(init_cfs_rq
, i
),
8200 &per_cpu(init_sched_entity
, i
), i
, 1,
8201 root_task_group
.se
[i
]);
8204 #endif /* CONFIG_FAIR_GROUP_SCHED */
8206 rq
->rt
.rt_runtime
= def_rt_bandwidth
.rt_runtime
;
8207 #ifdef CONFIG_RT_GROUP_SCHED
8208 INIT_LIST_HEAD(&rq
->leaf_rt_rq_list
);
8209 #ifdef CONFIG_CGROUP_SCHED
8210 init_tg_rt_entry(&init_task_group
, &rq
->rt
, NULL
, i
, 1, NULL
);
8211 #elif defined CONFIG_USER_SCHED
8212 init_tg_rt_entry(&root_task_group
, &rq
->rt
, NULL
, i
, 0, NULL
);
8213 init_tg_rt_entry(&init_task_group
,
8214 &per_cpu(init_rt_rq
, i
),
8215 &per_cpu(init_sched_rt_entity
, i
), i
, 1,
8216 root_task_group
.rt_se
[i
]);
8220 for (j
= 0; j
< CPU_LOAD_IDX_MAX
; j
++)
8221 rq
->cpu_load
[j
] = 0;
8225 rq
->active_balance
= 0;
8226 rq
->next_balance
= jiffies
;
8229 rq
->migration_thread
= NULL
;
8230 INIT_LIST_HEAD(&rq
->migration_queue
);
8231 rq_attach_root(rq
, &def_root_domain
);
8234 atomic_set(&rq
->nr_iowait
, 0);
8237 set_load_weight(&init_task
);
8239 #ifdef CONFIG_PREEMPT_NOTIFIERS
8240 INIT_HLIST_HEAD(&init_task
.preempt_notifiers
);
8244 open_softirq(SCHED_SOFTIRQ
, run_rebalance_domains
, NULL
);
8247 #ifdef CONFIG_RT_MUTEXES
8248 plist_head_init(&init_task
.pi_waiters
, &init_task
.pi_lock
);
8252 * The boot idle thread does lazy MMU switching as well:
8254 atomic_inc(&init_mm
.mm_count
);
8255 enter_lazy_tlb(&init_mm
, current
);
8258 * Make us the idle thread. Technically, schedule() should not be
8259 * called from this thread, however somewhere below it might be,
8260 * but because we are the idle thread, we just pick up running again
8261 * when this runqueue becomes "idle".
8263 init_idle(current
, smp_processor_id());
8265 * During early bootup we pretend to be a normal task:
8267 current
->sched_class
= &fair_sched_class
;
8269 scheduler_running
= 1;
8272 #ifdef CONFIG_DEBUG_SPINLOCK_SLEEP
8273 void __might_sleep(char *file
, int line
)
8276 static unsigned long prev_jiffy
; /* ratelimiting */
8278 if ((in_atomic() || irqs_disabled()) &&
8279 system_state
== SYSTEM_RUNNING
&& !oops_in_progress
) {
8280 if (time_before(jiffies
, prev_jiffy
+ HZ
) && prev_jiffy
)
8282 prev_jiffy
= jiffies
;
8283 printk(KERN_ERR
"BUG: sleeping function called from invalid"
8284 " context at %s:%d\n", file
, line
);
8285 printk("in_atomic():%d, irqs_disabled():%d\n",
8286 in_atomic(), irqs_disabled());
8287 debug_show_held_locks(current
);
8288 if (irqs_disabled())
8289 print_irqtrace_events(current
);
8294 EXPORT_SYMBOL(__might_sleep
);
8297 #ifdef CONFIG_MAGIC_SYSRQ
8298 static void normalize_task(struct rq
*rq
, struct task_struct
*p
)
8301 update_rq_clock(rq
);
8302 on_rq
= p
->se
.on_rq
;
8304 deactivate_task(rq
, p
, 0);
8305 __setscheduler(rq
, p
, SCHED_NORMAL
, 0);
8307 activate_task(rq
, p
, 0);
8308 resched_task(rq
->curr
);
8312 void normalize_rt_tasks(void)
8314 struct task_struct
*g
, *p
;
8315 unsigned long flags
;
8318 read_lock_irqsave(&tasklist_lock
, flags
);
8319 do_each_thread(g
, p
) {
8321 * Only normalize user tasks:
8326 p
->se
.exec_start
= 0;
8327 #ifdef CONFIG_SCHEDSTATS
8328 p
->se
.wait_start
= 0;
8329 p
->se
.sleep_start
= 0;
8330 p
->se
.block_start
= 0;
8332 task_rq(p
)->clock
= 0;
8336 * Renice negative nice level userspace
8339 if (TASK_NICE(p
) < 0 && p
->mm
)
8340 set_user_nice(p
, 0);
8344 spin_lock(&p
->pi_lock
);
8345 rq
= __task_rq_lock(p
);
8347 normalize_task(rq
, p
);
8349 __task_rq_unlock(rq
);
8350 spin_unlock(&p
->pi_lock
);
8351 } while_each_thread(g
, p
);
8353 read_unlock_irqrestore(&tasklist_lock
, flags
);
8356 #endif /* CONFIG_MAGIC_SYSRQ */
8360 * These functions are only useful for the IA64 MCA handling.
8362 * They can only be called when the whole system has been
8363 * stopped - every CPU needs to be quiescent, and no scheduling
8364 * activity can take place. Using them for anything else would
8365 * be a serious bug, and as a result, they aren't even visible
8366 * under any other configuration.
8370 * curr_task - return the current task for a given cpu.
8371 * @cpu: the processor in question.
8373 * ONLY VALID WHEN THE WHOLE SYSTEM IS STOPPED!
8375 struct task_struct
*curr_task(int cpu
)
8377 return cpu_curr(cpu
);
8381 * set_curr_task - set the current task for a given cpu.
8382 * @cpu: the processor in question.
8383 * @p: the task pointer to set.
8385 * Description: This function must only be used when non-maskable interrupts
8386 * are serviced on a separate stack. It allows the architecture to switch the
8387 * notion of the current task on a cpu in a non-blocking manner. This function
8388 * must be called with all CPU's synchronized, and interrupts disabled, the
8389 * and caller must save the original value of the current task (see
8390 * curr_task() above) and restore that value before reenabling interrupts and
8391 * re-starting the system.
8393 * ONLY VALID WHEN THE WHOLE SYSTEM IS STOPPED!
8395 void set_curr_task(int cpu
, struct task_struct
*p
)
8402 #ifdef CONFIG_FAIR_GROUP_SCHED
8403 static void free_fair_sched_group(struct task_group
*tg
)
8407 for_each_possible_cpu(i
) {
8409 kfree(tg
->cfs_rq
[i
]);
8419 int alloc_fair_sched_group(struct task_group
*tg
, struct task_group
*parent
)
8421 struct cfs_rq
*cfs_rq
;
8422 struct sched_entity
*se
, *parent_se
;
8426 tg
->cfs_rq
= kzalloc(sizeof(cfs_rq
) * nr_cpu_ids
, GFP_KERNEL
);
8429 tg
->se
= kzalloc(sizeof(se
) * nr_cpu_ids
, GFP_KERNEL
);
8433 tg
->shares
= NICE_0_LOAD
;
8435 for_each_possible_cpu(i
) {
8438 cfs_rq
= kmalloc_node(sizeof(struct cfs_rq
),
8439 GFP_KERNEL
|__GFP_ZERO
, cpu_to_node(i
));
8443 se
= kmalloc_node(sizeof(struct sched_entity
),
8444 GFP_KERNEL
|__GFP_ZERO
, cpu_to_node(i
));
8448 parent_se
= parent
? parent
->se
[i
] : NULL
;
8449 init_tg_cfs_entry(tg
, cfs_rq
, se
, i
, 0, parent_se
);
8458 static inline void register_fair_sched_group(struct task_group
*tg
, int cpu
)
8460 list_add_rcu(&tg
->cfs_rq
[cpu
]->leaf_cfs_rq_list
,
8461 &cpu_rq(cpu
)->leaf_cfs_rq_list
);
8464 static inline void unregister_fair_sched_group(struct task_group
*tg
, int cpu
)
8466 list_del_rcu(&tg
->cfs_rq
[cpu
]->leaf_cfs_rq_list
);
8469 static inline void free_fair_sched_group(struct task_group
*tg
)
8474 int alloc_fair_sched_group(struct task_group
*tg
, struct task_group
*parent
)
8479 static inline void register_fair_sched_group(struct task_group
*tg
, int cpu
)
8483 static inline void unregister_fair_sched_group(struct task_group
*tg
, int cpu
)
8488 #ifdef CONFIG_RT_GROUP_SCHED
8489 static void free_rt_sched_group(struct task_group
*tg
)
8493 destroy_rt_bandwidth(&tg
->rt_bandwidth
);
8495 for_each_possible_cpu(i
) {
8497 kfree(tg
->rt_rq
[i
]);
8499 kfree(tg
->rt_se
[i
]);
8507 int alloc_rt_sched_group(struct task_group
*tg
, struct task_group
*parent
)
8509 struct rt_rq
*rt_rq
;
8510 struct sched_rt_entity
*rt_se
, *parent_se
;
8514 tg
->rt_rq
= kzalloc(sizeof(rt_rq
) * nr_cpu_ids
, GFP_KERNEL
);
8517 tg
->rt_se
= kzalloc(sizeof(rt_se
) * nr_cpu_ids
, GFP_KERNEL
);
8521 init_rt_bandwidth(&tg
->rt_bandwidth
,
8522 ktime_to_ns(def_rt_bandwidth
.rt_period
), 0);
8524 for_each_possible_cpu(i
) {
8527 rt_rq
= kmalloc_node(sizeof(struct rt_rq
),
8528 GFP_KERNEL
|__GFP_ZERO
, cpu_to_node(i
));
8532 rt_se
= kmalloc_node(sizeof(struct sched_rt_entity
),
8533 GFP_KERNEL
|__GFP_ZERO
, cpu_to_node(i
));
8537 parent_se
= parent
? parent
->rt_se
[i
] : NULL
;
8538 init_tg_rt_entry(tg
, rt_rq
, rt_se
, i
, 0, parent_se
);
8547 static inline void register_rt_sched_group(struct task_group
*tg
, int cpu
)
8549 list_add_rcu(&tg
->rt_rq
[cpu
]->leaf_rt_rq_list
,
8550 &cpu_rq(cpu
)->leaf_rt_rq_list
);
8553 static inline void unregister_rt_sched_group(struct task_group
*tg
, int cpu
)
8555 list_del_rcu(&tg
->rt_rq
[cpu
]->leaf_rt_rq_list
);
8558 static inline void free_rt_sched_group(struct task_group
*tg
)
8563 int alloc_rt_sched_group(struct task_group
*tg
, struct task_group
*parent
)
8568 static inline void register_rt_sched_group(struct task_group
*tg
, int cpu
)
8572 static inline void unregister_rt_sched_group(struct task_group
*tg
, int cpu
)
8577 #ifdef CONFIG_GROUP_SCHED
8578 static void free_sched_group(struct task_group
*tg
)
8580 free_fair_sched_group(tg
);
8581 free_rt_sched_group(tg
);
8585 /* allocate runqueue etc for a new task group */
8586 struct task_group
*sched_create_group(struct task_group
*parent
)
8588 struct task_group
*tg
;
8589 unsigned long flags
;
8592 tg
= kzalloc(sizeof(*tg
), GFP_KERNEL
);
8594 return ERR_PTR(-ENOMEM
);
8596 if (!alloc_fair_sched_group(tg
, parent
))
8599 if (!alloc_rt_sched_group(tg
, parent
))
8602 spin_lock_irqsave(&task_group_lock
, flags
);
8603 for_each_possible_cpu(i
) {
8604 register_fair_sched_group(tg
, i
);
8605 register_rt_sched_group(tg
, i
);
8607 list_add_rcu(&tg
->list
, &task_groups
);
8609 WARN_ON(!parent
); /* root should already exist */
8611 tg
->parent
= parent
;
8612 list_add_rcu(&tg
->siblings
, &parent
->children
);
8613 INIT_LIST_HEAD(&tg
->children
);
8614 spin_unlock_irqrestore(&task_group_lock
, flags
);
8619 free_sched_group(tg
);
8620 return ERR_PTR(-ENOMEM
);
8623 /* rcu callback to free various structures associated with a task group */
8624 static void free_sched_group_rcu(struct rcu_head
*rhp
)
8626 /* now it should be safe to free those cfs_rqs */
8627 free_sched_group(container_of(rhp
, struct task_group
, rcu
));
8630 /* Destroy runqueue etc associated with a task group */
8631 void sched_destroy_group(struct task_group
*tg
)
8633 unsigned long flags
;
8636 spin_lock_irqsave(&task_group_lock
, flags
);
8637 for_each_possible_cpu(i
) {
8638 unregister_fair_sched_group(tg
, i
);
8639 unregister_rt_sched_group(tg
, i
);
8641 list_del_rcu(&tg
->list
);
8642 list_del_rcu(&tg
->siblings
);
8643 spin_unlock_irqrestore(&task_group_lock
, flags
);
8645 /* wait for possible concurrent references to cfs_rqs complete */
8646 call_rcu(&tg
->rcu
, free_sched_group_rcu
);
8649 /* change task's runqueue when it moves between groups.
8650 * The caller of this function should have put the task in its new group
8651 * by now. This function just updates tsk->se.cfs_rq and tsk->se.parent to
8652 * reflect its new group.
8654 void sched_move_task(struct task_struct
*tsk
)
8657 unsigned long flags
;
8660 rq
= task_rq_lock(tsk
, &flags
);
8662 update_rq_clock(rq
);
8664 running
= task_current(rq
, tsk
);
8665 on_rq
= tsk
->se
.on_rq
;
8668 dequeue_task(rq
, tsk
, 0);
8669 if (unlikely(running
))
8670 tsk
->sched_class
->put_prev_task(rq
, tsk
);
8672 set_task_rq(tsk
, task_cpu(tsk
));
8674 #ifdef CONFIG_FAIR_GROUP_SCHED
8675 if (tsk
->sched_class
->moved_group
)
8676 tsk
->sched_class
->moved_group(tsk
);
8679 if (unlikely(running
))
8680 tsk
->sched_class
->set_curr_task(rq
);
8682 enqueue_task(rq
, tsk
, 0);
8684 task_rq_unlock(rq
, &flags
);
8688 #ifdef CONFIG_FAIR_GROUP_SCHED
8689 static void __set_se_shares(struct sched_entity
*se
, unsigned long shares
)
8691 struct cfs_rq
*cfs_rq
= se
->cfs_rq
;
8696 dequeue_entity(cfs_rq
, se
, 0);
8698 se
->load
.weight
= shares
;
8699 se
->load
.inv_weight
= 0;
8702 enqueue_entity(cfs_rq
, se
, 0);
8705 static void set_se_shares(struct sched_entity
*se
, unsigned long shares
)
8707 struct cfs_rq
*cfs_rq
= se
->cfs_rq
;
8708 struct rq
*rq
= cfs_rq
->rq
;
8709 unsigned long flags
;
8711 spin_lock_irqsave(&rq
->lock
, flags
);
8712 __set_se_shares(se
, shares
);
8713 spin_unlock_irqrestore(&rq
->lock
, flags
);
8716 static DEFINE_MUTEX(shares_mutex
);
8718 int sched_group_set_shares(struct task_group
*tg
, unsigned long shares
)
8721 unsigned long flags
;
8724 * We can't change the weight of the root cgroup.
8730 * A weight of 0 or 1 can cause arithmetics problems.
8731 * (The default weight is 1024 - so there's no practical
8732 * limitation from this.)
8734 if (shares
< MIN_SHARES
)
8735 shares
= MIN_SHARES
;
8737 mutex_lock(&shares_mutex
);
8738 if (tg
->shares
== shares
)
8741 spin_lock_irqsave(&task_group_lock
, flags
);
8742 for_each_possible_cpu(i
)
8743 unregister_fair_sched_group(tg
, i
);
8744 list_del_rcu(&tg
->siblings
);
8745 spin_unlock_irqrestore(&task_group_lock
, flags
);
8747 /* wait for any ongoing reference to this group to finish */
8748 synchronize_sched();
8751 * Now we are free to modify the group's share on each cpu
8752 * w/o tripping rebalance_share or load_balance_fair.
8754 tg
->shares
= shares
;
8755 for_each_possible_cpu(i
) {
8759 cfs_rq_set_shares(tg
->cfs_rq
[i
], 0);
8760 set_se_shares(tg
->se
[i
], shares
/nr_cpu_ids
);
8764 * Enable load balance activity on this group, by inserting it back on
8765 * each cpu's rq->leaf_cfs_rq_list.
8767 spin_lock_irqsave(&task_group_lock
, flags
);
8768 for_each_possible_cpu(i
)
8769 register_fair_sched_group(tg
, i
);
8770 list_add_rcu(&tg
->siblings
, &tg
->parent
->children
);
8771 spin_unlock_irqrestore(&task_group_lock
, flags
);
8773 mutex_unlock(&shares_mutex
);
8777 unsigned long sched_group_shares(struct task_group
*tg
)
8783 #ifdef CONFIG_RT_GROUP_SCHED
8785 * Ensure that the real time constraints are schedulable.
8787 static DEFINE_MUTEX(rt_constraints_mutex
);
8789 static unsigned long to_ratio(u64 period
, u64 runtime
)
8791 if (runtime
== RUNTIME_INF
)
8794 return div64_u64(runtime
<< 16, period
);
8797 #ifdef CONFIG_CGROUP_SCHED
8798 static int __rt_schedulable(struct task_group
*tg
, u64 period
, u64 runtime
)
8800 struct task_group
*tgi
, *parent
= tg
->parent
;
8801 unsigned long total
= 0;
8804 if (global_rt_period() < period
)
8807 return to_ratio(period
, runtime
) <
8808 to_ratio(global_rt_period(), global_rt_runtime());
8811 if (ktime_to_ns(parent
->rt_bandwidth
.rt_period
) < period
)
8815 list_for_each_entry_rcu(tgi
, &parent
->children
, siblings
) {
8819 total
+= to_ratio(ktime_to_ns(tgi
->rt_bandwidth
.rt_period
),
8820 tgi
->rt_bandwidth
.rt_runtime
);
8824 return total
+ to_ratio(period
, runtime
) <
8825 to_ratio(ktime_to_ns(parent
->rt_bandwidth
.rt_period
),
8826 parent
->rt_bandwidth
.rt_runtime
);
8828 #elif defined CONFIG_USER_SCHED
8829 static int __rt_schedulable(struct task_group
*tg
, u64 period
, u64 runtime
)
8831 struct task_group
*tgi
;
8832 unsigned long total
= 0;
8833 unsigned long global_ratio
=
8834 to_ratio(global_rt_period(), global_rt_runtime());
8837 list_for_each_entry_rcu(tgi
, &task_groups
, list
) {
8841 total
+= to_ratio(ktime_to_ns(tgi
->rt_bandwidth
.rt_period
),
8842 tgi
->rt_bandwidth
.rt_runtime
);
8846 return total
+ to_ratio(period
, runtime
) < global_ratio
;
8850 /* Must be called with tasklist_lock held */
8851 static inline int tg_has_rt_tasks(struct task_group
*tg
)
8853 struct task_struct
*g
, *p
;
8854 do_each_thread(g
, p
) {
8855 if (rt_task(p
) && rt_rq_of_se(&p
->rt
)->tg
== tg
)
8857 } while_each_thread(g
, p
);
8861 static int tg_set_bandwidth(struct task_group
*tg
,
8862 u64 rt_period
, u64 rt_runtime
)
8866 mutex_lock(&rt_constraints_mutex
);
8867 read_lock(&tasklist_lock
);
8868 if (rt_runtime
== 0 && tg_has_rt_tasks(tg
)) {
8872 if (!__rt_schedulable(tg
, rt_period
, rt_runtime
)) {
8877 spin_lock_irq(&tg
->rt_bandwidth
.rt_runtime_lock
);
8878 tg
->rt_bandwidth
.rt_period
= ns_to_ktime(rt_period
);
8879 tg
->rt_bandwidth
.rt_runtime
= rt_runtime
;
8881 for_each_possible_cpu(i
) {
8882 struct rt_rq
*rt_rq
= tg
->rt_rq
[i
];
8884 spin_lock(&rt_rq
->rt_runtime_lock
);
8885 rt_rq
->rt_runtime
= rt_runtime
;
8886 spin_unlock(&rt_rq
->rt_runtime_lock
);
8888 spin_unlock_irq(&tg
->rt_bandwidth
.rt_runtime_lock
);
8890 read_unlock(&tasklist_lock
);
8891 mutex_unlock(&rt_constraints_mutex
);
8896 int sched_group_set_rt_runtime(struct task_group
*tg
, long rt_runtime_us
)
8898 u64 rt_runtime
, rt_period
;
8900 rt_period
= ktime_to_ns(tg
->rt_bandwidth
.rt_period
);
8901 rt_runtime
= (u64
)rt_runtime_us
* NSEC_PER_USEC
;
8902 if (rt_runtime_us
< 0)
8903 rt_runtime
= RUNTIME_INF
;
8905 return tg_set_bandwidth(tg
, rt_period
, rt_runtime
);
8908 long sched_group_rt_runtime(struct task_group
*tg
)
8912 if (tg
->rt_bandwidth
.rt_runtime
== RUNTIME_INF
)
8915 rt_runtime_us
= tg
->rt_bandwidth
.rt_runtime
;
8916 do_div(rt_runtime_us
, NSEC_PER_USEC
);
8917 return rt_runtime_us
;
8920 int sched_group_set_rt_period(struct task_group
*tg
, long rt_period_us
)
8922 u64 rt_runtime
, rt_period
;
8924 rt_period
= (u64
)rt_period_us
* NSEC_PER_USEC
;
8925 rt_runtime
= tg
->rt_bandwidth
.rt_runtime
;
8927 return tg_set_bandwidth(tg
, rt_period
, rt_runtime
);
8930 long sched_group_rt_period(struct task_group
*tg
)
8934 rt_period_us
= ktime_to_ns(tg
->rt_bandwidth
.rt_period
);
8935 do_div(rt_period_us
, NSEC_PER_USEC
);
8936 return rt_period_us
;
8939 static int sched_rt_global_constraints(void)
8943 mutex_lock(&rt_constraints_mutex
);
8944 if (!__rt_schedulable(NULL
, 1, 0))
8946 mutex_unlock(&rt_constraints_mutex
);
8951 static int sched_rt_global_constraints(void)
8953 unsigned long flags
;
8956 spin_lock_irqsave(&def_rt_bandwidth
.rt_runtime_lock
, flags
);
8957 for_each_possible_cpu(i
) {
8958 struct rt_rq
*rt_rq
= &cpu_rq(i
)->rt
;
8960 spin_lock(&rt_rq
->rt_runtime_lock
);
8961 rt_rq
->rt_runtime
= global_rt_runtime();
8962 spin_unlock(&rt_rq
->rt_runtime_lock
);
8964 spin_unlock_irqrestore(&def_rt_bandwidth
.rt_runtime_lock
, flags
);
8970 int sched_rt_handler(struct ctl_table
*table
, int write
,
8971 struct file
*filp
, void __user
*buffer
, size_t *lenp
,
8975 int old_period
, old_runtime
;
8976 static DEFINE_MUTEX(mutex
);
8979 old_period
= sysctl_sched_rt_period
;
8980 old_runtime
= sysctl_sched_rt_runtime
;
8982 ret
= proc_dointvec(table
, write
, filp
, buffer
, lenp
, ppos
);
8984 if (!ret
&& write
) {
8985 ret
= sched_rt_global_constraints();
8987 sysctl_sched_rt_period
= old_period
;
8988 sysctl_sched_rt_runtime
= old_runtime
;
8990 def_rt_bandwidth
.rt_runtime
= global_rt_runtime();
8991 def_rt_bandwidth
.rt_period
=
8992 ns_to_ktime(global_rt_period());
8995 mutex_unlock(&mutex
);
9000 #ifdef CONFIG_CGROUP_SCHED
9002 /* return corresponding task_group object of a cgroup */
9003 static inline struct task_group
*cgroup_tg(struct cgroup
*cgrp
)
9005 return container_of(cgroup_subsys_state(cgrp
, cpu_cgroup_subsys_id
),
9006 struct task_group
, css
);
9009 static struct cgroup_subsys_state
*
9010 cpu_cgroup_create(struct cgroup_subsys
*ss
, struct cgroup
*cgrp
)
9012 struct task_group
*tg
, *parent
;
9014 if (!cgrp
->parent
) {
9015 /* This is early initialization for the top cgroup */
9016 init_task_group
.css
.cgroup
= cgrp
;
9017 return &init_task_group
.css
;
9020 parent
= cgroup_tg(cgrp
->parent
);
9021 tg
= sched_create_group(parent
);
9023 return ERR_PTR(-ENOMEM
);
9025 /* Bind the cgroup to task_group object we just created */
9026 tg
->css
.cgroup
= cgrp
;
9032 cpu_cgroup_destroy(struct cgroup_subsys
*ss
, struct cgroup
*cgrp
)
9034 struct task_group
*tg
= cgroup_tg(cgrp
);
9036 sched_destroy_group(tg
);
9040 cpu_cgroup_can_attach(struct cgroup_subsys
*ss
, struct cgroup
*cgrp
,
9041 struct task_struct
*tsk
)
9043 #ifdef CONFIG_RT_GROUP_SCHED
9044 /* Don't accept realtime tasks when there is no way for them to run */
9045 if (rt_task(tsk
) && cgroup_tg(cgrp
)->rt_bandwidth
.rt_runtime
== 0)
9048 /* We don't support RT-tasks being in separate groups */
9049 if (tsk
->sched_class
!= &fair_sched_class
)
9057 cpu_cgroup_attach(struct cgroup_subsys
*ss
, struct cgroup
*cgrp
,
9058 struct cgroup
*old_cont
, struct task_struct
*tsk
)
9060 sched_move_task(tsk
);
9063 #ifdef CONFIG_FAIR_GROUP_SCHED
9064 static int cpu_shares_write_u64(struct cgroup
*cgrp
, struct cftype
*cftype
,
9067 return sched_group_set_shares(cgroup_tg(cgrp
), shareval
);
9070 static u64
cpu_shares_read_u64(struct cgroup
*cgrp
, struct cftype
*cft
)
9072 struct task_group
*tg
= cgroup_tg(cgrp
);
9074 return (u64
) tg
->shares
;
9078 #ifdef CONFIG_RT_GROUP_SCHED
9079 static ssize_t
cpu_rt_runtime_write(struct cgroup
*cgrp
, struct cftype
*cft
,
9082 return sched_group_set_rt_runtime(cgroup_tg(cgrp
), val
);
9085 static s64
cpu_rt_runtime_read(struct cgroup
*cgrp
, struct cftype
*cft
)
9087 return sched_group_rt_runtime(cgroup_tg(cgrp
));
9090 static int cpu_rt_period_write_uint(struct cgroup
*cgrp
, struct cftype
*cftype
,
9093 return sched_group_set_rt_period(cgroup_tg(cgrp
), rt_period_us
);
9096 static u64
cpu_rt_period_read_uint(struct cgroup
*cgrp
, struct cftype
*cft
)
9098 return sched_group_rt_period(cgroup_tg(cgrp
));
9102 static struct cftype cpu_files
[] = {
9103 #ifdef CONFIG_FAIR_GROUP_SCHED
9106 .read_u64
= cpu_shares_read_u64
,
9107 .write_u64
= cpu_shares_write_u64
,
9110 #ifdef CONFIG_RT_GROUP_SCHED
9112 .name
= "rt_runtime_us",
9113 .read_s64
= cpu_rt_runtime_read
,
9114 .write_s64
= cpu_rt_runtime_write
,
9117 .name
= "rt_period_us",
9118 .read_u64
= cpu_rt_period_read_uint
,
9119 .write_u64
= cpu_rt_period_write_uint
,
9124 static int cpu_cgroup_populate(struct cgroup_subsys
*ss
, struct cgroup
*cont
)
9126 return cgroup_add_files(cont
, ss
, cpu_files
, ARRAY_SIZE(cpu_files
));
9129 struct cgroup_subsys cpu_cgroup_subsys
= {
9131 .create
= cpu_cgroup_create
,
9132 .destroy
= cpu_cgroup_destroy
,
9133 .can_attach
= cpu_cgroup_can_attach
,
9134 .attach
= cpu_cgroup_attach
,
9135 .populate
= cpu_cgroup_populate
,
9136 .subsys_id
= cpu_cgroup_subsys_id
,
9140 #endif /* CONFIG_CGROUP_SCHED */
9142 #ifdef CONFIG_CGROUP_CPUACCT
9145 * CPU accounting code for task groups.
9147 * Based on the work by Paul Menage (menage@google.com) and Balbir Singh
9148 * (balbir@in.ibm.com).
9151 /* track cpu usage of a group of tasks */
9153 struct cgroup_subsys_state css
;
9154 /* cpuusage holds pointer to a u64-type object on every cpu */
9158 struct cgroup_subsys cpuacct_subsys
;
9160 /* return cpu accounting group corresponding to this container */
9161 static inline struct cpuacct
*cgroup_ca(struct cgroup
*cgrp
)
9163 return container_of(cgroup_subsys_state(cgrp
, cpuacct_subsys_id
),
9164 struct cpuacct
, css
);
9167 /* return cpu accounting group to which this task belongs */
9168 static inline struct cpuacct
*task_ca(struct task_struct
*tsk
)
9170 return container_of(task_subsys_state(tsk
, cpuacct_subsys_id
),
9171 struct cpuacct
, css
);
9174 /* create a new cpu accounting group */
9175 static struct cgroup_subsys_state
*cpuacct_create(
9176 struct cgroup_subsys
*ss
, struct cgroup
*cgrp
)
9178 struct cpuacct
*ca
= kzalloc(sizeof(*ca
), GFP_KERNEL
);
9181 return ERR_PTR(-ENOMEM
);
9183 ca
->cpuusage
= alloc_percpu(u64
);
9184 if (!ca
->cpuusage
) {
9186 return ERR_PTR(-ENOMEM
);
9192 /* destroy an existing cpu accounting group */
9194 cpuacct_destroy(struct cgroup_subsys
*ss
, struct cgroup
*cgrp
)
9196 struct cpuacct
*ca
= cgroup_ca(cgrp
);
9198 free_percpu(ca
->cpuusage
);
9202 /* return total cpu usage (in nanoseconds) of a group */
9203 static u64
cpuusage_read(struct cgroup
*cgrp
, struct cftype
*cft
)
9205 struct cpuacct
*ca
= cgroup_ca(cgrp
);
9206 u64 totalcpuusage
= 0;
9209 for_each_possible_cpu(i
) {
9210 u64
*cpuusage
= percpu_ptr(ca
->cpuusage
, i
);
9213 * Take rq->lock to make 64-bit addition safe on 32-bit
9216 spin_lock_irq(&cpu_rq(i
)->lock
);
9217 totalcpuusage
+= *cpuusage
;
9218 spin_unlock_irq(&cpu_rq(i
)->lock
);
9221 return totalcpuusage
;
9224 static int cpuusage_write(struct cgroup
*cgrp
, struct cftype
*cftype
,
9227 struct cpuacct
*ca
= cgroup_ca(cgrp
);
9236 for_each_possible_cpu(i
) {
9237 u64
*cpuusage
= percpu_ptr(ca
->cpuusage
, i
);
9239 spin_lock_irq(&cpu_rq(i
)->lock
);
9241 spin_unlock_irq(&cpu_rq(i
)->lock
);
9247 static struct cftype files
[] = {
9250 .read_u64
= cpuusage_read
,
9251 .write_u64
= cpuusage_write
,
9255 static int cpuacct_populate(struct cgroup_subsys
*ss
, struct cgroup
*cgrp
)
9257 return cgroup_add_files(cgrp
, ss
, files
, ARRAY_SIZE(files
));
9261 * charge this task's execution time to its accounting group.
9263 * called with rq->lock held.
9265 static void cpuacct_charge(struct task_struct
*tsk
, u64 cputime
)
9269 if (!cpuacct_subsys
.active
)
9274 u64
*cpuusage
= percpu_ptr(ca
->cpuusage
, task_cpu(tsk
));
9276 *cpuusage
+= cputime
;
9280 struct cgroup_subsys cpuacct_subsys
= {
9282 .create
= cpuacct_create
,
9283 .destroy
= cpuacct_destroy
,
9284 .populate
= cpuacct_populate
,
9285 .subsys_id
= cpuacct_subsys_id
,
9287 #endif /* CONFIG_CGROUP_CPUACCT */