sched.h 31 KB

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