sched.h 43 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641
  1. #include <linux/sched.h>
  2. #include <linux/sched/sysctl.h>
  3. #include <linux/sched/rt.h>
  4. #include <linux/sched/deadline.h>
  5. #include <linux/mutex.h>
  6. #include <linux/spinlock.h>
  7. #include <linux/stop_machine.h>
  8. #include <linux/tick.h>
  9. #include <linux/slab.h>
  10. #include "cpupri.h"
  11. #include "cpudeadline.h"
  12. #include "cpuacct.h"
  13. struct rq;
  14. struct cpuidle_state;
  15. /* task_struct::on_rq states: */
  16. #define TASK_ON_RQ_QUEUED 1
  17. #define TASK_ON_RQ_MIGRATING 2
  18. extern __read_mostly int scheduler_running;
  19. extern unsigned long calc_load_update;
  20. extern atomic_long_t calc_load_tasks;
  21. extern long calc_load_fold_active(struct rq *this_rq);
  22. extern void update_cpu_load_active(struct rq *this_rq);
  23. /*
  24. * Helpers for converting nanosecond timing to jiffy resolution
  25. */
  26. #define NS_TO_JIFFIES(TIME) ((unsigned long)(TIME) / (NSEC_PER_SEC / HZ))
  27. /*
  28. * Increase resolution of nice-level calculations for 64-bit architectures.
  29. * The extra resolution improves shares distribution and load balancing of
  30. * low-weight task groups (eg. nice +19 on an autogroup), deeper taskgroup
  31. * hierarchies, especially on larger systems. This is not a user-visible change
  32. * and does not change the user-interface for setting shares/weights.
  33. *
  34. * We increase resolution only if we have enough bits to allow this increased
  35. * resolution (i.e. BITS_PER_LONG > 32). The costs for increasing resolution
  36. * when BITS_PER_LONG <= 32 are pretty high and the returns do not justify the
  37. * increased costs.
  38. */
  39. #if 0 /* BITS_PER_LONG > 32 -- currently broken: it increases power usage under light load */
  40. # define SCHED_LOAD_RESOLUTION 10
  41. # define scale_load(w) ((w) << SCHED_LOAD_RESOLUTION)
  42. # define scale_load_down(w) ((w) >> SCHED_LOAD_RESOLUTION)
  43. #else
  44. # define SCHED_LOAD_RESOLUTION 0
  45. # define scale_load(w) (w)
  46. # define scale_load_down(w) (w)
  47. #endif
  48. #define SCHED_LOAD_SHIFT (10 + SCHED_LOAD_RESOLUTION)
  49. #define SCHED_LOAD_SCALE (1L << SCHED_LOAD_SHIFT)
  50. #define NICE_0_LOAD SCHED_LOAD_SCALE
  51. #define NICE_0_SHIFT SCHED_LOAD_SHIFT
  52. /*
  53. * Single value that decides SCHED_DEADLINE internal math precision.
  54. * 10 -> just above 1us
  55. * 9 -> just above 0.5us
  56. */
  57. #define DL_SCALE (10)
  58. /*
  59. * These are the 'tuning knobs' of the scheduler:
  60. */
  61. /*
  62. * single value that denotes runtime == period, ie unlimited time.
  63. */
  64. #define RUNTIME_INF ((u64)~0ULL)
  65. static inline int fair_policy(int policy)
  66. {
  67. return policy == SCHED_NORMAL || policy == SCHED_BATCH;
  68. }
  69. static inline int rt_policy(int policy)
  70. {
  71. return policy == SCHED_FIFO || policy == SCHED_RR;
  72. }
  73. static inline int dl_policy(int policy)
  74. {
  75. return policy == SCHED_DEADLINE;
  76. }
  77. static inline int task_has_rt_policy(struct task_struct *p)
  78. {
  79. return rt_policy(p->policy);
  80. }
  81. static inline int task_has_dl_policy(struct task_struct *p)
  82. {
  83. return dl_policy(p->policy);
  84. }
  85. static inline bool dl_time_before(u64 a, u64 b)
  86. {
  87. return (s64)(a - b) < 0;
  88. }
  89. /*
  90. * Tells if entity @a should preempt entity @b.
  91. */
  92. static inline bool
  93. dl_entity_preempt(struct sched_dl_entity *a, struct sched_dl_entity *b)
  94. {
  95. return dl_time_before(a->deadline, b->deadline);
  96. }
  97. /*
  98. * This is the priority-queue data structure of the RT scheduling class:
  99. */
  100. struct rt_prio_array {
  101. DECLARE_BITMAP(bitmap, MAX_RT_PRIO+1); /* include 1 bit for delimiter */
  102. struct list_head queue[MAX_RT_PRIO];
  103. };
  104. struct rt_bandwidth {
  105. /* nests inside the rq lock: */
  106. raw_spinlock_t rt_runtime_lock;
  107. ktime_t rt_period;
  108. u64 rt_runtime;
  109. struct hrtimer rt_period_timer;
  110. };
  111. void __dl_clear_params(struct task_struct *p);
  112. /*
  113. * To keep the bandwidth of -deadline tasks and groups under control
  114. * we need some place where:
  115. * - store the maximum -deadline bandwidth of the system (the group);
  116. * - cache the fraction of that bandwidth that is currently allocated.
  117. *
  118. * This is all done in the data structure below. It is similar to the
  119. * one used for RT-throttling (rt_bandwidth), with the main difference
  120. * that, since here we are only interested in admission control, we
  121. * do not decrease any runtime while the group "executes", neither we
  122. * need a timer to replenish it.
  123. *
  124. * With respect to SMP, the bandwidth is given on a per-CPU basis,
  125. * meaning that:
  126. * - dl_bw (< 100%) is the bandwidth of the system (group) on each CPU;
  127. * - dl_total_bw array contains, in the i-eth element, the currently
  128. * allocated bandwidth on the i-eth CPU.
  129. * Moreover, groups consume bandwidth on each CPU, while tasks only
  130. * consume bandwidth on the CPU they're running on.
  131. * Finally, dl_total_bw_cpu is used to cache the index of dl_total_bw
  132. * that will be shown the next time the proc or cgroup controls will
  133. * be red. It on its turn can be changed by writing on its own
  134. * control.
  135. */
  136. struct dl_bandwidth {
  137. raw_spinlock_t dl_runtime_lock;
  138. u64 dl_runtime;
  139. u64 dl_period;
  140. };
  141. static inline int dl_bandwidth_enabled(void)
  142. {
  143. return sysctl_sched_rt_runtime >= 0;
  144. }
  145. extern struct dl_bw *dl_bw_of(int i);
  146. struct dl_bw {
  147. raw_spinlock_t lock;
  148. u64 bw, total_bw;
  149. };
  150. extern struct mutex sched_domains_mutex;
  151. #ifdef CONFIG_CGROUP_SCHED
  152. #include <linux/cgroup.h>
  153. struct cfs_rq;
  154. struct rt_rq;
  155. extern struct list_head task_groups;
  156. struct cfs_bandwidth {
  157. #ifdef CONFIG_CFS_BANDWIDTH
  158. raw_spinlock_t lock;
  159. ktime_t period;
  160. u64 quota, runtime;
  161. s64 hierarchical_quota;
  162. u64 runtime_expires;
  163. int idle, timer_active;
  164. struct hrtimer period_timer, slack_timer;
  165. struct list_head throttled_cfs_rq;
  166. /* statistics */
  167. int nr_periods, nr_throttled;
  168. u64 throttled_time;
  169. #endif
  170. };
  171. /* task group related information */
  172. struct task_group {
  173. struct cgroup_subsys_state css;
  174. #ifdef CONFIG_FAIR_GROUP_SCHED
  175. /* schedulable entities of this group on each cpu */
  176. struct sched_entity **se;
  177. /* runqueue "owned" by this group on each cpu */
  178. struct cfs_rq **cfs_rq;
  179. unsigned long shares;
  180. #ifdef CONFIG_SMP
  181. atomic_long_t load_avg;
  182. atomic_t runnable_avg;
  183. #endif
  184. #endif
  185. #ifdef CONFIG_RT_GROUP_SCHED
  186. struct sched_rt_entity **rt_se;
  187. struct rt_rq **rt_rq;
  188. struct rt_bandwidth rt_bandwidth;
  189. #endif
  190. struct rcu_head rcu;
  191. struct list_head list;
  192. struct task_group *parent;
  193. struct list_head siblings;
  194. struct list_head children;
  195. #ifdef CONFIG_SCHED_AUTOGROUP
  196. struct autogroup *autogroup;
  197. #endif
  198. struct cfs_bandwidth cfs_bandwidth;
  199. };
  200. #ifdef CONFIG_FAIR_GROUP_SCHED
  201. #define ROOT_TASK_GROUP_LOAD NICE_0_LOAD
  202. /*
  203. * A weight of 0 or 1 can cause arithmetics problems.
  204. * A weight of a cfs_rq is the sum of weights of which entities
  205. * are queued on this cfs_rq, so a weight of a entity should not be
  206. * too large, so as the shares value of a task group.
  207. * (The default weight is 1024 - so there's no practical
  208. * limitation from this.)
  209. */
  210. #define MIN_SHARES (1UL << 1)
  211. #define MAX_SHARES (1UL << 18)
  212. #endif
  213. typedef int (*tg_visitor)(struct task_group *, void *);
  214. extern int walk_tg_tree_from(struct task_group *from,
  215. tg_visitor down, tg_visitor up, void *data);
  216. /*
  217. * Iterate the full tree, calling @down when first entering a node and @up when
  218. * leaving it for the final time.
  219. *
  220. * Caller must hold rcu_lock or sufficient equivalent.
  221. */
  222. static inline int walk_tg_tree(tg_visitor down, tg_visitor up, void *data)
  223. {
  224. return walk_tg_tree_from(&root_task_group, down, up, data);
  225. }
  226. extern int tg_nop(struct task_group *tg, void *data);
  227. extern void free_fair_sched_group(struct task_group *tg);
  228. extern int alloc_fair_sched_group(struct task_group *tg, struct task_group *parent);
  229. extern void unregister_fair_sched_group(struct task_group *tg, int cpu);
  230. extern void init_tg_cfs_entry(struct task_group *tg, struct cfs_rq *cfs_rq,
  231. struct sched_entity *se, int cpu,
  232. struct sched_entity *parent);
  233. extern void init_cfs_bandwidth(struct cfs_bandwidth *cfs_b);
  234. extern int sched_group_set_shares(struct task_group *tg, unsigned long shares);
  235. extern void __refill_cfs_bandwidth_runtime(struct cfs_bandwidth *cfs_b);
  236. extern void __start_cfs_bandwidth(struct cfs_bandwidth *cfs_b, bool force);
  237. extern void unthrottle_cfs_rq(struct cfs_rq *cfs_rq);
  238. extern void free_rt_sched_group(struct task_group *tg);
  239. extern int alloc_rt_sched_group(struct task_group *tg, struct task_group *parent);
  240. extern void init_tg_rt_entry(struct task_group *tg, struct rt_rq *rt_rq,
  241. struct sched_rt_entity *rt_se, int cpu,
  242. struct sched_rt_entity *parent);
  243. extern struct task_group *sched_create_group(struct task_group *parent);
  244. extern void sched_online_group(struct task_group *tg,
  245. struct task_group *parent);
  246. extern void sched_destroy_group(struct task_group *tg);
  247. extern void sched_offline_group(struct task_group *tg);
  248. extern void sched_move_task(struct task_struct *tsk);
  249. #ifdef CONFIG_FAIR_GROUP_SCHED
  250. extern int sched_group_set_shares(struct task_group *tg, unsigned long shares);
  251. #endif
  252. #else /* CONFIG_CGROUP_SCHED */
  253. struct cfs_bandwidth { };
  254. #endif /* CONFIG_CGROUP_SCHED */
  255. /* CFS-related fields in a runqueue */
  256. struct cfs_rq {
  257. struct load_weight load;
  258. unsigned int nr_running, h_nr_running;
  259. u64 exec_clock;
  260. u64 min_vruntime;
  261. #ifndef CONFIG_64BIT
  262. u64 min_vruntime_copy;
  263. #endif
  264. struct rb_root tasks_timeline;
  265. struct rb_node *rb_leftmost;
  266. /*
  267. * 'curr' points to currently running entity on this cfs_rq.
  268. * It is set to NULL otherwise (i.e when none are currently running).
  269. */
  270. struct sched_entity *curr, *next, *last, *skip;
  271. #ifdef CONFIG_SCHED_DEBUG
  272. unsigned int nr_spread_over;
  273. #endif
  274. #ifdef CONFIG_SMP
  275. /*
  276. * CFS Load tracking
  277. * Under CFS, load is tracked on a per-entity basis and aggregated up.
  278. * This allows for the description of both thread and group usage (in
  279. * the FAIR_GROUP_SCHED case).
  280. * runnable_load_avg is the sum of the load_avg_contrib of the
  281. * sched_entities on the rq.
  282. * blocked_load_avg is similar to runnable_load_avg except that its
  283. * the blocked sched_entities on the rq.
  284. * utilization_load_avg is the sum of the average running time of the
  285. * sched_entities on the rq.
  286. * utilization_blocked_avg is the utilization equivalent of
  287. * blocked_load_avg, i.e. the sum of running contributions of blocked
  288. * sched_entities associated with the rq.
  289. */
  290. unsigned long runnable_load_avg, blocked_load_avg;
  291. unsigned long utilization_load_avg, utilization_blocked_avg;
  292. atomic64_t decay_counter;
  293. u64 last_decay;
  294. atomic_long_t removed_load, removed_utilization;
  295. #ifdef CONFIG_FAIR_GROUP_SCHED
  296. /* Required to track per-cpu representation of a task_group */
  297. u32 tg_runnable_contrib;
  298. unsigned long tg_load_contrib;
  299. /*
  300. * h_load = weight * f(tg)
  301. *
  302. * Where f(tg) is the recursive weight fraction assigned to
  303. * this group.
  304. */
  305. unsigned long h_load;
  306. u64 last_h_load_update;
  307. struct sched_entity *h_load_next;
  308. #endif /* CONFIG_FAIR_GROUP_SCHED */
  309. #ifdef CONFIG_SCHED_HMP
  310. struct sched_avg avg;
  311. #endif
  312. #endif /* CONFIG_SMP */
  313. #ifdef CONFIG_FAIR_GROUP_SCHED
  314. struct rq *rq; /* cpu runqueue to which this cfs_rq is attached */
  315. /*
  316. * leaf cfs_rqs are those that hold tasks (lowest schedulable entity in
  317. * a hierarchy). Non-leaf lrqs hold other higher schedulable entities
  318. * (like users, containers etc.)
  319. *
  320. * leaf_cfs_rq_list ties together list of leaf cfs_rq's in a cpu. This
  321. * list is used during load balance.
  322. */
  323. int on_list;
  324. struct list_head leaf_cfs_rq_list;
  325. struct task_group *tg; /* group that "owns" this runqueue */
  326. #ifdef CONFIG_CFS_BANDWIDTH
  327. int runtime_enabled;
  328. u64 runtime_expires;
  329. s64 runtime_remaining;
  330. u64 throttled_clock, throttled_clock_task;
  331. u64 throttled_clock_task_time;
  332. int throttled, throttle_count;
  333. struct list_head throttled_list;
  334. #endif /* CONFIG_CFS_BANDWIDTH */
  335. #endif /* CONFIG_FAIR_GROUP_SCHED */
  336. };
  337. static inline int rt_bandwidth_enabled(void)
  338. {
  339. return sysctl_sched_rt_runtime >= 0;
  340. }
  341. /* Real-Time classes' related field in a runqueue: */
  342. struct rt_rq {
  343. struct rt_prio_array active;
  344. unsigned int rt_nr_running;
  345. #if defined CONFIG_SMP || defined CONFIG_RT_GROUP_SCHED
  346. struct {
  347. int curr; /* highest queued rt task prio */
  348. #ifdef CONFIG_SMP
  349. int next; /* next highest */
  350. #endif
  351. } highest_prio;
  352. #endif
  353. #ifdef CONFIG_SMP
  354. unsigned long rt_nr_migratory;
  355. unsigned long rt_nr_total;
  356. int overloaded;
  357. struct plist_head pushable_tasks;
  358. #endif
  359. int rt_queued;
  360. int rt_throttled;
  361. u64 rt_time;
  362. u64 rt_runtime;
  363. /* Nests inside the rq lock: */
  364. raw_spinlock_t rt_runtime_lock;
  365. #ifdef CONFIG_RT_GROUP_SCHED
  366. unsigned long rt_nr_boosted;
  367. struct rq *rq;
  368. struct task_group *tg;
  369. #endif
  370. };
  371. /* Deadline class' related fields in a runqueue */
  372. struct dl_rq {
  373. /* runqueue is an rbtree, ordered by deadline */
  374. struct rb_root rb_root;
  375. struct rb_node *rb_leftmost;
  376. unsigned long dl_nr_running;
  377. #ifdef CONFIG_SMP
  378. /*
  379. * Deadline values of the currently executing and the
  380. * earliest ready task on this rq. Caching these facilitates
  381. * the decision wether or not a ready but not running task
  382. * should migrate somewhere else.
  383. */
  384. struct {
  385. u64 curr;
  386. u64 next;
  387. } earliest_dl;
  388. unsigned long dl_nr_migratory;
  389. int overloaded;
  390. /*
  391. * Tasks on this rq that can be pushed away. They are kept in
  392. * an rb-tree, ordered by tasks' deadlines, with caching
  393. * of the leftmost (earliest deadline) element.
  394. */
  395. struct rb_root pushable_dl_tasks_root;
  396. struct rb_node *pushable_dl_tasks_leftmost;
  397. #else
  398. struct dl_bw dl_bw;
  399. #endif
  400. };
  401. #ifdef CONFIG_SMP
  402. /*
  403. * We add the notion of a root-domain which will be used to define per-domain
  404. * variables. Each exclusive cpuset essentially defines an island domain by
  405. * fully partitioning the member cpus from any other cpuset. Whenever a new
  406. * exclusive cpuset is created, we also create and attach a new root-domain
  407. * object.
  408. *
  409. */
  410. struct root_domain {
  411. atomic_t refcount;
  412. atomic_t rto_count;
  413. struct rcu_head rcu;
  414. cpumask_var_t span;
  415. cpumask_var_t online;
  416. /* Indicate more than one runnable task for any CPU */
  417. bool overload;
  418. /*
  419. * The bit corresponding to a CPU gets set here if such CPU has more
  420. * than one runnable -deadline task (as it is below for RT tasks).
  421. */
  422. cpumask_var_t dlo_mask;
  423. atomic_t dlo_count;
  424. struct dl_bw dl_bw;
  425. struct cpudl cpudl;
  426. /*
  427. * The "RT overload" flag: it gets set if a CPU has more than
  428. * one runnable RT task.
  429. */
  430. cpumask_var_t rto_mask;
  431. struct cpupri cpupri;
  432. };
  433. extern struct root_domain def_root_domain;
  434. #endif /* CONFIG_SMP */
  435. /*
  436. * This is the main, per-CPU runqueue data structure.
  437. *
  438. * Locking rule: those places that want to lock multiple runqueues
  439. * (such as the load balancing or the thread migration code), lock
  440. * acquire operations must be ordered by ascending &runqueue.
  441. */
  442. struct rq {
  443. /* runqueue lock: */
  444. raw_spinlock_t lock;
  445. /*
  446. * nr_running and cpu_load should be in the same cacheline because
  447. * remote CPUs use both these fields when doing load calculation.
  448. */
  449. unsigned int nr_running;
  450. #ifdef CONFIG_NUMA_BALANCING
  451. unsigned int nr_numa_running;
  452. unsigned int nr_preferred_running;
  453. #endif
  454. #define CPU_LOAD_IDX_MAX 5
  455. unsigned long cpu_load[CPU_LOAD_IDX_MAX];
  456. unsigned long last_load_update_tick;
  457. #ifdef CONFIG_NO_HZ_COMMON
  458. u64 nohz_stamp;
  459. unsigned long nohz_flags;
  460. #endif
  461. #ifdef CONFIG_NO_HZ_FULL
  462. unsigned long last_sched_tick;
  463. #endif
  464. int skip_clock_update;
  465. /* capture load from *all* tasks on this cpu: */
  466. struct load_weight load;
  467. unsigned long nr_load_updates;
  468. u64 nr_switches;
  469. struct cfs_rq cfs;
  470. struct rt_rq rt;
  471. struct dl_rq dl;
  472. #ifdef CONFIG_FAIR_GROUP_SCHED
  473. /* list of leaf cfs_rq on this cpu: */
  474. struct list_head leaf_cfs_rq_list;
  475. struct sched_avg avg;
  476. #endif /* CONFIG_FAIR_GROUP_SCHED */
  477. /*
  478. * This is part of a global counter where only the total sum
  479. * over all CPUs matters. A task can increase this counter on
  480. * one CPU and if it got migrated afterwards it may decrease
  481. * it on another CPU. Always updated under the runqueue lock:
  482. */
  483. unsigned long nr_uninterruptible;
  484. struct task_struct *curr, *idle, *stop;
  485. unsigned long next_balance;
  486. struct mm_struct *prev_mm;
  487. u64 clock;
  488. u64 clock_task;
  489. atomic_t nr_iowait;
  490. #ifdef CONFIG_SMP
  491. struct root_domain *rd;
  492. struct sched_domain *sd;
  493. unsigned long cpu_capacity;
  494. unsigned long cpu_capacity_orig;
  495. unsigned char idle_balance;
  496. /* For active balancing */
  497. int post_schedule;
  498. int active_balance;
  499. int push_cpu;
  500. struct cpu_stop_work active_balance_work;
  501. #ifdef CONFIG_SCHED_HMP
  502. struct task_struct *migrate_task;
  503. #endif
  504. /* cpu of this runqueue: */
  505. int cpu;
  506. int online;
  507. struct list_head cfs_tasks;
  508. u64 rt_avg;
  509. u64 age_stamp;
  510. u64 idle_stamp;
  511. u64 avg_idle;
  512. /* This is used to determine avg_idle's max value */
  513. u64 max_idle_balance_cost;
  514. #endif
  515. #ifdef CONFIG_IRQ_TIME_ACCOUNTING
  516. u64 prev_irq_time;
  517. #endif
  518. #ifdef CONFIG_PARAVIRT
  519. u64 prev_steal_time;
  520. #endif
  521. #ifdef CONFIG_PARAVIRT_TIME_ACCOUNTING
  522. u64 prev_steal_time_rq;
  523. #endif
  524. /* calc_load related fields */
  525. unsigned long calc_load_update;
  526. long calc_load_active;
  527. #ifdef CONFIG_SCHED_HRTICK
  528. #ifdef CONFIG_SMP
  529. int hrtick_csd_pending;
  530. struct call_single_data hrtick_csd;
  531. #endif
  532. struct hrtimer hrtick_timer;
  533. #endif
  534. #ifdef CONFIG_SCHEDSTATS
  535. /* latency stats */
  536. struct sched_info rq_sched_info;
  537. unsigned long long rq_cpu_time;
  538. /* could above be rq->cfs_rq.exec_clock + rq->rt_rq.rt_runtime ? */
  539. /* sys_sched_yield() stats */
  540. unsigned int yld_count;
  541. /* schedule() stats */
  542. unsigned int sched_count;
  543. unsigned int sched_goidle;
  544. /* try_to_wake_up() stats */
  545. unsigned int ttwu_count;
  546. unsigned int ttwu_local;
  547. #endif
  548. #ifdef CONFIG_SMP
  549. struct llist_head wake_list;
  550. #endif
  551. #ifdef CONFIG_CPU_IDLE
  552. /* Must be inspected within a rcu lock section */
  553. struct cpuidle_state *idle_state;
  554. #endif
  555. };
  556. static inline int cpu_of(struct rq *rq)
  557. {
  558. #ifdef CONFIG_SMP
  559. return rq->cpu;
  560. #else
  561. return 0;
  562. #endif
  563. }
  564. DECLARE_PER_CPU_SHARED_ALIGNED(struct rq, runqueues);
  565. #define cpu_rq(cpu) (&per_cpu(runqueues, (cpu)))
  566. #define this_rq() this_cpu_ptr(&runqueues)
  567. #define task_rq(p) cpu_rq(task_cpu(p))
  568. #define cpu_curr(cpu) (cpu_rq(cpu)->curr)
  569. #define raw_rq() raw_cpu_ptr(&runqueues)
  570. static inline u64 rq_clock(struct rq *rq)
  571. {
  572. return rq->clock;
  573. }
  574. static inline u64 rq_clock_task(struct rq *rq)
  575. {
  576. return rq->clock_task;
  577. }
  578. #ifdef CONFIG_NUMA_BALANCING
  579. extern void sched_setnuma(struct task_struct *p, int node);
  580. extern int migrate_task_to(struct task_struct *p, int cpu);
  581. extern int migrate_swap(struct task_struct *, struct task_struct *);
  582. #endif /* CONFIG_NUMA_BALANCING */
  583. #ifdef CONFIG_SMP
  584. extern void sched_ttwu_pending(void);
  585. #define rcu_dereference_check_sched_domain(p) \
  586. rcu_dereference_check((p), \
  587. lockdep_is_held(&sched_domains_mutex))
  588. /*
  589. * The domain tree (rq->sd) is protected by RCU's quiescent state transition.
  590. * See detach_destroy_domains: synchronize_sched for details.
  591. *
  592. * The domain tree of any CPU may only be accessed from within
  593. * preempt-disabled sections.
  594. */
  595. #define for_each_domain(cpu, __sd) \
  596. for (__sd = rcu_dereference_check_sched_domain(cpu_rq(cpu)->sd); \
  597. __sd; __sd = __sd->parent)
  598. #define for_each_lower_domain(sd) for (; sd; sd = sd->child)
  599. /**
  600. * highest_flag_domain - Return highest sched_domain containing flag.
  601. * @cpu: The cpu whose highest level of sched domain is to
  602. * be returned.
  603. * @flag: The flag to check for the highest sched_domain
  604. * for the given cpu.
  605. *
  606. * Returns the highest sched_domain of a cpu which contains the given flag.
  607. */
  608. static inline struct sched_domain *highest_flag_domain(int cpu, int flag)
  609. {
  610. struct sched_domain *sd, *hsd = NULL;
  611. for_each_domain(cpu, sd) {
  612. if (!(sd->flags & flag))
  613. break;
  614. hsd = sd;
  615. }
  616. return hsd;
  617. }
  618. static inline struct sched_domain *lowest_flag_domain(int cpu, int flag)
  619. {
  620. struct sched_domain *sd;
  621. for_each_domain(cpu, sd) {
  622. if (sd->flags & flag)
  623. break;
  624. }
  625. return sd;
  626. }
  627. DECLARE_PER_CPU(struct sched_domain *, sd_llc);
  628. DECLARE_PER_CPU(int, sd_llc_size);
  629. DECLARE_PER_CPU(int, sd_llc_id);
  630. DECLARE_PER_CPU(struct sched_domain *, sd_numa);
  631. DECLARE_PER_CPU(struct sched_domain *, sd_busy);
  632. DECLARE_PER_CPU(struct sched_domain *, sd_asym);
  633. struct sched_group_capacity {
  634. atomic_t ref;
  635. /*
  636. * CPU capacity of this group, SCHED_LOAD_SCALE being max capacity
  637. * for a single CPU.
  638. */
  639. unsigned int capacity;
  640. unsigned long next_update;
  641. int imbalance; /* XXX unrelated to capacity but shared group state */
  642. /*
  643. * Number of busy cpus in this group.
  644. */
  645. atomic_t nr_busy_cpus;
  646. unsigned long cpumask[0]; /* iteration mask */
  647. };
  648. struct sched_group {
  649. struct sched_group *next; /* Must be a circular list */
  650. atomic_t ref;
  651. unsigned int group_weight;
  652. struct sched_group_capacity *sgc;
  653. /*
  654. * The CPUs this group covers.
  655. *
  656. * NOTE: this field is variable length. (Allocated dynamically
  657. * by attaching extra space to the end of the structure,
  658. * depending on how many CPUs the kernel has booted up with)
  659. */
  660. unsigned long cpumask[0];
  661. };
  662. static inline struct cpumask *sched_group_cpus(struct sched_group *sg)
  663. {
  664. return to_cpumask(sg->cpumask);
  665. }
  666. /*
  667. * cpumask masking which cpus in the group are allowed to iterate up the domain
  668. * tree.
  669. */
  670. static inline struct cpumask *sched_group_mask(struct sched_group *sg)
  671. {
  672. return to_cpumask(sg->sgc->cpumask);
  673. }
  674. /**
  675. * group_first_cpu - Returns the first cpu in the cpumask of a sched_group.
  676. * @group: The group whose first cpu is to be returned.
  677. */
  678. static inline unsigned int group_first_cpu(struct sched_group *group)
  679. {
  680. return cpumask_first(sched_group_cpus(group));
  681. }
  682. extern int group_balance_cpu(struct sched_group *sg);
  683. #ifdef CONFIG_SCHED_HMP
  684. /* We need to know which cpus are fast and slow. */
  685. extern struct cpumask hmp_fast_cpu_mask;
  686. extern struct cpumask hmp_slow_cpu_mask;
  687. extern void __init arch_get_hmp_domains(struct list_head *hmp_domains_list);
  688. static LIST_HEAD(hmp_domains);
  689. DECLARE_PER_CPU(struct hmp_domain *, hmp_cpu_domain);
  690. #define hmp_cpu_domain(cpu) (per_cpu(hmp_cpu_domain, (cpu)))
  691. #endif
  692. #else
  693. static inline void sched_ttwu_pending(void) { }
  694. #endif /* CONFIG_SMP */
  695. #include "stats.h"
  696. #include "auto_group.h"
  697. #ifdef CONFIG_CGROUP_SCHED
  698. /*
  699. * Return the group to which this tasks belongs.
  700. *
  701. * We cannot use task_css() and friends because the cgroup subsystem
  702. * changes that value before the cgroup_subsys::attach() method is called,
  703. * therefore we cannot pin it and might observe the wrong value.
  704. *
  705. * The same is true for autogroup's p->signal->autogroup->tg, the autogroup
  706. * core changes this before calling sched_move_task().
  707. *
  708. * Instead we use a 'copy' which is updated from sched_move_task() while
  709. * holding both task_struct::pi_lock and rq::lock.
  710. */
  711. static inline struct task_group *task_group(struct task_struct *p)
  712. {
  713. return p->sched_task_group;
  714. }
  715. /* Change a task's cfs_rq and parent entity if it moves across CPUs/groups */
  716. static inline void set_task_rq(struct task_struct *p, unsigned int cpu)
  717. {
  718. #if defined(CONFIG_FAIR_GROUP_SCHED) || defined(CONFIG_RT_GROUP_SCHED)
  719. struct task_group *tg = task_group(p);
  720. #endif
  721. #ifdef CONFIG_FAIR_GROUP_SCHED
  722. p->se.cfs_rq = tg->cfs_rq[cpu];
  723. p->se.parent = tg->se[cpu];
  724. #endif
  725. #ifdef CONFIG_RT_GROUP_SCHED
  726. p->rt.rt_rq = tg->rt_rq[cpu];
  727. p->rt.parent = tg->rt_se[cpu];
  728. #endif
  729. }
  730. #else /* CONFIG_CGROUP_SCHED */
  731. static inline void set_task_rq(struct task_struct *p, unsigned int cpu) { }
  732. static inline struct task_group *task_group(struct task_struct *p)
  733. {
  734. return NULL;
  735. }
  736. #endif /* CONFIG_CGROUP_SCHED */
  737. static inline void __set_task_cpu(struct task_struct *p, unsigned int cpu)
  738. {
  739. set_task_rq(p, cpu);
  740. #ifdef CONFIG_SMP
  741. /*
  742. * After ->cpu is set up to a new value, task_rq_lock(p, ...) can be
  743. * successfuly executed on another CPU. We must ensure that updates of
  744. * per-task data have been completed by this moment.
  745. */
  746. smp_wmb();
  747. task_thread_info(p)->cpu = cpu;
  748. p->wake_cpu = cpu;
  749. #endif
  750. }
  751. /*
  752. * Tunables that become constants when CONFIG_SCHED_DEBUG is off:
  753. */
  754. #ifdef CONFIG_SCHED_DEBUG
  755. # include <linux/static_key.h>
  756. # define const_debug __read_mostly
  757. #else
  758. # define const_debug const
  759. #endif
  760. extern const_debug unsigned int sysctl_sched_features;
  761. #define SCHED_FEAT(name, enabled) \
  762. __SCHED_FEAT_##name ,
  763. enum {
  764. #include "features.h"
  765. __SCHED_FEAT_NR,
  766. };
  767. #undef SCHED_FEAT
  768. #if defined(CONFIG_SCHED_DEBUG) && defined(HAVE_JUMP_LABEL)
  769. #define SCHED_FEAT(name, enabled) \
  770. static __always_inline bool static_branch_##name(struct static_key *key) \
  771. { \
  772. return static_key_##enabled(key); \
  773. }
  774. #include "features.h"
  775. #undef SCHED_FEAT
  776. extern struct static_key sched_feat_keys[__SCHED_FEAT_NR];
  777. #define sched_feat(x) (static_branch_##x(&sched_feat_keys[__SCHED_FEAT_##x]))
  778. #else /* !(SCHED_DEBUG && HAVE_JUMP_LABEL) */
  779. #define sched_feat(x) (sysctl_sched_features & (1UL << __SCHED_FEAT_##x))
  780. #endif /* SCHED_DEBUG && HAVE_JUMP_LABEL */
  781. #ifdef CONFIG_NUMA_BALANCING
  782. #define sched_feat_numa(x) sched_feat(x)
  783. #ifdef CONFIG_SCHED_DEBUG
  784. #define numabalancing_enabled sched_feat_numa(NUMA)
  785. #else
  786. extern bool numabalancing_enabled;
  787. #endif /* CONFIG_SCHED_DEBUG */
  788. #else
  789. #define sched_feat_numa(x) (0)
  790. #define numabalancing_enabled (0)
  791. #endif /* CONFIG_NUMA_BALANCING */
  792. static inline u64 global_rt_period(void)
  793. {
  794. return (u64)sysctl_sched_rt_period * NSEC_PER_USEC;
  795. }
  796. static inline u64 global_rt_runtime(void)
  797. {
  798. if (sysctl_sched_rt_runtime < 0)
  799. return RUNTIME_INF;
  800. return (u64)sysctl_sched_rt_runtime * NSEC_PER_USEC;
  801. }
  802. static inline int task_current(struct rq *rq, struct task_struct *p)
  803. {
  804. return rq->curr == p;
  805. }
  806. static inline int task_running(struct rq *rq, struct task_struct *p)
  807. {
  808. #ifdef CONFIG_SMP
  809. return p->on_cpu;
  810. #else
  811. return task_current(rq, p);
  812. #endif
  813. }
  814. static inline int task_on_rq_queued(struct task_struct *p)
  815. {
  816. return p->on_rq == TASK_ON_RQ_QUEUED;
  817. }
  818. static inline int task_on_rq_migrating(struct task_struct *p)
  819. {
  820. return p->on_rq == TASK_ON_RQ_MIGRATING;
  821. }
  822. #ifndef prepare_arch_switch
  823. # define prepare_arch_switch(next) do { } while (0)
  824. #endif
  825. #ifndef finish_arch_switch
  826. # define finish_arch_switch(prev) do { } while (0)
  827. #endif
  828. #ifndef finish_arch_post_lock_switch
  829. # define finish_arch_post_lock_switch() do { } while (0)
  830. #endif
  831. static inline void prepare_lock_switch(struct rq *rq, struct task_struct *next)
  832. {
  833. #ifdef CONFIG_SMP
  834. /*
  835. * We can optimise this out completely for !SMP, because the
  836. * SMP rebalancing from interrupt is the only thing that cares
  837. * here.
  838. */
  839. next->on_cpu = 1;
  840. #endif
  841. }
  842. static inline void finish_lock_switch(struct rq *rq, struct task_struct *prev)
  843. {
  844. #ifdef CONFIG_SMP
  845. /*
  846. * After ->on_cpu is cleared, the task can be moved to a different CPU.
  847. * We must ensure this doesn't happen until the switch is completely
  848. * finished.
  849. */
  850. smp_wmb();
  851. prev->on_cpu = 0;
  852. #endif
  853. #ifdef CONFIG_DEBUG_SPINLOCK
  854. /* this is a valid case when another task releases the spinlock */
  855. rq->lock.owner = current;
  856. #endif
  857. /*
  858. * If we are tracking spinlock dependencies then we have to
  859. * fix up the runqueue lock - which gets 'carried over' from
  860. * prev into current:
  861. */
  862. spin_acquire(&rq->lock.dep_map, 0, 0, _THIS_IP_);
  863. raw_spin_unlock_irq(&rq->lock);
  864. }
  865. /*
  866. * wake flags
  867. */
  868. #define WF_SYNC 0x01 /* waker goes to sleep after wakeup */
  869. #define WF_FORK 0x02 /* child wakeup after fork */
  870. #define WF_MIGRATED 0x4 /* internal use, task got migrated */
  871. /*
  872. * To aid in avoiding the subversion of "niceness" due to uneven distribution
  873. * of tasks with abnormal "nice" values across CPUs the contribution that
  874. * each task makes to its run queue's load is weighted according to its
  875. * scheduling class and "nice" value. For SCHED_NORMAL tasks this is just a
  876. * scaled version of the new time slice allocation that they receive on time
  877. * slice expiry etc.
  878. */
  879. #define WEIGHT_IDLEPRIO 3
  880. #define WMULT_IDLEPRIO 1431655765
  881. /*
  882. * Nice levels are multiplicative, with a gentle 10% change for every
  883. * nice level changed. I.e. when a CPU-bound task goes from nice 0 to
  884. * nice 1, it will get ~10% less CPU time than another CPU-bound task
  885. * that remained on nice 0.
  886. *
  887. * The "10% effect" is relative and cumulative: from _any_ nice level,
  888. * if you go up 1 level, it's -10% CPU usage, if you go down 1 level
  889. * it's +10% CPU usage. (to achieve that we use a multiplier of 1.25.
  890. * If a task goes up by ~10% and another task goes down by ~10% then
  891. * the relative distance between them is ~25%.)
  892. */
  893. static const int prio_to_weight[40] = {
  894. /* -20 */ 88761, 71755, 56483, 46273, 36291,
  895. /* -15 */ 29154, 23254, 18705, 14949, 11916,
  896. /* -10 */ 9548, 7620, 6100, 4904, 3906,
  897. /* -5 */ 3121, 2501, 1991, 1586, 1277,
  898. /* 0 */ 1024, 820, 655, 526, 423,
  899. /* 5 */ 335, 272, 215, 172, 137,
  900. /* 10 */ 110, 87, 70, 56, 45,
  901. /* 15 */ 36, 29, 23, 18, 15,
  902. };
  903. /*
  904. * Inverse (2^32/x) values of the prio_to_weight[] array, precalculated.
  905. *
  906. * In cases where the weight does not change often, we can use the
  907. * precalculated inverse to speed up arithmetics by turning divisions
  908. * into multiplications:
  909. */
  910. static const u32 prio_to_wmult[40] = {
  911. /* -20 */ 48388, 59856, 76040, 92818, 118348,
  912. /* -15 */ 147320, 184698, 229616, 287308, 360437,
  913. /* -10 */ 449829, 563644, 704093, 875809, 1099582,
  914. /* -5 */ 1376151, 1717300, 2157191, 2708050, 3363326,
  915. /* 0 */ 4194304, 5237765, 6557202, 8165337, 10153587,
  916. /* 5 */ 12820798, 15790321, 19976592, 24970740, 31350126,
  917. /* 10 */ 39045157, 49367440, 61356676, 76695844, 95443717,
  918. /* 15 */ 119304647, 148102320, 186737708, 238609294, 286331153,
  919. };
  920. #define ENQUEUE_WAKEUP 1
  921. #define ENQUEUE_HEAD 2
  922. #ifdef CONFIG_SMP
  923. #define ENQUEUE_WAKING 4 /* sched_class::task_waking was called */
  924. #else
  925. #define ENQUEUE_WAKING 0
  926. #endif
  927. #define ENQUEUE_REPLENISH 8
  928. #define DEQUEUE_SLEEP 1
  929. #define RETRY_TASK ((void *)-1UL)
  930. struct sched_class {
  931. const struct sched_class *next;
  932. void (*enqueue_task) (struct rq *rq, struct task_struct *p, int flags);
  933. void (*dequeue_task) (struct rq *rq, struct task_struct *p, int flags);
  934. void (*yield_task) (struct rq *rq);
  935. bool (*yield_to_task) (struct rq *rq, struct task_struct *p, bool preempt);
  936. void (*check_preempt_curr) (struct rq *rq, struct task_struct *p, int flags);
  937. /*
  938. * It is the responsibility of the pick_next_task() method that will
  939. * return the next task to call put_prev_task() on the @prev task or
  940. * something equivalent.
  941. *
  942. * May return RETRY_TASK when it finds a higher prio class has runnable
  943. * tasks.
  944. */
  945. struct task_struct * (*pick_next_task) (struct rq *rq,
  946. struct task_struct *prev);
  947. void (*put_prev_task) (struct rq *rq, struct task_struct *p);
  948. #ifdef CONFIG_SMP
  949. int (*select_task_rq)(struct task_struct *p, int task_cpu, int sd_flag, int flags);
  950. void (*migrate_task_rq)(struct task_struct *p, int next_cpu);
  951. void (*post_schedule) (struct rq *this_rq);
  952. void (*task_waking) (struct task_struct *task);
  953. void (*task_woken) (struct rq *this_rq, struct task_struct *task);
  954. void (*set_cpus_allowed)(struct task_struct *p,
  955. const struct cpumask *newmask);
  956. void (*rq_online)(struct rq *rq);
  957. void (*rq_offline)(struct rq *rq);
  958. #endif
  959. void (*set_curr_task) (struct rq *rq);
  960. void (*task_tick) (struct rq *rq, struct task_struct *p, int queued);
  961. void (*task_fork) (struct task_struct *p);
  962. void (*task_dead) (struct task_struct *p);
  963. void (*switched_from) (struct rq *this_rq, struct task_struct *task);
  964. void (*switched_to) (struct rq *this_rq, struct task_struct *task);
  965. void (*prio_changed) (struct rq *this_rq, struct task_struct *task,
  966. int oldprio);
  967. unsigned int (*get_rr_interval) (struct rq *rq,
  968. struct task_struct *task);
  969. void (*update_curr) (struct rq *rq);
  970. #ifdef CONFIG_FAIR_GROUP_SCHED
  971. void (*task_move_group) (struct task_struct *p, int on_rq);
  972. #endif
  973. };
  974. static inline void put_prev_task(struct rq *rq, struct task_struct *prev)
  975. {
  976. prev->sched_class->put_prev_task(rq, prev);
  977. }
  978. #define sched_class_highest (&stop_sched_class)
  979. #define for_each_class(class) \
  980. for (class = sched_class_highest; class; class = class->next)
  981. extern const struct sched_class stop_sched_class;
  982. extern const struct sched_class dl_sched_class;
  983. extern const struct sched_class rt_sched_class;
  984. extern const struct sched_class fair_sched_class;
  985. extern const struct sched_class idle_sched_class;
  986. #ifdef CONFIG_SMP
  987. extern void update_group_capacity(struct sched_domain *sd, int cpu);
  988. extern void trigger_load_balance(struct rq *rq);
  989. extern void idle_enter_fair(struct rq *this_rq);
  990. extern void idle_exit_fair(struct rq *this_rq);
  991. #else
  992. static inline void idle_enter_fair(struct rq *rq) { }
  993. static inline void idle_exit_fair(struct rq *rq) { }
  994. #endif
  995. # ifdef CONFIG_MTK_SCHED_CMP_TGS
  996. extern int group_leader_is_empty(struct task_struct *p);
  997. # endif /* CONFIG_MTK_SCHED_CMP_TGS */
  998. # ifdef CONFIG_MTK_SCHED_CMP
  999. extern void get_cluster_cpus(struct cpumask *cpus, int cluster_id, bool exclusive_offline);
  1000. extern int get_cluster_id(unsigned int cpu);
  1001. # endif /* CONFIG_MTK_SCHED_CMP */
  1002. #ifdef CONFIG_CPU_IDLE
  1003. static inline void idle_set_state(struct rq *rq,
  1004. struct cpuidle_state *idle_state)
  1005. {
  1006. rq->idle_state = idle_state;
  1007. }
  1008. static inline struct cpuidle_state *idle_get_state(struct rq *rq)
  1009. {
  1010. WARN_ON(!rcu_read_lock_held());
  1011. return rq->idle_state;
  1012. }
  1013. #else
  1014. static inline void idle_set_state(struct rq *rq,
  1015. struct cpuidle_state *idle_state)
  1016. {
  1017. }
  1018. static inline struct cpuidle_state *idle_get_state(struct rq *rq)
  1019. {
  1020. return NULL;
  1021. }
  1022. #endif
  1023. extern void sched_init_granularity(void);
  1024. #ifdef CONFIG_HMP_PACK_SMALL_TASK
  1025. extern void update_packing_domain(int cpu);
  1026. #endif /* CONFIG_HMP_PACK_SMALL_TASK */
  1027. extern void update_max_interval(void);
  1028. extern void init_sched_dl_class(void);
  1029. extern void init_sched_rt_class(void);
  1030. extern void init_sched_fair_class(void);
  1031. extern void init_sched_dl_class(void);
  1032. extern void resched_curr(struct rq *rq);
  1033. extern void resched_cpu(int cpu);
  1034. extern struct rt_bandwidth def_rt_bandwidth;
  1035. extern void init_rt_bandwidth(struct rt_bandwidth *rt_b, u64 period, u64 runtime);
  1036. extern struct dl_bandwidth def_dl_bandwidth;
  1037. extern void init_dl_bandwidth(struct dl_bandwidth *dl_b, u64 period, u64 runtime);
  1038. extern void init_dl_task_timer(struct sched_dl_entity *dl_se);
  1039. unsigned long to_ratio(u64 period, u64 runtime);
  1040. extern void update_idle_cpu_load(struct rq *this_rq);
  1041. extern void init_task_runnable_average(struct task_struct *p);
  1042. #ifdef CONFIG_MTK_SCHED_RQAVG_KS
  1043. void sched_update_nr_prod(int cpu, unsigned long nr_running, int inc);
  1044. #endif
  1045. static inline void add_nr_running(struct rq *rq, unsigned count)
  1046. {
  1047. unsigned prev_nr = rq->nr_running;
  1048. #ifdef CONFIG_MTK_SCHED_RQAVG_KS
  1049. sched_update_nr_prod(cpu_of(rq), rq->nr_running, count);
  1050. #endif
  1051. rq->nr_running = prev_nr + count;
  1052. if (prev_nr < 2 && rq->nr_running >= 2) {
  1053. #ifdef CONFIG_SMP
  1054. if (!rq->rd->overload)
  1055. rq->rd->overload = true;
  1056. #endif
  1057. #ifdef CONFIG_NO_HZ_FULL
  1058. if (tick_nohz_full_cpu(rq->cpu)) {
  1059. /*
  1060. * Tick is needed if more than one task runs on a CPU.
  1061. * Send the target an IPI to kick it out of nohz mode.
  1062. *
  1063. * We assume that IPI implies full memory barrier and the
  1064. * new value of rq->nr_running is visible on reception
  1065. * from the target.
  1066. */
  1067. tick_nohz_full_kick_cpu(rq->cpu);
  1068. }
  1069. #endif
  1070. }
  1071. }
  1072. static inline void sub_nr_running(struct rq *rq, unsigned count)
  1073. {
  1074. #ifdef CONFIG_MTK_SCHED_RQAVG_KS
  1075. sched_update_nr_prod(cpu_of(rq), rq->nr_running, -count);
  1076. #endif
  1077. rq->nr_running -= count;
  1078. }
  1079. static inline void rq_last_tick_reset(struct rq *rq)
  1080. {
  1081. #ifdef CONFIG_NO_HZ_FULL
  1082. rq->last_sched_tick = jiffies;
  1083. #endif
  1084. }
  1085. extern void update_rq_clock(struct rq *rq);
  1086. extern void activate_task(struct rq *rq, struct task_struct *p, int flags);
  1087. extern void deactivate_task(struct rq *rq, struct task_struct *p, int flags);
  1088. extern void check_preempt_curr(struct rq *rq, struct task_struct *p, int flags);
  1089. extern const_debug unsigned int sysctl_sched_time_avg;
  1090. extern const_debug unsigned int sysctl_sched_nr_migrate;
  1091. extern const_debug unsigned int sysctl_sched_migration_cost;
  1092. static inline u64 sched_avg_period(void)
  1093. {
  1094. return (u64)sysctl_sched_time_avg * NSEC_PER_MSEC / 2;
  1095. }
  1096. #ifdef CONFIG_SCHED_HRTICK
  1097. /*
  1098. * Use hrtick when:
  1099. * - enabled by features
  1100. * - hrtimer is actually high res
  1101. */
  1102. static inline int hrtick_enabled(struct rq *rq)
  1103. {
  1104. if (!sched_feat(HRTICK))
  1105. return 0;
  1106. if (!cpu_active(cpu_of(rq)))
  1107. return 0;
  1108. return hrtimer_is_hres_active(&rq->hrtick_timer);
  1109. }
  1110. void hrtick_start(struct rq *rq, u64 delay);
  1111. #else
  1112. static inline int hrtick_enabled(struct rq *rq)
  1113. {
  1114. return 0;
  1115. }
  1116. #endif /* CONFIG_SCHED_HRTICK */
  1117. #ifdef CONFIG_SMP
  1118. extern void sched_avg_update(struct rq *rq);
  1119. extern unsigned long arch_scale_freq_capacity(struct sched_domain *sd, int cpu);
  1120. extern unsigned long arch_scale_cpu_capacity(struct sched_domain *sd, int cpu);
  1121. extern void arch_scale_set_curr_freq(int cpu, unsigned long freq);
  1122. extern void arch_scale_set_max_freq(int cpu, unsigned long freq);
  1123. static inline void sched_rt_avg_update(struct rq *rq, u64 rt_delta)
  1124. {
  1125. rq->rt_avg += rt_delta * arch_scale_freq_capacity(NULL, cpu_of(rq));
  1126. sched_avg_update(rq);
  1127. }
  1128. #else
  1129. static inline void sched_rt_avg_update(struct rq *rq, u64 rt_delta) { }
  1130. static inline void sched_avg_update(struct rq *rq) { }
  1131. #endif
  1132. extern void start_bandwidth_timer(struct hrtimer *period_timer, ktime_t period);
  1133. #ifdef CONFIG_SMP
  1134. #ifdef CONFIG_PREEMPT
  1135. static inline void double_rq_lock(struct rq *rq1, struct rq *rq2);
  1136. /*
  1137. * fair double_lock_balance: Safely acquires both rq->locks in a fair
  1138. * way at the expense of forcing extra atomic operations in all
  1139. * invocations. This assures that the double_lock is acquired using the
  1140. * same underlying policy as the spinlock_t on this architecture, which
  1141. * reduces latency compared to the unfair variant below. However, it
  1142. * also adds more overhead and therefore may reduce throughput.
  1143. */
  1144. static inline int _double_lock_balance(struct rq *this_rq, struct rq *busiest)
  1145. __releases(this_rq->lock)
  1146. __acquires(busiest->lock)
  1147. __acquires(this_rq->lock)
  1148. {
  1149. raw_spin_unlock(&this_rq->lock);
  1150. double_rq_lock(this_rq, busiest);
  1151. return 1;
  1152. }
  1153. #else
  1154. /*
  1155. * Unfair double_lock_balance: Optimizes throughput at the expense of
  1156. * latency by eliminating extra atomic operations when the locks are
  1157. * already in proper order on entry. This favors lower cpu-ids and will
  1158. * grant the double lock to lower cpus over higher ids under contention,
  1159. * regardless of entry order into the function.
  1160. */
  1161. static inline int _double_lock_balance(struct rq *this_rq, struct rq *busiest)
  1162. __releases(this_rq->lock)
  1163. __acquires(busiest->lock)
  1164. __acquires(this_rq->lock)
  1165. {
  1166. int ret = 0;
  1167. if (unlikely(!raw_spin_trylock(&busiest->lock))) {
  1168. if (busiest < this_rq) {
  1169. raw_spin_unlock(&this_rq->lock);
  1170. raw_spin_lock(&busiest->lock);
  1171. raw_spin_lock_nested(&this_rq->lock,
  1172. SINGLE_DEPTH_NESTING);
  1173. ret = 1;
  1174. } else
  1175. raw_spin_lock_nested(&busiest->lock,
  1176. SINGLE_DEPTH_NESTING);
  1177. }
  1178. return ret;
  1179. }
  1180. #endif /* CONFIG_PREEMPT */
  1181. /*
  1182. * double_lock_balance - lock the busiest runqueue, this_rq is locked already.
  1183. */
  1184. static inline int double_lock_balance(struct rq *this_rq, struct rq *busiest)
  1185. {
  1186. if (unlikely(!irqs_disabled())) {
  1187. /* printk() doesn't work good under rq->lock */
  1188. raw_spin_unlock(&this_rq->lock);
  1189. BUG_ON(1);
  1190. }
  1191. return _double_lock_balance(this_rq, busiest);
  1192. }
  1193. static inline void double_unlock_balance(struct rq *this_rq, struct rq *busiest)
  1194. __releases(busiest->lock)
  1195. {
  1196. raw_spin_unlock(&busiest->lock);
  1197. lock_set_subclass(&this_rq->lock.dep_map, 0, _RET_IP_);
  1198. }
  1199. static inline void double_lock(spinlock_t *l1, spinlock_t *l2)
  1200. {
  1201. if (l1 > l2)
  1202. swap(l1, l2);
  1203. spin_lock(l1);
  1204. spin_lock_nested(l2, SINGLE_DEPTH_NESTING);
  1205. }
  1206. static inline void double_lock_irq(spinlock_t *l1, spinlock_t *l2)
  1207. {
  1208. if (l1 > l2)
  1209. swap(l1, l2);
  1210. spin_lock_irq(l1);
  1211. spin_lock_nested(l2, SINGLE_DEPTH_NESTING);
  1212. }
  1213. static inline void double_raw_lock(raw_spinlock_t *l1, raw_spinlock_t *l2)
  1214. {
  1215. if (l1 > l2)
  1216. swap(l1, l2);
  1217. raw_spin_lock(l1);
  1218. raw_spin_lock_nested(l2, SINGLE_DEPTH_NESTING);
  1219. }
  1220. /*
  1221. * double_rq_lock - safely lock two runqueues
  1222. *
  1223. * Note this does not disable interrupts like task_rq_lock,
  1224. * you need to do so manually before calling.
  1225. */
  1226. static inline void double_rq_lock(struct rq *rq1, struct rq *rq2)
  1227. __acquires(rq1->lock)
  1228. __acquires(rq2->lock)
  1229. {
  1230. BUG_ON(!irqs_disabled());
  1231. if (rq1 == rq2) {
  1232. raw_spin_lock(&rq1->lock);
  1233. __acquire(rq2->lock); /* Fake it out ;) */
  1234. } else {
  1235. if (rq1 < rq2) {
  1236. raw_spin_lock(&rq1->lock);
  1237. raw_spin_lock_nested(&rq2->lock, SINGLE_DEPTH_NESTING);
  1238. } else {
  1239. raw_spin_lock(&rq2->lock);
  1240. raw_spin_lock_nested(&rq1->lock, SINGLE_DEPTH_NESTING);
  1241. }
  1242. }
  1243. }
  1244. /*
  1245. * double_rq_unlock - safely unlock two runqueues
  1246. *
  1247. * Note this does not restore interrupts like task_rq_unlock,
  1248. * you need to do so manually after calling.
  1249. */
  1250. static inline void double_rq_unlock(struct rq *rq1, struct rq *rq2)
  1251. __releases(rq1->lock)
  1252. __releases(rq2->lock)
  1253. {
  1254. raw_spin_unlock(&rq1->lock);
  1255. if (rq1 != rq2)
  1256. raw_spin_unlock(&rq2->lock);
  1257. else
  1258. __release(rq2->lock);
  1259. }
  1260. #else /* CONFIG_SMP */
  1261. /*
  1262. * double_rq_lock - safely lock two runqueues
  1263. *
  1264. * Note this does not disable interrupts like task_rq_lock,
  1265. * you need to do so manually before calling.
  1266. */
  1267. static inline void double_rq_lock(struct rq *rq1, struct rq *rq2)
  1268. __acquires(rq1->lock)
  1269. __acquires(rq2->lock)
  1270. {
  1271. BUG_ON(!irqs_disabled());
  1272. BUG_ON(rq1 != rq2);
  1273. raw_spin_lock(&rq1->lock);
  1274. __acquire(rq2->lock); /* Fake it out ;) */
  1275. }
  1276. /*
  1277. * double_rq_unlock - safely unlock two runqueues
  1278. *
  1279. * Note this does not restore interrupts like task_rq_unlock,
  1280. * you need to do so manually after calling.
  1281. */
  1282. static inline void double_rq_unlock(struct rq *rq1, struct rq *rq2)
  1283. __releases(rq1->lock)
  1284. __releases(rq2->lock)
  1285. {
  1286. BUG_ON(rq1 != rq2);
  1287. raw_spin_unlock(&rq1->lock);
  1288. __release(rq2->lock);
  1289. }
  1290. #endif
  1291. extern struct sched_entity *__pick_first_entity(struct cfs_rq *cfs_rq);
  1292. extern struct sched_entity *__pick_last_entity(struct cfs_rq *cfs_rq);
  1293. extern void print_cfs_stats(struct seq_file *m, int cpu);
  1294. extern void print_rt_stats(struct seq_file *m, int cpu);
  1295. extern void init_cfs_rq(struct cfs_rq *cfs_rq);
  1296. extern void init_rt_rq(struct rt_rq *rt_rq, struct rq *rq);
  1297. extern void init_dl_rq(struct dl_rq *dl_rq, struct rq *rq);
  1298. extern void cfs_bandwidth_usage_inc(void);
  1299. extern void cfs_bandwidth_usage_dec(void);
  1300. #ifdef CONFIG_NO_HZ_COMMON
  1301. enum rq_nohz_flag_bits {
  1302. NOHZ_TICK_STOPPED,
  1303. NOHZ_BALANCE_KICK,
  1304. };
  1305. #define nohz_flags(cpu) (&cpu_rq(cpu)->nohz_flags)
  1306. #endif
  1307. #ifdef CONFIG_IRQ_TIME_ACCOUNTING
  1308. DECLARE_PER_CPU(u64, cpu_hardirq_time);
  1309. DECLARE_PER_CPU(u64, cpu_softirq_time);
  1310. #ifndef CONFIG_64BIT
  1311. DECLARE_PER_CPU(seqcount_t, irq_time_seq);
  1312. static inline void irq_time_write_begin(void)
  1313. {
  1314. __this_cpu_inc(irq_time_seq.sequence);
  1315. smp_wmb();
  1316. }
  1317. static inline void irq_time_write_end(void)
  1318. {
  1319. smp_wmb();
  1320. __this_cpu_inc(irq_time_seq.sequence);
  1321. }
  1322. static inline u64 irq_time_read(int cpu)
  1323. {
  1324. u64 irq_time;
  1325. unsigned seq;
  1326. do {
  1327. seq = read_seqcount_begin(&per_cpu(irq_time_seq, cpu));
  1328. irq_time = per_cpu(cpu_softirq_time, cpu) +
  1329. per_cpu(cpu_hardirq_time, cpu);
  1330. } while (read_seqcount_retry(&per_cpu(irq_time_seq, cpu), seq));
  1331. return irq_time;
  1332. }
  1333. #else /* CONFIG_64BIT */
  1334. static inline void irq_time_write_begin(void)
  1335. {
  1336. }
  1337. static inline void irq_time_write_end(void)
  1338. {
  1339. }
  1340. static inline u64 irq_time_read(int cpu)
  1341. {
  1342. return per_cpu(cpu_softirq_time, cpu) + per_cpu(cpu_hardirq_time, cpu);
  1343. }
  1344. #endif /* CONFIG_64BIT */
  1345. #endif /* CONFIG_IRQ_TIME_ACCOUNTING */
  1346. /* sched: add for print ke log */
  1347. #ifdef CONFIG_SMP
  1348. static inline int rq_cpu(const struct rq *rq) { return rq->cpu; }
  1349. #else
  1350. static inline int rq_cpu(const struct rq *rq) { return 0; }
  1351. #endif
  1352. #ifdef TEST_SCHED_DEBUG_ENHANCEMENT
  1353. extern void lock_timekeeper(void);
  1354. #endif
  1355. #ifdef CONFIG_SCHED_DEBUG
  1356. extern void print_rt_rq(struct seq_file *m, int cpu, struct rt_rq *rt_rq);
  1357. #endif /* CONFIG_SCHED_DEBUG */
  1358. extern void unthrottle_offline_rt_rqs(struct rq *rq);