sched.h 50 KB

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