sched.h 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161
  1. #include <linux/sched.h>
  2. #include <linux/mutex.h>
  3. #include <linux/spinlock.h>
  4. #include <linux/stop_machine.h>
  5. #include "cpupri.h"
  6. extern __read_mostly int scheduler_running;
  7. /*
  8. * Convert user-nice values [ -20 ... 0 ... 19 ]
  9. * to static priority [ MAX_RT_PRIO..MAX_PRIO-1 ],
  10. * and back.
  11. */
  12. #define NICE_TO_PRIO(nice) (MAX_RT_PRIO + (nice) + 20)
  13. #define PRIO_TO_NICE(prio) ((prio) - MAX_RT_PRIO - 20)
  14. #define TASK_NICE(p) PRIO_TO_NICE((p)->static_prio)
  15. /*
  16. * 'User priority' is the nice value converted to something we
  17. * can work with better when scaling various scheduler parameters,
  18. * it's a [ 0 ... 39 ] range.
  19. */
  20. #define USER_PRIO(p) ((p)-MAX_RT_PRIO)
  21. #define TASK_USER_PRIO(p) USER_PRIO((p)->static_prio)
  22. #define MAX_USER_PRIO (USER_PRIO(MAX_PRIO))
  23. /*
  24. * Helpers for converting nanosecond timing to jiffy resolution
  25. */
  26. #define NS_TO_JIFFIES(TIME) ((unsigned long)(TIME) / (NSEC_PER_SEC / HZ))
  27. #define NICE_0_LOAD SCHED_LOAD_SCALE
  28. #define NICE_0_SHIFT SCHED_LOAD_SHIFT
  29. /*
  30. * These are the 'tuning knobs' of the scheduler:
  31. */
  32. /*
  33. * single value that denotes runtime == period, ie unlimited time.
  34. */
  35. #define RUNTIME_INF ((u64)~0ULL)
  36. static inline int rt_policy(int policy)
  37. {
  38. if (policy == SCHED_FIFO || policy == SCHED_RR)
  39. return 1;
  40. return 0;
  41. }
  42. static inline int task_has_rt_policy(struct task_struct *p)
  43. {
  44. return rt_policy(p->policy);
  45. }
  46. /*
  47. * This is the priority-queue data structure of the RT scheduling class:
  48. */
  49. struct rt_prio_array {
  50. DECLARE_BITMAP(bitmap, MAX_RT_PRIO+1); /* include 1 bit for delimiter */
  51. struct list_head queue[MAX_RT_PRIO];
  52. };
  53. struct rt_bandwidth {
  54. /* nests inside the rq lock: */
  55. raw_spinlock_t rt_runtime_lock;
  56. ktime_t rt_period;
  57. u64 rt_runtime;
  58. struct hrtimer rt_period_timer;
  59. };
  60. extern struct mutex sched_domains_mutex;
  61. #ifdef CONFIG_CGROUP_SCHED
  62. #include <linux/cgroup.h>
  63. struct cfs_rq;
  64. struct rt_rq;
  65. static LIST_HEAD(task_groups);
  66. struct cfs_bandwidth {
  67. #ifdef CONFIG_CFS_BANDWIDTH
  68. raw_spinlock_t lock;
  69. ktime_t period;
  70. u64 quota, runtime;
  71. s64 hierarchal_quota;
  72. u64 runtime_expires;
  73. int idle, timer_active;
  74. struct hrtimer period_timer, slack_timer;
  75. struct list_head throttled_cfs_rq;
  76. /* statistics */
  77. int nr_periods, nr_throttled;
  78. u64 throttled_time;
  79. #endif
  80. };
  81. /* task group related information */
  82. struct task_group {
  83. struct cgroup_subsys_state css;
  84. #ifdef CONFIG_FAIR_GROUP_SCHED
  85. /* schedulable entities of this group on each cpu */
  86. struct sched_entity **se;
  87. /* runqueue "owned" by this group on each cpu */
  88. struct cfs_rq **cfs_rq;
  89. unsigned long shares;
  90. atomic_t load_weight;
  91. #endif
  92. #ifdef CONFIG_RT_GROUP_SCHED
  93. struct sched_rt_entity **rt_se;
  94. struct rt_rq **rt_rq;
  95. struct rt_bandwidth rt_bandwidth;
  96. #endif
  97. struct rcu_head rcu;
  98. struct list_head list;
  99. struct task_group *parent;
  100. struct list_head siblings;
  101. struct list_head children;
  102. #ifdef CONFIG_SCHED_AUTOGROUP
  103. struct autogroup *autogroup;
  104. #endif
  105. struct cfs_bandwidth cfs_bandwidth;
  106. };
  107. #ifdef CONFIG_FAIR_GROUP_SCHED
  108. #define ROOT_TASK_GROUP_LOAD NICE_0_LOAD
  109. /*
  110. * A weight of 0 or 1 can cause arithmetics problems.
  111. * A weight of a cfs_rq is the sum of weights of which entities
  112. * are queued on this cfs_rq, so a weight of a entity should not be
  113. * too large, so as the shares value of a task group.
  114. * (The default weight is 1024 - so there's no practical
  115. * limitation from this.)
  116. */
  117. #define MIN_SHARES (1UL << 1)
  118. #define MAX_SHARES (1UL << 18)
  119. #endif
  120. /* Default task group.
  121. * Every task in system belong to this group at bootup.
  122. */
  123. extern struct task_group root_task_group;
  124. typedef int (*tg_visitor)(struct task_group *, void *);
  125. extern int walk_tg_tree_from(struct task_group *from,
  126. tg_visitor down, tg_visitor up, void *data);
  127. /*
  128. * Iterate the full tree, calling @down when first entering a node and @up when
  129. * leaving it for the final time.
  130. *
  131. * Caller must hold rcu_lock or sufficient equivalent.
  132. */
  133. static inline int walk_tg_tree(tg_visitor down, tg_visitor up, void *data)
  134. {
  135. return walk_tg_tree_from(&root_task_group, down, up, data);
  136. }
  137. extern int tg_nop(struct task_group *tg, void *data);
  138. extern void free_fair_sched_group(struct task_group *tg);
  139. extern int alloc_fair_sched_group(struct task_group *tg, struct task_group *parent);
  140. extern void unregister_fair_sched_group(struct task_group *tg, int cpu);
  141. extern void init_tg_cfs_entry(struct task_group *tg, struct cfs_rq *cfs_rq,
  142. struct sched_entity *se, int cpu,
  143. struct sched_entity *parent);
  144. extern void init_cfs_bandwidth(struct cfs_bandwidth *cfs_b);
  145. extern int sched_group_set_shares(struct task_group *tg, unsigned long shares);
  146. extern void __refill_cfs_bandwidth_runtime(struct cfs_bandwidth *cfs_b);
  147. extern void __start_cfs_bandwidth(struct cfs_bandwidth *cfs_b);
  148. extern void unthrottle_cfs_rq(struct cfs_rq *cfs_rq);
  149. extern void free_rt_sched_group(struct task_group *tg);
  150. extern int alloc_rt_sched_group(struct task_group *tg, struct task_group *parent);
  151. extern void init_tg_rt_entry(struct task_group *tg, struct rt_rq *rt_rq,
  152. struct sched_rt_entity *rt_se, int cpu,
  153. struct sched_rt_entity *parent);
  154. #else /* CONFIG_CGROUP_SCHED */
  155. struct cfs_bandwidth { };
  156. #endif /* CONFIG_CGROUP_SCHED */
  157. /* CFS-related fields in a runqueue */
  158. struct cfs_rq {
  159. struct load_weight load;
  160. unsigned long nr_running, h_nr_running;
  161. u64 exec_clock;
  162. u64 min_vruntime;
  163. #ifndef CONFIG_64BIT
  164. u64 min_vruntime_copy;
  165. #endif
  166. struct rb_root tasks_timeline;
  167. struct rb_node *rb_leftmost;
  168. struct list_head tasks;
  169. struct list_head *balance_iterator;
  170. /*
  171. * 'curr' points to currently running entity on this cfs_rq.
  172. * It is set to NULL otherwise (i.e when none are currently running).
  173. */
  174. struct sched_entity *curr, *next, *last, *skip;
  175. #ifdef CONFIG_SCHED_DEBUG
  176. unsigned int nr_spread_over;
  177. #endif
  178. #ifdef CONFIG_FAIR_GROUP_SCHED
  179. struct rq *rq; /* cpu runqueue to which this cfs_rq is attached */
  180. /*
  181. * leaf cfs_rqs are those that hold tasks (lowest schedulable entity in
  182. * a hierarchy). Non-leaf lrqs hold other higher schedulable entities
  183. * (like users, containers etc.)
  184. *
  185. * leaf_cfs_rq_list ties together list of leaf cfs_rq's in a cpu. This
  186. * list is used during load balance.
  187. */
  188. int on_list;
  189. struct list_head leaf_cfs_rq_list;
  190. struct task_group *tg; /* group that "owns" this runqueue */
  191. #ifdef CONFIG_SMP
  192. /*
  193. * the part of load.weight contributed by tasks
  194. */
  195. unsigned long task_weight;
  196. /*
  197. * h_load = weight * f(tg)
  198. *
  199. * Where f(tg) is the recursive weight fraction assigned to
  200. * this group.
  201. */
  202. unsigned long h_load;
  203. /*
  204. * Maintaining per-cpu shares distribution for group scheduling
  205. *
  206. * load_stamp is the last time we updated the load average
  207. * load_last is the last time we updated the load average and saw load
  208. * load_unacc_exec_time is currently unaccounted execution time
  209. */
  210. u64 load_avg;
  211. u64 load_period;
  212. u64 load_stamp, load_last, load_unacc_exec_time;
  213. unsigned long load_contribution;
  214. #endif /* CONFIG_SMP */
  215. #ifdef CONFIG_CFS_BANDWIDTH
  216. int runtime_enabled;
  217. u64 runtime_expires;
  218. s64 runtime_remaining;
  219. u64 throttled_timestamp;
  220. int throttled, throttle_count;
  221. struct list_head throttled_list;
  222. #endif /* CONFIG_CFS_BANDWIDTH */
  223. #endif /* CONFIG_FAIR_GROUP_SCHED */
  224. };
  225. static inline int rt_bandwidth_enabled(void)
  226. {
  227. return sysctl_sched_rt_runtime >= 0;
  228. }
  229. /* Real-Time classes' related field in a runqueue: */
  230. struct rt_rq {
  231. struct rt_prio_array active;
  232. unsigned long rt_nr_running;
  233. #if defined CONFIG_SMP || defined CONFIG_RT_GROUP_SCHED
  234. struct {
  235. int curr; /* highest queued rt task prio */
  236. #ifdef CONFIG_SMP
  237. int next; /* next highest */
  238. #endif
  239. } highest_prio;
  240. #endif
  241. #ifdef CONFIG_SMP
  242. unsigned long rt_nr_migratory;
  243. unsigned long rt_nr_total;
  244. int overloaded;
  245. struct plist_head pushable_tasks;
  246. #endif
  247. int rt_throttled;
  248. u64 rt_time;
  249. u64 rt_runtime;
  250. /* Nests inside the rq lock: */
  251. raw_spinlock_t rt_runtime_lock;
  252. #ifdef CONFIG_RT_GROUP_SCHED
  253. unsigned long rt_nr_boosted;
  254. struct rq *rq;
  255. struct list_head leaf_rt_rq_list;
  256. struct task_group *tg;
  257. #endif
  258. };
  259. #ifdef CONFIG_SMP
  260. /*
  261. * We add the notion of a root-domain which will be used to define per-domain
  262. * variables. Each exclusive cpuset essentially defines an island domain by
  263. * fully partitioning the member cpus from any other cpuset. Whenever a new
  264. * exclusive cpuset is created, we also create and attach a new root-domain
  265. * object.
  266. *
  267. */
  268. struct root_domain {
  269. atomic_t refcount;
  270. atomic_t rto_count;
  271. struct rcu_head rcu;
  272. cpumask_var_t span;
  273. cpumask_var_t online;
  274. /*
  275. * The "RT overload" flag: it gets set if a CPU has more than
  276. * one runnable RT task.
  277. */
  278. cpumask_var_t rto_mask;
  279. struct cpupri cpupri;
  280. };
  281. extern struct root_domain def_root_domain;
  282. #endif /* CONFIG_SMP */
  283. /*
  284. * This is the main, per-CPU runqueue data structure.
  285. *
  286. * Locking rule: those places that want to lock multiple runqueues
  287. * (such as the load balancing or the thread migration code), lock
  288. * acquire operations must be ordered by ascending &runqueue.
  289. */
  290. struct rq {
  291. /* runqueue lock: */
  292. raw_spinlock_t lock;
  293. /*
  294. * nr_running and cpu_load should be in the same cacheline because
  295. * remote CPUs use both these fields when doing load calculation.
  296. */
  297. unsigned long nr_running;
  298. #define CPU_LOAD_IDX_MAX 5
  299. unsigned long cpu_load[CPU_LOAD_IDX_MAX];
  300. unsigned long last_load_update_tick;
  301. #ifdef CONFIG_NO_HZ
  302. u64 nohz_stamp;
  303. unsigned long nohz_flags;
  304. #endif
  305. int skip_clock_update;
  306. /* capture load from *all* tasks on this cpu: */
  307. struct load_weight load;
  308. unsigned long nr_load_updates;
  309. u64 nr_switches;
  310. struct cfs_rq cfs;
  311. struct rt_rq rt;
  312. #ifdef CONFIG_FAIR_GROUP_SCHED
  313. /* list of leaf cfs_rq on this cpu: */
  314. struct list_head leaf_cfs_rq_list;
  315. #endif
  316. #ifdef CONFIG_RT_GROUP_SCHED
  317. struct list_head leaf_rt_rq_list;
  318. #endif
  319. /*
  320. * This is part of a global counter where only the total sum
  321. * over all CPUs matters. A task can increase this counter on
  322. * one CPU and if it got migrated afterwards it may decrease
  323. * it on another CPU. Always updated under the runqueue lock:
  324. */
  325. unsigned long nr_uninterruptible;
  326. struct task_struct *curr, *idle, *stop;
  327. unsigned long next_balance;
  328. struct mm_struct *prev_mm;
  329. u64 clock;
  330. u64 clock_task;
  331. atomic_t nr_iowait;
  332. #ifdef CONFIG_SMP
  333. struct root_domain *rd;
  334. struct sched_domain *sd;
  335. unsigned long cpu_power;
  336. unsigned char idle_balance;
  337. /* For active balancing */
  338. int post_schedule;
  339. int active_balance;
  340. int push_cpu;
  341. struct cpu_stop_work active_balance_work;
  342. /* cpu of this runqueue: */
  343. int cpu;
  344. int online;
  345. u64 rt_avg;
  346. u64 age_stamp;
  347. u64 idle_stamp;
  348. u64 avg_idle;
  349. #endif
  350. #ifdef CONFIG_IRQ_TIME_ACCOUNTING
  351. u64 prev_irq_time;
  352. #endif
  353. #ifdef CONFIG_PARAVIRT
  354. u64 prev_steal_time;
  355. #endif
  356. #ifdef CONFIG_PARAVIRT_TIME_ACCOUNTING
  357. u64 prev_steal_time_rq;
  358. #endif
  359. /* calc_load related fields */
  360. unsigned long calc_load_update;
  361. long calc_load_active;
  362. #ifdef CONFIG_SCHED_HRTICK
  363. #ifdef CONFIG_SMP
  364. int hrtick_csd_pending;
  365. struct call_single_data hrtick_csd;
  366. #endif
  367. struct hrtimer hrtick_timer;
  368. #endif
  369. #ifdef CONFIG_SCHEDSTATS
  370. /* latency stats */
  371. struct sched_info rq_sched_info;
  372. unsigned long long rq_cpu_time;
  373. /* could above be rq->cfs_rq.exec_clock + rq->rt_rq.rt_runtime ? */
  374. /* sys_sched_yield() stats */
  375. unsigned int yld_count;
  376. /* schedule() stats */
  377. unsigned int sched_count;
  378. unsigned int sched_goidle;
  379. /* try_to_wake_up() stats */
  380. unsigned int ttwu_count;
  381. unsigned int ttwu_local;
  382. #endif
  383. #ifdef CONFIG_SMP
  384. struct llist_head wake_list;
  385. #endif
  386. };
  387. static inline int cpu_of(struct rq *rq)
  388. {
  389. #ifdef CONFIG_SMP
  390. return rq->cpu;
  391. #else
  392. return 0;
  393. #endif
  394. }
  395. DECLARE_PER_CPU(struct rq, runqueues);
  396. #define cpu_rq(cpu) (&per_cpu(runqueues, (cpu)))
  397. #define this_rq() (&__get_cpu_var(runqueues))
  398. #define task_rq(p) cpu_rq(task_cpu(p))
  399. #define cpu_curr(cpu) (cpu_rq(cpu)->curr)
  400. #define raw_rq() (&__raw_get_cpu_var(runqueues))
  401. #ifdef CONFIG_SMP
  402. #define rcu_dereference_check_sched_domain(p) \
  403. rcu_dereference_check((p), \
  404. lockdep_is_held(&sched_domains_mutex))
  405. /*
  406. * The domain tree (rq->sd) is protected by RCU's quiescent state transition.
  407. * See detach_destroy_domains: synchronize_sched for details.
  408. *
  409. * The domain tree of any CPU may only be accessed from within
  410. * preempt-disabled sections.
  411. */
  412. #define for_each_domain(cpu, __sd) \
  413. for (__sd = rcu_dereference_check_sched_domain(cpu_rq(cpu)->sd); \
  414. __sd; __sd = __sd->parent)
  415. #define for_each_lower_domain(sd) for (; sd; sd = sd->child)
  416. /**
  417. * highest_flag_domain - Return highest sched_domain containing flag.
  418. * @cpu: The cpu whose highest level of sched domain is to
  419. * be returned.
  420. * @flag: The flag to check for the highest sched_domain
  421. * for the given cpu.
  422. *
  423. * Returns the highest sched_domain of a cpu which contains the given flag.
  424. */
  425. static inline struct sched_domain *highest_flag_domain(int cpu, int flag)
  426. {
  427. struct sched_domain *sd, *hsd = NULL;
  428. for_each_domain(cpu, sd) {
  429. if (!(sd->flags & flag))
  430. break;
  431. hsd = sd;
  432. }
  433. return hsd;
  434. }
  435. DECLARE_PER_CPU(struct sched_domain *, sd_llc);
  436. DECLARE_PER_CPU(int, sd_llc_id);
  437. #endif /* CONFIG_SMP */
  438. #include "stats.h"
  439. #include "auto_group.h"
  440. #ifdef CONFIG_CGROUP_SCHED
  441. /*
  442. * Return the group to which this tasks belongs.
  443. *
  444. * We use task_subsys_state_check() and extend the RCU verification with
  445. * pi->lock and rq->lock because cpu_cgroup_attach() holds those locks for each
  446. * task it moves into the cgroup. Therefore by holding either of those locks,
  447. * we pin the task to the current cgroup.
  448. */
  449. static inline struct task_group *task_group(struct task_struct *p)
  450. {
  451. struct task_group *tg;
  452. struct cgroup_subsys_state *css;
  453. css = task_subsys_state_check(p, cpu_cgroup_subsys_id,
  454. lockdep_is_held(&p->pi_lock) ||
  455. lockdep_is_held(&task_rq(p)->lock));
  456. tg = container_of(css, struct task_group, css);
  457. return autogroup_task_group(p, tg);
  458. }
  459. /* Change a task's cfs_rq and parent entity if it moves across CPUs/groups */
  460. static inline void set_task_rq(struct task_struct *p, unsigned int cpu)
  461. {
  462. #if defined(CONFIG_FAIR_GROUP_SCHED) || defined(CONFIG_RT_GROUP_SCHED)
  463. struct task_group *tg = task_group(p);
  464. #endif
  465. #ifdef CONFIG_FAIR_GROUP_SCHED
  466. p->se.cfs_rq = tg->cfs_rq[cpu];
  467. p->se.parent = tg->se[cpu];
  468. #endif
  469. #ifdef CONFIG_RT_GROUP_SCHED
  470. p->rt.rt_rq = tg->rt_rq[cpu];
  471. p->rt.parent = tg->rt_se[cpu];
  472. #endif
  473. }
  474. #else /* CONFIG_CGROUP_SCHED */
  475. static inline void set_task_rq(struct task_struct *p, unsigned int cpu) { }
  476. static inline struct task_group *task_group(struct task_struct *p)
  477. {
  478. return NULL;
  479. }
  480. #endif /* CONFIG_CGROUP_SCHED */
  481. static inline void __set_task_cpu(struct task_struct *p, unsigned int cpu)
  482. {
  483. set_task_rq(p, cpu);
  484. #ifdef CONFIG_SMP
  485. /*
  486. * After ->cpu is set up to a new value, task_rq_lock(p, ...) can be
  487. * successfuly executed on another CPU. We must ensure that updates of
  488. * per-task data have been completed by this moment.
  489. */
  490. smp_wmb();
  491. task_thread_info(p)->cpu = cpu;
  492. #endif
  493. }
  494. /*
  495. * Tunables that become constants when CONFIG_SCHED_DEBUG is off:
  496. */
  497. #ifdef CONFIG_SCHED_DEBUG
  498. # include <linux/jump_label.h>
  499. # define const_debug __read_mostly
  500. #else
  501. # define const_debug const
  502. #endif
  503. extern const_debug unsigned int sysctl_sched_features;
  504. #define SCHED_FEAT(name, enabled) \
  505. __SCHED_FEAT_##name ,
  506. enum {
  507. #include "features.h"
  508. __SCHED_FEAT_NR,
  509. };
  510. #undef SCHED_FEAT
  511. #if defined(CONFIG_SCHED_DEBUG) && defined(HAVE_JUMP_LABEL)
  512. static __always_inline bool static_branch__true(struct jump_label_key *key)
  513. {
  514. return likely(static_branch(key)); /* Not out of line branch. */
  515. }
  516. static __always_inline bool static_branch__false(struct jump_label_key *key)
  517. {
  518. return unlikely(static_branch(key)); /* Out of line branch. */
  519. }
  520. #define SCHED_FEAT(name, enabled) \
  521. static __always_inline bool static_branch_##name(struct jump_label_key *key) \
  522. { \
  523. return static_branch__##enabled(key); \
  524. }
  525. #include "features.h"
  526. #undef SCHED_FEAT
  527. extern struct jump_label_key sched_feat_keys[__SCHED_FEAT_NR];
  528. #define sched_feat(x) (static_branch_##x(&sched_feat_keys[__SCHED_FEAT_##x]))
  529. #else /* !(SCHED_DEBUG && HAVE_JUMP_LABEL) */
  530. #define sched_feat(x) (sysctl_sched_features & (1UL << __SCHED_FEAT_##x))
  531. #endif /* SCHED_DEBUG && HAVE_JUMP_LABEL */
  532. static inline u64 global_rt_period(void)
  533. {
  534. return (u64)sysctl_sched_rt_period * NSEC_PER_USEC;
  535. }
  536. static inline u64 global_rt_runtime(void)
  537. {
  538. if (sysctl_sched_rt_runtime < 0)
  539. return RUNTIME_INF;
  540. return (u64)sysctl_sched_rt_runtime * NSEC_PER_USEC;
  541. }
  542. static inline int task_current(struct rq *rq, struct task_struct *p)
  543. {
  544. return rq->curr == p;
  545. }
  546. static inline int task_running(struct rq *rq, struct task_struct *p)
  547. {
  548. #ifdef CONFIG_SMP
  549. return p->on_cpu;
  550. #else
  551. return task_current(rq, p);
  552. #endif
  553. }
  554. #ifndef prepare_arch_switch
  555. # define prepare_arch_switch(next) do { } while (0)
  556. #endif
  557. #ifndef finish_arch_switch
  558. # define finish_arch_switch(prev) do { } while (0)
  559. #endif
  560. #ifndef __ARCH_WANT_UNLOCKED_CTXSW
  561. static inline void prepare_lock_switch(struct rq *rq, struct task_struct *next)
  562. {
  563. #ifdef CONFIG_SMP
  564. /*
  565. * We can optimise this out completely for !SMP, because the
  566. * SMP rebalancing from interrupt is the only thing that cares
  567. * here.
  568. */
  569. next->on_cpu = 1;
  570. #endif
  571. }
  572. static inline void finish_lock_switch(struct rq *rq, struct task_struct *prev)
  573. {
  574. #ifdef CONFIG_SMP
  575. /*
  576. * After ->on_cpu is cleared, the task can be moved to a different CPU.
  577. * We must ensure this doesn't happen until the switch is completely
  578. * finished.
  579. */
  580. smp_wmb();
  581. prev->on_cpu = 0;
  582. #endif
  583. #ifdef CONFIG_DEBUG_SPINLOCK
  584. /* this is a valid case when another task releases the spinlock */
  585. rq->lock.owner = current;
  586. #endif
  587. /*
  588. * If we are tracking spinlock dependencies then we have to
  589. * fix up the runqueue lock - which gets 'carried over' from
  590. * prev into current:
  591. */
  592. spin_acquire(&rq->lock.dep_map, 0, 0, _THIS_IP_);
  593. raw_spin_unlock_irq(&rq->lock);
  594. }
  595. #else /* __ARCH_WANT_UNLOCKED_CTXSW */
  596. static inline void prepare_lock_switch(struct rq *rq, struct task_struct *next)
  597. {
  598. #ifdef CONFIG_SMP
  599. /*
  600. * We can optimise this out completely for !SMP, because the
  601. * SMP rebalancing from interrupt is the only thing that cares
  602. * here.
  603. */
  604. next->on_cpu = 1;
  605. #endif
  606. #ifdef __ARCH_WANT_INTERRUPTS_ON_CTXSW
  607. raw_spin_unlock_irq(&rq->lock);
  608. #else
  609. raw_spin_unlock(&rq->lock);
  610. #endif
  611. }
  612. static inline void finish_lock_switch(struct rq *rq, struct task_struct *prev)
  613. {
  614. #ifdef CONFIG_SMP
  615. /*
  616. * After ->on_cpu is cleared, the task can be moved to a different CPU.
  617. * We must ensure this doesn't happen until the switch is completely
  618. * finished.
  619. */
  620. smp_wmb();
  621. prev->on_cpu = 0;
  622. #endif
  623. #ifndef __ARCH_WANT_INTERRUPTS_ON_CTXSW
  624. local_irq_enable();
  625. #endif
  626. }
  627. #endif /* __ARCH_WANT_UNLOCKED_CTXSW */
  628. static inline void update_load_add(struct load_weight *lw, unsigned long inc)
  629. {
  630. lw->weight += inc;
  631. lw->inv_weight = 0;
  632. }
  633. static inline void update_load_sub(struct load_weight *lw, unsigned long dec)
  634. {
  635. lw->weight -= dec;
  636. lw->inv_weight = 0;
  637. }
  638. static inline void update_load_set(struct load_weight *lw, unsigned long w)
  639. {
  640. lw->weight = w;
  641. lw->inv_weight = 0;
  642. }
  643. /*
  644. * To aid in avoiding the subversion of "niceness" due to uneven distribution
  645. * of tasks with abnormal "nice" values across CPUs the contribution that
  646. * each task makes to its run queue's load is weighted according to its
  647. * scheduling class and "nice" value. For SCHED_NORMAL tasks this is just a
  648. * scaled version of the new time slice allocation that they receive on time
  649. * slice expiry etc.
  650. */
  651. #define WEIGHT_IDLEPRIO 3
  652. #define WMULT_IDLEPRIO 1431655765
  653. /*
  654. * Nice levels are multiplicative, with a gentle 10% change for every
  655. * nice level changed. I.e. when a CPU-bound task goes from nice 0 to
  656. * nice 1, it will get ~10% less CPU time than another CPU-bound task
  657. * that remained on nice 0.
  658. *
  659. * The "10% effect" is relative and cumulative: from _any_ nice level,
  660. * if you go up 1 level, it's -10% CPU usage, if you go down 1 level
  661. * it's +10% CPU usage. (to achieve that we use a multiplier of 1.25.
  662. * If a task goes up by ~10% and another task goes down by ~10% then
  663. * the relative distance between them is ~25%.)
  664. */
  665. static const int prio_to_weight[40] = {
  666. /* -20 */ 88761, 71755, 56483, 46273, 36291,
  667. /* -15 */ 29154, 23254, 18705, 14949, 11916,
  668. /* -10 */ 9548, 7620, 6100, 4904, 3906,
  669. /* -5 */ 3121, 2501, 1991, 1586, 1277,
  670. /* 0 */ 1024, 820, 655, 526, 423,
  671. /* 5 */ 335, 272, 215, 172, 137,
  672. /* 10 */ 110, 87, 70, 56, 45,
  673. /* 15 */ 36, 29, 23, 18, 15,
  674. };
  675. /*
  676. * Inverse (2^32/x) values of the prio_to_weight[] array, precalculated.
  677. *
  678. * In cases where the weight does not change often, we can use the
  679. * precalculated inverse to speed up arithmetics by turning divisions
  680. * into multiplications:
  681. */
  682. static const u32 prio_to_wmult[40] = {
  683. /* -20 */ 48388, 59856, 76040, 92818, 118348,
  684. /* -15 */ 147320, 184698, 229616, 287308, 360437,
  685. /* -10 */ 449829, 563644, 704093, 875809, 1099582,
  686. /* -5 */ 1376151, 1717300, 2157191, 2708050, 3363326,
  687. /* 0 */ 4194304, 5237765, 6557202, 8165337, 10153587,
  688. /* 5 */ 12820798, 15790321, 19976592, 24970740, 31350126,
  689. /* 10 */ 39045157, 49367440, 61356676, 76695844, 95443717,
  690. /* 15 */ 119304647, 148102320, 186737708, 238609294, 286331153,
  691. };
  692. /* Time spent by the tasks of the cpu accounting group executing in ... */
  693. enum cpuacct_stat_index {
  694. CPUACCT_STAT_USER, /* ... user mode */
  695. CPUACCT_STAT_SYSTEM, /* ... kernel mode */
  696. CPUACCT_STAT_NSTATS,
  697. };
  698. #define sched_class_highest (&stop_sched_class)
  699. #define for_each_class(class) \
  700. for (class = sched_class_highest; class; class = class->next)
  701. extern const struct sched_class stop_sched_class;
  702. extern const struct sched_class rt_sched_class;
  703. extern const struct sched_class fair_sched_class;
  704. extern const struct sched_class idle_sched_class;
  705. #ifdef CONFIG_SMP
  706. extern void trigger_load_balance(struct rq *rq, int cpu);
  707. extern void idle_balance(int this_cpu, struct rq *this_rq);
  708. #else /* CONFIG_SMP */
  709. static inline void idle_balance(int cpu, struct rq *rq)
  710. {
  711. }
  712. #endif
  713. extern void sysrq_sched_debug_show(void);
  714. extern void sched_init_granularity(void);
  715. extern void update_max_interval(void);
  716. extern void update_group_power(struct sched_domain *sd, int cpu);
  717. extern int update_runtime(struct notifier_block *nfb, unsigned long action, void *hcpu);
  718. extern void init_sched_rt_class(void);
  719. extern void init_sched_fair_class(void);
  720. extern void resched_task(struct task_struct *p);
  721. extern void resched_cpu(int cpu);
  722. extern struct rt_bandwidth def_rt_bandwidth;
  723. extern void init_rt_bandwidth(struct rt_bandwidth *rt_b, u64 period, u64 runtime);
  724. extern void update_cpu_load(struct rq *this_rq);
  725. #ifdef CONFIG_CGROUP_CPUACCT
  726. #include <linux/cgroup.h>
  727. /* track cpu usage of a group of tasks and its child groups */
  728. struct cpuacct {
  729. struct cgroup_subsys_state css;
  730. /* cpuusage holds pointer to a u64-type object on every cpu */
  731. u64 __percpu *cpuusage;
  732. struct kernel_cpustat __percpu *cpustat;
  733. };
  734. /* return cpu accounting group corresponding to this container */
  735. static inline struct cpuacct *cgroup_ca(struct cgroup *cgrp)
  736. {
  737. return container_of(cgroup_subsys_state(cgrp, cpuacct_subsys_id),
  738. struct cpuacct, css);
  739. }
  740. /* return cpu accounting group to which this task belongs */
  741. static inline struct cpuacct *task_ca(struct task_struct *tsk)
  742. {
  743. return container_of(task_subsys_state(tsk, cpuacct_subsys_id),
  744. struct cpuacct, css);
  745. }
  746. static inline struct cpuacct *parent_ca(struct cpuacct *ca)
  747. {
  748. if (!ca || !ca->css.cgroup->parent)
  749. return NULL;
  750. return cgroup_ca(ca->css.cgroup->parent);
  751. }
  752. extern void cpuacct_charge(struct task_struct *tsk, u64 cputime);
  753. #else
  754. static inline void cpuacct_charge(struct task_struct *tsk, u64 cputime) {}
  755. #endif
  756. static inline void inc_nr_running(struct rq *rq)
  757. {
  758. rq->nr_running++;
  759. }
  760. static inline void dec_nr_running(struct rq *rq)
  761. {
  762. rq->nr_running--;
  763. }
  764. extern void update_rq_clock(struct rq *rq);
  765. extern void activate_task(struct rq *rq, struct task_struct *p, int flags);
  766. extern void deactivate_task(struct rq *rq, struct task_struct *p, int flags);
  767. extern void check_preempt_curr(struct rq *rq, struct task_struct *p, int flags);
  768. extern const_debug unsigned int sysctl_sched_time_avg;
  769. extern const_debug unsigned int sysctl_sched_nr_migrate;
  770. extern const_debug unsigned int sysctl_sched_migration_cost;
  771. static inline u64 sched_avg_period(void)
  772. {
  773. return (u64)sysctl_sched_time_avg * NSEC_PER_MSEC / 2;
  774. }
  775. void calc_load_account_idle(struct rq *this_rq);
  776. #ifdef CONFIG_SCHED_HRTICK
  777. /*
  778. * Use hrtick when:
  779. * - enabled by features
  780. * - hrtimer is actually high res
  781. */
  782. static inline int hrtick_enabled(struct rq *rq)
  783. {
  784. if (!sched_feat(HRTICK))
  785. return 0;
  786. if (!cpu_active(cpu_of(rq)))
  787. return 0;
  788. return hrtimer_is_hres_active(&rq->hrtick_timer);
  789. }
  790. void hrtick_start(struct rq *rq, u64 delay);
  791. #else
  792. static inline int hrtick_enabled(struct rq *rq)
  793. {
  794. return 0;
  795. }
  796. #endif /* CONFIG_SCHED_HRTICK */
  797. #ifdef CONFIG_SMP
  798. extern void sched_avg_update(struct rq *rq);
  799. static inline void sched_rt_avg_update(struct rq *rq, u64 rt_delta)
  800. {
  801. rq->rt_avg += rt_delta;
  802. sched_avg_update(rq);
  803. }
  804. #else
  805. static inline void sched_rt_avg_update(struct rq *rq, u64 rt_delta) { }
  806. static inline void sched_avg_update(struct rq *rq) { }
  807. #endif
  808. extern void start_bandwidth_timer(struct hrtimer *period_timer, ktime_t period);
  809. #ifdef CONFIG_SMP
  810. #ifdef CONFIG_PREEMPT
  811. static inline void double_rq_lock(struct rq *rq1, struct rq *rq2);
  812. /*
  813. * fair double_lock_balance: Safely acquires both rq->locks in a fair
  814. * way at the expense of forcing extra atomic operations in all
  815. * invocations. This assures that the double_lock is acquired using the
  816. * same underlying policy as the spinlock_t on this architecture, which
  817. * reduces latency compared to the unfair variant below. However, it
  818. * also adds more overhead and therefore may reduce throughput.
  819. */
  820. static inline int _double_lock_balance(struct rq *this_rq, struct rq *busiest)
  821. __releases(this_rq->lock)
  822. __acquires(busiest->lock)
  823. __acquires(this_rq->lock)
  824. {
  825. raw_spin_unlock(&this_rq->lock);
  826. double_rq_lock(this_rq, busiest);
  827. return 1;
  828. }
  829. #else
  830. /*
  831. * Unfair double_lock_balance: Optimizes throughput at the expense of
  832. * latency by eliminating extra atomic operations when the locks are
  833. * already in proper order on entry. This favors lower cpu-ids and will
  834. * grant the double lock to lower cpus over higher ids under contention,
  835. * regardless of entry order into the function.
  836. */
  837. static inline int _double_lock_balance(struct rq *this_rq, struct rq *busiest)
  838. __releases(this_rq->lock)
  839. __acquires(busiest->lock)
  840. __acquires(this_rq->lock)
  841. {
  842. int ret = 0;
  843. if (unlikely(!raw_spin_trylock(&busiest->lock))) {
  844. if (busiest < this_rq) {
  845. raw_spin_unlock(&this_rq->lock);
  846. raw_spin_lock(&busiest->lock);
  847. raw_spin_lock_nested(&this_rq->lock,
  848. SINGLE_DEPTH_NESTING);
  849. ret = 1;
  850. } else
  851. raw_spin_lock_nested(&busiest->lock,
  852. SINGLE_DEPTH_NESTING);
  853. }
  854. return ret;
  855. }
  856. #endif /* CONFIG_PREEMPT */
  857. /*
  858. * double_lock_balance - lock the busiest runqueue, this_rq is locked already.
  859. */
  860. static inline int double_lock_balance(struct rq *this_rq, struct rq *busiest)
  861. {
  862. if (unlikely(!irqs_disabled())) {
  863. /* printk() doesn't work good under rq->lock */
  864. raw_spin_unlock(&this_rq->lock);
  865. BUG_ON(1);
  866. }
  867. return _double_lock_balance(this_rq, busiest);
  868. }
  869. static inline void double_unlock_balance(struct rq *this_rq, struct rq *busiest)
  870. __releases(busiest->lock)
  871. {
  872. raw_spin_unlock(&busiest->lock);
  873. lock_set_subclass(&this_rq->lock.dep_map, 0, _RET_IP_);
  874. }
  875. /*
  876. * double_rq_lock - safely lock two runqueues
  877. *
  878. * Note this does not disable interrupts like task_rq_lock,
  879. * you need to do so manually before calling.
  880. */
  881. static inline void double_rq_lock(struct rq *rq1, struct rq *rq2)
  882. __acquires(rq1->lock)
  883. __acquires(rq2->lock)
  884. {
  885. BUG_ON(!irqs_disabled());
  886. if (rq1 == rq2) {
  887. raw_spin_lock(&rq1->lock);
  888. __acquire(rq2->lock); /* Fake it out ;) */
  889. } else {
  890. if (rq1 < rq2) {
  891. raw_spin_lock(&rq1->lock);
  892. raw_spin_lock_nested(&rq2->lock, SINGLE_DEPTH_NESTING);
  893. } else {
  894. raw_spin_lock(&rq2->lock);
  895. raw_spin_lock_nested(&rq1->lock, SINGLE_DEPTH_NESTING);
  896. }
  897. }
  898. }
  899. /*
  900. * double_rq_unlock - safely unlock two runqueues
  901. *
  902. * Note this does not restore interrupts like task_rq_unlock,
  903. * you need to do so manually after calling.
  904. */
  905. static inline void double_rq_unlock(struct rq *rq1, struct rq *rq2)
  906. __releases(rq1->lock)
  907. __releases(rq2->lock)
  908. {
  909. raw_spin_unlock(&rq1->lock);
  910. if (rq1 != rq2)
  911. raw_spin_unlock(&rq2->lock);
  912. else
  913. __release(rq2->lock);
  914. }
  915. #else /* CONFIG_SMP */
  916. /*
  917. * double_rq_lock - safely lock two runqueues
  918. *
  919. * Note this does not disable interrupts like task_rq_lock,
  920. * you need to do so manually before calling.
  921. */
  922. static inline void double_rq_lock(struct rq *rq1, struct rq *rq2)
  923. __acquires(rq1->lock)
  924. __acquires(rq2->lock)
  925. {
  926. BUG_ON(!irqs_disabled());
  927. BUG_ON(rq1 != rq2);
  928. raw_spin_lock(&rq1->lock);
  929. __acquire(rq2->lock); /* Fake it out ;) */
  930. }
  931. /*
  932. * double_rq_unlock - safely unlock two runqueues
  933. *
  934. * Note this does not restore interrupts like task_rq_unlock,
  935. * you need to do so manually after calling.
  936. */
  937. static inline void double_rq_unlock(struct rq *rq1, struct rq *rq2)
  938. __releases(rq1->lock)
  939. __releases(rq2->lock)
  940. {
  941. BUG_ON(rq1 != rq2);
  942. raw_spin_unlock(&rq1->lock);
  943. __release(rq2->lock);
  944. }
  945. #endif
  946. extern struct sched_entity *__pick_first_entity(struct cfs_rq *cfs_rq);
  947. extern struct sched_entity *__pick_last_entity(struct cfs_rq *cfs_rq);
  948. extern void print_cfs_stats(struct seq_file *m, int cpu);
  949. extern void print_rt_stats(struct seq_file *m, int cpu);
  950. extern void init_cfs_rq(struct cfs_rq *cfs_rq);
  951. extern void init_rt_rq(struct rt_rq *rt_rq, struct rq *rq);
  952. extern void unthrottle_offline_cfs_rqs(struct rq *rq);
  953. extern void account_cfs_bandwidth_used(int enabled, int was_enabled);
  954. #ifdef CONFIG_NO_HZ
  955. enum rq_nohz_flag_bits {
  956. NOHZ_TICK_STOPPED,
  957. NOHZ_BALANCE_KICK,
  958. NOHZ_IDLE,
  959. };
  960. #define nohz_flags(cpu) (&cpu_rq(cpu)->nohz_flags)
  961. #endif