r8152: fix tx packets accounting
[linux/fpc-iii.git] / kernel / sched / sched.h
blobf564a1d2c9d5323ff5c4a446e9560e32e42de7f0
2 #include <linux/sched.h>
3 #include <linux/sched/sysctl.h>
4 #include <linux/sched/rt.h>
5 #include <linux/u64_stats_sync.h>
6 #include <linux/sched/deadline.h>
7 #include <linux/binfmts.h>
8 #include <linux/mutex.h>
9 #include <linux/spinlock.h>
10 #include <linux/stop_machine.h>
11 #include <linux/irq_work.h>
12 #include <linux/tick.h>
13 #include <linux/slab.h>
15 #include "cpupri.h"
16 #include "cpudeadline.h"
17 #include "cpuacct.h"
19 #ifdef CONFIG_SCHED_DEBUG
20 #define SCHED_WARN_ON(x) WARN_ONCE(x, #x)
21 #else
22 #define SCHED_WARN_ON(x) ((void)(x))
23 #endif
25 struct rq;
26 struct cpuidle_state;
28 /* task_struct::on_rq states: */
29 #define TASK_ON_RQ_QUEUED 1
30 #define TASK_ON_RQ_MIGRATING 2
32 extern __read_mostly int scheduler_running;
34 extern unsigned long calc_load_update;
35 extern atomic_long_t calc_load_tasks;
37 extern void calc_global_load_tick(struct rq *this_rq);
38 extern long calc_load_fold_active(struct rq *this_rq, long adjust);
40 #ifdef CONFIG_SMP
41 extern void cpu_load_update_active(struct rq *this_rq);
42 #else
43 static inline void cpu_load_update_active(struct rq *this_rq) { }
44 #endif
46 #ifdef CONFIG_SCHED_SMT
47 extern void update_idle_core(struct rq *rq);
48 #else
49 static inline void update_idle_core(struct rq *rq) { }
50 #endif
53 * Helpers for converting nanosecond timing to jiffy resolution
55 #define NS_TO_JIFFIES(TIME) ((unsigned long)(TIME) / (NSEC_PER_SEC / HZ))
58 * Increase resolution of nice-level calculations for 64-bit architectures.
59 * The extra resolution improves shares distribution and load balancing of
60 * low-weight task groups (eg. nice +19 on an autogroup), deeper taskgroup
61 * hierarchies, especially on larger systems. This is not a user-visible change
62 * and does not change the user-interface for setting shares/weights.
64 * We increase resolution only if we have enough bits to allow this increased
65 * resolution (i.e. 64bit). The costs for increasing resolution when 32bit are
66 * pretty high and the returns do not justify the increased costs.
68 * Really only required when CONFIG_FAIR_GROUP_SCHED is also set, but to
69 * increase coverage and consistency always enable it on 64bit platforms.
71 #ifdef CONFIG_64BIT
72 # define NICE_0_LOAD_SHIFT (SCHED_FIXEDPOINT_SHIFT + SCHED_FIXEDPOINT_SHIFT)
73 # define scale_load(w) ((w) << SCHED_FIXEDPOINT_SHIFT)
74 # define scale_load_down(w) ((w) >> SCHED_FIXEDPOINT_SHIFT)
75 #else
76 # define NICE_0_LOAD_SHIFT (SCHED_FIXEDPOINT_SHIFT)
77 # define scale_load(w) (w)
78 # define scale_load_down(w) (w)
79 #endif
82 * Task weight (visible to users) and its load (invisible to users) have
83 * independent resolution, but they should be well calibrated. We use
84 * scale_load() and scale_load_down(w) to convert between them. The
85 * following must be true:
87 * scale_load(sched_prio_to_weight[USER_PRIO(NICE_TO_PRIO(0))]) == NICE_0_LOAD
90 #define NICE_0_LOAD (1L << NICE_0_LOAD_SHIFT)
93 * Single value that decides SCHED_DEADLINE internal math precision.
94 * 10 -> just above 1us
95 * 9 -> just above 0.5us
97 #define DL_SCALE (10)
100 * These are the 'tuning knobs' of the scheduler:
104 * single value that denotes runtime == period, ie unlimited time.
106 #define RUNTIME_INF ((u64)~0ULL)
108 static inline int idle_policy(int policy)
110 return policy == SCHED_IDLE;
112 static inline int fair_policy(int policy)
114 return policy == SCHED_NORMAL || policy == SCHED_BATCH;
117 static inline int rt_policy(int policy)
119 return policy == SCHED_FIFO || policy == SCHED_RR;
122 static inline int dl_policy(int policy)
124 return policy == SCHED_DEADLINE;
126 static inline bool valid_policy(int policy)
128 return idle_policy(policy) || fair_policy(policy) ||
129 rt_policy(policy) || dl_policy(policy);
132 static inline int task_has_rt_policy(struct task_struct *p)
134 return rt_policy(p->policy);
137 static inline int task_has_dl_policy(struct task_struct *p)
139 return dl_policy(p->policy);
143 * Tells if entity @a should preempt entity @b.
145 static inline bool
146 dl_entity_preempt(struct sched_dl_entity *a, struct sched_dl_entity *b)
148 return dl_time_before(a->deadline, b->deadline);
152 * This is the priority-queue data structure of the RT scheduling class:
154 struct rt_prio_array {
155 DECLARE_BITMAP(bitmap, MAX_RT_PRIO+1); /* include 1 bit for delimiter */
156 struct list_head queue[MAX_RT_PRIO];
159 struct rt_bandwidth {
160 /* nests inside the rq lock: */
161 raw_spinlock_t rt_runtime_lock;
162 ktime_t rt_period;
163 u64 rt_runtime;
164 struct hrtimer rt_period_timer;
165 unsigned int rt_period_active;
168 void __dl_clear_params(struct task_struct *p);
171 * To keep the bandwidth of -deadline tasks and groups under control
172 * we need some place where:
173 * - store the maximum -deadline bandwidth of the system (the group);
174 * - cache the fraction of that bandwidth that is currently allocated.
176 * This is all done in the data structure below. It is similar to the
177 * one used for RT-throttling (rt_bandwidth), with the main difference
178 * that, since here we are only interested in admission control, we
179 * do not decrease any runtime while the group "executes", neither we
180 * need a timer to replenish it.
182 * With respect to SMP, the bandwidth is given on a per-CPU basis,
183 * meaning that:
184 * - dl_bw (< 100%) is the bandwidth of the system (group) on each CPU;
185 * - dl_total_bw array contains, in the i-eth element, the currently
186 * allocated bandwidth on the i-eth CPU.
187 * Moreover, groups consume bandwidth on each CPU, while tasks only
188 * consume bandwidth on the CPU they're running on.
189 * Finally, dl_total_bw_cpu is used to cache the index of dl_total_bw
190 * that will be shown the next time the proc or cgroup controls will
191 * be red. It on its turn can be changed by writing on its own
192 * control.
194 struct dl_bandwidth {
195 raw_spinlock_t dl_runtime_lock;
196 u64 dl_runtime;
197 u64 dl_period;
200 static inline int dl_bandwidth_enabled(void)
202 return sysctl_sched_rt_runtime >= 0;
205 extern struct dl_bw *dl_bw_of(int i);
207 struct dl_bw {
208 raw_spinlock_t lock;
209 u64 bw, total_bw;
212 static inline
213 void __dl_clear(struct dl_bw *dl_b, u64 tsk_bw)
215 dl_b->total_bw -= tsk_bw;
218 static inline
219 void __dl_add(struct dl_bw *dl_b, u64 tsk_bw)
221 dl_b->total_bw += tsk_bw;
224 static inline
225 bool __dl_overflow(struct dl_bw *dl_b, int cpus, u64 old_bw, u64 new_bw)
227 return dl_b->bw != -1 &&
228 dl_b->bw * cpus < dl_b->total_bw - old_bw + new_bw;
231 extern struct mutex sched_domains_mutex;
233 #ifdef CONFIG_CGROUP_SCHED
235 #include <linux/cgroup.h>
237 struct cfs_rq;
238 struct rt_rq;
240 extern struct list_head task_groups;
242 struct cfs_bandwidth {
243 #ifdef CONFIG_CFS_BANDWIDTH
244 raw_spinlock_t lock;
245 ktime_t period;
246 u64 quota, runtime;
247 s64 hierarchical_quota;
248 u64 runtime_expires;
250 int idle, period_active;
251 struct hrtimer period_timer, slack_timer;
252 struct list_head throttled_cfs_rq;
254 /* statistics */
255 int nr_periods, nr_throttled;
256 u64 throttled_time;
257 #endif
260 /* task group related information */
261 struct task_group {
262 struct cgroup_subsys_state css;
264 #ifdef CONFIG_FAIR_GROUP_SCHED
265 /* schedulable entities of this group on each cpu */
266 struct sched_entity **se;
267 /* runqueue "owned" by this group on each cpu */
268 struct cfs_rq **cfs_rq;
269 unsigned long shares;
271 #ifdef CONFIG_SMP
273 * load_avg can be heavily contended at clock tick time, so put
274 * it in its own cacheline separated from the fields above which
275 * will also be accessed at each tick.
277 atomic_long_t load_avg ____cacheline_aligned;
278 #endif
279 #endif
281 #ifdef CONFIG_RT_GROUP_SCHED
282 struct sched_rt_entity **rt_se;
283 struct rt_rq **rt_rq;
285 struct rt_bandwidth rt_bandwidth;
286 #endif
288 struct rcu_head rcu;
289 struct list_head list;
291 struct task_group *parent;
292 struct list_head siblings;
293 struct list_head children;
295 #ifdef CONFIG_SCHED_AUTOGROUP
296 struct autogroup *autogroup;
297 #endif
299 struct cfs_bandwidth cfs_bandwidth;
302 #ifdef CONFIG_FAIR_GROUP_SCHED
303 #define ROOT_TASK_GROUP_LOAD NICE_0_LOAD
306 * A weight of 0 or 1 can cause arithmetics problems.
307 * A weight of a cfs_rq is the sum of weights of which entities
308 * are queued on this cfs_rq, so a weight of a entity should not be
309 * too large, so as the shares value of a task group.
310 * (The default weight is 1024 - so there's no practical
311 * limitation from this.)
313 #define MIN_SHARES (1UL << 1)
314 #define MAX_SHARES (1UL << 18)
315 #endif
317 typedef int (*tg_visitor)(struct task_group *, void *);
319 extern int walk_tg_tree_from(struct task_group *from,
320 tg_visitor down, tg_visitor up, void *data);
323 * Iterate the full tree, calling @down when first entering a node and @up when
324 * leaving it for the final time.
326 * Caller must hold rcu_lock or sufficient equivalent.
328 static inline int walk_tg_tree(tg_visitor down, tg_visitor up, void *data)
330 return walk_tg_tree_from(&root_task_group, down, up, data);
333 extern int tg_nop(struct task_group *tg, void *data);
335 extern void free_fair_sched_group(struct task_group *tg);
336 extern int alloc_fair_sched_group(struct task_group *tg, struct task_group *parent);
337 extern void online_fair_sched_group(struct task_group *tg);
338 extern void unregister_fair_sched_group(struct task_group *tg);
339 extern void init_tg_cfs_entry(struct task_group *tg, struct cfs_rq *cfs_rq,
340 struct sched_entity *se, int cpu,
341 struct sched_entity *parent);
342 extern void init_cfs_bandwidth(struct cfs_bandwidth *cfs_b);
344 extern void __refill_cfs_bandwidth_runtime(struct cfs_bandwidth *cfs_b);
345 extern void start_cfs_bandwidth(struct cfs_bandwidth *cfs_b);
346 extern void unthrottle_cfs_rq(struct cfs_rq *cfs_rq);
348 extern void free_rt_sched_group(struct task_group *tg);
349 extern int alloc_rt_sched_group(struct task_group *tg, struct task_group *parent);
350 extern void init_tg_rt_entry(struct task_group *tg, struct rt_rq *rt_rq,
351 struct sched_rt_entity *rt_se, int cpu,
352 struct sched_rt_entity *parent);
354 extern struct task_group *sched_create_group(struct task_group *parent);
355 extern void sched_online_group(struct task_group *tg,
356 struct task_group *parent);
357 extern void sched_destroy_group(struct task_group *tg);
358 extern void sched_offline_group(struct task_group *tg);
360 extern void sched_move_task(struct task_struct *tsk);
362 #ifdef CONFIG_FAIR_GROUP_SCHED
363 extern int sched_group_set_shares(struct task_group *tg, unsigned long shares);
365 #ifdef CONFIG_SMP
366 extern void set_task_rq_fair(struct sched_entity *se,
367 struct cfs_rq *prev, struct cfs_rq *next);
368 #else /* !CONFIG_SMP */
369 static inline void set_task_rq_fair(struct sched_entity *se,
370 struct cfs_rq *prev, struct cfs_rq *next) { }
371 #endif /* CONFIG_SMP */
372 #endif /* CONFIG_FAIR_GROUP_SCHED */
374 #else /* CONFIG_CGROUP_SCHED */
376 struct cfs_bandwidth { };
378 #endif /* CONFIG_CGROUP_SCHED */
380 /* CFS-related fields in a runqueue */
381 struct cfs_rq {
382 struct load_weight load;
383 unsigned int nr_running, h_nr_running;
385 u64 exec_clock;
386 u64 min_vruntime;
387 #ifndef CONFIG_64BIT
388 u64 min_vruntime_copy;
389 #endif
391 struct rb_root tasks_timeline;
392 struct rb_node *rb_leftmost;
395 * 'curr' points to currently running entity on this cfs_rq.
396 * It is set to NULL otherwise (i.e when none are currently running).
398 struct sched_entity *curr, *next, *last, *skip;
400 #ifdef CONFIG_SCHED_DEBUG
401 unsigned int nr_spread_over;
402 #endif
404 #ifdef CONFIG_SMP
406 * CFS load tracking
408 struct sched_avg avg;
409 u64 runnable_load_sum;
410 unsigned long runnable_load_avg;
411 #ifdef CONFIG_FAIR_GROUP_SCHED
412 unsigned long tg_load_avg_contrib;
413 #endif
414 atomic_long_t removed_load_avg, removed_util_avg;
415 #ifndef CONFIG_64BIT
416 u64 load_last_update_time_copy;
417 #endif
419 #ifdef CONFIG_FAIR_GROUP_SCHED
421 * h_load = weight * f(tg)
423 * Where f(tg) is the recursive weight fraction assigned to
424 * this group.
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.
443 int on_list;
444 struct list_head leaf_cfs_rq_list;
445 struct task_group *tg; /* group that "owns" this runqueue */
447 #ifdef CONFIG_CFS_BANDWIDTH
448 int runtime_enabled;
449 u64 runtime_expires;
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 #if defined(CONFIG_IRQ_WORK) && defined(CONFIG_SMP)
467 # define HAVE_RT_PUSH_IPI
468 #endif
470 /* Real-Time classes' related field in a runqueue: */
471 struct rt_rq {
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
476 struct {
477 int curr; /* highest queued rt task prio */
478 #ifdef CONFIG_SMP
479 int next; /* next highest */
480 #endif
481 } highest_prio;
482 #endif
483 #ifdef CONFIG_SMP
484 unsigned long rt_nr_migratory;
485 unsigned long rt_nr_total;
486 int overloaded;
487 struct plist_head pushable_tasks;
488 #endif /* CONFIG_SMP */
489 int rt_queued;
491 int rt_throttled;
492 u64 rt_time;
493 u64 rt_runtime;
494 /* Nests inside the rq lock: */
495 raw_spinlock_t rt_runtime_lock;
497 #ifdef CONFIG_RT_GROUP_SCHED
498 unsigned long rt_nr_boosted;
500 struct rq *rq;
501 struct task_group *tg;
502 #endif
505 /* Deadline class' related fields in a runqueue */
506 struct dl_rq {
507 /* runqueue is an rbtree, ordered by deadline */
508 struct rb_root rb_root;
509 struct rb_node *rb_leftmost;
511 unsigned long dl_nr_running;
513 #ifdef CONFIG_SMP
515 * Deadline values of the currently executing and the
516 * earliest ready task on this rq. Caching these facilitates
517 * the decision wether or not a ready but not running task
518 * should migrate somewhere else.
520 struct {
521 u64 curr;
522 u64 next;
523 } earliest_dl;
525 unsigned long dl_nr_migratory;
526 int overloaded;
529 * Tasks on this rq that can be pushed away. They are kept in
530 * an rb-tree, ordered by tasks' deadlines, with caching
531 * of the leftmost (earliest deadline) element.
533 struct rb_root pushable_dl_tasks_root;
534 struct rb_node *pushable_dl_tasks_leftmost;
535 #else
536 struct dl_bw dl_bw;
537 #endif
540 #ifdef CONFIG_SMP
543 * We add the notion of a root-domain which will be used to define per-domain
544 * variables. Each exclusive cpuset essentially defines an island domain by
545 * fully partitioning the member cpus from any other cpuset. Whenever a new
546 * exclusive cpuset is created, we also create and attach a new root-domain
547 * object.
550 struct root_domain {
551 atomic_t refcount;
552 atomic_t rto_count;
553 struct rcu_head rcu;
554 cpumask_var_t span;
555 cpumask_var_t online;
557 /* Indicate more than one runnable task for any CPU */
558 bool overload;
561 * The bit corresponding to a CPU gets set here if such CPU has more
562 * than one runnable -deadline task (as it is below for RT tasks).
564 cpumask_var_t dlo_mask;
565 atomic_t dlo_count;
566 struct dl_bw dl_bw;
567 struct cpudl cpudl;
569 #ifdef HAVE_RT_PUSH_IPI
571 * For IPI pull requests, loop across the rto_mask.
573 struct irq_work rto_push_work;
574 raw_spinlock_t rto_lock;
575 /* These are only updated and read within rto_lock */
576 int rto_loop;
577 int rto_cpu;
578 /* These atomics are updated outside of a lock */
579 atomic_t rto_loop_next;
580 atomic_t rto_loop_start;
581 #endif
583 * The "RT overload" flag: it gets set if a CPU has more than
584 * one runnable RT task.
586 cpumask_var_t rto_mask;
587 struct cpupri cpupri;
589 unsigned long max_cpu_capacity;
592 extern struct root_domain def_root_domain;
593 extern void sched_get_rd(struct root_domain *rd);
594 extern void sched_put_rd(struct root_domain *rd);
596 #ifdef HAVE_RT_PUSH_IPI
597 extern void rto_push_irq_work_func(struct irq_work *work);
598 #endif
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.
608 struct rq {
609 /* runqueue lock: */
610 raw_spinlock_t lock;
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;
620 #endif
621 #define CPU_LOAD_IDX_MAX 5
622 unsigned long cpu_load[CPU_LOAD_IDX_MAX];
623 #ifdef CONFIG_NO_HZ_COMMON
624 #ifdef CONFIG_SMP
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;
631 #endif
632 /* capture load from *all* tasks on this cpu: */
633 struct load_weight load;
634 unsigned long nr_load_updates;
635 u64 nr_switches;
637 struct cfs_rq cfs;
638 struct rt_rq rt;
639 struct dl_rq dl;
641 #ifdef CONFIG_FAIR_GROUP_SCHED
642 /* list of leaf cfs_rq on this cpu: */
643 struct list_head leaf_cfs_rq_list;
644 #endif /* CONFIG_FAIR_GROUP_SCHED */
647 * This is part of a global counter where only the total sum
648 * over all CPUs matters. A task can increase this counter on
649 * one CPU and if it got migrated afterwards it may decrease
650 * it on another CPU. Always updated under the runqueue lock:
652 unsigned long nr_uninterruptible;
654 struct task_struct *curr, *idle, *stop;
655 unsigned long next_balance;
656 struct mm_struct *prev_mm;
658 unsigned int clock_skip_update;
659 u64 clock;
660 u64 clock_task;
662 atomic_t nr_iowait;
664 #ifdef CONFIG_SMP
665 struct root_domain *rd;
666 struct sched_domain *sd;
668 unsigned long cpu_capacity;
669 unsigned long cpu_capacity_orig;
671 struct callback_head *balance_callback;
673 unsigned char idle_balance;
674 /* For active balancing */
675 int active_balance;
676 int push_cpu;
677 struct cpu_stop_work active_balance_work;
678 /* cpu of this runqueue: */
679 int cpu;
680 int online;
682 struct list_head cfs_tasks;
684 u64 rt_avg;
685 u64 age_stamp;
686 u64 idle_stamp;
687 u64 avg_idle;
689 /* This is used to determine avg_idle's max value */
690 u64 max_idle_balance_cost;
691 #endif
693 #ifdef CONFIG_IRQ_TIME_ACCOUNTING
694 u64 prev_irq_time;
695 #endif
696 #ifdef CONFIG_PARAVIRT
697 u64 prev_steal_time;
698 #endif
699 #ifdef CONFIG_PARAVIRT_TIME_ACCOUNTING
700 u64 prev_steal_time_rq;
701 #endif
703 /* calc_load related fields */
704 unsigned long calc_load_update;
705 long calc_load_active;
707 #ifdef CONFIG_SCHED_HRTICK
708 #ifdef CONFIG_SMP
709 int hrtick_csd_pending;
710 struct call_single_data hrtick_csd;
711 #endif
712 struct hrtimer hrtick_timer;
713 #endif
715 #ifdef CONFIG_SCHEDSTATS
716 /* latency stats */
717 struct sched_info rq_sched_info;
718 unsigned long long rq_cpu_time;
719 /* could above be rq->cfs_rq.exec_clock + rq->rt_rq.rt_runtime ? */
721 /* sys_sched_yield() stats */
722 unsigned int yld_count;
724 /* schedule() stats */
725 unsigned int sched_count;
726 unsigned int sched_goidle;
728 /* try_to_wake_up() stats */
729 unsigned int ttwu_count;
730 unsigned int ttwu_local;
731 #endif
733 #ifdef CONFIG_SMP
734 struct llist_head wake_list;
735 #endif
737 #ifdef CONFIG_CPU_IDLE
738 /* Must be inspected within a rcu lock section */
739 struct cpuidle_state *idle_state;
740 #endif
743 static inline int cpu_of(struct rq *rq)
745 #ifdef CONFIG_SMP
746 return rq->cpu;
747 #else
748 return 0;
749 #endif
752 DECLARE_PER_CPU_SHARED_ALIGNED(struct rq, runqueues);
754 #define cpu_rq(cpu) (&per_cpu(runqueues, (cpu)))
755 #define this_rq() this_cpu_ptr(&runqueues)
756 #define task_rq(p) cpu_rq(task_cpu(p))
757 #define cpu_curr(cpu) (cpu_rq(cpu)->curr)
758 #define raw_rq() raw_cpu_ptr(&runqueues)
760 static inline u64 __rq_clock_broken(struct rq *rq)
762 return READ_ONCE(rq->clock);
765 static inline u64 rq_clock(struct rq *rq)
767 lockdep_assert_held(&rq->lock);
768 return rq->clock;
771 static inline u64 rq_clock_task(struct rq *rq)
773 lockdep_assert_held(&rq->lock);
774 return rq->clock_task;
777 #define RQCF_REQ_SKIP 0x01
778 #define RQCF_ACT_SKIP 0x02
780 static inline void rq_clock_skip_update(struct rq *rq, bool skip)
782 lockdep_assert_held(&rq->lock);
783 if (skip)
784 rq->clock_skip_update |= RQCF_REQ_SKIP;
785 else
786 rq->clock_skip_update &= ~RQCF_REQ_SKIP;
789 #ifdef CONFIG_NUMA
790 enum numa_topology_type {
791 NUMA_DIRECT,
792 NUMA_GLUELESS_MESH,
793 NUMA_BACKPLANE,
795 extern enum numa_topology_type sched_numa_topology_type;
796 extern int sched_max_numa_distance;
797 extern bool find_numa_distance(int distance);
798 #endif
800 #ifdef CONFIG_NUMA_BALANCING
801 /* The regions in numa_faults array from task_struct */
802 enum numa_faults_stats {
803 NUMA_MEM = 0,
804 NUMA_CPU,
805 NUMA_MEMBUF,
806 NUMA_CPUBUF
808 extern void sched_setnuma(struct task_struct *p, int node);
809 extern int migrate_task_to(struct task_struct *p, int cpu);
810 extern int migrate_swap(struct task_struct *, struct task_struct *);
811 #endif /* CONFIG_NUMA_BALANCING */
813 #ifdef CONFIG_SMP
815 static inline void
816 queue_balance_callback(struct rq *rq,
817 struct callback_head *head,
818 void (*func)(struct rq *rq))
820 lockdep_assert_held(&rq->lock);
822 if (unlikely(head->next))
823 return;
825 head->func = (void (*)(struct callback_head *))func;
826 head->next = rq->balance_callback;
827 rq->balance_callback = head;
830 extern void sched_ttwu_pending(void);
832 #define rcu_dereference_check_sched_domain(p) \
833 rcu_dereference_check((p), \
834 lockdep_is_held(&sched_domains_mutex))
837 * The domain tree (rq->sd) is protected by RCU's quiescent state transition.
838 * See detach_destroy_domains: synchronize_sched for details.
840 * The domain tree of any CPU may only be accessed from within
841 * preempt-disabled sections.
843 #define for_each_domain(cpu, __sd) \
844 for (__sd = rcu_dereference_check_sched_domain(cpu_rq(cpu)->sd); \
845 __sd; __sd = __sd->parent)
847 #define for_each_lower_domain(sd) for (; sd; sd = sd->child)
850 * highest_flag_domain - Return highest sched_domain containing flag.
851 * @cpu: The cpu whose highest level of sched domain is to
852 * be returned.
853 * @flag: The flag to check for the highest sched_domain
854 * for the given cpu.
856 * Returns the highest sched_domain of a cpu which contains the given flag.
858 static inline struct sched_domain *highest_flag_domain(int cpu, int flag)
860 struct sched_domain *sd, *hsd = NULL;
862 for_each_domain(cpu, sd) {
863 if (!(sd->flags & flag))
864 break;
865 hsd = sd;
868 return hsd;
871 static inline struct sched_domain *lowest_flag_domain(int cpu, int flag)
873 struct sched_domain *sd;
875 for_each_domain(cpu, sd) {
876 if (sd->flags & flag)
877 break;
880 return sd;
883 DECLARE_PER_CPU(struct sched_domain *, sd_llc);
884 DECLARE_PER_CPU(int, sd_llc_size);
885 DECLARE_PER_CPU(int, sd_llc_id);
886 DECLARE_PER_CPU(struct sched_domain_shared *, sd_llc_shared);
887 DECLARE_PER_CPU(struct sched_domain *, sd_numa);
888 DECLARE_PER_CPU(struct sched_domain *, sd_asym);
890 struct sched_group_capacity {
891 atomic_t ref;
893 * CPU capacity of this group, SCHED_CAPACITY_SCALE being max capacity
894 * for a single CPU.
896 unsigned int capacity;
897 unsigned long next_update;
898 int imbalance; /* XXX unrelated to capacity but shared group state */
900 unsigned long cpumask[0]; /* iteration mask */
903 struct sched_group {
904 struct sched_group *next; /* Must be a circular list */
905 atomic_t ref;
907 unsigned int group_weight;
908 struct sched_group_capacity *sgc;
911 * The CPUs this group covers.
913 * NOTE: this field is variable length. (Allocated dynamically
914 * by attaching extra space to the end of the structure,
915 * depending on how many CPUs the kernel has booted up with)
917 unsigned long cpumask[0];
920 static inline struct cpumask *sched_group_cpus(struct sched_group *sg)
922 return to_cpumask(sg->cpumask);
926 * cpumask masking which cpus in the group are allowed to iterate up the domain
927 * tree.
929 static inline struct cpumask *sched_group_mask(struct sched_group *sg)
931 return to_cpumask(sg->sgc->cpumask);
935 * group_first_cpu - Returns the first cpu in the cpumask of a sched_group.
936 * @group: The group whose first cpu is to be returned.
938 static inline unsigned int group_first_cpu(struct sched_group *group)
940 return cpumask_first(sched_group_cpus(group));
943 extern int group_balance_cpu(struct sched_group *sg);
945 #if defined(CONFIG_SCHED_DEBUG) && defined(CONFIG_SYSCTL)
946 void register_sched_domain_sysctl(void);
947 void unregister_sched_domain_sysctl(void);
948 #else
949 static inline void register_sched_domain_sysctl(void)
952 static inline void unregister_sched_domain_sysctl(void)
955 #endif
957 #else
959 static inline void sched_ttwu_pending(void) { }
961 #endif /* CONFIG_SMP */
963 #include "stats.h"
964 #include "auto_group.h"
966 #ifdef CONFIG_CGROUP_SCHED
969 * Return the group to which this tasks belongs.
971 * We cannot use task_css() and friends because the cgroup subsystem
972 * changes that value before the cgroup_subsys::attach() method is called,
973 * therefore we cannot pin it and might observe the wrong value.
975 * The same is true for autogroup's p->signal->autogroup->tg, the autogroup
976 * core changes this before calling sched_move_task().
978 * Instead we use a 'copy' which is updated from sched_move_task() while
979 * holding both task_struct::pi_lock and rq::lock.
981 static inline struct task_group *task_group(struct task_struct *p)
983 return p->sched_task_group;
986 /* Change a task's cfs_rq and parent entity if it moves across CPUs/groups */
987 static inline void set_task_rq(struct task_struct *p, unsigned int cpu)
989 #if defined(CONFIG_FAIR_GROUP_SCHED) || defined(CONFIG_RT_GROUP_SCHED)
990 struct task_group *tg = task_group(p);
991 #endif
993 #ifdef CONFIG_FAIR_GROUP_SCHED
994 set_task_rq_fair(&p->se, p->se.cfs_rq, tg->cfs_rq[cpu]);
995 p->se.cfs_rq = tg->cfs_rq[cpu];
996 p->se.parent = tg->se[cpu];
997 #endif
999 #ifdef CONFIG_RT_GROUP_SCHED
1000 p->rt.rt_rq = tg->rt_rq[cpu];
1001 p->rt.parent = tg->rt_se[cpu];
1002 #endif
1005 #else /* CONFIG_CGROUP_SCHED */
1007 static inline void set_task_rq(struct task_struct *p, unsigned int cpu) { }
1008 static inline struct task_group *task_group(struct task_struct *p)
1010 return NULL;
1013 #endif /* CONFIG_CGROUP_SCHED */
1015 static inline void __set_task_cpu(struct task_struct *p, unsigned int cpu)
1017 set_task_rq(p, cpu);
1018 #ifdef CONFIG_SMP
1020 * After ->cpu is set up to a new value, task_rq_lock(p, ...) can be
1021 * successfuly executed on another CPU. We must ensure that updates of
1022 * per-task data have been completed by this moment.
1024 smp_wmb();
1025 #ifdef CONFIG_THREAD_INFO_IN_TASK
1026 p->cpu = cpu;
1027 #else
1028 task_thread_info(p)->cpu = cpu;
1029 #endif
1030 p->wake_cpu = cpu;
1031 #endif
1035 * Tunables that become constants when CONFIG_SCHED_DEBUG is off:
1037 #ifdef CONFIG_SCHED_DEBUG
1038 # include <linux/static_key.h>
1039 # define const_debug __read_mostly
1040 #else
1041 # define const_debug const
1042 #endif
1044 extern const_debug unsigned int sysctl_sched_features;
1046 #define SCHED_FEAT(name, enabled) \
1047 __SCHED_FEAT_##name ,
1049 enum {
1050 #include "features.h"
1051 __SCHED_FEAT_NR,
1054 #undef SCHED_FEAT
1056 #if defined(CONFIG_SCHED_DEBUG) && defined(HAVE_JUMP_LABEL)
1057 #define SCHED_FEAT(name, enabled) \
1058 static __always_inline bool static_branch_##name(struct static_key *key) \
1060 return static_key_##enabled(key); \
1063 #include "features.h"
1065 #undef SCHED_FEAT
1067 extern struct static_key sched_feat_keys[__SCHED_FEAT_NR];
1068 #define sched_feat(x) (static_branch_##x(&sched_feat_keys[__SCHED_FEAT_##x]))
1069 #else /* !(SCHED_DEBUG && HAVE_JUMP_LABEL) */
1070 #define sched_feat(x) (sysctl_sched_features & (1UL << __SCHED_FEAT_##x))
1071 #endif /* SCHED_DEBUG && HAVE_JUMP_LABEL */
1073 extern struct static_key_false sched_numa_balancing;
1074 extern struct static_key_false sched_schedstats;
1076 static inline u64 global_rt_period(void)
1078 return (u64)sysctl_sched_rt_period * NSEC_PER_USEC;
1081 static inline u64 global_rt_runtime(void)
1083 if (sysctl_sched_rt_runtime < 0)
1084 return RUNTIME_INF;
1086 return (u64)sysctl_sched_rt_runtime * NSEC_PER_USEC;
1089 static inline int task_current(struct rq *rq, struct task_struct *p)
1091 return rq->curr == p;
1094 static inline int task_running(struct rq *rq, struct task_struct *p)
1096 #ifdef CONFIG_SMP
1097 return p->on_cpu;
1098 #else
1099 return task_current(rq, p);
1100 #endif
1103 static inline int task_on_rq_queued(struct task_struct *p)
1105 return p->on_rq == TASK_ON_RQ_QUEUED;
1108 static inline int task_on_rq_migrating(struct task_struct *p)
1110 return p->on_rq == TASK_ON_RQ_MIGRATING;
1113 #ifndef prepare_arch_switch
1114 # define prepare_arch_switch(next) do { } while (0)
1115 #endif
1116 #ifndef finish_arch_post_lock_switch
1117 # define finish_arch_post_lock_switch() do { } while (0)
1118 #endif
1120 static inline void prepare_lock_switch(struct rq *rq, struct task_struct *next)
1122 #ifdef CONFIG_SMP
1124 * We can optimise this out completely for !SMP, because the
1125 * SMP rebalancing from interrupt is the only thing that cares
1126 * here.
1128 next->on_cpu = 1;
1129 #endif
1132 static inline void finish_lock_switch(struct rq *rq, struct task_struct *prev)
1134 #ifdef CONFIG_SMP
1136 * After ->on_cpu is cleared, the task can be moved to a different CPU.
1137 * We must ensure this doesn't happen until the switch is completely
1138 * finished.
1140 * In particular, the load of prev->state in finish_task_switch() must
1141 * happen before this.
1143 * Pairs with the smp_cond_load_acquire() in try_to_wake_up().
1145 smp_store_release(&prev->on_cpu, 0);
1146 #endif
1147 #ifdef CONFIG_DEBUG_SPINLOCK
1148 /* this is a valid case when another task releases the spinlock */
1149 rq->lock.owner = current;
1150 #endif
1152 * If we are tracking spinlock dependencies then we have to
1153 * fix up the runqueue lock - which gets 'carried over' from
1154 * prev into current:
1156 spin_acquire(&rq->lock.dep_map, 0, 0, _THIS_IP_);
1158 raw_spin_unlock_irq(&rq->lock);
1162 * wake flags
1164 #define WF_SYNC 0x01 /* waker goes to sleep after wakeup */
1165 #define WF_FORK 0x02 /* child wakeup after fork */
1166 #define WF_MIGRATED 0x4 /* internal use, task got migrated */
1169 * To aid in avoiding the subversion of "niceness" due to uneven distribution
1170 * of tasks with abnormal "nice" values across CPUs the contribution that
1171 * each task makes to its run queue's load is weighted according to its
1172 * scheduling class and "nice" value. For SCHED_NORMAL tasks this is just a
1173 * scaled version of the new time slice allocation that they receive on time
1174 * slice expiry etc.
1177 #define WEIGHT_IDLEPRIO 3
1178 #define WMULT_IDLEPRIO 1431655765
1180 extern const int sched_prio_to_weight[40];
1181 extern const u32 sched_prio_to_wmult[40];
1184 * {de,en}queue flags:
1186 * DEQUEUE_SLEEP - task is no longer runnable
1187 * ENQUEUE_WAKEUP - task just became runnable
1189 * SAVE/RESTORE - an otherwise spurious dequeue/enqueue, done to ensure tasks
1190 * are in a known state which allows modification. Such pairs
1191 * should preserve as much state as possible.
1193 * MOVE - paired with SAVE/RESTORE, explicitly does not preserve the location
1194 * in the runqueue.
1196 * ENQUEUE_HEAD - place at front of runqueue (tail if not specified)
1197 * ENQUEUE_REPLENISH - CBS (replenish runtime and postpone deadline)
1198 * ENQUEUE_MIGRATED - the task was migrated during wakeup
1202 #define DEQUEUE_SLEEP 0x01
1203 #define DEQUEUE_SAVE 0x02 /* matches ENQUEUE_RESTORE */
1204 #define DEQUEUE_MOVE 0x04 /* matches ENQUEUE_MOVE */
1206 #define ENQUEUE_WAKEUP 0x01
1207 #define ENQUEUE_RESTORE 0x02
1208 #define ENQUEUE_MOVE 0x04
1210 #define ENQUEUE_HEAD 0x08
1211 #define ENQUEUE_REPLENISH 0x10
1212 #ifdef CONFIG_SMP
1213 #define ENQUEUE_MIGRATED 0x20
1214 #else
1215 #define ENQUEUE_MIGRATED 0x00
1216 #endif
1218 #define RETRY_TASK ((void *)-1UL)
1220 struct sched_class {
1221 const struct sched_class *next;
1223 void (*enqueue_task) (struct rq *rq, struct task_struct *p, int flags);
1224 void (*dequeue_task) (struct rq *rq, struct task_struct *p, int flags);
1225 void (*yield_task) (struct rq *rq);
1226 bool (*yield_to_task) (struct rq *rq, struct task_struct *p, bool preempt);
1228 void (*check_preempt_curr) (struct rq *rq, struct task_struct *p, int flags);
1231 * It is the responsibility of the pick_next_task() method that will
1232 * return the next task to call put_prev_task() on the @prev task or
1233 * something equivalent.
1235 * May return RETRY_TASK when it finds a higher prio class has runnable
1236 * tasks.
1238 struct task_struct * (*pick_next_task) (struct rq *rq,
1239 struct task_struct *prev,
1240 struct pin_cookie cookie);
1241 void (*put_prev_task) (struct rq *rq, struct task_struct *p);
1243 #ifdef CONFIG_SMP
1244 int (*select_task_rq)(struct task_struct *p, int task_cpu, int sd_flag, int flags);
1245 void (*migrate_task_rq)(struct task_struct *p);
1247 void (*task_woken) (struct rq *this_rq, struct task_struct *task);
1249 void (*set_cpus_allowed)(struct task_struct *p,
1250 const struct cpumask *newmask);
1252 void (*rq_online)(struct rq *rq);
1253 void (*rq_offline)(struct rq *rq);
1254 #endif
1256 void (*set_curr_task) (struct rq *rq);
1257 void (*task_tick) (struct rq *rq, struct task_struct *p, int queued);
1258 void (*task_fork) (struct task_struct *p);
1259 void (*task_dead) (struct task_struct *p);
1262 * The switched_from() call is allowed to drop rq->lock, therefore we
1263 * cannot assume the switched_from/switched_to pair is serliazed by
1264 * rq->lock. They are however serialized by p->pi_lock.
1266 void (*switched_from) (struct rq *this_rq, struct task_struct *task);
1267 void (*switched_to) (struct rq *this_rq, struct task_struct *task);
1268 void (*prio_changed) (struct rq *this_rq, struct task_struct *task,
1269 int oldprio);
1271 unsigned int (*get_rr_interval) (struct rq *rq,
1272 struct task_struct *task);
1274 void (*update_curr) (struct rq *rq);
1276 #define TASK_SET_GROUP 0
1277 #define TASK_MOVE_GROUP 1
1279 #ifdef CONFIG_FAIR_GROUP_SCHED
1280 void (*task_change_group) (struct task_struct *p, int type);
1281 #endif
1284 static inline void put_prev_task(struct rq *rq, struct task_struct *prev)
1286 prev->sched_class->put_prev_task(rq, prev);
1289 static inline void set_curr_task(struct rq *rq, struct task_struct *curr)
1291 curr->sched_class->set_curr_task(rq);
1294 #define sched_class_highest (&stop_sched_class)
1295 #define for_each_class(class) \
1296 for (class = sched_class_highest; class; class = class->next)
1298 extern const struct sched_class stop_sched_class;
1299 extern const struct sched_class dl_sched_class;
1300 extern const struct sched_class rt_sched_class;
1301 extern const struct sched_class fair_sched_class;
1302 extern const struct sched_class idle_sched_class;
1305 #ifdef CONFIG_SMP
1307 extern void update_group_capacity(struct sched_domain *sd, int cpu);
1309 extern void trigger_load_balance(struct rq *rq);
1311 extern void set_cpus_allowed_common(struct task_struct *p, const struct cpumask *new_mask);
1313 #endif
1315 #ifdef CONFIG_CPU_IDLE
1316 static inline void idle_set_state(struct rq *rq,
1317 struct cpuidle_state *idle_state)
1319 rq->idle_state = idle_state;
1322 static inline struct cpuidle_state *idle_get_state(struct rq *rq)
1324 SCHED_WARN_ON(!rcu_read_lock_held());
1325 return rq->idle_state;
1327 #else
1328 static inline void idle_set_state(struct rq *rq,
1329 struct cpuidle_state *idle_state)
1333 static inline struct cpuidle_state *idle_get_state(struct rq *rq)
1335 return NULL;
1337 #endif
1339 extern void sysrq_sched_debug_show(void);
1340 extern void sched_init_granularity(void);
1341 extern void update_max_interval(void);
1343 extern void init_sched_dl_class(void);
1344 extern void init_sched_rt_class(void);
1345 extern void init_sched_fair_class(void);
1347 extern void resched_curr(struct rq *rq);
1348 extern void resched_cpu(int cpu);
1350 extern struct rt_bandwidth def_rt_bandwidth;
1351 extern void init_rt_bandwidth(struct rt_bandwidth *rt_b, u64 period, u64 runtime);
1353 extern struct dl_bandwidth def_dl_bandwidth;
1354 extern void init_dl_bandwidth(struct dl_bandwidth *dl_b, u64 period, u64 runtime);
1355 extern void init_dl_task_timer(struct sched_dl_entity *dl_se);
1357 unsigned long to_ratio(u64 period, u64 runtime);
1359 extern void init_entity_runnable_average(struct sched_entity *se);
1360 extern void post_init_entity_util_avg(struct sched_entity *se);
1362 #ifdef CONFIG_NO_HZ_FULL
1363 extern bool sched_can_stop_tick(struct rq *rq);
1366 * Tick may be needed by tasks in the runqueue depending on their policy and
1367 * requirements. If tick is needed, lets send the target an IPI to kick it out of
1368 * nohz mode if necessary.
1370 static inline void sched_update_tick_dependency(struct rq *rq)
1372 int cpu;
1374 if (!tick_nohz_full_enabled())
1375 return;
1377 cpu = cpu_of(rq);
1379 if (!tick_nohz_full_cpu(cpu))
1380 return;
1382 if (sched_can_stop_tick(rq))
1383 tick_nohz_dep_clear_cpu(cpu, TICK_DEP_BIT_SCHED);
1384 else
1385 tick_nohz_dep_set_cpu(cpu, TICK_DEP_BIT_SCHED);
1387 #else
1388 static inline void sched_update_tick_dependency(struct rq *rq) { }
1389 #endif
1391 static inline void add_nr_running(struct rq *rq, unsigned count)
1393 unsigned prev_nr = rq->nr_running;
1395 rq->nr_running = prev_nr + count;
1397 if (prev_nr < 2 && rq->nr_running >= 2) {
1398 #ifdef CONFIG_SMP
1399 if (!rq->rd->overload)
1400 rq->rd->overload = true;
1401 #endif
1404 sched_update_tick_dependency(rq);
1407 static inline void sub_nr_running(struct rq *rq, unsigned count)
1409 rq->nr_running -= count;
1410 /* Check if we still need preemption */
1411 sched_update_tick_dependency(rq);
1414 static inline void rq_last_tick_reset(struct rq *rq)
1416 #ifdef CONFIG_NO_HZ_FULL
1417 rq->last_sched_tick = jiffies;
1418 #endif
1421 extern void update_rq_clock(struct rq *rq);
1423 extern void activate_task(struct rq *rq, struct task_struct *p, int flags);
1424 extern void deactivate_task(struct rq *rq, struct task_struct *p, int flags);
1426 extern void check_preempt_curr(struct rq *rq, struct task_struct *p, int flags);
1428 extern const_debug unsigned int sysctl_sched_time_avg;
1429 extern const_debug unsigned int sysctl_sched_nr_migrate;
1430 extern const_debug unsigned int sysctl_sched_migration_cost;
1432 static inline u64 sched_avg_period(void)
1434 return (u64)sysctl_sched_time_avg * NSEC_PER_MSEC / 2;
1437 #ifdef CONFIG_SCHED_HRTICK
1440 * Use hrtick when:
1441 * - enabled by features
1442 * - hrtimer is actually high res
1444 static inline int hrtick_enabled(struct rq *rq)
1446 if (!sched_feat(HRTICK))
1447 return 0;
1448 if (!cpu_active(cpu_of(rq)))
1449 return 0;
1450 return hrtimer_is_hres_active(&rq->hrtick_timer);
1453 void hrtick_start(struct rq *rq, u64 delay);
1455 #else
1457 static inline int hrtick_enabled(struct rq *rq)
1459 return 0;
1462 #endif /* CONFIG_SCHED_HRTICK */
1464 #ifdef CONFIG_SMP
1465 extern void sched_avg_update(struct rq *rq);
1467 #ifndef arch_scale_freq_capacity
1468 static __always_inline
1469 unsigned long arch_scale_freq_capacity(struct sched_domain *sd, int cpu)
1471 return SCHED_CAPACITY_SCALE;
1473 #endif
1475 #ifndef arch_scale_cpu_capacity
1476 static __always_inline
1477 unsigned long arch_scale_cpu_capacity(struct sched_domain *sd, int cpu)
1479 if (sd && (sd->flags & SD_SHARE_CPUCAPACITY) && (sd->span_weight > 1))
1480 return sd->smt_gain / sd->span_weight;
1482 return SCHED_CAPACITY_SCALE;
1484 #endif
1486 static inline void sched_rt_avg_update(struct rq *rq, u64 rt_delta)
1488 rq->rt_avg += rt_delta * arch_scale_freq_capacity(NULL, cpu_of(rq));
1489 sched_avg_update(rq);
1491 #else
1492 static inline void sched_rt_avg_update(struct rq *rq, u64 rt_delta) { }
1493 static inline void sched_avg_update(struct rq *rq) { }
1494 #endif
1496 struct rq_flags {
1497 unsigned long flags;
1498 struct pin_cookie cookie;
1501 struct rq *__task_rq_lock(struct task_struct *p, struct rq_flags *rf)
1502 __acquires(rq->lock);
1503 struct rq *task_rq_lock(struct task_struct *p, struct rq_flags *rf)
1504 __acquires(p->pi_lock)
1505 __acquires(rq->lock);
1507 static inline void __task_rq_unlock(struct rq *rq, struct rq_flags *rf)
1508 __releases(rq->lock)
1510 lockdep_unpin_lock(&rq->lock, rf->cookie);
1511 raw_spin_unlock(&rq->lock);
1514 static inline void
1515 task_rq_unlock(struct rq *rq, struct task_struct *p, struct rq_flags *rf)
1516 __releases(rq->lock)
1517 __releases(p->pi_lock)
1519 lockdep_unpin_lock(&rq->lock, rf->cookie);
1520 raw_spin_unlock(&rq->lock);
1521 raw_spin_unlock_irqrestore(&p->pi_lock, rf->flags);
1524 #ifdef CONFIG_SMP
1525 #ifdef CONFIG_PREEMPT
1527 static inline void double_rq_lock(struct rq *rq1, struct rq *rq2);
1530 * fair double_lock_balance: Safely acquires both rq->locks in a fair
1531 * way at the expense of forcing extra atomic operations in all
1532 * invocations. This assures that the double_lock is acquired using the
1533 * same underlying policy as the spinlock_t on this architecture, which
1534 * reduces latency compared to the unfair variant below. However, it
1535 * also adds more overhead and therefore may reduce throughput.
1537 static inline int _double_lock_balance(struct rq *this_rq, struct rq *busiest)
1538 __releases(this_rq->lock)
1539 __acquires(busiest->lock)
1540 __acquires(this_rq->lock)
1542 raw_spin_unlock(&this_rq->lock);
1543 double_rq_lock(this_rq, busiest);
1545 return 1;
1548 #else
1550 * Unfair double_lock_balance: Optimizes throughput at the expense of
1551 * latency by eliminating extra atomic operations when the locks are
1552 * already in proper order on entry. This favors lower cpu-ids and will
1553 * grant the double lock to lower cpus over higher ids under contention,
1554 * regardless of entry order into the function.
1556 static inline int _double_lock_balance(struct rq *this_rq, struct rq *busiest)
1557 __releases(this_rq->lock)
1558 __acquires(busiest->lock)
1559 __acquires(this_rq->lock)
1561 int ret = 0;
1563 if (unlikely(!raw_spin_trylock(&busiest->lock))) {
1564 if (busiest < this_rq) {
1565 raw_spin_unlock(&this_rq->lock);
1566 raw_spin_lock(&busiest->lock);
1567 raw_spin_lock_nested(&this_rq->lock,
1568 SINGLE_DEPTH_NESTING);
1569 ret = 1;
1570 } else
1571 raw_spin_lock_nested(&busiest->lock,
1572 SINGLE_DEPTH_NESTING);
1574 return ret;
1577 #endif /* CONFIG_PREEMPT */
1580 * double_lock_balance - lock the busiest runqueue, this_rq is locked already.
1582 static inline int double_lock_balance(struct rq *this_rq, struct rq *busiest)
1584 if (unlikely(!irqs_disabled())) {
1585 /* printk() doesn't work good under rq->lock */
1586 raw_spin_unlock(&this_rq->lock);
1587 BUG_ON(1);
1590 return _double_lock_balance(this_rq, busiest);
1593 static inline void double_unlock_balance(struct rq *this_rq, struct rq *busiest)
1594 __releases(busiest->lock)
1596 raw_spin_unlock(&busiest->lock);
1597 lock_set_subclass(&this_rq->lock.dep_map, 0, _RET_IP_);
1600 static inline void double_lock(spinlock_t *l1, spinlock_t *l2)
1602 if (l1 > l2)
1603 swap(l1, l2);
1605 spin_lock(l1);
1606 spin_lock_nested(l2, SINGLE_DEPTH_NESTING);
1609 static inline void double_lock_irq(spinlock_t *l1, spinlock_t *l2)
1611 if (l1 > l2)
1612 swap(l1, l2);
1614 spin_lock_irq(l1);
1615 spin_lock_nested(l2, SINGLE_DEPTH_NESTING);
1618 static inline void double_raw_lock(raw_spinlock_t *l1, raw_spinlock_t *l2)
1620 if (l1 > l2)
1621 swap(l1, l2);
1623 raw_spin_lock(l1);
1624 raw_spin_lock_nested(l2, SINGLE_DEPTH_NESTING);
1628 * double_rq_lock - safely lock two runqueues
1630 * Note this does not disable interrupts like task_rq_lock,
1631 * you need to do so manually before calling.
1633 static inline void double_rq_lock(struct rq *rq1, struct rq *rq2)
1634 __acquires(rq1->lock)
1635 __acquires(rq2->lock)
1637 BUG_ON(!irqs_disabled());
1638 if (rq1 == rq2) {
1639 raw_spin_lock(&rq1->lock);
1640 __acquire(rq2->lock); /* Fake it out ;) */
1641 } else {
1642 if (rq1 < rq2) {
1643 raw_spin_lock(&rq1->lock);
1644 raw_spin_lock_nested(&rq2->lock, SINGLE_DEPTH_NESTING);
1645 } else {
1646 raw_spin_lock(&rq2->lock);
1647 raw_spin_lock_nested(&rq1->lock, SINGLE_DEPTH_NESTING);
1653 * double_rq_unlock - safely unlock two runqueues
1655 * Note this does not restore interrupts like task_rq_unlock,
1656 * you need to do so manually after calling.
1658 static inline void double_rq_unlock(struct rq *rq1, struct rq *rq2)
1659 __releases(rq1->lock)
1660 __releases(rq2->lock)
1662 raw_spin_unlock(&rq1->lock);
1663 if (rq1 != rq2)
1664 raw_spin_unlock(&rq2->lock);
1665 else
1666 __release(rq2->lock);
1669 #else /* CONFIG_SMP */
1672 * double_rq_lock - safely lock two runqueues
1674 * Note this does not disable interrupts like task_rq_lock,
1675 * you need to do so manually before calling.
1677 static inline void double_rq_lock(struct rq *rq1, struct rq *rq2)
1678 __acquires(rq1->lock)
1679 __acquires(rq2->lock)
1681 BUG_ON(!irqs_disabled());
1682 BUG_ON(rq1 != rq2);
1683 raw_spin_lock(&rq1->lock);
1684 __acquire(rq2->lock); /* Fake it out ;) */
1688 * double_rq_unlock - safely unlock two runqueues
1690 * Note this does not restore interrupts like task_rq_unlock,
1691 * you need to do so manually after calling.
1693 static inline void double_rq_unlock(struct rq *rq1, struct rq *rq2)
1694 __releases(rq1->lock)
1695 __releases(rq2->lock)
1697 BUG_ON(rq1 != rq2);
1698 raw_spin_unlock(&rq1->lock);
1699 __release(rq2->lock);
1702 #endif
1704 extern struct sched_entity *__pick_first_entity(struct cfs_rq *cfs_rq);
1705 extern struct sched_entity *__pick_last_entity(struct cfs_rq *cfs_rq);
1707 #ifdef CONFIG_SCHED_DEBUG
1708 extern void print_cfs_stats(struct seq_file *m, int cpu);
1709 extern void print_rt_stats(struct seq_file *m, int cpu);
1710 extern void print_dl_stats(struct seq_file *m, int cpu);
1711 extern void
1712 print_cfs_rq(struct seq_file *m, int cpu, struct cfs_rq *cfs_rq);
1714 #ifdef CONFIG_NUMA_BALANCING
1715 extern void
1716 show_numa_stats(struct task_struct *p, struct seq_file *m);
1717 extern void
1718 print_numa_stats(struct seq_file *m, int node, unsigned long tsf,
1719 unsigned long tpf, unsigned long gsf, unsigned long gpf);
1720 #endif /* CONFIG_NUMA_BALANCING */
1721 #endif /* CONFIG_SCHED_DEBUG */
1723 extern void init_cfs_rq(struct cfs_rq *cfs_rq);
1724 extern void init_rt_rq(struct rt_rq *rt_rq);
1725 extern void init_dl_rq(struct dl_rq *dl_rq);
1727 extern void cfs_bandwidth_usage_inc(void);
1728 extern void cfs_bandwidth_usage_dec(void);
1730 #ifdef CONFIG_NO_HZ_COMMON
1731 enum rq_nohz_flag_bits {
1732 NOHZ_TICK_STOPPED,
1733 NOHZ_BALANCE_KICK,
1736 #define nohz_flags(cpu) (&cpu_rq(cpu)->nohz_flags)
1738 extern void nohz_balance_exit_idle(unsigned int cpu);
1739 #else
1740 static inline void nohz_balance_exit_idle(unsigned int cpu) { }
1741 #endif
1743 #ifdef CONFIG_IRQ_TIME_ACCOUNTING
1744 struct irqtime {
1745 u64 hardirq_time;
1746 u64 softirq_time;
1747 u64 irq_start_time;
1748 struct u64_stats_sync sync;
1751 DECLARE_PER_CPU(struct irqtime, cpu_irqtime);
1753 static inline u64 irq_time_read(int cpu)
1755 struct irqtime *irqtime = &per_cpu(cpu_irqtime, cpu);
1756 unsigned int seq;
1757 u64 total;
1759 do {
1760 seq = __u64_stats_fetch_begin(&irqtime->sync);
1761 total = irqtime->softirq_time + irqtime->hardirq_time;
1762 } while (__u64_stats_fetch_retry(&irqtime->sync, seq));
1764 return total;
1766 #endif /* CONFIG_IRQ_TIME_ACCOUNTING */
1768 #ifdef CONFIG_CPU_FREQ
1769 DECLARE_PER_CPU(struct update_util_data *, cpufreq_update_util_data);
1772 * cpufreq_update_util - Take a note about CPU utilization changes.
1773 * @rq: Runqueue to carry out the update for.
1774 * @flags: Update reason flags.
1776 * This function is called by the scheduler on the CPU whose utilization is
1777 * being updated.
1779 * It can only be called from RCU-sched read-side critical sections.
1781 * The way cpufreq is currently arranged requires it to evaluate the CPU
1782 * performance state (frequency/voltage) on a regular basis to prevent it from
1783 * being stuck in a completely inadequate performance level for too long.
1784 * That is not guaranteed to happen if the updates are only triggered from CFS,
1785 * though, because they may not be coming in if RT or deadline tasks are active
1786 * all the time (or there are RT and DL tasks only).
1788 * As a workaround for that issue, this function is called by the RT and DL
1789 * sched classes to trigger extra cpufreq updates to prevent it from stalling,
1790 * but that really is a band-aid. Going forward it should be replaced with
1791 * solutions targeted more specifically at RT and DL tasks.
1793 static inline void cpufreq_update_util(struct rq *rq, unsigned int flags)
1795 struct update_util_data *data;
1797 data = rcu_dereference_sched(*this_cpu_ptr(&cpufreq_update_util_data));
1798 if (data)
1799 data->func(data, rq_clock(rq), flags);
1802 static inline void cpufreq_update_this_cpu(struct rq *rq, unsigned int flags)
1804 if (cpu_of(rq) == smp_processor_id())
1805 cpufreq_update_util(rq, flags);
1807 #else
1808 static inline void cpufreq_update_util(struct rq *rq, unsigned int flags) {}
1809 static inline void cpufreq_update_this_cpu(struct rq *rq, unsigned int flags) {}
1810 #endif /* CONFIG_CPU_FREQ */
1812 #ifdef arch_scale_freq_capacity
1813 #ifndef arch_scale_freq_invariant
1814 #define arch_scale_freq_invariant() (true)
1815 #endif
1816 #else /* arch_scale_freq_capacity */
1817 #define arch_scale_freq_invariant() (false)
1818 #endif