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 if (static_key_enabled(&sched_feat_keys
[i
]))
196 static_key_slow_dec(&sched_feat_keys
[i
]);
199 static void sched_feat_enable(int i
)
201 if (!static_key_enabled(&sched_feat_keys
[i
]))
202 static_key_slow_inc(&sched_feat_keys
[i
]);
205 static void sched_feat_disable(int i
) { };
206 static void sched_feat_enable(int i
) { };
207 #endif /* HAVE_JUMP_LABEL */
209 static int sched_feat_set(char *cmp
)
214 if (strncmp(cmp
, "NO_", 3) == 0) {
219 for (i
= 0; i
< __SCHED_FEAT_NR
; i
++) {
220 if (strcmp(cmp
, sched_feat_names
[i
]) == 0) {
222 sysctl_sched_features
&= ~(1UL << i
);
223 sched_feat_disable(i
);
225 sysctl_sched_features
|= (1UL << i
);
226 sched_feat_enable(i
);
236 sched_feat_write(struct file
*filp
, const char __user
*ubuf
,
237 size_t cnt
, loff_t
*ppos
)
246 if (copy_from_user(&buf
, ubuf
, cnt
))
252 i
= sched_feat_set(cmp
);
253 if (i
== __SCHED_FEAT_NR
)
261 static int sched_feat_open(struct inode
*inode
, struct file
*filp
)
263 return single_open(filp
, sched_feat_show
, NULL
);
266 static const struct file_operations sched_feat_fops
= {
267 .open
= sched_feat_open
,
268 .write
= sched_feat_write
,
271 .release
= single_release
,
274 static __init
int sched_init_debug(void)
276 debugfs_create_file("sched_features", 0644, NULL
, NULL
,
281 late_initcall(sched_init_debug
);
282 #endif /* CONFIG_SCHED_DEBUG */
285 * Number of tasks to iterate in a single balance run.
286 * Limited because this is done with IRQs disabled.
288 const_debug
unsigned int sysctl_sched_nr_migrate
= 32;
291 * period over which we average the RT time consumption, measured
296 const_debug
unsigned int sysctl_sched_time_avg
= MSEC_PER_SEC
;
299 * period over which we measure -rt task cpu usage in us.
302 unsigned int sysctl_sched_rt_period
= 1000000;
304 __read_mostly
int scheduler_running
;
307 * part of the period that we allow rt tasks to run in us.
310 int sysctl_sched_rt_runtime
= 950000;
315 * __task_rq_lock - lock the rq @p resides on.
317 static inline struct rq
*__task_rq_lock(struct task_struct
*p
)
322 lockdep_assert_held(&p
->pi_lock
);
326 raw_spin_lock(&rq
->lock
);
327 if (likely(rq
== task_rq(p
)))
329 raw_spin_unlock(&rq
->lock
);
334 * task_rq_lock - lock p->pi_lock and lock the rq @p resides on.
336 static struct rq
*task_rq_lock(struct task_struct
*p
, unsigned long *flags
)
337 __acquires(p
->pi_lock
)
343 raw_spin_lock_irqsave(&p
->pi_lock
, *flags
);
345 raw_spin_lock(&rq
->lock
);
346 if (likely(rq
== task_rq(p
)))
348 raw_spin_unlock(&rq
->lock
);
349 raw_spin_unlock_irqrestore(&p
->pi_lock
, *flags
);
353 static void __task_rq_unlock(struct rq
*rq
)
356 raw_spin_unlock(&rq
->lock
);
360 task_rq_unlock(struct rq
*rq
, struct task_struct
*p
, unsigned long *flags
)
362 __releases(p
->pi_lock
)
364 raw_spin_unlock(&rq
->lock
);
365 raw_spin_unlock_irqrestore(&p
->pi_lock
, *flags
);
369 * this_rq_lock - lock this runqueue and disable interrupts.
371 static struct rq
*this_rq_lock(void)
378 raw_spin_lock(&rq
->lock
);
383 #ifdef CONFIG_SCHED_HRTICK
385 * Use HR-timers to deliver accurate preemption points.
387 * Its all a bit involved since we cannot program an hrt while holding the
388 * rq->lock. So what we do is store a state in in rq->hrtick_* and ask for a
391 * When we get rescheduled we reprogram the hrtick_timer outside of the
395 static void hrtick_clear(struct rq
*rq
)
397 if (hrtimer_active(&rq
->hrtick_timer
))
398 hrtimer_cancel(&rq
->hrtick_timer
);
402 * High-resolution timer tick.
403 * Runs from hardirq context with interrupts disabled.
405 static enum hrtimer_restart
hrtick(struct hrtimer
*timer
)
407 struct rq
*rq
= container_of(timer
, struct rq
, hrtick_timer
);
409 WARN_ON_ONCE(cpu_of(rq
) != smp_processor_id());
411 raw_spin_lock(&rq
->lock
);
413 rq
->curr
->sched_class
->task_tick(rq
, rq
->curr
, 1);
414 raw_spin_unlock(&rq
->lock
);
416 return HRTIMER_NORESTART
;
421 * called from hardirq (IPI) context
423 static void __hrtick_start(void *arg
)
427 raw_spin_lock(&rq
->lock
);
428 hrtimer_restart(&rq
->hrtick_timer
);
429 rq
->hrtick_csd_pending
= 0;
430 raw_spin_unlock(&rq
->lock
);
434 * Called to set the hrtick timer state.
436 * called with rq->lock held and irqs disabled
438 void hrtick_start(struct rq
*rq
, u64 delay
)
440 struct hrtimer
*timer
= &rq
->hrtick_timer
;
441 ktime_t time
= ktime_add_ns(timer
->base
->get_time(), delay
);
443 hrtimer_set_expires(timer
, time
);
445 if (rq
== this_rq()) {
446 hrtimer_restart(timer
);
447 } else if (!rq
->hrtick_csd_pending
) {
448 __smp_call_function_single(cpu_of(rq
), &rq
->hrtick_csd
, 0);
449 rq
->hrtick_csd_pending
= 1;
454 hotplug_hrtick(struct notifier_block
*nfb
, unsigned long action
, void *hcpu
)
456 int cpu
= (int)(long)hcpu
;
459 case CPU_UP_CANCELED
:
460 case CPU_UP_CANCELED_FROZEN
:
461 case CPU_DOWN_PREPARE
:
462 case CPU_DOWN_PREPARE_FROZEN
:
464 case CPU_DEAD_FROZEN
:
465 hrtick_clear(cpu_rq(cpu
));
472 static __init
void init_hrtick(void)
474 hotcpu_notifier(hotplug_hrtick
, 0);
478 * Called to set the hrtick timer state.
480 * called with rq->lock held and irqs disabled
482 void hrtick_start(struct rq
*rq
, u64 delay
)
484 __hrtimer_start_range_ns(&rq
->hrtick_timer
, ns_to_ktime(delay
), 0,
485 HRTIMER_MODE_REL_PINNED
, 0);
488 static inline void init_hrtick(void)
491 #endif /* CONFIG_SMP */
493 static void init_rq_hrtick(struct rq
*rq
)
496 rq
->hrtick_csd_pending
= 0;
498 rq
->hrtick_csd
.flags
= 0;
499 rq
->hrtick_csd
.func
= __hrtick_start
;
500 rq
->hrtick_csd
.info
= rq
;
503 hrtimer_init(&rq
->hrtick_timer
, CLOCK_MONOTONIC
, HRTIMER_MODE_REL
);
504 rq
->hrtick_timer
.function
= hrtick
;
506 #else /* CONFIG_SCHED_HRTICK */
507 static inline void hrtick_clear(struct rq
*rq
)
511 static inline void init_rq_hrtick(struct rq
*rq
)
515 static inline void init_hrtick(void)
518 #endif /* CONFIG_SCHED_HRTICK */
521 * resched_task - mark a task 'to be rescheduled now'.
523 * On UP this means the setting of the need_resched flag, on SMP it
524 * might also involve a cross-CPU call to trigger the scheduler on
528 void resched_task(struct task_struct
*p
)
532 assert_raw_spin_locked(&task_rq(p
)->lock
);
534 if (test_tsk_need_resched(p
))
537 set_tsk_need_resched(p
);
540 if (cpu
== smp_processor_id())
543 /* NEED_RESCHED must be visible before we test polling */
545 if (!tsk_is_polling(p
))
546 smp_send_reschedule(cpu
);
549 void resched_cpu(int cpu
)
551 struct rq
*rq
= cpu_rq(cpu
);
554 if (!raw_spin_trylock_irqsave(&rq
->lock
, flags
))
556 resched_task(cpu_curr(cpu
));
557 raw_spin_unlock_irqrestore(&rq
->lock
, flags
);
560 #ifdef CONFIG_NO_HZ_COMMON
562 * In the semi idle case, use the nearest busy cpu for migrating timers
563 * from an idle cpu. This is good for power-savings.
565 * We don't do similar optimization for completely idle system, as
566 * selecting an idle cpu will add more delays to the timers than intended
567 * (as that cpu's timer base may not be uptodate wrt jiffies etc).
569 int get_nohz_timer_target(void)
571 int cpu
= smp_processor_id();
573 struct sched_domain
*sd
;
576 for_each_domain(cpu
, sd
) {
577 for_each_cpu(i
, sched_domain_span(sd
)) {
589 * When add_timer_on() enqueues a timer into the timer wheel of an
590 * idle CPU then this timer might expire before the next timer event
591 * which is scheduled to wake up that CPU. In case of a completely
592 * idle system the next event might even be infinite time into the
593 * future. wake_up_idle_cpu() ensures that the CPU is woken up and
594 * leaves the inner idle loop so the newly added timer is taken into
595 * account when the CPU goes back to idle and evaluates the timer
596 * wheel for the next timer event.
598 static void wake_up_idle_cpu(int cpu
)
600 struct rq
*rq
= cpu_rq(cpu
);
602 if (cpu
== smp_processor_id())
606 * This is safe, as this function is called with the timer
607 * wheel base lock of (cpu) held. When the CPU is on the way
608 * to idle and has not yet set rq->curr to idle then it will
609 * be serialized on the timer wheel base lock and take the new
610 * timer into account automatically.
612 if (rq
->curr
!= rq
->idle
)
616 * We can set TIF_RESCHED on the idle task of the other CPU
617 * lockless. The worst case is that the other CPU runs the
618 * idle task through an additional NOOP schedule()
620 set_tsk_need_resched(rq
->idle
);
622 /* NEED_RESCHED must be visible before we test polling */
624 if (!tsk_is_polling(rq
->idle
))
625 smp_send_reschedule(cpu
);
628 static bool wake_up_full_nohz_cpu(int cpu
)
630 if (tick_nohz_full_cpu(cpu
)) {
631 if (cpu
!= smp_processor_id() ||
632 tick_nohz_tick_stopped())
633 smp_send_reschedule(cpu
);
640 void wake_up_nohz_cpu(int cpu
)
642 if (!wake_up_full_nohz_cpu(cpu
))
643 wake_up_idle_cpu(cpu
);
646 static inline bool got_nohz_idle_kick(void)
648 int cpu
= smp_processor_id();
650 if (!test_bit(NOHZ_BALANCE_KICK
, nohz_flags(cpu
)))
653 if (idle_cpu(cpu
) && !need_resched())
657 * We can't run Idle Load Balance on this CPU for this time so we
658 * cancel it and clear NOHZ_BALANCE_KICK
660 clear_bit(NOHZ_BALANCE_KICK
, nohz_flags(cpu
));
664 #else /* CONFIG_NO_HZ_COMMON */
666 static inline bool got_nohz_idle_kick(void)
671 #endif /* CONFIG_NO_HZ_COMMON */
673 #ifdef CONFIG_NO_HZ_FULL
674 bool sched_can_stop_tick(void)
680 /* Make sure rq->nr_running update is visible after the IPI */
683 /* More than one running task need preemption */
684 if (rq
->nr_running
> 1)
689 #endif /* CONFIG_NO_HZ_FULL */
691 void sched_avg_update(struct rq
*rq
)
693 s64 period
= sched_avg_period();
695 while ((s64
)(rq
->clock
- rq
->age_stamp
) > period
) {
697 * Inline assembly required to prevent the compiler
698 * optimising this loop into a divmod call.
699 * See __iter_div_u64_rem() for another example of this.
701 asm("" : "+rm" (rq
->age_stamp
));
702 rq
->age_stamp
+= period
;
707 #else /* !CONFIG_SMP */
708 void resched_task(struct task_struct
*p
)
710 assert_raw_spin_locked(&task_rq(p
)->lock
);
711 set_tsk_need_resched(p
);
713 #endif /* CONFIG_SMP */
715 #if defined(CONFIG_RT_GROUP_SCHED) || (defined(CONFIG_FAIR_GROUP_SCHED) && \
716 (defined(CONFIG_SMP) || defined(CONFIG_CFS_BANDWIDTH)))
718 * Iterate task_group tree rooted at *from, calling @down when first entering a
719 * node and @up when leaving it for the final time.
721 * Caller must hold rcu_lock or sufficient equivalent.
723 int walk_tg_tree_from(struct task_group
*from
,
724 tg_visitor down
, tg_visitor up
, void *data
)
726 struct task_group
*parent
, *child
;
732 ret
= (*down
)(parent
, data
);
735 list_for_each_entry_rcu(child
, &parent
->children
, siblings
) {
742 ret
= (*up
)(parent
, data
);
743 if (ret
|| parent
== from
)
747 parent
= parent
->parent
;
754 int tg_nop(struct task_group
*tg
, void *data
)
760 static void set_load_weight(struct task_struct
*p
)
762 int prio
= p
->static_prio
- MAX_RT_PRIO
;
763 struct load_weight
*load
= &p
->se
.load
;
766 * SCHED_IDLE tasks get minimal weight:
768 if (p
->policy
== SCHED_IDLE
) {
769 load
->weight
= scale_load(WEIGHT_IDLEPRIO
);
770 load
->inv_weight
= WMULT_IDLEPRIO
;
774 load
->weight
= scale_load(prio_to_weight
[prio
]);
775 load
->inv_weight
= prio_to_wmult
[prio
];
778 #ifdef CONFIG_MTK_SCHED_CMP_TGS
779 static void sched_tg_enqueue(struct rq
*rq
, struct task_struct
*p
)
783 struct task_struct
*tg
= p
->group_leader
;
785 if(group_leader_is_empty(p
))
787 id
= get_cluster_id(rq
->cpu
);
788 if (unlikely(WARN_ON(id
< 0)))
791 raw_spin_lock_irqsave(&tg
->thread_group_info_lock
, flags
);
792 tg
->thread_group_info
[id
].nr_running
++;
793 raw_spin_unlock_irqrestore(&tg
->thread_group_info_lock
, flags
);
796 mt_sched_printf("enqueue %d:%s %d:%s %d %lu %lu %lu, %lu %lu %lu",
797 tg
->pid
, tg
->comm
, p
->pid
, p
->comm
, id
, rq
->cpu
,
798 tg
->thread_group_info
[0].nr_running
,
799 tg
->thread_group_info
[0].cfs_nr_running
,
800 tg
->thread_group_info
[0].load_avg_ratio
,
801 tg
->thread_group_info
[1].nr_running
,
802 tg
->thread_group_info
[1].cfs_nr_running
,
803 tg
->thread_group_info
[1].load_avg_ratio
);
808 static void sched_tg_dequeue(struct rq
*rq
, struct task_struct
*p
)
812 struct task_struct
*tg
= p
->group_leader
;
814 if(group_leader_is_empty(p
))
816 id
= get_cluster_id(rq
->cpu
);
817 if (unlikely(WARN_ON(id
< 0)))
820 raw_spin_lock_irqsave(&tg
->thread_group_info_lock
, flags
);
821 //WARN_ON(!tg->thread_group_info[id].nr_running);
822 tg
->thread_group_info
[id
].nr_running
--;
823 raw_spin_unlock_irqrestore(&tg
->thread_group_info_lock
, flags
);
826 mt_sched_printf("dequeue %d:%s %d:%s %d %d %lu %lu %lu, %lu %lu %lu",
827 tg
->pid
, tg
->comm
, p
->pid
, p
->comm
, id
, rq
->cpu
,
828 tg
->thread_group_info
[0].nr_running
,
829 tg
->thread_group_info
[0].cfs_nr_running
,
830 tg
->thread_group_info
[0].load_avg_ratio
,
831 tg
->thread_group_info
[1].nr_running
,
832 tg
->thread_group_info
[1].cfs_nr_running
,
833 tg
->thread_group_info
[1].load_avg_ratio
);
840 #ifdef CONFIG_MTK_SCHED_CMP_TGS
841 static void tgs_log(struct rq
*rq
, struct task_struct
*p
)
843 #ifdef CONFIG_MT_SCHED_INFO
844 struct task_struct
*tg
= p
->group_leader
;
846 if(group_leader_is_empty(p
))
849 // if(!strncmp(tg->comm,"sched_test", 10)){
850 mt_sched_printf("%d:%s %d:%s %lu %lu %lu, %lu %lu %lu", tg
->pid
, tg
->comm
, p
->pid
, p
->comm
,
851 tg
->thread_group_info
[0].nr_running
,
852 tg
->thread_group_info
[0].cfs_nr_running
,
853 tg
->thread_group_info
[0].load_avg_ratio
,
854 tg
->thread_group_info
[1].nr_running
,
855 tg
->thread_group_info
[1].cfs_nr_running
,
856 tg
->thread_group_info
[1].load_avg_ratio
);
862 static void enqueue_task(struct rq
*rq
, struct task_struct
*p
, int flags
)
865 sched_info_queued(p
);
866 p
->sched_class
->enqueue_task(rq
, p
, flags
);
867 #ifdef CONFIG_MTK_SCHED_CMP_TGS
868 sched_tg_enqueue(rq
, p
);
873 static void dequeue_task(struct rq
*rq
, struct task_struct
*p
, int flags
)
876 sched_info_dequeued(p
);
877 p
->sched_class
->dequeue_task(rq
, p
, flags
);
878 #ifdef CONFIG_MTK_SCHED_CMP_TGS
879 sched_tg_dequeue(rq
, p
);
884 void activate_task(struct rq
*rq
, struct task_struct
*p
, int flags
)
886 if (task_contributes_to_load(p
))
887 rq
->nr_uninterruptible
--;
889 enqueue_task(rq
, p
, flags
);
891 #ifdef CONFIG_MT_LOAD_BALANCE_PROFILER
892 if( 2 <= rq
->nr_running
){
893 if (1 == cpumask_weight(&p
->cpus_allowed
))
894 mt_lbprof_update_state_has_lock(rq
->cpu
, MT_LBPROF_AFFINITY_STATE
);
896 mt_lbprof_update_state_has_lock(rq
->cpu
, MT_LBPROF_N_TASK_STATE
);
897 }else if ( (1 == rq
->nr_running
)){
898 mt_lbprof_update_state_has_lock(rq
->cpu
, MT_LBPROF_ONE_TASK_STATE
);
903 void deactivate_task(struct rq
*rq
, struct task_struct
*p
, int flags
)
905 if (task_contributes_to_load(p
))
906 rq
->nr_uninterruptible
++;
908 dequeue_task(rq
, p
, flags
);
910 #ifdef CONFIG_MT_LOAD_BALANCE_PROFILER
911 if ( 1 == rq
->nr_running
)
912 mt_lbprof_update_state_has_lock(rq
->cpu
, MT_LBPROF_ONE_TASK_STATE
);
913 else if (0 == rq
->nr_running
)
914 mt_lbprof_update_state_has_lock(rq
->cpu
, MT_LBPROF_NO_TASK_STATE
);
918 static void update_rq_clock_task(struct rq
*rq
, s64 delta
)
921 * In theory, the compile should just see 0 here, and optimize out the call
922 * to sched_rt_avg_update. But I don't trust it...
924 #if defined(CONFIG_IRQ_TIME_ACCOUNTING) || defined(CONFIG_PARAVIRT_TIME_ACCOUNTING)
925 s64 steal
= 0, irq_delta
= 0;
927 #ifdef CONFIG_IRQ_TIME_ACCOUNTING
928 irq_delta
= irq_time_read(cpu_of(rq
)) - rq
->prev_irq_time
;
931 * Since irq_time is only updated on {soft,}irq_exit, we might run into
932 * this case when a previous update_rq_clock() happened inside a
935 * When this happens, we stop ->clock_task and only update the
936 * prev_irq_time stamp to account for the part that fit, so that a next
937 * update will consume the rest. This ensures ->clock_task is
940 * It does however cause some slight miss-attribution of {soft,}irq
941 * time, a more accurate solution would be to update the irq_time using
942 * the current rq->clock timestamp, except that would require using
945 if (irq_delta
> delta
)
948 rq
->prev_irq_time
+= irq_delta
;
951 #ifdef CONFIG_PARAVIRT_TIME_ACCOUNTING
952 if (static_key_false((¶virt_steal_rq_enabled
))) {
955 steal
= paravirt_steal_clock(cpu_of(rq
));
956 steal
-= rq
->prev_steal_time_rq
;
958 if (unlikely(steal
> delta
))
961 st
= steal_ticks(steal
);
962 steal
= st
* TICK_NSEC
;
964 rq
->prev_steal_time_rq
+= steal
;
970 rq
->clock_task
+= delta
;
972 #if defined(CONFIG_IRQ_TIME_ACCOUNTING) || defined(CONFIG_PARAVIRT_TIME_ACCOUNTING)
973 if ((irq_delta
+ steal
) && sched_feat(NONTASK_POWER
))
974 sched_rt_avg_update(rq
, irq_delta
+ steal
);
978 void sched_set_stop_task(int cpu
, struct task_struct
*stop
)
980 //struct sched_param param = { .sched_priority = MAX_RT_PRIO - 1 };
981 struct sched_param param
= { .sched_priority
= RTPM_PRIO_CPU_CALLBACK
};
982 struct task_struct
*old_stop
= cpu_rq(cpu
)->stop
;
986 * Make it appear like a SCHED_FIFO task, its something
987 * userspace knows about and won't get confused about.
989 * Also, it will make PI more or less work without too
990 * much confusion -- but then, stop work should not
991 * rely on PI working anyway.
993 sched_setscheduler_nocheck(stop
, SCHED_FIFO
, ¶m
);
995 stop
->sched_class
= &stop_sched_class
;
998 cpu_rq(cpu
)->stop
= stop
;
1002 * Reset it back to a normal scheduling class so that
1003 * it can die in pieces.
1005 old_stop
->sched_class
= &rt_sched_class
;
1010 * __normal_prio - return the priority that is based on the static prio
1012 static inline int __normal_prio(struct task_struct
*p
)
1014 return p
->static_prio
;
1018 * Calculate the expected normal priority: i.e. priority
1019 * without taking RT-inheritance into account. Might be
1020 * boosted by interactivity modifiers. Changes upon fork,
1021 * setprio syscalls, and whenever the interactivity
1022 * estimator recalculates.
1024 static inline int normal_prio(struct task_struct
*p
)
1028 if (task_has_rt_policy(p
))
1029 prio
= MAX_RT_PRIO
-1 - p
->rt_priority
;
1031 prio
= __normal_prio(p
);
1036 * Calculate the current priority, i.e. the priority
1037 * taken into account by the scheduler. This value might
1038 * be boosted by RT tasks, or might be boosted by
1039 * interactivity modifiers. Will be RT if the task got
1040 * RT-boosted. If not then it returns p->normal_prio.
1042 static int effective_prio(struct task_struct
*p
)
1044 p
->normal_prio
= normal_prio(p
);
1046 * If we are RT tasks or we were boosted to RT priority,
1047 * keep the priority unchanged. Otherwise, update priority
1048 * to the normal priority:
1050 if (!rt_prio(p
->prio
))
1051 return p
->normal_prio
;
1056 * task_curr - is this task currently executing on a CPU?
1057 * @p: the task in question.
1059 inline int task_curr(const struct task_struct
*p
)
1061 return cpu_curr(task_cpu(p
)) == p
;
1064 static inline void check_class_changed(struct rq
*rq
, struct task_struct
*p
,
1065 const struct sched_class
*prev_class
,
1068 if (prev_class
!= p
->sched_class
) {
1069 if (prev_class
->switched_from
)
1070 prev_class
->switched_from(rq
, p
);
1071 p
->sched_class
->switched_to(rq
, p
);
1072 } else if (oldprio
!= p
->prio
)
1073 p
->sched_class
->prio_changed(rq
, p
, oldprio
);
1076 void check_preempt_curr(struct rq
*rq
, struct task_struct
*p
, int flags
)
1078 const struct sched_class
*class;
1080 if (p
->sched_class
== rq
->curr
->sched_class
) {
1081 rq
->curr
->sched_class
->check_preempt_curr(rq
, p
, flags
);
1083 for_each_class(class) {
1084 if (class == rq
->curr
->sched_class
)
1086 if (class == p
->sched_class
) {
1087 resched_task(rq
->curr
);
1094 * A queue event has occurred, and we're going to schedule. In
1095 * this case, we can save a useless back to back clock update.
1097 if (rq
->curr
->on_rq
&& test_tsk_need_resched(rq
->curr
))
1098 rq
->skip_clock_update
= 1;
1101 static ATOMIC_NOTIFIER_HEAD(task_migration_notifier
);
1103 void register_task_migration_notifier(struct notifier_block
*n
)
1105 atomic_notifier_chain_register(&task_migration_notifier
, n
);
1109 void set_task_cpu(struct task_struct
*p
, unsigned int new_cpu
)
1111 #ifdef CONFIG_SCHED_DEBUG
1113 * We should never call set_task_cpu() on a blocked task,
1114 * ttwu() will sort out the placement.
1116 WARN_ON_ONCE(p
->state
!= TASK_RUNNING
&& p
->state
!= TASK_WAKING
&&
1117 !(task_thread_info(p
)->preempt_count
& PREEMPT_ACTIVE
));
1119 #ifdef CONFIG_LOCKDEP
1121 * The caller should hold either p->pi_lock or rq->lock, when changing
1122 * a task's CPU. ->pi_lock for waking tasks, rq->lock for runnable tasks.
1124 * sched_move_task() holds both and thus holding either pins the cgroup,
1127 * Furthermore, all task_rq users should acquire both locks, see
1130 WARN_ON_ONCE(debug_locks
&& !(lockdep_is_held(&p
->pi_lock
) ||
1131 lockdep_is_held(&task_rq(p
)->lock
)));
1135 trace_sched_migrate_task(p
, new_cpu
);
1137 if (task_cpu(p
) != new_cpu
) {
1138 struct task_migration_notifier tmn
;
1140 if (p
->sched_class
->migrate_task_rq
)
1141 p
->sched_class
->migrate_task_rq(p
, new_cpu
);
1142 p
->se
.nr_migrations
++;
1143 perf_sw_event(PERF_COUNT_SW_CPU_MIGRATIONS
, 1, NULL
, 0);
1146 tmn
.from_cpu
= task_cpu(p
);
1147 tmn
.to_cpu
= new_cpu
;
1149 atomic_notifier_call_chain(&task_migration_notifier
, 0, &tmn
);
1152 __set_task_cpu(p
, new_cpu
);
1155 struct migration_arg
{
1156 struct task_struct
*task
;
1160 static int migration_cpu_stop(void *data
);
1163 * wait_task_inactive - wait for a thread to unschedule.
1165 * If @match_state is nonzero, it's the @p->state value just checked and
1166 * not expected to change. If it changes, i.e. @p might have woken up,
1167 * then return zero. When we succeed in waiting for @p to be off its CPU,
1168 * we return a positive number (its total switch count). If a second call
1169 * a short while later returns the same number, the caller can be sure that
1170 * @p has remained unscheduled the whole time.
1172 * The caller must ensure that the task *will* unschedule sometime soon,
1173 * else this function might spin for a *long* time. This function can't
1174 * be called with interrupts off, or it may introduce deadlock with
1175 * smp_call_function() if an IPI is sent by the same process we are
1176 * waiting to become inactive.
1178 unsigned long wait_task_inactive(struct task_struct
*p
, long match_state
)
1180 unsigned long flags
;
1187 * We do the initial early heuristics without holding
1188 * any task-queue locks at all. We'll only try to get
1189 * the runqueue lock when things look like they will
1195 * If the task is actively running on another CPU
1196 * still, just relax and busy-wait without holding
1199 * NOTE! Since we don't hold any locks, it's not
1200 * even sure that "rq" stays as the right runqueue!
1201 * But we don't care, since "task_running()" will
1202 * return false if the runqueue has changed and p
1203 * is actually now running somewhere else!
1205 while (task_running(rq
, p
)) {
1206 if (match_state
&& unlikely(p
->state
!= match_state
))
1212 * Ok, time to look more closely! We need the rq
1213 * lock now, to be *sure*. If we're wrong, we'll
1214 * just go back and repeat.
1216 rq
= task_rq_lock(p
, &flags
);
1217 trace_sched_wait_task(p
);
1218 running
= task_running(rq
, p
);
1221 if (!match_state
|| p
->state
== match_state
)
1222 ncsw
= p
->nvcsw
| LONG_MIN
; /* sets MSB */
1223 task_rq_unlock(rq
, p
, &flags
);
1226 * If it changed from the expected state, bail out now.
1228 if (unlikely(!ncsw
))
1232 * Was it really running after all now that we
1233 * checked with the proper locks actually held?
1235 * Oops. Go back and try again..
1237 if (unlikely(running
)) {
1243 * It's not enough that it's not actively running,
1244 * it must be off the runqueue _entirely_, and not
1247 * So if it was still runnable (but just not actively
1248 * running right now), it's preempted, and we should
1249 * yield - it could be a while.
1251 if (unlikely(on_rq
)) {
1252 ktime_t to
= ktime_set(0, NSEC_PER_SEC
/HZ
);
1254 set_current_state(TASK_UNINTERRUPTIBLE
);
1255 schedule_hrtimeout(&to
, HRTIMER_MODE_REL
);
1260 * Ahh, all good. It wasn't running, and it wasn't
1261 * runnable, which means that it will never become
1262 * running in the future either. We're all done!
1271 * kick_process - kick a running thread to enter/exit the kernel
1272 * @p: the to-be-kicked thread
1274 * Cause a process which is running on another CPU to enter
1275 * kernel-mode, without any delay. (to get signals handled.)
1277 * NOTE: this function doesn't have to take the runqueue lock,
1278 * because all it wants to ensure is that the remote task enters
1279 * the kernel. If the IPI races and the task has been migrated
1280 * to another CPU then no harm is done and the purpose has been
1283 void kick_process(struct task_struct
*p
)
1289 if ((cpu
!= smp_processor_id()) && task_curr(p
))
1290 smp_send_reschedule(cpu
);
1293 EXPORT_SYMBOL_GPL(kick_process
);
1294 #endif /* CONFIG_SMP */
1298 * ->cpus_allowed is protected by both rq->lock and p->pi_lock
1300 static int select_fallback_rq(int cpu
, struct task_struct
*p
)
1302 int nid
= cpu_to_node(cpu
);
1303 const struct cpumask
*nodemask
= NULL
;
1304 enum { cpuset
, possible
, fail
} state
= cpuset
;
1308 * If the node that the cpu is on has been offlined, cpu_to_node()
1309 * will return -1. There is no cpu on the node, and we should
1310 * select the cpu on the other node.
1313 nodemask
= cpumask_of_node(nid
);
1315 /* Look for allowed, online CPU in same node. */
1316 for_each_cpu(dest_cpu
, nodemask
) {
1317 if (!cpu_online(dest_cpu
))
1319 if (!cpu_active(dest_cpu
))
1321 if (cpumask_test_cpu(dest_cpu
, tsk_cpus_allowed(p
)))
1327 /* Any allowed, online CPU? */
1328 for_each_cpu(dest_cpu
, tsk_cpus_allowed(p
)) {
1329 if (!cpu_online(dest_cpu
))
1331 if (!cpu_active(dest_cpu
))
1338 /* No more Mr. Nice Guy. */
1339 cpuset_cpus_allowed_fallback(p
);
1344 do_set_cpus_allowed(p
, cpu_possible_mask
);
1355 if (state
!= cpuset
) {
1357 * Don't tell them about moving exiting tasks or
1358 * kernel threads (both mm NULL), since they never
1361 if (p
->mm
&& printk_ratelimit()) {
1362 printk_deferred("process %d (%s) no longer affine to cpu%d\n",
1363 task_pid_nr(p
), p
->comm
, cpu
);
1371 * The caller (fork, wakeup) owns p->pi_lock, ->cpus_allowed is stable.
1374 int select_task_rq(struct task_struct
*p
, int sd_flags
, int wake_flags
)
1376 int cpu
= p
->sched_class
->select_task_rq(p
, sd_flags
, wake_flags
);
1379 * In order not to call set_task_cpu() on a blocking task we need
1380 * to rely on ttwu() to place the task on a valid ->cpus_allowed
1383 * Since this is common to all placement strategies, this lives here.
1385 * [ this allows ->select_task() to simply return task_cpu(p) and
1386 * not worry about this generic constraint ]
1388 if (unlikely(!cpumask_test_cpu(cpu
, tsk_cpus_allowed(p
)) ||
1390 cpu
= select_fallback_rq(task_cpu(p
), p
);
1395 static void update_avg(u64
*avg
, u64 sample
)
1397 s64 diff
= sample
- *avg
;
1403 ttwu_stat(struct task_struct
*p
, int cpu
, int wake_flags
)
1405 #ifdef CONFIG_SCHEDSTATS
1406 struct rq
*rq
= this_rq();
1409 int this_cpu
= smp_processor_id();
1411 if (cpu
== this_cpu
) {
1412 schedstat_inc(rq
, ttwu_local
);
1413 schedstat_inc(p
, se
.statistics
.nr_wakeups_local
);
1415 struct sched_domain
*sd
;
1417 schedstat_inc(p
, se
.statistics
.nr_wakeups_remote
);
1419 for_each_domain(this_cpu
, sd
) {
1420 if (cpumask_test_cpu(cpu
, sched_domain_span(sd
))) {
1421 schedstat_inc(sd
, ttwu_wake_remote
);
1428 if (wake_flags
& WF_MIGRATED
)
1429 schedstat_inc(p
, se
.statistics
.nr_wakeups_migrate
);
1431 #endif /* CONFIG_SMP */
1433 schedstat_inc(rq
, ttwu_count
);
1434 schedstat_inc(p
, se
.statistics
.nr_wakeups
);
1436 if (wake_flags
& WF_SYNC
)
1437 schedstat_inc(p
, se
.statistics
.nr_wakeups_sync
);
1439 #endif /* CONFIG_SCHEDSTATS */
1442 static void ttwu_activate(struct rq
*rq
, struct task_struct
*p
, int en_flags
)
1444 activate_task(rq
, p
, en_flags
);
1447 /* if a worker is waking up, notify workqueue */
1448 if (p
->flags
& PF_WQ_WORKER
)
1449 wq_worker_waking_up(p
, cpu_of(rq
));
1453 * Mark the task runnable and perform wakeup-preemption.
1456 ttwu_do_wakeup(struct rq
*rq
, struct task_struct
*p
, int wake_flags
)
1458 check_preempt_curr(rq
, p
, wake_flags
);
1459 trace_sched_wakeup(p
, true);
1461 p
->state
= TASK_RUNNING
;
1463 if (p
->sched_class
->task_woken
)
1464 p
->sched_class
->task_woken(rq
, p
);
1466 if (rq
->idle_stamp
) {
1467 u64 delta
= rq
->clock
- rq
->idle_stamp
;
1468 u64 max
= 2*sysctl_sched_migration_cost
;
1473 update_avg(&rq
->avg_idle
, delta
);
1480 ttwu_do_activate(struct rq
*rq
, struct task_struct
*p
, int wake_flags
)
1483 if (p
->sched_contributes_to_load
)
1484 rq
->nr_uninterruptible
--;
1487 ttwu_activate(rq
, p
, ENQUEUE_WAKEUP
| ENQUEUE_WAKING
);
1488 ttwu_do_wakeup(rq
, p
, wake_flags
);
1492 * Called in case the task @p isn't fully descheduled from its runqueue,
1493 * in this case we must do a remote wakeup. Its a 'light' wakeup though,
1494 * since all we need to do is flip p->state to TASK_RUNNING, since
1495 * the task is still ->on_rq.
1497 static int ttwu_remote(struct task_struct
*p
, int wake_flags
)
1502 rq
= __task_rq_lock(p
);
1504 ttwu_do_wakeup(rq
, p
, wake_flags
);
1507 __task_rq_unlock(rq
);
1513 static void sched_ttwu_pending(void)
1515 struct rq
*rq
= this_rq();
1516 struct llist_node
*llist
= llist_del_all(&rq
->wake_list
);
1517 struct task_struct
*p
;
1519 raw_spin_lock(&rq
->lock
);
1522 p
= llist_entry(llist
, struct task_struct
, wake_entry
);
1523 llist
= llist_next(llist
);
1524 ttwu_do_activate(rq
, p
, 0);
1527 raw_spin_unlock(&rq
->lock
);
1532 IPI_CALL_FUNC_SINGLE
,
1535 void scheduler_ipi(void)
1537 if (llist_empty(&this_rq()->wake_list
)
1538 && !tick_nohz_full_cpu(smp_processor_id())
1539 && !got_nohz_idle_kick()){
1540 mt_trace_ISR_start(IPI_RESCHEDULE
);
1541 mt_trace_ISR_end(IPI_RESCHEDULE
);
1546 * Not all reschedule IPI handlers call irq_enter/irq_exit, since
1547 * traditionally all their work was done from the interrupt return
1548 * path. Now that we actually do some work, we need to make sure
1551 * Some archs already do call them, luckily irq_enter/exit nest
1554 * Arguably we should visit all archs and update all handlers,
1555 * however a fair share of IPIs are still resched only so this would
1556 * somewhat pessimize the simple resched case.
1559 mt_trace_ISR_start(IPI_RESCHEDULE
);
1560 tick_nohz_full_check();
1561 sched_ttwu_pending();
1564 * Check if someone kicked us for doing the nohz idle load balance.
1566 if (unlikely(got_nohz_idle_kick())) {
1567 this_rq()->idle_balance
= 1;
1568 raise_softirq_irqoff(SCHED_SOFTIRQ
);
1570 mt_trace_ISR_end(IPI_RESCHEDULE
);
1574 static void ttwu_queue_remote(struct task_struct
*p
, int cpu
)
1576 if (llist_add(&p
->wake_entry
, &cpu_rq(cpu
)->wake_list
))
1577 smp_send_reschedule(cpu
);
1580 bool cpus_share_cache(int this_cpu
, int that_cpu
)
1582 return per_cpu(sd_llc_id
, this_cpu
) == per_cpu(sd_llc_id
, that_cpu
);
1584 #endif /* CONFIG_SMP */
1586 static void ttwu_queue(struct task_struct
*p
, int cpu
)
1588 struct rq
*rq
= cpu_rq(cpu
);
1590 #if defined(CONFIG_SMP)
1591 if (sched_feat(TTWU_QUEUE
) && !cpus_share_cache(smp_processor_id(), cpu
)) {
1592 sched_clock_cpu(cpu
); /* sync clocks x-cpu */
1593 ttwu_queue_remote(p
, cpu
);
1598 raw_spin_lock(&rq
->lock
);
1599 ttwu_do_activate(rq
, p
, 0);
1600 raw_spin_unlock(&rq
->lock
);
1604 * try_to_wake_up - wake up a thread
1605 * @p: the thread to be awakened
1606 * @state: the mask of task states that can be woken
1607 * @wake_flags: wake modifier flags (WF_*)
1609 * Put it on the run-queue if it's not already there. The "current"
1610 * thread is always on the run-queue (except when the actual
1611 * re-schedule is in progress), and as such you're allowed to do
1612 * the simpler "current->state = TASK_RUNNING" to mark yourself
1613 * runnable without the overhead of this.
1615 * Returns %true if @p was woken up, %false if it was already running
1616 * or @state didn't match @p's state.
1619 try_to_wake_up(struct task_struct
*p
, unsigned int state
, int wake_flags
)
1621 unsigned long flags
;
1622 int cpu
, success
= 0;
1625 * If we are going to wake up a thread waiting for CONDITION we
1626 * need to ensure that CONDITION=1 done by the caller can not be
1627 * reordered with p->state check below. This pairs with mb() in
1628 * set_current_state() the waiting thread does.
1630 smp_mb__before_spinlock();
1631 raw_spin_lock_irqsave(&p
->pi_lock
, flags
);
1632 if (!(p
->state
& state
))
1635 success
= 1; /* we're going to change ->state */
1638 if (p
->on_rq
&& ttwu_remote(p
, wake_flags
))
1643 * If the owning (remote) cpu is still in the middle of schedule() with
1644 * this task as prev, wait until its done referencing the task.
1649 * Pairs with the smp_wmb() in finish_lock_switch().
1653 p
->sched_contributes_to_load
= !!task_contributes_to_load(p
);
1654 p
->state
= TASK_WAKING
;
1656 if (p
->sched_class
->task_waking
)
1657 p
->sched_class
->task_waking(p
);
1659 cpu
= select_task_rq(p
, SD_BALANCE_WAKE
, wake_flags
);
1660 if (task_cpu(p
) != cpu
) {
1661 #ifdef CONFIG_MT_LOAD_BALANCE_PROFILER
1662 char strings
[128]="";
1664 wake_flags
|= WF_MIGRATED
;
1665 #ifdef CONFIG_MT_LOAD_BALANCE_PROFILER
1666 snprintf(strings
, 128, "%d:%d:%s:wakeup:%d:%d:%s", task_cpu(current
), current
->pid
, current
->comm
, cpu
, p
->pid
, p
->comm
);
1667 trace_sched_lbprof_log(strings
);
1669 set_task_cpu(p
, cpu
);
1671 #endif /* CONFIG_SMP */
1675 ttwu_stat(p
, cpu
, wake_flags
);
1677 raw_spin_unlock_irqrestore(&p
->pi_lock
, flags
);
1683 * try_to_wake_up_local - try to wake up a local task with rq lock held
1684 * @p: the thread to be awakened
1686 * Put @p on the run-queue if it's not already there. The caller must
1687 * ensure that this_rq() is locked, @p is bound to this_rq() and not
1690 static void try_to_wake_up_local(struct task_struct
*p
)
1692 struct rq
*rq
= task_rq(p
);
1694 if (WARN_ON_ONCE(rq
!= this_rq()) ||
1695 WARN_ON_ONCE(p
== current
))
1698 lockdep_assert_held(&rq
->lock
);
1700 if (!raw_spin_trylock(&p
->pi_lock
)) {
1701 raw_spin_unlock(&rq
->lock
);
1702 raw_spin_lock(&p
->pi_lock
);
1703 raw_spin_lock(&rq
->lock
);
1706 if (!(p
->state
& TASK_NORMAL
))
1710 ttwu_activate(rq
, p
, ENQUEUE_WAKEUP
);
1712 ttwu_do_wakeup(rq
, p
, 0);
1713 ttwu_stat(p
, smp_processor_id(), 0);
1715 raw_spin_unlock(&p
->pi_lock
);
1719 * wake_up_process - Wake up a specific process
1720 * @p: The process to be woken up.
1722 * Attempt to wake up the nominated process and move it to the set of runnable
1723 * processes. Returns 1 if the process was woken up, 0 if it was already
1726 * It may be assumed that this function implies a write memory barrier before
1727 * changing the task state if and only if any tasks are woken up.
1729 int wake_up_process(struct task_struct
*p
)
1731 WARN_ON(task_is_stopped_or_traced(p
));
1732 return try_to_wake_up(p
, TASK_NORMAL
, 0);
1734 EXPORT_SYMBOL(wake_up_process
);
1736 int wake_up_state(struct task_struct
*p
, unsigned int state
)
1738 return try_to_wake_up(p
, state
, 0);
1742 * Perform scheduler related setup for a newly forked process p.
1743 * p is forked by current.
1745 * __sched_fork() is basic setup used by init_idle() too:
1747 static void __sched_fork(struct task_struct
*p
)
1752 p
->se
.exec_start
= 0;
1753 p
->se
.sum_exec_runtime
= 0;
1754 p
->se
.prev_sum_exec_runtime
= 0;
1755 p
->se
.nr_migrations
= 0;
1757 INIT_LIST_HEAD(&p
->se
.group_node
);
1760 * Load-tracking only depends on SMP, FAIR_GROUP_SCHED dependency below may be
1761 * removed when useful for applications beyond shares distribution (e.g.
1764 #if defined(CONFIG_SMP) && defined(CONFIG_FAIR_GROUP_SCHED)
1765 p
->se
.avg
.runnable_avg_period
= 0;
1766 p
->se
.avg
.runnable_avg_sum
= 0;
1767 #ifdef CONFIG_SCHED_HMP
1768 /* keep LOAD_AVG_MAX in sync with fair.c if load avg series is changed */
1769 #define LOAD_AVG_MAX 47742
1771 p
->se
.avg
.hmp_last_up_migration
= 0;
1772 p
->se
.avg
.hmp_last_down_migration
= 0;
1773 p
->se
.avg
.load_avg_ratio
= 1023;
1774 p
->se
.avg
.load_avg_contrib
=
1775 (1023 * scale_load_down(p
->se
.load
.weight
));
1776 p
->se
.avg
.runnable_avg_period
= LOAD_AVG_MAX
;
1777 p
->se
.avg
.runnable_avg_sum
= LOAD_AVG_MAX
;
1778 p
->se
.avg
.usage_avg_sum
= LOAD_AVG_MAX
;
1782 #ifdef CONFIG_SCHEDSTATS
1783 memset(&p
->se
.statistics
, 0, sizeof(p
->se
.statistics
));
1786 INIT_LIST_HEAD(&p
->rt
.run_list
);
1788 #ifdef CONFIG_PREEMPT_NOTIFIERS
1789 INIT_HLIST_HEAD(&p
->preempt_notifiers
);
1792 #ifdef CONFIG_NUMA_BALANCING
1793 if (p
->mm
&& atomic_read(&p
->mm
->mm_users
) == 1) {
1794 p
->mm
->numa_next_scan
= jiffies
;
1795 p
->mm
->numa_next_reset
= jiffies
;
1796 p
->mm
->numa_scan_seq
= 0;
1799 p
->node_stamp
= 0ULL;
1800 p
->numa_scan_seq
= p
->mm
? p
->mm
->numa_scan_seq
: 0;
1801 p
->numa_migrate_seq
= p
->mm
? p
->mm
->numa_scan_seq
- 1 : 0;
1802 p
->numa_scan_period
= sysctl_numa_balancing_scan_delay
;
1803 p
->numa_work
.next
= &p
->numa_work
;
1804 #endif /* CONFIG_NUMA_BALANCING */
1807 #ifdef CONFIG_NUMA_BALANCING
1808 #ifdef CONFIG_SCHED_DEBUG
1809 void set_numabalancing_state(bool enabled
)
1812 sched_feat_set("NUMA");
1814 sched_feat_set("NO_NUMA");
1817 __read_mostly
bool numabalancing_enabled
;
1819 void set_numabalancing_state(bool enabled
)
1821 numabalancing_enabled
= enabled
;
1823 #endif /* CONFIG_SCHED_DEBUG */
1824 #endif /* CONFIG_NUMA_BALANCING */
1827 * fork()/clone()-time setup:
1829 void sched_fork(struct task_struct
*p
)
1831 unsigned long flags
;
1832 int cpu
= get_cpu();
1836 * We mark the process as running here. This guarantees that
1837 * nobody will actually run it, and a signal or other external
1838 * event cannot wake it up and insert it on the runqueue either.
1840 p
->state
= TASK_RUNNING
;
1843 * Make sure we do not leak PI boosting priority to the child.
1845 p
->prio
= current
->normal_prio
;
1848 * Revert to default priority/policy on fork if requested.
1850 if (unlikely(p
->sched_reset_on_fork
)) {
1851 if (task_has_rt_policy(p
)) {
1852 p
->policy
= SCHED_NORMAL
;
1853 p
->static_prio
= NICE_TO_PRIO(0);
1855 } else if (PRIO_TO_NICE(p
->static_prio
) < 0)
1856 p
->static_prio
= NICE_TO_PRIO(0);
1858 p
->prio
= p
->normal_prio
= __normal_prio(p
);
1862 * We don't need the reset flag anymore after the fork. It has
1863 * fulfilled its duty:
1865 p
->sched_reset_on_fork
= 0;
1868 if (!rt_prio(p
->prio
))
1869 p
->sched_class
= &fair_sched_class
;
1871 if (p
->sched_class
->task_fork
)
1872 p
->sched_class
->task_fork(p
);
1875 * The child is not yet in the pid-hash so no cgroup attach races,
1876 * and the cgroup is pinned to this child due to cgroup_fork()
1877 * is ran before sched_fork().
1879 * Silence PROVE_RCU.
1881 raw_spin_lock_irqsave(&p
->pi_lock
, flags
);
1882 set_task_cpu(p
, cpu
);
1883 raw_spin_unlock_irqrestore(&p
->pi_lock
, flags
);
1885 #if defined(CONFIG_SCHEDSTATS) || defined(CONFIG_TASK_DELAY_ACCT)
1886 if (likely(sched_info_on()))
1887 memset(&p
->sched_info
, 0, sizeof(p
->sched_info
));
1889 #if defined(CONFIG_SMP)
1892 #ifdef CONFIG_PREEMPT_COUNT
1893 /* Want to start with kernel preemption disabled. */
1894 task_thread_info(p
)->preempt_count
= 1;
1897 plist_node_init(&p
->pushable_tasks
, MAX_PRIO
);
1904 * wake_up_new_task - wake up a newly created task for the first time.
1906 * This function will do some initial scheduler statistics housekeeping
1907 * that must be done for every newly created context, then puts the task
1908 * on the runqueue and wakes it.
1910 void wake_up_new_task(struct task_struct
*p
)
1912 unsigned long flags
;
1915 raw_spin_lock_irqsave(&p
->pi_lock
, flags
);
1918 * Fork balancing, do it here and not earlier because:
1919 * - cpus_allowed can change in the fork path
1920 * - any previously selected cpu might disappear through hotplug
1922 set_task_cpu(p
, select_task_rq(p
, SD_BALANCE_FORK
, 0));
1925 /* Initialize new task's runnable average */
1926 init_task_runnable_average(p
);
1927 rq
= __task_rq_lock(p
);
1928 activate_task(rq
, p
, 0);
1930 trace_sched_wakeup_new(p
, true);
1931 check_preempt_curr(rq
, p
, WF_FORK
);
1933 if (p
->sched_class
->task_woken
)
1934 p
->sched_class
->task_woken(rq
, p
);
1936 task_rq_unlock(rq
, p
, &flags
);
1939 #ifdef CONFIG_PREEMPT_NOTIFIERS
1942 * preempt_notifier_register - tell me when current is being preempted & rescheduled
1943 * @notifier: notifier struct to register
1945 void preempt_notifier_register(struct preempt_notifier
*notifier
)
1947 hlist_add_head(¬ifier
->link
, ¤t
->preempt_notifiers
);
1949 EXPORT_SYMBOL_GPL(preempt_notifier_register
);
1952 * preempt_notifier_unregister - no longer interested in preemption notifications
1953 * @notifier: notifier struct to unregister
1955 * This is safe to call from within a preemption notifier.
1957 void preempt_notifier_unregister(struct preempt_notifier
*notifier
)
1959 hlist_del(¬ifier
->link
);
1961 EXPORT_SYMBOL_GPL(preempt_notifier_unregister
);
1963 static void fire_sched_in_preempt_notifiers(struct task_struct
*curr
)
1965 struct preempt_notifier
*notifier
;
1967 hlist_for_each_entry(notifier
, &curr
->preempt_notifiers
, link
)
1968 notifier
->ops
->sched_in(notifier
, raw_smp_processor_id());
1972 fire_sched_out_preempt_notifiers(struct task_struct
*curr
,
1973 struct task_struct
*next
)
1975 struct preempt_notifier
*notifier
;
1977 hlist_for_each_entry(notifier
, &curr
->preempt_notifiers
, link
)
1978 notifier
->ops
->sched_out(notifier
, next
);
1981 #else /* !CONFIG_PREEMPT_NOTIFIERS */
1983 static void fire_sched_in_preempt_notifiers(struct task_struct
*curr
)
1988 fire_sched_out_preempt_notifiers(struct task_struct
*curr
,
1989 struct task_struct
*next
)
1993 #endif /* CONFIG_PREEMPT_NOTIFIERS */
1996 * prepare_task_switch - prepare to switch tasks
1997 * @rq: the runqueue preparing to switch
1998 * @prev: the current task that is being switched out
1999 * @next: the task we are going to switch to.
2001 * This is called with the rq lock held and interrupts off. It must
2002 * be paired with a subsequent finish_task_switch after the context
2005 * prepare_task_switch sets up locking and calls architecture specific
2009 prepare_task_switch(struct rq
*rq
, struct task_struct
*prev
,
2010 struct task_struct
*next
)
2012 trace_sched_switch(prev
, next
);
2013 sched_info_switch(prev
, next
);
2014 perf_event_task_sched_out(prev
, next
);
2015 fire_sched_out_preempt_notifiers(prev
, next
);
2016 prepare_lock_switch(rq
, next
);
2017 prepare_arch_switch(next
);
2021 * finish_task_switch - clean up after a task-switch
2022 * @rq: runqueue associated with task-switch
2023 * @prev: the thread we just switched away from.
2025 * finish_task_switch must be called after the context switch, paired
2026 * with a prepare_task_switch call before the context switch.
2027 * finish_task_switch will reconcile locking set up by prepare_task_switch,
2028 * and do any other architecture-specific cleanup actions.
2030 * Note that we may have delayed dropping an mm in context_switch(). If
2031 * so, we finish that here outside of the runqueue lock. (Doing it
2032 * with the lock held can cause deadlocks; see schedule() for
2035 static void finish_task_switch(struct rq
*rq
, struct task_struct
*prev
)
2036 __releases(rq
->lock
)
2038 struct mm_struct
*mm
= rq
->prev_mm
;
2044 * A task struct has one reference for the use as "current".
2045 * If a task dies, then it sets TASK_DEAD in tsk->state and calls
2046 * schedule one last time. The schedule call will never return, and
2047 * the scheduled task must drop that reference.
2048 * The test for TASK_DEAD must occur while the runqueue locks are
2049 * still held, otherwise prev could be scheduled on another cpu, die
2050 * there before we look at prev->state, and then the reference would
2052 * Manfred Spraul <manfred@colorfullife.com>
2054 prev_state
= prev
->state
;
2055 vtime_task_switch(prev
);
2056 finish_arch_switch(prev
);
2057 perf_event_task_sched_in(prev
, current
);
2058 finish_lock_switch(rq
, prev
);
2059 finish_arch_post_lock_switch();
2061 fire_sched_in_preempt_notifiers(current
);
2064 if (unlikely(prev_state
== TASK_DEAD
)) {
2066 * Remove function-return probe instances associated with this
2067 * task and put them back on the free list.
2069 kprobe_flush_task(prev
);
2070 put_task_struct(prev
);
2073 tick_nohz_task_switch(current
);
2078 /* assumes rq->lock is held */
2079 static inline void pre_schedule(struct rq
*rq
, struct task_struct
*prev
)
2081 if (prev
->sched_class
->pre_schedule
)
2082 prev
->sched_class
->pre_schedule(rq
, prev
);
2085 /* rq->lock is NOT held, but preemption is disabled */
2086 static inline void post_schedule(struct rq
*rq
)
2088 if (rq
->post_schedule
) {
2089 unsigned long flags
;
2091 raw_spin_lock_irqsave(&rq
->lock
, flags
);
2092 if (rq
->curr
->sched_class
->post_schedule
)
2093 rq
->curr
->sched_class
->post_schedule(rq
);
2094 raw_spin_unlock_irqrestore(&rq
->lock
, flags
);
2096 rq
->post_schedule
= 0;
2102 static inline void pre_schedule(struct rq
*rq
, struct task_struct
*p
)
2106 static inline void post_schedule(struct rq
*rq
)
2113 * schedule_tail - first thing a freshly forked thread must call.
2114 * @prev: the thread we just switched away from.
2116 asmlinkage
void schedule_tail(struct task_struct
*prev
)
2117 __releases(rq
->lock
)
2119 struct rq
*rq
= this_rq();
2121 finish_task_switch(rq
, prev
);
2124 * FIXME: do we need to worry about rq being invalidated by the
2129 #ifdef __ARCH_WANT_UNLOCKED_CTXSW
2130 /* In this case, finish_task_switch does not reenable preemption */
2133 if (current
->set_child_tid
)
2134 put_user(task_pid_vnr(current
), current
->set_child_tid
);
2138 * context_switch - switch to the new MM and the new
2139 * thread's register state.
2142 context_switch(struct rq
*rq
, struct task_struct
*prev
,
2143 struct task_struct
*next
)
2145 struct mm_struct
*mm
, *oldmm
;
2147 prepare_task_switch(rq
, prev
, next
);
2149 #ifdef CONFIG_MT65XX_TRACER
2150 if(get_mt65xx_mon_mode() == MODE_SCHED_SWITCH
)
2151 trace_mt65xx_mon_sched_switch(prev
, next
);
2154 oldmm
= prev
->active_mm
;
2156 * For paravirt, this is coupled with an exit in switch_to to
2157 * combine the page table reload and the switch backend into
2160 arch_start_context_switch(prev
);
2163 next
->active_mm
= oldmm
;
2164 atomic_inc(&oldmm
->mm_count
);
2165 enter_lazy_tlb(oldmm
, next
);
2167 switch_mm(oldmm
, mm
, next
);
2170 prev
->active_mm
= NULL
;
2171 rq
->prev_mm
= oldmm
;
2174 * Since the runqueue lock will be released by the next
2175 * task (which is an invalid locking op but in the case
2176 * of the scheduler it's an obvious special-case), so we
2177 * do an early lockdep release here:
2179 #ifndef __ARCH_WANT_UNLOCKED_CTXSW
2180 spin_release(&rq
->lock
.dep_map
, 1, _THIS_IP_
);
2183 context_tracking_task_switch(prev
, next
);
2184 /* Here we just switch the register state and the stack. */
2185 switch_to(prev
, next
, prev
);
2189 * this_rq must be evaluated again because prev may have moved
2190 * CPUs since it called schedule(), thus the 'rq' on its stack
2191 * frame will be invalid.
2193 finish_task_switch(this_rq(), prev
);
2197 * nr_running and nr_context_switches:
2199 * externally visible scheduler statistics: current number of runnable
2200 * threads, total number of context switches performed since bootup.
2202 unsigned long nr_running(void)
2204 unsigned long i
, sum
= 0;
2206 for_each_online_cpu(i
)
2207 sum
+= cpu_rq(i
)->nr_running
;
2212 unsigned long long nr_context_switches(void)
2215 unsigned long long sum
= 0;
2217 for_each_possible_cpu(i
)
2218 sum
+= cpu_rq(i
)->nr_switches
;
2223 unsigned long nr_iowait(void)
2225 unsigned long i
, sum
= 0;
2227 for_each_possible_cpu(i
)
2228 sum
+= atomic_read(&cpu_rq(i
)->nr_iowait
);
2233 unsigned long nr_iowait_cpu(int cpu
)
2235 struct rq
*this = cpu_rq(cpu
);
2236 return atomic_read(&this->nr_iowait
);
2239 unsigned long this_cpu_load(void)
2241 struct rq
*this = this_rq();
2242 return this->cpu_load
[0];
2245 unsigned long get_cpu_load(int cpu
)
2247 struct rq
*this = cpu_rq(cpu
);
2248 return this->cpu_load
[0];
2250 EXPORT_SYMBOL(get_cpu_load
);
2253 * Global load-average calculations
2255 * We take a distributed and async approach to calculating the global load-avg
2256 * in order to minimize overhead.
2258 * The global load average is an exponentially decaying average of nr_running +
2259 * nr_uninterruptible.
2261 * Once every LOAD_FREQ:
2264 * for_each_possible_cpu(cpu)
2265 * nr_active += cpu_of(cpu)->nr_running + cpu_of(cpu)->nr_uninterruptible;
2267 * avenrun[n] = avenrun[0] * exp_n + nr_active * (1 - exp_n)
2269 * Due to a number of reasons the above turns in the mess below:
2271 * - for_each_possible_cpu() is prohibitively expensive on machines with
2272 * serious number of cpus, therefore we need to take a distributed approach
2273 * to calculating nr_active.
2275 * \Sum_i x_i(t) = \Sum_i x_i(t) - x_i(t_0) | x_i(t_0) := 0
2276 * = \Sum_i { \Sum_j=1 x_i(t_j) - x_i(t_j-1) }
2278 * So assuming nr_active := 0 when we start out -- true per definition, we
2279 * can simply take per-cpu deltas and fold those into a global accumulate
2280 * to obtain the same result. See calc_load_fold_active().
2282 * Furthermore, in order to avoid synchronizing all per-cpu delta folding
2283 * across the machine, we assume 10 ticks is sufficient time for every
2284 * cpu to have completed this task.
2286 * This places an upper-bound on the IRQ-off latency of the machine. Then
2287 * again, being late doesn't loose the delta, just wrecks the sample.
2289 * - cpu_rq()->nr_uninterruptible isn't accurately tracked per-cpu because
2290 * this would add another cross-cpu cacheline miss and atomic operation
2291 * to the wakeup path. Instead we increment on whatever cpu the task ran
2292 * when it went into uninterruptible state and decrement on whatever cpu
2293 * did the wakeup. This means that only the sum of nr_uninterruptible over
2294 * all cpus yields the correct result.
2296 * This covers the NO_HZ=n code, for extra head-aches, see the comment below.
2299 /* Variables and functions for calc_load */
2300 static atomic_long_t calc_load_tasks
;
2301 static unsigned long calc_load_update
;
2302 unsigned long avenrun
[3];
2303 EXPORT_SYMBOL(avenrun
); /* should be removed */
2306 * get_avenrun - get the load average array
2307 * @loads: pointer to dest load array
2308 * @offset: offset to add
2309 * @shift: shift count to shift the result left
2311 * These values are estimates at best, so no need for locking.
2313 void get_avenrun(unsigned long *loads
, unsigned long offset
, int shift
)
2315 loads
[0] = (avenrun
[0] + offset
) << shift
;
2316 loads
[1] = (avenrun
[1] + offset
) << shift
;
2317 loads
[2] = (avenrun
[2] + offset
) << shift
;
2320 static long calc_load_fold_active(struct rq
*this_rq
)
2322 long nr_active
, delta
= 0;
2324 nr_active
= this_rq
->nr_running
;
2325 nr_active
+= (long) this_rq
->nr_uninterruptible
;
2327 if (nr_active
!= this_rq
->calc_load_active
) {
2328 delta
= nr_active
- this_rq
->calc_load_active
;
2329 this_rq
->calc_load_active
= nr_active
;
2336 * a1 = a0 * e + a * (1 - e)
2338 static unsigned long
2339 calc_load(unsigned long load
, unsigned long exp
, unsigned long active
)
2342 load
+= active
* (FIXED_1
- exp
);
2343 load
+= 1UL << (FSHIFT
- 1);
2344 return load
>> FSHIFT
;
2347 #ifdef CONFIG_NO_HZ_COMMON
2349 * Handle NO_HZ for the global load-average.
2351 * Since the above described distributed algorithm to compute the global
2352 * load-average relies on per-cpu sampling from the tick, it is affected by
2355 * The basic idea is to fold the nr_active delta into a global idle-delta upon
2356 * entering NO_HZ state such that we can include this as an 'extra' cpu delta
2357 * when we read the global state.
2359 * Obviously reality has to ruin such a delightfully simple scheme:
2361 * - When we go NO_HZ idle during the window, we can negate our sample
2362 * contribution, causing under-accounting.
2364 * We avoid this by keeping two idle-delta counters and flipping them
2365 * when the window starts, thus separating old and new NO_HZ load.
2367 * The only trick is the slight shift in index flip for read vs write.
2371 * |-|-----------|-|-----------|-|-----------|-|
2372 * r:0 0 1 1 0 0 1 1 0
2373 * w:0 1 1 0 0 1 1 0 0
2375 * This ensures we'll fold the old idle contribution in this window while
2376 * accumlating the new one.
2378 * - When we wake up from NO_HZ idle during the window, we push up our
2379 * contribution, since we effectively move our sample point to a known
2382 * This is solved by pushing the window forward, and thus skipping the
2383 * sample, for this cpu (effectively using the idle-delta for this cpu which
2384 * was in effect at the time the window opened). This also solves the issue
2385 * of having to deal with a cpu having been in NOHZ idle for multiple
2386 * LOAD_FREQ intervals.
2388 * When making the ILB scale, we should try to pull this in as well.
2390 static atomic_long_t calc_load_idle
[2];
2391 static int calc_load_idx
;
2393 static inline int calc_load_write_idx(void)
2395 int idx
= calc_load_idx
;
2398 * See calc_global_nohz(), if we observe the new index, we also
2399 * need to observe the new update time.
2404 * If the folding window started, make sure we start writing in the
2407 if (!time_before(jiffies
, calc_load_update
))
2413 static inline int calc_load_read_idx(void)
2415 return calc_load_idx
& 1;
2418 void calc_load_enter_idle(void)
2420 struct rq
*this_rq
= this_rq();
2424 * We're going into NOHZ mode, if there's any pending delta, fold it
2425 * into the pending idle delta.
2427 delta
= calc_load_fold_active(this_rq
);
2429 int idx
= calc_load_write_idx();
2430 atomic_long_add(delta
, &calc_load_idle
[idx
]);
2434 void calc_load_exit_idle(void)
2436 struct rq
*this_rq
= this_rq();
2439 * If we're still before the sample window, we're done.
2441 if (time_before(jiffies
, this_rq
->calc_load_update
))
2445 * We woke inside or after the sample window, this means we're already
2446 * accounted through the nohz accounting, so skip the entire deal and
2447 * sync up for the next window.
2449 this_rq
->calc_load_update
= calc_load_update
;
2450 if (time_before(jiffies
, this_rq
->calc_load_update
+ 10))
2451 this_rq
->calc_load_update
+= LOAD_FREQ
;
2454 static long calc_load_fold_idle(void)
2456 int idx
= calc_load_read_idx();
2459 if (atomic_long_read(&calc_load_idle
[idx
]))
2460 delta
= atomic_long_xchg(&calc_load_idle
[idx
], 0);
2466 * fixed_power_int - compute: x^n, in O(log n) time
2468 * @x: base of the power
2469 * @frac_bits: fractional bits of @x
2470 * @n: power to raise @x to.
2472 * By exploiting the relation between the definition of the natural power
2473 * function: x^n := x*x*...*x (x multiplied by itself for n times), and
2474 * the binary encoding of numbers used by computers: n := \Sum n_i * 2^i,
2475 * (where: n_i \elem {0, 1}, the binary vector representing n),
2476 * we find: x^n := x^(\Sum n_i * 2^i) := \Prod x^(n_i * 2^i), which is
2477 * of course trivially computable in O(log_2 n), the length of our binary
2480 static unsigned long
2481 fixed_power_int(unsigned long x
, unsigned int frac_bits
, unsigned int n
)
2483 unsigned long result
= 1UL << frac_bits
;
2488 result
+= 1UL << (frac_bits
- 1);
2489 result
>>= frac_bits
;
2495 x
+= 1UL << (frac_bits
- 1);
2503 * a1 = a0 * e + a * (1 - e)
2505 * a2 = a1 * e + a * (1 - e)
2506 * = (a0 * e + a * (1 - e)) * e + a * (1 - e)
2507 * = a0 * e^2 + a * (1 - e) * (1 + e)
2509 * a3 = a2 * e + a * (1 - e)
2510 * = (a0 * e^2 + a * (1 - e) * (1 + e)) * e + a * (1 - e)
2511 * = a0 * e^3 + a * (1 - e) * (1 + e + e^2)
2515 * an = a0 * e^n + a * (1 - e) * (1 + e + ... + e^n-1) [1]
2516 * = a0 * e^n + a * (1 - e) * (1 - e^n)/(1 - e)
2517 * = a0 * e^n + a * (1 - e^n)
2519 * [1] application of the geometric series:
2522 * S_n := \Sum x^i = -------------
2525 static unsigned long
2526 calc_load_n(unsigned long load
, unsigned long exp
,
2527 unsigned long active
, unsigned int n
)
2530 return calc_load(load
, fixed_power_int(exp
, FSHIFT
, n
), active
);
2534 * NO_HZ can leave us missing all per-cpu ticks calling
2535 * calc_load_account_active(), but since an idle CPU folds its delta into
2536 * calc_load_tasks_idle per calc_load_account_idle(), all we need to do is fold
2537 * in the pending idle delta if our idle period crossed a load cycle boundary.
2539 * Once we've updated the global active value, we need to apply the exponential
2540 * weights adjusted to the number of cycles missed.
2542 static void calc_global_nohz(void)
2544 long delta
, active
, n
;
2546 if (!time_before(jiffies
, calc_load_update
+ 10)) {
2548 * Catch-up, fold however many we are behind still
2550 delta
= jiffies
- calc_load_update
- 10;
2551 n
= 1 + (delta
/ LOAD_FREQ
);
2553 active
= atomic_long_read(&calc_load_tasks
);
2554 active
= active
> 0 ? active
* FIXED_1
: 0;
2556 avenrun
[0] = calc_load_n(avenrun
[0], EXP_1
, active
, n
);
2557 avenrun
[1] = calc_load_n(avenrun
[1], EXP_5
, active
, n
);
2558 avenrun
[2] = calc_load_n(avenrun
[2], EXP_15
, active
, n
);
2560 calc_load_update
+= n
* LOAD_FREQ
;
2564 * Flip the idle index...
2566 * Make sure we first write the new time then flip the index, so that
2567 * calc_load_write_idx() will see the new time when it reads the new
2568 * index, this avoids a double flip messing things up.
2573 #else /* !CONFIG_NO_HZ_COMMON */
2575 static inline long calc_load_fold_idle(void) { return 0; }
2576 static inline void calc_global_nohz(void) { }
2578 #endif /* CONFIG_NO_HZ_COMMON */
2581 * calc_load - update the avenrun load estimates 10 ticks after the
2582 * CPUs have updated calc_load_tasks.
2584 void calc_global_load(unsigned long ticks
)
2588 if (time_before(jiffies
, calc_load_update
+ 10))
2592 * Fold the 'old' idle-delta to include all NO_HZ cpus.
2594 delta
= calc_load_fold_idle();
2596 atomic_long_add(delta
, &calc_load_tasks
);
2598 active
= atomic_long_read(&calc_load_tasks
);
2599 active
= active
> 0 ? active
* FIXED_1
: 0;
2601 avenrun
[0] = calc_load(avenrun
[0], EXP_1
, active
);
2602 avenrun
[1] = calc_load(avenrun
[1], EXP_5
, active
);
2603 avenrun
[2] = calc_load(avenrun
[2], EXP_15
, active
);
2605 calc_load_update
+= LOAD_FREQ
;
2608 * In case we idled for multiple LOAD_FREQ intervals, catch up in bulk.
2614 * Called from update_cpu_load() to periodically update this CPU's
2617 static void calc_load_account_active(struct rq
*this_rq
)
2621 if (time_before(jiffies
, this_rq
->calc_load_update
))
2624 delta
= calc_load_fold_active(this_rq
);
2626 atomic_long_add(delta
, &calc_load_tasks
);
2628 this_rq
->calc_load_update
+= LOAD_FREQ
;
2632 * End of global load-average stuff
2636 * The exact cpuload at various idx values, calculated at every tick would be
2637 * load = (2^idx - 1) / 2^idx * load + 1 / 2^idx * cur_load
2639 * If a cpu misses updates for n-1 ticks (as it was idle) and update gets called
2640 * on nth tick when cpu may be busy, then we have:
2641 * load = ((2^idx - 1) / 2^idx)^(n-1) * load
2642 * load = (2^idx - 1) / 2^idx) * load + 1 / 2^idx * cur_load
2644 * decay_load_missed() below does efficient calculation of
2645 * load = ((2^idx - 1) / 2^idx)^(n-1) * load
2646 * avoiding 0..n-1 loop doing load = ((2^idx - 1) / 2^idx) * load
2648 * The calculation is approximated on a 128 point scale.
2649 * degrade_zero_ticks is the number of ticks after which load at any
2650 * particular idx is approximated to be zero.
2651 * degrade_factor is a precomputed table, a row for each load idx.
2652 * Each column corresponds to degradation factor for a power of two ticks,
2653 * based on 128 point scale.
2655 * row 2, col 3 (=12) says that the degradation at load idx 2 after
2656 * 8 ticks is 12/128 (which is an approximation of exact factor 3^8/4^8).
2658 * With this power of 2 load factors, we can degrade the load n times
2659 * by looking at 1 bits in n and doing as many mult/shift instead of
2660 * n mult/shifts needed by the exact degradation.
2662 #define DEGRADE_SHIFT 7
2663 static const unsigned char
2664 degrade_zero_ticks
[CPU_LOAD_IDX_MAX
] = {0, 8, 32, 64, 128};
2665 static const unsigned char
2666 degrade_factor
[CPU_LOAD_IDX_MAX
][DEGRADE_SHIFT
+ 1] = {
2667 {0, 0, 0, 0, 0, 0, 0, 0},
2668 {64, 32, 8, 0, 0, 0, 0, 0},
2669 {96, 72, 40, 12, 1, 0, 0},
2670 {112, 98, 75, 43, 15, 1, 0},
2671 {120, 112, 98, 76, 45, 16, 2} };
2674 * Update cpu_load for any missed ticks, due to tickless idle. The backlog
2675 * would be when CPU is idle and so we just decay the old load without
2676 * adding any new load.
2678 static unsigned long
2679 decay_load_missed(unsigned long load
, unsigned long missed_updates
, int idx
)
2683 if (!missed_updates
)
2686 if (missed_updates
>= degrade_zero_ticks
[idx
])
2690 return load
>> missed_updates
;
2692 while (missed_updates
) {
2693 if (missed_updates
% 2)
2694 load
= (load
* degrade_factor
[idx
][j
]) >> DEGRADE_SHIFT
;
2696 missed_updates
>>= 1;
2703 * Update rq->cpu_load[] statistics. This function is usually called every
2704 * scheduler tick (TICK_NSEC). With tickless idle this will not be called
2705 * every tick. We fix it up based on jiffies.
2707 static void __update_cpu_load(struct rq
*this_rq
, unsigned long this_load
,
2708 unsigned long pending_updates
)
2712 this_rq
->nr_load_updates
++;
2714 /* Update our load: */
2715 this_rq
->cpu_load
[0] = this_load
; /* Fasttrack for idx 0 */
2716 for (i
= 1, scale
= 2; i
< CPU_LOAD_IDX_MAX
; i
++, scale
+= scale
) {
2717 unsigned long old_load
, new_load
;
2719 /* scale is effectively 1 << i now, and >> i divides by scale */
2721 old_load
= this_rq
->cpu_load
[i
];
2722 old_load
= decay_load_missed(old_load
, pending_updates
- 1, i
);
2723 new_load
= this_load
;
2725 * Round up the averaging division if load is increasing. This
2726 * prevents us from getting stuck on 9 if the load is 10, for
2729 if (new_load
> old_load
)
2730 new_load
+= scale
- 1;
2732 this_rq
->cpu_load
[i
] = (old_load
* (scale
- 1) + new_load
) >> i
;
2735 sched_avg_update(this_rq
);
2739 /* moved to kernel/sched/proc.c at Linux 3.11-rc4 */
2740 static inline unsigned long get_rq_runnable_load(struct rq
*rq
)
2742 return rq
->cfs
.runnable_load_avg
;
2745 static inline unsigned long get_rq_runnable_load(struct rq
*rq
)
2747 return rq
->load
.weight
;
2751 #ifdef CONFIG_NO_HZ_COMMON
2753 * There is no sane way to deal with nohz on smp when using jiffies because the
2754 * cpu doing the jiffies update might drift wrt the cpu doing the jiffy reading
2755 * causing off-by-one errors in observed deltas; {0,2} instead of {1,1}.
2757 * Therefore we cannot use the delta approach from the regular tick since that
2758 * would seriously skew the load calculation. However we'll make do for those
2759 * updates happening while idle (nohz_idle_balance) or coming out of idle
2760 * (tick_nohz_idle_exit).
2762 * This means we might still be one tick off for nohz periods.
2766 * Called from nohz_idle_balance() to update the load ratings before doing the
2769 /* moved to kernel/sched/proc.c at Linux 3.11-rc4 */
2770 void update_idle_cpu_load(struct rq
*this_rq
)
2772 unsigned long curr_jiffies
= ACCESS_ONCE(jiffies
);
2773 unsigned long load
= this_rq
->load
.weight
;
2774 unsigned long pending_updates
;
2777 * bail if there's load or we're actually up-to-date.
2779 if (load
|| curr_jiffies
== this_rq
->last_load_update_tick
)
2782 pending_updates
= curr_jiffies
- this_rq
->last_load_update_tick
;
2783 this_rq
->last_load_update_tick
= curr_jiffies
;
2785 __update_cpu_load(this_rq
, load
, pending_updates
);
2789 * Called from tick_nohz_idle_exit() -- try and fix up the ticks we missed.
2791 /* moved to kernel/sched/proc.c at Linux 3.11-rc4 */
2792 void update_cpu_load_nohz(void)
2794 struct rq
*this_rq
= this_rq();
2795 unsigned long curr_jiffies
= ACCESS_ONCE(jiffies
);
2796 unsigned long pending_updates
;
2798 if (curr_jiffies
== this_rq
->last_load_update_tick
)
2801 raw_spin_lock(&this_rq
->lock
);
2802 pending_updates
= curr_jiffies
- this_rq
->last_load_update_tick
;
2803 if (pending_updates
) {
2804 this_rq
->last_load_update_tick
= curr_jiffies
;
2806 * We were idle, this means load 0, the current load might be
2807 * !0 due to remote wakeups and the sort.
2809 __update_cpu_load(this_rq
, 0, pending_updates
);
2811 raw_spin_unlock(&this_rq
->lock
);
2813 #endif /* CONFIG_NO_HZ_COMMON */
2816 * Called from scheduler_tick()
2818 /* moved to kernel/sched/proc.c at Linux 3.11-rc4 */
2819 static void update_cpu_load_active(struct rq
*this_rq
)
2821 unsigned long load
= get_rq_runnable_load(this_rq
);
2823 * See the mess around update_idle_cpu_load() / update_cpu_load_nohz().
2825 this_rq
->last_load_update_tick
= jiffies
;
2826 __update_cpu_load(this_rq
, load
, 1);
2828 calc_load_account_active(this_rq
);
2834 * sched_exec - execve() is a valuable balancing opportunity, because at
2835 * this point the task has the smallest effective memory and cache footprint.
2837 void sched_exec(void)
2839 struct task_struct
*p
= current
;
2840 unsigned long flags
;
2843 raw_spin_lock_irqsave(&p
->pi_lock
, flags
);
2844 dest_cpu
= p
->sched_class
->select_task_rq(p
, SD_BALANCE_EXEC
, 0);
2845 if (dest_cpu
== smp_processor_id())
2848 if (likely(cpu_active(dest_cpu
))) {
2849 struct migration_arg arg
= { p
, dest_cpu
};
2851 raw_spin_unlock_irqrestore(&p
->pi_lock
, flags
);
2852 stop_one_cpu(task_cpu(p
), migration_cpu_stop
, &arg
);
2856 raw_spin_unlock_irqrestore(&p
->pi_lock
, flags
);
2861 DEFINE_PER_CPU(struct kernel_stat
, kstat
);
2862 DEFINE_PER_CPU(struct kernel_cpustat
, kernel_cpustat
);
2864 EXPORT_PER_CPU_SYMBOL(kstat
);
2865 EXPORT_PER_CPU_SYMBOL(kernel_cpustat
);
2868 * Return any ns on the sched_clock that have not yet been accounted in
2869 * @p in case that task is currently running.
2871 * Called with task_rq_lock() held on @rq.
2873 static u64
do_task_delta_exec(struct task_struct
*p
, struct rq
*rq
)
2877 if (task_current(rq
, p
)) {
2878 update_rq_clock(rq
);
2879 ns
= rq
->clock_task
- p
->se
.exec_start
;
2887 unsigned long long task_delta_exec(struct task_struct
*p
)
2889 unsigned long flags
;
2893 rq
= task_rq_lock(p
, &flags
);
2894 ns
= do_task_delta_exec(p
, rq
);
2895 task_rq_unlock(rq
, p
, &flags
);
2901 * Return accounted runtime for the task.
2902 * In case the task is currently running, return the runtime plus current's
2903 * pending runtime that have not been accounted yet.
2905 unsigned long long task_sched_runtime(struct task_struct
*p
)
2907 unsigned long flags
;
2911 rq
= task_rq_lock(p
, &flags
);
2912 ns
= p
->se
.sum_exec_runtime
+ do_task_delta_exec(p
, rq
);
2913 task_rq_unlock(rq
, p
, &flags
);
2919 * This function gets called by the timer code, with HZ frequency.
2920 * We call it with interrupts disabled.
2922 void scheduler_tick(void)
2924 int cpu
= smp_processor_id();
2925 struct rq
*rq
= cpu_rq(cpu
);
2926 struct task_struct
*curr
= rq
->curr
;
2930 raw_spin_lock(&rq
->lock
);
2931 update_rq_clock(rq
);
2932 curr
->sched_class
->task_tick(rq
, curr
, 0);
2933 update_cpu_load_active(rq
);
2934 #ifdef CONFIG_MT_RT_SCHED
2935 mt_check_rt_policy(rq
);
2937 raw_spin_unlock(&rq
->lock
);
2939 perf_event_task_tick();
2940 #ifdef CONFIG_MT_SCHED_MONITOR
2941 if(smp_processor_id() == 0) //only record by CPU#0
2942 mt_save_irq_counts();
2945 rq
->idle_balance
= idle_cpu(cpu
);
2946 trigger_load_balance(rq
, cpu
);
2948 rq_last_tick_reset(rq
);
2951 #ifdef CONFIG_NO_HZ_FULL
2953 * scheduler_tick_max_deferment
2955 * Keep at least one tick per second when a single
2956 * active task is running because the scheduler doesn't
2957 * yet completely support full dynticks environment.
2959 * This makes sure that uptime, CFS vruntime, load
2960 * balancing, etc... continue to move forward, even
2961 * with a very low granularity.
2963 u64
scheduler_tick_max_deferment(void)
2965 struct rq
*rq
= this_rq();
2966 unsigned long next
, now
= ACCESS_ONCE(jiffies
);
2968 next
= rq
->last_sched_tick
+ HZ
;
2970 if (time_before_eq(next
, now
))
2973 return jiffies_to_usecs(next
- now
) * NSEC_PER_USEC
;
2977 notrace
unsigned long get_parent_ip(unsigned long addr
)
2979 if (in_lock_functions(addr
)) {
2980 addr
= CALLER_ADDR2
;
2981 if (in_lock_functions(addr
))
2982 addr
= CALLER_ADDR3
;
2987 #if defined(CONFIG_PREEMPT) && (defined(CONFIG_DEBUG_PREEMPT) || \
2988 defined(CONFIG_PREEMPT_TRACER))
2990 void __kprobes
add_preempt_count(int val
)
2992 #ifdef CONFIG_DEBUG_PREEMPT
2996 if (DEBUG_LOCKS_WARN_ON((preempt_count() < 0)))
2999 preempt_count() += val
;
3000 #ifdef CONFIG_DEBUG_PREEMPT
3002 * Spinlock count overflowing soon?
3004 DEBUG_LOCKS_WARN_ON((preempt_count() & PREEMPT_MASK
) >=
3007 //if (preempt_count() == val)
3008 // trace_preempt_off(CALLER_ADDR0, get_parent_ip(CALLER_ADDR1));
3009 if (preempt_count() == (val
& ~PREEMPT_ACTIVE
)){
3010 #ifdef CONFIG_PREEMPT_TRACER
3011 trace_preempt_off(CALLER_ADDR0
, get_parent_ip(CALLER_ADDR1
));
3013 #ifdef CONFIG_PREEMPT_MONITOR
3014 if(unlikely(__raw_get_cpu_var(mtsched_mon_enabled
) & 0x1)){
3015 //current->t_add_prmpt = sched_clock();
3016 MT_trace_preempt_off();
3021 EXPORT_SYMBOL(add_preempt_count
);
3023 void __kprobes
sub_preempt_count(int val
)
3025 #ifdef CONFIG_DEBUG_PREEMPT
3029 if (DEBUG_LOCKS_WARN_ON(val
> preempt_count()))
3032 * Is the spinlock portion underflowing?
3034 if (DEBUG_LOCKS_WARN_ON((val
< PREEMPT_MASK
) &&
3035 !(preempt_count() & PREEMPT_MASK
)))
3039 //if (preempt_count() == val)
3040 // trace_preempt_on(CALLER_ADDR0, get_parent_ip(CALLER_ADDR1));
3041 if (preempt_count() == (val
& ~PREEMPT_ACTIVE
)){
3042 #ifdef CONFIG_PREEMPT_TRACER
3043 trace_preempt_on(CALLER_ADDR0
, get_parent_ip(CALLER_ADDR1
));
3045 #ifdef CONFIG_PREEMPT_MONITOR
3046 if(unlikely(__raw_get_cpu_var(mtsched_mon_enabled
) & 0x1)){
3047 MT_trace_preempt_on();
3051 preempt_count() -= val
;
3053 EXPORT_SYMBOL(sub_preempt_count
);
3058 * Print scheduling while atomic bug:
3060 static noinline
void __schedule_bug(struct task_struct
*prev
)
3062 if (oops_in_progress
)
3065 printk(KERN_ERR
"BUG: scheduling while atomic: %s/%d/0x%08x\n",
3066 prev
->comm
, prev
->pid
, preempt_count());
3068 debug_show_held_locks(prev
);
3070 if (irqs_disabled())
3071 print_irqtrace_events(prev
);
3073 add_taint(TAINT_WARN
, LOCKDEP_STILL_OK
);
3078 * Various schedule()-time debugging checks and statistics:
3080 static inline void schedule_debug(struct task_struct
*prev
)
3083 * Test if we are atomic. Since do_exit() needs to call into
3084 * schedule() atomically, we ignore that path for now.
3085 * Otherwise, whine if we are scheduling when we should not be.
3087 if (unlikely(in_atomic_preempt_off() && !prev
->exit_state
))
3088 __schedule_bug(prev
);
3091 profile_hit(SCHED_PROFILING
, __builtin_return_address(0));
3093 schedstat_inc(this_rq(), sched_count
);
3096 static void put_prev_task(struct rq
*rq
, struct task_struct
*prev
)
3098 if (prev
->on_rq
|| rq
->skip_clock_update
< 0)
3099 update_rq_clock(rq
);
3100 prev
->sched_class
->put_prev_task(rq
, prev
);
3104 * Pick up the highest-prio task:
3106 static inline struct task_struct
*
3107 pick_next_task(struct rq
*rq
)
3109 const struct sched_class
*class;
3110 struct task_struct
*p
;
3113 * Optimization: we know that if all tasks are in
3114 * the fair class we can call that function directly:
3116 if (likely(rq
->nr_running
== rq
->cfs
.h_nr_running
)) {
3117 p
= fair_sched_class
.pick_next_task(rq
);
3122 for_each_class(class) {
3123 p
= class->pick_next_task(rq
);
3128 BUG(); /* the idle class will always have a runnable task */
3132 * __schedule() is the main scheduler function.
3134 * The main means of driving the scheduler and thus entering this function are:
3136 * 1. Explicit blocking: mutex, semaphore, waitqueue, etc.
3138 * 2. TIF_NEED_RESCHED flag is checked on interrupt and userspace return
3139 * paths. For example, see arch/x86/entry_64.S.
3141 * To drive preemption between tasks, the scheduler sets the flag in timer
3142 * interrupt handler scheduler_tick().
3144 * 3. Wakeups don't really cause entry into schedule(). They add a
3145 * task to the run-queue and that's it.
3147 * Now, if the new task added to the run-queue preempts the current
3148 * task, then the wakeup sets TIF_NEED_RESCHED and schedule() gets
3149 * called on the nearest possible occasion:
3151 * - If the kernel is preemptible (CONFIG_PREEMPT=y):
3153 * - in syscall or exception context, at the next outmost
3154 * preempt_enable(). (this might be as soon as the wake_up()'s
3157 * - in IRQ context, return from interrupt-handler to
3158 * preemptible context
3160 * - If the kernel is not preemptible (CONFIG_PREEMPT is not set)
3163 * - cond_resched() call
3164 * - explicit schedule() call
3165 * - return from syscall or exception to user-space
3166 * - return from interrupt-handler to user-space
3168 static void __sched
__schedule(void)
3170 struct task_struct
*prev
, *next
;
3171 unsigned long *switch_count
;
3177 cpu
= smp_processor_id();
3179 rcu_note_context_switch(cpu
);
3182 schedule_debug(prev
);
3184 if (sched_feat(HRTICK
))
3186 #ifdef CONFIG_MT_SCHED_MONITOR
3187 __raw_get_cpu_var(MT_trace_in_sched
) = 1;
3191 * Make sure that signal_pending_state()->signal_pending() below
3192 * can't be reordered with __set_current_state(TASK_INTERRUPTIBLE)
3193 * done by the caller to avoid the race with signal_wake_up().
3195 smp_mb__before_spinlock();
3196 raw_spin_lock_irq(&rq
->lock
);
3198 switch_count
= &prev
->nivcsw
;
3199 if (prev
->state
&& !(preempt_count() & PREEMPT_ACTIVE
)) {
3200 if (unlikely(signal_pending_state(prev
->state
, prev
))) {
3201 prev
->state
= TASK_RUNNING
;
3203 deactivate_task(rq
, prev
, DEQUEUE_SLEEP
);
3207 * If a worker went to sleep, notify and ask workqueue
3208 * whether it wants to wake up a task to maintain
3211 if (prev
->flags
& PF_WQ_WORKER
) {
3212 struct task_struct
*to_wakeup
;
3214 to_wakeup
= wq_worker_sleeping(prev
, cpu
);
3216 try_to_wake_up_local(to_wakeup
);
3219 switch_count
= &prev
->nvcsw
;
3222 pre_schedule(rq
, prev
);
3224 if (unlikely(!rq
->nr_running
))
3225 idle_balance(cpu
, rq
);
3227 put_prev_task(rq
, prev
);
3228 next
= pick_next_task(rq
);
3229 clear_tsk_need_resched(prev
);
3230 rq
->skip_clock_update
= 0;
3232 if (likely(prev
!= next
)) {
3237 context_switch(rq
, prev
, next
); /* unlocks the rq */
3239 * The context switch have flipped the stack from under us
3240 * and restored the local variables which were saved when
3241 * this task called schedule() in the past. prev == current
3242 * is still correct, but it can be moved to another cpu/rq.
3244 cpu
= smp_processor_id();
3247 raw_spin_unlock_irq(&rq
->lock
);
3249 #ifdef CONFIG_MT_RT_SCHED
3250 mt_post_schedule(rq
);
3252 #ifdef CONFIG_MT_SCHED_MONITOR
3253 __raw_get_cpu_var(MT_trace_in_sched
) = 0;
3257 sched_preempt_enable_no_resched();
3262 static inline void sched_submit_work(struct task_struct
*tsk
)
3264 if (!tsk
->state
|| tsk_is_pi_blocked(tsk
))
3267 * If we are going to sleep and we have plugged IO queued,
3268 * make sure to submit it to avoid deadlocks.
3270 if (blk_needs_flush_plug(tsk
))
3271 blk_schedule_flush_plug(tsk
);
3274 asmlinkage
void __sched
schedule(void)
3276 struct task_struct
*tsk
= current
;
3278 sched_submit_work(tsk
);
3281 EXPORT_SYMBOL(schedule
);
3283 #ifdef CONFIG_CONTEXT_TRACKING
3284 asmlinkage
void __sched
schedule_user(void)
3287 * If we come here after a random call to set_need_resched(),
3288 * or we have been woken up remotely but the IPI has not yet arrived,
3289 * we haven't yet exited the RCU idle mode. Do it here manually until
3290 * we find a better solution.
3299 * schedule_preempt_disabled - called with preemption disabled
3301 * Returns with preemption disabled. Note: preempt_count must be 1
3303 void __sched
schedule_preempt_disabled(void)
3305 sched_preempt_enable_no_resched();
3310 #ifdef CONFIG_PREEMPT
3312 * this is the entry point to schedule() from in-kernel preemption
3313 * off of preempt_enable. Kernel preemptions off return from interrupt
3314 * occur there and call schedule directly.
3316 asmlinkage
void __sched notrace
preempt_schedule(void)
3318 struct thread_info
*ti
= current_thread_info();
3321 * If there is a non-zero preempt_count or interrupts are disabled,
3322 * we do not want to preempt the current task. Just return..
3324 if (likely(ti
->preempt_count
|| irqs_disabled()))
3328 add_preempt_count_notrace(PREEMPT_ACTIVE
);
3330 sub_preempt_count_notrace(PREEMPT_ACTIVE
);
3333 * Check again in case we missed a preemption opportunity
3334 * between schedule and now.
3337 } while (need_resched());
3339 EXPORT_SYMBOL(preempt_schedule
);
3342 * this is the entry point to schedule() from kernel preemption
3343 * off of irq context.
3344 * Note, that this is called and return with irqs disabled. This will
3345 * protect us against recursive calling from irq.
3347 asmlinkage
void __sched
preempt_schedule_irq(void)
3349 struct thread_info
*ti
= current_thread_info();
3350 enum ctx_state prev_state
;
3352 /* Catch callers which need to be fixed */
3353 BUG_ON(ti
->preempt_count
|| !irqs_disabled());
3355 prev_state
= exception_enter();
3358 add_preempt_count(PREEMPT_ACTIVE
);
3361 local_irq_disable();
3362 sub_preempt_count(PREEMPT_ACTIVE
);
3365 * Check again in case we missed a preemption opportunity
3366 * between schedule and now.
3369 } while (need_resched());
3371 exception_exit(prev_state
);
3374 #endif /* CONFIG_PREEMPT */
3376 int default_wake_function(wait_queue_t
*curr
, unsigned mode
, int wake_flags
,
3379 return try_to_wake_up(curr
->private, mode
, wake_flags
);
3381 EXPORT_SYMBOL(default_wake_function
);
3384 * The core wakeup function. Non-exclusive wakeups (nr_exclusive == 0) just
3385 * wake everything up. If it's an exclusive wakeup (nr_exclusive == small +ve
3386 * number) then we wake all the non-exclusive tasks and one exclusive task.
3388 * There are circumstances in which we can try to wake a task which has already
3389 * started to run but is not in state TASK_RUNNING. try_to_wake_up() returns
3390 * zero in this (rare) case, and we handle it by continuing to scan the queue.
3392 static void __wake_up_common(wait_queue_head_t
*q
, unsigned int mode
,
3393 int nr_exclusive
, int wake_flags
, void *key
)
3395 wait_queue_t
*curr
, *next
;
3397 list_for_each_entry_safe(curr
, next
, &q
->task_list
, task_list
) {
3398 unsigned flags
= curr
->flags
;
3400 if (curr
->func(curr
, mode
, wake_flags
, key
) &&
3401 (flags
& WQ_FLAG_EXCLUSIVE
) && !--nr_exclusive
)
3407 * __wake_up - wake up threads blocked on a waitqueue.
3409 * @mode: which threads
3410 * @nr_exclusive: how many wake-one or wake-many threads to wake up
3411 * @key: is directly passed to the wakeup function
3413 * It may be assumed that this function implies a write memory barrier before
3414 * changing the task state if and only if any tasks are woken up.
3416 void __wake_up(wait_queue_head_t
*q
, unsigned int mode
,
3417 int nr_exclusive
, void *key
)
3419 unsigned long flags
;
3421 spin_lock_irqsave(&q
->lock
, flags
);
3422 __wake_up_common(q
, mode
, nr_exclusive
, 0, key
);
3423 spin_unlock_irqrestore(&q
->lock
, flags
);
3425 EXPORT_SYMBOL(__wake_up
);
3428 * Same as __wake_up but called with the spinlock in wait_queue_head_t held.
3430 void __wake_up_locked(wait_queue_head_t
*q
, unsigned int mode
, int nr
)
3432 __wake_up_common(q
, mode
, nr
, 0, NULL
);
3434 EXPORT_SYMBOL_GPL(__wake_up_locked
);
3436 void __wake_up_locked_key(wait_queue_head_t
*q
, unsigned int mode
, void *key
)
3438 __wake_up_common(q
, mode
, 1, 0, key
);
3440 EXPORT_SYMBOL_GPL(__wake_up_locked_key
);
3443 * __wake_up_sync_key - wake up threads blocked on a waitqueue.
3445 * @mode: which threads
3446 * @nr_exclusive: how many wake-one or wake-many threads to wake up
3447 * @key: opaque value to be passed to wakeup targets
3449 * The sync wakeup differs that the waker knows that it will schedule
3450 * away soon, so while the target thread will be woken up, it will not
3451 * be migrated to another CPU - ie. the two threads are 'synchronized'
3452 * with each other. This can prevent needless bouncing between CPUs.
3454 * On UP it can prevent extra preemption.
3456 * It may be assumed that this function implies a write memory barrier before
3457 * changing the task state if and only if any tasks are woken up.
3459 void __wake_up_sync_key(wait_queue_head_t
*q
, unsigned int mode
,
3460 int nr_exclusive
, void *key
)
3462 unsigned long flags
;
3463 int wake_flags
= WF_SYNC
;
3468 if (unlikely(!nr_exclusive
))
3471 spin_lock_irqsave(&q
->lock
, flags
);
3472 __wake_up_common(q
, mode
, nr_exclusive
, wake_flags
, key
);
3473 spin_unlock_irqrestore(&q
->lock
, flags
);
3475 EXPORT_SYMBOL_GPL(__wake_up_sync_key
);
3478 * __wake_up_sync - see __wake_up_sync_key()
3480 void __wake_up_sync(wait_queue_head_t
*q
, unsigned int mode
, int nr_exclusive
)
3482 __wake_up_sync_key(q
, mode
, nr_exclusive
, NULL
);
3484 EXPORT_SYMBOL_GPL(__wake_up_sync
); /* For internal use only */
3487 * complete: - signals a single thread waiting on this completion
3488 * @x: holds the state of this particular completion
3490 * This will wake up a single thread waiting on this completion. Threads will be
3491 * awakened in the same order in which they were queued.
3493 * See also complete_all(), wait_for_completion() and related routines.
3495 * It may be assumed that this function implies a write memory barrier before
3496 * changing the task state if and only if any tasks are woken up.
3498 void complete(struct completion
*x
)
3500 unsigned long flags
;
3502 spin_lock_irqsave(&x
->wait
.lock
, flags
);
3504 __wake_up_common(&x
->wait
, TASK_NORMAL
, 1, 0, NULL
);
3505 spin_unlock_irqrestore(&x
->wait
.lock
, flags
);
3507 EXPORT_SYMBOL(complete
);
3510 * complete_all: - signals all threads waiting on this completion
3511 * @x: holds the state of this particular completion
3513 * This will wake up all threads waiting on this particular completion event.
3515 * It may be assumed that this function implies a write memory barrier before
3516 * changing the task state if and only if any tasks are woken up.
3518 void complete_all(struct completion
*x
)
3520 unsigned long flags
;
3522 spin_lock_irqsave(&x
->wait
.lock
, flags
);
3523 x
->done
+= UINT_MAX
/2;
3524 __wake_up_common(&x
->wait
, TASK_NORMAL
, 0, 0, NULL
);
3525 spin_unlock_irqrestore(&x
->wait
.lock
, flags
);
3527 EXPORT_SYMBOL(complete_all
);
3529 static inline long __sched
3530 do_wait_for_common(struct completion
*x
,
3531 long (*action
)(long), long timeout
, int state
)
3534 DECLARE_WAITQUEUE(wait
, current
);
3536 __add_wait_queue_tail_exclusive(&x
->wait
, &wait
);
3538 if (signal_pending_state(state
, current
)) {
3539 timeout
= -ERESTARTSYS
;
3542 __set_current_state(state
);
3543 spin_unlock_irq(&x
->wait
.lock
);
3544 timeout
= action(timeout
);
3545 spin_lock_irq(&x
->wait
.lock
);
3546 } while (!x
->done
&& timeout
);
3547 __remove_wait_queue(&x
->wait
, &wait
);
3552 return timeout
?: 1;
3555 static inline long __sched
3556 __wait_for_common(struct completion
*x
,
3557 long (*action
)(long), long timeout
, int state
)
3561 spin_lock_irq(&x
->wait
.lock
);
3562 timeout
= do_wait_for_common(x
, action
, timeout
, state
);
3563 spin_unlock_irq(&x
->wait
.lock
);
3568 wait_for_common(struct completion
*x
, long timeout
, int state
)
3570 return __wait_for_common(x
, schedule_timeout
, timeout
, state
);
3574 wait_for_common_io(struct completion
*x
, long timeout
, int state
)
3576 return __wait_for_common(x
, io_schedule_timeout
, timeout
, state
);
3580 * wait_for_completion: - waits for completion of a task
3581 * @x: holds the state of this particular completion
3583 * This waits to be signaled for completion of a specific task. It is NOT
3584 * interruptible and there is no timeout.
3586 * See also similar routines (i.e. wait_for_completion_timeout()) with timeout
3587 * and interrupt capability. Also see complete().
3589 void __sched
wait_for_completion(struct completion
*x
)
3591 wait_for_common(x
, MAX_SCHEDULE_TIMEOUT
, TASK_UNINTERRUPTIBLE
);
3593 EXPORT_SYMBOL(wait_for_completion
);
3596 * wait_for_completion_timeout: - waits for completion of a task (w/timeout)
3597 * @x: holds the state of this particular completion
3598 * @timeout: timeout value in jiffies
3600 * This waits for either a completion of a specific task to be signaled or for a
3601 * specified timeout to expire. The timeout is in jiffies. It is not
3604 * The return value is 0 if timed out, and positive (at least 1, or number of
3605 * jiffies left till timeout) if completed.
3607 unsigned long __sched
3608 wait_for_completion_timeout(struct completion
*x
, unsigned long timeout
)
3610 return wait_for_common(x
, timeout
, TASK_UNINTERRUPTIBLE
);
3612 EXPORT_SYMBOL(wait_for_completion_timeout
);
3615 * wait_for_completion_io: - waits for completion of a task
3616 * @x: holds the state of this particular completion
3618 * This waits to be signaled for completion of a specific task. It is NOT
3619 * interruptible and there is no timeout. The caller is accounted as waiting
3622 void __sched
wait_for_completion_io(struct completion
*x
)
3624 wait_for_common_io(x
, MAX_SCHEDULE_TIMEOUT
, TASK_UNINTERRUPTIBLE
);
3626 EXPORT_SYMBOL(wait_for_completion_io
);
3629 * wait_for_completion_io_timeout: - waits for completion of a task (w/timeout)
3630 * @x: holds the state of this particular completion
3631 * @timeout: timeout value in jiffies
3633 * This waits for either a completion of a specific task to be signaled or for a
3634 * specified timeout to expire. The timeout is in jiffies. It is not
3635 * interruptible. The caller is accounted as waiting for IO.
3637 * The return value is 0 if timed out, and positive (at least 1, or number of
3638 * jiffies left till timeout) if completed.
3640 unsigned long __sched
3641 wait_for_completion_io_timeout(struct completion
*x
, unsigned long timeout
)
3643 return wait_for_common_io(x
, timeout
, TASK_UNINTERRUPTIBLE
);
3645 EXPORT_SYMBOL(wait_for_completion_io_timeout
);
3648 * wait_for_completion_interruptible: - waits for completion of a task (w/intr)
3649 * @x: holds the state of this particular completion
3651 * This waits for completion of a specific task to be signaled. It is
3654 * The return value is -ERESTARTSYS if interrupted, 0 if completed.
3656 int __sched
wait_for_completion_interruptible(struct completion
*x
)
3658 long t
= wait_for_common(x
, MAX_SCHEDULE_TIMEOUT
, TASK_INTERRUPTIBLE
);
3659 if (t
== -ERESTARTSYS
)
3663 EXPORT_SYMBOL(wait_for_completion_interruptible
);
3666 * wait_for_completion_interruptible_timeout: - waits for completion (w/(to,intr))
3667 * @x: holds the state of this particular completion
3668 * @timeout: timeout value in jiffies
3670 * This waits for either a completion of a specific task to be signaled or for a
3671 * specified timeout to expire. It is interruptible. The timeout is in jiffies.
3673 * The return value is -ERESTARTSYS if interrupted, 0 if timed out,
3674 * positive (at least 1, or number of jiffies left till timeout) if completed.
3677 wait_for_completion_interruptible_timeout(struct completion
*x
,
3678 unsigned long timeout
)
3680 return wait_for_common(x
, timeout
, TASK_INTERRUPTIBLE
);
3682 EXPORT_SYMBOL(wait_for_completion_interruptible_timeout
);
3685 * wait_for_completion_killable: - waits for completion of a task (killable)
3686 * @x: holds the state of this particular completion
3688 * This waits to be signaled for completion of a specific task. It can be
3689 * interrupted by a kill signal.
3691 * The return value is -ERESTARTSYS if interrupted, 0 if completed.
3693 int __sched
wait_for_completion_killable(struct completion
*x
)
3695 long t
= wait_for_common(x
, MAX_SCHEDULE_TIMEOUT
, TASK_KILLABLE
);
3696 if (t
== -ERESTARTSYS
)
3700 EXPORT_SYMBOL(wait_for_completion_killable
);
3703 * wait_for_completion_killable_timeout: - waits for completion of a task (w/(to,killable))
3704 * @x: holds the state of this particular completion
3705 * @timeout: timeout value in jiffies
3707 * This waits for either a completion of a specific task to be
3708 * signaled or for a specified timeout to expire. It can be
3709 * interrupted by a kill signal. 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_killable_timeout(struct completion
*x
,
3716 unsigned long timeout
)
3718 return wait_for_common(x
, timeout
, TASK_KILLABLE
);
3720 EXPORT_SYMBOL(wait_for_completion_killable_timeout
);
3723 * try_wait_for_completion - try to decrement a completion without blocking
3724 * @x: completion structure
3726 * Returns: 0 if a decrement cannot be done without blocking
3727 * 1 if a decrement succeeded.
3729 * If a completion is being used as a counting completion,
3730 * attempt to decrement the counter without blocking. This
3731 * enables us to avoid waiting if the resource the completion
3732 * is protecting is not available.
3734 bool try_wait_for_completion(struct completion
*x
)
3736 unsigned long flags
;
3739 spin_lock_irqsave(&x
->wait
.lock
, flags
);
3744 spin_unlock_irqrestore(&x
->wait
.lock
, flags
);
3747 EXPORT_SYMBOL(try_wait_for_completion
);
3750 * completion_done - Test to see if a completion has any waiters
3751 * @x: completion structure
3753 * Returns: 0 if there are waiters (wait_for_completion() in progress)
3754 * 1 if there are no waiters.
3757 bool completion_done(struct completion
*x
)
3759 unsigned long flags
;
3762 spin_lock_irqsave(&x
->wait
.lock
, flags
);
3765 spin_unlock_irqrestore(&x
->wait
.lock
, flags
);
3768 EXPORT_SYMBOL(completion_done
);
3771 sleep_on_common(wait_queue_head_t
*q
, int state
, long timeout
)
3773 unsigned long flags
;
3776 init_waitqueue_entry(&wait
, current
);
3778 __set_current_state(state
);
3780 spin_lock_irqsave(&q
->lock
, flags
);
3781 __add_wait_queue(q
, &wait
);
3782 spin_unlock(&q
->lock
);
3783 timeout
= schedule_timeout(timeout
);
3784 spin_lock_irq(&q
->lock
);
3785 __remove_wait_queue(q
, &wait
);
3786 spin_unlock_irqrestore(&q
->lock
, flags
);
3791 void __sched
interruptible_sleep_on(wait_queue_head_t
*q
)
3793 sleep_on_common(q
, TASK_INTERRUPTIBLE
, MAX_SCHEDULE_TIMEOUT
);
3795 EXPORT_SYMBOL(interruptible_sleep_on
);
3798 interruptible_sleep_on_timeout(wait_queue_head_t
*q
, long timeout
)
3800 return sleep_on_common(q
, TASK_INTERRUPTIBLE
, timeout
);
3802 EXPORT_SYMBOL(interruptible_sleep_on_timeout
);
3804 void __sched
sleep_on(wait_queue_head_t
*q
)
3806 sleep_on_common(q
, TASK_UNINTERRUPTIBLE
, MAX_SCHEDULE_TIMEOUT
);
3808 EXPORT_SYMBOL(sleep_on
);
3810 long __sched
sleep_on_timeout(wait_queue_head_t
*q
, long timeout
)
3812 return sleep_on_common(q
, TASK_UNINTERRUPTIBLE
, timeout
);
3814 EXPORT_SYMBOL(sleep_on_timeout
);
3816 #ifdef CONFIG_RT_MUTEXES
3819 * rt_mutex_setprio - set the current priority of a task
3821 * @prio: prio value (kernel-internal form)
3823 * This function changes the 'effective' priority of a task. It does
3824 * not touch ->normal_prio like __setscheduler().
3826 * Used by the rt_mutex code to implement priority inheritance logic.
3828 void rt_mutex_setprio(struct task_struct
*p
, int prio
)
3830 int oldprio
, on_rq
, running
;
3832 const struct sched_class
*prev_class
;
3834 BUG_ON(prio
< 0 || prio
> MAX_PRIO
);
3836 rq
= __task_rq_lock(p
);
3839 * Idle task boosting is a nono in general. There is one
3840 * exception, when PREEMPT_RT and NOHZ is active:
3842 * The idle task calls get_next_timer_interrupt() and holds
3843 * the timer wheel base->lock on the CPU and another CPU wants
3844 * to access the timer (probably to cancel it). We can safely
3845 * ignore the boosting request, as the idle CPU runs this code
3846 * with interrupts disabled and will complete the lock
3847 * protected section without being interrupted. So there is no
3848 * real need to boost.
3850 if (unlikely(p
== rq
->idle
)) {
3851 WARN_ON(p
!= rq
->curr
);
3852 WARN_ON(p
->pi_blocked_on
);
3856 trace_sched_pi_setprio(p
, prio
);
3858 prev_class
= p
->sched_class
;
3860 running
= task_current(rq
, p
);
3862 dequeue_task(rq
, p
, 0);
3864 p
->sched_class
->put_prev_task(rq
, p
);
3867 p
->sched_class
= &rt_sched_class
;
3869 p
->sched_class
= &fair_sched_class
;
3874 p
->sched_class
->set_curr_task(rq
);
3876 enqueue_task(rq
, p
, oldprio
< prio
? ENQUEUE_HEAD
: 0);
3878 check_class_changed(rq
, p
, prev_class
, oldprio
);
3880 __task_rq_unlock(rq
);
3884 #ifdef CONFIG_MT_PRIO_TRACER
3885 void set_user_nice_core(struct task_struct
*p
, long nice
)
3887 int old_prio
, delta
, on_rq
;
3888 unsigned long flags
;
3891 if (TASK_NICE(p
) == nice
|| nice
< -20 || nice
> 19)
3894 * We have to be careful, if called from sys_setpriority(),
3895 * the task might be in the middle of scheduling on another CPU.
3897 rq
= task_rq_lock(p
, &flags
);
3899 * The RT priorities are set via sched_setscheduler(), but we still
3900 * allow the 'normal' nice value to be set - but as expected
3901 * it wont have any effect on scheduling until the task is
3902 * SCHED_FIFO/SCHED_RR:
3904 if (task_has_rt_policy(p
)) {
3905 p
->static_prio
= NICE_TO_PRIO(nice
);
3910 dequeue_task(rq
, p
, 0);
3912 p
->static_prio
= NICE_TO_PRIO(nice
);
3915 p
->prio
= effective_prio(p
);
3916 delta
= p
->prio
- old_prio
;
3919 enqueue_task(rq
, p
, 0);
3921 * If the task increased its priority or is running and
3922 * lowered its priority, then reschedule its CPU:
3924 if (delta
< 0 || (delta
> 0 && task_running(rq
, p
)))
3925 resched_task(rq
->curr
);
3928 task_rq_unlock(rq
, p
, &flags
);
3931 void set_user_nice(struct task_struct
*p
, long nice
)
3933 set_user_nice_core(p
, nice
);
3934 /* setting nice implies to set a normal sched policy */
3935 update_prio_tracer(task_pid_nr(p
), NICE_TO_PRIO(nice
), 0, PTS_KRNL
);
3937 #else /* !CONFIG_MT_PRIO_TRACER */
3938 void set_user_nice(struct task_struct
*p
, long nice
)
3940 int old_prio
, delta
, on_rq
;
3941 unsigned long flags
;
3944 if (TASK_NICE(p
) == nice
|| nice
< -20 || nice
> 19)
3947 * We have to be careful, if called from sys_setpriority(),
3948 * the task might be in the middle of scheduling on another CPU.
3950 rq
= task_rq_lock(p
, &flags
);
3952 * The RT priorities are set via sched_setscheduler(), but we still
3953 * allow the 'normal' nice value to be set - but as expected
3954 * it wont have any effect on scheduling until the task is
3955 * SCHED_FIFO/SCHED_RR:
3957 if (task_has_rt_policy(p
)) {
3958 p
->static_prio
= NICE_TO_PRIO(nice
);
3963 dequeue_task(rq
, p
, 0);
3965 p
->static_prio
= NICE_TO_PRIO(nice
);
3968 p
->prio
= effective_prio(p
);
3969 delta
= p
->prio
- old_prio
;
3972 enqueue_task(rq
, p
, 0);
3974 * If the task increased its priority or is running and
3975 * lowered its priority, then reschedule its CPU:
3977 if (delta
< 0 || (delta
> 0 && task_running(rq
, p
)))
3978 resched_task(rq
->curr
);
3981 task_rq_unlock(rq
, p
, &flags
);
3984 EXPORT_SYMBOL(set_user_nice
);
3987 * can_nice - check if a task can reduce its nice value
3991 int can_nice(const struct task_struct
*p
, const int nice
)
3993 /* convert nice value [19,-20] to rlimit style value [1,40] */
3994 int nice_rlim
= 20 - nice
;
3996 return (nice_rlim
<= task_rlimit(p
, RLIMIT_NICE
) ||
3997 capable(CAP_SYS_NICE
));
4000 #ifdef __ARCH_WANT_SYS_NICE
4003 * sys_nice - change the priority of the current process.
4004 * @increment: priority increment
4006 * sys_setpriority is a more generic, but much slower function that
4007 * does similar things.
4009 SYSCALL_DEFINE1(nice
, int, increment
)
4014 * Setpriority might change our priority at the same moment.
4015 * We don't have to worry. Conceptually one call occurs first
4016 * and we have a single winner.
4018 if (increment
< -40)
4023 nice
= TASK_NICE(current
) + increment
;
4029 if (increment
< 0 && !can_nice(current
, nice
))
4032 retval
= security_task_setnice(current
, nice
);
4035 #ifdef CONFIG_MT_PRIO_TRACER
4036 set_user_nice_syscall(current
, nice
);
4038 set_user_nice(current
, nice
);
4046 * task_prio - return the priority value of a given task.
4047 * @p: the task in question.
4049 * This is the priority value as seen by users in /proc.
4050 * RT tasks are offset by -200. Normal tasks are centered
4051 * around 0, value goes from -16 to +15.
4053 int task_prio(const struct task_struct
*p
)
4055 return p
->prio
- MAX_RT_PRIO
;
4059 * task_nice - return the nice value of a given task.
4060 * @p: the task in question.
4062 int task_nice(const struct task_struct
*p
)
4064 return TASK_NICE(p
);
4066 EXPORT_SYMBOL(task_nice
);
4069 * idle_cpu - is a given cpu idle currently?
4070 * @cpu: the processor in question.
4072 int idle_cpu(int cpu
)
4074 struct rq
*rq
= cpu_rq(cpu
);
4076 if (rq
->curr
!= rq
->idle
)
4083 if (!llist_empty(&rq
->wake_list
))
4091 * idle_task - return the idle task for a given cpu.
4092 * @cpu: the processor in question.
4094 struct task_struct
*idle_task(int cpu
)
4096 return cpu_rq(cpu
)->idle
;
4100 * find_process_by_pid - find a process with a matching PID value.
4101 * @pid: the pid in question.
4103 static struct task_struct
*find_process_by_pid(pid_t pid
)
4105 return pid
? find_task_by_vpid(pid
) : current
;
4108 extern struct cpumask hmp_slow_cpu_mask
;
4110 /* Actually do priority change: must hold rq lock. */
4112 __setscheduler(struct rq
*rq
, struct task_struct
*p
, int policy
, int prio
)
4115 p
->rt_priority
= prio
;
4116 p
->normal_prio
= normal_prio(p
);
4117 /* we are holding p->pi_lock already */
4118 p
->prio
= rt_mutex_getprio(p
);
4119 if (rt_prio(p
->prio
)) {
4120 p
->sched_class
= &rt_sched_class
;
4123 p
->sched_class
= &fair_sched_class
;
4128 * check the target process has a UID that matches the current process's
4130 static bool check_same_owner(struct task_struct
*p
)
4132 const struct cred
*cred
= current_cred(), *pcred
;
4136 pcred
= __task_cred(p
);
4137 match
= (uid_eq(cred
->euid
, pcred
->euid
) ||
4138 uid_eq(cred
->euid
, pcred
->uid
));
4143 static int check_mt_allow_rt(struct sched_param
*param
)
4146 if(0 == MT_ALLOW_RT_PRIO_BIT
){
4147 //this condition check will be removed
4151 if(param
->sched_priority
& MT_ALLOW_RT_PRIO_BIT
){
4152 param
->sched_priority
&= ~MT_ALLOW_RT_PRIO_BIT
;
4158 static int __sched_setscheduler(struct task_struct
*p
, int policy
,
4159 const struct sched_param
*param
, bool user
)
4161 int retval
, oldprio
, oldpolicy
= -1, on_rq
, running
;
4162 unsigned long flags
;
4163 const struct sched_class
*prev_class
;
4167 /* may grab non-irq protected spin_locks */
4168 BUG_ON(in_interrupt());
4170 /* double check policy once rq lock held */
4172 reset_on_fork
= p
->sched_reset_on_fork
;
4173 policy
= oldpolicy
= p
->policy
;
4175 reset_on_fork
= !!(policy
& SCHED_RESET_ON_FORK
);
4176 policy
&= ~SCHED_RESET_ON_FORK
;
4178 if (policy
!= SCHED_FIFO
&& policy
!= SCHED_RR
&&
4179 policy
!= SCHED_NORMAL
&& policy
!= SCHED_BATCH
&&
4180 policy
!= SCHED_IDLE
)
4184 if(rt_policy(policy
)){
4185 if (!check_mt_allow_rt((struct sched_param
*)param
)){
4186 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
);
4192 * Valid priorities for SCHED_FIFO and SCHED_RR are
4193 * 1..MAX_USER_RT_PRIO-1, valid priority for SCHED_NORMAL,
4194 * SCHED_BATCH and SCHED_IDLE is 0.
4196 if (param
->sched_priority
< 0 ||
4197 (p
->mm
&& param
->sched_priority
> MAX_USER_RT_PRIO
-1) ||
4198 (!p
->mm
&& param
->sched_priority
> MAX_RT_PRIO
-1))
4200 if (rt_policy(policy
) != (param
->sched_priority
!= 0))
4204 * Allow unprivileged RT tasks to decrease priority:
4206 if (user
&& !capable(CAP_SYS_NICE
)) {
4207 if (rt_policy(policy
)) {
4208 unsigned long rlim_rtprio
=
4209 task_rlimit(p
, RLIMIT_RTPRIO
);
4211 /* can't set/change the rt policy */
4212 if (policy
!= p
->policy
&& !rlim_rtprio
)
4215 /* can't increase priority */
4216 if (param
->sched_priority
> p
->rt_priority
&&
4217 param
->sched_priority
> rlim_rtprio
)
4222 * Treat SCHED_IDLE as nice 20. Only allow a switch to
4223 * SCHED_NORMAL if the RLIMIT_NICE would normally permit it.
4225 if (p
->policy
== SCHED_IDLE
&& policy
!= SCHED_IDLE
) {
4226 if (!can_nice(p
, TASK_NICE(p
)))
4230 /* can't change other user's priorities */
4231 if (!check_same_owner(p
))
4234 /* Normal users shall not reset the sched_reset_on_fork flag */
4235 if (p
->sched_reset_on_fork
&& !reset_on_fork
)
4240 retval
= security_task_setscheduler(p
);
4246 * make sure no PI-waiters arrive (or leave) while we are
4247 * changing the priority of the task:
4249 * To be able to change p->policy safely, the appropriate
4250 * runqueue lock must be held.
4252 rq
= task_rq_lock(p
, &flags
);
4255 * Changing the policy of the stop threads its a very bad idea
4257 if (p
== rq
->stop
) {
4258 task_rq_unlock(rq
, p
, &flags
);
4263 * If not changing anything there's no need to proceed further:
4265 if (unlikely(policy
== p
->policy
&& (!rt_policy(policy
) ||
4266 param
->sched_priority
== p
->rt_priority
))) {
4267 task_rq_unlock(rq
, p
, &flags
);
4271 #ifdef CONFIG_RT_GROUP_SCHED
4274 * Do not allow realtime tasks into groups that have no runtime
4277 if (rt_bandwidth_enabled() && rt_policy(policy
) &&
4278 task_group(p
)->rt_bandwidth
.rt_runtime
== 0 &&
4279 !task_group_is_autogroup(task_group(p
))) {
4280 task_rq_unlock(rq
, p
, &flags
);
4286 /* recheck policy now with rq lock held */
4287 if (unlikely(oldpolicy
!= -1 && oldpolicy
!= p
->policy
)) {
4288 policy
= oldpolicy
= -1;
4289 task_rq_unlock(rq
, p
, &flags
);
4293 running
= task_current(rq
, p
);
4295 dequeue_task(rq
, p
, 0);
4297 p
->sched_class
->put_prev_task(rq
, p
);
4299 p
->sched_reset_on_fork
= reset_on_fork
;
4302 prev_class
= p
->sched_class
;
4303 __setscheduler(rq
, p
, policy
, param
->sched_priority
);
4306 p
->sched_class
->set_curr_task(rq
);
4308 enqueue_task(rq
, p
, 0);
4310 check_class_changed(rq
, p
, prev_class
, oldprio
);
4311 task_rq_unlock(rq
, p
, &flags
);
4313 rt_mutex_adjust_pi(p
);
4319 * sched_setscheduler - change the scheduling policy and/or RT priority of a thread.
4320 * @p: the task in question.
4321 * @policy: new policy.
4322 * @param: structure containing the new RT priority.
4324 * NOTE that the task may be already dead.
4326 #ifdef CONFIG_MT_PRIO_TRACER
4327 int sched_setscheduler_core(struct task_struct
*p
, int policy
,
4328 const struct sched_param
*param
)
4330 return __sched_setscheduler(p
, policy
, param
, true);
4333 int sched_setscheduler(struct task_struct
*p
, int policy
,
4334 const struct sched_param
*param
)
4338 retval
= sched_setscheduler_core(p
, policy
, param
);
4340 int prio
= param
->sched_priority
& ~MT_ALLOW_RT_PRIO_BIT
;
4341 if (!rt_policy(policy
))
4342 prio
= __normal_prio(p
);
4344 prio
= MAX_RT_PRIO
-1 - prio
;
4345 update_prio_tracer(task_pid_nr(p
), prio
, policy
, PTS_KRNL
);
4349 #else /* !CONFIG_MT_PRIO_TRACER */
4350 int sched_setscheduler(struct task_struct
*p
, int policy
,
4351 const struct sched_param
*param
)
4353 return __sched_setscheduler(p
, policy
, param
, true);
4356 EXPORT_SYMBOL_GPL(sched_setscheduler
);
4359 * sched_setscheduler_nocheck - change the scheduling policy and/or RT priority of a thread from kernelspace.
4360 * @p: the task in question.
4361 * @policy: new policy.
4362 * @param: structure containing the new RT priority.
4364 * Just like sched_setscheduler, only don't bother checking if the
4365 * current context has permission. For example, this is needed in
4366 * stop_machine(): we create temporary high priority worker threads,
4367 * but our caller might not have that capability.
4369 #ifdef CONFIG_MT_PRIO_TRACER
4370 int sched_setscheduler_nocheck_core(struct task_struct
*p
, int policy
,
4371 const struct sched_param
*param
)
4373 return __sched_setscheduler(p
, policy
, param
, false);
4377 int sched_setscheduler_nocheck(struct task_struct
*p
, int policy
,
4378 const struct sched_param
*param
)
4382 retval
= sched_setscheduler_nocheck_core(p
, policy
, param
);
4384 int prio
= param
->sched_priority
& ~MT_ALLOW_RT_PRIO_BIT
;
4385 if (!rt_policy(policy
))
4386 prio
= __normal_prio(p
);
4388 prio
= MAX_RT_PRIO
-1 - prio
;
4389 update_prio_tracer(task_pid_nr(p
), prio
, policy
, PTS_KRNL
);
4393 #else /* !CONFIG_MT_PRIO_TRACER */
4394 int sched_setscheduler_nocheck(struct task_struct
*p
, int policy
,
4395 const struct sched_param
*param
)
4397 return __sched_setscheduler(p
, policy
, param
, false);
4402 do_sched_setscheduler(pid_t pid
, int policy
, struct sched_param __user
*param
)
4404 struct sched_param lparam
;
4405 struct task_struct
*p
;
4408 if (!param
|| pid
< 0)
4410 if (copy_from_user(&lparam
, param
, sizeof(struct sched_param
)))
4415 p
= find_process_by_pid(pid
);
4416 #ifdef CONFIG_MT_PRIO_TRACER
4418 retval
= sched_setscheduler_syscall(p
, policy
, &lparam
);
4421 retval
= sched_setscheduler(p
, policy
, &lparam
);
4430 * sys_sched_setscheduler - set/change the scheduler policy and RT priority
4431 * @pid: the pid in question.
4432 * @policy: new policy.
4433 * @param: structure containing the new RT priority.
4435 SYSCALL_DEFINE3(sched_setscheduler
, pid_t
, pid
, int, policy
,
4436 struct sched_param __user
*, param
)
4438 /* negative values for policy are not valid */
4442 return do_sched_setscheduler(pid
, policy
, param
);
4446 * sys_sched_setparam - set/change the RT priority of a thread
4447 * @pid: the pid in question.
4448 * @param: structure containing the new RT priority.
4450 SYSCALL_DEFINE2(sched_setparam
, pid_t
, pid
, struct sched_param __user
*, param
)
4452 return do_sched_setscheduler(pid
, -1, param
);
4456 * sys_sched_getscheduler - get the policy (scheduling class) of a thread
4457 * @pid: the pid in question.
4459 SYSCALL_DEFINE1(sched_getscheduler
, pid_t
, pid
)
4461 struct task_struct
*p
;
4469 p
= find_process_by_pid(pid
);
4471 retval
= security_task_getscheduler(p
);
4474 | (p
->sched_reset_on_fork
? SCHED_RESET_ON_FORK
: 0);
4481 * sys_sched_getparam - get the RT priority of a thread
4482 * @pid: the pid in question.
4483 * @param: structure containing the RT priority.
4485 SYSCALL_DEFINE2(sched_getparam
, pid_t
, pid
, struct sched_param __user
*, param
)
4487 struct sched_param lp
;
4488 struct task_struct
*p
;
4491 if (!param
|| pid
< 0)
4495 p
= find_process_by_pid(pid
);
4500 retval
= security_task_getscheduler(p
);
4504 lp
.sched_priority
= p
->rt_priority
;
4508 * This one might sleep, we cannot do it with a spinlock held ...
4510 retval
= copy_to_user(param
, &lp
, sizeof(*param
)) ? -EFAULT
: 0;
4519 long sched_setaffinity(pid_t pid
, const struct cpumask
*in_mask
)
4521 cpumask_var_t cpus_allowed
, new_mask
;
4522 struct task_struct
*p
;
4528 p
= find_process_by_pid(pid
);
4532 printk(KERN_DEBUG
"SCHED: setaffinity find process %d fail\n", pid
);
4536 /* Prevent p going away */
4540 if (p
->flags
& PF_NO_SETAFFINITY
) {
4542 printk(KERN_DEBUG
"SCHED: setaffinity flags PF_NO_SETAFFINITY fail\n");
4545 if (!alloc_cpumask_var(&cpus_allowed
, GFP_KERNEL
)) {
4547 printk(KERN_DEBUG
"SCHED: setaffinity allo_cpumask_var for cpus_allowed fail\n");
4550 if (!alloc_cpumask_var(&new_mask
, GFP_KERNEL
)) {
4552 printk(KERN_DEBUG
"SCHED: setaffinity allo_cpumask_var for new_mask fail\n");
4553 goto out_free_cpus_allowed
;
4556 if (!check_same_owner(p
)) {
4558 if (!ns_capable(__task_cred(p
)->user_ns
, CAP_SYS_NICE
)) {
4560 printk(KERN_DEBUG
"SCHED: setaffinity check_same_owner and task_ns_capable fail\n");
4566 retval
= security_task_setscheduler(p
);
4568 printk(KERN_DEBUG
"SCHED: setaffinity security_task_setscheduler fail, status: %d\n", retval
);
4572 cpuset_cpus_allowed(p
, cpus_allowed
);
4573 cpumask_and(new_mask
, in_mask
, cpus_allowed
);
4575 retval
= set_cpus_allowed_ptr(p
, new_mask
);
4577 printk(KERN_DEBUG
"SCHED: set_cpus_allowed_ptr status %d\n", retval
);
4580 cpuset_cpus_allowed(p
, cpus_allowed
);
4581 if (!cpumask_subset(new_mask
, cpus_allowed
)) {
4583 * We must have raced with a concurrent cpuset
4584 * update. Just reset the cpus_allowed to the
4585 * cpuset's cpus_allowed
4587 cpumask_copy(new_mask
, cpus_allowed
);
4592 free_cpumask_var(new_mask
);
4593 out_free_cpus_allowed
:
4594 free_cpumask_var(cpus_allowed
);
4599 printk(KERN_DEBUG
"SCHED: setaffinity status %d\n", retval
);
4603 static int get_user_cpu_mask(unsigned long __user
*user_mask_ptr
, unsigned len
,
4604 struct cpumask
*new_mask
)
4606 if (len
< cpumask_size())
4607 cpumask_clear(new_mask
);
4608 else if (len
> cpumask_size())
4609 len
= cpumask_size();
4611 return copy_from_user(new_mask
, user_mask_ptr
, len
) ? -EFAULT
: 0;
4615 * sys_sched_setaffinity - set the cpu affinity of a process
4616 * @pid: pid of the process
4617 * @len: length in bytes of the bitmask pointed to by user_mask_ptr
4618 * @user_mask_ptr: user-space pointer to the new cpu mask
4620 SYSCALL_DEFINE3(sched_setaffinity
, pid_t
, pid
, unsigned int, len
,
4621 unsigned long __user
*, user_mask_ptr
)
4623 cpumask_var_t new_mask
;
4626 if (!alloc_cpumask_var(&new_mask
, GFP_KERNEL
))
4629 retval
= get_user_cpu_mask(user_mask_ptr
, len
, new_mask
);
4631 retval
= sched_setaffinity(pid
, new_mask
);
4632 free_cpumask_var(new_mask
);
4636 long sched_getaffinity(pid_t pid
, struct cpumask
*mask
)
4638 struct task_struct
*p
;
4639 unsigned long flags
;
4646 p
= find_process_by_pid(pid
);
4648 printk(KERN_DEBUG
"SCHED: getaffinity find process %d fail\n", pid
);
4652 retval
= security_task_getscheduler(p
);
4654 printk(KERN_DEBUG
"SCHED: getaffinity security_task_getscheduler fail, status: %d\n", retval
);
4658 raw_spin_lock_irqsave(&p
->pi_lock
, flags
);
4659 cpumask_and(mask
, &p
->cpus_allowed
, cpu_online_mask
);
4660 raw_spin_unlock_irqrestore(&p
->pi_lock
, flags
);
4667 printk(KERN_DEBUG
"SCHED: getaffinity status %d\n", retval
);
4673 * sys_sched_getaffinity - get the cpu affinity of a process
4674 * @pid: pid of the process
4675 * @len: length in bytes of the bitmask pointed to by user_mask_ptr
4676 * @user_mask_ptr: user-space pointer to hold the current cpu mask
4678 SYSCALL_DEFINE3(sched_getaffinity
, pid_t
, pid
, unsigned int, len
,
4679 unsigned long __user
*, user_mask_ptr
)
4684 if ((len
* BITS_PER_BYTE
) < nr_cpu_ids
)
4686 if (len
& (sizeof(unsigned long)-1))
4689 if (!alloc_cpumask_var(&mask
, GFP_KERNEL
))
4692 ret
= sched_getaffinity(pid
, mask
);
4694 size_t retlen
= min_t(size_t, len
, cpumask_size());
4696 if (copy_to_user(user_mask_ptr
, mask
, retlen
))
4701 free_cpumask_var(mask
);
4707 * sys_sched_yield - yield the current processor to other threads.
4709 * This function yields the current CPU to other tasks. If there are no
4710 * other threads running on this CPU then this function will return.
4712 SYSCALL_DEFINE0(sched_yield
)
4714 struct rq
*rq
= this_rq_lock();
4716 schedstat_inc(rq
, yld_count
);
4717 current
->sched_class
->yield_task(rq
);
4720 * Since we are going to call schedule() anyway, there's
4721 * no need to preempt or enable interrupts:
4723 __release(rq
->lock
);
4724 spin_release(&rq
->lock
.dep_map
, 1, _THIS_IP_
);
4725 do_raw_spin_unlock(&rq
->lock
);
4726 sched_preempt_enable_no_resched();
4733 static inline int should_resched(void)
4735 return need_resched() && !(preempt_count() & PREEMPT_ACTIVE
);
4738 static void __cond_resched(void)
4740 add_preempt_count(PREEMPT_ACTIVE
);
4742 sub_preempt_count(PREEMPT_ACTIVE
);
4745 int __sched
_cond_resched(void)
4747 if (should_resched()) {
4753 EXPORT_SYMBOL(_cond_resched
);
4756 * __cond_resched_lock() - if a reschedule is pending, drop the given lock,
4757 * call schedule, and on return reacquire the lock.
4759 * This works OK both with and without CONFIG_PREEMPT. We do strange low-level
4760 * operations here to prevent schedule() from being called twice (once via
4761 * spin_unlock(), once by hand).
4763 int __cond_resched_lock(spinlock_t
*lock
)
4765 int resched
= should_resched();
4768 lockdep_assert_held(lock
);
4770 if (spin_needbreak(lock
) || resched
) {
4781 EXPORT_SYMBOL(__cond_resched_lock
);
4783 int __sched
__cond_resched_softirq(void)
4785 BUG_ON(!in_softirq());
4787 if (should_resched()) {
4795 EXPORT_SYMBOL(__cond_resched_softirq
);
4798 * yield - yield the current processor to other threads.
4800 * Do not ever use this function, there's a 99% chance you're doing it wrong.
4802 * The scheduler is at all times free to pick the calling task as the most
4803 * eligible task to run, if removing the yield() call from your code breaks
4804 * it, its already broken.
4806 * Typical broken usage is:
4811 * where one assumes that yield() will let 'the other' process run that will
4812 * make event true. If the current task is a SCHED_FIFO task that will never
4813 * happen. Never use yield() as a progress guarantee!!
4815 * If you want to use yield() to wait for something, use wait_event().
4816 * If you want to use yield() to be 'nice' for others, use cond_resched().
4817 * If you still want to use yield(), do not!
4819 void __sched
yield(void)
4821 set_current_state(TASK_RUNNING
);
4824 EXPORT_SYMBOL(yield
);
4827 * yield_to - yield the current processor to another thread in
4828 * your thread group, or accelerate that thread toward the
4829 * processor it's on.
4831 * @preempt: whether task preemption is allowed or not
4833 * It's the caller's job to ensure that the target task struct
4834 * can't go away on us before we can do any checks.
4837 * true (>0) if we indeed boosted the target task.
4838 * false (0) if we failed to boost the target.
4839 * -ESRCH if there's no task to yield to.
4841 bool __sched
yield_to(struct task_struct
*p
, bool preempt
)
4843 struct task_struct
*curr
= current
;
4844 struct rq
*rq
, *p_rq
;
4845 unsigned long flags
;
4848 local_irq_save(flags
);
4854 * If we're the only runnable task on the rq and target rq also
4855 * has only one task, there's absolutely no point in yielding.
4857 if (rq
->nr_running
== 1 && p_rq
->nr_running
== 1) {
4862 double_rq_lock(rq
, p_rq
);
4863 while (task_rq(p
) != p_rq
) {
4864 double_rq_unlock(rq
, p_rq
);
4868 if (!curr
->sched_class
->yield_to_task
)
4871 if (curr
->sched_class
!= p
->sched_class
)
4874 if (task_running(p_rq
, p
) || p
->state
)
4877 yielded
= curr
->sched_class
->yield_to_task(rq
, p
, preempt
);
4879 schedstat_inc(rq
, yld_count
);
4881 * Make p's CPU reschedule; pick_next_entity takes care of
4884 if (preempt
&& rq
!= p_rq
)
4885 resched_task(p_rq
->curr
);
4889 double_rq_unlock(rq
, p_rq
);
4891 local_irq_restore(flags
);
4898 EXPORT_SYMBOL_GPL(yield_to
);
4901 * This task is about to go to sleep on IO. Increment rq->nr_iowait so
4902 * that process accounting knows that this is a task in IO wait state.
4904 void __sched
io_schedule(void)
4906 struct rq
*rq
= raw_rq();
4908 delayacct_blkio_start();
4909 atomic_inc(&rq
->nr_iowait
);
4910 blk_flush_plug(current
);
4911 current
->in_iowait
= 1;
4913 current
->in_iowait
= 0;
4914 atomic_dec(&rq
->nr_iowait
);
4915 delayacct_blkio_end();
4917 EXPORT_SYMBOL(io_schedule
);
4919 long __sched
io_schedule_timeout(long timeout
)
4921 struct rq
*rq
= raw_rq();
4924 delayacct_blkio_start();
4925 atomic_inc(&rq
->nr_iowait
);
4926 blk_flush_plug(current
);
4927 current
->in_iowait
= 1;
4928 ret
= schedule_timeout(timeout
);
4929 current
->in_iowait
= 0;
4930 atomic_dec(&rq
->nr_iowait
);
4931 delayacct_blkio_end();
4936 * sys_sched_get_priority_max - return maximum RT priority.
4937 * @policy: scheduling class.
4939 * this syscall returns the maximum rt_priority that can be used
4940 * by a given scheduling class.
4942 SYSCALL_DEFINE1(sched_get_priority_max
, int, policy
)
4949 ret
= MAX_USER_RT_PRIO
-1;
4961 * sys_sched_get_priority_min - return minimum RT priority.
4962 * @policy: scheduling class.
4964 * this syscall returns the minimum rt_priority that can be used
4965 * by a given scheduling class.
4967 SYSCALL_DEFINE1(sched_get_priority_min
, int, policy
)
4985 * sys_sched_rr_get_interval - return the default timeslice of a process.
4986 * @pid: pid of the process.
4987 * @interval: userspace pointer to the timeslice value.
4989 * this syscall writes the default timeslice value of a given process
4990 * into the user-space timespec buffer. A value of '0' means infinity.
4992 SYSCALL_DEFINE2(sched_rr_get_interval
, pid_t
, pid
,
4993 struct timespec __user
*, interval
)
4995 struct task_struct
*p
;
4996 unsigned int time_slice
;
4997 unsigned long flags
;
5007 p
= find_process_by_pid(pid
);
5011 retval
= security_task_getscheduler(p
);
5015 rq
= task_rq_lock(p
, &flags
);
5016 time_slice
= p
->sched_class
->get_rr_interval(rq
, p
);
5017 task_rq_unlock(rq
, p
, &flags
);
5020 jiffies_to_timespec(time_slice
, &t
);
5021 retval
= copy_to_user(interval
, &t
, sizeof(t
)) ? -EFAULT
: 0;
5029 static const char stat_nam
[] = TASK_STATE_TO_CHAR_STR
;
5030 #ifdef CONFIG_MT_DEBUG_MUTEXES
5031 void mt_mutex_state(struct task_struct
*p
)
5033 struct task_struct
*locker
;
5035 locker
= p
->blocked_on
->task_wait_on
;
5036 if(find_task_by_vpid(locker
->pid
) != NULL
){
5037 printk("Hint: wait on mutex, holder is [%d:%s:%ld]\n", locker
->pid
, locker
->comm
, locker
->state
);
5038 if(locker
->state
!= TASK_RUNNING
){
5039 printk("Mutex holder process[%d:%s] is not running now:\n", locker
->pid
, locker
->comm
);
5040 show_stack(locker
, NULL
);
5044 printk("Hint: wait on mutex, but holder already released lock\n");
5049 void sched_show_task(struct task_struct
*p
)
5051 unsigned long free
= 0;
5055 state
= p
->state
? __ffs(p
->state
) + 1 : 0;
5056 printk(KERN_INFO
"%-15.15s %c", p
->comm
,
5057 state
< sizeof(stat_nam
) - 1 ? stat_nam
[state
] : '?');
5058 #if BITS_PER_LONG == 32
5059 if (state
== TASK_RUNNING
)
5060 printk(KERN_CONT
" running ");
5062 printk(KERN_CONT
" %08lx ", thread_saved_pc(p
));
5064 if (state
== TASK_RUNNING
)
5065 printk(KERN_CONT
" running task ");
5067 printk(KERN_CONT
" %016lx ", thread_saved_pc(p
));
5069 #ifdef CONFIG_DEBUG_STACK_USAGE
5070 free
= stack_not_used(p
);
5073 ppid
= task_pid_nr(rcu_dereference(p
->real_parent
));
5075 printk(KERN_CONT
"%5lu %5d %6d 0x%08lx\n", free
,
5076 task_pid_nr(p
), ppid
,
5077 (unsigned long)task_thread_info(p
)->flags
);
5079 print_worker_info(KERN_INFO
, p
);
5080 show_stack(p
, NULL
);
5081 #ifdef CONFIG_MT_DEBUG_MUTEXES
5086 void show_state_filter(unsigned long state_filter
)
5088 struct task_struct
*g
, *p
;
5090 #if BITS_PER_LONG == 32
5092 " task PC stack pid father\n");
5095 " task PC stack pid father\n");
5098 do_each_thread(g
, p
) {
5100 * reset the NMI-timeout, listing all files on a slow
5101 * console might take a lot of time:
5103 touch_nmi_watchdog();
5104 if (!state_filter
|| (p
->state
& state_filter
))
5106 } while_each_thread(g
, p
);
5108 touch_all_softlockup_watchdogs();
5110 #ifdef CONFIG_SCHED_DEBUG
5111 sysrq_sched_debug_show();
5115 * Only show locks if all tasks are dumped:
5118 debug_show_all_locks();
5121 void __cpuinit
init_idle_bootup_task(struct task_struct
*idle
)
5123 idle
->sched_class
= &idle_sched_class
;
5127 * init_idle - set up an idle thread for a given CPU
5128 * @idle: task in question
5129 * @cpu: cpu the idle task belongs to
5131 * NOTE: this function does not set the idle thread's NEED_RESCHED
5132 * flag, to make booting more robust.
5134 void __cpuinit
init_idle(struct task_struct
*idle
, int cpu
)
5136 struct rq
*rq
= cpu_rq(cpu
);
5137 unsigned long flags
;
5139 raw_spin_lock_irqsave(&rq
->lock
, flags
);
5142 idle
->state
= TASK_RUNNING
;
5143 idle
->se
.exec_start
= sched_clock();
5145 do_set_cpus_allowed(idle
, cpumask_of(cpu
));
5147 * We're having a chicken and egg problem, even though we are
5148 * holding rq->lock, the cpu isn't yet set to this cpu so the
5149 * lockdep check in task_group() will fail.
5151 * Similar case to sched_fork(). / Alternatively we could
5152 * use task_rq_lock() here and obtain the other rq->lock.
5157 __set_task_cpu(idle
, cpu
);
5160 rq
->curr
= rq
->idle
= idle
;
5161 #if defined(CONFIG_SMP)
5164 raw_spin_unlock_irqrestore(&rq
->lock
, flags
);
5166 /* Set the preempt count _outside_ the spinlocks! */
5167 task_thread_info(idle
)->preempt_count
= 0;
5170 * The idle tasks have their own, simple scheduling class:
5172 idle
->sched_class
= &idle_sched_class
;
5173 ftrace_graph_init_idle_task(idle
, cpu
);
5174 vtime_init_idle(idle
, cpu
);
5175 #if defined(CONFIG_SMP)
5176 sprintf(idle
->comm
, "%s/%d", INIT_TASK_COMM
, cpu
);
5181 void do_set_cpus_allowed(struct task_struct
*p
, const struct cpumask
*new_mask
)
5183 if (p
->sched_class
&& p
->sched_class
->set_cpus_allowed
)
5184 p
->sched_class
->set_cpus_allowed(p
, new_mask
);
5186 cpumask_copy(&p
->cpus_allowed
, new_mask
);
5187 p
->nr_cpus_allowed
= cpumask_weight(new_mask
);
5191 * This is how migration works:
5193 * 1) we invoke migration_cpu_stop() on the target CPU using
5195 * 2) stopper starts to run (implicitly forcing the migrated thread
5197 * 3) it checks whether the migrated task is still in the wrong runqueue.
5198 * 4) if it's in the wrong runqueue then the migration thread removes
5199 * it and puts it into the right queue.
5200 * 5) stopper completes and stop_one_cpu() returns and the migration
5205 * Change a given task's CPU affinity. Migrate the thread to a
5206 * proper CPU and schedule it away if the CPU it's executing on
5207 * is removed from the allowed bitmask.
5209 * NOTE: the caller must have a valid reference to the task, the
5210 * task must not exit() & deallocate itself prematurely. The
5211 * call is not atomic; no spinlocks may be held.
5213 int set_cpus_allowed_ptr(struct task_struct
*p
, const struct cpumask
*new_mask
)
5215 unsigned long flags
;
5217 unsigned int dest_cpu
;
5220 rq
= task_rq_lock(p
, &flags
);
5222 if (cpumask_equal(&p
->cpus_allowed
, new_mask
))
5225 if (!cpumask_intersects(new_mask
, cpu_active_mask
)) {
5227 printk(KERN_DEBUG
"SCHED: intersects new_mask: %lu, cpu_active_mask: %lu\n", new_mask
->bits
[0], cpu_active_mask
->bits
[0]);
5231 do_set_cpus_allowed(p
, new_mask
);
5233 /* Can the task run on the task's current CPU? If so, we're done */
5234 if (cpumask_test_cpu(task_cpu(p
), new_mask
))
5237 dest_cpu
= cpumask_any_and(cpu_active_mask
, new_mask
);
5239 struct migration_arg arg
= { p
, dest_cpu
};
5240 /* Need help from migration thread: drop lock and wait. */
5241 task_rq_unlock(rq
, p
, &flags
);
5242 stop_one_cpu(cpu_of(rq
), migration_cpu_stop
, &arg
);
5243 tlb_migrate_finish(p
->mm
);
5247 task_rq_unlock(rq
, p
, &flags
);
5251 EXPORT_SYMBOL_GPL(set_cpus_allowed_ptr
);
5254 * Move (not current) task off this cpu, onto dest cpu. We're doing
5255 * this because either it can't run here any more (set_cpus_allowed()
5256 * away from this CPU, or CPU going down), or because we're
5257 * attempting to rebalance this task on exec (sched_exec).
5259 * So we race with normal scheduler movements, but that's OK, as long
5260 * as the task is no longer on this CPU.
5262 * Returns non-zero if task was successfully migrated.
5264 static int __migrate_task(struct task_struct
*p
, int src_cpu
, int dest_cpu
)
5266 struct rq
*rq_dest
, *rq_src
;
5269 if (unlikely(!cpu_active(dest_cpu
)))
5272 rq_src
= cpu_rq(src_cpu
);
5273 rq_dest
= cpu_rq(dest_cpu
);
5275 raw_spin_lock(&p
->pi_lock
);
5276 double_rq_lock(rq_src
, rq_dest
);
5277 /* Already moved. */
5278 if (task_cpu(p
) != src_cpu
)
5280 /* Affinity changed (again). */
5281 if (!cpumask_test_cpu(dest_cpu
, tsk_cpus_allowed(p
)))
5285 * If we're not on a rq, the next wake-up will ensure we're
5289 dequeue_task(rq_src
, p
, 0);
5290 set_task_cpu(p
, dest_cpu
);
5291 enqueue_task(rq_dest
, p
, 0);
5292 check_preempt_curr(rq_dest
, p
, 0);
5297 double_rq_unlock(rq_src
, rq_dest
);
5298 raw_spin_unlock(&p
->pi_lock
);
5303 * migration_cpu_stop - this will be executed by a highprio stopper thread
5304 * and performs thread migration by bumping thread off CPU then
5305 * 'pushing' onto another runqueue.
5307 static int migration_cpu_stop(void *data
)
5309 struct migration_arg
*arg
= data
;
5312 * The original target cpu might have gone down and we might
5313 * be on another cpu but it doesn't matter.
5315 local_irq_disable();
5316 __migrate_task(arg
->task
, raw_smp_processor_id(), arg
->dest_cpu
);
5321 #ifdef CONFIG_HOTPLUG_CPU
5324 * Ensures that the idle task is using init_mm right before its cpu goes
5327 void idle_task_exit(void)
5329 struct mm_struct
*mm
= current
->active_mm
;
5331 BUG_ON(cpu_online(smp_processor_id()));
5334 switch_mm(mm
, &init_mm
, current
);
5339 * Since this CPU is going 'away' for a while, fold any nr_active delta
5340 * we might have. Assumes we're called after migrate_tasks() so that the
5341 * nr_active count is stable.
5343 * Also see the comment "Global load-average calculations".
5345 static void calc_load_migrate(struct rq
*rq
)
5347 long delta
= calc_load_fold_active(rq
);
5349 atomic_long_add(delta
, &calc_load_tasks
);
5353 * Migrate all tasks from the rq, sleeping tasks will be migrated by
5354 * try_to_wake_up()->select_task_rq().
5356 * Called with rq->lock held even though we'er in stop_machine() and
5357 * there's no concurrency possible, we hold the required locks anyway
5358 * because of lock validation efforts.
5360 static void migrate_tasks(unsigned int dead_cpu
)
5362 struct rq
*rq
= cpu_rq(dead_cpu
);
5363 struct task_struct
*next
, *stop
= rq
->stop
;
5367 * Fudge the rq selection such that the below task selection loop
5368 * doesn't get stuck on the currently eligible stop task.
5370 * We're currently inside stop_machine() and the rq is either stuck
5371 * in the stop_machine_cpu_stop() loop, or we're executing this code,
5372 * either way we should never end up calling schedule() until we're
5376 /* MTK patch: prevent could not migrate RT task when RT throttle*/
5377 unthrottle_offline_rt_rqs(rq
);
5381 * There's this thread running, bail when that's the only
5384 if (rq
->nr_running
== 1)
5387 next
= pick_next_task(rq
);
5389 next
->sched_class
->put_prev_task(rq
, next
);
5391 /* Find suitable destination for @next, with force if needed. */
5392 dest_cpu
= select_fallback_rq(dead_cpu
, next
);
5393 raw_spin_unlock(&rq
->lock
);
5395 __migrate_task(next
, dead_cpu
, dest_cpu
);
5397 raw_spin_lock(&rq
->lock
);
5403 #endif /* CONFIG_HOTPLUG_CPU */
5405 #if defined(CONFIG_SCHED_DEBUG) && defined(CONFIG_SYSCTL)
5407 static struct ctl_table sd_ctl_dir
[] = {
5409 .procname
= "sched_domain",
5415 static struct ctl_table sd_ctl_root
[] = {
5417 .procname
= "kernel",
5419 .child
= sd_ctl_dir
,
5424 static struct ctl_table
*sd_alloc_ctl_entry(int n
)
5426 struct ctl_table
*entry
=
5427 kcalloc(n
, sizeof(struct ctl_table
), GFP_KERNEL
);
5432 static void sd_free_ctl_entry(struct ctl_table
**tablep
)
5434 struct ctl_table
*entry
;
5437 * In the intermediate directories, both the child directory and
5438 * procname are dynamically allocated and could fail but the mode
5439 * will always be set. In the lowest directory the names are
5440 * static strings and all have proc handlers.
5442 for (entry
= *tablep
; entry
->mode
; entry
++) {
5444 sd_free_ctl_entry(&entry
->child
);
5445 if (entry
->proc_handler
== NULL
)
5446 kfree(entry
->procname
);
5453 static int min_load_idx
= 0;
5454 static int max_load_idx
= CPU_LOAD_IDX_MAX
-1;
5457 set_table_entry(struct ctl_table
*entry
,
5458 const char *procname
, void *data
, int maxlen
,
5459 umode_t mode
, proc_handler
*proc_handler
,
5462 entry
->procname
= procname
;
5464 entry
->maxlen
= maxlen
;
5466 entry
->proc_handler
= proc_handler
;
5469 entry
->extra1
= &min_load_idx
;
5470 entry
->extra2
= &max_load_idx
;
5474 static struct ctl_table
*
5475 sd_alloc_ctl_domain_table(struct sched_domain
*sd
)
5477 struct ctl_table
*table
= sd_alloc_ctl_entry(13);
5482 set_table_entry(&table
[0], "min_interval", &sd
->min_interval
,
5483 sizeof(long), 0644, proc_doulongvec_minmax
, false);
5484 set_table_entry(&table
[1], "max_interval", &sd
->max_interval
,
5485 sizeof(long), 0644, proc_doulongvec_minmax
, false);
5486 set_table_entry(&table
[2], "busy_idx", &sd
->busy_idx
,
5487 sizeof(int), 0644, proc_dointvec_minmax
, true);
5488 set_table_entry(&table
[3], "idle_idx", &sd
->idle_idx
,
5489 sizeof(int), 0644, proc_dointvec_minmax
, true);
5490 set_table_entry(&table
[4], "newidle_idx", &sd
->newidle_idx
,
5491 sizeof(int), 0644, proc_dointvec_minmax
, true);
5492 set_table_entry(&table
[5], "wake_idx", &sd
->wake_idx
,
5493 sizeof(int), 0644, proc_dointvec_minmax
, true);
5494 set_table_entry(&table
[6], "forkexec_idx", &sd
->forkexec_idx
,
5495 sizeof(int), 0644, proc_dointvec_minmax
, true);
5496 set_table_entry(&table
[7], "busy_factor", &sd
->busy_factor
,
5497 sizeof(int), 0644, proc_dointvec_minmax
, false);
5498 set_table_entry(&table
[8], "imbalance_pct", &sd
->imbalance_pct
,
5499 sizeof(int), 0644, proc_dointvec_minmax
, false);
5500 set_table_entry(&table
[9], "cache_nice_tries",
5501 &sd
->cache_nice_tries
,
5502 sizeof(int), 0644, proc_dointvec_minmax
, false);
5503 set_table_entry(&table
[10], "flags", &sd
->flags
,
5504 sizeof(int), 0644, proc_dointvec_minmax
, false);
5505 set_table_entry(&table
[11], "name", sd
->name
,
5506 CORENAME_MAX_SIZE
, 0444, proc_dostring
, false);
5507 /* &table[12] is terminator */
5512 static ctl_table
*sd_alloc_ctl_cpu_table(int cpu
)
5514 struct ctl_table
*entry
, *table
;
5515 struct sched_domain
*sd
;
5516 int domain_num
= 0, i
;
5519 for_each_domain(cpu
, sd
)
5521 entry
= table
= sd_alloc_ctl_entry(domain_num
+ 1);
5526 for_each_domain(cpu
, sd
) {
5527 snprintf(buf
, 32, "domain%d", i
);
5528 entry
->procname
= kstrdup(buf
, GFP_KERNEL
);
5530 entry
->child
= sd_alloc_ctl_domain_table(sd
);
5537 static struct ctl_table_header
*sd_sysctl_header
;
5538 static void register_sched_domain_sysctl(void)
5540 int i
, cpu_num
= num_possible_cpus();
5541 struct ctl_table
*entry
= sd_alloc_ctl_entry(cpu_num
+ 1);
5544 WARN_ON(sd_ctl_dir
[0].child
);
5545 sd_ctl_dir
[0].child
= entry
;
5550 for_each_possible_cpu(i
) {
5551 snprintf(buf
, 32, "cpu%d", i
);
5552 entry
->procname
= kstrdup(buf
, GFP_KERNEL
);
5554 entry
->child
= sd_alloc_ctl_cpu_table(i
);
5558 WARN_ON(sd_sysctl_header
);
5559 sd_sysctl_header
= register_sysctl_table(sd_ctl_root
);
5562 /* may be called multiple times per register */
5563 static void unregister_sched_domain_sysctl(void)
5565 if (sd_sysctl_header
)
5566 unregister_sysctl_table(sd_sysctl_header
);
5567 sd_sysctl_header
= NULL
;
5568 if (sd_ctl_dir
[0].child
)
5569 sd_free_ctl_entry(&sd_ctl_dir
[0].child
);
5572 static void register_sched_domain_sysctl(void)
5575 static void unregister_sched_domain_sysctl(void)
5580 static void set_rq_online(struct rq
*rq
)
5583 const struct sched_class
*class;
5585 cpumask_set_cpu(rq
->cpu
, rq
->rd
->online
);
5588 for_each_class(class) {
5589 if (class->rq_online
)
5590 class->rq_online(rq
);
5595 static void set_rq_offline(struct rq
*rq
)
5598 const struct sched_class
*class;
5600 for_each_class(class) {
5601 if (class->rq_offline
)
5602 class->rq_offline(rq
);
5605 cpumask_clear_cpu(rq
->cpu
, rq
->rd
->online
);
5611 * migration_call - callback that gets triggered when a CPU is added.
5612 * Here we can start up the necessary migration thread for the new CPU.
5614 static int __cpuinit
5615 migration_call(struct notifier_block
*nfb
, unsigned long action
, void *hcpu
)
5617 int cpu
= (long)hcpu
;
5618 unsigned long flags
;
5619 struct rq
*rq
= cpu_rq(cpu
);
5621 switch (action
& ~CPU_TASKS_FROZEN
) {
5623 case CPU_UP_PREPARE
:
5624 rq
->calc_load_update
= calc_load_update
;
5628 /* Update our root-domain */
5629 raw_spin_lock_irqsave(&rq
->lock
, flags
);
5631 BUG_ON(!cpumask_test_cpu(cpu
, rq
->rd
->span
));
5634 raw_spin_unlock_irqrestore(&rq
->lock
, flags
);
5637 #ifdef CONFIG_HOTPLUG_CPU
5639 sched_ttwu_pending();
5640 /* Update our root-domain */
5641 raw_spin_lock_irqsave(&rq
->lock
, flags
);
5643 BUG_ON(!cpumask_test_cpu(cpu
, rq
->rd
->span
));
5647 BUG_ON(rq
->nr_running
!= 1); /* the migration thread */
5648 raw_spin_unlock_irqrestore(&rq
->lock
, flags
);
5652 calc_load_migrate(rq
);
5657 update_max_interval();
5663 * Register at high priority so that task migration (migrate_all_tasks)
5664 * happens before everything else. This has to be lower priority than
5665 * the notifier in the perf_event subsystem, though.
5667 static struct notifier_block __cpuinitdata migration_notifier
= {
5668 .notifier_call
= migration_call
,
5669 .priority
= CPU_PRI_MIGRATION
,
5672 static int __cpuinit
sched_cpu_active(struct notifier_block
*nfb
,
5673 unsigned long action
, void *hcpu
)
5675 switch (action
& ~CPU_TASKS_FROZEN
) {
5676 case CPU_DOWN_FAILED
:
5677 set_cpu_active((long)hcpu
, true);
5684 static int __cpuinit
sched_cpu_inactive(struct notifier_block
*nfb
,
5685 unsigned long action
, void *hcpu
)
5687 switch (action
& ~CPU_TASKS_FROZEN
) {
5688 case CPU_DOWN_PREPARE
:
5689 set_cpu_active((long)hcpu
, false);
5696 static int __init
migration_init(void)
5698 void *cpu
= (void *)(long)smp_processor_id();
5701 /* Initialize migration for the boot CPU */
5702 err
= migration_call(&migration_notifier
, CPU_UP_PREPARE
, cpu
);
5703 BUG_ON(err
== NOTIFY_BAD
);
5704 migration_call(&migration_notifier
, CPU_ONLINE
, cpu
);
5705 register_cpu_notifier(&migration_notifier
);
5707 /* Register cpu active notifiers */
5708 cpu_notifier(sched_cpu_active
, CPU_PRI_SCHED_ACTIVE
);
5709 cpu_notifier(sched_cpu_inactive
, CPU_PRI_SCHED_INACTIVE
);
5713 early_initcall(migration_init
);
5718 static cpumask_var_t sched_domains_tmpmask
; /* sched_domains_mutex */
5720 #ifdef CONFIG_SCHED_DEBUG
5722 static __read_mostly
int sched_debug_enabled
;
5724 static int __init
sched_debug_setup(char *str
)
5726 sched_debug_enabled
= 1;
5730 early_param("sched_debug", sched_debug_setup
);
5732 static inline bool sched_debug(void)
5734 return sched_debug_enabled
;
5737 static int sched_domain_debug_one(struct sched_domain
*sd
, int cpu
, int level
,
5738 struct cpumask
*groupmask
)
5740 struct sched_group
*group
= sd
->groups
;
5743 cpulist_scnprintf(str
, sizeof(str
), sched_domain_span(sd
));
5744 cpumask_clear(groupmask
);
5746 printk(KERN_DEBUG
"%*s domain %d: ", level
, "", level
);
5748 if (!(sd
->flags
& SD_LOAD_BALANCE
)) {
5749 printk("does not load-balance\n");
5751 printk(KERN_ERR
"ERROR: !SD_LOAD_BALANCE domain"
5756 printk(KERN_CONT
"span %s level %s\n", str
, sd
->name
);
5758 if (!cpumask_test_cpu(cpu
, sched_domain_span(sd
))) {
5759 printk(KERN_ERR
"ERROR: domain->span does not contain "
5762 if (!cpumask_test_cpu(cpu
, sched_group_cpus(group
))) {
5763 printk(KERN_ERR
"ERROR: domain->groups does not contain"
5767 printk(KERN_DEBUG
"%*s groups:", level
+ 1, "");
5771 printk(KERN_ERR
"ERROR: group is NULL\n");
5776 * Even though we initialize ->power to something semi-sane,
5777 * we leave power_orig unset. This allows us to detect if
5778 * domain iteration is still funny without causing /0 traps.
5780 if (!group
->sgp
->power_orig
) {
5781 printk(KERN_CONT
"\n");
5782 printk(KERN_ERR
"ERROR: domain->cpu_power not "
5787 if (!cpumask_weight(sched_group_cpus(group
))) {
5788 printk(KERN_CONT
"\n");
5789 printk(KERN_ERR
"ERROR: empty group\n");
5793 if (!(sd
->flags
& SD_OVERLAP
) &&
5794 cpumask_intersects(groupmask
, sched_group_cpus(group
))) {
5795 printk(KERN_CONT
"\n");
5796 printk(KERN_ERR
"ERROR: repeated CPUs\n");
5800 cpumask_or(groupmask
, groupmask
, sched_group_cpus(group
));
5802 cpulist_scnprintf(str
, sizeof(str
), sched_group_cpus(group
));
5804 printk(KERN_CONT
" %s", str
);
5805 if (group
->sgp
->power
!= SCHED_POWER_SCALE
) {
5806 printk(KERN_CONT
" (cpu_power = %d)",
5810 group
= group
->next
;
5811 } while (group
!= sd
->groups
);
5812 printk(KERN_CONT
"\n");
5814 if (!cpumask_equal(sched_domain_span(sd
), groupmask
))
5815 printk(KERN_ERR
"ERROR: groups don't span domain->span\n");
5818 !cpumask_subset(groupmask
, sched_domain_span(sd
->parent
)))
5819 printk(KERN_ERR
"ERROR: parent span is not a superset "
5820 "of domain->span\n");
5824 static void sched_domain_debug(struct sched_domain
*sd
, int cpu
)
5828 if (!sched_debug_enabled
)
5832 printk(KERN_DEBUG
"CPU%d attaching NULL sched-domain.\n", cpu
);
5836 printk(KERN_DEBUG
"CPU%d attaching sched-domain:\n", cpu
);
5839 if (sched_domain_debug_one(sd
, cpu
, level
, sched_domains_tmpmask
))
5847 #else /* !CONFIG_SCHED_DEBUG */
5848 # define sched_domain_debug(sd, cpu) do { } while (0)
5849 static inline bool sched_debug(void)
5853 #endif /* CONFIG_SCHED_DEBUG */
5855 static int sd_degenerate(struct sched_domain
*sd
)
5857 if (cpumask_weight(sched_domain_span(sd
)) == 1)
5860 /* Following flags need at least 2 groups */
5861 if (sd
->flags
& (SD_LOAD_BALANCE
|
5862 SD_BALANCE_NEWIDLE
|
5866 SD_SHARE_PKG_RESOURCES
)) {
5867 if (sd
->groups
!= sd
->groups
->next
)
5871 /* Following flags don't use groups */
5872 if (sd
->flags
& (SD_WAKE_AFFINE
))
5879 sd_parent_degenerate(struct sched_domain
*sd
, struct sched_domain
*parent
)
5881 unsigned long cflags
= sd
->flags
, pflags
= parent
->flags
;
5883 if (sd_degenerate(parent
))
5886 if (!cpumask_equal(sched_domain_span(sd
), sched_domain_span(parent
)))
5889 /* Flags needing groups don't count if only 1 group in parent */
5890 if (parent
->groups
== parent
->groups
->next
) {
5891 pflags
&= ~(SD_LOAD_BALANCE
|
5892 SD_BALANCE_NEWIDLE
|
5896 SD_SHARE_PKG_RESOURCES
);
5897 if (nr_node_ids
== 1)
5898 pflags
&= ~SD_SERIALIZE
;
5900 if (~cflags
& pflags
)
5906 static void free_rootdomain(struct rcu_head
*rcu
)
5908 struct root_domain
*rd
= container_of(rcu
, struct root_domain
, rcu
);
5910 cpupri_cleanup(&rd
->cpupri
);
5911 free_cpumask_var(rd
->rto_mask
);
5912 free_cpumask_var(rd
->online
);
5913 free_cpumask_var(rd
->span
);
5917 static void rq_attach_root(struct rq
*rq
, struct root_domain
*rd
)
5919 struct root_domain
*old_rd
= NULL
;
5920 unsigned long flags
;
5922 raw_spin_lock_irqsave(&rq
->lock
, flags
);
5927 if (cpumask_test_cpu(rq
->cpu
, old_rd
->online
))
5930 cpumask_clear_cpu(rq
->cpu
, old_rd
->span
);
5933 * If we dont want to free the old_rt yet then
5934 * set old_rd to NULL to skip the freeing later
5937 if (!atomic_dec_and_test(&old_rd
->refcount
))
5941 atomic_inc(&rd
->refcount
);
5944 cpumask_set_cpu(rq
->cpu
, rd
->span
);
5945 if (cpumask_test_cpu(rq
->cpu
, cpu_active_mask
))
5948 raw_spin_unlock_irqrestore(&rq
->lock
, flags
);
5951 call_rcu_sched(&old_rd
->rcu
, free_rootdomain
);
5954 static int init_rootdomain(struct root_domain
*rd
)
5956 memset(rd
, 0, sizeof(*rd
));
5958 if (!alloc_cpumask_var(&rd
->span
, GFP_KERNEL
))
5960 if (!alloc_cpumask_var(&rd
->online
, GFP_KERNEL
))
5962 if (!alloc_cpumask_var(&rd
->rto_mask
, GFP_KERNEL
))
5965 if (cpupri_init(&rd
->cpupri
) != 0)
5970 free_cpumask_var(rd
->rto_mask
);
5972 free_cpumask_var(rd
->online
);
5974 free_cpumask_var(rd
->span
);
5980 * By default the system creates a single root-domain with all cpus as
5981 * members (mimicking the global state we have today).
5983 struct root_domain def_root_domain
;
5985 static void init_defrootdomain(void)
5987 init_rootdomain(&def_root_domain
);
5989 atomic_set(&def_root_domain
.refcount
, 1);
5992 static struct root_domain
*alloc_rootdomain(void)
5994 struct root_domain
*rd
;
5996 rd
= kmalloc(sizeof(*rd
), GFP_KERNEL
);
6000 if (init_rootdomain(rd
) != 0) {
6008 static void free_sched_groups(struct sched_group
*sg
, int free_sgp
)
6010 struct sched_group
*tmp
, *first
;
6019 if (free_sgp
&& atomic_dec_and_test(&sg
->sgp
->ref
))
6024 } while (sg
!= first
);
6027 static void free_sched_domain(struct rcu_head
*rcu
)
6029 struct sched_domain
*sd
= container_of(rcu
, struct sched_domain
, rcu
);
6032 * If its an overlapping domain it has private groups, iterate and
6035 if (sd
->flags
& SD_OVERLAP
) {
6036 free_sched_groups(sd
->groups
, 1);
6037 } else if (atomic_dec_and_test(&sd
->groups
->ref
)) {
6038 kfree(sd
->groups
->sgp
);
6044 static void destroy_sched_domain(struct sched_domain
*sd
, int cpu
)
6046 call_rcu(&sd
->rcu
, free_sched_domain
);
6049 static void destroy_sched_domains(struct sched_domain
*sd
, int cpu
)
6051 for (; sd
; sd
= sd
->parent
)
6052 destroy_sched_domain(sd
, cpu
);
6056 * Keep a special pointer to the highest sched_domain that has
6057 * SD_SHARE_PKG_RESOURCE set (Last Level Cache Domain) for this
6058 * allows us to avoid some pointer chasing select_idle_sibling().
6060 * Also keep a unique ID per domain (we use the first cpu number in
6061 * the cpumask of the domain), this allows us to quickly tell if
6062 * two cpus are in the same cache domain, see cpus_share_cache().
6064 DEFINE_PER_CPU(struct sched_domain
*, sd_llc
);
6065 DEFINE_PER_CPU(int, sd_llc_id
);
6067 static void update_top_cache_domain(int cpu
)
6069 struct sched_domain
*sd
;
6072 sd
= highest_flag_domain(cpu
, SD_SHARE_PKG_RESOURCES
);
6074 id
= cpumask_first(sched_domain_span(sd
));
6076 rcu_assign_pointer(per_cpu(sd_llc
, cpu
), sd
);
6077 per_cpu(sd_llc_id
, cpu
) = id
;
6081 * Attach the domain 'sd' to 'cpu' as its base domain. Callers must
6082 * hold the hotplug lock.
6085 cpu_attach_domain(struct sched_domain
*sd
, struct root_domain
*rd
, int cpu
)
6087 struct rq
*rq
= cpu_rq(cpu
);
6088 struct sched_domain
*tmp
;
6090 /* Remove the sched domains which do not contribute to scheduling. */
6091 for (tmp
= sd
; tmp
; ) {
6092 struct sched_domain
*parent
= tmp
->parent
;
6096 if (sd_parent_degenerate(tmp
, parent
)) {
6097 tmp
->parent
= parent
->parent
;
6099 parent
->parent
->child
= tmp
;
6100 destroy_sched_domain(parent
, cpu
);
6105 if (sd
&& sd_degenerate(sd
)) {
6108 destroy_sched_domain(tmp
, cpu
);
6113 sched_domain_debug(sd
, cpu
);
6115 rq_attach_root(rq
, rd
);
6117 rcu_assign_pointer(rq
->sd
, sd
);
6118 destroy_sched_domains(tmp
, cpu
);
6120 #if defined (CONFIG_MTK_SCHED_CMP_PACK_SMALL_TASK) || defined (CONFIG_HMP_PACK_SMALL_TASK)
6121 update_packing_domain(cpu
);
6122 #endif /* CONFIG_MTK_SCHED_CMP_PACK_SMALL_TASK || CONFIG_HMP_PACK_SMALL_TASK */
6123 update_top_cache_domain(cpu
);
6126 /* cpus with isolated domains */
6127 static cpumask_var_t cpu_isolated_map
;
6129 /* Setup the mask of cpus configured for isolated domains */
6130 static int __init
isolated_cpu_setup(char *str
)
6132 alloc_bootmem_cpumask_var(&cpu_isolated_map
);
6133 cpulist_parse(str
, cpu_isolated_map
);
6137 __setup("isolcpus=", isolated_cpu_setup
);
6139 static const struct cpumask
*cpu_cpu_mask(int cpu
)
6141 return cpumask_of_node(cpu_to_node(cpu
));
6145 struct sched_domain
**__percpu sd
;
6146 struct sched_group
**__percpu sg
;
6147 struct sched_group_power
**__percpu sgp
;
6151 struct sched_domain
** __percpu sd
;
6152 struct root_domain
*rd
;
6162 struct sched_domain_topology_level
;
6164 typedef struct sched_domain
*(*sched_domain_init_f
)(struct sched_domain_topology_level
*tl
, int cpu
);
6165 typedef const struct cpumask
*(*sched_domain_mask_f
)(int cpu
);
6167 #define SDTL_OVERLAP 0x01
6169 struct sched_domain_topology_level
{
6170 sched_domain_init_f init
;
6171 sched_domain_mask_f mask
;
6174 struct sd_data data
;
6178 * Build an iteration mask that can exclude certain CPUs from the upwards
6181 * Asymmetric node setups can result in situations where the domain tree is of
6182 * unequal depth, make sure to skip domains that already cover the entire
6185 * In that case build_sched_domains() will have terminated the iteration early
6186 * and our sibling sd spans will be empty. Domains should always include the
6187 * cpu they're built on, so check that.
6190 static void build_group_mask(struct sched_domain
*sd
, struct sched_group
*sg
)
6192 const struct cpumask
*span
= sched_domain_span(sd
);
6193 struct sd_data
*sdd
= sd
->private;
6194 struct sched_domain
*sibling
;
6197 for_each_cpu(i
, span
) {
6198 sibling
= *per_cpu_ptr(sdd
->sd
, i
);
6199 if (!cpumask_test_cpu(i
, sched_domain_span(sibling
)))
6202 cpumask_set_cpu(i
, sched_group_mask(sg
));
6207 * Return the canonical balance cpu for this group, this is the first cpu
6208 * of this group that's also in the iteration mask.
6210 int group_balance_cpu(struct sched_group
*sg
)
6212 return cpumask_first_and(sched_group_cpus(sg
), sched_group_mask(sg
));
6216 build_overlap_sched_groups(struct sched_domain
*sd
, int cpu
)
6218 struct sched_group
*first
= NULL
, *last
= NULL
, *groups
= NULL
, *sg
;
6219 const struct cpumask
*span
= sched_domain_span(sd
);
6220 struct cpumask
*covered
= sched_domains_tmpmask
;
6221 struct sd_data
*sdd
= sd
->private;
6222 struct sched_domain
*child
;
6225 cpumask_clear(covered
);
6227 for_each_cpu(i
, span
) {
6228 struct cpumask
*sg_span
;
6230 if (cpumask_test_cpu(i
, covered
))
6233 child
= *per_cpu_ptr(sdd
->sd
, i
);
6235 /* See the comment near build_group_mask(). */
6236 if (!cpumask_test_cpu(i
, sched_domain_span(child
)))
6239 sg
= kzalloc_node(sizeof(struct sched_group
) + cpumask_size(),
6240 GFP_KERNEL
, cpu_to_node(cpu
));
6245 sg_span
= sched_group_cpus(sg
);
6247 child
= child
->child
;
6248 cpumask_copy(sg_span
, sched_domain_span(child
));
6250 cpumask_set_cpu(i
, sg_span
);
6252 cpumask_or(covered
, covered
, sg_span
);
6254 sg
->sgp
= *per_cpu_ptr(sdd
->sgp
, i
);
6255 if (atomic_inc_return(&sg
->sgp
->ref
) == 1)
6256 build_group_mask(sd
, sg
);
6259 * Initialize sgp->power such that even if we mess up the
6260 * domains and no possible iteration will get us here, we won't
6263 sg
->sgp
->power
= SCHED_POWER_SCALE
* cpumask_weight(sg_span
);
6266 * Make sure the first group of this domain contains the
6267 * canonical balance cpu. Otherwise the sched_domain iteration
6268 * breaks. See update_sg_lb_stats().
6270 if ((!groups
&& cpumask_test_cpu(cpu
, sg_span
)) ||
6271 group_balance_cpu(sg
) == cpu
)
6281 sd
->groups
= groups
;
6286 free_sched_groups(first
, 0);
6291 static int get_group(int cpu
, struct sd_data
*sdd
, struct sched_group
**sg
)
6293 struct sched_domain
*sd
= *per_cpu_ptr(sdd
->sd
, cpu
);
6294 struct sched_domain
*child
= sd
->child
;
6297 cpu
= cpumask_first(sched_domain_span(child
));
6300 *sg
= *per_cpu_ptr(sdd
->sg
, cpu
);
6301 (*sg
)->sgp
= *per_cpu_ptr(sdd
->sgp
, cpu
);
6302 atomic_set(&(*sg
)->sgp
->ref
, 1); /* for claim_allocations */
6309 * build_sched_groups will build a circular linked list of the groups
6310 * covered by the given span, and will set each group's ->cpumask correctly,
6311 * and ->cpu_power to 0.
6313 * Assumes the sched_domain tree is fully constructed
6316 build_sched_groups(struct sched_domain
*sd
, int cpu
)
6318 struct sched_group
*first
= NULL
, *last
= NULL
;
6319 struct sd_data
*sdd
= sd
->private;
6320 const struct cpumask
*span
= sched_domain_span(sd
);
6321 struct cpumask
*covered
;
6324 get_group(cpu
, sdd
, &sd
->groups
);
6325 atomic_inc(&sd
->groups
->ref
);
6327 if (cpu
!= cpumask_first(sched_domain_span(sd
)))
6330 lockdep_assert_held(&sched_domains_mutex
);
6331 covered
= sched_domains_tmpmask
;
6333 cpumask_clear(covered
);
6335 for_each_cpu(i
, span
) {
6336 struct sched_group
*sg
;
6337 int group
= get_group(i
, sdd
, &sg
);
6340 if (cpumask_test_cpu(i
, covered
))
6343 cpumask_clear(sched_group_cpus(sg
));
6345 cpumask_setall(sched_group_mask(sg
));
6347 for_each_cpu(j
, span
) {
6348 if (get_group(j
, sdd
, NULL
) != group
)
6351 cpumask_set_cpu(j
, covered
);
6352 cpumask_set_cpu(j
, sched_group_cpus(sg
));
6367 * Initialize sched groups cpu_power.
6369 * cpu_power indicates the capacity of sched group, which is used while
6370 * distributing the load between different sched groups in a sched domain.
6371 * Typically cpu_power for all the groups in a sched domain will be same unless
6372 * there are asymmetries in the topology. If there are asymmetries, group
6373 * having more cpu_power will pickup more load compared to the group having
6376 static void init_sched_groups_power(int cpu
, struct sched_domain
*sd
)
6378 struct sched_group
*sg
= sd
->groups
;
6380 WARN_ON(!sd
|| !sg
);
6383 sg
->group_weight
= cpumask_weight(sched_group_cpus(sg
));
6385 } while (sg
!= sd
->groups
);
6387 if (cpu
!= group_balance_cpu(sg
))
6390 update_group_power(sd
, cpu
);
6391 atomic_set(&sg
->sgp
->nr_busy_cpus
, sg
->group_weight
);
6394 int __weak
arch_sd_sibling_asym_packing(void)
6396 return 0*SD_ASYM_PACKING
;
6399 #if defined (CONFIG_MTK_SCHED_CMP_PACK_SMALL_TASK) || defined (CONFIG_HMP_PACK_SMALL_TASK)
6400 int __weak
arch_sd_share_power_line(void)
6402 return 0*SD_SHARE_POWERLINE
;
6404 #endif /* CONFIG_MTK_SCHED_CMP_PACK_SMALL_TASK || CONFIG_HMP_PACK_SMALL_TASK */
6406 * Initializers for schedule domains
6407 * Non-inlined to reduce accumulated stack pressure in build_sched_domains()
6410 #ifdef CONFIG_SCHED_DEBUG
6411 # define SD_INIT_NAME(sd, type) sd->name = #type
6413 # define SD_INIT_NAME(sd, type) do { } while (0)
6416 #define SD_INIT_FUNC(type) \
6417 static noinline struct sched_domain * \
6418 sd_init_##type(struct sched_domain_topology_level *tl, int cpu) \
6420 struct sched_domain *sd = *per_cpu_ptr(tl->data.sd, cpu); \
6421 *sd = SD_##type##_INIT; \
6422 SD_INIT_NAME(sd, type); \
6423 sd->private = &tl->data; \
6428 #ifdef CONFIG_SCHED_SMT
6429 SD_INIT_FUNC(SIBLING
)
6431 #ifdef CONFIG_SCHED_MC
6434 #ifdef CONFIG_SCHED_BOOK
6438 static int default_relax_domain_level
= -1;
6439 int sched_domain_level_max
;
6441 static int __init
setup_relax_domain_level(char *str
)
6443 if (kstrtoint(str
, 0, &default_relax_domain_level
))
6444 pr_warn("Unable to set relax_domain_level\n");
6448 __setup("relax_domain_level=", setup_relax_domain_level
);
6450 static void set_domain_attribute(struct sched_domain
*sd
,
6451 struct sched_domain_attr
*attr
)
6455 if (!attr
|| attr
->relax_domain_level
< 0) {
6456 if (default_relax_domain_level
< 0)
6459 request
= default_relax_domain_level
;
6461 request
= attr
->relax_domain_level
;
6462 if (request
< sd
->level
) {
6463 /* turn off idle balance on this domain */
6464 sd
->flags
&= ~(SD_BALANCE_WAKE
|SD_BALANCE_NEWIDLE
);
6466 /* turn on idle balance on this domain */
6467 sd
->flags
|= (SD_BALANCE_WAKE
|SD_BALANCE_NEWIDLE
);
6471 static void __sdt_free(const struct cpumask
*cpu_map
);
6472 static int __sdt_alloc(const struct cpumask
*cpu_map
);
6474 static void __free_domain_allocs(struct s_data
*d
, enum s_alloc what
,
6475 const struct cpumask
*cpu_map
)
6479 if (!atomic_read(&d
->rd
->refcount
))
6480 free_rootdomain(&d
->rd
->rcu
); /* fall through */
6482 free_percpu(d
->sd
); /* fall through */
6484 __sdt_free(cpu_map
); /* fall through */
6490 static enum s_alloc
__visit_domain_allocation_hell(struct s_data
*d
,
6491 const struct cpumask
*cpu_map
)
6493 memset(d
, 0, sizeof(*d
));
6495 if (__sdt_alloc(cpu_map
))
6496 return sa_sd_storage
;
6497 d
->sd
= alloc_percpu(struct sched_domain
*);
6499 return sa_sd_storage
;
6500 d
->rd
= alloc_rootdomain();
6503 return sa_rootdomain
;
6507 * NULL the sd_data elements we've used to build the sched_domain and
6508 * sched_group structure so that the subsequent __free_domain_allocs()
6509 * will not free the data we're using.
6511 static void claim_allocations(int cpu
, struct sched_domain
*sd
)
6513 struct sd_data
*sdd
= sd
->private;
6515 WARN_ON_ONCE(*per_cpu_ptr(sdd
->sd
, cpu
) != sd
);
6516 *per_cpu_ptr(sdd
->sd
, cpu
) = NULL
;
6518 if (atomic_read(&(*per_cpu_ptr(sdd
->sg
, cpu
))->ref
))
6519 *per_cpu_ptr(sdd
->sg
, cpu
) = NULL
;
6521 if (atomic_read(&(*per_cpu_ptr(sdd
->sgp
, cpu
))->ref
))
6522 *per_cpu_ptr(sdd
->sgp
, cpu
) = NULL
;
6525 #ifdef CONFIG_SCHED_SMT
6526 static const struct cpumask
*cpu_smt_mask(int cpu
)
6528 return topology_thread_cpumask(cpu
);
6533 * Topology list, bottom-up.
6535 static struct sched_domain_topology_level default_topology
[] = {
6536 #ifdef CONFIG_SCHED_SMT
6537 { sd_init_SIBLING
, cpu_smt_mask
, },
6539 #ifdef CONFIG_SCHED_MC
6540 { sd_init_MC
, cpu_coregroup_mask
, },
6542 #ifdef CONFIG_SCHED_BOOK
6543 { sd_init_BOOK
, cpu_book_mask
, },
6545 { sd_init_CPU
, cpu_cpu_mask
, },
6549 static struct sched_domain_topology_level
*sched_domain_topology
= default_topology
;
6553 static int sched_domains_numa_levels
;
6554 static int *sched_domains_numa_distance
;
6555 static struct cpumask
***sched_domains_numa_masks
;
6556 static int sched_domains_curr_level
;
6558 static inline int sd_local_flags(int level
)
6560 if (sched_domains_numa_distance
[level
] > RECLAIM_DISTANCE
)
6563 return SD_BALANCE_EXEC
| SD_BALANCE_FORK
| SD_WAKE_AFFINE
;
6566 static struct sched_domain
*
6567 sd_numa_init(struct sched_domain_topology_level
*tl
, int cpu
)
6569 struct sched_domain
*sd
= *per_cpu_ptr(tl
->data
.sd
, cpu
);
6570 int level
= tl
->numa_level
;
6571 int sd_weight
= cpumask_weight(
6572 sched_domains_numa_masks
[level
][cpu_to_node(cpu
)]);
6574 *sd
= (struct sched_domain
){
6575 .min_interval
= sd_weight
,
6576 .max_interval
= 2*sd_weight
,
6578 .imbalance_pct
= 125,
6579 .cache_nice_tries
= 2,
6586 .flags
= 1*SD_LOAD_BALANCE
6587 | 1*SD_BALANCE_NEWIDLE
6592 | 0*SD_SHARE_CPUPOWER
6593 | 0*SD_SHARE_PKG_RESOURCES
6595 | 0*SD_PREFER_SIBLING
6596 | sd_local_flags(level
)
6598 .last_balance
= jiffies
,
6599 .balance_interval
= sd_weight
,
6601 SD_INIT_NAME(sd
, NUMA
);
6602 sd
->private = &tl
->data
;
6605 * Ugly hack to pass state to sd_numa_mask()...
6607 sched_domains_curr_level
= tl
->numa_level
;
6612 static const struct cpumask
*sd_numa_mask(int cpu
)
6614 return sched_domains_numa_masks
[sched_domains_curr_level
][cpu_to_node(cpu
)];
6617 static void sched_numa_warn(const char *str
)
6619 static int done
= false;
6627 printk(KERN_WARNING
"ERROR: %s\n\n", str
);
6629 for (i
= 0; i
< nr_node_ids
; i
++) {
6630 printk(KERN_WARNING
" ");
6631 for (j
= 0; j
< nr_node_ids
; j
++)
6632 printk(KERN_CONT
"%02d ", node_distance(i
,j
));
6633 printk(KERN_CONT
"\n");
6635 printk(KERN_WARNING
"\n");
6638 static bool find_numa_distance(int distance
)
6642 if (distance
== node_distance(0, 0))
6645 for (i
= 0; i
< sched_domains_numa_levels
; i
++) {
6646 if (sched_domains_numa_distance
[i
] == distance
)
6653 static void sched_init_numa(void)
6655 int next_distance
, curr_distance
= node_distance(0, 0);
6656 struct sched_domain_topology_level
*tl
;
6660 sched_domains_numa_distance
= kzalloc(sizeof(int) * nr_node_ids
, GFP_KERNEL
);
6661 if (!sched_domains_numa_distance
)
6665 * O(nr_nodes^2) deduplicating selection sort -- in order to find the
6666 * unique distances in the node_distance() table.
6668 * Assumes node_distance(0,j) includes all distances in
6669 * node_distance(i,j) in order to avoid cubic time.
6671 next_distance
= curr_distance
;
6672 for (i
= 0; i
< nr_node_ids
; i
++) {
6673 for (j
= 0; j
< nr_node_ids
; j
++) {
6674 for (k
= 0; k
< nr_node_ids
; k
++) {
6675 int distance
= node_distance(i
, k
);
6677 if (distance
> curr_distance
&&
6678 (distance
< next_distance
||
6679 next_distance
== curr_distance
))
6680 next_distance
= distance
;
6683 * While not a strong assumption it would be nice to know
6684 * about cases where if node A is connected to B, B is not
6685 * equally connected to A.
6687 if (sched_debug() && node_distance(k
, i
) != distance
)
6688 sched_numa_warn("Node-distance not symmetric");
6690 if (sched_debug() && i
&& !find_numa_distance(distance
))
6691 sched_numa_warn("Node-0 not representative");
6693 if (next_distance
!= curr_distance
) {
6694 sched_domains_numa_distance
[level
++] = next_distance
;
6695 sched_domains_numa_levels
= level
;
6696 curr_distance
= next_distance
;
6701 * In case of sched_debug() we verify the above assumption.
6707 * 'level' contains the number of unique distances, excluding the
6708 * identity distance node_distance(i,i).
6710 * The sched_domains_numa_distance[] array includes the actual distance
6715 * Here, we should temporarily reset sched_domains_numa_levels to 0.
6716 * If it fails to allocate memory for array sched_domains_numa_masks[][],
6717 * the array will contain less then 'level' members. This could be
6718 * dangerous when we use it to iterate array sched_domains_numa_masks[][]
6719 * in other functions.
6721 * We reset it to 'level' at the end of this function.
6723 sched_domains_numa_levels
= 0;
6725 sched_domains_numa_masks
= kzalloc(sizeof(void *) * level
, GFP_KERNEL
);
6726 if (!sched_domains_numa_masks
)
6730 * Now for each level, construct a mask per node which contains all
6731 * cpus of nodes that are that many hops away from us.
6733 for (i
= 0; i
< level
; i
++) {
6734 sched_domains_numa_masks
[i
] =
6735 kzalloc(nr_node_ids
* sizeof(void *), GFP_KERNEL
);
6736 if (!sched_domains_numa_masks
[i
])
6739 for (j
= 0; j
< nr_node_ids
; j
++) {
6740 struct cpumask
*mask
= kzalloc(cpumask_size(), GFP_KERNEL
);
6744 sched_domains_numa_masks
[i
][j
] = mask
;
6746 for (k
= 0; k
< nr_node_ids
; k
++) {
6747 if (node_distance(j
, k
) > sched_domains_numa_distance
[i
])
6750 cpumask_or(mask
, mask
, cpumask_of_node(k
));
6755 tl
= kzalloc((ARRAY_SIZE(default_topology
) + level
) *
6756 sizeof(struct sched_domain_topology_level
), GFP_KERNEL
);
6761 * Copy the default topology bits..
6763 for (i
= 0; default_topology
[i
].init
; i
++)
6764 tl
[i
] = default_topology
[i
];
6767 * .. and append 'j' levels of NUMA goodness.
6769 for (j
= 0; j
< level
; i
++, j
++) {
6770 tl
[i
] = (struct sched_domain_topology_level
){
6771 .init
= sd_numa_init
,
6772 .mask
= sd_numa_mask
,
6773 .flags
= SDTL_OVERLAP
,
6778 sched_domain_topology
= tl
;
6780 sched_domains_numa_levels
= level
;
6783 static void sched_domains_numa_masks_set(int cpu
)
6786 int node
= cpu_to_node(cpu
);
6788 for (i
= 0; i
< sched_domains_numa_levels
; i
++) {
6789 for (j
= 0; j
< nr_node_ids
; j
++) {
6790 if (node_distance(j
, node
) <= sched_domains_numa_distance
[i
])
6791 cpumask_set_cpu(cpu
, sched_domains_numa_masks
[i
][j
]);
6796 static void sched_domains_numa_masks_clear(int cpu
)
6799 for (i
= 0; i
< sched_domains_numa_levels
; i
++) {
6800 for (j
= 0; j
< nr_node_ids
; j
++)
6801 cpumask_clear_cpu(cpu
, sched_domains_numa_masks
[i
][j
]);
6806 * Update sched_domains_numa_masks[level][node] array when new cpus
6809 static int sched_domains_numa_masks_update(struct notifier_block
*nfb
,
6810 unsigned long action
,
6813 int cpu
= (long)hcpu
;
6815 switch (action
& ~CPU_TASKS_FROZEN
) {
6817 sched_domains_numa_masks_set(cpu
);
6821 sched_domains_numa_masks_clear(cpu
);
6831 static inline void sched_init_numa(void)
6835 static int sched_domains_numa_masks_update(struct notifier_block
*nfb
,
6836 unsigned long action
,
6841 #endif /* CONFIG_NUMA */
6843 static int __sdt_alloc(const struct cpumask
*cpu_map
)
6845 struct sched_domain_topology_level
*tl
;
6848 for (tl
= sched_domain_topology
; tl
->init
; tl
++) {
6849 struct sd_data
*sdd
= &tl
->data
;
6851 sdd
->sd
= alloc_percpu(struct sched_domain
*);
6855 sdd
->sg
= alloc_percpu(struct sched_group
*);
6859 sdd
->sgp
= alloc_percpu(struct sched_group_power
*);
6863 for_each_cpu(j
, cpu_map
) {
6864 struct sched_domain
*sd
;
6865 struct sched_group
*sg
;
6866 struct sched_group_power
*sgp
;
6868 sd
= kzalloc_node(sizeof(struct sched_domain
) + cpumask_size(),
6869 GFP_KERNEL
, cpu_to_node(j
));
6873 *per_cpu_ptr(sdd
->sd
, j
) = sd
;
6875 sg
= kzalloc_node(sizeof(struct sched_group
) + cpumask_size(),
6876 GFP_KERNEL
, cpu_to_node(j
));
6882 *per_cpu_ptr(sdd
->sg
, j
) = sg
;
6884 sgp
= kzalloc_node(sizeof(struct sched_group_power
) + cpumask_size(),
6885 GFP_KERNEL
, cpu_to_node(j
));
6889 *per_cpu_ptr(sdd
->sgp
, j
) = sgp
;
6896 static void __sdt_free(const struct cpumask
*cpu_map
)
6898 struct sched_domain_topology_level
*tl
;
6901 for (tl
= sched_domain_topology
; tl
->init
; tl
++) {
6902 struct sd_data
*sdd
= &tl
->data
;
6904 for_each_cpu(j
, cpu_map
) {
6905 struct sched_domain
*sd
;
6908 sd
= *per_cpu_ptr(sdd
->sd
, j
);
6909 if (sd
&& (sd
->flags
& SD_OVERLAP
))
6910 free_sched_groups(sd
->groups
, 0);
6911 kfree(*per_cpu_ptr(sdd
->sd
, j
));
6915 kfree(*per_cpu_ptr(sdd
->sg
, j
));
6917 kfree(*per_cpu_ptr(sdd
->sgp
, j
));
6919 free_percpu(sdd
->sd
);
6921 free_percpu(sdd
->sg
);
6923 free_percpu(sdd
->sgp
);
6928 struct sched_domain
*build_sched_domain(struct sched_domain_topology_level
*tl
,
6929 struct s_data
*d
, const struct cpumask
*cpu_map
,
6930 struct sched_domain_attr
*attr
, struct sched_domain
*child
,
6933 struct sched_domain
*sd
= tl
->init(tl
, cpu
);
6937 cpumask_and(sched_domain_span(sd
), cpu_map
, tl
->mask(cpu
));
6939 sd
->level
= child
->level
+ 1;
6940 sched_domain_level_max
= max(sched_domain_level_max
, sd
->level
);
6944 set_domain_attribute(sd
, attr
);
6950 * Build sched domains for a given set of cpus and attach the sched domains
6951 * to the individual cpus
6953 static int build_sched_domains(const struct cpumask
*cpu_map
,
6954 struct sched_domain_attr
*attr
)
6956 enum s_alloc alloc_state
= sa_none
;
6957 struct sched_domain
*sd
;
6959 int i
, ret
= -ENOMEM
;
6961 alloc_state
= __visit_domain_allocation_hell(&d
, cpu_map
);
6962 if (alloc_state
!= sa_rootdomain
)
6965 /* Set up domains for cpus specified by the cpu_map. */
6966 for_each_cpu(i
, cpu_map
) {
6967 struct sched_domain_topology_level
*tl
;
6970 for (tl
= sched_domain_topology
; tl
->init
; tl
++) {
6971 sd
= build_sched_domain(tl
, &d
, cpu_map
, attr
, sd
, i
);
6972 if (tl
->flags
& SDTL_OVERLAP
|| sched_feat(FORCE_SD_OVERLAP
))
6973 sd
->flags
|= SD_OVERLAP
;
6974 if (cpumask_equal(cpu_map
, sched_domain_span(sd
)))
6981 *per_cpu_ptr(d
.sd
, i
) = sd
;
6984 /* Build the groups for the domains */
6985 for_each_cpu(i
, cpu_map
) {
6986 for (sd
= *per_cpu_ptr(d
.sd
, i
); sd
; sd
= sd
->parent
) {
6987 sd
->span_weight
= cpumask_weight(sched_domain_span(sd
));
6988 if (sd
->flags
& SD_OVERLAP
) {
6989 if (build_overlap_sched_groups(sd
, i
))
6992 if (build_sched_groups(sd
, i
))
6998 /* Calculate CPU power for physical packages and nodes */
6999 for (i
= nr_cpumask_bits
-1; i
>= 0; i
--) {
7000 if (!cpumask_test_cpu(i
, cpu_map
))
7003 for (sd
= *per_cpu_ptr(d
.sd
, i
); sd
; sd
= sd
->parent
) {
7004 claim_allocations(i
, sd
);
7005 init_sched_groups_power(i
, sd
);
7009 /* Attach the domains */
7011 for_each_cpu(i
, cpu_map
) {
7012 sd
= *per_cpu_ptr(d
.sd
, i
);
7013 cpu_attach_domain(sd
, d
.rd
, i
);
7019 __free_domain_allocs(&d
, alloc_state
, cpu_map
);
7023 static cpumask_var_t
*doms_cur
; /* current sched domains */
7024 static int ndoms_cur
; /* number of sched domains in 'doms_cur' */
7025 static struct sched_domain_attr
*dattr_cur
;
7026 /* attribues of custom domains in 'doms_cur' */
7029 * Special case: If a kmalloc of a doms_cur partition (array of
7030 * cpumask) fails, then fallback to a single sched domain,
7031 * as determined by the single cpumask fallback_doms.
7033 static cpumask_var_t fallback_doms
;
7036 * arch_update_cpu_topology lets virtualized architectures update the
7037 * cpu core maps. It is supposed to return 1 if the topology changed
7038 * or 0 if it stayed the same.
7040 int __attribute__((weak
)) arch_update_cpu_topology(void)
7045 cpumask_var_t
*alloc_sched_domains(unsigned int ndoms
)
7048 cpumask_var_t
*doms
;
7050 doms
= kmalloc(sizeof(*doms
) * ndoms
, GFP_KERNEL
);
7053 for (i
= 0; i
< ndoms
; i
++) {
7054 if (!alloc_cpumask_var(&doms
[i
], GFP_KERNEL
)) {
7055 free_sched_domains(doms
, i
);
7062 void free_sched_domains(cpumask_var_t doms
[], unsigned int ndoms
)
7065 for (i
= 0; i
< ndoms
; i
++)
7066 free_cpumask_var(doms
[i
]);
7071 * Set up scheduler domains and groups. Callers must hold the hotplug lock.
7072 * For now this just excludes isolated cpus, but could be used to
7073 * exclude other special cases in the future.
7075 static int init_sched_domains(const struct cpumask
*cpu_map
)
7079 arch_update_cpu_topology();
7081 doms_cur
= alloc_sched_domains(ndoms_cur
);
7083 doms_cur
= &fallback_doms
;
7084 cpumask_andnot(doms_cur
[0], cpu_map
, cpu_isolated_map
);
7085 err
= build_sched_domains(doms_cur
[0], NULL
);
7086 register_sched_domain_sysctl();
7092 * Detach sched domains from a group of cpus specified in cpu_map
7093 * These cpus will now be attached to the NULL domain
7095 static void detach_destroy_domains(const struct cpumask
*cpu_map
)
7100 for_each_cpu(i
, cpu_map
)
7101 cpu_attach_domain(NULL
, &def_root_domain
, i
);
7105 /* handle null as "default" */
7106 static int dattrs_equal(struct sched_domain_attr
*cur
, int idx_cur
,
7107 struct sched_domain_attr
*new, int idx_new
)
7109 struct sched_domain_attr tmp
;
7116 return !memcmp(cur
? (cur
+ idx_cur
) : &tmp
,
7117 new ? (new + idx_new
) : &tmp
,
7118 sizeof(struct sched_domain_attr
));
7122 * Partition sched domains as specified by the 'ndoms_new'
7123 * cpumasks in the array doms_new[] of cpumasks. This compares
7124 * doms_new[] to the current sched domain partitioning, doms_cur[].
7125 * It destroys each deleted domain and builds each new domain.
7127 * 'doms_new' is an array of cpumask_var_t's of length 'ndoms_new'.
7128 * The masks don't intersect (don't overlap.) We should setup one
7129 * sched domain for each mask. CPUs not in any of the cpumasks will
7130 * not be load balanced. If the same cpumask appears both in the
7131 * current 'doms_cur' domains and in the new 'doms_new', we can leave
7134 * The passed in 'doms_new' should be allocated using
7135 * alloc_sched_domains. This routine takes ownership of it and will
7136 * free_sched_domains it when done with it. If the caller failed the
7137 * alloc call, then it can pass in doms_new == NULL && ndoms_new == 1,
7138 * and partition_sched_domains() will fallback to the single partition
7139 * 'fallback_doms', it also forces the domains to be rebuilt.
7141 * If doms_new == NULL it will be replaced with cpu_online_mask.
7142 * ndoms_new == 0 is a special case for destroying existing domains,
7143 * and it will not create the default domain.
7145 * Call with hotplug lock held
7147 void partition_sched_domains(int ndoms_new
, cpumask_var_t doms_new
[],
7148 struct sched_domain_attr
*dattr_new
)
7153 mutex_lock(&sched_domains_mutex
);
7155 /* always unregister in case we don't destroy any domains */
7156 unregister_sched_domain_sysctl();
7158 /* Let architecture update cpu core mappings. */
7159 new_topology
= arch_update_cpu_topology();
7161 n
= doms_new
? ndoms_new
: 0;
7163 /* Destroy deleted domains */
7164 for (i
= 0; i
< ndoms_cur
; i
++) {
7165 for (j
= 0; j
< n
&& !new_topology
; j
++) {
7166 if (cpumask_equal(doms_cur
[i
], doms_new
[j
])
7167 && dattrs_equal(dattr_cur
, i
, dattr_new
, j
))
7170 /* no match - a current sched domain not in new doms_new[] */
7171 detach_destroy_domains(doms_cur
[i
]);
7176 if (doms_new
== NULL
) {
7178 doms_new
= &fallback_doms
;
7179 cpumask_andnot(doms_new
[0], cpu_active_mask
, cpu_isolated_map
);
7180 WARN_ON_ONCE(dattr_new
);
7183 /* Build new domains */
7184 for (i
= 0; i
< ndoms_new
; i
++) {
7185 for (j
= 0; j
< ndoms_cur
&& !new_topology
; j
++) {
7186 if (cpumask_equal(doms_new
[i
], doms_cur
[j
])
7187 && dattrs_equal(dattr_new
, i
, dattr_cur
, j
))
7190 /* no match - add a new doms_new */
7191 build_sched_domains(doms_new
[i
], dattr_new
? dattr_new
+ i
: NULL
);
7196 /* Remember the new sched domains */
7197 if (doms_cur
!= &fallback_doms
)
7198 free_sched_domains(doms_cur
, ndoms_cur
);
7199 kfree(dattr_cur
); /* kfree(NULL) is safe */
7200 doms_cur
= doms_new
;
7201 dattr_cur
= dattr_new
;
7202 ndoms_cur
= ndoms_new
;
7204 register_sched_domain_sysctl();
7206 mutex_unlock(&sched_domains_mutex
);
7209 static int num_cpus_frozen
; /* used to mark begin/end of suspend/resume */
7212 * Update cpusets according to cpu_active mask. If cpusets are
7213 * disabled, cpuset_update_active_cpus() becomes a simple wrapper
7214 * around partition_sched_domains().
7216 * If we come here as part of a suspend/resume, don't touch cpusets because we
7217 * want to restore it back to its original state upon resume anyway.
7219 static int cpuset_cpu_active(struct notifier_block
*nfb
, unsigned long action
,
7223 case CPU_ONLINE_FROZEN
:
7224 case CPU_DOWN_FAILED_FROZEN
:
7227 * num_cpus_frozen tracks how many CPUs are involved in suspend
7228 * resume sequence. As long as this is not the last online
7229 * operation in the resume sequence, just build a single sched
7230 * domain, ignoring cpusets.
7233 if (likely(num_cpus_frozen
)) {
7234 partition_sched_domains(1, NULL
, NULL
);
7239 * This is the last CPU online operation. So fall through and
7240 * restore the original sched domains by considering the
7241 * cpuset configurations.
7245 case CPU_DOWN_FAILED
:
7246 cpuset_update_active_cpus(true);
7254 static int cpuset_cpu_inactive(struct notifier_block
*nfb
, unsigned long action
,
7258 case CPU_DOWN_PREPARE
:
7259 cpuset_update_active_cpus(false);
7261 case CPU_DOWN_PREPARE_FROZEN
:
7263 partition_sched_domains(1, NULL
, NULL
);
7271 void __init
sched_init_smp(void)
7273 cpumask_var_t non_isolated_cpus
;
7275 alloc_cpumask_var(&non_isolated_cpus
, GFP_KERNEL
);
7276 alloc_cpumask_var(&fallback_doms
, GFP_KERNEL
);
7281 mutex_lock(&sched_domains_mutex
);
7282 init_sched_domains(cpu_active_mask
);
7283 cpumask_andnot(non_isolated_cpus
, cpu_possible_mask
, cpu_isolated_map
);
7284 if (cpumask_empty(non_isolated_cpus
))
7285 cpumask_set_cpu(smp_processor_id(), non_isolated_cpus
);
7286 mutex_unlock(&sched_domains_mutex
);
7289 hotcpu_notifier(sched_domains_numa_masks_update
, CPU_PRI_SCHED_ACTIVE
);
7290 hotcpu_notifier(cpuset_cpu_active
, CPU_PRI_CPUSET_ACTIVE
);
7291 hotcpu_notifier(cpuset_cpu_inactive
, CPU_PRI_CPUSET_INACTIVE
);
7293 /* RT runtime code needs to handle some hotplug events */
7294 hotcpu_notifier(update_runtime
, 0);
7298 /* Move init over to a non-isolated CPU */
7299 if (set_cpus_allowed_ptr(current
, non_isolated_cpus
) < 0)
7301 sched_init_granularity();
7302 free_cpumask_var(non_isolated_cpus
);
7304 init_sched_rt_class();
7307 void __init
sched_init_smp(void)
7309 sched_init_granularity();
7311 #endif /* CONFIG_SMP */
7313 const_debug
unsigned int sysctl_timer_migration
= 1;
7315 int in_sched_functions(unsigned long addr
)
7317 return in_lock_functions(addr
) ||
7318 (addr
>= (unsigned long)__sched_text_start
7319 && addr
< (unsigned long)__sched_text_end
);
7322 #ifdef CONFIG_CGROUP_SCHED
7324 * Default task group.
7325 * Every task in system belongs to this group at bootup.
7327 struct task_group root_task_group
;
7328 LIST_HEAD(task_groups
);
7331 DECLARE_PER_CPU(cpumask_var_t
, load_balance_mask
);
7333 void __init
sched_init(void)
7336 unsigned long alloc_size
= 0, ptr
;
7338 #ifdef CONFIG_FAIR_GROUP_SCHED
7339 alloc_size
+= 2 * nr_cpu_ids
* sizeof(void **);
7341 #ifdef CONFIG_RT_GROUP_SCHED
7342 alloc_size
+= 2 * nr_cpu_ids
* sizeof(void **);
7344 #ifdef CONFIG_CPUMASK_OFFSTACK
7345 alloc_size
+= num_possible_cpus() * cpumask_size();
7348 ptr
= (unsigned long)kzalloc(alloc_size
, GFP_NOWAIT
);
7350 #ifdef CONFIG_FAIR_GROUP_SCHED
7351 root_task_group
.se
= (struct sched_entity
**)ptr
;
7352 ptr
+= nr_cpu_ids
* sizeof(void **);
7354 root_task_group
.cfs_rq
= (struct cfs_rq
**)ptr
;
7355 ptr
+= nr_cpu_ids
* sizeof(void **);
7357 #endif /* CONFIG_FAIR_GROUP_SCHED */
7358 #ifdef CONFIG_RT_GROUP_SCHED
7359 root_task_group
.rt_se
= (struct sched_rt_entity
**)ptr
;
7360 ptr
+= nr_cpu_ids
* sizeof(void **);
7362 root_task_group
.rt_rq
= (struct rt_rq
**)ptr
;
7363 ptr
+= nr_cpu_ids
* sizeof(void **);
7365 #endif /* CONFIG_RT_GROUP_SCHED */
7366 #ifdef CONFIG_CPUMASK_OFFSTACK
7367 for_each_possible_cpu(i
) {
7368 per_cpu(load_balance_mask
, i
) = (void *)ptr
;
7369 ptr
+= cpumask_size();
7371 #endif /* CONFIG_CPUMASK_OFFSTACK */
7375 init_defrootdomain();
7378 init_rt_bandwidth(&def_rt_bandwidth
,
7379 global_rt_period(), global_rt_runtime());
7381 #ifdef CONFIG_RT_GROUP_SCHED
7382 init_rt_bandwidth(&root_task_group
.rt_bandwidth
,
7383 global_rt_period(), global_rt_runtime());
7384 #endif /* CONFIG_RT_GROUP_SCHED */
7386 #ifdef CONFIG_CGROUP_SCHED
7387 list_add(&root_task_group
.list
, &task_groups
);
7388 INIT_LIST_HEAD(&root_task_group
.children
);
7389 INIT_LIST_HEAD(&root_task_group
.siblings
);
7390 autogroup_init(&init_task
);
7392 #endif /* CONFIG_CGROUP_SCHED */
7394 for_each_possible_cpu(i
) {
7398 raw_spin_lock_init(&rq
->lock
);
7400 rq
->calc_load_active
= 0;
7401 rq
->calc_load_update
= jiffies
+ LOAD_FREQ
;
7402 #ifdef CONFIG_PROVE_LOCKING
7405 init_cfs_rq(&rq
->cfs
);
7406 init_rt_rq(&rq
->rt
, rq
);
7407 #ifdef CONFIG_FAIR_GROUP_SCHED
7408 root_task_group
.shares
= ROOT_TASK_GROUP_LOAD
;
7409 INIT_LIST_HEAD(&rq
->leaf_cfs_rq_list
);
7411 * How much cpu bandwidth does root_task_group get?
7413 * In case of task-groups formed thr' the cgroup filesystem, it
7414 * gets 100% of the cpu resources in the system. This overall
7415 * system cpu resource is divided among the tasks of
7416 * root_task_group and its child task-groups in a fair manner,
7417 * based on each entity's (task or task-group's) weight
7418 * (se->load.weight).
7420 * In other words, if root_task_group has 10 tasks of weight
7421 * 1024) and two child groups A0 and A1 (of weight 1024 each),
7422 * then A0's share of the cpu resource is:
7424 * A0's bandwidth = 1024 / (10*1024 + 1024 + 1024) = 8.33%
7426 * We achieve this by letting root_task_group's tasks sit
7427 * directly in rq->cfs (i.e root_task_group->se[] = NULL).
7429 init_cfs_bandwidth(&root_task_group
.cfs_bandwidth
);
7430 init_tg_cfs_entry(&root_task_group
, &rq
->cfs
, NULL
, i
, NULL
);
7431 #endif /* CONFIG_FAIR_GROUP_SCHED */
7433 rq
->rt
.rt_runtime
= def_rt_bandwidth
.rt_runtime
;
7434 #ifdef CONFIG_RT_GROUP_SCHED
7435 INIT_LIST_HEAD(&rq
->leaf_rt_rq_list
);
7436 init_tg_rt_entry(&root_task_group
, &rq
->rt
, NULL
, i
, NULL
);
7439 for (j
= 0; j
< CPU_LOAD_IDX_MAX
; j
++)
7440 rq
->cpu_load
[j
] = 0;
7442 rq
->last_load_update_tick
= jiffies
;
7447 rq
->cpu_power
= SCHED_POWER_SCALE
;
7448 rq
->post_schedule
= 0;
7449 rq
->active_balance
= 0;
7450 rq
->next_balance
= jiffies
;
7455 rq
->avg_idle
= 2*sysctl_sched_migration_cost
;
7457 INIT_LIST_HEAD(&rq
->cfs_tasks
);
7459 rq_attach_root(rq
, &def_root_domain
);
7460 #ifdef CONFIG_NO_HZ_COMMON
7463 #ifdef CONFIG_NO_HZ_FULL
7464 rq
->last_sched_tick
= 0;
7468 atomic_set(&rq
->nr_iowait
, 0);
7471 set_load_weight(&init_task
);
7473 #ifdef CONFIG_PREEMPT_NOTIFIERS
7474 INIT_HLIST_HEAD(&init_task
.preempt_notifiers
);
7477 #ifdef CONFIG_RT_MUTEXES
7478 plist_head_init(&init_task
.pi_waiters
);
7482 * The boot idle thread does lazy MMU switching as well:
7484 atomic_inc(&init_mm
.mm_count
);
7485 enter_lazy_tlb(&init_mm
, current
);
7488 * Make us the idle thread. Technically, schedule() should not be
7489 * called from this thread, however somewhere below it might be,
7490 * but because we are the idle thread, we just pick up running again
7491 * when this runqueue becomes "idle".
7493 init_idle(current
, smp_processor_id());
7495 calc_load_update
= jiffies
+ LOAD_FREQ
;
7498 * During early bootup we pretend to be a normal task:
7500 current
->sched_class
= &fair_sched_class
;
7503 zalloc_cpumask_var(&sched_domains_tmpmask
, GFP_NOWAIT
);
7504 /* May be allocated at isolcpus cmdline parse time */
7505 if (cpu_isolated_map
== NULL
)
7506 zalloc_cpumask_var(&cpu_isolated_map
, GFP_NOWAIT
);
7507 idle_thread_set_boot_cpu();
7509 init_sched_fair_class();
7511 scheduler_running
= 1;
7514 #ifdef CONFIG_DEBUG_ATOMIC_SLEEP
7515 static inline int preempt_count_equals(int preempt_offset
)
7517 int nested
= (preempt_count() & ~PREEMPT_ACTIVE
) + rcu_preempt_depth();
7519 return (nested
== preempt_offset
);
7522 static int __might_sleep_init_called
;
7523 int __init
__might_sleep_init(void)
7525 __might_sleep_init_called
= 1;
7528 early_initcall(__might_sleep_init
);
7530 void __might_sleep(const char *file
, int line
, int preempt_offset
)
7532 static unsigned long prev_jiffy
; /* ratelimiting */
7534 rcu_sleep_check(); /* WARN_ON_ONCE() by default, no rate limit reqd. */
7535 if ((preempt_count_equals(preempt_offset
) && !irqs_disabled()) ||
7538 if (system_state
!= SYSTEM_RUNNING
&&
7539 (!__might_sleep_init_called
|| system_state
!= SYSTEM_BOOTING
))
7541 if (time_before(jiffies
, prev_jiffy
+ HZ
) && prev_jiffy
)
7543 prev_jiffy
= jiffies
;
7546 "BUG: sleeping function called from invalid context at %s:%d\n",
7549 "in_atomic(): %d, irqs_disabled(): %d, pid: %d, name: %s\n",
7550 in_atomic(), irqs_disabled(),
7551 current
->pid
, current
->comm
);
7553 debug_show_held_locks(current
);
7554 if (irqs_disabled())
7555 print_irqtrace_events(current
);
7558 EXPORT_SYMBOL(__might_sleep
);
7561 #ifdef CONFIG_MAGIC_SYSRQ
7562 static void normalize_task(struct rq
*rq
, struct task_struct
*p
)
7564 const struct sched_class
*prev_class
= p
->sched_class
;
7565 int old_prio
= p
->prio
;
7570 dequeue_task(rq
, p
, 0);
7571 __setscheduler(rq
, p
, SCHED_NORMAL
, 0);
7573 enqueue_task(rq
, p
, 0);
7574 resched_task(rq
->curr
);
7577 check_class_changed(rq
, p
, prev_class
, old_prio
);
7580 void normalize_rt_tasks(void)
7582 struct task_struct
*g
, *p
;
7583 unsigned long flags
;
7586 read_lock_irqsave(&tasklist_lock
, flags
);
7587 do_each_thread(g
, p
) {
7589 * Only normalize user tasks:
7594 p
->se
.exec_start
= 0;
7595 #ifdef CONFIG_SCHEDSTATS
7596 p
->se
.statistics
.wait_start
= 0;
7597 p
->se
.statistics
.sleep_start
= 0;
7598 p
->se
.statistics
.block_start
= 0;
7603 * Renice negative nice level userspace
7606 if (TASK_NICE(p
) < 0 && p
->mm
)
7607 set_user_nice(p
, 0);
7611 raw_spin_lock(&p
->pi_lock
);
7612 rq
= __task_rq_lock(p
);
7614 normalize_task(rq
, p
);
7616 __task_rq_unlock(rq
);
7617 raw_spin_unlock(&p
->pi_lock
);
7618 } while_each_thread(g
, p
);
7620 read_unlock_irqrestore(&tasklist_lock
, flags
);
7623 #endif /* CONFIG_MAGIC_SYSRQ */
7625 #if defined(CONFIG_IA64) || defined(CONFIG_KGDB_KDB)
7627 * These functions are only useful for the IA64 MCA handling, or kdb.
7629 * They can only be called when the whole system has been
7630 * stopped - every CPU needs to be quiescent, and no scheduling
7631 * activity can take place. Using them for anything else would
7632 * be a serious bug, and as a result, they aren't even visible
7633 * under any other configuration.
7637 * curr_task - return the current task for a given cpu.
7638 * @cpu: the processor in question.
7640 * ONLY VALID WHEN THE WHOLE SYSTEM IS STOPPED!
7642 struct task_struct
*curr_task(int cpu
)
7644 return cpu_curr(cpu
);
7647 #endif /* defined(CONFIG_IA64) || defined(CONFIG_KGDB_KDB) */
7651 * set_curr_task - set the current task for a given cpu.
7652 * @cpu: the processor in question.
7653 * @p: the task pointer to set.
7655 * Description: This function must only be used when non-maskable interrupts
7656 * are serviced on a separate stack. It allows the architecture to switch the
7657 * notion of the current task on a cpu in a non-blocking manner. This function
7658 * must be called with all CPU's synchronized, and interrupts disabled, the
7659 * and caller must save the original value of the current task (see
7660 * curr_task() above) and restore that value before reenabling interrupts and
7661 * re-starting the system.
7663 * ONLY VALID WHEN THE WHOLE SYSTEM IS STOPPED!
7665 void set_curr_task(int cpu
, struct task_struct
*p
)
7672 #ifdef CONFIG_CGROUP_SCHED
7673 /* task_group_lock serializes the addition/removal of task groups */
7674 static DEFINE_SPINLOCK(task_group_lock
);
7676 static void free_sched_group(struct task_group
*tg
)
7678 free_fair_sched_group(tg
);
7679 free_rt_sched_group(tg
);
7684 /* allocate runqueue etc for a new task group */
7685 struct task_group
*sched_create_group(struct task_group
*parent
)
7687 struct task_group
*tg
;
7689 tg
= kzalloc(sizeof(*tg
), GFP_KERNEL
);
7691 return ERR_PTR(-ENOMEM
);
7693 if (!alloc_fair_sched_group(tg
, parent
))
7696 if (!alloc_rt_sched_group(tg
, parent
))
7702 free_sched_group(tg
);
7703 return ERR_PTR(-ENOMEM
);
7706 void sched_online_group(struct task_group
*tg
, struct task_group
*parent
)
7708 unsigned long flags
;
7710 spin_lock_irqsave(&task_group_lock
, flags
);
7711 list_add_rcu(&tg
->list
, &task_groups
);
7713 WARN_ON(!parent
); /* root should already exist */
7715 tg
->parent
= parent
;
7716 INIT_LIST_HEAD(&tg
->children
);
7717 list_add_rcu(&tg
->siblings
, &parent
->children
);
7718 spin_unlock_irqrestore(&task_group_lock
, flags
);
7721 /* rcu callback to free various structures associated with a task group */
7722 static void free_sched_group_rcu(struct rcu_head
*rhp
)
7724 /* now it should be safe to free those cfs_rqs */
7725 free_sched_group(container_of(rhp
, struct task_group
, rcu
));
7728 /* Destroy runqueue etc associated with a task group */
7729 void sched_destroy_group(struct task_group
*tg
)
7731 /* wait for possible concurrent references to cfs_rqs complete */
7732 call_rcu(&tg
->rcu
, free_sched_group_rcu
);
7735 void sched_offline_group(struct task_group
*tg
)
7737 unsigned long flags
;
7740 /* end participation in shares distribution */
7741 for_each_possible_cpu(i
)
7742 unregister_fair_sched_group(tg
, i
);
7744 spin_lock_irqsave(&task_group_lock
, flags
);
7745 list_del_rcu(&tg
->list
);
7746 list_del_rcu(&tg
->siblings
);
7747 spin_unlock_irqrestore(&task_group_lock
, flags
);
7750 /* change task's runqueue when it moves between groups.
7751 * The caller of this function should have put the task in its new group
7752 * by now. This function just updates tsk->se.cfs_rq and tsk->se.parent to
7753 * reflect its new group.
7755 void sched_move_task(struct task_struct
*tsk
)
7757 struct task_group
*tg
;
7759 unsigned long flags
;
7762 rq
= task_rq_lock(tsk
, &flags
);
7764 running
= task_current(rq
, tsk
);
7768 dequeue_task(rq
, tsk
, 0);
7769 if (unlikely(running
))
7770 tsk
->sched_class
->put_prev_task(rq
, tsk
);
7772 tg
= container_of(task_subsys_state_check(tsk
, cpu_cgroup_subsys_id
,
7773 lockdep_is_held(&tsk
->sighand
->siglock
)),
7774 struct task_group
, css
);
7775 tg
= autogroup_task_group(tsk
, tg
);
7776 tsk
->sched_task_group
= tg
;
7778 #ifdef CONFIG_FAIR_GROUP_SCHED
7779 if (tsk
->sched_class
->task_move_group
)
7780 tsk
->sched_class
->task_move_group(tsk
, on_rq
);
7783 set_task_rq(tsk
, task_cpu(tsk
));
7785 if (unlikely(running
))
7786 tsk
->sched_class
->set_curr_task(rq
);
7788 enqueue_task(rq
, tsk
, 0);
7790 task_rq_unlock(rq
, tsk
, &flags
);
7792 #endif /* CONFIG_CGROUP_SCHED */
7794 #if defined(CONFIG_RT_GROUP_SCHED) || defined(CONFIG_CFS_BANDWIDTH)
7795 static unsigned long to_ratio(u64 period
, u64 runtime
)
7797 if (runtime
== RUNTIME_INF
)
7800 return div64_u64(runtime
<< 20, period
);
7804 #ifdef CONFIG_RT_GROUP_SCHED
7806 * Ensure that the real time constraints are schedulable.
7808 static DEFINE_MUTEX(rt_constraints_mutex
);
7810 /* Must be called with tasklist_lock held */
7811 static inline int tg_has_rt_tasks(struct task_group
*tg
)
7813 struct task_struct
*g
, *p
;
7815 do_each_thread(g
, p
) {
7816 if (rt_task(p
) && task_rq(p
)->rt
.tg
== tg
)
7818 } while_each_thread(g
, p
);
7823 struct rt_schedulable_data
{
7824 struct task_group
*tg
;
7829 static int tg_rt_schedulable(struct task_group
*tg
, void *data
)
7831 struct rt_schedulable_data
*d
= data
;
7832 struct task_group
*child
;
7833 unsigned long total
, sum
= 0;
7834 u64 period
, runtime
;
7836 period
= ktime_to_ns(tg
->rt_bandwidth
.rt_period
);
7837 runtime
= tg
->rt_bandwidth
.rt_runtime
;
7840 period
= d
->rt_period
;
7841 runtime
= d
->rt_runtime
;
7845 * Cannot have more runtime than the period.
7847 if (runtime
> period
&& runtime
!= RUNTIME_INF
)
7851 * Ensure we don't starve existing RT tasks.
7853 if (rt_bandwidth_enabled() && !runtime
&& tg_has_rt_tasks(tg
))
7856 total
= to_ratio(period
, runtime
);
7859 * Nobody can have more than the global setting allows.
7861 if (total
> to_ratio(global_rt_period(), global_rt_runtime()))
7865 * The sum of our children's runtime should not exceed our own.
7867 list_for_each_entry_rcu(child
, &tg
->children
, siblings
) {
7868 period
= ktime_to_ns(child
->rt_bandwidth
.rt_period
);
7869 runtime
= child
->rt_bandwidth
.rt_runtime
;
7871 if (child
== d
->tg
) {
7872 period
= d
->rt_period
;
7873 runtime
= d
->rt_runtime
;
7876 sum
+= to_ratio(period
, runtime
);
7885 static int __rt_schedulable(struct task_group
*tg
, u64 period
, u64 runtime
)
7889 struct rt_schedulable_data data
= {
7891 .rt_period
= period
,
7892 .rt_runtime
= runtime
,
7896 ret
= walk_tg_tree(tg_rt_schedulable
, tg_nop
, &data
);
7902 static int tg_set_rt_bandwidth(struct task_group
*tg
,
7903 u64 rt_period
, u64 rt_runtime
)
7907 mutex_lock(&rt_constraints_mutex
);
7908 read_lock(&tasklist_lock
);
7909 err
= __rt_schedulable(tg
, rt_period
, rt_runtime
);
7913 raw_spin_lock_irq(&tg
->rt_bandwidth
.rt_runtime_lock
);
7914 tg
->rt_bandwidth
.rt_period
= ns_to_ktime(rt_period
);
7915 tg
->rt_bandwidth
.rt_runtime
= rt_runtime
;
7917 for_each_possible_cpu(i
) {
7918 struct rt_rq
*rt_rq
= tg
->rt_rq
[i
];
7920 raw_spin_lock(&rt_rq
->rt_runtime_lock
);
7921 rt_rq
->rt_runtime
= rt_runtime
;
7922 raw_spin_unlock(&rt_rq
->rt_runtime_lock
);
7924 raw_spin_unlock_irq(&tg
->rt_bandwidth
.rt_runtime_lock
);
7926 read_unlock(&tasklist_lock
);
7927 mutex_unlock(&rt_constraints_mutex
);
7932 static int sched_group_set_rt_runtime(struct task_group
*tg
, long rt_runtime_us
)
7934 u64 rt_runtime
, rt_period
;
7936 rt_period
= ktime_to_ns(tg
->rt_bandwidth
.rt_period
);
7937 rt_runtime
= (u64
)rt_runtime_us
* NSEC_PER_USEC
;
7938 if (rt_runtime_us
< 0)
7939 rt_runtime
= RUNTIME_INF
;
7941 return tg_set_rt_bandwidth(tg
, rt_period
, rt_runtime
);
7944 static long sched_group_rt_runtime(struct task_group
*tg
)
7948 if (tg
->rt_bandwidth
.rt_runtime
== RUNTIME_INF
)
7951 rt_runtime_us
= tg
->rt_bandwidth
.rt_runtime
;
7952 do_div(rt_runtime_us
, NSEC_PER_USEC
);
7953 return rt_runtime_us
;
7956 static int sched_group_set_rt_period(struct task_group
*tg
, long rt_period_us
)
7958 u64 rt_runtime
, rt_period
;
7960 rt_period
= (u64
)rt_period_us
* NSEC_PER_USEC
;
7961 rt_runtime
= tg
->rt_bandwidth
.rt_runtime
;
7966 return tg_set_rt_bandwidth(tg
, rt_period
, rt_runtime
);
7969 static long sched_group_rt_period(struct task_group
*tg
)
7973 rt_period_us
= ktime_to_ns(tg
->rt_bandwidth
.rt_period
);
7974 do_div(rt_period_us
, NSEC_PER_USEC
);
7975 return rt_period_us
;
7978 static int sched_rt_global_constraints(void)
7980 u64 runtime
, period
;
7983 if (sysctl_sched_rt_period
<= 0)
7986 runtime
= global_rt_runtime();
7987 period
= global_rt_period();
7990 * Sanity check on the sysctl variables.
7992 if (runtime
> period
&& runtime
!= RUNTIME_INF
)
7995 mutex_lock(&rt_constraints_mutex
);
7996 read_lock(&tasklist_lock
);
7997 ret
= __rt_schedulable(NULL
, 0, 0);
7998 read_unlock(&tasklist_lock
);
7999 mutex_unlock(&rt_constraints_mutex
);
8004 static int sched_rt_can_attach(struct task_group
*tg
, struct task_struct
*tsk
)
8006 /* Don't accept realtime tasks when there is no way for them to run */
8007 if (rt_task(tsk
) && tg
->rt_bandwidth
.rt_runtime
== 0)
8013 #else /* !CONFIG_RT_GROUP_SCHED */
8014 static int sched_rt_global_constraints(void)
8016 unsigned long flags
;
8019 if (sysctl_sched_rt_period
<= 0)
8023 * There's always some RT tasks in the root group
8024 * -- migration, kstopmachine etc..
8026 if (sysctl_sched_rt_runtime
== 0)
8029 raw_spin_lock_irqsave(&def_rt_bandwidth
.rt_runtime_lock
, flags
);
8030 for_each_possible_cpu(i
) {
8031 struct rt_rq
*rt_rq
= &cpu_rq(i
)->rt
;
8033 raw_spin_lock(&rt_rq
->rt_runtime_lock
);
8034 rt_rq
->rt_runtime
= global_rt_runtime();
8035 raw_spin_unlock(&rt_rq
->rt_runtime_lock
);
8037 raw_spin_unlock_irqrestore(&def_rt_bandwidth
.rt_runtime_lock
, flags
);
8041 #endif /* CONFIG_RT_GROUP_SCHED */
8043 int sched_rr_handler(struct ctl_table
*table
, int write
,
8044 void __user
*buffer
, size_t *lenp
,
8048 static DEFINE_MUTEX(mutex
);
8051 ret
= proc_dointvec(table
, write
, buffer
, lenp
, ppos
);
8052 /* make sure that internally we keep jiffies */
8053 /* also, writing zero resets timeslice to default */
8054 if (!ret
&& write
) {
8055 sched_rr_timeslice
= sched_rr_timeslice
<= 0 ?
8056 RR_TIMESLICE
: msecs_to_jiffies(sched_rr_timeslice
);
8058 mutex_unlock(&mutex
);
8062 int sched_rt_handler(struct ctl_table
*table
, int write
,
8063 void __user
*buffer
, size_t *lenp
,
8067 int old_period
, old_runtime
;
8068 static DEFINE_MUTEX(mutex
);
8071 old_period
= sysctl_sched_rt_period
;
8072 old_runtime
= sysctl_sched_rt_runtime
;
8074 ret
= proc_dointvec(table
, write
, buffer
, lenp
, ppos
);
8076 if (!ret
&& write
) {
8077 ret
= sched_rt_global_constraints();
8079 sysctl_sched_rt_period
= old_period
;
8080 sysctl_sched_rt_runtime
= old_runtime
;
8082 def_rt_bandwidth
.rt_runtime
= global_rt_runtime();
8083 def_rt_bandwidth
.rt_period
=
8084 ns_to_ktime(global_rt_period());
8087 mutex_unlock(&mutex
);
8092 #ifdef CONFIG_CGROUP_SCHED
8094 /* return corresponding task_group object of a cgroup */
8095 static inline struct task_group
*cgroup_tg(struct cgroup
*cgrp
)
8097 return container_of(cgroup_subsys_state(cgrp
, cpu_cgroup_subsys_id
),
8098 struct task_group
, css
);
8101 static struct cgroup_subsys_state
*cpu_cgroup_css_alloc(struct cgroup
*cgrp
)
8103 struct task_group
*tg
, *parent
;
8105 if (!cgrp
->parent
) {
8106 /* This is early initialization for the top cgroup */
8107 return &root_task_group
.css
;
8110 parent
= cgroup_tg(cgrp
->parent
);
8111 tg
= sched_create_group(parent
);
8113 return ERR_PTR(-ENOMEM
);
8118 static int cpu_cgroup_css_online(struct cgroup
*cgrp
)
8120 struct task_group
*tg
= cgroup_tg(cgrp
);
8121 struct task_group
*parent
;
8126 parent
= cgroup_tg(cgrp
->parent
);
8127 sched_online_group(tg
, parent
);
8131 static void cpu_cgroup_css_free(struct cgroup
*cgrp
)
8133 struct task_group
*tg
= cgroup_tg(cgrp
);
8135 sched_destroy_group(tg
);
8138 static void cpu_cgroup_css_offline(struct cgroup
*cgrp
)
8140 struct task_group
*tg
= cgroup_tg(cgrp
);
8142 sched_offline_group(tg
);
8146 cpu_cgroup_allow_attach(struct cgroup
*cgrp
, struct cgroup_taskset
*tset
)
8148 const struct cred
*cred
= current_cred(), *tcred
;
8149 struct task_struct
*task
;
8151 cgroup_taskset_for_each(task
, cgrp
, tset
) {
8152 tcred
= __task_cred(task
);
8154 if ((current
!= task
) && !capable(CAP_SYS_NICE
) &&
8155 cred
->euid
!= tcred
->uid
&& cred
->euid
!= tcred
->suid
)
8162 static int cpu_cgroup_can_attach(struct cgroup
*cgrp
,
8163 struct cgroup_taskset
*tset
)
8165 struct task_struct
*task
;
8167 cgroup_taskset_for_each(task
, cgrp
, tset
) {
8168 #ifdef CONFIG_RT_GROUP_SCHED
8169 if (!sched_rt_can_attach(cgroup_tg(cgrp
), task
))
8172 /* We don't support RT-tasks being in separate groups */
8173 if (task
->sched_class
!= &fair_sched_class
)
8180 static void cpu_cgroup_attach(struct cgroup
*cgrp
,
8181 struct cgroup_taskset
*tset
)
8183 struct task_struct
*task
;
8185 cgroup_taskset_for_each(task
, cgrp
, tset
)
8186 sched_move_task(task
);
8190 cpu_cgroup_exit(struct cgroup
*cgrp
, struct cgroup
*old_cgrp
,
8191 struct task_struct
*task
)
8194 * cgroup_exit() is called in the copy_process() failure path.
8195 * Ignore this case since the task hasn't ran yet, this avoids
8196 * trying to poke a half freed task state from generic code.
8198 if (!(task
->flags
& PF_EXITING
))
8201 sched_move_task(task
);
8204 #ifdef CONFIG_FAIR_GROUP_SCHED
8205 static int cpu_shares_write_u64(struct cgroup
*cgrp
, struct cftype
*cftype
,
8208 return sched_group_set_shares(cgroup_tg(cgrp
), scale_load(shareval
));
8211 static u64
cpu_shares_read_u64(struct cgroup
*cgrp
, struct cftype
*cft
)
8213 struct task_group
*tg
= cgroup_tg(cgrp
);
8215 return (u64
) scale_load_down(tg
->shares
);
8218 #ifdef CONFIG_CFS_BANDWIDTH
8219 static DEFINE_MUTEX(cfs_constraints_mutex
);
8221 const u64 max_cfs_quota_period
= 1 * NSEC_PER_SEC
; /* 1s */
8222 const u64 min_cfs_quota_period
= 1 * NSEC_PER_MSEC
; /* 1ms */
8224 static int __cfs_schedulable(struct task_group
*tg
, u64 period
, u64 runtime
);
8226 static int tg_set_cfs_bandwidth(struct task_group
*tg
, u64 period
, u64 quota
)
8228 int i
, ret
= 0, runtime_enabled
, runtime_was_enabled
;
8229 struct cfs_bandwidth
*cfs_b
= &tg
->cfs_bandwidth
;
8231 if (tg
== &root_task_group
)
8235 * Ensure we have at some amount of bandwidth every period. This is
8236 * to prevent reaching a state of large arrears when throttled via
8237 * entity_tick() resulting in prolonged exit starvation.
8239 if (quota
< min_cfs_quota_period
|| period
< min_cfs_quota_period
)
8243 * Likewise, bound things on the otherside by preventing insane quota
8244 * periods. This also allows us to normalize in computing quota
8247 if (period
> max_cfs_quota_period
)
8250 mutex_lock(&cfs_constraints_mutex
);
8251 ret
= __cfs_schedulable(tg
, period
, quota
);
8255 runtime_enabled
= quota
!= RUNTIME_INF
;
8256 runtime_was_enabled
= cfs_b
->quota
!= RUNTIME_INF
;
8258 * If we need to toggle cfs_bandwidth_used, off->on must occur
8259 * before making related changes, and on->off must occur afterwards
8261 if (runtime_enabled
&& !runtime_was_enabled
)
8262 cfs_bandwidth_usage_inc();
8263 raw_spin_lock_irq(&cfs_b
->lock
);
8264 cfs_b
->period
= ns_to_ktime(period
);
8265 cfs_b
->quota
= quota
;
8267 __refill_cfs_bandwidth_runtime(cfs_b
);
8268 /* restart the period timer (if active) to handle new period expiry */
8269 if (runtime_enabled
&& cfs_b
->timer_active
) {
8270 /* force a reprogram */
8271 cfs_b
->timer_active
= 0;
8272 __start_cfs_bandwidth(cfs_b
);
8274 raw_spin_unlock_irq(&cfs_b
->lock
);
8276 for_each_possible_cpu(i
) {
8277 struct cfs_rq
*cfs_rq
= tg
->cfs_rq
[i
];
8278 struct rq
*rq
= cfs_rq
->rq
;
8280 raw_spin_lock_irq(&rq
->lock
);
8281 cfs_rq
->runtime_enabled
= runtime_enabled
;
8282 cfs_rq
->runtime_remaining
= 0;
8284 if (cfs_rq
->throttled
)
8285 unthrottle_cfs_rq(cfs_rq
);
8286 raw_spin_unlock_irq(&rq
->lock
);
8288 if (runtime_was_enabled
&& !runtime_enabled
)
8289 cfs_bandwidth_usage_dec();
8291 mutex_unlock(&cfs_constraints_mutex
);
8296 int tg_set_cfs_quota(struct task_group
*tg
, long cfs_quota_us
)
8300 period
= ktime_to_ns(tg
->cfs_bandwidth
.period
);
8301 if (cfs_quota_us
< 0)
8302 quota
= RUNTIME_INF
;
8304 quota
= (u64
)cfs_quota_us
* NSEC_PER_USEC
;
8306 return tg_set_cfs_bandwidth(tg
, period
, quota
);
8309 long tg_get_cfs_quota(struct task_group
*tg
)
8313 if (tg
->cfs_bandwidth
.quota
== RUNTIME_INF
)
8316 quota_us
= tg
->cfs_bandwidth
.quota
;
8317 do_div(quota_us
, NSEC_PER_USEC
);
8322 int tg_set_cfs_period(struct task_group
*tg
, long cfs_period_us
)
8326 period
= (u64
)cfs_period_us
* NSEC_PER_USEC
;
8327 quota
= tg
->cfs_bandwidth
.quota
;
8329 return tg_set_cfs_bandwidth(tg
, period
, quota
);
8332 long tg_get_cfs_period(struct task_group
*tg
)
8336 cfs_period_us
= ktime_to_ns(tg
->cfs_bandwidth
.period
);
8337 do_div(cfs_period_us
, NSEC_PER_USEC
);
8339 return cfs_period_us
;
8342 static s64
cpu_cfs_quota_read_s64(struct cgroup
*cgrp
, struct cftype
*cft
)
8344 return tg_get_cfs_quota(cgroup_tg(cgrp
));
8347 static int cpu_cfs_quota_write_s64(struct cgroup
*cgrp
, struct cftype
*cftype
,
8350 return tg_set_cfs_quota(cgroup_tg(cgrp
), cfs_quota_us
);
8353 static u64
cpu_cfs_period_read_u64(struct cgroup
*cgrp
, struct cftype
*cft
)
8355 return tg_get_cfs_period(cgroup_tg(cgrp
));
8358 static int cpu_cfs_period_write_u64(struct cgroup
*cgrp
, struct cftype
*cftype
,
8361 return tg_set_cfs_period(cgroup_tg(cgrp
), cfs_period_us
);
8364 struct cfs_schedulable_data
{
8365 struct task_group
*tg
;
8370 * normalize group quota/period to be quota/max_period
8371 * note: units are usecs
8373 static u64
normalize_cfs_quota(struct task_group
*tg
,
8374 struct cfs_schedulable_data
*d
)
8382 period
= tg_get_cfs_period(tg
);
8383 quota
= tg_get_cfs_quota(tg
);
8386 /* note: these should typically be equivalent */
8387 if (quota
== RUNTIME_INF
|| quota
== -1)
8390 return to_ratio(period
, quota
);
8393 static int tg_cfs_schedulable_down(struct task_group
*tg
, void *data
)
8395 struct cfs_schedulable_data
*d
= data
;
8396 struct cfs_bandwidth
*cfs_b
= &tg
->cfs_bandwidth
;
8397 s64 quota
= 0, parent_quota
= -1;
8400 quota
= RUNTIME_INF
;
8402 struct cfs_bandwidth
*parent_b
= &tg
->parent
->cfs_bandwidth
;
8404 quota
= normalize_cfs_quota(tg
, d
);
8405 parent_quota
= parent_b
->hierarchal_quota
;
8408 * ensure max(child_quota) <= parent_quota, inherit when no
8411 if (quota
== RUNTIME_INF
)
8412 quota
= parent_quota
;
8413 else if (parent_quota
!= RUNTIME_INF
&& quota
> parent_quota
)
8416 cfs_b
->hierarchal_quota
= quota
;
8421 static int __cfs_schedulable(struct task_group
*tg
, u64 period
, u64 quota
)
8424 struct cfs_schedulable_data data
= {
8430 if (quota
!= RUNTIME_INF
) {
8431 do_div(data
.period
, NSEC_PER_USEC
);
8432 do_div(data
.quota
, NSEC_PER_USEC
);
8436 ret
= walk_tg_tree(tg_cfs_schedulable_down
, tg_nop
, &data
);
8442 static int cpu_stats_show(struct cgroup
*cgrp
, struct cftype
*cft
,
8443 struct cgroup_map_cb
*cb
)
8445 struct task_group
*tg
= cgroup_tg(cgrp
);
8446 struct cfs_bandwidth
*cfs_b
= &tg
->cfs_bandwidth
;
8448 cb
->fill(cb
, "nr_periods", cfs_b
->nr_periods
);
8449 cb
->fill(cb
, "nr_throttled", cfs_b
->nr_throttled
);
8450 cb
->fill(cb
, "throttled_time", cfs_b
->throttled_time
);
8454 #endif /* CONFIG_CFS_BANDWIDTH */
8455 #endif /* CONFIG_FAIR_GROUP_SCHED */
8457 #ifdef CONFIG_RT_GROUP_SCHED
8458 static int cpu_rt_runtime_write(struct cgroup
*cgrp
, struct cftype
*cft
,
8461 return sched_group_set_rt_runtime(cgroup_tg(cgrp
), val
);
8464 static s64
cpu_rt_runtime_read(struct cgroup
*cgrp
, struct cftype
*cft
)
8466 return sched_group_rt_runtime(cgroup_tg(cgrp
));
8469 static int cpu_rt_period_write_uint(struct cgroup
*cgrp
, struct cftype
*cftype
,
8472 return sched_group_set_rt_period(cgroup_tg(cgrp
), rt_period_us
);
8475 static u64
cpu_rt_period_read_uint(struct cgroup
*cgrp
, struct cftype
*cft
)
8477 return sched_group_rt_period(cgroup_tg(cgrp
));
8479 #endif /* CONFIG_RT_GROUP_SCHED */
8481 static struct cftype cpu_files
[] = {
8482 #ifdef CONFIG_FAIR_GROUP_SCHED
8485 .read_u64
= cpu_shares_read_u64
,
8486 .write_u64
= cpu_shares_write_u64
,
8489 #ifdef CONFIG_CFS_BANDWIDTH
8491 .name
= "cfs_quota_us",
8492 .read_s64
= cpu_cfs_quota_read_s64
,
8493 .write_s64
= cpu_cfs_quota_write_s64
,
8496 .name
= "cfs_period_us",
8497 .read_u64
= cpu_cfs_period_read_u64
,
8498 .write_u64
= cpu_cfs_period_write_u64
,
8502 .read_map
= cpu_stats_show
,
8505 #ifdef CONFIG_RT_GROUP_SCHED
8507 .name
= "rt_runtime_us",
8508 .read_s64
= cpu_rt_runtime_read
,
8509 .write_s64
= cpu_rt_runtime_write
,
8512 .name
= "rt_period_us",
8513 .read_u64
= cpu_rt_period_read_uint
,
8514 .write_u64
= cpu_rt_period_write_uint
,
8520 struct cgroup_subsys cpu_cgroup_subsys
= {
8522 .css_alloc
= cpu_cgroup_css_alloc
,
8523 .css_free
= cpu_cgroup_css_free
,
8524 .css_online
= cpu_cgroup_css_online
,
8525 .css_offline
= cpu_cgroup_css_offline
,
8526 .can_attach
= cpu_cgroup_can_attach
,
8527 .attach
= cpu_cgroup_attach
,
8528 .allow_attach
= cpu_cgroup_allow_attach
,
8529 .exit
= cpu_cgroup_exit
,
8530 .subsys_id
= cpu_cgroup_subsys_id
,
8531 .base_cftypes
= cpu_files
,
8535 #endif /* CONFIG_CGROUP_SCHED */
8537 void dump_cpu_task(int cpu
)
8539 pr_info("Task dump for CPU %d:\n", cpu
);
8540 sched_show_task(cpu_curr(cpu
));
8543 unsigned long long mt_get_thread_cputime(pid_t pid
)
8545 struct task_struct
*p
;
8546 p
= pid
? find_task_by_vpid(pid
) : current
;
8547 return task_sched_runtime(p
);
8549 unsigned long long mt_get_cpu_idle(int cpu
)
8551 unsigned long long *unused
= 0;
8552 return get_cpu_idle_time_us(cpu
, unused
);
8554 unsigned long long mt_sched_clock(void)
8556 return sched_clock();
8558 EXPORT_SYMBOL(mt_get_thread_cputime
);
8559 EXPORT_SYMBOL(mt_get_cpu_idle
);
8560 EXPORT_SYMBOL(mt_sched_clock
);