sched.h 50 KB

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