sched.h 40 KB

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