Linux 4.9.243
[linux/fpc-iii.git] / kernel / sched / sched.h
blob819bd5fb02647af58d4f184fa495f7b8a901300d
2 #include <linux/sched.h>
3 #include <linux/sched/sysctl.h>
4 #include <linux/sched/rt.h>
5 #include <linux/sched/smt.h>
6 #include <linux/u64_stats_sync.h>
7 #include <linux/sched/deadline.h>
8 #include <linux/kernel_stat.h>
9 #include <linux/binfmts.h>
10 #include <linux/mutex.h>
11 #include <linux/spinlock.h>
12 #include <linux/stop_machine.h>
13 #include <linux/irq_work.h>
14 #include <linux/tick.h>
15 #include <linux/slab.h>
17 #include "cpupri.h"
18 #include "cpudeadline.h"
19 #include "cpuacct.h"
21 #ifdef CONFIG_SCHED_DEBUG
22 #define SCHED_WARN_ON(x) WARN_ONCE(x, #x)
23 #else
24 #define SCHED_WARN_ON(x) ((void)(x))
25 #endif
27 struct rq;
28 struct cpuidle_state;
30 /* task_struct::on_rq states: */
31 #define TASK_ON_RQ_QUEUED 1
32 #define TASK_ON_RQ_MIGRATING 2
34 extern __read_mostly int scheduler_running;
36 extern unsigned long calc_load_update;
37 extern atomic_long_t calc_load_tasks;
39 extern void calc_global_load_tick(struct rq *this_rq);
40 extern long calc_load_fold_active(struct rq *this_rq, long adjust);
42 #ifdef CONFIG_SMP
43 extern void cpu_load_update_active(struct rq *this_rq);
44 #else
45 static inline void cpu_load_update_active(struct rq *this_rq) { }
46 #endif
48 #ifdef CONFIG_SCHED_SMT
49 extern void update_idle_core(struct rq *rq);
50 #else
51 static inline void update_idle_core(struct rq *rq) { }
52 #endif
55 * Helpers for converting nanosecond timing to jiffy resolution
57 #define NS_TO_JIFFIES(TIME) ((unsigned long)(TIME) / (NSEC_PER_SEC / HZ))
60 * Increase resolution of nice-level calculations for 64-bit architectures.
61 * The extra resolution improves shares distribution and load balancing of
62 * low-weight task groups (eg. nice +19 on an autogroup), deeper taskgroup
63 * hierarchies, especially on larger systems. This is not a user-visible change
64 * and does not change the user-interface for setting shares/weights.
66 * We increase resolution only if we have enough bits to allow this increased
67 * resolution (i.e. 64bit). The costs for increasing resolution when 32bit are
68 * pretty high and the returns do not justify the increased costs.
70 * Really only required when CONFIG_FAIR_GROUP_SCHED is also set, but to
71 * increase coverage and consistency always enable it on 64bit platforms.
73 #ifdef CONFIG_64BIT
74 # define NICE_0_LOAD_SHIFT (SCHED_FIXEDPOINT_SHIFT + SCHED_FIXEDPOINT_SHIFT)
75 # define scale_load(w) ((w) << SCHED_FIXEDPOINT_SHIFT)
76 # define scale_load_down(w) \
77 ({ \
78 unsigned long __w = (w); \
79 if (__w) \
80 __w = max(2UL, __w >> SCHED_FIXEDPOINT_SHIFT); \
81 __w; \
83 #else
84 # define NICE_0_LOAD_SHIFT (SCHED_FIXEDPOINT_SHIFT)
85 # define scale_load(w) (w)
86 # define scale_load_down(w) (w)
87 #endif
90 * Task weight (visible to users) and its load (invisible to users) have
91 * independent resolution, but they should be well calibrated. We use
92 * scale_load() and scale_load_down(w) to convert between them. The
93 * following must be true:
95 * scale_load(sched_prio_to_weight[USER_PRIO(NICE_TO_PRIO(0))]) == NICE_0_LOAD
98 #define NICE_0_LOAD (1L << NICE_0_LOAD_SHIFT)
101 * Single value that decides SCHED_DEADLINE internal math precision.
102 * 10 -> just above 1us
103 * 9 -> just above 0.5us
105 #define DL_SCALE (10)
108 * These are the 'tuning knobs' of the scheduler:
112 * single value that denotes runtime == period, ie unlimited time.
114 #define RUNTIME_INF ((u64)~0ULL)
116 static inline int idle_policy(int policy)
118 return policy == SCHED_IDLE;
120 static inline int fair_policy(int policy)
122 return policy == SCHED_NORMAL || policy == SCHED_BATCH;
125 static inline int rt_policy(int policy)
127 return policy == SCHED_FIFO || policy == SCHED_RR;
130 static inline int dl_policy(int policy)
132 return policy == SCHED_DEADLINE;
134 static inline bool valid_policy(int policy)
136 return idle_policy(policy) || fair_policy(policy) ||
137 rt_policy(policy) || dl_policy(policy);
140 static inline int task_has_rt_policy(struct task_struct *p)
142 return rt_policy(p->policy);
145 static inline int task_has_dl_policy(struct task_struct *p)
147 return dl_policy(p->policy);
151 * Tells if entity @a should preempt entity @b.
153 static inline bool
154 dl_entity_preempt(struct sched_dl_entity *a, struct sched_dl_entity *b)
156 return dl_time_before(a->deadline, b->deadline);
160 * This is the priority-queue data structure of the RT scheduling class:
162 struct rt_prio_array {
163 DECLARE_BITMAP(bitmap, MAX_RT_PRIO+1); /* include 1 bit for delimiter */
164 struct list_head queue[MAX_RT_PRIO];
167 struct rt_bandwidth {
168 /* nests inside the rq lock: */
169 raw_spinlock_t rt_runtime_lock;
170 ktime_t rt_period;
171 u64 rt_runtime;
172 struct hrtimer rt_period_timer;
173 unsigned int rt_period_active;
176 void __dl_clear_params(struct task_struct *p);
179 * To keep the bandwidth of -deadline tasks and groups under control
180 * we need some place where:
181 * - store the maximum -deadline bandwidth of the system (the group);
182 * - cache the fraction of that bandwidth that is currently allocated.
184 * This is all done in the data structure below. It is similar to the
185 * one used for RT-throttling (rt_bandwidth), with the main difference
186 * that, since here we are only interested in admission control, we
187 * do not decrease any runtime while the group "executes", neither we
188 * need a timer to replenish it.
190 * With respect to SMP, the bandwidth is given on a per-CPU basis,
191 * meaning that:
192 * - dl_bw (< 100%) is the bandwidth of the system (group) on each CPU;
193 * - dl_total_bw array contains, in the i-eth element, the currently
194 * allocated bandwidth on the i-eth CPU.
195 * Moreover, groups consume bandwidth on each CPU, while tasks only
196 * consume bandwidth on the CPU they're running on.
197 * Finally, dl_total_bw_cpu is used to cache the index of dl_total_bw
198 * that will be shown the next time the proc or cgroup controls will
199 * be red. It on its turn can be changed by writing on its own
200 * control.
202 struct dl_bandwidth {
203 raw_spinlock_t dl_runtime_lock;
204 u64 dl_runtime;
205 u64 dl_period;
208 static inline int dl_bandwidth_enabled(void)
210 return sysctl_sched_rt_runtime >= 0;
213 extern struct dl_bw *dl_bw_of(int i);
215 struct dl_bw {
216 raw_spinlock_t lock;
217 u64 bw, total_bw;
220 static inline
221 void __dl_clear(struct dl_bw *dl_b, u64 tsk_bw)
223 dl_b->total_bw -= tsk_bw;
226 static inline
227 void __dl_add(struct dl_bw *dl_b, u64 tsk_bw)
229 dl_b->total_bw += tsk_bw;
232 static inline
233 bool __dl_overflow(struct dl_bw *dl_b, int cpus, u64 old_bw, u64 new_bw)
235 return dl_b->bw != -1 &&
236 dl_b->bw * cpus < dl_b->total_bw - old_bw + new_bw;
239 extern struct mutex sched_domains_mutex;
241 #ifdef CONFIG_CGROUP_SCHED
243 #include <linux/cgroup.h>
245 struct cfs_rq;
246 struct rt_rq;
248 extern struct list_head task_groups;
250 struct cfs_bandwidth {
251 #ifdef CONFIG_CFS_BANDWIDTH
252 raw_spinlock_t lock;
253 ktime_t period;
254 u64 quota, runtime;
255 s64 hierarchical_quota;
256 u64 runtime_expires;
258 int idle, period_active;
259 struct hrtimer period_timer, slack_timer;
260 struct list_head throttled_cfs_rq;
262 /* statistics */
263 int nr_periods, nr_throttled;
264 u64 throttled_time;
266 bool distribute_running;
267 #endif
270 /* task group related information */
271 struct task_group {
272 struct cgroup_subsys_state css;
274 #ifdef CONFIG_FAIR_GROUP_SCHED
275 /* schedulable entities of this group on each cpu */
276 struct sched_entity **se;
277 /* runqueue "owned" by this group on each cpu */
278 struct cfs_rq **cfs_rq;
279 unsigned long shares;
281 #ifdef CONFIG_SMP
283 * load_avg can be heavily contended at clock tick time, so put
284 * it in its own cacheline separated from the fields above which
285 * will also be accessed at each tick.
287 atomic_long_t load_avg ____cacheline_aligned;
288 #endif
289 #endif
291 #ifdef CONFIG_RT_GROUP_SCHED
292 struct sched_rt_entity **rt_se;
293 struct rt_rq **rt_rq;
295 struct rt_bandwidth rt_bandwidth;
296 #endif
298 struct rcu_head rcu;
299 struct list_head list;
301 struct task_group *parent;
302 struct list_head siblings;
303 struct list_head children;
305 #ifdef CONFIG_SCHED_AUTOGROUP
306 struct autogroup *autogroup;
307 #endif
309 struct cfs_bandwidth cfs_bandwidth;
312 #ifdef CONFIG_FAIR_GROUP_SCHED
313 #define ROOT_TASK_GROUP_LOAD NICE_0_LOAD
316 * A weight of 0 or 1 can cause arithmetics problems.
317 * A weight of a cfs_rq is the sum of weights of which entities
318 * are queued on this cfs_rq, so a weight of a entity should not be
319 * too large, so as the shares value of a task group.
320 * (The default weight is 1024 - so there's no practical
321 * limitation from this.)
323 #define MIN_SHARES (1UL << 1)
324 #define MAX_SHARES (1UL << 18)
325 #endif
327 typedef int (*tg_visitor)(struct task_group *, void *);
329 extern int walk_tg_tree_from(struct task_group *from,
330 tg_visitor down, tg_visitor up, void *data);
333 * Iterate the full tree, calling @down when first entering a node and @up when
334 * leaving it for the final time.
336 * Caller must hold rcu_lock or sufficient equivalent.
338 static inline int walk_tg_tree(tg_visitor down, tg_visitor up, void *data)
340 return walk_tg_tree_from(&root_task_group, down, up, data);
343 extern int tg_nop(struct task_group *tg, void *data);
345 extern void free_fair_sched_group(struct task_group *tg);
346 extern int alloc_fair_sched_group(struct task_group *tg, struct task_group *parent);
347 extern void online_fair_sched_group(struct task_group *tg);
348 extern void unregister_fair_sched_group(struct task_group *tg);
349 extern void init_tg_cfs_entry(struct task_group *tg, struct cfs_rq *cfs_rq,
350 struct sched_entity *se, int cpu,
351 struct sched_entity *parent);
352 extern void init_cfs_bandwidth(struct cfs_bandwidth *cfs_b);
354 extern void __refill_cfs_bandwidth_runtime(struct cfs_bandwidth *cfs_b);
355 extern void start_cfs_bandwidth(struct cfs_bandwidth *cfs_b);
356 extern void unthrottle_cfs_rq(struct cfs_rq *cfs_rq);
358 extern void free_rt_sched_group(struct task_group *tg);
359 extern int alloc_rt_sched_group(struct task_group *tg, struct task_group *parent);
360 extern void init_tg_rt_entry(struct task_group *tg, struct rt_rq *rt_rq,
361 struct sched_rt_entity *rt_se, int cpu,
362 struct sched_rt_entity *parent);
364 extern struct task_group *sched_create_group(struct task_group *parent);
365 extern void sched_online_group(struct task_group *tg,
366 struct task_group *parent);
367 extern void sched_destroy_group(struct task_group *tg);
368 extern void sched_offline_group(struct task_group *tg);
370 extern void sched_move_task(struct task_struct *tsk);
372 #ifdef CONFIG_FAIR_GROUP_SCHED
373 extern int sched_group_set_shares(struct task_group *tg, unsigned long shares);
375 #ifdef CONFIG_SMP
376 extern void set_task_rq_fair(struct sched_entity *se,
377 struct cfs_rq *prev, struct cfs_rq *next);
378 #else /* !CONFIG_SMP */
379 static inline void set_task_rq_fair(struct sched_entity *se,
380 struct cfs_rq *prev, struct cfs_rq *next) { }
381 #endif /* CONFIG_SMP */
382 #endif /* CONFIG_FAIR_GROUP_SCHED */
384 #else /* CONFIG_CGROUP_SCHED */
386 struct cfs_bandwidth { };
388 #endif /* CONFIG_CGROUP_SCHED */
390 /* CFS-related fields in a runqueue */
391 struct cfs_rq {
392 struct load_weight load;
393 unsigned int nr_running, h_nr_running;
395 u64 exec_clock;
396 u64 min_vruntime;
397 #ifndef CONFIG_64BIT
398 u64 min_vruntime_copy;
399 #endif
401 struct rb_root tasks_timeline;
402 struct rb_node *rb_leftmost;
405 * 'curr' points to currently running entity on this cfs_rq.
406 * It is set to NULL otherwise (i.e when none are currently running).
408 struct sched_entity *curr, *next, *last, *skip;
410 #ifdef CONFIG_SCHED_DEBUG
411 unsigned int nr_spread_over;
412 #endif
414 #ifdef CONFIG_SMP
416 * CFS load tracking
418 struct sched_avg avg;
419 u64 runnable_load_sum;
420 unsigned long runnable_load_avg;
421 #ifdef CONFIG_FAIR_GROUP_SCHED
422 unsigned long tg_load_avg_contrib;
423 #endif
424 atomic_long_t removed_load_avg, removed_util_avg;
425 #ifndef CONFIG_64BIT
426 u64 load_last_update_time_copy;
427 #endif
429 #ifdef CONFIG_FAIR_GROUP_SCHED
431 * h_load = weight * f(tg)
433 * Where f(tg) is the recursive weight fraction assigned to
434 * this group.
436 unsigned long h_load;
437 u64 last_h_load_update;
438 struct sched_entity *h_load_next;
439 #endif /* CONFIG_FAIR_GROUP_SCHED */
440 #endif /* CONFIG_SMP */
442 #ifdef CONFIG_FAIR_GROUP_SCHED
443 struct rq *rq; /* cpu runqueue to which this cfs_rq is attached */
446 * leaf cfs_rqs are those that hold tasks (lowest schedulable entity in
447 * a hierarchy). Non-leaf lrqs hold other higher schedulable entities
448 * (like users, containers etc.)
450 * leaf_cfs_rq_list ties together list of leaf cfs_rq's in a cpu. This
451 * list is used during load balance.
453 int on_list;
454 struct list_head leaf_cfs_rq_list;
455 struct task_group *tg; /* group that "owns" this runqueue */
457 #ifdef CONFIG_CFS_BANDWIDTH
458 int runtime_enabled;
459 u64 runtime_expires;
460 s64 runtime_remaining;
462 u64 throttled_clock, throttled_clock_task;
463 u64 throttled_clock_task_time;
464 int throttled, throttle_count;
465 struct list_head throttled_list;
466 #endif /* CONFIG_CFS_BANDWIDTH */
467 #endif /* CONFIG_FAIR_GROUP_SCHED */
470 static inline int rt_bandwidth_enabled(void)
472 return sysctl_sched_rt_runtime >= 0;
475 /* RT IPI pull logic requires IRQ_WORK */
476 #if defined(CONFIG_IRQ_WORK) && defined(CONFIG_SMP)
477 # define HAVE_RT_PUSH_IPI
478 #endif
480 /* Real-Time classes' related field in a runqueue: */
481 struct rt_rq {
482 struct rt_prio_array active;
483 unsigned int rt_nr_running;
484 unsigned int rr_nr_running;
485 #if defined CONFIG_SMP || defined CONFIG_RT_GROUP_SCHED
486 struct {
487 int curr; /* highest queued rt task prio */
488 #ifdef CONFIG_SMP
489 int next; /* next highest */
490 #endif
491 } highest_prio;
492 #endif
493 #ifdef CONFIG_SMP
494 unsigned long rt_nr_migratory;
495 unsigned long rt_nr_total;
496 int overloaded;
497 struct plist_head pushable_tasks;
498 #endif /* CONFIG_SMP */
499 int rt_queued;
501 int rt_throttled;
502 u64 rt_time;
503 u64 rt_runtime;
504 /* Nests inside the rq lock: */
505 raw_spinlock_t rt_runtime_lock;
507 #ifdef CONFIG_RT_GROUP_SCHED
508 unsigned long rt_nr_boosted;
510 struct rq *rq;
511 struct task_group *tg;
512 #endif
515 /* Deadline class' related fields in a runqueue */
516 struct dl_rq {
517 /* runqueue is an rbtree, ordered by deadline */
518 struct rb_root rb_root;
519 struct rb_node *rb_leftmost;
521 unsigned long dl_nr_running;
523 #ifdef CONFIG_SMP
525 * Deadline values of the currently executing and the
526 * earliest ready task on this rq. Caching these facilitates
527 * the decision wether or not a ready but not running task
528 * should migrate somewhere else.
530 struct {
531 u64 curr;
532 u64 next;
533 } earliest_dl;
535 unsigned long dl_nr_migratory;
536 int overloaded;
539 * Tasks on this rq that can be pushed away. They are kept in
540 * an rb-tree, ordered by tasks' deadlines, with caching
541 * of the leftmost (earliest deadline) element.
543 struct rb_root pushable_dl_tasks_root;
544 struct rb_node *pushable_dl_tasks_leftmost;
545 #else
546 struct dl_bw dl_bw;
547 #endif
550 #ifdef CONFIG_SMP
553 * We add the notion of a root-domain which will be used to define per-domain
554 * variables. Each exclusive cpuset essentially defines an island domain by
555 * fully partitioning the member cpus from any other cpuset. Whenever a new
556 * exclusive cpuset is created, we also create and attach a new root-domain
557 * object.
560 struct root_domain {
561 atomic_t refcount;
562 atomic_t rto_count;
563 struct rcu_head rcu;
564 cpumask_var_t span;
565 cpumask_var_t online;
567 /* Indicate more than one runnable task for any CPU */
568 bool overload;
571 * The bit corresponding to a CPU gets set here if such CPU has more
572 * than one runnable -deadline task (as it is below for RT tasks).
574 cpumask_var_t dlo_mask;
575 atomic_t dlo_count;
576 struct dl_bw dl_bw;
577 struct cpudl cpudl;
579 #ifdef HAVE_RT_PUSH_IPI
581 * For IPI pull requests, loop across the rto_mask.
583 struct irq_work rto_push_work;
584 raw_spinlock_t rto_lock;
585 /* These are only updated and read within rto_lock */
586 int rto_loop;
587 int rto_cpu;
588 /* These atomics are updated outside of a lock */
589 atomic_t rto_loop_next;
590 atomic_t rto_loop_start;
591 #endif
593 * The "RT overload" flag: it gets set if a CPU has more than
594 * one runnable RT task.
596 cpumask_var_t rto_mask;
597 struct cpupri cpupri;
599 unsigned long max_cpu_capacity;
602 extern struct root_domain def_root_domain;
603 extern void sched_get_rd(struct root_domain *rd);
604 extern void sched_put_rd(struct root_domain *rd);
606 #ifdef HAVE_RT_PUSH_IPI
607 extern void rto_push_irq_work_func(struct irq_work *work);
608 #endif
609 #endif /* CONFIG_SMP */
612 * This is the main, per-CPU runqueue data structure.
614 * Locking rule: those places that want to lock multiple runqueues
615 * (such as the load balancing or the thread migration code), lock
616 * acquire operations must be ordered by ascending &runqueue.
618 struct rq {
619 /* runqueue lock: */
620 raw_spinlock_t lock;
623 * nr_running and cpu_load should be in the same cacheline because
624 * remote CPUs use both these fields when doing load calculation.
626 unsigned int nr_running;
627 #ifdef CONFIG_NUMA_BALANCING
628 unsigned int nr_numa_running;
629 unsigned int nr_preferred_running;
630 #endif
631 #define CPU_LOAD_IDX_MAX 5
632 unsigned long cpu_load[CPU_LOAD_IDX_MAX];
633 #ifdef CONFIG_NO_HZ_COMMON
634 #ifdef CONFIG_SMP
635 unsigned long last_load_update_tick;
636 #endif /* CONFIG_SMP */
637 unsigned long nohz_flags;
638 #endif /* CONFIG_NO_HZ_COMMON */
639 #ifdef CONFIG_NO_HZ_FULL
640 unsigned long last_sched_tick;
641 #endif
642 /* capture load from *all* tasks on this cpu: */
643 struct load_weight load;
644 unsigned long nr_load_updates;
645 u64 nr_switches;
647 struct cfs_rq cfs;
648 struct rt_rq rt;
649 struct dl_rq dl;
651 #ifdef CONFIG_FAIR_GROUP_SCHED
652 /* list of leaf cfs_rq on this cpu: */
653 struct list_head leaf_cfs_rq_list;
654 #endif /* CONFIG_FAIR_GROUP_SCHED */
657 * This is part of a global counter where only the total sum
658 * over all CPUs matters. A task can increase this counter on
659 * one CPU and if it got migrated afterwards it may decrease
660 * it on another CPU. Always updated under the runqueue lock:
662 unsigned long nr_uninterruptible;
664 struct task_struct *curr, *idle, *stop;
665 unsigned long next_balance;
666 struct mm_struct *prev_mm;
668 unsigned int clock_skip_update;
669 u64 clock;
670 u64 clock_task;
672 atomic_t nr_iowait;
674 #ifdef CONFIG_SMP
675 struct root_domain *rd;
676 struct sched_domain *sd;
678 unsigned long cpu_capacity;
679 unsigned long cpu_capacity_orig;
681 struct callback_head *balance_callback;
683 unsigned char idle_balance;
684 /* For active balancing */
685 int active_balance;
686 int push_cpu;
687 struct cpu_stop_work active_balance_work;
688 /* cpu of this runqueue: */
689 int cpu;
690 int online;
692 struct list_head cfs_tasks;
694 u64 rt_avg;
695 u64 age_stamp;
696 u64 idle_stamp;
697 u64 avg_idle;
699 /* This is used to determine avg_idle's max value */
700 u64 max_idle_balance_cost;
701 #endif
703 #ifdef CONFIG_IRQ_TIME_ACCOUNTING
704 u64 prev_irq_time;
705 #endif
706 #ifdef CONFIG_PARAVIRT
707 u64 prev_steal_time;
708 #endif
709 #ifdef CONFIG_PARAVIRT_TIME_ACCOUNTING
710 u64 prev_steal_time_rq;
711 #endif
713 /* calc_load related fields */
714 unsigned long calc_load_update;
715 long calc_load_active;
717 #ifdef CONFIG_SCHED_HRTICK
718 #ifdef CONFIG_SMP
719 int hrtick_csd_pending;
720 struct call_single_data hrtick_csd;
721 #endif
722 struct hrtimer hrtick_timer;
723 #endif
725 #ifdef CONFIG_SCHEDSTATS
726 /* latency stats */
727 struct sched_info rq_sched_info;
728 unsigned long long rq_cpu_time;
729 /* could above be rq->cfs_rq.exec_clock + rq->rt_rq.rt_runtime ? */
731 /* sys_sched_yield() stats */
732 unsigned int yld_count;
734 /* schedule() stats */
735 unsigned int sched_count;
736 unsigned int sched_goidle;
738 /* try_to_wake_up() stats */
739 unsigned int ttwu_count;
740 unsigned int ttwu_local;
741 #endif
743 #ifdef CONFIG_SMP
744 struct llist_head wake_list;
745 #endif
747 #ifdef CONFIG_CPU_IDLE
748 /* Must be inspected within a rcu lock section */
749 struct cpuidle_state *idle_state;
750 #endif
753 static inline int cpu_of(struct rq *rq)
755 #ifdef CONFIG_SMP
756 return rq->cpu;
757 #else
758 return 0;
759 #endif
762 DECLARE_PER_CPU_SHARED_ALIGNED(struct rq, runqueues);
764 #define cpu_rq(cpu) (&per_cpu(runqueues, (cpu)))
765 #define this_rq() this_cpu_ptr(&runqueues)
766 #define task_rq(p) cpu_rq(task_cpu(p))
767 #define cpu_curr(cpu) (cpu_rq(cpu)->curr)
768 #define raw_rq() raw_cpu_ptr(&runqueues)
770 static inline u64 __rq_clock_broken(struct rq *rq)
772 return READ_ONCE(rq->clock);
775 static inline u64 rq_clock(struct rq *rq)
777 lockdep_assert_held(&rq->lock);
778 return rq->clock;
781 static inline u64 rq_clock_task(struct rq *rq)
783 lockdep_assert_held(&rq->lock);
784 return rq->clock_task;
787 #define RQCF_REQ_SKIP 0x01
788 #define RQCF_ACT_SKIP 0x02
790 static inline void rq_clock_skip_update(struct rq *rq, bool skip)
792 lockdep_assert_held(&rq->lock);
793 if (skip)
794 rq->clock_skip_update |= RQCF_REQ_SKIP;
795 else
796 rq->clock_skip_update &= ~RQCF_REQ_SKIP;
799 #ifdef CONFIG_NUMA
800 enum numa_topology_type {
801 NUMA_DIRECT,
802 NUMA_GLUELESS_MESH,
803 NUMA_BACKPLANE,
805 extern enum numa_topology_type sched_numa_topology_type;
806 extern int sched_max_numa_distance;
807 extern bool find_numa_distance(int distance);
808 #endif
810 #ifdef CONFIG_NUMA_BALANCING
811 /* The regions in numa_faults array from task_struct */
812 enum numa_faults_stats {
813 NUMA_MEM = 0,
814 NUMA_CPU,
815 NUMA_MEMBUF,
816 NUMA_CPUBUF
818 extern void sched_setnuma(struct task_struct *p, int node);
819 extern int migrate_task_to(struct task_struct *p, int cpu);
820 extern int migrate_swap(struct task_struct *, struct task_struct *);
821 #endif /* CONFIG_NUMA_BALANCING */
823 #ifdef CONFIG_SMP
825 static inline void
826 queue_balance_callback(struct rq *rq,
827 struct callback_head *head,
828 void (*func)(struct rq *rq))
830 lockdep_assert_held(&rq->lock);
832 if (unlikely(head->next))
833 return;
835 head->func = (void (*)(struct callback_head *))func;
836 head->next = rq->balance_callback;
837 rq->balance_callback = head;
840 extern void sched_ttwu_pending(void);
842 #define rcu_dereference_check_sched_domain(p) \
843 rcu_dereference_check((p), \
844 lockdep_is_held(&sched_domains_mutex))
847 * The domain tree (rq->sd) is protected by RCU's quiescent state transition.
848 * See detach_destroy_domains: synchronize_sched for details.
850 * The domain tree of any CPU may only be accessed from within
851 * preempt-disabled sections.
853 #define for_each_domain(cpu, __sd) \
854 for (__sd = rcu_dereference_check_sched_domain(cpu_rq(cpu)->sd); \
855 __sd; __sd = __sd->parent)
857 #define for_each_lower_domain(sd) for (; sd; sd = sd->child)
860 * highest_flag_domain - Return highest sched_domain containing flag.
861 * @cpu: The cpu whose highest level of sched domain is to
862 * be returned.
863 * @flag: The flag to check for the highest sched_domain
864 * for the given cpu.
866 * Returns the highest sched_domain of a cpu which contains the given flag.
868 static inline struct sched_domain *highest_flag_domain(int cpu, int flag)
870 struct sched_domain *sd, *hsd = NULL;
872 for_each_domain(cpu, sd) {
873 if (!(sd->flags & flag))
874 break;
875 hsd = sd;
878 return hsd;
881 static inline struct sched_domain *lowest_flag_domain(int cpu, int flag)
883 struct sched_domain *sd;
885 for_each_domain(cpu, sd) {
886 if (sd->flags & flag)
887 break;
890 return sd;
893 DECLARE_PER_CPU(struct sched_domain *, sd_llc);
894 DECLARE_PER_CPU(int, sd_llc_size);
895 DECLARE_PER_CPU(int, sd_llc_id);
896 DECLARE_PER_CPU(struct sched_domain_shared *, sd_llc_shared);
897 DECLARE_PER_CPU(struct sched_domain *, sd_numa);
898 DECLARE_PER_CPU(struct sched_domain *, sd_asym);
900 struct sched_group_capacity {
901 atomic_t ref;
903 * CPU capacity of this group, SCHED_CAPACITY_SCALE being max capacity
904 * for a single CPU.
906 unsigned int capacity;
907 unsigned long next_update;
908 int imbalance; /* XXX unrelated to capacity but shared group state */
910 unsigned long cpumask[0]; /* iteration mask */
913 struct sched_group {
914 struct sched_group *next; /* Must be a circular list */
915 atomic_t ref;
917 unsigned int group_weight;
918 struct sched_group_capacity *sgc;
921 * The CPUs this group covers.
923 * NOTE: this field is variable length. (Allocated dynamically
924 * by attaching extra space to the end of the structure,
925 * depending on how many CPUs the kernel has booted up with)
927 unsigned long cpumask[0];
930 static inline struct cpumask *sched_group_cpus(struct sched_group *sg)
932 return to_cpumask(sg->cpumask);
936 * cpumask masking which cpus in the group are allowed to iterate up the domain
937 * tree.
939 static inline struct cpumask *sched_group_mask(struct sched_group *sg)
941 return to_cpumask(sg->sgc->cpumask);
945 * group_first_cpu - Returns the first cpu in the cpumask of a sched_group.
946 * @group: The group whose first cpu is to be returned.
948 static inline unsigned int group_first_cpu(struct sched_group *group)
950 return cpumask_first(sched_group_cpus(group));
953 extern int group_balance_cpu(struct sched_group *sg);
955 #if defined(CONFIG_SCHED_DEBUG) && defined(CONFIG_SYSCTL)
956 void register_sched_domain_sysctl(void);
957 void unregister_sched_domain_sysctl(void);
958 #else
959 static inline void register_sched_domain_sysctl(void)
962 static inline void unregister_sched_domain_sysctl(void)
965 #endif
967 #else
969 static inline void sched_ttwu_pending(void) { }
971 #endif /* CONFIG_SMP */
973 #include "stats.h"
974 #include "auto_group.h"
976 #ifdef CONFIG_CGROUP_SCHED
979 * Return the group to which this tasks belongs.
981 * We cannot use task_css() and friends because the cgroup subsystem
982 * changes that value before the cgroup_subsys::attach() method is called,
983 * therefore we cannot pin it and might observe the wrong value.
985 * The same is true for autogroup's p->signal->autogroup->tg, the autogroup
986 * core changes this before calling sched_move_task().
988 * Instead we use a 'copy' which is updated from sched_move_task() while
989 * holding both task_struct::pi_lock and rq::lock.
991 static inline struct task_group *task_group(struct task_struct *p)
993 return p->sched_task_group;
996 /* Change a task's cfs_rq and parent entity if it moves across CPUs/groups */
997 static inline void set_task_rq(struct task_struct *p, unsigned int cpu)
999 #if defined(CONFIG_FAIR_GROUP_SCHED) || defined(CONFIG_RT_GROUP_SCHED)
1000 struct task_group *tg = task_group(p);
1001 #endif
1003 #ifdef CONFIG_FAIR_GROUP_SCHED
1004 set_task_rq_fair(&p->se, p->se.cfs_rq, tg->cfs_rq[cpu]);
1005 p->se.cfs_rq = tg->cfs_rq[cpu];
1006 p->se.parent = tg->se[cpu];
1007 #endif
1009 #ifdef CONFIG_RT_GROUP_SCHED
1010 p->rt.rt_rq = tg->rt_rq[cpu];
1011 p->rt.parent = tg->rt_se[cpu];
1012 #endif
1015 #else /* CONFIG_CGROUP_SCHED */
1017 static inline void set_task_rq(struct task_struct *p, unsigned int cpu) { }
1018 static inline struct task_group *task_group(struct task_struct *p)
1020 return NULL;
1023 #endif /* CONFIG_CGROUP_SCHED */
1025 static inline void __set_task_cpu(struct task_struct *p, unsigned int cpu)
1027 set_task_rq(p, cpu);
1028 #ifdef CONFIG_SMP
1030 * After ->cpu is set up to a new value, task_rq_lock(p, ...) can be
1031 * successfuly executed on another CPU. We must ensure that updates of
1032 * per-task data have been completed by this moment.
1034 smp_wmb();
1035 #ifdef CONFIG_THREAD_INFO_IN_TASK
1036 p->cpu = cpu;
1037 #else
1038 task_thread_info(p)->cpu = cpu;
1039 #endif
1040 p->wake_cpu = cpu;
1041 #endif
1045 * Tunables that become constants when CONFIG_SCHED_DEBUG is off:
1047 #ifdef CONFIG_SCHED_DEBUG
1048 # include <linux/static_key.h>
1049 # define const_debug __read_mostly
1050 #else
1051 # define const_debug const
1052 #endif
1054 extern const_debug unsigned int sysctl_sched_features;
1056 #define SCHED_FEAT(name, enabled) \
1057 __SCHED_FEAT_##name ,
1059 enum {
1060 #include "features.h"
1061 __SCHED_FEAT_NR,
1064 #undef SCHED_FEAT
1066 #if defined(CONFIG_SCHED_DEBUG) && defined(HAVE_JUMP_LABEL)
1067 #define SCHED_FEAT(name, enabled) \
1068 static __always_inline bool static_branch_##name(struct static_key *key) \
1070 return static_key_##enabled(key); \
1073 #include "features.h"
1075 #undef SCHED_FEAT
1077 extern struct static_key sched_feat_keys[__SCHED_FEAT_NR];
1078 #define sched_feat(x) (static_branch_##x(&sched_feat_keys[__SCHED_FEAT_##x]))
1079 #else /* !(SCHED_DEBUG && HAVE_JUMP_LABEL) */
1080 #define sched_feat(x) (sysctl_sched_features & (1UL << __SCHED_FEAT_##x))
1081 #endif /* SCHED_DEBUG && HAVE_JUMP_LABEL */
1083 extern struct static_key_false sched_numa_balancing;
1084 extern struct static_key_false sched_schedstats;
1086 static inline u64 global_rt_period(void)
1088 return (u64)sysctl_sched_rt_period * NSEC_PER_USEC;
1091 static inline u64 global_rt_runtime(void)
1093 if (sysctl_sched_rt_runtime < 0)
1094 return RUNTIME_INF;
1096 return (u64)sysctl_sched_rt_runtime * NSEC_PER_USEC;
1099 static inline int task_current(struct rq *rq, struct task_struct *p)
1101 return rq->curr == p;
1104 static inline int task_running(struct rq *rq, struct task_struct *p)
1106 #ifdef CONFIG_SMP
1107 return p->on_cpu;
1108 #else
1109 return task_current(rq, p);
1110 #endif
1113 static inline int task_on_rq_queued(struct task_struct *p)
1115 return p->on_rq == TASK_ON_RQ_QUEUED;
1118 static inline int task_on_rq_migrating(struct task_struct *p)
1120 return p->on_rq == TASK_ON_RQ_MIGRATING;
1123 #ifndef prepare_arch_switch
1124 # define prepare_arch_switch(next) do { } while (0)
1125 #endif
1126 #ifndef finish_arch_post_lock_switch
1127 # define finish_arch_post_lock_switch() do { } while (0)
1128 #endif
1130 static inline void prepare_lock_switch(struct rq *rq, struct task_struct *next)
1132 #ifdef CONFIG_SMP
1134 * We can optimise this out completely for !SMP, because the
1135 * SMP rebalancing from interrupt is the only thing that cares
1136 * here.
1138 next->on_cpu = 1;
1139 #endif
1142 static inline void finish_lock_switch(struct rq *rq, struct task_struct *prev)
1144 #ifdef CONFIG_SMP
1146 * After ->on_cpu is cleared, the task can be moved to a different CPU.
1147 * We must ensure this doesn't happen until the switch is completely
1148 * finished.
1150 * In particular, the load of prev->state in finish_task_switch() must
1151 * happen before this.
1153 * Pairs with the smp_cond_load_acquire() in try_to_wake_up().
1155 smp_store_release(&prev->on_cpu, 0);
1156 #endif
1157 #ifdef CONFIG_DEBUG_SPINLOCK
1158 /* this is a valid case when another task releases the spinlock */
1159 rq->lock.owner = current;
1160 #endif
1162 * If we are tracking spinlock dependencies then we have to
1163 * fix up the runqueue lock - which gets 'carried over' from
1164 * prev into current:
1166 spin_acquire(&rq->lock.dep_map, 0, 0, _THIS_IP_);
1168 raw_spin_unlock_irq(&rq->lock);
1172 * wake flags
1174 #define WF_SYNC 0x01 /* waker goes to sleep after wakeup */
1175 #define WF_FORK 0x02 /* child wakeup after fork */
1176 #define WF_MIGRATED 0x4 /* internal use, task got migrated */
1179 * To aid in avoiding the subversion of "niceness" due to uneven distribution
1180 * of tasks with abnormal "nice" values across CPUs the contribution that
1181 * each task makes to its run queue's load is weighted according to its
1182 * scheduling class and "nice" value. For SCHED_NORMAL tasks this is just a
1183 * scaled version of the new time slice allocation that they receive on time
1184 * slice expiry etc.
1187 #define WEIGHT_IDLEPRIO 3
1188 #define WMULT_IDLEPRIO 1431655765
1190 extern const int sched_prio_to_weight[40];
1191 extern const u32 sched_prio_to_wmult[40];
1194 * {de,en}queue flags:
1196 * DEQUEUE_SLEEP - task is no longer runnable
1197 * ENQUEUE_WAKEUP - task just became runnable
1199 * SAVE/RESTORE - an otherwise spurious dequeue/enqueue, done to ensure tasks
1200 * are in a known state which allows modification. Such pairs
1201 * should preserve as much state as possible.
1203 * MOVE - paired with SAVE/RESTORE, explicitly does not preserve the location
1204 * in the runqueue.
1206 * ENQUEUE_HEAD - place at front of runqueue (tail if not specified)
1207 * ENQUEUE_REPLENISH - CBS (replenish runtime and postpone deadline)
1208 * ENQUEUE_MIGRATED - the task was migrated during wakeup
1212 #define DEQUEUE_SLEEP 0x01
1213 #define DEQUEUE_SAVE 0x02 /* matches ENQUEUE_RESTORE */
1214 #define DEQUEUE_MOVE 0x04 /* matches ENQUEUE_MOVE */
1216 #define ENQUEUE_WAKEUP 0x01
1217 #define ENQUEUE_RESTORE 0x02
1218 #define ENQUEUE_MOVE 0x04
1220 #define ENQUEUE_HEAD 0x08
1221 #define ENQUEUE_REPLENISH 0x10
1222 #ifdef CONFIG_SMP
1223 #define ENQUEUE_MIGRATED 0x20
1224 #else
1225 #define ENQUEUE_MIGRATED 0x00
1226 #endif
1228 #define RETRY_TASK ((void *)-1UL)
1230 struct sched_class {
1231 const struct sched_class *next;
1233 void (*enqueue_task) (struct rq *rq, struct task_struct *p, int flags);
1234 void (*dequeue_task) (struct rq *rq, struct task_struct *p, int flags);
1235 void (*yield_task) (struct rq *rq);
1236 bool (*yield_to_task) (struct rq *rq, struct task_struct *p, bool preempt);
1238 void (*check_preempt_curr) (struct rq *rq, struct task_struct *p, int flags);
1241 * It is the responsibility of the pick_next_task() method that will
1242 * return the next task to call put_prev_task() on the @prev task or
1243 * something equivalent.
1245 * May return RETRY_TASK when it finds a higher prio class has runnable
1246 * tasks.
1248 struct task_struct * (*pick_next_task) (struct rq *rq,
1249 struct task_struct *prev,
1250 struct pin_cookie cookie);
1251 void (*put_prev_task) (struct rq *rq, struct task_struct *p);
1253 #ifdef CONFIG_SMP
1254 int (*select_task_rq)(struct task_struct *p, int task_cpu, int sd_flag, int flags);
1255 void (*migrate_task_rq)(struct task_struct *p);
1257 void (*task_woken) (struct rq *this_rq, struct task_struct *task);
1259 void (*set_cpus_allowed)(struct task_struct *p,
1260 const struct cpumask *newmask);
1262 void (*rq_online)(struct rq *rq);
1263 void (*rq_offline)(struct rq *rq);
1264 #endif
1266 void (*set_curr_task) (struct rq *rq);
1267 void (*task_tick) (struct rq *rq, struct task_struct *p, int queued);
1268 void (*task_fork) (struct task_struct *p);
1269 void (*task_dead) (struct task_struct *p);
1272 * The switched_from() call is allowed to drop rq->lock, therefore we
1273 * cannot assume the switched_from/switched_to pair is serliazed by
1274 * rq->lock. They are however serialized by p->pi_lock.
1276 void (*switched_from) (struct rq *this_rq, struct task_struct *task);
1277 void (*switched_to) (struct rq *this_rq, struct task_struct *task);
1278 void (*prio_changed) (struct rq *this_rq, struct task_struct *task,
1279 int oldprio);
1281 unsigned int (*get_rr_interval) (struct rq *rq,
1282 struct task_struct *task);
1284 void (*update_curr) (struct rq *rq);
1286 #define TASK_SET_GROUP 0
1287 #define TASK_MOVE_GROUP 1
1289 #ifdef CONFIG_FAIR_GROUP_SCHED
1290 void (*task_change_group) (struct task_struct *p, int type);
1291 #endif
1294 static inline void put_prev_task(struct rq *rq, struct task_struct *prev)
1296 prev->sched_class->put_prev_task(rq, prev);
1299 static inline void set_curr_task(struct rq *rq, struct task_struct *curr)
1301 curr->sched_class->set_curr_task(rq);
1304 #define sched_class_highest (&stop_sched_class)
1305 #define for_each_class(class) \
1306 for (class = sched_class_highest; class; class = class->next)
1308 extern const struct sched_class stop_sched_class;
1309 extern const struct sched_class dl_sched_class;
1310 extern const struct sched_class rt_sched_class;
1311 extern const struct sched_class fair_sched_class;
1312 extern const struct sched_class idle_sched_class;
1315 #ifdef CONFIG_SMP
1317 extern void update_group_capacity(struct sched_domain *sd, int cpu);
1319 extern void trigger_load_balance(struct rq *rq);
1321 extern void set_cpus_allowed_common(struct task_struct *p, const struct cpumask *new_mask);
1323 #endif
1325 #ifdef CONFIG_CPU_IDLE
1326 static inline void idle_set_state(struct rq *rq,
1327 struct cpuidle_state *idle_state)
1329 rq->idle_state = idle_state;
1332 static inline struct cpuidle_state *idle_get_state(struct rq *rq)
1334 SCHED_WARN_ON(!rcu_read_lock_held());
1335 return rq->idle_state;
1337 #else
1338 static inline void idle_set_state(struct rq *rq,
1339 struct cpuidle_state *idle_state)
1343 static inline struct cpuidle_state *idle_get_state(struct rq *rq)
1345 return NULL;
1347 #endif
1349 extern void sysrq_sched_debug_show(void);
1350 extern void sched_init_granularity(void);
1351 extern void update_max_interval(void);
1353 extern void init_sched_dl_class(void);
1354 extern void init_sched_rt_class(void);
1355 extern void init_sched_fair_class(void);
1357 extern void resched_curr(struct rq *rq);
1358 extern void resched_cpu(int cpu);
1360 extern struct rt_bandwidth def_rt_bandwidth;
1361 extern void init_rt_bandwidth(struct rt_bandwidth *rt_b, u64 period, u64 runtime);
1363 extern struct dl_bandwidth def_dl_bandwidth;
1364 extern void init_dl_bandwidth(struct dl_bandwidth *dl_b, u64 period, u64 runtime);
1365 extern void init_dl_task_timer(struct sched_dl_entity *dl_se);
1367 unsigned long to_ratio(u64 period, u64 runtime);
1369 extern void init_entity_runnable_average(struct sched_entity *se);
1370 extern void post_init_entity_util_avg(struct sched_entity *se);
1372 #ifdef CONFIG_NO_HZ_FULL
1373 extern bool sched_can_stop_tick(struct rq *rq);
1376 * Tick may be needed by tasks in the runqueue depending on their policy and
1377 * requirements. If tick is needed, lets send the target an IPI to kick it out of
1378 * nohz mode if necessary.
1380 static inline void sched_update_tick_dependency(struct rq *rq)
1382 int cpu;
1384 if (!tick_nohz_full_enabled())
1385 return;
1387 cpu = cpu_of(rq);
1389 if (!tick_nohz_full_cpu(cpu))
1390 return;
1392 if (sched_can_stop_tick(rq))
1393 tick_nohz_dep_clear_cpu(cpu, TICK_DEP_BIT_SCHED);
1394 else
1395 tick_nohz_dep_set_cpu(cpu, TICK_DEP_BIT_SCHED);
1397 #else
1398 static inline void sched_update_tick_dependency(struct rq *rq) { }
1399 #endif
1401 static inline void add_nr_running(struct rq *rq, unsigned count)
1403 unsigned prev_nr = rq->nr_running;
1405 rq->nr_running = prev_nr + count;
1407 if (prev_nr < 2 && rq->nr_running >= 2) {
1408 #ifdef CONFIG_SMP
1409 if (!rq->rd->overload)
1410 rq->rd->overload = true;
1411 #endif
1414 sched_update_tick_dependency(rq);
1417 static inline void sub_nr_running(struct rq *rq, unsigned count)
1419 rq->nr_running -= count;
1420 /* Check if we still need preemption */
1421 sched_update_tick_dependency(rq);
1424 static inline void rq_last_tick_reset(struct rq *rq)
1426 #ifdef CONFIG_NO_HZ_FULL
1427 rq->last_sched_tick = jiffies;
1428 #endif
1431 extern void update_rq_clock(struct rq *rq);
1433 extern void activate_task(struct rq *rq, struct task_struct *p, int flags);
1434 extern void deactivate_task(struct rq *rq, struct task_struct *p, int flags);
1436 extern void check_preempt_curr(struct rq *rq, struct task_struct *p, int flags);
1438 extern const_debug unsigned int sysctl_sched_time_avg;
1439 extern const_debug unsigned int sysctl_sched_nr_migrate;
1440 extern const_debug unsigned int sysctl_sched_migration_cost;
1442 static inline u64 sched_avg_period(void)
1444 return (u64)sysctl_sched_time_avg * NSEC_PER_MSEC / 2;
1447 #ifdef CONFIG_SCHED_HRTICK
1450 * Use hrtick when:
1451 * - enabled by features
1452 * - hrtimer is actually high res
1454 static inline int hrtick_enabled(struct rq *rq)
1456 if (!sched_feat(HRTICK))
1457 return 0;
1458 if (!cpu_active(cpu_of(rq)))
1459 return 0;
1460 return hrtimer_is_hres_active(&rq->hrtick_timer);
1463 void hrtick_start(struct rq *rq, u64 delay);
1465 #else
1467 static inline int hrtick_enabled(struct rq *rq)
1469 return 0;
1472 #endif /* CONFIG_SCHED_HRTICK */
1474 #ifdef CONFIG_SMP
1475 extern void sched_avg_update(struct rq *rq);
1477 #ifndef arch_scale_freq_capacity
1478 static __always_inline
1479 unsigned long arch_scale_freq_capacity(struct sched_domain *sd, int cpu)
1481 return SCHED_CAPACITY_SCALE;
1483 #endif
1485 #ifndef arch_scale_cpu_capacity
1486 static __always_inline
1487 unsigned long arch_scale_cpu_capacity(struct sched_domain *sd, int cpu)
1489 if (sd && (sd->flags & SD_SHARE_CPUCAPACITY) && (sd->span_weight > 1))
1490 return sd->smt_gain / sd->span_weight;
1492 return SCHED_CAPACITY_SCALE;
1494 #endif
1496 static inline void sched_rt_avg_update(struct rq *rq, u64 rt_delta)
1498 rq->rt_avg += rt_delta * arch_scale_freq_capacity(NULL, cpu_of(rq));
1499 sched_avg_update(rq);
1501 #else
1502 static inline void sched_rt_avg_update(struct rq *rq, u64 rt_delta) { }
1503 static inline void sched_avg_update(struct rq *rq) { }
1504 #endif
1506 struct rq_flags {
1507 unsigned long flags;
1508 struct pin_cookie cookie;
1511 struct rq *__task_rq_lock(struct task_struct *p, struct rq_flags *rf)
1512 __acquires(rq->lock);
1513 struct rq *task_rq_lock(struct task_struct *p, struct rq_flags *rf)
1514 __acquires(p->pi_lock)
1515 __acquires(rq->lock);
1517 static inline void __task_rq_unlock(struct rq *rq, struct rq_flags *rf)
1518 __releases(rq->lock)
1520 lockdep_unpin_lock(&rq->lock, rf->cookie);
1521 raw_spin_unlock(&rq->lock);
1524 static inline void
1525 task_rq_unlock(struct rq *rq, struct task_struct *p, struct rq_flags *rf)
1526 __releases(rq->lock)
1527 __releases(p->pi_lock)
1529 lockdep_unpin_lock(&rq->lock, rf->cookie);
1530 raw_spin_unlock(&rq->lock);
1531 raw_spin_unlock_irqrestore(&p->pi_lock, rf->flags);
1534 #ifdef CONFIG_SMP
1535 #ifdef CONFIG_PREEMPT
1537 static inline void double_rq_lock(struct rq *rq1, struct rq *rq2);
1540 * fair double_lock_balance: Safely acquires both rq->locks in a fair
1541 * way at the expense of forcing extra atomic operations in all
1542 * invocations. This assures that the double_lock is acquired using the
1543 * same underlying policy as the spinlock_t on this architecture, which
1544 * reduces latency compared to the unfair variant below. However, it
1545 * also adds more overhead and therefore may reduce throughput.
1547 static inline int _double_lock_balance(struct rq *this_rq, struct rq *busiest)
1548 __releases(this_rq->lock)
1549 __acquires(busiest->lock)
1550 __acquires(this_rq->lock)
1552 raw_spin_unlock(&this_rq->lock);
1553 double_rq_lock(this_rq, busiest);
1555 return 1;
1558 #else
1560 * Unfair double_lock_balance: Optimizes throughput at the expense of
1561 * latency by eliminating extra atomic operations when the locks are
1562 * already in proper order on entry. This favors lower cpu-ids and will
1563 * grant the double lock to lower cpus over higher ids under contention,
1564 * regardless of entry order into the function.
1566 static inline int _double_lock_balance(struct rq *this_rq, struct rq *busiest)
1567 __releases(this_rq->lock)
1568 __acquires(busiest->lock)
1569 __acquires(this_rq->lock)
1571 int ret = 0;
1573 if (unlikely(!raw_spin_trylock(&busiest->lock))) {
1574 if (busiest < this_rq) {
1575 raw_spin_unlock(&this_rq->lock);
1576 raw_spin_lock(&busiest->lock);
1577 raw_spin_lock_nested(&this_rq->lock,
1578 SINGLE_DEPTH_NESTING);
1579 ret = 1;
1580 } else
1581 raw_spin_lock_nested(&busiest->lock,
1582 SINGLE_DEPTH_NESTING);
1584 return ret;
1587 #endif /* CONFIG_PREEMPT */
1590 * double_lock_balance - lock the busiest runqueue, this_rq is locked already.
1592 static inline int double_lock_balance(struct rq *this_rq, struct rq *busiest)
1594 if (unlikely(!irqs_disabled())) {
1595 /* printk() doesn't work good under rq->lock */
1596 raw_spin_unlock(&this_rq->lock);
1597 BUG_ON(1);
1600 return _double_lock_balance(this_rq, busiest);
1603 static inline void double_unlock_balance(struct rq *this_rq, struct rq *busiest)
1604 __releases(busiest->lock)
1606 raw_spin_unlock(&busiest->lock);
1607 lock_set_subclass(&this_rq->lock.dep_map, 0, _RET_IP_);
1610 static inline void double_lock(spinlock_t *l1, spinlock_t *l2)
1612 if (l1 > l2)
1613 swap(l1, l2);
1615 spin_lock(l1);
1616 spin_lock_nested(l2, SINGLE_DEPTH_NESTING);
1619 static inline void double_lock_irq(spinlock_t *l1, spinlock_t *l2)
1621 if (l1 > l2)
1622 swap(l1, l2);
1624 spin_lock_irq(l1);
1625 spin_lock_nested(l2, SINGLE_DEPTH_NESTING);
1628 static inline void double_raw_lock(raw_spinlock_t *l1, raw_spinlock_t *l2)
1630 if (l1 > l2)
1631 swap(l1, l2);
1633 raw_spin_lock(l1);
1634 raw_spin_lock_nested(l2, SINGLE_DEPTH_NESTING);
1638 * double_rq_lock - safely lock two runqueues
1640 * Note this does not disable interrupts like task_rq_lock,
1641 * you need to do so manually before calling.
1643 static inline void double_rq_lock(struct rq *rq1, struct rq *rq2)
1644 __acquires(rq1->lock)
1645 __acquires(rq2->lock)
1647 BUG_ON(!irqs_disabled());
1648 if (rq1 == rq2) {
1649 raw_spin_lock(&rq1->lock);
1650 __acquire(rq2->lock); /* Fake it out ;) */
1651 } else {
1652 if (rq1 < rq2) {
1653 raw_spin_lock(&rq1->lock);
1654 raw_spin_lock_nested(&rq2->lock, SINGLE_DEPTH_NESTING);
1655 } else {
1656 raw_spin_lock(&rq2->lock);
1657 raw_spin_lock_nested(&rq1->lock, SINGLE_DEPTH_NESTING);
1663 * double_rq_unlock - safely unlock two runqueues
1665 * Note this does not restore interrupts like task_rq_unlock,
1666 * you need to do so manually after calling.
1668 static inline void double_rq_unlock(struct rq *rq1, struct rq *rq2)
1669 __releases(rq1->lock)
1670 __releases(rq2->lock)
1672 raw_spin_unlock(&rq1->lock);
1673 if (rq1 != rq2)
1674 raw_spin_unlock(&rq2->lock);
1675 else
1676 __release(rq2->lock);
1679 #else /* CONFIG_SMP */
1682 * double_rq_lock - safely lock two runqueues
1684 * Note this does not disable interrupts like task_rq_lock,
1685 * you need to do so manually before calling.
1687 static inline void double_rq_lock(struct rq *rq1, struct rq *rq2)
1688 __acquires(rq1->lock)
1689 __acquires(rq2->lock)
1691 BUG_ON(!irqs_disabled());
1692 BUG_ON(rq1 != rq2);
1693 raw_spin_lock(&rq1->lock);
1694 __acquire(rq2->lock); /* Fake it out ;) */
1698 * double_rq_unlock - safely unlock two runqueues
1700 * Note this does not restore interrupts like task_rq_unlock,
1701 * you need to do so manually after calling.
1703 static inline void double_rq_unlock(struct rq *rq1, struct rq *rq2)
1704 __releases(rq1->lock)
1705 __releases(rq2->lock)
1707 BUG_ON(rq1 != rq2);
1708 raw_spin_unlock(&rq1->lock);
1709 __release(rq2->lock);
1712 #endif
1714 extern struct sched_entity *__pick_first_entity(struct cfs_rq *cfs_rq);
1715 extern struct sched_entity *__pick_last_entity(struct cfs_rq *cfs_rq);
1717 #ifdef CONFIG_SCHED_DEBUG
1718 extern void print_cfs_stats(struct seq_file *m, int cpu);
1719 extern void print_rt_stats(struct seq_file *m, int cpu);
1720 extern void print_dl_stats(struct seq_file *m, int cpu);
1721 extern void
1722 print_cfs_rq(struct seq_file *m, int cpu, struct cfs_rq *cfs_rq);
1724 #ifdef CONFIG_NUMA_BALANCING
1725 extern void
1726 show_numa_stats(struct task_struct *p, struct seq_file *m);
1727 extern void
1728 print_numa_stats(struct seq_file *m, int node, unsigned long tsf,
1729 unsigned long tpf, unsigned long gsf, unsigned long gpf);
1730 #endif /* CONFIG_NUMA_BALANCING */
1731 #endif /* CONFIG_SCHED_DEBUG */
1733 extern void init_cfs_rq(struct cfs_rq *cfs_rq);
1734 extern void init_rt_rq(struct rt_rq *rt_rq);
1735 extern void init_dl_rq(struct dl_rq *dl_rq);
1737 extern void cfs_bandwidth_usage_inc(void);
1738 extern void cfs_bandwidth_usage_dec(void);
1740 #ifdef CONFIG_NO_HZ_COMMON
1741 enum rq_nohz_flag_bits {
1742 NOHZ_TICK_STOPPED,
1743 NOHZ_BALANCE_KICK,
1746 #define nohz_flags(cpu) (&cpu_rq(cpu)->nohz_flags)
1748 extern void nohz_balance_exit_idle(unsigned int cpu);
1749 #else
1750 static inline void nohz_balance_exit_idle(unsigned int cpu) { }
1751 #endif
1753 #ifdef CONFIG_IRQ_TIME_ACCOUNTING
1754 struct irqtime {
1755 u64 total;
1756 u64 tick_delta;
1757 u64 irq_start_time;
1758 struct u64_stats_sync sync;
1761 DECLARE_PER_CPU(struct irqtime, cpu_irqtime);
1764 * Returns the irqtime minus the softirq time computed by ksoftirqd.
1765 * Otherwise ksoftirqd's sum_exec_runtime is substracted its own runtime
1766 * and never move forward.
1768 static inline u64 irq_time_read(int cpu)
1770 struct irqtime *irqtime = &per_cpu(cpu_irqtime, cpu);
1771 unsigned int seq;
1772 u64 total;
1774 do {
1775 seq = __u64_stats_fetch_begin(&irqtime->sync);
1776 total = irqtime->total;
1777 } while (__u64_stats_fetch_retry(&irqtime->sync, seq));
1779 return total;
1781 #endif /* CONFIG_IRQ_TIME_ACCOUNTING */
1783 #ifdef CONFIG_CPU_FREQ
1784 DECLARE_PER_CPU(struct update_util_data *, cpufreq_update_util_data);
1787 * cpufreq_update_util - Take a note about CPU utilization changes.
1788 * @rq: Runqueue to carry out the update for.
1789 * @flags: Update reason flags.
1791 * This function is called by the scheduler on the CPU whose utilization is
1792 * being updated.
1794 * It can only be called from RCU-sched read-side critical sections.
1796 * The way cpufreq is currently arranged requires it to evaluate the CPU
1797 * performance state (frequency/voltage) on a regular basis to prevent it from
1798 * being stuck in a completely inadequate performance level for too long.
1799 * That is not guaranteed to happen if the updates are only triggered from CFS,
1800 * though, because they may not be coming in if RT or deadline tasks are active
1801 * all the time (or there are RT and DL tasks only).
1803 * As a workaround for that issue, this function is called by the RT and DL
1804 * sched classes to trigger extra cpufreq updates to prevent it from stalling,
1805 * but that really is a band-aid. Going forward it should be replaced with
1806 * solutions targeted more specifically at RT and DL tasks.
1808 static inline void cpufreq_update_util(struct rq *rq, unsigned int flags)
1810 struct update_util_data *data;
1812 data = rcu_dereference_sched(*this_cpu_ptr(&cpufreq_update_util_data));
1813 if (data)
1814 data->func(data, rq_clock(rq), flags);
1817 static inline void cpufreq_update_this_cpu(struct rq *rq, unsigned int flags)
1819 if (cpu_of(rq) == smp_processor_id())
1820 cpufreq_update_util(rq, flags);
1822 #else
1823 static inline void cpufreq_update_util(struct rq *rq, unsigned int flags) {}
1824 static inline void cpufreq_update_this_cpu(struct rq *rq, unsigned int flags) {}
1825 #endif /* CONFIG_CPU_FREQ */
1827 #ifdef arch_scale_freq_capacity
1828 #ifndef arch_scale_freq_invariant
1829 #define arch_scale_freq_invariant() (true)
1830 #endif
1831 #else /* arch_scale_freq_capacity */
1832 #define arch_scale_freq_invariant() (false)
1833 #endif