sched.h 45 KB

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