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/perf_event.h>
43 #include <linux/security.h>
44 #include <linux/notifier.h>
45 #include <linux/profile.h>
46 #include <linux/freezer.h>
47 #include <linux/vmalloc.h>
48 #include <linux/blkdev.h>
49 #include <linux/delay.h>
50 #include <linux/pid_namespace.h>
51 #include <linux/smp.h>
52 #include <linux/threads.h>
53 #include <linux/timer.h>
54 #include <linux/rcupdate.h>
55 #include <linux/cpu.h>
56 #include <linux/cpuset.h>
57 #include <linux/percpu.h>
58 #include <linux/kthread.h>
59 #include <linux/proc_fs.h>
60 #include <linux/seq_file.h>
61 #include <linux/sysctl.h>
62 #include <linux/syscalls.h>
63 #include <linux/times.h>
64 #include <linux/tsacct_kern.h>
65 #include <linux/kprobes.h>
66 #include <linux/delayacct.h>
67 #include <linux/unistd.h>
68 #include <linux/pagemap.h>
69 #include <linux/hrtimer.h>
70 #include <linux/tick.h>
71 #include <linux/debugfs.h>
72 #include <linux/ctype.h>
73 #include <linux/ftrace.h>
76 #include <asm/irq_regs.h>
78 #include "sched_cpupri.h"
80 #define CREATE_TRACE_POINTS
81 #include <trace/events/sched.h>
84 * Convert user-nice values [ -20 ... 0 ... 19 ]
85 * to static priority [ MAX_RT_PRIO..MAX_PRIO-1 ],
88 #define NICE_TO_PRIO(nice) (MAX_RT_PRIO + (nice) + 20)
89 #define PRIO_TO_NICE(prio) ((prio) - MAX_RT_PRIO - 20)
90 #define TASK_NICE(p) PRIO_TO_NICE((p)->static_prio)
93 * 'User priority' is the nice value converted to something we
94 * can work with better when scaling various scheduler parameters,
95 * it's a [ 0 ... 39 ] range.
97 #define USER_PRIO(p) ((p)-MAX_RT_PRIO)
98 #define TASK_USER_PRIO(p) USER_PRIO((p)->static_prio)
99 #define MAX_USER_PRIO (USER_PRIO(MAX_PRIO))
102 * Helpers for converting nanosecond timing to jiffy resolution
104 #define NS_TO_JIFFIES(TIME) ((unsigned long)(TIME) / (NSEC_PER_SEC / HZ))
106 #define NICE_0_LOAD SCHED_LOAD_SCALE
107 #define NICE_0_SHIFT SCHED_LOAD_SHIFT
110 * These are the 'tuning knobs' of the scheduler:
112 * default timeslice is 100 msecs (used only for SCHED_RR tasks).
113 * Timeslices get refilled after they expire.
115 #define DEF_TIMESLICE (100 * HZ / 1000)
118 * single value that denotes runtime == period, ie unlimited time.
120 #define RUNTIME_INF ((u64)~0ULL)
122 static inline int rt_policy(int policy)
124 if (unlikely(policy == SCHED_FIFO || policy == SCHED_RR))
129 static inline int task_has_rt_policy(struct task_struct *p)
131 return rt_policy(p->policy);
135 * This is the priority-queue data structure of the RT scheduling class:
137 struct rt_prio_array {
138 DECLARE_BITMAP(bitmap, MAX_RT_PRIO+1); /* include 1 bit for delimiter */
139 struct list_head queue[MAX_RT_PRIO];
142 struct rt_bandwidth {
143 /* nests inside the rq lock: */
144 raw_spinlock_t rt_runtime_lock;
147 struct hrtimer rt_period_timer;
150 static struct rt_bandwidth def_rt_bandwidth;
152 static int do_sched_rt_period_timer(struct rt_bandwidth *rt_b, int overrun);
154 static enum hrtimer_restart sched_rt_period_timer(struct hrtimer *timer)
156 struct rt_bandwidth *rt_b =
157 container_of(timer, struct rt_bandwidth, rt_period_timer);
163 now = hrtimer_cb_get_time(timer);
164 overrun = hrtimer_forward(timer, now, rt_b->rt_period);
169 idle = do_sched_rt_period_timer(rt_b, overrun);
172 return idle ? HRTIMER_NORESTART : HRTIMER_RESTART;
176 void init_rt_bandwidth(struct rt_bandwidth *rt_b, u64 period, u64 runtime)
178 rt_b->rt_period = ns_to_ktime(period);
179 rt_b->rt_runtime = runtime;
181 raw_spin_lock_init(&rt_b->rt_runtime_lock);
183 hrtimer_init(&rt_b->rt_period_timer,
184 CLOCK_MONOTONIC, HRTIMER_MODE_REL);
185 rt_b->rt_period_timer.function = sched_rt_period_timer;
188 static inline int rt_bandwidth_enabled(void)
190 return sysctl_sched_rt_runtime >= 0;
193 static void start_rt_bandwidth(struct rt_bandwidth *rt_b)
197 if (!rt_bandwidth_enabled() || rt_b->rt_runtime == RUNTIME_INF)
200 if (hrtimer_active(&rt_b->rt_period_timer))
203 raw_spin_lock(&rt_b->rt_runtime_lock);
208 if (hrtimer_active(&rt_b->rt_period_timer))
211 now = hrtimer_cb_get_time(&rt_b->rt_period_timer);
212 hrtimer_forward(&rt_b->rt_period_timer, now, rt_b->rt_period);
214 soft = hrtimer_get_softexpires(&rt_b->rt_period_timer);
215 hard = hrtimer_get_expires(&rt_b->rt_period_timer);
216 delta = ktime_to_ns(ktime_sub(hard, soft));
217 __hrtimer_start_range_ns(&rt_b->rt_period_timer, soft, delta,
218 HRTIMER_MODE_ABS_PINNED, 0);
220 raw_spin_unlock(&rt_b->rt_runtime_lock);
223 #ifdef CONFIG_RT_GROUP_SCHED
224 static void destroy_rt_bandwidth(struct rt_bandwidth *rt_b)
226 hrtimer_cancel(&rt_b->rt_period_timer);
231 * sched_domains_mutex serializes calls to arch_init_sched_domains,
232 * detach_destroy_domains and partition_sched_domains.
234 static DEFINE_MUTEX(sched_domains_mutex);
236 #ifdef CONFIG_GROUP_SCHED
238 #include <linux/cgroup.h>
242 static LIST_HEAD(task_groups);
244 /* task group related information */
246 #ifdef CONFIG_CGROUP_SCHED
247 struct cgroup_subsys_state css;
250 #ifdef CONFIG_USER_SCHED
254 #ifdef CONFIG_FAIR_GROUP_SCHED
255 /* schedulable entities of this group on each cpu */
256 struct sched_entity **se;
257 /* runqueue "owned" by this group on each cpu */
258 struct cfs_rq **cfs_rq;
259 unsigned long shares;
262 #ifdef CONFIG_RT_GROUP_SCHED
263 struct sched_rt_entity **rt_se;
264 struct rt_rq **rt_rq;
266 struct rt_bandwidth rt_bandwidth;
270 struct list_head list;
272 struct task_group *parent;
273 struct list_head siblings;
274 struct list_head children;
277 #ifdef CONFIG_USER_SCHED
279 /* Helper function to pass uid information to create_sched_user() */
280 void set_tg_uid(struct user_struct *user)
282 user->tg->uid = user->uid;
287 * Every UID task group (including init_task_group aka UID-0) will
288 * be a child to this group.
290 struct task_group root_task_group;
292 #ifdef CONFIG_FAIR_GROUP_SCHED
293 /* Default task group's sched entity on each cpu */
294 static DEFINE_PER_CPU(struct sched_entity, init_sched_entity);
295 /* Default task group's cfs_rq on each cpu */
296 static DEFINE_PER_CPU_SHARED_ALIGNED(struct cfs_rq, init_tg_cfs_rq);
297 #endif /* CONFIG_FAIR_GROUP_SCHED */
299 #ifdef CONFIG_RT_GROUP_SCHED
300 static DEFINE_PER_CPU(struct sched_rt_entity, init_sched_rt_entity);
301 static DEFINE_PER_CPU_SHARED_ALIGNED(struct rt_rq, init_rt_rq_var);
302 #endif /* CONFIG_RT_GROUP_SCHED */
303 #else /* !CONFIG_USER_SCHED */
304 #define root_task_group init_task_group
305 #endif /* CONFIG_USER_SCHED */
307 /* task_group_lock serializes add/remove of task groups and also changes to
308 * a task group's cpu shares.
310 static DEFINE_SPINLOCK(task_group_lock);
312 #ifdef CONFIG_FAIR_GROUP_SCHED
315 static int root_task_group_empty(void)
317 return list_empty(&root_task_group.children);
321 #ifdef CONFIG_USER_SCHED
322 # define INIT_TASK_GROUP_LOAD (2*NICE_0_LOAD)
323 #else /* !CONFIG_USER_SCHED */
324 # define INIT_TASK_GROUP_LOAD NICE_0_LOAD
325 #endif /* CONFIG_USER_SCHED */
328 * A weight of 0 or 1 can cause arithmetics problems.
329 * A weight of a cfs_rq is the sum of weights of which entities
330 * are queued on this cfs_rq, so a weight of a entity should not be
331 * too large, so as the shares value of a task group.
332 * (The default weight is 1024 - so there's no practical
333 * limitation from this.)
336 #define MAX_SHARES (1UL << 18)
338 static int init_task_group_load = INIT_TASK_GROUP_LOAD;
341 /* Default task group.
342 * Every task in system belong to this group at bootup.
344 struct task_group init_task_group;
346 /* return group to which a task belongs */
347 static inline struct task_group *task_group(struct task_struct *p)
349 struct task_group *tg;
351 #ifdef CONFIG_USER_SCHED
353 tg = __task_cred(p)->user->tg;
355 #elif defined(CONFIG_CGROUP_SCHED)
356 tg = container_of(task_subsys_state(p, cpu_cgroup_subsys_id),
357 struct task_group, css);
359 tg = &init_task_group;
364 /* Change a task's cfs_rq and parent entity if it moves across CPUs/groups */
365 static inline void set_task_rq(struct task_struct *p, unsigned int cpu)
367 #ifdef CONFIG_FAIR_GROUP_SCHED
368 p->se.cfs_rq = task_group(p)->cfs_rq[cpu];
369 p->se.parent = task_group(p)->se[cpu];
372 #ifdef CONFIG_RT_GROUP_SCHED
373 p->rt.rt_rq = task_group(p)->rt_rq[cpu];
374 p->rt.parent = task_group(p)->rt_se[cpu];
380 static inline void set_task_rq(struct task_struct *p, unsigned int cpu) { }
381 static inline struct task_group *task_group(struct task_struct *p)
386 #endif /* CONFIG_GROUP_SCHED */
388 /* CFS-related fields in a runqueue */
390 struct load_weight load;
391 unsigned long nr_running;
396 struct rb_root tasks_timeline;
397 struct rb_node *rb_leftmost;
399 struct list_head tasks;
400 struct list_head *balance_iterator;
403 * 'curr' points to currently running entity on this cfs_rq.
404 * It is set to NULL otherwise (i.e when none are currently running).
406 struct sched_entity *curr, *next, *last;
408 unsigned int nr_spread_over;
410 #ifdef CONFIG_FAIR_GROUP_SCHED
411 struct rq *rq; /* cpu runqueue to which this cfs_rq is attached */
414 * leaf cfs_rqs are those that hold tasks (lowest schedulable entity in
415 * a hierarchy). Non-leaf lrqs hold other higher schedulable entities
416 * (like users, containers etc.)
418 * leaf_cfs_rq_list ties together list of leaf cfs_rq's in a cpu. This
419 * list is used during load balance.
421 struct list_head leaf_cfs_rq_list;
422 struct task_group *tg; /* group that "owns" this runqueue */
426 * the part of load.weight contributed by tasks
428 unsigned long task_weight;
431 * h_load = weight * f(tg)
433 * Where f(tg) is the recursive weight fraction assigned to
436 unsigned long h_load;
439 * this cpu's part of tg->shares
441 unsigned long shares;
444 * load.weight at the time we set shares
446 unsigned long rq_weight;
451 /* Real-Time classes' related field in a runqueue: */
453 struct rt_prio_array active;
454 unsigned long rt_nr_running;
455 #if defined CONFIG_SMP || defined CONFIG_RT_GROUP_SCHED
457 int curr; /* highest queued rt task prio */
459 int next; /* next highest */
464 unsigned long rt_nr_migratory;
465 unsigned long rt_nr_total;
467 struct plist_head pushable_tasks;
472 /* Nests inside the rq lock: */
473 raw_spinlock_t rt_runtime_lock;
475 #ifdef CONFIG_RT_GROUP_SCHED
476 unsigned long rt_nr_boosted;
479 struct list_head leaf_rt_rq_list;
480 struct task_group *tg;
481 struct sched_rt_entity *rt_se;
488 * We add the notion of a root-domain which will be used to define per-domain
489 * variables. Each exclusive cpuset essentially defines an island domain by
490 * fully partitioning the member cpus from any other cpuset. Whenever a new
491 * exclusive cpuset is created, we also create and attach a new root-domain
498 cpumask_var_t online;
501 * The "RT overload" flag: it gets set if a CPU has more than
502 * one runnable RT task.
504 cpumask_var_t rto_mask;
507 struct cpupri cpupri;
512 * By default the system creates a single root-domain with all cpus as
513 * members (mimicking the global state we have today).
515 static struct root_domain def_root_domain;
520 * This is the main, per-CPU runqueue data structure.
522 * Locking rule: those places that want to lock multiple runqueues
523 * (such as the load balancing or the thread migration code), lock
524 * acquire operations must be ordered by ascending &runqueue.
531 * nr_running and cpu_load should be in the same cacheline because
532 * remote CPUs use both these fields when doing load calculation.
534 unsigned long nr_running;
535 #define CPU_LOAD_IDX_MAX 5
536 unsigned long cpu_load[CPU_LOAD_IDX_MAX];
538 unsigned char in_nohz_recently;
540 /* capture load from *all* tasks on this cpu: */
541 struct load_weight load;
542 unsigned long nr_load_updates;
548 #ifdef CONFIG_FAIR_GROUP_SCHED
549 /* list of leaf cfs_rq on this cpu: */
550 struct list_head leaf_cfs_rq_list;
552 #ifdef CONFIG_RT_GROUP_SCHED
553 struct list_head leaf_rt_rq_list;
557 * This is part of a global counter where only the total sum
558 * over all CPUs matters. A task can increase this counter on
559 * one CPU and if it got migrated afterwards it may decrease
560 * it on another CPU. Always updated under the runqueue lock:
562 unsigned long nr_uninterruptible;
564 struct task_struct *curr, *idle;
565 unsigned long next_balance;
566 struct mm_struct *prev_mm;
573 struct root_domain *rd;
574 struct sched_domain *sd;
576 unsigned char idle_at_tick;
577 /* For active balancing */
581 /* cpu of this runqueue: */
585 unsigned long avg_load_per_task;
587 struct task_struct *migration_thread;
588 struct list_head migration_queue;
596 /* calc_load related fields */
597 unsigned long calc_load_update;
598 long calc_load_active;
600 #ifdef CONFIG_SCHED_HRTICK
602 int hrtick_csd_pending;
603 struct call_single_data hrtick_csd;
605 struct hrtimer hrtick_timer;
608 #ifdef CONFIG_SCHEDSTATS
610 struct sched_info rq_sched_info;
611 unsigned long long rq_cpu_time;
612 /* could above be rq->cfs_rq.exec_clock + rq->rt_rq.rt_runtime ? */
614 /* sys_sched_yield() stats */
615 unsigned int yld_count;
617 /* schedule() stats */
618 unsigned int sched_switch;
619 unsigned int sched_count;
620 unsigned int sched_goidle;
622 /* try_to_wake_up() stats */
623 unsigned int ttwu_count;
624 unsigned int ttwu_local;
627 unsigned int bkl_count;
631 static DEFINE_PER_CPU_SHARED_ALIGNED(struct rq, runqueues);
634 void check_preempt_curr(struct rq *rq, struct task_struct *p, int flags)
636 rq->curr->sched_class->check_preempt_curr(rq, p, flags);
639 static inline int cpu_of(struct rq *rq)
648 #define rcu_dereference_check_sched_domain(p) \
649 rcu_dereference_check((p), \
650 rcu_read_lock_sched_held() || \
651 lockdep_is_held(&sched_domains_mutex))
654 * The domain tree (rq->sd) is protected by RCU's quiescent state transition.
655 * See detach_destroy_domains: synchronize_sched for details.
657 * The domain tree of any CPU may only be accessed from within
658 * preempt-disabled sections.
660 #define for_each_domain(cpu, __sd) \
661 for (__sd = rcu_dereference_check_sched_domain(cpu_rq(cpu)->sd); __sd; __sd = __sd->parent)
663 #define cpu_rq(cpu) (&per_cpu(runqueues, (cpu)))
664 #define this_rq() (&__get_cpu_var(runqueues))
665 #define task_rq(p) cpu_rq(task_cpu(p))
666 #define cpu_curr(cpu) (cpu_rq(cpu)->curr)
667 #define raw_rq() (&__raw_get_cpu_var(runqueues))
669 inline void update_rq_clock(struct rq *rq)
671 rq->clock = sched_clock_cpu(cpu_of(rq));
675 * Tunables that become constants when CONFIG_SCHED_DEBUG is off:
677 #ifdef CONFIG_SCHED_DEBUG
678 # define const_debug __read_mostly
680 # define const_debug static const
685 * @cpu: the processor in question.
687 * Returns true if the current cpu runqueue is locked.
688 * This interface allows printk to be called with the runqueue lock
689 * held and know whether or not it is OK to wake up the klogd.
691 int runqueue_is_locked(int cpu)
693 return raw_spin_is_locked(&cpu_rq(cpu)->lock);
697 * Debugging: various feature bits
700 #define SCHED_FEAT(name, enabled) \
701 __SCHED_FEAT_##name ,
704 #include "sched_features.h"
709 #define SCHED_FEAT(name, enabled) \
710 (1UL << __SCHED_FEAT_##name) * enabled |
712 const_debug unsigned int sysctl_sched_features =
713 #include "sched_features.h"
718 #ifdef CONFIG_SCHED_DEBUG
719 #define SCHED_FEAT(name, enabled) \
722 static __read_mostly char *sched_feat_names[] = {
723 #include "sched_features.h"
729 static int sched_feat_show(struct seq_file *m, void *v)
733 for (i = 0; sched_feat_names[i]; i++) {
734 if (!(sysctl_sched_features & (1UL << i)))
736 seq_printf(m, "%s ", sched_feat_names[i]);
744 sched_feat_write(struct file *filp, const char __user *ubuf,
745 size_t cnt, loff_t *ppos)
755 if (copy_from_user(&buf, ubuf, cnt))
760 if (strncmp(buf, "NO_", 3) == 0) {
765 for (i = 0; sched_feat_names[i]; i++) {
766 int len = strlen(sched_feat_names[i]);
768 if (strncmp(cmp, sched_feat_names[i], len) == 0) {
770 sysctl_sched_features &= ~(1UL << i);
772 sysctl_sched_features |= (1UL << i);
777 if (!sched_feat_names[i])
785 static int sched_feat_open(struct inode *inode, struct file *filp)
787 return single_open(filp, sched_feat_show, NULL);
790 static const struct file_operations sched_feat_fops = {
791 .open = sched_feat_open,
792 .write = sched_feat_write,
795 .release = single_release,
798 static __init int sched_init_debug(void)
800 debugfs_create_file("sched_features", 0644, NULL, NULL,
805 late_initcall(sched_init_debug);
809 #define sched_feat(x) (sysctl_sched_features & (1UL << __SCHED_FEAT_##x))
812 * Number of tasks to iterate in a single balance run.
813 * Limited because this is done with IRQs disabled.
815 const_debug unsigned int sysctl_sched_nr_migrate = 32;
818 * ratelimit for updating the group shares.
821 unsigned int sysctl_sched_shares_ratelimit = 250000;
822 unsigned int normalized_sysctl_sched_shares_ratelimit = 250000;
825 * Inject some fuzzyness into changing the per-cpu group shares
826 * this avoids remote rq-locks at the expense of fairness.
829 unsigned int sysctl_sched_shares_thresh = 4;
832 * period over which we average the RT time consumption, measured
837 const_debug unsigned int sysctl_sched_time_avg = MSEC_PER_SEC;
840 * period over which we measure -rt task cpu usage in us.
843 unsigned int sysctl_sched_rt_period = 1000000;
845 static __read_mostly int scheduler_running;
848 * part of the period that we allow rt tasks to run in us.
851 int sysctl_sched_rt_runtime = 950000;
853 static inline u64 global_rt_period(void)
855 return (u64)sysctl_sched_rt_period * NSEC_PER_USEC;
858 static inline u64 global_rt_runtime(void)
860 if (sysctl_sched_rt_runtime < 0)
863 return (u64)sysctl_sched_rt_runtime * NSEC_PER_USEC;
866 #ifndef prepare_arch_switch
867 # define prepare_arch_switch(next) do { } while (0)
869 #ifndef finish_arch_switch
870 # define finish_arch_switch(prev) do { } while (0)
873 static inline int task_current(struct rq *rq, struct task_struct *p)
875 return rq->curr == p;
878 #ifndef __ARCH_WANT_UNLOCKED_CTXSW
879 static inline int task_running(struct rq *rq, struct task_struct *p)
881 return task_current(rq, p);
884 static inline void prepare_lock_switch(struct rq *rq, struct task_struct *next)
888 static inline void finish_lock_switch(struct rq *rq, struct task_struct *prev)
890 #ifdef CONFIG_DEBUG_SPINLOCK
891 /* this is a valid case when another task releases the spinlock */
892 rq->lock.owner = current;
895 * If we are tracking spinlock dependencies then we have to
896 * fix up the runqueue lock - which gets 'carried over' from
899 spin_acquire(&rq->lock.dep_map, 0, 0, _THIS_IP_);
901 raw_spin_unlock_irq(&rq->lock);
904 #else /* __ARCH_WANT_UNLOCKED_CTXSW */
905 static inline int task_running(struct rq *rq, struct task_struct *p)
910 return task_current(rq, p);
914 static inline void prepare_lock_switch(struct rq *rq, struct task_struct *next)
918 * We can optimise this out completely for !SMP, because the
919 * SMP rebalancing from interrupt is the only thing that cares
924 #ifdef __ARCH_WANT_INTERRUPTS_ON_CTXSW
925 raw_spin_unlock_irq(&rq->lock);
927 raw_spin_unlock(&rq->lock);
931 static inline void finish_lock_switch(struct rq *rq, struct task_struct *prev)
935 * After ->oncpu is cleared, the task can be moved to a different CPU.
936 * We must ensure this doesn't happen until the switch is completely
942 #ifndef __ARCH_WANT_INTERRUPTS_ON_CTXSW
946 #endif /* __ARCH_WANT_UNLOCKED_CTXSW */
949 * Check whether the task is waking, we use this to synchronize against
950 * ttwu() so that task_cpu() reports a stable number.
952 * We need to make an exception for PF_STARTING tasks because the fork
953 * path might require task_rq_lock() to work, eg. it can call
954 * set_cpus_allowed_ptr() from the cpuset clone_ns code.
956 static inline int task_is_waking(struct task_struct *p)
958 return unlikely((p->state == TASK_WAKING) && !(p->flags & PF_STARTING));
962 * __task_rq_lock - lock the runqueue a given task resides on.
963 * Must be called interrupts disabled.
965 static inline struct rq *__task_rq_lock(struct task_struct *p)
971 while (task_is_waking(p))
974 raw_spin_lock(&rq->lock);
975 if (likely(rq == task_rq(p) && !task_is_waking(p)))
977 raw_spin_unlock(&rq->lock);
982 * task_rq_lock - lock the runqueue a given task resides on and disable
983 * interrupts. Note the ordering: we can safely lookup the task_rq without
984 * explicitly disabling preemption.
986 static struct rq *task_rq_lock(struct task_struct *p, unsigned long *flags)
992 while (task_is_waking(p))
994 local_irq_save(*flags);
996 raw_spin_lock(&rq->lock);
997 if (likely(rq == task_rq(p) && !task_is_waking(p)))
999 raw_spin_unlock_irqrestore(&rq->lock, *flags);
1003 void task_rq_unlock_wait(struct task_struct *p)
1005 struct rq *rq = task_rq(p);
1007 smp_mb(); /* spin-unlock-wait is not a full memory barrier */
1008 raw_spin_unlock_wait(&rq->lock);
1011 static void __task_rq_unlock(struct rq *rq)
1012 __releases(rq->lock)
1014 raw_spin_unlock(&rq->lock);
1017 static inline void task_rq_unlock(struct rq *rq, unsigned long *flags)
1018 __releases(rq->lock)
1020 raw_spin_unlock_irqrestore(&rq->lock, *flags);
1024 * this_rq_lock - lock this runqueue and disable interrupts.
1026 static struct rq *this_rq_lock(void)
1027 __acquires(rq->lock)
1031 local_irq_disable();
1033 raw_spin_lock(&rq->lock);
1038 #ifdef CONFIG_SCHED_HRTICK
1040 * Use HR-timers to deliver accurate preemption points.
1042 * Its all a bit involved since we cannot program an hrt while holding the
1043 * rq->lock. So what we do is store a state in in rq->hrtick_* and ask for a
1046 * When we get rescheduled we reprogram the hrtick_timer outside of the
1052 * - enabled by features
1053 * - hrtimer is actually high res
1055 static inline int hrtick_enabled(struct rq *rq)
1057 if (!sched_feat(HRTICK))
1059 if (!cpu_active(cpu_of(rq)))
1061 return hrtimer_is_hres_active(&rq->hrtick_timer);
1064 static void hrtick_clear(struct rq *rq)
1066 if (hrtimer_active(&rq->hrtick_timer))
1067 hrtimer_cancel(&rq->hrtick_timer);
1071 * High-resolution timer tick.
1072 * Runs from hardirq context with interrupts disabled.
1074 static enum hrtimer_restart hrtick(struct hrtimer *timer)
1076 struct rq *rq = container_of(timer, struct rq, hrtick_timer);
1078 WARN_ON_ONCE(cpu_of(rq) != smp_processor_id());
1080 raw_spin_lock(&rq->lock);
1081 update_rq_clock(rq);
1082 rq->curr->sched_class->task_tick(rq, rq->curr, 1);
1083 raw_spin_unlock(&rq->lock);
1085 return HRTIMER_NORESTART;
1090 * called from hardirq (IPI) context
1092 static void __hrtick_start(void *arg)
1094 struct rq *rq = arg;
1096 raw_spin_lock(&rq->lock);
1097 hrtimer_restart(&rq->hrtick_timer);
1098 rq->hrtick_csd_pending = 0;
1099 raw_spin_unlock(&rq->lock);
1103 * Called to set the hrtick timer state.
1105 * called with rq->lock held and irqs disabled
1107 static void hrtick_start(struct rq *rq, u64 delay)
1109 struct hrtimer *timer = &rq->hrtick_timer;
1110 ktime_t time = ktime_add_ns(timer->base->get_time(), delay);
1112 hrtimer_set_expires(timer, time);
1114 if (rq == this_rq()) {
1115 hrtimer_restart(timer);
1116 } else if (!rq->hrtick_csd_pending) {
1117 __smp_call_function_single(cpu_of(rq), &rq->hrtick_csd, 0);
1118 rq->hrtick_csd_pending = 1;
1123 hotplug_hrtick(struct notifier_block *nfb, unsigned long action, void *hcpu)
1125 int cpu = (int)(long)hcpu;
1128 case CPU_UP_CANCELED:
1129 case CPU_UP_CANCELED_FROZEN:
1130 case CPU_DOWN_PREPARE:
1131 case CPU_DOWN_PREPARE_FROZEN:
1133 case CPU_DEAD_FROZEN:
1134 hrtick_clear(cpu_rq(cpu));
1141 static __init void init_hrtick(void)
1143 hotcpu_notifier(hotplug_hrtick, 0);
1147 * Called to set the hrtick timer state.
1149 * called with rq->lock held and irqs disabled
1151 static void hrtick_start(struct rq *rq, u64 delay)
1153 __hrtimer_start_range_ns(&rq->hrtick_timer, ns_to_ktime(delay), 0,
1154 HRTIMER_MODE_REL_PINNED, 0);
1157 static inline void init_hrtick(void)
1160 #endif /* CONFIG_SMP */
1162 static void init_rq_hrtick(struct rq *rq)
1165 rq->hrtick_csd_pending = 0;
1167 rq->hrtick_csd.flags = 0;
1168 rq->hrtick_csd.func = __hrtick_start;
1169 rq->hrtick_csd.info = rq;
1172 hrtimer_init(&rq->hrtick_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
1173 rq->hrtick_timer.function = hrtick;
1175 #else /* CONFIG_SCHED_HRTICK */
1176 static inline void hrtick_clear(struct rq *rq)
1180 static inline void init_rq_hrtick(struct rq *rq)
1184 static inline void init_hrtick(void)
1187 #endif /* CONFIG_SCHED_HRTICK */
1190 * resched_task - mark a task 'to be rescheduled now'.
1192 * On UP this means the setting of the need_resched flag, on SMP it
1193 * might also involve a cross-CPU call to trigger the scheduler on
1198 #ifndef tsk_is_polling
1199 #define tsk_is_polling(t) test_tsk_thread_flag(t, TIF_POLLING_NRFLAG)
1202 static void resched_task(struct task_struct *p)
1206 assert_raw_spin_locked(&task_rq(p)->lock);
1208 if (test_tsk_need_resched(p))
1211 set_tsk_need_resched(p);
1214 if (cpu == smp_processor_id())
1217 /* NEED_RESCHED must be visible before we test polling */
1219 if (!tsk_is_polling(p))
1220 smp_send_reschedule(cpu);
1223 static void resched_cpu(int cpu)
1225 struct rq *rq = cpu_rq(cpu);
1226 unsigned long flags;
1228 if (!raw_spin_trylock_irqsave(&rq->lock, flags))
1230 resched_task(cpu_curr(cpu));
1231 raw_spin_unlock_irqrestore(&rq->lock, flags);
1236 * When add_timer_on() enqueues a timer into the timer wheel of an
1237 * idle CPU then this timer might expire before the next timer event
1238 * which is scheduled to wake up that CPU. In case of a completely
1239 * idle system the next event might even be infinite time into the
1240 * future. wake_up_idle_cpu() ensures that the CPU is woken up and
1241 * leaves the inner idle loop so the newly added timer is taken into
1242 * account when the CPU goes back to idle and evaluates the timer
1243 * wheel for the next timer event.
1245 void wake_up_idle_cpu(int cpu)
1247 struct rq *rq = cpu_rq(cpu);
1249 if (cpu == smp_processor_id())
1253 * This is safe, as this function is called with the timer
1254 * wheel base lock of (cpu) held. When the CPU is on the way
1255 * to idle and has not yet set rq->curr to idle then it will
1256 * be serialized on the timer wheel base lock and take the new
1257 * timer into account automatically.
1259 if (rq->curr != rq->idle)
1263 * We can set TIF_RESCHED on the idle task of the other CPU
1264 * lockless. The worst case is that the other CPU runs the
1265 * idle task through an additional NOOP schedule()
1267 set_tsk_need_resched(rq->idle);
1269 /* NEED_RESCHED must be visible before we test polling */
1271 if (!tsk_is_polling(rq->idle))
1272 smp_send_reschedule(cpu);
1274 #endif /* CONFIG_NO_HZ */
1276 static u64 sched_avg_period(void)
1278 return (u64)sysctl_sched_time_avg * NSEC_PER_MSEC / 2;
1281 static void sched_avg_update(struct rq *rq)
1283 s64 period = sched_avg_period();
1285 while ((s64)(rq->clock - rq->age_stamp) > period) {
1286 rq->age_stamp += period;
1291 static void sched_rt_avg_update(struct rq *rq, u64 rt_delta)
1293 rq->rt_avg += rt_delta;
1294 sched_avg_update(rq);
1297 #else /* !CONFIG_SMP */
1298 static void resched_task(struct task_struct *p)
1300 assert_raw_spin_locked(&task_rq(p)->lock);
1301 set_tsk_need_resched(p);
1304 static void sched_rt_avg_update(struct rq *rq, u64 rt_delta)
1307 #endif /* CONFIG_SMP */
1309 #if BITS_PER_LONG == 32
1310 # define WMULT_CONST (~0UL)
1312 # define WMULT_CONST (1UL << 32)
1315 #define WMULT_SHIFT 32
1318 * Shift right and round:
1320 #define SRR(x, y) (((x) + (1UL << ((y) - 1))) >> (y))
1323 * delta *= weight / lw
1325 static unsigned long
1326 calc_delta_mine(unsigned long delta_exec, unsigned long weight,
1327 struct load_weight *lw)
1331 if (!lw->inv_weight) {
1332 if (BITS_PER_LONG > 32 && unlikely(lw->weight >= WMULT_CONST))
1335 lw->inv_weight = 1 + (WMULT_CONST-lw->weight/2)
1339 tmp = (u64)delta_exec * weight;
1341 * Check whether we'd overflow the 64-bit multiplication:
1343 if (unlikely(tmp > WMULT_CONST))
1344 tmp = SRR(SRR(tmp, WMULT_SHIFT/2) * lw->inv_weight,
1347 tmp = SRR(tmp * lw->inv_weight, WMULT_SHIFT);
1349 return (unsigned long)min(tmp, (u64)(unsigned long)LONG_MAX);
1352 static inline void update_load_add(struct load_weight *lw, unsigned long inc)
1358 static inline void update_load_sub(struct load_weight *lw, unsigned long dec)
1365 * To aid in avoiding the subversion of "niceness" due to uneven distribution
1366 * of tasks with abnormal "nice" values across CPUs the contribution that
1367 * each task makes to its run queue's load is weighted according to its
1368 * scheduling class and "nice" value. For SCHED_NORMAL tasks this is just a
1369 * scaled version of the new time slice allocation that they receive on time
1373 #define WEIGHT_IDLEPRIO 3
1374 #define WMULT_IDLEPRIO 1431655765
1377 * Nice levels are multiplicative, with a gentle 10% change for every
1378 * nice level changed. I.e. when a CPU-bound task goes from nice 0 to
1379 * nice 1, it will get ~10% less CPU time than another CPU-bound task
1380 * that remained on nice 0.
1382 * The "10% effect" is relative and cumulative: from _any_ nice level,
1383 * if you go up 1 level, it's -10% CPU usage, if you go down 1 level
1384 * it's +10% CPU usage. (to achieve that we use a multiplier of 1.25.
1385 * If a task goes up by ~10% and another task goes down by ~10% then
1386 * the relative distance between them is ~25%.)
1388 static const int prio_to_weight[40] = {
1389 /* -20 */ 88761, 71755, 56483, 46273, 36291,
1390 /* -15 */ 29154, 23254, 18705, 14949, 11916,
1391 /* -10 */ 9548, 7620, 6100, 4904, 3906,
1392 /* -5 */ 3121, 2501, 1991, 1586, 1277,
1393 /* 0 */ 1024, 820, 655, 526, 423,
1394 /* 5 */ 335, 272, 215, 172, 137,
1395 /* 10 */ 110, 87, 70, 56, 45,
1396 /* 15 */ 36, 29, 23, 18, 15,
1400 * Inverse (2^32/x) values of the prio_to_weight[] array, precalculated.
1402 * In cases where the weight does not change often, we can use the
1403 * precalculated inverse to speed up arithmetics by turning divisions
1404 * into multiplications:
1406 static const u32 prio_to_wmult[40] = {
1407 /* -20 */ 48388, 59856, 76040, 92818, 118348,
1408 /* -15 */ 147320, 184698, 229616, 287308, 360437,
1409 /* -10 */ 449829, 563644, 704093, 875809, 1099582,
1410 /* -5 */ 1376151, 1717300, 2157191, 2708050, 3363326,
1411 /* 0 */ 4194304, 5237765, 6557202, 8165337, 10153587,
1412 /* 5 */ 12820798, 15790321, 19976592, 24970740, 31350126,
1413 /* 10 */ 39045157, 49367440, 61356676, 76695844, 95443717,
1414 /* 15 */ 119304647, 148102320, 186737708, 238609294, 286331153,
1417 static void activate_task(struct rq *rq, struct task_struct *p, int wakeup);
1420 * runqueue iterator, to support SMP load-balancing between different
1421 * scheduling classes, without having to expose their internal data
1422 * structures to the load-balancing proper:
1424 struct rq_iterator {
1426 struct task_struct *(*start)(void *);
1427 struct task_struct *(*next)(void *);
1431 static unsigned long
1432 balance_tasks(struct rq *this_rq, int this_cpu, struct rq *busiest,
1433 unsigned long max_load_move, struct sched_domain *sd,
1434 enum cpu_idle_type idle, int *all_pinned,
1435 int *this_best_prio, struct rq_iterator *iterator);
1438 iter_move_one_task(struct rq *this_rq, int this_cpu, struct rq *busiest,
1439 struct sched_domain *sd, enum cpu_idle_type idle,
1440 struct rq_iterator *iterator);
1443 /* Time spent by the tasks of the cpu accounting group executing in ... */
1444 enum cpuacct_stat_index {
1445 CPUACCT_STAT_USER, /* ... user mode */
1446 CPUACCT_STAT_SYSTEM, /* ... kernel mode */
1448 CPUACCT_STAT_NSTATS,
1451 #ifdef CONFIG_CGROUP_CPUACCT
1452 static void cpuacct_charge(struct task_struct *tsk, u64 cputime);
1453 static void cpuacct_update_stats(struct task_struct *tsk,
1454 enum cpuacct_stat_index idx, cputime_t val);
1456 static inline void cpuacct_charge(struct task_struct *tsk, u64 cputime) {}
1457 static inline void cpuacct_update_stats(struct task_struct *tsk,
1458 enum cpuacct_stat_index idx, cputime_t val) {}
1461 static inline void inc_cpu_load(struct rq *rq, unsigned long load)
1463 update_load_add(&rq->load, load);
1466 static inline void dec_cpu_load(struct rq *rq, unsigned long load)
1468 update_load_sub(&rq->load, load);
1471 #if (defined(CONFIG_SMP) && defined(CONFIG_FAIR_GROUP_SCHED)) || defined(CONFIG_RT_GROUP_SCHED)
1472 typedef int (*tg_visitor)(struct task_group *, void *);
1475 * Iterate the full tree, calling @down when first entering a node and @up when
1476 * leaving it for the final time.
1478 static int walk_tg_tree(tg_visitor down, tg_visitor up, void *data)
1480 struct task_group *parent, *child;
1484 parent = &root_task_group;
1486 ret = (*down)(parent, data);
1489 list_for_each_entry_rcu(child, &parent->children, siblings) {
1496 ret = (*up)(parent, data);
1501 parent = parent->parent;
1510 static int tg_nop(struct task_group *tg, void *data)
1517 /* Used instead of source_load when we know the type == 0 */
1518 static unsigned long weighted_cpuload(const int cpu)
1520 return cpu_rq(cpu)->load.weight;
1524 * Return a low guess at the load of a migration-source cpu weighted
1525 * according to the scheduling class and "nice" value.
1527 * We want to under-estimate the load of migration sources, to
1528 * balance conservatively.
1530 static unsigned long source_load(int cpu, int type)
1532 struct rq *rq = cpu_rq(cpu);
1533 unsigned long total = weighted_cpuload(cpu);
1535 if (type == 0 || !sched_feat(LB_BIAS))
1538 return min(rq->cpu_load[type-1], total);
1542 * Return a high guess at the load of a migration-target cpu weighted
1543 * according to the scheduling class and "nice" value.
1545 static unsigned long target_load(int cpu, int type)
1547 struct rq *rq = cpu_rq(cpu);
1548 unsigned long total = weighted_cpuload(cpu);
1550 if (type == 0 || !sched_feat(LB_BIAS))
1553 return max(rq->cpu_load[type-1], total);
1556 static struct sched_group *group_of(int cpu)
1558 struct sched_domain *sd = rcu_dereference_sched(cpu_rq(cpu)->sd);
1566 static unsigned long power_of(int cpu)
1568 struct sched_group *group = group_of(cpu);
1571 return SCHED_LOAD_SCALE;
1573 return group->cpu_power;
1576 static int task_hot(struct task_struct *p, u64 now, struct sched_domain *sd);
1578 static unsigned long cpu_avg_load_per_task(int cpu)
1580 struct rq *rq = cpu_rq(cpu);
1581 unsigned long nr_running = ACCESS_ONCE(rq->nr_running);
1584 rq->avg_load_per_task = rq->load.weight / nr_running;
1586 rq->avg_load_per_task = 0;
1588 return rq->avg_load_per_task;
1591 #ifdef CONFIG_FAIR_GROUP_SCHED
1593 static __read_mostly unsigned long *update_shares_data;
1595 static void __set_se_shares(struct sched_entity *se, unsigned long shares);
1598 * Calculate and set the cpu's group shares.
1600 static void update_group_shares_cpu(struct task_group *tg, int cpu,
1601 unsigned long sd_shares,
1602 unsigned long sd_rq_weight,
1603 unsigned long *usd_rq_weight)
1605 unsigned long shares, rq_weight;
1608 rq_weight = usd_rq_weight[cpu];
1611 rq_weight = NICE_0_LOAD;
1615 * \Sum_j shares_j * rq_weight_i
1616 * shares_i = -----------------------------
1617 * \Sum_j rq_weight_j
1619 shares = (sd_shares * rq_weight) / sd_rq_weight;
1620 shares = clamp_t(unsigned long, shares, MIN_SHARES, MAX_SHARES);
1622 if (abs(shares - tg->se[cpu]->load.weight) >
1623 sysctl_sched_shares_thresh) {
1624 struct rq *rq = cpu_rq(cpu);
1625 unsigned long flags;
1627 raw_spin_lock_irqsave(&rq->lock, flags);
1628 tg->cfs_rq[cpu]->rq_weight = boost ? 0 : rq_weight;
1629 tg->cfs_rq[cpu]->shares = boost ? 0 : shares;
1630 __set_se_shares(tg->se[cpu], shares);
1631 raw_spin_unlock_irqrestore(&rq->lock, flags);
1636 * Re-compute the task group their per cpu shares over the given domain.
1637 * This needs to be done in a bottom-up fashion because the rq weight of a
1638 * parent group depends on the shares of its child groups.
1640 static int tg_shares_up(struct task_group *tg, void *data)
1642 unsigned long weight, rq_weight = 0, sum_weight = 0, shares = 0;
1643 unsigned long *usd_rq_weight;
1644 struct sched_domain *sd = data;
1645 unsigned long flags;
1651 local_irq_save(flags);
1652 usd_rq_weight = per_cpu_ptr(update_shares_data, smp_processor_id());
1654 for_each_cpu(i, sched_domain_span(sd)) {
1655 weight = tg->cfs_rq[i]->load.weight;
1656 usd_rq_weight[i] = weight;
1658 rq_weight += weight;
1660 * If there are currently no tasks on the cpu pretend there
1661 * is one of average load so that when a new task gets to
1662 * run here it will not get delayed by group starvation.
1665 weight = NICE_0_LOAD;
1667 sum_weight += weight;
1668 shares += tg->cfs_rq[i]->shares;
1672 rq_weight = sum_weight;
1674 if ((!shares && rq_weight) || shares > tg->shares)
1675 shares = tg->shares;
1677 if (!sd->parent || !(sd->parent->flags & SD_LOAD_BALANCE))
1678 shares = tg->shares;
1680 for_each_cpu(i, sched_domain_span(sd))
1681 update_group_shares_cpu(tg, i, shares, rq_weight, usd_rq_weight);
1683 local_irq_restore(flags);
1689 * Compute the cpu's hierarchical load factor for each task group.
1690 * This needs to be done in a top-down fashion because the load of a child
1691 * group is a fraction of its parents load.
1693 static int tg_load_down(struct task_group *tg, void *data)
1696 long cpu = (long)data;
1699 load = cpu_rq(cpu)->load.weight;
1701 load = tg->parent->cfs_rq[cpu]->h_load;
1702 load *= tg->cfs_rq[cpu]->shares;
1703 load /= tg->parent->cfs_rq[cpu]->load.weight + 1;
1706 tg->cfs_rq[cpu]->h_load = load;
1711 static void update_shares(struct sched_domain *sd)
1716 if (root_task_group_empty())
1719 now = cpu_clock(raw_smp_processor_id());
1720 elapsed = now - sd->last_update;
1722 if (elapsed >= (s64)(u64)sysctl_sched_shares_ratelimit) {
1723 sd->last_update = now;
1724 walk_tg_tree(tg_nop, tg_shares_up, sd);
1728 static void update_shares_locked(struct rq *rq, struct sched_domain *sd)
1730 if (root_task_group_empty())
1733 raw_spin_unlock(&rq->lock);
1735 raw_spin_lock(&rq->lock);
1738 static void update_h_load(long cpu)
1740 if (root_task_group_empty())
1743 walk_tg_tree(tg_load_down, tg_nop, (void *)cpu);
1748 static inline void update_shares(struct sched_domain *sd)
1752 static inline void update_shares_locked(struct rq *rq, struct sched_domain *sd)
1758 #ifdef CONFIG_PREEMPT
1760 static void double_rq_lock(struct rq *rq1, struct rq *rq2);
1763 * fair double_lock_balance: Safely acquires both rq->locks in a fair
1764 * way at the expense of forcing extra atomic operations in all
1765 * invocations. This assures that the double_lock is acquired using the
1766 * same underlying policy as the spinlock_t on this architecture, which
1767 * reduces latency compared to the unfair variant below. However, it
1768 * also adds more overhead and therefore may reduce throughput.
1770 static inline int _double_lock_balance(struct rq *this_rq, struct rq *busiest)
1771 __releases(this_rq->lock)
1772 __acquires(busiest->lock)
1773 __acquires(this_rq->lock)
1775 raw_spin_unlock(&this_rq->lock);
1776 double_rq_lock(this_rq, busiest);
1783 * Unfair double_lock_balance: Optimizes throughput at the expense of
1784 * latency by eliminating extra atomic operations when the locks are
1785 * already in proper order on entry. This favors lower cpu-ids and will
1786 * grant the double lock to lower cpus over higher ids under contention,
1787 * regardless of entry order into the function.
1789 static int _double_lock_balance(struct rq *this_rq, struct rq *busiest)
1790 __releases(this_rq->lock)
1791 __acquires(busiest->lock)
1792 __acquires(this_rq->lock)
1796 if (unlikely(!raw_spin_trylock(&busiest->lock))) {
1797 if (busiest < this_rq) {
1798 raw_spin_unlock(&this_rq->lock);
1799 raw_spin_lock(&busiest->lock);
1800 raw_spin_lock_nested(&this_rq->lock,
1801 SINGLE_DEPTH_NESTING);
1804 raw_spin_lock_nested(&busiest->lock,
1805 SINGLE_DEPTH_NESTING);
1810 #endif /* CONFIG_PREEMPT */
1813 * double_lock_balance - lock the busiest runqueue, this_rq is locked already.
1815 static int double_lock_balance(struct rq *this_rq, struct rq *busiest)
1817 if (unlikely(!irqs_disabled())) {
1818 /* printk() doesn't work good under rq->lock */
1819 raw_spin_unlock(&this_rq->lock);
1823 return _double_lock_balance(this_rq, busiest);
1826 static inline void double_unlock_balance(struct rq *this_rq, struct rq *busiest)
1827 __releases(busiest->lock)
1829 raw_spin_unlock(&busiest->lock);
1830 lock_set_subclass(&this_rq->lock.dep_map, 0, _RET_IP_);
1834 #ifdef CONFIG_FAIR_GROUP_SCHED
1835 static void cfs_rq_set_shares(struct cfs_rq *cfs_rq, unsigned long shares)
1838 cfs_rq->shares = shares;
1843 static void calc_load_account_active(struct rq *this_rq);
1844 static void update_sysctl(void);
1845 static int get_update_sysctl_factor(void);
1847 static inline void __set_task_cpu(struct task_struct *p, unsigned int cpu)
1849 set_task_rq(p, cpu);
1852 * After ->cpu is set up to a new value, task_rq_lock(p, ...) can be
1853 * successfuly executed on another CPU. We must ensure that updates of
1854 * per-task data have been completed by this moment.
1857 task_thread_info(p)->cpu = cpu;
1861 #include "sched_stats.h"
1862 #include "sched_idletask.c"
1863 #include "sched_fair.c"
1864 #include "sched_rt.c"
1865 #ifdef CONFIG_SCHED_DEBUG
1866 # include "sched_debug.c"
1869 #define sched_class_highest (&rt_sched_class)
1870 #define for_each_class(class) \
1871 for (class = sched_class_highest; class; class = class->next)
1873 static void inc_nr_running(struct rq *rq)
1878 static void dec_nr_running(struct rq *rq)
1883 static void set_load_weight(struct task_struct *p)
1885 if (task_has_rt_policy(p)) {
1886 p->se.load.weight = prio_to_weight[0] * 2;
1887 p->se.load.inv_weight = prio_to_wmult[0] >> 1;
1892 * SCHED_IDLE tasks get minimal weight:
1894 if (p->policy == SCHED_IDLE) {
1895 p->se.load.weight = WEIGHT_IDLEPRIO;
1896 p->se.load.inv_weight = WMULT_IDLEPRIO;
1900 p->se.load.weight = prio_to_weight[p->static_prio - MAX_RT_PRIO];
1901 p->se.load.inv_weight = prio_to_wmult[p->static_prio - MAX_RT_PRIO];
1904 static void update_avg(u64 *avg, u64 sample)
1906 s64 diff = sample - *avg;
1910 static void enqueue_task(struct rq *rq, struct task_struct *p, int wakeup)
1913 p->se.start_runtime = p->se.sum_exec_runtime;
1915 sched_info_queued(p);
1916 p->sched_class->enqueue_task(rq, p, wakeup);
1920 static void dequeue_task(struct rq *rq, struct task_struct *p, int sleep)
1923 if (p->se.last_wakeup) {
1924 update_avg(&p->se.avg_overlap,
1925 p->se.sum_exec_runtime - p->se.last_wakeup);
1926 p->se.last_wakeup = 0;
1928 update_avg(&p->se.avg_wakeup,
1929 sysctl_sched_wakeup_granularity);
1933 sched_info_dequeued(p);
1934 p->sched_class->dequeue_task(rq, p, sleep);
1939 * __normal_prio - return the priority that is based on the static prio
1941 static inline int __normal_prio(struct task_struct *p)
1943 return p->static_prio;
1947 * Calculate the expected normal priority: i.e. priority
1948 * without taking RT-inheritance into account. Might be
1949 * boosted by interactivity modifiers. Changes upon fork,
1950 * setprio syscalls, and whenever the interactivity
1951 * estimator recalculates.
1953 static inline int normal_prio(struct task_struct *p)
1957 if (task_has_rt_policy(p))
1958 prio = MAX_RT_PRIO-1 - p->rt_priority;
1960 prio = __normal_prio(p);
1965 * Calculate the current priority, i.e. the priority
1966 * taken into account by the scheduler. This value might
1967 * be boosted by RT tasks, or might be boosted by
1968 * interactivity modifiers. Will be RT if the task got
1969 * RT-boosted. If not then it returns p->normal_prio.
1971 static int effective_prio(struct task_struct *p)
1973 p->normal_prio = normal_prio(p);
1975 * If we are RT tasks or we were boosted to RT priority,
1976 * keep the priority unchanged. Otherwise, update priority
1977 * to the normal priority:
1979 if (!rt_prio(p->prio))
1980 return p->normal_prio;
1985 * activate_task - move a task to the runqueue.
1987 static void activate_task(struct rq *rq, struct task_struct *p, int wakeup)
1989 if (task_contributes_to_load(p))
1990 rq->nr_uninterruptible--;
1992 enqueue_task(rq, p, wakeup);
1997 * deactivate_task - remove a task from the runqueue.
1999 static void deactivate_task(struct rq *rq, struct task_struct *p, int sleep)
2001 if (task_contributes_to_load(p))
2002 rq->nr_uninterruptible++;
2004 dequeue_task(rq, p, sleep);
2009 * task_curr - is this task currently executing on a CPU?
2010 * @p: the task in question.
2012 inline int task_curr(const struct task_struct *p)
2014 return cpu_curr(task_cpu(p)) == p;
2017 static inline void check_class_changed(struct rq *rq, struct task_struct *p,
2018 const struct sched_class *prev_class,
2019 int oldprio, int running)
2021 if (prev_class != p->sched_class) {
2022 if (prev_class->switched_from)
2023 prev_class->switched_from(rq, p, running);
2024 p->sched_class->switched_to(rq, p, running);
2026 p->sched_class->prio_changed(rq, p, oldprio, running);
2031 * Is this task likely cache-hot:
2034 task_hot(struct task_struct *p, u64 now, struct sched_domain *sd)
2038 if (p->sched_class != &fair_sched_class)
2042 * Buddy candidates are cache hot:
2044 if (sched_feat(CACHE_HOT_BUDDY) && this_rq()->nr_running &&
2045 (&p->se == cfs_rq_of(&p->se)->next ||
2046 &p->se == cfs_rq_of(&p->se)->last))
2049 if (sysctl_sched_migration_cost == -1)
2051 if (sysctl_sched_migration_cost == 0)
2054 delta = now - p->se.exec_start;
2056 return delta < (s64)sysctl_sched_migration_cost;
2059 void set_task_cpu(struct task_struct *p, unsigned int new_cpu)
2061 #ifdef CONFIG_SCHED_DEBUG
2063 * We should never call set_task_cpu() on a blocked task,
2064 * ttwu() will sort out the placement.
2066 WARN_ON_ONCE(p->state != TASK_RUNNING && p->state != TASK_WAKING &&
2067 !(task_thread_info(p)->preempt_count & PREEMPT_ACTIVE));
2070 trace_sched_migrate_task(p, new_cpu);
2072 if (task_cpu(p) != new_cpu) {
2073 p->se.nr_migrations++;
2074 perf_sw_event(PERF_COUNT_SW_CPU_MIGRATIONS, 1, 1, NULL, 0);
2077 __set_task_cpu(p, new_cpu);
2080 struct migration_req {
2081 struct list_head list;
2083 struct task_struct *task;
2086 struct completion done;
2090 * The task's runqueue lock must be held.
2091 * Returns true if you have to wait for migration thread.
2094 migrate_task(struct task_struct *p, int dest_cpu, struct migration_req *req)
2096 struct rq *rq = task_rq(p);
2099 * If the task is not on a runqueue (and not running), then
2100 * the next wake-up will properly place the task.
2102 if (!p->se.on_rq && !task_running(rq, p))
2105 init_completion(&req->done);
2107 req->dest_cpu = dest_cpu;
2108 list_add(&req->list, &rq->migration_queue);
2114 * wait_task_context_switch - wait for a thread to complete at least one
2117 * @p must not be current.
2119 void wait_task_context_switch(struct task_struct *p)
2121 unsigned long nvcsw, nivcsw, flags;
2129 * The runqueue is assigned before the actual context
2130 * switch. We need to take the runqueue lock.
2132 * We could check initially without the lock but it is
2133 * very likely that we need to take the lock in every
2136 rq = task_rq_lock(p, &flags);
2137 running = task_running(rq, p);
2138 task_rq_unlock(rq, &flags);
2140 if (likely(!running))
2143 * The switch count is incremented before the actual
2144 * context switch. We thus wait for two switches to be
2145 * sure at least one completed.
2147 if ((p->nvcsw - nvcsw) > 1)
2149 if ((p->nivcsw - nivcsw) > 1)
2157 * wait_task_inactive - wait for a thread to unschedule.
2159 * If @match_state is nonzero, it's the @p->state value just checked and
2160 * not expected to change. If it changes, i.e. @p might have woken up,
2161 * then return zero. When we succeed in waiting for @p to be off its CPU,
2162 * we return a positive number (its total switch count). If a second call
2163 * a short while later returns the same number, the caller can be sure that
2164 * @p has remained unscheduled the whole time.
2166 * The caller must ensure that the task *will* unschedule sometime soon,
2167 * else this function might spin for a *long* time. This function can't
2168 * be called with interrupts off, or it may introduce deadlock with
2169 * smp_call_function() if an IPI is sent by the same process we are
2170 * waiting to become inactive.
2172 unsigned long wait_task_inactive(struct task_struct *p, long match_state)
2174 unsigned long flags;
2181 * We do the initial early heuristics without holding
2182 * any task-queue locks at all. We'll only try to get
2183 * the runqueue lock when things look like they will
2189 * If the task is actively running on another CPU
2190 * still, just relax and busy-wait without holding
2193 * NOTE! Since we don't hold any locks, it's not
2194 * even sure that "rq" stays as the right runqueue!
2195 * But we don't care, since "task_running()" will
2196 * return false if the runqueue has changed and p
2197 * is actually now running somewhere else!
2199 while (task_running(rq, p)) {
2200 if (match_state && unlikely(p->state != match_state))
2206 * Ok, time to look more closely! We need the rq
2207 * lock now, to be *sure*. If we're wrong, we'll
2208 * just go back and repeat.
2210 rq = task_rq_lock(p, &flags);
2211 trace_sched_wait_task(rq, p);
2212 running = task_running(rq, p);
2213 on_rq = p->se.on_rq;
2215 if (!match_state || p->state == match_state)
2216 ncsw = p->nvcsw | LONG_MIN; /* sets MSB */
2217 task_rq_unlock(rq, &flags);
2220 * If it changed from the expected state, bail out now.
2222 if (unlikely(!ncsw))
2226 * Was it really running after all now that we
2227 * checked with the proper locks actually held?
2229 * Oops. Go back and try again..
2231 if (unlikely(running)) {
2237 * It's not enough that it's not actively running,
2238 * it must be off the runqueue _entirely_, and not
2241 * So if it was still runnable (but just not actively
2242 * running right now), it's preempted, and we should
2243 * yield - it could be a while.
2245 if (unlikely(on_rq)) {
2246 schedule_timeout_uninterruptible(1);
2251 * Ahh, all good. It wasn't running, and it wasn't
2252 * runnable, which means that it will never become
2253 * running in the future either. We're all done!
2262 * kick_process - kick a running thread to enter/exit the kernel
2263 * @p: the to-be-kicked thread
2265 * Cause a process which is running on another CPU to enter
2266 * kernel-mode, without any delay. (to get signals handled.)
2268 * NOTE: this function doesnt have to take the runqueue lock,
2269 * because all it wants to ensure is that the remote task enters
2270 * the kernel. If the IPI races and the task has been migrated
2271 * to another CPU then no harm is done and the purpose has been
2274 void kick_process(struct task_struct *p)
2280 if ((cpu != smp_processor_id()) && task_curr(p))
2281 smp_send_reschedule(cpu);
2284 EXPORT_SYMBOL_GPL(kick_process);
2285 #endif /* CONFIG_SMP */
2288 * task_oncpu_function_call - call a function on the cpu on which a task runs
2289 * @p: the task to evaluate
2290 * @func: the function to be called
2291 * @info: the function call argument
2293 * Calls the function @func when the task is currently running. This might
2294 * be on the current CPU, which just calls the function directly
2296 void task_oncpu_function_call(struct task_struct *p,
2297 void (*func) (void *info), void *info)
2304 smp_call_function_single(cpu, func, info, 1);
2309 static int select_fallback_rq(int cpu, struct task_struct *p)
2312 const struct cpumask *nodemask = cpumask_of_node(cpu_to_node(cpu));
2314 /* Look for allowed, online CPU in same node. */
2315 for_each_cpu_and(dest_cpu, nodemask, cpu_active_mask)
2316 if (cpumask_test_cpu(dest_cpu, &p->cpus_allowed))
2319 /* Any allowed, online CPU? */
2320 dest_cpu = cpumask_any_and(&p->cpus_allowed, cpu_active_mask);
2321 if (dest_cpu < nr_cpu_ids)
2324 /* No more Mr. Nice Guy. */
2325 if (dest_cpu >= nr_cpu_ids) {
2327 cpuset_cpus_allowed_locked(p, &p->cpus_allowed);
2329 dest_cpu = cpumask_any_and(cpu_active_mask, &p->cpus_allowed);
2332 * Don't tell them about moving exiting tasks or
2333 * kernel threads (both mm NULL), since they never
2336 if (p->mm && printk_ratelimit()) {
2337 printk(KERN_INFO "process %d (%s) no "
2338 "longer affine to cpu%d\n",
2339 task_pid_nr(p), p->comm, cpu);
2347 * Gets called from 3 sites (exec, fork, wakeup), since it is called without
2348 * holding rq->lock we need to ensure ->cpus_allowed is stable, this is done
2351 * exec: is unstable, retry loop
2352 * fork & wake-up: serialize ->cpus_allowed against TASK_WAKING
2355 int select_task_rq(struct task_struct *p, int sd_flags, int wake_flags)
2357 int cpu = p->sched_class->select_task_rq(p, sd_flags, wake_flags);
2360 * In order not to call set_task_cpu() on a blocking task we need
2361 * to rely on ttwu() to place the task on a valid ->cpus_allowed
2364 * Since this is common to all placement strategies, this lives here.
2366 * [ this allows ->select_task() to simply return task_cpu(p) and
2367 * not worry about this generic constraint ]
2369 if (unlikely(!cpumask_test_cpu(cpu, &p->cpus_allowed) ||
2371 cpu = select_fallback_rq(task_cpu(p), p);
2378 * try_to_wake_up - wake up a thread
2379 * @p: the to-be-woken-up thread
2380 * @state: the mask of task states that can be woken
2381 * @sync: do a synchronous wakeup?
2383 * Put it on the run-queue if it's not already there. The "current"
2384 * thread is always on the run-queue (except when the actual
2385 * re-schedule is in progress), and as such you're allowed to do
2386 * the simpler "current->state = TASK_RUNNING" to mark yourself
2387 * runnable without the overhead of this.
2389 * returns failure only if the task is already active.
2391 static int try_to_wake_up(struct task_struct *p, unsigned int state,
2394 int cpu, orig_cpu, this_cpu, success = 0;
2395 unsigned long flags;
2396 struct rq *rq, *orig_rq;
2398 if (!sched_feat(SYNC_WAKEUPS))
2399 wake_flags &= ~WF_SYNC;
2401 this_cpu = get_cpu();
2404 rq = orig_rq = task_rq_lock(p, &flags);
2405 update_rq_clock(rq);
2406 if (!(p->state & state))
2416 if (unlikely(task_running(rq, p)))
2420 * In order to handle concurrent wakeups and release the rq->lock
2421 * we put the task in TASK_WAKING state.
2423 * First fix up the nr_uninterruptible count:
2425 if (task_contributes_to_load(p))
2426 rq->nr_uninterruptible--;
2427 p->state = TASK_WAKING;
2429 if (p->sched_class->task_waking)
2430 p->sched_class->task_waking(rq, p);
2432 __task_rq_unlock(rq);
2434 cpu = select_task_rq(p, SD_BALANCE_WAKE, wake_flags);
2435 if (cpu != orig_cpu) {
2437 * Since we migrate the task without holding any rq->lock,
2438 * we need to be careful with task_rq_lock(), since that
2439 * might end up locking an invalid rq.
2441 set_task_cpu(p, cpu);
2445 raw_spin_lock(&rq->lock);
2446 update_rq_clock(rq);
2449 * We migrated the task without holding either rq->lock, however
2450 * since the task is not on the task list itself, nobody else
2451 * will try and migrate the task, hence the rq should match the
2452 * cpu we just moved it to.
2454 WARN_ON(task_cpu(p) != cpu);
2455 WARN_ON(p->state != TASK_WAKING);
2457 #ifdef CONFIG_SCHEDSTATS
2458 schedstat_inc(rq, ttwu_count);
2459 if (cpu == this_cpu)
2460 schedstat_inc(rq, ttwu_local);
2462 struct sched_domain *sd;
2463 for_each_domain(this_cpu, sd) {
2464 if (cpumask_test_cpu(cpu, sched_domain_span(sd))) {
2465 schedstat_inc(sd, ttwu_wake_remote);
2470 #endif /* CONFIG_SCHEDSTATS */
2473 #endif /* CONFIG_SMP */
2474 schedstat_inc(p, se.nr_wakeups);
2475 if (wake_flags & WF_SYNC)
2476 schedstat_inc(p, se.nr_wakeups_sync);
2477 if (orig_cpu != cpu)
2478 schedstat_inc(p, se.nr_wakeups_migrate);
2479 if (cpu == this_cpu)
2480 schedstat_inc(p, se.nr_wakeups_local);
2482 schedstat_inc(p, se.nr_wakeups_remote);
2483 activate_task(rq, p, 1);
2487 * Only attribute actual wakeups done by this task.
2489 if (!in_interrupt()) {
2490 struct sched_entity *se = ¤t->se;
2491 u64 sample = se->sum_exec_runtime;
2493 if (se->last_wakeup)
2494 sample -= se->last_wakeup;
2496 sample -= se->start_runtime;
2497 update_avg(&se->avg_wakeup, sample);
2499 se->last_wakeup = se->sum_exec_runtime;
2503 trace_sched_wakeup(rq, p, success);
2504 check_preempt_curr(rq, p, wake_flags);
2506 p->state = TASK_RUNNING;
2508 if (p->sched_class->task_woken)
2509 p->sched_class->task_woken(rq, p);
2511 if (unlikely(rq->idle_stamp)) {
2512 u64 delta = rq->clock - rq->idle_stamp;
2513 u64 max = 2*sysctl_sched_migration_cost;
2518 update_avg(&rq->avg_idle, delta);
2523 task_rq_unlock(rq, &flags);
2530 * wake_up_process - Wake up a specific process
2531 * @p: The process to be woken up.
2533 * Attempt to wake up the nominated process and move it to the set of runnable
2534 * processes. Returns 1 if the process was woken up, 0 if it was already
2537 * It may be assumed that this function implies a write memory barrier before
2538 * changing the task state if and only if any tasks are woken up.
2540 int wake_up_process(struct task_struct *p)
2542 return try_to_wake_up(p, TASK_ALL, 0);
2544 EXPORT_SYMBOL(wake_up_process);
2546 int wake_up_state(struct task_struct *p, unsigned int state)
2548 return try_to_wake_up(p, state, 0);
2552 * Perform scheduler related setup for a newly forked process p.
2553 * p is forked by current.
2555 * __sched_fork() is basic setup used by init_idle() too:
2557 static void __sched_fork(struct task_struct *p)
2559 p->se.exec_start = 0;
2560 p->se.sum_exec_runtime = 0;
2561 p->se.prev_sum_exec_runtime = 0;
2562 p->se.nr_migrations = 0;
2563 p->se.last_wakeup = 0;
2564 p->se.avg_overlap = 0;
2565 p->se.start_runtime = 0;
2566 p->se.avg_wakeup = sysctl_sched_wakeup_granularity;
2568 #ifdef CONFIG_SCHEDSTATS
2569 p->se.wait_start = 0;
2571 p->se.wait_count = 0;
2574 p->se.sleep_start = 0;
2575 p->se.sleep_max = 0;
2576 p->se.sum_sleep_runtime = 0;
2578 p->se.block_start = 0;
2579 p->se.block_max = 0;
2581 p->se.slice_max = 0;
2583 p->se.nr_migrations_cold = 0;
2584 p->se.nr_failed_migrations_affine = 0;
2585 p->se.nr_failed_migrations_running = 0;
2586 p->se.nr_failed_migrations_hot = 0;
2587 p->se.nr_forced_migrations = 0;
2589 p->se.nr_wakeups = 0;
2590 p->se.nr_wakeups_sync = 0;
2591 p->se.nr_wakeups_migrate = 0;
2592 p->se.nr_wakeups_local = 0;
2593 p->se.nr_wakeups_remote = 0;
2594 p->se.nr_wakeups_affine = 0;
2595 p->se.nr_wakeups_affine_attempts = 0;
2596 p->se.nr_wakeups_passive = 0;
2597 p->se.nr_wakeups_idle = 0;
2601 INIT_LIST_HEAD(&p->rt.run_list);
2603 INIT_LIST_HEAD(&p->se.group_node);
2605 #ifdef CONFIG_PREEMPT_NOTIFIERS
2606 INIT_HLIST_HEAD(&p->preempt_notifiers);
2611 * fork()/clone()-time setup:
2613 void sched_fork(struct task_struct *p, int clone_flags)
2615 int cpu = get_cpu();
2619 * We mark the process as waking here. This guarantees that
2620 * nobody will actually run it, and a signal or other external
2621 * event cannot wake it up and insert it on the runqueue either.
2623 p->state = TASK_WAKING;
2626 * Revert to default priority/policy on fork if requested.
2628 if (unlikely(p->sched_reset_on_fork)) {
2629 if (p->policy == SCHED_FIFO || p->policy == SCHED_RR) {
2630 p->policy = SCHED_NORMAL;
2631 p->normal_prio = p->static_prio;
2634 if (PRIO_TO_NICE(p->static_prio) < 0) {
2635 p->static_prio = NICE_TO_PRIO(0);
2636 p->normal_prio = p->static_prio;
2641 * We don't need the reset flag anymore after the fork. It has
2642 * fulfilled its duty:
2644 p->sched_reset_on_fork = 0;
2648 * Make sure we do not leak PI boosting priority to the child.
2650 p->prio = current->normal_prio;
2652 if (!rt_prio(p->prio))
2653 p->sched_class = &fair_sched_class;
2655 if (p->sched_class->task_fork)
2656 p->sched_class->task_fork(p);
2658 set_task_cpu(p, cpu);
2660 #if defined(CONFIG_SCHEDSTATS) || defined(CONFIG_TASK_DELAY_ACCT)
2661 if (likely(sched_info_on()))
2662 memset(&p->sched_info, 0, sizeof(p->sched_info));
2664 #if defined(CONFIG_SMP) && defined(__ARCH_WANT_UNLOCKED_CTXSW)
2667 #ifdef CONFIG_PREEMPT
2668 /* Want to start with kernel preemption disabled. */
2669 task_thread_info(p)->preempt_count = 1;
2671 plist_node_init(&p->pushable_tasks, MAX_PRIO);
2677 * wake_up_new_task - wake up a newly created task for the first time.
2679 * This function will do some initial scheduler statistics housekeeping
2680 * that must be done for every newly created context, then puts the task
2681 * on the runqueue and wakes it.
2683 void wake_up_new_task(struct task_struct *p, unsigned long clone_flags)
2685 unsigned long flags;
2687 int cpu = get_cpu();
2691 * Fork balancing, do it here and not earlier because:
2692 * - cpus_allowed can change in the fork path
2693 * - any previously selected cpu might disappear through hotplug
2695 * We still have TASK_WAKING but PF_STARTING is gone now, meaning
2696 * ->cpus_allowed is stable, we have preemption disabled, meaning
2697 * cpu_online_mask is stable.
2699 cpu = select_task_rq(p, SD_BALANCE_FORK, 0);
2700 set_task_cpu(p, cpu);
2704 * Since the task is not on the rq and we still have TASK_WAKING set
2705 * nobody else will migrate this task.
2708 raw_spin_lock_irqsave(&rq->lock, flags);
2710 BUG_ON(p->state != TASK_WAKING);
2711 p->state = TASK_RUNNING;
2712 update_rq_clock(rq);
2713 activate_task(rq, p, 0);
2714 trace_sched_wakeup_new(rq, p, 1);
2715 check_preempt_curr(rq, p, WF_FORK);
2717 if (p->sched_class->task_woken)
2718 p->sched_class->task_woken(rq, p);
2720 task_rq_unlock(rq, &flags);
2724 #ifdef CONFIG_PREEMPT_NOTIFIERS
2727 * preempt_notifier_register - tell me when current is being preempted & rescheduled
2728 * @notifier: notifier struct to register
2730 void preempt_notifier_register(struct preempt_notifier *notifier)
2732 hlist_add_head(¬ifier->link, ¤t->preempt_notifiers);
2734 EXPORT_SYMBOL_GPL(preempt_notifier_register);
2737 * preempt_notifier_unregister - no longer interested in preemption notifications
2738 * @notifier: notifier struct to unregister
2740 * This is safe to call from within a preemption notifier.
2742 void preempt_notifier_unregister(struct preempt_notifier *notifier)
2744 hlist_del(¬ifier->link);
2746 EXPORT_SYMBOL_GPL(preempt_notifier_unregister);
2748 static void fire_sched_in_preempt_notifiers(struct task_struct *curr)
2750 struct preempt_notifier *notifier;
2751 struct hlist_node *node;
2753 hlist_for_each_entry(notifier, node, &curr->preempt_notifiers, link)
2754 notifier->ops->sched_in(notifier, raw_smp_processor_id());
2758 fire_sched_out_preempt_notifiers(struct task_struct *curr,
2759 struct task_struct *next)
2761 struct preempt_notifier *notifier;
2762 struct hlist_node *node;
2764 hlist_for_each_entry(notifier, node, &curr->preempt_notifiers, link)
2765 notifier->ops->sched_out(notifier, next);
2768 #else /* !CONFIG_PREEMPT_NOTIFIERS */
2770 static void fire_sched_in_preempt_notifiers(struct task_struct *curr)
2775 fire_sched_out_preempt_notifiers(struct task_struct *curr,
2776 struct task_struct *next)
2780 #endif /* CONFIG_PREEMPT_NOTIFIERS */
2783 * prepare_task_switch - prepare to switch tasks
2784 * @rq: the runqueue preparing to switch
2785 * @prev: the current task that is being switched out
2786 * @next: the task we are going to switch to.
2788 * This is called with the rq lock held and interrupts off. It must
2789 * be paired with a subsequent finish_task_switch after the context
2792 * prepare_task_switch sets up locking and calls architecture specific
2796 prepare_task_switch(struct rq *rq, struct task_struct *prev,
2797 struct task_struct *next)
2799 fire_sched_out_preempt_notifiers(prev, next);
2800 prepare_lock_switch(rq, next);
2801 prepare_arch_switch(next);
2805 * finish_task_switch - clean up after a task-switch
2806 * @rq: runqueue associated with task-switch
2807 * @prev: the thread we just switched away from.
2809 * finish_task_switch must be called after the context switch, paired
2810 * with a prepare_task_switch call before the context switch.
2811 * finish_task_switch will reconcile locking set up by prepare_task_switch,
2812 * and do any other architecture-specific cleanup actions.
2814 * Note that we may have delayed dropping an mm in context_switch(). If
2815 * so, we finish that here outside of the runqueue lock. (Doing it
2816 * with the lock held can cause deadlocks; see schedule() for
2819 static void finish_task_switch(struct rq *rq, struct task_struct *prev)
2820 __releases(rq->lock)
2822 struct mm_struct *mm = rq->prev_mm;
2828 * A task struct has one reference for the use as "current".
2829 * If a task dies, then it sets TASK_DEAD in tsk->state and calls
2830 * schedule one last time. The schedule call will never return, and
2831 * the scheduled task must drop that reference.
2832 * The test for TASK_DEAD must occur while the runqueue locks are
2833 * still held, otherwise prev could be scheduled on another cpu, die
2834 * there before we look at prev->state, and then the reference would
2836 * Manfred Spraul <manfred@colorfullife.com>
2838 prev_state = prev->state;
2839 finish_arch_switch(prev);
2840 #ifdef __ARCH_WANT_INTERRUPTS_ON_CTXSW
2841 local_irq_disable();
2842 #endif /* __ARCH_WANT_INTERRUPTS_ON_CTXSW */
2843 perf_event_task_sched_in(current);
2844 #ifdef __ARCH_WANT_INTERRUPTS_ON_CTXSW
2846 #endif /* __ARCH_WANT_INTERRUPTS_ON_CTXSW */
2847 finish_lock_switch(rq, prev);
2849 fire_sched_in_preempt_notifiers(current);
2852 if (unlikely(prev_state == TASK_DEAD)) {
2854 * Remove function-return probe instances associated with this
2855 * task and put them back on the free list.
2857 kprobe_flush_task(prev);
2858 put_task_struct(prev);
2864 /* assumes rq->lock is held */
2865 static inline void pre_schedule(struct rq *rq, struct task_struct *prev)
2867 if (prev->sched_class->pre_schedule)
2868 prev->sched_class->pre_schedule(rq, prev);
2871 /* rq->lock is NOT held, but preemption is disabled */
2872 static inline void post_schedule(struct rq *rq)
2874 if (rq->post_schedule) {
2875 unsigned long flags;
2877 raw_spin_lock_irqsave(&rq->lock, flags);
2878 if (rq->curr->sched_class->post_schedule)
2879 rq->curr->sched_class->post_schedule(rq);
2880 raw_spin_unlock_irqrestore(&rq->lock, flags);
2882 rq->post_schedule = 0;
2888 static inline void pre_schedule(struct rq *rq, struct task_struct *p)
2892 static inline void post_schedule(struct rq *rq)
2899 * schedule_tail - first thing a freshly forked thread must call.
2900 * @prev: the thread we just switched away from.
2902 asmlinkage void schedule_tail(struct task_struct *prev)
2903 __releases(rq->lock)
2905 struct rq *rq = this_rq();
2907 finish_task_switch(rq, prev);
2910 * FIXME: do we need to worry about rq being invalidated by the
2915 #ifdef __ARCH_WANT_UNLOCKED_CTXSW
2916 /* In this case, finish_task_switch does not reenable preemption */
2919 if (current->set_child_tid)
2920 put_user(task_pid_vnr(current), current->set_child_tid);
2924 * context_switch - switch to the new MM and the new
2925 * thread's register state.
2928 context_switch(struct rq *rq, struct task_struct *prev,
2929 struct task_struct *next)
2931 struct mm_struct *mm, *oldmm;
2933 prepare_task_switch(rq, prev, next);
2934 trace_sched_switch(rq, prev, next);
2936 oldmm = prev->active_mm;
2938 * For paravirt, this is coupled with an exit in switch_to to
2939 * combine the page table reload and the switch backend into
2942 arch_start_context_switch(prev);
2945 next->active_mm = oldmm;
2946 atomic_inc(&oldmm->mm_count);
2947 enter_lazy_tlb(oldmm, next);
2949 switch_mm(oldmm, mm, next);
2951 if (likely(!prev->mm)) {
2952 prev->active_mm = NULL;
2953 rq->prev_mm = oldmm;
2956 * Since the runqueue lock will be released by the next
2957 * task (which is an invalid locking op but in the case
2958 * of the scheduler it's an obvious special-case), so we
2959 * do an early lockdep release here:
2961 #ifndef __ARCH_WANT_UNLOCKED_CTXSW
2962 spin_release(&rq->lock.dep_map, 1, _THIS_IP_);
2965 /* Here we just switch the register state and the stack. */
2966 switch_to(prev, next, prev);
2970 * this_rq must be evaluated again because prev may have moved
2971 * CPUs since it called schedule(), thus the 'rq' on its stack
2972 * frame will be invalid.
2974 finish_task_switch(this_rq(), prev);
2978 * nr_running, nr_uninterruptible and nr_context_switches:
2980 * externally visible scheduler statistics: current number of runnable
2981 * threads, current number of uninterruptible-sleeping threads, total
2982 * number of context switches performed since bootup.
2984 unsigned long nr_running(void)
2986 unsigned long i, sum = 0;
2988 for_each_online_cpu(i)
2989 sum += cpu_rq(i)->nr_running;
2994 unsigned long nr_uninterruptible(void)
2996 unsigned long i, sum = 0;
2998 for_each_possible_cpu(i)
2999 sum += cpu_rq(i)->nr_uninterruptible;
3002 * Since we read the counters lockless, it might be slightly
3003 * inaccurate. Do not allow it to go below zero though:
3005 if (unlikely((long)sum < 0))
3011 unsigned long long nr_context_switches(void)
3014 unsigned long long sum = 0;
3016 for_each_possible_cpu(i)
3017 sum += cpu_rq(i)->nr_switches;
3022 unsigned long nr_iowait(void)
3024 unsigned long i, sum = 0;
3026 for_each_possible_cpu(i)
3027 sum += atomic_read(&cpu_rq(i)->nr_iowait);
3032 unsigned long nr_iowait_cpu(void)
3034 struct rq *this = this_rq();
3035 return atomic_read(&this->nr_iowait);
3038 unsigned long this_cpu_load(void)
3040 struct rq *this = this_rq();
3041 return this->cpu_load[0];
3045 /* Variables and functions for calc_load */
3046 static atomic_long_t calc_load_tasks;
3047 static unsigned long calc_load_update;
3048 unsigned long avenrun[3];
3049 EXPORT_SYMBOL(avenrun);
3052 * get_avenrun - get the load average array
3053 * @loads: pointer to dest load array
3054 * @offset: offset to add
3055 * @shift: shift count to shift the result left
3057 * These values are estimates at best, so no need for locking.
3059 void get_avenrun(unsigned long *loads, unsigned long offset, int shift)
3061 loads[0] = (avenrun[0] + offset) << shift;
3062 loads[1] = (avenrun[1] + offset) << shift;
3063 loads[2] = (avenrun[2] + offset) << shift;
3066 static unsigned long
3067 calc_load(unsigned long load, unsigned long exp, unsigned long active)
3070 load += active * (FIXED_1 - exp);
3071 return load >> FSHIFT;
3075 * calc_load - update the avenrun load estimates 10 ticks after the
3076 * CPUs have updated calc_load_tasks.
3078 void calc_global_load(void)
3080 unsigned long upd = calc_load_update + 10;
3083 if (time_before(jiffies, upd))
3086 active = atomic_long_read(&calc_load_tasks);
3087 active = active > 0 ? active * FIXED_1 : 0;
3089 avenrun[0] = calc_load(avenrun[0], EXP_1, active);
3090 avenrun[1] = calc_load(avenrun[1], EXP_5, active);
3091 avenrun[2] = calc_load(avenrun[2], EXP_15, active);
3093 calc_load_update += LOAD_FREQ;
3097 * Either called from update_cpu_load() or from a cpu going idle
3099 static void calc_load_account_active(struct rq *this_rq)
3101 long nr_active, delta;
3103 nr_active = this_rq->nr_running;
3104 nr_active += (long) this_rq->nr_uninterruptible;
3106 if (nr_active != this_rq->calc_load_active) {
3107 delta = nr_active - this_rq->calc_load_active;
3108 this_rq->calc_load_active = nr_active;
3109 atomic_long_add(delta, &calc_load_tasks);
3114 * Update rq->cpu_load[] statistics. This function is usually called every
3115 * scheduler tick (TICK_NSEC).
3117 static void update_cpu_load(struct rq *this_rq)
3119 unsigned long this_load = this_rq->load.weight;
3122 this_rq->nr_load_updates++;
3124 /* Update our load: */
3125 for (i = 0, scale = 1; i < CPU_LOAD_IDX_MAX; i++, scale += scale) {
3126 unsigned long old_load, new_load;
3128 /* scale is effectively 1 << i now, and >> i divides by scale */
3130 old_load = this_rq->cpu_load[i];
3131 new_load = this_load;
3133 * Round up the averaging division if load is increasing. This
3134 * prevents us from getting stuck on 9 if the load is 10, for
3137 if (new_load > old_load)
3138 new_load += scale-1;
3139 this_rq->cpu_load[i] = (old_load*(scale-1) + new_load) >> i;
3142 if (time_after_eq(jiffies, this_rq->calc_load_update)) {
3143 this_rq->calc_load_update += LOAD_FREQ;
3144 calc_load_account_active(this_rq);
3151 * double_rq_lock - safely lock two runqueues
3153 * Note this does not disable interrupts like task_rq_lock,
3154 * you need to do so manually before calling.
3156 static void double_rq_lock(struct rq *rq1, struct rq *rq2)
3157 __acquires(rq1->lock)
3158 __acquires(rq2->lock)
3160 BUG_ON(!irqs_disabled());
3162 raw_spin_lock(&rq1->lock);
3163 __acquire(rq2->lock); /* Fake it out ;) */
3166 raw_spin_lock(&rq1->lock);
3167 raw_spin_lock_nested(&rq2->lock, SINGLE_DEPTH_NESTING);
3169 raw_spin_lock(&rq2->lock);
3170 raw_spin_lock_nested(&rq1->lock, SINGLE_DEPTH_NESTING);
3173 update_rq_clock(rq1);
3174 update_rq_clock(rq2);
3178 * double_rq_unlock - safely unlock two runqueues
3180 * Note this does not restore interrupts like task_rq_unlock,
3181 * you need to do so manually after calling.
3183 static void double_rq_unlock(struct rq *rq1, struct rq *rq2)
3184 __releases(rq1->lock)
3185 __releases(rq2->lock)
3187 raw_spin_unlock(&rq1->lock);
3189 raw_spin_unlock(&rq2->lock);
3191 __release(rq2->lock);
3195 * sched_exec - execve() is a valuable balancing opportunity, because at
3196 * this point the task has the smallest effective memory and cache footprint.
3198 void sched_exec(void)
3200 struct task_struct *p = current;
3201 struct migration_req req;
3202 int dest_cpu, this_cpu;
3203 unsigned long flags;
3207 this_cpu = get_cpu();
3208 dest_cpu = select_task_rq(p, SD_BALANCE_EXEC, 0);
3209 if (dest_cpu == this_cpu) {
3214 rq = task_rq_lock(p, &flags);
3218 * select_task_rq() can race against ->cpus_allowed
3220 if (!cpumask_test_cpu(dest_cpu, &p->cpus_allowed)
3221 || unlikely(!cpu_active(dest_cpu))) {
3222 task_rq_unlock(rq, &flags);
3226 /* force the process onto the specified CPU */
3227 if (migrate_task(p, dest_cpu, &req)) {
3228 /* Need to wait for migration thread (might exit: take ref). */
3229 struct task_struct *mt = rq->migration_thread;
3231 get_task_struct(mt);
3232 task_rq_unlock(rq, &flags);
3233 wake_up_process(mt);
3234 put_task_struct(mt);
3235 wait_for_completion(&req.done);
3239 task_rq_unlock(rq, &flags);
3243 * pull_task - move a task from a remote runqueue to the local runqueue.
3244 * Both runqueues must be locked.
3246 static void pull_task(struct rq *src_rq, struct task_struct *p,
3247 struct rq *this_rq, int this_cpu)
3249 deactivate_task(src_rq, p, 0);
3250 set_task_cpu(p, this_cpu);
3251 activate_task(this_rq, p, 0);
3252 check_preempt_curr(this_rq, p, 0);
3256 * can_migrate_task - may task p from runqueue rq be migrated to this_cpu?
3259 int can_migrate_task(struct task_struct *p, struct rq *rq, int this_cpu,
3260 struct sched_domain *sd, enum cpu_idle_type idle,
3263 int tsk_cache_hot = 0;
3265 * We do not migrate tasks that are:
3266 * 1) running (obviously), or
3267 * 2) cannot be migrated to this CPU due to cpus_allowed, or
3268 * 3) are cache-hot on their current CPU.
3270 if (!cpumask_test_cpu(this_cpu, &p->cpus_allowed)) {
3271 schedstat_inc(p, se.nr_failed_migrations_affine);
3276 if (task_running(rq, p)) {
3277 schedstat_inc(p, se.nr_failed_migrations_running);
3282 * Aggressive migration if:
3283 * 1) task is cache cold, or
3284 * 2) too many balance attempts have failed.
3287 tsk_cache_hot = task_hot(p, rq->clock, sd);
3288 if (!tsk_cache_hot ||
3289 sd->nr_balance_failed > sd->cache_nice_tries) {
3290 #ifdef CONFIG_SCHEDSTATS
3291 if (tsk_cache_hot) {
3292 schedstat_inc(sd, lb_hot_gained[idle]);
3293 schedstat_inc(p, se.nr_forced_migrations);
3299 if (tsk_cache_hot) {
3300 schedstat_inc(p, se.nr_failed_migrations_hot);
3306 static unsigned long
3307 balance_tasks(struct rq *this_rq, int this_cpu, struct rq *busiest,
3308 unsigned long max_load_move, struct sched_domain *sd,
3309 enum cpu_idle_type idle, int *all_pinned,
3310 int *this_best_prio, struct rq_iterator *iterator)
3312 int loops = 0, pulled = 0, pinned = 0;
3313 struct task_struct *p;
3314 long rem_load_move = max_load_move;
3316 if (max_load_move == 0)
3322 * Start the load-balancing iterator:
3324 p = iterator->start(iterator->arg);
3326 if (!p || loops++ > sysctl_sched_nr_migrate)
3329 if ((p->se.load.weight >> 1) > rem_load_move ||
3330 !can_migrate_task(p, busiest, this_cpu, sd, idle, &pinned)) {
3331 p = iterator->next(iterator->arg);
3335 pull_task(busiest, p, this_rq, this_cpu);
3337 rem_load_move -= p->se.load.weight;
3339 #ifdef CONFIG_PREEMPT
3341 * NEWIDLE balancing is a source of latency, so preemptible kernels
3342 * will stop after the first task is pulled to minimize the critical
3345 if (idle == CPU_NEWLY_IDLE)
3350 * We only want to steal up to the prescribed amount of weighted load.
3352 if (rem_load_move > 0) {
3353 if (p->prio < *this_best_prio)
3354 *this_best_prio = p->prio;
3355 p = iterator->next(iterator->arg);
3360 * Right now, this is one of only two places pull_task() is called,
3361 * so we can safely collect pull_task() stats here rather than
3362 * inside pull_task().
3364 schedstat_add(sd, lb_gained[idle], pulled);
3367 *all_pinned = pinned;
3369 return max_load_move - rem_load_move;
3373 * move_tasks tries to move up to max_load_move weighted load from busiest to
3374 * this_rq, as part of a balancing operation within domain "sd".
3375 * Returns 1 if successful and 0 otherwise.
3377 * Called with both runqueues locked.
3379 static int move_tasks(struct rq *this_rq, int this_cpu, struct rq *busiest,
3380 unsigned long max_load_move,
3381 struct sched_domain *sd, enum cpu_idle_type idle,
3384 const struct sched_class *class = sched_class_highest;
3385 unsigned long total_load_moved = 0;
3386 int this_best_prio = this_rq->curr->prio;
3390 class->load_balance(this_rq, this_cpu, busiest,
3391 max_load_move - total_load_moved,
3392 sd, idle, all_pinned, &this_best_prio);
3393 class = class->next;
3395 #ifdef CONFIG_PREEMPT
3397 * NEWIDLE balancing is a source of latency, so preemptible
3398 * kernels will stop after the first task is pulled to minimize
3399 * the critical section.
3401 if (idle == CPU_NEWLY_IDLE && this_rq->nr_running)
3404 } while (class && max_load_move > total_load_moved);
3406 return total_load_moved > 0;
3410 iter_move_one_task(struct rq *this_rq, int this_cpu, struct rq *busiest,
3411 struct sched_domain *sd, enum cpu_idle_type idle,
3412 struct rq_iterator *iterator)
3414 struct task_struct *p = iterator->start(iterator->arg);
3418 if (can_migrate_task(p, busiest, this_cpu, sd, idle, &pinned)) {
3419 pull_task(busiest, p, this_rq, this_cpu);
3421 * Right now, this is only the second place pull_task()
3422 * is called, so we can safely collect pull_task()
3423 * stats here rather than inside pull_task().
3425 schedstat_inc(sd, lb_gained[idle]);
3429 p = iterator->next(iterator->arg);
3436 * move_one_task tries to move exactly one task from busiest to this_rq, as
3437 * part of active balancing operations within "domain".
3438 * Returns 1 if successful and 0 otherwise.
3440 * Called with both runqueues locked.
3442 static int move_one_task(struct rq *this_rq, int this_cpu, struct rq *busiest,
3443 struct sched_domain *sd, enum cpu_idle_type idle)
3445 const struct sched_class *class;
3447 for_each_class(class) {
3448 if (class->move_one_task(this_rq, this_cpu, busiest, sd, idle))
3454 /********** Helpers for find_busiest_group ************************/
3456 * sd_lb_stats - Structure to store the statistics of a sched_domain
3457 * during load balancing.
3459 struct sd_lb_stats {
3460 struct sched_group *busiest; /* Busiest group in this sd */
3461 struct sched_group *this; /* Local group in this sd */
3462 unsigned long total_load; /* Total load of all groups in sd */
3463 unsigned long total_pwr; /* Total power of all groups in sd */
3464 unsigned long avg_load; /* Average load across all groups in sd */
3466 /** Statistics of this group */
3467 unsigned long this_load;
3468 unsigned long this_load_per_task;
3469 unsigned long this_nr_running;
3471 /* Statistics of the busiest group */
3472 unsigned long max_load;
3473 unsigned long busiest_load_per_task;
3474 unsigned long busiest_nr_running;
3476 int group_imb; /* Is there imbalance in this sd */
3477 #if defined(CONFIG_SCHED_MC) || defined(CONFIG_SCHED_SMT)
3478 int power_savings_balance; /* Is powersave balance needed for this sd */
3479 struct sched_group *group_min; /* Least loaded group in sd */
3480 struct sched_group *group_leader; /* Group which relieves group_min */
3481 unsigned long min_load_per_task; /* load_per_task in group_min */
3482 unsigned long leader_nr_running; /* Nr running of group_leader */
3483 unsigned long min_nr_running; /* Nr running of group_min */
3488 * sg_lb_stats - stats of a sched_group required for load_balancing
3490 struct sg_lb_stats {
3491 unsigned long avg_load; /*Avg load across the CPUs of the group */
3492 unsigned long group_load; /* Total load over the CPUs of the group */
3493 unsigned long sum_nr_running; /* Nr tasks running in the group */
3494 unsigned long sum_weighted_load; /* Weighted load of group's tasks */
3495 unsigned long group_capacity;
3496 int group_imb; /* Is there an imbalance in the group ? */
3500 * group_first_cpu - Returns the first cpu in the cpumask of a sched_group.
3501 * @group: The group whose first cpu is to be returned.
3503 static inline unsigned int group_first_cpu(struct sched_group *group)
3505 return cpumask_first(sched_group_cpus(group));
3509 * get_sd_load_idx - Obtain the load index for a given sched domain.
3510 * @sd: The sched_domain whose load_idx is to be obtained.
3511 * @idle: The Idle status of the CPU for whose sd load_icx is obtained.
3513 static inline int get_sd_load_idx(struct sched_domain *sd,
3514 enum cpu_idle_type idle)
3520 load_idx = sd->busy_idx;
3523 case CPU_NEWLY_IDLE:
3524 load_idx = sd->newidle_idx;
3527 load_idx = sd->idle_idx;
3535 #if defined(CONFIG_SCHED_MC) || defined(CONFIG_SCHED_SMT)
3537 * init_sd_power_savings_stats - Initialize power savings statistics for
3538 * the given sched_domain, during load balancing.
3540 * @sd: Sched domain whose power-savings statistics are to be initialized.
3541 * @sds: Variable containing the statistics for sd.
3542 * @idle: Idle status of the CPU at which we're performing load-balancing.
3544 static inline void init_sd_power_savings_stats(struct sched_domain *sd,
3545 struct sd_lb_stats *sds, enum cpu_idle_type idle)
3548 * Busy processors will not participate in power savings
3551 if (idle == CPU_NOT_IDLE || !(sd->flags & SD_POWERSAVINGS_BALANCE))
3552 sds->power_savings_balance = 0;
3554 sds->power_savings_balance = 1;
3555 sds->min_nr_running = ULONG_MAX;
3556 sds->leader_nr_running = 0;
3561 * update_sd_power_savings_stats - Update the power saving stats for a
3562 * sched_domain while performing load balancing.
3564 * @group: sched_group belonging to the sched_domain under consideration.
3565 * @sds: Variable containing the statistics of the sched_domain
3566 * @local_group: Does group contain the CPU for which we're performing
3568 * @sgs: Variable containing the statistics of the group.
3570 static inline void update_sd_power_savings_stats(struct sched_group *group,
3571 struct sd_lb_stats *sds, int local_group, struct sg_lb_stats *sgs)
3574 if (!sds->power_savings_balance)
3578 * If the local group is idle or completely loaded
3579 * no need to do power savings balance at this domain
3581 if (local_group && (sds->this_nr_running >= sgs->group_capacity ||
3582 !sds->this_nr_running))
3583 sds->power_savings_balance = 0;
3586 * If a group is already running at full capacity or idle,
3587 * don't include that group in power savings calculations
3589 if (!sds->power_savings_balance ||
3590 sgs->sum_nr_running >= sgs->group_capacity ||
3591 !sgs->sum_nr_running)
3595 * Calculate the group which has the least non-idle load.
3596 * This is the group from where we need to pick up the load
3599 if ((sgs->sum_nr_running < sds->min_nr_running) ||
3600 (sgs->sum_nr_running == sds->min_nr_running &&
3601 group_first_cpu(group) > group_first_cpu(sds->group_min))) {
3602 sds->group_min = group;
3603 sds->min_nr_running = sgs->sum_nr_running;
3604 sds->min_load_per_task = sgs->sum_weighted_load /
3605 sgs->sum_nr_running;
3609 * Calculate the group which is almost near its
3610 * capacity but still has some space to pick up some load
3611 * from other group and save more power
3613 if (sgs->sum_nr_running + 1 > sgs->group_capacity)
3616 if (sgs->sum_nr_running > sds->leader_nr_running ||
3617 (sgs->sum_nr_running == sds->leader_nr_running &&
3618 group_first_cpu(group) < group_first_cpu(sds->group_leader))) {
3619 sds->group_leader = group;
3620 sds->leader_nr_running = sgs->sum_nr_running;
3625 * check_power_save_busiest_group - see if there is potential for some power-savings balance
3626 * @sds: Variable containing the statistics of the sched_domain
3627 * under consideration.
3628 * @this_cpu: Cpu at which we're currently performing load-balancing.
3629 * @imbalance: Variable to store the imbalance.
3632 * Check if we have potential to perform some power-savings balance.
3633 * If yes, set the busiest group to be the least loaded group in the
3634 * sched_domain, so that it's CPUs can be put to idle.
3636 * Returns 1 if there is potential to perform power-savings balance.
3639 static inline int check_power_save_busiest_group(struct sd_lb_stats *sds,
3640 int this_cpu, unsigned long *imbalance)
3642 if (!sds->power_savings_balance)
3645 if (sds->this != sds->group_leader ||
3646 sds->group_leader == sds->group_min)
3649 *imbalance = sds->min_load_per_task;
3650 sds->busiest = sds->group_min;
3655 #else /* CONFIG_SCHED_MC || CONFIG_SCHED_SMT */
3656 static inline void init_sd_power_savings_stats(struct sched_domain *sd,
3657 struct sd_lb_stats *sds, enum cpu_idle_type idle)
3662 static inline void update_sd_power_savings_stats(struct sched_group *group,
3663 struct sd_lb_stats *sds, int local_group, struct sg_lb_stats *sgs)
3668 static inline int check_power_save_busiest_group(struct sd_lb_stats *sds,
3669 int this_cpu, unsigned long *imbalance)
3673 #endif /* CONFIG_SCHED_MC || CONFIG_SCHED_SMT */
3676 unsigned long default_scale_freq_power(struct sched_domain *sd, int cpu)
3678 return SCHED_LOAD_SCALE;
3681 unsigned long __weak arch_scale_freq_power(struct sched_domain *sd, int cpu)
3683 return default_scale_freq_power(sd, cpu);
3686 unsigned long default_scale_smt_power(struct sched_domain *sd, int cpu)
3688 unsigned long weight = cpumask_weight(sched_domain_span(sd));
3689 unsigned long smt_gain = sd->smt_gain;
3696 unsigned long __weak arch_scale_smt_power(struct sched_domain *sd, int cpu)
3698 return default_scale_smt_power(sd, cpu);
3701 unsigned long scale_rt_power(int cpu)
3703 struct rq *rq = cpu_rq(cpu);
3704 u64 total, available;
3706 sched_avg_update(rq);
3708 total = sched_avg_period() + (rq->clock - rq->age_stamp);
3709 available = total - rq->rt_avg;
3711 if (unlikely((s64)total < SCHED_LOAD_SCALE))
3712 total = SCHED_LOAD_SCALE;