1
2#include <linux/sched.h>
3#include <linux/sched/sysctl.h>
4#include <linux/sched/rt.h>
5#include <linux/sched/deadline.h>
6#include <linux/mutex.h>
7#include <linux/spinlock.h>
8#include <linux/stop_machine.h>
9#include <linux/tick.h>
10#include <linux/slab.h>
11
12#include "cpupri.h"
13#include "cpudeadline.h"
14#include "cpuacct.h"
15
16struct rq;
17
18extern __read_mostly int scheduler_running;
19
20extern unsigned long calc_load_update;
21extern atomic_long_t calc_load_tasks;
22
23extern long calc_load_fold_active(struct rq *this_rq);
24extern void update_cpu_load_active(struct rq *this_rq);
25
26
27
28
29
30
31#define NICE_TO_PRIO(nice) (MAX_RT_PRIO + (nice) + 20)
32#define PRIO_TO_NICE(prio) ((prio) - MAX_RT_PRIO - 20)
33#define TASK_NICE(p) PRIO_TO_NICE((p)->static_prio)
34
35
36
37
38
39
40#define USER_PRIO(p) ((p)-MAX_RT_PRIO)
41#define TASK_USER_PRIO(p) USER_PRIO((p)->static_prio)
42#define MAX_USER_PRIO (USER_PRIO(MAX_PRIO))
43
44
45
46
47#define NS_TO_JIFFIES(TIME) ((unsigned long)(TIME) / (NSEC_PER_SEC / HZ))
48
49
50
51
52
53
54
55
56
57
58
59
60
61#if 0
62# define SCHED_LOAD_RESOLUTION 10
63# define scale_load(w) ((w) << SCHED_LOAD_RESOLUTION)
64# define scale_load_down(w) ((w) >> SCHED_LOAD_RESOLUTION)
65#else
66# define SCHED_LOAD_RESOLUTION 0
67# define scale_load(w) (w)
68# define scale_load_down(w) (w)
69#endif
70
71#define SCHED_LOAD_SHIFT (10 + SCHED_LOAD_RESOLUTION)
72#define SCHED_LOAD_SCALE (1L << SCHED_LOAD_SHIFT)
73
74#define NICE_0_LOAD SCHED_LOAD_SCALE
75#define NICE_0_SHIFT SCHED_LOAD_SHIFT
76
77
78
79
80
81
82#define DL_SCALE (10)
83
84
85
86
87
88
89
90
91#define RUNTIME_INF ((u64)~0ULL)
92
93static inline int fair_policy(int policy)
94{
95 return policy == SCHED_NORMAL || policy == SCHED_BATCH;
96}
97
98static inline int rt_policy(int policy)
99{
100 return policy == SCHED_FIFO || policy == SCHED_RR;
101}
102
103static inline int dl_policy(int policy)
104{
105 return policy == SCHED_DEADLINE;
106}
107
108static inline int task_has_rt_policy(struct task_struct *p)
109{
110 return rt_policy(p->policy);
111}
112
113static inline int task_has_dl_policy(struct task_struct *p)
114{
115 return dl_policy(p->policy);
116}
117
118static inline bool dl_time_before(u64 a, u64 b)
119{
120 return (s64)(a - b) < 0;
121}
122
123
124
125
126static inline bool
127dl_entity_preempt(struct sched_dl_entity *a, struct sched_dl_entity *b)
128{
129 return dl_time_before(a->deadline, b->deadline);
130}
131
132
133
134
135struct rt_prio_array {
136 DECLARE_BITMAP(bitmap, MAX_RT_PRIO+1);
137 struct list_head queue[MAX_RT_PRIO];
138};
139
140struct rt_bandwidth {
141
142 raw_spinlock_t rt_runtime_lock;
143 ktime_t rt_period;
144 u64 rt_runtime;
145 struct hrtimer rt_period_timer;
146};
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171struct dl_bandwidth {
172 raw_spinlock_t dl_runtime_lock;
173 u64 dl_runtime;
174 u64 dl_period;
175};
176
177static inline int dl_bandwidth_enabled(void)
178{
179 return sysctl_sched_rt_runtime >= 0;
180}
181
182extern struct dl_bw *dl_bw_of(int i);
183
184struct dl_bw {
185 raw_spinlock_t lock;
186 u64 bw, total_bw;
187};
188
189extern struct mutex sched_domains_mutex;
190
191#ifdef CONFIG_CGROUP_SCHED
192
193#include <linux/cgroup.h>
194
195struct cfs_rq;
196struct rt_rq;
197
198extern struct list_head task_groups;
199
200struct cfs_bandwidth {
201#ifdef CONFIG_CFS_BANDWIDTH
202 raw_spinlock_t lock;
203 ktime_t period;
204 u64 quota, runtime;
205 s64 hierarchal_quota;
206 u64 runtime_expires;
207
208 int idle, timer_active;
209 struct hrtimer period_timer, slack_timer;
210 struct list_head throttled_cfs_rq;
211
212
213 int nr_periods, nr_throttled;
214 u64 throttled_time;
215#endif
216};
217
218
219struct task_group {
220 struct cgroup_subsys_state css;
221
222#ifdef CONFIG_FAIR_GROUP_SCHED
223
224 struct sched_entity **se;
225
226 struct cfs_rq **cfs_rq;
227 unsigned long shares;
228
229#ifdef CONFIG_SMP
230 atomic_long_t load_avg;
231 atomic_t runnable_avg;
232#endif
233#endif
234
235#ifdef CONFIG_RT_GROUP_SCHED
236 struct sched_rt_entity **rt_se;
237 struct rt_rq **rt_rq;
238
239 struct rt_bandwidth rt_bandwidth;
240#endif
241
242 struct rcu_head rcu;
243 struct list_head list;
244
245 struct task_group *parent;
246 struct list_head siblings;
247 struct list_head children;
248
249#ifdef CONFIG_SCHED_AUTOGROUP
250 struct autogroup *autogroup;
251#endif
252
253 struct cfs_bandwidth cfs_bandwidth;
254};
255
256#ifdef CONFIG_FAIR_GROUP_SCHED
257#define ROOT_TASK_GROUP_LOAD NICE_0_LOAD
258
259
260
261
262
263
264
265
266
267#define MIN_SHARES (1UL << 1)
268#define MAX_SHARES (1UL << 18)
269#endif
270
271typedef int (*tg_visitor)(struct task_group *, void *);
272
273extern int walk_tg_tree_from(struct task_group *from,
274 tg_visitor down, tg_visitor up, void *data);
275
276
277
278
279
280
281
282static inline int walk_tg_tree(tg_visitor down, tg_visitor up, void *data)
283{
284 return walk_tg_tree_from(&root_task_group, down, up, data);
285}
286
287extern int tg_nop(struct task_group *tg, void *data);
288
289extern void free_fair_sched_group(struct task_group *tg);
290extern int alloc_fair_sched_group(struct task_group *tg, struct task_group *parent);
291extern void unregister_fair_sched_group(struct task_group *tg, int cpu);
292extern void init_tg_cfs_entry(struct task_group *tg, struct cfs_rq *cfs_rq,
293 struct sched_entity *se, int cpu,
294 struct sched_entity *parent);
295extern void init_cfs_bandwidth(struct cfs_bandwidth *cfs_b);
296extern int sched_group_set_shares(struct task_group *tg, unsigned long shares);
297
298extern void __refill_cfs_bandwidth_runtime(struct cfs_bandwidth *cfs_b);
299extern void __start_cfs_bandwidth(struct cfs_bandwidth *cfs_b);
300extern void unthrottle_cfs_rq(struct cfs_rq *cfs_rq);
301
302extern void free_rt_sched_group(struct task_group *tg);
303extern int alloc_rt_sched_group(struct task_group *tg, struct task_group *parent);
304extern void init_tg_rt_entry(struct task_group *tg, struct rt_rq *rt_rq,
305 struct sched_rt_entity *rt_se, int cpu,
306 struct sched_rt_entity *parent);
307
308extern struct task_group *sched_create_group(struct task_group *parent);
309extern void sched_online_group(struct task_group *tg,
310 struct task_group *parent);
311extern void sched_destroy_group(struct task_group *tg);
312extern void sched_offline_group(struct task_group *tg);
313
314extern void sched_move_task(struct task_struct *tsk);
315
316#ifdef CONFIG_FAIR_GROUP_SCHED
317extern int sched_group_set_shares(struct task_group *tg, unsigned long shares);
318#endif
319
320#else
321
322struct cfs_bandwidth { };
323
324#endif
325
326
327struct cfs_rq {
328 struct load_weight load;
329 unsigned int nr_running, h_nr_running;
330
331 u64 exec_clock;
332 u64 min_vruntime;
333#ifndef CONFIG_64BIT
334 u64 min_vruntime_copy;
335#endif
336
337 struct rb_root tasks_timeline;
338 struct rb_node *rb_leftmost;
339
340
341
342
343
344 struct sched_entity *curr, *next, *last, *skip;
345
346#ifdef CONFIG_SCHED_DEBUG
347 unsigned int nr_spread_over;
348#endif
349
350#ifdef CONFIG_SMP
351
352
353
354
355
356
357 unsigned long runnable_load_avg, blocked_load_avg;
358 atomic64_t decay_counter;
359 u64 last_decay;
360 atomic_long_t removed_load;
361
362#ifdef CONFIG_FAIR_GROUP_SCHED
363
364 u32 tg_runnable_contrib;
365 unsigned long tg_load_contrib;
366
367
368
369
370
371
372
373 unsigned long h_load;
374 u64 last_h_load_update;
375 struct sched_entity *h_load_next;
376#endif
377#endif
378
379#ifdef CONFIG_FAIR_GROUP_SCHED
380 struct rq *rq;
381
382
383
384
385
386
387
388
389
390 int on_list;
391 struct list_head leaf_cfs_rq_list;
392 struct task_group *tg;
393
394#ifdef CONFIG_CFS_BANDWIDTH
395 int runtime_enabled;
396 u64 runtime_expires;
397 s64 runtime_remaining;
398
399 u64 throttled_clock, throttled_clock_task;
400 u64 throttled_clock_task_time;
401 int throttled, throttle_count;
402 struct list_head throttled_list;
403#endif
404#endif
405};
406
407static inline int rt_bandwidth_enabled(void)
408{
409 return sysctl_sched_rt_runtime >= 0;
410}
411
412
413struct rt_rq {
414 struct rt_prio_array active;
415 unsigned int rt_nr_running;
416#if defined CONFIG_SMP || defined CONFIG_RT_GROUP_SCHED
417 struct {
418 int curr;
419#ifdef CONFIG_SMP
420 int next;
421#endif
422 } highest_prio;
423#endif
424#ifdef CONFIG_SMP
425 unsigned long rt_nr_migratory;
426 unsigned long rt_nr_total;
427 int overloaded;
428 struct plist_head pushable_tasks;
429#endif
430 int rt_throttled;
431 u64 rt_time;
432 u64 rt_runtime;
433
434 raw_spinlock_t rt_runtime_lock;
435
436#ifdef CONFIG_RT_GROUP_SCHED
437 unsigned long rt_nr_boosted;
438
439 struct rq *rq;
440 struct task_group *tg;
441#endif
442};
443
444
445struct dl_rq {
446
447 struct rb_root rb_root;
448 struct rb_node *rb_leftmost;
449
450 unsigned long dl_nr_running;
451
452#ifdef CONFIG_SMP
453
454
455
456
457
458
459 struct {
460 u64 curr;
461 u64 next;
462 } earliest_dl;
463
464 unsigned long dl_nr_migratory;
465 int overloaded;
466
467
468
469
470
471
472 struct rb_root pushable_dl_tasks_root;
473 struct rb_node *pushable_dl_tasks_leftmost;
474#else
475 struct dl_bw dl_bw;
476#endif
477};
478
479#ifdef CONFIG_SMP
480
481
482
483
484
485
486
487
488
489struct root_domain {
490 atomic_t refcount;
491 atomic_t rto_count;
492 struct rcu_head rcu;
493 cpumask_var_t span;
494 cpumask_var_t online;
495
496
497
498
499
500 cpumask_var_t dlo_mask;
501 atomic_t dlo_count;
502 struct dl_bw dl_bw;
503 struct cpudl cpudl;
504
505
506
507
508
509 cpumask_var_t rto_mask;
510 struct cpupri cpupri;
511};
512
513extern struct root_domain def_root_domain;
514
515#endif
516
517
518
519
520
521
522
523
524struct rq {
525
526 raw_spinlock_t lock;
527
528
529
530
531
532 unsigned int nr_running;
533#ifdef CONFIG_NUMA_BALANCING
534 unsigned int nr_numa_running;
535 unsigned int nr_preferred_running;
536#endif
537 #define CPU_LOAD_IDX_MAX 5
538 unsigned long cpu_load[CPU_LOAD_IDX_MAX];
539 unsigned long last_load_update_tick;
540#ifdef CONFIG_NO_HZ_COMMON
541 u64 nohz_stamp;
542 unsigned long nohz_flags;
543#endif
544#ifdef CONFIG_NO_HZ_FULL
545 unsigned long last_sched_tick;
546#endif
547 int skip_clock_update;
548
549
550 struct load_weight load;
551 unsigned long nr_load_updates;
552 u64 nr_switches;
553
554 struct cfs_rq cfs;
555 struct rt_rq rt;
556 struct dl_rq dl;
557
558#ifdef CONFIG_FAIR_GROUP_SCHED
559
560 struct list_head leaf_cfs_rq_list;
561#endif
562
563#ifdef CONFIG_RT_GROUP_SCHED
564 struct list_head leaf_rt_rq_list;
565#endif
566
567
568
569
570
571
572
573 unsigned long nr_uninterruptible;
574
575 struct task_struct *curr, *idle, *stop;
576 unsigned long next_balance;
577 struct mm_struct *prev_mm;
578
579 u64 clock;
580 u64 clock_task;
581
582 atomic_t nr_iowait;
583
584#ifdef CONFIG_SMP
585 struct root_domain *rd;
586 struct sched_domain *sd;
587
588 unsigned long cpu_power;
589
590 unsigned char idle_balance;
591
592 int post_schedule;
593 int active_balance;
594 int push_cpu;
595 struct cpu_stop_work active_balance_work;
596
597 int cpu;
598 int online;
599
600 struct list_head cfs_tasks;
601
602 u64 rt_avg;
603 u64 age_stamp;
604 u64 idle_stamp;
605 u64 avg_idle;
606
607
608 u64 max_idle_balance_cost;
609#endif
610
611#ifdef CONFIG_IRQ_TIME_ACCOUNTING
612 u64 prev_irq_time;
613#endif
614#ifdef CONFIG_PARAVIRT
615 u64 prev_steal_time;
616#endif
617#ifdef CONFIG_PARAVIRT_TIME_ACCOUNTING
618 u64 prev_steal_time_rq;
619#endif
620
621
622 unsigned long calc_load_update;
623 long calc_load_active;
624
625#ifdef CONFIG_SCHED_HRTICK
626#ifdef CONFIG_SMP
627 int hrtick_csd_pending;
628 struct call_single_data hrtick_csd;
629#endif
630 struct hrtimer hrtick_timer;
631#endif
632
633#ifdef CONFIG_SCHEDSTATS
634
635 struct sched_info rq_sched_info;
636 unsigned long long rq_cpu_time;
637
638
639
640 unsigned int yld_count;
641
642
643 unsigned int sched_count;
644 unsigned int sched_goidle;
645
646
647 unsigned int ttwu_count;
648 unsigned int ttwu_local;
649#endif
650
651#ifdef CONFIG_SMP
652 struct llist_head wake_list;
653#endif
654
655 struct sched_avg avg;
656};
657
658static inline int cpu_of(struct rq *rq)
659{
660#ifdef CONFIG_SMP
661 return rq->cpu;
662#else
663 return 0;
664#endif
665}
666
667DECLARE_PER_CPU(struct rq, runqueues);
668
669#define cpu_rq(cpu) (&per_cpu(runqueues, (cpu)))
670#define this_rq() (&__get_cpu_var(runqueues))
671#define task_rq(p) cpu_rq(task_cpu(p))
672#define cpu_curr(cpu) (cpu_rq(cpu)->curr)
673#define raw_rq() (&__raw_get_cpu_var(runqueues))
674
675static inline u64 rq_clock(struct rq *rq)
676{
677 return rq->clock;
678}
679
680static inline u64 rq_clock_task(struct rq *rq)
681{
682 return rq->clock_task;
683}
684
685#ifdef CONFIG_NUMA_BALANCING
686extern void sched_setnuma(struct task_struct *p, int node);
687extern int migrate_task_to(struct task_struct *p, int cpu);
688extern int migrate_swap(struct task_struct *, struct task_struct *);
689#endif
690
691#ifdef CONFIG_SMP
692
693#define rcu_dereference_check_sched_domain(p) \
694 rcu_dereference_check((p), \
695 lockdep_is_held(&sched_domains_mutex))
696
697
698
699
700
701
702
703
704#define for_each_domain(cpu, __sd) \
705 for (__sd = rcu_dereference_check_sched_domain(cpu_rq(cpu)->sd); \
706 __sd; __sd = __sd->parent)
707
708#define for_each_lower_domain(sd) for (; sd; sd = sd->child)
709
710
711
712
713
714
715
716
717
718
719static inline struct sched_domain *highest_flag_domain(int cpu, int flag)
720{
721 struct sched_domain *sd, *hsd = NULL;
722
723 for_each_domain(cpu, sd) {
724 if (!(sd->flags & flag))
725 break;
726 hsd = sd;
727 }
728
729 return hsd;
730}
731
732static inline struct sched_domain *lowest_flag_domain(int cpu, int flag)
733{
734 struct sched_domain *sd;
735
736 for_each_domain(cpu, sd) {
737 if (sd->flags & flag)
738 break;
739 }
740
741 return sd;
742}
743
744DECLARE_PER_CPU(struct sched_domain *, sd_llc);
745DECLARE_PER_CPU(int, sd_llc_size);
746DECLARE_PER_CPU(int, sd_llc_id);
747DECLARE_PER_CPU(struct sched_domain *, sd_numa);
748DECLARE_PER_CPU(struct sched_domain *, sd_busy);
749DECLARE_PER_CPU(struct sched_domain *, sd_asym);
750
751struct sched_group_power {
752 atomic_t ref;
753
754
755
756
757 unsigned int power, power_orig;
758 unsigned long next_update;
759 int imbalance;
760
761
762
763 atomic_t nr_busy_cpus;
764
765 unsigned long cpumask[0];
766};
767
768struct sched_group {
769 struct sched_group *next;
770 atomic_t ref;
771
772 unsigned int group_weight;
773 struct sched_group_power *sgp;
774
775
776
777
778
779
780
781
782 unsigned long cpumask[0];
783};
784
785static inline struct cpumask *sched_group_cpus(struct sched_group *sg)
786{
787 return to_cpumask(sg->cpumask);
788}
789
790
791
792
793
794static inline struct cpumask *sched_group_mask(struct sched_group *sg)
795{
796 return to_cpumask(sg->sgp->cpumask);
797}
798
799
800
801
802
803static inline unsigned int group_first_cpu(struct sched_group *group)
804{
805 return cpumask_first(sched_group_cpus(group));
806}
807
808extern int group_balance_cpu(struct sched_group *sg);
809
810#endif
811
812#include "stats.h"
813#include "auto_group.h"
814
815#ifdef CONFIG_CGROUP_SCHED
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830static inline struct task_group *task_group(struct task_struct *p)
831{
832 return p->sched_task_group;
833}
834
835
836static inline void set_task_rq(struct task_struct *p, unsigned int cpu)
837{
838#if defined(CONFIG_FAIR_GROUP_SCHED) || defined(CONFIG_RT_GROUP_SCHED)
839 struct task_group *tg = task_group(p);
840#endif
841
842#ifdef CONFIG_FAIR_GROUP_SCHED
843 p->se.cfs_rq = tg->cfs_rq[cpu];
844 p->se.parent = tg->se[cpu];
845#endif
846
847#ifdef CONFIG_RT_GROUP_SCHED
848 p->rt.rt_rq = tg->rt_rq[cpu];
849 p->rt.parent = tg->rt_se[cpu];
850#endif
851}
852
853#else
854
855static inline void set_task_rq(struct task_struct *p, unsigned int cpu) { }
856static inline struct task_group *task_group(struct task_struct *p)
857{
858 return NULL;
859}
860
861#endif
862
863static inline void __set_task_cpu(struct task_struct *p, unsigned int cpu)
864{
865 set_task_rq(p, cpu);
866#ifdef CONFIG_SMP
867
868
869
870
871
872 smp_wmb();
873 task_thread_info(p)->cpu = cpu;
874 p->wake_cpu = cpu;
875#endif
876}
877
878
879
880
881#ifdef CONFIG_SCHED_DEBUG
882# include <linux/static_key.h>
883# define const_debug __read_mostly
884#else
885# define const_debug const
886#endif
887
888extern const_debug unsigned int sysctl_sched_features;
889
890#define SCHED_FEAT(name, enabled) \
891 __SCHED_FEAT_##name ,
892
893enum {
894#include "features.h"
895 __SCHED_FEAT_NR,
896};
897
898#undef SCHED_FEAT
899
900#if defined(CONFIG_SCHED_DEBUG) && defined(HAVE_JUMP_LABEL)
901static __always_inline bool static_branch__true(struct static_key *key)
902{
903 return static_key_true(key);
904}
905
906static __always_inline bool static_branch__false(struct static_key *key)
907{
908 return static_key_false(key);
909}
910
911#define SCHED_FEAT(name, enabled) \
912static __always_inline bool static_branch_##name(struct static_key *key) \
913{ \
914 return static_branch__##enabled(key); \
915}
916
917#include "features.h"
918
919#undef SCHED_FEAT
920
921extern struct static_key sched_feat_keys[__SCHED_FEAT_NR];
922#define sched_feat(x) (static_branch_##x(&sched_feat_keys[__SCHED_FEAT_##x]))
923#else
924#define sched_feat(x) (sysctl_sched_features & (1UL << __SCHED_FEAT_##x))
925#endif
926
927#ifdef CONFIG_NUMA_BALANCING
928#define sched_feat_numa(x) sched_feat(x)
929#ifdef CONFIG_SCHED_DEBUG
930#define numabalancing_enabled sched_feat_numa(NUMA)
931#else
932extern bool numabalancing_enabled;
933#endif
934#else
935#define sched_feat_numa(x) (0)
936#define numabalancing_enabled (0)
937#endif
938
939static inline u64 global_rt_period(void)
940{
941 return (u64)sysctl_sched_rt_period * NSEC_PER_USEC;
942}
943
944static inline u64 global_rt_runtime(void)
945{
946 if (sysctl_sched_rt_runtime < 0)
947 return RUNTIME_INF;
948
949 return (u64)sysctl_sched_rt_runtime * NSEC_PER_USEC;
950}
951
952static inline int task_current(struct rq *rq, struct task_struct *p)
953{
954 return rq->curr == p;
955}
956
957static inline int task_running(struct rq *rq, struct task_struct *p)
958{
959#ifdef CONFIG_SMP
960 return p->on_cpu;
961#else
962 return task_current(rq, p);
963#endif
964}
965
966
967#ifndef prepare_arch_switch
968# define prepare_arch_switch(next) do { } while (0)
969#endif
970#ifndef finish_arch_switch
971# define finish_arch_switch(prev) do { } while (0)
972#endif
973#ifndef finish_arch_post_lock_switch
974# define finish_arch_post_lock_switch() do { } while (0)
975#endif
976
977#ifndef __ARCH_WANT_UNLOCKED_CTXSW
978static inline void prepare_lock_switch(struct rq *rq, struct task_struct *next)
979{
980#ifdef CONFIG_SMP
981
982
983
984
985
986 next->on_cpu = 1;
987#endif
988}
989
990static inline void finish_lock_switch(struct rq *rq, struct task_struct *prev)
991{
992#ifdef CONFIG_SMP
993
994
995
996
997
998 smp_wmb();
999 prev->on_cpu = 0;
1000#endif
1001#ifdef CONFIG_DEBUG_SPINLOCK
1002
1003 rq->lock.owner = current;
1004#endif
1005
1006
1007
1008
1009
1010 spin_acquire(&rq->lock.dep_map, 0, 0, _THIS_IP_);
1011
1012 raw_spin_unlock_irq(&rq->lock);
1013}
1014
1015#else
1016static inline void prepare_lock_switch(struct rq *rq, struct task_struct *next)
1017{
1018#ifdef CONFIG_SMP
1019
1020
1021
1022
1023
1024 next->on_cpu = 1;
1025#endif
1026 raw_spin_unlock(&rq->lock);
1027}
1028
1029static inline void finish_lock_switch(struct rq *rq, struct task_struct *prev)
1030{
1031#ifdef CONFIG_SMP
1032
1033
1034
1035
1036
1037 smp_wmb();
1038 prev->on_cpu = 0;
1039#endif
1040 local_irq_enable();
1041}
1042#endif
1043
1044
1045
1046
1047#define WF_SYNC 0x01
1048#define WF_FORK 0x02
1049#define WF_MIGRATED 0x4
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060#define WEIGHT_IDLEPRIO 3
1061#define WMULT_IDLEPRIO 1431655765
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075static const int prio_to_weight[40] = {
1076 88761, 71755, 56483, 46273, 36291,
1077 29154, 23254, 18705, 14949, 11916,
1078 9548, 7620, 6100, 4904, 3906,
1079 3121, 2501, 1991, 1586, 1277,
1080 1024, 820, 655, 526, 423,
1081 335, 272, 215, 172, 137,
1082 110, 87, 70, 56, 45,
1083 36, 29, 23, 18, 15,
1084};
1085
1086
1087
1088
1089
1090
1091
1092
1093static const u32 prio_to_wmult[40] = {
1094 48388, 59856, 76040, 92818, 118348,
1095 147320, 184698, 229616, 287308, 360437,
1096 449829, 563644, 704093, 875809, 1099582,
1097 1376151, 1717300, 2157191, 2708050, 3363326,
1098 4194304, 5237765, 6557202, 8165337, 10153587,
1099 12820798, 15790321, 19976592, 24970740, 31350126,
1100 39045157, 49367440, 61356676, 76695844, 95443717,
1101 119304647, 148102320, 186737708, 238609294, 286331153,
1102};
1103
1104#define ENQUEUE_WAKEUP 1
1105#define ENQUEUE_HEAD 2
1106#ifdef CONFIG_SMP
1107#define ENQUEUE_WAKING 4
1108#else
1109#define ENQUEUE_WAKING 0
1110#endif
1111#define ENQUEUE_REPLENISH 8
1112
1113#define DEQUEUE_SLEEP 1
1114
1115struct sched_class {
1116 const struct sched_class *next;
1117
1118 void (*enqueue_task) (struct rq *rq, struct task_struct *p, int flags);
1119 void (*dequeue_task) (struct rq *rq, struct task_struct *p, int flags);
1120 void (*yield_task) (struct rq *rq);
1121 bool (*yield_to_task) (struct rq *rq, struct task_struct *p, bool preempt);
1122
1123 void (*check_preempt_curr) (struct rq *rq, struct task_struct *p, int flags);
1124
1125 struct task_struct * (*pick_next_task) (struct rq *rq);
1126 void (*put_prev_task) (struct rq *rq, struct task_struct *p);
1127
1128#ifdef CONFIG_SMP
1129 int (*select_task_rq)(struct task_struct *p, int task_cpu, int sd_flag, int flags);
1130 void (*migrate_task_rq)(struct task_struct *p, int next_cpu);
1131
1132 void (*pre_schedule) (struct rq *this_rq, struct task_struct *task);
1133 void (*post_schedule) (struct rq *this_rq);
1134 void (*task_waking) (struct task_struct *task);
1135 void (*task_woken) (struct rq *this_rq, struct task_struct *task);
1136
1137 void (*set_cpus_allowed)(struct task_struct *p,
1138 const struct cpumask *newmask);
1139
1140 void (*rq_online)(struct rq *rq);
1141 void (*rq_offline)(struct rq *rq);
1142#endif
1143
1144 void (*set_curr_task) (struct rq *rq);
1145 void (*task_tick) (struct rq *rq, struct task_struct *p, int queued);
1146 void (*task_fork) (struct task_struct *p);
1147 void (*task_dead) (struct task_struct *p);
1148
1149 void (*switched_from) (struct rq *this_rq, struct task_struct *task);
1150 void (*switched_to) (struct rq *this_rq, struct task_struct *task);
1151 void (*prio_changed) (struct rq *this_rq, struct task_struct *task,
1152 int oldprio);
1153
1154 unsigned int (*get_rr_interval) (struct rq *rq,
1155 struct task_struct *task);
1156
1157#ifdef CONFIG_FAIR_GROUP_SCHED
1158 void (*task_move_group) (struct task_struct *p, int on_rq);
1159#endif
1160};
1161
1162#define sched_class_highest (&stop_sched_class)
1163#define for_each_class(class) \
1164 for (class = sched_class_highest; class; class = class->next)
1165
1166extern const struct sched_class stop_sched_class;
1167extern const struct sched_class dl_sched_class;
1168extern const struct sched_class rt_sched_class;
1169extern const struct sched_class fair_sched_class;
1170extern const struct sched_class idle_sched_class;
1171
1172
1173#ifdef CONFIG_SMP
1174
1175extern void update_group_power(struct sched_domain *sd, int cpu);
1176
1177extern void trigger_load_balance(struct rq *rq);
1178extern void idle_balance(int this_cpu, struct rq *this_rq);
1179
1180extern void idle_enter_fair(struct rq *this_rq);
1181extern void idle_exit_fair(struct rq *this_rq);
1182
1183#else
1184
1185static inline void idle_balance(int cpu, struct rq *rq)
1186{
1187}
1188
1189#endif
1190
1191extern void sysrq_sched_debug_show(void);
1192extern void sched_init_granularity(void);
1193extern void update_max_interval(void);
1194
1195extern void init_sched_dl_class(void);
1196extern void init_sched_rt_class(void);
1197extern void init_sched_fair_class(void);
1198extern void init_sched_dl_class(void);
1199
1200extern void resched_task(struct task_struct *p);
1201extern void resched_cpu(int cpu);
1202
1203extern struct rt_bandwidth def_rt_bandwidth;
1204extern void init_rt_bandwidth(struct rt_bandwidth *rt_b, u64 period, u64 runtime);
1205
1206extern struct dl_bandwidth def_dl_bandwidth;
1207extern void init_dl_bandwidth(struct dl_bandwidth *dl_b, u64 period, u64 runtime);
1208extern void init_dl_task_timer(struct sched_dl_entity *dl_se);
1209
1210unsigned long to_ratio(u64 period, u64 runtime);
1211
1212extern void update_idle_cpu_load(struct rq *this_rq);
1213
1214extern void init_task_runnable_average(struct task_struct *p);
1215
1216#ifdef CONFIG_PARAVIRT
1217static inline u64 steal_ticks(u64 steal)
1218{
1219 if (unlikely(steal > NSEC_PER_SEC))
1220 return div_u64(steal, TICK_NSEC);
1221
1222 return __iter_div_u64_rem(steal, TICK_NSEC, &steal);
1223}
1224#endif
1225
1226static inline void inc_nr_running(struct rq *rq)
1227{
1228 rq->nr_running++;
1229
1230#ifdef CONFIG_NO_HZ_FULL
1231 if (rq->nr_running == 2) {
1232 if (tick_nohz_full_cpu(rq->cpu)) {
1233
1234 smp_wmb();
1235 smp_send_reschedule(rq->cpu);
1236 }
1237 }
1238#endif
1239}
1240
1241static inline void dec_nr_running(struct rq *rq)
1242{
1243 rq->nr_running--;
1244}
1245
1246static inline void rq_last_tick_reset(struct rq *rq)
1247{
1248#ifdef CONFIG_NO_HZ_FULL
1249 rq->last_sched_tick = jiffies;
1250#endif
1251}
1252
1253extern void update_rq_clock(struct rq *rq);
1254
1255extern void activate_task(struct rq *rq, struct task_struct *p, int flags);
1256extern void deactivate_task(struct rq *rq, struct task_struct *p, int flags);
1257
1258extern void check_preempt_curr(struct rq *rq, struct task_struct *p, int flags);
1259
1260extern const_debug unsigned int sysctl_sched_time_avg;
1261extern const_debug unsigned int sysctl_sched_nr_migrate;
1262extern const_debug unsigned int sysctl_sched_migration_cost;
1263
1264static inline u64 sched_avg_period(void)
1265{
1266 return (u64)sysctl_sched_time_avg * NSEC_PER_MSEC / 2;
1267}
1268
1269#ifdef CONFIG_SCHED_HRTICK
1270
1271
1272
1273
1274
1275
1276static inline int hrtick_enabled(struct rq *rq)
1277{
1278 if (!sched_feat(HRTICK))
1279 return 0;
1280 if (!cpu_active(cpu_of(rq)))
1281 return 0;
1282 return hrtimer_is_hres_active(&rq->hrtick_timer);
1283}
1284
1285void hrtick_start(struct rq *rq, u64 delay);
1286
1287#else
1288
1289static inline int hrtick_enabled(struct rq *rq)
1290{
1291 return 0;
1292}
1293
1294#endif
1295
1296#ifdef CONFIG_SMP
1297extern void sched_avg_update(struct rq *rq);
1298static inline void sched_rt_avg_update(struct rq *rq, u64 rt_delta)
1299{
1300 rq->rt_avg += rt_delta;
1301 sched_avg_update(rq);
1302}
1303#else
1304static inline void sched_rt_avg_update(struct rq *rq, u64 rt_delta) { }
1305static inline void sched_avg_update(struct rq *rq) { }
1306#endif
1307
1308extern void start_bandwidth_timer(struct hrtimer *period_timer, ktime_t period);
1309
1310#ifdef CONFIG_SMP
1311#ifdef CONFIG_PREEMPT
1312
1313static inline void double_rq_lock(struct rq *rq1, struct rq *rq2);
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323static inline int _double_lock_balance(struct rq *this_rq, struct rq *busiest)
1324 __releases(this_rq->lock)
1325 __acquires(busiest->lock)
1326 __acquires(this_rq->lock)
1327{
1328 raw_spin_unlock(&this_rq->lock);
1329 double_rq_lock(this_rq, busiest);
1330
1331 return 1;
1332}
1333
1334#else
1335
1336
1337
1338
1339
1340
1341
1342static inline int _double_lock_balance(struct rq *this_rq, struct rq *busiest)
1343 __releases(this_rq->lock)
1344 __acquires(busiest->lock)
1345 __acquires(this_rq->lock)
1346{
1347 int ret = 0;
1348
1349 if (unlikely(!raw_spin_trylock(&busiest->lock))) {
1350 if (busiest < this_rq) {
1351 raw_spin_unlock(&this_rq->lock);
1352 raw_spin_lock(&busiest->lock);
1353 raw_spin_lock_nested(&this_rq->lock,
1354 SINGLE_DEPTH_NESTING);
1355 ret = 1;
1356 } else
1357 raw_spin_lock_nested(&busiest->lock,
1358 SINGLE_DEPTH_NESTING);
1359 }
1360 return ret;
1361}
1362
1363#endif
1364
1365
1366
1367
1368static inline int double_lock_balance(struct rq *this_rq, struct rq *busiest)
1369{
1370 if (unlikely(!irqs_disabled())) {
1371
1372 raw_spin_unlock(&this_rq->lock);
1373 BUG_ON(1);
1374 }
1375
1376 return _double_lock_balance(this_rq, busiest);
1377}
1378
1379static inline void double_unlock_balance(struct rq *this_rq, struct rq *busiest)
1380 __releases(busiest->lock)
1381{
1382 raw_spin_unlock(&busiest->lock);
1383 lock_set_subclass(&this_rq->lock.dep_map, 0, _RET_IP_);
1384}
1385
1386static inline void double_lock(spinlock_t *l1, spinlock_t *l2)
1387{
1388 if (l1 > l2)
1389 swap(l1, l2);
1390
1391 spin_lock(l1);
1392 spin_lock_nested(l2, SINGLE_DEPTH_NESTING);
1393}
1394
1395static inline void double_raw_lock(raw_spinlock_t *l1, raw_spinlock_t *l2)
1396{
1397 if (l1 > l2)
1398 swap(l1, l2);
1399
1400 raw_spin_lock(l1);
1401 raw_spin_lock_nested(l2, SINGLE_DEPTH_NESTING);
1402}
1403
1404
1405
1406
1407
1408
1409
1410static inline void double_rq_lock(struct rq *rq1, struct rq *rq2)
1411 __acquires(rq1->lock)
1412 __acquires(rq2->lock)
1413{
1414 BUG_ON(!irqs_disabled());
1415 if (rq1 == rq2) {
1416 raw_spin_lock(&rq1->lock);
1417 __acquire(rq2->lock);
1418 } else {
1419 if (rq1 < rq2) {
1420 raw_spin_lock(&rq1->lock);
1421 raw_spin_lock_nested(&rq2->lock, SINGLE_DEPTH_NESTING);
1422 } else {
1423 raw_spin_lock(&rq2->lock);
1424 raw_spin_lock_nested(&rq1->lock, SINGLE_DEPTH_NESTING);
1425 }
1426 }
1427}
1428
1429
1430
1431
1432
1433
1434
1435static inline void double_rq_unlock(struct rq *rq1, struct rq *rq2)
1436 __releases(rq1->lock)
1437 __releases(rq2->lock)
1438{
1439 raw_spin_unlock(&rq1->lock);
1440 if (rq1 != rq2)
1441 raw_spin_unlock(&rq2->lock);
1442 else
1443 __release(rq2->lock);
1444}
1445
1446#else
1447
1448
1449
1450
1451
1452
1453
1454static inline void double_rq_lock(struct rq *rq1, struct rq *rq2)
1455 __acquires(rq1->lock)
1456 __acquires(rq2->lock)
1457{
1458 BUG_ON(!irqs_disabled());
1459 BUG_ON(rq1 != rq2);
1460 raw_spin_lock(&rq1->lock);
1461 __acquire(rq2->lock);
1462}
1463
1464
1465
1466
1467
1468
1469
1470static inline void double_rq_unlock(struct rq *rq1, struct rq *rq2)
1471 __releases(rq1->lock)
1472 __releases(rq2->lock)
1473{
1474 BUG_ON(rq1 != rq2);
1475 raw_spin_unlock(&rq1->lock);
1476 __release(rq2->lock);
1477}
1478
1479#endif
1480
1481extern struct sched_entity *__pick_first_entity(struct cfs_rq *cfs_rq);
1482extern struct sched_entity *__pick_last_entity(struct cfs_rq *cfs_rq);
1483extern void print_cfs_stats(struct seq_file *m, int cpu);
1484extern void print_rt_stats(struct seq_file *m, int cpu);
1485
1486extern void init_cfs_rq(struct cfs_rq *cfs_rq);
1487extern void init_rt_rq(struct rt_rq *rt_rq, struct rq *rq);
1488extern void init_dl_rq(struct dl_rq *dl_rq, struct rq *rq);
1489
1490extern void cfs_bandwidth_usage_inc(void);
1491extern void cfs_bandwidth_usage_dec(void);
1492
1493#ifdef CONFIG_NO_HZ_COMMON
1494enum rq_nohz_flag_bits {
1495 NOHZ_TICK_STOPPED,
1496 NOHZ_BALANCE_KICK,
1497};
1498
1499#define nohz_flags(cpu) (&cpu_rq(cpu)->nohz_flags)
1500#endif
1501
1502#ifdef CONFIG_IRQ_TIME_ACCOUNTING
1503
1504DECLARE_PER_CPU(u64, cpu_hardirq_time);
1505DECLARE_PER_CPU(u64, cpu_softirq_time);
1506
1507#ifndef CONFIG_64BIT
1508DECLARE_PER_CPU(seqcount_t, irq_time_seq);
1509
1510static inline void irq_time_write_begin(void)
1511{
1512 __this_cpu_inc(irq_time_seq.sequence);
1513 smp_wmb();
1514}
1515
1516static inline void irq_time_write_end(void)
1517{
1518 smp_wmb();
1519 __this_cpu_inc(irq_time_seq.sequence);
1520}
1521
1522static inline u64 irq_time_read(int cpu)
1523{
1524 u64 irq_time;
1525 unsigned seq;
1526
1527 do {
1528 seq = read_seqcount_begin(&per_cpu(irq_time_seq, cpu));
1529 irq_time = per_cpu(cpu_softirq_time, cpu) +
1530 per_cpu(cpu_hardirq_time, cpu);
1531 } while (read_seqcount_retry(&per_cpu(irq_time_seq, cpu), seq));
1532
1533 return irq_time;
1534}
1535#else
1536static inline void irq_time_write_begin(void)
1537{
1538}
1539
1540static inline void irq_time_write_end(void)
1541{
1542}
1543
1544static inline u64 irq_time_read(int cpu)
1545{
1546 return per_cpu(cpu_softirq_time, cpu) + per_cpu(cpu_hardirq_time, cpu);
1547}
1548#endif
1549#endif
1550