sched.h 50 KB

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