1 /* SPDX-License-Identifier: GPL-2.0 */
3 #include <linux/sched.h>
4 #include <linux/sched/autogroup.h>
5 #include <linux/sched/sysctl.h>
6 #include <linux/sched/topology.h>
7 #include <linux/sched/rt.h>
8 #include <linux/sched/deadline.h>
9 #include <linux/sched/clock.h>
10 #include <linux/sched/wake_q.h>
11 #include <linux/sched/signal.h>
12 #include <linux/sched/numa_balancing.h>
13 #include <linux/sched/mm.h>
14 #include <linux/sched/cpufreq.h>
15 #include <linux/sched/stat.h>
16 #include <linux/sched/nohz.h>
17 #include <linux/sched/debug.h>
18 #include <linux/sched/hotplug.h>
19 #include <linux/sched/task.h>
20 #include <linux/sched/task_stack.h>
21 #include <linux/sched/cputime.h>
22 #include <linux/sched/init.h>
23 #include <linux/sched/smt.h>
25 #include <linux/u64_stats_sync.h>
26 #include <linux/kernel_stat.h>
27 #include <linux/binfmts.h>
28 #include <linux/mutex.h>
29 #include <linux/spinlock.h>
30 #include <linux/stop_machine.h>
31 #include <linux/irq_work.h>
32 #include <linux/tick.h>
33 #include <linux/slab.h>
35 #ifdef CONFIG_PARAVIRT
36 #include <asm/paravirt.h>
40 #include "cpudeadline.h"
43 #ifdef CONFIG_SCHED_DEBUG
44 # define SCHED_WARN_ON(x) WARN_ONCE(x, #x)
46 # define SCHED_WARN_ON(x) ({ (void)(x), 0; })
52 /* task_struct::on_rq states: */
53 #define TASK_ON_RQ_QUEUED 1
54 #define TASK_ON_RQ_MIGRATING 2
56 extern __read_mostly int scheduler_running;
58 extern unsigned long calc_load_update;
59 extern atomic_long_t calc_load_tasks;
61 extern void calc_global_load_tick(struct rq *this_rq);
62 extern long calc_load_fold_active(struct rq *this_rq, long adjust);
65 extern void cpu_load_update_active(struct rq *this_rq);
67 static inline void cpu_load_update_active(struct rq *this_rq) { }
71 * Helpers for converting nanosecond timing to jiffy resolution
73 #define NS_TO_JIFFIES(TIME) ((unsigned long)(TIME) / (NSEC_PER_SEC / HZ))
76 * Increase resolution of nice-level calculations for 64-bit architectures.
77 * The extra resolution improves shares distribution and load balancing of
78 * low-weight task groups (eg. nice +19 on an autogroup), deeper taskgroup
79 * hierarchies, especially on larger systems. This is not a user-visible change
80 * and does not change the user-interface for setting shares/weights.
82 * We increase resolution only if we have enough bits to allow this increased
83 * resolution (i.e. 64bit). The costs for increasing resolution when 32bit are
84 * pretty high and the returns do not justify the increased costs.
86 * Really only required when CONFIG_FAIR_GROUP_SCHED is also set, but to
87 * increase coverage and consistency always enable it on 64bit platforms.
90 # define NICE_0_LOAD_SHIFT (SCHED_FIXEDPOINT_SHIFT + SCHED_FIXEDPOINT_SHIFT)
91 # define scale_load(w) ((w) << SCHED_FIXEDPOINT_SHIFT)
92 # define scale_load_down(w) \
94 unsigned long __w = (w); \
96 __w = max(2UL, __w >> SCHED_FIXEDPOINT_SHIFT); \
100 # define NICE_0_LOAD_SHIFT (SCHED_FIXEDPOINT_SHIFT)
101 # define scale_load(w) (w)
102 # define scale_load_down(w) (w)
106 * Task weight (visible to users) and its load (invisible to users) have
107 * independent resolution, but they should be well calibrated. We use
108 * scale_load() and scale_load_down(w) to convert between them. The
109 * following must be true:
111 * scale_load(sched_prio_to_weight[USER_PRIO(NICE_TO_PRIO(0))]) == NICE_0_LOAD
114 #define NICE_0_LOAD (1L << NICE_0_LOAD_SHIFT)
117 * Single value that decides SCHED_DEADLINE internal math precision.
118 * 10 -> just above 1us
119 * 9 -> just above 0.5us
121 #define DL_SCALE (10)
124 * These are the 'tuning knobs' of the scheduler:
128 * single value that denotes runtime == period, ie unlimited time.
130 #define RUNTIME_INF ((u64)~0ULL)
132 static inline int idle_policy(int policy)
134 return policy == SCHED_IDLE;
136 static inline int fair_policy(int policy)
138 return policy == SCHED_NORMAL || policy == SCHED_BATCH;
141 static inline int rt_policy(int policy)
143 return policy == SCHED_FIFO || policy == SCHED_RR;
146 static inline int dl_policy(int policy)
148 return policy == SCHED_DEADLINE;
150 static inline bool valid_policy(int policy)
152 return idle_policy(policy) || fair_policy(policy) ||
153 rt_policy(policy) || dl_policy(policy);
156 static inline int task_has_rt_policy(struct task_struct *p)
158 return rt_policy(p->policy);
161 static inline int task_has_dl_policy(struct task_struct *p)
163 return dl_policy(p->policy);
167 * Tells if entity @a should preempt entity @b.
170 dl_entity_preempt(struct sched_dl_entity *a, struct sched_dl_entity *b)
172 return dl_time_before(a->deadline, b->deadline);
176 * This is the priority-queue data structure of the RT scheduling class:
178 struct rt_prio_array {
179 DECLARE_BITMAP(bitmap, MAX_RT_PRIO+1); /* include 1 bit for delimiter */
180 struct list_head queue[MAX_RT_PRIO];
183 struct rt_bandwidth {
184 /* nests inside the rq lock: */
185 raw_spinlock_t rt_runtime_lock;
188 struct hrtimer rt_period_timer;
189 unsigned int rt_period_active;
192 void __dl_clear_params(struct task_struct *p);
194 struct dl_bandwidth {
195 raw_spinlock_t dl_runtime_lock;
200 static inline int dl_bandwidth_enabled(void)
202 return sysctl_sched_rt_runtime >= 0;
206 * To keep the bandwidth of -deadline tasks under control
207 * we need some place where:
208 * - store the maximum -deadline bandwidth of each cpu;
209 * - cache the fraction of bandwidth that is currently allocated in
212 * This is all done in the data structure below. It is similar to the
213 * one used for RT-throttling (rt_bandwidth), with the main difference
214 * that, since here we are only interested in admission control, we
215 * do not decrease any runtime while the group "executes", neither we
216 * need a timer to replenish it.
218 * With respect to SMP, bandwidth is given on a per root domain basis,
220 * - bw (< 100%) is the deadline bandwidth of each CPU;
221 * - total_bw is the currently allocated bandwidth in each root domain;
228 static inline void __dl_update(struct dl_bw *dl_b, s64 bw);
231 void __dl_clear(struct dl_bw *dl_b, u64 tsk_bw, int cpus)
233 dl_b->total_bw -= tsk_bw;
234 __dl_update(dl_b, (s32)tsk_bw / cpus);
238 void __dl_add(struct dl_bw *dl_b, u64 tsk_bw, int cpus)
240 dl_b->total_bw += tsk_bw;
241 __dl_update(dl_b, -((s32)tsk_bw / cpus));
245 bool __dl_overflow(struct dl_bw *dl_b, int cpus, u64 old_bw, u64 new_bw)
247 return dl_b->bw != -1 &&
248 dl_b->bw * cpus < dl_b->total_bw - old_bw + new_bw;
251 void dl_change_utilization(struct task_struct *p, u64 new_bw);
252 extern void init_dl_bw(struct dl_bw *dl_b);
253 extern int sched_dl_global_validate(void);
254 extern void sched_dl_do_global(void);
255 extern int sched_dl_overflow(struct task_struct *p, int policy,
256 const struct sched_attr *attr);
257 extern void __setparam_dl(struct task_struct *p, const struct sched_attr *attr);
258 extern void __getparam_dl(struct task_struct *p, struct sched_attr *attr);
259 extern bool __checkparam_dl(const struct sched_attr *attr);
260 extern void __dl_clear_params(struct task_struct *p);
261 extern bool dl_param_changed(struct task_struct *p, const struct sched_attr *attr);
262 extern int dl_task_can_attach(struct task_struct *p,
263 const struct cpumask *cs_cpus_allowed);
264 extern int dl_cpuset_cpumask_can_shrink(const struct cpumask *cur,
265 const struct cpumask *trial);
266 extern bool dl_cpu_busy(unsigned int cpu);
268 #ifdef CONFIG_CGROUP_SCHED
270 #include <linux/cgroup.h>
275 extern struct list_head task_groups;
277 struct cfs_bandwidth {
278 #ifdef CONFIG_CFS_BANDWIDTH
282 s64 hierarchical_quota;
284 short idle, period_active;
285 struct hrtimer period_timer, slack_timer;
286 struct list_head throttled_cfs_rq;
289 int nr_periods, nr_throttled;
292 bool distribute_running;
296 /* task group related information */
298 struct cgroup_subsys_state css;
300 #ifdef CONFIG_FAIR_GROUP_SCHED
301 /* schedulable entities of this group on each cpu */
302 struct sched_entity **se;
303 /* runqueue "owned" by this group on each cpu */
304 struct cfs_rq **cfs_rq;
305 unsigned long shares;
309 * load_avg can be heavily contended at clock tick time, so put
310 * it in its own cacheline separated from the fields above which
311 * will also be accessed at each tick.
313 atomic_long_t load_avg ____cacheline_aligned;
317 #ifdef CONFIG_RT_GROUP_SCHED
318 struct sched_rt_entity **rt_se;
319 struct rt_rq **rt_rq;
321 struct rt_bandwidth rt_bandwidth;
325 struct list_head list;
327 struct task_group *parent;
328 struct list_head siblings;
329 struct list_head children;
331 #ifdef CONFIG_SCHED_AUTOGROUP
332 struct autogroup *autogroup;
335 struct cfs_bandwidth cfs_bandwidth;
338 #ifdef CONFIG_FAIR_GROUP_SCHED
339 #define ROOT_TASK_GROUP_LOAD NICE_0_LOAD
342 * A weight of 0 or 1 can cause arithmetics problems.
343 * A weight of a cfs_rq is the sum of weights of which entities
344 * are queued on this cfs_rq, so a weight of a entity should not be
345 * too large, so as the shares value of a task group.
346 * (The default weight is 1024 - so there's no practical
347 * limitation from this.)
349 #define MIN_SHARES (1UL << 1)
350 #define MAX_SHARES (1UL << 18)
353 typedef int (*tg_visitor)(struct task_group *, void *);
355 extern int walk_tg_tree_from(struct task_group *from,
356 tg_visitor down, tg_visitor up, void *data);
359 * Iterate the full tree, calling @down when first entering a node and @up when
360 * leaving it for the final time.
362 * Caller must hold rcu_lock or sufficient equivalent.
364 static inline int walk_tg_tree(tg_visitor down, tg_visitor up, void *data)
366 return walk_tg_tree_from(&root_task_group, down, up, data);
369 extern int tg_nop(struct task_group *tg, void *data);
371 extern void free_fair_sched_group(struct task_group *tg);
372 extern int alloc_fair_sched_group(struct task_group *tg, struct task_group *parent);
373 extern void online_fair_sched_group(struct task_group *tg);
374 extern void unregister_fair_sched_group(struct task_group *tg);
375 extern void init_tg_cfs_entry(struct task_group *tg, struct cfs_rq *cfs_rq,
376 struct sched_entity *se, int cpu,
377 struct sched_entity *parent);
378 extern void init_cfs_bandwidth(struct cfs_bandwidth *cfs_b);
380 extern void __refill_cfs_bandwidth_runtime(struct cfs_bandwidth *cfs_b);
381 extern void start_cfs_bandwidth(struct cfs_bandwidth *cfs_b);
382 extern void unthrottle_cfs_rq(struct cfs_rq *cfs_rq);
384 extern void free_rt_sched_group(struct task_group *tg);
385 extern int alloc_rt_sched_group(struct task_group *tg, struct task_group *parent);
386 extern void init_tg_rt_entry(struct task_group *tg, struct rt_rq *rt_rq,
387 struct sched_rt_entity *rt_se, int cpu,
388 struct sched_rt_entity *parent);
389 extern int sched_group_set_rt_runtime(struct task_group *tg, long rt_runtime_us);
390 extern int sched_group_set_rt_period(struct task_group *tg, u64 rt_period_us);
391 extern long sched_group_rt_runtime(struct task_group *tg);
392 extern long sched_group_rt_period(struct task_group *tg);
393 extern int sched_rt_can_attach(struct task_group *tg, struct task_struct *tsk);
395 extern struct task_group *sched_create_group(struct task_group *parent);
396 extern void sched_online_group(struct task_group *tg,
397 struct task_group *parent);
398 extern void sched_destroy_group(struct task_group *tg);
399 extern void sched_offline_group(struct task_group *tg);
401 extern void sched_move_task(struct task_struct *tsk);
403 #ifdef CONFIG_FAIR_GROUP_SCHED
404 extern int sched_group_set_shares(struct task_group *tg, unsigned long shares);
407 extern void set_task_rq_fair(struct sched_entity *se,
408 struct cfs_rq *prev, struct cfs_rq *next);
409 #else /* !CONFIG_SMP */
410 static inline void set_task_rq_fair(struct sched_entity *se,
411 struct cfs_rq *prev, struct cfs_rq *next) { }
412 #endif /* CONFIG_SMP */
413 #endif /* CONFIG_FAIR_GROUP_SCHED */
415 #else /* CONFIG_CGROUP_SCHED */
417 struct cfs_bandwidth { };
419 #endif /* CONFIG_CGROUP_SCHED */
421 /* CFS-related fields in a runqueue */
423 struct load_weight load;
424 unsigned int nr_running, h_nr_running;
429 u64 min_vruntime_copy;
432 struct rb_root_cached tasks_timeline;
435 * 'curr' points to currently running entity on this cfs_rq.
436 * It is set to NULL otherwise (i.e when none are currently running).
438 struct sched_entity *curr, *next, *last, *skip;
440 #ifdef CONFIG_SCHED_DEBUG
441 unsigned int nr_spread_over;
448 struct sched_avg avg;
449 u64 runnable_load_sum;
450 unsigned long runnable_load_avg;
451 #ifdef CONFIG_FAIR_GROUP_SCHED
452 unsigned long tg_load_avg_contrib;
453 unsigned long propagate_avg;
455 atomic_long_t removed_load_avg, removed_util_avg;
457 u64 load_last_update_time_copy;
460 #ifdef CONFIG_FAIR_GROUP_SCHED
462 * h_load = weight * f(tg)
464 * Where f(tg) is the recursive weight fraction assigned to
467 unsigned long h_load;
468 u64 last_h_load_update;
469 struct sched_entity *h_load_next;
470 #endif /* CONFIG_FAIR_GROUP_SCHED */
471 #endif /* CONFIG_SMP */
473 #ifdef CONFIG_FAIR_GROUP_SCHED
474 struct rq *rq; /* cpu runqueue to which this cfs_rq is attached */
477 * leaf cfs_rqs are those that hold tasks (lowest schedulable entity in
478 * a hierarchy). Non-leaf lrqs hold other higher schedulable entities
479 * (like users, containers etc.)
481 * leaf_cfs_rq_list ties together list of leaf cfs_rq's in a cpu. This
482 * list is used during load balance.
485 struct list_head leaf_cfs_rq_list;
486 struct task_group *tg; /* group that "owns" this runqueue */
488 #ifdef CONFIG_CFS_BANDWIDTH
490 s64 runtime_remaining;
492 u64 throttled_clock, throttled_clock_task;
493 u64 throttled_clock_task_time;
494 int throttled, throttle_count;
495 struct list_head throttled_list;
496 #endif /* CONFIG_CFS_BANDWIDTH */
497 #endif /* CONFIG_FAIR_GROUP_SCHED */
500 static inline int rt_bandwidth_enabled(void)
502 return sysctl_sched_rt_runtime >= 0;
505 /* RT IPI pull logic requires IRQ_WORK */
506 #if defined(CONFIG_IRQ_WORK) && defined(CONFIG_SMP)
507 # define HAVE_RT_PUSH_IPI
510 /* Real-Time classes' related field in a runqueue: */
512 struct rt_prio_array active;
513 unsigned int rt_nr_running;
514 unsigned int rr_nr_running;
515 #if defined CONFIG_SMP || defined CONFIG_RT_GROUP_SCHED
517 int curr; /* highest queued rt task prio */
519 int next; /* next highest */
524 unsigned long rt_nr_migratory;
525 unsigned long rt_nr_total;
527 struct plist_head pushable_tasks;
528 #endif /* CONFIG_SMP */
534 /* Nests inside the rq lock: */
535 raw_spinlock_t rt_runtime_lock;
537 #ifdef CONFIG_RT_GROUP_SCHED
538 unsigned long rt_nr_boosted;
541 struct task_group *tg;
545 /* Deadline class' related fields in a runqueue */
547 /* runqueue is an rbtree, ordered by deadline */
548 struct rb_root_cached root;
550 unsigned long dl_nr_running;
554 * Deadline values of the currently executing and the
555 * earliest ready task on this rq. Caching these facilitates
556 * the decision wether or not a ready but not running task
557 * should migrate somewhere else.
564 unsigned long dl_nr_migratory;
568 * Tasks on this rq that can be pushed away. They are kept in
569 * an rb-tree, ordered by tasks' deadlines, with caching
570 * of the leftmost (earliest deadline) element.
572 struct rb_root_cached pushable_dl_tasks_root;
577 * "Active utilization" for this runqueue: increased when a
578 * task wakes up (becomes TASK_RUNNING) and decreased when a
584 * Utilization of the tasks "assigned" to this runqueue (including
585 * the tasks that are in runqueue and the tasks that executed on this
586 * CPU and blocked). Increased when a task moves to this runqueue, and
587 * decreased when the task moves away (migrates, changes scheduling
588 * policy, or terminates).
589 * This is needed to compute the "inactive utilization" for the
590 * runqueue (inactive utilization = this_bw - running_bw).
596 * Inverse of the fraction of CPU utilization that can be reclaimed
597 * by the GRUB algorithm.
604 static inline bool sched_asym_prefer(int a, int b)
606 return arch_asym_cpu_priority(a) > arch_asym_cpu_priority(b);
610 * We add the notion of a root-domain which will be used to define per-domain
611 * variables. Each exclusive cpuset essentially defines an island domain by
612 * fully partitioning the member cpus from any other cpuset. Whenever a new
613 * exclusive cpuset is created, we also create and attach a new root-domain
622 cpumask_var_t online;
624 /* Indicate more than one runnable task for any CPU */
628 * The bit corresponding to a CPU gets set here if such CPU has more
629 * than one runnable -deadline task (as it is below for RT tasks).
631 cpumask_var_t dlo_mask;
636 #ifdef HAVE_RT_PUSH_IPI
638 * For IPI pull requests, loop across the rto_mask.
640 struct irq_work rto_push_work;
641 raw_spinlock_t rto_lock;
642 /* These are only updated and read within rto_lock */
645 /* These atomics are updated outside of a lock */
646 atomic_t rto_loop_next;
647 atomic_t rto_loop_start;
650 * The "RT overload" flag: it gets set if a CPU has more than
651 * one runnable RT task.
653 cpumask_var_t rto_mask;
654 struct cpupri cpupri;
656 unsigned long max_cpu_capacity;
659 extern struct root_domain def_root_domain;
660 extern struct mutex sched_domains_mutex;
662 extern void init_defrootdomain(void);
663 extern int sched_init_domains(const struct cpumask *cpu_map);
664 extern void rq_attach_root(struct rq *rq, struct root_domain *rd);
665 extern void sched_get_rd(struct root_domain *rd);
666 extern void sched_put_rd(struct root_domain *rd);
668 #ifdef HAVE_RT_PUSH_IPI
669 extern void rto_push_irq_work_func(struct irq_work *work);
671 #endif /* CONFIG_SMP */
674 * This is the main, per-CPU runqueue data structure.
676 * Locking rule: those places that want to lock multiple runqueues
677 * (such as the load balancing or the thread migration code), lock
678 * acquire operations must be ordered by ascending &runqueue.
685 * nr_running and cpu_load should be in the same cacheline because
686 * remote CPUs use both these fields when doing load calculation.
688 unsigned int nr_running;
689 #ifdef CONFIG_NUMA_BALANCING
690 unsigned int nr_numa_running;
691 unsigned int nr_preferred_running;
693 #define CPU_LOAD_IDX_MAX 5
694 unsigned long cpu_load[CPU_LOAD_IDX_MAX];
695 #ifdef CONFIG_NO_HZ_COMMON
697 unsigned long last_load_update_tick;
698 #endif /* CONFIG_SMP */
699 unsigned long nohz_flags;
700 #endif /* CONFIG_NO_HZ_COMMON */
701 #ifdef CONFIG_NO_HZ_FULL
702 unsigned long last_sched_tick;
704 /* capture load from *all* tasks on this cpu: */
705 struct load_weight load;
706 unsigned long nr_load_updates;
713 #ifdef CONFIG_FAIR_GROUP_SCHED
714 /* list of leaf cfs_rq on this cpu: */
715 struct list_head leaf_cfs_rq_list;
716 struct list_head *tmp_alone_branch;
717 #endif /* CONFIG_FAIR_GROUP_SCHED */
720 * This is part of a global counter where only the total sum
721 * over all CPUs matters. A task can increase this counter on
722 * one CPU and if it got migrated afterwards it may decrease
723 * it on another CPU. Always updated under the runqueue lock:
725 unsigned long nr_uninterruptible;
727 struct task_struct *curr, *idle, *stop;
728 unsigned long next_balance;
729 struct mm_struct *prev_mm;
731 unsigned int clock_update_flags;
738 struct root_domain *rd;
739 struct sched_domain *sd;
741 unsigned long cpu_capacity;
742 unsigned long cpu_capacity_orig;
744 struct callback_head *balance_callback;
746 unsigned char idle_balance;
747 /* For active balancing */
750 struct cpu_stop_work active_balance_work;
751 /* cpu of this runqueue: */
755 struct list_head cfs_tasks;
762 /* This is used to determine avg_idle's max value */
763 u64 max_idle_balance_cost;
766 #ifdef CONFIG_IRQ_TIME_ACCOUNTING
769 #ifdef CONFIG_PARAVIRT
772 #ifdef CONFIG_PARAVIRT_TIME_ACCOUNTING
773 u64 prev_steal_time_rq;
776 /* calc_load related fields */
777 unsigned long calc_load_update;
778 long calc_load_active;
780 #ifdef CONFIG_SCHED_HRTICK
782 int hrtick_csd_pending;
783 call_single_data_t hrtick_csd;
785 struct hrtimer hrtick_timer;
788 #ifdef CONFIG_SCHEDSTATS
790 struct sched_info rq_sched_info;
791 unsigned long long rq_cpu_time;
792 /* could above be rq->cfs_rq.exec_clock + rq->rt_rq.rt_runtime ? */
794 /* sys_sched_yield() stats */
795 unsigned int yld_count;
797 /* schedule() stats */
798 unsigned int sched_count;
799 unsigned int sched_goidle;
801 /* try_to_wake_up() stats */
802 unsigned int ttwu_count;
803 unsigned int ttwu_local;
807 struct llist_head wake_list;
810 #ifdef CONFIG_CPU_IDLE
811 /* Must be inspected within a rcu lock section */
812 struct cpuidle_state *idle_state;
816 static inline int cpu_of(struct rq *rq)
826 #ifdef CONFIG_SCHED_SMT
827 extern void __update_idle_core(struct rq *rq);
829 static inline void update_idle_core(struct rq *rq)
831 if (static_branch_unlikely(&sched_smt_present))
832 __update_idle_core(rq);
836 static inline void update_idle_core(struct rq *rq) { }
839 DECLARE_PER_CPU_SHARED_ALIGNED(struct rq, runqueues);
841 #define cpu_rq(cpu) (&per_cpu(runqueues, (cpu)))
842 #define this_rq() this_cpu_ptr(&runqueues)
843 #define task_rq(p) cpu_rq(task_cpu(p))
844 #define cpu_curr(cpu) (cpu_rq(cpu)->curr)
845 #define raw_rq() raw_cpu_ptr(&runqueues)
847 static inline u64 __rq_clock_broken(struct rq *rq)
849 return READ_ONCE(rq->clock);
853 * rq::clock_update_flags bits
855 * %RQCF_REQ_SKIP - will request skipping of clock update on the next
856 * call to __schedule(). This is an optimisation to avoid
857 * neighbouring rq clock updates.
859 * %RQCF_ACT_SKIP - is set from inside of __schedule() when skipping is
860 * in effect and calls to update_rq_clock() are being ignored.
862 * %RQCF_UPDATED - is a debug flag that indicates whether a call has been
863 * made to update_rq_clock() since the last time rq::lock was pinned.
865 * If inside of __schedule(), clock_update_flags will have been
866 * shifted left (a left shift is a cheap operation for the fast path
867 * to promote %RQCF_REQ_SKIP to %RQCF_ACT_SKIP), so you must use,
869 * if (rq-clock_update_flags >= RQCF_UPDATED)
871 * to check if %RQCF_UPADTED is set. It'll never be shifted more than
872 * one position though, because the next rq_unpin_lock() will shift it
875 #define RQCF_REQ_SKIP 0x01
876 #define RQCF_ACT_SKIP 0x02
877 #define RQCF_UPDATED 0x04
879 static inline void assert_clock_updated(struct rq *rq)
882 * The only reason for not seeing a clock update since the
883 * last rq_pin_lock() is if we're currently skipping updates.
885 SCHED_WARN_ON(rq->clock_update_flags < RQCF_ACT_SKIP);
888 static inline u64 rq_clock(struct rq *rq)
890 lockdep_assert_held(&rq->lock);
891 assert_clock_updated(rq);
896 static inline u64 rq_clock_task(struct rq *rq)
898 lockdep_assert_held(&rq->lock);
899 assert_clock_updated(rq);
901 return rq->clock_task;
904 static inline void rq_clock_skip_update(struct rq *rq, bool skip)
906 lockdep_assert_held(&rq->lock);
908 rq->clock_update_flags |= RQCF_REQ_SKIP;
910 rq->clock_update_flags &= ~RQCF_REQ_SKIP;
915 struct pin_cookie cookie;
916 #ifdef CONFIG_SCHED_DEBUG
918 * A copy of (rq::clock_update_flags & RQCF_UPDATED) for the
919 * current pin context is stashed here in case it needs to be
920 * restored in rq_repin_lock().
922 unsigned int clock_update_flags;
926 static inline void rq_pin_lock(struct rq *rq, struct rq_flags *rf)
928 rf->cookie = lockdep_pin_lock(&rq->lock);
930 #ifdef CONFIG_SCHED_DEBUG
931 rq->clock_update_flags &= (RQCF_REQ_SKIP|RQCF_ACT_SKIP);
932 rf->clock_update_flags = 0;
936 static inline void rq_unpin_lock(struct rq *rq, struct rq_flags *rf)
938 #ifdef CONFIG_SCHED_DEBUG
939 if (rq->clock_update_flags > RQCF_ACT_SKIP)
940 rf->clock_update_flags = RQCF_UPDATED;
943 lockdep_unpin_lock(&rq->lock, rf->cookie);
946 static inline void rq_repin_lock(struct rq *rq, struct rq_flags *rf)
948 lockdep_repin_lock(&rq->lock, rf->cookie);
950 #ifdef CONFIG_SCHED_DEBUG
952 * Restore the value we stashed in @rf for this pin context.
954 rq->clock_update_flags |= rf->clock_update_flags;
959 enum numa_topology_type {
964 extern enum numa_topology_type sched_numa_topology_type;
965 extern int sched_max_numa_distance;
966 extern bool find_numa_distance(int distance);
970 extern void sched_init_numa(void);
971 extern void sched_domains_numa_masks_set(unsigned int cpu);
972 extern void sched_domains_numa_masks_clear(unsigned int cpu);
974 static inline void sched_init_numa(void) { }
975 static inline void sched_domains_numa_masks_set(unsigned int cpu) { }
976 static inline void sched_domains_numa_masks_clear(unsigned int cpu) { }
979 #ifdef CONFIG_NUMA_BALANCING
980 /* The regions in numa_faults array from task_struct */
981 enum numa_faults_stats {
987 extern void sched_setnuma(struct task_struct *p, int node);
988 extern int migrate_task_to(struct task_struct *p, int cpu);
989 extern int migrate_swap(struct task_struct *, struct task_struct *);
990 #endif /* CONFIG_NUMA_BALANCING */
995 queue_balance_callback(struct rq *rq,
996 struct callback_head *head,
997 void (*func)(struct rq *rq))
999 lockdep_assert_held(&rq->lock);
1001 if (unlikely(head->next))
1004 head->func = (void (*)(struct callback_head *))func;
1005 head->next = rq->balance_callback;
1006 rq->balance_callback = head;
1009 extern void sched_ttwu_pending(void);
1011 #define rcu_dereference_check_sched_domain(p) \
1012 rcu_dereference_check((p), \
1013 lockdep_is_held(&sched_domains_mutex))
1016 * The domain tree (rq->sd) is protected by RCU's quiescent state transition.
1017 * See detach_destroy_domains: synchronize_sched for details.
1019 * The domain tree of any CPU may only be accessed from within
1020 * preempt-disabled sections.
1022 #define for_each_domain(cpu, __sd) \
1023 for (__sd = rcu_dereference_check_sched_domain(cpu_rq(cpu)->sd); \
1024 __sd; __sd = __sd->parent)
1026 #define for_each_lower_domain(sd) for (; sd; sd = sd->child)
1029 * highest_flag_domain - Return highest sched_domain containing flag.
1030 * @cpu: The cpu whose highest level of sched domain is to
1032 * @flag: The flag to check for the highest sched_domain
1033 * for the given cpu.
1035 * Returns the highest sched_domain of a cpu which contains the given flag.
1037 static inline struct sched_domain *highest_flag_domain(int cpu, int flag)
1039 struct sched_domain *sd, *hsd = NULL;
1041 for_each_domain(cpu, sd) {
1042 if (!(sd->flags & flag))
1050 static inline struct sched_domain *lowest_flag_domain(int cpu, int flag)
1052 struct sched_domain *sd;
1054 for_each_domain(cpu, sd) {
1055 if (sd->flags & flag)
1062 DECLARE_PER_CPU(struct sched_domain *, sd_llc);
1063 DECLARE_PER_CPU(int, sd_llc_size);
1064 DECLARE_PER_CPU(int, sd_llc_id);
1065 DECLARE_PER_CPU(struct sched_domain_shared *, sd_llc_shared);
1066 DECLARE_PER_CPU(struct sched_domain *, sd_numa);
1067 DECLARE_PER_CPU(struct sched_domain *, sd_asym);
1069 struct sched_group_capacity {
1072 * CPU capacity of this group, SCHED_CAPACITY_SCALE being max capacity
1075 unsigned long capacity;
1076 unsigned long min_capacity; /* Min per-CPU capacity in group */
1077 unsigned long next_update;
1078 int imbalance; /* XXX unrelated to capacity but shared group state */
1080 #ifdef CONFIG_SCHED_DEBUG
1084 unsigned long cpumask[0]; /* balance mask */
1087 struct sched_group {
1088 struct sched_group *next; /* Must be a circular list */
1091 unsigned int group_weight;
1092 struct sched_group_capacity *sgc;
1093 int asym_prefer_cpu; /* cpu of highest priority in group */
1096 * The CPUs this group covers.
1098 * NOTE: this field is variable length. (Allocated dynamically
1099 * by attaching extra space to the end of the structure,
1100 * depending on how many CPUs the kernel has booted up with)
1102 unsigned long cpumask[0];
1105 static inline struct cpumask *sched_group_span(struct sched_group *sg)
1107 return to_cpumask(sg->cpumask);
1111 * See build_balance_mask().
1113 static inline struct cpumask *group_balance_mask(struct sched_group *sg)
1115 return to_cpumask(sg->sgc->cpumask);
1119 * group_first_cpu - Returns the first cpu in the cpumask of a sched_group.
1120 * @group: The group whose first cpu is to be returned.
1122 static inline unsigned int group_first_cpu(struct sched_group *group)
1124 return cpumask_first(sched_group_span(group));
1127 extern int group_balance_cpu(struct sched_group *sg);
1129 #if defined(CONFIG_SCHED_DEBUG) && defined(CONFIG_SYSCTL)
1130 void register_sched_domain_sysctl(void);
1131 void dirty_sched_domain_sysctl(int cpu);
1132 void unregister_sched_domain_sysctl(void);
1134 static inline void register_sched_domain_sysctl(void)
1137 static inline void dirty_sched_domain_sysctl(int cpu)
1140 static inline void unregister_sched_domain_sysctl(void)
1147 static inline void sched_ttwu_pending(void) { }
1149 #endif /* CONFIG_SMP */
1152 #include "autogroup.h"
1154 #ifdef CONFIG_CGROUP_SCHED
1157 * Return the group to which this tasks belongs.
1159 * We cannot use task_css() and friends because the cgroup subsystem
1160 * changes that value before the cgroup_subsys::attach() method is called,
1161 * therefore we cannot pin it and might observe the wrong value.
1163 * The same is true for autogroup's p->signal->autogroup->tg, the autogroup
1164 * core changes this before calling sched_move_task().
1166 * Instead we use a 'copy' which is updated from sched_move_task() while
1167 * holding both task_struct::pi_lock and rq::lock.
1169 static inline struct task_group *task_group(struct task_struct *p)
1171 return p->sched_task_group;
1174 /* Change a task's cfs_rq and parent entity if it moves across CPUs/groups */
1175 static inline void set_task_rq(struct task_struct *p, unsigned int cpu)
1177 #if defined(CONFIG_FAIR_GROUP_SCHED) || defined(CONFIG_RT_GROUP_SCHED)
1178 struct task_group *tg = task_group(p);
1181 #ifdef CONFIG_FAIR_GROUP_SCHED
1182 set_task_rq_fair(&p->se, p->se.cfs_rq, tg->cfs_rq[cpu]);
1183 p->se.cfs_rq = tg->cfs_rq[cpu];
1184 p->se.parent = tg->se[cpu];
1187 #ifdef CONFIG_RT_GROUP_SCHED
1188 p->rt.rt_rq = tg->rt_rq[cpu];
1189 p->rt.parent = tg->rt_se[cpu];
1193 #else /* CONFIG_CGROUP_SCHED */
1195 static inline void set_task_rq(struct task_struct *p, unsigned int cpu) { }
1196 static inline struct task_group *task_group(struct task_struct *p)
1201 #endif /* CONFIG_CGROUP_SCHED */
1203 static inline void __set_task_cpu(struct task_struct *p, unsigned int cpu)
1205 set_task_rq(p, cpu);
1208 * After ->cpu is set up to a new value, task_rq_lock(p, ...) can be
1209 * successfuly executed on another CPU. We must ensure that updates of
1210 * per-task data have been completed by this moment.
1213 #ifdef CONFIG_THREAD_INFO_IN_TASK
1216 task_thread_info(p)->cpu = cpu;
1223 * Tunables that become constants when CONFIG_SCHED_DEBUG is off:
1225 #ifdef CONFIG_SCHED_DEBUG
1226 # include <linux/static_key.h>
1227 # define const_debug __read_mostly
1229 # define const_debug const
1232 extern const_debug unsigned int sysctl_sched_features;
1234 #define SCHED_FEAT(name, enabled) \
1235 __SCHED_FEAT_##name ,
1238 #include "features.h"
1244 #if defined(CONFIG_SCHED_DEBUG) && defined(HAVE_JUMP_LABEL)
1245 #define SCHED_FEAT(name, enabled) \
1246 static __always_inline bool static_branch_##name(struct static_key *key) \
1248 return static_key_##enabled(key); \
1251 #include "features.h"
1255 extern struct static_key sched_feat_keys[__SCHED_FEAT_NR];
1256 #define sched_feat(x) (static_branch_##x(&sched_feat_keys[__SCHED_FEAT_##x]))
1257 #else /* !(SCHED_DEBUG && HAVE_JUMP_LABEL) */
1258 #define sched_feat(x) (sysctl_sched_features & (1UL << __SCHED_FEAT_##x))
1259 #endif /* SCHED_DEBUG && HAVE_JUMP_LABEL */
1261 extern struct static_key_false sched_numa_balancing;
1262 extern struct static_key_false sched_schedstats;
1264 static inline u64 global_rt_period(void)
1266 return (u64)sysctl_sched_rt_period * NSEC_PER_USEC;
1269 static inline u64 global_rt_runtime(void)
1271 if (sysctl_sched_rt_runtime < 0)
1274 return (u64)sysctl_sched_rt_runtime * NSEC_PER_USEC;
1277 static inline int task_current(struct rq *rq, struct task_struct *p)
1279 return rq->curr == p;
1282 static inline int task_running(struct rq *rq, struct task_struct *p)
1287 return task_current(rq, p);
1291 static inline int task_on_rq_queued(struct task_struct *p)
1293 return p->on_rq == TASK_ON_RQ_QUEUED;
1296 static inline int task_on_rq_migrating(struct task_struct *p)
1298 return p->on_rq == TASK_ON_RQ_MIGRATING;
1301 #ifndef prepare_arch_switch
1302 # define prepare_arch_switch(next) do { } while (0)
1304 #ifndef finish_arch_post_lock_switch
1305 # define finish_arch_post_lock_switch() do { } while (0)
1308 static inline void prepare_lock_switch(struct rq *rq, struct task_struct *next)
1312 * We can optimise this out completely for !SMP, because the
1313 * SMP rebalancing from interrupt is the only thing that cares
1320 static inline void finish_lock_switch(struct rq *rq, struct task_struct *prev)
1324 * After ->on_cpu is cleared, the task can be moved to a different CPU.
1325 * We must ensure this doesn't happen until the switch is completely
1328 * In particular, the load of prev->state in finish_task_switch() must
1329 * happen before this.
1331 * Pairs with the smp_cond_load_acquire() in try_to_wake_up().
1333 smp_store_release(&prev->on_cpu, 0);
1335 #ifdef CONFIG_DEBUG_SPINLOCK
1336 /* this is a valid case when another task releases the spinlock */
1337 rq->lock.owner = current;
1340 * If we are tracking spinlock dependencies then we have to
1341 * fix up the runqueue lock - which gets 'carried over' from
1342 * prev into current:
1344 spin_acquire(&rq->lock.dep_map, 0, 0, _THIS_IP_);
1346 raw_spin_unlock_irq(&rq->lock);
1352 #define WF_SYNC 0x01 /* waker goes to sleep after wakeup */
1353 #define WF_FORK 0x02 /* child wakeup after fork */
1354 #define WF_MIGRATED 0x4 /* internal use, task got migrated */
1357 * To aid in avoiding the subversion of "niceness" due to uneven distribution
1358 * of tasks with abnormal "nice" values across CPUs the contribution that
1359 * each task makes to its run queue's load is weighted according to its
1360 * scheduling class and "nice" value. For SCHED_NORMAL tasks this is just a
1361 * scaled version of the new time slice allocation that they receive on time
1365 #define WEIGHT_IDLEPRIO 3
1366 #define WMULT_IDLEPRIO 1431655765
1368 extern const int sched_prio_to_weight[40];
1369 extern const u32 sched_prio_to_wmult[40];
1372 * {de,en}queue flags:
1374 * DEQUEUE_SLEEP - task is no longer runnable
1375 * ENQUEUE_WAKEUP - task just became runnable
1377 * SAVE/RESTORE - an otherwise spurious dequeue/enqueue, done to ensure tasks
1378 * are in a known state which allows modification. Such pairs
1379 * should preserve as much state as possible.
1381 * MOVE - paired with SAVE/RESTORE, explicitly does not preserve the location
1384 * ENQUEUE_HEAD - place at front of runqueue (tail if not specified)
1385 * ENQUEUE_REPLENISH - CBS (replenish runtime and postpone deadline)
1386 * ENQUEUE_MIGRATED - the task was migrated during wakeup
1390 #define DEQUEUE_SLEEP 0x01
1391 #define DEQUEUE_SAVE 0x02 /* matches ENQUEUE_RESTORE */
1392 #define DEQUEUE_MOVE 0x04 /* matches ENQUEUE_MOVE */
1393 #define DEQUEUE_NOCLOCK 0x08 /* matches ENQUEUE_NOCLOCK */
1395 #define ENQUEUE_WAKEUP 0x01
1396 #define ENQUEUE_RESTORE 0x02
1397 #define ENQUEUE_MOVE 0x04
1398 #define ENQUEUE_NOCLOCK 0x08
1400 #define ENQUEUE_HEAD 0x10
1401 #define ENQUEUE_REPLENISH 0x20
1403 #define ENQUEUE_MIGRATED 0x40
1405 #define ENQUEUE_MIGRATED 0x00
1408 #define RETRY_TASK ((void *)-1UL)
1410 struct sched_class {
1411 const struct sched_class *next;
1413 void (*enqueue_task) (struct rq *rq, struct task_struct *p, int flags);
1414 void (*dequeue_task) (struct rq *rq, struct task_struct *p, int flags);
1415 void (*yield_task) (struct rq *rq);
1416 bool (*yield_to_task) (struct rq *rq, struct task_struct *p, bool preempt);
1418 void (*check_preempt_curr) (struct rq *rq, struct task_struct *p, int flags);
1421 * It is the responsibility of the pick_next_task() method that will
1422 * return the next task to call put_prev_task() on the @prev task or
1423 * something equivalent.
1425 * May return RETRY_TASK when it finds a higher prio class has runnable
1428 struct task_struct * (*pick_next_task) (struct rq *rq,
1429 struct task_struct *prev,
1430 struct rq_flags *rf);
1431 void (*put_prev_task) (struct rq *rq, struct task_struct *p);
1434 int (*select_task_rq)(struct task_struct *p, int task_cpu, int sd_flag, int flags);
1435 void (*migrate_task_rq)(struct task_struct *p);
1437 void (*task_woken) (struct rq *this_rq, struct task_struct *task);
1439 void (*set_cpus_allowed)(struct task_struct *p,
1440 const struct cpumask *newmask);
1442 void (*rq_online)(struct rq *rq);
1443 void (*rq_offline)(struct rq *rq);
1446 void (*set_curr_task) (struct rq *rq);
1447 void (*task_tick) (struct rq *rq, struct task_struct *p, int queued);
1448 void (*task_fork) (struct task_struct *p);
1449 void (*task_dead) (struct task_struct *p);
1452 * The switched_from() call is allowed to drop rq->lock, therefore we
1453 * cannot assume the switched_from/switched_to pair is serliazed by
1454 * rq->lock. They are however serialized by p->pi_lock.
1456 void (*switched_from) (struct rq *this_rq, struct task_struct *task);
1457 void (*switched_to) (struct rq *this_rq, struct task_struct *task);
1458 void (*prio_changed) (struct rq *this_rq, struct task_struct *task,
1461 unsigned int (*get_rr_interval) (struct rq *rq,
1462 struct task_struct *task);
1464 void (*update_curr) (struct rq *rq);
1466 #define TASK_SET_GROUP 0
1467 #define TASK_MOVE_GROUP 1
1469 #ifdef CONFIG_FAIR_GROUP_SCHED
1470 void (*task_change_group) (struct task_struct *p, int type);
1474 static inline void put_prev_task(struct rq *rq, struct task_struct *prev)
1476 prev->sched_class->put_prev_task(rq, prev);
1479 static inline void set_curr_task(struct rq *rq, struct task_struct *curr)
1481 curr->sched_class->set_curr_task(rq);
1485 #define sched_class_highest (&stop_sched_class)
1487 #define sched_class_highest (&dl_sched_class)
1489 #define for_each_class(class) \
1490 for (class = sched_class_highest; class; class = class->next)
1492 extern const struct sched_class stop_sched_class;
1493 extern const struct sched_class dl_sched_class;
1494 extern const struct sched_class rt_sched_class;
1495 extern const struct sched_class fair_sched_class;
1496 extern const struct sched_class idle_sched_class;
1501 extern void update_group_capacity(struct sched_domain *sd, int cpu);
1503 extern void trigger_load_balance(struct rq *rq);
1505 extern void set_cpus_allowed_common(struct task_struct *p, const struct cpumask *new_mask);
1509 #ifdef CONFIG_CPU_IDLE
1510 static inline void idle_set_state(struct rq *rq,
1511 struct cpuidle_state *idle_state)
1513 rq->idle_state = idle_state;
1516 static inline struct cpuidle_state *idle_get_state(struct rq *rq)
1518 SCHED_WARN_ON(!rcu_read_lock_held());
1519 return rq->idle_state;
1522 static inline void idle_set_state(struct rq *rq,
1523 struct cpuidle_state *idle_state)
1527 static inline struct cpuidle_state *idle_get_state(struct rq *rq)
1533 extern void schedule_idle(void);
1535 extern void sysrq_sched_debug_show(void);
1536 extern void sched_init_granularity(void);
1537 extern void update_max_interval(void);
1539 extern void init_sched_dl_class(void);
1540 extern void init_sched_rt_class(void);
1541 extern void init_sched_fair_class(void);
1543 extern void resched_curr(struct rq *rq);
1544 extern void resched_cpu(int cpu);
1546 extern struct rt_bandwidth def_rt_bandwidth;
1547 extern void init_rt_bandwidth(struct rt_bandwidth *rt_b, u64 period, u64 runtime);
1549 extern struct dl_bandwidth def_dl_bandwidth;
1550 extern void init_dl_bandwidth(struct dl_bandwidth *dl_b, u64 period, u64 runtime);
1551 extern void init_dl_task_timer(struct sched_dl_entity *dl_se);
1552 extern void init_dl_inactive_task_timer(struct sched_dl_entity *dl_se);
1553 extern void init_dl_rq_bw_ratio(struct dl_rq *dl_rq);
1556 #define BW_UNIT (1 << BW_SHIFT)
1557 #define RATIO_SHIFT 8
1558 unsigned long to_ratio(u64 period, u64 runtime);
1560 extern void init_entity_runnable_average(struct sched_entity *se);
1561 extern void post_init_entity_util_avg(struct sched_entity *se);
1563 #ifdef CONFIG_NO_HZ_FULL
1564 extern bool sched_can_stop_tick(struct rq *rq);
1567 * Tick may be needed by tasks in the runqueue depending on their policy and
1568 * requirements. If tick is needed, lets send the target an IPI to kick it out of
1569 * nohz mode if necessary.
1571 static inline void sched_update_tick_dependency(struct rq *rq)
1575 if (!tick_nohz_full_enabled())
1580 if (!tick_nohz_full_cpu(cpu))
1583 if (sched_can_stop_tick(rq))
1584 tick_nohz_dep_clear_cpu(cpu, TICK_DEP_BIT_SCHED);
1586 tick_nohz_dep_set_cpu(cpu, TICK_DEP_BIT_SCHED);
1589 static inline void sched_update_tick_dependency(struct rq *rq) { }
1592 static inline void add_nr_running(struct rq *rq, unsigned count)
1594 unsigned prev_nr = rq->nr_running;
1596 rq->nr_running = prev_nr + count;
1598 if (prev_nr < 2 && rq->nr_running >= 2) {
1600 if (!rq->rd->overload)
1601 rq->rd->overload = true;
1605 sched_update_tick_dependency(rq);
1608 static inline void sub_nr_running(struct rq *rq, unsigned count)
1610 rq->nr_running -= count;
1611 /* Check if we still need preemption */
1612 sched_update_tick_dependency(rq);
1615 static inline void rq_last_tick_reset(struct rq *rq)
1617 #ifdef CONFIG_NO_HZ_FULL
1618 rq->last_sched_tick = jiffies;
1622 extern void update_rq_clock(struct rq *rq);
1624 extern void activate_task(struct rq *rq, struct task_struct *p, int flags);
1625 extern void deactivate_task(struct rq *rq, struct task_struct *p, int flags);
1627 extern void check_preempt_curr(struct rq *rq, struct task_struct *p, int flags);
1629 extern const_debug unsigned int sysctl_sched_time_avg;
1630 extern const_debug unsigned int sysctl_sched_nr_migrate;
1631 extern const_debug unsigned int sysctl_sched_migration_cost;
1633 static inline u64 sched_avg_period(void)
1635 return (u64)sysctl_sched_time_avg * NSEC_PER_MSEC / 2;
1638 #ifdef CONFIG_SCHED_HRTICK
1642 * - enabled by features
1643 * - hrtimer is actually high res
1645 static inline int hrtick_enabled(struct rq *rq)
1647 if (!sched_feat(HRTICK))
1649 if (!cpu_active(cpu_of(rq)))
1651 return hrtimer_is_hres_active(&rq->hrtick_timer);
1654 void hrtick_start(struct rq *rq, u64 delay);
1658 static inline int hrtick_enabled(struct rq *rq)
1663 #endif /* CONFIG_SCHED_HRTICK */
1666 extern void sched_avg_update(struct rq *rq);
1668 #ifndef arch_scale_freq_capacity
1669 static __always_inline
1670 unsigned long arch_scale_freq_capacity(struct sched_domain *sd, int cpu)
1672 return SCHED_CAPACITY_SCALE;
1676 #ifndef arch_scale_cpu_capacity
1677 static __always_inline
1678 unsigned long arch_scale_cpu_capacity(struct sched_domain *sd, int cpu)
1680 if (sd && (sd->flags & SD_SHARE_CPUCAPACITY) && (sd->span_weight > 1))
1681 return sd->smt_gain / sd->span_weight;
1683 return SCHED_CAPACITY_SCALE;
1687 static inline void sched_rt_avg_update(struct rq *rq, u64 rt_delta)
1689 rq->rt_avg += rt_delta * arch_scale_freq_capacity(NULL, cpu_of(rq));
1690 sched_avg_update(rq);
1693 static inline void sched_rt_avg_update(struct rq *rq, u64 rt_delta) { }
1694 static inline void sched_avg_update(struct rq *rq) { }
1697 struct rq *__task_rq_lock(struct task_struct *p, struct rq_flags *rf)
1698 __acquires(rq->lock);
1700 struct rq *task_rq_lock(struct task_struct *p, struct rq_flags *rf)
1701 __acquires(p->pi_lock)
1702 __acquires(rq->lock);
1704 static inline void __task_rq_unlock(struct rq *rq, struct rq_flags *rf)
1705 __releases(rq->lock)
1707 rq_unpin_lock(rq, rf);
1708 raw_spin_unlock(&rq->lock);
1712 task_rq_unlock(struct rq *rq, struct task_struct *p, struct rq_flags *rf)
1713 __releases(rq->lock)
1714 __releases(p->pi_lock)
1716 rq_unpin_lock(rq, rf);
1717 raw_spin_unlock(&rq->lock);
1718 raw_spin_unlock_irqrestore(&p->pi_lock, rf->flags);
1722 rq_lock_irqsave(struct rq *rq, struct rq_flags *rf)
1723 __acquires(rq->lock)
1725 raw_spin_lock_irqsave(&rq->lock, rf->flags);
1726 rq_pin_lock(rq, rf);
1730 rq_lock_irq(struct rq *rq, struct rq_flags *rf)
1731 __acquires(rq->lock)
1733 raw_spin_lock_irq(&rq->lock);
1734 rq_pin_lock(rq, rf);
1738 rq_lock(struct rq *rq, struct rq_flags *rf)
1739 __acquires(rq->lock)
1741 raw_spin_lock(&rq->lock);
1742 rq_pin_lock(rq, rf);
1746 rq_relock(struct rq *rq, struct rq_flags *rf)
1747 __acquires(rq->lock)
1749 raw_spin_lock(&rq->lock);
1750 rq_repin_lock(rq, rf);
1754 rq_unlock_irqrestore(struct rq *rq, struct rq_flags *rf)
1755 __releases(rq->lock)
1757 rq_unpin_lock(rq, rf);
1758 raw_spin_unlock_irqrestore(&rq->lock, rf->flags);
1762 rq_unlock_irq(struct rq *rq, struct rq_flags *rf)
1763 __releases(rq->lock)
1765 rq_unpin_lock(rq, rf);
1766 raw_spin_unlock_irq(&rq->lock);
1770 rq_unlock(struct rq *rq, struct rq_flags *rf)
1771 __releases(rq->lock)
1773 rq_unpin_lock(rq, rf);
1774 raw_spin_unlock(&rq->lock);
1778 #ifdef CONFIG_PREEMPT
1780 static inline void double_rq_lock(struct rq *rq1, struct rq *rq2);
1783 * fair double_lock_balance: Safely acquires both rq->locks in a fair
1784 * way at the expense of forcing extra atomic operations in all
1785 * invocations. This assures that the double_lock is acquired using the
1786 * same underlying policy as the spinlock_t on this architecture, which
1787 * reduces latency compared to the unfair variant below. However, it
1788 * also adds more overhead and therefore may reduce throughput.
1790 static inline int _double_lock_balance(struct rq *this_rq, struct rq *busiest)
1791 __releases(this_rq->lock)
1792 __acquires(busiest->lock)
1793 __acquires(this_rq->lock)
1795 raw_spin_unlock(&this_rq->lock);
1796 double_rq_lock(this_rq, busiest);
1803 * Unfair double_lock_balance: Optimizes throughput at the expense of
1804 * latency by eliminating extra atomic operations when the locks are
1805 * already in proper order on entry. This favors lower cpu-ids and will
1806 * grant the double lock to lower cpus over higher ids under contention,
1807 * regardless of entry order into the function.
1809 static inline int _double_lock_balance(struct rq *this_rq, struct rq *busiest)
1810 __releases(this_rq->lock)
1811 __acquires(busiest->lock)
1812 __acquires(this_rq->lock)
1816 if (unlikely(!raw_spin_trylock(&busiest->lock))) {
1817 if (busiest < this_rq) {
1818 raw_spin_unlock(&this_rq->lock);
1819 raw_spin_lock(&busiest->lock);
1820 raw_spin_lock_nested(&this_rq->lock,
1821 SINGLE_DEPTH_NESTING);
1824 raw_spin_lock_nested(&busiest->lock,
1825 SINGLE_DEPTH_NESTING);
1830 #endif /* CONFIG_PREEMPT */
1833 * double_lock_balance - lock the busiest runqueue, this_rq is locked already.
1835 static inline int double_lock_balance(struct rq *this_rq, struct rq *busiest)
1837 if (unlikely(!irqs_disabled())) {
1838 /* printk() doesn't work good under rq->lock */
1839 raw_spin_unlock(&this_rq->lock);
1843 return _double_lock_balance(this_rq, busiest);
1846 static inline void double_unlock_balance(struct rq *this_rq, struct rq *busiest)
1847 __releases(busiest->lock)
1849 raw_spin_unlock(&busiest->lock);
1850 lock_set_subclass(&this_rq->lock.dep_map, 0, _RET_IP_);
1853 static inline void double_lock(spinlock_t *l1, spinlock_t *l2)
1859 spin_lock_nested(l2, SINGLE_DEPTH_NESTING);
1862 static inline void double_lock_irq(spinlock_t *l1, spinlock_t *l2)
1868 spin_lock_nested(l2, SINGLE_DEPTH_NESTING);
1871 static inline void double_raw_lock(raw_spinlock_t *l1, raw_spinlock_t *l2)
1877 raw_spin_lock_nested(l2, SINGLE_DEPTH_NESTING);
1881 * double_rq_lock - safely lock two runqueues
1883 * Note this does not disable interrupts like task_rq_lock,
1884 * you need to do so manually before calling.
1886 static inline void double_rq_lock(struct rq *rq1, struct rq *rq2)
1887 __acquires(rq1->lock)
1888 __acquires(rq2->lock)
1890 BUG_ON(!irqs_disabled());
1892 raw_spin_lock(&rq1->lock);
1893 __acquire(rq2->lock); /* Fake it out ;) */
1896 raw_spin_lock(&rq1->lock);
1897 raw_spin_lock_nested(&rq2->lock, SINGLE_DEPTH_NESTING);
1899 raw_spin_lock(&rq2->lock);
1900 raw_spin_lock_nested(&rq1->lock, SINGLE_DEPTH_NESTING);
1906 * double_rq_unlock - safely unlock two runqueues
1908 * Note this does not restore interrupts like task_rq_unlock,
1909 * you need to do so manually after calling.
1911 static inline void double_rq_unlock(struct rq *rq1, struct rq *rq2)
1912 __releases(rq1->lock)
1913 __releases(rq2->lock)
1915 raw_spin_unlock(&rq1->lock);
1917 raw_spin_unlock(&rq2->lock);
1919 __release(rq2->lock);
1922 extern void set_rq_online (struct rq *rq);
1923 extern void set_rq_offline(struct rq *rq);
1924 extern bool sched_smp_initialized;
1926 #else /* CONFIG_SMP */
1929 * double_rq_lock - safely lock two runqueues
1931 * Note this does not disable interrupts like task_rq_lock,
1932 * you need to do so manually before calling.
1934 static inline void double_rq_lock(struct rq *rq1, struct rq *rq2)
1935 __acquires(rq1->lock)
1936 __acquires(rq2->lock)
1938 BUG_ON(!irqs_disabled());
1940 raw_spin_lock(&rq1->lock);
1941 __acquire(rq2->lock); /* Fake it out ;) */
1945 * double_rq_unlock - safely unlock two runqueues
1947 * Note this does not restore interrupts like task_rq_unlock,
1948 * you need to do so manually after calling.
1950 static inline void double_rq_unlock(struct rq *rq1, struct rq *rq2)
1951 __releases(rq1->lock)
1952 __releases(rq2->lock)
1955 raw_spin_unlock(&rq1->lock);
1956 __release(rq2->lock);
1961 extern struct sched_entity *__pick_first_entity(struct cfs_rq *cfs_rq);
1962 extern struct sched_entity *__pick_last_entity(struct cfs_rq *cfs_rq);
1964 #ifdef CONFIG_SCHED_DEBUG
1965 extern bool sched_debug_enabled;
1967 extern void print_cfs_stats(struct seq_file *m, int cpu);
1968 extern void print_rt_stats(struct seq_file *m, int cpu);
1969 extern void print_dl_stats(struct seq_file *m, int cpu);
1970 extern void print_cfs_rq(struct seq_file *m, int cpu, struct cfs_rq *cfs_rq);
1971 extern void print_rt_rq(struct seq_file *m, int cpu, struct rt_rq *rt_rq);
1972 extern void print_dl_rq(struct seq_file *m, int cpu, struct dl_rq *dl_rq);
1973 #ifdef CONFIG_NUMA_BALANCING
1975 show_numa_stats(struct task_struct *p, struct seq_file *m);
1977 print_numa_stats(struct seq_file *m, int node, unsigned long tsf,
1978 unsigned long tpf, unsigned long gsf, unsigned long gpf);
1979 #endif /* CONFIG_NUMA_BALANCING */
1980 #endif /* CONFIG_SCHED_DEBUG */
1982 extern void init_cfs_rq(struct cfs_rq *cfs_rq);
1983 extern void init_rt_rq(struct rt_rq *rt_rq);
1984 extern void init_dl_rq(struct dl_rq *dl_rq);
1986 extern void cfs_bandwidth_usage_inc(void);
1987 extern void cfs_bandwidth_usage_dec(void);
1989 #ifdef CONFIG_NO_HZ_COMMON
1990 enum rq_nohz_flag_bits {
1995 #define nohz_flags(cpu) (&cpu_rq(cpu)->nohz_flags)
1997 extern void nohz_balance_exit_idle(unsigned int cpu);
1999 static inline void nohz_balance_exit_idle(unsigned int cpu) { }
2005 void __dl_update(struct dl_bw *dl_b, s64 bw)
2007 struct root_domain *rd = container_of(dl_b, struct root_domain, dl_bw);
2010 RCU_LOCKDEP_WARN(!rcu_read_lock_sched_held(),
2011 "sched RCU must be held");
2012 for_each_cpu_and(i, rd->span, cpu_active_mask) {
2013 struct rq *rq = cpu_rq(i);
2015 rq->dl.extra_bw += bw;
2020 void __dl_update(struct dl_bw *dl_b, s64 bw)
2022 struct dl_rq *dl = container_of(dl_b, struct dl_rq, dl_bw);
2029 #ifdef CONFIG_IRQ_TIME_ACCOUNTING
2034 struct u64_stats_sync sync;
2037 DECLARE_PER_CPU(struct irqtime, cpu_irqtime);
2040 * Returns the irqtime minus the softirq time computed by ksoftirqd.
2041 * Otherwise ksoftirqd's sum_exec_runtime is substracted its own runtime
2042 * and never move forward.
2044 static inline u64 irq_time_read(int cpu)
2046 struct irqtime *irqtime = &per_cpu(cpu_irqtime, cpu);
2051 seq = __u64_stats_fetch_begin(&irqtime->sync);
2052 total = irqtime->total;
2053 } while (__u64_stats_fetch_retry(&irqtime->sync, seq));
2057 #endif /* CONFIG_IRQ_TIME_ACCOUNTING */
2059 #ifdef CONFIG_CPU_FREQ
2060 DECLARE_PER_CPU(struct update_util_data *, cpufreq_update_util_data);
2063 * cpufreq_update_util - Take a note about CPU utilization changes.
2064 * @rq: Runqueue to carry out the update for.
2065 * @flags: Update reason flags.
2067 * This function is called by the scheduler on the CPU whose utilization is
2070 * It can only be called from RCU-sched read-side critical sections.
2072 * The way cpufreq is currently arranged requires it to evaluate the CPU
2073 * performance state (frequency/voltage) on a regular basis to prevent it from
2074 * being stuck in a completely inadequate performance level for too long.
2075 * That is not guaranteed to happen if the updates are only triggered from CFS,
2076 * though, because they may not be coming in if RT or deadline tasks are active
2077 * all the time (or there are RT and DL tasks only).
2079 * As a workaround for that issue, this function is called by the RT and DL
2080 * sched classes to trigger extra cpufreq updates to prevent it from stalling,
2081 * but that really is a band-aid. Going forward it should be replaced with
2082 * solutions targeted more specifically at RT and DL tasks.
2084 static inline void cpufreq_update_util(struct rq *rq, unsigned int flags)
2086 struct update_util_data *data;
2088 data = rcu_dereference_sched(*per_cpu_ptr(&cpufreq_update_util_data,
2091 data->func(data, rq_clock(rq), flags);
2094 static inline void cpufreq_update_util(struct rq *rq, unsigned int flags) {}
2095 #endif /* CONFIG_CPU_FREQ */
2097 #ifdef arch_scale_freq_capacity
2098 #ifndef arch_scale_freq_invariant
2099 #define arch_scale_freq_invariant() (true)
2101 #else /* arch_scale_freq_capacity */
2102 #define arch_scale_freq_invariant() (false)