sched.h 58 KB

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