2 #include <linux/sched.h>
3 #include <linux/sched/sysctl.h>
4 #include <linux/sched/topology.h>
5 #include <linux/sched/rt.h>
6 #include <linux/sched/clock.h>
7 #include <linux/sched/wake_q.h>
8 #include <linux/sched/mm.h>
9 #include <linux/u64_stats_sync.h>
10 #include <linux/sched/deadline.h>
11 #include <linux/kernel_stat.h>
12 #include <linux/binfmts.h>
13 #include <linux/mutex.h>
14 #include <linux/spinlock.h>
15 #include <linux/stop_machine.h>
16 #include <linux/irq_work.h>
17 #include <linux/tick.h>
18 #include <linux/slab.h>
21 #include "cpudeadline.h"
24 #ifdef CONFIG_SCHED_DEBUG
25 #define SCHED_WARN_ON(x) WARN_ONCE(x, #x)
27 #define SCHED_WARN_ON(x) ((void)(x))
33 /* task_struct::on_rq states: */
34 #define TASK_ON_RQ_QUEUED 1
35 #define TASK_ON_RQ_MIGRATING 2
37 extern __read_mostly
int scheduler_running
;
39 extern unsigned long calc_load_update
;
40 extern atomic_long_t calc_load_tasks
;
42 extern void calc_global_load_tick(struct rq
*this_rq
);
43 extern long calc_load_fold_active(struct rq
*this_rq
, long adjust
);
46 extern void cpu_load_update_active(struct rq
*this_rq
);
48 static inline void cpu_load_update_active(struct rq
*this_rq
) { }
52 * Helpers for converting nanosecond timing to jiffy resolution
54 #define NS_TO_JIFFIES(TIME) ((unsigned long)(TIME) / (NSEC_PER_SEC / HZ))
57 * Increase resolution of nice-level calculations for 64-bit architectures.
58 * The extra resolution improves shares distribution and load balancing of
59 * low-weight task groups (eg. nice +19 on an autogroup), deeper taskgroup
60 * hierarchies, especially on larger systems. This is not a user-visible change
61 * and does not change the user-interface for setting shares/weights.
63 * We increase resolution only if we have enough bits to allow this increased
64 * resolution (i.e. 64bit). The costs for increasing resolution when 32bit are
65 * pretty high and the returns do not justify the increased costs.
67 * Really only required when CONFIG_FAIR_GROUP_SCHED is also set, but to
68 * increase coverage and consistency always enable it on 64bit platforms.
71 # define NICE_0_LOAD_SHIFT (SCHED_FIXEDPOINT_SHIFT + SCHED_FIXEDPOINT_SHIFT)
72 # define scale_load(w) ((w) << SCHED_FIXEDPOINT_SHIFT)
73 # define scale_load_down(w) ((w) >> SCHED_FIXEDPOINT_SHIFT)
75 # define NICE_0_LOAD_SHIFT (SCHED_FIXEDPOINT_SHIFT)
76 # define scale_load(w) (w)
77 # define scale_load_down(w) (w)
81 * Task weight (visible to users) and its load (invisible to users) have
82 * independent resolution, but they should be well calibrated. We use
83 * scale_load() and scale_load_down(w) to convert between them. The
84 * following must be true:
86 * scale_load(sched_prio_to_weight[USER_PRIO(NICE_TO_PRIO(0))]) == NICE_0_LOAD
89 #define NICE_0_LOAD (1L << NICE_0_LOAD_SHIFT)
92 * Single value that decides SCHED_DEADLINE internal math precision.
93 * 10 -> just above 1us
94 * 9 -> just above 0.5us
99 * These are the 'tuning knobs' of the scheduler:
103 * single value that denotes runtime == period, ie unlimited time.
105 #define RUNTIME_INF ((u64)~0ULL)
107 static inline int idle_policy(int policy
)
109 return policy
== SCHED_IDLE
;
111 static inline int fair_policy(int policy
)
113 return policy
== SCHED_NORMAL
|| policy
== SCHED_BATCH
;
116 static inline int rt_policy(int policy
)
118 return policy
== SCHED_FIFO
|| policy
== SCHED_RR
;
121 static inline int dl_policy(int policy
)
123 return policy
== SCHED_DEADLINE
;
125 static inline bool valid_policy(int policy
)
127 return idle_policy(policy
) || fair_policy(policy
) ||
128 rt_policy(policy
) || dl_policy(policy
);
131 static inline int task_has_rt_policy(struct task_struct
*p
)
133 return rt_policy(p
->policy
);
136 static inline int task_has_dl_policy(struct task_struct
*p
)
138 return dl_policy(p
->policy
);
142 * Tells if entity @a should preempt entity @b.
145 dl_entity_preempt(struct sched_dl_entity
*a
, struct sched_dl_entity
*b
)
147 return dl_time_before(a
->deadline
, b
->deadline
);
151 * This is the priority-queue data structure of the RT scheduling class:
153 struct rt_prio_array
{
154 DECLARE_BITMAP(bitmap
, MAX_RT_PRIO
+1); /* include 1 bit for delimiter */
155 struct list_head queue
[MAX_RT_PRIO
];
158 struct rt_bandwidth
{
159 /* nests inside the rq lock: */
160 raw_spinlock_t rt_runtime_lock
;
163 struct hrtimer rt_period_timer
;
164 unsigned int rt_period_active
;
167 void __dl_clear_params(struct task_struct
*p
);
170 * To keep the bandwidth of -deadline tasks and groups under control
171 * we need some place where:
172 * - store the maximum -deadline bandwidth of the system (the group);
173 * - cache the fraction of that bandwidth that is currently allocated.
175 * This is all done in the data structure below. It is similar to the
176 * one used for RT-throttling (rt_bandwidth), with the main difference
177 * that, since here we are only interested in admission control, we
178 * do not decrease any runtime while the group "executes", neither we
179 * need a timer to replenish it.
181 * With respect to SMP, the bandwidth is given on a per-CPU basis,
183 * - dl_bw (< 100%) is the bandwidth of the system (group) on each CPU;
184 * - dl_total_bw array contains, in the i-eth element, the currently
185 * allocated bandwidth on the i-eth CPU.
186 * Moreover, groups consume bandwidth on each CPU, while tasks only
187 * consume bandwidth on the CPU they're running on.
188 * Finally, dl_total_bw_cpu is used to cache the index of dl_total_bw
189 * that will be shown the next time the proc or cgroup controls will
190 * be red. It on its turn can be changed by writing on its own
193 struct dl_bandwidth
{
194 raw_spinlock_t dl_runtime_lock
;
199 static inline int dl_bandwidth_enabled(void)
201 return sysctl_sched_rt_runtime
>= 0;
204 extern struct dl_bw
*dl_bw_of(int i
);
212 void __dl_clear(struct dl_bw
*dl_b
, u64 tsk_bw
)
214 dl_b
->total_bw
-= tsk_bw
;
218 void __dl_add(struct dl_bw
*dl_b
, u64 tsk_bw
)
220 dl_b
->total_bw
+= tsk_bw
;
224 bool __dl_overflow(struct dl_bw
*dl_b
, int cpus
, u64 old_bw
, u64 new_bw
)
226 return dl_b
->bw
!= -1 &&
227 dl_b
->bw
* cpus
< dl_b
->total_bw
- old_bw
+ new_bw
;
230 extern void init_dl_bw(struct dl_bw
*dl_b
);
232 #ifdef CONFIG_CGROUP_SCHED
234 #include <linux/cgroup.h>
239 extern struct list_head task_groups
;
241 struct cfs_bandwidth
{
242 #ifdef CONFIG_CFS_BANDWIDTH
246 s64 hierarchical_quota
;
249 int idle
, period_active
;
250 struct hrtimer period_timer
, slack_timer
;
251 struct list_head throttled_cfs_rq
;
254 int nr_periods
, nr_throttled
;
259 /* task group related information */
261 struct cgroup_subsys_state css
;
263 #ifdef CONFIG_FAIR_GROUP_SCHED
264 /* schedulable entities of this group on each cpu */
265 struct sched_entity
**se
;
266 /* runqueue "owned" by this group on each cpu */
267 struct cfs_rq
**cfs_rq
;
268 unsigned long shares
;
272 * load_avg can be heavily contended at clock tick time, so put
273 * it in its own cacheline separated from the fields above which
274 * will also be accessed at each tick.
276 atomic_long_t load_avg ____cacheline_aligned
;
280 #ifdef CONFIG_RT_GROUP_SCHED
281 struct sched_rt_entity
**rt_se
;
282 struct rt_rq
**rt_rq
;
284 struct rt_bandwidth rt_bandwidth
;
288 struct list_head list
;
290 struct task_group
*parent
;
291 struct list_head siblings
;
292 struct list_head children
;
294 #ifdef CONFIG_SCHED_AUTOGROUP
295 struct autogroup
*autogroup
;
298 struct cfs_bandwidth cfs_bandwidth
;
301 #ifdef CONFIG_FAIR_GROUP_SCHED
302 #define ROOT_TASK_GROUP_LOAD NICE_0_LOAD
305 * A weight of 0 or 1 can cause arithmetics problems.
306 * A weight of a cfs_rq is the sum of weights of which entities
307 * are queued on this cfs_rq, so a weight of a entity should not be
308 * too large, so as the shares value of a task group.
309 * (The default weight is 1024 - so there's no practical
310 * limitation from this.)
312 #define MIN_SHARES (1UL << 1)
313 #define MAX_SHARES (1UL << 18)
316 typedef int (*tg_visitor
)(struct task_group
*, void *);
318 extern int walk_tg_tree_from(struct task_group
*from
,
319 tg_visitor down
, tg_visitor up
, void *data
);
322 * Iterate the full tree, calling @down when first entering a node and @up when
323 * leaving it for the final time.
325 * Caller must hold rcu_lock or sufficient equivalent.
327 static inline int walk_tg_tree(tg_visitor down
, tg_visitor up
, void *data
)
329 return walk_tg_tree_from(&root_task_group
, down
, up
, data
);
332 extern int tg_nop(struct task_group
*tg
, void *data
);
334 extern void free_fair_sched_group(struct task_group
*tg
);
335 extern int alloc_fair_sched_group(struct task_group
*tg
, struct task_group
*parent
);
336 extern void online_fair_sched_group(struct task_group
*tg
);
337 extern void unregister_fair_sched_group(struct task_group
*tg
);
338 extern void init_tg_cfs_entry(struct task_group
*tg
, struct cfs_rq
*cfs_rq
,
339 struct sched_entity
*se
, int cpu
,
340 struct sched_entity
*parent
);
341 extern void init_cfs_bandwidth(struct cfs_bandwidth
*cfs_b
);
343 extern void __refill_cfs_bandwidth_runtime(struct cfs_bandwidth
*cfs_b
);
344 extern void start_cfs_bandwidth(struct cfs_bandwidth
*cfs_b
);
345 extern void unthrottle_cfs_rq(struct cfs_rq
*cfs_rq
);
347 extern void free_rt_sched_group(struct task_group
*tg
);
348 extern int alloc_rt_sched_group(struct task_group
*tg
, struct task_group
*parent
);
349 extern void init_tg_rt_entry(struct task_group
*tg
, struct rt_rq
*rt_rq
,
350 struct sched_rt_entity
*rt_se
, int cpu
,
351 struct sched_rt_entity
*parent
);
353 extern struct task_group
*sched_create_group(struct task_group
*parent
);
354 extern void sched_online_group(struct task_group
*tg
,
355 struct task_group
*parent
);
356 extern void sched_destroy_group(struct task_group
*tg
);
357 extern void sched_offline_group(struct task_group
*tg
);
359 extern void sched_move_task(struct task_struct
*tsk
);
361 #ifdef CONFIG_FAIR_GROUP_SCHED
362 extern int sched_group_set_shares(struct task_group
*tg
, unsigned long shares
);
365 extern void set_task_rq_fair(struct sched_entity
*se
,
366 struct cfs_rq
*prev
, struct cfs_rq
*next
);
367 #else /* !CONFIG_SMP */
368 static inline void set_task_rq_fair(struct sched_entity
*se
,
369 struct cfs_rq
*prev
, struct cfs_rq
*next
) { }
370 #endif /* CONFIG_SMP */
371 #endif /* CONFIG_FAIR_GROUP_SCHED */
373 #else /* CONFIG_CGROUP_SCHED */
375 struct cfs_bandwidth
{ };
377 #endif /* CONFIG_CGROUP_SCHED */
379 /* CFS-related fields in a runqueue */
381 struct load_weight load
;
382 unsigned int nr_running
, h_nr_running
;
387 u64 min_vruntime_copy
;
390 struct rb_root tasks_timeline
;
391 struct rb_node
*rb_leftmost
;
394 * 'curr' points to currently running entity on this cfs_rq.
395 * It is set to NULL otherwise (i.e when none are currently running).
397 struct sched_entity
*curr
, *next
, *last
, *skip
;
399 #ifdef CONFIG_SCHED_DEBUG
400 unsigned int nr_spread_over
;
407 struct sched_avg avg
;
408 u64 runnable_load_sum
;
409 unsigned long runnable_load_avg
;
410 #ifdef CONFIG_FAIR_GROUP_SCHED
411 unsigned long tg_load_avg_contrib
;
412 unsigned long propagate_avg
;
414 atomic_long_t removed_load_avg
, removed_util_avg
;
416 u64 load_last_update_time_copy
;
419 #ifdef CONFIG_FAIR_GROUP_SCHED
421 * h_load = weight * f(tg)
423 * Where f(tg) is the recursive weight fraction assigned to
426 unsigned long h_load
;
427 u64 last_h_load_update
;
428 struct sched_entity
*h_load_next
;
429 #endif /* CONFIG_FAIR_GROUP_SCHED */
430 #endif /* CONFIG_SMP */
432 #ifdef CONFIG_FAIR_GROUP_SCHED
433 struct rq
*rq
; /* cpu runqueue to which this cfs_rq is attached */
436 * leaf cfs_rqs are those that hold tasks (lowest schedulable entity in
437 * a hierarchy). Non-leaf lrqs hold other higher schedulable entities
438 * (like users, containers etc.)
440 * leaf_cfs_rq_list ties together list of leaf cfs_rq's in a cpu. This
441 * list is used during load balance.
444 struct list_head leaf_cfs_rq_list
;
445 struct task_group
*tg
; /* group that "owns" this runqueue */
447 #ifdef CONFIG_CFS_BANDWIDTH
450 s64 runtime_remaining
;
452 u64 throttled_clock
, throttled_clock_task
;
453 u64 throttled_clock_task_time
;
454 int throttled
, throttle_count
;
455 struct list_head throttled_list
;
456 #endif /* CONFIG_CFS_BANDWIDTH */
457 #endif /* CONFIG_FAIR_GROUP_SCHED */
460 static inline int rt_bandwidth_enabled(void)
462 return sysctl_sched_rt_runtime
>= 0;
465 /* RT IPI pull logic requires IRQ_WORK */
466 #ifdef CONFIG_IRQ_WORK
467 # define HAVE_RT_PUSH_IPI
470 /* Real-Time classes' related field in a runqueue: */
472 struct rt_prio_array active
;
473 unsigned int rt_nr_running
;
474 unsigned int rr_nr_running
;
475 #if defined CONFIG_SMP || defined CONFIG_RT_GROUP_SCHED
477 int curr
; /* highest queued rt task prio */
479 int next
; /* next highest */
484 unsigned long rt_nr_migratory
;
485 unsigned long rt_nr_total
;
487 struct plist_head pushable_tasks
;
488 #ifdef HAVE_RT_PUSH_IPI
491 struct irq_work push_work
;
492 raw_spinlock_t push_lock
;
494 #endif /* CONFIG_SMP */
500 /* Nests inside the rq lock: */
501 raw_spinlock_t rt_runtime_lock
;
503 #ifdef CONFIG_RT_GROUP_SCHED
504 unsigned long rt_nr_boosted
;
507 struct task_group
*tg
;
511 /* Deadline class' related fields in a runqueue */
513 /* runqueue is an rbtree, ordered by deadline */
514 struct rb_root rb_root
;
515 struct rb_node
*rb_leftmost
;
517 unsigned long dl_nr_running
;
521 * Deadline values of the currently executing and the
522 * earliest ready task on this rq. Caching these facilitates
523 * the decision wether or not a ready but not running task
524 * should migrate somewhere else.
531 unsigned long dl_nr_migratory
;
535 * Tasks on this rq that can be pushed away. They are kept in
536 * an rb-tree, ordered by tasks' deadlines, with caching
537 * of the leftmost (earliest deadline) element.
539 struct rb_root pushable_dl_tasks_root
;
540 struct rb_node
*pushable_dl_tasks_leftmost
;
548 static inline bool sched_asym_prefer(int a
, int b
)
550 return arch_asym_cpu_priority(a
) > arch_asym_cpu_priority(b
);
554 * We add the notion of a root-domain which will be used to define per-domain
555 * variables. Each exclusive cpuset essentially defines an island domain by
556 * fully partitioning the member cpus from any other cpuset. Whenever a new
557 * exclusive cpuset is created, we also create and attach a new root-domain
566 cpumask_var_t online
;
568 /* Indicate more than one runnable task for any CPU */
572 * The bit corresponding to a CPU gets set here if such CPU has more
573 * than one runnable -deadline task (as it is below for RT tasks).
575 cpumask_var_t dlo_mask
;
581 * The "RT overload" flag: it gets set if a CPU has more than
582 * one runnable RT task.
584 cpumask_var_t rto_mask
;
585 struct cpupri cpupri
;
587 unsigned long max_cpu_capacity
;
590 extern struct root_domain def_root_domain
;
591 extern struct mutex sched_domains_mutex
;
592 extern cpumask_var_t fallback_doms
;
593 extern cpumask_var_t sched_domains_tmpmask
;
595 extern void init_defrootdomain(void);
596 extern int init_sched_domains(const struct cpumask
*cpu_map
);
597 extern void rq_attach_root(struct rq
*rq
, struct root_domain
*rd
);
599 #endif /* CONFIG_SMP */
602 * This is the main, per-CPU runqueue data structure.
604 * Locking rule: those places that want to lock multiple runqueues
605 * (such as the load balancing or the thread migration code), lock
606 * acquire operations must be ordered by ascending &runqueue.
613 * nr_running and cpu_load should be in the same cacheline because
614 * remote CPUs use both these fields when doing load calculation.
616 unsigned int nr_running
;
617 #ifdef CONFIG_NUMA_BALANCING
618 unsigned int nr_numa_running
;
619 unsigned int nr_preferred_running
;
621 #define CPU_LOAD_IDX_MAX 5
622 unsigned long cpu_load
[CPU_LOAD_IDX_MAX
];
623 #ifdef CONFIG_NO_HZ_COMMON
625 unsigned long last_load_update_tick
;
626 #endif /* CONFIG_SMP */
627 unsigned long nohz_flags
;
628 #endif /* CONFIG_NO_HZ_COMMON */
629 #ifdef CONFIG_NO_HZ_FULL
630 unsigned long last_sched_tick
;
632 /* capture load from *all* tasks on this cpu: */
633 struct load_weight load
;
634 unsigned long nr_load_updates
;
641 #ifdef CONFIG_FAIR_GROUP_SCHED
642 /* list of leaf cfs_rq on this cpu: */
643 struct list_head leaf_cfs_rq_list
;
644 struct list_head
*tmp_alone_branch
;
645 #endif /* CONFIG_FAIR_GROUP_SCHED */
648 * This is part of a global counter where only the total sum
649 * over all CPUs matters. A task can increase this counter on
650 * one CPU and if it got migrated afterwards it may decrease
651 * it on another CPU. Always updated under the runqueue lock:
653 unsigned long nr_uninterruptible
;
655 struct task_struct
*curr
, *idle
, *stop
;
656 unsigned long next_balance
;
657 struct mm_struct
*prev_mm
;
659 unsigned int clock_update_flags
;
666 struct root_domain
*rd
;
667 struct sched_domain
*sd
;
669 unsigned long cpu_capacity
;
670 unsigned long cpu_capacity_orig
;
672 struct callback_head
*balance_callback
;
674 unsigned char idle_balance
;
675 /* For active balancing */
678 struct cpu_stop_work active_balance_work
;
679 /* cpu of this runqueue: */
683 struct list_head cfs_tasks
;
690 /* This is used to determine avg_idle's max value */
691 u64 max_idle_balance_cost
;
694 #ifdef CONFIG_IRQ_TIME_ACCOUNTING
697 #ifdef CONFIG_PARAVIRT
700 #ifdef CONFIG_PARAVIRT_TIME_ACCOUNTING
701 u64 prev_steal_time_rq
;
704 /* calc_load related fields */
705 unsigned long calc_load_update
;
706 long calc_load_active
;
708 #ifdef CONFIG_SCHED_HRTICK
710 int hrtick_csd_pending
;
711 struct call_single_data hrtick_csd
;
713 struct hrtimer hrtick_timer
;
716 #ifdef CONFIG_SCHEDSTATS
718 struct sched_info rq_sched_info
;
719 unsigned long long rq_cpu_time
;
720 /* could above be rq->cfs_rq.exec_clock + rq->rt_rq.rt_runtime ? */
722 /* sys_sched_yield() stats */
723 unsigned int yld_count
;
725 /* schedule() stats */
726 unsigned int sched_count
;
727 unsigned int sched_goidle
;
729 /* try_to_wake_up() stats */
730 unsigned int ttwu_count
;
731 unsigned int ttwu_local
;
735 struct llist_head wake_list
;
738 #ifdef CONFIG_CPU_IDLE
739 /* Must be inspected within a rcu lock section */
740 struct cpuidle_state
*idle_state
;
744 static inline int cpu_of(struct rq
*rq
)
754 #ifdef CONFIG_SCHED_SMT
756 extern struct static_key_false sched_smt_present
;
758 extern void __update_idle_core(struct rq
*rq
);
760 static inline void update_idle_core(struct rq
*rq
)
762 if (static_branch_unlikely(&sched_smt_present
))
763 __update_idle_core(rq
);
767 static inline void update_idle_core(struct rq
*rq
) { }
770 DECLARE_PER_CPU_SHARED_ALIGNED(struct rq
, runqueues
);
772 #define cpu_rq(cpu) (&per_cpu(runqueues, (cpu)))
773 #define this_rq() this_cpu_ptr(&runqueues)
774 #define task_rq(p) cpu_rq(task_cpu(p))
775 #define cpu_curr(cpu) (cpu_rq(cpu)->curr)
776 #define raw_rq() raw_cpu_ptr(&runqueues)
778 static inline u64
__rq_clock_broken(struct rq
*rq
)
780 return READ_ONCE(rq
->clock
);
784 * rq::clock_update_flags bits
786 * %RQCF_REQ_SKIP - will request skipping of clock update on the next
787 * call to __schedule(). This is an optimisation to avoid
788 * neighbouring rq clock updates.
790 * %RQCF_ACT_SKIP - is set from inside of __schedule() when skipping is
791 * in effect and calls to update_rq_clock() are being ignored.
793 * %RQCF_UPDATED - is a debug flag that indicates whether a call has been
794 * made to update_rq_clock() since the last time rq::lock was pinned.
796 * If inside of __schedule(), clock_update_flags will have been
797 * shifted left (a left shift is a cheap operation for the fast path
798 * to promote %RQCF_REQ_SKIP to %RQCF_ACT_SKIP), so you must use,
800 * if (rq-clock_update_flags >= RQCF_UPDATED)
802 * to check if %RQCF_UPADTED is set. It'll never be shifted more than
803 * one position though, because the next rq_unpin_lock() will shift it
806 #define RQCF_REQ_SKIP 0x01
807 #define RQCF_ACT_SKIP 0x02
808 #define RQCF_UPDATED 0x04
810 static inline void assert_clock_updated(struct rq
*rq
)
813 * The only reason for not seeing a clock update since the
814 * last rq_pin_lock() is if we're currently skipping updates.
816 SCHED_WARN_ON(rq
->clock_update_flags
< RQCF_ACT_SKIP
);
819 static inline u64
rq_clock(struct rq
*rq
)
821 lockdep_assert_held(&rq
->lock
);
822 assert_clock_updated(rq
);
827 static inline u64
rq_clock_task(struct rq
*rq
)
829 lockdep_assert_held(&rq
->lock
);
830 assert_clock_updated(rq
);
832 return rq
->clock_task
;
835 static inline void rq_clock_skip_update(struct rq
*rq
, bool skip
)
837 lockdep_assert_held(&rq
->lock
);
839 rq
->clock_update_flags
|= RQCF_REQ_SKIP
;
841 rq
->clock_update_flags
&= ~RQCF_REQ_SKIP
;
846 struct pin_cookie cookie
;
847 #ifdef CONFIG_SCHED_DEBUG
849 * A copy of (rq::clock_update_flags & RQCF_UPDATED) for the
850 * current pin context is stashed here in case it needs to be
851 * restored in rq_repin_lock().
853 unsigned int clock_update_flags
;
857 static inline void rq_pin_lock(struct rq
*rq
, struct rq_flags
*rf
)
859 rf
->cookie
= lockdep_pin_lock(&rq
->lock
);
861 #ifdef CONFIG_SCHED_DEBUG
862 rq
->clock_update_flags
&= (RQCF_REQ_SKIP
|RQCF_ACT_SKIP
);
863 rf
->clock_update_flags
= 0;
867 static inline void rq_unpin_lock(struct rq
*rq
, struct rq_flags
*rf
)
869 #ifdef CONFIG_SCHED_DEBUG
870 if (rq
->clock_update_flags
> RQCF_ACT_SKIP
)
871 rf
->clock_update_flags
= RQCF_UPDATED
;
874 lockdep_unpin_lock(&rq
->lock
, rf
->cookie
);
877 static inline void rq_repin_lock(struct rq
*rq
, struct rq_flags
*rf
)
879 lockdep_repin_lock(&rq
->lock
, rf
->cookie
);
881 #ifdef CONFIG_SCHED_DEBUG
883 * Restore the value we stashed in @rf for this pin context.
885 rq
->clock_update_flags
|= rf
->clock_update_flags
;
890 enum numa_topology_type
{
895 extern enum numa_topology_type sched_numa_topology_type
;
896 extern int sched_max_numa_distance
;
897 extern bool find_numa_distance(int distance
);
901 extern void sched_init_numa(void);
902 extern void sched_domains_numa_masks_set(unsigned int cpu
);
903 extern void sched_domains_numa_masks_clear(unsigned int cpu
);
905 static inline void sched_init_numa(void) { }
906 static inline void sched_domains_numa_masks_set(unsigned int cpu
) { }
907 static inline void sched_domains_numa_masks_clear(unsigned int cpu
) { }
910 #ifdef CONFIG_NUMA_BALANCING
911 /* The regions in numa_faults array from task_struct */
912 enum numa_faults_stats
{
918 extern void sched_setnuma(struct task_struct
*p
, int node
);
919 extern int migrate_task_to(struct task_struct
*p
, int cpu
);
920 extern int migrate_swap(struct task_struct
*, struct task_struct
*);
921 #endif /* CONFIG_NUMA_BALANCING */
926 queue_balance_callback(struct rq
*rq
,
927 struct callback_head
*head
,
928 void (*func
)(struct rq
*rq
))
930 lockdep_assert_held(&rq
->lock
);
932 if (unlikely(head
->next
))
935 head
->func
= (void (*)(struct callback_head
*))func
;
936 head
->next
= rq
->balance_callback
;
937 rq
->balance_callback
= head
;
940 extern void sched_ttwu_pending(void);
942 #define rcu_dereference_check_sched_domain(p) \
943 rcu_dereference_check((p), \
944 lockdep_is_held(&sched_domains_mutex))
947 * The domain tree (rq->sd) is protected by RCU's quiescent state transition.
948 * See detach_destroy_domains: synchronize_sched for details.
950 * The domain tree of any CPU may only be accessed from within
951 * preempt-disabled sections.
953 #define for_each_domain(cpu, __sd) \
954 for (__sd = rcu_dereference_check_sched_domain(cpu_rq(cpu)->sd); \
955 __sd; __sd = __sd->parent)
957 #define for_each_lower_domain(sd) for (; sd; sd = sd->child)
960 * highest_flag_domain - Return highest sched_domain containing flag.
961 * @cpu: The cpu whose highest level of sched domain is to
963 * @flag: The flag to check for the highest sched_domain
966 * Returns the highest sched_domain of a cpu which contains the given flag.
968 static inline struct sched_domain
*highest_flag_domain(int cpu
, int flag
)
970 struct sched_domain
*sd
, *hsd
= NULL
;
972 for_each_domain(cpu
, sd
) {
973 if (!(sd
->flags
& flag
))
981 static inline struct sched_domain
*lowest_flag_domain(int cpu
, int flag
)
983 struct sched_domain
*sd
;
985 for_each_domain(cpu
, sd
) {
986 if (sd
->flags
& flag
)
993 DECLARE_PER_CPU(struct sched_domain
*, sd_llc
);
994 DECLARE_PER_CPU(int, sd_llc_size
);
995 DECLARE_PER_CPU(int, sd_llc_id
);
996 DECLARE_PER_CPU(struct sched_domain_shared
*, sd_llc_shared
);
997 DECLARE_PER_CPU(struct sched_domain
*, sd_numa
);
998 DECLARE_PER_CPU(struct sched_domain
*, sd_asym
);
1000 struct sched_group_capacity
{
1003 * CPU capacity of this group, SCHED_CAPACITY_SCALE being max capacity
1006 unsigned long capacity
;
1007 unsigned long min_capacity
; /* Min per-CPU capacity in group */
1008 unsigned long next_update
;
1009 int imbalance
; /* XXX unrelated to capacity but shared group state */
1011 unsigned long cpumask
[0]; /* iteration mask */
1014 struct sched_group
{
1015 struct sched_group
*next
; /* Must be a circular list */
1018 unsigned int group_weight
;
1019 struct sched_group_capacity
*sgc
;
1020 int asym_prefer_cpu
; /* cpu of highest priority in group */
1023 * The CPUs this group covers.
1025 * NOTE: this field is variable length. (Allocated dynamically
1026 * by attaching extra space to the end of the structure,
1027 * depending on how many CPUs the kernel has booted up with)
1029 unsigned long cpumask
[0];
1032 static inline struct cpumask
*sched_group_cpus(struct sched_group
*sg
)
1034 return to_cpumask(sg
->cpumask
);
1038 * cpumask masking which cpus in the group are allowed to iterate up the domain
1041 static inline struct cpumask
*sched_group_mask(struct sched_group
*sg
)
1043 return to_cpumask(sg
->sgc
->cpumask
);
1047 * group_first_cpu - Returns the first cpu in the cpumask of a sched_group.
1048 * @group: The group whose first cpu is to be returned.
1050 static inline unsigned int group_first_cpu(struct sched_group
*group
)
1052 return cpumask_first(sched_group_cpus(group
));
1055 extern int group_balance_cpu(struct sched_group
*sg
);
1057 #if defined(CONFIG_SCHED_DEBUG) && defined(CONFIG_SYSCTL)
1058 void register_sched_domain_sysctl(void);
1059 void unregister_sched_domain_sysctl(void);
1061 static inline void register_sched_domain_sysctl(void)
1064 static inline void unregister_sched_domain_sysctl(void)
1071 static inline void sched_ttwu_pending(void) { }
1073 #endif /* CONFIG_SMP */
1076 #include "autogroup.h"
1078 #ifdef CONFIG_CGROUP_SCHED
1081 * Return the group to which this tasks belongs.
1083 * We cannot use task_css() and friends because the cgroup subsystem
1084 * changes that value before the cgroup_subsys::attach() method is called,
1085 * therefore we cannot pin it and might observe the wrong value.
1087 * The same is true for autogroup's p->signal->autogroup->tg, the autogroup
1088 * core changes this before calling sched_move_task().
1090 * Instead we use a 'copy' which is updated from sched_move_task() while
1091 * holding both task_struct::pi_lock and rq::lock.
1093 static inline struct task_group
*task_group(struct task_struct
*p
)
1095 return p
->sched_task_group
;
1098 /* Change a task's cfs_rq and parent entity if it moves across CPUs/groups */
1099 static inline void set_task_rq(struct task_struct
*p
, unsigned int cpu
)
1101 #if defined(CONFIG_FAIR_GROUP_SCHED) || defined(CONFIG_RT_GROUP_SCHED)
1102 struct task_group
*tg
= task_group(p
);
1105 #ifdef CONFIG_FAIR_GROUP_SCHED
1106 set_task_rq_fair(&p
->se
, p
->se
.cfs_rq
, tg
->cfs_rq
[cpu
]);
1107 p
->se
.cfs_rq
= tg
->cfs_rq
[cpu
];
1108 p
->se
.parent
= tg
->se
[cpu
];
1111 #ifdef CONFIG_RT_GROUP_SCHED
1112 p
->rt
.rt_rq
= tg
->rt_rq
[cpu
];
1113 p
->rt
.parent
= tg
->rt_se
[cpu
];
1117 #else /* CONFIG_CGROUP_SCHED */
1119 static inline void set_task_rq(struct task_struct
*p
, unsigned int cpu
) { }
1120 static inline struct task_group
*task_group(struct task_struct
*p
)
1125 #endif /* CONFIG_CGROUP_SCHED */
1127 static inline void __set_task_cpu(struct task_struct
*p
, unsigned int cpu
)
1129 set_task_rq(p
, cpu
);
1132 * After ->cpu is set up to a new value, task_rq_lock(p, ...) can be
1133 * successfuly executed on another CPU. We must ensure that updates of
1134 * per-task data have been completed by this moment.
1137 #ifdef CONFIG_THREAD_INFO_IN_TASK
1140 task_thread_info(p
)->cpu
= cpu
;
1147 * Tunables that become constants when CONFIG_SCHED_DEBUG is off:
1149 #ifdef CONFIG_SCHED_DEBUG
1150 # include <linux/static_key.h>
1151 # define const_debug __read_mostly
1153 # define const_debug const
1156 extern const_debug
unsigned int sysctl_sched_features
;
1158 #define SCHED_FEAT(name, enabled) \
1159 __SCHED_FEAT_##name ,
1162 #include "features.h"
1168 #if defined(CONFIG_SCHED_DEBUG) && defined(HAVE_JUMP_LABEL)
1169 #define SCHED_FEAT(name, enabled) \
1170 static __always_inline bool static_branch_##name(struct static_key *key) \
1172 return static_key_##enabled(key); \
1175 #include "features.h"
1179 extern struct static_key sched_feat_keys
[__SCHED_FEAT_NR
];
1180 #define sched_feat(x) (static_branch_##x(&sched_feat_keys[__SCHED_FEAT_##x]))
1181 #else /* !(SCHED_DEBUG && HAVE_JUMP_LABEL) */
1182 #define sched_feat(x) (sysctl_sched_features & (1UL << __SCHED_FEAT_##x))
1183 #endif /* SCHED_DEBUG && HAVE_JUMP_LABEL */
1185 extern struct static_key_false sched_numa_balancing
;
1186 extern struct static_key_false sched_schedstats
;
1188 static inline u64
global_rt_period(void)
1190 return (u64
)sysctl_sched_rt_period
* NSEC_PER_USEC
;
1193 static inline u64
global_rt_runtime(void)
1195 if (sysctl_sched_rt_runtime
< 0)
1198 return (u64
)sysctl_sched_rt_runtime
* NSEC_PER_USEC
;
1201 static inline int task_current(struct rq
*rq
, struct task_struct
*p
)
1203 return rq
->curr
== p
;
1206 static inline int task_running(struct rq
*rq
, struct task_struct
*p
)
1211 return task_current(rq
, p
);
1215 static inline int task_on_rq_queued(struct task_struct
*p
)
1217 return p
->on_rq
== TASK_ON_RQ_QUEUED
;
1220 static inline int task_on_rq_migrating(struct task_struct
*p
)
1222 return p
->on_rq
== TASK_ON_RQ_MIGRATING
;
1225 #ifndef prepare_arch_switch
1226 # define prepare_arch_switch(next) do { } while (0)
1228 #ifndef finish_arch_post_lock_switch
1229 # define finish_arch_post_lock_switch() do { } while (0)
1232 static inline void prepare_lock_switch(struct rq
*rq
, struct task_struct
*next
)
1236 * We can optimise this out completely for !SMP, because the
1237 * SMP rebalancing from interrupt is the only thing that cares
1244 static inline void finish_lock_switch(struct rq
*rq
, struct task_struct
*prev
)
1248 * After ->on_cpu is cleared, the task can be moved to a different CPU.
1249 * We must ensure this doesn't happen until the switch is completely
1252 * In particular, the load of prev->state in finish_task_switch() must
1253 * happen before this.
1255 * Pairs with the smp_cond_load_acquire() in try_to_wake_up().
1257 smp_store_release(&prev
->on_cpu
, 0);
1259 #ifdef CONFIG_DEBUG_SPINLOCK
1260 /* this is a valid case when another task releases the spinlock */
1261 rq
->lock
.owner
= current
;
1264 * If we are tracking spinlock dependencies then we have to
1265 * fix up the runqueue lock - which gets 'carried over' from
1266 * prev into current:
1268 spin_acquire(&rq
->lock
.dep_map
, 0, 0, _THIS_IP_
);
1270 raw_spin_unlock_irq(&rq
->lock
);
1276 #define WF_SYNC 0x01 /* waker goes to sleep after wakeup */
1277 #define WF_FORK 0x02 /* child wakeup after fork */
1278 #define WF_MIGRATED 0x4 /* internal use, task got migrated */
1281 * To aid in avoiding the subversion of "niceness" due to uneven distribution
1282 * of tasks with abnormal "nice" values across CPUs the contribution that
1283 * each task makes to its run queue's load is weighted according to its
1284 * scheduling class and "nice" value. For SCHED_NORMAL tasks this is just a
1285 * scaled version of the new time slice allocation that they receive on time
1289 #define WEIGHT_IDLEPRIO 3
1290 #define WMULT_IDLEPRIO 1431655765
1292 extern const int sched_prio_to_weight
[40];
1293 extern const u32 sched_prio_to_wmult
[40];
1296 * {de,en}queue flags:
1298 * DEQUEUE_SLEEP - task is no longer runnable
1299 * ENQUEUE_WAKEUP - task just became runnable
1301 * SAVE/RESTORE - an otherwise spurious dequeue/enqueue, done to ensure tasks
1302 * are in a known state which allows modification. Such pairs
1303 * should preserve as much state as possible.
1305 * MOVE - paired with SAVE/RESTORE, explicitly does not preserve the location
1308 * ENQUEUE_HEAD - place at front of runqueue (tail if not specified)
1309 * ENQUEUE_REPLENISH - CBS (replenish runtime and postpone deadline)
1310 * ENQUEUE_MIGRATED - the task was migrated during wakeup
1314 #define DEQUEUE_SLEEP 0x01
1315 #define DEQUEUE_SAVE 0x02 /* matches ENQUEUE_RESTORE */
1316 #define DEQUEUE_MOVE 0x04 /* matches ENQUEUE_MOVE */
1318 #define ENQUEUE_WAKEUP 0x01
1319 #define ENQUEUE_RESTORE 0x02
1320 #define ENQUEUE_MOVE 0x04
1322 #define ENQUEUE_HEAD 0x08
1323 #define ENQUEUE_REPLENISH 0x10
1325 #define ENQUEUE_MIGRATED 0x20
1327 #define ENQUEUE_MIGRATED 0x00
1330 #define RETRY_TASK ((void *)-1UL)
1332 struct sched_class
{
1333 const struct sched_class
*next
;
1335 void (*enqueue_task
) (struct rq
*rq
, struct task_struct
*p
, int flags
);
1336 void (*dequeue_task
) (struct rq
*rq
, struct task_struct
*p
, int flags
);
1337 void (*yield_task
) (struct rq
*rq
);
1338 bool (*yield_to_task
) (struct rq
*rq
, struct task_struct
*p
, bool preempt
);
1340 void (*check_preempt_curr
) (struct rq
*rq
, struct task_struct
*p
, int flags
);
1343 * It is the responsibility of the pick_next_task() method that will
1344 * return the next task to call put_prev_task() on the @prev task or
1345 * something equivalent.
1347 * May return RETRY_TASK when it finds a higher prio class has runnable
1350 struct task_struct
* (*pick_next_task
) (struct rq
*rq
,
1351 struct task_struct
*prev
,
1352 struct rq_flags
*rf
);
1353 void (*put_prev_task
) (struct rq
*rq
, struct task_struct
*p
);
1356 int (*select_task_rq
)(struct task_struct
*p
, int task_cpu
, int sd_flag
, int flags
);
1357 void (*migrate_task_rq
)(struct task_struct
*p
);
1359 void (*task_woken
) (struct rq
*this_rq
, struct task_struct
*task
);
1361 void (*set_cpus_allowed
)(struct task_struct
*p
,
1362 const struct cpumask
*newmask
);
1364 void (*rq_online
)(struct rq
*rq
);
1365 void (*rq_offline
)(struct rq
*rq
);
1368 void (*set_curr_task
) (struct rq
*rq
);
1369 void (*task_tick
) (struct rq
*rq
, struct task_struct
*p
, int queued
);
1370 void (*task_fork
) (struct task_struct
*p
);
1371 void (*task_dead
) (struct task_struct
*p
);
1374 * The switched_from() call is allowed to drop rq->lock, therefore we
1375 * cannot assume the switched_from/switched_to pair is serliazed by
1376 * rq->lock. They are however serialized by p->pi_lock.
1378 void (*switched_from
) (struct rq
*this_rq
, struct task_struct
*task
);
1379 void (*switched_to
) (struct rq
*this_rq
, struct task_struct
*task
);
1380 void (*prio_changed
) (struct rq
*this_rq
, struct task_struct
*task
,
1383 unsigned int (*get_rr_interval
) (struct rq
*rq
,
1384 struct task_struct
*task
);
1386 void (*update_curr
) (struct rq
*rq
);
1388 #define TASK_SET_GROUP 0
1389 #define TASK_MOVE_GROUP 1
1391 #ifdef CONFIG_FAIR_GROUP_SCHED
1392 void (*task_change_group
) (struct task_struct
*p
, int type
);
1396 static inline void put_prev_task(struct rq
*rq
, struct task_struct
*prev
)
1398 prev
->sched_class
->put_prev_task(rq
, prev
);
1401 static inline void set_curr_task(struct rq
*rq
, struct task_struct
*curr
)
1403 curr
->sched_class
->set_curr_task(rq
);
1406 #define sched_class_highest (&stop_sched_class)
1407 #define for_each_class(class) \
1408 for (class = sched_class_highest; class; class = class->next)
1410 extern const struct sched_class stop_sched_class
;
1411 extern const struct sched_class dl_sched_class
;
1412 extern const struct sched_class rt_sched_class
;
1413 extern const struct sched_class fair_sched_class
;
1414 extern const struct sched_class idle_sched_class
;
1419 extern void update_group_capacity(struct sched_domain
*sd
, int cpu
);
1421 extern void trigger_load_balance(struct rq
*rq
);
1423 extern void set_cpus_allowed_common(struct task_struct
*p
, const struct cpumask
*new_mask
);
1427 #ifdef CONFIG_CPU_IDLE
1428 static inline void idle_set_state(struct rq
*rq
,
1429 struct cpuidle_state
*idle_state
)
1431 rq
->idle_state
= idle_state
;
1434 static inline struct cpuidle_state
*idle_get_state(struct rq
*rq
)
1436 SCHED_WARN_ON(!rcu_read_lock_held());
1437 return rq
->idle_state
;
1440 static inline void idle_set_state(struct rq
*rq
,
1441 struct cpuidle_state
*idle_state
)
1445 static inline struct cpuidle_state
*idle_get_state(struct rq
*rq
)
1451 extern void sysrq_sched_debug_show(void);
1452 extern void sched_init_granularity(void);
1453 extern void update_max_interval(void);
1455 extern void init_sched_dl_class(void);
1456 extern void init_sched_rt_class(void);
1457 extern void init_sched_fair_class(void);
1459 extern void resched_curr(struct rq
*rq
);
1460 extern void resched_cpu(int cpu
);
1462 extern struct rt_bandwidth def_rt_bandwidth
;
1463 extern void init_rt_bandwidth(struct rt_bandwidth
*rt_b
, u64 period
, u64 runtime
);
1465 extern struct dl_bandwidth def_dl_bandwidth
;
1466 extern void init_dl_bandwidth(struct dl_bandwidth
*dl_b
, u64 period
, u64 runtime
);
1467 extern void init_dl_task_timer(struct sched_dl_entity
*dl_se
);
1469 unsigned long to_ratio(u64 period
, u64 runtime
);
1471 extern void init_entity_runnable_average(struct sched_entity
*se
);
1472 extern void post_init_entity_util_avg(struct sched_entity
*se
);
1474 #ifdef CONFIG_NO_HZ_FULL
1475 extern bool sched_can_stop_tick(struct rq
*rq
);
1478 * Tick may be needed by tasks in the runqueue depending on their policy and
1479 * requirements. If tick is needed, lets send the target an IPI to kick it out of
1480 * nohz mode if necessary.
1482 static inline void sched_update_tick_dependency(struct rq
*rq
)
1486 if (!tick_nohz_full_enabled())
1491 if (!tick_nohz_full_cpu(cpu
))
1494 if (sched_can_stop_tick(rq
))
1495 tick_nohz_dep_clear_cpu(cpu
, TICK_DEP_BIT_SCHED
);
1497 tick_nohz_dep_set_cpu(cpu
, TICK_DEP_BIT_SCHED
);
1500 static inline void sched_update_tick_dependency(struct rq
*rq
) { }
1503 static inline void add_nr_running(struct rq
*rq
, unsigned count
)
1505 unsigned prev_nr
= rq
->nr_running
;
1507 rq
->nr_running
= prev_nr
+ count
;
1509 if (prev_nr
< 2 && rq
->nr_running
>= 2) {
1511 if (!rq
->rd
->overload
)
1512 rq
->rd
->overload
= true;
1516 sched_update_tick_dependency(rq
);
1519 static inline void sub_nr_running(struct rq
*rq
, unsigned count
)
1521 rq
->nr_running
-= count
;
1522 /* Check if we still need preemption */
1523 sched_update_tick_dependency(rq
);
1526 static inline void rq_last_tick_reset(struct rq
*rq
)
1528 #ifdef CONFIG_NO_HZ_FULL
1529 rq
->last_sched_tick
= jiffies
;
1533 extern void update_rq_clock(struct rq
*rq
);
1535 extern void activate_task(struct rq
*rq
, struct task_struct
*p
, int flags
);
1536 extern void deactivate_task(struct rq
*rq
, struct task_struct
*p
, int flags
);
1538 extern void check_preempt_curr(struct rq
*rq
, struct task_struct
*p
, int flags
);
1540 extern const_debug
unsigned int sysctl_sched_time_avg
;
1541 extern const_debug
unsigned int sysctl_sched_nr_migrate
;
1542 extern const_debug
unsigned int sysctl_sched_migration_cost
;
1544 static inline u64
sched_avg_period(void)
1546 return (u64
)sysctl_sched_time_avg
* NSEC_PER_MSEC
/ 2;
1549 #ifdef CONFIG_SCHED_HRTICK
1553 * - enabled by features
1554 * - hrtimer is actually high res
1556 static inline int hrtick_enabled(struct rq
*rq
)
1558 if (!sched_feat(HRTICK
))
1560 if (!cpu_active(cpu_of(rq
)))
1562 return hrtimer_is_hres_active(&rq
->hrtick_timer
);
1565 void hrtick_start(struct rq
*rq
, u64 delay
);
1569 static inline int hrtick_enabled(struct rq
*rq
)
1574 #endif /* CONFIG_SCHED_HRTICK */
1577 extern void sched_avg_update(struct rq
*rq
);
1579 #ifndef arch_scale_freq_capacity
1580 static __always_inline
1581 unsigned long arch_scale_freq_capacity(struct sched_domain
*sd
, int cpu
)
1583 return SCHED_CAPACITY_SCALE
;
1587 #ifndef arch_scale_cpu_capacity
1588 static __always_inline
1589 unsigned long arch_scale_cpu_capacity(struct sched_domain
*sd
, int cpu
)
1591 if (sd
&& (sd
->flags
& SD_SHARE_CPUCAPACITY
) && (sd
->span_weight
> 1))
1592 return sd
->smt_gain
/ sd
->span_weight
;
1594 return SCHED_CAPACITY_SCALE
;
1598 static inline void sched_rt_avg_update(struct rq
*rq
, u64 rt_delta
)
1600 rq
->rt_avg
+= rt_delta
* arch_scale_freq_capacity(NULL
, cpu_of(rq
));
1601 sched_avg_update(rq
);
1604 static inline void sched_rt_avg_update(struct rq
*rq
, u64 rt_delta
) { }
1605 static inline void sched_avg_update(struct rq
*rq
) { }
1608 struct rq
*__task_rq_lock(struct task_struct
*p
, struct rq_flags
*rf
)
1609 __acquires(rq
->lock
);
1610 struct rq
*task_rq_lock(struct task_struct
*p
, struct rq_flags
*rf
)
1611 __acquires(p
->pi_lock
)
1612 __acquires(rq
->lock
);
1614 static inline void __task_rq_unlock(struct rq
*rq
, struct rq_flags
*rf
)
1615 __releases(rq
->lock
)
1617 rq_unpin_lock(rq
, rf
);
1618 raw_spin_unlock(&rq
->lock
);
1622 task_rq_unlock(struct rq
*rq
, struct task_struct
*p
, struct rq_flags
*rf
)
1623 __releases(rq
->lock
)
1624 __releases(p
->pi_lock
)
1626 rq_unpin_lock(rq
, rf
);
1627 raw_spin_unlock(&rq
->lock
);
1628 raw_spin_unlock_irqrestore(&p
->pi_lock
, rf
->flags
);
1632 #ifdef CONFIG_PREEMPT
1634 static inline void double_rq_lock(struct rq
*rq1
, struct rq
*rq2
);
1637 * fair double_lock_balance: Safely acquires both rq->locks in a fair
1638 * way at the expense of forcing extra atomic operations in all
1639 * invocations. This assures that the double_lock is acquired using the
1640 * same underlying policy as the spinlock_t on this architecture, which
1641 * reduces latency compared to the unfair variant below. However, it
1642 * also adds more overhead and therefore may reduce throughput.
1644 static inline int _double_lock_balance(struct rq
*this_rq
, struct rq
*busiest
)
1645 __releases(this_rq
->lock
)
1646 __acquires(busiest
->lock
)
1647 __acquires(this_rq
->lock
)
1649 raw_spin_unlock(&this_rq
->lock
);
1650 double_rq_lock(this_rq
, busiest
);
1657 * Unfair double_lock_balance: Optimizes throughput at the expense of
1658 * latency by eliminating extra atomic operations when the locks are
1659 * already in proper order on entry. This favors lower cpu-ids and will
1660 * grant the double lock to lower cpus over higher ids under contention,
1661 * regardless of entry order into the function.
1663 static inline int _double_lock_balance(struct rq
*this_rq
, struct rq
*busiest
)
1664 __releases(this_rq
->lock
)
1665 __acquires(busiest
->lock
)
1666 __acquires(this_rq
->lock
)
1670 if (unlikely(!raw_spin_trylock(&busiest
->lock
))) {
1671 if (busiest
< this_rq
) {
1672 raw_spin_unlock(&this_rq
->lock
);
1673 raw_spin_lock(&busiest
->lock
);
1674 raw_spin_lock_nested(&this_rq
->lock
,
1675 SINGLE_DEPTH_NESTING
);
1678 raw_spin_lock_nested(&busiest
->lock
,
1679 SINGLE_DEPTH_NESTING
);
1684 #endif /* CONFIG_PREEMPT */
1687 * double_lock_balance - lock the busiest runqueue, this_rq is locked already.
1689 static inline int double_lock_balance(struct rq
*this_rq
, struct rq
*busiest
)
1691 if (unlikely(!irqs_disabled())) {
1692 /* printk() doesn't work good under rq->lock */
1693 raw_spin_unlock(&this_rq
->lock
);
1697 return _double_lock_balance(this_rq
, busiest
);
1700 static inline void double_unlock_balance(struct rq
*this_rq
, struct rq
*busiest
)
1701 __releases(busiest
->lock
)
1703 raw_spin_unlock(&busiest
->lock
);
1704 lock_set_subclass(&this_rq
->lock
.dep_map
, 0, _RET_IP_
);
1707 static inline void double_lock(spinlock_t
*l1
, spinlock_t
*l2
)
1713 spin_lock_nested(l2
, SINGLE_DEPTH_NESTING
);
1716 static inline void double_lock_irq(spinlock_t
*l1
, spinlock_t
*l2
)
1722 spin_lock_nested(l2
, SINGLE_DEPTH_NESTING
);
1725 static inline void double_raw_lock(raw_spinlock_t
*l1
, raw_spinlock_t
*l2
)
1731 raw_spin_lock_nested(l2
, SINGLE_DEPTH_NESTING
);
1735 * double_rq_lock - safely lock two runqueues
1737 * Note this does not disable interrupts like task_rq_lock,
1738 * you need to do so manually before calling.
1740 static inline void double_rq_lock(struct rq
*rq1
, struct rq
*rq2
)
1741 __acquires(rq1
->lock
)
1742 __acquires(rq2
->lock
)
1744 BUG_ON(!irqs_disabled());
1746 raw_spin_lock(&rq1
->lock
);
1747 __acquire(rq2
->lock
); /* Fake it out ;) */
1750 raw_spin_lock(&rq1
->lock
);
1751 raw_spin_lock_nested(&rq2
->lock
, SINGLE_DEPTH_NESTING
);
1753 raw_spin_lock(&rq2
->lock
);
1754 raw_spin_lock_nested(&rq1
->lock
, SINGLE_DEPTH_NESTING
);
1760 * double_rq_unlock - safely unlock two runqueues
1762 * Note this does not restore interrupts like task_rq_unlock,
1763 * you need to do so manually after calling.
1765 static inline void double_rq_unlock(struct rq
*rq1
, struct rq
*rq2
)
1766 __releases(rq1
->lock
)
1767 __releases(rq2
->lock
)
1769 raw_spin_unlock(&rq1
->lock
);
1771 raw_spin_unlock(&rq2
->lock
);
1773 __release(rq2
->lock
);
1776 extern void set_rq_online (struct rq
*rq
);
1777 extern void set_rq_offline(struct rq
*rq
);
1778 extern bool sched_smp_initialized
;
1780 #else /* CONFIG_SMP */
1783 * double_rq_lock - safely lock two runqueues
1785 * Note this does not disable interrupts like task_rq_lock,
1786 * you need to do so manually before calling.
1788 static inline void double_rq_lock(struct rq
*rq1
, struct rq
*rq2
)
1789 __acquires(rq1
->lock
)
1790 __acquires(rq2
->lock
)
1792 BUG_ON(!irqs_disabled());
1794 raw_spin_lock(&rq1
->lock
);
1795 __acquire(rq2
->lock
); /* Fake it out ;) */
1799 * double_rq_unlock - safely unlock two runqueues
1801 * Note this does not restore interrupts like task_rq_unlock,
1802 * you need to do so manually after calling.
1804 static inline void double_rq_unlock(struct rq
*rq1
, struct rq
*rq2
)
1805 __releases(rq1
->lock
)
1806 __releases(rq2
->lock
)
1809 raw_spin_unlock(&rq1
->lock
);
1810 __release(rq2
->lock
);
1815 extern struct sched_entity
*__pick_first_entity(struct cfs_rq
*cfs_rq
);
1816 extern struct sched_entity
*__pick_last_entity(struct cfs_rq
*cfs_rq
);
1818 #ifdef CONFIG_SCHED_DEBUG
1819 extern void print_cfs_stats(struct seq_file
*m
, int cpu
);
1820 extern void print_rt_stats(struct seq_file
*m
, int cpu
);
1821 extern void print_dl_stats(struct seq_file
*m
, int cpu
);
1823 print_cfs_rq(struct seq_file
*m
, int cpu
, struct cfs_rq
*cfs_rq
);
1825 #ifdef CONFIG_NUMA_BALANCING
1827 show_numa_stats(struct task_struct
*p
, struct seq_file
*m
);
1829 print_numa_stats(struct seq_file
*m
, int node
, unsigned long tsf
,
1830 unsigned long tpf
, unsigned long gsf
, unsigned long gpf
);
1831 #endif /* CONFIG_NUMA_BALANCING */
1832 #endif /* CONFIG_SCHED_DEBUG */
1834 extern void init_cfs_rq(struct cfs_rq
*cfs_rq
);
1835 extern void init_rt_rq(struct rt_rq
*rt_rq
);
1836 extern void init_dl_rq(struct dl_rq
*dl_rq
);
1838 extern void cfs_bandwidth_usage_inc(void);
1839 extern void cfs_bandwidth_usage_dec(void);
1841 #ifdef CONFIG_NO_HZ_COMMON
1842 enum rq_nohz_flag_bits
{
1847 #define nohz_flags(cpu) (&cpu_rq(cpu)->nohz_flags)
1849 extern void nohz_balance_exit_idle(unsigned int cpu
);
1851 static inline void nohz_balance_exit_idle(unsigned int cpu
) { }
1854 #ifdef CONFIG_IRQ_TIME_ACCOUNTING
1858 struct u64_stats_sync sync
;
1861 DECLARE_PER_CPU(struct irqtime
, cpu_irqtime
);
1863 static inline u64
irq_time_read(int cpu
)
1865 struct irqtime
*irqtime
= &per_cpu(cpu_irqtime
, cpu
);
1866 u64
*cpustat
= kcpustat_cpu(cpu
).cpustat
;
1871 seq
= __u64_stats_fetch_begin(&irqtime
->sync
);
1872 total
= cpustat
[CPUTIME_SOFTIRQ
] + cpustat
[CPUTIME_IRQ
];
1873 } while (__u64_stats_fetch_retry(&irqtime
->sync
, seq
));
1877 #endif /* CONFIG_IRQ_TIME_ACCOUNTING */
1879 #ifdef CONFIG_CPU_FREQ
1880 DECLARE_PER_CPU(struct update_util_data
*, cpufreq_update_util_data
);
1883 * cpufreq_update_util - Take a note about CPU utilization changes.
1884 * @rq: Runqueue to carry out the update for.
1885 * @flags: Update reason flags.
1887 * This function is called by the scheduler on the CPU whose utilization is
1890 * It can only be called from RCU-sched read-side critical sections.
1892 * The way cpufreq is currently arranged requires it to evaluate the CPU
1893 * performance state (frequency/voltage) on a regular basis to prevent it from
1894 * being stuck in a completely inadequate performance level for too long.
1895 * That is not guaranteed to happen if the updates are only triggered from CFS,
1896 * though, because they may not be coming in if RT or deadline tasks are active
1897 * all the time (or there are RT and DL tasks only).
1899 * As a workaround for that issue, this function is called by the RT and DL
1900 * sched classes to trigger extra cpufreq updates to prevent it from stalling,
1901 * but that really is a band-aid. Going forward it should be replaced with
1902 * solutions targeted more specifically at RT and DL tasks.
1904 static inline void cpufreq_update_util(struct rq
*rq
, unsigned int flags
)
1906 struct update_util_data
*data
;
1908 data
= rcu_dereference_sched(*this_cpu_ptr(&cpufreq_update_util_data
));
1910 data
->func(data
, rq_clock(rq
), flags
);
1913 static inline void cpufreq_update_this_cpu(struct rq
*rq
, unsigned int flags
)
1915 if (cpu_of(rq
) == smp_processor_id())
1916 cpufreq_update_util(rq
, flags
);
1919 static inline void cpufreq_update_util(struct rq
*rq
, unsigned int flags
) {}
1920 static inline void cpufreq_update_this_cpu(struct rq
*rq
, unsigned int flags
) {}
1921 #endif /* CONFIG_CPU_FREQ */
1923 #ifdef arch_scale_freq_capacity
1924 #ifndef arch_scale_freq_invariant
1925 #define arch_scale_freq_invariant() (true)
1927 #else /* arch_scale_freq_capacity */
1928 #define arch_scale_freq_invariant() (false)