sched.h 31 KB

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