sched.h 31 KB

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