sched.h 46 KB

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