sched.h 28 KB

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