sched.h 47 KB

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