sched.h 57 KB

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