sched.h 29 KB

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