sched.h 32 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243
  1. #include <linux/sched.h>
  2. #include <linux/sched/sysctl.h>
  3. #include <linux/sched/rt.h>
  4. #include <linux/mutex.h>
  5. #include <linux/spinlock.h>
  6. #include <linux/stop_machine.h>
  7. #include "cpupri.h"
  8. extern __read_mostly int scheduler_running;
  9. /*
  10. * Convert user-nice values [ -20 ... 0 ... 19 ]
  11. * to static priority [ MAX_RT_PRIO..MAX_PRIO-1 ],
  12. * and back.
  13. */
  14. #define NICE_TO_PRIO(nice) (MAX_RT_PRIO + (nice) + 20)
  15. #define PRIO_TO_NICE(prio) ((prio) - MAX_RT_PRIO - 20)
  16. #define TASK_NICE(p) PRIO_TO_NICE((p)->static_prio)
  17. /*
  18. * 'User priority' is the nice value converted to something we
  19. * can work with better when scaling various scheduler parameters,
  20. * it's a [ 0 ... 39 ] range.
  21. */
  22. #define USER_PRIO(p) ((p)-MAX_RT_PRIO)
  23. #define TASK_USER_PRIO(p) USER_PRIO((p)->static_prio)
  24. #define MAX_USER_PRIO (USER_PRIO(MAX_PRIO))
  25. /*
  26. * Helpers for converting nanosecond timing to jiffy resolution
  27. */
  28. #define NS_TO_JIFFIES(TIME) ((unsigned long)(TIME) / (NSEC_PER_SEC / HZ))
  29. #define NICE_0_LOAD SCHED_LOAD_SCALE
  30. #define NICE_0_SHIFT SCHED_LOAD_SHIFT
  31. /*
  32. * These are the 'tuning knobs' of the scheduler:
  33. */
  34. /*
  35. * single value that denotes runtime == period, ie unlimited time.
  36. */
  37. #define RUNTIME_INF ((u64)~0ULL)
  38. static inline int rt_policy(int policy)
  39. {
  40. if (policy == SCHED_FIFO || policy == SCHED_RR)
  41. return 1;
  42. return 0;
  43. }
  44. static inline int task_has_rt_policy(struct task_struct *p)
  45. {
  46. return rt_policy(p->policy);
  47. }
  48. /*
  49. * This is the priority-queue data structure of the RT scheduling class:
  50. */
  51. struct rt_prio_array {
  52. DECLARE_BITMAP(bitmap, MAX_RT_PRIO+1); /* include 1 bit for delimiter */
  53. struct list_head queue[MAX_RT_PRIO];
  54. };
  55. struct rt_bandwidth {
  56. /* nests inside the rq lock: */
  57. raw_spinlock_t rt_runtime_lock;
  58. ktime_t rt_period;
  59. u64 rt_runtime;
  60. struct hrtimer rt_period_timer;
  61. };
  62. extern struct mutex sched_domains_mutex;
  63. #ifdef CONFIG_CGROUP_SCHED
  64. #include <linux/cgroup.h>
  65. struct cfs_rq;
  66. struct rt_rq;
  67. extern struct list_head task_groups;
  68. struct cfs_bandwidth {
  69. #ifdef CONFIG_CFS_BANDWIDTH
  70. raw_spinlock_t lock;
  71. ktime_t period;
  72. u64 quota, runtime;
  73. s64 hierarchal_quota;
  74. u64 runtime_expires;
  75. int idle, timer_active;
  76. struct hrtimer period_timer, slack_timer;
  77. struct list_head throttled_cfs_rq;
  78. /* statistics */
  79. int nr_periods, nr_throttled;
  80. u64 throttled_time;
  81. #endif
  82. };
  83. /* task group related information */
  84. struct task_group {
  85. struct cgroup_subsys_state css;
  86. #ifdef CONFIG_FAIR_GROUP_SCHED
  87. /* schedulable entities of this group on each cpu */
  88. struct sched_entity **se;
  89. /* runqueue "owned" by this group on each cpu */
  90. struct cfs_rq **cfs_rq;
  91. unsigned long shares;
  92. atomic_t load_weight;
  93. atomic64_t load_avg;
  94. atomic_t runnable_avg;
  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 int 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. /*
  173. * 'curr' points to currently running entity on this cfs_rq.
  174. * It is set to NULL otherwise (i.e when none are currently running).
  175. */
  176. struct sched_entity *curr, *next, *last, *skip;
  177. #ifdef CONFIG_SCHED_DEBUG
  178. unsigned int nr_spread_over;
  179. #endif
  180. #ifdef CONFIG_SMP
  181. /*
  182. * Load-tracking only depends on SMP, FAIR_GROUP_SCHED dependency below may be
  183. * removed when useful for applications beyond shares distribution (e.g.
  184. * load-balance).
  185. */
  186. #ifdef CONFIG_FAIR_GROUP_SCHED
  187. /*
  188. * CFS Load tracking
  189. * Under CFS, load is tracked on a per-entity basis and aggregated up.
  190. * This allows for the description of both thread and group usage (in
  191. * the FAIR_GROUP_SCHED case).
  192. */
  193. u64 runnable_load_avg, blocked_load_avg;
  194. atomic64_t decay_counter, removed_load;
  195. u64 last_decay;
  196. #endif /* CONFIG_FAIR_GROUP_SCHED */
  197. /* These always depend on CONFIG_FAIR_GROUP_SCHED */
  198. #ifdef CONFIG_FAIR_GROUP_SCHED
  199. u32 tg_runnable_contrib;
  200. u64 tg_load_contrib;
  201. #endif /* CONFIG_FAIR_GROUP_SCHED */
  202. /*
  203. * h_load = weight * f(tg)
  204. *
  205. * Where f(tg) is the recursive weight fraction assigned to
  206. * this group.
  207. */
  208. unsigned long h_load;
  209. #endif /* CONFIG_SMP */
  210. #ifdef CONFIG_FAIR_GROUP_SCHED
  211. struct rq *rq; /* cpu runqueue to which this cfs_rq is attached */
  212. /*
  213. * leaf cfs_rqs are those that hold tasks (lowest schedulable entity in
  214. * a hierarchy). Non-leaf lrqs hold other higher schedulable entities
  215. * (like users, containers etc.)
  216. *
  217. * leaf_cfs_rq_list ties together list of leaf cfs_rq's in a cpu. This
  218. * list is used during load balance.
  219. */
  220. int on_list;
  221. struct list_head leaf_cfs_rq_list;
  222. struct task_group *tg; /* group that "owns" this runqueue */
  223. #ifdef CONFIG_CFS_BANDWIDTH
  224. int runtime_enabled;
  225. u64 runtime_expires;
  226. s64 runtime_remaining;
  227. u64 throttled_clock, throttled_clock_task;
  228. u64 throttled_clock_task_time;
  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. #ifdef CONFIG_NUMA_BALANCING
  547. #define sched_feat_numa(x) sched_feat(x)
  548. #ifdef CONFIG_SCHED_DEBUG
  549. #define numabalancing_enabled sched_feat_numa(NUMA)
  550. #else
  551. extern bool numabalancing_enabled;
  552. #endif /* CONFIG_SCHED_DEBUG */
  553. #else
  554. #define sched_feat_numa(x) (0)
  555. #define numabalancing_enabled (0)
  556. #endif /* CONFIG_NUMA_BALANCING */
  557. static inline u64 global_rt_period(void)
  558. {
  559. return (u64)sysctl_sched_rt_period * NSEC_PER_USEC;
  560. }
  561. static inline u64 global_rt_runtime(void)
  562. {
  563. if (sysctl_sched_rt_runtime < 0)
  564. return RUNTIME_INF;
  565. return (u64)sysctl_sched_rt_runtime * NSEC_PER_USEC;
  566. }
  567. static inline int task_current(struct rq *rq, struct task_struct *p)
  568. {
  569. return rq->curr == p;
  570. }
  571. static inline int task_running(struct rq *rq, struct task_struct *p)
  572. {
  573. #ifdef CONFIG_SMP
  574. return p->on_cpu;
  575. #else
  576. return task_current(rq, p);
  577. #endif
  578. }
  579. #ifndef prepare_arch_switch
  580. # define prepare_arch_switch(next) do { } while (0)
  581. #endif
  582. #ifndef finish_arch_switch
  583. # define finish_arch_switch(prev) do { } while (0)
  584. #endif
  585. #ifndef finish_arch_post_lock_switch
  586. # define finish_arch_post_lock_switch() do { } while (0)
  587. #endif
  588. #ifndef __ARCH_WANT_UNLOCKED_CTXSW
  589. static inline void prepare_lock_switch(struct rq *rq, struct task_struct *next)
  590. {
  591. #ifdef CONFIG_SMP
  592. /*
  593. * We can optimise this out completely for !SMP, because the
  594. * SMP rebalancing from interrupt is the only thing that cares
  595. * here.
  596. */
  597. next->on_cpu = 1;
  598. #endif
  599. }
  600. static inline void finish_lock_switch(struct rq *rq, struct task_struct *prev)
  601. {
  602. #ifdef CONFIG_SMP
  603. /*
  604. * After ->on_cpu is cleared, the task can be moved to a different CPU.
  605. * We must ensure this doesn't happen until the switch is completely
  606. * finished.
  607. */
  608. smp_wmb();
  609. prev->on_cpu = 0;
  610. #endif
  611. #ifdef CONFIG_DEBUG_SPINLOCK
  612. /* this is a valid case when another task releases the spinlock */
  613. rq->lock.owner = current;
  614. #endif
  615. /*
  616. * If we are tracking spinlock dependencies then we have to
  617. * fix up the runqueue lock - which gets 'carried over' from
  618. * prev into current:
  619. */
  620. spin_acquire(&rq->lock.dep_map, 0, 0, _THIS_IP_);
  621. raw_spin_unlock_irq(&rq->lock);
  622. }
  623. #else /* __ARCH_WANT_UNLOCKED_CTXSW */
  624. static inline void prepare_lock_switch(struct rq *rq, struct task_struct *next)
  625. {
  626. #ifdef CONFIG_SMP
  627. /*
  628. * We can optimise this out completely for !SMP, because the
  629. * SMP rebalancing from interrupt is the only thing that cares
  630. * here.
  631. */
  632. next->on_cpu = 1;
  633. #endif
  634. raw_spin_unlock(&rq->lock);
  635. }
  636. static inline void finish_lock_switch(struct rq *rq, struct task_struct *prev)
  637. {
  638. #ifdef CONFIG_SMP
  639. /*
  640. * After ->on_cpu is cleared, the task can be moved to a different CPU.
  641. * We must ensure this doesn't happen until the switch is completely
  642. * finished.
  643. */
  644. smp_wmb();
  645. prev->on_cpu = 0;
  646. #endif
  647. local_irq_enable();
  648. }
  649. #endif /* __ARCH_WANT_UNLOCKED_CTXSW */
  650. static inline void update_load_add(struct load_weight *lw, unsigned long inc)
  651. {
  652. lw->weight += inc;
  653. lw->inv_weight = 0;
  654. }
  655. static inline void update_load_sub(struct load_weight *lw, unsigned long dec)
  656. {
  657. lw->weight -= dec;
  658. lw->inv_weight = 0;
  659. }
  660. static inline void update_load_set(struct load_weight *lw, unsigned long w)
  661. {
  662. lw->weight = w;
  663. lw->inv_weight = 0;
  664. }
  665. /*
  666. * To aid in avoiding the subversion of "niceness" due to uneven distribution
  667. * of tasks with abnormal "nice" values across CPUs the contribution that
  668. * each task makes to its run queue's load is weighted according to its
  669. * scheduling class and "nice" value. For SCHED_NORMAL tasks this is just a
  670. * scaled version of the new time slice allocation that they receive on time
  671. * slice expiry etc.
  672. */
  673. #define WEIGHT_IDLEPRIO 3
  674. #define WMULT_IDLEPRIO 1431655765
  675. /*
  676. * Nice levels are multiplicative, with a gentle 10% change for every
  677. * nice level changed. I.e. when a CPU-bound task goes from nice 0 to
  678. * nice 1, it will get ~10% less CPU time than another CPU-bound task
  679. * that remained on nice 0.
  680. *
  681. * The "10% effect" is relative and cumulative: from _any_ nice level,
  682. * if you go up 1 level, it's -10% CPU usage, if you go down 1 level
  683. * it's +10% CPU usage. (to achieve that we use a multiplier of 1.25.
  684. * If a task goes up by ~10% and another task goes down by ~10% then
  685. * the relative distance between them is ~25%.)
  686. */
  687. static const int prio_to_weight[40] = {
  688. /* -20 */ 88761, 71755, 56483, 46273, 36291,
  689. /* -15 */ 29154, 23254, 18705, 14949, 11916,
  690. /* -10 */ 9548, 7620, 6100, 4904, 3906,
  691. /* -5 */ 3121, 2501, 1991, 1586, 1277,
  692. /* 0 */ 1024, 820, 655, 526, 423,
  693. /* 5 */ 335, 272, 215, 172, 137,
  694. /* 10 */ 110, 87, 70, 56, 45,
  695. /* 15 */ 36, 29, 23, 18, 15,
  696. };
  697. /*
  698. * Inverse (2^32/x) values of the prio_to_weight[] array, precalculated.
  699. *
  700. * In cases where the weight does not change often, we can use the
  701. * precalculated inverse to speed up arithmetics by turning divisions
  702. * into multiplications:
  703. */
  704. static const u32 prio_to_wmult[40] = {
  705. /* -20 */ 48388, 59856, 76040, 92818, 118348,
  706. /* -15 */ 147320, 184698, 229616, 287308, 360437,
  707. /* -10 */ 449829, 563644, 704093, 875809, 1099582,
  708. /* -5 */ 1376151, 1717300, 2157191, 2708050, 3363326,
  709. /* 0 */ 4194304, 5237765, 6557202, 8165337, 10153587,
  710. /* 5 */ 12820798, 15790321, 19976592, 24970740, 31350126,
  711. /* 10 */ 39045157, 49367440, 61356676, 76695844, 95443717,
  712. /* 15 */ 119304647, 148102320, 186737708, 238609294, 286331153,
  713. };
  714. /* Time spent by the tasks of the cpu accounting group executing in ... */
  715. enum cpuacct_stat_index {
  716. CPUACCT_STAT_USER, /* ... user mode */
  717. CPUACCT_STAT_SYSTEM, /* ... kernel mode */
  718. CPUACCT_STAT_NSTATS,
  719. };
  720. #define sched_class_highest (&stop_sched_class)
  721. #define for_each_class(class) \
  722. for (class = sched_class_highest; class; class = class->next)
  723. extern const struct sched_class stop_sched_class;
  724. extern const struct sched_class rt_sched_class;
  725. extern const struct sched_class fair_sched_class;
  726. extern const struct sched_class idle_sched_class;
  727. #ifdef CONFIG_SMP
  728. extern void trigger_load_balance(struct rq *rq, int cpu);
  729. extern void idle_balance(int this_cpu, struct rq *this_rq);
  730. #else /* CONFIG_SMP */
  731. static inline void idle_balance(int cpu, struct rq *rq)
  732. {
  733. }
  734. #endif
  735. extern void sysrq_sched_debug_show(void);
  736. extern void sched_init_granularity(void);
  737. extern void update_max_interval(void);
  738. extern void update_group_power(struct sched_domain *sd, int cpu);
  739. extern int update_runtime(struct notifier_block *nfb, unsigned long action, void *hcpu);
  740. extern void init_sched_rt_class(void);
  741. extern void init_sched_fair_class(void);
  742. extern void resched_task(struct task_struct *p);
  743. extern void resched_cpu(int cpu);
  744. extern struct rt_bandwidth def_rt_bandwidth;
  745. extern void init_rt_bandwidth(struct rt_bandwidth *rt_b, u64 period, u64 runtime);
  746. extern void update_idle_cpu_load(struct rq *this_rq);
  747. #ifdef CONFIG_CGROUP_CPUACCT
  748. #include <linux/cgroup.h>
  749. /* track cpu usage of a group of tasks and its child groups */
  750. struct cpuacct {
  751. struct cgroup_subsys_state css;
  752. /* cpuusage holds pointer to a u64-type object on every cpu */
  753. u64 __percpu *cpuusage;
  754. struct kernel_cpustat __percpu *cpustat;
  755. };
  756. extern struct cgroup_subsys cpuacct_subsys;
  757. extern struct cpuacct root_cpuacct;
  758. /* return cpu accounting group corresponding to this container */
  759. static inline struct cpuacct *cgroup_ca(struct cgroup *cgrp)
  760. {
  761. return container_of(cgroup_subsys_state(cgrp, cpuacct_subsys_id),
  762. struct cpuacct, css);
  763. }
  764. /* return cpu accounting group to which this task belongs */
  765. static inline struct cpuacct *task_ca(struct task_struct *tsk)
  766. {
  767. return container_of(task_subsys_state(tsk, cpuacct_subsys_id),
  768. struct cpuacct, css);
  769. }
  770. static inline struct cpuacct *parent_ca(struct cpuacct *ca)
  771. {
  772. if (!ca || !ca->css.cgroup->parent)
  773. return NULL;
  774. return cgroup_ca(ca->css.cgroup->parent);
  775. }
  776. extern void cpuacct_charge(struct task_struct *tsk, u64 cputime);
  777. #else
  778. static inline void cpuacct_charge(struct task_struct *tsk, u64 cputime) {}
  779. #endif
  780. #ifdef CONFIG_PARAVIRT
  781. static inline u64 steal_ticks(u64 steal)
  782. {
  783. if (unlikely(steal > NSEC_PER_SEC))
  784. return div_u64(steal, TICK_NSEC);
  785. return __iter_div_u64_rem(steal, TICK_NSEC, &steal);
  786. }
  787. #endif
  788. static inline void inc_nr_running(struct rq *rq)
  789. {
  790. rq->nr_running++;
  791. }
  792. static inline void dec_nr_running(struct rq *rq)
  793. {
  794. rq->nr_running--;
  795. }
  796. extern void update_rq_clock(struct rq *rq);
  797. extern void activate_task(struct rq *rq, struct task_struct *p, int flags);
  798. extern void deactivate_task(struct rq *rq, struct task_struct *p, int flags);
  799. extern void check_preempt_curr(struct rq *rq, struct task_struct *p, int flags);
  800. extern const_debug unsigned int sysctl_sched_time_avg;
  801. extern const_debug unsigned int sysctl_sched_nr_migrate;
  802. extern const_debug unsigned int sysctl_sched_migration_cost;
  803. static inline u64 sched_avg_period(void)
  804. {
  805. return (u64)sysctl_sched_time_avg * NSEC_PER_MSEC / 2;
  806. }
  807. #ifdef CONFIG_SCHED_HRTICK
  808. /*
  809. * Use hrtick when:
  810. * - enabled by features
  811. * - hrtimer is actually high res
  812. */
  813. static inline int hrtick_enabled(struct rq *rq)
  814. {
  815. if (!sched_feat(HRTICK))
  816. return 0;
  817. if (!cpu_active(cpu_of(rq)))
  818. return 0;
  819. return hrtimer_is_hres_active(&rq->hrtick_timer);
  820. }
  821. void hrtick_start(struct rq *rq, u64 delay);
  822. #else
  823. static inline int hrtick_enabled(struct rq *rq)
  824. {
  825. return 0;
  826. }
  827. #endif /* CONFIG_SCHED_HRTICK */
  828. #ifdef CONFIG_SMP
  829. extern void sched_avg_update(struct rq *rq);
  830. static inline void sched_rt_avg_update(struct rq *rq, u64 rt_delta)
  831. {
  832. rq->rt_avg += rt_delta;
  833. sched_avg_update(rq);
  834. }
  835. #else
  836. static inline void sched_rt_avg_update(struct rq *rq, u64 rt_delta) { }
  837. static inline void sched_avg_update(struct rq *rq) { }
  838. #endif
  839. extern void start_bandwidth_timer(struct hrtimer *period_timer, ktime_t period);
  840. #ifdef CONFIG_SMP
  841. #ifdef CONFIG_PREEMPT
  842. static inline void double_rq_lock(struct rq *rq1, struct rq *rq2);
  843. /*
  844. * fair double_lock_balance: Safely acquires both rq->locks in a fair
  845. * way at the expense of forcing extra atomic operations in all
  846. * invocations. This assures that the double_lock is acquired using the
  847. * same underlying policy as the spinlock_t on this architecture, which
  848. * reduces latency compared to the unfair variant below. However, it
  849. * also adds more overhead and therefore may reduce throughput.
  850. */
  851. static inline int _double_lock_balance(struct rq *this_rq, struct rq *busiest)
  852. __releases(this_rq->lock)
  853. __acquires(busiest->lock)
  854. __acquires(this_rq->lock)
  855. {
  856. raw_spin_unlock(&this_rq->lock);
  857. double_rq_lock(this_rq, busiest);
  858. return 1;
  859. }
  860. #else
  861. /*
  862. * Unfair double_lock_balance: Optimizes throughput at the expense of
  863. * latency by eliminating extra atomic operations when the locks are
  864. * already in proper order on entry. This favors lower cpu-ids and will
  865. * grant the double lock to lower cpus over higher ids under contention,
  866. * regardless of entry order into the function.
  867. */
  868. static inline int _double_lock_balance(struct rq *this_rq, struct rq *busiest)
  869. __releases(this_rq->lock)
  870. __acquires(busiest->lock)
  871. __acquires(this_rq->lock)
  872. {
  873. int ret = 0;
  874. if (unlikely(!raw_spin_trylock(&busiest->lock))) {
  875. if (busiest < this_rq) {
  876. raw_spin_unlock(&this_rq->lock);
  877. raw_spin_lock(&busiest->lock);
  878. raw_spin_lock_nested(&this_rq->lock,
  879. SINGLE_DEPTH_NESTING);
  880. ret = 1;
  881. } else
  882. raw_spin_lock_nested(&busiest->lock,
  883. SINGLE_DEPTH_NESTING);
  884. }
  885. return ret;
  886. }
  887. #endif /* CONFIG_PREEMPT */
  888. /*
  889. * double_lock_balance - lock the busiest runqueue, this_rq is locked already.
  890. */
  891. static inline int double_lock_balance(struct rq *this_rq, struct rq *busiest)
  892. {
  893. if (unlikely(!irqs_disabled())) {
  894. /* printk() doesn't work good under rq->lock */
  895. raw_spin_unlock(&this_rq->lock);
  896. BUG_ON(1);
  897. }
  898. return _double_lock_balance(this_rq, busiest);
  899. }
  900. static inline void double_unlock_balance(struct rq *this_rq, struct rq *busiest)
  901. __releases(busiest->lock)
  902. {
  903. raw_spin_unlock(&busiest->lock);
  904. lock_set_subclass(&this_rq->lock.dep_map, 0, _RET_IP_);
  905. }
  906. /*
  907. * double_rq_lock - safely lock two runqueues
  908. *
  909. * Note this does not disable interrupts like task_rq_lock,
  910. * you need to do so manually before calling.
  911. */
  912. static inline void double_rq_lock(struct rq *rq1, struct rq *rq2)
  913. __acquires(rq1->lock)
  914. __acquires(rq2->lock)
  915. {
  916. BUG_ON(!irqs_disabled());
  917. if (rq1 == rq2) {
  918. raw_spin_lock(&rq1->lock);
  919. __acquire(rq2->lock); /* Fake it out ;) */
  920. } else {
  921. if (rq1 < rq2) {
  922. raw_spin_lock(&rq1->lock);
  923. raw_spin_lock_nested(&rq2->lock, SINGLE_DEPTH_NESTING);
  924. } else {
  925. raw_spin_lock(&rq2->lock);
  926. raw_spin_lock_nested(&rq1->lock, SINGLE_DEPTH_NESTING);
  927. }
  928. }
  929. }
  930. /*
  931. * double_rq_unlock - safely unlock two runqueues
  932. *
  933. * Note this does not restore interrupts like task_rq_unlock,
  934. * you need to do so manually after calling.
  935. */
  936. static inline void double_rq_unlock(struct rq *rq1, struct rq *rq2)
  937. __releases(rq1->lock)
  938. __releases(rq2->lock)
  939. {
  940. raw_spin_unlock(&rq1->lock);
  941. if (rq1 != rq2)
  942. raw_spin_unlock(&rq2->lock);
  943. else
  944. __release(rq2->lock);
  945. }
  946. #else /* CONFIG_SMP */
  947. /*
  948. * double_rq_lock - safely lock two runqueues
  949. *
  950. * Note this does not disable interrupts like task_rq_lock,
  951. * you need to do so manually before calling.
  952. */
  953. static inline void double_rq_lock(struct rq *rq1, struct rq *rq2)
  954. __acquires(rq1->lock)
  955. __acquires(rq2->lock)
  956. {
  957. BUG_ON(!irqs_disabled());
  958. BUG_ON(rq1 != rq2);
  959. raw_spin_lock(&rq1->lock);
  960. __acquire(rq2->lock); /* Fake it out ;) */
  961. }
  962. /*
  963. * double_rq_unlock - safely unlock two runqueues
  964. *
  965. * Note this does not restore interrupts like task_rq_unlock,
  966. * you need to do so manually after calling.
  967. */
  968. static inline void double_rq_unlock(struct rq *rq1, struct rq *rq2)
  969. __releases(rq1->lock)
  970. __releases(rq2->lock)
  971. {
  972. BUG_ON(rq1 != rq2);
  973. raw_spin_unlock(&rq1->lock);
  974. __release(rq2->lock);
  975. }
  976. #endif
  977. extern struct sched_entity *__pick_first_entity(struct cfs_rq *cfs_rq);
  978. extern struct sched_entity *__pick_last_entity(struct cfs_rq *cfs_rq);
  979. extern void print_cfs_stats(struct seq_file *m, int cpu);
  980. extern void print_rt_stats(struct seq_file *m, int cpu);
  981. extern void init_cfs_rq(struct cfs_rq *cfs_rq);
  982. extern void init_rt_rq(struct rt_rq *rt_rq, struct rq *rq);
  983. extern void account_cfs_bandwidth_used(int enabled, int was_enabled);
  984. #ifdef CONFIG_NO_HZ
  985. enum rq_nohz_flag_bits {
  986. NOHZ_TICK_STOPPED,
  987. NOHZ_BALANCE_KICK,
  988. NOHZ_IDLE,
  989. };
  990. #define nohz_flags(cpu) (&cpu_rq(cpu)->nohz_flags)
  991. #endif
  992. #ifdef CONFIG_IRQ_TIME_ACCOUNTING
  993. DECLARE_PER_CPU(u64, cpu_hardirq_time);
  994. DECLARE_PER_CPU(u64, cpu_softirq_time);
  995. #ifndef CONFIG_64BIT
  996. DECLARE_PER_CPU(seqcount_t, irq_time_seq);
  997. static inline void irq_time_write_begin(void)
  998. {
  999. __this_cpu_inc(irq_time_seq.sequence);
  1000. smp_wmb();
  1001. }
  1002. static inline void irq_time_write_end(void)
  1003. {
  1004. smp_wmb();
  1005. __this_cpu_inc(irq_time_seq.sequence);
  1006. }
  1007. static inline u64 irq_time_read(int cpu)
  1008. {
  1009. u64 irq_time;
  1010. unsigned seq;
  1011. do {
  1012. seq = read_seqcount_begin(&per_cpu(irq_time_seq, cpu));
  1013. irq_time = per_cpu(cpu_softirq_time, cpu) +
  1014. per_cpu(cpu_hardirq_time, cpu);
  1015. } while (read_seqcount_retry(&per_cpu(irq_time_seq, cpu), seq));
  1016. return irq_time;
  1017. }
  1018. #else /* CONFIG_64BIT */
  1019. static inline void irq_time_write_begin(void)
  1020. {
  1021. }
  1022. static inline void irq_time_write_end(void)
  1023. {
  1024. }
  1025. static inline u64 irq_time_read(int cpu)
  1026. {
  1027. return per_cpu(cpu_softirq_time, cpu) + per_cpu(cpu_hardirq_time, cpu);
  1028. }
  1029. #endif /* CONFIG_64BIT */
  1030. #endif /* CONFIG_IRQ_TIME_ACCOUNTING */