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