sched.h 47 KB

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