2 #include <linux/sched.h>
3 #include <linux/sched/sysctl.h>
4 #include <linux/sched/rt.h>
5 #include <linux/mutex.h>
6 #include <linux/spinlock.h>
7 #include <linux/stop_machine.h>
8 #include <linux/tick.h>
13 extern __read_mostly
int scheduler_running
;
15 extern unsigned long get_cpu_load(int cpu
);
17 * 'User priority' is the nice value converted to something we
18 * can work with better when scaling various scheduler parameters,
19 * it's a [ 0 ... 39 ] range.
21 #define USER_PRIO(p) ((p)-MAX_RT_PRIO)
22 #define TASK_USER_PRIO(p) USER_PRIO((p)->static_prio)
23 #define MAX_USER_PRIO (USER_PRIO(MAX_PRIO))
26 * Helpers for converting nanosecond timing to jiffy resolution
28 #define NS_TO_JIFFIES(TIME) ((unsigned long)(TIME) / (NSEC_PER_SEC / HZ))
31 * Increase resolution of nice-level calculations for 64-bit architectures.
32 * The extra resolution improves shares distribution and load balancing of
33 * low-weight task groups (eg. nice +19 on an autogroup), deeper taskgroup
34 * hierarchies, especially on larger systems. This is not a user-visible change
35 * and does not change the user-interface for setting shares/weights.
37 * We increase resolution only if we have enough bits to allow this increased
38 * resolution (i.e. BITS_PER_LONG > 32). The costs for increasing resolution
39 * when BITS_PER_LONG <= 32 are pretty high and the returns do not justify the
42 #if 0 /* BITS_PER_LONG > 32 -- currently broken: it increases power usage under light load */
43 # define SCHED_LOAD_RESOLUTION 10
44 # define scale_load(w) ((w) << SCHED_LOAD_RESOLUTION)
45 # define scale_load_down(w) ((w) >> SCHED_LOAD_RESOLUTION)
47 # define SCHED_LOAD_RESOLUTION 0
48 # define scale_load(w) (w)
49 # define scale_load_down(w) (w)
52 #define SCHED_LOAD_SHIFT (10 + SCHED_LOAD_RESOLUTION)
53 #define SCHED_LOAD_SCALE (1L << SCHED_LOAD_SHIFT)
55 #define NICE_0_LOAD SCHED_LOAD_SCALE
56 #define NICE_0_SHIFT SCHED_LOAD_SHIFT
59 * These are the 'tuning knobs' of the scheduler:
63 * single value that denotes runtime == period, ie unlimited time.
65 #define RUNTIME_INF ((u64)~0ULL)
67 static inline int rt_policy(int policy
)
69 if (policy
== SCHED_FIFO
|| policy
== SCHED_RR
)
74 static inline int task_has_rt_policy(struct task_struct
*p
)
76 return rt_policy(p
->policy
);
80 * This is the priority-queue data structure of the RT scheduling class:
82 struct rt_prio_array
{
83 DECLARE_BITMAP(bitmap
, MAX_RT_PRIO
+1); /* include 1 bit for delimiter */
84 struct list_head queue
[MAX_RT_PRIO
];
88 /* nests inside the rq lock: */
89 raw_spinlock_t rt_runtime_lock
;
92 struct hrtimer rt_period_timer
;
95 extern struct mutex sched_domains_mutex
;
97 #ifdef CONFIG_CGROUP_SCHED
99 #include <linux/cgroup.h>
104 extern struct list_head task_groups
;
106 struct cfs_bandwidth
{
107 #ifdef CONFIG_CFS_BANDWIDTH
111 s64 hierarchal_quota
;
114 int idle
, timer_active
;
115 struct hrtimer period_timer
, slack_timer
;
116 struct list_head throttled_cfs_rq
;
119 int nr_periods
, nr_throttled
;
124 /* task group related information */
126 struct cgroup_subsys_state css
;
128 #ifdef CONFIG_FAIR_GROUP_SCHED
129 /* schedulable entities of this group on each cpu */
130 struct sched_entity
**se
;
131 /* runqueue "owned" by this group on each cpu */
132 struct cfs_rq
**cfs_rq
;
133 unsigned long shares
;
136 atomic_long_t load_avg
;
137 atomic_t runnable_avg
, usage_avg
;
141 #ifdef CONFIG_RT_GROUP_SCHED
142 struct sched_rt_entity
**rt_se
;
143 struct rt_rq
**rt_rq
;
145 struct rt_bandwidth rt_bandwidth
;
149 struct list_head list
;
151 struct task_group
*parent
;
152 struct list_head siblings
;
153 struct list_head children
;
155 #ifdef CONFIG_SCHED_AUTOGROUP
156 struct autogroup
*autogroup
;
159 struct cfs_bandwidth cfs_bandwidth
;
162 #ifdef CONFIG_FAIR_GROUP_SCHED
163 #define ROOT_TASK_GROUP_LOAD NICE_0_LOAD
166 * A weight of 0 or 1 can cause arithmetics problems.
167 * A weight of a cfs_rq is the sum of weights of which entities
168 * are queued on this cfs_rq, so a weight of a entity should not be
169 * too large, so as the shares value of a task group.
170 * (The default weight is 1024 - so there's no practical
171 * limitation from this.)
173 #define MIN_SHARES (1UL << 1)
174 #define MAX_SHARES (1UL << 18)
177 typedef int (*tg_visitor
)(struct task_group
*, void *);
179 extern int walk_tg_tree_from(struct task_group
*from
,
180 tg_visitor down
, tg_visitor up
, void *data
);
183 * Iterate the full tree, calling @down when first entering a node and @up when
184 * leaving it for the final time.
186 * Caller must hold rcu_lock or sufficient equivalent.
188 static inline int walk_tg_tree(tg_visitor down
, tg_visitor up
, void *data
)
190 return walk_tg_tree_from(&root_task_group
, down
, up
, data
);
193 extern int tg_nop(struct task_group
*tg
, void *data
);
195 extern void free_fair_sched_group(struct task_group
*tg
);
196 extern int alloc_fair_sched_group(struct task_group
*tg
, struct task_group
*parent
);
197 extern void unregister_fair_sched_group(struct task_group
*tg
, int cpu
);
198 extern void init_tg_cfs_entry(struct task_group
*tg
, struct cfs_rq
*cfs_rq
,
199 struct sched_entity
*se
, int cpu
,
200 struct sched_entity
*parent
);
201 extern void init_cfs_bandwidth(struct cfs_bandwidth
*cfs_b
);
202 extern int sched_group_set_shares(struct task_group
*tg
, unsigned long shares
);
204 extern void __refill_cfs_bandwidth_runtime(struct cfs_bandwidth
*cfs_b
);
205 extern void __start_cfs_bandwidth(struct cfs_bandwidth
*cfs_b
);
206 extern void unthrottle_cfs_rq(struct cfs_rq
*cfs_rq
);
208 extern void free_rt_sched_group(struct task_group
*tg
);
209 extern int alloc_rt_sched_group(struct task_group
*tg
, struct task_group
*parent
);
210 extern void init_tg_rt_entry(struct task_group
*tg
, struct rt_rq
*rt_rq
,
211 struct sched_rt_entity
*rt_se
, int cpu
,
212 struct sched_rt_entity
*parent
);
214 extern struct task_group
*sched_create_group(struct task_group
*parent
);
215 extern void sched_online_group(struct task_group
*tg
,
216 struct task_group
*parent
);
217 extern void sched_destroy_group(struct task_group
*tg
);
218 extern void sched_offline_group(struct task_group
*tg
);
220 extern void sched_move_task(struct task_struct
*tsk
);
222 #ifdef CONFIG_FAIR_GROUP_SCHED
223 extern int sched_group_set_shares(struct task_group
*tg
, unsigned long shares
);
226 #else /* CONFIG_CGROUP_SCHED */
228 struct cfs_bandwidth
{ };
230 #endif /* CONFIG_CGROUP_SCHED */
232 /* CFS-related fields in a runqueue */
234 struct load_weight load
;
235 unsigned int nr_running
, h_nr_running
;
240 u64 min_vruntime_copy
;
243 struct rb_root tasks_timeline
;
244 struct rb_node
*rb_leftmost
;
247 * 'curr' points to currently running entity on this cfs_rq.
248 * It is set to NULL otherwise (i.e when none are currently running).
250 struct sched_entity
*curr
, *next
, *last
, *skip
;
252 #ifdef CONFIG_SCHED_DEBUG
253 unsigned int nr_spread_over
;
259 * Under CFS, load is tracked on a per-entity basis and aggregated up.
260 * This allows for the description of both thread and group usage (in
261 * the FAIR_GROUP_SCHED case).
263 unsigned long runnable_load_avg
, blocked_load_avg
;
264 atomic64_t decay_counter
;
266 atomic_long_t removed_load
;
268 #ifdef CONFIG_FAIR_GROUP_SCHED
269 /* Required to track per-cpu representation of a task_group */
270 u32 tg_runnable_contrib
, tg_usage_contrib
;
271 unsigned long tg_load_contrib
;
272 #endif /* CONFIG_FAIR_GROUP_SCHED */
274 struct sched_avg avg
;
277 * h_load = weight * f(tg)
279 * Where f(tg) is the recursive weight fraction assigned to
282 unsigned long h_load
;
283 #endif /* CONFIG_SMP */
285 #ifdef CONFIG_FAIR_GROUP_SCHED
286 struct rq
*rq
; /* cpu runqueue to which this cfs_rq is attached */
289 * leaf cfs_rqs are those that hold tasks (lowest schedulable entity in
290 * a hierarchy). Non-leaf lrqs hold other higher schedulable entities
291 * (like users, containers etc.)
293 * leaf_cfs_rq_list ties together list of leaf cfs_rq's in a cpu. This
294 * list is used during load balance.
297 struct list_head leaf_cfs_rq_list
;
298 struct task_group
*tg
; /* group that "owns" this runqueue */
300 #ifdef CONFIG_CFS_BANDWIDTH
303 s64 runtime_remaining
;
305 u64 throttled_clock
, throttled_clock_task
;
306 u64 throttled_clock_task_time
;
307 int throttled
, throttle_count
;
308 struct list_head throttled_list
;
309 #endif /* CONFIG_CFS_BANDWIDTH */
310 #endif /* CONFIG_FAIR_GROUP_SCHED */
313 static inline int rt_bandwidth_enabled(void)
315 return sysctl_sched_rt_runtime
>= 0;
318 /* Real-Time classes' related field in a runqueue: */
320 struct rt_prio_array active
;
321 unsigned int rt_nr_running
;
322 #if defined CONFIG_SMP || defined CONFIG_RT_GROUP_SCHED
324 int curr
; /* highest queued rt task prio */
326 int next
; /* next highest */
331 unsigned long rt_nr_migratory
;
332 unsigned long rt_nr_total
;
334 struct plist_head pushable_tasks
;
337 int rt_disable_borrow
;
340 /* Nests inside the rq lock: */
341 raw_spinlock_t rt_runtime_lock
;
343 #ifdef CONFIG_RT_GROUP_SCHED
344 unsigned long rt_nr_boosted
;
347 struct list_head leaf_rt_rq_list
;
348 struct task_group
*tg
;
355 * We add the notion of a root-domain which will be used to define per-domain
356 * variables. Each exclusive cpuset essentially defines an island domain by
357 * fully partitioning the member cpus from any other cpuset. Whenever a new
358 * exclusive cpuset is created, we also create and attach a new root-domain
367 cpumask_var_t online
;
370 * The "RT overload" flag: it gets set if a CPU has more than
371 * one runnable RT task.
373 cpumask_var_t rto_mask
;
374 struct cpupri cpupri
;
377 extern struct root_domain def_root_domain
;
379 #endif /* CONFIG_SMP */
382 * This is the main, per-CPU runqueue data structure.
384 * Locking rule: those places that want to lock multiple runqueues
385 * (such as the load balancing or the thread migration code), lock
386 * acquire operations must be ordered by ascending &runqueue.
393 * nr_running and cpu_load should be in the same cacheline because
394 * remote CPUs use both these fields when doing load calculation.
396 unsigned int nr_running
;
397 #define CPU_LOAD_IDX_MAX 5
398 unsigned long cpu_load
[CPU_LOAD_IDX_MAX
];
399 unsigned long last_load_update_tick
;
400 #ifdef CONFIG_NO_HZ_COMMON
402 unsigned long nohz_flags
;
404 #ifdef CONFIG_NO_HZ_FULL
405 unsigned long last_sched_tick
;
407 int skip_clock_update
;
409 /* capture load from *all* tasks on this cpu: */
410 struct load_weight load
;
411 unsigned long nr_load_updates
;
417 #ifdef CONFIG_FAIR_GROUP_SCHED
418 /* list of leaf cfs_rq on this cpu: */
419 struct list_head leaf_cfs_rq_list
;
421 unsigned long h_load_throttle
;
422 #endif /* CONFIG_SMP */
423 #endif /* CONFIG_FAIR_GROUP_SCHED */
425 #ifdef CONFIG_RT_GROUP_SCHED
426 struct list_head leaf_rt_rq_list
;
430 * This is part of a global counter where only the total sum
431 * over all CPUs matters. A task can increase this counter on
432 * one CPU and if it got migrated afterwards it may decrease
433 * it on another CPU. Always updated under the runqueue lock:
435 unsigned long nr_uninterruptible
;
437 struct task_struct
*curr
, *idle
, *stop
;
438 unsigned long next_balance
;
439 struct mm_struct
*prev_mm
;
447 struct root_domain
*rd
;
448 struct sched_domain
*sd
;
450 unsigned long cpu_power
;
452 unsigned char idle_balance
;
453 /* For active balancing */
457 struct cpu_stop_work active_balance_work
;
458 #ifdef CONFIG_SCHED_HMP
459 struct task_struct
*migrate_task
;
461 /* cpu of this runqueue: */
465 struct list_head cfs_tasks
;
473 #ifdef CONFIG_IRQ_TIME_ACCOUNTING
476 #ifdef CONFIG_PARAVIRT
479 #ifdef CONFIG_PARAVIRT_TIME_ACCOUNTING
480 u64 prev_steal_time_rq
;
483 /* calc_load related fields */
484 unsigned long calc_load_update
;
485 long calc_load_active
;
487 #ifdef CONFIG_SCHED_HRTICK
489 int hrtick_csd_pending
;
490 struct call_single_data hrtick_csd
;
492 struct hrtimer hrtick_timer
;
495 #ifdef CONFIG_SCHEDSTATS
497 struct sched_info rq_sched_info
;
498 unsigned long long rq_cpu_time
;
499 /* could above be rq->cfs_rq.exec_clock + rq->rt_rq.rt_runtime ? */
501 /* sys_sched_yield() stats */
502 unsigned int yld_count
;
504 /* schedule() stats */
505 unsigned int sched_count
;
506 unsigned int sched_goidle
;
508 /* try_to_wake_up() stats */
509 unsigned int ttwu_count
;
510 unsigned int ttwu_local
;
514 struct llist_head wake_list
;
517 struct sched_avg avg
;
520 static inline int cpu_of(struct rq
*rq
)
529 DECLARE_PER_CPU(struct rq
, runqueues
);
531 #define cpu_rq(cpu) (&per_cpu(runqueues, (cpu)))
532 #define this_rq() (&__get_cpu_var(runqueues))
533 #define task_rq(p) cpu_rq(task_cpu(p))
534 #define cpu_curr(cpu) (cpu_rq(cpu)->curr)
535 #define raw_rq() (&__raw_get_cpu_var(runqueues))
539 #define rcu_dereference_check_sched_domain(p) \
540 rcu_dereference_check((p), \
541 lockdep_is_held(&sched_domains_mutex))
544 * The domain tree (rq->sd) is protected by RCU's quiescent state transition.
545 * See detach_destroy_domains: synchronize_sched for details.
547 * The domain tree of any CPU may only be accessed from within
548 * preempt-disabled sections.
550 #define for_each_domain(cpu, __sd) \
551 for (__sd = rcu_dereference_check_sched_domain(cpu_rq(cpu)->sd); \
552 __sd; __sd = __sd->parent)
554 #define for_each_lower_domain(sd) for (; sd; sd = sd->child)
557 * highest_flag_domain - Return highest sched_domain containing flag.
558 * @cpu: The cpu whose highest level of sched domain is to
560 * @flag: The flag to check for the highest sched_domain
563 * Returns the highest sched_domain of a cpu which contains the given flag.
565 static inline struct sched_domain
*highest_flag_domain(int cpu
, int flag
)
567 struct sched_domain
*sd
, *hsd
= NULL
;
569 for_each_domain(cpu
, sd
) {
570 if (!(sd
->flags
& flag
))
578 DECLARE_PER_CPU(struct sched_domain
*, sd_llc
);
579 DECLARE_PER_CPU(int, sd_llc_id
);
581 struct sched_group_power
{
584 * CPU power of this group, SCHED_LOAD_SCALE being max power for a
587 unsigned int power
, power_orig
;
588 unsigned long next_update
;
590 * Number of busy cpus in this group.
592 atomic_t nr_busy_cpus
;
594 unsigned long cpumask
[0]; /* iteration mask */
598 struct sched_group
*next
; /* Must be a circular list */
601 unsigned int group_weight
;
602 struct sched_group_power
*sgp
;
605 * The CPUs this group covers.
607 * NOTE: this field is variable length. (Allocated dynamically
608 * by attaching extra space to the end of the structure,
609 * depending on how many CPUs the kernel has booted up with)
611 unsigned long cpumask
[0];
614 static inline struct cpumask
*sched_group_cpus(struct sched_group
*sg
)
616 return to_cpumask(sg
->cpumask
);
620 * cpumask masking which cpus in the group are allowed to iterate up the domain
623 static inline struct cpumask
*sched_group_mask(struct sched_group
*sg
)
625 return to_cpumask(sg
->sgp
->cpumask
);
629 * group_first_cpu - Returns the first cpu in the cpumask of a sched_group.
630 * @group: The group whose first cpu is to be returned.
632 static inline unsigned int group_first_cpu(struct sched_group
*group
)
634 return cpumask_first(sched_group_cpus(group
));
637 extern int group_balance_cpu(struct sched_group
*sg
);
639 #ifdef CONFIG_SCHED_HMP
640 static LIST_HEAD(hmp_domains
);
641 DECLARE_PER_CPU(struct hmp_domain
*, hmp_cpu_domain
);
642 #define hmp_cpu_domain(cpu) (per_cpu(hmp_cpu_domain, (cpu)))
643 #endif /* CONFIG_SCHED_HMP */
645 #endif /* CONFIG_SMP */
648 #include "auto_group.h"
650 #ifdef CONFIG_CGROUP_SCHED
653 * Return the group to which this tasks belongs.
655 * We cannot use task_subsys_state() and friends because the cgroup
656 * subsystem changes that value before the cgroup_subsys::attach() method
657 * is called, therefore we cannot pin it and might observe the wrong value.
659 * The same is true for autogroup's p->signal->autogroup->tg, the autogroup
660 * core changes this before calling sched_move_task().
662 * Instead we use a 'copy' which is updated from sched_move_task() while
663 * holding both task_struct::pi_lock and rq::lock.
665 static inline struct task_group
*task_group(struct task_struct
*p
)
667 return p
->sched_task_group
;
670 /* Change a task's cfs_rq and parent entity if it moves across CPUs/groups */
671 static inline void set_task_rq(struct task_struct
*p
, unsigned int cpu
)
673 #if defined(CONFIG_FAIR_GROUP_SCHED) || defined(CONFIG_RT_GROUP_SCHED)
674 struct task_group
*tg
= task_group(p
);
677 #ifdef CONFIG_FAIR_GROUP_SCHED
678 p
->se
.cfs_rq
= tg
->cfs_rq
[cpu
];
679 p
->se
.parent
= tg
->se
[cpu
];
682 #ifdef CONFIG_RT_GROUP_SCHED
683 p
->rt
.rt_rq
= tg
->rt_rq
[cpu
];
684 p
->rt
.parent
= tg
->rt_se
[cpu
];
688 #else /* CONFIG_CGROUP_SCHED */
690 static inline void set_task_rq(struct task_struct
*p
, unsigned int cpu
) { }
691 static inline struct task_group
*task_group(struct task_struct
*p
)
696 #endif /* CONFIG_CGROUP_SCHED */
698 static inline void __set_task_cpu(struct task_struct
*p
, unsigned int cpu
)
703 * After ->cpu is set up to a new value, task_rq_lock(p, ...) can be
704 * successfuly executed on another CPU. We must ensure that updates of
705 * per-task data have been completed by this moment.
708 task_thread_info(p
)->cpu
= cpu
;
710 #ifdef CONFIG_FAIR_GROUP_SCHED
711 BUG_ON(p
->se
.cfs_rq
->rq
->cpu
!= cpu
);
718 * Tunables that become constants when CONFIG_SCHED_DEBUG is off:
720 #ifdef CONFIG_SCHED_DEBUG
721 # include <linux/static_key.h>
722 # define const_debug __read_mostly
724 # define const_debug const
727 extern const_debug
unsigned int sysctl_sched_features
;
729 #define SCHED_FEAT(name, enabled) \
730 __SCHED_FEAT_##name ,
733 #include "features.h"
739 #if defined(CONFIG_SCHED_DEBUG) && defined(HAVE_JUMP_LABEL)
740 static __always_inline
bool static_branch__true(struct static_key
*key
)
742 return static_key_true(key
); /* Not out of line branch. */
745 static __always_inline
bool static_branch__false(struct static_key
*key
)
747 return static_key_false(key
); /* Out of line branch. */
750 #define SCHED_FEAT(name, enabled) \
751 static __always_inline bool static_branch_##name(struct static_key *key) \
753 return static_branch__##enabled(key); \
756 #include "features.h"
760 extern struct static_key sched_feat_keys
[__SCHED_FEAT_NR
];
761 #define sched_feat(x) (static_branch_##x(&sched_feat_keys[__SCHED_FEAT_##x]))
762 #else /* !(SCHED_DEBUG && HAVE_JUMP_LABEL) */
763 #define sched_feat(x) (sysctl_sched_features & (1UL << __SCHED_FEAT_##x))
764 #endif /* SCHED_DEBUG && HAVE_JUMP_LABEL */
766 #ifdef CONFIG_NUMA_BALANCING
767 #define sched_feat_numa(x) sched_feat(x)
768 #ifdef CONFIG_SCHED_DEBUG
769 #define numabalancing_enabled sched_feat_numa(NUMA)
771 extern bool numabalancing_enabled
;
772 #endif /* CONFIG_SCHED_DEBUG */
774 #define sched_feat_numa(x) (0)
775 #define numabalancing_enabled (0)
776 #endif /* CONFIG_NUMA_BALANCING */
778 static inline u64
global_rt_period(void)
780 return (u64
)sysctl_sched_rt_period
* NSEC_PER_USEC
;
783 static inline u64
global_rt_runtime(void)
785 if (sysctl_sched_rt_runtime
< 0)
788 return (u64
)sysctl_sched_rt_runtime
* NSEC_PER_USEC
;
793 static inline int task_current(struct rq
*rq
, struct task_struct
*p
)
795 return rq
->curr
== p
;
798 static inline int task_running(struct rq
*rq
, struct task_struct
*p
)
803 return task_current(rq
, p
);
808 #ifndef prepare_arch_switch
809 # define prepare_arch_switch(next) do { } while (0)
811 #ifndef finish_arch_switch
812 # define finish_arch_switch(prev) do { } while (0)
814 #ifndef finish_arch_post_lock_switch
815 # define finish_arch_post_lock_switch() do { } while (0)
818 #ifdef CONFIG_MT_RT_SCHED
819 extern void mt_check_rt_policy(struct rq
*this_rq
);
820 extern int push_need_released_rt_task(struct rq
*rq
, struct task_struct
*p
);
821 extern int pull_rt_task(struct rq
*this_rq
);
822 extern int mt_post_schedule(struct rq
*rq
);
825 #ifdef CONFIG_MT_RT_SCHED_LOG
826 #ifdef CONFIG_MT_RT_SCHED_DEBUG
827 #define mt_rt_printf(x...) \
829 char strings[128]=""; \
830 snprintf(strings, 128, x); \
831 printk(KERN_NOTICE x); \
832 trace_sched_rt_log(strings); \
835 #define mt_rt_printf(x...) \
837 char strings[128]=""; \
838 snprintf(strings, 128, x); \
839 trace_sched_rt_log(strings); \
843 #define mt_rt_printf do {} while (0)
846 #ifndef __ARCH_WANT_UNLOCKED_CTXSW
847 static inline void prepare_lock_switch(struct rq
*rq
, struct task_struct
*next
)
851 * We can optimise this out completely for !SMP, because the
852 * SMP rebalancing from interrupt is the only thing that cares
859 static inline void finish_lock_switch(struct rq
*rq
, struct task_struct
*prev
)
863 * After ->on_cpu is cleared, the task can be moved to a different CPU.
864 * We must ensure this doesn't happen until the switch is completely
870 #ifdef CONFIG_DEBUG_SPINLOCK
871 /* this is a valid case when another task releases the spinlock */
872 rq
->lock
.owner
= current
;
875 * If we are tracking spinlock dependencies then we have to
876 * fix up the runqueue lock - which gets 'carried over' from
879 spin_acquire(&rq
->lock
.dep_map
, 0, 0, _THIS_IP_
);
880 #ifdef CONFIG_MT_RT_SCHED
881 if(test_tsk_need_released(prev
)){
882 clear_tsk_need_released(prev
);
883 push_need_released_rt_task(rq
, prev
);
886 raw_spin_unlock_irq(&rq
->lock
);
889 #else /* __ARCH_WANT_UNLOCKED_CTXSW */
890 static inline void prepare_lock_switch(struct rq
*rq
, struct task_struct
*next
)
894 * We can optimise this out completely for !SMP, because the
895 * SMP rebalancing from interrupt is the only thing that cares
900 raw_spin_unlock(&rq
->lock
);
903 static inline void finish_lock_switch(struct rq
*rq
, struct task_struct
*prev
)
907 * After ->on_cpu is cleared, the task can be moved to a different CPU.
908 * We must ensure this doesn't happen until the switch is completely
916 #endif /* __ARCH_WANT_UNLOCKED_CTXSW */
921 #define WF_SYNC 0x01 /* waker goes to sleep after wakeup */
922 #define WF_FORK 0x02 /* child wakeup after fork */
923 #define WF_MIGRATED 0x4 /* internal use, task got migrated */
925 static inline void update_load_add(struct load_weight
*lw
, unsigned long inc
)
931 static inline void update_load_sub(struct load_weight
*lw
, unsigned long dec
)
937 static inline void update_load_set(struct load_weight
*lw
, unsigned long w
)
944 * To aid in avoiding the subversion of "niceness" due to uneven distribution
945 * of tasks with abnormal "nice" values across CPUs the contribution that
946 * each task makes to its run queue's load is weighted according to its
947 * scheduling class and "nice" value. For SCHED_NORMAL tasks this is just a
948 * scaled version of the new time slice allocation that they receive on time
952 #define WEIGHT_IDLEPRIO 3
953 #define WMULT_IDLEPRIO 1431655765
956 * Nice levels are multiplicative, with a gentle 10% change for every
957 * nice level changed. I.e. when a CPU-bound task goes from nice 0 to
958 * nice 1, it will get ~10% less CPU time than another CPU-bound task
959 * that remained on nice 0.
961 * The "10% effect" is relative and cumulative: from _any_ nice level,
962 * if you go up 1 level, it's -10% CPU usage, if you go down 1 level
963 * it's +10% CPU usage. (to achieve that we use a multiplier of 1.25.
964 * If a task goes up by ~10% and another task goes down by ~10% then
965 * the relative distance between them is ~25%.)
967 static const int prio_to_weight
[40] = {
968 /* -20 */ 88761, 71755, 56483, 46273, 36291,
969 /* -15 */ 29154, 23254, 18705, 14949, 11916,
970 /* -10 */ 9548, 7620, 6100, 4904, 3906,
971 /* -5 */ 3121, 2501, 1991, 1586, 1277,
972 /* 0 */ 1024, 820, 655, 526, 423,
973 /* 5 */ 335, 272, 215, 172, 137,
974 /* 10 */ 110, 87, 70, 56, 45,
975 /* 15 */ 36, 29, 23, 18, 15,
979 * Inverse (2^32/x) values of the prio_to_weight[] array, precalculated.
981 * In cases where the weight does not change often, we can use the
982 * precalculated inverse to speed up arithmetics by turning divisions
983 * into multiplications:
985 static const u32 prio_to_wmult
[40] = {
986 /* -20 */ 48388, 59856, 76040, 92818, 118348,
987 /* -15 */ 147320, 184698, 229616, 287308, 360437,
988 /* -10 */ 449829, 563644, 704093, 875809, 1099582,
989 /* -5 */ 1376151, 1717300, 2157191, 2708050, 3363326,
990 /* 0 */ 4194304, 5237765, 6557202, 8165337, 10153587,
991 /* 5 */ 12820798, 15790321, 19976592, 24970740, 31350126,
992 /* 10 */ 39045157, 49367440, 61356676, 76695844, 95443717,
993 /* 15 */ 119304647, 148102320, 186737708, 238609294, 286331153,
996 #define ENQUEUE_WAKEUP 1
997 #define ENQUEUE_HEAD 2
999 #define ENQUEUE_WAKING 4 /* sched_class::task_waking was called */
1001 #define ENQUEUE_WAKING 0
1004 #define DEQUEUE_SLEEP 1
1006 struct sched_class
{
1007 const struct sched_class
*next
;
1009 void (*enqueue_task
) (struct rq
*rq
, struct task_struct
*p
, int flags
);
1010 void (*dequeue_task
) (struct rq
*rq
, struct task_struct
*p
, int flags
);
1011 void (*yield_task
) (struct rq
*rq
);
1012 bool (*yield_to_task
) (struct rq
*rq
, struct task_struct
*p
, bool preempt
);
1014 void (*check_preempt_curr
) (struct rq
*rq
, struct task_struct
*p
, int flags
);
1016 struct task_struct
* (*pick_next_task
) (struct rq
*rq
);
1017 void (*put_prev_task
) (struct rq
*rq
, struct task_struct
*p
);
1020 int (*select_task_rq
)(struct task_struct
*p
, int sd_flag
, int flags
);
1021 void (*migrate_task_rq
)(struct task_struct
*p
, int next_cpu
);
1023 void (*pre_schedule
) (struct rq
*this_rq
, struct task_struct
*task
);
1024 void (*post_schedule
) (struct rq
*this_rq
);
1025 void (*task_waking
) (struct task_struct
*task
);
1026 void (*task_woken
) (struct rq
*this_rq
, struct task_struct
*task
);
1028 void (*set_cpus_allowed
)(struct task_struct
*p
,
1029 const struct cpumask
*newmask
);
1031 void (*rq_online
)(struct rq
*rq
);
1032 void (*rq_offline
)(struct rq
*rq
);
1035 void (*set_curr_task
) (struct rq
*rq
);
1036 void (*task_tick
) (struct rq
*rq
, struct task_struct
*p
, int queued
);
1037 void (*task_fork
) (struct task_struct
*p
);
1039 void (*switched_from
) (struct rq
*this_rq
, struct task_struct
*task
);
1040 void (*switched_to
) (struct rq
*this_rq
, struct task_struct
*task
);
1041 void (*prio_changed
) (struct rq
*this_rq
, struct task_struct
*task
,
1044 unsigned int (*get_rr_interval
) (struct rq
*rq
,
1045 struct task_struct
*task
);
1047 #ifdef CONFIG_FAIR_GROUP_SCHED
1048 void (*task_move_group
) (struct task_struct
*p
, int on_rq
);
1052 #define sched_class_highest (&stop_sched_class)
1053 #define for_each_class(class) \
1054 for (class = sched_class_highest; class; class = class->next)
1056 extern const struct sched_class stop_sched_class
;
1057 extern const struct sched_class rt_sched_class
;
1058 extern const struct sched_class fair_sched_class
;
1059 extern const struct sched_class idle_sched_class
;
1064 extern void update_group_power(struct sched_domain
*sd
, int cpu
);
1066 extern void trigger_load_balance(struct rq
*rq
, int cpu
);
1067 extern void idle_balance(int this_cpu
, struct rq
*this_rq
);
1069 extern void idle_enter_fair(struct rq
*this_rq
);
1070 extern void idle_exit_fair(struct rq
*this_rq
);
1073 # ifdef CONFIG_MTK_SCHED_CMP_TGS
1074 extern int group_leader_is_empty(struct task_struct
*p
);
1075 # endif /* CONFIG_MTK_SCHED_CMP_TGS */
1077 # ifdef CONFIG_MTK_SCHED_CMP
1078 extern void get_cluster_cpus(struct cpumask
*cpus
, int cluster_id
,
1079 bool exclusive_offline
);
1080 extern int get_cluster_id(unsigned int cpu
);
1081 # endif /* CONFIG_MTK_SCHED_CMP */
1083 #else /* CONFIG_SMP */
1085 static inline void idle_balance(int cpu
, struct rq
*rq
)
1091 extern void sysrq_sched_debug_show(void);
1092 extern void sched_init_granularity(void);
1093 #if defined (CONFIG_MTK_SCHED_CMP_PACK_SMALL_TASK) || defined (CONFIG_HMP_PACK_SMALL_TASK)
1094 extern void update_packing_domain(int cpu
);
1095 #endif /* CONFIG_HMP_PACK_SMALL_TASK */
1096 extern void update_max_interval(void);
1097 extern int update_runtime(struct notifier_block
*nfb
, unsigned long action
, void *hcpu
);
1098 extern void init_sched_rt_class(void);
1099 extern void init_sched_fair_class(void);
1101 extern void resched_task(struct task_struct
*p
);
1102 extern void resched_cpu(int cpu
);
1104 extern struct rt_bandwidth def_rt_bandwidth
;
1105 extern void init_rt_bandwidth(struct rt_bandwidth
*rt_b
, u64 period
, u64 runtime
);
1107 extern void update_idle_cpu_load(struct rq
*this_rq
);
1109 extern void init_task_runnable_average(struct task_struct
*p
);
1111 #ifdef CONFIG_PARAVIRT
1112 static inline u64
steal_ticks(u64 steal
)
1114 if (unlikely(steal
> NSEC_PER_SEC
))
1115 return div_u64(steal
, TICK_NSEC
);
1117 return __iter_div_u64_rem(steal
, TICK_NSEC
, &steal
);
1121 static inline void inc_nr_running(struct rq
*rq
)
1123 #ifdef CONFIG_MTK_SCHED_RQAVG_KS
1124 sched_update_nr_prod(cpu_of(rq
), rq
->nr_running
, true);
1125 #endif /* CONFIG_MTK_SCHED_RQAVG_KS */
1128 #ifdef CONFIG_NO_HZ_FULL
1129 if (rq
->nr_running
== 2) {
1130 if (tick_nohz_full_cpu(rq
->cpu
)) {
1131 /* Order rq->nr_running write against the IPI */
1133 smp_send_reschedule(rq
->cpu
);
1139 static inline void dec_nr_running(struct rq
*rq
)
1141 #ifdef CONFIG_MTK_SCHED_RQAVG_KS
1142 sched_update_nr_prod(cpu_of(rq
), rq
->nr_running
, false);
1143 #endif /* CONFIG_MTK_SCHED_RQAVG_KS */
1147 static inline void rq_last_tick_reset(struct rq
*rq
)
1149 #ifdef CONFIG_NO_HZ_FULL
1150 rq
->last_sched_tick
= jiffies
;
1154 extern void update_rq_clock(struct rq
*rq
);
1156 extern void activate_task(struct rq
*rq
, struct task_struct
*p
, int flags
);
1157 extern void deactivate_task(struct rq
*rq
, struct task_struct
*p
, int flags
);
1159 extern void check_preempt_curr(struct rq
*rq
, struct task_struct
*p
, int flags
);
1161 extern const_debug
unsigned int sysctl_sched_time_avg
;
1162 extern const_debug
unsigned int sysctl_sched_nr_migrate
;
1163 extern const_debug
unsigned int sysctl_sched_migration_cost
;
1165 static inline u64
sched_avg_period(void)
1167 return (u64
)sysctl_sched_time_avg
* NSEC_PER_MSEC
/ 2;
1170 #ifdef CONFIG_SCHED_HRTICK
1174 * - enabled by features
1175 * - hrtimer is actually high res
1177 static inline int hrtick_enabled(struct rq
*rq
)
1179 if (!sched_feat(HRTICK
))
1181 if (!cpu_active(cpu_of(rq
)))
1183 return hrtimer_is_hres_active(&rq
->hrtick_timer
);
1186 void hrtick_start(struct rq
*rq
, u64 delay
);
1190 static inline int hrtick_enabled(struct rq
*rq
)
1195 #endif /* CONFIG_SCHED_HRTICK */
1198 extern void sched_avg_update(struct rq
*rq
);
1199 static inline void sched_rt_avg_update(struct rq
*rq
, u64 rt_delta
)
1201 rq
->rt_avg
+= rt_delta
;
1202 sched_avg_update(rq
);
1205 static inline void sched_rt_avg_update(struct rq
*rq
, u64 rt_delta
) { }
1206 static inline void sched_avg_update(struct rq
*rq
) { }
1209 extern void start_bandwidth_timer(struct hrtimer
*period_timer
, ktime_t period
);
1212 #ifdef CONFIG_PREEMPT
1214 static inline void double_rq_lock(struct rq
*rq1
, struct rq
*rq2
);
1217 * fair double_lock_balance: Safely acquires both rq->locks in a fair
1218 * way at the expense of forcing extra atomic operations in all
1219 * invocations. This assures that the double_lock is acquired using the
1220 * same underlying policy as the spinlock_t on this architecture, which
1221 * reduces latency compared to the unfair variant below. However, it
1222 * also adds more overhead and therefore may reduce throughput.
1224 static inline int _double_lock_balance(struct rq
*this_rq
, struct rq
*busiest
)
1225 __releases(this_rq
->lock
)
1226 __acquires(busiest
->lock
)
1227 __acquires(this_rq
->lock
)
1229 raw_spin_unlock(&this_rq
->lock
);
1230 double_rq_lock(this_rq
, busiest
);
1237 * Unfair double_lock_balance: Optimizes throughput at the expense of
1238 * latency by eliminating extra atomic operations when the locks are
1239 * already in proper order on entry. This favors lower cpu-ids and will
1240 * grant the double lock to lower cpus over higher ids under contention,
1241 * regardless of entry order into the function.
1243 static inline int _double_lock_balance(struct rq
*this_rq
, struct rq
*busiest
)
1244 __releases(this_rq
->lock
)
1245 __acquires(busiest
->lock
)
1246 __acquires(this_rq
->lock
)
1250 if (unlikely(!raw_spin_trylock(&busiest
->lock
))) {
1251 if (busiest
< this_rq
) {
1252 raw_spin_unlock(&this_rq
->lock
);
1253 raw_spin_lock(&busiest
->lock
);
1254 raw_spin_lock_nested(&this_rq
->lock
,
1255 SINGLE_DEPTH_NESTING
);
1258 raw_spin_lock_nested(&busiest
->lock
,
1259 SINGLE_DEPTH_NESTING
);
1264 #endif /* CONFIG_PREEMPT */
1267 * double_lock_balance - lock the busiest runqueue, this_rq is locked already.
1269 static inline int double_lock_balance(struct rq
*this_rq
, struct rq
*busiest
)
1271 if (unlikely(!irqs_disabled())) {
1272 /* printk() doesn't work good under rq->lock */
1273 raw_spin_unlock(&this_rq
->lock
);
1277 return _double_lock_balance(this_rq
, busiest
);
1280 static inline void double_unlock_balance(struct rq
*this_rq
, struct rq
*busiest
)
1281 __releases(busiest
->lock
)
1283 raw_spin_unlock(&busiest
->lock
);
1284 lock_set_subclass(&this_rq
->lock
.dep_map
, 0, _RET_IP_
);
1288 * double_rq_lock - safely lock two runqueues
1290 * Note this does not disable interrupts like task_rq_lock,
1291 * you need to do so manually before calling.
1293 static inline void double_rq_lock(struct rq
*rq1
, struct rq
*rq2
)
1294 __acquires(rq1
->lock
)
1295 __acquires(rq2
->lock
)
1297 BUG_ON(!irqs_disabled());
1299 raw_spin_lock(&rq1
->lock
);
1300 __acquire(rq2
->lock
); /* Fake it out ;) */
1303 raw_spin_lock(&rq1
->lock
);
1304 raw_spin_lock_nested(&rq2
->lock
, SINGLE_DEPTH_NESTING
);
1306 raw_spin_lock(&rq2
->lock
);
1307 raw_spin_lock_nested(&rq1
->lock
, SINGLE_DEPTH_NESTING
);
1313 * double_rq_unlock - safely unlock two runqueues
1315 * Note this does not restore interrupts like task_rq_unlock,
1316 * you need to do so manually after calling.
1318 static inline void double_rq_unlock(struct rq
*rq1
, struct rq
*rq2
)
1319 __releases(rq1
->lock
)
1320 __releases(rq2
->lock
)
1322 raw_spin_unlock(&rq1
->lock
);
1324 raw_spin_unlock(&rq2
->lock
);
1326 __release(rq2
->lock
);
1329 #else /* CONFIG_SMP */
1332 * double_rq_lock - safely lock two runqueues
1334 * Note this does not disable interrupts like task_rq_lock,
1335 * you need to do so manually before calling.
1337 static inline void double_rq_lock(struct rq
*rq1
, struct rq
*rq2
)
1338 __acquires(rq1
->lock
)
1339 __acquires(rq2
->lock
)
1341 BUG_ON(!irqs_disabled());
1343 raw_spin_lock(&rq1
->lock
);
1344 __acquire(rq2
->lock
); /* Fake it out ;) */
1348 * double_rq_unlock - safely unlock two runqueues
1350 * Note this does not restore interrupts like task_rq_unlock,
1351 * you need to do so manually after calling.
1353 static inline void double_rq_unlock(struct rq
*rq1
, struct rq
*rq2
)
1354 __releases(rq1
->lock
)
1355 __releases(rq2
->lock
)
1358 raw_spin_unlock(&rq1
->lock
);
1359 __release(rq2
->lock
);
1364 extern struct sched_entity
*__pick_first_entity(struct cfs_rq
*cfs_rq
);
1365 extern struct sched_entity
*__pick_last_entity(struct cfs_rq
*cfs_rq
);
1366 extern void print_cfs_stats(struct seq_file
*m
, int cpu
);
1367 extern void print_rt_stats(struct seq_file
*m
, int cpu
);
1369 extern void init_cfs_rq(struct cfs_rq
*cfs_rq
);
1370 extern void init_rt_rq(struct rt_rq
*rt_rq
, struct rq
*rq
);
1371 extern void unthrottle_offline_rt_rqs(struct rq
*rq
);
1373 extern void cfs_bandwidth_usage_inc(void);
1374 extern void cfs_bandwidth_usage_dec(void);
1376 #ifdef CONFIG_NO_HZ_COMMON
1377 enum rq_nohz_flag_bits
{
1382 #define nohz_flags(cpu) (&cpu_rq(cpu)->nohz_flags)
1385 #ifdef CONFIG_IRQ_TIME_ACCOUNTING
1387 DECLARE_PER_CPU(u64
, cpu_hardirq_time
);
1388 DECLARE_PER_CPU(u64
, cpu_softirq_time
);
1390 #ifndef CONFIG_64BIT
1391 DECLARE_PER_CPU(seqcount_t
, irq_time_seq
);
1393 static inline void irq_time_write_begin(void)
1395 __this_cpu_inc(irq_time_seq
.sequence
);
1399 static inline void irq_time_write_end(void)
1402 __this_cpu_inc(irq_time_seq
.sequence
);
1405 static inline u64
irq_time_read(int cpu
)
1411 seq
= read_seqcount_begin(&per_cpu(irq_time_seq
, cpu
));
1412 irq_time
= per_cpu(cpu_softirq_time
, cpu
) +
1413 per_cpu(cpu_hardirq_time
, cpu
);
1414 } while (read_seqcount_retry(&per_cpu(irq_time_seq
, cpu
), seq
));
1418 #else /* CONFIG_64BIT */
1419 static inline void irq_time_write_begin(void)
1423 static inline void irq_time_write_end(void)
1427 static inline u64
irq_time_read(int cpu
)
1429 return per_cpu(cpu_softirq_time
, cpu
) + per_cpu(cpu_hardirq_time
, cpu
);
1431 #endif /* CONFIG_64BIT */
1432 #endif /* CONFIG_IRQ_TIME_ACCOUNTING */
1435 static inline int rq_cpu(const struct rq
*rq
) { return rq
->cpu
; }
1437 static inline int rq_cpu(const struct rq
*rq
) { return 0; }
1440 static inline void account_reset_rq(struct rq
*rq
)
1442 #ifdef CONFIG_IRQ_TIME_ACCOUNTING
1443 rq
->prev_irq_time
= 0;
1445 #ifdef CONFIG_PARAVIRT
1446 rq
->prev_steal_time
= 0;
1448 #ifdef CONFIG_PARAVIRT_TIME_ACCOUNTING
1449 rq
->prev_steal_time_rq
= 0;