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 <asm/mmu_context.h>
36 #include <linux/interrupt.h>
37 #include <linux/capability.h>
38 #include <linux/completion.h>
39 #include <linux/kernel_stat.h>
40 #include <linux/debug_locks.h>
41 #include <linux/perf_event.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/proc_fs.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/unistd.h>
66 #include <linux/pagemap.h>
67 #include <linux/hrtimer.h>
68 #include <linux/tick.h>
69 #include <linux/debugfs.h>
70 #include <linux/ctype.h>
71 #include <linux/ftrace.h>
72 #include <linux/slab.h>
73 #include <linux/init_task.h>
74 #include <linux/binfmts.h>
75 #include <linux/context_tracking.h>
77 #include <asm/switch_to.h>
79 #include <asm/irq_regs.h>
80 #include <asm/mutex.h>
81 #ifdef CONFIG_PARAVIRT
82 #include <asm/paravirt.h>
86 #include "../workqueue_internal.h"
87 #include "../smpboot.h"
89 #ifdef CONFIG_MT65XX_TRACER
90 #include "mach/mt_mon.h"
91 #include "linux/aee.h"
94 #include <linux/mt_sched_mon.h>
95 #define CREATE_TRACE_POINTS
96 #include <trace/events/sched.h>
98 #include <mtlbprof/mtlbprof.h>
99 #include <mtlbprof/mtlbprof_stat.h>
101 #ifdef CONFIG_MT_PRIO_TRACER
102 # include <linux/prio_tracer.h>
105 void start_bandwidth_timer(struct hrtimer
*period_timer
, ktime_t period
)
108 ktime_t soft
, hard
, now
;
111 if (hrtimer_active(period_timer
))
114 now
= hrtimer_cb_get_time(period_timer
);
115 hrtimer_forward(period_timer
, now
, period
);
117 soft
= hrtimer_get_softexpires(period_timer
);
118 hard
= hrtimer_get_expires(period_timer
);
119 delta
= ktime_to_ns(ktime_sub(hard
, soft
));
120 __hrtimer_start_range_ns(period_timer
, soft
, delta
,
121 HRTIMER_MODE_ABS_PINNED
, 0);
125 DEFINE_MUTEX(sched_domains_mutex
);
126 DEFINE_PER_CPU_SHARED_ALIGNED(struct rq
, runqueues
);
128 static void update_rq_clock_task(struct rq
*rq
, s64 delta
);
130 void update_rq_clock(struct rq
*rq
)
134 if (rq
->skip_clock_update
> 0)
137 delta
= sched_clock_cpu(cpu_of(rq
)) - rq
->clock
;
139 update_rq_clock_task(rq
, delta
);
143 * Debugging: various feature bits
146 #define SCHED_FEAT(name, enabled) \
147 (1UL << __SCHED_FEAT_##name) * enabled |
149 const_debug
unsigned int sysctl_sched_features
=
150 #include "features.h"
155 #ifdef CONFIG_SCHED_DEBUG
156 #define SCHED_FEAT(name, enabled) \
159 static const char * const sched_feat_names
[] = {
160 #include "features.h"
165 static int sched_feat_show(struct seq_file
*m
, void *v
)
169 for (i
= 0; i
< __SCHED_FEAT_NR
; i
++) {
170 if (!(sysctl_sched_features
& (1UL << i
)))
172 seq_printf(m
, "%s ", sched_feat_names
[i
]);
179 #ifdef HAVE_JUMP_LABEL
181 #define jump_label_key__true STATIC_KEY_INIT_TRUE
182 #define jump_label_key__false STATIC_KEY_INIT_FALSE
184 #define SCHED_FEAT(name, enabled) \
185 jump_label_key__##enabled ,
187 struct static_key sched_feat_keys
[__SCHED_FEAT_NR
] = {
188 #include "features.h"
193 static void sched_feat_disable(int i
)
195 static_key_disable(&sched_feat_keys
[i
]);
198 static void sched_feat_enable(int i
)
200 static_key_enable(&sched_feat_keys
[i
]);
203 static void sched_feat_disable(int i
) { };
204 static void sched_feat_enable(int i
) { };
205 #endif /* HAVE_JUMP_LABEL */
207 static int sched_feat_set(char *cmp
)
212 if (strncmp(cmp
, "NO_", 3) == 0) {
217 for (i
= 0; i
< __SCHED_FEAT_NR
; i
++) {
218 if (strcmp(cmp
, sched_feat_names
[i
]) == 0) {
220 sysctl_sched_features
&= ~(1UL << i
);
221 sched_feat_disable(i
);
223 sysctl_sched_features
|= (1UL << i
);
224 sched_feat_enable(i
);
234 sched_feat_write(struct file
*filp
, const char __user
*ubuf
,
235 size_t cnt
, loff_t
*ppos
)
244 if (copy_from_user(&buf
, ubuf
, cnt
))
250 i
= sched_feat_set(cmp
);
251 if (i
== __SCHED_FEAT_NR
)
259 static int sched_feat_open(struct inode
*inode
, struct file
*filp
)
261 return single_open(filp
, sched_feat_show
, NULL
);
264 static const struct file_operations sched_feat_fops
= {
265 .open
= sched_feat_open
,
266 .write
= sched_feat_write
,
269 .release
= single_release
,
272 static __init
int sched_init_debug(void)
274 debugfs_create_file("sched_features", 0644, NULL
, NULL
,
279 late_initcall(sched_init_debug
);
280 #endif /* CONFIG_SCHED_DEBUG */
283 * Number of tasks to iterate in a single balance run.
284 * Limited because this is done with IRQs disabled.
286 const_debug
unsigned int sysctl_sched_nr_migrate
= 32;
289 * period over which we average the RT time consumption, measured
294 const_debug
unsigned int sysctl_sched_time_avg
= MSEC_PER_SEC
;
297 * period over which we measure -rt task cpu usage in us.
300 unsigned int sysctl_sched_rt_period
= 1000000;
302 __read_mostly
int scheduler_running
;
305 * part of the period that we allow rt tasks to run in us.
308 int sysctl_sched_rt_runtime
= 950000;
313 * __task_rq_lock - lock the rq @p resides on.
315 static inline struct rq
*__task_rq_lock(struct task_struct
*p
)
320 lockdep_assert_held(&p
->pi_lock
);
324 raw_spin_lock(&rq
->lock
);
325 if (likely(rq
== task_rq(p
)))
327 raw_spin_unlock(&rq
->lock
);
332 * task_rq_lock - lock p->pi_lock and lock the rq @p resides on.
334 static struct rq
*task_rq_lock(struct task_struct
*p
, unsigned long *flags
)
335 __acquires(p
->pi_lock
)
341 raw_spin_lock_irqsave(&p
->pi_lock
, *flags
);
343 raw_spin_lock(&rq
->lock
);
344 if (likely(rq
== task_rq(p
)))
346 raw_spin_unlock(&rq
->lock
);
347 raw_spin_unlock_irqrestore(&p
->pi_lock
, *flags
);
351 static void __task_rq_unlock(struct rq
*rq
)
354 raw_spin_unlock(&rq
->lock
);
358 task_rq_unlock(struct rq
*rq
, struct task_struct
*p
, unsigned long *flags
)
360 __releases(p
->pi_lock
)
362 raw_spin_unlock(&rq
->lock
);
363 raw_spin_unlock_irqrestore(&p
->pi_lock
, *flags
);
367 * this_rq_lock - lock this runqueue and disable interrupts.
369 static struct rq
*this_rq_lock(void)
376 raw_spin_lock(&rq
->lock
);
381 #ifdef CONFIG_SCHED_HRTICK
383 * Use HR-timers to deliver accurate preemption points.
385 * Its all a bit involved since we cannot program an hrt while holding the
386 * rq->lock. So what we do is store a state in in rq->hrtick_* and ask for a
389 * When we get rescheduled we reprogram the hrtick_timer outside of the
393 static void hrtick_clear(struct rq
*rq
)
395 if (hrtimer_active(&rq
->hrtick_timer
))
396 hrtimer_cancel(&rq
->hrtick_timer
);
400 * High-resolution timer tick.
401 * Runs from hardirq context with interrupts disabled.
403 static enum hrtimer_restart
hrtick(struct hrtimer
*timer
)
405 struct rq
*rq
= container_of(timer
, struct rq
, hrtick_timer
);
407 WARN_ON_ONCE(cpu_of(rq
) != smp_processor_id());
409 raw_spin_lock(&rq
->lock
);
411 rq
->curr
->sched_class
->task_tick(rq
, rq
->curr
, 1);
412 raw_spin_unlock(&rq
->lock
);
414 return HRTIMER_NORESTART
;
419 * called from hardirq (IPI) context
421 static void __hrtick_start(void *arg
)
425 raw_spin_lock(&rq
->lock
);
426 hrtimer_restart(&rq
->hrtick_timer
);
427 rq
->hrtick_csd_pending
= 0;
428 raw_spin_unlock(&rq
->lock
);
432 * Called to set the hrtick timer state.
434 * called with rq->lock held and irqs disabled
436 void hrtick_start(struct rq
*rq
, u64 delay
)
438 struct hrtimer
*timer
= &rq
->hrtick_timer
;
439 ktime_t time
= ktime_add_ns(timer
->base
->get_time(), delay
);
441 hrtimer_set_expires(timer
, time
);
443 if (rq
== this_rq()) {
444 hrtimer_restart(timer
);
445 } else if (!rq
->hrtick_csd_pending
) {
446 __smp_call_function_single(cpu_of(rq
), &rq
->hrtick_csd
, 0);
447 rq
->hrtick_csd_pending
= 1;
452 hotplug_hrtick(struct notifier_block
*nfb
, unsigned long action
, void *hcpu
)
454 int cpu
= (int)(long)hcpu
;
457 case CPU_UP_CANCELED
:
458 case CPU_UP_CANCELED_FROZEN
:
459 case CPU_DOWN_PREPARE
:
460 case CPU_DOWN_PREPARE_FROZEN
:
462 case CPU_DEAD_FROZEN
:
463 hrtick_clear(cpu_rq(cpu
));
470 static __init
void init_hrtick(void)
472 hotcpu_notifier(hotplug_hrtick
, 0);
476 * Called to set the hrtick timer state.
478 * called with rq->lock held and irqs disabled
480 void hrtick_start(struct rq
*rq
, u64 delay
)
482 __hrtimer_start_range_ns(&rq
->hrtick_timer
, ns_to_ktime(delay
), 0,
483 HRTIMER_MODE_REL_PINNED
, 0);
486 static inline void init_hrtick(void)
489 #endif /* CONFIG_SMP */
491 static void init_rq_hrtick(struct rq
*rq
)
494 rq
->hrtick_csd_pending
= 0;
496 rq
->hrtick_csd
.flags
= 0;
497 rq
->hrtick_csd
.func
= __hrtick_start
;
498 rq
->hrtick_csd
.info
= rq
;
501 hrtimer_init(&rq
->hrtick_timer
, CLOCK_MONOTONIC
, HRTIMER_MODE_REL
);
502 rq
->hrtick_timer
.function
= hrtick
;
504 #else /* CONFIG_SCHED_HRTICK */
505 static inline void hrtick_clear(struct rq
*rq
)
509 static inline void init_rq_hrtick(struct rq
*rq
)
513 static inline void init_hrtick(void)
516 #endif /* CONFIG_SCHED_HRTICK */
519 * resched_task - mark a task 'to be rescheduled now'.
521 * On UP this means the setting of the need_resched flag, on SMP it
522 * might also involve a cross-CPU call to trigger the scheduler on
526 void resched_task(struct task_struct
*p
)
530 assert_raw_spin_locked(&task_rq(p
)->lock
);
532 if (test_tsk_need_resched(p
))
535 set_tsk_need_resched(p
);
538 if (cpu
== smp_processor_id())
541 /* NEED_RESCHED must be visible before we test polling */
543 if (!tsk_is_polling(p
))
544 smp_send_reschedule(cpu
);
547 void resched_cpu(int cpu
)
549 struct rq
*rq
= cpu_rq(cpu
);
552 if (!raw_spin_trylock_irqsave(&rq
->lock
, flags
))
554 resched_task(cpu_curr(cpu
));
555 raw_spin_unlock_irqrestore(&rq
->lock
, flags
);
558 #ifdef CONFIG_NO_HZ_COMMON
560 * In the semi idle case, use the nearest busy cpu for migrating timers
561 * from an idle cpu. This is good for power-savings.
563 * We don't do similar optimization for completely idle system, as
564 * selecting an idle cpu will add more delays to the timers than intended
565 * (as that cpu's timer base may not be uptodate wrt jiffies etc).
567 int get_nohz_timer_target(void)
569 int cpu
= smp_processor_id();
571 struct sched_domain
*sd
;
574 for_each_domain(cpu
, sd
) {
575 for_each_cpu(i
, sched_domain_span(sd
)) {
587 * When add_timer_on() enqueues a timer into the timer wheel of an
588 * idle CPU then this timer might expire before the next timer event
589 * which is scheduled to wake up that CPU. In case of a completely
590 * idle system the next event might even be infinite time into the
591 * future. wake_up_idle_cpu() ensures that the CPU is woken up and
592 * leaves the inner idle loop so the newly added timer is taken into
593 * account when the CPU goes back to idle and evaluates the timer
594 * wheel for the next timer event.
596 static void wake_up_idle_cpu(int cpu
)
598 struct rq
*rq
= cpu_rq(cpu
);
600 if (cpu
== smp_processor_id())
604 * This is safe, as this function is called with the timer
605 * wheel base lock of (cpu) held. When the CPU is on the way
606 * to idle and has not yet set rq->curr to idle then it will
607 * be serialized on the timer wheel base lock and take the new
608 * timer into account automatically.
610 if (rq
->curr
!= rq
->idle
)
614 * We can set TIF_RESCHED on the idle task of the other CPU
615 * lockless. The worst case is that the other CPU runs the
616 * idle task through an additional NOOP schedule()
618 set_tsk_need_resched(rq
->idle
);
620 /* NEED_RESCHED must be visible before we test polling */
622 if (!tsk_is_polling(rq
->idle
))
623 smp_send_reschedule(cpu
);
626 static bool wake_up_full_nohz_cpu(int cpu
)
628 if (tick_nohz_full_cpu(cpu
)) {
629 if (cpu
!= smp_processor_id() ||
630 tick_nohz_tick_stopped())
631 smp_send_reschedule(cpu
);
638 void wake_up_nohz_cpu(int cpu
)
640 if (!wake_up_full_nohz_cpu(cpu
))
641 wake_up_idle_cpu(cpu
);
644 static inline bool got_nohz_idle_kick(void)
646 int cpu
= smp_processor_id();
648 if (!test_bit(NOHZ_BALANCE_KICK
, nohz_flags(cpu
)))
651 if (idle_cpu(cpu
) && !need_resched())
655 * We can't run Idle Load Balance on this CPU for this time so we
656 * cancel it and clear NOHZ_BALANCE_KICK
658 clear_bit(NOHZ_BALANCE_KICK
, nohz_flags(cpu
));
662 #else /* CONFIG_NO_HZ_COMMON */
664 static inline bool got_nohz_idle_kick(void)
669 #endif /* CONFIG_NO_HZ_COMMON */
671 #ifdef CONFIG_NO_HZ_FULL
672 bool sched_can_stop_tick(void)
678 /* Make sure rq->nr_running update is visible after the IPI */
681 /* More than one running task need preemption */
682 if (rq
->nr_running
> 1)
687 #endif /* CONFIG_NO_HZ_FULL */
689 void sched_avg_update(struct rq
*rq
)
691 s64 period
= sched_avg_period();
693 while ((s64
)(rq
->clock
- rq
->age_stamp
) > period
) {
695 * Inline assembly required to prevent the compiler
696 * optimising this loop into a divmod call.
697 * See __iter_div_u64_rem() for another example of this.
699 asm("" : "+rm" (rq
->age_stamp
));
700 rq
->age_stamp
+= period
;
705 #else /* !CONFIG_SMP */
706 void resched_task(struct task_struct
*p
)
708 assert_raw_spin_locked(&task_rq(p
)->lock
);
709 set_tsk_need_resched(p
);
711 #endif /* CONFIG_SMP */
713 #if defined(CONFIG_RT_GROUP_SCHED) || (defined(CONFIG_FAIR_GROUP_SCHED) && \
714 (defined(CONFIG_SMP) || defined(CONFIG_CFS_BANDWIDTH)))
716 * Iterate task_group tree rooted at *from, calling @down when first entering a
717 * node and @up when leaving it for the final time.
719 * Caller must hold rcu_lock or sufficient equivalent.
721 int walk_tg_tree_from(struct task_group
*from
,
722 tg_visitor down
, tg_visitor up
, void *data
)
724 struct task_group
*parent
, *child
;
730 ret
= (*down
)(parent
, data
);
733 list_for_each_entry_rcu(child
, &parent
->children
, siblings
) {
740 ret
= (*up
)(parent
, data
);
741 if (ret
|| parent
== from
)
745 parent
= parent
->parent
;
752 int tg_nop(struct task_group
*tg
, void *data
)
758 static void set_load_weight(struct task_struct
*p
)
760 int prio
= p
->static_prio
- MAX_RT_PRIO
;
761 struct load_weight
*load
= &p
->se
.load
;
764 * SCHED_IDLE tasks get minimal weight:
766 if (p
->policy
== SCHED_IDLE
) {
767 load
->weight
= scale_load(WEIGHT_IDLEPRIO
);
768 load
->inv_weight
= WMULT_IDLEPRIO
;
772 load
->weight
= scale_load(prio_to_weight
[prio
]);
773 load
->inv_weight
= prio_to_wmult
[prio
];
776 #ifdef CONFIG_MTK_SCHED_CMP_TGS
777 static void sched_tg_enqueue(struct rq
*rq
, struct task_struct
*p
)
781 struct task_struct
*tg
= p
->group_leader
;
783 if(group_leader_is_empty(p
))
785 id
= get_cluster_id(rq
->cpu
);
786 if (unlikely(WARN_ON(id
< 0)))
789 raw_spin_lock_irqsave(&tg
->thread_group_info_lock
, flags
);
790 tg
->thread_group_info
[id
].nr_running
++;
791 raw_spin_unlock_irqrestore(&tg
->thread_group_info_lock
, flags
);
794 mt_sched_printf("enqueue %d:%s %d:%s %d %lu %lu %lu, %lu %lu %lu",
795 tg
->pid
, tg
->comm
, p
->pid
, p
->comm
, id
, rq
->cpu
,
796 tg
->thread_group_info
[0].nr_running
,
797 tg
->thread_group_info
[0].cfs_nr_running
,
798 tg
->thread_group_info
[0].load_avg_ratio
,
799 tg
->thread_group_info
[1].nr_running
,
800 tg
->thread_group_info
[1].cfs_nr_running
,
801 tg
->thread_group_info
[1].load_avg_ratio
);
806 static void sched_tg_dequeue(struct rq
*rq
, struct task_struct
*p
)
810 struct task_struct
*tg
= p
->group_leader
;
812 if(group_leader_is_empty(p
))
814 id
= get_cluster_id(rq
->cpu
);
815 if (unlikely(WARN_ON(id
< 0)))
818 raw_spin_lock_irqsave(&tg
->thread_group_info_lock
, flags
);
819 //WARN_ON(!tg->thread_group_info[id].nr_running);
820 tg
->thread_group_info
[id
].nr_running
--;
821 raw_spin_unlock_irqrestore(&tg
->thread_group_info_lock
, flags
);
824 mt_sched_printf("dequeue %d:%s %d:%s %d %d %lu %lu %lu, %lu %lu %lu",
825 tg
->pid
, tg
->comm
, p
->pid
, p
->comm
, id
, rq
->cpu
,
826 tg
->thread_group_info
[0].nr_running
,
827 tg
->thread_group_info
[0].cfs_nr_running
,
828 tg
->thread_group_info
[0].load_avg_ratio
,
829 tg
->thread_group_info
[1].nr_running
,
830 tg
->thread_group_info
[1].cfs_nr_running
,
831 tg
->thread_group_info
[1].load_avg_ratio
);
838 #ifdef CONFIG_MTK_SCHED_CMP_TGS
839 static void tgs_log(struct rq
*rq
, struct task_struct
*p
)
841 #ifdef CONFIG_MT_SCHED_INFO
842 struct task_struct
*tg
= p
->group_leader
;
844 if(group_leader_is_empty(p
))
847 // if(!strncmp(tg->comm,"sched_test", 10)){
848 mt_sched_printf("%d:%s %d:%s %lu %lu %lu, %lu %lu %lu", tg
->pid
, tg
->comm
, p
->pid
, p
->comm
,
849 tg
->thread_group_info
[0].nr_running
,
850 tg
->thread_group_info
[0].cfs_nr_running
,
851 tg
->thread_group_info
[0].load_avg_ratio
,
852 tg
->thread_group_info
[1].nr_running
,
853 tg
->thread_group_info
[1].cfs_nr_running
,
854 tg
->thread_group_info
[1].load_avg_ratio
);
860 static void enqueue_task(struct rq
*rq
, struct task_struct
*p
, int flags
)
863 sched_info_queued(p
);
864 p
->sched_class
->enqueue_task(rq
, p
, flags
);
865 #ifdef CONFIG_MTK_SCHED_CMP_TGS
866 sched_tg_enqueue(rq
, p
);
871 static void dequeue_task(struct rq
*rq
, struct task_struct
*p
, int flags
)
874 sched_info_dequeued(p
);
875 p
->sched_class
->dequeue_task(rq
, p
, flags
);
876 #ifdef CONFIG_MTK_SCHED_CMP_TGS
877 sched_tg_dequeue(rq
, p
);
882 void activate_task(struct rq
*rq
, struct task_struct
*p
, int flags
)
884 if (task_contributes_to_load(p
))
885 rq
->nr_uninterruptible
--;
887 enqueue_task(rq
, p
, flags
);
889 #ifdef CONFIG_MT_LOAD_BALANCE_PROFILER
890 if( 2 <= rq
->nr_running
){
891 if (1 == cpumask_weight(&p
->cpus_allowed
))
892 mt_lbprof_update_state_has_lock(rq
->cpu
, MT_LBPROF_AFFINITY_STATE
);
894 mt_lbprof_update_state_has_lock(rq
->cpu
, MT_LBPROF_N_TASK_STATE
);
895 }else if ( (1 == rq
->nr_running
)){
896 mt_lbprof_update_state_has_lock(rq
->cpu
, MT_LBPROF_ONE_TASK_STATE
);
901 void deactivate_task(struct rq
*rq
, struct task_struct
*p
, int flags
)
903 if (task_contributes_to_load(p
))
904 rq
->nr_uninterruptible
++;
906 dequeue_task(rq
, p
, flags
);
908 #ifdef CONFIG_MT_LOAD_BALANCE_PROFILER
909 if ( 1 == rq
->nr_running
)
910 mt_lbprof_update_state_has_lock(rq
->cpu
, MT_LBPROF_ONE_TASK_STATE
);
911 else if (0 == rq
->nr_running
)
912 mt_lbprof_update_state_has_lock(rq
->cpu
, MT_LBPROF_NO_TASK_STATE
);
916 static void update_rq_clock_task(struct rq
*rq
, s64 delta
)
919 * In theory, the compile should just see 0 here, and optimize out the call
920 * to sched_rt_avg_update. But I don't trust it...
922 #if defined(CONFIG_IRQ_TIME_ACCOUNTING) || defined(CONFIG_PARAVIRT_TIME_ACCOUNTING)
923 s64 steal
= 0, irq_delta
= 0;
925 #ifdef CONFIG_IRQ_TIME_ACCOUNTING
926 irq_delta
= irq_time_read(cpu_of(rq
)) - rq
->prev_irq_time
;
929 * Since irq_time is only updated on {soft,}irq_exit, we might run into
930 * this case when a previous update_rq_clock() happened inside a
933 * When this happens, we stop ->clock_task and only update the
934 * prev_irq_time stamp to account for the part that fit, so that a next
935 * update will consume the rest. This ensures ->clock_task is
938 * It does however cause some slight miss-attribution of {soft,}irq
939 * time, a more accurate solution would be to update the irq_time using
940 * the current rq->clock timestamp, except that would require using
943 if (irq_delta
> delta
)
946 rq
->prev_irq_time
+= irq_delta
;
949 #ifdef CONFIG_PARAVIRT_TIME_ACCOUNTING
950 if (static_key_false((¶virt_steal_rq_enabled
))) {
953 steal
= paravirt_steal_clock(cpu_of(rq
));
954 steal
-= rq
->prev_steal_time_rq
;
956 if (unlikely(steal
> delta
))
959 st
= steal_ticks(steal
);
960 steal
= st
* TICK_NSEC
;
962 rq
->prev_steal_time_rq
+= steal
;
968 rq
->clock_task
+= delta
;
970 #if defined(CONFIG_IRQ_TIME_ACCOUNTING) || defined(CONFIG_PARAVIRT_TIME_ACCOUNTING)
971 if ((irq_delta
+ steal
) && sched_feat(NONTASK_POWER
))
972 sched_rt_avg_update(rq
, irq_delta
+ steal
);
976 void sched_set_stop_task(int cpu
, struct task_struct
*stop
)
978 //struct sched_param param = { .sched_priority = MAX_RT_PRIO - 1 };
979 struct sched_param param
= { .sched_priority
= RTPM_PRIO_CPU_CALLBACK
};
980 struct task_struct
*old_stop
= cpu_rq(cpu
)->stop
;
984 * Make it appear like a SCHED_FIFO task, its something
985 * userspace knows about and won't get confused about.
987 * Also, it will make PI more or less work without too
988 * much confusion -- but then, stop work should not
989 * rely on PI working anyway.
991 sched_setscheduler_nocheck(stop
, SCHED_FIFO
, ¶m
);
993 stop
->sched_class
= &stop_sched_class
;
996 cpu_rq(cpu
)->stop
= stop
;
1000 * Reset it back to a normal scheduling class so that
1001 * it can die in pieces.
1003 old_stop
->sched_class
= &rt_sched_class
;
1008 * __normal_prio - return the priority that is based on the static prio
1010 static inline int __normal_prio(struct task_struct
*p
)
1012 return p
->static_prio
;
1016 * Calculate the expected normal priority: i.e. priority
1017 * without taking RT-inheritance into account. Might be
1018 * boosted by interactivity modifiers. Changes upon fork,
1019 * setprio syscalls, and whenever the interactivity
1020 * estimator recalculates.
1022 static inline int normal_prio(struct task_struct
*p
)
1026 if (task_has_rt_policy(p
))
1027 prio
= MAX_RT_PRIO
-1 - p
->rt_priority
;
1029 prio
= __normal_prio(p
);
1034 * Calculate the current priority, i.e. the priority
1035 * taken into account by the scheduler. This value might
1036 * be boosted by RT tasks, or might be boosted by
1037 * interactivity modifiers. Will be RT if the task got
1038 * RT-boosted. If not then it returns p->normal_prio.
1040 static int effective_prio(struct task_struct
*p
)
1042 p
->normal_prio
= normal_prio(p
);
1044 * If we are RT tasks or we were boosted to RT priority,
1045 * keep the priority unchanged. Otherwise, update priority
1046 * to the normal priority:
1048 if (!rt_prio(p
->prio
))
1049 return p
->normal_prio
;
1054 * task_curr - is this task currently executing on a CPU?
1055 * @p: the task in question.
1057 inline int task_curr(const struct task_struct
*p
)
1059 return cpu_curr(task_cpu(p
)) == p
;
1062 static inline void check_class_changed(struct rq
*rq
, struct task_struct
*p
,
1063 const struct sched_class
*prev_class
,
1066 if (prev_class
!= p
->sched_class
) {
1067 if (prev_class
->switched_from
)
1068 prev_class
->switched_from(rq
, p
);
1069 p
->sched_class
->switched_to(rq
, p
);
1070 } else if (oldprio
!= p
->prio
)
1071 p
->sched_class
->prio_changed(rq
, p
, oldprio
);
1074 void check_preempt_curr(struct rq
*rq
, struct task_struct
*p
, int flags
)
1076 const struct sched_class
*class;
1078 if (p
->sched_class
== rq
->curr
->sched_class
) {
1079 rq
->curr
->sched_class
->check_preempt_curr(rq
, p
, flags
);
1081 for_each_class(class) {
1082 if (class == rq
->curr
->sched_class
)
1084 if (class == p
->sched_class
) {
1085 resched_task(rq
->curr
);
1092 * A queue event has occurred, and we're going to schedule. In
1093 * this case, we can save a useless back to back clock update.
1095 if (rq
->curr
->on_rq
&& test_tsk_need_resched(rq
->curr
))
1096 rq
->skip_clock_update
= 1;
1099 static ATOMIC_NOTIFIER_HEAD(task_migration_notifier
);
1101 void register_task_migration_notifier(struct notifier_block
*n
)
1103 atomic_notifier_chain_register(&task_migration_notifier
, n
);
1107 void set_task_cpu(struct task_struct
*p
, unsigned int new_cpu
)
1109 #ifdef CONFIG_SCHED_DEBUG
1111 * We should never call set_task_cpu() on a blocked task,
1112 * ttwu() will sort out the placement.
1114 WARN_ON_ONCE(p
->state
!= TASK_RUNNING
&& p
->state
!= TASK_WAKING
&&
1115 !(task_thread_info(p
)->preempt_count
& PREEMPT_ACTIVE
));
1117 #ifdef CONFIG_LOCKDEP
1119 * The caller should hold either p->pi_lock or rq->lock, when changing
1120 * a task's CPU. ->pi_lock for waking tasks, rq->lock for runnable tasks.
1122 * sched_move_task() holds both and thus holding either pins the cgroup,
1125 * Furthermore, all task_rq users should acquire both locks, see
1128 WARN_ON_ONCE(debug_locks
&& !(lockdep_is_held(&p
->pi_lock
) ||
1129 lockdep_is_held(&task_rq(p
)->lock
)));
1133 trace_sched_migrate_task(p
, new_cpu
);
1135 if (task_cpu(p
) != new_cpu
) {
1136 struct task_migration_notifier tmn
;
1138 if (p
->sched_class
->migrate_task_rq
)
1139 p
->sched_class
->migrate_task_rq(p
, new_cpu
);
1140 p
->se
.nr_migrations
++;
1141 perf_sw_event(PERF_COUNT_SW_CPU_MIGRATIONS
, 1, NULL
, 0);
1144 tmn
.from_cpu
= task_cpu(p
);
1145 tmn
.to_cpu
= new_cpu
;
1147 atomic_notifier_call_chain(&task_migration_notifier
, 0, &tmn
);
1150 __set_task_cpu(p
, new_cpu
);
1153 struct migration_arg
{
1154 struct task_struct
*task
;
1158 static int migration_cpu_stop(void *data
);
1161 * wait_task_inactive - wait for a thread to unschedule.
1163 * If @match_state is nonzero, it's the @p->state value just checked and
1164 * not expected to change. If it changes, i.e. @p might have woken up,
1165 * then return zero. When we succeed in waiting for @p to be off its CPU,
1166 * we return a positive number (its total switch count). If a second call
1167 * a short while later returns the same number, the caller can be sure that
1168 * @p has remained unscheduled the whole time.
1170 * The caller must ensure that the task *will* unschedule sometime soon,
1171 * else this function might spin for a *long* time. This function can't
1172 * be called with interrupts off, or it may introduce deadlock with
1173 * smp_call_function() if an IPI is sent by the same process we are
1174 * waiting to become inactive.
1176 unsigned long wait_task_inactive(struct task_struct
*p
, long match_state
)
1178 unsigned long flags
;
1185 * We do the initial early heuristics without holding
1186 * any task-queue locks at all. We'll only try to get
1187 * the runqueue lock when things look like they will
1193 * If the task is actively running on another CPU
1194 * still, just relax and busy-wait without holding
1197 * NOTE! Since we don't hold any locks, it's not
1198 * even sure that "rq" stays as the right runqueue!
1199 * But we don't care, since "task_running()" will
1200 * return false if the runqueue has changed and p
1201 * is actually now running somewhere else!
1203 while (task_running(rq
, p
)) {
1204 if (match_state
&& unlikely(p
->state
!= match_state
))
1210 * Ok, time to look more closely! We need the rq
1211 * lock now, to be *sure*. If we're wrong, we'll
1212 * just go back and repeat.
1214 rq
= task_rq_lock(p
, &flags
);
1215 trace_sched_wait_task(p
);
1216 running
= task_running(rq
, p
);
1219 if (!match_state
|| p
->state
== match_state
)
1220 ncsw
= p
->nvcsw
| LONG_MIN
; /* sets MSB */
1221 task_rq_unlock(rq
, p
, &flags
);
1224 * If it changed from the expected state, bail out now.
1226 if (unlikely(!ncsw
))
1230 * Was it really running after all now that we
1231 * checked with the proper locks actually held?
1233 * Oops. Go back and try again..
1235 if (unlikely(running
)) {
1241 * It's not enough that it's not actively running,
1242 * it must be off the runqueue _entirely_, and not
1245 * So if it was still runnable (but just not actively
1246 * running right now), it's preempted, and we should
1247 * yield - it could be a while.
1249 if (unlikely(on_rq
)) {
1250 ktime_t to
= ktime_set(0, NSEC_PER_SEC
/HZ
);
1252 set_current_state(TASK_UNINTERRUPTIBLE
);
1253 schedule_hrtimeout(&to
, HRTIMER_MODE_REL
);
1258 * Ahh, all good. It wasn't running, and it wasn't
1259 * runnable, which means that it will never become
1260 * running in the future either. We're all done!
1269 * kick_process - kick a running thread to enter/exit the kernel
1270 * @p: the to-be-kicked thread
1272 * Cause a process which is running on another CPU to enter
1273 * kernel-mode, without any delay. (to get signals handled.)
1275 * NOTE: this function doesn't have to take the runqueue lock,
1276 * because all it wants to ensure is that the remote task enters
1277 * the kernel. If the IPI races and the task has been migrated
1278 * to another CPU then no harm is done and the purpose has been
1281 void kick_process(struct task_struct
*p
)
1287 if ((cpu
!= smp_processor_id()) && task_curr(p
))
1288 smp_send_reschedule(cpu
);
1291 EXPORT_SYMBOL_GPL(kick_process
);
1292 #endif /* CONFIG_SMP */
1296 * ->cpus_allowed is protected by both rq->lock and p->pi_lock
1298 static int select_fallback_rq(int cpu
, struct task_struct
*p
)
1300 int nid
= cpu_to_node(cpu
);
1301 const struct cpumask
*nodemask
= NULL
;
1302 enum { cpuset
, possible
, fail
} state
= cpuset
;
1306 * If the node that the cpu is on has been offlined, cpu_to_node()
1307 * will return -1. There is no cpu on the node, and we should
1308 * select the cpu on the other node.
1311 nodemask
= cpumask_of_node(nid
);
1313 /* Look for allowed, online CPU in same node. */
1314 for_each_cpu(dest_cpu
, nodemask
) {
1315 if (!cpu_online(dest_cpu
))
1317 if (!cpu_active(dest_cpu
))
1319 if (cpumask_test_cpu(dest_cpu
, tsk_cpus_allowed(p
)))
1325 /* Any allowed, online CPU? */
1326 for_each_cpu(dest_cpu
, tsk_cpus_allowed(p
)) {
1327 if (!cpu_online(dest_cpu
))
1329 if (!cpu_active(dest_cpu
))
1336 /* No more Mr. Nice Guy. */
1337 cpuset_cpus_allowed_fallback(p
);
1342 do_set_cpus_allowed(p
, cpu_possible_mask
);
1353 if (state
!= cpuset
) {
1355 * Don't tell them about moving exiting tasks or
1356 * kernel threads (both mm NULL), since they never
1359 if (p
->mm
&& printk_ratelimit()) {
1360 printk_deferred("process %d (%s) no longer affine to cpu%d\n",
1361 task_pid_nr(p
), p
->comm
, cpu
);
1369 * The caller (fork, wakeup) owns p->pi_lock, ->cpus_allowed is stable.
1372 int select_task_rq(struct task_struct
*p
, int sd_flags
, int wake_flags
)
1374 int cpu
= p
->sched_class
->select_task_rq(p
, sd_flags
, wake_flags
);
1377 * In order not to call set_task_cpu() on a blocking task we need
1378 * to rely on ttwu() to place the task on a valid ->cpus_allowed
1381 * Since this is common to all placement strategies, this lives here.
1383 * [ this allows ->select_task() to simply return task_cpu(p) and
1384 * not worry about this generic constraint ]
1386 if (unlikely(!cpumask_test_cpu(cpu
, tsk_cpus_allowed(p
)) ||
1388 cpu
= select_fallback_rq(task_cpu(p
), p
);
1393 static void update_avg(u64
*avg
, u64 sample
)
1395 s64 diff
= sample
- *avg
;
1401 ttwu_stat(struct task_struct
*p
, int cpu
, int wake_flags
)
1403 #ifdef CONFIG_SCHEDSTATS
1404 struct rq
*rq
= this_rq();
1407 int this_cpu
= smp_processor_id();
1409 if (cpu
== this_cpu
) {
1410 schedstat_inc(rq
, ttwu_local
);
1411 schedstat_inc(p
, se
.statistics
.nr_wakeups_local
);
1413 struct sched_domain
*sd
;
1415 schedstat_inc(p
, se
.statistics
.nr_wakeups_remote
);
1417 for_each_domain(this_cpu
, sd
) {
1418 if (cpumask_test_cpu(cpu
, sched_domain_span(sd
))) {
1419 schedstat_inc(sd
, ttwu_wake_remote
);
1426 if (wake_flags
& WF_MIGRATED
)
1427 schedstat_inc(p
, se
.statistics
.nr_wakeups_migrate
);
1429 #endif /* CONFIG_SMP */
1431 schedstat_inc(rq
, ttwu_count
);
1432 schedstat_inc(p
, se
.statistics
.nr_wakeups
);
1434 if (wake_flags
& WF_SYNC
)
1435 schedstat_inc(p
, se
.statistics
.nr_wakeups_sync
);
1437 #endif /* CONFIG_SCHEDSTATS */
1440 static void ttwu_activate(struct rq
*rq
, struct task_struct
*p
, int en_flags
)
1442 activate_task(rq
, p
, en_flags
);
1445 /* if a worker is waking up, notify workqueue */
1446 if (p
->flags
& PF_WQ_WORKER
)
1447 wq_worker_waking_up(p
, cpu_of(rq
));
1451 * Mark the task runnable and perform wakeup-preemption.
1454 ttwu_do_wakeup(struct rq
*rq
, struct task_struct
*p
, int wake_flags
)
1456 check_preempt_curr(rq
, p
, wake_flags
);
1457 trace_sched_wakeup(p
, true);
1459 p
->state
= TASK_RUNNING
;
1461 if (p
->sched_class
->task_woken
)
1462 p
->sched_class
->task_woken(rq
, p
);
1464 if (rq
->idle_stamp
) {
1465 u64 delta
= rq
->clock
- rq
->idle_stamp
;
1466 u64 max
= 2*sysctl_sched_migration_cost
;
1471 update_avg(&rq
->avg_idle
, delta
);
1478 ttwu_do_activate(struct rq
*rq
, struct task_struct
*p
, int wake_flags
)
1481 if (p
->sched_contributes_to_load
)
1482 rq
->nr_uninterruptible
--;
1485 ttwu_activate(rq
, p
, ENQUEUE_WAKEUP
| ENQUEUE_WAKING
);
1486 ttwu_do_wakeup(rq
, p
, wake_flags
);
1490 * Called in case the task @p isn't fully descheduled from its runqueue,
1491 * in this case we must do a remote wakeup. Its a 'light' wakeup though,
1492 * since all we need to do is flip p->state to TASK_RUNNING, since
1493 * the task is still ->on_rq.
1495 static int ttwu_remote(struct task_struct
*p
, int wake_flags
)
1500 rq
= __task_rq_lock(p
);
1502 ttwu_do_wakeup(rq
, p
, wake_flags
);
1505 __task_rq_unlock(rq
);
1511 static void sched_ttwu_pending(void)
1513 struct rq
*rq
= this_rq();
1514 struct llist_node
*llist
= llist_del_all(&rq
->wake_list
);
1515 struct task_struct
*p
;
1517 raw_spin_lock(&rq
->lock
);
1520 p
= llist_entry(llist
, struct task_struct
, wake_entry
);
1521 llist
= llist_next(llist
);
1522 ttwu_do_activate(rq
, p
, 0);
1525 raw_spin_unlock(&rq
->lock
);
1530 IPI_CALL_FUNC_SINGLE
,
1533 void scheduler_ipi(void)
1535 if (llist_empty(&this_rq()->wake_list
)
1536 && !tick_nohz_full_cpu(smp_processor_id())
1537 && !got_nohz_idle_kick()){
1538 mt_trace_ISR_start(IPI_RESCHEDULE
);
1539 mt_trace_ISR_end(IPI_RESCHEDULE
);
1544 * Not all reschedule IPI handlers call irq_enter/irq_exit, since
1545 * traditionally all their work was done from the interrupt return
1546 * path. Now that we actually do some work, we need to make sure
1549 * Some archs already do call them, luckily irq_enter/exit nest
1552 * Arguably we should visit all archs and update all handlers,
1553 * however a fair share of IPIs are still resched only so this would
1554 * somewhat pessimize the simple resched case.
1557 mt_trace_ISR_start(IPI_RESCHEDULE
);
1558 tick_nohz_full_check();
1559 sched_ttwu_pending();
1562 * Check if someone kicked us for doing the nohz idle load balance.
1564 if (unlikely(got_nohz_idle_kick())) {
1565 this_rq()->idle_balance
= 1;
1566 raise_softirq_irqoff(SCHED_SOFTIRQ
);
1568 mt_trace_ISR_end(IPI_RESCHEDULE
);
1572 static void ttwu_queue_remote(struct task_struct
*p
, int cpu
)
1574 if (llist_add(&p
->wake_entry
, &cpu_rq(cpu
)->wake_list
))
1575 smp_send_reschedule(cpu
);
1578 bool cpus_share_cache(int this_cpu
, int that_cpu
)
1580 return per_cpu(sd_llc_id
, this_cpu
) == per_cpu(sd_llc_id
, that_cpu
);
1582 #endif /* CONFIG_SMP */
1584 static void ttwu_queue(struct task_struct
*p
, int cpu
)
1586 struct rq
*rq
= cpu_rq(cpu
);
1588 #if defined(CONFIG_SMP)
1589 if (sched_feat(TTWU_QUEUE
) && !cpus_share_cache(smp_processor_id(), cpu
)) {
1590 sched_clock_cpu(cpu
); /* sync clocks x-cpu */
1591 ttwu_queue_remote(p
, cpu
);
1596 raw_spin_lock(&rq
->lock
);
1597 ttwu_do_activate(rq
, p
, 0);
1598 raw_spin_unlock(&rq
->lock
);
1602 * try_to_wake_up - wake up a thread
1603 * @p: the thread to be awakened
1604 * @state: the mask of task states that can be woken
1605 * @wake_flags: wake modifier flags (WF_*)
1607 * Put it on the run-queue if it's not already there. The "current"
1608 * thread is always on the run-queue (except when the actual
1609 * re-schedule is in progress), and as such you're allowed to do
1610 * the simpler "current->state = TASK_RUNNING" to mark yourself
1611 * runnable without the overhead of this.
1613 * Returns %true if @p was woken up, %false if it was already running
1614 * or @state didn't match @p's state.
1617 try_to_wake_up(struct task_struct
*p
, unsigned int state
, int wake_flags
)
1619 unsigned long flags
;
1620 int cpu
, success
= 0;
1623 * If we are going to wake up a thread waiting for CONDITION we
1624 * need to ensure that CONDITION=1 done by the caller can not be
1625 * reordered with p->state check below. This pairs with mb() in
1626 * set_current_state() the waiting thread does.
1628 smp_mb__before_spinlock();
1629 raw_spin_lock_irqsave(&p
->pi_lock
, flags
);
1630 if (!(p
->state
& state
))
1633 success
= 1; /* we're going to change ->state */
1637 * Ensure we load p->on_rq _after_ p->state, otherwise it would
1638 * be possible to, falsely, observe p->on_rq == 0 and get stuck
1639 * in smp_cond_load_acquire() below.
1641 * sched_ttwu_pending() try_to_wake_up()
1642 * [S] p->on_rq = 1; [L] P->state
1643 * UNLOCK rq->lock -----.
1647 * LOCK rq->lock -----'
1651 * [S] p->state = UNINTERRUPTIBLE [L] p->on_rq
1653 * Pairs with the UNLOCK+LOCK on rq->lock from the
1654 * last wakeup of our task and the schedule that got our task
1658 if (p
->on_rq
&& ttwu_remote(p
, wake_flags
))
1663 * Ensure we load p->on_cpu _after_ p->on_rq, otherwise it would be
1664 * possible to, falsely, observe p->on_cpu == 0.
1666 * One must be running (->on_cpu == 1) in order to remove oneself
1667 * from the runqueue.
1669 * [S] ->on_cpu = 1; [L] ->on_rq
1673 * [S] ->on_rq = 0; [L] ->on_cpu
1675 * Pairs with the full barrier implied in the UNLOCK+LOCK on rq->lock
1676 * from the consecutive calls to schedule(); the first switching to our
1677 * task, the second putting it to sleep.
1682 * If the owning (remote) cpu is still in the middle of schedule() with
1683 * this task as prev, wait until its done referencing the task.
1688 * Pairs with the smp_wmb() in finish_lock_switch().
1692 p
->sched_contributes_to_load
= !!task_contributes_to_load(p
);
1693 p
->state
= TASK_WAKING
;
1695 if (p
->sched_class
->task_waking
)
1696 p
->sched_class
->task_waking(p
);
1698 cpu
= select_task_rq(p
, SD_BALANCE_WAKE
, wake_flags
);
1699 if (task_cpu(p
) != cpu
) {
1700 #ifdef CONFIG_MT_LOAD_BALANCE_PROFILER
1701 char strings
[128]="";
1703 wake_flags
|= WF_MIGRATED
;
1704 #ifdef CONFIG_MT_LOAD_BALANCE_PROFILER
1705 snprintf(strings
, 128, "%d:%d:%s:wakeup:%d:%d:%s", task_cpu(current
), current
->pid
, current
->comm
, cpu
, p
->pid
, p
->comm
);
1706 trace_sched_lbprof_log(strings
);
1708 set_task_cpu(p
, cpu
);
1710 #endif /* CONFIG_SMP */
1714 ttwu_stat(p
, cpu
, wake_flags
);
1716 raw_spin_unlock_irqrestore(&p
->pi_lock
, flags
);
1722 * try_to_wake_up_local - try to wake up a local task with rq lock held
1723 * @p: the thread to be awakened
1725 * Put @p on the run-queue if it's not already there. The caller must
1726 * ensure that this_rq() is locked, @p is bound to this_rq() and not
1729 static void try_to_wake_up_local(struct task_struct
*p
)
1731 struct rq
*rq
= task_rq(p
);
1733 if (WARN_ON_ONCE(rq
!= this_rq()) ||
1734 WARN_ON_ONCE(p
== current
))
1737 lockdep_assert_held(&rq
->lock
);
1739 if (!raw_spin_trylock(&p
->pi_lock
)) {
1740 raw_spin_unlock(&rq
->lock
);
1741 raw_spin_lock(&p
->pi_lock
);
1742 raw_spin_lock(&rq
->lock
);
1745 if (!(p
->state
& TASK_NORMAL
))
1749 ttwu_activate(rq
, p
, ENQUEUE_WAKEUP
);
1751 ttwu_do_wakeup(rq
, p
, 0);
1752 ttwu_stat(p
, smp_processor_id(), 0);
1754 raw_spin_unlock(&p
->pi_lock
);
1758 * wake_up_process - Wake up a specific process
1759 * @p: The process to be woken up.
1761 * Attempt to wake up the nominated process and move it to the set of runnable
1762 * processes. Returns 1 if the process was woken up, 0 if it was already
1765 * It may be assumed that this function implies a write memory barrier before
1766 * changing the task state if and only if any tasks are woken up.
1768 int wake_up_process(struct task_struct
*p
)
1770 return try_to_wake_up(p
, TASK_NORMAL
, 0);
1772 EXPORT_SYMBOL(wake_up_process
);
1774 int wake_up_state(struct task_struct
*p
, unsigned int state
)
1776 return try_to_wake_up(p
, state
, 0);
1780 * Perform scheduler related setup for a newly forked process p.
1781 * p is forked by current.
1783 * __sched_fork() is basic setup used by init_idle() too:
1785 static void __sched_fork(struct task_struct
*p
)
1790 p
->se
.exec_start
= 0;
1791 p
->se
.sum_exec_runtime
= 0;
1792 p
->se
.prev_sum_exec_runtime
= 0;
1793 p
->se
.nr_migrations
= 0;
1795 INIT_LIST_HEAD(&p
->se
.group_node
);
1798 * Load-tracking only depends on SMP, FAIR_GROUP_SCHED dependency below may be
1799 * removed when useful for applications beyond shares distribution (e.g.
1802 #if defined(CONFIG_SMP) && defined(CONFIG_FAIR_GROUP_SCHED)
1803 p
->se
.avg
.runnable_avg_period
= 0;
1804 p
->se
.avg
.runnable_avg_sum
= 0;
1805 #ifdef CONFIG_SCHED_HMP
1806 /* keep LOAD_AVG_MAX in sync with fair.c if load avg series is changed */
1807 #define LOAD_AVG_MAX 47742
1809 p
->se
.avg
.hmp_last_up_migration
= 0;
1810 p
->se
.avg
.hmp_last_down_migration
= 0;
1811 p
->se
.avg
.load_avg_ratio
= 1023;
1812 p
->se
.avg
.load_avg_contrib
=
1813 (1023 * scale_load_down(p
->se
.load
.weight
));
1814 p
->se
.avg
.runnable_avg_period
= LOAD_AVG_MAX
;
1815 p
->se
.avg
.runnable_avg_sum
= LOAD_AVG_MAX
;
1816 p
->se
.avg
.usage_avg_sum
= LOAD_AVG_MAX
;
1820 #ifdef CONFIG_SCHEDSTATS
1821 memset(&p
->se
.statistics
, 0, sizeof(p
->se
.statistics
));
1824 INIT_LIST_HEAD(&p
->rt
.run_list
);
1826 #ifdef CONFIG_PREEMPT_NOTIFIERS
1827 INIT_HLIST_HEAD(&p
->preempt_notifiers
);
1830 #ifdef CONFIG_NUMA_BALANCING
1831 if (p
->mm
&& atomic_read(&p
->mm
->mm_users
) == 1) {
1832 p
->mm
->numa_next_scan
= jiffies
;
1833 p
->mm
->numa_next_reset
= jiffies
;
1834 p
->mm
->numa_scan_seq
= 0;
1837 p
->node_stamp
= 0ULL;
1838 p
->numa_scan_seq
= p
->mm
? p
->mm
->numa_scan_seq
: 0;
1839 p
->numa_migrate_seq
= p
->mm
? p
->mm
->numa_scan_seq
- 1 : 0;
1840 p
->numa_scan_period
= sysctl_numa_balancing_scan_delay
;
1841 p
->numa_work
.next
= &p
->numa_work
;
1842 #endif /* CONFIG_NUMA_BALANCING */
1845 #ifdef CONFIG_NUMA_BALANCING
1846 #ifdef CONFIG_SCHED_DEBUG
1847 void set_numabalancing_state(bool enabled
)
1850 sched_feat_set("NUMA");
1852 sched_feat_set("NO_NUMA");
1855 __read_mostly
bool numabalancing_enabled
;
1857 void set_numabalancing_state(bool enabled
)
1859 numabalancing_enabled
= enabled
;
1861 #endif /* CONFIG_SCHED_DEBUG */
1862 #endif /* CONFIG_NUMA_BALANCING */
1865 * fork()/clone()-time setup:
1867 void sched_fork(struct task_struct
*p
)
1869 unsigned long flags
;
1870 int cpu
= get_cpu();
1874 * We mark the process as running here. This guarantees that
1875 * nobody will actually run it, and a signal or other external
1876 * event cannot wake it up and insert it on the runqueue either.
1878 p
->state
= TASK_RUNNING
;
1881 * Make sure we do not leak PI boosting priority to the child.
1883 p
->prio
= current
->normal_prio
;
1886 * Revert to default priority/policy on fork if requested.
1888 if (unlikely(p
->sched_reset_on_fork
)) {
1889 if (task_has_rt_policy(p
)) {
1890 p
->policy
= SCHED_NORMAL
;
1891 p
->static_prio
= NICE_TO_PRIO(0);
1893 } else if (PRIO_TO_NICE(p
->static_prio
) < 0)
1894 p
->static_prio
= NICE_TO_PRIO(0);
1896 p
->prio
= p
->normal_prio
= __normal_prio(p
);
1900 * We don't need the reset flag anymore after the fork. It has
1901 * fulfilled its duty:
1903 p
->sched_reset_on_fork
= 0;
1906 if (!rt_prio(p
->prio
))
1907 p
->sched_class
= &fair_sched_class
;
1909 if (p
->sched_class
->task_fork
)
1910 p
->sched_class
->task_fork(p
);
1913 * The child is not yet in the pid-hash so no cgroup attach races,
1914 * and the cgroup is pinned to this child due to cgroup_fork()
1915 * is ran before sched_fork().
1917 * Silence PROVE_RCU.
1919 raw_spin_lock_irqsave(&p
->pi_lock
, flags
);
1920 set_task_cpu(p
, cpu
);
1921 raw_spin_unlock_irqrestore(&p
->pi_lock
, flags
);
1923 #if defined(CONFIG_SCHEDSTATS) || defined(CONFIG_TASK_DELAY_ACCT)
1924 if (likely(sched_info_on()))
1925 memset(&p
->sched_info
, 0, sizeof(p
->sched_info
));
1927 #if defined(CONFIG_SMP)
1930 #ifdef CONFIG_PREEMPT_COUNT
1931 /* Want to start with kernel preemption disabled. */
1932 task_thread_info(p
)->preempt_count
= 1;
1935 plist_node_init(&p
->pushable_tasks
, MAX_PRIO
);
1942 * wake_up_new_task - wake up a newly created task for the first time.
1944 * This function will do some initial scheduler statistics housekeeping
1945 * that must be done for every newly created context, then puts the task
1946 * on the runqueue and wakes it.
1948 void wake_up_new_task(struct task_struct
*p
)
1950 unsigned long flags
;
1953 raw_spin_lock_irqsave(&p
->pi_lock
, flags
);
1956 * Fork balancing, do it here and not earlier because:
1957 * - cpus_allowed can change in the fork path
1958 * - any previously selected cpu might disappear through hotplug
1960 set_task_cpu(p
, select_task_rq(p
, SD_BALANCE_FORK
, 0));
1963 /* Initialize new task's runnable average */
1964 init_task_runnable_average(p
);
1965 rq
= __task_rq_lock(p
);
1966 activate_task(rq
, p
, 0);
1968 trace_sched_wakeup_new(p
, true);
1969 check_preempt_curr(rq
, p
, WF_FORK
);
1971 if (p
->sched_class
->task_woken
)
1972 p
->sched_class
->task_woken(rq
, p
);
1974 task_rq_unlock(rq
, p
, &flags
);
1977 #ifdef CONFIG_PREEMPT_NOTIFIERS
1980 * preempt_notifier_register - tell me when current is being preempted & rescheduled
1981 * @notifier: notifier struct to register
1983 void preempt_notifier_register(struct preempt_notifier
*notifier
)
1985 hlist_add_head(¬ifier
->link
, ¤t
->preempt_notifiers
);
1987 EXPORT_SYMBOL_GPL(preempt_notifier_register
);
1990 * preempt_notifier_unregister - no longer interested in preemption notifications
1991 * @notifier: notifier struct to unregister
1993 * This is safe to call from within a preemption notifier.
1995 void preempt_notifier_unregister(struct preempt_notifier
*notifier
)
1997 hlist_del(¬ifier
->link
);
1999 EXPORT_SYMBOL_GPL(preempt_notifier_unregister
);
2001 static void fire_sched_in_preempt_notifiers(struct task_struct
*curr
)
2003 struct preempt_notifier
*notifier
;
2005 hlist_for_each_entry(notifier
, &curr
->preempt_notifiers
, link
)
2006 notifier
->ops
->sched_in(notifier
, raw_smp_processor_id());
2010 fire_sched_out_preempt_notifiers(struct task_struct
*curr
,
2011 struct task_struct
*next
)
2013 struct preempt_notifier
*notifier
;
2015 hlist_for_each_entry(notifier
, &curr
->preempt_notifiers
, link
)
2016 notifier
->ops
->sched_out(notifier
, next
);
2019 #else /* !CONFIG_PREEMPT_NOTIFIERS */
2021 static void fire_sched_in_preempt_notifiers(struct task_struct
*curr
)
2026 fire_sched_out_preempt_notifiers(struct task_struct
*curr
,
2027 struct task_struct
*next
)
2031 #endif /* CONFIG_PREEMPT_NOTIFIERS */
2034 * prepare_task_switch - prepare to switch tasks
2035 * @rq: the runqueue preparing to switch
2036 * @prev: the current task that is being switched out
2037 * @next: the task we are going to switch to.
2039 * This is called with the rq lock held and interrupts off. It must
2040 * be paired with a subsequent finish_task_switch after the context
2043 * prepare_task_switch sets up locking and calls architecture specific
2047 prepare_task_switch(struct rq
*rq
, struct task_struct
*prev
,
2048 struct task_struct
*next
)
2050 trace_sched_switch(prev
, next
);
2051 sched_info_switch(prev
, next
);
2052 perf_event_task_sched_out(prev
, next
);
2053 fire_sched_out_preempt_notifiers(prev
, next
);
2054 prepare_lock_switch(rq
, next
);
2055 prepare_arch_switch(next
);
2059 * finish_task_switch - clean up after a task-switch
2060 * @rq: runqueue associated with task-switch
2061 * @prev: the thread we just switched away from.
2063 * finish_task_switch must be called after the context switch, paired
2064 * with a prepare_task_switch call before the context switch.
2065 * finish_task_switch will reconcile locking set up by prepare_task_switch,
2066 * and do any other architecture-specific cleanup actions.
2068 * Note that we may have delayed dropping an mm in context_switch(). If
2069 * so, we finish that here outside of the runqueue lock. (Doing it
2070 * with the lock held can cause deadlocks; see schedule() for
2073 static void finish_task_switch(struct rq
*rq
, struct task_struct
*prev
)
2074 __releases(rq
->lock
)
2076 struct mm_struct
*mm
= rq
->prev_mm
;
2082 * A task struct has one reference for the use as "current".
2083 * If a task dies, then it sets TASK_DEAD in tsk->state and calls
2084 * schedule one last time. The schedule call will never return, and
2085 * the scheduled task must drop that reference.
2086 * The test for TASK_DEAD must occur while the runqueue locks are
2087 * still held, otherwise prev could be scheduled on another cpu, die
2088 * there before we look at prev->state, and then the reference would
2090 * Manfred Spraul <manfred@colorfullife.com>
2092 prev_state
= prev
->state
;
2093 vtime_task_switch(prev
);
2094 finish_arch_switch(prev
);
2095 perf_event_task_sched_in(prev
, current
);
2096 finish_lock_switch(rq
, prev
);
2097 finish_arch_post_lock_switch();
2099 fire_sched_in_preempt_notifiers(current
);
2102 if (unlikely(prev_state
== TASK_DEAD
)) {
2104 * Remove function-return probe instances associated with this
2105 * task and put them back on the free list.
2107 kprobe_flush_task(prev
);
2108 put_task_struct(prev
);
2111 tick_nohz_task_switch(current
);
2116 /* assumes rq->lock is held */
2117 static inline void pre_schedule(struct rq
*rq
, struct task_struct
*prev
)
2119 if (prev
->sched_class
->pre_schedule
)
2120 prev
->sched_class
->pre_schedule(rq
, prev
);
2123 /* rq->lock is NOT held, but preemption is disabled */
2124 static inline void post_schedule(struct rq
*rq
)
2126 if (rq
->post_schedule
) {
2127 unsigned long flags
;
2129 raw_spin_lock_irqsave(&rq
->lock
, flags
);
2130 if (rq
->curr
->sched_class
->post_schedule
)
2131 rq
->curr
->sched_class
->post_schedule(rq
);
2132 raw_spin_unlock_irqrestore(&rq
->lock
, flags
);
2134 rq
->post_schedule
= 0;
2140 static inline void pre_schedule(struct rq
*rq
, struct task_struct
*p
)
2144 static inline void post_schedule(struct rq
*rq
)
2151 * schedule_tail - first thing a freshly forked thread must call.
2152 * @prev: the thread we just switched away from.
2154 asmlinkage
void schedule_tail(struct task_struct
*prev
)
2155 __releases(rq
->lock
)
2157 struct rq
*rq
= this_rq();
2159 finish_task_switch(rq
, prev
);
2162 * FIXME: do we need to worry about rq being invalidated by the
2167 #ifdef __ARCH_WANT_UNLOCKED_CTXSW
2168 /* In this case, finish_task_switch does not reenable preemption */
2171 if (current
->set_child_tid
)
2172 put_user(task_pid_vnr(current
), current
->set_child_tid
);
2176 * context_switch - switch to the new MM and the new
2177 * thread's register state.
2180 context_switch(struct rq
*rq
, struct task_struct
*prev
,
2181 struct task_struct
*next
)
2183 struct mm_struct
*mm
, *oldmm
;
2185 prepare_task_switch(rq
, prev
, next
);
2187 #ifdef CONFIG_MT65XX_TRACER
2188 if(get_mt65xx_mon_mode() == MODE_SCHED_SWITCH
)
2189 trace_mt65xx_mon_sched_switch(prev
, next
);
2192 oldmm
= prev
->active_mm
;
2194 * For paravirt, this is coupled with an exit in switch_to to
2195 * combine the page table reload and the switch backend into
2198 arch_start_context_switch(prev
);
2201 next
->active_mm
= oldmm
;
2202 atomic_inc(&oldmm
->mm_count
);
2203 enter_lazy_tlb(oldmm
, next
);
2205 switch_mm(oldmm
, mm
, next
);
2208 prev
->active_mm
= NULL
;
2209 rq
->prev_mm
= oldmm
;
2212 * Since the runqueue lock will be released by the next
2213 * task (which is an invalid locking op but in the case
2214 * of the scheduler it's an obvious special-case), so we
2215 * do an early lockdep release here:
2217 #ifndef __ARCH_WANT_UNLOCKED_CTXSW
2218 spin_release(&rq
->lock
.dep_map
, 1, _THIS_IP_
);
2221 context_tracking_task_switch(prev
, next
);
2222 /* Here we just switch the register state and the stack. */
2223 switch_to(prev
, next
, prev
);
2227 * this_rq must be evaluated again because prev may have moved
2228 * CPUs since it called schedule(), thus the 'rq' on its stack
2229 * frame will be invalid.
2231 finish_task_switch(this_rq(), prev
);
2235 * nr_running and nr_context_switches:
2237 * externally visible scheduler statistics: current number of runnable
2238 * threads, total number of context switches performed since bootup.
2240 unsigned long nr_running(void)
2242 unsigned long i
, sum
= 0;
2244 for_each_online_cpu(i
)
2245 sum
+= cpu_rq(i
)->nr_running
;
2250 unsigned long long nr_context_switches(void)
2253 unsigned long long sum
= 0;
2255 for_each_possible_cpu(i
)
2256 sum
+= cpu_rq(i
)->nr_switches
;
2261 unsigned long nr_iowait(void)
2263 unsigned long i
, sum
= 0;
2265 for_each_possible_cpu(i
)
2266 sum
+= atomic_read(&cpu_rq(i
)->nr_iowait
);
2271 unsigned long nr_iowait_cpu(int cpu
)
2273 struct rq
*this = cpu_rq(cpu
);
2274 return atomic_read(&this->nr_iowait
);
2277 unsigned long this_cpu_load(void)
2279 struct rq
*this = this_rq();
2280 return this->cpu_load
[0];
2283 unsigned long get_cpu_load(int cpu
)
2285 struct rq
*this = cpu_rq(cpu
);
2286 return this->cpu_load
[0];
2288 EXPORT_SYMBOL(get_cpu_load
);
2291 * Global load-average calculations
2293 * We take a distributed and async approach to calculating the global load-avg
2294 * in order to minimize overhead.
2296 * The global load average is an exponentially decaying average of nr_running +
2297 * nr_uninterruptible.
2299 * Once every LOAD_FREQ:
2302 * for_each_possible_cpu(cpu)
2303 * nr_active += cpu_of(cpu)->nr_running + cpu_of(cpu)->nr_uninterruptible;
2305 * avenrun[n] = avenrun[0] * exp_n + nr_active * (1 - exp_n)
2307 * Due to a number of reasons the above turns in the mess below:
2309 * - for_each_possible_cpu() is prohibitively expensive on machines with
2310 * serious number of cpus, therefore we need to take a distributed approach
2311 * to calculating nr_active.
2313 * \Sum_i x_i(t) = \Sum_i x_i(t) - x_i(t_0) | x_i(t_0) := 0
2314 * = \Sum_i { \Sum_j=1 x_i(t_j) - x_i(t_j-1) }
2316 * So assuming nr_active := 0 when we start out -- true per definition, we
2317 * can simply take per-cpu deltas and fold those into a global accumulate
2318 * to obtain the same result. See calc_load_fold_active().
2320 * Furthermore, in order to avoid synchronizing all per-cpu delta folding
2321 * across the machine, we assume 10 ticks is sufficient time for every
2322 * cpu to have completed this task.
2324 * This places an upper-bound on the IRQ-off latency of the machine. Then
2325 * again, being late doesn't loose the delta, just wrecks the sample.
2327 * - cpu_rq()->nr_uninterruptible isn't accurately tracked per-cpu because
2328 * this would add another cross-cpu cacheline miss and atomic operation
2329 * to the wakeup path. Instead we increment on whatever cpu the task ran
2330 * when it went into uninterruptible state and decrement on whatever cpu
2331 * did the wakeup. This means that only the sum of nr_uninterruptible over
2332 * all cpus yields the correct result.
2334 * This covers the NO_HZ=n code, for extra head-aches, see the comment below.
2337 /* Variables and functions for calc_load */
2338 static atomic_long_t calc_load_tasks
;
2339 static unsigned long calc_load_update
;
2340 unsigned long avenrun
[3];
2341 EXPORT_SYMBOL(avenrun
); /* should be removed */
2344 * get_avenrun - get the load average array
2345 * @loads: pointer to dest load array
2346 * @offset: offset to add
2347 * @shift: shift count to shift the result left
2349 * These values are estimates at best, so no need for locking.
2351 void get_avenrun(unsigned long *loads
, unsigned long offset
, int shift
)
2353 loads
[0] = (avenrun
[0] + offset
) << shift
;
2354 loads
[1] = (avenrun
[1] + offset
) << shift
;
2355 loads
[2] = (avenrun
[2] + offset
) << shift
;
2358 static long calc_load_fold_active(struct rq
*this_rq
)
2360 long nr_active
, delta
= 0;
2362 nr_active
= this_rq
->nr_running
;
2363 nr_active
+= (long) this_rq
->nr_uninterruptible
;
2365 if (nr_active
!= this_rq
->calc_load_active
) {
2366 delta
= nr_active
- this_rq
->calc_load_active
;
2367 this_rq
->calc_load_active
= nr_active
;
2374 * a1 = a0 * e + a * (1 - e)
2376 static unsigned long
2377 calc_load(unsigned long load
, unsigned long exp
, unsigned long active
)
2380 load
+= active
* (FIXED_1
- exp
);
2381 load
+= 1UL << (FSHIFT
- 1);
2382 return load
>> FSHIFT
;
2385 #ifdef CONFIG_NO_HZ_COMMON
2387 * Handle NO_HZ for the global load-average.
2389 * Since the above described distributed algorithm to compute the global
2390 * load-average relies on per-cpu sampling from the tick, it is affected by
2393 * The basic idea is to fold the nr_active delta into a global idle-delta upon
2394 * entering NO_HZ state such that we can include this as an 'extra' cpu delta
2395 * when we read the global state.
2397 * Obviously reality has to ruin such a delightfully simple scheme:
2399 * - When we go NO_HZ idle during the window, we can negate our sample
2400 * contribution, causing under-accounting.
2402 * We avoid this by keeping two idle-delta counters and flipping them
2403 * when the window starts, thus separating old and new NO_HZ load.
2405 * The only trick is the slight shift in index flip for read vs write.
2409 * |-|-----------|-|-----------|-|-----------|-|
2410 * r:0 0 1 1 0 0 1 1 0
2411 * w:0 1 1 0 0 1 1 0 0
2413 * This ensures we'll fold the old idle contribution in this window while
2414 * accumlating the new one.
2416 * - When we wake up from NO_HZ idle during the window, we push up our
2417 * contribution, since we effectively move our sample point to a known
2420 * This is solved by pushing the window forward, and thus skipping the
2421 * sample, for this cpu (effectively using the idle-delta for this cpu which
2422 * was in effect at the time the window opened). This also solves the issue
2423 * of having to deal with a cpu having been in NOHZ idle for multiple
2424 * LOAD_FREQ intervals.
2426 * When making the ILB scale, we should try to pull this in as well.
2428 static atomic_long_t calc_load_idle
[2];
2429 static int calc_load_idx
;
2431 static inline int calc_load_write_idx(void)
2433 int idx
= calc_load_idx
;
2436 * See calc_global_nohz(), if we observe the new index, we also
2437 * need to observe the new update time.
2442 * If the folding window started, make sure we start writing in the
2445 if (!time_before(jiffies
, calc_load_update
))
2451 static inline int calc_load_read_idx(void)
2453 return calc_load_idx
& 1;
2456 void calc_load_enter_idle(void)
2458 struct rq
*this_rq
= this_rq();
2462 * We're going into NOHZ mode, if there's any pending delta, fold it
2463 * into the pending idle delta.
2465 delta
= calc_load_fold_active(this_rq
);
2467 int idx
= calc_load_write_idx();
2468 atomic_long_add(delta
, &calc_load_idle
[idx
]);
2472 void calc_load_exit_idle(void)
2474 struct rq
*this_rq
= this_rq();
2477 * If we're still before the sample window, we're done.
2479 if (time_before(jiffies
, this_rq
->calc_load_update
))
2483 * We woke inside or after the sample window, this means we're already
2484 * accounted through the nohz accounting, so skip the entire deal and
2485 * sync up for the next window.
2487 this_rq
->calc_load_update
= calc_load_update
;
2488 if (time_before(jiffies
, this_rq
->calc_load_update
+ 10))
2489 this_rq
->calc_load_update
+= LOAD_FREQ
;
2492 static long calc_load_fold_idle(void)
2494 int idx
= calc_load_read_idx();
2497 if (atomic_long_read(&calc_load_idle
[idx
]))
2498 delta
= atomic_long_xchg(&calc_load_idle
[idx
], 0);
2504 * fixed_power_int - compute: x^n, in O(log n) time
2506 * @x: base of the power
2507 * @frac_bits: fractional bits of @x
2508 * @n: power to raise @x to.
2510 * By exploiting the relation between the definition of the natural power
2511 * function: x^n := x*x*...*x (x multiplied by itself for n times), and
2512 * the binary encoding of numbers used by computers: n := \Sum n_i * 2^i,
2513 * (where: n_i \elem {0, 1}, the binary vector representing n),
2514 * we find: x^n := x^(\Sum n_i * 2^i) := \Prod x^(n_i * 2^i), which is
2515 * of course trivially computable in O(log_2 n), the length of our binary
2518 static unsigned long
2519 fixed_power_int(unsigned long x
, unsigned int frac_bits
, unsigned int n
)
2521 unsigned long result
= 1UL << frac_bits
;
2526 result
+= 1UL << (frac_bits
- 1);
2527 result
>>= frac_bits
;
2533 x
+= 1UL << (frac_bits
- 1);
2541 * a1 = a0 * e + a * (1 - e)
2543 * a2 = a1 * e + a * (1 - e)
2544 * = (a0 * e + a * (1 - e)) * e + a * (1 - e)
2545 * = a0 * e^2 + a * (1 - e) * (1 + e)
2547 * a3 = a2 * e + a * (1 - e)
2548 * = (a0 * e^2 + a * (1 - e) * (1 + e)) * e + a * (1 - e)
2549 * = a0 * e^3 + a * (1 - e) * (1 + e + e^2)
2553 * an = a0 * e^n + a * (1 - e) * (1 + e + ... + e^n-1) [1]
2554 * = a0 * e^n + a * (1 - e) * (1 - e^n)/(1 - e)
2555 * = a0 * e^n + a * (1 - e^n)
2557 * [1] application of the geometric series:
2560 * S_n := \Sum x^i = -------------
2563 static unsigned long
2564 calc_load_n(unsigned long load
, unsigned long exp
,
2565 unsigned long active
, unsigned int n
)
2568 return calc_load(load
, fixed_power_int(exp
, FSHIFT
, n
), active
);
2572 * NO_HZ can leave us missing all per-cpu ticks calling
2573 * calc_load_account_active(), but since an idle CPU folds its delta into
2574 * calc_load_tasks_idle per calc_load_account_idle(), all we need to do is fold
2575 * in the pending idle delta if our idle period crossed a load cycle boundary.
2577 * Once we've updated the global active value, we need to apply the exponential
2578 * weights adjusted to the number of cycles missed.
2580 static void calc_global_nohz(void)
2582 long delta
, active
, n
;
2584 if (!time_before(jiffies
, calc_load_update
+ 10)) {
2586 * Catch-up, fold however many we are behind still
2588 delta
= jiffies
- calc_load_update
- 10;
2589 n
= 1 + (delta
/ LOAD_FREQ
);
2591 active
= atomic_long_read(&calc_load_tasks
);
2592 active
= active
> 0 ? active
* FIXED_1
: 0;
2594 avenrun
[0] = calc_load_n(avenrun
[0], EXP_1
, active
, n
);
2595 avenrun
[1] = calc_load_n(avenrun
[1], EXP_5
, active
, n
);
2596 avenrun
[2] = calc_load_n(avenrun
[2], EXP_15
, active
, n
);
2598 calc_load_update
+= n
* LOAD_FREQ
;
2602 * Flip the idle index...
2604 * Make sure we first write the new time then flip the index, so that
2605 * calc_load_write_idx() will see the new time when it reads the new
2606 * index, this avoids a double flip messing things up.
2611 #else /* !CONFIG_NO_HZ_COMMON */
2613 static inline long calc_load_fold_idle(void) { return 0; }
2614 static inline void calc_global_nohz(void) { }
2616 #endif /* CONFIG_NO_HZ_COMMON */
2619 * calc_load - update the avenrun load estimates 10 ticks after the
2620 * CPUs have updated calc_load_tasks.
2622 void calc_global_load(unsigned long ticks
)
2626 if (time_before(jiffies
, calc_load_update
+ 10))
2630 * Fold the 'old' idle-delta to include all NO_HZ cpus.
2632 delta
= calc_load_fold_idle();
2634 atomic_long_add(delta
, &calc_load_tasks
);
2636 active
= atomic_long_read(&calc_load_tasks
);
2637 active
= active
> 0 ? active
* FIXED_1
: 0;
2639 avenrun
[0] = calc_load(avenrun
[0], EXP_1
, active
);
2640 avenrun
[1] = calc_load(avenrun
[1], EXP_5
, active
);
2641 avenrun
[2] = calc_load(avenrun
[2], EXP_15
, active
);
2643 calc_load_update
+= LOAD_FREQ
;
2646 * In case we idled for multiple LOAD_FREQ intervals, catch up in bulk.
2652 * Called from update_cpu_load() to periodically update this CPU's
2655 static void calc_load_account_active(struct rq
*this_rq
)
2659 if (time_before(jiffies
, this_rq
->calc_load_update
))
2662 delta
= calc_load_fold_active(this_rq
);
2664 atomic_long_add(delta
, &calc_load_tasks
);
2666 this_rq
->calc_load_update
+= LOAD_FREQ
;
2670 * End of global load-average stuff
2674 * The exact cpuload at various idx values, calculated at every tick would be
2675 * load = (2^idx - 1) / 2^idx * load + 1 / 2^idx * cur_load
2677 * If a cpu misses updates for n-1 ticks (as it was idle) and update gets called
2678 * on nth tick when cpu may be busy, then we have:
2679 * load = ((2^idx - 1) / 2^idx)^(n-1) * load
2680 * load = (2^idx - 1) / 2^idx) * load + 1 / 2^idx * cur_load
2682 * decay_load_missed() below does efficient calculation of
2683 * load = ((2^idx - 1) / 2^idx)^(n-1) * load
2684 * avoiding 0..n-1 loop doing load = ((2^idx - 1) / 2^idx) * load
2686 * The calculation is approximated on a 128 point scale.
2687 * degrade_zero_ticks is the number of ticks after which load at any
2688 * particular idx is approximated to be zero.
2689 * degrade_factor is a precomputed table, a row for each load idx.
2690 * Each column corresponds to degradation factor for a power of two ticks,
2691 * based on 128 point scale.
2693 * row 2, col 3 (=12) says that the degradation at load idx 2 after
2694 * 8 ticks is 12/128 (which is an approximation of exact factor 3^8/4^8).
2696 * With this power of 2 load factors, we can degrade the load n times
2697 * by looking at 1 bits in n and doing as many mult/shift instead of
2698 * n mult/shifts needed by the exact degradation.
2700 #define DEGRADE_SHIFT 7
2701 static const unsigned char
2702 degrade_zero_ticks
[CPU_LOAD_IDX_MAX
] = {0, 8, 32, 64, 128};
2703 static const unsigned char
2704 degrade_factor
[CPU_LOAD_IDX_MAX
][DEGRADE_SHIFT
+ 1] = {
2705 {0, 0, 0, 0, 0, 0, 0, 0},
2706 {64, 32, 8, 0, 0, 0, 0, 0},
2707 {96, 72, 40, 12, 1, 0, 0},
2708 {112, 98, 75, 43, 15, 1, 0},
2709 {120, 112, 98, 76, 45, 16, 2} };
2712 * Update cpu_load for any missed ticks, due to tickless idle. The backlog
2713 * would be when CPU is idle and so we just decay the old load without
2714 * adding any new load.
2716 static unsigned long
2717 decay_load_missed(unsigned long load
, unsigned long missed_updates
, int idx
)
2721 if (!missed_updates
)
2724 if (missed_updates
>= degrade_zero_ticks
[idx
])
2728 return load
>> missed_updates
;
2730 while (missed_updates
) {
2731 if (missed_updates
% 2)
2732 load
= (load
* degrade_factor
[idx
][j
]) >> DEGRADE_SHIFT
;
2734 missed_updates
>>= 1;
2741 * Update rq->cpu_load[] statistics. This function is usually called every
2742 * scheduler tick (TICK_NSEC). With tickless idle this will not be called
2743 * every tick. We fix it up based on jiffies.
2745 static void __update_cpu_load(struct rq
*this_rq
, unsigned long this_load
,
2746 unsigned long pending_updates
)
2750 this_rq
->nr_load_updates
++;
2752 /* Update our load: */
2753 this_rq
->cpu_load
[0] = this_load
; /* Fasttrack for idx 0 */
2754 for (i
= 1, scale
= 2; i
< CPU_LOAD_IDX_MAX
; i
++, scale
+= scale
) {
2755 unsigned long old_load
, new_load
;
2757 /* scale is effectively 1 << i now, and >> i divides by scale */
2759 old_load
= this_rq
->cpu_load
[i
];
2760 old_load
= decay_load_missed(old_load
, pending_updates
- 1, i
);
2761 new_load
= this_load
;
2763 * Round up the averaging division if load is increasing. This
2764 * prevents us from getting stuck on 9 if the load is 10, for
2767 if (new_load
> old_load
)
2768 new_load
+= scale
- 1;
2770 this_rq
->cpu_load
[i
] = (old_load
* (scale
- 1) + new_load
) >> i
;
2773 sched_avg_update(this_rq
);
2777 /* moved to kernel/sched/proc.c at Linux 3.11-rc4 */
2778 static inline unsigned long get_rq_runnable_load(struct rq
*rq
)
2780 return rq
->cfs
.runnable_load_avg
;
2783 static inline unsigned long get_rq_runnable_load(struct rq
*rq
)
2785 return rq
->load
.weight
;
2789 #ifdef CONFIG_NO_HZ_COMMON
2791 * There is no sane way to deal with nohz on smp when using jiffies because the
2792 * cpu doing the jiffies update might drift wrt the cpu doing the jiffy reading
2793 * causing off-by-one errors in observed deltas; {0,2} instead of {1,1}.
2795 * Therefore we cannot use the delta approach from the regular tick since that
2796 * would seriously skew the load calculation. However we'll make do for those
2797 * updates happening while idle (nohz_idle_balance) or coming out of idle
2798 * (tick_nohz_idle_exit).
2800 * This means we might still be one tick off for nohz periods.
2804 * Called from nohz_idle_balance() to update the load ratings before doing the
2807 /* moved to kernel/sched/proc.c at Linux 3.11-rc4 */
2808 void update_idle_cpu_load(struct rq
*this_rq
)
2810 unsigned long curr_jiffies
= ACCESS_ONCE(jiffies
);
2811 unsigned long load
= this_rq
->load
.weight
;
2812 unsigned long pending_updates
;
2815 * bail if there's load or we're actually up-to-date.
2817 if (load
|| curr_jiffies
== this_rq
->last_load_update_tick
)
2820 pending_updates
= curr_jiffies
- this_rq
->last_load_update_tick
;
2821 this_rq
->last_load_update_tick
= curr_jiffies
;
2823 __update_cpu_load(this_rq
, load
, pending_updates
);
2827 * Called from tick_nohz_idle_exit() -- try and fix up the ticks we missed.
2829 /* moved to kernel/sched/proc.c at Linux 3.11-rc4 */
2830 void update_cpu_load_nohz(void)
2832 struct rq
*this_rq
= this_rq();
2833 unsigned long curr_jiffies
= ACCESS_ONCE(jiffies
);
2834 unsigned long pending_updates
;
2836 if (curr_jiffies
== this_rq
->last_load_update_tick
)
2839 raw_spin_lock(&this_rq
->lock
);
2840 pending_updates
= curr_jiffies
- this_rq
->last_load_update_tick
;
2841 if (pending_updates
) {
2842 this_rq
->last_load_update_tick
= curr_jiffies
;
2844 * We were idle, this means load 0, the current load might be
2845 * !0 due to remote wakeups and the sort.
2847 __update_cpu_load(this_rq
, 0, pending_updates
);
2849 raw_spin_unlock(&this_rq
->lock
);
2851 #endif /* CONFIG_NO_HZ_COMMON */
2854 * Called from scheduler_tick()
2856 /* moved to kernel/sched/proc.c at Linux 3.11-rc4 */
2857 static void update_cpu_load_active(struct rq
*this_rq
)
2859 unsigned long load
= get_rq_runnable_load(this_rq
);
2861 * See the mess around update_idle_cpu_load() / update_cpu_load_nohz().
2863 this_rq
->last_load_update_tick
= jiffies
;
2864 __update_cpu_load(this_rq
, load
, 1);
2866 calc_load_account_active(this_rq
);
2872 * sched_exec - execve() is a valuable balancing opportunity, because at
2873 * this point the task has the smallest effective memory and cache footprint.
2875 void sched_exec(void)
2877 struct task_struct
*p
= current
;
2878 unsigned long flags
;
2881 raw_spin_lock_irqsave(&p
->pi_lock
, flags
);
2882 dest_cpu
= p
->sched_class
->select_task_rq(p
, SD_BALANCE_EXEC
, 0);
2883 if (dest_cpu
== smp_processor_id())
2886 if (likely(cpu_active(dest_cpu
))) {
2887 struct migration_arg arg
= { p
, dest_cpu
};
2889 raw_spin_unlock_irqrestore(&p
->pi_lock
, flags
);
2890 stop_one_cpu(task_cpu(p
), migration_cpu_stop
, &arg
);
2894 raw_spin_unlock_irqrestore(&p
->pi_lock
, flags
);
2899 DEFINE_PER_CPU(struct kernel_stat
, kstat
);
2900 DEFINE_PER_CPU(struct kernel_cpustat
, kernel_cpustat
);
2902 EXPORT_PER_CPU_SYMBOL(kstat
);
2903 EXPORT_PER_CPU_SYMBOL(kernel_cpustat
);
2906 * Return any ns on the sched_clock that have not yet been accounted in
2907 * @p in case that task is currently running.
2909 * Called with task_rq_lock() held on @rq.
2911 static u64
do_task_delta_exec(struct task_struct
*p
, struct rq
*rq
)
2915 if (task_current(rq
, p
)) {
2916 update_rq_clock(rq
);
2917 ns
= rq
->clock_task
- p
->se
.exec_start
;
2925 unsigned long long task_delta_exec(struct task_struct
*p
)
2927 unsigned long flags
;
2931 rq
= task_rq_lock(p
, &flags
);
2932 ns
= do_task_delta_exec(p
, rq
);
2933 task_rq_unlock(rq
, p
, &flags
);
2939 * Return accounted runtime for the task.
2940 * In case the task is currently running, return the runtime plus current's
2941 * pending runtime that have not been accounted yet.
2943 unsigned long long task_sched_runtime(struct task_struct
*p
)
2945 unsigned long flags
;
2949 rq
= task_rq_lock(p
, &flags
);
2950 ns
= p
->se
.sum_exec_runtime
+ do_task_delta_exec(p
, rq
);
2951 task_rq_unlock(rq
, p
, &flags
);
2957 * This function gets called by the timer code, with HZ frequency.
2958 * We call it with interrupts disabled.
2960 void scheduler_tick(void)
2962 int cpu
= smp_processor_id();
2963 struct rq
*rq
= cpu_rq(cpu
);
2964 struct task_struct
*curr
= rq
->curr
;
2968 raw_spin_lock(&rq
->lock
);
2969 update_rq_clock(rq
);
2970 curr
->sched_class
->task_tick(rq
, curr
, 0);
2971 update_cpu_load_active(rq
);
2972 #ifdef CONFIG_MT_RT_SCHED
2973 mt_check_rt_policy(rq
);
2975 raw_spin_unlock(&rq
->lock
);
2977 perf_event_task_tick();
2978 #ifdef CONFIG_MT_SCHED_MONITOR
2979 if(smp_processor_id() == 0) //only record by CPU#0
2980 mt_save_irq_counts();
2983 rq
->idle_balance
= idle_cpu(cpu
);
2984 trigger_load_balance(rq
, cpu
);
2986 rq_last_tick_reset(rq
);
2989 #ifdef CONFIG_NO_HZ_FULL
2991 * scheduler_tick_max_deferment
2993 * Keep at least one tick per second when a single
2994 * active task is running because the scheduler doesn't
2995 * yet completely support full dynticks environment.
2997 * This makes sure that uptime, CFS vruntime, load
2998 * balancing, etc... continue to move forward, even
2999 * with a very low granularity.
3001 u64
scheduler_tick_max_deferment(void)
3003 struct rq
*rq
= this_rq();
3004 unsigned long next
, now
= ACCESS_ONCE(jiffies
);
3006 next
= rq
->last_sched_tick
+ HZ
;
3008 if (time_before_eq(next
, now
))
3011 return jiffies_to_usecs(next
- now
) * NSEC_PER_USEC
;
3015 notrace
unsigned long get_parent_ip(unsigned long addr
)
3017 if (in_lock_functions(addr
)) {
3018 addr
= CALLER_ADDR2
;
3019 if (in_lock_functions(addr
))
3020 addr
= CALLER_ADDR3
;
3025 #if defined(CONFIG_PREEMPT) && (defined(CONFIG_DEBUG_PREEMPT) || \
3026 defined(CONFIG_PREEMPT_TRACER))
3028 void __kprobes
add_preempt_count(int val
)
3030 #ifdef CONFIG_DEBUG_PREEMPT
3034 if (DEBUG_LOCKS_WARN_ON((preempt_count() < 0)))
3037 preempt_count() += val
;
3038 #ifdef CONFIG_DEBUG_PREEMPT
3040 * Spinlock count overflowing soon?
3042 DEBUG_LOCKS_WARN_ON((preempt_count() & PREEMPT_MASK
) >=
3045 //if (preempt_count() == val)
3046 // trace_preempt_off(CALLER_ADDR0, get_parent_ip(CALLER_ADDR1));
3047 if (preempt_count() == (val
& ~PREEMPT_ACTIVE
)){
3048 #ifdef CONFIG_PREEMPT_TRACER
3049 trace_preempt_off(CALLER_ADDR0
, get_parent_ip(CALLER_ADDR1
));
3051 #ifdef CONFIG_PREEMPT_MONITOR
3052 if(unlikely(__raw_get_cpu_var(mtsched_mon_enabled
) & 0x1)){
3053 //current->t_add_prmpt = sched_clock();
3054 MT_trace_preempt_off();
3059 EXPORT_SYMBOL(add_preempt_count
);
3061 void __kprobes
sub_preempt_count(int val
)
3063 #ifdef CONFIG_DEBUG_PREEMPT
3067 if (DEBUG_LOCKS_WARN_ON(val
> preempt_count()))
3070 * Is the spinlock portion underflowing?
3072 if (DEBUG_LOCKS_WARN_ON((val
< PREEMPT_MASK
) &&
3073 !(preempt_count() & PREEMPT_MASK
)))
3077 //if (preempt_count() == val)
3078 // trace_preempt_on(CALLER_ADDR0, get_parent_ip(CALLER_ADDR1));
3079 if (preempt_count() == (val
& ~PREEMPT_ACTIVE
)){
3080 #ifdef CONFIG_PREEMPT_TRACER
3081 trace_preempt_on(CALLER_ADDR0
, get_parent_ip(CALLER_ADDR1
));
3083 #ifdef CONFIG_PREEMPT_MONITOR
3084 if(unlikely(__raw_get_cpu_var(mtsched_mon_enabled
) & 0x1)){
3085 MT_trace_preempt_on();
3089 preempt_count() -= val
;
3091 EXPORT_SYMBOL(sub_preempt_count
);
3096 * Print scheduling while atomic bug:
3098 static noinline
void __schedule_bug(struct task_struct
*prev
)
3100 if (oops_in_progress
)
3103 printk(KERN_ERR
"BUG: scheduling while atomic: %s/%d/0x%08x\n",
3104 prev
->comm
, prev
->pid
, preempt_count());
3106 debug_show_held_locks(prev
);
3108 if (irqs_disabled())
3109 print_irqtrace_events(prev
);
3111 add_taint(TAINT_WARN
, LOCKDEP_STILL_OK
);
3116 * Various schedule()-time debugging checks and statistics:
3118 static inline void schedule_debug(struct task_struct
*prev
)
3121 * Test if we are atomic. Since do_exit() needs to call into
3122 * schedule() atomically, we ignore that path for now.
3123 * Otherwise, whine if we are scheduling when we should not be.
3125 if (unlikely(in_atomic_preempt_off() && !prev
->exit_state
))
3126 __schedule_bug(prev
);
3129 profile_hit(SCHED_PROFILING
, __builtin_return_address(0));
3131 schedstat_inc(this_rq(), sched_count
);
3134 static void put_prev_task(struct rq
*rq
, struct task_struct
*prev
)
3136 if (prev
->on_rq
|| rq
->skip_clock_update
< 0)
3137 update_rq_clock(rq
);
3138 prev
->sched_class
->put_prev_task(rq
, prev
);
3142 * Pick up the highest-prio task:
3144 static inline struct task_struct
*
3145 pick_next_task(struct rq
*rq
)
3147 const struct sched_class
*class;
3148 struct task_struct
*p
;
3151 * Optimization: we know that if all tasks are in
3152 * the fair class we can call that function directly:
3154 if (likely(rq
->nr_running
== rq
->cfs
.h_nr_running
)) {
3155 p
= fair_sched_class
.pick_next_task(rq
);
3160 for_each_class(class) {
3161 p
= class->pick_next_task(rq
);
3166 BUG(); /* the idle class will always have a runnable task */
3170 * __schedule() is the main scheduler function.
3172 * The main means of driving the scheduler and thus entering this function are:
3174 * 1. Explicit blocking: mutex, semaphore, waitqueue, etc.
3176 * 2. TIF_NEED_RESCHED flag is checked on interrupt and userspace return
3177 * paths. For example, see arch/x86/entry_64.S.
3179 * To drive preemption between tasks, the scheduler sets the flag in timer
3180 * interrupt handler scheduler_tick().
3182 * 3. Wakeups don't really cause entry into schedule(). They add a
3183 * task to the run-queue and that's it.
3185 * Now, if the new task added to the run-queue preempts the current
3186 * task, then the wakeup sets TIF_NEED_RESCHED and schedule() gets
3187 * called on the nearest possible occasion:
3189 * - If the kernel is preemptible (CONFIG_PREEMPT=y):
3191 * - in syscall or exception context, at the next outmost
3192 * preempt_enable(). (this might be as soon as the wake_up()'s
3195 * - in IRQ context, return from interrupt-handler to
3196 * preemptible context
3198 * - If the kernel is not preemptible (CONFIG_PREEMPT is not set)
3201 * - cond_resched() call
3202 * - explicit schedule() call
3203 * - return from syscall or exception to user-space
3204 * - return from interrupt-handler to user-space
3206 static void __sched
__schedule(void)
3208 struct task_struct
*prev
, *next
;
3209 unsigned long *switch_count
;
3215 cpu
= smp_processor_id();
3217 rcu_note_context_switch(cpu
);
3220 schedule_debug(prev
);
3222 if (sched_feat(HRTICK
))
3224 #ifdef CONFIG_MT_SCHED_MONITOR
3225 __raw_get_cpu_var(MT_trace_in_sched
) = 1;
3229 * Make sure that signal_pending_state()->signal_pending() below
3230 * can't be reordered with __set_current_state(TASK_INTERRUPTIBLE)
3231 * done by the caller to avoid the race with signal_wake_up().
3233 smp_mb__before_spinlock();
3234 raw_spin_lock_irq(&rq
->lock
);
3236 switch_count
= &prev
->nivcsw
;
3237 if (prev
->state
&& !(preempt_count() & PREEMPT_ACTIVE
)) {
3238 if (unlikely(signal_pending_state(prev
->state
, prev
))) {
3239 prev
->state
= TASK_RUNNING
;
3241 deactivate_task(rq
, prev
, DEQUEUE_SLEEP
);
3245 * If a worker went to sleep, notify and ask workqueue
3246 * whether it wants to wake up a task to maintain
3249 if (prev
->flags
& PF_WQ_WORKER
) {
3250 struct task_struct
*to_wakeup
;
3252 to_wakeup
= wq_worker_sleeping(prev
, cpu
);
3254 try_to_wake_up_local(to_wakeup
);
3257 switch_count
= &prev
->nvcsw
;
3260 pre_schedule(rq
, prev
);
3262 if (unlikely(!rq
->nr_running
))
3263 idle_balance(cpu
, rq
);
3265 put_prev_task(rq
, prev
);
3266 next
= pick_next_task(rq
);
3267 clear_tsk_need_resched(prev
);
3268 rq
->skip_clock_update
= 0;
3270 if (likely(prev
!= next
)) {
3275 context_switch(rq
, prev
, next
); /* unlocks the rq */
3277 * The context switch have flipped the stack from under us
3278 * and restored the local variables which were saved when
3279 * this task called schedule() in the past. prev == current
3280 * is still correct, but it can be moved to another cpu/rq.
3282 cpu
= smp_processor_id();
3285 raw_spin_unlock_irq(&rq
->lock
);
3287 #ifdef CONFIG_MT_RT_SCHED
3288 mt_post_schedule(rq
);
3290 #ifdef CONFIG_MT_SCHED_MONITOR
3291 __raw_get_cpu_var(MT_trace_in_sched
) = 0;
3295 sched_preempt_enable_no_resched();
3300 static inline void sched_submit_work(struct task_struct
*tsk
)
3302 if (!tsk
->state
|| tsk_is_pi_blocked(tsk
))
3305 * If we are going to sleep and we have plugged IO queued,
3306 * make sure to submit it to avoid deadlocks.
3308 if (blk_needs_flush_plug(tsk
))
3309 blk_schedule_flush_plug(tsk
);
3312 asmlinkage
void __sched
schedule(void)
3314 struct task_struct
*tsk
= current
;
3316 sched_submit_work(tsk
);
3319 EXPORT_SYMBOL(schedule
);
3321 #ifdef CONFIG_CONTEXT_TRACKING
3322 asmlinkage
void __sched
schedule_user(void)
3325 * If we come here after a random call to set_need_resched(),
3326 * or we have been woken up remotely but the IPI has not yet arrived,
3327 * we haven't yet exited the RCU idle mode. Do it here manually until
3328 * we find a better solution.
3337 * schedule_preempt_disabled - called with preemption disabled
3339 * Returns with preemption disabled. Note: preempt_count must be 1
3341 void __sched
schedule_preempt_disabled(void)
3343 sched_preempt_enable_no_resched();
3348 #ifdef CONFIG_PREEMPT
3350 * this is the entry point to schedule() from in-kernel preemption
3351 * off of preempt_enable. Kernel preemptions off return from interrupt
3352 * occur there and call schedule directly.
3354 asmlinkage
void __sched notrace
preempt_schedule(void)
3356 struct thread_info
*ti
= current_thread_info();
3359 * If there is a non-zero preempt_count or interrupts are disabled,
3360 * we do not want to preempt the current task. Just return..
3362 if (likely(ti
->preempt_count
|| irqs_disabled()))
3366 add_preempt_count_notrace(PREEMPT_ACTIVE
);
3368 sub_preempt_count_notrace(PREEMPT_ACTIVE
);
3371 * Check again in case we missed a preemption opportunity
3372 * between schedule and now.
3375 } while (need_resched());
3377 EXPORT_SYMBOL(preempt_schedule
);
3380 * this is the entry point to schedule() from kernel preemption
3381 * off of irq context.
3382 * Note, that this is called and return with irqs disabled. This will
3383 * protect us against recursive calling from irq.
3385 asmlinkage
void __sched
preempt_schedule_irq(void)
3387 struct thread_info
*ti
= current_thread_info();
3388 enum ctx_state prev_state
;
3390 /* Catch callers which need to be fixed */
3391 BUG_ON(ti
->preempt_count
|| !irqs_disabled());
3393 prev_state
= exception_enter();
3396 add_preempt_count(PREEMPT_ACTIVE
);
3399 local_irq_disable();
3400 sub_preempt_count(PREEMPT_ACTIVE
);
3403 * Check again in case we missed a preemption opportunity
3404 * between schedule and now.
3407 } while (need_resched());
3409 exception_exit(prev_state
);
3412 #endif /* CONFIG_PREEMPT */
3414 int default_wake_function(wait_queue_t
*curr
, unsigned mode
, int wake_flags
,
3417 return try_to_wake_up(curr
->private, mode
, wake_flags
);
3419 EXPORT_SYMBOL(default_wake_function
);
3422 * The core wakeup function. Non-exclusive wakeups (nr_exclusive == 0) just
3423 * wake everything up. If it's an exclusive wakeup (nr_exclusive == small +ve
3424 * number) then we wake all the non-exclusive tasks and one exclusive task.
3426 * There are circumstances in which we can try to wake a task which has already
3427 * started to run but is not in state TASK_RUNNING. try_to_wake_up() returns
3428 * zero in this (rare) case, and we handle it by continuing to scan the queue.
3430 static void __wake_up_common(wait_queue_head_t
*q
, unsigned int mode
,
3431 int nr_exclusive
, int wake_flags
, void *key
)
3433 wait_queue_t
*curr
, *next
;
3435 list_for_each_entry_safe(curr
, next
, &q
->task_list
, task_list
) {
3436 unsigned flags
= curr
->flags
;
3438 if (curr
->func(curr
, mode
, wake_flags
, key
) &&
3439 (flags
& WQ_FLAG_EXCLUSIVE
) && !--nr_exclusive
)
3445 * __wake_up - wake up threads blocked on a waitqueue.
3447 * @mode: which threads
3448 * @nr_exclusive: how many wake-one or wake-many threads to wake up
3449 * @key: is directly passed to the wakeup function
3451 * It may be assumed that this function implies a write memory barrier before
3452 * changing the task state if and only if any tasks are woken up.
3454 void __wake_up(wait_queue_head_t
*q
, unsigned int mode
,
3455 int nr_exclusive
, void *key
)
3457 unsigned long flags
;
3459 spin_lock_irqsave(&q
->lock
, flags
);
3460 __wake_up_common(q
, mode
, nr_exclusive
, 0, key
);
3461 spin_unlock_irqrestore(&q
->lock
, flags
);
3463 EXPORT_SYMBOL(__wake_up
);
3466 * Same as __wake_up but called with the spinlock in wait_queue_head_t held.
3468 void __wake_up_locked(wait_queue_head_t
*q
, unsigned int mode
, int nr
)
3470 __wake_up_common(q
, mode
, nr
, 0, NULL
);
3472 EXPORT_SYMBOL_GPL(__wake_up_locked
);
3474 void __wake_up_locked_key(wait_queue_head_t
*q
, unsigned int mode
, void *key
)
3476 __wake_up_common(q
, mode
, 1, 0, key
);
3478 EXPORT_SYMBOL_GPL(__wake_up_locked_key
);
3481 * __wake_up_sync_key - wake up threads blocked on a waitqueue.
3483 * @mode: which threads
3484 * @nr_exclusive: how many wake-one or wake-many threads to wake up
3485 * @key: opaque value to be passed to wakeup targets
3487 * The sync wakeup differs that the waker knows that it will schedule
3488 * away soon, so while the target thread will be woken up, it will not
3489 * be migrated to another CPU - ie. the two threads are 'synchronized'
3490 * with each other. This can prevent needless bouncing between CPUs.
3492 * On UP it can prevent extra preemption.
3494 * It may be assumed that this function implies a write memory barrier before
3495 * changing the task state if and only if any tasks are woken up.
3497 void __wake_up_sync_key(wait_queue_head_t
*q
, unsigned int mode
,
3498 int nr_exclusive
, void *key
)
3500 unsigned long flags
;
3501 int wake_flags
= WF_SYNC
;
3506 if (unlikely(!nr_exclusive
))
3509 spin_lock_irqsave(&q
->lock
, flags
);
3510 __wake_up_common(q
, mode
, nr_exclusive
, wake_flags
, key
);
3511 spin_unlock_irqrestore(&q
->lock
, flags
);
3513 EXPORT_SYMBOL_GPL(__wake_up_sync_key
);
3516 * __wake_up_sync - see __wake_up_sync_key()
3518 void __wake_up_sync(wait_queue_head_t
*q
, unsigned int mode
, int nr_exclusive
)
3520 __wake_up_sync_key(q
, mode
, nr_exclusive
, NULL
);
3522 EXPORT_SYMBOL_GPL(__wake_up_sync
); /* For internal use only */
3525 * complete: - signals a single thread waiting on this completion
3526 * @x: holds the state of this particular completion
3528 * This will wake up a single thread waiting on this completion. Threads will be
3529 * awakened in the same order in which they were queued.
3531 * See also complete_all(), wait_for_completion() and related routines.
3533 * It may be assumed that this function implies a write memory barrier before
3534 * changing the task state if and only if any tasks are woken up.
3536 void complete(struct completion
*x
)
3538 unsigned long flags
;
3540 spin_lock_irqsave(&x
->wait
.lock
, flags
);
3542 __wake_up_common(&x
->wait
, TASK_NORMAL
, 1, 0, NULL
);
3543 spin_unlock_irqrestore(&x
->wait
.lock
, flags
);
3545 EXPORT_SYMBOL(complete
);
3548 * complete_all: - signals all threads waiting on this completion
3549 * @x: holds the state of this particular completion
3551 * This will wake up all threads waiting on this particular completion event.
3553 * It may be assumed that this function implies a write memory barrier before
3554 * changing the task state if and only if any tasks are woken up.
3556 void complete_all(struct completion
*x
)
3558 unsigned long flags
;
3560 spin_lock_irqsave(&x
->wait
.lock
, flags
);
3561 x
->done
+= UINT_MAX
/2;
3562 __wake_up_common(&x
->wait
, TASK_NORMAL
, 0, 0, NULL
);
3563 spin_unlock_irqrestore(&x
->wait
.lock
, flags
);
3565 EXPORT_SYMBOL(complete_all
);
3567 static inline long __sched
3568 do_wait_for_common(struct completion
*x
,
3569 long (*action
)(long), long timeout
, int state
)
3572 DECLARE_WAITQUEUE(wait
, current
);
3574 __add_wait_queue_tail_exclusive(&x
->wait
, &wait
);
3576 if (signal_pending_state(state
, current
)) {
3577 timeout
= -ERESTARTSYS
;
3580 __set_current_state(state
);
3581 spin_unlock_irq(&x
->wait
.lock
);
3582 timeout
= action(timeout
);
3583 spin_lock_irq(&x
->wait
.lock
);
3584 } while (!x
->done
&& timeout
);
3585 __remove_wait_queue(&x
->wait
, &wait
);
3590 return timeout
?: 1;
3593 static inline long __sched
3594 __wait_for_common(struct completion
*x
,
3595 long (*action
)(long), long timeout
, int state
)
3599 spin_lock_irq(&x
->wait
.lock
);
3600 timeout
= do_wait_for_common(x
, action
, timeout
, state
);
3601 spin_unlock_irq(&x
->wait
.lock
);
3606 wait_for_common(struct completion
*x
, long timeout
, int state
)
3608 return __wait_for_common(x
, schedule_timeout
, timeout
, state
);
3612 wait_for_common_io(struct completion
*x
, long timeout
, int state
)
3614 return __wait_for_common(x
, io_schedule_timeout
, timeout
, state
);
3618 * wait_for_completion: - waits for completion of a task
3619 * @x: holds the state of this particular completion
3621 * This waits to be signaled for completion of a specific task. It is NOT
3622 * interruptible and there is no timeout.
3624 * See also similar routines (i.e. wait_for_completion_timeout()) with timeout
3625 * and interrupt capability. Also see complete().
3627 void __sched
wait_for_completion(struct completion
*x
)
3629 wait_for_common(x
, MAX_SCHEDULE_TIMEOUT
, TASK_UNINTERRUPTIBLE
);
3631 EXPORT_SYMBOL(wait_for_completion
);
3634 * wait_for_completion_timeout: - waits for completion of a task (w/timeout)
3635 * @x: holds the state of this particular completion
3636 * @timeout: timeout value in jiffies
3638 * This waits for either a completion of a specific task to be signaled or for a
3639 * specified timeout to expire. The timeout is in jiffies. It is not
3642 * The return value is 0 if timed out, and positive (at least 1, or number of
3643 * jiffies left till timeout) if completed.
3645 unsigned long __sched
3646 wait_for_completion_timeout(struct completion
*x
, unsigned long timeout
)
3648 return wait_for_common(x
, timeout
, TASK_UNINTERRUPTIBLE
);
3650 EXPORT_SYMBOL(wait_for_completion_timeout
);
3653 * wait_for_completion_io: - waits for completion of a task
3654 * @x: holds the state of this particular completion
3656 * This waits to be signaled for completion of a specific task. It is NOT
3657 * interruptible and there is no timeout. The caller is accounted as waiting
3660 void __sched
wait_for_completion_io(struct completion
*x
)
3662 wait_for_common_io(x
, MAX_SCHEDULE_TIMEOUT
, TASK_UNINTERRUPTIBLE
);
3664 EXPORT_SYMBOL(wait_for_completion_io
);
3667 * wait_for_completion_io_timeout: - waits for completion of a task (w/timeout)
3668 * @x: holds the state of this particular completion
3669 * @timeout: timeout value in jiffies
3671 * This waits for either a completion of a specific task to be signaled or for a
3672 * specified timeout to expire. The timeout is in jiffies. It is not
3673 * interruptible. The caller is accounted as waiting for IO.
3675 * The return value is 0 if timed out, and positive (at least 1, or number of
3676 * jiffies left till timeout) if completed.
3678 unsigned long __sched
3679 wait_for_completion_io_timeout(struct completion
*x
, unsigned long timeout
)
3681 return wait_for_common_io(x
, timeout
, TASK_UNINTERRUPTIBLE
);
3683 EXPORT_SYMBOL(wait_for_completion_io_timeout
);
3686 * wait_for_completion_interruptible: - waits for completion of a task (w/intr)
3687 * @x: holds the state of this particular completion
3689 * This waits for completion of a specific task to be signaled. It is
3692 * The return value is -ERESTARTSYS if interrupted, 0 if completed.
3694 int __sched
wait_for_completion_interruptible(struct completion
*x
)
3696 long t
= wait_for_common(x
, MAX_SCHEDULE_TIMEOUT
, TASK_INTERRUPTIBLE
);
3697 if (t
== -ERESTARTSYS
)
3701 EXPORT_SYMBOL(wait_for_completion_interruptible
);
3704 * wait_for_completion_interruptible_timeout: - waits for completion (w/(to,intr))
3705 * @x: holds the state of this particular completion
3706 * @timeout: timeout value in jiffies
3708 * This waits for either a completion of a specific task to be signaled or for a
3709 * specified timeout to expire. It is interruptible. The timeout is in jiffies.
3711 * The return value is -ERESTARTSYS if interrupted, 0 if timed out,
3712 * positive (at least 1, or number of jiffies left till timeout) if completed.
3715 wait_for_completion_interruptible_timeout(struct completion
*x
,
3716 unsigned long timeout
)
3718 return wait_for_common(x
, timeout
, TASK_INTERRUPTIBLE
);
3720 EXPORT_SYMBOL(wait_for_completion_interruptible_timeout
);
3723 * wait_for_completion_killable: - waits for completion of a task (killable)
3724 * @x: holds the state of this particular completion
3726 * This waits to be signaled for completion of a specific task. It can be
3727 * interrupted by a kill signal.
3729 * The return value is -ERESTARTSYS if interrupted, 0 if completed.
3731 int __sched
wait_for_completion_killable(struct completion
*x
)
3733 long t
= wait_for_common(x
, MAX_SCHEDULE_TIMEOUT
, TASK_KILLABLE
);
3734 if (t
== -ERESTARTSYS
)
3738 EXPORT_SYMBOL(wait_for_completion_killable
);
3741 * wait_for_completion_killable_timeout: - waits for completion of a task (w/(to,killable))
3742 * @x: holds the state of this particular completion
3743 * @timeout: timeout value in jiffies
3745 * This waits for either a completion of a specific task to be
3746 * signaled or for a specified timeout to expire. It can be
3747 * interrupted by a kill signal. The timeout is in jiffies.
3749 * The return value is -ERESTARTSYS if interrupted, 0 if timed out,
3750 * positive (at least 1, or number of jiffies left till timeout) if completed.
3753 wait_for_completion_killable_timeout(struct completion
*x
,
3754 unsigned long timeout
)
3756 return wait_for_common(x
, timeout
, TASK_KILLABLE
);
3758 EXPORT_SYMBOL(wait_for_completion_killable_timeout
);
3761 * try_wait_for_completion - try to decrement a completion without blocking
3762 * @x: completion structure
3764 * Returns: 0 if a decrement cannot be done without blocking
3765 * 1 if a decrement succeeded.
3767 * If a completion is being used as a counting completion,
3768 * attempt to decrement the counter without blocking. This
3769 * enables us to avoid waiting if the resource the completion
3770 * is protecting is not available.
3772 bool try_wait_for_completion(struct completion
*x
)
3774 unsigned long flags
;
3777 spin_lock_irqsave(&x
->wait
.lock
, flags
);
3782 spin_unlock_irqrestore(&x
->wait
.lock
, flags
);
3785 EXPORT_SYMBOL(try_wait_for_completion
);
3788 * completion_done - Test to see if a completion has any waiters
3789 * @x: completion structure
3791 * Returns: 0 if there are waiters (wait_for_completion() in progress)
3792 * 1 if there are no waiters.
3795 bool completion_done(struct completion
*x
)
3797 unsigned long flags
;
3800 spin_lock_irqsave(&x
->wait
.lock
, flags
);
3803 spin_unlock_irqrestore(&x
->wait
.lock
, flags
);
3806 EXPORT_SYMBOL(completion_done
);
3809 sleep_on_common(wait_queue_head_t
*q
, int state
, long timeout
)
3811 unsigned long flags
;
3814 init_waitqueue_entry(&wait
, current
);
3816 __set_current_state(state
);
3818 spin_lock_irqsave(&q
->lock
, flags
);
3819 __add_wait_queue(q
, &wait
);
3820 spin_unlock(&q
->lock
);
3821 timeout
= schedule_timeout(timeout
);
3822 spin_lock_irq(&q
->lock
);
3823 __remove_wait_queue(q
, &wait
);
3824 spin_unlock_irqrestore(&q
->lock
, flags
);
3829 void __sched
interruptible_sleep_on(wait_queue_head_t
*q
)
3831 sleep_on_common(q
, TASK_INTERRUPTIBLE
, MAX_SCHEDULE_TIMEOUT
);
3833 EXPORT_SYMBOL(interruptible_sleep_on
);
3836 interruptible_sleep_on_timeout(wait_queue_head_t
*q
, long timeout
)
3838 return sleep_on_common(q
, TASK_INTERRUPTIBLE
, timeout
);
3840 EXPORT_SYMBOL(interruptible_sleep_on_timeout
);
3842 void __sched
sleep_on(wait_queue_head_t
*q
)
3844 sleep_on_common(q
, TASK_UNINTERRUPTIBLE
, MAX_SCHEDULE_TIMEOUT
);
3846 EXPORT_SYMBOL(sleep_on
);
3848 long __sched
sleep_on_timeout(wait_queue_head_t
*q
, long timeout
)
3850 return sleep_on_common(q
, TASK_UNINTERRUPTIBLE
, timeout
);
3852 EXPORT_SYMBOL(sleep_on_timeout
);
3854 #ifdef CONFIG_RT_MUTEXES
3857 * rt_mutex_setprio - set the current priority of a task
3859 * @prio: prio value (kernel-internal form)
3861 * This function changes the 'effective' priority of a task. It does
3862 * not touch ->normal_prio like __setscheduler().
3864 * Used by the rt_mutex code to implement priority inheritance logic.
3866 void rt_mutex_setprio(struct task_struct
*p
, int prio
)
3868 int oldprio
, on_rq
, running
;
3870 const struct sched_class
*prev_class
;
3872 BUG_ON(prio
< 0 || prio
> MAX_PRIO
);
3874 rq
= __task_rq_lock(p
);
3877 * Idle task boosting is a nono in general. There is one
3878 * exception, when PREEMPT_RT and NOHZ is active:
3880 * The idle task calls get_next_timer_interrupt() and holds
3881 * the timer wheel base->lock on the CPU and another CPU wants
3882 * to access the timer (probably to cancel it). We can safely
3883 * ignore the boosting request, as the idle CPU runs this code
3884 * with interrupts disabled and will complete the lock
3885 * protected section without being interrupted. So there is no
3886 * real need to boost.
3888 if (unlikely(p
== rq
->idle
)) {
3889 WARN_ON(p
!= rq
->curr
);
3890 WARN_ON(p
->pi_blocked_on
);
3894 trace_sched_pi_setprio(p
, prio
);
3896 prev_class
= p
->sched_class
;
3898 running
= task_current(rq
, p
);
3900 dequeue_task(rq
, p
, 0);
3902 p
->sched_class
->put_prev_task(rq
, p
);
3905 p
->sched_class
= &rt_sched_class
;
3907 p
->sched_class
= &fair_sched_class
;
3912 p
->sched_class
->set_curr_task(rq
);
3914 enqueue_task(rq
, p
, oldprio
< prio
? ENQUEUE_HEAD
: 0);
3916 check_class_changed(rq
, p
, prev_class
, oldprio
);
3918 __task_rq_unlock(rq
);
3922 #ifdef CONFIG_MT_PRIO_TRACER
3923 void set_user_nice_core(struct task_struct
*p
, long nice
)
3925 int old_prio
, delta
, on_rq
;
3926 unsigned long flags
;
3929 if (TASK_NICE(p
) == nice
|| nice
< -20 || nice
> 19)
3932 * We have to be careful, if called from sys_setpriority(),
3933 * the task might be in the middle of scheduling on another CPU.
3935 rq
= task_rq_lock(p
, &flags
);
3937 * The RT priorities are set via sched_setscheduler(), but we still
3938 * allow the 'normal' nice value to be set - but as expected
3939 * it wont have any effect on scheduling until the task is
3940 * SCHED_FIFO/SCHED_RR:
3942 if (task_has_rt_policy(p
)) {
3943 p
->static_prio
= NICE_TO_PRIO(nice
);
3948 dequeue_task(rq
, p
, 0);
3950 p
->static_prio
= NICE_TO_PRIO(nice
);
3953 p
->prio
= effective_prio(p
);
3954 delta
= p
->prio
- old_prio
;
3957 enqueue_task(rq
, p
, 0);
3959 * If the task increased its priority or is running and
3960 * lowered its priority, then reschedule its CPU:
3962 if (delta
< 0 || (delta
> 0 && task_running(rq
, p
)))
3963 resched_task(rq
->curr
);
3966 task_rq_unlock(rq
, p
, &flags
);
3969 void set_user_nice(struct task_struct
*p
, long nice
)
3971 set_user_nice_core(p
, nice
);
3972 /* setting nice implies to set a normal sched policy */
3973 update_prio_tracer(task_pid_nr(p
), NICE_TO_PRIO(nice
), 0, PTS_KRNL
);
3975 #else /* !CONFIG_MT_PRIO_TRACER */
3976 void set_user_nice(struct task_struct
*p
, long nice
)
3978 int old_prio
, delta
, on_rq
;
3979 unsigned long flags
;
3982 if (TASK_NICE(p
) == nice
|| nice
< -20 || nice
> 19)
3985 * We have to be careful, if called from sys_setpriority(),
3986 * the task might be in the middle of scheduling on another CPU.
3988 rq
= task_rq_lock(p
, &flags
);
3990 * The RT priorities are set via sched_setscheduler(), but we still
3991 * allow the 'normal' nice value to be set - but as expected
3992 * it wont have any effect on scheduling until the task is
3993 * SCHED_FIFO/SCHED_RR:
3995 if (task_has_rt_policy(p
)) {
3996 p
->static_prio
= NICE_TO_PRIO(nice
);
4001 dequeue_task(rq
, p
, 0);
4003 p
->static_prio
= NICE_TO_PRIO(nice
);
4006 p
->prio
= effective_prio(p
);
4007 delta
= p
->prio
- old_prio
;
4010 enqueue_task(rq
, p
, 0);
4012 * If the task increased its priority or is running and
4013 * lowered its priority, then reschedule its CPU:
4015 if (delta
< 0 || (delta
> 0 && task_running(rq
, p
)))
4016 resched_task(rq
->curr
);
4019 task_rq_unlock(rq
, p
, &flags
);
4022 EXPORT_SYMBOL(set_user_nice
);
4025 * can_nice - check if a task can reduce its nice value
4029 int can_nice(const struct task_struct
*p
, const int nice
)
4031 /* convert nice value [19,-20] to rlimit style value [1,40] */
4032 int nice_rlim
= 20 - nice
;
4034 return (nice_rlim
<= task_rlimit(p
, RLIMIT_NICE
) ||
4035 capable(CAP_SYS_NICE
));
4038 #ifdef __ARCH_WANT_SYS_NICE
4041 * sys_nice - change the priority of the current process.
4042 * @increment: priority increment
4044 * sys_setpriority is a more generic, but much slower function that
4045 * does similar things.
4047 SYSCALL_DEFINE1(nice
, int, increment
)
4052 * Setpriority might change our priority at the same moment.
4053 * We don't have to worry. Conceptually one call occurs first
4054 * and we have a single winner.
4056 if (increment
< -40)
4061 nice
= TASK_NICE(current
) + increment
;
4067 if (increment
< 0 && !can_nice(current
, nice
))
4070 retval
= security_task_setnice(current
, nice
);
4073 #ifdef CONFIG_MT_PRIO_TRACER
4074 set_user_nice_syscall(current
, nice
);
4076 set_user_nice(current
, nice
);
4084 * task_prio - return the priority value of a given task.
4085 * @p: the task in question.
4087 * This is the priority value as seen by users in /proc.
4088 * RT tasks are offset by -200. Normal tasks are centered
4089 * around 0, value goes from -16 to +15.
4091 int task_prio(const struct task_struct
*p
)
4093 return p
->prio
- MAX_RT_PRIO
;
4097 * task_nice - return the nice value of a given task.
4098 * @p: the task in question.
4100 int task_nice(const struct task_struct
*p
)
4102 return TASK_NICE(p
);
4104 EXPORT_SYMBOL(task_nice
);
4107 * idle_cpu - is a given cpu idle currently?
4108 * @cpu: the processor in question.
4110 int idle_cpu(int cpu
)
4112 struct rq
*rq
= cpu_rq(cpu
);
4114 if (rq
->curr
!= rq
->idle
)
4121 if (!llist_empty(&rq
->wake_list
))
4129 * idle_task - return the idle task for a given cpu.
4130 * @cpu: the processor in question.
4132 struct task_struct
*idle_task(int cpu
)
4134 return cpu_rq(cpu
)->idle
;
4138 * find_process_by_pid - find a process with a matching PID value.
4139 * @pid: the pid in question.
4141 static struct task_struct
*find_process_by_pid(pid_t pid
)
4143 return pid
? find_task_by_vpid(pid
) : current
;
4146 extern struct cpumask hmp_slow_cpu_mask
;
4148 /* Actually do priority change: must hold rq lock. */
4150 __setscheduler(struct rq
*rq
, struct task_struct
*p
, int policy
, int prio
)
4153 p
->rt_priority
= prio
;
4154 p
->normal_prio
= normal_prio(p
);
4155 /* we are holding p->pi_lock already */
4156 p
->prio
= rt_mutex_getprio(p
);
4157 if (rt_prio(p
->prio
)) {
4158 p
->sched_class
= &rt_sched_class
;
4161 p
->sched_class
= &fair_sched_class
;
4166 * check the target process has a UID that matches the current process's
4168 static bool check_same_owner(struct task_struct
*p
)
4170 const struct cred
*cred
= current_cred(), *pcred
;
4174 pcred
= __task_cred(p
);
4175 match
= (uid_eq(cred
->euid
, pcred
->euid
) ||
4176 uid_eq(cred
->euid
, pcred
->uid
));
4181 static int check_mt_allow_rt(struct sched_param
*param
)
4184 if(0 == MT_ALLOW_RT_PRIO_BIT
){
4185 //this condition check will be removed
4189 if(param
->sched_priority
& MT_ALLOW_RT_PRIO_BIT
){
4190 param
->sched_priority
&= ~MT_ALLOW_RT_PRIO_BIT
;
4196 static int __sched_setscheduler(struct task_struct
*p
, int policy
,
4197 const struct sched_param
*param
, bool user
)
4199 int retval
, oldprio
, oldpolicy
= -1, on_rq
, running
;
4200 unsigned long flags
;
4201 const struct sched_class
*prev_class
;
4205 /* may grab non-irq protected spin_locks */
4206 BUG_ON(in_interrupt());
4208 /* double check policy once rq lock held */
4210 reset_on_fork
= p
->sched_reset_on_fork
;
4211 policy
= oldpolicy
= p
->policy
;
4213 reset_on_fork
= !!(policy
& SCHED_RESET_ON_FORK
);
4214 policy
&= ~SCHED_RESET_ON_FORK
;
4216 if (policy
!= SCHED_FIFO
&& policy
!= SCHED_RR
&&
4217 policy
!= SCHED_NORMAL
&& policy
!= SCHED_BATCH
&&
4218 policy
!= SCHED_IDLE
)
4222 if(rt_policy(policy
)){
4223 if (!check_mt_allow_rt((struct sched_param
*)param
)){
4224 printk("[RT_MONITOR]WARNNING [%d:%s] SET NOT ALLOW RT Prio [%d] for proc [%d:%s]\n", current
->pid
, current
->comm
, param
->sched_priority
, p
->pid
, p
->comm
);
4230 * Valid priorities for SCHED_FIFO and SCHED_RR are
4231 * 1..MAX_USER_RT_PRIO-1, valid priority for SCHED_NORMAL,
4232 * SCHED_BATCH and SCHED_IDLE is 0.
4234 if (param
->sched_priority
< 0 ||
4235 (p
->mm
&& param
->sched_priority
> MAX_USER_RT_PRIO
-1) ||
4236 (!p
->mm
&& param
->sched_priority
> MAX_RT_PRIO
-1))
4238 if (rt_policy(policy
) != (param
->sched_priority
!= 0))
4242 * Allow unprivileged RT tasks to decrease priority:
4244 if (user
&& !capable(CAP_SYS_NICE
)) {
4245 if (rt_policy(policy
)) {
4246 unsigned long rlim_rtprio
=
4247 task_rlimit(p
, RLIMIT_RTPRIO
);
4249 /* can't set/change the rt policy */
4250 if (policy
!= p
->policy
&& !rlim_rtprio
)
4253 /* can't increase priority */
4254 if (param
->sched_priority
> p
->rt_priority
&&
4255 param
->sched_priority
> rlim_rtprio
)
4260 * Treat SCHED_IDLE as nice 20. Only allow a switch to
4261 * SCHED_NORMAL if the RLIMIT_NICE would normally permit it.
4263 if (p
->policy
== SCHED_IDLE
&& policy
!= SCHED_IDLE
) {
4264 if (!can_nice(p
, TASK_NICE(p
)))
4268 /* can't change other user's priorities */
4269 if (!check_same_owner(p
))
4272 /* Normal users shall not reset the sched_reset_on_fork flag */
4273 if (p
->sched_reset_on_fork
&& !reset_on_fork
)
4278 retval
= security_task_setscheduler(p
);
4284 * make sure no PI-waiters arrive (or leave) while we are
4285 * changing the priority of the task:
4287 * To be able to change p->policy safely, the appropriate
4288 * runqueue lock must be held.
4290 rq
= task_rq_lock(p
, &flags
);
4293 * Changing the policy of the stop threads its a very bad idea
4295 if (p
== rq
->stop
) {
4296 task_rq_unlock(rq
, p
, &flags
);
4301 * If not changing anything there's no need to proceed further:
4303 if (unlikely(policy
== p
->policy
&& (!rt_policy(policy
) ||
4304 param
->sched_priority
== p
->rt_priority
))) {
4305 task_rq_unlock(rq
, p
, &flags
);
4309 #ifdef CONFIG_RT_GROUP_SCHED
4312 * Do not allow realtime tasks into groups that have no runtime
4315 if (rt_bandwidth_enabled() && rt_policy(policy
) &&
4316 task_group(p
)->rt_bandwidth
.rt_runtime
== 0 &&
4317 !task_group_is_autogroup(task_group(p
))) {
4318 task_rq_unlock(rq
, p
, &flags
);
4324 /* recheck policy now with rq lock held */
4325 if (unlikely(oldpolicy
!= -1 && oldpolicy
!= p
->policy
)) {
4326 policy
= oldpolicy
= -1;
4327 task_rq_unlock(rq
, p
, &flags
);
4331 running
= task_current(rq
, p
);
4333 dequeue_task(rq
, p
, 0);
4335 p
->sched_class
->put_prev_task(rq
, p
);
4337 p
->sched_reset_on_fork
= reset_on_fork
;
4340 prev_class
= p
->sched_class
;
4341 __setscheduler(rq
, p
, policy
, param
->sched_priority
);
4344 p
->sched_class
->set_curr_task(rq
);
4346 enqueue_task(rq
, p
, 0);
4348 check_class_changed(rq
, p
, prev_class
, oldprio
);
4349 task_rq_unlock(rq
, p
, &flags
);
4351 rt_mutex_adjust_pi(p
);
4357 * sched_setscheduler - change the scheduling policy and/or RT priority of a thread.
4358 * @p: the task in question.
4359 * @policy: new policy.
4360 * @param: structure containing the new RT priority.
4362 * NOTE that the task may be already dead.
4364 #ifdef CONFIG_MT_PRIO_TRACER
4365 int sched_setscheduler_core(struct task_struct
*p
, int policy
,
4366 const struct sched_param
*param
)
4368 return __sched_setscheduler(p
, policy
, param
, true);
4371 int sched_setscheduler(struct task_struct
*p
, int policy
,
4372 const struct sched_param
*param
)
4376 retval
= sched_setscheduler_core(p
, policy
, param
);
4378 int prio
= param
->sched_priority
& ~MT_ALLOW_RT_PRIO_BIT
;
4379 if (!rt_policy(policy
))
4380 prio
= __normal_prio(p
);
4382 prio
= MAX_RT_PRIO
-1 - prio
;
4383 update_prio_tracer(task_pid_nr(p
), prio
, policy
, PTS_KRNL
);
4387 #else /* !CONFIG_MT_PRIO_TRACER */
4388 int sched_setscheduler(struct task_struct
*p
, int policy
,
4389 const struct sched_param
*param
)
4391 return __sched_setscheduler(p
, policy
, param
, true);
4394 EXPORT_SYMBOL_GPL(sched_setscheduler
);
4397 * sched_setscheduler_nocheck - change the scheduling policy and/or RT priority of a thread from kernelspace.
4398 * @p: the task in question.
4399 * @policy: new policy.
4400 * @param: structure containing the new RT priority.
4402 * Just like sched_setscheduler, only don't bother checking if the
4403 * current context has permission. For example, this is needed in
4404 * stop_machine(): we create temporary high priority worker threads,
4405 * but our caller might not have that capability.
4407 #ifdef CONFIG_MT_PRIO_TRACER
4408 int sched_setscheduler_nocheck_core(struct task_struct
*p
, int policy
,
4409 const struct sched_param
*param
)
4411 return __sched_setscheduler(p
, policy
, param
, false);
4415 int sched_setscheduler_nocheck(struct task_struct
*p
, int policy
,
4416 const struct sched_param
*param
)
4420 retval
= sched_setscheduler_nocheck_core(p
, policy
, param
);
4422 int prio
= param
->sched_priority
& ~MT_ALLOW_RT_PRIO_BIT
;
4423 if (!rt_policy(policy
))
4424 prio
= __normal_prio(p
);
4426 prio
= MAX_RT_PRIO
-1 - prio
;
4427 update_prio_tracer(task_pid_nr(p
), prio
, policy
, PTS_KRNL
);
4431 #else /* !CONFIG_MT_PRIO_TRACER */
4432 int sched_setscheduler_nocheck(struct task_struct
*p
, int policy
,
4433 const struct sched_param
*param
)
4435 return __sched_setscheduler(p
, policy
, param
, false);
4440 do_sched_setscheduler(pid_t pid
, int policy
, struct sched_param __user
*param
)
4442 struct sched_param lparam
;
4443 struct task_struct
*p
;
4446 if (!param
|| pid
< 0)
4448 if (copy_from_user(&lparam
, param
, sizeof(struct sched_param
)))
4453 p
= find_process_by_pid(pid
);
4454 #ifdef CONFIG_MT_PRIO_TRACER
4456 retval
= sched_setscheduler_syscall(p
, policy
, &lparam
);
4459 retval
= sched_setscheduler(p
, policy
, &lparam
);
4468 * sys_sched_setscheduler - set/change the scheduler policy and RT priority
4469 * @pid: the pid in question.
4470 * @policy: new policy.
4471 * @param: structure containing the new RT priority.
4473 SYSCALL_DEFINE3(sched_setscheduler
, pid_t
, pid
, int, policy
,
4474 struct sched_param __user
*, param
)
4476 /* negative values for policy are not valid */
4480 return do_sched_setscheduler(pid
, policy
, param
);
4484 * sys_sched_setparam - set/change the RT priority of a thread
4485 * @pid: the pid in question.
4486 * @param: structure containing the new RT priority.
4488 SYSCALL_DEFINE2(sched_setparam
, pid_t
, pid
, struct sched_param __user
*, param
)
4490 return do_sched_setscheduler(pid
, -1, param
);
4494 * sys_sched_getscheduler - get the policy (scheduling class) of a thread
4495 * @pid: the pid in question.
4497 SYSCALL_DEFINE1(sched_getscheduler
, pid_t
, pid
)
4499 struct task_struct
*p
;
4507 p
= find_process_by_pid(pid
);
4509 retval
= security_task_getscheduler(p
);
4512 | (p
->sched_reset_on_fork
? SCHED_RESET_ON_FORK
: 0);
4519 * sys_sched_getparam - get the RT priority of a thread
4520 * @pid: the pid in question.
4521 * @param: structure containing the RT priority.
4523 SYSCALL_DEFINE2(sched_getparam
, pid_t
, pid
, struct sched_param __user
*, param
)
4525 struct sched_param lp
;
4526 struct task_struct
*p
;
4529 if (!param
|| pid
< 0)
4533 p
= find_process_by_pid(pid
);
4538 retval
= security_task_getscheduler(p
);
4542 lp
.sched_priority
= p
->rt_priority
;
4546 * This one might sleep, we cannot do it with a spinlock held ...
4548 retval
= copy_to_user(param
, &lp
, sizeof(*param
)) ? -EFAULT
: 0;
4557 long sched_setaffinity(pid_t pid
, const struct cpumask
*in_mask
)
4559 cpumask_var_t cpus_allowed
, new_mask
;
4560 struct task_struct
*p
;
4566 p
= find_process_by_pid(pid
);
4570 printk(KERN_DEBUG
"SCHED: setaffinity find process %d fail\n", pid
);
4574 /* Prevent p going away */
4578 if (p
->flags
& PF_NO_SETAFFINITY
) {
4580 printk(KERN_DEBUG
"SCHED: setaffinity flags PF_NO_SETAFFINITY fail\n");
4583 if (!alloc_cpumask_var(&cpus_allowed
, GFP_KERNEL
)) {
4585 printk(KERN_DEBUG
"SCHED: setaffinity allo_cpumask_var for cpus_allowed fail\n");
4588 if (!alloc_cpumask_var(&new_mask
, GFP_KERNEL
)) {
4590 printk(KERN_DEBUG
"SCHED: setaffinity allo_cpumask_var for new_mask fail\n");
4591 goto out_free_cpus_allowed
;
4594 if (!check_same_owner(p
)) {
4596 if (!ns_capable(__task_cred(p
)->user_ns
, CAP_SYS_NICE
)) {
4598 printk(KERN_DEBUG
"SCHED: setaffinity check_same_owner and task_ns_capable fail\n");
4604 retval
= security_task_setscheduler(p
);
4606 printk(KERN_DEBUG
"SCHED: setaffinity security_task_setscheduler fail, status: %d\n", retval
);
4610 cpuset_cpus_allowed(p
, cpus_allowed
);
4611 cpumask_and(new_mask
, in_mask
, cpus_allowed
);
4613 retval
= set_cpus_allowed_ptr(p
, new_mask
);
4615 printk(KERN_DEBUG
"SCHED: set_cpus_allowed_ptr status %d\n", retval
);
4618 cpuset_cpus_allowed(p
, cpus_allowed
);
4619 if (!cpumask_subset(new_mask
, cpus_allowed
)) {
4621 * We must have raced with a concurrent cpuset
4622 * update. Just reset the cpus_allowed to the
4623 * cpuset's cpus_allowed
4625 cpumask_copy(new_mask
, cpus_allowed
);
4630 free_cpumask_var(new_mask
);
4631 out_free_cpus_allowed
:
4632 free_cpumask_var(cpus_allowed
);
4637 printk(KERN_DEBUG
"SCHED: setaffinity status %d\n", retval
);
4641 static int get_user_cpu_mask(unsigned long __user
*user_mask_ptr
, unsigned len
,
4642 struct cpumask
*new_mask
)
4644 if (len
< cpumask_size())
4645 cpumask_clear(new_mask
);
4646 else if (len
> cpumask_size())
4647 len
= cpumask_size();
4649 return copy_from_user(new_mask
, user_mask_ptr
, len
) ? -EFAULT
: 0;
4653 * sys_sched_setaffinity - set the cpu affinity of a process
4654 * @pid: pid of the process
4655 * @len: length in bytes of the bitmask pointed to by user_mask_ptr
4656 * @user_mask_ptr: user-space pointer to the new cpu mask
4658 SYSCALL_DEFINE3(sched_setaffinity
, pid_t
, pid
, unsigned int, len
,
4659 unsigned long __user
*, user_mask_ptr
)
4661 cpumask_var_t new_mask
;
4664 if (!alloc_cpumask_var(&new_mask
, GFP_KERNEL
))
4667 retval
= get_user_cpu_mask(user_mask_ptr
, len
, new_mask
);
4669 retval
= sched_setaffinity(pid
, new_mask
);
4670 free_cpumask_var(new_mask
);
4674 long sched_getaffinity(pid_t pid
, struct cpumask
*mask
)
4676 struct task_struct
*p
;
4677 unsigned long flags
;
4684 p
= find_process_by_pid(pid
);
4686 printk(KERN_DEBUG
"SCHED: getaffinity find process %d fail\n", pid
);
4690 retval
= security_task_getscheduler(p
);
4692 printk(KERN_DEBUG
"SCHED: getaffinity security_task_getscheduler fail, status: %d\n", retval
);
4696 raw_spin_lock_irqsave(&p
->pi_lock
, flags
);
4697 cpumask_and(mask
, &p
->cpus_allowed
, cpu_online_mask
);
4698 raw_spin_unlock_irqrestore(&p
->pi_lock
, flags
);
4705 printk(KERN_DEBUG
"SCHED: getaffinity status %d\n", retval
);
4711 * sys_sched_getaffinity - get the cpu affinity of a process
4712 * @pid: pid of the process
4713 * @len: length in bytes of the bitmask pointed to by user_mask_ptr
4714 * @user_mask_ptr: user-space pointer to hold the current cpu mask
4716 SYSCALL_DEFINE3(sched_getaffinity
, pid_t
, pid
, unsigned int, len
,
4717 unsigned long __user
*, user_mask_ptr
)
4722 if ((len
* BITS_PER_BYTE
) < nr_cpu_ids
)
4724 if (len
& (sizeof(unsigned long)-1))
4727 if (!alloc_cpumask_var(&mask
, GFP_KERNEL
))
4730 ret
= sched_getaffinity(pid
, mask
);
4732 size_t retlen
= min_t(size_t, len
, cpumask_size());
4734 if (copy_to_user(user_mask_ptr
, mask
, retlen
))
4739 free_cpumask_var(mask
);
4745 * sys_sched_yield - yield the current processor to other threads.
4747 * This function yields the current CPU to other tasks. If there are no
4748 * other threads running on this CPU then this function will return.
4750 SYSCALL_DEFINE0(sched_yield
)
4752 struct rq
*rq
= this_rq_lock();
4754 schedstat_inc(rq
, yld_count
);
4755 current
->sched_class
->yield_task(rq
);
4758 * Since we are going to call schedule() anyway, there's
4759 * no need to preempt or enable interrupts:
4761 __release(rq
->lock
);
4762 spin_release(&rq
->lock
.dep_map
, 1, _THIS_IP_
);
4763 do_raw_spin_unlock(&rq
->lock
);
4764 sched_preempt_enable_no_resched();
4771 static inline int should_resched(void)
4773 return need_resched() && !(preempt_count() & PREEMPT_ACTIVE
);
4776 static void __cond_resched(void)
4778 add_preempt_count(PREEMPT_ACTIVE
);
4780 sub_preempt_count(PREEMPT_ACTIVE
);
4783 int __sched
_cond_resched(void)
4785 if (should_resched()) {
4791 EXPORT_SYMBOL(_cond_resched
);
4794 * __cond_resched_lock() - if a reschedule is pending, drop the given lock,
4795 * call schedule, and on return reacquire the lock.
4797 * This works OK both with and without CONFIG_PREEMPT. We do strange low-level
4798 * operations here to prevent schedule() from being called twice (once via
4799 * spin_unlock(), once by hand).
4801 int __cond_resched_lock(spinlock_t
*lock
)
4803 int resched
= should_resched();
4806 lockdep_assert_held(lock
);
4808 if (spin_needbreak(lock
) || resched
) {
4819 EXPORT_SYMBOL(__cond_resched_lock
);
4821 int __sched
__cond_resched_softirq(void)
4823 BUG_ON(!in_softirq());
4825 if (should_resched()) {
4833 EXPORT_SYMBOL(__cond_resched_softirq
);
4836 * yield - yield the current processor to other threads.
4838 * Do not ever use this function, there's a 99% chance you're doing it wrong.
4840 * The scheduler is at all times free to pick the calling task as the most
4841 * eligible task to run, if removing the yield() call from your code breaks
4842 * it, its already broken.
4844 * Typical broken usage is:
4849 * where one assumes that yield() will let 'the other' process run that will
4850 * make event true. If the current task is a SCHED_FIFO task that will never
4851 * happen. Never use yield() as a progress guarantee!!
4853 * If you want to use yield() to wait for something, use wait_event().
4854 * If you want to use yield() to be 'nice' for others, use cond_resched().
4855 * If you still want to use yield(), do not!
4857 void __sched
yield(void)
4859 set_current_state(TASK_RUNNING
);
4862 EXPORT_SYMBOL(yield
);
4865 * yield_to - yield the current processor to another thread in
4866 * your thread group, or accelerate that thread toward the
4867 * processor it's on.
4869 * @preempt: whether task preemption is allowed or not
4871 * It's the caller's job to ensure that the target task struct
4872 * can't go away on us before we can do any checks.
4875 * true (>0) if we indeed boosted the target task.
4876 * false (0) if we failed to boost the target.
4877 * -ESRCH if there's no task to yield to.
4879 bool __sched
yield_to(struct task_struct
*p
, bool preempt
)
4881 struct task_struct
*curr
= current
;
4882 struct rq
*rq
, *p_rq
;
4883 unsigned long flags
;
4886 local_irq_save(flags
);
4892 * If we're the only runnable task on the rq and target rq also
4893 * has only one task, there's absolutely no point in yielding.
4895 if (rq
->nr_running
== 1 && p_rq
->nr_running
== 1) {
4900 double_rq_lock(rq
, p_rq
);
4901 while (task_rq(p
) != p_rq
) {
4902 double_rq_unlock(rq
, p_rq
);
4906 if (!curr
->sched_class
->yield_to_task
)
4909 if (curr
->sched_class
!= p
->sched_class
)
4912 if (task_running(p_rq
, p
) || p
->state
)
4915 yielded
= curr
->sched_class
->yield_to_task(rq
, p
, preempt
);
4917 schedstat_inc(rq
, yld_count
);
4919 * Make p's CPU reschedule; pick_next_entity takes care of
4922 if (preempt
&& rq
!= p_rq
)
4923 resched_task(p_rq
->curr
);
4927 double_rq_unlock(rq
, p_rq
);
4929 local_irq_restore(flags
);
4936 EXPORT_SYMBOL_GPL(yield_to
);
4939 * This task is about to go to sleep on IO. Increment rq->nr_iowait so
4940 * that process accounting knows that this is a task in IO wait state.
4942 void __sched
io_schedule(void)
4944 struct rq
*rq
= raw_rq();
4946 delayacct_blkio_start();
4947 atomic_inc(&rq
->nr_iowait
);
4948 blk_flush_plug(current
);
4949 current
->in_iowait
= 1;
4951 current
->in_iowait
= 0;
4952 atomic_dec(&rq
->nr_iowait
);
4953 delayacct_blkio_end();
4955 EXPORT_SYMBOL(io_schedule
);
4957 long __sched
io_schedule_timeout(long timeout
)
4959 struct rq
*rq
= raw_rq();
4962 delayacct_blkio_start();
4963 atomic_inc(&rq
->nr_iowait
);
4964 blk_flush_plug(current
);
4965 current
->in_iowait
= 1;
4966 ret
= schedule_timeout(timeout
);
4967 current
->in_iowait
= 0;
4968 atomic_dec(&rq
->nr_iowait
);
4969 delayacct_blkio_end();
4974 * sys_sched_get_priority_max - return maximum RT priority.
4975 * @policy: scheduling class.
4977 * this syscall returns the maximum rt_priority that can be used
4978 * by a given scheduling class.
4980 SYSCALL_DEFINE1(sched_get_priority_max
, int, policy
)
4987 ret
= MAX_USER_RT_PRIO
-1;
4999 * sys_sched_get_priority_min - return minimum RT priority.
5000 * @policy: scheduling class.
5002 * this syscall returns the minimum rt_priority that can be used
5003 * by a given scheduling class.
5005 SYSCALL_DEFINE1(sched_get_priority_min
, int, policy
)
5023 * sys_sched_rr_get_interval - return the default timeslice of a process.
5024 * @pid: pid of the process.
5025 * @interval: userspace pointer to the timeslice value.
5027 * this syscall writes the default timeslice value of a given process
5028 * into the user-space timespec buffer. A value of '0' means infinity.
5030 SYSCALL_DEFINE2(sched_rr_get_interval
, pid_t
, pid
,
5031 struct timespec __user
*, interval
)
5033 struct task_struct
*p
;
5034 unsigned int time_slice
;
5035 unsigned long flags
;
5045 p
= find_process_by_pid(pid
);
5049 retval
= security_task_getscheduler(p
);
5053 rq
= task_rq_lock(p
, &flags
);
5054 time_slice
= p
->sched_class
->get_rr_interval(rq
, p
);
5055 task_rq_unlock(rq
, p
, &flags
);
5058 jiffies_to_timespec(time_slice
, &t
);
5059 retval
= copy_to_user(interval
, &t
, sizeof(t
)) ? -EFAULT
: 0;
5067 static const char stat_nam
[] = TASK_STATE_TO_CHAR_STR
;
5068 #ifdef CONFIG_MT_DEBUG_MUTEXES
5069 void mt_mutex_state(struct task_struct
*p
)
5071 struct task_struct
*locker
;
5073 locker
= p
->blocked_on
->task_wait_on
;
5074 if(find_task_by_vpid(locker
->pid
) != NULL
){
5075 printk("Hint: wait on mutex, holder is [%d:%s:%ld]\n", locker
->pid
, locker
->comm
, locker
->state
);
5076 if(locker
->state
!= TASK_RUNNING
){
5077 printk("Mutex holder process[%d:%s] is not running now:\n", locker
->pid
, locker
->comm
);
5078 show_stack(locker
, NULL
);
5082 printk("Hint: wait on mutex, but holder already released lock\n");
5087 void sched_show_task(struct task_struct
*p
)
5089 unsigned long free
= 0;
5093 state
= p
->state
? __ffs(p
->state
) + 1 : 0;
5094 printk(KERN_INFO
"%-15.15s %c", p
->comm
,
5095 state
< sizeof(stat_nam
) - 1 ? stat_nam
[state
] : '?');
5096 #if BITS_PER_LONG == 32
5097 if (state
== TASK_RUNNING
)
5098 printk(KERN_CONT
" running ");
5100 printk(KERN_CONT
" %08lx ", thread_saved_pc(p
));
5102 if (state
== TASK_RUNNING
)
5103 printk(KERN_CONT
" running task ");
5105 printk(KERN_CONT
" %016lx ", thread_saved_pc(p
));
5107 #ifdef CONFIG_DEBUG_STACK_USAGE
5108 free
= stack_not_used(p
);
5111 ppid
= task_pid_nr(rcu_dereference(p
->real_parent
));
5113 printk(KERN_CONT
"%5lu %5d %6d 0x%08lx\n", free
,
5114 task_pid_nr(p
), ppid
,
5115 (unsigned long)task_thread_info(p
)->flags
);
5117 print_worker_info(KERN_INFO
, p
);
5118 show_stack(p
, NULL
);
5119 #ifdef CONFIG_MT_DEBUG_MUTEXES
5124 void show_state_filter(unsigned long state_filter
)
5126 struct task_struct
*g
, *p
;
5128 #if BITS_PER_LONG == 32
5130 " task PC stack pid father\n");
5133 " task PC stack pid father\n");
5136 do_each_thread(g
, p
) {
5138 * reset the NMI-timeout, listing all files on a slow
5139 * console might take a lot of time:
5141 touch_nmi_watchdog();
5142 if (!state_filter
|| (p
->state
& state_filter
))
5144 } while_each_thread(g
, p
);
5146 touch_all_softlockup_watchdogs();
5148 #ifdef CONFIG_SCHED_DEBUG
5150 sysrq_sched_debug_show();
5154 * Only show locks if all tasks are dumped:
5157 debug_show_all_locks();
5160 void __cpuinit
init_idle_bootup_task(struct task_struct
*idle
)
5162 idle
->sched_class
= &idle_sched_class
;
5166 * init_idle - set up an idle thread for a given CPU
5167 * @idle: task in question
5168 * @cpu: cpu the idle task belongs to
5170 * NOTE: this function does not set the idle thread's NEED_RESCHED
5171 * flag, to make booting more robust.
5173 void __cpuinit
init_idle(struct task_struct
*idle
, int cpu
)
5175 struct rq
*rq
= cpu_rq(cpu
);
5176 unsigned long flags
;
5178 raw_spin_lock_irqsave(&rq
->lock
, flags
);
5181 idle
->state
= TASK_RUNNING
;
5182 idle
->se
.exec_start
= sched_clock();
5184 do_set_cpus_allowed(idle
, cpumask_of(cpu
));
5186 * We're having a chicken and egg problem, even though we are
5187 * holding rq->lock, the cpu isn't yet set to this cpu so the
5188 * lockdep check in task_group() will fail.
5190 * Similar case to sched_fork(). / Alternatively we could
5191 * use task_rq_lock() here and obtain the other rq->lock.
5196 __set_task_cpu(idle
, cpu
);
5199 rq
->curr
= rq
->idle
= idle
;
5200 #if defined(CONFIG_SMP)
5203 raw_spin_unlock_irqrestore(&rq
->lock
, flags
);
5205 /* Set the preempt count _outside_ the spinlocks! */
5206 task_thread_info(idle
)->preempt_count
= 0;
5209 * The idle tasks have their own, simple scheduling class:
5211 idle
->sched_class
= &idle_sched_class
;
5212 ftrace_graph_init_idle_task(idle
, cpu
);
5213 vtime_init_idle(idle
, cpu
);
5214 #if defined(CONFIG_SMP)
5215 sprintf(idle
->comm
, "%s/%d", INIT_TASK_COMM
, cpu
);
5220 void do_set_cpus_allowed(struct task_struct
*p
, const struct cpumask
*new_mask
)
5222 if (p
->sched_class
&& p
->sched_class
->set_cpus_allowed
)
5223 p
->sched_class
->set_cpus_allowed(p
, new_mask
);
5225 cpumask_copy(&p
->cpus_allowed
, new_mask
);
5226 p
->nr_cpus_allowed
= cpumask_weight(new_mask
);
5230 * This is how migration works:
5232 * 1) we invoke migration_cpu_stop() on the target CPU using
5234 * 2) stopper starts to run (implicitly forcing the migrated thread
5236 * 3) it checks whether the migrated task is still in the wrong runqueue.
5237 * 4) if it's in the wrong runqueue then the migration thread removes
5238 * it and puts it into the right queue.
5239 * 5) stopper completes and stop_one_cpu() returns and the migration
5244 * Change a given task's CPU affinity. Migrate the thread to a
5245 * proper CPU and schedule it away if the CPU it's executing on
5246 * is removed from the allowed bitmask.
5248 * NOTE: the caller must have a valid reference to the task, the
5249 * task must not exit() & deallocate itself prematurely. The
5250 * call is not atomic; no spinlocks may be held.
5252 int set_cpus_allowed_ptr(struct task_struct
*p
, const struct cpumask
*new_mask
)
5254 unsigned long flags
;
5256 unsigned int dest_cpu
;
5259 rq
= task_rq_lock(p
, &flags
);
5261 if (cpumask_equal(&p
->cpus_allowed
, new_mask
))
5264 if (!cpumask_intersects(new_mask
, cpu_active_mask
)) {
5266 printk(KERN_DEBUG
"SCHED: intersects new_mask: %lu, cpu_active_mask: %lu\n", new_mask
->bits
[0], cpu_active_mask
->bits
[0]);
5270 do_set_cpus_allowed(p
, new_mask
);
5272 /* Can the task run on the task's current CPU? If so, we're done */
5273 if (cpumask_test_cpu(task_cpu(p
), new_mask
))
5276 dest_cpu
= cpumask_any_and(cpu_active_mask
, new_mask
);
5278 struct migration_arg arg
= { p
, dest_cpu
};
5279 /* Need help from migration thread: drop lock and wait. */
5280 task_rq_unlock(rq
, p
, &flags
);
5281 stop_one_cpu(cpu_of(rq
), migration_cpu_stop
, &arg
);
5282 tlb_migrate_finish(p
->mm
);
5286 task_rq_unlock(rq
, p
, &flags
);
5290 EXPORT_SYMBOL_GPL(set_cpus_allowed_ptr
);
5293 * Move (not current) task off this cpu, onto dest cpu. We're doing
5294 * this because either it can't run here any more (set_cpus_allowed()
5295 * away from this CPU, or CPU going down), or because we're
5296 * attempting to rebalance this task on exec (sched_exec).
5298 * So we race with normal scheduler movements, but that's OK, as long
5299 * as the task is no longer on this CPU.
5301 * Returns non-zero if task was successfully migrated.
5303 static int __migrate_task(struct task_struct
*p
, int src_cpu
, int dest_cpu
)
5305 struct rq
*rq_dest
, *rq_src
;
5308 if (unlikely(!cpu_active(dest_cpu
)))
5311 rq_src
= cpu_rq(src_cpu
);
5312 rq_dest
= cpu_rq(dest_cpu
);
5314 raw_spin_lock(&p
->pi_lock
);
5315 double_rq_lock(rq_src
, rq_dest
);
5316 /* Already moved. */
5317 if (task_cpu(p
) != src_cpu
)
5319 /* Affinity changed (again). */
5320 if (!cpumask_test_cpu(dest_cpu
, tsk_cpus_allowed(p
)))
5324 * If we're not on a rq, the next wake-up will ensure we're
5328 dequeue_task(rq_src
, p
, 0);
5329 set_task_cpu(p
, dest_cpu
);
5330 enqueue_task(rq_dest
, p
, 0);
5331 check_preempt_curr(rq_dest
, p
, 0);
5336 double_rq_unlock(rq_src
, rq_dest
);
5337 raw_spin_unlock(&p
->pi_lock
);
5342 * migration_cpu_stop - this will be executed by a highprio stopper thread
5343 * and performs thread migration by bumping thread off CPU then
5344 * 'pushing' onto another runqueue.
5346 static int migration_cpu_stop(void *data
)
5348 struct migration_arg
*arg
= data
;
5351 * The original target cpu might have gone down and we might
5352 * be on another cpu but it doesn't matter.
5354 local_irq_disable();
5355 __migrate_task(arg
->task
, raw_smp_processor_id(), arg
->dest_cpu
);
5360 #ifdef CONFIG_HOTPLUG_CPU
5363 * Ensures that the idle task is using init_mm right before its cpu goes
5366 void idle_task_exit(void)
5368 struct mm_struct
*mm
= current
->active_mm
;
5370 BUG_ON(cpu_online(smp_processor_id()));
5373 switch_mm(mm
, &init_mm
, current
);
5378 * Since this CPU is going 'away' for a while, fold any nr_active delta
5379 * we might have. Assumes we're called after migrate_tasks() so that the
5380 * nr_active count is stable.
5382 * Also see the comment "Global load-average calculations".
5384 static void calc_load_migrate(struct rq
*rq
)
5386 long delta
= calc_load_fold_active(rq
);
5388 atomic_long_add(delta
, &calc_load_tasks
);
5392 * Migrate all tasks from the rq, sleeping tasks will be migrated by
5393 * try_to_wake_up()->select_task_rq().
5395 * Called with rq->lock held even though we'er in stop_machine() and
5396 * there's no concurrency possible, we hold the required locks anyway
5397 * because of lock validation efforts.
5399 static void migrate_tasks(unsigned int dead_cpu
)
5401 struct rq
*rq
= cpu_rq(dead_cpu
);
5402 struct task_struct
*next
, *stop
= rq
->stop
;
5406 * Fudge the rq selection such that the below task selection loop
5407 * doesn't get stuck on the currently eligible stop task.
5409 * We're currently inside stop_machine() and the rq is either stuck
5410 * in the stop_machine_cpu_stop() loop, or we're executing this code,
5411 * either way we should never end up calling schedule() until we're
5415 /* MTK patch: prevent could not migrate RT task when RT throttle*/
5416 unthrottle_offline_rt_rqs(rq
);
5420 * There's this thread running, bail when that's the only
5423 if (rq
->nr_running
== 1)
5426 next
= pick_next_task(rq
);
5428 next
->sched_class
->put_prev_task(rq
, next
);
5430 /* Find suitable destination for @next, with force if needed. */
5431 dest_cpu
= select_fallback_rq(dead_cpu
, next
);
5432 raw_spin_unlock(&rq
->lock
);
5434 __migrate_task(next
, dead_cpu
, dest_cpu
);
5436 raw_spin_lock(&rq
->lock
);
5442 #endif /* CONFIG_HOTPLUG_CPU */
5444 #if defined(CONFIG_SCHED_DEBUG) && defined(CONFIG_SYSCTL)
5446 static struct ctl_table sd_ctl_dir
[] = {
5448 .procname
= "sched_domain",
5454 static struct ctl_table sd_ctl_root
[] = {
5456 .procname
= "kernel",
5458 .child
= sd_ctl_dir
,
5463 static struct ctl_table
*sd_alloc_ctl_entry(int n
)
5465 struct ctl_table
*entry
=
5466 kcalloc(n
, sizeof(struct ctl_table
), GFP_KERNEL
);
5471 static void sd_free_ctl_entry(struct ctl_table
**tablep
)
5473 struct ctl_table
*entry
;
5476 * In the intermediate directories, both the child directory and
5477 * procname are dynamically allocated and could fail but the mode
5478 * will always be set. In the lowest directory the names are
5479 * static strings and all have proc handlers.
5481 for (entry
= *tablep
; entry
->mode
; entry
++) {
5483 sd_free_ctl_entry(&entry
->child
);
5484 if (entry
->proc_handler
== NULL
)
5485 kfree(entry
->procname
);
5492 static int min_load_idx
= 0;
5493 static int max_load_idx
= CPU_LOAD_IDX_MAX
-1;
5496 set_table_entry(struct ctl_table
*entry
,
5497 const char *procname
, void *data
, int maxlen
,
5498 umode_t mode
, proc_handler
*proc_handler
,
5501 entry
->procname
= procname
;
5503 entry
->maxlen
= maxlen
;
5505 entry
->proc_handler
= proc_handler
;
5508 entry
->extra1
= &min_load_idx
;
5509 entry
->extra2
= &max_load_idx
;
5513 static struct ctl_table
*
5514 sd_alloc_ctl_domain_table(struct sched_domain
*sd
)
5516 struct ctl_table
*table
= sd_alloc_ctl_entry(13);
5521 set_table_entry(&table
[0], "min_interval", &sd
->min_interval
,
5522 sizeof(long), 0644, proc_doulongvec_minmax
, false);
5523 set_table_entry(&table
[1], "max_interval", &sd
->max_interval
,
5524 sizeof(long), 0644, proc_doulongvec_minmax
, false);
5525 set_table_entry(&table
[2], "busy_idx", &sd
->busy_idx
,
5526 sizeof(int), 0644, proc_dointvec_minmax
, true);
5527 set_table_entry(&table
[3], "idle_idx", &sd
->idle_idx
,
5528 sizeof(int), 0644, proc_dointvec_minmax
, true);
5529 set_table_entry(&table
[4], "newidle_idx", &sd
->newidle_idx
,
5530 sizeof(int), 0644, proc_dointvec_minmax
, true);
5531 set_table_entry(&table
[5], "wake_idx", &sd
->wake_idx
,
5532 sizeof(int), 0644, proc_dointvec_minmax
, true);
5533 set_table_entry(&table
[6], "forkexec_idx", &sd
->forkexec_idx
,
5534 sizeof(int), 0644, proc_dointvec_minmax
, true);
5535 set_table_entry(&table
[7], "busy_factor", &sd
->busy_factor
,
5536 sizeof(int), 0644, proc_dointvec_minmax
, false);
5537 set_table_entry(&table
[8], "imbalance_pct", &sd
->imbalance_pct
,
5538 sizeof(int), 0644, proc_dointvec_minmax
, false);
5539 set_table_entry(&table
[9], "cache_nice_tries",
5540 &sd
->cache_nice_tries
,
5541 sizeof(int), 0644, proc_dointvec_minmax
, false);
5542 set_table_entry(&table
[10], "flags", &sd
->flags
,
5543 sizeof(int), 0644, proc_dointvec_minmax
, false);
5544 set_table_entry(&table
[11], "name", sd
->name
,
5545 CORENAME_MAX_SIZE
, 0444, proc_dostring
, false);
5546 /* &table[12] is terminator */
5551 static ctl_table
*sd_alloc_ctl_cpu_table(int cpu
)
5553 struct ctl_table
*entry
, *table
;
5554 struct sched_domain
*sd
;
5555 int domain_num
= 0, i
;
5558 for_each_domain(cpu
, sd
)
5560 entry
= table
= sd_alloc_ctl_entry(domain_num
+ 1);
5565 for_each_domain(cpu
, sd
) {
5566 snprintf(buf
, 32, "domain%d", i
);
5567 entry
->procname
= kstrdup(buf
, GFP_KERNEL
);
5569 entry
->child
= sd_alloc_ctl_domain_table(sd
);
5576 static struct ctl_table_header
*sd_sysctl_header
;
5577 static void register_sched_domain_sysctl(void)
5579 int i
, cpu_num
= num_possible_cpus();
5580 struct ctl_table
*entry
= sd_alloc_ctl_entry(cpu_num
+ 1);
5583 WARN_ON(sd_ctl_dir
[0].child
);
5584 sd_ctl_dir
[0].child
= entry
;
5589 for_each_possible_cpu(i
) {
5590 snprintf(buf
, 32, "cpu%d", i
);
5591 entry
->procname
= kstrdup(buf
, GFP_KERNEL
);
5593 entry
->child
= sd_alloc_ctl_cpu_table(i
);
5597 WARN_ON(sd_sysctl_header
);
5598 sd_sysctl_header
= register_sysctl_table(sd_ctl_root
);
5601 /* may be called multiple times per register */
5602 static void unregister_sched_domain_sysctl(void)
5604 if (sd_sysctl_header
)
5605 unregister_sysctl_table(sd_sysctl_header
);
5606 sd_sysctl_header
= NULL
;
5607 if (sd_ctl_dir
[0].child
)
5608 sd_free_ctl_entry(&sd_ctl_dir
[0].child
);
5611 static void register_sched_domain_sysctl(void)
5614 static void unregister_sched_domain_sysctl(void)
5619 static void set_rq_online(struct rq
*rq
)
5622 const struct sched_class
*class;
5624 cpumask_set_cpu(rq
->cpu
, rq
->rd
->online
);
5627 for_each_class(class) {
5628 if (class->rq_online
)
5629 class->rq_online(rq
);
5634 static void set_rq_offline(struct rq
*rq
)
5637 const struct sched_class
*class;
5639 for_each_class(class) {
5640 if (class->rq_offline
)
5641 class->rq_offline(rq
);
5644 cpumask_clear_cpu(rq
->cpu
, rq
->rd
->online
);
5650 * migration_call - callback that gets triggered when a CPU is added.
5651 * Here we can start up the necessary migration thread for the new CPU.
5653 static int __cpuinit
5654 migration_call(struct notifier_block
*nfb
, unsigned long action
, void *hcpu
)
5656 int cpu
= (long)hcpu
;
5657 unsigned long flags
;
5658 struct rq
*rq
= cpu_rq(cpu
);
5660 switch (action
& ~CPU_TASKS_FROZEN
) {
5662 case CPU_UP_PREPARE
:
5663 rq
->calc_load_update
= calc_load_update
;
5664 account_reset_rq(rq
);
5668 /* Update our root-domain */
5669 raw_spin_lock_irqsave(&rq
->lock
, flags
);
5671 BUG_ON(!cpumask_test_cpu(cpu
, rq
->rd
->span
));
5674 raw_spin_unlock_irqrestore(&rq
->lock
, flags
);
5677 #ifdef CONFIG_HOTPLUG_CPU
5679 sched_ttwu_pending();
5680 /* Update our root-domain */
5681 raw_spin_lock_irqsave(&rq
->lock
, flags
);
5683 BUG_ON(!cpumask_test_cpu(cpu
, rq
->rd
->span
));
5687 BUG_ON(rq
->nr_running
!= 1); /* the migration thread */
5688 raw_spin_unlock_irqrestore(&rq
->lock
, flags
);
5692 calc_load_migrate(rq
);
5697 update_max_interval();
5703 * Register at high priority so that task migration (migrate_all_tasks)
5704 * happens before everything else. This has to be lower priority than
5705 * the notifier in the perf_event subsystem, though.
5707 static struct notifier_block __cpuinitdata migration_notifier
= {
5708 .notifier_call
= migration_call
,
5709 .priority
= CPU_PRI_MIGRATION
,
5712 static int __cpuinit
sched_cpu_active(struct notifier_block
*nfb
,
5713 unsigned long action
, void *hcpu
)
5715 switch (action
& ~CPU_TASKS_FROZEN
) {
5716 case CPU_DOWN_FAILED
:
5717 set_cpu_active((long)hcpu
, true);
5724 static int __cpuinit
sched_cpu_inactive(struct notifier_block
*nfb
,
5725 unsigned long action
, void *hcpu
)
5727 switch (action
& ~CPU_TASKS_FROZEN
) {
5728 case CPU_DOWN_PREPARE
:
5729 set_cpu_active((long)hcpu
, false);
5736 static int __init
migration_init(void)
5738 void *cpu
= (void *)(long)smp_processor_id();
5741 /* Initialize migration for the boot CPU */
5742 err
= migration_call(&migration_notifier
, CPU_UP_PREPARE
, cpu
);
5743 BUG_ON(err
== NOTIFY_BAD
);
5744 migration_call(&migration_notifier
, CPU_ONLINE
, cpu
);
5745 register_cpu_notifier(&migration_notifier
);
5747 /* Register cpu active notifiers */
5748 cpu_notifier(sched_cpu_active
, CPU_PRI_SCHED_ACTIVE
);
5749 cpu_notifier(sched_cpu_inactive
, CPU_PRI_SCHED_INACTIVE
);
5753 early_initcall(migration_init
);
5758 static cpumask_var_t sched_domains_tmpmask
; /* sched_domains_mutex */
5760 #ifdef CONFIG_SCHED_DEBUG
5762 static __read_mostly
int sched_debug_enabled
;
5764 static int __init
sched_debug_setup(char *str
)
5766 sched_debug_enabled
= 1;
5770 early_param("sched_debug", sched_debug_setup
);
5772 static inline bool sched_debug(void)
5774 return sched_debug_enabled
;
5777 static int sched_domain_debug_one(struct sched_domain
*sd
, int cpu
, int level
,
5778 struct cpumask
*groupmask
)
5780 struct sched_group
*group
= sd
->groups
;
5783 cpulist_scnprintf(str
, sizeof(str
), sched_domain_span(sd
));
5784 cpumask_clear(groupmask
);
5786 printk(KERN_DEBUG
"%*s domain %d: ", level
, "", level
);
5788 if (!(sd
->flags
& SD_LOAD_BALANCE
)) {
5789 printk("does not load-balance\n");
5791 printk(KERN_ERR
"ERROR: !SD_LOAD_BALANCE domain"
5796 printk(KERN_CONT
"span %s level %s\n", str
, sd
->name
);
5798 if (!cpumask_test_cpu(cpu
, sched_domain_span(sd
))) {
5799 printk(KERN_ERR
"ERROR: domain->span does not contain "
5802 if (!cpumask_test_cpu(cpu
, sched_group_cpus(group
))) {
5803 printk(KERN_ERR
"ERROR: domain->groups does not contain"
5807 printk(KERN_DEBUG
"%*s groups:", level
+ 1, "");
5811 printk(KERN_ERR
"ERROR: group is NULL\n");
5816 * Even though we initialize ->power to something semi-sane,
5817 * we leave power_orig unset. This allows us to detect if
5818 * domain iteration is still funny without causing /0 traps.
5820 if (!group
->sgp
->power_orig
) {
5821 printk(KERN_CONT
"\n");
5822 printk(KERN_ERR
"ERROR: domain->cpu_power not "
5827 if (!cpumask_weight(sched_group_cpus(group
))) {
5828 printk(KERN_CONT
"\n");
5829 printk(KERN_ERR
"ERROR: empty group\n");
5833 if (!(sd
->flags
& SD_OVERLAP
) &&
5834 cpumask_intersects(groupmask
, sched_group_cpus(group
))) {
5835 printk(KERN_CONT
"\n");
5836 printk(KERN_ERR
"ERROR: repeated CPUs\n");
5840 cpumask_or(groupmask
, groupmask
, sched_group_cpus(group
));
5842 cpulist_scnprintf(str
, sizeof(str
), sched_group_cpus(group
));
5844 printk(KERN_CONT
" %s", str
);
5845 if (group
->sgp
->power
!= SCHED_POWER_SCALE
) {
5846 printk(KERN_CONT
" (cpu_power = %d)",
5850 group
= group
->next
;
5851 } while (group
!= sd
->groups
);
5852 printk(KERN_CONT
"\n");
5854 if (!cpumask_equal(sched_domain_span(sd
), groupmask
))
5855 printk(KERN_ERR
"ERROR: groups don't span domain->span\n");
5858 !cpumask_subset(groupmask
, sched_domain_span(sd
->parent
)))
5859 printk(KERN_ERR
"ERROR: parent span is not a superset "
5860 "of domain->span\n");
5864 static void sched_domain_debug(struct sched_domain
*sd
, int cpu
)
5868 if (!sched_debug_enabled
)
5872 printk(KERN_DEBUG
"CPU%d attaching NULL sched-domain.\n", cpu
);
5876 printk(KERN_DEBUG
"CPU%d attaching sched-domain:\n", cpu
);
5879 if (sched_domain_debug_one(sd
, cpu
, level
, sched_domains_tmpmask
))
5887 #else /* !CONFIG_SCHED_DEBUG */
5888 # define sched_domain_debug(sd, cpu) do { } while (0)
5889 static inline bool sched_debug(void)
5893 #endif /* CONFIG_SCHED_DEBUG */
5895 static int sd_degenerate(struct sched_domain
*sd
)
5897 if (cpumask_weight(sched_domain_span(sd
)) == 1)
5900 /* Following flags need at least 2 groups */
5901 if (sd
->flags
& (SD_LOAD_BALANCE
|
5902 SD_BALANCE_NEWIDLE
|
5906 SD_SHARE_PKG_RESOURCES
)) {
5907 if (sd
->groups
!= sd
->groups
->next
)
5911 /* Following flags don't use groups */
5912 if (sd
->flags
& (SD_WAKE_AFFINE
))
5919 sd_parent_degenerate(struct sched_domain
*sd
, struct sched_domain
*parent
)
5921 unsigned long cflags
= sd
->flags
, pflags
= parent
->flags
;
5923 if (sd_degenerate(parent
))
5926 if (!cpumask_equal(sched_domain_span(sd
), sched_domain_span(parent
)))
5929 /* Flags needing groups don't count if only 1 group in parent */
5930 if (parent
->groups
== parent
->groups
->next
) {
5931 pflags
&= ~(SD_LOAD_BALANCE
|
5932 SD_BALANCE_NEWIDLE
|
5936 SD_SHARE_PKG_RESOURCES
);
5937 if (nr_node_ids
== 1)
5938 pflags
&= ~SD_SERIALIZE
;
5940 if (~cflags
& pflags
)
5946 static void free_rootdomain(struct rcu_head
*rcu
)
5948 struct root_domain
*rd
= container_of(rcu
, struct root_domain
, rcu
);
5950 cpupri_cleanup(&rd
->cpupri
);
5951 free_cpumask_var(rd
->rto_mask
);
5952 free_cpumask_var(rd
->online
);
5953 free_cpumask_var(rd
->span
);
5957 static void rq_attach_root(struct rq
*rq
, struct root_domain
*rd
)
5959 struct root_domain
*old_rd
= NULL
;
5960 unsigned long flags
;
5962 raw_spin_lock_irqsave(&rq
->lock
, flags
);
5967 if (cpumask_test_cpu(rq
->cpu
, old_rd
->online
))
5970 cpumask_clear_cpu(rq
->cpu
, old_rd
->span
);
5973 * If we dont want to free the old_rt yet then
5974 * set old_rd to NULL to skip the freeing later
5977 if (!atomic_dec_and_test(&old_rd
->refcount
))
5981 atomic_inc(&rd
->refcount
);
5984 cpumask_set_cpu(rq
->cpu
, rd
->span
);
5985 if (cpumask_test_cpu(rq
->cpu
, cpu_active_mask
))
5988 raw_spin_unlock_irqrestore(&rq
->lock
, flags
);
5991 call_rcu_sched(&old_rd
->rcu
, free_rootdomain
);
5994 static int init_rootdomain(struct root_domain
*rd
)
5996 memset(rd
, 0, sizeof(*rd
));
5998 if (!alloc_cpumask_var(&rd
->span
, GFP_KERNEL
))
6000 if (!alloc_cpumask_var(&rd
->online
, GFP_KERNEL
))
6002 if (!alloc_cpumask_var(&rd
->rto_mask
, GFP_KERNEL
))
6005 if (cpupri_init(&rd
->cpupri
) != 0)
6010 free_cpumask_var(rd
->rto_mask
);
6012 free_cpumask_var(rd
->online
);
6014 free_cpumask_var(rd
->span
);
6020 * By default the system creates a single root-domain with all cpus as
6021 * members (mimicking the global state we have today).
6023 struct root_domain def_root_domain
;
6025 static void init_defrootdomain(void)
6027 init_rootdomain(&def_root_domain
);
6029 atomic_set(&def_root_domain
.refcount
, 1);
6032 static struct root_domain
*alloc_rootdomain(void)
6034 struct root_domain
*rd
;
6036 rd
= kmalloc(sizeof(*rd
), GFP_KERNEL
);
6040 if (init_rootdomain(rd
) != 0) {
6048 static void free_sched_groups(struct sched_group
*sg
, int free_sgp
)
6050 struct sched_group
*tmp
, *first
;
6059 if (free_sgp
&& atomic_dec_and_test(&sg
->sgp
->ref
))
6064 } while (sg
!= first
);
6067 static void free_sched_domain(struct rcu_head
*rcu
)
6069 struct sched_domain
*sd
= container_of(rcu
, struct sched_domain
, rcu
);
6072 * If its an overlapping domain it has private groups, iterate and
6075 if (sd
->flags
& SD_OVERLAP
) {
6076 free_sched_groups(sd
->groups
, 1);
6077 } else if (atomic_dec_and_test(&sd
->groups
->ref
)) {
6078 kfree(sd
->groups
->sgp
);
6084 static void destroy_sched_domain(struct sched_domain
*sd
, int cpu
)
6086 call_rcu(&sd
->rcu
, free_sched_domain
);
6089 static void destroy_sched_domains(struct sched_domain
*sd
, int cpu
)
6091 for (; sd
; sd
= sd
->parent
)
6092 destroy_sched_domain(sd
, cpu
);
6096 * Keep a special pointer to the highest sched_domain that has
6097 * SD_SHARE_PKG_RESOURCE set (Last Level Cache Domain) for this
6098 * allows us to avoid some pointer chasing select_idle_sibling().
6100 * Also keep a unique ID per domain (we use the first cpu number in
6101 * the cpumask of the domain), this allows us to quickly tell if
6102 * two cpus are in the same cache domain, see cpus_share_cache().
6104 DEFINE_PER_CPU(struct sched_domain
*, sd_llc
);
6105 DEFINE_PER_CPU(int, sd_llc_id
);
6107 static void update_top_cache_domain(int cpu
)
6109 struct sched_domain
*sd
;
6112 sd
= highest_flag_domain(cpu
, SD_SHARE_PKG_RESOURCES
);
6114 id
= cpumask_first(sched_domain_span(sd
));
6116 rcu_assign_pointer(per_cpu(sd_llc
, cpu
), sd
);
6117 per_cpu(sd_llc_id
, cpu
) = id
;
6121 * Attach the domain 'sd' to 'cpu' as its base domain. Callers must
6122 * hold the hotplug lock.
6125 cpu_attach_domain(struct sched_domain
*sd
, struct root_domain
*rd
, int cpu
)
6127 struct rq
*rq
= cpu_rq(cpu
);
6128 struct sched_domain
*tmp
;
6130 /* Remove the sched domains which do not contribute to scheduling. */
6131 for (tmp
= sd
; tmp
; ) {
6132 struct sched_domain
*parent
= tmp
->parent
;
6136 if (sd_parent_degenerate(tmp
, parent
)) {
6137 tmp
->parent
= parent
->parent
;
6139 parent
->parent
->child
= tmp
;
6140 destroy_sched_domain(parent
, cpu
);
6145 if (sd
&& sd_degenerate(sd
)) {
6148 destroy_sched_domain(tmp
, cpu
);
6153 sched_domain_debug(sd
, cpu
);
6155 rq_attach_root(rq
, rd
);
6157 rcu_assign_pointer(rq
->sd
, sd
);
6158 destroy_sched_domains(tmp
, cpu
);
6160 #if defined (CONFIG_MTK_SCHED_CMP_PACK_SMALL_TASK) || defined (CONFIG_HMP_PACK_SMALL_TASK)
6161 update_packing_domain(cpu
);
6162 #endif /* CONFIG_MTK_SCHED_CMP_PACK_SMALL_TASK || CONFIG_HMP_PACK_SMALL_TASK */
6163 update_top_cache_domain(cpu
);
6166 /* cpus with isolated domains */
6167 static cpumask_var_t cpu_isolated_map
;
6169 /* Setup the mask of cpus configured for isolated domains */
6170 static int __init
isolated_cpu_setup(char *str
)
6172 alloc_bootmem_cpumask_var(&cpu_isolated_map
);
6173 cpulist_parse(str
, cpu_isolated_map
);
6177 __setup("isolcpus=", isolated_cpu_setup
);
6179 static const struct cpumask
*cpu_cpu_mask(int cpu
)
6181 return cpumask_of_node(cpu_to_node(cpu
));
6185 struct sched_domain
**__percpu sd
;
6186 struct sched_group
**__percpu sg
;
6187 struct sched_group_power
**__percpu sgp
;
6191 struct sched_domain
** __percpu sd
;
6192 struct root_domain
*rd
;
6202 struct sched_domain_topology_level
;
6204 typedef struct sched_domain
*(*sched_domain_init_f
)(struct sched_domain_topology_level
*tl
, int cpu
);
6205 typedef const struct cpumask
*(*sched_domain_mask_f
)(int cpu
);
6207 #define SDTL_OVERLAP 0x01
6209 struct sched_domain_topology_level
{
6210 sched_domain_init_f init
;
6211 sched_domain_mask_f mask
;
6214 struct sd_data data
;
6218 * Build an iteration mask that can exclude certain CPUs from the upwards
6221 * Asymmetric node setups can result in situations where the domain tree is of
6222 * unequal depth, make sure to skip domains that already cover the entire
6225 * In that case build_sched_domains() will have terminated the iteration early
6226 * and our sibling sd spans will be empty. Domains should always include the
6227 * cpu they're built on, so check that.
6230 static void build_group_mask(struct sched_domain
*sd
, struct sched_group
*sg
)
6232 const struct cpumask
*span
= sched_domain_span(sd
);
6233 struct sd_data
*sdd
= sd
->private;
6234 struct sched_domain
*sibling
;
6237 for_each_cpu(i
, span
) {
6238 sibling
= *per_cpu_ptr(sdd
->sd
, i
);
6239 if (!cpumask_test_cpu(i
, sched_domain_span(sibling
)))
6242 cpumask_set_cpu(i
, sched_group_mask(sg
));
6247 * Return the canonical balance cpu for this group, this is the first cpu
6248 * of this group that's also in the iteration mask.
6250 int group_balance_cpu(struct sched_group
*sg
)
6252 return cpumask_first_and(sched_group_cpus(sg
), sched_group_mask(sg
));
6256 build_overlap_sched_groups(struct sched_domain
*sd
, int cpu
)
6258 struct sched_group
*first
= NULL
, *last
= NULL
, *groups
= NULL
, *sg
;
6259 const struct cpumask
*span
= sched_domain_span(sd
);
6260 struct cpumask
*covered
= sched_domains_tmpmask
;
6261 struct sd_data
*sdd
= sd
->private;
6262 struct sched_domain
*child
;
6265 cpumask_clear(covered
);
6267 for_each_cpu(i
, span
) {
6268 struct cpumask
*sg_span
;
6270 if (cpumask_test_cpu(i
, covered
))
6273 child
= *per_cpu_ptr(sdd
->sd
, i
);
6275 /* See the comment near build_group_mask(). */
6276 if (!cpumask_test_cpu(i
, sched_domain_span(child
)))
6279 sg
= kzalloc_node(sizeof(struct sched_group
) + cpumask_size(),
6280 GFP_KERNEL
, cpu_to_node(cpu
));
6285 sg_span
= sched_group_cpus(sg
);
6287 child
= child
->child
;
6288 cpumask_copy(sg_span
, sched_domain_span(child
));
6290 cpumask_set_cpu(i
, sg_span
);
6292 cpumask_or(covered
, covered
, sg_span
);
6294 sg
->sgp
= *per_cpu_ptr(sdd
->sgp
, i
);
6295 if (atomic_inc_return(&sg
->sgp
->ref
) == 1)
6296 build_group_mask(sd
, sg
);
6299 * Initialize sgp->power such that even if we mess up the
6300 * domains and no possible iteration will get us here, we won't
6303 sg
->sgp
->power
= SCHED_POWER_SCALE
* cpumask_weight(sg_span
);
6306 * Make sure the first group of this domain contains the
6307 * canonical balance cpu. Otherwise the sched_domain iteration
6308 * breaks. See update_sg_lb_stats().
6310 if ((!groups
&& cpumask_test_cpu(cpu
, sg_span
)) ||
6311 group_balance_cpu(sg
) == cpu
)
6321 sd
->groups
= groups
;
6326 free_sched_groups(first
, 0);
6331 static int get_group(int cpu
, struct sd_data
*sdd
, struct sched_group
**sg
)
6333 struct sched_domain
*sd
= *per_cpu_ptr(sdd
->sd
, cpu
);
6334 struct sched_domain
*child
= sd
->child
;
6337 cpu
= cpumask_first(sched_domain_span(child
));
6340 *sg
= *per_cpu_ptr(sdd
->sg
, cpu
);
6341 (*sg
)->sgp
= *per_cpu_ptr(sdd
->sgp
, cpu
);
6342 atomic_set(&(*sg
)->sgp
->ref
, 1); /* for claim_allocations */
6349 * build_sched_groups will build a circular linked list of the groups
6350 * covered by the given span, and will set each group's ->cpumask correctly,
6351 * and ->cpu_power to 0.
6353 * Assumes the sched_domain tree is fully constructed
6356 build_sched_groups(struct sched_domain
*sd
, int cpu
)
6358 struct sched_group
*first
= NULL
, *last
= NULL
;
6359 struct sd_data
*sdd
= sd
->private;
6360 const struct cpumask
*span
= sched_domain_span(sd
);
6361 struct cpumask
*covered
;
6364 get_group(cpu
, sdd
, &sd
->groups
);
6365 atomic_inc(&sd
->groups
->ref
);
6367 if (cpu
!= cpumask_first(sched_domain_span(sd
)))
6370 lockdep_assert_held(&sched_domains_mutex
);
6371 covered
= sched_domains_tmpmask
;
6373 cpumask_clear(covered
);
6375 for_each_cpu(i
, span
) {
6376 struct sched_group
*sg
;
6377 int group
= get_group(i
, sdd
, &sg
);
6380 if (cpumask_test_cpu(i
, covered
))
6383 cpumask_clear(sched_group_cpus(sg
));
6385 cpumask_setall(sched_group_mask(sg
));
6387 for_each_cpu(j
, span
) {
6388 if (get_group(j
, sdd
, NULL
) != group
)
6391 cpumask_set_cpu(j
, covered
);
6392 cpumask_set_cpu(j
, sched_group_cpus(sg
));
6407 * Initialize sched groups cpu_power.
6409 * cpu_power indicates the capacity of sched group, which is used while
6410 * distributing the load between different sched groups in a sched domain.
6411 * Typically cpu_power for all the groups in a sched domain will be same unless
6412 * there are asymmetries in the topology. If there are asymmetries, group
6413 * having more cpu_power will pickup more load compared to the group having
6416 static void init_sched_groups_power(int cpu
, struct sched_domain
*sd
)
6418 struct sched_group
*sg
= sd
->groups
;
6420 WARN_ON(!sd
|| !sg
);
6423 sg
->group_weight
= cpumask_weight(sched_group_cpus(sg
));
6425 } while (sg
!= sd
->groups
);
6427 if (cpu
!= group_balance_cpu(sg
))
6430 update_group_power(sd
, cpu
);
6431 atomic_set(&sg
->sgp
->nr_busy_cpus
, sg
->group_weight
);
6434 int __weak
arch_sd_sibling_asym_packing(void)
6436 return 0*SD_ASYM_PACKING
;
6439 #if defined (CONFIG_MTK_SCHED_CMP_PACK_SMALL_TASK) || defined (CONFIG_HMP_PACK_SMALL_TASK)
6440 int __weak
arch_sd_share_power_line(void)
6442 return 0*SD_SHARE_POWERLINE
;
6444 #endif /* CONFIG_MTK_SCHED_CMP_PACK_SMALL_TASK || CONFIG_HMP_PACK_SMALL_TASK */
6446 * Initializers for schedule domains
6447 * Non-inlined to reduce accumulated stack pressure in build_sched_domains()
6450 #ifdef CONFIG_SCHED_DEBUG
6451 # define SD_INIT_NAME(sd, type) sd->name = #type
6453 # define SD_INIT_NAME(sd, type) do { } while (0)
6456 #define SD_INIT_FUNC(type) \
6457 static noinline struct sched_domain * \
6458 sd_init_##type(struct sched_domain_topology_level *tl, int cpu) \
6460 struct sched_domain *sd = *per_cpu_ptr(tl->data.sd, cpu); \
6461 *sd = SD_##type##_INIT; \
6462 SD_INIT_NAME(sd, type); \
6463 sd->private = &tl->data; \
6468 #ifdef CONFIG_SCHED_SMT
6469 SD_INIT_FUNC(SIBLING
)
6471 #ifdef CONFIG_SCHED_MC
6474 #ifdef CONFIG_SCHED_BOOK
6478 static int default_relax_domain_level
= -1;
6479 int sched_domain_level_max
;
6481 static int __init
setup_relax_domain_level(char *str
)
6483 if (kstrtoint(str
, 0, &default_relax_domain_level
))
6484 pr_warn("Unable to set relax_domain_level\n");
6488 __setup("relax_domain_level=", setup_relax_domain_level
);
6490 static void set_domain_attribute(struct sched_domain
*sd
,
6491 struct sched_domain_attr
*attr
)
6495 if (!attr
|| attr
->relax_domain_level
< 0) {
6496 if (default_relax_domain_level
< 0)
6499 request
= default_relax_domain_level
;
6501 request
= attr
->relax_domain_level
;
6502 if (request
< sd
->level
) {
6503 /* turn off idle balance on this domain */
6504 sd
->flags
&= ~(SD_BALANCE_WAKE
|SD_BALANCE_NEWIDLE
);
6506 /* turn on idle balance on this domain */
6507 sd
->flags
|= (SD_BALANCE_WAKE
|SD_BALANCE_NEWIDLE
);
6511 static void __sdt_free(const struct cpumask
*cpu_map
);
6512 static int __sdt_alloc(const struct cpumask
*cpu_map
);
6514 static void __free_domain_allocs(struct s_data
*d
, enum s_alloc what
,
6515 const struct cpumask
*cpu_map
)
6519 if (!atomic_read(&d
->rd
->refcount
))
6520 free_rootdomain(&d
->rd
->rcu
); /* fall through */
6522 free_percpu(d
->sd
); /* fall through */
6524 __sdt_free(cpu_map
); /* fall through */
6530 static enum s_alloc
__visit_domain_allocation_hell(struct s_data
*d
,
6531 const struct cpumask
*cpu_map
)
6533 memset(d
, 0, sizeof(*d
));
6535 if (__sdt_alloc(cpu_map
))
6536 return sa_sd_storage
;
6537 d
->sd
= alloc_percpu(struct sched_domain
*);
6539 return sa_sd_storage
;
6540 d
->rd
= alloc_rootdomain();
6543 return sa_rootdomain
;
6547 * NULL the sd_data elements we've used to build the sched_domain and
6548 * sched_group structure so that the subsequent __free_domain_allocs()
6549 * will not free the data we're using.
6551 static void claim_allocations(int cpu
, struct sched_domain
*sd
)
6553 struct sd_data
*sdd
= sd
->private;
6555 WARN_ON_ONCE(*per_cpu_ptr(sdd
->sd
, cpu
) != sd
);
6556 *per_cpu_ptr(sdd
->sd
, cpu
) = NULL
;
6558 if (atomic_read(&(*per_cpu_ptr(sdd
->sg
, cpu
))->ref
))
6559 *per_cpu_ptr(sdd
->sg
, cpu
) = NULL
;
6561 if (atomic_read(&(*per_cpu_ptr(sdd
->sgp
, cpu
))->ref
))
6562 *per_cpu_ptr(sdd
->sgp
, cpu
) = NULL
;
6565 #ifdef CONFIG_SCHED_SMT
6566 static const struct cpumask
*cpu_smt_mask(int cpu
)
6568 return topology_thread_cpumask(cpu
);
6573 * Topology list, bottom-up.
6575 static struct sched_domain_topology_level default_topology
[] = {
6576 #ifdef CONFIG_SCHED_SMT
6577 { sd_init_SIBLING
, cpu_smt_mask
, },
6579 #ifdef CONFIG_SCHED_MC
6580 { sd_init_MC
, cpu_coregroup_mask
, },
6582 #ifdef CONFIG_SCHED_BOOK
6583 { sd_init_BOOK
, cpu_book_mask
, },
6585 { sd_init_CPU
, cpu_cpu_mask
, },
6589 static struct sched_domain_topology_level
*sched_domain_topology
= default_topology
;
6593 static int sched_domains_numa_levels
;
6594 static int *sched_domains_numa_distance
;
6595 static struct cpumask
***sched_domains_numa_masks
;
6596 static int sched_domains_curr_level
;
6598 static inline int sd_local_flags(int level
)
6600 if (sched_domains_numa_distance
[level
] > RECLAIM_DISTANCE
)
6603 return SD_BALANCE_EXEC
| SD_BALANCE_FORK
| SD_WAKE_AFFINE
;
6606 static struct sched_domain
*
6607 sd_numa_init(struct sched_domain_topology_level
*tl
, int cpu
)
6609 struct sched_domain
*sd
= *per_cpu_ptr(tl
->data
.sd
, cpu
);
6610 int level
= tl
->numa_level
;
6611 int sd_weight
= cpumask_weight(
6612 sched_domains_numa_masks
[level
][cpu_to_node(cpu
)]);
6614 *sd
= (struct sched_domain
){
6615 .min_interval
= sd_weight
,
6616 .max_interval
= 2*sd_weight
,
6618 .imbalance_pct
= 125,
6619 .cache_nice_tries
= 2,
6626 .flags
= 1*SD_LOAD_BALANCE
6627 | 1*SD_BALANCE_NEWIDLE
6632 | 0*SD_SHARE_CPUPOWER
6633 | 0*SD_SHARE_PKG_RESOURCES
6635 | 0*SD_PREFER_SIBLING
6636 | sd_local_flags(level
)
6638 .last_balance
= jiffies
,
6639 .balance_interval
= sd_weight
,
6641 SD_INIT_NAME(sd
, NUMA
);
6642 sd
->private = &tl
->data
;
6645 * Ugly hack to pass state to sd_numa_mask()...
6647 sched_domains_curr_level
= tl
->numa_level
;
6652 static const struct cpumask
*sd_numa_mask(int cpu
)
6654 return sched_domains_numa_masks
[sched_domains_curr_level
][cpu_to_node(cpu
)];
6657 static void sched_numa_warn(const char *str
)
6659 static int done
= false;
6667 printk(KERN_WARNING
"ERROR: %s\n\n", str
);
6669 for (i
= 0; i
< nr_node_ids
; i
++) {
6670 printk(KERN_WARNING
" ");
6671 for (j
= 0; j
< nr_node_ids
; j
++)
6672 printk(KERN_CONT
"%02d ", node_distance(i
,j
));
6673 printk(KERN_CONT
"\n");
6675 printk(KERN_WARNING
"\n");
6678 static bool find_numa_distance(int distance
)
6682 if (distance
== node_distance(0, 0))
6685 for (i
= 0; i
< sched_domains_numa_levels
; i
++) {
6686 if (sched_domains_numa_distance
[i
] == distance
)
6693 static void sched_init_numa(void)
6695 int next_distance
, curr_distance
= node_distance(0, 0);
6696 struct sched_domain_topology_level
*tl
;
6700 sched_domains_numa_distance
= kzalloc(sizeof(int) * nr_node_ids
, GFP_KERNEL
);
6701 if (!sched_domains_numa_distance
)
6705 * O(nr_nodes^2) deduplicating selection sort -- in order to find the
6706 * unique distances in the node_distance() table.
6708 * Assumes node_distance(0,j) includes all distances in
6709 * node_distance(i,j) in order to avoid cubic time.
6711 next_distance
= curr_distance
;
6712 for (i
= 0; i
< nr_node_ids
; i
++) {
6713 for (j
= 0; j
< nr_node_ids
; j
++) {
6714 for (k
= 0; k
< nr_node_ids
; k
++) {
6715 int distance
= node_distance(i
, k
);
6717 if (distance
> curr_distance
&&
6718 (distance
< next_distance
||
6719 next_distance
== curr_distance
))
6720 next_distance
= distance
;
6723 * While not a strong assumption it would be nice to know
6724 * about cases where if node A is connected to B, B is not
6725 * equally connected to A.
6727 if (sched_debug() && node_distance(k
, i
) != distance
)
6728 sched_numa_warn("Node-distance not symmetric");
6730 if (sched_debug() && i
&& !find_numa_distance(distance
))
6731 sched_numa_warn("Node-0 not representative");
6733 if (next_distance
!= curr_distance
) {
6734 sched_domains_numa_distance
[level
++] = next_distance
;
6735 sched_domains_numa_levels
= level
;
6736 curr_distance
= next_distance
;
6741 * In case of sched_debug() we verify the above assumption.
6747 * 'level' contains the number of unique distances, excluding the
6748 * identity distance node_distance(i,i).
6750 * The sched_domains_numa_distance[] array includes the actual distance
6755 * Here, we should temporarily reset sched_domains_numa_levels to 0.
6756 * If it fails to allocate memory for array sched_domains_numa_masks[][],
6757 * the array will contain less then 'level' members. This could be
6758 * dangerous when we use it to iterate array sched_domains_numa_masks[][]
6759 * in other functions.
6761 * We reset it to 'level' at the end of this function.
6763 sched_domains_numa_levels
= 0;
6765 sched_domains_numa_masks
= kzalloc(sizeof(void *) * level
, GFP_KERNEL
);
6766 if (!sched_domains_numa_masks
)
6770 * Now for each level, construct a mask per node which contains all
6771 * cpus of nodes that are that many hops away from us.
6773 for (i
= 0; i
< level
; i
++) {
6774 sched_domains_numa_masks
[i
] =
6775 kzalloc(nr_node_ids
* sizeof(void *), GFP_KERNEL
);
6776 if (!sched_domains_numa_masks
[i
])
6779 for (j
= 0; j
< nr_node_ids
; j
++) {
6780 struct cpumask
*mask
= kzalloc(cpumask_size(), GFP_KERNEL
);
6784 sched_domains_numa_masks
[i
][j
] = mask
;
6786 for (k
= 0; k
< nr_node_ids
; k
++) {
6787 if (node_distance(j
, k
) > sched_domains_numa_distance
[i
])
6790 cpumask_or(mask
, mask
, cpumask_of_node(k
));
6795 tl
= kzalloc((ARRAY_SIZE(default_topology
) + level
) *
6796 sizeof(struct sched_domain_topology_level
), GFP_KERNEL
);
6801 * Copy the default topology bits..
6803 for (i
= 0; default_topology
[i
].init
; i
++)
6804 tl
[i
] = default_topology
[i
];
6807 * .. and append 'j' levels of NUMA goodness.
6809 for (j
= 0; j
< level
; i
++, j
++) {
6810 tl
[i
] = (struct sched_domain_topology_level
){
6811 .init
= sd_numa_init
,
6812 .mask
= sd_numa_mask
,
6813 .flags
= SDTL_OVERLAP
,
6818 sched_domain_topology
= tl
;
6820 sched_domains_numa_levels
= level
;
6823 static void sched_domains_numa_masks_set(int cpu
)
6826 int node
= cpu_to_node(cpu
);
6828 for (i
= 0; i
< sched_domains_numa_levels
; i
++) {
6829 for (j
= 0; j
< nr_node_ids
; j
++) {
6830 if (node_distance(j
, node
) <= sched_domains_numa_distance
[i
])
6831 cpumask_set_cpu(cpu
, sched_domains_numa_masks
[i
][j
]);
6836 static void sched_domains_numa_masks_clear(int cpu
)
6839 for (i
= 0; i
< sched_domains_numa_levels
; i
++) {
6840 for (j
= 0; j
< nr_node_ids
; j
++)
6841 cpumask_clear_cpu(cpu
, sched_domains_numa_masks
[i
][j
]);
6846 * Update sched_domains_numa_masks[level][node] array when new cpus
6849 static int sched_domains_numa_masks_update(struct notifier_block
*nfb
,
6850 unsigned long action
,
6853 int cpu
= (long)hcpu
;
6855 switch (action
& ~CPU_TASKS_FROZEN
) {
6857 sched_domains_numa_masks_set(cpu
);
6861 sched_domains_numa_masks_clear(cpu
);
6871 static inline void sched_init_numa(void)
6875 static int sched_domains_numa_masks_update(struct notifier_block
*nfb
,
6876 unsigned long action
,
6881 #endif /* CONFIG_NUMA */
6883 static int __sdt_alloc(const struct cpumask
*cpu_map
)
6885 struct sched_domain_topology_level
*tl
;
6888 for (tl
= sched_domain_topology
; tl
->init
; tl
++) {
6889 struct sd_data
*sdd
= &tl
->data
;
6891 sdd
->sd
= alloc_percpu(struct sched_domain
*);
6895 sdd
->sg
= alloc_percpu(struct sched_group
*);
6899 sdd
->sgp
= alloc_percpu(struct sched_group_power
*);
6903 for_each_cpu(j
, cpu_map
) {
6904 struct sched_domain
*sd
;
6905 struct sched_group
*sg
;
6906 struct sched_group_power
*sgp
;
6908 sd
= kzalloc_node(sizeof(struct sched_domain
) + cpumask_size(),
6909 GFP_KERNEL
, cpu_to_node(j
));
6913 *per_cpu_ptr(sdd
->sd
, j
) = sd
;
6915 sg
= kzalloc_node(sizeof(struct sched_group
) + cpumask_size(),
6916 GFP_KERNEL
, cpu_to_node(j
));
6922 *per_cpu_ptr(sdd
->sg
, j
) = sg
;
6924 sgp
= kzalloc_node(sizeof(struct sched_group_power
) + cpumask_size(),
6925 GFP_KERNEL
, cpu_to_node(j
));
6929 *per_cpu_ptr(sdd
->sgp
, j
) = sgp
;
6936 static void __sdt_free(const struct cpumask
*cpu_map
)
6938 struct sched_domain_topology_level
*tl
;
6941 for (tl
= sched_domain_topology
; tl
->init
; tl
++) {
6942 struct sd_data
*sdd
= &tl
->data
;
6944 for_each_cpu(j
, cpu_map
) {
6945 struct sched_domain
*sd
;
6948 sd
= *per_cpu_ptr(sdd
->sd
, j
);
6949 if (sd
&& (sd
->flags
& SD_OVERLAP
))
6950 free_sched_groups(sd
->groups
, 0);
6951 kfree(*per_cpu_ptr(sdd
->sd
, j
));
6955 kfree(*per_cpu_ptr(sdd
->sg
, j
));
6957 kfree(*per_cpu_ptr(sdd
->sgp
, j
));
6959 free_percpu(sdd
->sd
);
6961 free_percpu(sdd
->sg
);
6963 free_percpu(sdd
->sgp
);
6968 struct sched_domain
*build_sched_domain(struct sched_domain_topology_level
*tl
,
6969 struct s_data
*d
, const struct cpumask
*cpu_map
,
6970 struct sched_domain_attr
*attr
, struct sched_domain
*child
,
6973 struct sched_domain
*sd
= tl
->init(tl
, cpu
);
6977 cpumask_and(sched_domain_span(sd
), cpu_map
, tl
->mask(cpu
));
6979 sd
->level
= child
->level
+ 1;
6980 sched_domain_level_max
= max(sched_domain_level_max
, sd
->level
);
6984 set_domain_attribute(sd
, attr
);
6990 * Build sched domains for a given set of cpus and attach the sched domains
6991 * to the individual cpus
6993 static int build_sched_domains(const struct cpumask
*cpu_map
,
6994 struct sched_domain_attr
*attr
)
6996 enum s_alloc alloc_state
= sa_none
;
6997 struct sched_domain
*sd
;
6999 int i
, ret
= -ENOMEM
;
7001 alloc_state
= __visit_domain_allocation_hell(&d
, cpu_map
);
7002 if (alloc_state
!= sa_rootdomain
)
7005 /* Set up domains for cpus specified by the cpu_map. */
7006 for_each_cpu(i
, cpu_map
) {
7007 struct sched_domain_topology_level
*tl
;
7010 for (tl
= sched_domain_topology
; tl
->init
; tl
++) {
7011 sd
= build_sched_domain(tl
, &d
, cpu_map
, attr
, sd
, i
);
7012 if (tl
->flags
& SDTL_OVERLAP
|| sched_feat(FORCE_SD_OVERLAP
))
7013 sd
->flags
|= SD_OVERLAP
;
7014 if (cpumask_equal(cpu_map
, sched_domain_span(sd
)))
7021 *per_cpu_ptr(d
.sd
, i
) = sd
;
7024 /* Build the groups for the domains */
7025 for_each_cpu(i
, cpu_map
) {
7026 for (sd
= *per_cpu_ptr(d
.sd
, i
); sd
; sd
= sd
->parent
) {
7027 sd
->span_weight
= cpumask_weight(sched_domain_span(sd
));
7028 if (sd
->flags
& SD_OVERLAP
) {
7029 if (build_overlap_sched_groups(sd
, i
))
7032 if (build_sched_groups(sd
, i
))
7038 /* Calculate CPU power for physical packages and nodes */
7039 for (i
= nr_cpumask_bits
-1; i
>= 0; i
--) {
7040 if (!cpumask_test_cpu(i
, cpu_map
))
7043 for (sd
= *per_cpu_ptr(d
.sd
, i
); sd
; sd
= sd
->parent
) {
7044 claim_allocations(i
, sd
);
7045 init_sched_groups_power(i
, sd
);
7049 /* Attach the domains */
7051 for_each_cpu(i
, cpu_map
) {
7052 sd
= *per_cpu_ptr(d
.sd
, i
);
7053 cpu_attach_domain(sd
, d
.rd
, i
);
7059 __free_domain_allocs(&d
, alloc_state
, cpu_map
);
7063 static cpumask_var_t
*doms_cur
; /* current sched domains */
7064 static int ndoms_cur
; /* number of sched domains in 'doms_cur' */
7065 static struct sched_domain_attr
*dattr_cur
;
7066 /* attribues of custom domains in 'doms_cur' */
7069 * Special case: If a kmalloc of a doms_cur partition (array of
7070 * cpumask) fails, then fallback to a single sched domain,
7071 * as determined by the single cpumask fallback_doms.
7073 static cpumask_var_t fallback_doms
;
7076 * arch_update_cpu_topology lets virtualized architectures update the
7077 * cpu core maps. It is supposed to return 1 if the topology changed
7078 * or 0 if it stayed the same.
7080 int __attribute__((weak
)) arch_update_cpu_topology(void)
7085 cpumask_var_t
*alloc_sched_domains(unsigned int ndoms
)
7088 cpumask_var_t
*doms
;
7090 doms
= kmalloc(sizeof(*doms
) * ndoms
, GFP_KERNEL
);
7093 for (i
= 0; i
< ndoms
; i
++) {
7094 if (!alloc_cpumask_var(&doms
[i
], GFP_KERNEL
)) {
7095 free_sched_domains(doms
, i
);
7102 void free_sched_domains(cpumask_var_t doms
[], unsigned int ndoms
)
7105 for (i
= 0; i
< ndoms
; i
++)
7106 free_cpumask_var(doms
[i
]);
7111 * Set up scheduler domains and groups. Callers must hold the hotplug lock.
7112 * For now this just excludes isolated cpus, but could be used to
7113 * exclude other special cases in the future.
7115 static int init_sched_domains(const struct cpumask
*cpu_map
)
7119 arch_update_cpu_topology();
7121 doms_cur
= alloc_sched_domains(ndoms_cur
);
7123 doms_cur
= &fallback_doms
;
7124 cpumask_andnot(doms_cur
[0], cpu_map
, cpu_isolated_map
);
7125 err
= build_sched_domains(doms_cur
[0], NULL
);
7126 register_sched_domain_sysctl();
7132 * Detach sched domains from a group of cpus specified in cpu_map
7133 * These cpus will now be attached to the NULL domain
7135 static void detach_destroy_domains(const struct cpumask
*cpu_map
)
7140 for_each_cpu(i
, cpu_map
)
7141 cpu_attach_domain(NULL
, &def_root_domain
, i
);
7145 /* handle null as "default" */
7146 static int dattrs_equal(struct sched_domain_attr
*cur
, int idx_cur
,
7147 struct sched_domain_attr
*new, int idx_new
)
7149 struct sched_domain_attr tmp
;
7156 return !memcmp(cur
? (cur
+ idx_cur
) : &tmp
,
7157 new ? (new + idx_new
) : &tmp
,
7158 sizeof(struct sched_domain_attr
));
7162 * Partition sched domains as specified by the 'ndoms_new'
7163 * cpumasks in the array doms_new[] of cpumasks. This compares
7164 * doms_new[] to the current sched domain partitioning, doms_cur[].
7165 * It destroys each deleted domain and builds each new domain.
7167 * 'doms_new' is an array of cpumask_var_t's of length 'ndoms_new'.
7168 * The masks don't intersect (don't overlap.) We should setup one
7169 * sched domain for each mask. CPUs not in any of the cpumasks will
7170 * not be load balanced. If the same cpumask appears both in the
7171 * current 'doms_cur' domains and in the new 'doms_new', we can leave
7174 * The passed in 'doms_new' should be allocated using
7175 * alloc_sched_domains. This routine takes ownership of it and will
7176 * free_sched_domains it when done with it. If the caller failed the
7177 * alloc call, then it can pass in doms_new == NULL && ndoms_new == 1,
7178 * and partition_sched_domains() will fallback to the single partition
7179 * 'fallback_doms', it also forces the domains to be rebuilt.
7181 * If doms_new == NULL it will be replaced with cpu_online_mask.
7182 * ndoms_new == 0 is a special case for destroying existing domains,
7183 * and it will not create the default domain.
7185 * Call with hotplug lock held
7187 void partition_sched_domains(int ndoms_new
, cpumask_var_t doms_new
[],
7188 struct sched_domain_attr
*dattr_new
)
7193 mutex_lock(&sched_domains_mutex
);
7195 /* always unregister in case we don't destroy any domains */
7196 unregister_sched_domain_sysctl();
7198 /* Let architecture update cpu core mappings. */
7199 new_topology
= arch_update_cpu_topology();
7201 n
= doms_new
? ndoms_new
: 0;
7203 /* Destroy deleted domains */
7204 for (i
= 0; i
< ndoms_cur
; i
++) {
7205 for (j
= 0; j
< n
&& !new_topology
; j
++) {
7206 if (cpumask_equal(doms_cur
[i
], doms_new
[j
])
7207 && dattrs_equal(dattr_cur
, i
, dattr_new
, j
))
7210 /* no match - a current sched domain not in new doms_new[] */
7211 detach_destroy_domains(doms_cur
[i
]);
7216 if (doms_new
== NULL
) {
7218 doms_new
= &fallback_doms
;
7219 cpumask_andnot(doms_new
[0], cpu_active_mask
, cpu_isolated_map
);
7220 WARN_ON_ONCE(dattr_new
);
7223 /* Build new domains */
7224 for (i
= 0; i
< ndoms_new
; i
++) {
7225 for (j
= 0; j
< ndoms_cur
&& !new_topology
; j
++) {
7226 if (cpumask_equal(doms_new
[i
], doms_cur
[j
])
7227 && dattrs_equal(dattr_new
, i
, dattr_cur
, j
))
7230 /* no match - add a new doms_new */
7231 build_sched_domains(doms_new
[i
], dattr_new
? dattr_new
+ i
: NULL
);
7236 /* Remember the new sched domains */
7237 if (doms_cur
!= &fallback_doms
)
7238 free_sched_domains(doms_cur
, ndoms_cur
);
7239 kfree(dattr_cur
); /* kfree(NULL) is safe */
7240 doms_cur
= doms_new
;
7241 dattr_cur
= dattr_new
;
7242 ndoms_cur
= ndoms_new
;
7244 register_sched_domain_sysctl();
7246 mutex_unlock(&sched_domains_mutex
);
7249 static int num_cpus_frozen
; /* used to mark begin/end of suspend/resume */
7252 * Update cpusets according to cpu_active mask. If cpusets are
7253 * disabled, cpuset_update_active_cpus() becomes a simple wrapper
7254 * around partition_sched_domains().
7256 * If we come here as part of a suspend/resume, don't touch cpusets because we
7257 * want to restore it back to its original state upon resume anyway.
7259 static int cpuset_cpu_active(struct notifier_block
*nfb
, unsigned long action
,
7263 case CPU_ONLINE_FROZEN
:
7264 case CPU_DOWN_FAILED_FROZEN
:
7267 * num_cpus_frozen tracks how many CPUs are involved in suspend
7268 * resume sequence. As long as this is not the last online
7269 * operation in the resume sequence, just build a single sched
7270 * domain, ignoring cpusets.
7273 if (likely(num_cpus_frozen
)) {
7274 partition_sched_domains(1, NULL
, NULL
);
7279 * This is the last CPU online operation. So fall through and
7280 * restore the original sched domains by considering the
7281 * cpuset configurations.
7285 case CPU_DOWN_FAILED
:
7286 cpuset_update_active_cpus(true);
7294 static int cpuset_cpu_inactive(struct notifier_block
*nfb
, unsigned long action
,
7298 case CPU_DOWN_PREPARE
:
7299 cpuset_update_active_cpus(false);
7301 case CPU_DOWN_PREPARE_FROZEN
:
7303 partition_sched_domains(1, NULL
, NULL
);
7311 void __init
sched_init_smp(void)
7313 cpumask_var_t non_isolated_cpus
;
7315 alloc_cpumask_var(&non_isolated_cpus
, GFP_KERNEL
);
7316 alloc_cpumask_var(&fallback_doms
, GFP_KERNEL
);
7321 mutex_lock(&sched_domains_mutex
);
7322 init_sched_domains(cpu_active_mask
);
7323 cpumask_andnot(non_isolated_cpus
, cpu_possible_mask
, cpu_isolated_map
);
7324 if (cpumask_empty(non_isolated_cpus
))
7325 cpumask_set_cpu(smp_processor_id(), non_isolated_cpus
);
7326 mutex_unlock(&sched_domains_mutex
);
7329 hotcpu_notifier(sched_domains_numa_masks_update
, CPU_PRI_SCHED_ACTIVE
);
7330 hotcpu_notifier(cpuset_cpu_active
, CPU_PRI_CPUSET_ACTIVE
);
7331 hotcpu_notifier(cpuset_cpu_inactive
, CPU_PRI_CPUSET_INACTIVE
);
7333 /* RT runtime code needs to handle some hotplug events */
7334 hotcpu_notifier(update_runtime
, 0);
7338 /* Move init over to a non-isolated CPU */
7339 if (set_cpus_allowed_ptr(current
, non_isolated_cpus
) < 0)
7341 sched_init_granularity();
7342 free_cpumask_var(non_isolated_cpus
);
7344 init_sched_rt_class();
7347 void __init
sched_init_smp(void)
7349 sched_init_granularity();
7351 #endif /* CONFIG_SMP */
7353 const_debug
unsigned int sysctl_timer_migration
= 1;
7355 int in_sched_functions(unsigned long addr
)
7357 return in_lock_functions(addr
) ||
7358 (addr
>= (unsigned long)__sched_text_start
7359 && addr
< (unsigned long)__sched_text_end
);
7362 #ifdef CONFIG_CGROUP_SCHED
7364 * Default task group.
7365 * Every task in system belongs to this group at bootup.
7367 struct task_group root_task_group
;
7368 LIST_HEAD(task_groups
);
7371 DECLARE_PER_CPU(cpumask_var_t
, load_balance_mask
);
7373 void __init
sched_init(void)
7376 unsigned long alloc_size
= 0, ptr
;
7378 #ifdef CONFIG_FAIR_GROUP_SCHED
7379 alloc_size
+= 2 * nr_cpu_ids
* sizeof(void **);
7381 #ifdef CONFIG_RT_GROUP_SCHED
7382 alloc_size
+= 2 * nr_cpu_ids
* sizeof(void **);
7384 #ifdef CONFIG_CPUMASK_OFFSTACK
7385 alloc_size
+= num_possible_cpus() * cpumask_size();
7388 ptr
= (unsigned long)kzalloc(alloc_size
, GFP_NOWAIT
);
7390 #ifdef CONFIG_FAIR_GROUP_SCHED
7391 root_task_group
.se
= (struct sched_entity
**)ptr
;
7392 ptr
+= nr_cpu_ids
* sizeof(void **);
7394 root_task_group
.cfs_rq
= (struct cfs_rq
**)ptr
;
7395 ptr
+= nr_cpu_ids
* sizeof(void **);
7397 #endif /* CONFIG_FAIR_GROUP_SCHED */
7398 #ifdef CONFIG_RT_GROUP_SCHED
7399 root_task_group
.rt_se
= (struct sched_rt_entity
**)ptr
;
7400 ptr
+= nr_cpu_ids
* sizeof(void **);
7402 root_task_group
.rt_rq
= (struct rt_rq
**)ptr
;
7403 ptr
+= nr_cpu_ids
* sizeof(void **);
7405 #endif /* CONFIG_RT_GROUP_SCHED */
7406 #ifdef CONFIG_CPUMASK_OFFSTACK
7407 for_each_possible_cpu(i
) {
7408 per_cpu(load_balance_mask
, i
) = (void *)ptr
;
7409 ptr
+= cpumask_size();
7411 #endif /* CONFIG_CPUMASK_OFFSTACK */
7415 init_defrootdomain();
7418 init_rt_bandwidth(&def_rt_bandwidth
,
7419 global_rt_period(), global_rt_runtime());
7421 #ifdef CONFIG_RT_GROUP_SCHED
7422 init_rt_bandwidth(&root_task_group
.rt_bandwidth
,
7423 global_rt_period(), global_rt_runtime());
7424 #endif /* CONFIG_RT_GROUP_SCHED */
7426 #ifdef CONFIG_CGROUP_SCHED
7427 list_add(&root_task_group
.list
, &task_groups
);
7428 INIT_LIST_HEAD(&root_task_group
.children
);
7429 INIT_LIST_HEAD(&root_task_group
.siblings
);
7430 autogroup_init(&init_task
);
7432 #endif /* CONFIG_CGROUP_SCHED */
7434 for_each_possible_cpu(i
) {
7438 raw_spin_lock_init(&rq
->lock
);
7440 rq
->calc_load_active
= 0;
7441 rq
->calc_load_update
= jiffies
+ LOAD_FREQ
;
7442 #ifdef CONFIG_PROVE_LOCKING
7445 init_cfs_rq(&rq
->cfs
);
7446 init_rt_rq(&rq
->rt
, rq
);
7447 #ifdef CONFIG_FAIR_GROUP_SCHED
7448 root_task_group
.shares
= ROOT_TASK_GROUP_LOAD
;
7449 INIT_LIST_HEAD(&rq
->leaf_cfs_rq_list
);
7451 * How much cpu bandwidth does root_task_group get?
7453 * In case of task-groups formed thr' the cgroup filesystem, it
7454 * gets 100% of the cpu resources in the system. This overall
7455 * system cpu resource is divided among the tasks of
7456 * root_task_group and its child task-groups in a fair manner,
7457 * based on each entity's (task or task-group's) weight
7458 * (se->load.weight).
7460 * In other words, if root_task_group has 10 tasks of weight
7461 * 1024) and two child groups A0 and A1 (of weight 1024 each),
7462 * then A0's share of the cpu resource is:
7464 * A0's bandwidth = 1024 / (10*1024 + 1024 + 1024) = 8.33%
7466 * We achieve this by letting root_task_group's tasks sit
7467 * directly in rq->cfs (i.e root_task_group->se[] = NULL).
7469 init_cfs_bandwidth(&root_task_group
.cfs_bandwidth
);
7470 init_tg_cfs_entry(&root_task_group
, &rq
->cfs
, NULL
, i
, NULL
);
7471 #endif /* CONFIG_FAIR_GROUP_SCHED */
7473 rq
->rt
.rt_runtime
= def_rt_bandwidth
.rt_runtime
;
7474 #ifdef CONFIG_RT_GROUP_SCHED
7475 INIT_LIST_HEAD(&rq
->leaf_rt_rq_list
);
7476 init_tg_rt_entry(&root_task_group
, &rq
->rt
, NULL
, i
, NULL
);
7479 for (j
= 0; j
< CPU_LOAD_IDX_MAX
; j
++)
7480 rq
->cpu_load
[j
] = 0;
7482 rq
->last_load_update_tick
= jiffies
;
7487 rq
->cpu_power
= SCHED_POWER_SCALE
;
7488 rq
->post_schedule
= 0;
7489 rq
->active_balance
= 0;
7490 rq
->next_balance
= jiffies
;
7495 rq
->avg_idle
= 2*sysctl_sched_migration_cost
;
7497 INIT_LIST_HEAD(&rq
->cfs_tasks
);
7499 rq_attach_root(rq
, &def_root_domain
);
7500 #ifdef CONFIG_NO_HZ_COMMON
7503 #ifdef CONFIG_NO_HZ_FULL
7504 rq
->last_sched_tick
= 0;
7508 atomic_set(&rq
->nr_iowait
, 0);
7511 set_load_weight(&init_task
);
7513 #ifdef CONFIG_PREEMPT_NOTIFIERS
7514 INIT_HLIST_HEAD(&init_task
.preempt_notifiers
);
7517 #ifdef CONFIG_RT_MUTEXES
7518 plist_head_init(&init_task
.pi_waiters
);
7522 * The boot idle thread does lazy MMU switching as well:
7524 atomic_inc(&init_mm
.mm_count
);
7525 enter_lazy_tlb(&init_mm
, current
);
7528 * Make us the idle thread. Technically, schedule() should not be
7529 * called from this thread, however somewhere below it might be,
7530 * but because we are the idle thread, we just pick up running again
7531 * when this runqueue becomes "idle".
7533 init_idle(current
, smp_processor_id());
7535 calc_load_update
= jiffies
+ LOAD_FREQ
;
7538 * During early bootup we pretend to be a normal task:
7540 current
->sched_class
= &fair_sched_class
;
7543 zalloc_cpumask_var(&sched_domains_tmpmask
, GFP_NOWAIT
);
7544 /* May be allocated at isolcpus cmdline parse time */
7545 if (cpu_isolated_map
== NULL
)
7546 zalloc_cpumask_var(&cpu_isolated_map
, GFP_NOWAIT
);
7547 idle_thread_set_boot_cpu();
7549 init_sched_fair_class();
7551 scheduler_running
= 1;
7554 #ifdef CONFIG_DEBUG_ATOMIC_SLEEP
7555 static inline int preempt_count_equals(int preempt_offset
)
7557 int nested
= (preempt_count() & ~PREEMPT_ACTIVE
) + rcu_preempt_depth();
7559 return (nested
== preempt_offset
);
7562 static int __might_sleep_init_called
;
7563 int __init
__might_sleep_init(void)
7565 __might_sleep_init_called
= 1;
7568 early_initcall(__might_sleep_init
);
7570 void __might_sleep(const char *file
, int line
, int preempt_offset
)
7572 static unsigned long prev_jiffy
; /* ratelimiting */
7574 rcu_sleep_check(); /* WARN_ON_ONCE() by default, no rate limit reqd. */
7575 if ((preempt_count_equals(preempt_offset
) && !irqs_disabled()) ||
7578 if (system_state
!= SYSTEM_RUNNING
&&
7579 (!__might_sleep_init_called
|| system_state
!= SYSTEM_BOOTING
))
7581 if (time_before(jiffies
, prev_jiffy
+ HZ
) && prev_jiffy
)
7583 prev_jiffy
= jiffies
;
7586 "BUG: sleeping function called from invalid context at %s:%d\n",
7589 "in_atomic(): %d, irqs_disabled(): %d, pid: %d, name: %s\n",
7590 in_atomic(), irqs_disabled(),
7591 current
->pid
, current
->comm
);
7593 debug_show_held_locks(current
);
7594 if (irqs_disabled())
7595 print_irqtrace_events(current
);
7598 EXPORT_SYMBOL(__might_sleep
);
7601 #ifdef CONFIG_MAGIC_SYSRQ
7602 static void normalize_task(struct rq
*rq
, struct task_struct
*p
)
7604 const struct sched_class
*prev_class
= p
->sched_class
;
7605 int old_prio
= p
->prio
;
7610 dequeue_task(rq
, p
, 0);
7611 __setscheduler(rq
, p
, SCHED_NORMAL
, 0);
7613 enqueue_task(rq
, p
, 0);
7614 resched_task(rq
->curr
);
7617 check_class_changed(rq
, p
, prev_class
, old_prio
);
7620 void normalize_rt_tasks(void)
7622 struct task_struct
*g
, *p
;
7623 unsigned long flags
;
7626 read_lock_irqsave(&tasklist_lock
, flags
);
7627 do_each_thread(g
, p
) {
7629 * Only normalize user tasks:
7634 p
->se
.exec_start
= 0;
7635 #ifdef CONFIG_SCHEDSTATS
7636 p
->se
.statistics
.wait_start
= 0;
7637 p
->se
.statistics
.sleep_start
= 0;
7638 p
->se
.statistics
.block_start
= 0;
7643 * Renice negative nice level userspace
7646 if (TASK_NICE(p
) < 0 && p
->mm
)
7647 set_user_nice(p
, 0);
7651 raw_spin_lock(&p
->pi_lock
);
7652 rq
= __task_rq_lock(p
);
7654 normalize_task(rq
, p
);
7656 __task_rq_unlock(rq
);
7657 raw_spin_unlock(&p
->pi_lock
);
7658 } while_each_thread(g
, p
);
7660 read_unlock_irqrestore(&tasklist_lock
, flags
);
7663 #endif /* CONFIG_MAGIC_SYSRQ */
7665 #if defined(CONFIG_IA64) || defined(CONFIG_KGDB_KDB)
7667 * These functions are only useful for the IA64 MCA handling, or kdb.
7669 * They can only be called when the whole system has been
7670 * stopped - every CPU needs to be quiescent, and no scheduling
7671 * activity can take place. Using them for anything else would
7672 * be a serious bug, and as a result, they aren't even visible
7673 * under any other configuration.
7677 * curr_task - return the current task for a given cpu.
7678 * @cpu: the processor in question.
7680 * ONLY VALID WHEN THE WHOLE SYSTEM IS STOPPED!
7682 struct task_struct
*curr_task(int cpu
)
7684 return cpu_curr(cpu
);
7687 #endif /* defined(CONFIG_IA64) || defined(CONFIG_KGDB_KDB) */
7691 * set_curr_task - set the current task for a given cpu.
7692 * @cpu: the processor in question.
7693 * @p: the task pointer to set.
7695 * Description: This function must only be used when non-maskable interrupts
7696 * are serviced on a separate stack. It allows the architecture to switch the
7697 * notion of the current task on a cpu in a non-blocking manner. This function
7698 * must be called with all CPU's synchronized, and interrupts disabled, the
7699 * and caller must save the original value of the current task (see
7700 * curr_task() above) and restore that value before reenabling interrupts and
7701 * re-starting the system.
7703 * ONLY VALID WHEN THE WHOLE SYSTEM IS STOPPED!
7705 void set_curr_task(int cpu
, struct task_struct
*p
)
7712 #ifdef CONFIG_CGROUP_SCHED
7713 /* task_group_lock serializes the addition/removal of task groups */
7714 static DEFINE_SPINLOCK(task_group_lock
);
7716 static void free_sched_group(struct task_group
*tg
)
7718 free_fair_sched_group(tg
);
7719 free_rt_sched_group(tg
);
7724 /* allocate runqueue etc for a new task group */
7725 struct task_group
*sched_create_group(struct task_group
*parent
)
7727 struct task_group
*tg
;
7729 tg
= kzalloc(sizeof(*tg
), GFP_KERNEL
);
7731 return ERR_PTR(-ENOMEM
);
7733 if (!alloc_fair_sched_group(tg
, parent
))
7736 if (!alloc_rt_sched_group(tg
, parent
))
7742 free_sched_group(tg
);
7743 return ERR_PTR(-ENOMEM
);
7746 void sched_online_group(struct task_group
*tg
, struct task_group
*parent
)
7748 unsigned long flags
;
7750 spin_lock_irqsave(&task_group_lock
, flags
);
7751 list_add_rcu(&tg
->list
, &task_groups
);
7753 WARN_ON(!parent
); /* root should already exist */
7755 tg
->parent
= parent
;
7756 INIT_LIST_HEAD(&tg
->children
);
7757 list_add_rcu(&tg
->siblings
, &parent
->children
);
7758 spin_unlock_irqrestore(&task_group_lock
, flags
);
7761 /* rcu callback to free various structures associated with a task group */
7762 static void free_sched_group_rcu(struct rcu_head
*rhp
)
7764 /* now it should be safe to free those cfs_rqs */
7765 free_sched_group(container_of(rhp
, struct task_group
, rcu
));
7768 /* Destroy runqueue etc associated with a task group */
7769 void sched_destroy_group(struct task_group
*tg
)
7771 /* wait for possible concurrent references to cfs_rqs complete */
7772 call_rcu(&tg
->rcu
, free_sched_group_rcu
);
7775 void sched_offline_group(struct task_group
*tg
)
7777 unsigned long flags
;
7780 /* end participation in shares distribution */
7781 for_each_possible_cpu(i
)
7782 unregister_fair_sched_group(tg
, i
);
7784 spin_lock_irqsave(&task_group_lock
, flags
);
7785 list_del_rcu(&tg
->list
);
7786 list_del_rcu(&tg
->siblings
);
7787 spin_unlock_irqrestore(&task_group_lock
, flags
);
7790 /* change task's runqueue when it moves between groups.
7791 * The caller of this function should have put the task in its new group
7792 * by now. This function just updates tsk->se.cfs_rq and tsk->se.parent to
7793 * reflect its new group.
7795 void sched_move_task(struct task_struct
*tsk
)
7797 struct task_group
*tg
;
7799 unsigned long flags
;
7802 rq
= task_rq_lock(tsk
, &flags
);
7804 running
= task_current(rq
, tsk
);
7808 dequeue_task(rq
, tsk
, 0);
7809 if (unlikely(running
))
7810 tsk
->sched_class
->put_prev_task(rq
, tsk
);
7812 tg
= container_of(task_subsys_state_check(tsk
, cpu_cgroup_subsys_id
,
7813 lockdep_is_held(&tsk
->sighand
->siglock
)),
7814 struct task_group
, css
);
7815 tg
= autogroup_task_group(tsk
, tg
);
7816 tsk
->sched_task_group
= tg
;
7818 #ifdef CONFIG_FAIR_GROUP_SCHED
7819 if (tsk
->sched_class
->task_move_group
)
7820 tsk
->sched_class
->task_move_group(tsk
, on_rq
);
7823 set_task_rq(tsk
, task_cpu(tsk
));
7825 if (unlikely(running
))
7826 tsk
->sched_class
->set_curr_task(rq
);
7828 enqueue_task(rq
, tsk
, 0);
7830 task_rq_unlock(rq
, tsk
, &flags
);
7832 #endif /* CONFIG_CGROUP_SCHED */
7834 #if defined(CONFIG_RT_GROUP_SCHED) || defined(CONFIG_CFS_BANDWIDTH)
7835 static unsigned long to_ratio(u64 period
, u64 runtime
)
7837 if (runtime
== RUNTIME_INF
)
7840 return div64_u64(runtime
<< 20, period
);
7844 #ifdef CONFIG_RT_GROUP_SCHED
7846 * Ensure that the real time constraints are schedulable.
7848 static DEFINE_MUTEX(rt_constraints_mutex
);
7850 /* Must be called with tasklist_lock held */
7851 static inline int tg_has_rt_tasks(struct task_group
*tg
)
7853 struct task_struct
*g
, *p
;
7855 do_each_thread(g
, p
) {
7856 if (rt_task(p
) && task_rq(p
)->rt
.tg
== tg
)
7858 } while_each_thread(g
, p
);
7863 struct rt_schedulable_data
{
7864 struct task_group
*tg
;
7869 static int tg_rt_schedulable(struct task_group
*tg
, void *data
)
7871 struct rt_schedulable_data
*d
= data
;
7872 struct task_group
*child
;
7873 unsigned long total
, sum
= 0;
7874 u64 period
, runtime
;
7876 period
= ktime_to_ns(tg
->rt_bandwidth
.rt_period
);
7877 runtime
= tg
->rt_bandwidth
.rt_runtime
;
7880 period
= d
->rt_period
;
7881 runtime
= d
->rt_runtime
;
7885 * Cannot have more runtime than the period.
7887 if (runtime
> period
&& runtime
!= RUNTIME_INF
)
7891 * Ensure we don't starve existing RT tasks.
7893 if (rt_bandwidth_enabled() && !runtime
&& tg_has_rt_tasks(tg
))
7896 total
= to_ratio(period
, runtime
);
7899 * Nobody can have more than the global setting allows.
7901 if (total
> to_ratio(global_rt_period(), global_rt_runtime()))
7905 * The sum of our children's runtime should not exceed our own.
7907 list_for_each_entry_rcu(child
, &tg
->children
, siblings
) {
7908 period
= ktime_to_ns(child
->rt_bandwidth
.rt_period
);
7909 runtime
= child
->rt_bandwidth
.rt_runtime
;
7911 if (child
== d
->tg
) {
7912 period
= d
->rt_period
;
7913 runtime
= d
->rt_runtime
;
7916 sum
+= to_ratio(period
, runtime
);
7925 static int __rt_schedulable(struct task_group
*tg
, u64 period
, u64 runtime
)
7929 struct rt_schedulable_data data
= {
7931 .rt_period
= period
,
7932 .rt_runtime
= runtime
,
7936 ret
= walk_tg_tree(tg_rt_schedulable
, tg_nop
, &data
);
7942 static int tg_set_rt_bandwidth(struct task_group
*tg
,
7943 u64 rt_period
, u64 rt_runtime
)
7947 mutex_lock(&rt_constraints_mutex
);
7948 read_lock(&tasklist_lock
);
7949 err
= __rt_schedulable(tg
, rt_period
, rt_runtime
);
7953 raw_spin_lock_irq(&tg
->rt_bandwidth
.rt_runtime_lock
);
7954 tg
->rt_bandwidth
.rt_period
= ns_to_ktime(rt_period
);
7955 tg
->rt_bandwidth
.rt_runtime
= rt_runtime
;
7957 for_each_possible_cpu(i
) {
7958 struct rt_rq
*rt_rq
= tg
->rt_rq
[i
];
7960 raw_spin_lock(&rt_rq
->rt_runtime_lock
);
7961 rt_rq
->rt_runtime
= rt_runtime
;
7962 raw_spin_unlock(&rt_rq
->rt_runtime_lock
);
7964 raw_spin_unlock_irq(&tg
->rt_bandwidth
.rt_runtime_lock
);
7966 read_unlock(&tasklist_lock
);
7967 mutex_unlock(&rt_constraints_mutex
);
7972 static int sched_group_set_rt_runtime(struct task_group
*tg
, long rt_runtime_us
)
7974 u64 rt_runtime
, rt_period
;
7976 rt_period
= ktime_to_ns(tg
->rt_bandwidth
.rt_period
);
7977 rt_runtime
= (u64
)rt_runtime_us
* NSEC_PER_USEC
;
7978 if (rt_runtime_us
< 0)
7979 rt_runtime
= RUNTIME_INF
;
7981 return tg_set_rt_bandwidth(tg
, rt_period
, rt_runtime
);
7984 static long sched_group_rt_runtime(struct task_group
*tg
)
7988 if (tg
->rt_bandwidth
.rt_runtime
== RUNTIME_INF
)
7991 rt_runtime_us
= tg
->rt_bandwidth
.rt_runtime
;
7992 do_div(rt_runtime_us
, NSEC_PER_USEC
);
7993 return rt_runtime_us
;
7996 static int sched_group_set_rt_period(struct task_group
*tg
, long rt_period_us
)
7998 u64 rt_runtime
, rt_period
;
8000 rt_period
= (u64
)rt_period_us
* NSEC_PER_USEC
;
8001 rt_runtime
= tg
->rt_bandwidth
.rt_runtime
;
8006 return tg_set_rt_bandwidth(tg
, rt_period
, rt_runtime
);
8009 static long sched_group_rt_period(struct task_group
*tg
)
8013 rt_period_us
= ktime_to_ns(tg
->rt_bandwidth
.rt_period
);
8014 do_div(rt_period_us
, NSEC_PER_USEC
);
8015 return rt_period_us
;
8018 static int sched_rt_global_constraints(void)
8020 u64 runtime
, period
;
8023 if (sysctl_sched_rt_period
<= 0)
8026 runtime
= global_rt_runtime();
8027 period
= global_rt_period();
8030 * Sanity check on the sysctl variables.
8032 if (runtime
> period
&& runtime
!= RUNTIME_INF
)
8035 mutex_lock(&rt_constraints_mutex
);
8036 read_lock(&tasklist_lock
);
8037 ret
= __rt_schedulable(NULL
, 0, 0);
8038 read_unlock(&tasklist_lock
);
8039 mutex_unlock(&rt_constraints_mutex
);
8044 static int sched_rt_can_attach(struct task_group
*tg
, struct task_struct
*tsk
)
8046 /* Don't accept realtime tasks when there is no way for them to run */
8047 if (rt_task(tsk
) && tg
->rt_bandwidth
.rt_runtime
== 0)
8053 #else /* !CONFIG_RT_GROUP_SCHED */
8054 static int sched_rt_global_constraints(void)
8056 unsigned long flags
;
8059 if (sysctl_sched_rt_period
<= 0)
8063 * There's always some RT tasks in the root group
8064 * -- migration, kstopmachine etc..
8066 if (sysctl_sched_rt_runtime
== 0)
8069 raw_spin_lock_irqsave(&def_rt_bandwidth
.rt_runtime_lock
, flags
);
8070 for_each_possible_cpu(i
) {
8071 struct rt_rq
*rt_rq
= &cpu_rq(i
)->rt
;
8073 raw_spin_lock(&rt_rq
->rt_runtime_lock
);
8074 rt_rq
->rt_runtime
= global_rt_runtime();
8075 raw_spin_unlock(&rt_rq
->rt_runtime_lock
);
8077 raw_spin_unlock_irqrestore(&def_rt_bandwidth
.rt_runtime_lock
, flags
);
8081 #endif /* CONFIG_RT_GROUP_SCHED */
8083 int sched_rr_handler(struct ctl_table
*table
, int write
,
8084 void __user
*buffer
, size_t *lenp
,
8088 static DEFINE_MUTEX(mutex
);
8091 ret
= proc_dointvec(table
, write
, buffer
, lenp
, ppos
);
8092 /* make sure that internally we keep jiffies */
8093 /* also, writing zero resets timeslice to default */
8094 if (!ret
&& write
) {
8095 sched_rr_timeslice
= sched_rr_timeslice
<= 0 ?
8096 RR_TIMESLICE
: msecs_to_jiffies(sched_rr_timeslice
);
8098 mutex_unlock(&mutex
);
8102 int sched_rt_handler(struct ctl_table
*table
, int write
,
8103 void __user
*buffer
, size_t *lenp
,
8107 int old_period
, old_runtime
;
8108 static DEFINE_MUTEX(mutex
);
8111 old_period
= sysctl_sched_rt_period
;
8112 old_runtime
= sysctl_sched_rt_runtime
;
8114 ret
= proc_dointvec(table
, write
, buffer
, lenp
, ppos
);
8116 if (!ret
&& write
) {
8117 ret
= sched_rt_global_constraints();
8119 sysctl_sched_rt_period
= old_period
;
8120 sysctl_sched_rt_runtime
= old_runtime
;
8122 def_rt_bandwidth
.rt_runtime
= global_rt_runtime();
8123 def_rt_bandwidth
.rt_period
=
8124 ns_to_ktime(global_rt_period());
8127 mutex_unlock(&mutex
);
8132 #ifdef CONFIG_CGROUP_SCHED
8134 /* return corresponding task_group object of a cgroup */
8135 static inline struct task_group
*cgroup_tg(struct cgroup
*cgrp
)
8137 return container_of(cgroup_subsys_state(cgrp
, cpu_cgroup_subsys_id
),
8138 struct task_group
, css
);
8141 static struct cgroup_subsys_state
*cpu_cgroup_css_alloc(struct cgroup
*cgrp
)
8143 struct task_group
*tg
, *parent
;
8145 if (!cgrp
->parent
) {
8146 /* This is early initialization for the top cgroup */
8147 return &root_task_group
.css
;
8150 parent
= cgroup_tg(cgrp
->parent
);
8151 tg
= sched_create_group(parent
);
8153 return ERR_PTR(-ENOMEM
);
8158 static int cpu_cgroup_css_online(struct cgroup
*cgrp
)
8160 struct task_group
*tg
= cgroup_tg(cgrp
);
8161 struct task_group
*parent
;
8166 parent
= cgroup_tg(cgrp
->parent
);
8167 sched_online_group(tg
, parent
);
8171 static void cpu_cgroup_css_free(struct cgroup
*cgrp
)
8173 struct task_group
*tg
= cgroup_tg(cgrp
);
8175 sched_destroy_group(tg
);
8178 static void cpu_cgroup_css_offline(struct cgroup
*cgrp
)
8180 struct task_group
*tg
= cgroup_tg(cgrp
);
8182 sched_offline_group(tg
);
8186 cpu_cgroup_allow_attach(struct cgroup
*cgrp
, struct cgroup_taskset
*tset
)
8188 const struct cred
*cred
= current_cred(), *tcred
;
8189 struct task_struct
*task
;
8191 cgroup_taskset_for_each(task
, cgrp
, tset
) {
8192 tcred
= __task_cred(task
);
8194 if ((current
!= task
) && !capable(CAP_SYS_NICE
) &&
8195 cred
->euid
!= tcred
->uid
&& cred
->euid
!= tcred
->suid
)
8202 static int cpu_cgroup_can_attach(struct cgroup
*cgrp
,
8203 struct cgroup_taskset
*tset
)
8205 struct task_struct
*task
;
8207 cgroup_taskset_for_each(task
, cgrp
, tset
) {
8208 #ifdef CONFIG_RT_GROUP_SCHED
8209 if (!sched_rt_can_attach(cgroup_tg(cgrp
), task
))
8212 /* We don't support RT-tasks being in separate groups */
8213 if (task
->sched_class
!= &fair_sched_class
)
8220 static void cpu_cgroup_attach(struct cgroup
*cgrp
,
8221 struct cgroup_taskset
*tset
)
8223 struct task_struct
*task
;
8225 cgroup_taskset_for_each(task
, cgrp
, tset
)
8226 sched_move_task(task
);
8230 cpu_cgroup_exit(struct cgroup
*cgrp
, struct cgroup
*old_cgrp
,
8231 struct task_struct
*task
)
8234 * cgroup_exit() is called in the copy_process() failure path.
8235 * Ignore this case since the task hasn't ran yet, this avoids
8236 * trying to poke a half freed task state from generic code.
8238 if (!(task
->flags
& PF_EXITING
))
8241 sched_move_task(task
);
8244 #ifdef CONFIG_FAIR_GROUP_SCHED
8245 static int cpu_shares_write_u64(struct cgroup
*cgrp
, struct cftype
*cftype
,
8248 return sched_group_set_shares(cgroup_tg(cgrp
), scale_load(shareval
));
8251 static u64
cpu_shares_read_u64(struct cgroup
*cgrp
, struct cftype
*cft
)
8253 struct task_group
*tg
= cgroup_tg(cgrp
);
8255 return (u64
) scale_load_down(tg
->shares
);
8258 #ifdef CONFIG_CFS_BANDWIDTH
8259 static DEFINE_MUTEX(cfs_constraints_mutex
);
8261 const u64 max_cfs_quota_period
= 1 * NSEC_PER_SEC
; /* 1s */
8262 const u64 min_cfs_quota_period
= 1 * NSEC_PER_MSEC
; /* 1ms */
8264 static int __cfs_schedulable(struct task_group
*tg
, u64 period
, u64 runtime
);
8266 static int tg_set_cfs_bandwidth(struct task_group
*tg
, u64 period
, u64 quota
)
8268 int i
, ret
= 0, runtime_enabled
, runtime_was_enabled
;
8269 struct cfs_bandwidth
*cfs_b
= &tg
->cfs_bandwidth
;
8271 if (tg
== &root_task_group
)
8275 * Ensure we have at some amount of bandwidth every period. This is
8276 * to prevent reaching a state of large arrears when throttled via
8277 * entity_tick() resulting in prolonged exit starvation.
8279 if (quota
< min_cfs_quota_period
|| period
< min_cfs_quota_period
)
8283 * Likewise, bound things on the otherside by preventing insane quota
8284 * periods. This also allows us to normalize in computing quota
8287 if (period
> max_cfs_quota_period
)
8290 mutex_lock(&cfs_constraints_mutex
);
8291 ret
= __cfs_schedulable(tg
, period
, quota
);
8295 runtime_enabled
= quota
!= RUNTIME_INF
;
8296 runtime_was_enabled
= cfs_b
->quota
!= RUNTIME_INF
;
8298 * If we need to toggle cfs_bandwidth_used, off->on must occur
8299 * before making related changes, and on->off must occur afterwards
8301 if (runtime_enabled
&& !runtime_was_enabled
)
8302 cfs_bandwidth_usage_inc();
8303 raw_spin_lock_irq(&cfs_b
->lock
);
8304 cfs_b
->period
= ns_to_ktime(period
);
8305 cfs_b
->quota
= quota
;
8307 __refill_cfs_bandwidth_runtime(cfs_b
);
8308 /* restart the period timer (if active) to handle new period expiry */
8309 if (runtime_enabled
&& cfs_b
->timer_active
) {
8310 /* force a reprogram */
8311 cfs_b
->timer_active
= 0;
8312 __start_cfs_bandwidth(cfs_b
);
8314 raw_spin_unlock_irq(&cfs_b
->lock
);
8316 for_each_possible_cpu(i
) {
8317 struct cfs_rq
*cfs_rq
= tg
->cfs_rq
[i
];
8318 struct rq
*rq
= cfs_rq
->rq
;
8320 raw_spin_lock_irq(&rq
->lock
);
8321 cfs_rq
->runtime_enabled
= runtime_enabled
;
8322 cfs_rq
->runtime_remaining
= 0;
8324 if (cfs_rq
->throttled
)
8325 unthrottle_cfs_rq(cfs_rq
);
8326 raw_spin_unlock_irq(&rq
->lock
);
8328 if (runtime_was_enabled
&& !runtime_enabled
)
8329 cfs_bandwidth_usage_dec();
8331 mutex_unlock(&cfs_constraints_mutex
);
8336 int tg_set_cfs_quota(struct task_group
*tg
, long cfs_quota_us
)
8340 period
= ktime_to_ns(tg
->cfs_bandwidth
.period
);
8341 if (cfs_quota_us
< 0)
8342 quota
= RUNTIME_INF
;
8344 quota
= (u64
)cfs_quota_us
* NSEC_PER_USEC
;
8346 return tg_set_cfs_bandwidth(tg
, period
, quota
);
8349 long tg_get_cfs_quota(struct task_group
*tg
)
8353 if (tg
->cfs_bandwidth
.quota
== RUNTIME_INF
)
8356 quota_us
= tg
->cfs_bandwidth
.quota
;
8357 do_div(quota_us
, NSEC_PER_USEC
);
8362 int tg_set_cfs_period(struct task_group
*tg
, long cfs_period_us
)
8366 period
= (u64
)cfs_period_us
* NSEC_PER_USEC
;
8367 quota
= tg
->cfs_bandwidth
.quota
;
8369 return tg_set_cfs_bandwidth(tg
, period
, quota
);
8372 long tg_get_cfs_period(struct task_group
*tg
)
8376 cfs_period_us
= ktime_to_ns(tg
->cfs_bandwidth
.period
);
8377 do_div(cfs_period_us
, NSEC_PER_USEC
);
8379 return cfs_period_us
;
8382 static s64
cpu_cfs_quota_read_s64(struct cgroup
*cgrp
, struct cftype
*cft
)
8384 return tg_get_cfs_quota(cgroup_tg(cgrp
));
8387 static int cpu_cfs_quota_write_s64(struct cgroup
*cgrp
, struct cftype
*cftype
,
8390 return tg_set_cfs_quota(cgroup_tg(cgrp
), cfs_quota_us
);
8393 static u64
cpu_cfs_period_read_u64(struct cgroup
*cgrp
, struct cftype
*cft
)
8395 return tg_get_cfs_period(cgroup_tg(cgrp
));
8398 static int cpu_cfs_period_write_u64(struct cgroup
*cgrp
, struct cftype
*cftype
,
8401 return tg_set_cfs_period(cgroup_tg(cgrp
), cfs_period_us
);
8404 struct cfs_schedulable_data
{
8405 struct task_group
*tg
;
8410 * normalize group quota/period to be quota/max_period
8411 * note: units are usecs
8413 static u64
normalize_cfs_quota(struct task_group
*tg
,
8414 struct cfs_schedulable_data
*d
)
8422 period
= tg_get_cfs_period(tg
);
8423 quota
= tg_get_cfs_quota(tg
);
8426 /* note: these should typically be equivalent */
8427 if (quota
== RUNTIME_INF
|| quota
== -1)
8430 return to_ratio(period
, quota
);
8433 static int tg_cfs_schedulable_down(struct task_group
*tg
, void *data
)
8435 struct cfs_schedulable_data
*d
= data
;
8436 struct cfs_bandwidth
*cfs_b
= &tg
->cfs_bandwidth
;
8437 s64 quota
= 0, parent_quota
= -1;
8440 quota
= RUNTIME_INF
;
8442 struct cfs_bandwidth
*parent_b
= &tg
->parent
->cfs_bandwidth
;
8444 quota
= normalize_cfs_quota(tg
, d
);
8445 parent_quota
= parent_b
->hierarchal_quota
;
8448 * ensure max(child_quota) <= parent_quota, inherit when no
8451 if (quota
== RUNTIME_INF
)
8452 quota
= parent_quota
;
8453 else if (parent_quota
!= RUNTIME_INF
&& quota
> parent_quota
)
8456 cfs_b
->hierarchal_quota
= quota
;
8461 static int __cfs_schedulable(struct task_group
*tg
, u64 period
, u64 quota
)
8464 struct cfs_schedulable_data data
= {
8470 if (quota
!= RUNTIME_INF
) {
8471 do_div(data
.period
, NSEC_PER_USEC
);
8472 do_div(data
.quota
, NSEC_PER_USEC
);
8476 ret
= walk_tg_tree(tg_cfs_schedulable_down
, tg_nop
, &data
);
8482 static int cpu_stats_show(struct cgroup
*cgrp
, struct cftype
*cft
,
8483 struct cgroup_map_cb
*cb
)
8485 struct task_group
*tg
= cgroup_tg(cgrp
);
8486 struct cfs_bandwidth
*cfs_b
= &tg
->cfs_bandwidth
;
8488 cb
->fill(cb
, "nr_periods", cfs_b
->nr_periods
);
8489 cb
->fill(cb
, "nr_throttled", cfs_b
->nr_throttled
);
8490 cb
->fill(cb
, "throttled_time", cfs_b
->throttled_time
);
8494 #endif /* CONFIG_CFS_BANDWIDTH */
8495 #endif /* CONFIG_FAIR_GROUP_SCHED */
8497 #ifdef CONFIG_RT_GROUP_SCHED
8498 static int cpu_rt_runtime_write(struct cgroup
*cgrp
, struct cftype
*cft
,
8501 return sched_group_set_rt_runtime(cgroup_tg(cgrp
), val
);
8504 static s64
cpu_rt_runtime_read(struct cgroup
*cgrp
, struct cftype
*cft
)
8506 return sched_group_rt_runtime(cgroup_tg(cgrp
));
8509 static int cpu_rt_period_write_uint(struct cgroup
*cgrp
, struct cftype
*cftype
,
8512 return sched_group_set_rt_period(cgroup_tg(cgrp
), rt_period_us
);
8515 static u64
cpu_rt_period_read_uint(struct cgroup
*cgrp
, struct cftype
*cft
)
8517 return sched_group_rt_period(cgroup_tg(cgrp
));
8519 #endif /* CONFIG_RT_GROUP_SCHED */
8521 static struct cftype cpu_files
[] = {
8522 #ifdef CONFIG_FAIR_GROUP_SCHED
8525 .read_u64
= cpu_shares_read_u64
,
8526 .write_u64
= cpu_shares_write_u64
,
8529 #ifdef CONFIG_CFS_BANDWIDTH
8531 .name
= "cfs_quota_us",
8532 .read_s64
= cpu_cfs_quota_read_s64
,
8533 .write_s64
= cpu_cfs_quota_write_s64
,
8536 .name
= "cfs_period_us",
8537 .read_u64
= cpu_cfs_period_read_u64
,
8538 .write_u64
= cpu_cfs_period_write_u64
,
8542 .read_map
= cpu_stats_show
,
8545 #ifdef CONFIG_RT_GROUP_SCHED
8547 .name
= "rt_runtime_us",
8548 .read_s64
= cpu_rt_runtime_read
,
8549 .write_s64
= cpu_rt_runtime_write
,
8552 .name
= "rt_period_us",
8553 .read_u64
= cpu_rt_period_read_uint
,
8554 .write_u64
= cpu_rt_period_write_uint
,
8560 struct cgroup_subsys cpu_cgroup_subsys
= {
8562 .css_alloc
= cpu_cgroup_css_alloc
,
8563 .css_free
= cpu_cgroup_css_free
,
8564 .css_online
= cpu_cgroup_css_online
,
8565 .css_offline
= cpu_cgroup_css_offline
,
8566 .can_attach
= cpu_cgroup_can_attach
,
8567 .attach
= cpu_cgroup_attach
,
8568 .allow_attach
= cpu_cgroup_allow_attach
,
8569 .exit
= cpu_cgroup_exit
,
8570 .subsys_id
= cpu_cgroup_subsys_id
,
8571 .base_cftypes
= cpu_files
,
8575 #endif /* CONFIG_CGROUP_SCHED */
8577 void dump_cpu_task(int cpu
)
8579 pr_info("Task dump for CPU %d:\n", cpu
);
8580 sched_show_task(cpu_curr(cpu
));
8583 unsigned long long mt_get_thread_cputime(pid_t pid
)
8585 struct task_struct
*p
;
8586 p
= pid
? find_task_by_vpid(pid
) : current
;
8587 return task_sched_runtime(p
);
8589 unsigned long long mt_get_cpu_idle(int cpu
)
8591 unsigned long long *unused
= 0;
8592 return get_cpu_idle_time_us(cpu
, unused
);
8594 unsigned long long mt_sched_clock(void)
8596 return sched_clock();
8598 EXPORT_SYMBOL(mt_get_thread_cputime
);
8599 EXPORT_SYMBOL(mt_get_cpu_idle
);
8600 EXPORT_SYMBOL(mt_sched_clock
);