sched.h 43 KB

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