sched.h 44 KB

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