sched.h 56 KB

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