sched.h 43 KB

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