sched.h 47 KB

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