sched.h 46 KB

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