sched.h 46 KB

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