sched.h 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551
  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);
  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_throttled;
  342. u64 rt_time;
  343. u64 rt_runtime;
  344. /* Nests inside the rq lock: */
  345. raw_spinlock_t rt_runtime_lock;
  346. #ifdef CONFIG_RT_GROUP_SCHED
  347. unsigned long rt_nr_boosted;
  348. struct rq *rq;
  349. struct task_group *tg;
  350. #endif
  351. };
  352. #ifdef CONFIG_RT_GROUP_SCHED
  353. static inline int rt_rq_throttled(struct rt_rq *rt_rq)
  354. {
  355. return rt_rq->rt_throttled && !rt_rq->rt_nr_boosted;
  356. }
  357. #else
  358. static inline int rt_rq_throttled(struct rt_rq *rt_rq)
  359. {
  360. return rt_rq->rt_throttled;
  361. }
  362. #endif
  363. /* Deadline class' related fields in a runqueue */
  364. struct dl_rq {
  365. /* runqueue is an rbtree, ordered by deadline */
  366. struct rb_root rb_root;
  367. struct rb_node *rb_leftmost;
  368. unsigned long dl_nr_running;
  369. #ifdef CONFIG_SMP
  370. /*
  371. * Deadline values of the currently executing and the
  372. * earliest ready task on this rq. Caching these facilitates
  373. * the decision wether or not a ready but not running task
  374. * should migrate somewhere else.
  375. */
  376. struct {
  377. u64 curr;
  378. u64 next;
  379. } earliest_dl;
  380. unsigned long dl_nr_migratory;
  381. int overloaded;
  382. /*
  383. * Tasks on this rq that can be pushed away. They are kept in
  384. * an rb-tree, ordered by tasks' deadlines, with caching
  385. * of the leftmost (earliest deadline) element.
  386. */
  387. struct rb_root pushable_dl_tasks_root;
  388. struct rb_node *pushable_dl_tasks_leftmost;
  389. #else
  390. struct dl_bw dl_bw;
  391. #endif
  392. };
  393. #ifdef CONFIG_SMP
  394. /*
  395. * We add the notion of a root-domain which will be used to define per-domain
  396. * variables. Each exclusive cpuset essentially defines an island domain by
  397. * fully partitioning the member cpus from any other cpuset. Whenever a new
  398. * exclusive cpuset is created, we also create and attach a new root-domain
  399. * object.
  400. *
  401. */
  402. struct root_domain {
  403. atomic_t refcount;
  404. atomic_t rto_count;
  405. struct rcu_head rcu;
  406. cpumask_var_t span;
  407. cpumask_var_t online;
  408. /*
  409. * The bit corresponding to a CPU gets set here if such CPU has more
  410. * than one runnable -deadline task (as it is below for RT tasks).
  411. */
  412. cpumask_var_t dlo_mask;
  413. atomic_t dlo_count;
  414. struct dl_bw dl_bw;
  415. struct cpudl cpudl;
  416. /*
  417. * The "RT overload" flag: it gets set if a CPU has more than
  418. * one runnable RT task.
  419. */
  420. cpumask_var_t rto_mask;
  421. struct cpupri cpupri;
  422. };
  423. extern struct root_domain def_root_domain;
  424. #endif /* CONFIG_SMP */
  425. /*
  426. * This is the main, per-CPU runqueue data structure.
  427. *
  428. * Locking rule: those places that want to lock multiple runqueues
  429. * (such as the load balancing or the thread migration code), lock
  430. * acquire operations must be ordered by ascending &runqueue.
  431. */
  432. struct rq {
  433. /* runqueue lock: */
  434. raw_spinlock_t lock;
  435. /*
  436. * nr_running and cpu_load should be in the same cacheline because
  437. * remote CPUs use both these fields when doing load calculation.
  438. */
  439. unsigned int nr_running;
  440. #ifdef CONFIG_NUMA_BALANCING
  441. unsigned int nr_numa_running;
  442. unsigned int nr_preferred_running;
  443. #endif
  444. #define CPU_LOAD_IDX_MAX 5
  445. unsigned long cpu_load[CPU_LOAD_IDX_MAX];
  446. unsigned long last_load_update_tick;
  447. #ifdef CONFIG_NO_HZ_COMMON
  448. u64 nohz_stamp;
  449. unsigned long nohz_flags;
  450. #endif
  451. #ifdef CONFIG_NO_HZ_FULL
  452. unsigned long last_sched_tick;
  453. #endif
  454. int skip_clock_update;
  455. /* capture load from *all* tasks on this cpu: */
  456. struct load_weight load;
  457. unsigned long nr_load_updates;
  458. u64 nr_switches;
  459. struct cfs_rq cfs;
  460. struct rt_rq rt;
  461. struct dl_rq dl;
  462. #ifdef CONFIG_FAIR_GROUP_SCHED
  463. /* list of leaf cfs_rq on this cpu: */
  464. struct list_head leaf_cfs_rq_list;
  465. struct sched_avg avg;
  466. #endif /* CONFIG_FAIR_GROUP_SCHED */
  467. /*
  468. * This is part of a global counter where only the total sum
  469. * over all CPUs matters. A task can increase this counter on
  470. * one CPU and if it got migrated afterwards it may decrease
  471. * it on another CPU. Always updated under the runqueue lock:
  472. */
  473. unsigned long nr_uninterruptible;
  474. struct task_struct *curr, *idle, *stop;
  475. unsigned long next_balance;
  476. struct mm_struct *prev_mm;
  477. u64 clock;
  478. u64 clock_task;
  479. atomic_t nr_iowait;
  480. #ifdef CONFIG_SMP
  481. struct root_domain *rd;
  482. struct sched_domain *sd;
  483. unsigned long cpu_power;
  484. unsigned char idle_balance;
  485. /* For active balancing */
  486. int post_schedule;
  487. int active_balance;
  488. int push_cpu;
  489. struct cpu_stop_work active_balance_work;
  490. /* cpu of this runqueue: */
  491. int cpu;
  492. int online;
  493. struct list_head cfs_tasks;
  494. u64 rt_avg;
  495. u64 age_stamp;
  496. u64 idle_stamp;
  497. u64 avg_idle;
  498. /* This is used to determine avg_idle's max value */
  499. u64 max_idle_balance_cost;
  500. #endif
  501. #ifdef CONFIG_IRQ_TIME_ACCOUNTING
  502. u64 prev_irq_time;
  503. #endif
  504. #ifdef CONFIG_PARAVIRT
  505. u64 prev_steal_time;
  506. #endif
  507. #ifdef CONFIG_PARAVIRT_TIME_ACCOUNTING
  508. u64 prev_steal_time_rq;
  509. #endif
  510. /* calc_load related fields */
  511. unsigned long calc_load_update;
  512. long calc_load_active;
  513. #ifdef CONFIG_SCHED_HRTICK
  514. #ifdef CONFIG_SMP
  515. int hrtick_csd_pending;
  516. struct call_single_data hrtick_csd;
  517. #endif
  518. struct hrtimer hrtick_timer;
  519. #endif
  520. #ifdef CONFIG_SCHEDSTATS
  521. /* latency stats */
  522. struct sched_info rq_sched_info;
  523. unsigned long long rq_cpu_time;
  524. /* could above be rq->cfs_rq.exec_clock + rq->rt_rq.rt_runtime ? */
  525. /* sys_sched_yield() stats */
  526. unsigned int yld_count;
  527. /* schedule() stats */
  528. unsigned int sched_count;
  529. unsigned int sched_goidle;
  530. /* try_to_wake_up() stats */
  531. unsigned int ttwu_count;
  532. unsigned int ttwu_local;
  533. #endif
  534. #ifdef CONFIG_SMP
  535. struct llist_head wake_list;
  536. #endif
  537. };
  538. static inline int cpu_of(struct rq *rq)
  539. {
  540. #ifdef CONFIG_SMP
  541. return rq->cpu;
  542. #else
  543. return 0;
  544. #endif
  545. }
  546. DECLARE_PER_CPU(struct rq, runqueues);
  547. #define cpu_rq(cpu) (&per_cpu(runqueues, (cpu)))
  548. #define this_rq() (&__get_cpu_var(runqueues))
  549. #define task_rq(p) cpu_rq(task_cpu(p))
  550. #define cpu_curr(cpu) (cpu_rq(cpu)->curr)
  551. #define raw_rq() (&__raw_get_cpu_var(runqueues))
  552. static inline u64 rq_clock(struct rq *rq)
  553. {
  554. return rq->clock;
  555. }
  556. static inline u64 rq_clock_task(struct rq *rq)
  557. {
  558. return rq->clock_task;
  559. }
  560. #ifdef CONFIG_NUMA_BALANCING
  561. extern void sched_setnuma(struct task_struct *p, int node);
  562. extern int migrate_task_to(struct task_struct *p, int cpu);
  563. extern int migrate_swap(struct task_struct *, struct task_struct *);
  564. #endif /* CONFIG_NUMA_BALANCING */
  565. #ifdef CONFIG_SMP
  566. #define rcu_dereference_check_sched_domain(p) \
  567. rcu_dereference_check((p), \
  568. lockdep_is_held(&sched_domains_mutex))
  569. /*
  570. * The domain tree (rq->sd) is protected by RCU's quiescent state transition.
  571. * See detach_destroy_domains: synchronize_sched for details.
  572. *
  573. * The domain tree of any CPU may only be accessed from within
  574. * preempt-disabled sections.
  575. */
  576. #define for_each_domain(cpu, __sd) \
  577. for (__sd = rcu_dereference_check_sched_domain(cpu_rq(cpu)->sd); \
  578. __sd; __sd = __sd->parent)
  579. #define for_each_lower_domain(sd) for (; sd; sd = sd->child)
  580. /**
  581. * highest_flag_domain - Return highest sched_domain containing flag.
  582. * @cpu: The cpu whose highest level of sched domain is to
  583. * be returned.
  584. * @flag: The flag to check for the highest sched_domain
  585. * for the given cpu.
  586. *
  587. * Returns the highest sched_domain of a cpu which contains the given flag.
  588. */
  589. static inline struct sched_domain *highest_flag_domain(int cpu, int flag)
  590. {
  591. struct sched_domain *sd, *hsd = NULL;
  592. for_each_domain(cpu, sd) {
  593. if (!(sd->flags & flag))
  594. break;
  595. hsd = sd;
  596. }
  597. return hsd;
  598. }
  599. static inline struct sched_domain *lowest_flag_domain(int cpu, int flag)
  600. {
  601. struct sched_domain *sd;
  602. for_each_domain(cpu, sd) {
  603. if (sd->flags & flag)
  604. break;
  605. }
  606. return sd;
  607. }
  608. DECLARE_PER_CPU(struct sched_domain *, sd_llc);
  609. DECLARE_PER_CPU(int, sd_llc_size);
  610. DECLARE_PER_CPU(int, sd_llc_id);
  611. DECLARE_PER_CPU(struct sched_domain *, sd_numa);
  612. DECLARE_PER_CPU(struct sched_domain *, sd_busy);
  613. DECLARE_PER_CPU(struct sched_domain *, sd_asym);
  614. struct sched_group_power {
  615. atomic_t ref;
  616. /*
  617. * CPU power of this group, SCHED_LOAD_SCALE being max power for a
  618. * single CPU.
  619. */
  620. unsigned int power, power_orig;
  621. unsigned long next_update;
  622. int imbalance; /* XXX unrelated to power but shared group state */
  623. /*
  624. * Number of busy cpus in this group.
  625. */
  626. atomic_t nr_busy_cpus;
  627. unsigned long cpumask[0]; /* iteration mask */
  628. };
  629. struct sched_group {
  630. struct sched_group *next; /* Must be a circular list */
  631. atomic_t ref;
  632. unsigned int group_weight;
  633. struct sched_group_power *sgp;
  634. /*
  635. * The CPUs this group covers.
  636. *
  637. * NOTE: this field is variable length. (Allocated dynamically
  638. * by attaching extra space to the end of the structure,
  639. * depending on how many CPUs the kernel has booted up with)
  640. */
  641. unsigned long cpumask[0];
  642. };
  643. static inline struct cpumask *sched_group_cpus(struct sched_group *sg)
  644. {
  645. return to_cpumask(sg->cpumask);
  646. }
  647. /*
  648. * cpumask masking which cpus in the group are allowed to iterate up the domain
  649. * tree.
  650. */
  651. static inline struct cpumask *sched_group_mask(struct sched_group *sg)
  652. {
  653. return to_cpumask(sg->sgp->cpumask);
  654. }
  655. /**
  656. * group_first_cpu - Returns the first cpu in the cpumask of a sched_group.
  657. * @group: The group whose first cpu is to be returned.
  658. */
  659. static inline unsigned int group_first_cpu(struct sched_group *group)
  660. {
  661. return cpumask_first(sched_group_cpus(group));
  662. }
  663. extern int group_balance_cpu(struct sched_group *sg);
  664. #endif /* CONFIG_SMP */
  665. #include "stats.h"
  666. #include "auto_group.h"
  667. #ifdef CONFIG_CGROUP_SCHED
  668. /*
  669. * Return the group to which this tasks belongs.
  670. *
  671. * We cannot use task_css() and friends because the cgroup subsystem
  672. * changes that value before the cgroup_subsys::attach() method is called,
  673. * therefore we cannot pin it and might observe the wrong value.
  674. *
  675. * The same is true for autogroup's p->signal->autogroup->tg, the autogroup
  676. * core changes this before calling sched_move_task().
  677. *
  678. * Instead we use a 'copy' which is updated from sched_move_task() while
  679. * holding both task_struct::pi_lock and rq::lock.
  680. */
  681. static inline struct task_group *task_group(struct task_struct *p)
  682. {
  683. return p->sched_task_group;
  684. }
  685. /* Change a task's cfs_rq and parent entity if it moves across CPUs/groups */
  686. static inline void set_task_rq(struct task_struct *p, unsigned int cpu)
  687. {
  688. #if defined(CONFIG_FAIR_GROUP_SCHED) || defined(CONFIG_RT_GROUP_SCHED)
  689. struct task_group *tg = task_group(p);
  690. #endif
  691. #ifdef CONFIG_FAIR_GROUP_SCHED
  692. p->se.cfs_rq = tg->cfs_rq[cpu];
  693. p->se.parent = tg->se[cpu];
  694. #endif
  695. #ifdef CONFIG_RT_GROUP_SCHED
  696. p->rt.rt_rq = tg->rt_rq[cpu];
  697. p->rt.parent = tg->rt_se[cpu];
  698. #endif
  699. }
  700. #else /* CONFIG_CGROUP_SCHED */
  701. static inline void set_task_rq(struct task_struct *p, unsigned int cpu) { }
  702. static inline struct task_group *task_group(struct task_struct *p)
  703. {
  704. return NULL;
  705. }
  706. #endif /* CONFIG_CGROUP_SCHED */
  707. static inline void __set_task_cpu(struct task_struct *p, unsigned int cpu)
  708. {
  709. set_task_rq(p, cpu);
  710. #ifdef CONFIG_SMP
  711. /*
  712. * After ->cpu is set up to a new value, task_rq_lock(p, ...) can be
  713. * successfuly executed on another CPU. We must ensure that updates of
  714. * per-task data have been completed by this moment.
  715. */
  716. smp_wmb();
  717. task_thread_info(p)->cpu = cpu;
  718. p->wake_cpu = cpu;
  719. #endif
  720. }
  721. /*
  722. * Tunables that become constants when CONFIG_SCHED_DEBUG is off:
  723. */
  724. #ifdef CONFIG_SCHED_DEBUG
  725. # include <linux/static_key.h>
  726. # define const_debug __read_mostly
  727. #else
  728. # define const_debug const
  729. #endif
  730. extern const_debug unsigned int sysctl_sched_features;
  731. #define SCHED_FEAT(name, enabled) \
  732. __SCHED_FEAT_##name ,
  733. enum {
  734. #include "features.h"
  735. __SCHED_FEAT_NR,
  736. };
  737. #undef SCHED_FEAT
  738. #if defined(CONFIG_SCHED_DEBUG) && defined(HAVE_JUMP_LABEL)
  739. static __always_inline bool static_branch__true(struct static_key *key)
  740. {
  741. return static_key_true(key); /* Not out of line branch. */
  742. }
  743. static __always_inline bool static_branch__false(struct static_key *key)
  744. {
  745. return static_key_false(key); /* Out of line branch. */
  746. }
  747. #define SCHED_FEAT(name, enabled) \
  748. static __always_inline bool static_branch_##name(struct static_key *key) \
  749. { \
  750. return static_branch__##enabled(key); \
  751. }
  752. #include "features.h"
  753. #undef SCHED_FEAT
  754. extern struct static_key sched_feat_keys[__SCHED_FEAT_NR];
  755. #define sched_feat(x) (static_branch_##x(&sched_feat_keys[__SCHED_FEAT_##x]))
  756. #else /* !(SCHED_DEBUG && HAVE_JUMP_LABEL) */
  757. #define sched_feat(x) (sysctl_sched_features & (1UL << __SCHED_FEAT_##x))
  758. #endif /* SCHED_DEBUG && HAVE_JUMP_LABEL */
  759. #ifdef CONFIG_NUMA_BALANCING
  760. #define sched_feat_numa(x) sched_feat(x)
  761. #ifdef CONFIG_SCHED_DEBUG
  762. #define numabalancing_enabled sched_feat_numa(NUMA)
  763. #else
  764. extern bool numabalancing_enabled;
  765. #endif /* CONFIG_SCHED_DEBUG */
  766. #else
  767. #define sched_feat_numa(x) (0)
  768. #define numabalancing_enabled (0)
  769. #endif /* CONFIG_NUMA_BALANCING */
  770. static inline u64 global_rt_period(void)
  771. {
  772. return (u64)sysctl_sched_rt_period * NSEC_PER_USEC;
  773. }
  774. static inline u64 global_rt_runtime(void)
  775. {
  776. if (sysctl_sched_rt_runtime < 0)
  777. return RUNTIME_INF;
  778. return (u64)sysctl_sched_rt_runtime * NSEC_PER_USEC;
  779. }
  780. static inline int task_current(struct rq *rq, struct task_struct *p)
  781. {
  782. return rq->curr == p;
  783. }
  784. static inline int task_running(struct rq *rq, struct task_struct *p)
  785. {
  786. #ifdef CONFIG_SMP
  787. return p->on_cpu;
  788. #else
  789. return task_current(rq, p);
  790. #endif
  791. }
  792. #ifndef prepare_arch_switch
  793. # define prepare_arch_switch(next) do { } while (0)
  794. #endif
  795. #ifndef finish_arch_switch
  796. # define finish_arch_switch(prev) do { } while (0)
  797. #endif
  798. #ifndef finish_arch_post_lock_switch
  799. # define finish_arch_post_lock_switch() do { } while (0)
  800. #endif
  801. #ifndef __ARCH_WANT_UNLOCKED_CTXSW
  802. static inline void prepare_lock_switch(struct rq *rq, struct task_struct *next)
  803. {
  804. #ifdef CONFIG_SMP
  805. /*
  806. * We can optimise this out completely for !SMP, because the
  807. * SMP rebalancing from interrupt is the only thing that cares
  808. * here.
  809. */
  810. next->on_cpu = 1;
  811. #endif
  812. }
  813. static inline void finish_lock_switch(struct rq *rq, struct task_struct *prev)
  814. {
  815. #ifdef CONFIG_SMP
  816. /*
  817. * After ->on_cpu is cleared, the task can be moved to a different CPU.
  818. * We must ensure this doesn't happen until the switch is completely
  819. * finished.
  820. */
  821. smp_wmb();
  822. prev->on_cpu = 0;
  823. #endif
  824. #ifdef CONFIG_DEBUG_SPINLOCK
  825. /* this is a valid case when another task releases the spinlock */
  826. rq->lock.owner = current;
  827. #endif
  828. /*
  829. * If we are tracking spinlock dependencies then we have to
  830. * fix up the runqueue lock - which gets 'carried over' from
  831. * prev into current:
  832. */
  833. spin_acquire(&rq->lock.dep_map, 0, 0, _THIS_IP_);
  834. raw_spin_unlock_irq(&rq->lock);
  835. }
  836. #else /* __ARCH_WANT_UNLOCKED_CTXSW */
  837. static inline void prepare_lock_switch(struct rq *rq, struct task_struct *next)
  838. {
  839. #ifdef CONFIG_SMP
  840. /*
  841. * We can optimise this out completely for !SMP, because the
  842. * SMP rebalancing from interrupt is the only thing that cares
  843. * here.
  844. */
  845. next->on_cpu = 1;
  846. #endif
  847. raw_spin_unlock(&rq->lock);
  848. }
  849. static inline void finish_lock_switch(struct rq *rq, struct task_struct *prev)
  850. {
  851. #ifdef CONFIG_SMP
  852. /*
  853. * After ->on_cpu is cleared, the task can be moved to a different CPU.
  854. * We must ensure this doesn't happen until the switch is completely
  855. * finished.
  856. */
  857. smp_wmb();
  858. prev->on_cpu = 0;
  859. #endif
  860. local_irq_enable();
  861. }
  862. #endif /* __ARCH_WANT_UNLOCKED_CTXSW */
  863. /*
  864. * wake flags
  865. */
  866. #define WF_SYNC 0x01 /* waker goes to sleep after wakeup */
  867. #define WF_FORK 0x02 /* child wakeup after fork */
  868. #define WF_MIGRATED 0x4 /* internal use, task got migrated */
  869. /*
  870. * To aid in avoiding the subversion of "niceness" due to uneven distribution
  871. * of tasks with abnormal "nice" values across CPUs the contribution that
  872. * each task makes to its run queue's load is weighted according to its
  873. * scheduling class and "nice" value. For SCHED_NORMAL tasks this is just a
  874. * scaled version of the new time slice allocation that they receive on time
  875. * slice expiry etc.
  876. */
  877. #define WEIGHT_IDLEPRIO 3
  878. #define WMULT_IDLEPRIO 1431655765
  879. /*
  880. * Nice levels are multiplicative, with a gentle 10% change for every
  881. * nice level changed. I.e. when a CPU-bound task goes from nice 0 to
  882. * nice 1, it will get ~10% less CPU time than another CPU-bound task
  883. * that remained on nice 0.
  884. *
  885. * The "10% effect" is relative and cumulative: from _any_ nice level,
  886. * if you go up 1 level, it's -10% CPU usage, if you go down 1 level
  887. * it's +10% CPU usage. (to achieve that we use a multiplier of 1.25.
  888. * If a task goes up by ~10% and another task goes down by ~10% then
  889. * the relative distance between them is ~25%.)
  890. */
  891. static const int prio_to_weight[40] = {
  892. /* -20 */ 88761, 71755, 56483, 46273, 36291,
  893. /* -15 */ 29154, 23254, 18705, 14949, 11916,
  894. /* -10 */ 9548, 7620, 6100, 4904, 3906,
  895. /* -5 */ 3121, 2501, 1991, 1586, 1277,
  896. /* 0 */ 1024, 820, 655, 526, 423,
  897. /* 5 */ 335, 272, 215, 172, 137,
  898. /* 10 */ 110, 87, 70, 56, 45,
  899. /* 15 */ 36, 29, 23, 18, 15,
  900. };
  901. /*
  902. * Inverse (2^32/x) values of the prio_to_weight[] array, precalculated.
  903. *
  904. * In cases where the weight does not change often, we can use the
  905. * precalculated inverse to speed up arithmetics by turning divisions
  906. * into multiplications:
  907. */
  908. static const u32 prio_to_wmult[40] = {
  909. /* -20 */ 48388, 59856, 76040, 92818, 118348,
  910. /* -15 */ 147320, 184698, 229616, 287308, 360437,
  911. /* -10 */ 449829, 563644, 704093, 875809, 1099582,
  912. /* -5 */ 1376151, 1717300, 2157191, 2708050, 3363326,
  913. /* 0 */ 4194304, 5237765, 6557202, 8165337, 10153587,
  914. /* 5 */ 12820798, 15790321, 19976592, 24970740, 31350126,
  915. /* 10 */ 39045157, 49367440, 61356676, 76695844, 95443717,
  916. /* 15 */ 119304647, 148102320, 186737708, 238609294, 286331153,
  917. };
  918. #define ENQUEUE_WAKEUP 1
  919. #define ENQUEUE_HEAD 2
  920. #ifdef CONFIG_SMP
  921. #define ENQUEUE_WAKING 4 /* sched_class::task_waking was called */
  922. #else
  923. #define ENQUEUE_WAKING 0
  924. #endif
  925. #define ENQUEUE_REPLENISH 8
  926. #define DEQUEUE_SLEEP 1
  927. #define RETRY_TASK ((void *)-1UL)
  928. struct sched_class {
  929. const struct sched_class *next;
  930. void (*enqueue_task) (struct rq *rq, struct task_struct *p, int flags);
  931. void (*dequeue_task) (struct rq *rq, struct task_struct *p, int flags);
  932. void (*yield_task) (struct rq *rq);
  933. bool (*yield_to_task) (struct rq *rq, struct task_struct *p, bool preempt);
  934. void (*check_preempt_curr) (struct rq *rq, struct task_struct *p, int flags);
  935. /*
  936. * It is the responsibility of the pick_next_task() method that will
  937. * return the next task to call put_prev_task() on the @prev task or
  938. * something equivalent.
  939. *
  940. * May return RETRY_TASK when it finds a higher prio class has runnable
  941. * tasks.
  942. */
  943. struct task_struct * (*pick_next_task) (struct rq *rq,
  944. struct task_struct *prev);
  945. void (*put_prev_task) (struct rq *rq, struct task_struct *p);
  946. #ifdef CONFIG_SMP
  947. int (*select_task_rq)(struct task_struct *p, int task_cpu, int sd_flag, int flags);
  948. void (*migrate_task_rq)(struct task_struct *p, int next_cpu);
  949. void (*post_schedule) (struct rq *this_rq);
  950. void (*task_waking) (struct task_struct *task);
  951. void (*task_woken) (struct rq *this_rq, struct task_struct *task);
  952. void (*set_cpus_allowed)(struct task_struct *p,
  953. const struct cpumask *newmask);
  954. void (*rq_online)(struct rq *rq);
  955. void (*rq_offline)(struct rq *rq);
  956. #endif
  957. void (*set_curr_task) (struct rq *rq);
  958. void (*task_tick) (struct rq *rq, struct task_struct *p, int queued);
  959. void (*task_fork) (struct task_struct *p);
  960. void (*task_dead) (struct task_struct *p);
  961. void (*switched_from) (struct rq *this_rq, struct task_struct *task);
  962. void (*switched_to) (struct rq *this_rq, struct task_struct *task);
  963. void (*prio_changed) (struct rq *this_rq, struct task_struct *task,
  964. int oldprio);
  965. unsigned int (*get_rr_interval) (struct rq *rq,
  966. struct task_struct *task);
  967. #ifdef CONFIG_FAIR_GROUP_SCHED
  968. void (*task_move_group) (struct task_struct *p, int on_rq);
  969. #endif
  970. };
  971. static inline void put_prev_task(struct rq *rq, struct task_struct *prev)
  972. {
  973. prev->sched_class->put_prev_task(rq, prev);
  974. }
  975. #define sched_class_highest (&stop_sched_class)
  976. #define for_each_class(class) \
  977. for (class = sched_class_highest; class; class = class->next)
  978. extern const struct sched_class stop_sched_class;
  979. extern const struct sched_class dl_sched_class;
  980. extern const struct sched_class rt_sched_class;
  981. extern const struct sched_class fair_sched_class;
  982. extern const struct sched_class idle_sched_class;
  983. #ifdef CONFIG_SMP
  984. extern void update_group_power(struct sched_domain *sd, int cpu);
  985. extern void trigger_load_balance(struct rq *rq);
  986. extern void idle_enter_fair(struct rq *this_rq);
  987. extern void idle_exit_fair(struct rq *this_rq);
  988. #else
  989. static inline void idle_enter_fair(struct rq *rq) { }
  990. static inline void idle_exit_fair(struct rq *rq) { }
  991. #endif
  992. extern void sysrq_sched_debug_show(void);
  993. extern void sched_init_granularity(void);
  994. extern void update_max_interval(void);
  995. extern void init_sched_dl_class(void);
  996. extern void init_sched_rt_class(void);
  997. extern void init_sched_fair_class(void);
  998. extern void init_sched_dl_class(void);
  999. extern void resched_task(struct task_struct *p);
  1000. extern void resched_cpu(int cpu);
  1001. extern struct rt_bandwidth def_rt_bandwidth;
  1002. extern void init_rt_bandwidth(struct rt_bandwidth *rt_b, u64 period, u64 runtime);
  1003. extern struct dl_bandwidth def_dl_bandwidth;
  1004. extern void init_dl_bandwidth(struct dl_bandwidth *dl_b, u64 period, u64 runtime);
  1005. extern void init_dl_task_timer(struct sched_dl_entity *dl_se);
  1006. unsigned long to_ratio(u64 period, u64 runtime);
  1007. extern void update_idle_cpu_load(struct rq *this_rq);
  1008. extern void init_task_runnable_average(struct task_struct *p);
  1009. static inline void inc_nr_running(struct rq *rq)
  1010. {
  1011. rq->nr_running++;
  1012. #ifdef CONFIG_NO_HZ_FULL
  1013. if (rq->nr_running == 2) {
  1014. if (tick_nohz_full_cpu(rq->cpu)) {
  1015. /* Order rq->nr_running write against the IPI */
  1016. smp_wmb();
  1017. smp_send_reschedule(rq->cpu);
  1018. }
  1019. }
  1020. #endif
  1021. }
  1022. static inline void dec_nr_running(struct rq *rq)
  1023. {
  1024. rq->nr_running--;
  1025. }
  1026. static inline void rq_last_tick_reset(struct rq *rq)
  1027. {
  1028. #ifdef CONFIG_NO_HZ_FULL
  1029. rq->last_sched_tick = jiffies;
  1030. #endif
  1031. }
  1032. extern void update_rq_clock(struct rq *rq);
  1033. extern void activate_task(struct rq *rq, struct task_struct *p, int flags);
  1034. extern void deactivate_task(struct rq *rq, struct task_struct *p, int flags);
  1035. extern void check_preempt_curr(struct rq *rq, struct task_struct *p, int flags);
  1036. extern const_debug unsigned int sysctl_sched_time_avg;
  1037. extern const_debug unsigned int sysctl_sched_nr_migrate;
  1038. extern const_debug unsigned int sysctl_sched_migration_cost;
  1039. static inline u64 sched_avg_period(void)
  1040. {
  1041. return (u64)sysctl_sched_time_avg * NSEC_PER_MSEC / 2;
  1042. }
  1043. #ifdef CONFIG_SCHED_HRTICK
  1044. /*
  1045. * Use hrtick when:
  1046. * - enabled by features
  1047. * - hrtimer is actually high res
  1048. */
  1049. static inline int hrtick_enabled(struct rq *rq)
  1050. {
  1051. if (!sched_feat(HRTICK))
  1052. return 0;
  1053. if (!cpu_active(cpu_of(rq)))
  1054. return 0;
  1055. return hrtimer_is_hres_active(&rq->hrtick_timer);
  1056. }
  1057. void hrtick_start(struct rq *rq, u64 delay);
  1058. #else
  1059. static inline int hrtick_enabled(struct rq *rq)
  1060. {
  1061. return 0;
  1062. }
  1063. #endif /* CONFIG_SCHED_HRTICK */
  1064. #ifdef CONFIG_SMP
  1065. extern void sched_avg_update(struct rq *rq);
  1066. static inline void sched_rt_avg_update(struct rq *rq, u64 rt_delta)
  1067. {
  1068. rq->rt_avg += rt_delta;
  1069. sched_avg_update(rq);
  1070. }
  1071. #else
  1072. static inline void sched_rt_avg_update(struct rq *rq, u64 rt_delta) { }
  1073. static inline void sched_avg_update(struct rq *rq) { }
  1074. #endif
  1075. extern void start_bandwidth_timer(struct hrtimer *period_timer, ktime_t period);
  1076. #ifdef CONFIG_SMP
  1077. #ifdef CONFIG_PREEMPT
  1078. static inline void double_rq_lock(struct rq *rq1, struct rq *rq2);
  1079. /*
  1080. * fair double_lock_balance: Safely acquires both rq->locks in a fair
  1081. * way at the expense of forcing extra atomic operations in all
  1082. * invocations. This assures that the double_lock is acquired using the
  1083. * same underlying policy as the spinlock_t on this architecture, which
  1084. * reduces latency compared to the unfair variant below. However, it
  1085. * also adds more overhead and therefore may reduce throughput.
  1086. */
  1087. static inline int _double_lock_balance(struct rq *this_rq, struct rq *busiest)
  1088. __releases(this_rq->lock)
  1089. __acquires(busiest->lock)
  1090. __acquires(this_rq->lock)
  1091. {
  1092. raw_spin_unlock(&this_rq->lock);
  1093. double_rq_lock(this_rq, busiest);
  1094. return 1;
  1095. }
  1096. #else
  1097. /*
  1098. * Unfair double_lock_balance: Optimizes throughput at the expense of
  1099. * latency by eliminating extra atomic operations when the locks are
  1100. * already in proper order on entry. This favors lower cpu-ids and will
  1101. * grant the double lock to lower cpus over higher ids under contention,
  1102. * regardless of entry order into the function.
  1103. */
  1104. static inline int _double_lock_balance(struct rq *this_rq, struct rq *busiest)
  1105. __releases(this_rq->lock)
  1106. __acquires(busiest->lock)
  1107. __acquires(this_rq->lock)
  1108. {
  1109. int ret = 0;
  1110. if (unlikely(!raw_spin_trylock(&busiest->lock))) {
  1111. if (busiest < this_rq) {
  1112. raw_spin_unlock(&this_rq->lock);
  1113. raw_spin_lock(&busiest->lock);
  1114. raw_spin_lock_nested(&this_rq->lock,
  1115. SINGLE_DEPTH_NESTING);
  1116. ret = 1;
  1117. } else
  1118. raw_spin_lock_nested(&busiest->lock,
  1119. SINGLE_DEPTH_NESTING);
  1120. }
  1121. return ret;
  1122. }
  1123. #endif /* CONFIG_PREEMPT */
  1124. /*
  1125. * double_lock_balance - lock the busiest runqueue, this_rq is locked already.
  1126. */
  1127. static inline int double_lock_balance(struct rq *this_rq, struct rq *busiest)
  1128. {
  1129. if (unlikely(!irqs_disabled())) {
  1130. /* printk() doesn't work good under rq->lock */
  1131. raw_spin_unlock(&this_rq->lock);
  1132. BUG_ON(1);
  1133. }
  1134. return _double_lock_balance(this_rq, busiest);
  1135. }
  1136. static inline void double_unlock_balance(struct rq *this_rq, struct rq *busiest)
  1137. __releases(busiest->lock)
  1138. {
  1139. raw_spin_unlock(&busiest->lock);
  1140. lock_set_subclass(&this_rq->lock.dep_map, 0, _RET_IP_);
  1141. }
  1142. static inline void double_lock(spinlock_t *l1, spinlock_t *l2)
  1143. {
  1144. if (l1 > l2)
  1145. swap(l1, l2);
  1146. spin_lock(l1);
  1147. spin_lock_nested(l2, SINGLE_DEPTH_NESTING);
  1148. }
  1149. static inline void double_lock_irq(spinlock_t *l1, spinlock_t *l2)
  1150. {
  1151. if (l1 > l2)
  1152. swap(l1, l2);
  1153. spin_lock_irq(l1);
  1154. spin_lock_nested(l2, SINGLE_DEPTH_NESTING);
  1155. }
  1156. static inline void double_raw_lock(raw_spinlock_t *l1, raw_spinlock_t *l2)
  1157. {
  1158. if (l1 > l2)
  1159. swap(l1, l2);
  1160. raw_spin_lock(l1);
  1161. raw_spin_lock_nested(l2, SINGLE_DEPTH_NESTING);
  1162. }
  1163. /*
  1164. * double_rq_lock - safely lock two runqueues
  1165. *
  1166. * Note this does not disable interrupts like task_rq_lock,
  1167. * you need to do so manually before calling.
  1168. */
  1169. static inline void double_rq_lock(struct rq *rq1, struct rq *rq2)
  1170. __acquires(rq1->lock)
  1171. __acquires(rq2->lock)
  1172. {
  1173. BUG_ON(!irqs_disabled());
  1174. if (rq1 == rq2) {
  1175. raw_spin_lock(&rq1->lock);
  1176. __acquire(rq2->lock); /* Fake it out ;) */
  1177. } else {
  1178. if (rq1 < rq2) {
  1179. raw_spin_lock(&rq1->lock);
  1180. raw_spin_lock_nested(&rq2->lock, SINGLE_DEPTH_NESTING);
  1181. } else {
  1182. raw_spin_lock(&rq2->lock);
  1183. raw_spin_lock_nested(&rq1->lock, SINGLE_DEPTH_NESTING);
  1184. }
  1185. }
  1186. }
  1187. /*
  1188. * double_rq_unlock - safely unlock two runqueues
  1189. *
  1190. * Note this does not restore interrupts like task_rq_unlock,
  1191. * you need to do so manually after calling.
  1192. */
  1193. static inline void double_rq_unlock(struct rq *rq1, struct rq *rq2)
  1194. __releases(rq1->lock)
  1195. __releases(rq2->lock)
  1196. {
  1197. raw_spin_unlock(&rq1->lock);
  1198. if (rq1 != rq2)
  1199. raw_spin_unlock(&rq2->lock);
  1200. else
  1201. __release(rq2->lock);
  1202. }
  1203. #else /* CONFIG_SMP */
  1204. /*
  1205. * double_rq_lock - safely lock two runqueues
  1206. *
  1207. * Note this does not disable interrupts like task_rq_lock,
  1208. * you need to do so manually before calling.
  1209. */
  1210. static inline void double_rq_lock(struct rq *rq1, struct rq *rq2)
  1211. __acquires(rq1->lock)
  1212. __acquires(rq2->lock)
  1213. {
  1214. BUG_ON(!irqs_disabled());
  1215. BUG_ON(rq1 != rq2);
  1216. raw_spin_lock(&rq1->lock);
  1217. __acquire(rq2->lock); /* Fake it out ;) */
  1218. }
  1219. /*
  1220. * double_rq_unlock - safely unlock two runqueues
  1221. *
  1222. * Note this does not restore interrupts like task_rq_unlock,
  1223. * you need to do so manually after calling.
  1224. */
  1225. static inline void double_rq_unlock(struct rq *rq1, struct rq *rq2)
  1226. __releases(rq1->lock)
  1227. __releases(rq2->lock)
  1228. {
  1229. BUG_ON(rq1 != rq2);
  1230. raw_spin_unlock(&rq1->lock);
  1231. __release(rq2->lock);
  1232. }
  1233. #endif
  1234. extern struct sched_entity *__pick_first_entity(struct cfs_rq *cfs_rq);
  1235. extern struct sched_entity *__pick_last_entity(struct cfs_rq *cfs_rq);
  1236. extern void print_cfs_stats(struct seq_file *m, int cpu);
  1237. extern void print_rt_stats(struct seq_file *m, int cpu);
  1238. extern void init_cfs_rq(struct cfs_rq *cfs_rq);
  1239. extern void init_rt_rq(struct rt_rq *rt_rq, struct rq *rq);
  1240. extern void init_dl_rq(struct dl_rq *dl_rq, struct rq *rq);
  1241. extern void cfs_bandwidth_usage_inc(void);
  1242. extern void cfs_bandwidth_usage_dec(void);
  1243. #ifdef CONFIG_NO_HZ_COMMON
  1244. enum rq_nohz_flag_bits {
  1245. NOHZ_TICK_STOPPED,
  1246. NOHZ_BALANCE_KICK,
  1247. };
  1248. #define nohz_flags(cpu) (&cpu_rq(cpu)->nohz_flags)
  1249. #endif
  1250. #ifdef CONFIG_IRQ_TIME_ACCOUNTING
  1251. DECLARE_PER_CPU(u64, cpu_hardirq_time);
  1252. DECLARE_PER_CPU(u64, cpu_softirq_time);
  1253. #ifndef CONFIG_64BIT
  1254. DECLARE_PER_CPU(seqcount_t, irq_time_seq);
  1255. static inline void irq_time_write_begin(void)
  1256. {
  1257. __this_cpu_inc(irq_time_seq.sequence);
  1258. smp_wmb();
  1259. }
  1260. static inline void irq_time_write_end(void)
  1261. {
  1262. smp_wmb();
  1263. __this_cpu_inc(irq_time_seq.sequence);
  1264. }
  1265. static inline u64 irq_time_read(int cpu)
  1266. {
  1267. u64 irq_time;
  1268. unsigned seq;
  1269. do {
  1270. seq = read_seqcount_begin(&per_cpu(irq_time_seq, cpu));
  1271. irq_time = per_cpu(cpu_softirq_time, cpu) +
  1272. per_cpu(cpu_hardirq_time, cpu);
  1273. } while (read_seqcount_retry(&per_cpu(irq_time_seq, cpu), seq));
  1274. return irq_time;
  1275. }
  1276. #else /* CONFIG_64BIT */
  1277. static inline void irq_time_write_begin(void)
  1278. {
  1279. }
  1280. static inline void irq_time_write_end(void)
  1281. {
  1282. }
  1283. static inline u64 irq_time_read(int cpu)
  1284. {
  1285. return per_cpu(cpu_softirq_time, cpu) + per_cpu(cpu_hardirq_time, cpu);
  1286. }
  1287. #endif /* CONFIG_64BIT */
  1288. #endif /* CONFIG_IRQ_TIME_ACCOUNTING */