sched.h 54 KB

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