sched.h 50 KB

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