sched.h 54 KB

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