sched.h 40 KB

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