sched.h 46 KB

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