sched.h 45 KB

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