sched.h 58 KB

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