sched.h 31 KB

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