2 #include <linux/sched.h>
3 #include <linux/sched/sysctl.h>
4 #include <linux/sched/rt.h>
5 #include <linux/sched/deadline.h>
6 #include <linux/mutex.h>
7 #include <linux/spinlock.h>
8 #include <linux/stop_machine.h>
9 #include <linux/irq_work.h>
10 #include <linux/tick.h>
11 #include <linux/slab.h>
14 #include "cpudeadline.h"
20 /* task_struct::on_rq states: */
21 #define TASK_ON_RQ_QUEUED 1
22 #define TASK_ON_RQ_MIGRATING 2
24 extern __read_mostly int scheduler_running;
26 extern unsigned long calc_load_update;
27 extern atomic_long_t calc_load_tasks;
29 extern void calc_global_load_tick(struct rq *this_rq);
30 extern long calc_load_fold_active(struct rq *this_rq);
33 extern void update_cpu_load_active(struct rq *this_rq);
34 extern void check_for_migration(struct rq *rq, struct task_struct *p);
36 static inline void update_cpu_load_active(struct rq *this_rq) { }
37 static inline void check_for_migration(struct rq *rq, struct task_struct *p) { }
41 * Helpers for converting nanosecond timing to jiffy resolution
43 #define NS_TO_JIFFIES(TIME) ((unsigned long)(TIME) / (NSEC_PER_SEC / HZ))
46 * Increase resolution of nice-level calculations for 64-bit architectures.
47 * The extra resolution improves shares distribution and load balancing of
48 * low-weight task groups (eg. nice +19 on an autogroup), deeper taskgroup
49 * hierarchies, especially on larger systems. This is not a user-visible change
50 * and does not change the user-interface for setting shares/weights.
52 * We increase resolution only if we have enough bits to allow this increased
53 * resolution (i.e. BITS_PER_LONG > 32). The costs for increasing resolution
54 * when BITS_PER_LONG <= 32 are pretty high and the returns do not justify the
57 #if 0 /* BITS_PER_LONG > 32 -- currently broken: it increases power usage under light load */
58 # define SCHED_LOAD_RESOLUTION 10
59 # define scale_load(w) ((w) << SCHED_LOAD_RESOLUTION)
60 # define scale_load_down(w) ((w) >> SCHED_LOAD_RESOLUTION)
62 # define SCHED_LOAD_RESOLUTION 0
63 # define scale_load(w) (w)
64 # define scale_load_down(w) (w)
67 #define SCHED_LOAD_SHIFT (10 + SCHED_LOAD_RESOLUTION)
68 #define SCHED_LOAD_SCALE (1L << SCHED_LOAD_SHIFT)
70 #define NICE_0_LOAD SCHED_LOAD_SCALE
71 #define NICE_0_SHIFT SCHED_LOAD_SHIFT
74 * Single value that decides SCHED_DEADLINE internal math precision.
75 * 10 -> just above 1us
76 * 9 -> just above 0.5us
81 * These are the 'tuning knobs' of the scheduler:
85 * single value that denotes runtime == period, ie unlimited time.
87 #define RUNTIME_INF ((u64)~0ULL)
89 static inline int idle_policy(int policy)
91 return policy == SCHED_IDLE;
93 static inline int fair_policy(int policy)
95 return policy == SCHED_NORMAL || policy == SCHED_BATCH;
98 static inline int rt_policy(int policy)
100 return policy == SCHED_FIFO || policy == SCHED_RR;
103 static inline int dl_policy(int policy)
105 return policy == SCHED_DEADLINE;
107 static inline bool valid_policy(int policy)
109 return idle_policy(policy) || fair_policy(policy) ||
110 rt_policy(policy) || dl_policy(policy);
113 static inline int task_has_rt_policy(struct task_struct *p)
115 return rt_policy(p->policy);
118 static inline int task_has_dl_policy(struct task_struct *p)
120 return dl_policy(p->policy);
124 * Tells if entity @a should preempt entity @b.
127 dl_entity_preempt(struct sched_dl_entity *a, struct sched_dl_entity *b)
129 return dl_time_before(a->deadline, b->deadline);
133 * This is the priority-queue data structure of the RT scheduling class:
135 struct rt_prio_array {
136 DECLARE_BITMAP(bitmap, MAX_RT_PRIO+1); /* include 1 bit for delimiter */
137 struct list_head queue[MAX_RT_PRIO];
140 struct rt_bandwidth {
141 /* nests inside the rq lock: */
142 raw_spinlock_t rt_runtime_lock;
145 struct hrtimer rt_period_timer;
146 unsigned int rt_period_active;
149 void __dl_clear_params(struct task_struct *p);
152 * To keep the bandwidth of -deadline tasks and groups under control
153 * we need some place where:
154 * - store the maximum -deadline bandwidth of the system (the group);
155 * - cache the fraction of that bandwidth that is currently allocated.
157 * This is all done in the data structure below. It is similar to the
158 * one used for RT-throttling (rt_bandwidth), with the main difference
159 * that, since here we are only interested in admission control, we
160 * do not decrease any runtime while the group "executes", neither we
161 * need a timer to replenish it.
163 * With respect to SMP, the bandwidth is given on a per-CPU basis,
165 * - dl_bw (< 100%) is the bandwidth of the system (group) on each CPU;
166 * - dl_total_bw array contains, in the i-eth element, the currently
167 * allocated bandwidth on the i-eth CPU.
168 * Moreover, groups consume bandwidth on each CPU, while tasks only
169 * consume bandwidth on the CPU they're running on.
170 * Finally, dl_total_bw_cpu is used to cache the index of dl_total_bw
171 * that will be shown the next time the proc or cgroup controls will
172 * be red. It on its turn can be changed by writing on its own
175 struct dl_bandwidth {
176 raw_spinlock_t dl_runtime_lock;
181 static inline int dl_bandwidth_enabled(void)
183 return sysctl_sched_rt_runtime >= 0;
186 extern struct dl_bw *dl_bw_of(int i);
194 void __dl_clear(struct dl_bw *dl_b, u64 tsk_bw)
196 dl_b->total_bw -= tsk_bw;
200 void __dl_add(struct dl_bw *dl_b, u64 tsk_bw)
202 dl_b->total_bw += tsk_bw;
206 bool __dl_overflow(struct dl_bw *dl_b, int cpus, u64 old_bw, u64 new_bw)
208 return dl_b->bw != -1 &&
209 dl_b->bw * cpus < dl_b->total_bw - old_bw + new_bw;
212 extern struct mutex sched_domains_mutex;
214 #ifdef CONFIG_CGROUP_SCHED
216 #include <linux/cgroup.h>
221 extern struct list_head task_groups;
223 struct cfs_bandwidth {
224 #ifdef CONFIG_CFS_BANDWIDTH
228 s64 hierarchical_quota;
231 int idle, period_active;
232 struct hrtimer period_timer, slack_timer;
233 struct list_head throttled_cfs_rq;
236 int nr_periods, nr_throttled;
239 bool distribute_running;
243 /* task group related information */
245 struct cgroup_subsys_state css;
247 #ifdef CONFIG_FAIR_GROUP_SCHED
248 /* schedulable entities of this group on each cpu */
249 struct sched_entity **se;
250 /* runqueue "owned" by this group on each cpu */
251 struct cfs_rq **cfs_rq;
252 unsigned long shares;
255 atomic_long_t load_avg;
259 #ifdef CONFIG_RT_GROUP_SCHED
260 struct sched_rt_entity **rt_se;
261 struct rt_rq **rt_rq;
263 struct rt_bandwidth rt_bandwidth;
267 struct list_head list;
269 struct task_group *parent;
270 struct list_head siblings;
271 struct list_head children;
273 #ifdef CONFIG_SCHED_AUTOGROUP
274 struct autogroup *autogroup;
277 struct cfs_bandwidth cfs_bandwidth;
280 #ifdef CONFIG_FAIR_GROUP_SCHED
281 #define ROOT_TASK_GROUP_LOAD NICE_0_LOAD
284 * A weight of 0 or 1 can cause arithmetics problems.
285 * A weight of a cfs_rq is the sum of weights of which entities
286 * are queued on this cfs_rq, so a weight of a entity should not be
287 * too large, so as the shares value of a task group.
288 * (The default weight is 1024 - so there's no practical
289 * limitation from this.)
291 #define MIN_SHARES (1UL << 1)
292 #define MAX_SHARES (1UL << 18)
295 typedef int (*tg_visitor)(struct task_group *, void *);
297 extern int walk_tg_tree_from(struct task_group *from,
298 tg_visitor down, tg_visitor up, void *data);
301 * Iterate the full tree, calling @down when first entering a node and @up when
302 * leaving it for the final time.
304 * Caller must hold rcu_lock or sufficient equivalent.
306 static inline int walk_tg_tree(tg_visitor down, tg_visitor up, void *data)
308 return walk_tg_tree_from(&root_task_group, down, up, data);
311 extern int tg_nop(struct task_group *tg, void *data);
313 extern void free_fair_sched_group(struct task_group *tg);
314 extern int alloc_fair_sched_group(struct task_group *tg, struct task_group *parent);
315 extern void unregister_fair_sched_group(struct task_group *tg);
316 extern void init_tg_cfs_entry(struct task_group *tg, struct cfs_rq *cfs_rq,
317 struct sched_entity *se, int cpu,
318 struct sched_entity *parent);
319 extern void init_cfs_bandwidth(struct cfs_bandwidth *cfs_b);
320 extern int sched_group_set_shares(struct task_group *tg, unsigned long shares);
322 extern void __refill_cfs_bandwidth_runtime(struct cfs_bandwidth *cfs_b);
323 extern void start_cfs_bandwidth(struct cfs_bandwidth *cfs_b);
324 extern void unthrottle_cfs_rq(struct cfs_rq *cfs_rq);
326 extern void free_rt_sched_group(struct task_group *tg);
327 extern int alloc_rt_sched_group(struct task_group *tg, struct task_group *parent);
328 extern void init_tg_rt_entry(struct task_group *tg, struct rt_rq *rt_rq,
329 struct sched_rt_entity *rt_se, int cpu,
330 struct sched_rt_entity *parent);
332 extern struct task_group *sched_create_group(struct task_group *parent);
333 extern void sched_online_group(struct task_group *tg,
334 struct task_group *parent);
335 extern void sched_destroy_group(struct task_group *tg);
336 extern void sched_offline_group(struct task_group *tg);
338 extern void sched_move_task(struct task_struct *tsk);
340 #ifdef CONFIG_FAIR_GROUP_SCHED
341 extern int sched_group_set_shares(struct task_group *tg, unsigned long shares);
344 extern void set_task_rq_fair(struct sched_entity *se,
345 struct cfs_rq *prev, struct cfs_rq *next);
346 #else /* !CONFIG_SMP */
347 static inline void set_task_rq_fair(struct sched_entity *se,
348 struct cfs_rq *prev, struct cfs_rq *next) { }
349 #endif /* CONFIG_SMP */
350 #endif /* CONFIG_FAIR_GROUP_SCHED */
352 #else /* CONFIG_CGROUP_SCHED */
354 struct cfs_bandwidth { };
356 #endif /* CONFIG_CGROUP_SCHED */
358 /* CFS-related fields in a runqueue */
360 struct load_weight load;
361 unsigned int nr_running, h_nr_running;
366 u64 min_vruntime_copy;
369 struct rb_root tasks_timeline;
370 struct rb_node *rb_leftmost;
373 * 'curr' points to currently running entity on this cfs_rq.
374 * It is set to NULL otherwise (i.e when none are currently running).
376 struct sched_entity *curr, *next, *last, *skip;
378 #ifdef CONFIG_SCHED_DEBUG
379 unsigned int nr_spread_over;
386 struct sched_avg avg;
387 u64 runnable_load_sum;
388 unsigned long runnable_load_avg;
389 #ifdef CONFIG_FAIR_GROUP_SCHED
390 unsigned long tg_load_avg_contrib;
391 unsigned long propagate_avg;
393 atomic_long_t removed_load_avg, removed_util_avg;
395 u64 load_last_update_time_copy;
398 #ifdef CONFIG_FAIR_GROUP_SCHED
400 * h_load = weight * f(tg)
402 * Where f(tg) is the recursive weight fraction assigned to
405 unsigned long h_load;
406 u64 last_h_load_update;
407 struct sched_entity *h_load_next;
408 #endif /* CONFIG_FAIR_GROUP_SCHED */
409 #endif /* CONFIG_SMP */
411 #ifdef CONFIG_FAIR_GROUP_SCHED
412 struct rq *rq; /* cpu runqueue to which this cfs_rq is attached */
415 * leaf cfs_rqs are those that hold tasks (lowest schedulable entity in
416 * a hierarchy). Non-leaf lrqs hold other higher schedulable entities
417 * (like users, containers etc.)
419 * leaf_cfs_rq_list ties together list of leaf cfs_rq's in a cpu. This
420 * list is used during load balance.
423 struct list_head leaf_cfs_rq_list;
424 struct task_group *tg; /* group that "owns" this runqueue */
426 #ifdef CONFIG_SCHED_WALT
427 u64 cumulative_runnable_avg;
430 #ifdef CONFIG_CFS_BANDWIDTH
433 s64 runtime_remaining;
435 u64 throttled_clock, throttled_clock_task;
436 u64 throttled_clock_task_time;
437 int throttled, throttle_count, throttle_uptodate;
438 struct list_head throttled_list;
439 #endif /* CONFIG_CFS_BANDWIDTH */
440 #endif /* CONFIG_FAIR_GROUP_SCHED */
443 static inline int rt_bandwidth_enabled(void)
445 return sysctl_sched_rt_runtime >= 0;
448 /* RT IPI pull logic requires IRQ_WORK */
449 #if defined(CONFIG_IRQ_WORK) && defined(CONFIG_SMP)
450 # define HAVE_RT_PUSH_IPI
453 /* Real-Time classes' related field in a runqueue: */
455 struct rt_prio_array active;
456 unsigned int rt_nr_running;
457 #if defined CONFIG_SMP || defined CONFIG_RT_GROUP_SCHED
459 int curr; /* highest queued rt task prio */
461 int next; /* next highest */
466 unsigned long rt_nr_migratory;
467 unsigned long rt_nr_total;
469 struct plist_head pushable_tasks;
470 #endif /* CONFIG_SMP */
476 /* Nests inside the rq lock: */
477 raw_spinlock_t rt_runtime_lock;
479 #ifdef CONFIG_RT_GROUP_SCHED
480 unsigned long rt_nr_boosted;
483 struct task_group *tg;
487 /* Deadline class' related fields in a runqueue */
489 /* runqueue is an rbtree, ordered by deadline */
490 struct rb_root rb_root;
491 struct rb_node *rb_leftmost;
493 unsigned long dl_nr_running;
497 * Deadline values of the currently executing and the
498 * earliest ready task on this rq. Caching these facilitates
499 * the decision wether or not a ready but not running task
500 * should migrate somewhere else.
507 unsigned long dl_nr_migratory;
511 * Tasks on this rq that can be pushed away. They are kept in
512 * an rb-tree, ordered by tasks' deadlines, with caching
513 * of the leftmost (earliest deadline) element.
515 struct rb_root pushable_dl_tasks_root;
516 struct rb_node *pushable_dl_tasks_leftmost;
520 /* This is the "average utilization" for this runqueue */
526 struct max_cpu_capacity {
533 * We add the notion of a root-domain which will be used to define per-domain
534 * variables. Each exclusive cpuset essentially defines an island domain by
535 * fully partitioning the member cpus from any other cpuset. Whenever a new
536 * exclusive cpuset is created, we also create and attach a new root-domain
545 cpumask_var_t online;
547 /* Indicate more than one runnable task for any CPU */
550 /* Indicate one or more cpus over-utilized (tipping point) */
554 * The bit corresponding to a CPU gets set here if such CPU has more
555 * than one runnable -deadline task (as it is below for RT tasks).
557 cpumask_var_t dlo_mask;
562 #ifdef HAVE_RT_PUSH_IPI
564 * For IPI pull requests, loop across the rto_mask.
566 struct irq_work rto_push_work;
567 raw_spinlock_t rto_lock;
568 /* These are only updated and read within rto_lock */
571 /* These atomics are updated outside of a lock */
572 atomic_t rto_loop_next;
573 atomic_t rto_loop_start;
576 * The "RT overload" flag: it gets set if a CPU has more than
577 * one runnable RT task.
579 cpumask_var_t rto_mask;
580 struct cpupri cpupri;
582 /* Maximum cpu capacity in the system. */
583 struct max_cpu_capacity max_cpu_capacity;
585 /* First cpu with maximum and minimum original capacity */
586 int max_cap_orig_cpu, min_cap_orig_cpu;
589 extern struct root_domain def_root_domain;
590 extern void sched_get_rd(struct root_domain *rd);
591 extern void sched_put_rd(struct root_domain *rd);
593 #ifdef HAVE_RT_PUSH_IPI
594 extern void rto_push_irq_work_func(struct irq_work *work);
596 #endif /* CONFIG_SMP */
599 * This is the main, per-CPU runqueue data structure.
601 * Locking rule: those places that want to lock multiple runqueues
602 * (such as the load balancing or the thread migration code), lock
603 * acquire operations must be ordered by ascending &runqueue.
610 * nr_running and cpu_load should be in the same cacheline because
611 * remote CPUs use both these fields when doing load calculation.
613 unsigned int nr_running;
614 #ifdef CONFIG_NUMA_BALANCING
615 unsigned int nr_numa_running;
616 unsigned int nr_preferred_running;
618 #define CPU_LOAD_IDX_MAX 5
619 unsigned long cpu_load[CPU_LOAD_IDX_MAX];
620 unsigned long last_load_update_tick;
621 unsigned int misfit_task;
622 #ifdef CONFIG_NO_HZ_COMMON
624 unsigned long nohz_flags;
626 #ifdef CONFIG_NO_HZ_FULL
627 unsigned long last_sched_tick;
630 #ifdef CONFIG_CPU_QUIET
631 /* time-based average load */
633 u64 nr_running_integral;
634 seqcount_t ave_seqcnt;
637 /* capture load from *all* tasks on this cpu: */
638 struct load_weight load;
639 unsigned long nr_load_updates;
646 #ifdef CONFIG_FAIR_GROUP_SCHED
647 /* list of leaf cfs_rq on this cpu: */
648 struct list_head leaf_cfs_rq_list;
649 struct list_head *tmp_alone_branch;
650 #endif /* CONFIG_FAIR_GROUP_SCHED */
653 * This is part of a global counter where only the total sum
654 * over all CPUs matters. A task can increase this counter on
655 * one CPU and if it got migrated afterwards it may decrease
656 * it on another CPU. Always updated under the runqueue lock:
658 unsigned long nr_uninterruptible;
660 struct task_struct *curr, *idle, *stop;
661 unsigned long next_balance;
662 struct mm_struct *prev_mm;
664 unsigned int clock_skip_update;
671 struct root_domain *rd;
672 struct sched_domain *sd;
674 unsigned long cpu_capacity;
675 unsigned long cpu_capacity_orig;
677 struct callback_head *balance_callback;
679 unsigned char idle_balance;
680 /* For active balancing */
683 struct task_struct *push_task;
684 struct cpu_stop_work active_balance_work;
685 /* cpu of this runqueue: */
689 struct list_head cfs_tasks;
696 /* This is used to determine avg_idle's max value */
697 u64 max_idle_balance_cost;
700 #ifdef CONFIG_SCHED_WALT
701 u64 cumulative_runnable_avg;
703 u64 curr_runnable_sum;
704 u64 prev_runnable_sum;
705 u64 nt_curr_runnable_sum;
706 u64 nt_prev_runnable_sum;
710 u64 cum_window_demand;
711 #endif /* CONFIG_SCHED_WALT */
714 #ifdef CONFIG_IRQ_TIME_ACCOUNTING
717 #ifdef CONFIG_PARAVIRT
720 #ifdef CONFIG_PARAVIRT_TIME_ACCOUNTING
721 u64 prev_steal_time_rq;
724 /* calc_load related fields */
725 unsigned long calc_load_update;
726 long calc_load_active;
728 #ifdef CONFIG_SCHED_HRTICK
730 int hrtick_csd_pending;
731 struct call_single_data hrtick_csd;
733 struct hrtimer hrtick_timer;
736 #ifdef CONFIG_SCHEDSTATS
738 struct sched_info rq_sched_info;
739 unsigned long long rq_cpu_time;
740 /* could above be rq->cfs_rq.exec_clock + rq->rt_rq.rt_runtime ? */
742 /* sys_sched_yield() stats */
743 unsigned int yld_count;
745 /* schedule() stats */
746 unsigned int sched_count;
747 unsigned int sched_goidle;
749 /* try_to_wake_up() stats */
750 unsigned int ttwu_count;
751 unsigned int ttwu_local;
753 struct eas_stats eas_stats;
758 struct llist_head wake_list;
761 #ifdef CONFIG_CPU_IDLE
762 /* Must be inspected within a rcu lock section */
763 struct cpuidle_state *idle_state;
768 static inline int cpu_of(struct rq *rq)
777 DECLARE_PER_CPU_SHARED_ALIGNED(struct rq, runqueues);
779 #define cpu_rq(cpu) (&per_cpu(runqueues, (cpu)))
780 #define this_rq() this_cpu_ptr(&runqueues)
781 #define task_rq(p) cpu_rq(task_cpu(p))
782 #define cpu_curr(cpu) (cpu_rq(cpu)->curr)
783 #define raw_rq() raw_cpu_ptr(&runqueues)
785 static inline u64 __rq_clock_broken(struct rq *rq)
787 return READ_ONCE(rq->clock);
790 static inline u64 rq_clock(struct rq *rq)
792 lockdep_assert_held(&rq->lock);
796 static inline u64 rq_clock_task(struct rq *rq)
798 lockdep_assert_held(&rq->lock);
799 return rq->clock_task;
802 #define RQCF_REQ_SKIP 0x01
803 #define RQCF_ACT_SKIP 0x02
805 static inline void rq_clock_skip_update(struct rq *rq, bool skip)
807 lockdep_assert_held(&rq->lock);
809 rq->clock_skip_update |= RQCF_REQ_SKIP;
811 rq->clock_skip_update &= ~RQCF_REQ_SKIP;
815 enum numa_topology_type {
820 extern enum numa_topology_type sched_numa_topology_type;
821 extern int sched_max_numa_distance;
822 extern bool find_numa_distance(int distance);
825 #ifdef CONFIG_NUMA_BALANCING
826 /* The regions in numa_faults array from task_struct */
827 enum numa_faults_stats {
833 extern void sched_setnuma(struct task_struct *p, int node);
834 extern int migrate_task_to(struct task_struct *p, int cpu);
835 extern int migrate_swap(struct task_struct *, struct task_struct *);
836 #endif /* CONFIG_NUMA_BALANCING */
841 queue_balance_callback(struct rq *rq,
842 struct callback_head *head,
843 void (*func)(struct rq *rq))
845 lockdep_assert_held(&rq->lock);
847 if (unlikely(head->next))
850 head->func = (void (*)(struct callback_head *))func;
851 head->next = rq->balance_callback;
852 rq->balance_callback = head;
855 extern void sched_ttwu_pending(void);
857 #define rcu_dereference_check_sched_domain(p) \
858 rcu_dereference_check((p), \
859 lockdep_is_held(&sched_domains_mutex))
862 * The domain tree (rq->sd) is protected by RCU's quiescent state transition.
863 * See detach_destroy_domains: synchronize_sched for details.
865 * The domain tree of any CPU may only be accessed from within
866 * preempt-disabled sections.
868 #define for_each_domain(cpu, __sd) \
869 for (__sd = rcu_dereference_check_sched_domain(cpu_rq(cpu)->sd); \
870 __sd; __sd = __sd->parent)
872 #define for_each_lower_domain(sd) for (; sd; sd = sd->child)
875 * highest_flag_domain - Return highest sched_domain containing flag.
876 * @cpu: The cpu whose highest level of sched domain is to
878 * @flag: The flag to check for the highest sched_domain
881 * Returns the highest sched_domain of a cpu which contains the given flag.
883 static inline struct sched_domain *highest_flag_domain(int cpu, int flag)
885 struct sched_domain *sd, *hsd = NULL;
887 for_each_domain(cpu, sd) {
888 if (!(sd->flags & flag))
896 static inline struct sched_domain *lowest_flag_domain(int cpu, int flag)
898 struct sched_domain *sd;
900 for_each_domain(cpu, sd) {
901 if (sd->flags & flag)
908 DECLARE_PER_CPU(struct sched_domain *, sd_llc);
909 DECLARE_PER_CPU(int, sd_llc_size);
910 DECLARE_PER_CPU(int, sd_llc_id);
911 DECLARE_PER_CPU(struct sched_domain *, sd_numa);
912 DECLARE_PER_CPU(struct sched_domain *, sd_busy);
913 DECLARE_PER_CPU(struct sched_domain *, sd_asym);
914 DECLARE_PER_CPU(struct sched_domain *, sd_ea);
915 DECLARE_PER_CPU(struct sched_domain *, sd_scs);
917 struct sched_group_capacity {
920 * CPU capacity of this group, SCHED_LOAD_SCALE being max capacity
923 unsigned long capacity;
924 unsigned long max_capacity; /* Max per-cpu capacity in group */
925 unsigned long min_capacity; /* Min per-CPU capacity in group */
926 unsigned long next_update;
927 int imbalance; /* XXX unrelated to capacity but shared group state */
929 * Number of busy cpus in this group.
931 atomic_t nr_busy_cpus;
933 unsigned long cpumask[0]; /* iteration mask */
937 struct sched_group *next; /* Must be a circular list */
940 unsigned int group_weight;
941 struct sched_group_capacity *sgc;
942 const struct sched_group_energy *sge;
945 * The CPUs this group covers.
947 * NOTE: this field is variable length. (Allocated dynamically
948 * by attaching extra space to the end of the structure,
949 * depending on how many CPUs the kernel has booted up with)
951 unsigned long cpumask[0];
954 static inline struct cpumask *sched_group_cpus(struct sched_group *sg)
956 return to_cpumask(sg->cpumask);
960 * cpumask masking which cpus in the group are allowed to iterate up the domain
963 static inline struct cpumask *sched_group_mask(struct sched_group *sg)
965 return to_cpumask(sg->sgc->cpumask);
969 * group_first_cpu - Returns the first cpu in the cpumask of a sched_group.
970 * @group: The group whose first cpu is to be returned.
972 static inline unsigned int group_first_cpu(struct sched_group *group)
974 return cpumask_first(sched_group_cpus(group));
977 extern int group_balance_cpu(struct sched_group *sg);
981 static inline void sched_ttwu_pending(void) { }
983 #endif /* CONFIG_SMP */
986 #include "auto_group.h"
988 #ifdef CONFIG_CGROUP_SCHED
991 * Return the group to which this tasks belongs.
993 * We cannot use task_css() and friends because the cgroup subsystem
994 * changes that value before the cgroup_subsys::attach() method is called,
995 * therefore we cannot pin it and might observe the wrong value.
997 * The same is true for autogroup's p->signal->autogroup->tg, the autogroup
998 * core changes this before calling sched_move_task().
1000 * Instead we use a 'copy' which is updated from sched_move_task() while
1001 * holding both task_struct::pi_lock and rq::lock.
1003 static inline struct task_group *task_group(struct task_struct *p)
1005 return p->sched_task_group;
1008 /* Change a task's cfs_rq and parent entity if it moves across CPUs/groups */
1009 static inline void set_task_rq(struct task_struct *p, unsigned int cpu)
1011 #if defined(CONFIG_FAIR_GROUP_SCHED) || defined(CONFIG_RT_GROUP_SCHED)
1012 struct task_group *tg = task_group(p);
1015 #ifdef CONFIG_FAIR_GROUP_SCHED
1016 set_task_rq_fair(&p->se, p->se.cfs_rq, tg->cfs_rq[cpu]);
1017 p->se.cfs_rq = tg->cfs_rq[cpu];
1018 p->se.parent = tg->se[cpu];
1021 #ifdef CONFIG_RT_GROUP_SCHED
1022 p->rt.rt_rq = tg->rt_rq[cpu];
1023 p->rt.parent = tg->rt_se[cpu];
1027 #else /* CONFIG_CGROUP_SCHED */
1029 static inline void set_task_rq(struct task_struct *p, unsigned int cpu) { }
1030 static inline struct task_group *task_group(struct task_struct *p)
1035 #endif /* CONFIG_CGROUP_SCHED */
1037 static inline void __set_task_cpu(struct task_struct *p, unsigned int cpu)
1039 set_task_rq(p, cpu);
1042 * After ->cpu is set up to a new value, task_rq_lock(p, ...) can be
1043 * successfuly executed on another CPU. We must ensure that updates of
1044 * per-task data have been completed by this moment.
1047 #ifdef CONFIG_THREAD_INFO_IN_TASK
1050 task_thread_info(p)->cpu = cpu;
1057 * Tunables that become constants when CONFIG_SCHED_DEBUG is off:
1059 #ifdef CONFIG_SCHED_DEBUG
1060 # include <linux/static_key.h>
1061 # define const_debug __read_mostly
1063 # define const_debug const
1066 extern const_debug unsigned int sysctl_sched_features;
1068 #define SCHED_FEAT(name, enabled) \
1069 __SCHED_FEAT_##name ,
1072 #include "features.h"
1078 #if defined(CONFIG_SCHED_DEBUG) && defined(HAVE_JUMP_LABEL)
1079 #define SCHED_FEAT(name, enabled) \
1080 static __always_inline bool static_branch_##name(struct static_key *key) \
1082 return static_key_##enabled(key); \
1085 #include "features.h"
1089 extern struct static_key sched_feat_keys[__SCHED_FEAT_NR];
1090 #define sched_feat(x) (static_branch_##x(&sched_feat_keys[__SCHED_FEAT_##x]))
1091 #else /* !(SCHED_DEBUG && HAVE_JUMP_LABEL) */
1092 #define sched_feat(x) (sysctl_sched_features & (1UL << __SCHED_FEAT_##x))
1093 #endif /* SCHED_DEBUG && HAVE_JUMP_LABEL */
1095 extern struct static_key_false sched_numa_balancing;
1097 static inline u64 global_rt_period(void)
1099 return (u64)sysctl_sched_rt_period * NSEC_PER_USEC;
1102 static inline u64 global_rt_runtime(void)
1104 if (sysctl_sched_rt_runtime < 0)
1107 return (u64)sysctl_sched_rt_runtime * NSEC_PER_USEC;
1110 static inline int task_current(struct rq *rq, struct task_struct *p)
1112 return rq->curr == p;
1115 static inline int task_running(struct rq *rq, struct task_struct *p)
1120 return task_current(rq, p);
1124 static inline int task_on_rq_queued(struct task_struct *p)
1126 return p->on_rq == TASK_ON_RQ_QUEUED;
1129 static inline int task_on_rq_migrating(struct task_struct *p)
1131 return p->on_rq == TASK_ON_RQ_MIGRATING;
1134 #ifndef prepare_arch_switch
1135 # define prepare_arch_switch(next) do { } while (0)
1137 #ifndef finish_arch_post_lock_switch
1138 # define finish_arch_post_lock_switch() do { } while (0)
1141 static inline void prepare_lock_switch(struct rq *rq, struct task_struct *next)
1145 * We can optimise this out completely for !SMP, because the
1146 * SMP rebalancing from interrupt is the only thing that cares
1153 static inline void finish_lock_switch(struct rq *rq, struct task_struct *prev)
1157 * After ->on_cpu is cleared, the task can be moved to a different CPU.
1158 * We must ensure this doesn't happen until the switch is completely
1161 * In particular, the load of prev->state in finish_task_switch() must
1162 * happen before this.
1164 * Pairs with the control dependency and rmb in try_to_wake_up().
1166 smp_store_release(&prev->on_cpu, 0);
1168 #ifdef CONFIG_DEBUG_SPINLOCK
1169 /* this is a valid case when another task releases the spinlock */
1170 rq->lock.owner = current;
1173 * If we are tracking spinlock dependencies then we have to
1174 * fix up the runqueue lock - which gets 'carried over' from
1175 * prev into current:
1177 spin_acquire(&rq->lock.dep_map, 0, 0, _THIS_IP_);
1179 raw_spin_unlock_irq(&rq->lock);
1185 #define WF_SYNC 0x01 /* waker goes to sleep after wakeup */
1186 #define WF_FORK 0x02 /* child wakeup after fork */
1187 #define WF_MIGRATED 0x4 /* internal use, task got migrated */
1190 * To aid in avoiding the subversion of "niceness" due to uneven distribution
1191 * of tasks with abnormal "nice" values across CPUs the contribution that
1192 * each task makes to its run queue's load is weighted according to its
1193 * scheduling class and "nice" value. For SCHED_NORMAL tasks this is just a
1194 * scaled version of the new time slice allocation that they receive on time
1198 #define WEIGHT_IDLEPRIO 3
1199 #define WMULT_IDLEPRIO 1431655765
1202 * Nice levels are multiplicative, with a gentle 10% change for every
1203 * nice level changed. I.e. when a CPU-bound task goes from nice 0 to
1204 * nice 1, it will get ~10% less CPU time than another CPU-bound task
1205 * that remained on nice 0.
1207 * The "10% effect" is relative and cumulative: from _any_ nice level,
1208 * if you go up 1 level, it's -10% CPU usage, if you go down 1 level
1209 * it's +10% CPU usage. (to achieve that we use a multiplier of 1.25.
1210 * If a task goes up by ~10% and another task goes down by ~10% then
1211 * the relative distance between them is ~25%.)
1213 static const int prio_to_weight[40] = {
1214 /* -20 */ 88761, 71755, 56483, 46273, 36291,
1215 /* -15 */ 29154, 23254, 18705, 14949, 11916,
1216 /* -10 */ 9548, 7620, 6100, 4904, 3906,
1217 /* -5 */ 3121, 2501, 1991, 1586, 1277,
1218 /* 0 */ 1024, 820, 655, 526, 423,
1219 /* 5 */ 335, 272, 215, 172, 137,
1220 /* 10 */ 110, 87, 70, 56, 45,
1221 /* 15 */ 36, 29, 23, 18, 15,
1225 * Inverse (2^32/x) values of the prio_to_weight[] array, precalculated.
1227 * In cases where the weight does not change often, we can use the
1228 * precalculated inverse to speed up arithmetics by turning divisions
1229 * into multiplications:
1231 static const u32 prio_to_wmult[40] = {
1232 /* -20 */ 48388, 59856, 76040, 92818, 118348,
1233 /* -15 */ 147320, 184698, 229616, 287308, 360437,
1234 /* -10 */ 449829, 563644, 704093, 875809, 1099582,
1235 /* -5 */ 1376151, 1717300, 2157191, 2708050, 3363326,
1236 /* 0 */ 4194304, 5237765, 6557202, 8165337, 10153587,
1237 /* 5 */ 12820798, 15790321, 19976592, 24970740, 31350126,
1238 /* 10 */ 39045157, 49367440, 61356676, 76695844, 95443717,
1239 /* 15 */ 119304647, 148102320, 186737708, 238609294, 286331153,
1242 #define ENQUEUE_WAKEUP 0x01
1243 #define ENQUEUE_HEAD 0x02
1245 #define ENQUEUE_WAKING 0x04 /* sched_class::task_waking was called */
1247 #define ENQUEUE_WAKING 0x00
1249 #define ENQUEUE_REPLENISH 0x08
1250 #define ENQUEUE_RESTORE 0x10
1251 #define ENQUEUE_WAKEUP_NEW 0x20
1253 #define DEQUEUE_SLEEP 0x01
1254 #define DEQUEUE_SAVE 0x02
1256 #define RETRY_TASK ((void *)-1UL)
1258 struct sched_class {
1259 const struct sched_class *next;
1261 void (*enqueue_task) (struct rq *rq, struct task_struct *p, int flags);
1262 void (*dequeue_task) (struct rq *rq, struct task_struct *p, int flags);
1263 void (*yield_task) (struct rq *rq);
1264 bool (*yield_to_task) (struct rq *rq, struct task_struct *p, bool preempt);
1266 void (*check_preempt_curr) (struct rq *rq, struct task_struct *p, int flags);
1269 * It is the responsibility of the pick_next_task() method that will
1270 * return the next task to call put_prev_task() on the @prev task or
1271 * something equivalent.
1273 * May return RETRY_TASK when it finds a higher prio class has runnable
1276 struct task_struct * (*pick_next_task) (struct rq *rq,
1277 struct task_struct *prev);
1278 void (*put_prev_task) (struct rq *rq, struct task_struct *p);
1281 int (*select_task_rq)(struct task_struct *p, int task_cpu, int sd_flag, int flags,
1282 int subling_count_hint);
1283 void (*migrate_task_rq)(struct task_struct *p);
1285 void (*task_waking) (struct task_struct *task);
1286 void (*task_woken) (struct rq *this_rq, struct task_struct *task);
1288 void (*set_cpus_allowed)(struct task_struct *p,
1289 const struct cpumask *newmask);
1291 void (*rq_online)(struct rq *rq);
1292 void (*rq_offline)(struct rq *rq);
1295 void (*set_curr_task) (struct rq *rq);
1296 void (*task_tick) (struct rq *rq, struct task_struct *p, int queued);
1297 void (*task_fork) (struct task_struct *p);
1298 void (*task_dead) (struct task_struct *p);
1301 * The switched_from() call is allowed to drop rq->lock, therefore we
1302 * cannot assume the switched_from/switched_to pair is serliazed by
1303 * rq->lock. They are however serialized by p->pi_lock.
1305 void (*switched_from) (struct rq *this_rq, struct task_struct *task);
1306 void (*switched_to) (struct rq *this_rq, struct task_struct *task);
1307 void (*prio_changed) (struct rq *this_rq, struct task_struct *task,
1310 unsigned int (*get_rr_interval) (struct rq *rq,
1311 struct task_struct *task);
1313 void (*update_curr) (struct rq *rq);
1315 #define TASK_SET_GROUP 0
1316 #define TASK_MOVE_GROUP 1
1318 #ifdef CONFIG_FAIR_GROUP_SCHED
1319 void (*task_change_group)(struct task_struct *p, int type);
1323 static inline void put_prev_task(struct rq *rq, struct task_struct *prev)
1325 prev->sched_class->put_prev_task(rq, prev);
1328 #define sched_class_highest (&stop_sched_class)
1329 #define for_each_class(class) \
1330 for (class = sched_class_highest; class; class = class->next)
1332 extern const struct sched_class stop_sched_class;
1333 extern const struct sched_class dl_sched_class;
1334 extern const struct sched_class rt_sched_class;
1335 extern const struct sched_class fair_sched_class;
1336 extern const struct sched_class idle_sched_class;
1341 extern void init_max_cpu_capacity(struct max_cpu_capacity *mcc);
1342 extern void update_group_capacity(struct sched_domain *sd, int cpu);
1344 extern void trigger_load_balance(struct rq *rq);
1346 extern void idle_enter_fair(struct rq *this_rq);
1347 extern void idle_exit_fair(struct rq *this_rq);
1349 extern void set_cpus_allowed_common(struct task_struct *p, const struct cpumask *new_mask);
1353 static inline void idle_enter_fair(struct rq *rq) { }
1354 static inline void idle_exit_fair(struct rq *rq) { }
1358 #ifdef CONFIG_CPU_IDLE
1359 static inline void idle_set_state(struct rq *rq,
1360 struct cpuidle_state *idle_state)
1362 rq->idle_state = idle_state;
1365 static inline struct cpuidle_state *idle_get_state(struct rq *rq)
1367 WARN_ON(!rcu_read_lock_held());
1368 return rq->idle_state;
1371 static inline void idle_set_state_idx(struct rq *rq, int idle_state_idx)
1373 rq->idle_state_idx = idle_state_idx;
1376 static inline int idle_get_state_idx(struct rq *rq)
1378 WARN_ON(!rcu_read_lock_held());
1379 return rq->idle_state_idx;
1382 static inline void idle_set_state(struct rq *rq,
1383 struct cpuidle_state *idle_state)
1387 static inline struct cpuidle_state *idle_get_state(struct rq *rq)
1392 static inline void idle_set_state_idx(struct rq *rq, int idle_state_idx)
1396 static inline int idle_get_state_idx(struct rq *rq)
1402 extern void sysrq_sched_debug_show(void);
1403 extern void sched_init_granularity(void);
1404 extern void update_max_interval(void);
1406 extern void init_sched_dl_class(void);
1407 extern void init_sched_rt_class(void);
1408 extern void init_sched_fair_class(void);
1410 extern void resched_curr(struct rq *rq);
1411 extern void resched_cpu(int cpu);
1413 extern struct rt_bandwidth def_rt_bandwidth;
1414 extern void init_rt_bandwidth(struct rt_bandwidth *rt_b, u64 period, u64 runtime);
1415 extern void init_rt_schedtune_timer(struct sched_rt_entity *rt_se);
1417 extern struct dl_bandwidth def_dl_bandwidth;
1418 extern void init_dl_bandwidth(struct dl_bandwidth *dl_b, u64 period, u64 runtime);
1419 extern void init_dl_task_timer(struct sched_dl_entity *dl_se);
1421 unsigned long to_ratio(u64 period, u64 runtime);
1423 extern void init_entity_runnable_average(struct sched_entity *se);
1424 extern void post_init_entity_util_avg(struct sched_entity *se);
1426 static inline void __add_nr_running(struct rq *rq, unsigned count)
1428 unsigned prev_nr = rq->nr_running;
1430 rq->nr_running = prev_nr + count;
1432 if (prev_nr < 2 && rq->nr_running >= 2) {
1434 if (!rq->rd->overload)
1435 rq->rd->overload = true;
1438 #ifdef CONFIG_NO_HZ_FULL
1439 if (tick_nohz_full_cpu(rq->cpu)) {
1441 * Tick is needed if more than one task runs on a CPU.
1442 * Send the target an IPI to kick it out of nohz mode.
1444 * We assume that IPI implies full memory barrier and the
1445 * new value of rq->nr_running is visible on reception
1448 tick_nohz_full_kick_cpu(rq->cpu);
1454 static inline void __sub_nr_running(struct rq *rq, unsigned count)
1456 rq->nr_running -= count;
1459 #ifdef CONFIG_CPU_QUIET
1460 #define NR_AVE_SCALE(x) ((x) << FSHIFT)
1461 static inline u64 do_nr_running_integral(struct rq *rq)
1464 u64 nr_running_integral = rq->nr_running_integral;
1466 deltax = rq->clock_task - rq->nr_last_stamp;
1467 nr = NR_AVE_SCALE(rq->nr_running);
1469 nr_running_integral += nr * deltax;
1471 return nr_running_integral;
1474 static inline void add_nr_running(struct rq *rq, unsigned count)
1476 write_seqcount_begin(&rq->ave_seqcnt);
1477 rq->nr_running_integral = do_nr_running_integral(rq);
1478 rq->nr_last_stamp = rq->clock_task;
1479 __add_nr_running(rq, count);
1480 write_seqcount_end(&rq->ave_seqcnt);
1483 static inline void sub_nr_running(struct rq *rq, unsigned count)
1485 write_seqcount_begin(&rq->ave_seqcnt);
1486 rq->nr_running_integral = do_nr_running_integral(rq);
1487 rq->nr_last_stamp = rq->clock_task;
1488 __sub_nr_running(rq, count);
1489 write_seqcount_end(&rq->ave_seqcnt);
1492 #define add_nr_running __add_nr_running
1493 #define sub_nr_running __sub_nr_running
1496 static inline void rq_last_tick_reset(struct rq *rq)
1498 #ifdef CONFIG_NO_HZ_FULL
1499 rq->last_sched_tick = jiffies;
1503 extern void update_rq_clock(struct rq *rq);
1505 extern void activate_task(struct rq *rq, struct task_struct *p, int flags);
1506 extern void deactivate_task(struct rq *rq, struct task_struct *p, int flags);
1508 extern void check_preempt_curr(struct rq *rq, struct task_struct *p, int flags);
1510 extern const_debug unsigned int sysctl_sched_time_avg;
1511 extern const_debug unsigned int sysctl_sched_nr_migrate;
1512 extern const_debug unsigned int sysctl_sched_migration_cost;
1514 static inline u64 sched_avg_period(void)
1516 return (u64)sysctl_sched_time_avg * NSEC_PER_MSEC / 2;
1519 #ifdef CONFIG_SCHED_HRTICK
1523 * - enabled by features
1524 * - hrtimer is actually high res
1526 static inline int hrtick_enabled(struct rq *rq)
1528 if (!sched_feat(HRTICK))
1530 if (!cpu_active(cpu_of(rq)))
1532 return hrtimer_is_hres_active(&rq->hrtick_timer);
1535 void hrtick_start(struct rq *rq, u64 delay);
1539 static inline int hrtick_enabled(struct rq *rq)
1544 #endif /* CONFIG_SCHED_HRTICK */
1547 extern void sched_avg_update(struct rq *rq);
1549 #ifndef arch_scale_freq_capacity
1550 static __always_inline
1551 unsigned long arch_scale_freq_capacity(struct sched_domain *sd, int cpu)
1553 return SCHED_CAPACITY_SCALE;
1557 #ifndef arch_scale_cpu_capacity
1558 static __always_inline
1559 unsigned long arch_scale_cpu_capacity(struct sched_domain *sd, int cpu)
1561 if (sd && (sd->flags & SD_SHARE_CPUCAPACITY) && (sd->span_weight > 1))
1562 return sd->smt_gain / sd->span_weight;
1564 return SCHED_CAPACITY_SCALE;
1569 static inline unsigned long capacity_of(int cpu)
1571 return cpu_rq(cpu)->cpu_capacity;
1574 static inline unsigned long capacity_orig_of(int cpu)
1576 return cpu_rq(cpu)->cpu_capacity_orig;
1579 extern unsigned int sysctl_sched_use_walt_cpu_util;
1580 extern unsigned int walt_ravg_window;
1581 extern bool walt_disabled;
1584 * cpu_util returns the amount of capacity of a CPU that is used by CFS
1585 * tasks. The unit of the return value must be the one of capacity so we can
1586 * compare the utilization with the capacity of the CPU that is available for
1587 * CFS task (ie cpu_capacity).
1589 * cfs_rq.avg.util_avg is the sum of running time of runnable tasks plus the
1590 * recent utilization of currently non-runnable tasks on a CPU. It represents
1591 * the amount of utilization of a CPU in the range [0..capacity_orig] where
1592 * capacity_orig is the cpu_capacity available at the highest frequency
1593 * (arch_scale_freq_capacity()).
1594 * The utilization of a CPU converges towards a sum equal to or less than the
1595 * current capacity (capacity_curr <= capacity_orig) of the CPU because it is
1596 * the running time on this CPU scaled by capacity_curr.
1598 * Nevertheless, cfs_rq.avg.util_avg can be higher than capacity_curr or even
1599 * higher than capacity_orig because of unfortunate rounding in
1600 * cfs.avg.util_avg or just after migrating tasks and new task wakeups until
1601 * the average stabilizes with the new running time. We need to check that the
1602 * utilization stays within the range of [0..capacity_orig] and cap it if
1603 * necessary. Without utilization capping, a group could be seen as overloaded
1604 * (CPU0 utilization at 121% + CPU1 utilization at 80%) whereas CPU1 has 20% of
1605 * available capacity. We allow utilization to overshoot capacity_curr (but not
1606 * capacity_orig) as it useful for predicting the capacity required after task
1607 * migrations (scheduler-driven DVFS).
1609 static inline unsigned long __cpu_util(int cpu, int delta)
1611 unsigned long util = cpu_rq(cpu)->cfs.avg.util_avg;
1612 unsigned long capacity = capacity_orig_of(cpu);
1614 #ifdef CONFIG_SCHED_WALT
1615 if (!walt_disabled && sysctl_sched_use_walt_cpu_util)
1616 util = div64_u64(cpu_rq(cpu)->cumulative_runnable_avg,
1617 walt_ravg_window >> SCHED_LOAD_SHIFT);
1623 return (delta >= capacity) ? capacity : delta;
1626 static inline unsigned long cpu_util(int cpu)
1628 return __cpu_util(cpu, 0);
1631 static inline unsigned long cpu_util_freq(int cpu)
1633 unsigned long util = cpu_rq(cpu)->cfs.avg.util_avg;
1634 unsigned long capacity = capacity_orig_of(cpu);
1636 #ifdef CONFIG_SCHED_WALT
1637 if (!walt_disabled && sysctl_sched_use_walt_cpu_util)
1638 util = div64_u64(cpu_rq(cpu)->prev_runnable_sum,
1639 walt_ravg_window >> SCHED_LOAD_SHIFT);
1641 return (util >= capacity) ? capacity : util;
1646 static inline void sched_rt_avg_update(struct rq *rq, u64 rt_delta)
1648 rq->rt_avg += rt_delta * arch_scale_freq_capacity(NULL, cpu_of(rq));
1651 static inline void sched_rt_avg_update(struct rq *rq, u64 rt_delta) { }
1652 static inline void sched_avg_update(struct rq *rq) { }
1656 * __task_rq_lock - lock the rq @p resides on.
1658 static inline struct rq *__task_rq_lock(struct task_struct *p)
1659 __acquires(rq->lock)
1663 lockdep_assert_held(&p->pi_lock);
1667 raw_spin_lock(&rq->lock);
1668 if (likely(rq == task_rq(p) && !task_on_rq_migrating(p))) {
1669 lockdep_pin_lock(&rq->lock);
1672 raw_spin_unlock(&rq->lock);
1674 while (unlikely(task_on_rq_migrating(p)))
1680 * task_rq_lock - lock p->pi_lock and lock the rq @p resides on.
1682 static inline struct rq *task_rq_lock(struct task_struct *p, unsigned long *flags)
1683 __acquires(p->pi_lock)
1684 __acquires(rq->lock)
1689 raw_spin_lock_irqsave(&p->pi_lock, *flags);
1691 raw_spin_lock(&rq->lock);
1693 * move_queued_task() task_rq_lock()
1695 * ACQUIRE (rq->lock)
1696 * [S] ->on_rq = MIGRATING [L] rq = task_rq()
1697 * WMB (__set_task_cpu()) ACQUIRE (rq->lock);
1698 * [S] ->cpu = new_cpu [L] task_rq()
1700 * RELEASE (rq->lock)
1702 * If we observe the old cpu in task_rq_lock, the acquire of
1703 * the old rq->lock will fully serialize against the stores.
1705 * If we observe the new cpu in task_rq_lock, the acquire will
1706 * pair with the WMB to ensure we must then also see migrating.
1708 if (likely(rq == task_rq(p) && !task_on_rq_migrating(p))) {
1709 lockdep_pin_lock(&rq->lock);
1712 raw_spin_unlock(&rq->lock);
1713 raw_spin_unlock_irqrestore(&p->pi_lock, *flags);
1715 while (unlikely(task_on_rq_migrating(p)))
1720 static inline void __task_rq_unlock(struct rq *rq)
1721 __releases(rq->lock)
1723 lockdep_unpin_lock(&rq->lock);
1724 raw_spin_unlock(&rq->lock);
1728 task_rq_unlock(struct rq *rq, struct task_struct *p, unsigned long *flags)
1729 __releases(rq->lock)
1730 __releases(p->pi_lock)
1732 lockdep_unpin_lock(&rq->lock);
1733 raw_spin_unlock(&rq->lock);
1734 raw_spin_unlock_irqrestore(&p->pi_lock, *flags);
1737 extern struct rq *lock_rq_of(struct task_struct *p, unsigned long *flags);
1738 extern void unlock_rq_of(struct rq *rq, struct task_struct *p, unsigned long *flags);
1741 #ifdef CONFIG_PREEMPT
1743 static inline void double_rq_lock(struct rq *rq1, struct rq *rq2);
1746 * fair double_lock_balance: Safely acquires both rq->locks in a fair
1747 * way at the expense of forcing extra atomic operations in all
1748 * invocations. This assures that the double_lock is acquired using the
1749 * same underlying policy as the spinlock_t on this architecture, which
1750 * reduces latency compared to the unfair variant below. However, it
1751 * also adds more overhead and therefore may reduce throughput.
1753 static inline int _double_lock_balance(struct rq *this_rq, struct rq *busiest)
1754 __releases(this_rq->lock)
1755 __acquires(busiest->lock)
1756 __acquires(this_rq->lock)
1758 raw_spin_unlock(&this_rq->lock);
1759 double_rq_lock(this_rq, busiest);
1766 * Unfair double_lock_balance: Optimizes throughput at the expense of
1767 * latency by eliminating extra atomic operations when the locks are
1768 * already in proper order on entry. This favors lower cpu-ids and will
1769 * grant the double lock to lower cpus over higher ids under contention,
1770 * regardless of entry order into the function.
1772 static inline int _double_lock_balance(struct rq *this_rq, struct rq *busiest)
1773 __releases(this_rq->lock)
1774 __acquires(busiest->lock)
1775 __acquires(this_rq->lock)
1779 if (unlikely(!raw_spin_trylock(&busiest->lock))) {
1780 if (busiest < this_rq) {
1781 raw_spin_unlock(&this_rq->lock);
1782 raw_spin_lock(&busiest->lock);
1783 raw_spin_lock_nested(&this_rq->lock,
1784 SINGLE_DEPTH_NESTING);
1787 raw_spin_lock_nested(&busiest->lock,
1788 SINGLE_DEPTH_NESTING);
1793 #endif /* CONFIG_PREEMPT */
1796 * double_lock_balance - lock the busiest runqueue, this_rq is locked already.
1798 static inline int double_lock_balance(struct rq *this_rq, struct rq *busiest)
1800 if (unlikely(!irqs_disabled())) {
1801 /* printk() doesn't work good under rq->lock */
1802 raw_spin_unlock(&this_rq->lock);
1806 return _double_lock_balance(this_rq, busiest);
1809 static inline void double_unlock_balance(struct rq *this_rq, struct rq *busiest)
1810 __releases(busiest->lock)
1812 if (this_rq != busiest)
1813 raw_spin_unlock(&busiest->lock);
1814 lock_set_subclass(&this_rq->lock.dep_map, 0, _RET_IP_);
1817 static inline void double_lock(spinlock_t *l1, spinlock_t *l2)
1823 spin_lock_nested(l2, SINGLE_DEPTH_NESTING);
1826 static inline void double_lock_irq(spinlock_t *l1, spinlock_t *l2)
1832 spin_lock_nested(l2, SINGLE_DEPTH_NESTING);
1835 static inline void double_raw_lock(raw_spinlock_t *l1, raw_spinlock_t *l2)
1841 raw_spin_lock_nested(l2, SINGLE_DEPTH_NESTING);
1845 * double_rq_lock - safely lock two runqueues
1847 * Note this does not disable interrupts like task_rq_lock,
1848 * you need to do so manually before calling.
1850 static inline void double_rq_lock(struct rq *rq1, struct rq *rq2)
1851 __acquires(rq1->lock)
1852 __acquires(rq2->lock)
1854 BUG_ON(!irqs_disabled());
1856 raw_spin_lock(&rq1->lock);
1857 __acquire(rq2->lock); /* Fake it out ;) */
1860 raw_spin_lock(&rq1->lock);
1861 raw_spin_lock_nested(&rq2->lock, SINGLE_DEPTH_NESTING);
1863 raw_spin_lock(&rq2->lock);
1864 raw_spin_lock_nested(&rq1->lock, SINGLE_DEPTH_NESTING);
1870 * double_rq_unlock - safely unlock two runqueues
1872 * Note this does not restore interrupts like task_rq_unlock,
1873 * you need to do so manually after calling.
1875 static inline void double_rq_unlock(struct rq *rq1, struct rq *rq2)
1876 __releases(rq1->lock)
1877 __releases(rq2->lock)
1879 raw_spin_unlock(&rq1->lock);
1881 raw_spin_unlock(&rq2->lock);
1883 __release(rq2->lock);
1886 #else /* CONFIG_SMP */
1889 * double_rq_lock - safely lock two runqueues
1891 * Note this does not disable interrupts like task_rq_lock,
1892 * you need to do so manually before calling.
1894 static inline void double_rq_lock(struct rq *rq1, struct rq *rq2)
1895 __acquires(rq1->lock)
1896 __acquires(rq2->lock)
1898 BUG_ON(!irqs_disabled());
1900 raw_spin_lock(&rq1->lock);
1901 __acquire(rq2->lock); /* Fake it out ;) */
1905 * double_rq_unlock - safely unlock two runqueues
1907 * Note this does not restore interrupts like task_rq_unlock,
1908 * you need to do so manually after calling.
1910 static inline void double_rq_unlock(struct rq *rq1, struct rq *rq2)
1911 __releases(rq1->lock)
1912 __releases(rq2->lock)
1915 raw_spin_unlock(&rq1->lock);
1916 __release(rq2->lock);
1921 extern struct sched_entity *__pick_first_entity(struct cfs_rq *cfs_rq);
1922 extern struct sched_entity *__pick_last_entity(struct cfs_rq *cfs_rq);
1924 #ifdef CONFIG_SCHED_DEBUG
1925 extern void print_cfs_stats(struct seq_file *m, int cpu);
1926 extern void print_rt_stats(struct seq_file *m, int cpu);
1927 extern void print_dl_stats(struct seq_file *m, int cpu);
1929 print_cfs_rq(struct seq_file *m, int cpu, struct cfs_rq *cfs_rq);
1931 #ifdef CONFIG_NUMA_BALANCING
1933 show_numa_stats(struct task_struct *p, struct seq_file *m);
1935 print_numa_stats(struct seq_file *m, int node, unsigned long tsf,
1936 unsigned long tpf, unsigned long gsf, unsigned long gpf);
1937 #endif /* CONFIG_NUMA_BALANCING */
1938 #endif /* CONFIG_SCHED_DEBUG */
1940 extern void init_cfs_rq(struct cfs_rq *cfs_rq);
1941 extern void init_rt_rq(struct rt_rq *rt_rq);
1942 extern void init_dl_rq(struct dl_rq *dl_rq);
1944 extern void cfs_bandwidth_usage_inc(void);
1945 extern void cfs_bandwidth_usage_dec(void);
1947 #ifdef CONFIG_NO_HZ_COMMON
1948 enum rq_nohz_flag_bits {
1953 #define nohz_flags(cpu) (&cpu_rq(cpu)->nohz_flags)
1956 #ifdef CONFIG_IRQ_TIME_ACCOUNTING
1958 DECLARE_PER_CPU(u64, cpu_hardirq_time);
1959 DECLARE_PER_CPU(u64, cpu_softirq_time);
1961 #ifndef CONFIG_64BIT
1962 DECLARE_PER_CPU(seqcount_t, irq_time_seq);
1964 static inline void irq_time_write_begin(void)
1966 __this_cpu_inc(irq_time_seq.sequence);
1970 static inline void irq_time_write_end(void)
1973 __this_cpu_inc(irq_time_seq.sequence);
1976 static inline u64 irq_time_read(int cpu)
1982 seq = read_seqcount_begin(&per_cpu(irq_time_seq, cpu));
1983 irq_time = per_cpu(cpu_softirq_time, cpu) +
1984 per_cpu(cpu_hardirq_time, cpu);
1985 } while (read_seqcount_retry(&per_cpu(irq_time_seq, cpu), seq));
1989 #else /* CONFIG_64BIT */
1990 static inline void irq_time_write_begin(void)
1994 static inline void irq_time_write_end(void)
1998 static inline u64 irq_time_read(int cpu)
2000 return per_cpu(cpu_softirq_time, cpu) + per_cpu(cpu_hardirq_time, cpu);
2002 #endif /* CONFIG_64BIT */
2003 #endif /* CONFIG_IRQ_TIME_ACCOUNTING */
2005 #ifdef CONFIG_CPU_FREQ
2006 DECLARE_PER_CPU(struct update_util_data *, cpufreq_update_util_data);
2009 * cpufreq_update_util - Take a note about CPU utilization changes.
2010 * @rq: Runqueue to carry out the update for.
2011 * @flags: Update reason flags.
2013 * This function is called by the scheduler on the CPU whose utilization is
2016 * It can only be called from RCU-sched read-side critical sections.
2018 * The way cpufreq is currently arranged requires it to evaluate the CPU
2019 * performance state (frequency/voltage) on a regular basis to prevent it from
2020 * being stuck in a completely inadequate performance level for too long.
2021 * That is not guaranteed to happen if the updates are only triggered from CFS,
2022 * though, because they may not be coming in if RT or deadline tasks are active
2023 * all the time (or there are RT and DL tasks only).
2025 * As a workaround for that issue, this function is called by the RT and DL
2026 * sched classes to trigger extra cpufreq updates to prevent it from stalling,
2027 * but that really is a band-aid. Going forward it should be replaced with
2028 * solutions targeted more specifically at RT and DL tasks.
2030 static inline void cpufreq_update_util(struct rq *rq, unsigned int flags)
2032 struct update_util_data *data;
2034 data = rcu_dereference_sched(*this_cpu_ptr(&cpufreq_update_util_data));
2036 data->func(data, rq_clock(rq), flags);
2039 static inline void cpufreq_update_this_cpu(struct rq *rq, unsigned int flags)
2041 if (cpu_of(rq) == smp_processor_id())
2042 cpufreq_update_util(rq, flags);
2045 static inline void cpufreq_update_util(struct rq *rq, unsigned int flags) {}
2046 static inline void cpufreq_update_this_cpu(struct rq *rq, unsigned int flags) {}
2047 #endif /* CONFIG_CPU_FREQ */
2049 #ifdef CONFIG_SCHED_WALT
2052 walt_task_in_cum_window_demand(struct rq *rq, struct task_struct *p)
2054 return cpu_of(rq) == task_cpu(p) &&
2055 (p->on_rq || p->last_sleep_ts >= rq->window_start);
2058 #endif /* CONFIG_SCHED_WALT */
2060 #ifdef arch_scale_freq_capacity
2061 #ifndef arch_scale_freq_invariant
2062 #define arch_scale_freq_invariant() (true)
2064 #else /* arch_scale_freq_capacity */
2065 #define arch_scale_freq_invariant() (false)