sched.h 45 KB

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