sched.h 45 KB

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